diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000000..2408084cf9 --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,6 @@ +# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json +reviews: + auto_review: + # Dependabot bumps are checked by CI; skip CodeRabbit reviews on them. + ignore_usernames: + - "dependabot[bot]" diff --git a/.config/nextest.toml b/.config/nextest.toml index 83ee9cd6f6..2ef6d6f215 100644 --- a/.config/nextest.toml +++ b/.config/nextest.toml @@ -1,3 +1,7 @@ +# Refuse to run under a nextest older than the fixed release: GHSA-vc25-qg58-w57c is fixed in +# 0.9.150, and CI installs exactly that version (an older nextest exits 92 REQUIRED_VERSION_NOT_MET). +nextest-version = "0.9.150" + # cargo-nextest (https://nexte.st): runs every test in its own process, in parallel across test # binaries. CI runs it with the `ci` profile below (#6117); locally it is optional and `cargo test` # keeps working (#2204). @@ -15,10 +19,29 @@ gpu = { max-threads = 4 } filter = 'package(photocraft-gpu) | (package(photocraft-ui-egui) & (kind(test) | test(/canvas|gpu|mip|type_tool|background_redraw::|floating_group_titles|swap_arrow::tests::rendered_glyph/)))' test-group = 'gpu' -# CI (`--profile ci`, .github/workflows/ci.yml). Stops at the first failure so a red PR frees its -# runner, fails a test that hangs instead of letting it hold the runner, and repeats every failure -# at the end of the log. The default profile's GPU override still applies. +# CI (`--profile ci`, .github/workflows/ci.yml). Collect all failures in one integration run +# instead of exposing one regression per rebuild. A hanging test still terminates, and every +# failure is repeated at the end of the log. The default profile's GPU override still applies. [profile.ci] -fail-fast = true +fail-fast = false slow-timeout = { period = "60s", terminate-after = 10 } failure-output = "immediate-final" + +# Keep phase timings for the remaining expensive Windows GPU regressions visible. This does +# not relax their timeout, isolate failures differently or remove any test cases. +# Page eviction and budget borrowing redraw multi-effect documents. On the Windows +# CPU-backed adapter it uses all GPU slots, avoiding oversubscription without skipping work. +[[profile.ci.overrides]] +platform = 'cfg(target_os = "windows")' +filter = 'package(photocraft-gpu) & test(/pages_are_evicted_under_the_budget|layer_pages_that_just_miss_borrow_the_effect_map_share/)' +threads-required = 4 +success-output = "immediate" + +[[profile.ci.overrides]] +platform = 'cfg(target_os = "windows")' +filter = 'package(photocraft-gpu) & test(/documents_larger_than_the_texture_limit|layer_effects_update_incrementally|pages_are_evicted_under_the_budget|the_focused_view_stays_resident_after_a_full_refresh/)' +success-output = "immediate" +# These multi-page effect regressions saturate the Windows software GPU. Reserve two +# scheduling slots so four independent devices do not compete for the same CPU renderer. +# All cases and the 60-second warning remain unchanged. +threads-required = 2 diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000000..2c4b8cb1a4 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,30 @@ +version: 2 +updates: + # Maintain Rust / Cargo dependencies + - package-ecosystem: "cargo" + directory: "/" + schedule: + interval: "daily" + time: "06:00" + timezone: "America/New_York" + # Consolidate updates into a single PR rather than one PR per crate + groups: + cargo-dependencies: + patterns: + - "*" + commit-message: + prefix: "chore(deps)" + + # Maintain GitHub Actions workflows + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" + time: "06:00" + timezone: "America/New_York" + groups: + actions: + patterns: + - "*" + commit-message: + prefix: "ci(deps)" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4303f0aac4..c15bc9542b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,40 @@ name: CI +# Quality gates for every push to main and every pull request. The Linux gate runs through the +# suite's shared gate workflow in craft-common-actions (`cargo xtask ci`); the Windows legs, the +# test variant and the corpus, web and fuzz jobs are PhotoCraft's own. on: push: branches: [main] pull_request: - types: [opened, synchronize, reopened, ready_for_review] + # `labeled` so that adding `full-ci` to an open PR starts a run with Windows in it. + types: [opened, synchronize, reopened, labeled] workflow_dispatch: inputs: + full_sweep: + description: "Run every testkit sweep case (runtime comparison and integration validation)" + type: boolean + default: false + color_range: + description: "Compare cropped Color Range on 24 MP (Linux, exact outputs)" + type: boolean + default: false + selection_combining: + description: "Compare direct GRAY8 selection combining on 36 MP (Linux, exact outputs)" + type: boolean + default: false + brush_tip: + description: "Compare cached round-tip rasterization on 24 MP" + type: boolean + default: false + brush_tip_base: + description: "Baseline commit for the round-tip comparison" + type: string + default: "main" + selection_morphology: + description: "Compare selection morphology on 36 MP (Linux, exact outputs)" + type: boolean + default: false fuzz: description: "Run the fuzz job" type: boolean @@ -14,59 +42,89 @@ on: fuzz_seconds: description: "Seconds per fuzz target" default: "600" + dry_mixer: + description: "Compare the live Mixer Brush against the origin/main merge base on 24 MP (Linux)" + type: boolean + default: false + +# The token is only used to check out the code: no job writes to the repository. +permissions: + contents: read env: CARGO_TERM_COLOR: always RUST_BACKTRACE: 1 + PHOTOCRAFT_FULL_SWEEP: ${{ inputs.full_sweep && '1' || '0' }} # No test or fuzz run may print a page: the engine refuses to spool, and if something ever # bypasses that, lp fails loudly on a printer that does not exist. PHOTOCRAFT_NO_PRINT: "1" LPDEST: photocraft-no-such-printer - # Debug and test builds keep line tables (file:line in panic backtraces) but drop the rest of the - # debug info: faster codegen and links, and a smaller target dir (the Linux test job ran out of - # disk with full debug info). The Windows test legs drop debuginfo entirely (see the test job). - CARGO_PROFILE_DEV_DEBUG: line-tables-only -# A newer push to a PR branch cancels its older run. Runs on main are never cancelled: a merge -# that lands while main's run is in progress waits for it, so every main state that gets a run is -# checked to the end. (Cancelling here left main unverified whenever merges came close together.) +# A newer push to a PR branch cancels its older run. Every push to main gets a run of its own that +# is never cancelled, so each state of main is checked to the end. Adding any label but `full-ci` +# gets a group of its own, so it neither runs anything nor cancels a run. concurrency: - group: ci-${{ github.ref }} + group: ci-${{ github.event_name == 'pull_request' && (github.event.action != 'labeled' || github.event.label.name == 'full-ci') && github.ref || github.run_id }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - # Which OSes the test job runs on. main, manual runs and PRs labelled `full-ci` get Linux and - # Windows. Other PRs get Linux, plus Windows when they touch code whose behaviour differs by - # platform: the app shell, UI, text and fonts, GPU, pen input, build scripts or the lockfile. - # macOS is tested only by release.yml. + # Whether the Windows legs and the web build run. main, manual runs and PRs labelled `full-ci` + # get both. Other PRs get Windows when they touch code whose behaviour differs by platform: the + # app shell, UI, text and fonts, GPU, pen input, native saves, build scripts or the lockfile. + # Format changes must exercise the Windows-only publication barriers (#8034). macOS is tested + # only by release.yml. platforms: + if: github.event.action != 'labeled' || github.event.label.name == 'full-ci' timeout-minutes: 10 name: select platforms runs-on: ubuntu-latest outputs: - legs: ${{ steps.pick.outputs.legs }} + windows: ${{ steps.pick.outputs.windows }} web: ${{ steps.pick.outputs.web }} steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 + persist-credentials: false + - name: Git dependencies come from storytold + run: | + set -euo pipefail + test -f Cargo.lock || { echo "Cargo.lock is missing"; exit 1; } + # Source replacement, patches and path overrides in cargo config never show up + # in Cargo.lock. Parsed rather than grepped: TOML has many spellings of one key. + # Cargo still reads the legacy .cargo/config. + python3 - <<'PY' + import os, sys, tomllib + for path in (".cargo/config", ".cargo/config.toml"): + if not os.path.exists(path): + continue + config = tomllib.load(open(path, "rb")) + replaced = [name for name, source in config.get("source", {}).items() + if isinstance(source, dict) and "replace-with" in source] + if replaced or "patch" in config or "paths" in config: + print(f"{path} replaces, patches or overrides a source") + sys.exit(1) + PY + # Fails when Cargo.lock is out of date with the manifests. + cargo metadata --locked --format-version 1 > /dev/null + python3 - <<'PY' + import sys, tomllib + lock = tomllib.load(open("Cargo.lock", "rb")) + bad = sorted({p["source"] for p in lock.get("package", []) + if p.get("source", "").startswith("git+") + and not p["source"].startswith("git+https://github.com/storytold/")}) + if bad: + print("Git dependencies outside github.com/storytold:", *bad, sep="\n") + sys.exit(1) + PY - id: pick env: EVENT: ${{ github.event_name }} BASE: ${{ github.event.pull_request.base.sha }} FULL: ${{ contains(github.event.pull_request.labels.*.name, 'full-ci') }} run: | - # No macOS on PRs or main: the org's 5 macOS slots are kept for release builds, and - # release.yml builds and tests macOS (a failing release is fixed and rebuilt). - # Windows tests run as two legs in parallel: the unit tests and the integration tests. As - # one job they took ~28 min on the Windows runner; the legs take ~13 and ~15. Splitting - # the integration tests over a third runner (nextest --partition) saved ~2 min of wall - # time but cost ~7 runner-minutes and one more Windows slot per PR. - linux='{"os":"ubuntu-latest","part":"all"}' - win() { printf '{"os":"windows-latest","part":"%s"}' "$1"; } - all="[$linux,$(win unit),$(win integration)]" if [ "$EVENT" != pull_request ] || [ "$FULL" = true ]; then - echo "legs=$all" >> "$GITHUB_OUTPUT" + echo "windows=true" >> "$GITHUB_OUTPUT" echo "web=true" >> "$GITHUB_OUTPUT" exit 0 fi @@ -78,67 +136,101 @@ jobs: else echo "web=false" >> "$GITHUB_OUTPUT" fi - if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(ui-egui|text|gpu|tablet|heif)/|Cargo\.lock$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then - echo "legs=$all" >> "$GITHUB_OUTPUT" + if git diff --name-only "$BASE" HEAD | grep -Eq '^(apps/|crates/(ui-egui|text|gpu|tablet|heif|format)/|Cargo\.lock$|(.*/)?build\.rs$|\.github/workflows/ci\.yml$)'; then + echo "windows=true" >> "$GITHUB_OUTPUT" else - echo "legs=[$linux]" >> "$GITHUB_OUTPUT" + echo "windows=false" >> "$GITHUB_OUTPUT" fi - test: - timeout-minutes: 100 - name: test (${{ matrix.os }}${{ matrix.part != 'all' && format(', {0}', matrix.part) || '' }}) + # The Linux gate. Its rust-cache entry is the one `test (without heif)` restores. + gate: + if: github.event.action != 'labeled' || github.event.label.name == 'full-ci' + # Tracks @main so suite-wide CI fixes reach every app without a PR in each repository. + uses: storytold/craft-common-actions/.github/workflows/gate.yml@main + with: + # egui/winit/rfd. + packages: >- + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev + libgtk-3-dev + # The tests build with the optional craft-fonts input (crates/text/build.rs), so the + # Japanese font tests that skip without it run. Same commit as release.yml. + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + # Cargo.toml's dev profile already keeps line tables only. + dev-debug: "" + env: | + PHOTOCRAFT_FULL_SWEEP=${{ inputs.full_sweep && '1' || '0' }} + PHOTOCRAFT_NO_PRINT=1 + LPDEST=photocraft-no-such-printer + timeout-minutes: 100 + # fmt, clippy, layers, scorecard, tests, panic_hunt, doctests and the wasm32 check, with + # `--locked --features heif`, then the missing-translations report (xtask/src/main.rs). + command: cargo xtask ci + + # Windows tests run as two legs in parallel: the unit tests and the integration tests. As one job + # they took ~28 min on the Windows runner; the legs take ~13 and ~15. Splitting the integration + # tests over a third runner (nextest --partition) saved ~2 min of wall time but cost ~7 + # runner-minutes and one more Windows slot per PR. They stay outside the shared gate for sccache, + # which needs its own action step. + windows: needs: platforms - runs-on: ${{ matrix.os }} + if: needs.platforms.outputs.windows == 'true' + timeout-minutes: 100 + name: test (windows-latest, ${{ matrix.part }}) + runs-on: windows-latest strategy: fail-fast: false matrix: - include: ${{ fromJSON(needs.platforms.outputs.legs) }} + part: [unit, integration] env: # No debuginfo on Windows: writing PDBs for ~200 test binaries cost 2.2 of ~21 build minutes. # A panic still names its file and line; only backtrace frames lose theirs (Linux keeps # line-tables-only, so a backtrace with lines is one OS away). - CARGO_PROFILE_DEV_DEBUG: ${{ startsWith(matrix.os, 'windows') && '0' || 'line-tables-only' }} + CARGO_PROFILE_DEV_DEBUG: "0" + # The all-language UI tests shape real Japanese, Chinese, Korean and Devanagari labels + # with kittest's strict `MissingGlyphPolicy::Panic`. A hosted Windows image has no + # guaranteed Japanese or Simplified Chinese system font, and egui 0.37 panics on a + # missing glyph; the app's lazy system fallback runs after layout, too late for the + # first headless frame. Use the same pinned central fonts as Linux and release, so every + # language is drawn by a real embedded face rather than skipping. Same commit as the gate + # above and release.yml. + CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts + CRAFT_FONTS_REQUIRED: "1" steps: - - uses: actions/checkout@v4 - - # Unused toolchains in the image (Android SDK, .NET, GHC, CodeQL) are deleted in the - # background: the space is free before the craft-fonts build needs it, and nothing waits. - - name: Install Linux system dependencies (egui/winit/rfd) - if: runner.os == 'Linux' - run: | - nohup sudo rm -rf /usr/local/lib/android /usr/share/dotnet /opt/ghc /usr/local/.ghcup \ - /opt/hostedtoolcache/CodeQL /dev/null 2>&1 & - sudo apt-get update - sudo apt-get install -y --no-install-recommends \ - libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev \ - libgl1-mesa-dev libgtk-3-dev - - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - components: clippy, rustfmt + persist-credentials: false - # Only main saves caches; PRs restore main's. Every PR used to save its own set (up to - # 3.8 GB), usable by that PR only, so the repo's 10 GB cache budget kept evicting main's. + - name: Check out craft-fonts + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: storytold/craft-fonts + ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + path: craft-fonts + persist-credentials: false + + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + with: + components: clippy + + # Only main saves caches; PRs restore main's, so the repo's 10 GB cache budget keeps main's. # A failing test still saves: the cache holds compiled dependencies, not test results. - # The Windows legs share one key (same dependencies); the integration leg saves it, - # because it also runs Clippy and so leaves Clippy's dependency metadata in it. - - uses: Swatinem/rust-cache@v2 + # Both legs share one key (same dependencies); the integration leg saves it, because it also + # runs Clippy and so leaves Clippy's dependency metadata in it. + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: - save-if: ${{ github.ref == 'refs/heads/main' && (matrix.part == 'all' || matrix.part == 'integration') }} + save-if: ${{ github.ref == 'refs/heads/main' && matrix.part == 'integration' }} cache-on-failure: true # sccache caches the workspace crates' compiled libraries, which rust-cache leaves out: # warm, each Windows leg built 4.5 min faster. Like rust-cache, only main writes (PRs read # main's entries); a full set is ~90 MB, so it barely touches the 10 GB budget (#2688). # Test executables can't be cached, so a PR still links its tests. - - name: Install sccache (Windows) - if: runner.os == 'Windows' + - name: Install sccache uses: Mozilla-Actions/sccache-action@fc920bf0ec8de6ee65d409111f7ec508035751ba # v0.0.11 with: version: v0.18.0 - - name: Use sccache (Windows) - if: runner.os == 'Windows' + - name: Use sccache env: MAIN: ${{ github.ref == 'refs/heads/main' }} run: | @@ -147,139 +239,80 @@ jobs: if [ "$MAIN" != true ]; then echo "SCCACHE_GHA_RW_MODE=READ_ONLY" >> "$GITHUB_ENV"; fi shell: bash - - name: Format - if: runner.os == 'Linux' - run: cargo fmt --all -- --check - - # Keep formatting, strict Clippy, layering and scorecard checks ahead of tests. - name: Clippy - if: matrix.part == 'all' || matrix.part == 'integration' + if: matrix.part == 'integration' run: cargo clippy --workspace --all-targets --locked --features heif -- -D warnings - name: Dependency layering - if: matrix.part == 'all' || matrix.part == 'integration' + if: matrix.part == 'integration' run: cargo xtask layers - # docs/scorecard.md must match its sources (checklists, budgets, baseline, corpus floors, - # prefs audit). Static: it reads committed files only, no benchmarks run here. - - name: Scorecard up to date - if: runner.os == 'Linux' - run: cargo xtask scorecard --check - - # nextest runs the test binaries in parallel (cargo test runs them one at a time), with the - # GPU tests capped by the `gpu` group in .config/nextest.toml (#2204). - - uses: taiki-e/install-action@83ac0ad63c0167e6f06796fab0fce28db1bf3db0 # v2.87.22 + - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 with: - tool: cargo-nextest@0.9.146 + tool: cargo-nextest@0.9.150 - # `--features heif`: official builds open HEIC photos via the optional photocraft-heif crate. - # `--lib --bins --tests` leaves out the examples, which cargo test would build and link but - # never run (Clippy's `--all-targets` still checks they compile). nextest skips doctests, so - # they get their own step. - # - # On Windows the unit leg runs `--lib --bins` and the integration leg `--test '*'` (every + # The unit leg runs `--lib --bins` and the integration leg `--test '*'` (every # integration-test target); the feature selection stays the same, so both legs share the - # dependency cache. The `ci` profile fails fast and terminates hung tests - # (.config/nextest.toml); assertion failures are not retried. + # dependency cache. - name: Test env: PART: ${{ matrix.part }} run: | case "$PART" in unit) targets=(--lib --bins) ;; - integration) targets=(--test '*') ;; - *) targets=(--lib --bins --tests) ;; + *) targets=(--test '*') ;; esac cargo nextest run --profile ci --workspace --locked --features heif "${targets[@]}" shell: bash - name: Doctests - if: matrix.part == 'all' || matrix.part == 'unit' + if: matrix.part == 'unit' run: cargo test --workspace --locked --features heif --doc - # The Windows tests used to be one job named `test (windows-latest)`; they're now legs named - # `test (windows-latest, unit)` and `test (windows-latest, integration)`. This job keeps the old - # name reporting, so a required status check on it (none today) would keep working. It fails - # unless the whole test matrix and both Linux variants passed. `needs` reports one result - # for all matrix legs; when a PR runs no Windows legs it still checks every Linux result. - windows-gate: - timeout-minutes: 5 - name: test (windows-latest) - needs: [platforms, test, test-variants] - if: ${{ !cancelled() }} - runs-on: ubuntu-latest + # The same codecs without the optional decoder: HEIC must stay a clear "not in this build" error + # for distributors who leave it out. Runs beside the gate and restores the gate's dependency + # cache without saving one: the dependencies are the same, and the 10 GB cache budget is tight + # (#2688). + test-without-heif: + if: github.event.action != 'labeled' || github.event.label.name == 'full-ci' + timeout-minutes: 60 + name: test (without heif) + # The gate's runner: its cache entry is keyed on the OS. + runs-on: ubuntu-24.04 + env: + # The gate sets these; rust-cache hashes CARGO_* and RUST* variables into its key. + CARGO_INCREMENTAL: 0 + CARGO_NET_RETRY: 10 steps: - - env: - PLATFORMS: ${{ needs.platforms.result }} - TEST: ${{ needs.test.result }} - VARIANTS: ${{ needs.test-variants.result }} - run: | - echo "platforms: $PLATFORMS, test: $TEST, variants: $VARIANTS" - [ "$PLATFORMS" = success ] && [ "$TEST" = success ] && [ "$VARIANTS" = success ] - - # The two other builds of the Linux tests, in parallel with the test job instead of after it (they - # added ~10 min to its critical path). Each restores the test job's cache (`shared-key: test` is - # that job's key) and saves none: the dependencies are the same, and the 10 GB cache budget is - # tight (#2688). - test-variants: - timeout-minutes: 100 - name: test (${{ matrix.variant }}) - runs-on: ubuntu-latest - strategy: - fail-fast: false - matrix: - variant: [without heif, with craft-fonts] - steps: - - uses: actions/checkout@v4 - - - name: Install Linux system dependencies (egui/winit/rfd) - run: | - nohup sudo rm -rf /usr/local/lib/android /usr/share/dotnet /opt/ghc /usr/local/.ghcup \ - /opt/hostedtoolcache/CodeQL /dev/null 2>&1 & - sudo apt-get update - sudo apt-get install -y --no-install-recommends \ - libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev \ - libgl1-mesa-dev libgtk-3-dev - - - uses: dtolnay/rust-toolchain@stable - - - uses: Swatinem/rust-cache@v2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - shared-key: test + persist-credentials: false + + - name: Install Linux build dependencies + uses: storytold/craft-common-actions/actions/install-apt-packages@main + with: + packages: >- + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev + libgtk-3-dev + + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + + # gate.yml keys its cache `-rust--gate`. + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + prefix-key: v1-rust + shared-key: ${{ hashFiles('Cargo.toml') }}-gate save-if: false - - uses: taiki-e/install-action@83ac0ad63c0167e6f06796fab0fce28db1bf3db0 # v2.87.22 + - uses: taiki-e/install-action@8cca0918f8471c33515202b73f56ab27f2bed283 # v2.87.29 with: - tool: cargo-nextest@0.9.146 + tool: cargo-nextest@0.9.150 - # The same codecs without the optional decoder: HEIC must stay a clear "not in this build" - # error for distributors who leave it out. - name: Test without heif - if: matrix.variant == 'without heif' run: cargo nextest run -p photocraft-heif -p photocraft-codecs -p photocraft-io --locked --profile ci - # The tests built with the optional craft-fonts input (crates/text/build.rs), so the Japanese - # font tests that skip without it run. Keep the test job's exact `--workspace --features heif - # --lib --bins --tests`, so the restored dependencies match and only the workspace rebuilds. - # Filter execution to the three crates that read the optional font input (#6127). - - name: Check out craft-fonts - if: matrix.variant == 'with craft-fonts' - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 - with: - repository: storytold/craft-fonts - ref: abb83316d96aa59c1cf64784289e378fe9fa5695 - path: craft-fonts - - - name: Test with craft-fonts - if: matrix.variant == 'with craft-fonts' - env: - CRAFT_FONTS_DIR: ${{ github.workspace }}/craft-fonts - CRAFT_FONTS_REQUIRED: "1" - run: >- - cargo nextest run --profile ci --workspace --locked --features heif --lib --bins --tests - -E 'package(photocraft-text) | package(photocraft-io) | package(photocraft-ui-egui)' - corpus: + if: github.event.action != 'labeled' || github.event.label.name == 'full-ci' timeout-minutes: 30 # Real-file corpus tests (cargo feature `corpus`), on every PR and on main. The corpora are # pinned in xtask/src/corpus_pins.rs and sha256-verified (xtask/*.sha256) after both a cache @@ -295,15 +328,17 @@ jobs: CARGO_PROFILE_RELEASE_LTO: "false" CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16" steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: key: corpus save-if: ${{ github.ref == 'refs/heads/main' }} cache-on-failure: true - name: Restore test corpora - uses: actions/cache@v4 + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: path: corpus/ key: corpus-${{ hashFiles('xtask/src/corpus_pins.rs', 'xtask/*.sha256') }} @@ -312,23 +347,6 @@ jobs: - name: Corpus tests run: cargo xtask test-corpus - wasm: - timeout-minutes: 30 - name: wasm32 check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable - with: - targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2 - with: - key: wasm32 - save-if: ${{ github.ref == 'refs/heads/main' }} - cache-on-failure: true - - name: Check wasm-safe crates - run: cargo xtask wasm - web: timeout-minutes: 60 name: web build and wasm size gate @@ -340,18 +358,20 @@ jobs: # Match release.yml so PR results reflect what would actually ship. TRUNK_VERSION: 0.21.14 steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable with: targets: wasm32-unknown-unknown - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: key: ci-web save-if: ${{ github.ref == 'refs/heads/main' }} cache-on-failure: true - name: Install trunk run: | - curl -fsSL "https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz" \ + curl --proto '=https' -fsSL "https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}/trunk-x86_64-unknown-linux-gnu.tar.gz" \ | tar -xz -C "$HOME/.cargo/bin" trunk trunk --version # package.sh runs Trunk's release builds, validates relative URLs, cuts the wasm into @@ -398,6 +418,7 @@ jobs: - { crate: psd, name: metadata } - { crate: psd, name: tagged } - { crate: psd, name: resources } + - { crate: psd, name: asl } - { crate: codecs, name: decode_any } - { crate: codecs, name: decode_png } - { crate: codecs, name: decode_jpeg } @@ -408,19 +429,237 @@ jobs: - { crate: affinity, name: import_preview, args: "--features io" } - { crate: format, name: load } steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@nightly - - uses: Swatinem/rust-cache@v2 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # nightly + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: workspaces: crates/${{ matrix.target.crate }}/fuzz - name: Install cargo-fuzz run: cargo install cargo-fuzz --locked - name: Fuzz working-directory: crates/${{ matrix.target.crate }} - run: cargo fuzz run ${{ matrix.target.name }} ${{ matrix.target.args }} -- -max_total_time=${{ inputs.fuzz_seconds }} + env: + FUZZ_SECONDS: ${{ inputs.fuzz_seconds }} + run: cargo fuzz run ${{ matrix.target.name }} ${{ matrix.target.args }} -- -max_total_time="$FUZZ_SECONDS" - name: Upload crash artifacts if: failure() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: fuzz-${{ matrix.target.crate }}-${{ matrix.target.name }} path: crates/${{ matrix.target.crate }}/fuzz/artifacts + + color-range-performance: + if: inputs.color_range == true + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + RAYON_NUM_THREADS: "4" + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install Linux build dependencies + uses: storytold/craft-common-actions/actions/install-apt-packages@main + with: + packages: >- + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev + libgtk-3-dev + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: color-range-perf + - name: Compare exact outputs and release timings on 24 MP + run: | + set -o pipefail + cargo test --release -p photocraft-engine --lib color_range_release_comparison -- --ignored --nocapture | tee color-range-perf.txt + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: always() + with: + name: color-range-performance + path: color-range-perf.txt + + selection-morphology: + if: inputs.selection_morphology == true + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + RAYON_NUM_THREADS: "4" + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install Linux build dependencies + uses: storytold/craft-common-actions/actions/install-apt-packages@main + with: + packages: >- + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev + libgtk-3-dev + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: selection-morphology-perf + - name: Compare exact outputs and release timings on 36 MP + run: | + set -o pipefail + cargo run --release -p photocraft-algo --example bench_selection_morphology | tee selection-morphology-perf.txt + - name: Compare cropped command reads and writeback on 36 MP + run: | + set -o pipefail + cargo test --release -p photocraft-engine --lib sparse_modify_release_comparison -- --ignored --nocapture | tee -a selection-morphology-perf.txt + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: always() + with: + name: selection-morphology-performance + path: selection-morphology-perf.txt + + selection-morphology-quick-perf: + if: inputs.selection_morphology == true || inputs.color_range == true || inputs.selection_combining == true || inputs.brush_tip == true || inputs.dry_mixer == true + name: quick performance suite + # Match perf-nightly.yml: Linux llvmpipe lacks the float render targets needed by + # bevel_bench, so its required GPU rows cannot be measured there. + runs-on: macos-15 + timeout-minutes: 60 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: perf-release + - name: Quick performance suite + run: cargo xtask perf --quick + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: always() + with: + name: selection-morphology-quick-perf + path: | + target/perf/*.json + target/perf/summary.md + + selection-combining: + if: inputs.selection_combining == true + runs-on: ubuntu-latest + timeout-minutes: 30 + env: + RAYON_NUM_THREADS: "4" + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + key: selection-combining-perf + - name: Compare exact outputs and release timings on 36 MP + run: | + set -o pipefail + cargo test --release -p photocraft-algo --lib gray8_combining_release_comparison -- --ignored --nocapture | tee selection-combining-perf.txt + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: always() + with: + name: selection-combining-performance + path: selection-combining-perf.txt + + brush-tip: + if: inputs.brush_tip == true + runs-on: ubuntu-24.04 + timeout-minutes: 30 + env: + RAYON_NUM_THREADS: "4" + steps: + - uses: actions/checkout@v7 + with: + persist-credentials: false + path: after + - uses: actions/checkout@v7 + with: + persist-credentials: false + ref: ${{ inputs.brush_tip_base }} + path: before + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 + with: + workspaces: | + before -> target + after -> target + key: brush-tip-release + - name: Build the same benchmark against both versions + run: | + cp after/crates/paint/examples/big_dab_bench.rs before/crates/paint/examples/big_dab_bench.rs + cargo build --release --locked --manifest-path before/Cargo.toml -p photocraft-paint --example big_dab_bench + cargo build --release --locked --manifest-path after/Cargo.toml -p photocraft-paint --example big_dab_bench + - name: Compare round and transformed tips on 24 MP + run: | + set -o pipefail + for size in 200 2000; do + for angle in 0 0.7; do + for version in before after; do + echo "$version: size=$size angle=$angle" | tee -a brush-tip.txt + "$version/target/release/examples/big_dab_bench" "$size" 0.37 "$angle" | tee -a brush-tip.txt + done + done + done + - uses: actions/upload-artifact@v7 + if: always() + with: + name: brush-tip-performance + path: brush-tip.txt + dry-mixer: + if: inputs.dry_mixer == true + runs-on: ubuntu-latest + timeout-minutes: 90 + env: + RAYON_NUM_THREADS: "4" + CARGO_PROFILE_RELEASE_LTO: "false" + CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "16" + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Pin baseline to the merge base with origin/main + id: baseline + run: | + git remote add upstream https://github.com/storytold/photocraft.git + git fetch --no-tags upstream main + base=$(git merge-base HEAD upstream/main) + echo "sha=$base" >> "$GITHUB_OUTPUT" + git worktree add --detach "$RUNNER_TEMP/photocraft-baseline" "$base" + mkdir -p "$RUNNER_TEMP/photocraft-baseline/crates/engine/examples" + cp crates/engine/examples/bench_dry_mixer.rs "$RUNNER_TEMP/photocraft-baseline/crates/engine/examples/bench_dry_mixer.rs" + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable + - name: Install Linux build dependencies + uses: storytold/craft-common-actions/actions/install-apt-packages@main + with: + packages: >- + libxkbcommon-dev libwayland-dev libx11-dev libxrandr-dev libxi-dev libgl1-mesa-dev + libgtk-3-dev + - name: Benchmark baseline and candidate on this runner + run: | + set -o pipefail + echo "baseline=${{ steps.baseline.outputs.sha }}" | tee dry-mixer-perf.txt + echo "candidate=${GITHUB_SHA}" | tee -a dry-mixer-perf.txt + CARGO_TARGET_DIR="$RUNNER_TEMP/target-baseline" cargo run --release --manifest-path "$RUNNER_TEMP/photocraft-baseline/Cargo.toml" -p photocraft-engine --example bench_dry_mixer | tee baseline-mixer.txt + CARGO_TARGET_DIR="$RUNNER_TEMP/target-candidate" cargo run --release -p photocraft-engine --example bench_dry_mixer | tee candidate-mixer.txt + cat baseline-mixer.txt candidate-mixer.txt >> dry-mixer-perf.txt + python3 - <<'PY' + import re + def fingerprints(path): + rows = open(path).readlines() + result = {} + for row in rows: + match = re.search(r'wet=\s*(\d+)% sample_all=\s*(\w+)\s+size=\s*(\d+).*blake3=(\w+)', row) + if match: + result[match.group(1, 2, 3)] = match.group(4) + return result + before, after = fingerprints('baseline-mixer.txt'), fingerprints('candidate-mixer.txt') + assert len(before) == 12 and before == after, 'baseline/candidate mixer output or state differs' + PY + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + if: always() + with: + name: dry-mixer-performance + path: dry-mixer-perf.txt diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 67e6b31894..e93efc1018 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -31,8 +31,8 @@ jobs: name: mdBook build runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: dtolnay/rust-toolchain@stable + - uses: actions/checkout@v7 + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable - name: Install mdBook run: cargo install mdbook --version 0.4.52 --locked - name: Build documentation diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml index 970c6577a4..2b98d8c11d 100644 --- a/.github/workflows/freebsd.yml +++ b/.github/workflows/freebsd.yml @@ -9,6 +9,8 @@ name: FreeBSD # or dependency change: a drifted dependency still shows up within a day. PRs that change the # FreeBSD packaging itself still run it. on: + push: + branches: [release] schedule: - cron: "17 2 * * *" pull_request: @@ -30,7 +32,9 @@ jobs: # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. uses: storytold/craft-common-actions/.github/workflows/freebsd.yml@main with: - craft-fonts-ref: abb83316d96aa59c1cf64784289e378fe9fa5695 + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 cargo-build-extra-args: "--features heif" - custom-test-command: "CARGO_PROFILE_DEV_DEBUG=0 cargo test --workspace --locked --features heif --exclude photocraft-ui-egui && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --lib && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --doc && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --test '*' -- --test-threads=1" + # The VM's software Vulkan driver crashes (SIGSEGV) when several tests set up a device at + # once, so photocraft-gpu's tests run one at a time, like ui-egui's integration tests. + custom-test-command: "CARGO_PROFILE_DEV_DEBUG=0 cargo test --workspace --locked --features heif --exclude photocraft-ui-egui --exclude photocraft-gpu && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-gpu --locked -- --test-threads=1 && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --lib && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --doc && CARGO_PROFILE_DEV_DEBUG=0 cargo test -p photocraft-ui-egui --locked --test '*' -- --test-threads=1" run-package-check: true diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml index 18392587a8..01cb9cd663 100644 --- a/.github/workflows/nix.yml +++ b/.github/workflows/nix.yml @@ -18,16 +18,6 @@ concurrency: 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: | - if [ -f flake.nix ]; then - nix flake check --no-build --all-systems - else - echo "No flake.nix present in this repository; skipping check." - fi + nix: + # Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos. + uses: storytold/craft-common-actions/.github/workflows/nix.yml@main diff --git a/.github/workflows/packaging-lint.yml b/.github/workflows/packaging-lint.yml index 1d110e28f2..6848088d37 100644 --- a/.github/workflows/packaging-lint.yml +++ b/.github/workflows/packaging-lint.yml @@ -29,3 +29,27 @@ jobs: steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - run: bash packaging/linux/verified-download-test.sh + + windows-install-policy: + timeout-minutes: 10 + name: Windows installation policy (no app build) + runs-on: windows-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Install the pinned packaging toolchain + shell: pwsh + run: | + $tools = Join-Path $env:RUNNER_TEMP 'craft-install-policy-wix' + dotnet tool install --tool-path $tools wix --version 5.0.2 + if ($LASTEXITCODE -ne 0) { throw 'WiX installation failed.' } + $tools >> $env:GITHUB_PATH + $policy = Get-Content -Raw -LiteralPath packaging/windows/install-policy.json | ConvertFrom-Json + foreach ($extension in @($policy.extensions)) { + & (Join-Path $tools 'wix.exe') extension add -g "WixToolset.$extension.wixext/5.0.2" + if ($LASTEXITCODE -ne 0) { throw "WiX extension $extension installation failed." } + } + - name: Build and check non-executable installer fixtures + shell: pwsh + run: ./packaging/windows/test-install-policy.ps1 -OutputDirectory (Join-Path $env:RUNNER_TEMP 'craft-install-policy-tests') diff --git a/.github/workflows/perf-nightly.yml b/.github/workflows/perf-nightly.yml index 10d22292d2..71afc03e14 100644 --- a/.github/workflows/perf-nightly.yml +++ b/.github/workflows/perf-nightly.yml @@ -4,10 +4,13 @@ name: Perf (nightly) # Metal), every night and on demand. It never runs on pull requests, so it can't block one. # It fails when an enforced budget in perf/budgets.toml breaks or a scenario's p50 regresses # more than 15 % against perf/baseline.json from the same machine class (other classes are -# never compared). The table goes to the job summary; results.json is uploaded as an artifact. +# never compared; a baseline built from several runs is judged against its highest p50). The +# table goes to the job summary; results.json is uploaded as the `perf-results` artifact. # -# To give this runner class its own baseline: run it by hand with "update_baseline", download -# the `perf-baseline` artifact and commit it as perf/baseline.json in a PR. +# To give this runner class its own baseline: download the `perf-results` artifact of the last +# five or more nightly runs (`gh run download -n perf-results -D runs/`), run +# `cargo xtask perf --update-baseline --from runs//results.json ... --allow-contended` (this +# runner is never idle) and commit perf/baseline.json and docs/scorecard.md in a PR. on: schedule: @@ -40,13 +43,13 @@ jobs: runs-on: macos-15 timeout-minutes: 120 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 with: persist-credentials: false - - uses: dtolnay/rust-toolchain@stable + - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable - - uses: Swatinem/rust-cache@v2 + - uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3 with: key: perf-release @@ -58,7 +61,8 @@ jobs: run: | args=() if [ "$QUICK" = "true" ]; then args+=(--quick); fi - if [ "$UPDATE" = "true" ]; then args+=(--update-baseline); fi + # This runner is never idle: a baseline from it is recorded labelled as contended. + if [ "$UPDATE" = "true" ]; then args+=(--update-baseline --allow-contended); fi cargo xtask perf "${args[@]}" - name: Job summary @@ -72,7 +76,7 @@ jobs: - name: Upload results if: always() - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: perf-results path: | @@ -83,7 +87,7 @@ jobs: - name: Upload baseline if: always() && inputs.update_baseline - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: perf-baseline path: perf/baseline.json diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bfdca9e3e9..4b78fac526 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -24,7 +24,7 @@ jobs: with: app-name: photocraft app-title: PhotoCraft - craft-fonts-ref: abb83316d96aa59c1cf64784289e378fe9fa5695 + craft-fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 version: ${{ inputs.version }} secrets: APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }} diff --git a/.github/workflows/sonarcloud.yml b/.github/workflows/sonarcloud.yml new file mode 100644 index 0000000000..8f1aa5ad52 --- /dev/null +++ b/.github/workflows/sonarcloud.yml @@ -0,0 +1,24 @@ +name: SonarCloud + +on: + push: + branches: [main] + pull_request: + types: [opened, synchronize, reopened] + +permissions: + contents: read + +concurrency: + group: sonarcloud-${{ github.ref }} + # A push to main is the run that publishes coverage; superseded runs there would never finish. + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + +jobs: + sonarcloud: + # Track shared CI improvements on main. + uses: storytold/craft-common-actions/.github/workflows/sonarcloud.yml@main + with: + timeout-minutes: 120 + fonts-ref: b076417b5880bd4154bd22f00695b1d28c6bea83 + secrets: inherit diff --git a/.github/workflows/windows-arm64.yml b/.github/workflows/windows-arm64.yml index 30a07f4cd5..fe092e498a 100644 --- a/.github/workflows/windows-arm64.yml +++ b/.github/workflows/windows-arm64.yml @@ -7,6 +7,8 @@ name: Windows ARM64 # Nightly on main (dependency and runner-image drift shows up within a day), and on PRs that # change the Windows packaging itself. on: + push: + branches: [release] pull_request: paths: - .github/workflows/windows-arm64.yml @@ -29,3 +31,5 @@ jobs: with: app-name: photocraft app-dir: PhotoCraft + install-relative-path: Programs/ArtCraft/PhotoCraft + install-scope: perUser diff --git a/.gitignore b/.gitignore index f310f32921..0e71c973d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *~ -plan/ +/plan/ /target target/ /dist diff --git a/AGENTS.md b/AGENTS.md index 97b5ccdbc0..e849cb4506 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -19,7 +19,7 @@ PhotoCraft is an open-source, native, Photoshop-comparable image editor written | `docs/parity-checklist.md` | Generated list of every Photoshop menu item, live or missing | | `docs/scorecard.md` | Generated scorecard: performance budgets and numbers, corpus floors, per-area checklists (tools, files, UI, type, automation, reliability, distribution), settings that do nothing | | `crates//README.md` (where present) | Public API of that crate | -| [photocraft-corpus](https://github.com/storytold/photocraft-corpus) + `docs/development.md` › Test corpora | Real-file test oracles (our Photoshop-authored PSDs); with psd-tools, ag-psd and PngSuite fetched into `corpus/` by `cargo xtask corpus --all` at the pins in `xtask/src/corpus_pins.rs` | +| [photocraft-corpus](https://github.com/storytold/photocraft-corpus) + `docs/development.md` › Test corpora | Real-file test oracles (our Photoshop-authored PSDs and GIMP-authored XCFs); with psd-tools, ag-psd and PngSuite fetched into `corpus/` by `cargo xtask corpus --all` at the pins in `xtask/src/corpus_pins.rs` | ## 2. Workspace map @@ -27,7 +27,7 @@ PhotoCraft is an open-source, native, Photoshop-comparable image editor written crates/ geom cms color raster L0 foundation (geometry, ICC colour management, pixel formats + blend math, COW tiles) psd codecs L0 standalone format crates (no workspace deps; publishable) - tablet L0 standalone pen tablet input (macOS AppKit, X11 XInput2); the one isolated unsafe crate + tablet L0 standalone pen tablet input (macOS AppKit, X11 XInput2, Windows barrel button); the one isolated unsafe crate doc L1 document model (layers, masks, adjustments, effects, smart objects: pure data) ops paint algo text vector L2 history, brush engine, imaging algorithms, type engine, paths/shapes compose gpu format L3 CPU compositor (the oracle), wgpu compositor, .pcraft native format @@ -52,13 +52,13 @@ xtask/ cargo xtask layers | wasm | ci | stats | corpus | t People trust PhotoCraft with their work, and a crash loses it. A malformed file, a bad command or MCP param, a corrupt settings file, an odd keystroke or a full disk must produce an error the user or agent can act on, never a panic. Don't ship a feature by adding a panic path; fix a crash before building on top of it. The shared standard is `../craftrules/standards/never-crash.md`. - **Non-test code never panics.** No `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!` or `unimplemented!`. Return the crate's error type and propagate with `?`; use `ok_or(..)?`, `let .. else { return Err(..) }`, `if let`, or `unwrap_or*` where a fallback is truly correct (never one that silently corrupts a document). Unfinished features return an "unsupported" error. The only exception is a provably infallible literal: `#[allow(clippy::expect_used)]` plus `.expect("why it can't fail")`. -- **No `unsafe`.** The workspace sets `unsafe_code = "forbid"`. The one exception is the isolated helper crate `photocraft-tablet` (`crates/tablet`): winit drops pen tablet data, and reading it on macOS needs an AppKit event monitor (Objective-C interop). Only its `src/macos.rs` allows `unsafe` (`unsafe_code = "deny"` crate-wide, every block has a `SAFETY:` comment, tested against real `NSEvent`s); its X11 path and all mapping code are safe. Don't add `unsafe` anywhere else. +- **No `unsafe`.** The workspace sets `unsafe_code = "forbid"`. The one exception is the isolated helper crate `photocraft-tablet` (`crates/tablet`): winit drops pen tablet data, and reading it on macOS needs an AppKit event monitor (Objective-C interop) and on Windows a pointer message hook (`GetPointerPenInfo`). Only its `src/macos.rs` and `src/windows.rs` allow `unsafe` (`unsafe_code = "deny"` crate-wide, every block has a `SAFETY:` comment, tested against real `NSEvent`s and pointer messages); its X11 path and all mapping code are safe. Don't add `unsafe` anywhere else. - **Input-derived numbers are hostile.** Use `get()` rather than `[i]`/`[a..b]` for indices from files, params, selections or arithmetic on them; slice strings only at char boundaries; use `checked_*`/`saturating_*` for lengths, offsets and counts; guard division by zero and NaN/inf casts; cap allocations sized by input. - **Bound recursion** with depth limits or seen-sets (documents can be deep or cyclic). - **Don't cascade.** Handle lock poisoning (`lock().unwrap_or_else(PoisonError::into_inner)`) and treat thread joins as `Result`s. - **Last-resort guard.** The app shell must catch an escaped panic around command dispatch and file import/export, reports it as an error and keeps the document. It's a safety net, not a licence to panic. Keep `panic = "unwind"`. - **Prove it.** Every crash fix comes with a small synthetic regression test that panicked before the fix. -- **Enforced by clippy.** `clippy.toml` allows `unwrap`/`expect`/`panic`/indexing in tests only. Clean crates carry `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`; new crates start with it. +- **Enforced by clippy.** `clippy.toml` allows `unwrap`/`expect`/`panic` in tests only. Clean production crate roots carry `#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]`; every new workspace crate must also include `[lints] workspace = true` in `Cargo.toml` to inherit `[workspace.lints]`. 1. **Everything is a command.** New user-visible behaviour = a command in the engine (`crates/engine/src/*_cmds.rs`, registered in `commands.rs`) with id, label, menu path, shortcut, params doc, `enabled` and `run`, plus tests. The UI, CLI, control channel and MCP all dispatch commands by id. Use the **exact id from `crates/ui-egui/src/menu_catalog.rs`** and the menu item goes live automatically. Only pure view/window state (zoom, panels, screen mode) belongs to the shell (`menus.rs` `UI_COMMANDS`). 2. **No format or colour assumptions.** Bit depth (8/16/32f) and colour model (RGB/Gray/CMYK/Lab…) are runtime data. Never introduce a `u8`-only pixel path in public APIs. Never assume sRGB: colour conversions go through `photocraft-cms` (`Transform`, `transform::cached`). Test at several depths. @@ -70,6 +70,7 @@ People trust PhotoCraft with their work, and a crash loses it. A malformed file, 8. **Performance is a feature.** Benchmark heavy operations on a 24–36 MP image in release. Work per tile in parallel (rayon), skip empty tiles, never scan a full surface per frame (cache per revision), and record before/after timings in the dev log. 9. **Never panic on input** (see *Never crash* above). A command's `run` closure and anything it calls must return `Err`, never panic, for *any* params or document state: validate params, check bounds before indexing or dividing, and reject absurd sizes before allocating. The `panic_hunt` integration test fuzzes every command with adversarial params and must stay green. +10. **Time goes through `chrono`** (workspace dependency). Never hand-roll UTC/calendar conversion (days-to-date algorithms, leap-year math); a crate owns only its output format string. The one exception is range extension: past the years chrono can represent, shift the date by whole 400-year Gregorian cycles (146,097 days, after which the calendar repeats), let chrono compute the month and day, and add the cycles back to the year (splitting seconds into hours, minutes and seconds is plain arithmetic). ## 4. Picking work @@ -107,7 +108,7 @@ cargo xtask test-corpus # if you touched psd, io, codecs, compose, gpu, text While iterating, run less and keep the full commands above as the final gate: `cargo test -p --lib --tests` skips the examples and doctests plain `cargo test` builds every time (ui-egui has 11 examples), and `cargo test -p --test ` runs one integration-test file. `cargo nextest run` runs tests in parallel across binaries; see `docs/development.md` › Faster test loops. -**Test corpora.** Real-file corpora live in `corpus/` (gitignored, never committed), fetched at pinned commits and sha256-verified by `cargo xtask corpus --all`: our Photoshop-authored oracles from https://github.com/storytold/photocraft-corpus plus psd-tools, ag-psd and PngSuite from their upstreams. Pins: `xtask/src/corpus_pins.rs`. The corpus tests are opt-in (cargo feature `corpus`): plain `cargo test` skips them, and with the feature on a missing corpus fails ("run `cargo xtask corpus --all`"). `cargo xtask test-corpus` fetches and runs them all. CI always runs them (the `corpus` job, cached by pin). Never commit corpus files; new oracles go to photocraft-corpus (its `AGENTS.md`), then a pin bump here. Details: `docs/development.md` › Test corpora. +**Test corpora.** Real-file corpora live in `corpus/` (gitignored, never committed), fetched at pinned commits and sha256-verified by `cargo xtask corpus --all`: our Photoshop- and GIMP-authored oracles from https://github.com/storytold/photocraft-corpus plus psd-tools, ag-psd and PngSuite from their upstreams. Pins: `xtask/src/corpus_pins.rs`. The corpus tests are opt-in (cargo feature `corpus`): plain `cargo test` skips them, and with the feature on a missing corpus fails ("run `cargo xtask corpus --all`"). `cargo xtask test-corpus` fetches and runs them all. CI always runs them (the `corpus` job, cached by pin). Never commit corpus files; new oracles go to photocraft-corpus (its `AGENTS.md`), then a pin bump here. Details: `docs/development.md` › Test corpora. Commands must **never panic** on bad input (Rule 9): every `run` closure and the code it calls returns `Err`, not a panic, for any params or document state. New commands come with a graceful-failure test (empty/out-of-range/wrong-type params → `Err`, not a crash). @@ -157,3 +158,8 @@ If the field has a PSD equivalent, map it in `crates/io` too, and keep unknown P GitHub profiles). Never hand-edit `contributors.json`. - Maintainers refresh the stats with `python3 ../../craftrules/scripts/contributors.py .` (it also re-verifies who wrote each `people.toml` entry; `--check` only verifies). + +## CI workflows +- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here. +- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time. +- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it. diff --git a/ATTRIBUTION.md b/ATTRIBUTION.md index 26eca62464..66cd9f1307 100644 --- a/ATTRIBUTION.md +++ b/ATTRIBUTION.md @@ -23,26 +23,39 @@ next to them. | `crates/ui-egui/src/i18n/cs.tsv` | Czech UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | | `crates/ui-egui/src/i18n/hu.tsv` | Hungarian UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | | `crates/ui-egui/src/i18n/vi.tsv` | Vietnamese UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/ar.tsv` | Arabic UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | | `crates/ui-egui/src/i18n/tr.tsv` | Turkish UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | | `crates/ui-egui/src/i18n/sr.tsv` | Serbian (Latin, Ekavian) UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/ru.tsv` | Russian UI translations | PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/sv.tsv` | Swedish UI translations | @kler, @davvarn and PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/fi.tsv` | Finnish UI translations | @Janiz-zzz, @metuuu and PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/da.tsv` | Danish UI translations | @pechnikov2026-star and PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `crates/ui-egui/src/i18n/nb.tsv` | Norwegian Bokmål UI translations | @nikolainyegaard, @Bolla76 and PhotoCraft contributors | Original translations of PhotoCraft's English labels | MIT OR Apache-2.0, [`LICENSE-translations.txt`](crates/ui-egui/src/i18n/LICENSE-translations.txt) | +| `epaint_default_fonts::EGUI_ICONS` (build input; deflated into `OUT_DIR/egui_fonts/egui-icons.ttf.deflate`, registered by `crates/ui-egui/src/egui_fonts.rs`) | egui-icons, five-glyph subset of emoji-icon-font | John Slegers | [egui fork at 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [original](https://github.com/jslegers/emoji-icon-font); egui subset, unmodified here | MIT; Copyright (c) 2014 John Slegers; full upstream notice in [NOTICE](NOTICE) | | `assets/fonts/Inter-Regular.ttf`, `Inter-Medium.ttf`, `Inter-SemiBold.ttf` | Inter 4.001 (UI font) | The Inter Project Authors (Rasmus Andersson) | | SIL OFL 1.1, [`assets/fonts/OFL-Inter.txt`](assets/fonts/OFL-Inter.txt) | | `assets/fonts/JetBrainsMono-Regular.ttf` | JetBrains Mono 2.305 (numeric font) | The JetBrains Mono Project Authors | | SIL OFL 1.1, [`assets/fonts/OFL-JetBrainsMono.txt`](assets/fonts/OFL-JetBrainsMono.txt) | -| `assets/icons/*.svg` except `slice-knife.svg`, `sampler.svg`, `artboard.svg`, `perspective-crop.svg`, `frame-placeholder.svg`, `gradient.svg`, `type-mask.svg`, `type-mask-vertical.svg`, `clip-below.svg`, `clip-release.svg`, `layer-mask.svg`, `adjustment-layer.svg`, `brush-replace.svg`, `quick-selection.svg`, `eraser-background.svg`, `eraser-magic.svg`, `lasso-magnetic.svg`, `direct-select.svg`, `convert-point.svg`, `pen-add-anchor.svg`, `pen-delete-anchor.svg`, `pen-freeform.svg`, `pen-curvature.svg`, `bandage-sparkle.svg`, `painting-symmetry.svg`, `symmetry-*.svg` and `marquee-{row,column}.svg` (113 files; `star-filled.svg` is `star.svg` with a solid fill) | Lucide icons | Lucide Icons and Contributors | (`icons/.svg`; `align-*` are now named `text-align-*` upstream) | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | +| `assets/icons/*.svg` except `slice-knife.svg`, `slice-select.svg`, `font-similar.svg`, `font-variable.svg`, `sampler.svg`, `artboard.svg`, `perspective-crop.svg`, `frame-placeholder.svg`, `ellipse-dashed.svg`, `gradient.svg`, `type-mask.svg`, `type-mask-vertical.svg`, `text-warp.svg`, `clip-below.svg`, `clip-release.svg`, `layer-mask.svg`, `adjustment-layer.svg`, `brush-replace.svg`, `quick-selection.svg`, `eraser-background.svg`, `eraser-magic.svg`, `lasso-magnetic.svg`, `direct-select.svg`, `convert-point.svg`, `pen-add-anchor.svg`, `pen-delete-anchor.svg`, `pen-freeform.svg`, `pen-curvature.svg`, `bandage-sparkle.svg`, `painting-symmetry.svg`, `symmetry-*.svg`, `path-op-*.svg` and `marquee-{row,column}.svg`, `align-edge-*.svg` and `distribute-*.svg` (114 files; `star-filled.svg` is `star.svg` with a solid fill, `rotate-ccw.svg` is `rotate-cw.svg` mirrored) | Lucide icons | Lucide Icons and Contributors | (`icons/.svg`; `align-*` are now named `text-align-*` upstream) | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/{check,chevron-down,chevron-left,chevron-right,chevron-up,chevrons-left,chevrons-right,circle,clock,compass,info,link,link-2,lock,minus,monitor,moon,move,navigation,plus,search,square,trash,triangle,type,x,zoom-in}.svg` | Lucide icons derived from Feather (subset of the row above) | Cole Bemis (Feather), Lucide Contributors | via Lucide | MIT (Feather) and ISC (Lucide), [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/slice-knife.svg` | Slice tool glyph | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/slice-select.svg` | Slice Select tool glyph | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/sampler.svg` | Color Sampler tool glyph: a target reticle marking a sampled pixel | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/font-similar.svg`, `assets/icons/font-variable.svg` | Font menu filter glyphs: two waves (Show Similar Fonts) and a T over a slider (Show Variable Fonts), drawn on the 24 px icon grid | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/convert-point.svg` | Convert Point tool glyph: an angle with a square anchor on its apex, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/artboard.svg` | Artboard tool glyph: a board with a tick at each end of its edges | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/perspective-crop.svg` | Perspective Crop tool glyph: a crop frame in perspective with its grid and crop-mark ticks | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/type-mask.svg`, `assets/icons/type-mask-vertical.svg` | Horizontal and Vertical Type Mask tool glyphs: a T drawn as marching ants (the vertical one beside a downward arrow), on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/path-op-{new-layer,combine,subtract,intersect,exclude,merge}.svg` | Path Operations glyphs (Pen options bar): a single square for New Layer, and two overlapping squares showing the area each operation keeps (union, back minus front, overlap, everything but the overlap, the merged outline), drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/text-warp.svg` | Create Warped Text glyph (Type tool options bar): a T above a bowed arc, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/marquee-row.svg`, `assets/icons/marquee-column.svg` | Single Row and Single Column Marquee tool glyphs: a dashed row (column) between two edge bars, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/clip-below.svg`, `assets/icons/clip-release.svg` | Clipping-mask cursors (⌥ over the line between two layers): a hooked arrow onto the layer below; the same with a small "no" badge to release | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/layer-mask.svg`, `assets/icons/adjustment-layer.svg` | Layers panel footer glyphs: Add a mask (a frame around a solid disc) and New Fill or Adjustment Layer (a circle, one half solid), drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/align-edge-*.svg`, `assets/icons/distribute-*.svg` | Move tool options-bar glyphs: Align (six edges/centres) and Distribute (edges, centres, spacing), a guide line or edge lines with bars, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | +| `assets/icons/ellipse-dashed.svg` | Elliptical Marquee tool glyph: a dashed horizontal ellipse, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/frame-placeholder.svg` | Frame tool glyph: an empty placeholder frame, crossed corner to corner | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/gradient.svg` | Gradient tool glyph: a rounded rectangle filled with a left-to-right linear ramp, drawn on Lucide's 24 px grid and stroke | PhotoCraft contributors | Original work | MIT OR Apache-2.0 | | `assets/icons/eraser-background.svg`, `assets/icons/eraser-magic.svg` | Background Eraser and Magic Eraser tool glyphs: Lucide `eraser` plus added marks | Lucide Icons and Contributors; marks by PhotoCraft contributors | Derived from `icons/eraser.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/brush-replace.svg` | Color Replacement tool glyph: Lucide `brush` plus two swap arrows | Lucide Icons and Contributors; arrows by PhotoCraft contributors | Derived from `icons/brush.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | -| `assets/icons/quick-selection.svg` | Quick Selection tool glyph: Lucide `brush` plus a dashed selection arc around the bristles | Lucide Icons and Contributors; arc by PhotoCraft contributors | Derived from `icons/brush.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | +| `assets/icons/quick-selection.svg` | Quick Selection tool glyph: Lucide `brush`, scaled to 70%, inside a dashed upright rounded "C" selection | Lucide Icons and Contributors; selection by PhotoCraft contributors | Derived from `icons/brush.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/bandage-sparkle.svg` | Remove tool glyph: Lucide `bandage` moved down, plus a sparkle mark | Lucide Icons and Contributors; mark by PhotoCraft contributors | Derived from `icons/bandage.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/direct-select.svg` | Direct Selection tool glyph: Lucide `mouse-pointer-2` plus an anchor square | Lucide Icons and Contributors; square by PhotoCraft contributors | Derived from `icons/mouse-pointer-2.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | | `assets/icons/pen-freeform.svg`, `assets/icons/pen-curvature.svg` | Freeform Pen and Curvature Pen tool glyphs: Lucide `pen-tool` scaled down, plus a freehand squiggle or a curve through two points | Lucide Icons and Contributors; marks by PhotoCraft contributors | Derived from `icons/pen-tool.svg` | ISC, [`assets/icons/LICENSE-lucide.txt`](assets/icons/LICENSE-lucide.txt) | @@ -57,6 +70,7 @@ next to them. | `crates/ui-egui/src/theme.rs` (Solarized Dark palette) | Solarized colour scheme used by the Solarized Dark theme | Ethan Schoonover | | MIT, [`LICENSE-solarized.txt`](crates/ui-egui/src/LICENSE-solarized.txt) | | `crates/ui-egui/src/theme.rs` (Breeze Light and Breeze Dark themes) | The Breeze Light and Breeze Dark themes are original colour values on PhotoCraft's Studio layout, chosen to look close to KDE Plasma's Breeze (cool blue-greys, blue accent, 5 px frame radius) so the app blends into a Plasma desktop. None of Breeze's colour values, files or code are copied; the values are a few levels away from its schemes and tuned for text contrast | PhotoCraft contributors | Inspired by the look of KDE Breeze, (its colour schemes are LGPL-2.0-or-later) | Original work | | `crates/ui-egui/src/theme.rs` (Soft Dark and Soft Light themes) | Original colour values and radii for two low-contrast themes on PhotoCraft's Studio layout; no third-party palette, file or code is used | PhotoCraft contributors | Original | Original work | +| `crates/algo/data/libfacedetection.bin` (face detection weights, read by `crates/algo/src/faces.rs`, whose network code is a port of the library's) | libfacedetection CNN (YuNet design) weights and inference code, weights converted to half precision | Shiqi Yu | [ShiqiYu/libfacedetection](https://github.com/ShiqiYu/libfacedetection) (`src/facedetectcnn-data.cpp`, `facedetectcnn.cpp`, `facedetectcnn-model.cpp`) | BSD-3-Clause, [`LICENSE-libfacedetection.txt`](crates/algo/data/LICENSE-libfacedetection.txt) | ### Optional build input: craft-fonts (not files in this repo) @@ -82,6 +96,8 @@ The other built-in ICC profiles and the generated LUT looks are produced by code | `docs/brand/` (all files) | ArtCraft name, wordmark and mark | ArtCraft Team | getartcraft.com | Not open source; trademarks of the ArtCraft Team, [`docs/brand/LICENSE-brand.txt`](docs/brand/LICENSE-brand.txt) | | `docs/images/photocraft-*.jpg` | PhotoCraft screenshots | PhotoCraft contributors (UI) | Rendered offscreen with the `snapshot` example | MIT OR Apache-2.0 (UI); the artwork in each is public domain, listed below | | `docs/images/preferences-apply-*.png` | Preferences before and after adding Apply (no artwork) | PhotoCraft contributors | PhotoCraft control-channel capture and offscreen `snapshot` example | MIT OR Apache-2.0 | +| `docs/images/gradient-overlay-method-*.png` | Gradient Overlay Method selector, before and after (synthetic gradient) | @oRE-o | Rendered offscreen with the `snapshot` example; no external artwork | MIT OR Apache-2.0 | +| `docs/images/cpu-pixel-alignment/*.png` | CPU canvas pixel alignment, before and after #8321 | PhotoCraft contributors | Original synthetic noise rendered with the offscreen `snapshot` example; see the adjacent README | MIT OR Apache-2.0, adjacent `LICENSE-MIT` and `LICENSE-APACHE` | Artwork shown in the screenshots (all public domain, via Wikimedia Commons; details in [`docs/images/SOURCES.md`](docs/images/SOURCES.md)): @@ -107,12 +123,14 @@ next to the files: | Path (fetched) | Title | Author | Source | License | |---|---|---|---|---| -| `corpus/photoshop/` (256 PSDs) | Photoshop oracle corpus: smart filters, layer-style effects, type, adjustments in every mode and depth | PhotoCraft contributors (authored with Adobe Photoshop 2026 by a script) | [https://github.com/storytold/photocraft-corpus](https://github.com/storytold/photocraft-corpus) (`photoshop/`, with its generator and README) | MIT OR Apache-2.0 | +| `corpus/photoshop/` (258 PSDs) | Photoshop oracle corpus: smart filters, layer-style effects, type, adjustments in every mode and depth | PhotoCraft contributors (authored with Adobe Photoshop 2026 by a script) | [https://github.com/storytold/photocraft-corpus](https://github.com/storytold/photocraft-corpus) (`photoshop/`, with its generator and README) | MIT OR Apache-2.0 | +| `corpus/gimp/` (28 files, 251 KB) | GIMP XCF oracles: 40×30 procedural layers written by GIMP 3.2.6 through the corpus generator `tools/gimp-xcf/xcf_fixtures.py`, each with GIMP's merged rendering as a PNG | PhotoCraft contributors (authored with GIMP 3.2.6 by a script) | [https://github.com/storytold/photocraft-corpus](https://github.com/storytold/photocraft-corpus) (`gimp/`, with its generator and README) | MIT OR Apache-2.0 | | `corpus/psd-tools/` (309 files) | psd-tools test set | Kota Yamaguchi and contributors | [psd-tools `tests/psd_files`](https://github.com/psd-tools/psd-tools/tree/main/tests/psd_files) | MIT, Copyright (c) 2019 Kota Yamaguchi | | `corpus/psd/` (170 files) | Small selection of the psd-tools and ag-psd test files | Kota Yamaguchi; Agamnentzar | psd-tools (above) and [ag-psd `test/`](https://github.com/Agamnentzar/ag-psd/tree/master/test) | MIT (both) | | `corpus/heif/` (9 files, 0.1 MB) | HEIC/HEIF test files: `heic-rs/` checkerboards, RGB strips and a grid-tiled photo with EXIF and XMP (synthetic pixels encoded by macOS `sips`, each `.ref.png` Apple's decode); `pillow-heif/` the 10-bit RGBA `RGBA_10__29x100.heif` and its source `RGBA_16__29x100.png` | Thomas Braun (heic-rs); Pillow-Heif contributors | [heic-rs `tests/fixtures`](https://github.com/tbraun96/heic-rs/tree/main/tests/fixtures), [pillow-heif `tests/images`](https://github.com/bigcat88/pillow_heif/tree/master/tests/images) | MIT OR Apache-2.0 (heic-rs); BSD-3-Clause (pillow-heif) | | `corpus/exr/` (5 files, 2.3 MB) | The deep OpenEXR test images `11`, `42`, `64`, `multivariate` and `objectid.deep.exr` | Contributors to the OpenEXR Project | [openexr `src/test/bin/test_images`](https://github.com/AcademySoftwareFoundation/openexr/tree/main/src/test/bin/test_images) | BSD-3-Clause | | `corpus/affinity/` (60 files) | Public Affinity documents and PNG references: `vector-art/` (9 documents), `afdesignload/` (9), `jac21/` (2), `asset-store-template/` (1), and `affinity-samples/` (18 CC0 documents plus exported references) | samuel-etver; Nick Beeuwsaert; Jeremy Cantu; DAIKI (eviltwo); satoshoe-dev | [vector-art `simple/`](https://github.com/samuel-etver/vector-art/tree/255f8add3c8f0740196e22bd59502b811b532f0b/simple), [AFDesignLoad `testDesigns/`](https://github.com/NickBeeuwsaert/AFDesignLoad/tree/a18dd50a7079fb861a28835eeefa0d015f03f4a1/testDesigns), [Jac21/Branding `Logos/JC/DesignerFiles/Affinity`](https://github.com/Jac21/Branding/tree/57ae3f45bf4637f6927c0b07de25b922c55f944f/Logos/JC/DesignerFiles/Affinity), [AssetStoreTemplate `AssetStoreTemplate/`](https://github.com/eviltwo/AssetStoreTemplate/tree/f671981ee89d15526285b3cc87b4393d3defe644/AssetStoreTemplate), [affinity-samples](https://github.com/satoshoe-dev/affinity-samples/tree/96aca6bd86bbe7a677f1cf0630bd832d23943be8) | CC0-1.0 (vector-art and affinity-samples); MIT, Copyright (c) 2015 Nick Beeuwsaert; MIT, Copyright (c) 2018 Jeremy Cantu; MIT, Copyright (c) 2024 DAIKI | +| `corpus/atn/` (16 files, 0.9 MB) | Photoshop Actions sets, one folder per repository: `sbaril/` (Frequency Separation, Firefly, Dimension, Harmonize, Photo, Quick Object Extraction, Generative Upscale), `bjango/` (Bjango Actions, Adjustment Actions, Scale Actions), `bkwld/` (Save layers for web), `atntools/` (test.atn), `trimmer/` (Trimmer), `alpha-utility/` (AlphaUtilityAdditions), `gif-creator/` (BannerActions) | Stéphane Baril; Bjango and Marc Edwards; BKWLD; Maurits Rijk; damsmcfly; UCLA Game Lab; overbyte | [sbaril/Photoshop-Scripts](https://github.com/sbaril/Photoshop-Scripts), [bjango/Bjango-Actions](https://github.com/bjango/Bjango-Actions), [BKWLD/photoshop-actions](https://github.com/BKWLD/photoshop-actions), [mrijk/atntools](https://github.com/mrijk/atntools), [damsmcfly/photoshop-cleaner-trimmer](https://github.com/damsmcfly/photoshop-cleaner-trimmer), [uclagamelab/AlphaUtilityAdditions](https://github.com/uclagamelab/AlphaUtilityAdditions), [overbyte/photoshop-gif-creator-script](https://github.com/overbyte/photoshop-gif-creator-script), pinned in [xtask/src/corpus_pins.rs](xtask/src/corpus_pins.rs) | MIT (sbaril, BKWLD, atntools, gif-creator), BSD (bjango, per `Help/License.md`), BSD-2-Clause (alpha-utility), Unlicense (trimmer); each licence file sits next to its files | | `corpus/pngsuite/` | PngSuite | Willem van Schaik | | Public domain | | `corpus/pixls/` (11 files) | Real camera raws, one per decode path: Canon PowerShot SX50 HS (CR2 and its DNG conversion), Nikon D3 uncompressed NEF and Z6 paired 12/14-bit lossy and packed NEFs, Sony DSC-RX0 cRAW ARW, Panasonic DC-G9 high-res RW2, Olympus E-1 GRBG ORF, plus the E-5 packed ORF as the documented known-unsupported case. Per file (each CC0, per its database entry): [IMG_4059.CR2](https://raw.pixls.us/data/Canon/PowerShot%20SX50%20HS/IMG_4059.CR2), [CRW_4061.DNG](https://raw.pixls.us/data/Canon/PowerShot%20SX50%20HS/CRW_4061.DNG), [JD1_8203.NEF](https://raw.pixls.us/data/Nikon/D3/JD1_8203.NEF), [DSC00009.ARW](https://raw.pixls.us/data/Sony/DSC-RX0/DSC00009.ARW), [P1000475.RW2](https://raw.pixls.us/data/Panasonic/DC-G9/P1000475.RW2), [E_1__C106743_gredos.ORF](https://raw.pixls.us/data/Olympus/E-1/E_1__C106743_gredos.ORF), [_7061961_copy.ORF](https://raw.pixls.us/data/Olympus/E-5/_7061961_copy.ORF), [DSC_0750.NEF](https://raw.pixls.us/data/Nikon/Z%206/DSC_0750.NEF), [DSC_0751.NEF](https://raw.pixls.us/data/Nikon/Z%206/DSC_0751.NEF), [DSC_0753.NEF](https://raw.pixls.us/data/Nikon/Z%206/DSC_0753.NEF), [DSC_0754.NEF](https://raw.pixls.us/data/Nikon/Z%206/DSC_0754.NEF) | The photographers of the pixls.us raw samples database | [raw.pixls.us](https://raw.pixls.us) `data/`, fetched per [xtask/src/corpus_pins.rs](xtask/src/corpus_pins.rs) | CC0 / public domain, per file as linked | diff --git a/Cargo.lock b/Cargo.lock index 138ca3d814..8186c52112 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 4 [[package]] name = "accesskit" -version = "0.24.1" +version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3b7f7f85a7e5f68090000ed7622545829afd484d210358702ae4cb97dd0c320" +checksum = "ad442f58ee04714aaa0ba0a2768c1ea1935b29507bb22ddece8cbbc76db02932" dependencies = [ "enumn", "serde", @@ -15,12 +15,12 @@ dependencies = [ [[package]] name = "accesskit_atspi_common" -version = "0.18.1" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e8c61bee90b42a772d39d06a740207dc71a4e780004ace1db8d99fb1baaa954" +checksum = "52c182f9c282ac9c5638d876d551d15e5f7d397ec263349a0c6a2b61595dd5e4" dependencies = [ "accesskit", - "accesskit_consumer 0.36.0", + "accesskit_consumer", "atspi-common", "phf", "serde", @@ -29,43 +29,37 @@ dependencies = [ [[package]] name = "accesskit_consumer" -version = "0.35.0" +version = "0.39.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53cf47daed85312e763fbf85ceca136e0d7abc68e0a7e12abe11f48172bc3b10" +checksum = "8a45e2bb300bea6d5f33a82c3e051c3ddc6f41c209750f68a0acbe991a284344" dependencies = [ "accesskit", - "hashbrown 0.16.1", + "hashbrown 0.17.1", ] [[package]] -name = "accesskit_consumer" -version = "0.36.0" +name = "accesskit_ios" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25e0d7e25d06f4dc21d1774d67146e9e80d6789216cbd4d1e88185b0095dba60" +checksum = "28e539d64b27fd4c14de8f78ada8f7c944c092f52f7e49fb63316507a57f6937" dependencies = [ "accesskit", - "hashbrown 0.16.1", -] - -[[package]] -name = "accesskit_consumer" -version = "0.38.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d10a236f96f87d70732e44520046785431ef01d5bcd6b041317bfadd2f88245" -dependencies = [ - "accesskit", - "hashbrown 0.16.1", + "accesskit_consumer", + "hashbrown 0.17.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", + "objc2-ui-kit 0.2.2", ] [[package]] name = "accesskit_macos" -version = "0.26.3" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce02dc63b43f0c9296af9ac946312a2dc8814427d7a64d2d600971dac55b6076" +checksum = "5701624bf6a11efb6d4c8e922bf44126ed96250e5faa9a7a0468c979635839a4" dependencies = [ "accesskit", - "accesskit_consumer 0.38.0", - "hashbrown 0.16.1", + "accesskit_consumer", + "hashbrown 0.17.1", "objc2 0.5.2", "objc2-app-kit 0.2.2", "objc2-foundation 0.2.2", @@ -73,9 +67,9 @@ dependencies = [ [[package]] name = "accesskit_unix" -version = "0.21.1" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b016ca8db0ea0ea2ceff29a9d6240391492d960716aa471967c00e8cc8cb197c" +checksum = "202f24df034a7476d07b7f74284de84f6d62aabd858dbe7ee9cad3b7ad6f8f9d" dependencies = [ "accesskit", "accesskit_atspi_common", @@ -91,13 +85,13 @@ dependencies = [ [[package]] name = "accesskit_windows" -version = "0.32.1" +version = "0.35.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eff7009f1a532e917d66970a1e80c965140c6cfbbabbdde3d64e5431e6c78e21" +checksum = "ce63f35d6bdcf59f26b76b3379063f738e6412cef46999cc772d46aa3de35adb" dependencies = [ "accesskit", - "accesskit_consumer 0.35.0", - "hashbrown 0.16.1", + "accesskit_consumer", + "hashbrown 0.17.1", "static_assertions", "windows", "windows-core", @@ -105,11 +99,12 @@ dependencies = [ [[package]] name = "accesskit_winit" -version = "0.32.2" +version = "0.34.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fe9a94394896352cc4660ca2288bd4ef883d83238853c038b44070c8f134313" +checksum = "ee96dd150375c05912d8a473f98c2fb155fb3234fac88292d396548a2460fa77" dependencies = [ "accesskit", + "accesskit_ios", "accesskit_macos", "accesskit_unix", "accesskit_windows", @@ -179,6 +174,15 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "alloca" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5a7d05ea6aea7e9e64d25b9156ba2fee3fdd659e34e41063cd2fc7cd020d7f4" +dependencies = [ + "cc", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -241,12 +245,56 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.104" @@ -261,21 +309,21 @@ checksum = "c3d036a3c4ab069c7b410a2ce876bd74808d2d0888a82667669f8e783a898bf1" [[package]] name = "arboard" -version = "3.6.1" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0348a1c054491f4bfe6ab86a7b6ab1e44e45d899005de92f58b3df180b36ddaf" +checksum = "aeb9553e62312c8756a5979254fc6d7da8bbeb5986a1e17037fab932f7037917" dependencies = [ "clipboard-win", "image", "log", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-foundation", "objc2-core-graphics", "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.60.2", + "windows-sys 0.61.2", "wl-clipboard-rs", "x11rb", ] @@ -611,15 +659,6 @@ dependencies = [ "syn 2.0.119", ] -[[package]] -name = "bit-set" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08807e080ed7f9d5433fa9b275196cfc35414f66a0c79d864dc51a0d825231a3" -dependencies = [ - "bit-vec 0.8.0", -] - [[package]] name = "bit-set" version = "0.10.0" @@ -630,10 +669,13 @@ dependencies = [ ] [[package]] -name = "bit-vec" -version = "0.8.0" +name = "bit-set" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e764a1d40d510daf35e07be9eb06e75770908c27d411ee6c92109c9840eaaf7" +checksum = "56d87354e4229f54a44f7bf2435906a4656dba36026ab6eaca629a2c436a691c" +dependencies = [ + "bit-vec 0.10.1", +] [[package]] name = "bit-vec" @@ -641,6 +683,16 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" +[[package]] +name = "bit-vec" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5727b15fa97d4f4fee0a3b7c3d550ed0269f54329207b86388de918604e31269" +dependencies = [ + "borsh", + "serde", +] + [[package]] name = "bit_field" version = "0.10.3" @@ -714,7 +766,7 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", ] [[package]] @@ -730,6 +782,30 @@ dependencies = [ "piper", ] +[[package]] +name = "borsh" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "553c5d846a6ba5150c65e3b1b8ec073bcf1abc20f9b7220de384a4443ea4e20a" +dependencies = [ + "borsh-derive", + "bytes", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12cdfe656708a01f89b451a7d36466e6fe6c414de0aa18fc54f864f6f9ca9f56" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 3.0.6", +] + [[package]] name = "brotli-decompressor" version = "5.0.3" @@ -849,7 +925,7 @@ dependencies = [ "maybe-owned", "rustix 1.1.5", "rustix-linux-procfs", - "windows-sys 0.60.2", + "windows-sys 0.61.2", "winx", ] @@ -993,6 +1069,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -1001,8 +1078,31 @@ version = "4.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_complete" +version = "4.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "037e2a1a92236d0aff7e845093f64661d6df4c02c9fcc61a60e9e1d736fa392f" +dependencies = [ + "clap", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 3.0.6", ] [[package]] @@ -1011,6 +1111,16 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" +[[package]] +name = "clap_mangen" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "211d617eaa4b735c96c9e0228fcbdb5120ef623f2b8cb67ffb84c3e02dbc28a4" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "clipboard-win" version = "5.4.1" @@ -1053,6 +1163,15 @@ dependencies = [ "x509-cert", ] +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "codespan-reporting" version = "0.13.1" @@ -1079,6 +1198,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + [[package]] name = "colored" version = "2.2.0" @@ -1222,14 +1347,55 @@ dependencies = [ "libc", ] +[[package]] +name = "craft-docpath" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" + +[[package]] +name = "craft-fonts-build" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" + +[[package]] +name = "craft-i18n" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +dependencies = [ + "fixed_decimal", + "icu_decimal", + "icu_locale_core", + "icu_locale_fallback", + "icu_normalizer", + "icu_plurals", + "icu_properties", + "icu_provider", + "icu_provider_adapters", + "icu_provider_blob", + "log", + "objc2-foundation 0.3.2", + "sys-locale", + "unicode-bidi", + "windows-registry", + "writeable", +] + +[[package]] +name = "craft-jpeg" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=3d37da389a3618ab9b76109da11416e9ca4ea719#3d37da389a3618ab9b76109da11416e9ca4ea719" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" dependencies = [ "craft-winprint-gdi", - "miniz_oxide 0.8.9", - "roxmltree", + "miniz_oxide 0.9.1", + "roxmltree 0.21.1", "tempfile", "winprint", "winsafe", @@ -1238,10 +1404,48 @@ dependencies = [ [[package]] name = "craft-ui" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=5c874d51eeb3de70e735c8ce87a59a7dedd47675#5c874d51eeb3de70e735c8ce87a59a7dedd47675" +source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0" dependencies = [ + "craft-fonts-build", + "craft-i18n", "egui", + "epaint", + "js-sys", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-foundation 0.3.2", "serde", + "skrifa", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "craft-update" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=04aa1703b32d299e54f78fcde4182f2b393a9116#04aa1703b32d299e54f78fcde4182f2b393a9116" +dependencies = [ + "chrono", + "dunce", + "egui", + "flate2", + "getrandom 0.3.4", + "image", + "log", + "plist", + "semver", + "serde", + "serde_json", + "sha2", + "tar", + "tempfile", + "ureq", + "verifysign", + "winreg", + "winsafe", + "zip", ] [[package]] @@ -1254,32 +1458,31 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.5.2" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78" +checksum = "0ba106e844045ddc02c20768ac4b0b7cb45f2cc2ecbd833ac9f5dacb183c2055" dependencies = [ "cfg-if", ] [[package]] name = "criterion" -version = "0.5.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" +checksum = "950046b2aa2492f9a536f5f4f9a3de7b9e2476e575e05bd6c333371add4d98f3" dependencies = [ + "alloca", "anes", "cast", "ciborium", "clap", "criterion-plot", - "is-terminal", - "itertools 0.10.5", + "itertools 0.13.0", "num-traits", - "once_cell", "oorandom", + "page_size", "regex", "serde", - "serde_derive", "serde_json", "tinytemplate", "walkdir", @@ -1287,12 +1490,12 @@ dependencies = [ [[package]] name = "criterion-plot" -version = "0.5.0" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +checksum = "d8d80a2f4f5b554395e47b5d8305bc3d27813bacb73493eb1001e8f76dae29ea" dependencies = [ "cast", - "itertools 0.10.5", + "itertools 0.13.0", ] [[package]] @@ -1427,6 +1630,12 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + [[package]] name = "dify" version = "0.8.0" @@ -1465,7 +1674,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.6.2", "libc", - "objc2 0.6.4", + "objc2 0.6.5", ] [[package]] @@ -1578,9 +1787,8 @@ checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" [[package]] name = "ecolor" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0656a7530b9e2841ee7a5f983965bdd9840c32f4012f759cfd44b912cced6b34" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", "emath", @@ -1589,9 +1797,8 @@ dependencies = [ [[package]] name = "eframe" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55ad41fac6e3149abb4866e93df37dbd33baa95db525c3368c944584e95a5dc2" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -1605,7 +1812,7 @@ dependencies = [ "image", "js-sys", "log", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", "parking_lot", @@ -1614,6 +1821,7 @@ dependencies = [ "profiling", "raw-window-handle", "ron", + "send_wrapper", "serde", "static_assertions", "wasm-bindgen", @@ -1626,9 +1834,8 @@ dependencies = [ [[package]] name = "egui" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc938cc27cd911415e1e4d151bc56ff9df063d8db081374e87aeb805ea74b2ba" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "accesskit", "ahash", @@ -1648,9 +1855,8 @@ dependencies = [ [[package]] name = "egui-wgpu" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cef1f6d1bba40b09a4d0d62cb822b81030922cfbf65c5373398c3a3caafc5bd" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -1668,31 +1874,34 @@ dependencies = [ [[package]] name = "egui-winit" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98466000559d66ba4db786a30448b01483946add4188e7f10f522a27b4fb89cc" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "accesskit_winit", "arboard", + "block2 0.6.2", "bytemuck", "egui", + "image", "log", - "objc2 0.6.4", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", "objc2-ui-kit 0.3.2", + "parking_lot", "profiling", "raw-window-handle", "serde", "smithay-clipboard", "web-time", + "windows-sys 0.61.2", "winit", ] [[package]] name = "egui_extras" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05cbdc785020861553f230f23d330ba1fb72613b9d8e0e457f3bdc8ba6db6dcf" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "egui", @@ -1705,9 +1914,8 @@ dependencies = [ [[package]] name = "egui_glow" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "606ed4167daf341e5753d703285c4c1d562324b2c07aa8f405cd7d066fe58f3b" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", "egui", @@ -1719,9 +1927,8 @@ dependencies = [ [[package]] name = "egui_kittest" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eb3f3effb2726fee23449293e67acbf6cc6ce35525b1722bd5743bb348114d5" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "dify", "eframe", @@ -1746,9 +1953,8 @@ checksum = "252afb9ae5eaa683babdc6a068b3f5726eb19e05070c731f9b2a23a7c3e8ed34" [[package]] name = "emath" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52c8d4141cf0f60fb2f7aefaf352d3ffa5171ef0ab09731c64391801e95e48f1" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "bytemuck", "serde", @@ -1776,22 +1982,22 @@ checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" [[package]] name = "enum-map" -version = "2.7.3" +version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6866f3bfdf8207509a033af1a75a7b08abda06bbaaeae6669323fd5a097df2e9" +checksum = "d4532960034c15ba2f55e9eee631040ecc473c32e6c4b200828c759840f13a06" dependencies = [ "enum-map-derive", ] [[package]] name = "enum-map-derive" -version = "0.17.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f282cfdfe92516eb26c2af8589c274c7c17681f5ecc03c18255fe741c6aa64eb" +checksum = "7127272f64b3e098f7d81f350d672081b879bff896d219c42370dc858b1af9ff" dependencies = [ "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.6", ] [[package]] @@ -1828,9 +2034,8 @@ dependencies = [ [[package]] name = "epaint" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a0ec0308dc23130fc1a7d4623a8534c2ed069c6dc84e258ee4f3de99615aa1a3" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" dependencies = [ "ahash", "bytemuck", @@ -1847,16 +2052,17 @@ dependencies = [ "serde", "skrifa", "smallvec", + "unicode-bidi", "unicode-general-category", + "unicode-properties", "unicode-segmentation", "vello_cpu", ] [[package]] name = "epaint_default_fonts" -version = "0.36.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773fa9c96dd0dbef887e39d0ed6177f141cce4d68e6041df77570aa3702dfa13" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" [[package]] name = "equator" @@ -1891,7 +2097,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1929,6 +2135,18 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "exacl" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de9e551ced9d700a31dacb8b168129640240fcd7a279a3fa4d59608b84ebcae" +dependencies = [ + "bitflags 2.13.2", + "log", + "scopeguard", + "uuid", +] + [[package]] name = "exr" version = "1.74.2" @@ -2003,6 +2221,17 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484" +[[package]] +name = "fixed_decimal" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2214fb00f669ee96bee5bb29dfb1ddabe058b2bc426817efc79afa03c9bc55ae" +dependencies = [ + "displaydoc", + "smallvec", + "writeable", +] + [[package]] name = "fixedbitset" version = "0.5.7" @@ -2060,7 +2289,7 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90642cf66dde7f205dc8b42dfe57b47d0f3a29f571b832742883ef512e23fe55" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", ] @@ -2099,7 +2328,7 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbc773e24e02d4ddd8395fd30dc147524273a83e54e0f312d986ea30de5f5646" dependencies = [ - "roxmltree", + "roxmltree 0.20.0", ] [[package]] @@ -2124,9 +2353,17 @@ dependencies = [ "hashbrown 0.17.1", "linebender_resource_handle", "memmap2", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-text", + "objc2-foundation 0.3.2", "parlance", "read-fonts", + "roxmltree 0.21.1", "smallvec", + "windows", + "windows-core", + "yeslogic-fontconfig-sys", ] [[package]] @@ -2417,9 +2654,9 @@ dependencies = [ [[package]] name = "glifo" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed4a1bb24121291d27230c1b1b44e07d6a9b28cefdb32fe1581dfb84e14f940a" +checksum = "282a26c1e23de04bdab3e34a21b6f877a479b96737792516b9b8b8f69b6661be" dependencies = [ "bytemuck", "foldhash 0.2.0", @@ -2463,7 +2700,7 @@ dependencies = [ "glutin_glx_sys", "glutin_wgl_sys", "libloading", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-foundation", "objc2-foundation 0.3.2", @@ -2776,6 +3013,23 @@ dependencies = [ "zerovec", ] +[[package]] +name = "icu_decimal" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eb8f655bba2d0c0459e43a5b0e6cd3cd388109898fb3d85d048165e8b0abd08" +dependencies = [ + "displaydoc", + "fixed_decimal", + "icu_locale_core", + "icu_pattern", + "icu_plurals", + "icu_provider", + "serde", + "writeable", + "zerovec", +] + [[package]] name = "icu_locale_core" version = "2.3.0" @@ -2821,6 +3075,9 @@ dependencies = [ "icu_properties", "icu_provider", "smallvec", + "utf16_iter", + "utf8_iter", + "write16", "zerovec", ] @@ -2830,6 +3087,32 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0" +[[package]] +name = "icu_pattern" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b3db611446bd92c246b03e4aa0bafa71ddb194af431ddbb42b9d70e4bfce1a0" +dependencies = [ + "displaydoc", + "either", + "serde", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_plurals" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e475e6766ef87b1d3c1f97be6363995b0bfaeddbc789671615df598a97ff0593" +dependencies = [ + "displaydoc", + "fixed_decimal", + "icu_provider", + "serde", + "zerovec", +] + [[package]] name = "icu_properties" version = "2.3.0" @@ -2859,6 +3142,7 @@ checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73" dependencies = [ "displaydoc", "icu_locale_core", + "postcard", "serde", "stable_deref_trait", "writeable", @@ -2868,6 +3152,30 @@ dependencies = [ "zerovec", ] +[[package]] +name = "icu_provider_adapters" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df887bc4f55dbc773c7c45a0c7dfa75be886f7b7812830a71ab7271cc1801820" +dependencies = [ + "icu_locale_fallback", + "icu_provider", +] + +[[package]] +name = "icu_provider_blob" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5916b44fac319d3f58e4d8d805a627df19e27937b582cd99fcde3bb6b7ce1b0e" +dependencies = [ + "icu_provider", + "postcard", + "serde", + "writeable", + "zerotrie", + "zerovec", +] + [[package]] name = "icu_segmenter" version = "2.3.0" @@ -2990,7 +3298,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20fd6de4ccfcc187e38bc21cfa543cb5a302cb86a8b114eb7f0bf0dc9f8ac00f" dependencies = [ "io-lifetimes 3.0.1", - "windows-sys 0.60.2", + "windows-sys 0.59.0", ] [[package]] @@ -3020,17 +3328,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "is-terminal" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.59.0", -] - [[package]] name = "is-wsl" version = "0.4.0" @@ -3041,6 +3338,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.10.5" @@ -3050,6 +3353,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itertools" version = "0.14.0" @@ -3366,12 +3678,12 @@ checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc" [[package]] name = "kittest" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ceaa75eb0036a32b6b9833962eb18137449e9817e2e586006471925b727fd5" +checksum = "0bb81a721ba76aa095b7f45deb180504f786904e56ca4b8e141f66eb89a78b75" dependencies = [ "accesskit", - "accesskit_consumer 0.35.0", + "accesskit_consumer", ] [[package]] @@ -3417,9 +3729,9 @@ checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8" [[package]] name = "libc" -version = "0.2.189" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "libfuzzer-sys" @@ -3536,6 +3848,9 @@ name = "litemap" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" +dependencies = [ + "serde_core", +] [[package]] name = "litrs" @@ -3677,20 +3992,20 @@ dependencies = [ [[package]] name = "muda" -version = "0.21.1" +version = "0.21.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a7b000cca0783af26837fe2ebd9ddf1c42279463c085b97622ee1d2f444b89e" +checksum = "4340e89c178916e62879c5c36b7f699019369be413e496178a6983ce91b8bc03" dependencies = [ "crossbeam-channel", "dpi", "keyboard-types", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-foundation", "objc2-foundation 0.3.2", "png 0.18.1", "thiserror 2.0.21", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -3773,6 +4088,18 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "no_std_io2" version = "0.9.4" @@ -3833,6 +4160,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + [[package]] name = "num-derive" version = "0.4.2" @@ -3914,9 +4247,9 @@ dependencies = [ [[package]] name = "objc2" -version = "0.6.4" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a12a8ed07aefc768292f076dc3ac8c48f3781c8f2d5851dd3d98950e8c5a89f" +checksum = "08849bbd4767dfae9457696856ae1c84fe4e0281bbe4a7abff2d0e06fb7981f8" dependencies = [ "objc2-encode", ] @@ -3946,7 +4279,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.6.2", "libc", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-cloud-kit 0.3.2", "objc2-core-data 0.3.2", "objc2-core-foundation", @@ -3967,7 +4300,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.6.2", "dispatch2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-avf-audio", "objc2-core-audio-types", "objc2-core-foundation", @@ -3986,7 +4319,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13a380031deed8e99db00065c45937da434ca987c034e13b87e4441f9e4090be" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -4010,7 +4343,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -4032,7 +4365,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1eebcea8b0dbff5f7c8504f3107c68fc061a3eb44932051c8cf8a68d969c3b2" dependencies = [ "dispatch2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-audio-types", "objc2-core-foundation", ] @@ -4044,7 +4377,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a89f2ec274a0cf4a32642b2991e8b351a404d290da87bb6a9a9d8632490bd1c" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", ] [[package]] @@ -4066,7 +4399,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -4080,7 +4413,7 @@ dependencies = [ "block2 0.6.2", "dispatch2", "libc", - "objc2 0.6.4", + "objc2 0.6.5", ] [[package]] @@ -4093,7 +4426,7 @@ dependencies = [ "block2 0.6.2", "dispatch2", "libc", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-io-surface", "objc2-metal 0.3.2", @@ -4117,7 +4450,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -4142,7 +4475,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.6.2", "dispatch2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-audio", "objc2-core-audio-types", "objc2-core-foundation", @@ -4156,7 +4489,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", ] @@ -4169,7 +4502,7 @@ checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" dependencies = [ "bitflags 2.13.2", "block2 0.6.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", "objc2-io-surface", @@ -4204,7 +4537,7 @@ dependencies = [ "bitflags 2.13.2", "block2 0.6.2", "libc", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", ] @@ -4214,7 +4547,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b0446e98cf4a784cc7a0177715ff317eeaa8463841c616cfc78aa4f953c4ea" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", ] @@ -4226,7 +4559,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", ] @@ -4248,7 +4581,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edd9fdde720df3da7046bb9097811000c1e7ab5cd579fa89d96b27d56781fb30" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-audio-types", "objc2-core-foundation", "objc2-core-media", @@ -4274,7 +4607,7 @@ checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" dependencies = [ "bitflags 2.13.2", "block2 0.6.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-foundation 0.3.2", ] @@ -4298,7 +4631,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", "objc2-foundation 0.3.2", @@ -4343,7 +4676,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d87d638e33c06f577498cbcc50491496a3ed4246998a7fbba7ccb98b1e7eab22" dependencies = [ "bitflags 2.13.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-foundation 0.3.2", ] @@ -4378,6 +4711,12 @@ version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "oorandom" version = "11.1.5" @@ -4386,9 +4725,9 @@ checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" [[package]] name = "open" -version = "5.4.4" +version = "5.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa576c76302b7b808eecc68061e67336c47833ef9d22caa74dda10fa9675eebc" +checksum = "8d8c8b8e725ad26404d6896eb2f064294a0a7b4f1d9120210d85544684c4ca95" dependencies = [ "is-wsl", "libc", @@ -4436,7 +4775,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", +] + +[[package]] +name = "page_size" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d5b2194ed13191c1999ae0704b7839fb18384fa22e49b57eeaa97d79ce40da" +dependencies = [ + "libc", + "winapi", ] [[package]] @@ -4570,9 +4919,9 @@ dependencies = [ [[package]] name = "phf" -version = "0.13.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf" +checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" dependencies = [ "phf_macros", "phf_shared", @@ -4581,9 +4930,9 @@ dependencies = [ [[package]] name = "phf_generator" -version = "0.13.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "135ace3a761e564ec88c03a77317a7c6b80bb7f7135ef2544dbe054243b89737" +checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100" dependencies = [ "fastrand", "phf_shared", @@ -4591,9 +4940,9 @@ dependencies = [ [[package]] name = "phf_macros" -version = "0.13.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "812f032b54b1e759ccd5f8b6677695d5268c588701effba24601f6932f8269ef" +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" dependencies = [ "phf_generator", "phf_shared", @@ -4604,20 +4953,23 @@ dependencies = [ [[package]] name = "phf_shared" -version = "0.13.1" +version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266" +checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89" dependencies = [ "siphasher", ] [[package]] name = "photocraft" -version = "0.9.0" +version = "0.17.0" dependencies = [ "arboard", "async-io", + "chrono", + "clap", "core-graphics2", + "craft-update", "dispatch2", "eframe", "egui", @@ -4625,7 +4977,7 @@ dependencies = [ "futures-lite", "log", "muda", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-foundation 0.3.2", "open", @@ -4645,7 +4997,10 @@ dependencies = [ "resvg", "rfd", "ron", + "serde", "serde_json", + "tempfile", + "thiserror 2.0.21", "winit", "winresource", "winsafe", @@ -4657,7 +5012,7 @@ dependencies = [ [[package]] name = "photocraft-affinity" -version = "0.9.0" +version = "0.17.0" dependencies = [ "crc32fast", "flate2", @@ -4667,11 +5022,13 @@ dependencies = [ [[package]] name = "photocraft-algo" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "criterion", "photocraft-color", "photocraft-geom", "photocraft-raster", + "photocraft-testkit", "rayon", "rustfft", "serde", @@ -4680,19 +5037,22 @@ dependencies = [ [[package]] name = "photocraft-automation" -version = "0.9.0" +version = "0.17.0" dependencies = [ - "base64 0.22.1", + "base64 0.23.1", "cap-std", + "clap", "getrandom 0.3.4", "photocraft-codecs", "photocraft-compose", "photocraft-doc", "photocraft-engine", "photocraft-format", - "photocraft-geom", "photocraft-io", + "photocraft-raster", + "photocraft-raw", "rmcp", + "rustix 1.1.5", "schemars", "serde", "serde_json", @@ -4702,21 +5062,26 @@ dependencies = [ [[package]] name = "photocraft-cli" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "clap", + "clap_complete", + "clap_mangen", "photocraft-automation", "photocraft-codecs", "photocraft-engine", "photocraft-format", "photocraft-io", + "photocraft-raw", "serde_json", + "thiserror 2.0.21", "tokio", "winresource", ] [[package]] name = "photocraft-cms" -version = "0.9.0" +version = "0.17.0" dependencies = [ "moxcms", "rayon", @@ -4724,14 +5089,16 @@ dependencies = [ [[package]] name = "photocraft-codecs" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "craft-jpeg", "exr", "flate2", "half", "image", "image-webp", "jpeg-encoder", + "jxl-oxide", "photocraft-heif", "png 0.18.1", "proptest", @@ -4741,11 +5108,12 @@ dependencies = [ "tiff", "zune-core 0.5.3", "zune-jpeg 0.5.15", + "zune-jpegxl", ] [[package]] name = "photocraft-color" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-cms", "serde", @@ -4753,9 +5121,10 @@ dependencies = [ [[package]] name = "photocraft-compose" -version = "0.9.0" +version = "0.17.0" dependencies = [ "criterion", + "photocraft-cms", "photocraft-color", "photocraft-doc", "photocraft-geom", @@ -4768,7 +5137,7 @@ dependencies = [ [[package]] name = "photocraft-doc" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-color", "photocraft-geom", @@ -4779,9 +5148,10 @@ dependencies = [ [[package]] name = "photocraft-engine" -version = "0.9.0" +version = "0.17.0" dependencies = [ "blake3", + "chrono", "craft-print", "exr", "flate2", @@ -4795,29 +5165,35 @@ dependencies = [ "photocraft-format", "photocraft-geom", "photocraft-io", + "photocraft-ipp", "photocraft-ops", "photocraft-paint", "photocraft-plugins", "photocraft-psd", "photocraft-raster", "photocraft-raw", + "photocraft-testkit", "photocraft-text", "photocraft-vector", "rayon", - "roxmltree", + "roxmltree 0.21.1", + "same-file", + "semver", "serde", "serde_json", "tempfile", "thiserror 2.0.21", "wat", + "winsafe", ] [[package]] name = "photocraft-format" -version = "0.9.0" +version = "0.17.0" dependencies = [ "blake3", "crc32fast", + "exacl", "flate2", "libc", "photocraft-codecs", @@ -4831,21 +5207,25 @@ dependencies = [ "serde", "serde_json", "thiserror 2.0.21", + "winsafe", ] [[package]] name = "photocraft-geom" -version = "0.9.0" +version = "0.17.0" dependencies = [ "serde", ] [[package]] name = "photocraft-gpu" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "bytemuck", "half", "log", + "photocraft-algo", + "photocraft-cms", "photocraft-color", "photocraft-compose", "photocraft-doc", @@ -4859,18 +5239,20 @@ dependencies = [ [[package]] name = "photocraft-heif" -version = "0.9.0" +version = "0.17.0" dependencies = [ "heic-rs", ] [[package]] name = "photocraft-io" -version = "0.9.0" +version = "0.17.0" dependencies = [ "blake3", + "craft-docpath", "crc32fast", "flate2", + "miniz_oxide 0.9.1", "photocraft-affinity", "photocraft-algo", "photocraft-cms", @@ -4884,41 +5266,53 @@ dependencies = [ "photocraft-psd", "photocraft-raster", "photocraft-raw", + "photocraft-testkit", "photocraft-text", "photocraft-vector", "proptest", "resvg", - "roxmltree", + "roxmltree 0.21.1", "serde_json", "thiserror 2.0.21", + "tiff", + "zune-core 0.5.3", + "zune-jpegxl", ] +[[package]] +name = "photocraft-ipp" +version = "0.17.0" + [[package]] name = "photocraft-ops" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-doc", ] [[package]] name = "photocraft-paint" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "photocraft-cms", "photocraft-color", "photocraft-geom", "photocraft-raster", + "rayon", + "rustc-hash 2.1.3", "serde", "serde_json", ] [[package]] name = "photocraft-plugins" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-color", "photocraft-geom", "photocraft-raster", "rayon", + "rustix 1.1.5", "serde", "serde_json", "thiserror 2.0.21", @@ -4928,7 +5322,7 @@ dependencies = [ [[package]] name = "photocraft-psd" -version = "0.9.0" +version = "0.17.0" dependencies = [ "flate2", "photocraft-psd", @@ -4939,17 +5333,18 @@ dependencies = [ [[package]] name = "photocraft-raster" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-color", "photocraft-geom", "proptest", + "rayon", "serde", ] [[package]] name = "photocraft-raw" -version = "0.9.0" +version = "0.17.0" dependencies = [ "flate2", "image", @@ -4964,10 +5359,10 @@ dependencies = [ [[package]] name = "photocraft-tablet" -version = "0.9.0" +version = "0.17.0" dependencies = [ "block2 0.6.2", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-graphics", "objc2-foundation 0.3.2", @@ -4976,18 +5371,15 @@ dependencies = [ [[package]] name = "photocraft-testkit" -version = "0.9.0" +version = "0.17.0" dependencies = [ "memory-stats", - "photocraft-color", - "photocraft-geom", - "photocraft-raster", "serde_json", ] [[package]] name = "photocraft-text" -version = "0.9.0" +version = "0.17.0" dependencies = [ "flate2", "parley", @@ -4996,16 +5388,18 @@ dependencies = [ "photocraft-geom", "photocraft-psd", "photocraft-raster", + "rayon", "skrifa", ] [[package]] name = "photocraft-ui-egui" -version = "0.9.0" +version = "0.17.0" dependencies = [ - "base64 0.22.1", + "base64 0.23.1", "craft-print", "craft-ui", + "craft-update", "eframe", "egui", "egui_extras", @@ -5036,13 +5430,16 @@ dependencies = [ "serde", "serde_json", "sys-locale", + "tempfile", "toml", + "unicode-bidi", + "web-time", "winsafe", ] [[package]] name = "photocraft-vector" -version = "0.9.0" +version = "0.17.0" dependencies = [ "photocraft-color", "photocraft-doc", @@ -5053,7 +5450,7 @@ dependencies = [ [[package]] name = "photocraft-web" -version = "0.9.0" +version = "0.17.0" dependencies = [ "eframe", "egui", @@ -5075,7 +5472,7 @@ dependencies = [ [[package]] name = "photocraft-web-boot" -version = "0.9.0" +version = "0.17.0" dependencies = [ "flate2", "js-sys", @@ -5164,6 +5561,19 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" +[[package]] +name = "plist" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327a4cb2ad49dc48adfdc9da89e22c64d488b508c67a044d71ba729e4521a285" +dependencies = [ + "base64 0.23.1", + "indexmap", + "quick-xml 0.42.0", + "serde", + "time", +] + [[package]] name = "png" version = "0.17.16" @@ -5240,6 +5650,16 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "serde", +] + [[package]] name = "potential_utf" version = "0.1.6" @@ -5251,6 +5671,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -5338,16 +5764,17 @@ dependencies = [ [[package]] name = "proptest" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744" +checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" dependencies = [ - "bit-set 0.8.0", - "bit-vec 0.8.0", + "bit-set 0.11.1", + "bit-vec 0.10.1", "bitflags 2.13.2", + "chacha20", + "core_detect", "num-traits", - "rand 0.9.5", - "rand_chacha", + "rand 0.10.3", "rand_xorshift", "regex-syntax", "rusty-fork", @@ -5414,6 +5841,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "quick-xml" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" +dependencies = [ + "memchr", +] + [[package]] name = "quinn" version = "0.11.12" @@ -5468,7 +5904,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -5555,11 +5991,11 @@ dependencies = [ [[package]] name = "rand_xorshift" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a" +checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" dependencies = [ - "rand_core 0.9.5", + "rand_core 0.10.1", ] [[package]] @@ -5638,7 +6074,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135" dependencies = [ - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-foundation 0.3.2", "objc2-quartz-core 0.3.2", @@ -5834,7 +6270,7 @@ dependencies = [ "js-sys", "libc", "log", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-core-foundation", "objc2-foundation 0.3.2", @@ -5875,13 +6311,14 @@ dependencies = [ [[package]] name = "rmcp" -version = "3.5.0" +version = "3.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fae7019994ae0fe4ada40b732f798f3ff26f0f04facb1477f1bf37eb4f18a2d3" +checksum = "ceb42e9e47443cd83a834dea57b5816d35481417bae72ade5dc5615c59122e26" dependencies = [ "chrono", "futures", "indexmap", + "libc", "pastey 0.2.3", "pin-project-lite", "rmcp-macros", @@ -5898,9 +6335,9 @@ dependencies = [ [[package]] name = "rmcp-macros" -version = "3.5.0" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7e66877136e969d5a2d561ee5841b17717a0619aebd64abb47a2a2f46e34683" +checksum = "967fa7f7935d7d6217d593df0e8e5c9307bcaf256974a5a9ee64476c97735389" dependencies = [ "darling", "proc-macro2", @@ -5909,6 +6346,12 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "roff" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "323c417e1d9665a65b263ec744ba09030cfb277e9daa0b018a4ab62e57bc8189" + [[package]] name = "ron" version = "0.12.2" @@ -5929,6 +6372,15 @@ version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c20b6793b5c2fa6553b250154b78d6d0db37e72700ae35fad9387a46f487c97" +[[package]] +name = "roxmltree" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1964b10c76125c36f8afe190065a4bf9a87bf324842c05701330bba9f1cacbb" +dependencies = [ + "memchr", +] + [[package]] name = "rustc-hash" version = "1.1.0" @@ -5987,7 +6439,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -6002,12 +6454,14 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.45" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "aws-lc-rs", + "log", "once_cell", + "ring", "rustls-pki-types", "rustls-webpki", "subtle", @@ -6054,7 +6508,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -6152,9 +6606,9 @@ dependencies = [ [[package]] name = "schemars" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "687274d293b6cdc6e73e0fee520bf2049650090d7164f87672d212a3c530cf4a" +checksum = "ab508826f74a77ca9d5aba6ff19b522583ee3eaf28a19384ff3d0e5835fadf6e" dependencies = [ "chrono", "dyn-clone", @@ -6166,9 +6620,9 @@ dependencies = [ [[package]] name = "schemars_derive" -version = "1.2.2" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d98c67716b46af2f0b8cf752abc930f6f9aecfbf671ecfb531db8a31dbe4e2ba" +checksum = "e1c3a92094fa7d61aa124645844facb6b554dfc797136d0f5fd1f890e2bffc69" dependencies = [ "proc-macro2", "quote", @@ -6222,6 +6676,16 @@ name = "semver" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] + +[[package]] +name = "send_wrapper" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" [[package]] name = "serde" @@ -6266,9 +6730,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.151" +version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" dependencies = [ "itoa", "memchr", @@ -6692,7 +7156,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -6858,7 +7322,10 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4" dependencies = [ + "js-sys", "libc", + "wasm-bindgen", + "web-sys", ] [[package]] @@ -6919,10 +7386,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix 1.1.5", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -6988,6 +7455,36 @@ dependencies = [ "zune-jpeg 0.5.15", ] +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-skia" version = "0.11.4" @@ -7064,9 +7561,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.53.1" +version = "1.54.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" dependencies = [ "bytes", "libc", @@ -7302,6 +7799,12 @@ dependencies = [ "rustc-hash 2.1.3", ] +[[package]] +name = "typed-path" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e" + [[package]] name = "typeid" version = "1.0.3" @@ -7322,7 +7825,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -7403,6 +7906,35 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "ureq" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cde93ff05758a36a851a398a772b0b79a82fbe3c186a46f2696baaeaf84c966b" +dependencies = [ + "base64 0.23.1", + "flate2", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "rustls-platform-verifier", + "ureq-proto", + "utf8-zero", +] + +[[package]] +name = "ureq-proto" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841583fc36089d7d59419568ae157f43a5a03a2e5a81ba87c77d1596ef46e922" +dependencies = [ + "base64 0.23.1", + "http", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.8" @@ -7429,7 +7961,7 @@ dependencies = [ "kurbo 0.11.3", "log", "pico-args", - "roxmltree", + "roxmltree 0.20.0", "rustybuzz", "simplecss", "siphasher", @@ -7442,12 +7974,30 @@ dependencies = [ "xmlwriter", ] +[[package]] +name = "utf16_iter" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8232dd3cdaed5356e0f716d285e4b40b932ac434100fe9b7e0e8e935b9e6246" + +[[package]] +name = "utf8-zero" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" + [[package]] name = "utf8_iter" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.26.1" @@ -7473,9 +8023,9 @@ dependencies = [ [[package]] name = "vello_common" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2e9aed918117e8152c9eddfd8362d73c465c23f26c44786aa331707b8a64fa2" +checksum = "bbb2141a2bca6e6d598e471fd4d1d7eed8e020aad6a28187edda07f091a325dd" dependencies = [ "bytemuck", "fearless_simd", @@ -7488,9 +8038,9 @@ dependencies = [ [[package]] name = "vello_cpu" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac7349e1f55f6b801c7c277958df4ea53e7f20f21e8014910ad888b2ecda93ea" +checksum = "ace506ca414548966fcf3c283be94eb1cad8254de488791ee698bf438b0890a9" dependencies = [ "bytemuck", "glifo", @@ -7498,6 +8048,16 @@ dependencies = [ "vello_common", ] +[[package]] +name = "verifysign" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6acfcc42a8d44f340d001193d97ef56bf33210b51ad760ae8308c97d5241ca51" +dependencies = [ + "core-foundation 0.10.1", + "windows-sys 0.61.2", +] + [[package]] name = "version-compare" version = "0.2.1" @@ -7611,12 +8171,12 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.261.0" +version = "0.262.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2608e8bb6d67fd68f5a8d0eb1363d6e7bcbc1f8ded5a0bd3a1e382462b876b22" +checksum = "4495a6f85dd94c411c749303efcbc5defade0b0cbdbc0541495241ab917a9b5a" dependencies = [ "leb128fmt", - "wasmparser 0.261.0", + "wasmparser 0.262.0", ] [[package]] @@ -7671,9 +8231,9 @@ dependencies = [ [[package]] name = "wasmparser" -version = "0.261.0" +version = "0.262.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f20f20e44f7e8aeb6744823ea9d869ede51e51be4fdaedede2852282e54d2d8" +checksum = "974d643b125b272607e2fff8012ea14a8328acd7621e4bafa7afb32500052b1b" dependencies = [ "bitflags 2.13.2", "indexmap", @@ -7682,9 +8242,9 @@ dependencies = [ [[package]] name = "wast" -version = "261.0.0" +version = "262.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "776443145731a4062e5b0d392892a2005909b6ab72d9fdc3cad53dd1a714e44a" +checksum = "30612a0c29249b5f185888a107e8a519d920226b7d07f9d351fd094e9f75eb23" dependencies = [ "bumpalo", "leb128fmt", @@ -7695,9 +8255,9 @@ dependencies = [ [[package]] name = "wat" -version = "1.261.0" +version = "1.262.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7b4d1a49ea73a8f3326e74e3a05db667001b16bd1035ed3356fc1a0ed05ca7f" +checksum = "e228027f2b07bbaf946980ebbe2437fca73be6f54c40c2a8d4f42f277075f8d1" dependencies = [ "wast", ] @@ -7821,7 +8381,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" dependencies = [ "proc-macro2", - "quick-xml", + "quick-xml 0.41.0", "quote", ] @@ -8015,7 +8575,7 @@ dependencies = [ "naga", "naga-types", "ndk-sys", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-core-foundation", "objc2-core-graphics", "objc2-foundation 0.3.2", @@ -8076,15 +8636,37 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows" version = "0.62.2" @@ -8203,7 +8785,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets 0.52.6", + "windows-targets", ] [[package]] @@ -8212,16 +8794,7 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets 0.52.6", -] - -[[package]] -name = "windows-sys" -version = "0.60.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" -dependencies = [ - "windows-targets 0.53.5", + "windows-targets", ] [[package]] @@ -8239,31 +8812,14 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm 0.52.6", - "windows_aarch64_msvc 0.52.6", - "windows_i686_gnu 0.52.6", - "windows_i686_gnullvm 0.52.6", - "windows_i686_msvc 0.52.6", - "windows_x86_64_gnu 0.52.6", - "windows_x86_64_gnullvm 0.52.6", - "windows_x86_64_msvc 0.52.6", -] - -[[package]] -name = "windows-targets" -version = "0.53.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" -dependencies = [ - "windows-link", - "windows_aarch64_gnullvm 0.53.1", - "windows_aarch64_msvc 0.53.1", - "windows_i686_gnu 0.53.1", - "windows_i686_gnullvm 0.53.1", - "windows_i686_msvc 0.53.1", - "windows_x86_64_gnu 0.53.1", - "windows_x86_64_gnullvm 0.53.1", - "windows_x86_64_msvc 0.53.1", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] @@ -8281,101 +8837,52 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" - [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" -[[package]] -name = "windows_aarch64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" - [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" -[[package]] -name = "windows_i686_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" - [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" -[[package]] -name = "windows_i686_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" - [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" -[[package]] -name = "windows_i686_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" - [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" -[[package]] -name = "windows_x86_64_gnu" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" - [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" - [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" -[[package]] -name = "windows_x86_64_msvc" -version = "0.53.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" - [[package]] name = "winit" version = "0.30.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6755fa58a9f8350bd1e472d4c3fcc25f824ec358933bba33306d0b63df5978d" +source = "git+https://github.com/storytold/winit.git?rev=a517b3846ee8457d84ee4975110bfda6797c44dc#a517b3846ee8457d84ee4975110bfda6797c44dc" dependencies = [ "ahash", "android-activity", @@ -8453,10 +8960,20 @@ dependencies = [ ] [[package]] -name = "winresource" -version = "0.1.31" +name = "winreg" +version = "0.55.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0986a8b1d586b7d3e4fe3d9ea39fb451ae22869dcea4aa109d287a374d866087" +checksum = "cb5a765337c50e9ec252c2069be9bf91c7df47afb103b642ba3a53bf8101be97" +dependencies = [ + "cfg-if", + "windows-sys 0.59.0", +] + +[[package]] +name = "winresource" +version = "0.1.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2bc6d36480b829272a5bdabf08e6dca8004761c1da7e35efef800540c66221b" dependencies = [ "toml", "version_check", @@ -8502,11 +9019,20 @@ dependencies = [ "wayland-protocols-wlr", ] +[[package]] +name = "write16" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1890f4022759daae28ed4fe62859b1236caebfc61ede2f63ed4e695f3f6d936" + [[package]] name = "writeable" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" +dependencies = [ + "either", +] [[package]] name = "x11-dl" @@ -8585,7 +9111,7 @@ dependencies = [ "image", "libwayshot-xcap", "log", - "objc2 0.6.4", + "objc2 0.6.5", "objc2-app-kit 0.3.2", "objc2-av-foundation", "objc2-core-foundation", @@ -8614,7 +9140,7 @@ checksum = "a6c2ad15e0e922856ee89afe862b8992334bbe7953adad56cd1199358cb30566" dependencies = [ "bitflags 2.13.2", "libc", - "quick-xml", + "quick-xml 0.41.0", ] [[package]] @@ -8662,8 +9188,9 @@ checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" [[package]] name = "xtask" -version = "0.9.0" +version = "0.17.0" dependencies = [ + "chrono", "serde", "serde_json", "toml", @@ -8675,6 +9202,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5a4b21e1a62b67a2970e6831bc091d7b87e119e7f9791aef9702e3bef04448" +[[package]] +name = "yeslogic-fontconfig-sys" +version = "6.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d8b8abf912b9a29ff112e1671c97c33636903d13a69712037190e6805af4f76" +dependencies = [ + "dlib", + "once_cell", + "pkg-config", +] + [[package]] name = "yoke" version = "0.8.3" @@ -8872,6 +9410,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" dependencies = [ "displaydoc", + "litemap", + "serde_core", "yoke", "zerofrom", "zerovec", @@ -8900,6 +9440,23 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "zip" +version = "9.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886b00003a72c4ccee3f42cfc34908d33b2508a74d827dc2a4244706475c704" +dependencies = [ + "crc32fast", + "flate2", + "getrandom 0.4.3", + "indexmap", + "memchr", + "nix", + "typed-path", + "windows-sys 0.61.2", + "zopfli", +] + [[package]] name = "zlib-rs" version = "0.6.8" @@ -8912,6 +9469,18 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" +[[package]] +name = "zopfli" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aaf7fc5d30c28483d93805c4a5e12b05bbb52407fa67c5f8bd552374cd01fb11" +dependencies = [ + "bumpalo", + "crc32fast", + "log", + "simd-adler32", +] + [[package]] name = "zune-core" version = "0.4.12" @@ -9000,3 +9569,18 @@ dependencies = [ "syn 3.0.6", "winnow", ] + +[[patch.unused]] +name = "egui_demo_lib" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" + +[[patch.unused]] +name = "egui_inspection" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" + +[[patch.unused]] +name = "egui_system_font_provider" +version = "0.37.0" +source = "git+https://github.com/storytold/egui.git?rev=6d518fc799681571963842f95b55a6b68431b15a#6d518fc799681571963842f95b55a6b68431b15a" diff --git a/Cargo.toml b/Cargo.toml index 0d0f93ae7c..144b7e4475 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,13 +4,13 @@ members = ["crates/*", "apps/*", "xtask"] default-members = ["crates/*", "apps/photocraft", "apps/photocraft-cli", "xtask"] [workspace.package] -version = "0.9.0" +version = "0.17.0" edition = "2024" license = "MIT OR Apache-2.0" rust-version = "1.95" [workspace.dependencies] -craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "5c874d51eeb3de70e735c8ce87a59a7dedd47675", features = ["serde"] } +craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", features = ["serde"] } # internal photocraft-geom = { path = "crates/geom" } photocraft-color = { path = "crates/color" } @@ -36,47 +36,66 @@ photocraft-engine = { path = "crates/engine" } photocraft-ui-egui = { path = "crates/ui-egui" } photocraft-testkit = { path = "crates/testkit" } photocraft-tablet = { path = "crates/tablet" } +photocraft-ipp = { path = "crates/ipp" } # external -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "3d37da389a3618ab9b76109da11416e9ca4ea719" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +craft-jpeg = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" } +# craft-update: app-specific pretend versions apply to startup and manual checks. +craft-update = { git = "https://github.com/storytold/craft-libs", version = "=0.1.0", rev = "04aa1703b32d299e54f78fcde4182f2b393a9116", features = ["egui"] } thiserror = "2" serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" +clap = { version = "4", features = ["derive", "env"] } +clap_complete = "4.6.11" +clap_mangen = { version = "0.3.3", features = ["env"] } arc-swap = "1" bytemuck = { version = "1", features = ["derive"] } -flate2 = { version = "1", default-features = false, features = ["rust_backend"] } +flate2 = { version = "1.1.10", default-features = false, features = ["zlib-rs", "runtime_detection"] } image = { version = "0.25", default-features = false } # Baseline-JPEG decode of lossy DNG tiles (compression 34892), shared with zune-jpeg. zune-core = "0.5" zune-jpeg = "0.5" proptest = "1" -eframe = { version = "0.36", default-features = false } +eframe = { version = "0.37", default-features = false } # Without `default_fonts`, which embeds egui's fonts uncompressed: ui-egui ships them deflated # (egui_fonts.rs; the web build's size gate). -egui = { version = "0.36", default-features = false } -egui_kittest = "0.36" +egui = { version = "0.37", default-features = false } +egui_kittest = "0.37" rfd = "0.17" log = "0.4" +# All date/time conversion goes through chrono (no hand-rolled calendar code beyond the 400-year range extension; AGENTS.md). +chrono = { version = "0.4.45", default-features = false, features = ["std"] } rayon = "1" -criterion = { version = "0.5", default-features = false } +rustc-hash = "2.1.3" +criterion = { version = "0.8", default-features = false } getrandom = "0.3" +# Monotonic clock on native and wasm (screenshot deadlines: a wall clock can jump). +web-time = "1" [workspace.lints.rust] unsafe_code = "forbid" [workspace.lints.clippy] +cognitive_complexity = "deny" +too_many_arguments = "deny" +wildcard_imports = "deny" dbg_macro = "warn" todo = "warn" +# Dependencies carry no debug info: their frames in a backtrace lose file:line, and the target dir +# and link times shrink. The test profile inherits both settings. [profile.dev.package."*"] opt-level = 2 +debug = false [profile.dev] opt-level = 1 # Line tables keep file:line in panic backtraces but drop variable info, which roughly halves link -# time and PDB size for the ~260 test binaries (#2204). CI sets the same through -# CARGO_PROFILE_DEV_DEBUG (and no debuginfo at all for the Windows tests). To inspect locals in a -# debugger, build with CARGO_PROFILE_DEV_DEBUG=true. +# time and PDB size for the test binaries (#2204). CI's Windows legs build with no debuginfo at all +# (CARGO_PROFILE_DEV_DEBUG=0). To inspect locals in a debugger, build with +# CARGO_PROFILE_DEV_DEBUG=true. debug = "line-tables-only" # Test builds compile ui-egui without optimization: its library and unit tests were the biggest @@ -136,6 +155,14 @@ opt-level = "z" [profile.wasm-release.package.naga] opt-level = "z" +# The browser UI toolkit favors size to stay within the WebAssembly upload budget. +[profile.wasm-release.package.egui] +opt-level = "z" +[profile.wasm-release.package.epaint] +opt-level = "z" +[profile.wasm-release.package.eframe] +opt-level = "z" + # Pixel processing keeps its speed-oriented optimization level, with two exceptions. # Colour-management transforms are built once and cached (`transform::cached`), so "z" costs # little runtime. The browser app composites per frame on the GPU (`crates/gpu`, WebGPU/WebGL2), @@ -157,3 +184,20 @@ opt-level = "z" [profile.wasm-release.package.photocraft-paint] opt-level = 3 +# Keep the complete egui family and native event bridge on reviewed fork commits. +[patch.crates-io] +ecolor = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +emath = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +epaint = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui-winit = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_glow = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_extras = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +eframe = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "6d518fc799681571963842f95b55a6b68431b15a" } +winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" } diff --git a/Dockerfile b/Dockerfile index 66734f255a..1803d3a2e9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,8 +25,8 @@ RUN set -eu; \ asset="trunk-${arch}-unknown-linux-musl.tar.gz"; \ url="https://github.com/trunk-rs/trunk/releases/download/v${TRUNK_VERSION}"; \ cd /tmp; \ - curl -fsSL --retry 3 -A Photocraft-dev "$url/$asset" -o "$asset"; \ - curl -fsSL --retry 3 -A Photocraft-dev "$url/$asset.sha256" -o "$asset.sha256"; \ + curl --proto '=https' -fsSL --retry 3 -A Photocraft-dev "$url/$asset" -o "$asset"; \ + curl --proto '=https' -fsSL --retry 3 -A Photocraft-dev "$url/$asset.sha256" -o "$asset.sha256"; \ printf '%s %s\n' "$(cat "$asset.sha256")" "$asset" | sha256sum -c -; \ tar -xzf "$asset" -C /usr/local/bin trunk; \ rm "$asset" "$asset.sha256"; \ diff --git a/NOTICE b/NOTICE index 887010d4a6..6250865673 100644 --- a/NOTICE +++ b/NOTICE @@ -16,8 +16,26 @@ Inter font (assets/fonts/Inter-*.ttf) — SIL Open Font License 1.1. See assets/ JetBrains Mono font (assets/fonts/JetBrainsMono-Regular.ttf) — SIL Open Font License 1.1. See assets/fonts/OFL-JetBrainsMono.txt. Lucide icons (assets/icons/*.svg, except slice-knife.svg) — ISC; icons derived from Feather also MIT. See assets/icons/LICENSE-lucide.txt. SCOWL English word list (assets/dict/en_US-scowl-50.txt.gz) — SCOWL license. See assets/dict/LICENSE-SCOWL.txt. +libfacedetection face detection weights and network (crates/algo/data/libfacedetection.bin, crates/algo/src/faces/cnn.rs) — BSD-3-Clause, Copyright (c) 2015-2019, Shiqi Yu, all rights reserved. See crates/algo/data/LICENSE-libfacedetection.txt. Builds made with craft-fonts (CRAFT_FONTS_DIR; all official releases) also embed the fonts listed in https://github.com/storytold/craft-fonts/blob/main/ATTRIBUTION.md (today BIZ UDPGothic, Shippori Mincho and BIZ UDMincho; not the web build), under the SIL Open Font License 1.1. Their licence texts ship with the package as OFL-.txt. + +The egui-icons font subset is embedded from epaint_default_fonts by crates/ui-egui/build.rs +and registered in the native and web fallback stacks by crates/ui-egui/src/egui_fonts.rs. +It is a subset of emoji-icon-font by John Slegers; the same upstream MIT license applies. +Source: https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts +Original: https://github.com/jslegers/emoji-icon-font +The following notice is reproduced verbatim from emoji-icon-font-mit-license.txt: + +MIT License + +Copyright (c) 2014 John Slegers + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index d1e839323c..3c4e586f26 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@

+

+ PhotoCraft icon: an engraved nine-tailed fox on a blue tile +

+

PhotoCraft

@@ -20,6 +24,9 @@ macOS · Windows · Linux · FreeBSD · Web License: MIT OR Apache-2.0 Status: early alpha +
+ Scanned by SonarCloud + Reviewed by CodeRabbit

@@ -178,8 +185,8 @@ Every screenshot here is the real app at work on public-domain art, rendered off \n"); - by_row[r0].push((c0, td)); - } - let mut s = format!( - "\n\n{t}\n\n\n\n\n
-

🧰 67 tools

- Move · Artboard · Rectangular and Elliptical Marquee · Single Row Marquee · Single Column Marquee · Lasso · Polygonal Lasso · Magnetic Lasso · Object Selection · Quick Selection · Magic Wand · Crop · Perspective Crop · Slice · Slice Select · Frame · Eyedropper · Color Sampler · Ruler · Note · Count · Remove · Spot Healing Brush · Healing Brush · Patch · Content-Aware Move · Red Eye · Brush · Pencil · Mixer Brush · Color Replacement · Clone Stamp · Pattern Stamp · History Brush · Eraser · Background Eraser · Magic Eraser · Gradient · Paint Bucket · Blur · Sharpen · Smudge · Dodge · Burn · Sponge · Pen · Freeform Pen · Curvature Pen · Add Anchor Point · Delete Anchor Point · Convert Point · Horizontal and Vertical Type · Horizontal and Vertical Type Mask · Path Selection · Direct Selection · Rectangle · Ellipse · Triangle · Polygon · Line · Custom Shape · Hand · Rotate View · Zoom +

🧰 68 tools

+ Move · Artboard · Rectangular and Elliptical Marquee · Single Row Marquee · Single Column Marquee · Lasso · Polygonal Lasso · Magnetic Lasso · Object Selection · Quick Selection · Magic Wand · Crop · Perspective Crop · Slice · Slice Select · Frame · Eyedropper · Color Sampler · Ruler · Note · Count · Remove · Spot Healing Brush · Healing Brush · Patch · Content-Aware Move · Red Eye · Brush · Pencil · Mixer Brush · Color Replacement · Clone Stamp · Pattern Stamp · History Brush · Art History Brush · Eraser · Background Eraser · Magic Eraser · Gradient · Paint Bucket · Blur · Sharpen · Smudge · Dodge · Burn · Sponge · Pen · Freeform Pen · Curvature Pen · Add Anchor Point · Delete Anchor Point · Convert Point · Horizontal and Vertical Type · Horizontal and Vertical Type Mask · Path Selection · Direct Selection · Rectangle · Ellipse · Triangle · Polygon · Line · Custom Shape · Hand · Rotate View · Zoom

🖌️ A real brush engine

@@ -199,7 +206,7 @@ Every screenshot here is the real app at work on public-domain art, rendered off

🗂️ Formats

- PSD and PSB, layered TIFF (Photoshop's layer data in the TIFF, read and written in either byte order), Paint.NET PDN3 (read, with editable layers and all 14 blend modes), OpenRaster (read and write, with layers, groups and blend modes, for Krita, MyPaint and GIMP), Krita .kra (read, with paint layers, groups, blend modes and transparency masks), SVG (opens as shape layers, places as a vector Smart Object), plus flat PNG, JPEG, TIFF, WebP (lossy and lossless), GIF, BMP, TGA, ICO, QOI, PNM, OpenEXR, Radiance HDR and FITS (astronomy images: 8/16-bit and 32-bit float, gray or RGB, header kept), and the native .pcraft format. Flat formats retain the depths they support: PNG keeps 8/16-bit samples, TIFF keeps 8/16-bit and 32-bit float samples, and OpenEXR keeps half/float samples; JPEG, WebP and the other 8-bit formats reduce higher-depth samples with an export warning. See the codec capability matrix for each format. HEIC photos from iPhone and Mac are read-only (included in official builds; optional --features heif for source builds). AVIF is write-only with the optional avif build feature; AVIF decoding and JPEG XL are unavailable. Affinity documents (.af, .afdesign, .afphoto, .afpub) open natively, read only, with artboards, layers, vectors, text, images and masks; effects, adjustments and other unsupported parts are listed in a warning, and Affinity export is unavailable. + PSD and PSB, layered TIFF (Photoshop's layer data in the TIFF, read and written in either byte order), Paint.NET PDN3 (read, with editable layers and all 14 blend modes), OpenRaster (read and write, with layers, groups and blend modes, for Krita, MyPaint and GIMP), GIMP XCF (read, with layers, groups, masks, blend modes, channels and the selection), Krita .kra (read, with paint layers, groups, blend modes and transparency masks), SVG (opens as shape layers, places as a vector Smart Object), plus flat PNG, JPEG, TIFF, WebP (lossy and lossless), GIF, BMP, TGA, ICO, QOI, PNM, OpenEXR, Radiance HDR and FITS (astronomy images: 8/16-bit and 32-bit float, gray or RGB, header kept), and the native .pcraft format. Flat formats retain the depths they support: PNG keeps 8/16-bit samples, TIFF keeps 8/16-bit and 32-bit float samples, and OpenEXR keeps half/float samples; JPEG, WebP and the other 8-bit formats reduce higher-depth samples with an export warning. See the codec capability matrix for each format. HEIC photos from iPhone and Mac are read-only (included in official builds; optional --features heif for source builds). AVIF is write-only with the optional avif build feature; AVIF decoding and JPEG XL are unavailable. Affinity documents (.af, .afdesign, .afphoto, .afpub) open natively, read only, with artboards, layers, vectors, text, images and masks; effects, adjustments and other unsupported parts are listed in a warning, and Affinity export is unavailable.

🪄 The everyday essentials

@@ -214,7 +221,7 @@ Every screenshot here is the real app at work on public-domain art, rendered off PhotoCraft's PSD support is a standalone crate written from Adobe's public specification and tested against a corpus of real-world files. -- **Faithful round trips:** opening and re-saving a document renders the same for 307 of the 309 files in the psd-tools test set and 169 of 170 in our mixed ag-psd/psd-tools set (`crates/io/tests/corpus.rs`; fetch the psd-tools set with `cargo xtask corpus --psd-tools`), and anything we don't model yet (raw blocks, descriptors, extras) is carried over instead of being dropped. A re-saved file is not byte-identical to its source: PhotoCraft rewrites image resources, layer records and the composite. Only the standalone `photocraft-psd` crate, parsing and writing a file without the document model, reproduces every parseable corpus file byte for byte (`crates/psd/tests/corpus.rs`). +- **Faithful round trips:** opening and re-saving a document renders the same for 307 of the 309 files in the psd-tools test set and 169 of 170 in our mixed ag-psd/psd-tools set (`crates/io/tests/it/corpus.rs`; fetch the psd-tools set with `cargo xtask corpus --psd-tools`), and anything we don't model yet (raw blocks, descriptors, extras) is carried over instead of being dropped. A re-saved file is not byte-identical to its source: PhotoCraft rewrites image resources, layer records and the composite. Only the standalone `photocraft-psd` crate, parsing and writing a file without the document model, reproduces every parseable corpus file byte for byte (`crates/psd/tests/it/corpus.rs`). - **Pixels that match:** a composite oracle compares our render with Photoshop's own merged image, covering gradient interpolation (Classic, Perceptual and Linear), layer effects, shape strokes, clipping and fill opacity. - **Large documents:** PSB, 16 and 32-bit files, and CMYK and Lab documents open natively. @@ -275,8 +282,12 @@ flatpak install --user photocraft--linux-x86_64.flatpak # or -linux-a flatpak run ai.storyteller.photocraft ``` +The Flatpak prints through the host's CUPS socket (File › Print talks IPP to it directly, no `lp` needed); without CUPS, or without a printer, File › Print opens the system print dialog through the print portal. See [printing](docs/printing.md). + The AppImage needs no install: the first run registers its launcher icon and menu entry in `~/.local/share` so the dock shows PhotoCraft's icon on Wayland. Set `PHOTOCRAFT_NO_DESKTOP_INTEGRATION=1` to skip that, and see [`docs/releasing.md`](docs/releasing.md) › Linux to undo it. +To use the bundled CLI, run `./photocraft--linux-x86_64.AppImage --cli --help`, or `--cli mcp` to start the MCP server on stdio. CLI launches skip desktop integration. A symlink named `photocraft-cli` also selects the CLI; see [`docs/development.md`](docs/development.md#headless-cli). + On a Wayland session, files dropped on the window don't open yet: winit 0.30, the windowing library under egui, has no Wayland drag and drop (#386). Use File › Open, or copy the image in your file manager and paste it with Ctrl+V. To drag and drop, start PhotoCraft under XWayland: `WAYLAND_DISPLAY= photocraft`, `WAYLAND_DISPLAY= ./photocraft--linux-x86_64.AppImage`, or `flatpak run --nosocket=wayland --socket=x11 ai.storyteller.photocraft`. On Wayland, PhotoCraft shows the command for your install in a notice until you dismiss it. Preferences › Performance › Linux display server = X11 does the same on every launch. With a fractional display scale (125%, 150%, 175%) an XWayland window is drawn at a lower resolution and enlarged by the compositor, so the interface is blurred there; native Wayland stays sharp (#1072). On macOS, the command-line tool comes as `photocraft-cli--macos-universal.zip`. The binary is signed with the same Developer ID as the app and notarized by Apple. A bare binary can't carry a stapled notarization ticket the way the DMG does, so the first time you run it macOS checks the notarization online. You can confirm it yourself: @@ -300,6 +311,19 @@ Maintainers: [`docs/releasing.md`](docs/releasing.md) explains how releases are > [!IMPORTANT] > **Status:** PhotoCraft is in early alpha, and we want to be straight about where it stands: much of Photoshop's feature surface exists in some form, but **it is not yet a Photoshop replacement for daily professional work**. The biggest gaps are AI/generative features, 15 missing tools, depth in typography and pro workflows, and plug-in compatibility. Every Photoshop menu item is wired to a command ([`docs/parity-checklist.md`](docs/parity-checklist.md)), but that measures wiring, not behaviour. The honest, dimension-by-dimension picture and where we're going next are in the [roadmap](ROADMAP.md) and [parity assessment](docs/target-app-parity.md). Expect rough edges, and please file issues (include your OS, document size, layer count and a screenshot). You can also tell us what broke on [Discord](https://discord.gg/artcraft). +## Community packages + +These installers are maintained by community members, not by the PhotoCraft team. + +On Gentoo, the [::snakebyte overlay](https://github.com/switch87/snakebyte-overlay) packages the Linux release as `media-gfx/photocraft-bin`: + +```sh +eselect repository add snakebyte git https://github.com/switch87/snakebyte-overlay.git +emaint sync -r snakebyte +echo 'media-gfx/photocraft-bin ~amd64' >> /etc/portage/package.accept_keywords/photocraft +emerge --ask media-gfx/photocraft-bin +``` + ## Documentation Developer, architecture, automation, format, and security documentation is maintained in the [PhotoCraft documentation book](book/). @@ -321,7 +345,7 @@ sha256-verified. Fetch them with `cargo xtask corpus --all` and run the tests wi **New to PhotoCraft?** Download it from the [PhotoCraft page on getartcraft.com](https://getartcraft.com/apps/photocraft). That's the easiest way to install it. -**Want a specific build or format?** On GitHub, the [latest release](https://github.com/storytold/photocraft/releases/latest) has every build listed below, and [all releases](https://github.com/storytold/photocraft/releases) has earlier versions and their notes. `` in the file names is the version number, and `SHA256SUMS.txt` lists a checksum for every file. +**Want a specific build or format?** On GitHub, the [latest release](https://github.com/storytold/photocraft/releases/latest) has every build listed below, and [all releases](https://github.com/storytold/photocraft/releases) have earlier versions and their notes. `` in the file names is the version number, and `SHA256SUMS.txt` lists a checksum for every file. ### Windows diff --git a/ROADMAP.md b/ROADMAP.md index f41b71bfc9..829ff75af8 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -2,7 +2,7 @@ **Stage: alpha** · next: beta, ~30 points (ready for real work ~45% → ~75%, with reliable PSD) and ~1,100–1,800 h away -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-13 · **Change:** minor (oracle floor 145 of 258 and 953 open issues, as in gaps.md and on GitHub; toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026 +> **Last reviewed:** 2026-10-17 · **Last updated:** 2026-10-17 · **Change:** minor (Object Finder retains large enclosed foregrounds (#6548); Russian catalog covers every dynamic label; previously minor: oracle floor 145 of 258 and 953 open issues, as in gaps.md and on GitHub; toolbox count 56 of 68, 12 missing) · **Target:** Adobe Photoshop 2026 PhotoCraft's one-page summary of how close it is to Adobe Photoshop 2026 (27.11.0, measured from the installed app on 2026-10-10) at version 0.6.0. Detail: [target-app parity](docs/target-app-parity.md) @@ -104,7 +104,7 @@ because engine messages are still English. Detail: [localization-parity](docs/lo | Korean | `ko` | 100% | partial | | Vietnamese | `vi` | 0% | none | -Also shipped: Traditional Chinese, Russian, Ukrainian, Czech, Polish, Greek, Dutch, Italian (8). +Also shipped: Traditional Chinese, Russian (every dynamic label, [notes](docs/localization-ru.md)), Ukrainian, Czech, Polish, Greek, Dutch, Italian (8). ## Upcoming @@ -122,6 +122,7 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md). | Date | Entry | |---|---| +| 2026-10-17 | Object Finder classifies background by boundary coverage, retaining large enclosed foregrounds and rejecting surrounding backdrop (#6548); learned segmentation remains open (#2788) | | 2026-10-10 | Full re-measure against Photoshop 2026 27.11.0 (installed bundle inspected): breadth ~76%, ready ~45%, alpha. Progress docs reorganized to the craftrules standard (`parity.md` → `parity-checklist.md`, `parity-estimate.md` merged into `target-app-parity.md`). 0.6.0 released. | | 2026-10-09 | Remove tool, Rotate View, crop rotation; OpenRaster and Paint.NET; 39 Affinity documents in the corpus | | 2026-10-08 | 0.5.0. Affinity import; Magnetic Lasso, Pattern Stamp, Mixer Brush, Pencil, Patch, Vertical Type on the toolbar | @@ -135,7 +136,9 @@ Detail and the next releases: [docs/roadmap.md](docs/roadmap.md). | Date | Change | Summary | |---|---|---| -| 2026-10-13 | minor | Focus item 1 quoted the oracle floor as 133 of 256; it is 145 of 258 (`crates/io/tests/corpus.rs`, gaps.md G1). Open issues 579 → 953 (2026-10-13) | +| 2026-10-17 | minor | Object Finder retains large enclosed foregrounds (#6548) | +| 2026-10-13 | minor | Focus item 1 quoted the oracle floor as 133 of 256; it is 145 of 258 (`crates/io/tests/it/corpus.rs`, gaps.md G1). Open issues 579 → 953 (2026-10-13) | +| 2026-10-15 | minor | Russian catalog covers every dynamic UI label the other catalogs translate | | 2026-10-11 | minor | Toolbox count 56 of 68 (12 missing) with the Convert Point tool (#3204) | | 2026-10-10 | minor | Readiness by audience with hours per tier | | 2026-10-10 | minor | Mainstream-practitioner and essentials-user numbers added | diff --git a/apps/photocraft-cli/Cargo.toml b/apps/photocraft-cli/Cargo.toml index f4848f379a..94e04b518a 100644 --- a/apps/photocraft-cli/Cargo.toml +++ b/apps/photocraft-cli/Cargo.toml @@ -28,9 +28,19 @@ photocraft-engine = { workspace = true } photocraft-io = { workspace = true } photocraft-format = { path = "../../crates/format" } photocraft-codecs = { workspace = true } +clap = { workspace = true } serde_json = { workspace = true } +thiserror = { workspace = true } tokio = { version = "1", features = ["rt-multi-thread", "macros"] } +[dev-dependencies] +# Synthetic camera raws (#7789). +photocraft-raw = { workspace = true, features = ["testgen"] } +# Shell completions and man pages (examples/man-and-completions.rs, for the packages); not in +# the binary. +clap_complete = { workspace = true } +clap_mangen = { workspace = true } + [build-dependencies] # Icon + VERSIONINFO for photocraft-cli.exe; build.rs only uses it when targeting Windows. winresource = "0.1" diff --git a/apps/photocraft-cli/build.rs b/apps/photocraft-cli/build.rs index c757a4a7c5..3951c46b81 100644 --- a/apps/photocraft-cli/build.rs +++ b/apps/photocraft-cli/build.rs @@ -12,6 +12,14 @@ fn main() -> std::io::Result<()> { if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") { return Ok(()); } + // Printing to PDF (winprint's `PdfCreateRenderer`) imports Windows.Data.Pdf.dll, which + // stripped Windows images (NTLite, some N/LTSC builds) lack. A load-time import stops the exe + // from starting at all ("windows.data.pdf.dll was not found", #5884), so load it on first use; + // the print command checks for it first and reports an error instead. + if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { + println!("cargo:rustc-link-arg-bins=/DELAYLOAD:windows.data.pdf.dll"); + println!("cargo:rustc-link-arg-bins=delayimp.lib"); + } let mut res = winresource::WindowsResource::new(); // FileVersion / ProductVersion strings and the numeric FILEVERSION / PRODUCTVERSION are left // to `WindowsResource::new`, which reads the Cargo package version. A pre-release such as diff --git a/apps/photocraft-cli/examples/man-and-completions.rs b/apps/photocraft-cli/examples/man-and-completions.rs new file mode 100644 index 0000000000..ddb9cf0233 --- /dev/null +++ b/apps/photocraft-cli/examples/man-and-completions.rs @@ -0,0 +1,41 @@ +//! Writes photocraft-cli's shell completions ([`clap_complete`]: bash, zsh, fish, elvish, +//! PowerShell) and man pages ([`clap_mangen`]: `photocraft-cli.1` and one per subcommand, such as +//! `photocraft-cli-convert.1`) into a directory. The Linux and FreeBSD packages ship them +//! (`packaging/linux/package.sh`, `packaging/freebsd/package.sh`): +//! +//! ```sh +//! cargo run -p photocraft-cli --example man-and-completions -- +//! ``` + +use std::path::Path; +use std::process::ExitCode; + +use clap::ValueEnum; +use clap_complete::Shell; + +fn main() -> ExitCode { + let mut args = std::env::args_os().skip(1); + let (Some(dir), None) = (args.next(), args.next()) else { + eprintln!("usage: man-and-completions "); + return ExitCode::from(2); + }; + let dir = Path::new(&dir); + match generate(dir) { + Ok(()) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("error: {}: {e}", dir.display()); + ExitCode::FAILURE + } + } +} + +fn generate(dir: &Path) -> std::io::Result<()> { + std::fs::create_dir_all(dir)?; + let mut cmd = photocraft_cli::command(); + for &shell in Shell::value_variants() { + clap_complete::generate_to(shell, &mut cmd, "photocraft-cli", dir)?; + } + // A fresh command: `clap_complete::generate_to` built `cmd`, adding the `help` subcommand, + // which `clap_mangen` would otherwise give pages of its own (`photocraft-cli-help-convert.1`). + clap_mangen::generate_to(photocraft_cli::command(), dir) +} diff --git a/apps/photocraft-cli/src/actions.rs b/apps/photocraft-cli/src/actions.rs new file mode 100644 index 0000000000..4d494c8a82 --- /dev/null +++ b/apps/photocraft-cli/src/actions.rs @@ -0,0 +1,116 @@ +use std::path::{Path, PathBuf}; + +use photocraft_engine::actions_cmds::Action; +use serde_json::{Value, json}; + +/// Why an actions file can't be played. +#[derive(Debug, thiserror::Error)] +pub enum ActionsError { + #[error("actions JSON: {0}")] + Json(#[source] serde_json::Error), + /// The steps don't parse ([`photocraft_engine::automate_cmds::parse_action`]). + #[error(transparent)] + Steps(#[from] photocraft_engine::EngineError), + #[error("--action picks an action from an Actions set ({{\"actions\": [{{\"name\", \"steps\"}}…]}}), but this file holds one action's steps")] + NotASet, + #[error("Actions set: {0}")] + Set(#[source] serde_json::Error), + #[error("no action named `{name}` in the set (it holds {})", quoted(names))] + NoSuchAction { name: String, names: Vec }, + #[error("the Actions set holds {} actions: pick one with --action ({})", names.len(), quoted(names))] + PickOne { names: Vec }, + #[error("{}: {source}", path.display())] + Read { path: PathBuf, source: std::io::Error }, + #[error("{}: {source}", path.display())] + Utf8 { path: PathBuf, source: std::string::FromUtf8Error }, + /// A Photoshop Actions file that doesn't import ([`photocraft_engine::atn_cmds::import`]). + #[error("{}: {message}", path.display())] + Atn { path: PathBuf, message: String }, + #[error("no action named `{name}` in the set `{set}` (it holds {})", quoted(names))] + NoSuchAtnAction { name: String, set: String, names: Vec }, + #[error("{}: the set holds no actions", path.display())] + EmptyAtn { path: PathBuf }, + #[error("the set `{set}` holds {} actions: pick one with --action ({})", names.len(), quoted(names))] + PickOneAtn { set: String, names: Vec }, +} + +/// `` `a`, `b` ``: the action names an error lists. +fn quoted(names: &[String]) -> String { + names.iter().map(|n| format!("`{n}`")).collect::>().join(", ") +} + +fn names(actions: &[Action]) -> Vec { + actions.iter().map(|a| a.name.clone()).collect() +} + +/// Parse an actions file: the steps of a recorded action or a droplet (`[id, params]` pairs, +/// `{"command": id, "params": {…}}` objects or bare ids; `"id"` is accepted for `"command"`), as a +/// list or wrapped in `{"actions": […]}`, `{"steps": […]}` or a droplet (#489). +pub fn parse_actions(text: &str) -> Result, ActionsError> { + let v: Value = serde_json::from_str(text).map_err(ActionsError::Json)?; + steps_of(&v) +} + +fn steps_of(v: &Value) -> Result { + Ok(photocraft_engine::automate_cmds::parse_action(v.get("actions").unwrap_or(v), "batch --actions")?) +} + +/// Engine commands to run in order: `(id, params)`. +pub type Steps = Vec<(String, Value)>; + +/// The steps `batch` plays on each file, and the actions they can call. An Actions set (the +/// Actions panel's `actions.json`: `{"actions": [{"name", "steps"}…]}`, or the bare list) plays +/// the action `name` picks (optional when the set holds one) with the whole set loaded, as +/// Photoshop's Batch does; anything else is one action's steps ([`parse_actions`]) (#2786). +pub fn load_actions(text: &str, name: Option<&str>) -> Result<(Steps, Vec), ActionsError> { + let v: Value = serde_json::from_str(text).map_err(ActionsError::Json)?; + let list = v.get("actions").unwrap_or(&v); + let is_set = list.as_array().is_some_and(|a| !a.is_empty() && a.iter().all(|x| x.get("name").is_some_and(Value::is_string))); + if !is_set { + if name.is_some() { + return Err(ActionsError::NotASet); + } + return Ok((steps_of(&v)?, Vec::new())); + } + let set: Vec = serde_json::from_value(list.clone()).map_err(ActionsError::Set)?; + let action = match (name, set.as_slice()) { + (Some(n), _) => set.iter().find(|a| a.name == n).ok_or_else(|| ActionsError::NoSuchAction { name: n.to_owned(), names: names(&set) })?, + (None, [only]) => only, + (None, _) => return Err(ActionsError::PickOne { names: names(&set) }), + }; + let play = vec![("actions.play".to_string(), json!({"action": action.name}))]; + Ok((play, set)) +} + +/// [`load_actions`] for a file on disk: a Photoshop Actions file (`.atn`, by extension or by +/// its version word) is converted first; the played action's steps with no PhotoCraft +/// equivalent come back as warnings (they fail at playback, as in Photoshop when a step can't run). +pub fn load_actions_file(path: &Path, name: Option<&str>) -> Result<(Steps, Vec, Vec), ActionsError> { + let bytes = std::fs::read(path).map_err(|source| ActionsError::Read { path: path.to_path_buf(), source })?; + let is_atn = path.extension().is_some_and(|e| e.eq_ignore_ascii_case("atn")) || bytes.starts_with(&[0, 0, 0, 16]); + if !is_atn { + let text = String::from_utf8(bytes).map_err(|source| ActionsError::Utf8 { path: path.to_path_buf(), source })?; + let (steps, set) = load_actions(&text, name)?; + return Ok((steps, set, Vec::new())); + } + let imp = photocraft_engine::atn_cmds::import(&bytes).map_err(|message| ActionsError::Atn { path: path.to_path_buf(), message })?; + let action = match (name, imp.actions.as_slice()) { + (Some(n), _) => imp.actions.iter().find(|a| a.name == n).ok_or_else(|| ActionsError::NoSuchAtnAction { + name: n.to_owned(), + set: imp.set.clone(), + names: names(&imp.actions), + })?, + (None, [only]) => only, + (None, []) => return Err(ActionsError::EmptyAtn { path: path.to_path_buf() }), + (None, _) => return Err(ActionsError::PickOneAtn { set: imp.set.clone(), names: names(&imp.actions) }), + }; + let warnings = imp + .unsupported + .iter() + .filter(|u| u.action == action.name) + .map(|u| format!("`{}` step {}: Photoshop's \"{}\" ({}) has no PhotoCraft equivalent yet", u.action, u.step + 1, u.name, u.event)) + .chain(imp.warnings.iter().cloned()) + .collect(); + let play = vec![("actions.play".to_string(), json!({"action": action.name}))]; + Ok((play, imp.actions, warnings)) +} diff --git a/apps/photocraft-cli/src/args.rs b/apps/photocraft-cli/src/args.rs new file mode 100644 index 0000000000..91b99ef33b --- /dev/null +++ b/apps/photocraft-cli/src/args.rs @@ -0,0 +1,428 @@ +//! The command line, as clap types. Values are parsed into their types here, so a bad one is a +//! usage error naming its flag (exit 2) and the handlers never see it (#7071). A flag a +//! subcommand doesn't take is a usage error too, so a typo such as `--fromat` can't be silently +//! ignored (#423), and so is a repeated one: `--quality 80 --quality 60` is more likely a mistake +//! than a choice. A value starting with `-` is written `--out=-image.png` (or `./-image.png`), so a +//! value flag never takes the next flag as its value; negative numbers are kept where they mean +//! something. + +use std::ffi::OsString; +use std::fmt; +use std::net::{Ipv4Addr, SocketAddr}; +use std::path::PathBuf; + +use clap::error::ErrorKind; +use clap::parser::ValueSource; +use clap::{ArgAction, ArgGroup, ArgMatches, Args, CommandFactory, FromArgMatches, Parser, Subcommand}; +use photocraft_automation::args::{AutomationArgs, ControlArgs, blank_env_is_unset, conflict, control_port, name_env_vars}; +use photocraft_codecs::{JpegFormat, TiffCompression}; +use photocraft_io::{DdsCompression, DpxBits, TgaBits}; +use serde_json::Value; + +/// photocraft-cli: headless PhotoCraft +#[derive(Parser)] +#[command(name = "photocraft-cli", version = photocraft_engine::build_info::long_version_static(), arg_required_else_help = true)] +pub(crate) struct Cli { + #[command(subcommand)] + pub(crate) command: Command, +} + +#[derive(Subcommand)] +pub(crate) enum Command { + Convert(ConvertArgs), + Info(InfoArgs), + Run(RunArgs), + Batch(BatchArgs), + Droplet(DropletArgs), + Commands(CommandsArgs), + Mcp(McpArgs), + Serve(ServeArgs), +} + +// How an image is written: shared by `convert`, `run` and `batch`. +#[derive(Args)] +pub(crate) struct ExportArgs { + /// Output format, overriding the output's extension (png, jpg, tif, webp, …) + #[arg(long, value_name = "EXT")] + pub(crate) format: Option, + /// JPEG or WebP quality, 1-100. A WebP written with a quality is lossy, without one lossless + // allow_negative_numbers makes `--quality -1` an out-of-range value rather than an unexpected argument. + #[arg(long, value_name = "1-100", value_parser = clap::value_parser!(u8).range(1..=100), allow_negative_numbers = true)] + pub(crate) quality: Option, + /// Keep the layers in TIFF output (Photoshop layer data); TIFF output is flat without it + #[arg(long)] + pub(crate) tiff_layers: bool, + /// Compress TIFF output: none (the default), lzw or zip + #[arg(long, value_name = "none|lzw|zip", value_parser = tiff_compression)] + pub(crate) tiff_compression: Option, + /// A TGA's bits per pixel: 32 writes the alpha channel (or else the transparency) as its + /// alpha, 24 none. Without it: 32 with an alpha channel or transparency, else 24 + #[arg(long, value_name = "24|32", value_parser = tga_bits)] + pub(crate) tga_bits: Option, + /// A DPX's bits per sample. Without it: 8 for an 8-bit document, else 16 + #[arg(long, value_name = "8|10|12|16", value_parser = dpx_bits)] + pub(crate) dpx_bits: Option, + /// A DDS texture's compression: none (uncompressed, the default), bc1 (DXT1: alpha on or + /// off), bc2 (DXT3) or bc3 (DXT5) + #[arg(long, value_name = "none|bc1|bc2|bc3", value_parser = dds_compression)] + pub(crate) dds_compression: Option, + /// Also write a DDS texture's full mip chain + #[arg(long)] + pub(crate) dds_mipmaps: bool, + /// A JPEG's scans, as JPEG Options picks them: baseline (the default), optimized (Huffman + /// tables fitted to the image; RGB is written 4:2:0 and keeps the standard tables, so only Gray + /// and CMYK shrink) or progressive + #[arg(long, value_name = "baseline|optimized|progressive", value_parser = jpeg_format)] + pub(crate) jpeg_format: Option, +} + +/// Convert between formats (.pcraft, .psd, .png, .jpg, .tif, .webp, .exr, …) +/// +/// An that is the file is refused, as the result would replace the original; +/// --in-place allows it. +#[derive(Args)] +pub(crate) struct ConvertArgs { + /// The file to read + #[arg(value_name = "IN")] + pub(crate) input: PathBuf, + /// The file to write; its extension picks the format unless --format is given + #[arg(value_name = "OUT")] + pub(crate) output: PathBuf, + /// Allow an that is the file, replacing the original + #[arg(long)] + pub(crate) in_place: bool, + #[command(flatten)] + pub(crate) export: ExportArgs, +} + +/// Print the document as JSON (size, mode, depth, layer tree) +#[derive(Args)] +pub(crate) struct InfoArgs { + pub(crate) file: PathBuf, + /// One line of JSON instead of pretty-printed + #[arg(long)] + pub(crate) compact: bool, +} + +/// Open a file (or start a new document), run engine commands in order, save the result +/// +/// Prints each command's JSON result. Give exactly one of or --new, and at least one --cmd +/// or --out. An --out that is the is refused, as the result would replace the original; +/// --in-place allows it. +#[derive(Args)] +#[command(group(ArgGroup::new("source").args(["file", "new"]).required(true)))] +#[command(group(ArgGroup::new("work").args(["cmd", "out"]).required(true).multiple(true)))] +pub(crate) struct RunArgs { + /// The document to open + pub(crate) file: Option, + /// Start from a new document with these JSON parameters instead of opening a file + #[arg(long, value_name = "JSON", value_parser = json)] + pub(crate) new: Option, + /// An engine command to run; repeat for several, run in order + #[arg(long, value_name = "ID", action = ArgAction::Append)] + pub(crate) cmd: Vec, + /// JSON parameters for the --cmd before it (default {}) + #[arg(long, value_name = "JSON", action = ArgAction::Append, value_parser = json)] + pub(crate) params: Vec, + /// Save the result to this file + #[arg(long, value_name = "FILE")] + pub(crate) out: Option, + /// Allow an --out that is the , replacing the original + #[arg(long)] + pub(crate) in_place: bool, + #[command(flatten)] + pub(crate) export: ExportArgs, + /// `--cmd` and `--params` values in command-line order, set by [`parse`]. + #[arg(skip)] + pub(crate) steps: Vec, +} + +/// One `--cmd` or `--params` of `run`, in command-line order. +pub(crate) enum Step { + Cmd(String), + Params(Value), +} + +/// Apply an action list to every image in a directory +/// +/// A Photoshop Actions file (.atn) is a set: its action --action names plays, as below. JSON steps +/// are [id, params] pairs, {"command": id, "params": {…}} objects or bare ids, as a recorded +/// action or droplet stores them (a list, or wrapped in {"actions": …}, {"steps": …} or a +/// droplet). An Actions set (the Actions panel's actions.json, {"actions": [{"name", "steps"}…]}) +/// plays the action --action names (needed when the set holds more than one) with the whole set +/// loaded, so its steps can play other actions of the set. +#[derive(Args)] +pub(crate) struct BatchArgs { + /// The action list: actions.json, a droplet, or a Photoshop Actions file (.atn) + #[arg(long, value_name = "FILE")] + pub(crate) actions: PathBuf, + /// Which action of an Actions set to play + #[arg(long, value_name = "NAME")] + pub(crate) action: Option, + /// The folder of images to process + #[arg(long = "in", value_name = "DIR")] + pub(crate) in_dir: PathBuf, + /// The folder to write the results to, as . + #[arg(long, value_name = "DIR")] + pub(crate) out: PathBuf, + /// An --out folder that is the --in folder is refused, as the results would replace the + /// originals; --in-place allows it + #[arg(long)] + pub(crate) in_place: bool, + #[command(flatten)] + pub(crate) export: ExportArgs, +} + +/// Run a droplet (File › Automate › Create Droplet) on images and folders +#[derive(Args)] +pub(crate) struct DropletArgs { + /// The droplet to run + #[arg(value_name = "FILE.pcdroplet")] + pub(crate) file: String, + /// Images and folders to run it on + #[arg(value_name = "FILE-OR-DIR", required = true)] + pub(crate) inputs: Vec, + /// The folder to write the results to + #[arg(long, value_name = "DIR")] + pub(crate) out: Option, +} + +/// List the engine command registry +#[derive(Args)] +pub(crate) struct CommandsArgs { + /// Print the entries as JSON + #[arg(long)] + pub(crate) json: bool, + /// Only the commands matching this text + #[arg(long, value_name = "TEXT", allow_negative_numbers = true)] + pub(crate) filter: Option, +} + +/// Run the MCP server on stdio (headless engine, or bridge to a running `photocraft --control `) +/// +/// Headless: each automation root grants that direction of file access; an omitted root denies it. +/// Prefer absolute root paths in MCP client configurations. Bridge: set the roots on the desktop +/// app; root flags are refused. The token flags require --bridge (or PHOTOCRAFT_CONTROL_PORT). +#[derive(Args)] +pub(crate) struct McpArgs { + /// Bridge to the PhotoCraft app listening here instead of a headless engine; a bare port means + /// 127.0.0.1: + #[arg(long, value_name = "127.0.0.1:PORT", env = "PHOTOCRAFT_CONTROL_PORT", value_parser = bridge)] + pub(crate) bridge: Option, + /// `--bridge` came from PHOTOCRAFT_CONTROL_PORT, set by [`parse`]. + #[arg(skip)] + pub(crate) bridge_from_env: bool, + #[command(flatten)] + pub(crate) control: ControlArgs, + #[command(flatten)] + pub(crate) automation: AutomationArgs, +} + +/// Keep one headless session open and answer JSON lines on stdio, or on 127.0.0.1: +/// +/// Each request is a JSON line {"id", "method", "params"}. Methods: engine.execute, +/// jobs.list/cancel, engine.commands, +/// doc.open/new/place/save/exportLayers/exportDataSets/importDataSets/inspect/render/select/close, +/// session.list, batch, methods (docs/control-protocol.md#headless-server). The token flags require +/// --port (or PHOTOCRAFT_CONTROL_PORT); stdio needs no control token. +#[derive(Args)] +pub(crate) struct ServeArgs { + /// Listen on 127.0.0.1: instead of stdio + #[arg(long, value_name = "PORT", env = "PHOTOCRAFT_CONTROL_PORT", value_parser = control_port)] + pub(crate) port: Option, + /// `--port` came from PHOTOCRAFT_CONTROL_PORT, set by [`parse`]. + #[arg(skip)] + pub(crate) port_from_env: bool, + #[command(flatten)] + pub(crate) control: ControlArgs, + #[command(flatten)] + pub(crate) automation: AutomationArgs, +} + +/// Why a value was refused: clap prints it after the flag and its value (exit 2). +#[derive(Debug, thiserror::Error)] +enum ValueError { + #[error("expected 24 or 32")] + TgaBits, + #[error("expected 8, 10, 12 or 16")] + DpxBits, + #[error("expected none, bc1, bc2 or bc3")] + DdsCompression, + #[error("expected none, lzw or zip")] + TiffCompression, + #[error("expected baseline, optimized or progressive")] + JpegFormat, + /// Why a `--bridge` value, or a PHOTOCRAFT_CONTROL_PORT value for `mcp`, is refused. + #[error("expected a port, 0-65535 (--bridge also takes 127.0.0.1:)")] + Bridge, +} + +/// Why `parse` refuses `mcp` or `serve` options clap accepted: clap prints it as a usage error +/// (exit 2). +#[derive(Debug, thiserror::Error)] +enum ServerOptionError { + /// PHOTOCRAFT_CONTROL_PORT holds a port, as for the desktop app's --control: only the flag + /// takes host:port. + #[error("invalid value '{0}' for 'PHOTOCRAFT_CONTROL_PORT': {bridge}", bridge = ValueError::Bridge)] + BridgeVar(String), + /// With --bridge the roots are the desktop process's to set: accepting them here promises + /// authority the bridge can't give (#8035). + #[error("configure automation roots on the desktop app; --automation-read-root and --automation-write-root require headless MCP mode")] + BridgeRoots, + /// A token flag without the bridge or port that reads it would be ignored (#8035). + #[error("{flag} requires {transport}")] + NeedsTransport { flag: &'static str, transport: &'static str }, +} + +impl ServerOptionError { + fn into_clap(self, cmd: &mut clap::Command) -> clap::Error { + let kind = match self { + Self::BridgeVar(_) => ErrorKind::ValueValidation, + Self::BridgeRoots => ErrorKind::ArgumentConflict, + Self::NeedsTransport { .. } => ErrorKind::MissingRequiredArgument, + }; + cmd.error(kind, self) + } +} + +fn tga_bits(s: &str) -> Result { + s.parse().ok().and_then(TgaBits::from_bits).ok_or(ValueError::TgaBits) +} + +fn dpx_bits(s: &str) -> Result { + s.parse().ok().and_then(DpxBits::from_bits).ok_or(ValueError::DpxBits) +} + +fn dds_compression(s: &str) -> Result { + DdsCompression::from_name(s).ok_or(ValueError::DdsCompression) +} + +fn tiff_compression(s: &str) -> Result { + match s { + "none" => Ok(TiffCompression::None), + "lzw" => Ok(TiffCompression::Lzw), + "zip" => Ok(TiffCompression::Deflate), + _ => Err(ValueError::TiffCompression), + } +} + +fn jpeg_format(s: &str) -> Result { + JpegFormat::parse(s).ok_or(ValueError::JpegFormat) +} + +/// Where `mcp --bridge` connects. Shown as the address: `127.0.0.1:` for a port. +#[derive(Clone)] +pub(crate) enum Bridge { + /// A bare port: the app on 127.0.0.1. + Port(u16), + /// `host:port`, as given. Whether the host is loopback is the bridge's to say. + Addr(String), +} + +impl fmt::Display for Bridge { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Bridge::Port(port) => SocketAddr::from((Ipv4Addr::LOCALHOST, *port)).fmt(f), + Bridge::Addr(addr) => f.write_str(addr), + } + } +} + +/// `mcp --bridge`, or PHOTOCRAFT_CONTROL_PORT, which holds a port. +fn bridge(s: &str) -> Result { + match control_port(s) { + Ok(port) => Ok(Bridge::Port(port)), + Err(_) if s.contains(':') => Ok(Bridge::Addr(s.to_owned())), + Err(_) => Err(ValueError::Bridge), + } +} + +/// `--new` and `--params`: bad JSON is a usage error, before any work. Whether the value is an +/// object is the command's to say. +fn json(s: &str) -> Result { + serde_json::from_str(s) +} + +/// `--cmd` and `--params` values back in command-line order: clap keeps each flag's values apart, +/// their indices say how they interleave. +fn steps(m: &ArgMatches) -> Vec { + let mut steps: Vec<(usize, Step)> = Vec::new(); + if let (Some(at), Ok(Some(ids))) = (m.indices_of("cmd"), m.try_get_many::("cmd")) { + steps.extend(at.zip(ids).map(|(i, id)| (i, Step::Cmd(id.clone())))); + } + if let (Some(at), Ok(Some(params))) = (m.indices_of("params"), m.try_get_many::("params")) { + steps.extend(at.zip(params).map(|(i, p)| (i, Step::Params(p.clone())))); + } + steps.sort_by_key(|s| s.0); + steps.into_iter().map(|(_, s)| s).collect() +} + +/// Parse the arguments (without the binary name). clap reads them as `OsString`s: a path need not +/// be valid Unicode (#1108), text must. A flag left out falls back to its environment variable, +/// unless that is empty, and a bad value from one is a usage error naming the variable (#2491). A +/// bare `--help` or `-h` is help even after a mistake on the same line (`--out --help`, `mcp +/// stray.psd --help`): clap reports the mistake first. +pub(crate) fn parse(args: &[OsString]) -> Result { + let mut cmd = Cli::command().mut_subcommands(|sub| sub.mut_args(blank_env_is_unset)); + let argv = std::iter::once(OsString::from("photocraft-cli")).chain(args.iter().cloned()); + let matches = match cmd.try_get_matches_from_mut(argv) { + Err(e) if args.iter().take_while(|a| *a != "--").any(|a| *a == "--help" || *a == "-h") => { + let help = [OsString::from("photocraft-cli"), args.first().cloned().unwrap_or_default(), OsString::from("--help")]; + return Err(cmd.try_get_matches_from_mut(help).err().unwrap_or(e)); + } + matches => matches.map_err(|e| name_env_vars(e, &cmd, args))?, + }; + let mut cli = Cli::from_arg_matches(&matches)?; + let from_env = |m: &ArgMatches, id: &str| m.value_source(id) == Some(ValueSource::EnvVariable); + match (&mut cli.command, matches.subcommand()) { + (Command::Run(r), Some((_, m))) => r.steps = steps(m), + (Command::Mcp(a), Some((name, m))) => { + a.bridge_from_env = from_env(m, "bridge"); + if a.bridge_from_env + && let Some(Bridge::Addr(v)) = &a.bridge + { + return Err(ServerOptionError::BridgeVar(v.clone()).into_clap(&mut cmd)); + } + if let Some(sub) = cmd.find_subcommand_mut(name) { + // Only the bridge reads a token, so a stale exported pair doesn't stop a headless + // run. + if a.bridge.is_some() { + conflict(sub, m, "control_token", "control_token_file")?; + } + // The roots are the headless engine's: the variable switching to the bridge would + // drop them, and the error names it. + if a.bridge_from_env { + conflict(sub, m, "automation_read_root", "bridge")?; + conflict(sub, m, "automation_write_root", "bridge")?; + } + if a.bridge.is_some() && (a.automation.automation_read_root.is_some() || a.automation.automation_write_root.is_some()) { + return Err(ServerOptionError::BridgeRoots.into_clap(sub)); + } + check_server(sub, m, a.bridge.is_some(), "--bridge")?; + } + } + (Command::Serve(a), Some((name, m))) => { + a.port_from_env = from_env(m, "port"); + if let Some(sub) = cmd.find_subcommand_mut(name) { + // Only the TCP server reads a token, so a stale exported pair doesn't stop stdio. + if a.port.is_some() { + conflict(sub, m, "control_token", "control_token_file")?; + } + check_server(sub, m, a.port.is_some(), "--port")?; + } + } + _ => {} + } + Ok(cli) +} + +/// Refuse a token flag without the bridge or port that reads it (as a flag or its variable). A +/// token variable alone may be a stale export, so it isn't refused. +fn check_server(sub: &mut clap::Command, m: &ArgMatches, has_transport: bool, transport: &'static str) -> Result<(), clap::Error> { + for (id, flag) in [("control_token", "--control-token"), ("control_token_file", "--control-token-file")] { + if m.value_source(id) == Some(ValueSource::CommandLine) && !has_transport { + return Err(ServerOptionError::NeedsTransport { flag, transport }.into_clap(sub)); + } + } + Ok(()) +} diff --git a/apps/photocraft-cli/src/automation.rs b/apps/photocraft-cli/src/automation.rs new file mode 100644 index 0000000000..062f2f2eef --- /dev/null +++ b/apps/photocraft-cli/src/automation.rs @@ -0,0 +1,82 @@ +use std::io::Write; +use std::net::{Ipv4Addr, SocketAddr}; + +use serde_json::Value; + +use photocraft_automation::{Headless, PhotocraftMcp, security}; + +use crate::{ + Result, + args::{CommandsArgs, McpArgs, ServeArgs}, + output::{JsonStyle, print_json}, +}; + +pub(crate) fn commands(a: &CommandsArgs, out: &mut dyn Write) -> Result { + let h = Headless::new(); + let list = h.command_list(); + let needle = a.filter.as_deref().map(str::to_lowercase); + let items: Vec<&Value> = list + .as_array() + .map(|v| v.iter().collect::>()) + .unwrap_or_default() + .into_iter() + .filter(|c| needle.as_ref().is_none_or(|n| photocraft_engine::commands::list_entry_matches(c, n))) + .collect(); + if a.json { + return print_json(out, &Value::Array(items.into_iter().cloned().collect()), JsonStyle::Pretty); + } + for c in items { + let menu = c["menu"].as_array().map(|m| m.iter().filter_map(Value::as_str).collect::>().join(" > ")).unwrap_or_default(); + writeln!(out, "{:<44} {:<32} {}", c["id"].as_str().unwrap_or(""), c["label"].as_str().unwrap_or(""), menu)?; + } + Ok(()) +} + +pub(crate) fn serve(a: &ServeArgs, err: &mut dyn Write) -> Result { + use std::sync::{Arc, Mutex}; + let h = Arc::new(Mutex::new(Headless::with_workspace(a.automation.workspace()?))); + match a.port { + Some(port) => { + if a.port_from_env { + // Name the mode switch: an exported variable changes what this command does, and + // unsetting it (or setting it empty) is the way back to stdio. + let _ = writeln!(err, "photocraft-cli: PHOTOCRAFT_CONTROL_PORT={port} is set; serving on TCP instead of stdio (unset it to serve stdio)"); + } + let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port)).to_string(); + let (supplied, token_file) = (a.control.control_token.as_deref(), a.control.control_token_file.as_deref()); + let token = security::server_token(supplied, token_file)?; + if let Some(path) = token_file { + let _ = writeln!(err, "photocraft-cli control token file: {}", path.display()); + } else if supplied.is_none() { + let _ = writeln!(err, "photocraft-cli control token: {token}"); + } + photocraft_automation::rpc::serve_tcp(&addr, h, token, |local| { + let _ = writeln!(err, "photocraft-cli serving on {local}"); + })?; + Ok(()) + } + None => { + let stdin = std::io::stdin(); + Ok(photocraft_automation::rpc::serve_lines(&h, stdin.lock(), std::io::stdout())?) + } + } +} + +pub(crate) fn mcp(a: &McpArgs, err: &mut dyn Write) -> Result { + let server = match &a.bridge { + Some(addr) => { + if a.bridge_from_env { + // Name the mode switch, as `serve` does: the way back to headless is to unset it. + let _ = writeln!( + err, + "photocraft-cli: PHOTOCRAFT_CONTROL_PORT is set; bridging to the app on {addr} instead of a headless engine (unset it to run headless)" + ); + } + let token = security::client_token(a.control.control_token.as_deref(), a.control.control_token_file.as_deref())?; + PhotocraftMcp::bridge(&addr.to_string(), &token)? + } + None => PhotocraftMcp::headless_with_workspace(a.automation.workspace()?), + }; + let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build()?; + Ok(rt.block_on(server.serve_stdio())?) +} diff --git a/apps/photocraft-cli/src/batch.rs b/apps/photocraft-cli/src/batch.rs new file mode 100644 index 0000000000..02bb1bb702 --- /dev/null +++ b/apps/photocraft-cli/src/batch.rs @@ -0,0 +1,213 @@ +use std::io::Write; +use std::path::{Path, PathBuf}; + +use photocraft_automation::Headless; +use photocraft_engine::actions_cmds::Action; +use photocraft_io::ExportOptions; +use serde_json::json; + +use crate::{ + CliError, Result, + actions::load_actions_file, + args::{BatchArgs, DropletArgs}, + output::warn_all, +}; + +/// Whether `a` and `b` name the same existing folder, however they are spelt (relative, with a +/// trailing separator, through a symbolic link). +fn same_dir(a: &Path, b: &Path) -> bool { + matches!((std::fs::canonicalize(a), std::fs::canonicalize(b)), (Ok(a), Ok(b)) if a == b) +} + +pub(crate) fn is_input(p: &Path) -> bool { + let known = |e: &str| { + matches!(e, "pcraft" | "psd" | "psb" | "pdn" | "ora" | "kra" | "krz" | "af" | "afdesign" | "afphoto" | "afpub") + || photocraft_codecs::from_extension(e).is_some_and(|f| photocraft_codecs::caps(f).read) + }; + p.is_file() && p.extension().is_some_and(|e| known(&e.to_string_lossy().to_ascii_lowercase())) +} + +/// Drops the Save and view steps the desktop records from `action` (Batch saves each result +/// itself, #3143), keeping its unchecked and dialog flags on the steps that remain (#6860). +fn strip_shell_steps(action: &mut Action) { + use photocraft_engine::actions_cmds::{shell_save_command, shell_view_command}; + let (mut steps, mut disabled, mut dialogs) = (Vec::new(), std::collections::BTreeSet::new(), std::collections::BTreeSet::new()); + for (i, (id, p)) in std::mem::take(&mut action.steps).into_iter().enumerate() { + if shell_save_command(&id) || shell_view_command(&id) { + continue; + } + if action.disabled.contains(&i) { + disabled.insert(steps.len()); + } + if action.dialogs.contains(&i) { + dialogs.insert(steps.len()); + } + steps.push((id, p)); + } + (action.steps, action.disabled, action.dialogs) = (steps, disabled, dialogs); +} + +pub(crate) fn batch(a: &BatchArgs, out: &mut dyn Write, err: &mut dyn Write) -> Result { + let (in_dir, out_dir) = (a.in_dir.as_path(), a.out.as_path()); + let (actions, mut set, warnings) = load_actions_file(&a.actions, a.action.as_deref())?; + set.iter_mut().for_each(strip_shell_steps); + warn_all(err, &warnings); + let opts = ExportOptions::from(&a.export); + // Results are saved as `/.`, so an `--out` that is the `--in` folder would + // replace the originals (#492). + if !a.in_place && same_dir(in_dir, out_dir) { + return Err(CliError::OutIsIn(out_dir.to_path_buf())); + } + std::fs::create_dir_all(out_dir)?; + let mut inputs: Vec = std::fs::read_dir(in_dir) + .map_err(|source| CliError::ReadDir { path: in_dir.to_path_buf(), source })? + .flatten() + .map(|e| e.path()) + .filter(|p| is_input(p)) + .collect(); + inputs.sort(); + let (mut ok, mut failed) = (0, 0); + let mut written = photocraft_engine::file_cmds::OutputClaims::default(); + // `--format .jpg` names outputs `.jpg`, as `--format jpg` does (#490). + let format = a.export.format.as_deref().map(|f| f.strip_prefix('.').unwrap_or(f)); + for input in &inputs { + let ext = format.map(str::to_owned).or_else(|| input.extension().map(|e| e.to_string_lossy().into_owned())).unwrap_or_else(|| "png".into()); + let stem = input.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); + let target = out_dir.join(format!("{stem}.{ext}")); + let target_text = target.to_string_lossy(); + let r = (|| -> Result> { + written.check(&target_text).map_err(CliError::OutputClaimed)?; + let mut h = Headless::trusted_local(); + // Steps that play another action find it in the set; the session's playback stack + // stops an action that calls itself. + h.session.actions.list = set.clone(); + h.open(input)?; + for (id, p) in &actions { + if photocraft_engine::actions_cmds::shell_view_command(id) + || photocraft_engine::actions_cmds::shell_save_command(id) + || photocraft_engine::actions_cmds::noop_step(&h.session, id, p) + { + continue; + } + let r = h.command_run(id, p.clone()).map_err(|source| CliError::Command { id: id.clone(), source })?; + // A failed step inside a called action makes this file an error, not a saved result. + if let Some(message) = photocraft_engine::actions_cmds::nested_failure(id, &r) { + return Err(CliError::NestedFailure { id: id.clone(), message }); + } + } + let r = h.save(None, Some(&target), Some(&ext), &opts)?; + Ok(serde_json::from_value(r["warnings"].clone()).unwrap_or_default()) + })(); + match r { + Ok(ws) => { + ok += 1; + written.record(&target_text, &input.to_string_lossy()); + let _ = writeln!(out, "ok {} -> {}", input.display(), target.display()); + warn_all(err, &ws); + } + Err(e) => { + failed += 1; + let _ = writeln!(err, "FAIL {}: {e}", input.display()); + } + } + } + let _ = writeln!(out, "{ok} succeeded, {failed} failed"); + if failed > 0 { Err(CliError::FilesFailed(failed)) } else { Ok(()) } +} + +pub(crate) fn droplet(a: &DropletArgs, out: &mut dyn Write, err: &mut dyn Write) -> Result { + let (file, inputs) = (&a.file, &a.inputs); + let mut p = json!({"droplet": file, "input": inputs}); + if let Some(o) = &a.out { + p["output"] = json!(o); + } + let mut s = photocraft_engine::Session::new(); + let r = s.execute("file.automate.runDroplet", p)?; + for f in r["files"].as_array().into_iter().flatten() { + let _ = writeln!(out, "ok {}", f.as_str().unwrap_or_default()); + } + let errors = r["errors"].as_array().cloned().unwrap_or_default(); + for e in &errors { + let _ = writeln!(err, "FAIL {}: {}", e["file"].as_str().unwrap_or_default(), e["error"].as_str().unwrap_or_default()); + } + if errors.is_empty() { Ok(()) } else { Err(CliError::FilesFailed(errors.len())) } +} + +#[cfg(test)] +mod batch_action_set_tests { + use super::*; + use crate::test_support::{Invoke, invoke}; + use serde_json::Value; + + /// A batched action that plays another action of its set finds it in every file's session; a + /// failure inside the called action fails the file, and a self-call stops instead of looping. + #[test] + fn batch_plays_an_action_that_calls_another_action_of_its_set() { + let dir = std::env::temp_dir().join(format!("photocraft-cli-action-set-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let path = |name: &str| dir.join(name).to_string_lossy().to_string(); + std::fs::create_dir_all(dir.join("in")).unwrap(); + for name in ["a", "b"] { + let Invoke { code, stderr, .. } = invoke(&["run", "--new", r#"{"width":8,"height":6}"#, "--out", &path(&format!("in/{name}.png"))]); + assert_eq!(code, 0, "{stderr}"); + } + let set = json!({"version": 1, "actions": [ + {"name": "Outer", "steps": [["actions.play", {"action": "Rotate"}]]}, + {"name": "Rotate", "steps": [["image.imageRotation.90cw", {}]]}, + {"name": "Calls bad", "steps": [["actions.play", {"action": "Bad"}]]}, + {"name": "Bad", "steps": [["layer.delete", {"layer": 999_999}]]}, + {"name": "Loop", "steps": [["actions.play", {"action": "Loop"}]]}, + ]}); + std::fs::write(dir.join("actions.json"), set.to_string()).unwrap(); + let batch = |action: &str| invoke(&["batch", "--actions", &path("actions.json"), "--action", action, "--in", &path("in"), "--out", &path(action)]); + + let Invoke { code, stdout, stderr } = batch("Outer"); + assert_eq!(code, 0, "{stdout}{stderr}"); + for name in ["a", "b"] { + let Invoke { code, stdout: info, stderr } = invoke(&["info", &path(&format!("Outer/{name}.png")), "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&info).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(6), Some(8)), "rotated by the called action"); + } + for action in ["Calls bad", "Loop"] { + let Invoke { code, stdout, stderr } = batch(action); + assert_ne!(code, 0, "{stdout}"); + assert_eq!(stderr.matches("FAIL").count(), 2, "{stderr}"); + assert!(!dir.join(action).join("a.png").exists(), "a file whose called action failed is not saved"); + } + let _ = std::fs::remove_dir_all(&dir); + } + + /// Save and view steps recorded inside a batched action set are skipped, also in a called + /// action, and unchecked steps stay unchecked once they are gone (#6860). + #[test] + fn batch_skips_save_and_view_steps_inside_an_action_set() { + let dir = std::env::temp_dir().join(format!("photocraft-cli-action-set-save-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let path = |name: &str| dir.join(name).to_string_lossy().to_string(); + std::fs::create_dir_all(dir.join("in")).unwrap(); + let Invoke { code, stderr, .. } = invoke(&["run", "--new", r#"{"width":8,"height":6}"#, "--out", &path("in/a.png")]); + assert_eq!(code, 0, "{stderr}"); + let set = json!({"version": 1, "actions": [ + {"name": "Outer", "steps": [["actions.play", {"action": "Rotate"}], ["file.saveAs", {}]]}, + {"name": "Rotate", "steps": [ + ["file.save", {}], + ["image.imageRotation.90cw", {}], + ["view.fitOnScreen", {}], + ["image.imageRotation.90cw", {}], + ["file.saveAs", {}], + ], "disabled": [3], "dialogs": [1]}, + ]}); + std::fs::write(dir.join("actions.json"), set.to_string()).unwrap(); + for action in ["Outer", "Rotate"] { + let Invoke { code, stdout, stderr } = + invoke(&["batch", "--actions", &path("actions.json"), "--action", action, "--in", &path("in"), "--out", &path(action)]); + assert_eq!(code, 0, "{stdout}{stderr}"); + let Invoke { code, stdout: info, stderr } = invoke(&["info", &path(&format!("{action}/a.png")), "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&info).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(6), Some(8)), "{action}: rotated once, the unchecked step skipped"); + } + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/apps/photocraft-cli/src/document.rs b/apps/photocraft-cli/src/document.rs new file mode 100644 index 0000000000..95d4c28288 --- /dev/null +++ b/apps/photocraft-cli/src/document.rs @@ -0,0 +1,231 @@ +use std::io::Write; +use std::path::Path; + +use photocraft_automation::{Headless, files}; +use serde_json::{Value, json}; + +use photocraft_engine::type_extra_cmds::missing_font_warnings; +use photocraft_io::ExportOptions; + +use crate::{ + CliError, Result, + args::{ConvertArgs, InfoArgs, RunArgs, Step}, + output::{JsonStyle, print_json, warn_all}, +}; + +/// An output that is the input file itself, however it is spelt (relative, with `./`, through a +/// hard or symbolic link), would replace the original, and a lossy re-encode can't be undone: it +/// is refused unless `--in-place` asks for it, as `batch` refuses an `--out` that is its `--in` +/// folder (#7437). The paths are compared when the command starts and the save opens `output` +/// again later, so another process swapping in a link meanwhile goes unnoticed: this guards +/// against mistakes, it is not a security boundary. +fn refuse_overwriting_input(in_place: bool, input: &Path, output: &Path) -> Result { + if !in_place && photocraft_engine::file_cmds::same_file(input, output) { + return Err(CliError::OutIsInput(output.to_path_buf())); + } + Ok(()) +} + +pub(crate) fn convert(a: &ConvertArgs, _out: &mut dyn Write, err: &mut dyn Write) -> Result { + let (input, output) = (&a.input, &a.output); + let opts = ExportOptions::from(&a.export); + refuse_overwriting_input(a.in_place, input, output)?; + let mut o = files::open(input)?; + // Preferences › Raw Defaults › Resolution at its default, as `run` and `info` open a raw. + photocraft_engine::file_cmds::raw_defaults(&photocraft_engine::Session::new(), &o.warnings, &mut o.document); + warn_all(err, &o.warnings); + warn_all(err, &missing_font_warnings(&o.document)); + let ws = files::save(&o.document, output, a.export.format.as_deref(), &opts, None)?; + warn_all(err, &ws); + Ok(()) +} + +pub(crate) fn info(a: &InfoArgs, out: &mut dyn Write) -> Result { + let file = &a.file; + let mut h = Headless::trusted_local(); + let opened = h.open(file)?; + let mut doc = h.inspect(None)?; + let mut warnings = opened["warnings"].as_array().cloned().unwrap_or_default(); + if let Some(st) = h.session.active() { + warnings.extend(missing_font_warnings(&st.doc).into_iter().map(Value::String)); + } + doc["warnings"] = Value::Array(warnings); + doc["file"] = json!(file.to_string_lossy()); + // Drop per-session noise. + if let Value::Object(m) = &mut doc { + for k in ["history", "canUndo", "canRedo", "revision"] { + m.remove(k); + } + } + print_json(out, &doc, if a.compact { JsonStyle::Compact } else { JsonStyle::Pretty }) +} + +/// `(id, params)` pairs from repeated `--cmd`/`--params` flags. +fn command_list(a: &RunArgs) -> Result> { + let mut cmds: Vec<(String, Value)> = Vec::new(); + for step in &a.steps { + match step { + Step::Cmd(id) => cmds.push((id.clone(), json!({}))), + Step::Params(p) => cmds.last_mut().ok_or(CliError::ParamsBeforeCmd)?.1 = p.clone(), + } + } + Ok(cmds) +} + +pub(crate) fn run_cmds(a: &RunArgs, out: &mut dyn Write, err: &mut dyn Write) -> Result { + let opts = ExportOptions::from(&a.export); + let mut h = Headless::trusted_local(); + // Opening, running commands and exporting can all notice the same missing font: + // one `run` invocation reports each warning line at most once. + let mut reported_fonts = std::collections::HashSet::::new(); + // clap's `source` group gives exactly one of the two, and its `work` group a --cmd or --out. + if let Some(new) = &a.new { + h.command_run("file.new", new.clone()).map_err(CliError::New)?; + } else if let Some(file) = &a.file { + // Refused before the file is read or any command runs. + if let Some(out) = &a.out { + refuse_overwriting_input(a.in_place, file, out)?; + } + let o = h.open(file)?; + let ws: Vec = serde_json::from_value(o["warnings"].clone()).unwrap_or_default(); + warn_all(err, &ws); + reported_fonts.extend(ws); + } + let cmds = command_list(a)?; + for (id, params) in cmds { + // A step that is a silent no-op in action playback (Deselect with nothing selected, an Arrange that cannot move the layer) is one in a script too (#8802). + if photocraft_engine::actions_cmds::noop_step(&h.session, &id, ¶ms) { + print_json(out, &json!({"command": id, "result": null}), JsonStyle::Compact)?; + continue; + } + let r = h.command_run(&id, params).map_err(|source| CliError::Command { id: id.clone(), source })?; + if let Some(message) = photocraft_engine::actions_cmds::nested_failure(&id, &r) { + return Err(CliError::NestedFailure { id, message }); + } + let new_warnings: Vec = + h.session.active().map(|st| missing_font_warnings(&st.doc)).unwrap_or_default().into_iter().filter(|w| reported_fonts.insert(w.clone())).collect(); + warn_all(err, &new_warnings); + let mut result = json!({"command": id, "result": r}); + if !new_warnings.is_empty() { + result["warnings"] = json!(new_warnings); + } + print_json(out, &result, JsonStyle::Compact)?; + } + // A `run` with only `--out` still reports missing fonts before exporting. + let remaining: Vec = + h.session.active().map(|st| missing_font_warnings(&st.doc)).unwrap_or_default().into_iter().filter(|w| reported_fonts.insert(w.clone())).collect(); + warn_all(err, &remaining); + if let Some(o) = a.out.as_deref() { + let r = h.save(None, Some(o), a.export.format.as_deref(), &opts)?; + let ws: Vec = serde_json::from_value(r["warnings"].clone()).unwrap_or_default(); + let ws: Vec = ws.into_iter().filter(|w| reported_fonts.insert(w.clone())).collect(); + warn_all(err, &ws); + } + Ok(()) +} + +#[cfg(test)] +mod missing_font_warning_tests { + use super::*; + use crate::{ + batch::is_input, + test_support::{Invoke, invoke}, + }; + + #[test] + fn missing_typeface_is_reported_by_run_info_and_convert() { + let family = "DefinitelyMissingPhotoCraftTypeface1251"; + let folder = std::env::temp_dir().join(format!("photocraft-cli-font-warnings-{}", std::process::id())); + std::fs::create_dir_all(&folder).unwrap(); + let native = folder.join("missing.pcraft"); + let png = folder.join("missing.png"); + let native_str = native.to_string_lossy().to_string(); + let png_str = png.to_string_lossy().to_string(); + let params = json!({"x": 6, "y": 38, "text": "Missing font warning", "font": family, "size": 20}).to_string(); + + let Invoke { code, stdout, stderr } = invoke(&[ + "run", + "--new", + r#"{"width":256,"height":96,"background":"white"}"#, + "--cmd", + "type.create", + "--params", + ¶ms, + "--cmd", + "type.resolveMissingFonts", + "--out", + &native_str, + ]); + assert_eq!(code, 0, "{stderr}"); + assert_eq!(stderr.matches(family).count(), 1, "multi-command runs should not repeat the same missing font warning"); + let commands: Vec = stdout.lines().map(|s| serde_json::from_str(s).unwrap()).collect(); + assert_eq!(commands.len(), 2, "{stdout}"); + assert!(commands[0]["warnings"].as_array().unwrap().iter().any(|w| w.as_str().is_some_and(|s| s.contains(family)))); + assert!(commands[1].get("warnings").is_none(), "only newly detected missing fonts are reported"); + + let Invoke { code, stdout, stderr } = invoke(&["info", &native_str, "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert!(info["warnings"].as_array().unwrap().iter().any(|w| w.as_str().is_some_and(|s| s.contains(family)))); + assert!(stderr.is_empty(), "info puts warnings in JSON, not stderr"); + + let Invoke { code, stderr, .. } = invoke(&["convert", &native_str, &png_str]); + assert_eq!(code, 0, "{stderr}"); + assert!(stderr.contains(&format!("warning: font '{family}'")), "{stderr}"); + assert!(png.is_file()); + + // Even with no commands, run --out checks the opened document before export. + let Invoke { code, stdout, stderr } = invoke(&["run", &native_str, "--out", &png_str]); + assert_eq!(code, 0, "{stderr}"); + assert!(stdout.is_empty()); + assert_eq!(stderr.matches(family).count(), 1); + + let _ = std::fs::remove_dir_all(&folder); + } + + #[test] + fn convert_reads_and_writes_openraster() { + let folder = std::env::temp_dir().join(format!("photocraft-cli-ora-{}", std::process::id())); + std::fs::create_dir_all(&folder).unwrap(); + let path = |name: &str| folder.join(name).to_string_lossy().to_string(); + let (native, ora, png) = (path("layers.pcraft"), path("layers.ora"), path("back.png")); + let Invoke { code, stderr, .. } = invoke(&["run", "--new", r#"{"width":16,"height":12}"#, "--cmd", "layer.new.layer", "--out", &native]); + assert_eq!(code, 0, "{stderr}"); + let Invoke { code, stderr, .. } = invoke(&["convert", &native, &ora]); + assert_eq!(code, 0, "{stderr}"); + let Invoke { code, stdout, stderr } = invoke(&["info", &ora, "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!(info["layers"].as_array().map(Vec::len), Some(2), "{info}"); + let Invoke { code, stderr, .. } = invoke(&["convert", &ora, &png]); + assert_eq!(code, 0, "{stderr}"); + assert!(is_input(std::path::Path::new(&ora))); + // Krita documents are batch inputs too (import-only). + for name in ["sketch.kra", "old.KRZ"] { + std::fs::write(folder.join(name), b"").unwrap(); + assert!(is_input(&folder.join(name)), "{name}"); + } + let _ = std::fs::remove_dir_all(&folder); + } + + #[test] + fn no_type_layers_do_not_produce_missing_font_warnings() { + let folder = std::env::temp_dir().join(format!("photocraft-cli-no-font-warning-{}", std::process::id())); + std::fs::create_dir_all(&folder).unwrap(); + let native = folder.join("blank.pcraft"); + let png = folder.join("blank.png"); + let native_str = native.to_string_lossy().to_string(); + let png_str = png.to_string_lossy().to_string(); + let Invoke { code, stderr, .. } = invoke(&["run", "--new", r#"{"width":16,"height":16}"#, "--out", &native_str]); + assert_eq!(code, 0, "{stderr}"); + assert!(!stderr.contains("font '"), "{stderr}"); + let Invoke { code, stdout, stderr } = invoke(&["info", &native_str, "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert!(info["warnings"].as_array().unwrap().iter().all(|w| !w.as_str().unwrap_or_default().contains("font '"))); + let Invoke { code, stderr, .. } = invoke(&["convert", &native_str, &png_str]); + assert_eq!(code, 0, "{stderr}"); + assert!(!stderr.contains("font '"), "{stderr}"); + let _ = std::fs::remove_dir_all(&folder); + } +} diff --git a/apps/photocraft-cli/src/error.rs b/apps/photocraft-cli/src/error.rs new file mode 100644 index 0000000000..3e4aa3caef --- /dev/null +++ b/apps/photocraft-cli/src/error.rs @@ -0,0 +1,42 @@ +use std::path::PathBuf; + +use photocraft_automation::AutomationError; + +use crate::actions::ActionsError; + +/// Why a subcommand failed: printed as `error: {e}`, exit 1. +#[derive(Debug, thiserror::Error)] +pub(crate) enum CliError { + #[error(transparent)] + Automation(#[from] AutomationError), + #[error(transparent)] + Engine(#[from] photocraft_engine::EngineError), + #[error(transparent)] + Io(#[from] std::io::Error), + #[error(transparent)] + Json(#[from] serde_json::Error), + #[error(transparent)] + Actions(#[from] ActionsError), + #[error("{}: {source}", path.display())] + ReadDir { path: PathBuf, source: std::io::Error }, + #[error("--params must follow a --cmd")] + ParamsBeforeCmd, + #[error("--new: {0}")] + New(#[source] AutomationError), + #[error("`{id}`: {source}")] + Command { id: String, source: AutomationError }, + /// A step inside an action the command played failed ([`photocraft_engine::actions_cmds::nested_failure`]). + #[error("`{id}`: {message}")] + NestedFailure { id: String, message: String }, + #[error("{} is the input file, so the result would replace the original: choose another output, or pass --in-place to overwrite it", .0.display())] + OutIsInput(PathBuf), + #[error("--out {} is the --in folder, so the results would replace the originals: choose another --out folder, or pass --in-place to overwrite them", .0.display())] + OutIsIn(PathBuf), + /// Another input already wrote this output ([`photocraft_engine::file_cmds::OutputClaims::check`]). + #[error("{0}")] + OutputClaimed(String), + #[error("{0} file(s) failed")] + FilesFailed(usize), +} + +pub(crate) type Result = std::result::Result; diff --git a/apps/photocraft-cli/src/lib.rs b/apps/photocraft-cli/src/lib.rs index 03001ab02d..c431b5529f 100644 --- a/apps/photocraft-cli/src/lib.rs +++ b/apps/photocraft-cli/src/lib.rs @@ -3,197 +3,63 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +use std::ffi::OsString; use std::io::Write; -use std::path::{Path, PathBuf}; -use photocraft_automation::{AuthorizedWorkspace, Headless, PhotocraftMcp, files, security}; -use photocraft_engine::actions_cmds::Action; -use photocraft_io::ExportOptions; -use serde_json::{Value, json}; +use clap::CommandFactory; -pub const USAGE: &str = "\ -photocraft-cli: headless PhotoCraft +mod actions; +mod args; +mod automation; +mod batch; +mod document; +mod error; +mod output; -USAGE: - photocraft-cli convert [--format ] [--quality <1-100>] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] - Convert between formats (.pcraft, .psd, .png, .jpg, .tif, .webp, .exr, …). - TIFF output is flat unless --tiff-layers keeps the layers (Photoshop layer data). - --quality sets the JPEG or WebP quality; a WebP written with a quality is lossy, without one lossless. - --tga-bits sets a TGA's bits per pixel: 32 writes the alpha channel (or else the transparency) as - its alpha, 24 none. Without it: 32 with an alpha channel or transparency, else 24. - --dpx-bits sets a DPX's bits per sample. Without it: 8 for an 8-bit document, else 16. - photocraft-cli info [--compact] - Print the document as JSON (size, mode, depth, layer tree). - photocraft-cli run ( | --new ) --cmd [--params ] [--cmd …] [--out ] [--format ] [--quality <1-100>] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] - Open a file, run engine commands in order, save the result. Each --params - applies to the preceding --cmd. Prints each command's JSON result. - photocraft-cli batch --actions [--action ] --in --out [--format ] [--quality <1-100>] [--in-place] [--tiff-layers] [--tga-bits <24|32>] [--dpx-bits <8|10|12|16>] - Apply an action list to every image in a directory. A Photoshop Actions file (.atn) is a - set: its action --action names plays, as below. JSON steps are [id, params] pairs, - {\"command\": id, \"params\": {…}} objects or bare ids, as a recorded action or droplet stores them - (a list, or wrapped in {\"actions\": …}, {\"steps\": …} or a droplet). An Actions set (the Actions - panel's actions.json, {\"actions\": [{\"name\", \"steps\"}…]}) plays the action --action names (needed - when the set holds more than one) with the whole set loaded, so its steps can play other actions - of the set. An --out folder that is the --in folder is refused, as the results would replace - the originals; --in-place allows it. - photocraft-cli droplet … [--out ] - Run a droplet (File › Automate › Create Droplet) on images and folders. - photocraft-cli commands [--json] [--filter ] - List the engine command registry. - photocraft-cli mcp [--bridge <127.0.0.1:port>] [--control-token <64-hex> | --control-token-file ] - [--automation-read-root ] [--automation-write-root ] - Run the MCP server on stdio (headless engine, or bridge to a running `photocraft --control `). - photocraft-cli serve [--port ] [--control-token <64-hex> | --control-token-file ] - [--automation-read-root ] [--automation-write-root ] - Keep one headless session open and answer JSON lines ({\"id\",\"method\",\"params\"}) on stdio, - or on 127.0.0.1:. Methods: engine.execute, jobs.list/cancel, engine.commands, - doc.open/new/place/save/exportLayers/exportDataSets/importDataSets/inspect/render/select/close, session.list, batch, methods - (docs/control-protocol.md#headless-server). +pub use actions::{ActionsError, Steps, load_actions, load_actions_file, parse_actions}; +use args::{Command, parse}; +use automation::{commands, mcp, serve}; +use batch::{batch, droplet}; +use document::{convert, info, run_cmds}; +pub(crate) use error::{CliError, Result}; - photocraft-cli --help (or -h) prints this text. A flag the subcommand doesn't take is - an error. -"; - -struct Args { - positional: Vec, - flags: Vec<(String, Option)>, +/// The command line's clap definition, for generating shell completions and man pages +/// (`examples/man-and-completions.rs`). +pub fn command() -> clap::Command { + args::Cli::command() } -/// A subcommand: the flags it reads (taking a value, or given bare) and what it runs. A flag -/// outside these is a usage error, so a typo such as `--fromat` can't be silently ignored (#423). -struct Subcommand { - name: &'static str, - values: &'static [&'static str], - bare: &'static [&'static str], - run: fn(&Args, &mut dyn Write, &mut dyn Write) -> R, -} - -const SUBCOMMANDS: &[Subcommand] = &[ - Subcommand { name: "convert", values: &["--format", "--quality", "--tga-bits", "--dpx-bits"], bare: &["--tiff-layers"], run: convert }, - Subcommand { name: "info", values: &[], bare: &["--compact"], run: |a, out, _| info(a, out) }, - Subcommand { - name: "run", - values: &["--new", "--cmd", "--params", "--out", "--format", "--quality", "--tga-bits", "--dpx-bits"], - bare: &["--tiff-layers"], - run: run_cmds, - }, - Subcommand { - name: "batch", - values: &["--actions", "--action", "--in", "--out", "--format", "--quality", "--tga-bits", "--dpx-bits"], - bare: &["--in-place", "--tiff-layers"], - run: batch, - }, - Subcommand { name: "droplet", values: &["--out"], bare: &[], run: droplet }, - Subcommand { name: "commands", values: &["--filter"], bare: &["--json"], run: |a, out, _| commands(a, out) }, - Subcommand { - name: "mcp", - values: &["--bridge", "--control-token", "--control-token-file", "--automation-read-root", "--automation-write-root"], - bare: &[], - run: |a, _, _| mcp(a), - }, - Subcommand { - name: "serve", - values: &["--port", "--control-token", "--control-token-file", "--automation-read-root", "--automation-write-root"], - bare: &[], - run: |a, _, err| serve(a, err), - }, -]; - -/// The subcommand's arguments, or `None` when they ask for help (`--help` or `-h`). -fn parse(sub: &Subcommand, args: &[String]) -> Result, String> { - let mut a = Args { positional: Vec::new(), flags: Vec::new() }; - let unknown = |flag: &str| format!("unknown flag {flag} for {}", sub.name); - let mut i = 0; - while let Some(s) = args.get(i) { - if s == "--help" || s == "-h" { - return Ok(None); - } - if let Some((k, v)) = s.split_once('=').filter(|(k, _)| k.starts_with("--")) { - if sub.bare.contains(&k) { - return Err(format!("{k} takes no value")); - } - if !sub.values.contains(&k) { - return Err(unknown(k)); - } - a.flags.push((k.to_owned(), Some(v.to_owned()))); - } else if sub.values.contains(&s.as_str()) { - let v = args.get(i + 1).ok_or_else(|| format!("{s} needs a value"))?; - a.flags.push((s.clone(), Some(v.clone()))); - i += 1; - } else if sub.bare.contains(&s.as_str()) { - a.flags.push((s.clone(), None)); - } else if s.starts_with("--") { - return Err(unknown(s)); - } else { - a.positional.push(s.clone()); - } - i += 1; - } - Ok(Some(a)) -} - -impl Args { - fn get(&self, k: &str) -> Option<&str> { - self.flags.iter().rev().find(|(f, _)| f == k).and_then(|(_, v)| v.as_deref()) - } - fn has(&self, k: &str) -> bool { - self.flags.iter().any(|(f, _)| f == k) - } -} - -type R = Result<(), String>; - -/// [`run`] on the raw process arguments. An argument that is not valid Unicode is a usage error -/// naming its position and a lossy rendering (exit 2), never a panic: `std::env::args()` aborts -/// the process on one (issue #1108). -pub fn run_os(args: &[std::ffi::OsString], out: &mut dyn Write, err: &mut dyn Write) -> i32 { - let mut strings = Vec::with_capacity(args.len()); - for (i, a) in args.iter().enumerate() { - match a.clone().into_string() { - Ok(s) => strings.push(s), - Err(raw) => { - let _ = writeln!(err, "error: argument {} is not valid Unicode: `{}`\n\n{USAGE}", i + 1, raw.to_string_lossy()); - return 2; - } - } - } - run(&strings, out, err) -} - -/// Run the CLI. Returns the process exit code (0 ok, 1 failure, 2 usage). -pub fn run(args: &[String], out: &mut dyn Write, err: &mut dyn Write) -> i32 { - let Some(cmd) = args.first() else { - let _ = write!(err, "{USAGE}"); - return 2; - }; - match cmd.as_str() { - "-h" | "--help" | "help" => { - let _ = write!(out, "{USAGE}"); - return 0; - } - "--version" | "version" => { - let _ = writeln!(out, "photocraft-cli {}", photocraft_engine::build_info::long_version()); - return 0; - } - _ => {} - } - let Some(sub) = SUBCOMMANDS.iter().find(|s| s.name == cmd) else { - let _ = writeln!(err, "error: unknown command `{cmd}`\n\n{USAGE}"); - return 2; - }; - let parsed = match parse(sub, &args[1..]) { - Ok(Some(p)) => p, - // ` --help`: usage, without reading inputs or starting a server. - Ok(None) => { - let _ = write!(out, "{USAGE}"); - return 0; - } +/// Run the CLI on its arguments (without the program name). Returns the process exit code (0 ok, +/// 1 failure, 2 usage). clap reads the arguments as they are: a path that is not valid Unicode (a +/// Latin-1 file name on Linux) is taken, and one where text is expected is a usage error (exit 2). +/// Never a panic, as `std::env::args()` aborts on one (issue #1108). +pub fn run(args: I, out: &mut dyn Write, err: &mut dyn Write) -> i32 +where + I: IntoIterator, + T: Into, +{ + let args: Vec = args.into_iter().map(Into::into).collect(); + // Help and `--version` (`help`, `-h`, ` --help`, `-V`) come back as errors too, + // for `out`, exit 0: the subcommand never runs. + let cli = match parse(&args) { + Ok(cli) => cli, Err(e) => { - let _ = writeln!(err, "error: {e}\n\n{USAGE}"); - return 2; + let to: &mut dyn Write = if e.use_stderr() { err } else { out }; + let _ = write!(to, "{}", e.render()); + return e.exit_code(); } }; - match (sub.run)(&parsed, out, err) { + let result = match &cli.command { + Command::Convert(a) => convert(a, out, err), + Command::Info(a) => info(a, out), + Command::Run(a) => run_cmds(a, out, err), + Command::Batch(a) => batch(a, out, err), + Command::Droplet(a) => droplet(a, out, err), + Command::Commands(a) => commands(a, out), + Command::Mcp(a) => mcp(a, err), + Command::Serve(a) => serve(a, err), + }; + match result { Ok(()) => 0, Err(e) => { let _ = writeln!(err, "error: {e}"); @@ -202,553 +68,18 @@ pub fn run(args: &[String], out: &mut dyn Write, err: &mut dyn Write) -> i32 { } } -fn export_opts(a: &Args) -> Result { - // TIFF output is flat unless asked: scripted conversions keep predictable sizes. - let mut o = ExportOptions { tiff_layers: a.has("--tiff-layers"), ..Default::default() }; - if let Some(q) = a.get("--quality") { - let q = q.parse().ok().filter(|q| (1..=100).contains(q)).ok_or_else(|| format!("bad --quality `{q}`: expected a whole number from 1 to 100"))?; - o.encode.jpeg_quality = q; - // Asking for a quality asks for a lossy WebP; the default WebP stays lossless. - o.encode.webp_quality = q; - o.encode.webp_lossless = false; - } - if let Some(b) = a.get("--tga-bits") { - o.tga_bits = Some(b.parse().ok().and_then(photocraft_io::TgaBits::from_bits).ok_or_else(|| format!("bad --tga-bits `{b}`: expected 24 or 32"))?); - } - if let Some(b) = a.get("--dpx-bits") { - o.encode.dpx_bits = - Some(b.parse().ok().and_then(photocraft_io::DpxBits::from_bits).ok_or_else(|| format!("bad --dpx-bits `{b}`: expected 8, 10, 12 or 16"))?); - } - Ok(o) -} - -fn warn_all(err: &mut dyn Write, ws: &[String]) { - for w in ws { - let _ = writeln!(err, "warning: {w}"); - } -} - -/// The engine knows which type families aren't installed. Warn before an export -/// uses a fallback face rather than silently changing the appearance of the text. -fn missing_font_warnings(fonts: Vec) -> Vec { - fonts.into_iter().map(|font| format!("font '{font}' is not installed; text may render using a fallback face")).collect() -} - -fn automation_workspace(args: &Args) -> Result { - AuthorizedWorkspace::new(args.get("--automation-read-root").map(Path::new), args.get("--automation-write-root").map(Path::new)) - .map_err(|error| error.to_string()) -} - -fn convert(a: &Args, _out: &mut dyn Write, err: &mut dyn Write) -> R { - let [input, output] = a.positional.as_slice() else { - return Err("convert needs ".into()); - }; - let opts = export_opts(a)?; - let o = files::open(Path::new(input)).map_err(|e| e.to_string())?; - warn_all(err, &o.warnings); - warn_all(err, &photocraft_engine::type_extra_cmds::missing_font_warnings(&o.document)); - let ws = files::save(&o.document, Path::new(output), a.get("--format"), &opts, None).map_err(|e| e.to_string())?; - warn_all(err, &ws); - Ok(()) -} - -fn print_json(out: &mut dyn Write, v: &Value, compact: bool) -> R { - let s = if compact { serde_json::to_string(v) } else { serde_json::to_string_pretty(v) }.map_err(|e| e.to_string())?; - writeln!(out, "{s}").map_err(|e| e.to_string()) -} - -fn info(a: &Args, out: &mut dyn Write) -> R { - let [file] = a.positional.as_slice() else { - return Err("info needs ".into()); - }; - let mut h = Headless::trusted_local(); - let opened = h.open(Path::new(file)).map_err(|e| e.to_string())?; - let mut doc = h.inspect(None).map_err(|e| e.to_string())?; - let mut warnings = opened["warnings"].as_array().cloned().unwrap_or_default(); - if let Some(st) = h.session.active() { - warnings.extend(missing_font_warnings(photocraft_engine::type_extra_cmds::missing_fonts(&st.doc)).into_iter().map(Value::String)); - } - doc["warnings"] = Value::Array(warnings); - doc["file"] = json!(file); - // Drop per-session noise. - if let Value::Object(m) = &mut doc { - for k in ["history", "canUndo", "canRedo", "revision"] { - m.remove(k); - } - } - print_json(out, &doc, a.has("--compact")) -} - -/// `(id, params)` pairs from repeated `--cmd`/`--params` flags. -fn command_list(a: &Args) -> Result, String> { - let mut cmds: Vec<(String, Value)> = Vec::new(); - for (k, v) in &a.flags { - match (k.as_str(), v) { - ("--cmd", Some(id)) => cmds.push((id.clone(), json!({}))), - ("--params", Some(p)) => { - let last = cmds.last_mut().ok_or("--params must follow a --cmd")?; - last.1 = serde_json::from_str(p).map_err(|e| format!("bad --params JSON for `{}`: {e}", last.0))?; - } - _ => {} - } - } - Ok(cmds) -} - -fn run_cmds(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { - let opts = export_opts(a)?; - let mut h = Headless::trusted_local(); - // Opening, running commands and exporting can all notice the same missing font: - // one `run` invocation reports each warning line at most once. - let mut reported_fonts = std::collections::HashSet::::new(); - match (a.positional.as_slice(), a.get("--new")) { - ([file], None) => { - let o = h.open(Path::new(file)).map_err(|e| e.to_string())?; - let ws: Vec = serde_json::from_value(o["warnings"].clone()).unwrap_or_default(); - reported_fonts.extend(ws.iter().cloned()); - warn_all(err, &ws); - reported_fonts.extend(ws); - } - ([], Some(new)) => { - let p: Value = serde_json::from_str(new).map_err(|e| format!("bad --new JSON: {e}"))?; - h.command_run("file.new", p).map_err(|e| format!("--new: {e}"))?; - } - _ => return Err("run needs exactly one of or --new ".into()), - } - let cmds = command_list(a)?; - if cmds.is_empty() && a.get("--out").is_none() { - return Err("run needs at least one --cmd (or --out)".into()); - } - for (id, params) in cmds { - let r = h.command_run(&id, params).map_err(|e| format!("`{id}`: {e}"))?; - if let Some(e) = photocraft_engine::actions_cmds::nested_failure(&id, &r) { - return Err(format!("`{id}`: {e}")); - } - let new_warnings: Vec = h - .session - .active() - .map(|st| missing_font_warnings(photocraft_engine::type_extra_cmds::missing_fonts(&st.doc))) - .unwrap_or_default() - .into_iter() - .filter(|w| reported_fonts.insert(w.clone())) - .collect(); - warn_all(err, &new_warnings); - let mut result = json!({"command": id, "result": r}); - if !new_warnings.is_empty() { - result["warnings"] = json!(new_warnings); - } - print_json(out, &result, true)?; - } - // A `run` with only `--out` still reports missing fonts before exporting. - let remaining: Vec = h - .session - .active() - .map(|st| missing_font_warnings(photocraft_engine::type_extra_cmds::missing_fonts(&st.doc))) - .unwrap_or_default() - .into_iter() - .filter(|w| reported_fonts.insert(w.clone())) - .collect(); - warn_all(err, &remaining); - if let Some(o) = a.get("--out") { - let r = h.save(None, Some(Path::new(o)), a.get("--format"), &opts).map_err(|e| e.to_string())?; - let ws: Vec = serde_json::from_value(r["warnings"].clone()).unwrap_or_default(); - let ws: Vec = ws.into_iter().filter(|w| reported_fonts.insert(w.clone())).collect(); - warn_all(err, &ws); - } - Ok(()) -} - -/// Parse an actions file: the steps of a recorded action or a droplet (`[id, params]` pairs, -/// `{"command": id, "params": {…}}` objects or bare ids; `"id"` is accepted for `"command"`), as a -/// list or wrapped in `{"actions": […]}`, `{"steps": […]}` or a droplet (#489). -pub fn parse_actions(text: &str) -> Result, String> { - let v: Value = serde_json::from_str(text).map_err(|e| format!("actions JSON: {e}"))?; - steps_of(&v) -} - -fn steps_of(v: &Value) -> Result { - photocraft_engine::automate_cmds::parse_action(v.get("actions").unwrap_or(v), "batch --actions").map_err(|e| e.to_string()) -} - -/// Engine commands to run in order: `(id, params)`. -pub type Steps = Vec<(String, Value)>; - -/// The steps `batch` plays on each file, and the actions they can call. An Actions set (the -/// Actions panel's `actions.json`: `{"actions": [{"name", "steps"}…]}`, or the bare list) plays -/// the action `name` picks (optional when the set holds one) with the whole set loaded, as -/// Photoshop's Batch does; anything else is one action's steps ([`parse_actions`]) (#2786). -pub fn load_actions(text: &str, name: Option<&str>) -> Result<(Steps, Vec), String> { - let v: Value = serde_json::from_str(text).map_err(|e| format!("actions JSON: {e}"))?; - let list = v.get("actions").unwrap_or(&v); - let is_set = list.as_array().is_some_and(|a| !a.is_empty() && a.iter().all(|x| x.get("name").is_some_and(Value::is_string))); - if !is_set { - if name.is_some() { - return Err("--action picks an action from an Actions set ({\"actions\": [{\"name\", \"steps\"}…]}), but this file holds one action's steps".into()); - } - return Ok((steps_of(&v)?, Vec::new())); - } - let set: Vec = serde_json::from_value(list.clone()).map_err(|e| format!("Actions set: {e}"))?; - let names = || set.iter().map(|a| format!("`{}`", a.name)).collect::>().join(", "); - let action = match (name, set.as_slice()) { - (Some(n), _) => set.iter().find(|a| a.name == n).ok_or_else(|| format!("no action named `{n}` in the set (it holds {})", names()))?, - (None, [only]) => only, - (None, _) => return Err(format!("the Actions set holds {} actions: pick one with --action ({})", set.len(), names())), - }; - let play = vec![("actions.play".to_string(), json!({"action": action.name}))]; - Ok((play, set)) -} - -/// [`load_actions`] for a file on disk: a Photoshop Actions file (`.atn`, by extension or by -/// its version word) is converted first; the played action's steps with no PhotoCraft -/// equivalent come back as warnings (they fail at playback, as in Photoshop when a step can't run). -pub fn load_actions_file(path: &str, name: Option<&str>) -> Result<(Steps, Vec, Vec), String> { - let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?; - let is_atn = Path::new(path).extension().is_some_and(|e| e.eq_ignore_ascii_case("atn")) || bytes.starts_with(&[0, 0, 0, 16]); - if !is_atn { - let text = String::from_utf8(bytes).map_err(|e| format!("{path}: {e}"))?; - let (steps, set) = load_actions(&text, name)?; - return Ok((steps, set, Vec::new())); - } - let imp = photocraft_engine::atn_cmds::import(&bytes).map_err(|e| format!("{path}: {e}"))?; - let names = || imp.actions.iter().map(|a| format!("`{}`", a.name)).collect::>().join(", "); - let action = match (name, imp.actions.as_slice()) { - (Some(n), _) => { - imp.actions.iter().find(|a| a.name == n).ok_or_else(|| format!("no action named `{n}` in the set `{}` (it holds {})", imp.set, names()))? - } - (None, [only]) => only, - (None, []) => return Err(format!("{path}: the set holds no actions")), - (None, _) => return Err(format!("the set `{}` holds {} actions: pick one with --action ({})", imp.set, imp.actions.len(), names())), - }; - let warnings = imp - .unsupported - .iter() - .filter(|u| u.action == action.name) - .map(|u| format!("`{}` step {}: Photoshop's \"{}\" ({}) has no PhotoCraft equivalent yet", u.action, u.step + 1, u.name, u.event)) - .chain(imp.warnings.iter().cloned()) - .collect(); - let play = vec![("actions.play".to_string(), json!({"action": action.name}))]; - Ok((play, imp.actions, warnings)) -} - -/// Whether `a` and `b` name the same existing folder, however they are spelt (relative, with a -/// trailing separator, through a symbolic link). -fn same_dir(a: &Path, b: &Path) -> bool { - matches!((std::fs::canonicalize(a), std::fs::canonicalize(b)), (Ok(a), Ok(b)) if a == b) -} - -fn is_input(p: &Path) -> bool { - let known = |e: &str| { - matches!(e, "pcraft" | "psd" | "psb" | "pdn" | "ora" | "kra" | "krz" | "af" | "afdesign" | "afphoto" | "afpub") - || photocraft_codecs::from_extension(e).is_some_and(|f| photocraft_codecs::caps(f).read) - }; - p.is_file() && p.extension().is_some_and(|e| known(&e.to_string_lossy().to_ascii_lowercase())) -} - -fn batch(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { - let actions_path = a.get("--actions").ok_or("batch needs --actions ")?; - let in_dir = PathBuf::from(a.get("--in").ok_or("batch needs --in ")?); - let out_dir = PathBuf::from(a.get("--out").ok_or("batch needs --out ")?); - let (actions, set, warnings) = load_actions_file(actions_path, a.get("--action"))?; - for w in &warnings { - let _ = writeln!(err, "warning: {w}"); - } - let opts = export_opts(a)?; - // Results are saved as `/.`, so an `--out` that is the `--in` folder would - // replace the originals (#492). - if !a.has("--in-place") && same_dir(&in_dir, &out_dir) { - return Err(format!( - "--out {} is the --in folder, so the results would replace the originals: choose another --out folder, or pass --in-place to overwrite them", - out_dir.display() - )); - } - std::fs::create_dir_all(&out_dir).map_err(|e| e.to_string())?; - let mut inputs: Vec = - std::fs::read_dir(&in_dir).map_err(|e| format!("{}: {e}", in_dir.display()))?.flatten().map(|e| e.path()).filter(|p| is_input(p)).collect(); - inputs.sort(); - let (mut ok, mut failed) = (0, 0); - let mut written = photocraft_engine::file_cmds::OutputClaims::default(); - // `--format .jpg` names outputs `.jpg`, as `--format jpg` does (#490). - let format = a.get("--format").map(|f| f.strip_prefix('.').unwrap_or(f)); - for input in &inputs { - let ext = format.map(str::to_owned).or_else(|| input.extension().map(|e| e.to_string_lossy().into_owned())).unwrap_or_else(|| "png".into()); - let stem = input.file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); - let target = out_dir.join(format!("{stem}.{ext}")); - let target_text = target.to_string_lossy(); - let r = (|| -> Result, String> { - written.check(&target_text)?; - let mut h = Headless::trusted_local(); - // Steps that play another action find it in the set; the session's playback stack - // stops an action that calls itself. - h.session.actions.list = set.clone(); - h.open(input).map_err(|e| e.to_string())?; - for (id, p) in &actions { - if photocraft_engine::actions_cmds::shell_view_command(id) || photocraft_engine::actions_cmds::shell_save_command(id) { - continue; - } - let r = h.command_run(id, p.clone()).map_err(|e| format!("`{id}`: {e}"))?; - // A failed step inside a called action makes this file an error, not a saved result. - if let Some(e) = photocraft_engine::actions_cmds::nested_failure(id, &r) { - return Err(format!("`{id}`: {e}")); - } - } - let r = h.save(None, Some(&target), Some(&ext), &opts).map_err(|e| e.to_string())?; - Ok(serde_json::from_value(r["warnings"].clone()).unwrap_or_default()) - })(); - match r { - Ok(ws) => { - ok += 1; - written.record(&target_text, &input.to_string_lossy()); - let _ = writeln!(out, "ok {} -> {}", input.display(), target.display()); - warn_all(err, &ws); - } - Err(e) => { - failed += 1; - let _ = writeln!(err, "FAIL {}: {e}", input.display()); - } - } - } - let _ = writeln!(out, "{ok} succeeded, {failed} failed"); - if failed > 0 { Err(format!("{failed} file(s) failed")) } else { Ok(()) } -} - -fn droplet(a: &Args, out: &mut dyn Write, err: &mut dyn Write) -> R { - let (file, inputs) = a.positional.split_first().ok_or("droplet needs and inputs")?; - if inputs.is_empty() { - return Err("droplet needs at least one input file or folder".into()); - } - let mut p = json!({"droplet": file, "input": inputs}); - if let Some(o) = a.get("--out") { - p["output"] = json!(o); - } - let mut s = photocraft_engine::Session::new(); - let r = s.execute("file.automate.runDroplet", p).map_err(|e| e.to_string())?; - for f in r["files"].as_array().into_iter().flatten() { - let _ = writeln!(out, "ok {}", f.as_str().unwrap_or_default()); - } - let errors = r["errors"].as_array().cloned().unwrap_or_default(); - for e in &errors { - let _ = writeln!(err, "FAIL {}: {}", e["file"].as_str().unwrap_or_default(), e["error"].as_str().unwrap_or_default()); - } - if errors.is_empty() { Ok(()) } else { Err(format!("{} file(s) failed", errors.len())) } -} - -fn commands(a: &Args, out: &mut dyn Write) -> R { - let h = Headless::new(); - let list = h.command_list(); - let needle = a.get("--filter").map(str::to_lowercase); - let items: Vec<&Value> = list - .as_array() - .map(|v| v.iter().collect::>()) - .unwrap_or_default() - .into_iter() - .filter(|c| needle.as_ref().is_none_or(|n| photocraft_engine::commands::list_entry_matches(c, n))) - .collect(); - if a.has("--json") { - return print_json(out, &Value::Array(items.into_iter().cloned().collect()), false); - } - for c in items { - let menu = c["menu"].as_array().map(|m| m.iter().filter_map(Value::as_str).collect::>().join(" > ")).unwrap_or_default(); - writeln!(out, "{:<44} {:<32} {}", c["id"].as_str().unwrap_or(""), c["label"].as_str().unwrap_or(""), menu).map_err(|e| e.to_string())?; - } - Ok(()) -} - -fn serve(a: &Args, err: &mut dyn Write) -> R { - use std::sync::{Arc, Mutex}; - let h = Arc::new(Mutex::new(Headless::with_workspace(automation_workspace(a)?))); - match a.get("--port") { - Some(port) => { - let port: u16 = port.parse().map_err(|_| format!("bad --port `{port}`"))?; - let addr = format!("127.0.0.1:{port}"); - let (supplied, token_file) = security::token_inputs(a.get("--control-token").map(str::to_owned), a.get("--control-token-file").map(PathBuf::from)); - let token = security::server_token(supplied.as_deref(), token_file.as_deref()).map_err(|e| e.to_string())?; - if let Some(path) = &token_file { - let _ = writeln!(err, "photocraft-cli control token file: {}", path.display()); - } else if supplied.is_none() { - let _ = writeln!(err, "photocraft-cli control token: {token}"); - } - photocraft_automation::rpc::serve_tcp(&addr, h, token, |local| { - let _ = writeln!(err, "photocraft-cli serving on {local}"); - }) - .map_err(|e| e.to_string()) - } - None => { - let stdin = std::io::stdin(); - photocraft_automation::rpc::serve_lines(&h, stdin.lock(), std::io::stdout()).map_err(|e| e.to_string()) - } - } -} - -fn mcp(a: &Args) -> R { - let server = match a.get("--bridge") { - Some(addr) => { - let (supplied, token_file) = security::token_inputs(a.get("--control-token").map(str::to_owned), a.get("--control-token-file").map(PathBuf::from)); - let token = security::client_token(supplied.as_deref(), token_file.as_deref()).map_err(|e| e.to_string())?; - PhotocraftMcp::bridge(addr, &token).map_err(|e| e.to_string())? - } - None => PhotocraftMcp::headless_with_workspace(automation_workspace(a)?), - }; - let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build().map_err(|e| e.to_string())?; - rt.block_on(server.serve_stdio()).map_err(|e| e.to_string()) -} - #[cfg(test)] -mod missing_font_warning_tests { - use super::*; - - fn invoke(args: &[&str]) -> (i32, String, String) { - let mut output = Vec::new(); - let mut errors = Vec::new(); - let args: Vec = args.iter().map(|s| s.to_string()).collect(); - let code = run(&args, &mut output, &mut errors); - (code, String::from_utf8(output).unwrap(), String::from_utf8(errors).unwrap()) +pub(crate) mod test_support { + /// What [`crate::run`] returned and wrote. + pub(crate) struct Invoke { + pub(crate) code: i32, + pub(crate) stdout: String, + pub(crate) stderr: String, } - #[test] - fn missing_typeface_is_reported_by_run_info_and_convert() { - let family = "DefinitelyMissingPhotoCraftTypeface1251"; - let folder = std::env::temp_dir().join(format!("photocraft-cli-font-warnings-{}", std::process::id())); - std::fs::create_dir_all(&folder).unwrap(); - let native = folder.join("missing.pcraft"); - let png = folder.join("missing.png"); - let native_str = native.to_string_lossy().to_string(); - let png_str = png.to_string_lossy().to_string(); - let params = json!({"x": 6, "y": 38, "text": "Missing font warning", "font": family, "size": 20}).to_string(); - - let (code, stdout, stderr) = invoke(&[ - "run", - "--new", - r#"{"width":256,"height":96,"background":"white"}"#, - "--cmd", - "type.create", - "--params", - ¶ms, - "--cmd", - "type.resolveMissingFonts", - "--out", - &native_str, - ]); - assert_eq!(code, 0, "{stderr}"); - assert_eq!(stderr.matches(family).count(), 1, "multi-command runs should not repeat the same missing font warning"); - let commands: Vec = stdout.lines().map(|s| serde_json::from_str(s).unwrap()).collect(); - assert_eq!(commands.len(), 2, "{stdout}"); - assert!(commands[0]["warnings"].as_array().unwrap().iter().any(|w| w.as_str().is_some_and(|s| s.contains(family)))); - assert!(commands[1].get("warnings").is_none(), "only newly detected missing fonts are reported"); - - let (code, stdout, stderr) = invoke(&["info", &native_str, "--compact"]); - assert_eq!(code, 0, "{stderr}"); - let info: Value = serde_json::from_str(&stdout).unwrap(); - assert!(info["warnings"].as_array().unwrap().iter().any(|w| w.as_str().is_some_and(|s| s.contains(family)))); - assert!(stderr.is_empty(), "info puts warnings in JSON, not stderr"); - - let (code, _, stderr) = invoke(&["convert", &native_str, &png_str]); - assert_eq!(code, 0, "{stderr}"); - assert!(stderr.contains(&format!("warning: font '{family}'")), "{stderr}"); - assert!(png.is_file()); - - // Even with no commands, run --out checks the opened document before export. - let (code, stdout, stderr) = invoke(&["run", &native_str, "--out", &png_str]); - assert_eq!(code, 0, "{stderr}"); - assert!(stdout.is_empty()); - assert_eq!(stderr.matches(family).count(), 1); - - let _ = std::fs::remove_dir_all(&folder); - } - - #[test] - fn convert_reads_and_writes_openraster() { - let folder = std::env::temp_dir().join(format!("photocraft-cli-ora-{}", std::process::id())); - std::fs::create_dir_all(&folder).unwrap(); - let path = |name: &str| folder.join(name).to_string_lossy().to_string(); - let (native, ora, png) = (path("layers.pcraft"), path("layers.ora"), path("back.png")); - let (code, _, stderr) = invoke(&["run", "--new", r#"{"width":16,"height":12}"#, "--cmd", "layer.new.layer", "--out", &native]); - assert_eq!(code, 0, "{stderr}"); - let (code, _, stderr) = invoke(&["convert", &native, &ora]); - assert_eq!(code, 0, "{stderr}"); - let (code, stdout, stderr) = invoke(&["info", &ora, "--compact"]); - assert_eq!(code, 0, "{stderr}"); - let info: Value = serde_json::from_str(&stdout).unwrap(); - assert_eq!(info["layers"].as_array().map(Vec::len), Some(2), "{info}"); - let (code, _, stderr) = invoke(&["convert", &ora, &png]); - assert_eq!(code, 0, "{stderr}"); - assert!(is_input(std::path::Path::new(&ora))); - // Krita documents are batch inputs too (import-only). - for name in ["sketch.kra", "old.KRZ"] { - std::fs::write(folder.join(name), b"").unwrap(); - assert!(is_input(&folder.join(name)), "{name}"); - } - let _ = std::fs::remove_dir_all(&folder); - } - - #[test] - fn no_type_layers_do_not_produce_missing_font_warnings() { - let folder = std::env::temp_dir().join(format!("photocraft-cli-no-font-warning-{}", std::process::id())); - std::fs::create_dir_all(&folder).unwrap(); - let native = folder.join("blank.pcraft"); - let png = folder.join("blank.png"); - let native_str = native.to_string_lossy().to_string(); - let png_str = png.to_string_lossy().to_string(); - let (code, _, stderr) = invoke(&["run", "--new", r#"{"width":16,"height":16}"#, "--out", &native_str]); - assert_eq!(code, 0, "{stderr}"); - assert!(!stderr.contains("font '"), "{stderr}"); - let (code, stdout, stderr) = invoke(&["info", &native_str, "--compact"]); - assert_eq!(code, 0, "{stderr}"); - let info: Value = serde_json::from_str(&stdout).unwrap(); - assert!(info["warnings"].as_array().unwrap().iter().all(|w| !w.as_str().unwrap_or_default().contains("font '"))); - let (code, _, stderr) = invoke(&["convert", &native_str, &png_str]); - assert_eq!(code, 0, "{stderr}"); - assert!(!stderr.contains("font '"), "{stderr}"); - let _ = std::fs::remove_dir_all(&folder); - } -} - -#[cfg(test)] -mod batch_action_set_tests { - use super::*; - - fn invoke(args: &[&str]) -> (i32, String, String) { - let (mut output, mut errors) = (Vec::new(), Vec::new()); - let args: Vec = args.iter().map(|s| s.to_string()).collect(); - let code = run(&args, &mut output, &mut errors); - (code, String::from_utf8(output).unwrap(), String::from_utf8(errors).unwrap()) - } - - /// A batched action that plays another action of its set finds it in every file's session; a - /// failure inside the called action fails the file, and a self-call stops instead of looping. - #[test] - fn batch_plays_an_action_that_calls_another_action_of_its_set() { - let dir = std::env::temp_dir().join(format!("photocraft-cli-action-set-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - let path = |name: &str| dir.join(name).to_string_lossy().to_string(); - std::fs::create_dir_all(dir.join("in")).unwrap(); - for name in ["a", "b"] { - let (code, _, stderr) = invoke(&["run", "--new", r#"{"width":8,"height":6}"#, "--out", &path(&format!("in/{name}.png"))]); - assert_eq!(code, 0, "{stderr}"); - } - let set = json!({"version": 1, "actions": [ - {"name": "Outer", "steps": [["actions.play", {"action": "Rotate"}]]}, - {"name": "Rotate", "steps": [["image.imageRotation.90cw", {}]]}, - {"name": "Calls bad", "steps": [["actions.play", {"action": "Bad"}]]}, - {"name": "Bad", "steps": [["layer.delete", {"layer": 999_999}]]}, - {"name": "Loop", "steps": [["actions.play", {"action": "Loop"}]]}, - ]}); - std::fs::write(dir.join("actions.json"), set.to_string()).unwrap(); - let batch = |action: &str| invoke(&["batch", "--actions", &path("actions.json"), "--action", action, "--in", &path("in"), "--out", &path(action)]); - - let (code, stdout, stderr) = batch("Outer"); - assert_eq!(code, 0, "{stdout}{stderr}"); - for name in ["a", "b"] { - let (code, info, stderr) = invoke(&["info", &path(&format!("Outer/{name}.png")), "--compact"]); - assert_eq!(code, 0, "{stderr}"); - let info: Value = serde_json::from_str(&info).unwrap(); - assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(6), Some(8)), "rotated by the called action"); - } - for action in ["Calls bad", "Loop"] { - let (code, stdout, stderr) = batch(action); - assert_ne!(code, 0, "{stdout}"); - assert_eq!(stderr.matches("FAIL").count(), 2, "{stderr}"); - assert!(!dir.join(action).join("a.png").exists(), "a file whose called action failed is not saved"); - } - let _ = std::fs::remove_dir_all(&dir); + pub(crate) fn invoke(args: &[&str]) -> Invoke { + let (mut stdout, mut stderr) = (Vec::new(), Vec::new()); + let code = crate::run(args, &mut stdout, &mut stderr); + Invoke { code, stdout: String::from_utf8(stdout).unwrap(), stderr: String::from_utf8(stderr).unwrap() } } } diff --git a/apps/photocraft-cli/src/main.rs b/apps/photocraft-cli/src/main.rs index c0bce7826b..ea5598f99d 100644 --- a/apps/photocraft-cli/src/main.rs +++ b/apps/photocraft-cli/src/main.rs @@ -1,9 +1,8 @@ #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] fn main() { - // `args_os`, not `args`: a non-Unicode argument (a Latin-1 file name on Linux) must be a - // usage error, not a panic before anything is printed (issue #1108). - let args: Vec = std::env::args_os().skip(1).collect(); - let code = photocraft_cli::run_os(&args, &mut std::io::stdout(), &mut std::io::stderr()); + // `args_os`, not `args`: a non-Unicode argument (a Latin-1 file name on Linux) must reach clap, + // not panic before anything is printed (issue #1108). + let code = photocraft_cli::run(std::env::args_os().skip(1), &mut std::io::stdout(), &mut std::io::stderr()); std::process::exit(code); } diff --git a/apps/photocraft-cli/src/output.rs b/apps/photocraft-cli/src/output.rs new file mode 100644 index 0000000000..34101c9ec0 --- /dev/null +++ b/apps/photocraft-cli/src/output.rs @@ -0,0 +1,51 @@ +use photocraft_io::ExportOptions; +use serde_json::Value; +use std::io::Write; + +use crate::{Result, args::ExportArgs}; + +/// The `--format` family's options. A bad value never gets here: clap refused it. +impl From<&ExportArgs> for ExportOptions { + fn from(a: &ExportArgs) -> Self { + // TIFF output is flat unless asked: scripted conversions keep predictable sizes. + let mut o = ExportOptions { tiff_layers: a.tiff_layers, tga_bits: a.tga_bits, ..Default::default() }; + if let Some(q) = a.quality { + o.encode.jpeg_quality = q; + // Asking for a quality asks for a lossy WebP; the default WebP stays lossless. + o.encode.webp_quality = q; + o.encode.webp_lossless = false; + } + o.encode.dpx_bits = a.dpx_bits; + if let Some(c) = a.tiff_compression { + o.encode.tiff_compression = c; + } + if let Some(f) = a.jpeg_format { + o.encode.jpeg_format = f; + } + if let Some(c) = a.dds_compression { + o.encode.dds.compression = c; + } + o.encode.dds.mipmaps = a.dds_mipmaps; + o + } +} + +pub(crate) fn warn_all(err: &mut dyn Write, ws: &[String]) { + for w in ws { + let _ = writeln!(err, "warning: {w}"); + } +} + +/// How [`print_json`] lays the JSON out. +pub(crate) enum JsonStyle { + Pretty, + Compact, +} + +pub(crate) fn print_json(out: &mut dyn Write, v: &Value, style: JsonStyle) -> Result { + let s = match style { + JsonStyle::Pretty => serde_json::to_string_pretty(v), + JsonStyle::Compact => serde_json::to_string(v), + }?; + Ok(writeln!(out, "{s}")?) +} diff --git a/apps/photocraft-cli/tests/cli.rs b/apps/photocraft-cli/tests/cli.rs deleted file mode 100644 index 3c28a8196e..0000000000 --- a/apps/photocraft-cli/tests/cli.rs +++ /dev/null @@ -1,755 +0,0 @@ -//! Process-level tests of the `photocraft-cli` binary. - -use std::io::{BufRead, BufReader, Write}; -use std::path::{Path, PathBuf}; -use std::process::{Command, Stdio}; - -use serde_json::{Value, json}; - -fn bin() -> Command { - Command::new(env!("CARGO_BIN_EXE_photocraft-cli")) -} - -fn tmp(name: &str) -> PathBuf { - let d = std::env::temp_dir().join(format!("pc-cli-{}-{name}", std::process::id())); - let _ = std::fs::remove_dir_all(&d); - std::fs::create_dir_all(&d).unwrap(); - d -} - -fn write_png(path: &Path, w: u32, h: u32, seed: u8) { - let data: Vec = (0..w * h * 4).map(|i| (i as u8).wrapping_mul(seed).wrapping_add(seed)).collect(); - let img = photocraft_codecs::Image::from_u8(w, h, photocraft_codecs::ChannelLayout::Rgba, data).unwrap(); - std::fs::write(path, photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); -} - -fn ok(cmd: &mut Command) -> (String, String) { - let o = cmd.output().unwrap(); - let (out, err) = (String::from_utf8_lossy(&o.stdout).into_owned(), String::from_utf8_lossy(&o.stderr).into_owned()); - assert!(o.status.success(), "exit {:?}\nstdout: {out}\nstderr: {err}", o.status); - (out, err) -} - -#[test] -fn usage_and_unknown_command() { - let o = bin().output().unwrap(); - assert_eq!(o.status.code(), Some(2)); - assert!(String::from_utf8_lossy(&o.stderr).contains("USAGE")); - let o = bin().arg("frobnicate").output().unwrap(); - assert_eq!(o.status.code(), Some(2)); - let (out, _) = ok(bin().arg("--help")); - assert!(out.contains("convert")); - let (out, _) = ok(bin().arg("--version")); - assert!(out.starts_with("photocraft-cli ")); -} - -/// #423: ` --help` and `-h` print usage and return at once (no inputs read, no files -/// written, no server started), whatever else is on the line. -#[test] -fn every_subcommand_answers_help() { - let d = tmp("help"); - for sub in ["convert", "info", "run", "batch", "droplet", "commands", "mcp", "serve"] { - for help in ["--help", "-h"] { - let o = bin().current_dir(&d).arg(sub).arg(help).arg("--out").arg("x.png").stdin(Stdio::null()).output().unwrap(); - let out = String::from_utf8_lossy(&o.stdout); - assert_eq!(o.status.code(), Some(0), "{sub} {help}: {}", String::from_utf8_lossy(&o.stderr)); - assert!(out.contains("USAGE") && out.contains("photocraft-cli batch"), "{sub} {help}: {out}"); - } - } - assert_eq!(std::fs::read_dir(&d).unwrap().count(), 0, "help wrote nothing"); -} - -/// #423: a flag the subcommand doesn't take is a usage error naming it, before any work. -#[test] -fn unknown_flags_are_usage_errors() { - let d = tmp("flags"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - let actions = d.join("actions.json"); - std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); - let out_dir = d.join("out"); - let fails = |cmd: &mut Command, flag: &str| { - let o = cmd.output().unwrap(); - let err = String::from_utf8_lossy(&o.stderr); - assert_eq!(o.status.code(), Some(2), "{err}"); - assert!(err.contains(&format!("unknown flag {flag}")), "{err}"); - }; - for typo in [&["--fromat", "jpg"][..], &["--fromat=jpg"][..]] { - fails(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir).args(typo), "--fromat"); - assert!(!out_dir.exists(), "nothing written"); - } - let x = d.join("x.png"); - fails(bin().arg("run").arg(input.join("a.png")).args(["--cmd", "image.adjustments.invert", "--param", "{}", "--out"]).arg(&x), "--param"); - assert!(!x.exists()); - // A flag one subcommand takes is still unknown to another; a bare flag takes no value. - fails(bin().args(["info", "--json"]).arg(input.join("a.png")), "--json"); - let o = bin().args(["commands", "--json=yes"]).output().unwrap(); - assert_eq!(o.status.code(), Some(2)); - assert!(String::from_utf8_lossy(&o.stderr).contains("--json takes no value")); - // Every flag still works in both forms. - let (out, _) = ok(bin().args(["commands", "--json", "--filter=gaussian"])); - assert!(out.contains("filter.blur.gaussianBlur"), "{out}"); - // The Elliptical Marquee is a `select.rect` option, found by its keywords (#5060). - for needle in ["ellipse", "marquee"] { - let (out, _) = ok(bin().args(["commands", "--filter", needle])); - assert!(out.contains("select.rect"), "{needle}: {out}"); - } - ok(bin().args(["batch", "--actions"]).arg(&actions).arg(format!("--in={}", input.display())).arg("--out").arg(&out_dir).args([ - "--format=jpg", - "--quality", - "80", - ])); - assert!(out_dir.join("a.jpg").exists()); - let (out, _) = ok(bin().arg("info").arg(input.join("a.png")).arg("--compact")); - assert!(!out.trim().contains('\n'), "compact JSON is one line"); -} - -#[test] -fn convert_png_pcraft_png_is_lossless() { - let d = tmp("convert"); - let (a, b, c) = (d.join("a.png"), d.join("b.pcraft"), d.join("c.png")); - write_png(&a, 37, 23, 7); - ok(bin().args(["convert"]).arg(&a).arg(&b)); - assert!(photocraft_format::is_pcraft(&std::fs::read(&b).unwrap())); - ok(bin().args(["convert"]).arg(&b).arg(&c)); - let x = photocraft_codecs::decode(&std::fs::read(&a).unwrap()).unwrap(); - let y = photocraft_codecs::decode(&std::fs::read(&c).unwrap()).unwrap(); - assert_eq!(x.to_rgba8(), y.to_rgba8()); - // Format override and lossy warning path. - ok(bin().args(["convert"]).arg(&a).arg(d.join("out.bin")).args(["--format", "jpg", "--quality", "60"])); - assert!(photocraft_codecs::detect(&std::fs::read(d.join("out.bin")).unwrap()) == Some(photocraft_codecs::Format::Jpeg)); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn convert_errors() { - let d = tmp("converr"); - let o = bin().args(["convert", "/missing/in.png"]).arg(d.join("x.png")).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - let o = bin().args(["convert", "only-one"]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn info_prints_layer_tree_json() { - let d = tmp("info"); - let a = d.join("a.png"); - write_png(&a, 10, 6, 3); - let (out, _) = ok(bin().arg("info").arg(&a)); - let v: Value = serde_json::from_str(&out).unwrap(); - assert_eq!(v["width"], 10); - assert_eq!(v["height"], 6); - // An RGBA PNG opens as a normal "Layer 0", not the locked Background (as in Photoshop). - assert_eq!(v["layers"][0]["name"], "Layer 0"); - assert!(v.get("history").is_none()); - let (out, _) = ok(bin().arg("info").arg(&a).arg("--compact")); - assert_eq!(out.trim().lines().count(), 1); - std::fs::remove_dir_all(d).unwrap(); -} - -/// #518, #523: a truncated JPEG and an animated GIF open, but `info` lists why the image is -/// incomplete and `convert` prints it on stderr (and still succeeds); complete files stay quiet. -#[test] -fn info_and_convert_report_decode_warnings() { - let d = tmp("decode-warnings"); - let noise: Vec = (0..64 * 48 * 3).map(|i: u32| (i.wrapping_mul(7919) % 251) as u8).collect(); - let img = photocraft_codecs::Image::from_u8(64, 48, photocraft_codecs::ChannelLayout::Rgb, noise).unwrap(); - let jpeg = photocraft_codecs::encode(&img, photocraft_codecs::Format::Jpeg, &Default::default()).unwrap(); - // Three 1x1 frames. - let mut gif = b"GIF89a\x01\0\x01\0\x80\0\0\0\0\0\xFF\xFF\xFF".to_vec(); - for _ in 0..3 { - gif.extend_from_slice(b"\x2C\0\0\0\0\x01\0\x01\0\0\x02\x02\x44\x01\0"); - } - gif.push(0x3B); - let cases = [ - ("whole.jpg", &jpeg[..], None), - ("half.jpg", &jpeg[..jpeg.len() / 2], Some("JPEG data ends early (the file is truncated or damaged); part of the image is missing")), - ("anim.gif", &gif[..], Some("only the first of 3 frames was imported")), - ]; - for (name, bytes, warning) in cases { - let file = d.join(name); - std::fs::write(&file, bytes).unwrap(); - let (out, _) = ok(bin().arg("info").arg(&file).arg("--compact")); - let v: Value = serde_json::from_str(&out).unwrap(); - assert_eq!(v["warnings"], json!(warning.into_iter().collect::>()), "{name}"); - let (_, err) = ok(bin().arg("convert").arg(&file).arg(d.join(format!("{name}.png")))); - assert_eq!(err.trim(), warning.map(|w| format!("warning: {w}")).unwrap_or_default(), "{name}"); - } - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn run_commands_and_save() { - let d = tmp("run"); - let out_pc = d.join("r.pcraft"); - let (out, _) = ok(bin() - .args(["run", "--new", r#"{"width":40,"height":30,"name":"Run"}"#]) - .args(["--cmd", "layer.new.layer", "--params", r#"{"name":"Ink"}"#]) - .args(["--cmd", "paint.stroke", "--params", r##"{"points":[[2,2,1],[30,20,1]],"size":5,"color":"#00ff00"}"##]) - .args(["--cmd", "document.inspect"]) - .arg("--out") - .arg(&out_pc)); - let lines: Vec = out.lines().map(|l| serde_json::from_str(l).unwrap()).collect(); - assert_eq!(lines.len(), 3); - assert_eq!(lines[2]["command"], "document.inspect"); - assert_eq!(lines[2]["result"]["name"], "Run"); - let (info, _) = ok(bin().arg("info").arg(&out_pc)); - let v: Value = serde_json::from_str(&info).unwrap(); - // Saving a native document gives the reopened bundle its output filename. - assert_eq!(v["name"], "r.pcraft"); - let names: Vec<&str> = v["layers"].as_array().unwrap().iter().filter_map(|l| l["name"].as_str()).collect(); - assert_eq!(names, ["Ink", "Background"]); - // Continue from the saved file and export PNG. - let png = d.join("r.png"); - ok(bin().arg("run").arg(&out_pc).args(["--cmd", "layer.new.layer"]).arg("--out").arg(&png)); - assert_eq!(photocraft_codecs::decode(&std::fs::read(&png).unwrap()).unwrap().dimensions(), (40, 30)); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn run_stops_after_failed_action_without_exporting() { - let d = tmp("run-failed-action"); - let out = d.join("result.png"); - let result = bin() - .args(["run", "--new", r#"{"width":8,"height":8}"#]) - .args(["--cmd", "layer.new.layer"]) - .args(["--cmd", "actions.record", "--params", r#"{"name":"Delete"}"#]) - .args(["--cmd", "layer.delete", "--cmd", "actions.stop"]) - // The recorded deletion succeeded with two layers; replay cannot delete the last one. - .args(["--cmd", "actions.play", "--params", r#"{"action":"Delete"}"#]) - .args(["--cmd", "document.inspect", "--out"]) - .arg(&out) - .output() - .unwrap(); - let error = String::from_utf8_lossy(&result.stderr); - assert_eq!(result.status.code(), Some(1), "stdout: {}\nstderr: {error}", String::from_utf8_lossy(&result.stdout)); - assert!(error.contains("actions.play") && error.contains("Delete") && error.contains("step 1"), "{error}"); - let rows: Vec = String::from_utf8_lossy(&result.stdout).lines().map(|line| serde_json::from_str(line).unwrap()).collect(); - assert!(rows.iter().all(|row| row["command"] != "document.inspect"), "later commands must not run"); - assert!(!out.exists(), "a failed action must not export a partial result"); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn run_continues_after_successful_action_and_exports() { - let d = tmp("run-successful-action"); - let out = d.join("result.png"); - let (stdout, _) = ok(bin() - .args(["run", "--new", r#"{"width":8,"height":8}"#]) - .args(["--cmd", "actions.record", "--params", r#"{"name":"Add"}"#]) - .args(["--cmd", "layer.new.layer", "--cmd", "actions.stop"]) - .args(["--cmd", "actions.play", "--params", r#"{"action":"Add"}"#]) - .args(["--cmd", "document.inspect", "--out"]) - .arg(&out)); - let rows: Vec = stdout.lines().map(|line| serde_json::from_str(line).unwrap()).collect(); - let playback = rows.iter().find(|row| row["command"] == "actions.play").unwrap(); - assert_eq!(playback["result"]["ran"], 1); - assert!(playback["result"].get("failed").is_none()); - assert!(rows.iter().any(|row| row["command"] == "document.inspect")); - assert!(out.is_file()); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn run_errors() { - let o = bin().args(["run", "--new", "{}", "--cmd", "no.such"]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - assert!(String::from_utf8_lossy(&o.stderr).contains("unknown command")); - let o = bin().args(["run", "--new", "{}", "--params", "{}"]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - let o = bin().args(["run", "--new", "{", "--cmd", "x"]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - // Params must be a JSON object, for `--new` and for each `--params`. - let o = bin().args(["run", "--new", "[3]", "--cmd", "layer.new.layer"]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - let e = String::from_utf8_lossy(&o.stderr); - assert!(e.contains("--new") && e.contains("must be a JSON object"), "{e}"); - let o = bin().args(["run", "--new", "{}", "--cmd", "layer.new.layer", "--params", "\"x\""]).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - let e = String::from_utf8_lossy(&o.stderr); - assert!(e.contains("layer.new.layer") && e.contains("must be a JSON object"), "{e}"); -} - -#[test] -fn run_rejects_invalid_filter_parameters_without_exporting() { - let d = tmp("invalid-filter-params"); - let out = d.join("result.png"); - for (params, key) in [(r#"{"radius":"big"}"#, "radius"), (r#"{"radus":3}"#, "radus"), (r#"{"radius":5000}"#, "radius"), (r#"{"radius":-4}"#, "radius")] { - let result = bin() - .args(["run", "--new", r#"{"width":8,"height":8}"#, "--cmd", "filter.blur.gaussianBlur", "--params", params, "--out"]) - .arg(&out) - .output() - .unwrap(); - assert_eq!(result.status.code(), Some(1)); - let error = String::from_utf8_lossy(&result.stderr); - assert!(error.contains("filter.blur.gaussianBlur") && error.contains(key), "{error}"); - assert!(!out.exists()); - } - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn batch_applies_actions_to_directory() { - let d = tmp("batch"); - let (inp, outp) = (d.join("in"), d.join("out")); - std::fs::create_dir_all(&inp).unwrap(); - write_png(&inp.join("one.png"), 12, 8, 1); - write_png(&inp.join("two.png"), 9, 9, 2); - std::fs::write(inp.join("notes.txt"), "ignored").unwrap(); - let actions = d.join("actions.json"); - std::fs::write(&actions, json!([{"command": "layer.new.layer", "params": {"name": "Batch"}}]).to_string()).unwrap(); - let (out, _) = ok(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&inp).arg("--out").arg(&outp).args(["--format", "pcraft"])); - assert!(out.contains("2 succeeded, 0 failed"), "{out}"); - for n in ["one", "two"] { - let (info, _) = ok(bin().arg("info").arg(outp.join(format!("{n}.pcraft")))); - let v: Value = serde_json::from_str(&info).unwrap(); - assert_eq!(v["layers"][0]["name"], "Batch"); - } - // A failing action reports and exits 1. - std::fs::write(&actions, r#"{"actions":[{"id":"no.such.command"}]}"#).unwrap(); - let o = bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&inp).arg("--out").arg(&outp).output().unwrap(); - assert_eq!(o.status.code(), Some(1)); - assert!(String::from_utf8_lossy(&o.stderr).contains("FAIL")); - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn batch_skips_recorded_view_and_save_steps() { - let d = tmp("batch-shell-steps"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - let source = input.join("a.png"); - write_png(&source, 12, 8, 3); - let original = std::fs::read(&source).unwrap(); - let recorded_save = d.join("recorded-save.psd"); - let mut cases = Vec::new(); - for (name, id) in [("zoom-in", "view.zoomIn"), ("zoom-out", "view.zoomOut"), ("fit", "view.fitOnScreen"), ("actual-pixels", "view.actualPixels")] { - cases.push((name, json!([[id, {}], ["image.imageRotation.90cw", {}]]).to_string())); - } - cases.push(("save", json!([["file.save", {}], ["image.imageRotation.90cw", {}]]).to_string())); - cases.push(("save-as", json!([["file.saveAs", {"path": recorded_save.to_string_lossy()}], ["image.imageRotation.90cw", {}]]).to_string())); - - for (name, actions) in cases { - let o = batch_with(&d, &input, name, &actions, &[]); - let (stdout, stderr) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); - assert!(o.status.success(), "{name}: {stdout}\n{stderr}"); - assert!(stdout.contains("1 succeeded, 0 failed"), "{name}: {stdout}"); - let output = d.join(name).join("a.png"); - let (info, _) = ok(bin().arg("info").arg(&output)); - let info: Value = serde_json::from_str(&info).unwrap(); - assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(12)), "{name}: {info}"); - assert_eq!(std::fs::read(&source).unwrap(), original, "{name} changed its input"); - assert!(!recorded_save.exists(), "{name} wrote the recorded Save As path"); - } - std::fs::remove_dir_all(d).unwrap(); -} - -#[test] -fn parse_actions_formats() { - let a = photocraft_cli::parse_actions(r#"[{"command":"a","params":{"x":1}},{"id":"b"}]"#).unwrap(); - assert_eq!(a, vec![("a".to_string(), json!({"x":1})), ("b".to_string(), json!({}))]); - assert!(photocraft_cli::parse_actions(r#"{"actions":[{"command":"c"}]}"#).unwrap().len() == 1); - assert!(photocraft_cli::parse_actions(r#"[{"params":{}}]"#).is_err()); - assert!(photocraft_cli::parse_actions("7").is_err()); - // #489: the step shapes a recorded action and a droplet store, bare or wrapped. - let want = vec![("a".to_string(), json!({"x":1})), ("b".to_string(), json!({}))]; - for text in [ - r#"[["a",{"x":1}],["b"]]"#, - r#"[["a",{"x":1}],"b"]"#, - r#"{"steps":[["a",{"x":1}],["b",{}]]}"#, - r#"{"photocraftDroplet":1,"action":{"steps":[["a",{"x":1}],{"command":"b"}]}}"#, - r#"{"actions":[["a",{"x":1}],["b",{}]]}"#, - ] { - assert_eq!(photocraft_cli::parse_actions(text).unwrap(), want, "{text}"); - } - for text in ["[42]", "[[]]", "[[7,{}]]", r#"{"name":"x"}"#] { - assert!(photocraft_cli::parse_actions(text).is_err(), "{text}"); - } -} - -/// Runs `batch` on `input` with `actions` (saved as `.json`), writing to the folder `/`. -fn batch_with(d: &Path, input: &Path, name: &str, actions: &str, extra: &[&str]) -> std::process::Output { - let file = d.join(format!("{name}.json")); - std::fs::write(&file, actions).unwrap(); - bin().args(["batch", "--actions"]).arg(&file).arg("--in").arg(input).arg("--out").arg(d.join(name)).args(extra).output().unwrap() -} - -/// #489: `batch --actions` takes `[id, params]` steps and a droplet's steps, with the same results -/// as the object form; a malformed step fails before anything is written. -#[test] -fn batch_accepts_recorded_action_steps() { - let d = tmp("batch-steps"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - write_png(&input.join("b.png"), 6, 5, 9); - let shapes = [ - ("objects", r#"[{"command":"image.adjustments.invert"}]"#), - ("pairs", r#"[["image.adjustments.invert",{}]]"#), - ("ids", r#"["image.adjustments.invert"]"#), - ("droplet", r#"{"photocraftDroplet":1,"action":{"steps":[["image.adjustments.invert",{}]]}}"#), - ]; - for (name, actions) in shapes { - let o = batch_with(&d, &input, name, actions, &[]); - assert!(o.status.success(), "{name}: {}", String::from_utf8_lossy(&o.stderr)); - assert!(String::from_utf8_lossy(&o.stdout).contains("2 succeeded, 0 failed"), "{name}"); - for f in ["a.png", "b.png"] { - assert_eq!(std::fs::read(d.join(name).join(f)).unwrap(), std::fs::read(d.join("objects").join(f)).unwrap(), "{name}/{f}"); - } - } - let o = batch_with(&d, &input, "bad", "[42]", &[]); - assert_eq!(o.status.code(), Some(1)); - assert!(!d.join("bad").exists(), "nothing written"); -} - -/// #490: a leading dot on `--format` doesn't double the dot in output names. -#[test] -fn batch_format_with_leading_dot() { - let d = tmp("batch-dot"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - write_png(&input.join("b.png"), 6, 5, 9); - let invert = r#"[{"command":"image.adjustments.invert"}]"#; - for (name, format) in [("dot", ".jpg"), ("plain", "jpg")] { - let o = batch_with(&d, &input, name, invert, &["--format", format]); - assert!(o.status.success(), "{format}: {}", String::from_utf8_lossy(&o.stderr)); - let mut names: Vec = std::fs::read_dir(d.join(name)).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); - names.sort(); - assert_eq!(names, ["a.jpg", "b.jpg"], "{format}"); - assert_eq!(photocraft_codecs::detect(&std::fs::read(d.join(name).join("a.jpg")).unwrap()), Some(photocraft_codecs::Format::Jpeg)); - } -} - -/// #491: `--quality` outside 1-100 is an error for every subcommand, whatever the number's size, -/// and nothing is written. -#[test] -fn quality_outside_1_to_100_is_refused() { - let d = tmp("quality"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - let a = input.join("a.png"); - write_png(&a, 8, 4, 3); - let invert = r#"[{"command":"image.adjustments.invert"}]"#; - for q in ["0", "101", "255", "256", "1000", "-1", "abc", "50.5", ""] { - let out = d.join("q.jpg"); - let (mut convert, mut run) = (bin(), bin()); - convert.arg("convert").arg(&a).arg(&out).args(["--quality", q]); - run.arg("run").arg(&a).args(["--cmd", "image.adjustments.invert", "--out"]).arg(&out).args(["--quality", q]); - for cmd in [&mut convert, &mut run] { - let o = cmd.output().unwrap(); - assert_eq!(o.status.code(), Some(1), "--quality {q}"); - assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("bad --quality `{q}`: expected a whole number from 1 to 100")), "--quality {q}"); - assert!(!out.exists(), "--quality {q} wrote a file"); - } - let o = batch_with(&d, &input, "qb", invert, &["--format", "jpg", "--quality", q]); - assert_eq!(o.status.code(), Some(1), "batch --quality {q}"); - assert!(!d.join("qb").exists(), "batch --quality {q} wrote a folder"); - } - // The documented range still encodes, lower quality making a smaller file. - let sizes: Vec = ["1", "50", "100"] - .iter() - .map(|q| { - let out = d.join(format!("q{q}.jpg")); - ok(bin().arg("convert").arg(&a).arg(&out).args(["--quality", q])); - std::fs::read(out).unwrap().len() - }) - .collect(); - assert!(sizes[0] <= sizes[1] && sizes[1] <= sizes[2], "{sizes:?}"); -} - -/// DPX converts both ways, `info` reports its size, mode and depth, and `--dpx-bits` picks the -/// bits per sample (byte 803 of the header); anything but 8, 10, 12 or 16 is refused (#1049). -#[test] -fn dpx_converts_both_ways_with_the_bits_asked_for() { - let d = tmp("dpx"); - let a = d.join("a.png"); - write_png(&a, 8, 4, 3); - for (bits, want) in [(None, 8), (Some("10"), 10), (Some("12"), 12), (Some("16"), 16)] { - let out = d.join("a.dpx"); - let mut convert = bin(); - convert.arg("convert").arg(&a).arg(&out); - if let Some(b) = bits { - convert.args(["--dpx-bits", b]); - } - ok(&mut convert); - let bytes = std::fs::read(&out).unwrap(); - assert_eq!((&bytes[..4], bytes[803]), (&b"SDPX"[..], want), "--dpx-bits {bits:?}"); - } - let (info, _) = ok(bin().arg("info").arg(d.join("a.dpx")).arg("--compact")); - let v: Value = serde_json::from_str(&info).unwrap(); - assert_eq!((v["width"].as_u64(), v["height"].as_u64()), (Some(8), Some(4)), "{v}"); - assert!(info.contains("16") && info.to_lowercase().contains("rgb"), "{info}"); - let back = d.join("back.png"); - ok(bin().arg("convert").arg(d.join("a.dpx")).arg(&back)); - assert!(back.exists()); - for bad in ["24", "0", "abc"] { - let out = d.join("bad.dpx"); - let o = bin().arg("convert").arg(&a).arg(&out).args(["--dpx-bits", bad]).output().unwrap(); - assert_eq!(o.status.code(), Some(1), "--dpx-bits {bad}"); - assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("bad --dpx-bits `{bad}`: expected 8, 10, 12 or 16")), "--dpx-bits {bad}"); - assert!(!out.exists(), "--dpx-bits {bad} wrote a file"); - } -} - -/// `--tga-bits` picks a TGA's bits per pixel (byte 16 of its header); anything but 24 or 32 is -/// refused and nothing is written. -#[test] -fn tga_bits_choose_the_targa_depth() { - let d = tmp("tga-bits"); - let a = d.join("a.png"); - write_png(&a, 8, 4, 3); - for (bits, want) in [(None, 32), (Some("24"), 24), (Some("32"), 32)] { - let out = d.join("a.tga"); - let mut convert = bin(); - convert.arg("convert").arg(&a).arg(&out); - if let Some(b) = bits { - convert.args(["--tga-bits", b]); - } - ok(&mut convert); - assert_eq!(std::fs::read(&out).unwrap()[16], want, "--tga-bits {bits:?}"); - } - for bad in ["16", "0", "abc", ""] { - let out = d.join("bad.tga"); - let o = bin().arg("convert").arg(&a).arg(&out).args(["--tga-bits", bad]).output().unwrap(); - assert_eq!(o.status.code(), Some(1), "--tga-bits {bad}"); - assert!(String::from_utf8_lossy(&o.stderr).contains(&format!("bad --tga-bits `{bad}`: expected 24 or 32")), "--tga-bits {bad}"); - assert!(!out.exists(), "--tga-bits {bad} wrote a file"); - } -} - -/// #492: an `--out` folder that is the `--in` folder, however it is spelt, is refused before -/// anything is written, unless `--in-place` asks for it. -#[test] -fn batch_refuses_to_write_over_its_inputs() { - let d = tmp("batch-in-place"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - let img = photocraft_codecs::Image::from_u8(8, 4, photocraft_codecs::ChannelLayout::Rgb, vec![90; 96]).unwrap(); - std::fs::write(input.join("a.tif"), photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &Default::default()).unwrap()).unwrap(); - let actions = d.join("actions.json"); - std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); - let snapshot = || { - let mut files: Vec<(PathBuf, Vec)> = - std::fs::read_dir(&input).unwrap().map(|e| e.unwrap().path()).map(|p| (p.clone(), std::fs::read(p).unwrap())).collect(); - files.sort(); - files - }; - let before = snapshot(); - let batch = |out: &Path, extra: &[&str]| { - bin().current_dir(&d).args(["batch", "--actions"]).arg(&actions).args(["--in", "in", "--out"]).arg(out).args(extra).output().unwrap() - }; - #[cfg(unix)] - std::os::unix::fs::symlink(&input, d.join("link")).unwrap(); - let link = cfg!(unix).then(|| PathBuf::from("link")); - for out in [PathBuf::from("in"), PathBuf::from("./in/"), input.clone(), input.join(".")].iter().chain(&link) { - for extra in [&[][..], &["--format", "tif"][..]] { - let o = batch(out, extra); - let err = String::from_utf8_lossy(&o.stderr); - assert_eq!(o.status.code(), Some(1), "--out {} {extra:?}: {err}", out.display()); - assert!(err.contains("is the --in folder") && err.contains("--in-place"), "{err}"); - assert_eq!(snapshot(), before, "--out {} {extra:?} changed the originals", out.display()); - } - } - // A separate folder runs as before, leaving the originals alone. - let (out, _) = ok(bin().current_dir(&d).args(["batch", "--actions"]).arg(&actions).args(["--in", "in", "--out", "out"])); - assert!(out.contains("2 succeeded, 0 failed"), "{out}"); - assert_eq!(snapshot(), before); - // `--in-place` opts in: each result replaces its original. - let o = batch(Path::new("in"), &["--in-place"]); - assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); - let after = snapshot(); - assert_eq!(after.len(), 2); - assert!(after.iter().zip(&before).all(|(a, b)| a.0 == b.0 && a.1 != b.1), "both inputs replaced"); -} - -#[test] -fn commands_listing() { - let (out, _) = ok(bin().arg("commands")); - assert!(out.contains("file.new")); - let (out, _) = ok(bin().args(["commands", "--json", "--filter", "layer.new"])); - let v: Value = serde_json::from_str(&out).unwrap(); - assert!( - v.as_array() - .unwrap() - .iter() - .all(|c| { c["id"].as_str().unwrap().contains("layer.new") || c["label"].as_str().unwrap().to_lowercase().contains("layer.new") }) - ); -} - -#[test] -fn in_process_run_matches_binary() { - let mut out = Vec::new(); - let mut err = Vec::new(); - let code = photocraft_cli::run(&["commands".into(), "--filter".into(), "file.new".into()], &mut out, &mut err); - assert_eq!(code, 0); - assert!(String::from_utf8(out).unwrap().contains("file.new")); -} - -#[test] -fn mcp_stdio_handshake_and_tool_call() { - let mut child = bin().arg("mcp").stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().unwrap(); - let mut stdin = child.stdin.take().unwrap(); - let mut stdout = BufReader::new(child.stdout.take().unwrap()); - let mut send = |v: Value| { - writeln!(stdin, "{v}").unwrap(); - stdin.flush().unwrap(); - }; - let mut recv = |id: u64| -> Value { - loop { - let mut line = String::new(); - assert!(stdout.read_line(&mut line).unwrap() > 0, "server closed"); - let v: Value = serde_json::from_str(&line).unwrap(); - if v["id"] == id { - return v; - } - } - }; - send(json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{ - "protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}})); - let init = recv(1); - assert_eq!(init["result"]["serverInfo"]["name"], "photocraft", "{init}"); - send(json!({"jsonrpc":"2.0","method":"notifications/initialized"})); - send(json!({"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}})); - let tools = recv(2); - let names: Vec<&str> = tools["result"]["tools"].as_array().unwrap().iter().filter_map(|t| t["name"].as_str()).collect(); - assert!(names.contains(&"command_run") && names.contains(&"doc_render_preview"), "{names:?}"); - send(json!({"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"doc_new","arguments":{"width":8,"height":8}}})); - let r = recv(3); - assert_ne!(r["result"]["isError"], true, "{r}"); - send(json!({"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"doc_render_preview","arguments":{}}})); - let r = recv(4); - assert_eq!(r["result"]["content"][0]["type"], "image", "{r}"); - let _ = child.kill(); - let _ = child.wait(); -} - -/// `--format` mapping two inputs to one name fails the second instead of overwriting the first (#420). -#[test] -fn batch_fails_inputs_that_share_an_output_name() { - let d = tmp("batch-collide"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - // Same stem in other formats; `A.tif` differs only in case. - let img = photocraft_codecs::Image::from_u8(8, 4, photocraft_codecs::ChannelLayout::Rgb, vec![90; 96]).unwrap(); - for (name, format) in [("a.bmp", photocraft_codecs::Format::Bmp), ("A.tif", photocraft_codecs::Format::Tiff)] { - std::fs::write(input.join(name), photocraft_codecs::encode(&img, format, &Default::default()).unwrap()).unwrap(); - } - let actions = d.join("actions.json"); - std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); - let out_dir = d.join("out"); - let o = bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir).args(["--format", "png"]).output().unwrap(); - let (out, err) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); - assert!( - !o.status.success(), - "a lost result must fail the run -{out} -{err}" - ); - assert!(out.contains("1 succeeded, 2 failed"), "{out}"); - assert_eq!(err.matches("not written").count(), 2, "{err}"); - assert_eq!(std::fs::read_dir(&out_dir).unwrap().count(), 1); - // Keeping each file's format, every input gets its own output. - let out_dir = d.join("same"); - let (out, _) = ok(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir)); - assert!(out.contains("3 succeeded, 0 failed"), "{out}"); -} - -#[test] -fn droplet_runs_an_action_on_files() { - let d = tmp("droplet"); - write_png(&d.join("a.png"), 8, 4, 3); - write_png(&d.join("b.png"), 6, 2, 5); - let droplet = d.join("rot.pcdroplet"); - std::fs::write( - &droplet, - json!({"photocraftDroplet": 1, "name": "rot", "action": {"steps": [["image.imageRotation.90cw", {}]]}, "options": {"format": "png"}}).to_string(), - ) - .unwrap(); - let out_dir = d.join("out"); - let (out, _) = ok(bin().arg("droplet").arg(&droplet).arg(d.join("a.png")).arg(d.join("b.png")).arg("--out").arg(&out_dir)); - assert_eq!(out.lines().filter(|l| l.starts_with("ok")).count(), 2, "{out}"); - let img = photocraft_codecs::decode(&std::fs::read(out_dir.join("a.png")).unwrap()).unwrap(); - assert_eq!(img.dimensions(), (4, 8)); - let o = bin().arg("droplet").arg(d.join("a.png")).arg(d.join("b.png")).output().unwrap(); - assert!(!o.status.success(), "not a droplet"); -} - -#[test] -fn tiff_output_is_flat_unless_tiff_layers_is_given() { - let d = tmp("tiff-layers"); - let layer_count = |path: &std::path::Path| -> usize { - let (info, _) = ok(bin().arg("info").arg(path)); - let v: Value = serde_json::from_str(&info).unwrap(); - v["layers"].as_array().unwrap().len() - }; - for (name, flag, layers) in [("flat.tif", None, 1), ("layered.tif", Some("--tiff-layers"), 2)] { - let out = d.join(name); - let mut cmd = bin(); - cmd.args(["run", "--new", r#"{"width":16,"height":16}"#]).args(["--cmd", "layer.new.layer", "--params", r#"{"name":"Ink"}"#]); - if let Some(flag) = flag { - cmd.arg(flag); - } - ok(cmd.arg("--out").arg(&out)); - assert_eq!(layer_count(&out), layers, "{name}"); - } - // convert honours the same flag. - let src = d.join("layered.tif"); - let flat = d.join("converted.tif"); - ok(bin().arg("convert").arg(&src).arg(&flat)); - assert_eq!(layer_count(&flat), 1); - let kept = d.join("kept.tif"); - ok(bin().arg("convert").arg(&src).arg(&kept).arg("--tiff-layers")); - assert_eq!(layer_count(&kept), 2); - std::fs::remove_dir_all(d).unwrap(); -} - -/// Issue #1108: a non-Unicode argument (a Latin-1 file name on Linux) used to panic in -/// `std::env::args()` before anything was printed. It is a usage error naming the argument. -#[cfg(unix)] -#[test] -fn a_non_unicode_argument_is_a_usage_error_not_a_panic() { - use std::os::unix::ffi::OsStrExt; - let name = std::ffi::OsStr::from_bytes(b"caf\xe9.png"); - for args in [vec![std::ffi::OsStr::new("info"), name], vec![std::ffi::OsStr::new("convert"), name, std::ffi::OsStr::new("out.png")]] { - let o = bin().args(&args).output().unwrap(); - let err = String::from_utf8_lossy(&o.stderr); - assert_eq!(o.status.code(), Some(2), "{err}"); - assert!(err.contains("argument 2 is not valid Unicode") && err.contains("caf\u{fffd}.png"), "{err}"); - assert!(!err.contains("panicked"), "{err}"); - } -} - -/// `batch --actions set.atn`: a Photoshop Actions file plays like an Actions set, and its steps -/// PhotoCraft can't play are warned about up front. -#[test] -fn batch_plays_a_photoshop_actions_file() { - use photocraft_engine::actions_cmds::Action; - let d = tmp("atn"); - let input = d.join("in"); - std::fs::create_dir_all(&input).unwrap(); - write_png(&input.join("a.png"), 8, 4, 3); - let invert = Action::new("Negative", vec![("image.adjustments.invert".into(), json!({})), ("select.all".into(), json!({}))]); - let odd = Action::new("Odd", vec![(photocraft_engine::atn_cmds::PHOTOSHOP_STEP.into(), json!({"event": "syntheticFill", "name": "Generative Fill"}))]); - let (bytes, skipped) = photocraft_engine::atn_cmds::export("Set", &[&invert, &odd], &[], false).unwrap(); - assert!(skipped.is_empty()); - let atn = d.join("Set.atn"); - std::fs::write(&atn, bytes).unwrap(); - let out_dir = d.join("out"); - // Two actions: the set needs --action. - let o = bin().args(["batch", "--actions"]).arg(&atn).arg("--in").arg(&input).arg("--out").arg(&out_dir).output().unwrap(); - assert!(!o.status.success()); - assert!(String::from_utf8_lossy(&o.stderr).contains("--action"), "{}", String::from_utf8_lossy(&o.stderr)); - let (_, err) = ok(bin().args(["batch", "--actions"]).arg(&atn).args(["--action", "Negative", "--in"]).arg(&input).arg("--out").arg(&out_dir)); - assert!(out_dir.join("a.png").exists()); - assert!(!err.contains("warning"), "{err}"); - // The unsupported step is warned about, and fails the file as Photoshop would stop on it. - let o = bin().args(["batch", "--actions"]).arg(&atn).args(["--action", "Odd", "--in"]).arg(&input).arg("--out").arg(d.join("out2")).output().unwrap(); - let err = String::from_utf8_lossy(&o.stderr); - assert!(err.contains("Generative Fill"), "{err}"); - assert!(!o.status.success()); -} diff --git a/apps/photocraft-cli/tests/it/cli.rs b/apps/photocraft-cli/tests/it/cli.rs new file mode 100644 index 0000000000..e592d552e2 --- /dev/null +++ b/apps/photocraft-cli/tests/it/cli.rs @@ -0,0 +1,1498 @@ +//! Process-level tests of the `photocraft-cli` binary. + +use std::io::{BufRead, BufReader, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use serde_json::{Value, json}; + +/// The binary, without the variables `mcp` and `serve` fall back to: a test that wants one sets it. +fn bin() -> Command { + let mut cmd = Command::new(crate::binary()); + for var in ENV_VARS { + cmd.env_remove(var); + } + cmd +} + +const ENV_VARS: [&str; 3] = ["PHOTOCRAFT_CONTROL_PORT", "PHOTOCRAFT_CONTROL_TOKEN", "PHOTOCRAFT_CONTROL_TOKEN_FILE"]; + +fn tmp(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("pc-cli-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn write_png(path: &Path, w: u32, h: u32, seed: u8) { + let data: Vec = (0..w * h * 4).map(|i| (i as u8).wrapping_mul(seed).wrapping_add(seed)).collect(); + let img = photocraft_codecs::Image::from_u8(w, h, photocraft_codecs::ChannelLayout::Rgba, data).unwrap(); + std::fs::write(path, photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); +} + +/// What a successful run printed. +struct Printed { + stdout: String, + stderr: String, +} + +fn ok(cmd: &mut Command) -> Printed { + let o = cmd.output().unwrap(); + let (stdout, stderr) = (String::from_utf8_lossy(&o.stdout).into_owned(), String::from_utf8_lossy(&o.stderr).into_owned()); + assert!(o.status.success(), "exit {:?}\nstdout: {stdout}\nstderr: {stderr}", o.status); + Printed { stdout, stderr } +} + +#[test] +fn usage_and_unknown_command() { + let o = bin().output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&o.stderr).contains("convert")); + let o = bin().arg("frobnicate").output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + // `help` names a subcommand or nothing: anything else is a usage error too. + let o = bin().args(["help", "frobnicate"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(!o.stderr.is_empty()); + let out = ok(bin().arg("--help")).stdout; + assert!(out.contains("convert")); + let out = ok(bin().arg("--version")).stdout; + assert!(out.starts_with("photocraft-cli ")); +} + +/// #423: ` --help` and `-h` print usage and return at once (no inputs read, no files +/// written, no server started), whatever else is on the line. +#[test] +fn every_subcommand_answers_help() { + let d = tmp("help"); + for sub in ["convert", "info", "run", "batch", "droplet", "commands", "mcp", "serve"] { + for help in ["--help", "-h"] { + let o = bin().current_dir(&d).arg(sub).arg(help).arg("--out").arg("x.png").stdin(Stdio::null()).output().unwrap(); + let out = String::from_utf8_lossy(&o.stdout); + assert_eq!(o.status.code(), Some(0), "{sub} {help}: {}", String::from_utf8_lossy(&o.stderr)); + assert!(out.contains(&format!("photocraft-cli {sub}")), "{sub} {help}: {out}"); + } + } + // Help where a flag's value should be is still help, not a missing value or a file named `-h`. + // So is help after another mistake. + for args in [ + &["run", "x.png", "--out", "--help"][..], + &["commands", "--filter", "-h"], + &["mcp", "--bridge", "-h"], + &["serve", "--port", "--help"], + &["mcp", "stray.psd", "--help"], + &["serve", "--port", "abc", "--port", "1", "-h"], + ] { + let o = bin().current_dir(&d).args(args).stdin(Stdio::null()).output().unwrap(); + assert_eq!(o.status.code(), Some(0), "{args:?}: {}", String::from_utf8_lossy(&o.stderr)); + assert!(String::from_utf8_lossy(&o.stdout).contains(&format!("photocraft-cli {}", args[0])), "{args:?}"); + } + assert_eq!(std::fs::read_dir(&d).unwrap().count(), 0, "help wrote nothing"); +} + +/// #423: a flag the subcommand doesn't take is a usage error naming it, before any work. +#[test] +fn unknown_flags_are_usage_errors() { + let d = tmp("flags"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + let actions = d.join("actions.json"); + std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); + let out_dir = d.join("out"); + let fails = |cmd: &mut Command, flag: &str| { + let o = cmd.output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "{err}"); + assert!(err.contains(flag), "{err}"); + }; + for typo in [&["--fromat", "jpg"][..], &["--fromat=jpg"][..]] { + fails(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir).args(typo), "--fromat"); + assert!(!out_dir.exists(), "nothing written"); + } + let x = d.join("x.png"); + fails(bin().arg("run").arg(input.join("a.png")).args(["--cmd", "image.adjustments.invert", "--param", "{}", "--out"]).arg(&x), "--param"); + assert!(!x.exists()); + // A flag one subcommand takes is still unknown to another; a bare flag takes no value. + fails(bin().args(["info", "--json"]).arg(input.join("a.png")), "--json"); + let o = bin().args(["commands", "--json=yes"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&o.stderr).contains("--json")); + // A flag can't be a value: a missing value is reported, not filled with the next flag. + let o = bin() + .args(["batch", "--actions"]) + .arg(&actions) + .arg("--in") + .arg(&input) + .arg("--out") + .arg(&out_dir) + .args(["--format", "--quality", "80"]) + .output() + .unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&o.stderr).contains("a value is required for '--format")); + assert!(!out_dir.exists(), "nothing written"); + // `--flag=value` still passes a value that starts with `--`. + ok(bin().args(["commands", "--filter=--json"])); + // Every flag still works in both forms. + let out = ok(bin().args(["commands", "--json", "--filter=gaussian"])).stdout; + assert!(out.contains("filter.blur.gaussianBlur"), "{out}"); + // The Elliptical Marquee is a `select.rect` option, found by its keywords (#5060). + for needle in ["ellipse", "marquee"] { + let out = ok(bin().args(["commands", "--filter", needle])).stdout; + assert!(out.contains("select.rect"), "{needle}: {out}"); + } + ok(bin().args(["batch", "--actions"]).arg(&actions).arg(format!("--in={}", input.display())).arg("--out").arg(&out_dir).args([ + "--format=jpg", + "--quality", + "80", + ])); + assert!(out_dir.join("a.jpg").exists()); + let out = ok(bin().arg("info").arg(input.join("a.png")).arg("--compact")).stdout; + assert!(!out.trim().contains('\n'), "compact JSON is one line"); +} + +fn server_usage_error(args: &[&str], expected: &str) { + let o = bin().args(args).env_remove("PHOTOCRAFT_CONTROL_TOKEN").env_remove("PHOTOCRAFT_CONTROL_TOKEN_FILE").stdin(Stdio::null()).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "{args:?}: {err}"); + assert!(o.stdout.is_empty(), "usage errors must leave protocol stdout empty: {args:?}"); + assert!(err.contains(expected), "{args:?}: expected {expected:?} in {err}"); +} + +#[test] +fn server_modes_reject_ignored_or_conflicting_options() { + for sub in ["mcp", "serve"] { + server_usage_error(&[sub, "stray.psd"], "unexpected argument 'stray.psd'"); + for flag in ["--control-token", "--control-token-file"] { + server_usage_error(&[sub, flag, "unused"], if sub == "mcp" { "requires --bridge" } else { "requires --port" }); + } + for flag in ["--automation-read-root", "--automation-write-root"] { + server_usage_error(&[sub, flag, ".", flag, "."], "cannot be used multiple times"); + server_usage_error(&[sub, &format!("{flag}=")], &format!("a value is required for '{flag} ")); + server_usage_error(&[sub, flag, "--automation-write-root", "."], &format!("a value is required for '{flag} ")); + } + let transport = if sub == "mcp" { "--bridge" } else { "--port" }; + for flag in [transport, "--control-token", "--control-token-file"] { + // Valid values, so the repeat is what is refused (a transport value is parsed first). + server_usage_error(&[sub, flag, "7878", &format!("{flag}=7879")], "cannot be used multiple times"); + server_usage_error(&[sub, flag, ""], &format!("for '{flag} ")); + } + server_usage_error(&[sub, transport, "7878", "--control-token", "unused", "--control-token-file", "unused"], "cannot be used with"); + } + for flag in ["--automation-read-root", "--automation-write-root"] { + server_usage_error(&["mcp", "--bridge", "127.0.0.1:7878", flag, "."], "configure automation roots on the desktop app"); + } +} + +/// Each launch line would, before validation, have started a TCP server and written the token file. +#[test] +fn server_validation_does_not_consume_stdio_or_create_files() { + let d = tmp("server-validation"); + let token = d.join("must-not-be-created.token"); + let other = d.join("other.token"); + let token_flag = format!("--control-token-file={}", token.display()); + let other_flag = format!("--control-token-file={}", other.display()); + for tail in [&["stray"][..], &["--automation-read-root", ".", "--automation-read-root", "."], &["--port", "0"], &[&other_flag]] { + let mut child = bin() + .current_dir(&d) + .args(["serve", "--port", "0", &token_flag]) + .args(tail) + .env_remove("PHOTOCRAFT_CONTROL_TOKEN") + .env_remove("PHOTOCRAFT_CONTROL_TOKEN_FILE") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + // Keep stdin open: invalid launch arguments must fail before starting a server. + let input = child.stdin.take().unwrap(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while child.try_wait().unwrap().is_none() && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(10)); + } + if child.try_wait().unwrap().is_none() { + child.kill().unwrap(); + child.wait().unwrap(); + panic!("{tail:?}: invalid configuration started a server"); + } + drop(input); + let o = child.wait_with_output().unwrap(); + assert_eq!(o.status.code(), Some(2), "{tail:?}: {}", String::from_utf8_lossy(&o.stderr)); + assert!(o.stdout.is_empty(), "{tail:?}"); + assert!(!token.exists() && !other.exists(), "{tail:?}: validation must happen before creating credentials"); + } + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn convert_png_pcraft_png_is_lossless() { + let d = tmp("convert"); + let (a, b, c) = (d.join("a.png"), d.join("b.pcraft"), d.join("c.png")); + write_png(&a, 37, 23, 7); + ok(bin().args(["convert"]).arg(&a).arg(&b)); + assert!(photocraft_format::is_pcraft(&std::fs::read(&b).unwrap())); + ok(bin().args(["convert"]).arg(&b).arg(&c)); + let x = photocraft_codecs::decode(&std::fs::read(&a).unwrap()).unwrap(); + let y = photocraft_codecs::decode(&std::fs::read(&c).unwrap()).unwrap(); + assert_eq!(x.to_rgba8(), y.to_rgba8()); + // Format override and lossy warning path. + ok(bin().args(["convert"]).arg(&a).arg(d.join("out.bin")).args(["--format", "jpg", "--quality", "60"])); + assert!(photocraft_codecs::detect(&std::fs::read(d.join("out.bin")).unwrap()) == Some(photocraft_codecs::Format::Jpeg)); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn convert_errors() { + let d = tmp("converr"); + let o = bin().args(["convert", "/missing/in.png"]).arg(d.join("x.png")).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + let o = bin().args(["convert", "only-one"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn info_prints_layer_tree_json() { + let d = tmp("info"); + let a = d.join("a.png"); + write_png(&a, 10, 6, 3); + let out = ok(bin().arg("info").arg(&a)).stdout; + let v: Value = serde_json::from_str(&out).unwrap(); + assert_eq!(v["width"], 10); + assert_eq!(v["height"], 6); + // An RGBA PNG opens as a normal "Layer 0", not the locked Background (as in Photoshop). + assert_eq!(v["layers"][0]["name"], "Layer 0"); + assert!(v.get("history").is_none()); + let out = ok(bin().arg("info").arg(&a).arg("--compact")).stdout; + assert_eq!(out.trim().lines().count(), 1); + std::fs::remove_dir_all(d).unwrap(); +} + +/// #518, #523: a truncated JPEG and an animated GIF open, but `info` lists why the image is +/// incomplete and `convert` prints it on stderr (and still succeeds); complete files stay quiet. +#[test] +fn info_and_convert_report_decode_warnings() { + let d = tmp("decode-warnings"); + let noise: Vec = (0..64 * 48 * 3).map(|i: u32| (i.wrapping_mul(7919) % 251) as u8).collect(); + let img = photocraft_codecs::Image::from_u8(64, 48, photocraft_codecs::ChannelLayout::Rgb, noise).unwrap(); + let jpeg = photocraft_codecs::encode(&img, photocraft_codecs::Format::Jpeg, &Default::default()).unwrap(); + // Three 1x1 frames. + let mut gif = b"GIF89a\x01\0\x01\0\x80\0\0\0\0\0\xFF\xFF\xFF".to_vec(); + for _ in 0..3 { + gif.extend_from_slice(b"\x2C\0\0\0\0\x01\0\x01\0\0\x02\x02\x44\x01\0"); + } + gif.push(0x3B); + // A GIF opens in Indexed Color (#7886), so `convert` to PNG also notes the palette PNG it + // writes, after the decode warning. + let cases = [ + ("whole.jpg", &jpeg[..], None, None), + ("half.jpg", &jpeg[..jpeg.len() / 2], Some("JPEG data ends early (the file is truncated or damaged); part of the image is missing"), None), + ("anim.gif", &gif[..], Some("only the first of 3 frames was imported"), Some("written as an 8-bit palette PNG (2 colours)")), + ]; + for (name, bytes, warning, export) in cases { + let file = d.join(name); + std::fs::write(&file, bytes).unwrap(); + let out = ok(bin().arg("info").arg(&file).arg("--compact")).stdout; + let v: Value = serde_json::from_str(&out).unwrap(); + assert_eq!(v["warnings"], json!(warning.into_iter().collect::>()), "{name}"); + let err = ok(bin().arg("convert").arg(&file).arg(d.join(format!("{name}.png")))).stderr; + let expected: Vec = warning.into_iter().chain(export).map(|w| format!("warning: {w}")).collect(); + assert_eq!( + err.trim(), + expected.join( + " +" + ), + "{name}" + ); + } + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn run_commands_and_save() { + let d = tmp("run"); + let out_pc = d.join("r.pcraft"); + let out = ok(bin() + .args(["run", "--new", r#"{"width":40,"height":30,"name":"Run"}"#]) + .args(["--cmd", "layer.new.layer", "--params", r#"{"name":"Ink"}"#]) + .args(["--cmd", "paint.stroke", "--params", r##"{"points":[[2,2,1],[30,20,1]],"size":5,"color":"#00ff00"}"##]) + .args(["--cmd", "document.inspect"]) + .arg("--out") + .arg(&out_pc)) + .stdout; + let lines: Vec = out.lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(lines.len(), 3); + assert_eq!(lines[2]["command"], "document.inspect"); + assert_eq!(lines[2]["result"]["name"], "Run"); + let info = ok(bin().arg("info").arg(&out_pc)).stdout; + let v: Value = serde_json::from_str(&info).unwrap(); + // Saving a native document gives the reopened bundle its output filename. + assert_eq!(v["name"], "r.pcraft"); + let names: Vec<&str> = v["layers"].as_array().unwrap().iter().filter_map(|l| l["name"].as_str()).collect(); + assert_eq!(names, ["Ink", "Background"]); + // Continue from the saved file and export PNG. + let png = d.join("r.png"); + ok(bin().arg("run").arg(&out_pc).args(["--cmd", "layer.new.layer"]).arg("--out").arg(&png)); + assert_eq!(photocraft_codecs::decode(&std::fs::read(&png).unwrap()).unwrap().dimensions(), (40, 30)); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn run_stops_after_failed_action_without_exporting() { + let d = tmp("run-failed-action"); + let out = d.join("result.png"); + let result = bin() + .args(["run", "--new", r#"{"width":8,"height":8}"#]) + .args(["--cmd", "layer.new.layer"]) + .args(["--cmd", "actions.record", "--params", r#"{"name":"Delete"}"#]) + .args(["--cmd", "layer.delete", "--cmd", "actions.stop"]) + // The recorded deletion succeeded with two layers; replay cannot delete the last one. + .args(["--cmd", "actions.play", "--params", r#"{"action":"Delete"}"#]) + .args(["--cmd", "document.inspect", "--out"]) + .arg(&out) + .output() + .unwrap(); + let error = String::from_utf8_lossy(&result.stderr); + assert_eq!(result.status.code(), Some(1), "stdout: {}\nstderr: {error}", String::from_utf8_lossy(&result.stdout)); + assert!(error.contains("actions.play") && error.contains("Delete") && error.contains("step 1"), "{error}"); + let rows: Vec = String::from_utf8_lossy(&result.stdout).lines().map(|line| serde_json::from_str(line).unwrap()).collect(); + assert!(rows.iter().all(|row| row["command"] != "document.inspect"), "later commands must not run"); + assert!(!out.exists(), "a failed action must not export a partial result"); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn run_continues_after_successful_action_and_exports() { + let d = tmp("run-successful-action"); + let out = d.join("result.png"); + let stdout = ok(bin() + .args(["run", "--new", r#"{"width":8,"height":8}"#]) + .args(["--cmd", "actions.record", "--params", r#"{"name":"Add"}"#]) + .args(["--cmd", "layer.new.layer", "--cmd", "actions.stop"]) + .args(["--cmd", "actions.play", "--params", r#"{"action":"Add"}"#]) + .args(["--cmd", "document.inspect", "--out"]) + .arg(&out)) + .stdout; + let rows: Vec = stdout.lines().map(|line| serde_json::from_str(line).unwrap()).collect(); + let playback = rows.iter().find(|row| row["command"] == "actions.play").unwrap(); + assert_eq!(playback["result"]["ran"], 1); + assert!(playback["result"].get("failed").is_none()); + assert!(rows.iter().any(|row| row["command"] == "document.inspect")); + assert!(out.is_file()); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn run_errors() { + let o = bin().args(["run", "--new", "{}", "--cmd", "no.such"]).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + assert!(String::from_utf8_lossy(&o.stderr).contains("unknown command")); + // Neither a --cmd nor --out, and JSON that doesn't parse, are usage errors naming the flag. + let o = bin().args(["run", "--new", "{}", "--params", "{}"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&o.stderr).contains("--cmd")); + let o = bin().args(["run", "--new", "{", "--cmd", "x"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2)); + assert!(String::from_utf8_lossy(&o.stderr).contains("--new")); + // Params must be a JSON object, for `--new` and for each `--params`. + let o = bin().args(["run", "--new", "[3]", "--cmd", "layer.new.layer"]).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + let e = String::from_utf8_lossy(&o.stderr); + assert!(e.contains("--new") && e.contains("must be a JSON object"), "{e}"); + let o = bin().args(["run", "--new", "{}", "--cmd", "layer.new.layer", "--params", "\"x\""]).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + let e = String::from_utf8_lossy(&o.stderr); + assert!(e.contains("layer.new.layer") && e.contains("must be a JSON object"), "{e}"); +} + +#[test] +fn run_rejects_invalid_filter_parameters_without_exporting() { + let d = tmp("invalid-filter-params"); + let out = d.join("result.png"); + for (params, key) in [(r#"{"radius":"big"}"#, "radius"), (r#"{"radus":3}"#, "radus"), (r#"{"radius":5000}"#, "radius"), (r#"{"radius":-4}"#, "radius")] { + let result = bin() + .args(["run", "--new", r#"{"width":8,"height":8}"#, "--cmd", "filter.blur.gaussianBlur", "--params", params, "--out"]) + .arg(&out) + .output() + .unwrap(); + assert_eq!(result.status.code(), Some(1)); + let error = String::from_utf8_lossy(&result.stderr); + assert!(error.contains("filter.blur.gaussianBlur") && error.contains(key), "{error}"); + assert!(!out.exists()); + } + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn batch_applies_actions_to_directory() { + let d = tmp("batch"); + let (inp, outp) = (d.join("in"), d.join("out")); + std::fs::create_dir_all(&inp).unwrap(); + write_png(&inp.join("one.png"), 12, 8, 1); + write_png(&inp.join("two.png"), 9, 9, 2); + std::fs::write(inp.join("notes.txt"), "ignored").unwrap(); + let actions = d.join("actions.json"); + std::fs::write(&actions, json!([{"command": "layer.new.layer", "params": {"name": "Batch"}}]).to_string()).unwrap(); + let out = ok(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&inp).arg("--out").arg(&outp).args(["--format", "pcraft"])).stdout; + assert!(out.contains("2 succeeded, 0 failed"), "{out}"); + for n in ["one", "two"] { + let info = ok(bin().arg("info").arg(outp.join(format!("{n}.pcraft")))).stdout; + let v: Value = serde_json::from_str(&info).unwrap(); + assert_eq!(v["layers"][0]["name"], "Batch"); + } + // A failing action reports and exits 1. + std::fs::write(&actions, r#"{"actions":[{"id":"no.such.command"}]}"#).unwrap(); + let o = bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&inp).arg("--out").arg(&outp).output().unwrap(); + assert_eq!(o.status.code(), Some(1)); + assert!(String::from_utf8_lossy(&o.stderr).contains("FAIL")); + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn batch_skips_recorded_view_and_save_steps() { + let d = tmp("batch-shell-steps"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + let source = input.join("a.png"); + write_png(&source, 12, 8, 3); + let original = std::fs::read(&source).unwrap(); + let recorded_save = d.join("recorded-save.psd"); + let mut cases = Vec::new(); + for (name, id) in [("zoom-in", "view.zoomIn"), ("zoom-out", "view.zoomOut"), ("fit", "view.fitOnScreen"), ("actual-pixels", "view.actualPixels")] { + cases.push((name, json!([[id, {}], ["image.imageRotation.90cw", {}]]).to_string())); + } + cases.push(("save", json!([["file.save", {}], ["image.imageRotation.90cw", {}]]).to_string())); + cases.push(("save-as", json!([["file.saveAs", {"path": recorded_save.to_string_lossy()}], ["image.imageRotation.90cw", {}]]).to_string())); + + for (name, actions) in cases { + let o = batch_with(&d, &input, name, &actions, &[]); + let (stdout, stderr) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + assert!(o.status.success(), "{name}: {stdout}\n{stderr}"); + assert!(stdout.contains("1 succeeded, 0 failed"), "{name}: {stdout}"); + let output = d.join(name).join("a.png"); + let info = ok(bin().arg("info").arg(&output)).stdout; + let info: Value = serde_json::from_str(&info).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(12)), "{name}: {info}"); + assert_eq!(std::fs::read(&source).unwrap(), original, "{name} changed its input"); + assert!(!recorded_save.exists(), "{name} wrote the recorded Save As path"); + } + std::fs::remove_dir_all(d).unwrap(); +} + +#[test] +fn parse_actions_formats() { + let a = photocraft_cli::parse_actions(r#"[{"command":"a","params":{"x":1}},{"id":"b"}]"#).unwrap(); + assert_eq!(a, vec![("a".to_string(), json!({"x":1})), ("b".to_string(), json!({}))]); + assert!(photocraft_cli::parse_actions(r#"{"actions":[{"command":"c"}]}"#).unwrap().len() == 1); + assert!(photocraft_cli::parse_actions(r#"[{"params":{}}]"#).is_err()); + assert!(photocraft_cli::parse_actions("7").is_err()); + // #489: the step shapes a recorded action and a droplet store, bare or wrapped. + let want = vec![("a".to_string(), json!({"x":1})), ("b".to_string(), json!({}))]; + for text in [ + r#"[["a",{"x":1}],["b"]]"#, + r#"[["a",{"x":1}],"b"]"#, + r#"{"steps":[["a",{"x":1}],["b",{}]]}"#, + r#"{"photocraftDroplet":1,"action":{"steps":[["a",{"x":1}],{"command":"b"}]}}"#, + r#"{"actions":[["a",{"x":1}],["b",{}]]}"#, + ] { + assert_eq!(photocraft_cli::parse_actions(text).unwrap(), want, "{text}"); + } + for text in ["[42]", "[[]]", "[[7,{}]]", r#"{"name":"x"}"#] { + assert!(photocraft_cli::parse_actions(text).is_err(), "{text}"); + } +} + +/// Runs `batch` on `input` with `actions` (saved as `.json`), writing to the folder `/`. +fn batch_with(d: &Path, input: &Path, name: &str, actions: &str, extra: &[&str]) -> std::process::Output { + let file = d.join(format!("{name}.json")); + std::fs::write(&file, actions).unwrap(); + bin().args(["batch", "--actions"]).arg(&file).arg("--in").arg(input).arg("--out").arg(d.join(name)).args(extra).output().unwrap() +} + +/// #489: `batch --actions` takes `[id, params]` steps and a droplet's steps, with the same results +/// as the object form; a malformed step fails before anything is written. +#[test] +fn batch_accepts_recorded_action_steps() { + let d = tmp("batch-steps"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + write_png(&input.join("b.png"), 6, 5, 9); + let shapes = [ + ("objects", r#"[{"command":"image.adjustments.invert"}]"#), + ("pairs", r#"[["image.adjustments.invert",{}]]"#), + ("ids", r#"["image.adjustments.invert"]"#), + ("droplet", r#"{"photocraftDroplet":1,"action":{"steps":[["image.adjustments.invert",{}]]}}"#), + ]; + for (name, actions) in shapes { + let o = batch_with(&d, &input, name, actions, &[]); + assert!(o.status.success(), "{name}: {}", String::from_utf8_lossy(&o.stderr)); + assert!(String::from_utf8_lossy(&o.stdout).contains("2 succeeded, 0 failed"), "{name}"); + for f in ["a.png", "b.png"] { + assert_eq!(std::fs::read(d.join(name).join(f)).unwrap(), std::fs::read(d.join("objects").join(f)).unwrap(), "{name}/{f}"); + } + } + let o = batch_with(&d, &input, "bad", "[42]", &[]); + assert_eq!(o.status.code(), Some(1)); + assert!(!d.join("bad").exists(), "nothing written"); +} + +/// #490: a leading dot on `--format` doesn't double the dot in output names. +#[test] +fn batch_format_with_leading_dot() { + let d = tmp("batch-dot"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + write_png(&input.join("b.png"), 6, 5, 9); + let invert = r#"[{"command":"image.adjustments.invert"}]"#; + for (name, format) in [("dot", ".jpg"), ("plain", "jpg")] { + let o = batch_with(&d, &input, name, invert, &["--format", format]); + assert!(o.status.success(), "{format}: {}", String::from_utf8_lossy(&o.stderr)); + let mut names: Vec = std::fs::read_dir(d.join(name)).unwrap().map(|e| e.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names, ["a.jpg", "b.jpg"], "{format}"); + assert_eq!(photocraft_codecs::detect(&std::fs::read(d.join(name).join("a.jpg")).unwrap()), Some(photocraft_codecs::Format::Jpeg)); + } +} + +/// #491: `--quality` outside 1-100 is a usage error for every subcommand, whatever the number's +/// size, and nothing is written. +#[test] +fn quality_outside_1_to_100_is_refused() { + let d = tmp("quality"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + let a = input.join("a.png"); + write_png(&a, 8, 4, 3); + let invert = r#"[{"command":"image.adjustments.invert"}]"#; + for q in ["0", "101", "255", "256", "1000", "-1", "abc", "50.5", ""] { + let out = d.join("q.jpg"); + let (mut convert, mut run) = (bin(), bin()); + convert.arg("convert").arg(&a).arg(&out).args(["--quality", q]); + run.arg("run").arg(&a).args(["--cmd", "image.adjustments.invert", "--out"]).arg(&out).args(["--quality", q]); + for cmd in [&mut convert, &mut run] { + let o = cmd.output().unwrap(); + assert_eq!(o.status.code(), Some(2), "--quality {q}"); + assert!(String::from_utf8_lossy(&o.stderr).contains("--quality"), "--quality {q}"); + assert!(!out.exists(), "--quality {q} wrote a file"); + } + let o = batch_with(&d, &input, "qb", invert, &["--format", "jpg", "--quality", q]); + assert_eq!(o.status.code(), Some(2), "batch --quality {q}"); + assert!(!d.join("qb").exists(), "batch --quality {q} wrote a folder"); + } + // The documented range still encodes, lower quality making a smaller file. + let sizes: Vec = ["1", "50", "100"] + .iter() + .map(|q| { + let out = d.join(format!("q{q}.jpg")); + ok(bin().arg("convert").arg(&a).arg(&out).args(["--quality", q])); + std::fs::read(out).unwrap().len() + }) + .collect(); + assert!(sizes[0] <= sizes[1] && sizes[1] <= sizes[2], "{sizes:?}"); +} + +/// DPX converts both ways, `info` reports its size, mode and depth, and `--dpx-bits` picks the +/// bits per sample (byte 803 of the header); anything but 8, 10, 12 or 16 is a usage error (#1049). +#[test] +fn dpx_converts_both_ways_with_the_bits_asked_for() { + let d = tmp("dpx"); + let a = d.join("a.png"); + write_png(&a, 8, 4, 3); + for (bits, want) in [(None, 8), (Some("10"), 10), (Some("12"), 12), (Some("16"), 16)] { + let out = d.join("a.dpx"); + let mut convert = bin(); + convert.arg("convert").arg(&a).arg(&out); + if let Some(b) = bits { + convert.args(["--dpx-bits", b]); + } + ok(&mut convert); + let bytes = std::fs::read(&out).unwrap(); + assert_eq!((&bytes[..4], bytes[803]), (&b"SDPX"[..], want), "--dpx-bits {bits:?}"); + } + let info = ok(bin().arg("info").arg(d.join("a.dpx")).arg("--compact")).stdout; + let v: Value = serde_json::from_str(&info).unwrap(); + assert_eq!((v["width"].as_u64(), v["height"].as_u64()), (Some(8), Some(4)), "{v}"); + assert!(info.contains("16") && info.to_lowercase().contains("rgb"), "{info}"); + let back = d.join("back.png"); + ok(bin().arg("convert").arg(d.join("a.dpx")).arg(&back)); + assert!(back.exists()); + for bad in ["24", "0", "abc"] { + let out = d.join("bad.dpx"); + let o = bin().arg("convert").arg(&a).arg(&out).args(["--dpx-bits", bad]).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "--dpx-bits {bad}"); + assert!(err.contains("--dpx-bits") && err.contains("expected 8, 10, 12 or 16"), "--dpx-bits {bad}: {err}"); + assert!(!out.exists(), "--dpx-bits {bad} wrote a file"); + } +} + +/// DDS converts both ways; `--dds-compression` picks the FourCC (bytes 84–87; none writes +/// uncompressed pixels) and `--dds-mipmaps` the mip count (bytes 28–31). A compression that +/// isn't none, bc1, bc2 or bc3 is a usage error and nothing is written (#1327). +#[test] +fn dds_converts_both_ways_with_the_compression_asked_for() { + let d = tmp("dds"); + let a = d.join("a.png"); + write_png(&a, 8, 4, 3); + for (args, fourcc, mips) in [ + (&[][..], [0; 4], 1), + (&["--dds-compression", "bc1"][..], *b"DXT1", 1), + (&["--dds-compression", "DXT3", "--dds-mipmaps"][..], *b"DXT3", 4), + (&["--dds-compression", "bc3"][..], *b"DXT5", 1), + (&["--dds-mipmaps"][..], [0; 4], 4), + ] { + let out = d.join("a.dds"); + ok(bin().arg("convert").arg(&a).arg(&out).args(args)); + let bytes = std::fs::read(&out).unwrap(); + let mip_count = u32::from_le_bytes(bytes[28..32].try_into().unwrap()); + assert_eq!((&bytes[..4], &bytes[84..88], mip_count), (&b"DDS "[..], &fourcc[..], mips), "{args:?}"); + let back = d.join("back.png"); + ok(bin().arg("convert").arg(&out).arg(&back)); + assert!(back.exists(), "{args:?}"); + } + for bad in ["bc7", "dxt2", ""] { + let out = d.join("bad.dds"); + let o = bin().arg("convert").arg(&a).arg(&out).args(["--dds-compression", bad]).output().unwrap(); + assert_eq!(o.status.code(), Some(2), "--dds-compression {bad}"); + assert!(String::from_utf8_lossy(&o.stderr).contains("--dds-compression"), "--dds-compression {bad}"); + assert!(!out.exists(), "--dds-compression {bad} wrote a file"); + } +} + +/// `--tga-bits` picks a TGA's bits per pixel (byte 16 of its header); anything but 24 or 32 is a +/// usage error and nothing is written. +#[test] +fn tga_bits_choose_the_targa_depth() { + let d = tmp("tga-bits"); + let a = d.join("a.png"); + write_png(&a, 8, 4, 3); + for (bits, want) in [(None, 32), (Some("24"), 24), (Some("32"), 32)] { + let out = d.join("a.tga"); + let mut convert = bin(); + convert.arg("convert").arg(&a).arg(&out); + if let Some(b) = bits { + convert.args(["--tga-bits", b]); + } + ok(&mut convert); + assert_eq!(std::fs::read(&out).unwrap()[16], want, "--tga-bits {bits:?}"); + } + for bad in ["16", "0", "abc", ""] { + let out = d.join("bad.tga"); + let o = bin().arg("convert").arg(&a).arg(&out).args(["--tga-bits", bad]).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "--tga-bits {bad}"); + assert!(err.contains("--tga-bits") && err.contains("expected 24 or 32"), "--tga-bits {bad}: {err}"); + assert!(!out.exists(), "--tga-bits {bad} wrote a file"); + } +} + +/// #7437: `run --out` to that same file, however `--out` is spelt, is refused before the +/// file is read or any command runs, unless `--in-place` asks for it (as `batch` does, #492). +#[test] +fn run_refuses_to_write_over_its_input() { + let d = tmp("run-in-place"); + write_png(&d.join("in.png"), 8, 4, 7); + let before = std::fs::read(d.join("in.png")).unwrap(); + let run = |out: &Path, extra: &[&str]| { + bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out"]).arg(out).args(extra).output().unwrap() + }; + let mut outs = vec![PathBuf::from("in.png"), PathBuf::from("./in.png"), d.join("in.png"), d.join(".").join("in.png")]; + std::fs::hard_link(d.join("in.png"), d.join("hard.png")).unwrap(); + outs.push(PathBuf::from("hard.png")); + #[cfg(unix)] + { + std::os::unix::fs::symlink(d.join("in.png"), d.join("soft.png")).unwrap(); + outs.push(PathBuf::from("soft.png")); + } + for out in &outs { + for extra in [&[][..], &["--format", "png"][..]] { + let o = run(out, extra); + let (stdout, err) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + assert_eq!(o.status.code(), Some(1), "--out {} {extra:?}: {err}", out.display()); + assert!(err.contains("is the input file") && err.contains("--in-place"), "{err}"); + assert!(stdout.is_empty(), "--out {} {extra:?} ran commands: {stdout}", out.display()); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before, "--out {} {extra:?} changed the original", out.display()); + } + } + // Another file is written as before, leaving the original alone. + ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "out.png"])); + assert!(d.join("out.png").is_file()); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before); + // `--in-place` opts in: the inverted result replaces the original, which still opens. + ok(bin().current_dir(&d).args(["run", "in.png", "--cmd", "image.adjustments.invert", "--out", "in.png", "--in-place"])); + assert_ne!(std::fs::read(d.join("in.png")).unwrap(), before, "--in-place left the original unchanged"); + let out = ok(bin().current_dir(&d).args(["info", "in.png", "--compact"])).stdout; + let info: Value = serde_json::from_str(&out).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(4)), "{info}"); +} + +/// `--jpeg-format` picks the JPEG's scans: progressive writes a progressive frame (SOF2). One it +/// doesn't name is a usage error and nothing is written. +#[test] +fn jpeg_format_chooses_the_scans() { + let d = tmp("jpeg-format"); + let a = d.join("a.png"); + write_png(&a, 16, 8, 3); + let marker = |b: &[u8], m: u8| b.windows(2).any(|w| w == [0xFF, m]); + for (format, progressive) in [(None, false), (Some("baseline"), false), (Some("optimized"), false), (Some("progressive"), true)] { + let out = d.join("a.jpg"); + let mut convert = bin(); + convert.arg("convert").arg(&a).arg(&out); + if let Some(f) = format { + convert.args(["--jpeg-format", f]); + } + ok(&mut convert); + assert_eq!(marker(&std::fs::read(&out).unwrap(), 0xC2), progressive, "--jpeg-format {format:?}"); + } + for bad in ["Progressive", "1", ""] { + let out = d.join("bad.jpg"); + let o = bin().arg("convert").arg(&a).arg(&out).args(["--jpeg-format", bad]).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "--jpeg-format {bad}"); + assert!(err.contains("--jpeg-format") && err.contains("expected baseline, optimized or progressive"), "--jpeg-format {bad}: {err}"); + assert!(!out.exists(), "--jpeg-format {bad} wrote a file"); + } +} + +/// #492: an `--out` folder that is the `--in` folder, however it is spelt, is refused before +/// anything is written, unless `--in-place` asks for it. +#[test] +fn batch_refuses_to_write_over_its_inputs() { + let d = tmp("batch-in-place"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + let img = photocraft_codecs::Image::from_u8(8, 4, photocraft_codecs::ChannelLayout::Rgb, vec![90; 96]).unwrap(); + std::fs::write(input.join("a.tif"), photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &Default::default()).unwrap()).unwrap(); + let actions = d.join("actions.json"); + std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); + let snapshot = || { + let mut files: Vec<(PathBuf, Vec)> = + std::fs::read_dir(&input).unwrap().map(|e| e.unwrap().path()).map(|p| (p.clone(), std::fs::read(p).unwrap())).collect(); + files.sort(); + files + }; + let before = snapshot(); + let batch = |out: &Path, extra: &[&str]| { + bin().current_dir(&d).args(["batch", "--actions"]).arg(&actions).args(["--in", "in", "--out"]).arg(out).args(extra).output().unwrap() + }; + #[cfg(unix)] + std::os::unix::fs::symlink(&input, d.join("link")).unwrap(); + let link = cfg!(unix).then(|| PathBuf::from("link")); + for out in [PathBuf::from("in"), PathBuf::from("./in/"), input.clone(), input.join(".")].iter().chain(&link) { + for extra in [&[][..], &["--format", "tif"][..]] { + let o = batch(out, extra); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(1), "--out {} {extra:?}: {err}", out.display()); + assert!(err.contains("is the --in folder") && err.contains("--in-place"), "{err}"); + assert_eq!(snapshot(), before, "--out {} {extra:?} changed the originals", out.display()); + } + } + // A separate folder runs as before, leaving the originals alone. + let out = ok(bin().current_dir(&d).args(["batch", "--actions"]).arg(&actions).args(["--in", "in", "--out", "out"])).stdout; + assert!(out.contains("2 succeeded, 0 failed"), "{out}"); + assert_eq!(snapshot(), before); + // `--in-place` opts in: each result replaces its original. + let o = batch(Path::new("in"), &["--in-place"]); + assert!(o.status.success(), "{}", String::from_utf8_lossy(&o.stderr)); + let after = snapshot(); + assert_eq!(after.len(), 2); + assert!(after.iter().zip(&before).all(|(a, b)| a.0 == b.0 && a.1 != b.1), "both inputs replaced"); +} + +/// #7437: `convert` to its own input file, however the output is spelt, is refused before +/// anything is read or written, unless `--in-place` asks for it (as `batch` does, #492). +#[test] +fn convert_refuses_to_write_over_its_input() { + let d = tmp("convert-in-place"); + write_png(&d.join("in.png"), 8, 4, 5); + let before = std::fs::read(d.join("in.png")).unwrap(); + let convert = |out: &Path, extra: &[&str]| bin().current_dir(&d).args(["convert", "in.png"]).arg(out).args(extra).output().unwrap(); + let mut outs = vec![PathBuf::from("in.png"), PathBuf::from("./in.png"), d.join("in.png"), d.join(".").join("in.png")]; + std::fs::hard_link(d.join("in.png"), d.join("hard.png")).unwrap(); + outs.push(PathBuf::from("hard.png")); + #[cfg(unix)] + { + std::os::unix::fs::symlink(d.join("in.png"), d.join("soft.png")).unwrap(); + outs.push(PathBuf::from("soft.png")); + } + for out in &outs { + for extra in [&[][..], &["--format", "png"][..], &["--quality", "80"][..]] { + let o = convert(out, extra); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(1), "{} {extra:?}: {err}", out.display()); + assert!(err.contains("is the input file") && err.contains("--in-place"), "{err}"); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before, "{} {extra:?} changed the original", out.display()); + } + } + // Another file converts as before, leaving the original alone. + ok(bin().current_dir(&d).args(["convert", "in.png", "out.jpg"])); + assert!(d.join("out.jpg").is_file()); + assert_eq!(std::fs::read(d.join("in.png")).unwrap(), before); + // `--in-place` opts in: the result replaces the original, which still opens. + ok(bin().current_dir(&d).args(["convert", "in.png", "in.png", "--in-place"])); + let Printed { stdout: out, .. } = ok(bin().current_dir(&d).args(["info", "in.png", "--compact"])); + let info: Value = serde_json::from_str(&out).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(8), Some(4)), "{info}"); +} + +#[test] +fn commands_listing() { + let out = ok(bin().arg("commands")).stdout; + assert!(out.contains("file.new")); + let out = ok(bin().args(["commands", "--json", "--filter", "layer.new"])).stdout; + let v: Value = serde_json::from_str(&out).unwrap(); + assert!( + v.as_array() + .unwrap() + .iter() + .all(|c| { c["id"].as_str().unwrap().contains("layer.new") || c["label"].as_str().unwrap().to_lowercase().contains("layer.new") }) + ); +} + +#[test] +fn in_process_run_matches_binary() { + let mut out = Vec::new(); + let mut err = Vec::new(); + let code = photocraft_cli::run(["commands", "--filter", "file.new"], &mut out, &mut err); + assert_eq!(code, 0); + assert!(String::from_utf8(out).unwrap().contains("file.new")); +} + +#[test] +fn mcp_stdio_handshake_and_tool_call() { + let mut child = bin().arg("mcp").stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().unwrap(); + let mut stdin = child.stdin.take().unwrap(); + let mut stdout = BufReader::new(child.stdout.take().unwrap()); + let mut send = |v: Value| mcp_send(&mut stdin, v); + let mut recv = |id: u64| mcp_recv(&mut stdout, id); + send(json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{ + "protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}})); + let init = recv(1); + assert_eq!(init["result"]["serverInfo"]["name"], "photocraft", "{init}"); + send(json!({"jsonrpc":"2.0","method":"notifications/initialized"})); + send(json!({"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}})); + let tools = recv(2); + let names: Vec<&str> = tools["result"]["tools"].as_array().unwrap().iter().filter_map(|t| t["name"].as_str()).collect(); + assert!(names.contains(&"command_run") && names.contains(&"doc_render_preview"), "{names:?}"); + send(json!({"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"doc_new","arguments":{"width":8,"height":8}}})); + let r = recv(3); + assert_ne!(r["result"]["isError"], true, "{r}"); + send(json!({"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"doc_render_preview","arguments":{}}})); + let r = recv(4); + assert_eq!(r["result"]["content"][0]["type"], "image", "{r}"); + let _ = child.kill(); + let _ = child.wait(); +} + +/// Send one JSON-RPC line to an `mcp` child. +fn mcp_send(stdin: &mut impl Write, v: Value) { + writeln!(stdin, "{v}").unwrap(); + stdin.flush().unwrap(); +} + +/// Read an `mcp` child's lines up to the reply to `id`. +fn mcp_recv(stdout: &mut impl BufRead, id: u64) -> Value { + loop { + let mut line = String::new(); + assert!(stdout.read_line(&mut line).unwrap() > 0, "server closed"); + let v: Value = serde_json::from_str(&line).unwrap(); + if v["id"] == id { + return v; + } + } +} + +/// `mcp --bridge` takes a port or anything with a `:`; anything else is a usage error. A bare port +/// means 127.0.0.1, and the bridge connects at the first call, so with no app there the call fails +/// naming that address. +#[test] +fn mcp_bridge_takes_a_port_or_an_address() { + let o = bin().args(["mcp", "--bridge", "nope"]).stdin(Stdio::null()).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "{err}"); + assert!(err.contains("invalid value 'nope' for '--bridge"), "{err}"); + assert!(err.contains("expected a port, 0-65535 (--bridge also takes 127.0.0.1:)"), "{err}"); + + // Nothing listens on port 1 in a test run. The token only has to be well-formed. + let token = "a".repeat(64); + let mut child = bin() + .args(["mcp", "--bridge", "1", "--control-token", token.as_str()]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + let mut stdin = child.stdin.take().unwrap(); + let mut stdout = BufReader::new(child.stdout.take().unwrap()); + mcp_send( + &mut stdin, + json!({"jsonrpc":"2.0","id":1,"method":"initialize","params":{ + "protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}}), + ); + mcp_recv(&mut stdout, 1); + mcp_send(&mut stdin, json!({"jsonrpc":"2.0","method":"notifications/initialized"})); + mcp_send(&mut stdin, json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"session_list","arguments":{}}})); + let r = mcp_recv(&mut stdout, 2); + assert_eq!(r["result"]["isError"], true, "{r}"); + assert!(r["result"]["content"][0]["text"].as_str().unwrap_or_default().contains("127.0.0.1:1"), "{r}"); + let _ = child.kill(); + let _ = child.wait(); +} + +/// `--format` mapping two inputs to one name fails the second instead of overwriting the first (#420). +#[test] +fn batch_fails_inputs_that_share_an_output_name() { + let d = tmp("batch-collide"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + // Same stem in other formats; `A.tif` differs only in case. + let img = photocraft_codecs::Image::from_u8(8, 4, photocraft_codecs::ChannelLayout::Rgb, vec![90; 96]).unwrap(); + for (name, format) in [("a.bmp", photocraft_codecs::Format::Bmp), ("A.tif", photocraft_codecs::Format::Tiff)] { + std::fs::write(input.join(name), photocraft_codecs::encode(&img, format, &Default::default()).unwrap()).unwrap(); + } + let actions = d.join("actions.json"); + std::fs::write(&actions, r#"[{"command":"image.adjustments.invert"}]"#).unwrap(); + let out_dir = d.join("out"); + let o = bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir).args(["--format", "png"]).output().unwrap(); + let (out, err) = (String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + assert!( + !o.status.success(), + "a lost result must fail the run +{out} +{err}" + ); + assert!(out.contains("1 succeeded, 2 failed"), "{out}"); + assert_eq!(err.matches("not written").count(), 2, "{err}"); + assert_eq!(std::fs::read_dir(&out_dir).unwrap().count(), 1); + // Keeping each file's format, every input gets its own output. + let out_dir = d.join("same"); + let out = ok(bin().args(["batch", "--actions"]).arg(&actions).arg("--in").arg(&input).arg("--out").arg(&out_dir)).stdout; + assert!(out.contains("3 succeeded, 0 failed"), "{out}"); +} + +#[test] +fn droplet_runs_an_action_on_files() { + let d = tmp("droplet"); + write_png(&d.join("a.png"), 8, 4, 3); + write_png(&d.join("b.png"), 6, 2, 5); + let droplet = d.join("rot.pcdroplet"); + std::fs::write( + &droplet, + json!({"photocraftDroplet": 1, "name": "rot", "action": {"steps": [["image.imageRotation.90cw", {}]]}, "options": {"format": "png"}}).to_string(), + ) + .unwrap(); + let out_dir = d.join("out"); + let out = ok(bin().arg("droplet").arg(&droplet).arg(d.join("a.png")).arg(d.join("b.png")).arg("--out").arg(&out_dir)).stdout; + assert_eq!(out.lines().filter(|l| l.starts_with("ok")).count(), 2, "{out}"); + let img = photocraft_codecs::decode(&std::fs::read(out_dir.join("a.png")).unwrap()).unwrap(); + assert_eq!(img.dimensions(), (4, 8)); + let o = bin().arg("droplet").arg(d.join("a.png")).arg(d.join("b.png")).output().unwrap(); + assert!(!o.status.success(), "not a droplet"); +} + +#[test] +fn tiff_output_is_flat_unless_tiff_layers_is_given() { + let d = tmp("tiff-layers"); + let layer_count = |path: &std::path::Path| -> usize { + let info = ok(bin().arg("info").arg(path)).stdout; + let v: Value = serde_json::from_str(&info).unwrap(); + v["layers"].as_array().unwrap().len() + }; + for (name, flag, layers) in [("flat.tif", None, 1), ("layered.tif", Some("--tiff-layers"), 2)] { + let out = d.join(name); + let mut cmd = bin(); + cmd.args(["run", "--new", r#"{"width":16,"height":16}"#]).args(["--cmd", "layer.new.layer", "--params", r#"{"name":"Ink"}"#]); + if let Some(flag) = flag { + cmd.arg(flag); + } + ok(cmd.arg("--out").arg(&out)); + assert_eq!(layer_count(&out), layers, "{name}"); + } + // convert honours the same flag. + let src = d.join("layered.tif"); + let flat = d.join("converted.tif"); + ok(bin().arg("convert").arg(&src).arg(&flat)); + assert_eq!(layer_count(&flat), 1); + let kept = d.join("kept.tif"); + ok(bin().arg("convert").arg(&src).arg(&kept).arg("--tiff-layers")); + assert_eq!(layer_count(&kept), 2); + std::fs::remove_dir_all(d).unwrap(); +} + +/// Issue #1108: a non-Unicode argument (a Latin-1 file name on Linux) used to panic in +/// `std::env::args()` before anything was printed. clap reads the arguments as they are: a path +/// takes one, and text (`--filter`) refuses it with a usage error. +#[cfg(unix)] +#[test] +fn a_non_unicode_path_is_taken_and_non_unicode_text_is_a_usage_error() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + let d = tmp("non-unicode"); + let name = OsStr::from_bytes(b"caf\xe9.png"); + // No such file yet: the handler runs and fails on it (exit 1), no usage error. + for args in [vec![OsStr::new("info"), name], vec![OsStr::new("convert"), name, OsStr::new("out.png")]] { + let o = bin().current_dir(&d).args(&args).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(1), "{args:?}: {err}"); + assert!(!err.contains("panicked"), "{err}"); + } + // Linux file systems take the name (macOS ones refuse it). `convert` reads it; `info` opens + // through the automation layer, which still needs UTF-8 paths, and says so (exit 1). + if cfg!(target_os = "linux") { + write_png(&d.join(name), 8, 4, 3); + ok(bin().current_dir(&d).arg("convert").arg(name).arg("out.png")); + assert!(d.join("out.png").is_file()); + let o = bin().current_dir(&d).arg("info").arg(name).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(1), "{err}"); + assert!(err.contains("UTF-8"), "{err}"); + } + let o = bin().args([OsStr::new("commands"), OsStr::new("--filter"), name]).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!((o.status.code(), o.stdout.is_empty()), (Some(2), true), "{err}"); + assert!(err.contains("UTF-8") && !err.contains("panicked"), "{err}"); + std::fs::remove_dir_all(d).unwrap(); +} + +/// `batch --actions set.atn`: a Photoshop Actions file plays like an Actions set, and its steps +/// PhotoCraft can't play are warned about up front. +#[test] +fn batch_plays_a_photoshop_actions_file() { + use photocraft_engine::actions_cmds::Action; + let d = tmp("atn"); + let input = d.join("in"); + std::fs::create_dir_all(&input).unwrap(); + write_png(&input.join("a.png"), 8, 4, 3); + let invert = Action::new("Negative", vec![("image.adjustments.invert".into(), json!({})), ("select.all".into(), json!({}))]); + let odd = Action::new("Odd", vec![(photocraft_engine::atn_cmds::PHOTOSHOP_STEP.into(), json!({"event": "syntheticFill", "name": "Generative Fill"}))]); + let (bytes, skipped) = photocraft_engine::atn_cmds::export("Set", &[&invert, &odd], &[], false).unwrap(); + assert!(skipped.is_empty()); + let atn = d.join("Set.atn"); + std::fs::write(&atn, bytes).unwrap(); + let out_dir = d.join("out"); + // Two actions: the set needs --action. + let o = bin().args(["batch", "--actions"]).arg(&atn).arg("--in").arg(&input).arg("--out").arg(&out_dir).output().unwrap(); + assert!(!o.status.success()); + assert!(String::from_utf8_lossy(&o.stderr).contains("--action"), "{}", String::from_utf8_lossy(&o.stderr)); + let err = ok(bin().args(["batch", "--actions"]).arg(&atn).args(["--action", "Negative", "--in"]).arg(&input).arg("--out").arg(&out_dir)).stderr; + assert!(out_dir.join("a.png").exists()); + assert!(!err.contains("warning"), "{err}"); + // The unsupported step is warned about, and fails the file as Photoshop would stop on it. + let o = bin().args(["batch", "--actions"]).arg(&atn).args(["--action", "Odd", "--in"]).arg(&input).arg("--out").arg(d.join("out2")).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert!(err.contains("Generative Fill"), "{err}"); + assert!(!o.status.success()); +} + +/// What `photocraft_cli::run` returned and wrote, in-process. +struct Invoke { + code: i32, + stdout: String, + stderr: String, +} + +fn invoke(args: &[&str]) -> Invoke { + let (mut stdout, mut stderr) = (Vec::new(), Vec::new()); + let code = photocraft_cli::run(args, &mut stdout, &mut stderr); + Invoke { code, stdout: String::from_utf8(stdout).unwrap(), stderr: String::from_utf8(stderr).unwrap() } +} + +const SUBCOMMANDS: [&str; 8] = ["convert", "info", "run", "batch", "droplet", "commands", "mcp", "serve"]; + +/// Pins top-level dispatch: help and the version go to stdout (exit 0), no arguments is a usage +/// error on stderr. There is no `version` subcommand. +#[test] +fn top_level_help_version_and_no_arguments() { + let Invoke { code, stdout: out, stderr: err } = invoke(&[]); + assert_eq!((code, out.is_empty(), err.is_empty()), (2, true, false)); + for help in ["--help", "-h", "help"] { + let Invoke { code, stdout: out, stderr: err } = invoke(&[help]); + assert_eq!((code, out.is_empty(), err.is_empty()), (0, false, true), "{help}"); + } + for version in ["--version", "-V"] { + let Invoke { code, stdout: out, stderr: err } = invoke(&[version]); + assert_eq!((code, err.as_str()), (0, ""), "{version}"); + assert!(out.starts_with("photocraft-cli ") && out.ends_with('\n') && out.lines().count() == 1, "{version}: {out}"); + } + let Invoke { code, stdout: out, stderr: err } = invoke(&["version"]); + assert_eq!((code, out.is_empty()), (2, true)); + assert!(err.contains("version"), "{err}"); + let Invoke { code, stdout: out, stderr: err } = invoke(&["frobnicate"]); + assert_eq!((code, out.is_empty()), (2, true)); + assert!(err.contains("frobnicate"), "{err}"); +} + +/// ` --help` / `-h` prints help on stdout and returns without running the subcommand +/// (`mcp` and `serve` would otherwise block on stdin). +#[test] +fn every_subcommand_help_returns_on_stdout() { + for sub in SUBCOMMANDS { + for help in ["--help", "-h"] { + let Invoke { code, stdout: out, stderr: err } = invoke(&[sub, help]); + assert_eq!((code, out.is_empty(), err.is_empty()), (0, false, true), "{sub} {help}: {err}"); + // `commands` would also exit 0 with output if it ran; its list names `file.new`, help doesn't. + assert!(!out.contains("file.new"), "{sub} {help} ran instead of printing help: {out}"); + } + } +} + +/// Every subcommand refuses an unknown flag (bare or `=value`) with exit 2, naming it on stderr. +#[test] +fn every_subcommand_rejects_an_unknown_flag() { + for sub in SUBCOMMANDS { + for flag in [&["--fromat"][..], &["--fromat=jpg"][..], &["--fromat", "jpg"][..]] { + let args: Vec<&str> = [sub].into_iter().chain(flag.iter().copied()).collect(); + let Invoke { code, stdout: out, stderr: err } = invoke(&args); + assert_eq!((code, out.is_empty()), (2, true), "{args:?}: {err}"); + assert!(err.contains("--fromat"), "{args:?}: {err}"); + } + } +} + +/// `--flag=value`, a bare flag given a value, a value flag without one, repeated flags, and values +/// that start with `-`. +#[test] +fn flag_value_forms_and_repeats() { + let ids = |out: &str| out.lines().filter_map(|l| l.split_whitespace().next()).map(str::to_owned).collect::>(); + let Invoke { code, stdout: eq, stderr: err } = invoke(&["commands", "--filter=file.new"]); + assert_eq!(code, 0, "{err}"); + let spaced = invoke(&["commands", "--filter", "file.new"]).stdout; + assert_eq!(eq, spaced); + assert!(ids(&eq).contains(&"file.new".to_string()), "{eq}"); + // A repeated flag is a usage error naming it, more likely a mistake than a choice. + for repeat in [&["commands", "--filter", "zzz-no-match", "--filter", "file.new"][..], &["commands", "--json", "--json"][..]] { + let Invoke { code, stdout: out, stderr: err } = invoke(repeat); + assert_eq!((code, out.is_empty()), (2, true), "{repeat:?}"); + assert!(err.contains(repeat[1]) && err.contains("cannot be used multiple times"), "{repeat:?}: {err}"); + } + let Invoke { code, stderr: err, .. } = invoke(&["convert", "a.png", "b.jpg", "--quality", "80", "--quality", "60"]); + assert_eq!(code, 2); + assert!(err.contains("--quality"), "{err}"); + // A value starting with `-` needs `=`; without it the token is a flag, never the value. A + // negative number is still a value. + let Invoke { code, stdout: out, stderr: err } = invoke(&["commands", "--filter=-zzz-no-match"]); + assert_eq!((code, out.as_str()), (0, ""), "{err}"); + for value in ["-zzz-no-match", "--json"] { + let Invoke { code, stdout: out, .. } = invoke(&["commands", "--filter", value]); + assert_eq!((code, out.is_empty()), (2, true), "--filter {value}"); + } + let Invoke { code, stderr: err, .. } = invoke(&["commands", "--filter", "-1"]); + assert_eq!(code, 0, "{err}"); + // A bare flag takes no value; a value flag needs one. + let Invoke { code, stdout: out, stderr: err } = invoke(&["commands", "--json=1"]); + assert_eq!((code, out.is_empty()), (2, true)); + assert!(err.contains("--json"), "{err}"); + let Invoke { code, stdout: out, stderr: err } = invoke(&["commands", "--filter"]); + assert_eq!((code, out.is_empty()), (2, true)); + assert!(err.contains("--filter"), "{err}"); +} + +/// Each `--params` applies to the `--cmd` before it; a `--cmd` without one gets `{}`; a +/// `--params` ahead of every `--cmd` fails naming `--params`. +#[test] +fn run_pairs_params_with_the_preceding_cmd() { + let Invoke { code, stdout: out, stderr: err } = invoke(&[ + "run", + "--new", + r#"{"width":8,"height":8}"#, + "--cmd", + "layer.new.layer", + "--params", + r#"{"name":"First"}"#, + "--cmd", + "layer.new.layer", + "--cmd", + "layer.new.layer", + "--params", + r#"{"name":"Third"}"#, + "--cmd", + "document.inspect", + ]); + assert_eq!(code, 0, "{err}"); + let rows: Vec = out.lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(rows.len(), 4, "{out}"); + let names: Vec<&str> = rows[3]["result"]["layers"].as_array().unwrap().iter().filter_map(|l| l["name"].as_str()).collect(); + let count = |n: &str| names.iter().filter(|x| **x == n).count(); + assert_eq!((count("First"), count("Third")), (1, 1), "each --params applies to its own --cmd only: {names:?}"); + // Three new layers plus the background; the middle one (no --params) has a default name. + assert_eq!(names.len(), 4, "{names:?}"); + // Layers list top first: Third, the unnamed middle one, First, Background. Pairing --params + // with --cmd by position (params[i] -> cmd[i]) would name the middle one Third instead. + assert_eq!((names[0], names[2]), ("Third", "First"), "{names:?}"); + let Invoke { code, stderr: err, .. } = invoke(&["run", "--new", "{}", "--params", "{}", "--cmd", "layer.new.layer"]); + assert_ne!(code, 0); + assert!(err.contains("--params"), "{err}"); +} + +/// #7789: `convert` opens a developed raw at Raw Defaults › Resolution (default 300 ppi), as +/// `run`, `info` and the app do; it wrote 72. +#[test] +fn convert_opens_a_raw_at_the_raw_defaults_resolution() { + use photocraft_raw::testgen::{DngSpec, mosaic, scene}; + let d = tmp("convert-raw"); + let (w, h) = (24, 16); + let (raw, psd) = (d.join("IMG_0001.dng"), d.join("IMG_0001.psd")); + std::fs::write(&raw, DngSpec::cfa(w, h, mosaic(&scene(w, h), w, [0, 1, 1, 2], 0, 65535)).build()).unwrap(); + ok(bin().args(["convert"]).arg(&raw).arg(&psd)); + let doc = photocraft_io::import("IMG_0001.psd", &std::fs::read(&psd).unwrap()).unwrap().document; + assert_eq!(doc.resolution_dpi, 300.0); + std::fs::remove_dir_all(d).unwrap(); +} + +/// `--out=-image.png` writes `-image.png`; `--out -image.png` is a usage error and writes nothing. +#[test] +fn a_value_starting_with_a_dash_is_written_with_equals() { + let d = tmp("dash-value"); + let o = bin().current_dir(&d).args(["run", "--new", r#"{"width":4,"height":4}"#, "--out", "-image.png"]).output().unwrap(); + assert_eq!(o.status.code(), Some(2), "{}", String::from_utf8_lossy(&o.stderr)); + assert_eq!(std::fs::read_dir(&d).unwrap().count(), 0); + ok(bin().current_dir(&d).args(["run", "--new", r#"{"width":4,"height":4}"#, "--out=-image.png"])); + assert!(d.join("-image.png").is_file()); + std::fs::remove_dir_all(d).unwrap(); +} + +/// `run` takes exactly one of `` or `--new`, and `mcp`/`serve` one of `--control-token` or +/// `--control-token-file`: anything else is a usage error naming the flags, before any work. +#[test] +fn either_or_flags_are_usage_errors() { + let Invoke { code, stderr: err, .. } = invoke(&["run", "a.png", "--new", "{}", "--cmd", "layer.new.layer"]); + assert_eq!(code, 2); + assert!(err.contains("--new") && err.contains("cannot be used with"), "{err}"); + let Invoke { code, stderr: err, .. } = invoke(&["run", "--cmd", "layer.new.layer"]); + assert_eq!(code, 2); + assert!(err.contains("--new"), "{err}"); + let Invoke { code, stderr: err, .. } = invoke(&["run", "--new", "{}", "--cmd", "layer.new.layer", "--params", "{"]); + assert_eq!(code, 2); + assert!(err.contains("--params"), "{err}"); + let token = "a".repeat(64); + for mode in [["mcp", "--bridge", "1"], ["serve", "--port", "0"]] { + let o = bin().args(mode).args(["--control-token", token.as_str(), "--control-token-file", "token"]).stdin(Stdio::null()).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "{mode:?}: {err}"); + assert!(err.contains("--control-token-file") && err.contains("cannot be used with"), "{mode:?}: {err}"); + } + for port in ["70000", "x"] { + let o = bin().args(["serve", "--port", port]).stdin(Stdio::null()).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert_eq!(o.status.code(), Some(2), "--port {port}: {err}"); + assert!(err.contains("--port"), "--port {port}: {err}"); + } +} + +/// How a run of the binary ended. +struct Exited { + code: Option, + stderr: String, +} + +/// The binary run with `args` and one variable set, stdin at its end. +fn with_var(args: &[&str], var: &str, value: &str) -> Exited { + let o = bin().args(args).env(var, value).stdin(Stdio::null()).output().unwrap(); + Exited { code: o.status.code(), stderr: String::from_utf8_lossy(&o.stderr).into_owned() } +} + +/// A port nothing listens on, for a `serve` to take. +fn free_port() -> u16 { + std::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).unwrap().local_addr().unwrap().port() +} + +/// `serve` on TCP never exits: read its stderr until it says it is serving (or its stderr closes), +/// then kill it. +fn serve_tcp_stderr(cmd: &mut Command) -> String { + use std::io::Read as _; + let mut child = cmd.stdin(Stdio::null()).stderr(Stdio::piped()).spawn().unwrap(); + // Read on a thread, so a `serve` that stays silent with stderr open fails the test after the + // deadline instead of hanging it. + let mut stderr = child.stderr.take().unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let mut buf = [0u8; 4096]; + while let Ok(n) = stderr.read(&mut buf) { + if n == 0 || tx.send(buf[..n].to_vec()).is_err() { + break; + } + } + }); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); + let mut err = String::new(); + while !err.contains("photocraft-cli serving on") { + match rx.recv_timeout(deadline.saturating_duration_since(std::time::Instant::now())) { + Ok(chunk) => err.push_str(&String::from_utf8_lossy(&chunk)), + Err(_) => break, + } + } + let _ = child.kill(); + let _ = child.wait(); + err +} + +/// #2491: `PHOTOCRAFT_CONTROL_PORT` turns `serve` from stdio into a TCP listener and `mcp` from a +/// headless engine into a bridge, and both say so on stderr. A flag wins, an empty value counts as +/// unset, and a bad value is a usage error naming the variable. +#[test] +fn control_port_env_switches_modes_with_a_notice_and_flags_win() { + let port = free_port(); + let err = serve_tcp_stderr(bin().arg("serve").env("PHOTOCRAFT_CONTROL_PORT", port.to_string())); + assert!(err.contains(&format!("PHOTOCRAFT_CONTROL_PORT={port} is set; serving on TCP instead of stdio")), "{err}"); + assert!(err.contains("unset it to serve stdio") && err.contains("photocraft-cli serving on"), "{err}"); + + // No token is given, so the bridge fails after the notice. + let err = with_var(&["mcp"], "PHOTOCRAFT_CONTROL_PORT", &port.to_string()).stderr; + assert!(err.contains(&format!("PHOTOCRAFT_CONTROL_PORT is set; bridging to the app on 127.0.0.1:{port}")), "{err}"); + assert!(err.contains("unset it to run headless"), "{err}"); + + // The flag wins, so the variable's bad value is never parsed. + let err = serve_tcp_stderr(bin().args(["serve", "--port"]).arg(port.to_string()).env("PHOTOCRAFT_CONTROL_PORT", "not-a-port")); + assert!(err.contains("photocraft-cli serving on") && !err.contains("PHOTOCRAFT_CONTROL_PORT"), "{err}"); + + // Empty or blank counts as unset: `serve` reads stdio (at its end, so it exits at once) and + // `mcp` stays headless. + // A root on the command line is the headless engine's: the variable can't switch to a bridge + // that would drop it. + for root in ["--automation-read-root", "--automation-write-root"] { + let Exited { code, stderr: err } = with_var(&["mcp", root, "."], "PHOTOCRAFT_CONTROL_PORT", &port.to_string()); + assert_eq!(code, Some(2), "{root}: {err}"); + assert!(err.contains(root) && err.contains("cannot be used with 'PHOTOCRAFT_CONTROL_PORT'") && !err.contains("bridging"), "{root}: {err}"); + } + + for empty in ["", " "] { + let Exited { code, stderr: err } = with_var(&["serve"], "PHOTOCRAFT_CONTROL_PORT", empty); + assert!(code == Some(0) && !err.contains("PHOTOCRAFT_CONTROL_PORT"), "{empty:?}: {err}"); + let err = with_var(&["mcp"], "PHOTOCRAFT_CONTROL_PORT", empty).stderr; + assert!(!err.contains("bridging"), "{empty:?}: {err}"); + } + + for (sub, bad) in [ + ("serve", "not-a-port"), + ("serve", "70000"), + ("mcp", "nope"), + ("mcp", "70000"), + // `--bridge` takes host:port, the variable only a port. + ("mcp", "127.0.0.1:7878"), + ("mcp", ":7878"), + ("mcp", "bad:port"), + ] { + let Exited { code, stderr: err } = with_var(&[sub], "PHOTOCRAFT_CONTROL_PORT", bad); + assert_eq!(code, Some(2), "{sub} {bad}: {err}"); + assert!(err.contains(&format!("invalid value '{bad}' for 'PHOTOCRAFT_CONTROL_PORT'")), "{sub} {bad}: {err}"); + } +} + +/// `--control-token` and `--control-token-file` fall back to `PHOTOCRAFT_CONTROL_TOKEN` and +/// `PHOTOCRAFT_CONTROL_TOKEN_FILE`. One from the environment and the other as a flag is the +/// either/or usage error, naming the variable, when a token is used: a stale exported pair doesn't +/// stop headless `mcp` or stdio `serve`. Help names the token's variable, never its value. +#[test] +fn control_token_env_vars_are_the_flags_fallbacks() { + let token = "a".repeat(64); + for mode in [["mcp", "--bridge", "1"], ["serve", "--port", "0"]] { + let Exited { code, stderr: err } = with_var(&[&mode[..], &["--control-token", token.as_str()][..]].concat(), "PHOTOCRAFT_CONTROL_TOKEN_FILE", "token"); + assert_eq!(code, Some(2), "{mode:?}: {err}"); + assert!(err.contains("PHOTOCRAFT_CONTROL_TOKEN_FILE") && err.contains("cannot be used with"), "{mode:?}: {err}"); + let Exited { code, stderr: err } = with_var(&[&mode[..], &["--control-token-file", "token"][..]].concat(), "PHOTOCRAFT_CONTROL_TOKEN", &token); + assert_eq!(code, Some(2), "{mode:?}: {err}"); + assert!(err.contains("PHOTOCRAFT_CONTROL_TOKEN") && err.contains("cannot be used with") && !err.contains(&token), "{mode:?}: {err}"); + } + for sub in ["mcp", "serve"] { + let o = bin().arg(sub).env("PHOTOCRAFT_CONTROL_TOKEN", &token).env("PHOTOCRAFT_CONTROL_TOKEN_FILE", "token").stdin(Stdio::null()).output().unwrap(); + let err = String::from_utf8_lossy(&o.stderr); + assert!(!err.contains("cannot be used with"), "{sub}: {err}"); + // stdio `serve` exits 0 at the end of its input; how headless `mcp` ends there isn't + // pinned, so it only must not be the usage error. + if sub == "serve" { + assert_eq!(o.status.code(), Some(0), "{sub}: {err}"); + } else { + assert_ne!(o.status.code(), Some(2), "{sub}: {err}"); + } + let o = bin().args([sub, "--help"]).env("PHOTOCRAFT_CONTROL_TOKEN", &token).output().unwrap(); + let out = String::from_utf8_lossy(&o.stdout); + assert!(out.contains("[env: PHOTOCRAFT_CONTROL_TOKEN]") && !out.contains(&token), "{sub}: {out}"); + } + // The server takes the variable's token, so it neither generates nor prints one; an empty + // variable counts as unset. + let err = serve_tcp_stderr(bin().args(["serve", "--port"]).arg(free_port().to_string()).env("PHOTOCRAFT_CONTROL_TOKEN", &token)); + assert!(err.contains("photocraft-cli serving on") && !err.contains("control token"), "{err}"); + let err = serve_tcp_stderr(bin().args(["serve", "--port"]).arg(free_port().to_string()).env("PHOTOCRAFT_CONTROL_TOKEN", "")); + assert!(err.contains("photocraft-cli serving on") && err.contains("photocraft-cli control token: "), "{err}"); +} + +/// #8802: Deselect with nothing selected is skipped in a `run`, as action playback does (#6744), +/// so a script that deselects after Reveal Selection still finishes and saves. +#[test] +fn run_skips_deselect_with_nothing_selected() { + let folder = tmp("deselect-run"); + let native = folder.join("out.pcraft").to_string_lossy().to_string(); + let Invoke { code, stdout, stderr } = invoke(&[ + "run", + "--new", + r#"{"width":16,"height":16}"#, + "--cmd", + "select.rect", + "--params", + r#"{"x":0,"y":0,"width":8,"height":16}"#, + "--cmd", + "layer.new.layer", + "--cmd", + "layer.layerMask.revealSelection", + "--cmd", + "select.deselect", + "--cmd", + "layer.new.layer", + "--params", + r#"{"name":"after"}"#, + "--out", + &native, + ]); + assert_eq!(code, 0, "{stderr}"); + let results: Vec = stdout.lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(results.len(), 5, "every command reports a result: {stdout}"); + assert_eq!(results[3], json!({"command": "select.deselect", "result": null})); + let Invoke { code, stdout, stderr } = invoke(&["info", &native, "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!(info["layers"][0]["name"], "after", "the steps after Deselect ran: {info}"); + + // Deselect with a selection still deselects: Inverse afterwards finds nothing to invert. + let Invoke { code, stderr, .. } = invoke(&[ + "run", + "--new", + r#"{"width":16,"height":16}"#, + "--cmd", + "select.all", + "--cmd", + "select.deselect", + "--cmd", + "select.inverse", + "--out", + &native, + ]); + assert_eq!(code, 1, "{stderr}"); + assert!(stderr.contains("select.inverse") && stderr.contains("no selection"), "{stderr}"); + let _ = std::fs::remove_dir_all(&folder); +} + +/// #8802: a batched action that starts with Deselect plays on files with nothing selected, as the +/// same action does in the Actions panel (#6744). +#[test] +fn batch_skips_deselect_with_nothing_selected() { + let dir = tmp("deselect-batch"); + let path = |name: &str| dir.join(name).to_string_lossy().to_string(); + std::fs::create_dir_all(dir.join("in")).unwrap(); + let Invoke { code, stderr, .. } = invoke(&["run", "--new", r#"{"width":8,"height":6}"#, "--out", &path("in/a.png")]); + assert_eq!(code, 0, "{stderr}"); + std::fs::write(dir.join("steps.json"), json!([["select.deselect", {}], ["image.imageRotation.90cw", {}]]).to_string()).unwrap(); + let Invoke { code, stdout, stderr } = invoke(&["batch", "--actions", &path("steps.json"), "--in", &path("in"), "--out", &path("out")]); + assert_eq!(code, 0, "{stdout}{stderr}"); + assert!(stdout.contains("1 succeeded, 0 failed"), "{stdout}"); + let Invoke { code, stdout, stderr } = invoke(&["info", &path("out/a.png"), "--compact"]); + assert_eq!(code, 0, "{stderr}"); + let info: Value = serde_json::from_str(&stdout).unwrap(); + assert_eq!((info["width"].as_u64(), info["height"].as_u64()), (Some(6), Some(8)), "the step after Deselect ran: {info}"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/apps/photocraft-cli/tests/it/main.rs b/apps/photocraft-cli/tests/it/main.rs new file mode 100644 index 0000000000..65092cdee4 --- /dev/null +++ b/apps/photocraft-cli/tests/it/main.rs @@ -0,0 +1,10 @@ +//! The crate's integration tests, linked into one test binary. + +mod cli; +mod man_and_completions; +mod orientation; + +/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid. +fn binary() -> std::ffi::OsString { + std::env::var_os("NEXTEST_BIN_EXE_photocraft_cli").unwrap_or_else(|| env!("CARGO_BIN_EXE_photocraft-cli").into()) +} diff --git a/apps/photocraft-cli/tests/it/man_and_completions.rs b/apps/photocraft-cli/tests/it/man_and_completions.rs new file mode 100644 index 0000000000..9c1e2aabbf --- /dev/null +++ b/apps/photocraft-cli/tests/it/man_and_completions.rs @@ -0,0 +1,55 @@ +//! The clap definition is consistent, and every shell's completions and every man page +//! (`examples/man-and-completions.rs`) can be generated from it. + +use clap::ValueEnum; +use clap_complete::Shell; + +#[test] +fn command_is_consistent() { + photocraft_cli::command().debug_assert(); +} + +#[test] +fn completions_generate_for_every_shell() { + for &shell in Shell::value_variants() { + let mut out = Vec::new(); + clap_complete::generate(shell, &mut photocraft_cli::command(), "photocraft-cli", &mut out); + assert!(!out.is_empty(), "{shell}"); + } +} + +#[test] +fn man_pages_render_for_the_command_and_every_subcommand() { + let cmd = photocraft_cli::command(); + for page in std::iter::once(&cmd).chain(cmd.get_subcommands()) { + let mut out = Vec::new(); + clap_mangen::Man::new(page.clone()).render(&mut out).unwrap(); + assert!(!out.is_empty(), "{}", page.get_name()); + } +} + +/// The example's two `generate_to` calls, repeated here, write the five completion files under the +/// names `packaging/linux/package.sh` and `packaging/freebsd/package.sh` install, `photocraft-cli.1` +/// and a page per subcommand, and no pages for the `help` subcommand. +#[test] +fn the_example_writes_the_files_the_packages_install() { + let dir = std::env::temp_dir().join(format!("pc-cli-{}-man-and-completions", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let mut cmd = photocraft_cli::command(); + for &shell in Shell::value_variants() { + clap_complete::generate_to(shell, &mut cmd, "photocraft-cli", &dir).unwrap(); + } + clap_mangen::generate_to(photocraft_cli::command(), &dir).unwrap(); + + let mut written: Vec = std::fs::read_dir(&dir).unwrap().map(|e| e.unwrap().file_name().into_string().unwrap()).collect(); + written.sort(); + let completions = ["photocraft-cli.bash", "_photocraft-cli", "photocraft-cli.fish", "photocraft-cli.elv", "_photocraft-cli.ps1"]; + let subcommands = ["convert", "info", "run", "batch", "droplet", "commands", "mcp", "serve"]; + let mut expected: Vec = completions.map(String::from).into(); + expected.push("photocraft-cli.1".into()); + expected.extend(subcommands.iter().map(|s| format!("photocraft-cli-{s}.1"))); + expected.sort(); + assert_eq!(written, expected); + std::fs::remove_dir_all(&dir).unwrap(); +} diff --git a/apps/photocraft-cli/tests/orientation.rs b/apps/photocraft-cli/tests/it/orientation.rs similarity index 94% rename from apps/photocraft-cli/tests/orientation.rs rename to apps/photocraft-cli/tests/it/orientation.rs index 101195581f..8e8da558c7 100644 --- a/apps/photocraft-cli/tests/orientation.rs +++ b/apps/photocraft-cli/tests/it/orientation.rs @@ -10,7 +10,7 @@ use photocraft_codecs::{ChannelLayout, DecodeOptions, EncodeOptions, Format, Ima use serde_json::Value; fn bin() -> Command { - Command::new(env!("CARGO_BIN_EXE_photocraft-cli")) + Command::new(crate::binary()) } fn tmp(name: &str) -> PathBuf { @@ -125,7 +125,8 @@ fn orientation_6_jpeg_converts_upright_and_rotates_back() { let (a, b) = (d.join("a.pcraft"), d.join("b.pcraft")); ok(bin().arg("convert").arg(&jpg).arg(&a)); ok(bin().arg("convert").arg(&a).arg(&b)); - // Everything but the file's own path and saved document name must match. + // Everything but the file's own path and saved document name (which the first snapshot is + // named after) must match. let info = |p: &Path| { let mut v = serde_json::from_str::(&ok(bin().arg("info").arg(p))).unwrap(); v.as_object_mut().unwrap().remove("file"); @@ -136,8 +137,12 @@ fn orientation_6_jpeg_converts_upright_and_rotates_back() { assert_eq!((ia["width"].as_u64(), ia["height"].as_u64()), (Some(16), Some(32))); assert_eq!(ia["name"], "a.pcraft"); assert_eq!(ib["name"], "b.pcraft"); - ia.as_object_mut().unwrap().remove("name"); - ib.as_object_mut().unwrap().remove("name"); + assert_eq!(ia["snapshots"][0]["name"], "a.pcraft"); + assert_eq!(ib["snapshots"][0]["name"], "b.pcraft"); + for v in [&mut ia, &mut ib] { + v.as_object_mut().unwrap().remove("name"); + v["snapshots"][0].as_object_mut().unwrap().remove("name"); + } assert_eq!(ia, ib); let (pa, pb) = (d.join("a.png"), d.join("b.png")); ok(bin().arg("convert").arg(&a).arg(&pa)); diff --git a/apps/photocraft-web-boot/Cargo.toml b/apps/photocraft-web-boot/Cargo.toml index dd12341af2..7443ead089 100644 --- a/apps/photocraft-web-boot/Cargo.toml +++ b/apps/photocraft-web-boot/Cargo.toml @@ -21,4 +21,4 @@ flate2 = { workspace = true } wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" js-sys = "0.3" -web-sys = { version = "0.3", features = ["Window", "Document", "Element", "Node", "Request", "RequestInit", "RequestCache", "Response", "ResponseInit", "Headers", "ReadableStream", "WritableStream", "TransformStream", "TransformStreamDefaultController", "Transformer", "StreamPipeOptions", "ReadableWritablePair", "console"] } +web-sys = { version = "0.3", features = ["Window", "Document", "Element", "Node", "Request", "RequestInit", "RequestCache", "Response", "ResponseInit", "Headers", "ReadableStream", "ReadableStreamDefaultReader", "WritableStream", "WritableStreamDefaultWriter", "TransformStream", "TransformStreamDefaultController", "Transformer", "StreamPipeOptions", "ReadableWritablePair", "console"] } diff --git a/apps/photocraft-web-boot/src/main.rs b/apps/photocraft-web-boot/src/main.rs index 56c2bf1517..c7dc50d9d5 100644 --- a/apps/photocraft-web-boot/src/main.rs +++ b/apps/photocraft-web-boot/src/main.rs @@ -8,7 +8,10 @@ //! one part, however large the app grows. //! //! The browser inflates the parts (`DecompressionStream`: Safari 16.4, Firefox 113, Chrome 80). -//! In one that can't, this module inflates them itself, a little slower. +//! In one that can't, this module inflates them itself, a little slower. Some servers recognise +//! the gzip in a part and send it with `Content-Encoding: gzip` (Apache's `mod_mime_magic` +//! labels it `x-gzip`), so the browser has inflated it already: the joined download is then +//! taken as it is, by its first bytes. //! //! A download that fails is tried again, the later tries past the browser's cache (a bad copy //! there would otherwise fail forever). When every try fails the page says so and offers a reload. @@ -36,6 +39,34 @@ fn parts(list: &str) -> Option> { (!parts.is_empty() && parts.iter().all(plain)).then_some(parts) } +/// What the joined parts hold, told by their first bytes. +#[cfg(any(target_arch = "wasm32", test))] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Payload { + /// The gzip packaging made, still to be inflated. + Gzip, + /// The app's WebAssembly: the server sent the gzip with a `Content-Encoding` the browser + /// already undid. + Wasm, +} + +/// Enough leading bytes to tell the payloads apart. +#[cfg(any(target_arch = "wasm32", test))] +const HEAD: usize = 4; + +/// What a download starting with `head` (at least `HEAD` bytes, or all there were) holds, or +/// `None` when it is neither, such as a web page or nothing at all. +#[cfg(any(target_arch = "wasm32", test))] +fn payload(head: &[u8]) -> Option { + if head.starts_with(&[0x1f, 0x8b]) { + Some(Payload::Gzip) + } else if head.starts_with(b"\0asm") { + Some(Payload::Wasm) + } else { + None + } +} + /// Gzip inflated a chunk at a time, for browsers that can't do it themselves. #[cfg(any(target_arch = "wasm32", test))] struct Inflater(flate2::write::GzDecoder>); @@ -69,11 +100,11 @@ mod boot { use wasm_bindgen::prelude::*; use wasm_bindgen_futures::{JsFuture, spawn_local}; use web_sys::{ - Headers, ReadableStream, ReadableWritablePair, RequestCache, RequestInit, Response, ResponseInit, StreamPipeOptions, TransformStream, - TransformStreamDefaultController, Transformer, WritableStream, + Headers, ReadableStream, ReadableStreamDefaultReader, ReadableWritablePair, RequestCache, RequestInit, Response, ResponseInit, StreamPipeOptions, + TransformStream, TransformStreamDefaultController, Transformer, WritableStream, WritableStreamDefaultWriter, }; - use super::Inflater; + use super::{HEAD, Inflater, Payload}; /// Where packaging puts the app, next to this module's own files. const PARTS: &str = "./app/parts.txt"; @@ -172,11 +203,71 @@ mod boot { Ok(compressed.pipe_through(&pair)) } + /// The next chunk `reader` gives, or `None` at the end of the stream. + async fn read(reader: &ReadableStreamDefaultReader) -> Result, JsValue> { + let result = JsFuture::from(reader.read()).await?; + if js_sys::Reflect::get(&result, &JsValue::from_str("done"))?.is_truthy() { + return Ok(None); + } + Ok(Some(js_sys::Reflect::get(&result, &JsValue::from_str("value"))?.dyn_into()?)) + } + + /// Writes `chunk` into `writer`. When nothing will read it any more (the app's loader gave up + /// on its stream), the download from `reader` is cancelled too, so it doesn't stay pending. + async fn write(chunk: &js_sys::Uint8Array, reader: &ReadableStreamDefaultReader, writer: &WritableStreamDefaultWriter) -> Result<(), JsValue> { + if let Err(e) = JsFuture::from(writer.write_with_chunk(chunk)).await { + let _ = reader.cancel_with_reason(&e); + return Err(e); + } + Ok(()) + } + + /// Writes the `held` chunks, then the rest of `reader`'s stream, into `writer`, then closes it. + async fn pass_on(held: Vec, reader: &ReadableStreamDefaultReader, writer: &WritableStreamDefaultWriter) -> Result<(), JsValue> { + for chunk in &held { + write(chunk, reader, writer).await?; + } + while let Some(chunk) = read(reader).await? { + write(&chunk, reader, writer).await?; + } + JsFuture::from(writer.close()).await?; + Ok(()) + } + + /// The app's WebAssembly from the `joined` parts: inflated, unless its first bytes show the + /// browser has already done that. + async fn unpacked(joined: &ReadableStream) -> Result { + let reader: ReadableStreamDefaultReader = joined.get_reader().unchecked_into(); + let (mut head, mut held) = (Vec::with_capacity(HEAD), Vec::new()); + while head.len() < HEAD { + let Some(chunk) = read(&reader).await? else { break }; + let wanted = u32::try_from(HEAD - head.len()).unwrap_or(u32::MAX).min(chunk.length()); + head.extend(chunk.subarray(0, wanted).to_vec()); + held.push(chunk); + } + let Some(payload) = super::payload(&head) else { + let e = JsValue::from_str("the download is not PhotoCraft's WebAssembly"); + // Stops the rest of the download, which nothing will read. + let _ = reader.cancel_with_reason(&e); + return Err(e); + }; + let rest = TransformStream::new()?; + let writer = rest.writable().get_writer()?; + spawn_local(async move { + if let Err(e) = pass_on(held, &reader, &writer).await { + let _ = writer.abort_with_reason(&e); + } + }); + match payload { + Payload::Gzip => inflated(&rest.readable()), + Payload::Wasm => Ok(rest.readable()), + } + } + async fn run(fresh: bool) -> Result<(), JsValue> { let list = JsFuture::from(fetch(PARTS, fresh).await?.text()?).await?.as_string().unwrap_or_default(); let parts = super::parts(&list).ok_or_else(|| JsValue::from_str("app/parts.txt is not a parts list"))?; let joined = TransformStream::new()?; - let wasm = inflated(&joined.readable())?; // Why the download stopped: the app's loader only reports that its stream broke. let cause = Rc::new(RefCell::new(None)); let failed = cause.clone(); @@ -187,6 +278,10 @@ mod boot { failed.replace(Some(e)); } }); + let wasm = match unpacked(&joined.readable()).await { + Ok(wasm) => wasm, + Err(e) => return Err(cause.take().unwrap_or(e)), + }; let headers = Headers::new()?; headers.set("Content-Type", "application/wasm")?; let init = ResponseInit::new(); @@ -258,7 +353,7 @@ fn main() { #[cfg(test)] mod tests { - use super::{Inflater, MARKER, parts}; + use super::{HEAD, Inflater, MARKER, Payload, parts, payload}; fn gzip(bytes: &[u8]) -> Vec { let mut z = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); @@ -317,6 +412,22 @@ mod tests { assert!(run(b"").is_err(), "nothing at all"); } + /// A server that recognises the gzip (Apache's `mod_mime_magic`) sends it with + /// `Content-Encoding: x-gzip`, and the browser inflates it on the way (#7639). The joined parts + /// are then the app's WebAssembly, which must start as it is, not be inflated a second time. + #[test] + fn the_first_bytes_tell_gzip_from_wasm_the_browser_already_inflated() { + let mut wasm = b"\0asm\x01\0\0\0".to_vec(); + wasm.extend(sample()); + let packed = gzip(&wasm); + assert_eq!(payload(&packed[..HEAD]), Some(Payload::Gzip), "as packaged"); + assert_eq!(payload(&wasm[..HEAD]), Some(Payload::Wasm), "inflated by the browser"); + assert_eq!(payload(&packed[..2]), Some(Payload::Gzip), "a download shorter than HEAD"); + assert_eq!(payload(b""), None, "a web page"); + assert_eq!(payload(b"\0as"), None, "a cut-off wasm header"); + assert_eq!(payload(b""), None, "nothing at all"); + } + #[test] fn a_parts_list_gives_its_names_in_order() { let list = format!("{MARKER}\nphotocraft-web-0123abcd.part-aa\r\n\n photocraft-web-0123abcd.part-ab \n"); diff --git a/apps/photocraft-web/Cargo.toml b/apps/photocraft-web/Cargo.toml index 786165b532..0176e0fe84 100644 --- a/apps/photocraft-web/Cargo.toml +++ b/apps/photocraft-web/Cargo.toml @@ -29,12 +29,12 @@ log = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] # `wgpu` pulls egui-wgpu's defaults: WebGPU plus the WebGL2 fallback. -eframe = { workspace = true, features = ["wgpu", "accesskit"] } +eframe = { workspace = true, features = ["wgpu", "accesskit", "clipboard"] } rfd = { workspace = true } wasm-bindgen = "0.2" wasm-bindgen-futures = "0.4" js-sys = "0.3" -web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "Storage", "Event", "EventTarget", "MouseEvent", "PointerEvent", "BeforeUnloadEvent", "AbortController", "AbortSignal", "DomException", "Response", "IdbFactory", "IdbOpenDbRequest", "IdbDatabase", "IdbRequest", "IdbTransaction", "IdbTransactionMode", "IdbObjectStore", "IdbCursor", "IdbCursorWithValue", "RequestInit", "Navigator"] } +web-sys = { version = "0.3", features = ["Window", "Document", "Element", "HtmlElement", "HtmlCanvasElement", "DomRect", "HtmlAnchorElement", "Blob", "BlobPropertyBag", "Url", "Location", "CssStyleDeclaration", "Storage", "Event", "EventTarget", "MouseEvent", "PointerEvent", "BeforeUnloadEvent", "AbortController", "AbortSignal", "DomException", "Response", "IdbFactory", "IdbOpenDbRequest", "IdbDatabase", "IdbRequest", "IdbTransaction", "IdbTransactionMode", "IdbObjectStore", "IdbCursor", "IdbCursorWithValue", "RequestInit", "RequestMode", "Navigator"] } [dev-dependencies] serde_json = { workspace = true } diff --git a/apps/photocraft-web/src/main.rs b/apps/photocraft-web/src/main.rs index 428143c880..0802a02ebf 100644 --- a/apps/photocraft-web/src/main.rs +++ b/apps/photocraft-web/src/main.rs @@ -6,10 +6,10 @@ //! //! Differences from the desktop app: //! - no TCP control server (browsers can't listen on sockets); -//! - File → Open uses the browser file picker; bytes arrive asynchronously through -//! `Services::inbox`; +//! - file pickers return browser file contents asynchronously through `Services::file_dialog`; +//! this includes File → Open and commands such as Place Embedded; //! - saving/exporting triggers a browser download; -//! - dropped files are read asynchronously by `web::WebShell` and delivered through the inbox. +//! - dropped files are read asynchronously by `web::WebShell` and delivered through `Services::inbox`. //! //! URL query flags: `?cpu` forces the CPU canvas path (same as `PHOTOCRAFT_CPU_CANVAS=1`); //! `?webgl` forces the WebGL2 backend instead of WebGPU. diff --git a/apps/photocraft-web/src/preset_bridge.rs b/apps/photocraft-web/src/preset_bridge.rs index a7445bbc14..a39fdfb4b4 100644 --- a/apps/photocraft-web/src/preset_bridge.rs +++ b/apps/photocraft-web/src/preset_bridge.rs @@ -9,8 +9,8 @@ use photocraft_engine::{ }; pub type Batch = BTreeMap>>; -/// Three sampled images per preset (tip, dual tip, texture), plus group/index/action records. -pub const MAX_RECORDS: usize = MAX_GROUPS + MAX_PRESETS_PER_GROUP * 3 + 2; +/// Three sampled images per preset (tip, dual tip, texture), plus group/index/action/pattern records. +pub const MAX_RECORDS: usize = MAX_GROUPS + MAX_PRESETS_PER_GROUP * 3 + 3; #[derive(Default)] struct State { @@ -45,6 +45,7 @@ pub fn record_limit(name: &str) -> Result { match name { INDEX_FILE => Ok(MAX_INDEX_BYTES), ACTIONS_FILE => Ok(MAX_ACTIONS_BYTES), + PATTERNS_FILE => Ok(MAX_PATTERNS_BYTES), _ if name.starts_with("tips/") && leaf.ends_with(".pctip") => Ok(MAX_TIP_BYTES), _ if !name.contains('/') && name.ends_with(".pcbrushes") => Ok(MAX_GROUP_BYTES), _ => Err("unknown preset record type".into()), diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rs index eb350d17eb..024888c65d 100644 --- a/apps/photocraft-web/src/web.rs +++ b/apps/photocraft-web/src/web.rs @@ -1,8 +1,10 @@ //! The browser shell: web `Services`, drag-and-drop, and the eframe web runner. +use std::cell::Cell; use std::collections::{HashMap, HashSet}; +use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::{Arc, Mutex, PoisonError}; use photocraft_codecs::{ChannelLayout, EncodeOptions, Image}; use photocraft_doc::Document; @@ -15,11 +17,12 @@ use wasm_bindgen::JsCast as _; type Inbox = Arc)>>>; /// Everything File › Open reads: PhotoCraft, Photoshop, OpenRaster, Krita and Affinity documents, flat images, and -/// Photoshop brushes (.abr), gradients (.grd) and swatches (.aco, .ase), which go to the preset libraries. +/// Photoshop brushes (.abr), gradients (.grd), layer styles (.asl) and swatches (.aco, .act, .ase), which go to the preset libraries. const OPEN_EXTS: &[&str] = &[ - "pcraft", "pdn", "ora", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr", - "pbm", "pgm", "ppm", "pam", "pfm", "dds", "fits", "fit", "fts", "dpx", "cin", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", - "orf", "rw2", "raf", "abr", "grd", "svg", "svgz", "aco", "ase", "af", "afdesign", "afphoto", "afpub", + "pcraft", "pdn", "ora", "xcf", "xcfgz", "gz", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", + "qoi", "exr", "hdr", "pbm", "pgm", "ppm", "pam", "pfm", "dds", "fits", "fit", "fts", "dpx", "cin", "dcm", "dicom", "dc3", "iff", "lbm", "ilbm", "heic", + "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf", "rw2", "raf", "abr", "grd", "asl", "svg", "svgz", "aco", "act", "ase", "af", + "afdesign", "afphoto", "afpub", ]; const SVG_EXTS: &[&str] = &["svg", "svgz"]; const CANVAS_ID: &str = "photocraft_canvas"; @@ -77,13 +80,16 @@ pub fn start() { canvas, options, Box::new(move |cc| { + PhotocraftApp::install_type_reset_hooks(&cc.egui_ctx); PhotocraftApp::setup_context(&cc.egui_ctx, ThemeKind::Pro); let inbox: Inbox = Arc::default(); + let place_inbox: Inbox = Arc::default(); let served: Served = Arc::default(); load_font_manifest(served.clone(), cc.egui_ctx.clone()); let (session, warnings) = crate::preset_bridge::session(presets.clone()); preset_warnings.extend(warnings); - let mut app = PhotocraftApp::new(session, services(inbox.clone())); + let drag = listen_drag(&pen_target); + let mut app = PhotocraftApp::new(session, services(inbox.clone(), place_inbox.clone(), drag_pos_service(pen_target.clone(), drag))); if !preset_warnings.is_empty() { photocraft_ui_egui::notices::post(&mut app, i18n::t("Some brush presets could not be loaded"), preset_warnings, true, None); } @@ -97,7 +103,7 @@ pub fn start() { } let unsaved = Arc::new(AtomicBool::new(false)); guard_unload(unsaved.clone()); - Ok(Box::new(WebShell { app, inbox, unsaved, served, database, presets })) + Ok(Box::new(WebShell { app, inbox, place_inbox, unsaved, served, database, presets, ui_language: "" })) }), ) .await; @@ -140,6 +146,38 @@ fn listen_pen(target: &web_sys::HtmlCanvasElement, feed: photocraft_ui_egui::sty } } +/// The pointer in a drag over the page, in CSS pixels relative to the viewport (`clientX`, +/// `clientY`): the last `dragover` or `drop` event's. +type DragPos = Rc>>; + +/// Follows files dragged over the canvas. eframe's drop events carry no pointer position and the +/// page gets no pointer events during a drag, so this is where a drop lands: on the document's +/// canvas it places the files, elsewhere it opens them (#5749). +fn listen_drag(target: &web_sys::HtmlCanvasElement) -> DragPos { + use wasm_bindgen::closure::Closure; + let pos = DragPos::default(); + for kind in ["dragover", "drop"] { + let pos = pos.clone(); + // A DragEvent is a MouseEvent: only its client position is read. + let cb = Closure::::new(move |e: web_sys::MouseEvent| pos.set(Some((e.client_x() as f32, e.client_y() as f32)))); + if target.add_event_listener_with_callback(kind, cb.as_ref().unchecked_ref()).is_ok() { + cb.forget(); + } + } + pos +} + +/// `Services::cursor_pos` for the web: the last drag position in egui points, mapped as eframe +/// maps pointer events (relative to the canvas, divided by the zoom factor). +fn drag_pos_service(canvas: web_sys::HtmlCanvasElement, pos: DragPos) -> photocraft_ui_egui::CursorPosFn { + Box::new(move |ctx: &egui::Context| { + let (x, y) = pos.get()?; + let rect = canvas.get_bounding_client_rect(); + let zoom = ctx.zoom_factor(); + (zoom.is_finite() && zoom > 0.0).then(|| egui::pos2((x - rect.left() as f32) / zoom, (y - rect.top() as f32) / zoom)) + }) +} + /// Closing or reloading the tab while a document has unsaved changes asks first, as closing the /// desktop window does: the browser shows its own "Leave site?" prompt (#1380). `unsaved` is /// refreshed every frame by [`WebShell`]. @@ -181,12 +219,15 @@ fn load_font_manifest(served: Served, ctx: egui::Context) { files.entry(f.family.clone()).or_default().push((f.file.clone(), f.integrity.clone())); } log::info!("photocraft-web: {} served font families", files.len()); - served.lock().unwrap_or_else(|e| e.into_inner()).files = files; + served.lock().unwrap_or_else(PoisonError::into_inner).files = files; // The families for the font menus, and the script fallbacks (the manifest's `scripts`). served_fonts::add_fonts(&fonts); // Fetch now the fallbacks of the scripts the browser's languages use (an Arabic reader gets // the Arabic font before typing); the rest is fetched when text or a font menu needs it. - served_fonts::request_for_languages(browser_languages().iter().map(String::as_str)); + // Also the interface language's: `serve_fonts` asked for it once already, maybe before + // this manifest arrived, when no fallback was registered yet. + let ui_language = photocraft_ui_egui::i18n::current().code(); + served_fonts::request_for_languages(browser_languages().iter().map(String::as_str).chain([ui_language])); ctx.request_repaint(); }); } @@ -196,20 +237,21 @@ fn browser_languages() -> Vec { web_sys::window().map(|w| w.navigator().languages().iter().filter_map(|l| l.as_string()).collect()).unwrap_or_default() } -/// GET `url` (relative to the page) and read the whole body. With `integrity` (a Subresource -/// Integrity value), the browser rejects a body whose hash differs. +/// GET `url` (a relative path on the same origin) and read the whole body. With `integrity` (a +/// Subresource Integrity value), the browser rejects a body whose hash differs. async fn fetch_bytes(url: &str, integrity: Option<&str>) -> Result, String> { use wasm_bindgen_futures::JsFuture; + if !served_fonts::is_relative_path(url) { + return Err("url must be a relative path".into()); + } let js = |e: wasm_bindgen::JsValue| format!("{e:?}"); let window = web_sys::window().ok_or("no window")?; - let request = match integrity { - Some(sri) => { - let init = web_sys::RequestInit::new(); - init.set_integrity(sri); - window.fetch_with_str_and_init(url, &init) - } - None => window.fetch_with_str(url), - }; + let init = web_sys::RequestInit::new(); + init.set_mode(web_sys::RequestMode::SameOrigin); + if let Some(sri) = integrity { + init.set_integrity(sri); + } + let request = window.fetch_with_str_and_init(url, &init); let resp: web_sys::Response = JsFuture::from(request).await.map_err(js)?.dyn_into().map_err(|_| "not a Response")?; if !resp.ok() { return Err(format!("HTTP {}", resp.status())); @@ -219,15 +261,20 @@ async fn fetch_bytes(url: &str, integrity: Option<&str>) -> Result, Stri } /// Wraps the app to read dropped files asynchronously (browsers can't read them synchronously, -/// so the app's own drop path can't handle them) and feed them through the inbox. +/// so the app's own drop path can't handle them) and feed them through the inboxes. struct WebShell { app: PhotocraftApp, + /// Dropped files to open as documents. inbox: Inbox, + /// Dropped files to place on the active document's canvas. + place_inbox: Inbox, /// Read by the `beforeunload` listener ([`guard_unload`]). unsaved: Arc, served: Served, database: Option, presets: crate::preset_bridge::Bridge, + /// The interface language whose served script font was last asked for. + ui_language: &'static str, } impl WebShell { @@ -252,9 +299,15 @@ impl WebShell { /// Fetches the served families asked for (picked in a font menu, or needed by a layout) and /// installs the files that arrived. fn serve_fonts(&mut self, ctx: &egui::Context) { + // The interface language's script font draws its labels (Arabic, Japanese…). + let ui_language = photocraft_ui_egui::i18n::current().code(); + if ui_language != self.ui_language { + self.ui_language = ui_language; + served_fonts::request_for_languages([ui_language]); + } let requested = served_fonts::take_requests(); let (fetch, arrived) = { - let mut s = self.served.lock().unwrap_or_else(|e| e.into_inner()); + let mut s = self.served.lock().unwrap_or_else(PoisonError::into_inner); let mut fetch = Vec::new(); for family in requested { if let Some(files) = s.files.get(&family).cloned() @@ -271,37 +324,57 @@ impl WebShell { wasm_bindgen_futures::spawn_local(async move { match fetch_bytes(&file, integrity.as_deref()).await { Ok(bytes) => { - served.lock().unwrap_or_else(|e| e.into_inner()).arrived.push((family, bytes)); + served.lock().unwrap_or_else(PoisonError::into_inner).arrived.push((family, bytes)); ctx.request_repaint(); } Err(e) => log::error!("couldn't fetch served font {file}: {e}"), } }); } - served_fonts::install(&mut self.app, arrived); + served_fonts::install(&mut self.app, ctx, arrived); } } impl eframe::App for WebShell { + fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) { + // A tap reaches eframe as Pointer Events and Touch Events: keep one press per tap, or the + // twin press closes the menu the first one opened (#2095). + photocraft_ui_egui::touch_twins::drop_twins(ctx, raw_input); + // The browser build has no TCP control server, so `inject_synthetic` finds an empty + // queue, but the clipboard key mapping (`Event::PasteImage`), legacy pinch, pen-hold and + // titlebar normalization live in the app hook and must run here too. + eframe::App::raw_input_hook(&mut self.app, ctx, raw_input); + } + fn logic(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { let dropped = ctx.input_mut(|i| std::mem::take(&mut i.raw.dropped_files)); - for f in dropped { - let inbox = self.inbox.clone(); + if !dropped.is_empty() { + let names: Vec = + dropped.iter().map(|f| f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into())).collect(); + // Decided now, where the drop landed: on the document's canvas the files are placed + // as layers (as on the desktop), elsewhere opened. + let places = self.app.dropped_on_canvas(ctx, &names); + let files: Vec<_> = dropped.into_iter().zip(names).zip(places).collect(); + let (inbox, place_inbox) = (self.inbox.clone(), self.place_inbox.clone()); let ctx = ctx.clone(); + // One after another, so they open or place in the order they were dropped. wasm_bindgen_futures::spawn_local(async move { - let name = f.path().file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| "dropped".into()); - match f.bytes_async().await { - Ok(bytes) => { - inbox.lock().unwrap_or_else(|e| e.into_inner()).push((name, bytes)); - ctx.request_repaint(); + for ((f, name), place) in files { + match f.bytes_async().await { + Ok(bytes) => { + let queue = if place { &place_inbox } else { &inbox }; + queue.lock().unwrap_or_else(PoisonError::into_inner).push((name, bytes)); + ctx.request_repaint(); + } + Err(e) => log::error!("couldn't read dropped file {name}: {e}"), } - Err(e) => log::error!("couldn't read dropped file {name}: {e}"), } }); } self.serve_fonts(ctx); - self.app.logic(ctx, frame); + self.app.logic_frame(ctx, frame); self.save_presets(ctx); + self.app.finish_type_reset_logic(ctx); } fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { @@ -328,16 +401,17 @@ impl eframe::App for WebShell { }); }); } - self.app.ui(ui, frame); + self.app.ui_frame(ui, frame); self.save_presets(ui.ctx()); + self.app.finish_type_reset_pass(ui.ctx()); } } -fn services(inbox: Inbox) -> Services { +fn services(inbox: Inbox, place_inbox: Inbox, cursor_pos: photocraft_ui_egui::CursorPosFn) -> Services { Services { screen_pick: screen_color_service(), - import: Some(Box::new(|name: &str, bytes: &[u8], max_svg_group_depth: usize| { - photocraft_io::import_with_svg_group_depth(name, bytes, max_svg_group_depth).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string()) + import: Some(Box::new(|name: &str, bytes: &[u8], opts: &photocraft_io::ImportOptions| { + photocraft_io::import_with_options(name, bytes, opts).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string()) })), export: Some(Box::new(|doc: &Document, path: &str, settings: &photocraft_ui_egui::ExportSettings| { photocraft_io::export(doc, path, &settings.io_options()).map(|r| (r.bytes, r.warnings)).map_err(|e| e.to_string()) @@ -379,11 +453,15 @@ fn services(inbox: Inbox) -> Services { } })), write: Some(Box::new(|path: &str, bytes: &[u8]| download(path, bytes))), + // A download may be cancelled and never reports back: it doesn't save the document. + write_unconfirmed: true, encode_png: Some(Box::new(|w, h, rgba| { let img = Image::from_u8(w, h, ChannelLayout::Rgba, rgba.to_vec()).map_err(|e| e.to_string())?; photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &EncodeOptions::default()).map_err(|e| e.to_string()) })), inbox: Some(inbox), + place_inbox: Some(place_inbox), + cursor_pos: Some(cursor_pos), // Preferences live in the browser's localStorage. load_prefs: Some(Box::new(|| local_storage()?.get_item(PREFS_KEY).ok().flatten())), save_prefs: Some(Box::new(|text: &str| local_storage().ok_or("no localStorage")?.set_item(PREFS_KEY, text).map_err(|e| format!("{e:?}")))), diff --git a/apps/photocraft/Cargo.toml b/apps/photocraft/Cargo.toml index 8b7c832bb2..5de916c250 100644 --- a/apps/photocraft/Cargo.toml +++ b/apps/photocraft/Cargo.toml @@ -29,17 +29,23 @@ photocraft-automation = { workspace = true } photocraft-ui-egui = { workspace = true } # Pen tablet pressure/tilt on macOS and X11 (the workspace's one isolated unsafe helper crate). photocraft-tablet = { workspace = true } -eframe = { workspace = true, features = ["wgpu", "accesskit", "wayland", "x11", "persistence"] } +eframe = { workspace = true, features = ["wgpu", "accesskit", "wayland", "x11", "persistence", "clipboard"] } egui = { workspace = true } +serde = { workspace = true } serde_json = { workspace = true } log = { workspace = true } +chrono = { workspace = true } +clap = { workspace = true } +thiserror = { workspace = true } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] +craft-update = { workspace = true } rfd = { workspace = true } -arboard = { version = "3.6", features = ["wayland-data-control"] } +arboard = { version = "3.7", features = ["wayland-data-control"] } open = "5" # Reads the saved window layout (eframe persistence) to drop values that would abort startup. ron = "0.12" +# Background release checks; HTTPS with Rust TLS, no async runtime. [target.'cfg(target_os = "macos")'.dependencies] # Safe CoreGraphics bindings, compatible with the existing macOS 11 deployment target. @@ -50,14 +56,15 @@ core-graphics2 = { version = "=0.6.1", default-features = false, features = ["di # safe API. Pinned exactly: review the diff before bumping. fmv-macos-events = "=0.1.0" # Safe AppKit calls only: turn off AppKit's title bar dragging (mac_window.rs) and find where the -# pointer is when files are dropped (cursor.rs; winit 0.30 drops carry no position). Already in +# pointer is when files are dropped (cursor.rs; winit 0.30 drops carry no position), and play the +# system alert sound for Beep When Done (services.rs, NSBeep). Already in # Cargo.lock (winit, photocraft-tablet); pinned exactly to the same versions. -objc2 = "=0.6.4" +objc2 = "=0.6.5" objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSArray", "NSEnumerator", "NSGeometry"] } -objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSApplicationScripting", "NSEvent", "NSResponder", "NSView", "NSWindow"] } +objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSApplicationScripting", "NSEvent", "NSGraphics", "NSResponder", "NSView", "NSWindow"] } # The macOS menu bar (mac_menu.rs): a standalone native menu crate (from the tauri-apps org, but # not Tauri: no webview, no JS), AppKit menus behind a safe API (no GTK with default features off). Pinned exactly: review the diff before bumping. -muda = { version = "=0.21.1", default-features = false } +muda = { version = "=0.21.2", default-features = false } # Quit with `terminate:` from the main queue, outside winit's event handler (mac_window.rs). # Already in Cargo.lock (winit, objc2-app-kit); pinned exactly to the same version. dispatch2 = { version = "=0.3.1", default-features = false, features = ["std"] } @@ -65,6 +72,8 @@ dispatch2 = { version = "=0.3.1", default-features = false, features = ["std"] } # Drop position on X11 (`cursor.rs`): pure-Rust X client, already in Cargo.lock; pinned exactly. [target.'cfg(target_os = "linux")'.dependencies] photocraft-cms = { workspace = true } +# The print portal's PDF: a private (0600) temporary file, removed when the dialog is done. +tempfile = "3" x11rb = { version = "=0.13.2", default-features = false, features = ["image", "randr", "xkb"] } # Clipboard images in types other than PNG on Wayland (`services.rs`, #2546): arboard asks for # image/png only. Already in Cargo.lock (arboard's wayland-data-control); pinned exactly. diff --git a/apps/photocraft/build.rs b/apps/photocraft/build.rs index cbba622589..48abc9f1a5 100644 --- a/apps/photocraft/build.rs +++ b/apps/photocraft/build.rs @@ -11,6 +11,14 @@ fn main() { if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") { return; } + // Printing to PDF (winprint's `PdfCreateRenderer`) imports Windows.Data.Pdf.dll, which + // stripped Windows images (NTLite, some N/LTSC builds) lack. A load-time import stops the exe + // from starting at all ("windows.data.pdf.dll was not found", #5884), so load it on first use; + // the print command checks for it first and reports an error instead. + if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { + println!("cargo:rustc-link-arg-bins=/DELAYLOAD:windows.data.pdf.dll"); + println!("cargo:rustc-link-arg-bins=delayimp.lib"); + } let mut res = winresource::WindowsResource::new(); res.set_icon("../../assets/app-icon/photocraft.ico") .set("ProductName", "PhotoCraft") diff --git a/apps/photocraft/src/app_dirs.rs b/apps/photocraft/src/app_dirs.rs index 2897bebdb0..4405608842 100644 --- a/apps/photocraft/src/app_dirs.rs +++ b/apps/photocraft/src/app_dirs.rs @@ -9,6 +9,9 @@ //! folder can't be written (read-only medium, Program Files), the app warns and falls back. //! 3. The platform convention: macOS `~/Library/Application Support/Photocraft`, Windows //! `%APPDATA%\Photocraft`, Linux `$XDG_CONFIG_HOME/photocraft` or `~/.config/photocraft`. +//! +//! One exception: the platform convention keeps crash-recovery autosaves out of what is synced +//! with the settings: the Windows roaming profile and the Linux config folder ([`recovery_dir`]). use std::ffi::OsString; use std::path::{Path, PathBuf}; @@ -60,6 +63,396 @@ pub fn config_dir() -> Option { current().dir.clone() } +/// The crash-recovery folder's name. +const RECOVERY: &str = "Recovery"; + +/// Where crash-recovery autosaves live, with any left in the settings directory by an earlier +/// version moved there first ([`adopt_legacy_recovery`]). Call it once, at startup. +pub fn recovery_dir() -> Option { + let data = current(); + let dir = resolve_recovery(data, |k| std::env::var_os(k))?; + Some(match data.dir.as_ref().map(|d| d.join(RECOVERY)) { + Some(legacy) => adopt_legacy_recovery(&legacy, &dir), + None => dir, + }) +} + +/// The recovery folder for `data`. Autosaves hold whole documents and are rewritten every few +/// minutes, so with the platform convention they stay out of what is synced with the settings: +/// - Windows (#5648): the machine-local `%LOCALAPPDATA%\Photocraft\Recovery`; the roaming +/// `%APPDATA%` may be synced at sign-in and sign-out or capped by policy. +/// - Linux and other XDG systems (#6612): `$XDG_STATE_HOME/photocraft/Recovery`, by default +/// `~/.local/state/photocraft/Recovery`, where the XDG Base Directory spec puts state that +/// should outlive a restart but isn't configuration; dotfile managers and config sync often copy +/// `~/.config` whole. As the spec asks, a relative `XDG_STATE_HOME` is ignored. +/// - macOS: `~/Library/Application Support/Photocraft/Recovery` as before. Apple's guidelines put +/// app-created data that can't be regenerated there; `~/Library/Caches` may be purged by the +/// system. +/// +/// They are never in a temp or cache folder, which cleanup tools may empty before a crashed +/// document is recovered. The override and portable folders keep everything in one place. +pub fn resolve_recovery(data: &DataDir, env: impl Fn(&str) -> Option) -> Option { + if data.mode == Mode::Platform { + if cfg!(windows) { + if let Some(local) = env("LOCALAPPDATA").filter(|l| !l.is_empty()) { + return Some(PathBuf::from(local).join("Photocraft").join(RECOVERY)); + } + } else if !cfg!(target_os = "macos") { + let state = env("XDG_STATE_HOME") + .map(PathBuf::from) + .filter(|s| s.is_absolute()) + .or_else(|| env("HOME").filter(|h| !h.is_empty()).map(|h| PathBuf::from(h).join(".local").join("state"))); + if let Some(state) = state { + return Some(state.join("photocraft").join(RECOVERY)); + } + } + } + data.dir.as_ref().map(|d| d.join(RECOVERY)) +} + +/// The lock file, beside the recovery folder, that keeps two PhotoCraft processes starting at +/// once (`--new-instance`, control launches) from moving the same autosaves. +const MIGRATION_LOCK: &str = "recovery-migration.lock"; + +/// How long a launch waits for another one's move before going ahead without moving anything. +const MIGRATION_WAIT: std::time::Duration = std::time::Duration::from_secs(5); + +/// Suffix of a copy staged for publishing ([`stage_copy`]): disposable, the original still exists. +const STAGED: &str = ".moving"; + +/// Suffix of an original set aside while it is copied to another volume ([`move_across`]): it +/// may be the only copy, so it is put back, never thrown away, if the move doesn't finish. +const HELD: &str = ".held"; + +/// Move autosaves from `legacy` (the recovery folder of versions before #5648) into `dir`, so a +/// crash just before an update is still offered for recovery. Returns the folder this process +/// uses for its recovery store. That is `legacy` only when `dir` doesn't exist and no process can +/// be moving autosaves (the move failed, or its lock can't be made), so nothing deletes what is +/// written there. A launch that finds another one moving uses `dir`, which the move never +/// replaces or removes. See [`adopt_locked`] for how they move. +pub fn adopt_legacy_recovery(legacy: &Path, dir: &Path) -> PathBuf { + adopt_legacy_recovery_waiting(legacy, dir, MIGRATION_WAIT) +} + +/// [`adopt_legacy_recovery`], waiting up to `wait` for another process's move. +fn adopt_legacy_recovery_waiting(legacy: &Path, dir: &Path, wait: std::time::Duration) -> PathBuf { + let held_aside = || legacy.parent().is_some_and(|p| !side_entries(p, legacy, HELD).is_empty()); + if legacy == dir || (!legacy.is_dir() && !held_aside()) { + return dir.to_path_buf(); + } + // An empty old folder has nothing to move (`remove_dir` only removes an empty one). + if !held_aside() && std::fs::remove_dir(legacy).is_ok() { + return dir.to_path_buf(); + } + let unmoved = || if dir.exists() || !legacy.is_dir() { dir.to_path_buf() } else { legacy.to_path_buf() }; + let Some(parent) = dir.parent() else { return unmoved() }; + let lock = + std::fs::create_dir_all(parent).and_then(|()| std::fs::OpenOptions::new().create(true).truncate(false).write(true).open(parent.join(MIGRATION_LOCK))); + let Ok(lock) = lock else { + // No lock file means no other process can be moving: the old folder is safe to use. + log::warn!("couldn't open the recovery move lock in {}; autosaves stay where they are", parent.display()); + return unmoved(); + }; + let deadline = std::time::Instant::now() + wait; + loop { + match lock.try_lock() { + Ok(()) => break, + Err(std::fs::TryLockError::WouldBlock) if std::time::Instant::now() < deadline => std::thread::sleep(std::time::Duration::from_millis(50)), + Err(std::fs::TryLockError::WouldBlock) => { + // Another process is moving and may yet remove the old folder: never write there. + log::info!("another PhotoCraft is moving crash-recovery autosaves; using {}", dir.display()); + return dir.to_path_buf(); + } + Err(std::fs::TryLockError::Error(e)) => { + // Locking fails for every process here alike, so none can be moving. + log::warn!("couldn't lock {}: {e}; autosaves stay where they are", parent.join(MIGRATION_LOCK).display()); + return unmoved(); + } + } + } + // The lock file stays (removing it would race a process about to lock it); the OS releases + // the lock when `lock` is dropped or the process ends. + let used = adopt_locked(legacy, dir); + if used == legacy { + // This process writes its autosaves to the old folder: it keeps the lock for as long as it + // runs, so no other launch moves (and removes) the folder under it. + KEPT_LOCKS.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(lock); + } + used +} + +/// Migration locks of the old folders this process uses (see [`adopt_legacy_recovery`]), kept +/// until it ends. +static KEPT_LOCKS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + +/// The move itself, under the migration lock. First, what a process that ended mid-move left is +/// tidied: originals it set aside are put back, copies it staged are removed. An empty `legacy` +/// is then just removed. Otherwise the whole folder moves if `dir` doesn't exist yet; when both +/// exist, each autosave (`.pcraft` bundle and `.json` sidecar, see +/// `photocraft_format::autosave`) moves as a whole, bundle first, unless `dir` already has either +/// part, and one that stays is left in `legacy` untouched. Every move is a [`move_path`]. +fn adopt_locked(legacy: &Path, dir: &Path) -> PathBuf { + if let Some(parent) = legacy.parent() { + for held in side_entries(parent, legacy, HELD) { + put_back(&held, legacy); + } + } + // Autosave parts held aside inside the old folder by a per-autosave move. + let held_parts: Vec<(PathBuf, String)> = std::fs::read_dir(legacy) + .into_iter() + .flatten() + .flatten() + .filter_map(|e| e.file_name().to_str().and_then(|n| side_of(n, HELD)).map(|orig| (e.path(), orig.to_owned()))) + .collect(); + for (held, original) in held_parts { + put_back(&held, &legacy.join(original)); + } + for folder in [dir.parent(), Some(dir)].into_iter().flatten() { + remove_stale_staging(folder); + } + // Another process may have finished the move while this one waited. + if !legacy.is_dir() { + return dir.to_path_buf(); + } + if std::fs::remove_dir(legacy).is_ok() { + return dir.to_path_buf(); + } + if !dir.exists() { + // The common case: the whole folder moves at once. + return match move_path(legacy, dir) { + Ok(()) => { + log::info!("moved crash-recovery autosaves from {} to {}", legacy.display(), dir.display()); + dir.to_path_buf() + } + Err(e) => { + log::warn!("couldn't move crash-recovery autosaves from {} to {} ({e}); using the old folder", legacy.display(), dir.display()); + if legacy.is_dir() { legacy.to_path_buf() } else { dir.to_path_buf() } + } + }; + } + move_entries(legacy, dir); + // Only an emptied folder goes; anything that couldn't move stays for a later launch. + let _ = std::fs::remove_dir(legacy); + dir.to_path_buf() +} + +/// Move each autosave of `from` into `to` (both folders): bundle first, then sidecar, skipped if +/// `to` has either part. Temp files of an interrupted write (`photocraft_format::atomic`) go +/// along too, for the recovery store's startup sweep there. +fn move_entries(from: &Path, to: &Path) { + let Ok(entries) = std::fs::read_dir(from) else { return }; + let names: Vec = entries.flatten().filter_map(|e| e.file_name().to_str().map(str::to_owned)).collect(); + let keys: std::collections::BTreeSet<&str> = names.iter().filter_map(|n| n.strip_suffix(".pcraft").or_else(|| n.strip_suffix(".json"))).collect(); + for key in keys { + let parts = [format!("{key}.pcraft"), format!("{key}.json")]; + if parts.iter().any(|p| std::fs::symlink_metadata(to.join(p)).is_ok()) { + continue; + } + // A bundle that moves without its sidecar is still listed (`list_recovery` reads a + // committed bundle's manifest); a sidecar must never move without its bundle. + for part in &parts { + let src = from.join(part); + if std::fs::symlink_metadata(&src).is_ok() + && let Err(e) = move_path(&src, &to.join(part)) + { + log::warn!("couldn't move {} to {}: {e}", src.display(), to.display()); + break; + } + } + } + for name in names.iter().filter(|n| n.ends_with(".pcsave.tmp")) { + let _ = move_path(&from.join(name), &to.join(name)); + } +} + +/// How deep [`copy_tree`] follows folders: an autosave bundle is a few levels deep. +const MAX_COPY_DEPTH: u32 = 16; + +fn already_exists(path: &Path) -> std::io::Error { + std::io::Error::new(std::io::ErrorKind::AlreadyExists, format!("{} already exists", path.display())) +} + +/// A path beside `of`, named `..-`, that no other call uses. +fn side_path(of: &Path, suffix: &str) -> std::io::Result { + static NEXT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); + let parent = of.parent().ok_or_else(|| std::io::Error::other(format!("{} has no parent folder", of.display())))?; + let name = of.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + Ok(parent.join(format!(".{name}.{}-{}{suffix}", std::process::id(), NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed)))) +} + +/// The name `side` was made from, if it is a [`side_path`] name with `suffix`. +fn side_of<'a>(side: &'a str, suffix: &str) -> Option<&'a str> { + let rest = side.strip_prefix('.')?.strip_suffix(suffix)?; + let (name, ids) = rest.rsplit_once('.')?; + let (pid, n) = ids.split_once('-')?; + let digits = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()); + (!name.is_empty() && digits(pid) && digits(n)).then_some(name) +} + +/// The side paths with `suffix` in `folder` made from `of`. +fn side_entries(folder: &Path, of: &Path, suffix: &str) -> Vec { + let Some(name) = of.file_name().and_then(|n| n.to_str()) else { return Vec::new() }; + std::fs::read_dir(folder) + .into_iter() + .flatten() + .flatten() + .filter(|e| e.file_name().to_str().and_then(|n| side_of(n, suffix)) == Some(name)) + .map(|e| e.path()) + .collect() +} + +/// Move the file or folder `from` to `to` without ever replacing anything at `to`. On one volume +/// a folder is renamed (Windows refuses an existing target; elsewhere only an empty folder could +/// be replaced) and a file is hard-linked, which fails on an existing target, and then unlinked +/// at `from`. Otherwise (another volume, e.g. a roaming profile redirected to a network share +/// while `%LOCALAPPDATA%` stays local) it is copied ([`move_across`]). +fn move_path(from: &Path, to: &Path) -> std::io::Result<()> { + if std::fs::symlink_metadata(to).is_ok() { + return Err(already_exists(to)); + } + let meta = std::fs::symlink_metadata(from)?; + let moved = if meta.is_dir() { std::fs::rename(from, to) } else { std::fs::hard_link(from, to).and_then(|()| std::fs::remove_file(from)) }; + match moved { + Ok(()) => Ok(()), + Err(e) if matches!(e.kind(), std::io::ErrorKind::AlreadyExists | std::io::ErrorKind::DirectoryNotEmpty) => Err(e), + Err(_) => move_across(from, to), + } +} + +/// Move `from` to `to` by copying. `from` is first renamed aside on its own volume +/// (`..-.held`, [`hold`]), so a process still writing to the old path (another +/// launch that couldn't take the lock, an older PhotoCraft) starts a new `from` there instead of +/// adding to what is copied, and nothing it writes is removed. The held original is copied to a +/// staged path ([`stage_copy`]), published without replacing anything ([`publish`]), and only +/// then removed. If the copy or publish fails, the held original is put back ([`put_back`]). +fn move_across(from: &Path, to: &Path) -> std::io::Result<()> { + let held = hold(from)?; + match stage_copy(&held, to).and_then(|staged| publish(&staged, to)) { + Ok(()) => { + let removed = if std::fs::symlink_metadata(&held)?.is_dir() { std::fs::remove_dir_all(&held) } else { std::fs::remove_file(&held) }; + if let Err(e) = removed { + log::warn!("copied {} to {} but couldn't remove {}: {e}", from.display(), to.display(), held.display()); + } + Ok(()) + } + Err(e) => { + put_back(&held, from); + Err(e) + } + } +} + +/// Rename `from` aside on its own volume (see [`move_across`]); returns where it is now. +fn hold(from: &Path) -> std::io::Result { + let held = side_path(from, HELD)?; + std::fs::rename(from, &held)?; + Ok(held) +} + +/// Return the held original `held` to `original`. If `original` has been made again meanwhile (a +/// writer that kept using the old path), the held folder's entries move into it without replacing +/// anything ([`move_entries`]); whatever can't move stays held, logged, and is tried again under +/// the next move's lock ([`adopt_locked`] puts back what a process that ended mid-move held). +/// Nothing held is ever removed with data in it. +fn put_back(held: &Path, original: &Path) { + if std::fs::symlink_metadata(original).is_err() && std::fs::rename(held, original).is_ok() { + return; + } + if held.is_dir() && original.is_dir() { + move_entries(held, original); + if std::fs::remove_dir(held).is_ok() { + return; + } + } + log::warn!("{} is kept aside: {} exists", held.display(), original.display()); +} + +/// Copy `from` to a staging path beside `to` that this call creates exclusively, so nothing +/// another process made is ever written to or cleaned up. On failure the staged copy is removed. +fn stage_copy(from: &Path, to: &Path) -> std::io::Result { + let staged = side_path(to, STAGED)?; + let is_dir = std::fs::symlink_metadata(from)?.is_dir(); + let copied = if is_dir { copy_tree(from, &staged, 0) } else { copy_file(from, &staged) }; + if let Err(e) = copied { + // `copy_tree` and `copy_file` create `staged` exclusively, and everything under a + // fresh folder is new: `AlreadyExists` means `staged` was someone else's to keep. + if e.kind() != std::io::ErrorKind::AlreadyExists { + let _ = if is_dir { std::fs::remove_dir_all(&staged) } else { std::fs::remove_file(&staged) }; + } + return Err(e); + } + Ok(staged) +} + +/// Remove copies a process left staged in `folder` when it ended mid-move: disposable, since the +/// original is still in place or held aside. Called only under the migration lock, which every +/// staging happens under. Links are never followed. +fn remove_stale_staging(folder: &Path) { + let Ok(entries) = std::fs::read_dir(folder) else { return }; + for entry in entries.flatten() { + if entry.file_name().to_str().is_none_or(|n| side_of(n, STAGED).is_none()) { + continue; + } + let removed = match entry.file_type() { + Ok(t) if t.is_dir() => std::fs::remove_dir_all(entry.path()), + Ok(t) if t.is_file() => std::fs::remove_file(entry.path()), + _ => continue, + }; + if let Err(e) = removed { + log::warn!("couldn't remove the stale staged copy {}: {e}", entry.path().display()); + } + } +} + +/// Give the staged copy its final name `to` without replacing anything there (see [`move_path`] +/// for the folder and file cases). On failure the staged copy is removed and `to` is untouched. +fn publish(staged: &Path, to: &Path) -> std::io::Result<()> { + let is_dir = std::fs::symlink_metadata(staged)?.is_dir(); + let published = if std::fs::symlink_metadata(to).is_ok() { + Err(already_exists(to)) + } else if is_dir { + std::fs::rename(staged, to) + } else { + std::fs::hard_link(staged, to).and_then(|()| std::fs::remove_file(staged)) + }; + if published.is_err() { + let _ = if is_dir { std::fs::remove_dir_all(staged) } else { std::fs::remove_file(staged) }; + } + published +} + +/// Copy the folder `from` to the new folder `to` (created here; an existing `to` is an error): +/// plain files and folders only (a link or other special file is refused), at most +/// [`MAX_COPY_DEPTH`] levels. +fn copy_tree(from: &Path, to: &Path, depth: u32) -> std::io::Result<()> { + if depth > MAX_COPY_DEPTH { + return Err(std::io::Error::other(format!("{} is nested too deep", from.display()))); + } + std::fs::create_dir(to)?; + for entry in std::fs::read_dir(from)? { + let entry = entry?; + let target = to.join(entry.file_name()); + if entry.file_type()?.is_dir() { + copy_tree(&entry.path(), &target, depth.saturating_add(1))?; + } else { + copy_file(&entry.path(), &target)?; + } + } + Ok(()) +} + +/// Copy one plain file (a link or other special file is refused) to the new file `to` (an +/// existing `to` is an error), flushed to disk. +fn copy_file(from: &Path, to: &Path) -> std::io::Result<()> { + if !std::fs::symlink_metadata(from)?.file_type().is_file() { + return Err(std::io::Error::other(format!("{} is not a plain file", from.display()))); + } + let mut src = std::fs::File::open(from)?; + let mut dst = std::fs::OpenOptions::new().write(true).create_new(true).open(to)?; + std::io::copy(&mut src, &mut dst)?; + dst.sync_all() +} + /// Resolve the data directory from the environment (`env`) and the executable's folder. pub fn resolve(env: impl Fn(&str) -> Option, exe_dir: Option<&Path>) -> DataDir { if let Some(d) = env("PHOTOCRAFT_CONFIG_DIR").filter(|d| !d.is_empty()) { @@ -109,126 +502,4 @@ fn ensure_writable(dir: &Path) -> std::io::Result<()> { } #[cfg(test)] -mod tests { - use super::*; - use std::collections::HashMap; - use std::sync::atomic::{AtomicUsize, Ordering}; - - fn temp(tag: &str) -> PathBuf { - static N: AtomicUsize = AtomicUsize::new(0); - let d = std::env::temp_dir().join(format!("photocraft-app-dirs-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed))); - let _ = std::fs::remove_dir_all(&d); - std::fs::create_dir_all(&d).unwrap(); - d - } - - fn env_of(pairs: &[(&str, &Path)]) -> impl Fn(&str) -> Option + use<> { - let m: HashMap = pairs.iter().map(|(k, v)| (k.to_string(), v.as_os_str().to_owned())).collect(); - move |k| m.get(k).cloned() - } - - /// A fake user folder that every platform's convention resolves under. - fn fake_home(root: &Path) -> impl Fn(&str) -> Option + use<> { - let home = root.join("home"); - let appdata = root.join("appdata"); - env_of(&[("HOME", &home), ("APPDATA", &appdata)]) - } - - #[test] - fn no_marker_uses_the_platform_dir() { - let root = temp("platform"); - let exe = root.join("bin"); - std::fs::create_dir_all(&exe).unwrap(); - let env = fake_home(&root); - let d = resolve(&env, Some(&exe)); - assert_eq!(d.mode, Mode::Platform); - assert!(d.warning.is_none()); - let dir = d.dir.unwrap(); - assert_eq!(Some(dir.clone()), platform_dir(&env)); - assert!(!dir.starts_with(&exe)); - assert!(!exe.join(PORTABLE_DATA_DIR).exists(), "no portable folder without a marker"); - // The convention per platform. - if cfg!(target_os = "macos") { - assert_eq!(dir, root.join("home/Library/Application Support/Photocraft")); - } else if cfg!(windows) { - assert_eq!(dir, root.join("appdata").join("Photocraft")); - } else { - assert_eq!(dir, root.join("home/.config/photocraft")); - } - let _ = std::fs::remove_dir_all(&root); - } - - #[test] - fn either_marker_keeps_everything_beside_the_exe() { - for marker in PORTABLE_MARKERS { - let root = temp("portable"); - let exe = root.join("PhotoCraft"); - std::fs::create_dir_all(&exe).unwrap(); - std::fs::write(exe.join(marker), b"").unwrap(); - let d = resolve(fake_home(&root), Some(&exe)); - assert_eq!(d.mode, Mode::Portable, "{marker}"); - assert!(d.warning.is_none()); - let dir = d.dir.unwrap(); - assert_eq!(dir, exe.join(PORTABLE_DATA_DIR)); - assert!(dir.is_dir(), "the data folder is created"); - assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0, "the write probe is removed"); - // Every derived path sits under the exe dir. - for sub in ["preferences.json", "Presets", "Recovery", crate::gpu_startup::MARKER_FILE] { - assert!(dir.join(sub).starts_with(&exe)); - } - assert!(!root.join("home").exists() && !root.join("appdata").exists(), "nothing written to the user folder"); - let _ = std::fs::remove_dir_all(&root); - } - } - - #[test] - fn a_marker_folder_is_not_a_marker() { - let root = temp("dir-marker"); - let exe = root.join("bin"); - std::fs::create_dir_all(exe.join("portable.txt")).unwrap(); - assert_eq!(resolve(fake_home(&root), Some(&exe)).mode, Mode::Platform); - let _ = std::fs::remove_dir_all(&root); - } - - #[test] - fn unwritable_portable_folder_falls_back_with_a_warning() { - let root = temp("unwritable"); - let exe = root.join("bin"); - std::fs::create_dir_all(&exe).unwrap(); - std::fs::write(exe.join("portable.txt"), b"").unwrap(); - // A file where the data folder should go: it can't be created, on every platform. - std::fs::write(exe.join(PORTABLE_DATA_DIR), b"in the way").unwrap(); - let env = fake_home(&root); - let d = resolve(&env, Some(&exe)); - assert_eq!(d.mode, Mode::Platform); - assert_eq!(d.dir, platform_dir(&env)); - let w = d.warning.unwrap(); - assert!(w.contains("portable") && w.contains("isn't writable"), "{w}"); - // No user folder at all: still no panic, just no directory. - let d = resolve(|_| None, Some(&exe)); - assert!(d.dir.is_none() && d.warning.is_some()); - let _ = std::fs::remove_dir_all(&root); - } - - #[test] - fn override_wins_over_portable() { - let root = temp("override"); - let exe = root.join("bin"); - std::fs::create_dir_all(&exe).unwrap(); - std::fs::write(exe.join("portable.txt"), b"").unwrap(); - let custom = root.join("custom"); - let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &custom)]), Some(&exe)); - assert_eq!((d.mode, d.dir), (Mode::Override, Some(custom))); - assert!(!exe.join(PORTABLE_DATA_DIR).exists()); - let _ = std::fs::remove_dir_all(&root); - } - - #[test] - fn missing_exe_dir_or_env_never_panics() { - let d = resolve(|_| None, None); - assert_eq!((d.mode, d.dir), (Mode::Platform, None)); - let empty = PathBuf::new(); - let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &empty), ("HOME", &empty), ("APPDATA", &empty)]), None); - assert_eq!(d.dir, None, "empty variables are ignored"); - } -} +mod tests; diff --git a/apps/photocraft/src/app_dirs/tests.rs b/apps/photocraft/src/app_dirs/tests.rs new file mode 100644 index 0000000000..c54299fb58 --- /dev/null +++ b/apps/photocraft/src/app_dirs/tests.rs @@ -0,0 +1,445 @@ +use super::*; +use std::collections::HashMap; +use std::sync::atomic::{AtomicUsize, Ordering}; + +fn temp(tag: &str) -> PathBuf { + static N: AtomicUsize = AtomicUsize::new(0); + let d = std::env::temp_dir().join(format!("photocraft-app-dirs-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed))); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn env_of(pairs: &[(&str, &Path)]) -> impl Fn(&str) -> Option + use<> { + let m: HashMap = pairs.iter().map(|(k, v)| (k.to_string(), v.as_os_str().to_owned())).collect(); + move |k| m.get(k).cloned() +} + +/// A fake user folder that every platform's convention resolves under. +fn fake_home(root: &Path) -> impl Fn(&str) -> Option + use<> { + let home = root.join("home"); + let appdata = root.join("appdata"); + env_of(&[("HOME", &home), ("APPDATA", &appdata)]) +} + +#[test] +fn no_marker_uses_the_platform_dir() { + let root = temp("platform"); + let exe = root.join("bin"); + std::fs::create_dir_all(&exe).unwrap(); + let env = fake_home(&root); + let d = resolve(&env, Some(&exe)); + assert_eq!(d.mode, Mode::Platform); + assert!(d.warning.is_none()); + let dir = d.dir.unwrap(); + assert_eq!(Some(dir.clone()), platform_dir(&env)); + assert!(!dir.starts_with(&exe)); + assert!(!exe.join(PORTABLE_DATA_DIR).exists(), "no portable folder without a marker"); + // The convention per platform. + if cfg!(target_os = "macos") { + assert_eq!(dir, root.join("home/Library/Application Support/Photocraft")); + } else if cfg!(windows) { + assert_eq!(dir, root.join("appdata").join("Photocraft")); + } else { + assert_eq!(dir, root.join("home/.config/photocraft")); + } + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn either_marker_keeps_everything_beside_the_exe() { + for marker in PORTABLE_MARKERS { + let root = temp("portable"); + let exe = root.join("PhotoCraft"); + std::fs::create_dir_all(&exe).unwrap(); + std::fs::write(exe.join(marker), b"").unwrap(); + let d = resolve(fake_home(&root), Some(&exe)); + assert_eq!(d.mode, Mode::Portable, "{marker}"); + assert!(d.warning.is_none()); + let dir = d.dir.unwrap(); + assert_eq!(dir, exe.join(PORTABLE_DATA_DIR)); + assert!(dir.is_dir(), "the data folder is created"); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 0, "the write probe is removed"); + // Every derived path sits under the exe dir. + for sub in ["preferences.json", "Presets", "Recovery", crate::gpu_startup::MARKER_FILE] { + assert!(dir.join(sub).starts_with(&exe)); + } + assert!(!root.join("home").exists() && !root.join("appdata").exists(), "nothing written to the user folder"); + let _ = std::fs::remove_dir_all(&root); + } +} + +#[test] +fn a_marker_folder_is_not_a_marker() { + let root = temp("dir-marker"); + let exe = root.join("bin"); + std::fs::create_dir_all(exe.join("portable.txt")).unwrap(); + assert_eq!(resolve(fake_home(&root), Some(&exe)).mode, Mode::Platform); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn unwritable_portable_folder_falls_back_with_a_warning() { + let root = temp("unwritable"); + let exe = root.join("bin"); + std::fs::create_dir_all(&exe).unwrap(); + std::fs::write(exe.join("portable.txt"), b"").unwrap(); + // A file where the data folder should go: it can't be created, on every platform. + std::fs::write(exe.join(PORTABLE_DATA_DIR), b"in the way").unwrap(); + let env = fake_home(&root); + let d = resolve(&env, Some(&exe)); + assert_eq!(d.mode, Mode::Platform); + assert_eq!(d.dir, platform_dir(&env)); + let w = d.warning.unwrap(); + assert!(w.contains("portable") && w.contains("isn't writable"), "{w}"); + // No user folder at all: still no panic, just no directory. + let d = resolve(|_| None, Some(&exe)); + assert!(d.dir.is_none() && d.warning.is_some()); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn override_wins_over_portable() { + let root = temp("override"); + let exe = root.join("bin"); + std::fs::create_dir_all(&exe).unwrap(); + std::fs::write(exe.join("portable.txt"), b"").unwrap(); + let custom = root.join("custom"); + let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &custom)]), Some(&exe)); + assert_eq!((d.mode, d.dir), (Mode::Override, Some(custom))); + assert!(!exe.join(PORTABLE_DATA_DIR).exists()); + let _ = std::fs::remove_dir_all(&root); +} + +/// #5648: on Windows, autosaves go to the machine-local `%LOCALAPPDATA%`, not the roaming +/// profile; an override or portable folder keeps them inside itself. +#[test] +fn autosaves_stay_out_of_the_roaming_profile() { + let root = temp("recovery-local"); + let settings = root.join("appdata").join("Photocraft"); + let local = root.join("local"); + let env = env_of(&[("LOCALAPPDATA", &local)]); + let platform = DataDir { dir: Some(settings.clone()), mode: Mode::Platform, warning: None }; + let expected = if cfg!(windows) { local.join("Photocraft").join("Recovery") } else { settings.join("Recovery") }; + assert_eq!(resolve_recovery(&platform, &env), Some(expected)); + for mode in [Mode::Override, Mode::Portable] { + let d = DataDir { dir: Some(settings.clone()), mode, warning: None }; + assert_eq!(resolve_recovery(&d, &env), Some(settings.join("Recovery")), "{mode:?}"); + } + // No (or an empty) LOCALAPPDATA: beside the settings, as before. + let empty = PathBuf::new(); + assert_eq!(resolve_recovery(&platform, env_of(&[("LOCALAPPDATA", &empty)])), Some(settings.join("Recovery"))); + assert_eq!(resolve_recovery(&platform, |_| None), Some(settings.join("Recovery"))); + let nowhere = DataDir { dir: None, mode: Mode::Platform, warning: None }; + assert_eq!(resolve_recovery(&nowhere, |_| None), None); + let _ = std::fs::remove_dir_all(&root); +} + +/// #6612: on Linux (and other XDG systems) autosaves go to `$XDG_STATE_HOME`, by default +/// `~/.local/state`, not the config folder; a relative `XDG_STATE_HOME` is ignored. Windows +/// and macOS don't read it, and an override or portable folder keeps them inside itself. +#[test] +fn autosaves_go_to_the_xdg_state_folder() { + let root = temp("recovery-xdg"); + let settings = root.join("home").join(".config").join("photocraft"); + let home = root.join("home"); + let state = root.join("state"); + let platform = DataDir { dir: Some(settings.clone()), mode: Mode::Platform, warning: None }; + let xdg = !cfg!(windows) && !cfg!(target_os = "macos"); + let expect = |xdg_path: PathBuf| Some(if xdg { xdg_path } else { settings.join("Recovery") }); + assert_eq!(resolve_recovery(&platform, env_of(&[("XDG_STATE_HOME", &state), ("HOME", &home)])), expect(state.join("photocraft").join("Recovery"))); + let default = home.join(".local").join("state").join("photocraft").join("Recovery"); + assert_eq!(resolve_recovery(&platform, env_of(&[("HOME", &home)])), expect(default.clone())); + let relative = PathBuf::from("relative/state"); + assert_eq!(resolve_recovery(&platform, env_of(&[("XDG_STATE_HOME", &relative), ("HOME", &home)])), expect(default.clone())); + let empty = PathBuf::new(); + assert_eq!(resolve_recovery(&platform, env_of(&[("XDG_STATE_HOME", &empty), ("HOME", &home)])), expect(default)); + // No home at all: beside the settings, as before. + assert_eq!(resolve_recovery(&platform, |_| None), Some(settings.join("Recovery"))); + for mode in [Mode::Override, Mode::Portable] { + let d = DataDir { dir: Some(settings.clone()), mode, warning: None }; + assert_eq!(resolve_recovery(&d, env_of(&[("XDG_STATE_HOME", &state), ("HOME", &home)])), Some(settings.join("Recovery")), "{mode:?}"); + } + let _ = std::fs::remove_dir_all(&root); +} + +/// An autosave bundle and its sidecar, as `format::Autosaver` writes them. +fn autosave(dir: &Path, key: &str) { + std::fs::create_dir_all(dir.join(format!("{key}.pcraft"))).unwrap(); + std::fs::write(dir.join(format!("{key}.pcraft")).join("manifest.json"), key).unwrap(); + std::fs::write(dir.join(format!("{key}.json")), key).unwrap(); +} + +#[test] +fn autosaves_from_the_old_folder_move_to_the_new_one() { + let root = temp("recovery-move"); + let legacy = root.join("roaming").join("Recovery"); + let dir = root.join("local").join("Photocraft").join("Recovery"); + autosave(&legacy, "a"); + assert_eq!(adopt_legacy_recovery(&legacy, &dir), dir); + assert!(!legacy.exists(), "the old folder is gone"); + assert_eq!(std::fs::read_to_string(dir.join("a.pcraft").join("manifest.json")).unwrap(), "a"); + assert!(dir.join("a.json").is_file()); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn autosaves_merge_into_an_existing_folder_without_overwriting() { + let root = temp("recovery-merge"); + let legacy = root.join("roaming").join("Recovery"); + let dir = root.join("local").join("Recovery"); + autosave(&legacy, "old"); + autosave(&legacy, "both"); + // A temp file of an interrupted write moves along, for the recovery store's sweep. + let temp_file = ".old.json.4294967294-0.pcsave.tmp"; + std::fs::write(legacy.join(temp_file), b"partial").unwrap(); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("both.json"), "newer").unwrap(); + assert_eq!(adopt_legacy_recovery(&legacy, &dir), dir); + assert!(dir.join(temp_file).is_file() && !legacy.join(temp_file).exists()); + assert!(dir.join("old.pcraft").is_dir() && dir.join("old.json").is_file(), "a whole autosave moves"); + assert_eq!(std::fs::read_to_string(dir.join("both.json")).unwrap(), "newer", "never overwritten"); + assert!(!dir.join("both.pcraft").exists(), "a clashing autosave isn't split"); + assert!(legacy.join("both.pcraft").is_dir() && legacy.join("both.json").is_file(), "it stays behind whole"); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn autosaves_that_cannot_move_keep_being_used_where_they_are() { + let root = temp("recovery-stuck"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "a"); + // A file where the new folder's parent should be: nothing can be created there. + std::fs::write(root.join("blocked"), b"in the way").unwrap(); + let dir = root.join("blocked").join("Recovery"); + assert_eq!(adopt_legacy_recovery(&legacy, &dir), legacy); + assert!(legacy.join("a.json").is_file(), "nothing lost"); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn an_empty_old_folder_is_removed_even_where_it_cannot_move() { + let root = temp("recovery-empty"); + let legacy = root.join("roaming").join("Recovery"); + std::fs::create_dir_all(&legacy).unwrap(); + std::fs::write(root.join("blocked"), b"in the way").unwrap(); + let dir = root.join("blocked").join("Recovery"); + assert_eq!(adopt_legacy_recovery(&legacy, &dir), dir, "nothing to keep listed"); + assert!(!legacy.exists(), "the empty old folder is gone"); + let _ = std::fs::remove_dir_all(&root); +} + +/// Where a rename can't cross volumes (a roaming profile on a network share), an autosave +/// is copied whole, nested folders included, and only then removed. +#[test] +fn a_move_across_volumes_copies_then_removes() { + let root = temp("recovery-copy"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "a"); + std::fs::create_dir_all(legacy.join("a.pcraft").join("tiles")).unwrap(); + std::fs::write(legacy.join("a.pcraft").join("tiles").join("t0.zst"), b"tile").unwrap(); + let dir = root.join("local").join("Recovery"); + std::fs::create_dir_all(root.join("local")).unwrap(); + move_across(&legacy, &dir).unwrap(); + assert!(!legacy.exists(), "the original is removed after the copy"); + assert_eq!(side_leftovers(&root.join("roaming")), 0, "nothing is left held aside"); + assert_eq!(std::fs::read_to_string(dir.join("a.pcraft").join("manifest.json")).unwrap(), "a"); + assert_eq!(std::fs::read(dir.join("a.pcraft").join("tiles").join("t0.zst")).unwrap(), b"tile"); + assert_eq!(std::fs::read_to_string(dir.join("a.json")).unwrap(), "a"); + // One file on its own, as the per-autosave moves do. + move_across(&dir.join("a.json"), &root.join("a.json")).unwrap(); + assert!(!dir.join("a.json").exists() && root.join("a.json").is_file()); + assert_eq!(side_leftovers(&dir) + side_leftovers(&root), 0); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn a_failed_copy_puts_the_original_back_and_leaves_no_partial_copy() { + let root = temp("recovery-copy-fail"); + let legacy = root.join("Recovery"); + autosave(&legacy, "a"); + // Deeper than any bundle: the copy gives up part way. + let mut deep = legacy.join("a.pcraft"); + for _ in 0..=MAX_COPY_DEPTH { + deep = deep.join("d"); + } + std::fs::create_dir_all(&deep).unwrap(); + let dir = root.join("Local"); + assert!(move_across(&legacy, &dir).is_err()); + assert!(!dir.exists(), "nothing is published"); + assert_eq!(side_leftovers(&root), 0, "no staged copy, nothing left held aside"); + assert!(legacy.join("a.json").is_file() && deep.is_dir(), "the original is back in place"); + // A target that already exists is never copied into or removed. + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("keep"), b"mine").unwrap(); + assert!(move_across(&legacy.join("a.json"), &dir).is_err()); + assert_eq!(std::fs::read(dir.join("keep")).unwrap(), b"mine"); + assert!(legacy.join("a.json").is_file()); + assert_eq!(side_leftovers(&root) + side_leftovers(&legacy), 0); + let _ = std::fs::remove_dir_all(&root); +} + +/// Staged (`.moving`) and held (`.held`) side paths left in `dir`. +fn side_leftovers(dir: &Path) -> usize { + std::fs::read_dir(dir) + .unwrap() + .flatten() + .filter(|e| e.file_name().to_str().is_some_and(|n| side_of(n, STAGED).is_some() || side_of(n, HELD).is_some())) + .count() +} + +/// The target appears after the copy is staged (another process got there first): publishing +/// fails, that process's target keeps its contents, the staged copy is removed and the +/// original is left as it was. Folder and file alike. +#[test] +fn a_target_that_appears_while_copying_is_never_replaced_or_removed() { + let root = temp("recovery-race"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "a"); + let local = root.join("local"); + std::fs::create_dir_all(&local).unwrap(); + // A whole folder. + let dir = local.join("Recovery"); + let staged = stage_copy(&legacy, &dir).unwrap(); + autosave(&dir, "theirs"); + let e = publish(&staged, &dir).unwrap_err(); + assert_eq!(e.kind(), std::io::ErrorKind::AlreadyExists); + assert_eq!(std::fs::read_to_string(dir.join("theirs.json")).unwrap(), "theirs", "their folder is intact"); + assert!(!dir.join("a.json").exists(), "nothing was copied into it"); + assert!(!staged.exists() && side_leftovers(&local) == 0, "only the staged copy is removed"); + assert!(legacy.join("a.json").is_file(), "the original is untouched"); + // One file. + let to = dir.join("a.json"); + let staged = stage_copy(&legacy.join("a.json"), &to).unwrap(); + std::fs::write(&to, "theirs").unwrap(); + assert_eq!(publish(&staged, &to).unwrap_err().kind(), std::io::ErrorKind::AlreadyExists); + assert_eq!(std::fs::read_to_string(&to).unwrap(), "theirs"); + assert!(!staged.exists() && legacy.join("a.json").is_file()); + let _ = std::fs::remove_dir_all(&root); +} + +/// A process holds the lock and is copying the old folder (held aside, copy staged) when a +/// second launch gives up waiting. The second launch must get the new folder, never the old +/// one, and what it writes there survives the first one finishing: the publish refuses the +/// now-existing folder and the original is put back, so both autosaves stay (#6412 review). +#[test] +fn a_launch_that_times_out_never_writes_where_a_move_removes() { + let _spawns = crate::no_child_spawns(); + let root = temp("recovery-timeout"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "old"); + let local = root.join("local"); + std::fs::create_dir_all(&local).unwrap(); + let dir = local.join("Recovery"); + // First process: takes the lock, sets the original aside and stages its copy, then pauses. + let first = std::fs::OpenOptions::new().create(true).truncate(false).write(true).open(local.join(MIGRATION_LOCK)).unwrap(); + first.lock().unwrap(); + let held = hold(&legacy).unwrap(); + let staged = stage_copy(&held, &dir).unwrap(); + // Second launch: times out and writes a newer autosave where it was told to. + let chosen = adopt_legacy_recovery_waiting(&legacy, &dir, std::time::Duration::ZERO); + assert_eq!(chosen, dir, "a launch that couldn't get the lock never uses the old folder"); + autosave(&chosen, "new"); + // First process resumes: publish, and on failure put the original back (as `move_across`). + assert_eq!(publish(&staged, &dir).unwrap_err().kind(), std::io::ErrorKind::AlreadyExists); + put_back(&held, &legacy); + drop(first); + assert_eq!(std::fs::read_to_string(dir.join("new.json")).unwrap(), "new", "the newer autosave survives"); + assert_eq!(std::fs::read_to_string(legacy.join("old.json")).unwrap(), "old", "the old one is back in place"); + assert_eq!(side_leftovers(&local) + side_leftovers(&root.join("roaming")), 0); + // The next launch merges them. + assert_eq!(adopt_legacy_recovery_waiting(&legacy, &dir, std::time::Duration::ZERO), dir); + assert!(dir.join("old.json").is_file() && dir.join("new.json").is_file() && !legacy.exists()); + let _ = std::fs::remove_dir_all(&root); +} + +/// A writer that still uses the old path while it is being copied (an older PhotoCraft) +/// starts a new folder there; the move removes only the original it held aside. +#[test] +fn a_write_to_the_old_path_during_a_copy_is_not_removed() { + let root = temp("recovery-old-writer"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "old"); + let local = root.join("local"); + std::fs::create_dir_all(&local).unwrap(); + let dir = local.join("Recovery"); + let held = hold(&legacy).unwrap(); + let staged = stage_copy(&held, &dir).unwrap(); + autosave(&legacy, "new"); + publish(&staged, &dir).unwrap(); + std::fs::remove_dir_all(&held).unwrap(); + assert_eq!(std::fs::read_to_string(dir.join("old.json")).unwrap(), "old"); + assert_eq!(std::fs::read_to_string(legacy.join("new.json")).unwrap(), "new", "the new write is untouched"); + let _ = std::fs::remove_dir_all(&root); +} + +/// What a process killed mid-move left is tidied at the next move: a held original is put +/// back (it may be the only copy) and moved, a staged copy is removed; other dot-files and +/// look-alike names are not touched. +#[test] +fn a_killed_move_is_tidied_next_time() { + let root = temp("recovery-stale"); + let roaming = root.join("roaming"); + let legacy = roaming.join("Recovery"); + autosave(&legacy, "a"); + let held = hold(&legacy).unwrap(); + assert!(!legacy.exists() && held.is_dir()); + let local = root.join("local"); + let dir = local.join("Recovery"); + let stale = local.join(".Recovery.4294967294-3.moving"); + autosave(&stale, "half"); + let kept = [local.join(".Recovery.moving"), local.join(".keep.1-x.moving"), local.join("notes.txt")]; + for k in &kept { + std::fs::write(k, b"x").unwrap(); + } + assert_eq!(adopt_legacy_recovery_waiting(&legacy, &dir, std::time::Duration::ZERO), dir); + assert_eq!(std::fs::read_to_string(dir.join("a.json")).unwrap(), "a", "the held original was put back and moved"); + assert!(!stale.exists(), "the stale staged copy is gone"); + assert_eq!(side_leftovers(&roaming), 0); + assert!(kept.iter().all(|k| k.is_file()), "only side names are touched"); + assert_eq!(side_of(".a.json.12-0.moving", STAGED), Some("a.json")); + assert!(side_of("a.json.12-0.moving", STAGED).is_none() && side_of(".a.json.-0.moving", STAGED).is_none()); + assert!(side_of(".a.json.12-0.held", STAGED).is_none() && side_of(".a.json.12-0.held", HELD) == Some("a.json")); + let _ = std::fs::remove_dir_all(&root); +} + +/// While another process holds the move lock, a launch moves nothing and uses the new folder; +/// once the lock is released, the move happens. +#[test] +fn a_move_waits_for_another_process_holding_the_lock() { + let _spawns = crate::no_child_spawns(); + let root = temp("recovery-lock"); + let legacy = root.join("roaming").join("Recovery"); + autosave(&legacy, "a"); + let dir = root.join("local").join("Recovery"); + std::fs::create_dir_all(root.join("local")).unwrap(); + let held = std::fs::OpenOptions::new().create(true).truncate(false).write(true).open(root.join("local").join(MIGRATION_LOCK)).unwrap(); + held.lock().unwrap(); + assert_eq!(adopt_legacy_recovery_waiting(&legacy, &dir, std::time::Duration::ZERO), dir); + assert!(legacy.join("a.json").is_file() && !dir.exists(), "nothing moved under another's lock"); + drop(held); + assert_eq!(adopt_legacy_recovery_waiting(&legacy, &dir, std::time::Duration::ZERO), dir); + assert!(dir.join("a.json").is_file() && !legacy.exists()); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn no_old_folder_or_the_same_folder_changes_nothing() { + let root = temp("recovery-noop"); + let dir = root.join("Recovery"); + assert_eq!(adopt_legacy_recovery(&root.join("missing"), &dir), dir); + assert!(!dir.exists(), "nothing is created up front"); + autosave(&dir, "a"); + assert_eq!(adopt_legacy_recovery(&dir, &dir), dir); + assert!(dir.join("a.json").is_file()); + let _ = std::fs::remove_dir_all(&root); +} + +#[test] +fn missing_exe_dir_or_env_never_panics() { + let d = resolve(|_| None, None); + assert_eq!((d.mode, d.dir), (Mode::Platform, None)); + let empty = PathBuf::new(); + let d = resolve(env_of(&[("PHOTOCRAFT_CONFIG_DIR", &empty), ("HOME", &empty), ("APPDATA", &empty)]), None); + assert_eq!(d.dir, None, "empty variables are ignored"); +} diff --git a/apps/photocraft/src/app_icon.rs b/apps/photocraft/src/app_icon.rs index 54eea2d6b5..4fb8765e52 100644 --- a/apps/photocraft/src/app_icon.rs +++ b/apps/photocraft/src/app_icon.rs @@ -15,7 +15,7 @@ //! be stamped on the shortcut, or the taskbar would stop matching the two. /// Window, taskbar and (when running unbundled) Dock icon. macOS gets the padded 1024 px render -/// on Apple's icon grid; elsewhere the tighter 256 px hicolor render reads better at small sizes. +/// on Apple's icon grid; elsewhere the 256 px hicolor render of the full tile. pub fn window_icon() -> egui::IconData { match eframe::icon_data::from_png_bytes(PNG) { Ok(icon) => icon, @@ -34,6 +34,13 @@ pub fn sets_runtime_icon(exe: Option<&std::path::Path>) -> bool { !(cfg!(target_os = "macos") && exe.is_some_and(in_app_bundle)) } +/// Value for [`egui::ViewportBuilder::with_icon`]: [`window_icon`] when [`sets_runtime_icon`] +/// is true; otherwise [`egui::IconData::default`] so eframe does not substitute its own icon +/// on the Dock after the window opens (#7253). +pub fn viewport_icon(exe: Option<&std::path::Path>) -> egui::IconData { + if sets_runtime_icon(exe) { window_icon() } else { egui::IconData::default() } +} + /// Whether `exe` is an app bundle's executable: `…/Name.app/Contents/MacOS/binary`. fn in_app_bundle(exe: &std::path::Path) -> bool { let name = |p: Option<&std::path::Path>| p.and_then(|p| p.file_name()).map(|n| n.to_string_lossy().into_owned()); @@ -80,6 +87,19 @@ mod tests { assert_eq!(sets_runtime_icon(Some(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft"))), !cfg!(target_os = "macos")); } + /// #7253: omitting `with_icon` made eframe paint its default glyph on the Dock once the window loaded. + #[test] + fn viewport_icon_blocks_eframe_default_when_runtime_icon_is_skipped() { + use std::path::Path; + let bundled = Some(Path::new("/Applications/PhotoCraft.app/Contents/MacOS/photocraft")); + if sets_runtime_icon(bundled) { + let icon = viewport_icon(bundled); + assert!(icon.width >= 256 && icon.width == icon.height); + } else { + assert_eq!(viewport_icon(bundled), egui::IconData::default()); + } + } + #[test] fn window_icon_decodes() { let icon = window_icon(); @@ -88,6 +108,16 @@ mod tests { assert!(icon.rgba.chunks(4).any(|p| p[3] > 0), "the icon isn't blank"); } + #[test] + fn windows_and_linux_icons_keep_the_round_corners() { + // rx=112 of 512 leaves (10, 10) of a 256 px render transparent; the old crop filled it (#4478). + let png = include_bytes!("../../../assets/app-icon/hicolor/256x256/apps/ai.storyteller.photocraft.png"); + let icon = eframe::icon_data::from_png_bytes(png).unwrap(); + assert_eq!(icon.width, 256); + assert_eq!(icon.rgba[(10 * 256 + 10) * 4 + 3], 0, "corner is transparent"); + assert_eq!(icon.rgba[(128 * 256 + 2) * 4 + 3], 255, "the tile is full bleed"); + } + /// The `.ico` build.rs embeds: every size Windows asks for, each a valid PNG or BMP image. #[test] fn exe_icon_has_every_shell_size() { diff --git a/apps/photocraft/src/control_server.rs b/apps/photocraft/src/control_server.rs index 30ae610b70..4834327b8e 100644 --- a/apps/photocraft/src/control_server.rs +++ b/apps/photocraft/src/control_server.rs @@ -16,16 +16,11 @@ use photocraft_ui_egui::ControlRequest; use photocraft_ui_egui::control::REPLY_DEADLINE; use serde_json::{Value, json}; -pub fn start(port: u16, token: String, ctx: egui::Context) -> Receiver { +/// Start serving a listener already bound during startup, before a window is opened. +pub fn start(listener: TcpListener, token: String, ctx: egui::Context) -> Receiver { let (tx, rx) = channel::(); - let listener = match TcpListener::bind(("127.0.0.1", port)) { - Ok(l) => l, - Err(e) => { - eprintln!("photocraft: control server failed to bind 127.0.0.1:{port}: {e}"); - return rx; - } - }; - eprintln!("photocraft: control server listening on 127.0.0.1:{port}"); + let port = listener.local_addr().map_or(0, |a| a.port()); + eprintln!("photocraft: control server listening on {}", listening_addr(&listener, port)); std::thread::spawn(move || { let limiter = ConnectionLimiter::new(MAX_CONNECTIONS); let token = Arc::new(token); @@ -47,6 +42,16 @@ pub fn start(port: u16, token: String, ctx: egui::Context) -> Receiver String { + match listener.local_addr() { + Ok(addr) => addr.to_string(), + Err(_) => format!("127.0.0.1:{requested_port}"), + } +} + fn serve(stream: TcpStream, token: &str, tx: Sender, ctx: egui::Context) { serve_with_auth_deadline(stream, token, tx, ctx, AUTH_DEADLINE); } @@ -131,6 +136,41 @@ mod tests { use super::*; use std::io::BufRead; + /// #6427: with port 0 the log names the port the OS bound, which a client can connect to. + #[test] + fn port_zero_logs_the_bound_port() { + let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let bound = listener.local_addr().unwrap(); + assert_ne!(bound.port(), 0); + let logged = listening_addr(&listener, 0); + assert_eq!(logged, format!("127.0.0.1:{}", bound.port())); + TcpStream::connect(&logged).unwrap(); + } + + #[test] + fn a_prebound_listener_authenticates_and_dispatches_requests() { + const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let requests = start(listener, TOKEN.into(), egui::Context::default()); + let mut stream = TcpStream::connect(address).unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut replies = BufReader::new(stream.try_clone().unwrap()); + writeln!(stream, "{}", json!({"id": 1, "method": "auth", "params": {"token": TOKEN}})).unwrap(); + let mut line = String::new(); + replies.read_line(&mut line).unwrap(); + assert_eq!(serde_json::from_str::(&line).unwrap()["result"]["authenticated"], true); + writeln!(stream, "{}", json!({"id": 2, "method": "test.bound"})).unwrap(); + let request = requests.recv_timeout(Duration::from_secs(5)).unwrap(); + assert_eq!(request.method, "test.bound"); + request.reply.send(json!({"ok": true, "result": "served"})).unwrap(); + line.clear(); + replies.read_line(&mut line).unwrap(); + let reply: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(reply["id"], 2); + assert_eq!(reply["result"], "served"); + } + /// #4965: a connection that keeps sending without authenticating is closed at the deadline /// (releasing its connection permit), and nothing it sent is dispatched. #[test] diff --git a/apps/photocraft/src/file_dialog_probe.rs b/apps/photocraft/src/file_dialog_probe.rs index 9ee14c0afb..eed74a58f2 100644 --- a/apps/photocraft/src/file_dialog_probe.rs +++ b/apps/photocraft/src/file_dialog_probe.rs @@ -70,25 +70,61 @@ pub fn start() { /// Whether the portal's `FileChooser` interface is on the session bus: `Some(false)` only when the /// bus or the interface is definitely absent, `None` for anything inconclusive (a slow reply). +/// +/// Asks the portal object to introspect itself rather than reading the interface's `version` +/// property: xdg-desktop-portal exports `FileChooser` only when a backend offers it, and without +/// one a property read fails with `InvalidArgs` ("No such interface"), an error the old check +/// didn't count as absent (#5739). The interface list answers the question directly. #[cfg(target_os = "linux")] fn file_chooser_portal() -> Option { let Ok(conn) = zbus::blocking::connection::Builder::session().and_then(|b| b.method_timeout(std::time::Duration::from_secs(5)).build()) else { return Some(false); }; - let proxy = zbus::blocking::proxy::Builder::>::new(&conn) - .destination("org.freedesktop.portal.Desktop") - .and_then(|b| b.path("/org/freedesktop/portal/desktop")) - .and_then(|b| b.interface("org.freedesktop.portal.FileChooser")) - .and_then(|b| b.cache_properties(zbus::proxy::CacheProperties::No).build()) - .ok()?; - match proxy.get_property::("version") { - Ok(_) => Some(true), - Err(zbus::Error::MethodError(name, _, _)) => { - let name = name.as_str(); - let absent = ["ServiceUnknown", "UnknownObject", "UnknownInterface", "UnknownProperty"].iter().any(|n| name.ends_with(n)); - absent.then_some(false) - } - Err(_) => None, + let reply = conn.call_method( + Some("org.freedesktop.portal.Desktop"), + "/org/freedesktop/portal/desktop", + Some("org.freedesktop.DBus.Introspectable"), + "Introspect", + &(), + ); + match reply { + Ok(message) => match message.body().deserialize::() { + Ok(xml) => Some(interface_listed(&xml)), + Err(_) => None, + }, + Err(e) => classify_error(&e), + } +} + +/// Whether introspection XML lists the portal's `FileChooser` interface (not a longer name such as +/// `FileChooser2`). +#[cfg(target_os = "linux")] +fn interface_listed(xml: &str) -> bool { + ["\"", "'"].iter().any(|quote| xml.contains(&format!(" Option { + use zbus::fdo::Error as Fdo; + // Names a bus daemon or portal answers with when the thing asked about is absent. + const ABSENT: [&str; 7] = ["ServiceUnknown", "NameHasNoOwner", "UnknownObject", "UnknownInterface", "UnknownMethod", "UnknownProperty", "InvalidArgs"]; + match e { + zbus::Error::FDO(fdo) => matches!( + **fdo, + Fdo::ServiceUnknown(_) + | Fdo::NameHasNoOwner(_) + | Fdo::UnknownObject(_) + | Fdo::UnknownInterface(_) + | Fdo::UnknownMethod(_) + | Fdo::UnknownProperty(_) + | Fdo::InvalidArgs(_) + ) + .then_some(false), + zbus::Error::MethodError(name, _, _) => ABSENT.iter().any(|n| name.as_str().ends_with(n)).then_some(false), + _ => None, } } @@ -126,4 +162,69 @@ mod tests { assert!(!on_path("kdialog", &path)); std::fs::remove_dir_all(&dir).unwrap(); } + + #[cfg(target_os = "linux")] + mod portal { + use super::super::*; + + const WITH_FILE_CHOOSER: &str = r#" + + + + + + +"#; + const WITHOUT_FILE_CHOOSER: &str = r#" + + +"#; + + fn method_error(name: &'static str) -> zbus::Error { + let call = zbus::message::Message::method_call("/a", "M").unwrap().build(&()).unwrap(); + let reply = zbus::message::Message::error(&call.header(), name).unwrap().build(&"detail").unwrap(); + zbus::Error::MethodError(name.try_into().unwrap(), Some("detail".into()), reply) + } + + #[test] + fn the_interface_list_says_whether_a_file_chooser_is_offered() { + assert!(interface_listed(WITH_FILE_CHOOSER)); + assert!(interface_listed(""), "single quotes"); + assert!(!interface_listed(WITHOUT_FILE_CHOOSER), "portal without a backend"); + assert!(!interface_listed(r#""#), "longer name"); + assert!(!interface_listed("")); + } + + #[test] + fn dbus_errors_for_a_missing_interface_mean_absent() { + // zbus's fdo proxies hand D-Bus errors back as `Error::FDO`, a plain method call as + // `MethodError`; both forms count. The portal answers `InvalidArgs` ("No such interface") + // when no backend offers the interface (#5739). + for e in [ + zbus::fdo::Error::InvalidArgs("No such interface".into()), + zbus::fdo::Error::UnknownInterface("x".into()), + zbus::fdo::Error::ServiceUnknown("x".into()), + zbus::fdo::Error::NameHasNoOwner("x".into()), + zbus::fdo::Error::UnknownObject("x".into()), + ] { + let desc = format!("{e:?}"); + assert_eq!(classify_error(&zbus::Error::FDO(Box::new(e))), Some(false), "{desc}"); + } + } + + #[test] + fn method_error_names_still_count_as_absent() { + for name in ["org.freedesktop.DBus.Error.ServiceUnknown", "org.freedesktop.DBus.Error.InvalidArgs", "org.freedesktop.DBus.Error.UnknownMethod"] { + assert_eq!(classify_error(&method_error(name)), Some(false), "{name}"); + } + assert_eq!(classify_error(&method_error("org.freedesktop.DBus.Error.AccessDenied")), None); + } + + #[test] + fn inconclusive_errors_are_not_absent() { + assert_eq!(classify_error(&zbus::Error::FDO(Box::new(zbus::fdo::Error::NoReply("slow".into())))), None); + assert_eq!(classify_error(&zbus::Error::FDO(Box::new(zbus::fdo::Error::Failed("x".into())))), None); + assert_eq!(classify_error(&zbus::Error::InputOutput(std::sync::Arc::new(std::io::Error::other("gone")))), None); + } + } } diff --git a/apps/photocraft/src/gpu_startup.rs b/apps/photocraft/src/gpu_startup.rs index e79b4bafac..56d721e495 100644 --- a/apps/photocraft/src/gpu_startup.rs +++ b/apps/photocraft/src/gpu_startup.rs @@ -12,8 +12,10 @@ //! - macOS: Metal → CPU //! //! The choice that worked is remembered in `performance.gpuBackend` (Preferences › Performance, -//! where it can be reset). With `auto`, Intel adapters on Windows use DX12. `WGPU_BACKEND` still -//! overrides everything, and `--safe-gpu` forces the CPU path for one launch. +//! where it can be reset). With `auto`, Intel and Qualcomm (Windows on ARM) adapters on Windows +//! use DX12, and when DXGI lists an Intel adapter the instance leaves Vulkan out, so the first +//! start doesn't load the Intel Vulkan driver either. `WGPU_BACKEND` still overrides everything, +//! and `--safe-gpu` forces the CPU path for one launch. //! //! "CPU" composites on the CPU and draws the window with a software adapter where the platform //! has one (WARP on Windows, llvmpipe over GL elsewhere). On Linux a GL adapter that can't present @@ -33,6 +35,10 @@ use serde_json::{Value, json}; pub const MARKER_FILE: &str = "gpu-starting.json"; /// PCI vendor id of Intel. pub const INTEL: u32 = 0x8086; +/// Qualcomm's vendor id as its Vulkan driver reports it. +pub const QUALCOMM: u32 = 0x5143; +/// Qualcomm's vendor id as DXGI (DX12) reports it: "QCOM". +pub const QUALCOMM_DXGI: u32 = 0x4D4F_4351; /// Largest marker read (a marker is a few hundred bytes). const MARKER_MAX: u64 = 64 << 10; @@ -204,6 +210,28 @@ pub fn backends(plan: &Plan, os: Os) -> Option { }) } +/// Instance backends for `auto` on Windows (no `WGPU_BACKEND`) when `dx12_vendors`, the PCI vendor +/// ids of the DX12 adapters, includes Intel: DX12, with GL as the last resort (see [`rank`]). An +/// instance with Vulkan loads the Intel Vulkan driver before the adapter selector runs, and #4's +/// crash is in that driver. `dx12_vendors` is called only for that plan; `None` keeps [`backends`]' +/// choice. +pub fn windows_auto_backends(plan: &Plan, os: Os, dx12_vendors: impl FnOnce() -> Vec) -> Option { + let auto = plan.env.is_none() && plan.backend == GpuBackend::Auto && os == Os::Windows; + (auto && dx12_vendors().contains(&INTEL)).then_some(wgpu::Backends::DX12 | wgpu::Backends::GL) +} + +/// The PCI vendor ids of the DX12 adapters, from a DX12-only instance (DXGI, no Vulkan driver) +/// with `desc`'s flags and backend options (the shader compiler from [`dx12_compiler`]). +fn dx12_vendors(desc: &wgpu::InstanceDescriptor) -> Vec { + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { + backends: wgpu::Backends::DX12, + flags: desc.flags, + backend_options: desc.backend_options.clone(), + ..wgpu::InstanceDescriptor::new_without_display_handle() + }); + crate::services::block_on(instance.enumerate_adapters(wgpu::Backends::DX12)).iter().map(|a| a.get_info().vendor).collect() +} + /// The DX12 shader compiler (#712). wgpu's default loads `dxcompiler.dll` by name, and the /// Windows DLL search falls through to the current directory and `PATH`, so another program's DXC /// build (a browser's, an SDK's) got loaded and failed device creation or crashed the first shader @@ -220,6 +248,18 @@ pub fn dx12_compiler(env: Option<&str>, exe_dir: Option<&Path>) -> wgpu::Dx12Com } } +/// The GPU makers whose Windows Vulkan driver `auto` avoids, so DX12 comes first: Intel's +/// (`igvk64.dll` crashed while the device was created, #4) and Qualcomm's Adreno driver on +/// Windows on ARM (it lost the device mid-session and the window went with it; DX12 on the same +/// machine didn't, #7537). The name is for System Info. +fn dx12_first_on_windows(vendor: u32) -> Option<&'static str> { + match vendor { + INTEL => Some("Intel"), + QUALCOMM | QUALCOMM_DXGI => Some("Qualcomm"), + _ => None, + } +} + /// What adapter selection needs to know about an adapter. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Candidate { @@ -228,13 +268,16 @@ pub struct Candidate { pub backend: wgpu::Backend, /// Can present to the window. pub surface_ok: bool, + /// Its name matches the adapter the user asked for ([`adapter_matches`]). + pub wanted: bool, } /// Rank of an adapter (lower is better): high-performance device types first (egui's power /// preference) or software adapters first for `cpu`; then backends, with DX12 before Vulkan for -/// Intel on Windows. For `cpu`, any GL adapter comes before Vulkan, which is only the fallback -/// for presenting the window (see [`backends`]). On Windows GL comes after every other adapter, -/// WARP included: GL there (VMware's SVGA3D, GL-only old GPUs) drew a black window (#5191, #1788). +/// Intel and Qualcomm on Windows ([`dx12_first_on_windows`]). For `cpu`, any GL adapter comes +/// before Vulkan, which is only the fallback for presenting the window (see [`backends`]). On +/// Windows GL comes after every other adapter, WARP included: GL there (VMware's SVGA3D, GL-only +/// old GPUs) drew a black window (#5191, #1788). fn rank(a: &Candidate, backend: GpuBackend, os: Os) -> (u8, u8, u8) { let fallback = u8::from((backend == GpuBackend::Cpu && a.backend == wgpu::Backend::Vulkan) || (os == Os::Windows && a.backend == wgpu::Backend::Gl)); let ty = match a.device_type { @@ -250,7 +293,7 @@ fn rank(a: &Candidate, backend: GpuBackend, os: Os) -> (u8, u8, u8) { _ => ty, }; let be = match a.backend { - wgpu::Backend::Dx12 if os == Os::Windows && a.vendor == INTEL => 0, + wgpu::Backend::Dx12 if os == Os::Windows && dx12_first_on_windows(a.vendor).is_some() => 0, wgpu::Backend::Vulkan | wgpu::Backend::Metal => 1, wgpu::Backend::Dx12 => 2, wgpu::Backend::Gl => 3, @@ -259,19 +302,62 @@ fn rank(a: &Candidate, backend: GpuBackend, os: Os) -> (u8, u8, u8) { (fallback, ty, be) } -/// The adapter to use among `adapters` (`None` when there is none). +/// The adapter to use among `adapters` (`None` when there is none). Adapters the user asked for +/// win over the rest when any of them can present; otherwise the ranking decides (see +/// [`wanted_missed`]). pub fn pick(adapters: &[Candidate], backend: GpuBackend, os: Os) -> Option { - adapters.iter().enumerate().filter(|(_, a)| a.surface_ok).min_by_key(|(i, a)| (rank(a, backend, os), *i)).map(|(i, _)| i) + let usable = |a: &&Candidate| a.surface_ok; + let any_wanted = adapters.iter().filter(usable).any(|a| a.wanted); + adapters.iter().enumerate().filter(|(_, a)| a.surface_ok && (a.wanted || !any_wanted)).min_by_key(|(i, a)| (rank(a, backend, os), *i)).map(|(i, _)| i) } -/// Whether picking `chosen` applied the Intel-on-Windows DX12 default (a Vulkan adapter of the -/// same vendor was available). -pub fn intel_dx12_applied(adapters: &[Candidate], chosen: usize, os: Os) -> bool { - let Some(c) = adapters.get(chosen) else { return false }; - os == Os::Windows - && c.vendor == INTEL - && c.backend == wgpu::Backend::Dx12 - && adapters.iter().any(|a| a.vendor == INTEL && a.backend == wgpu::Backend::Vulkan) +/// Whether an adapter called `name` is the one the user asked for: `wanted` is a case-insensitive +/// piece of its name ("nvidia"), or several words that each appear in it ("arc a370m" finds +/// "Intel(R) Arc(tm) A370M Graphics"). An empty `wanted` matches nothing. +pub fn adapter_matches(name: &str, wanted: &str) -> bool { + photocraft_engine::prefs::gpu_adapter_matches(name, wanted) +} + +/// The user asked for an adapter and none of `adapters` that can present is it. +pub fn wanted_missed(adapters: &[Candidate], wanted: Option<&str>) -> bool { + wanted.is_some() && !adapters.iter().any(|a| a.surface_ok && a.wanted) +} + +/// Choosing an adapter is Linux-only for now: Windows has its own Intel/DX12 selection and macOS a +/// single Metal path, and neither is tested with a requested adapter. +pub const ADAPTER_CHOICE: bool = cfg!(target_os = "linux"); + +/// The adapter request that applies to this launch: none off Linux (`linux`), and none in CPU mode, +/// which wants a software adapter whatever the preference says. +pub fn applicable_adapter(wanted: Option, backend: GpuBackend, linux: bool) -> Option { + wanted.filter(|_| linux && backend != GpuBackend::Cpu) +} + +/// Whether `PHOTOCRAFT_GPU_ADAPTER` makes this launch its own process: only when startup will act +/// on it (a non-empty value, on Linux, outside CPU mode). Otherwise it changes nothing, and the +/// launch may still be forwarded to the running window. +pub fn adapter_override_is_process_specific(env: Option<&str>, mode: RenderingMode, linux: bool) -> bool { + env.is_some_and(|v| !v.trim().is_empty()) && linux && mode != RenderingMode::Cpu +} + +/// The adapter to ask for: `PHOTOCRAFT_GPU_ADAPTER` when set (for one launch), else +/// `performance.gpuAdapter`; `None` is automatic. +pub fn wanted_adapter(env: Option<&str>, pref: &str) -> Option { + [env.unwrap_or(""), pref].iter().map(|s| s.trim()).find(|s| !s.is_empty()).map(|s| s.chars().take(256).collect()) +} + +/// The maker's name when picking `chosen` applied the Windows DX12 default of +/// [`dx12_first_on_windows`]: a Vulkan adapter of the same maker was available, or, for Intel, +/// `vulkan_left_out` ([`windows_auto_backends`] left Vulkan out of the instance). +pub fn dx12_default_applied(adapters: &[Candidate], chosen: usize, os: Os, vulkan_left_out: bool) -> Option<&'static str> { + let c = adapters.get(chosen)?; + if os != Os::Windows || c.backend != wgpu::Backend::Dx12 { + return None; + } + let maker = dx12_first_on_windows(c.vendor)?; + let applied = + (vulkan_left_out && c.vendor == INTEL) || adapters.iter().any(|a| a.backend == wgpu::Backend::Vulkan && dx12_first_on_windows(a.vendor) == Some(maker)); + applied.then_some(maker) } /// wgpu compiles indirect-call validation shaders inside `request_device`; Metal's compiler @@ -283,9 +369,9 @@ fn skips_indirect_validation(os: Os, intel: bool) -> bool { } /// Configure eframe's wgpu setup for `plan`: instance backends, the DX12 shader compiler, adapter -/// selection (Intel on Windows, software adapters for `cpu`) and the marker update once the -/// adapter is chosen. `note` receives a remark for System Info (e.g. the Intel default). -pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, sentinel: SharedSentinel, note: Arc>>) { +/// selection (Intel and Qualcomm on Windows, software adapters for `cpu`) and the marker update +/// once the adapter is chosen. `note` receives a remark for System Info (e.g. the DX12 default). +pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, wanted: Option, sentinel: SharedSentinel, note: Arc>>) { if let egui_wgpu::WgpuSetup::CreateNew(create) = setup { if let Some(b) = backends(plan, os) { create.instance_descriptor.backends = b; @@ -298,7 +384,13 @@ pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, sentinel let env = std::env::var("WGPU_DX12_COMPILER").ok(); create.instance_descriptor.backend_options.dx12.shader_compiler = dx12_compiler(env.as_deref(), exe.as_deref().and_then(Path::parent)); } - let select = plan.env.is_none() && (plan.backend == GpuBackend::Cpu || (plan.backend == GpuBackend::Auto && os == Os::Windows)); + let without_vulkan = windows_auto_backends(plan, os, || dx12_vendors(&create.instance_descriptor)); + if let Some(b) = without_vulkan { + create.instance_descriptor.backends = b; + } + let vulkan_left_out = without_vulkan.is_some(); + let wanted = applicable_adapter(wanted, plan.backend, ADAPTER_CHOICE); + let select = wanted.is_some() || (plan.env.is_none() && (plan.backend == GpuBackend::Cpu || (plan.backend == GpuBackend::Auto && os == Os::Windows))); if select { let backend = plan.backend; create.native_adapter_selector = Some(Arc::new(move |adapters: &[wgpu::Adapter], surface: Option<&wgpu::Surface<'_>>| { @@ -311,17 +403,24 @@ pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, sentinel device_type: i.device_type, backend: i.backend, surface_ok: surface.is_none_or(|s| a.is_surface_supported(s)), + wanted: wanted.as_deref().is_some_and(|w| adapter_matches(&i.name, w)), } }) .collect(); let i = pick(&cands, backend, os).ok_or_else(|| "no graphics adapter found".to_string())?; + if let Some(w) = wanted.as_deref().filter(|_| wanted_missed(&cands, wanted.as_deref())) { + *note.lock().unwrap_or_else(PoisonError::into_inner) = + Some(format!("No graphics adapter named \"{w}\" can draw the window; using the automatic choice")); + } if backend == GpuBackend::Cpu && cands.get(i).is_some_and(|c| c.device_type != wgpu::DeviceType::Cpu) { *note.lock().unwrap_or_else(PoisonError::into_inner) = Some("CPU image rendering; no software graphics adapter is available, so the window uses hardware graphics".into()); } - if backend == GpuBackend::Auto && intel_dx12_applied(&cands, i, os) { + if backend == GpuBackend::Auto + && let Some(maker) = dx12_default_applied(&cands, i, os, vulkan_left_out) + { *note.lock().unwrap_or_else(PoisonError::into_inner) = - Some("Intel graphics on Windows: using DirectX 12 (the Intel Vulkan driver is known to crash)".into()); + Some(format!("{maker} graphics on Windows: using DirectX 12 (the {maker} Vulkan driver is known to crash)")); } adapters.get(i).cloned().ok_or_else(|| "no graphics adapter found".to_string()) })); @@ -443,6 +542,13 @@ pub fn read_display_server(path: Option<&Path>) -> photocraft_engine::prefs::Lin .unwrap_or_default() } +/// Preferences › Performance › Graphics Adapter (`performance.gpuAdapter`), read before the window +/// opens; empty (automatic) when the file or value is missing. +pub fn read_gpu_adapter(path: Option<&Path>) -> String { + let v: Value = path.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok()).unwrap_or(Value::Null); + v.get("performance").and_then(|p| p.get("gpuAdapter")).and_then(Value::as_str).unwrap_or("").chars().take(256).collect() +} + /// Preferences › Performance › Low-Latency Drawing (`performance.lowLatencyDrawing`, off unless /// turned on), read before the window opens: it picks the surface's present mode. pub fn read_low_latency(path: Option<&Path>) -> bool { @@ -463,470 +569,4 @@ pub fn read_rendering_prefs(path: Option<&Path>) -> (GpuBackend, RenderingMode) } #[cfg(test)] -mod tests { - use super::*; - use GpuBackend::*; - - fn crashed(backend: &str, adapter_backend: &str) -> Marker { - Marker { backend: backend.into(), adapter: "Intel(R) UHD Graphics".into(), adapter_backend: adapter_backend.into(), ..Default::default() } - } - - #[test] - fn low_latency_drawing_is_read_before_the_window_opens_and_defaults_off() { - let dir = std::env::temp_dir().join(format!("photocraft-low-latency-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let file = dir.join("preferences.json"); - let read = |json: &str| { - std::fs::write(&file, json).unwrap(); - read_low_latency(Some(&file)) - }; - assert!(read(r#"{"performance":{"lowLatencyDrawing":true}}"#)); - assert!(!read(r#"{"performance":{"lowLatencyDrawing":false}}"#)); - assert!(!read(r#"{"performance":{"lowLatencyDrawing":"nope"}}"#), "garbage keeps the default (off)"); - assert!(!read("not json")); - assert!(!read_low_latency(None), "no settings file: the default (off)"); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn display_server_preference_is_read_before_the_window_opens() { - use photocraft_engine::prefs::LinuxDisplayServer; - let dir = std::env::temp_dir().join(format!("photocraft-display-server-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let file = dir.join("preferences.json"); - let read = |json: &str| { - std::fs::write(&file, json).unwrap(); - read_display_server(Some(&file)) - }; - assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"x11"}}"#), LinuxDisplayServer::X11); - assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"auto","gpuBackend":"vulkan"}}"#), LinuxDisplayServer::Auto); - // Unknown values, other types, broken JSON and a missing file all start as Auto. - assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"wayland"}}"#), LinuxDisplayServer::Auto); - assert_eq!(read(r#"{"performance":{"linuxDisplayServer":11}}"#), LinuxDisplayServer::Auto); - assert_eq!(read("{not json"), LinuxDisplayServer::Auto); - assert_eq!(read_display_server(Some(&dir.join("missing.json"))), LinuxDisplayServer::Auto); - assert_eq!(read_display_server(None), LinuxDisplayServer::Auto); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn cpu_policy_ignores_hardware_backend_override() { - let p = plan_with_mode(Vulkan, RenderingMode::Cpu, None, Some("vulkan"), false, Os::Windows); - assert_eq!(p.backend, Cpu); - assert!(p.env.is_none()); - assert_eq!(plan_with_mode(Vulkan, RenderingMode::Cpu, Some(&crashed("vulkan", "vulkan")), None, false, Os::Windows).backend, Cpu); - let p = plan_with_mode(Auto, RenderingMode::Gpu, None, Some("vulkan"), true, Os::Windows); - assert_eq!(p.backend, Cpu); - assert!(p.env.is_none()); - } - - #[test] - fn no_marker_uses_the_preference() { - for os in [Os::Windows, Os::Mac, Os::Other] { - let p = plan(Auto, None, None, false, os); - assert_eq!(p, Plan { backend: Auto, env: None, reason: None, remember: false }); - } - assert_eq!(plan(Dx12, None, None, false, Os::Windows).backend, Dx12); - assert_eq!(plan(Gl, None, None, false, Os::Other).backend, Gl); - // A backend the platform lacks is `auto`. - assert_eq!(plan(Metal, None, None, false, Os::Windows).backend, Auto); - assert_eq!(plan(Dx12, None, None, false, Os::Mac).backend, Auto); - assert_eq!(plan(Dx12, None, None, false, Os::Other).backend, Auto); - } - - #[test] - fn a_crash_moves_down_each_chain_and_is_remembered() { - // Windows: Vulkan → DX12 → CPU. - let p = plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Windows); - assert_eq!((p.backend, p.remember), (Dx12, true)); - assert!(p.reason.as_deref().unwrap_or("").contains("vulkan (Intel(R) UHD Graphics)"), "{p:?}"); - assert_eq!(plan(Dx12, Some(&crashed("dx12", "dx12")), None, false, Os::Windows).backend, Cpu); - // Crashed before the adapter was chosen (enumeration): from `auto`. - assert_eq!(plan(Auto, Some(&crashed("auto", "")), None, false, Os::Windows).backend, Dx12); - // `auto` that had already picked DX12 (the Intel default) goes to the CPU. - assert_eq!(plan(Auto, Some(&crashed("auto", "dx12")), None, false, Os::Windows).backend, Cpu); - // Linux: Vulkan → GL → CPU. - assert_eq!(plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Other).backend, Gl); - assert_eq!(plan(Gl, Some(&crashed("gl", "gl")), None, false, Os::Other).backend, Cpu); - // macOS: Metal → CPU. - assert_eq!(plan(Auto, Some(&crashed("auto", "metal")), None, false, Os::Mac).backend, Cpu); - // The CPU path stays there, and isn't "remembered" again. - let p = plan(Cpu, Some(&crashed("cpu", "dx12")), None, false, Os::Windows); - assert_eq!((p.backend, p.remember), (Cpu, false)); - // An unreadable marker still counts as a crash. - assert_eq!(plan(Auto, Some(&Marker::parse("{garbage")), None, false, Os::Windows).backend, Dx12); - } - - #[test] - fn env_override_beats_everything() { - let p = plan(Cpu, Some(&crashed("auto", "vulkan")), Some("vulkan"), false, Os::Windows); - assert_eq!(p.env.as_deref(), Some("vulkan")); - assert_eq!(p.backend, Auto); - assert!(!p.remember); - assert_eq!(backends(&p, Os::Windows), None, "egui reads WGPU_BACKEND itself"); - // A crash under the override doesn't count against `auto` later. - let p = plan(Auto, Some(&Marker { backend: "env:vulkan".into(), ..Default::default() }), None, false, Os::Windows); - assert_eq!(p.backend, Auto); - // Empty means unset. - assert_eq!(plan(Auto, None, Some(" "), false, Os::Windows).env, None); - // --safe-gpu with the override: CPU canvas, the override's backends. - assert_eq!(plan(Auto, None, Some("dx12"), true, Os::Windows).backend, Cpu); - } - - /// #3483: A temporary CPU recovery must not change the next launch. - #[test] - fn cpu_recovery_requires_explicit_confirmation_to_persist() { - let gl_crash = crashed("gl", "gl"); - let cpu = plan_with_mode(Auto, RenderingMode::Auto, Some(&gl_crash), None, false, Os::Other); - assert_eq!((cpu.backend, cpu.remember), (Cpu, true)); - assert_eq!(backend_to_remember(&cpu), None); - - let safe = plan_with_mode(Auto, RenderingMode::Auto, None, None, true, Os::Other); - assert_eq!(safe.backend, Cpu); - assert_eq!(backend_to_remember(&safe), None); - // An unconfirmed fallback leaves the original automatic selection intact. - assert_eq!(plan_with_mode(Auto, RenderingMode::Auto, None, None, false, Os::Other).backend, Auto); - // A working hardware fallback can still be remembered. - let vk_crash = crashed("auto", "vulkan"); - let gl = plan_with_mode(Auto, RenderingMode::Auto, Some(&vk_crash), None, false, Os::Other); - assert_eq!(backend_to_remember(&gl), Some(Gl)); - // CPU explicitly selected in preferences still works. - assert_eq!(plan_with_mode(Auto, RenderingMode::Cpu, None, None, false, Os::Other).backend, Cpu); - } - - #[test] - fn safe_gpu_forces_the_cpu_for_one_launch() { - let p = plan(Vulkan, Some(&crashed("vulkan", "vulkan")), None, true, Os::Other); - assert_eq!((p.backend, p.remember), (Cpu, false)); - assert_eq!(backends(&p, Os::Windows), Some(wgpu::Backends::DX12)); - assert_eq!(backends(&p, Os::Other), Some(wgpu::Backends::GL | wgpu::Backends::VULKAN)); - assert_eq!(backends(&p, Os::Mac), Some(wgpu::Backends::METAL)); - } - - fn cand(vendor: u32, device_type: wgpu::DeviceType, backend: wgpu::Backend) -> Candidate { - Candidate { vendor, device_type, backend, surface_ok: true } - } - - #[test] - fn intel_on_windows_defaults_to_dx12() { - use wgpu::{Backend as B, DeviceType as T}; - let intel = [cand(INTEL, T::IntegratedGpu, B::Vulkan), cand(INTEL, T::IntegratedGpu, B::Dx12), cand(INTEL, T::IntegratedGpu, B::Gl)]; - let i = pick(&intel, Auto, Os::Windows).unwrap(); - assert_eq!(intel[i].backend, B::Dx12); - assert!(intel_dx12_applied(&intel, i, Os::Windows)); - // Elsewhere (and for other vendors) Vulkan stays first. - assert_eq!(intel[pick(&intel, Auto, Os::Other).unwrap()].backend, B::Vulkan); - let nv = [cand(0x10de, T::DiscreteGpu, B::Dx12), cand(0x10de, T::DiscreteGpu, B::Vulkan)]; - let i = pick(&nv, Auto, Os::Windows).unwrap(); - assert_eq!(nv[i].backend, B::Vulkan); - assert!(!intel_dx12_applied(&nv, i, Os::Windows)); - // Intel iGPU + discrete GPU: the discrete one wins (high performance), on Vulkan. - let hybrid = [intel[0], intel[1], nv[1]]; - assert_eq!(pick(&hybrid, Auto, Os::Windows), Some(2)); - // Adapters that can't present lose to ones that can. - let mut only = [cand(0x10de, T::DiscreteGpu, B::Vulkan), intel[1]]; - only[0].surface_ok = false; - assert_eq!(pick(&only, Auto, Os::Windows), Some(1)); - assert_eq!(pick(&[], Auto, Os::Windows), None); - } - - #[test] - fn cpu_prefers_software_adapters() { - use wgpu::{Backend as B, DeviceType as T}; - let a = [cand(INTEL, T::IntegratedGpu, B::Dx12), cand(0x1414, T::Cpu, B::Dx12)]; - assert_eq!(pick(&a, Cpu, Os::Windows), Some(1)); - // No software adapter: still something. - assert_eq!(pick(&a[..1], Cpu, Os::Windows), Some(0)); - } - - /// #1337: on Linux with NVIDIA, the only GL adapter (NVIDIA's EGL) can't present, so CPU mode - /// found no adapter and never started. GL still wins whenever it can present. - #[test] - fn cpu_on_linux_falls_back_to_vulkan_only_when_gl_cannot_present() { - use wgpu::{Backend as B, DeviceType as T}; - const NVIDIA: u32 = 0x10de; - let llvmpipe = cand(0x10005, T::Cpu, B::Gl); - let nvidia_gl = cand(NVIDIA, T::DiscreteGpu, B::Gl); - let nvidia_vk = cand(NVIDIA, T::DiscreteGpu, B::Vulkan); - let lavapipe = cand(0x10005, T::Cpu, B::Vulkan); - // GL presents: same choice as GL-only, software first, then hardware GL over any Vulkan. - assert_eq!(pick(&[nvidia_vk, lavapipe, llvmpipe], Cpu, Os::Other), Some(2)); - assert_eq!(pick(&[nvidia_vk, lavapipe, nvidia_gl], Cpu, Os::Other), Some(2)); - // GL can't present: Vulkan for the window, software first. - let mut gl = nvidia_gl; - gl.surface_ok = false; - assert_eq!(pick(&[gl, nvidia_vk, lavapipe], Cpu, Os::Other), Some(2)); - assert_eq!(pick(&[gl, nvidia_vk], Cpu, Os::Other), Some(1)); - // Other backends rank as before. - assert_eq!(pick(&[nvidia_gl, nvidia_vk], Auto, Os::Other), Some(1)); - } - - /// #5191, #1788: a Windows VM offers VMware's SVGA3D over GL (`Other`) and WARP (`Cpu`, DX12). - /// GL drew a black window; WARP works. GL stays the last resort. - #[test] - fn windows_auto_prefers_warp_over_gl() { - use wgpu::{Backend as B, DeviceType as T}; - let svga_gl = cand(0x15ad, T::Other, B::Gl); - let warp = cand(0x1414, T::Cpu, B::Dx12); - assert_eq!(pick(&[svga_gl, warp], Auto, Os::Windows), Some(1)); - assert_eq!(pick(&[cand(INTEL, T::IntegratedGpu, B::Gl), warp], Auto, Os::Windows), Some(1)); - assert_eq!(pick(&[svga_gl], Auto, Os::Windows), Some(0)); - // Elsewhere GL keeps its rank. - assert_eq!(pick(&[svga_gl, warp], Auto, Os::Other), Some(0)); - } - - #[test] - fn adapters_must_present_even_when_none_support_the_surface() { - let mut a = cand(INTEL, wgpu::DeviceType::IntegratedGpu, wgpu::Backend::Dx12); - a.surface_ok = false; - assert_eq!(pick(&[a], Auto, Os::Windows), None); - assert_eq!(pick(&[a], Cpu, Os::Windows), None); - } - - #[test] - fn explicit_policy_overrides_legacy_cpu_backend() { - assert_eq!(plan_with_mode(Cpu, RenderingMode::Gpu, None, None, false, Os::Mac).backend, Auto); - assert_eq!(plan_with_mode(Metal, RenderingMode::Cpu, None, None, false, Os::Mac).backend, Cpu); - assert_eq!(plan_with_mode(Auto, RenderingMode::Gpu, None, None, true, Os::Mac).backend, Cpu); - } - - #[test] - fn marker_round_trip_and_lenient_parse() { - let m = Marker { - backend: "auto".into(), - adapter: "A \"quoted\" GPU".into(), - adapter_backend: "vulkan".into(), - driver: "1.2".into(), - version: "0.1".into(), - }; - assert_eq!(Marker::parse(&m.to_json()), m); - assert_eq!(Marker::parse(""), Marker::default()); - assert_eq!(Marker::parse("[1,2]").tried(), Some(Auto)); - assert_eq!(Marker::parse(r#"{"backend": 7}"#).tried(), Some(Auto)); - assert_eq!(Marker::parse(r#"{"backend": "env:dx12"}"#).tried(), None); - let long = format!(r#"{{"adapter": "{}"}}"#, "x".repeat(10_000)); - assert_eq!(Marker::parse(&long).adapter.len(), 256); - } - - #[test] - fn a_failed_start_keeps_its_marker_only_when_a_safer_backend_exists() { - // GPU backends move down the chain on the next start. - assert!(keep_marker_after_error(&plan(Auto, None, None, false, Os::Other), Os::Other)); - assert!(keep_marker_after_error(&plan(Gl, None, None, false, Os::Other), Os::Other)); - assert!(keep_marker_after_error(&plan(Dx12, None, None, false, Os::Windows), Os::Windows)); - assert!(keep_marker_after_error(&plan(Auto, None, None, false, Os::Mac), Os::Mac)); - // The CPU path is the end of every chain, including the `--safe-gpu` retry. - for os in [Os::Windows, Os::Mac, Os::Other] { - assert!(!keep_marker_after_error(&plan(Cpu, None, None, false, os), os)); - assert!(!keep_marker_after_error(&plan(Auto, None, None, true, os), os)); - } - // `WGPU_BACKEND` is the user's choice; no plan reads its marker. - assert!(!keep_marker_after_error(&plan(Auto, None, Some("vulkan"), false, Os::Other), Os::Other)); - } - - /// A failed CPU start (no usable GL adapter under XWayland) left a `cpu` marker, so every - /// later start was planned on CPU and failed the same way until the marker was deleted by hand. - #[test] - fn a_failed_cpu_start_does_not_pin_later_starts() { - let dir = temp_dir("cpu-error"); - let (_, s) = Sentinel::begin(&dir); - let mut s = s.expect("sentinel"); - let failed = plan(Auto, None, None, true, Os::Other); - s.write(Marker { backend: failed.backend.name().into(), ..Default::default() }).unwrap(); - // eframe returned an error: what `main` does with the marker. - if keep_marker_after_error(&failed, Os::Other) { - drop(s); - } else { - s.finish(); - } - let (prev, _s) = begin_when_unlocked(&dir); - assert_eq!(prev, Previous::Clean); - assert_eq!(plan(Auto, prev.crashed(), None, false, Os::Other).backend, Auto); - let _ = std::fs::remove_dir_all(&dir); - } - - fn temp_dir(name: &str) -> PathBuf { - let d = std::env::temp_dir().join(format!("photocraft-gpu-startup-{name}-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&d); - d - } - - /// `Sentinel::begin`, called again up to 50 times, 10 ms apart, while it returns - /// `Previous::Busy`. Other tests in this binary spawn processes: between fork and exec a child - /// briefly shares the marker's flock, so `begin` can see the lock as busy just after a test - /// drops its sentinel. - fn begin_when_unlocked(dir: &Path) -> (Previous, Option) { - let (mut prev, mut s) = Sentinel::begin(dir); - for _ in 0..50 { - if prev != Previous::Busy { - break; - } - std::thread::sleep(std::time::Duration::from_millis(10)); - (prev, s) = Sentinel::begin(dir); - } - (prev, s) - } - - #[test] - fn sentinel_detects_a_crash_and_clears_on_success() { - let dir = temp_dir("cycle"); - // First launch: nothing left behind. - let (prev, s) = Sentinel::begin(&dir); - assert_eq!(prev, Previous::Clean); - let mut s = s.expect("sentinel"); - s.write(Marker { backend: "auto".into(), ..Default::default() }).unwrap(); - s.write(Marker { backend: "auto".into(), adapter: "GPU".into(), adapter_backend: "vulkan".into(), ..Default::default() }).unwrap(); - // While it is starting, a second instance sees it busy, not crashed. - let (busy, none) = Sentinel::begin(&dir); - assert_eq!(busy, Previous::Busy); - assert!(none.is_none()); - // The process dies in the driver: the lock goes, the marker stays. - drop(s); - let (prev, s) = begin_when_unlocked(&dir); - let m = prev.crashed().cloned().expect("crash detected"); - assert_eq!((m.adapter.as_str(), m.tried()), ("GPU", Some(Vulkan))); - // This start succeeds: the next one finds nothing. - let mut s = s.expect("sentinel"); - s.write(Marker { backend: "dx12".into(), ..Default::default() }).unwrap(); - s.finish(); - assert!(!dir.join(MARKER_FILE).exists()); - let (prev, _s) = Sentinel::begin(&dir); - assert_eq!(prev, Previous::Clean); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn begin_when_unlocked_waits_for_a_briefly_held_lock() { - let dir = temp_dir("shared-lock"); - let (_, s) = Sentinel::begin(&dir); - let mut s = s.expect("sentinel"); - s.write(Marker { backend: "vulkan".into(), ..Default::default() }).unwrap(); - // After the start dies, a second handle on the marker holds the lock for 50 ms, as a - // spawned child's copy of the descriptor does until the child calls exec. - let held = File::open(dir.join(MARKER_FILE)).unwrap(); - drop(s); - held.lock().unwrap(); - assert_eq!(Sentinel::begin(&dir).0, Previous::Busy); - let release = std::thread::spawn(move || { - std::thread::sleep(std::time::Duration::from_millis(50)); - held.unlock().unwrap(); - }); - let (prev, s) = begin_when_unlocked(&dir); - release.join().unwrap(); - assert_eq!(prev.crashed().map(|m| m.backend.as_str()), Some("vulkan")); - s.expect("sentinel").finish(); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn returned_non_graphics_errors_do_not_trigger_backend_recovery() { - let cases = [ - ("success", Ok(()), false), - ("app-error", Err(eframe::Error::AppCreation(Box::new(std::io::Error::other("app setup failed")))), false), - ("gpu-error", Err(eframe::Error::Wgpu(egui_wgpu::WgpuError::CustomNativeAdapterSelectionError("no adapter".into()))), true), - ]; - for (name, result, keep) in cases { - let dir = temp_dir(name); - let (_, s) = Sentinel::begin(&dir); - let mut s = s.expect("sentinel"); - s.write(Marker { backend: "vulkan".into(), ..Default::default() }).unwrap(); - if keep_marker_after_run(&result) { - drop(s); - } else { - s.finish(); - } - let (previous, s) = begin_when_unlocked(&dir); - assert_eq!(previous.crashed().is_some(), keep, "{name}: {result:?}"); - if !keep { - assert_eq!(plan(Vulkan, previous.crashed(), None, false, Os::Other).backend, Vulkan); - } - s.expect("sentinel").finish(); - let _ = std::fs::remove_dir_all(&dir); - } - } - - #[test] - fn unwritable_config_dir_is_not_fatal() { - let dir = temp_dir("file"); - std::fs::create_dir_all(dir.parent().unwrap()).unwrap(); - std::fs::write(&dir, b"not a directory").unwrap(); - let (prev, s) = Sentinel::begin(&dir.join("sub")); - assert_eq!(prev, Previous::Clean); - assert!(s.is_none()); - let _ = std::fs::remove_file(&dir); - } - - #[test] - fn prefs_are_read_leniently() { - let dir = temp_dir("prefs"); - std::fs::create_dir_all(&dir).unwrap(); - let p = dir.join("preferences.json"); - assert_eq!(read_rendering_prefs(None), (Auto, RenderingMode::Auto)); - assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); - std::fs::write(&p, r#"{"performance": {"gpuBackend": "dx12", "useGpu": false}, "interface": {}}"#).unwrap(); - assert_eq!(read_rendering_prefs(Some(&p)), (Dx12, RenderingMode::Cpu)); - std::fs::write(&p, r#"{"performance": {"gpuBackend": "quantum"}}"#).unwrap(); - assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); - std::fs::write(&p, r#"{"performance": {"useGpu": false, "renderingMode": "gpu"}}"#).unwrap(); - assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Gpu)); - std::fs::write(&p, "{not json").unwrap(); - assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); - let _ = std::fs::remove_dir_all(&dir); - } - - /// #712: never a `dxcompiler.dll` found through the DLL search path (current directory, PATH). - #[test] - fn dx12_compiler_is_fxc_or_dxc_beside_the_exe() { - use wgpu::Dx12Compiler as C; - let dir = temp_dir("dxc"); - std::fs::create_dir_all(&dir).unwrap(); - assert!(matches!(dx12_compiler(None, None), C::Fxc)); - assert!(matches!(dx12_compiler(None, Some(&dir)), C::Fxc)); - std::fs::write(dir.join("dxcompiler.dll"), b"MZ").unwrap(); - let expected = dir.join("dxcompiler.dll"); - assert!(matches!(dx12_compiler(None, Some(&dir)), C::DynamicDxc { dxc_path } if Path::new(&dxc_path) == expected)); - // An explicit WGPU_DX12_COMPILER wins; an unknown value is ignored. - assert!(matches!(dx12_compiler(Some(" FXC "), Some(&dir)), C::Fxc)); - assert!(matches!(dx12_compiler(Some("dxc"), None), C::DynamicDxc { dxc_path } if dxc_path == "dxcompiler.dll")); - assert!(matches!(dx12_compiler(Some("quantum"), None), C::Fxc)); - let _ = std::fs::remove_dir_all(&dir); - - // The app's setup replaces wgpu's default (`Auto`: DXC by name from the search path). - let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); - let plan = plan(Auto, None, None, false, Os::Windows); - configure(&mut setup, &plan, Os::Windows, Arc::new(Mutex::new(None)), Arc::default()); - let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; - if std::env::var_os("WGPU_DX12_COMPILER").is_none() { - let safe = match &create.instance_descriptor.backend_options.dx12.shader_compiler { - C::Fxc => true, - C::DynamicDxc { dxc_path } => Path::new(dxc_path).is_absolute(), - C::StaticDxc | C::Auto => false, - }; - assert!(safe, "{:?}", create.instance_descriptor.backend_options.dx12.shader_compiler); - } - } - - #[test] - fn intel_macs_skip_the_indirect_validation_shaders() { - // Metal's compiler crashed on them on Intel Iris Pro, losing the device in every mode (#3107). - assert!(skips_indirect_validation(Os::Mac, true)); - // Apple silicon, Windows and Linux keep wgpu's validation. - for (os, intel) in [(Os::Mac, false), (Os::Windows, true), (Os::Other, true), (Os::Other, false)] { - assert!(!skips_indirect_validation(os, intel), "{os:?} intel={intel}"); - } - for (os, safe_gpu) in [(Os::Windows, false), (Os::Other, true), (Os::Other, false)] { - let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); - let egui_wgpu::WgpuSetup::CreateNew(before) = &setup else { panic!("a new instance") }; - let before = before.instance_descriptor.flags; - configure(&mut setup, &plan(Auto, None, None, safe_gpu, os), os, Arc::new(Mutex::new(None)), Arc::default()); - let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; - assert_eq!(create.instance_descriptor.flags, before, "{os:?}: flags untouched"); - } - if cfg!(target_arch = "x86_64") && std::env::var_os("WGPU_VALIDATION_INDIRECT_CALL").is_none() { - let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); - configure(&mut setup, &plan(Auto, None, None, false, Os::Mac), Os::Mac, Arc::new(Mutex::new(None)), Arc::default()); - let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; - assert!(!create.instance_descriptor.flags.contains(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL)); - } - } -} +mod tests; diff --git a/apps/photocraft/src/gpu_startup/tests.rs b/apps/photocraft/src/gpu_startup/tests.rs new file mode 100644 index 0000000000..ee1f0ce1e6 --- /dev/null +++ b/apps/photocraft/src/gpu_startup/tests.rs @@ -0,0 +1,595 @@ +use super::*; +use GpuBackend::*; + +fn crashed(backend: &str, adapter_backend: &str) -> Marker { + Marker { backend: backend.into(), adapter: "Intel(R) UHD Graphics".into(), adapter_backend: adapter_backend.into(), ..Default::default() } +} + +#[test] +fn low_latency_drawing_is_read_before_the_window_opens_and_defaults_off() { + let dir = std::env::temp_dir().join(format!("photocraft-low-latency-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("preferences.json"); + let read = |json: &str| { + std::fs::write(&file, json).unwrap(); + read_low_latency(Some(&file)) + }; + assert!(read(r#"{"performance":{"lowLatencyDrawing":true}}"#)); + assert!(!read(r#"{"performance":{"lowLatencyDrawing":false}}"#)); + assert!(!read(r#"{"performance":{"lowLatencyDrawing":"nope"}}"#), "garbage keeps the default (off)"); + assert!(!read("not json")); + assert!(!read_low_latency(None), "no settings file: the default (off)"); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn display_server_preference_is_read_before_the_window_opens() { + use photocraft_engine::prefs::LinuxDisplayServer; + let dir = std::env::temp_dir().join(format!("photocraft-display-server-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("preferences.json"); + let read = |json: &str| { + std::fs::write(&file, json).unwrap(); + read_display_server(Some(&file)) + }; + assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"x11"}}"#), LinuxDisplayServer::X11); + assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"auto","gpuBackend":"vulkan"}}"#), LinuxDisplayServer::Auto); + // Unknown values, other types, broken JSON and a missing file all start as Auto. + assert_eq!(read(r#"{"performance":{"linuxDisplayServer":"wayland"}}"#), LinuxDisplayServer::Auto); + assert_eq!(read(r#"{"performance":{"linuxDisplayServer":11}}"#), LinuxDisplayServer::Auto); + assert_eq!(read("{not json"), LinuxDisplayServer::Auto); + assert_eq!(read_display_server(Some(&dir.join("missing.json"))), LinuxDisplayServer::Auto); + assert_eq!(read_display_server(None), LinuxDisplayServer::Auto); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn cpu_policy_ignores_hardware_backend_override() { + let p = plan_with_mode(Vulkan, RenderingMode::Cpu, None, Some("vulkan"), false, Os::Windows); + assert_eq!(p.backend, Cpu); + assert!(p.env.is_none()); + assert_eq!(plan_with_mode(Vulkan, RenderingMode::Cpu, Some(&crashed("vulkan", "vulkan")), None, false, Os::Windows).backend, Cpu); + let p = plan_with_mode(Auto, RenderingMode::Gpu, None, Some("vulkan"), true, Os::Windows); + assert_eq!(p.backend, Cpu); + assert!(p.env.is_none()); +} + +#[test] +fn no_marker_uses_the_preference() { + for os in [Os::Windows, Os::Mac, Os::Other] { + let p = plan(Auto, None, None, false, os); + assert_eq!(p, Plan { backend: Auto, env: None, reason: None, remember: false }); + } + assert_eq!(plan(Dx12, None, None, false, Os::Windows).backend, Dx12); + assert_eq!(plan(Gl, None, None, false, Os::Other).backend, Gl); + // A backend the platform lacks is `auto`. + assert_eq!(plan(Metal, None, None, false, Os::Windows).backend, Auto); + assert_eq!(plan(Dx12, None, None, false, Os::Mac).backend, Auto); + assert_eq!(plan(Dx12, None, None, false, Os::Other).backend, Auto); +} + +#[test] +fn a_crash_moves_down_each_chain_and_is_remembered() { + // Windows: Vulkan → DX12 → CPU. + let p = plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Windows); + assert_eq!((p.backend, p.remember), (Dx12, true)); + assert!(p.reason.as_deref().unwrap_or("").contains("vulkan (Intel(R) UHD Graphics)"), "{p:?}"); + assert_eq!(plan(Dx12, Some(&crashed("dx12", "dx12")), None, false, Os::Windows).backend, Cpu); + // Crashed before the adapter was chosen (enumeration): from `auto`. + assert_eq!(plan(Auto, Some(&crashed("auto", "")), None, false, Os::Windows).backend, Dx12); + // `auto` that had already picked DX12 (the Intel default) goes to the CPU. + assert_eq!(plan(Auto, Some(&crashed("auto", "dx12")), None, false, Os::Windows).backend, Cpu); + // Linux: Vulkan → GL → CPU. + assert_eq!(plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Other).backend, Gl); + assert_eq!(plan(Gl, Some(&crashed("gl", "gl")), None, false, Os::Other).backend, Cpu); + // macOS: Metal → CPU. + assert_eq!(plan(Auto, Some(&crashed("auto", "metal")), None, false, Os::Mac).backend, Cpu); + // The CPU path stays there, and isn't "remembered" again. + let p = plan(Cpu, Some(&crashed("cpu", "dx12")), None, false, Os::Windows); + assert_eq!((p.backend, p.remember), (Cpu, false)); + // An unreadable marker still counts as a crash. + assert_eq!(plan(Auto, Some(&Marker::parse("{garbage")), None, false, Os::Windows).backend, Dx12); +} + +#[test] +fn env_override_beats_everything() { + let p = plan(Cpu, Some(&crashed("auto", "vulkan")), Some("vulkan"), false, Os::Windows); + assert_eq!(p.env.as_deref(), Some("vulkan")); + assert_eq!(p.backend, Auto); + assert!(!p.remember); + assert_eq!(backends(&p, Os::Windows), None, "egui reads WGPU_BACKEND itself"); + // A crash under the override doesn't count against `auto` later. + let p = plan(Auto, Some(&Marker { backend: "env:vulkan".into(), ..Default::default() }), None, false, Os::Windows); + assert_eq!(p.backend, Auto); + // Empty means unset. + assert_eq!(plan(Auto, None, Some(" "), false, Os::Windows).env, None); + // --safe-gpu with the override: CPU canvas, the override's backends. + assert_eq!(plan(Auto, None, Some("dx12"), true, Os::Windows).backend, Cpu); +} + +/// #3483: A temporary CPU recovery must not change the next launch. +#[test] +fn cpu_recovery_requires_explicit_confirmation_to_persist() { + let gl_crash = crashed("gl", "gl"); + let cpu = plan_with_mode(Auto, RenderingMode::Auto, Some(&gl_crash), None, false, Os::Other); + assert_eq!((cpu.backend, cpu.remember), (Cpu, true)); + assert_eq!(backend_to_remember(&cpu), None); + + let safe = plan_with_mode(Auto, RenderingMode::Auto, None, None, true, Os::Other); + assert_eq!(safe.backend, Cpu); + assert_eq!(backend_to_remember(&safe), None); + // An unconfirmed fallback leaves the original automatic selection intact. + assert_eq!(plan_with_mode(Auto, RenderingMode::Auto, None, None, false, Os::Other).backend, Auto); + // A working hardware fallback can still be remembered. + let vk_crash = crashed("auto", "vulkan"); + let gl = plan_with_mode(Auto, RenderingMode::Auto, Some(&vk_crash), None, false, Os::Other); + assert_eq!(backend_to_remember(&gl), Some(Gl)); + // CPU explicitly selected in preferences still works. + assert_eq!(plan_with_mode(Auto, RenderingMode::Cpu, None, None, false, Os::Other).backend, Cpu); +} + +#[test] +fn safe_gpu_forces_the_cpu_for_one_launch() { + let p = plan(Vulkan, Some(&crashed("vulkan", "vulkan")), None, true, Os::Other); + assert_eq!((p.backend, p.remember), (Cpu, false)); + assert_eq!(backends(&p, Os::Windows), Some(wgpu::Backends::DX12)); + assert_eq!(backends(&p, Os::Other), Some(wgpu::Backends::GL | wgpu::Backends::VULKAN)); + assert_eq!(backends(&p, Os::Mac), Some(wgpu::Backends::METAL)); +} + +fn cand(vendor: u32, device_type: wgpu::DeviceType, backend: wgpu::Backend) -> Candidate { + Candidate { vendor, device_type, backend, surface_ok: true, wanted: false } +} + +#[test] +fn a_requested_adapter_beats_the_automatic_ranking() { + use wgpu::{Backend as B, DeviceType as T}; + // The order Vulkan lists this machine's GPUs in: iGPU, dGPU, eGPU. + let mut a = [cand(INTEL, T::IntegratedGpu, B::Vulkan), cand(INTEL, T::DiscreteGpu, B::Vulkan), cand(0x10de, T::DiscreteGpu, B::Vulkan)]; + assert_eq!(pick(&a, Auto, Os::Other), Some(1), "automatic takes the first discrete adapter, the Arc"); + a[2].wanted = true; + assert_eq!(pick(&a, Auto, Os::Other), Some(2)); + a[2].wanted = false; + a[0].wanted = true; + assert_eq!(pick(&a, Auto, Os::Other), Some(0), "even the iGPU, when asked for"); + // A wanted adapter that can't present is skipped, not an error. + a[0].surface_ok = false; + assert_eq!(pick(&a, Auto, Os::Other), Some(1)); + assert!(wanted_missed(&a, Some("iris"))); + assert!(!wanted_missed(&a, None)); + a[0].surface_ok = true; + assert!(!wanted_missed(&a, Some("iris"))); + // Among several wanted ones (same GPU on Vulkan and GL) the usual ranking decides. + let mut gl = [cand(0x10de, T::DiscreteGpu, B::Gl), cand(0x10de, T::DiscreteGpu, B::Vulkan)]; + gl.iter_mut().for_each(|c| c.wanted = true); + assert_eq!(pick(&gl, Auto, Os::Other), Some(1)); +} + +#[test] +fn an_adapter_request_applies_on_linux_outside_cpu_mode() { + let w = || Some("nvidia".to_string()); + assert_eq!(applicable_adapter(w(), Auto, true), w()); + assert_eq!(applicable_adapter(w(), Vulkan, true), w()); + assert_eq!(applicable_adapter(w(), Cpu, true), None); + assert_eq!(applicable_adapter(w(), Auto, false), None, "Windows and macOS keep their own selection"); + assert_eq!(applicable_adapter(None, Auto, true), None); +} + +#[test] +fn an_adapter_override_keeps_its_own_process_only_when_it_applies() { + let on = |env, mode, linux| adapter_override_is_process_specific(env, mode, linux); + assert!(on(Some("nvidia"), RenderingMode::Auto, true)); + assert!(on(Some("nvidia"), RenderingMode::Gpu, true)); + assert!(!on(Some("nvidia"), RenderingMode::Cpu, true), "ignored in CPU mode"); + assert!(!on(Some("nvidia"), RenderingMode::Auto, false), "ignored off Linux"); + assert!(!on(Some(" "), RenderingMode::Auto, true)); + assert!(!on(None, RenderingMode::Auto, true)); +} + +#[test] +fn adapter_names_match_loosely_and_the_environment_wins() { + assert!(adapter_matches("NVIDIA GeForce RTX 5060 Ti", "nvidia")); + assert!(adapter_matches("Intel(R) Arc(tm) A370M Graphics (DG2)", " ARC A370M ")); + assert!(!adapter_matches("Intel(R) Iris(R) Xe Graphics (ADL GT2)", "arc")); + assert!(!adapter_matches("anything", "")); + assert!(!adapter_matches("anything", " ")); + assert_eq!(wanted_adapter(None, ""), None); + assert_eq!(wanted_adapter(Some(" "), "iris"), Some("iris".into())); + assert_eq!(wanted_adapter(Some("nvidia"), "iris"), Some("nvidia".into())); + assert_eq!(wanted_adapter(Some(&"x".repeat(1000)), "").map(|w| w.len()), Some(256)); +} + +#[test] +fn gpu_adapter_is_read_before_the_window_opens() { + let dir = std::env::temp_dir().join(format!("photocraft-gpu-adapter-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("preferences.json"); + let read = |json: &str| { + std::fs::write(&file, json).unwrap(); + read_gpu_adapter(Some(&file)) + }; + assert_eq!(read(r#"{"performance":{"gpuAdapter":"nvidia"}}"#), "nvidia"); + assert_eq!(read(r#"{"performance":{"gpuAdapter":7}}"#), ""); + assert_eq!(read(r#"{"performance":{}}"#), ""); + assert_eq!(read("not json"), ""); + assert_eq!(read_gpu_adapter(None), ""); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn intel_on_windows_defaults_to_dx12() { + use wgpu::{Backend as B, DeviceType as T}; + let intel = [cand(INTEL, T::IntegratedGpu, B::Vulkan), cand(INTEL, T::IntegratedGpu, B::Dx12), cand(INTEL, T::IntegratedGpu, B::Gl)]; + let i = pick(&intel, Auto, Os::Windows).unwrap(); + assert_eq!(intel[i].backend, B::Dx12); + assert_eq!(dx12_default_applied(&intel, i, Os::Windows, false), Some("Intel")); + // Elsewhere (and for other vendors) Vulkan stays first. + assert_eq!(intel[pick(&intel, Auto, Os::Other).unwrap()].backend, B::Vulkan); + let nv = [cand(0x10de, T::DiscreteGpu, B::Dx12), cand(0x10de, T::DiscreteGpu, B::Vulkan)]; + let i = pick(&nv, Auto, Os::Windows).unwrap(); + assert_eq!(nv[i].backend, B::Vulkan); + assert_eq!(dx12_default_applied(&nv, i, Os::Windows, false), None); + // Intel iGPU + discrete GPU: the discrete one wins (high performance), on Vulkan. + let hybrid = [intel[0], intel[1], nv[1]]; + assert_eq!(pick(&hybrid, Auto, Os::Windows), Some(2)); + // Adapters that can't present lose to ones that can. + let mut only = [cand(0x10de, T::DiscreteGpu, B::Vulkan), intel[1]]; + only[0].surface_ok = false; + assert_eq!(pick(&only, Auto, Os::Windows), Some(1)); + assert_eq!(pick(&[], Auto, Os::Windows), None); +} + +/// #7537: on Windows on ARM the Adreno Vulkan driver lost the device mid-session and took the +/// window with it; DX12 on the same machine didn't. DXGI and Vulkan report different vendor ids. +#[test] +fn qualcomm_on_windows_defaults_to_dx12() { + use wgpu::{Backend as B, DeviceType as T}; + let adreno = [cand(QUALCOMM, T::IntegratedGpu, B::Vulkan), cand(QUALCOMM_DXGI, T::IntegratedGpu, B::Dx12)]; + let i = pick(&adreno, Auto, Os::Windows).unwrap(); + assert_eq!(adreno[i].backend, B::Dx12); + assert_eq!(dx12_default_applied(&adreno, i, Os::Windows, false), Some("Qualcomm")); + // Only Vulkan offered: still Vulkan, and no note. + assert_eq!(pick(&adreno[..1], Auto, Os::Windows), Some(0)); + assert_eq!(dx12_default_applied(&adreno, 0, Os::Windows, false), None); + // An Intel Vulkan adapter doesn't make a Qualcomm DX12 pick "the default". + let mixed = [cand(INTEL, T::IntegratedGpu, B::Vulkan), adreno[1]]; + assert_eq!(dx12_default_applied(&mixed, 1, Os::Windows, false), None); + // Vulkan left out of the instance (an Intel adapter was listed) speaks only for Intel. + assert_eq!(dx12_default_applied(&adreno[1..], 0, Os::Windows, true), None); + // Elsewhere Vulkan stays first. + assert_eq!(adreno[pick(&adreno, Auto, Os::Other).unwrap()].backend, B::Vulkan); +} + +/// #4: an instance with Vulkan loads the Intel Vulkan driver, which crashed at startup, before +/// the selector picks Intel's DX12 adapter. With an Intel adapter in the DX12 list, `auto` on +/// Windows creates the instance without Vulkan. +#[test] +fn auto_on_windows_with_an_intel_adapter_leaves_vulkan_out_of_the_instance() { + use wgpu::{Backend as B, DeviceType as T}; + const NVIDIA: u32 = 0x10de; + const AMD: u32 = 0x1002; + const WARP: u32 = 0x1414; + let auto = plan(Auto, None, None, false, Os::Windows); + let dx12_gl = Some(wgpu::Backends::DX12 | wgpu::Backends::GL); + assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![INTEL, WARP]), dx12_gl); + // Intel iGPU + discrete GPU: the discrete GPU is then used on DX12. + assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![NVIDIA, INTEL, WARP]), dx12_gl); + // No Intel adapter: egui's default backends, Vulkan included. + assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![NVIDIA, WARP]), None); + assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![AMD]), None); + assert_eq!(windows_auto_backends(&auto, Os::Windows, Vec::new), None); + // `WGPU_BACKEND`, `--safe-gpu`, a chosen backend, a crash fallback and other platforms + // don't list the DX12 adapters at all. + let not_listed = || -> Vec { panic!("DX12 adapters listed") }; + for (p, os) in [ + (plan(Auto, None, Some("vulkan"), false, Os::Windows), Os::Windows), + (plan(Auto, None, None, true, Os::Windows), Os::Windows), + (plan(Vulkan, None, None, false, Os::Windows), Os::Windows), + (plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Windows), Os::Windows), + (plan(Auto, None, None, false, Os::Other), Os::Other), + (plan(Auto, None, None, false, Os::Mac), Os::Mac), + ] { + assert_eq!(windows_auto_backends(&p, os, not_listed), None, "{p:?}"); + } + // Without Vulkan adapters to compare with, System Info still names the Intel default. + let listed = [cand(INTEL, T::IntegratedGpu, B::Dx12), cand(WARP, T::Cpu, B::Dx12), cand(INTEL, T::IntegratedGpu, B::Gl)]; + let i = pick(&listed, Auto, Os::Windows).unwrap(); + assert_eq!(i, 0); + assert_eq!(dx12_default_applied(&listed, i, Os::Windows, true), Some("Intel")); + assert_eq!(dx12_default_applied(&listed, i, Os::Windows, false), None); +} + +#[test] +fn cpu_prefers_software_adapters() { + use wgpu::{Backend as B, DeviceType as T}; + let a = [cand(INTEL, T::IntegratedGpu, B::Dx12), cand(0x1414, T::Cpu, B::Dx12)]; + assert_eq!(pick(&a, Cpu, Os::Windows), Some(1)); + // No software adapter: still something. + assert_eq!(pick(&a[..1], Cpu, Os::Windows), Some(0)); +} + +/// #1337: on Linux with NVIDIA, the only GL adapter (NVIDIA's EGL) can't present, so CPU mode +/// found no adapter and never started. GL still wins whenever it can present. +#[test] +fn cpu_on_linux_falls_back_to_vulkan_only_when_gl_cannot_present() { + use wgpu::{Backend as B, DeviceType as T}; + const NVIDIA: u32 = 0x10de; + let llvmpipe = cand(0x10005, T::Cpu, B::Gl); + let nvidia_gl = cand(NVIDIA, T::DiscreteGpu, B::Gl); + let nvidia_vk = cand(NVIDIA, T::DiscreteGpu, B::Vulkan); + let lavapipe = cand(0x10005, T::Cpu, B::Vulkan); + // GL presents: same choice as GL-only, software first, then hardware GL over any Vulkan. + assert_eq!(pick(&[nvidia_vk, lavapipe, llvmpipe], Cpu, Os::Other), Some(2)); + assert_eq!(pick(&[nvidia_vk, lavapipe, nvidia_gl], Cpu, Os::Other), Some(2)); + // GL can't present: Vulkan for the window, software first. + let mut gl = nvidia_gl; + gl.surface_ok = false; + assert_eq!(pick(&[gl, nvidia_vk, lavapipe], Cpu, Os::Other), Some(2)); + assert_eq!(pick(&[gl, nvidia_vk], Cpu, Os::Other), Some(1)); + // Other backends rank as before. + assert_eq!(pick(&[nvidia_gl, nvidia_vk], Auto, Os::Other), Some(1)); +} + +/// #5191, #1788: a Windows VM offers VMware's SVGA3D over GL (`Other`) and WARP (`Cpu`, DX12). +/// GL drew a black window; WARP works. GL stays the last resort. +#[test] +fn windows_auto_prefers_warp_over_gl() { + use wgpu::{Backend as B, DeviceType as T}; + let svga_gl = cand(0x15ad, T::Other, B::Gl); + let warp = cand(0x1414, T::Cpu, B::Dx12); + assert_eq!(pick(&[svga_gl, warp], Auto, Os::Windows), Some(1)); + assert_eq!(pick(&[cand(INTEL, T::IntegratedGpu, B::Gl), warp], Auto, Os::Windows), Some(1)); + assert_eq!(pick(&[svga_gl], Auto, Os::Windows), Some(0)); + // Elsewhere GL keeps its rank. + assert_eq!(pick(&[svga_gl, warp], Auto, Os::Other), Some(0)); +} + +#[test] +fn adapters_must_present_even_when_none_support_the_surface() { + let mut a = cand(INTEL, wgpu::DeviceType::IntegratedGpu, wgpu::Backend::Dx12); + a.surface_ok = false; + assert_eq!(pick(&[a], Auto, Os::Windows), None); + assert_eq!(pick(&[a], Cpu, Os::Windows), None); +} + +#[test] +fn explicit_policy_overrides_legacy_cpu_backend() { + assert_eq!(plan_with_mode(Cpu, RenderingMode::Gpu, None, None, false, Os::Mac).backend, Auto); + assert_eq!(plan_with_mode(Metal, RenderingMode::Cpu, None, None, false, Os::Mac).backend, Cpu); + assert_eq!(plan_with_mode(Auto, RenderingMode::Gpu, None, None, true, Os::Mac).backend, Cpu); +} + +#[test] +fn marker_round_trip_and_lenient_parse() { + let m = + Marker { backend: "auto".into(), adapter: "A \"quoted\" GPU".into(), adapter_backend: "vulkan".into(), driver: "1.2".into(), version: "0.1".into() }; + assert_eq!(Marker::parse(&m.to_json()), m); + assert_eq!(Marker::parse(""), Marker::default()); + assert_eq!(Marker::parse("[1,2]").tried(), Some(Auto)); + assert_eq!(Marker::parse(r#"{"backend": 7}"#).tried(), Some(Auto)); + assert_eq!(Marker::parse(r#"{"backend": "env:dx12"}"#).tried(), None); + let long = format!(r#"{{"adapter": "{}"}}"#, "x".repeat(10_000)); + assert_eq!(Marker::parse(&long).adapter.len(), 256); +} + +#[test] +fn a_failed_start_keeps_its_marker_only_when_a_safer_backend_exists() { + // GPU backends move down the chain on the next start. + assert!(keep_marker_after_error(&plan(Auto, None, None, false, Os::Other), Os::Other)); + assert!(keep_marker_after_error(&plan(Gl, None, None, false, Os::Other), Os::Other)); + assert!(keep_marker_after_error(&plan(Dx12, None, None, false, Os::Windows), Os::Windows)); + assert!(keep_marker_after_error(&plan(Auto, None, None, false, Os::Mac), Os::Mac)); + // The CPU path is the end of every chain, including the `--safe-gpu` retry. + for os in [Os::Windows, Os::Mac, Os::Other] { + assert!(!keep_marker_after_error(&plan(Cpu, None, None, false, os), os)); + assert!(!keep_marker_after_error(&plan(Auto, None, None, true, os), os)); + } + // `WGPU_BACKEND` is the user's choice; no plan reads its marker. + assert!(!keep_marker_after_error(&plan(Auto, None, Some("vulkan"), false, Os::Other), Os::Other)); +} + +/// A failed CPU start (no usable GL adapter under XWayland) left a `cpu` marker, so every +/// later start was planned on CPU and failed the same way until the marker was deleted by hand. +#[test] +fn a_failed_cpu_start_does_not_pin_later_starts() { + let dir = temp_dir("cpu-error"); + let (_, s) = Sentinel::begin(&dir); + let mut s = s.expect("sentinel"); + let failed = plan(Auto, None, None, true, Os::Other); + s.write(Marker { backend: failed.backend.name().into(), ..Default::default() }).unwrap(); + // eframe returned an error: what `main` does with the marker. + if keep_marker_after_error(&failed, Os::Other) { + drop(s); + } else { + s.finish(); + } + let (prev, _s) = begin_when_unlocked(&dir); + assert_eq!(prev, Previous::Clean); + assert_eq!(plan(Auto, prev.crashed(), None, false, Os::Other).backend, Auto); + let _ = std::fs::remove_dir_all(&dir); +} + +fn temp_dir(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("photocraft-gpu-startup-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + d +} + +/// `Sentinel::begin`, called again up to 50 times, 10 ms apart, while it returns +/// `Previous::Busy`. Other tests in this binary spawn processes: between fork and exec a child +/// briefly shares the marker's flock, so `begin` can see the lock as busy just after a test +/// drops its sentinel. +fn begin_when_unlocked(dir: &Path) -> (Previous, Option) { + let (mut prev, mut s) = Sentinel::begin(dir); + for _ in 0..50 { + if prev != Previous::Busy { + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + (prev, s) = Sentinel::begin(dir); + } + (prev, s) +} + +#[test] +fn sentinel_detects_a_crash_and_clears_on_success() { + let dir = temp_dir("cycle"); + // First launch: nothing left behind. + let (prev, s) = Sentinel::begin(&dir); + assert_eq!(prev, Previous::Clean); + let mut s = s.expect("sentinel"); + s.write(Marker { backend: "auto".into(), ..Default::default() }).unwrap(); + s.write(Marker { backend: "auto".into(), adapter: "GPU".into(), adapter_backend: "vulkan".into(), ..Default::default() }).unwrap(); + // While it is starting, a second instance sees it busy, not crashed. + let (busy, none) = Sentinel::begin(&dir); + assert_eq!(busy, Previous::Busy); + assert!(none.is_none()); + // The process dies in the driver: the lock goes, the marker stays. + drop(s); + let (prev, s) = begin_when_unlocked(&dir); + let m = prev.crashed().cloned().expect("crash detected"); + assert_eq!((m.adapter.as_str(), m.tried()), ("GPU", Some(Vulkan))); + // This start succeeds: the next one finds nothing. + let mut s = s.expect("sentinel"); + s.write(Marker { backend: "dx12".into(), ..Default::default() }).unwrap(); + s.finish(); + assert!(!dir.join(MARKER_FILE).exists()); + let (prev, _s) = Sentinel::begin(&dir); + assert_eq!(prev, Previous::Clean); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn begin_when_unlocked_waits_for_a_briefly_held_lock() { + let dir = temp_dir("shared-lock"); + let (_, s) = Sentinel::begin(&dir); + let mut s = s.expect("sentinel"); + s.write(Marker { backend: "vulkan".into(), ..Default::default() }).unwrap(); + // After the start dies, a second handle on the marker holds the lock for 50 ms, as a + // spawned child's copy of the descriptor does until the child calls exec. + let held = File::open(dir.join(MARKER_FILE)).unwrap(); + drop(s); + held.lock().unwrap(); + assert_eq!(Sentinel::begin(&dir).0, Previous::Busy); + let release = std::thread::spawn(move || { + std::thread::sleep(std::time::Duration::from_millis(50)); + held.unlock().unwrap(); + }); + let (prev, s) = begin_when_unlocked(&dir); + release.join().unwrap(); + assert_eq!(prev.crashed().map(|m| m.backend.as_str()), Some("vulkan")); + s.expect("sentinel").finish(); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn returned_non_graphics_errors_do_not_trigger_backend_recovery() { + let cases = [ + ("success", Ok(()), false), + ("app-error", Err(eframe::Error::AppCreation(Box::new(std::io::Error::other("app setup failed")))), false), + ("gpu-error", Err(eframe::Error::Wgpu(egui_wgpu::WgpuError::CustomNativeAdapterSelectionError("no adapter".into()))), true), + ]; + for (name, result, keep) in cases { + let dir = temp_dir(name); + let (_, s) = Sentinel::begin(&dir); + let mut s = s.expect("sentinel"); + s.write(Marker { backend: "vulkan".into(), ..Default::default() }).unwrap(); + if keep_marker_after_run(&result) { + drop(s); + } else { + s.finish(); + } + let (previous, s) = begin_when_unlocked(&dir); + assert_eq!(previous.crashed().is_some(), keep, "{name}: {result:?}"); + if !keep { + assert_eq!(plan(Vulkan, previous.crashed(), None, false, Os::Other).backend, Vulkan); + } + s.expect("sentinel").finish(); + let _ = std::fs::remove_dir_all(&dir); + } +} + +#[test] +fn unwritable_config_dir_is_not_fatal() { + let dir = temp_dir("file"); + std::fs::create_dir_all(dir.parent().unwrap()).unwrap(); + std::fs::write(&dir, b"not a directory").unwrap(); + let (prev, s) = Sentinel::begin(&dir.join("sub")); + assert_eq!(prev, Previous::Clean); + assert!(s.is_none()); + let _ = std::fs::remove_file(&dir); +} + +#[test] +fn prefs_are_read_leniently() { + let dir = temp_dir("prefs"); + std::fs::create_dir_all(&dir).unwrap(); + let p = dir.join("preferences.json"); + assert_eq!(read_rendering_prefs(None), (Auto, RenderingMode::Auto)); + assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); + std::fs::write(&p, r#"{"performance": {"gpuBackend": "dx12", "useGpu": false}, "interface": {}}"#).unwrap(); + assert_eq!(read_rendering_prefs(Some(&p)), (Dx12, RenderingMode::Cpu)); + std::fs::write(&p, r#"{"performance": {"gpuBackend": "quantum"}}"#).unwrap(); + assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); + std::fs::write(&p, r#"{"performance": {"useGpu": false, "renderingMode": "gpu"}}"#).unwrap(); + assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Gpu)); + std::fs::write(&p, "{not json").unwrap(); + assert_eq!(read_rendering_prefs(Some(&p)), (Auto, RenderingMode::Auto)); + let _ = std::fs::remove_dir_all(&dir); +} + +/// #712: never a `dxcompiler.dll` found through the DLL search path (current directory, PATH). +#[test] +fn dx12_compiler_is_fxc_or_dxc_beside_the_exe() { + use wgpu::Dx12Compiler as C; + let dir = temp_dir("dxc"); + std::fs::create_dir_all(&dir).unwrap(); + assert!(matches!(dx12_compiler(None, None), C::Fxc)); + assert!(matches!(dx12_compiler(None, Some(&dir)), C::Fxc)); + std::fs::write(dir.join("dxcompiler.dll"), b"MZ").unwrap(); + let expected = dir.join("dxcompiler.dll"); + assert!(matches!(dx12_compiler(None, Some(&dir)), C::DynamicDxc { dxc_path } if Path::new(&dxc_path) == expected)); + // An explicit WGPU_DX12_COMPILER wins; an unknown value is ignored. + assert!(matches!(dx12_compiler(Some(" FXC "), Some(&dir)), C::Fxc)); + assert!(matches!(dx12_compiler(Some("dxc"), None), C::DynamicDxc { dxc_path } if dxc_path == "dxcompiler.dll")); + assert!(matches!(dx12_compiler(Some("quantum"), None), C::Fxc)); + let _ = std::fs::remove_dir_all(&dir); + + // The app's setup replaces wgpu's default (`Auto`: DXC by name from the search path). + let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); + let plan = plan(Auto, None, None, false, Os::Windows); + configure(&mut setup, &plan, Os::Windows, None, Arc::new(Mutex::new(None)), Arc::default()); + let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; + if std::env::var_os("WGPU_DX12_COMPILER").is_none() { + let safe = match &create.instance_descriptor.backend_options.dx12.shader_compiler { + C::Fxc => true, + C::DynamicDxc { dxc_path } => Path::new(dxc_path).is_absolute(), + C::StaticDxc | C::Auto => false, + }; + assert!(safe, "{:?}", create.instance_descriptor.backend_options.dx12.shader_compiler); + } +} + +#[test] +fn intel_macs_skip_the_indirect_validation_shaders() { + // Metal's compiler crashed on them on Intel Iris Pro, losing the device in every mode (#3107). + assert!(skips_indirect_validation(Os::Mac, true)); + // Apple silicon, Windows and Linux keep wgpu's validation. + for (os, intel) in [(Os::Mac, false), (Os::Windows, true), (Os::Other, true), (Os::Other, false)] { + assert!(!skips_indirect_validation(os, intel), "{os:?} intel={intel}"); + } + for (os, safe_gpu) in [(Os::Windows, false), (Os::Other, true), (Os::Other, false)] { + let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); + let egui_wgpu::WgpuSetup::CreateNew(before) = &setup else { panic!("a new instance") }; + let before = before.instance_descriptor.flags; + configure(&mut setup, &plan(Auto, None, None, safe_gpu, os), os, None, Arc::new(Mutex::new(None)), Arc::default()); + let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; + assert_eq!(create.instance_descriptor.flags, before, "{os:?}: flags untouched"); + } + if cfg!(target_arch = "x86_64") && std::env::var_os("WGPU_VALIDATION_INDIRECT_CALL").is_none() { + let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); + configure(&mut setup, &plan(Auto, None, None, false, Os::Mac), Os::Mac, None, Arc::new(Mutex::new(None)), Arc::default()); + let egui_wgpu::WgpuSetup::CreateNew(create) = &setup else { panic!("a new instance") }; + assert!(!create.instance_descriptor.flags.contains(wgpu::InstanceFlags::VALIDATION_INDIRECT_CALL)); + } +} diff --git a/apps/photocraft/src/launch_args.rs b/apps/photocraft/src/launch_args.rs new file mode 100644 index 0000000000..5e54f84ed8 --- /dev/null +++ b/apps/photocraft/src/launch_args.rs @@ -0,0 +1,340 @@ +//! The desktop app's command line, as clap types (#7071): `photocraft --help` comes from these doc +//! comments, so the help and the parser can't disagree. Anything they don't declare is a usage +//! error naming it (exit status 2), so a typo such as `--safegpu` doesn't open the window and then +//! fail to open the flag as a file (#6431); so is a flag without its value, rather than a silent +//! fall back to a generated token or to no root (#6429), a repeated flag, and a bad +//! `--present-mode` or `--frame-latency`. A value never starts with `-` (write `--opt=-value`), +//! except for negative numbers, and a file whose name starts with `-` goes after `--`. +//! +//! The control and automation flags are `photocraft-automation`'s, shared with `photocraft-cli`. +//! Only the desktop app reads the automation roots from `PHOTOCRAFT_AUTOMATION_*_ROOT` (#1551). + +use std::ffi::OsString; +use std::fmt; +use std::path::PathBuf; + +use clap::builder::FalseyValueParser; +use clap::parser::ValueSource; +use clap::{Command, CommandFactory, FromArgMatches, Parser}; +use eframe::wgpu::PresentMode; +use photocraft_automation::args::{AutomationArgs, ControlArgs, blank_env_is_unset, conflict, control_port, name_env_vars}; + +use crate::presentation; + +/// Opens PhotoCraft with the given files +#[derive(Parser)] +#[command(name = "photocraft", version = photocraft_engine::build_info::long_version_static(), after_help = ENVIRONMENT)] +pub(crate) struct LaunchArgs { + /// Files to open. On Windows and Linux they open as new tabs in the PhotoCraft already running. + /// Put `--` before a name that starts with `-` + pub(crate) files: Vec, + /// Start with the CPU renderer for this launch (after a graphics driver crash) + #[arg(long)] + pub(crate) safe_gpu: bool, + /// macOS: keep the menus inside the window instead of the menu bar (the variable: 0, false, no + /// or off is off; empty is unset) + #[arg(long, env = "PHOTOCRAFT_IN_WINDOW_MENUS", value_parser = FalseyValueParser::new())] + pub(crate) in_window_menus: bool, + /// Windows and Linux: start a separate PhotoCraft instead of opening the files in the one + /// already running + #[arg(long)] + pub(crate) new_instance: bool, + /// How frames reach the display: fifo, autovsync or novsync (also immediate or mailbox). + /// Without it, PHOTOCRAFT_PRESENT_MODE + #[arg(long, value_name = "MODE", value_parser = present_mode)] + pub(crate) present_mode: Option, + /// Frames queued for the display, 1 to 3. Without it, PHOTOCRAFT_FRAME_LATENCY + // allow_negative_numbers makes `--frame-latency -1` a bad value rather than an unexpected argument. + #[arg(long, value_name = "1-3", value_parser = frame_latency, allow_negative_numbers = true)] + pub(crate) frame_latency: Option, + /// Time each brush stroke's frames: stderr prints a summary per stroke, a CSV file path also + /// records every frame. Without it, PHOTOCRAFT_STROKE_TRACE + #[arg(long, value_name = "stderr|FILE.csv")] + pub(crate) stroke_trace: Option, + /// Start the localhost JSON-lines control server on this port + #[arg(long, value_name = "PORT", env = "PHOTOCRAFT_CONTROL_PORT", value_parser = control_port, allow_negative_numbers = true)] + pub(crate) control: Option, + #[command(flatten)] + pub(crate) auth: ControlArgs, + #[command(flatten)] + pub(crate) automation: AutomationArgs, + /// The per-process options given on the command line, set by [`parse`]. + #[arg(skip)] + pub(crate) launch_options: Vec, +} + +/// The variables that aren't a flag's fallback, for the end of `--help`. +const ENVIRONMENT: &str = "\ +Environment: PHOTOCRAFT_NEW_INSTANCE (anything but 0 or empty: as --new-instance), PHOTOCRAFT_CONFIG_DIR (the settings folder)"; + +/// An option that applies only to the process it's given to: a launch with any of them keeps its +/// own process instead of handing its files to the running one (`single_instance`). Shown as the +/// flag, or the variable. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum PerProcess { + AutomationReadRoot, + AutomationWriteRoot, + SafeGpu, + InWindowMenus, + PresentMode, + FrameLatency, + StrokeTrace, + /// PHOTOCRAFT_GPU_ADAPTER, when startup will act on it (`gpu_startup`). Not a flag. + GpuAdapterEnv, +} + +impl PerProcess { + /// The flags, in the order a log line lists them. A flag's variable asks for the same thing, so + /// it counts too (#7242); the variables that aren't a flag's fallback are counted by + /// `single_instance::env_launch_options`. + pub(crate) const FLAGS: [PerProcess; 7] = + [Self::AutomationReadRoot, Self::AutomationWriteRoot, Self::SafeGpu, Self::InWindowMenus, Self::PresentMode, Self::FrameLatency, Self::StrokeTrace]; + + /// The flag's clap argument id. + fn id(self) -> Option<&'static str> { + Some(match self { + Self::AutomationReadRoot => "automation_read_root", + Self::AutomationWriteRoot => "automation_write_root", + Self::SafeGpu => "safe_gpu", + Self::InWindowMenus => "in_window_menus", + Self::PresentMode => "present_mode", + Self::FrameLatency => "frame_latency", + Self::StrokeTrace => "stroke_trace", + Self::GpuAdapterEnv => return None, + }) + } +} + +impl fmt::Display for PerProcess { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::AutomationReadRoot => "--automation-read-root", + Self::AutomationWriteRoot => "--automation-write-root", + Self::SafeGpu => "--safe-gpu", + Self::InWindowMenus => "--in-window-menus", + Self::PresentMode => "--present-mode", + Self::FrameLatency => "--frame-latency", + Self::StrokeTrace => "--stroke-trace", + Self::GpuAdapterEnv => "PHOTOCRAFT_GPU_ADAPTER", + }) + } +} + +/// The files to open, as [`LaunchArgs::file_names`] sorts them. +#[derive(Debug, PartialEq)] +pub(crate) struct FileNames { + pub(crate) openable: Vec, + /// Names that are not valid Unicode, shown lossily: reported, not opened. + pub(crate) unreadable: Vec, +} + +impl LaunchArgs { + /// The files to open, and those whose names are not valid Unicode (#1108): a file manager can + /// hand over a Latin-1 name on Linux. Those are reported in a notice once the app is up. + pub(crate) fn file_names(&self) -> FileNames { + let mut names = FileNames { openable: Vec::new(), unreadable: Vec::new() }; + for f in &self.files { + match f.to_str() { + Some(s) => names.openable.push(s.to_owned()), + None => names.unreadable.push(f.to_string_lossy().into_owned()), + } + } + names + } +} + +/// The command line, with the desktop app's variables for the automation roots. An empty or blank +/// variable counts as unset. +pub(crate) fn command() -> Command { + LaunchArgs::command() + .mut_arg("automation_read_root", |a| a.env("PHOTOCRAFT_AUTOMATION_READ_ROOT")) + .mut_arg("automation_write_root", |a| a.env("PHOTOCRAFT_AUTOMATION_WRITE_ROOT")) + .mut_args(blank_env_is_unset) +} + +/// Parse the launch arguments (without the binary name) with `cmd`, from [`command`]. Old macOS +/// passes a process serial number (`-psn_…`) when launched from Finder: it is dropped first, unless +/// it comes after `--`. A bad value from a variable is a usage error naming the variable. +pub(crate) fn parse(mut cmd: Command, args: impl IntoIterator) -> Result { + let mut options = true; + let args: Vec = args + .into_iter() + .filter(|a| { + options &= a != "--"; + !(options && a.to_str().is_some_and(|s| s.starts_with("-psn_"))) + }) + .collect(); + let argv = std::iter::once(OsString::from("photocraft")).chain(args.iter().cloned()); + let matches = cmd.try_get_matches_from_mut(argv).map_err(|e| name_env_vars(e, &cmd, &args))?; + let mut launch = LaunchArgs::from_arg_matches(&matches)?; + // Only the control server reads a token, so a stale exported pair doesn't stop a launch. + if launch.control.is_some() { + conflict(&mut cmd, &matches, "control_token", "control_token_file")?; + } + // `PHOTOCRAFT_IN_WINDOW_MENUS=0` (or off, false, no) is a variable that turns the menus off. + let menus = launch.in_window_menus; + let given = |p: &PerProcess| match p.id().and_then(|id| matches.value_source(id)) { + Some(ValueSource::CommandLine) => true, + Some(ValueSource::EnvVariable) => *p != PerProcess::InWindowMenus || menus, + _ => false, + }; + launch.launch_options = PerProcess::FLAGS.into_iter().filter(given).collect(); + Ok(launch) +} + +/// Why a value was refused: clap prints it after the flag and its value (exit 2). +#[derive(Debug, thiserror::Error)] +enum ValueError { + #[error("expected fifo, autovsync or novsync (or immediate, mailbox)")] + PresentMode, + #[error("expected 1, 2 or 3")] + FrameLatency, +} + +fn present_mode(s: &str) -> Result { + presentation::present_mode(s).ok_or(ValueError::PresentMode) +} + +fn frame_latency(s: &str) -> Result { + presentation::frame_latency(s).ok_or(ValueError::FrameLatency) +} + +#[cfg(test)] +mod tests { + use super::*; + use clap::error::ErrorKind; + + /// Parse without the variables, so one exported where the tests run can't change them (the + /// process tests in `tests/command_line_usage.rs` cover the variables). + fn parse_args(args: &[&str]) -> Result { + parse(command().mut_args(|a| a.env(None::<&str>)), args.iter().map(OsString::from)) + } + + fn usage_error(args: &[&str]) -> String { + let Err(e) = parse_args(args) else { panic!("{args:?} parsed") }; + assert_eq!(e.exit_code(), 2, "{args:?}: {e}"); + e.to_string() + } + + #[test] + fn control_port_rejects_unparseable_values_naming_them() { + // Out of range, negative and empty values fail, naming the value (#701); a following + // flag is never taken as the value. + assert!(usage_error(&["--control", "78787"]).contains("invalid value '78787' for '--control '")); + assert!(usage_error(&["--control", "-1"]).contains("invalid value '-1'")); + assert!(usage_error(&["--control="]).contains("invalid value '' for '--control '")); + assert!(usage_error(&["--control", "--safe-gpu"]).contains("a value is required for '--control '")); + } + + #[test] + fn flags_without_a_value_are_usage_errors() { + // #6429: a missing value fails the launch, rather than falling back to a generated token + // or to no root. The tuning flags too, now (#7071). + for flag in [ + "--control", + "--control-token", + "--control-token-file", + "--automation-read-root", + "--automation-write-root", + "--present-mode", + "--frame-latency", + "--stroke-trace", + ] { + let err = usage_error(&[flag]); + assert!(err.contains(&format!("a value is required for '{flag} <")), "{flag}: {err}"); + } + } + + #[test] + fn bad_tuning_values_and_repeats_are_usage_errors() { + assert!(usage_error(&["--present-mode", "fast"]).contains("expected fifo, autovsync or novsync")); + for latency in ["0", "4", "-1", "one"] { + let err = usage_error(&["--frame-latency", latency]); + assert!(err.contains(&format!("invalid value '{latency}' for '--frame-latency <1-3>'")), "{err}"); + } + assert!(usage_error(&["--safe-gpu", "--safe-gpu"]).contains("cannot be used multiple times")); + assert!(usage_error(&["--control", "1", "--control", "2"]).contains("cannot be used multiple times")); + // A token and a token file are either/or for the control server, and ignored without it. + let pair = ["--control-token", "abcd", "--control-token-file", "/tmp/token"]; + assert!(usage_error(&[&["--control", "1"][..], &pair[..]].concat()).contains("cannot be used with")); + assert!(parse_args(&pair).is_ok()); + } + + #[test] + fn values_are_parsed_and_per_process_options_recorded() { + let parsed = parse_args(&[ + "--stroke-trace", + "stderr", + "--control", + "50494", + "--control-token", + "abcd", + "--automation-read-root", + "/r", + "--automation-write-root", + "/w", + "--safe-gpu", + "--in-window-menus", + "--new-instance", + "--present-mode", + "Mailbox", + "--frame-latency", + "1", + "photo.psd", + ]) + .unwrap(); + assert_eq!(parsed.control, Some(50494)); + assert_eq!(parsed.auth.control_token.as_deref(), Some("abcd")); + assert_eq!(parsed.auth.control_token_file, None); + assert_eq!(parsed.automation.automation_read_root.as_deref(), Some(std::path::Path::new("/r"))); + assert_eq!(parsed.automation.automation_write_root.as_deref(), Some(std::path::Path::new("/w"))); + assert!(parsed.safe_gpu && parsed.in_window_menus && parsed.new_instance); + assert_eq!(parsed.present_mode, Some(PresentMode::AutoNoVsync)); + assert_eq!(parsed.frame_latency, Some(1)); + assert_eq!(parsed.stroke_trace.as_deref(), Some("stderr")); + // In a fixed order: `single_instance` only lists them in a log line. + assert_eq!(parsed.launch_options, PerProcess::FLAGS); + assert_eq!(parsed.file_names(), FileNames { openable: vec!["photo.psd".to_string()], unreadable: vec![] }); + + let parsed = parse_args(&["--control-token-file", "/tmp/token", "--new-instance"]).unwrap(); + assert_eq!(parsed.auth.control_token_file.as_deref(), Some(std::path::Path::new("/tmp/token"))); + assert!(parsed.launch_options.is_empty() && !parsed.in_window_menus, "{:?}", parsed.launch_options); + } + + #[test] + fn version_stops_parsing() { + let e = parse_args(&["--version", "--control"]).err().unwrap(); + assert_eq!((e.kind(), e.exit_code()), (ErrorKind::DisplayVersion, 0)); + assert_eq!(e.to_string(), format!("photocraft {}\n", photocraft_engine::build_info::long_version())); + } + + #[test] + fn help_stops_parsing_and_double_dash_ends_options() { + let e = parse_args(&["-h", "--safegpu", "--control"]).err().unwrap(); + assert_eq!((e.kind(), e.exit_code()), (ErrorKind::DisplayHelp, 0)); + + assert!(usage_error(&["--safegpu", "--", "--help"]).contains("unexpected argument '--safegpu'")); + let parsed = parse_args(&["a.psd", "--", "--help", "-x"]).unwrap(); + assert_eq!(parsed.file_names().openable, ["a.psd", "--help", "-x"]); + } + + #[test] + fn a_finder_process_serial_number_is_dropped() { + let parsed = parse_args(&["-psn_0_12345", "a.psd"]).unwrap(); + assert_eq!(parsed.file_names().openable, ["a.psd"]); + let parsed = parse_args(&["--", "-psn_x.psd"]).unwrap(); + assert_eq!(parsed.file_names().openable, ["-psn_x.psd"]); + } + + /// #1108: a name that isn't valid Unicode is reported, not opened; a token must be text. + #[cfg(unix)] + #[test] + fn non_unicode_file_names_are_reported_and_tokens_rejected() { + use std::os::unix::ffi::OsStringExt as _; + let latin1 = OsString::from_vec(b"caf\xe9.psd".to_vec()); + let parsed = parse(command().mut_args(|a| a.env(None::<&str>)), [latin1.clone()]).unwrap(); + assert_eq!(parsed.file_names(), FileNames { openable: vec![], unreadable: vec!["caf\u{fffd}.psd".to_string()] }); + let e = parse(command().mut_args(|a| a.env(None::<&str>)), [OsString::from("--control-token"), latin1]).err().unwrap(); + assert_eq!((e.kind(), e.exit_code()), (ErrorKind::InvalidUtf8, 2)); + } +} diff --git a/apps/photocraft/src/logging.rs b/apps/photocraft/src/logging.rs index 292428b8ce..f5f014a3c1 100644 --- a/apps/photocraft/src/logging.rs +++ b/apps/photocraft/src/logging.rs @@ -4,7 +4,9 @@ //! lost device, the crash guard's panic report) vanished. A launch from a desktop menu has no //! terminal, so the file is what a bug report can attach: `/logs/photocraft.log` //! (see `app_dirs`). Each start moves the previous log to `photocraft.1.log` (and that one to -//! `.2`), so the log of a run that crashed survives the next launch. +//! `.2`), so the log of a run that crashed survives the next launch. A start while another +//! instance is still running leaves that instance's log alone and writes `photocraft-.log` +//! (see [`rotate`]). //! //! Levels: `info` for PhotoCraft's own crates, `warn` for everything else (wgpu and naga are //! chatty). `RUST_LOG` replaces that with env_logger-style directives: `debug`, @@ -15,7 +17,7 @@ //! written once the file is attached. Writing never panics: a file that can't be created or //! written leaves standard error as the only sink. -use std::fs::File; +use std::fs::{File, OpenOptions, TryLockError}; use std::io::Write; use std::path::{Path, PathBuf}; use std::sync::{Mutex, OnceLock, PoisonError}; @@ -89,37 +91,112 @@ fn matches(name: &str, target: &str) -> bool { } /// `2026-10-08T07:59:17.728Z` (UTC, milliseconds). A clock before 1970 reads as the epoch. +/// A clock far past chrono's year range still converts (the calendar repeats every 400 years), so +/// logging never panics and never silently resets to the epoch for a convertible instant. pub fn timestamp(t: SystemTime) -> String { let since = t.duration_since(std::time::UNIX_EPOCH).unwrap_or_default(); - let secs = since.as_secs(); - let (days, rem) = (secs / 86_400, secs % 86_400); - let (y, m, d) = civil_from_days(days); - format!("{y:04}-{m:02}-{d:02}T{:02}:{:02}:{:02}.{:03}Z", rem / 3600, rem % 3600 / 60, rem % 60, since.subsec_millis()) + // Pre-1970 is clamped to the epoch by `unwrap_or_default` above. `as_secs` is `u64`; a value + // past `i64::MAX` seconds saturates rather than falling back to the epoch. + let secs = i64::try_from(since.as_secs()).unwrap_or(i64::MAX); + let (y, t) = utc_parts(secs, since.subsec_nanos()); + // `{:04}` rather than `%Y`, which writes a `+` before years past 9999. + format!("{y:04}-{}", t.format("%m-%dT%H:%M:%S%.3fZ")) } -/// Days since 1970-01-01 to a proleptic Gregorian (year, month, day) (Howard Hinnant's -/// `civil_from_days`, unsigned because `timestamp` clamps at the epoch). -fn civil_from_days(days: u64) -> (u64, u64, u64) { - let z = days + 719_468; - let era = z / 146_097; - let doe = z % 146_097; - let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; - let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); - let mp = (5 * doy + 2) / 153; - let d = doy - (153 * mp + 2) / 5 + 1; - let m = if mp < 10 { mp + 3 } else { mp - 9 }; - let y = yoe + era * 400 + u64::from(m <= 2); - (y, m, d) +/// Unix seconds (+ nanos) → (proleptic Gregorian year, the same wall-clock instant moved into +/// chrono's range by whole 400-year cycles). The calendar repeats every 400 years (146_097 days), +/// so month, day and time of day come from chrono and only the year is carried, which keeps every +/// `i64` exact. Replaces the old hand-rolled `civil_from_days`; chrono is the only calendar logic. +fn utc_parts(secs: i64, nanos: u32) -> (i64, chrono::NaiveDateTime) { + use chrono::Datelike; + let (days, sod) = (secs.div_euclid(86_400), secs.rem_euclid(86_400)); + let (cycles, rest) = (days.div_euclid(146_097), days.rem_euclid(146_097)); + // `rest + 719_163` (1970-01-01 is CE day 719_163) is always a valid CE day, and `sod` < 86_400. + let date = i32::try_from(rest + 719_163).ok().and_then(chrono::NaiveDate::from_num_days_from_ce_opt); + let time = u32::try_from(sod).ok().and_then(|s| chrono::NaiveTime::from_num_seconds_from_midnight_opt(s, nanos.min(999_999_999))); + match (date, time) { + (Some(d), Some(t)) => { + let y = i128::from(d.year()) + i128::from(cycles) * 400; + (i64::try_from(y).unwrap_or(if y < 0 { i64::MIN } else { i64::MAX }), d.and_time(t)) + } + _ => (1970, chrono::NaiveDateTime::default()), // unreachable + } } fn numbered(dir: &Path, n: usize) -> PathBuf { if n == 0 { dir.join(LOG_FILE) } else { dir.join(format!("photocraft.{n}.log")) } } -/// Shift the previous logs up one (`.log` → `.1.log` → … → `..log`, the oldest dropped) -/// and create a fresh, empty log file in `dir` (created if missing). -pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> { +/// Held by the instance that writes [`LOG_FILE`], from its start until it exits. A separate lock +/// file rather than a lock on the log itself: Windows locks are mandatory, and a locked log could +/// not be opened for a bug report while the app runs. +pub const OWNER_LOCK: &str = "photocraft.lock"; +/// Held while one start rotates and prunes the logs, so two starts at once can't interleave. +pub const ROTATE_LOCK: &str = "rotate.lock"; + +/// The log of an instance started while another one was writing [`LOG_FILE`]. +fn instance_log(dir: &Path, pid: u32) -> PathBuf { + dir.join(format!("photocraft-{pid}.log")) +} + +/// The lock file that marks [`instance_log`] as in use. +fn instance_lock(dir: &Path, pid: u32) -> PathBuf { + dir.join(format!("photocraft-{pid}.lock")) +} + +/// The process id in an [`instance_log`] file name. +fn instance_pid(name: &str) -> Option { + let digits = name.strip_prefix("photocraft-")?.strip_suffix(".log")?; + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + digits.parse().ok() +} + +fn open_lock(path: &Path) -> std::io::Result { + OpenOptions::new().read(true).write(true).create(true).truncate(false).open(path) +} + +/// Take `file`'s lock without waiting: `Ok(true)` holds it, `Ok(false)` when another process holds it. +fn try_hold(file: &File) -> std::io::Result { + match file.try_lock() { + Ok(()) => Ok(true), + Err(TryLockError::WouldBlock) => Ok(false), + Err(TryLockError::Error(e)) => Err(e), + } +} + +/// Open a fresh, empty log file in `dir` (created if missing). +/// +/// Normally the previous logs shift up one (`.log` → `.1.log` → … → `..log`, the oldest +/// dropped). While another instance is still writing [`LOG_FILE`] (it holds [`OWNER_LOCK`]), the +/// chain is left alone and this start writes `photocraft-.log` instead: rotating would move +/// that instance's live log away and, a few launches later, delete it (#6432). Such side-by-side +/// logs are pruned to the newest [`KEEP`] once their instances have exited. On a file system +/// without locks every start rotates, as before. +pub fn rotate(dir: &Path) -> std::io::Result { std::fs::create_dir_all(dir)?; + // Waits only for another start's few file operations; without locking support, go on. + let guard = open_lock(&dir.join(ROTATE_LOCK)).ok().filter(|g| g.lock().is_ok()); + let log = rotate_locked(dir); + drop(guard); + log +} + +fn rotate_locked(dir: &Path) -> std::io::Result { + prune_instance_logs(dir); + let owner = open_lock(&dir.join(OWNER_LOCK))?; + let state = try_hold(&owner); + if let Ok(false) = state { + let pid = std::process::id(); + let lock = open_lock(&instance_lock(dir, pid))?; + let held = try_hold(&lock).unwrap_or(false); + let path = instance_log(dir, pid); + let file = File::create(&path)?; + return Ok(LogFile { path, file, owner: held.then_some(lock) }); + } + // This start holds the owner lock (or the file system has no locks). + let held = state.unwrap_or(false); for n in (1..=KEEP).rev() { match std::fs::rename(numbered(dir, n - 1), numbered(dir, n)) { Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e), @@ -128,7 +205,40 @@ pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> { } let path = numbered(dir, 0); let file = File::create(&path)?; - Ok((path, file)) + Ok(LogFile { path, file, owner: held.then_some(owner) }) +} + +/// Remove the side-by-side logs of instances that have exited, all but the newest [`KEEP`]. +/// A log whose lock file is held belongs to a running instance and is never touched. Failures +/// are ignored: pruning only saves disk space. +fn prune_instance_logs(dir: &Path) { + let Ok(entries) = std::fs::read_dir(dir) else { return }; + let mut finished: Vec<(SystemTime, u32)> = Vec::new(); + for entry in entries.flatten() { + let Some(pid) = entry.file_name().to_str().and_then(instance_pid) else { continue }; + // A missing lock file means nobody holds it; an unknown lock state counts as running. + let running = match OpenOptions::new().read(true).write(true).open(instance_lock(dir, pid)) { + Ok(lock) => !try_hold(&lock).unwrap_or(false), + Err(_) => false, + }; + if !running { + let modified = entry.metadata().and_then(|m| m.modified()).unwrap_or(SystemTime::UNIX_EPOCH); + finished.push((modified, pid)); + } + } + finished.sort_unstable_by(|a, b| b.cmp(a)); + for &(_, pid) in finished.iter().skip(KEEP) { + let _ = std::fs::remove_file(instance_log(dir, pid)); + let _ = std::fs::remove_file(instance_lock(dir, pid)); + } +} + +/// A log file opened by [`rotate`]. +pub struct LogFile { + pub path: PathBuf, + pub file: File, + /// The held lock that marks `path` as in use; `None` where locking isn't supported. + owner: Option, } /// One formatted log line: ` [] : \n`. @@ -205,19 +315,31 @@ pub struct AppLogger { filter: Filter, stderr: bool, sink: Mutex, + /// The file [`AppLogger::attach_dir`] opened, so a failure can tell the user what to attach. + path: Mutex>, + /// The lock that marks the attached log file as in use (see [`rotate`]); held until exit. + owner: Mutex>, } impl AppLogger { pub fn new(filter: Filter, stderr: bool) -> AppLogger { - AppLogger { filter, stderr, sink: Mutex::new(Sink::new(MAX_FILE_BYTES)) } + AppLogger { filter, stderr, sink: Mutex::new(Sink::new(MAX_FILE_BYTES)), path: Mutex::new(None), owner: Mutex::new(None) } } /// Rotate the logs in `dir` and send records to the fresh file; returns its path. pub fn attach_dir(&self, dir: &Path) -> Result { - let (path, file) = rotate(dir).map_err(|e| format!("{}: {e}", dir.display()))?; + let LogFile { path, file, owner } = rotate(dir).map_err(|e| format!("{}: {e}", dir.display()))?; self.sink.lock().unwrap_or_else(PoisonError::into_inner).attach(file); + *self.path.lock().unwrap_or_else(PoisonError::into_inner) = Some(path.clone()); + *self.owner.lock().unwrap_or_else(PoisonError::into_inner) = owner; Ok(path) } + + /// The log file records are written to, once [`AppLogger::attach_dir`] opened one: `None` when + /// the file could not be created (standard error is the only sink, see [`install`]). + pub fn path(&self) -> Option { + self.path.lock().unwrap_or_else(PoisonError::into_inner).clone() + } } impl log::Log for AppLogger { @@ -266,6 +388,21 @@ mod tests { d } + /// The startup-failure dialog names the log file, so the logger has to remember it (#7643). + /// A start that couldn't create one keeps `None` and the dialog leaves the line out. + #[test] + fn path_names_the_attached_log_file() { + let _spawns = crate::no_child_spawns(); + let dir = temp_dir("path"); + let logger = AppLogger::new(Filter::parse(DEFAULT_FILTER), false); + assert_eq!(logger.path(), None, "nothing is written before a file is attached"); + let path = logger.attach_dir(&dir).expect("attach"); + assert_eq!(path, dir.join(LOG_FILE)); + assert_eq!(logger.path(), Some(path)); + drop(logger); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn the_default_filter_shows_photocraft_info_and_other_crates_warnings() { let f = Filter::parse(DEFAULT_FILTER); @@ -312,13 +449,28 @@ mod tests { assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(1_709_164_800)), "2024-02-29T00:00:00.000Z"); // Before the epoch (a clock set wrong) clamps instead of panicking. assert_eq!(timestamp(UNIX_EPOCH - Duration::from_secs(5)), "1970-01-01T00:00:00.000Z"); + // Past chrono's year range still converts via the 400-year cycle (the old code did too, + // through `civil_from_days`): never a silent reset to the epoch. Platform-safe: Windows' + // SystemTime can't hold this instant, so it is only checked where it exists. + // `u64::MAX / 2` seconds == `i64::MAX` seconds → year 292277026596 (old output). + if let Some(far) = UNIX_EPOCH.checked_add(Duration::from_secs(u64::MAX / 2)) { + assert_eq!(timestamp(far), "292277026596-12-04T15:30:07.000Z"); + } + // Years past 9999 keep the plain zero-padded year (no `+`). + assert_eq!(timestamp(UNIX_EPOCH + Duration::from_secs(253_402_300_800)), "10000-01-01T00:00:00.000Z"); + // 10_000_000_000_000 s is far beyond chrono's range; old output is year 318857. + // Windows' SystemTime holds only up to about year 30828, so this is checked where it fits. + if let Some(far) = UNIX_EPOCH.checked_add(Duration::from_secs(10_000_000_000_000)) { + assert_eq!(timestamp(far), "318857-05-20T17:46:40.000Z"); + } } #[test] fn rotation_keeps_the_previous_logs_and_starts_an_empty_file() { + let _spawns = crate::no_child_spawns(); let dir = temp_dir("rotate"); for run in 1..=4 { - let (path, _file) = rotate(&dir).expect("rotate"); + let LogFile { path, file: _file, owner: _owner } = rotate(&dir).expect("rotate"); assert_eq!(path, dir.join(LOG_FILE)); assert_eq!(std::fs::read_to_string(&path).expect("read"), ""); std::fs::write(&path, format!("run {run}")).expect("write"); @@ -330,6 +482,66 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// #6432: a second launch moved the running instance's live log to `.1.log`, a third to + /// `.2.log`, and a fourth deleted it. + #[test] + fn a_second_start_leaves_the_running_instance_log_alone() { + let _spawns = crate::no_child_spawns(); + let dir = temp_dir("second"); + let mut first = rotate(&dir).expect("first start"); + assert_eq!(first.path, dir.join(LOG_FILE)); + first.file.write_all(b"first, before\n").expect("write"); + for _ in 0..KEEP + 1 { + let second = rotate(&dir).expect("second start"); + assert_eq!(second.path, instance_log(&dir, std::process::id())); + assert_eq!(read(&second.path), ""); + } + first.file.write_all(b"first, after\n").expect("write"); + assert_eq!(read(&dir.join(LOG_FILE)), "first, before\nfirst, after\n"); + assert!(!dir.join("photocraft.1.log").exists(), "the running instance's log was rotated"); + // Once the first instance is gone, the next start rotates as before. + drop(first); + let third = rotate(&dir).expect("third start"); + assert_eq!(third.path, dir.join(LOG_FILE)); + assert_eq!(read(&dir.join("photocraft.1.log")), "first, before\nfirst, after\n"); + drop(third); + let _ = std::fs::remove_dir_all(&dir); + } + + #[test] + fn instance_logs_are_pruned_only_when_no_one_holds_them() { + let _spawns = crate::no_child_spawns(); + let dir = temp_dir("prune"); + let first = rotate(&dir).expect("first start"); + // Earlier side-by-side runs that have exited, oldest first; the oldest has no lock file + // (as if its lock file had been removed), the others an unlocked one. + let now = SystemTime::now(); + for (age, pid) in [(400u64, 1u32), (300, 2), (200, 3), (100, 4)] { + let path = instance_log(&dir, pid); + std::fs::write(&path, format!("run {pid}")).expect("write"); + File::options().write(true).open(&path).expect("open").set_modified(now - Duration::from_secs(age)).expect("mtime"); + if pid != 1 { + std::fs::write(instance_lock(&dir, pid), "").expect("lock file"); + } + } + // One older still running: its log is never removed, however old. + let live = instance_log(&dir, 5); + std::fs::write(&live, "live").expect("write"); + File::options().write(true).open(&live).expect("open").set_modified(now - Duration::from_secs(900)).expect("mtime"); + let held = File::create(instance_lock(&dir, 5)).expect("lock file"); + held.try_lock().expect("hold"); + + let second = rotate(&dir).expect("second start"); + assert_eq!(second.path, instance_log(&dir, std::process::id())); + // The newest KEEP finished ones stay, the rest go with their lock files. + assert!(instance_log(&dir, 4).exists() && instance_log(&dir, 3).exists()); + assert!(!instance_log(&dir, 2).exists() && !instance_log(&dir, 1).exists()); + assert!(!instance_lock(&dir, 2).exists()); + assert_eq!(read(&live), "live"); + drop((first, second, held)); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn rotation_into_an_unusable_directory_is_an_error_not_a_panic() { let dir = temp_dir("blocked"); @@ -355,7 +567,7 @@ mod tests { #[test] fn lines_logged_before_the_file_exists_are_written_when_it_is_attached() { let dir = temp_dir("pending"); - let (path, file) = rotate(&dir).expect("rotate"); + let LogFile { path, file, .. } = rotate(&dir).expect("rotate"); let mut sink = Sink::new(MAX_FILE_BYTES); sink.write("early 1\n"); sink.write("early 2\n"); @@ -368,7 +580,7 @@ mod tests { #[test] fn the_pending_buffer_is_bounded_and_says_how_much_it_dropped() { let dir = temp_dir("bounded"); - let (path, file) = rotate(&dir).expect("rotate"); + let LogFile { path, file, .. } = rotate(&dir).expect("rotate"); let mut sink = Sink::new(MAX_FILE_BYTES); for i in 0..MAX_PENDING + 5 { sink.write(&format!("line {i}\n")); @@ -384,7 +596,7 @@ mod tests { #[test] fn the_file_stops_at_its_size_cap_with_one_note() { let dir = temp_dir("cap"); - let (path, file) = rotate(&dir).expect("rotate"); + let LogFile { path, file, .. } = rotate(&dir).expect("rotate"); let mut sink = Sink::new(100); sink.attach(file); for i in 0..50 { diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs index abd70eb418..2a83b78ce5 100644 --- a/apps/photocraft/src/main.rs +++ b/apps/photocraft/src/main.rs @@ -1,9 +1,7 @@ //! PhotoCraft desktop app. //! -//! Usage: `photocraft [--control ] [--control-token <64-hex> | -//! --control-token-file ] [--automation-read-root ] -//! [--automation-write-root ] [--safe-gpu] [--in-window-menus] [--present-mode ] -//! [--frame-latency ] [--stroke-trace ] [--new-instance] [files…]` +//! Usage: `photocraft [options] [--] [files…]`; `photocraft --help` lists the options, which are +//! declared, with what makes a usage error (exit status 2), in `launch_args`. //! //! On Windows and Linux a launch hands its files to the PhotoCraft that is already running (new //! tabs in its window) and exits; `--new-instance` starts a separate copy instead, as does any @@ -26,7 +24,8 @@ //! `{"id":1,"ok":true,"result":…}`. See `photocraft_ui_egui::control` for the methods. // Release builds on Windows are GUI-subsystem apps, so launching from the Start Menu or Explorer -// doesn't open a console window. (`--version` output then only shows when redirected.) +// doesn't open a console window. (`--help`, `--version` and usage errors then only show when +// redirected.) #![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -44,6 +43,7 @@ mod file_dialog_probe; mod gpu_startup; #[cfg(target_os = "linux")] mod icon_theme; +mod launch_args; #[cfg(target_os = "macos")] mod mac_menu; #[cfg(target_os = "macos")] @@ -58,15 +58,19 @@ mod photoshop_settings; #[cfg(target_os = "linux")] mod portal_save; mod presentation; +// The print portal needs the session bus (zbus, Linux only); its pure helpers are tested everywhere. +#[cfg(any(target_os = "linux", test))] +mod print_portal; mod screen_color; mod services; // macOS gets one instance from LaunchServices (open-documents Apple events). #[cfg(any(not(target_os = "macos"), test))] mod single_instance; -// Windows gets pen pressure from winit (WM_POINTER); the web runner has its own listener. -#[cfg(any(target_os = "macos", target_os = "linux", test))] +mod startup_report; +// Tablet support (macOS AppKit monitor, Linux X11/Wayland, Windows barrel button). mod tablet; mod ui_state; +mod updates; mod window_frame; use photocraft_engine::Session; @@ -89,15 +93,60 @@ fn custom_titlebar(prefs_file: Option<&std::path::Path>) -> bool { CUSTOM_TITLEBAR && !prefs.interface.system_title_bar } +/// Preferences › Integrations, read before the window opens the way [`custom_titlebar`] is: +/// whether agents may drive this launch at all, and the port to listen on when no `--control` +/// was given (0 = only with `--control`). A missing or unreadable file keeps the defaults. +/// +/// The section is read on its own, and when it doesn't read as a whole each value on its own, +/// as the app's lenient load reads them (#6611): a wrong-typed value elsewhere in the file, or +/// in the other field, must not turn Allow Agent Control back on while the dialog shows it off. +fn integrations(prefs_file: Option<&std::path::Path>) -> photocraft_engine::prefs::Integrations { + use serde_json::Value; + let mut out = photocraft_engine::prefs::Preferences::default().integrations; + let file: Option = prefs_file.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str(&t).ok()); + let Some(section) = file.as_ref().and_then(|v| v.get("integrations")) else { + return out; + }; + if let Ok(whole) = serde_json::from_value(section.clone()) { + return whole; + } + if let Some(allow) = section.get("allowAgentControl").and_then(Value::as_bool) { + out.allow_agent_control = allow; + } + if let Some(port) = section.get("controlPort").and_then(Value::as_u64).and_then(|p| u32::try_from(p).ok()) { + out.control_port = port; + } + out +} + +/// The control port this launch listens on, and the warning to print when a request was refused. +/// `--control` (or `PHOTOCRAFT_CONTROL_PORT`) wins over the preference; Allow Agent Control off +/// refuses both, and says so rather than ignoring the flag silently. A preferred port outside +/// 1..=65535 is refused the same way. +fn control_port_for(cli: Option, prefs: &photocraft_engine::prefs::Integrations) -> (Option, Option) { + if !prefs.allow_agent_control { + let warning = cli.map(|p| format!("photocraft: Preferences › Integrations › Allow Agent Control is off: not listening on port {p}\n")); + return (None, warning); + } + if let Some(port) = cli { + return (Some(port), None); + } + match prefs.control_port { + 0 => (None, None), + p => match u16::try_from(p).ok().filter(|p| *p != 0) { + Some(port) => (Some(port), None), + None => (None, Some(format!("photocraft: Preferences › Integrations › Control Port {p} is not a usable port: not listening\n"))), + }, + } +} + /// The main window: 1440 × 900 (shrunk to fit the monitor, and maximized on the first frame /// when it still doesn't fit, `work_area::fit_window`), centred on the main monitor. Without /// `centered`, Windows cascades each new window from the top-left corner, so it opened at a /// different offset every launch (#419). A later launch restores the saved position instead /// (`main` turns `centered` off, #4826). Wayland compositors place windows themselves. fn native_options(custom_titlebar: bool) -> eframe::NativeOptions { - let viewport = egui::ViewportBuilder::default(); - // Not inside a macOS app bundle: its Dock icon is the bundle's .icns (`app_icon`, #5849). - let viewport = if app_icon::sets_runtime_icon(std::env::current_exe().ok().as_deref()) { viewport.with_icon(app_icon::window_icon()) } else { viewport }; + let viewport = egui::ViewportBuilder::default().with_icon(app_icon::viewport_icon(std::env::current_exe().ok().as_deref())); eframe::NativeOptions { viewport: viewport .with_app_id(APP_ID) @@ -110,6 +159,9 @@ fn native_options(custom_titlebar: bool) -> eframe::NativeOptions { .with_titlebar_shown(false) .with_title_shown(false), centered: true, + // PhotoCraft owns its fallback fonts (`cjk_fonts`, craft-fonts): eframe's system-font + // provider would compete with it and make glyph choice machine-dependent (#8512). + system_font_fallback: false, // eframe saves native window geometry and egui panel/window sizes on exit. // Keep that state beside preferences, including config overrides and portable mode. persistence_path: services::config_dir().map(|dir| dir.join("ui.ron")), @@ -142,150 +194,9 @@ fn at_least_min_size(mut builder: egui::ViewportBuilder) -> egui::ViewportBuilde builder } -/// Parse a `--control` / `PHOTOCRAFT_CONTROL_PORT` value. An unparseable port is an error that -/// names the value (issue #701): silently running with no control server leaves a launcher or -/// agent unable to tell a typo from a successful grant. -fn parse_control_port(value: &str, source: &str) -> Result { - value.trim().parse().map_err(|_| format!("{source}: `{value}` is not a valid port (expected a number from 0 to 65535)")) -} - -/// Process exit status for the collected `--control` / `PHOTOCRAFT_CONTROL_PORT` errors: `None` -/// when there are none, else 2 (a command-line usage error), never 0. -fn control_args_exit_code(errors: &[String]) -> Option { - if errors.is_empty() { None } else { Some(2) } -} - -#[cfg(test)] -mod control_port_tests { - use super::{control_args_exit_code, parse_control_port}; - - #[test] - fn control_port_errors_exit_non_zero() { - assert_eq!(control_args_exit_code(&[]), None); - let errors = vec![parse_control_port("nope", "--control").unwrap_err()]; - assert_eq!(control_args_exit_code(&errors), Some(2)); - } - - #[test] - fn control_port_accepts_valid_numbers() { - assert_eq!(parse_control_port("50494", "--control"), Ok(50494)); - assert_eq!(parse_control_port(" 8080 ", "--control"), Ok(8080)); - assert_eq!(parse_control_port("0", "PHOTOCRAFT_CONTROL_PORT"), Ok(0)); - } - - #[test] - fn control_port_rejects_unparseable_values_naming_them() { - // Out of range, a following flag eaten as the value, and an empty value all fail, - // naming the offending value in the message. - let err = parse_control_port("78787", "--control").unwrap_err(); - assert!(err.contains("78787"), "{err}"); - assert!(parse_control_port("--safe-gpu", "--control").is_err()); - assert!(parse_control_port("", "--control").is_err()); - } -} - -fn main() -> eframe::Result { - // First, so the panic hook and every start-up warning are recorded (`logging`). - let logger = logging::install(); - crash_guard::install_hook(); - // The system font scan can take seconds (minutes on some Macs): run it off the UI thread. - photocraft_ui_egui::type_tool::warm_fonts(); - let mut control_port: Option = None; - let mut control_arg_errors: Vec = Vec::new(); - if let Some(value) = std::env::var("PHOTOCRAFT_CONTROL_PORT").ok().filter(|v| !v.trim().is_empty()) { - match parse_control_port(&value, "PHOTOCRAFT_CONTROL_PORT") { - Ok(port) => control_port = Some(port), - Err(error) => control_arg_errors.push(error), - } - } - let mut control_token = None; - let mut control_token_file = None; - let mut automation_read_root = std::env::var_os("PHOTOCRAFT_AUTOMATION_READ_ROOT").map(std::path::PathBuf::from); - let mut automation_write_root = std::env::var_os("PHOTOCRAFT_AUTOMATION_WRITE_ROOT").map(std::path::PathBuf::from); - let mut files = Vec::new(); - // Arguments that are not valid Unicode (issue #1108): a file manager can hand over a Latin-1 - // file name on Linux, and `std::env::args()` would panic before any window opened. Paths are - // reported in a notice once the app is up; a control token is a usage error like a bad port. - let mut unreadable_paths: Vec = Vec::new(); - let mut safe_gpu = false; - let mut new_instance = false; - // Options that apply only to the process they're given to; a launch with any keeps its own. - let mut launch_options: Vec<&'static str> = Vec::new(); - let (mut present_mode_arg, mut frame_latency_arg, mut stroke_trace_arg): (Option, Option, Option) = (None, None, None); - let mut in_window_menus = std::env::var_os("PHOTOCRAFT_IN_WINDOW_MENUS").is_some_and(|v| !v.is_empty() && v != "0"); - let mut args = std::env::args_os().skip(1); - while let Some(a) = args.next() { - match a.to_str() { - Some("--control") => match args.next() { - Some(value) => match parse_control_port(&value.to_string_lossy(), "--control") { - Ok(port) => control_port = Some(port), - Err(error) => control_arg_errors.push(error), - }, - None => control_arg_errors.push("--control: missing port value (expected `--control `)".to_string()), - }, - Some("--control-token") => match args.next().map(std::ffi::OsString::into_string) { - Some(Ok(token)) => control_token = Some(token), - Some(Err(raw)) => control_arg_errors.push(format!("--control-token: value is not valid Unicode (`{}`)", raw.to_string_lossy())), - None => control_token = None, - }, - Some("--control-token-file") => control_token_file = args.next().map(std::path::PathBuf::from), - Some("--automation-read-root") => { - launch_options.push("--automation-read-root"); - automation_read_root = args.next().map(std::path::PathBuf::from); - } - Some("--automation-write-root") => { - launch_options.push("--automation-write-root"); - automation_write_root = args.next().map(std::path::PathBuf::from); - } - Some("--safe-gpu") => { - launch_options.push("--safe-gpu"); - safe_gpu = true; - } - Some(flag @ ("--present-mode" | "--frame-latency" | "--stroke-trace")) => { - launch_options.push(match flag { - "--present-mode" => "--present-mode", - "--frame-latency" => "--frame-latency", - _ => "--stroke-trace", - }); - let flag = flag.to_string(); - match args.next().map(std::ffi::OsString::into_string) { - Some(Ok(v)) => match flag.as_str() { - "--present-mode" => present_mode_arg = Some(v), - "--frame-latency" => frame_latency_arg = Some(v), - _ => stroke_trace_arg = Some(v), - }, - // A bad value is reported and ignored; it never fails the launch. - _ => eprintln!("photocraft: {flag}: missing or unreadable value; ignored"), - } - } - Some("--in-window-menus") => { - launch_options.push("--in-window-menus"); - in_window_menus = true; - } - Some("--new-instance") => new_instance = true, - Some("--version") => { - println!("photocraft {}", photocraft_engine::build_info::long_version()); - return Ok(()); - } - // Old macOS passes a process serial number when launched from Finder. - Some(s) if s.starts_with("-psn_") => {} - Some(s) => files.push(s.to_owned()), - None => unreadable_paths.push(a.to_string_lossy().into_owned()), - } - } - - // A malformed control port must not silently drop the control server (issue #701): name the - // bad value and fail the launch, like `photocraft-cli serve --port` does for the same typo. - // Exit status 2 is the usual command-line usage error, so a launcher sees the failure. - if let Some(code) = control_args_exit_code(&control_arg_errors) { - for error in &control_arg_errors { - eprintln!("photocraft: {error}"); - } - std::process::exit(code); - } - - // The log file lives under the settings directory; opened after the arguments, so `--version` - // and usage errors leave no file behind. Records logged until now are written to it first. +/// Attach the log file under the settings directory. Called after the arguments are parsed, so +/// `--version` and usage errors leave no file behind. Records logged until now are written first. +fn attach_log_file(logger: Option<&'static logging::AppLogger>) { if let (Some(logger), Some(dir)) = (logger, services::config_dir()) { match logger.attach_dir(&dir.join("logs")) { Ok(path) => log::info!("PhotoCraft {}, log file {}", photocraft_engine::build_info::long_version(), path.display()), @@ -293,59 +204,64 @@ fn main() -> eframe::Result { Err(e) => log::warn!("no log file: {e}"), } } +} - // One PhotoCraft per user (#3708): files opened from Explorer or a file manager while it runs - // become tabs in its window. Only with the per-user settings folder (a portable copy's may be - // shared between users), and not for automation, `--new-instance` or per-process options. - #[cfg(not(target_os = "macos"))] - let single_instance = { - let new_instance_env = std::env::var("PHOTOCRAFT_NEW_INSTANCE").ok(); - let context = single_instance::LaunchContext { - per_user_settings: app_dirs::current().mode == app_dirs::Mode::Platform, - new_instance_flag: new_instance, - new_instance_env: new_instance_env.as_deref(), - control: control_port.is_some(), - unreadable_paths: !unreadable_paths.is_empty(), - launch_options: &launch_options, - }; - match (single_instance::skip_reason(&context), services::config_dir()) { - (Some(reason), _) => { - log::info!("running as a separate PhotoCraft: {reason}"); - None - } - (None, None) => None, - (None, Some(dir)) => { - let paths: Vec = files.iter().map(|f| single_instance::absolute(f)).collect(); - match single_instance::claim(&dir, &paths) { - single_instance::Launch::Forwarded => { - log::info!("handed {} file(s) to the running PhotoCraft", paths.len()); - return Ok(()); - } - single_instance::Launch::Primary(primary) => Some(primary), - single_instance::Launch::Standalone(reason) => { - log::warn!("starting a separate PhotoCraft: {reason}"); - None - } +/// One PhotoCraft per user (#3708). Only with the per-user settings folder (a portable copy's may +/// be shared between users), and not for automation, `--new-instance` or per-process options. +/// `Break` when the files were handed to the running PhotoCraft and this launch is done. +#[cfg(not(target_os = "macos"))] +fn claim_single_instance( + files: &[String], + new_instance: bool, + control: bool, + unreadable_paths: bool, + mut launch_options: Vec, +) -> std::ops::ControlFlow<(), Option> { + use std::ops::ControlFlow::{Break, Continue}; + let new_instance_env = std::env::var("PHOTOCRAFT_NEW_INSTANCE").ok(); + // A variable documented as the equivalent of a per-launch flag counts as that flag: a + // launch that sets only `PHOTOCRAFT_PRESENT_MODE` (say) has per-process options too, so it + // keeps its own process instead of handing its files over and being ignored. + launch_options.extend_from_slice(&single_instance::env_launch_options()); + let context = single_instance::LaunchContext { + per_user_settings: app_dirs::current().mode == app_dirs::Mode::Platform, + new_instance_flag: new_instance, + new_instance_env: new_instance_env.as_deref(), + control, + unreadable_paths, + launch_options: &launch_options, + }; + match (single_instance::skip_reason(&context), services::config_dir()) { + (Some(reason), _) => { + log::info!("running as a separate PhotoCraft: {reason}"); + Continue(None) + } + (None, None) => Continue(None), + (None, Some(dir)) => { + let paths: Vec = files.iter().map(|f| single_instance::absolute(f)).collect(); + match single_instance::claim(&dir, &paths) { + single_instance::Launch::Forwarded => { + log::info!("handed {} file(s) to the running PhotoCraft", paths.len()); + Break(()) + } + single_instance::Launch::Primary(primary) => Continue(Some(primary)), + single_instance::Launch::Standalone(reason) => { + log::warn!("starting a separate PhotoCraft: {reason}"); + Continue(None) } } } - }; - // Lives until the event loop returns, then `shutdown` lets the next launch become the primary. - #[cfg(not(target_os = "macos"))] - let single_instance = &single_instance; - #[cfg(target_os = "macos")] - let _ = (new_instance, &launch_options); + } +} - // The display server to open the window on: Xwayland for a pen on Wayland, which gives this - // app no pen input (#639). - #[cfg(target_os = "linux")] - let native_session = linux_libs::session_from_env(|name| std::env::var(name).ok()); - #[cfg(target_os = "linux")] +/// The display server to open the window on: Xwayland for a pen on Wayland, which gives this app +/// no pen input (#639). Exits with the package to install when its libraries are missing. +#[cfg(target_os = "linux")] +fn linux_display_session() -> linux_libs::DisplaySession { let session = tablet::display_session(); // Preferences › Performance › Linux display server = X11: Xwayland on a Wayland session too, // where native file drops work (winit 0.30 has none on Wayland, #386). Only when Xwayland can // run the window ($DISPLAY set, X11 libraries installed); otherwise the session's own. - #[cfg(target_os = "linux")] let session = if session == linux_libs::DisplaySession::Wayland && gpu_startup::read_display_server(services::prefs_file().as_deref()) == photocraft_engine::prefs::LinuxDisplayServer::X11 && std::env::var_os("DISPLAY").is_some_and(|d| !d.is_empty()) @@ -356,42 +272,135 @@ fn main() -> eframe::Result { } else { session }; - // winit and wgpu dlopen the windowing and GPU libraries, and some of those crates panic when // one is missing (issue #201). Name the package to install and exit instead. - #[cfg(target_os = "linux")] if let Err(message) = linux_libs::preflight(session) { eprint!("{message}"); std::process::exit(1); } + session +} - let control = if let Some(port) = control_port { - let (supplied, token_file) = photocraft_automation::security::token_inputs(control_token, control_token_file); - let token = match photocraft_automation::security::server_token(supplied.as_deref(), token_file.as_deref()) { - Ok(token) => token, - Err(e) => { - eprintln!("photocraft: cannot configure control authentication: {e}"); - std::process::exit(2); - } - }; - if let Some(path) = token_file { - eprintln!("photocraft: control token file: {}", path.display()); - } else if supplied.is_none() { - eprintln!("photocraft: control token: {token}"); - } else { - eprintln!("photocraft: using supplied control token"); +/// The control server's port, token and workspace, when it is on (exits 2 on a bad configuration). +fn control_setup( + cli_port: Option, + auth: photocraft_automation::args::ControlArgs, + automation: &photocraft_automation::args::AutomationArgs, +) -> Option<(std::net::TcpListener, String, photocraft_automation::workspace::AuthorizedWorkspace)> { + let (port, warning) = control_port_for(cli_port, &integrations(services::prefs_file().as_deref())); + if let Some(warning) = &warning { + eprint!("{warning}"); + } + let port = port?; + let listener = match std::net::TcpListener::bind(("127.0.0.1", port)) { + Ok(listener) => listener, + Err(error) => { + eprintln!("photocraft: control server failed to bind 127.0.0.1:{port}: {error}"); + std::process::exit(2); } - let workspace = match photocraft_automation::AuthorizedWorkspace::new(automation_read_root.as_deref(), automation_write_root.as_deref()) { - Ok(workspace) => workspace, - Err(error) => { - eprintln!("photocraft: cannot configure automation workspace: {error}"); - std::process::exit(2); - } - }; - Some((port, token, workspace)) - } else { - None }; + let (supplied, token_file) = (auth.control_token, auth.control_token_file); + let token = match photocraft_automation::security::server_token(supplied.as_deref(), token_file.as_deref()) { + Ok(token) => token, + Err(e) => { + eprintln!("photocraft: cannot configure control authentication: {e}"); + std::process::exit(2); + } + }; + if let Some(path) = token_file { + eprintln!("photocraft: control token file: {}", path.display()); + } else if supplied.is_none() { + eprintln!("photocraft: control token: {token}"); + } else { + eprintln!("photocraft: using supplied control token"); + } + let workspace = match automation.workspace() { + Ok(workspace) => workspace, + Err(error) => { + eprintln!("photocraft: cannot configure automation workspace: {error}"); + std::process::exit(2); + } + }; + Some((listener, token, workspace)) +} + +/// Record this start's backend in the GPU startup marker, if the marker is locked. +fn write_start_marker(sentinel: &gpu_startup::SharedSentinel, plan: &gpu_startup::Plan) { + if let Some(s) = sentinel.lock().unwrap_or_else(std::sync::PoisonError::into_inner).as_mut() { + let backend = match &plan.env { + Some(v) => format!("env:{v}"), + None => plan.backend.name().to_string(), + }; + let marker = gpu_startup::Marker { backend, version: photocraft_engine::build_info::long_version().to_string(), ..Default::default() }; + if let Err(e) = s.write(marker) { + log::warn!("GPU startup marker: {e}"); + } + } +} + +fn main() -> eframe::Result { + // First, so the panic hook and every start-up warning are recorded (`logging`). + let logger = logging::install(); + crash_guard::install_hook(); + #[cfg(not(target_arch = "wasm32"))] + if let Some(done) = craft_update::self_update::finish(&photocraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::>()) { + if let Err(e) = done { + eprintln!("PhotoCraft update helper failed: {e}"); + std::process::exit(1); + } + return Ok(()); + } + + // The system font scan can take seconds (minutes on some Macs): run it off the UI thread. + photocraft_ui_egui::type_tool::warm_fonts(); + // `--help` and `--version` print to standard output and exit 0. A usage error (an unknown + // option, a missing or bad value, a bad PHOTOCRAFT_CONTROL_PORT: issues #701, #6429, #6431) + // names the argument on standard error and exits 2, so a launcher sees the failure. Neither + // panics when the stream is closed or a pipe (`photocraft --help | head -1`). + let launch = launch_args::parse(launch_args::command(), std::env::args_os().skip(1)).unwrap_or_else(|e| e.exit()); + let launch_args::FileNames { openable: files, unreadable: unreadable_paths } = launch.file_names(); + let launch_args::LaunchArgs { + files: _, + safe_gpu, + in_window_menus, + new_instance, + present_mode, + frame_latency, + stroke_trace, + control: control_port, + auth, + automation, + mut launch_options, + } = launch; + + attach_log_file(logger); + + // An adapter named in the environment is chosen at startup, which a forwarded launch never reaches. + let rendering_mode = gpu_startup::read_rendering_prefs(services::prefs_file().as_deref()).1; + if gpu_startup::adapter_override_is_process_specific(std::env::var("PHOTOCRAFT_GPU_ADAPTER").ok().as_deref(), rendering_mode, gpu_startup::ADAPTER_CHOICE) { + launch_options.push(launch_args::PerProcess::GpuAdapterEnv); + } + // One PhotoCraft per user (#3708): files opened from Explorer or a file manager while it runs + // become tabs in its window. Lives until the event loop returns, then `shutdown` lets the next + // launch become the primary. + #[cfg(not(target_os = "macos"))] + let single_instance = match claim_single_instance(&files, new_instance, control_port.is_some(), !unreadable_paths.is_empty(), launch_options) { + std::ops::ControlFlow::Break(()) => return Ok(()), + std::ops::ControlFlow::Continue(primary) => primary, + }; + #[cfg(not(target_os = "macos"))] + let single_instance = &single_instance; + #[cfg(target_os = "macos")] + let _ = (new_instance, &launch_options); + + // Bind and authenticate the authorized control listener before display/GPU initialization. + let control = control_setup(control_port, auth, &automation); + + #[cfg(target_os = "linux")] + let native_session = linux_libs::session_from_env(|name| std::env::var(name).ok()); + #[cfg(target_os = "linux")] + let session = linux_display_session(); + // Finder / Dock / Open With deliver files as Apple events, not arguments; catch the one that // launched us as well as later ones. Lives until the event loop returns. #[cfg(target_os = "macos")] @@ -405,6 +414,11 @@ fn main() -> eframe::Result { let tablet_monitor = std::cell::OnceCell::new(); #[cfg(target_os = "macos")] let tablet_monitor = &tablet_monitor; + // The Windows pointer message hook; dropping it unhooks when the event loop returns. + #[cfg(target_os = "windows")] + let tablet_monitor_win = std::cell::OnceCell::new(); + #[cfg(target_os = "windows")] + let tablet_monitor_win = &tablet_monitor_win; // Read the displays' ICC profiles while the window opens (colour-managed canvas; `None` // where the platform has no reader). @@ -444,26 +458,19 @@ fn main() -> eframe::Result { log::warn!("the previous start didn't finish (GPU backend {}, adapter {:?}); {}", m.backend, m.adapter, plan.reason.as_deref().unwrap_or("")); } let sentinel: gpu_startup::SharedSentinel = std::sync::Arc::new(std::sync::Mutex::new(sentinel)); - if let Some(s) = sentinel.lock().unwrap_or_else(std::sync::PoisonError::into_inner).as_mut() { - let backend = match &plan.env { - Some(v) => format!("env:{v}"), - None => plan.backend.name().to_string(), - }; - let marker = gpu_startup::Marker { backend, version: photocraft_engine::build_info::long_version().to_string(), ..Default::default() }; - if let Err(e) = s.write(marker) { - log::warn!("GPU startup marker: {e}"); - } - } + write_start_marker(&sentinel, &plan); let gpu_note: std::sync::Arc>> = Default::default(); // The adapter's real texture limits (egui asks for 8192 px), so big documents stay on the GPU. - gpu_startup::configure(&mut options.wgpu_options.wgpu_setup, &plan, os, sentinel.clone(), gpu_note.clone()); + let wanted_adapter = + gpu_startup::wanted_adapter(std::env::var("PHOTOCRAFT_GPU_ADAPTER").ok().as_deref(), &gpu_startup::read_gpu_adapter(services::prefs_file().as_deref())); + gpu_startup::configure(&mut options.wgpu_options.wgpu_setup, &plan, os, wanted_adapter, sentinel.clone(), gpu_note.clone()); // Preferences › Performance › Low-Latency Drawing (#1343): without vsync a stroke shows about // 50 ms sooner; wgpu picks Immediate, else Mailbox, else Fifo by what the driver offers. if gpu_startup::read_low_latency(services::prefs_file().as_deref()) { options.wgpu_options.surface.present_mode = eframe::wgpu::PresentMode::AutoNoVsync; } // Testers' present mode and frame latency overrides (#1343), after the preference. - let present = presentation::Overrides::from_args_or_env(present_mode_arg.as_deref(), frame_latency_arg.as_deref()); + let present = presentation::Overrides::from_args_or_env(present_mode, frame_latency); options.wgpu_options.surface = present.apply(options.wgpu_options.surface); let present_desc = presentation::describe(&options.wgpu_options.surface, &present); // Without vsync, frames are capped at 240 fps (#4472) so repaint loops don't spin flat out. @@ -476,6 +483,10 @@ fn main() -> eframe::Result { log::info!("GPU startup: {:?} ({sentinel_ms:.2} ms)", plan); let retry_cpu = !safe_gpu && plan.backend != photocraft_engine::prefs::GpuBackend::Cpu; let keep_marker = gpu_startup::keep_marker_after_error(&plan, os); + // For the failure dialog below: the plan itself moves into the window closure. With + // `WGPU_BACKEND` set (`plan.env`) wgpu picks the backend, so name that rather than `auto`. + let started_renderer = plan.env.clone().unwrap_or_else(|| plan.backend.name().to_string()); + let started_note = plan.reason.clone(); let app_created = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)); let created_in_callback = app_created.clone(); let started_sentinel = sentinel.clone(); @@ -499,7 +510,16 @@ fn main() -> eframe::Result { services::xwayland_command(var("FLATPAK_ID").as_deref(), var("APPIMAGE").as_deref(), std::env::var_os("DISPLAY").is_some()); } } - let mut app = PhotocraftApp::new(Session::new(), services); + #[cfg(target_os = "linux")] + let session = { + let mut session = Session::new(); + session.print_spooler = print_portal::spooler(); + session + }; + #[cfg(not(target_os = "linux"))] + let session = Session::new(); + PhotocraftApp::install_type_reset_hooks(&cc.egui_ctx); + let mut app = PhotocraftApp::new(session, services); app.min_frame_ms = uncapped.then_some(1000.0 / 240.0); app.integrated_titlebar = cfg!(target_os = "macos"); app.custom_titlebar = custom_titlebar; @@ -533,7 +553,7 @@ fn main() -> eframe::Result { let info = &mut app.perf.gpu_info; info.preference = pref.name().to_string(); info.present = present_desc.clone(); - if let Some(v) = &stroke_trace_arg { + if let Some(v) = &stroke_trace { app.stroke_trace.set(v); } info.selected = if plan.env.is_some() { "env".into() } else { plan.backend.name().to_string() }; @@ -544,9 +564,19 @@ fn main() -> eframe::Result { info.canvas = "cpu".into(); if let Some(rs) = cc.wgpu_render_state.clone() { app.perf.gpu_info.set_adapter(&rs.adapter.get_info()); + app.perf.gpu_info.set_available(&rs.available_adapters); + // Every egui texture uploads through this adapter, whichever canvas draws: + // Iris Xe on DX12 shows stale memory in all-zero texels (texel_guard.rs). + photocraft_ui_egui::texel_guard::install(&cc.egui_ctx, &rs.adapter.get_info()); let software_window = rs.adapter.get_info().device_type == eframe::wgpu::DeviceType::Cpu; if software_window && mode != photocraft_engine::prefs::RenderingMode::Cpu { - app.perf.gpu_info.fallback = Some("No compatible hardware graphics adapter; using software graphics.".into()); + // Keep an earlier note, such as a requested adapter that couldn't be used. + let software = "No compatible hardware graphics adapter; using software graphics."; + let fallback = &mut app.perf.gpu_info.fallback; + *fallback = Some(match fallback.take() { + Some(earlier) => format!("{earlier}. {software}"), + None => software.into(), + }); } if !software_window && plan.backend != photocraft_engine::prefs::GpuBackend::Cpu @@ -555,8 +585,9 @@ fn main() -> eframe::Result { { app.set_wgpu(rs); } else { - // The window still draws with wgpu: record its errors instead of panicking. - let _ = photocraft_ui_egui::gpu_canvas::DeviceHealth::watch(&rs.device); + // The window still draws with wgpu: record its errors instead of panicking, + // and notice when the device is lost (#3438). + app.watch_window_device(&rs.device); } } let fallback_reason = std::env::var("PHOTOCRAFT_GPU_STARTUP_FAILURE").ok().or_else(|| { @@ -582,8 +613,8 @@ fn main() -> eframe::Result { log::warn!("couldn't remember GPU backend {}: {e}", b.name()); } }); - if let Some((port, token, _)) = control { - let rx = control_server::start(port, token, cc.egui_ctx.clone()); + if let Some((listener, token, _)) = control { + let rx = control_server::start(listener, token, cc.egui_ctx.clone()); app = app.with_control(rx); } #[cfg(target_os = "macos")] @@ -623,8 +654,13 @@ fn main() -> eframe::Result { if let Some(monitor) = tablet::install_macos(&app.stylus.feed) { let _ = tablet_monitor.set(monitor); } + #[cfg(target_os = "windows")] + if let Some(monitor) = tablet::install_windows() { + let _ = tablet_monitor_win.set(monitor); + } #[cfg(target_os = "linux")] tablet::spawn_x11(&app.stylus.feed, display); + app.services.right_button_down = tablet::right_button_service(); // Paths on the command line (Linux/Windows file associations, `photocraft a.psd`). app.open_paths(&files); if !unreadable_paths.is_empty() { @@ -675,9 +711,10 @@ fn main() -> eframe::Result { s.finish(); } if let Ok(exe) = std::env::current_exe() { + // `--safe-gpu` first: after a `--`, it would be a file name. let launched = std::process::Command::new(exe) - .args(std::env::args_os().skip(1)) .arg("--safe-gpu") + .args(std::env::args_os().skip(1)) .env_remove("WGPU_BACKEND") .env("PHOTOCRAFT_GPU_STARTUP_FAILURE", &reason) .spawn(); @@ -687,9 +724,32 @@ fn main() -> eframe::Result { } } } + // A launch that fails before a window exists leaves nothing on screen, and a desktop launch + // has no console for the `Err` above: log it, then say why in a native dialog with the log + // file to attach (#7643). A terminal or automation launch can read standard error or the + // exit status instead, so it keeps the silent exit. + if let Err(error) = &result { + log::error!("PhotoCraft could not start: {error}"); + if startup_report::needs_dialog(control_port.is_some()) { + startup_report::show(&error.to_string(), &started_renderer, started_note.as_deref(), logger.and_then(logging::AppLogger::path).as_deref()); + } + } result } +/// Tests that spawn child processes hold this for reading, tests that check who holds a file lock +/// for writing ([`no_child_spawns`]): until its `exec`, a child holds a copy of every open file, +/// `flock`s included, so a spawn in another test could keep a lock this process just dropped held +/// for a moment. +#[cfg(test)] +pub(crate) static CHILD_SPAWNS: std::sync::RwLock<()> = std::sync::RwLock::new(()); + +/// Held by a test that checks who holds a file lock: no other test spawns a child meanwhile. +#[cfg(test)] +pub(crate) fn no_child_spawns() -> std::sync::RwLockWriteGuard<'static, ()> { + CHILD_SPAWNS.write().unwrap_or_else(std::sync::PoisonError::into_inner) +} + #[cfg(test)] mod tests { #[test] @@ -726,10 +786,101 @@ mod tests { egui::Panel::right("dock").default_size(290.0).show(ui, |ui| ui.set_min_width(ui.available_width())); }); output.textures_delta.clear(); - let panel = egui::containers::panel::PanelState::load(&restored, egui::Id::new("dock")).unwrap(); + let panel = egui::containers::panel::PanelState::load(&restored, photocraft_ui_egui::panels::panel_state_id(&restored, "dock")).unwrap(); assert_eq!(panel.size().x, 410.0); } + /// A 0.36 `ui.ron` saved panel widths under `Id::new("dock")`; the first 0.37 launch copies + /// them to the parent-scoped key, leaves the old one for a rollback, and a later resize + /// survives two more restarts. + #[test] + fn saved_036_panel_widths_migrate_and_survive_two_restarts() { + #[derive(Default)] + struct Storage(std::collections::BTreeMap); + impl eframe::Storage for Storage { + fn get_string(&self, key: &str) -> Option { + self.0.get(key).cloned() + } + fn set_string(&mut self, key: &str, value: String) { + self.0.insert(key.into(), value); + } + fn remove_string(&mut self, key: &str) { + self.0.remove(key); + } + fn flush(&mut self) {} + } + let state = |width: f32| egui::containers::panel::PanelState { outer_rect: egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(width, 600.0)) }; + let input = || egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1200.0, 800.0))), ..Default::default() }; + let draw = |ctx: &egui::Context| { + let mut output = ctx.run_ui(input(), |ui| { + egui::Panel::right("dock").default_size(290.0).show(ui, |ui| ui.set_min_width(ui.available_width())); + }); + output.textures_delta.clear(); + }; + let save = |ctx: &egui::Context| { + let mut storage = Storage::default(); + ctx.memory(|memory| eframe::set_value(&mut storage, "egui", memory)); + storage + }; + let restore = |storage: &Storage| { + let ctx = egui::Context::default(); + ctx.memory_mut(|memory| *memory = eframe::get_value(storage, "egui").expect("the saved memory")); + ctx + }; + + // The real 0.14/0.15 shape: the panel state under the old key inside the egui memory. + let ctx = egui::Context::default(); + ctx.data_mut(|d| d.insert_persisted(egui::Id::unique("dock"), state(410.0))); + let storage = save(&ctx); + + let first = restore(&storage); + photocraft_ui_egui::panels::migrate_panel_state(&first); + draw(&first); + let key = photocraft_ui_egui::panels::panel_state_id(&first, "dock"); + assert_eq!(egui::containers::panel::PanelState::load(&first, key).map(|p| p.size().x), Some(410.0)); + assert!(egui::containers::panel::PanelState::load(&first, egui::Id::unique("dock")).is_some(), "the 0.36 key is kept"); + // Resize in 0.37: the new key wins from now on. + first.data_mut(|d| d.insert_persisted(key, state(500.0))); + let storage = save(&first); + + for restart in 0..2 { + let next = restore(&storage); + photocraft_ui_egui::panels::migrate_panel_state(&next); + draw(&next); + let key = photocraft_ui_egui::panels::panel_state_id(&next, "dock"); + let width = egui::containers::panel::PanelState::load(&next, key).map(|p| p.size().x).unwrap_or_default(); + assert!((width - 500.0).abs() < 0.5, "restart {restart}: the old 410 key must not win (got {width})"); + } + } + + /// Schema-probe `ui.ron` shapes *reconstructed* from the documented 0.14/0.15/0.37 layouts, + /// not recorded machine saves: 0.14 and 0.15 both store the flat window entry, which the + /// first 0.37 launch rewrites; a 0.37 file is left alone. Keeping all three probes makes a + /// future change to any migration rule fail here first. They do not prove eframe's real + /// `Memory`/`PanelState` deserialization. + #[test] + fn the_ui_ron_schema_probe_fixtures_migrate() { + for (name, fixture) in [("0.14", include_str!("../tests/fixtures/ui-0.14.ron")), ("0.15", include_str!("../tests/fixtures/ui-0.15.ron"))] { + let text = super::ui_state::migrate_for_test(fixture).unwrap_or_else(|e| panic!("{name}: {e}")).unwrap_or_else(|| panic!("{name}: not rewritten")); + assert!(text.contains("normal_geometry"), "{name}: the flat window entry is gone: {text}"); + assert!(!super::ui_state::saved_position_for_test(fixture), "{name}: a flat entry is not what 0.37 restores"); + } + let old = include_str!("../tests/fixtures/ui-0.14.ron"); + assert!(super::ui_state::migrate_for_test(old).expect("migratable").expect("rewritten").contains("x:1100.0"), "the 0.14 size is preserved"); + let current = include_str!("../tests/fixtures/ui-0.15.ron"); + assert!( + super::ui_state::migrate_for_test(current).expect("migratable").expect("rewritten").contains("maximized:true"), + "0.15 maximized state survives" + ); + // Already 0.37-shaped: no rewrite, and (with no saved position) the window centres. + let current = include_str!("../tests/fixtures/ui-0.37.ron"); + assert_eq!(super::ui_state::migrate_for_test(current), Ok(None)); + assert!(!super::ui_state::saved_position_for_test(current)); + // And a 0.37 file with a restorable outer position is a saved position. + let with_position = current.replace("outer_position_pixels:None", "outer_position_pixels:Some((x:2048.0,y:1228.0))"); + assert!(super::ui_state::saved_position_for_test(&with_position)); + } + #[test] fn the_window_opens_centred_at_its_default_size() { let o = super::native_options(super::CUSTOM_TITLEBAR); @@ -798,6 +949,91 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + #[test] + fn integrations_preferences_decide_the_control_port() { + use photocraft_engine::prefs::Integrations; + let off = Integrations { allow_agent_control: false, control_port: 0 }; + let on = Integrations { allow_agent_control: true, control_port: 0 }; + let preferred = Integrations { allow_agent_control: true, control_port: 7878 }; + + // Nothing asked for, nothing listening. + assert_eq!(super::control_port_for(None, &on), (None, None)); + // --control wins over the preference. + assert_eq!(super::control_port_for(Some(9000), &preferred), (Some(9000), None)); + // The preference opens the port when --control is absent. + assert_eq!(super::control_port_for(None, &preferred), (Some(7878), None)); + + // Allow Agent Control off refuses both, and says so when --control asked for one + // rather than ignoring the flag. + assert_eq!(super::control_port_for(None, &off), (None, None)); + let (port, warning) = super::control_port_for(Some(9000), &off); + assert_eq!(port, None); + assert!(warning.is_some_and(|w| w.contains("Allow Agent Control") && w.contains("9000")), "the refusal names the port"); + let off_with_port = Integrations { allow_agent_control: false, control_port: 7878 }; + assert_eq!(super::control_port_for(None, &off_with_port), (None, None), "off beats a preferred port"); + + // A port a hand-edited file could hold, outside 1..=65535, is refused and named. + let huge = Integrations { allow_agent_control: true, control_port: 70_000 }; + let (port, warning) = super::control_port_for(None, &huge); + assert_eq!(port, None); + assert!(warning.is_some_and(|w| w.contains("70000")), "the refusal names the value"); + } + + #[test] + fn integrations_fall_back_to_the_defaults_without_a_readable_file() { + let dir = std::env::temp_dir().join(format!("pc-integrations-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("prefs.json"); + let default = photocraft_engine::prefs::Integrations::default(); + + assert_eq!(super::integrations(None), default, "no preferences file"); + std::fs::write(&file, "not json").unwrap(); + assert_eq!(super::integrations(Some(&file)), default, "an unreadable file"); + std::fs::write(&file, r#"{"integrations":{"allowAgentControl":false,"controlPort":7878}}"#).unwrap(); + let read = super::integrations(Some(&file)); + assert!(!read.allow_agent_control); + assert_eq!(read.control_port, 7878); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The app loads preferences leniently (#6611): one wrong-typed value resets only itself. So + /// a bad value elsewhere, or in the port, must not turn Allow Agent Control back on. + #[test] + fn a_bad_value_elsewhere_keeps_agent_control_off() { + let dir = std::env::temp_dir().join(format!("pc-integrations-lenient-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let file = dir.join("prefs.json"); + // What the Preferences dialog shows for the same file. + let shown = |text: &str| { + let mut session = photocraft_engine::Session::new(); + session.load_prefs_json(text).unwrap(); + session.prefs().integrations.clone() + }; + let read = |text: &str| { + std::fs::write(&file, text).unwrap(); + let read = super::integrations(Some(&file)); + assert_eq!(read, shown(text), "{text}"); + read + }; + + let r = read(r#"{"interface":{"enableAnimations":"nope"},"integrations":{"allowAgentControl":false,"controlPort":7878}}"#); + assert!(!r.allow_agent_control, "a bad value in another section"); + assert_eq!(r.control_port, 7878); + let r = read(r#"{"integrations":{"allowAgentControl":false,"controlPort":"x"}}"#); + assert!(!r.allow_agent_control, "a bad port"); + assert_eq!(r.control_port, 0, "the bad port alone takes its default"); + // A bad Allow Agent Control alone takes its default. + let r = read(r#"{"integrations":{"allowAgentControl":"no","controlPort":7878}}"#); + assert_eq!(r, photocraft_engine::prefs::Integrations { allow_agent_control: true, control_port: 7878 }); + read(r#"{"integrations":{"allowAgentControl":false,"controlPort":-1}}"#); + read(r#"{"integrations":{"allowAgentControl":false,"controlPort":4294967296}}"#); + read(r#"{"integrations":"off"}"#); + // serde reads a struct from a list too; the app does. + assert!(!read(r#"{"integrations":[false]}"#).allow_agent_control, "a list"); + assert_eq!(read(r#"{"integrations":[false,7878]}"#).control_port, 7878); + let _ = std::fs::remove_dir_all(&dir); + } + /// X11 startup notification and docks match a window to its launcher by `StartupWMClass`, /// so it has to be the class the window carries: the app id, on Wayland and X11 alike. #[test] diff --git a/apps/photocraft/src/monitor_profile.rs b/apps/photocraft/src/monitor_profile.rs index 8e33869696..c210e096ec 100644 --- a/apps/photocraft/src/monitor_profile.rs +++ b/apps/photocraft/src/monitor_profile.rs @@ -495,6 +495,7 @@ mod tests { #[cfg(unix)] #[test] fn a_hung_helper_is_stopped_and_big_replies_dont_block() { + let _spawns = crate::CHILD_SPAWNS.read().unwrap_or_else(std::sync::PoisonError::into_inner); let t0 = std::time::Instant::now(); let e = run_helper(std::process::Command::new("sleep").arg("30"), std::time::Duration::from_millis(300)).unwrap_err(); assert!(e.contains("took longer than"), "{e}"); diff --git a/apps/photocraft/src/portal_save.rs b/apps/photocraft/src/portal_save.rs index 42588e2bc4..d16432193b 100644 --- a/apps/photocraft/src/portal_save.rs +++ b/apps/photocraft/src/portal_save.rs @@ -156,6 +156,18 @@ fn file_uri_path(uri: &str) -> Option { Some(PathBuf::from(std::ffi::OsString::from_vec(out))) } +/// Which file type's extensions belong on the name the dialog returned: the one it reported, or, +/// when the dialog offered only one type, that one. It reports no pick then, and the name it was +/// given is the stem (`services::dialog_file_name`), so a bare `Swatches` has to come back as +/// `Swatches.aco`. `None` when the answer names no type this dialog offered. +fn chosen_type(filters: &[Filter], picked: Option) -> Option { + match picked { + Some(i) if i < filters.len() => Some(i), + None if filters.len() == 1 => Some(0), + _ => None, + } +} + /// Save As on Linux: the portal's dialog, with the name given the extension of the file type /// picked. When that changes the name to one that already exists, the dialog opens again under /// the new name, so the portal asks before replacing it (it asked only about the typed name). @@ -163,10 +175,10 @@ pub fn save_as(conn: &zbus::blocking::Connection, mut req: SaveRequest) -> Resul loop { let Some(saved) = save_file(conn, &req)? else { return Ok(None) }; let picked = saved.filter.as_deref().and_then(|label| req.filters.iter().position(|(l, _)| l == label)); - // With one type offered there is nothing to pick, and a typed extension is the user's. - let exts: &[String] = match picked { - Some(i) if req.filters.len() > 1 => &req.filters[i].1, - _ => &[], + // A typed extension is the user's own: `with_chosen_type` keeps one it recognises. + let exts: &[String] = match chosen_type(&req.filters, picked) { + Some(i) => &req.filters[i].1, + None => &[], }; let path = crate::services::with_chosen_type(&saved.path, req.suggested.as_deref(), exts); if path == saved.path || !path.exists() { diff --git a/apps/photocraft/src/portal_save/tests.rs b/apps/photocraft/src/portal_save/tests.rs index ed44a10d9c..7a939639af 100644 --- a/apps/photocraft/src/portal_save/tests.rs +++ b/apps/photocraft/src/portal_save/tests.rs @@ -36,17 +36,22 @@ fn mock_portal(script: Script) -> Option<(zbus::blocking::Connection, Seen, std: /// once the user is done with it (here after 300 ms). fn mock_portal_with(script: Script, own_paths: bool) -> Option<(zbus::blocking::Connection, Seen, std::process::Child)> { use std::io::BufRead; + let spawns = crate::CHILD_SPAWNS.read().unwrap_or_else(std::sync::PoisonError::into_inner); let mut daemon = std::process::Command::new("dbus-daemon").args(["--session", "--nofork", "--print-address=1"]).stdout(std::process::Stdio::piped()).spawn().ok()?; + drop(spawns); let mut address = String::new(); std::io::BufReader::new(daemon.stdout.take()?).read_line(&mut address).ok()?; let address = address.trim().to_string(); let server = zbus::blocking::connection::Builder::address(address.as_str()).unwrap().name("org.freedesktop.portal.Desktop").unwrap().build().unwrap(); let seen: Seen = Arc::default(); let log = seen.clone(); + // A stream only gets the messages that arrive after it exists: made in the thread, it could + // miss a `SaveFile` sent before the thread got going, and the call would wait forever. + let messages = zbus::blocking::MessageIterator::from(&server); std::thread::spawn(move || { let mut script = script.into_iter(); - for msg in zbus::blocking::MessageIterator::from(&server) { + for msg in messages { let Ok(msg) = msg else { break }; let header = msg.header(); if header.message_type() != zbus::message::Type::MethodCall || header.member().map(|m| m.as_str()) != Some("SaveFile") { @@ -230,3 +235,33 @@ fn a_single_file_type_keeps_the_typed_extension() { assert_eq!(save_as(&conn, req).unwrap(), Some(PathBuf::from("/tmp/photocraft-none/Swatches.ase"))); daemon.kill().ok(); } + +/// The same single type, with the name left as the dialog shows it: the bare stem comes back, and +/// that one type gives it its extension instead of a file named `Swatches` being written. +#[test] +fn a_single_file_type_gives_the_bare_name_its_extension() { + let typed: &'static str = "file:///tmp/photocraft-none/Swatches"; + let Some((conn, _seen, mut daemon)) = mock_portal(vec![(0, Some((typed, "Color Swatches")))]) else { + eprintln!("no dbus-daemon: skipped"); + return; + }; + let req = SaveRequest { filters: vec![("Color Swatches".into(), vec!["aco".into()])], ..request("Swatches.aco") }; + assert_eq!(save_as(&conn, req).unwrap(), Some(PathBuf::from("/tmp/photocraft-none/Swatches.aco"))); + daemon.kill().ok(); +} + +/// Which type's extensions the returned name gets. The portal cases above need a `dbus-daemon`, +/// so the rule itself is checked here too, which needs none. +#[test] +fn the_type_that_gives_the_extension_is_the_one_picked() { + let one: Vec = vec![("Color Swatches".into(), vec!["aco".into()])]; + // One type offered: the dialog reports no pick, and it was given the stem. + assert_eq!(chosen_type(&one, None), Some(0)); + assert_eq!(chosen_type(&one, Some(0)), Some(0)); + let many: Vec = request("test").filters; + assert_eq!(chosen_type(&many, Some(1)), Some(1)); + // Several types and no pick: nothing to add, as before. + assert_eq!(chosen_type(&many, None), None); + // An answer naming a type this dialog never offered is not a pick either. + assert_eq!(chosen_type(&many, Some(99)), None); +} diff --git a/apps/photocraft/src/presentation.rs b/apps/photocraft/src/presentation.rs index c6f7e4196a..ded323872b 100644 --- a/apps/photocraft/src/presentation.rs +++ b/apps/photocraft/src/presentation.rs @@ -2,8 +2,10 @@ //! latency without a rebuild (#1343): `PHOTOCRAFT_PRESENT_MODE` (`fifo`, `autovsync`, or `novsync`, //! also spelled `immediate`, `mailbox` or `autonovsync`: no vsync, wgpu's Immediate → Mailbox → Fifo //! by what the driver offers) and `PHOTOCRAFT_FRAME_LATENCY` (`1` to `3`, frames the swap chain may -//! queue). Unset, the platform default applies. A value that isn't one of these is ignored with a -//! warning in the log, never a failed start; a mode the driver may not offer is never requested. +//! queue). Unset, the platform default applies. A variable's value that isn't one of these is ignored +//! with a warning in the log, never a failed start; the `--present-mode` and `--frame-latency` flags +//! take the same values, and a bad one is a usage error (#7071). A mode the driver may not offer is +//! never requested. //! The result is logged at start and shown in the stroke trace. use eframe::egui_wgpu::SurfaceConfig; @@ -16,45 +18,54 @@ pub struct Overrides { pub latency: Option, } +/// A `--present-mode` / `PHOTOCRAFT_PRESENT_MODE` value, or `None` when it isn't one. +pub fn present_mode(v: &str) -> Option { + match v.trim().to_ascii_lowercase().as_str() { + // Only modes every surface takes: wgpu crashes on an explicit Immediate, Mailbox or + // FifoRelaxed the driver doesn't offer (X11/NVIDIA has no Mailbox, #1343). Without + // vsync, wgpu picks Immediate, else Mailbox, else Fifo. + "fifo" | "vsync" => Some(PresentMode::Fifo), + "autovsync" => Some(PresentMode::AutoVsync), + "novsync" | "immediate" | "mailbox" | "autonovsync" => Some(PresentMode::AutoNoVsync), + _ => None, + } +} + +/// A `--frame-latency` / `PHOTOCRAFT_FRAME_LATENCY` value (1 to 3), or `None` when it isn't one. +pub fn frame_latency(v: &str) -> Option { + v.trim().parse().ok().filter(|n| (1..=3).contains(n)) +} + /// Parse the two variables' values. Returns the overrides and a warning per rejected value. pub fn parse(mode: Option<&str>, latency: Option<&str>) -> (Overrides, Vec) { let mut warnings = Vec::new(); let mode = mode.map(str::trim).filter(|v| !v.is_empty()).and_then(|v| { - let m = match v.to_ascii_lowercase().as_str() { - // Only modes every surface takes: wgpu crashes on an explicit Immediate, Mailbox or - // FifoRelaxed the driver doesn't offer (X11/NVIDIA has no Mailbox, #1343). Without - // vsync, wgpu picks Immediate, else Mailbox, else Fifo. - "fifo" | "vsync" => PresentMode::Fifo, - "autovsync" => PresentMode::AutoVsync, - "novsync" | "immediate" | "mailbox" | "autonovsync" => PresentMode::AutoNoVsync, - _ => { - warnings.push(format!("PHOTOCRAFT_PRESENT_MODE={v:?} isn't fifo, autovsync or novsync (immediate, mailbox); ignored")); - return None; - } - }; - Some(m) - }); - let latency = latency.map(str::trim).filter(|v| !v.is_empty()).and_then(|v| match v.parse::() { - Ok(n @ 1..=3) => Some(n), - _ => { - warnings.push(format!("PHOTOCRAFT_FRAME_LATENCY={v:?} isn't 1, 2 or 3; ignored")); - None + let m = present_mode(v); + if m.is_none() { + warnings.push(format!("PHOTOCRAFT_PRESENT_MODE={v:?} isn't fifo, autovsync or novsync (immediate, mailbox); ignored")); } + m + }); + let latency = latency.map(str::trim).filter(|v| !v.is_empty()).and_then(|v| { + let n = frame_latency(v); + if n.is_none() { + warnings.push(format!("PHOTOCRAFT_FRAME_LATENCY={v:?} isn't 1, 2 or 3; ignored")); + } + n }); (Overrides { mode, latency }, warnings) } impl Overrides { - /// `--present-mode` / `--frame-latency` when given, else `PHOTOCRAFT_PRESENT_MODE` / - /// `PHOTOCRAFT_FRAME_LATENCY`; rejected values are logged. - pub fn from_args_or_env(mode: Option<&str>, latency: Option<&str>) -> Self { - let env_mode = std::env::var("PHOTOCRAFT_PRESENT_MODE").ok(); - let env_latency = std::env::var("PHOTOCRAFT_FRAME_LATENCY").ok(); - let (o, warnings) = parse(mode.or(env_mode.as_deref()), latency.or(env_latency.as_deref())); + /// `--present-mode` / `--frame-latency` when given (checked by the command line), else + /// `PHOTOCRAFT_PRESENT_MODE` / `PHOTOCRAFT_FRAME_LATENCY`, whose rejected values are logged. + pub fn from_args_or_env(mode: Option, latency: Option) -> Self { + let env = |given: bool, name: &str| if given { None } else { std::env::var(name).ok() }; + let (o, warnings) = parse(env(mode.is_some(), "PHOTOCRAFT_PRESENT_MODE").as_deref(), env(latency.is_some(), "PHOTOCRAFT_FRAME_LATENCY").as_deref()); for w in warnings { log::warn!("{w}"); } - o + Self { mode: mode.or(o.mode), latency: latency.or(o.latency) } } /// `surface` with the overrides applied. diff --git a/apps/photocraft/src/print_portal.rs b/apps/photocraft/src/print_portal.rs new file mode 100644 index 0000000000..0eef02a01c --- /dev/null +++ b/apps/photocraft/src/print_portal.rs @@ -0,0 +1,239 @@ +//! Printing through the XDG Print portal (`org.freedesktop.portal.Print`): the system print +//! dialog, which a sandboxed app can reach without a CUPS socket. The engine prints over IPP +//! when it can reach a print server (`photocraft-ipp`); this is its fallback when it cannot, and +//! the only route with `PHOTOCRAFT_PRINT_PORTAL=1` (`0` disables it). `PreparePrint` shows the +//! dialog seeded with the job's copies and paper; `Print` hands it the PDF. Both answer through a +//! `Request` object's `Response` signal, which this module waits for on its own thread so the +//! window keeps painting while the dialog is open; the outcome goes to the log. +//! https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Print.html + +#[cfg(target_os = "linux")] +use photocraft_engine::print_cmds::{PrintJob, Spooler}; + +/// The spooler to install on the session, unless `PHOTOCRAFT_PRINT_PORTAL=0`. The portal itself +/// is looked up per job, so a missing one costs nothing at start-up. +#[cfg(target_os = "linux")] +pub fn spooler() -> Option { + let setting = std::env::var("PHOTOCRAFT_PRINT_PORTAL").ok(); + if setting.as_deref() == Some("0") { + return None; + } + Some(Spooler { name: "portal", preferred: setting.as_deref() == Some("1"), run: std::sync::Arc::new(print) }) +} + +/// Opens the system print dialog for the job and returns as soon as it is up. +#[cfg(target_os = "linux")] +fn print(job: &PrintJob) -> Result { + let conn = zbus::blocking::connection::Builder::session() + .and_then(|b| b.method_timeout(std::time::Duration::from_secs(5)).build()) + .map_err(|e| format!("no session bus for the print portal ({e}); print to PDF instead"))?; + let proxy = portal(&conn)?; + let version: u32 = proxy.get_property("version").map_err(|e| format!("no print portal on this desktop ({e}); print to PDF instead"))?; + // The document goes to a private temporary file (0600, created atomically by `tempfile`), + // not a guessable name in a shared /tmp. The guard owns the file: it lives on the portal + // thread until the dialog is done, and a failure to start that thread drops it here. + let mut file = tempfile::Builder::new().prefix("photocraft-print-").suffix(".pdf").tempfile().map_err(|e| format!("cannot create the print file: {e}"))?; + std::io::Write::write_all(&mut file, &job.pdf).map_err(|e| format!("cannot write the print file: {e}"))?; + let (title, settings, page_setup) = (job.title.clone(), settings(job), page_setup(job)); + std::thread::Builder::new() + .name("print-portal".into()) + .spawn(move || { + let outcome = dialog(&conn, &title, &settings, &page_setup, file.path()); + drop(file); + match outcome { + Ok(what) => log::info!("print portal (v{version}): {what}"), + Err(e) => log::warn!("print portal (v{version}): {e}"), + } + }) + .map_err(|e| format!("cannot start the print thread: {e}"))?; + Ok("the system print dialog".into()) +} + +#[cfg(target_os = "linux")] +fn portal(conn: &zbus::blocking::Connection) -> Result, String> { + zbus::blocking::Proxy::new(conn, "org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", "org.freedesktop.portal.Print") + .map_err(|e| e.to_string()) +} + +/// `PreparePrint` then `Print`, each waited for on its request's `Response` signal. +#[cfg(target_os = "linux")] +fn dialog( + conn: &zbus::blocking::Connection, + title: &str, + settings: &[(&'static str, Seed)], + page_setup: &[(&'static str, Seed)], + pdf: &std::path::Path, +) -> Result { + use std::collections::HashMap; + use zbus::zvariant::{OwnedObjectPath, OwnedValue, Value}; + type Dict<'a> = HashMap<&'a str, Value<'a>>; + fn as_dict(pairs: &[(&'static str, Seed)]) -> Dict<'static> { + pairs + .iter() + .map(|(k, v)| { + let value = match v { + Seed::Text(t) => Value::from(t.clone()), + Seed::Mm(mm) => Value::from(*mm), + }; + (*k, value) + }) + .collect() + } + let proxy = portal(conn)?; + let sender = conn.unique_name().map(|n| n.as_str().to_string()).ok_or("the bus connection has no name")?; + let wait = |token: &str| -> Result, String> { + let path = request_path(&sender, token); + let request = zbus::blocking::Proxy::new(conn, "org.freedesktop.portal.Desktop", path, "org.freedesktop.portal.Request").map_err(|e| e.to_string())?; + request.receive_signal("Response").map_err(|e| e.to_string()) + }; + let response = |mut signals: zbus::blocking::proxy::SignalIterator<'static>| -> Result, String> { + let message = signals.next().ok_or("the print portal went away")?; + let (code, results): (u32, HashMap) = message.body().deserialize().map_err(|e| format!("odd portal reply: {e}"))?; + match code { + 0 => Ok(results), + 1 => Err("cancelled in the print dialog".into()), + _ => Err("the print dialog failed".into()), + } + }; + + let token = format!("photocraft_prepare_{}", nonce()); + let signals = wait(&token)?; + let mut options: Dict<'_> = HashMap::new(); + options.insert("handle_token", Value::from(token.as_str())); + let _: OwnedObjectPath = + proxy.call("PreparePrint", &("", title, as_dict(settings), as_dict(page_setup), options)).map_err(|e| format!("PreparePrint failed: {e}"))?; + let results = response(signals)?; + let prepared = results.get("token").and_then(|v| u32::try_from(v.clone()).ok()).ok_or("the print dialog gave no token")?; + + let file = std::fs::File::open(pdf).map_err(|e| format!("cannot reopen the print file: {e}"))?; + let token = format!("photocraft_print_{}", nonce()); + let signals = wait(&token)?; + let mut options: Dict<'_> = HashMap::new(); + options.insert("handle_token", Value::from(token.as_str())); + options.insert("token", Value::from(prepared)); + let fd = zbus::zvariant::Fd::from(std::os::fd::AsFd::as_fd(&file)); + let _: OwnedObjectPath = proxy.call("Print", &("", title, fd, options)).map_err(|e| format!("Print failed: {e}"))?; + response(signals)?; + Ok("printed through the system print dialog".into()) +} + +#[cfg(target_os = "linux")] +fn nonce() -> u64 { + use std::sync::atomic::{AtomicU64, Ordering}; + static N: AtomicU64 = AtomicU64::new(0); + let t = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(0); + t ^ N.fetch_add(1, Ordering::Relaxed) +} + +/// The request object the portal answers on: the sender's unique name without its colon and +/// with dots as underscores, then the handle token. +fn request_path(unique_name: &str, token: &str) -> String { + format!("/org/freedesktop/portal/desktop/request/{}/{token}", unique_name.trim_start_matches(':').replace('.', "_")) +} + +/// A value seeded into the dialog: GtkPrintSettings are strings; a GtkPageSetup's sizes are +/// doubles in millimetres (`gtk_paper_size_new_from_gvariant` ignores anything else). +#[derive(Debug, Clone, PartialEq)] +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +enum Seed { + Text(String), + Mm(f64), +} + +fn text(s: &str) -> Seed { + Seed::Text(s.to_string()) +} + +/// The standard paper sizes, as (PWG name, PPD name, width mm, height mm), portrait. GTK's +/// print settings take the PWG name; its page setup finds the size by the PPD name. +const PAPERS: [(&str, &str, f64, f64); 8] = [ + ("na_letter", "Letter", 215.9, 279.4), + ("na_legal", "Legal", 215.9, 355.6), + ("na_ledger", "Ledger", 279.4, 431.8), + ("iso_a3", "A3", 297.0, 420.0), + ("iso_a4", "A4", 210.0, 297.0), + ("iso_a5", "A5", 148.0, 210.0), + ("na_index-4x6", "4x6", 101.6, 152.4), + ("na_5x7", "5x7", 127.0, 177.8), +]; + +const MM_PER_PT: f64 = 25.4 / 72.0; + +type Paper = (&'static str, &'static str, f64, f64); + +/// The job's paper: its row in `PAPERS` when it is a standard size, and the portrait size in +/// millimetres. +fn paper(paper_pt: (f64, f64)) -> (Option<&'static Paper>, f64, f64) { + let (w, h) = (paper_pt.0.min(paper_pt.1) * MM_PER_PT, paper_pt.0.max(paper_pt.1) * MM_PER_PT); + let row = PAPERS.iter().find(|(_, _, pw, ph)| (pw - w).abs() < 1.0 && (ph - h).abs() < 1.0); + (row, w, h) +} + +fn orientation(landscape: bool) -> Seed { + text(if landscape { "landscape" } else { "portrait" }) +} + +/// `PreparePrint`'s settings (GtkPrintSettings keys, string values). +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +fn settings_for(copies: u32, paper_pt: (f64, f64), landscape: bool) -> Vec<(&'static str, Seed)> { + let (row, w, h) = paper(paper_pt); + let mut v = vec![("n-copies", text(&copies.to_string())), ("orientation", orientation(landscape))]; + match row { + Some((pwg, _, _, _)) => v.push(("paper-format", text(pwg))), + None => v.extend([("paper-width", text(&format!("{w:.2}"))), ("paper-height", text(&format!("{h:.2}")))]), + } + v +} + +/// `PreparePrint`'s page setup (GtkPageSetup keys): the size in millimetres, found by its PPD +/// name when it has one, else a custom size named by its dimensions. +#[cfg_attr(not(target_os = "linux"), allow(dead_code))] +fn page_setup_for(paper_pt: (f64, f64), landscape: bool) -> Vec<(&'static str, Seed)> { + let (row, w, h) = paper(paper_pt); + let mut v = vec![("Orientation", orientation(landscape)), ("Width", Seed::Mm(w)), ("Height", Seed::Mm(h))]; + match row { + Some((_, ppd, _, _)) => v.push(("PPDName", text(ppd))), + None => v.push(("Name", text(&format!("custom_{w:.0}x{h:.0}mm")))), + } + v +} + +#[cfg(target_os = "linux")] +fn settings(job: &PrintJob) -> Vec<(&'static str, Seed)> { + settings_for(job.copies, job.paper, job.landscape) +} + +#[cfg(target_os = "linux")] +fn page_setup(job: &PrintJob) -> Vec<(&'static str, Seed)> { + page_setup_for(job.paper, job.landscape) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn request_path_follows_the_portal_rule() { + assert_eq!(request_path(":1.42", "photocraft_prepare_7"), "/org/freedesktop/portal/desktop/request/1_42/photocraft_prepare_7"); + } + + #[test] + fn standard_papers_get_their_pwg_and_ppd_names_and_custom_ones_their_size() { + // A4 landscape as the engine lays it out: width > height, still A4. + let s = settings_for(2, (841.89, 595.28), true); + assert_eq!(s, vec![("n-copies", text("2")), ("orientation", text("landscape")), ("paper-format", text("iso_a4"))]); + let p = page_setup_for((841.89, 595.28), true); + assert_eq!(p[0], ("Orientation", text("landscape"))); + let Seed::Mm(w) = p[1].1 else { panic!("width is millimetres") }; + let Seed::Mm(h) = p[2].1 else { panic!("height is millimetres") }; + assert!((w - 210.0).abs() < 0.01 && (h - 297.0).abs() < 0.01, "{w} x {h}"); + assert_eq!(p[3], ("PPDName", text("A4"))); + assert_eq!(settings_for(1, (612.0, 792.0), false)[2], ("paper-format", text("na_letter"))); + assert_eq!(page_setup_for((612.0, 792.0), false)[3], ("PPDName", text("Letter"))); + assert_eq!(settings_for(1, (288.0, 432.0), false)[2], ("paper-format", text("na_index-4x6"))); + // 100 x 200 mm is nobody's standard. + let custom = settings_for(1, (283.46, 566.93), false); + assert_eq!(&custom[2..], &[("paper-width", text("100.00")), ("paper-height", text("200.00"))]); + assert_eq!(page_setup_for((283.46, 566.93), false)[3], ("Name", text("custom_100x200mm"))); + } +} diff --git a/apps/photocraft/src/screen_color.rs b/apps/photocraft/src/screen_color.rs index 8a0d4ca901..ec8cd6378c 100644 --- a/apps/photocraft/src/screen_color.rs +++ b/apps/photocraft/src/screen_color.rs @@ -1,6 +1,11 @@ //! Desktop colour capture starts only on an explicit UI request. No snapshots are written to //! disk, transmitted or retained after a pick. Wayland delegates to its permission portal. -use photocraft_ui_egui::screen_picker::{Capture, MAX_PIXELS, Pending, ScreenImage, Service}; +use photocraft_ui_egui::screen_picker::{Capture, Pending, Service}; +// Only the platform capture backends (and the tests) build images and check sizes. +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", test))] +use photocraft_ui_egui::screen_picker::MAX_PIXELS; +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))] +use photocraft_ui_egui::screen_picker::ScreenImage; pub fn service(cc: &eframe::CreationContext<'_>, wayland: bool) -> Option { if !cfg!(any(target_os = "linux", target_os = "macos", target_os = "windows")) { @@ -34,6 +39,7 @@ pub fn service(cc: &eframe::CreationContext<'_>, wayland: bool) -> Option Option { placements.iter().find(|p| p.x == x && p.y == y && p.width == w && p.height == h).map(|p| p.index) } +#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", test))] fn count(width: usize, height: usize) -> Result { width.checked_mul(height).filter(|&n| n > 0 && n <= MAX_PIXELS).ok_or_else(|| "Screen capture exceeds the 64 MP memory budget".into()) } diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index 6c34fda33a..09d40d2cde 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -1,12 +1,10 @@ //! Native platform services: file dialogs (rfd), filesystem, codecs. -use photocraft_codecs::{ChannelLayout, EncodeOptions, Image, SampleType as CS}; -use photocraft_color::{ColorMode, SampleType}; -use photocraft_doc::{Document, Layer, Size}; +use photocraft_codecs::{ChannelLayout, EncodeOptions, Image}; +use photocraft_doc::Document; use photocraft_format::RecoveryStore; -use photocraft_geom::Rect; use photocraft_ui_egui::{FileDialogAnswer, FileDialogReply, FileDialogRequest, Recoverable, Services}; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::future::Future; use std::path::{Path, PathBuf}; use std::pin::Pin; @@ -17,12 +15,13 @@ use std::sync::Arc; mod recovery_tests; /// Everything File › Open reads: PhotoCraft, Photoshop, OpenRaster, Krita and Affinity documents, flat images, and -/// Photoshop brushes (.abr), gradients (.grd), swatches (.aco, .ase) and keyboard shortcut sets +/// Photoshop brushes (.abr), gradients (.grd), layer styles (.asl), swatches (.aco, .act, .ase) and keyboard shortcut sets /// (.kys), which go to the preset libraries and Edit › Keyboard Shortcuts. const OPEN_EXTS: &[&str] = &[ - "pcraft", "pdn", "ora", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "ico", "qoi", "exr", "hdr", - "pbm", "pgm", "ppm", "pam", "pfm", "pcx", "dds", "xpm", "fits", "fit", "fts", "dpx", "cin", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", - "arw", "pef", "orf", "rw2", "raf", "abr", "grd", "svg", "svgz", "aco", "ase", "kys", "af", "afdesign", "afphoto", "afpub", + "pcraft", "pdn", "ora", "xcf", "xcfgz", "gz", "kra", "krz", "psd", "psb", "psdt", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "dib", "tga", + "ico", "qoi", "exr", "hdr", "pbm", "pgm", "ppm", "pnm", "pam", "pfm", "pcx", "dds", "xpm", "fits", "fit", "fts", "dpx", "cin", "dcm", "dicom", "dc3", + "iff", "lbm", "ilbm", "heic", "heif", "hif", "dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf", "rw2", "raf", "abr", "grd", "asl", "svg", "svgz", + "aco", "act", "ase", "kys", "af", "afdesign", "afphoto", "afpub", ]; const SVG_EXTS: &[&str] = &["svg", "svgz"]; @@ -36,16 +35,51 @@ fn open_filter_extensions(extensions: &[&str]) -> Vec { extensions.iter().map(|ext| if case_sensitive_globs { ext.chars().map(class).collect() } else { ext.to_string() }).collect() } -/// An explicit SVG filter keeps vector artwork discoverable even when a native picker defaults to -/// the PhotoCraft-only filter. SVG is also included in All Formats for normal multi-format opens. +/// File › Open's file types after All Formats, as Photoshop lists them: one per format, so a folder +/// of mixed files can be narrowed to one kind (#6337). Together they cover every extension of +/// [`OPEN_EXTS`]. An explicit SVG filter also keeps vector artwork discoverable when a native +/// picker defaults to another filter. +const OPEN_FILTERS: &[(&str, &[&str])] = &[ + ("PhotoCraft", &["pcraft"]), + ("Photoshop", &["psd", "psb", "psdt"]), + ("Affinity", &["af", "afdesign", "afphoto", "afpub"]), + ("Krita", &["kra", "krz"]), + ("GIMP", &["xcf", "xcfgz", "gz"]), + ("OpenRaster", &["ora"]), + ("Paint.NET", &["pdn"]), + ("SVG", SVG_EXTS), + ("PNG", &["png"]), + ("JPEG", &["jpg", "jpeg"]), + ("HEIF", &["heic", "heif", "hif"]), + ("TIFF", &["tif", "tiff"]), + ("WebP", &["webp"]), + ("GIF", &["gif"]), + ("BMP", &["bmp", "dib"]), + ("Targa", &["tga"]), + ("ICO", &["ico"]), + ("QOI", &["qoi"]), + ("PCX", &["pcx"]), + ("DDS", &["dds"]), + ("XPM", &["xpm"]), + ("Portable Bitmap", &["pbm", "pgm", "ppm", "pnm", "pam", "pfm"]), + ("OpenEXR", &["exr"]), + ("Radiance HDR", &["hdr"]), + ("FITS", &["fits", "fit", "fts"]), + ("DPX and Cineon", &["dpx", "cin"]), + ("Amiga IFF", &["iff", "lbm", "ilbm"]), + ("DICOM", &["dcm", "dicom", "dc3"]), + ("Camera Raw", &["dng", "cr2", "cr3", "nef", "nrw", "arw", "pef", "orf", "rw2", "raf"]), + ("Brushes", &["abr"]), + ("Gradients", &["grd"]), + ("Styles", &["asl"]), + ("Swatches", &["aco", "act", "ase"]), + ("Keyboard Shortcuts", &["kys"]), +]; + fn open_filters() -> Vec<(&'static str, Vec)> { - vec![ - ("All Formats", open_filter_extensions(OPEN_EXTS)), - ("PhotoCraft", open_filter_extensions(&["pcraft"])), - ("OpenRaster", open_filter_extensions(&["ora"])), - ("Krita", open_filter_extensions(&["kra", "krz"])), - ("SVG", open_filter_extensions(SVG_EXTS)), - ] + std::iter::once(("All Formats", open_filter_extensions(OPEN_EXTS))) + .chain(OPEN_FILTERS.iter().map(|(name, exts)| (*name, open_filter_extensions(exts)))) + .collect() } /// File › Save As formats: (filter name, extensions). The filter matching the suggested name's @@ -57,26 +91,47 @@ const SAVE_FILTERS: &[(&str, &[&str])] = &[ ("PhotoCraft", &["pcraft"]), ("OpenRaster", &["ora"]), ("PNG", &["png"]), - ("JPEG", &["jpg"]), + ("JPEG", &["jpg", "jpeg"]), + ("JPEG XL", &["jxl"]), ("WebP", &["webp"]), - ("TIFF", &["tif"]), + ("TIFF", &["tif", "tiff"]), ("Targa", &["tga"]), // Also in Photoshop's Save As list; ICO is PhotoCraft's own (#4357): up to 256×256, with alpha. - ("BMP", &["bmp"]), + ("BMP", &["bmp", "dib"]), ("GIF", &["gif"]), ("ICO", &["ico"]), ("OpenEXR", &["exr"]), + // The encoder picks P5, P6, P7 or PFM from the image, not from the name, so only the neutral + // .pnm is offered: a .pbm or .pgm name would hold pixmap data (CodeRabbit on #9775). + ("Netpbm", &["pnm"]), + ("Radiance HDR", &["hdr"]), ("FITS", &["fits", "fit"]), ("DPX", &["dpx"]), + // Game and 3D textures; DDS Options asks for the compression and mipmaps (#1327). + ("DDS", &["dds"]), + ("Cineon", &["cin"]), + ("Amiga IFF", &["iff"]), ]; /// Non-document files the shell saves (Swatches panel exports): offered alone, so the dialog /// never swaps their extension for a document format's. -const OTHER_SAVE_FILTERS: &[(&str, &[&str])] = &[("Color Swatches", &["aco"]), ("Swatch Exchange", &["ase"])]; +const OTHER_SAVE_FILTERS: &[(&str, &[&str])] = &[("Color Swatches", &["aco"]), ("Color Table", &["act"]), ("Swatch Exchange", &["ase"])]; + +/// `suggested` with a Netpbm subtype's extension (an opened .pgm) as the neutral .pnm, the name +/// every save dialog starts from: the encoder picks P5, P6, P7 or PFM from the image, so the old +/// suffix could name other data, and macOS and rfd keep the suggested name as it is. +fn netpbm_as_pnm(suggested: &str) -> String { + let path = Path::new(suggested); + let subtype = path.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).is_some_and(|e| ["pbm", "pgm", "ppm", "pam", "pfm"].contains(&e.as_str())); + if subtype { path.with_extension("pnm").to_string_lossy().into_owned() } else { suggested.to_string() } +} /// Lists the save dialog's file types with the `suggested` type first (added if unlisted), so the dialog keeps that extension instead of .psd. fn save_filters(suggested: &str) -> Vec<(String, Vec)> { let ext = Path::new(suggested).extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default(); + // A Netpbm subtype's name (an opened .pgm) starts on the Netpbm type as .pnm: the encoder picks + // the subtype from the image, so the old name could describe other data (CodeRabbit on #9775). + let ext = if ["pbm", "pgm", "ppm", "pam", "pfm"].contains(&ext.as_str()) { "pnm".to_string() } else { ext }; if let Some((name, exts)) = OTHER_SAVE_FILTERS.iter().find(|(_, exts)| exts.contains(&ext.as_str())) { return vec![(name.to_string(), exts.iter().map(|e| e.to_string()).collect())]; } @@ -160,17 +215,14 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, let dialog = if let Some(exts) = extensions { dialog.add_filter("Supported Files", &open_filter_extensions(&exts.iter().map(String::as_str).collect::>())) } else { - open_filters() - .into_iter() - .fold(dialog, |dialog, (name, extensions)| dialog.add_filter(name, &extensions)) - .add_filter("Paint.NET", &open_filter_extensions(&["pdn"])) + open_filters().into_iter().fold(dialog, |dialog, (name, extensions)| dialog.add_filter(name, &extensions)) }; if multiple { let picked = dialog.pick_files(); - Box::pin(async move { picked.await.map(|files| FileDialogAnswer::Paths(files.iter().map(path_of).collect())) }) + Box::pin(async move { picked.await.map(|files| answer(files.iter().map(|f| f.path()), false, FileDialogAnswer::Paths)) }) } else { let picked = dialog.pick_file(); - Box::pin(async move { picked.await.map(|file| FileDialogAnswer::Paths(vec![path_of(&file)])) }) + Box::pin(async move { picked.await.map(|file| answer([file.path()], false, FileDialogAnswer::Paths)) }) } } FileDialogRequest::Folder { initial_dir } => { @@ -178,9 +230,10 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, dialog = dialog.set_directory(dir); } let picked = dialog.pick_folder(); - Box::pin(async move { picked.await.map(|folder| FileDialogAnswer::FolderTo(path_of(&folder))) }) + Box::pin(async move { picked.await.map(|folder| answer_one(folder.path(), true, FileDialogAnswer::FolderTo)) }) } FileDialogRequest::Save { suggested } => { + let suggested = netpbm_as_pnm(&suggested); let filters = save_filters(&suggested); let name = dialog_file_name(&suggested, cfg!(windows)); let dir = Path::new(&suggested).parent().filter(|p| !p.as_os_str().is_empty()).map(Path::to_path_buf); @@ -213,10 +266,10 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, .map_err(|e| e.to_string()) .and_then(|conn| crate::portal_save::save_as(&conn, req)); match portal { - Ok(path) => path.map(|p| FileDialogAnswer::SaveTo(p.to_string_lossy().into_owned())), + Ok(path) => path.map(|p| answer_one(&p, true, FileDialogAnswer::SaveTo)), Err(e) => { log::info!("portal save dialog unavailable ({e}), using rfd"); - dialog.save_file().await.map(|file| FileDialogAnswer::SaveTo(path_of(&file))) + dialog.save_file().await.map(|file| answer_one(file.path(), true, FileDialogAnswer::SaveTo)) } } }) @@ -225,7 +278,7 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, { let _ = filters; let picked = dialog.save_file(); - Box::pin(async move { picked.await.map(|file| FileDialogAnswer::SaveTo(path_of(&file))) }) + Box::pin(async move { picked.await.map(|file| answer_one(file.path(), true, FileDialogAnswer::SaveTo)) }) } } }; @@ -239,12 +292,26 @@ fn show_file_dialog(request: FileDialogRequest, parent: Option<&eframe::Frame>, } } -fn path_of(file: &rfd::FileHandle) -> String { - file.path().to_string_lossy().into_owned() +/// The dialog's answer for the chosen `paths`, or the error for one whose name isn't valid +/// Unicode, which a lossy copy would turn into another file's name (#6601). `saving`: the paths +/// are to be written to. +fn answer<'a>(paths: impl IntoIterator, saving: bool, make: impl FnOnce(Vec) -> FileDialogAnswer) -> FileDialogAnswer { + match paths.into_iter().map(|p| photocraft_ui_egui::file_open::path_text(p, saving)).collect() { + Ok(paths) => make(paths), + Err(e) => FileDialogAnswer::Unavailable(e), + } +} + +/// [`answer`] for a dialog that picks one path. +fn answer_one(path: &Path, saving: bool, make: impl FnOnce(String) -> FileDialogAnswer) -> FileDialogAnswer { + match photocraft_ui_egui::file_open::path_text(path, saving) { + Ok(path) => make(path), + Err(e) => FileDialogAnswer::Unavailable(e), + } } /// Runs `future` to completion on this thread, sleeping until it is woken. -fn block_on(future: impl Future) -> T { +pub(crate) fn block_on(future: impl Future) -> T { struct Unpark(std::thread::Thread); impl std::task::Wake for Unpark { fn wake(self: Arc) { @@ -264,8 +331,8 @@ fn block_on(future: impl Future) -> T { } /// Per-user settings directory: `PHOTOCRAFT_CONFIG_DIR`, else `/PhotoCraftData` in -/// portable mode, else the platform convention. Everything the app persists lives under it; see -/// [`crate::app_dirs`]. +/// portable mode, else the platform convention. Everything the app persists lives under it +/// except, on Windows and Linux, crash-recovery autosaves; see [`crate::app_dirs`]. pub fn config_dir() -> Option { crate::app_dirs::config_dir() } @@ -280,8 +347,11 @@ pub fn presets_dir() -> Option { config_dir().map(|d| d.join("Presets")) } +/// Crash-recovery autosaves: under the settings directory, except where that is synced with the +/// settings: the Windows roaming profile and the Linux config folder (see +/// [`crate::app_dirs::resolve_recovery`]). fn recovery_dir() -> Option { - config_dir().map(|d| d.join("Recovery")) + crate::app_dirs::recovery_dir() } /// Write `bytes` crash-safely (temp file beside the target, fsync, rename, directory fsync; see @@ -296,6 +366,44 @@ fn write_atomic_progress(path: &Path, bytes: &[u8], progress: &dyn Fn(f32)) -> R photocraft_format::atomic_write_progress(path, bytes, progress).map_err(|e| e.to_string()) } +/// Whether `a` and `b` reach the same file: the same device and inode on Unix, the same final path +/// on Windows (where it holds the name's letter case as stored). False when either can't be read. +fn same_file(a: &Path, b: &Path) -> bool { + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + match (std::fs::metadata(a), std::fs::metadata(b)) { + (Ok(a), Ok(b)) => a.dev() == b.dev() && a.ino() == b.ino(), + _ => false, + } + } + #[cfg(not(unix))] + { + match (std::fs::canonicalize(a), std::fs::canonicalize(b)) { + (Ok(a), Ok(b)) => a == b, + _ => false, + } + } +} + +/// Whether `a` and `b` name one entry in one folder: `same_file` finds they reach the same file +/// in the same folder, and the folder doesn't list both names. Another spelling of a name on a +/// file system that ignores letter case is the same entry; a hard link is another entry, which a +/// save replaces while the first name keeps the old file. False when the folder can't be read. +fn same_entry(a: &Path, b: &Path, same_file: impl Fn(&Path, &Path) -> bool) -> bool { + let folder = |p: &Path| p.parent().filter(|d| !d.as_os_str().is_empty()).unwrap_or(Path::new(".")).to_path_buf(); + if !same_file(a, b) || !same_file(&folder(a), &folder(b)) { + return false; + } + let (Some(x), Some(y)) = (a.file_name(), b.file_name()) else { return false }; + if x == y { + return true; + } + let Ok(entries) = std::fs::read_dir(folder(a)) else { return false }; + let listed: Vec<_> = entries.filter_map(|e| e.ok().map(|e| e.file_name())).collect(); + !(listed.iter().any(|n| n == x) && listed.iter().any(|n| n == y)) +} + fn export_options(settings: &photocraft_ui_egui::ExportSettings) -> photocraft_io::ExportOptions { settings.io_options() } @@ -353,12 +461,13 @@ fn with_store(store: &SharedRecovery, f: impl FnOnce(&mut RecoveryStore) -> R fn recovery_services(dir: Option) -> Services { let recover_dir = dir.clone(); let store: SharedRecovery = Rc::new(RefCell::new(dir.map(RecoveryStore::new))); - let (s1, s2, s4) = (store.clone(), store.clone(), store.clone()); + let (s1, s2, s4, s5) = (store.clone(), store.clone(), store.clone(), store.clone()); Services { autosave: Some(Box::new(move |doc: &Arc, revision: u64, path: Option<&str>| { with_store(&s1, |s| s.autosave_checked(doc, revision, path.map(str::to_string)))? })), autosave_results: Some(Box::new(move || with_store(&s4, |s| s.take_completed()).unwrap_or_default())), + flush_autosaves: Some(Box::new(move || with_store(&s5, RecoveryStore::flush).unwrap_or_default())), discard_autosave: Some(Box::new(move |id: u64| { let _ = with_store(&s2, |s| s.discard(id)); })), @@ -381,6 +490,17 @@ fn recovery_services(dir: Option) -> Services { } } +/// The OS clipboard read and write run under the crash guard, like Open and Export (#7582): a +/// panic in arboard, or in decoding a copied file, becomes the paste's (or copy's) error and the +/// app keeps its documents. +fn guarded_clipboard_get(mut read: impl FnMut() -> Result)>, String> + 'static) -> photocraft_ui_egui::ClipboardGetFn { + Box::new(move || crate::crash_guard::guard("Paste", &mut read)) +} + +fn guarded_clipboard_set(mut write: impl FnMut(u32, u32, &[u8]) -> Result<(), String> + 'static) -> photocraft_ui_egui::ClipboardSetFn { + Box::new(move |w: u32, h: u32, px: &[u8]| crate::crash_guard::guard("Copy", || write(w, h, px))) +} + /// The pixels to paste when the clipboard holds copied files rather than an image (Copy in /// Files, Finder or Explorer puts paths on the clipboard, #338): the first file that decodes, /// upright, as RGBA8. Files that aren't images are skipped after reading only their header. @@ -441,6 +561,61 @@ fn image_from_offers(offers: &[String], mut fetch: impl FnMut(&str) -> Option Option<(u32, u32, Vec)> { + if u64::try_from(text.len()).ok()? > MAX_CLIPBOARD_SVG_BYTES || !text.trim_start_matches('\u{feff}').trim_start().starts_with('<') { + return None; + } + crate::crash_guard::guard("Paste", || photocraft_io::svg::rasterize_rgba8(text.as_bytes()).map_err(|e| e.to_string())).ok() +} + +/// What a paste reads from the OS clipboard, in this order: the first copied file that is an +/// image (Finder also puts the file's icon on the clipboard as an image, which would otherwise +/// paste instead of the file), the clipboard's image, then a drawing copied as SVG markup. Each +/// read runs only when the ones before it found nothing. A read that fails, such as one of a busy +/// clipboard, returns its error, and the reads after it don't run. +fn clipboard_image( + clipboard: &mut C, + files: impl FnOnce(&mut C) -> Result>, String>, + image: impl FnOnce(&mut C) -> Result)>, String>, + text: impl FnOnce(&mut C) -> Result, String>, +) -> Result)>, String> { + if let Some(img) = files(clipboard)?.and_then(|paths| image_from_files(&paths)) { + return Ok(Some(img)); + } + if let Some(img) = image(clipboard)? { + return Ok(Some(img)); + } + Ok(text(clipboard)?.and_then(|text| image_from_svg_text(&text))) +} + +/// One OS clipboard read as [`clipboard_image`] takes it: no content in the format asked for is +/// `None`, and any other failure, such as a busy clipboard or a broken pipe on Wayland, is the +/// read's error. +fn clipboard_read(read: Result) -> Result, String> { + match read { + Ok(value) => Ok(Some(value)), + Err(arboard::Error::ContentNotAvailable) => Ok(None), + Err(error) => Err(error.to_string()), + } +} + +/// Clipboard text as [`clipboard_image`] takes it: like [`clipboard_read`], except that text that +/// isn't UTF-8 is `None`, like other text that isn't SVG markup. +fn clipboard_text(read: Result) -> Result, String> { + match read { + Err(arboard::Error::ConversionFailure) => Ok(None), + read => clipboard_read(read), + } +} + /// The shell command that starts this install under XWayland, where winit delivers file drops /// (winit 0.30 has none on Wayland, #386). A Flatpak gets the X11 socket only without the Wayland /// one, an AppImage relaunches by its own path, and anything else is `photocraft` on the PATH. @@ -462,6 +637,7 @@ fn shell_word(s: &str) -> String { pub fn native(automation: Option) -> Services { let clip: Rc>> = Rc::default(); + let clip_unreadable: Rc> = Rc::default(); let automation_read = automation.clone().map(|workspace| { Box::new(move |path: &str| { let bytes = workspace.read(path).map_err(|error| error.to_string())?; @@ -492,9 +668,9 @@ pub fn native(automation: Option) -> }) as photocraft_ui_egui::AutomationCommandFn }); Services { - import: Some(Box::new(|name: &str, bytes: &[u8], max_svg_group_depth: usize| { + import: Some(Box::new(|name: &str, bytes: &[u8], opts: &photocraft_io::ImportOptions| { crate::crash_guard::guard("Open", || { - photocraft_io::import_with_svg_group_depth(name, bytes, max_svg_group_depth).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string()) + photocraft_io::import_with_options(name, bytes, opts).map(|r| (r.document, r.warnings)).map_err(|e| e.to_string()) }) })), export: Some(Box::new(|doc: &Document, path: &str, settings: &photocraft_ui_egui::ExportSettings| { @@ -504,6 +680,12 @@ pub fn native(automation: Option) -> save_file: Some(Arc::new(save_file)), file_dialog: Some(Box::new(show_file_dialog)), write: Some(Box::new(|path: &str, bytes: &[u8]| write_atomic(Path::new(path), bytes))), + file_stamp: Some(Box::new(|path: &str| { + // Follows a symlink, as Save writes through it to the file it points at. + let meta = std::fs::metadata(path).ok()?; + Some(photocraft_ui_egui::FileStamp { len: meta.len(), modified: meta.modified().ok() }) + })), + same_file: Some(Box::new(|a: &str, b: &str| same_entry(Path::new(a), Path::new(b), same_file))), automation_read, automation_write, automation_open_pcraft, @@ -517,9 +699,10 @@ pub fn native(automation: Option) -> inbox: None, open_url: Some(Box::new(|url: &str| open::that(url).map_err(|e| e.to_string()))), beep: Some(platform_beep), + check_updates: Some(crate::updates::service()), clipboard_set_image: Some({ let clip = clip.clone(); - Box::new(move |w: u32, h: u32, px: &[u8]| { + guarded_clipboard_set(move |w: u32, h: u32, px: &[u8]| { let mut slot = clip.try_borrow_mut().map_err(|_| "clipboard is busy".to_string())?; let cb = match slot.as_mut() { Some(c) => c, @@ -530,26 +713,41 @@ pub fn native(automation: Option) -> }), clipboard_get_image: Some({ let clip = clip.clone(); - Box::new(move || { - let mut slot = clip.try_borrow_mut().ok()?; + let unreadable = clip_unreadable.clone(); + guarded_clipboard_get(move || { + let mut slot = clip.try_borrow_mut().map_err(|_| "clipboard is busy".to_string())?; let cb = match slot.as_mut() { Some(c) => c, - None => slot.insert(arboard::Clipboard::new().ok()?), + None => { + // arboard opens only when some backend works. In a Flatpak on Wayland none + // does (#5031): the sandbox hides the data-control protocol from it, and + // `--socket=fallback-x11` gives no X server while a compositor runs. + let opened = arboard::Clipboard::new(); + unreadable.set(opened.is_err()); + slot.insert(opened.map_err(|e| e.to_string())?) + } }; - // Copied files first: Finder also puts the file's icon on the clipboard as an - // image, which would otherwise paste instead of the file. - if let Some(img) = cb.get().file_list().ok().and_then(|paths| image_from_files(&paths)) { - return Some(img); - } - if let Ok(img) = cb.get_image() { - return Some((img.width as u32, img.height as u32, img.bytes.into_owned())); - } - #[cfg(target_os = "linux")] - return wayland_clipboard_image(); - #[cfg(not(target_os = "linux"))] - None + clipboard_image( + cb, + |cb| clipboard_read(cb.get().file_list()), + |cb| match cb.get_image() { + Ok(img) => Ok(Some((img.width as u32, img.height as u32, img.bytes.into_owned()))), + Err(error) => { + #[cfg(target_os = "linux")] + if let Some(image) = wayland_clipboard_image() { + return Ok(Some(image)); + } + match error { + arboard::Error::ContentNotAvailable => Ok(None), + _ => Err(error.to_string()), + } + } + }, + |cb| clipboard_text(cb.get_text()), + ) }) }), + clipboard_unreadable: Some(Box::new(move || clip_unreadable.get())), load_prefs: Some(Box::new(|| std::fs::read_to_string(prefs_file()?).ok())), photoshop_shortcuts: Some(Box::new(|| crate::photoshop_settings::live_keyboard_shortcuts(&|k| std::env::var_os(k)))), save_prefs: Some(Box::new(|text: &str| write_atomic(&prefs_file().ok_or("no config directory")?, text.as_bytes()))), @@ -574,588 +772,18 @@ fn platform_beep() { { let _ = winsafe::MessageBeep(winsafe::co::MBP::OK); } - #[cfg(not(target_os = "windows"))] + // The user's alert sound, at the alert volume set in System Settings ▸ Sound. The terminal + // bell below is silent for an app started from the Finder or the Dock. + #[cfg(target_os = "macos")] + objc2_app_kit::NSBeep(); + #[cfg(not(any(target_os = "windows", target_os = "macos")))] { - // No beep API among this app's other platforms' dependencies: write the terminal bell - // (audible when launched from a terminal, which is the same cue macOS terminals use). + // No beep API among the other platforms' dependencies: write the terminal bell (audible + // when launched from a terminal). use std::io::Write as _; let _ = std::io::stdout().write_all(b"\x07").and_then(|()| std::io::stdout().flush()); } } -/// Flat-image import via photocraft-codecs (kept for reference/tests; the app uses photocraft-io). -#[allow(dead_code)] -pub fn import_flat(name: &str, bytes: &[u8]) -> Result { - let img = photocraft_codecs::decode(bytes).map_err(|e| e.to_string())?; - let (w, h) = (img.width(), img.height()); - let depth = match img.sample_type() { - CS::U8 => SampleType::U8, - CS::U16 => SampleType::U16, - _ => SampleType::F32, - }; - let gray = matches!(img.layout(), ChannelLayout::Gray | ChannelLayout::GrayA); - let cmyk = matches!(img.layout(), ChannelLayout::Cmyk | ChannelLayout::CmykA); - let mode = if gray { - ColorMode::Grayscale - } else if cmyk { - ColorMode::Cmyk - } else { - ColorMode::Rgb - }; - let target = match mode { - ColorMode::Grayscale => ChannelLayout::GrayA, - ColorMode::Cmyk => ChannelLayout::CmykA, - _ => ChannelLayout::Rgba, - }; - let sample = match depth { - SampleType::U8 => CS::U8, - SampleType::U16 => CS::U16, - SampleType::F32 => CS::F32, - }; - let conv = img.convert(target, sample); - let stem = std::path::Path::new(name).file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or(name.to_string()); - let mut doc = Document::new(stem, Size::new(w, h), mode, depth); - doc.icc_profile = img.icc.clone().map(std::sync::Arc::new); - if let Some((x, _)) = img.meta.dpi { - doc.resolution_dpi = x; - } - let mut layer = Layer::raster("Background", doc.pixel_format()); - let data = conv.to_normalized(); - layer.surface_mut().ok_or("new raster layer has no pixels")?.write_region(Rect::from_xywh(0, 0, w, h), &data); - doc.layers.push(layer); - Ok(doc) -} - -#[allow(dead_code)] -pub fn export_flat(doc: &Document, path: &str) -> Result, String> { - let format = photocraft_codecs::from_extension(path).ok_or_else(|| format!("unknown file type for {path}"))?; - let buf = photocraft_compose::flatten(doc); - let (w, h) = (buf.rect.width(), buf.rect.height()); - let data: Vec = buf.px.iter().flat_map(|p| *p).collect(); - let img = match doc.depth { - SampleType::U8 => Image::from_u8(w, h, ChannelLayout::Rgba, buf.to_rgba8().pixels), - SampleType::U16 => Image::from_u16(w, h, ChannelLayout::Rgba, &data.iter().map(|v| (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16).collect::>()), - SampleType::F32 => Image::from_f32(w, h, ChannelLayout::Rgba, &data), - } - .map_err(|e| e.to_string())?; - let img = match &doc.icc_profile { - Some(icc) if doc.mode == ColorMode::Rgb => img.with_icc(Some((**icc).clone())), - _ => img, - }; - photocraft_codecs::encode(&img, format, &EncodeOptions::default()).map_err(|e| e.to_string()) -} - #[cfg(test)] -mod tests { - use super::*; - use photocraft_engine::Session; - use photocraft_format::list_recovery; - use photocraft_ui_egui::{PhotocraftApp, prefs_ui}; - use serde_json::{Value, json}; - - /// #6121: the file type picked in the save dialog gives the name its extension where the - /// dialog left the one it opened with, and only there. - #[test] - fn the_picked_file_type_gives_the_name_its_extension() { - let exts = |e: &[&str]| e.iter().map(|s| s.to_string()).collect::>(); - let (png, psd) = (exts(&["png"]), exts(&["psd", "psb"])); - let opened = Some("Untitled-1.psd"); - let cases = [ - // PNG picked, the name as the dialog opened (or renamed, its extension kept). - ("/pics/Untitled-1.psd", opened, &png, "/pics/Untitled-1.png"), - ("/pics/art.psd", opened, &png, "/pics/art.png"), - ("/pics/ART.PSD", opened, &png, "/pics/ART.png"), - // No extension: the type's. One PhotoCraft doesn't save is part of the name. - ("/pics/art", opened, &png, "/pics/art.png"), - ("/pics/scan.2024", opened, &png, "/pics/scan.2024.png"), - // Typed, an extension PhotoCraft saves is the user's: kept, whatever the type says - // (the PSD type untouched, or PNG picked and .jpg typed). - ("/pics/art.png", opened, &psd, "/pics/art.png"), - ("/pics/art.jpg", opened, &png, "/pics/art.jpg"), - ("/pics/art.pcraft", opened, &png, "/pics/art.pcraft"), - // Already the type's: kept, including its other extensions. - ("/pics/art.png", opened, &png, "/pics/art.png"), - ("/pics/art.PNG", opened, &png, "/pics/art.PNG"), - ("/pics/big.psb", opened, &psd, "/pics/big.psb"), - // The dialog opened on a .pcraft: that is the extension the type replaces. - ("/pics/art.pcraft", Some("art.pcraft"), &png, "/pics/art.png"), - ("/pics/art.psd", Some("art.pcraft"), &png, "/pics/art.psd"), - ]; - for (typed, opened, chosen, want) in cases { - assert_eq!(with_chosen_type(Path::new(typed), opened, chosen), PathBuf::from(want), "{typed} (opened {opened:?})"); - } - // No type picked: the name as typed. - assert_eq!(with_chosen_type(Path::new("/pics/art.psd"), opened, &[]), PathBuf::from("/pics/art.psd")); - } - - #[cfg(all(unix, not(target_os = "macos")))] - #[test] - fn open_filters_cover_uppercase_and_mixed_case_extensions() { - // Matches `text` against a glob of literals and `[..]` classes, as the portal would after `*.`. - fn matches(glob: &str, text: &str) -> bool { - let mut text = text.chars(); - let mut glob = glob.chars(); - while let Some(g) = glob.next() { - let class: String = if g == '[' { glob.by_ref().take_while(|&c| c != ']').collect() } else { g.to_string() }; - if !text.next().is_some_and(|t| class.contains(t)) { - return false; - } - } - text.next().is_none() - } - assert_eq!(open_filter_extensions(&["cr2", "pcraft"]), ["[cC][rR]2", "[pP][cC][rR][aA][fF][tT]"]); - let globs = open_filter_extensions(OPEN_EXTS); - assert_eq!(globs.len(), OPEN_EXTS.len()); - for (ext, glob) in OPEN_EXTS.iter().zip(&globs) { - // Every casing: lower, UPPER, and alternating both ways (JpG, jPg). - let alternating = |upper_first: bool| -> String { - ext.chars().enumerate().map(|(i, c)| if (i % 2 == 0) == upper_first { c.to_ascii_uppercase() } else { c }).collect() - }; - for name in [ext.to_string(), ext.to_ascii_uppercase(), alternating(true), alternating(false)] { - assert!(matches(glob, &name), "{glob} should match .{name}"); - } - assert!(!matches(glob, &format!("{ext}x")) && !matches(glob, &ext[..ext.len() - 1]), "{glob} matches only .{ext}"); - } - assert!(!globs.iter().any(|glob| matches(glob, "txt"))); - let svg_filter = open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts).unwrap_or_default(); - assert_eq!(svg_filter, open_filter_extensions(SVG_EXTS)); - assert!(svg_filter.iter().any(|glob| matches(glob, "svg")) && svg_filter.iter().any(|glob| matches(glob, "svgz"))); - } - - #[cfg(not(all(unix, not(target_os = "macos"))))] - #[test] - fn open_filters_keep_literal_extensions_on_windows_and_macos() { - assert_eq!(open_filter_extensions(OPEN_EXTS), OPEN_EXTS); - assert_eq!(open_filter_extensions(&["pcraft"]), ["pcraft"]); - assert_eq!(open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts), Some(vec!["svg".to_string(), "svgz".to_string()])); - } - - #[test] - fn xwayland_command_matches_how_photocraft_was_installed() { - // Flatpak: WAYLAND_DISPLAY doesn't reach the sandbox's socket choice; flatpak's flags do. - let flatpak = xwayland_command(Some("ai.storyteller.photocraft"), None, false); - assert_eq!(flatpak.as_deref(), Some("flatpak run --nosocket=wayland --socket=x11 ai.storyteller.photocraft")); - // AppImage: its own path, quoted for the shell (spaces, quotes). - let appimage = xwayland_command(None, Some("/home/me/My Apps/it's.AppImage"), true); - assert_eq!(appimage.as_deref(), Some(r"WAYLAND_DISPLAY= '/home/me/My Apps/it'\''s.AppImage'")); - let bare = xwayland_command(None, Some("/opt/photocraft-0.3.0-linux-x86_64.AppImage"), true); - assert_eq!(bare.as_deref(), Some("WAYLAND_DISPLAY= /opt/photocraft-0.3.0-linux-x86_64.AppImage")); - // deb, rpm, AUR, tarball: the binary on the PATH. - assert_eq!(xwayland_command(None, None, true).as_deref(), Some("WAYLAND_DISPLAY= photocraft")); - assert_eq!(xwayland_command(Some(""), Some(""), true).as_deref(), Some("WAYLAND_DISPLAY= photocraft")); - // No X server (XWayland disabled): nothing to suggest. - assert_eq!(xwayland_command(None, Some("/opt/p.AppImage"), false), None); - } - - /// Tests every native save dialog and records the suggested file name. Each name's file type must come first - /// in the save panel, or the panel appends the first type's extension (`photo.webp.psd`, `photo.gif.psd`). - #[test] - fn every_save_dialog_leads_with_its_own_extension() { - let asked: Rc>> = Rc::default(); - let log = asked.clone(); - // Record each save dialog's suggested name and cancel it, as the user would. - let services = Services { - file_dialog: Some(Box::new(move |request, _parent, reply| { - if let FileDialogRequest::Save { suggested } = request { - log.borrow_mut().push(suggested); - } - reply.send(None); - })), - ..Default::default() - }; - let mut app = PhotocraftApp::new(Session::new(), services); - let ctx = egui::Context::default(); - // Dialogs are shown on the next frame: poll once so each is answered before the next asks. - let invoke = |app: &mut PhotocraftApp, id: &str| { - let r = photocraft_ui_egui::menus::invoke(app, &ctx, id, json!({})); - app.poll_file_dialog(&ctx, None); - r - }; - let dialog = |app: &mut PhotocraftApp, id: &str, fields: Value| { - let d = invoke(app, id).unwrap()["dialog"].as_u64().unwrap(); - for (k, v) in fields.as_object().unwrap() { - app.ui.dialog_mut(d).unwrap().fields.insert(k.clone(), v.clone()); - } - let _ = photocraft_ui_egui::dialogs::confirm(app, d); - app.poll_file_dialog(&ctx, None); - }; - new_doc(&mut app, "#ff0000"); - // Save As, Save a Copy. - // Files in a format Save As can't write, like .dng, should suggest .psd instead. - let _ = invoke(&mut app, "file.saveAs"); - for name in ["cat.pcraft", "cat.jpeg", "cat.gif", "cat.bmp", "cat.dng"] { - app.session.active_mut().unwrap().path = Some(name.into()); - let _ = invoke(&mut app, "file.saveAs"); - } - let _ = invoke(&mut app, "file.saveACopy"); - // Export As, Quick Export, Save for Web (with and without slices). - for format in ["png", "jpg", "webp", "tif", "tga"] { - dialog(&mut app, "file.export.exportAs", json!({"format": format})); - } - for format in ["png", "jpg", "gif", "webp"] { - app.run("prefs.set", json!({"path": "export.quickExportFormat", "value": format})).unwrap(); - let _ = invoke(&mut app, "file.export.quickExportAsPng"); - } - for format in ["gif", "png8", "png24", "jpeg", "wbmp"] { - dialog(&mut app, "file.export.saveForWebLegacy", json!({"format": format})); - } - app.run("slice.new", json!({"x": 0, "y": 0, "width": 2, "height": 2})).unwrap(); - dialog(&mut app, "file.export.saveForWebLegacy", json!({"html": true})); - // Measurement Log, Export/Import Presets. - app.run("image.analysis.recordMeasurements", json!({})).unwrap(); - let _ = invoke(&mut app, "measurementLog.export"); - dialog(&mut app, "edit.presets.exportImportPresets", json!({"action": "export"})); - let asked = asked.borrow(); - let exts: Vec<&str> = asked.iter().filter_map(|s| s.rsplit_once('.').map(|(_, e)| e)).collect(); - // The extension each save above should suggest, in order. - let want = [ - "psd", // Save As untitled - "pcraft jpeg gif bmp psd", // Save As opened .pcraft .jpeg .gif .bmp .dng - "psd", // Save a Copy - "png jpg webp tif tga", // Export As - "png jpg gif webp", // Quick Export - "gif png png jpg wbmp html", // Save for Web: gif png8 png24 jpeg wbmp, then with slices - "csv pcpresets", // Measurement Log, Export Presets - ] - .join(" "); - assert_eq!(exts, want.split(' ').collect::>()); - for (name, ext) in asked.iter().zip(exts) { - // The first type's first extension is the one Windows adds to the bare name. - assert_eq!(save_filters(name)[0].1.first().map(String::as_str), Some(ext), "{name}: {:?}", save_filters(name)[0]); - } - assert_eq!(save_filters("Untitled")[0].0, "PSD Document", "no extension keeps the default"); - } - - /// Every flat format Save As offers is one PhotoCraft writes, and ICO is among them (#4357). - #[test] - fn save_as_offers_only_writable_formats_including_ico() { - // Layered formats, and PDF, are written by `photocraft-io`, not a codec. - let layered = ["psd", "psb", "pcraft", "ora", "pdf"]; - for (name, exts) in SAVE_FILTERS { - for ext in exts.iter().filter(|e| !layered.contains(e)) { - let format = photocraft_codecs::from_extension(ext).unwrap_or_else(|| panic!("{name}: .{ext} is no known format")); - assert!(format.caps().write, "{name}: .{ext} can't be written"); - } - } - for ext in ["ico", "gif", "bmp"] { - assert!(save_filters("Untitled-1.psd").iter().any(|(_, exts)| exts.iter().any(|e| e == ext)), ".{ext} is offered"); - } - assert_eq!(save_filters("icon.ico")[0].0, "ICO", "an .ico name leads with its own type"); - // What Save As writes there: an icon up to 256×256, and a clear error past that. - let doc = |side: u32| { - let mut s = Session::new(); - s.execute("file.new", json!({"width": side, "height": side})).unwrap(); - s.active().unwrap().doc.clone() - }; - let opts = export_options(&photocraft_ui_egui::ExportSettings::default()); - let icon = photocraft_io::export(&doc(256), "icon.ico", &opts).unwrap(); - assert_eq!(icon.bytes[..4], [0, 0, 1, 0], "an ICO header"); - let err = photocraft_io::export(&doc(300), "icon.ico", &opts).map(|_| ()).unwrap_err().to_string(); - assert!(err.contains("256"), "{err}"); - } - - /// #4394: with File name extensions shown, Windows kept `image.psd` when the type was changed to - /// PhotoCraft. Given the bare `image`, it adds the extension of whichever type is picked - /// (checked by hand with rfd 0.17.2 on Windows 11), defaulting to the first type's. - #[test] - fn the_windows_save_dialog_gets_a_bare_name_so_the_type_picks_the_extension() { - for (suggested, name, first) in [ - ("/pics/image.psd", "image", "psd"), - ("/pics/image.psb", "image", "psb"), - ("/pics/image.pcraft", "image", "pcraft"), - ("/pics/sky.fit", "sky", "fit"), - ("cat.jpeg", "cat", "jpeg"), - ("swatches.aco", "swatches", "aco"), - ] { - assert_eq!(dialog_file_name(suggested, true).as_deref(), Some(name), "{suggested}"); - assert_eq!(save_filters(suggested)[0].1[0], first, "{suggested}"); - // Elsewhere the dialogs keep the whole name (see `dialog_file_name`). - let whole = Path::new(suggested).file_name().unwrap().to_string_lossy().into_owned(); - assert_eq!(dialog_file_name(suggested, false), Some(whole), "{suggested}"); - } - // Kept whole: a second dot (Windows could keep `scan.jpg`), the user's upper case, no - // extension, and a dotfile. - for name in ["scan.jpg.psd", "my.photo.psd", "transparent.PNG", "Untitled", ".psd"] { - assert_eq!(dialog_file_name(name, true).as_deref(), Some(name)); - } - assert_eq!(dialog_file_name("", true), None); - } - - #[test] - fn block_on_waits_for_a_wake_from_another_thread() { - let (tx, rx) = std::sync::mpsc::channel::(); - let mut woken = false; - let ready = std::future::poll_fn(move |cx| { - if woken { - return std::task::Poll::Ready(7); - } - woken = true; - let _ = tx.send(cx.waker().clone()); - std::task::Poll::Pending - }); - let waker = std::thread::spawn(move || rx.recv().unwrap().wake()); - assert_eq!(block_on(ready), 7); - waker.join().unwrap(); - } - - const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; - const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; - const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; - - /// One app launch with crash recovery in `dir` (dropping it is the crash: background writes - /// already queued finish, nothing else runs). - fn launch(dir: &Path) -> PhotocraftApp { - let mut app = PhotocraftApp::new(Session::new(), recovery_services(Some(dir.to_path_buf()))); - let ctx = egui::Context::default(); - prefs_ui::tick(&mut app, &ctx); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); - while app.session.has_jobs() && std::time::Instant::now() < deadline { - photocraft_ui_egui::jobs_ui::tick(&mut app, &ctx); - std::thread::sleep(std::time::Duration::from_millis(1)); - } - assert!(!app.session.has_jobs(), "recovery finished"); - app - } - - /// The first pixel of each open document, and whether it's unsaved. - fn open_docs(app: &PhotocraftApp) -> Vec<([f32; 4], bool)> { - app.session.documents().iter().map(|d| (photocraft_compose::flatten(&d.doc).px.first().copied().unwrap_or_default(), d.is_dirty())).collect() - } - - fn index_of(app: &PhotocraftApp, color: [f32; 4]) -> usize { - open_docs(app).iter().position(|(c, _)| *c == color).unwrap() - } - - fn new_doc(app: &mut PhotocraftApp, color: &str) { - app.run("file.new", json!({"width": 4, "height": 4})).unwrap(); - app.run("edit.fill", json!({"color": color})).unwrap(); - } - - fn autosave(app: &mut PhotocraftApp, ctx: &egui::Context) { - prefs_ui::autosave_now(app); - prefs_ui::tick(app, ctx); - } - - #[test] - fn recovered_documents_survive_a_second_crash_until_saved_or_closed() { - let dir = std::env::temp_dir().join(format!("photocraft-recovery-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - let ctx = egui::Context::default(); - // Launch 1: two unsaved documents are autosaved, then the app crashes. - { - let mut app = launch(&dir); - new_doc(&mut app, "#ff0000"); - new_doc(&mut app, "#0000ff"); - autosave(&mut app, &ctx); - } - assert_eq!(list_recovery(&dir).len(), 2); - // Launch 2 recovers both and crashes again before its first autosave (#444). - { - let mut app = launch(&dir); - let mut docs = open_docs(&app); - docs.sort_by(|a, b| a.0[0].total_cmp(&b.0[0])); - assert_eq!(docs, [(BLUE, true), (RED, true)], "recovered and still unsaved"); - prefs_ui::tick(&mut app, &ctx); - } - assert_eq!(list_recovery(&dir).len(), 2, "recovering keeps the entries on disk"); - // Launch 3 has both again; autosaving unchanged documents adds nothing, a new document - // (whose id may repeat one from an earlier launch) gets an entry of its own. - { - let mut app = launch(&dir); - assert_eq!(app.session.documents().len(), 2); - autosave(&mut app, &ctx); - new_doc(&mut app, "#00ff00"); - autosave(&mut app, &ctx); - } - assert_eq!(list_recovery(&dir).len(), 3); - // Launch 4: closing and saving recovered documents removes their entries, no duplicates. - let mut app = launch(&dir); - assert_eq!(app.session.documents().len(), 3); - let red = index_of(&app, RED); - app.run("file.close", json!({"document": red})).unwrap(); - // Saving marks the revision saved (the file write itself is the save service's job). - assert!(app.session.set_active(index_of(&app, BLUE))); - let st = app.session.active_mut().unwrap(); - st.saved_revision = st.revision; - prefs_ui::tick(&mut app, &ctx); - let left = list_recovery(&dir); - assert_eq!(left.len(), 1); - assert_eq!(photocraft_compose::flatten(&photocraft_format::recover(&left[0]).unwrap()).px.first().copied(), Some(GREEN)); - let green = index_of(&app, GREEN); - app.run("file.close", json!({"document": green})).unwrap(); - prefs_ui::tick(&mut app, &ctx); - assert!(list_recovery(&dir).is_empty()); - drop(app); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn failed_background_autosave_is_reported_and_retried_without_another_edit() { - let dir = temp("autosave-retry"); - let recovery = dir.join("Recovery"); - // A regular file prevents creation of the recovery bundle directory. - std::fs::write(&recovery, b"blocked").unwrap(); - let ctx = egui::Context::default(); - let mut app = launch(&recovery); - new_doc(&mut app, "#ff0000"); - let revision = app.session.active().unwrap().revision; - assert_ne!(revision, 0, "a sidecar-less bundle lists as revision 0"); - autosave(&mut app, &ctx); - - let mut failed = false; - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); - while std::time::Instant::now() < deadline { - prefs_ui::tick(&mut app, &ctx); - if app.ui.status.starts_with("Autosave failed:") { - failed = true; - break; - } - std::thread::sleep(std::time::Duration::from_millis(10)); - } - assert!(failed, "background write failures must reach the UI"); - assert!(app.ui.status_error); - assert!(list_recovery(&recovery).is_empty()); - - // No edit occurs: retrying this very same revision must still work. - std::fs::remove_file(&recovery).unwrap(); - std::fs::create_dir(&recovery).unwrap(); - autosave(&mut app, &ctx); - // The worker commits the bundle's manifest before it publishes the sidecar, and - // `list_recovery` lists a committed sidecar-less bundle (revision 0) on its own, so - // the first non-empty listing can come from the middle of the write. Wait for the write - // to finish: the entry then carries the sidecar's revision. - let mut recovered = Vec::new(); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); - while std::time::Instant::now() < deadline { - prefs_ui::tick(&mut app, &ctx); - recovered = list_recovery(&recovery); - if recovered.iter().any(|e| e.info.revision == revision) { - break; - } - std::thread::sleep(std::time::Duration::from_millis(10)); - } - assert_eq!(recovered.len(), 1, "unchanged revision should retry after failure"); - assert_eq!(recovered[0].info.revision, revision); - let restored = photocraft_format::recover(&recovered[0]).unwrap(); - assert_eq!(photocraft_compose::flatten(&restored).px.first().copied(), Some(RED)); - drop(app); - std::fs::remove_dir_all(dir).unwrap(); - } - - use std::sync::atomic::{AtomicUsize, Ordering}; - - fn temp(tag: &str) -> PathBuf { - static N: AtomicUsize = AtomicUsize::new(0); - let d = std::env::temp_dir().join(format!("photocraft-services-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed))); - let _ = std::fs::remove_dir_all(&d); - std::fs::create_dir_all(&d).unwrap(); - d - } - - fn red_png(w: u32, h: u32) -> Vec { - let img = Image::from_u8(w, h, ChannelLayout::Rgba, [255u8, 0, 0, 255].repeat((w * h) as usize)).unwrap(); - photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &EncodeOptions::default()).unwrap() - } - - /// A 1×1 uncompressed 32-bit TGA (TGA has no magic number). - fn tga_1x1() -> Vec { - let mut b = vec![0u8, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 32, 8]; - b.extend_from_slice(&[0, 0, 255, 255]); // BGRA red - b - } - - #[test] - fn copied_image_file_pastes_its_pixels() { - let dir = temp("png"); - let png = dir.join("red.png"); - std::fs::write(&png, red_png(3, 2)).unwrap(); - let (w, h, px) = image_from_files(&[png]).unwrap(); - assert_eq!((w, h), (3, 2)); - assert_eq!(px, [255u8, 0, 0, 255].repeat(6)); - } - - #[test] - fn crlf_from_a_uri_list_is_ignored() { - // arboard splits text/uri-list on LF, so GNOME Files' CRLF list leaves a '\r' behind. - let dir = temp("crlf"); - let png = dir.join("red image.png"); - std::fs::write(&png, red_png(2, 2)).unwrap(); - let with_cr = PathBuf::from(format!("{}\r", png.display())); - assert_eq!(image_from_files(&[with_cr]).map(|(w, h, _)| (w, h)), Some((2, 2))); - } - - #[test] - fn non_images_are_skipped_for_the_first_image() { - let dir = temp("mixed"); - let (txt, png) = (dir.join("notes.txt"), dir.join("red.png")); - std::fs::write(&txt, b"not an image").unwrap(); - std::fs::write(&png, red_png(4, 1)).unwrap(); - let missing = dir.join("gone.png"); - assert_eq!(image_from_files(&[missing.clone(), txt.clone(), dir.clone(), png]).map(|(w, h, _)| (w, h)), Some((4, 1))); - assert!(image_from_files(&[missing, txt, dir]).is_none()); - assert!(image_from_files(&[]).is_none()); - } - - #[test] - fn a_damaged_image_is_skipped_not_a_panic() { - let dir = temp("damaged"); - let bad = dir.join("cut.png"); - std::fs::write(&bad, &red_png(8, 8)[..20]).unwrap(); - assert!(image_from_files(&[bad]).is_none()); - } - - fn encoded(format: photocraft_codecs::Format, w: u32, h: u32) -> Vec { - let img = Image::from_u8(w, h, ChannelLayout::Rgba, [255u8, 0, 0, 255].repeat((w * h) as usize)).unwrap(); - photocraft_codecs::encode(&img, format, &EncodeOptions::default()).unwrap() - } - - #[test] - fn non_png_clipboard_images_paste() { - // #2546: a screenshot tool that offers only image/bmp or image/jpeg pasted nothing. - let bmp = encoded(photocraft_codecs::Format::Bmp, 3, 2); - let offers = vec!["text/plain".to_string(), "image/bmp".to_string()]; - let mut asked = Vec::new(); - let got = image_from_offers(&offers, |mime| { - asked.push(mime.to_string()); - Some(bmp.clone()) - }); - assert_eq!(got, Some((3, 2, [255u8, 0, 0, 255].repeat(6)))); - assert_eq!(asked, ["image/bmp"], "text offers are never fetched"); - - let jpeg = encoded(photocraft_codecs::Format::Jpeg, 16, 8); - let offers = vec!["image/jpeg".to_string()]; - assert_eq!(image_from_offers(&offers, |_| Some(jpeg.clone())).map(|(w, h, _)| (w, h)), Some((16, 8))); - } - - #[test] - fn undecodable_clipboard_offers_fall_through_to_the_next() { - let bmp = encoded(photocraft_codecs::Format::Bmp, 2, 2); - let cut = encoded(photocraft_codecs::Format::Png, 8, 8)[..20].to_vec(); - let offers: Vec = ["image/webp", "image/png", "image/x-tga", "image/bmp"].map(String::from).to_vec(); - let got = image_from_offers(&offers, |mime| match mime { - "image/webp" => None, - "image/png" => Some(cut.clone()), - "image/x-tga" => Some(b"not an image".to_vec()), - _ => Some(bmp.clone()), - }); - assert_eq!(got.map(|(w, h, _)| (w, h)), Some((2, 2))); - assert!(image_from_offers(&offers[..3], |_| Some(cut.clone())).is_none()); - assert!(image_from_offers(&[], |_| Some(bmp.clone())).is_none()); - } - - #[test] - fn clipboard_tga_is_trusted_only_by_its_type() { - assert_eq!(image_from_offers(&["image/x-tga".to_string()], |_| Some(tga_1x1())), Some((1, 1, vec![255, 0, 0, 255]))); - assert!(image_from_offers(&["image/x-unknown".to_string()], |_| Some(tga_1x1())).is_none()); - } - - #[test] - fn tga_is_trusted_only_with_its_extension() { - let dir = temp("tga"); - let (tga, bin) = (dir.join("red.tga"), dir.join("red.bin")); - std::fs::write(&tga, tga_1x1()).unwrap(); - std::fs::write(&bin, tga_1x1()).unwrap(); - assert_eq!(image_from_files(&[tga]), Some((1, 1, vec![255, 0, 0, 255]))); - assert!(image_from_files(&[bin]).is_none()); - } -} +mod tests; diff --git a/apps/photocraft/src/services/recovery_tests.rs b/apps/photocraft/src/services/recovery_tests.rs index b1bd2d51c0..23191ed7f9 100644 --- a/apps/photocraft/src/services/recovery_tests.rs +++ b/apps/photocraft/src/services/recovery_tests.rs @@ -1,5 +1,6 @@ use super::*; -use photocraft_color::Color; +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::Size; use photocraft_format::{Autosaver, RecoveryEntry, SaveOptions, list_recovery}; use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/apps/photocraft/src/services/tests.rs b/apps/photocraft/src/services/tests.rs new file mode 100644 index 0000000000..8f12fee908 --- /dev/null +++ b/apps/photocraft/src/services/tests.rs @@ -0,0 +1,888 @@ +use super::*; +use photocraft_engine::Session; +use photocraft_format::list_recovery; +use photocraft_ui_egui::{PhotocraftApp, prefs_ui}; +use serde_json::{Value, json}; + +/// #6601: a chosen path whose name isn't valid Unicode is reported, never handed on lossily +/// (Open would fail with "No such file", Save would write a second, renamed file). +#[test] +fn a_chosen_path_that_is_not_valid_unicode_is_an_error_not_another_name() { + #[cfg(unix)] + let bad = { + use std::os::unix::ffi::OsStrExt; + std::env::temp_dir().join(std::ffi::OsStr::from_bytes(b"img\xFF.png")) + }; + #[cfg(windows)] + let bad = { + use std::os::windows::ffi::OsStringExt; + let mut wide: Vec = "img".encode_utf16().collect(); + wide.push(0xD800); + wide.extend(".png".encode_utf16()); + std::env::temp_dir().join(std::ffi::OsString::from_wide(&wide)) + }; + let good = std::env::temp_dir().join("ok.png"); + let save = |path: &Path| answer_one(path, true, FileDialogAnswer::SaveTo); + assert!(matches!(save(&bad), FileDialogAnswer::Unavailable(e) if e.contains("not valid Unicode") && e.contains("another name"))); + assert!(matches!(save(&good), FileDialogAnswer::SaveTo(p) if p == good.to_str().unwrap())); + let open = answer([good.as_path(), bad.as_path()], false, FileDialogAnswer::Paths); + assert!(matches!(open, FileDialogAnswer::Unavailable(e) if e.contains("open it again"))); +} + +/// #6121: the file type picked in the save dialog gives the name its extension where the +/// dialog left the one it opened with, and only there. +#[test] +fn the_picked_file_type_gives_the_name_its_extension() { + let exts = |e: &[&str]| e.iter().map(|s| s.to_string()).collect::>(); + let (png, psd) = (exts(&["png"]), exts(&["psd", "psb"])); + let opened = Some("Untitled-1.psd"); + let cases = [ + // PNG picked, the name as the dialog opened (or renamed, its extension kept). + ("/pics/Untitled-1.psd", opened, &png, "/pics/Untitled-1.png"), + ("/pics/art.psd", opened, &png, "/pics/art.png"), + ("/pics/ART.PSD", opened, &png, "/pics/ART.png"), + // No extension: the type's. One PhotoCraft doesn't save is part of the name. + ("/pics/art", opened, &png, "/pics/art.png"), + ("/pics/scan.2024", opened, &png, "/pics/scan.2024.png"), + // Typed, an extension PhotoCraft saves is the user's: kept, whatever the type says + // (the PSD type untouched, or PNG picked and .jpg typed). + ("/pics/art.png", opened, &psd, "/pics/art.png"), + ("/pics/art.jpg", opened, &png, "/pics/art.jpg"), + ("/pics/art.pcraft", opened, &png, "/pics/art.pcraft"), + // Already the type's: kept, including its other extensions. + ("/pics/art.png", opened, &png, "/pics/art.png"), + ("/pics/art.PNG", opened, &png, "/pics/art.PNG"), + ("/pics/big.psb", opened, &psd, "/pics/big.psb"), + // The dialog opened on a .pcraft: that is the extension the type replaces. + ("/pics/art.pcraft", Some("art.pcraft"), &png, "/pics/art.png"), + ("/pics/art.psd", Some("art.pcraft"), &png, "/pics/art.psd"), + ]; + for (typed, opened, chosen, want) in cases { + assert_eq!(with_chosen_type(Path::new(typed), opened, chosen), PathBuf::from(want), "{typed} (opened {opened:?})"); + } + // No type picked: the name as typed. + assert_eq!(with_chosen_type(Path::new("/pics/art.psd"), opened, &[]), PathBuf::from("/pics/art.psd")); +} + +/// Regression using the real clipboard on an isolated Windows CI runner. +#[cfg(target_os = "windows")] +#[test] +#[ignore = "requires exclusive use of the Windows clipboard"] +fn busy_native_clipboard_preserves_copied_shape() { + for id in ["edit.paste", "edit.pasteSpecial.pasteInPlace"] { + let mut native = native(None); + let services = + Services { clipboard_set_image: native.clipboard_set_image.take(), clipboard_get_image: native.clipboard_get_image.take(), ..Default::default() }; + let mut app = PhotocraftApp::new(Session::new(), services); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [10, 10, 40, 20], "fill": "#ff0000"})).unwrap(); + app.run("edit.copy", json!({})).unwrap(); + assert!(app.session.clipboard.as_ref().unwrap().layers.is_some()); + assert!(app.services.clipboard_get_image.as_mut().unwrap()().unwrap().is_some(), "native export succeeded"); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + + // Another thread holds the OS clipboard open beyond arboard's bounded retry window. + let (ready_tx, ready_rx) = std::sync::mpsc::channel(); + let (release_tx, release_rx) = std::sync::mpsc::channel(); + let holder = std::thread::spawn(move || { + // A non-null window distinguishes this owner from arboard's null-window opens. + let guard = winsafe::HWND::GetDesktopWindow().OpenClipboard().unwrap(); + ready_tx.send(()).unwrap(); + release_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap(); + drop(guard); + }); + ready_rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap(); + let contention = arboard::Clipboard::new().unwrap().get_image().err(); + let first = app.run(id, json!({})); + release_tx.send(()).unwrap(); + holder.join().unwrap(); + assert!(matches!(contention, Some(arboard::Error::ClipboardOccupied)), "diagnostic must actually occupy the clipboard: {contention:?}"); + eprintln!("paste while clipboard occupied: {first:?}"); + assert!(first.is_err(), "report contention rather than a successful no-op"); + assert!( + app.session.clipboard.as_ref().is_some_and(|clip| clip.layers.is_some()), + "a temporary OS read failure must not discard the copied editable shape" + ); + app.run(id, json!({})).unwrap(); + let state = app.session.active().unwrap(); + assert!( + matches!(state.doc.layer(state.active_layer.unwrap()).unwrap().content, photocraft_doc::LayerContent::Shape(_)), + "retry keeps the shape editable" + ); + } +} + +#[cfg(all(unix, not(target_os = "macos")))] +#[test] +fn open_filters_cover_uppercase_and_mixed_case_extensions() { + // Matches `text` against a glob of literals and `[..]` classes, as the portal would after `*.`. + fn matches(glob: &str, text: &str) -> bool { + let mut text = text.chars(); + let mut glob = glob.chars(); + while let Some(g) = glob.next() { + let class: String = if g == '[' { glob.by_ref().take_while(|&c| c != ']').collect() } else { g.to_string() }; + if !text.next().is_some_and(|t| class.contains(t)) { + return false; + } + } + text.next().is_none() + } + assert_eq!(open_filter_extensions(&["cr2", "pcraft"]), ["[cC][rR]2", "[pP][cC][rR][aA][fF][tT]"]); + let globs = open_filter_extensions(OPEN_EXTS); + assert_eq!(globs.len(), OPEN_EXTS.len()); + for (ext, glob) in OPEN_EXTS.iter().zip(&globs) { + // Every casing: lower, UPPER, and alternating both ways (JpG, jPg). + let alternating = |upper_first: bool| -> String { + ext.chars().enumerate().map(|(i, c)| if (i % 2 == 0) == upper_first { c.to_ascii_uppercase() } else { c }).collect() + }; + for name in [ext.to_string(), ext.to_ascii_uppercase(), alternating(true), alternating(false)] { + assert!(matches(glob, &name), "{glob} should match .{name}"); + } + assert!(!matches(glob, &format!("{ext}x")) && !matches(glob, &ext[..ext.len() - 1]), "{glob} matches only .{ext}"); + } + assert!(!globs.iter().any(|glob| matches(glob, "txt"))); + let svg_filter = open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts).unwrap_or_default(); + assert_eq!(svg_filter, open_filter_extensions(SVG_EXTS)); + let psd_filter = open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts).unwrap_or_default(); + assert_eq!(psd_filter, open_filter_extensions(&["psd", "psb", "psdt"])); + assert!(psd_filter.iter().any(|glob| matches(glob, "psd")) && psd_filter.iter().any(|glob| matches(glob, "PSD"))); + assert!(svg_filter.iter().any(|glob| matches(glob, "svg")) && svg_filter.iter().any(|glob| matches(glob, "svgz"))); +} + +/// #6337: after All Formats, every format File › Open reads has its own filter, and no filter +/// offers an extension All Formats doesn't. +#[test] +fn every_open_format_has_its_own_filter() { + let filters = open_filters(); + assert_eq!(filters[0], ("All Formats", open_filter_extensions(OPEN_EXTS)), "All Formats comes first"); + let mut named: Vec<&str> = OPEN_FILTERS.iter().flat_map(|(_, exts)| exts.iter().copied()).collect(); + let total = named.len(); + named.sort_unstable(); + named.dedup(); + assert_eq!(named.len(), total, "an extension is listed under two filters"); + let mut all = OPEN_EXTS.to_vec(); + all.sort_unstable(); + assert_eq!(named, all, "the named filters cover All Formats exactly"); + let names: Vec<&str> = filters.iter().map(|(name, _)| *name).collect(); + for name in ["Photoshop", "Affinity", "HEIF", "PNG", "JPEG", "TIFF", "Portable Bitmap", "OpenEXR", "Radiance HDR", "Paint.NET"] { + assert!(names.contains(&name), "{name}"); + } +} + +#[cfg(not(all(unix, not(target_os = "macos"))))] +#[test] +fn open_filters_keep_literal_extensions_on_windows_and_macos() { + assert_eq!(open_filter_extensions(OPEN_EXTS), OPEN_EXTS); + assert_eq!(open_filter_extensions(&["pcraft"]), ["pcraft"]); + assert_eq!(open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts), Some(vec!["svg".to_string(), "svgz".to_string()])); + assert_eq!( + open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts), + Some(vec!["psd".to_string(), "psb".to_string(), "psdt".to_string()]) + ); +} + +#[test] +fn native_services_provide_update_transport() { + assert!(native(None).check_updates.is_some()); +} + +#[test] +fn xwayland_command_matches_how_photocraft_was_installed() { + // Flatpak: WAYLAND_DISPLAY doesn't reach the sandbox's socket choice; flatpak's flags do. + let flatpak = xwayland_command(Some("ai.storyteller.photocraft"), None, false); + assert_eq!(flatpak.as_deref(), Some("flatpak run --nosocket=wayland --socket=x11 ai.storyteller.photocraft")); + // AppImage: its own path, quoted for the shell (spaces, quotes). + let appimage = xwayland_command(None, Some("/home/me/My Apps/it's.AppImage"), true); + assert_eq!(appimage.as_deref(), Some(r"WAYLAND_DISPLAY= '/home/me/My Apps/it'\''s.AppImage'")); + let bare = xwayland_command(None, Some("/opt/photocraft-0.3.0-linux-x86_64.AppImage"), true); + assert_eq!(bare.as_deref(), Some("WAYLAND_DISPLAY= /opt/photocraft-0.3.0-linux-x86_64.AppImage")); + // deb, rpm, AUR, tarball: the binary on the PATH. + assert_eq!(xwayland_command(None, None, true).as_deref(), Some("WAYLAND_DISPLAY= photocraft")); + assert_eq!(xwayland_command(Some(""), Some(""), true).as_deref(), Some("WAYLAND_DISPLAY= photocraft")); + // No X server (XWayland disabled): nothing to suggest. + assert_eq!(xwayland_command(None, Some("/opt/p.AppImage"), false), None); +} + +/// Tests every native save dialog and records the suggested file name. Each name's file type must come first +/// in the save panel, or the panel appends the first type's extension (`photo.webp.psd`, `photo.gif.psd`). +#[test] +fn every_save_dialog_leads_with_its_own_extension() { + let asked: Rc>> = Rc::default(); + let log = asked.clone(); + // Record each save dialog's suggested name and cancel it, as the user would. + let services = Services { + file_dialog: Some(Box::new(move |request, _parent, reply| { + if let FileDialogRequest::Save { suggested } = request { + log.borrow_mut().push(suggested); + } + reply.send(None); + })), + ..Default::default() + }; + let mut app = PhotocraftApp::new(Session::new(), services); + let ctx = egui::Context::default(); + // Dialogs are shown on the next frame: poll once so each is answered before the next asks. + let invoke = |app: &mut PhotocraftApp, id: &str| { + let r = photocraft_ui_egui::menus::invoke(app, &ctx, id, json!({})); + app.poll_file_dialog(&ctx, None); + r + }; + let dialog = |app: &mut PhotocraftApp, id: &str, fields: Value| { + let d = invoke(app, id).unwrap()["dialog"].as_u64().unwrap(); + for (k, v) in fields.as_object().unwrap() { + app.ui.dialog_mut(d).unwrap().fields.insert(k.clone(), v.clone()); + } + let _ = photocraft_ui_egui::dialogs::confirm(app, d); + app.poll_file_dialog(&ctx, None); + }; + new_doc(&mut app, "#ff0000"); + // Save As, Save a Copy. + // Files in a format Save As can't write, like .dng, should suggest .psd instead. + let _ = invoke(&mut app, "file.saveAs"); + for name in ["cat.pcraft", "cat.jpeg", "cat.gif", "cat.bmp", "cat.dng"] { + app.session.active_mut().unwrap().path = Some(name.into()); + let _ = invoke(&mut app, "file.saveAs"); + } + let _ = invoke(&mut app, "file.saveACopy"); + // Export As, Quick Export, Save for Web (with and without slices). + for format in ["png", "jpg", "webp", "tif", "tga"] { + dialog(&mut app, "file.export.exportAs", json!({"format": format})); + } + for format in ["png", "jpg", "gif", "webp"] { + app.run("prefs.set", json!({"path": "export.quickExportFormat", "value": format})).unwrap(); + let _ = invoke(&mut app, "file.export.quickExportAsPng"); + } + for format in ["gif", "png8", "png24", "jpeg", "webp", "wbmp"] { + dialog(&mut app, "file.export.saveForWebLegacy", json!({"format": format})); + } + app.run("slice.new", json!({"x": 0, "y": 0, "width": 2, "height": 2})).unwrap(); + dialog(&mut app, "file.export.saveForWebLegacy", json!({"html": true})); + // Measurement Log, Export/Import Presets. + app.run("image.analysis.recordMeasurements", json!({})).unwrap(); + let _ = invoke(&mut app, "measurementLog.export"); + dialog(&mut app, "edit.presets.exportImportPresets", json!({"action": "export"})); + let asked = asked.borrow(); + let exts: Vec<&str> = asked.iter().filter_map(|s| s.rsplit_once('.').map(|(_, e)| e)).collect(); + // The extension each save above should suggest, in order. + let want = [ + "psd", // Save As untitled + "pcraft jpeg gif bmp psd", // Save As opened .pcraft .jpeg .gif .bmp .dng + "psd", // Save a Copy + "png jpg webp tif tga", // Export As + "png jpg gif webp", // Quick Export + "gif png png jpg webp wbmp html", // Save for Web: gif png8 png24 jpeg webp wbmp, then with slices + "csv pcpresets", // Measurement Log, Export Presets + ] + .join(" "); + assert_eq!(exts, want.split(' ').collect::>()); + for (name, ext) in asked.iter().zip(exts) { + // The first type's first extension is the one Windows adds to the bare name. + assert_eq!(save_filters(name)[0].1.first().map(String::as_str), Some(ext), "{name}: {:?}", save_filters(name)[0]); + } + assert_eq!(save_filters("Untitled")[0].0, "PSD Document", "no extension keeps the default"); +} + +/// Every flat format Save As offers is one PhotoCraft writes, and ICO is among them (#4357). +#[test] +fn save_as_offers_only_writable_formats_including_ico() { + // Layered formats, and PDF, are written by `photocraft-io`, not a codec. + let layered = ["psd", "psb", "pcraft", "ora", "pdf"]; + for (name, exts) in SAVE_FILTERS { + for ext in exts.iter().filter(|e| !layered.contains(e)) { + let format = photocraft_codecs::from_extension(ext).unwrap_or_else(|| panic!("{name}: .{ext} is no known format")); + assert!(format.caps().write, "{name}: .{ext} can't be written"); + } + } + for ext in ["ico", "gif", "bmp", "dds", "pnm", "hdr", "dib", "jpeg", "tiff"] { + assert!(save_filters("Untitled-1.psd").iter().any(|(_, exts)| exts.iter().any(|e| e == ext)), ".{ext} is offered"); + } + // The codec writes P5/P6/P7/PF from the image, never from the name, so Netpbm offers only the + // neutral .pnm (#3609; CodeRabbit on #9775): a .pbm, .pgm or .pfm name would hold other data. + let netpbm = save_filters("Untitled").into_iter().find(|(name, _)| name == "Netpbm").map(|(_, exts)| exts); + assert_eq!(netpbm, Some(vec!["pnm".to_string()])); + // The suggested name itself becomes .pnm, as macOS and rfd keep it (CodeRabbit on #9775). + assert_eq!(netpbm_as_pnm("scans/image.pgm"), Path::new("scans/image.pnm").to_string_lossy()); + assert_eq!(netpbm_as_pnm("image.PFM"), "image.pnm"); + assert_eq!(netpbm_as_pnm("image.png"), "image.png"); + assert_eq!(netpbm_as_pnm("Untitled-1"), "Untitled-1"); + // Also when Save As starts from a subtype's name: no filter offers that subtype again. + for name in ["image.pbm", "image.pgm", "image.ppm", "image.pam", "image.pfm"] { + let filters = save_filters(name); + assert_eq!(filters[0], ("Netpbm".to_string(), vec!["pnm".to_string()]), "{name}"); + let subtype = name.rsplit('.').next().unwrap_or_default(); + assert!(!filters.iter().any(|(_, exts)| exts.iter().any(|e| e == subtype)), "{name}: .{subtype} is offered"); + } + // File Open lists readable Save As formats in their own type. Export-only formats must + // stay absent from both Open filters rather than advertising an unavailable decoder. + for (name, exts) in SAVE_FILTERS { + for ext in exts.iter().filter(|e| **e != "pdf") { + if photocraft_codecs::from_extension(ext).is_some_and(|format| !format.caps().read) { + assert!(!OPEN_EXTS.contains(ext), "{name}: export-only .{ext} must not appear in All Formats"); + assert!(!OPEN_FILTERS.iter().any(|(_, open)| open.contains(ext)), "{name}: export-only .{ext} must not appear in an Open type"); + continue; + } + assert!(OPEN_EXTS.contains(ext), "{name}: Open's All Formats lacks .{ext}"); + assert!(OPEN_FILTERS.iter().any(|(_, open)| open.contains(ext)), "{name}: no Open type lists .{ext}"); + } + } + assert_eq!(save_filters("icon.ico")[0].0, "ICO", "an .ico name leads with its own type"); + // What Save As writes there: an icon up to 256×256, and a clear error past that. + let doc = |side: u32| { + let mut s = Session::new(); + s.execute("file.new", json!({"width": side, "height": side})).unwrap(); + s.active().unwrap().doc.clone() + }; + let opts = export_options(&photocraft_ui_egui::ExportSettings::default()); + let icon = photocraft_io::export(&doc(256), "icon.ico", &opts).unwrap(); + assert_eq!(icon.bytes[..4], [0, 0, 1, 0], "an ICO header"); + // And what the Netpbm and Radiance rows write. + let pnm = photocraft_io::export(&doc(4), "x.pnm", &opts).unwrap(); + assert_eq!(pnm.bytes[..2], *b"P6", "an RGB document saves as a binary pixmap"); + let hdr = photocraft_io::export(&doc(4), "x.hdr", &opts).unwrap(); + assert!(hdr.bytes.starts_with(b"#?RADIANCE"), "a Radiance header"); + let err = photocraft_io::export(&doc(300), "icon.ico", &opts).map(|_| ()).unwrap_err().to_string(); + assert!(err.contains("256"), "{err}"); +} + +/// #4394: with File name extensions shown, Windows kept `image.psd` when the type was changed to +/// PhotoCraft. Given the bare `image`, it adds the extension of whichever type is picked +/// (checked by hand with rfd 0.17.2 on Windows 11), defaulting to the first type's. +#[test] +fn the_windows_save_dialog_gets_a_bare_name_so_the_type_picks_the_extension() { + for (suggested, name, first) in [ + ("/pics/image.psd", "image", "psd"), + ("/pics/image.psb", "image", "psb"), + ("/pics/image.pcraft", "image", "pcraft"), + ("/pics/sky.fit", "sky", "fit"), + ("cat.jpeg", "cat", "jpeg"), + ("swatches.aco", "swatches", "aco"), + ] { + assert_eq!(dialog_file_name(suggested, true).as_deref(), Some(name), "{suggested}"); + assert_eq!(save_filters(suggested)[0].1[0], first, "{suggested}"); + // Elsewhere the dialogs keep the whole name (see `dialog_file_name`). + let whole = Path::new(suggested).file_name().unwrap().to_string_lossy().into_owned(); + assert_eq!(dialog_file_name(suggested, false), Some(whole), "{suggested}"); + } + // Kept whole: a second dot (Windows could keep `scan.jpg`), the user's upper case, no + // extension, and a dotfile. + for name in ["scan.jpg.psd", "my.photo.psd", "transparent.PNG", "Untitled", ".psd"] { + assert_eq!(dialog_file_name(name, true).as_deref(), Some(name)); + } + assert_eq!(dialog_file_name("", true), None); +} + +/// A name in another letter case reaches the same file where the file system ignores case +/// (Windows, and macOS by default), and only there. Two files with the same contents are two. +#[test] +fn same_file_follows_the_file_system() { + let dir = std::env::temp_dir().join(format!("photocraft-same-file-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (cat, dog, upper) = (dir.join("CAT.psd"), dir.join("dog.psd"), dir.join("CAT.PSD")); + std::fs::write(&cat, b"same").unwrap(); + std::fs::write(&dog, b"same").unwrap(); + assert!(same_file(&cat, &cat)); + assert!(!same_file(&cat, &dog)); + assert_eq!(same_file(&upper, &cat), upper.exists(), "the same file only where letter case is ignored"); + assert!(!same_file(&dir.join("missing.psd"), &cat)); + std::fs::remove_dir_all(&dir).unwrap(); +} + +/// A hard link is another entry in the folder, though it reaches the same file: a save to it +/// replaces that entry and leaves the other name on the old file. Another spelling of a name, +/// where letter case is ignored, is the same entry. +#[test] +fn a_hard_link_is_another_entry() { + let dir = std::env::temp_dir().join(format!("photocraft-same-entry-{}", std::process::id())); + std::fs::create_dir_all(dir.join("other")).unwrap(); + let (cat, bare, upper, other) = (dir.join("CAT.psd"), dir.join("CAT"), dir.join("CAT.PSD"), dir.join("other").join("CAT.psd")); + std::fs::write(&cat, b"cat").unwrap(); + std::fs::hard_link(&cat, &bare).unwrap(); + std::fs::hard_link(&cat, &other).unwrap(); + // Where both names reach one file (one inode on Unix), the folder still lists both. + let one_file = |_: &Path, _: &Path| true; + assert!(!same_entry(&bare, &cat, one_file)); + assert!(!same_entry(&bare, &cat, same_file)); + assert!(!same_entry(&other, &cat, same_file), "another folder"); + assert!(same_entry(&cat, &cat, one_file)); + assert!(same_entry(&cat, &cat, same_file)); + if upper.exists() { + assert!(same_entry(&upper, &cat, same_file), "one entry where letter case is ignored"); + } else { + std::fs::hard_link(&cat, &upper).unwrap(); + assert!(!same_entry(&upper, &cat, same_file), "two entries where letter case tells names apart"); + } + assert!(!same_entry(&dir.join("missing.psd"), &cat, same_file)); + std::fs::remove_dir_all(&dir).unwrap(); +} + +#[test] +fn block_on_waits_for_a_wake_from_another_thread() { + let (tx, rx) = std::sync::mpsc::channel::(); + let mut woken = false; + let ready = std::future::poll_fn(move |cx| { + if woken { + return std::task::Poll::Ready(7); + } + woken = true; + let _ = tx.send(cx.waker().clone()); + std::task::Poll::Pending + }); + let waker = std::thread::spawn(move || rx.recv().unwrap().wake()); + assert_eq!(block_on(ready), 7); + waker.join().unwrap(); +} + +const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; +const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; +const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; + +/// One app launch with crash recovery in `dir` (dropping it is the crash: background writes +/// already queued finish, nothing else runs). +fn launch(dir: &Path) -> PhotocraftApp { + let mut app = PhotocraftApp::new(Session::new(), recovery_services(Some(dir.to_path_buf()))); + let ctx = egui::Context::default(); + prefs_ui::tick(&mut app, &ctx); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while app.session.has_jobs() && std::time::Instant::now() < deadline { + photocraft_ui_egui::jobs_ui::tick(&mut app, &ctx); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + assert!(!app.session.has_jobs(), "recovery finished"); + app +} + +/// The first pixel of each open document, and whether it's unsaved. +fn open_docs(app: &PhotocraftApp) -> Vec<([f32; 4], bool)> { + app.session.documents().iter().map(|d| (photocraft_compose::flatten(&d.doc).px.first().copied().unwrap_or_default(), d.is_dirty())).collect() +} + +fn index_of(app: &PhotocraftApp, color: [f32; 4]) -> usize { + open_docs(app).iter().position(|(c, _)| *c == color).unwrap() +} + +fn new_doc(app: &mut PhotocraftApp, color: &str) { + app.run("file.new", json!({"width": 4, "height": 4})).unwrap(); + app.run("edit.fill", json!({"color": color})).unwrap(); +} + +fn autosave(app: &mut PhotocraftApp, ctx: &egui::Context) { + prefs_ui::autosave_now(app); + prefs_ui::tick(app, ctx); +} + +#[test] +fn recovered_documents_survive_a_second_crash_until_saved_or_closed() { + let dir = std::env::temp_dir().join(format!("photocraft-recovery-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let ctx = egui::Context::default(); + // Launch 1: two unsaved documents are autosaved, then the app crashes. + { + let mut app = launch(&dir); + new_doc(&mut app, "#ff0000"); + new_doc(&mut app, "#0000ff"); + autosave(&mut app, &ctx); + } + assert_eq!(list_recovery(&dir).len(), 2); + // Launch 2 recovers both and crashes again before its first autosave (#444). + { + let mut app = launch(&dir); + let mut docs = open_docs(&app); + docs.sort_by(|a, b| a.0[0].total_cmp(&b.0[0])); + assert_eq!(docs, [(BLUE, true), (RED, true)], "recovered and still unsaved"); + prefs_ui::tick(&mut app, &ctx); + } + assert_eq!(list_recovery(&dir).len(), 2, "recovering keeps the entries on disk"); + // Launch 3 has both again; autosaving unchanged documents adds nothing, a new document + // (whose id may repeat one from an earlier launch) gets an entry of its own. + { + let mut app = launch(&dir); + assert_eq!(app.session.documents().len(), 2); + autosave(&mut app, &ctx); + new_doc(&mut app, "#00ff00"); + autosave(&mut app, &ctx); + } + assert_eq!(list_recovery(&dir).len(), 3); + // Launch 4: closing and saving recovered documents removes their entries, no duplicates. + let mut app = launch(&dir); + assert_eq!(app.session.documents().len(), 3); + let red = index_of(&app, RED); + app.run("file.close", json!({"document": red})).unwrap(); + // Saving marks the revision saved (the file write itself is the save service's job). + assert!(app.session.set_active(index_of(&app, BLUE))); + let st = app.session.active_mut().unwrap(); + st.saved_revision = st.revision; + prefs_ui::tick(&mut app, &ctx); + let left = list_recovery(&dir); + assert_eq!(left.len(), 1); + assert_eq!(photocraft_compose::flatten(&photocraft_format::recover(&left[0]).unwrap()).px.first().copied(), Some(GREEN)); + let green = index_of(&app, GREEN); + app.run("file.close", json!({"document": green})).unwrap(); + prefs_ui::tick(&mut app, &ctx); + assert!(list_recovery(&dir).is_empty()); + drop(app); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn failed_background_autosave_is_reported_and_retried_without_another_edit() { + let dir = temp("autosave-retry"); + let recovery = dir.join("Recovery"); + // A regular file prevents creation of the recovery bundle directory. + std::fs::write(&recovery, b"blocked").unwrap(); + let ctx = egui::Context::default(); + let mut app = launch(&recovery); + new_doc(&mut app, "#ff0000"); + let revision = app.session.active().unwrap().revision; + assert_ne!(revision, 0, "a sidecar-less bundle lists as revision 0"); + autosave(&mut app, &ctx); + + let mut failed = false; + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); + while std::time::Instant::now() < deadline { + prefs_ui::tick(&mut app, &ctx); + if app.ui.status.starts_with("Autosave failed:") { + failed = true; + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } + assert!(failed, "background write failures must reach the UI"); + assert!(app.ui.status_error); + assert!(list_recovery(&recovery).is_empty()); + + // No edit occurs: retrying this very same revision must still work. + std::fs::remove_file(&recovery).unwrap(); + std::fs::create_dir(&recovery).unwrap(); + autosave(&mut app, &ctx); + // The worker commits the bundle's manifest before it publishes the sidecar, and + // `list_recovery` lists a committed sidecar-less bundle (revision 0) on its own, so + // the first non-empty listing can come from the middle of the write. Wait for the write + // to finish: the entry then carries the sidecar's revision. + let mut recovered = Vec::new(); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); + while std::time::Instant::now() < deadline { + prefs_ui::tick(&mut app, &ctx); + recovered = list_recovery(&recovery); + if recovered.iter().any(|e| e.info.revision == revision) { + break; + } + std::thread::sleep(std::time::Duration::from_millis(10)); + } + assert_eq!(recovered.len(), 1, "unchanged revision should retry after failure"); + assert_eq!(recovered[0].info.revision, revision); + let restored = photocraft_format::recover(&recovered[0]).unwrap(); + assert_eq!(photocraft_compose::flatten(&restored).px.first().copied(), Some(RED)); + drop(app); + std::fs::remove_dir_all(dir).unwrap(); +} + +use std::sync::atomic::{AtomicUsize, Ordering}; + +fn temp(tag: &str) -> PathBuf { + static N: AtomicUsize = AtomicUsize::new(0); + let d = std::env::temp_dir().join(format!("photocraft-services-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed))); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn red_png(w: u32, h: u32) -> Vec { + let img = Image::from_u8(w, h, ChannelLayout::Rgba, [255u8, 0, 0, 255].repeat((w * h) as usize)).unwrap(); + photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &EncodeOptions::default()).unwrap() +} + +/// A 1×1 uncompressed 32-bit TGA (TGA has no magic number). +fn tga_1x1() -> Vec { + let mut b = vec![0u8, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 32, 8]; + b.extend_from_slice(&[0, 0, 255, 255]); // BGRA red + b +} + +#[test] +fn copied_image_file_pastes_its_pixels() { + let dir = temp("png"); + let png = dir.join("red.png"); + std::fs::write(&png, red_png(3, 2)).unwrap(); + let (w, h, px) = image_from_files(&[png]).unwrap(); + assert_eq!((w, h), (3, 2)); + assert_eq!(px, [255u8, 0, 0, 255].repeat(6)); +} + +#[test] +fn crlf_from_a_uri_list_is_ignored() { + // arboard splits text/uri-list on LF, so GNOME Files' CRLF list leaves a '\r' behind. + let dir = temp("crlf"); + let png = dir.join("red image.png"); + std::fs::write(&png, red_png(2, 2)).unwrap(); + let with_cr = PathBuf::from(format!("{}\r", png.display())); + assert_eq!(image_from_files(&[with_cr]).map(|(w, h, _)| (w, h)), Some((2, 2))); +} + +#[test] +fn non_images_are_skipped_for_the_first_image() { + let dir = temp("mixed"); + let (txt, png) = (dir.join("notes.txt"), dir.join("red.png")); + std::fs::write(&txt, b"not an image").unwrap(); + std::fs::write(&png, red_png(4, 1)).unwrap(); + let missing = dir.join("gone.png"); + assert_eq!(image_from_files(&[missing.clone(), txt.clone(), dir.clone(), png]).map(|(w, h, _)| (w, h)), Some((4, 1))); + assert!(image_from_files(&[missing, txt, dir]).is_none()); + assert!(image_from_files(&[]).is_none()); +} + +#[test] +fn a_damaged_image_is_skipped_not_a_panic() { + let dir = temp("damaged"); + let bad = dir.join("cut.png"); + std::fs::write(&bad, &red_png(8, 8)[..20]).unwrap(); + assert!(image_from_files(&[bad]).is_none()); +} + +fn encoded(format: photocraft_codecs::Format, w: u32, h: u32) -> Vec { + let img = Image::from_u8(w, h, ChannelLayout::Rgba, [255u8, 0, 0, 255].repeat((w * h) as usize)).unwrap(); + photocraft_codecs::encode(&img, format, &EncodeOptions::default()).unwrap() +} + +#[test] +fn non_png_clipboard_images_paste() { + // #2546: a screenshot tool that offers only image/bmp or image/jpeg pasted nothing. + let bmp = encoded(photocraft_codecs::Format::Bmp, 3, 2); + let offers = vec!["text/plain".to_string(), "image/bmp".to_string()]; + let mut asked = Vec::new(); + let got = image_from_offers(&offers, |mime| { + asked.push(mime.to_string()); + Some(bmp.clone()) + }); + assert_eq!(got, Some((3, 2, [255u8, 0, 0, 255].repeat(6)))); + assert_eq!(asked, ["image/bmp"], "text offers are never fetched"); + + let jpeg = encoded(photocraft_codecs::Format::Jpeg, 16, 8); + let offers = vec!["image/jpeg".to_string()]; + assert_eq!(image_from_offers(&offers, |_| Some(jpeg.clone())).map(|(w, h, _)| (w, h)), Some((16, 8))); +} + +#[test] +fn undecodable_clipboard_offers_fall_through_to_the_next() { + let bmp = encoded(photocraft_codecs::Format::Bmp, 2, 2); + let cut = encoded(photocraft_codecs::Format::Png, 8, 8)[..20].to_vec(); + let offers: Vec = ["image/webp", "image/png", "image/x-tga", "image/bmp"].map(String::from).to_vec(); + let got = image_from_offers(&offers, |mime| match mime { + "image/webp" => None, + "image/png" => Some(cut.clone()), + "image/x-tga" => Some(b"not an image".to_vec()), + _ => Some(bmp.clone()), + }); + assert_eq!(got.map(|(w, h, _)| (w, h)), Some((2, 2))); + assert!(image_from_offers(&offers[..3], |_| Some(cut.clone())).is_none()); + assert!(image_from_offers(&[], |_| Some(bmp.clone())).is_none()); +} + +#[test] +fn clipboard_tga_is_trusted_only_by_its_type() { + assert_eq!(image_from_offers(&["image/x-tga".to_string()], |_| Some(tga_1x1())), Some((1, 1, vec![255, 0, 0, 255]))); + assert!(image_from_offers(&["image/x-unknown".to_string()], |_| Some(tga_1x1())).is_none()); +} + +#[test] +fn tga_is_trusted_only_with_its_extension() { + let dir = temp("tga"); + let (tga, bin) = (dir.join("red.tga"), dir.join("red.bin")); + std::fs::write(&tga, tga_1x1()).unwrap(); + std::fs::write(&bin, tga_1x1()).unwrap(); + assert_eq!(image_from_files(&[tga]), Some((1, 1, vec![255, 0, 0, 255]))); + assert!(image_from_files(&[bin]).is_none()); +} + +/// A 121 × 81 white rectangle with a 1-unit black stroke, written as markup the way a vector +/// app's Copy puts it on the clipboard as text. +const COPIED_RECTANGLE: &str = r##" + + + +"##; + +/// A 40 × 20 drawing whose left half is a red square; the right half has no art. +const HALF_RED: &str = r##""##; + +/// The clipboard as `clipboard_image` reads it when it holds only `text`. +fn from_text(text: &str) -> Option<(u32, u32, Vec)> { + clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| Ok(Some(text.to_string()))).expect("no read fails") +} + +fn pixel(img: &(u32, u32, Vec), x: u32, y: u32) -> [u8; 4] { + let at = ((y * img.0 + x) * 4) as usize; + img.2[at..at + 4].try_into().unwrap() +} + +#[test] +fn svg_markup_copied_as_text_pastes_its_pixels() { + let img = from_text(COPIED_RECTANGLE).expect("the drawing's pixels"); + assert_eq!((img.0, img.1, img.2.len()), (121, 81, 121 * 81 * 4), "one pixel per SVG unit"); + assert_eq!(pixel(&img, 60, 40), [255, 255, 255, 255], "the white fill"); + for (x, y) in [(0, 40), (120, 40), (60, 0), (60, 80)] { + let [r, g, b, a] = pixel(&img, x, y); + assert!(a == 255 && r < 8 && g < 8 && b < 8, "the black stroke at {x},{y}: {:?}", [r, g, b, a]); + } + let img = from_text(HALF_RED).expect("the drawing's pixels"); + assert_eq!((img.0, img.1), (40, 20)); + assert_eq!(pixel(&img, 5, 10), [255, 0, 0, 255]); + assert_eq!(pixel(&img, 30, 10), [0, 0, 0, 0], "transparent where there is no art"); +} + +#[test] +fn text_that_is_not_a_drawing_pastes_nothing() { + for text in ["", "hello", "

not a drawing

", ""] { + assert!(from_text(text).is_none(), "{text:?}"); + } + // Markup that doesn't parse, and markup past the read limit, paste nothing either. + assert!(from_text("").is_none()); + let mut huge = String::from(""); + huge.push_str(&" ".repeat(usize::try_from(MAX_CLIPBOARD_SVG_BYTES).unwrap())); + huge.push_str(""); + assert!(from_text(&huge).is_none(), "markup past the limit isn't parsed"); +} + +#[test] +fn svg_clipboard_text_accepts_a_long_xml_preamble() { + let padding = "x".repeat(8192); + for prefix in [format!(""), format!("\n"), format!("")] { + let text = format!("{prefix}{HALF_RED}"); + let img = from_text(&text).expect("SVG after the XML preamble"); + assert_eq!((img.0, img.1), (40, 20)); + assert_eq!(pixel(&img, 5, 10), [255, 0, 0, 255]); + assert_eq!(pixel(&img, 30, 10), [0, 0, 0, 0]); + } + assert!(from_text(&format!("")).is_none()); + assert!(from_text(&format!("")).is_none()); +} + +#[test] +fn a_drawing_past_the_pixel_limit_pastes_scaled_down() { + let wide = r##""##; + let img = from_text(wide).expect("scaled down, not dropped"); + let pixels = u64::from(img.0) * u64::from(img.1); + assert!(pixels <= 24_000_000 && pixels > 12_000_000, "{} × {}", img.0, img.1); + assert!(img.0.abs_diff(img.1 * 2) <= 2, "the proportions are kept: {} × {}", img.0, img.1); + assert_eq!(pixel(&img, img.0 / 2, img.1 / 2), [0, 0, 255, 255]); +} + +/// Copied files and an image come before SVG text, which is read only when neither is there. +#[test] +fn copied_files_and_images_come_before_svg_text() { + let dir = temp("svg-order"); + let png = dir.join("red.png"); + std::fs::write(&png, red_png(3, 2)).unwrap(); + let blue = (2, 2, [0u8, 0, 255, 255].repeat(4)); + let mut text_read = false; + let svg_text = |read: &mut bool| { + *read = true; + Ok(Some(COPIED_RECTANGLE.to_string())) + }; + let got = clipboard_image(&mut text_read, |_| Ok(Some(vec![png.clone()])), |_| Ok(Some(blue.clone())), svg_text); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((3, 2)), "the copied file"); + let got = clipboard_image(&mut text_read, |_| Ok(None), |_| Ok(Some(blue.clone())), svg_text); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((2, 2)), "the image, not the SVG text"); + assert!(!text_read, "the text isn't read while there is a file or an image"); + // A copied file that isn't an image (an .svg) falls through to the image, then the text. + let svg = dir.join("drawing.svg"); + std::fs::write(&svg, COPIED_RECTANGLE).unwrap(); + let got = clipboard_image(&mut (), |_| Ok(Some(vec![svg.clone()])), |_| Ok(None), |_| Ok(Some(COPIED_RECTANGLE.to_string()))); + assert_eq!(got.unwrap().map(|(w, h, _)| (w, h)), Some((121, 81))); +} + +/// A failed read, such as one of a busy clipboard, is the paste's error: the reads after it, +/// the SVG text included, don't run. +#[test] +fn a_failed_clipboard_read_is_an_error_and_the_text_is_not_read() { + fn busy() -> Result { + Err("the clipboard is busy".to_string()) + } + let mut text_read = false; + let svg_text = |read: &mut bool| { + *read = true; + Ok(Some(COPIED_RECTANGLE.to_string())) + }; + let got = clipboard_image(&mut text_read, |_| busy(), |_| Ok(None), svg_text); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the file list's error"); + let got = clipboard_image(&mut text_read, |_| Ok(None), |_| busy(), svg_text); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the image's error"); + assert!(!text_read, "the text isn't read after a failed read"); + let got = clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| busy()); + assert_eq!(got, Err("the clipboard is busy".to_string()), "the text's error"); +} + +/// Only a clipboard with nothing in the format asked for reads as nothing to paste. A read +/// that fails in any other way, such as a broken pipe on Wayland, is the paste's error, for +/// the copied files and the text alike. Text that isn't UTF-8 reads as no SVG markup. +#[test] +fn only_missing_content_reads_as_nothing_to_paste() { + use arboard::Error; + let failures = || [Error::ClipboardOccupied, Error::ClipboardNotSupported, Error::Unknown { description: "broken pipe".to_string() }]; + assert_eq!(clipboard_read(Ok::<_, Error>(vec![PathBuf::from("a.png")])), Ok(Some(vec![PathBuf::from("a.png")]))); + assert_eq!(clipboard_read::>(Err(Error::ContentNotAvailable)), Ok(None)); + for error in failures().into_iter().chain([Error::ConversionFailure]) { + let message = error.to_string(); + assert_eq!(clipboard_read::>(Err(error)), Err(message.clone()), "{message}"); + } + assert_eq!(clipboard_text(Ok("words".to_string())), Ok(Some("words".to_string()))); + assert_eq!(clipboard_text(Err(Error::ContentNotAvailable)), Ok(None)); + assert_eq!(clipboard_text(Err(Error::ConversionFailure)), Ok(None), "text that isn't UTF-8"); + for error in failures() { + let message = error.to_string(); + assert_eq!(clipboard_text(Err(error)), Err(message.clone()), "{message}"); + } +} + +/// Edit › Paste with SVG markup as the clipboard's only content adds a new layer that holds the +/// drawing's art; other text is still a quiet "nothing to paste". +#[test] +fn paste_turns_svg_text_on_the_clipboard_into_a_layer() { + let text: Rc> = Rc::new(RefCell::new(HALF_RED.to_string())); + let clip = text.clone(); + let services = Services { + clipboard_get_image: Some(Box::new(move || clipboard_image(&mut (), |_| Ok(None), |_| Ok(None), |_| Ok(Some(clip.borrow().clone()))))), + ..Default::default() + }; + let mut app = PhotocraftApp::new(Session::new(), services); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + let layers = app.session.active().unwrap().doc.layers.len(); + let r = photocraft_ui_egui::menus::invoke(&mut app, &ctx, "edit.paste", json!({})).unwrap(); + assert_ne!(r["pasted"], json!(false), "{r}"); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers.len(), layers + 1); + let surface = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + let bounds = surface.content_bounds(); + assert_eq!((bounds.width(), bounds.height()), (20, 20), "the red half; the transparent half adds nothing"); + *text.borrow_mut() = "just words".to_string(); + let r = photocraft_ui_egui::menus::invoke(&mut app, &ctx, "edit.paste", json!({})).unwrap(); + assert_eq!(r["pasted"], json!(false)); + assert_eq!(app.ui.status, "Nothing to paste: the clipboard holds no image"); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1); +} + +/// #7582: a panic while reading the OS clipboard (in arboard, or decoding a copied file) is +/// the paste's error in the status bar, not a crash: the document stays and the next paste +/// works. The same for a panic while writing it. +#[test] +fn a_panicking_clipboard_read_is_reported_not_a_crash() { + let panics = Rc::new(Cell::new(true)); + let p = panics.clone(); + let get = guarded_clipboard_get(move || if p.get() { panic!("clipboard decoder bug") } else { Ok(Some((2, 2, [0u8, 0, 255, 255].repeat(4)))) }); + let mut app = PhotocraftApp::new(Session::new(), Services { clipboard_get_image: Some(get), ..Default::default() }); + app.run("file.new", json!({"width": 8, "height": 8})).unwrap(); + let layers = app.session.active().unwrap().doc.layers.len(); + + let error = app.run("edit.paste", json!({})).unwrap_err(); + assert!(error.contains("Paste failed"), "{error}"); + assert!(app.ui.status_error && app.ui.status.contains("Paste failed"), "{}", app.ui.status); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers, "the document is unchanged"); + + panics.set(false); + app.run("edit.paste", json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1, "the next paste works"); + + let mut set = guarded_clipboard_set(|_: u32, _: u32, _: &[u8]| panic!("clipboard writer bug")); + assert!(set(1, 1, &[0; 4]).unwrap_err().contains("Copy failed")); +} diff --git a/apps/photocraft/src/single_instance.rs b/apps/photocraft/src/single_instance.rs index d2cd65c678..4d836adc90 100644 --- a/apps/photocraft/src/single_instance.rs +++ b/apps/photocraft/src/single_instance.rs @@ -3,8 +3,9 @@ //! copy of the program. //! //! The first launch takes an exclusive OS lock on `instance.lock` in the settings directory (the OS -//! drops it when the process ends, crash included), listens on a loopback port and writes the port -//! plus a fresh 256-bit token to `instance.json`. A later launch that finds the lock taken sends its +//! drops it when the process ends, crash included), listens on a loopback port (inside a Flatpak +//! sandbox, on a Unix socket: see [`transport`]) and writes where it listens plus a fresh 256-bit +//! token to `instance.json`. A later launch that finds the lock taken sends its //! paths there; the primary queues them for the UI as [`OsEvent::Open`], which opens each one and //! brings the window forward. Explorer starts one process per file when several are opened at once; //! the lock decides the race, so they all end up as tabs in one window. @@ -26,7 +27,11 @@ use std::fs::{File, OpenOptions}; use std::io::{BufReader, Read, Write}; use std::net::{Ipv4Addr, SocketAddr, TcpListener, TcpStream}; +#[cfg(unix)] +use std::os::unix::net::{UnixListener, UnixStream}; use std::path::Path; +#[cfg(unix)] +use std::path::PathBuf; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::mpsc::{SyncSender, sync_channel}; use std::sync::{Arc, Mutex, PoisonError}; @@ -36,8 +41,13 @@ use photocraft_automation::security::{LineRead, generate_token, read_bounded_lin use photocraft_ui_egui::{OsEvent, OsEventsFn}; use serde_json::{Value, json}; +use crate::launch_args::PerProcess; + const LOCK_FILE: &str = "instance.lock"; const INFO_FILE: &str = "instance.json"; +/// The Unix socket a sandboxed primary listens on, in the app's runtime directory. +#[cfg(unix)] +const SOCKET_FILE: &str = "instance.sock"; /// More paths than this in one request is not a file manager. const MAX_PATHS: usize = 4096; /// Launches served at once; a stalled client holds only its own slot, never the next launch. @@ -59,6 +69,45 @@ pub struct Timing { /// Generous enough for a primary that is still opening its window. pub const TIMING: Timing = Timing { handoff: Duration::from_secs(10), ack: Duration::from_secs(8), request: Duration::from_secs(5) }; +/// How the primary listens for later launches. +#[derive(Clone, Debug, PartialEq)] +pub enum Transport { + /// A loopback port (the default): private to the machine, reachable by every process on it. + Loopback, + /// A Unix socket at this path. Inside a Flatpak sandbox a loopback port is not enough: each + /// `flatpak run` has its own network namespace, so the second sandbox's `127.0.0.1` never + /// reaches the first (#1540). `$XDG_RUNTIME_DIR/app//` is the same directory in every + /// sandbox of the app, and the user's alone (0700). + #[cfg(unix)] + Socket(PathBuf), +} + +/// The transport for this process: a Unix socket when running inside a Flatpak (`FLATPAK_ID` is +/// set and `XDG_RUNTIME_DIR` names the runtime directory), else a loopback port. +pub fn transport() -> Transport { + #[cfg(unix)] + if let Some(path) = sandbox_socket(std::env::var_os("FLATPAK_ID").as_deref(), std::env::var_os("XDG_RUNTIME_DIR").as_deref()) { + return Transport::Socket(path); + } + Transport::Loopback +} + +/// `$XDG_RUNTIME_DIR/app//instance.sock`, when both are set. +#[cfg(unix)] +fn sandbox_socket(flatpak_id: Option<&std::ffi::OsStr>, runtime_dir: Option<&std::ffi::OsStr>) -> Option { + let id = flatpak_id.filter(|v| !v.is_empty())?; + let dir = runtime_dir.filter(|v| !v.is_empty())?; + Some(Path::new(dir).join("app").join(id).join(SOCKET_FILE)) +} + +/// Where the running primary listens, as `instance.json` records it. +#[derive(Clone, Debug, PartialEq)] +enum Endpoint { + Port(u16), + #[cfg(unix)] + Socket(PathBuf), +} + /// Outcome of [`claim`]. pub enum Launch { /// This process is the running instance; keep [`Primary`] alive and attach it to the app. @@ -82,8 +131,54 @@ pub struct LaunchContext<'a> { pub control: bool, /// Paths that aren't valid Unicode: this launch reports them. pub unreadable_paths: bool, - /// Options that only apply to the process they're given to (`--safe-gpu`, …). - pub launch_options: &'a [&'static str], + /// Options that only apply to the process they're given to (`--safe-gpu`, …), including the + /// ones this launch's environment sets (see [`env_launch_options`]). + pub launch_options: &'a [PerProcess], +} + +/// Variables documented as the equivalent of a per-launch flag, each with the option it stands for +/// (`PHOTOCRAFT_PRESENT_MODE` is `--present-mode`) and the rule that says whether its value asks for +/// that option at all. Setting one asks for the same thing as giving the flag, so it belongs to this +/// process alone. These are read where they take effect, not by the command line; the variables +/// that are a flag's fallback (`PHOTOCRAFT_IN_WINDOW_MENUS`, the automation roots) are counted by +/// [`crate::launch_args::parse`], as clap reads them. +const ENV_LAUNCH_OPTIONS: [(&str, PerProcess, ValueRule); 3] = [ + ("PHOTOCRAFT_PRESENT_MODE", PerProcess::PresentMode, present_mode_is_on), + ("PHOTOCRAFT_FRAME_LATENCY", PerProcess::FrameLatency, frame_latency_is_on), + ("PHOTOCRAFT_STROKE_TRACE", PerProcess::StrokeTrace, stroke_trace_is_on), +]; + +/// Whether a variable's value asks this launch for its own option. Each rule matches how that +/// variable is read where it takes effect, so a value ignored there keeps no process here either. +type ValueRule = fn(&str) -> bool; + +/// A value `presentation::parse` accepts, handed over as written: it trims, treats empty as unset and +/// warns and ignores anything it does not recognise, so an unrecognised `PHOTOCRAFT_PRESENT_MODE` +/// changes nothing there and so needs no process of its own here. +fn present_mode_is_on(value: &str) -> bool { + crate::presentation::parse(Some(value), None).0.mode.is_some() +} + +/// [`present_mode_is_on`] for `PHOTOCRAFT_FRAME_LATENCY`, which takes `1` to `3`. +fn frame_latency_is_on(value: &str) -> bool { + crate::presentation::parse(None, Some(value)).0.latency.is_some() +} + +/// Anything except `""`, `0`, `off` and `false`, which is how `StrokeTrace::from_value` reads it: the +/// rest is a stderr summary or a CSV path. It trims, so `" 0 "` is off there and off here. +fn stroke_trace_is_on(value: &str) -> bool { + !matches!(value.trim(), "" | "0" | "off" | "false") +} + +/// The per-process options this launch's environment sets. A launch that sets only variables would +/// otherwise hand its files to the running instance, which never sees the override, and exit. +pub fn env_launch_options() -> Vec { + env_launch_options_from(|name| std::env::var_os(name)) +} + +/// [`env_launch_options`] with the reader given, so the rule is testable without the environment. +fn env_launch_options_from(get: impl Fn(&str) -> Option) -> Vec { + ENV_LAUNCH_OPTIONS.iter().filter(|(name, _, is_on)| get(name).is_some_and(|v| is_on(&v.to_string_lossy()))).map(|(_, option, _)| *option).collect() } /// Why this launch runs as its own instance, or `None` to take part in single-instance. @@ -101,7 +196,8 @@ pub fn skip_reason(c: &LaunchContext) -> Option { return Some("some paths aren't valid Unicode".into()); } if !c.launch_options.is_empty() { - return Some(format!("{} only applies to a new process", c.launch_options.join(", "))); + let options: Vec = c.launch_options.iter().map(ToString::to_string).collect(); + return Some(format!("{} only applies to a new process", options.join(", "))); } None } @@ -110,6 +206,10 @@ pub fn skip_reason(c: &LaunchContext) -> Option { pub struct Primary { lock: Mutex>, shared: Arc, + /// The socket file to remove on the first shutdown (a Unix socket transport only); taken + /// then, so the second call (`Drop`) can't remove the socket the next primary has bound. + #[cfg(unix)] + socket: Mutex>, } struct Shared { @@ -151,6 +251,10 @@ impl Primary { self.shared.closed.store(true, Ordering::Release); // Dropping the senders refuses every connection still waiting for the UI. self.shared.inbox.lock().unwrap_or_else(PoisonError::into_inner).pending.clear(); + #[cfg(unix)] + if let Some(path) = self.socket.lock().unwrap_or_else(PoisonError::into_inner).take() { + let _ = std::fs::remove_file(path); + } drop(self.lock.lock().unwrap_or_else(PoisonError::into_inner).take()); } } @@ -211,9 +315,15 @@ pub fn claim(dir: &Path, paths: &[String]) -> Launch { claim_with(dir, paths, TIMING) } -/// [`claim`] with explicit timeouts. Until `timing.handoff` runs out, each round tries the lock -/// (free once a quitting or crashed primary is gone) and then the running primary. +/// [`claim`] with explicit timeouts, over the transport this process would use ([`transport`]). pub fn claim_with(dir: &Path, paths: &[String], timing: Timing) -> Launch { + claim_in(dir, paths, timing, &transport()) +} + +/// [`claim`] over `transport`. Until `timing.handoff` runs out, each round tries the lock (free +/// once a quitting or crashed primary is gone) and then the running primary, wherever +/// `instance.json` says it listens. +pub fn claim_in(dir: &Path, paths: &[String], timing: Timing, transport: &Transport) -> Launch { if let Err(e) = std::fs::create_dir_all(dir) { return Launch::Standalone(format!("{}: {e}", dir.display())); } @@ -227,7 +337,7 @@ pub fn claim_with(dir: &Path, paths: &[String], timing: Timing) -> Launch { loop { match lock.try_lock() { Ok(()) => { - return match serve(dir, lock, timing) { + return match serve(dir, lock, timing, transport) { Ok(primary) => Launch::Primary(primary), Err(e) => Launch::Standalone(e), }; @@ -235,8 +345,8 @@ pub fn claim_with(dir: &Path, paths: &[String], timing: Timing) -> Launch { Err(std::fs::TryLockError::WouldBlock) => {} Err(std::fs::TryLockError::Error(e)) => return Launch::Standalone(format!("{}: {e}", lock_path.display())), } - if let Some((port, token)) = read_info(dir) { - match send(port, &token, paths, deadline) { + if let Some((endpoint, token)) = read_info(dir) { + match send(&endpoint, &token, paths, deadline) { Ok(()) => return Launch::Forwarded, Err(e) => last = e, } @@ -248,11 +358,15 @@ pub fn claim_with(dir: &Path, paths: &[String], timing: Timing) -> Launch { } } -fn serve(dir: &Path, lock: File, timing: Timing) -> Result { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).map_err(|e| format!("loopback listener: {e}"))?; - let port = listener.local_addr().map_err(|e| format!("loopback listener: {e}"))?.port(); +fn serve(dir: &Path, lock: File, timing: Timing, transport: &Transport) -> Result { + let (listener, endpoint) = Listener::bind(transport)?; let token = generate_token().map_err(|e| e.to_string())?; - write_info(dir, port, &token)?; + write_info(dir, &endpoint, &token)?; + #[cfg(unix)] + let socket = match &endpoint { + Endpoint::Socket(path) => Some(path.clone()), + Endpoint::Port(_) => None, + }; let shared = Arc::new(Shared { inbox: Mutex::default(), closed: AtomicBool::new(false) }); let listening = Arc::clone(&shared); let spawned = std::thread::Builder::new().name("single-instance".into()).spawn(move || { @@ -260,7 +374,7 @@ fn serve(dir: &Path, lock: File, timing: Timing) -> Result { let clients = Arc::new(AtomicUsize::new(0)); loop { let stream = match listener.accept() { - Ok((stream, _)) => stream, + Ok(stream) => stream, // Back off instead of spinning when accept keeps failing (e.g. out of descriptors). Err(_) => { std::thread::sleep(Duration::from_millis(100)); @@ -288,11 +402,117 @@ fn serve(dir: &Path, lock: File, timing: Timing) -> Result { } }); spawned.map_err(|e| format!("single-instance listener thread: {e}"))?; - Ok(Primary { lock: Mutex::new(Some(lock)), shared }) + Ok(Primary { + lock: Mutex::new(Some(lock)), + shared, + #[cfg(unix)] + socket: Mutex::new(socket), + }) +} + +/// The primary's listener on either transport. +enum Listener { + Tcp(TcpListener), + #[cfg(unix)] + Unix(UnixListener), +} + +impl Listener { + fn bind(transport: &Transport) -> Result<(Listener, Endpoint), String> { + match transport { + Transport::Loopback => { + let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).map_err(|e| format!("loopback listener: {e}"))?; + let port = listener.local_addr().map_err(|e| format!("loopback listener: {e}"))?.port(); + Ok((Listener::Tcp(listener), Endpoint::Port(port))) + } + #[cfg(unix)] + Transport::Socket(path) => { + let at = |e: std::io::Error| format!("{}: {e}", path.display()); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(at)?; + } + // This process holds the lock, so a file already there is a crashed primary's. + let _ = std::fs::remove_file(path); + let listener = UnixListener::bind(path).map_err(at)?; + // The runtime directory is the user's alone already; keep the socket so too. + std::fs::set_permissions(path, std::os::unix::fs::PermissionsExt::from_mode(0o600)).map_err(at)?; + Ok((Listener::Unix(listener), Endpoint::Socket(path.clone()))) + } + } + } + + fn accept(&self) -> std::io::Result { + match self { + Listener::Tcp(l) => l.accept().map(|(s, _)| Stream::Tcp(s)), + #[cfg(unix)] + Listener::Unix(l) => l.accept().map(|(s, _)| Stream::Unix(s)), + } + } +} + +/// One connection, on either transport. +enum Stream { + Tcp(TcpStream), + #[cfg(unix)] + Unix(UnixStream), +} + +impl Stream { + /// Connect to the primary; `timeout` bounds the loopback connect (a Unix connect is immediate). + fn connect(endpoint: &Endpoint, timeout: Duration) -> std::io::Result { + match endpoint { + Endpoint::Port(port) => TcpStream::connect_timeout(&SocketAddr::from((Ipv4Addr::LOCALHOST, *port)), timeout).map(Stream::Tcp), + #[cfg(unix)] + Endpoint::Socket(path) => UnixStream::connect(path).map(Stream::Unix), + } + } + + fn set_read_timeout(&self, dur: Option) -> std::io::Result<()> { + match self { + Stream::Tcp(s) => s.set_read_timeout(dur), + #[cfg(unix)] + Stream::Unix(s) => s.set_read_timeout(dur), + } + } + + fn set_write_timeout(&self, dur: Option) -> std::io::Result<()> { + match self { + Stream::Tcp(s) => s.set_write_timeout(dur), + #[cfg(unix)] + Stream::Unix(s) => s.set_write_timeout(dur), + } + } + + /// One read through a shared reference (both socket types read through `&self`). + fn read_once(&self, buf: &mut [u8]) -> std::io::Result { + match self { + Stream::Tcp(s) => (&*s).read(buf), + #[cfg(unix)] + Stream::Unix(s) => (&*s).read(buf), + } + } +} + +impl Write for Stream { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + Stream::Tcp(s) => s.write(buf), + #[cfg(unix)] + Stream::Unix(s) => s.write(buf), + } + } + + fn flush(&mut self) -> std::io::Result<()> { + match self { + Stream::Tcp(s) => s.flush(), + #[cfg(unix)] + Stream::Unix(s) => s.flush(), + } + } } /// Read one request, hand its paths to the UI and answer `ok` only if the UI took them. -fn serve_client(stream: TcpStream, token: &str, shared: &Shared, timing: Timing) { +fn serve_client(stream: Stream, token: &str, shared: &Shared, timing: Timing) { let taken = match read_request(&stream, timing.request) { Some(line) => match parse_request(&line, token) { Some((paths, wait)) => shared.hand_over(paths, wait.min(timing.ack)), @@ -308,7 +528,7 @@ fn serve_client(stream: TcpStream, token: &str, shared: &Shared, timing: Timing) /// A reader that fails once `until` passes, however slowly the bytes trickle in. struct Deadline<'a> { - stream: &'a TcpStream, + stream: &'a Stream, until: Instant, } @@ -317,25 +537,30 @@ impl Read for Deadline<'_> { let left = self.until.checked_duration_since(Instant::now()).filter(|d| !d.is_zero()); let left = left.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::TimedOut, "request deadline passed"))?; self.stream.set_read_timeout(Some(left))?; - let mut stream = self.stream; - stream.read(buf) + self.stream.read_once(buf) } } /// The request line, if it arrives complete within `total`. -fn read_request(stream: &TcpStream, total: Duration) -> Option { +fn read_request(stream: &Stream, total: Duration) -> Option { let mut reader = BufReader::new(Deadline { stream, until: Instant::now() + total }); let mut line = String::new(); matches!(read_bounded_line(&mut reader, &mut line), Ok(LineRead::Line)).then_some(line) } -/// Port and token, written to a temporary file and renamed so a reader never sees half of it. +/// Endpoint and token, written to a temporary file and renamed so a reader never sees half of it. /// On Unix the file is the user's alone (0600): another local user who read the token could make /// this app open any path. (Windows keeps the per-user ACL of the settings folder.) -fn write_info(dir: &Path, port: u16, token: &str) -> Result<(), String> { +fn write_info(dir: &Path, endpoint: &Endpoint, token: &str) -> Result<(), String> { let path = dir.join(INFO_FILE); let tmp = dir.join(format!("{INFO_FILE}.{}.tmp", std::process::id())); - let body = json!({"port": port, "token": token, "pid": std::process::id()}).to_string(); + let mut body = json!({"token": token, "pid": std::process::id()}); + match endpoint { + Endpoint::Port(port) => body["port"] = json!(port), + #[cfg(unix)] + Endpoint::Socket(socket) => body["socket"] = json!(socket.to_string_lossy()), + } + let body = body.to_string(); // A leftover from a crash could carry wider permissions; the mode applies only on create. let _ = std::fs::remove_file(&tmp); let mut options = OpenOptions::new(); @@ -349,10 +574,15 @@ fn write_info(dir: &Path, port: u16, token: &str) -> Result<(), String> { }) } -fn read_info(dir: &Path) -> Option<(u16, String)> { +fn read_info(dir: &Path) -> Option<(Endpoint, String)> { let v: Value = serde_json::from_slice(&std::fs::read(dir.join(INFO_FILE)).ok()?).ok()?; + let token = v.get("token")?.as_str()?.to_string(); + #[cfg(unix)] + if let Some(socket) = v.get("socket").and_then(Value::as_str) { + return Some((Endpoint::Socket(PathBuf::from(socket)), token)); + } let port = u16::try_from(v.get("port")?.as_u64()?).ok()?; - Some((port, v.get("token")?.as_str()?.to_string())) + Some((Endpoint::Port(port), token)) } /// `{"token":…,"open":[…],"wait_ms":…}`: the paths, and how long the sender will wait for the UI. @@ -370,11 +600,10 @@ fn parse_request(line: &str, token: &str) -> Option<(Vec, Duration)> { Some((paths, wait)) } -/// Offer `paths` to the primary on `port`; `Ok` once its UI has taken them. -fn send(port: u16, token: &str, paths: &[String], deadline: Instant) -> Result<(), String> { +/// Offer `paths` to the primary at `endpoint`; `Ok` once its UI has taken them. +fn send(endpoint: &Endpoint, token: &str, paths: &[String], deadline: Instant) -> Result<(), String> { let left = deadline.checked_duration_since(Instant::now()).filter(|d| !d.is_zero()).ok_or("out of time")?; - let addr = SocketAddr::from((Ipv4Addr::LOCALHOST, port)); - let mut stream = TcpStream::connect_timeout(&addr, left).map_err(|e| e.to_string())?; + let mut stream = Stream::connect(endpoint, left).map_err(|e| e.to_string())?; // The primary must answer before our deadline, so it waits a little less than we do. let wait_ms = u64::try_from(left.saturating_sub(REPLY_MARGIN).as_millis()).unwrap_or(u64::MAX); let request = json!({"token": token, "open": paths, "wait_ms": wait_ms}); @@ -398,251 +627,4 @@ pub fn absolute(path: &str) -> String { } #[cfg(test)] -mod tests { - use super::*; - use std::path::PathBuf; - use std::sync::mpsc::channel; - - const FAST: Timing = Timing { handoff: Duration::from_millis(1500), ack: Duration::from_millis(500), request: Duration::from_millis(800) }; - - fn temp_dir(name: &str) -> PathBuf { - let dir = std::env::temp_dir().join(format!("photocraft-single-instance-{name}-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&dir); - dir - } - - /// Launch with `paths` on another thread (it blocks until the primary's UI takes them). - fn launch(dir: &Path, paths: &[&str], timing: Timing) -> std::thread::JoinHandle { - let (dir, paths): (PathBuf, Vec) = (dir.to_path_buf(), paths.iter().map(|p| p.to_string()).collect()); - std::thread::spawn(move || claim_with(&dir, &paths, timing)) - } - - /// Play the UI: drain until `n` events arrived (or 5 s passed). - fn drain(ui: &mut OsEventsFn, n: usize) -> Vec { - let mut got = Vec::new(); - let deadline = Instant::now() + Duration::from_secs(5); - while got.len() < n && Instant::now() < deadline { - got.extend(ui()); - std::thread::sleep(Duration::from_millis(10)); - } - got - } - - fn primary(dir: &Path) -> Primary { - match claim_with(dir, &[], FAST) { - Launch::Primary(p) => p, - _ => panic!("first launch should be primary"), - } - } - - #[test] - fn second_launch_hands_its_paths_to_the_first() { - let dir = temp_dir("handoff"); - let primary = primary(&dir); - let mut ui = primary.connect(&egui::Context::default()); - let paths = ["C:\\art\\one.psd", "/home/a/two é.png"]; - let second = launch(&dir, &paths, TIMING); - assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(paths.iter().map(|p| p.to_string()).collect())]); - assert!(matches!(second.join().unwrap(), Launch::Forwarded)); - // A plain second start (no files) just brings the window forward. - let third = launch(&dir, &[], TIMING); - assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(Vec::new())]); - assert!(matches!(third.join().unwrap(), Launch::Forwarded)); - drop(primary); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn simultaneous_launches_all_land_in_one_instance() { - let dir = temp_dir("race"); - let (tx, rx) = channel(); - for i in 0..6 { - let (dir, tx) = (dir.clone(), tx.clone()); - std::thread::spawn(move || { - let _ = tx.send(claim(&dir, &[format!("file{i}.psd")])); - }); - } - drop(tx); - let (mut primaries, mut forwarded, mut events) = (Vec::new(), 0, Vec::new()); - let mut ui: Option = None; - let deadline = Instant::now() + Duration::from_secs(10); - while primaries.len() + forwarded < 6 && Instant::now() < deadline { - while let Ok(launch) = rx.try_recv() { - match launch { - Launch::Primary(p) => primaries.push(p), - Launch::Forwarded => forwarded += 1, - Launch::Standalone(reason) => panic!("standalone: {reason}"), - } - } - if ui.is_none() - && let Some(p) = primaries.first() - { - ui = Some(p.connect(&egui::Context::default())); - } - if let Some(ui) = ui.as_mut() { - events.extend(ui()); - } - std::thread::sleep(Duration::from_millis(10)); - } - assert_eq!((primaries.len(), forwarded, events.len()), (1, 5, 5)); - drop(primaries); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn a_crashed_instance_leaves_nothing_in_the_way() { - let dir = temp_dir("stale"); - std::fs::create_dir_all(&dir).unwrap(); - // instance.json from a process that is gone; the lock is free. - std::fs::write(dir.join(INFO_FILE), r#"{"port":1,"token":"x","pid":1}"#).unwrap(); - let primary = primary(&dir); - assert!(read_info(&dir).is_some_and(|(port, _)| port != 1)); - drop(primary); - // Released with the handle, as on process exit. - assert!(matches!(claim_with(&dir, &[], FAST), Launch::Primary(_))); - let _ = std::fs::remove_dir_all(&dir); - } - - /// Review of #3755, point 1 (and the quitting half of point 3): a launch waiting on a primary - /// that shuts down (closing, or about to relaunch itself in CPU mode) isn't told `ok`; it takes - /// the lock over and opens the file itself. - #[test] - fn a_launch_waiting_on_a_closing_instance_takes_over() { - let dir = temp_dir("closing"); - let primary = primary(&dir); - // The UI never drains (it is shutting down, or never came up). - let waiting = launch(&dir, &["a.psd"], FAST); - std::thread::sleep(Duration::from_millis(200)); - primary.shutdown(); - assert!(matches!(waiting.join().unwrap(), Launch::Primary(_)), "the waiting launch becomes the primary"); - // After shutdown new connections are refused too. - let (port, token) = read_info(&dir).unwrap(); - drop(primary); - let short = Instant::now() + Duration::from_millis(300); - assert!(send(port, &token, &["b.psd".into()], short).is_err()); - let _ = std::fs::remove_dir_all(&dir); - } - - /// Review of #3755, point 3: a primary whose UI never takes the paths (hung) doesn't - /// acknowledge them, and doesn't keep them queued to open later as well. - #[test] - fn a_hung_instance_never_swallows_a_file() { - let dir = temp_dir("hung-ui"); - let primary = primary(&dir); - let mut ui = primary.connect(&egui::Context::default()); - let started = Instant::now(); - assert!(matches!(claim_with(&dir, &["a.psd".into()], FAST), Launch::Standalone(_))); - assert!(started.elapsed() < FAST.handoff + Duration::from_secs(1), "{:?}", started.elapsed()); - assert!(ui().is_empty(), "withdrawn, not opened later"); - drop(primary); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn requests_without_the_token_or_malformed_are_rejected() { - let token = generate_token().unwrap(); - let ok = parse_request(&json!({"token": token, "open": ["a"], "wait_ms": 250}).to_string(), &token); - assert_eq!(ok, Some((vec!["a".to_string()], Duration::from_millis(250)))); - let wrong = generate_token().unwrap(); - assert_eq!(parse_request(&json!({"token": wrong, "open": ["a"]}).to_string(), &token), None); - for bad in ["", "not json", "{}", r#"{"token":5}"#, &json!({"token": token}).to_string(), &json!({"token": token, "open": [1]}).to_string()] { - assert_eq!(parse_request(bad, &token), None, "{bad:?}"); - } - let many: Vec = (0..=MAX_PATHS).map(|i| i.to_string()).collect(); - assert_eq!(parse_request(&json!({"token": token, "open": many}).to_string(), &token), None); - } - - #[test] - fn a_stalled_client_does_not_hold_up_the_next_launch() { - let dir = temp_dir("stalled"); - let primary = primary(&dir); - let mut ui = primary.connect(&egui::Context::default()); - let (port, _) = read_info(&dir).unwrap(); - // Connects and never sends a line (each would otherwise hold the listener). - let stalled: Vec = (0..3).map(|_| TcpStream::connect((Ipv4Addr::LOCALHOST, port)).unwrap()).collect(); - let started = Instant::now(); - let next = launch(&dir, &["b.psd"], FAST); - assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(vec!["b.psd".into()])]); - assert!(matches!(next.join().unwrap(), Launch::Forwarded)); - assert!(started.elapsed() < FAST.request, "took {:?}", started.elapsed()); - drop(stalled); - drop(primary); - let _ = std::fs::remove_dir_all(&dir); - } - - /// A client that trickles a byte at a time can't keep its slot past the request deadline. - #[test] - fn a_trickling_client_is_cut_off_at_the_request_deadline() { - let dir = temp_dir("trickle"); - let primary = primary(&dir); - let (port, _) = read_info(&dir).unwrap(); - let mut slow = TcpStream::connect((Ipv4Addr::LOCALHOST, port)).unwrap(); - let started = Instant::now(); - let mut cut_off = false; - while started.elapsed() < FAST.request * 4 { - if slow.write_all(b"{").is_err() { - cut_off = true; - break; - } - std::thread::sleep(Duration::from_millis(100)); - } - assert!(cut_off, "still connected after {:?}", started.elapsed()); - assert!(started.elapsed() < FAST.request * 3, "{:?}", started.elapsed()); - drop(primary); - let _ = std::fs::remove_dir_all(&dir); - } - - #[cfg(unix)] - #[test] - fn the_token_file_is_private_to_the_user() { - use std::os::unix::fs::PermissionsExt; - let dir = temp_dir("mode"); - std::fs::create_dir_all(&dir).unwrap(); - // A wide leftover temporary file from a crashed start doesn't widen the new one. - let tmp = dir.join(format!("{INFO_FILE}.{}.tmp", std::process::id())); - std::fs::write(&tmp, "{}").unwrap(); - std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o644)).unwrap(); - write_info(&dir, 1, &generate_token().unwrap()).unwrap(); - let mode = std::fs::metadata(dir.join(INFO_FILE)).unwrap().permissions().mode() & 0o777; - assert_eq!(mode, 0o600); - let _ = std::fs::remove_dir_all(&dir); - } - - #[test] - fn an_unreachable_instance_falls_back_to_a_standalone_launch() { - let dir = temp_dir("unreachable"); - std::fs::create_dir_all(&dir).unwrap(); - // Someone holds the lock but nothing listens on the advertised port. - let held = File::create(dir.join(LOCK_FILE)).unwrap(); - held.lock().unwrap(); - let closed = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap().local_addr().unwrap().port(); - write_info(&dir, closed, &generate_token().unwrap()).unwrap(); - assert!(matches!(claim_with(&dir, &["a.psd".into()], FAST), Launch::Standalone(_))); - drop(held); - let _ = std::fs::remove_dir_all(&dir); - } - - /// Review of #3755, point 2 and the smaller points: who takes part in single-instance. - #[test] - fn only_per_user_launches_without_process_options_take_part() { - let base = LaunchContext { - per_user_settings: true, - new_instance_flag: false, - new_instance_env: None, - control: false, - unreadable_paths: false, - launch_options: &[], - }; - assert_eq!(skip_reason(&base), None); - // A portable copy's folder or a PHOTOCRAFT_CONFIG_DIR may be shared between users. - assert!(skip_reason(&LaunchContext { per_user_settings: false, ..base }).is_some()); - assert!(skip_reason(&LaunchContext { new_instance_flag: true, ..base }).is_some()); - assert!(skip_reason(&LaunchContext { new_instance_env: Some("1"), ..base }).is_some()); - assert_eq!(skip_reason(&LaunchContext { new_instance_env: Some("0"), ..base }), None); - assert_eq!(skip_reason(&LaunchContext { new_instance_env: Some(""), ..base }), None); - assert!(skip_reason(&LaunchContext { control: true, ..base }).is_some()); - assert!(skip_reason(&LaunchContext { unreadable_paths: true, ..base }).is_some()); - let reason = skip_reason(&LaunchContext { launch_options: &["--safe-gpu"], ..base }); - assert!(reason.is_some_and(|r| r.contains("--safe-gpu"))); - } -} +mod tests; diff --git a/apps/photocraft/src/single_instance/tests.rs b/apps/photocraft/src/single_instance/tests.rs new file mode 100644 index 0000000000..dc5755719f --- /dev/null +++ b/apps/photocraft/src/single_instance/tests.rs @@ -0,0 +1,367 @@ +use super::*; +use std::path::PathBuf; +use std::sync::mpsc::channel; + +const FAST: Timing = Timing { handoff: Duration::from_millis(1500), ack: Duration::from_millis(500), request: Duration::from_millis(800) }; + +fn temp_dir(name: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!("photocraft-single-instance-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + dir +} + +/// Launch with `paths` on another thread (it blocks until the primary's UI takes them). +fn launch(dir: &Path, paths: &[&str], timing: Timing) -> std::thread::JoinHandle { + let (dir, paths): (PathBuf, Vec) = (dir.to_path_buf(), paths.iter().map(|p| p.to_string()).collect()); + std::thread::spawn(move || claim_with(&dir, &paths, timing)) +} + +/// Play the UI: drain until `n` events arrived (or 5 s passed). +fn drain(ui: &mut OsEventsFn, n: usize) -> Vec { + let mut got = Vec::new(); + let deadline = Instant::now() + Duration::from_secs(5); + while got.len() < n && Instant::now() < deadline { + got.extend(ui()); + std::thread::sleep(Duration::from_millis(10)); + } + got +} + +fn primary(dir: &Path) -> Primary { + match claim_with(dir, &[], FAST) { + Launch::Primary(p) => p, + _ => panic!("first launch should be primary"), + } +} + +/// A socket path short enough for every Unix (`sun_path` is about 100 bytes; macOS: 104). +#[cfg(unix)] +fn short_socket_path(tag: &str) -> PathBuf { + std::env::temp_dir().join(format!("pc-si-{}-{tag}", std::process::id())).join(SOCKET_FILE) +} + +/// The loopback port the primary under `dir` listens on. +fn port_of(dir: &Path) -> u16 { + match read_info(dir).unwrap().0 { + Endpoint::Port(port) => port, + #[cfg(unix)] + Endpoint::Socket(path) => panic!("expected a loopback port, found {}", path.display()), + } +} + +#[test] +fn second_launch_hands_its_paths_to_the_first() { + let dir = temp_dir("handoff"); + let primary = primary(&dir); + let mut ui = primary.connect(&egui::Context::default()); + let paths = ["C:\\art\\one.psd", "/home/a/two é.png"]; + let second = launch(&dir, &paths, TIMING); + assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(paths.iter().map(|p| p.to_string()).collect())]); + assert!(matches!(second.join().unwrap(), Launch::Forwarded)); + // A plain second start (no files) just brings the window forward. + let third = launch(&dir, &[], TIMING); + assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(Vec::new())]); + assert!(matches!(third.join().unwrap(), Launch::Forwarded)); + drop(primary); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn simultaneous_launches_all_land_in_one_instance() { + let dir = temp_dir("race"); + let (tx, rx) = channel(); + for i in 0..6 { + let (dir, tx) = (dir.clone(), tx.clone()); + std::thread::spawn(move || { + let _ = tx.send(claim(&dir, &[format!("file{i}.psd")])); + }); + } + drop(tx); + let (mut primaries, mut forwarded, mut events) = (Vec::new(), 0, Vec::new()); + let mut ui: Option = None; + let deadline = Instant::now() + Duration::from_secs(10); + while primaries.len() + forwarded < 6 && Instant::now() < deadline { + while let Ok(launch) = rx.try_recv() { + match launch { + Launch::Primary(p) => primaries.push(p), + Launch::Forwarded => forwarded += 1, + Launch::Standalone(reason) => panic!("standalone: {reason}"), + } + } + if ui.is_none() + && let Some(p) = primaries.first() + { + ui = Some(p.connect(&egui::Context::default())); + } + if let Some(ui) = ui.as_mut() { + events.extend(ui()); + } + std::thread::sleep(Duration::from_millis(10)); + } + assert_eq!((primaries.len(), forwarded, events.len()), (1, 5, 5)); + drop(primaries); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn a_crashed_instance_leaves_nothing_in_the_way() { + let dir = temp_dir("stale"); + std::fs::create_dir_all(&dir).unwrap(); + // instance.json from a process that is gone; the lock is free. + std::fs::write(dir.join(INFO_FILE), r#"{"port":1,"token":"x","pid":1}"#).unwrap(); + let primary = primary(&dir); + assert!(read_info(&dir).is_some_and(|(endpoint, _)| endpoint != Endpoint::Port(1))); + drop(primary); + // Released with the handle, as on process exit. + assert!(matches!(claim_with(&dir, &[], FAST), Launch::Primary(_))); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Review of #3755, point 1 (and the quitting half of point 3): a launch waiting on a primary +/// that shuts down (closing, or about to relaunch itself in CPU mode) isn't told `ok`; it takes +/// the lock over and opens the file itself. +#[test] +fn a_launch_waiting_on_a_closing_instance_takes_over() { + let dir = temp_dir("closing"); + let primary = primary(&dir); + // The UI never drains (it is shutting down, or never came up). + let waiting = launch(&dir, &["a.psd"], FAST); + std::thread::sleep(Duration::from_millis(200)); + primary.shutdown(); + assert!(matches!(waiting.join().unwrap(), Launch::Primary(_)), "the waiting launch becomes the primary"); + // After shutdown new connections are refused too. + let (endpoint, token) = read_info(&dir).unwrap(); + drop(primary); + let short = Instant::now() + Duration::from_millis(300); + assert!(send(&endpoint, &token, &["b.psd".into()], short).is_err()); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Review of #3755, point 3: a primary whose UI never takes the paths (hung) doesn't +/// acknowledge them, and doesn't keep them queued to open later as well. +#[test] +fn a_hung_instance_never_swallows_a_file() { + let dir = temp_dir("hung-ui"); + let primary = primary(&dir); + let mut ui = primary.connect(&egui::Context::default()); + let started = Instant::now(); + assert!(matches!(claim_with(&dir, &["a.psd".into()], FAST), Launch::Standalone(_))); + assert!(started.elapsed() < FAST.handoff + Duration::from_secs(1), "{:?}", started.elapsed()); + assert!(ui().is_empty(), "withdrawn, not opened later"); + drop(primary); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn requests_without_the_token_or_malformed_are_rejected() { + let token = generate_token().unwrap(); + let ok = parse_request(&json!({"token": token, "open": ["a"], "wait_ms": 250}).to_string(), &token); + assert_eq!(ok, Some((vec!["a".to_string()], Duration::from_millis(250)))); + let wrong = generate_token().unwrap(); + assert_eq!(parse_request(&json!({"token": wrong, "open": ["a"]}).to_string(), &token), None); + for bad in ["", "not json", "{}", r#"{"token":5}"#, &json!({"token": token}).to_string(), &json!({"token": token, "open": [1]}).to_string()] { + assert_eq!(parse_request(bad, &token), None, "{bad:?}"); + } + let many: Vec = (0..=MAX_PATHS).map(|i| i.to_string()).collect(); + assert_eq!(parse_request(&json!({"token": token, "open": many}).to_string(), &token), None); +} + +#[test] +fn a_stalled_client_does_not_hold_up_the_next_launch() { + let dir = temp_dir("stalled"); + let primary = primary(&dir); + let mut ui = primary.connect(&egui::Context::default()); + let port = port_of(&dir); + // Connects and never sends a line (each would otherwise hold the listener). + let stalled: Vec = (0..3).map(|_| TcpStream::connect((Ipv4Addr::LOCALHOST, port)).unwrap()).collect(); + let started = Instant::now(); + let next = launch(&dir, &["b.psd"], FAST); + assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(vec!["b.psd".into()])]); + assert!(matches!(next.join().unwrap(), Launch::Forwarded)); + assert!(started.elapsed() < FAST.request, "took {:?}", started.elapsed()); + drop(stalled); + drop(primary); + let _ = std::fs::remove_dir_all(&dir); +} + +/// A client that trickles a byte at a time can't keep its slot past the request deadline. +#[test] +fn a_trickling_client_is_cut_off_at_the_request_deadline() { + let dir = temp_dir("trickle"); + let primary = primary(&dir); + let port = port_of(&dir); + let mut slow = TcpStream::connect((Ipv4Addr::LOCALHOST, port)).unwrap(); + let started = Instant::now(); + let mut cut_off = false; + while started.elapsed() < FAST.request * 4 { + if slow.write_all(b"{").is_err() { + cut_off = true; + break; + } + std::thread::sleep(Duration::from_millis(100)); + } + assert!(cut_off, "still connected after {:?}", started.elapsed()); + assert!(started.elapsed() < FAST.request * 3, "{:?}", started.elapsed()); + drop(primary); + let _ = std::fs::remove_dir_all(&dir); +} + +#[cfg(unix)] +#[test] +fn the_token_file_is_private_to_the_user() { + use std::os::unix::fs::PermissionsExt; + let dir = temp_dir("mode"); + std::fs::create_dir_all(&dir).unwrap(); + // A wide leftover temporary file from a crashed start doesn't widen the new one. + let tmp = dir.join(format!("{INFO_FILE}.{}.tmp", std::process::id())); + std::fs::write(&tmp, "{}").unwrap(); + std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o644)).unwrap(); + write_info(&dir, &Endpoint::Port(1), &generate_token().unwrap()).unwrap(); + let mode = std::fs::metadata(dir.join(INFO_FILE)).unwrap().permissions().mode() & 0o777; + assert_eq!(mode, 0o600); + let _ = std::fs::remove_dir_all(&dir); +} + +#[test] +fn an_unreachable_instance_falls_back_to_a_standalone_launch() { + let dir = temp_dir("unreachable"); + std::fs::create_dir_all(&dir).unwrap(); + // Someone holds the lock but nothing listens on the advertised port. + let held = File::create(dir.join(LOCK_FILE)).unwrap(); + held.lock().unwrap(); + let closed = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap().local_addr().unwrap().port(); + write_info(&dir, &Endpoint::Port(closed), &generate_token().unwrap()).unwrap(); + assert!(matches!(claim_with(&dir, &["a.psd".into()], FAST), Launch::Standalone(_))); + drop(held); + let _ = std::fs::remove_dir_all(&dir); +} + +/// Review of #3755, point 2 and the smaller points: who takes part in single-instance. +#[test] +fn only_per_user_launches_without_process_options_take_part() { + let base = LaunchContext { + per_user_settings: true, + new_instance_flag: false, + new_instance_env: None, + control: false, + unreadable_paths: false, + launch_options: &[], + }; + assert_eq!(skip_reason(&base), None); + // A portable copy's folder or a PHOTOCRAFT_CONFIG_DIR may be shared between users. + assert!(skip_reason(&LaunchContext { per_user_settings: false, ..base }).is_some()); + assert!(skip_reason(&LaunchContext { new_instance_flag: true, ..base }).is_some()); + assert!(skip_reason(&LaunchContext { new_instance_env: Some("1"), ..base }).is_some()); + assert_eq!(skip_reason(&LaunchContext { new_instance_env: Some("0"), ..base }), None); + assert_eq!(skip_reason(&LaunchContext { new_instance_env: Some(""), ..base }), None); + assert!(skip_reason(&LaunchContext { control: true, ..base }).is_some()); + assert!(skip_reason(&LaunchContext { unreadable_paths: true, ..base }).is_some()); + let reason = skip_reason(&LaunchContext { launch_options: &[PerProcess::SafeGpu, PerProcess::GpuAdapterEnv], ..base }); + assert_eq!(reason.as_deref(), Some("--safe-gpu, PHOTOCRAFT_GPU_ADAPTER only applies to a new process")); +} + +/// #1540: inside a Flatpak sandbox the hand-off runs over a Unix socket in the app's runtime +/// directory, which every sandbox of the app shares, with the same lock, token and reply. +#[cfg(unix)] +#[test] +fn a_sandboxed_second_launch_hands_its_paths_over_a_unix_socket() { + use std::os::unix::fs::PermissionsExt; + let dir = temp_dir("socket"); + // Short: a Unix socket path is limited to about 100 bytes (macOS: 104). + let socket = short_socket_path("a"); + let transport = Transport::Socket(socket.clone()); + let primary = match claim_in(&dir, &[], FAST, &transport) { + Launch::Primary(p) => p, + _ => panic!("first launch should be primary"), + }; + assert_eq!(std::fs::metadata(&socket).unwrap().permissions().mode() & 0o777, 0o600); + let info: Value = serde_json::from_slice(&std::fs::read(dir.join(INFO_FILE)).unwrap()).unwrap(); + assert_eq!(info.get("socket").and_then(Value::as_str), Some(socket.to_str().unwrap())); + assert!(info.get("port").is_none()); + let mut ui = primary.connect(&egui::Context::default()); + let paths = ["/home/a/one.psd".to_string()]; + let (d, p, t) = (dir.clone(), paths.clone(), transport.clone()); + let second = std::thread::spawn(move || claim_in(&d, &p, TIMING, &t)); + assert_eq!(drain(&mut ui, 1), vec![OsEvent::Open(paths.to_vec())]); + assert!(matches!(second.join().unwrap(), Launch::Forwarded)); + // The token still gates the socket. + let (endpoint, _) = read_info(&dir).unwrap(); + assert!(send(&endpoint, "not the token", &["x.psd".into()], Instant::now() + FAST.handoff).is_err()); + primary.shutdown(); + assert!(!socket.exists(), "shutdown removes the socket file"); + // The next primary binds the same path; the old one's drop (a second shutdown) must leave it. + let next = match claim_in(&dir, &[], FAST, &transport) { + Launch::Primary(p) => p, + _ => panic!("the lock is free again"), + }; + drop(primary); + assert!(socket.exists(), "a second shutdown doesn't remove the next primary's socket"); + drop(next); + let _ = std::fs::remove_dir_all(&dir); +} + +/// A socket file left by a crashed primary (and a stale `instance.json` naming it) is replaced. +#[cfg(unix)] +#[test] +fn a_crashed_primary_s_socket_file_does_not_block_the_next_one() { + use std::os::unix::fs::FileTypeExt; + let dir = temp_dir("stale-socket"); + let socket = short_socket_path("b"); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::create_dir_all(socket.parent().unwrap()).unwrap(); + std::fs::write(&socket, b"not a socket").unwrap(); + write_info(&dir, &Endpoint::Socket(socket.clone()), &generate_token().unwrap()).unwrap(); + let primary = match claim_in(&dir, &["a.psd".into()], FAST, &Transport::Socket(socket.clone())) { + Launch::Primary(p) => p, + Launch::Forwarded => panic!("nothing was listening"), + Launch::Standalone(e) => panic!("{e}"), + }; + assert!(std::fs::metadata(&socket).unwrap().file_type().is_socket()); + drop(primary); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_dir_all(socket.parent().unwrap()); +} + +/// The socket transport is chosen only inside a Flatpak with a runtime directory. +#[cfg(unix)] +#[test] +fn the_socket_transport_needs_a_flatpak_id_and_a_runtime_directory() { + use std::ffi::OsStr; + let (id, run) = (OsStr::new("ai.storyteller.photocraft"), OsStr::new("/run/user/1000")); + assert_eq!(sandbox_socket(Some(id), Some(run)), Some(PathBuf::from("/run/user/1000/app/ai.storyteller.photocraft/instance.sock"))); + assert_eq!(sandbox_socket(None, Some(run)), None); + assert_eq!(sandbox_socket(Some(id), None), None); + assert_eq!(sandbox_socket(Some(OsStr::new("")), Some(run)), None); + assert_eq!(sandbox_socket(Some(id), Some(OsStr::new(""))), None); +} + +/// A variable documented as the equivalent of a per-launch flag counts as that flag: a launch +/// that sets only variables has per-process options, so it keeps its own process rather than +/// handing its files to the running instance, which never sees the override. +#[test] +fn an_environment_override_counts_as_a_process_option() { + let with = |values: &[(&str, &str)]| env_launch_options_from(|n| values.iter().find(|(k, _)| *k == n).map(|(_, v)| (*v).into())); + assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), vec![PerProcess::PresentMode]); + assert_eq!(with(&[("PHOTOCRAFT_STROKE_TRACE", "strokes.csv"), ("PHOTOCRAFT_FRAME_LATENCY", "2")]), vec![PerProcess::FrameLatency, PerProcess::StrokeTrace]); + // Nothing set, or a value that means off, is an ordinary launch. Each variable is off the way + // it is read where it takes effect, not by one rule shared across all three. + assert!(with(&[]).is_empty()); + assert!(with(&[("PHOTOCRAFT_FRAME_LATENCY", "")]).is_empty()); + // The trace is off at `off` and `false` as well, and a value the presentation parser rejects + // is ignored there, so neither keeps a process of its own here. + assert!(with(&[("PHOTOCRAFT_STROKE_TRACE", "off"), ("PHOTOCRAFT_PRESENT_MODE", "maybe"), ("PHOTOCRAFT_FRAME_LATENCY", "4")]).is_empty()); + // The trace and the two presentation values are trimmed where they take effect. + assert_eq!(with(&[("PHOTOCRAFT_PRESENT_MODE", " novsync "), ("PHOTOCRAFT_STROKE_TRACE", " 1 ")]), vec![PerProcess::PresentMode, PerProcess::StrokeTrace]); + // The menus and the automation roots are flags' fallbacks: `launch_args::parse` counts them. + assert!(with(&[("PHOTOCRAFT_IN_WINDOW_MENUS", "1"), ("PHOTOCRAFT_AUTOMATION_WRITE_ROOT", "/tmp/out")]).is_empty()); + let base = LaunchContext { + per_user_settings: true, + new_instance_flag: false, + new_instance_env: None, + control: false, + unreadable_paths: false, + launch_options: &[], + }; + let reason = skip_reason(&LaunchContext { launch_options: &with(&[("PHOTOCRAFT_PRESENT_MODE", "novsync")]), ..base }); + assert!(reason.as_deref().is_some_and(|r| r.contains("--present-mode")), "{reason:?}"); +} diff --git a/apps/photocraft/src/startup_report.rs b/apps/photocraft/src/startup_report.rs new file mode 100644 index 0000000000..554b99dea0 --- /dev/null +++ b/apps/photocraft/src/startup_report.rs @@ -0,0 +1,104 @@ +//! Telling the user why a launch failed when there is no window to say it in (#7643). +//! +//! A desktop launch has no console — release builds are GUI-subsystem apps on Windows — so the +//! `Err` that ends `main` used to vanish and PhotoCraft "just closed". This shows one native +//! dialog with the error, the renderer that was tried and the log file to attach. The text is +//! English diagnostics, like Help › System Info, so a bug report reads the same in every language. +//! +//! Terminal and automation launches keep the old behaviour: they can read standard error or the +//! exit status, and a modal dialog would leave a script waiting. + +use std::io::IsTerminal; +use std::path::Path; + +/// Whether a failed launch needs the dialog: no terminal under standard error, no control client +/// (an agent launch reads the exit status instead of clicking OK), and a screen to put it on. +pub fn dialog_wanted(stderr_is_terminal: bool, automation: bool, display: bool) -> bool { + !stderr_is_terminal && !automation && display +} + +/// Whether this launch asks for the dialog (see [`dialog_wanted`]). +pub fn needs_dialog(automation: bool) -> bool { + dialog_wanted(std::io::stderr().is_terminal(), automation, has_display()) +} + +/// Whether there is a screen for a dialog: always on Windows and macOS; on Linux a headless box +/// (a CI runner, an SSH session — `startup_without_display` removes both variables) has none. +fn has_display() -> bool { + #[cfg(target_os = "linux")] + { + ["DISPLAY", "WAYLAND_DISPLAY"].iter().any(|key| std::env::var_os(key).is_some_and(|v| !v.is_empty())) + } + #[cfg(not(target_os = "linux"))] + { + true + } +} + +/// The dialog's text: the error, the renderer that was tried and why it wasn't the preference, +/// the version and, when one was opened, the log file. +pub fn text(error: &str, renderer: &str, note: Option<&str>, version: &str, log: Option<&Path>) -> String { + let mut text = format!("PhotoCraft could not start.\n\n{error}\n\nRenderer: {renderer}"); + if let Some(note) = note { + text.push_str(&format!("\nStartup: {note}")); + } + text.push_str(&format!("\nVersion: {version}")); + if let Some(path) = log { + text.push_str(&format!("\nLog file: {}", path.display())); + } + text +} + +/// Show [`text`] in a native error dialog. Called after the event loop returned, so nothing is +/// repainting and the launch was about to fail anyway. +pub fn show(error: &str, renderer: &str, note: Option<&str>, log: Option<&Path>) { + let version = photocraft_engine::build_info::long_version(); + let body = text(error, renderer, note, &version, log); + log::info!("showing the startup-failure dialog"); + let _ = rfd::MessageDialog::new() + .set_level(rfd::MessageLevel::Error) + .set_title(photocraft_ui_egui::i18n::t("PhotoCraft could not start")) + .set_description(body) + .set_buttons(rfd::MessageButtons::Ok) + .show(); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_desktop_launch_asks_the_user_and_a_script_does_not() { + assert!(dialog_wanted(false, false, true), "no console: the error would vanish"); + assert!(!dialog_wanted(true, false, true), "a terminal shows the error itself"); + assert!(!dialog_wanted(false, true, true), "an automation client reads the exit status"); + assert!(!dialog_wanted(false, false, false), "a headless launch has no screen for a dialog"); + } + + #[test] + fn the_text_names_the_error_renderer_version_and_log() { + let log = Path::new("/home/me/.config/photocraft/logs/photocraft.log"); + let body = text("no adapter for Vulkan", "dx12", None, "0.15.1 (abc1234)", Some(log)); + for part in [ + "could not start", + "no adapter for Vulkan", + "Renderer: dx12", + "Version: 0.15.1 (abc1234)", + "Log file: /home/me/.config/photocraft/logs/photocraft.log", + ] { + assert!(body.contains(part), "{part:?} missing from {body:?}"); + } + assert!(!body.contains("Startup:"), "no fallback to explain: {body:?}"); + // A start whose log file could not be created says so by leaving the line out. + assert!(!text("boom", "cpu", None, "0.15.1", None).contains("Log file:")); + } + + /// A crash fallback is the confusing case: the app picks a renderer the user never chose, so + /// the dialog has to say why as well as which. + #[test] + fn a_fallback_that_was_not_the_preference_is_explained() { + let note = "the previous start didn't finish on vulkan; using dx12"; + let body = text("device lost", "dx12", Some(note), "0.15.1", None); + assert!(body.contains(&format!("Renderer: dx12\nStartup: {note}\n")), "{body:?}"); + } +} diff --git a/apps/photocraft/src/tablet.rs b/apps/photocraft/src/tablet.rs index fc063d2fbc..19ba28996c 100644 --- a/apps/photocraft/src/tablet.rs +++ b/apps/photocraft/src/tablet.rs @@ -1,10 +1,12 @@ -//! Pen tablet pressure, tilt, rotation and eraser end on macOS and Linux X11 (issue #79). +//! Pen tablet pressure, tilt, rotation and eraser end on macOS and Linux X11 (issue #79), and +//! barrel button on Windows (#5905). //! -//! winit 0.30 drops tablet data on both, so `photocraft-tablet` reads it beside winit (an AppKit -//! local event monitor; XInput2 raw events on a second X connection) and this module writes each -//! sample into the UI's [`StylusFeed`], where the canvas reads it exactly like the web runner's -//! Pointer Events and automation's simulated pen. Windows needs nothing here: winit forwards -//! `WM_POINTER` pressure as touch force. +//! winit 0.30 drops tablet data on both macOS and X11, so `photocraft-tablet` reads it beside winit +//! (an AppKit local event monitor; XInput2 raw events on a second X connection) and this module writes +//! each sample into the UI's [`StylusFeed`], where the canvas reads it exactly like the web runner's +//! Pointer Events and automation's simulated pen. On Windows, winit forwards `WM_POINTER` pressure +//! as touch force, but drops `PEN_FLAG_BARREL` (`0x0001`); `photocraft-tablet` recovers the barrel +//! button through a thread-local message hook beside winit. //! //! Wayland compositors give a pen only to clients that bind the tablet protocol //! (`zwp_tablet_v2`), which winit 0.30 doesn't (and binding it on winit's connection needs @@ -19,15 +21,36 @@ use photocraft_tablet::Sample; use photocraft_ui_egui::stylus::{PenSample, StylusFeed}; /// A tablet sample as the UI's pen sample. +#[allow(dead_code)] pub fn pen_sample(s: Sample) -> PenSample { PenSample { pressure: s.pressure, tilt_x: s.tilt_x, tilt_y: s.tilt_y, rotation: s.rotation, eraser: s.eraser }.sanitized() } /// Writes tablet samples into `feed` (`None` = a mouse). +#[allow(dead_code)] pub fn sink(feed: StylusFeed) -> impl Fn(Option) + Send + 'static { move |s| feed.set(s.map(pen_sample)) } +/// Install the Windows pointer message hook (main thread; keep the result until the event loop +/// returns). On failure this logs why: the pen barrel button is then missed. +#[cfg(target_os = "windows")] +pub fn install_windows() -> Option { + photocraft_tablet::windows::Monitor::install().map_err(|e| log::warn!("{e}")).ok() +} + +/// Stylus barrel button down check (Windows). The right mouse button is left to egui, which +/// reports the logical secondary button (swapped buttons included) for the app's own window only. +#[cfg(target_os = "windows")] +pub fn right_button_service() -> Option { + Some(Box::new(photocraft_tablet::windows::is_barrel_button_down)) +} + +#[cfg(not(target_os = "windows"))] +pub fn right_button_service() -> Option { + None +} + /// Install the AppKit tablet monitor (main thread, once winit created the shared application: /// from eframe's app creation, not before the event loop). Keep the result until the event loop /// returns. On failure this logs why: pens then paint like a mouse. diff --git a/apps/photocraft/src/ui_state.rs b/apps/photocraft/src/ui_state.rs index c4de67a994..b356fb26c2 100644 --- a/apps/photocraft/src/ui_state.rs +++ b/apps/photocraft/src/ui_state.rs @@ -6,16 +6,32 @@ //! hand-edited file would then crash every launch. [`sanitize`] runs first and removes a file //! holding such values (the window and panels open at their defaults; nothing else is lost). //! A file that isn't RON at all is left alone: eframe already ignores it. +//! +//! egui 0.37 also replaced the flat `WindowSettings` (`inner_position_pixels`, +//! `inner_size_points: Option`, …) with `normal_geometry: { outer_position_pixels, +//! scale_factor, inner_size_points }`. eframe reads the file before any application code, so an +//! old value cannot be interpreted lazily: [`sanitize`] rewrites it before eframe loads it and +//! keeps a one-time `ui.ron.pre-0.37` backup. The old panel keys in the egui memory part are +//! untouched (`panels::panel_state_id` migrates them additively), but this window rewrite is +//! one-way: after it, a 0.36 build ignores the window entry and opens at its default geometry. +//! Restoring the backup is the documented rollback. -use std::path::Path; +use std::{collections::HashMap, path::Path}; /// Window positions, sizes and panel rects beyond this many points or pixels are corrupt. const MAX_COORD: f64 = 1.0e6; /// egui's zoom factor is the UI scale over the display scale (75% on a 2× display is 0.375, /// 300% on a 1× display is 3). const ZOOM: std::ops::RangeInclusive = 0.1..=10.0; +/// The size [`crate::native_options`] opens a window with when an old save has no usable size. +const DEFAULT_SIZE: f32 = 1440.0; +const DEFAULT_HEIGHT: f32 = 900.0; +/// Kept beside `ui.ron` before the one-way 0.36 → 0.37 window rewrite. +const BACKUP_SUFFIX: &str = "pre-0.37"; -/// Remove `path` if it holds state that would crash startup. Never fails: errors are logged. +/// Remove `path` if it holds state that would crash startup, and migrate an old window entry to +/// the 0.37 schema before eframe reads it. Never fails: errors are logged and leave the file +/// alone (eframe then ignores what it cannot read). pub fn sanitize(path: Option<&Path>) { let Some(path) = path else { return }; let Ok(text) = std::fs::read_to_string(path) else { return }; @@ -24,30 +40,72 @@ pub fn sanitize(path: Option<&Path>) { if let Err(e) = std::fs::remove_file(path) { log::warn!("could not remove {}: {e}", path.display()); } + return; + } + match migrate_window(text) { + Ok(Some(text)) => write_migrated(path, &text), + Ok(None) => {} + Err(why) => log::warn!("not rewriting the saved window geometry in {}: {why}", path.display()), } } -/// Whether `path` holds a window position from an earlier session, which eframe restores. Then -/// `NativeOptions::centered` must be off: eframe applies it after restoring the saved geometry, +/// Write the migrated file, keeping a one-time backup. A failed backup aborts the rewrite: the +/// pre-0.37 window geometry is the documented rollback, so never destroy the only copy. +fn write_migrated(path: &Path, text: &str) { + let backup = backup_path(path); + if !backup.exists() + && let Err(e) = std::fs::copy(path, &backup) + { + log::warn!("could not back up {} to {}: {e}; leaving it unchanged", path.display(), backup.display()); + return; + } + if let Err(e) = std::fs::write(path, text) { + log::warn!("could not write the migrated window layout to {}: {e}", path.display()); + } +} + +fn backup_path(path: &Path) -> std::path::PathBuf { + let mut name = path.file_name().map(std::ffi::OsStr::to_os_string).unwrap_or_else(|| "ui.ron".into()); + name.push(format!(".{BACKUP_SUFFIX}")); + path.with_file_name(name) +} + +/// Whether `path` holds a window position that eframe restores, which means +/// `NativeOptions::centered` must be off: eframe applies `centered` after the restored geometry, /// so it moved the window back to the centre of the main monitor on every launch (#4826). +/// +/// Only the 0.37 `normal_geometry.outer_position_pixels` counts: `egui_winit::WindowGeometry` +/// sets it and `initialize_viewport_builder` applies it. A 0.36 flat entry is not restored by +/// 0.37 (`sanitize` rewrites it first), and its macOS `inner_position_pixels` was never an outer +/// position. pub fn has_saved_position(path: Option<&Path>) -> bool { let Some(text) = path.and_then(|p| std::fs::read_to_string(p).ok()) else { return false }; saved_position(&text) } fn saved_position(text: &str) -> bool { - let Ok(kv) = ron::from_str::>(text) else { return false }; + let Ok(kv) = ron::from_str::>(text) else { return false }; let Some(window) = kv.get("window").and_then(|w| ron::from_str::(w).ok()) else { return false }; - // eframe restores the inner position on macOS and the outer position elsewhere - // (`egui_winit::WindowSettings::initialize_viewport_builder`). - let key = if cfg!(target_os = "macos") { "inner_position_pixels" } else { "outer_position_pixels" }; - matches!(field(&window, key), Some(ron::Value::Option(Some(_)))) + let Some(geometry) = field(&window, "normal_geometry") else { return false }; + matches!(field(geometry, "outer_position_pixels"), Some(ron::Value::Option(Some(_)))) +} + +/// The 0.36 → 0.37 rewrite of one `ui.ron` text, for the recorded 0.14/0.15 fixtures. +#[cfg(test)] +pub(crate) fn migrate_for_test(text: &str) -> Result, String> { + migrate_window(text.to_owned()) +} + +/// [`saved_position`] for the recorded fixtures. +#[cfg(test)] +pub(crate) fn saved_position_for_test(text: &str) -> bool { + saved_position(text) } /// `Err` names the first value that would crash startup. fn check(text: &str) -> Result<(), String> { // eframe stores a map of key → RON string; a file that doesn't parse is ignored by eframe. - let Ok(kv) = ron::from_str::>(text) else { return Ok(()) }; + let Ok(kv) = ron::from_str::>(text) else { return Ok(()) }; for (key, value) in &kv { // eframe also skips an entry that doesn't parse. let Ok(v) = ron::from_str::(value) else { continue }; @@ -60,6 +118,108 @@ fn check(text: &str) -> Result<(), String> { Ok(()) } +/// `Some(new_text)` when `text` is an old-schema file that should be rewritten; `None` when it +/// is already 0.37-shaped or holds no window entry; `Err` when the old window entry is unusable +/// and must be left untouched. +fn migrate_window(text: String) -> Result, String> { + let Ok(mut kv) = ron::from_str::>(&text) else { return Ok(None) }; + let Some(value) = kv.get("window").cloned() else { return Ok(None) }; + let Ok(window) = ron::from_str::(&value) else { return Ok(None) }; + if field(&window, "normal_geometry").is_some() { + return Ok(None); + } + if field(&window, "inner_size_points").is_none() && field(&window, "inner_position_pixels").is_none() && field(&window, "outer_position_pixels").is_none() { + return Ok(None); + } + let migrated = migrate_window_value(&window)?; + kv.insert("window".into(), migrated); + ron::to_string(&kv).map(Some).map_err(|e| format!("cannot write the new window entry: {e}")) +} + +/// The RON payload eframe 0.37 deserializes as `egui_winit::WindowSettings`. +/// +/// The field order and names mirror `WindowGeometry`; the types are local because +/// `egui_winit::WindowGeometry` keeps its fields private. +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq)] +struct PointV37 { + x: f32, + y: f32, +} + +impl PointV37 { + fn sane(self) -> Option { + let ok = |v: f32| v.is_finite() && (v.abs() as f64) <= MAX_COORD; + (ok(self.x) && ok(self.y)).then_some(self) + } +} + +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq)] +struct GeometryV37 { + outer_position_pixels: Option, + scale_factor: f32, + inner_size_points: PointV37, +} + +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, Copy, PartialEq)] +struct WindowV37 { + normal_geometry: GeometryV37, + fullscreen: bool, + maximized: bool, +} + +fn migrate_window_value(window: &ron::Value) -> Result { + let size = old_size(window)?; + // The old macOS entry stored an inner (not outer) position in logical points and Linux saved + // no scale factor, so a faithful restore is only possible for the Windows physical outer + // position. Everywhere else the window opens centred with its saved size. + let position = if cfg!(target_os = "windows") { old_position(window, "outer_position_pixels") } else { None }; + let window = WindowV37 { + normal_geometry: GeometryV37 { + outer_position_pixels: position, + // Physical pixels of the old Windows position: 0.37 divides by zoom × scale. + scale_factor: 1.0, + inner_size_points: size, + }, + fullscreen: old_bool(window, "fullscreen"), + maximized: old_bool(window, "maximized"), + }; + ron::to_string(&window).map_err(|e| format!("cannot serialize the new window entry: {e}")) +} + +fn old_size(window: &ron::Value) -> Result { + let value = field(window, "inner_size_points"); + let Some(ron::Value::Option(Some(size))) = value else { + // A missing size is the app's own opening size, not corrupt data. + return Ok(PointV37 { x: DEFAULT_SIZE, y: DEFAULT_HEIGHT }); + }; + let point = point_value(size).ok_or_else(|| "inner_size_points is not a finite (x, y) point".to_string())?; + let point = point.sane().ok_or_else(|| "inner_size_points is not finite or is absurdly large".to_string())?; + if point.x < 1.0 || point.y < 1.0 { + return Err("inner_size_points has a zero or negative side".into()); + } + Ok(point) +} + +/// One dimension of an old `Option` field, rejecting corrupt values rather than guessing. +fn old_position(window: &ron::Value, key: &str) -> Option { + match field(window, key) { + Some(ron::Value::Option(Some(p))) => point_value(p).and_then(PointV37::sane), + // `None`, missing, and malformed all mean "no position"; corrupt positions elsewhere are + // caught by `check`, which removes a file with non-finite values before this runs. + _ => None, + } +} + +fn old_bool(window: &ron::Value, key: &str) -> bool { + matches!(field(window, key), Some(ron::Value::Bool(true))) +} + +fn point_value(v: &ron::Value) -> Option { + let x = field(v, "x").and_then(as_f32)?; + let y = field(v, "y").and_then(as_f32)?; + Some(PointV37 { x, y }) +} + /// Every float finite and within [`MAX_COORD`]; depth-limited (the file is untrusted). fn floats_sane(v: &ron::Value, depth: usize) -> Result<(), String> { if depth > 64 { @@ -86,6 +246,13 @@ fn as_f64(v: &ron::Value) -> Option { } } +fn as_f32(v: &ron::Value) -> Option { + as_f64(v).and_then(|f| { + let f = f as f32; + f.is_finite().then_some(f) + }) +} + fn field<'a>(v: &'a ron::Value, name: &str) -> Option<&'a ron::Value> { match v { ron::Value::Map(m) => m.get(&ron::Value::String(name.into())), @@ -95,7 +262,8 @@ fn field<'a>(v: &'a ron::Value, name: &str) -> Option<&'a ron::Value> { #[cfg(test)] mod tests { - use super::{check, saved_position}; + use super::{DEFAULT_HEIGHT, DEFAULT_SIZE, WindowV37, backup_path, check, migrate_window, saved_position}; + use std::{collections::HashMap, path::PathBuf}; fn file(window: &str, zoom: &str) -> String { let egui = format!("(options:(zoom_factor:{zoom},max_passes:2),data:([(18446744073709551615,(ron:\"(x:1.0)\"))]))"); @@ -103,6 +271,17 @@ mod tests { } const WINDOW: &str = "(inner_position_pixels:Some((x:616.0,y:430.0)),fullscreen:false,inner_size_points:Some((x:1100.0,y:760.0)))"; + const WINDOW_WITH_OUTER: &str = "(inner_position_pixels:Some((x:2048.0,y:1265.0)),outer_position_pixels:Some((x:2048.0,y:1228.0)),fullscreen:false,maximized:false,inner_size_points:Some((x:880.0,y:480.0)))"; + + /// Read the window payload back as eframe 0.37 would. + fn window_of(text: &str) -> WindowV37 { + let kv: HashMap = ron::from_str(text).expect("the migrated file is a RON map"); + ron::from_str(kv.get("window").expect("a window entry")).expect("0.37 WindowSettings shape") + } + + fn migrated(window: &str) -> String { + migrate_window(file(window, "1.0")).expect("migratable").expect("rewritten") + } #[test] fn a_saved_layout_is_kept() { @@ -122,13 +301,83 @@ mod tests { } } + #[test] + fn an_old_size_and_maximized_state_survive() { + let text = migrated(WINDOW_WITH_OUTER); + let window = window_of(&text); + assert_eq!(window.normal_geometry.inner_size_points.x, 880.0); + assert_eq!(window.normal_geometry.inner_size_points.y, 480.0); + assert!(!window.fullscreen); + assert!(!window.maximized); + let maximized = migrated(&WINDOW.replace("fullscreen:false", "fullscreen:true,maximized:true")); + assert!(window_of(&maximized).fullscreen && window_of(&maximized).maximized); + } + + #[test] + fn a_missing_size_uses_the_default_opening_size() { + // A window entry with a position but no size migrates and keeps the app's opening size. + let window = window_of(&migrated("(inner_position_pixels:Some((x:616.0,y:430.0)),fullscreen:false,maximized:false)")); + assert_eq!(window.normal_geometry.inner_size_points.x, DEFAULT_SIZE); + assert_eq!(window.normal_geometry.inner_size_points.y, DEFAULT_HEIGHT); + // A window entry with no geometry fields at all is left alone: it is not a 0.36 window. + assert_eq!(migrate_window(file("(fullscreen:false,maximized:false)", "1.0")), Ok(None)); + // So is a file with no window-like fields at all. + assert_eq!(migrate_window(file("(something:1)", "1.0")), Ok(None)); + } + + #[test] + fn the_old_position_is_kept_only_on_windows() { + let text = migrated(WINDOW_WITH_OUTER); + let pos = window_of(&text).normal_geometry.outer_position_pixels; + if cfg!(target_os = "windows") { + let pos = pos.expect("the Windows physical outer position survives"); + assert_eq!((pos.x, pos.y), (2048.0, 1228.0)); + assert_eq!(window_of(&text).normal_geometry.scale_factor, 1.0); + } else { + assert!(pos.is_none(), "an old macOS/Linux position is not a restorable outer position"); + } + } + + #[test] + fn a_corrupt_old_size_is_not_rewritten() { + let bad = WINDOW.replace("(x:1100.0,y:760.0)", "(x:1100.0,y:0.0)"); + assert!(migrate_window(file(&bad, "1.0")).is_err()); + let huge = WINDOW.replace("(x:1100.0,y:760.0)", "(x:1100.0,y:9000000.0)"); + assert!(migrate_window(file(&huge, "1.0")).is_err()); + } + + #[test] + fn an_already_migrated_file_is_left_alone() { + let text = migrated(WINDOW); + assert_eq!(migrate_window(text), Ok(None)); + // A 0.36 build accepts the entry, but its missing flat geometry defaults to None. + // It cannot recover position or size from the nested 0.37 geometry. + let kv: HashMap = ron::from_str(&migrated(WINDOW)).unwrap(); + let old: OldWindow = ron::from_str(kv.get("window").unwrap()).unwrap(); + assert!(old.inner_position_pixels.is_none() && old.outer_position_pixels.is_none()); + assert!(old.inner_size_points.is_none(), "documented rollback loss of saved geometry"); + // Restoring the original backup restores both the old position and size. + let restored: OldWindow = ron::from_str(WINDOW).unwrap(); + let position = restored.inner_position_pixels.unwrap(); + let size = restored.inner_size_points.unwrap(); + assert_eq!((position.x, position.y), (616.0, 430.0)); + assert_eq!((size.x, size.y), (1100.0, 760.0)); + } + #[test] fn a_saved_window_position_is_detected() { - // #4826: the position from the report (Linux Mint, window moved to the third monitor). - let window = "(inner_position_pixels:Some((x:2048.0,y:1265.0)),outer_position_pixels:Some((x:2048.0,y:1228.0)),fullscreen:false,maximized:false,inner_size_points:Some((x:880.0,y:480.0)))"; - assert!(saved_position(&file(window, "1.0"))); - let no_position = "(inner_position_pixels:None,outer_position_pixels:None,fullscreen:false,maximized:false,inner_size_points:Some((x:880.0,y:480.0)))"; + // A 0.36 flat entry is not what eframe 0.37 restores (it would open at the OS default); + // `sanitize` rewrites it before eframe runs, and the migrated nested value is detected. + assert!(!saved_position(&file(WINDOW_WITH_OUTER, "1.0")), "an unmigrated flat entry is not a restorable position"); + // An old macOS inner position is not an outer position either. + assert!(!saved_position(&file(WINDOW, "1.0"))); + let no_position = + "(normal_geometry:(outer_position_pixels:None,scale_factor:1.0,inner_size_points:(x:880.0,y:480.0)),fullscreen:false,maximized:false)"; assert!(!saved_position(&file(no_position, "1.0"))); + // The migrated nested geometry is what a 0.37 save holds (on Windows only, from the + // migration; a later 0.37 save on macOS/Linux has one too and eframe restores it). + let text = migrated(WINDOW_WITH_OUTER); + assert_eq!(saved_position(&text), cfg!(target_os = "windows")); assert!(!saved_position(&format!("{{\"egui\":{:?}}}", "()")), "no window entry"); for text in ["", "not ron {{{", "{\"window\":\"(((\"}"] { assert!(!saved_position(text), "{text:?}"); @@ -148,7 +397,7 @@ mod tests { } #[test] - fn sanitize_removes_only_a_bad_file() { + fn sanitize_removes_only_a_bad_file_and_migrates_a_good_one() { let dir = std::env::temp_dir().join(format!("photocraft-ui-state-{}", std::process::id())); std::fs::create_dir_all(&dir).unwrap(); let good = dir.join("good.ron"); @@ -159,7 +408,58 @@ mod tests { super::sanitize(Some(&bad)); super::sanitize(Some(&dir.join("missing.ron"))); super::sanitize(None); - assert!(good.exists() && !bad.exists()); + assert!(!bad.exists()); + assert!(good.exists()); + // Migrated in place, with the one-time pre-0.37 backup beside it. + let text = std::fs::read_to_string(&good).unwrap(); + assert!(window_of(&text).normal_geometry.inner_size_points.x == 1100.0); + let backup = backup_path(&good); + assert!(backup.exists(), "the pre-0.37 layout is kept for rollback"); + assert_eq!(std::fs::read_to_string(&backup).unwrap(), file(WINDOW, "1.0")); + // A second run leaves both untouched. + super::sanitize(Some(&good)); + assert_eq!(std::fs::read_to_string(&good).unwrap(), text); let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn the_backup_is_not_overwritten_by_a_later_migration() { + let dir = std::env::temp_dir().join(format!("photocraft-ui-state-backup-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("ui.ron"); + std::fs::write(&path, file(WINDOW, "1.0")).unwrap(); + super::sanitize(Some(&path)); + let backup = backup_path(&path); + let original = std::fs::read_to_string(&backup).unwrap(); + // Pretend a 0.36 run wrote its old schema again; the backup must stay the first one. + std::fs::write(&path, file(&WINDOW.replace("1100.0", "1111.0"), "1.0")).unwrap(); + super::sanitize(Some(&path)); + assert_eq!(std::fs::read_to_string(&backup).unwrap(), original); + assert!(std::fs::read_to_string(&path).unwrap().contains("1111")); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The 0.36 flat schema, kept to prove the new nested value is a one-way migration. + #[derive(Default, serde::Deserialize)] + #[serde(default)] + #[allow(dead_code)] + struct OldWindow { + inner_position_pixels: Option, + outer_position_pixels: Option, + fullscreen: bool, + maximized: bool, + inner_size_points: Option, + } + + #[derive(serde::Deserialize)] + #[allow(dead_code)] + struct Point { + x: f32, + y: f32, + } + + #[test] + fn the_backup_path_sits_beside_ui_ron() { + assert_eq!(backup_path(&PathBuf::from("/tmp/x/ui.ron")), PathBuf::from("/tmp/x/ui.ron.pre-0.37")); + } } diff --git a/apps/photocraft/src/updates.rs b/apps/photocraft/src/updates.rs new file mode 100644 index 0000000000..a11a7b63c9 --- /dev/null +++ b/apps/photocraft/src/updates.rs @@ -0,0 +1,27 @@ +//! Release checks through `craft-update`, the library every Craft App shares. Only public release +//! metadata is requested. + +use photocraft_engine::updates_cmds::{CheckFn, CheckResult, Release}; + +pub fn service() -> CheckFn { + Box::new(|| { + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::Builder::new() + .name("update check".into()) + .spawn(move || { + let result = check(); + if let Err(e) = &result { + log::warn!("Update check: {e}"); + } + let _ = tx.send(result); + }) + .map_err(|e| format!("Could not start update check: {e}"))?; + Ok(rx) + }) +} + +fn check() -> CheckResult { + let http = craft_update::Http::new("PhotoCraft/Update")?; + let found = craft_update::check_for_update(&photocraft_ui_egui::UPDATE_APP, &http, photocraft_engine::build_info::VERSION)?; + Ok(found.map(|release| Release { version: release.tag.trim_start_matches(['v', 'V']).to_string(), url: release.html_url })) +} diff --git a/apps/photocraft/src/window_frame.rs b/apps/photocraft/src/window_frame.rs index e418049d07..56c9be1b6a 100644 --- a/apps/photocraft/src/window_frame.rs +++ b/apps/photocraft/src/window_frame.rs @@ -20,6 +20,11 @@ //! the work area plus its own frame, clear of the taskbar. winit pins the client area to the work //! area either way (`WM_NCCALCSIZE`), so the app's pixels do not move. //! +//! An auto-hidden taskbar reserves no work area, so that trim leaves the maximize covering the +//! whole monitor, and the shell, taking it for a full-screen app, no longer reveals the taskbar +//! when the pointer reaches its edge (#4845). On each edge with an auto-hidden taskbar the guard +//! instead stops the window one pixel short of the monitor, as other custom-frame apps do. +//! //! The Windows calls go through safe `winsafe` wrappers; the workspace forbids `unsafe`, and //! winsafe can't wrap a raw handle without it, so the window is found by its handle instead. @@ -29,7 +34,20 @@ pub use platform::service; mod platform { use eframe::wgpu::rwh::{HasWindowHandle, RawWindowHandle}; use photocraft_ui_egui::WindowFrameFn; - use winsafe::{HWND, HwndPlace, POINT, SIZE, co}; + use winsafe::prelude::Handle; + use winsafe::{AtomStr, HWND, HwndPlace, POINT, SIZE, co}; + + /// A rect in desktop coordinates: `(left, top, right, bottom)`, right and bottom exclusive. + type Rect = (i32, i32, i32, i32); + + /// A monitor edge. + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + enum Edge { + Left, + Top, + Right, + Bottom, + } /// A guard that keeps the window borderless each frame, or `None` when the app uses the /// system title bar (the window is decorated) or its handle can't be found. @@ -99,8 +117,11 @@ mod platform { return; }; let (m, w) = (mi.rcMonitor, mi.rcWork); - let Some((left, top, right, bottom)) = - clamped_rect((r.left, r.top, r.right, r.bottom), (m.left, m.top, m.right, m.bottom), (w.left, w.top, w.right, w.bottom)) + let (rect, monitor, work) = ((r.left, r.top, r.right, r.bottom), (m.left, m.top, m.right, m.bottom), (w.left, w.top, w.right, w.bottom)); + // The taskbars (a couple of `FindWindowEx` calls) are only looked up while the window + // covers the whole monitor, which a trimmed maximize no longer does. + let Some((left, top, right, bottom)) = clamped_rect(rect, monitor, work) + .or_else(|| covers(rect, monitor).then(|| short_of_autohide_taskbars(rect, monitor, work, &taskbar_edges(monitor))).flatten()) else { return; }; @@ -153,6 +174,69 @@ mod platform { (corrected != rect).then_some(corrected) } + /// Whether `rect` reaches every edge of `monitor`: what the shell takes for a full-screen app. + fn covers(rect: Rect, monitor: Rect) -> bool { + rect.0 <= monitor.0 && rect.1 <= monitor.1 && rect.2 >= monitor.2 && rect.3 >= monitor.3 + } + + /// The edges of `monitor` that have a taskbar (the primary `Shell_TrayWnd` or a secondary + /// monitor's `Shell_SecondaryTrayWnd`), shown or hidden. + fn taskbar_edges(monitor: Rect) -> Vec { + let mut edges = Vec::new(); + for class in ["Shell_TrayWnd", "Shell_SecondaryTrayWnd"] { + let mut after: Option = None; + // One taskbar per monitor; the bound only guards against a misbehaving shell. + for _ in 0..64 { + let Ok(Some(tray)) = HWND::NULL.FindWindowEx(after.as_ref(), AtomStr::from_str(class), None) else { + break; + }; + if let Ok(r) = tray.GetWindowRect() { + edges.extend(taskbar_edge((r.left, r.top, r.right, r.bottom), monitor)); + } + after = Some(tray); + } + } + edges + } + + /// The edge of `monitor` a taskbar with rect `taskbar` sits on: the one nearest its centre, + /// or `None` when it is on another monitor. An auto-hidden taskbar is parked mostly past its + /// edge with a sliver left on screen, so its centre may lie outside the monitor. + fn taskbar_edge(taskbar: Rect, monitor: Rect) -> Option { + let (tl, tt, tr, tb) = (i64::from(taskbar.0), i64::from(taskbar.1), i64::from(taskbar.2), i64::from(taskbar.3)); + let (ml, mt, mr, mb) = (i64::from(monitor.0), i64::from(monitor.1), i64::from(monitor.2), i64::from(monitor.3)); + if tl >= tr || tt >= tb || ml >= mr || mt >= mb || tr <= ml || tl >= mr || tb <= mt || tt >= mb { + return None; + } + // Doubled centre and distances, so nothing rounds. + let (cx, cy) = (tl + tr, tt + tb); + [(Edge::Left, cx - 2 * ml), (Edge::Top, cy - 2 * mt), (Edge::Right, 2 * mr - cx), (Edge::Bottom, 2 * mb - cy)] + .into_iter() + .min_by_key(|&(_, distance)| distance) + .map(|(edge, _)| edge) + } + + /// A maximize covering its monitor, trimmed to stop one pixel short of each edge where a + /// taskbar reserves no work area (it auto-hides), so the shell still reveals it there. + /// `None` when there is nothing to trim. + fn short_of_autohide_taskbars(rect: Rect, monitor: Rect, work: Rect, taskbars: &[Edge]) -> Option { + if !covers(rect, monitor) { + return None; + } + let (mut l, mut t, mut r, mut b) = rect; + for edge in taskbars { + match edge { + Edge::Left if work.0 == monitor.0 => l = monitor.0.checked_add(1)?, + Edge::Top if work.1 == monitor.1 => t = monitor.1.checked_add(1)?, + Edge::Right if work.2 == monitor.2 => r = monitor.2.checked_sub(1)?, + Edge::Bottom if work.3 == monitor.3 => b = monitor.3.checked_sub(1)?, + _ => {} + } + } + let trimmed = (l, t, r, b); + (l < r && t < b && trimmed != rect).then_some(trimmed) + } + /// `style` without the OS title bar and frame (`WS_CAPTION`, which already includes /// `WS_BORDER`). The sizing border (`WS_THICKFRAME`) stays, so edge resizing and Aero snap /// keep working, as they did with winit 0.27's undecorated windows. @@ -162,7 +246,7 @@ mod platform { #[cfg(test)] mod tests { - use super::{borderless, clamped_rect}; + use super::{Edge, borderless, clamped_rect, short_of_autohide_taskbars, taskbar_edge}; use winsafe::co; #[test] @@ -221,6 +305,35 @@ mod platform { assert_eq!(clamped_rect((-500, -8, 1428, 1088), monitor, work), None, "dragged"); assert_eq!(clamped_rect((0, 48, 960, 1080), monitor, work), None, "half-screen"); } + + #[test] + fn a_maximize_leaves_a_pixel_strip_on_an_auto_hidden_taskbar_edge() { + // #4845: an auto-hidden taskbar reserves no work area. + let monitor = (0, 0, 1920, 1080); + let rect = (-8, -8, 1928, 1088); + for (edge, expected) in + [(Edge::Bottom, (-8, -8, 1928, 1079)), (Edge::Top, (-8, 1, 1928, 1088)), (Edge::Left, (1, -8, 1928, 1088)), (Edge::Right, (-8, -8, 1919, 1088))] + { + assert_eq!(short_of_autohide_taskbars(rect, monitor, monitor, &[edge]), Some(expected)); + assert_eq!(short_of_autohide_taskbars(expected, monitor, monitor, &[edge]), None, "already trimmed"); + } + assert_eq!(short_of_autohide_taskbars(rect, monitor, monitor, &[]), None, "no taskbar"); + // A taskbar that reserves its work area is the clamp's job. + assert_eq!(short_of_autohide_taskbars(rect, monitor, (0, 0, 1920, 1032), &[Edge::Bottom]), None); + assert_eq!(short_of_autohide_taskbars((0, 0, 960, 1080), monitor, monitor, &[Edge::Bottom]), None, "half-screen"); + } + + #[test] + fn a_taskbar_sits_on_the_monitor_edge_nearest_its_centre() { + let monitor = (0, 0, 1920, 1080); + assert_eq!(taskbar_edge((0, 1032, 1920, 1080), monitor), Some(Edge::Bottom), "shown"); + assert_eq!(taskbar_edge((0, 1078, 1920, 1126), monitor), Some(Edge::Bottom), "auto-hidden"); + assert_eq!(taskbar_edge((0, -46, 1920, 2), monitor), Some(Edge::Top)); + assert_eq!(taskbar_edge((-60, 0, 2, 1080), monitor), Some(Edge::Left)); + assert_eq!(taskbar_edge((1918, 0, 1980, 1080), monitor), Some(Edge::Right)); + assert_eq!(taskbar_edge((1920, 1032, 3840, 1080), monitor), None, "other monitor"); + assert!(taskbar_edge((i32::MIN, i32::MIN, i32::MAX, i32::MAX), monitor).is_some(), "no overflow"); + } } } diff --git a/apps/photocraft/tests/control_exit_code.rs b/apps/photocraft/tests/control_exit_code.rs deleted file mode 100644 index 280afac261..0000000000 --- a/apps/photocraft/tests/control_exit_code.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! `--control` setup failures must exit non-zero (#1816): a launcher that sees -//! status 0 believes the control server is running when it is not. -//! -//! These tests run the real binary as a subprocess and assert the exit code, -//! covering both failure paths in `main.rs`: control authentication and the -//! automation workspace. - -/// A syntactically valid 64-hex-char token, so the workspace test reaches the -/// workspace check instead of failing on token validation first. -const VALID_TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; - -/// Run `photocraft` with the given arguments in an isolated config dir and -/// return the exit status and stderr. Each call gets its own config dir (tests run in -/// parallel), and no display variables, so a regression fails fast instead of opening a window. -fn run_photocraft(args: &[&str]) -> (std::process::ExitStatus, String) { - static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); - let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); - let dir = std::env::temp_dir().join(format!("photocraft-control-exit-{}-{n}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let output = std::process::Command::new(env!("CARGO_BIN_EXE_photocraft")) - .args(args) - .env("PHOTOCRAFT_CONFIG_DIR", &dir) - // Isolate from the host environment so the test is reproducible. - .env_remove("PHOTOCRAFT_CONTROL_PORT") - .env_remove("PHOTOCRAFT_CONTROL_TOKEN") - .env_remove("PHOTOCRAFT_CONTROL_TOKEN_FILE") - .env_remove("PHOTOCRAFT_AUTOMATION_READ_ROOT") - .env_remove("PHOTOCRAFT_AUTOMATION_WRITE_ROOT") - .env_remove("DISPLAY") - .env_remove("WAYLAND_DISPLAY") - .env_remove("WAYLAND_SOCKET") - .output() - .expect("failed to run photocraft binary"); - std::fs::remove_dir_all(&dir).ok(); - (output.status, String::from_utf8_lossy(&output.stderr).into_owned()) -} - -#[test] -fn invalid_control_token_exits_with_status_2() { - // `validate_token` rejects a token that is not exactly 64 hex characters. - let (status, stderr) = run_photocraft(&["--control", "0", "--control-token", "invalid"]); - assert_eq!(status.code(), Some(2), "invalid control token must exit 2, stderr: {stderr}"); - assert!(stderr.contains("cannot configure control authentication"), "stderr: {stderr}"); -} - -#[test] -fn both_token_and_token_file_exits_with_status_2() { - // `server_token` rejects the combination of a supplied token and a token file. - let dir = std::env::temp_dir().join(format!("photocraft-token-file-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let token_file = dir.join("token.txt"); - std::fs::write(&token_file, VALID_TOKEN).unwrap(); - let (status, stderr) = run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--control-token-file", token_file.to_str().unwrap()]); - std::fs::remove_dir_all(&dir).ok(); - assert_eq!(status.code(), Some(2), "token + token file must exit 2, stderr: {stderr}"); - assert!(stderr.contains("cannot configure control authentication"), "stderr: {stderr}"); -} - -#[test] -fn missing_automation_read_root_exits_with_status_2() { - // `AuthorizedWorkspace::new` fails when the read root does not exist. - let (status, stderr) = run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--automation-read-root", "/nonexistent/photocraft-test-path"]); - assert_eq!(status.code(), Some(2), "missing read root must exit 2, stderr: {stderr}"); - assert!(stderr.contains("cannot configure automation workspace"), "stderr: {stderr}"); -} - -#[test] -fn missing_automation_write_root_exits_with_status_2() { - // `AuthorizedWorkspace::new` fails when the write root does not exist. - let (status, stderr) = run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--automation-write-root", "/nonexistent/photocraft-test-path"]); - assert_eq!(status.code(), Some(2), "missing write root must exit 2, stderr: {stderr}"); - assert!(stderr.contains("cannot configure automation workspace"), "stderr: {stderr}"); -} diff --git a/apps/photocraft/tests/fixtures/README.md b/apps/photocraft/tests/fixtures/README.md new file mode 100644 index 0000000000..1eb90452f0 --- /dev/null +++ b/apps/photocraft/tests/fixtures/README.md @@ -0,0 +1,7 @@ +These files are **schema probes**, not recorded machine saves. + +`ui-0.14.ron`, `ui-0.15.ron` and `ui-0.37.ron` are minimal payloads reconstructed from the +documented eframe/egui layouts. They exercise `apps/photocraft/src/ui_state.rs` (the flat +window entry versus the nested `normal_geometry` entry) only. They do not prove that eframe +and egui deserialize a real `Memory` or `PanelState`; a captured 0.14/0.15/0.37 `ui.ron` +would be needed for that, with the machine-specific data reviewed first. diff --git a/apps/photocraft/tests/fixtures/ui-0.14.ron b/apps/photocraft/tests/fixtures/ui-0.14.ron new file mode 100644 index 0000000000..619999cf2b --- /dev/null +++ b/apps/photocraft/tests/fixtures/ui-0.14.ron @@ -0,0 +1 @@ +{"window":"(inner_position_pixels:Some((x:616.0,y:430.0)),outer_position_pixels:None,fullscreen:false,maximized:false,inner_size_points:Some((x:1100.0,y:760.0)))","egui":"()"} diff --git a/apps/photocraft/tests/fixtures/ui-0.15.ron b/apps/photocraft/tests/fixtures/ui-0.15.ron new file mode 100644 index 0000000000..7edccd600b --- /dev/null +++ b/apps/photocraft/tests/fixtures/ui-0.15.ron @@ -0,0 +1 @@ +{"window":"(inner_position_pixels:None,outer_position_pixels:Some((x:2048.0,y:1228.0)),fullscreen:false,maximized:true,inner_size_points:Some((x:1440.0,y:900.0)))","egui":"()"} diff --git a/apps/photocraft/tests/fixtures/ui-0.37.ron b/apps/photocraft/tests/fixtures/ui-0.37.ron new file mode 100644 index 0000000000..d5973c734f --- /dev/null +++ b/apps/photocraft/tests/fixtures/ui-0.37.ron @@ -0,0 +1 @@ +{"window":"(normal_geometry:(outer_position_pixels:None,scale_factor:2.0,inner_size_points:(x:1440.0,y:900.0)),fullscreen:false,maximized:false)","egui":"()"} diff --git a/apps/photocraft/tests/it/command_line_usage.rs b/apps/photocraft/tests/it/command_line_usage.rs new file mode 100644 index 0000000000..a0d8377638 --- /dev/null +++ b/apps/photocraft/tests/it/command_line_usage.rs @@ -0,0 +1,157 @@ +//! `photocraft --help` and unknown options must not open the window (#6431): before the fix, +//! any argument the loop didn't know was taken as a file, so `--help` or a typo such as +//! `--safegpu` launched the GUI and logged "Couldn't open --help". +//! +//! These run the real binary, and every case must exit before the window opens: Windows and +//! macOS runners have a desktop, so a launch that gets that far hangs until the test times out. +//! Cases whose arguments are valid add a usage error before them: `--control-token` with +//! `--control-token-file` is only checked once every argument is read (and only with `--control`), +//! so stderr shows how the other arguments were taken. + +/// The variables the command line reads: a test that wants one sets it. +const VARS: [&str; 6] = [ + "PHOTOCRAFT_CONTROL_PORT", + "PHOTOCRAFT_CONTROL_TOKEN", + "PHOTOCRAFT_CONTROL_TOKEN_FILE", + "PHOTOCRAFT_AUTOMATION_READ_ROOT", + "PHOTOCRAFT_AUTOMATION_WRITE_ROOT", + "PHOTOCRAFT_IN_WINDOW_MENUS", +]; + +/// A usage error found only after every argument is read. +const LATE_ERROR: [&str; 6] = ["--control", "0", "--control-token", "abcd", "--control-token-file", "token"]; + +/// How a run of `photocraft` ended. +struct Run { + status: std::process::ExitStatus, + stdout: String, + stderr: String, +} + +/// Run `photocraft` with `args` and `vars` in an isolated config dir. +fn run_with(args: &[&str], vars: &[(&str, &str)]) -> Run { + static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("photocraft-usage-{}-{n}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut cmd = std::process::Command::new(crate::binary()); + for var in VARS { + cmd.env_remove(var); + } + let output = cmd + .args(args) + .envs(vars.iter().copied()) + .env("PHOTOCRAFT_CONFIG_DIR", &dir) + .env_remove("DISPLAY") + .env_remove("WAYLAND_DISPLAY") + .env_remove("WAYLAND_SOCKET") + .output() + .expect("failed to run photocraft binary"); + std::fs::remove_dir_all(&dir).ok(); + Run { status: output.status, stdout: String::from_utf8_lossy(&output.stdout).into_owned(), stderr: String::from_utf8_lossy(&output.stderr).into_owned() } +} + +fn run_photocraft(args: &[&str]) -> Run { + run_with(args, &[]) +} + +/// [`LATE_ERROR`] then `args`: exit 2, and the stderr. +fn late_error(args: &[&str], vars: &[(&str, &str)]) -> String { + let Run { status, stdout, stderr } = run_with(&[&LATE_ERROR[..], args].concat(), vars); + assert_eq!(status.code(), Some(2), "{args:?}: {stderr}"); + assert!(stderr.contains("cannot be used with"), "{args:?}: {stderr}"); + assert!(stdout.is_empty(), "{args:?}: {stdout}"); + stderr +} + +#[test] +fn help_prints_usage_and_exits_0() { + for flag in ["--help", "-h"] { + let Run { status, stdout, stderr } = run_photocraft(&[flag]); + assert_eq!(status.code(), Some(0), "{flag} must exit 0, stderr: {stderr}"); + assert!(stdout.contains("Usage: photocraft"), "{flag} stdout: {stdout}"); + for option in ["--safe-gpu", "--new-instance", "--control ", "--version", "`--`", "PHOTOCRAFT_AUTOMATION_READ_ROOT", "PHOTOCRAFT_NEW_INSTANCE"] { + assert!(stdout.contains(option), "{flag} usage must list {option}: {stdout}"); + } + assert!(!stderr.contains("Couldn't open"), "{flag} was taken as a file: {stderr}"); + } +} + +#[test] +fn help_wins_over_later_arguments() { + let Run { status, stdout, .. } = run_photocraft(&["--help", "picture.psd", "--safegpu"]); + assert_eq!(status.code(), Some(0)); + assert!(stdout.contains("Usage: photocraft"), "stdout: {stdout}"); +} + +#[test] +fn version_prints_the_long_version() { + let Run { status, stdout, .. } = run_photocraft(&["--version", "--safegpu"]); + assert_eq!(status.code(), Some(0)); + assert_eq!(stdout, format!("photocraft {}\n", photocraft_engine::build_info::long_version())); +} + +#[test] +fn unknown_options_exit_2_naming_them() { + for (args, named) in [ + (&["--safegpu"][..], "unexpected argument '--safegpu'"), + (&["-x"][..], "unexpected argument '-x'"), + (&["--help=yes"][..], "unexpected value 'yes' for '--help'"), + // An unknown flag after a bad value is reported first: clap drops the pending value's error. + (&["--control", "nope", "--safegpu"][..], "unexpected argument '--safegpu'"), + (&["--control", "nope"][..], "invalid value 'nope' for '--control '"), + ] { + let Run { status, stdout, stderr } = run_photocraft(args); + assert_eq!(status.code(), Some(2), "{args:?} must be a usage error, stderr: {stderr}"); + assert!(stderr.contains(named), "stderr: {stderr}"); + assert!(stderr.contains("--help"), "stderr should point at --help: {stderr}"); + assert!(stdout.is_empty(), "stdout: {stdout}"); + } +} + +#[test] +fn arguments_after_double_dash_are_files() { + // `--` ends the options: `--help` and `-x` after it are file names, so neither prints usage + // nor is an unknown option; only the late error is reported. + let stderr = late_error(&["--", "--help", "-x"], &[]); + assert!(!stderr.contains("unexpected"), "stderr: {stderr}"); +} + +#[test] +fn macos_process_serial_number_is_still_ignored() { + // Old macOS passes `-psn_` when launching from Finder; it is neither a file nor an error. + let stderr = late_error(&["-psn_0_12345"], &[]); + assert!(!stderr.contains("unexpected"), "stderr: {stderr}"); +} + +#[test] +fn new_instance_is_a_known_option() { + let stderr = late_error(&["--new-instance"], &[]); + assert!(!stderr.contains("unexpected"), "stderr: {stderr}"); +} + +/// The variables stand in for their flags: a flag wins, an empty variable counts as unset, a bad +/// value is a usage error naming the variable, and a flag without its value is still an error +/// when its variable is set (#6429). +#[test] +fn variables_are_the_flags_fallbacks() { + let Run { status, stderr, .. } = run_with(&[], &[("PHOTOCRAFT_CONTROL_PORT", "nope")]); + assert_eq!(status.code(), Some(2), "stderr: {stderr}"); + assert!(stderr.contains("invalid value 'nope' for 'PHOTOCRAFT_CONTROL_PORT'"), "stderr: {stderr}"); + // With the flag (in LATE_ERROR), the variable is never read. + let stderr = late_error(&[], &[("PHOTOCRAFT_CONTROL_PORT", "nope")]); + assert!(!stderr.contains("PHOTOCRAFT_CONTROL_PORT"), "stderr: {stderr}"); + // The variable turns the control server on as the flag does, so the token pair is checked. + let Run { status, stderr, .. } = run_with(&LATE_ERROR[2..], &[("PHOTOCRAFT_CONTROL_PORT", "0")]); + assert_eq!(status.code(), Some(2), "stderr: {stderr}"); + assert!(stderr.contains("cannot be used with"), "stderr: {stderr}"); + let Run { status, stderr, .. } = run_with(&["--automation-read-root"], &[("PHOTOCRAFT_AUTOMATION_READ_ROOT", "/env/read")]); + assert_eq!(status.code(), Some(2), "stderr: {stderr}"); + assert!(stderr.contains("a value is required for '--automation-read-root '"), "stderr: {stderr}"); + // The other half of the token pair from a variable is the same either/or error, naming it + // but never showing the token. + let token = "a".repeat(64); + let Run { status, stderr, .. } = run_with(&["--control", "0", "--control-token-file", "token"], &[("PHOTOCRAFT_CONTROL_TOKEN", token.as_str())]); + assert_eq!(status.code(), Some(2), "stderr: {stderr}"); + assert!(stderr.contains("PHOTOCRAFT_CONTROL_TOKEN") && stderr.contains("cannot be used with") && !stderr.contains(&token), "stderr: {stderr}"); +} diff --git a/apps/photocraft/tests/it/control_exit_code.rs b/apps/photocraft/tests/it/control_exit_code.rs new file mode 100644 index 0000000000..98c3243133 --- /dev/null +++ b/apps/photocraft/tests/it/control_exit_code.rs @@ -0,0 +1,122 @@ +//! `--control` setup failures must exit non-zero (#1816): a launcher that sees +//! status 0 believes the control server is running when it is not. +//! +//! These tests run the real binary as a subprocess and assert the exit code, +//! covering control authentication, the automation workspace and listener binding. + +/// A syntactically valid 64-hex-char token, so the workspace test reaches the +/// workspace check instead of failing on token validation first. +const VALID_TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + +/// How a run of `photocraft` ended. +struct Run { + status: std::process::ExitStatus, + stderr: String, +} + +/// Run `photocraft` with the given arguments in an isolated config dir and +/// return how it ended. Each call gets its own config dir (tests run in +/// parallel), and no display variables, so a regression fails fast instead of opening a window. +fn run_photocraft(args: &[&str]) -> Run { + run_with(args, &[]) +} + +/// [`run_photocraft`], with `vars` set. +fn run_with(args: &[&str], vars: &[(&str, &str)]) -> Run { + static NEXT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + let n = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("photocraft-control-exit-{}-{n}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let mut child = std::process::Command::new(crate::binary()) + .args(args) + .env("PHOTOCRAFT_CONFIG_DIR", &dir) + // Isolate from the host environment so the test is reproducible. + .env_remove("PHOTOCRAFT_CONTROL_PORT") + .env_remove("PHOTOCRAFT_CONTROL_TOKEN") + .env_remove("PHOTOCRAFT_CONTROL_TOKEN_FILE") + .env_remove("PHOTOCRAFT_AUTOMATION_READ_ROOT") + .env_remove("PHOTOCRAFT_AUTOMATION_WRITE_ROOT") + .env_remove("DISPLAY") + .env_remove("WAYLAND_DISPLAY") + .env_remove("WAYLAND_SOCKET") + .envs(vars.iter().copied()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .expect("failed to run photocraft binary"); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + let timed_out = loop { + if child.try_wait().unwrap().is_some() { + break false; + } + if std::time::Instant::now() >= deadline { + child.kill().unwrap(); + break true; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + }; + let output = child.wait_with_output().unwrap(); + let gpu_started = dir.join("gpu-starting.json").exists(); + std::fs::remove_dir_all(&dir).ok(); + assert!(!timed_out, "PhotoCraft did not exit: {}", String::from_utf8_lossy(&output.stderr)); + assert!(!gpu_started, "control setup errors must exit before GPU startup"); + Run { status: output.status, stderr: String::from_utf8_lossy(&output.stderr).into_owned() } +} + +#[test] +fn an_occupied_control_port_exits_with_status_2() { + let held = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = held.local_addr().unwrap().port().to_string(); + let Run { status, stderr } = run_photocraft(&["--control", &port, "--control-token", VALID_TOKEN]); + assert_eq!(status.code(), Some(2), "occupied control port must exit 2, stderr: {stderr}"); + assert!(stderr.contains(&format!("failed to bind 127.0.0.1:{port}")), "stderr: {stderr}"); +} + +#[test] +fn invalid_control_token_exits_with_status_2() { + // `validate_token` rejects a token that is not exactly 64 hex characters. + let Run { status, stderr } = run_photocraft(&["--control", "0", "--control-token", "invalid"]); + assert_eq!(status.code(), Some(2), "invalid control token must exit 2, stderr: {stderr}"); + assert!(stderr.contains("cannot configure control authentication"), "stderr: {stderr}"); +} + +#[test] +fn both_token_and_token_file_exits_with_status_2() { + // The command line rejects a supplied token together with a token file (#7071), before + // `server_token` would. + let dir = std::env::temp_dir().join(format!("photocraft-token-file-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let token_file = dir.join("token.txt"); + std::fs::write(&token_file, VALID_TOKEN).unwrap(); + let Run { status, stderr } = run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--control-token-file", token_file.to_str().unwrap()]); + std::fs::remove_dir_all(&dir).ok(); + assert_eq!(status.code(), Some(2), "token + token file must exit 2, stderr: {stderr}"); + assert!(stderr.contains("cannot be used with"), "stderr: {stderr}"); +} + +#[test] +fn missing_automation_read_root_exits_with_status_2() { + // `AuthorizedWorkspace::new` fails when the read root does not exist. + let Run { status, stderr } = + run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--automation-read-root", "/nonexistent/photocraft-test-path"]); + assert_eq!(status.code(), Some(2), "missing read root must exit 2, stderr: {stderr}"); + assert!(stderr.contains("cannot configure automation workspace"), "stderr: {stderr}"); +} + +#[test] +fn missing_automation_write_root_exits_with_status_2() { + // `AuthorizedWorkspace::new` fails when the write root does not exist. + let Run { status, stderr } = + run_photocraft(&["--control", "0", "--control-token", VALID_TOKEN, "--automation-write-root", "/nonexistent/photocraft-test-path"]); + assert_eq!(status.code(), Some(2), "missing write root must exit 2, stderr: {stderr}"); + assert!(stderr.contains("cannot configure automation workspace"), "stderr: {stderr}"); +} + +#[test] +fn automation_roots_come_from_the_environment_too() { + for var in ["PHOTOCRAFT_AUTOMATION_READ_ROOT", "PHOTOCRAFT_AUTOMATION_WRITE_ROOT"] { + let Run { status, stderr } = run_with(&["--control", "0", "--control-token", VALID_TOKEN], &[(var, "/nonexistent/photocraft-test-path")]); + assert_eq!(status.code(), Some(2), "{var}: missing root must exit 2, stderr: {stderr}"); + assert!(stderr.contains("cannot configure automation workspace"), "{var}: stderr: {stderr}"); + } +} diff --git a/apps/photocraft/tests/it/main.rs b/apps/photocraft/tests/it/main.rs new file mode 100644 index 0000000000..73bea9196a --- /dev/null +++ b/apps/photocraft/tests/it/main.rs @@ -0,0 +1,10 @@ +//! The crate's integration tests, linked into one test binary. + +mod command_line_usage; +mod control_exit_code; +mod startup_without_display; + +/// Nextest archives relocate binaries; plain Cargo keeps the compile-time path valid. +fn binary() -> std::ffi::OsString { + std::env::var_os("NEXTEST_BIN_EXE_photocraft").unwrap_or_else(|| env!("CARGO_BIN_EXE_photocraft").into()) +} diff --git a/apps/photocraft/tests/startup_without_display.rs b/apps/photocraft/tests/it/startup_without_display.rs similarity index 96% rename from apps/photocraft/tests/startup_without_display.rs rename to apps/photocraft/tests/it/startup_without_display.rs index 5ffb5783a9..b0faf77d5b 100644 --- a/apps/photocraft/tests/startup_without_display.rs +++ b/apps/photocraft/tests/it/startup_without_display.rs @@ -30,7 +30,7 @@ fn missing_display_preserves_existing_gpu_crash_evidence() { } fn launch_without_display(dir: &std::path::Path) -> String { - let output = std::process::Command::new(env!("CARGO_BIN_EXE_photocraft")) + let output = std::process::Command::new(crate::binary()) .env("PHOTOCRAFT_CONFIG_DIR", dir) .env_remove("DISPLAY") .env_remove("WAYLAND_DISPLAY") diff --git a/assets/app-icon/README.md b/assets/app-icon/README.md index 8ddb66f093..b1ce53b3d8 100644 --- a/assets/app-icon/README.md +++ b/assets/app-icon/README.md @@ -17,8 +17,8 @@ Exactly three colours: ## Geometry A 512-unit tile (`viewBox="0 0 512 512"`), rounded square with `rx=112`, no border. The macOS -renders pad it onto Apple's 824/1024 icon grid; the Windows and Linux renders crop 22 units off -each side so the figure reads at 16–48 px. +renders pad it onto Apple's 824/1024 icon grid; the Windows and Linux renders use the whole tile, +as the other Crafting Apps do. ## Provenance diff --git a/assets/app-icon/hicolor/128x128/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/128x128/apps/ai.storyteller.photocraft.png index a28597fc9b..9f65c1fec1 100644 Binary files a/assets/app-icon/hicolor/128x128/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/128x128/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/16x16/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/16x16/apps/ai.storyteller.photocraft.png index ec4cb49675..02e1b9886a 100644 Binary files a/assets/app-icon/hicolor/16x16/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/16x16/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/24x24/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/24x24/apps/ai.storyteller.photocraft.png index 249b94787b..4b321d2bf5 100644 Binary files a/assets/app-icon/hicolor/24x24/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/24x24/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/256x256/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/256x256/apps/ai.storyteller.photocraft.png index db2eb42d8a..b707a2fa91 100644 Binary files a/assets/app-icon/hicolor/256x256/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/256x256/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/32x32/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/32x32/apps/ai.storyteller.photocraft.png index ad50ecb450..3694573349 100644 Binary files a/assets/app-icon/hicolor/32x32/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/32x32/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/48x48/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/48x48/apps/ai.storyteller.photocraft.png index 341185fdd8..d2ed2d865e 100644 Binary files a/assets/app-icon/hicolor/48x48/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/48x48/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/512x512/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/512x512/apps/ai.storyteller.photocraft.png index e54f75c920..ba0bee419e 100644 Binary files a/assets/app-icon/hicolor/512x512/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/512x512/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/hicolor/64x64/apps/ai.storyteller.photocraft.png b/assets/app-icon/hicolor/64x64/apps/ai.storyteller.photocraft.png index e093144cf2..b5cbfd4fa0 100644 Binary files a/assets/app-icon/hicolor/64x64/apps/ai.storyteller.photocraft.png and b/assets/app-icon/hicolor/64x64/apps/ai.storyteller.photocraft.png differ diff --git a/assets/app-icon/photocraft.ico b/assets/app-icon/photocraft.ico index 88c8a0adad..7d4d947f11 100644 Binary files a/assets/app-icon/photocraft.ico and b/assets/app-icon/photocraft.ico differ diff --git a/assets/icons/align-edge-bottom.svg b/assets/icons/align-edge-bottom.svg new file mode 100644 index 0000000000..e2993c8305 --- /dev/null +++ b/assets/icons/align-edge-bottom.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/align-edge-hcenter.svg b/assets/icons/align-edge-hcenter.svg new file mode 100644 index 0000000000..264b4fe9bb --- /dev/null +++ b/assets/icons/align-edge-hcenter.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/align-edge-left.svg b/assets/icons/align-edge-left.svg new file mode 100644 index 0000000000..192310fd3f --- /dev/null +++ b/assets/icons/align-edge-left.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/align-edge-right.svg b/assets/icons/align-edge-right.svg new file mode 100644 index 0000000000..0386cf7475 --- /dev/null +++ b/assets/icons/align-edge-right.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/align-edge-top.svg b/assets/icons/align-edge-top.svg new file mode 100644 index 0000000000..54d4bdce21 --- /dev/null +++ b/assets/icons/align-edge-top.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/align-edge-vcenter.svg b/assets/icons/align-edge-vcenter.svg new file mode 100644 index 0000000000..6df5d35096 --- /dev/null +++ b/assets/icons/align-edge-vcenter.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/distribute-bottom.svg b/assets/icons/distribute-bottom.svg new file mode 100644 index 0000000000..20d3b31c33 --- /dev/null +++ b/assets/icons/distribute-bottom.svg @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/assets/icons/distribute-hcenter.svg b/assets/icons/distribute-hcenter.svg new file mode 100644 index 0000000000..34faee88f4 --- /dev/null +++ b/assets/icons/distribute-hcenter.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + diff --git a/assets/icons/distribute-horizontal.svg b/assets/icons/distribute-horizontal.svg new file mode 100644 index 0000000000..fef7592891 --- /dev/null +++ b/assets/icons/distribute-horizontal.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/distribute-left.svg b/assets/icons/distribute-left.svg new file mode 100644 index 0000000000..075305c0c0 --- /dev/null +++ b/assets/icons/distribute-left.svg @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/assets/icons/distribute-right.svg b/assets/icons/distribute-right.svg new file mode 100644 index 0000000000..659d58fbee --- /dev/null +++ b/assets/icons/distribute-right.svg @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/assets/icons/distribute-top.svg b/assets/icons/distribute-top.svg new file mode 100644 index 0000000000..1f02d74c9b --- /dev/null +++ b/assets/icons/distribute-top.svg @@ -0,0 +1,19 @@ + + + + + + + + + diff --git a/assets/icons/distribute-vcenter.svg b/assets/icons/distribute-vcenter.svg new file mode 100644 index 0000000000..4d1734a083 --- /dev/null +++ b/assets/icons/distribute-vcenter.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + diff --git a/assets/icons/distribute-vertical.svg b/assets/icons/distribute-vertical.svg new file mode 100644 index 0000000000..fdc7c6abd2 --- /dev/null +++ b/assets/icons/distribute-vertical.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/ellipse-dashed.svg b/assets/icons/ellipse-dashed.svg new file mode 100644 index 0000000000..f852dcf3a7 --- /dev/null +++ b/assets/icons/ellipse-dashed.svg @@ -0,0 +1,13 @@ + + + diff --git a/assets/icons/font-similar.svg b/assets/icons/font-similar.svg new file mode 100644 index 0000000000..e2399f7ab0 --- /dev/null +++ b/assets/icons/font-similar.svg @@ -0,0 +1,14 @@ + + + + diff --git a/assets/icons/font-variable.svg b/assets/icons/font-variable.svg new file mode 100644 index 0000000000..4415bcd32a --- /dev/null +++ b/assets/icons/font-variable.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/path-op-combine.svg b/assets/icons/path-op-combine.svg new file mode 100644 index 0000000000..4c60a71e25 --- /dev/null +++ b/assets/icons/path-op-combine.svg @@ -0,0 +1,13 @@ + + + diff --git a/assets/icons/path-op-exclude.svg b/assets/icons/path-op-exclude.svg new file mode 100644 index 0000000000..e2bea5b413 --- /dev/null +++ b/assets/icons/path-op-exclude.svg @@ -0,0 +1,13 @@ + + + diff --git a/assets/icons/path-op-intersect.svg b/assets/icons/path-op-intersect.svg new file mode 100644 index 0000000000..a6bca9787e --- /dev/null +++ b/assets/icons/path-op-intersect.svg @@ -0,0 +1,15 @@ + + + + + diff --git a/assets/icons/path-op-merge.svg b/assets/icons/path-op-merge.svg new file mode 100644 index 0000000000..32dbe69ffe --- /dev/null +++ b/assets/icons/path-op-merge.svg @@ -0,0 +1,13 @@ + + + diff --git a/assets/icons/path-op-new-layer.svg b/assets/icons/path-op-new-layer.svg new file mode 100644 index 0000000000..21b26eac87 --- /dev/null +++ b/assets/icons/path-op-new-layer.svg @@ -0,0 +1,13 @@ + + + diff --git a/assets/icons/path-op-subtract.svg b/assets/icons/path-op-subtract.svg new file mode 100644 index 0000000000..c461ac44ad --- /dev/null +++ b/assets/icons/path-op-subtract.svg @@ -0,0 +1,14 @@ + + + + diff --git a/assets/icons/quick-selection.svg b/assets/icons/quick-selection.svg index 454b38bae6..7e63dc66b9 100644 --- a/assets/icons/quick-selection.svg +++ b/assets/icons/quick-selection.svg @@ -9,8 +9,8 @@ stroke-linecap="round" stroke-linejoin="round" > - - - - + + + + diff --git a/assets/icons/rotate-ccw.svg b/assets/icons/rotate-ccw.svg new file mode 100644 index 0000000000..988d5fe949 --- /dev/null +++ b/assets/icons/rotate-ccw.svg @@ -0,0 +1,14 @@ + + + + diff --git a/assets/icons/slice-select.svg b/assets/icons/slice-select.svg new file mode 100644 index 0000000000..a32003ada0 --- /dev/null +++ b/assets/icons/slice-select.svg @@ -0,0 +1,16 @@ + + + + + + diff --git a/assets/icons/text-warp.svg b/assets/icons/text-warp.svg new file mode 100644 index 0000000000..03f30ed87b --- /dev/null +++ b/assets/icons/text-warp.svg @@ -0,0 +1,17 @@ + + + + + + + diff --git a/book/src/automation/cli.md b/book/src/automation/cli.md index 05a7c3da27..6137b12466 100644 --- a/book/src/automation/cli.md +++ b/book/src/automation/cli.md @@ -63,4 +63,23 @@ layers because that would lose their group's blending semantics. Paths supplied to the current CLI are ordinary operating-system paths. The CLI does not restrict them to a workspace root. Batch output creates the requested output directory, and save operations may create parent directories. `batch` refuses an output directory that is its input directory (the results would replace the originals) unless `--in-place` is given. Run untrusted action files only in an appropriately isolated account or environment. -CLI parsing reports malformed flags and JSON as errors, and a flag the subcommand doesn't take (a typo such as `--fromat`) is a usage error (exit status 2) rather than being ignored. It is not a security policy layer. Engine commands remain responsible for validating their own parameters. +CLI parsing checks the flags and their values before any work starts. A flag the subcommand doesn't take (a typo such as `--fromat`), a flag given twice (only `--cmd` and `--params` may repeat), a value out of range (`--quality 0`), malformed `--new` or `--params` JSON, `` together with `--new`, and `--control-token` together with `--control-token-file` are all usage errors (exit status 2), rather than being ignored or failing later. A value flag never takes the next flag as its value, so a value starting with `-` is written with `=` (`--out=-image.png`) or as `./-image.png`; negative numbers such as `--filter -1` still work. A path that is not valid Unicode reaches the command instead of being a usage error: `convert` reads and writes it, while `info`, `run` and `batch` open files through the automation layer, which still needs UTF-8 paths and fails with exit status 1 (`droplet` takes its arguments as text, so it refuses such a path as a usage error). `photocraft-cli --version` (or `-V`) prints the version; there is no `version` subcommand. + +`mcp` and `serve` reject positional arguments, empty values and options for the wrong mode before +starting a server. Explicit control-token flags require `mcp --bridge` or `serve --port` (or +`PHOTOCRAFT_CONTROL_PORT`, below). Configure automation roots on the desktop process when using +bridge mode; the CLI rejects root flags there. See the [MCP launch guide](mcp.md) for examples. + +`mcp` and `serve` fall back to environment variables: `PHOTOCRAFT_CONTROL_TOKEN` and `PHOTOCRAFT_CONTROL_TOKEN_FILE` for the token flags, and `PHOTOCRAFT_CONTROL_PORT` for `serve --port` and `mcp --bridge` (which then bridges to `127.0.0.1:`). The automation roots are flags only: the desktop app's `PHOTOCRAFT_AUTOMATION_READ_ROOT` and `PHOTOCRAFT_AUTOMATION_WRITE_ROOT` grant the CLI nothing. A flag wins over its variable, an empty variable counts as unset, and a bad value is a usage error naming the variable. When `PHOTOCRAFT_CONTROL_PORT` switches `serve` to TCP or `mcp` to bridging, the command says so on standard error. A token together with a token file is a usage error only where a token is read (`serve` on TCP, `mcp` bridging), so a stale exported pair doesn't stop stdio `serve` or headless `mcp`. An automation root given to `mcp` while the variable would switch it to bridging is a usage error too: the roots are the headless engine's, and the bridge would drop them. ` --help` lists each variable (unless it is set but empty), never with the token's value. A variable holds a port, so `PHOTOCRAFT_CONTROL_PORT=127.0.0.1:7878` is refused; only `--bridge` takes `host:port`. + +This checking is not a security policy layer. Engine commands remain responsible for validating their own parameters. + +### Shell completions and man pages + +The Linux `.deb` and `.rpm` packages install `man photocraft-cli` (plus a page per subcommand, such as `man photocraft-cli-convert`) and completions for bash, zsh and fish (zsh's in `/usr/share/zsh/site-functions`, which the `.deb` also links from Debian's `/usr/share/zsh/vendor-completions`); the Linux tarball and the FreeBSD tarball carry the same files, the FreeBSD one under `man/man1` and `share/`. The pages list each flag's environment variable, as `--help` does. Without a package, generate them from a source checkout: + +```sh +cargo run -p photocraft-cli --example man-and-completions -- out/ +``` + +`out/` then holds `photocraft-cli.bash`, `_photocraft-cli` (zsh), `photocraft-cli.fish`, `photocraft-cli.elv` (elvish), `_photocraft-cli.ps1` (PowerShell) and the `.1` man pages. For example, copy `photocraft-cli.bash` to `~/.local/share/bash-completion/completions/photocraft-cli`, `photocraft-cli.fish` into `~/.config/fish/completions/`, `_photocraft-cli` into a directory on zsh's `$fpath`, and read a page with `man -l out/photocraft-cli.1`. The desktop app `photocraft` has neither. diff --git a/book/src/automation/mcp.md b/book/src/automation/mcp.md index 4f3d02496a..11ce2f4549 100644 --- a/book/src/automation/mcp.md +++ b/book/src/automation/mcp.md @@ -5,6 +5,9 @@ - **Headless:** an in-process `Headless` session owns the engine and performs file I/O. - **Bridge:** `BridgeClient` forwards tools to a desktop application started with `photocraft --control `. +For a source checkout, first [install the CLI on PATH and register the server](../../../docs/mcp.md#install-and-register). +The installed binary survives `cargo clean`; a symlink into `target/release` does not. + Start the headless server with: ```sh @@ -17,6 +20,16 @@ Bridge to a running desktop application with the same private token file used by photocraft-cli mcp --bridge 127.0.0.1:7878 --control-token-file /private/path/control.token ``` +Use absolute root paths in MCP client configurations, since a relative root is resolved from the +client's launch directory. In bridge mode the roots are configured on the desktop app, and root +flags on `photocraft-cli mcp --bridge` are rejected. Explicit `--control-token` and +`--control-token-file` flags require `--bridge` (or `PHOTOCRAFT_CONTROL_PORT`) and are mutually +exclusive; bridge credentials can also come from `PHOTOCRAFT_CONTROL_TOKEN` or +`PHOTOCRAFT_CONTROL_TOKEN_FILE`. Each launch option +except `--help`/`-h` takes one non-empty value and cannot be repeated. Stray positional arguments, +missing values and options for the wrong mode are usage errors (exit 2) reported on stderr before +the server starts. + Tools cover session/document operations, command discovery and execution, batching, and—when bridged—UI inspection and control. Tool schemas improve correctness but are not authorization boundaries. ## From an installed release @@ -25,13 +38,13 @@ The release packages ship `photocraft-cli` alongside the desktop app, so no buil | Install | CLI | |---|---| -| Windows (MSI) | `C:\Program Files\PhotoCraft\photocraft-cli.exe` by default (wherever you installed it otherwise), not on `PATH` | +| Windows (MSI) | `%LOCALAPPDATA%\Programs\ArtCraft\PhotoCraft\photocraft-cli.exe` by default (`C:\Program Files\ArtCraft\PhotoCraft\photocraft-cli.exe` for an all-users install, under `Program Files (x86)` instead for the 32-bit build, or wherever you installed it otherwise), not on `PATH` | | Linux (deb, rpm) | `/usr/bin/photocraft-cli` | | macOS | the separate `photocraft-cli--macos-.zip` release asset (the `.app` holds only the desktop app) | ```sh -# Windows, default install folder -claude mcp add photocraft -- "C:\Program Files\PhotoCraft\photocraft-cli.exe" mcp --automation-read-root --automation-write-root +# Windows (PowerShell), default install folder +claude mcp add photocraft -- "$env:LOCALAPPDATA\Programs\ArtCraft\PhotoCraft\photocraft-cli.exe" mcp --automation-read-root --automation-write-root # Linux, or macOS with the CLI unzipped onto PATH claude mcp add photocraft -- photocraft-cli mcp --automation-read-root --automation-write-root ``` diff --git a/book/src/development/release-security.md b/book/src/development/release-security.md index 742ab11095..99a95f1f82 100644 --- a/book/src/development/release-security.md +++ b/book/src/development/release-security.md @@ -6,6 +6,7 @@ PhotoCraft's release workflow builds platform packages and a draft GitHub Releas - platform-specific signing/notarization steps exist for macOS and Windows; - signing secrets are scoped through the GitHub `release` environment; +- the Azure Trusted Signing client that Windows signing loads is pinned to a version and SHA-256, and refused on a mismatch; - release artifacts include `SHA256SUMS.txt`; - the final release job receives `contents: write`, while build jobs use narrower access; - artifacts remain in a draft release until a maintainer publishes them. diff --git a/book/src/formats/pcraft.md b/book/src/formats/pcraft.md index 39385877a1..40ecbdd127 100644 --- a/book/src/formats/pcraft.md +++ b/book/src/formats/pcraft.md @@ -8,8 +8,11 @@ tiles/.zst blobs/.zst thumb.png composite/preview.png +save.lock ``` +`save.lock` exists only in directory bundles: a save holds an OS file lock on it while it writes objects, publishes the manifest and removes unreferenced objects, so concurrent saves of one bundle take turns. It is empty, and readers ignore it. + The manifest is versioned and describes the document tree. Tiles and blobs are content-addressed. Loaders verify ZIP entry bounds and CRCs; document content also uses stored hashes to detect mismatched or corrupted tile/blob content. Unsupported newer format versions fail explicitly. ## Implemented load limits diff --git a/book/src/formats/raster-formats.md b/book/src/formats/raster-formats.md index 2e65d51550..13ab740a86 100644 --- a/book/src/formats/raster-formats.md +++ b/book/src/formats/raster-formats.md @@ -1,10 +1,22 @@ # Raster formats -`photocraft-codecs` provides format detection, capability declarations, decode limits, metadata handling, and encoding for flat images. The default build declares read/write support for PNG, JPEG, TIFF, WebP, GIF, BMP, TGA, ICO, Netpbm/PFM, QOI, OpenEXR, and Radiance HDR. HEIF/HEIC (iPhone and Mac photos) is read-only, decoded in pure Rust by `heic-rs` through the optional `photocraft-heif` crate. It is a build-time choice behind the `heif` cargo feature (`cargo build -p photocraft --features heif`): official builds include it; without it HEIC files are still recognised and opening one reports that HEIC support isn't included in that build. AVIF encoding is optional; AVIF decoding is not implemented in the current crate. +`photocraft-codecs` provides format detection, capability declarations, decode limits, metadata handling, and encoding for flat images. The default build declares read/write support for PNG, JPEG, TIFF, WebP, GIF, BMP, TGA, ICO, Netpbm/PFM, QOI, OpenEXR, and Radiance HDR. HEIF/HEIC (iPhone and Mac photos) is read-only, decoded in pure Rust by `heic-rs` through the optional `photocraft-heif` crate. It is a build-time choice behind the `heif` cargo feature (`cargo build -p photocraft --features heif`): official builds include it; without it HEIC files are still recognised and opening one reports that HEIC support isn't included in that build. AVIF encoding is optional; AVIF decoding is not implemented in the current crate. Lossless JPEG XL (.jxl) export uses the pure-Rust `zune-jpegxl` encoder for 8/16-bit gray, RGB and RGBA data; gray+alpha is expanded to RGBA at the same bit depth. JPEG XL import is tracked separately in #2868. ## Layered TIFF -A TIFF saved by Photoshop with its layers carries them in two private tags: 37724 (`ImageSourceData`, the same layer section a PSD has, preceded by the string `Adobe Photoshop Document Data Block`) and 34377 (the PSD image resources). `photocraft-codecs` passes both through as opaque bytes on `Metadata`; `photocraft-io` rebuilds a PSD model from them plus the TIFF's pixels and hands it to the PSD importer, so groups, masks, layer styles, type, shapes, smart objects and adjustment layers open from a TIFF exactly as from a PSD, and are written back the same way. In Intel-order (`II`) files Photoshop stores the whole block structure byte-swapped (reversed keys such as `MIB8` and `ryaL`, little-endian integers and doubles) while pixel samples stay big-endian; `photocraft-psd`'s `tiff` module converts between the two orders field by field, and drops, with a warning, any block whose layout it does not know rather than pass byte-swapped data to other readers. Limits: a TIFF holds one transparency sample, so extra alpha channels are not written; Lab documents are saved flat. Save As asks "Including layers will increase file size" (with "Discard Layers and Save a Copy") while *Ask Before Saving Layered TIFF Files* is on in Preferences › File Handling. Scripted and agent saves write a flat TIFF unless they ask for layers: `--tiff-layers` for `photocraft-cli convert`, `run` and `batch`, and `"tiffLayers": true` in the save, batch and export commands and MCP `doc_save`. +A TIFF saved by Photoshop with its layers carries them in two private tags: 37724 (`ImageSourceData`, the same layer section a PSD has, preceded by the string `Adobe Photoshop Document Data Block`) and 34377 (the PSD image resources). `photocraft-codecs` passes both through as opaque bytes on `Metadata`; `photocraft-io` rebuilds a PSD model from them plus the TIFF's pixels and hands it to the PSD importer, so groups, masks, layer styles, type, shapes, smart objects and adjustment layers open from a TIFF exactly as from a PSD, and are written back the same way. In Intel-order (`II`) files Photoshop stores the whole block structure byte-swapped (reversed keys such as `MIB8` and `ryaL`, little-endian integers and doubles) while pixel samples stay big-endian; `photocraft-psd`'s `tiff` module converts between the two orders field by field, and drops, with a warning, any block whose layout it does not know rather than pass byte-swapped data to other readers. Limits: up to sixteen extra samples (spot colours and alpha channels, plus a Quick Mask) are written in the IFD and round-trip, with Photoshop image resources for the channel name, ink colour and solidity; Lab documents are saved flat, with their extra samples. Save As asks "Including layers will increase file size" (with "Discard Layers and Save a Copy") while *Ask Before Saving Layered TIFF Files* is on in Preferences › File Handling. Scripted and agent saves write a flat TIFF unless they ask for layers: `--tiff-layers` for `photocraft-cli convert`, `run` and `batch`, and `"tiffLayers": true` in the save, batch and export commands and MCP `doc_save`. + +## JPEG XL + +Save As, Export As and the CLI can write a lossless, single-image `.jxl` bare +codestream at 8 or 16 bits per sample. Gray, RGB and RGBA are encoded +natively. Grayscale with alpha is expanded to RGBA at the original bit depth +to preserve both grayscale and alpha samples. For 16-bit RGBA the encoder's +incorrect default 8-bit alpha header is corrected to explicitly describe +16-bit alpha, with an independent decoder oracle checking alpha values. +The bare codestream has no ICC, EXIF, XMP or DPI boxes; the export path +converts tagged RGB to sRGB and surfaces metadata losses as fidelity warnings. +JPEG XL opening remains under review in #2868. ## WebP @@ -19,6 +31,8 @@ Every route that writes a document to a flat format (Save As, the CLI, MCP `doc_ - Formats without transparency (JPEG, Radiance HDR) get the image composited over white, as Export As and Quick Export do; in CMYK, white is no ink. - Formats that can't embed an ICC profile (GIF, BMP, TGA, QOI, ICO, Netpbm/PFM) get RGB documents in another profile (linear light, Display P3, …) converted to sRGB through the colour engine (perceptual intent), since an untagged file is read as sRGB. OpenEXR and Radiance HDR get linear sRGB instead (linear gray for a grayscale OpenEXR), which is how they are tagged when opened. - TGA, as Photoshop's Targa: an RGB Targa is 24 or 32 bits per pixel. Save As and Save a Copy ask (Targa Options, starting at 32 with an alpha channel, else 24); Export As has a Resolution choice (Auto, 24 or 32 bits/pixel); scripts pass `tgaBits` (`--tga-bits` on the CLI). At 32 bits the document's alpha channel is the file's alpha when it has exactly one (spot channels don't count; with several, none is used), and the colours are written exactly, never premultiplied or cleared where the alpha is 0, so a game texture's mask and its colours both survive. Without a usable alpha channel the layers' transparency is the alpha (opaque without any). 24 bits write no alpha, compositing transparency over white. Without a choice: 32 with an alpha channel or transparency, else 24. A 32-bit Targa opens as an opaque Background with its alpha as "Alpha 1", so it round-trips. Grayscale Targas are 8-bit. +- JPEG, as Photoshop's JPEG Options: File › Save, Save As and Save a Copy ask for the Quality on Photoshop's 0–12 scale (with its Low, Medium, High and Maximum presets) and a Format Option: Baseline ("Standard"), Baseline Optimized (Huffman tables fitted to the image) or Progressive. The quality starts at the opened JPEG's own quality, else at the Export preference's JPEG quality; left there, an opened JPEG is written at exactly its own quality. Scripts pass `"quality": 0..12` and `"jpegFormat": "baseline" | "optimized" | "progressive"` (on the CLI, `--quality` on the encoder's 1–100 scale and `--jpeg-format`) and aren't asked; recorded actions replay the answer. The scale maps onto the encoder's 1–100 as `round(q / 12 × 99 + 1)`. RGB JPEGs are written 4:2:0, where Baseline Optimized keeps the standard tables for now (Gray and CMYK, written 4:4:4, get fitted ones). Matte, the progressive scan count (Scans 3–5) and the file-size preview aren't offered. +- DDS, for game and 3D textures: Save As and Save a Copy ask (DDS Options) for the compression (None, uncompressed and lossless; BC1/DXT1, whose alpha is on or off; BC2/DXT3; BC3/DXT5) and whether to generate mipmaps (the full chain down to 1×1, each level a box filter of the one above). Scripts pass `ddsCompression` (`none`, `bc1`, `bc2`, `bc3`) and `ddsMipmaps` (`--dds-compression`, `--dds-mipmaps` on the CLI). Opening one reads its top mip level. Flat TIFF and JPEG exports of composited CMYK documents convert the RGB composite back through the document's CMYK ICC profile (relative colorimetric with black point compensation) and embed diff --git a/clippy.toml b/clippy.toml index 4b6c79f555..3522a15b69 100644 --- a/clippy.toml +++ b/clippy.toml @@ -2,3 +2,7 @@ allow-unwrap-in-tests = true allow-expect-in-tests = true allow-panic-in-tests = true allow-indexing-slicing-in-tests = true +disallowed-types = [ + { path = "egui::ComboBox", reason = "use craft_ui::ComboBox: egui's combo box menu can't grow back while open (egui#5225)" }, +] +cognitive-complexity-threshold = 30 diff --git a/contributors/people.toml b/contributors/people.toml index 8694c72698..20f9f5b143 100644 --- a/contributors/people.toml +++ b/contributors/people.toml @@ -17,6 +17,13 @@ # To remove your names, delete your entry (or set fields empty) in a PR. The next # contributors.py run, and the next build, drop them. +[people.brianblakely] +sync_github_name = true + +[people.abdul-karim-mia] +real_name = "Abdul karim mia" +sync_github_name = false + [people.jonathanwxh-cell] display_name = "jonathnwxh" sync_github_name = false @@ -72,6 +79,9 @@ sync_github_name = true [people.maxm11] sync_github_name = true +[people.mweinbach] +sync_github_name = true + [people.skraus2] sync_github_name = true @@ -116,6 +126,9 @@ sync_github_name = false [people.axlecoffee] sync_github_name = true +[people.zi-ger] +sync_github_name = true + [people.Losminek] sync_github_name = true @@ -159,6 +172,116 @@ sync_github_name = false real_name = "Ufuk Efe" sync_github_name = true +[people.nicolasfalesy] +sync_github_name = true + [people.sidney] real_name = "Sidney Markowitz" sync_github_name = true + +[people.theoabw] +real_name = "Theo Wilenius" + +[people.TaylorBurke] +sync_github_name = true + +[people.1Git2Clone] +sync_github_name = true + +[people.Silvertree2010] +display_name = "Netter" +sync_github_name = false + +[people.Byxis] +sync_github_name = true + +[people.00xJS] +sync_github_name = true + +[people.guillehr2] +real_name = "Guillem Hermida" +sync_github_name = true + +[people.fjamesprice] +sync_github_name = true + +[people.ha1des] +sync_github_name = true + +[people.webdevtodayjason] +display_name = "Jason Brashear" + +[people.AndreeSalazar] +real_name = "Andreé Salazar" +sync_github_name = true + +[people.LorenzoFasolino] +real_name = "Lorenzo Fasolino" +display_name = "Lorenzo Fasolino" +sync_github_name = false + +[people.zolyy] +real_name = "Gall Zoltan" +sync_github_name = false + +[people.theshubh77] +real_name = "Shubham Bhamare" +sync_github_name = true + +[people.ksaxena149] +sync_github_name = true + +[people.giang17] +real_name = "Giang Nguyen" +sync_github_name = true + +[people.aqibmohd271] +sync_github_name = true + +[people.marcoarnulfo] +real_name = "Marco Arnulfo" +display_name = "Marco Arnulfo" +sync_github_name = false + +[people.Gotix-eg] +real_name = "Ahmed Tammam" +display_name = "Gotix" +sync_github_name = true + +[people.adi-1108] +real_name = "Aditya Vishwakarma" +sync_github_name = true + +[people.benandez] +real_name = "Martin Ball" + +[people.ronibalthazar] +sync_github_name = true + +[people.batuhangozegu] +real_name = "Batuhan Gözegü" +sync_github_name = false + +[people.drunkeninja42] +real_name = "Kushagra Sharma" + +[people.WebWork-rrs] +display_name = "Ratul Paul" + +[people.softwaregames1] +sync_github_name = true + +[people.VoiceLessQ] +display_name = "VoiceLessQ" +sync_github_name = false + +[people.erenova] +display_name = "Erenova" +sync_github_name = false + +[people.jasallen] +real_name = "Jason DeBoever" +sync_github_name = true + +[people.dylanmtaylor] +sync_github_name = true diff --git a/crates/affinity/README.md b/crates/affinity/README.md index 3e4e066156..1be7fd954e 100644 --- a/crates/affinity/README.md +++ b/crates/affinity/README.md @@ -108,11 +108,11 @@ The same reader is shared, as an independent copy, with VectorCraft's `vectorcra * Unit tests on synthetic containers built by the `synth` feature (stored, zlib and zstd entries, checksums, budgets, cycles, hostile streams, every truncation) and property tests of random - mutations, here and in `photocraft-io`'s `tests/affinity.rs`. + mutations, here and in `photocraft-io`'s `tests/it/affinity.rs`. * `cargo xtask corpus --affinity` fetches 21 public CC0/MIT documents, including the four Affinity 3 `.af` files of [samuel-etver/vector-art](https://github.com/samuel-etver/vector-art) - (CC0), at pinned commits, each checked against `xtask/affinity-corpus.sha256`. `tests/real_files.rs` - parses them; `photocraft-io`'s `tests/affinity_corpus.rs` imports and flattens each one and + (CC0), at pinned commits, each checked against `xtask/affinity-corpus.sha256`. `tests/it/real_files.rs` + parses them; `photocraft-io`'s `tests/it/affinity_corpus.rs` imports and flattens each one and compares it with the thumbnail Affinity saved in it (mean difference 0–4.6 of 255, with a ceiling per file). * `fuzz/` has `cargo-fuzz` targets for the whole reader (`preview`), the object stream (`stream`) @@ -139,11 +139,18 @@ zstd window, 4096 saved revisions, array lengths no longer than the bytes left, 16 777 216 decoded values per document stream (fields and array elements together, counted before anything is allocated: a value takes about 40 bytes in memory however few it took in the file; the largest of those 189 public documents uses 3.3 million), 384 levels of object nesting, 128 levels of layers, 500 000 layers, four million curve -nodes per curve, 64 megapixels per pixel layer (cropped to its content first) and 1024 gradient +nodes per curve, 4 levels of spot colours defined through spot colours, 64 megapixels per pixel layer (cropped to its content first) and 1024 gradient stops; `photocraft-io` adds 300 000 pixels a side per document and 64 megapixels per image on the canvas. Every archive entry's CRC-32 and size must match. Malformed input returns an `Error`; the crate has no panics outside tests. +Compound outlines have a separate active-reference cycle check and a depth cap of 64. Across +the document, geometry expansion admits at most 500 000 combined outline calls and operand-slot +visits (including nulls and non-object values), and four million points created or copied into +compounds. Operand slots are charged before filtering, without first collecting an operand list. +A compound that crosses a limit or contains a cycle is omitted +with a warning as a whole; ordinary shared operands remain valid and keep each occurrence's transform. + ## Privacy Affinity documents can hold the folder they were saved in, user names, original image paths and diff --git a/crates/affinity/src/access.rs b/crates/affinity/src/access.rs index 0b60ed7dbd..a93ec84462 100644 --- a/crates/affinity/src/access.rs +++ b/crates/affinity/src/access.rs @@ -20,6 +20,12 @@ impl Stream { self.class(id) == Some(Tag::of(class)) } + /// The raw slot count of an object's array field (including null slots), for charging + /// traversal work before nulls are filtered out. `None` when the field is not an array. + pub fn array_len(&self, id: ObjId, tag: &[u8; 4]) -> Option { + self.field(id, tag)?.array_len() + } + /// A child object (shared, linked or inline); `None` for null or missing. pub fn obj(&self, id: ObjId, tag: &[u8; 4]) -> Option { match self.field(id, tag)? { diff --git a/crates/affinity/src/container.rs b/crates/affinity/src/container.rs index 880b7236d1..96fdbd706c 100644 --- a/crates/affinity/src/container.rs +++ b/crates/affinity/src/container.rs @@ -15,6 +15,11 @@ const DOCUMENT_KIND: &[u8; 4] = b"nsrP"; /// A save appends one table; real files have one to a few hundred. const MAX_TABLES: usize = 4096; +/// Distinct ids the allocation tables may name; each costs a map slot even when only deleted. +const MAX_ENTRIES: usize = 1 << 22; +/// Bytes of entry names the allocation tables may name. A name is retained twice (in the entry +/// and as its map key), and tables can point many names at the same bytes. +const MAX_NAME_BYTES: usize = 32 << 20; /// zstd window ceiling: real documents use a few MiB; this stops a header from reserving gigabytes. const MAX_ZSTD_WINDOW: u64 = 64 << 20; @@ -164,8 +169,9 @@ impl<'a> Archive<'a> { pub fn open(bytes: &'a [u8], limits: Limits) -> Result { let header = header(bytes)?; // Newest record per id: (table date, table index, record). - let mut latest: HashMap)> = HashMap::new(); + let mut latest: HashMap>)> = HashMap::new(); let mut names: HashMap = HashMap::new(); + let mut name_bytes = 0usize; let mut seen = HashSet::new(); let mut next = header.fat_offset; let mut tables = 0usize; @@ -222,19 +228,31 @@ impl<'a> Archive<'a> { } else { stored }; - record = Some(Entry { id, name: String::new(), offset, size, compressed, crc, compression }); + record = Some(Box::new(Entry { id, name: String::new(), offset, size, compressed, crc, compression })); } 2 => {} _ => return Err(Error::Malformed("unknown allocation record")), } if flag == 0 { let len = usize::from(c.u16()?); - let name = String::from_utf8_lossy(c.take(len)?).into_owned(); - names.entry(id).or_insert(name); + let raw = c.take(len)?; + // Only the first name per id is kept: charge and copy just that one. + if let std::collections::hash_map::Entry::Vacant(slot) = names.entry(id) { + let name = String::from_utf8_lossy(raw).into_owned(); + name_bytes = name_bytes.saturating_add(name.len()); + if name_bytes > MAX_NAME_BYTES { + return Err(Error::Limit("archive entry names too large")); + } + slot.insert(name); + } } // Equal dates: the first table in the chain (the newest) wins, as in afread. let replace = latest.get(&id).is_none_or(|(d, _, _)| date > *d); if replace { + // Deletion records are five bytes each: cap what distinct ids may retain. + if latest.len() >= MAX_ENTRIES && !latest.contains_key(&id) { + return Err(Error::Limit("too many archive entries")); + } latest.insert(id, (date, tables, record)); } } @@ -248,7 +266,7 @@ impl<'a> Archive<'a> { for (id, (_, _, record)) in latest { if let (Some(mut e), Some(name)) = (record, names.get(&id)) { e.name = name.clone(); - entries.insert(e.name.clone(), e); + entries.insert(e.name.clone(), *e); } } Ok(Self { bytes, header, entries, revisions: tables, extracted: 0, limits }) @@ -278,6 +296,9 @@ impl<'a> Archive<'a> { if total > self.limits.max_total { return Err(Error::Limit("document too large")); } + // Charge before decoding: a corrupt entry that fails its checksum has already cost the + // decompression, and an importer that recovers must not get it again for free. + self.extracted = total; let mut c = Cursor::new(self.bytes, offset(e.offset)?); if c.take(4)? != b"#Fil" { return Err(Error::Malformed("archive entry has no data block")); @@ -297,7 +318,6 @@ impl<'a> Archive<'a> { if crc32fast::hash(&out) != e.crc { return Err(Error::Malformed("archive entry checksum mismatch")); } - self.extracted = total; Ok(out) } } @@ -312,7 +332,7 @@ fn read_bounded(mut r: impl Read, size: usize) -> Result, Error> { } fn inflate(data: &[u8], size: usize) -> Result, Error> { - read_bounded(flate2::read::ZlibDecoder::new(data), size) + read_bounded(flate2::bufread::ZlibDecoder::new(data), size) } fn unzstd(data: &[u8], size: usize) -> Result, Error> { diff --git a/crates/affinity/src/geometry.rs b/crates/affinity/src/geometry.rs index 08554134db..0c1a871b27 100644 --- a/crates/affinity/src/geometry.rs +++ b/crates/affinity/src/geometry.rs @@ -6,10 +6,113 @@ use crate::stream::{ObjId, Tag, Value}; /// Nodes per curve object. const MAX_NODES: usize = 4_000_000; +const MAX_COMPOUND_DEPTH: usize = 64; +const MAX_OUTLINE_VISITS: usize = 500_000; +const MAX_OUTLINE_POINTS: usize = 4_000_000; +/// Curve records decoded across the document. A shared curve is decoded again for each +/// occurrence, and records that only move handles add no points. +const MAX_OUTLINE_RECORDS: usize = 8_000_000; + +/// Geometry is expanded per occurrence, since parametric outlines can depend on their world +/// transform. Bound work across the entire document instead of memoizing by object id alone. +pub(crate) struct Budget { + visits_left: usize, + points_left: usize, + records_left: usize, +} + +impl Default for Budget { + fn default() -> Self { + Self { visits_left: MAX_OUTLINE_VISITS, points_left: MAX_OUTLINE_POINTS, records_left: MAX_OUTLINE_RECORDS } + } +} + +impl Budget { + /// Charge an outline call or a raw compound operand slot before inspecting it. + fn visit(&mut self) -> Result<(), &'static str> { + self.visits_left = self.visits_left.checked_sub(1).ok_or("compound geometry budget exhausted")?; + Ok(()) + } + + /// Charge curve records before they are decoded. + fn records(&mut self, n: usize) -> Result<(), &'static str> { + self.records_left = self.records_left.checked_sub(n).ok_or("curve geometry budget exhausted")?; + Ok(()) + } + + /// Charge both leaf geometry and each copy into a compound before allocating that copy. + fn path(&mut self, path: &Path) -> Result<(), &'static str> { + let points = path.subpaths.iter().fold(0usize, |n, s| n.saturating_add(1).saturating_add(s.segments.len().saturating_mul(3))); + let Some(left) = self.points_left.checked_sub(points) else { + // Do not rebuild an expensive leaf for every subsequent root after exhaustion. + self.visits_left = 0; + return Err("compound geometry budget exhausted"); + }; + self.points_left = left; + Ok(()) + } +} /// The node-space outline of a curve, shape or compound node, and whether it fills even-odd. /// Instances (master-page or symbol copies) take the geometry of their master. pub(crate) fn outline(r: &mut Reader, id: ObjId, class: Tag, world: Affine) -> Option<(Path, bool)> { + match outline_inner(r, id, class, world, &mut Vec::new()) { + Ok(path) => path, + Err(reason) => { + r.warn(reason); + None + } + } +} + +fn outline_inner(r: &mut Reader, id: ObjId, class: Tag, world: Affine, active: &mut Vec) -> Result, &'static str> { + r.geometry_budget.visit()?; + if active.contains(&id) { + return Err("cyclic compound shape"); + } + // Independent of the layer-tree limit: Comp operands do not pass through Reader::node. + if active.len() >= MAX_COMPOUND_DEPTH { + return Err("compound depth exceeds 64 levels"); + } + if class != Tag::of(b"Comp") { + let path = leaf_outline(r, id, class, world); + if let Some((path, _)) = &path { + r.geometry_budget.path(path)?; + } + return Ok(path); + } + active.push(id); + let result = compound(r, id, world, active); + active.pop(); + result +} + +fn compound(r: &mut Reader, id: ObjId, world: Affine, active: &mut Vec) -> Result, &'static str> { + let s = r.s; + // One path of all operands' contours, filled even-odd, as before. A safety failure rejects + // the whole outline: a partial add/subtract shape could silently change its meaning. + let mut path = Path::default(); + // Charge every slot before filtering nulls or malformed values. Stream::objs would scan + // and allocate the whole list first, repeating uncharged work for each shared occurrence. + let children = if let Some(Value::Array(children)) = s.field(id, b"Chld") { children.as_slice() } else { &[] }; + for child in children { + r.geometry_budget.visit()?; + let Value::Obj(Some(c)) = child else { continue }; + let c = *c; + let local = s.floats::<6>(c, b"Xfrm").map(Affine::from_xfrm).unwrap_or(Affine::IDENTITY); + let Some(class) = s.class(c) else { return Ok(None) }; + if s.enumeration(c, b"ComO").is_some_and(|(op, _)| op != 0 && op != 2) { + r.warn("compound shapes that intersect or exclude (imported as add)"); + } + if let Some((p, _)) = outline_inner(r, c, class, local.then(world), active)? { + r.geometry_budget.path(&p)?; + path.subpaths.extend(p.transformed(local).subpaths); + } + } + Ok(Some((path, true))) +} + +fn leaf_outline(r: &mut Reader, id: ObjId, class: Tag, world: Affine) -> Option<(Path, bool)> { let s = r.s; match &class.0.to_be_bytes() { b"PCrv" | b"TxtC" => { @@ -18,23 +121,6 @@ pub(crate) fn outline(r: &mut Reader, id: ObjId, class: Tag, world: Affine) -> O let multi = path.subpaths.len() > 1; Some((path, multi)) } - b"Comp" => { - // One path of all the operands' contours in the compound's space, filled even-odd: this - // matches add and subtract operands in every public sample; a true union of overlapping - // added operands would differ. - let mut path = Path::default(); - for c in s.objs(id, b"Chld") { - let local = s.floats::<6>(c, b"Xfrm").map(Affine::from_xfrm).unwrap_or(Affine::IDENTITY); - let class = s.class(c)?; - if s.enumeration(c, b"ComO").is_some_and(|(op, _)| op != 0 && op != 2) { - r.warn("compound shapes that intersect or exclude (imported as add)"); - } - if let Some((p, _)) = outline(r, c, class, local.then(world)) { - path.subpaths.extend(p.transformed(local).subpaths); - } - } - Some((path, true)) - } _ => { let (shape, box_owner) = match s.obj(id, b"Shpe") { Some(sh) => (sh, id), @@ -62,7 +148,21 @@ pub(crate) fn curves(r: &mut Reader, crvs: ObjId) -> Option { Value::UInt(n) => usize::try_from(*n).ok()?, _ => return None, }; - let corners = s.obj(crvs, b"CnrD").and_then(|c| corner_radii(s.positional(c).collect())); + let corners = match s.obj(crvs, b"CnrD") { + Some(c) => { + let fields: Vec<&Value> = s.positional(c).collect(); + // The corner arrays are scanned for every occurrence of a shared curve, so charge + // them like records before they are walked. + if let Some(len) = fields.get(1).and_then(|v| v.array_len()) + && let Err(reason) = r.geometry_budget.records(len) + { + r.warn(reason); + return None; + } + corner_radii(fields) + } + None => None, + }; let mut path = Path::default(); let mut seen = 0usize; for index in 0..count { @@ -70,13 +170,23 @@ pub(crate) fn curves(r: &mut Reader, crvs: ObjId) -> Option { Value::Bool(b) => *b, _ => return None, }; - let Value::Array(records) = fields.next()? else { return None }; - seen = seen.checked_add(records.len())?; + let records = fields.next()?; + let len = records.array_len()?; + seen = seen.checked_add(len)?; if seen > MAX_NODES { r.warn("curves with more than four million nodes"); return None; } - let anchors = anchors(records)?; + if let Err(reason) = r.geometry_budget.records(len) { + r.warn(reason); + return None; + } + let anchors = match records { + Value::Array(a) => anchors(a.iter().map(record)), + Value::Records { size: 18, data } => anchors(data.as_chunks::<18>().0.iter().map(record18)), + // Records of another width are no curve nodes. + _ => anchors(std::iter::repeat_n(None, len)), + }?; let radii = if index == 0 { corners.as_ref() } else { None }; if let Some(sp) = subpath(&anchors, closed, radii) { path.subpaths.push(sp); @@ -98,22 +208,24 @@ struct Anchor { fn record(v: &Value) -> Option<(Point, u8)> { let Value::Bytes(b) = v else { return None }; - if b.len() != 18 { - return None; - } - let x = f64::from_le_bytes(b.get(0..8)?.try_into().ok()?); - let y = f64::from_le_bytes(b.get(8..16)?.try_into().ok()?); + record18(b.as_slice().try_into().ok()?) +} + +/// One 18-byte curve node: f64 x, f64 y, u8 kind, u8 role. +fn record18(b: &[u8; 18]) -> Option<(Point, u8)> { + let x = f64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]]); + let y = f64::from_le_bytes([b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]]); if !x.is_finite() || !y.is_finite() { return None; } - Some((Point { x, y }, *b.get(17)?)) + Some((Point { x, y }, b[17])) } -fn anchors(records: &[Value]) -> Option> { +fn anchors(records: impl ExactSizeIterator>) -> Option> { let mut out: Vec = Vec::with_capacity(records.len() / 3 + 1); let mut pending_in = None; - for (i, v) in records.iter().enumerate() { - let (p, role) = record(v)?; + for (i, v) in records.enumerate() { + let (p, role) = v?; match role { 0 => { out.push(Anchor { p, inh: pending_in.take(), out: None, record: i }); @@ -134,7 +246,16 @@ fn anchors(records: &[Value]) -> Option> { fn subpath(anchors: &[Anchor], closed: bool, radii: Option<&Vec<(usize, f64)>>) -> Option { let first = anchors.first()?; if let Some(radii) = radii.filter(|r| !r.is_empty()) { - let corner = |a: &Anchor| radii.iter().filter(|(rec, _)| *rec == a.record).map(|(_, r)| *r).fold(0.0, f64::max); + // Index each record's maximum radius once. Scanning the whole radius list per anchor + // was O(anchors * radii), which a file with large anchor and radius arrays could drive + // into quadratic work. Duplicate record indices keep the maximum, matching the prior + // fold(0.0, f64::max) behavior. + let mut radius_by_record: std::collections::HashMap = std::collections::HashMap::with_capacity(radii.len()); + for &(rec, r) in radii { + let slot = radius_by_record.entry(rec).or_insert(0.0); + *slot = slot.max(r); + } + let corner = |a: &Anchor| radius_by_record.get(&a.record).copied().unwrap_or(0.0); let polyline = anchors.iter().all(|a| a.inh.is_none_or(|h| h == a.p) && a.out.is_none_or(|h| h == a.p)); if polyline { let mut pts: Vec<(Point, f64)> = anchors.iter().map(|a| (a.p, corner(a))).collect(); @@ -173,3 +294,15 @@ fn corner_radii(fields: Vec<&Value>) -> Option> { .collect(), ) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn curve_records_are_charged_across_the_document() { + let mut b = Budget::default(); + assert!(b.records(MAX_OUTLINE_RECORDS).is_ok()); + assert_eq!(b.records(1), Err("curve geometry budget exhausted")); + } +} diff --git a/crates/affinity/src/model.rs b/crates/affinity/src/model.rs index 7fba5a4597..3d74333111 100644 --- a/crates/affinity/src/model.rs +++ b/crates/affinity/src/model.rs @@ -283,11 +283,26 @@ const MAX_TREE_DEPTH: usize = 128; /// Total layer nodes imported. const MAX_NODES: usize = 500_000; +/// Total raw `Chld` slots inspected across the whole traversal, including null slots and slots +/// reached through repeated references to a shared group. The node limit only counts produced +/// nodes, so a shared group holding a large null-filled child array could still be rescanned +/// many times; this bounds that work. Generous relative to MAX_NODES for real documents. +const MAX_CHILD_SLOTS: usize = 8_000_000; +/// Bytes of layer names the model may retain across all occurrences. +const MAX_NAME_BYTES: usize = 64 << 20; +/// Page rectangles the model may retain across all spreads (real books have a few hundred). +const MAX_PAGES: usize = 1 << 18; +/// Bytes of font names the text runs may retain across the document. +const MAX_FONT_NAME_BYTES: usize = 16 << 20; + /// Read a document: archive, `doc.dat` and the model. pub fn read(bytes: &[u8], limits: Limits) -> Result { let mut archive = Archive::open(bytes, limits)?; let doc = archive.read("doc.dat")?; let s = stream::parse(&doc)?; + // The parsed stream owns copies of everything it needs: do not hold the inflated bytes + // (hundreds of MB for a large vector document) while the model is built. + drop(doc); Reader::new(&s, &mut archive, limits).document() } @@ -297,7 +312,12 @@ pub(crate) struct Reader<'s, 'a, 'b> { pub(crate) dpi: f64, warnings: BTreeMap, nodes: usize, + child_slots: usize, + name_bytes: usize, + pages: usize, + font_name_bytes: usize, active: HashSet, + pub(crate) geometry_budget: crate::geometry::Budget, /// Decoded pixels still available ([`Limits::max_pixel_bytes`] less what the layers read so /// far hold). pixels_left: usize, @@ -305,7 +325,20 @@ pub(crate) struct Reader<'s, 'a, 'b> { impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { fn new(s: &'s Stream, archive: &'b mut Archive<'a>, limits: Limits) -> Self { - Self { s, archive, dpi: 72.0, warnings: BTreeMap::new(), nodes: 0, active: HashSet::new(), pixels_left: limits.max_pixel_bytes } + Self { + s, + archive, + dpi: 72.0, + warnings: BTreeMap::new(), + nodes: 0, + child_slots: 0, + name_bytes: 0, + pages: 0, + font_name_bytes: 0, + active: HashSet::new(), + geometry_budget: crate::geometry::Budget::default(), + pixels_left: limits.max_pixel_bytes, + } } /// Admit a layer's decoded pixels before they are allocated (#4960). `rgba` is the layer's @@ -343,7 +376,7 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { self.warn("an unknown kind of page"); continue; } - let pages = pages(s, spread); + let pages = self.pages(spread)?; let default_size = s.floats::<2>(doc, b"DfSz").filter(|[w, h]| *w > 0.0 && *h > 0.0).map(|[w, h]| Rect { x0: 0.0, y0: 0.0, x1: w, y1: h }); let bounds = s.floats::<4>(spread, b"SprB").map(rect).or_else(|| pages.iter().copied().reduce(union)).or(default_size).unwrap_or(Rect { x0: 0.0, @@ -373,6 +406,10 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { let s = self.s; let mut out = Vec::new(); let mut mask = None; + // Charge every raw child slot (null slots included) against a document-wide budget + // before filtering. A shared group with a large null-filled Chld array could otherwise + // be rescanned through each reference without the node limit noticing. + self.charge_slots(s.array_len(parent, b"Chld").unwrap_or(0))?; for c in s.objs(parent, b"Chld") { if s.is(c, b"MRst") { // A mask layer inside a group or layer masks that container. @@ -394,6 +431,15 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { Ok((out, mask)) } + /// Counts raw array slots (null ones included) against [`MAX_CHILD_SLOTS`]. + pub(crate) fn charge_slots(&mut self, n: usize) -> Result<(), Error> { + self.child_slots = self.child_slots.saturating_add(n); + if self.child_slots > MAX_CHILD_SLOTS { + return Err(Error::Limit("too many child slots in the layer tree")); + } + Ok(()) + } + fn node(&mut self, id: ObjId, parent: Affine, depth: usize) -> Result, Error> { let s = self.s; if depth > MAX_TREE_DEPTH { @@ -412,7 +458,13 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { let local = s.floats::<6>(id, b"Xfrm").map(Affine::from_xfrm).unwrap_or(Affine::IDENTITY); let world = local.then(parent); let class = s.class(id).unwrap_or(Tag(0)); - let name = s.str(id, b"Desc").unwrap_or_default().to_string(); + // A shared layer's name is copied for every occurrence, so charge the bytes first. + let name = s.str(id, b"Desc").unwrap_or_default(); + self.name_bytes = self.name_bytes.saturating_add(name.len()); + if self.name_bytes > MAX_NAME_BYTES { + return Err(Error::Limit("layer names too large")); + } + let name = name.to_string(); let visible = s.bool(id, b"Visi").unwrap_or(true); let locked = s.bool(id, b"Edtb").is_some_and(|e| !e); let opacity = s.f64(id, b"Opac").map_or(1.0, |o| o.clamp(0.0, 1.0)); @@ -435,6 +487,7 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { let (mask, attached_mask) = self.attached_masks(id, world)?; let pixel_mask = attached_mask.or(child_mask); self.active.remove(&id); + self.charge_slots(s.array_len(id, b"FiEf").unwrap_or(0))?; if s.objs(id, b"FiEf").iter().any(|e| s.bool(*e, b"Enab") != Some(false)) { self.warn("layer effects (shadows, glows, outlines, bevels…)"); } @@ -511,6 +564,8 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { /// an attached mask layer is a pixel mask. Adjustments attached the same way are reported. fn attached_masks(&mut self, id: ObjId, world: Affine) -> Result<(Option, Option), Error> { let s = self.s; + // Every occurrence of a shared layer scans its whole attachment array, null slots included. + self.charge_slots(s.array_len(id, b"AdCh").unwrap_or(0))?; let attached = s.objs(id, b"AdCh"); let mut mask: Option = None; let mut pixels: Option = None; @@ -559,21 +614,51 @@ impl<'s, 'a, 'b> Reader<'s, 'a, 'b> { } } -/// Page rectangles: `SpMd.PagR[].rctp` (Publisher 2, Affinity 3) or `SprB` split into `PagC` -/// equal pages (Publisher 1 facing spreads). -fn pages(s: &Stream, spread: ObjId) -> Vec { - if let Some(md) = s.obj(spread, b"SpMd") { - let v: Vec = s.objs(md, b"PagR").into_iter().filter_map(|p| s.floats::<4>(p, b"rctp").map(rect)).filter(|r| r.x1 > r.x0 && r.y1 > r.y0).collect(); - if !v.is_empty() { - return v; +impl Reader<'_, '_, '_> { + /// Page rectangles: `SpMd.PagR[].rctp` (Publisher 2, Affinity 3) or `SprB` split into `PagC` + /// equal pages (Publisher 1 facing spreads). A spread listed several times, or sharing one + /// large page array, is expanded into a new vector each time, so the slots scanned and the + /// rectangles retained are charged across the document. + fn pages(&mut self, spread: ObjId) -> Result, Error> { + let s = self.s; + if let Some(md) = s.obj(spread, b"SpMd") { + self.charge_slots(s.array_len(md, b"PagR").unwrap_or(0))?; + let remaining = MAX_PAGES.saturating_sub(self.pages); + let v: Vec = s + .objs(md, b"PagR") + .into_iter() + .filter_map(|p| s.floats::<4>(p, b"rctp").map(rect)) + .filter(|r| r.x1 > r.x0 && r.y1 > r.y0) + .take(remaining.saturating_add(1)) + .collect(); + if !v.is_empty() { + self.charge_pages(v.len())?; + return Ok(v); + } } + Ok(match (s.floats::<4>(spread, b"SprB").map(rect), s.int(spread, b"PagC")) { + (Some(b), Some(n @ 2..=16)) => { + self.charge_pages(n as usize)?; + let w = (b.x1 - b.x0) / n as f64; + (0..n).map(|i| Rect { x0: b.x0 + w * i as f64, x1: b.x0 + w * (i + 1) as f64, ..b }).collect() + } + _ => Vec::new(), + }) } - match (s.floats::<4>(spread, b"SprB").map(rect), s.int(spread, b"PagC")) { - (Some(b), Some(n @ 2..=16)) => { - let w = (b.x1 - b.x0) / n as f64; - (0..n).map(|i| Rect { x0: b.x0 + w * i as f64, x1: b.x0 + w * (i + 1) as f64, ..b }).collect() + + /// Counts retained font-name bytes against [`MAX_FONT_NAME_BYTES`]; false once it is spent. + pub(crate) fn charge_font_name(&mut self, n: usize) -> bool { + self.font_name_bytes = self.font_name_bytes.saturating_add(n); + self.font_name_bytes <= MAX_FONT_NAME_BYTES + } + + /// Counts retained page rectangles against [`MAX_PAGES`]. + fn charge_pages(&mut self, n: usize) -> Result<(), Error> { + self.pages = self.pages.saturating_add(n); + if self.pages > MAX_PAGES { + return Err(Error::Limit("too many pages")); } - _ => Vec::new(), + Ok(()) } } diff --git a/crates/affinity/src/paint.rs b/crates/affinity/src/paint.rs index c4ebff8446..904f642123 100644 --- a/crates/affinity/src/paint.rs +++ b/crates/affinity/src/paint.rs @@ -129,13 +129,29 @@ const MAX_STOPS: usize = 1024; fn floats(v: &Value) -> Option> { match v { Value::Floats(f) => Some(f.clone()), - Value::Bytes(b) if b.len() % 4 == 0 => Some(b.as_chunks::<4>().0.iter().map(|c| f64::from(f32::from_le_bytes(*c))).collect()), + Value::Bytes(b) if b.len() % 4 == 0 => Some(f32_samples(b)), _ => None, } } +/// Little-endian `f32` samples widened to `f64`. +fn f32_samples(b: &[u8]) -> Vec { + b.as_chunks::<4>().0.iter().map(|c| f64::from(f32::from_le_bytes(*c))).collect() +} + +/// Spot colours defined through spot colours. A spot colour (`Pant`) is read through its RGB +/// equivalent, else its base colour, which in real files hold plain colours. Colours are shared +/// objects, so one can link to itself, and with both links followed at every level a colour read +/// costs up to 2^(depth+1) lookups. +const MAX_SPOT_DEPTH: usize = 4; + /// A colour object (`RGBA`, `HSLA`, `CMYK`, `GRAY`, `LABA`, `Pant`). pub(crate) fn color(r: &mut Reader, id: ObjId) -> Option { + color_at(r, id, 0) +} + +/// [`color`] of an object `depth` spot colours down. +fn color_at(r: &mut Reader, id: ObjId, depth: usize) -> Option { let s = r.s; let class = s.class(id)?; let col = || -> Option> { @@ -166,8 +182,12 @@ pub(crate) fn color(r: &mut Reader, id: ObjId) -> Option { Color::Lab { l: 100.0 * u(0)?, a: 255.0 * u(1)? - 128.0, b: 255.0 * u(2)? - 128.0, alpha: u(3)? } } b"Pant" => { + if depth >= MAX_SPOT_DEPTH { + r.warn("spot colours nested deeper than 4 levels (left empty)"); + return None; + } r.warn("spot colours (imported as their RGB equivalent)"); - return s.obj(id, b"srgb").and_then(|c| color(r, c)).or_else(|| s.obj(id, b"base").and_then(|c| color(r, c))); + return s.obj(id, b"srgb").and_then(|c| color_at(r, c, depth + 1)).or_else(|| s.obj(id, b"base").and_then(|c| color_at(r, c, depth + 1))); } b"RegC" => Color::Cmyk { c: 1.0, m: 1.0, y: 1.0, k: 1.0, a: 1.0 }, _ => { @@ -217,9 +237,18 @@ pub(crate) fn descriptor(r: &mut Reader, id: ObjId, local: Affine) -> Option> = match s.field(grad, b"Posn") { - Some(Value::Array(a)) => a.iter().filter_map(floats).collect(), + Some(Value::Array(a)) => a.iter().filter_map(floats).take(MAX_STOPS).collect(), + Some(Value::Records { size, data }) if *size % 4 == 0 && *size > 0 => { + data.chunks_exact(usize::from(*size)).map(f32_samples).take(MAX_STOPS).collect() + } _ => Vec::new(), }; let mut stops = Vec::new(); diff --git a/crates/affinity/src/preview.rs b/crates/affinity/src/preview.rs index f50cb8c3e9..d3341eeb1c 100644 --- a/crates/affinity/src/preview.rs +++ b/crates/affinity/src/preview.rs @@ -94,11 +94,8 @@ pub fn preview(bytes: &[u8]) -> Result, Error> { } image_data = true; } - b"IEND" => { - if !image_data { - return Err(Error::Malformed("PNG has no image data")); - } - } + b"IEND" if !image_data => return Err(Error::Malformed("PNG has no image data")), + b"IEND" => {} _ if kind.first().is_some_and(u8::is_ascii_uppercase) => { return Err(Error::Unsupported("unknown critical PNG chunk")); } diff --git a/crates/affinity/src/raster.rs b/crates/affinity/src/raster.rs index bb1ecc112f..226c66a459 100644 --- a/crates/affinity/src/raster.rs +++ b/crates/affinity/src/raster.rs @@ -181,24 +181,32 @@ fn plane( let height = s.int(bitmap, b"BmpH").and_then(|v| usize::try_from(v).ok()).unwrap_or(0); let tw = s.int(bitmap, &tag(b"TWi", c)).and_then(|v| usize::try_from(v).ok()).unwrap_or_else(|| (width * bps).div_ceil(TILE)); let th = s.int(bitmap, &tag(b"THi", c)).and_then(|v| usize::try_from(v).ok()).unwrap_or_else(|| height.div_ceil(TILE)); - let states: Vec = match s.field(bitmap, &tag(b"Sta", c)) { - Some(Value::Array(a)) => a.iter().map(|v| if let Value::UInt(u) = v { u8::try_from(*u).unwrap_or(255) } else { 255 }).collect(), - _ => Vec::new(), + // Borrow the file-controlled arrays: collecting the whole state array first costs its full + // length on every occurrence of a shared layer, however small the declared grid is. + let states: &[Value] = match s.field(bitmap, &tag(b"Sta", c)) { + Some(Value::Array(a)) => a, + _ => &[], }; - let tiles = s.objs(bitmap, &tag(b"Idx", c)); + let mut tiles = match s.field(bitmap, &tag(b"Idx", c)) { + Some(Value::Array(a)) => a.as_slice(), + _ => &[], + } + .iter() + .filter_map(|v| if let Value::Obj(Some(o)) = v { Some(*o) } else { None }); + // The scan below runs for every occurrence of a shared bitmap, whatever it produces. + r.charge_slots(states.len().min(tw.saturating_mul(th)))?; let row = (w as usize) * bps; let mut out = vec![0u8; row * h as usize]; let (bx0, bx1) = (x0 as usize * bps, (x0 + w) as usize * bps); let (py0, py1) = (y0 as usize, (y0 + h) as usize); - let mut next = 0usize; for (t, state) in states.iter().enumerate().take(tw.saturating_mul(th)) { + let state = &if let Value::UInt(u) = state { u8::try_from(*u).unwrap_or(255) } else { 255 }; let (ty, tx) = (t / tw.max(1), t % tw.max(1)); let fill = match state { 0 | 1 => None, 2 => Some(0xFF), 4 => { - let Some(blck) = tiles.get(next).copied() else { return Err(Error::Malformed("missing pixel tile")) }; - next += 1; + let Some(blck) = tiles.next() else { return Err(Error::Malformed("missing pixel tile")) }; let (ox, oy) = (tx * TILE, ty * TILE); if ox >= bx1 || ox + TILE <= bx0 || oy >= py1 || oy + TILE <= py0 { continue; diff --git a/crates/affinity/src/stream.rs b/crates/affinity/src/stream.rs index a755747c45..348fc6540a 100644 --- a/crates/affinity/src/stream.rs +++ b/crates/affinity/src/stream.rs @@ -53,6 +53,23 @@ pub enum Value { /// `None` is a null object. Obj(Option), Array(Vec), + /// An array of fixed-size records (`0x2c`, curve nodes) held in one buffer: `size` bytes each. + /// Counts as one value per record against [`MAX_VALUES`], like the array of `Bytes` it replaces. + Records { + size: u16, + data: Vec, + }, +} + +impl Value { + /// Slot count of an array field (including null slots); `None` for a scalar. + pub fn array_len(&self) -> Option { + match self { + Value::Array(a) => Some(a.len()), + Value::Records { size, data } => Some(data.len() / usize::from(*size).max(1)), + _ => None, + } + } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -73,11 +90,51 @@ pub struct Object { pub chain: Vec, /// Fields in stream order; positional fields have tag 0. pub fields: Vec<(Tag, Value)>, + /// Index for objects with more than [`SCAN`] fields: (tag, index of its first occurrence) + /// sorted by tag, so `get` is a binary search. A shared object can be referenced by many + /// sibling layers, and each reference used to rescan all of its fields; indexing once keeps + /// repeated lookups from multiplying into quadratic work. Smaller objects have no index + /// (and no per-object hash map to build): scanning a handful of fields is cheaper. + index: Vec<(Tag, u32)>, +} + +/// Objects with at most this many fields are scanned instead of indexed. +const SCAN: usize = 12; + +fn build_index(fields: &[(Tag, Value)]) -> Vec<(Tag, u32)> { + if fields.len() <= SCAN { + return Vec::new(); + } + // Positional fields share tag 0 and are not looked up by tag. Sorting by (tag, index) and + // keeping the first of each tag preserves first-match semantics. + let mut index: Vec<(Tag, u32)> = fields.iter().zip(0u32..).filter(|((t, _), _)| t.0 != 0).map(|((t, _), i)| (*t, i)).collect(); + index.sort_unstable(); + index.dedup_by_key(|e| e.0); + index } impl Object { + pub fn new(class: Tag, kind: Kind, chain: Vec, fields: Vec<(Tag, Value)>) -> Self { + let index = build_index(&fields); + Object { class, kind, chain, fields, index } + } + pub fn get(&self, tag: Tag) -> Option<&Value> { - self.fields.iter().find(|(t, _)| *t == tag).map(|(_, v)| v) + if tag.0 == 0 { + return None; + } + if self.fields.len() <= SCAN { + return self.fields.iter().find(|(t, _)| *t == tag).map(|(_, v)| v); + } + let at = self.index.binary_search_by_key(&tag, |e| e.0).ok()?; + self.index.get(at).and_then(|&(_, i)| self.fields.get(i as usize)).map(|(_, v)| v) + } + + /// Replaces the fields (the root object is filled in after it is pushed) and rebuilds the + /// tag index so `get` stays correct. + fn set_fields(&mut self, fields: Vec<(Tag, Value)>) { + self.index = build_index(&fields); + self.fields = fields; } } @@ -124,10 +181,10 @@ pub fn parse(bytes: &[u8]) -> Result { c.u32()?; } let mut p = Parser { c, objects: Vec::new(), shared: std::collections::HashMap::new(), depth: 0, values: 0 }; - let root = p.push(Object { class, kind: Kind::Root, chain: vec![class], fields: Vec::new() })?; + let root = p.push(Object::new(class, Kind::Root, vec![class], Vec::new()))?; let fields = p.fields(true, class)?; if let Some(o) = p.objects.get_mut(root) { - o.fields = fields; + o.set_fields(fields); } Ok(Stream { root, objects: p.objects }) } @@ -252,7 +309,7 @@ impl Parser<'_> { } 0x30 => { let fields = self.fields(false, class)?; - let id = self.push(Object { class, kind: Kind::Positional, chain: Vec::new(), fields })?; + let id = self.push(Object::new(class, Kind::Positional, Vec::new(), fields))?; Value::Obj(Some(id)) } 0x31 | 0x32 if !tagged => return Err(Error::Malformed("object without a field tag")), @@ -303,14 +360,17 @@ impl Parser<'_> { 0x2c => { let n = usize::try_from(self.c.u32()?).map_err(|_| Error::Malformed("array length"))?; let size = usize::from(self.c.u16()?); - if n.checked_mul(size).is_none_or(|b| b > self.c.remaining()) { + // A zero width passes the length check for any count, then costs a Value per record. + if size == 0 || n.checked_mul(size).is_none_or(|b| b > self.c.remaining()) { return Err(Error::Malformed("array longer than its data")); } - let mut v = self.vec(n)?; - for _ in 0..n { - v.push(Value::Bytes(self.c.take(size)?.to_vec())); - } - Ok(Value::Array(v)) + // Still one value per record against the budget, but a single allocation. + self.spend(n)?; + let bytes = self.c.take(n * size)?; + let mut data = Vec::new(); + data.try_reserve_exact(bytes.len()).map_err(|_| Error::Limit("not enough memory for the document stream"))?; + data.extend_from_slice(bytes); + Ok(Value::Records { size: size as u16, data }) } 0x32 => { let n = self.count(1)?; @@ -350,7 +410,7 @@ impl Parser<'_> { } }; let fields = self.fields(true, class)?; - let id = self.push(Object { class, kind: Kind::Inline, chain: vec![class], fields })?; + let id = self.push(Object::new(class, Kind::Inline, vec![class], fields))?; Ok(Value::Obj(Some(id))) } @@ -369,7 +429,7 @@ impl Parser<'_> { return Err(Error::Malformed("object defined twice")); } // Register before the fields: an object's own fields may refer back to it. - let id = self.push(Object { class: Tag(0), kind: Kind::Shared, chain: Vec::new(), fields: Vec::new() })?; + let id = self.push(Object::new(Tag(0), Kind::Shared, Vec::new(), Vec::new()))?; self.shared.insert(key, id); let mut chain = Vec::new(); let mut fields = Vec::new(); @@ -395,7 +455,7 @@ impl Parser<'_> { let class = chain.first().copied().unwrap_or(Tag(0)); fields.extend(self.fields(true, class)?); if let Some(o) = self.objects.get_mut(id) { - *o = Object { class, kind: Kind::Shared, chain, fields }; + *o = Object::new(class, Kind::Shared, chain, fields); } Ok(Value::Obj(Some(id))) } diff --git a/crates/affinity/src/tests.rs b/crates/affinity/src/tests.rs index 59ae99044a..021b532529 100644 --- a/crates/affinity/src/tests.rs +++ b/crates/affinity/src/tests.rs @@ -1,4 +1,4 @@ -//! Unit tests on synthetic inputs; `tests/real_files.rs` covers public Affinity documents. +//! Unit tests on synthetic inputs; `tests/it/real_files.rs` covers public Affinity documents. mod preview { use crate::container::header; @@ -258,6 +258,20 @@ mod archive { assert!(matches!(a.read("doc.dat"), Err(Error::Limit(_)))); } + #[test] + fn a_failed_read_still_spends_the_extraction_allowance() { + let d = doc(); + let file = synth::container(&[("doc.dat", &d, Method::Zstd)], None); + let mut broken = file.clone(); + broken[90] ^= 0x55; + // Room for exactly one read: the failed attempt must use it up, so a caller that + // recovers from the checksum error cannot make the same entry decode again. + let mut a = Archive::open(&broken, Limits { max_entry: usize::MAX, max_total: d.len(), ..Limits::default() }).unwrap(); + assert!(a.read("doc.dat").is_err()); + assert_eq!(a.extracted(), d.len()); + assert!(matches!(a.read("doc.dat"), Err(Error::Limit(_)))); + } + #[test] fn a_declared_size_smaller_than_the_data_is_rejected_not_truncated() { let big = vec![7u8; 1 << 20]; @@ -302,6 +316,17 @@ mod archive { } } + #[test] + fn fixed_records_of_zero_width_are_rejected_before_any_value_is_made() { + // Four million records of no bytes each pass a length check that multiplies by the width. + let mut f = vec![0x80 | 0x2c]; + f.extend(tag(b"Big_").0.to_le_bytes()); + f.extend(4_000_000u32.to_le_bytes()); + f.extend(0u16.to_le_bytes()); + let d = synth::stream(&[(tag(b"Big_"), F::Raw(f))]); + assert_eq!(stream::parse(&d).unwrap_err(), Error::Malformed("array longer than its data")); + } + #[test] fn a_small_compressed_entry_cannot_make_the_stream_allocate_gigabytes() { // Each decoded value takes about 40 bytes, and a bool array packs eight in a byte: 16 MiB @@ -398,6 +423,29 @@ mod shapes { } } + #[test] + fn artistic_text_reads_its_characters_runs_and_anchor() { + let glyphs = F::Obj(tag(b"Glyp"), vec![(tag(b"Utf8"), F::Str("Hi".into()))]); + let run = F::Obj(tag(b"Item"), vec![]); + let attributes = F::Obj(tag(b"GAtt"), vec![(tag(b"Runs"), F::Objs(tag(b"Runs"), vec![vec![(tag(b"Indx"), F::I32(2)), (tag(b"Item"), run)]]))]); + let block = vec![(tag(b"Glyp"), glyphs), (tag(b"GAtt"), attributes)]; + let text = F::Def( + 100, + vec![tag(b"TxtA")], + vec![ + (tag(b"TxtH"), F::Obj(tag(b"ArFr"), vec![(tag(b"FrmB"), F::F64s(vec![0.0, 0.0, 100.0, 50.0]))])), + (tag(b"StSt"), F::Obj(tag(b"StSt"), vec![(tag(b"Blok"), F::Objs(tag(b"Blok"), vec![block]))])), + ], + ); + let document = read(vec![text], 100); + let Kind::Text(t) = &document.spreads[0].nodes[0].kind else { panic!("expected text") }; + assert_eq!(t.runs.len(), 1); + assert_eq!(t.runs[0].text, "Hi"); + assert_eq!(t.align, crate::model::Align::Left); + assert_eq!(t.anchor, Point { x: 0.0, y: 0.0 }); + assert!(t.frame.is_none()); + } + #[test] fn pie_angles_with_huge_finite_values_do_not_loop_or_emit_nonfinite_points() { let document = read(vec![shape(100, b"ShPi", vec![(tag(b"AngS"), F::F64(1.0e308)), (tag(b"AngE"), F::F64(-1.0e308))])], 100); @@ -538,3 +586,213 @@ mod pixels { ); } } + +/// Spot colours that link to themselves or to each other. Each document is read on a 1 MiB +/// stack, the smallest main-thread stack PhotoCraft runs on (Windows), first in a child process +/// that must finish within 20 s. A stack overflow aborts the process it happens in, so unbounded +/// recursion fails the one test that caused it instead of taking the other tests in the binary +/// down with it. +mod spot_colours { + use std::io::{Read, Write}; + use std::process::{Command, Stdio}; + use std::time::{Duration, Instant}; + + use crate::container::Limits; + use crate::model::{Document, Kind}; + use crate::paint::{Color, Paint}; + use crate::stream::Tag; + use crate::synth::{self, F, Method, tag}; + + /// A one-page document whose page holds `top`, read in a child process first and then here. + fn read(top: Vec) -> Document { + let spread = F::Def(2, vec![tag(b"Sprd")], vec![(tag(b"SprB"), F::F64s(vec![0.0, 0.0, 400.0, 300.0])), (tag(b"Chld"), F::Shared(top))]); + let data = synth::stream(&[(tag(b"DocR"), F::Def(1, vec![tag(b"DocN")], vec![(tag(b"Chld"), F::Shared(vec![spread]))]))]); + let bytes = synth::container(&[("doc.dat", &data, Method::Zlib)], None); + let test = std::thread::current().name().unwrap_or("reader").to_owned(); + read_in_child(&bytes, &test); + read_on_small_stack(bytes, &test).unwrap() + } + + /// Reads on a 1 MiB thread named after the test, so a stack overflow message names the test. + fn read_on_small_stack(bytes: Vec, test: &str) -> Result { + let reader = std::thread::Builder::new().name(test.to_owned()).stack_size(1 << 20).spawn(move || crate::read(&bytes, Limits::default())).unwrap(); + reader.join().unwrap_or_else(|panic| std::panic::resume_unwind(panic)) + } + + /// Set in the child process to the name of the test it reads for. + const CHILD: &str = "PHOTOCRAFT_AFFINITY_READ_CHILD"; + + /// Runs `read_child` in a new process of this test binary on `bytes` and fails the test if + /// that process crashes, panics or takes over 20 s, or never read the document. + fn read_in_child(bytes: &[u8], test: &str) { + let mut child = Command::new(std::env::current_exe().unwrap()) + .args(["tests::spot_colours::read_child", "--exact", "--ignored", "--nocapture", "--test-threads=1"]) + .env(CHILD, test) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let drain = |mut pipe: Box| { + std::thread::spawn(move || { + let mut text = String::new(); + let _ = pipe.read_to_string(&mut text); + text + }) + }; + let stdout = drain(Box::new(child.stdout.take().unwrap())); + let stderr = drain(Box::new(child.stderr.take().unwrap())); + // Fed from a thread, so a child that never reads cannot block the deadline below; a child + // that dies first breaks the pipe and is reported by its exit status. + let (mut stdin, input) = (child.stdin.take().unwrap(), bytes.to_vec()); + let feed = std::thread::spawn(move || { + let _ = stdin.write_all(&input); + }); + let deadline = Instant::now() + Duration::from_secs(20); + let status = loop { + if let Some(status) = child.try_wait().unwrap() { + break Some(status); + } + if Instant::now() >= deadline { + let _ = child.kill(); + let _ = child.wait(); + break None; + } + std::thread::sleep(Duration::from_millis(10)); + }; + feed.join().unwrap(); + let output = format!("{}{}", stdout.join().unwrap(), stderr.join().unwrap()); + match status { + None => panic!("reading the document did not finish within 20 s:\n{output}"), + Some(status) if !status.success() => panic!("reading the document failed in a child process ({status}):\n{output}"), + // A filter that matched no test would also exit successfully, having read nothing. + Some(_) => assert!(output.contains(&format!("read {} bytes", bytes.len())), "the child process did not read the document:\n{output}"), + } + } + + /// Not a test of its own: `read_in_child` runs it in a child process to read the document on + /// its stdin. Without the variable, as when run with `--ignored`, it does nothing. + #[test] + #[ignore = "run by read_in_child in a child process"] + fn read_child() { + let Some(test) = std::env::var_os(CHILD) else { return }; + let mut bytes = Vec::new(); + std::io::stdin().read_to_end(&mut bytes).unwrap(); + let len = bytes.len(); + let _ = read_on_small_stack(bytes, &test.to_string_lossy()); + println!("read {len} bytes"); + } + + /// An ellipse filled with `colour`. + fn filled(id: u32, colour: F) -> F { + F::Def( + id, + vec![tag(b"ShpN")], + vec![ + (tag(b"ShpB"), F::F64s(vec![0.0, 0.0, 1.0, 1.0])), + (tag(b"Shpe"), F::Def(id + 1_000_000, vec![tag(b"ShpE")], vec![])), + (tag(b"BFFl"), F::Shared(vec![F::Def(id + 2_000_000, vec![tag(b"FilS")], vec![(tag(b"Colr"), colour)])])), + ], + ) + } + + fn spot(id: u32, fields: Vec<(Tag, F)>) -> F { + F::Def(id, vec![tag(b"Pant")], fields) + } + + fn fills(doc: &Document) -> &[Paint] { + match &doc.spreads[0].nodes[0].kind { + Kind::Shape { fills, .. } => fills, + other => panic!("expected a shape, got {other:?}"), + } + } + + fn warned(doc: &Document, what: &str) -> bool { + doc.warnings.iter().any(|w| w.contains(what)) + } + + #[test] + fn a_spot_colour_that_is_its_own_rgb_equivalent_is_refused() { + let doc = read(vec![filled(10, spot(20, vec![(tag(b"srgb"), F::Ref(20))]))]); + assert!(fills(&doc).is_empty(), "{:?}", fills(&doc)); + assert!(warned(&doc, "spot colours nested deeper than 4 levels"), "{:?}", doc.warnings); + } + + #[test] + fn spot_colours_that_are_each_others_base_and_rgb_equivalent_are_refused() { + let doc = read(vec![filled(10, spot(20, vec![(tag(b"base"), spot(21, vec![(tag(b"srgb"), F::Ref(20))]))]))]); + assert!(fills(&doc).is_empty(), "{:?}", fills(&doc)); + assert!(warned(&doc, "spot colours nested deeper than 4 levels"), "{:?}", doc.warnings); + } + + #[test] + fn spot_colours_of_spot_colours_do_not_take_exponential_time() { + // Each level's RGB equivalent and base are the next level, and the last is in an unknown + // model, so both are followed everywhere: 2^41 colour reads before the fix. + fn level(id: u32, left: u32) -> F { + if left == 0 { + return F::Def(id, vec![tag(b"XXXX")], vec![]); + } + spot(id, vec![(tag(b"srgb"), level(id + 1, left - 1)), (tag(b"base"), F::Ref(id + 1))]) + } + let doc = read(vec![filled(10, level(100, 40))]); + assert!(fills(&doc).is_empty(), "{:?}", fills(&doc)); + } + + /// `spots` spot colours, each defined through the next, around an opaque RGB colour. + fn spots(id: u32, spots: u32) -> F { + if spots == 0 { + let rgba: Vec = [1.0f32, 0.5, 0.25, 1.0].iter().flat_map(|c| c.to_le_bytes()).collect(); + return F::Def(id, vec![tag(b"RGBA")], vec![(tag(b"_col"), F::Struct(rgba))]); + } + spot(id, vec![(tag(b"srgb"), self::spots(id + 1, spots - 1))]) + } + + #[test] + fn spot_colours_nest_four_levels_and_no_deeper() { + let doc = read(vec![filled(10, spots(100, 4))]); + assert_eq!(fills(&doc), [Paint::Solid(Color::Rgb { r: 1.0, g: 0.5, b: 0.25, a: 1.0 })]); + let doc = read(vec![filled(10, spots(100, 5))]); + assert!(fills(&doc).is_empty(), "{:?}", fills(&doc)); + assert!(warned(&doc, "spot colours nested deeper than 4 levels"), "{:?}", doc.warnings); + } +} + +/// Object lookup and record arrays against the structures they replaced. +mod stream_storage { + use crate::stream::{self, Kind, Object, Tag, Value}; + use crate::synth::{self, F, tag}; + + /// The per-object map `Object::get` used before: first occurrence of each real tag. + fn reference(fields: &[(Tag, Value)], t: Tag) -> Option<&Value> { + let mut index = std::collections::HashMap::new(); + for (i, (ft, _)) in fields.iter().enumerate() { + if ft.0 != 0 { + index.entry(*ft).or_insert(i); + } + } + index.get(&t).map(|&i| &fields[i].1) + } + + #[test] + fn lookup_keeps_first_match_for_every_object_size() { + for n in 0..60u32 { + // Tags repeat (n % 7 distinct tags) and every fifth field is positional (tag 0). + let fields: Vec<(Tag, Value)> = (0..n).map(|i| (if i % 5 == 0 { Tag(0) } else { Tag(100 + i % 7) }, Value::UInt(u64::from(i)))).collect(); + let object = Object::new(Tag(1), Kind::Inline, vec![Tag(1)], fields.clone()); + for t in 0..120 { + assert_eq!(object.get(Tag(t)), reference(&fields, Tag(t)), "{n} fields, tag {t}"); + } + } + } + + #[test] + fn a_fixed_size_array_is_one_buffer_and_one_value_per_record() { + let records: Vec = (0..18 * 5).map(|i| i as u8).collect(); + let bytes = synth::stream(&[(tag(b"Nods"), F::Records(18, records.chunks(18).map(<[u8]>::to_vec).collect()))]); + let s = stream::parse(&bytes).unwrap(); + let root = s.object(s.root).unwrap(); + assert_eq!(root.get(Tag::of(b"Nods")), Some(&Value::Records { size: 18, data: records })); + assert_eq!(root.get(Tag::of(b"Nods")).and_then(Value::array_len), Some(5)); + } +} diff --git a/crates/affinity/src/text.rs b/crates/affinity/src/text.rs index c34e4e436a..96c11b4805 100644 --- a/crates/affinity/src/text.rs +++ b/crates/affinity/src/text.rs @@ -4,7 +4,7 @@ use crate::model::{Affine, Align, Point, Reader, Text, TextRun, rect}; use crate::paint::{self, Paint}; -use crate::stream::{ObjId, Value}; +use crate::stream::{ObjId, Stream, Value}; /// Characters read per text node (stories are split into blocks; this caps hostile ones). const MAX_CHARS: usize = 1 << 20; @@ -21,20 +21,7 @@ pub(crate) fn read(r: &mut Reader, id: ObjId, world: Affine) -> Option { let mut placeholders = false; for block in s.objs(story, b"Blok") { let glyphs = s.obj(block, b"Glyp")?; - let chars: Vec = match s.str(glyphs, b"Utf8") { - Some(t) => t.chars().collect(), - None => { - // Inline objects (page numbers, anchors) each take one index before the segment text. - let mut v = Vec::new(); - for seg in s.objs(glyphs, b"Mixd") { - let inline = s.objs(seg, b"Glys").len(); - v.extend(std::iter::repeat_n('\u{FFFC}', inline)); - placeholders |= inline > 0; - v.extend(s.str(seg, b"Utf8").unwrap_or_default().chars()); - } - v - } - }; + let chars = block_chars(s, glyphs, total, &mut placeholders); total = total.checked_add(chars.len())?; if total > MAX_CHARS { r.warn("text longer than a million characters (truncated)"); @@ -68,7 +55,7 @@ pub(crate) fn read(r: &mut Reader, id: ObjId, world: Affine) -> Option { _ => bounds.x0, }; if !artistic { - if s.objs(frame, b"ColW").len() > 1 || matches!(s.field(frame, b"ColW"), Some(Value::Array(a)) if a.len() > 1) { + if s.objs(frame, b"ColW").len() > 1 || s.array_len(frame, b"ColW").is_some_and(|n| n > 1) { r.warn("text frames with several columns (imported as one column)"); } if s.class(id) == Some(crate::stream::Tag::of(b"TxtC")) { @@ -78,6 +65,32 @@ pub(crate) fn read(r: &mut Reader, id: ObjId, world: Affine) -> Option { Some(Text { runs, align, anchor: Point { x: anchor_x, y: first_baseline }, frame: if artistic { None } else { Some(bounds) }, transform: world }) } +/// The characters of one text block. A block without a UTF-8 string is a list of segments: each +/// inline object (page number, anchor) takes one `U+FFFC` before its segment's text. Stops once +/// `total` plus these characters would pass the cap. +fn block_chars(s: &Stream, glyphs: ObjId, total: usize, placeholders: &mut bool) -> Vec { + // One past the allowance is enough to trip the check in `read`; both arms stop there. + let room = MAX_CHARS.saturating_sub(total).saturating_add(1); + match s.str(glyphs, b"Utf8") { + Some(t) => t.chars().take(room).collect(), + None => { + // Inline objects (page numbers, anchors) each take one index before the segment text. + let mut v = Vec::new(); + // Shared segments repeated by reference must not expand beyond the allowance first. + for seg in s.objs(glyphs, b"Mixd") { + if v.len() >= room { + break; + } + let inline = s.objs(seg, b"Glys").len(); + v.extend(std::iter::repeat_n('\u{FFFC}', inline.min(room - v.len()))); + *placeholders |= inline > 0; + v.extend(s.str(seg, b"Utf8").unwrap_or_default().chars().take(room.saturating_sub(v.len()))); + } + v + } + } +} + fn slot(v: Option<&Value>, i: usize, f: impl Fn(&Value) -> Option) -> Option { match v? { Value::Array(a) => a.get(i).and_then(f), @@ -100,6 +113,24 @@ fn int(v: &Value) -> Option { } } +/// Longest font name kept; real PostScript and family names are far shorter. +const MAX_FONT_NAME: usize = 256; + +/// A font name for one run. Runs repeat the same shared string, so its copies are charged +/// against a document-wide byte budget; an overlong name or an exhausted budget leaves it empty. +fn font_name(r: &mut Reader, font: Option, tag: &[u8; 4]) -> String { + let name = font.and_then(|f| r.s.str(f, tag)).unwrap_or_default(); + if name.len() > MAX_FONT_NAME { + r.warn("font names longer than 256 bytes (left empty)"); + return String::new(); + } + if !r.charge_font_name(name.len()) { + r.warn("font names beyond the document metadata budget (left empty)"); + return String::new(); + } + name.to_string() +} + fn run_attrs(r: &mut Reader, attrs: ObjId, text: String, world: Affine) -> TextRun { let s = r.s; let doubles = s.field(attrs, b"Doub"); @@ -119,11 +150,12 @@ fn run_attrs(r: &mut Reader, attrs: ObjId, text: String, world: Affine) -> TextR r.warn("horizontally scaled text"); } let font = s.obj(attrs, b"DFnt"); - let get = |tag: &[u8; 4]| font.and_then(|f| s.str(f, tag)).unwrap_or_default().to_string(); + let postscript = font_name(r, font, b"Post"); + let family = font_name(r, font, b"Famy"); TextRun { text, - postscript: get(b"Post"), - family: get(b"Famy"), + postscript, + family, weight: font.and_then(|f| s.int(f, b"Wegt")).unwrap_or(400), italic: font.and_then(|f| s.bool(f, b"Ital")).unwrap_or(false), size, diff --git a/crates/affinity/tests/it/compound_limits.rs b/crates/affinity/tests/it/compound_limits.rs new file mode 100644 index 0000000000..6801f7f6d6 --- /dev/null +++ b/crates/affinity/tests/it/compound_limits.rs @@ -0,0 +1,214 @@ +//! #6423: compound operands form a graph, not necessarily a tree. +#![cfg(feature = "synth")] +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +use photocraft_affinity::model::{Document, Kind}; +use photocraft_affinity::synth::{self, F, Method, tag}; +use photocraft_affinity::{Limits, read}; +use std::time::{Duration, Instant}; + +fn compound(id: u32, children: Vec) -> F { + F::Def(id, vec![tag(b"Comp")], vec![(tag(b"Chld"), F::Shared(children))]) +} + +fn rectangle() -> F { + F::Def(100, vec![tag(b"ShpN")], vec![(tag(b"ShpB"), F::F64s(vec![0.0, 0.0, 10.0, 20.0])), (tag(b"Shpe"), F::Def(101, vec![tag(b"ShNR")], vec![]))]) +} + +fn document_bytes(definitions: Vec, roots: Vec) -> Vec { + let data = synth::stream(&[ + (tag(b"Defs"), F::Shared(definitions)), + ( + tag(b"DocR"), + F::Def( + 1, + vec![tag(b"DocN")], + vec![( + tag(b"Chld"), + F::Shared(vec![F::Def( + 2, + vec![tag(b"Sprd")], + vec![(tag(b"SprB"), F::F64s(vec![0.0, 0.0, 400.0, 300.0])), (tag(b"Chld"), F::Shared(roots))], + )]), + )], + ), + ), + ]); + synth::container(&[("doc.dat", &data, Method::Zlib)], None) +} + +fn document(definitions: Vec, roots: Vec) -> Document { + read(&document_bytes(definitions, roots), Limits::default()).unwrap() +} + +// A stack overflow aborts rather than unwinds. Keep that failure (and an exponential hang) +// inside a child process, so the regression fails without killing the entire test runner. +fn isolated(name: &str, check: impl FnOnce()) { + if std::env::var("PHOTOCRAFT_COMPOUND_CASE").as_deref() == Ok(name) { + check(); + return; + } + // The harness names a test by its module path below the crate root. + let test = module_path!().split_once("::").map_or(name.to_string(), |(_, m)| format!("{m}::{name}")); + let mut child = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", &test, "--nocapture"]) + .env("PHOTOCRAFT_COMPOUND_CASE", name) + .spawn() + .unwrap(); + let start = Instant::now(); + loop { + if let Some(status) = child.try_wait().unwrap() { + assert!(status.success(), "compound import failed: {status}"); + break; + } + if start.elapsed() > Duration::from_secs(10) { + child.kill().unwrap(); + child.wait().unwrap(); + panic!("compound import exceeded ten seconds"); + } + std::thread::sleep(Duration::from_millis(20)); + } +} + +fn unsupported(doc: &Document, warning: &str) { + assert!(matches!(doc.spreads[0].nodes[0].kind, Kind::Unsupported)); + assert!(doc.warnings.iter().any(|w| w.contains(warning)), "{:?}", doc.warnings); +} + +#[test] +fn self_referencing_compound_is_reported_without_aborting() { + isolated("self_referencing_compound_is_reported_without_aborting", || { + let doc = document(vec![], vec![compound(10, vec![F::Ref(10)]), rectangle()]); + unsupported(&doc, "cyclic compound"); + assert!(matches!(doc.spreads[0].nodes[1].kind, Kind::Shape { .. }), "a cycle must not poison unrelated shapes"); + }); +} + +#[test] +fn indirectly_cyclic_compound_is_not_imported_as_a_partial_shape() { + isolated("indirectly_cyclic_compound_is_not_imported_as_a_partial_shape", || { + let doc = document(vec![rectangle()], vec![compound(10, vec![F::Ref(100), compound(11, vec![F::Ref(10)])])]); + unsupported(&doc, "cyclic compound"); + }); +} + +fn nested(levels: u32, repeated: bool) -> Document { + // Flat definitions bypass the stream parser's nesting cap: only references form the depth. + let mut definitions = vec![rectangle()]; + let mut previous = 100; + for id in 200..200 + levels { + let children = if repeated { vec![F::Ref(previous), F::Ref(previous)] } else { vec![F::Ref(previous)] }; + definitions.push(compound(id, children)); + previous = id; + } + document(definitions, vec![F::Ref(previous)]) +} + +#[test] +fn deep_compound_graph_is_bounded_on_a_small_stack() { + isolated("deep_compound_graph_is_bounded_on_a_small_stack", || { + std::thread::Builder::new().stack_size(512 * 1024).spawn(|| unsupported(&nested(1000, false), "compound depth")).unwrap().join().unwrap(); + }); +} + +#[test] +fn shared_operands_cannot_expand_exponentially() { + isolated("shared_operands_cannot_expand_exponentially", || unsupported(&nested(40, true), "compound geometry budget")); +} + +#[test] +fn shared_empty_operands_also_have_a_work_limit() { + isolated("shared_empty_operands_also_have_a_work_limit", || { + let mut definitions = vec![compound(100, vec![])]; + let mut previous = 100; + for id in 200..240 { + definitions.push(compound(id, vec![F::Ref(previous), F::Ref(previous)])); + previous = id; + } + unsupported(&document(definitions, vec![F::Ref(previous)]), "compound geometry budget"); + }); +} + +#[test] +fn geometry_budget_is_shared_across_layer_roots() { + isolated("geometry_budget_is_shared_across_layer_roots", || { + let mut definitions = vec![rectangle()]; + let mut previous = 100; + for id in 200..214 { + definitions.push(compound(id, vec![F::Ref(previous), F::Ref(previous)])); + previous = id; + } + let mut roots = vec![F::Ref(previous); 12]; + roots.push(F::Ref(100)); + let doc = document(definitions, roots); + let nodes = &doc.spreads[0].nodes; + assert!(matches!(nodes[0].kind, Kind::Shape { .. }), "a modest shared graph still imports"); + assert!(matches!(nodes.last().unwrap().kind, Kind::Unsupported), "later roots cannot reset the exhausted budget"); + assert!(doc.warnings.iter().any(|w| w.contains("compound geometry budget"))); + }); +} + +#[test] +fn ordinary_shared_operands_keep_both_transformed_copies() { + let mut translated = compound(201, vec![F::Ref(100)]); + let F::Def(_, _, fields) = &mut translated else { unreachable!() }; + fields.push((tag(b"Xfrm"), F::F64s(vec![1.0, 0.0, 30.0, 0.0, 1.0, 40.0]))); + let doc = document(vec![rectangle(), compound(200, vec![F::Ref(100)]), translated], vec![compound(202, vec![F::Ref(200), F::Ref(201)])]); + assert!(doc.warnings.is_empty(), "{:?}", doc.warnings); + let Kind::Shape { path, .. } = &doc.spreads[0].nodes[0].kind else { panic!("expected compound outline") }; + assert_eq!(path.subpaths.len(), 2); + for (a, b) in path.subpaths[0].segments.iter().flatten().zip(path.subpaths[1].segments.iter().flatten()) { + assert_eq!(b.x, a.x + 30.0); + assert_eq!(b.y, a.y + 40.0); + } +} + +#[test] +fn null_operand_slots_cannot_bypass_geometry_budget() { + isolated("null_operand_slots_cannot_bypass_geometry_budget", || { + let bytes = document_bytes(vec![compound(10, vec![F::Null; 1_000_000])], vec![F::Ref(10); 20_000]); + let start = Instant::now(); + let doc = read(&bytes, Limits::default()).unwrap(); + eprintln!("one million nulls / 20,000 placements: {} bytes, read {:.3}s", bytes.len(), start.elapsed().as_secs_f64()); + unsupported(&doc, "compound geometry budget"); + assert_eq!(doc.spreads[0].nodes.len(), 20_000); + assert!(doc.spreads[0].nodes.iter().all(|n| matches!(n.kind, Kind::Unsupported))); + }); +} + +#[test] +fn null_operand_slots_share_budget_across_layer_roots() { + isolated("null_operand_slots_share_budget_across_layer_roots", || { + let doc = document(vec![compound(10, vec![F::Null; 1_000])], vec![F::Ref(10); 1_000]); + assert!(matches!(doc.spreads[0].nodes[0].kind, Kind::Shape { .. }), "a small empty compound remains valid"); + assert!(matches!(doc.spreads[0].nodes.last().unwrap().kind, Kind::Unsupported)); + assert!(doc.warnings.iter().any(|w| w.contains("compound geometry budget"))); + }); +} + +#[test] +fn null_operand_slots_cannot_leave_a_partial_shape() { + isolated("null_operand_slots_cannot_leave_a_partial_shape", || { + let mut slots = vec![F::Ref(100)]; + slots.extend(vec![F::Null; 1_000_000]); + let doc = document(vec![rectangle(), compound(10, slots)], vec![F::Ref(10)]); + unsupported(&doc, "compound geometry budget"); + }); +} + +#[test] +fn non_object_operand_slots_also_consume_budget() { + isolated("non_object_operand_slots_also_consume_budget", || { + let comp = F::Def(10, vec![tag(b"Comp")], vec![(tag(b"Chld"), F::U8s(vec![0; 1_000_000]))]); + unsupported(&document(vec![comp], vec![F::Ref(10)]), "compound geometry budget"); + }); +} + +#[test] +fn ordinary_null_operand_slots_preserve_shared_contours() { + let doc = document(vec![rectangle(), compound(10, vec![F::Null, F::Ref(100), F::Null, F::Ref(100), F::Null])], vec![F::Ref(10)]); + assert!(doc.warnings.is_empty(), "{:?}", doc.warnings); + let Kind::Shape { path, .. } = &doc.spreads[0].nodes[0].kind else { panic!("expected compound outline") }; + assert_eq!(path.subpaths.len(), 2); + assert_eq!(path.subpaths[0], path.subpaths[1]); +} diff --git a/crates/affinity/tests/it/main.rs b/crates/affinity/tests/it/main.rs new file mode 100644 index 0000000000..5d0a5740b8 --- /dev/null +++ b/crates/affinity/tests/it/main.rs @@ -0,0 +1,4 @@ +//! The crate's integration tests, linked into one test binary. + +mod compound_limits; +mod real_files; diff --git a/crates/affinity/tests/real_files.rs b/crates/affinity/tests/it/real_files.rs similarity index 100% rename from crates/affinity/tests/real_files.rs rename to crates/affinity/tests/it/real_files.rs diff --git a/crates/algo/Cargo.toml b/crates/algo/Cargo.toml index c4f34b4b1d..767c8044d9 100644 --- a/crates/algo/Cargo.toml +++ b/crates/algo/Cargo.toml @@ -21,4 +21,10 @@ rustfft = "6.4.1" rayon = { workspace = true } [dev-dependencies] +photocraft-testkit = { workspace = true } serde_json = { workspace = true } +criterion = { workspace = true } + +[[bench]] +name = "blur" +harness = false diff --git a/crates/algo/benches/blur.rs b/crates/algo/benches/blur.rs new file mode 100644 index 0000000000..3165b40cd0 --- /dev/null +++ b/crates/algo/benches/blur.rs @@ -0,0 +1,33 @@ +//! `cargo bench -p photocraft-algo` +use criterion::{Criterion, criterion_group, criterion_main}; +use photocraft_algo::{FilterParams, apply}; +use photocraft_color::PixelFormat; +use photocraft_geom::Rect; +use photocraft_raster::Surface; + +fn textured(n: u32) -> Surface { + let r = Rect::new(0, 0, n as i32, n as i32); + let mut s = Surface::new(PixelFormat::RGBA8); + let bytes: Vec = (0..n).flat_map(|y| (0..n).flat_map(move |x| [(x ^ y) as u8, (x / 3) as u8, (y * 7) as u8, 255])).collect(); + s.write_interleaved(r, &bytes); + s +} + +/// Gaussian Blur's direct-kernel path (radius up to 4; larger radii use box passes), including +/// the variance-matched kernels below radius 1 (#8544). +fn gaussian_direct(c: &mut Criterion) { + let n = 2048; + let s = textured(n); + let area = Rect::new(0, 0, n as i32, n as i32); + for radius in [0.3f32, 0.8, 3.0] { + let p = FilterParams::GaussianBlur { radius }; + c.bench_function(&format!("gaussian_blur_{radius}px_2048"), |b| b.iter(|| apply(&s, &p, area, area, None))); + } +} + +criterion_group! { + name = benches; + config = Criterion::default().sample_size(10); + targets = gaussian_direct +} +criterion_main!(benches); diff --git a/crates/algo/data/LICENSE-libfacedetection.txt b/crates/algo/data/LICENSE-libfacedetection.txt new file mode 100644 index 0000000000..33abd64f06 --- /dev/null +++ b/crates/algo/data/LICENSE-libfacedetection.txt @@ -0,0 +1,35 @@ +By downloading, copying, installing or using the software you agree to this license. +If you do not agree to this license, do not download, install, +copy or use the software. + + + License Agreement For libfacedetection + (3-clause BSD License) + +Copyright (c) 2015-2019, Shiqi Yu, all rights reserved. +shiqi.yu@gmail.com + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + * Neither the names of the copyright holders nor the names of the contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +This software is provided by the copyright holders and contributors "as is" and +any express or implied warranties, including, but not limited to, the implied +warranties of merchantability and fitness for a particular purpose are disclaimed. +In no event shall copyright holders or contributors be liable for any direct, +indirect, incidental, special, exemplary, or consequential damages +(including, but not limited to, procurement of substitute goods or services; +loss of use, data, or profits; or business interruption) however caused +and on any theory of liability, whether in contract, strict liability, +or tort (including negligence or otherwise) arising in any way out of +the use of this software, even if advised of the possibility of such damage. \ No newline at end of file diff --git a/crates/algo/data/libfacedetection.bin b/crates/algo/data/libfacedetection.bin new file mode 100644 index 0000000000..0941c4f689 Binary files /dev/null and b/crates/algo/data/libfacedetection.bin differ diff --git a/crates/algo/examples/bench_filters2.rs b/crates/algo/examples/bench_filters2.rs index a84bb3ca59..3b9aacae24 100644 --- a/crates/algo/examples/bench_filters2.rs +++ b/crates/algo/examples/bench_filters2.rs @@ -1,13 +1,36 @@ //! Timing for the heavier second-batch filters on a ~24 MP (6000×4000) RGBA8 layer. -//! `cargo run --release -p photocraft-algo --example bench_filters2 [filter-name-substring]` -use photocraft_algo::*; +//! +//! `cargo run --release -p photocraft-algo --example bench_filters2 [name-substring] [--reps N] [--quick] [--json ]` +//! +//! `--json` writes the report `cargo xtask perf` reads: `{"rows": [{"name", "samples_ms"}]}`. +//! `--quick` runs the list on a 1500×1000 layer instead, for `cargo xtask perf --quick`. +use photocraft_algo::{ + BlurPath, BlurQuality, BlurShape, DepthSource, Distribution, FieldPin, FilterParams, IrisPin, Light, SmartBlurMode, SpinPin, TextureChannel, apply, + output_area, +}; use photocraft_color::PixelFormat; use photocraft_geom::Rect; use photocraft_raster::Surface; fn main() { - let only = std::env::args().nth(1); - let (w, h) = (6000, 4000); + let a: Vec = std::env::args().skip(1).collect(); + let flag = |k: &str| a.iter().position(|v| v == k); + let value = |k: &str| flag(k).and_then(|i| a.get(i + 1)).cloned(); + let mut only: Option = None; + let mut i = 0; + while i < a.len() { + match a[i].as_str() { + "--json" | "--reps" => i += 2, + "--quick" => i += 1, + _ => { + only.get_or_insert_with(|| a[i].clone()); + i += 1; + } + } + } + let reps: usize = value("--reps").and_then(|v| v.parse().ok()).unwrap_or(1).max(1); + let quick = flag("--quick").is_some(); + let (w, h) = if quick { (1500, 1000) } else { (6000, 4000) }; let r = Rect::new(0, 0, w, h); let mut s = Surface::new(PixelFormat::RGBA8); // Busy deterministic content (gradients plus texture) so edge-aware filters do real work. @@ -33,6 +56,8 @@ fn main() { depth_map: None, }, FilterParams::SmartBlur { radius: 3.0, threshold: 25.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal }, + FilterParams::SmartBlur { radius: 12.0, threshold: 25.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal }, + FilterParams::SmartBlur { radius: 100.0, threshold: 25.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal }, FilterParams::ShapeBlur { radius: 10.0, shape: BlurShape::Star }, FilterParams::TiltShift { blur: 15.0, center_x: 0.5, center_y: 0.5, angle: 0.0, focus: 0.1, transition: 0.15 }, FilterParams::IrisBlur { pins: vec![IrisPin::default()] }, @@ -53,14 +78,49 @@ fn main() { }, FilterParams::Relight { angle: 45.0, elevation: 40.0, intensity: 40.0, ambient: 55.0, warmth: 0.0, softness: 25.0 }, ]; + let mut rows: Vec<(String, Vec)> = Vec::new(); for p in cases { if only.as_deref().is_some_and(|o| !p.label().to_lowercase().contains(&o.to_lowercase())) { continue; } let area = output_area(&p, s.content_bounds(), r, None).intersect(&r); - let t = std::time::Instant::now(); - let out = apply(&s, &p, area, r, None); - println!("{:<18} {w}x{h} RGBA8: {:.2?}", p.label(), t.elapsed()); - drop(out); + // Smart Blur's cost peaks near radius 12, where High quality visits every pixel in the circle. + let name = match p { + FilterParams::SmartBlur { radius, .. } => format!("{} r{radius}", p.label()), + _ => p.label().to_string(), + }; + let mut samples = Vec::with_capacity(reps); + for _ in 0..reps { + let t = std::time::Instant::now(); + let out = apply(&s, &p, area, r, None); + let ms = t.elapsed().as_secs_f64() * 1e3; + samples.push(ms); + drop(out); + } + let mut sorted = samples.clone(); + sorted.sort_by(f64::total_cmp); + let median = match sorted.len() % 2 { + 1 => sorted[sorted.len() / 2], + _ => (sorted[sorted.len() / 2 - 1] + sorted[sorted.len() / 2]) / 2.0, + }; + println!("{name:<18} {w}x{h} RGBA8: {median:.1} ms{reps_note}", reps_note = if reps > 1 { format!(" (median of {reps})") } else { String::new() }); + rows.push((name, samples)); + } + if let Some(path) = value("--json") { + // Two fields per row and ASCII filter labels: hand-written rather than a serde dependency + // this crate does not otherwise need. + let body: Vec = rows + .iter() + .map(|(name, samples)| { + let s: Vec = samples.iter().map(|v| format!("{v:.4}")).collect(); + format!("{{\"name\":\"{name}\",\"samples_ms\":[{}]}}", s.join(",")) + }) + .collect(); + let text = format!("{{\"rows\":[{}]}}", body.join(",")); + if let Err(e) = std::fs::write(&path, text) { + eprintln!("write {path}: {e}"); + std::process::exit(1); + } + println!("report: {path}"); } } diff --git a/crates/algo/examples/bench_liquify.rs b/crates/algo/examples/bench_liquify.rs index 2eea3b951b..78165171f2 100644 --- a/crates/algo/examples/bench_liquify.rs +++ b/crates/algo/examples/bench_liquify.rs @@ -127,10 +127,23 @@ fn main() { photocraft_algo::puppet::ITERATIONS, t.elapsed().as_secs_f64() * 1e3 ); - let dst: Vec<[f64; 2]> = pw.pins.iter().map(|p| p.dst).collect(); - let t = Instant::now(); - let _ = solver.solve(&dst, &[None; 3], PuppetMode::Normal, 4, Some(&v)); - println!("puppet interactive re-solve (4 warm rounds): {:.2} ms", t.elapsed().as_secs_f64() * 1e3); + // Compare the old path-dependent preview with the command-equivalent drag solve, using + // changing destinations and the same cached factorization (no remeshing per event). + for (label, rounds, warm) in [("6 warm rounds", 6, true), ("canonical cold solve", photocraft_algo::puppet::ITERATIONS, false)] { + let mut previous = v.clone(); + let mut times = Vec::new(); + for step in 1..=60 { + let mut dst: Vec<[f64; 2]> = pw.pins.iter().map(|p| p.dst).collect(); + dst[1][0] -= f64::from(step) * 10.0; + dst[1][1] += (f64::from(step) * 0.1).sin() * 400.0; + let t = Instant::now(); + let (next, _) = solver.solve(&dst, &[None; 3], pw.mode, rounds, warm.then_some(previous.as_slice())); + times.push(t.elapsed().as_secs_f64() * 1e3); + previous = std::hint::black_box(next); + } + times.sort_by(f64::total_cmp); + println!("puppet drag ({label}, 60 moves): median {:.2} ms, p95 {:.2} ms, max {:.2} ms", times[30], times[56], times[59]); + } let t = Instant::now(); let _ = render(&s, b, &solver.mesh, &v, &order, Interp::Bicubic); println!("puppet render 24 MP: {:.0} ms", t.elapsed().as_secs_f64() * 1e3); diff --git a/crates/algo/examples/bench_multiband.rs b/crates/algo/examples/bench_multiband.rs new file mode 100644 index 0000000000..2e18f9457e --- /dev/null +++ b/crates/algo/examples/bench_multiband.rs @@ -0,0 +1,161 @@ +//! Time and peak memory of Photomerge's multi-band blend on overlapping RGB sources. +//! Backwards compatible: `cargo run --release -p photocraft-algo --example bench_multiband [n] [mp]`. +//! `cargo xtask perf --bench bench_multiband --quick` also collects repeatable JSON timings. +//! The peak is the entire process's resident-set high-water mark, not an estimate of allocations +//! by the blend alone (`photocraft_testkit::perf`: the kernel's mark on Linux, sampled elsewhere). +use std::path::PathBuf; +use std::time::Instant; + +use photocraft_algo::panorama::{self, RoiImage}; +use photocraft_testkit::perf::{self, RssSampler, Summary}; +use serde_json::json; + +const ROW: &str = "Photomerge multiband blend"; + +#[derive(Debug, PartialEq)] +struct Options { + n: usize, + mp: f64, + reps: usize, + json: Option, +} + +/// Parses legacy `[n] [mp]` arguments and the perf runner's `--reps`/`--json` flags. +/// Rejects missing, out-of-range, or non-finite values before allocating images. +fn options(args: impl IntoIterator) -> Result { + let mut out = Options { n: 4, mp: 24.0, reps: 1, json: None }; + let mut positional = Vec::new(); + let mut it = args.into_iter(); + while let Some(arg) = it.next() { + match arg.as_str() { + "--json" => { + let path = it.next().filter(|p| !p.starts_with('-')).ok_or("--json needs a file path")?; + out.json = Some(PathBuf::from(path)); + } + "--reps" => { + out.reps = it.next().ok_or("--reps needs an integer")?.parse().map_err(|_| "--reps needs an integer")?; + } + _ if !arg.starts_with('-') => positional.push(arg), + _ => return Err(format!("unknown option: {arg}")), + } + } + if positional.len() > 2 { + return Err("expected at most [n] [mp]".into()); + } + if let Some(n) = positional.first() { + out.n = n.parse().map_err(|_| "n must be an integer")?; + } + if let Some(mp) = positional.get(1) { + out.mp = mp.parse().map_err(|_| "mp must be a number")?; + } + if !(1..=16).contains(&out.n) || !(1..=25).contains(&out.reps) || !out.mp.is_finite() || !(0.01..=36.0).contains(&out.mp) { + return Err("expected 1..=16 images, 0.01..=36 MP per image and 1..=25 repetitions".into()); + } + Ok(out) +} + +/// Builds deterministic overlapping RGB inputs and times each multi-band blend. +/// Writes individual samples and whole-process peak RSS to JSON when requested. +fn run() -> Result<(), String> { + let o = options(std::env::args().skip(1))?; + let sh = ((o.mp * 1e6 / 1.5).sqrt().round() as usize).max(2); + let sw = sh * 3 / 2; + let step = sw / 2; + let (cw, ch) = (step * (o.n - 1) + sw, sh); + let rois: Vec = (0..o.n) + .map(|k| { + let px = (0..sw * sh * 3).map(|i| ((i / 3 % sw * 7 + i / 3 / sw * 3 + k * 31 + i % 3 * 50) % 251) as f32 / 251.0).collect(); + RoiImage { x0: (step * k) as i32, y0: 0, w: sw, h: sh, ch: 3, px, alpha: vec![1.0; sw * sh] } + }) + .collect(); + let wts: Vec> = (0..o.n) + .map(|k| (0..sw * sh).map(|i| if ((step * k + i % sw + step / 2) / step).saturating_sub(1).min(o.n - 1) == k { 1.0 } else { 0.0 }).collect()) + .collect(); + let levels = panorama::blend_levels(cw, ch).min(7); + let before = perf::process_peak_rss_bytes().or_else(perf::current_rss_bytes); + let sampler = RssSampler::start(std::time::Duration::from_millis(2)); + let mut samples_ms = Vec::with_capacity(o.reps); + for _ in 0..o.reps { + let t0 = Instant::now(); + let output = panorama::multiband(cw, ch, &rois, &wts, levels); + samples_ms.push(t0.elapsed().as_secs_f64() * 1000.0); + std::hint::black_box(&output); + } + let peak = perf::process_peak_rss_bytes().or(sampler.peak()); + let delta = before.zip(peak).map(|(a, b)| b.saturating_sub(a) as f64 / (1024.0 * 1024.0)); + // The same nearest-rank p50 that `cargo xtask perf` reports for these samples. + let p50 = Summary::of(&samples_ms).map_or(f64::NAN, |s| s.p50); + println!( + "{} × {}×{} ({:.2} MP) RGB, canvas {}×{}, {} levels: {:.1} ms p50 ({} runs), peak +{}", + o.n, + sw, + sh, + o.mp, + cw, + ch, + levels, + p50, + o.reps, + delta.map_or("n/a".to_string(), |m| format!("{m:.0} MiB")) + ); + if let Some(path) = o.json { + let context = json!({"sources": o.n, "mp_per_source": o.mp, "canvas_width": cw, "canvas_height": ch, "levels": levels}); + let report = perf::report("bench_multiband", context, vec![perf::row(ROW, &samples_ms, peak, None)], Some(&sampler)); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?; + } + std::fs::write(&path, serde_json::to_vec_pretty(&report).map_err(|e| e.to_string())?).map_err(|e| format!("write {}: {e}", path.display()))?; + } + Ok(()) +} + +/// Runs the benchmark, reporting argument or output errors with a failing exit status. +fn main() { + if let Err(e) = run() { + eprintln!("bench_multiband: {e}"); + std::process::exit(2); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Converts borrowed argument slices for the owned-string command-line parser. + fn parse(args: &[&str]) -> Result { + options(args.iter().map(|s| s.to_string())) + } + + /// Confirms backwards compatibility with the positional and perf-runner forms. + #[test] + fn parses_old_positional_args_and_perf_runner_args() { + assert_eq!(parse(&[]).unwrap().n, 4); + assert_eq!( + parse(&["2", "0.25", "--reps", "3", "--json", "out.json"]).unwrap(), + Options { n: 2, mp: 0.25, reps: 3, json: Some(PathBuf::from("out.json")) } + ); + // The 24-36 MP range AGENTS.md benchmarks at, as before the flags were added. + assert_eq!(parse(&["4", "36"]).unwrap().mp, 36.0); + } + + /// Checks invalid counts, non-finite sizes, repetitions, and missing flag values. + #[test] + fn rejects_bad_benchmark_args() { + for args in [ + vec!["0"], + vec!["17"], + vec!["2", "0"], + vec!["2", "NaN"], + vec!["2", "inf"], + vec!["2", "37"], + vec!["--reps", "0"], + vec!["--reps", "26"], + vec!["--json"], + vec!["--json", "--reps", "5"], + vec!["--reps"], + vec!["--unknown"], + ] { + assert!(parse(&args).is_err(), "{args:?}"); + } + } +} diff --git a/crates/algo/examples/bench_photo.rs b/crates/algo/examples/bench_photo.rs index dc0354886d..11d29e9afd 100644 --- a/crates/algo/examples/bench_photo.rs +++ b/crates/algo/examples/bench_photo.rs @@ -122,6 +122,24 @@ fn main() { }; wideangle::apply(&s, r, &p, Interp::Bicubic) }); + // The mesh solve alone, on the dialog's 800 x 533 proxy. + let proxy = Rect::new(0, 0, 800, 533); + let solve_case = |constraints: Vec| { + let p = WideAngle { model: WideModel::Fisheye, focal_length: 12.0, constraints, ..Default::default() }; + wideangle::solve(&p, proxy) + }; + time("wide angle solve: 800x533 proxy, H + V", &only, || { + solve_case(vec![ + Constraint { a: [60.0, 80.0], b: [740.0, 80.0], orientation: Orientation::Horizontal }, + Constraint { a: [100.0, 60.0], b: [100.0, 470.0], orientation: Orientation::Vertical }, + ]) + }); + time("wide angle solve: 800x533 proxy, 2 free", &only, || { + solve_case(vec![ + Constraint { a: [60.0, 80.0], b: [740.0, 95.0], orientation: Orientation::Free }, + Constraint { a: [100.0, 60.0], b: [110.0, 470.0], orientation: Orientation::Free }, + ]) + }); time("hdr: merge 3 exposures + local adaptation", &only, || { let shots: Vec> = [0.5f32, 1.0, 2.0].iter().map(|k| px.iter().map(|q| [(q[0] * k).min(1.0), (q[1] * k).min(1.0), (q[2] * k).min(1.0), 1.0]).collect()).collect(); diff --git a/crates/algo/examples/bench_pixelate.rs b/crates/algo/examples/bench_pixelate.rs index a37e8f0775..5c81d5d325 100644 --- a/crates/algo/examples/bench_pixelate.rs +++ b/crates/algo/examples/bench_pixelate.rs @@ -1,5 +1,6 @@ //! Full filter timing, including tile reads, filtering, writeback and pruning. //! RAYON_NUM_THREADS=16 cargo run --release -p photocraft-algo --example bench_pixelate -- facet 6000 4000 u8 3 output.bin +//! Also: crystallize, crystallize-large, pointillize, pointillize-large. //! Optional final argument after output.bin: rgb (default), gray, cmyk or lab. //! Run the same example at the base commit and the PR commit, then compare output.bin with cmp. use photocraft_algo::{FilterParams, apply_in}; @@ -29,11 +30,15 @@ fn main() -> Result<(), Box> { _ => return Err("depth must be u8, u16 or f32".into()), }; let params = match case { + "crystallize" => FilterParams::Crystallize { cell_size: 10.0, seed: 37 }, + "crystallize-large" => FilterParams::Crystallize { cell_size: 100.0, seed: 37 }, + "pointillize" => FilterParams::Pointillize { cell_size: 5.0, seed: 37, background: [1.0; 4] }, + "pointillize-large" => FilterParams::Pointillize { cell_size: 100.0, seed: 37, background: [1.0; 4] }, "facet" => FilterParams::Facet, "halftone" => FilterParams::ColorHalftone { max_radius: 8.0, angles: [108.0, 162.0, 90.0, 45.0] }, "halftone-small" => FilterParams::ColorHalftone { max_radius: 2.0, angles: [108.0, 162.0, 90.0, 45.0] }, "halftone-large" => FilterParams::ColorHalftone { max_radius: 64.0, angles: [108.0, 162.0, 90.0, 45.0] }, - _ => return Err("case must be facet, halftone, halftone-small or halftone-large".into()), + _ => return Err("unknown pixelate benchmark case".into()), }; let mode = match args.get(6).map(String::as_str).unwrap_or("rgb") { "rgb" => ColorMode::Rgb, diff --git a/crates/algo/examples/bench_seam.rs b/crates/algo/examples/bench_seam.rs new file mode 100644 index 0000000000..50225c4b50 --- /dev/null +++ b/crates/algo/examples/bench_seam.rs @@ -0,0 +1,32 @@ +//! Timing and output hash for Content-Aware Scale's seam carving core (P28). +//! +//! `cargo run --release -p photocraft-algo --example bench_seam [width height new_width channels]` +//! +//! Prints the carve time and an FNV-1a hash of the output's raw f32 bits, so two builds can be +//! compared with `cmp`-level strictness without writing tens of megabytes of pixels to disk. +use std::time::Instant; + +fn main() { + let a: Vec = std::env::args().skip(1).collect(); + let num = |i: usize, d: usize| a.get(i).and_then(|s| s.parse().ok()).unwrap_or(d); + let (w, h, new_w, ch) = (num(0, 6000), num(1, 4000), num(2, 5400), num(3, 4)); + if !(1..=4).contains(&ch) { + eprintln!("channels must be 1..=4"); + std::process::exit(2); + } + // Deterministic photo-ish content: gradients with fine detail, so the energy map carries + // structure instead of one flat value per row. + let img: Vec = (0..w * h) + .flat_map(|i| { + let (x, y) = ((i % w) as f32, (i / w) as f32); + let d = ((x * 0.13).sin() * (y * 0.07).cos()).abs() * 0.3; + [(x / w as f32) * 0.6 + d, (y / h as f32) * 0.9 - d, d, 1.0].into_iter().take(ch) + }) + .collect(); + let t = Instant::now(); + let out = photocraft_algo::seam::carve_width(w, h, ch, &img, None, new_w); + let ms = t.elapsed().as_secs_f64() * 1e3; + let hash = + out.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, v| v.to_bits().to_le_bytes().iter().fold(h, |h, b| (h ^ u64::from(*b)).wrapping_mul(0x100_0000_01b3))); + println!("{w}x{h}x{ch} -> {new_w}: {ms:.1} ms, {} samples, fnv {hash:016x}", out.len()); +} diff --git a/crates/algo/examples/bench_seam_cas.rs b/crates/algo/examples/bench_seam_cas.rs new file mode 100644 index 0000000000..b8acbfe3f2 --- /dev/null +++ b/crates/algo/examples/bench_seam_cas.rs @@ -0,0 +1,78 @@ +//! Timing of Content-Aware Scale's seam carving alone (no document plumbing), on a synthetic +//! photo-like RGBA image: perf scenario P28's 24 MP width change (#211), the same image's height +//! (the transposed pass) and two images short enough for the exact full-resolution seam search. +//! Prints an FNV-style hash of each output's bits (over 32-bit words), so a speed-up can be checked +//! to choose exactly the same seams. +//! `cargo run --release -p photocraft-algo --example bench_seam_cas [reps] [case filter]` +use std::time::Instant; + +use photocraft_algo::seam::carve; + +/// Smooth gradients, a few hard-edged shapes and fine noise: enough structure that seams are +/// steered by content, as in a photo. +fn photo(w: usize, h: usize) -> Vec { + let mut seed = 0x9e37_79b9u32; + let mut noise = move || { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + (seed % 1000) as f32 / 1000.0 - 0.5 + }; + let mut img = Vec::with_capacity(w * h * 4); + for y in 0..h { + for x in 0..w { + let (u, v) = (x as f32 / w as f32, y as f32 / h as f32); + let sky = 0.35 + 0.4 * v; + let disc = ((u - 0.3).powi(2) + (v - 0.55).powi(2)) < 0.02; + let bar = (0.62..0.66).contains(&u) && v > 0.3; + let hill = v > 0.7 + 0.08 * (u * 11.0).sin(); + let (r, g, b) = if disc { + (0.85, 0.3, 0.2) + } else if bar { + (0.1, 0.1, 0.12) + } else if hill { + (0.2, 0.45 + 0.1 * (u * 40.0).sin(), 0.15) + } else { + (sky * 0.6, sky * 0.8, sky) + }; + let n = noise() * 0.02; + img.extend_from_slice(&[r + n, g + n, b + n, 1.0]); + } + } + img +} + +fn fnv(v: &[f32]) -> u64 { + v.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, f| (h ^ u64::from(f.to_bits())).wrapping_mul(0x0100_0000_01b3)) +} + +fn main() { + let args: Vec = std::env::args().collect(); + let reps = args.get(1).and_then(|s| s.parse::().ok()).unwrap_or(1).clamp(1, 20); + let only = args.get(2).cloned(); + // (name, w, h, new w, new h) + let cases = [ + ("24 MP to 90 % width (P28, proxy rows)", 6000, 4000, 5400, 4000), + ("24 MP to 90 % height (transposed, proxy rows)", 6000, 4000, 6000, 3600), + ("5.4 MP to 90 % width (exact)", 3000, 1800, 2700, 1800), + ("1.5 MP to 70 % width (exact)", 1500, 1000, 1050, 1000), + ]; + for (name, w, h, nw, nh) in cases { + if only.as_ref().is_some_and(|o| !name.contains(o.as_str())) { + continue; + } + let img = photo(w, h); + let mut times = Vec::with_capacity(reps); + let mut hash = 0; + for _ in 0..reps { + let t = Instant::now(); + let out = carve(w, h, 4, &img, None, nw, nh); + times.push(t.elapsed().as_secs_f64() * 1000.0); + hash = fnv(&out); + } + times.sort_by(f64::total_cmp); + let p50 = times[times.len() / 2]; + let max = times[times.len() - 1]; + println!("{name:<48} p50 {p50:>9.1} ms max {max:>9.1} ms output {hash:016x}"); + } +} diff --git a/crates/algo/examples/bench_selection_morphology.rs b/crates/algo/examples/bench_selection_morphology.rs new file mode 100644 index 0000000000..71fbc7438e --- /dev/null +++ b/crates/algo/examples/bench_selection_morphology.rs @@ -0,0 +1,64 @@ +//! Compare the former full-canvas formulas with cropped morphology on 36 MP. +//! Run in release on CI; includes output allocation and checks exact equality. +use photocraft_algo::selection::{border, contract, edt, expand}; +use std::{hint::black_box, time::Instant}; + +type Op = fn(&[f32], usize, usize, f32) -> Vec; + +fn old_expand(m: &[f32], w: usize, h: usize, r: f32) -> Vec { + let seeds: Vec = m.iter().map(|v| *v >= 0.5).collect(); + m.iter().zip(edt(&seeds, w, h)).map(|(v, d)| v.max((r + 1.0 - d).clamp(0.0, 1.0))).collect() +} +fn old_contract(m: &[f32], w: usize, h: usize, r: f32) -> Vec { + let inv: Vec = m.iter().map(|v| 1.0 - v).collect(); + old_expand(&inv, w, h, r).into_iter().map(|v| 1.0 - v).collect() +} +fn old_border(m: &[f32], w: usize, h: usize, r: f32) -> Vec { + let inside: Vec = m.iter().map(|v| *v >= 0.5).collect(); + let outside: Vec = inside.iter().map(|v| !v).collect(); + let (di, dout) = (edt(&inside, w, h), edt(&outside, w, h)); + inside.iter().zip(di.iter().zip(&dout)).map(|(s, (a, b))| (1.0 - ((if *s { b } else { a }) - 1.0) / r).clamp(0.0, 1.0)).collect() +} +fn main() -> Result<(), Box> { + let (w, h) = (6000, 6000); + println!("36 MP; median of three runs including allocation; RAYON_NUM_THREADS={}", std::env::var("RAYON_NUM_THREADS").unwrap_or_default()); + for (case, size) in [("sparse", 50), ("dense", 4000), ("full", 6000)] { + let mut m = vec![0.0; w * h]; + let start = (w - size) / 2; + for row in m.chunks_exact_mut(w).skip(start).take(size) { + if let Some(span) = row.get_mut(start..start + size) { + span.fill(1.0) + } + } + for r in [3.0, 50.0] { + for (name, old, new) in + [("expand", old_expand as Op, expand as Op), ("contract", old_contract as Op, contract as Op), ("border", old_border as Op, border as Op)] + { + let mut timings = [0.0; 2]; + let expected = old(&m, w, h, r); + if new(&m, w, h, r) != expected { + return Err(format!("output mismatch: {case} {name} {r}").into()); + } + drop(expected); + for (slot, op) in timings.iter_mut().zip([old, new]) { + let mut runs = [0.0; 3]; + for time in &mut runs { + let now = Instant::now(); + let result = black_box(op(black_box(&m), w, h, r)); + *time = now.elapsed().as_secs_f64() * 1000.0; + drop(result); + } + runs.sort_by(f64::total_cmp); + *slot = runs.get(1).copied().unwrap_or_default(); + } + println!( + "{case} {name} r={r}: old={:.2}ms new={:.2}ms speedup={:.2}x exact=true", + timings.first().copied().unwrap_or_default(), + timings.get(1).copied().unwrap_or_default(), + timings.first().copied().unwrap_or_default() / timings.get(1).copied().unwrap_or(1.0) + ); + } + } + } + Ok(()) +} diff --git a/crates/algo/examples/bench_stack_entropy.rs b/crates/algo/examples/bench_stack_entropy.rs new file mode 100644 index 0000000000..b496b69d3f --- /dev/null +++ b/crates/algo/examples/bench_stack_entropy.rs @@ -0,0 +1,104 @@ +//! Stack Mode Entropy timings and exact output hashes, including lookup preparation. +//! Run the same example on both revisions: `cargo run --release -p photocraft-algo --example +//! bench_stack_entropy -- [--quick] [--reps N] [--threads N] [--json path]`. +//! Full cases use 24/36 MP; quick cases use 1/16 as many pixels. The deep-stack fallback is +//! deliberately small. Input generation, output hashing and deallocation are outside timings. + +use std::path::PathBuf; +use std::time::Instant; + +use photocraft_algo::stack::{Stat, combine}; +use photocraft_testkit::perf::{self, Summary}; +use serde_json::json; + +struct Options { + quick: bool, + reps: usize, + threads: Option, + json: Option, +} + +fn options() -> Result { + let mut o = Options { quick: false, reps: 5, threads: None, json: None }; + let mut args = std::env::args().skip(1); + while let Some(arg) = args.next() { + match arg.as_str() { + "--quick" => o.quick = true, + "--reps" => o.reps = args.next().ok_or("--reps needs an integer")?.parse().map_err(|_| "invalid --reps")?, + "--threads" => o.threads = Some(args.next().ok_or("--threads needs an integer")?.parse().map_err(|_| "invalid --threads")?), + "--json" => o.json = Some(args.next().ok_or("--json needs a path")?.into()), + _ => return Err(format!("unknown option: {arg}")), + } + } + if !(1..=25).contains(&o.reps) || o.threads.is_some_and(|n| !(1..=64).contains(&n)) { + return Err("expected 1..=25 repetitions and 1..=64 threads".into()); + } + Ok(o) +} + +fn benchmark(o: &Options) -> Result<(), String> { + let mut rows = Vec::new(); + let cases = [ + ("3 layers opaque 24 MP", 3, 24_000_000, false), + ("7 layers varying alpha 24 MP", 7, 24_000_000, true), + ("3 layers opaque 36 MP", 3, 36_000_000, false), + ("257 layers varying alpha", 257, 8192, true), + ]; + for (name, count, pixels, masked) in cases { + let pixels = if o.quick && pixels > 8192 { pixels / 16 } else { pixels }; + let layers: Vec> = (0..count) + .map(|k| { + (0..pixels) + .map(|i| { + [ + ((i * 31 + k * 17) % 256) as f32 / 255.0, + ((i / 7 + k * 29) % 32) as f32 / 31.0, + ((i + k * 13) % 1024) as f32 / 255.0 - 0.5, + if masked && k > i % (count + 1) { 0.0 } else { 1.0 }, + ] + }) + .collect() + }) + .collect(); + // Warm the worker pool and input pages before collecting samples. + std::hint::black_box(combine(&layers, Stat::Entropy)); + let mut samples = Vec::with_capacity(o.reps); + let mut hash = 0u64; + for rep in 0..o.reps { + let start = Instant::now(); + let out = std::hint::black_box(combine(&layers, Stat::Entropy)); + samples.push(start.elapsed().as_secs_f64() * 1000.0); + if rep == 0 { + hash = out.iter().flatten().fold(0xcbf29ce484222325, |h, v| (h ^ u64::from(v.to_bits())).wrapping_mul(0x100000001b3)); + } + } + let p50 = Summary::of(&samples).map_or(0.0, |s| s.p50); + println!("{name}: {pixels} pixels, {p50:.3} ms p50, hash {hash:016x}, samples_ms={samples:?}"); + let mut row = perf::row(name, &samples, perf::process_peak_rss_bytes(), None); + row["output_hash"] = json!(format!("{hash:016x}")); + row["pixels"] = json!(pixels); + rows.push(row); + } + if let Some(path) = &o.json { + let report = perf::report("bench_stack_entropy", json!({"quick": o.quick, "threads": o.threads}), rows, None); + std::fs::write(path, serde_json::to_vec_pretty(&report).map_err(|e| e.to_string())?).map_err(|e| format!("write {}: {e}", path.display()))?; + } + Ok(()) +} + +fn run() -> Result<(), String> { + let o = options()?; + #[cfg(not(target_arch = "wasm32"))] + if let Some(threads) = o.threads { + let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().map_err(|e| e.to_string())?; + return pool.install(|| benchmark(&o)); + } + benchmark(&o) +} + +fn main() { + if let Err(e) = run() { + eprintln!("{e}"); + std::process::exit(1); + } +} diff --git a/crates/algo/src/art_history.rs b/crates/algo/src/art_history.rs new file mode 100644 index 0000000000..d5895a466f --- /dev/null +++ b/crates/algo/src/art_history.rs @@ -0,0 +1,658 @@ +//! Oriented history-state dabs: sample a snapshot patch, pose it along the stroke, and stamp. +//! +//! Tile processing reads each dab's rotated source straight from the history state, so a dab +//! that crosses a tile edge paints the same as an untiled pass. Scatter is seeded, so the same +//! stroke is replayable. + +#![allow(clippy::needless_range_loop)] + +/// Mixes the history-state index into dab scatter so two documents with different snapshots diverge. +pub const RNG_SEED: u32 = 0xA7B1_57E0; + +/// Largest number of dabs one stroke may emit. +pub const MAX_DABS: usize = 4096; + +/// Largest coverage rectangle, in pixels, a stroke may allocate. +pub const MAX_COVERAGE: u64 = 4096 * 4096; + +/// Why [`stamp`] refused to write. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum StampError { + /// A zero-sized buffer, no dabs, or nothing to cover. + Empty, + /// Source and destination disagree on channel count. + Mismatched, + /// The read/write window would exceed [`MAX_COVERAGE`]. + TooLarge, +} + +/// Named dab layout. Multipliers are documented in the command params; keep them stable. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Style { + TightShort, + TightMedium, + TightLong, + LooseMedium, + LooseLong, + Dab, + TightCurl, + LooseCurl, +} + +impl Style { + /// Parse a command-style identifier (`tightMedium`, …). + pub fn parse(name: &str) -> Option { + Some(match name { + "tightShort" => Self::TightShort, + "tightMedium" => Self::TightMedium, + "tightLong" => Self::TightLong, + "looseMedium" => Self::LooseMedium, + "looseLong" => Self::LooseLong, + "dab" => Self::Dab, + "tightCurl" => Self::TightCurl, + "looseCurl" => Self::LooseCurl, + _ => return None, + }) + } + + /// Stable id used in command params. + pub fn as_str(self) -> &'static str { + match self { + Self::TightShort => "tightShort", + Self::TightMedium => "tightMedium", + Self::TightLong => "tightLong", + Self::LooseMedium => "looseMedium", + Self::LooseLong => "looseLong", + Self::Dab => "dab", + Self::TightCurl => "tightCurl", + Self::LooseCurl => "looseCurl", + } + } + + fn spacing_mul(self) -> f32 { + match self { + Self::TightShort => 0.35, + Self::TightMedium => 0.55, + Self::TightLong => 0.85, + Self::LooseMedium => 0.70, + Self::LooseLong => 1.10, + Self::Dab => 1.60, + Self::TightCurl => 0.50, + Self::LooseCurl => 0.80, + } + } + + fn scatter_mul(self) -> f32 { + match self { + Self::TightShort => 0.00, + Self::TightMedium => 0.00, + Self::TightLong => 0.05, + Self::LooseMedium => 0.45, + Self::LooseLong => 0.70, + Self::Dab => 0.10, + Self::TightCurl => 0.05, + Self::LooseCurl => 0.40, + } + } + + fn curl_deg(self) -> f32 { + match self { + Self::TightCurl => 18.0, + Self::LooseCurl => 28.0, + _ => 0.0, + } + } + + fn size_mul(self) -> f32 { + match self { + Self::TightShort => 0.7, + Self::TightMedium => 0.9, + Self::TightLong => 1.1, + Self::LooseMedium => 1.0, + Self::LooseLong => 1.2, + Self::Dab => 1.0, + Self::TightCurl => 0.9, + Self::LooseCurl => 1.1, + } + } +} + +/// One posed dab in document pixels. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Dab { + pub x: f32, + pub y: f32, + pub theta: f32, + pub radius: f32, + pub pressure: f32, +} + +/// Spacing along the stroke, in pixels. +pub fn spacing_px(size: f32, area: f32, style: Style) -> Option { + if !(size.is_finite() && area.is_finite()) || size <= 0.0 { + return None; + } + let s = size * style.spacing_mul() * (0.25 + 0.02 * area); + if s.is_finite() && s > 0.0 { Some(s) } else { None } +} + +/// Reach of a dab around its stroke point: half the dab plus its scatter, plus a margin. +pub fn halo_radius(size: f32, style: Style) -> Option { + if !size.is_finite() || size <= 0.0 { + return None; + } + let v = (size * style.size_mul() / 2.0 + style.scatter_mul() * size + 2.0).ceil(); + if !v.is_finite() || v > 1_000_000.0 { + return None; + } + Some(v as i32) +} + +/// Side of the rotated source square (`ceil(size · size_mul) + 2`). +pub fn sample_side(size: f32, style: Style) -> Option { + if !size.is_finite() || size <= 0.0 { + return None; + } + let v = (size * style.size_mul()).ceil() + 2.0; + if !v.is_finite() || v > 1_000_000.0 { + return None; + } + Some(v as i32) +} + +fn mix32(seed: u32, dab: u32, lane: u32) -> u32 { + let mut x = seed ^ dab.wrapping_mul(0x9E37_79B9) ^ lane.wrapping_mul(0x85EB_CA6B); + x = x.wrapping_add(0x9E37_79B9); + x ^= x >> 16; + x = x.wrapping_mul(0x7FEB_352D); + x ^= x >> 15; + x = x.wrapping_mul(0x846C_A68B); + x ^= x >> 16; + x +} + +fn signed01(seed: u32, dab: u32, lane: u32) -> f32 { + let u = mix32(seed, dab, lane) >> 8; + (u as f32) * (2.0 / 16_777_215.0) - 1.0 +} + +fn dab_radius(size: f32, style: Style) -> Option { + let r = size * style.size_mul() * 0.5; + if r.is_finite() && r > 0.0 { Some(r) } else { None } +} + +/// Walk `points` and emit posed dabs. `seed` is typically [`RNG_SEED`] XOR the history index. +pub fn place_dabs(points: &[[f32; 2]], size: f32, area: f32, style: Style, seed: u32) -> Result, StampError> { + if points.is_empty() { + return Err(StampError::Empty); + } + let spacing = spacing_px(size, area, style).ok_or(StampError::Empty)?; + let radius = dab_radius(size, style).ok_or(StampError::TooLarge)?; + let curl = style.curl_deg().to_radians(); + let scatter = style.scatter_mul(); + let mut out = Vec::new(); + let mut emit = |i: u32, x: f32, y: f32, tx: f32, ty: f32, pressure: f32| -> Result<(), StampError> { + if !(x.is_finite() && y.is_finite() && tx.is_finite() && ty.is_finite()) { + return Err(StampError::Empty); + } + let u = signed01(seed, i, 1); + let v = signed01(seed, i, 2); + let w = signed01(seed, i, 3); + let dx = scatter * size * u; + let dy = scatter * size * v; + let (cx, cy) = (x + dx, y + dy); + if !(cx.is_finite() && cy.is_finite()) { + return Err(StampError::TooLarge); + } + let theta = ty.atan2(tx) + (i as f32) * curl + scatter * w; + if !theta.is_finite() { + return Err(StampError::Empty); + } + if out.len() >= MAX_DABS { + return Err(StampError::TooLarge); + } + out.push(Dab { x: cx, y: cy, theta, radius, pressure: pressure.clamp(0.0, 1.0) }); + Ok(()) + }; + + let Some(first) = points.first() else { + return Err(StampError::Empty); + }; + if !first[0].is_finite() || !first[1].is_finite() { + return Err(StampError::Empty); + } + let (mut px, mut py) = (first[0], first[1]); + let (mut tx, mut ty) = (1.0f32, 0.0f32); + if let Some(second) = points.get(1) { + tx = second[0] - px; + ty = second[1] - py; + if tx == 0.0 && ty == 0.0 { + tx = 1.0; + } + } + emit(0, px, py, tx, ty, 1.0)?; + + let mut carry = 0.0f32; + let mut i = 1u32; + for w in points.windows(2) { + let (Some(a), Some(b)) = (w.first(), w.get(1)) else { continue }; + if !(a[0].is_finite() && a[1].is_finite() && b[0].is_finite() && b[1].is_finite()) { + return Err(StampError::Empty); + } + let (dx, dy) = (b[0] - a[0], b[1] - a[1]); + let len = dx.hypot(dy); + if !len.is_finite() { + return Err(StampError::Empty); + } + if len < 1.0e-6 { + continue; + } + tx = dx; + ty = dy; + let mut consumed = 0.0f32; + while carry + (len - consumed) >= spacing { + let need = spacing - carry; + if !need.is_finite() || need < 0.0 { + return Err(StampError::Empty); + } + consumed += need; + if consumed > len { + break; + } + let t = consumed / len; + px = a[0] + dx * t; + py = a[1] + dy * t; + emit(i, px, py, tx, ty, 1.0)?; + i = i.saturating_add(1); + carry = 0.0; + } + carry += len - consumed; + if !carry.is_finite() { + return Err(StampError::Empty); + } + } + if out.is_empty() { Err(StampError::Empty) } else { Ok(out) } +} + +/// Bounding rectangle of a dab, in document pixels (1 px of anti-alias slack). +pub fn dab_bounds(dab: &Dab) -> Option<(i32, i32, i32, i32)> { + let r = dab.radius + 1.0; + if !r.is_finite() { + return None; + } + let x0 = (dab.x - r).floor(); + let y0 = (dab.y - r).floor(); + let x1 = (dab.x + r).ceil(); + let y1 = (dab.y + r).ceil(); + if !(x0.is_finite() && y0.is_finite() && x1.is_finite() && y1.is_finite()) { + return None; + } + let clamp = |v: f32| v.clamp(i32::MIN as f32, i32::MAX as f32) as i32; + Some((clamp(x0), clamp(y0), clamp(x1), clamp(y1))) +} + +fn coverage_at(dab: &Dab, x: i32, y: i32, hardness: f32) -> f32 { + let dx = x as f32 + 0.5 - dab.x; + let dy = y as f32 + 0.5 - dab.y; + let d = dx.hypot(dy); + if !d.is_finite() || d >= dab.radius + 0.5 { + return 0.0; + } + let edge = (dab.radius + 0.5 - d).clamp(0.0, 1.0); + let h = hardness.clamp(0.0, 1.0); + let inner = dab.radius * h; + let falloff = if d <= inner || dab.radius - inner < 1.0e-3 { + 1.0 + } else { + let t = ((d - inner) / (dab.radius - inner)).clamp(0.0, 1.0); + 1.0 - t * t * (3.0 - 2.0 * t) + }; + edge * falloff +} + +fn pixel_index(x: usize, y: usize, w: usize, h: usize, ch: usize) -> Option { + if x >= w || y >= h || ch == 0 { + return None; + } + y.checked_mul(w)?.checked_add(x)?.checked_mul(ch) +} + +fn origin_index(origin: (i32, i32), x: i32, y: i32, w: usize, h: usize, ch: usize) -> Option { + let dx = x.checked_sub(origin.0)?; + let dy = y.checked_sub(origin.1)?; + pixel_index(usize::try_from(dx).ok()?, usize::try_from(dy).ok()?, w, h, ch) +} + +fn floor_i32(v: f32) -> i32 { + v.clamp(-2_147_483_648.0, 2_147_483_647.0).floor() as i32 +} + +struct SrcView<'a> { + data: &'a [f32], + w: usize, + h: usize, + origin: (i32, i32), + ch: usize, +} + +fn sample_bilinear(src: SrcView<'_>, sx: f32, sy: f32, out: &mut [f32]) -> bool { + let ch = src.ch; + if ch == 0 || out.len() < ch { + return false; + } + let dst = &mut out[..ch]; + dst.fill(0.0); + if !(sx.is_finite() && sy.is_finite()) { + return false; + } + // `sx, sy` are continuous coordinates; pixel `i` has its centre at `i + 0.5`. + let lx = sx - src.origin.0 as f32 - 0.5; + let ly = sy - src.origin.1 as f32 - 0.5; + if !(lx.is_finite() && ly.is_finite()) { + return false; + } + let x0 = floor_i32(lx); + let y0 = floor_i32(ly); + let fx = (lx - x0 as f32).clamp(0.0, 1.0); + let fy = (ly - y0 as f32).clamp(0.0, 1.0); + let mut any = false; + for (iy, wy) in [(0i32, 1.0 - fy), (1i32, fy)] { + for (ix, wx) in [(0i32, 1.0 - fx), (1i32, fx)] { + let wt = wx * wy; + if wt <= 0.0 { + continue; + } + let px = x0.saturating_add(ix); + let py = y0.saturating_add(iy); + if px < 0 || py < 0 { + continue; + } + let (Ok(ux), Ok(uy)) = (usize::try_from(px), usize::try_from(py)) else { continue }; + let Some(base) = pixel_index(ux, uy, src.w, src.h, ch) else { continue }; + let Some(pxs) = src.data.get(base..base.saturating_add(ch)) else { continue }; + for (d, s) in dst.iter_mut().zip(pxs.iter()) { + *d += *s * wt; + } + any = true; + } + } + any +} + +fn rotated_source(dab: &Dab, x: i32, y: i32) -> (f32, f32) { + let dx = x as f32 + 0.5 - dab.x; + let dy = y as f32 + 0.5 - dab.y; + let (c, s) = (dab.theta.cos(), dab.theta.sin()); + (dab.x + c * dx + s * dy, dab.y - s * dx + c * dy) +} + +fn over_native(dst: &mut [f32], src: &[f32], k: f32, alpha: Option, lock: bool) { + if k <= 0.0 { + return; + } + match alpha { + Some(a) => { + let Some((&da, &sa0)) = dst.get(a).zip(src.get(a)) else { return }; + let sa = sa0 * k; + let oa = sa + da * (1.0 - sa); + if oa > 0.0 { + for c in 0..a { + let Some(d) = dst.get_mut(c) else { continue }; + let Some(&s) = src.get(c) else { continue }; + *d = (s * sa + *d * da * (1.0 - sa)) / oa; + } + } + if !lock && let Some(d) = dst.get_mut(a) { + *d = oa; + } + } + None => { + for (d, s) in dst.iter_mut().zip(src.iter()) { + *d += (*s - *d) * k; + } + } + } +} + +fn buf_len(w: usize, h: usize, ch: usize) -> Option { + w.checked_mul(h)?.checked_mul(ch) +} + +/// Parameters for [`stamp`]. +pub struct StampParams<'a> { + pub dest: &'a mut [f32], + pub dest_w: usize, + pub dest_h: usize, + pub dest_origin: (i32, i32), + pub src: &'a [f32], + pub src_w: usize, + pub src_h: usize, + pub src_origin: (i32, i32), + pub ch: usize, + pub dabs: &'a [Dab], + pub hardness: f32, + pub opacity: f32, + pub flow: f32, + pub tolerance: f32, + /// Destination tile edge; `0` processes the whole buffer as one tile. + pub tile: i32, + pub selection: Option<&'a [f32]>, + pub lock_transparent: bool, + pub alpha: Option, +} + +fn dest_pixels(p: &StampParams<'_>) -> Result { + let n = buf_len(p.dest_w, p.dest_h, p.ch).ok_or(StampError::TooLarge)?; + if p.dest.len() < n { + return Err(StampError::Empty); + } + let sn = buf_len(p.src_w, p.src_h, p.ch).ok_or(StampError::TooLarge)?; + if p.src.len() < sn { + return Err(StampError::Empty); + } + if p.ch == 0 || p.dest_w == 0 || p.dest_h == 0 { + return Err(StampError::Empty); + } + Ok(n as u64) +} + +fn tile_grid(w: usize, h: usize, origin: (i32, i32), tile: i32) -> Vec<(i32, i32, i32, i32)> { + let (x0, y0) = origin; + let x1 = x0.saturating_add(w.min(i32::MAX as usize) as i32); + let y1 = y0.saturating_add(h.min(i32::MAX as usize) as i32); + if tile <= 0 { + return vec![(x0, y0, x1, y1)]; + } + let mut tiles = Vec::new(); + let mut y = y0; + while y < y1 { + let ny = y.saturating_add(tile).min(y1); + let mut x = x0; + while x < x1 { + let nx = x.saturating_add(tile).min(x1); + tiles.push((x, y, nx, ny)); + x = nx; + } + y = ny; + } + tiles +} + +fn stamp_dab_on_rect(p: &mut StampParams<'_>, dab: &Dab, rect: (i32, i32, i32, i32)) -> Result { + let (x0, y0, x1, y1) = rect; + if x1 <= x0 || y1 <= y0 { + return Ok(0); + } + let Some(tw) = (x1 as i64 - x0 as i64).try_into().ok().filter(|w: &usize| *w > 0) else { + return Err(StampError::TooLarge); + }; + let Some(th) = (y1 as i64 - y0 as i64).try_into().ok().filter(|h: &usize| *h > 0) else { + return Err(StampError::TooLarge); + }; + let area = (tw as u64).saturating_mul(th as u64); + if area > MAX_COVERAGE { + return Err(StampError::TooLarge); + } + let n = tw.checked_mul(th).and_then(|m| m.checked_mul(p.ch)).ok_or(StampError::TooLarge)?; + let mut local = vec![0.0f32; n]; + let mut cov = vec![0.0f32; tw.checked_mul(th).ok_or(StampError::TooLarge)?]; + // Copy dest tile into local. + for y in y0..y1 { + for x in x0..x1 { + let lx = (x - x0) as usize; + let ly = (y - y0) as usize; + let Some(di) = origin_index(p.dest_origin, x, y, p.dest_w, p.dest_h, p.ch) else { continue }; + let Some(li) = pixel_index(lx, ly, tw, th, p.ch) else { continue }; + if let (Some(dst), Some(src)) = (local.get_mut(li..li + p.ch), p.dest.get(di..di + p.ch)) { + dst.copy_from_slice(src); + } + if let Some(c) = cov.get_mut(ly.saturating_mul(tw).saturating_add(lx)) { + *c = coverage_at(dab, x, y, p.hardness); + } + } + } + + // The rotated read keeps the distance to the dab centre, so it stays inside the dab's bounds: + // sample the source directly, whichever tile the destination pixel is in. + let mut sampled = vec![0.0f32; p.ch]; + let mut written = 0u32; + let k_stroke = (p.opacity * p.flow * dab.pressure).clamp(0.0, 1.0); + for y in y0..y1 { + for x in x0..x1 { + let lx = (x - x0) as usize; + let ly = (y - y0) as usize; + let Some(&c) = cov.get(ly.saturating_mul(tw).saturating_add(lx)) else { continue }; + if c <= 0.0 { + continue; + } + let mut k = c * k_stroke; + if let Some(sel) = p.selection { + let Some(si) = origin_index(p.dest_origin, x, y, p.dest_w, p.dest_h, 1) else { continue }; + let Some(&sv) = sel.get(si) else { continue }; + k *= sv.clamp(0.0, 1.0); + } + if k <= 0.0 { + continue; + } + let (sx, sy) = rotated_source(dab, x, y); + if !sample_bilinear(SrcView { data: p.src, w: p.src_w, h: p.src_h, origin: p.src_origin, ch: p.ch }, sx, sy, &mut sampled) { + continue; + } + let Some(li) = pixel_index(lx, ly, tw, th, p.ch) else { continue }; + let Some(dst) = local.get_mut(li..li + p.ch) else { continue }; + if p.lock_transparent && p.alpha.is_some_and(|a| dst.get(a).is_some_and(|&v| v <= 0.0)) { + continue; + } + over_native(dst, &sampled, k, p.alpha, p.lock_transparent); + written = written.saturating_add(1); + } + } + + // Write the dest tile back. + for y in y0..y1 { + for x in x0..x1 { + let lx = (x - x0) as usize; + let ly = (y - y0) as usize; + let Some(di) = origin_index(p.dest_origin, x, y, p.dest_w, p.dest_h, p.ch) else { continue }; + let Some(li) = pixel_index(lx, ly, tw, th, p.ch) else { continue }; + if let (Some(dst), Some(src)) = (p.dest.get_mut(di..di + p.ch), local.get(li..li + p.ch)) { + dst.copy_from_slice(src); + } + } + } + Ok(written) +} + +fn dab_intersects(dab: &Dab, tile: (i32, i32, i32, i32)) -> bool { + let Some((x0, y0, x1, y1)) = dab_bounds(dab) else { return false }; + x0 < tile.2 && x1 > tile.0 && y0 < tile.3 && y1 > tile.1 +} + +/// Mean difference between `src` and `dest` over the dab's covered pixels (working-space peak is +/// 1, so a tolerance of 50% is `0.5`). Per pixel it is the larger of the mean colour change and the +/// alpha change: an opaque pixel's unchanged alpha must not water a colour change down, and the +/// Eraser keeps the colour and lowers only the alpha. `None` if nothing is covered. +fn dab_mean_diff(p: &StampParams<'_>, dab: &Dab) -> Option { + let (x0, y0, x1, y1) = dab_bounds(dab)?; + let mut sampled = vec![0.0f32; p.ch]; + let mut sum = 0.0f32; + let mut count = 0u32; + for y in y0..y1 { + for x in x0..x1 { + if coverage_at(dab, x, y, p.hardness) <= 0.0 { + continue; + } + let Some(di) = origin_index(p.dest_origin, x, y, p.dest_w, p.dest_h, p.ch) else { continue }; + let Some(dst) = p.dest.get(di..di.saturating_add(p.ch)) else { continue }; + let (sx, sy) = rotated_source(dab, x, y); + if !sample_bilinear(SrcView { data: p.src, w: p.src_w, h: p.src_h, origin: p.src_origin, ch: p.ch }, sx, sy, &mut sampled) { + continue; + } + let nch = p.ch.min(dst.len()).min(sampled.len()); + if nch == 0 { + continue; + } + let (mut colour, mut colours, mut alpha) = (0.0f32, 0u32, 0.0f32); + for i in 0..nch { + let Some((&s, &d)) = sampled.get(i).zip(dst.get(i)) else { continue }; + if p.alpha == Some(i) { + alpha = (s - d).abs(); + } else { + colour += (s - d).abs(); + colours += 1; + } + } + let colour = if colours == 0 { 0.0 } else { colour / colours as f32 }; + sum += colour.max(alpha); + count = count.saturating_add(1); + } + } + if count == 0 { + return None; + } + let mean = sum / count as f32; + mean.is_finite().then_some(mean) +} + +/// Stamp posed dabs into `dest`, tile by tile, reading `src` at each dab's rotated positions. +/// +/// Sequential dabs overlap: dab *n* sees dab *n − 1*. Returns how many destination pixels were +/// written (tolerance-skipped dabs count as zero). +pub fn stamp(p: &mut StampParams<'_>) -> Result { + dest_pixels(p)?; + if p.src_w == 0 || p.src_h == 0 { + return Err(StampError::Empty); + } + if p.dabs.is_empty() { + return Err(StampError::Empty); + } + let dest_area = (p.dest_w as u64).saturating_mul(p.dest_h as u64); + if dest_area > MAX_COVERAGE { + return Err(StampError::TooLarge); + } + let tiles = tile_grid(p.dest_w, p.dest_h, p.dest_origin, p.tile); + let mut written = 0u32; + let dabs: Vec = p.dabs.to_vec(); + for dab in &dabs { + let Some(bounds) = dab_bounds(dab) else { continue }; + if p.tolerance > 0.0 && dab_mean_diff(p, dab).is_some_and(|m| m < p.tolerance) { + continue; + } + let overlapping: Vec<(i32, i32, i32, i32)> = tiles.iter().copied().filter(|t| dab_intersects(dab, *t)).collect(); + if overlapping.is_empty() { + continue; + } + // Sequential tiles: each writes dest before the next dab. Parallel tiles of one dab do not + // share destination pixels, so they can run independently; we still apply them in order + // here so overlapping dabs stay deterministic. + for tile in overlapping { + let (bx0, by0, bx1, by1) = bounds; + let dest_rect = (tile.0.max(bx0), tile.1.max(by0), tile.2.min(bx1), tile.3.min(by1)); + written = written.saturating_add(stamp_dab_on_rect(p, dab, dest_rect)?); + } + } + Ok(written) +} + +#[cfg(test)] +mod tests; diff --git a/crates/algo/src/art_history/tests.rs b/crates/algo/src/art_history/tests.rs new file mode 100644 index 0000000000..373cfc304b --- /dev/null +++ b/crates/algo/src/art_history/tests.rs @@ -0,0 +1,393 @@ +use super::*; + +fn fill(w: usize, h: usize, ch: usize, f: impl Fn(i32, i32, usize) -> f32) -> Vec { + let mut v = vec![0.0; w * h * ch]; + for y in 0..h { + for x in 0..w { + for c in 0..ch { + v[(y * w + x) * ch + c] = f(x as i32, y as i32, c); + } + } + } + v +} + +fn horizontal(len: f32) -> Vec<[f32; 2]> { + vec![[10.0, 32.0], [10.0 + len, 32.0]] +} + +#[test] +fn tight_short_emits_more_dabs_than_tight_long() { + let pts = horizontal(100.0); + let short = place_dabs(&pts, 10.0, 50.0, Style::TightShort, RNG_SEED).unwrap(); + let long = place_dabs(&pts, 10.0, 50.0, Style::TightLong, RNG_SEED).unwrap(); + assert!(short.len() > long.len(), "short {} long {}", short.len(), long.len()); + let s = spacing_px(10.0, 50.0, Style::TightShort).unwrap(); + let l = spacing_px(10.0, 50.0, Style::TightLong).unwrap(); + assert!(s < l, "{s} {l}"); +} + +#[test] +fn tight_curl_angles_increase_and_loose_curl_scatters() { + let pts = horizontal(80.0); + let tight = place_dabs(&pts, 12.0, 50.0, Style::TightCurl, RNG_SEED).unwrap(); + let loose = place_dabs(&pts, 12.0, 50.0, Style::LooseCurl, RNG_SEED).unwrap(); + assert!(tight.len() >= 3 && loose.len() >= 3); + let dt: Vec = tight.windows(2).map(|w| w[1].theta - w[0].theta).collect(); + assert!(dt.iter().all(|d| *d > 0.05), "tight curl steps {dt:?}"); + let dl: Vec = loose.windows(2).map(|w| w[1].theta - w[0].theta).collect(); + let mean = |v: &[f32]| v.iter().sum::() / v.len() as f32; + assert!(mean(&dl) > mean(&dt), "loose curl {dl:?} vs tight {dt:?}"); + let scatter_t: f32 = tight.iter().map(|d| (d.y - 32.0).abs()).sum(); + let scatter_l: f32 = loose.iter().map(|d| (d.y - 32.0).abs()).sum(); + assert!(scatter_l > scatter_t, "loose scatter {scatter_l} tight {scatter_t}"); +} + +#[test] +fn seeded_scatter_is_replayable() { + let pts = horizontal(60.0); + let a = place_dabs(&pts, 16.0, 40.0, Style::LooseMedium, RNG_SEED ^ 3).unwrap(); + let b = place_dabs(&pts, 16.0, 40.0, Style::LooseMedium, RNG_SEED ^ 3).unwrap(); + assert_eq!(a, b); + let c = place_dabs(&pts, 16.0, 40.0, Style::LooseMedium, RNG_SEED ^ 4).unwrap(); + assert_ne!(a, c); +} + +fn fixture(ch: usize) -> (usize, usize, Vec, Vec, Dab) { + let (w, h) = (128usize, 96usize); + let src = fill(w, h, ch, |x, y, c| if c + 1 == ch && ch > 1 { 1.0 } else { ((x * 13 + y * 7 + c as i32 * 3).rem_euclid(251) as f32) / 255.0 }); + let dest0 = fill(w, h, ch, |_, _, c| if c + 1 == ch && ch > 1 { 1.0 } else { 0.1 }); + // Centre 1 px inside a 64-tile so the rotated read crosses the seam. + let dab = Dab { x: 63.0, y: 40.0, theta: 0.4, radius: 18.0, pressure: 1.0 }; + (w, h, src, dest0, dab) +} + +#[allow(clippy::too_many_arguments)] +fn stamp_run(w: usize, h: usize, ch: usize, src: &[f32], dest: &mut [f32], dab: &Dab, tile: i32) -> u32 { + stamp_dabs(w, h, ch, src, dest, std::slice::from_ref(dab), tile) +} + +#[allow(clippy::too_many_arguments)] +fn stamp_dabs(w: usize, h: usize, ch: usize, src: &[f32], dest: &mut [f32], dabs: &[Dab], tile: i32) -> u32 { + let mut p = StampParams { + dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs, + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 0.0, + tile, + selection: None, + lock_transparent: false, + alpha: (ch > 1).then_some(ch - 1), + }; + stamp(&mut p).unwrap() +} + +fn max_abs(a: &[f32], b: &[f32]) -> f32 { + a.iter().zip(b).fold(0.0f32, |m, (x, y)| m.max((x - y).abs())) +} + +fn max_u8(a: &[f32], b: &[f32]) -> i32 { + a.iter() + .zip(b) + .map(|(x, y)| { + let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as i32; + (q(*x) - q(*y)).abs() + }) + .max() + .unwrap_or(0) +} + +#[test] +fn tiled_stamp_matches_untiled_at_a_tile_seam() { + let (w, h, src, dest0, dab) = fixture(4); + let mut tiled = dest0.clone(); + let mut flat = dest0.clone(); + let n_t = stamp_run(w, h, 4, &src, &mut tiled, &dab, 64); + let n_f = stamp_run(w, h, 4, &src, &mut flat, &dab, 0); + assert!(n_t > 0 && n_f > 0, "wrote {n_t} tiled / {n_f} flat"); + assert_eq!(max_u8(&tiled, &flat), 0, "8-bit seam"); + assert!(max_abs(&tiled, &flat) <= 1.0 / 32_767.0, "16-bit seam {}", max_abs(&tiled, &flat)); + assert!(max_abs(&tiled, &flat) <= 1.0e-5, "32f seam {}", max_abs(&tiled, &flat)); + + let (w, h, src, dest0, dab) = fixture(1); + let mut tiled = dest0.clone(); + let mut flat = dest0; + stamp_run(w, h, 1, &src, &mut tiled, &dab, 64); + stamp_run(w, h, 1, &src, &mut flat, &dab, 0); + assert_eq!(max_u8(&tiled, &flat), 0, "gray 8-bit seam"); +} + +#[test] +fn leftward_and_curl_strokes_match_untiled_across_a_tile_edge() { + let (w, h, ch) = (128usize, 96usize, 4); + let src = fill(w, h, ch, |x, y, c| if c == 3 { 1.0 } else { ((x * 13 + y * 7 + c as i32 * 3).rem_euclid(251) as f32) / 255.0 }); + let dest0 = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.1 }); + for style in [Style::TightMedium, Style::TightCurl, Style::LooseCurl] { + let dabs = place_dabs(&[[110.0, 40.0], [20.0, 40.0]], 40.0, 50.0, style, RNG_SEED).unwrap(); + let mut tiled = dest0.clone(); + let mut flat = dest0.clone(); + stamp_dabs(w, h, ch, &src, &mut tiled, &dabs, 64); + stamp_dabs(w, h, ch, &src, &mut flat, &dabs, 0); + assert_eq!(max_abs(&tiled, &flat), 0.0, "{} seam at x = 64", style.as_str()); + } +} + +/// A dab at angle 0 copies the history pixel under each covered pixel, not a blend of it +/// and its lower-right neighbours. +#[test] +fn unrotated_dab_restores_the_source_pixel() { + let (w, h, ch) = (48usize, 48usize, 4); + let src = fill(w, h, ch, |x, y, c| if c == 3 { 1.0 } else { ((x * 37 + y * 11 + c as i32 * 5).rem_euclid(97) as f32) / 96.0 }); + let mut dest = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.5 }); + let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 }; + stamp_run(w, h, ch, &src, &mut dest, &dab, 0); + for (x, y) in [(24usize, 24usize), (20, 22), (27, 26)] { + let i = (y * w + x) * ch; + assert_eq!(&dest[i..i + ch], &src[i..i + ch], "pixel {x},{y}"); + } +} + +#[test] +fn halo_covers_rotated_read_reach() { + let size = 40.0; + for style in [Style::TightShort, Style::TightMedium, Style::TightLong, Style::LooseMedium, Style::LooseLong, Style::Dab, Style::TightCurl, Style::LooseCurl] + { + let halo = halo_radius(size, style).unwrap(); + let side = sample_side(size, style).unwrap(); + let reach = (side + 1) / 2; + assert!(halo >= reach, "{:?}: halo {halo} < reach {reach} (side {side})", style.as_str()); + } +} + +#[test] +fn tolerance_skips_an_unchanged_layer() { + let (w, h, ch) = (48usize, 48usize, 4); + let src = fill(w, h, ch, |x, y, c| if c == 3 { 1.0 } else { (x + y) as f32 / 100.0 }); + let mut dest = src.clone(); + let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 }; + let mut p = StampParams { + dest: &mut dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src: &src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 1.0, + tile: 0, + selection: None, + lock_transparent: false, + alpha: Some(3), + }; + let n = stamp(&mut p).unwrap(); + assert_eq!(n, 0); + assert_eq!(dest, src); +} + +#[test] +fn tolerance_zero_writes_where_history_differs() { + let (w, h, ch) = (48usize, 48usize, 4); + let src = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.9 }); + let mut dest = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.1 }); + let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 }; + let mut p = StampParams { + dest: &mut dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src: &src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 0.0, + tile: 0, + selection: None, + lock_transparent: false, + alpha: Some(3), + }; + let n = stamp(&mut p).unwrap(); + assert!(n > 0); + let i = (24 * w + 24) * ch; + assert!((dest[i] - 0.9).abs() < 1.0e-5, "{}", dest[i]); +} + +#[test] +fn tolerance_ignores_unchanged_alpha() { + // Colour moved by 0.3 and alpha did not: the 0.25 threshold must not see a 0.225 average. + let (w, h, ch) = (48usize, 48usize, 4); + let src = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.6 }); + let dest0 = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.3 }); + let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 }; + for (tolerance, paints) in [(0.25, true), (0.35, false)] { + let mut dest = dest0.clone(); + let mut p = StampParams { + dest: &mut dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src: &src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance, + tile: 0, + selection: None, + lock_transparent: false, + alpha: Some(3), + }; + let n = stamp(&mut p).unwrap(); + assert_eq!(n > 0, paints, "tolerance {tolerance}"); + } +} + +/// The Eraser keeps the colour and lowers only the alpha: an erased patch differs from the +/// history state and is painted back with a tolerance above 0. +#[test] +fn tolerance_sees_an_erased_patch() { + let (w, h, ch) = (48usize, 48usize, 4); + let src = fill(w, h, ch, |_, _, c| if c == 0 || c == 3 { 1.0 } else { 0.0 }); + let mut dest = fill(w, h, ch, |_, _, c| match c { + 0 => 1.0, + 3 => 0.6, + _ => 0.0, + }); + let dab = Dab { x: 24.0, y: 24.0, theta: 0.0, radius: 8.0, pressure: 1.0 }; + let mut p = StampParams { + dest: &mut dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src: &src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 0.25, + tile: 0, + selection: None, + lock_transparent: false, + alpha: Some(3), + }; + assert!(stamp(&mut p).unwrap() > 0, "alpha 1 -> 0.6 is over tolerance 0.25"); + let i = (24 * w + 24) * ch; + assert!((dest[i + 3] - 1.0).abs() < 1.0e-5, "alpha restored, got {}", dest[i + 3]); +} +#[test] +fn empty_or_mismatched_buffers_error() { + let mut dest = vec![0.0f32; 16]; + let src = vec![0.0f32; 16]; + let dab = Dab { x: 1.0, y: 1.0, theta: 0.0, radius: 1.0, pressure: 1.0 }; + let mut p = StampParams { + dest: &mut dest, + dest_w: 0, + dest_h: 4, + dest_origin: (0, 0), + src: &src, + src_w: 4, + src_h: 4, + src_origin: (0, 0), + ch: 1, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 0.0, + tile: 0, + selection: None, + lock_transparent: false, + alpha: None, + }; + assert_eq!(stamp(&mut p), Err(StampError::Empty)); + let mut dest = vec![0.0f32; 16]; + let src = vec![0.0f32; 8]; + let mut p = StampParams { + dest: &mut dest, + dest_w: 4, + dest_h: 4, + dest_origin: (0, 0), + src: &src, + src_w: 2, + src_h: 2, + src_origin: (0, 0), + ch: 4, + dabs: std::slice::from_ref(&dab), + hardness: 1.0, + opacity: 1.0, + flow: 1.0, + tolerance: 0.0, + tile: 0, + selection: None, + lock_transparent: false, + alpha: None, + }; + assert!(stamp(&mut p).is_err()); + assert!(place_dabs(&[], 10.0, 50.0, Style::TightMedium, 0).is_err()); + assert!(Style::parse("nope").is_none()); + assert_eq!(Style::parse("tightMedium"), Some(Style::TightMedium)); +} + +#[test] +fn two_stamps_with_the_same_seed_match() { + let pts = horizontal(50.0); + let dabs = place_dabs(&pts, 12.0, 50.0, Style::LooseCurl, RNG_SEED).unwrap(); + let (w, h, ch) = (80usize, 64usize, 4); + let src = fill(w, h, ch, |x, y, c| if c == 3 { 1.0 } else { (x + 3 * y) as f32 / 200.0 }); + let dest0 = fill(w, h, ch, |_, _, c| if c == 3 { 1.0 } else { 0.2 }); + let run = |dest: &mut [f32]| { + let mut p = StampParams { + dest, + dest_w: w, + dest_h: h, + dest_origin: (0, 0), + src: &src, + src_w: w, + src_h: h, + src_origin: (0, 0), + ch, + dabs: &dabs, + hardness: 0.8, + opacity: 1.0, + flow: 1.0, + tolerance: 0.0, + tile: 64, + selection: None, + lock_transparent: false, + alpha: Some(3), + }; + stamp(&mut p).unwrap() + }; + let mut a = dest0.clone(); + let mut b = dest0; + assert_eq!(run(&mut a), run(&mut b)); + assert_eq!(a, b); +} diff --git a/crates/algo/src/artistic_fx.rs b/crates/algo/src/artistic_fx.rs index 4d2e85ed1a..4143b30657 100644 --- a/crates/algo/src/artistic_fx.rs +++ b/crates/algo/src/artistic_fx.rs @@ -17,6 +17,7 @@ use photocraft_geom::Rect; use crate::Ctx; use crate::artistic::GalleryEffect; use crate::artistic::GalleryFilter as F; +use crate::cutout; use crate::fxutil::{box_blur_n, cell_point, gauss_blur_n, gauss_box_radii, rgba, set_rgba}; use crate::image::Image; use crate::noise::hash01; @@ -409,7 +410,8 @@ pub(crate) fn effect_reach(e: &GalleryEffect) -> i32 { let g = |k: &str| e.get(k); match e.filter { F::ColoredPencil => 2, - F::Cutout => gs(cutout_sigma(e)), + // Traced over the whole image before tiling (see [`Prepared`]). + F::Cutout => 0, F::DryBrush | F::Fresco => kuwahara_reach(1 + g("brushSize") as usize) + gs(1.0) + 2, F::FilmGrain | F::DiffuseGlow | F::ChalkCharcoal | F::Reticulation | F::Grain | F::MosaicTiles | F::Texturizer => 0, F::RoughPastels => gs(0.6 + 1.2) + ((2.0 + g("strokeLength")) / 2.0) as i32 + 3, @@ -437,7 +439,7 @@ pub(crate) fn effect_reach(e: &GalleryEffect) -> i32 { F::Chrome => gs(1.0 + g("smoothness") * 0.4) + 2, F::ConteCrayon => 0, F::GraphicPen => (g("strokeLength") / 2.0) as i32 + 2, - F::HalftonePattern => gs(0.5 + g("size") * 0.3), + F::HalftonePattern => gs(halftone_cell(e) * 0.15), F::NotePaper => gs(1.0) + gs(1.2) + 2, F::Photocopy => gs(1.0 + g("detail") * 0.5), F::Plaster => gs(0.5 + g("smoothness") * 0.8) + gs(1.5) + 2, @@ -451,8 +453,57 @@ pub(crate) fn effect_reach(e: &GalleryEffect) -> i32 { } } -fn cutout_sigma(e: &GalleryEffect) -> f32 { - 0.5 + e.get("edgeSimplicity") * (1.1 - 0.3 * e.get("edgeFidelity")) +fn cutout_settings(e: &GalleryEffect) -> cutout::Settings { + cutout::Settings { levels: e.get("numberOfLevels").round().clamp(2.0, 8.0) as usize, simplicity: e.get("edgeSimplicity"), fidelity: e.get("edgeFidelity") } +} +/// Halftone Pattern's screen spacing in pixels (about 3.5 at Size 1, 25.5 at 12). +fn halftone_cell(e: &GalleryEffect) -> f32 { + 1.5 + 2.0 * e.get("size").clamp(1.0, 12.0) +} +/// The round-dot spot function `(cos 2πu + cos 2πv) / 2` sampled over a cell and sorted: a +/// value's rank among them is the share of the cell below it, which makes the thresholds even. +fn halftone_dot_ranks() -> &'static [f32] { + static RANKS: std::sync::OnceLock> = std::sync::OnceLock::new(); + RANKS.get_or_init(|| { + const N: usize = 128; + let mut v: Vec = (0..N * N) + .map(|i| { + let (u, w) = (((i % N) as f32 + 0.5) / N as f32, ((i / N) as f32 + 0.5) / N as f32); + ((2.0 * PI * u).cos() + (2.0 * PI * w).cos()) / 2.0 + }) + .collect(); + v.sort_by(f32::total_cmp); + v + }) +} +/// For each tone (0 = paper … 1 = solid ink, 256 steps), the level `τ` at which the soft +/// threshold `clamp(0.5 + (τ − s) · gain)` over evenly spread thresholds `s` inks exactly that +/// share of a cell on average. +fn halftone_offsets(gain: f32) -> Vec { + let half = 0.5 / gain.max(1e-3); + // Average ink over s in [0, 1] for level τ: 1 below τ − half, a ramp to 0 at τ + half. + let cover = |tau: f32| { + let (a, z) = (tau - half, tau + half); + let solid = a.clamp(0.0, 1.0); + let (r0, r1) = (a.clamp(0.0, 1.0), z.clamp(0.0, 1.0)); + let ramp = if r1 > r0 { (0.5 + tau * gain) * (r1 - r0) - 0.5 * gain * (r1 * r1 - r0 * r0) } else { 0.0 }; + solid + ramp + }; + (0..256) + .map(|i| { + let target = i as f32 / 255.0; + let (mut lo, mut hi) = (-half - 1.0, 1.0 + half + 1.0); + for _ in 0..40 { + let mid = (lo + hi) / 2.0; + if cover(mid) < target { lo = mid } else { hi = mid } + } + match i { + 0 => -half - 1.0, + 255 => 1.0 + half + 1.0, + _ => (lo + hi) / 2.0, + } + }) + .collect() } fn daubs_radius(e: &GalleryEffect) -> usize { let wide = matches!(e.choice("brushType"), "wideSharp" | "wideBlurry"); @@ -492,6 +543,54 @@ pub(crate) fn run(effects: &[GalleryEffect], src: &Image, out: Rect, ctx: &Ctx) for e in effects { apply(e, &mut win, ctx); } + finish(&win, src, out, ctx) +} + +/// Whether a stack needs the whole image at once (Cutout traces it). +pub(crate) fn is_global(effects: &[GalleryEffect]) -> bool { + effects.iter().any(|e| e.filter == F::Cutout) +} + +/// A stack with a Cutout, prepared once for all tiles: the effects up to the last Cutout run +/// over the whole image and it is traced; each tile then only fills in the traced shapes and +/// runs the effects after it. +pub(crate) struct Prepared { + traced: cutout::Traced, + rest: Vec, +} + +impl Prepared { + /// `None` for a stack that has no Cutout (it runs tile by tile as usual). + pub(crate) fn new(effects: &[GalleryEffect], src: &Image, ctx: &Ctx) -> Option { + let last = effects.iter().rposition(|e| e.filter == F::Cutout)?; + let r = src.rect; + let (w, h) = (r.width() as usize, r.height() as usize); + let mut win = Win { r, w, h, c: src.data.chunks_exact(src.ch).map(|px| rgb(rgba(ctx, px))).collect() }; + for e in effects.get(..last).unwrap_or_default() { + apply(e, &mut win, ctx); + } + let traced = cutout::trace(&win.c, r, &cutout_settings(effects.get(last)?)); + Some(Prepared { traced, rest: effects.get(last + 1..).unwrap_or_default().to_vec() }) + } + + /// The stack's result over `out` (`src` is the image it was prepared from). + pub(crate) fn run(&self, src: &Image, out: Rect, ctx: &Ctx) -> Vec { + let r = out.inflate(reach(&self.rest)).intersect(&src.rect); + let (w, h) = (r.width() as usize, r.height() as usize); + if w == 0 || h == 0 { + return src.crop(out); + } + let mut win = Win { r, w, h, c: self.traced.render(r) }; + for e in &self.rest { + apply(e, &mut win, ctx); + } + finish(&win, src, out, ctx) + } +} + +/// `out` of the source with its colour from `win` (alpha kept). +fn finish(win: &Win, src: &Image, out: Rect, ctx: &Ctx) -> Vec { + let n = src.ch; let mut res = src.crop(out); let ow = out.width() as usize; for (i, px) in res.chunks_exact_mut(n).enumerate() { @@ -506,12 +605,20 @@ pub(crate) fn run(effects: &[GalleryEffect], src: &Image, out: Rect, ctx: &Ctx) res } +/// Applies one gallery effect to the working window in place. Each category handles its own +/// filters and reports whether the effect was one of them. fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { + let handled = + apply_artistic(e, win) || apply_brush_strokes(e, win) || apply_distort(e, win) || apply_sketch(e, win, ctx) || apply_stylize_texture(e, win, ctx); + debug_assert!(handled, "every gallery filter belongs to a category"); +} + +/// Artistic filters: the painterly looks of the gallery. +fn apply_artistic(e: &GalleryEffect, win: &mut Win) -> bool { let g = |k: &str| e.get(k); let (w, h) = (win.w, win.h); let (fg, bg) = (rgb(e.foreground), rgb(e.background)); match e.filter { - // ---------------- Artistic ---------------- F::ColoredPencil => { let pw = g("pencilWidth"); let pressure = g("strokePressure") / 15.0; @@ -528,16 +635,7 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { win.c[i] = mix(paper, map(win.c[i], |v| v * 0.88), cov); } } - F::Cutout => { - blur3(&mut win.c, w, h, cutout_sigma(e)); - let levels = g("numberOfLevels").round(); - for p in &mut win.c { - // Flat tones per luminance level, keeping each region's hue. - let l = lum(p).max(1e-4); - let k = posterize(l, levels) / l; - *p = map(*p, |v| posterize(clamp01(v * k), levels * 2.0)); - } - } + F::Cutout => win.c = cutout::trace(&win.c, win.r, &cutout_settings(e)).render(win.r), F::DryBrush | F::Fresco => { let fresco = e.filter == F::Fresco; kuwahara(win, 1 + g("brushSize") as usize); @@ -695,7 +793,16 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { win.c[i] = map(saturate(win.c[i], 1.2), |v| clamp01(v * k + n).powf(1.1 + sh * 0.6)); } } - // ---------------- Brush Strokes ---------------- + _ => return false, + } + true +} + +/// Brush Strokes filters. +fn apply_brush_strokes(e: &GalleryEffect, win: &mut Win) -> bool { + let g = |k: &str| e.get(k); + let (w, h) = (win.w, win.h); + match e.filter { F::AccentedEdges => { let mut l = win.lum(); blur1(&mut l, w, h, 0.4 + g("smoothness") * 0.3); @@ -809,7 +916,17 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { *p = map(*p, |v| clamp01((v - 0.5) * (1.0 + con) + 0.5) * (1.0 - ink * 0.9 * (0.5 + con))); } } - // ---------------- Distort ---------------- + _ => return false, + } + true +} + +/// Distort filters. +fn apply_distort(e: &GalleryEffect, win: &mut Win) -> bool { + let g = |k: &str| e.get(k); + let (w, h) = (win.w, win.h); + let bg = rgb(e.background); + match e.filter { F::DiffuseGlow => { let (gr, ga, ca) = (g("graininess") / 10.0, g("glowAmount") / 20.0, g("clearAmount") / 20.0); let thr = 0.2 + ca * 0.7; @@ -853,7 +970,17 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { win.c[i] = src.sample(x as f32 + 0.5 + dx, y as f32 + 0.5 + dy); } } - // ---------------- Sketch ---------------- + _ => return false, + } + true +} + +/// Sketch filters (foreground is ink, background is paper). +fn apply_sketch(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) -> bool { + let g = |k: &str| e.get(k); + let (w, h) = (win.w, win.h); + let (fg, bg) = (rgb(e.foreground), rgb(e.background)); + match e.filter { F::BasRelief => { let mut l = win.lum(); let orig = l.clone(); @@ -933,36 +1060,46 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { } } F::HalftonePattern => { - let size = g("size"); - let con = g("contrast") / 50.0; - let cell = 3.0 + size * 2.0; + // A halftone screen that keeps a continuous range of tones: each pixel compares its + // ink (the darkness of the image) with the screen's threshold there, and Contrast is + // how steeply: at 0 the image keeps its tones with the screen faintly through it, at + // 50 it is crisp dots, lines or rings. The thresholds are spread evenly over each cell, + // and each tone is offset so a cell's average ink is exactly that tone: black stays + // solid and white stays paper at any Contrast. + let cell = halftone_cell(e); + let gain = 0.5 + g("contrast") * 0.6; + let offsets = halftone_offsets(gain); let mut l = win.lum(); - blur1(&mut l, w, h, 0.5 + size * 0.3); + blur1(&mut l, w, h, cell * 0.15); let b = ctx.bounds; let (cx, cy) = ((b.x0 + b.x1) as f32 / 2.0, (b.y0 + b.y1) as f32 / 2.0); let ty = e.choice("patternType"); + let dots = halftone_dot_ranks(); for i in 0..win.c.len() { let (x, y) = win.xy(i); let (fx, fy) = (x as f32 + 0.5, y as f32 + 0.5); - let d = clamp01((1.0 - l[i] - 0.5) * (1.0 + con * 3.0) + 0.5); - let aa = 1.0 / cell; - let ink = match ty { - "circle" => { - let r = ((fx - cx).powi(2) + (fy - cy).powi(2)).sqrt() / cell; - let q = (r.fract() - 0.5).abs() * 2.0; - smoothstep(d + aa, d - aa, q) - } - "line" => { - let q = ((fy / cell).fract() - 0.5).abs() * 2.0; - smoothstep(d + aa, d - aa, q) - } + let band = |t: f32| ((t / cell).rem_euclid(1.0) - 0.5).abs() * 2.0; + let spot = match ty { + // Rings round the middle of the image, inked from the middle of each ring. + "circle" => band(((fx - cx).powi(2) + (fy - cy).powi(2)).sqrt()), + // Horizontal lines, inked from the middle of each. + "line" => band(fy), + // Round dots on a screen at 45° (as a black halftone screen is), that grow + // from each cell's centre, meet at 50% as a checkerboard of squares square to + // the image, and fill the cell at 100%. _ => { - let (u, v) = ((fx / cell).fract() - 0.5, (fy / cell).fract() - 0.5); - let q = (u * u + v * v).sqrt() * 2.0; - let rr = d.sqrt() * 1.15; - smoothstep(rr + aa, rr - aa, q) + let (rx, ry) = ((fx + fy) * std::f32::consts::FRAC_1_SQRT_2, (fy - fx) * std::f32::consts::FRAC_1_SQRT_2); + let (u, v) = ((rx / cell).rem_euclid(1.0), (ry / cell).rem_euclid(1.0)); + let f = ((2.0 * PI * u).cos() + (2.0 * PI * v).cos()) / 2.0; + let k = dots.partition_point(|&r| r < f); + k as f32 / dots.len().max(1) as f32 } }; + let tone = clamp01(1.0 - l[i]) * (offsets.len() - 1) as f32; + let (k, f) = (tone.floor() as usize, tone.fract()); + let at = |j: usize| offsets.get(j.min(offsets.len() - 1)).copied().unwrap_or(0.5); + let level = at(k) + (at(k + 1) - at(k)) * f; + let ink = clamp01(0.5 + (level - spot) * gain); win.c[i] = mix(bg, fg, ink); } } @@ -1047,7 +1184,17 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { win.c[i] = map(win.c[i], |v| (v * (0.85 + 0.25 * fib) - 0.5) * (co * 1.6 + 0.2) + 0.5 + (br - 0.5) * 0.8); } } - // ---------------- Stylize ---------------- + _ => return false, + } + true +} + +/// Stylize and Texture filters. +fn apply_stylize_texture(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) -> bool { + let g = |k: &str| e.get(k); + let (w, h) = (win.w, win.h); + let (fg, bg) = (rgb(e.foreground), rgb(e.background)); + match e.filter { F::GlowingEdges => { blur3(&mut win.c, w, h, 0.3 + g("smoothness") * 0.25); let mut e3 = vec![[0.0f32; 3]; win.c.len()]; @@ -1063,7 +1210,6 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { *p = map(*q, |v| clamp01(v * k)); } } - // ---------------- Texture ---------------- F::Craquelure => { let (sp, dep, bri) = (g("crackSpacing"), g("crackDepth") / 10.0, g("crackBrightness") / 10.0); let cell = sp * 1.3 + 1.0; @@ -1179,7 +1325,9 @@ fn apply(e: &GalleryEffect, win: &mut Win, ctx: &Ctx) { } } F::Texturizer => texturize(win, e.choice("texture"), g("scaling"), g("relief"), e.choice("light"), e.flag("invert")), + _ => return false, } + true } fn normalize3(v: [f32; 3]) -> [f32; 3] { @@ -1237,7 +1385,7 @@ mod tests { let fp = FilterParams::FilterGallery { effects: vec![e.clone()] }; apply_tiled(&s, &fp, b, b, None, tile, Some(b)).read_region(b) }; - for f in [G::RoughPastels, G::AngledStrokes] { + for f in [G::RoughPastels, G::AngledStrokes, G::Cutout] { let base = run(&GalleryEffect::new(f), 256); for prm in f.params() { let crate::GalleryParamKind::Range { min, max, .. } = prm.kind else { continue }; @@ -1254,6 +1402,135 @@ mod tests { } } + /// Runs one Cutout over a grey image given as rows of values; returns the red channel. + fn cutout_on(rows: &[Vec], set: &[(&str, f32)]) -> Vec> { + let (w, h) = (rows[0].len() as i32, rows.len() as i32); + let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::F32, true)); + for (y, row) in rows.iter().enumerate() { + for (x, v) in row.iter().enumerate() { + s.write_pixel(x as i32, y as i32, &[*v, *v, *v, 1.0]); + } + } + let mut e = GalleryEffect::new(crate::GalleryFilter::Cutout); + for (k, v) in set { + e.set(k, *v); + } + let b = Rect::new(0, 0, w, h); + let out = apply_tiled(&s, &FilterParams::FilterGallery { effects: vec![e] }, b, b, None, 256, Some(b)).read_region(b); + out.chunks(4 * w as usize).map(|r| r.chunks(4).map(|p| p[0]).collect()).collect() + } + + #[test] + fn cutout_makes_flat_shapes_with_clean_edges() { + // It used to posterize every pixel after a blur: noise survived as speckle, edges were + // ragged and smooth gradients banded instead of becoming flat paper. + let noisy: Vec> = (0..32).map(|y| (0..64).map(|x| if x < 32 { 0.2 } else { 0.8 } + (hash01(x, y, 0, 7) - 0.5) * 0.16).collect()).collect(); + let out = cutout_on(&noisy, &[]); + for (x0, x1) in [(2, 28), (36, 62)] { + let vals: Vec = (2..30).flat_map(|y| out[y][x0..x1].to_vec()).collect(); + let spread = vals.iter().fold(f32::MIN, |a, &v| a.max(v)) - vals.iter().fold(f32::MAX, |a, &v| a.min(v)); + assert!(spread < 1e-4, "a noisy area should be one flat tone, spread {spread}"); + } + // The cut is an anti-aliased pixel or two, not a blur. + for y in 0..32 { + let (lo, hi) = (out[y][16], out[y][48]); + let soft = out[y].iter().filter(|&&v| (v - lo).abs() > 0.01 && (v - hi).abs() > 0.01).count(); + assert!(soft <= 2, "row {y}: {soft} edge pixels"); + } + // A ramp becomes `numberOfLevels` flat steps (the palette is the image's own colours, so + // one may go to the wide flat margins and merge as noise). + let ramp: Vec> = (0..12).map(|_| (0..144).map(|x| ((x as f32 - 24.0) / 95.0).clamp(0.0, 1.0)).collect()).collect(); + for levels in [2.0, 5.0, 8.0] { + let row = &cutout_on(&ramp, &[("numberOfLevels", levels)])[6]; + let mut tones: Vec = Vec::new(); + for win in row.windows(3) { + if (win[0] - win[2]).abs() < 1e-4 && tones.last().is_none_or(|t| (t - win[1]).abs() > 1e-3) { + tones.push(win[1]); + } + } + assert!(tones.len() <= levels as usize && tones.len() + 1 >= levels as usize, "levels {levels}: tones {tones:?}"); + assert!(tones.windows(2).all(|t| t[0] < t[1]), "levels {levels}: tones {tones:?}"); + } + // Edge Simplicity drops specks. + let speck: Vec> = (0..32).map(|y| (0..32).map(|x| if (15..18).contains(&x) && (15..18).contains(&y) { 0.9 } else { 0.2 }).collect()).collect(); + let detailed = cutout_on(&speck, &[("edgeSimplicity", 0.0)]); + let simple = cutout_on(&speck, &[("edgeSimplicity", 10.0)]); + assert!(detailed[16][16] - detailed[2][2] > 0.5, "the speck should survive at simplicity 0"); + assert!((simple[16][16] - simple[2][2]).abs() < 1e-4, "the speck should go at simplicity 10"); + } + + #[test] + fn cutout_keeps_the_tone_of_surviving_islands_at_low_fidelity() { + for (size, simplicity) in [(2, 0.0), (3, 2.0), (4, 0.0), (4, 2.0), (6, 2.0)] { + let rows: Vec> = + (0..32).map(|y| (0..32).map(|x| if (12..12 + size).contains(&x) && (12..12 + size).contains(&y) { 0.9 } else { 0.2 }).collect()).collect(); + let reference = cutout_on(&rows, &[("edgeSimplicity", simplicity), ("edgeFidelity", 2.0)]); + let centre = 12 + size / 2; + for fidelity in [1.0, 1.5, 2.0] { + let out = cutout_on(&rows, &[("edgeSimplicity", simplicity), ("edgeFidelity", fidelity)]); + assert!( + size == 2 || (out[centre][centre] - reference[centre][centre]).abs() < 0.02, + "{size}x{size}, simplicity {simplicity}, fidelity {fidelity}: {} vs {}", + out[centre][centre], + reference[centre][centre] + ); + assert!(out[centre][centre] - out[2][2] > 0.5, "the island keeps its contrast"); + } + } + } + + #[test] + fn a_stack_around_a_cutout_is_tile_independent() { + // Effects before a Cutout are traced with it over the whole image; effects after it run + // per tile on the filled-in shapes. + use crate::GalleryFilter as G; + let b = Rect::new(0, 0, 56, 40); + let s = pattern(SampleType::F32); + let fp = + FilterParams::FilterGallery { effects: vec![GalleryEffect::new(G::Spatter), GalleryEffect::new(G::Cutout), GalleryEffect::new(G::GlowingEdges)] }; + let a = apply_tiled(&s, &fp, b, b, None, 256, Some(b)).read_region(b); + let t = apply_tiled(&s, &fp, b, b, None, 16, Some(b)).read_region(b); + let worst = a.iter().zip(&t).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max); + assert!(worst < 1e-3, "differs across tilings by {worst}"); + } + + /// Flat greys keep their tone through every pattern and Contrast, black and white stay + /// solid, and Contrast makes the screen crisper. + #[test] + fn halftone_keeps_tones_and_contrast_sets_how_hard() { + // It used to be all ink or paper at any Contrast, with white crosses in the shadows + // (dots could never fill their cell) and mid-greys that came out the wrong darkness. + use crate::GalleryFilter as G; + let b = Rect::new(0, 0, 60, 60); + let run = |grey: f32, pattern: &str, contrast: f32| { + let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::F32, true)); + s.fill_rect(b, &[grey, grey, grey, 1.0]); + let mut e = GalleryEffect::new(G::HalftonePattern); + assert!(e.set_choice("patternType", pattern)); + e.set("size", 4.0); + e.set("contrast", contrast); + let out = apply_tiled(&s, &FilterParams::FilterGallery { effects: vec![e] }, b, b, None, 256, Some(b)).read_region(b); + // Ink (1 = foreground black) of every pixel away from the canvas edge. + out.chunks(4).enumerate().filter(|(i, _)| (6..54).contains(&(i % 60)) && (6..54).contains(&(i / 60))).map(|(_, p)| 1.0 - p[0]).collect::>() + }; + for pattern in ["dot", "circle", "line"] { + for contrast in [0.0, 5.0, 50.0] { + for grey in [0.2, 0.5, 0.8] { + let ink = run(grey, pattern, contrast); + let mean = ink.iter().sum::() / ink.len() as f32; + assert!((mean - (1.0 - grey)).abs() < 0.06, "{pattern} contrast {contrast}: grey {grey} inks {mean}"); + } + let solid = run(0.0, pattern, contrast); + assert!(solid.iter().all(|&v| v > 0.99), "{pattern} contrast {contrast}: black isn't solid"); + let paper = run(1.0, pattern, contrast); + assert!(paper.iter().all(|&v| v < 0.01), "{pattern} contrast {contrast}: white isn't paper"); + } + let soft = run(0.5, pattern, 0.0).iter().filter(|&&v| v > 0.05 && v < 0.95).count(); + let hard = run(0.5, pattern, 50.0).iter().filter(|&&v| v > 0.05 && v < 0.95).count(); + assert!(hard * 4 < soft, "{pattern}: contrast 50 should be much crisper ({hard} vs {soft} grey pixels)"); + } + } + #[test] fn params_parse_and_clamp() { for f in crate::GalleryFilter::ALL { @@ -1305,7 +1582,7 @@ mod tests { } /// Visual check: `GALLERY_RAW=src.raw GALLERY_W=480 GALLERY_H=322 cargo test -p photocraft-algo --release gallery_sheet -- --ignored` - /// writes `.raw` (RGB8) beside the input for every filter. + /// writes `.raw` (RGB8) beside the input for every filter (or one, see `GALLERY_ONLY`). #[test] #[ignore] fn gallery_sheet() { @@ -1318,11 +1595,29 @@ mod tests { let b = Rect::new(0, 0, w, h); s.write_region(b, &data); let dir = std::path::Path::new(&path).parent().unwrap().to_path_buf(); - for f in crate::GalleryFilter::ALL { - let fp = FilterParams::FilterGallery { effects: vec![GalleryEffect::new(*f)] }; + // Optional: GALLERY_ONLY= renders one filter; GALLERY_SET="key=value,key=name" sets its + // sliders (numbers) or choices (names); GALLERY_TAG is appended to the file name. + let only = std::env::var("GALLERY_ONLY").ok(); + let set = std::env::var("GALLERY_SET").unwrap_or_default(); + let tag = std::env::var("GALLERY_TAG").unwrap_or_default(); + if let Some(k) = &only { + assert!(crate::GalleryFilter::ALL.iter().any(|f| f.key() == k), "GALLERY_ONLY={k} is not a gallery filter"); + } + for f in crate::GalleryFilter::ALL.iter().filter(|f| only.as_deref().is_none_or(|k| k == f.key())) { + let mut e = GalleryEffect::new(*f); + for kv in set.split(',').filter(|kv| !kv.is_empty()) { + let (k, v) = kv.split_once('=').unwrap(); + // `set` ignores unknown keys; a misspelt one would render the default unnoticed. + assert!(f.params().iter().any(|p| p.key == k), "{k} is not a parameter of {}", f.key()); + match v.parse::() { + Ok(n) => e.set(k, n), + Err(_) => assert!(e.set_choice(k, v), "{k} has no choice {v}"), + } + } + let fp = FilterParams::FilterGallery { effects: vec![e] }; let out = crate::apply_in(&s, &fp, b, b, None, b).read_region(b); let raw: Vec = out.chunks(4).flat_map(|p| [p[0], p[1], p[2]].map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)).collect(); - std::fs::write(dir.join(format!("{}.raw", f.key())), raw).unwrap(); + std::fs::write(dir.join(format!("{}{tag}.raw", f.key())), raw).unwrap(); } } } diff --git a/crates/algo/src/blur.rs b/crates/algo/src/blur.rs index ae8b12f40a..819bf6fa78 100644 --- a/crates/algo/src/blur.rs +++ b/crates/algo/src/blur.rs @@ -31,6 +31,43 @@ pub(crate) fn gaussian_kernel(sigma: f32) -> Vec { k.into_iter().map(|v| v / s).collect() } +/// Gaussian Blur's kernel for `radius` (its standard deviation). Sampling the curve at whole +/// pixels leaves a small radius almost no blur: at 0.3 the neighbour weight is about 0.004, a +/// tenth of the spread asked for (#8544). Below 1 the sampling width is instead chosen so the +/// kernel has exactly the variance `radius²`; from 1 up the sampled kernel is within about 0.4% +/// of it (its 3σ truncation), so larger radii are unchanged and the blur grows smoothly from 0.05. +pub(crate) fn gaussian_blur_kernel(radius: f32) -> Vec { + if !(0.05..1.0).contains(&radius) { + return gaussian_kernel(radius); + } + let variance = |k: &[f32]| { + let r = (k.len() / 2) as f32; + k.iter().enumerate().map(|(i, w)| w * (i as f32 - r).powi(2)).sum::() + }; + // Sampled at `width` with one fixed half-width for every radius below 1: its variance then + // rises steadily with `width` (the search can't settle on a step where taps are added), and + // the blur grows without the dips a width chosen per radius made at 1/3 and 2/3. Three is the + // half-width `gaussian_kernel` uses at radius 1, so the hand-over is smooth too. + let half = 3; + let sampled = |width: f32| { + let k: Vec = (-half..=half).map(|i| (-(i * i) as f32 / (2.0 * width * width)).exp()).collect(); + let s: f32 = k.iter().sum(); + k.into_iter().map(|v| v / s).collect::>() + }; + let target = radius * radius; + // The sampled kernel's variance only falls short of sigma², so the width lies above `radius`. + let (mut lo, mut hi) = (radius, radius + 1.0); + for _ in 0..40 { + let mid = 0.5 * (lo + hi); + if variance(&sampled(mid)) < target { + lo = mid; + } else { + hi = mid; + } + } + sampled(0.5 * (lo + hi)) +} + /// Separable convolution of `src` (premultiplied internally) for `out`. /// `src` must cover `out` grown by the kernel radii. pub(crate) fn conv_sep(src: &Image, out: Rect, kx: &[f32], ky: &[f32], alpha: bool) -> Vec { @@ -107,13 +144,12 @@ fn conv_row(src: &[f32], dst: &mut [f32], kernel: &[f32]) { pub(crate) fn gaussian(src: &Image, out: Rect, ctx: &Ctx, radius: f32) -> Vec { // Exact kernel for small radii; beyond that three box passes approximate the Gaussian with - // cost independent of the radius (Kovesi, "Fast almost-Gaussian filtering", 2010). - match box_widths(&FilterParams::GaussianBlur { radius }) { - Some(boxes) => box_passes(src, out, ctx.alpha, &boxes), - None => { - let k = gaussian_kernel(radius); - conv_sep(src, out, &k, &k, ctx.alpha) - } + // cost independent of the radius (Kovesi, "Fast almost-Gaussian filtering", 2010). Built + // from the description other devices run (`separable`), so they blur the same (#5772). + match crate::separable::Separable::of(&FilterParams::GaussianBlur { radius }) { + Some(crate::separable::Separable::Boxes(boxes)) => box_passes(src, out, ctx.alpha, &boxes), + Some(crate::separable::Separable::Kernel(k)) => conv_sep(src, out, &k, &k, ctx.alpha), + None => src.crop(out), } } @@ -212,12 +248,17 @@ fn box_pass_px(src: &[f32], dst: &mut [f32], r: usize) { } /// Box passes of the given radii in turn along the line in `s.line` (`w`-float elements, as many -/// as `dst` holds plus twice the radii's sum; consumed), ending in `dst`. -fn line_passes(dst: &mut [f32], w: usize, radii: &[usize], s: &mut Scratch) { +/// as `dst` holds plus twice the radii's sum; consumed), ending in `dst`. Stops early (leaving +/// `dst` unfinished) when `ctl` is cancelled before a pass: a column strip's passes take tens of +/// milliseconds on a tall image, too long to finish once cancelled (#6205). +fn line_passes(dst: &mut [f32], w: usize, radii: &[usize], s: &mut Scratch, ctl: &Interrupt) { let Scratch { line, next, acc } = s; let Some(len) = dst.len().checked_div(w) else { return }; let mut tail: usize = radii.iter().sum(); for (k, &r) in radii.iter().enumerate() { + if ctl.cancelled() { + return; + } tail -= r; if k + 1 == radii.len() { box_pass(line, dst, w, r, acc); @@ -281,7 +322,7 @@ pub(crate) fn box_blur( (0..left).for_each(|_| s.line.extend_from_slice(first)); s.line.extend_from_slice(&row); (0..right).for_each(|_| s.line.extend_from_slice(last)); - line_passes(dst, n, &radii, &mut s); + line_passes(dst, n, &radii, &mut s, ctl); } }); // Vertical passes, strip by strip, the same way down the columns. @@ -300,7 +341,10 @@ pub(crate) fn box_blur( (0..rows).for_each(|y| s.line.extend_from_slice(row(y))); (0..bottom).for_each(|_| s.line.extend_from_slice(row(rows - 1))); let mut res = vec![0.0f32; oh * w]; - line_passes(&mut res, w, &radii, &mut s); + line_passes(&mut res, w, &radii, &mut s, ctl); + if ctl.cancelled() { + return (t, Vec::new()); + } unpremultiply(&mut res, n, alpha); (t, res) }); @@ -776,408 +820,4 @@ fn surface_direct(src: &Image, out: Rect, ctx: &Ctx, r: i32, t: f32) -> Vec } #[cfg(test)] -mod tests { - use super::*; - use photocraft_color::{ColorMode, PixelFormat, SampleType}; - use photocraft_raster::Surface; - - #[test] - fn radial_blur_samples_large_arcs_beyond_64_intervals() { - let bounds = Rect::new(0, 0, 1000, 1000); - let out = Rect::new(999, 500, 1000, 501); - let ctx = Ctx { bounds, mode: photocraft_color::ColorMode::Grayscale, alpha: false }; - let mut img = Image::new(bounds, 1); - let (cx, cy) = (500.0f32, 500.0f32); - let (x, y) = (999.5f32, 500.5f32); - let (dx, dy) = (x - cx, y - cy); - let arc = 100.0f32.to_radians(); - let count = radial_intervals(arc * dx.hypot(dy), RadialQuality::Good); - assert_eq!(count, 256); - assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Draft), 64); - assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Good), 256); - assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Best), 4096); - assert_eq!(radial_intervals(f32::NAN, RadialQuality::Best), 1); - - // Put a small bright patch halfway between two samples from the former 64-interval - // limit. Dense sampling should pick it up; the coarse path misses it entirely. - let sparse_midpoint = (31.5 / 64.0 - 0.5) * arc; - let (s, c) = sparse_midpoint.sin_cos(); - let px = (cx + dx * c - dy * s - 0.5).round() as i32; - let py = (cy + dx * s + dy * c - 0.5).round() as i32; - for yy in py - 1..=py + 1 { - for xx in px - 1..=px + 1 { - let index = (yy - bounds.y0) as usize * bounds.width() as usize + (xx - bounds.x0) as usize; - img.data[index] = 1.0; - } - } - - let sample_path = |x: f32, y: f32, pts: &mut Vec<(f32, f32)>, intervals: usize| { - let (dx, dy) = (x - cx, y - cy); - for i in 0..=intervals { - let t = (i as f32 / intervals as f32 - 0.5) * arc; - let (s, c) = t.sin_cos(); - pts.push((cx + dx * c - dy * s, cy + dx * s + dy * c)); - } - }; - let actual = radial(&img, out, &ctx, 100.0, RadialMethod::Spin, RadialQuality::Good, (0.5, 0.5))[0]; - let expected = average_samples(&img, out, &ctx, |x, y, pts| sample_path(x, y, pts, 8192))[0]; - let coarse = average_samples(&img, out, &ctx, |x, y, pts| sample_path(x, y, pts, 64))[0]; - - assert!(actual > 0.001, "dense radial samples should resolve the bright patch, got {actual}"); - assert!((actual - expected).abs() < 0.0015, "radial result {actual} differs from dense reference {expected}"); - assert!(coarse < expected * 0.1, "former 64-interval sampling unexpectedly resolved the patch: {coarse} vs {expected}"); - } - - #[test] - fn fixed_channel_convolution_matches_scalar_tap_order() { - fn check() { - for width in [1, 7, 256] { - for radius in [0.0, 0.5, 3.0, 4.0] { - let k = gaussian_kernel(radius); - let src: Vec = (0..(width + k.len() - 1) * N).map(|i| ((i * 73) % 251) as f32 / 250.0).collect(); - let mut expected = vec![0.0f32; width * N]; - for x in 0..width { - for (i, w) in k.iter().enumerate() { - for c in 0..N { - expected[x * N + c] += src[(x + i) * N + c] * w; - } - } - } - let mut actual = vec![0.0f32; width * N]; - conv_row::(&src, &mut actual, &k); - assert_eq!(actual, expected, "channels={N} width={width} radius={radius}"); - } - } - } - check::<1>(); - check::<2>(); - check::<3>(); - check::<4>(); - check::<5>(); - } - - #[test] - fn box_gaussian_matches_exact_kernel() { - // A hard vertical edge (premultiplied RGBA-ish, 2 channels) blurred with σ = 12. - let src_rect = Rect::new(-60, -60, 180, 68); - let mut img = Image::new(src_rect, 2); - for y in src_rect.y0..src_rect.y1 { - for x in src_rect.x0..src_rect.x1 { - let i = ((y - src_rect.y0) as usize * src_rect.width() as usize + (x - src_rect.x0) as usize) * 2; - img.data[i] = if x < 60 { 1.0 } else { 0.0 }; - img.data[i + 1] = 1.0; - } - } - let out = Rect::new(0, 0, 120, 8); - let k = gaussian_kernel(12.0); - let exact = conv_sep(&img, out, &k, &k, true); - let fast = box_passes(&img, out, true, &boxes_for_gauss(12.0, 3)); - let err = exact.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err < 0.02, "max error {err}"); - assert_eq!(boxes_for_gauss(10.0, 3).len(), 3); - } - - #[test] - fn box_passes_match_the_direct_box_kernel() { - // Noisy RGBA with varying alpha, so premultiplication and every channel are exercised. - for r in [5usize, 17, 60] { - let (w, h, m) = (40, 24, r as i32 + 2); - let src_rect = Rect::new(-m, -m, w + m, h + m); - let mut img = Image::new(src_rect, 4); - let mut s = 0x9e37_79b9u32; - for v in img.data.iter_mut() { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - *v = (s % 256) as f32 / 255.0; - } - let out = Rect::new(0, 0, w, h); - let k = vec![1.0 / (2 * r + 1) as f32; 2 * r + 1]; - let direct = conv_sep(&img, out, &k, &k, true); - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; - let fast = boxed(&img, out, &ctx, r as f32); - let err = direct.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err < 1e-5, "radius {r}: max error {err}"); - } - } - - /// The definition the running sums must match: each box applied in turn as a direct - /// convolution (cost per pixel proportional to the radius) to `img`, which covers `out` grown - /// by every box's reach (its own edge handling included). - fn direct_boxes(img: &Image, out: Rect, boxes: &[usize], alpha: bool) -> Vec { - let mut cur = img.clone(); - let mut left: i32 = boxes.iter().map(|w| (w / 2) as i32).sum(); - for &w in boxes { - left -= (w / 2) as i32; - let k = vec![1.0 / w as f32; w]; - let rect = out.inflate(left); - cur = Image { rect, ch: img.ch, data: conv_sep(&cur, rect, &k, &k, alpha) }; - } - cur.data - } - - /// Noisy pixels over `r` (alpha varying, some fully transparent). - fn noisy(fmt: PixelFormat, r: Rect, seed: u32) -> Surface { - let mut s = Surface::new(fmt); - let mut st = seed; - let mut v = Vec::new(); - for _ in 0..r.width() * r.height() { - for c in 0..fmt.channels() { - st ^= st << 13; - st ^= st >> 17; - st ^= st << 5; - let x = (st % 256) as f32 / 255.0; - v.push(if fmt.alpha && c + 1 == fmt.channels() && st.is_multiple_of(5) { 0.0 } else { x }); - } - } - s.write_region(r, &v); - s - } - - #[test] - fn layer_box_blurs_match_direct_convolution_at_any_radius() { - // Gaussian and Box Blur at radii up to several times the layer (37 × 23), filtered as a - // document layer (canvas edge repeated, as Photoshop does) and on their own (transparent - // beyond), at every depth, with and without alpha, 1 to 5 channels. - let canvas = Rect::new(0, 0, 37, 23); - let formats = [ - PixelFormat::RGBA8, - PixelFormat::RGBA16, - PixelFormat::RGBA32F, - PixelFormat::GRAY8, - PixelFormat::CMYKA8, - PixelFormat::new(ColorMode::Lab, SampleType::F32, false), - ]; - let params = [ - FilterParams::GaussianBlur { radius: 4.5 }, - FilterParams::GaussianBlur { radius: 9.3 }, - FilterParams::GaussianBlur { radius: 40.0 }, - FilterParams::BoxBlur { radius: 5.0 }, - FilterParams::BoxBlur { radius: 70.0 }, - ]; - for (i, fmt) in formats.into_iter().enumerate() { - let s = noisy(fmt, canvas, 0x9e37_79b9 + i as u32); - // A level of the depth (colour and alpha are each rounded on write) plus float noise. - let tol = match fmt.sample { - SampleType::U8 => 1.0 / 255.0, - SampleType::U16 => 1.0 / 65535.0, - _ => 0.0, - } + 2e-5; - for p in ¶ms { - let boxes = box_widths(p).expect("a box-pass blur"); - let reach: i32 = boxes.iter().map(|w| (w / 2) as i32).sum(); - let area = crate::output_area(p, s.content_bounds(), canvas, None); - for (got, img, out) in [ - (crate::apply_in(&s, p, area, canvas, None, canvas), Image::read_clamped(&s, canvas.inflate(reach), canvas), canvas), - (crate::apply(&s, p, area, canvas, None), Image::read(&s, area.inflate(reach)), area), - ] { - // Compared alpha-weighted: colour is meaningless where (nearly) transparent. - let (mut got, mut want) = (got.read_region(out), direct_boxes(&img, out, &boxes, fmt.alpha)); - premultiply(&mut got, img.ch, fmt.alpha); - premultiply(&mut want, img.ch, fmt.alpha); - let err = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err <= tol, "{} on {fmt:?} over {out:?}: max error {err}", p.label()); - } - } - } - } - - #[test] - fn layer_box_blurs_mix_by_the_selection_and_stop_when_cancelled() { - let canvas = Rect::new(0, 0, 37, 23); - let s = noisy(PixelFormat::RGBA32F, canvas, 7); - let p = FilterParams::GaussianBlur { radius: 12.0 }; - let full = crate::apply_in(&s, &p, canvas, canvas, None, canvas); - let half = Surface::with_default(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false), &[0.5]); - let mixed = crate::apply_in(&s, &p, canvas, canvas, Some(&half), canvas); - for (x, y) in [(0, 0), (18, 11), (36, 22)] { - let (o, f, m) = (s.pixel(x, y), full.pixel(x, y), mixed.pixel(x, y)); - assert!((m[3] - (o[3] + f[3]) / 2.0).abs() < 1e-5, "({x},{y}) alpha"); - for c in 0..3 { - assert!((m[c] * m[3] - (o[c] * o[3] + f[c] * f[3]) / 2.0).abs() < 1e-5, "({x},{y}) premultiplied channel {c}"); - } - } - let cancel = || true; - let ctl = Interrupt::cancel_only(&cancel); - assert!(crate::apply_in_with(&s, &p, canvas, canvas, None, canvas, &ctl).is_none()); - } - - #[test] - fn surface_blur_8bit_histogram_matches_the_direct_sum() { - // 8-bit levels with some fully transparent pixels, which the sum skips. - let src_rect = Rect::new(-14, -14, 40, 34); - let mut img = Image::new(src_rect, 4); - let mut s = 0x6c07_8965u32; - for px in img.data.as_chunks_mut::<4>().0 { - for v in px.iter_mut() { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - *v = (s % 256) as f32 / 255.0; - } - if s.is_multiple_of(7) { - px[3] = 0.0; - } - } - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; - // The output reaches the source edge, where samples read as transparent. - let out = Rect::new(-2, 0, 26, 20); - for (r, threshold) in [(1, 2.0), (4, 15.0), (12, 60.0), (12, 255.0)] { - let t = (threshold / 255.0) * 2.5; - let reach = ((t * 255.0f32).ceil() as usize).min(255); - let fast = surface_8bit(&img, out, &ctx, r, t, reach).expect("8-bit input"); - let direct = surface_direct(&img, out, &ctx, r, t); - let err = direct.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err < 1e-5, "r {r} threshold {threshold}: max error {err}"); - } - // Not 8-bit: the histogram declines and the direct sum is used. - // (0, 0) is inside every window above. - let i = (14 * src_rect.width() as usize + 14) * 4; - img.data[i] = 0.5 / 255.0; - assert!(surface_8bit(&img, out, &ctx, 4, 0.1, 26).is_none()); - } - - #[test] - fn motion_kernel_matches_the_per_pixel_samples() { - // Noisy RGBA with some transparent pixels; the output reaches the source edge. - let src_rect = Rect::new(-30, -30, 50, 40); - let mut img = Image::new(src_rect, 4); - let mut s = 0x1b87_3593u32; - for px in img.data.as_chunks_mut::<4>().0 { - for v in px.iter_mut() { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - *v = (s % 1000) as f32 / 999.0; - } - if s.is_multiple_of(5) { - px[3] = 0.0; - } - } - let out = Rect::new(-12, -6, 30, 22); - for alpha in [true, false] { - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha }; - for (angle, distance) in [(0.0, 1.0), (0.0, 9.0), (30.0, 7.0), (90.0, 12.0), (-45.0, 20.0), (137.0, 33.5), (200.0, 40.0)] { - let fast = motion(&img, out, &ctx, angle, distance); - let offs = motion_offsets(angle, distance); - let slow = average_samples(&img, out, &ctx, |x, y, pts| pts.extend(offs.iter().map(|&(dx, dy)| (x + dx, y + dy)))); - let err = slow.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err < 1e-5, "alpha {alpha} angle {angle} distance {distance}: max error {err}"); - } - } - } - - /// The direct sum with f64 accumulators: the reference both fast paths are held to. - fn surface_reference(src: &Image, out: Rect, ctx: &Ctx, r: i32, t: f32) -> Vec { - let n = src.ch; - let cc = if ctx.alpha { n - 1 } else { n }; - let mut res = Vec::new(); - for y in out.y0..out.y1 { - for x in out.x0..out.x1 { - let p = src.px(x, y); - for &v0 in p.iter().take(cc) { - let c = res.len() % n; - let (mut acc, mut wsum) = (0.0f64, 0.0f64); - for yy in y - r..=y + r { - for xx in x - r..=x + r { - if ctx.alpha && src.get(xx, yy, n - 1) <= 0.0 { - continue; - } - let v = src.get(xx, yy, c); - let w = (1.0 - (v - v0).abs() / t).max(0.0); - acc += f64::from(v) * f64::from(w); - wsum += f64::from(w); - } - } - res.push(if wsum > 0.0 { (acc / wsum) as f32 } else { v0 }); - } - if ctx.alpha { - res.push(p[n - 1]); - } - } - } - res - } - - #[test] - fn surface_blur_ranked_sums_match_the_weighted_window_at_any_depth() { - // 16-bit levels and arbitrary floats (including negatives and values above 1), with some - // fully transparent pixels, which the sum skips. - for (quant, channels, alpha) in [(Some(65535.0f32), 4, true), (None, 4, true), (None, 1, false)] { - let src_rect = Rect::new(-20, -20, 48, 40); - let mut img = Image::new(src_rect, channels); - let mut s = 0x1b87_3593u32; - for px in img.data.chunks_mut(channels) { - for v in px.iter_mut() { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - let raw = (s % 10_000) as f32 / 10_000.0; - *v = quant.map_or(raw * 1.5 - 0.25, |q| (raw * q).round() / q); - } - if alpha && s.is_multiple_of(7) { - px[channels - 1] = 0.0; - } - } - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha }; - // The output reaches the source edge, where samples read as 0. - let out = Rect::new(-6, 0, 30, 22); - for (r, threshold) in [(1, 2.0), (4, 15.0), (12, 60.0), (16, 255.0)] { - let t = (threshold / 255.0) * 2.5; - let ranked = surface_ranked(&img, out, &ctx, r, t).expect("finite input"); - let reference = surface_reference(&img, out, &ctx, r, t); - assert_eq!(ranked.len(), reference.len()); - let err = reference.iter().zip(&ranked).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(err < 1e-6, "{quant:?} r {r} threshold {threshold}: max error {err}"); - } - } - } - - #[test] - fn surface_blur_ranked_declines_non_finite_samples_and_handles_empty_output() { - let src_rect = Rect::new(0, 0, 8, 8); - let mut img = Image::new(src_rect, 1); - img.data.fill(0.5); - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Grayscale, alpha: false }; - assert_eq!(surface_ranked(&img, Rect::new(2, 2, 2, 6), &ctx, 2, 0.1), Some(Vec::new())); - img.data[9] = f32::NAN; - assert!(surface_ranked(&img, Rect::new(0, 0, 8, 8), &ctx, 2, 0.1).is_none()); - // The public entry still answers, through the direct sum. - assert_eq!(surface(&img, Rect::new(0, 0, 8, 8), &ctx, 12.0, 15.0).len(), 64); - } - - /// Release-mode timings of the direct sum and the ranked path on one 256 px tile of - /// 16-bit-like and float noise (`cargo test -p photocraft-algo --release -- --ignored --nocapture surface_timing`). - #[test] - #[ignore] - fn surface_timing() { - for (label, quant) in [("16-bit", Some(65535.0f32)), ("float", None)] { - for r in [2, 4, 8, 16, 32, 64, 100] { - let out = Rect::new(0, 0, 256, 256); - let src_rect = out.inflate(r); - let mut img = Image::new(src_rect, 4); - let mut s = 0x2545_f491u32; - let len = img.data.len(); - for (i, v) in img.data.iter_mut().enumerate() { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - // Smooth gradient plus noise, so the threshold keeps a realistic share. - let x = (i / 4) as f32 / len as f32 * 4.0; - let raw = (0.5 + 0.4 * (x * 6.0).sin() + (s % 1000) as f32 / 20000.0).clamp(0.0, 1.0); - *v = quant.map_or(raw, |q| (raw * q).round() / q); - } - let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; - let t = (15.0 / 255.0) * 2.5; - let a = std::time::Instant::now(); - let direct = surface_direct(&img, out, &ctx, r, t); - let td = a.elapsed(); - let a = std::time::Instant::now(); - let ranked = surface_ranked(&img, out, &ctx, r, t).expect("finite"); - let tr = a.elapsed(); - let err = direct.iter().zip(&ranked).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - println!("{label} r {r:3}: direct {td:>10.2?} ranked {tr:>10.2?} max err {err:.2e}"); - } - } - } -} +mod tests; diff --git a/crates/algo/src/blur/motion_fft.rs b/crates/algo/src/blur/motion_fft.rs index 9da1b1a1b7..a0bf9dce6a 100644 --- a/crates/algo/src/blur/motion_fft.rs +++ b/crates/algo/src/blur/motion_fft.rs @@ -361,23 +361,7 @@ impl Plan { { return None; } - let mut scales = [0.0f64; 5]; - for (i, pixel) in src.data.chunks_exact(src.ch).enumerate() { - if i % 4096 == 0 && ctl.cancelled() { - return None; - } - if pixel.iter().any(|value| !value.is_finite() || value.abs() > MAX_SAMPLE) - || (alpha && !pixel.last().is_some_and(|value| (0.0..=1.0).contains(value))) - { - return None; - } - let a = if alpha { *pixel.last()? } else { 1.0 }; - for (c, &value) in pixel.iter().enumerate() { - let value = if alpha && c + 1 < src.ch { value * a } else { value }; - let scale = scales.get_mut(c)?; - *scale = scale.max(f64::from(value.abs())); - } - } + let scales = channel_scales(src, alpha, ctl)?; let mut result = buffer(ow.checked_mul(oh)?.checked_mul(src.ch)?, 0.0f64)?; let read = self.read_rect(out)?; let used = read.intersect(&src.rect); @@ -393,81 +377,18 @@ impl Plan { if ctl.cancelled() { return None; } + let pair = ChannelPair { src, alpha, scales: &scales, channel }; data.fill(Number::default()); - for y in used.y0..used.y1 { - if (y - used.y0) % 8 == 0 && ctl.cancelled() { - return None; - } - let source_start = - usize::try_from(y - src.rect.y0).ok()?.checked_mul(sw)?.checked_add(usize::try_from(used.x0 - src.rect.x0).ok()?)?.checked_mul(src.ch)?; - let source_len = usize::try_from(used.x1 - used.x0).ok()?.checked_mul(src.ch)?; - let row = src.data.get(source_start..source_start.checked_add(source_len)?)?; - let start = usize::try_from(y - read.y0).ok()?.checked_mul(self.width)?.checked_add(usize::try_from(used.x0 - read.x0).ok()?)?; - let dst = data.get_mut(start..start.checked_add(source_len / src.ch)?)?; - for (pixel, dst) in row.chunks_exact(src.ch).zip(dst) { - let a = if alpha { *pixel.last()? } else { 1.0 }; - // Normalize each channel separately before packing a pair. - // Otherwise a bright HDR colour's FFT residual could swamp - // a small alpha channel sharing the imaginary component. - let sample = |c: usize| -> f64 { - match (pixel.get(c), scales.get(c)) { - (Some(&value), Some(&scale)) if scale > 0.0 => f64::from(if alpha && c + 1 < src.ch { value * a } else { value }) / scale, - _ => 0.0, - } - }; - *dst = Number::new(sample(channel), sample(channel + 1)); - } - } + self.pack_channel_pair(&pair, used, read, &mut data, ctl)?; self.transform(&mut data, &mut transposed, &mut scratch, false, ctl)?; for (value, kernel) in data.iter_mut().zip(&self.kernel) { *value *= kernel; } self.transform(&mut data, &mut transposed, &mut scratch, true, ctl)?; - let x0 = usize::try_from(-self.min_x).ok()?; - let y0 = usize::try_from(-self.min_y).ok()?; - for (y, dst) in result.chunks_exact_mut(ow * src.ch).enumerate() { - let start = y.checked_add(y0)?.checked_mul(self.width)?.checked_add(x0)?; - let values = data.get(start..start.checked_add(ow)?)?; - for (pixel, value) in dst.chunks_exact_mut(src.ch).zip(values) { - *pixel.get_mut(channel)? = value.re * norm * scales.get(channel)?; - if let Some(next) = pixel.get_mut(channel + 1) { - *next = value.im * norm * scales.get(channel + 1)?; - } - } - } + self.unpack_channel_pair(&pair, &data, &mut result, ow, norm)?; } if alpha { - // The row kernel accumulates in f32. Recompute very small alpha - // near its cutoff so a different summation order cannot expose colour. - let uncertainty = f64::from(self.distance.abs().ceil()) * f64::from(f32::EPSILON) * scales.get(src.ch - 1)?; - for (i, pixel) in result.chunks_exact_mut(src.ch).enumerate() { - if i % 16 == 0 && ctl.cancelled() { - return None; - } - let a = pixel.last_mut()?; - // Valid alpha is nonnegative. Remove transform roundoff at - // zero to keep fully transparent regions empty. - if a.abs() < 1e-14 * scales.get(src.ch - 1)? { - *a = 0.0; - } - *a = a.clamp(0.0, 1.0); - let a = *a; - // The row kernel's f32 sum can straddle the cutoff at long distances. - // Keep its branch and straight colour exactly for that narrow - // range, rather than amplifying a tiny alpha difference. - if a > 0.0 && (a <= 1.01e-7 + uncertainty || (0.99e-7..=1.01e-7).contains(&(a as f32))) { - let x = out.x0.checked_add(i32::try_from(i % ow).ok()?)?; - let y = out.y0.checked_add(i32::try_from(i / ow).ok()?)?; - let original = sample_row_point(src, x, y, alpha, &self.taps, ctl)?; - for (dst, value) in pixel.iter_mut().zip(original) { - *dst = f64::from(value); - } - continue; - } - for value in pixel.iter_mut().take(src.ch - 1) { - *value = if a as f32 > 1e-7 { *value / a } else { 0.0 }; - } - } + self.restore_small_alpha(src, out, ow, &scales, &mut result, ctl)?; } let mut output = buffer(result.len(), 0.0f32)?; for (dst, value) in output.iter_mut().zip(result) { @@ -475,6 +396,125 @@ impl Plan { } Some(output) } + + /// Packs the pair of channels into the real and imaginary parts of `data`, over the source + /// rows that fall inside the transform tile, each channel normalised by its own scale. + fn pack_channel_pair(&self, pair: &ChannelPair, used: Rect, read: Rect, data: &mut [Number], ctl: &Interrupt) -> Option<()> { + let ChannelPair { src, alpha, scales, channel } = *pair; + let (sw, _) = dimensions(src.rect)?; + for y in used.y0..used.y1 { + if (y - used.y0) % 8 == 0 && ctl.cancelled() { + return None; + } + let source_start = + usize::try_from(y - src.rect.y0).ok()?.checked_mul(sw)?.checked_add(usize::try_from(used.x0 - src.rect.x0).ok()?)?.checked_mul(src.ch)?; + let source_len = usize::try_from(used.x1 - used.x0).ok()?.checked_mul(src.ch)?; + let row = src.data.get(source_start..source_start.checked_add(source_len)?)?; + let start = usize::try_from(y - read.y0).ok()?.checked_mul(self.width)?.checked_add(usize::try_from(used.x0 - read.x0).ok()?)?; + let dst = data.get_mut(start..start.checked_add(source_len / src.ch)?)?; + for (pixel, dst) in row.chunks_exact(src.ch).zip(dst) { + let a = if alpha { *pixel.last()? } else { 1.0 }; + // Normalize each channel separately before packing a pair. + // Otherwise a bright HDR colour's FFT residual could swamp + // a small alpha channel sharing the imaginary component. + let sample = |c: usize| -> f64 { + match (pixel.get(c), scales.get(c)) { + (Some(&value), Some(&scale)) if scale > 0.0 => f64::from(if alpha && c + 1 < src.ch { value * a } else { value }) / scale, + _ => 0.0, + } + }; + *dst = Number::new(sample(channel), sample(channel + 1)); + } + } + Some(()) + } + + /// Writes the filtered pair of channels from `data` into the output pixels, undoing the + /// per-channel normalisation. + fn unpack_channel_pair(&self, pair: &ChannelPair, data: &[Number], result: &mut [f64], ow: usize, norm: f64) -> Option<()> { + let ChannelPair { src, scales, channel, .. } = *pair; + let x0 = usize::try_from(-self.min_x).ok()?; + let y0 = usize::try_from(-self.min_y).ok()?; + for (y, dst) in result.chunks_exact_mut(ow * src.ch).enumerate() { + let start = y.checked_add(y0)?.checked_mul(self.width)?.checked_add(x0)?; + let values = data.get(start..start.checked_add(ow)?)?; + for (pixel, value) in dst.chunks_exact_mut(src.ch).zip(values) { + *pixel.get_mut(channel)? = value.re * norm * scales.get(channel)?; + if let Some(next) = pixel.get_mut(channel + 1) { + *next = value.im * norm * scales.get(channel + 1)?; + } + } + } + Some(()) + } + + /// Straightens the colour of the filtered pixels by their alpha, taking care of the pixels + /// whose alpha is at the cutoff. + fn restore_small_alpha(&self, src: &Image, out: Rect, ow: usize, scales: &[f64; 5], result: &mut [f64], ctl: &Interrupt) -> Option<()> { + // The row kernel accumulates in f32. Recompute very small alpha + // near its cutoff so a different summation order cannot expose colour. + let uncertainty = f64::from(self.distance.abs().ceil()) * f64::from(f32::EPSILON) * scales.get(src.ch - 1)?; + for (i, pixel) in result.chunks_exact_mut(src.ch).enumerate() { + if i % 16 == 0 && ctl.cancelled() { + return None; + } + let a = pixel.last_mut()?; + // Valid alpha is nonnegative. Remove transform roundoff at + // zero to keep fully transparent regions empty. + if a.abs() < 1e-14 * scales.get(src.ch - 1)? { + *a = 0.0; + } + *a = a.clamp(0.0, 1.0); + let a = *a; + // The row kernel's f32 sum can straddle the cutoff at long distances. + // Keep its branch and straight colour exactly for that narrow + // range, rather than amplifying a tiny alpha difference. + if a > 0.0 && (a <= 1.01e-7 + uncertainty || (0.99e-7..=1.01e-7).contains(&(a as f32))) { + let x = out.x0.checked_add(i32::try_from(i % ow).ok()?)?; + let y = out.y0.checked_add(i32::try_from(i / ow).ok()?)?; + let original = sample_row_point(src, x, y, true, &self.taps, ctl)?; + for (dst, value) in pixel.iter_mut().zip(original) { + *dst = f64::from(value); + } + continue; + } + for value in pixel.iter_mut().take(src.ch - 1) { + *value = if a as f32 > 1e-7 { *value / a } else { 0.0 }; + } + } + Some(()) + } +} + +/// One pair of channels (packed as real and imaginary parts) of a filter call. +#[derive(Clone, Copy)] +struct ChannelPair<'a> { + src: &'a Image, + alpha: bool, + scales: &'a [f64; 5], + channel: usize, +} + +/// Per-channel peak magnitude of the (alpha-premultiplied) samples. `None` when a sample is not +/// finite, is too large, or its alpha is outside 0..=1, and when cancelled. +fn channel_scales(src: &Image, alpha: bool, ctl: &Interrupt) -> Option<[f64; 5]> { + let mut scales = [0.0f64; 5]; + for (i, pixel) in src.data.chunks_exact(src.ch).enumerate() { + if i % 4096 == 0 && ctl.cancelled() { + return None; + } + if pixel.iter().any(|value| !value.is_finite() || value.abs() > MAX_SAMPLE) || (alpha && !pixel.last().is_some_and(|value| (0.0..=1.0).contains(value))) + { + return None; + } + let a = if alpha { *pixel.last()? } else { 1.0 }; + for (c, &value) in pixel.iter().enumerate() { + let value = if alpha && c + 1 < src.ch { value * a } else { value }; + let scale = scales.get_mut(c)?; + *scale = scale.max(f64::from(value.abs())); + } + } + Some(scales) } #[cfg(test)] diff --git a/crates/algo/src/blur/tests.rs b/crates/algo/src/blur/tests.rs new file mode 100644 index 0000000000..028ac65dcc --- /dev/null +++ b/crates/algo/src/blur/tests.rs @@ -0,0 +1,530 @@ +use super::*; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_raster::Surface; + +/// #8544, rendered: Gaussian Blur at 0.1–0.3 px through the filter itself (the direct-kernel +/// path) visibly softens a hard edge, more with each step, as the kernel predicts. +#[test] +fn small_gaussian_radii_soften_a_rendered_edge() { + let canvas = Rect::new(0, 0, 20, 4); + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(canvas, &[0.0, 0.0, 0.0, 1.0]); + s.fill_rect(Rect::new(10, 0, 20, 4), &[1.0, 1.0, 1.0, 1.0]); + let mut last = 0.0f32; + for radius in [0.1f32, 0.2, 0.3] { + let p = FilterParams::GaussianBlur { radius }; + assert!(box_widths(&p).is_none(), "the direct-kernel path"); + let out = crate::apply(&s, &p, canvas, canvas, None); + // The last black pixel takes the white side's share of the kernel. + let got = out.pixel(9, 1)[0]; + let k = gaussian_blur_kernel(radius); + let want: f32 = k[k.len() / 2 + 1..].iter().sum(); + assert!((got - want).abs() <= 1.0 / 255.0, "radius {radius}: {got} vs {want}"); + assert!(got >= 1.0 / 255.0 && got > last, "radius {radius}: {got} after {last}"); + last = got; + } +} + +/// #8544: Gaussian Blur spreads a hard edge for every radius from 0.1, growing smoothly +/// with the radius (no dead zone up to 0.3, then a jump); from 1 up the kernel is unchanged. +#[test] +fn small_gaussian_radii_blur_smoothly() { + let spread = |k: &[f32]| { + let r = (k.len() / 2) as f32; + k.iter().enumerate().map(|(i, w)| w * (i as f32 - r).powi(2)).sum::() + }; + let mut last = 0.0f32; + for step in 1..=20 { + let radius = step as f32 * 0.05; + let k = gaussian_blur_kernel(radius); + assert!((k.iter().sum::() - 1.0).abs() < 1e-5, "normalised at {radius}"); + if radius < 1.0 { + assert!((spread(&k) - radius * radius).abs() < 1e-4, "variance at {radius}: {}", spread(&k)); + } + // A hard 0 → 1 edge: the first pixel past it takes the kernel's tail beyond the centre. + let c = k.len() / 2; + let edge: f32 = k[c + 1..].iter().sum(); + assert!(edge > last, "radius {radius}: edge {edge} after {last}"); + if radius > 0.1 { + assert!(edge - last < 0.05, "radius {radius}: jump {} from {last}", edge - last); + } + last = edge; + } + assert!(gaussian_blur_kernel(0.1).len() >= 3, "0.1 blurs"); + // No step where the variance-matched range hands over to plain sampling. + let edge = |r: f32| { + let k = gaussian_blur_kernel(r); + k[k.len() / 2 + 1..].iter().sum::() + }; + assert!((edge(0.999) - edge(1.0)).abs() < 0.005, "{} vs {}", edge(0.999), edge(1.0)); + // No dip where a per-radius width used to add taps (1/3 and 2/3). + for pair in [(0.33f32, 0.34f32), (0.66, 0.67)] { + assert!(edge(pair.1) > edge(pair.0), "edge falls from {} to {}", pair.0, pair.1); + } + // Exact variance right up to the hand-over, past where `gaussian_kernel` changes width. + for radius in [0.98f32, 0.995, 0.998, 0.9995] { + let k = gaussian_blur_kernel(radius); + assert!((spread(&k) - radius * radius).abs() < 2e-5, "variance at {radius}: {}", spread(&k)); + } + for radius in [1.0, 1.5, 3.0, 4.0] { + assert_eq!(gaussian_blur_kernel(radius), gaussian_kernel(radius), "{radius}"); + } + assert_eq!(gaussian_blur_kernel(0.0), vec![1.0]); +} + +#[test] +fn radial_blur_samples_large_arcs_beyond_64_intervals() { + let bounds = Rect::new(0, 0, 1000, 1000); + let out = Rect::new(999, 500, 1000, 501); + let ctx = Ctx { bounds, mode: photocraft_color::ColorMode::Grayscale, alpha: false }; + let mut img = Image::new(bounds, 1); + let (cx, cy) = (500.0f32, 500.0f32); + let (x, y) = (999.5f32, 500.5f32); + let (dx, dy) = (x - cx, y - cy); + let arc = 100.0f32.to_radians(); + let count = radial_intervals(arc * dx.hypot(dy), RadialQuality::Good); + assert_eq!(count, 256); + assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Draft), 64); + assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Good), 256); + assert_eq!(radial_intervals(f32::INFINITY, RadialQuality::Best), 4096); + assert_eq!(radial_intervals(f32::NAN, RadialQuality::Best), 1); + + // Put a small bright patch halfway between two samples from the former 64-interval + // limit. Dense sampling should pick it up; the coarse path misses it entirely. + let sparse_midpoint = (31.5 / 64.0 - 0.5) * arc; + let (s, c) = sparse_midpoint.sin_cos(); + let px = (cx + dx * c - dy * s - 0.5).round() as i32; + let py = (cy + dx * s + dy * c - 0.5).round() as i32; + for yy in py - 1..=py + 1 { + for xx in px - 1..=px + 1 { + let index = (yy - bounds.y0) as usize * bounds.width() as usize + (xx - bounds.x0) as usize; + img.data[index] = 1.0; + } + } + + let sample_path = |x: f32, y: f32, pts: &mut Vec<(f32, f32)>, intervals: usize| { + let (dx, dy) = (x - cx, y - cy); + for i in 0..=intervals { + let t = (i as f32 / intervals as f32 - 0.5) * arc; + let (s, c) = t.sin_cos(); + pts.push((cx + dx * c - dy * s, cy + dx * s + dy * c)); + } + }; + let actual = radial(&img, out, &ctx, 100.0, RadialMethod::Spin, RadialQuality::Good, (0.5, 0.5))[0]; + let expected = average_samples(&img, out, &ctx, |x, y, pts| sample_path(x, y, pts, 8192))[0]; + let coarse = average_samples(&img, out, &ctx, |x, y, pts| sample_path(x, y, pts, 64))[0]; + + assert!(actual > 0.001, "dense radial samples should resolve the bright patch, got {actual}"); + assert!((actual - expected).abs() < 0.0015, "radial result {actual} differs from dense reference {expected}"); + assert!(coarse < expected * 0.1, "former 64-interval sampling unexpectedly resolved the patch: {coarse} vs {expected}"); +} + +#[test] +fn fixed_channel_convolution_matches_scalar_tap_order() { + fn check() { + for width in [1, 7, 256] { + for radius in [0.0, 0.5, 3.0, 4.0] { + let k = gaussian_kernel(radius); + let src: Vec = (0..(width + k.len() - 1) * N).map(|i| ((i * 73) % 251) as f32 / 250.0).collect(); + let mut expected = vec![0.0f32; width * N]; + for x in 0..width { + for (i, w) in k.iter().enumerate() { + for c in 0..N { + expected[x * N + c] += src[(x + i) * N + c] * w; + } + } + } + let mut actual = vec![0.0f32; width * N]; + conv_row::(&src, &mut actual, &k); + assert_eq!(actual, expected, "channels={N} width={width} radius={radius}"); + } + } + } + check::<1>(); + check::<2>(); + check::<3>(); + check::<4>(); + check::<5>(); +} + +#[test] +fn box_gaussian_matches_exact_kernel() { + // A hard vertical edge (premultiplied RGBA-ish, 2 channels) blurred with σ = 12. + let src_rect = Rect::new(-60, -60, 180, 68); + let mut img = Image::new(src_rect, 2); + for y in src_rect.y0..src_rect.y1 { + for x in src_rect.x0..src_rect.x1 { + let i = ((y - src_rect.y0) as usize * src_rect.width() as usize + (x - src_rect.x0) as usize) * 2; + img.data[i] = if x < 60 { 1.0 } else { 0.0 }; + img.data[i + 1] = 1.0; + } + } + let out = Rect::new(0, 0, 120, 8); + let k = gaussian_kernel(12.0); + let exact = conv_sep(&img, out, &k, &k, true); + let fast = box_passes(&img, out, true, &boxes_for_gauss(12.0, 3)); + let err = exact.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err < 0.02, "max error {err}"); + assert_eq!(boxes_for_gauss(10.0, 3).len(), 3); +} + +#[test] +fn box_passes_match_the_direct_box_kernel() { + // Noisy RGBA with varying alpha, so premultiplication and every channel are exercised. + for r in [5usize, 17, 60] { + let (w, h, m) = (40, 24, r as i32 + 2); + let src_rect = Rect::new(-m, -m, w + m, h + m); + let mut img = Image::new(src_rect, 4); + let mut s = 0x9e37_79b9u32; + for v in img.data.iter_mut() { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + *v = (s % 256) as f32 / 255.0; + } + let out = Rect::new(0, 0, w, h); + let k = vec![1.0 / (2 * r + 1) as f32; 2 * r + 1]; + let direct = conv_sep(&img, out, &k, &k, true); + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; + let fast = boxed(&img, out, &ctx, r as f32); + let err = direct.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err < 1e-5, "radius {r}: max error {err}"); + } +} + +/// The definition the running sums must match: each box applied in turn as a direct +/// convolution (cost per pixel proportional to the radius) to `img`, which covers `out` grown +/// by every box's reach (its own edge handling included). +fn direct_boxes(img: &Image, out: Rect, boxes: &[usize], alpha: bool) -> Vec { + let mut cur = img.clone(); + let mut left: i32 = boxes.iter().map(|w| (w / 2) as i32).sum(); + for &w in boxes { + left -= (w / 2) as i32; + let k = vec![1.0 / w as f32; w]; + let rect = out.inflate(left); + cur = Image { rect, ch: img.ch, data: conv_sep(&cur, rect, &k, &k, alpha) }; + } + cur.data +} + +/// Noisy pixels over `r` (alpha varying, some fully transparent). +fn noisy(fmt: PixelFormat, r: Rect, seed: u32) -> Surface { + let mut s = Surface::new(fmt); + let mut st = seed; + let mut v = Vec::new(); + for _ in 0..r.width() * r.height() { + for c in 0..fmt.channels() { + st ^= st << 13; + st ^= st >> 17; + st ^= st << 5; + let x = (st % 256) as f32 / 255.0; + v.push(if fmt.alpha && c + 1 == fmt.channels() && st.is_multiple_of(5) { 0.0 } else { x }); + } + } + s.write_region(r, &v); + s +} + +#[test] +fn layer_box_blurs_match_direct_convolution_at_any_radius() { + // Gaussian and Box Blur at radii up to several times the layer (37 × 23), filtered as a + // document layer (canvas edge repeated, as Photoshop does) and on their own (transparent + // beyond), at every depth, with and without alpha, 1 to 5 channels. + let canvas = Rect::new(0, 0, 37, 23); + let formats = [ + PixelFormat::RGBA8, + PixelFormat::RGBA16, + PixelFormat::RGBA32F, + PixelFormat::GRAY8, + PixelFormat::CMYKA8, + PixelFormat::new(ColorMode::Lab, SampleType::F32, false), + ]; + let params = [ + FilterParams::GaussianBlur { radius: 4.5 }, + FilterParams::GaussianBlur { radius: 9.3 }, + FilterParams::GaussianBlur { radius: 40.0 }, + FilterParams::BoxBlur { radius: 5.0 }, + FilterParams::BoxBlur { radius: 70.0 }, + ]; + for (i, fmt) in formats.into_iter().enumerate() { + let s = noisy(fmt, canvas, 0x9e37_79b9 + i as u32); + // A level of the depth (colour and alpha are each rounded on write) plus float noise. + let tol = match fmt.sample { + SampleType::U8 => 1.0 / 255.0, + SampleType::U16 => 1.0 / 65535.0, + _ => 0.0, + } + 2e-5; + for p in ¶ms { + let boxes = box_widths(p).expect("a box-pass blur"); + let reach: i32 = boxes.iter().map(|w| (w / 2) as i32).sum(); + let area = crate::output_area(p, s.content_bounds(), canvas, None); + for (got, img, out) in [ + (crate::apply_in(&s, p, area, canvas, None, canvas), Image::read_clamped(&s, canvas.inflate(reach), canvas), canvas), + (crate::apply(&s, p, area, canvas, None), Image::read(&s, area.inflate(reach)), area), + ] { + // Compared alpha-weighted: colour is meaningless where (nearly) transparent. + let (mut got, mut want) = (got.read_region(out), direct_boxes(&img, out, &boxes, fmt.alpha)); + premultiply(&mut got, img.ch, fmt.alpha); + premultiply(&mut want, img.ch, fmt.alpha); + let err = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err <= tol, "{} on {fmt:?} over {out:?}: max error {err}", p.label()); + } + } + } +} + +#[test] +fn layer_box_blurs_mix_by_the_selection_and_stop_when_cancelled() { + let canvas = Rect::new(0, 0, 37, 23); + let s = noisy(PixelFormat::RGBA32F, canvas, 7); + let p = FilterParams::GaussianBlur { radius: 12.0 }; + let full = crate::apply_in(&s, &p, canvas, canvas, None, canvas); + let half = Surface::with_default(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false), &[0.5]); + let mixed = crate::apply_in(&s, &p, canvas, canvas, Some(&half), canvas); + for (x, y) in [(0, 0), (18, 11), (36, 22)] { + let (o, f, m) = (s.pixel(x, y), full.pixel(x, y), mixed.pixel(x, y)); + assert!((m[3] - (o[3] + f[3]) / 2.0).abs() < 1e-5, "({x},{y}) alpha"); + for c in 0..3 { + assert!((m[c] * m[3] - (o[c] * o[3] + f[c] * f[3]) / 2.0).abs() < 1e-5, "({x},{y}) premultiplied channel {c}"); + } + } + let cancel = || true; + let ctl = Interrupt::cancel_only(&cancel); + assert!(crate::apply_in_with(&s, &p, canvas, canvas, None, canvas, &ctl).is_none()); +} + +#[test] +fn surface_blur_8bit_histogram_matches_the_direct_sum() { + // 8-bit levels with some fully transparent pixels, which the sum skips. + let src_rect = Rect::new(-14, -14, 40, 34); + let mut img = Image::new(src_rect, 4); + let mut s = 0x6c07_8965u32; + for px in img.data.as_chunks_mut::<4>().0 { + for v in px.iter_mut() { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + *v = (s % 256) as f32 / 255.0; + } + if s.is_multiple_of(7) { + px[3] = 0.0; + } + } + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; + // The output reaches the source edge, where samples read as transparent. + let out = Rect::new(-2, 0, 26, 20); + for (r, threshold) in [(1, 2.0), (4, 15.0), (12, 60.0), (12, 255.0)] { + let t = (threshold / 255.0) * 2.5; + let reach = ((t * 255.0f32).ceil() as usize).min(255); + let fast = surface_8bit(&img, out, &ctx, r, t, reach).expect("8-bit input"); + let direct = surface_direct(&img, out, &ctx, r, t); + let err = direct.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err < 1e-5, "r {r} threshold {threshold}: max error {err}"); + } + // Not 8-bit: the histogram declines and the direct sum is used. + // (0, 0) is inside every window above. + let i = (14 * src_rect.width() as usize + 14) * 4; + img.data[i] = 0.5 / 255.0; + assert!(surface_8bit(&img, out, &ctx, 4, 0.1, 26).is_none()); +} + +#[test] +fn motion_kernel_matches_the_per_pixel_samples() { + // Noisy RGBA with some transparent pixels; the output reaches the source edge. + let src_rect = Rect::new(-30, -30, 50, 40); + let mut img = Image::new(src_rect, 4); + let mut s = 0x1b87_3593u32; + for px in img.data.as_chunks_mut::<4>().0 { + for v in px.iter_mut() { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + *v = (s % 1000) as f32 / 999.0; + } + if s.is_multiple_of(5) { + px[3] = 0.0; + } + } + let out = Rect::new(-12, -6, 30, 22); + for alpha in [true, false] { + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha }; + for (angle, distance) in [(0.0, 1.0), (0.0, 9.0), (30.0, 7.0), (90.0, 12.0), (-45.0, 20.0), (137.0, 33.5), (200.0, 40.0)] { + let fast = motion(&img, out, &ctx, angle, distance); + let offs = motion_offsets(angle, distance); + let slow = average_samples(&img, out, &ctx, |x, y, pts| pts.extend(offs.iter().map(|&(dx, dy)| (x + dx, y + dy)))); + let err = slow.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err < 1e-5, "alpha {alpha} angle {angle} distance {distance}: max error {err}"); + } + } +} + +/// The direct sum with f64 accumulators: the reference both fast paths are held to. +fn surface_reference(src: &Image, out: Rect, ctx: &Ctx, r: i32, t: f32) -> Vec { + let n = src.ch; + let cc = if ctx.alpha { n - 1 } else { n }; + let mut res = Vec::new(); + for y in out.y0..out.y1 { + for x in out.x0..out.x1 { + let p = src.px(x, y); + for &v0 in p.iter().take(cc) { + let c = res.len() % n; + let (mut acc, mut wsum) = (0.0f64, 0.0f64); + for yy in y - r..=y + r { + for xx in x - r..=x + r { + if ctx.alpha && src.get(xx, yy, n - 1) <= 0.0 { + continue; + } + let v = src.get(xx, yy, c); + let w = (1.0 - (v - v0).abs() / t).max(0.0); + acc += f64::from(v) * f64::from(w); + wsum += f64::from(w); + } + } + res.push(if wsum > 0.0 { (acc / wsum) as f32 } else { v0 }); + } + if ctx.alpha { + res.push(p[n - 1]); + } + } + } + res +} + +#[test] +fn surface_blur_ranked_sums_match_the_weighted_window_at_any_depth() { + // 16-bit levels and arbitrary floats (including negatives and values above 1), with some + // fully transparent pixels, which the sum skips. + for (quant, channels, alpha) in [(Some(65535.0f32), 4, true), (None, 4, true), (None, 1, false)] { + let src_rect = Rect::new(-20, -20, 48, 40); + let mut img = Image::new(src_rect, channels); + let mut s = 0x1b87_3593u32; + for px in img.data.chunks_mut(channels) { + for v in px.iter_mut() { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + let raw = (s % 10_000) as f32 / 10_000.0; + *v = quant.map_or(raw * 1.5 - 0.25, |q| (raw * q).round() / q); + } + if alpha && s.is_multiple_of(7) { + px[channels - 1] = 0.0; + } + } + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha }; + // The output reaches the source edge, where samples read as 0. + let out = Rect::new(-6, 0, 30, 22); + for (r, threshold) in [(1, 2.0), (4, 15.0), (12, 60.0), (16, 255.0)] { + let t = (threshold / 255.0) * 2.5; + let ranked = surface_ranked(&img, out, &ctx, r, t).expect("finite input"); + let reference = surface_reference(&img, out, &ctx, r, t); + assert_eq!(ranked.len(), reference.len()); + let err = reference.iter().zip(&ranked).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(err < 1e-6, "{quant:?} r {r} threshold {threshold}: max error {err}"); + } + } +} + +#[test] +fn surface_blur_ranked_declines_non_finite_samples_and_handles_empty_output() { + let src_rect = Rect::new(0, 0, 8, 8); + let mut img = Image::new(src_rect, 1); + img.data.fill(0.5); + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Grayscale, alpha: false }; + assert_eq!(surface_ranked(&img, Rect::new(2, 2, 2, 6), &ctx, 2, 0.1), Some(Vec::new())); + img.data[9] = f32::NAN; + assert!(surface_ranked(&img, Rect::new(0, 0, 8, 8), &ctx, 2, 0.1).is_none()); + // The public entry still answers, through the direct sum. + assert_eq!(surface(&img, Rect::new(0, 0, 8, 8), &ctx, 12.0, 15.0).len(), 64); +} + +/// Release-mode timings of the direct sum and the ranked path on one 256 px tile of +/// 16-bit-like and float noise (`cargo test -p photocraft-algo --release -- --ignored --nocapture surface_timing`). +#[test] +#[ignore] +fn surface_timing() { + for (label, quant) in [("16-bit", Some(65535.0f32)), ("float", None)] { + for r in [2, 4, 8, 16, 32, 64, 100] { + let out = Rect::new(0, 0, 256, 256); + let src_rect = out.inflate(r); + let mut img = Image::new(src_rect, 4); + let mut s = 0x2545_f491u32; + let len = img.data.len(); + for (i, v) in img.data.iter_mut().enumerate() { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + // Smooth gradient plus noise, so the threshold keeps a realistic share. + let x = (i / 4) as f32 / len as f32 * 4.0; + let raw = (0.5 + 0.4 * (x * 6.0).sin() + (s % 1000) as f32 / 20000.0).clamp(0.0, 1.0); + *v = quant.map_or(raw, |q| (raw * q).round() / q); + } + let ctx = Ctx { bounds: src_rect, mode: crate::ColorMode::Rgb, alpha: true }; + let t = (15.0 / 255.0) * 2.5; + let a = std::time::Instant::now(); + let direct = surface_direct(&img, out, &ctx, r, t); + let td = a.elapsed(); + let a = std::time::Instant::now(); + let ranked = surface_ranked(&img, out, &ctx, r, t).expect("finite"); + let tr = a.elapsed(); + let err = direct.iter().zip(&ranked).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + println!("{label} r {r:3}: direct {td:>10.2?} ranked {tr:>10.2?} max err {err:.2e}"); + } + } +} + +/// A column strip's passes stop between passes once cancelled, not only before the strip +/// (#6205); uncancelled, every pass runs. +#[test] +fn line_passes_stop_between_passes_when_cancelled() { + use std::sync::atomic::{AtomicUsize, Ordering}; + let (radii, w, len) = ([2usize, 3, 2], 4usize, 50usize); + let reach: usize = radii.iter().sum(); + let line: Vec = (0..(len + 2 * reach) * w).map(|i| (i % 7) as f32 + 1.0).collect(); + let run = |ctl: &Interrupt| { + let mut s = Scratch { line: line.clone(), ..Scratch::default() }; + let mut dst = vec![0.0f32; len * w]; + line_passes(&mut dst, w, &radii, &mut s, ctl); + dst + }; + assert!(run(&Interrupt::NONE).iter().all(|v| *v > 0.0)); + let calls = AtomicUsize::new(0); + let cancel = || calls.fetch_add(1, Ordering::SeqCst) >= 1; + // Cancelled after the first pass: the last pass (the one writing `dst`) never runs. + assert!(run(&Interrupt::cancel_only(&cancel)).iter().all(|v| *v == 0.0)); + assert_eq!(calls.load(Ordering::SeqCst), 2); +} + +/// A box blur cancelled at any of its checks returns `None` (never a half-blurred result); +/// one that is asked but never cancelled is bit-identical to the uninterruptible run. +#[test] +fn box_blur_honours_a_cancel_at_every_check() { + use std::sync::atomic::{AtomicUsize, Ordering}; + let (n, src) = (4usize, Rect::new(0, 0, 150, 90)); + let px: Vec = (0..src.width() as usize * src.height() as usize * n).map(|i| (i as f32 * 0.13).sin() * 0.5 + 0.5).collect(); + let read = |r: Rect, buf: &mut Vec| { + buf.clear(); + let (x0, x1) = ((r.x0 * n as i32) as usize, (r.x1 * n as i32) as usize); + let row = r.y0 as usize * src.width() as usize * n; + buf.extend_from_slice(&px[row + x0..row + x1]); + }; + let Some(boxes) = box_widths(&FilterParams::GaussianBlur { radius: 12.0 }) else { panic!("box passes") }; + let out = Rect::new(-5, 3, 155, 80); + let want = box_blur(src, &read, out, n, true, &boxes, &Interrupt::NONE).unwrap(); + let checks = AtomicUsize::new(0); + let count = || { + checks.fetch_add(1, Ordering::SeqCst); + false + }; + let got = box_blur(src, &read, out, n, true, &boxes, &Interrupt::cancel_only(&count)).unwrap(); + assert_eq!(got.len(), want.len()); + for ((ta, a), (tb, b)) in got.iter().zip(&want) { + assert_eq!(ta, tb); + assert!(a.iter().zip(b).all(|(x, y)| x.to_bits() == y.to_bits()), "strip {ta:?} differs"); + } + let total = checks.load(Ordering::SeqCst); + for k in (0..total).step_by((total / 40).max(1)) { + let seen = AtomicUsize::new(0); + let cancel = || seen.fetch_add(1, Ordering::SeqCst) >= k; + assert!(box_blur(src, &read, out, n, true, &boxes, &Interrupt::cancel_only(&cancel)).is_none(), "cancelled at check {k}"); + } +} diff --git a/crates/algo/src/blur2.rs b/crates/algo/src/blur2.rs index e917405083..5096a9fb9a 100644 --- a/crates/algo/src/blur2.rs +++ b/crates/algo/src/blur2.rs @@ -181,7 +181,6 @@ fn conv_spans(src: &Image, out: Rect, ctx: &Ctx, spans: &Spans) -> Vec { } #[cfg(test)] -#[path = "shape_blur_tests.rs"] mod shape_tests; // ---------- smart blur ---------- @@ -189,6 +188,11 @@ mod shape_tests; /// Smart Blur: averages only neighbours within `threshold` levels of the /// centre pixel (so edges stay crisp); Edge Only / Overlay Edge draw the /// edges (where neighbours differ by more than the threshold) in white. +/// +/// Neighbours outside `src` read as zero, as [`Image::get`] does. Pixels whose whole +/// kernel lies inside `src` (all of them, with the usual halo) read neighbours at +/// precomputed offsets into the window instead of by coordinates, and for up to five +/// channels go through [`smart_rows`] (#7921). pub(crate) fn smart_blur(src: &Image, out: Rect, ctx: &Ctx, radius: f32, threshold: f32, quality: BlurQuality, mode: SmartBlurMode) -> Vec { let n = src.ch; let cc = ncol(ctx, n); @@ -201,31 +205,48 @@ pub(crate) fn smart_blur(src: &Image, out: Rect, ctx: &Ctx, radius: f32, thresho BlurQuality::High => 12, }; let stride = ((ri + target - 1) / target).max(1); - let offs: Vec<(i32, i32)> = (-ri..=ri) + let win = src.rect; + let ww = win.width() as isize; + // Each offset with its distance in samples within the window. + let offs: Vec<(i32, i32, isize)> = (-ri..=ri) .step_by(stride as usize) .flat_map(|dy| (-ri..=ri).step_by(stride as usize).map(move |dx| (dx, dy))) .filter(|(dx, dy)| ((dx * dx + dy * dy) as f32) <= r * r + 0.5) + .map(|(dx, dy)| (dx, dy, (dy as isize * ww + dx as isize) * n as isize)) .collect(); + let at = |x: i32, y: i32| win.contains(x, y).then(|| ((y - win.y0) as usize * ww as usize + (x - win.x0) as usize) * n); + let zeros = [0.0f32; MAXC]; + let data = &src.data[..]; + // The pixel at sample `i`, or zeros outside the window. + let sample = |i: Option| i.and_then(|i| data.get(i..i + n)).unwrap_or(&zeros[..n]); let white = native(ctx, [1.0, 1.0, 1.0, 1.0]); let black = native(ctx, [0.0, 0.0, 0.0, 1.0]); let mut res = src.crop(out); - let mut q = [0.0f32; MAXC]; + let inner = Rect::new(win.x0.saturating_add(ri), win.y0.saturating_add(ri), win.x1.saturating_sub(ri), win.y1.saturating_sub(ri)).intersect(&out); + let lin: Vec = offs.iter().map(|o| o.2).collect(); + let rows = match (mode, n, ctx.alpha) { + (SmartBlurMode::Normal, 1, false) => smart_rows::<1, false>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 1, true) => smart_rows::<1, true>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 2, false) => smart_rows::<2, false>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 2, true) => smart_rows::<2, true>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 3, false) => smart_rows::<3, false>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 3, true) => smart_rows::<3, true>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 4, false) => smart_rows::<4, false>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 4, true) => smart_rows::<4, true>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 5, false) => smart_rows::<5, false>(src, out, inner, &lin, t, &mut res), + (SmartBlurMode::Normal, 5, true) => smart_rows::<5, true>(src, out, inner, &lin, t, &mut res), + _ => Rect::EMPTY, + }; let mut acc = [0.0f32; MAXC]; for (i, px) in res.chunks_exact_mut(n).enumerate() { let (x, y) = xy(out, i); + if rows.contains(x, y) { + continue; + } let me: [f32; MAXC] = std::array::from_fn(|c| if c < n { px[c] } else { 0.0 }); - let differs = |q: &[f32]| (0..cc).any(|c| (q[c] - me[c]).abs() > t); + let differs = |q: &[f32]| q[..cc].iter().zip(&me).any(|(q, m)| (q - m).abs() > t); if mode != SmartBlurMode::Normal { - let mut edge = false; - for (dx, dy) in [(1, 0), (0, 1), (-1, 0), (0, -1)] { - for (c, v) in q.iter_mut().enumerate().take(n) { - *v = src.get(x + dx, y + dy, c); - } - if differs(&q[..n]) { - edge = true; - break; - } - } + let edge = [(1, 0), (0, 1), (-1, 0), (0, -1)].into_iter().any(|(dx, dy)| differs(sample(at(x + dx, y + dy)))); let a = if ctx.alpha { px[n - 1] } else { 1.0 }; if edge { px[..cc].copy_from_slice(&white[..cc]); @@ -237,18 +258,28 @@ pub(crate) fn smart_blur(src: &Image, out: Rect, ctx: &Ctx, radius: f32, thresho } continue; } + let interior = x - ri >= win.x0 && x + ri < win.x1 && y - ri >= win.y0 && y + ri < win.y1; + let centre = if interior { at(x, y) } else { None }; acc[..n].fill(0.0); let mut wsum = 0.0f32; - for &(dx, dy) in &offs { - for (c, v) in q.iter_mut().enumerate().take(n) { - *v = src.get(x + dx, y + dy, c); - } - if differs(&q[..n]) { + for &(dx, dy, d) in &offs { + let q = sample(match centre { + Some(c) => c.checked_add_signed(d), + None => at(x + dx, y + dy), + }); + if differs(q) { continue; } - let a = if ctx.alpha { q[n - 1] } else { 1.0 }; - for c in 0..n { - acc[c] += if ctx.alpha && c < n - 1 { q[c] * a } else { q[c] }; + if ctx.alpha { + let a = q[n - 1]; + for (s, v) in acc.iter_mut().zip(&q[..cc]) { + *s += v * a; + } + acc[n - 1] += a; + } else { + for (s, v) in acc.iter_mut().zip(q) { + *s += v; + } } wsum += 1.0; } @@ -262,6 +293,76 @@ pub(crate) fn smart_blur(src: &Image, out: Rect, ctx: &Ctx, radius: f32, thresho res } +/// Smart Blur's normal mode over `inner` (pixels whose whole kernel lies in `src`) for `N` +/// channels, the last alpha if `A`; `lin` are the neighbours as sample offsets in the window. +/// Each offset sweeps a whole row, so neighbours load contiguously and a rejected one adds +/// `-0.0` (an exact identity) instead of branching: every pixel sums its neighbours in the same +/// order as [`smart_blur`]'s per-pixel loop, giving the same bits. Returns the area written: the +/// rows finished, empty if the layout doesn't match. +fn smart_rows(src: &Image, out: Rect, inner: Rect, lin: &[isize], t: f32, res: &mut [f32]) -> Rect { + let win = src.rect; + if src.ch != N || inner.is_empty() || !win.contains_rect(&inner) || !out.contains_rect(&inner) { + return Rect::EMPTY; + } + let cc = if A { N - 1 } else { N }; + let (ww, ow, len) = (win.width() as usize, out.width() as usize, inner.width() as usize); + let pixels = len * N; + let mut acc = vec![[0.0f32; N]; len]; + let mut wsum = vec![0.0f32; len]; + // Rows before `y` are done; a row is written only once all its neighbours were read. + let done = |y: i32| Rect::new(inner.x0, inner.y0, inner.x1, y); + for y in inner.y0..inner.y1 { + let centre = ((y - win.y0) as usize * ww + (inner.x0 - win.x0) as usize) * N; + let Some(me) = src.data.get(centre..centre + pixels) else { + return done(y); + }; + let me = me.as_chunks::().0; + acc.fill([0.0; N]); + wsum.fill(0.0); + for &d in lin { + let Some(q) = centre.checked_add_signed(d).and_then(|i| src.data.get(i..i + pixels)) else { + return done(y); + }; + for (((s, w), q), m) in acc.iter_mut().zip(wsum.iter_mut()).zip(q.as_chunks::().0).zip(me) { + // As `differs`: a NaN difference is not over the threshold. + let mut far = false; + for c in 0..cc { + far |= (q[c] - m[c]).abs() > t; + } + let keep = !far; + if A { + let a = q[N - 1]; + for c in 0..N - 1 { + s[c] += if keep { q[c] * a } else { -0.0 }; + } + s[N - 1] += if keep { a } else { -0.0 }; + } else { + for c in 0..N { + s[c] += if keep { q[c] } else { -0.0 }; + } + } + *w += if keep { 1.0 } else { 0.0 }; + } + } + let o = ((y - out.y0) as usize * ow + (inner.x0 - out.x0) as usize) * N; + let Some(dst) = res.get_mut(o..o + pixels) else { + return done(y); + }; + for ((px, s), &w) in dst.as_chunks_mut::().0.iter_mut().zip(&acc).zip(&wsum) { + if w > 0.0 { + for c in 0..N { + px[c] = s[c] / w; + } + unpremul_px(px, A); + } + } + } + inner +} + +#[cfg(test)] +mod smart_tests; + // ---------- lens blur ---------- pub(crate) struct LensSpec<'a> { @@ -335,7 +436,7 @@ pub(crate) fn lens_blur(src: &Image, out: Rect, ctx: &Ctx, spec: &LensSpec) -> V } src.get(x, y, n - 1) } - DepthSource::LayerMask => spec.depth_map?.get(x, y, 0), + DepthSource::LayerMask | DepthSource::Channel => spec.depth_map?.get(x, y, 0), }; Some(if spec.invert { 1.0 - d } else { d }) }; diff --git a/crates/algo/src/shape_blur_tests.rs b/crates/algo/src/blur2/shape_tests.rs similarity index 100% rename from crates/algo/src/shape_blur_tests.rs rename to crates/algo/src/blur2/shape_tests.rs diff --git a/crates/algo/src/blur2/smart_tests.rs b/crates/algo/src/blur2/smart_tests.rs new file mode 100644 index 0000000000..505764e42e --- /dev/null +++ b/crates/algo/src/blur2/smart_tests.rs @@ -0,0 +1,173 @@ +//! The pre-#7921 Smart Blur (every neighbour read per channel through `Image::get`) stays as an +//! exact oracle: the offset fast path must give the same bits for every mode, depth and layout. +use super::{BlurQuality, Ctx, Image, MAXC, Rect, SmartBlurMode, native, ncol, smart_blur, unpremul_px, xy}; +use photocraft_color::ColorMode; + +#[allow(clippy::too_many_arguments)] +fn smart_blur_oracle(src: &Image, out: Rect, ctx: &Ctx, radius: f32, threshold: f32, quality: BlurQuality, mode: SmartBlurMode) -> Vec { + let n = src.ch; + let cc = ncol(ctx, n); + let r = radius.clamp(0.1, 100.0); + let t = threshold.clamp(0.1, 100.0) / 255.0; + let ri = r.ceil() as i32; + let target = match quality { + BlurQuality::Low => 4, + BlurQuality::Medium => 7, + BlurQuality::High => 12, + }; + let stride = ((ri + target - 1) / target).max(1); + let offs: Vec<(i32, i32)> = (-ri..=ri) + .step_by(stride as usize) + .flat_map(|dy| (-ri..=ri).step_by(stride as usize).map(move |dx| (dx, dy))) + .filter(|(dx, dy)| ((dx * dx + dy * dy) as f32) <= r * r + 0.5) + .collect(); + let white = native(ctx, [1.0, 1.0, 1.0, 1.0]); + let black = native(ctx, [0.0, 0.0, 0.0, 1.0]); + let mut res = src.crop(out); + let mut q = [0.0f32; MAXC]; + let mut acc = [0.0f32; MAXC]; + for (i, px) in res.chunks_exact_mut(n).enumerate() { + let (x, y) = xy(out, i); + let me: [f32; MAXC] = std::array::from_fn(|c| if c < n { px[c] } else { 0.0 }); + let differs = |q: &[f32]| (0..cc).any(|c| (q[c] - me[c]).abs() > t); + if mode != SmartBlurMode::Normal { + let mut edge = false; + for (dx, dy) in [(1, 0), (0, 1), (-1, 0), (0, -1)] { + for (c, v) in q.iter_mut().enumerate().take(n) { + *v = src.get(x + dx, y + dy, c); + } + if differs(&q[..n]) { + edge = true; + break; + } + } + let a = if ctx.alpha { px[n - 1] } else { 1.0 }; + if edge { + px[..cc].copy_from_slice(&white[..cc]); + } else if mode == SmartBlurMode::EdgeOnly { + px[..cc].copy_from_slice(&black[..cc]); + } + if ctx.alpha { + px[n - 1] = a; + } + continue; + } + acc[..n].fill(0.0); + let mut wsum = 0.0f32; + for &(dx, dy) in &offs { + for (c, v) in q.iter_mut().enumerate().take(n) { + *v = src.get(x + dx, y + dy, c); + } + if differs(&q[..n]) { + continue; + } + let a = if ctx.alpha { q[n - 1] } else { 1.0 }; + for c in 0..n { + acc[c] += if ctx.alpha && c < n - 1 { q[c] * a } else { q[c] }; + } + wsum += 1.0; + } + if wsum > 0.0 { + for c in 0..n { + px[c] = acc[c] / wsum; + } + unpremul_px(px, ctx.alpha); + } + } + res +} + +/// Flat blocks with small noise, so some neighbours fall within the threshold and some don't. +fn image(rect: Rect, n: usize, alpha: bool) -> Image { + let w = rect.width() as usize; + let data = (0..w * rect.height() as usize) + .flat_map(|i| { + let (x, y) = (i % w, i / w); + let block = ((x / 5 + y / 7) % 3) as f32 * 0.3; + (0..n).map(move |c| { + let noise = ((i * 31 + c * 13) % 17) as f32 / 160.0; + if alpha && c + 1 == n { [1.0, 0.0, 0.4, 0.75][(x + y) % 4] } else { block + noise } + }) + }) + .collect(); + Image { rect, ch: n, data } +} + +fn exact(a: &[f32], b: &[f32], what: &str) { + assert_eq!(a.len(), b.len(), "{what}"); + for (i, (a, b)) in a.iter().zip(b).enumerate() { + assert!(a.to_bits() == b.to_bits() || (a.is_nan() && b.is_nan()), "{what}: sample {i}: {a} != {b}"); + } +} + +#[test] +fn offset_fast_path_matches_the_per_channel_oracle() { + let mut layouts: Vec<(ColorMode, usize, bool)> = vec![ + (ColorMode::Rgb, 3, false), + (ColorMode::Rgb, 4, true), + (ColorMode::Grayscale, 1, false), + (ColorMode::Grayscale, 2, true), + (ColorMode::Cmyk, 4, false), + (ColorMode::Cmyk, 5, true), + (ColorMode::Lab, 4, true), + ]; + layouts.extend((1..=MAXC).flat_map(|n| [(ColorMode::Multichannel, n, false), (ColorMode::Multichannel, n, true)])); + let win = Rect::new(-7, -4, 33, 30); + for (mode, n, alpha) in layouts { + let src = image(win, n, alpha); + let ctx = Ctx { bounds: win, mode, alpha }; + for radius in [0.1f32, 1.0, 3.0, 12.0, 17.5] { + let ri = f32::ceil(radius) as i32; + // The usual tile (the window less the halo), the whole window, and one sticking out of it. + let inner = Rect::new(win.x0 + ri, win.y0 + ri, win.x1 - ri, win.y1 - ri); + for out in [inner, win, Rect::new(-12, -9, 10, 8), Rect::EMPTY] { + for quality in [BlurQuality::Low, BlurQuality::Medium, BlurQuality::High] { + for sm in [SmartBlurMode::Normal, SmartBlurMode::EdgeOnly, SmartBlurMode::OverlayEdge] { + for threshold in [8.0, 60.0] { + let what = format!("{mode:?} n={n} alpha={alpha} r={radius} {out:?} {quality:?} {sm:?} t={threshold}"); + exact( + &smart_blur(&src, out, &ctx, radius, threshold, quality, sm), + &smart_blur_oracle(&src, out, &ctx, radius, threshold, quality, sm), + &what, + ); + } + } + } + } + } + } +} + +#[test] +fn out_of_window_neighbours_read_as_zero() { + // A one-pixel window: every neighbour is outside and reads as transparent black, so a black + // centre averages with all of them and a white one keeps itself. + let ctx = Ctx { bounds: Rect::new(0, 0, 1, 1), mode: ColorMode::Rgb, alpha: true }; + for v in [0.0, 1.0] { + let src = Image { rect: Rect::new(0, 0, 1, 1), ch: 4, data: vec![v, v, v, 1.0] }; + let got = smart_blur(&src, src.rect, &ctx, 3.0, 25.0, BlurQuality::High, SmartBlurMode::Normal); + exact(&got, &smart_blur_oracle(&src, src.rect, &ctx, 3.0, 25.0, BlurQuality::High, SmartBlurMode::Normal), "1 px"); + } +} + +#[test] +fn nan_and_infinite_samples_match_the_oracle() { + // A NaN difference is never over the threshold, so it is averaged in; an infinite one is. + let win = Rect::new(0, 0, 24, 20); + for (n, alpha) in [(3, false), (4, true)] { + let mut src = image(win, n, alpha); + for (i, v) in [(5, f32::NAN), (97, f32::INFINITY), (230, f32::NEG_INFINITY), (311, -0.0)] { + let len = src.data.len(); + src.data[(i * n) % len] = v; + } + let ctx = Ctx { bounds: win, mode: ColorMode::Rgb, alpha }; + let out = Rect::new(4, 4, 20, 16); + for threshold in [8.0, 100.0] { + exact( + &smart_blur(&src, out, &ctx, 4.0, threshold, BlurQuality::High, SmartBlurMode::Normal), + &smart_blur_oracle(&src, out, &ctx, 4.0, threshold, BlurQuality::High, SmartBlurMode::Normal), + &format!("n={n} t={threshold}"), + ); + } + } +} diff --git a/crates/algo/src/camera_raw.rs b/crates/algo/src/camera_raw.rs index f855b08bfe..5bd0c8ab06 100644 --- a/crates/algo/src/camera_raw.rs +++ b/crates/algo/src/camera_raw.rs @@ -56,8 +56,11 @@ pub fn validate_curve(points: &[[f32; 2]]) -> Result<(), String> { use crate::photo_util::{hash01, linear_to_srgb, par_rows, srgb_to_linear}; mod local_tone; +mod per_pixel; mod whites_blacks; +pub use per_pixel::{CURVE_LUT, Grade, Grain, PerPixel, Vignette, VignetteStyle, WB_STEPS, WhitesBlacks, per_pixel, white_black_points}; + /// How the pixels encode light: the sRGB curve (gamma 0) or a pure power law (ProPhoto's 1.8, /// Adobe RGB's 2.2). The tone controls work on light, so the document's own curve decodes it. #[derive(Clone, Copy, Debug, PartialEq)] @@ -67,22 +70,16 @@ impl Encoding { pub(crate) fn of(gamma: f32) -> Self { Encoding(if gamma.is_finite() && gamma > 0.0 { gamma.clamp(0.5, 4.0) } else { 0.0 }) } + /// A value in this encoding as linear light. pub(crate) fn decode(self, v: f32) -> f32 { if self.0 > 0.0 { v.max(0.0).powf(self.0) } else { srgb_to_linear(v) } } + /// Linear light back into this encoding, the inverse of [`Encoding::decode`]. pub(crate) fn encode(self, v: f32) -> f32 { if self.0 > 0.0 { v.max(0.0).powf(1.0 / self.0) } else { linear_to_srgb(v) } } } -fn srgb_encode(v: f32) -> f32 { - linear_to_srgb(v) -} - -fn srgb_decode(v: f32) -> f32 { - srgb_to_linear(v) -} - fn luma_linear(c: [f32; 3]) -> f32 { luma(c) } @@ -172,6 +169,13 @@ pub struct CameraRaw { /// The document's transfer curve: 0 for sRGB's, else the gamma of a power-law space (1.8 /// for ProPhoto). Set from the document's profile; Whites and Blacks act on light. pub encoding_gamma: f32, + // Calibration (Camera Raw's Calibration panel) + /// Shadows › Tint: −100 (green) … 100 (magenta), strongest in the darkest tones. + pub shadow_tint: f32, + /// Red, Green and Blue Primary › Hue: −100 … 100, the primary turned towards its neighbours. + pub calibration_hue: [f32; 3], + /// Red, Green and Blue Primary › Saturation: −100 (grey) … 100 (purer). + pub calibration_sat: [f32; 3], } impl Default for CameraRaw { @@ -228,6 +232,9 @@ impl Default for CameraRaw { seed: 0, pixel_scale: 1.0, encoding_gamma: 0.0, + shadow_tint: 0.0, + calibration_hue: [0.0; 3], + calibration_sat: [0.0; 3], } } } @@ -235,6 +242,90 @@ impl Default for CameraRaw { /// Hue band centres of the Color Mixer (degrees). pub const HSL_BANDS: [f32; 8] = [0.0, 30.0, 60.0, 120.0, 180.0, 240.0, 270.0, 300.0]; +/// How far a primary turns at Hue ±100 (degrees), as the Color Mixer's hue sliders do. +const CALIBRATION_TURN: f32 = 30.0; + +/// How much of each primary (red, green, blue) a hue is made of: triangular weights around +/// 0°, 120° and 240° that sum to one, so a colour between two primaries follows both. +pub fn calibration_weights(hue: f32) -> [f32; 3] { + let h = hue.rem_euclid(360.0); + let mut w = [0.0f32; 3]; + for (i, centre) in [0.0f32, 120.0, 240.0].iter().enumerate() { + let d = (h - centre).abs().min(360.0 - (h - centre).abs()); + w[i] = (1.0 - d / 120.0).max(0.0); + } + let sum: f32 = w.iter().sum(); + if sum > 1e-6 { w.map(|v| v / sum) } else { [1.0, 0.0, 0.0] } +} + +/// The Calibration panel's primaries on one linear-light pixel: its hue turns by the weighted +/// Hue of the primaries it is made of (±100 is ±30°, positive towards the next primary) and its +/// saturation scales by their weighted Saturation (−100 greys it); the luminance is kept, so +/// neutrals are untouched. +fn calibrate(c: [f32; 3], hue: [f32; 3], sat: [f32; 3]) -> [f32; 3] { + let [h, s, v] = rgb_to_hsv(c.map(|x| x.max(0.0))); + if s <= 1e-5 || v <= 1e-6 { + return c; + } + let w = calibration_weights(h); + let (mut dh, mut ds) = (0.0f32, 0.0f32); + for i in 0..3 { + dh += w[i] * hue[i].clamp(-100.0, 100.0); + ds += w[i] * sat[i].clamp(-100.0, 100.0); + } + let nh = h + dh / 100.0 * CALIBRATION_TURN; + let ns = (s * (1.0 + ds / 100.0)).clamp(0.0, 1.0); + let out = hsv_to_rgb(nh, ns, v); + let (l0, l1) = (luma(c), luma(out)); + if l1 > 1e-6 { out.map(|x| x * l0 / l1) } else { out } +} + +/// Shadows › Tint on one linear-light pixel: `tint` −1 (green) … 1 (magenta), weighted by how +/// dark the pixel is; the luminance is kept. +fn shadow_tint(c: [f32; 3], tint: f32) -> [f32; 3] { + let l = luma(c).max(0.0); + // Linear 0.18 is middle grey: full strength below it, fading out towards white. + let weight = (1.0 - (l / 0.18).sqrt().min(1.0)).powi(2) * 0.5 + (1.0 - l.min(1.0)).powi(4) * 0.5; + let k = tint * 0.12 * weight * (l + 0.02); + let o = [c[0] + k, c[1] - k, c[2] + k].map(|v| v.max(0.0)); + let l2 = luma(o); + if l2 > 1e-6 { o.map(|v| v * l / l2) } else { o } +} + +/// Camera Raw's Auto for the Light panel: Exposure, Contrast, Highlights, Shadows, Whites and +/// Blacks from the image's luminance histogram (`px` in the document's encoding). A well +/// exposed image gets near-zero settings; a dark one more exposure and lifted shadows; one +/// with a crushed or blown end gets Blacks or Whites back from the edge. The other panels are +/// left alone, as Camera Raw's Auto leaves them. +pub fn auto_tone(px: &[[f32; 4]], p: &mut CameraRaw) { + if px.is_empty() { + return; + } + let mut lum: Vec = px.iter().map(|q| luma([q[0], q[1], q[2]]).clamp(0.0, 1.0)).collect(); + lum.sort_by(|a, b| a.total_cmp(b)); + let at = |f: f32| lum[((lum.len() - 1) as f32 * f).round() as usize]; + let (black, shadow, median, light, white) = (at(0.001), at(0.1), at(0.5), at(0.9), at(0.999)); + // Exposure: the median towards middle grey (sRGB 0.46), damped so flat scenes aren't + // forced to it. + let median_lin = srgb_to_linear(median).max(1e-4); + p.exposure = (0.6 * (0.18f32 / median_lin).log2()).clamp(-2.0, 2.0); + // Blacks and Whites bring the ends to the edges without clipping more than Camera Raw does. + p.blacks = ((0.04 - black) * 400.0).clamp(-40.0, 40.0); + p.whites = ((0.96 - white) * 300.0).clamp(-40.0, 40.0); + // Shadows up when the darker tenth is crushed, highlights down when the brighter tenth is + // near white. + p.shadows = ((0.25 - shadow) * 200.0).clamp(0.0, 50.0); + p.highlights = ((light - 0.85) * -300.0).clamp(-50.0, 0.0); + // A little contrast for a flat histogram. + let spread = (light - shadow).max(0.0); + p.contrast = ((0.6 - spread) * 60.0).clamp(0.0, 25.0); + // Shown as Camera Raw shows them: Exposure to a hundredth of a stop, the others whole. + p.exposure = (p.exposure * 100.0).round() / 100.0; + for v in [&mut p.blacks, &mut p.whites, &mut p.shadows, &mut p.highlights, &mut p.contrast] { + *v = v.round(); + } +} + fn luma(c: [f32; 3]) -> f32 { 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2] } @@ -540,6 +631,10 @@ impl CameraRaw { fn grade_neutral(&self) -> bool { self.grade_shadows.neutral() && self.grade_midtones.neutral() && self.grade_highlights.neutral() && self.grade_global.neutral() } + /// The Calibration panel at rest. + fn calibration_neutral(&self) -> bool { + self.shadow_tint == 0.0 && self.calibration_hue.iter().chain(&self.calibration_sat).all(|v| *v == 0.0) + } fn curves_neutral(&self) -> bool { [self.curve_highlights, self.curve_lights, self.curve_darks, self.curve_shadows].iter().all(|v| *v == 0.0) && [&self.point_curve, &self.point_curve_red, &self.point_curve_green, &self.point_curve_blue].iter().all(|c| is_linear(c)) @@ -547,6 +642,7 @@ impl CameraRaw { /// True when every control is neutral (the filter is an identity). pub fn is_identity(&self) -> bool { self.wb_neutral() + && self.calibration_neutral() && self.tone_neutral() && self.dehaze == 0.0 && self.vibrance == 0.0 @@ -586,14 +682,27 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo let ps = p.pixel_scale.clamp(0.01, 1.0); let long = w.max(h) as f32; // 1. White balance + exposure in linear light. - if !p.wb_neutral() { - let mut g = white_balance_gains(p.temperature, p.tint); - let norm = luma(g); - g = g.map(|v| v / norm * 2f32.powf(p.exposure)); + if let Some(g) = per_pixel::gains(p) { par_rows(px, w, 1, |_, row| { for q in row.iter_mut() { - for c in 0..3 { - q[c] = linear_to_srgb((srgb_to_linear(q[c]) * g[c]).max(0.0)); + *q = balance(*q, g); + } + }); + } + // 1b. Calibration: the primaries turned and purer or greyer, and the shadows tinted, in + // linear light (the camera's primaries are what Camera Raw's panel calibrates). + if !p.calibration_neutral() { + let primaries = p.calibration_hue.iter().chain(&p.calibration_sat).any(|v| *v != 0.0); + let tint = p.shadow_tint.clamp(-100.0, 100.0) / 100.0; + par_rows(px, w, 1, |_, row| { + for q in row.iter_mut() { + let c = [srgb_to_linear(q[0]), srgb_to_linear(q[1]), srgb_to_linear(q[2])]; + let mut o = if primaries { calibrate(c, p.calibration_hue, p.calibration_sat) } else { c }; + if tint != 0.0 { + o = shadow_tint(o, tint); + } + for k in 0..3 { + q[k] = linear_to_srgb(o[k].max(0.0)); } } }); @@ -612,7 +721,6 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo let clar = (p.clarity != 0.0).then(|| guided_fast(&lc, &lc, w, h, ((long * 0.02) as usize).max(3), 0.005)); let tex = (p.texture != 0.0).then(|| gauss(&lc, w, h, (long * 0.002).max(1.5 * ps))); let k_con = p.contrast / 100.0 * 0.6; - let tau = std::f32::consts::TAU; par_rows(px, w, 1, |y, row| { for (x, q) in row.iter_mut().enumerate() { let i = y * w + x; @@ -625,22 +733,14 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo if let Some(t) = &tex { lv += p.texture / 100.0 * 0.9 * (lc[i] - t[i]).clamp(-0.1, 0.1); } - if k_con != 0.0 { - let xx = lv.clamp(0.0, 1.0); - lv += -k_con * (tau * xx).sin() / tau; - } - let lv = lv.max(0.0) + (l0 - lc[i]); - let ratio = if l0 > 1e-5 { lv / l0 } else { 1.0 }; - for c in 0..3 { - q[c] = if l0 > 1e-5 { q[c] * ratio } else { q[c] + (lv - l0) }; - } + *q = tone(*q, l0, lc[i], lv, k_con); } }); } // 3b. Whites and Blacks: global curves adapted to the image's black and white points. if p.whites != 0.0 || p.blacks != 0.0 { let enc = Encoding::of(p.encoding_gamma); - let (bp, wp) = luminance_points(px, w, h, enc); + let (bp, wp) = luminance_points(px, w, h, enc, |q| q); let (xw, xb) = whites_blacks::amounts(p.whites.clamp(-100.0, 100.0), p.blacks.clamp(-100.0, 100.0), bp, wp); whites_blacks::apply(px, w, xw, xb, enc); } @@ -652,14 +752,7 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo let (master, cr, cg, cb) = (pts(&p.point_curve), pts(&p.point_curve_red), pts(&p.point_curve_green), pts(&p.point_curve_blue)); let hsl = !p.hsl_neutral(); let grade = !p.grade_neutral(); - let wheel_tint = |wh: &Wheel| -> [f32; 3] { - let c = hsv_to_rgb(wh.hue, 1.0, 1.0); - let l = luma(c); - [c[0] - l, c[1] - l, c[2] - l].map(|v| v * wh.sat / 100.0 * 0.3) - }; - let tints = [wheel_tint(&p.grade_shadows), wheel_tint(&p.grade_midtones), wheel_tint(&p.grade_highlights), wheel_tint(&p.grade_global)]; - let blend = 0.15 + p.grade_blending.clamp(0.0, 100.0) / 100.0 * 0.35; - let bal = p.grade_balance.clamp(-100.0, 100.0) / 100.0 * 0.25; + let Grade { tints, lums, blend, balance: bal } = Grade::new(p); par_rows(px, w, 1, |_, row| { for q in row.iter_mut() { let mut c = [q[0], q[1], q[2]]; @@ -712,7 +805,6 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo let hi = smoothstep(0.5 + bal - blend, 1.0, l); let mid = (1.0 - sh - hi).max(0.0); let masks = [sh, mid, hi, 1.0]; - let lums = [p.grade_shadows.lum, p.grade_midtones.lum, p.grade_highlights.lum, p.grade_global.lum]; for k in 0..4 { for ch in 0..3 { c[ch] += masks[k] * tints[k][ch]; @@ -738,11 +830,7 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo vignette(px, w, h, p); } if p.grain_amount > 0.0 { - let cell = 1.0 + p.grain_size / 25.0; - let rough = p.grain_roughness / 100.0; - let amt = p.grain_amount / 100.0 * 0.12; - let seed = p.seed as u64; - let pscale = 1.0 / ps; + let Grain { cell, roughness: rough, amount: amt, seed, scale: pscale } = Grain::new(p); par_rows(px, w, 1, |y, row| { for (x, q) in row.iter_mut().enumerate() { // Sample in full-resolution coordinates so previews match the result. @@ -765,24 +853,60 @@ pub fn develop(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, float: bo } } +/// The end of stage 3 on one pixel: Contrast (strength `k_con`) on the luminance `lv` that +/// Clarity and Texture left, then the pixel rescaled to it. `l0` is its luminance, `lc` that +/// clipped to 1. +fn tone(mut q: [f32; 4], l0: f32, lc: f32, mut lv: f32, k_con: f32) -> [f32; 4] { + if k_con != 0.0 { + let tau = std::f32::consts::TAU; + let xx = lv.clamp(0.0, 1.0); + lv += -k_con * (tau * xx).sin() / tau; + } + let lv = lv.max(0.0) + (l0 - lc); + let ratio = if l0 > 1e-5 { lv / l0 } else { 1.0 }; + for c in 0..3 { + q[c] = if l0 > 1e-5 { q[c] * ratio } else { q[c] + (lv - l0) }; + } + q +} + +/// Stage 3 on one pixel when Contrast is its only control. +fn contrast(q: [f32; 4], k_con: f32) -> [f32; 4] { + let l0 = luma([q[0], q[1], q[2]]).max(0.0); + let lc = l0.min(1.0); + tone(q, l0, lc, lc, k_con) +} + +/// Stage 1 on one pixel: white balance and exposure `gains` in linear light. +fn balance(mut q: [f32; 4], g: [f32; 3]) -> [f32; 4] { + for c in 0..3 { + q[c] = linear_to_srgb((srgb_to_linear(q[c]) * g[c]).max(0.0)); + } + q +} + /// The image's black and white points as sRGB-encoded grey levels: the 3rd percentile of the /// luminance and the 99.5th of the brightest channel, on a copy at most 256 pixels across (so a -/// preview proxy and the full image agree). -fn luminance_points(px: &[[f32; 4]], w: usize, h: usize, enc: Encoding) -> (f32, f32) { +/// preview proxy and the full image agree). `map` is applied to each pixel first. +fn luminance_points(px: &[[f32; 4]], w: usize, h: usize, enc: Encoding, map: impl Fn([f32; 4]) -> [f32; 4] + Sync) -> (f32, f32) { let f = w.max(h).div_ceil(256).max(1); let (sw, sh) = (w.div_ceil(f), h.div_ceil(f)); - let mut sum = vec![[0.0f64; 4]; sw * sh]; - for y in 0..h { - for x in 0..w { - let q = px[y * w + x]; - let s = &mut sum[(y / f) * sw + x / f]; - for c in 0..3 { - s[c] += f64::from(enc.decode(q[c]).clamp(0.0, 1.0)); + // Each band of `f` rows sums into its own row of cells, in the same order as one pass would. + let bands = crate::photo_util::par_map(sh, |sy| { + let mut sum = vec![[0.0f64; 4]; sw]; + for y in sy * f..((sy + 1) * f).min(h) { + for x in 0..w { + let q = map(px[y * w + x]); + let s = &mut sum[x / f]; + for c in 0..3 { + s[c] += f64::from(enc.decode(q[c]).clamp(0.0, 1.0)); + } + s[3] += 1.0; } - s[3] += 1.0; } - } - let cells: Vec<[f32; 3]> = sum.iter().filter(|s| s[3] > 0.0).map(|s| [0, 1, 2].map(|c| (s[c] / s[3]) as f32)).collect(); + sum + }); + let cells: Vec<[f32; 3]> = bands.iter().flatten().filter(|s| s[3] > 0.0).map(|s| [0, 1, 2].map(|c| (s[c] / s[3]) as f32)).collect(); if cells.is_empty() { return (0.0, 1.0); } @@ -824,23 +948,25 @@ fn dehaze(px: &mut [[f32; 4]], w: usize, h: usize, amount: f32) { *s = s.map(|v| v / c.max(1.0)); } let r = (sw.max(sh) / 60).max(2); - let min_filter = |v: &[f32]| -> Vec { + // The minimum (or, with `f32::max`, maximum) over each (2r + 1)² window. + let rank_filter = |v: &[f32], start: f32, pick: fn(f32, f32) -> f32| -> Vec { let mut a = vec![0.0f32; sw * sh]; for y in 0..sh { for x in 0..sw { let (x0, x1) = (x.saturating_sub(r), (x + r).min(sw - 1)); - a[y * sw + x] = (x0..=x1).map(|xx| v[y * sw + xx]).fold(f32::MAX, f32::min); + a[y * sw + x] = (x0..=x1).map(|xx| v[y * sw + xx]).fold(start, pick); } } let mut b = vec![0.0f32; sw * sh]; for y in 0..sh { for x in 0..sw { let (y0, y1) = (y.saturating_sub(r), (y + r).min(sh - 1)); - b[y * sw + x] = (y0..=y1).map(|yy| a[yy * sw + x]).fold(f32::MAX, f32::min); + b[y * sw + x] = (y0..=y1).map(|yy| a[yy * sw + x]).fold(start, pick); } } b }; + let min_filter = |v: &[f32]| rank_filter(v, f32::MAX, f32::min); let dark = min_filter(&small.iter().map(|c| c[0].min(c[1]).min(c[2])).collect::>()); // Atmospheric light: mean colour of the brightest 0.1 % of the dark channel. let mut idx: Vec = (0..sw * sh).collect(); @@ -854,7 +980,15 @@ fn dehaze(px: &mut [[f32; 4]], w: usize, h: usize, amount: f32) { } let air = air.map(|v| v.max(0.2)); let norm_dark = min_filter(&small.iter().map(|c| (c[0] / air[0]).min(c[1] / air[1]).min(c[2] / air[2])).collect::>()); - let t_raw: Vec = norm_dark.iter().map(|d| 1.0 - 0.95 * d).collect(); + // Opened (a maximum filter of the same radius after the minimum): the minimum grows every + // dark object by `r`, so sky beside a dark building took its dark channel, counted as + // haze-free and kept a bright halo (#9273). Opening shrinks dark regions back to their + // outline and is still a lower bound of the dark channel over the window. It raises the dark + // channel on average, so the 0.95 weight is scaled to keep the slider's overall strength. + let opened = rank_filter(&norm_dark, f32::MIN, f32::max); + let mean = |v: &[f32]| v.iter().sum::() / v.len().max(1) as f32; + let weight = 0.95 * mean(&norm_dark) / mean(&opened).max(1e-6); + let t_raw: Vec = opened.iter().map(|d| 1.0 - weight * d).collect(); let guide: Vec = small.iter().map(|c| luma(*c)).collect(); let t = guided(&guide, &t_raw, sw, sh, r * 2, 1e-3); let ks = k as f32; @@ -929,24 +1063,7 @@ fn detail(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw, ps: f32) { } fn vignette(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw) { - let amt = p.vignette_amount / 100.0; - let mid = p.vignette_midpoint.clamp(0.0, 100.0) / 100.0; - let feather = p.vignette_feather.clamp(0.0, 100.0) / 100.0; - let round = p.vignette_roundness.clamp(-100.0, 100.0) / 100.0; - let hl = p.vignette_highlights.clamp(0.0, 100.0) / 100.0; - let style = p.vignette_style.as_str(); - let (cx, cy) = (w as f32 / 2.0, h as f32 / 2.0); - // Roundness: 0 follows the frame's aspect, +1 a circle, −1 a rounded rectangle. - let (ax, ay) = if round >= 0.0 { - let r = cx.min(cy) + (cx.max(cy) - cx.min(cy)) * (1.0 - round); - (if cx >= cy { r } else { cx }, if cy > cx { r } else { cy }) - } else { - (cx, cy) - }; - let pw = 2.0 + (-round).max(0.0) * 6.0; - let inner = 1.0 - mid * 0.8; - let edge0 = inner - feather * 0.5 * inner; - let edge1 = inner + feather * 0.5 * (1.4 - inner) + 0.02; + let Vignette { amount: amt, highlights: hl, style, centre: [cx, cy], axes: [ax, ay], power: pw, edges: [edge0, edge1] } = Vignette::new(w, h, p); par_rows(px, w, 1, |y, row| { for (x, q) in row.iter_mut().enumerate() { let (dx, dy) = (((x as f32 + 0.5 - cx) / ax).abs(), ((y as f32 + 0.5 - cy) / ay).abs()); @@ -957,7 +1074,7 @@ fn vignette(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw) { } let l = luma([q[0], q[1], q[2]]).clamp(0.0, 1.0); match style { - "paintOverlay" => { + VignetteStyle::PaintOverlay => { let target = if amt < 0.0 { 0.0 } else { 1.0 }; let k = m * amt.abs(); for c in 0..3 { @@ -971,7 +1088,7 @@ fn vignette(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw) { let protect = smoothstep(0.6, 1.0, l) * hl; k += (1.0 - k) * protect; } - if style == "colorPriority" { + if style == VignetteStyle::ColorPriority { let nl = l * k; for c in 0..3 { q[c] += nl - l; @@ -991,6 +1108,37 @@ fn vignette(px: &mut [[f32; 4]], w: usize, h: usize, p: &CameraRaw) { mod tests { use super::*; + /// #9273: Dehaze clears the haze evenly over a sky, right up to a dark building, instead of + /// leaving a bright halo of uncorrected haze around it. Synthetic scene: a clear sky and a + /// dark tower, hazed with `I = J·t + A·(1 − t)`. + #[test] + fn dehaze_leaves_no_halo_beside_dark_objects() { + let (w, h) = (600usize, 400usize); + let tower = |x: usize, y: usize| (250..350).contains(&x) && y >= 120; + let (t, air) = (0.55f32, [0.9f32, 0.9, 0.9]); + let mut px: Vec<[f32; 4]> = (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + let j = if tower(x, y) { [0.06, 0.06, 0.07] } else { [0.35 + 0.1 * y as f32 / h as f32, 0.5, 0.8] }; + let c: [f32; 3] = std::array::from_fn(|k| j[k] * t + air[k] * (1.0 - t)); + [c[0], c[1], c[2], 1.0] + }) + .collect(); + dehaze(&mut px, w, h, 1.0); + let sky = |xs: std::ops::Range| { + let v: Vec = + (150..390).flat_map(|y| xs.clone().map(move |x| (x, y))).map(|(x, y)| luma([px[y * w + x][0], px[y * w + x][1], px[y * w + x][2]])).collect(); + v.iter().sum::() / v.len() as f32 + }; + // Sky 1–8 px from the tower against open sky: the haze left beside it, in 8-bit levels. + let halo = (sky(242..250) - sky(20..80)) * 255.0; + // 6.4 levels before the dark channel was opened, under 1 after. + assert!(halo < 3.0, "a {halo:.1}-level halo beside the tower"); + // The open sky is still cleared: darker than the hazy input. + let hazy = luma(std::array::from_fn(|k| [0.4, 0.5, 0.8][k] * t + air[k] * (1.0 - t))); + assert!(sky(20..80) < hazy - 0.02, "{} vs hazy {hazy}", sky(20..80)); + } + #[test] fn new_curves_are_validated_but_saved_curves_still_load_and_render() { for field in ["pointCurve", "pointCurveRed", "pointCurveGreen", "pointCurveBlue"] { @@ -1150,6 +1298,105 @@ mod tests { assert!(v[0][0] < 0.35 && (v[(h / 2) * w + w / 2][0] - 0.5).abs() < 0.01, "{:?}", v[0]); } + #[test] + fn calibration_keeps_the_develop_off_the_per_pixel_gpu_stages() { + // The per-pixel stages (the Camera Raw dialog's GPU preview) don't run Calibration, so + // settings with it must fall back to the CPU develop instead of previewing without it. + for p in [ + CameraRaw { shadow_tint: 40.0, ..Default::default() }, + CameraRaw { calibration_hue: [0.0, 0.0, 30.0], whites: 20.0, ..Default::default() }, + CameraRaw { calibration_sat: [-50.0, 0.0, 0.0], exposure: 0.5, ..Default::default() }, + ] { + assert!(!p.spatial()); + assert!(per_pixel(&p, 2, 2, false, Some((0.0, 1.0))).is_none(), "{p:?}"); + } + assert!(per_pixel(&CameraRaw { exposure: 0.5, ..Default::default() }, 2, 2, false, None).is_some()); + } + + #[test] + fn calibration_turns_primaries_keeps_neutrals_and_tints_shadows() { + let w = calibration_weights(0.0); + assert!((w[0] - 1.0).abs() < 1e-6, "pure red is all red primary: {w:?}"); + let w = calibration_weights(60.0); + assert!((w[0] - 0.5).abs() < 1e-6 && (w[1] - 0.5).abs() < 1e-6, "yellow is half red, half green: {w:?}"); + let w = calibration_weights(300.0); + assert!((w[2] - 0.5).abs() < 1e-6 && (w[0] - 0.5).abs() < 1e-6, "magenta wraps round: {w:?}"); + let (none, zero) = ([0.0f32; 3], [0.0f32; 3]); + let grey = calibrate([0.3, 0.3, 0.3], [100.0, -100.0, 50.0], [100.0, -100.0, 100.0]); + assert!(grey.iter().all(|v| (v - 0.3).abs() < 1e-5), "greys are untouched: {grey:?}"); + // Red turned towards green: it gains green, keeps its luminance. + let red = calibrate([0.5, 0.0, 0.0], [100.0, 0.0, 0.0], zero); + assert!(red[1] > 0.05 && (red[1] / red[0] - 0.5).abs() < 0.02 && red[2] < 1e-6, "red turns 30° towards green (orange): {red:?}"); + assert!((luma(red) - luma([0.5, 0.0, 0.0])).abs() < 1e-4, "luminance kept: {red:?}"); + // Blue at saturation −100 is grey; green at −50 is paler. + let blue = calibrate([0.0, 0.0, 0.8], none, [0.0, 0.0, -100.0]); + assert!((blue[0] - blue[1]).abs() < 1e-5 && (blue[1] - blue[2]).abs() < 1e-5, "blue at −100 saturation is grey: {blue:?}"); + let green = calibrate([0.2, 0.8, 0.2], none, [0.0, -50.0, 0.0]); + assert!(green[0] > 0.2 && green[1] < 0.8, "green at −50 is paler: {green:?}"); + // A primary's sliders leave the other primaries alone. + let unchanged = calibrate([0.0, 0.0, 0.8], [100.0, 100.0, 0.0], [100.0, 100.0, 0.0]); + assert!(unchanged.iter().zip([0.0, 0.0, 0.8]).all(|(a, b)| (a - b).abs() < 1e-5), "{unchanged:?}"); + // Shadows tint: magenta lifts red and blue over green in a dark pixel, keeps luminance, + // and leaves white alone. + let dark = shadow_tint([0.02, 0.02, 0.02], 1.0); + assert!(dark[0] > dark[1] && dark[2] > dark[1], "{dark:?}"); + assert!((luma(dark) - 0.02).abs() < 1e-4, "luminance kept: {dark:?}"); + let green = shadow_tint([0.02, 0.02, 0.02], -1.0); + assert!(green[1] > green[0], "{green:?}"); + let white = shadow_tint([1.0, 1.0, 1.0], 1.0); + assert!(white.iter().all(|v| (v - 1.0).abs() < 1e-3), "white is not tinted: {white:?}"); + // Through develop: neutral settings are an identity, a tint changes dark pixels only. + let mut p = CameraRaw::default(); + assert!(p.is_identity()); + p.shadow_tint = 100.0; + assert!(!p.is_identity()); + let mut px = vec![[0.1, 0.1, 0.1, 1.0], [0.95, 0.95, 0.95, 1.0]]; + develop(&mut px, 2, 1, &p, false); + assert!(px[0][0] > px[0][1], "dark grey turns magenta: {:?}", px[0]); + assert!((px[1][0] - px[1][1]).abs() < 0.01, "near-white stays neutral: {:?}", px[1]); + p = CameraRaw::default(); + p.calibration_hue[2] = 100.0; + let mut px = vec![[0.0, 0.0, 0.8, 1.0]]; + develop(&mut px, 1, 1, &p, false); + assert!(px[0][0] > 0.05, "blue turned towards red: {:?}", px[0]); + } + + #[test] + fn auto_tone_brightens_dark_and_darkens_bright_images() { + let mut p = CameraRaw::default(); + auto_tone(&[], &mut p); + assert_eq!(p, CameraRaw::default(), "no pixels: nothing changes"); + let dark: Vec<[f32; 4]> = (0..4096) + .map(|i| { + let v = 0.05 + (i % 64) as f32 / 64.0 * 0.15; + [v, v, v, 1.0] + }) + .collect(); + auto_tone(&dark, &mut p); + assert!(p.exposure > 0.5, "a dark image gets exposure: {}", p.exposure); + assert!(p.shadows > 0.0, "and lifted shadows: {}", p.shadows); + let mut p = CameraRaw::default(); + let bright: Vec<[f32; 4]> = (0..4096) + .map(|i| { + let v = 0.8 + (i % 64) as f32 / 64.0 * 0.19; + [v, v, v, 1.0] + }) + .collect(); + auto_tone(&bright, &mut p); + assert!(p.exposure < -0.5, "a bright image loses exposure: {}", p.exposure); + assert!(p.highlights < 0.0, "and its highlights come down: {}", p.highlights); + let mut p = CameraRaw::default(); + let good: Vec<[f32; 4]> = (0..4096) + .map(|i| { + let v = 0.04 + (i % 64) as f32 / 63.0 * 0.92; + [v, v, v, 1.0] + }) + .collect(); + auto_tone(&good, &mut p); + assert!(p.exposure.abs() < 0.3 && p.blacks.abs() < 10.0 && p.whites.abs() < 10.0, "a full-range image is left nearly alone: {p:?}"); + assert!(p.saturation == 0.0 && p.hsl_neutral(), "other panels untouched"); + } + #[test] fn float_documents_keep_overrange() { let mut a = vec![[2.0f32, 1.5, 0.5, 1.0]; 16]; diff --git a/crates/algo/src/camera_raw/local_tone.rs b/crates/algo/src/camera_raw/local_tone.rs index e1305c5d49..1dbf58421f 100644 --- a/crates/algo/src/camera_raw/local_tone.rs +++ b/crates/algo/src/camera_raw/local_tone.rs @@ -173,6 +173,39 @@ fn up(p: &Plane, w: usize, h: usize) -> Plane { Plane { w, h, v: out } } +/// The source rows of [`up`]`(p, w, h)` at output row `y`, one per tap (`None` where the +/// zero-stuffed row is all zeros). +fn up_rows(p: &Plane, h: usize, y: usize) -> [Option; 5] { + let lh = p.h.min(h.div_ceil(2)); + std::array::from_fn(|k| { + let m = mirror(y as isize + k as isize - 2, h); + (m.is_multiple_of(2) && m / 2 < lh).then_some(m / 2) + }) +} + +/// One sample of [`up`]`(p, w, h)`: column `x` of the output row whose [`up_rows`] are `rows`. +/// It adds the same terms in the same order as `up`, so it gives the same bits. +fn up_at(p: &Plane, w: usize, rows: &[Option; 5], x: usize) -> f32 { + let lw = p.w.min(w.div_ceil(2)); + let col = |j: usize| -> f32 { (0..5).filter_map(|k| rows[k].map(|r| TAPS[k] * (p.v[r * p.w + j] * 4.0))).sum() }; + if x >= 2 && x + 2 < w { + // Away from the edges no tap mirrors. + (0..5) + .filter_map(|k| { + let i = x + k - 2; + (i.is_multiple_of(2) && i / 2 < lw).then(|| TAPS[k] * col(i / 2)) + }) + .sum() + } else { + (0..5) + .filter_map(|k| { + let m = mirror(x as isize + k as isize - 2, w); + (m.is_multiple_of(2) && m / 2 < lw).then(|| TAPS[k] * col(m / 2)) + }) + .sum() + } +} + /// Levels down to about 2 pixels on the short side. fn depth(w: usize, h: usize) -> usize { let mut s = w.min(h); @@ -217,14 +250,14 @@ fn llf(u: &Plane, remap: impl Fn(f32, f32) -> f32 + Sync) -> Plane { }); let gr = gaussian(m, n); for l in 0..n - 1 { - let coarse = up(&gr[l + 1], gr[l].w, gr[l].h); - let (gl, fine, lw) = (&g[l].v, &gr[l].v, g[l].w); + let (gl, fine, lw, lh, coarse) = (&g[l].v, &gr[l].v, g[l].w, g[l].h, &gr[l + 1]); rows(&mut lap[l], lw, |y, row| { + let taps = up_rows(coarse, lh, y); for (x, o) in row.iter_mut().enumerate() { let i = y * lw + x; let wgt = (1.0 - (gl[i].clamp(lo, hi) - r).abs() / step).max(0.0); if wgt > 0.0 { - *o += wgt * (fine[i] - coarse.v[i]); + *o += wgt * (fine[i] - up_at(coarse, lw, &taps, x)); } } }); @@ -244,8 +277,10 @@ fn llf(u: &Plane, remap: impl Fn(f32, f32) -> f32 + Sync) -> Plane { /// The `q` quantile (0 … 1) of `v`. fn quantile(v: &[f32], q: f32) -> f32 { let mut s = v.to_vec(); - s.sort_by(f32::total_cmp); - s[((s.len() - 1) as f32 * q).round() as usize] + // Selecting the one element is linear, a full sort is not (5 sorts of the work copy were a + // fifteenth of a render). `total_cmp` ties are bit-identical, so it is the sorted element. + let i = ((s.len() - 1) as f32 * q).round() as usize; + *s.select_nth_unstable_by(i, f32::total_cmp).1 } /// 1st and 99th percentiles of the log luminance on a copy at most 256 pixels across. @@ -444,11 +479,33 @@ mod tests { assert_eq!(blur_step(&p, step).v, blur_step_reference(&p, step).v, "blur {w}×{h} step {step}"); } for (uw, uh) in [(w * 2, h * 2), (w * 2 - 1, h * 2 - 1), (w * 2, h * 2 - 1), (w.max(2) * 2 - 3, h * 2)] { - assert_eq!(up(&p, uw, uh).v, up_reference(&p, uw, uh).v, "up {w}×{h} → {uw}×{uh}"); + let full = up(&p, uw, uh); + assert_eq!(full.v, up_reference(&p, uw, uh).v, "up {w}×{h} → {uw}×{uh}"); + // The filter samples `up` one pixel at a time; every sample has the same bits. + for y in 0..uh { + let taps = up_rows(&p, uh, y); + for x in 0..uw { + assert_eq!(up_at(&p, uw, &taps, x).to_bits(), full.v[y * uw + x].to_bits(), "up_at {w}×{h} → {uw}×{uh} at {x},{y}"); + } + } } } } + #[test] + fn quantile_is_the_sorted_element() { + let p = noise_plane(301, 211, 9); + let mut v = p.v.clone(); + // Ties and signed zeros, which `total_cmp` orders by their bits. + v.extend([0.0, -0.0, 0.0, -0.0, 3.5, 3.5, f32::MIN_POSITIVE]); + let mut sorted = v.clone(); + sorted.sort_by(f32::total_cmp); + for q in [0.0, 0.001, 0.01, 0.25, 0.5, 0.99, 1.0] { + let want = sorted[((sorted.len() - 1) as f32 * q).round() as usize]; + assert_eq!(quantile(&v, q).to_bits(), want.to_bits(), "q {q}"); + } + } + fn grey(w: usize, h: usize, f: impl Fn(usize, usize) -> f32) -> Vec<[f32; 4]> { (0..w * h).map(|i| f(i % w, i / w)).map(|v| [v, v, v, 1.0]).collect() } diff --git a/crates/algo/src/camera_raw/per_pixel.rs b/crates/algo/src/camera_raw/per_pixel.rs new file mode 100644 index 0000000000..6400db8a25 --- /dev/null +++ b/crates/algo/src/camera_raw/per_pixel.rs @@ -0,0 +1,240 @@ +//! Develop's per-pixel stages resolved for one image: every constant and table they use, so +//! another implementation of them (the GPU preview, `photocraft_gpu::CameraRawDevelop`) runs the +//! same maths. [`develop`](super::develop) builds its own constants with the same functions. + +use super::{CameraRaw, Encoding, Wheel, curve_lut, hsv_to_rgb, is_linear, luma, parametric_lut, white_balance_gains, whites_blacks}; + +/// Entries of the tone curve tables (parametric and point curves). +pub const CURVE_LUT: usize = 1024; +/// Samples of a Whites or Blacks curve row: grey levels 0, 1/32, …, 1. +pub const WB_STEPS: usize = whites_blacks::STEPS; + +/// White balance and exposure: channel gains in linear light, when the stage is on. +pub(crate) fn gains(p: &CameraRaw) -> Option<[f32; 3]> { + if p.wb_neutral() { + return None; + } + let g = white_balance_gains(p.temperature, p.tint); + let norm = luma(g); + Some(g.map(|v| v / norm * 2f32.powf(p.exposure))) +} + +/// Contrast's strength, when Contrast is set. +fn contrast(p: &CameraRaw) -> Option { + (p.contrast != 0.0).then_some(p.contrast / 100.0 * 0.6) +} + +/// Whites and Blacks as two curve rows for the image's amounts. +#[derive(Clone, Debug, PartialEq)] +pub struct WhitesBlacks { + /// The Whites curve over grey levels 0, 1/32, …, 1, when Whites is set. + pub whites: Option<[f32; WB_STEPS]>, + /// The Blacks curve, applied after Whites. + pub blacks: Option<[f32; WB_STEPS]>, + /// The clipping directions (Whites up, Blacks down) curve each channel; the others scale the + /// pixel by the change of its luminance. + pub per_channel: bool, +} + +/// Color Grading's wheels resolved. +#[derive(Clone, Debug, PartialEq)] +pub struct Grade { + /// Colour offsets of the shadows, midtones, highlights and global wheels. + pub tints: [[f32; 3]; 4], + /// Luminance of the same wheels (slider units). + pub lums: [f32; 4], + pub blend: f32, + pub balance: f32, +} + +impl Grade { + pub(crate) fn new(p: &CameraRaw) -> Self { + let tint = |wh: &Wheel| -> [f32; 3] { + let c = hsv_to_rgb(wh.hue, 1.0, 1.0); + let l = luma(c); + [c[0] - l, c[1] - l, c[2] - l].map(|v| v * wh.sat / 100.0 * 0.3) + }; + Grade { + tints: [tint(&p.grade_shadows), tint(&p.grade_midtones), tint(&p.grade_highlights), tint(&p.grade_global)], + lums: [p.grade_shadows.lum, p.grade_midtones.lum, p.grade_highlights.lum, p.grade_global.lum], + blend: 0.15 + p.grade_blending.clamp(0.0, 100.0) / 100.0 * 0.35, + balance: p.grade_balance.clamp(-100.0, 100.0) / 100.0 * 0.25, + } + } +} + +/// The vignette's `vignette_style`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum VignetteStyle { + HighlightPriority, + ColorPriority, + PaintOverlay, +} + +/// The post-crop vignette's shape on a `w × h` frame. +#[derive(Clone, Debug, PartialEq)] +pub struct Vignette { + /// −1 … 1. + pub amount: f32, + /// 0 … 1. + pub highlights: f32, + pub style: VignetteStyle, + /// Frame centre (pixels). + pub centre: [f32; 2], + /// Half axes of the shape (pixels). + pub axes: [f32; 2], + /// Exponent of the superellipse. + pub power: f32, + /// The mask's smoothstep edges in normalised radius. + pub edges: [f32; 2], +} + +impl Vignette { + pub(crate) fn new(w: usize, h: usize, p: &CameraRaw) -> Self { + let mid = p.vignette_midpoint.clamp(0.0, 100.0) / 100.0; + let feather = p.vignette_feather.clamp(0.0, 100.0) / 100.0; + let round = p.vignette_roundness.clamp(-100.0, 100.0) / 100.0; + let (cx, cy) = (w as f32 / 2.0, h as f32 / 2.0); + // Roundness: 0 follows the frame's aspect, +1 a circle, −1 a rounded rectangle. + let (ax, ay) = if round >= 0.0 { + let r = cx.min(cy) + (cx.max(cy) - cx.min(cy)) * (1.0 - round); + (if cx >= cy { r } else { cx }, if cy > cx { r } else { cy }) + } else { + (cx, cy) + }; + let inner = 1.0 - mid * 0.8; + Vignette { + amount: p.vignette_amount / 100.0, + highlights: p.vignette_highlights.clamp(0.0, 100.0) / 100.0, + style: match p.vignette_style.as_str() { + "paintOverlay" => VignetteStyle::PaintOverlay, + "colorPriority" => VignetteStyle::ColorPriority, + _ => VignetteStyle::HighlightPriority, + }, + centre: [cx, cy], + axes: [ax, ay], + power: 2.0 + (-round).max(0.0) * 6.0, + edges: [inner - feather * 0.5 * inner, inner + feather * 0.5 * (1.4 - inner) + 0.02], + } + } +} + +/// Grain: value noise sampled in full-resolution coordinates. +#[derive(Clone, Debug, PartialEq)] +pub struct Grain { + /// Noise cell size (full-resolution pixels). + pub cell: f32, + /// Weight of the finer octave. + pub roughness: f32, + pub amount: f32, + pub seed: u64, + /// Full-resolution pixels per pixel (`1 / pixel_scale`). + pub scale: f32, +} + +impl Grain { + pub(crate) fn new(p: &CameraRaw) -> Self { + Grain { + cell: 1.0 + p.grain_size / 25.0, + roughness: p.grain_roughness / 100.0, + amount: p.grain_amount / 100.0 * 0.12, + seed: u64::from(p.seed), + scale: 1.0 / p.pixel_scale.clamp(0.01, 1.0), + } + } +} + +/// Develop's per-pixel stages for one image and settings, in order. `None` stages are off. +#[derive(Clone, Debug, PartialEq)] +pub struct PerPixel { + /// 1. White balance and exposure gains in linear light (sRGB-decoded values). + pub gains: Option<[f32; 3]>, + /// 3. Contrast's strength (Contrast without Texture or Clarity is per pixel). + pub contrast: Option, + /// 3b. Whites and Blacks, in the document's encoding. + pub whites_blacks: Option, + /// The document's transfer curve: 0 for sRGB's, else a power-law gamma. + pub encoding: f32, + /// 4. Vibrance and Saturation (slider / 100). + pub presence: Option<[f32; 2]>, + /// 5. The parametric tone curve, [`CURVE_LUT`] entries over 0 … 1. + pub parametric: Option>, + /// 5. Point curves: master, red, green, blue. + pub point: [Option>; 4], + /// 6. Color Mixer hue, saturation and luminance per band. + pub hsl: Option<[[f32; 8]; 3]>, + /// 7. Color Grading. + pub grade: Option, + /// 9. Vignette. + pub vignette: Option, + /// 9. Grain. + pub grain: Option, + /// Clip RGB to 0 … 1 at the end (all but 32-bit documents). + pub clamp: bool, +} + +impl CameraRaw { + /// Whether developing needs a stage that reads neighbouring pixels: Dehaze, Shadows and + /// Highlights, Texture, Clarity, sharpening or noise reduction. + pub fn spatial(&self) -> bool { + self.dehaze != 0.0 + || [self.highlights, self.shadows, self.texture, self.clarity].iter().any(|v| *v != 0.0) + || self.sharpen_amount > 0.0 + || self.noise_luminance > 0.0 + || self.noise_color > 0.0 + } +} + +/// The image's black and white points that Whites and Blacks adapt to, for `px` (`w × h`, +/// display-encoded) developed per pixel with `p`: what [`develop`](super::develop) measures +/// after white balance and Contrast. `None` when Whites and Blacks are 0 or `p` needs a +/// spatial stage. +pub fn white_black_points(px: &[[f32; 4]], w: usize, h: usize, p: &CameraRaw) -> Option<(f32, f32)> { + if (p.whites == 0.0 && p.blacks == 0.0) || p.spatial() || px.len() != w.saturating_mul(h) { + return None; + } + let enc = Encoding::of(p.encoding_gamma); + let (g, k_con) = (gains(p), contrast(p)); + Some(super::luminance_points(px, w, h, enc, |q| { + let q = g.map_or(q, |g| super::balance(q, g)); + k_con.map_or(q, |k| super::contrast(q, k)) + })) +} + +/// Develop's stages for settings without spatial stages, on a `w × h` image; `points` are the +/// image's [`white_black_points`]. `None` when `p` needs a spatial stage or Calibration (these +/// stages don't run it), or Whites or Blacks is set without `points`. The points themselves +/// leave Calibration out, so a caller may cache them across Calibration changes. +pub fn per_pixel(p: &CameraRaw, w: usize, h: usize, float: bool, points: Option<(f32, f32)>) -> Option { + if p.spatial() || !p.calibration_neutral() { + return None; + } + let identity = p.is_identity(); + let whites_blacks = if p.whites != 0.0 || p.blacks != 0.0 { + let (bp, wp) = points?; + let (xw, xb) = whites_blacks::amounts(p.whites.clamp(-100.0, 100.0), p.blacks.clamp(-100.0, 100.0), bp, wp); + (xw != 0.0 || xb != 0.0).then(|| WhitesBlacks { + whites: (xw != 0.0).then(|| whites_blacks::whites_row(xw)), + blacks: (xb != 0.0).then(|| whites_blacks::blacks_row(xb)), + per_channel: xw > 0.0 || xb < 0.0, + }) + } else { + None + }; + let parametric_on = !(p.curve_highlights == 0.0 && p.curve_lights == 0.0 && p.curve_darks == 0.0 && p.curve_shadows == 0.0); + let pts = |c: &Vec<[f32; 2]>| (!is_linear(c)).then(|| curve_lut(c, CURVE_LUT)); + Some(PerPixel { + gains: gains(p), + contrast: contrast(p), + whites_blacks, + encoding: Encoding::of(p.encoding_gamma).0, + presence: (p.vibrance != 0.0 || p.saturation != 0.0).then_some([p.vibrance / 100.0, p.saturation / 100.0]), + parametric: parametric_on.then(|| parametric_lut(p, CURVE_LUT)), + point: [pts(&p.point_curve), pts(&p.point_curve_red), pts(&p.point_curve_green), pts(&p.point_curve_blue)], + hsl: (!p.hsl_neutral()).then_some([p.hsl_hue, p.hsl_sat, p.hsl_lum]), + grade: (!p.grade_neutral()).then(|| Grade::new(p)), + vignette: (p.vignette_amount != 0.0).then(|| Vignette::new(w, h, p)), + grain: (p.grain_amount > 0.0).then(|| Grain::new(p)), + clamp: !float && !identity, + }) +} diff --git a/crates/algo/src/camera_raw/whites_blacks.rs b/crates/algo/src/camera_raw/whites_blacks.rs index 382ade50e9..cfce5378b4 100644 --- a/crates/algo/src/camera_raw/whites_blacks.rs +++ b/crates/algo/src/camera_raw/whites_blacks.rs @@ -24,7 +24,7 @@ use super::Encoding; use crate::photo_util::par_rows; /// Grey levels the curves are sampled at: 0, 1/32, …, 1 (sRGB-encoded). -const STEPS: usize = 33; +pub(crate) const STEPS: usize = 33; /// The first row's internal amount; rows are 10 apart. const WHITES_FROM: f32 = -100.0; const BLACKS_FROM: f32 = -100.0; @@ -315,6 +315,25 @@ fn curve(rows: &[[f32; STEPS]], from: f32, x: f32, v: f32) -> f32 { at(&rows[i]) * (1.0 - t) + at(&rows[i + 1]) * t } +/// The curve at a fractional internal amount `x` as one row: `curve(rows, from, x, v)` is this +/// row interpolated at `v`, because both interpolations are linear. +fn row_at(rows: &[[f32; STEPS]], from: f32, x: f32) -> [f32; STEPS] { + let r = ((x - from) / 10.0).clamp(0.0, (rows.len() - 1) as f32); + let i = (r as usize).min(rows.len() - 2); + let t = r - i as f32; + std::array::from_fn(|j| rows[i][j] * (1.0 - t) + rows[i + 1][j] * t) +} + +/// The Whites curve at internal amount `x` as a row over grey levels 0, 1/32, …, 1. +pub(crate) fn whites_row(x: f32) -> [f32; STEPS] { + row_at(&WHITES, WHITES_FROM, x) +} + +/// The Blacks curve at internal amount `x` as a row over grey levels 0, 1/32, …, 1. +pub(crate) fn blacks_row(x: f32) -> [f32; STEPS] { + row_at(&BLACKS, BLACKS_FROM, x) +} + /// Whites at internal amount `x` (−100 … 270) of grey level `v`. pub(crate) fn whites(v: f32, x: f32) -> f32 { curve(&WHITES, WHITES_FROM, x, v) @@ -369,14 +388,14 @@ pub(crate) fn apply(px: &mut [[f32; 4]], width: usize, w: f32, b: f32, enc: Enco if per_channel { for c in 0..3 { let over = (lin[c] - 1.0).max(0.0); - q[c] = enc.encode(super::srgb_decode(grey(super::srgb_encode(lin[c].min(1.0)))) + over); + q[c] = enc.encode(super::srgb_to_linear(grey(super::linear_to_srgb(lin[c].min(1.0)))) + over); } } else { let y = super::luma_linear(lin).max(0.0); if y <= 1e-7 { continue; } - let y2 = super::srgb_decode(grey(super::srgb_encode(y.min(1.0)))) + (y - 1.0).max(0.0); + let y2 = super::srgb_to_linear(grey(super::linear_to_srgb(y.min(1.0)))) + (y - 1.0).max(0.0); let k = y2 / y; for c in 0..3 { q[c] = enc.encode(lin[c] * k); diff --git a/crates/algo/src/content_aware.rs b/crates/algo/src/content_aware.rs index 76880cd5ad..955a911119 100644 --- a/crates/algo/src/content_aware.rs +++ b/crates/algo/src/content_aware.rs @@ -114,6 +114,21 @@ struct Variant { ok: Vec, } +/// Most pixels the mosaic of the image and its variants may span. It is one dense rectangle, so +/// thin images turned to different angles leave it mostly empty: the cost grows with the square +/// of their length, not with their pixels. +const MAX_MOSAIC_PIXELS: usize = 1 << 25; + +/// Size of the mosaic of an `w`×`h` image and `extra` variant sizes separated by `gap`, or `None` +/// when it spans more than [`MAX_MOSAIC_PIXELS`] (or overflows). +fn mosaic_size(w: usize, h: usize, gap: usize, extra: &[(usize, usize)]) -> Option<(usize, usize)> { + let col_w = extra.iter().map(|v| v.0).max().unwrap_or(0); + let col_h = extra.iter().try_fold(0usize, |n, v| n.checked_add(v.1)?.checked_add(gap))?; + let mw = w.checked_add(gap)?.checked_add(col_w)?; + let mh = h.max(col_h); + (mw.checked_mul(mh)? <= MAX_MOSAIC_PIXELS).then_some((mw, mh)) +} + /// A source buffer and which of its pixels may be sampled. #[derive(Clone, Copy)] struct Src<'a> { @@ -149,13 +164,20 @@ fn sample(src: Src, x: f32, y: f32, out: &mut [f32]) -> bool { /// Transformed copy of the source: rotation (degrees) and scale about the centre, optional mirror. /// `Err` when cancelled. -fn variant(src: Src, deg: f32, scale: f32, mirror: bool, ctl: &Interrupt) -> Result { - let Src { w, h, ch, .. } = src; +/// Dimensions of the [`variant`] copy: the same bounding-box maths without building its pixels. +fn variant_dims(w: usize, h: usize, deg: f32, scale: f32) -> (usize, usize) { let (s, c) = (-deg.to_radians()).sin_cos(); - // Output size: the rotated, scaled bounding box. let (fw, fh) = (w as f32 * scale, h as f32 * scale); let ow = ((fw * c.abs() + fh * s.abs()).round() as usize).max(1); let oh = ((fw * s.abs() + fh * c.abs()).round() as usize).max(1); + (ow, oh) +} + +fn variant(src: Src, deg: f32, scale: f32, mirror: bool, ctl: &Interrupt) -> Result { + let Src { w, h, ch, .. } = src; + // Output size: the rotated, scaled bounding box. + let (ow, oh) = variant_dims(w, h, deg, scale); + let (s, c) = (-deg.to_radians()).sin_cos(); let (icx, icy) = ((w as f32 - 1.0) / 2.0, (h as f32 - 1.0) / 2.0); let (ocx, ocy) = ((ow as f32 - 1.0) / 2.0, (oh as f32 - 1.0) / 2.0); let mut out = Variant { w: ow, h: oh, img: vec![0.0; ow * oh * ch], ok: vec![false; ow * oh] }; @@ -243,17 +265,27 @@ fn fill_cow( let gap = 2 * params.patch_radius + 2; let ok: Vec = par::map(w * h, true, |i| !hole[i] && source[i]); let src = Src { w, h, ch, img: &img, ok: &ok }; - // Variants: the identity (which holds the hole) first, then the adaptations. - let mut extra: Vec = Vec::new(); + // Variants: the identity (which holds the hole) first, then the adaptations. Their pixels are + // expensive, so the mosaic budget is settled on their dimensions before any variant is built; + // when the variants would not fit, the image itself is completed without them. + let mut specs: Vec<(f32, f32, bool)> = Vec::new(); if opts.mirror { - extra.push(variant(src, 0.0, 1.0, true, ctl)?); + specs.push((0.0, 1.0, true)); } for ° in &opts.rotations { - extra.push(variant(src, deg, 1.0, false, ctl)?); + specs.push((deg, 1.0, false)); } if opts.scale { - extra.push(variant(src, 0.0, 0.8, false, ctl)?); - extra.push(variant(src, 0.0, 1.25, false, ctl)?); + specs.push((0.0, 0.8, false)); + specs.push((0.0, 1.25, false)); + } + let dims: Vec<(usize, usize)> = specs.iter().map(|&(deg, scale, _)| variant_dims(w, h, deg, scale)).collect(); + let size = if specs.is_empty() { None } else { mosaic_size(w, h, gap, &dims) }; + let mut extra: Vec = Vec::new(); + if size.is_some() { + for &(deg, scale, mirror) in &specs { + extra.push(variant(src, deg, scale, mirror, ctl)?); + } } let role = |s: usize| { if hole[s] { @@ -265,14 +297,9 @@ fn fill_cow( } }; // Mosaic: identity at the left, variants in a column to its right, gaps that are no image. - // Without variants it is the image itself. - let (mw, mh, mosaic, roles): (usize, usize, Option>, Vec) = if extra.is_empty() { - (w, h, None, par::map(w * h, true, role)) - } else { - let col_w = extra.iter().map(|v| v.w).max().unwrap_or(0); - let col_h: usize = extra.iter().map(|v| v.h + gap).sum(); - let mw = w + gap + col_w; - let mh = h.max(col_h); + // Without variants it is the image itself; so it is when the variants would not fit + // (`size` above is then None and `extra` is empty). + let (mw, mh, mosaic, roles): (usize, usize, Option>, Vec) = if let Some((mw, mh)) = size { let mut mimg = vec![0.0f32; mw * mh * ch]; let mut roles = vec![Px::Void; mw * mh]; for y in 0..h { @@ -297,6 +324,8 @@ fn fill_cow( oy += v.h + gap; } (mw, mh, Some(mimg), roles) + } else { + (w, h, None, par::map(w * h, true, role)) }; drop((extra, ok)); ctl.check()?; @@ -566,400 +595,132 @@ pub fn level_transform(ch: usize, from: &[f32], to: &[f32], a: Adapt) -> Vec<(f3 .collect() } -#[cfg(test)] -mod tests { - use super::*; +/// One stroke of the Content-Aware Fill workspace's Sampling Brush: a hard round brush of +/// diameter `size` along `points` (document px), adding to the sampling area or taking from it. +#[derive(Clone, Debug, PartialEq)] +pub struct SamplingStroke { + pub add: bool, + pub size: f64, + pub points: Vec<[f64; 2]>, +} - /// Vertical stripes (period 6) in grey, with a hole in the middle. - fn stripes(w: usize, h: usize) -> (Vec, Vec) { - let img: Vec = (0..w * h).map(|i| if (i % w) % 6 < 3 { 0.2 } else { 0.8 }).collect(); - let hole: Vec = (0..w * h).map(|i| (i % w).abs_diff(w / 2) < 4 && (i / w).abs_diff(h / 2) < 4).collect(); - (img, hole) - } - - #[test] - fn cancelled_fill_stops_and_progress_reaches_one() { - use std::sync::atomic::{AtomicU32, Ordering}; - let (w, h) = (60, 40); - let (img, hole) = stripes(w, h); - let source = vec![true; w * h]; - let yes = || true; - let cancelled = photocraft_raster::Interrupt::cancel_only(&yes); - assert!(fill_with(w, h, 1, &img, &hole, &source, &FillOptions::default(), &cancelled).is_err()); - let last = AtomicU32::new(0); - let no = || false; - let progress = |f: f32| { - assert!(f >= f32::from_bits(last.load(Ordering::Relaxed)), "progress went backwards"); - last.store(f.to_bits(), Ordering::Relaxed); - }; - let out = fill_with(w, h, 1, &img, &hole, &source, &FillOptions::default(), &photocraft_raster::Interrupt::new(&no, &progress)).unwrap(); - assert_eq!(f32::from_bits(last.load(Ordering::Relaxed)), 1.0); - assert_eq!(out, fill(w, h, 1, &img, &hole, &source, &FillOptions::default()), "same result as the plain fill"); - } - - #[test] - fn filling_in_place_is_filling_a_copy() { - let (w, h) = (90, 70); - let rgb: Vec = noise(w, h, 4).iter().enumerate().map(|(i, v)| v * 0.5 + 0.3 * ((i / 3 % w) % 11 < 5) as u8 as f32).collect(); - let hole: Vec = (0..w * h).map(|i| (30..52).contains(&(i % w)) && (25..44).contains(&(i / w))).collect(); - let source: Vec = (0..w * h).map(|i| i % w < 75).collect(); - let none = Interrupt::NONE; - for opts in [FillOptions::default(), FillOptions { color_adaptation: Adapt::NONE, mirror: true, ..Default::default() }] { - let copy = fill_with(w, h, 3, &rgb, &hole, &source, &opts, &none).unwrap(); - let owned = fill_owned(w, h, 3, rgb.clone(), &hole, &source, &opts, &none).unwrap(); - assert_eq!(copy.iter().map(|v| v.to_bits()).collect::>(), owned.iter().map(|v| v.to_bits()).collect::>()); - // Only the hole changes. - assert!((0..w * h).all(|i| hole[i] || (0..3).all(|c| owned[i * 3 + c].to_bits() == rgb[i * 3 + c].to_bits()))); - } - // Nothing to copy from: the membrane, either way. - let nowhere = vec![false; w * h]; - let copy = fill_with(w, h, 3, &rgb, &hole, &nowhere, &FillOptions::default(), &none).unwrap(); - assert_eq!(copy, fill_owned(w, h, 3, rgb.clone(), &hole, &nowhere, &FillOptions::default(), &none).unwrap()); - } - - #[test] - fn auto_sampling_can_be_cancelled() { - let (w, h) = (200, 160); - let img: Vec = noise(w, h, 5).iter().enumerate().map(|(i, v)| v * 0.1 + if (i / 3) % w < 120 { 0.3 } else { 0.6 }).collect(); - let hole: Vec = (0..w * h).map(|i| (80..110).contains(&(i % w)) && (60..90).contains(&(i / w))).collect(); - let yes = || true; - assert!(auto_sampling_with(w, h, 3, &img, &hole, &Interrupt::cancel_only(&yes)).is_err()); - assert_eq!(auto_sampling_with(w, h, 3, &img, &hole, &Interrupt::NONE).unwrap(), auto_sampling(w, h, 3, &img, &hole)); - } - - #[test] - fn fills_only_the_hole_with_known_texture() { - let (w, h) = (48, 32); - let (img, hole) = stripes(w, h); - let src = vec![true; w * h]; - let out = fill(w, h, 1, &img, &hole, &src, &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); - for i in 0..w * h { - if !hole[i] { - assert_eq!(out[i], img[i]); - } - } - // The hole is filled with stripe values, not a flat average. - let vals: Vec = (0..w * h).filter(|i| hole[*i]).map(|i| out[i]).collect(); - assert!(vals.iter().any(|v| *v < 0.35) && vals.iter().any(|v| *v > 0.65), "{vals:?}"); - } - - #[test] - fn sampling_area_restricts_sources() { - // Left half dark, right half bright; sampling only the right half fills bright. - let (w, h) = (40, 20); - let img: Vec = (0..w * h).map(|i| if i % w < 20 { 0.1 } else { 0.9 }).collect(); - let hole: Vec = (0..w * h).map(|i| (16..24).contains(&(i % w)) && (6..14).contains(&(i / w))).collect(); - let right: Vec = (0..w * h).map(|i| i % w >= 24).collect(); - let out = fill(w, h, 1, &img, &hole, &right, &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); - let mean: f32 = (0..w * h).filter(|i| hole[*i]).map(|i| out[i]).sum::() / 64.0; - assert!(mean > 0.8, "mean {mean}"); - } - - #[test] - fn adaptations_run_and_stay_deterministic() { - let (w, h) = (32, 24); - let (img, hole) = stripes(w, h); - let rgb: Vec = img.iter().flat_map(|v| [*v, *v * 0.5, 1.0 - *v]).collect(); - let src = vec![true; w * h]; - let opts = FillOptions { color_adaptation: color_level("high"), rotations: rotation_level("low"), scale: true, mirror: true, seed: 7 }; - let a = fill(w, h, 3, &rgb, &hole, &src, &opts); - let b = fill(w, h, 3, &rgb, &hole, &src, &opts); - assert_eq!(a, b); - assert!(a.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v))); - } - - #[test] - fn color_adaptation_follows_a_gradient() { - // A horizontal brightness ramp with a hole: adaptation keeps the fill near the ramp. - let (w, h) = (40, 20); - let img: Vec = (0..w * h).map(|i| (i % w) as f32 / w as f32).collect(); - let hole: Vec = (0..w * h).map(|i| (14..26).contains(&(i % w)) && (5..15).contains(&(i / w))).collect(); - let src = vec![true; w * h]; - let out = fill(w, h, 1, &img, &hole, &src, &FillOptions { color_adaptation: color_level("veryHigh"), ..Default::default() }); - let err: f32 = (0..w * h).filter(|i| hole[*i]).map(|i| (out[i] - img[i]).abs()).sum::() / 120.0; - assert!(err < 0.12, "mean error {err}"); - } - - /// A vertical sky gradient (each row one colour) filled at every adaptation level stays the - /// gradient: no tinted blotch where the copied rows' colours were shifted per channel. - #[test] - fn color_adaptation_leaves_a_smooth_gradient_untinted() { - let (w, h) = (120, 60); - let row = |y: usize| { - let t = y as f32 / (h - 1) as f32; - [0.27 + 0.45 * t, 0.53 + 0.32 * t, 0.86 + 0.1 * t] - }; - let img: Vec = (0..w * h).flat_map(|i| row(i / w)).collect(); - let hole: Vec = (0..w * h).map(|i| (50..70).contains(&(i % w)) && (20..40).contains(&(i / w))).collect(); - let src = vec![true; w * h]; - for level in ["none", "default", "high", "veryHigh"] { - let out = fill(w, h, 3, &img, &hole, &src, &FillOptions { color_adaptation: color_level(level), ..Default::default() }); - let (out, img) = (&out, &img); - let worst = (0..w * h).filter(|i| hole[*i]).flat_map(|i| (0..3).map(move |c| (out[i * 3 + c] - img[i * 3 + c]).abs())).fold(0.0f32, f32::max); - assert!(worst < 3.0 / 255.0, "{level}: off by {}", worst * 255.0); - } - } - - /// I.i.d. noise: every value occurs once, so a filled pixel equal to an image pixel was copied. - fn noise(w: usize, h: usize, seed: u64) -> Vec { - let mut z = seed; - (0..w * h * 3) - .map(|_| { - z = z.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); - 0.2 + 0.6 * ((z >> 40) as f32 / (1u64 << 24) as f32) - }) - .collect() - } - - #[test] - fn sampling_window_is_photoshops() { - let canvas = Rect::new(0, 0, 800, 800); - let sq = |x0: i32, y0: i32, w: i32, h: i32| Rect::new(x0, y0, x0 + w, y0 + h); - // Photoshop 25.4's sampling overlay: a square of side 4·√(max(w,50)·max(h,50)) centred on - // the selection's bounds. - assert_eq!(sampling_window(sq(375, 375, 50, 50), canvas), sq(300, 300, 200, 200)); - assert_eq!(sampling_window(sq(350, 350, 100, 100), canvas), sq(200, 200, 400, 400)); - assert_eq!(sampling_window(sq(395, 395, 10, 10), canvas), sq(300, 300, 200, 200)); - assert_eq!(sampling_window(sq(370, 370, 60, 60), canvas), sq(280, 280, 240, 240)); - // Long selections (Photoshop's overlay, read off the screen to a pixel: 281, 399, 691). - assert_eq!(sampling_window(sq(350, 385, 100, 30), canvas), sq(258, 258, 283, 283)); - assert_eq!(sampling_window(sq(300, 375, 200, 50), canvas), sq(200, 200, 400, 400)); - assert_eq!(sampling_window(sq(250, 350, 300, 100), canvas), sq(53, 53, 693, 693)); - // At the canvas edge the square slides inwards rather than being cut. - assert_eq!(sampling_window(sq(10, 375, 50, 50), canvas), sq(0, 300, 200, 200)); - assert_eq!(sampling_window(sq(5, 5, 60, 60), canvas), sq(0, 0, 240, 240)); - // A canvas smaller than the square clips it; a very long selection keeps its length. - assert_eq!(sampling_window(sq(40, 40, 50, 50), Rect::new(0, 0, 150, 120)), Rect::new(0, 0, 150, 120)); - assert_eq!(sampling_window(sq(100, 995, 1800, 10), Rect::new(0, 0, 2000, 2000)), sq(100, 400, 1800, 1200)); - assert_eq!(sampling_window(Rect::EMPTY, canvas), Rect::EMPTY); - } - - #[test] - fn auto_sampling_keeps_what_looks_like_the_surroundings() { - // Grey texture, dark on the left, 50 levels brighter on the right; the hole is in the dark - // part near the boundary (Photoshop's Auto leaves the bright part out). - let (w, h) = (200, 200); - let tex = noise(w, h, 3); - let shade = |x: usize, band: bool| { - if band { - if (130..150).contains(&x) { 0.55 } else { 0.35 } - } else if x < 135 { - 0.35 - } else { - 0.55 - } - }; - for band in [false, true] { - let img: Vec = (0..w * h * 3).map(|i| shade((i / 3) % w, band) + (tex[i] - 0.5) * 0.1).collect(); - let hole: Vec = (0..w * h).map(|i| (75..125).contains(&(i % w)) && (75..125).contains(&(i / w))).collect(); - let area = auto_sampling(w, h, 3, &img, &hole); - let frac = |x0: usize, x1: usize| { - let n = (0..w * h).filter(|i| (x0..x1).contains(&(i % w)) && !hole[*i]).count(); - (0..w * h).filter(|i| (x0..x1).contains(&(i % w)) && area[*i]).count() as f32 / n as f32 - }; - assert!(hole.iter().zip(&area).all(|(hl, a)| !(*hl && *a)), "never the hole"); - assert!(frac(0, 125) > 0.95, "the dark side is sampled ({})", frac(0, 125)); - if band { - // A bright band through the window is left out; the dark area beyond it is not. - assert!(frac(133, 147) < 0.05, "the band is excluded ({})", frac(133, 147)); - assert!(frac(155, 200) > 0.9, "beyond the band is sampled ({})", frac(155, 200)); - } else { - assert!(frac(140, 200) < 0.05, "the bright side is excluded ({})", frac(140, 200)); - } - } - } - - #[test] - fn auto_sampling_keeps_the_surroundings_of_every_part_of_a_thin_selection() { - // A long thin selection across a grey ramp, and a small knob selected with it in a red - // corner. Each part keeps its own surroundings; a compact selection is unchanged. - let (w, h) = (900, 600); - let tex = noise(w, h, 4); - let img: Vec = (0..w * h * 3) - .map(|i| { - let (x, y, c) = ((i / 3) % w, (i / 3) / w, i % 3); - let base = if x > 690 && y > 420 { [0.7, 0.25, 0.2][c] } else { 0.2 + 0.5 * x as f32 / w as f32 }; - base + (tex[i] - 0.5) * 0.04 - }) - .collect(); - let line = |i: usize| (30..870).contains(&(i % w)) && (180..192).contains(&(i / w)); - let knob = |i: usize| ((i % w) as f32 - 795.0).hypot((i / w) as f32 - 510.0) < 10.0; - let hole: Vec = (0..w * h).map(|i| line(i) || knob(i)).collect(); - let area = auto_sampling(w, h, 3, &img, &hole); - let around_knob = (0..w * h).filter(|&i| !hole[i] && ((i % w) as f32 - 795.0).hypot((i / w) as f32 - 510.0) < 24.0); - let (n, kept) = around_knob.fold((0, 0), |(n, k), i| (n + 1, k + usize::from(area[i]))); - assert!(kept as f32 > 0.9 * n as f32, "{kept} of {n} pixels around the knob"); - assert!(thin_band(w, h, &hole) >= 20.0, "{}", thin_band(w, h, &hole)); - let disc: Vec = (0..w * h).map(|i| ((i % w) as f32 - 450.0).hypot((i / w) as f32 - 300.0) < 90.0).collect(); - assert_eq!(thin_band(w, h, &disc), 1.5); - } - - #[test] - fn without_colour_adaptation_the_fill_copies_pixels_exactly() { - // A smooth texture with fine noise, so that every value is unique: a filled pixel equal to - // an image pixel was copied from it. Photoshop's fill (colour adaptation None) is such - // copies, joined by narrow seams. - let (w, h) = (120, 120); - let fine = noise(w, h, 9); - let img: Vec = (0..w * h * 3) - .map(|i| { - let (x, y, c) = (((i / 3) % w) as f32, ((i / 3) / w) as f32, i % 3); - 0.5 + 0.2 * (x * 0.21 + c as f32).sin() * (y * 0.17).cos() + 0.1 * (x * 0.05 - y * 0.08).sin() + (fine[i] - 0.5) * 0.08 - }) - .collect(); - let hole: Vec = (0..w * h).map(|i| (40..80).contains(&(i % w)) && (40..80).contains(&(i / w))).collect(); - // Votes of identical copies average to the copy up to float rounding. - let key = |v: &[f32], i: usize| [0, 1, 2].map(|c| (v[i * 3 + c] * 1e5).round() as i64); - let known: std::collections::HashSet<[i64; 3]> = (0..w * h).filter(|i| !hole[*i]).map(|i| key(&img, i)).collect(); - let out = fill(w, h, 3, &img, &hole, &vec![true; w * h], &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); - let exact = (0..w * h).filter(|i| hole[*i] && known.contains(&key(&out, *i))).count(); - assert!(exact as f32 > 0.2 * 1600.0, "{exact} of 1600 filled pixels are exact copies"); - // And the fill is as busy as the noise around it, not a blur of it. - let lap = |v: &[f32], x: usize, y: usize| { - let p = |x: usize, y: usize| v[(y * w + x) * 3]; - (4.0 * p(x, y) - p(x - 1, y) - p(x + 1, y) - p(x, y - 1) - p(x, y + 1)).abs() - }; - let mean = |inside: bool| { - let pts: Vec<(usize, usize)> = (1..h - 1).flat_map(|y| (1..w - 1).map(move |x| (x, y))).filter(|&(x, y)| hole[y * w + x] == inside).collect(); - pts.iter().map(|&(x, y)| lap(&out, x, y)).sum::() / pts.len() as f32 - }; - assert!(mean(true) > 0.6 * mean(false), "fill detail {} against {}", mean(true), mean(false)); - } - - #[test] - fn patch_color_shifts_up_to_about_four_hundredths_per_step() { - // Content 0.4 brighter than where it lands (Photoshop's Patch, Color 3 and 10). - let from: Vec = (0..300).map(|i| 0.7 + 0.05 * ((i % 7) as f32 / 7.0)).collect(); - let to: Vec = from.iter().map(|v| v - 0.4).collect(); - let level = from.iter().sum::() / 300.0; - let shift = |c: u8| { - let gb = level_transform(1, &from, &to, color_amount(c)); - level - from.iter().map(|v| gb[0].0 * v + gb[0].1).sum::() / 300.0 - }; - assert_eq!(shift(0), 0.0); - assert!((shift(3) - 0.12).abs() < 0.01, "{}", shift(3)); - assert!((shift(10) - 0.4).abs() < 0.01, "{}", shift(10)); - } - - /// A soft background (gradients, broad out-of-focus blobs of colour, fine noise) with thin, long bright - /// objects over it: three coiled strings 3 to 7 px wide and two straighter ones, each with an - /// anti-aliased edge and a faint halo. Returns the image; what removing the coils should give - /// (the background and the straighter strings, which stay); and the selection a user would draw - /// around the coils, `margin` px wide. - fn strings_scene(w: usize, h: usize, margin: f32) -> (Vec, Vec, Vec) { - let fine = noise(w, h, 11); - // Out-of-focus colour: broad blobs over two gradients. - let blob = noise(3, 3, 5); - let bg: Vec = (0..w * h * 3) - .map(|i| { - let (x, y, c) = (((i / 3) % w) as f32 / w as f32, ((i / 3) / w) as f32 / h as f32, i % 3); - let mut v = [0.18 + 0.25 * x, 0.22 + 0.1 * y, 0.3 - 0.1 * x][c]; - for b in blob.as_chunks::<3>().0 { - let (bx, by, s) = (b[0] * 1.6 - 0.3, b[1] * 1.6 - 0.3, 0.1 + 0.25 * b[2]); - v += (b[(c + 1) % 3] - 0.2) * 0.5 * (-((x - bx).powi(2) + (y - by).powi(2) * 0.56) / (2.0 * s * s)).exp(); - } - v + (fine[i] - 0.5) * 0.03 - }) - .collect(); - // Signed distance to each object's surface, stamped from points along it. - let stamp = |sd: &mut [f32], pts: &mut dyn Iterator, half: f32| { - for (px, py) in pts { - let reach = (half + 4.0) as i32 + 1; - for dy in -reach..=reach { - for dx in -reach..=reach { - let (x, y) = (px as i32 + dx, py as i32 + dy); - if x >= 0 && y >= 0 && (x as usize) < w && (y as usize) < h { - let i = y as usize * w + x as usize; - sd[i] = sd[i].min((x as f32 - px).hypot(y as f32 - py) - half); - } - } - } - } - }; - let mut coils = vec![f32::INFINITY; w * h]; - for (k, half) in [1.5f32, 2.5, 3.5].into_iter().enumerate() { - let (y0, r) = ((0.22 + 0.28 * k as f32) * h as f32, 10.0 + 2.0 * k as f32); - let mut pts = (0..4000).map(|j| { - let t = j as f32 / 4000.0; - let a = 8.0 * std::f32::consts::TAU * t; - (18.0 + t * (w as f32 - 60.0) + r * a.cos(), y0 + 6.0 * (6.0 * t).sin() + 0.7 * r * a.sin()) - }); - stamp(&mut coils, &mut pts, half); - } - let mut straight = vec![f32::INFINITY; w * h]; - for y in [0.07, 0.93] { - stamp(&mut straight, &mut (0..2000).map(|j| (j as f32 / 2000.0 * w as f32, y * h as f32 + 3.0 * (j as f32 / 300.0).sin())), 2.0); - } - let draw = |base: &[f32], sd: &[f32]| -> Vec { - let mut img = base.to_vec(); - for i in 0..w * h { - let cover = (0.5 - sd[i]).clamp(0.0, 1.0); - let halo = if sd[i] > 0.0 { 0.08 * (-sd[i] * sd[i] / 2.9).exp() } else { 0.0 }; - for (c, metal) in [0.9f32, 0.85, 0.7].into_iter().enumerate() { - let v = &mut img[i * 3 + c]; - *v = *v * (1.0 - cover) + metal * cover + halo * (1.0 - cover); - } - } - img - }; - let clean = draw(&bg, &straight); - let both: Vec = coils.iter().zip(&straight).map(|(a, b)| a.min(*b)).collect(); - let img = draw(&bg, &both); - let hole = coils.iter().map(|d| *d <= margin).collect(); - (img, clean, hole) - } - - /// RMS over the hole of the error averaged over 7 × 7 pixels (in levels of 255): the noise - /// cancels out, a ghost of the removed outline or a blotch of another shade does not. - fn blotch_error(w: usize, h: usize, out: &[f32], clean: &[f32], hole: &[bool]) -> f32 { - let (mut sum, mut n) = (0.0f64, 0usize); - for y in 3..h - 3 { - for x in 3..w - 3 { - if !hole[y * w + x] { - continue; - } - let mut e = [0.0f32; 3]; - for j in (y - 3..=y + 3).flat_map(|yy| (x - 3..=x + 3).map(move |xx| yy * w + xx)) { - for (c, ec) in e.iter_mut().enumerate() { - *ec += out[j * 3 + c] - clean[j * 3 + c]; - } - } - sum += f64::from(e.iter().map(|v| (v / 49.0).powi(2)).sum::() / 3.0); - n += 1; - } - } - ((sum / n.max(1) as f64).sqrt() * 255.0) as f32 - } - - /// Removing thin, long objects (guitar strings, wires) from a soft background (#1366): the - /// fill is the background, with no ghost of their outline and nothing of their colour copied - /// back in. Their selection is long but only a few pixels thick, so the fill is worked out at - /// a scale where it is still a few pixels thick rather than one where the strings' surroundings - /// have merged into a blur. - #[test] - fn thin_objects_leave_no_ghost_and_none_of_their_colour() { - let (w, h) = (360, 270); - let (img, clean, hole) = strings_scene(w, h, 2.0); - let source = auto_sampling(w, h, 3, &img, &hole); - let out = fill(w, h, 3, &img, &hole, &source, &FillOptions::default()); - let blotch = blotch_error(w, h, &out, &clean, &hole); - let luma = |v: &[f32], i: usize| (v[i * 3] + v[i * 3 + 1] + v[i * 3 + 2]) / 3.0; - let bright = (0..w * h).filter(|&i| hole[i] && luma(&out, i) > luma(&clean, i) + 0.06).count(); - // Before #1366 the outlines stayed as darker and lighter blotches (13 levels of 255) and - // 1121 pixels took the strings' colour. - assert!(blotch < 2.5, "ghosts and blotches: {blotch:.2} levels of 255"); - assert_eq!(bright, 0, "filled pixels with the strings' colour"); - } - - #[test] - fn variant_geometry() { - let img = vec![0.5f32; 10 * 6]; - let ok = vec![true; 60]; - let src = Src { w: 10, h: 6, ch: 1, img: &img, ok: &ok }; - let v = variant(src, 90.0, 1.0, false, &Interrupt::NONE).unwrap(); - assert_eq!((v.w, v.h), (6, 10)); - let v = variant(src, 0.0, 0.8, true, &Interrupt::NONE).unwrap(); - assert_eq!((v.w, v.h), (8, 5)); - assert!(v.ok.iter().filter(|o| **o).count() > 20); +impl SamplingStroke { + /// Bounds of the pixels the stroke covers. + pub fn bounds(&self) -> Rect { + let r = (self.size / 2.0).max(0.5); + self.points.iter().fold(Rect::EMPTY, |acc, p| { + let b = Rect::new( + (p[0] - r).floor() as i32, + (p[1] - r).floor() as i32, + ((p[0] + r).ceil() as i32).saturating_add(1), + ((p[1] + r).ceil() as i32).saturating_add(1), + ); + if acc.is_empty() { b } else { acc.union(&b) } + }) } } + +/// Squared distance from `p` to the segment `a`–`b`. +fn seg_dist2(p: [f64; 2], a: [f64; 2], b: [f64; 2]) -> f64 { + let (dx, dy) = (b[0] - a[0], b[1] - a[1]); + let l2 = dx * dx + dy * dy; + let t = if l2 > 0.0 { (((p[0] - a[0]) * dx + (p[1] - a[1]) * dy) / l2).clamp(0.0, 1.0) } else { 0.0 }; + let (qx, qy) = (a[0] + t * dx - p[0], a[1] + t * dy - p[1]); + qx * qx + qy * qy +} + +/// Paint `strokes` in order into `mask` (row-major over `area`): a pixel whose centre lies within +/// half the brush size of a stroke's path is set (adding) or cleared (subtracting). A mask that +/// does not cover `area` exactly is left as it is. +pub fn paint_strokes(mask: &mut [bool], area: Rect, strokes: &[SamplingStroke]) { + let w = area.width() as usize; + if mask.len() != w * area.height() as usize { + return; + } + for s in strokes { + let r = (s.size / 2.0).max(0.5); + let r2 = r.powi(2); + let segs: Vec<([f64; 2], [f64; 2])> = match s.points.as_slice() { + [] => continue, + [p] => vec![(*p, *p)], + pts => pts.windows(2).map(|p| (p[0], p[1])).collect(), + }; + // A stroke covers the union of its segments' pixels: each segment only looks at its own + // bounds. + for (a, b) in segs { + let bounds = Rect::new( + (a[0].min(b[0]) - r).floor() as i32, + (a[1].min(b[1]) - r).floor() as i32, + ((a[0].max(b[0]) + r).ceil() as i32).saturating_add(1), + ((a[1].max(b[1]) + r).ceil() as i32).saturating_add(1), + ) + .intersect(&area); + for y in bounds.y0..bounds.y1 { + for x in bounds.x0..bounds.x1 { + let c = [f64::from(x) + 0.5, f64::from(y) + 0.5]; + if seg_dist2(c, a, b) <= r2 + && let Some(m) = mask.get_mut((y - area.y0) as usize * w + (x - area.x0) as usize) + { + *m = s.add; + } + } + } + } + } +} + +/// The pixels of `area` (row-major) whose centres lie inside the polygon `pts` (even-odd rule): +/// what a lasso outlines. +pub fn polygon_mask(area: Rect, pts: &[[f64; 2]]) -> Vec { + let (w, h) = (area.width() as usize, area.height() as usize); + let mut out = vec![false; w * h]; + if pts.len() < 3 { + return out; + } + let mut xs: Vec = Vec::new(); + for row in 0..h { + let cy = f64::from(area.y0) + row as f64 + 0.5; + xs.clear(); + for i in 0..pts.len() { + let (a, b) = (pts[i], pts[(i + 1) % pts.len()]); + if (a[1] <= cy) != (b[1] <= cy) { + xs.push(a[0] + (cy - a[1]) / (b[1] - a[1]) * (b[0] - a[0])); + } + } + xs.sort_by(f64::total_cmp); + for pair in xs.as_chunks::<2>().0 { + // Pixel centres x + 0.5 within [pair0, pair1). + let x0 = ((pair[0] - 0.5).ceil() as i64 - i64::from(area.x0)).clamp(0, w as i64) as usize; + let x1 = ((pair[1] - 0.5).ceil() as i64 - i64::from(area.x0)).clamp(0, w as i64) as usize; + out[row * w + x0..row * w + x1.max(x0)].iter_mut().for_each(|v| *v = true); + } + } + out +} + +/// Grow (`by` > 0) or shrink (`by` < 0) a mask by `|by|` pixels (Euclidean): the lasso's Expand +/// and Contract. +pub fn grow_mask(mask: &[bool], w: usize, h: usize, by: i32) -> Vec { + if by == 0 || w == 0 || h == 0 { + return mask.to_vec(); + } + let r = f32::from(u16::try_from(by.unsigned_abs()).unwrap_or(u16::MAX)); + if by > 0 { + let d = crate::selection::edt(mask, w, h); + d.iter().map(|v| *v <= r).collect() + } else { + let outside: Vec = mask.iter().map(|v| !v).collect(); + let d = crate::selection::edt(&outside, w, h); + d.iter().map(|v| *v > r).collect() + } +} + +/// The Sampling Brush's starting size for a selection with bounds `hole`: a tenth of the sampling +/// window's side, as Photoshop opens the workspace (20 px for a 50 px selection, 40 for 100). +pub fn default_brush_size(hole: Rect) -> f64 { + let (w, h) = (f64::from(hole.width().max(50)), f64::from(hole.height().max(50))); + (0.4 * (w * h).sqrt()).round() +} + +#[cfg(test)] +mod tests; diff --git a/crates/algo/src/content_aware/tests.rs b/crates/algo/src/content_aware/tests.rs new file mode 100644 index 0000000000..183e6e5b88 --- /dev/null +++ b/crates/algo/src/content_aware/tests.rs @@ -0,0 +1,445 @@ +use super::mosaic_size; + +fn blank(w: usize, h: usize) -> (usize, usize) { + (w, h) +} + +#[test] +fn a_thin_image_with_turned_variants_does_not_get_a_quadratic_mosaic() { + // 40000×1 plus its 90° copy (1×40000): about 1.6 billion mosaic cells for 80000 pixels. + assert_eq!(mosaic_size(40_000, 1, 8, &[blank(1, 40_000)]), None); + assert_eq!(mosaic_size(usize::MAX, 1, 8, &[blank(1, 1)]), None); + assert_eq!(mosaic_size(100, 100, 8, &[blank(100, 100)]), Some((208, 108))); +} + +use super::*; + +/// Vertical stripes (period 6) in grey, with a hole in the middle. +fn stripes(w: usize, h: usize) -> (Vec, Vec) { + let img: Vec = (0..w * h).map(|i| if (i % w) % 6 < 3 { 0.2 } else { 0.8 }).collect(); + let hole: Vec = (0..w * h).map(|i| (i % w).abs_diff(w / 2) < 4 && (i / w).abs_diff(h / 2) < 4).collect(); + (img, hole) +} + +#[test] +fn strokes_paint_their_path_and_leave_a_mismatched_mask_alone() { + let area = Rect::new(10, 20, 50, 40); + let (w, h) = (40, 20); + let strokes = [ + SamplingStroke { add: true, size: 6.0, points: vec![[12.0, 25.0], [40.0, 25.0], [45.0, 35.0]] }, + SamplingStroke { add: false, size: 3.0, points: vec![[30.0, 25.0]] }, + SamplingStroke { add: true, size: 2.0, points: Vec::new() }, + ]; + let mut mask = vec![false; w * h]; + paint_strokes(&mut mask, area, &strokes); + // The same pixels as testing every pixel against every segment of each stroke in turn. + let mut want = vec![false; w * h]; + for s in &strokes { + let r2 = (s.size / 2.0).max(0.5).powi(2); + let segs: Vec<_> = if s.points.len() == 1 { vec![(s.points[0], s.points[0])] } else { s.points.windows(2).map(|p| (p[0], p[1])).collect() }; + for (i, m) in want.iter_mut().enumerate() { + let c = [f64::from(area.x0) + (i % w) as f64 + 0.5, f64::from(area.y0) + (i / w) as f64 + 0.5]; + if segs.iter().any(|(a, b)| seg_dist2(c, *a, *b) <= r2) { + *m = s.add; + } + } + } + assert_eq!(mask, want); + assert!(mask[5 * w + 10] && !mask[5 * w + 20], "added along the path, cleared under the subtracting dab"); + // A mask shorter or longer than the area is not touched (and nothing panics). + for len in [0, 7, w * h - 1, w * h + 3] { + let mut short = vec![false; len]; + paint_strokes(&mut short, area, &strokes); + assert!(short.iter().all(|v| !v), "len {len}"); + } + // Hostile coordinates stay inside the area. + let mut m = vec![false; w * h]; + let wild = [SamplingStroke { add: true, size: 5000.0, points: vec![[f64::MAX, -1e300], [f64::NAN, 1e7]] }]; + paint_strokes(&mut m, area, &wild); +} + +#[test] +fn cancelled_fill_stops_and_progress_reaches_one() { + use std::sync::atomic::{AtomicU32, Ordering}; + let (w, h) = (60, 40); + let (img, hole) = stripes(w, h); + let source = vec![true; w * h]; + let yes = || true; + let cancelled = photocraft_raster::Interrupt::cancel_only(&yes); + assert!(fill_with(w, h, 1, &img, &hole, &source, &FillOptions::default(), &cancelled).is_err()); + let last = AtomicU32::new(0); + let no = || false; + let progress = |f: f32| { + assert!(f >= f32::from_bits(last.load(Ordering::Relaxed)), "progress went backwards"); + last.store(f.to_bits(), Ordering::Relaxed); + }; + let out = fill_with(w, h, 1, &img, &hole, &source, &FillOptions::default(), &photocraft_raster::Interrupt::new(&no, &progress)).unwrap(); + assert_eq!(f32::from_bits(last.load(Ordering::Relaxed)), 1.0); + assert_eq!(out, fill(w, h, 1, &img, &hole, &source, &FillOptions::default()), "same result as the plain fill"); +} + +#[test] +fn filling_in_place_is_filling_a_copy() { + let (w, h) = (90, 70); + let rgb: Vec = noise(w, h, 4).iter().enumerate().map(|(i, v)| v * 0.5 + 0.3 * ((i / 3 % w) % 11 < 5) as u8 as f32).collect(); + let hole: Vec = (0..w * h).map(|i| (30..52).contains(&(i % w)) && (25..44).contains(&(i / w))).collect(); + let source: Vec = (0..w * h).map(|i| i % w < 75).collect(); + let none = Interrupt::NONE; + for opts in [FillOptions::default(), FillOptions { color_adaptation: Adapt::NONE, mirror: true, ..Default::default() }] { + let copy = fill_with(w, h, 3, &rgb, &hole, &source, &opts, &none).unwrap(); + let owned = fill_owned(w, h, 3, rgb.clone(), &hole, &source, &opts, &none).unwrap(); + assert_eq!(copy.iter().map(|v| v.to_bits()).collect::>(), owned.iter().map(|v| v.to_bits()).collect::>()); + // Only the hole changes. + assert!((0..w * h).all(|i| hole[i] || (0..3).all(|c| owned[i * 3 + c].to_bits() == rgb[i * 3 + c].to_bits()))); + } + // Nothing to copy from: the membrane, either way. + let nowhere = vec![false; w * h]; + let copy = fill_with(w, h, 3, &rgb, &hole, &nowhere, &FillOptions::default(), &none).unwrap(); + assert_eq!(copy, fill_owned(w, h, 3, rgb.clone(), &hole, &nowhere, &FillOptions::default(), &none).unwrap()); +} + +#[test] +fn auto_sampling_can_be_cancelled() { + let (w, h) = (200, 160); + let img: Vec = noise(w, h, 5).iter().enumerate().map(|(i, v)| v * 0.1 + if (i / 3) % w < 120 { 0.3 } else { 0.6 }).collect(); + let hole: Vec = (0..w * h).map(|i| (80..110).contains(&(i % w)) && (60..90).contains(&(i / w))).collect(); + let yes = || true; + assert!(auto_sampling_with(w, h, 3, &img, &hole, &Interrupt::cancel_only(&yes)).is_err()); + assert_eq!(auto_sampling_with(w, h, 3, &img, &hole, &Interrupt::NONE).unwrap(), auto_sampling(w, h, 3, &img, &hole)); +} + +#[test] +fn fills_only_the_hole_with_known_texture() { + let (w, h) = (48, 32); + let (img, hole) = stripes(w, h); + let src = vec![true; w * h]; + let out = fill(w, h, 1, &img, &hole, &src, &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); + for i in 0..w * h { + if !hole[i] { + assert_eq!(out[i], img[i]); + } + } + // The hole is filled with stripe values, not a flat average. + let vals: Vec = (0..w * h).filter(|i| hole[*i]).map(|i| out[i]).collect(); + assert!(vals.iter().any(|v| *v < 0.35) && vals.iter().any(|v| *v > 0.65), "{vals:?}"); +} + +#[test] +fn sampling_area_restricts_sources() { + // Left half dark, right half bright; sampling only the right half fills bright. + let (w, h) = (40, 20); + let img: Vec = (0..w * h).map(|i| if i % w < 20 { 0.1 } else { 0.9 }).collect(); + let hole: Vec = (0..w * h).map(|i| (16..24).contains(&(i % w)) && (6..14).contains(&(i / w))).collect(); + let right: Vec = (0..w * h).map(|i| i % w >= 24).collect(); + let out = fill(w, h, 1, &img, &hole, &right, &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); + let mean: f32 = (0..w * h).filter(|i| hole[*i]).map(|i| out[i]).sum::() / 64.0; + assert!(mean > 0.8, "mean {mean}"); +} + +#[test] +fn adaptations_run_and_stay_deterministic() { + let (w, h) = (32, 24); + let (img, hole) = stripes(w, h); + let rgb: Vec = img.iter().flat_map(|v| [*v, *v * 0.5, 1.0 - *v]).collect(); + let src = vec![true; w * h]; + let opts = FillOptions { color_adaptation: color_level("high"), rotations: rotation_level("low"), scale: true, mirror: true, seed: 7 }; + let a = fill(w, h, 3, &rgb, &hole, &src, &opts); + let b = fill(w, h, 3, &rgb, &hole, &src, &opts); + assert_eq!(a, b); + assert!(a.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v))); +} + +#[test] +fn color_adaptation_follows_a_gradient() { + // A horizontal brightness ramp with a hole: adaptation keeps the fill near the ramp. + let (w, h) = (40, 20); + let img: Vec = (0..w * h).map(|i| (i % w) as f32 / w as f32).collect(); + let hole: Vec = (0..w * h).map(|i| (14..26).contains(&(i % w)) && (5..15).contains(&(i / w))).collect(); + let src = vec![true; w * h]; + let out = fill(w, h, 1, &img, &hole, &src, &FillOptions { color_adaptation: color_level("veryHigh"), ..Default::default() }); + let err: f32 = (0..w * h).filter(|i| hole[*i]).map(|i| (out[i] - img[i]).abs()).sum::() / 120.0; + assert!(err < 0.12, "mean error {err}"); +} + +/// A vertical sky gradient (each row one colour) filled at every adaptation level stays the +/// gradient: no tinted blotch where the copied rows' colours were shifted per channel. +#[test] +fn color_adaptation_leaves_a_smooth_gradient_untinted() { + let (w, h) = (120, 60); + let row = |y: usize| { + let t = y as f32 / (h - 1) as f32; + [0.27 + 0.45 * t, 0.53 + 0.32 * t, 0.86 + 0.1 * t] + }; + let img: Vec = (0..w * h).flat_map(|i| row(i / w)).collect(); + let hole: Vec = (0..w * h).map(|i| (50..70).contains(&(i % w)) && (20..40).contains(&(i / w))).collect(); + let src = vec![true; w * h]; + for level in ["none", "default", "high", "veryHigh"] { + let out = fill(w, h, 3, &img, &hole, &src, &FillOptions { color_adaptation: color_level(level), ..Default::default() }); + let (out, img) = (&out, &img); + let worst = (0..w * h).filter(|i| hole[*i]).flat_map(|i| (0..3).map(move |c| (out[i * 3 + c] - img[i * 3 + c]).abs())).fold(0.0f32, f32::max); + assert!(worst < 3.0 / 255.0, "{level}: off by {}", worst * 255.0); + } +} + +/// I.i.d. noise: every value occurs once, so a filled pixel equal to an image pixel was copied. +fn noise(w: usize, h: usize, seed: u64) -> Vec { + let mut z = seed; + (0..w * h * 3) + .map(|_| { + z = z.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); + 0.2 + 0.6 * ((z >> 40) as f32 / (1u64 << 24) as f32) + }) + .collect() +} + +#[test] +fn sampling_window_is_photoshops() { + let canvas = Rect::new(0, 0, 800, 800); + let sq = |x0: i32, y0: i32, w: i32, h: i32| Rect::new(x0, y0, x0 + w, y0 + h); + // Photoshop 25.4's sampling overlay: a square of side 4·√(max(w,50)·max(h,50)) centred on + // the selection's bounds. + assert_eq!(sampling_window(sq(375, 375, 50, 50), canvas), sq(300, 300, 200, 200)); + assert_eq!(sampling_window(sq(350, 350, 100, 100), canvas), sq(200, 200, 400, 400)); + assert_eq!(sampling_window(sq(395, 395, 10, 10), canvas), sq(300, 300, 200, 200)); + assert_eq!(sampling_window(sq(370, 370, 60, 60), canvas), sq(280, 280, 240, 240)); + // Long selections (Photoshop's overlay, read off the screen to a pixel: 281, 399, 691). + assert_eq!(sampling_window(sq(350, 385, 100, 30), canvas), sq(258, 258, 283, 283)); + assert_eq!(sampling_window(sq(300, 375, 200, 50), canvas), sq(200, 200, 400, 400)); + assert_eq!(sampling_window(sq(250, 350, 300, 100), canvas), sq(53, 53, 693, 693)); + // At the canvas edge the square slides inwards rather than being cut. + assert_eq!(sampling_window(sq(10, 375, 50, 50), canvas), sq(0, 300, 200, 200)); + assert_eq!(sampling_window(sq(5, 5, 60, 60), canvas), sq(0, 0, 240, 240)); + // A canvas smaller than the square clips it; a very long selection keeps its length. + assert_eq!(sampling_window(sq(40, 40, 50, 50), Rect::new(0, 0, 150, 120)), Rect::new(0, 0, 150, 120)); + assert_eq!(sampling_window(sq(100, 995, 1800, 10), Rect::new(0, 0, 2000, 2000)), sq(100, 400, 1800, 1200)); + assert_eq!(sampling_window(Rect::EMPTY, canvas), Rect::EMPTY); +} + +#[test] +fn auto_sampling_keeps_what_looks_like_the_surroundings() { + // Grey texture, dark on the left, 50 levels brighter on the right; the hole is in the dark + // part near the boundary (Photoshop's Auto leaves the bright part out). + let (w, h) = (200, 200); + let tex = noise(w, h, 3); + let shade = |x: usize, band: bool| { + if band { + if (130..150).contains(&x) { 0.55 } else { 0.35 } + } else if x < 135 { + 0.35 + } else { + 0.55 + } + }; + for band in [false, true] { + let img: Vec = (0..w * h * 3).map(|i| shade((i / 3) % w, band) + (tex[i] - 0.5) * 0.1).collect(); + let hole: Vec = (0..w * h).map(|i| (75..125).contains(&(i % w)) && (75..125).contains(&(i / w))).collect(); + let area = auto_sampling(w, h, 3, &img, &hole); + let frac = |x0: usize, x1: usize| { + let n = (0..w * h).filter(|i| (x0..x1).contains(&(i % w)) && !hole[*i]).count(); + (0..w * h).filter(|i| (x0..x1).contains(&(i % w)) && area[*i]).count() as f32 / n as f32 + }; + assert!(hole.iter().zip(&area).all(|(hl, a)| !(*hl && *a)), "never the hole"); + assert!(frac(0, 125) > 0.95, "the dark side is sampled ({})", frac(0, 125)); + if band { + // A bright band through the window is left out; the dark area beyond it is not. + assert!(frac(133, 147) < 0.05, "the band is excluded ({})", frac(133, 147)); + assert!(frac(155, 200) > 0.9, "beyond the band is sampled ({})", frac(155, 200)); + } else { + assert!(frac(140, 200) < 0.05, "the bright side is excluded ({})", frac(140, 200)); + } + } +} + +#[test] +fn auto_sampling_keeps_the_surroundings_of_every_part_of_a_thin_selection() { + // A long thin selection across a grey ramp, and a small knob selected with it in a red + // corner. Each part keeps its own surroundings; a compact selection is unchanged. + let (w, h) = (900, 600); + let tex = noise(w, h, 4); + let img: Vec = (0..w * h * 3) + .map(|i| { + let (x, y, c) = ((i / 3) % w, (i / 3) / w, i % 3); + let base = if x > 690 && y > 420 { [0.7, 0.25, 0.2][c] } else { 0.2 + 0.5 * x as f32 / w as f32 }; + base + (tex[i] - 0.5) * 0.04 + }) + .collect(); + let line = |i: usize| (30..870).contains(&(i % w)) && (180..192).contains(&(i / w)); + let knob = |i: usize| ((i % w) as f32 - 795.0).hypot((i / w) as f32 - 510.0) < 10.0; + let hole: Vec = (0..w * h).map(|i| line(i) || knob(i)).collect(); + let area = auto_sampling(w, h, 3, &img, &hole); + let around_knob = (0..w * h).filter(|&i| !hole[i] && ((i % w) as f32 - 795.0).hypot((i / w) as f32 - 510.0) < 24.0); + let (n, kept) = around_knob.fold((0, 0), |(n, k), i| (n + 1, k + usize::from(area[i]))); + assert!(kept as f32 > 0.9 * n as f32, "{kept} of {n} pixels around the knob"); + assert!(thin_band(w, h, &hole) >= 20.0, "{}", thin_band(w, h, &hole)); + let disc: Vec = (0..w * h).map(|i| ((i % w) as f32 - 450.0).hypot((i / w) as f32 - 300.0) < 90.0).collect(); + assert_eq!(thin_band(w, h, &disc), 1.5); +} + +#[test] +fn without_colour_adaptation_the_fill_copies_pixels_exactly() { + // A smooth texture with fine noise, so that every value is unique: a filled pixel equal to + // an image pixel was copied from it. Photoshop's fill (colour adaptation None) is such + // copies, joined by narrow seams. + let (w, h) = (120, 120); + let fine = noise(w, h, 9); + let img: Vec = (0..w * h * 3) + .map(|i| { + let (x, y, c) = (((i / 3) % w) as f32, ((i / 3) / w) as f32, i % 3); + 0.5 + 0.2 * (x * 0.21 + c as f32).sin() * (y * 0.17).cos() + 0.1 * (x * 0.05 - y * 0.08).sin() + (fine[i] - 0.5) * 0.08 + }) + .collect(); + let hole: Vec = (0..w * h).map(|i| (40..80).contains(&(i % w)) && (40..80).contains(&(i / w))).collect(); + // Votes of identical copies average to the copy up to float rounding. + let key = |v: &[f32], i: usize| [0, 1, 2].map(|c| (v[i * 3 + c] * 1e5).round() as i64); + let known: std::collections::HashSet<[i64; 3]> = (0..w * h).filter(|i| !hole[*i]).map(|i| key(&img, i)).collect(); + let out = fill(w, h, 3, &img, &hole, &vec![true; w * h], &FillOptions { color_adaptation: Adapt::NONE, ..Default::default() }); + let exact = (0..w * h).filter(|i| hole[*i] && known.contains(&key(&out, *i))).count(); + assert!(exact as f32 > 0.2 * 1600.0, "{exact} of 1600 filled pixels are exact copies"); + // And the fill is as busy as the noise around it, not a blur of it. + let lap = |v: &[f32], x: usize, y: usize| { + let p = |x: usize, y: usize| v[(y * w + x) * 3]; + (4.0 * p(x, y) - p(x - 1, y) - p(x + 1, y) - p(x, y - 1) - p(x, y + 1)).abs() + }; + let mean = |inside: bool| { + let pts: Vec<(usize, usize)> = (1..h - 1).flat_map(|y| (1..w - 1).map(move |x| (x, y))).filter(|&(x, y)| hole[y * w + x] == inside).collect(); + pts.iter().map(|&(x, y)| lap(&out, x, y)).sum::() / pts.len() as f32 + }; + assert!(mean(true) > 0.6 * mean(false), "fill detail {} against {}", mean(true), mean(false)); +} + +#[test] +fn patch_color_shifts_up_to_about_four_hundredths_per_step() { + // Content 0.4 brighter than where it lands (Photoshop's Patch, Color 3 and 10). + let from: Vec = (0..300).map(|i| 0.7 + 0.05 * ((i % 7) as f32 / 7.0)).collect(); + let to: Vec = from.iter().map(|v| v - 0.4).collect(); + let level = from.iter().sum::() / 300.0; + let shift = |c: u8| { + let gb = level_transform(1, &from, &to, color_amount(c)); + level - from.iter().map(|v| gb[0].0 * v + gb[0].1).sum::() / 300.0 + }; + assert_eq!(shift(0), 0.0); + assert!((shift(3) - 0.12).abs() < 0.01, "{}", shift(3)); + assert!((shift(10) - 0.4).abs() < 0.01, "{}", shift(10)); +} + +/// A soft background (gradients, broad out-of-focus blobs of colour, fine noise) with thin, long bright +/// objects over it: three coiled strings 3 to 7 px wide and two straighter ones, each with an +/// anti-aliased edge and a faint halo. Returns the image; what removing the coils should give +/// (the background and the straighter strings, which stay); and the selection a user would draw +/// around the coils, `margin` px wide. +fn strings_scene(w: usize, h: usize, margin: f32) -> (Vec, Vec, Vec) { + let fine = noise(w, h, 11); + // Out-of-focus colour: broad blobs over two gradients. + let blob = noise(3, 3, 5); + let bg: Vec = (0..w * h * 3) + .map(|i| { + let (x, y, c) = (((i / 3) % w) as f32 / w as f32, ((i / 3) / w) as f32 / h as f32, i % 3); + let mut v = [0.18 + 0.25 * x, 0.22 + 0.1 * y, 0.3 - 0.1 * x][c]; + for b in blob.as_chunks::<3>().0 { + let (bx, by, s) = (b[0] * 1.6 - 0.3, b[1] * 1.6 - 0.3, 0.1 + 0.25 * b[2]); + v += (b[(c + 1) % 3] - 0.2) * 0.5 * (-((x - bx).powi(2) + (y - by).powi(2) * 0.56) / (2.0 * s * s)).exp(); + } + v + (fine[i] - 0.5) * 0.03 + }) + .collect(); + // Signed distance to each object's surface, stamped from points along it. + let stamp = |sd: &mut [f32], pts: &mut dyn Iterator, half: f32| { + for (px, py) in pts { + let reach = (half + 4.0) as i32 + 1; + for dy in -reach..=reach { + for dx in -reach..=reach { + let (x, y) = (px as i32 + dx, py as i32 + dy); + if x >= 0 && y >= 0 && (x as usize) < w && (y as usize) < h { + let i = y as usize * w + x as usize; + sd[i] = sd[i].min((x as f32 - px).hypot(y as f32 - py) - half); + } + } + } + } + }; + let mut coils = vec![f32::INFINITY; w * h]; + for (k, half) in [1.5f32, 2.5, 3.5].into_iter().enumerate() { + let (y0, r) = ((0.22 + 0.28 * k as f32) * h as f32, 10.0 + 2.0 * k as f32); + let mut pts = (0..4000).map(|j| { + let t = j as f32 / 4000.0; + let a = 8.0 * std::f32::consts::TAU * t; + (18.0 + t * (w as f32 - 60.0) + r * a.cos(), y0 + 6.0 * (6.0 * t).sin() + 0.7 * r * a.sin()) + }); + stamp(&mut coils, &mut pts, half); + } + let mut straight = vec![f32::INFINITY; w * h]; + for y in [0.07, 0.93] { + stamp(&mut straight, &mut (0..2000).map(|j| (j as f32 / 2000.0 * w as f32, y * h as f32 + 3.0 * (j as f32 / 300.0).sin())), 2.0); + } + let draw = |base: &[f32], sd: &[f32]| -> Vec { + let mut img = base.to_vec(); + for i in 0..w * h { + let cover = (0.5 - sd[i]).clamp(0.0, 1.0); + let halo = if sd[i] > 0.0 { 0.08 * (-sd[i] * sd[i] / 2.9).exp() } else { 0.0 }; + for (c, metal) in [0.9f32, 0.85, 0.7].into_iter().enumerate() { + let v = &mut img[i * 3 + c]; + *v = *v * (1.0 - cover) + metal * cover + halo * (1.0 - cover); + } + } + img + }; + let clean = draw(&bg, &straight); + let both: Vec = coils.iter().zip(&straight).map(|(a, b)| a.min(*b)).collect(); + let img = draw(&bg, &both); + let hole = coils.iter().map(|d| *d <= margin).collect(); + (img, clean, hole) +} + +/// RMS over the hole of the error averaged over 7 × 7 pixels (in levels of 255): the noise +/// cancels out, a ghost of the removed outline or a blotch of another shade does not. +fn blotch_error(w: usize, h: usize, out: &[f32], clean: &[f32], hole: &[bool]) -> f32 { + let (mut sum, mut n) = (0.0f64, 0usize); + for y in 3..h - 3 { + for x in 3..w - 3 { + if !hole[y * w + x] { + continue; + } + let mut e = [0.0f32; 3]; + for j in (y - 3..=y + 3).flat_map(|yy| (x - 3..=x + 3).map(move |xx| yy * w + xx)) { + for (c, ec) in e.iter_mut().enumerate() { + *ec += out[j * 3 + c] - clean[j * 3 + c]; + } + } + sum += f64::from(e.iter().map(|v| (v / 49.0).powi(2)).sum::() / 3.0); + n += 1; + } + } + ((sum / n.max(1) as f64).sqrt() * 255.0) as f32 +} + +/// Removing thin, long objects (guitar strings, wires) from a soft background (#1366): the +/// fill is the background, with no ghost of their outline and nothing of their colour copied +/// back in. Their selection is long but only a few pixels thick, so the fill is worked out at +/// a scale where it is still a few pixels thick rather than one where the strings' surroundings +/// have merged into a blur. +#[test] +fn thin_objects_leave_no_ghost_and_none_of_their_colour() { + let (w, h) = (360, 270); + let (img, clean, hole) = strings_scene(w, h, 2.0); + let source = auto_sampling(w, h, 3, &img, &hole); + let out = fill(w, h, 3, &img, &hole, &source, &FillOptions::default()); + let blotch = blotch_error(w, h, &out, &clean, &hole); + let luma = |v: &[f32], i: usize| (v[i * 3] + v[i * 3 + 1] + v[i * 3 + 2]) / 3.0; + let bright = (0..w * h).filter(|&i| hole[i] && luma(&out, i) > luma(&clean, i) + 0.06).count(); + // Before #1366 the outlines stayed as darker and lighter blotches (13 levels of 255) and + // 1121 pixels took the strings' colour. + assert!(blotch < 2.5, "ghosts and blotches: {blotch:.2} levels of 255"); + assert_eq!(bright, 0, "filled pixels with the strings' colour"); +} + +#[test] +fn variant_geometry() { + let img = vec![0.5f32; 10 * 6]; + let ok = vec![true; 60]; + let src = Src { w: 10, h: 6, ch: 1, img: &img, ok: &ok }; + let v = variant(src, 90.0, 1.0, false, &Interrupt::NONE).unwrap(); + assert_eq!((v.w, v.h), (6, 10)); + let v = variant(src, 0.0, 0.8, true, &Interrupt::NONE).unwrap(); + assert_eq!((v.w, v.h), (8, 5)); + assert!(v.ok.iter().filter(|o| **o).count() > 20); +} diff --git a/crates/algo/src/cutout/colour.rs b/crates/algo/src/cutout/colour.rs new file mode 100644 index 0000000000..75ddeed2e8 --- /dev/null +++ b/crates/algo/src/cutout/colour.rs @@ -0,0 +1,263 @@ +//! Colour and palette: Oklab, edge-keeping smoothing, and the picture's own palette. + +use super::Px; +use super::util::transpose; +use crate::par; +use crate::quantize::{kmeans, median_cut}; + +/// Oklab (Björn Ottosson, 2020) of an sRGB colour. +pub(super) fn oklab(p: Px) -> Px { + let lin = |v: f32| { + let v = v.clamp(0.0, 1.0); + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } + }; + let (r, g, b) = (lin(p[0]), lin(p[1]), lin(p[2])); + let l = (0.412_221_46 * r + 0.536_332_55 * g + 0.051_445_995 * b).cbrt(); + let m = (0.211_903_5 * r + 0.680_699_5 * g + 0.107_396_96 * b).cbrt(); + let s = (0.088_302_46 * r + 0.281_718_85 * g + 0.629_978_7 * b).cbrt(); + [ + 0.210_454_26 * l + 0.793_617_8 * m - 0.004_072_047 * s, + 1.977_998_5 * l - 2.428_592_2 * m + 0.450_593_7 * s, + 0.025_904_037 * l + 0.782_771_77 * m - 0.808_675_77 * s, + ] +} + +/// sRGB of an Oklab colour, or `None` outside the sRGB gamut. +pub(super) fn srgb(lab: Px) -> Option { + let l = (lab[0] + 0.396_337_78 * lab[1] + 0.215_803_76 * lab[2]).powi(3); + let m = (lab[0] - 0.105_561_346 * lab[1] - 0.063_854_17 * lab[2]).powi(3); + let s = (lab[0] - 0.089_484_18 * lab[1] - 1.291_485_5 * lab[2]).powi(3); + let lin = [ + 4.076_741_7 * l - 3.307_711_6 * m + 0.230_969_94 * s, + -1.268_438 * l + 2.609_757_4 * m - 0.341_319_38 * s, + -0.004_196_086_3 * l - 0.703_418_6 * m + 1.707_614_7 * s, + ]; + if lin.iter().any(|v| !(-1e-4..=1.0001).contains(v)) { + return None; + } + let enc = |v: f32| { + let v = v.clamp(0.0, 1.0); + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } + }; + Some(lin.map(enc)) +} + +/// sRGB at Oklab lightness `l`, hue/strength `a`, `b`; out of gamut it loses strength, not hue. +pub(super) fn in_gamut(l: f32, a: f32, b: f32) -> Px { + if let Some(c) = srgb([l, a, b]) { + return c; + } + let (mut ok, mut bad) = (0.0f32, 1.0f32); + for _ in 0..10 { + let t = (ok + bad) / 2.0; + if srgb([l, a * t, b * t]).is_some() { ok = t } else { bad = t } + } + srgb([l, a * ok, b * ok]).unwrap_or([l, l, l]) +} + +/// Squared distance between two colours. +pub(super) fn dist2(a: Px, b: Px) -> f32 { + (a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2) + (a[2] - b[2]).powi(2) +} + +/// Bilateral smoothing in Oklab, `rounds` times along rows then columns. +pub(super) fn smooth(mut lab: Vec, w: usize, h: usize, rounds: usize) -> Vec { + for _ in 0..rounds { + bilateral_rows(&mut lab, w); + let mut t = transpose(&lab, w, h); + bilateral_rows(&mut t, h); + lab = transpose(&t, h, w); + } + lab +} + +/// One bilateral pass along every row of `buf` (rows of `w` pixels), seven pixels wide. +fn bilateral_rows(buf: &mut [Px], w: usize) { + const R: isize = 3; + // Colours this far apart (Oklab) count half as much as equal ones (a Cauchy weight: as + // edge-keeping as a Gaussian one and much cheaper). + const HALF: f32 = 0.06; + let k = 1.0 / (HALF * HALF); + let spatial: [f32; 7] = std::array::from_fn(|i| (-((i as f32 - 3.0).powi(2)) / 8.0).exp()); + par::chunks_mut(buf, w, true, |_, row| { + let src = row.to_vec(); + for (x, out) in row.iter_mut().enumerate() { + let c = src[x]; + let (mut acc, mut wsum) = ([0.0f32; 3], 0.0f32); + for d in -R..=R { + let q = src[(x as isize + d).clamp(0, w as isize - 1) as usize]; + let wt = spatial[(d + R) as usize] / (1.0 + dist2(q, c) * k); + acc = [acc[0] + q[0] * wt, acc[1] + q[1] * wt, acc[2] + q[2] * wt]; + wsum += wt; + } + *out = [acc[0] / wsum, acc[1] / wsum, acc[2] / wsum]; + } + }); +} + +/// At most `levels` colours of the image's own; colours closer than noise are merged. +pub(super) fn palette(lab: &[Px], levels: usize) -> Vec { + const SAMPLES: usize = 1 << 16; + const SAME: f32 = 0.045; + // Hue centres (Oklab a, b) this far apart are different colours, not one colour's spread. + const HUES_APART: f32 = 0.08; + let step = lab.len().div_ceil(SAMPLES).max(1); + let pts: Vec<(Px, u32)> = lab.iter().step_by(step).map(|p| (*p, 1)).collect(); + let mut centres = median_cut(&pts, levels); + kmeans(&pts, &mut centres, 10); + // Number of Levels counts tones: a palette colour whose pixels are two clearly different hues + // of that tone (a blue and a pink) splits in two, or touching areas of both would be one + // shape of their grey average. + let mut split = Vec::with_capacity(centres.len() * 2); + for (k, c) in centres.iter().enumerate() { + let members: Vec<(Px, u32)> = pts.iter().filter(|(p, _)| nearest(¢res, *p) as usize == k).map(|(p, _)| ([0.0, p[1], p[2]], 1)).collect(); + let mut hues = median_cut(&members, 2); + kmeans(&members, &mut hues, 6); + match hues.as_slice() { + [a, b] if dist2(*a, *b).sqrt() > HUES_APART => split.extend([[c[0], a[1], a[2]], [c[0], b[1], b[2]]]), + _ => split.push(*c), + } + } + let mut centres = split; + kmeans(&pts, &mut centres, 4); + let mut weight = vec![0u32; centres.len()]; + for (p, _) in &pts { + if let Some(wt) = weight.get_mut(nearest(¢res, *p) as usize) { + *wt += 1; + } + } + let mut cs: Vec<(Px, f32)> = centres.into_iter().zip(weight).filter(|c| c.1 > 0).map(|(c, wt)| (c, wt as f32)).collect(); + loop { + let mut best: Option<(usize, usize, f32)> = None; + for i in 0..cs.len() { + for j in i + 1..cs.len() { + let d = dist2(cs[i].0, cs[j].0).sqrt(); + if d < SAME && best.is_none_or(|b| d < b.2) { + best = Some((i, j, d)); + } + } + } + let Some((i, j, _)) = best else { break }; + let (a, b) = (cs[i], cs[j]); + let t = a.1 + b.1; + cs[i] = (std::array::from_fn(|k| (a.0[k] * a.1 + b.0[k] * b.1) / t), t); + cs.remove(j); + } + if cs.is_empty() { + return vec![[0.5, 0.0, 0.0]]; + } + cs.into_iter().map(|c| c.0).collect() +} + +/// Index of the palette colour closest to `p`. +pub(super) fn nearest(palette: &[Px], p: Px) -> u8 { + let mut best = (0usize, f32::MAX); + for (i, c) in palette.iter().enumerate() { + let d = dist2(*c, p); + if d < best.1 { + best = (i, d); + } + } + best.0 as u8 +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::photo_util::hash01; + + /// Oklab pixels of `w`×`h`: `colour(x)` plus noise of ±`noise` in each channel. + fn noisy(w: usize, h: usize, noise: f32, colour: impl Fn(usize) -> Px) -> Vec { + (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as i64, (i / w) as i64); + let c = colour(i % w); + std::array::from_fn(|k| c[k] + (hash01(x, y, k as u64 + 1) - 0.5) * 2.0 * noise) + }) + .collect() + } + + #[test] + fn oklab_round_trips_and_marks_the_gamut() { + let white = oklab([1.0, 1.0, 1.0]); + assert!((white[0] - 1.0).abs() < 1e-3 && white[1].abs() < 1e-3 && white[2].abs() < 1e-3, "{white:?}"); + assert!(oklab([0.0, 0.0, 0.0])[0].abs() < 1e-6); + for p in [[0.2, 0.5, 0.9], [0.95, 0.1, 0.3], [0.5, 0.5, 0.5], [0.0, 1.0, 0.0]] { + let back = srgb(oklab(p)).unwrap_or([-1.0; 3]); + assert!(dist2(back, p) < 1e-6, "{p:?} came back as {back:?}"); + } + // Very light and very colourful: no sRGB colour is that. + assert_eq!(srgb([0.95, 0.3, 0.0]), None); + } + + #[test] + fn out_of_gamut_colours_lose_strength_not_hue() { + let (l, a, b) = (0.95, 0.3, 0.1); + let c = in_gamut(l, a, b); + let lab = oklab(c); + assert!((lab[0] - l).abs() < 0.01, "lightness kept: {lab:?}"); + let chroma = (lab[1] * lab[1] + lab[2] * lab[2]).sqrt(); + assert!(chroma > 0.01 && chroma < (a * a + b * b).sqrt(), "weaker but still coloured: {chroma}"); + assert!((lab[2] / lab[1] - b / a).abs() < 0.05, "same hue: {lab:?}"); + // In gamut it is just the conversion. + assert_eq!(in_gamut(0.6, 0.02, -0.03), srgb([0.6, 0.02, -0.03]).unwrap_or_default()); + } + + #[test] + fn smoothing_flattens_noise_and_keeps_edges() { + let (w, h) = (40usize, 20usize); + let lab = noisy(w, h, 0.02, |x| if x < 20 { [0.3, 0.0, 0.0] } else { [0.8, 0.0, 0.0] }); + let out = smooth(lab.clone(), w, h, 2); + let spread = |v: &[Px], xs: std::ops::Range| { + let l: Vec = (0..h).flat_map(|y| xs.clone().map(move |x| y * w + x)).map(|i| v[i][0]).collect(); + let mean = l.iter().sum::() / l.len() as f32; + (l.iter().map(|v| (v - mean).powi(2)).sum::() / l.len() as f32).sqrt() + }; + assert!(spread(&out, 2..16) < spread(&lab, 2..16) / 2.0, "noise kept: {} vs {}", spread(&out, 2..16), spread(&lab, 2..16)); + // The step stays a step: the pixels either side of it keep most of their difference. + let step = (0..h).map(|y| out[y * w + 20][0] - out[y * w + 19][0]).sum::() / h as f32; + assert!(step > 0.4, "edge blurred to {step}"); + } + + #[test] + fn the_palette_finds_the_pictures_own_colours() { + let (w, h) = (60usize, 20usize); + let (dark, light) = ([0.25, 0.02, -0.03], [0.75, -0.04, 0.05]); + let lab = noisy(w, h, 0.01, |x| if x < 30 { dark } else { light }); + // Asked for up to six, it gives two: colours only noise apart are one. + let p = palette(&lab, 6); + assert_eq!(p.len(), 2, "{p:?}"); + for c in [dark, light] { + assert!(p.iter().any(|q| dist2(*q, c).sqrt() < 0.02), "{c:?} not in {p:?}"); + } + assert_eq!(nearest(&p, dark), nearest(&p, [0.27, 0.0, -0.02])); + assert_ne!(nearest(&p, dark), nearest(&p, light)); + } + + #[test] + fn a_tone_of_two_different_hues_splits_in_two() { + // A dark tone and a light one, the light one half blue and half pink: two levels give + // three colours, or the blue and pink areas would be one shape of their grey average. + let (w, h) = (60usize, 20usize); + let (dark, blue, pink) = ([0.2, 0.0, 0.0], [0.7, -0.03, -0.1], [0.7, 0.12, 0.0]); + let lab = noisy(w, h, 0.005, |x| { + if x < 30 { + dark + } else if x < 45 { + blue + } else { + pink + } + }); + let p = palette(&lab, 2); + assert_eq!(p.len(), 3, "{p:?}"); + for c in [dark, blue, pink] { + assert!(p.iter().any(|q| dist2(*q, c).sqrt() < 0.03), "{c:?} not in {p:?}"); + } + } + + #[test] + fn an_empty_image_still_has_a_palette() { + assert_eq!(palette(&[], 4).len(), 1); + } +} diff --git a/crates/algo/src/cutout/curves.rs b/crates/algo/src/cutout/curves.rs new file mode 100644 index 0000000000..9d7e90a44e --- /dev/null +++ b/crates/algo/src/cutout/curves.rs @@ -0,0 +1,380 @@ +//! Curves: each chain fitted once, with its corners kept, its runs simplified (Douglas–Peucker) +//! and drawn as Catmull-Rom curves. + +use super::{Pt, Settings}; + +/// `a - b`. +fn sub(a: Pt, b: Pt) -> Pt { + [a[0] - b[0], a[1] - b[1]] +} + +/// Length of `a`. +fn len(a: Pt) -> f32 { + (a[0] * a[0] + a[1] * a[1]).sqrt() +} + +/// Fits one chain: corners kept, runs simplified and smoothed. Its ends stay where they are, so +/// chains meeting at a junction still meet. +pub(super) fn fit(pts: &[Pt], closed: bool, s: &Settings) -> Vec { + let n = pts.len(); + if n < 3 { + return pts.to_vec(); + } + let original = pts; + let rounded = round_off(pts, closed, s.rounding()); + let pts = &rounded[..]; + let at = |i: isize| pts[i.rem_euclid(n as isize) as usize]; + // Turn at each point, measured over a few cracks either side. + const K: isize = 3; + let turn = |i: isize| { + let (a, b) = (sub(at(i), at(i - K)), sub(at(i + K), at(i))); + let (la, lb) = (len(a), len(b)); + if la < 1e-6 || lb < 1e-6 { 0.0 } else { ((a[0] * b[0] + a[1] * b[1]) / (la * lb)).clamp(-1.0, 1.0).acos() } + }; + let span = if closed { 0..n as isize } else { K..(n as isize - K) }; + let turns: Vec<(isize, f32)> = span.clone().map(|i| (i, turn(i))).collect(); + let limit = s.corner_turn(); + let mut corners: Vec = Vec::new(); + for &(i, t) in &turns { + if t <= limit { + continue; + } + // Only the sharpest point of a bend. + let sharpest = (-K..=K).all(|o| { + let j = i + o; + let in_span = closed || span.contains(&j); + !in_span || o == 0 || { + let tj = turn(j); + tj < t || (tj == t && o > 0) + } + }); + if sharpest { + corners.push(i.rem_euclid(n as isize) as usize); + } + } + // Square a corner back where the pixels cut it: where the straight runs either side meet. + let mut p = pts.to_vec(); + for &i in &corners { + let ii = i as isize; + if let Some(q) = intersect(at(ii - K), at(ii - 1), at(ii + 1), at(ii + K)) + && len(sub(q, at(ii))) <= 1.0 + { + p[i] = q; + } + } + let mut keep: Vec = if closed { corners.clone() } else { std::iter::once(0).chain(corners.iter().copied()).chain(std::iter::once(n - 1)).collect() }; + keep.sort_unstable(); + keep.dedup(); + let mut is_corner = vec![false; n]; + for &k in &keep { + is_corner[k] = true; + } + if closed && keep.len() < 2 { + // A smooth loop: split it at two far-apart points (not corners) to simplify each half. + let a = keep.first().copied().unwrap_or(0); + let far = (0..n).max_by(|&i, &j| len(sub(p[i], p[a])).total_cmp(&len(sub(p[j], p[a])))).unwrap_or(n / 2); + keep = if a == far { vec![a, (a + n / 2) % n] } else { vec![a.min(far), a.max(far)] }; + } + // Douglas–Peucker between consecutive kept points. + let tol = s.tolerance(); + let mut verts: Vec = Vec::new(); + let pieces = if closed { keep.len() } else { keep.len() - 1 }; + for k in 0..pieces { + let a = keep[k]; + let b = if k + 1 < keep.len() { keep[k + 1] } else { keep[0] + n }; + verts.push(a); + let idx: Vec = (a..=b).collect(); + let mut marks = vec![false; idx.len()]; + simplify(&idx.iter().map(|&i| p[i % n]).collect::>(), 0, idx.len() - 1, tol, &mut marks); + verts.extend(idx.iter().zip(&marks).skip(1).take(idx.len().saturating_sub(2)).filter(|m| *m.1).map(|(&i, _)| i % n)); + } + if !closed { + verts.push(n - 1); + } + let v: Vec = verts.iter().map(|&i| p[i]).collect(); + let corner: Vec = verts.iter().map(|&i| is_corner[i]).collect(); + let out = curves(&v, &corner, closed); + if closed { + let before = signed_area(original); + let after = signed_area(&out); + // Region merging decides which islands survive. Rounding and simplification must not + // erase one (or reverse its hole): keep the original shared outline if half its area + // would be lost. Close it explicitly because the fill consumes consecutive segments. + if before * after <= 0.0 || after.abs() < before.abs() * 0.5 { + let mut out = original.to_vec(); + out.extend(original.first().copied()); + return out; + } + } + out +} + +/// Signed loop area, relative to its first point to avoid cancellation far from the origin. +fn signed_area(pts: &[Pt]) -> f64 { + let Some(origin) = pts.first() else { return 0.0 }; + pts.iter() + .zip(pts.iter().cycle().skip(1)) + .map(|(a, b)| { + let (ax, ay) = (f64::from(a[0]) - f64::from(origin[0]), f64::from(a[1]) - f64::from(origin[1])); + let (bx, by) = (f64::from(b[0]) - f64::from(origin[0]), f64::from(b[1]) - f64::from(origin[1])); + ax * by - bx * ay + }) + .sum::() + * 0.5 +} + +/// Two passes of a moving average over `r` points either side; an open chain's ends (junctions) +/// stay put, and the window shrinks towards them so they don't pull the outline. +fn round_off(pts: &[Pt], closed: bool, r: usize) -> Vec { + let n = pts.len(); + let mut p = pts.to_vec(); + if r == 0 || n < 3 { + return p; + } + for _ in 0..2 { + let src = p.clone(); + for (i, q) in p.iter_mut().enumerate() { + let rr = if closed { r.min((n - 1) / 2) } else { r.min(i).min(n - 1 - i) }; + if rr == 0 { + continue; + } + let (mut sx, mut sy) = (0.0f32, 0.0f32); + for o in -(rr as isize)..=rr as isize { + let s = src[(i as isize + o).rem_euclid(n as isize) as usize]; + sx += s[0]; + sy += s[1]; + } + let k = (2 * rr + 1) as f32; + *q = [sx / k, sy / k]; + } + } + p +} + +/// Where the lines through `a0`–`a1` and `b0`–`b1` cross, if they aren't parallel. +fn intersect(a0: Pt, a1: Pt, b0: Pt, b1: Pt) -> Option { + let (da, db) = (sub(a1, a0), sub(b1, b0)); + let den = da[0] * db[1] - da[1] * db[0]; + if den.abs() < 1e-6 { + return None; + } + let t = ((b0[0] - a0[0]) * db[1] - (b0[1] - a0[1]) * db[0]) / den; + Some([a0[0] + da[0] * t, a0[1] + da[1] * t]) +} + +/// Marks the points of `p[a..=b]` that Douglas–Peucker keeps at tolerance `tol`. +fn simplify(p: &[Pt], a: usize, b: usize, tol: f32, marks: &mut [bool]) { + let mut stack = vec![(a, b)]; + while let Some((a, b)) = stack.pop() { + if b <= a + 1 { + continue; + } + let (pa, d) = (p[a], sub(p[b], p[a])); + let l = len(d); + let mut best = (a, 0.0f32); + for (i, q) in p.iter().enumerate().take(b).skip(a + 1) { + let e = sub(*q, pa); + let dist = if l < 1e-6 { len(e) } else { (e[0] * d[1] - e[1] * d[0]).abs() / l }; + if dist > best.1 { + best = (i, dist); + } + } + if best.1 > tol { + marks[best.0] = true; + stack.push((a, best.0)); + stack.push((best.0, b)); + } + } +} + +/// Catmull-Rom curves through `v` (one-sided at corners), flattened to a polyline. +fn curves(v: &[Pt], corner: &[bool], closed: bool) -> Vec { + let m = v.len(); + if m < 2 { + return v.to_vec(); + } + let at = |i: isize| v[i.rem_euclid(m as isize) as usize]; + let sharp = |i: isize| (!closed && (i <= 0 || i >= m as isize - 1)) || corner[i.rem_euclid(m as isize) as usize]; + let mut out = vec![v[0]]; + let segs = if closed { m } else { m - 1 }; + for i in 0..segs as isize { + let (p0, p1) = (at(i), at(i + 1)); + let d = len(sub(p1, p0)); + let (s0, s1) = (sharp(i), sharp(i + 1)); + if s0 && s1 || d < 1e-6 { + out.push(p1); + continue; + } + // Tangents scaled to this segment, so short and long neighbours don't overshoot. + let t0 = if s0 { + sub(p1, p0) + } else { + let dp = len(sub(p0, at(i - 1))); + let k = d / (dp + d).max(1e-6); + let t = sub(p1, at(i - 1)); + [t[0] * k, t[1] * k] + }; + let t1 = if s1 { + sub(p1, p0) + } else { + let dn = len(sub(at(i + 2), p1)); + let k = d / (d + dn).max(1e-6); + let t = sub(at(i + 2), p0); + [t[0] * k, t[1] * k] + }; + let c1 = [p0[0] + t0[0] / 3.0, p0[1] + t0[1] / 3.0]; + let c2 = [p1[0] - t1[0] / 3.0, p1[1] - t1[1] / 3.0]; + let steps = ((d / 1.5).ceil() as usize).clamp(2, 24); + for k in 1..=steps { + let t = k as f32 / steps as f32; + let u = 1.0 - t; + let (b0, b1, b2, b3) = (u * u * u, 3.0 * u * u * t, 3.0 * u * t * t, t * t * t); + out.push([b0 * p0[0] + b1 * c1[0] + b2 * c2[0] + b3 * p1[0], b0 * p0[1] + b1 * c1[1] + b2 * c2[1] + b3 * p1[1]]); + } + } + out +} + +/// Bounding box `[x0, y0, x1, y1]` of `pts`. +pub(super) fn bounds(pts: &[Pt]) -> [f32; 4] { + pts.iter().fold([f32::MAX, f32::MAX, f32::MIN, f32::MIN], |b, p| [b[0].min(p[0]), b[1].min(p[1]), b[2].max(p[0]), b[3].max(p[1])]) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn settings(fidelity: f32) -> Settings { + Settings { levels: 4, simplicity: 2.0, fidelity } + } + + /// The outline of a square from (2, 2) to (12, 12), one point per unit, starting at a corner. + fn square() -> Vec { + let mut p = Vec::new(); + for i in 0..10 { + p.push([2.0 + i as f32, 2.0]); + } + for i in 0..10 { + p.push([12.0, 2.0 + i as f32]); + } + for i in 0..10 { + p.push([12.0 - i as f32, 12.0]); + } + for i in 0..10 { + p.push([2.0, 12.0 - i as f32]); + } + p + } + + fn close(a: Pt, b: Pt) -> bool { + len(sub(a, b)) < 1e-3 + } + + #[test] + fn douglas_peucker_keeps_only_points_off_the_line() { + // An L, slightly ragged: only its corner is further from the end-to-end line than the + // tolerance, and the two arms are straight within it. + let wobble = |i: usize| 0.05 * (i % 2) as f32; + let p: Vec = (0..9).map(|i| if i <= 4 { [i as f32, wobble(i)] } else { [4.0 + wobble(i), (i - 4) as f32] }).collect(); + let mut marks = vec![false; p.len()]; + simplify(&p, 0, 8, 0.5, &mut marks); + assert_eq!(marks.iter().filter(|m| **m).count(), 1, "{marks:?}"); + assert!(marks[4]); + let mut marks = vec![false; p.len()]; + simplify(&p, 0, 8, 3.0, &mut marks); + assert!(marks.iter().all(|m| !m), "under the tolerance nothing is kept"); + } + + #[test] + fn lines_cross_where_they_should() { + let q = intersect([0.0, 0.0], [1.0, 1.0], [0.0, 2.0], [1.0, 1.0]); + assert!(q.is_some_and(|q| close(q, [1.0, 1.0])), "{q:?}"); + assert_eq!(intersect([0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [2.0, 1.0]), None); + } + + #[test] + fn rounding_off_keeps_an_open_chains_ends() { + let p: Vec = (0..12).map(|i| [i as f32, if i % 2 == 0 { 0.0 } else { 1.0 }]).collect(); + let r = round_off(&p, false, 3); + assert_eq!((r[0], r[11]), (p[0], p[11])); + assert!(r[5][1] > 0.2 && r[5][1] < 0.8, "the zigzag is averaged out: {:?}", r[5]); + assert_eq!(round_off(&p, false, 0), p); + } + + #[test] + fn a_square_keeps_its_four_corners_and_straight_sides() { + let out = fit(&square(), true, &settings(2.0)); + for c in [[2.0, 2.0], [12.0, 2.0], [12.0, 12.0], [2.0, 12.0]] { + assert!(out.iter().any(|p| close(*p, c)), "corner {c:?} lost: {out:?}"); + } + let on_side = |p: &Pt| [p[0] - 2.0, p[0] - 12.0, p[1] - 2.0, p[1] - 12.0].iter().any(|d| d.abs() < 1e-3); + assert!(out.iter().all(on_side), "{out:?}"); + assert!(out.len() < 10, "the sides are simplified to their ends: {out:?}"); + } + + #[test] + fn a_rounded_corner_is_squared_back() { + // The pixels cut the corner at (12, 2): the outline goes (11, 2), (12, 3) instead. + let mut p = square(); + p[10] = [11.5, 2.5]; + let out = fit(&p, true, &settings(2.0)); + assert!(out.iter().any(|q| close(*q, [12.0, 2.0])), "{out:?}"); + } + + #[test] + fn an_open_chain_ends_where_it_started_at_any_fidelity() { + let p: Vec = (0..30).map(|i| [i as f32, (i as f32 / 5.0).sin() * 4.0 + 0.3 * (i % 3) as f32]).collect(); + for f in [1.0, 2.0, 3.0] { + let out = fit(&p, false, &settings(f)); + assert_eq!((out.first(), out.last()), (p.first(), p.last()), "fidelity {f}"); + } + } + + #[test] + fn curves_pass_through_their_vertices() { + let v = [[0.0, 0.0], [4.0, 3.0], [8.0, 0.0], [12.0, 4.0]]; + let out = curves(&v, &[false; 4], false); + for p in v { + assert!(out.iter().any(|q| close(*q, p)), "{p:?} missed: {out:?}"); + } + assert!(out.len() > 8, "flattened into steps: {}", out.len()); + // Between two corners a segment is straight. + assert_eq!(curves(&v, &[true; 4], false), v.to_vec()); + } + + #[test] + fn fitting_keeps_small_islands_and_their_winding() { + use super::super::edges::follow_edges; + let (w, h) = (32, 32); + for size in [2, 3, 4, 6] { + let reg: Vec = (0..w * h).map(|i| u32::from((12..12 + size).contains(&(i % w)) && (12..12 + size).contains(&(i / w)))).collect(); + let colours = [[0.2; 3], [0.9; 3]]; + let c: Vec<_> = reg.iter().map(|&r| colours[r as usize]).collect(); + let (chains, _) = follow_edges(®, &colours, &c, w, h); + let (pts, closed) = chains.iter().find(|(_, closed)| *closed).unwrap(); + assert!(*closed); + let area = |p: &[Pt]| p.iter().zip(p.iter().cycle().skip(1)).map(|(a, b)| a[0] as f64 * b[1] as f64 - b[0] as f64 * a[1] as f64).sum::() / 2.0; + let original = area(pts); + for simplicity in [0.0, 2.0] { + let s = Settings { simplicity, ..settings(2.0) }; + if size * size < s.min_area() as usize { + continue; + } + for fidelity in [1.0, 1.5, 2.0, 3.0] { + let out = fit(pts, true, &Settings { fidelity, ..s }); + let fitted = area(&out); + assert!( + fitted * original > 0.0 && fitted.abs() >= original.abs() * 0.5, + "{size}x{size}, simplicity {simplicity}, fidelity {fidelity}: {original} -> {fitted}" + ); + assert_eq!(out.first(), out.last(), "fallbacks must close the loop for the fill"); + } + } + } + } + + #[test] + fn bounds_cover_every_point() { + assert_eq!(bounds(&[[1.0, 5.0], [-2.0, 3.0], [4.0, 4.0]]), [-2.0, 3.0, 4.0, 5.0]); + } +} diff --git a/crates/algo/src/cutout/edges.rs b/crates/algo/src/cutout/edges.rs new file mode 100644 index 0000000000..8b0a39c41b --- /dev/null +++ b/crates/algo/src/cutout/edges.rs @@ -0,0 +1,210 @@ +//! Edges: the cracks between regions, linked into chains from junction to junction, each crack +//! placed where the anti-aliasing says the edge is. + +use super::{OUT, Pt, Px}; +use crate::par; + +/// A chain's points, and whether it is closed. +pub(super) type Chain = (Vec, bool); +/// A pixel, or a lattice point (the corner at a pixel's top left). +type Cell = (i64, i64); + +/// A step along the lattice, y down. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Dir { + E, + W, + S, + N, +} + +/// Links the cracks between regions into chains. Each chain is its points (the junctions it runs +/// between, if any, and one sub-pixel point per crack) and whether it is closed; its sides are +/// the regions on its left and right (y down). +pub(super) fn follow_edges(reg: &[u32], colours: &[Px], c: &[Px], w: usize, h: usize) -> (Vec, Vec<(u32, u32)>) { + let (wi, hi) = (w as i64, h as i64); + let at = |x: i64, y: i64| -> u32 { if x < 0 || y < 0 || x >= wi || y >= hi { OUT } else { reg[y as usize * w + x as usize] } }; + let hcount = w * (h + 1); + // Horizontal crack (x, y)–(x+1, y) is `y * w + x`; vertical (x, y)–(x, y+1) follows them. + let hc = |x: i64, y: i64| (y as usize) * w + x as usize; + let vc = |x: i64, y: i64| hcount + (y as usize) * (w + 1) + x as usize; + let mut visited = vec![false; hcount + (w + 1) * h]; + // The crack leaving lattice point (x, y) in `d`: its id, the pixels on its left and right, + // and the point it reaches. + let step = |x: i64, y: i64, d: Dir| -> Option<(usize, Cell, Cell, Cell)> { + match d { + Dir::E if x < wi => Some((hc(x, y), (x, y - 1), (x, y), (x + 1, y))), + Dir::W if x > 0 => Some((hc(x - 1, y), (x - 1, y), (x - 1, y - 1), (x - 1, y))), + Dir::S if y < hi => Some((vc(x, y), (x, y), (x - 1, y), (x, y + 1))), + Dir::N if y > 0 => Some((vc(x, y - 1), (x - 1, y - 1), (x, y - 1), (x, y - 1))), + _ => None, + } + }; + // Which cracks separate two regions, and which lattice points are junctions, worked out + // once (in parallel) before the walk. + let mut is_edge = vec![false; visited.len()]; + let (hb, vb) = is_edge.split_at_mut(hcount); + par::chunks_mut(hb, w, true, |y, row| { + for (x, b) in row.iter_mut().enumerate() { + *b = at(x as i64, y as i64 - 1) != at(x as i64, y as i64); + } + }); + par::chunks_mut(vb, w + 1, true, |y, row| { + for (x, b) in row.iter_mut().enumerate() { + *b = at(x as i64 - 1, y as i64) != at(x as i64, y as i64); + } + }); + let boundary = |x: i64, y: i64, d: Dir| step(x, y, d).filter(|s| is_edge[s.0]); + let mut is_junction = vec![false; (w + 1) * (h + 1)]; + par::chunks_mut(&mut is_junction, w + 1, true, |y, row| { + let y = y as i64; + for (x, j) in row.iter_mut().enumerate() { + let x = x as i64; + let deg = [Dir::E, Dir::W, Dir::S, Dir::N].iter().filter(|&&d| boundary(x, y, d).is_some()).count(); + *j = deg >= 3 || (deg > 0 && (x == 0 || x == wi) && (y == 0 || y == hi)); + } + }); + let junction = |x: i64, y: i64| is_junction[y as usize * (w + 1) + x as usize]; + // One point per crack: its middle, moved along the normal from left to right pixel by what + // their colours say about where the edge really is. + let crack_point = |l: (i64, i64), r: (i64, i64)| -> Pt { + let mid = [(l.0 + r.0 + 1) as f32 / 2.0, (l.1 + r.1 + 1) as f32 / 2.0]; + let (n0, n1) = ((r.0 - l.0) as f32, (r.1 - l.1) as f32); + let (ra, rb) = (at(l.0, l.1), at(r.0, r.1)); + let (Some(ca), Some(cb)) = (colours.get(ra as usize), colours.get(rb as usize)) else { return mid }; + let d = [cb[0] - ca[0], cb[1] - ca[1], cb[2] - ca[2]]; + let dd = d[0] * d[0] + d[1] * d[1] + d[2] * d[2]; + if dd < 1e-4 { + return mid; + } + // How far each pixel's colour is from its own region's towards the other's (0–1). + let frac = |p: (i64, i64)| { + let q = c[p.1 as usize * w + p.0 as usize]; + ((q[0] - ca[0]) * d[0] + (q[1] - ca[1]) * d[1] + (q[2] - ca[2]) * d[2]) / dd + }; + let (fl, fr) = (frac(l).clamp(0.0, 1.0), frac(r).clamp(0.0, 1.0)); + let off = ((1.0 - fr) - fl).clamp(-0.5, 0.5); + [mid[0] + n0 * off, mid[1] + n1 * off] + }; + let mut chains = Vec::new(); + let mut sides = Vec::new(); + let mut walk = |x: i64, y: i64, first: Dir, visited: &mut Vec| { + let open = junction(x, y); + let mut pts = if open { vec![[x as f32, y as f32]] } else { Vec::new() }; + let (mut cx, mut cy, mut d) = (x, y, first); + let mut side = (OUT, OUT); + let mut closed = !open; + while let Some((id, l, r, to)) = boundary(cx, cy, d) { + if visited[id] { + break; + } + visited[id] = true; + if pts.len() <= usize::from(open) { + side = (at(l.0, l.1), at(r.0, r.1)); + } + pts.push(crack_point(l, r)); + (cx, cy) = to; + if junction(cx, cy) { + pts.push([cx as f32, cy as f32]); + closed = false; + break; + } + let back = match d { + Dir::E => Dir::W, + Dir::W => Dir::E, + Dir::S => Dir::N, + Dir::N => Dir::S, + }; + let Some(next) = [Dir::E, Dir::S, Dir::W, Dir::N].into_iter().find(|&nd| nd != back && boundary(cx, cy, nd).is_some_and(|b| !visited[b.0])) else { + break; + }; + d = next; + } + if pts.len() >= 2 { + chains.push((pts, closed)); + sides.push(side); + } + }; + for y in 0..=hi { + for x in 0..=wi { + if junction(x, y) { + for d in [Dir::E, Dir::S, Dir::W, Dir::N] { + if boundary(x, y, d).is_some_and(|b| !visited[b.0]) { + walk(x, y, d, &mut visited); + } + } + } + } + } + // What is left are closed loops with no junction (an island inside one other region). + for y in 0..=hi { + for x in 0..=wi { + for d in [Dir::E, Dir::S] { + if boundary(x, y, d).is_some_and(|b| !visited[b.0]) { + walk(x, y, d, &mut visited); + } + } + } + } + (chains, sides) +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Regions drawn as digits, one per pixel, with grey `colours` (region `k` is `k / 4`). + fn regions(rows: &[&str]) -> (Vec, Vec, usize, usize) { + let reg: Vec = rows.iter().flat_map(|r| r.bytes()).map(|b| u32::from(b - b'0')).collect(); + let n = reg.iter().max().map_or(0, |m| m + 1); + (reg, (0..n).map(|k| [k as f32 / 4.0; 3]).collect(), rows[0].len(), rows.len()) + } + + /// The chain whose sides are regions `a` and `b`, either way round. + fn between(chains: &[Chain], sides: &[(u32, u32)], a: u32, b: u32) -> Vec { + chains.iter().zip(sides).filter(|(_, s)| **s == (a, b) || **s == (b, a)).map(|(c, _)| c.clone()).collect() + } + + #[test] + fn the_edge_between_two_regions_is_one_chain_between_junctions() { + let (reg, colours, w, h) = regions(&["000111", "000111", "000111", "000111"]); + let c: Vec = reg.iter().map(|&r| colours[r as usize]).collect(); + let (chains, sides) = follow_edges(®, &colours, &c, w, h); + let inner = between(&chains, &sides, 0, 1); + assert_eq!(inner.len(), 1, "{chains:?}"); + let (pts, closed) = &inner[0]; + assert!(!closed); + // From junction to junction (where it meets the canvas edge), one point per crack. + let ends = [pts[0], pts[pts.len() - 1]]; + assert!(ends.contains(&[3.0, 0.0]) && ends.contains(&[3.0, 4.0]), "{pts:?}"); + assert_eq!(pts.len(), 2 + 4); + assert!(pts.iter().all(|p| p[0] == 3.0), "flat colours: the edge is on the crack: {pts:?}"); + // Every crack between two regions, the canvas edge included, is in exactly one chain. + let cracks: usize = chains.iter().map(|(p, closed)| p.len() - if *closed { 0 } else { 2 }).sum(); + assert_eq!(cracks, 4 + 2 * (w + h)); + } + + #[test] + fn an_island_is_one_closed_chain() { + let (reg, colours, w, h) = regions(&["00000", "01100", "01100", "00000"]); + let c: Vec = reg.iter().map(|&r| colours[r as usize]).collect(); + let (chains, sides) = follow_edges(®, &colours, &c, w, h); + let island = between(&chains, &sides, 0, 1); + assert_eq!(island.len(), 1); + assert!(island[0].1, "closed"); + assert_eq!(island[0].0.len(), 8, "one point per crack around the 2x2 island"); + } + + #[test] + fn an_anti_aliased_edge_is_placed_inside_its_pixel() { + // Region 0 is black and region 1 white; the last column of region 0 is half white, so + // the edge runs down the middle of that column. + let (reg, _, w, h) = regions(&["000111", "000111", "000111"]); + let colours = vec![[0.0; 3], [1.0; 3]]; + let c: Vec = reg.iter().enumerate().map(|(i, &r)| if i % w == 2 { [0.5; 3] } else { colours[r as usize] }).collect(); + let (chains, sides) = follow_edges(®, &colours, &c, w, h); + let inner = between(&chains, &sides, 0, 1); + let pts = &inner[0].0; + assert!(pts[1..pts.len() - 1].iter().all(|p| (p[0] - 2.5).abs() < 1e-5), "{pts:?}"); + } +} diff --git a/crates/algo/src/cutout/fill.rs b/crates/algo/src/cutout/fill.rs new file mode 100644 index 0000000000..f2e96973ae --- /dev/null +++ b/crates/algo/src/cutout/fill.rs @@ -0,0 +1,119 @@ +//! Fill: exact-area scan conversion of the traced shapes, so edges are anti-aliased and shapes +//! that share an edge sum to full coverage. + +use super::Pt; + +/// Accumulates the signed area of segment `a`–`b` into `acc` (rows of `stride` cells, `h` rows +/// visible, columns `0..stride - 2` visible): the part left of the tile is drawn down its left +/// edge (it still winds the pixels to its right), the part right of it is dropped. +pub(super) fn line_clipped(acc: &mut [f32], stride: usize, h: usize, a: Pt, b: Pt) { + if !(a[0].is_finite() && a[1].is_finite() && b[0].is_finite() && b[1].is_finite()) || a[1] == b[1] { + return; + } + let wf = (stride - 2) as f32; + // At most the two ends and two crossings; on the stack, as this runs for every segment drawn. + let mut buf = [0.0f32, 1.0, 0.0, 0.0]; + let mut n = 2; + for x in [0.0, wf] { + let t = (x - a[0]) / (b[0] - a[0]); + if t > 0.0 && t < 1.0 { + buf[n] = t; + n += 1; + } + } + let cuts = &mut buf[..n]; + cuts.sort_by(f32::total_cmp); + for t in cuts.windows(2) { + let p = [a[0] + (b[0] - a[0]) * t[0], a[1] + (b[1] - a[1]) * t[0]]; + let q = [a[0] + (b[0] - a[0]) * t[1], a[1] + (b[1] - a[1]) * t[1]]; + let mx = (p[0] + q[0]) / 2.0; + if mx >= wf { + continue; + } + if mx < 0.0 { + line(acc, stride, h, [0.0, p[1]], [0.0, q[1]]); + } else { + line(acc, stride, h, [p[0].clamp(0.0, wf), p[1]], [q[0].clamp(0.0, wf), q[1]]); + } + } +} + +/// Signed-area accumulation of one segment inside the tile (the scanline method of font +/// rasterisers): after a running sum along each row, a cell holds the coverage winding. +fn line(acc: &mut [f32], stride: usize, h: usize, p0: Pt, p1: Pt) { + if p0[1] == p1[1] { + return; + } + let (dir, p0, p1) = if p0[1] < p1[1] { (1.0f32, p0, p1) } else { (-1.0, p1, p0) }; + let dxdy = (p1[0] - p0[0]) / (p1[1] - p0[1]); + let mut x = p0[0]; + let ystart = p0[1].max(0.0); + if p0[1] < 0.0 { + x -= p0[1] * dxdy; + } + let yend = p1[1].min(h as f32); + if ystart >= yend { + return; + } + let max_x = (stride - 1) as f32; + for y in (ystart as usize)..(yend.ceil() as usize).min(h) { + let ls = y * stride; + let dy = ((y + 1) as f32).min(p1[1]) - (y as f32).max(p0[1]); + let xnext = x + dxdy * dy; + let d = dy * dir; + let (x0, x1) = if x < xnext { (x, xnext) } else { (xnext, x) }; + let (x0, x1) = (x0.clamp(0.0, max_x - 1.0), x1.clamp(0.0, max_x - 1.0)); + let x0f = x0.floor(); + let x0i = x0f as usize; + let x1c = x1.ceil(); + let x1i = x1c as usize; + if x1i <= x0i + 1 { + let xmf = 0.5 * (x0 + x1) - x0f; + acc[ls + x0i] += d - d * xmf; + acc[ls + x0i + 1] += d * xmf; + } else { + let sx = (x1 - x0).recip(); + let x0r = x0 - x0f; + let a0 = 0.5 * sx * (1.0 - x0r) * (1.0 - x0r); + let x1r = x1 - x1c + 1.0; + let am = 0.5 * sx * x1r * x1r; + acc[ls + x0i] += d * a0; + if x1i == x0i + 2 { + acc[ls + x0i + 1] += d * (1.0 - a0 - am); + } else { + let a1 = sx * (1.5 - x0r); + acc[ls + x0i + 1] += d * (a1 - a0); + for xi in x0i + 2..x1i - 1 { + acc[ls + xi] += d * sx; + } + let a2 = a1 + (x1i - x0i - 3) as f32 * sx; + acc[ls + x1i - 1] += d * (1.0 - a2 - am); + } + acc[ls + x1i] += d * am; + } + x = xnext; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_square_covers_its_area_exactly() { + let mut acc = vec![0.0f32; 12 * 10]; + let sq = [[2.25, 2.5], [7.75, 2.5], [7.75, 6.5], [2.25, 6.5], [2.25, 2.5]]; + for s in sq.windows(2) { + line_clipped(&mut acc, 12, 10, s[0], s[1]); + } + let mut total = 0.0; + for y in 0..10 { + let mut sum = 0.0; + for x in 0..10 { + sum += acc[y * 12 + x]; + total += sum.abs().min(1.0); + } + } + assert!((total - 5.5 * 4.0).abs() < 1e-4, "{total}"); + } +} diff --git a/crates/algo/src/cutout/mod.rs b/crates/algo/src/cutout/mod.rs new file mode 100644 index 0000000000..b3081f488e --- /dev/null +++ b/crates/algo/src/cutout/mod.rs @@ -0,0 +1,352 @@ +//! Cutout (Filter Gallery › Artistic): the image as flat sheets of cut paper. +//! +//! The image is traced into shapes, and the shapes are filled back in: +//! +//! 1. **Smooth** within tones, edges kept: a bilateral filter (C. Tomasi and R. Manduchi, +//! "Bilateral Filtering for Gray and Color Images", 1998) in Oklab, so a photo's noise doesn't +//! decide pixel by pixel which of two colours a pixel gets. +//! 2. **Palette**: median cut (Heckbert 1982) then k-means over the smoothed colours, Number of +//! Levels tones of the picture's own; a tone made of two clearly different hues splits in two, +//! and colours only noise apart are merged. +//! 3. **Regions**: 4-connected areas of one palette colour. Regions smaller than the Edge +//! Simplicity area, or thin slivers (the blends along an edge between two other colours), are +//! merged smallest first into the neighbour they share most border with. Each region is filled +//! with the mean colour of its own pixels. +//! 4. **Edges**: the cracks between regions are linked into chains from junction to junction +//! (where three regions meet, or the canvas edge). Each crack is moved along its normal to where +//! the pixels on either side say the edge is (an anti-aliased pixel is part one colour, part the +//! other), so outlines are right to a fraction of a pixel. A chain is fitted once, so two +//! neighbouring shapes share one and the same edge. +//! 5. **Curves**: turns sharper than Edge Fidelity allows are kept as corners (squared back where +//! the pixels rounded them); the runs between are simplified to the Edge Simplicity tolerance +//! (Douglas–Peucker) and drawn as Catmull-Rom curves. +//! 6. **Fill**: each region is scan-converted with exact area coverage, so edges are anti-aliased +//! and the coverages of shapes that share an edge sum to one: no seams. +//! +//! Tracing reads the whole image and runs once; filling runs per tile and gives the same pixels +//! whatever the tiling. +//! +//! Each stage is its own module: [`colour`] (steps 1–2), [`regions`] (3), [`edges`] (4), +//! [`curves`] (5) and [`fill`] (6); [`trace`] runs them in turn. + +use photocraft_geom::Rect; + +use crate::par; + +mod colour; +mod curves; +mod edges; +mod fill; +mod regions; +mod util; + +use colour::{nearest, oklab, palette, smooth}; +use curves::{bounds, fit}; +use edges::follow_edges; +use fill::line_clipped; +use regions::{components, local_paper, merge_regions, paper_colours}; + +type Px = [f32; 3]; +type Pt = [f32; 2]; + +/// Region id of the area outside the image. +const OUT: u32 = u32::MAX; + +/// The filter's three sliders. +#[derive(Clone, Copy, Debug)] +pub(crate) struct Settings { + /// Number of Levels (2–8): the most colours the palette has. + pub levels: usize, + /// Edge Simplicity (0–10): smaller shapes merge and outlines simplify more. + pub simplicity: f32, + /// Edge Fidelity (1–3): more corners are kept. + pub fidelity: f32, +} + +impl Settings { + /// Regions with fewer pixels merge into a neighbour. + fn min_area(&self) -> u32 { + (1.0 + self.simplicity.clamp(0.0, 10.0)).powi(2).round() as u32 + } + /// Regions thinner than this (twice the area over the perimeter, in pixels) merge too. + fn sliver(&self) -> f32 { + 0.6 + 0.1 * self.simplicity.clamp(0.0, 10.0) + } + /// Douglas–Peucker tolerance of the outlines, in pixels: looser with less Edge Fidelity + /// (twice as loose at 1, half at 3). + fn tolerance(&self) -> f32 { + (0.5 + 0.12 * self.simplicity.clamp(0.0, 10.0)) * 2f32.powf(2.0 - self.fidelity.clamp(1.0, 3.0)) + } + /// Below Edge Fidelity 2 the outline is rounded off before it is fitted: points averaged with + /// this many neighbours either side (3 at fidelity 1). + fn rounding(&self) -> usize { + ((2.0 - self.fidelity.clamp(1.0, 3.0)) * 3.0).round().max(0.0) as usize + } + /// Turns sharper than this stay corners. + fn corner_turn(&self) -> f32 { + (100.0 - 22.5 * self.fidelity.clamp(1.0, 3.0)).to_radians() + } + /// Rounds of edge-preserving smoothing. + fn smooth_rounds(&self) -> usize { + 2 + (self.simplicity.clamp(0.0, 10.0) / 4.0) as usize + } +} + +/// A traced image: the shapes and their colours, ready to fill any tile. +pub(crate) struct Traced { + /// Document position of the traced window's top-left pixel; outlines are relative to it. + x0: i32, + y0: i32, + /// Fitted edges (polylines), each shared by the one or two regions it separates. + chains: Vec>, + /// Per chain, its bounding box `[x0, y0, x1, y1]`. + chain_boxes: Vec<[f32; 4]>, + regions: Vec, + /// Width of the traced window, each pixel's region, and its paper colour (the region's + /// lightness with the hue of its own pixels nearby). + width: usize, + reg: Vec, + field: Vec, +} + +/// One shape: its flat colour, bounding box and outline. +struct Region { + colour: Px, + bbox: [f32; 4], + /// The chains around the region, reversed where the region is on their right. + edges: Vec<(u32, bool)>, +} + +/// Traces `c` (sRGB colour of every pixel of `r`, row-major). +pub(crate) fn trace(c: &[Px], r: Rect, s: &Settings) -> Traced { + let (w, h) = (r.width() as usize, r.height() as usize); + let empty = Traced { x0: r.x0, y0: r.y0, chains: Vec::new(), chain_boxes: Vec::new(), regions: Vec::new(), width: 0, reg: Vec::new(), field: Vec::new() }; + if w == 0 || h == 0 || c.len() != w * h { + return empty; + } + let mut lab = vec![[0.0f32; 3]; w * h]; + par::chunks_mut(&mut lab, w, true, |y, row| { + for (o, p) in row.iter_mut().zip(&c[y * w..(y + 1) * w]) { + *o = oklab(*p); + } + }); + let original = lab.clone(); + let lab = smooth(lab, w, h, s.smooth_rounds()); + let palette = palette(&lab, s.levels.clamp(2, 8)); + let mut labels = vec![0u8; w * h]; + par::chunks_mut(&mut labels, w, true, |y, row| { + for (o, p) in row.iter_mut().zip(&lab[y * w..(y + 1) * w]) { + *o = nearest(&palette, *p); + } + }); + let (comp, n) = components(&labels, w, h); + let (reg, colours) = merge_regions(&comp, n, c, w, h, s); + let (chains, sides) = follow_edges(®, &colours, c, w, h); + let chains: Vec> = par::map(chains.len(), true, |i| fit(&chains[i].0, chains[i].1, s)); + let chain_boxes: Vec<[f32; 4]> = chains.iter().map(|ch| bounds(ch)).collect(); + let paint = paper_colours(®, colours.len(), &original); + let field = local_paper(®, &paint, &original, w, h, 3.0 + 1.2 * s.simplicity.clamp(0.0, 10.0)); + let mut regions: Vec = + paint.into_iter().map(|colour| Region { colour, bbox: [f32::MAX, f32::MAX, f32::MIN, f32::MIN], edges: Vec::new() }).collect(); + for (k, &(left, right)) in sides.iter().enumerate() { + for (side, rev) in [(left, false), (right, true)] { + if let Some(g) = regions.get_mut(side as usize) { + g.edges.push((k as u32, rev)); + let b = chain_boxes[k]; + g.bbox = [g.bbox[0].min(b[0]), g.bbox[1].min(b[1]), g.bbox[2].max(b[2]), g.bbox[3].max(b[3])]; + } + } + } + Traced { x0: r.x0, y0: r.y0, chains, chain_boxes, regions, width: w, reg, field } +} + +impl Traced { + /// The cut paper over `r` (document coordinates): sRGB colour per pixel, row-major. + pub(crate) fn render(&self, r: Rect) -> Vec { + let (w, h) = (r.width() as usize, r.height() as usize); + let mut col = vec![[0.0f32; 3]; w * h]; + if w == 0 || h == 0 { + return col; + } + let mut weight = vec![0.0f32; w * h]; + let stride = w + 2; + let mut acc = vec![0.0f32; stride * h]; + let (ox, oy) = ((self.x0 - r.x0) as f32, (self.y0 - r.y0) as f32); + let (wf, hf) = (w as f32, h as f32); + let (dx, dy) = ((r.x0 - self.x0) as i64, (r.y0 - self.y0) as i64); + let th = self.reg.len() / self.width.max(1); + for (gi, g) in self.regions.iter().enumerate() { + let b = [g.bbox[0] + ox, g.bbox[1] + oy, g.bbox[2] + ox, g.bbox[3] + oy]; + if b[2] <= 0.0 || b[0] >= wf || b[3] <= 0.0 || b[1] >= hf { + continue; + } + for &(k, rev) in &g.edges { + let (Some(pts), Some(cb)) = (self.chains.get(k as usize), self.chain_boxes.get(k as usize)) else { continue }; + // Chains left of the tile still count (they wind the rows they cross); chains to + // its right or above or below it don't. + if cb[0] + ox >= wf || cb[3] + oy <= 0.0 || cb[1] + oy >= hf { + continue; + } + for seg in pts.windows(2) { + let (a, z) = if rev { (seg[1], seg[0]) } else { (seg[0], seg[1]) }; + line_clipped(&mut acc, stride, h, [a[0] + ox, a[1] + oy], [z[0] + ox, z[1] + oy]); + } + } + let y0 = b[1].floor().max(0.0) as usize; + let y1 = (b[3].ceil().max(0.0) as usize).min(h); + let xe = ((b[2].ceil().max(0.0) as usize) + 1).min(w); + for y in y0..y1 { + let row = &mut acc[y * stride..(y + 1) * stride]; + let mut sum = 0.0f32; + for (x, v) in row.iter().enumerate().take(xe) { + sum += v; + let cov = sum.abs().min(1.0); + if cov > 0.0 { + let i = y * w + x; + // Inside its own pixels a shape takes the local colour; where it reaches + // into a neighbour's edge pixel, its own flat colour. + let (tx, ty) = (x as i64 + dx, y as i64 + dy); + let t = (tx >= 0 && ty >= 0 && (tx as usize) < self.width && (ty as usize) < th).then(|| ty as usize * self.width + tx as usize); + let paint = match t { + Some(t) if self.reg[t] as usize == gi => self.field[t], + _ => g.colour, + }; + col[i] = [col[i][0] + paint[0] * cov, col[i][1] + paint[1] * cov, col[i][2] + paint[2] * cov]; + weight[i] += cov; + } + } + row.fill(0.0); + } + } + for (p, &wt) in col.iter_mut().zip(&weight) { + if wt > 1e-6 { + *p = [p[0] / wt, p[1] / wt, p[2] / wt]; + } + } + col + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use colour::dist2; + + fn settings(levels: usize, simplicity: f32, fidelity: f32) -> Settings { + Settings { levels, simplicity, fidelity } + } + + /// Total coverage of every pixel: each pixel is covered exactly once by the shapes. + fn coverage(t: &Traced, r: Rect) -> Vec { + let (w, h) = (r.width() as usize, r.height() as usize); + let mut weight = vec![0.0f32; w * h]; + let stride = w + 2; + let mut acc = vec![0.0f32; stride * h]; + for g in &t.regions { + for &(k, rev) in &g.edges { + for seg in t.chains[k as usize].windows(2) { + let (a, z) = if rev { (seg[1], seg[0]) } else { (seg[0], seg[1]) }; + line_clipped(&mut acc, stride, h, a, z); + } + } + for y in 0..h { + let mut sum = 0.0; + for x in 0..w { + sum += acc[y * stride + x]; + weight[y * w + x] += sum.abs().min(1.0); + } + acc[y * stride..(y + 1) * stride].fill(0.0); + } + } + weight + } + + #[test] + fn shapes_cover_every_pixel_once_with_no_seams() { + // Blobs of four colours touching each other and the canvas edge, with T-junctions, + // saddles and holes. + let (w, h) = (48usize, 36usize); + let pal = [[0.1, 0.1, 0.1], [0.9, 0.2, 0.2], [0.2, 0.7, 0.3], [0.95, 0.95, 0.9]]; + let c: Vec = (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as f32, (i / w) as f32); + let k = ((x / 7.0).sin() + (y / 5.0).cos() + (x * y / 90.0).sin()).abs() * 1.6; + pal[(k as usize).min(3)] + }) + .collect(); + let r = Rect::new(0, 0, w as i32, h as i32); + for fidelity in [1.0, 3.0] { + let t = trace(&c, r, &settings(4, 2.0, fidelity)); + assert!(t.regions.len() > 4); + let cov = coverage(&t, r); + let worst = cov.iter().map(|v| (v - 1.0).abs()).fold(0.0, f32::max); + assert!(worst < 1e-3, "coverage off by {worst}"); + } + } + + #[test] + fn edge_fidelity_changes_the_outlines() { + // It used to set only which bends count as corners: on smooth shapes 0.5% of pixels + // changed from fidelity 1 to 2 and 2.8% from 2 to 3 (now about 9% and 5%). + // Large smooth shapes, as in a photo: few corners, long curved outlines. + let (w, h) = (160usize, 120usize); + let pal = [[0.1, 0.1, 0.1], [0.9, 0.3, 0.2], [0.3, 0.6, 0.9]]; + let c: Vec = (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as f32, (i / w) as f32); + let k = ((x / 17.0).sin() * 1.3 + (y / 13.0).cos() + (x * y / 900.0).sin()).abs(); + pal[(k as usize).min(2)] + }) + .collect(); + let r = Rect::new(0, 0, w as i32, h as i32); + let out = |f: f32| trace(&c, r, &settings(3, 2.0, f)).render(r); + let changed = |a: &[Px], b: &[Px]| a.iter().zip(b).filter(|(p, q)| dist2(**p, **q) > 0.01).count() as f32 / a.len() as f32; + let (loose, normal, tight) = (out(1.0), out(2.0), out(3.0)); + assert!(changed(&loose, &normal) > 0.04, "fidelity 1 vs 2: {}", changed(&loose, &normal)); + assert!(changed(&normal, &tight) > 0.04, "fidelity 2 vs 3: {}", changed(&normal, &tight)); + } + + #[test] + fn empty_images_stop_before_the_transpose_and_row_passes() { + for r in [Rect::new(0, 0, 0, 4), Rect::new(0, 0, 4, 0), Rect::new(0, 0, 0, 0)] { + let t = trace(&[], r, &settings(4, 2.0, 1.0)); + assert!(t.regions.is_empty()); + assert!(t.chains.is_empty()); + assert!(t.field.is_empty()); + } + } + + #[test] + fn filling_does_not_depend_on_the_tiling() { + let (w, h) = (40usize, 30usize); + let c: Vec = (0..w * h).map(|i| if ((i % w) / 6 + (i / w) / 5) % 3 == 0 { [0.8, 0.3, 0.1] } else { [0.1, 0.3, 0.7] }).collect(); + let r = Rect::new(10, 20, 10 + w as i32, 20 + h as i32); + let t = trace(&c, r, &settings(3, 1.0, 2.0)); + let whole = t.render(r); + for tile in [Rect::new(10, 20, 23, 31), Rect::new(23, 31, 50, 50), Rect::new(37, 20, 50, 33)] { + let part = t.render(tile); + for y in tile.y0..tile.y1 { + for x in tile.x0..tile.x1 { + let a = whole[((y - r.y0) as usize) * w + (x - r.x0) as usize]; + let b = part[((y - tile.y0) as usize) * tile.width() as usize + (x - tile.x0) as usize]; + assert!(dist2(a, b) < 1e-8, "({x}, {y}): {a:?} vs {b:?}"); + } + } + } + } + + #[test] + fn noise_merges_into_neighbours_and_noise_apart_colours_are_one() { + let (w, h) = (32usize, 32usize); + let c: Vec = (0..w * h) + .map(|i| { + let v = 0.3 + crate::photo_util::hash01((i % w) as i64, (i / w) as i64, 5) * 0.06; + if (11..14).contains(&(i % w)) && (11..14).contains(&(i / w)) { [0.9, 0.9, 0.9] } else { [v, v, v] } + }) + .collect(); + let r = Rect::new(0, 0, w as i32, h as i32); + assert_eq!(trace(&c, r, &settings(6, 0.0, 2.0)).regions.len(), 2, "the speck stays at simplicity 0"); + assert_eq!(trace(&c, r, &settings(6, 10.0, 2.0)).regions.len(), 1, "the speck merges at simplicity 10"); + } +} diff --git a/crates/algo/src/cutout/regions.rs b/crates/algo/src/cutout/regions.rs new file mode 100644 index 0000000000..6c467b2ef2 --- /dev/null +++ b/crates/algo/src/cutout/regions.rs @@ -0,0 +1,357 @@ +//! Regions: connected areas of one palette colour, the merge of small ones and slivers, and the +//! paper colour of each region. + +use std::cmp::Reverse; +use std::collections::{BinaryHeap, HashMap}; +use std::hash::BuildHasherDefault; + +use super::colour::{in_gamut, oklab, srgb}; +use super::util::{FxHasher, find, transpose, transpose_u32}; +use super::{Px, Settings}; +use crate::par; + +/// 4-connected components of equal labels, numbered in raster order of their first pixel. +pub(super) fn components(labels: &[u8], w: usize, h: usize) -> (Vec, usize) { + let mut comp = vec![0u32; w * h]; + let mut parent: Vec = Vec::new(); + for y in 0..h { + for x in 0..w { + let i = y * w + x; + let up = (y > 0 && labels[i - w] == labels[i]).then(|| comp[i - w]); + let left = (x > 0 && labels[i - 1] == labels[i]).then(|| comp[i - 1]); + comp[i] = match (up, left) { + (Some(a), Some(b)) => { + let (ra, rb) = (find(&mut parent, a), find(&mut parent, b)); + let (lo, hi) = (ra.min(rb), ra.max(rb)); + parent[hi as usize] = lo; + lo + } + (Some(a), None) | (None, Some(a)) => a, + (None, None) => { + parent.push(parent.len() as u32); + parent.len() as u32 - 1 + } + }; + } + } + let mut index = vec![u32::MAX; parent.len()]; + let mut n = 0u32; + for c in comp.iter_mut() { + let root = find(&mut parent, *c) as usize; + if index[root] == u32::MAX { + index[root] = n; + n += 1; + } + *c = index[root]; + } + (comp, n as usize) +} + +/// Merges small and sliver components into their neighbours. Returns each pixel's region and +/// each region's mean colour. +pub(super) fn merge_regions(comp: &[u32], n: usize, c: &[Px], w: usize, h: usize, s: &Settings) -> (Vec, Vec) { + let mut area = vec![0u32; n]; + let mut sum = vec![[0.0f64; 3]; n]; + for (&k, p) in comp.iter().zip(c) { + let k = k as usize; + area[k] += 1; + sum[k] = [sum[k][0] + f64::from(p[0]), sum[k][1] + f64::from(p[1]), sum[k][2] + f64::from(p[2])]; + } + // Shared border lengths (in cracks) and perimeters, the canvas edge included. + let mut shared: HashMap> = HashMap::default(); + let mut perim = vec![0u32; n]; + for y in 0..h { + for x in 0..w { + let a = comp[y * w + x]; + for (nx, ny) in [(x + 1, y), (x, y + 1)] { + if nx < w && ny < h { + let b = comp[ny * w + nx]; + if a != b { + *shared.entry((u64::from(a.min(b)) << 32) | u64::from(a.max(b))).or_insert(0) += 1; + } + } + } + perim[a as usize] += u32::from(x == 0) + u32::from(x + 1 == w) + u32::from(y == 0) + u32::from(y + 1 == h); + } + } + let mut adj: Vec> = vec![Vec::new(); n]; + let mut pairs: Vec<(u64, u32)> = shared.into_iter().collect(); + pairs.sort_unstable(); + for (key, len) in pairs { + let (a, b) = ((key >> 32) as u32, key as u32); + adj[a as usize].push((b, len)); + adj[b as usize].push((a, len)); + perim[a as usize] += len; + perim[b as usize] += len; + } + let (min_area, sliver) = (s.min_area(), s.sliver()); + let mergeable = |a: u32, p: u32| a < min_area || 2.0 * a as f32 / (p.max(1) as f32) < sliver; + let mut parent: Vec = (0..n as u32).collect(); + let mut heap: BinaryHeap> = (0..n).filter(|&i| mergeable(area[i], perim[i])).map(|i| Reverse((area[i], i as u32))).collect(); + while let Some(Reverse((a, id))) = heap.pop() { + let i = id as usize; + if find(&mut parent, id) != id || area[i] != a || !mergeable(a, perim[i]) { + continue; + } + // The neighbours as they are now, with their total shared border. + let mut nb: Vec<(u32, u32)> = std::mem::take(&mut adj[i]).into_iter().map(|(k, len)| (find(&mut parent, k), len)).filter(|&(k, _)| k != id).collect(); + nb.sort_unstable(); + nb.dedup_by(|b, a| { + if a.0 == b.0 { + a.1 += b.1; + true + } else { + false + } + }); + let Some(&(t, len)) = nb.iter().max_by(|a, b| a.1.cmp(&b.1).then(b.0.cmp(&a.0))) else { + adj[i] = nb; + continue; + }; + let ti = t as usize; + parent[i] = t; + area[ti] += area[i]; + sum[ti] = [sum[ti][0] + sum[i][0], sum[ti][1] + sum[i][1], sum[ti][2] + sum[i][2]]; + perim[ti] = (perim[ti] + perim[i]).saturating_sub(2 * len); + // Appended as they are; resolved and summed when `t` itself merges. + adj[ti].extend(nb.into_iter().filter(|&(k, _)| k != t)); + if mergeable(area[ti], perim[ti]) { + heap.push(Reverse((area[ti], t))); + } + } + let mut index = vec![u32::MAX; n]; + let mut colours = Vec::new(); + let mut reg = vec![0u32; comp.len()]; + for (r, &k) in reg.iter_mut().zip(comp) { + let root = find(&mut parent, k) as usize; + if index[root] == u32::MAX { + index[root] = colours.len() as u32; + let a = f64::from(area[root].max(1)); + colours.push([(sum[root][0] / a) as f32, (sum[root][1] / a) as f32, (sum[root][2] / a) as f32]); + } + *r = index[root]; + } + (reg, colours) +} + +/// The paper colour of every region. A plain average of a region's pixels greys it (a face of +/// pink and cream patches averages to beige, hues cancel), and a whole image of averages sits in +/// the middle tones. So in Oklab each region keeps its average hue at the average *strength* of +/// its pixels' colour, and lightness is stretched over the image's regions so the darkest paper is +/// near black and the lightest near white. Out-of-gamut colours lose chroma, not hue. +pub(super) fn paper_colours(reg: &[u32], n: usize, lab: &[Px]) -> Vec { + let mut acc = vec![[0.0f64; 5]; n]; + for (&r, p) in reg.iter().zip(lab) { + if let Some(a) = acc.get_mut(r as usize) { + let chroma = (p[1] * p[1] + p[2] * p[2]).sqrt(); + *a = [a[0] + f64::from(p[0]), a[1] + f64::from(p[1]), a[2] + f64::from(p[2]), a[3] + f64::from(chroma), a[4] + 1.0]; + } + } + let mean: Vec<(Px, f32, f64)> = acc + .iter() + .map(|a| { + let k = a[4].max(1.0); + ([(a[0] / k) as f32, (a[1] / k) as f32, (a[2] / k) as f32], (a[3] / k) as f32, a[4]) + }) + .collect(); + // The lightness range of the image's paper, by area (ignoring the extreme 1% at each end). + let mut by_l: Vec<(f32, f64)> = mean.iter().map(|m| (m.0[0], m.2)).collect(); + by_l.sort_by(|a, b| a.0.total_cmp(&b.0)); + let total: f64 = by_l.iter().map(|m| m.1).sum(); + let at = |q: f64| { + let mut run = 0.0; + for &(l, a) in &by_l { + run += a; + if run >= q * total { + return l; + } + } + by_l.last().map_or(0.5, |m| m.0) + }; + let (lo, hi) = (at(0.01), at(0.99)); + const STRETCH: f32 = 0.75; + mean.iter() + .map(|(m, chroma, _)| { + let ab = (m[1] * m[1] + m[2] * m[2]).sqrt(); + let k = if ab > 1e-5 { chroma / ab } else { 1.0 }; + let stretched = if hi - lo > 0.05 { m[0] + (0.04 + (m[0] - lo) / (hi - lo) * 0.88 - m[0]) * STRETCH } else { m[0] }; + let stretched = stretched.clamp(0.0, 1.0); + let colour = |l: f32, t: f32| srgb([l, m[1] * k * t, m[2] * k * t]); + if let Some(c) = colour(stretched, 1.0) { + return c; + } + // Colour comes before contrast: a strong colour the stretch took out of gamut (a + // bright blue pushed towards white) moves back towards its own lightness first. + let own = m[0].clamp(0.0, 1.0); + if colour(own, 1.0).is_some() { + let (mut ok, mut bad) = (own, stretched); + for _ in 0..12 { + let l = (ok + bad) / 2.0; + if colour(l, 1.0).is_some() { ok = l } else { bad = l } + } + return colour(ok, 1.0).unwrap_or([ok, ok, ok]); + } + // Even its own lightness can't hold it: keep the stretch, lose chroma (not hue). + let (mut ok, mut bad) = (0.0f32, 1.0f32); + for _ in 0..12 { + let t = (ok + bad) / 2.0; + if colour(stretched, t).is_some() { ok = t } else { bad = t } + } + colour(stretched, ok).unwrap_or([stretched, stretched, stretched]) + }) + .collect() +} + +/// Each pixel's paper colour: its region's lightness (flat across the shape) with the average +/// hue, at the average strength, of the region's own pixels within about `reach` pixels. Colour +/// can drift gently across a large shape (pink cheeks on a cream face) but never crosses a cut. +/// `paper` is each region's colour (from [`paper_colours`]). +pub(super) fn local_paper(reg: &[u32], paper: &[Px], lab: &[Px], w: usize, h: usize, reach: f32) -> Vec { + let mut v: Vec = lab.iter().map(|p| [p[1], p[2], (p[1] * p[1] + p[2] * p[2]).sqrt()]).collect(); + let regt = transpose_u32(reg, w, h); + let a = (-1.0 / reach.max(0.5)).exp(); + for _ in 0..2 { + same_region_rows(&mut v, reg, w, a); + let mut t = transpose(&v, w, h); + same_region_rows(&mut t, ®t, h, a); + v = transpose(&t, h, w); + } + let light: Vec = paper.iter().map(|c| oklab(*c)[0]).collect(); + let mut out = vec![[0.0f32; 3]; w * h]; + par::chunks_mut(&mut out, w, true, |y, row| { + for (x, o) in row.iter_mut().enumerate() { + let i = y * w + x; + let (q, l) = (v[i], light.get(reg[i] as usize).copied().unwrap_or(0.5)); + let ab = (q[0] * q[0] + q[1] * q[1]).sqrt(); + let k = if ab > 1e-5 { q[2] / ab } else { 1.0 }; + *o = in_gamut(l, q[0] * k, q[1] * k); + } + }); + out +} + +/// Exponential smoothing along each row (both ways) that restarts at every change of region. +fn same_region_rows(v: &mut [Px], reg: &[u32], w: usize, a: f32) { + let mut chunks: Vec<(&mut [Px], &[u32])> = v.chunks_mut(w).zip(reg.chunks(w)).collect(); + let f = |(row, rg): &mut (&mut [Px], &[u32])| { + let n = row.len(); + let src = row.to_vec(); + let mut fwd = vec![([0.0f32; 3], 0.0f32); n]; + for x in 0..n { + let (p, d) = if x > 0 && rg[x] == rg[x - 1] { fwd[x - 1] } else { ([0.0; 3], 0.0) }; + fwd[x] = ([src[x][0] + a * p[0], src[x][1] + a * p[1], src[x][2] + a * p[2]], 1.0 + a * d); + } + let mut back = ([0.0f32; 3], 0.0f32); + for x in (0..n).rev() { + if x + 1 < n && rg[x] != rg[x + 1] { + back = ([0.0; 3], 0.0); + } + back = ([src[x][0] + a * back.0[0], src[x][1] + a * back.0[1], src[x][2] + a * back.0[2]], 1.0 + a * back.1); + let den = fwd[x].1 + back.1 - 1.0; + row[x] = std::array::from_fn(|c| (fwd[x].0[c] + back.0[c] - src[x][c]) / den); + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + chunks.par_iter_mut().for_each(f); + } + #[cfg(target_arch = "wasm32")] + chunks.iter_mut().for_each(f); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn settings(simplicity: f32) -> Settings { + Settings { levels: 4, simplicity, fidelity: 2.0 } + } + + /// Labels drawn as text, one character per pixel. + fn labels(rows: &[&str]) -> (Vec, usize, usize) { + (rows.iter().flat_map(|r| r.bytes()).collect(), rows[0].len(), rows.len()) + } + + #[test] + fn components_are_four_connected_and_numbered_in_raster_order() { + let (l, w, h) = labels(&["aab", "abb", "bba"]); + let (comp, n) = components(&l, w, h); + // The top-left `a`s, the `b`s (one area), then the lone `a` (touching the first only + // diagonally), in the order their first pixels come. + assert_eq!(n, 3); + assert_eq!(comp, vec![0, 0, 1, 0, 1, 1, 1, 1, 2]); + // A U shape: both arms are found as separate runs first and joined further down. + let (l, w, h) = labels(&["a.a", "a.a", "aaa"]); + let (comp, n) = components(&l, w, h); + assert_eq!(n, 2); + assert_eq!(comp[0], comp[2]); + } + + /// Regions from labels, with one flat colour per label. + fn merged(rows: &[&str], simplicity: f32) -> (Vec, Vec) { + let (l, w, h) = labels(rows); + let c: Vec = l.iter().map(|&b| [f32::from(b - b'a') / 4.0; 3]).collect(); + let (comp, n) = components(&l, w, h); + merge_regions(&comp, n, &c, w, h, &settings(simplicity)) + } + + #[test] + fn small_regions_merge_into_the_neighbour_they_share_most_border_with() { + let rows = ["aaaaaaaa", "aaaaaaaa", "aacaaaaa", "aaccbbbb", "aaaabbbb", "aaaabbbb", "aaaabbbb"]; + // At Edge Simplicity 0 every region is kept. + assert_eq!(merged(&rows, 0.0).1.len(), 3); + // At 1 (regions under four pixels merge) the three `c`s go into the `a`s, which they share seven + // cracks with (one with the `b`s). + let (reg, colours) = merged(&rows, 1.0); + assert_eq!(colours.len(), 2); + assert_eq!(reg[2 * 8 + 2], reg[0]); + assert_ne!(reg[3 * 8 + 4], reg[0]); + // A region's colour is the mean of its pixels: 37 `a`s (0.0) and 3 `c`s (0.5). + let a = colours[reg[0] as usize][0]; + assert!((a - 1.5 / 40.0).abs() < 1e-6, "{a}"); + } + + #[test] + fn slivers_merge_though_they_are_large() { + // A line one pixel wide between two areas: the blend along an edge, not a shape. + let rows: Vec = (0..40).map(|_| format!("{}c{}", "a".repeat(10), "b".repeat(9))).collect(); + let rows: Vec<&str> = rows.iter().map(String::as_str).collect(); + assert_eq!(merged(&rows, 0.0).1.len(), 3, "kept at Edge Simplicity 0"); + let (_, colours) = merged(&rows, 4.0); + assert_eq!(colours.len(), 2, "40 pixels is over the 25 that merge at 4, but it is a sliver"); + } + + #[test] + fn paper_is_stretched_towards_black_and_white_and_keeps_its_hue() { + // Three regions: a dark and a light grey, and a mid-tone orange. + let lab = [[0.35, 0.0, 0.0], [0.65, 0.0, 0.0], [0.5, 0.06, 0.05]]; + let reg: Vec = (0..300).map(|i| (i / 100) as u32).collect(); + let px: Vec = reg.iter().map(|&r| lab[r as usize]).collect(); + let paper: Vec = paper_colours(®, 3, &px).into_iter().map(oklab).collect(); + assert!(paper[0][0] < 0.25 && paper[1][0] > 0.75, "{paper:?}"); + assert!((paper[2][0] - 0.5).abs() < 0.05, "a middle tone stays in the middle: {paper:?}"); + assert!((paper[2][2] / paper[2][1] - 0.05 / 0.06).abs() < 0.05, "hue kept: {paper:?}"); + assert!(paper[0][1].abs() < 1e-3 && paper[0][2].abs() < 1e-3, "grey stays grey: {paper:?}"); + } + + #[test] + fn local_colour_drifts_within_a_shape_but_never_crosses_a_cut() { + // One light region, pink on the left and cream on the right, next to a blue one. + let (w, h) = (30usize, 4usize); + let reg: Vec = (0..w * h).map(|i| u32::from(i % w >= 20)).collect(); + let lab: Vec = (0..w * h) + .map(|i| match i % w { + 0..10 => [0.8, 0.08, 0.02], + 10..20 => [0.8, 0.01, 0.04], + _ => [0.4, -0.02, -0.12], + }) + .collect(); + let paper = [srgb([0.8, 0.04, 0.03]).unwrap_or_default(), srgb([0.4, -0.02, -0.12]).unwrap_or_default()]; + let out: Vec = local_paper(®, &paper, &lab, w, h, 3.0).into_iter().map(oklab).collect(); + let (left, right, edge) = (out[w + 2], out[w + 17], out[w + 19]); + assert!(left[1] > right[1] + 0.03, "the pink side is pinker: {left:?} vs {right:?}"); + assert!((left[0] - right[0]).abs() < 0.01, "one lightness across the shape: {left:?} vs {right:?}"); + assert!(edge[2] > 0.0, "no blue from the next shape at the cut: {edge:?}"); + } +} diff --git a/crates/algo/src/cutout/util.rs b/crates/algo/src/cutout/util.rs new file mode 100644 index 0000000000..233c6aaf08 --- /dev/null +++ b/crates/algo/src/cutout/util.rs @@ -0,0 +1,84 @@ +//! Small helpers shared by the Cutout stages. + +use std::hash::Hasher; + +use super::Px; +use crate::par; + +/// Row-major `w`×`h` colours as `h`×`w` (columns become rows), so a pass along rows can run +/// along columns too. +pub(super) fn transpose(buf: &[Px], w: usize, h: usize) -> Vec { + let mut out = vec![[0.0f32; 3]; w * h]; + par::chunks_mut(&mut out, h, true, |x, col| { + for (y, p) in col.iter_mut().enumerate() { + *p = buf[y * w + x]; + } + }); + out +} + +/// [`transpose`] for region ids. +pub(super) fn transpose_u32(buf: &[u32], w: usize, h: usize) -> Vec { + let mut out = vec![0u32; w * h]; + for (x, col) in out.chunks_mut(h).enumerate() { + for (y, p) in col.iter_mut().enumerate() { + *p = buf[y * w + x]; + } + } + out +} + +/// Union-find root of `i`, halving the path on the way. +pub(super) fn find(parent: &mut [u32], mut i: u32) -> u32 { + while parent[i as usize] != i { + let p = parent[i as usize]; + parent[i as usize] = parent[p as usize]; + i = p; + } + i +} + +/// A fast deterministic hasher for the region adjacency map. +#[derive(Default)] +pub(super) struct FxHasher(u64); + +impl Hasher for FxHasher { + fn write(&mut self, bytes: &[u8]) { + for &b in bytes { + self.write_u64(u64::from(b)); + } + } + fn write_u64(&mut self, v: u64) { + self.0 = (self.0.rotate_left(5) ^ v).wrapping_mul(0x517c_c1b7_2722_0a95); + } + fn finish(&self) -> u64 { + self.0 + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn transposing_twice_gives_the_image_back() { + let (w, h) = (5usize, 3usize); + let c: Vec = (0..w * h).map(|i| [i as f32, 0.0, 0.0]).collect(); + let t = transpose(&c, w, h); + assert_eq!(t[1], c[w], "the second value of the first column is the first of the second row"); + assert_eq!(transpose(&t, h, w), c); + let ids: Vec = (0..(w * h) as u32).collect(); + assert_eq!(transpose_u32(&ids, w, h), t.iter().map(|p| p[0] as u32).collect::>()); + assert_eq!(transpose_u32(&transpose_u32(&ids, w, h), h, w), ids); + } + + #[test] + fn find_returns_the_root_and_shortens_the_path() { + // 4 -> 3 -> 2 -> 1 -> 0. + let mut parent = vec![0, 0, 1, 2, 3]; + assert_eq!(find(&mut parent, 4), 0); + assert_eq!(find(&mut parent, 0), 0); + assert!(parent[4] < 3, "the path was halved: {parent:?}"); + assert!(parent.iter().all(|&p| p <= 2)); + } +} diff --git a/crates/algo/src/denoise.rs b/crates/algo/src/denoise.rs index 07f4745635..57260050bd 100644 --- a/crates/algo/src/denoise.rs +++ b/crates/algo/src/denoise.rs @@ -28,28 +28,57 @@ pub(crate) fn reach() -> f32 { (2 * MAX_LUMA_R + 2 * MAX_CHROMA_R + 6) as f32 } -/// Guided filter of `p` with guide `g` (both `w × h`), box radius `r`. -fn guided(g: &[f32], p: &[f32], w: usize, h: usize, r: usize, eps: f32) -> Vec { - let len = w * h; - // Interleave the four box-filtered moments to run one pass. - let mut m = vec![0.0f32; len * 4]; - for i in 0..len { - m[i * 4] = g[i]; - m[i * 4 + 1] = p[i]; - m[i * 4 + 2] = g[i] * g[i]; - m[i * 4 + 3] = g[i] * p[i]; +/// Shared first and second moments of a guide (two filtered channels). +fn guide_moments(g: &[f32], w: usize, h: usize, r: usize) -> Vec { + let mut moments = Vec::with_capacity(g.len() * 2); + for &v in g { + moments.extend_from_slice(&[v, v * v]); } - box_blur_n(&mut m, w, h, 4, r); - let mut ab = vec![0.0f32; len * 2]; - for i in 0..len { - let (mg, mp, mgg, mgp) = (m[i * 4], m[i * 4 + 1], m[i * 4 + 2], m[i * 4 + 3]); - let var = (mgg - mg * mg).max(0.0); - let a = (mgp - mg * mp) / (var + eps); - ab[i * 2] = a; - ab[i * 2 + 1] = mp - a * mg; + box_blur_n(&mut moments, w, h, 2, r); + moments +} + +/// Self-guided luminance: mean(p) = mean(g), mean(g*p) = mean(g*g). +fn guided_luma(g: &[f32], w: usize, h: usize, r: usize, eps: f32) -> Vec { + let mut ab = guide_moments(g, w, h, r); + for pair in ab.as_chunks_mut::<2>().0.iter_mut() { + let mean = pair[0]; + let covariance = pair[1] - mean * mean; + // Keep the original unclamped numerator, including roundoff below zero. + let a = covariance / (covariance.max(0.0) + eps); + pair[0] = a; + pair[1] = mean - a * mean; } box_blur_n(&mut ab, w, h, 2, r); - (0..len).map(|i| ab[i * 2] * g[i] + ab[i * 2 + 1]).collect() + g.iter().zip(ab.as_chunks::<2>().0.iter()).map(|(&v, pair)| pair[0] * v + pair[1]).collect() +} + +/// Chroma planes share the cleaned guide's mean and regularised variance. +/// Two guide channels plus two moments and two coefficients per chroma plane: +/// ten filtered channels, versus twelve for two independent guided filters. +fn guided_chroma(g: &[f32], planes: [&mut [f32]; 2], w: usize, h: usize, r: usize, eps: f32, amount: f32) { + let mut stats = guide_moments(g, w, h, r); + for pair in stats.as_chunks_mut::<2>().0.iter_mut() { + pair[1] = (pair[1] - pair[0] * pair[0]).max(0.0) + eps; + } + for plane in planes { + let mut ab = Vec::with_capacity(g.len() * 2); + for (&guide, &p) in g.iter().zip(plane.iter()) { + ab.extend_from_slice(&[p, guide * p]); + } + box_blur_n(&mut ab, w, h, 2, r); + for (pair, guide) in ab.as_chunks_mut::<2>().0.iter_mut().zip(stats.as_chunks::<2>().0.iter()) { + let mean = pair[0]; + let a = (pair[1] - guide[0] * mean) / guide[1]; + pair[0] = a; + pair[1] = mean - a * guide[0]; + } + box_blur_n(&mut ab, w, h, 2, r); + for ((v, &guide), pair) in plane.iter_mut().zip(g).zip(ab.as_chunks::<2>().0.iter()) { + let filtered = pair[0] * guide + pair[1]; + *v += (filtered - *v) * amount; + } + } } pub(crate) fn reduce_noise(src: &Image, out: Rect, ctx: &Ctx, spec: &DenoiseSpec) -> Vec { @@ -125,7 +154,7 @@ pub(crate) fn reduce_noise(src: &Image, out: Rect, ctx: &Ctx, spec: &DenoiseSpec let r = (1.0 + spec.strength.clamp(0.0, 10.0) * 0.4).round() as usize; let keep = spec.preserve.clamp(0.0, 100.0) / 100.0; let eps = (0.004 + 0.05 * s).powi(2) * (1.05 - keep); - let d = guided(&yv, &yv, ww, wh, r.min(MAX_LUMA_R), eps); + let d = guided_luma(&yv, ww, wh, r.min(MAX_LUMA_R), eps); let k = s.sqrt(); for (v, dv) in yv.iter_mut().zip(d) { *v += (dv - *v) * k; @@ -135,12 +164,7 @@ pub(crate) fn reduce_noise(src: &Image, out: Rect, ctx: &Ctx, spec: &DenoiseSpec if col > 0.0 && model != Model::Gray { let r = (1.0 + col * 8.0).round() as usize; let eps = (0.01 + 0.05 * col).powi(2); - for plane in [&mut c1, &mut c2] { - let d = guided(&yv, plane, ww, wh, r.min(MAX_CHROMA_R), eps); - for (v, dv) in plane.iter_mut().zip(d) { - *v += (dv - *v) * col; - } - } + guided_chroma(&yv, [&mut c1, &mut c2], ww, wh, r.min(MAX_CHROMA_R), eps, col); } // Sharpen Details: unsharp mask on luma. if sh > 0.0 { @@ -178,3 +202,106 @@ pub(crate) fn reduce_noise(src: &Image, out: Rect, ctx: &Ctx, spec: &DenoiseSpec } res } + +#[cfg(test)] +mod tests { + use super::*; + + /// Guided filter of `p` with guide `g` (both `w × h`), box radius `r`. + fn reference_guided(g: &[f32], p: &[f32], w: usize, h: usize, r: usize, eps: f32) -> Vec { + let len = w * h; + // Interleave the four box-filtered moments to run one pass. + let mut m = vec![0.0f32; len * 4]; + for i in 0..len { + m[i * 4] = g[i]; + m[i * 4 + 1] = p[i]; + m[i * 4 + 2] = g[i] * g[i]; + m[i * 4 + 3] = g[i] * p[i]; + } + box_blur_n(&mut m, w, h, 4, r); + let mut ab = vec![0.0f32; len * 2]; + for i in 0..len { + let (mg, mp, mgg, mgp) = (m[i * 4], m[i * 4 + 1], m[i * 4 + 2], m[i * 4 + 3]); + let var = (mgg - mg * mg).max(0.0); + let a = (mgp - mg * mp) / (var + eps); + ab[i * 2] = a; + ab[i * 2 + 1] = mp - a * mg; + } + box_blur_n(&mut ab, w, h, 2, r); + (0..len).map(|i| ab[i * 2] * g[i] + ab[i * 2 + 1]).collect() + } + + #[test] + fn shared_statistics_match_independent_filters_bit_for_bit() { + for (w, h) in [(1, 1), (1, 19), (23, 1), (7, 5), (33, 17)] { + let len = w * h; + for flat in [false, true] { + let g: Vec = (0..len).map(|i| if flat { 0.1 } else { ((i * 37 % 101) as f32 - 20.0) / 63.0 }).collect(); + let p: Vec = (0..len).map(|i| (i * 13 % 71) as f32 / 70.0 - 0.5).collect(); + let q: Vec = (0..len).map(|i| (i * 29 % 83) as f32 / 41.0 - 1.0).collect(); + for r in [0, 1, MAX_LUMA_R, MAX_CHROMA_R, 25] { + for eps in [0.000001, 0.0036] { + let expected = reference_guided(&g, &g, w, h, r, eps); + let actual = guided_luma(&g, w, h, r, eps); + assert_bits(&actual, &expected); + for amount in [0.01, 0.45, 1.0] { + let mut actual_p = p.clone(); + let mut actual_q = q.clone(); + guided_chroma(&g, [&mut actual_p, &mut actual_q], w, h, r, eps, amount); + for (original, actual) in [(&p, &actual_p), (&q, &actual_q)] { + let filtered = reference_guided(&g, original, w, h, r, eps); + let expected: Vec = original.iter().zip(filtered).map(|(&v, d)| v + (d - v) * amount).collect(); + assert_bits(actual, &expected); + } + } + } + } + } + } + } + + #[test] + fn reduce_noise_guides_both_chroma_planes_by_the_cleaned_luma() { + // Caller wiring: Lab keeps its planes as they are, so each output plane is compared with + // the independent filters run on the planes in order (luma by itself, chroma by the + // cleaned luma). + let (w, h) = (19, 13); + let rect = Rect::new(0, 0, w as i32, h as i32); + let mut src = Image::new(rect, 3); + for (i, px) in src.data.as_chunks_mut::<3>().0.iter_mut().enumerate() { + px[0] = (i * 37 % 101) as f32 / 100.0; + px[1] = (i * 13 % 71) as f32 / 70.0 - 0.5; + px[2] = (i * 29 % 83) as f32 / 41.0 - 1.0; + } + let ctx = Ctx { bounds: rect, mode: ColorMode::Lab, alpha: false }; + let spec = DenoiseSpec { strength: 6.0, preserve: 30.0, color: 50.0, sharpen: 0.0, jpeg: false }; + let out = reduce_noise(&src, rect, &ctx, &spec); + + let plane = |c: usize| -> Vec { src.data.as_chunks::<3>().0.iter().map(|px| px[c]).collect() }; + let (luma, chroma) = (plane(0), [plane(1), plane(2)]); + let s = spec.strength / 10.0; + let eps = (0.004 + 0.05 * s).powi(2) * (1.05 - spec.preserve / 100.0); + let smooth = reference_guided(&luma, &luma, w, h, (1.0 + spec.strength * 0.4).round() as usize, eps); + let cleaned: Vec = luma.iter().zip(&smooth).map(|(&v, &d)| v + (d - v) * s.sqrt()).collect(); + let col = spec.color / 100.0; + let chroma_r = (1.0 + col * 8.0).round() as usize; + let chroma_eps = (0.01 + 0.05 * col).powi(2); + let mut expected = vec![cleaned]; + for p in &chroma { + let filtered = reference_guided(&expected[0], p, w, h, chroma_r, chroma_eps); + expected.push(p.iter().zip(filtered).map(|(&v, f)| v + (f - v) * col).collect()); + } + for (c, expected) in expected.iter().enumerate() { + let actual: Vec = out.as_chunks::<3>().0.iter().map(|px| px[c]).collect(); + assert_bits(&actual, expected); + assert!(actual.iter().zip(&plane(c)).any(|(a, o)| (a - o).abs() > 1e-3), "plane {c} was filtered"); + } + } + + fn assert_bits(actual: &[f32], expected: &[f32]) { + assert_eq!(actual.len(), expected.len()); + for (i, (actual, expected)) in actual.iter().zip(expected).enumerate() { + assert_eq!(actual.to_bits(), expected.to_bits(), "pixel {i}: {actual} != {expected}"); + } + } +} diff --git a/crates/algo/src/distort.rs b/crates/algo/src/distort.rs index 664169afa4..4f8356f60d 100644 --- a/crates/algo/src/distort.rs +++ b/crates/algo/src/distort.rs @@ -28,32 +28,47 @@ pub(crate) fn centre(b: Rect) -> (f32, f32, f32) { (b.x0 as f32 + w / 2.0, b.y0 as f32 + h / 2.0, w.min(h) / 2.0) } +/// The bounds' centre and half width and height: Twirl, Pinch and Spherize act inside the ellipse +/// inscribed in the bounds, as Photoshop's do, so on a wide or tall image the effect reaches all +/// four edges instead of stopping at a circle fitted to the shorter side (#5643). A point maps to +/// unit-circle coordinates `(u, v)` and back. +fn ellipse(b: Rect) -> (f32, f32, f32, f32) { + let (w, h) = (b.width() as f32, b.height() as f32); + (b.x0 as f32 + w / 2.0, b.y0 as f32 + h / 2.0, w / 2.0, h / 2.0) +} + pub(crate) fn twirl(src: &Image, out: Rect, ctx: &Ctx, angle: f32) -> Vec { - let (cx, cy, rr) = centre(ctx.bounds); + let (cx, cy, hx, hy) = ellipse(ctx.bounds); let a = angle.to_radians(); remap(src, out, ctx, Edge::Repeat, |x, y| { - let (dx, dy) = (x - cx, y - cy); - let r = (dx * dx + dy * dy).sqrt(); - if r >= rr || rr <= 0.0 { + if hx <= 0.0 || hy <= 0.0 { return (x, y); } - let t = a * (1.0 - r / rr); + let (u, v) = ((x - cx) / hx, (y - cy) / hy); + let r = (u * u + v * v).sqrt(); + if r >= 1.0 { + return (x, y); + } + let t = a * (1.0 - r); let (s, c) = t.sin_cos(); - (cx + dx * c - dy * s, cy + dx * s + dy * c) + (cx + (u * c - v * s) * hx, cy + (u * s + v * c) * hy) }) } pub(crate) fn pinch(src: &Image, out: Rect, ctx: &Ctx, amount: f32) -> Vec { - let (cx, cy, rr) = centre(ctx.bounds); + let (cx, cy, hx, hy) = ellipse(ctx.bounds); let a = (amount / 100.0).clamp(-1.0, 1.0); remap(src, out, ctx, Edge::Repeat, |x, y| { - let (dx, dy) = (x - cx, y - cy); - let r = (dx * dx + dy * dy).sqrt(); - if r >= rr || r <= 0.0 { + if hx <= 0.0 || hy <= 0.0 { return (x, y); } - let rs = rr * (r / rr).powf(1.0 - a * 0.5); - (cx + dx * rs / r, cy + dy * rs / r) + let (u, v) = ((x - cx) / hx, (y - cy) / hy); + let r = (u * u + v * v).sqrt(); + if r >= 1.0 || r <= 0.0 { + return (x, y); + } + let k = r.powf(1.0 - a * 0.5) / r; + (cx + u * k * hx, cy + v * k * hy) }) } @@ -64,20 +79,22 @@ fn sphere_1d(rn: f32, a: f32) -> f32 { } pub(crate) fn spherize(src: &Image, out: Rect, ctx: &Ctx, amount: f32, mode: SpherizeMode) -> Vec { - let b = ctx.bounds; - let (cx, cy, rr) = centre(b); - let (hx, hy) = (b.width() as f32 / 2.0, b.height() as f32 / 2.0); + let (cx, cy, hx, hy) = ellipse(ctx.bounds); let a = (amount / 100.0).clamp(-1.0, 1.0); remap(src, out, ctx, Edge::Repeat, |x, y| { let (dx, dy) = (x - cx, y - cy); match mode { SpherizeMode::Normal => { - let r = (dx * dx + dy * dy).sqrt(); - if r >= rr || r <= 0.0 { + if hx <= 0.0 || hy <= 0.0 { return (x, y); } - let rs = rr * sphere_1d(r / rr, a); - (cx + dx * rs / r, cy + dy * rs / r) + let (u, v) = (dx / hx, dy / hy); + let r = (u * u + v * v).sqrt(); + if r >= 1.0 || r <= 0.0 { + return (x, y); + } + let k = sphere_1d(r, a) / r; + (cx + u * k * hx, cy + v * k * hy) } SpherizeMode::HorizontalOnly => { if dx.abs() >= hx { @@ -95,6 +112,10 @@ pub(crate) fn spherize(src: &Image, out: Rect, ctx: &Ctx, amount: f32, mode: Sph }) } +/// The most Wave generators the renderer models: it clamps the setting to this, so a file asking +/// for more (the command allows up to 999) would render differently from how it was authored. +pub const MAX_WAVE_GENERATORS: u32 = 64; + /// Wave generator settings. pub(crate) struct WaveSpec { pub generators: u32, @@ -120,8 +141,9 @@ fn shape(t: WaveType, phase: f32) -> f32 { } } +/// Wave: displace `src` over `out` by the average of `w.generators` seeded waves (at most `MAX_WAVE_GENERATORS`) along each axis. pub(crate) fn wave(src: &Image, out: Rect, ctx: &Ctx, w: WaveSpec) -> Vec { - let n = w.generators.clamp(1, 64); + let n = w.generators.clamp(1, MAX_WAVE_GENERATORS); let lerp = |(a, b): (f32, f32), t: f32| a + (b - a).max(0.0) * t; let gens: Vec<(f32, f32, f32, f32)> = (0..n) .map(|i| { diff --git a/crates/algo/src/exif.rs b/crates/algo/src/exif.rs index d18bcdec9e..15c81fbe9c 100644 --- a/crates/algo/src/exif.rs +++ b/crates/algo/src/exif.rs @@ -87,6 +87,8 @@ impl Tiff<'_> { } } fn text(&self, typ: u16, count: u32, field: usize) -> Option { + #[cfg(test)] + TEXT_DECODES.with(|c| c.set(c.get() + 1)); if typ != 2 { return None; } @@ -97,6 +99,9 @@ impl Tiff<'_> { } } +#[cfg(test)] +thread_local!(static TEXT_DECODES: std::cell::Cell = const { std::cell::Cell::new(0) }); + /// Reads [`CameraInfo`] from a TIFF-structured EXIF payload (an `Exif\0\0` prefix is skipped). pub fn read(exif: &[u8]) -> CameraInfo { let b = exif.strip_prefix(b"Exif\0\0").unwrap_or(exif); @@ -117,15 +122,17 @@ pub fn read(exif: &[u8]) -> CameraInfo { seen.push(off); for (tag, typ, count, field) in t.ifd(off) { match tag { - 0x010F => info.make = t.text(typ, count, field), - 0x0110 => info.model = t.text(typ, count, field), + // Duplicate TIFF text tags can point at the same large payload. Keep the first + // recognized value and do not repeatedly decode/copy later duplicates. + 0x010F if info.make.is_none() => info.make = t.text(typ, count, field), + 0x0110 if info.model.is_none() => info.model = t.text(typ, count, field), 0x829A => info.exposure_time = t.number(typ, count, field), 0x829D => info.f_number = t.number(typ, count, field), 0x8827 => info.iso = t.number(typ, count, field), 0x920A => info.focal_length = t.number(typ, count, field), 0xA405 => info.focal_length_35mm = t.number(typ, count, field).filter(|v| *v > 0.0), - 0xA434 => info.lens = t.text(typ, count, field), - 0x9003 => info.date_taken = t.text(typ, count, field), + 0xA434 if info.lens.is_none() => info.lens = t.text(typ, count, field), + 0x9003 if info.date_taken.is_none() => info.date_taken = t.text(typ, count, field), 0x8769 => { if let Some(p) = t.u32(field) { queue.push(p as usize); @@ -252,6 +259,47 @@ mod tests { /// Hostile payloads: huge counts, offsets near `u32::MAX`, self-referencing IFDs and random /// bytes read as `None` fields instead of panicking or overflowing (usize is 32 bits on wasm). + #[test] + fn duplicate_text_tags_keep_the_first_value_without_copying_large_duplicates() { + let value = vec![b'X'; 2 * 1024 * 1024]; + let mut b = b"II*\0".to_vec(); + b.extend_from_slice(&8u32.to_le_bytes()); + // IFD0: Make="M", a duplicate Make pointing at a 2 MiB payload, and the Exif IFD link. + b.extend_from_slice(&3u16.to_le_bytes()); + b.extend_from_slice(&0x010Fu16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&2u32.to_le_bytes()); + b.extend_from_slice(b"M\0\0\0"); + b.extend_from_slice(&0x010Fu16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&(value.len() as u32).to_le_bytes()); + b.extend_from_slice(&80u32.to_le_bytes()); + b.extend_from_slice(&0x8769u16.to_le_bytes()); + b.extend_from_slice(&4u16.to_le_bytes()); + b.extend_from_slice(&1u32.to_le_bytes()); + b.extend_from_slice(&50u32.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + // Exif IFD at 50: DateTimeOriginal="D" and the same large duplicate payload at offset 80. + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&0x9003u16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&2u32.to_le_bytes()); + b.extend_from_slice(b"D\0\0\0"); + b.extend_from_slice(&0x9003u16.to_le_bytes()); + b.extend_from_slice(&2u16.to_le_bytes()); + b.extend_from_slice(&(value.len() as u32).to_le_bytes()); + b.extend_from_slice(&80u32.to_le_bytes()); + b.extend_from_slice(&0u32.to_le_bytes()); + b.extend_from_slice(&value); + + TEXT_DECODES.with(|c| c.set(0)); + let info = read(&b); + assert_eq!(info.make.as_deref(), Some("M")); + assert_eq!(info.date_taken.as_deref(), Some("D")); + // One decode per tag: duplicates must be skipped before `text` copies their payload. + assert_eq!(TEXT_DECODES.with(|c| c.get()), 2); + } + #[test] fn hostile_payloads_never_panic() { let entry = |tag: u16, typ: u16, count: u32, value: u32| { diff --git a/crates/algo/src/faces.rs b/crates/algo/src/faces.rs new file mode 100644 index 0000000000..aa246fcaf8 --- /dev/null +++ b/crates/algo/src/faces.rs @@ -0,0 +1,90 @@ +//! Face detection: a port of libfacedetection's CNN (Shiqi Yu, BSD-3; `data/LICENSE-libfacedetection.txt`). +//! +//! The network is the YuNet design: a backbone of 1×1 pointwise and 3×3 depthwise convolutions with +//! 2×2 max pooling, a feature pyramid at strides 8, 16 and 32, and per-cell heads for the face score, +//! the box and five landmarks (eyes, nose tip, mouth corners). The weights (`data/libfacedetection.bin`, +//! converted to half precision from the library's `facedetectcnn-data.cpp`) are about 100 KB, and +//! inference is plain Rust: no runtime, so it works offline. +//! +//! [`detect`] takes straight RGBA in `0..=1` and returns faces in pixel coordinates of that image. +//! +//! The browser build (wasm32) leaves the network and its weights out, as `photocraft-raw` does with +//! its JPEG XL decoder: they would push the wasm over the per-file cap of static hosts +//! (packaging/web/package.sh). There [`AVAILABLE`] is false and [`detect`] finds nothing. + +#[cfg(not(target_arch = "wasm32"))] +mod cnn; + +/// Whether this build can detect faces (not the browser build: see the module docs). +pub const AVAILABLE: bool = cfg!(not(target_arch = "wasm32")); + +/// Longest side the network looks at; larger images are scaled down first (faces smaller than +/// about 10 px at this scale are missed). +pub const MAX_SIDE: usize = 1280; + +/// One detected face, in pixels of the image passed to [`detect`]. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Face { + /// Confidence `0..=1` (the library reports faces above 0.5 as reliable). + pub score: f32, + /// Left, top, width and height. + pub rect: [f32; 4], + /// Right eye, left eye (as seen in the image: the first is on the image's left), nose tip, + /// right mouth corner, left mouth corner. + pub landmarks: [[f32; 2]; 5], +} + +/// Faces in `px` (straight RGBA `0..=1`, `w × h`, row-major) scoring at least `min_score`, best +/// first. Transparent pixels count as white. `None` when `px` doesn't match the size. +#[cfg(not(target_arch = "wasm32"))] +pub fn detect(px: &[[f32; 4]], w: usize, h: usize, min_score: f32) -> Option> { + if w == 0 || h == 0 || px.len() != w.checked_mul(h)? { + return None; + } + // Scale down (box filter) so the longer side is at most MAX_SIDE. + let step = w.max(h).div_ceil(MAX_SIDE).max(1); + let (sw, sh) = (w.div_ceil(step), h.div_ceil(step)); + let mut bgr = vec![[0.0f32; 3]; sw * sh]; + for y in 0..sh { + for x in 0..sw { + let (mut acc, mut n) = ([0.0f32; 3], 0.0f32); + for yy in y * step..((y + 1) * step).min(h) { + for xx in x * step..((x + 1) * step).min(w) { + let [r, g, b, a] = px[yy * w + xx]; + let a = a.clamp(0.0, 1.0); + for (k, v) in [b, g, r].into_iter().enumerate() { + acc[k] += v.clamp(0.0, 1.0) * a + (1.0 - a); + } + n += 1.0; + } + } + bgr[y * sw + x] = acc.map(|v| (v / n.max(1.0) * 255.0).round()); + } + } + let s = step as f32; + let faces = cnn::network(&bgr, sw, sh)? + .into_iter() + .filter(|f| f.score >= min_score) + .map(|f| Face { score: f.score, rect: f.rect.map(|v| v * s), landmarks: f.landmarks.map(|[x, y]| [x * s, y * s]) }) + .collect(); + Some(faces) +} + +/// The browser build has no detector (see the module docs): always `None`. +#[cfg(target_arch = "wasm32")] +pub fn detect(_px: &[[f32; 4]], _w: usize, _h: usize, _min_score: f32) -> Option> { + None +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + + #[test] + fn no_faces_in_flat_or_tiny_images_and_bad_sizes_are_refused() { + assert_eq!(detect(&vec![[0.5, 0.5, 0.5, 1.0]; 64 * 48], 64, 48, 0.5), Some(vec![])); + assert_eq!(detect(&[[1.0, 1.0, 1.0, 1.0]], 1, 1, 0.5), Some(vec![])); + assert_eq!(detect(&[[1.0; 4]; 3], 2, 2, 0.5), None); + assert_eq!(detect(&[], 0, 0, 0.5), None); + } +} diff --git a/crates/algo/src/faces/cnn.rs b/crates/algo/src/faces/cnn.rs new file mode 100644 index 0000000000..b86cac7eb2 --- /dev/null +++ b/crates/algo/src/faces/cnn.rs @@ -0,0 +1,323 @@ +//! The network itself (native builds only: see the parent module's docs). + +use super::Face; +use crate::par; + +/// The weights: `PCFACE1\0`, the layer count (u16), then per layer its input channels (u16), filters +/// (u16) and kind (u8: 0 pointwise, 1 depthwise), the weights and the biases, all little-endian f16. +static WEIGHTS: &[u8] = include_bytes!("../../data/libfacedetection.bin"); + +const MAGIC: &[u8] = b"PCFACE1\0"; +const LAYERS: usize = 53; +/// Faces whose boxes overlap more than this (intersection over union) are one face. +const NMS_OVERLAP: f32 = 0.45; +/// Candidates kept before non-maximum suppression, as the library does. +const TOP_K: usize = 1000; + +struct Layer { + channels: usize, + filters: usize, + depthwise: bool, + weights: Vec, + biases: Vec, +} + +/// A feature map: `rows × cols` cells of `channels` values each. +#[derive(Clone)] +struct Blob { + rows: usize, + cols: usize, + channels: usize, + data: Vec, +} + +impl Blob { + fn new(rows: usize, cols: usize, channels: usize) -> Self { + Blob { rows, cols, channels, data: vec![0.0; rows * cols * channels] } + } +} + +fn f16_to_f32(h: u16) -> f32 { + let sign = if h & 0x8000 != 0 { -1.0 } else { 1.0 }; + let exp = i32::from((h >> 10) & 0x1f); + let frac = f32::from(h & 0x3ff); + match exp { + 0 => sign * frac * (2.0f32).powi(-24), + 31 => { + if frac == 0.0 { + sign * f32::INFINITY + } else { + f32::NAN + } + } + _ => sign * (1.0 + frac / 1024.0) * (2.0f32).powi(exp - 15), + } +} + +fn parse(bytes: &[u8]) -> Option> { + let rest = bytes.strip_prefix(MAGIC)?; + let mut at = 0usize; + let u16_at = |n: &mut usize| -> Option { + let b = rest.get(*n..*n + 2)?; + *n += 2; + Some(u16::from_le_bytes([b[0], b[1]])) + }; + let count = usize::from(u16_at(&mut at)?); + if count != LAYERS { + return None; + } + let mut layers = Vec::with_capacity(count); + for _ in 0..count { + let channels = usize::from(u16_at(&mut at)?); + let filters = usize::from(u16_at(&mut at)?); + let depthwise = *rest.get(at)? == 1; + at += 1; + let nw = if depthwise { 9 * channels } else { filters * channels }; + let mut read = |n: usize| -> Option> { (0..n).map(|_| u16_at(&mut at).map(f16_to_f32)).collect() }; + let weights = read(nw)?; + let biases = read(filters)?; + if depthwise && filters != channels { + return None; + } + layers.push(Layer { channels, filters, depthwise, weights, biases }); + } + (at == rest.len()).then_some(layers) +} + +/// A 1×1 or 3×3 depthwise convolution with bias, then ReLU when `relu`. +fn conv(x: &Blob, l: &Layer, relu: bool) -> Option { + if x.channels != l.channels { + return None; + } + let mut out = Blob::new(x.rows, x.cols, l.filters); + let (rows, cols, cin, cout) = (x.rows, x.cols, x.channels, l.filters); + par::chunks_mut(&mut out.data, cols * cout, rows * cols * cout > 1 << 16, |row, line| { + for col in 0..cols { + let o = &mut line[col * cout..(col + 1) * cout]; + if l.depthwise { + o.copy_from_slice(&l.biases); + for r in row.saturating_sub(1)..(row + 2).min(rows) { + for c in col.saturating_sub(1)..(col + 2).min(cols) { + let k = (r + 1 - row) * 3 + (c + 1 - col); + let input = &x.data[(r * cols + c) * cin..(r * cols + c + 1) * cin]; + let w = &l.weights[k * cin..(k + 1) * cin]; + for ((o, i), w) in o.iter_mut().zip(input).zip(w) { + *o += i * w; + } + } + } + } else { + let input = &x.data[(row * cols + col) * cin..(row * cols + col + 1) * cin]; + for (f, o) in o.iter_mut().enumerate() { + let w = &l.weights[f * cin..(f + 1) * cin]; + *o = l.biases[f] + input.iter().zip(w).map(|(i, w)| i * w).sum::(); + } + } + if relu { + for v in o.iter_mut() { + *v = v.max(0.0); + } + } + } + }); + Some(out) +} + +/// Pointwise then depthwise (ReLU after the depthwise one when `relu`). +fn conv_dp(x: &Blob, p: &Layer, d: &Layer, relu: bool) -> Option { + conv(&conv(x, p, false)?, d, relu) +} + +fn unit(x: &Blob, l: &[Layer]) -> Option { + let [p1, d1, p2, d2] = l else { return None }; + conv_dp(&conv_dp(x, p1, d1, true)?, p2, d2, true) +} + +fn max_pool(x: &Blob) -> Blob { + let rows = x.rows.saturating_sub(3).div_ceil(2) + 1; + let cols = x.cols.saturating_sub(3).div_ceil(2) + 1; + let mut out = Blob::new(rows, cols, x.channels); + for r in 0..rows { + for c in 0..cols { + let o = (r * cols + c) * x.channels; + for ch in 0..x.channels { + let mut m = f32::NEG_INFINITY; + for rr in r * 2..(r * 2 + 2).min(x.rows) { + for cc in c * 2..(c * 2 + 2).min(x.cols) { + m = m.max(x.data[(rr * x.cols + cc) * x.channels + ch]); + } + } + out.data[o + ch] = m; + } + } + } + out +} + +/// `small` upsampled ×2 (nearest) plus `big`. +fn up_add(small: &Blob, big: &Blob) -> Option { + if small.rows * 2 != big.rows || small.cols * 2 != big.cols || small.channels != big.channels { + return None; + } + let mut out = big.clone(); + let ch = big.channels; + for r in 0..big.rows { + for c in 0..big.cols { + let s = ((r / 2) * small.cols + c / 2) * ch; + let o = (r * big.cols + c) * ch; + for k in 0..ch { + out.data[o + k] += small.data[s + k]; + } + } + } + Some(out) +} + +/// The network's input: each cell holds the 3×3 neighbourhood (stride 2) of BGR values `0..=255`, +/// so the first, 3×3 stride-2 convolution is a pointwise one. The size is padded to 32. +fn input(bgr: &[[f32; 3]], w: usize, h: usize) -> Blob { + let rows = h.div_ceil(32) * 16; + let cols = w.div_ceil(32) * 16; + let mut b = Blob::new(rows, cols, 32); + for r in 0..rows { + for c in 0..cols { + let cell = (r * cols + c) * 32; + for fy in 0..3 { + let Some(y) = (r * 2 + fy).checked_sub(1).filter(|&y| y < h) else { continue }; + for fx in 0..3 { + let Some(x) = (c * 2 + fx).checked_sub(1).filter(|&x| x < w) else { continue }; + let o = cell + (fy * 3 + fx) * 3; + b.data[o..o + 3].copy_from_slice(&bgr[y * w + x]); + } + } + } + } + b +} + +/// The heads of one pyramid level, decoded to candidate faces (in network pixels). +fn candidates(feat: &Blob, l: &[Layer], heads: [usize; 4], stride: f32, out: &mut Vec) -> Option<()> { + let head = |i: usize| conv_dp(feat, l.get(i)?, l.get(i + 1)?, false); + let (cls, reg, obj, kps) = (head(heads[0])?, head(heads[1])?, head(heads[2])?, head(heads[3])?); + if cls.channels != 1 || obj.channels != 1 || reg.channels != 4 || kps.channels != 10 { + return None; + } + let sigmoid = |v: f32| 1.0 / (1.0 + (-v.clamp(-88.0, 88.0)).exp()); + for r in 0..feat.rows { + for c in 0..feat.cols { + let i = r * feat.cols + c; + let score = (sigmoid(cls.data[i]) * sigmoid(obj.data[i])).sqrt(); + if score < 0.2 { + continue; + } + let (px, py) = (c as f32 * stride, r as f32 * stride); + let b = ®.data[i * 4..i * 4 + 4]; + let (cx, cy) = (b[0] * stride + px, b[1] * stride + py); + let (bw, bh) = (b[2].exp() * stride, b[3].exp() * stride); + let k = &kps.data[i * 10..i * 10 + 10]; + let mut landmarks = [[0.0; 2]; 5]; + for (n, lm) in landmarks.iter_mut().enumerate() { + *lm = [k[2 * n] * stride + px, k[2 * n + 1] * stride + py]; + } + out.push(Face { score, rect: [cx - bw / 2.0, cy - bh / 2.0, bw, bh], landmarks }); + } + } + Some(()) +} + +fn iou(a: &[f32; 4], b: &[f32; 4]) -> f32 { + let w = ((a[0] + a[2]).min(b[0] + b[2]) - a[0].max(b[0])).max(0.0); + let h = ((a[1] + a[3]).min(b[1] + b[3]) - a[1].max(b[1])).max(0.0); + let i = w * h; + if i <= 0.0 { 0.0 } else { i / (a[2] * a[3] + b[2] * b[3] - i) } +} + +/// The heads (cls, reg, obj, kps: their first layer) of the stride 8, 16 and 32 levels. +const HEADS: [[usize; 4]; 3] = [[29, 35, 41, 47], [31, 37, 43, 49], [33, 39, 45, 51]]; + +/// The backbone and feature pyramid: the stride 8, 16 and 32 feature maps the heads read. +fn features(bgr: &[[f32; 3]], w: usize, h: usize, l: &[Layer]) -> Option<[Blob; 3]> { + let x = conv(&input(bgr, w, h), l.first()?, true)?; + let x = max_pool(&conv_dp(&x, l.get(1)?, l.get(2)?, true)?); + let x = unit(&unit(&x, l.get(3..7)?)?, l.get(7..11)?)?; + let f8 = unit(&max_pool(&x), l.get(11..15)?)?; + let f16 = unit(&max_pool(&f8), l.get(15..19)?)?; + let f32_ = unit(&max_pool(&f16), l.get(19..23)?)?; + let f32_ = conv_dp(&f32_, l.get(27)?, l.get(28)?, true)?; + let f16 = conv_dp(&up_add(&f32_, &f16)?, l.get(25)?, l.get(26)?, true)?; + let f8 = conv_dp(&up_add(&f16, &f8)?, l.get(23)?, l.get(24)?, true)?; + Some([f8, f16, f32_]) +} + +pub(super) fn network(bgr: &[[f32; 3]], w: usize, h: usize) -> Option> { + let l = parse(WEIGHTS)?; + let levels = features(bgr, w, h, &l)?; + let mut faces = Vec::new(); + // Heads (cls, reg, obj, kps) per level, as in the library's model. + for ((feat, heads), stride) in levels.iter().zip(HEADS).zip([8.0, 16.0, 32.0]) { + candidates(feat, &l, heads, stride, &mut faces)?; + } + faces.sort_by(|a, b| b.score.total_cmp(&a.score)); + faces.truncate(TOP_K); + let mut kept: Vec = Vec::new(); + for f in faces { + if kept.iter().all(|k| iou(&k.rect, &f.rect) <= NMS_OVERLAP) { + kept.push(f); + } + } + Some(kept) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn weights_parse() { + let l = parse(WEIGHTS).expect("bundled weights"); + assert_eq!(l.len(), LAYERS); + assert_eq!((l[0].channels, l[0].filters, l[0].depthwise), (32, 16, false)); + assert_eq!((l[2].channels, l[2].filters, l[2].depthwise), (16, 16, true)); + assert!(l.iter().all(|l| l.weights.iter().chain(&l.biases).all(|v| v.is_finite()))); + // Damaged weights are refused, not read past their end. + assert!(parse(&WEIGHTS[..WEIGHTS.len() - 1]).is_none()); + assert!(parse(b"nope").is_none()); + } + + /// The sum of every head's raw output on a fixed test image matches libfacedetection's own C++ + /// (computed with its f32 weights; ours are f16, hence the tolerance). + #[test] + fn heads_match_the_reference_implementation() { + let (w, h) = (96usize, 72usize); + let bgr: Vec<[f32; 3]> = (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + let (r, g, b) = ((x * 7 + y * 3) % 256, (x * x + y * 5) % 256, (x * y * 3 + 11) % 256); + [b as f32, g as f32, r as f32] + }) + .collect(); + let l = parse(WEIGHTS).unwrap(); + let levels = features(&bgr, w, h, &l).unwrap(); + let reference = + [27.647039, 549.579923, -1645.299678, 914.784787, -6.149318, 35.705980, -454.742148, 163.847111, 1.267630, 28.415509, -85.662257, 62.294208]; + let mut got = Vec::new(); + for (feat, heads) in levels.iter().zip(HEADS) { + for first in heads { + let o = conv_dp(feat, &l[first], &l[first + 1], false).unwrap(); + got.push(o.data.iter().map(|&v| f64::from(v)).sum::()); + } + } + for (g, r) in got.iter().zip(reference) { + assert!((g - r).abs() <= 0.01 * r.abs().max(10.0), "{got:?}"); + } + } + + #[test] + fn f16_decoding() { + assert_eq!(f16_to_f32(0x3c00), 1.0); + assert_eq!(f16_to_f32(0xc000), -2.0); + assert_eq!(f16_to_f32(0x0000), 0.0); + assert_eq!(f16_to_f32(0x3555), 0.333_251_95); + assert_eq!(f16_to_f32(0x0001), 2.0f32.powi(-24)); + } +} diff --git a/crates/algo/src/features.rs b/crates/algo/src/features.rs index ab98de757e..80de7b993e 100644 --- a/crates/algo/src/features.rs +++ b/crates/algo/src/features.rs @@ -45,12 +45,9 @@ pub const PATCH_RADIUS: usize = 15; struct Rng(u64); impl Rng { + /// The next value of the SplitMix64 sequence. fn next(&mut self) -> u64 { - self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); - let mut z = self.0; - z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); - z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); - z ^ (z >> 31) + crate::photo_util::splitmix64(&mut self.0) } fn unit(&mut self) -> f64 { (self.next() >> 11) as f64 / (1u64 << 53) as f64 @@ -60,25 +57,98 @@ impl Rng { } } +/// Columns per band of the vertical blur pass: 64 f32 = 256 bytes = four cache lines per row, so +/// walking down a band touches whole lines, and the band's working set stays in cache. +const BLUR_BAND: usize = 64; + +/// Box blur of radius `r` (edges clamped): a running f32 sum along rows, then along columns. +/// +/// Both passes are parallel and give bit for bit what the one-line-at-a-time version gave: every +/// output sums its window in the same order. Rows are independent. For the column pass, the +/// image is cut into bands of columns; each column keeps its own f32 running sum, started from +/// its first window summed in order and updated in the same order, so only the loop nest changes +/// (the inner loop runs across a band's columns and vectorises), never any column's arithmetic. fn blur(w: usize, h: usize, v: &[f32], r: usize) -> Vec { - let pass = |src: &[f32], horizontal: bool| -> Vec { - let mut dst = vec![0.0f32; src.len()]; - let (n, m) = if horizontal { (h, w) } else { (w, h) }; - for line in 0..n { - let at = |k: i64| { - let k = k.clamp(0, m as i64 - 1) as usize; - if horizontal { src[line * w + k] } else { src[k * w + line] } - }; - let mut acc: f32 = (-(r as i64)..=r as i64).map(at).sum(); - for k in 0..m { - let i = if horizontal { line * w + k } else { k * w + line }; - dst[i] = acc / (2 * r + 1) as f32; - acc += at(k as i64 + r as i64 + 1) - at(k as i64 - r as i64); + if w == 0 || h == 0 { + return Vec::new(); + } + let den = (2 * r + 1) as f32; + let mut tmp = vec![0.0f32; w * h]; + par_rows(&mut tmp, w, |line, dst| { + let src = &v[line * w..(line + 1) * w]; + let at = |k: i64| src[k.clamp(0, w as i64 - 1) as usize]; + let mut acc: f32 = (-(r as i64)..=r as i64).map(at).sum(); + for (k, o) in dst.iter_mut().enumerate() { + *o = acc / den; + acc += at(k as i64 + r as i64 + 1) - at(k as i64 - r as i64); + } + }); + let mut dst = vec![0.0f32; w * h]; + // One list of row segments per band, each segment a `&mut` slice of `dst`: no `unsafe`. + let mut bands: Vec> = (0..w.div_ceil(BLUR_BAND)).map(|_| Vec::with_capacity(h)).collect(); + for row in dst.chunks_exact_mut(w) { + for (band, seg) in bands.iter_mut().zip(row.chunks_mut(BLUR_BAND)) { + band.push(seg); + } + } + let src = &tmp; + let run = |bi: usize, segs: &mut [&mut [f32]], acc: &mut Vec| { + let (x0, bw) = (bi * BLUR_BAND, segs.first().map_or(0, |s| s.len())); + let row = |k: i64| { + let o = (k.clamp(0, h as i64 - 1) as usize) * w + x0; + &src[o..o + bw] + }; + // The first window, summed in order from the same start as `Iterator::sum` uses. + acc.clear(); + acc.resize(bw, std::iter::empty::().sum()); + for k in -(r as i64)..=r as i64 { + for (t, &x) in acc.iter_mut().zip(row(k)) { + *t += x; + } + } + for (k, seg) in segs.iter_mut().enumerate() { + let (add, sub) = (row(k as i64 + r as i64 + 1), row(k as i64 - r as i64)); + for (((o, t), &p), &m) in seg.iter_mut().zip(acc.iter_mut()).zip(add).zip(sub) { + *o = *t / den; + *t += p - m; } } - dst }; - pass(&pass(v, true), false) + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + bands.into_par_iter().enumerate().for_each_init(Vec::new, |acc, (bi, mut segs)| run(bi, &mut segs, acc)); + } + #[cfg(target_arch = "wasm32")] + { + let mut acc = Vec::new(); + for (bi, mut segs) in bands.into_iter().enumerate() { + run(bi, &mut segs, &mut acc); + } + } + dst +} + +/// Runs `f(y, row)` over the `w`-wide rows of `buf`, in parallel on native targets. +fn par_rows(buf: &mut [f32], w: usize, f: impl Fn(usize, &mut [f32]) + Sync + Send) { + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + buf.par_chunks_mut(w).enumerate().for_each(|(y, row)| f(y, row)); + } + #[cfg(target_arch = "wasm32")] + buf.chunks_mut(w).enumerate().for_each(|(y, row)| f(y, row)); +} + +/// [`par_rows`] over three row-aligned buffers in lockstep. +fn par_rows3(a: &mut [f32], b: &mut [f32], c: &mut [f32], w: usize, f: impl Fn(usize, &mut [f32], &mut [f32], &mut [f32]) + Sync + Send) { + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + a.par_chunks_mut(w).zip(b.par_chunks_mut(w)).zip(c.par_chunks_mut(w)).enumerate().for_each(|(y, ((a, b), c))| f(y, a, b, c)); + } + #[cfg(target_arch = "wasm32")] + a.chunks_mut(w).zip(b.chunks_mut(w)).zip(c.chunks_mut(w)).enumerate().for_each(|(y, ((a, b), c))| f(y, a, b, c)); } /// Harris corners: at most `max` corners at least `min_dist` pixels apart, strongest first, @@ -88,28 +158,40 @@ pub fn harris(w: usize, h: usize, img: &[f32], max: usize, min_dist: f32, valid: return Vec::new(); } let (mut xx, mut yy, mut xy) = (vec![0.0f32; w * h], vec![0.0f32; w * h], vec![0.0f32; w * h]); - for y in 1..h - 1 { + // Rows are independent (each output pixel reads only `img`), so they run in parallel. + par_rows3(&mut xx, &mut yy, &mut xy, w, |y, xxr, yyr, xyr| { + if y == 0 || y + 1 == h { + return; + } for x in 1..w - 1 { let p = |dx: i64, dy: i64| img[((y as i64 + dy) as usize) * w + (x as i64 + dx) as usize]; // Sobel. let gx = (p(1, -1) + 2.0 * p(1, 0) + p(1, 1)) - (p(-1, -1) + 2.0 * p(-1, 0) + p(-1, 1)); let gy = (p(-1, 1) + 2.0 * p(0, 1) + p(1, 1)) - (p(-1, -1) + 2.0 * p(0, -1) + p(1, -1)); - let i = y * w + x; - xx[i] = gx * gx; - yy[i] = gy * gy; - xy[i] = gx * gy; + xxr[x] = gx * gx; + yyr[x] = gy * gy; + xyr[x] = gx * gy; } - } + }); // Two box passes approximate the Gaussian window. let (xx, yy, xy) = (blur(w, h, &blur(w, h, &xx, 1), 1), blur(w, h, &blur(w, h, &yy, 1), 1), blur(w, h, &blur(w, h, &xy, 1), 1)); - let r: Vec = (0..w * h).map(|i| xx[i] * yy[i] - xy[i] * xy[i] - 0.04 * (xx[i] + yy[i]).powi(2)).collect(); + let mut r = vec![0.0f32; w * h]; + par_rows(&mut r, w, |y, row| { + for (x, o) in row.iter_mut().enumerate() { + let i = y * w + x; + *o = xx[i] * yy[i] - xy[i] * xy[i] - 0.04 * (xx[i] + yy[i]).powi(2); + } + }); let peak = r.iter().copied().fold(0.0f32, f32::max); if peak <= 0.0 { return Vec::new(); } let m = PATCH_RADIUS + 1; - let mut cand: Vec = Vec::new(); - for y in m..h - m { + // Rows are scanned in parallel and joined in row order; the sort below fully orders the + // candidates anyway (score, then y, then x), so the order they arrive in cannot matter. + let rows = par_map((h - m).saturating_sub(m), |i| { + let y = m + i; + let mut found: Vec = Vec::new(); for x in m..w - m { let v = r[y * w + x]; if v < peak * 0.01 { @@ -125,9 +207,11 @@ pub fn harris(w: usize, h: usize, img: &[f32], max: usize, min_dist: f32, valid: continue; } } - cand.push(Corner { x: x as f32, y: y as f32, score: v }); + found.push(Corner { x: x as f32, y: y as f32, score: v }); } - } + found + }); + let mut cand: Vec = rows.into_iter().flatten().collect(); cand.sort_by(|a, b| b.score.total_cmp(&a.score).then(a.y.total_cmp(&b.y)).then(a.x.total_cmp(&b.x))); // Minimum spacing via a coarse occupancy grid. let cell = min_dist.max(1.0); @@ -176,37 +260,37 @@ pub fn describe(w: usize, h: usize, img: &[f32], corners: &[Corner], upright: bo let (xi, yi) = ((x.round() as i64).clamp(0, w as i64 - 1) as usize, (y.round() as i64).clamp(0, h as i64 - 1) as usize); smooth[yi * w + xi] }; - corners - .iter() - .map(|c| { - let (s, co) = if upright { - (0.0, 1.0) - } else { - let (mut m10, mut m01) = (0.0f32, 0.0f32); - let r = PATCH_RADIUS as i64; - for dy in -r..=r { - for dx in -r..=r { - if dx * dx + dy * dy <= r * r { - let v = at(c.x + dx as f32, c.y + dy as f32); - m10 += dx as f32 * v; - m01 += dy as f32 * v; - } + // Every descriptor depends only on its own corner and the smoothed image: computed in + // parallel, kept in corner order. + par_map(corners.len(), |ci| { + let c = &corners[ci]; + let (s, co) = if upright { + (0.0, 1.0) + } else { + let (mut m10, mut m01) = (0.0f32, 0.0f32); + let r = PATCH_RADIUS as i64; + for dy in -r..=r { + for dx in -r..=r { + if dx * dx + dy * dy <= r * r { + let v = at(c.x + dx as f32, c.y + dy as f32); + m10 += dx as f32 * v; + m01 += dy as f32 * v; } } - m01.atan2(m10).sin_cos() - }; - let mut d = [0u64; 4]; - for (i, p) in pairs().iter().enumerate() { - let rot = |x: f32, y: f32| (c.x + x * co - y * s, c.y + x * s + y * co); - let (ax, ay) = rot(p[0], p[1]); - let (bx, by) = rot(p[2], p[3]); - if at(ax, ay) < at(bx, by) { - d[i / 64] |= 1 << (i % 64); - } } - d - }) - .collect() + m01.atan2(m10).sin_cos() + }; + let mut d = [0u64; 4]; + for (i, p) in pairs().iter().enumerate() { + let rot = |x: f32, y: f32| (c.x + x * co - y * s, c.y + x * s + y * co); + let (ax, ay) = rot(p[0], p[1]); + let (bx, by) = rot(p[2], p[3]); + if at(ax, ay) < at(bx, by) { + d[i / 64] |= 1 << (i % 64); + } + } + d + }) } pub fn hamming(a: &Descriptor, b: &Descriptor) -> u32 { @@ -560,3 +644,124 @@ mod tests { assert_eq!(hamming(&[0, 0, 0, 0], &[1, 3, 0, u64::MAX]), 67); } } + +/// The row / column-band parallel `blur`, `harris` and `describe` against the serial code they +/// replaced. Every output must stay bit for bit the same. +#[cfg(test)] +mod parallel_tests { + use super::*; + + /// The serial blur exactly as it was before the parallelisation. + fn ref_blur(w: usize, h: usize, v: &[f32], r: usize) -> Vec { + let pass = |src: &[f32], horizontal: bool| -> Vec { + let mut dst = vec![0.0f32; src.len()]; + let (n, m) = if horizontal { (h, w) } else { (w, h) }; + for line in 0..n { + let at = |k: i64| { + let k = k.clamp(0, m as i64 - 1) as usize; + if horizontal { src[line * w + k] } else { src[k * w + line] } + }; + let mut acc: f32 = (-(r as i64)..=r as i64).map(at).sum(); + for k in 0..m { + let i = if horizontal { line * w + k } else { k * w + line }; + dst[i] = acc / (2 * r + 1) as f32; + acc += at(k as i64 + r as i64 + 1) - at(k as i64 - r as i64); + } + } + dst + }; + pass(&pass(v, true), false) + } + + fn mix(mut z: u64) -> u64 { + z = z.wrapping_add(0x9E37_79B9_7F4A_7C15); + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + + /// Integer-only data with negatives, signed zeros and large values, so a different order of + /// additions cannot hide. + fn noise(len: usize, seed: u64) -> Vec { + (0..len as u64) + .map(|i| { + let z = mix(seed.wrapping_mul(0x1234_5678_9ABC).wrapping_add(i)); + let v = (z >> 40) as f32 / 16_777_216.0; + match z & 31 { + 0 => -v, + 1 => -0.0, + 2 => v * 1000.0, + _ => v, + } + }) + .collect() + } + + /// Blocks of random brightness: lots of strong corners, no libm in the making. Only the + /// recorded-hash test below uses it, and that one is Linux x86_64 only. + #[cfg(all(target_os = "linux", target_arch = "x86_64"))] + fn blocks(w: usize, h: usize, seed: u64) -> Vec { + (0..w * h).map(|i| (mix(seed + ((i % w) / 8) as u64 * 4096 + ((i / w) / 8) as u64) >> 40) as f32 / 16_777_216.0).collect() + } + + #[cfg(all(target_os = "linux", target_arch = "x86_64"))] + fn fnv(parts: &[u64]) -> u64 { + let mut h = 0xcbf2_9ce4_8422_2325u64; + for p in parts { + h ^= p; + h = h.wrapping_mul(0x0000_0100_0000_01b3); + } + h + } + + #[test] + fn blur_matches_the_serial_code() { + // Widths around the 64-column band (1 band, band + 1, several bands, a ragged last one), + // 1-wide and 1-high images, and radii 0, 1, 2, larger than the image. + let widths = [1usize, 2, 3, 31, 63, 64, 65, 127, 128, 129, 200]; + let heights = [1usize, 2, 3, 5, 17, 40]; + for (a, &w) in widths.iter().enumerate() { + for (b, &h) in heights.iter().enumerate() { + let v = noise(w * h, (a * 10 + b) as u64); + for r in [0usize, 1, 2, 3, 7, 50] { + let (got, want) = (blur(w, h, &v, r), ref_blur(w, h, &v, r)); + let (g, e): (Vec, Vec) = (got.iter().map(|x| x.to_bits()).collect(), want.iter().map(|x| x.to_bits()).collect()); + assert_eq!(g, e, "blur {w}x{h} r={r}"); + } + } + } + assert!(blur(0, 0, &[], 1).is_empty()); + } + + // `harris` and `describe` go through the platform's hypot / ln / cos / atan2 / sin_cos, so the + // hashes recorded from the old code only hold where those are the same functions. + #[cfg(all(target_os = "linux", target_arch = "x86_64"))] + #[test] + fn harris_and_describe_hashes_recorded_from_the_serial_code() { + // Sizes: a whole number of 64-column bands and a ragged one; with and without a validity mask. + for (w, h, max, md, want_corners, want_desc) in + [(211usize, 157usize, 300usize, 6.0f32, 0xeccc6928d3ae3b8cu64, 0xd1e6caae4d68f253u64), (128, 97, 60, 3.0, 0xe3f6615117fa7da7, 0x818ae7e965ecf88f)] + { + let img = blocks(w, h, 7); + let valid: Vec = (0..w * h).map(|i| !((i % w) > w / 2 && (i / w) < h / 3)).collect(); + let mut parts = Vec::new(); + for v in [None, Some(valid.as_slice())] { + let c = harris(w, h, &img, max, md, v); + for k in &c { + parts.extend([k.x.to_bits() as u64, k.y.to_bits() as u64, k.score.to_bits() as u64]); + } + parts.push(c.len() as u64); + } + assert_eq!(fnv(&parts), want_corners, "harris {w}x{h}"); + let c = harris(w, h, &img, max, md, None); + assert!(c.len() > 10, "{} corners", c.len()); + let mut parts = Vec::new(); + for upright in [false, true] { + for d in describe(w, h, &img, &c, upright) { + parts.extend(d); + } + } + assert_eq!(fnv(&parts), want_desc, "describe {w}x{h}"); + } + } +} diff --git a/crates/algo/src/gallery.rs b/crates/algo/src/gallery.rs index 53ed13d3b8..e0d2c44a9e 100644 --- a/crates/algo/src/gallery.rs +++ b/crates/algo/src/gallery.rs @@ -15,11 +15,9 @@ use crate::image::{Image, premultiply}; use crate::{BlurPath, Ctx, FieldPin, IrisPin, SpinPin}; #[cfg(test)] -#[path = "tests_iris.rs"] mod tests; #[cfg(test)] -#[path = "tests_tilt_shift.rs"] mod tilt_shift_tests; /// Gaussian σ for a gallery blur amount in px. diff --git a/crates/algo/src/tests_iris.rs b/crates/algo/src/gallery/tests.rs similarity index 99% rename from crates/algo/src/tests_iris.rs rename to crates/algo/src/gallery/tests.rs index 2d54c8f134..e822d7a938 100644 --- a/crates/algo/src/tests_iris.rs +++ b/crates/algo/src/gallery/tests.rs @@ -2,7 +2,6 @@ use super::*; use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_raster::{Interrupt, Surface}; -#[path = "tests_iris_reference.rs"] mod reference; fn image(rect: Rect, ch: usize) -> Image { diff --git a/crates/algo/src/tests_iris_reference.rs b/crates/algo/src/gallery/tests/reference.rs similarity index 100% rename from crates/algo/src/tests_iris_reference.rs rename to crates/algo/src/gallery/tests/reference.rs diff --git a/crates/algo/src/tests_tilt_shift.rs b/crates/algo/src/gallery/tilt_shift_tests.rs similarity index 93% rename from crates/algo/src/tests_tilt_shift.rs rename to crates/algo/src/gallery/tilt_shift_tests.rs index 88066ca9d6..5d3ea66678 100644 --- a/crates/algo/src/tests_tilt_shift.rs +++ b/crates/algo/src/gallery/tilt_shift_tests.rs @@ -2,7 +2,6 @@ use super::*; use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_raster::{Interrupt, Surface}; -#[path = "tests_tilt_shift_reference.rs"] mod reference; fn image(rect: Rect, ch: usize) -> Image { @@ -52,11 +51,19 @@ fn tilt_shift_matches_frozen_kernel_at_band_boundaries_and_edges() { } cases.push(params(80.0, (0.5, 0.5), 0.0, 5.0, 0.1)); cases.push(params(80.0, (-5.0, -5.0), 27.0, 0.1, 0.15)); + // The channel count and alpha are independent of the band geometry: a run takes a quarter of + // their combinations, a different quarter each day. + let sweep = photocraft_testkit::Sweep::new(4); + let mut case = 0; for ch in 1..=6 { let src = image(bounds.inflate(12), ch); for alpha in [false, true] { let ctx = Ctx { bounds, mode: ColorMode::Rgb, alpha }; for p in &cases { + case += 1; + if !sweep.includes(case) { + continue; + } check(&src, bounds, &ctx, p); check(&src, Rect::new(-7, 17, 15, 39), &ctx, p); } @@ -146,6 +153,10 @@ fn tilt_shift_depths_models_selection_and_tile_edges_match_reference() { selection.fill_rect(bounds, &[0.4]); selection.fill_rect(Rect::new(3, 19, 35, 44), &[1.0]); selection.fill_rect(Rect::new(10, 20, 20, 30), &[0.0]); + // Pixel formats and tile layouts are independent: a run takes a quarter of their combinations, + // a different quarter each day. + let sweep = photocraft_testkit::Sweep::new(4); + let mut case = 0; for mode in [ ColorMode::Rgb, ColorMode::Grayscale, @@ -167,6 +178,10 @@ fn tilt_shift_depths_models_selection_and_tile_edges_match_reference() { assert_eq!(p.halo_for(bounds), crate::Halo::Radius(radius)); for extent in [None, Some(bounds)] { for tile in [16, 47, 128] { + case += 1; + if !sweep.includes(case) { + continue; + } let got = crate::apply_tiled(&surface, &p, area, bounds, Some(&selection), tile, extent); let mut want = surface.clone(); for y in (area.y0..area.y1).step_by(tile as usize) { diff --git a/crates/algo/src/tests_tilt_shift_reference.rs b/crates/algo/src/gallery/tilt_shift_tests/reference.rs similarity index 100% rename from crates/algo/src/tests_tilt_shift_reference.rs rename to crates/algo/src/gallery/tilt_shift_tests/reference.rs diff --git a/crates/algo/src/histogram.rs b/crates/algo/src/histogram.rs index 4a4fb45e20..849b222c2a 100644 --- a/crates/algo/src/histogram.rs +++ b/crates/algo/src/histogram.rs @@ -28,6 +28,18 @@ impl RgbHistogram { /// Each visible pixel counts once, including partially transparent pixels. Non-finite /// pixels are rejected as a whole; transparent RGB cannot contribute phantom peaks. pub fn from_rgba(pixels: &[[f32; 4]]) -> Self { + // Camera Raw counts its whole proxy (~450k pixels) on every slider step, so the count + // runs in blocks across cores (#211, P36). Integer sums don't depend on the order the + // blocks finish in, so the result is the same as one pass over all pixels. + #[cfg(not(target_arch = "wasm32"))] + if pixels.len() >= PARALLEL_PIXELS { + use rayon::prelude::*; + return pixels.par_chunks(PARALLEL_PIXELS / 2).map(Self::count).reduce(Self::default, Self::merge); + } + Self::count(pixels) + } + + fn count(pixels: &[[f32; 4]]) -> Self { let mut h = Self::default(); for pixel in pixels { if !pixel[3].is_finite() { @@ -68,8 +80,29 @@ impl RgbHistogram { } h } + + /// Sum of two block counts. Saturating like the counters themselves. + #[cfg(not(target_arch = "wasm32"))] + fn merge(mut self, other: Self) -> Self { + let add = |a: &mut [u64], b: &[u64]| a.iter_mut().zip(b).for_each(|(a, b)| *a = a.saturating_add(*b)); + for (a, b) in self.channels.iter_mut().zip(&other.channels) { + add(a, b); + } + add(&mut self.underflow, &other.underflow); + add(&mut self.overflow, &other.overflow); + add(&mut self.shadows, &other.shadows); + add(&mut self.highlights, &other.highlights); + self.samples = self.samples.saturating_add(other.samples); + self.transparent = self.transparent.saturating_add(other.transparent); + self.invalid = self.invalid.saturating_add(other.invalid); + self + } } +/// Below this many pixels one pass is faster than splitting the count across cores. +#[cfg(not(target_arch = "wasm32"))] +const PARALLEL_PIXELS: usize = 64 * 1024; + /// Clipped channels of one visible straight RGBA pixel as bit masks (bit 0=R, 1=G, 2=B): /// `(shadows, highlights)` at exact endpoints ≤0 / ≥1, not rounded edge bins. /// Transparent and non-finite pixels clip nothing. @@ -94,6 +127,30 @@ pub fn clipping(p: [f32; 4]) -> (u8, u8) { mod tests { use super::*; + // The parallel path, and its threshold, exist only off wasm32. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn matches_the_single_pass_on_every_domain_and_across_block_edges() { + // Every special value at every channel, plus a dense ramp through each bin's rounding + // edge; lengths around the parallel threshold and its block size. + let special = [0.0, -0.0, 1.0, -1e-7, 1.0 + 1e-7, 0.5 / 255.0, 254.5 / 255.0, -3.0, 4.0, f32::NAN, f32::INFINITY, f32::NEG_INFINITY, f32::MIN_POSITIVE]; + let mut pool: Vec<[f32; 4]> = Vec::new(); + for (i, &v) in special.iter().enumerate() { + for alpha in [1.0, 0.5, 0.0, -1.0, f32::NAN] { + pool.push([v, special[(i + 3) % special.len()], 0.25, alpha]); + pool.push([0.75, v, special[(i + 5) % special.len()], alpha]); + } + } + pool.extend((0..4096).map(|i| { + let v = i as f32 / 4095.0 * 1.2 - 0.1; + [v, 1.0 - v, (v * 7.0).fract(), 1.0] + })); + for len in [0, 1, PARALLEL_PIXELS / 2 + 1, PARALLEL_PIXELS - 1, PARALLEL_PIXELS, 3 * PARALLEL_PIXELS + 17] { + let pixels: Vec<_> = (0..len).map(|i| pool[(i * 7919) % pool.len()]).collect(); + assert_eq!(RgbHistogram::from_rgba(&pixels), RgbHistogram::count(&pixels), "{len} pixels"); + } + } + #[test] fn exact_bins_and_channel_totals() { let pixels: Vec<_> = (0..BINS).map(|i| [i as f32 / 255.0, (255 - i) as f32 / 255.0, 0.5, 0.25]).collect(); diff --git a/crates/algo/src/inpaint.rs b/crates/algo/src/inpaint.rs index ee472b2296..c781140ac3 100644 --- a/crates/algo/src/inpaint.rs +++ b/crates/algo/src/inpaint.rs @@ -51,66 +51,25 @@ use crate::poisson::membrane_fill; mod proximity_integral; use proximity_integral::HoleIntegral; -/// Below this many cells a pass runs on the calling thread (thread hand-off costs more than it -/// saves). Every pass gives the same result either way. -const PAR_MIN: usize = 64 * 64; +mod common; +mod solver; +mod sources; +mod synthesis; -/// [`PAR_MIN`] for the passes that match or vote a patch per cell. -const PAR_MIN_PATCHES: usize = 16 * 16; - -/// Largest level (in cells) whose source patches' moments [`Solver::prepare`] keeps. -const SOURCE_MOMENTS_MAX: usize = 1 << 20; - -/// SplitMix64: tiny deterministic RNG. -#[derive(Clone)] -pub(crate) struct Rng(u64); -impl Rng { - pub(crate) fn new(seed: u64) -> Self { - Rng(seed ^ 0x9E37_79B9_7F4A_7C15) - } - pub(crate) fn next(&mut self) -> u64 { - self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); - let mut z = self.0; - z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); - z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); - z ^ (z >> 31) - } - /// Skip `n` numbers (the same state as after `n` calls of [`Rng::next`]). - pub(crate) fn skip(&mut self, n: u64) { - self.0 = self.0.wrapping_add(n.wrapping_mul(0x9E37_79B9_7F4A_7C15)); - } - /// Uniform integer in `lo..=hi`. - pub(crate) fn range(&mut self, lo: i32, hi: i32) -> i32 { - if hi <= lo { - return lo; - } - lo + (self.next() % (hi - lo + 1) as u64) as i32 - } -} - -/// How far a copied patch may be adjusted to its target, per channel: `gain · source + bias`, the -/// gain within `gain` and the bias within `±bias` (Image Melding's photometric transform). The -/// gain is the ratio of the patches' standard deviations, the bias what then matches their means. -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct Adapt { - pub gain: (f32, f32), - pub bias: f32, -} - -impl Adapt { - /// Copies are taken as they are. - pub const NONE: Adapt = Adapt { gain: (1.0, 1.0), bias: 0.0 }; - - pub fn is_none(&self) -> bool { - self.gain == (1.0, 1.0) && self.bias == 0.0 - } -} - -impl Default for Adapt { - fn default() -> Self { - Self::NONE - } -} +#[cfg(test)] +use common::Bounds; +pub(crate) use common::Rng; +pub use common::{Adapt, Px}; +use common::{Level, SOURCE_MOMENTS_MAX}; +use solver::Solver; +pub(crate) use solver::fit; +#[cfg(test)] +use solver::least_sum; +use sources::{Coarser, Sources, THIN_RATIO, downsample, hole_bounds, hole_thickness, peel_affordable, targets_around, valid_sources, window_count}; +#[cfg(test)] +use sources::{hole_bbox, integral}; +use synthesis::onion_peel; +pub use synthesis::{best_offset, synthesize}; /// Parameters for [`complete`]. #[derive(Clone, Debug, PartialEq)] @@ -137,1413 +96,6 @@ impl Default for CompleteParams { } } -/// What a pixel is to [`complete_masked`]. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Px { - /// Not part of the image (a gap, or outside a transformed copy): ignored. - Void, - /// Known, part of the context the fill must agree with, but not copied from. - Known, - /// Known and may be copied from. - Source, - /// To be filled. - Hole, -} - -/// One pyramid level. Level 0 borrows the caller's buffers until it is solved. -struct Level<'a> { - w: usize, - h: usize, - img: Cow<'a, [f32]>, - px: Cow<'a, [Px]>, -} - -impl Level<'_> { - const EMPTY: Level<'static> = Level { w: 0, h: 0, img: Cow::Borrowed(&[]), px: Cow::Borrowed(&[]) }; -} - -/// The half-open box `[x0, x1) × [y0, y1)` of a level's grid. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -struct Bounds { - x0: usize, - y0: usize, - x1: usize, - y1: usize, -} - -impl Bounds { - /// Grown by `r` on every side, within a `w × h` grid. - fn grow(self, r: usize, w: usize, h: usize) -> Bounds { - Bounds { x0: self.x0.saturating_sub(r), y0: self.y0.saturating_sub(r), x1: self.x1.saturating_add(r).min(w), y1: self.y1.saturating_add(r).min(h) } - } - - /// The same box on the next coarser level (whose cells are 2 × 2 of these). - fn half(self) -> Bounds { - Bounds { x0: self.x0 / 2, y0: self.y0 / 2, x1: self.x1.div_ceil(2), y1: self.y1.div_ceil(2) } - } - - fn width(&self) -> usize { - self.x1.saturating_sub(self.x0) - } - - fn height(&self) -> usize { - self.y1.saturating_sub(self.y0) - } - - fn area(&self) -> usize { - self.width() * self.height() - } -} - -/// Half-size level: a cell is a hole if any of its pixels is; else it averages its non-void -/// pixels, and may be copied from only if all of them may. -fn downsample(l: &Level, ch: usize) -> Level<'static> { - let (w, h) = (l.w.div_ceil(2), l.h.div_ceil(2)); - let mut img = vec![0.0f32; w * h * ch]; - let mut px = vec![Px::Void; w * h]; - let (lw, lh, limg, lpx) = (l.w, l.h, &*l.img, &*l.px); - let row = |y: usize, irow: &mut [f32], prow: &mut [Px]| { - for (x, p) in prow.iter_mut().enumerate() { - let mut n = 0.0f32; - let o = x * ch; - let (mut hole, mut all_source, mut any) = (false, true, false); - for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { - let (sx, sy) = (x * 2 + dx, y * 2 + dy); - if sx >= lw || sy >= lh { - continue; - } - let si = sy * lw + sx; - match lpx[si] { - Px::Hole => hole = true, - Px::Void => all_source = false, - k => { - any = true; - all_source &= k == Px::Source; - for c in 0..ch { - irow[o + c] += limg[si * ch + c]; - } - n += 1.0; - } - } - } - if n > 0.0 { - for c in 0..ch { - irow[o + c] /= n; - } - } - *p = if hole { - Px::Hole - } else if !any { - Px::Void - } else if all_source { - Px::Source - } else { - Px::Known - }; - } - }; - par::chunks2_mut(&mut img, w * ch, &mut px, w, w * h >= PAR_MIN, row); - Level { w, h, img: Cow::Owned(img), px: Cow::Owned(px) } -} - -/// Summed-area table of a boolean grid (`(w+1) × (h+1)`). Not inlined: in [`synthesize`] it made -/// Create Texture's candidate loop slower (measured). -#[inline(never)] -fn integral(w: usize, h: usize, m: &[bool]) -> Vec { - let mut s = vec![0u32; (w + 1) * (h + 1)]; - for y in 0..h { - let mut row = 0u32; - for x in 0..w { - row += u32::from(m[y * w + x]); - s[(y + 1) * (w + 1) + x + 1] = s[y * (w + 1) + x + 1] + row; - } - } - s -} - -/// Count of set cells in the clipped window `[x0, x1) × [y0, y1)`. -fn window_count(s: &[u32], gw: usize, gh: usize, x0: i32, y0: i32, x1: i32, y1: i32) -> u32 { - let (x0, y0) = (x0.max(0) as usize, y0.max(0) as usize); - let (x1, y1) = ((x1.max(0) as usize).min(gw), (y1.max(0) as usize).min(gh)); - if x1 <= x0 || y1 <= y0 { - return 0; - } - let w1 = gw + 1; - s[y1 * w1 + x1] + s[y0 * w1 + x0] - s[y0 * w1 + x1] - s[y1 * w1 + x0] -} - -fn hole_bbox(w: usize, h: usize, hole: &[bool]) -> Option<(usize, usize, usize, usize)> { - let mut b: Option<(usize, usize, usize, usize)> = None; - for y in 0..h { - for x in 0..w { - if hole[y * w + x] { - b = Some(match b { - None => (x, y, x + 1, y + 1), - Some((x0, y0, x1, y1)) => (x0.min(x), y0.min(y), x1.max(x + 1), y1.max(y + 1)), - }); - } - } - } - b -} - -/// A hole at most `1 / THIN_RATIO` as thick as its bounds are long counts as thin (a wire, a -/// string, a hair, a long brush stroke): see the module docs. -const THIN_RATIO: usize = 4; - -/// How thick the hole within bounds `hb` of a `w`-wide role grid is: twice the largest distance -/// from a hole pixel to the nearest pixel that is not one (a long, thin hole is thin however long -/// its bounds). -fn hole_thickness(w: usize, hb: Bounds, px: &[Px]) -> usize { - // The bounds and a ring of non-hole cells around them (also past the grid's edge). - let (gw, gh) = (hb.width() + 2, hb.height() + 2); - let outside: Vec = par::map(gw * gh, gw * gh >= PAR_MIN, |i| { - let (gx, gy) = (i % gw, i / gw); - if gx == 0 || gy == 0 || gx > hb.width() || gy > hb.height() { - return true; - } - px.get((hb.y0 + gy - 1) * w + hb.x0 + gx - 1) != Some(&Px::Hole) - }); - let deepest = crate::selection::edt(&outside, gw, gh).into_iter().fold(0.0f32, f32::max); - (2.0 * deepest).ceil() as usize -} - -/// Most target pixels × sources the onion peel of a thin hole may compare (its search is -/// exhaustive, though most comparisons stop early: about a second at this many). -const PEEL_BUDGET: usize = 1 << 28; - -/// Whether the onion peel of the hole cells within `hb` of a `w`-wide role grid from `sources` -/// source centres stays within [`PEEL_BUDGET`]. -fn peel_affordable(w: usize, px: &[Px], hb: Bounds, sources: usize) -> bool { - let holes: usize = (hb.y0..hb.y1).map(|y| px.get(y * w + hb.x0..y * w + hb.x1).map_or(0, |row| row.iter().filter(|k| **k == Px::Hole).count())).sum(); - holes.saturating_mul(sources) <= PEEL_BUDGET -} - -/// Bounds of the hole pixels of a role grid. -fn hole_bounds(w: usize, h: usize, px: &[Px]) -> Option { - // Per row the first and last hole column; the union of the rows' spans. - let rows: Vec> = par::map(h, w * h >= PAR_MIN, |y| { - let row = px.get(y * w..(y + 1) * w)?; - let x0 = row.iter().position(|k| *k == Px::Hole)?; - let x1 = row.iter().rposition(|k| *k == Px::Hole).unwrap_or(x0) + 1; - Some((x0, x1)) - }); - let mut b: Option = None; - for (y, span) in rows.into_iter().enumerate() { - if let Some((x0, x1)) = span { - b = Some(match b { - None => Bounds { x0, y0: y, x1, y1: y + 1 }, - Some(b) => Bounds { x0: b.x0.min(x0), y0: b.y0, x1: b.x1.max(x1), y1: y + 1 }, - }); - } - } - b -} - -/// Valid source centres: the whole `2r+1` patch inside the grid and copyable. Found per row -/// (each pixel's horizontal run), then per column of runs. `None` when cancelled. -fn valid_sources(w: usize, h: usize, r: usize, px: &[Px], ctl: &photocraft_raster::Interrupt) -> Option> { - const BAND: usize = 64; - let side = 2 * r + 1; - let par = w * h >= PAR_MIN; - let mut run = vec![false; w * h]; - if w < side || h < side { - return Some(run); - } - // Per row: is the run of `side` pixels centred on each one all copyable (a sliding count)? - par::chunks_mut(&mut run, w, par, |y, out| { - if y % BAND == 0 && ctl.cancelled() { - return; - } - let row = &px[y * w..(y + 1) * w]; - let copyable = |x: usize| usize::from(row[x] == Px::Source); - let mut count: usize = (0..side).map(copyable).sum(); - out[r] = count == side; - for (x, o) in out.iter_mut().enumerate().take(w - r).skip(r + 1) { - count = count + copyable(x + r) - copyable(x - r - 1); - *o = count == side; - } - }); - if ctl.cancelled() { - return None; - } - // Per column, in bands of rows: how many such runs end each row in a row; a centre is valid - // where `side` of them stack up to `r` rows below it. - let mut valid = vec![false; w * h]; - par::chunks_mut(&mut valid, BAND * w, par, |band, out| { - if ctl.cancelled() { - return; - } - let (y0, rows) = (band * BAND, out.len() / w); - let mut stack = vec![0usize; w]; - for yy in y0.saturating_sub(r)..(y0 + rows + r).min(h) { - for (x, n) in stack.iter_mut().enumerate() { - *n = if run[yy * w + x] { *n + 1 } else { 0 }; - } - // `yy` is the bottom row of centre `y`'s patch. - if yy < y0 + r || yy < 2 * r { - continue; - } - let y = yy - r; - for (v, n) in out[(y - y0) * w..(y - y0 + 1) * w].iter_mut().zip(&stack).take(w - r).skip(r) { - *v = *n >= side; - } - } - }); - if ctl.cancelled() { - return None; - } - Some(valid) -} - -/// Columns per block of [`Sources`]' index. -const SOURCE_BLOCK: usize = 64; - -/// The valid source centres in row-major order, indexed by counts per row and per block of -/// [`SOURCE_BLOCK`] columns (so the `k`-th is found without listing them all). -struct Sources { - /// Valid centres before each row (`h + 1` entries). - row_start: Vec, - /// Per row, the valid centres in it before each block. - block_start: Vec, - blocks: usize, -} - -impl Sources { - fn new(valid: &[bool], w: usize) -> Sources { - let h = valid.len().checked_div(w).unwrap_or(0); - let blocks = w.div_ceil(SOURCE_BLOCK); - let mut block_start = vec![0u32; h * blocks]; - // Per row: the block prefix, and the row's count. - let counts: Vec = par::map(h, valid.len() >= PAR_MIN, |y| valid[y * w..(y + 1) * w].iter().filter(|v| **v).count()); - par::chunks_mut(&mut block_start, blocks, valid.len() >= PAR_MIN, |y, out| { - let mut n = 0u32; - for (b, o) in out.iter_mut().enumerate() { - *o = n; - n += valid[y * w + b * SOURCE_BLOCK..y * w + ((b + 1) * SOURCE_BLOCK).min(w)].iter().filter(|v| **v).count() as u32; - } - }); - let mut row_start = Vec::with_capacity(counts.len() + 1); - let mut total = 0usize; - row_start.push(0); - for c in counts { - total += c; - row_start.push(total); - } - Sources { row_start, block_start, blocks } - } - - fn len(&self) -> usize { - self.row_start.last().copied().unwrap_or(0) - } - - /// The `k`-th valid centre in row-major order (`k < len`). - fn nth(&self, k: usize, valid: &[bool], w: usize) -> (i32, i32) { - let y = self.row_start.partition_point(|&s| s <= k).saturating_sub(1); - let left = k.saturating_sub(self.row_start.get(y).copied().unwrap_or(0)); - let row_blocks = self.block_start.get(y * self.blocks..(y + 1) * self.blocks).unwrap_or(&[]); - let b = row_blocks.partition_point(|&s| s as usize <= left).saturating_sub(1); - let mut left = left.saturating_sub(row_blocks.get(b).copied().unwrap_or(0) as usize); - let x0 = b * SOURCE_BLOCK; - for (x, v) in valid.iter().skip(y * w + x0).take(w.saturating_sub(x0)).enumerate() { - if *v { - if left == 0 { - return ((x0 + x) as i32, y as i32); - } - left -= 1; - } - } - (0, 0) - } - - /// All of them, in row-major order. - fn all(&self, valid: &[bool], w: usize) -> Vec<(i32, i32)> { - let mut out = Vec::with_capacity(self.len()); - for (i, v) in valid.iter().enumerate() { - if *v { - out.push(((i % w) as i32, (i / w) as i32)); - } - } - out - } -} - -/// Per cell of `tb` (the hole's bounds `hb` grown by the patch radius, where all of them lie): is -/// it a target centre (its patch overlaps the hole)? -fn targets_around(w: usize, r: usize, px: &[Px], hb: Bounds, tb: Bounds) -> Vec { - // Hole counts summed over the hole's bounds: every hole pixel lies inside them. - let (bw, bh) = (hb.width(), hb.height()); - let mut s = vec![0u32; (bw + 1) * (bh + 1)]; - for y in 0..bh { - let mut row = 0u32; - for x in 0..bw { - row += u32::from(px[(hb.y0 + y) * w + hb.x0 + x] == Px::Hole); - s[(y + 1) * (bw + 1) + x + 1] = s[y * (bw + 1) + x + 1] + row; - } - } - let mut targets = vec![false; tb.area()]; - let ri = r as i32; - let (ox, oy) = (hb.x0 as i32, hb.y0 as i32); - for y in tb.y0..tb.y1 { - for x in tb.x0..tb.x1 { - let k = (y - tb.y0) * tb.width() + (x - tb.x0); - let (x, y) = (x as i32, y as i32); - targets[k] = window_count(&s, bw, bh, x - ri - ox, y - ri - oy, x + ri + 1 - ox, y + ri + 1 - oy) > 0; - } - } - targets -} - -/// A finished coarser level: its size, pixels and match field (the source centre per cell of -/// its target box, `i32::MIN` where there is none; empty when it had nothing to copy). -struct Coarser { - w: usize, - h: usize, - img: Vec, - field: Vec<(i32, i32)>, - tbox: Bounds, -} - -impl Coarser { - /// The match of cell `(x, y)` (`i32::MIN` outside the target box). - fn field_at(&self, x: usize, y: usize) -> (i32, i32) { - let t = self.tbox; - if x < t.x0 || x >= t.x1 || y < t.y0 || y >= t.y1 { - return (i32::MIN, i32::MIN); - } - self.field.get((y - t.y0) * t.width() + (x - t.x0)).copied().unwrap_or((i32::MIN, i32::MIN)) - } -} - -/// Most channels whose moments the colour adaptation fits (the rest are copied as they are). -const MOM_CH: usize = 8; - -/// Per-level PatchMatch/EM state. -struct Solver<'a> { - w: usize, - h: usize, - ch: usize, - r: i32, - img: &'a mut Vec, - px: &'a [Px], - /// Valid source centres: the whole patch is inside the grid and may be copied from. - valid: Vec, - /// The hole's bounds and the targets' (the hole's grown by the patch radius): nothing outside - /// them changes or is matched. The match field, its costs and these per-target tables are - /// kept over `tbox`, row-major. - hole_box: Bounds, - tbox: Bounds, - /// Target centres (the patch overlaps the hole). - targets: Vec, - /// Per cell of `tbox`: the whole patch is inside the grid and not void (so its distances - /// need no bounds checks and its own moments can be kept). - full: Vec, - /// With colour adaptation, per cell of `tbox` and channel (at most [`MOM_CH`]): `Σt, Σt²` of - /// the patch, for full patches, as of the last [`Solver::costs`]. - tmom: Vec<[f32; 2]>, - /// With colour adaptation where it pays: per source centre and channel, `Σs, Σs²` of its - /// patch (source pixels never change within a level). Empty otherwise. - smom: Vec<[f32; 2]>, - adapt: Adapt, - /// Cancellation: checked per row band; a cancelled solve's output is discarded. - ctl: photocraft_raster::Interrupt<'a>, -} - -/// A channel deviation under which a patch counts as flat for [`fit`] (about 5 levels of 255). -const FLAT: f32 = 0.02; - -/// Gain and bias of one channel for target moments `(Σt, Σt²)` and source moments `(Σs, Σs²)` -/// over `n` samples, within the limits. -#[inline] -pub(crate) fn fit(a: Adapt, n: f32, st: f32, stt: f32, ss: f32, sss: f32) -> (f32, f32) { - let (mt, ms) = (st / n, ss / n); - let vt = (stt / n - mt * mt).max(0.0); - let vs = (sss / n - ms * ms).max(0.0); - let g = if vs > 1e-10 { (vt / vs).sqrt() } else { 1.0 }; - let (mut lo, mut hi) = a.gain; - // Nearly flat patches of about the same colour (their means within the bias of each other): - // the ratio of their deviations is noise, so match the mean first, keeping the gain where the - // bias can still bring the source's mean to the target's. Gained about zero, a gain at its - // limit moved bright means further than the bias could bring back, and each channel by a - // different amount: a tinted blotch in a smooth gradient. Textured patches, and flat ones - // whose colours differ more (Content-Aware Move's Color), are fitted as before. - if vt.max(vs) < FLAT * FLAT && (mt - ms).abs() <= a.bias && ms > 1e-6 { - let (glo, ghi) = ((mt - a.bias) / ms, (mt + a.bias) / ms); - if glo.max(lo) <= ghi.min(hi) { - (lo, hi) = (glo.max(lo), ghi.min(hi)); - } - } - let g = g.clamp(lo, hi); - let b = (mt - g * ms).clamp(-a.bias, a.bias); - (g, b) -} - -impl Solver<'_> { - #[inline] - fn is_valid(&self, x: i32, y: i32) -> bool { - x >= 0 && y >= 0 && (x as usize) < self.w && (y as usize) < self.h && self.valid[y as usize * self.w + x as usize] - } - - /// Mark the full target patches (`full`), and keep the source moments where they pay off. - fn prepare(&mut self, keep_source_moments: bool) { - let (w, h, r, tb) = (self.w, self.h, self.r as usize, self.tbox); - let side = 2 * r + 1; - let mut full = vec![false; tb.area()]; - let px = self.px; - par::chunks_mut(&mut full, tb.width(), tb.area() >= PAR_MIN_PATCHES, |ty, out| { - let y = tb.y0 + ty; - if y < r || y + r >= h { - return; - } - for (tx, f) in out.iter_mut().enumerate() { - let x = tb.x0 + tx; - if x < r || x + r >= w { - continue; - } - *f = (y - r..=y + r).all(|yy| px[yy * w + x - r..yy * w + x - r + side].iter().all(|k| *k != Px::Void)); - } - }); - self.full = full; - if self.adapt.is_none() { - return; - } - let c8 = self.ch.min(MOM_CH); - self.tmom = vec![[0.0; 2]; tb.area() * c8]; - if !keep_source_moments { - return; - } - let (ch, img, valid, ctl) = (self.ch, &*self.img, &self.valid, &self.ctl); - let mut smom = vec![[0.0f32; 2]; w * h * c8]; - par::chunks_mut(&mut smom, w * c8, w * h >= PAR_MIN_PATCHES, |y, out| { - if ctl.cancelled() { - return; - } - for x in 0..w { - if valid[y * w + x] { - patch_moments(img, w, ch, c8, side, x - r, y - r, &mut out[x * c8..(x + 1) * c8]); - } - } - }); - self.smom = smom; - } - - /// Distance between the target patch at `t` (its pixels inside the grid and not void; `k` its - /// index in the `tbox` tables) and the source patch at `s`, per sample; with colour adaptation - /// the source is fitted first. - #[inline] - fn dist(&self, t: (i32, i32), k: usize, s: (i32, i32), cutoff: f32) -> f32 { - let full = self.full[k]; - if !self.adapt.is_none() { - return if full { self.dist_adapted_full(k, t, s) } else { self.dist_adapted(t, s) }; - } - if full { self.dist_full(t, s, cutoff) } else { self.dist_clipped(t, s, cutoff) } - } - - /// [`Solver::dist`] without adaptation, of a patch that may be clipped. - fn dist_clipped(&self, t: (i32, i32), s: (i32, i32), cutoff: f32) -> f32 { - let (w, h, ch, r) = (self.w as i32, self.h as i32, self.ch, self.r); - let full = ((2 * r + 1) * (2 * r + 1)) as f32 * ch as f32; - let mut sum = 0.0f32; - let mut n = 0usize; - for dy in -r..=r { - let ty = t.1 + dy; - if ty < 0 || ty >= h { - continue; - } - let sy = s.1 + dy; - for dx in -r..=r { - let tx = t.0 + dx; - if tx < 0 || tx >= w || self.px[ty as usize * self.w + tx as usize] == Px::Void { - continue; - } - let ti = (ty as usize * self.w + tx as usize) * ch; - let si = (sy as usize * self.w + (s.0 + dx) as usize) * ch; - for c in 0..ch { - let d = self.img[ti + c] - self.img[si + c]; - sum += d * d; - } - n += ch; - } - // Early out: the mean can only be ≥ sum / (full patch size). - if sum > cutoff * full { - return f32::INFINITY; - } - } - if n == 0 { f32::INFINITY } else { sum / n as f32 } - } - - /// [`Solver::dist_clipped`] of a full patch: the same sums, in the same order. It gives up - /// (infinity) as soon as the sum shows the patch is worse than `cutoff`, which the sum, only - /// growing, can show after any pixel: the caller only asks whether it is better. - fn dist_full(&self, t: (i32, i32), s: (i32, i32), cutoff: f32) -> f32 { - let (w, ch, r) = (self.w, self.ch, self.r); - let side = (2 * r + 1) as usize; - let full = ((2 * r + 1) * (2 * r + 1)) as f32 * ch as f32; - let (span, limit) = (side * ch, cutoff * full); - let mut sum = 0.0f32; - for dy in -r..=r { - let ti = ((t.1 + dy) as usize * w + (t.0 - r) as usize) * ch; - let si = ((s.1 + dy) as usize * w + (s.0 - r) as usize) * ch; - for (tp, sp) in self.img[ti..ti + span].chunks_exact(ch).zip(self.img[si..si + span].chunks_exact(ch)) { - for (a, b) in tp.iter().zip(sp) { - let d = a - b; - sum += d * d; - } - if sum > limit { - return f32::INFINITY; - } - } - } - sum / (side * side * ch) as f32 - } - - /// Moments of the target patch at `t` and the source patch at `s` per channel: - /// `[Σt, Σt², Σs, Σs², Σts]`, and the sample count. - fn moments(&self, t: (i32, i32), s: (i32, i32), m: &mut [[f32; 5]]) -> usize { - let (w, h, ch, r) = (self.w as i32, self.h as i32, self.ch, self.r); - m.iter_mut().for_each(|v| *v = [0.0; 5]); - let mut n = 0usize; - for dy in -r..=r { - let ty = t.1 + dy; - if ty < 0 || ty >= h { - continue; - } - let sy = s.1 + dy; - for dx in -r..=r { - let tx = t.0 + dx; - if tx < 0 || tx >= w || self.px[ty as usize * self.w + tx as usize] == Px::Void { - continue; - } - let ti = (ty as usize * self.w + tx as usize) * ch; - let si = (sy as usize * self.w + (s.0 + dx) as usize) * ch; - for (c, mc) in m.iter_mut().enumerate().take(ch) { - let (a, b) = (self.img[ti + c], self.img[si + c]); - mc[0] += a; - mc[1] += a * a; - mc[2] += b; - mc[3] += b * b; - mc[4] += a * b; - } - n += 1; - } - } - n - } - - /// [`Solver::moments`] of a full target patch (`k` in the `tbox` tables): the target's own - /// sums kept from [`Solver::costs`], the source's from [`Solver::prepare`] when kept, each - /// summed in the same order as there. - fn moments_full(&self, k: usize, t: (i32, i32), s: (i32, i32), m: &mut [[f32; 5]]) { - let (w, ch, r) = (self.w, self.ch, self.r); - let c8 = m.len(); - let side = (2 * r + 1) as usize; - let tm = &self.tmom[k * c8..(k + 1) * c8]; - let sm = self.smom.get((s.1 as usize * w + s.0 as usize) * c8..(s.1 as usize * w + s.0 as usize + 1) * c8); - let mut acc = [[0.0f32; 3]; MOM_CH]; - for dy in -r..=r { - let ti = ((t.1 + dy) as usize * w + (t.0 - r) as usize) * ch; - let si = ((s.1 + dy) as usize * w + (s.0 - r) as usize) * ch; - let (trow, srow) = (&self.img[ti..ti + side * ch], &self.img[si..si + side * ch]); - if sm.is_some() { - for (tp, sp) in trow.chunks_exact(ch).zip(srow.chunks_exact(ch)) { - for c in 0..c8 { - acc[c][2] += tp[c] * sp[c]; - } - } - } else { - for (tp, sp) in trow.chunks_exact(ch).zip(srow.chunks_exact(ch)) { - for c in 0..c8 { - let (a, b) = (tp[c], sp[c]); - acc[c][0] += b; - acc[c][1] += b * b; - acc[c][2] += a * b; - } - } - } - } - for (c, mc) in m.iter_mut().enumerate() { - let [ss, sss] = match sm { - Some(sm) => sm[c], - None => [acc[c][0], acc[c][1]], - }; - *mc = [tm[c][0], tm[c][1], ss, sss, acc[c][2]]; - } - } - - fn dist_adapted(&self, t: (i32, i32), s: (i32, i32)) -> f32 { - let mut m = [[0.0f32; 5]; MOM_CH]; - let ch = self.ch.min(MOM_CH); - let n = self.moments(t, s, &mut m[..ch]); - if n == 0 { - return f32::INFINITY; - } - adapted_cost(self.adapt, n as f32, &m[..ch]) - } - - fn dist_adapted_full(&self, k: usize, t: (i32, i32), s: (i32, i32)) -> f32 { - let mut m = [[0.0f32; 5]; MOM_CH]; - let ch = self.ch.min(MOM_CH); - self.moments_full(k, t, s, &mut m[..ch]); - let side = (2 * self.r + 1) as usize; - adapted_cost(self.adapt, (side * side) as f32, &m[..ch]) - } - - /// Gain and bias per channel for the match `t → s` (identity without adaptation). - fn transform(&self, t: (i32, i32), k: usize, s: (i32, i32), out: &mut [(f32, f32)]) { - if self.adapt.is_none() { - out.iter_mut().for_each(|v| *v = (1.0, 0.0)); - return; - } - let mut m = [[0.0f32; 5]; MOM_CH]; - let ch = self.ch.min(MOM_CH); - let n = if self.full[k] { - self.moments_full(k, t, s, &mut m[..ch]); - let side = (2 * self.r + 1) as usize; - side * side - } else { - self.moments(t, s, &mut m[..ch]) - }; - for (c, o) in out.iter_mut().enumerate() { - *o = if n == 0 || c >= ch { (1.0, 0.0) } else { fit(self.adapt, n as f32, m[c][0], m[c][1], m[c][2], m[c][3]) }; - } - } - - /// One PatchMatch step at target `t` (`k` in the target box) whose match is `cur`: propagation - /// from the matches of the neighbours visited just before it (`left` in its row, `up` in its - /// column, in this pass's direction `step`), then random search in exponentially shrinking - /// windows around the best (two numbers from `rng` per window). - #[allow(clippy::too_many_arguments)] - #[inline] - fn pm_step( - &self, - t: (i32, i32), - k: usize, - cur: ((i32, i32), f32), - left: Option<(i32, i32)>, - up: Option<(i32, i32)>, - step: i32, - rng: &mut Rng, - search_radius: i32, - ) -> ((i32, i32), f32) { - let (mut best, mut bc) = cur; - if let Some(c) = left { - let cand = (c.0 + step, c.1); - if self.is_valid(cand.0, cand.1) && cand != best { - let d = self.dist(t, k, cand, bc); - if d < bc { - (best, bc) = (cand, d); - } - } - } - if let Some(c) = up { - let cand = (c.0, c.1 + step); - if self.is_valid(cand.0, cand.1) && cand != best { - let d = self.dist(t, k, cand, bc); - if d < bc { - (best, bc) = (cand, d); - } - } - } - let mut rad = search_radius; - while rad >= 1 { - let cand = (best.0 + rng.range(-rad, rad), best.1 + rng.range(-rad, rad)); - if self.is_valid(cand.0, cand.1) && cand != best { - let d = self.dist(t, k, cand, bc); - if d < bc { - (best, bc) = (cand, d); - } - } - rad /= 2; - } - (best, bc) - } - - /// One PatchMatch pass over all target rows, in bands of the grid's rows: each band is - /// visited in raster order (backwards on odd passes), propagating within itself, its random - /// numbers seeded by its index. `nnf` and `cost` are over the target box. The bands run in - /// parallel; when there are fewer of them than threads, each is also swept in wavefronts of - /// blocks (a cell depends only on its neighbours before it in its row and its column), which - /// visits every cell with the same neighbours and random numbers as the raster order. - fn patchmatch(&self, nnf: &mut [(i32, i32)], cost: &mut [f32], pass: usize, seed: u64, search_radius: i32) { - const BAND: usize = 16; - let tb = self.tbox; - let tw = tb.width(); - if tw == 0 || tb.height() == 0 || nnf.len() < tb.area() || cost.len() < tb.area() { - return; - } - let reverse = pass % 2 == 1; - let step: i32 = if reverse { -1 } else { 1 }; - let band_seed = |bi: usize| seed ^ (bi as u64).wrapping_mul(0x2545_F491_4F6C_DD1D) ^ ((pass as u64) << 40); - // The bands crossing the target box: index, first row, rows in the box. - let mut bands = Vec::new(); - let mut y = tb.y0; - while y < tb.y1 { - let bi = y / BAND; - let end = ((bi + 1) * BAND).min(tb.y1); - bands.push((bi, y, end - y)); - y = end; - } - let threads = par::threads(); - if tb.area() >= PAR_MIN_PATCHES * 8 && !bands.is_empty() && bands.len() < threads { - self.patchmatch_wavefront(nnf, cost, &bands, reverse, &band_seed, search_radius); - return; - } - let band_fn = |bi: usize, ys: usize, nn: &mut [(i32, i32)], co: &mut [f32]| { - if self.ctl.cancelled() { - return; - } - let rows = nn.len() / tw; - let mut rng = Rng::new(band_seed(bi)); - for ry in 0..rows { - if self.ctl.cancelled() { - return; - } - let ly = if reverse { rows - 1 - ry } else { ry }; - let y = ys + ly; - for rx in 0..tw { - let lx = if reverse { tw - 1 - rx } else { rx }; - let li = ly * tw + lx; - let k = (y - tb.y0) * tw + lx; - if !self.targets[k] { - continue; - } - // Neighbours outside the target box hold no match: nothing to try. - let nx = lx as i32 - step; - let left = (nx >= 0 && (nx as usize) < tw).then(|| nn[ly * tw + nx as usize]); - let nly = ly as i32 - step; - let up = (nly >= 0 && (nly as usize) < rows).then(|| nn[nly as usize * tw + lx]); - let t = ((tb.x0 + lx) as i32, y as i32); - (nn[li], co[li]) = self.pm_step(t, k, (nn[li], co[li]), left, up, step, &mut rng, search_radius); - } - } - }; - // The target box's rows, band by band. - let mut parts = Vec::with_capacity(bands.len()); - let (mut nn_rest, mut co_rest) = (&mut nnf[..tb.area()], &mut cost[..tb.area()]); - for &(bi, ys, rows) in &bands { - let (nn, nr) = std::mem::take(&mut nn_rest).split_at_mut(rows * tw); - let (co, cr) = std::mem::take(&mut co_rest).split_at_mut(rows * tw); - parts.push((bi, ys, nn, co)); - (nn_rest, co_rest) = (nr, cr); - } - let par = tb.area() >= PAR_MIN_PATCHES && parts.len() > 1; - par::each(parts, par, |(bi, ys, nn, co)| band_fn(bi, ys, nn, co)); - } - - /// [`Solver::patchmatch`] swept in wavefronts: the cells of each band in blocks of a row, a - /// block once the block before it in its row and the one above it (in the pass's direction) - /// are done. A target's random numbers are those it would draw in raster order (each target - /// draws the same count). - fn patchmatch_wavefront( - &self, - nnf: &mut [(i32, i32)], - cost: &mut [f32], - bands: &[(usize, usize, usize)], - reverse: bool, - band_seed: &(dyn Fn(usize) -> u64 + Sync), - search_radius: i32, - ) { - use std::sync::atomic::{AtomicU32, AtomicU64, Ordering::Relaxed}; - - let tb = self.tbox; - let tw = tb.width(); - let step: i32 = if reverse { -1 } else { 1 }; - let pack = |(x, y): (i32, i32)| (u64::from(x as u32) << 32) | u64::from(y as u32); - let unpack = |v: u64| ((v >> 32) as u32 as i32, v as u32 as i32); - // Random numbers each target draws. - let mut draws = 0u64; - let mut rad = search_radius; - while rad >= 1 { - draws += 2; - rad /= 2; - } - // Targets before each column of each row of the box. - let mut before = vec![0u32; tb.height() * (tw + 1)]; - for (ry, row) in before.chunks_mut(tw + 1).enumerate() { - for lx in 0..tw { - row[lx + 1] = row[lx] + u32::from(self.targets[ry * tw + lx]); - } - } - // Targets visited before each of a band's rows (in the pass's order). - let row_base: Vec> = bands - .iter() - .map(|&(_, ys, rows)| { - let mut base = Vec::with_capacity(rows); - let mut n = 0u64; - for pr in 0..rows { - base.push(n); - let ly = if reverse { rows - 1 - pr } else { pr }; - n += u64::from(before[(ys + ly - tb.y0) * (tw + 1) + tw]); - } - base - }) - .collect(); - let block = (tw / 8).clamp(4, 32); - let blocks = tw.div_ceil(block); - let nn: Vec = nnf[..tb.area()].iter().map(|m| AtomicU64::new(pack(*m))).collect(); - let co: Vec = cost[..tb.area()].iter().map(|c| AtomicU32::new(c.to_bits())).collect(); - let sweep = |(j, pr, b): (usize, usize, usize)| { - let (bi, ys, rows) = bands[j]; - let ly = if reverse { rows - 1 - pr } else { pr }; - let y = ys + ly; - let ry = y - tb.y0; - let (pc0, pc1) = (b * block, ((b + 1) * block).min(tw)); - // The targets visited before this block, in raster order. - let first = if reverse { tw - 1 - pc0 } else { pc0 }; - let row = &before[ry * (tw + 1)..(ry + 1) * (tw + 1)]; - let in_row = if reverse { row[tw] - row[first + 1] } else { row[first] }; - let mut rng = Rng::new(band_seed(bi)); - rng.skip((row_base[j][pr] + u64::from(in_row)) * draws); - for pc in pc0..pc1 { - let lx = if reverse { tw - 1 - pc } else { pc }; - let k = ry * tw + lx; - if !self.targets[k] { - continue; - } - let nx = lx as i32 - step; - let left = (nx >= 0 && (nx as usize) < tw).then(|| unpack(nn[ry * tw + nx as usize].load(Relaxed))); - let nly = ly as i32 - step; - let up = (nly >= 0 && (nly as usize) < rows).then(|| unpack(nn[(ys + nly as usize - tb.y0) * tw + lx].load(Relaxed))); - let cur = (unpack(nn[k].load(Relaxed)), f32::from_bits(co[k].load(Relaxed))); - let t = ((tb.x0 + lx) as i32, y as i32); - let (m, c) = self.pm_step(t, k, cur, left, up, step, &mut rng, search_radius); - nn[k].store(pack(m), Relaxed); - co[k].store(c.to_bits(), Relaxed); - } - }; - // Wavefront `d`: block `b` of (visited) row `pr` with `pr + b = d`, in every band. Each - // wave ends before the next starts. - let longest = bands.iter().map(|b| b.2).max().unwrap_or(0); - for d in 0..longest + blocks - 1 { - if self.ctl.cancelled() { - return; - } - let wave: Vec<(usize, usize, usize)> = bands - .iter() - .enumerate() - .flat_map(|(j, &(_, _, rows))| (0..rows).filter(move |&pr| pr <= d && d - pr < blocks).map(move |pr| (j, pr, d - pr))) - .collect(); - par::each(wave, true, sweep); - } - for (m, a) in nnf.iter_mut().zip(&nn) { - *m = unpack(a.load(Relaxed)); - } - for (c, a) in cost.iter_mut().zip(&co) { - *c = f32::from_bits(a.load(Relaxed)); - } - } - - /// Recompute every target's match cost (hole pixels change between iterations), and with - /// colour adaptation the full target patches' own moments. - fn costs(&mut self, nnf: &[(i32, i32)], cost: &mut [f32]) -> Result<(), photocraft_raster::Cancelled> { - let (w, ch, r, tb) = (self.w, self.ch, self.r as usize, self.tbox); - let par = tb.area() >= PAR_MIN_PATCHES; - self.ctl.check()?; - if !self.adapt.is_none() { - let c8 = ch.min(MOM_CH); - let side = 2 * r + 1; - let mut tmom = std::mem::take(&mut self.tmom); - let (img, full, ctl) = (&*self.img, &self.full, &self.ctl); - par::chunks_mut(&mut tmom, tb.width() * c8, par, |ty, out| { - if ctl.cancelled() { - return; - } - for tx in 0..tb.width() { - if full[ty * tb.width() + tx] { - patch_moments(img, w, ch, c8, side, tb.x0 + tx - r, tb.y0 + ty - r, &mut out[tx * c8..(tx + 1) * c8]); - } - } - }); - self.tmom = tmom; - } - let this = &*self; - par::chunks_mut(cost, tb.width(), par, |ty, co| { - if this.ctl.cancelled() { - return; - } - let y = tb.y0 + ty; - for (tx, c) in co.iter_mut().enumerate() { - let k = ty * tb.width() + tx; - if this.targets[k] { - *c = this.dist(((tb.x0 + tx) as i32, y as i32), k, nnf[k], f32::INFINITY); - } - } - }); - self.ctl.check() - } - - /// Re-estimate hole pixels by a similarity-weighted vote of all overlapping patch matches - /// (each copy adjusted by its gain and bias). - fn vote(&mut self, nnf: &[(i32, i32)], cost: &[f32]) { - let (w, h, ch, r) = (self.w, self.h, self.ch, self.r); - let (tb, hb) = (self.tbox, self.hole_box); - // A cancelled solve is thrown away. - if self.ctl.cancelled() { - return; - } - let used = |k: usize| self.targets[k] && cost[k].is_finite(); - // σ² from the 75th percentile of match costs (Wexler et al. use a robust scale like this). - let cs: Vec = (0..tb.area()).filter(|&k| used(k)).map(|k| cost[k]).collect(); - let mut cs = cs; - let sigma2 = if cs.is_empty() { - 1.0 - } else { - let k = (cs.len() * 3 / 4).min(cs.len() - 1); - let (_, v, _) = cs.select_nth_unstable_by(k, |a, b| a.total_cmp(b)); - v.max(1e-6) - }; - drop(cs); - // Each target's weight and photometric transform (computed once, used by every pixel it - // covers). - let adapted = !self.adapt.is_none(); - let par = tb.area() >= PAR_MIN_PATCHES; - // A weight (zero for a target that doesn't vote) and its match's centre (as a pixel - // index) per target. - let mut wt = vec![0.0f32; tb.area()]; - let mut src = vec![0isize; tb.area()]; - par::chunks2_mut(&mut wt, tb.width(), &mut src, tb.width(), par, |ty, wrow, srow| { - for (tx, (o, sc)) in wrow.iter_mut().zip(srow.iter_mut()).enumerate() { - let k = ty * tb.width() + tx; - if used(k) { - *o = (-cost[k] / (2.0 * sigma2)).exp().max(1e-8); - *sc = nnf[k].1 as isize * w as isize + nnf[k].0 as isize; - } - } - }); - let mut gb: Vec<(f32, f32)> = Vec::new(); - if adapted { - gb = vec![(1.0f32, 0.0f32); tb.area() * ch]; - let this = &*self; - par::chunks_mut(&mut gb, tb.width() * ch, par, |ty, out| { - if this.ctl.cancelled() { - return; - } - let y = tb.y0 + ty; - for tx in 0..tb.width() { - let k = ty * tb.width() + tx; - if used(k) { - this.transform(((tb.x0 + tx) as i32, y as i32), k, nnf[k], &mut out[tx * ch..(tx + 1) * ch]); - } - } - }); - } - // The vote, into a buffer over the hole's bounds (every pixel reads the old pixels). - let votes = Votes { w, h, ch, r: r as usize, tbox: tb, img: &*self.img, wt: &wt, src: &src, gb: &gb }; - let (px, ctl) = (self.px, &self.ctl); - let mut out = vec![0.0f32; hb.area() * ch]; - let mut got = vec![false; hb.area()]; - let row = |yy: usize, orow: &mut [f32], grow: &mut [bool]| { - if ctl.cancelled() { - return; - } - let y = hb.y0 + yy; - for x in hb.x0..hb.x1 { - if px[y * w + x] != Px::Hole { - continue; - } - let o = (x - hb.x0) * ch; - let dst = &mut orow[o..o + ch]; - grow[x - hb.x0] = match (ch, adapted) { - (1, false) => votes.pixel::<1, false>(x, y, dst), - (3, false) => votes.pixel::<3, false>(x, y, dst), - (4, false) => votes.pixel::<4, false>(x, y, dst), - (1, true) => votes.pixel::<1, true>(x, y, dst), - (3, true) => votes.pixel::<3, true>(x, y, dst), - (4, true) => votes.pixel::<4, true>(x, y, dst), - _ => votes.pixel_any(x, y, adapted, dst), - }; - } - }; - par::chunks2_mut(&mut out, hb.width() * ch, &mut got, hb.width(), hb.area() >= PAR_MIN_PATCHES, row); - for yy in 0..hb.height() { - for xx in 0..hb.width() { - if got[yy * hb.width() + xx] { - let (d, s) = (((hb.y0 + yy) * w + hb.x0 + xx) * ch, (yy * hb.width() + xx) * ch); - self.img[d..d + ch].copy_from_slice(&out[s..s + ch]); - } - } - } - } -} - -/// What [`Solver::vote`] votes with: per target (over the target box) its weight (zero when it -/// doesn't vote), its match's centre as a pixel index, and with colour adaptation its gain and -/// bias per channel. -struct Votes<'a> { - w: usize, - h: usize, - ch: usize, - r: usize, - tbox: Bounds, - img: &'a [f32], - wt: &'a [f32], - src: &'a [isize], - gb: &'a [(f32, f32)], -} - -impl Votes<'_> { - /// Hole pixel `(x, y)`'s vote into `out` (`CH` channels): the weighted mean of what the - /// matches of the patches covering it put there, summed patch by patch in raster order of - /// their centres. False when no patch votes. - #[inline(always)] - fn pixel(&self, x: usize, y: usize, out: &mut [f32]) -> bool { - let (w, r, tb) = (self.w, self.r, self.tbox); - let mut acc = [0.0f32; CH]; - let mut wsum = 0.0f32; - for ty in y.saturating_sub(r)..=(y + r).min(self.h - 1) { - let dy = ty as isize - y as isize; - let krow = (ty - tb.y0) * tb.width(); - for tx in x.saturating_sub(r)..=(x + r).min(w - 1) { - let k = krow + tx - tb.x0; - let wt = self.wt[k]; - if wt == 0.0 { - continue; - } - // The pixel of the match's patch that lands here. - let si = (self.src[k] - dy * w as isize - (tx as isize - x as isize)) as usize * CH; - let s = &self.img[si..si + CH]; - for c in 0..CH { - let v = if ADAPT { - let (g, b) = self.gb[k * CH + c]; - g * s[c] + b - } else { - s[c] - }; - acc[c] += v * wt; - } - wsum += wt; - } - } - if wsum > 0.0 { - for (d, a) in out.iter_mut().zip(&acc) { - *d = a / wsum; - } - return true; - } - false - } - - /// [`Votes::pixel`] for any number of channels. - fn pixel_any(&self, x: usize, y: usize, adapted: bool, out: &mut [f32]) -> bool { - let (w, r, tb, ch) = (self.w, self.r, self.tbox, self.ch); - let mut acc = vec![0.0f32; ch]; - let mut wsum = 0.0f32; - for ty in y.saturating_sub(r)..=(y + r).min(self.h - 1) { - let dy = ty as isize - y as isize; - let krow = (ty - tb.y0) * tb.width(); - for tx in x.saturating_sub(r)..=(x + r).min(w - 1) { - let k = krow + tx - tb.x0; - let wt = self.wt[k]; - if wt == 0.0 { - continue; - } - let si = (self.src[k] - dy * w as isize - (tx as isize - x as isize)) as usize * ch; - for (c, a) in acc.iter_mut().enumerate() { - let v = if adapted { - let (g, b) = self.gb[k * ch + c]; - g * self.img[si + c] + b - } else { - self.img[si + c] - }; - *a += v * wt; - } - wsum += wt; - } - } - if wsum > 0.0 { - for (d, a) in out.iter_mut().zip(&acc) { - *d = a / wsum; - } - return true; - } - false - } -} - -/// `Σv, Σv²` per channel (the first `out.len()`) of the `side × side` patch with top-left -/// `(x0, y0)`, summed row by row, pixel by pixel, as [`Solver::moments`] does. -#[allow(clippy::too_many_arguments)] -fn patch_moments(img: &[f32], w: usize, ch: usize, c8: usize, side: usize, x0: usize, y0: usize, out: &mut [[f32; 2]]) { - out.iter_mut().for_each(|v| *v = [0.0; 2]); - for y in y0..y0 + side { - let row = &img[(y * w + x0) * ch..(y * w + x0 + side) * ch]; - for p in row.chunks_exact(ch) { - for (c, o) in out.iter_mut().enumerate().take(c8) { - let a = p[c]; - o[0] += a; - o[1] += a * a; - } - } - } -} - -/// The adapted distance from the moments `[Σt, Σt², Σs, Σs², Σts]` of each channel over `n` -/// samples: what is left of the target after the source is fitted to it, per sample. -#[inline] -fn adapted_cost(a: Adapt, nf: f32, m: &[[f32; 5]]) -> f32 { - let mut sum = 0.0f32; - for mc in m { - let (g, b) = fit(a, nf, mc[0], mc[1], mc[2], mc[3]); - // Σ (t − g s − b)², expanded. - sum += mc[1] + g * g * mc[3] + nf * b * b - 2.0 * g * mc[4] - 2.0 * b * mc[0] + 2.0 * g * b * mc[2]; - } - sum.max(0.0) / (nf * m.len() as f32) -} - -/// `Σ (t − s)²` over a target patch's known pixels (offsets `offs` from its centre, samples -/// `tv`, `CH` per pixel) against the source patch centred at pixel `base`, summed in that order; -/// `None` once the sum reaches `limit` (it only grows). -#[inline(always)] -fn known_sse(image: &[f32], offs: &[isize], tv: &[f32], base: isize, limit: f32) -> Option { - let mut e = 0.0f32; - for (o, t) in offs.iter().zip(tv.as_chunks::().0) { - let si = (base + o) as usize * CH; - let s = &image[si..si + CH]; - for c in 0..CH { - let d = t[c] - s[c]; - e += d * d; - } - if e >= limit { - return None; - } - } - Some(e) -} - -/// [`known_sse`] of [`LANES`] sources at once, each against its own limit (summed in step: the -/// sums are independent, so the processor overlaps them). -#[inline(always)] -fn known_sse_lanes(image: &[f32], offs: &[isize], tv: &[f32], bases: [isize; LANES], limits: [f32; LANES]) -> [Option; LANES] { - let mut e = [0.0f32; LANES]; - for (o, t) in offs.iter().zip(tv.as_chunks::().0) { - for l in 0..LANES { - let si = (bases[l] + o) as usize * CH; - let s = &image[si..si + CH]; - for c in 0..CH { - let d = t[c] - s[c]; - e[l] += d * d; - } - } - // Sums only grow: once all of them reached their limits, none can win. - if (0..LANES).all(|l| e[l] >= limits[l]) { - break; - } - } - std::array::from_fn(|l| (e[l] < limits[l]).then_some(e[l])) -} - -/// Sources [`known_sse_lanes`] sums at once. -const LANES: usize = 4; - -/// [`known_sse`] for any number of channels. -fn known_sse_any(image: &[f32], ch: usize, offs: &[isize], tv: &[f32], base: isize, limit: f32) -> Option { - let mut e = 0.0f32; - for (o, t) in offs.iter().zip(tv.chunks_exact(ch.max(1))) { - let si = (base + o) as usize * ch; - for (tc, sc) in t.iter().zip(&image[si..si + ch]) { - let d = tc - sc; - e += d * d; - } - if e >= limit { - return None; - } - } - Some(e) -} - -/// The best source so far of [`onion_peel`]'s search. -#[derive(Clone, Copy)] -struct Best { - /// Error per sample, and the place in the sources. - v: f32, - j: usize, - /// Sums of squared errors from which `sum / n` is above `v`, and at least `v`. - worse: f32, - equal: f32, -} - -/// The least sum `e ≥ 0` with `e / n > v` (`strict`) or `e / n ≥ v` in float arithmetic (the -/// quotient only grows with `e`); infinity for an infinite `v`. -fn least_sum(v: f32, n: f32, strict: bool) -> f32 { - let holds = |e: f32| if strict { e / n > v } else { e / n >= v }; - if !v.is_finite() || !n.is_finite() || n <= 0.0 { - return f32::INFINITY; - } - let mut e = (v * n).max(0.0); - while e > 0.0 && holds(e.next_down()) { - e = e.next_down(); - } - while !holds(e) && e < f32::INFINITY { - e = e.next_up(); - } - e -} - -/// Fill the hole from its edge inwards, one ring at a time: each pixel takes the centre of the -/// source patch that best matches what is already known around it (the first in `sources` of -/// those that match best). `hb` bounds the hole. -/// -/// The search is exhaustive, but most sources are given up early (#2771, #2805): the target's -/// known pixels are gathered once, the search starts from where the pixel's filled neighbours -/// took their sources (or every 16th source at the hole's edge), and a source's error stops being -/// added up once it can only lose (it only grows). So it finds the same source as a plain scan in -/// order, sooner. -#[allow(clippy::too_many_arguments)] -fn onion_peel( - w: usize, - h: usize, - ch: usize, - r: i32, - img: &mut [f32], - px: &[Px], - hb: Bounds, - sources: &[(i32, i32)], - ctl: &photocraft_raster::Interrupt, -) -> Result<(), photocraft_raster::Cancelled> { - const NONE: u32 = u32::MAX; - let mut known: Vec = px.iter().map(|k| matches!(k, Px::Known | Px::Source)).collect(); - // Each source centre's place in `sources`, and the source each filled pixel took. - let mut index = vec![NONE; w * h]; - for (j, &(x, y)) in sources.iter().enumerate() { - index[y as usize * w + x as usize] = j as u32; - } - let mut took = vec![NONE; w * h]; - let neighbours = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]; - loop { - ctl.check()?; - let mut front: Vec = Vec::new(); - for y in hb.y0..hb.y1 { - for x in hb.x0..hb.x1 { - let i = y * w + x; - if known[i] || px[i] != Px::Hole { - continue; - } - let (x, y) = (x as i32, y as i32); - let edge = (-1..=1).any(|dy: i32| { - (-1..=1).any(|dx: i32| { - let (nx, ny) = (x + dx, y + dy); - nx >= 0 && ny >= 0 && (nx as usize) < w && (ny as usize) < h && known[ny as usize * w + nx as usize] - }) - }); - if edge { - front.push(i); - } - } - } - if front.is_empty() { - break; - } - let (known_now, took_now) = (&known, &took); - let image = &*img; - let best = |&i: &usize| -> (Vec, u32) { - let (x, y) = ((i % w) as i32, (i / w) as i32); - // The known pixels of the target patch, in the plain scan's order: each one's offset - // from the patch centre (in pixels) and its samples (gathered once, #2805). - let side = (2 * r + 1) as usize; - let mut offs: Vec = Vec::with_capacity(side * side); - let mut tv: Vec = Vec::with_capacity(side * side * ch); - for dy in -r..=r { - for dx in -r..=r { - let (tx, ty) = (x + dx, y + dy); - if tx >= 0 && ty >= 0 && (tx as usize) < w && (ty as usize) < h && known_now[ty as usize * w + tx as usize] { - let ti = (ty as usize * w + tx as usize) * ch; - offs.push(dy as isize * w as isize + dx as isize); - tv.extend_from_slice(&image[ti..ti + ch]); - } - } - } - // The best so far: its error per sample, its place in `sources` (the first of equals - // wins, as in a scan in order), and the sums of squares at which a source surely loses - // to it (`e / n` above the best) or ties it (at least the best). - let mut best = Best { v: f32::INFINITY, j: usize::MAX, worse: f32::INFINITY, equal: f32::INFINITY }; - if !offs.is_empty() { - let nf = offs.len() as f32; - let base_of = |j: usize| sources.get(j).map_or(0, |&(sx, sy)| sy as isize * w as isize + sx as isize); - // The sum at which source `j` can't win: a tie loses to an earlier source. - let limit_of = |j: usize, best: &Best| if j > best.j { best.equal } else { best.worse }; - // Source `j`'s whole sum `e` (it did not reach its limit): does it win? - let adopt = |j: usize, e: f32, best: &mut Best| { - let v = e / nf; - if v < f32::INFINITY && (v < best.v || (v == best.v && j < best.j)) { - *best = Best { v, j, worse: least_sum(v, nf, true), equal: least_sum(v, nf, false) }; - } - }; - let consider = |j: usize, best: &mut Best| { - if j >= sources.len() { - return; - } - let (base, limit) = (base_of(j), limit_of(j, best)); - let e = match ch { - 1 => known_sse::<1>(image, &offs, &tv, base, limit), - 3 => known_sse::<3>(image, &offs, &tv, base, limit), - 4 => known_sse::<4>(image, &offs, &tv, base, limit), - _ => known_sse_any(image, ch, &offs, &tv, base, limit), - }; - if let Some(e) = e { - adopt(j, e, best); - } - }; - // A close bound early: where the filled neighbours' sources point (shifted as in - // PatchMatch's propagation), or at the hole's edge, where none is filled yet, - // every 16th source. - for (dx, dy) in neighbours { - let (qx, qy) = (x + dx, y + dy); - if qx < 0 || qy < 0 || qx as usize >= w || qy as usize >= h { - continue; - } - let Some(&(sx, sy)) = sources.get(took_now[qy as usize * w + qx as usize] as usize) else { continue }; - let (cx, cy) = (sx - dx, sy - dy); - if cx >= 0 && cy >= 0 && (cx as usize) < w && (cy as usize) < h && index[cy as usize * w + cx as usize] != NONE { - consider(index[cy as usize * w + cx as usize] as usize, &mut best); - } - } - if best.j == usize::MAX { - for j in (0..sources.len()).step_by(16) { - consider(j, &mut best); - } - } - // The scan in order, `LANES` sources at a time (their sums are independent, so they - // overlap), each against its limit from the best before them: the best only gets - // better, so a source given up would have lost to it anyway. - let mut j = 0; - while j + LANES <= sources.len() { - if j.is_multiple_of(4096) && j > 0 && ctl.cancelled() { - break; - } - let bases: [isize; LANES] = std::array::from_fn(|l| base_of(j + l)); - let limits: [f32; LANES] = std::array::from_fn(|l| limit_of(j + l, &best)); - let sums = match ch { - 1 => known_sse_lanes::<1>(image, &offs, &tv, bases, limits), - 3 => known_sse_lanes::<3>(image, &offs, &tv, bases, limits), - 4 => known_sse_lanes::<4>(image, &offs, &tv, bases, limits), - _ => std::array::from_fn(|l| known_sse_any(image, ch, &offs, &tv, bases[l], limits[l])), - }; - for (l, e) in sums.into_iter().enumerate() { - if let Some(e) = e { - adopt(j + l, e, &mut best); - } - } - j += LANES; - } - for j in j..sources.len() { - consider(j, &mut best); - } - } - let (sx, sy) = sources.get(best.j).copied().unwrap_or((0, 0)); - let si = (sy as usize * w + sx as usize) * ch; - (image[si..si + ch].to_vec(), if best.j == usize::MAX { NONE } else { best.j as u32 }) - }; - let vals: Vec<(Vec, u32)> = par::map(front.len(), front.len() * sources.len() >= 4096, |k| best(&front[k])); - ctl.check()?; - for (&i, (v, j)) in front.iter().zip(vals) { - img[i * ch..(i + 1) * ch].copy_from_slice(&v); - known[i] = true; - took[i] = j; - } - } - Ok(()) -} - /// Content-aware completion of `hole` (see module docs), copying from any pixel outside it. Returns /// `None` when there is no fully-known patch to copy from (the caller can fall back to /// [`membrane_fill`]). @@ -1615,29 +167,7 @@ fn complete_cow<'a>( } let r = p.patch_radius.max(1); let psz = 2 * r + 1; - // Pyramid: shrink until the hole is about four patches across (a deeper pyramid decides the - // structure where the texture has averaged out, and fills flat). A thin hole stops while it - // is still about two patches thick: through it, not along it, is what counts (#1366). - let mut levels = vec![Level { w, h, img, px: Cow::Borrowed(px) }]; - let mut boxes = vec![hb0]; - let mut ext = hb0.width().max(hb0.height()); - let thickness = hole_thickness(w, hb0, px); - let thin = thickness.saturating_mul(THIN_RATIO) <= ext; - if thin { - ext = thickness; - } - let mut exts = vec![ext]; - while let Some(l) = levels.last() { - ctl.check()?; - if ext <= if thin { 2 * psz } else { 4 * psz } || l.w / 2 < 3 * psz || l.h / 2 < 3 * psz { - break; - } - let d = downsample(l, ch); - levels.push(d); - boxes.push(boxes[boxes.len() - 1].half()); - ext = ext.div_ceil(2); - exts.push(ext); - } + let Pyramid { mut levels, boxes, exts, thin } = build_pyramid(Level { w, h, img, px: Cow::Borrowed(px) }, ch, hb0, psz, ctl)?; let nlev = levels.len(); // The coarser level's result. let mut prev: Option = None; @@ -1657,15 +187,7 @@ fn complete_cow<'a>( return Ok(Err(limg)); } // The level's own pixels to fill (at level 0 the caller's, or a copy made in parallel). - let mut limg = match limg { - Cow::Owned(v) => v, - Cow::Borrowed(b) => { - const CHUNK: usize = 1 << 16; - let mut v = vec![0.0f32; b.len()]; - par::chunks_mut(&mut v, CHUNK, true, |i, d| d.copy_from_slice(&b[i * CHUNK..i * CHUNK + d.len()])); - v - } - }; + let mut limg = into_owned_pixels(limg); let targets = targets_around(lw, r, &lpx, hb, tb); ctl.check()?; // Initialise the hole: at the coarsest level peeled from its edge inwards (or a membrane @@ -1673,40 +195,13 @@ fn complete_cow<'a>( // its scaled-up matches. let coarser = prev.take(); match &coarser { - Some(c) => { - let (pw, ph) = (c.w, c.h); - for y in hb.y0..hb.y1 { - for x in hb.x0..hb.x1 { - if lpx[y * lw + x] == Px::Hole { - let (sx, sy) = ((x / 2).min(pw - 1), (y / 2).min(ph - 1)); - let (d, s) = ((y * lw + x) * ch, (sy * pw + sx) * ch); - limg[d..d + ch].copy_from_slice(&c.img[s..s + ch]); - } - } - } - } + Some(c) => copy_up_coarser(&mut limg, &lpx, lw, ch, hb, c), // A thin hole, when peeling it would take too long, takes the membrane below: a few // patches thick here, a membrane across it is its surroundings. None if sources.len() > 0 && (!thin || peel_affordable(lw, &lpx, hb, sources.len())) => { onion_peel(lw, lh, ch, ri, &mut limg, &lpx, hb, &sources.all(&valid, lw), ctl)? } - None => { - let mut unknown: Vec = lpx.iter().map(|k| matches!(k, Px::Hole | Px::Void)).collect(); - // Across a thin hole the membrane starts a patch radius out, past the - // anti-aliased fringe or halo that a selection drawn close to the object leaves. - if thin { - let hole: Vec = lpx.iter().map(|k| *k == Px::Hole).collect(); - for (u, d) in unknown.iter_mut().zip(crate::selection::edt(&hole, lw, lh)) { - *u |= d <= r as f32; - } - } - let m = membrane_fill(lw, lh, ch, &limg, &unknown); - for (i, k) in lpx.iter().enumerate() { - if *k == Px::Hole { - limg[i * ch..(i + 1) * ch].copy_from_slice(&m[i * ch..(i + 1) * ch]); - } - } - } + None => fill_hole_with_membrane(&mut limg, &lpx, (lw, lh, ch), r, thin), } if sources.len() == 0 { prev = Some(Coarser { w: lw, h: lh, img: limg, field: Vec::new(), tbox: tb }); @@ -1738,36 +233,7 @@ fn complete_cow<'a>( // NNF init: the coarser level's matches scaled up where they land on a valid source, // random otherwise. let mut rng = Rng::new(p.seed ^ (li as u64) << 20); - let tw = tb.width(); - let mut nnf = vec![(0i32, 0i32); tb.area()]; - let mut upsampled = false; - for y in tb.y0..tb.y1 { - if y % 32 == 0 { - ctl.check()?; - } - for x in tb.x0..tb.x1 { - let k = (y - tb.y0) * tw + (x - tb.x0); - if !solver.targets[k] { - continue; - } - let mut cand = None; - if let Some(c) = &coarser - && !c.field.is_empty() - { - let (cx, cy) = ((x / 2).min(c.w - 1), (y / 2).min(c.h - 1)); - let (sx, sy) = c.field_at(cx, cy); - if sx != i32::MIN { - // The same displacement, twice as long. - let c = (x as i32 + 2 * (sx - cx as i32), y as i32 + 2 * (sy - cy as i32)); - if solver.is_valid(c.0, c.1) { - cand = Some(c); - } - } - } - upsampled |= cand.is_some(); - nnf[k] = cand.unwrap_or_else(|| sources.nth((rng.next() % sources.len() as u64) as usize, &solver.valid, lw)); - } - } + let (mut nnf, upsampled) = seed_matches(&solver, coarser.as_ref(), &sources, &mut rng, lw, tb, ctl)?; // The coarser level has given all it had. drop(coarser); let mut cost = vec![f32::INFINITY; tb.area()]; @@ -1818,677 +284,138 @@ fn complete_cow<'a>( Ok(prev.map(|c| c.img).ok_or(Cow::Owned(Vec::new()))) } -/// Proximity match: the displacement `(dx, dy)` (within `max_radius`) whose surroundings best match the -/// ring of `ring` pixels around the hole, with the displaced hole fully inside the grid and outside the -/// hole. `None` if no displacement fits. -pub fn best_offset(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], ring: usize, max_radius: i32) -> Option<(i32, i32)> { - let (bx0, by0, bx1, by1) = hole_bbox(w, h, hole)?; - // No valid translation can move farther than the source image's extent. - // Bound arbitrary public API inputs before constructing i32 search ranges: - // usize -> i32 truncation and signed increments otherwise overflow or hang. - let extent = w.max(h).min((i32::MAX / 4) as usize) as i32; - let max_radius = max_radius.max(0).min(extent); - if max_radius == 0 { +/// The levels of the completion, finest first, with the hole's bounds and extent at each. +struct Pyramid<'a> { + levels: Vec>, + boxes: Vec, + exts: Vec, + /// The hole is a thin one: its thickness, not its length, decides how deep to go. + thin: bool, +} + +/// Shrinks the image until the hole is about four patches across (a deeper pyramid decides the +/// structure where the texture has averaged out, and fills flat). A thin hole stops while it is +/// still about two patches thick: through it, not along it, is what counts (#1366). +fn build_pyramid<'a>( + first: Level<'a>, + ch: usize, + hb0: common::Bounds, + psz: usize, + ctl: &photocraft_raster::Interrupt, +) -> Result, photocraft_raster::Cancelled> { + let thickness = hole_thickness(first.w, hb0, &first.px); + let mut levels = vec![first]; + let mut boxes = vec![hb0]; + let mut ext = hb0.width().max(hb0.height()); + let thin = thickness.saturating_mul(THIN_RATIO) <= ext; + if thin { + ext = thickness; + } + let mut exts = vec![ext]; + while let Some(l) = levels.last() { + ctl.check()?; + if ext <= if thin { 2 * psz } else { 4 * psz } || l.w / 2 < 3 * psz || l.h / 2 < 3 * psz { + break; + } + let d = downsample(l, ch); + levels.push(d); + boxes.push(boxes[boxes.len() - 1].half()); + ext = ext.div_ceil(2); + exts.push(ext); + } + Ok(Pyramid { levels, boxes, exts, thin }) +} + +/// The level's own pixels to fill: the caller's, or a copy made in parallel. +fn into_owned_pixels(img: Cow<'_, [f32]>) -> Vec { + match img { + Cow::Owned(v) => v, + Cow::Borrowed(b) => { + const CHUNK: usize = 1 << 16; + let mut v = vec![0.0f32; b.len()]; + par::chunks_mut(&mut v, CHUNK, true, |i, d| d.copy_from_slice(&b[i * CHUNK..i * CHUNK + d.len()])); + v + } + } +} + +/// Starts the hole from the coarser level's pixels, scaled up. +fn copy_up_coarser(img: &mut [f32], px: &[Px], w: usize, ch: usize, hb: common::Bounds, c: &Coarser) { + let (pw, ph) = (c.w, c.h); + for y in hb.y0..hb.y1 { + for x in hb.x0..hb.x1 { + if px[y * w + x] == Px::Hole { + let (sx, sy) = ((x / 2).min(pw - 1), (y / 2).min(ph - 1)); + let (d, s) = ((y * w + x) * ch, (sy * pw + sx) * ch); + img[d..d + ch].copy_from_slice(&c.img[s..s + ch]); + } + } + } +} + +/// Starts the hole as a membrane over its surroundings, for where nothing can be copied. `dims` +/// is the level's width, height and channel count. +fn fill_hole_with_membrane(img: &mut [f32], px: &[Px], dims: (usize, usize, usize), r: usize, thin: bool) { + let (w, h, ch) = dims; + let mut unknown: Vec = px.iter().map(|k| matches!(k, Px::Hole | Px::Void)).collect(); + // Across a thin hole the membrane starts a patch radius out, past the + // anti-aliased fringe or halo that a selection drawn close to the object leaves. + if thin { + let hole: Vec = px.iter().map(|k| *k == Px::Hole).collect(); + for (u, d) in unknown.iter_mut().zip(crate::selection::edt(&hole, w, h)) { + *u |= d <= r as f32; + } + } + let m = membrane_fill(w, h, ch, img, &unknown); + for (i, k) in px.iter().enumerate() { + if *k == Px::Hole { + img[i * ch..(i + 1) * ch].copy_from_slice(&m[i * ch..(i + 1) * ch]); + } + } +} + +/// The coarser level's match for `(x, y)`, scaled up, where it lands on a valid source. +fn upscaled_match(solver: &Solver, c: &Coarser, x: usize, y: usize) -> Option<(i32, i32)> { + let (cx, cy) = ((x / 2).min(c.w - 1), (y / 2).min(c.h - 1)); + let (sx, sy) = c.field_at(cx, cy); + if sx == i32::MIN { return None; } - let ring = ring.max(1).min(extent as usize) as i32; - // Every set cell lies in the hole bounds. Counts outside that rectangle are zero, - // so source rejection needs a table for the hole, not for the whole photo. - let hs = HoleIntegral::new(w, hole, (bx0, by0, bx1, by1))?; - // Sample points: the hole's ring (dilated minus hole), subsampled for speed. - let mut pts = Vec::new(); - let (x0, y0) = (bx0 as i32 - ring, by0 as i32 - ring); - let (x1, y1) = (bx1 as i32 + ring, by1 as i32 + ring); - for y in y0.max(0)..y1.min(h as i32) { - for x in x0.max(0)..x1.min(w as i32) { - if hole[y as usize * w + x as usize] { + // The same displacement, twice as long. + let m = (x as i32 + 2 * (sx - cx as i32), y as i32 + 2 * (sy - cy as i32)); + solver.is_valid(m.0, m.1).then_some(m) +} + +/// NNF init: the coarser level's matches scaled up where they land on a valid source, random +/// otherwise. Also says whether any came from the coarser level. +fn seed_matches( + solver: &Solver, + coarser: Option<&Coarser>, + sources: &Sources, + rng: &mut Rng, + lw: usize, + tb: common::Bounds, + ctl: &photocraft_raster::Interrupt, +) -> Result<(Vec<(i32, i32)>, bool), photocraft_raster::Cancelled> { + let tw = tb.width(); + let mut nnf = vec![(0i32, 0i32); tb.area()]; + let mut upsampled = false; + for y in tb.y0..tb.y1 { + if y % 32 == 0 { + ctl.check()?; + } + for x in tb.x0..tb.x1 { + let k = (y - tb.y0) * tw + (x - tb.x0); + if !solver.targets[k] { continue; } - if hs.count(x - ring, y - ring, x + ring + 1, y + ring + 1) > 0 { - pts.push((x, y)); - } + let cand = coarser.filter(|c| !c.field.is_empty()).and_then(|c| upscaled_match(solver, c, x, y)); + upsampled |= cand.is_some(); + nnf[k] = cand.unwrap_or_else(|| sources.nth((rng.next() % sources.len() as u64) as usize, &solver.valid, lw)); } } - let stride = (pts.len() / 2000).max(1); - let pts: Vec<(i32, i32)> = pts.into_iter().step_by(stride).collect(); - let eval = |dx: i32, dy: i32, cutoff: f32| -> Option { - let (hx0, hy0, hx1, hy1) = (x0 + dx, y0 + dy, x1 + dx, y1 + dy); - let inside = hx0 >= 0 && hy0 >= 0 && hx1 <= w as i32 && hy1 <= h as i32; - if (dx, dy) == (0, 0) || !inside || hs.count(hx0, hy0, hx1, hy1) != 0 { - return None; - } - let mut e = 0.0f32; - let n = pts.len().max(1) as f32; - let penalty = 1.0 + 1e-3 * ((dx as f32).hypot(dy as f32) / max_radius as f32); - for (i, &(x, y)) in pts.iter().enumerate() { - let (a, b) = ((y as usize * w + x as usize) * ch, ((y + dy) as usize * w + (x + dx) as usize) * ch); - for c in 0..ch { - let d = img[a + c] - img[b + c]; - e += d * d; - } - // SSD terms are nonnegative. The partial score is a lower bound on the - // final score; use the same division and penalty to keep float rounding - // and strict tie ordering unchanged. Check in batches to keep this cheap. - if i % 32 == 31 && cutoff.is_finite() && e / n * penalty >= cutoff { - return None; - } - } - // Mild preference for nearer sources on ties. - Some(e / n * penalty) - }; - // Coarse grid over the search window, then a full-resolution refinement around the winner. - let step = (max_radius / 24).max(1); - let mut best: Option<((i32, i32), f32)> = None; - let consider = |dx: i32, dy: i32, best: &mut Option<((i32, i32), f32)>| { - if let Some(e) = eval(dx, dy, best.map_or(f32::INFINITY, |(_, error)| error)) - && best.is_none_or(|(_, be)| e < be) - { - *best = Some(((dx, dy), e)); - } - }; - let mut dy = -max_radius; - while dy <= max_radius { - let mut dx = -max_radius; - while dx <= max_radius { - consider(dx, dy, &mut best); - dx += step; - } - dy += step; - } - if step > 1 - && let Some(((bx, by), _)) = best - { - for dy in (by - step)..=(by + step) { - for dx in (bx - step)..=(bx + step) { - consider(dx, dy, &mut best); - } - } - } - best.map(|b| b.0) -} - -/// Create texture: fill the hole with blocks of surrounding texture (see module docs). -pub fn synthesize(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], block: usize, seed: u64) -> Vec { - let mut out = img.to_vec(); - let Some((bx0, by0, bx1, by1)) = hole_bbox(w, h, hole) else { return out }; - let b = block.max(4) as i32; - let ov = (b / 3).max(1); - let hs = integral(w, h, hole); - // Candidate block origins: whole block (with overlap) inside the grid and outside the hole. - let mut cands = Vec::new(); - for y in ov..(h as i32 - b - ov) { - for x in ov..(w as i32 - b - ov) { - if window_count(&hs, w, h, x - ov, y - ov, x + b + ov, y + b + ov) == 0 { - cands.push((x, y)); - } - } - } - if cands.is_empty() { - return membrane_fill(w, h, ch, img, hole); - } - let mut filled: Vec = hole.iter().map(|x| !x).collect(); - let mut rng = Rng::new(seed); - let mut by = by0 as i32; - while by < by1 as i32 { - let mut bx = bx0 as i32; - while bx < bx1 as i32 { - if window_count(&hs, w, h, bx, by, bx + b, by + b) > 0 { - // Pick the candidate whose block best agrees with already-known pixels around/inside. - let mut best = (cands[0], f32::INFINITY); - for _ in 0..48 { - let c = cands[(rng.next() % cands.len() as u64) as usize]; - let (mut e, mut n) = (0.0f32, 0u32); - for y in (by - ov)..(by + b + ov) { - for x in (bx - ov)..(bx + b + ov) { - if x < 0 || y < 0 || x >= w as i32 || y >= h as i32 || !filled[y as usize * w + x as usize] { - continue; - } - let (a, s) = ((y as usize * w + x as usize) * ch, ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch); - for k in 0..ch { - let d = out[a + k] - img[s + k]; - e += d * d; - } - n += 1; - } - } - let e = if n == 0 { 0.0 } else { e / n as f32 }; - if e < best.1 { - best = (c, e); - } - } - let c = best.0; - let (x0, y0) = ((bx - ov).max(0), (by - ov).max(0)); - let (x1, y1) = ((bx + b + ov).min(w as i32), (by + b + ov).min(h as i32)); - let src = |x: i32, y: i32| ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch; - // Squared difference between what is there and the block, where both exist. - let err = |x: i32, y: i32| { - let i = y as usize * w + x as usize; - if !(hole[i] && filled[i]) { - return 0.0; - } - let (a, s) = (i * ch, src(x, y)); - (0..ch).map(|q| (out[a + q] - img[s + q]).powi(2)).sum::() - }; - // Minimum-error boundary cuts through the overlaps with the blocks to the left and - // above: the block is copied right of and below them, untouched pixels are kept - // on the other side. Copying (rather than averaging) keeps the texture's grain. - let left = min_cut((x0..(bx + ov).min(x1)).len(), (y0..y1).len(), |u, v| err(x0 + u as i32, y0 + v as i32)); - let top = min_cut((y0..(by + ov).min(y1)).len(), (x0..x1).len(), |u, v| err(x0 + v as i32, y0 + u as i32)); - for y in y0..y1 { - for x in x0..x1 { - let i = y as usize * w + x as usize; - if !hole[i] { - continue; - } - let (u, v) = ((x - x0) as usize, (y - y0) as usize); - let keep = filled[i] && (left.get(v).is_some_and(|s| u < *s) || top.get(u).is_some_and(|s| v < *s)); - if !keep { - let (a, s) = (i * ch, src(x, y)); - out[a..a + ch].copy_from_slice(&img[s..s + ch]); - } - filled[i] = true; - } - } - } - bx += b; - } - by += b; - } - out -} - -/// The minimum-error cut through a band `across` wide and `along` long (Efros & Freeman's -/// boundary cut, by dynamic programming): for each step along the band, the offset across it -/// where the cut runs (`err(across, along)` is the cost of a pixel). Pixels before the cut keep -/// what is there. Empty when there is no cost anywhere (nothing to cut through). -fn min_cut(across: usize, along: usize, err: impl Fn(usize, usize) -> f32) -> Vec { - if across == 0 || along == 0 { - return Vec::new(); - } - // Cumulative cost, step by step along the band; the cut may move by one pixel per step. - let mut acc: Vec = (0..along).flat_map(|v| (0..across).map(move |u| (u, v))).map(|(u, v)| err(u, v)).collect(); - if acc.iter().all(|x| *x == 0.0) { - return Vec::new(); - } - for v in 1..along { - for u in 0..across { - let lo = u.saturating_sub(1); - let hi = (u + 1).min(across - 1); - let m = (lo..=hi).map(|k| acc[(v - 1) * across + k]).fold(f32::INFINITY, f32::min); - acc[v * across + u] += m; - } - } - let argmin = |v: usize, lo: usize, hi: usize| (lo..=hi).min_by(|a, b| acc[v * across + a].total_cmp(&acc[v * across + b])).unwrap_or(lo); - let mut cut = vec![0; along]; - let mut u = argmin(along - 1, 0, across - 1); - for v in (0..along).rev() { - if v + 1 < along { - u = argmin(v, u.saturating_sub(1), (u + 1).min(across - 1)); - } - cut[v] = u; - } - cut + Ok((nnf, upsampled)) } #[cfg(test)] -mod tests { - use super::*; - - /// The mean is matched first: on a nearly flat bright patch with a noisy deviation ratio the - /// gain stays where the bias can still reach the target's mean. - #[test] - fn fit_matches_the_mean_before_the_contrast() { - let a = Adapt { gain: (0.9, 1.1), bias: 0.04 }; - // Target mean 0.70, source mean 0.70; deviations 0.01 vs 0.004 (ratio 2.5). - let n = 25.0; - let (mt, ms, sdt, sds) = (0.70f32, 0.70f32, 0.01f32, 0.004f32); - let (g, b) = fit(a, n, mt * n, (sdt * sdt + mt * mt) * n, ms * n, (sds * sds + ms * ms) * n); - assert!((g * ms + b - mt).abs() < 1e-5, "mean {} vs {mt} (g {g}, b {b})", g * ms + b); - // Where the mean can't be reached anyway, the limits hold as before. - let (g, b) = fit(a, n, 0.2 * n, 0.04 * n, 0.9 * n, 0.81 * n); - assert!((a.gain.0..=a.gain.1).contains(&g) && b.abs() <= a.bias + 1e-6); - } - - /// Smooth gradient plus deterministic fine noise. - fn texture(w: usize, h: usize) -> Vec { - let mut rng = Rng::new(7); - (0..w * h) - .flat_map(|i| { - let (x, y) = ((i % w) as f32, (i / w) as f32); - let n = (rng.next() % 1000) as f32 / 1000.0 * 0.04 - 0.02; - [0.3 + 0.004 * x + n, 0.5 + 0.002 * y + n, 0.4 + n] - }) - .collect() - } - - fn disc(w: usize, h: usize, cx: f32, cy: f32, r: f32) -> Vec { - (0..w * h).map(|i| ((i % w) as f32 - cx).hypot((i / w) as f32 - cy) < r).collect() - } - - fn hole_rmse(a: &[f32], b: &[f32], hole: &[bool], ch: usize) -> f32 { - let (mut e, mut n) = (0.0f32, 0); - for (i, hh) in hole.iter().enumerate() { - if *hh { - for c in 0..ch { - e += (a[i * ch + c] - b[i * ch + c]).powi(2); - n += 1; - } - } - } - (e / n as f32).sqrt() - } - - #[test] - fn completion_fills_blemish_from_texture() { - let (w, h, ch) = (96, 96, 3); - let clean = texture(w, h); - let hole = disc(w, h, 48.0, 48.0, 9.0); - let mut dirty = clean.clone(); - for (i, hh) in hole.iter().enumerate() { - if *hh { - dirty[i * ch..i * ch + ch].copy_from_slice(&[0.02, 0.02, 0.02]); - } - } - let out = complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap(); - let before = hole_rmse(&dirty, &clean, &hole, ch); - let after = hole_rmse(&out, &clean, &hole, ch); - assert!(after < 0.06 && after < before * 0.15, "rmse before {before} after {after}"); - // Outside the hole untouched. - assert!(out.iter().zip(&dirty).zip(hole.iter().flat_map(|h| [*h; 3])).all(|((a, b), h)| h || a == b)); - // Deterministic. - assert_eq!(out, complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap()); - } - - #[test] - fn completion_continues_structure() { - // Vertical stripes: the filled hole should keep the stripes (not a blur). - let (w, h, ch) = (80, 80, 1); - let clean: Vec = (0..w * h).map(|i| if (i % w / 4) % 2 == 0 { 0.2 } else { 0.8 }).collect(); - let hole = disc(w, h, 40.0, 40.0, 7.0); - let dirty: Vec = clean.iter().zip(&hole).map(|(c, h)| if *h { 0.5 } else { *c }).collect(); - let out = complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap(); - let err = hole_rmse(&out, &clean, &hole, ch); - assert!(err < 0.15, "{err}"); - } - - #[test] - fn proximity_finds_matching_offset() { - let (w, h, ch) = (80, 60, 1); - // Periodic pattern with period 10 in x: a match is any multiple-of-10 displacement. - let img: Vec = (0..w * h).map(|i| ((i % w) as f32 * std::f32::consts::TAU / 10.0).sin() * 0.4 + 0.5).collect(); - let hole = disc(w, h, 40.0, 30.0, 5.0); - let (dx, dy) = best_offset(w, h, ch, &img, &hole, 3, 20).unwrap(); - assert_eq!(dx.rem_euclid(10), 0, "dx {dx} dy {dy}"); - assert!((dx, dy) != (0, 0)); - } - - #[test] - fn proximity_search_clamps_hostile_radii_without_overflow_or_hanging() { - let (w, h, ch) = (24, 20, 1); - let img: Vec = (0..w * h).map(|i| (i % w) as f32 / w as f32).collect(); - let hole = disc(w, h, 12.0, 10.0, 2.0); - // User-supplied values used to overflow i32 loops, cast a huge usize - // ring into a negative number, or take billions of search iterations. - let result = best_offset(w, h, ch, &img, &hole, usize::MAX, i32::MAX); - if let Some((dx, dy)) = result { - assert!(dx.abs() <= w as i32 && dy.abs() <= h as i32); - } - assert_eq!(best_offset(w, h, ch, &img, &hole, 2, -1), None); - assert_eq!(best_offset(w, h, ch, &img, &hole, 2, 0), None); - } - - /// Structure, fine noise and flat patches (so that some matches tie exactly), `ch` channels. - fn busy(w: usize, h: usize, ch: usize, seed: u64) -> Vec { - let mut rng = Rng::new(seed); - (0..w * h) - .flat_map(|i| { - let (x, y) = (i % w, i / w); - let flat = (x / 9 + y / 7) % 5 == 0; - let n = (rng.next() % 1000) as f32 / 1000.0; - (0..ch) - .map(move |c| if flat { 0.25 + 0.1 * c as f32 } else { (0.2 + 0.004 * x as f32 + 0.1 * ((x + 2 * y + c) % 6) as f32 + 0.05 * n).min(1.0) }) - }) - .collect() - } - - fn bits(v: &[f32]) -> Vec { - v.iter().map(|x| x.to_bits()).collect() - } - - /// Roles: a hole, a band that is context only, a corner that is no image. - fn roles(w: usize, h: usize, hole: &[bool]) -> Vec { - (0..w * h) - .map(|i| { - let (x, y) = (i % w, i / w); - if hole[i] { - Px::Hole - } else if x < 4 && y < 6 { - Px::Void - } else if (w * 3 / 4..w * 3 / 4 + 3).contains(&x) { - Px::Known - } else { - Px::Source - } - }) - .collect() - } - - #[test] - fn a_hole_is_as_thick_as_it_is_through_not_along() { - let (w, h) = (240, 120); - let all = Bounds { x0: 0, y0: 0, x1: w, y1: h }; - let thickness = |hole: &[bool]| { - let px: Vec = hole.iter().map(|&o| if o { Px::Hole } else { Px::Source }).collect(); - hole_thickness(w, hole_bounds(w, h, &px).unwrap_or(all), &px) - }; - // A band 200 px long and 9 px thick; a disc of radius 20; the band touching the edge - // counts the outside of the image as not hole. - let band: Vec = (0..w * h).map(|i| (20..220).contains(&(i % w)) && (50..59).contains(&(i / w))).collect(); - assert!((9..=11).contains(&thickness(&band)), "{}", thickness(&band)); - assert!((39..=42).contains(&thickness(&disc(w, h, 120.0, 60.0, 20.0)))); - let edge: Vec = (0..w * h).map(|i| i / w < 9).collect(); - assert!((9..=11).contains(&thickness(&edge)), "{}", thickness(&edge)); - } - - /// A long thin hole goes the thin way (a shallow pyramid that starts from a membrane): it is - /// filled, only it changes, and the result does not depend on the thread count either. - #[test] - #[cfg(not(target_arch = "wasm32"))] - fn thin_holes_are_filled_the_same_on_any_number_of_threads() { - let (w, h) = (300, 200); - let img = busy(w, h, 3, 5); - // A wavy line 6 px thick across the image. - let hole: Vec = (0..w * h).map(|i| (((i % w) as f32 * 0.05).sin() * 30.0 + 100.0 - (i / w) as f32).abs() < 3.0).collect(); - let px = roles(w, h, &hole); - let run = |threads: usize| { - let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); - pool.install(|| complete_masked(w, h, 3, &img, &px, &CompleteParams::default(), &photocraft_raster::Interrupt::NONE)).unwrap().unwrap() - }; - let one = run(1); - assert_eq!(bits(&one), bits(&run(7))); - assert!((0..w * h).all(|i| hole[i] || (0..3).all(|c| one[i * 3 + c].to_bits() == img[i * 3 + c].to_bits()))); - assert!((0..w * h).filter(|&i| hole[i]).all(|i| (0..3).all(|c| one[i * 3 + c].is_finite()))); - } - - #[test] - #[cfg(not(target_arch = "wasm32"))] - fn results_do_not_depend_on_the_thread_count() { - // Big enough for the parallel passes, and for PatchMatch to sweep in wavefronts where - // there are fewer bands than threads (never with one thread). - let (w, h) = (300, 260); - for (ch, adapt) in [(3, Adapt::NONE), (4, Adapt { gain: (0.9, 1.1), bias: 0.04 })] { - let img = busy(w, h, ch, 3); - let hole = disc(w, h, 140.0, 130.0, 40.0); - let px = roles(w, h, &hole); - let p = CompleteParams { adapt, ..Default::default() }; - let run = |threads: usize| { - let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); - bits(&pool.install(|| complete_masked(w, h, ch, &img, &px, &p, &photocraft_raster::Interrupt::NONE)).unwrap().unwrap()) - }; - let one = run(1); - assert_eq!(one, run(3), "{ch} channels, 3 threads"); - assert_eq!(one, run(16), "{ch} channels, 16 threads"); - } - } - - /// The onion peel as first written: every source in order, every known pixel summed. - fn onion_peel_plain(w: usize, h: usize, ch: usize, r: i32, img: &mut [f32], px: &[Px], sources: &[(i32, i32)]) { - let mut known: Vec = px.iter().map(|k| matches!(k, Px::Known | Px::Source)).collect(); - loop { - let front: Vec = (0..w * h) - .filter(|&i| { - let (x, y) = ((i % w) as i32, (i / w) as i32); - !known[i] - && px[i] == Px::Hole - && (-1..=1).any(|dy: i32| { - (-1..=1).any(|dx: i32| { - let (nx, ny) = (x + dx, y + dy); - nx >= 0 && ny >= 0 && (nx as usize) < w && (ny as usize) < h && known[ny as usize * w + nx as usize] - }) - }) - }) - .collect(); - if front.is_empty() { - break; - } - let vals: Vec> = front - .iter() - .map(|&i| { - let (x, y) = ((i % w) as i32, (i / w) as i32); - let mut best = (f32::INFINITY, (0i32, 0i32)); - for &(sx, sy) in sources { - let (mut e, mut n) = (0.0f32, 0usize); - for dy in -r..=r { - for dx in -r..=r { - let (tx, ty) = (x + dx, y + dy); - if tx < 0 || ty < 0 || tx as usize >= w || ty as usize >= h || !known[ty as usize * w + tx as usize] { - continue; - } - let (ti, si) = ((ty as usize * w + tx as usize) * ch, ((sy + dy) as usize * w + (sx + dx) as usize) * ch); - for c in 0..ch { - let d = img[ti + c] - img[si + c]; - e += d * d; - } - n += 1; - } - } - if n > 0 && e / (n as f32) < best.0 { - best = (e / n as f32, (sx, sy)); - } - } - let si = (best.1.1 as usize * w + best.1.0 as usize) * ch; - img[si..si + ch].to_vec() - }) - .collect(); - for (&i, v) in front.iter().zip(vals) { - img[i * ch..(i + 1) * ch].copy_from_slice(&v); - known[i] = true; - } - } - } - - /// #2805's case (upstream's own speed-up of the onion peel, superseded here): repeating - /// texture with many exact ties, two holes; the result must be the plain scan's to the bit. - #[test] - fn onion_peel_picks_exactly_what_the_plain_scan_picks() { - let (w, h, ch, r) = (48, 40, 3, 3); - let mut img = texture(w, h); - for y in 0..h { - for x in 6..w / 2 { - for c in 0..ch { - img[(y * w + x) * ch + c] = img[(y * w + x % 6) * ch + c]; - } - } - } - let none = photocraft_raster::Interrupt::NONE; - for hole in [disc(w, h, 20.0, 18.0, 7.5), disc(w, h, 34.0, 22.0, 5.0)] { - let px: Vec = hole.iter().map(|h| if *h { Px::Hole } else { Px::Source }).collect(); - let near = integral(w, h, &hole); - let sources: Vec<(i32, i32)> = (r..h as i32 - r) - .flat_map(|y| (r..w as i32 - r).map(move |x| (x, y))) - .filter(|&(x, y)| window_count(&near, w, h, x - r, y - r, x + r + 1, y + r + 1) == 0) - .collect(); - let (mut fast, mut plain) = (img.clone(), img.clone()); - onion_peel(w, h, ch, r, &mut fast, &px, hole_bounds(w, h, &px).unwrap(), &sources, &none).unwrap(); - onion_peel_plain(w, h, ch, r, &mut plain, &px, &sources); - assert!(fast.iter().zip(&plain).all(|(a, b)| a.to_bits() == b.to_bits())); - assert_ne!(fast, img); - } - } - - #[test] - fn onion_peel_takes_the_plain_scans_sources() { - let none = photocraft_raster::Interrupt::NONE; - for (w, h, ch, seed) in [(44, 36, 1, 1), (50, 40, 3, 2), (40, 40, 4, 3)] { - let mut img = busy(w, h, ch, seed); - // Also an image of a few exact values, where many sources tie. - if seed == 3 { - img.iter_mut().for_each(|v| *v = (*v * 3.0).round() / 3.0); - } - let hole = disc(w, h, w as f32 * 0.45, h as f32 * 0.5, 7.5); - let px = roles(w, h, &hole); - let valid = valid_sources(w, h, 3, &px, &none).unwrap(); - let sources = Sources::new(&valid, w).all(&valid, w); - let (mut a, mut b) = (img.clone(), img.clone()); - onion_peel(w, h, ch, 3, &mut a, &px, hole_bounds(w, h, &px).unwrap(), &sources, &none).unwrap(); - onion_peel_plain(w, h, ch, 3, &mut b, &px, &sources); - assert_eq!(bits(&a), bits(&b), "{w}×{h}×{ch}"); - } - } - - #[test] - fn least_sum_is_where_the_quotient_crosses() { - let mut rng = Rng::new(5); - for i in 0..4000 { - let n = (1 + rng.next() % 49) as f32; - let v = match i % 4 { - 0 => 0.0, - 1 => (rng.next() % 1000) as f32 * 1e-9, - _ => (rng.next() % 1_000_000) as f32 / 1_000_000.0, - }; - for strict in [true, false] { - let holds = |e: f32| if strict { e / n > v } else { e / n >= v }; - let e = least_sum(v, n, strict); - assert!(holds(e), "{v} {n} {strict}: {e}"); - assert!(e == 0.0 || !holds(e.next_down()), "{v} {n} {strict}: {e} is not the least"); - } - } - assert_eq!(least_sum(f32::INFINITY, 4.0, true), f32::INFINITY); - } - - #[test] - fn valid_sources_are_the_wholly_copyable_patches() { - let (w, h, r) = (37, 29, 2); - let mut rng = Rng::new(9); - let px: Vec = (0..w * h) - .map(|_| match rng.next() % 23 { - 0 => Px::Hole, - 1 => Px::Void, - 2 => Px::Known, - _ => Px::Source, - }) - .collect(); - let valid = valid_sources(w, h, r, &px, &photocraft_raster::Interrupt::NONE).unwrap(); - let source: Vec = px.iter().map(|k| *k == Px::Source).collect(); - let ss = integral(w, h, &source); - let (ri, full) = (r as i32, ((2 * r + 1) * (2 * r + 1)) as u32); - for y in 0..h as i32 { - for x in 0..w as i32 { - let inside = x >= ri && y >= ri && x + ri < w as i32 && y + ri < h as i32; - let want = inside && window_count(&ss, w, h, x - ri, y - ri, x + ri + 1, y + ri + 1) == full; - assert_eq!(valid[y as usize * w + x as usize], want, "({x}, {y})"); - } - } - // Counting per row and block finds the centres in order. - let sources = Sources::new(&valid, w); - let list = sources.all(&valid, w); - assert!(list.len() > 10 && list.len() == sources.len()); - for (k, p) in list.iter().enumerate() { - assert_eq!(sources.nth(k, &valid, w), *p); - } - // And on a grid wider than a block, with empty blocks and rows. - let (w, h) = (300, 9); - let wide: Vec = (0..w * h).map(|i| (i % w) % 7 == 3 && (i % w) / 64 != 2 && i / w != 4).collect(); - let sources = Sources::new(&wide, w); - let list = sources.all(&wide, w); - for (k, p) in list.iter().enumerate() { - assert_eq!(sources.nth(k, &wide, w), *p); - } - } - - #[test] - fn full_patches_measure_as_clipped_ones() { - let (w, h, ch, r) = (46, 38, 4, 3); - let mut img = busy(w, h, ch, 4); - let hole = disc(w, h, 20.0, 18.0, 6.0); - let px = roles(w, h, &hole); - let none = photocraft_raster::Interrupt::NONE; - let hb = hole_bounds(w, h, &px).unwrap(); - let tb = hb.grow(r, w, h); - let valid = valid_sources(w, h, r, &px, &none).unwrap(); - let picks: Vec<(i32, i32)> = Sources::new(&valid, w).all(&valid, w).iter().step_by(37).copied().collect(); - assert!(picks.len() > 5); - for adapt in [Adapt::NONE, Adapt { gain: (0.8, 1.25), bias: 0.08 }] { - for keep in [false, true] { - let targets = targets_around(w, r, &px, hb, tb); - let mut s = Solver { - w, - h, - ch, - r: r as i32, - img: &mut img, - px: &px, - valid: valid.clone(), - targets, - hole_box: hb, - tbox: tb, - full: Vec::new(), - tmom: Vec::new(), - smom: Vec::new(), - adapt, - ctl: none, - }; - s.prepare(keep); - let nnf = vec![picks[0]; tb.area()]; - let mut cost = vec![f32::INFINITY; tb.area()]; - s.costs(&nnf, &mut cost).unwrap(); - let mut checked = 0; - for k in 0..tb.area() { - if !s.targets[k] || !s.full[k] { - continue; - } - let t = ((tb.x0 + k % tb.width()) as i32, (tb.y0 + k / tb.width()) as i32); - for &src in &picks { - if adapt.is_none() { - let exact = s.dist_clipped(t, src, f32::INFINITY); - assert_eq!(s.dist_full(t, src, f32::INFINITY).to_bits(), exact.to_bits()); - for cutoff in [exact * 0.5, exact, exact * 2.0] { - let (a, b) = (s.dist_full(t, src, cutoff), s.dist_clipped(t, src, cutoff)); - assert_eq!(a < cutoff, b < cutoff); - } - } else { - let (mut m1, mut m2) = ([[0.0f32; 5]; 4], [[0.0f32; 5]; 4]); - s.moments_full(k, t, src, &mut m1); - assert_eq!(s.moments(t, src, &mut m2), (2 * r + 1) * (2 * r + 1)); - assert_eq!(m1.map(|c| c.map(f32::to_bits)), m2.map(|c| c.map(f32::to_bits))); - assert_eq!(s.dist_adapted_full(k, t, src).to_bits(), s.dist_adapted(t, src).to_bits()); - } - checked += 1; - } - } - assert!(checked > 100, "{checked}"); - } - } - } - - #[test] - fn owned_completion_fills_in_place_or_gives_the_pixels_back() { - let (w, h, ch) = (64, 52, 3); - let img = busy(w, h, ch, 6); - let hole = disc(w, h, 30.0, 26.0, 8.0); - let px = roles(w, h, &hole); - let (p, none) = (CompleteParams::default(), photocraft_raster::Interrupt::NONE); - let borrowed = complete_masked(w, h, ch, &img, &px, &p, &none).unwrap().unwrap(); - let owned = complete_masked_owned(w, h, ch, img.clone(), &px, &p, &none).unwrap().unwrap(); - assert_eq!(bits(&owned), bits(&borrowed)); - // Nothing may be copied: no completion, and the pixels come back as they were. - let known: Vec = px.iter().map(|k| if *k == Px::Source { Px::Known } else { *k }).collect(); - assert!(complete_masked(w, h, ch, &img, &known, &p, &none).unwrap().is_none()); - let back = complete_masked_owned(w, h, ch, img.clone(), &known, &p, &none).unwrap().unwrap_err(); - assert_eq!(bits(&back), bits(&img)); - // Cancelled, it stops. - let yes = || true; - assert!(complete_masked_owned(w, h, ch, img, &px, &p, &photocraft_raster::Interrupt::cancel_only(&yes)).is_err()); - } - - #[test] - fn synthesis_fills_hole_with_plausible_values() { - let (w, h, ch) = (64, 64, 3); - let clean = texture(w, h); - let hole = disc(w, h, 32.0, 32.0, 6.0); - let out = synthesize(w, h, ch, &clean, &hole, 8, 3); - let err = hole_rmse(&out, &clean, &hole, ch); - assert!(err < 0.15, "{err}"); - assert!(out.iter().all(|v| v.is_finite())); - } -} +mod tests; diff --git a/crates/algo/src/inpaint/common.rs b/crates/algo/src/inpaint/common.rs new file mode 100644 index 0000000000..16989a6079 --- /dev/null +++ b/crates/algo/src/inpaint/common.rs @@ -0,0 +1,119 @@ +use super::Cow; + +/// Below this many cells a pass runs on the calling thread (thread hand-off costs more than it +/// saves). Every pass gives the same result either way. +pub(super) const PAR_MIN: usize = 64 * 64; + +/// [`PAR_MIN`] for the passes that match or vote a patch per cell. +pub(super) const PAR_MIN_PATCHES: usize = 16 * 16; + +/// Largest level (in cells) whose source patches' moments [`Solver::prepare`] keeps. +pub(super) const SOURCE_MOMENTS_MAX: usize = 1 << 20; + +/// SplitMix64: tiny deterministic RNG. +#[derive(Clone)] +pub(crate) struct Rng(pub(crate) u64); + +impl Rng { + /// A generator whose sequence is fixed by `seed`. + pub(crate) fn new(seed: u64) -> Self { + Rng(seed ^ 0x9E37_79B9_7F4A_7C15) + } + /// The next value of the SplitMix64 sequence. + pub(crate) fn next(&mut self) -> u64 { + crate::photo_util::splitmix64(&mut self.0) + } + /// Skip `n` numbers (the same state as after `n` calls of [`Rng::next`]). + pub(crate) fn skip(&mut self, n: u64) { + self.0 = self.0.wrapping_add(n.wrapping_mul(0x9E37_79B9_7F4A_7C15)); + } + /// Uniform integer in `lo..=hi`. + pub(crate) fn range(&mut self, lo: i32, hi: i32) -> i32 { + if hi <= lo { + return lo; + } + lo + (self.next() % (hi - lo + 1) as u64) as i32 + } +} + +/// How far a copied patch may be adjusted to its target, per channel: `gain · source + bias`, the +/// gain within `gain` and the bias within `±bias` (Image Melding's photometric transform). The +/// gain is the ratio of the patches' standard deviations, the bias what then matches their means. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Adapt { + pub gain: (f32, f32), + pub bias: f32, +} + +impl Adapt { + /// Copies are taken as they are. + pub const NONE: Adapt = Adapt { gain: (1.0, 1.0), bias: 0.0 }; + + pub fn is_none(&self) -> bool { + self.gain == (1.0, 1.0) && self.bias == 0.0 + } +} + +impl Default for Adapt { + fn default() -> Self { + Self::NONE + } +} + +/// What a pixel is to [`complete_masked`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Px { + /// Not part of the image (a gap, or outside a transformed copy): ignored. + Void, + /// Known, part of the context the fill must agree with, but not copied from. + Known, + /// Known and may be copied from. + Source, + /// To be filled. + Hole, +} + +/// One pyramid level. Level 0 borrows the caller's buffers until it is solved. +pub(super) struct Level<'a> { + pub(super) w: usize, + pub(super) h: usize, + pub(super) img: Cow<'a, [f32]>, + pub(super) px: Cow<'a, [Px]>, +} + +impl Level<'_> { + pub(super) const EMPTY: Level<'static> = Level { w: 0, h: 0, img: Cow::Borrowed(&[]), px: Cow::Borrowed(&[]) }; +} + +/// The half-open box `[x0, x1) × [y0, y1)` of a level's grid. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) struct Bounds { + pub(super) x0: usize, + pub(super) y0: usize, + pub(super) x1: usize, + pub(super) y1: usize, +} + +impl Bounds { + /// Grown by `r` on every side, within a `w × h` grid. + pub(super) fn grow(self, r: usize, w: usize, h: usize) -> Bounds { + Bounds { x0: self.x0.saturating_sub(r), y0: self.y0.saturating_sub(r), x1: self.x1.saturating_add(r).min(w), y1: self.y1.saturating_add(r).min(h) } + } + + /// The same box on the next coarser level (whose cells are 2 × 2 of these). + pub(super) fn half(self) -> Bounds { + Bounds { x0: self.x0 / 2, y0: self.y0 / 2, x1: self.x1.div_ceil(2), y1: self.y1.div_ceil(2) } + } + + pub(super) fn width(&self) -> usize { + self.x1.saturating_sub(self.x0) + } + + pub(super) fn height(&self) -> usize { + self.y1.saturating_sub(self.y0) + } + + pub(super) fn area(&self) -> usize { + self.width() * self.height() + } +} diff --git a/crates/algo/src/inpaint/solver.rs b/crates/algo/src/inpaint/solver.rs new file mode 100644 index 0000000000..67e0e2645e --- /dev/null +++ b/crates/algo/src/inpaint/solver.rs @@ -0,0 +1,866 @@ +use super::common::{Adapt, Bounds, PAR_MIN_PATCHES, Px, Rng}; +use super::par; + +/// Most channels whose moments the colour adaptation fits (the rest are copied as they are). +const MOM_CH: usize = 8; + +/// Per-level PatchMatch/EM state. +pub(super) struct Solver<'a> { + pub(super) w: usize, + pub(super) h: usize, + pub(super) ch: usize, + pub(super) r: i32, + pub(super) img: &'a mut Vec, + pub(super) px: &'a [Px], + /// Valid source centres: the whole patch is inside the grid and may be copied from. + pub(super) valid: Vec, + /// The hole's bounds and the targets' (the hole's grown by the patch radius): nothing outside + /// them changes or is matched. The match field, its costs and these per-target tables are + /// kept over `tbox`, row-major. + pub(super) hole_box: Bounds, + pub(super) tbox: Bounds, + /// Target centres (the patch overlaps the hole). + pub(super) targets: Vec, + /// Per cell of `tbox`: the whole patch is inside the grid and not void (so its distances + /// need no bounds checks and its own moments can be kept). + pub(super) full: Vec, + /// With colour adaptation, per cell of `tbox` and channel (at most [`MOM_CH`]): `Σt, Σt²` of + /// the patch, for full patches, as of the last [`Solver::costs`]. + pub(super) tmom: Vec<[f32; 2]>, + /// With colour adaptation where it pays: per source centre and channel, `Σs, Σs²` of its + /// patch (source pixels never change within a level). Empty otherwise. + pub(super) smom: Vec<[f32; 2]>, + pub(super) adapt: Adapt, + /// Cancellation: checked per row band; a cancelled solve's output is discarded. + pub(super) ctl: photocraft_raster::Interrupt<'a>, +} + +/// A channel deviation under which a patch counts as flat for [`fit`] (about 5 levels of 255). +const FLAT: f32 = 0.02; + +/// Gain and bias of one channel for target moments `(Σt, Σt²)` and source moments `(Σs, Σs²)` +/// over `n` samples, within the limits. +#[inline] +pub(crate) fn fit(a: Adapt, n: f32, st: f32, stt: f32, ss: f32, sss: f32) -> (f32, f32) { + let (mt, ms) = (st / n, ss / n); + let vt = (stt / n - mt * mt).max(0.0); + let vs = (sss / n - ms * ms).max(0.0); + let g = if vs > 1e-10 { (vt / vs).sqrt() } else { 1.0 }; + let (mut lo, mut hi) = a.gain; + // Nearly flat patches of about the same colour (their means within the bias of each other): + // the ratio of their deviations is noise, so match the mean first, keeping the gain where the + // bias can still bring the source's mean to the target's. Gained about zero, a gain at its + // limit moved bright means further than the bias could bring back, and each channel by a + // different amount: a tinted blotch in a smooth gradient. Textured patches, and flat ones + // whose colours differ more (Content-Aware Move's Color), are fitted as before. + if vt.max(vs) < FLAT * FLAT && (mt - ms).abs() <= a.bias && ms > 1e-6 { + let (glo, ghi) = ((mt - a.bias) / ms, (mt + a.bias) / ms); + if glo.max(lo) <= ghi.min(hi) { + (lo, hi) = (glo.max(lo), ghi.min(hi)); + } + } + let g = g.clamp(lo, hi); + let b = (mt - g * ms).clamp(-a.bias, a.bias); + (g, b) +} + +impl Solver<'_> { + #[inline] + pub(super) fn is_valid(&self, x: i32, y: i32) -> bool { + x >= 0 && y >= 0 && (x as usize) < self.w && (y as usize) < self.h && self.valid[y as usize * self.w + x as usize] + } + + /// Mark the full target patches (`full`), and keep the source moments where they pay off. + pub(super) fn prepare(&mut self, keep_source_moments: bool) { + let (w, h, r, tb) = (self.w, self.h, self.r as usize, self.tbox); + let side = 2 * r + 1; + let mut full = vec![false; tb.area()]; + let px = self.px; + par::chunks_mut(&mut full, tb.width(), tb.area() >= PAR_MIN_PATCHES, |ty, out| { + let y = tb.y0 + ty; + if y < r || y + r >= h { + return; + } + for (tx, f) in out.iter_mut().enumerate() { + let x = tb.x0 + tx; + if x < r || x + r >= w { + continue; + } + *f = (y - r..=y + r).all(|yy| px[yy * w + x - r..yy * w + x - r + side].iter().all(|k| *k != Px::Void)); + } + }); + self.full = full; + if self.adapt.is_none() { + return; + } + let c8 = self.ch.min(MOM_CH); + self.tmom = vec![[0.0; 2]; tb.area() * c8]; + if !keep_source_moments { + return; + } + let (ch, img, valid, ctl) = (self.ch, &*self.img, &self.valid, &self.ctl); + let mut smom = vec![[0.0f32; 2]; w * h * c8]; + par::chunks_mut(&mut smom, w * c8, w * h >= PAR_MIN_PATCHES, |y, out| { + if ctl.cancelled() { + return; + } + for x in 0..w { + if valid[y * w + x] { + patch_moments(img, w, ch, c8, side, x - r, y - r, &mut out[x * c8..(x + 1) * c8]); + } + } + }); + self.smom = smom; + } + + /// Distance between the target patch at `t` (its pixels inside the grid and not void; `k` its + /// index in the `tbox` tables) and the source patch at `s`, per sample; with colour adaptation + /// the source is fitted first. + #[inline] + fn dist(&self, t: (i32, i32), k: usize, s: (i32, i32), cutoff: f32) -> f32 { + let full = self.full[k]; + if !self.adapt.is_none() { + return if full { self.dist_adapted_full(k, t, s) } else { self.dist_adapted(t, s) }; + } + if full { self.dist_full(t, s, cutoff) } else { self.dist_clipped(t, s, cutoff) } + } + + /// [`Solver::dist`] without adaptation, of a patch that may be clipped. + pub(super) fn dist_clipped(&self, t: (i32, i32), s: (i32, i32), cutoff: f32) -> f32 { + let (w, h, ch, r) = (self.w as i32, self.h as i32, self.ch, self.r); + let full = ((2 * r + 1) * (2 * r + 1)) as f32 * ch as f32; + let mut sum = 0.0f32; + let mut n = 0usize; + for dy in -r..=r { + let ty = t.1 + dy; + if ty < 0 || ty >= h { + continue; + } + let sy = s.1 + dy; + for dx in -r..=r { + let tx = t.0 + dx; + if tx < 0 || tx >= w || self.px[ty as usize * self.w + tx as usize] == Px::Void { + continue; + } + let ti = (ty as usize * self.w + tx as usize) * ch; + let si = (sy as usize * self.w + (s.0 + dx) as usize) * ch; + for c in 0..ch { + let d = self.img[ti + c] - self.img[si + c]; + sum += d * d; + } + n += ch; + } + // Early out: the mean can only be ≥ sum / (full patch size). + if sum > cutoff * full { + return f32::INFINITY; + } + } + if n == 0 { f32::INFINITY } else { sum / n as f32 } + } + + /// [`Solver::dist_clipped`] of a full patch: the same sums, in the same order. It gives up + /// (infinity) as soon as the sum shows the patch is worse than `cutoff`, which the sum, only + /// growing, can show after any pixel: the caller only asks whether it is better. + pub(super) fn dist_full(&self, t: (i32, i32), s: (i32, i32), cutoff: f32) -> f32 { + let (w, ch, r) = (self.w, self.ch, self.r); + let side = (2 * r + 1) as usize; + let full = ((2 * r + 1) * (2 * r + 1)) as f32 * ch as f32; + let (span, limit) = (side * ch, cutoff * full); + let mut sum = 0.0f32; + for dy in -r..=r { + let ti = ((t.1 + dy) as usize * w + (t.0 - r) as usize) * ch; + let si = ((s.1 + dy) as usize * w + (s.0 - r) as usize) * ch; + for (tp, sp) in self.img[ti..ti + span].chunks_exact(ch).zip(self.img[si..si + span].chunks_exact(ch)) { + for (a, b) in tp.iter().zip(sp) { + let d = a - b; + sum += d * d; + } + if sum > limit { + return f32::INFINITY; + } + } + } + sum / (side * side * ch) as f32 + } + + /// Moments of the target patch at `t` and the source patch at `s` per channel: + /// `[Σt, Σt², Σs, Σs², Σts]`, and the sample count. + pub(super) fn moments(&self, t: (i32, i32), s: (i32, i32), m: &mut [[f32; 5]]) -> usize { + let (w, h, ch, r) = (self.w as i32, self.h as i32, self.ch, self.r); + m.iter_mut().for_each(|v| *v = [0.0; 5]); + let mut n = 0usize; + for dy in -r..=r { + let ty = t.1 + dy; + if ty < 0 || ty >= h { + continue; + } + let sy = s.1 + dy; + for dx in -r..=r { + let tx = t.0 + dx; + if tx < 0 || tx >= w || self.px[ty as usize * self.w + tx as usize] == Px::Void { + continue; + } + let ti = (ty as usize * self.w + tx as usize) * ch; + let si = (sy as usize * self.w + (s.0 + dx) as usize) * ch; + for (c, mc) in m.iter_mut().enumerate().take(ch) { + let (a, b) = (self.img[ti + c], self.img[si + c]); + mc[0] += a; + mc[1] += a * a; + mc[2] += b; + mc[3] += b * b; + mc[4] += a * b; + } + n += 1; + } + } + n + } + + /// [`Solver::moments`] of a full target patch (`k` in the `tbox` tables): the target's own + /// sums kept from [`Solver::costs`], the source's from [`Solver::prepare`] when kept, each + /// summed in the same order as there. + pub(super) fn moments_full(&self, k: usize, t: (i32, i32), s: (i32, i32), m: &mut [[f32; 5]]) { + let (w, ch, r) = (self.w, self.ch, self.r); + let c8 = m.len(); + let side = (2 * r + 1) as usize; + let tm = &self.tmom[k * c8..(k + 1) * c8]; + let sm = self.smom.get((s.1 as usize * w + s.0 as usize) * c8..(s.1 as usize * w + s.0 as usize + 1) * c8); + let mut acc = [[0.0f32; 3]; MOM_CH]; + for dy in -r..=r { + let ti = ((t.1 + dy) as usize * w + (t.0 - r) as usize) * ch; + let si = ((s.1 + dy) as usize * w + (s.0 - r) as usize) * ch; + let (trow, srow) = (&self.img[ti..ti + side * ch], &self.img[si..si + side * ch]); + if sm.is_some() { + for (tp, sp) in trow.chunks_exact(ch).zip(srow.chunks_exact(ch)) { + for c in 0..c8 { + acc[c][2] += tp[c] * sp[c]; + } + } + } else { + for (tp, sp) in trow.chunks_exact(ch).zip(srow.chunks_exact(ch)) { + for c in 0..c8 { + let (a, b) = (tp[c], sp[c]); + acc[c][0] += b; + acc[c][1] += b * b; + acc[c][2] += a * b; + } + } + } + } + for (c, mc) in m.iter_mut().enumerate() { + let [ss, sss] = match sm { + Some(sm) => sm[c], + None => [acc[c][0], acc[c][1]], + }; + *mc = [tm[c][0], tm[c][1], ss, sss, acc[c][2]]; + } + } + + pub(super) fn dist_adapted(&self, t: (i32, i32), s: (i32, i32)) -> f32 { + let mut m = [[0.0f32; 5]; MOM_CH]; + let ch = self.ch.min(MOM_CH); + let n = self.moments(t, s, &mut m[..ch]); + if n == 0 { + return f32::INFINITY; + } + adapted_cost(self.adapt, n as f32, &m[..ch]) + } + + pub(super) fn dist_adapted_full(&self, k: usize, t: (i32, i32), s: (i32, i32)) -> f32 { + let mut m = [[0.0f32; 5]; MOM_CH]; + let ch = self.ch.min(MOM_CH); + self.moments_full(k, t, s, &mut m[..ch]); + let side = (2 * self.r + 1) as usize; + adapted_cost(self.adapt, (side * side) as f32, &m[..ch]) + } + + /// Gain and bias per channel for the match `t → s` (identity without adaptation). + fn transform(&self, t: (i32, i32), k: usize, s: (i32, i32), out: &mut [(f32, f32)]) { + if self.adapt.is_none() { + out.iter_mut().for_each(|v| *v = (1.0, 0.0)); + return; + } + let mut m = [[0.0f32; 5]; MOM_CH]; + let ch = self.ch.min(MOM_CH); + let n = if self.full[k] { + self.moments_full(k, t, s, &mut m[..ch]); + let side = (2 * self.r + 1) as usize; + side * side + } else { + self.moments(t, s, &mut m[..ch]) + }; + for (c, o) in out.iter_mut().enumerate() { + *o = if n == 0 || c >= ch { (1.0, 0.0) } else { fit(self.adapt, n as f32, m[c][0], m[c][1], m[c][2], m[c][3]) }; + } + } + + /// One PatchMatch step at target `t` (`k` in the target box) whose match is `cur`: propagation + /// from the matches of the neighbours visited just before it (`left` in its row, `up` in its + /// column, in this pass's direction `step`), then random search in exponentially shrinking + /// windows around the best (two numbers from `rng` per window). + #[allow(clippy::too_many_arguments)] + #[inline] + fn pm_step( + &self, + t: (i32, i32), + k: usize, + cur: ((i32, i32), f32), + left: Option<(i32, i32)>, + up: Option<(i32, i32)>, + step: i32, + rng: &mut Rng, + search_radius: i32, + ) -> ((i32, i32), f32) { + let (mut best, mut bc) = cur; + if let Some(c) = left { + let cand = (c.0 + step, c.1); + if self.is_valid(cand.0, cand.1) && cand != best { + let d = self.dist(t, k, cand, bc); + if d < bc { + (best, bc) = (cand, d); + } + } + } + if let Some(c) = up { + let cand = (c.0, c.1 + step); + if self.is_valid(cand.0, cand.1) && cand != best { + let d = self.dist(t, k, cand, bc); + if d < bc { + (best, bc) = (cand, d); + } + } + } + let mut rad = search_radius; + while rad >= 1 { + let cand = (best.0 + rng.range(-rad, rad), best.1 + rng.range(-rad, rad)); + if self.is_valid(cand.0, cand.1) && cand != best { + let d = self.dist(t, k, cand, bc); + if d < bc { + (best, bc) = (cand, d); + } + } + rad /= 2; + } + (best, bc) + } + + /// One PatchMatch pass over all target rows, in bands of the grid's rows: each band is + /// visited in raster order (backwards on odd passes), propagating within itself, its random + /// numbers seeded by its index. `nnf` and `cost` are over the target box. The bands run in + /// parallel; when there are fewer of them than threads, each is also swept in wavefronts of + /// blocks (a cell depends only on its neighbours before it in its row and its column), which + /// visits every cell with the same neighbours and random numbers as the raster order. + pub(super) fn patchmatch(&self, nnf: &mut [(i32, i32)], cost: &mut [f32], pass: usize, seed: u64, search_radius: i32) { + const BAND: usize = 16; + let tb = self.tbox; + let tw = tb.width(); + if tw == 0 || tb.height() == 0 || nnf.len() < tb.area() || cost.len() < tb.area() { + return; + } + let reverse = pass % 2 == 1; + let step: i32 = if reverse { -1 } else { 1 }; + let band_seed = |bi: usize| seed ^ (bi as u64).wrapping_mul(0x2545_F491_4F6C_DD1D) ^ ((pass as u64) << 40); + // The bands crossing the target box: index, first row, rows in the box. + let mut bands = Vec::new(); + let mut y = tb.y0; + while y < tb.y1 { + let bi = y / BAND; + let end = ((bi + 1) * BAND).min(tb.y1); + bands.push((bi, y, end - y)); + y = end; + } + let threads = par::threads(); + if tb.area() >= PAR_MIN_PATCHES * 8 && !bands.is_empty() && bands.len() < threads { + self.patchmatch_wavefront(nnf, cost, &bands, reverse, &band_seed, search_radius); + return; + } + let band_fn = |bi: usize, ys: usize, nn: &mut [(i32, i32)], co: &mut [f32]| { + if self.ctl.cancelled() { + return; + } + let rows = nn.len() / tw; + let mut rng = Rng::new(band_seed(bi)); + for ry in 0..rows { + if self.ctl.cancelled() { + return; + } + let ly = if reverse { rows - 1 - ry } else { ry }; + let y = ys + ly; + for rx in 0..tw { + let lx = if reverse { tw - 1 - rx } else { rx }; + let li = ly * tw + lx; + let k = (y - tb.y0) * tw + lx; + if !self.targets[k] { + continue; + } + // Neighbours outside the target box hold no match: nothing to try. + let nx = lx as i32 - step; + let left = (nx >= 0 && (nx as usize) < tw).then(|| nn[ly * tw + nx as usize]); + let nly = ly as i32 - step; + let up = (nly >= 0 && (nly as usize) < rows).then(|| nn[nly as usize * tw + lx]); + let t = ((tb.x0 + lx) as i32, y as i32); + (nn[li], co[li]) = self.pm_step(t, k, (nn[li], co[li]), left, up, step, &mut rng, search_radius); + } + } + }; + // The target box's rows, band by band. + let mut parts = Vec::with_capacity(bands.len()); + let (mut nn_rest, mut co_rest) = (&mut nnf[..tb.area()], &mut cost[..tb.area()]); + for &(bi, ys, rows) in &bands { + let (nn, nr) = std::mem::take(&mut nn_rest).split_at_mut(rows * tw); + let (co, cr) = std::mem::take(&mut co_rest).split_at_mut(rows * tw); + parts.push((bi, ys, nn, co)); + (nn_rest, co_rest) = (nr, cr); + } + let par = tb.area() >= PAR_MIN_PATCHES && parts.len() > 1; + par::each(parts, par, |(bi, ys, nn, co)| band_fn(bi, ys, nn, co)); + } + + /// [`Solver::patchmatch`] swept in wavefronts: the cells of each band in blocks of a row, a + /// block once the block before it in its row and the one above it (in the pass's direction) + /// are done. A target's random numbers are those it would draw in raster order (each target + /// draws the same count). + fn patchmatch_wavefront( + &self, + nnf: &mut [(i32, i32)], + cost: &mut [f32], + bands: &[(usize, usize, usize)], + reverse: bool, + band_seed: &(dyn Fn(usize) -> u64 + Sync), + search_radius: i32, + ) { + use std::sync::atomic::{AtomicU32, AtomicU64, Ordering::Relaxed}; + + let tb = self.tbox; + let tw = tb.width(); + let step: i32 = if reverse { -1 } else { 1 }; + let pack = |(x, y): (i32, i32)| (u64::from(x as u32) << 32) | u64::from(y as u32); + let unpack = |v: u64| ((v >> 32) as u32 as i32, v as u32 as i32); + // Random numbers each target draws. + let mut draws = 0u64; + let mut rad = search_radius; + while rad >= 1 { + draws += 2; + rad /= 2; + } + // Targets before each column of each row of the box. + let mut before = vec![0u32; tb.height() * (tw + 1)]; + for (ry, row) in before.chunks_mut(tw + 1).enumerate() { + for lx in 0..tw { + row[lx + 1] = row[lx] + u32::from(self.targets[ry * tw + lx]); + } + } + // Targets visited before each of a band's rows (in the pass's order). + let row_base: Vec> = bands + .iter() + .map(|&(_, ys, rows)| { + let mut base = Vec::with_capacity(rows); + let mut n = 0u64; + for pr in 0..rows { + base.push(n); + let ly = if reverse { rows - 1 - pr } else { pr }; + n += u64::from(before[(ys + ly - tb.y0) * (tw + 1) + tw]); + } + base + }) + .collect(); + let block = (tw / 8).clamp(4, 32); + let blocks = tw.div_ceil(block); + let nn: Vec = nnf[..tb.area()].iter().map(|m| AtomicU64::new(pack(*m))).collect(); + let co: Vec = cost[..tb.area()].iter().map(|c| AtomicU32::new(c.to_bits())).collect(); + let sweep = |(j, pr, b): (usize, usize, usize)| { + let (bi, ys, rows) = bands[j]; + let ly = if reverse { rows - 1 - pr } else { pr }; + let y = ys + ly; + let ry = y - tb.y0; + let (pc0, pc1) = (b * block, ((b + 1) * block).min(tw)); + // The targets visited before this block, in raster order. + let first = if reverse { tw - 1 - pc0 } else { pc0 }; + let row = &before[ry * (tw + 1)..(ry + 1) * (tw + 1)]; + let in_row = if reverse { row[tw] - row[first + 1] } else { row[first] }; + let mut rng = Rng::new(band_seed(bi)); + rng.skip((row_base[j][pr] + u64::from(in_row)) * draws); + for pc in pc0..pc1 { + let lx = if reverse { tw - 1 - pc } else { pc }; + let k = ry * tw + lx; + if !self.targets[k] { + continue; + } + let nx = lx as i32 - step; + let left = (nx >= 0 && (nx as usize) < tw).then(|| unpack(nn[ry * tw + nx as usize].load(Relaxed))); + let nly = ly as i32 - step; + let up = (nly >= 0 && (nly as usize) < rows).then(|| unpack(nn[(ys + nly as usize - tb.y0) * tw + lx].load(Relaxed))); + let cur = (unpack(nn[k].load(Relaxed)), f32::from_bits(co[k].load(Relaxed))); + let t = ((tb.x0 + lx) as i32, y as i32); + let (m, c) = self.pm_step(t, k, cur, left, up, step, &mut rng, search_radius); + nn[k].store(pack(m), Relaxed); + co[k].store(c.to_bits(), Relaxed); + } + }; + // Wavefront `d`: block `b` of (visited) row `pr` with `pr + b = d`, in every band. Each + // wave ends before the next starts. + let longest = bands.iter().map(|b| b.2).max().unwrap_or(0); + for d in 0..longest + blocks - 1 { + if self.ctl.cancelled() { + return; + } + let wave: Vec<(usize, usize, usize)> = bands + .iter() + .enumerate() + .flat_map(|(j, &(_, _, rows))| (0..rows).filter(move |&pr| pr <= d && d - pr < blocks).map(move |pr| (j, pr, d - pr))) + .collect(); + par::each(wave, true, sweep); + } + for (m, a) in nnf.iter_mut().zip(&nn) { + *m = unpack(a.load(Relaxed)); + } + for (c, a) in cost.iter_mut().zip(&co) { + *c = f32::from_bits(a.load(Relaxed)); + } + } + + /// Recompute every target's match cost (hole pixels change between iterations), and with + /// colour adaptation the full target patches' own moments. + pub(super) fn costs(&mut self, nnf: &[(i32, i32)], cost: &mut [f32]) -> Result<(), photocraft_raster::Cancelled> { + let (w, ch, r, tb) = (self.w, self.ch, self.r as usize, self.tbox); + let par = tb.area() >= PAR_MIN_PATCHES; + self.ctl.check()?; + if !self.adapt.is_none() { + let c8 = ch.min(MOM_CH); + let side = 2 * r + 1; + let mut tmom = std::mem::take(&mut self.tmom); + let (img, full, ctl) = (&*self.img, &self.full, &self.ctl); + par::chunks_mut(&mut tmom, tb.width() * c8, par, |ty, out| { + if ctl.cancelled() { + return; + } + for tx in 0..tb.width() { + if full[ty * tb.width() + tx] { + patch_moments(img, w, ch, c8, side, tb.x0 + tx - r, tb.y0 + ty - r, &mut out[tx * c8..(tx + 1) * c8]); + } + } + }); + self.tmom = tmom; + } + let this = &*self; + par::chunks_mut(cost, tb.width(), par, |ty, co| { + if this.ctl.cancelled() { + return; + } + let y = tb.y0 + ty; + for (tx, c) in co.iter_mut().enumerate() { + let k = ty * tb.width() + tx; + if this.targets[k] { + *c = this.dist(((tb.x0 + tx) as i32, y as i32), k, nnf[k], f32::INFINITY); + } + } + }); + self.ctl.check() + } + + /// Re-estimate hole pixels by a similarity-weighted vote of all overlapping patch matches + /// (each copy adjusted by its gain and bias). + pub(super) fn vote(&mut self, nnf: &[(i32, i32)], cost: &[f32]) { + let (w, h, ch, r) = (self.w, self.h, self.ch, self.r); + let (tb, hb) = (self.tbox, self.hole_box); + // A cancelled solve is thrown away. + if self.ctl.cancelled() { + return; + } + let used = |k: usize| self.targets[k] && cost[k].is_finite(); + // σ² from the 75th percentile of match costs (Wexler et al. use a robust scale like this). + let cs: Vec = (0..tb.area()).filter(|&k| used(k)).map(|k| cost[k]).collect(); + let mut cs = cs; + let sigma2 = if cs.is_empty() { + 1.0 + } else { + let k = (cs.len() * 3 / 4).min(cs.len() - 1); + let (_, v, _) = cs.select_nth_unstable_by(k, |a, b| a.total_cmp(b)); + v.max(1e-6) + }; + drop(cs); + // Each target's weight and photometric transform (computed once, used by every pixel it + // covers). + let adapted = !self.adapt.is_none(); + let par = tb.area() >= PAR_MIN_PATCHES; + // A weight (zero for a target that doesn't vote) and its match's centre (as a pixel + // index) per target. + let mut wt = vec![0.0f32; tb.area()]; + let mut src = vec![0isize; tb.area()]; + par::chunks2_mut(&mut wt, tb.width(), &mut src, tb.width(), par, |ty, wrow, srow| { + for (tx, (o, sc)) in wrow.iter_mut().zip(srow.iter_mut()).enumerate() { + let k = ty * tb.width() + tx; + if used(k) { + *o = (-cost[k] / (2.0 * sigma2)).exp().max(1e-8); + *sc = nnf[k].1 as isize * w as isize + nnf[k].0 as isize; + } + } + }); + let mut gb: Vec<(f32, f32)> = Vec::new(); + if adapted { + gb = vec![(1.0f32, 0.0f32); tb.area() * ch]; + let this = &*self; + par::chunks_mut(&mut gb, tb.width() * ch, par, |ty, out| { + if this.ctl.cancelled() { + return; + } + let y = tb.y0 + ty; + for tx in 0..tb.width() { + let k = ty * tb.width() + tx; + if used(k) { + this.transform(((tb.x0 + tx) as i32, y as i32), k, nnf[k], &mut out[tx * ch..(tx + 1) * ch]); + } + } + }); + } + // The vote, into a buffer over the hole's bounds (every pixel reads the old pixels). + let votes = Votes { w, h, ch, r: r as usize, tbox: tb, img: &*self.img, wt: &wt, src: &src, gb: &gb }; + let (px, ctl) = (self.px, &self.ctl); + let mut out = vec![0.0f32; hb.area() * ch]; + let mut got = vec![false; hb.area()]; + let row = |yy: usize, orow: &mut [f32], grow: &mut [bool]| { + if ctl.cancelled() { + return; + } + let y = hb.y0 + yy; + for x in hb.x0..hb.x1 { + if px[y * w + x] != Px::Hole { + continue; + } + let o = (x - hb.x0) * ch; + let dst = &mut orow[o..o + ch]; + grow[x - hb.x0] = match (ch, adapted) { + (1, false) => votes.pixel::<1, false>(x, y, dst), + (3, false) => votes.pixel::<3, false>(x, y, dst), + (4, false) => votes.pixel::<4, false>(x, y, dst), + (1, true) => votes.pixel::<1, true>(x, y, dst), + (3, true) => votes.pixel::<3, true>(x, y, dst), + (4, true) => votes.pixel::<4, true>(x, y, dst), + _ => votes.pixel_any(x, y, adapted, dst), + }; + } + }; + par::chunks2_mut(&mut out, hb.width() * ch, &mut got, hb.width(), hb.area() >= PAR_MIN_PATCHES, row); + for yy in 0..hb.height() { + for xx in 0..hb.width() { + if got[yy * hb.width() + xx] { + let (d, s) = (((hb.y0 + yy) * w + hb.x0 + xx) * ch, (yy * hb.width() + xx) * ch); + self.img[d..d + ch].copy_from_slice(&out[s..s + ch]); + } + } + } + } +} + +/// What [`Solver::vote`] votes with: per target (over the target box) its weight (zero when it +/// doesn't vote), its match's centre as a pixel index, and with colour adaptation its gain and +/// bias per channel. +struct Votes<'a> { + w: usize, + h: usize, + ch: usize, + r: usize, + tbox: Bounds, + img: &'a [f32], + wt: &'a [f32], + src: &'a [isize], + gb: &'a [(f32, f32)], +} + +impl Votes<'_> { + /// Hole pixel `(x, y)`'s vote into `out` (`CH` channels): the weighted mean of what the + /// matches of the patches covering it put there, summed patch by patch in raster order of + /// their centres. False when no patch votes. + #[inline(always)] + fn pixel(&self, x: usize, y: usize, out: &mut [f32]) -> bool { + let (w, r, tb) = (self.w, self.r, self.tbox); + let mut acc = [0.0f32; CH]; + let mut wsum = 0.0f32; + for ty in y.saturating_sub(r)..=(y + r).min(self.h - 1) { + let dy = ty as isize - y as isize; + let krow = (ty - tb.y0) * tb.width(); + for tx in x.saturating_sub(r)..=(x + r).min(w - 1) { + let k = krow + tx - tb.x0; + let wt = self.wt[k]; + if wt == 0.0 { + continue; + } + // The pixel of the match's patch that lands here. + let si = (self.src[k] - dy * w as isize - (tx as isize - x as isize)) as usize * CH; + let s = &self.img[si..si + CH]; + for c in 0..CH { + let v = if ADAPT { + let (g, b) = self.gb[k * CH + c]; + g * s[c] + b + } else { + s[c] + }; + acc[c] += v * wt; + } + wsum += wt; + } + } + if wsum > 0.0 { + for (d, a) in out.iter_mut().zip(&acc) { + *d = a / wsum; + } + return true; + } + false + } + + /// [`Votes::pixel`] for any number of channels. + fn pixel_any(&self, x: usize, y: usize, adapted: bool, out: &mut [f32]) -> bool { + let (w, r, tb, ch) = (self.w, self.r, self.tbox, self.ch); + let mut acc = vec![0.0f32; ch]; + let mut wsum = 0.0f32; + for ty in y.saturating_sub(r)..=(y + r).min(self.h - 1) { + let dy = ty as isize - y as isize; + let krow = (ty - tb.y0) * tb.width(); + for tx in x.saturating_sub(r)..=(x + r).min(w - 1) { + let k = krow + tx - tb.x0; + let wt = self.wt[k]; + if wt == 0.0 { + continue; + } + let si = (self.src[k] - dy * w as isize - (tx as isize - x as isize)) as usize * ch; + for (c, a) in acc.iter_mut().enumerate() { + let v = if adapted { + let (g, b) = self.gb[k * ch + c]; + g * self.img[si + c] + b + } else { + self.img[si + c] + }; + *a += v * wt; + } + wsum += wt; + } + } + if wsum > 0.0 { + for (d, a) in out.iter_mut().zip(&acc) { + *d = a / wsum; + } + return true; + } + false + } +} + +/// `Σv, Σv²` per channel (the first `out.len()`) of the `side × side` patch with top-left +/// `(x0, y0)`, summed row by row, pixel by pixel, as [`Solver::moments`] does. +#[allow(clippy::too_many_arguments)] +fn patch_moments(img: &[f32], w: usize, ch: usize, c8: usize, side: usize, x0: usize, y0: usize, out: &mut [[f32; 2]]) { + out.iter_mut().for_each(|v| *v = [0.0; 2]); + for y in y0..y0 + side { + let row = &img[(y * w + x0) * ch..(y * w + x0 + side) * ch]; + for p in row.chunks_exact(ch) { + for (c, o) in out.iter_mut().enumerate().take(c8) { + let a = p[c]; + o[0] += a; + o[1] += a * a; + } + } + } +} + +/// The adapted distance from the moments `[Σt, Σt², Σs, Σs², Σts]` of each channel over `n` +/// samples: what is left of the target after the source is fitted to it, per sample. +#[inline] +fn adapted_cost(a: Adapt, nf: f32, m: &[[f32; 5]]) -> f32 { + let mut sum = 0.0f32; + for mc in m { + let (g, b) = fit(a, nf, mc[0], mc[1], mc[2], mc[3]); + // Σ (t − g s − b)², expanded. + sum += mc[1] + g * g * mc[3] + nf * b * b - 2.0 * g * mc[4] - 2.0 * b * mc[0] + 2.0 * g * b * mc[2]; + } + sum.max(0.0) / (nf * m.len() as f32) +} + +/// `Σ (t − s)²` over a target patch's known pixels (offsets `offs` from its centre, samples +/// `tv`, `CH` per pixel) against the source patch centred at pixel `base`, summed in that order; +/// `None` once the sum reaches `limit` (it only grows). +#[inline(always)] +pub(super) fn known_sse(image: &[f32], offs: &[isize], tv: &[f32], base: isize, limit: f32) -> Option { + let mut e = 0.0f32; + for (o, t) in offs.iter().zip(tv.as_chunks::().0) { + let si = (base + o) as usize * CH; + let s = &image[si..si + CH]; + for c in 0..CH { + let d = t[c] - s[c]; + e += d * d; + } + if e >= limit { + return None; + } + } + Some(e) +} + +/// [`known_sse`] of [`LANES`] sources at once, each against its own limit (summed in step: the +/// sums are independent, so the processor overlaps them). +#[inline(always)] +pub(super) fn known_sse_lanes(image: &[f32], offs: &[isize], tv: &[f32], bases: [isize; LANES], limits: [f32; LANES]) -> [Option; LANES] { + let mut e = [0.0f32; LANES]; + for (o, t) in offs.iter().zip(tv.as_chunks::().0) { + for l in 0..LANES { + let si = (bases[l] + o) as usize * CH; + let s = &image[si..si + CH]; + for c in 0..CH { + let d = t[c] - s[c]; + e[l] += d * d; + } + } + // Sums only grow: once all of them reached their limits, none can win. + if (0..LANES).all(|l| e[l] >= limits[l]) { + break; + } + } + std::array::from_fn(|l| (e[l] < limits[l]).then_some(e[l])) +} + +/// Sources [`known_sse_lanes`] sums at once. +pub(super) const LANES: usize = 4; + +/// [`known_sse`] for any number of channels. +pub(super) fn known_sse_any(image: &[f32], ch: usize, offs: &[isize], tv: &[f32], base: isize, limit: f32) -> Option { + let mut e = 0.0f32; + for (o, t) in offs.iter().zip(tv.chunks_exact(ch.max(1))) { + let si = (base + o) as usize * ch; + for (tc, sc) in t.iter().zip(&image[si..si + ch]) { + let d = tc - sc; + e += d * d; + } + if e >= limit { + return None; + } + } + Some(e) +} + +/// The best source so far of [`onion_peel`]'s search. +#[derive(Clone, Copy)] +pub(super) struct Best { + /// Error per sample, and the place in the sources. + pub(super) v: f32, + pub(super) j: usize, + /// Sums of squared errors from which `sum / n` is above `v`, and at least `v`. + pub(super) worse: f32, + pub(super) equal: f32, +} + +/// The least sum `e ≥ 0` with `e / n > v` (`strict`) or `e / n ≥ v` in float arithmetic (the +/// quotient only grows with `e`); infinity for an infinite `v`. +pub(super) fn least_sum(v: f32, n: f32, strict: bool) -> f32 { + let holds = |e: f32| if strict { e / n > v } else { e / n >= v }; + if !v.is_finite() || !n.is_finite() || n <= 0.0 { + return f32::INFINITY; + } + let mut e = (v * n).max(0.0); + while e > 0.0 && holds(e.next_down()) { + e = e.next_down(); + } + while !holds(e) && e < f32::INFINITY { + e = e.next_up(); + } + e +} diff --git a/crates/algo/src/inpaint/sources.rs b/crates/algo/src/inpaint/sources.rs new file mode 100644 index 0000000000..fcc255e49a --- /dev/null +++ b/crates/algo/src/inpaint/sources.rs @@ -0,0 +1,326 @@ +use super::common::{Bounds, Level, PAR_MIN, Px}; +use super::{Cow, par}; + +/// Half-size level: a cell is a hole if any of its pixels is; else it averages its non-void +/// pixels, and may be copied from only if all of them may. +pub(super) fn downsample(l: &Level, ch: usize) -> Level<'static> { + let (w, h) = (l.w.div_ceil(2), l.h.div_ceil(2)); + let mut img = vec![0.0f32; w * h * ch]; + let mut px = vec![Px::Void; w * h]; + let (lw, lh, limg, lpx) = (l.w, l.h, &*l.img, &*l.px); + let row = |y: usize, irow: &mut [f32], prow: &mut [Px]| { + for (x, p) in prow.iter_mut().enumerate() { + let mut n = 0.0f32; + let o = x * ch; + let (mut hole, mut all_source, mut any) = (false, true, false); + for (dx, dy) in [(0, 0), (1, 0), (0, 1), (1, 1)] { + let (sx, sy) = (x * 2 + dx, y * 2 + dy); + if sx >= lw || sy >= lh { + continue; + } + let si = sy * lw + sx; + match lpx[si] { + Px::Hole => hole = true, + Px::Void => all_source = false, + k => { + any = true; + all_source &= k == Px::Source; + for c in 0..ch { + irow[o + c] += limg[si * ch + c]; + } + n += 1.0; + } + } + } + if n > 0.0 { + for c in 0..ch { + irow[o + c] /= n; + } + } + *p = if hole { + Px::Hole + } else if !any { + Px::Void + } else if all_source { + Px::Source + } else { + Px::Known + }; + } + }; + par::chunks2_mut(&mut img, w * ch, &mut px, w, w * h >= PAR_MIN, row); + Level { w, h, img: Cow::Owned(img), px: Cow::Owned(px) } +} + +/// Summed-area table of a boolean grid (`(w+1) × (h+1)`). Not inlined: in [`synthesize`] it made +/// Create Texture's candidate loop slower (measured). +#[inline(never)] +pub(super) fn integral(w: usize, h: usize, m: &[bool]) -> Vec { + let mut s = vec![0u32; (w + 1) * (h + 1)]; + for y in 0..h { + let mut row = 0u32; + for x in 0..w { + row += u32::from(m[y * w + x]); + s[(y + 1) * (w + 1) + x + 1] = s[y * (w + 1) + x + 1] + row; + } + } + s +} + +/// Count of set cells in the clipped window `[x0, x1) × [y0, y1)`. +pub(super) fn window_count(s: &[u32], gw: usize, gh: usize, x0: i32, y0: i32, x1: i32, y1: i32) -> u32 { + let (x0, y0) = (x0.max(0) as usize, y0.max(0) as usize); + let (x1, y1) = ((x1.max(0) as usize).min(gw), (y1.max(0) as usize).min(gh)); + if x1 <= x0 || y1 <= y0 { + return 0; + } + let w1 = gw + 1; + s[y1 * w1 + x1] + s[y0 * w1 + x0] - s[y0 * w1 + x1] - s[y1 * w1 + x0] +} + +pub(super) fn hole_bbox(w: usize, h: usize, hole: &[bool]) -> Option<(usize, usize, usize, usize)> { + let mut b: Option<(usize, usize, usize, usize)> = None; + for y in 0..h { + for x in 0..w { + if hole[y * w + x] { + b = Some(match b { + None => (x, y, x + 1, y + 1), + Some((x0, y0, x1, y1)) => (x0.min(x), y0.min(y), x1.max(x + 1), y1.max(y + 1)), + }); + } + } + } + b +} + +/// A hole at most `1 / THIN_RATIO` as thick as its bounds are long counts as thin (a wire, a +/// string, a hair, a long brush stroke): see the module docs. +pub(super) const THIN_RATIO: usize = 4; + +/// How thick the hole within bounds `hb` of a `w`-wide role grid is: twice the largest distance +/// from a hole pixel to the nearest pixel that is not one (a long, thin hole is thin however long +/// its bounds). +pub(super) fn hole_thickness(w: usize, hb: Bounds, px: &[Px]) -> usize { + // The bounds and a ring of non-hole cells around them (also past the grid's edge). + let (gw, gh) = (hb.width() + 2, hb.height() + 2); + let outside: Vec = par::map(gw * gh, gw * gh >= PAR_MIN, |i| { + let (gx, gy) = (i % gw, i / gw); + if gx == 0 || gy == 0 || gx > hb.width() || gy > hb.height() { + return true; + } + px.get((hb.y0 + gy - 1) * w + hb.x0 + gx - 1) != Some(&Px::Hole) + }); + let deepest = crate::selection::edt(&outside, gw, gh).into_iter().fold(0.0f32, f32::max); + (2.0 * deepest).ceil() as usize +} + +/// Most target pixels × sources the onion peel of a thin hole may compare (its search is +/// exhaustive, though most comparisons stop early: about a second at this many). +const PEEL_BUDGET: usize = 1 << 28; + +/// Whether the onion peel of the hole cells within `hb` of a `w`-wide role grid from `sources` +/// source centres stays within [`PEEL_BUDGET`]. +pub(super) fn peel_affordable(w: usize, px: &[Px], hb: Bounds, sources: usize) -> bool { + let holes: usize = (hb.y0..hb.y1).map(|y| px.get(y * w + hb.x0..y * w + hb.x1).map_or(0, |row| row.iter().filter(|k| **k == Px::Hole).count())).sum(); + holes.saturating_mul(sources) <= PEEL_BUDGET +} + +/// Bounds of the hole pixels of a role grid. +pub(super) fn hole_bounds(w: usize, h: usize, px: &[Px]) -> Option { + // A zero-width grid holds no pixels whatever its height; don't size a row table by it. + if w == 0 || h == 0 { + return None; + } + // Per row the first and last hole column; the union of the rows' spans. + let rows: Vec> = par::map(h, w * h >= PAR_MIN, |y| { + let row = px.get(y * w..(y + 1) * w)?; + let x0 = row.iter().position(|k| *k == Px::Hole)?; + let x1 = row.iter().rposition(|k| *k == Px::Hole).unwrap_or(x0) + 1; + Some((x0, x1)) + }); + let mut b: Option = None; + for (y, span) in rows.into_iter().enumerate() { + if let Some((x0, x1)) = span { + b = Some(match b { + None => Bounds { x0, y0: y, x1, y1: y + 1 }, + Some(b) => Bounds { x0: b.x0.min(x0), y0: b.y0, x1: b.x1.max(x1), y1: y + 1 }, + }); + } + } + b +} + +/// Valid source centres: the whole `2r+1` patch inside the grid and copyable. Found per row +/// (each pixel's horizontal run), then per column of runs. `None` when cancelled. +pub(super) fn valid_sources(w: usize, h: usize, r: usize, px: &[Px], ctl: &photocraft_raster::Interrupt) -> Option> { + const BAND: usize = 64; + let side = 2 * r + 1; + let par = w * h >= PAR_MIN; + let mut run = vec![false; w * h]; + if w < side || h < side { + return Some(run); + } + // Per row: is the run of `side` pixels centred on each one all copyable (a sliding count)? + par::chunks_mut(&mut run, w, par, |y, out| { + if y % BAND == 0 && ctl.cancelled() { + return; + } + let row = &px[y * w..(y + 1) * w]; + let copyable = |x: usize| usize::from(row[x] == Px::Source); + let mut count: usize = (0..side).map(copyable).sum(); + out[r] = count == side; + for (x, o) in out.iter_mut().enumerate().take(w - r).skip(r + 1) { + count = count + copyable(x + r) - copyable(x - r - 1); + *o = count == side; + } + }); + if ctl.cancelled() { + return None; + } + // Per column, in bands of rows: how many such runs end each row in a row; a centre is valid + // where `side` of them stack up to `r` rows below it. + let mut valid = vec![false; w * h]; + par::chunks_mut(&mut valid, BAND * w, par, |band, out| { + if ctl.cancelled() { + return; + } + let (y0, rows) = (band * BAND, out.len() / w); + let mut stack = vec![0usize; w]; + for yy in y0.saturating_sub(r)..(y0 + rows + r).min(h) { + for (x, n) in stack.iter_mut().enumerate() { + *n = if run[yy * w + x] { *n + 1 } else { 0 }; + } + // `yy` is the bottom row of centre `y`'s patch. + if yy < y0 + r || yy < 2 * r { + continue; + } + let y = yy - r; + for (v, n) in out[(y - y0) * w..(y - y0 + 1) * w].iter_mut().zip(&stack).take(w - r).skip(r) { + *v = *n >= side; + } + } + }); + if ctl.cancelled() { + return None; + } + Some(valid) +} + +/// Columns per block of [`Sources`]' index. +const SOURCE_BLOCK: usize = 64; + +/// The valid source centres in row-major order, indexed by counts per row and per block of +/// [`SOURCE_BLOCK`] columns (so the `k`-th is found without listing them all). +pub(super) struct Sources { + /// Valid centres before each row (`h + 1` entries). + row_start: Vec, + /// Per row, the valid centres in it before each block. + block_start: Vec, + blocks: usize, +} + +impl Sources { + pub(super) fn new(valid: &[bool], w: usize) -> Sources { + let h = valid.len().checked_div(w).unwrap_or(0); + let blocks = w.div_ceil(SOURCE_BLOCK); + let mut block_start = vec![0u32; h * blocks]; + // Per row: the block prefix, and the row's count. + let counts: Vec = par::map(h, valid.len() >= PAR_MIN, |y| valid[y * w..(y + 1) * w].iter().filter(|v| **v).count()); + par::chunks_mut(&mut block_start, blocks, valid.len() >= PAR_MIN, |y, out| { + let mut n = 0u32; + for (b, o) in out.iter_mut().enumerate() { + *o = n; + n += valid[y * w + b * SOURCE_BLOCK..y * w + ((b + 1) * SOURCE_BLOCK).min(w)].iter().filter(|v| **v).count() as u32; + } + }); + let mut row_start = Vec::with_capacity(counts.len() + 1); + let mut total = 0usize; + row_start.push(0); + for c in counts { + total += c; + row_start.push(total); + } + Sources { row_start, block_start, blocks } + } + + pub(super) fn len(&self) -> usize { + self.row_start.last().copied().unwrap_or(0) + } + + /// The `k`-th valid centre in row-major order (`k < len`). + pub(super) fn nth(&self, k: usize, valid: &[bool], w: usize) -> (i32, i32) { + let y = self.row_start.partition_point(|&s| s <= k).saturating_sub(1); + let left = k.saturating_sub(self.row_start.get(y).copied().unwrap_or(0)); + let row_blocks = self.block_start.get(y * self.blocks..(y + 1) * self.blocks).unwrap_or(&[]); + let b = row_blocks.partition_point(|&s| s as usize <= left).saturating_sub(1); + let mut left = left.saturating_sub(row_blocks.get(b).copied().unwrap_or(0) as usize); + let x0 = b * SOURCE_BLOCK; + for (x, v) in valid.iter().skip(y * w + x0).take(w.saturating_sub(x0)).enumerate() { + if *v { + if left == 0 { + return ((x0 + x) as i32, y as i32); + } + left -= 1; + } + } + (0, 0) + } + + /// All of them, in row-major order. + pub(super) fn all(&self, valid: &[bool], w: usize) -> Vec<(i32, i32)> { + let mut out = Vec::with_capacity(self.len()); + for (i, v) in valid.iter().enumerate() { + if *v { + out.push(((i % w) as i32, (i / w) as i32)); + } + } + out + } +} + +/// Per cell of `tb` (the hole's bounds `hb` grown by the patch radius, where all of them lie): is +/// it a target centre (its patch overlaps the hole)? +pub(super) fn targets_around(w: usize, r: usize, px: &[Px], hb: Bounds, tb: Bounds) -> Vec { + // Hole counts summed over the hole's bounds: every hole pixel lies inside them. + let (bw, bh) = (hb.width(), hb.height()); + let mut s = vec![0u32; (bw + 1) * (bh + 1)]; + for y in 0..bh { + let mut row = 0u32; + for x in 0..bw { + row += u32::from(px[(hb.y0 + y) * w + hb.x0 + x] == Px::Hole); + s[(y + 1) * (bw + 1) + x + 1] = s[y * (bw + 1) + x + 1] + row; + } + } + let mut targets = vec![false; tb.area()]; + let ri = r as i32; + let (ox, oy) = (hb.x0 as i32, hb.y0 as i32); + for y in tb.y0..tb.y1 { + for x in tb.x0..tb.x1 { + let k = (y - tb.y0) * tb.width() + (x - tb.x0); + let (x, y) = (x as i32, y as i32); + targets[k] = window_count(&s, bw, bh, x - ri - ox, y - ri - oy, x + ri + 1 - ox, y + ri + 1 - oy) > 0; + } + } + targets +} + +/// A finished coarser level: its size, pixels and match field (the source centre per cell of +/// its target box, `i32::MIN` where there is none; empty when it had nothing to copy). +pub(super) struct Coarser { + pub(super) w: usize, + pub(super) h: usize, + pub(super) img: Vec, + pub(super) field: Vec<(i32, i32)>, + pub(super) tbox: Bounds, +} + +impl Coarser { + /// The match of cell `(x, y)` (`i32::MIN` outside the target box). + pub(super) fn field_at(&self, x: usize, y: usize) -> (i32, i32) { + let t = self.tbox; + if x < t.x0 || x >= t.x1 || y < t.y0 || y >= t.y1 { + return (i32::MIN, i32::MIN); + } + self.field.get((y - t.y0) * t.width() + (x - t.x0)).copied().unwrap_or((i32::MIN, i32::MIN)) + } +} diff --git a/crates/algo/src/inpaint/synthesis.rs b/crates/algo/src/inpaint/synthesis.rs new file mode 100644 index 0000000000..cd0452ab4c --- /dev/null +++ b/crates/algo/src/inpaint/synthesis.rs @@ -0,0 +1,381 @@ +use super::common::{Bounds, Px, Rng}; +use super::solver::{Best, LANES, known_sse, known_sse_any, known_sse_lanes, least_sum}; +use super::sources::{hole_bbox, integral, window_count}; +use super::{HoleIntegral, membrane_fill, par}; + +/// Fill the hole from its edge inwards, one ring at a time: each pixel takes the centre of the +/// source patch that best matches what is already known around it (the first in `sources` of +/// those that match best). `hb` bounds the hole. +/// +/// The search is exhaustive, but most sources are given up early (#2771, #2805): the target's +/// known pixels are gathered once, the search starts from where the pixel's filled neighbours +/// took their sources (or every 16th source at the hole's edge), and a source's error stops being +/// added up once it can only lose (it only grows). So it finds the same source as a plain scan in +/// order, sooner. +#[allow(clippy::too_many_arguments)] +pub(super) fn onion_peel( + w: usize, + h: usize, + ch: usize, + r: i32, + img: &mut [f32], + px: &[Px], + hb: Bounds, + sources: &[(i32, i32)], + ctl: &photocraft_raster::Interrupt, +) -> Result<(), photocraft_raster::Cancelled> { + const NONE: u32 = u32::MAX; + let mut known: Vec = px.iter().map(|k| matches!(k, Px::Known | Px::Source)).collect(); + // Each source centre's place in `sources`, and the source each filled pixel took. + let mut index = vec![NONE; w * h]; + for (j, &(x, y)) in sources.iter().enumerate() { + index[y as usize * w + x as usize] = j as u32; + } + let mut took = vec![NONE; w * h]; + let neighbours = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]; + loop { + ctl.check()?; + let mut front: Vec = Vec::new(); + for y in hb.y0..hb.y1 { + for x in hb.x0..hb.x1 { + let i = y * w + x; + if known[i] || px[i] != Px::Hole { + continue; + } + let (x, y) = (x as i32, y as i32); + let edge = (-1..=1).any(|dy: i32| { + (-1..=1).any(|dx: i32| { + let (nx, ny) = (x + dx, y + dy); + nx >= 0 && ny >= 0 && (nx as usize) < w && (ny as usize) < h && known[ny as usize * w + nx as usize] + }) + }); + if edge { + front.push(i); + } + } + } + if front.is_empty() { + break; + } + let (known_now, took_now) = (&known, &took); + let image = &*img; + let best = |&i: &usize| -> (Vec, u32) { + let (x, y) = ((i % w) as i32, (i / w) as i32); + // The known pixels of the target patch, in the plain scan's order: each one's offset + // from the patch centre (in pixels) and its samples (gathered once, #2805). + let side = (2 * r + 1) as usize; + let mut offs: Vec = Vec::with_capacity(side * side); + let mut tv: Vec = Vec::with_capacity(side * side * ch); + for dy in -r..=r { + for dx in -r..=r { + let (tx, ty) = (x + dx, y + dy); + if tx >= 0 && ty >= 0 && (tx as usize) < w && (ty as usize) < h && known_now[ty as usize * w + tx as usize] { + let ti = (ty as usize * w + tx as usize) * ch; + offs.push(dy as isize * w as isize + dx as isize); + tv.extend_from_slice(&image[ti..ti + ch]); + } + } + } + // The best so far: its error per sample, its place in `sources` (the first of equals + // wins, as in a scan in order), and the sums of squares at which a source surely loses + // to it (`e / n` above the best) or ties it (at least the best). + let mut best = Best { v: f32::INFINITY, j: usize::MAX, worse: f32::INFINITY, equal: f32::INFINITY }; + if !offs.is_empty() { + let nf = offs.len() as f32; + let base_of = |j: usize| sources.get(j).map_or(0, |&(sx, sy)| sy as isize * w as isize + sx as isize); + // The sum at which source `j` can't win: a tie loses to an earlier source. + let limit_of = |j: usize, best: &Best| if j > best.j { best.equal } else { best.worse }; + // Source `j`'s whole sum `e` (it did not reach its limit): does it win? + let adopt = |j: usize, e: f32, best: &mut Best| { + let v = e / nf; + if v < f32::INFINITY && (v < best.v || (v == best.v && j < best.j)) { + *best = Best { v, j, worse: least_sum(v, nf, true), equal: least_sum(v, nf, false) }; + } + }; + let consider = |j: usize, best: &mut Best| { + if j >= sources.len() { + return; + } + let (base, limit) = (base_of(j), limit_of(j, best)); + let e = match ch { + 1 => known_sse::<1>(image, &offs, &tv, base, limit), + 3 => known_sse::<3>(image, &offs, &tv, base, limit), + 4 => known_sse::<4>(image, &offs, &tv, base, limit), + _ => known_sse_any(image, ch, &offs, &tv, base, limit), + }; + if let Some(e) = e { + adopt(j, e, best); + } + }; + // A close bound early: where the filled neighbours' sources point (shifted as in + // PatchMatch's propagation), or at the hole's edge, where none is filled yet, + // every 16th source. + for (dx, dy) in neighbours { + let (qx, qy) = (x + dx, y + dy); + if qx < 0 || qy < 0 || qx as usize >= w || qy as usize >= h { + continue; + } + let Some(&(sx, sy)) = sources.get(took_now[qy as usize * w + qx as usize] as usize) else { continue }; + let (cx, cy) = (sx - dx, sy - dy); + if cx >= 0 && cy >= 0 && (cx as usize) < w && (cy as usize) < h && index[cy as usize * w + cx as usize] != NONE { + consider(index[cy as usize * w + cx as usize] as usize, &mut best); + } + } + if best.j == usize::MAX { + for j in (0..sources.len()).step_by(16) { + consider(j, &mut best); + } + } + // The scan in order, `LANES` sources at a time (their sums are independent, so they + // overlap), each against its limit from the best before them: the best only gets + // better, so a source given up would have lost to it anyway. + let mut j = 0; + while j + LANES <= sources.len() { + if j.is_multiple_of(4096) && j > 0 && ctl.cancelled() { + break; + } + let bases: [isize; LANES] = std::array::from_fn(|l| base_of(j + l)); + let limits: [f32; LANES] = std::array::from_fn(|l| limit_of(j + l, &best)); + let sums = match ch { + 1 => known_sse_lanes::<1>(image, &offs, &tv, bases, limits), + 3 => known_sse_lanes::<3>(image, &offs, &tv, bases, limits), + 4 => known_sse_lanes::<4>(image, &offs, &tv, bases, limits), + _ => std::array::from_fn(|l| known_sse_any(image, ch, &offs, &tv, bases[l], limits[l])), + }; + for (l, e) in sums.into_iter().enumerate() { + if let Some(e) = e { + adopt(j + l, e, &mut best); + } + } + j += LANES; + } + for j in j..sources.len() { + consider(j, &mut best); + } + } + let (sx, sy) = sources.get(best.j).copied().unwrap_or((0, 0)); + let si = (sy as usize * w + sx as usize) * ch; + (image[si..si + ch].to_vec(), if best.j == usize::MAX { NONE } else { best.j as u32 }) + }; + let vals: Vec<(Vec, u32)> = par::map(front.len(), front.len() * sources.len() >= 4096, |k| best(&front[k])); + ctl.check()?; + for (&i, (v, j)) in front.iter().zip(vals) { + img[i * ch..(i + 1) * ch].copy_from_slice(&v); + known[i] = true; + took[i] = j; + } + } + Ok(()) +} + +/// Proximity match: the displacement `(dx, dy)` (within `max_radius`) whose surroundings best match the +/// ring of `ring` pixels around the hole, with the displaced hole fully inside the grid and outside the +/// hole. `None` if no displacement fits. +pub fn best_offset(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], ring: usize, max_radius: i32) -> Option<(i32, i32)> { + let (bx0, by0, bx1, by1) = hole_bbox(w, h, hole)?; + // No valid translation can move farther than the source image's extent. + // Bound arbitrary public API inputs before constructing i32 search ranges: + // usize -> i32 truncation and signed increments otherwise overflow or hang. + let extent = w.max(h).min((i32::MAX / 4) as usize) as i32; + let max_radius = max_radius.max(0).min(extent); + if max_radius == 0 { + return None; + } + let ring = ring.max(1).min(extent as usize) as i32; + // Every set cell lies in the hole bounds. Counts outside that rectangle are zero, + // so source rejection needs a table for the hole, not for the whole photo. + let hs = HoleIntegral::new(w, hole, (bx0, by0, bx1, by1))?; + // Sample points: the hole's ring (dilated minus hole), subsampled for speed. + let mut pts = Vec::new(); + let (x0, y0) = (bx0 as i32 - ring, by0 as i32 - ring); + let (x1, y1) = (bx1 as i32 + ring, by1 as i32 + ring); + for y in y0.max(0)..y1.min(h as i32) { + for x in x0.max(0)..x1.min(w as i32) { + if hole[y as usize * w + x as usize] { + continue; + } + if hs.count(x - ring, y - ring, x + ring + 1, y + ring + 1) > 0 { + pts.push((x, y)); + } + } + } + let stride = (pts.len() / 2000).max(1); + let pts: Vec<(i32, i32)> = pts.into_iter().step_by(stride).collect(); + let eval = |dx: i32, dy: i32, cutoff: f32| -> Option { + let (hx0, hy0, hx1, hy1) = (x0 + dx, y0 + dy, x1 + dx, y1 + dy); + let inside = hx0 >= 0 && hy0 >= 0 && hx1 <= w as i32 && hy1 <= h as i32; + if (dx, dy) == (0, 0) || !inside || hs.count(hx0, hy0, hx1, hy1) != 0 { + return None; + } + let mut e = 0.0f32; + let n = pts.len().max(1) as f32; + let penalty = 1.0 + 1e-3 * ((dx as f32).hypot(dy as f32) / max_radius as f32); + for (i, &(x, y)) in pts.iter().enumerate() { + let (a, b) = ((y as usize * w + x as usize) * ch, ((y + dy) as usize * w + (x + dx) as usize) * ch); + for c in 0..ch { + let d = img[a + c] - img[b + c]; + e += d * d; + } + // SSD terms are nonnegative. The partial score is a lower bound on the + // final score; use the same division and penalty to keep float rounding + // and strict tie ordering unchanged. Check in batches to keep this cheap. + if i % 32 == 31 && cutoff.is_finite() && e / n * penalty >= cutoff { + return None; + } + } + // Mild preference for nearer sources on ties. + Some(e / n * penalty) + }; + // Coarse grid over the search window, then a full-resolution refinement around the winner. + let step = (max_radius / 24).max(1); + let mut best: Option<((i32, i32), f32)> = None; + let consider = |dx: i32, dy: i32, best: &mut Option<((i32, i32), f32)>| { + if let Some(e) = eval(dx, dy, best.map_or(f32::INFINITY, |(_, error)| error)) + && best.is_none_or(|(_, be)| e < be) + { + *best = Some(((dx, dy), e)); + } + }; + let mut dy = -max_radius; + while dy <= max_radius { + let mut dx = -max_radius; + while dx <= max_radius { + consider(dx, dy, &mut best); + dx += step; + } + dy += step; + } + if step > 1 + && let Some(((bx, by), _)) = best + { + for dy in (by - step)..=(by + step) { + for dx in (bx - step)..=(bx + step) { + consider(dx, dy, &mut best); + } + } + } + best.map(|b| b.0) +} + +/// Create texture: fill the hole with blocks of surrounding texture (see module docs). +pub fn synthesize(w: usize, h: usize, ch: usize, img: &[f32], hole: &[bool], block: usize, seed: u64) -> Vec { + let mut out = img.to_vec(); + let Some((bx0, by0, bx1, by1)) = hole_bbox(w, h, hole) else { return out }; + let b = block.max(4) as i32; + let ov = (b / 3).max(1); + let hs = integral(w, h, hole); + // Candidate block origins: whole block (with overlap) inside the grid and outside the hole. + let mut cands = Vec::new(); + for y in ov..(h as i32 - b - ov) { + for x in ov..(w as i32 - b - ov) { + if window_count(&hs, w, h, x - ov, y - ov, x + b + ov, y + b + ov) == 0 { + cands.push((x, y)); + } + } + } + if cands.is_empty() { + return membrane_fill(w, h, ch, img, hole); + } + let mut filled: Vec = hole.iter().map(|x| !x).collect(); + let mut rng = Rng::new(seed); + let mut by = by0 as i32; + while by < by1 as i32 { + let mut bx = bx0 as i32; + while bx < bx1 as i32 { + if window_count(&hs, w, h, bx, by, bx + b, by + b) > 0 { + // Pick the candidate whose block best agrees with already-known pixels around/inside. + let mut best = (cands[0], f32::INFINITY); + for _ in 0..48 { + let c = cands[(rng.next() % cands.len() as u64) as usize]; + let (mut e, mut n) = (0.0f32, 0u32); + for y in (by - ov)..(by + b + ov) { + for x in (bx - ov)..(bx + b + ov) { + if x < 0 || y < 0 || x >= w as i32 || y >= h as i32 || !filled[y as usize * w + x as usize] { + continue; + } + let (a, s) = ((y as usize * w + x as usize) * ch, ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch); + for k in 0..ch { + let d = out[a + k] - img[s + k]; + e += d * d; + } + n += 1; + } + } + let e = if n == 0 { 0.0 } else { e / n as f32 }; + if e < best.1 { + best = (c, e); + } + } + let c = best.0; + let (x0, y0) = ((bx - ov).max(0), (by - ov).max(0)); + let (x1, y1) = ((bx + b + ov).min(w as i32), (by + b + ov).min(h as i32)); + let src = |x: i32, y: i32| ((y - by + c.1) as usize * w + (x - bx + c.0) as usize) * ch; + // Squared difference between what is there and the block, where both exist. + let err = |x: i32, y: i32| { + let i = y as usize * w + x as usize; + if !(hole[i] && filled[i]) { + return 0.0; + } + let (a, s) = (i * ch, src(x, y)); + (0..ch).map(|q| (out[a + q] - img[s + q]).powi(2)).sum::() + }; + // Minimum-error boundary cuts through the overlaps with the blocks to the left and + // above: the block is copied right of and below them, untouched pixels are kept + // on the other side. Copying (rather than averaging) keeps the texture's grain. + let left = min_cut((x0..(bx + ov).min(x1)).len(), (y0..y1).len(), |u, v| err(x0 + u as i32, y0 + v as i32)); + let top = min_cut((y0..(by + ov).min(y1)).len(), (x0..x1).len(), |u, v| err(x0 + v as i32, y0 + u as i32)); + for y in y0..y1 { + for x in x0..x1 { + let i = y as usize * w + x as usize; + if !hole[i] { + continue; + } + let (u, v) = ((x - x0) as usize, (y - y0) as usize); + let keep = filled[i] && (left.get(v).is_some_and(|s| u < *s) || top.get(u).is_some_and(|s| v < *s)); + if !keep { + let (a, s) = (i * ch, src(x, y)); + out[a..a + ch].copy_from_slice(&img[s..s + ch]); + } + filled[i] = true; + } + } + } + bx += b; + } + by += b; + } + out +} + +/// The minimum-error cut through a band `across` wide and `along` long (Efros & Freeman's +/// boundary cut, by dynamic programming): for each step along the band, the offset across it +/// where the cut runs (`err(across, along)` is the cost of a pixel). Pixels before the cut keep +/// what is there. Empty when there is no cost anywhere (nothing to cut through). +fn min_cut(across: usize, along: usize, err: impl Fn(usize, usize) -> f32) -> Vec { + if across == 0 || along == 0 { + return Vec::new(); + } + // Cumulative cost, step by step along the band; the cut may move by one pixel per step. + let mut acc: Vec = (0..along).flat_map(|v| (0..across).map(move |u| (u, v))).map(|(u, v)| err(u, v)).collect(); + if acc.iter().all(|x| *x == 0.0) { + return Vec::new(); + } + for v in 1..along { + for u in 0..across { + let lo = u.saturating_sub(1); + let hi = (u + 1).min(across - 1); + let m = (lo..=hi).map(|k| acc[(v - 1) * across + k]).fold(f32::INFINITY, f32::min); + acc[v * across + u] += m; + } + } + let argmin = |v: usize, lo: usize, hi: usize| (lo..=hi).min_by(|a, b| acc[v * across + a].total_cmp(&acc[v * across + b])).unwrap_or(lo); + let mut cut = vec![0; along]; + let mut u = argmin(along - 1, 0, across - 1); + for v in (0..along).rev() { + if v + 1 < along { + u = argmin(v, u.saturating_sub(1), (u + 1).min(across - 1)); + } + cut[v] = u; + } + cut +} diff --git a/crates/algo/src/inpaint/tests.rs b/crates/algo/src/inpaint/tests.rs new file mode 100644 index 0000000000..45690fac01 --- /dev/null +++ b/crates/algo/src/inpaint/tests.rs @@ -0,0 +1,466 @@ +use super::*; + +/// The mean is matched first: on a nearly flat bright patch with a noisy deviation ratio the +/// gain stays where the bias can still reach the target's mean. +#[test] +fn fit_matches_the_mean_before_the_contrast() { + let a = Adapt { gain: (0.9, 1.1), bias: 0.04 }; + // Target mean 0.70, source mean 0.70; deviations 0.01 vs 0.004 (ratio 2.5). + let n = 25.0; + let (mt, ms, sdt, sds) = (0.70f32, 0.70f32, 0.01f32, 0.004f32); + let (g, b) = fit(a, n, mt * n, (sdt * sdt + mt * mt) * n, ms * n, (sds * sds + ms * ms) * n); + assert!((g * ms + b - mt).abs() < 1e-5, "mean {} vs {mt} (g {g}, b {b})", g * ms + b); + // Where the mean can't be reached anyway, the limits hold as before. + let (g, b) = fit(a, n, 0.2 * n, 0.04 * n, 0.9 * n, 0.81 * n); + assert!((a.gain.0..=a.gain.1).contains(&g) && b.abs() <= a.bias + 1e-6); +} + +/// A zero-width image has no pixels however tall it is claimed to be: no per-row table. +#[test] +fn zero_width_images_complete_without_sizing_by_height() { + let p = CompleteParams::default(); + assert_eq!(complete(0, 1 << 40, 3, &[], &[], &p), Some(Vec::new())); + assert_eq!(complete(1 << 40, 0, 3, &[], &[], &p), Some(Vec::new())); +} + +/// Smooth gradient plus deterministic fine noise. +fn texture(w: usize, h: usize) -> Vec { + let mut rng = Rng::new(7); + (0..w * h) + .flat_map(|i| { + let (x, y) = ((i % w) as f32, (i / w) as f32); + let n = (rng.next() % 1000) as f32 / 1000.0 * 0.04 - 0.02; + [0.3 + 0.004 * x + n, 0.5 + 0.002 * y + n, 0.4 + n] + }) + .collect() +} + +fn disc(w: usize, h: usize, cx: f32, cy: f32, r: f32) -> Vec { + (0..w * h).map(|i| ((i % w) as f32 - cx).hypot((i / w) as f32 - cy) < r).collect() +} + +fn hole_rmse(a: &[f32], b: &[f32], hole: &[bool], ch: usize) -> f32 { + let (mut e, mut n) = (0.0f32, 0); + for (i, hh) in hole.iter().enumerate() { + if *hh { + for c in 0..ch { + e += (a[i * ch + c] - b[i * ch + c]).powi(2); + n += 1; + } + } + } + (e / n as f32).sqrt() +} + +#[test] +fn completion_fills_blemish_from_texture() { + let (w, h, ch) = (96, 96, 3); + let clean = texture(w, h); + let hole = disc(w, h, 48.0, 48.0, 9.0); + let mut dirty = clean.clone(); + for (i, hh) in hole.iter().enumerate() { + if *hh { + dirty[i * ch..i * ch + ch].copy_from_slice(&[0.02, 0.02, 0.02]); + } + } + let out = complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap(); + let before = hole_rmse(&dirty, &clean, &hole, ch); + let after = hole_rmse(&out, &clean, &hole, ch); + assert!(after < 0.06 && after < before * 0.15, "rmse before {before} after {after}"); + // Outside the hole untouched. + assert!(out.iter().zip(&dirty).zip(hole.iter().flat_map(|h| [*h; 3])).all(|((a, b), h)| h || a == b)); + // Deterministic. + assert_eq!(out, complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap()); +} + +#[test] +fn completion_continues_structure() { + // Vertical stripes: the filled hole should keep the stripes (not a blur). + let (w, h, ch) = (80, 80, 1); + let clean: Vec = (0..w * h).map(|i| if (i % w / 4) % 2 == 0 { 0.2 } else { 0.8 }).collect(); + let hole = disc(w, h, 40.0, 40.0, 7.0); + let dirty: Vec = clean.iter().zip(&hole).map(|(c, h)| if *h { 0.5 } else { *c }).collect(); + let out = complete(w, h, ch, &dirty, &hole, &CompleteParams::default()).unwrap(); + let err = hole_rmse(&out, &clean, &hole, ch); + assert!(err < 0.15, "{err}"); +} + +#[test] +fn proximity_finds_matching_offset() { + let (w, h, ch) = (80, 60, 1); + // Periodic pattern with period 10 in x: a match is any multiple-of-10 displacement. + let img: Vec = (0..w * h).map(|i| ((i % w) as f32 * std::f32::consts::TAU / 10.0).sin() * 0.4 + 0.5).collect(); + let hole = disc(w, h, 40.0, 30.0, 5.0); + let (dx, dy) = best_offset(w, h, ch, &img, &hole, 3, 20).unwrap(); + assert_eq!(dx.rem_euclid(10), 0, "dx {dx} dy {dy}"); + assert!((dx, dy) != (0, 0)); +} + +#[test] +fn proximity_search_clamps_hostile_radii_without_overflow_or_hanging() { + let (w, h, ch) = (24, 20, 1); + let img: Vec = (0..w * h).map(|i| (i % w) as f32 / w as f32).collect(); + let hole = disc(w, h, 12.0, 10.0, 2.0); + // User-supplied values used to overflow i32 loops, cast a huge usize + // ring into a negative number, or take billions of search iterations. + let result = best_offset(w, h, ch, &img, &hole, usize::MAX, i32::MAX); + if let Some((dx, dy)) = result { + assert!(dx.abs() <= w as i32 && dy.abs() <= h as i32); + } + assert_eq!(best_offset(w, h, ch, &img, &hole, 2, -1), None); + assert_eq!(best_offset(w, h, ch, &img, &hole, 2, 0), None); +} + +/// Structure, fine noise and flat patches (so that some matches tie exactly), `ch` channels. +fn busy(w: usize, h: usize, ch: usize, seed: u64) -> Vec { + let mut rng = Rng::new(seed); + (0..w * h) + .flat_map(|i| { + let (x, y) = (i % w, i / w); + let flat = (x / 9 + y / 7) % 5 == 0; + let n = (rng.next() % 1000) as f32 / 1000.0; + (0..ch).map(move |c| if flat { 0.25 + 0.1 * c as f32 } else { (0.2 + 0.004 * x as f32 + 0.1 * ((x + 2 * y + c) % 6) as f32 + 0.05 * n).min(1.0) }) + }) + .collect() +} + +fn bits(v: &[f32]) -> Vec { + v.iter().map(|x| x.to_bits()).collect() +} + +/// Roles: a hole, a band that is context only, a corner that is no image. +fn roles(w: usize, h: usize, hole: &[bool]) -> Vec { + (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + if hole[i] { + Px::Hole + } else if x < 4 && y < 6 { + Px::Void + } else if (w * 3 / 4..w * 3 / 4 + 3).contains(&x) { + Px::Known + } else { + Px::Source + } + }) + .collect() +} + +#[test] +fn a_hole_is_as_thick_as_it_is_through_not_along() { + let (w, h) = (240, 120); + let all = Bounds { x0: 0, y0: 0, x1: w, y1: h }; + let thickness = |hole: &[bool]| { + let px: Vec = hole.iter().map(|&o| if o { Px::Hole } else { Px::Source }).collect(); + hole_thickness(w, hole_bounds(w, h, &px).unwrap_or(all), &px) + }; + // A band 200 px long and 9 px thick; a disc of radius 20; the band touching the edge + // counts the outside of the image as not hole. + let band: Vec = (0..w * h).map(|i| (20..220).contains(&(i % w)) && (50..59).contains(&(i / w))).collect(); + assert!((9..=11).contains(&thickness(&band)), "{}", thickness(&band)); + assert!((39..=42).contains(&thickness(&disc(w, h, 120.0, 60.0, 20.0)))); + let edge: Vec = (0..w * h).map(|i| i / w < 9).collect(); + assert!((9..=11).contains(&thickness(&edge)), "{}", thickness(&edge)); +} + +/// A long thin hole goes the thin way (a shallow pyramid that starts from a membrane): it is +/// filled, only it changes, and the result does not depend on the thread count either. +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn thin_holes_are_filled_the_same_on_any_number_of_threads() { + let (w, h) = (300, 200); + let img = busy(w, h, 3, 5); + // A wavy line 6 px thick across the image. + let hole: Vec = (0..w * h).map(|i| (((i % w) as f32 * 0.05).sin() * 30.0 + 100.0 - (i / w) as f32).abs() < 3.0).collect(); + let px = roles(w, h, &hole); + let run = |threads: usize| { + let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); + pool.install(|| complete_masked(w, h, 3, &img, &px, &CompleteParams::default(), &photocraft_raster::Interrupt::NONE)).unwrap().unwrap() + }; + let one = run(1); + assert_eq!(bits(&one), bits(&run(7))); + assert!((0..w * h).all(|i| hole[i] || (0..3).all(|c| one[i * 3 + c].to_bits() == img[i * 3 + c].to_bits()))); + assert!((0..w * h).filter(|&i| hole[i]).all(|i| (0..3).all(|c| one[i * 3 + c].is_finite()))); +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn results_do_not_depend_on_the_thread_count() { + // Big enough for the parallel passes, and for PatchMatch to sweep in wavefronts where + // there are fewer bands than threads (never with one thread). + let (w, h) = (300, 260); + for (ch, adapt) in [(3, Adapt::NONE), (4, Adapt { gain: (0.9, 1.1), bias: 0.04 })] { + let img = busy(w, h, ch, 3); + let hole = disc(w, h, 140.0, 130.0, 40.0); + let px = roles(w, h, &hole); + let p = CompleteParams { adapt, ..Default::default() }; + let run = |threads: usize| { + let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); + bits(&pool.install(|| complete_masked(w, h, ch, &img, &px, &p, &photocraft_raster::Interrupt::NONE)).unwrap().unwrap()) + }; + let one = run(1); + assert_eq!(one, run(3), "{ch} channels, 3 threads"); + assert_eq!(one, run(16), "{ch} channels, 16 threads"); + } +} + +/// The onion peel as first written: every source in order, every known pixel summed. +fn onion_peel_plain(w: usize, h: usize, ch: usize, r: i32, img: &mut [f32], px: &[Px], sources: &[(i32, i32)]) { + let mut known: Vec = px.iter().map(|k| matches!(k, Px::Known | Px::Source)).collect(); + loop { + let front: Vec = (0..w * h) + .filter(|&i| { + let (x, y) = ((i % w) as i32, (i / w) as i32); + !known[i] + && px[i] == Px::Hole + && (-1..=1).any(|dy: i32| { + (-1..=1).any(|dx: i32| { + let (nx, ny) = (x + dx, y + dy); + nx >= 0 && ny >= 0 && (nx as usize) < w && (ny as usize) < h && known[ny as usize * w + nx as usize] + }) + }) + }) + .collect(); + if front.is_empty() { + break; + } + let vals: Vec> = front + .iter() + .map(|&i| { + let (x, y) = ((i % w) as i32, (i / w) as i32); + let mut best = (f32::INFINITY, (0i32, 0i32)); + for &(sx, sy) in sources { + let (mut e, mut n) = (0.0f32, 0usize); + for dy in -r..=r { + for dx in -r..=r { + let (tx, ty) = (x + dx, y + dy); + if tx < 0 || ty < 0 || tx as usize >= w || ty as usize >= h || !known[ty as usize * w + tx as usize] { + continue; + } + let (ti, si) = ((ty as usize * w + tx as usize) * ch, ((sy + dy) as usize * w + (sx + dx) as usize) * ch); + for c in 0..ch { + let d = img[ti + c] - img[si + c]; + e += d * d; + } + n += 1; + } + } + if n > 0 && e / (n as f32) < best.0 { + best = (e / n as f32, (sx, sy)); + } + } + let si = (best.1.1 as usize * w + best.1.0 as usize) * ch; + img[si..si + ch].to_vec() + }) + .collect(); + for (&i, v) in front.iter().zip(vals) { + img[i * ch..(i + 1) * ch].copy_from_slice(&v); + known[i] = true; + } + } +} + +/// #2805's case (upstream's own speed-up of the onion peel, superseded here): repeating +/// texture with many exact ties, two holes; the result must be the plain scan's to the bit. +#[test] +fn onion_peel_picks_exactly_what_the_plain_scan_picks() { + let (w, h, ch, r) = (48, 40, 3, 3); + let mut img = texture(w, h); + for y in 0..h { + for x in 6..w / 2 { + for c in 0..ch { + img[(y * w + x) * ch + c] = img[(y * w + x % 6) * ch + c]; + } + } + } + let none = photocraft_raster::Interrupt::NONE; + for hole in [disc(w, h, 20.0, 18.0, 7.5), disc(w, h, 34.0, 22.0, 5.0)] { + let px: Vec = hole.iter().map(|h| if *h { Px::Hole } else { Px::Source }).collect(); + let near = integral(w, h, &hole); + let sources: Vec<(i32, i32)> = (r..h as i32 - r) + .flat_map(|y| (r..w as i32 - r).map(move |x| (x, y))) + .filter(|&(x, y)| window_count(&near, w, h, x - r, y - r, x + r + 1, y + r + 1) == 0) + .collect(); + let (mut fast, mut plain) = (img.clone(), img.clone()); + onion_peel(w, h, ch, r, &mut fast, &px, hole_bounds(w, h, &px).unwrap(), &sources, &none).unwrap(); + onion_peel_plain(w, h, ch, r, &mut plain, &px, &sources); + assert!(fast.iter().zip(&plain).all(|(a, b)| a.to_bits() == b.to_bits())); + assert_ne!(fast, img); + } +} + +#[test] +fn onion_peel_takes_the_plain_scans_sources() { + let none = photocraft_raster::Interrupt::NONE; + for (w, h, ch, seed) in [(44, 36, 1, 1), (50, 40, 3, 2), (40, 40, 4, 3)] { + let mut img = busy(w, h, ch, seed); + // Also an image of a few exact values, where many sources tie. + if seed == 3 { + img.iter_mut().for_each(|v| *v = (*v * 3.0).round() / 3.0); + } + let hole = disc(w, h, w as f32 * 0.45, h as f32 * 0.5, 7.5); + let px = roles(w, h, &hole); + let valid = valid_sources(w, h, 3, &px, &none).unwrap(); + let sources = Sources::new(&valid, w).all(&valid, w); + let (mut a, mut b) = (img.clone(), img.clone()); + onion_peel(w, h, ch, 3, &mut a, &px, hole_bounds(w, h, &px).unwrap(), &sources, &none).unwrap(); + onion_peel_plain(w, h, ch, 3, &mut b, &px, &sources); + assert_eq!(bits(&a), bits(&b), "{w}×{h}×{ch}"); + } +} + +#[test] +fn least_sum_is_where_the_quotient_crosses() { + let mut rng = Rng::new(5); + for i in 0..4000 { + let n = (1 + rng.next() % 49) as f32; + let v = match i % 4 { + 0 => 0.0, + 1 => (rng.next() % 1000) as f32 * 1e-9, + _ => (rng.next() % 1_000_000) as f32 / 1_000_000.0, + }; + for strict in [true, false] { + let holds = |e: f32| if strict { e / n > v } else { e / n >= v }; + let e = least_sum(v, n, strict); + assert!(holds(e), "{v} {n} {strict}: {e}"); + assert!(e == 0.0 || !holds(e.next_down()), "{v} {n} {strict}: {e} is not the least"); + } + } + assert_eq!(least_sum(f32::INFINITY, 4.0, true), f32::INFINITY); +} + +#[test] +fn valid_sources_are_the_wholly_copyable_patches() { + let (w, h, r) = (37, 29, 2); + let mut rng = Rng::new(9); + let px: Vec = (0..w * h) + .map(|_| match rng.next() % 23 { + 0 => Px::Hole, + 1 => Px::Void, + 2 => Px::Known, + _ => Px::Source, + }) + .collect(); + let valid = valid_sources(w, h, r, &px, &photocraft_raster::Interrupt::NONE).unwrap(); + let source: Vec = px.iter().map(|k| *k == Px::Source).collect(); + let ss = integral(w, h, &source); + let (ri, full) = (r as i32, ((2 * r + 1) * (2 * r + 1)) as u32); + for y in 0..h as i32 { + for x in 0..w as i32 { + let inside = x >= ri && y >= ri && x + ri < w as i32 && y + ri < h as i32; + let want = inside && window_count(&ss, w, h, x - ri, y - ri, x + ri + 1, y + ri + 1) == full; + assert_eq!(valid[y as usize * w + x as usize], want, "({x}, {y})"); + } + } + // Counting per row and block finds the centres in order. + let sources = Sources::new(&valid, w); + let list = sources.all(&valid, w); + assert!(list.len() > 10 && list.len() == sources.len()); + for (k, p) in list.iter().enumerate() { + assert_eq!(sources.nth(k, &valid, w), *p); + } + // And on a grid wider than a block, with empty blocks and rows. + let (w, h) = (300, 9); + let wide: Vec = (0..w * h).map(|i| (i % w) % 7 == 3 && (i % w) / 64 != 2 && i / w != 4).collect(); + let sources = Sources::new(&wide, w); + let list = sources.all(&wide, w); + for (k, p) in list.iter().enumerate() { + assert_eq!(sources.nth(k, &wide, w), *p); + } +} + +#[test] +fn full_patches_measure_as_clipped_ones() { + let (w, h, ch, r) = (46, 38, 4, 3); + let mut img = busy(w, h, ch, 4); + let hole = disc(w, h, 20.0, 18.0, 6.0); + let px = roles(w, h, &hole); + let none = photocraft_raster::Interrupt::NONE; + let hb = hole_bounds(w, h, &px).unwrap(); + let tb = hb.grow(r, w, h); + let valid = valid_sources(w, h, r, &px, &none).unwrap(); + let picks: Vec<(i32, i32)> = Sources::new(&valid, w).all(&valid, w).iter().step_by(37).copied().collect(); + assert!(picks.len() > 5); + for adapt in [Adapt::NONE, Adapt { gain: (0.8, 1.25), bias: 0.08 }] { + for keep in [false, true] { + let targets = targets_around(w, r, &px, hb, tb); + let mut s = Solver { + w, + h, + ch, + r: r as i32, + img: &mut img, + px: &px, + valid: valid.clone(), + targets, + hole_box: hb, + tbox: tb, + full: Vec::new(), + tmom: Vec::new(), + smom: Vec::new(), + adapt, + ctl: none, + }; + s.prepare(keep); + let nnf = vec![picks[0]; tb.area()]; + let mut cost = vec![f32::INFINITY; tb.area()]; + s.costs(&nnf, &mut cost).unwrap(); + let mut checked = 0; + for k in 0..tb.area() { + if !s.targets[k] || !s.full[k] { + continue; + } + let t = ((tb.x0 + k % tb.width()) as i32, (tb.y0 + k / tb.width()) as i32); + for &src in &picks { + if adapt.is_none() { + let exact = s.dist_clipped(t, src, f32::INFINITY); + assert_eq!(s.dist_full(t, src, f32::INFINITY).to_bits(), exact.to_bits()); + for cutoff in [exact * 0.5, exact, exact * 2.0] { + let (a, b) = (s.dist_full(t, src, cutoff), s.dist_clipped(t, src, cutoff)); + assert_eq!(a < cutoff, b < cutoff); + } + } else { + let (mut m1, mut m2) = ([[0.0f32; 5]; 4], [[0.0f32; 5]; 4]); + s.moments_full(k, t, src, &mut m1); + assert_eq!(s.moments(t, src, &mut m2), (2 * r + 1) * (2 * r + 1)); + assert_eq!(m1.map(|c| c.map(f32::to_bits)), m2.map(|c| c.map(f32::to_bits))); + assert_eq!(s.dist_adapted_full(k, t, src).to_bits(), s.dist_adapted(t, src).to_bits()); + } + checked += 1; + } + } + assert!(checked > 100, "{checked}"); + } + } +} + +#[test] +fn owned_completion_fills_in_place_or_gives_the_pixels_back() { + let (w, h, ch) = (64, 52, 3); + let img = busy(w, h, ch, 6); + let hole = disc(w, h, 30.0, 26.0, 8.0); + let px = roles(w, h, &hole); + let (p, none) = (CompleteParams::default(), photocraft_raster::Interrupt::NONE); + let borrowed = complete_masked(w, h, ch, &img, &px, &p, &none).unwrap().unwrap(); + let owned = complete_masked_owned(w, h, ch, img.clone(), &px, &p, &none).unwrap().unwrap(); + assert_eq!(bits(&owned), bits(&borrowed)); + // Nothing may be copied: no completion, and the pixels come back as they were. + let known: Vec = px.iter().map(|k| if *k == Px::Source { Px::Known } else { *k }).collect(); + assert!(complete_masked(w, h, ch, &img, &known, &p, &none).unwrap().is_none()); + let back = complete_masked_owned(w, h, ch, img.clone(), &known, &p, &none).unwrap().unwrap_err(); + assert_eq!(bits(&back), bits(&img)); + // Cancelled, it stops. + let yes = || true; + assert!(complete_masked_owned(w, h, ch, img, &px, &p, &photocraft_raster::Interrupt::cancel_only(&yes)).is_err()); +} + +#[test] +fn synthesis_fills_hole_with_plausible_values() { + let (w, h, ch) = (64, 64, 3); + let clean = texture(w, h); + let hole = disc(w, h, 32.0, 32.0, 6.0); + let out = synthesize(w, h, ch, &clean, &hole, 8, 3); + let err = hole_rmse(&out, &clean, &hole, ch); + assert!(err < 0.15, "{err}"); + assert!(out.iter().all(|v| v.is_finite())); +} diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs index 49ab15871c..03ffe38144 100644 --- a/crates/algo/src/lib.rs +++ b/crates/algo/src/lib.rs @@ -18,6 +18,7 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod art_history; mod artistic; mod artistic_fx; mod blur; @@ -25,6 +26,7 @@ mod blur2; pub mod camera_raw; pub mod color_to_alpha; pub mod content_aware; +mod cutout; mod denoise; pub mod dff; pub mod direct; @@ -32,6 +34,7 @@ mod distort; mod distort2; pub mod erase; pub mod exif; +pub mod faces; pub mod features; mod fxutil; mod gallery; @@ -70,6 +73,7 @@ pub mod seam; pub mod segment; pub mod selection; mod selection_blur; +pub mod separable; mod shape_blur; mod sharpen; pub mod stack; @@ -85,6 +89,7 @@ pub mod warp; pub mod wideangle; pub use artistic::{GALLERY_CATEGORIES, GalleryEffect, GalleryFilter, GalleryParam, GalleryParamKind}; +pub use distort::MAX_WAVE_GENERATORS; pub use image::{Edge, Image}; pub use params_ext::*; @@ -410,6 +415,14 @@ pub enum FilterParams { }, // ---- Render ---- + /// Clouds (or Difference Clouds, which subtracts them from the pixels) between two straight + /// sRGB RGBA colours; the cloud size follows the bounds. + Clouds { + seed: u32, + difference: bool, + foreground: [f32; 4], + background: [f32; 4], + }, /// Variance 1–64, strength 1–64; colours straight sRGB RGBA. Fibers { variance: f32, @@ -481,7 +494,8 @@ pub enum FilterParams { distribution: Distribution, monochromatic: bool, seed: u32, - /// Depth for [`DepthSource::LayerMask`] (channel 0, document coordinates), supplied by the caller. + /// Depth for [`DepthSource::LayerMask`] and [`DepthSource::Channel`] (channel 0, document + /// coordinates), supplied by the caller. #[serde(skip)] depth_map: Option>, }, @@ -550,8 +564,11 @@ pub enum FilterParams { impl FilterParams { /// Pixels read outside an output tile. pub fn halo(&self) -> Halo { - let g = |sigma: f32| Halo::Radius((sigma.max(0.0) * 3.0).ceil() as i32 + 1); + let g = |sigma: f32| Halo::Radius(((sigma.max(0.0) * 3.0).ceil() as i32).saturating_add(1)); match self { + // Small Gaussian radii use the calibrated seven-tap kernel as well: its reach is + // three pixels even when ceil(3 * radius) + 1 would be smaller. + FilterParams::GaussianBlur { radius } | FilterParams::HighPass { radius } if (0.05..1.0).contains(radius) => Halo::Radius(3), FilterParams::GaussianBlur { radius } | FilterParams::HighPass { radius } => g(*radius), FilterParams::UnsharpMask { radius, .. } => Halo::Radius(sharpen::exact::halo(*radius)), FilterParams::SmartSharpen { radius, .. } => g(*radius), @@ -560,9 +577,9 @@ impl FilterParams { | FilterParams::Median { radius } | FilterParams::DustAndScratches { radius, .. } | FilterParams::Minimum { radius, .. } - | FilterParams::Maximum { radius, .. } => Halo::Radius(radius.max(0.0).ceil() as i32 + 1), + | FilterParams::Maximum { radius, .. } => Halo::Radius((radius.max(0.0).ceil() as i32).saturating_add(1)), FilterParams::MotionBlur { distance, .. } => Halo::Radius((distance.abs() / 2.0).ceil() as i32 + 2), - FilterParams::Mosaic { cell_size } => Halo::Radius(cell_size.max(1.0).ceil() as i32 + 1), + FilterParams::Mosaic { cell_size } => Halo::Radius((cell_size.max(1.0).ceil() as i32).saturating_add(1)), FilterParams::Emboss { height, .. } => Halo::Radius(height.abs().ceil() as i32 + 2), FilterParams::FindEdges => Halo::Radius(1), FilterParams::AddNoise { .. } | FilterParams::Solarize | FilterParams::Invert | FilterParams::Desaturate => Halo::Radius(0), @@ -604,7 +621,13 @@ impl FilterParams { /// the reference bounds even where the layer is transparent, as Photoshop's does on an /// empty layer, while still reading no neighbourhood. pub fn renders_from_nothing(&self) -> bool { - matches!(self, FilterParams::Fibers { .. }) + matches!(self, FilterParams::Fibers { .. } | FilterParams::Clouds { difference: false, .. }) + } + + /// Whether the filter places and sizes its pattern by the canvas even with a selection + /// (Clouds, Fibers): its bounds are then the canvas, and the selection only masks the result. + pub fn sizes_by_canvas(&self) -> bool { + matches!(self, FilterParams::Clouds { .. } | FilterParams::Fibers { .. }) } /// Human-readable name. @@ -651,6 +674,8 @@ impl FilterParams { FilterParams::Displace { .. } => "Displace", FilterParams::Shear { .. } => "Shear", FilterParams::ZigZag { .. } => "ZigZag", + FilterParams::Clouds { difference: false, .. } => "Clouds", + FilterParams::Clouds { difference: true, .. } => "Difference Clouds", FilterParams::Fibers { .. } => "Fibers", FilterParams::LensFlare { .. } => "Lens Flare", FilterParams::LightingEffects { .. } => "Lighting Effects", @@ -679,7 +704,7 @@ impl FilterParams { /// Halos of the second filter batch. fn halo_ext(p: &FilterParams) -> Halo { - let r = |v: f32| Halo::Radius(v.max(0.0).ceil() as i32 + 1); + let r = |v: f32| Halo::Radius((v.max(0.0).ceil() as i32).saturating_add(1)); match p { FilterParams::ColorHalftone { max_radius, .. } => r(max_radius * 3.0), FilterParams::Crystallize { cell_size, .. } => r(cell_size.max(1.0) * 3.0), @@ -695,13 +720,14 @@ fn halo_ext(p: &FilterParams) -> Halo { FilterParams::Wind { method, .. } => r(stylize2::wind_reach(*method)), FilterParams::Displace { .. } | FilterParams::Shear { .. } | FilterParams::ZigZag { .. } | FilterParams::SpinBlur { .. } => Halo::Bounds, // Generators and per-pixel renders only need the bounds geometry (always in the context). - FilterParams::Fibers { .. } | FilterParams::LensFlare { .. } => Halo::Radius(0), + FilterParams::Fibers { .. } | FilterParams::LensFlare { .. } | FilterParams::Clouds { .. } => Halo::Radius(0), FilterParams::LightingEffects { .. } => r(1.0), FilterParams::Relight { intensity, .. } if *intensity == 0.0 => Halo::Radius(0), FilterParams::Relight { .. } => Halo::Radius(relight::halo_radius_max()), FilterParams::ReduceNoise { .. } => r(denoise::reach()), - FilterParams::SmartBlur { radius, .. } => r(*radius), - FilterParams::LensBlur { radius, .. } => r(*radius), + // The kernels clamp the radius to 100 px: size the source window by what they use. + FilterParams::SmartBlur { radius, .. } => r(radius.min(100.0)), + FilterParams::LensBlur { radius, .. } => r(radius.min(100.0)), FilterParams::ShapeBlur { radius, .. } => r(shape_blur::radius(*radius)), FilterParams::TiltShift { blur, .. } => r(gallery::reach(*blur)), FilterParams::IrisBlur { pins } => r(gallery::reach(pins.iter().map(|p| p.blur).fold(0.0, f32::max))), @@ -709,6 +735,7 @@ fn halo_ext(p: &FilterParams) -> Halo { FilterParams::PathBlur { paths } => r(paths.iter().map(|p| p.speed.abs()).fold(0.0, f32::max) / 2.0 + 1.0), FilterParams::Custom { .. } => r(2.0), FilterParams::DeInterlace { .. } => r(1.0), + FilterParams::FilterGallery { effects } if artistic_fx::is_global(effects) => Halo::Bounds, FilterParams::FilterGallery { effects } => Halo::Radius(artistic_fx::reach(effects)), _ => Halo::Radius(0), } @@ -743,8 +770,8 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec noise::add(src, out, ctx, *amount, *distribution, *monochromatic, *seed), FilterParams::Median { radius } => noise::median(src, out, *radius, None), FilterParams::DustAndScratches { radius, threshold } => noise::median(src, out, *radius, Some(*threshold)), - FilterParams::Minimum { radius, preserve } => other::min_max_preserve(src, out, *radius, false, *preserve), - FilterParams::Maximum { radius, preserve } => other::min_max_preserve(src, out, *radius, true, *preserve), + FilterParams::Minimum { radius, preserve } => min_max_ink(src, out, ctx, *radius, false, *preserve), + FilterParams::Maximum { radius, preserve } => min_max_ink(src, out, ctx, *radius, true, *preserve), FilterParams::Offset { horizontal, vertical, undefined } => other::offset(src, out, ctx, *horizontal, *vertical, *undefined), FilterParams::Mosaic { cell_size } => stylize::mosaic(src, out, ctx, *cell_size), FilterParams::Emboss { angle, height, amount } => stylize::emboss(src, out, ctx, *angle, *height, *amount), @@ -815,6 +842,7 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec distort2::shear(src, out, ctx, points, *undefined), FilterParams::ZigZag { amount, ridges, style } => distort2::zigzag(src, out, ctx, *amount, *ridges, *style), + FilterParams::Clouds { seed, difference, foreground, background } => render::clouds(src, out, ctx, *seed, *difference, (*foreground, *background)), FilterParams::Fibers { variance, strength, seed, foreground, background } => { render::fibers(src, out, ctx, *variance, *strength, *seed, (*foreground, *background)) } @@ -908,6 +936,23 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec Vec { + if !fxutil::subtractive(ctx) { + return other::min_max_preserve(src, out, radius, max, preserve); + } + let n = src.ch.max(1); + let cc = if ctx.alpha { n - 1 } else { n }; + let flip = |data: &mut [f32]| data.chunks_exact_mut(n).for_each(|px| px.iter_mut().take(cc).for_each(|v| *v = 1.0 - *v)); + let mut inv = src.clone(); + flip(&mut inv.data); + let mut res = other::min_max_preserve(&inv, out, radius, max, preserve); + flip(&mut res); + res +} + /// Output tile size used by [`apply`]. pub const TILE: i32 = 256; @@ -962,15 +1007,24 @@ pub fn apply_in_with( } /// Results do not depend on the tiling, so wide-halo filters use bigger tiles to keep the -/// re-read margin (and its cost) below ~2× the tile area. +/// re-read margin (and its cost) below ~2× the tile area. The radius is input-derived, so the +/// halo arithmetic is done in `i64` and the result capped: an `i32::MAX` radius must not +/// overflow in debug builds, and must not produce a tile bigger than the hard cap. fn auto_tile(params: &FilterParams, bounds: Rect) -> i32 { match params.halo_for(bounds) { - Halo::Radius(r) => TILE.max((2 * r + 63) / 64 * 64).min(2048), + // The radius is input-derived: widen to i64 before the arithmetic and cap the + // conversion, so an i32::MAX radius cannot overflow (debug panic) and cannot make a + // tile bigger than the hard cap below (131_072 = 2048 * 64, one tile row). + Halo::Radius(r) => { + let halo = ((2 * i64::from(r) + 63) / 64 * 64).min(131_072); + TILE.max(halo as i32).min(2048) + } Halo::Bounds => TILE, } } /// [`apply`] / [`apply_in`] with an explicit tile size (tests use it to check tile independence). +/// Nonpositive tile sizes are treated as one pixel. pub fn apply_tiled( surface: &Surface, params: &FilterParams, @@ -1013,6 +1067,8 @@ fn apply_tiled_with_impl( ctl: &photocraft_raster::Interrupt, prepare_fast_paths: bool, ) -> Option { + // Every dispatch path needs a positive step, including Motion Blur's fallback. + let tile = tile.max(1); if ctl.cancelled() { return None; } @@ -1056,18 +1112,28 @@ fn apply_tiled_with_impl( return None; } let halo = params.halo_for(bounds); - if matches!(params, FilterParams::UnsharpMask { .. }) { - let Halo::Radius(reach) = halo else { return None }; + // Every windowed filter: a Filter Gallery stack sums its effects' reach, and a radius is + // caller-supplied, so a short parameter set can ask for a huge source window per tile. + if let Halo::Radius(reach) = halo { let (w, h) = (area.width().min(tile as u32) as usize, area.height().min(tile as u32) as usize); let read_samples = w.checked_add(2 * reach as usize)?.checked_mul(h.checked_add(2 * reach as usize)?)?.checked_mul(fmt.channels())?; let tile_count = u64::from(area.width()).div_ceil(tile as u64).checked_mul(u64::from(area.height()).div_ceil(tile as u64))?; // Reject hostile source-window sizes before Image::read allocates them. // Ordinary filter parameters and automatic tiles fit these bounds. - if read_samples > 512 * 1024 * 1024 || read_samples.checked_mul(std::mem::size_of::())? > isize::MAX as usize || tile_count > 1_048_576 { + let too_many_tiles = matches!(params, FilterParams::UnsharpMask { .. }) && tile_count > 1_048_576; + if read_samples > 512 * 1024 * 1024 || read_samples.checked_mul(std::mem::size_of::())? > isize::MAX as usize || too_many_tiles { return None; } } let shared = (halo == Halo::Bounds).then(|| Image::read(surface, bounds.union(&area))); + // A Filter Gallery stack with a Cutout traces the whole image once; tiles only fill it in. + let gallery = match (params, &shared) { + (FilterParams::FilterGallery { effects }, Some(src)) => artistic_fx::Prepared::new(effects, src, &ctx), + _ => None, + }; + if ctl.cancelled() { + return None; + } // Shape rasterization depends only on params, not on a tile's pixels. let shape = match params { FilterParams::ShapeBlur { radius, shape } => { @@ -1091,16 +1157,43 @@ fn apply_tiled_with_impl( // Tiles finished so far, for progress reported per tile (from any worker thread). let finished = std::sync::atomic::AtomicUsize::new(0); let total = tiles.len().max(1); - let run = |t: &Rect| -> (Rect, Vec) { + enum TileData { + Samples(Vec), + Encoded(Vec), + } + // Cell filters become writeback-bound once their geometry is reused. Encode + // on the tile workers; committing the group then only copies native bytes. + let finish = |data: Vec| { + if matches!(params, FilterParams::Crystallize { .. } | FilterParams::Pointillize { .. }) + && let Some(bytes) = pixelate::cells::encode(fmt, &data) + { + TileData::Encoded(bytes) + } else { + TileData::Samples(data) + } + }; + let run = |t: &Rect| -> (Rect, TileData) { // Cancelled: skip the remaining tiles of the group (the result is discarded). if ctl.cancelled() { - return (*t, Vec::new()); + return (*t, TileData::Samples(Vec::new())); } let tick = || { let n = finished.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; // The last few percent are the writes after each group. ctl.progress(0.98 * n as f32 / total as f32); }; + let sparse = match *params { + FilterParams::Crystallize { cell_size, seed } => pixelate::cells::tile(surface, *t, &ctx, cell_size, seed, None, extent), + FilterParams::Pointillize { cell_size, seed, background } => pixelate::cells::tile(surface, *t, &ctx, cell_size, seed, Some(background), extent), + _ => None, + }; + if let Some((src, mut data)) = sparse { + if let Some(sel) = selection { + mix_selection(&mut data, *t, sel, &src, fmt.alpha); + } + tick(); + return (*t, finish(data)); + } let owned; let src = match (&shared, halo) { (Some(s), _) => s, @@ -1118,6 +1211,7 @@ fn apply_tiled_with_impl( } }; let Some(mut data) = (match (&shape, &radial) { + _ if gallery.is_some() => gallery.as_ref().map(|g| g.run(src, *t, &ctx)), (Some(shape), _) => shape.checked_run(src, *t, &ctx), (None, Some(plan)) => match plan.filter(src, *t, &ctx, ctl) { Some(data) => Some(data), @@ -1129,13 +1223,13 @@ fn apply_tiled_with_impl( _ => Some(kernel(params, src, *t, &ctx)), }, }) else { - return (*t, Vec::new()); + return (*t, TileData::Samples(Vec::new())); }; if let Some(sel) = selection { mix_selection(&mut data, *t, sel, src, fmt.alpha); } tick(); - (*t, data) + (*t, finish(data)) }; // Tiles are filtered in groups of about RESULT_BUDGET bytes (at least one per core) and each // group is written before the next starts, so a huge layer never holds all its results as @@ -1151,24 +1245,33 @@ fn apply_tiled_with_impl( return None; } #[cfg(not(target_arch = "wasm32"))] - let results: Vec<(Rect, Vec)> = { + let results: Vec<(Rect, TileData)> = { use rayon::prelude::*; chunk.par_iter().map(run).collect() }; #[cfg(target_arch = "wasm32")] - let results: Vec<(Rect, Vec)> = chunk.iter().map(run).collect(); + let results: Vec<(Rect, TileData)> = chunk.iter().map(run).collect(); if ctl.cancelled() { return None; } // Checked filters can reject malformed geometry or an allocation failure. // Do not hand an incomplete tile to Surface::write_region. if (shape.is_some() || matches!(params, FilterParams::UnsharpMask { .. })) - && results.iter().any(|(t, data)| data.len() != t.width() as usize * t.height() as usize * fmt.channels()) + && results.iter().any(|(t, data)| { + let pixels = t.width() as usize * t.height() as usize; + match data { + TileData::Samples(v) => v.len() != pixels * fmt.channels(), + TileData::Encoded(v) => v.len() != pixels * fmt.bytes_per_pixel(), + } + }) { return None; } for (t, data) in results { - out.write_region(t, &data); + match data { + TileData::Samples(data) => out.write_region(t, &data), + TileData::Encoded(data) => out.write_interleaved(t, &data), + } } } out.prune(); @@ -1287,15 +1390,6 @@ const RESULT_BUDGET: usize = 256 << 20; #[cfg(test)] mod tests; -#[cfg(test)] -mod tests_ext; - -#[cfg(test)] -mod tests_mosaic; - -#[cfg(test)] -mod tests_selection_mix; - /// The current upstream sampler through the identical source/selection/tile pipeline. #[cfg(test)] #[allow(clippy::too_many_arguments)] diff --git a/crates/algo/src/liquify.rs b/crates/algo/src/liquify.rs index adbca8680f..379500b25c 100644 --- a/crates/algo/src/liquify.rs +++ b/crates/algo/src/liquify.rs @@ -19,6 +19,7 @@ //! copied untouched. [`ProxyImage::render`] is the cheap preview path: bilinear field and //! bilinear source on a downsampled RGBA copy, re-rendered only where a dab changed things. +use crate::par; use photocraft_geom::Rect; use photocraft_raster::Surface; use serde::{Deserialize, Serialize}; @@ -186,6 +187,13 @@ struct StrokeCursor { carry: f64, } +/// Dabs over this many field nodes compute their rows in parallel (an 800 px brush has about +/// 125,000); smaller ones are not worth the hand-off. +const PARALLEL_DAB_NODES: usize = 16_384; + +/// Bound completed output buffers independently of the document's size. +const RENDER_BATCH_TILES: usize = 64; + /// Distance between dabs along a stroke for brush radius `r`: a twentieth of the radius, so a /// drag is many small pushes and its edge stays smooth. fn dab_spacing(r: f64) -> f64 { @@ -204,11 +212,20 @@ pub fn auto_cell(bounds: Rect) -> f64 { if bounds.size().area() <= 4_200_000 { 2.0 } else { 4.0 } } +/// Most nodes of a field's displacement grid (12 bytes each with the freeze mask): 192 MB. +const MAX_FIELD_NODES: usize = 1 << 24; + impl LiquifyField { pub fn new(bounds: Rect, cell: f64) -> Self { - let cell = cell.clamp(1.0, 64.0); - let w = (f64::from(bounds.width()) / cell).ceil() as usize + 2; - let h = (f64::from(bounds.height()) / cell).ceil() as usize + 2; + let mut cell = cell.clamp(1.0, 64.0); + let nodes = |cell: f64| ((f64::from(bounds.width()) / cell).ceil() as usize + 2, (f64::from(bounds.height()) / cell).ceil() as usize + 2); + let (mut w, mut h) = nodes(cell); + // The extent is the document canvas, which a file declares: past the node budget the + // field gets coarser (it is interpolated anyway) instead of asking for gigabytes. + while w.saturating_mul(h) > MAX_FIELD_NODES { + cell *= 2.0; + (w, h) = nodes(cell); + } LiquifyField { bounds, cell, w, h, d: vec![[0.0; 2]; w * h], freeze: vec![0.0; w * h], cursor: None } } @@ -487,9 +504,11 @@ impl LiquifyField { bilinear(&self.d, self.w, self.h, gx, gy) } }; - let mut writes: Vec<(usize, [f32; 2], f32)> = Vec::with_capacity((i1 - i0 + 1) * (j1 - j0 + 1)); + // Every node reads only the snapshot and the field as it was before this dab (the writes + // land after), so rows are independent: a large brush computes them in parallel. let inv_cell = 1.0 / self.cell; - for j in j0..=j1 { + let node_row = |j: usize| -> Vec<(usize, [f32; 2], f32)> { + let mut row = Vec::with_capacity(i1 - i0 + 1); for i in i0..=i1 { let p = self.node_pos(i, j); let (dx, dy) = (p[0] - c[0], p[1] - c[1]); @@ -503,7 +522,7 @@ impl LiquifyField { if s.tool == LiquifyTool::Freeze || s.tool == LiquifyTool::Thaw { let f = self.freeze[idx]; let v = if s.tool == LiquifyTool::Freeze { (f as f64 + k_raw).min(1.0) } else { (f as f64 - k_raw).max(0.0) }; - writes.push((idx, self.d[idx], v as f32)); + row.push((idx, self.d[idx], v as f32)); continue; } let k = k_raw * (1.0 - f64::from(self.freeze[idx])); @@ -553,10 +572,13 @@ impl LiquifyField { [(q[0] + dq[0] - p[0]) as f32, (q[1] + dq[1] - p[1]) as f32] } }; - writes.push((idx, new, self.freeze[idx])); + row.push((idx, new, self.freeze[idx])); } - } - for (idx, d, f) in writes { + row + }; + let rows = j1 - j0 + 1; + let writes = par::map(rows, rows * (i1 - i0 + 1) > PARALLEL_DAB_NODES, |row| node_row(j0 + row)); + for (idx, d, f) in writes.into_iter().flatten() { self.d[idx] = d; self.freeze[idx] = f; } @@ -574,14 +596,11 @@ impl LiquifyField { } /// Catmull-Rom (bicubic) displacement at grid coordinates. - fn sample_cubic(&self, gx: f64, gy: f64, wx: &[f64; 4], wy: &[f64; 4]) -> [f64; 2] { - let (ix, iy) = (gx.floor() as isize, gy.floor() as isize); + fn sample_cubic(&self, xs: &[(usize, f64); 4], ys: &[(usize, f64); 4]) -> [f64; 2] { let mut acc = [0.0f64; 2]; - for (jj, wyj) in wy.iter().enumerate() { - let y = (iy - 1 + jj as isize).clamp(0, self.h as isize - 1) as usize; + for &(y, wyj) in ys { let row = &self.d[y * self.w..(y + 1) * self.w]; - for (ii, wxi) in wx.iter().enumerate() { - let x = (ix - 1 + ii as isize).clamp(0, self.w as isize - 1) as usize; + for &(x, wxi) in xs { let k = wxi * wyj; acc[0] += f64::from(row[x][0]) * k; acc[1] += f64::from(row[x][1]) * k; @@ -641,6 +660,13 @@ fn catmull_rom(t: f64) -> [f64; 4] { [-0.5 * t3 + t2 - 0.5 * t, 1.5 * t3 - 2.5 * t2 + 1.0, -1.5 * t3 + 2.0 * t2 + 0.5 * t, 0.5 * t3 - 0.5 * t2] } +/// A field axis's clamped nodes and weights, reusable across a tile's rows or columns. +fn cubic_taps(g: f64, len: usize) -> [(usize, f64); 4] { + let i = g.floor() as isize; + let weights = catmull_rom(g - g.floor()); + std::array::from_fn(|k| ((i - 1 + k as isize).clamp(0, len as isize - 1) as usize, weights[k])) +} + /// Resamples `src` through the field: inside `field.bounds` each pixel shows the source at /// `p + d(p)` (bicubic field, bicubic premultiplied source, edge pixels repeat past the field /// bounds); everything outside the bounds is kept. Same format as `src`. @@ -653,24 +679,40 @@ pub fn apply_liquify(src: &Surface, field: &LiquifyField) -> Surface { if field.is_identity() || b.is_empty() { return out; } - let maxd = f64::from(field.max_displacement()).ceil() as i32 + 3; + // A 1D Catmull-Rom stencil's absolute weights sum to at most 5/4, so the 2D + // interpolated displacement can reach 25/16 of the largest nodal displacement. + let maxd = ((f64::from(field.max_displacement()) * (25.0 / 16.0)).ceil() as i32).saturating_add(3); + let has_freeze = field.freeze.iter().any(|&f| f > 0.0); let tiles: Vec = b.tiles().map(|t| t.rect().intersect(&b)).filter(|r| !r.is_empty() && field.touches(*r)).collect(); let work = |t: &Rect| -> Option<(Rect, Vec)> { let w = t.width() as usize; let h = t.height() as usize; - // Field per pixel (bicubic), rows share their y weights. + // Field per pixel (bicubic), with axis taps shared across rows and columns. let mut disp = vec![[0.0f64; 2]; w * h]; let (mut fx0, mut fy0, mut fx1, mut fy1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN); let mut moved = false; + let columns: Vec<_> = (0..w) + .map(|x| { + let px = f64::from(t.x0) + x as f64 + 0.5; + let gx = (px - f64::from(b.x0) - 0.5) / field.cell; + (px, cubic_taps(gx, field.w)) + }) + .collect(); for y in 0..h { let py = f64::from(t.y0) + y as f64 + 0.5; let gy = (py - f64::from(b.y0) - 0.5) / field.cell; - let wy = catmull_rom(gy - gy.floor()); - for x in 0..w { - let px = f64::from(t.x0) + x as f64 + 0.5; - let gx = (px - f64::from(b.x0) - 0.5) / field.cell; - let wx = catmull_rom(gx - gx.floor()); - let d = field.sample_cubic(gx, gy, &wx, &wy); + let ys = cubic_taps(gy, field.h); + for (x, (px, xs)) in columns.iter().enumerate() { + let px = *px; + let mut d = field.sample_cubic(xs, &ys); + if has_freeze { + let mask = f64::from(field.freeze_at(px, py)).clamp(0.0, 1.0); + if mask > 0.0 { + let linear = field.sample(px, py); + d[0] = d[0] * (1.0 - mask) + linear[0] * mask; + d[1] = d[1] * (1.0 - mask) + linear[1] * mask; + } + } if d[0] != 0.0 || d[1] != 0.0 { moved = true; } @@ -744,15 +786,10 @@ pub fn apply_liquify(src: &Surface, field: &LiquifyField) -> Surface { } Some((*t, outp)) }; - #[cfg(not(target_arch = "wasm32"))] - let done: Vec<(Rect, Vec)> = { - use rayon::prelude::*; - tiles.par_iter().filter_map(work).collect() - }; - #[cfg(target_arch = "wasm32")] - let done: Vec<(Rect, Vec)> = tiles.iter().filter_map(work).collect(); - for (r, v) in done { - out.write_region(r, &v); + for batch in tiles.chunks(RENDER_BATCH_TILES) { + for (r, v) in par::map(batch.len(), true, |i| batch.get(i).and_then(work)).into_iter().flatten() { + out.write_region(r, &v); + } } if alpha { out.prune(); @@ -920,18 +957,23 @@ impl ProxyImage { return false; } source_scratch.resize(source_len, [0.0; 4]); - src.read_rgba_into(src_rect, source_scratch); - if !src.format().alpha { - for pixel in source_scratch.iter_mut() { - pixel[3] = 1.0; + // Read and premultiply the source in one pass per band of rows, the bands in parallel: at + // an 800 px brush it is about a million pixels (P30). + let opaque = !src.format().alpha; + const BAND_ROWS: usize = 32; + par::chunks_mut(source_scratch, sw * BAND_ROWS, true, |band_index, band| { + let y = src_rect.y0 + (band_index * BAND_ROWS) as i32; + src.read_rgba_into(Rect::new(src_rect.x0, y, src_rect.x1, y + (band.len() / sw) as i32), band); + for pixel in band.iter_mut() { + if opaque { + pixel[3] = 1.0; + } + let alpha = pixel[3]; + pixel[0] *= alpha; + pixel[1] *= alpha; + pixel[2] *= alpha; } - } - for pixel in source_scratch.iter_mut() { - let alpha = pixel[3]; - pixel[0] *= alpha; - pixel[1] *= alpha; - pixel[2] *= alpha; - } + }); let x0 = (region.x0 - bounds.x0) as usize; let x1 = (region.x1 - bounds.x0) as usize; @@ -993,454 +1035,4 @@ impl ProxyImage { } #[cfg(test)] -mod tests { - use super::*; - use photocraft_color::{ColorMode, PixelFormat, SampleType}; - - /// The segment walk before #937: every dab, whether or not it can reach the field. - fn full_walk(f: &mut LiquifyField, s: &LiquifyStroke, a: [f64; 3], b: [f64; 3]) -> Rect { - f.walk_segment(s, a, b, false) - } - - #[test] - fn a_segment_walks_only_the_dabs_that_can_reach_the_field() { - // #937: a segment to a far-off point walked one dab per spacing step along its whole - // length (~3e14 for 1e15 px). Skipping the dabs that can't reach a field node must leave - // every result bit-identical to the full walk. - use LiquifyTool::*; - let bounds = Rect::new(-7, 3, 89, 67); - let segments: [([f64; 3], [f64; 3]); 10] = [ - ([10.0, 20.0, 1.0], [70.0, 50.0, 0.5]), // inside - ([-900.0, 30.0, 1.0], [40.0, 31.0, 1.0]), // in from far left - ([40.0, 30.0, 1.0], [40.0, 4000.0, 0.2]), // out downwards - ([-500.0, -500.0, 1.0], [600.0, 600.0, 1.0]), // diagonal through - ([-300.0, -9.0, 1.0], [300.0, -9.0, 1.0]), // parallel, just above (the brush reaches) - ([-300.0, -400.0, 1.0], [300.0, -400.0, 1.0]), // parallel, far above (reaches nothing) - ([88.5, -50.0, 1.0], [88.5, 120.0, 1.0]), // vertical, along the right edge - ([200.0, 200.0, 1.0], [201.0, 900.0, 1.0]), // entirely outside - ([0.0, 0.0, 1.0], [0.25, 0.1, 1.0]), // shorter than one step - ([60.0, 40.0, 1.0], [-2000.0, 45.0, 1.0]), // continues the stroke before it, out left - ]; - for tool in [ForwardWarp, PushLeft, Reconstruct, Smooth, TwirlCw, TwirlCcw, Pucker, Bloat, Freeze, Thaw] { - for size in [6.0, 40.0] { - for (a, b) in segments { - let s = LiquifyStroke::new(tool, size); - let (mut fast, mut full) = (LiquifyField::new(bounds, 2.0), LiquifyField::new(bounds, 2.0)); - // Some displacement first, so Reconstruct and Smooth have something to undo. - for f in [&mut fast, &mut full] { - f.stroke_segment(&LiquifyStroke::new(ForwardWarp, 30.0), [20.0, 20.0, 1.0], [60.0, 40.0, 1.0]); - } - let dirty = fast.stroke_segment(&s, a, b); - assert_eq!(dirty, full_walk(&mut full, &s, a, b), "{tool:?} {size} {a:?} {b:?}"); - assert!(fast.d == full.d && fast.freeze == full.freeze, "{tool:?} {size} {a:?} {b:?}"); - } - } - } - } - - /// The same path sampled by `parts` pointer events per corner-to-corner leg, unevenly. - fn sampled(path: &[[f64; 3]], parts: usize) -> Vec> { - let mut out = vec![path[0].to_vec()]; - for w in path.windows(2) { - let (a, b) = (w[0], w[1]); - // Uneven steps, as a real pointer gives: t grows quadratically. - for k in 1..=parts { - let t = (k * k) as f64 / (parts * parts) as f64; - out.push((0..3).map(|i| a[i] + (b[i] - a[i]) * t).collect()); - } - } - out - } - - /// #2606: dabs restarted at every pointer event, so a fast mouse (few far-apart events) and a - /// slow one (many close ones) warped the same path differently. Spacing now runs on along the - /// whole stroke: any sampling of a path gives the same field. - #[test] - fn the_result_does_not_depend_on_how_finely_the_path_was_sampled() { - use LiquifyTool::*; - let path = [[20.0, 20.0, 1.0], [62.0, 26.0, 0.6], [70.0, 50.0, 0.9], [28.0, 55.0, 1.0], [30.0, 30.0, 1.0]]; - for tool in [ForwardWarp, PushLeft, TwirlCw, Bloat, Reconstruct, Smooth, Freeze] { - for size in [12.0, 40.0] { - let field = |parts: usize| { - let mut warp = LiquifyStroke::new(ForwardWarp, 30.0); - warp.points = vec![vec![30.0, 30.0], vec![50.0, 40.0]]; - let mut s = LiquifyStroke::new(tool, size); - s.points = sampled(&path, parts); - // Some displacement first, so Reconstruct and Smooth have something to change. - LiquifyField::from_strokes(bounds(), 2.0, &[warp, s]) - }; - let coarse = field(1); - for parts in [3, 17, 60] { - let fine = field(parts); - let dd = coarse.d.iter().zip(&fine.d).map(|(p, q)| (p[0] - q[0]).abs().max((p[1] - q[1]).abs())).fold(0.0f32, f32::max); - let df = coarse.freeze.iter().zip(&fine.freeze).map(|(p, q)| (p - q).abs()).fold(0.0f32, f32::max); - assert!(dd < 1e-3 && df < 1e-4, "{tool:?} {size}px, {parts} events per leg: field differs by {dd} px, mask by {df}"); - } - } - } - } - - /// A long drag whose events land exactly on dab positions, along a diagonal: rounding must not - /// add or drop the dab at a segment's end, or the deep displacement a long Forward Warp builds - /// up differs by a whole dab between a slow and a fast pointer. - #[test] - fn a_long_diagonal_drag_gives_one_field_at_any_event_spacing() { - let field = |step: f64| { - let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); - s.points = (0..=(300.0 / step) as usize).map(|k| vec![40.0 + k as f64 * step * 0.6, 20.0 + k as f64 * step * 0.8]).collect(); - LiquifyField::from_strokes(Rect::new(0, 0, 320, 300), 2.0, &[s]) - }; - let (slow, fast) = (field(2.0), field(60.0)); - let dd = slow.d.iter().zip(&fast.d).map(|(p, q)| (p[0] - q[0]).abs().max((p[1] - q[1]).abs())).fold(0.0f32, f32::max); - assert!(slow.max_displacement() > 50.0, "the drag builds up a deep displacement"); - assert!(dd < 1e-3, "slow and fast pointer fields differ by {dd} px"); - } - - #[test] - fn a_stroke_interrupted_by_a_jump_restarts_its_spacing() { - // A segment that does not start where the last one ended begins a new run of dabs there. - let s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 20.0); - let mut f = LiquifyField::new(bounds(), 2.0); - f.stroke_begin(&s, [10.0, 10.0, 1.0]); - f.stroke_segment(&s, [10.0, 10.0, 1.0], [10.3, 10.0, 1.0]); - let mut g = LiquifyField::new(bounds(), 2.0); - f.stroke_segment(&s, [40.0, 30.0, 1.0], [50.0, 30.0, 1.0]); - g.stroke_begin(&s, [40.0, 30.0, 1.0]); - g.stroke_segment(&s, [40.0, 30.0, 1.0], [50.0, 30.0, 1.0]); - assert_eq!(f, g); - // Hostile points: nothing panics and the next segment starts afresh. - f.stroke_segment(&s, [f64::NAN, 3.0, 1.0], [4.0, f64::INFINITY, 1.0]); - f.stroke_segment(&s, [4.0, 3.0, f64::NAN], [9.0, 3.0, 1.0]); - } - - #[test] - fn a_segment_to_a_far_off_point_returns() { - // The issue's request: about 3.3e14 steps before the fix. Also a segment whose length - // overflows f64. - let mut f = LiquifyField::new(Rect::new(0, 0, 24, 16), 2.0); - let s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0); - let dirty = f.stroke_segment(&s, [0.0, 0.0, 1.0], [1e15, 0.0, 1.0]); - assert!(!dirty.is_empty() && dirty.x1 <= 64, "{dirty:?}"); - f.stroke_segment(&s, [-1e308, 5.0, 1.0], [1e308, 5.0, 1.0]); - f.stroke_segment(&LiquifyStroke::new(LiquifyTool::Bloat, 30.0), [5.0, -1e15, 1.0], [5.0, 1e15, 1.0]); - } - - fn sample(st: SampleType) -> Surface { - let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, st, true)); - for y in 0..64 { - for x in 0..96 { - let v = ((x / 4 + y / 4) % 2) as f32; - s.write_pixel(x, y, &[v, (x as f32) / 96.0, (y as f32) / 64.0, 1.0]); - } - } - s - } - - fn bounds() -> Rect { - Rect::new(0, 0, 96, 64) - } - - fn worst(a: &Surface, b: &Surface, r: Rect) -> f32 { - a.read_region(r).iter().zip(b.read_region(r)).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max) - } - - fn lasso(points: &[[f64; 2]], amount: Option) -> LiquifyStroke { - let mut s = LiquifyStroke::new(LiquifyTool::LassoMask, 1.0); - s.points = points.iter().map(|p| p.to_vec()).collect(); - s.amount = amount; - s - } - - /// Reference even-odd test (ray casting), to check the scanline fill against. - fn inside(p: [f64; 2], poly: &[[f64; 2]]) -> bool { - let mut inside = false; - let mut j = poly.len() - 1; - for (i, a) in poly.iter().enumerate() { - let b = poly[j]; - if (a[1] > p[1]) != (b[1] > p[1]) && p[0] < (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0] { - inside = !inside; - } - j = i; - } - inside - } - - #[test] - fn lasso_freezes_exactly_the_nodes_inside_a_concave_polygon() { - // A concave "C" with a notch, at a non-integer cell size. - let poly = [[10.0, 8.0], [70.0, 8.0], [70.0, 20.0], [30.0, 22.5], [33.0, 40.0], [80.0, 41.0], [60.0, 58.0], [9.0, 55.0]]; - for cell in [1.0, 2.0, 3.5] { - let mut f = LiquifyField::new(bounds(), cell); - let dirty = f.apply_stroke(&lasso(&poly, None)); - assert_eq!(dirty, Rect::new(9, 8, 80, 58)); - for j in 0..f.h { - for i in 0..f.w { - let want = if inside(f.node_pos(i, j), &poly) { 1.0 } else { 0.0 }; - assert_eq!(f.freeze[j * f.w + i], want, "cell {cell}: node ({i}, {j}) at {:?}", f.node_pos(i, j)); - } - } - assert!(f.freeze.contains(&1.0)); - // Strokes replay to the same field (undo rebuilds from the list). - assert_eq!(LiquifyField::from_strokes(bounds(), cell, &[lasso(&poly, None)]), f); - } - } - - #[test] - fn lasso_thaws_with_amount_zero() { - let mut f = LiquifyField::new(bounds(), 2.0); - f.apply_stroke(&LiquifyStroke::new(LiquifyTool::FreezeAll, 1.0)); - assert!(f.freeze.iter().all(|&v| v == 1.0)); - let square = [[20.0, 20.0], [40.0, 20.0], [40.0, 40.0], [20.0, 40.0]]; - f.apply_stroke(&lasso(&square, Some(0.0))); - assert_eq!(f.freeze_at(30.0, 30.0), 0.0, "thawed inside"); - assert_eq!(f.freeze_at(5.0, 5.0), 1.0, "still frozen outside"); - } - - #[test] - fn lasso_ignores_degenerate_and_hostile_polygons() { - let empty = LiquifyField::new(bounds(), 2.0); - let cases: [&[[f64; 2]]; 6] = [ - &[], - &[[10.0, 10.0], [50.0, 50.0]], - &[[f64::NAN, 10.0], [50.0, f64::INFINITY], [20.0, 30.0]], - // Entirely outside the canvas, on every side. - &[[-50.0, -50.0], [-10.0, -50.0], [-10.0, -10.0]], - &[[200.0, 10.0], [300.0, 10.0], [250.0, 50.0]], - &[[10.0, 1e12], [50.0, 1e12], [30.0, 2e12]], - ]; - for poly in cases { - let mut f = empty.clone(); - assert_eq!(f.apply_stroke(&lasso(poly, None)), Rect::EMPTY, "{poly:?}"); - assert_eq!(f, empty, "{poly:?}"); - } - // Huge coordinates around the canvas clamp to it instead of overflowing. - let mut f = empty.clone(); - f.apply_stroke(&lasso(&[[-1e15, -1e15], [1e15, -1e15], [1e15, 1e15], [-1e15, 1e15]], None)); - assert!(f.freeze.iter().all(|&v| v == 1.0)); - // A NaN point among good ones is skipped. - let mut f = empty.clone(); - f.apply_stroke(&lasso(&[[10.0, 10.0], [f64::NAN, 0.0], [50.0, 10.0], [50.0, 50.0], [10.0, 50.0]], None)); - assert_eq!(f.freeze_at(30.0, 30.0), 1.0); - } - - #[test] - fn the_brush_fades_to_nothing_at_its_rim_at_every_density() { - for density in [0.0, 0.25, 0.5, 0.75, 1.0, f64::NAN, -3.0, 7.0] { - assert_eq!(falloff(0.0, density), 1.0, "full strength at the centre ({density})"); - assert_eq!(falloff(1.0, density), 0.0, "nothing at the rim ({density})"); - // Flat at the rim: the last 2 % of the radius moves at most a couple of percent. - assert!(falloff(0.98f64.powi(2), density) < 0.02, "{density}: {}", falloff(0.98f64.powi(2), density)); - let mut prev = 1.0; - for k in 1..=100 { - let v = falloff(k as f64 / 100.0, density); - assert!(v <= prev && v >= 0.0, "{density}: not falling at t² = {k}%"); - prev = v; - } - } - // Density widens the strong core: more strength at every radius, as in Photoshop. - for k in 1..100 { - let t2 = k as f64 / 100.0; - assert!(falloff(t2, 1.0) >= falloff(t2, 0.5) && falloff(t2, 0.5) >= falloff(t2, 0.0), "t² = {t2}"); - } - assert!(falloff(0.5, 1.0) > 0.5 && falloff(0.5, 0.0) < 0.2); - } - - /// #2606: the dab's strength used to stop at 1 - Density at its rim, so a Forward Warp moved - /// everything just inside the brush and nothing just outside, tearing the image along every - /// dab's circle (a hard-edged block at Density 0, half strength at the default 50). - #[test] - fn forward_warp_leaves_the_brush_rim_in_place() { - // A drag of one dab spacing (a twentieth of the radius): one dab, centred where it started. - let (r, drag) = (20.0, 1.0); - for density in [0.0, 50.0, 100.0] { - let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 2.0 * r); - s.density = density; - s.points = vec![vec![48.0, 32.0], vec![48.0 + drag, 32.0]]; - let f = LiquifyField::from_strokes(bounds(), 1.0, &[s]); - let mut rim = 0; - for j in 0..f.h { - for i in 0..f.w { - let p = f.node_pos(i, j); - let t = (p[0] - 48.0).hypot(p[1] - 32.0) / r; - if (0.97..1.0).contains(&t) { - let d = f.d[j * f.w + i]; - assert!(f64::from(d[0].hypot(d[1])) < 0.03 * drag, "density {density}: a node at {t:.3} r moved {d:?}"); - rim += 1; - } - } - } - assert!(rim > 20, "{rim} rim nodes"); - assert!(f.sample(48.0, 32.0)[0] < -0.9 * drag, "density {density}: the centre follows the brush"); - } - } - - #[test] - fn no_strokes_is_identity_at_every_depth() { - for st in [SampleType::U8, SampleType::U16, SampleType::F32] { - let s = sample(st); - let f = LiquifyField::new(bounds(), 2.0); - assert_eq!(apply_liquify(&s, &f), s); - // A field with exactly-zero strokes (zero pressure) is still identity. - let mut st0 = LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0); - st0.pressure = 0.0; - st0.points = vec![vec![40.0, 30.0], vec![60.0, 30.0]]; - let f = LiquifyField::from_strokes(bounds(), 2.0, &[st0]); - assert!(f.is_identity()); - } - } - - #[test] - fn full_resolution_preview_matches_the_unit_scale_proxy_and_partial_updates() { - let canvas = Rect::new(0, 0, 96, 64); - for sample_type in [SampleType::U8, SampleType::U16, SampleType::F32] { - let source = sample(sample_type); - let mut stroke = LiquifyStroke::new(LiquifyTool::ForwardWarp, 32.0); - stroke.points = vec![vec![38.0, 30.0, 1.0], vec![52.0, 34.0, 0.8]]; - let mut field = LiquifyField::from_strokes(canvas, 2.0, &[stroke]); - - let proxy = ProxyImage::new(&source, canvas, 96); - assert_eq!(proxy.scale, 1.0); - let mut want = vec![[0; 4]; proxy.w * proxy.h]; - proxy.render(&field, [0, 0, proxy.w, proxy.h], &mut want); - - let mut got = vec![[0; 4]; proxy.w * proxy.h]; - let mut scratch = Vec::new(); - assert!(ProxyImage::render_full_resolution(&source, &field, canvas, canvas, &mut got, &mut scratch)); - assert_eq!(got, want, "full-resolution render at {sample_type:?}"); - - let mut more = LiquifyStroke::new(LiquifyTool::Bloat, 16.0); - more.points = vec![vec![43.0, 32.0, 1.0]]; - let dirty = field.apply_stroke(&more); - proxy.render(&field, proxy.proxy_rect(dirty), &mut want); - assert!(ProxyImage::render_full_resolution(&source, &field, canvas, dirty, &mut got, &mut scratch)); - assert_eq!(got, want, "partial update at {sample_type:?}"); - } - } - - #[test] - fn forward_warp_displaces_along_the_stroke_and_reconstruct_restores() { - let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); - s.points = vec![vec![40.0, 32.0], vec![52.0, 32.0]]; - let mut f = LiquifyField::from_strokes(bounds(), 2.0, &[s]); - // Content moved right: the output at the end point samples from the left. - let d = f.sample(52.0, 32.0); - assert!(d[0] < -4.0 && d[1].abs() < 0.5, "{d:?}"); - // A pixel-level check: a marker column follows the stroke. - let mut img = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, true)); - img.fill_rect(bounds(), &[0.0, 1.0]); - img.fill_rect(Rect::new(40, 28, 42, 36), &[1.0, 1.0]); - let out = apply_liquify(&img, &f); - let brightest = (30..70).max_by(|a, b| out.pixel(*a, 32)[0].total_cmp(&out.pixel(*b, 32)[0])).unwrap(); - assert!(brightest > 44, "marker moved right: {brightest}"); - // Reconstruct All restores the original exactly. - let mut rec = LiquifyStroke::new(LiquifyTool::ReconstructAll, 10.0); - rec.amount = Some(100.0); - f.apply_stroke(&rec); - assert!(f.is_identity()); - assert_eq!(apply_liquify(&img, &f), img); - } - - #[test] - fn reconstruct_brush_reduces_the_field_and_freeze_protects() { - let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); - s.points = vec![vec![30.0, 32.0], vec![45.0, 32.0]]; - let mut f = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); - let before = f.max_displacement(); - let mut r = LiquifyStroke::new(LiquifyTool::Reconstruct, 80.0); - r.points = vec![vec![40.0, 32.0]; 30]; - f.apply_stroke(&r); - assert!(f.max_displacement() < before * 0.2, "{} vs {before}", f.max_displacement()); - // Frozen area doesn't move. - let mut fr = LiquifyStroke::new(LiquifyTool::Freeze, 60.0); - fr.density = 100.0; - fr.points = vec![vec![40.0, 32.0]]; - let mut g = LiquifyField::new(bounds(), 2.0); - g.apply_stroke(&fr); - assert!(g.freeze_at(40.0, 32.0) > 0.99); - g.apply_stroke(&s); - assert!(g.sample(40.0, 32.0)[0].abs() < 1e-3); - // Thaw unfreezes. - let mut th = fr.clone(); - th.tool = LiquifyTool::Thaw; - g.apply_stroke(&th); - assert!(g.freeze_at(40.0, 32.0) < 0.01); - // Mask All / Invert / None. - g.apply_stroke(&LiquifyStroke::new(LiquifyTool::FreezeAll, 1.0)); - assert!(g.freeze.iter().all(|f| *f == 1.0)); - g.apply_stroke(&LiquifyStroke::new(LiquifyTool::InvertFreeze, 1.0)); - assert!(g.freeze.iter().all(|f| *f == 0.0)); - g.apply_stroke(&fr); - g.apply_stroke(&LiquifyStroke::new(LiquifyTool::InvertFreeze, 1.0)); - assert!(g.freeze_at(40.0, 32.0) < 0.01 && g.freeze_at(5.0, 5.0) > 0.99); - g.apply_stroke(&LiquifyStroke::new(LiquifyTool::ThawAll, 1.0)); - assert!(g.freeze.iter().all(|f| *f == 0.0)); - } - - #[test] - fn twirl_pucker_bloat_and_push_left_have_the_right_sense() { - let at = |tool: LiquifyTool, pts: Vec>| { - let mut s = LiquifyStroke::new(tool, 40.0); - s.points = pts; - LiquifyField::from_strokes(bounds(), 2.0, &[s]) - }; - let c = vec![vec![48.0, 32.0]; 10]; - // Pucker pulls content in: the output samples outward (d points away from the centre). - let f = at(LiquifyTool::Pucker, c.clone()); - assert!(f.sample(58.0, 32.0)[0] > 0.1); - let f = at(LiquifyTool::Bloat, c.clone()); - assert!(f.sample(58.0, 32.0)[0] < -0.1); - // Twirl clockwise (y down): content right of centre moves down, so it samples from above. - let f = at(LiquifyTool::TwirlCw, c.clone()); - assert!(f.sample(58.0, 32.0)[1] < -0.1, "{:?}", f.sample(58.0, 32.0)); - let f = at(LiquifyTool::TwirlCcw, c); - assert!(f.sample(58.0, 32.0)[1] > 0.1); - // Push Left while dragging up moves content left (samples from the right). - let f = at(LiquifyTool::PushLeft, vec![vec![48.0, 44.0], vec![48.0, 20.0]]); - assert!(f.sample(48.0, 32.0)[0] > 0.5, "{:?}", f.sample(48.0, 32.0)); - // Smooth reduces sharp field differences. - let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 12.0); - s.points = vec![vec![40.0, 32.0], vec![50.0, 32.0]]; - let mut g = LiquifyField::from_strokes(bounds(), 2.0, &[s]); - let m0 = g.max_displacement(); - let mut sm = LiquifyStroke::new(LiquifyTool::Smooth, 60.0); - sm.points = vec![vec![45.0, 32.0]; 20]; - g.apply_stroke(&sm); - assert!(g.max_displacement() < m0); - } - - #[test] - fn replay_is_deterministic_and_depths_agree() { - let mut s = LiquifyStroke::new(LiquifyTool::TwirlCw, 50.0); - s.points = vec![vec![48.0, 32.0, 0.5], vec![50.0, 33.0, 1.0], vec![52.0, 34.0]]; - let a = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); - let b = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); - assert_eq!(a, b); - let r8 = apply_liquify(&sample(SampleType::U8), &a); - let r32 = apply_liquify(&sample(SampleType::F32), &a); - assert!(worst(&r8, &r32, bounds()) < 2.0 / 255.0 + 1e-4); - } - - #[test] - fn proxy_matches_the_full_render_roughly() { - let src = sample(SampleType::U8); - let mut s = LiquifyStroke::new(LiquifyTool::Bloat, 40.0); - s.points = vec![vec![48.0, 32.0]; 8]; - let f = LiquifyField::from_strokes(bounds(), 2.0, &[s]); - let p = ProxyImage::new(&src, bounds(), 96); - assert_eq!((p.w, p.h, p.scale), (96, 64, 1.0)); - let mut out = vec![[0u8; 4]; p.w * p.h]; - p.render(&f, [0, 0, p.w, p.h], &mut out); - let full = apply_liquify(&src, &f); - let mut bad = 0; - for y in 0..64 { - for x in 0..96 { - let q = full.pixel(x, y); - let o = out[y as usize * 96 + x as usize]; - if (f32::from(o[1]) / 255.0 - q[1]).abs() > 0.1 { - bad += 1; - } - } - } - assert!(bad < 96 * 64 / 50, "{bad}"); - } -} +mod tests; diff --git a/crates/algo/src/liquify/tests.rs b/crates/algo/src/liquify/tests.rs new file mode 100644 index 0000000000..1cc360b2c3 --- /dev/null +++ b/crates/algo/src/liquify/tests.rs @@ -0,0 +1,532 @@ +use super::*; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; + +/// The segment walk before #937: every dab, whether or not it can reach the field. +fn full_walk(f: &mut LiquifyField, s: &LiquifyStroke, a: [f64; 3], b: [f64; 3]) -> Rect { + f.walk_segment(s, a, b, false) +} + +#[test] +fn a_segment_walks_only_the_dabs_that_can_reach_the_field() { + // #937: a segment to a far-off point walked one dab per spacing step along its whole + // length (~3e14 for 1e15 px). Skipping the dabs that can't reach a field node must leave + // every result bit-identical to the full walk. + use LiquifyTool::*; + let bounds = Rect::new(-7, 3, 89, 67); + let segments: [([f64; 3], [f64; 3]); 10] = [ + ([10.0, 20.0, 1.0], [70.0, 50.0, 0.5]), // inside + ([-900.0, 30.0, 1.0], [40.0, 31.0, 1.0]), // in from far left + ([40.0, 30.0, 1.0], [40.0, 4000.0, 0.2]), // out downwards + ([-500.0, -500.0, 1.0], [600.0, 600.0, 1.0]), // diagonal through + ([-300.0, -9.0, 1.0], [300.0, -9.0, 1.0]), // parallel, just above (the brush reaches) + ([-300.0, -400.0, 1.0], [300.0, -400.0, 1.0]), // parallel, far above (reaches nothing) + ([88.5, -50.0, 1.0], [88.5, 120.0, 1.0]), // vertical, along the right edge + ([200.0, 200.0, 1.0], [201.0, 900.0, 1.0]), // entirely outside + ([0.0, 0.0, 1.0], [0.25, 0.1, 1.0]), // shorter than one step + ([60.0, 40.0, 1.0], [-2000.0, 45.0, 1.0]), // continues the stroke before it, out left + ]; + for tool in [ForwardWarp, PushLeft, Reconstruct, Smooth, TwirlCw, TwirlCcw, Pucker, Bloat, Freeze, Thaw] { + for size in [6.0, 40.0] { + for (a, b) in segments { + let s = LiquifyStroke::new(tool, size); + let (mut fast, mut full) = (LiquifyField::new(bounds, 2.0), LiquifyField::new(bounds, 2.0)); + // Some displacement first, so Reconstruct and Smooth have something to undo. + for f in [&mut fast, &mut full] { + f.stroke_segment(&LiquifyStroke::new(ForwardWarp, 30.0), [20.0, 20.0, 1.0], [60.0, 40.0, 1.0]); + } + let dirty = fast.stroke_segment(&s, a, b); + assert_eq!(dirty, full_walk(&mut full, &s, a, b), "{tool:?} {size} {a:?} {b:?}"); + assert!(fast.d == full.d && fast.freeze == full.freeze, "{tool:?} {size} {a:?} {b:?}"); + } + } + } +} + +/// The same path sampled by `parts` pointer events per corner-to-corner leg, unevenly. +fn sampled(path: &[[f64; 3]], parts: usize) -> Vec> { + let mut out = vec![path[0].to_vec()]; + for w in path.windows(2) { + let (a, b) = (w[0], w[1]); + // Uneven steps, as a real pointer gives: t grows quadratically. + for k in 1..=parts { + let t = (k * k) as f64 / (parts * parts) as f64; + out.push((0..3).map(|i| a[i] + (b[i] - a[i]) * t).collect()); + } + } + out +} + +/// #2606: dabs restarted at every pointer event, so a fast mouse (few far-apart events) and a +/// slow one (many close ones) warped the same path differently. Spacing now runs on along the +/// whole stroke: any sampling of a path gives the same field. +#[test] +fn the_result_does_not_depend_on_how_finely_the_path_was_sampled() { + use LiquifyTool::*; + let path = [[20.0, 20.0, 1.0], [62.0, 26.0, 0.6], [70.0, 50.0, 0.9], [28.0, 55.0, 1.0], [30.0, 30.0, 1.0]]; + for tool in [ForwardWarp, PushLeft, TwirlCw, Bloat, Reconstruct, Smooth, Freeze] { + for size in [12.0, 40.0] { + let field = |parts: usize| { + let mut warp = LiquifyStroke::new(ForwardWarp, 30.0); + warp.points = vec![vec![30.0, 30.0], vec![50.0, 40.0]]; + let mut s = LiquifyStroke::new(tool, size); + s.points = sampled(&path, parts); + // Some displacement first, so Reconstruct and Smooth have something to change. + LiquifyField::from_strokes(bounds(), 2.0, &[warp, s]) + }; + let coarse = field(1); + for parts in [3, 17, 60] { + let fine = field(parts); + let dd = coarse.d.iter().zip(&fine.d).map(|(p, q)| (p[0] - q[0]).abs().max((p[1] - q[1]).abs())).fold(0.0f32, f32::max); + let df = coarse.freeze.iter().zip(&fine.freeze).map(|(p, q)| (p - q).abs()).fold(0.0f32, f32::max); + assert!(dd < 1e-3 && df < 1e-4, "{tool:?} {size}px, {parts} events per leg: field differs by {dd} px, mask by {df}"); + } + } + } +} + +/// A long drag whose events land exactly on dab positions, along a diagonal: rounding must not +/// add or drop the dab at a segment's end, or the deep displacement a long Forward Warp builds +/// up differs by a whole dab between a slow and a fast pointer. +#[test] +fn a_long_diagonal_drag_gives_one_field_at_any_event_spacing() { + let field = |step: f64| { + let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); + s.points = (0..=(300.0 / step) as usize).map(|k| vec![40.0 + k as f64 * step * 0.6, 20.0 + k as f64 * step * 0.8]).collect(); + LiquifyField::from_strokes(Rect::new(0, 0, 320, 300), 2.0, &[s]) + }; + let (slow, fast) = (field(2.0), field(60.0)); + let dd = slow.d.iter().zip(&fast.d).map(|(p, q)| (p[0] - q[0]).abs().max((p[1] - q[1]).abs())).fold(0.0f32, f32::max); + assert!(slow.max_displacement() > 50.0, "the drag builds up a deep displacement"); + assert!(dd < 1e-3, "slow and fast pointer fields differ by {dd} px"); +} + +#[test] +fn a_huge_canvas_gets_a_coarser_field_not_a_huge_one() { + // 400000² px at 2 px per node is 4·10¹⁰ nodes. + let f = LiquifyField::new(Rect::new(0, 0, 400_000, 400_000), 2.0); + assert!(f.w * f.h <= MAX_FIELD_NODES && f.d.len() == f.w * f.h && f.freeze.len() == f.d.len()); + assert!(f.cell > 64.0); + assert_eq!(LiquifyField::new(Rect::new(0, 0, 100, 100), 2.0).cell, 2.0); +} + +#[test] +fn a_stroke_interrupted_by_a_jump_restarts_its_spacing() { + // A segment that does not start where the last one ended begins a new run of dabs there. + let s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 20.0); + let mut f = LiquifyField::new(bounds(), 2.0); + f.stroke_begin(&s, [10.0, 10.0, 1.0]); + f.stroke_segment(&s, [10.0, 10.0, 1.0], [10.3, 10.0, 1.0]); + let mut g = LiquifyField::new(bounds(), 2.0); + f.stroke_segment(&s, [40.0, 30.0, 1.0], [50.0, 30.0, 1.0]); + g.stroke_begin(&s, [40.0, 30.0, 1.0]); + g.stroke_segment(&s, [40.0, 30.0, 1.0], [50.0, 30.0, 1.0]); + assert_eq!(f, g); + // Hostile points: nothing panics and the next segment starts afresh. + f.stroke_segment(&s, [f64::NAN, 3.0, 1.0], [4.0, f64::INFINITY, 1.0]); + f.stroke_segment(&s, [4.0, 3.0, f64::NAN], [9.0, 3.0, 1.0]); +} + +#[test] +fn a_segment_to_a_far_off_point_returns() { + // The issue's request: about 3.3e14 steps before the fix. Also a segment whose length + // overflows f64. + let mut f = LiquifyField::new(Rect::new(0, 0, 24, 16), 2.0); + let s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0); + let dirty = f.stroke_segment(&s, [0.0, 0.0, 1.0], [1e15, 0.0, 1.0]); + assert!(!dirty.is_empty() && dirty.x1 <= 64, "{dirty:?}"); + f.stroke_segment(&s, [-1e308, 5.0, 1.0], [1e308, 5.0, 1.0]); + f.stroke_segment(&LiquifyStroke::new(LiquifyTool::Bloat, 30.0), [5.0, -1e15, 1.0], [5.0, 1e15, 1.0]); +} + +fn sample(st: SampleType) -> Surface { + let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, st, true)); + for y in 0..64 { + for x in 0..96 { + let v = ((x / 4 + y / 4) % 2) as f32; + s.write_pixel(x, y, &[v, (x as f32) / 96.0, (y as f32) / 64.0, 1.0]); + } + } + s +} + +fn bounds() -> Rect { + Rect::new(0, 0, 96, 64) +} + +fn worst(a: &Surface, b: &Surface, r: Rect) -> f32 { + a.read_region(r).iter().zip(b.read_region(r)).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max) +} + +fn lasso(points: &[[f64; 2]], amount: Option) -> LiquifyStroke { + let mut s = LiquifyStroke::new(LiquifyTool::LassoMask, 1.0); + s.points = points.iter().map(|p| p.to_vec()).collect(); + s.amount = amount; + s +} + +/// Reference even-odd test (ray casting), to check the scanline fill against. +fn inside(p: [f64; 2], poly: &[[f64; 2]]) -> bool { + let mut inside = false; + let mut j = poly.len() - 1; + for (i, a) in poly.iter().enumerate() { + let b = poly[j]; + if (a[1] > p[1]) != (b[1] > p[1]) && p[0] < (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0] { + inside = !inside; + } + j = i; + } + inside +} + +#[test] +fn lasso_freezes_exactly_the_nodes_inside_a_concave_polygon() { + // A concave "C" with a notch, at a non-integer cell size. + let poly = [[10.0, 8.0], [70.0, 8.0], [70.0, 20.0], [30.0, 22.5], [33.0, 40.0], [80.0, 41.0], [60.0, 58.0], [9.0, 55.0]]; + for cell in [1.0, 2.0, 3.5] { + let mut f = LiquifyField::new(bounds(), cell); + let dirty = f.apply_stroke(&lasso(&poly, None)); + assert_eq!(dirty, Rect::new(9, 8, 80, 58)); + for j in 0..f.h { + for i in 0..f.w { + let want = if inside(f.node_pos(i, j), &poly) { 1.0 } else { 0.0 }; + assert_eq!(f.freeze[j * f.w + i], want, "cell {cell}: node ({i}, {j}) at {:?}", f.node_pos(i, j)); + } + } + assert!(f.freeze.contains(&1.0)); + // Strokes replay to the same field (undo rebuilds from the list). + assert_eq!(LiquifyField::from_strokes(bounds(), cell, &[lasso(&poly, None)]), f); + } +} + +#[test] +fn lasso_thaws_with_amount_zero() { + let mut f = LiquifyField::new(bounds(), 2.0); + f.apply_stroke(&LiquifyStroke::new(LiquifyTool::FreezeAll, 1.0)); + assert!(f.freeze.iter().all(|&v| v == 1.0)); + let square = [[20.0, 20.0], [40.0, 20.0], [40.0, 40.0], [20.0, 40.0]]; + f.apply_stroke(&lasso(&square, Some(0.0))); + assert_eq!(f.freeze_at(30.0, 30.0), 0.0, "thawed inside"); + assert_eq!(f.freeze_at(5.0, 5.0), 1.0, "still frozen outside"); +} + +#[test] +fn lasso_ignores_degenerate_and_hostile_polygons() { + let empty = LiquifyField::new(bounds(), 2.0); + let cases: [&[[f64; 2]]; 6] = [ + &[], + &[[10.0, 10.0], [50.0, 50.0]], + &[[f64::NAN, 10.0], [50.0, f64::INFINITY], [20.0, 30.0]], + // Entirely outside the canvas, on every side. + &[[-50.0, -50.0], [-10.0, -50.0], [-10.0, -10.0]], + &[[200.0, 10.0], [300.0, 10.0], [250.0, 50.0]], + &[[10.0, 1e12], [50.0, 1e12], [30.0, 2e12]], + ]; + for poly in cases { + let mut f = empty.clone(); + assert_eq!(f.apply_stroke(&lasso(poly, None)), Rect::EMPTY, "{poly:?}"); + assert_eq!(f, empty, "{poly:?}"); + } + // Huge coordinates around the canvas clamp to it instead of overflowing. + let mut f = empty.clone(); + f.apply_stroke(&lasso(&[[-1e15, -1e15], [1e15, -1e15], [1e15, 1e15], [-1e15, 1e15]], None)); + assert!(f.freeze.iter().all(|&v| v == 1.0)); + // A NaN point among good ones is skipped. + let mut f = empty.clone(); + f.apply_stroke(&lasso(&[[10.0, 10.0], [f64::NAN, 0.0], [50.0, 10.0], [50.0, 50.0], [10.0, 50.0]], None)); + assert_eq!(f.freeze_at(30.0, 30.0), 1.0); +} + +#[test] +fn the_brush_fades_to_nothing_at_its_rim_at_every_density() { + for density in [0.0, 0.25, 0.5, 0.75, 1.0, f64::NAN, -3.0, 7.0] { + assert_eq!(falloff(0.0, density), 1.0, "full strength at the centre ({density})"); + assert_eq!(falloff(1.0, density), 0.0, "nothing at the rim ({density})"); + // Flat at the rim: the last 2 % of the radius moves at most a couple of percent. + assert!(falloff(0.98f64.powi(2), density) < 0.02, "{density}: {}", falloff(0.98f64.powi(2), density)); + let mut prev = 1.0; + for k in 1..=100 { + let v = falloff(k as f64 / 100.0, density); + assert!(v <= prev && v >= 0.0, "{density}: not falling at t² = {k}%"); + prev = v; + } + } + // Density widens the strong core: more strength at every radius, as in Photoshop. + for k in 1..100 { + let t2 = k as f64 / 100.0; + assert!(falloff(t2, 1.0) >= falloff(t2, 0.5) && falloff(t2, 0.5) >= falloff(t2, 0.0), "t² = {t2}"); + } + assert!(falloff(0.5, 1.0) > 0.5 && falloff(0.5, 0.0) < 0.2); +} + +/// #2606: the dab's strength used to stop at 1 - Density at its rim, so a Forward Warp moved +/// everything just inside the brush and nothing just outside, tearing the image along every +/// dab's circle (a hard-edged block at Density 0, half strength at the default 50). +#[test] +fn forward_warp_leaves_the_brush_rim_in_place() { + // A drag of one dab spacing (a twentieth of the radius): one dab, centred where it started. + let (r, drag) = (20.0, 1.0); + for density in [0.0, 50.0, 100.0] { + let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 2.0 * r); + s.density = density; + s.points = vec![vec![48.0, 32.0], vec![48.0 + drag, 32.0]]; + let f = LiquifyField::from_strokes(bounds(), 1.0, &[s]); + let mut rim = 0; + for j in 0..f.h { + for i in 0..f.w { + let p = f.node_pos(i, j); + let t = (p[0] - 48.0).hypot(p[1] - 32.0) / r; + if (0.97..1.0).contains(&t) { + let d = f.d[j * f.w + i]; + assert!(f64::from(d[0].hypot(d[1])) < 0.03 * drag, "density {density}: a node at {t:.3} r moved {d:?}"); + rim += 1; + } + } + } + assert!(rim > 20, "{rim} rim nodes"); + assert!(f.sample(48.0, 32.0)[0] < -0.9 * drag, "density {density}: the centre follows the brush"); + } +} + +#[test] +fn a_wide_integer_translation_keeps_channels_and_tile_edges_at_every_format() { + let b = Rect::new(13, -1, 13 + 70 * 256, 1); + let w = b.width() as usize; + for mode in [ColorMode::Grayscale, ColorMode::Rgb, ColorMode::Cmyk] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let format = PixelFormat::new(mode, depth, true); + let n = format.channels(); + let mut data = Vec::new(); + for i in 0..w * 2 { + let alpha = if i % 13 == 0 { 0.0 } else { 0.5 }; + for c in 0..mode.color_channels() { + data.push(if alpha == 0.0 { 0.0 } else { ((i * 19 + c * 43) % 251 + 2) as f32 / 255.0 }); + } + data.push(alpha); + } + let mut source = Surface::new(format); + source.write_region(b, &data); + source.write_pixel(b.x0 - 1, b.y0 - 1, &vec![0.75; n]); + let before = source.read_region(b); + let mut field = LiquifyField::new(b, 2.5); + field.d.fill([-1.0, 0.0]); + let output = apply_liquify(&source, &field); + let after = output.read_region(b); + let tolerance = if depth == SampleType::F32 { 4.0 * f32::EPSILON } else { 0.0 }; + for (original, moved) in before.chunks_exact(w * n).zip(after.chunks_exact(w * n)) { + for (x, pixel) in moved.chunks_exact(n).enumerate() { + let offset = x.saturating_sub(1) * n; + for (actual, expected) in pixel.iter().zip(&original[offset..offset + n]) { + assert!((actual - expected).abs() <= tolerance, "{mode:?}/{depth:?} at column {x}"); + } + } + } + assert_eq!(output.pixel(b.x0 - 1, b.y0 - 1), source.pixel(b.x0 - 1, b.y0 - 1)); + } + } +} + +#[test] +fn no_strokes_is_identity_at_every_depth() { + for st in [SampleType::U8, SampleType::U16, SampleType::F32] { + let s = sample(st); + let f = LiquifyField::new(bounds(), 2.0); + assert_eq!(apply_liquify(&s, &f), s); + // A field with exactly-zero strokes (zero pressure) is still identity. + let mut st0 = LiquifyStroke::new(LiquifyTool::ForwardWarp, 30.0); + st0.pressure = 0.0; + st0.points = vec![vec![40.0, 30.0], vec![60.0, 30.0]]; + let f = LiquifyField::from_strokes(bounds(), 2.0, &[st0]); + assert!(f.is_identity()); + } +} + +#[test] +fn full_resolution_preview_matches_the_unit_scale_proxy_and_partial_updates() { + let canvas = Rect::new(0, 0, 96, 64); + for sample_type in [SampleType::U8, SampleType::U16, SampleType::F32] { + let source = sample(sample_type); + let mut stroke = LiquifyStroke::new(LiquifyTool::ForwardWarp, 32.0); + stroke.points = vec![vec![38.0, 30.0, 1.0], vec![52.0, 34.0, 0.8]]; + let mut field = LiquifyField::from_strokes(canvas, 2.0, &[stroke]); + + let proxy = ProxyImage::new(&source, canvas, 96); + assert_eq!(proxy.scale, 1.0); + let mut want = vec![[0; 4]; proxy.w * proxy.h]; + proxy.render(&field, [0, 0, proxy.w, proxy.h], &mut want); + + let mut got = vec![[0; 4]; proxy.w * proxy.h]; + let mut scratch = Vec::new(); + assert!(ProxyImage::render_full_resolution(&source, &field, canvas, canvas, &mut got, &mut scratch)); + assert_eq!(got, want, "full-resolution render at {sample_type:?}"); + + let mut more = LiquifyStroke::new(LiquifyTool::Bloat, 16.0); + more.points = vec![vec![43.0, 32.0, 1.0]]; + let dirty = field.apply_stroke(&more); + proxy.render(&field, proxy.proxy_rect(dirty), &mut want); + assert!(ProxyImage::render_full_resolution(&source, &field, canvas, dirty, &mut got, &mut scratch)); + assert_eq!(got, want, "partial update at {sample_type:?}"); + } +} + +#[test] +fn forward_warp_displaces_along_the_stroke_and_reconstruct_restores() { + let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); + s.points = vec![vec![40.0, 32.0], vec![52.0, 32.0]]; + let mut f = LiquifyField::from_strokes(bounds(), 2.0, &[s]); + // Content moved right: the output at the end point samples from the left. + let d = f.sample(52.0, 32.0); + assert!(d[0] < -4.0 && d[1].abs() < 0.5, "{d:?}"); + // A pixel-level check: a marker column follows the stroke. + let mut img = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, true)); + img.fill_rect(bounds(), &[0.0, 1.0]); + img.fill_rect(Rect::new(40, 28, 42, 36), &[1.0, 1.0]); + let out = apply_liquify(&img, &f); + let brightest = (30..70).max_by(|a, b| out.pixel(*a, 32)[0].total_cmp(&out.pixel(*b, 32)[0])).unwrap(); + assert!(brightest > 44, "marker moved right: {brightest}"); + // Reconstruct All restores the original exactly. + let mut rec = LiquifyStroke::new(LiquifyTool::ReconstructAll, 10.0); + rec.amount = Some(100.0); + f.apply_stroke(&rec); + assert!(f.is_identity()); + assert_eq!(apply_liquify(&img, &f), img); +} + +#[test] +fn reconstruct_brush_reduces_the_field_and_freeze_protects() { + let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); + s.points = vec![vec![30.0, 32.0], vec![45.0, 32.0]]; + let mut f = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); + let before = f.max_displacement(); + let mut r = LiquifyStroke::new(LiquifyTool::Reconstruct, 80.0); + r.points = vec![vec![40.0, 32.0]; 30]; + f.apply_stroke(&r); + assert!(f.max_displacement() < before * 0.2, "{} vs {before}", f.max_displacement()); + // Frozen area doesn't move. + let mut fr = LiquifyStroke::new(LiquifyTool::Freeze, 60.0); + fr.density = 100.0; + fr.points = vec![vec![40.0, 32.0]]; + let mut g = LiquifyField::new(bounds(), 2.0); + g.apply_stroke(&fr); + assert!(g.freeze_at(40.0, 32.0) > 0.99); + g.apply_stroke(&s); + assert!(g.sample(40.0, 32.0)[0].abs() < 1e-3); + // Thaw unfreezes. + let mut th = fr.clone(); + th.tool = LiquifyTool::Thaw; + g.apply_stroke(&th); + assert!(g.freeze_at(40.0, 32.0) < 0.01); + // Mask All / Invert / None. + g.apply_stroke(&LiquifyStroke::new(LiquifyTool::FreezeAll, 1.0)); + assert!(g.freeze.iter().all(|f| *f == 1.0)); + g.apply_stroke(&LiquifyStroke::new(LiquifyTool::InvertFreeze, 1.0)); + assert!(g.freeze.iter().all(|f| *f == 0.0)); + g.apply_stroke(&fr); + g.apply_stroke(&LiquifyStroke::new(LiquifyTool::InvertFreeze, 1.0)); + assert!(g.freeze_at(40.0, 32.0) < 0.01 && g.freeze_at(5.0, 5.0) > 0.99); + g.apply_stroke(&LiquifyStroke::new(LiquifyTool::ThawAll, 1.0)); + assert!(g.freeze.iter().all(|f| *f == 0.0)); +} + +#[test] +fn fully_frozen_pixels_keep_their_mapping_when_an_adjacent_area_is_warped() { + let bounds = bounds(); + let mut src = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::F32, true)); + for y in 0..bounds.height() { + for x in 0..bounds.width() { + src.write_pixel(x as i32, y as i32, &[x as f32 / bounds.width() as f32, y as f32 / bounds.height() as f32, 0.0, 1.0]); + } + } + + let mut mask_only = LiquifyField::new(bounds, 2.0); + mask_only.apply_stroke(&lasso(&[[0.0, 0.0], [40.0, 0.0], [40.0, 64.0], [0.0, 64.0]], None)); + assert_eq!(apply_liquify(&src, &mask_only), src); + + let mut field = LiquifyField::new(bounds, 2.0); + let mut first_warp = LiquifyStroke::new(LiquifyTool::ForwardWarp, 40.0); + first_warp.points = vec![vec![22.0, 32.0], vec![34.0, 32.0]]; + field.apply_stroke(&first_warp); + + field.apply_stroke(&lasso(&[[0.0, 0.0], [40.0, 0.0], [40.0, 64.0], [0.0, 64.0]], None)); + let frozen_pixel = (37, 32); + assert!(field.freeze_at(f64::from(frozen_pixel.0) + 0.5, f64::from(frozen_pixel.1) + 0.5) > 0.999); + let before = apply_liquify(&src, &field).pixel(frozen_pixel.0, frozen_pixel.1); + assert_ne!(before, src.pixel(frozen_pixel.0, frozen_pixel.1), "freezing must retain the earlier warp"); + let neighbor_before = apply_liquify(&src, &field).pixel(43, 32); + + let mut adjacent_warp = LiquifyStroke::new(LiquifyTool::ForwardWarp, 24.0); + adjacent_warp.points = vec![vec![44.0, 32.0], vec![52.0, 32.0]]; + field.apply_stroke(&adjacent_warp); + + let after = apply_liquify(&src, &field).pixel(frozen_pixel.0, frozen_pixel.1); + let neighbor_after = apply_liquify(&src, &field).pixel(43, 32); + assert_eq!(before, after, "a fully frozen pixel must keep its pre-neighbor-warp mapping"); + assert_ne!(neighbor_before, neighbor_after, "an adjacent unfrozen pixel must still move"); +} + +#[test] +fn twirl_pucker_bloat_and_push_left_have_the_right_sense() { + let at = |tool: LiquifyTool, pts: Vec>| { + let mut s = LiquifyStroke::new(tool, 40.0); + s.points = pts; + LiquifyField::from_strokes(bounds(), 2.0, &[s]) + }; + let c = vec![vec![48.0, 32.0]; 10]; + // Pucker pulls content in: the output samples outward (d points away from the centre). + let f = at(LiquifyTool::Pucker, c.clone()); + assert!(f.sample(58.0, 32.0)[0] > 0.1); + let f = at(LiquifyTool::Bloat, c.clone()); + assert!(f.sample(58.0, 32.0)[0] < -0.1); + // Twirl clockwise (y down): content right of centre moves down, so it samples from above. + let f = at(LiquifyTool::TwirlCw, c.clone()); + assert!(f.sample(58.0, 32.0)[1] < -0.1, "{:?}", f.sample(58.0, 32.0)); + let f = at(LiquifyTool::TwirlCcw, c); + assert!(f.sample(58.0, 32.0)[1] > 0.1); + // Push Left while dragging up moves content left (samples from the right). + let f = at(LiquifyTool::PushLeft, vec![vec![48.0, 44.0], vec![48.0, 20.0]]); + assert!(f.sample(48.0, 32.0)[0] > 0.5, "{:?}", f.sample(48.0, 32.0)); + // Smooth reduces sharp field differences. + let mut s = LiquifyStroke::new(LiquifyTool::ForwardWarp, 12.0); + s.points = vec![vec![40.0, 32.0], vec![50.0, 32.0]]; + let mut g = LiquifyField::from_strokes(bounds(), 2.0, &[s]); + let m0 = g.max_displacement(); + let mut sm = LiquifyStroke::new(LiquifyTool::Smooth, 60.0); + sm.points = vec![vec![45.0, 32.0]; 20]; + g.apply_stroke(&sm); + assert!(g.max_displacement() < m0); +} + +#[test] +fn replay_is_deterministic_and_depths_agree() { + let mut s = LiquifyStroke::new(LiquifyTool::TwirlCw, 50.0); + s.points = vec![vec![48.0, 32.0, 0.5], vec![50.0, 33.0, 1.0], vec![52.0, 34.0]]; + let a = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); + let b = LiquifyField::from_strokes(bounds(), 2.0, std::slice::from_ref(&s)); + assert_eq!(a, b); + let r8 = apply_liquify(&sample(SampleType::U8), &a); + let r32 = apply_liquify(&sample(SampleType::F32), &a); + assert!(worst(&r8, &r32, bounds()) < 2.0 / 255.0 + 1e-4); +} + +#[test] +fn proxy_matches_the_full_render_roughly() { + let src = sample(SampleType::U8); + let mut s = LiquifyStroke::new(LiquifyTool::Bloat, 40.0); + s.points = vec![vec![48.0, 32.0]; 8]; + let f = LiquifyField::from_strokes(bounds(), 2.0, &[s]); + let p = ProxyImage::new(&src, bounds(), 96); + assert_eq!((p.w, p.h, p.scale), (96, 64, 1.0)); + let mut out = vec![[0u8; 4]; p.w * p.h]; + p.render(&f, [0, 0, p.w, p.h], &mut out); + let full = apply_liquify(&src, &f); + let mut bad = 0; + for y in 0..64 { + for x in 0..96 { + let q = full.pixel(x, y); + let o = out[y as usize * 96 + x as usize]; + if (f32::from(o[1]) / 255.0 - q[1]).abs() > 0.1 { + bad += 1; + } + } + } + assert!(bad < 96 * 64 / 50, "{bad}"); +} diff --git a/crates/algo/src/magnetic.rs b/crates/algo/src/magnetic.rs index f2dab08816..54759237cb 100644 --- a/crates/algo/src/magnetic.rs +++ b/crates/algo/src/magnetic.rs @@ -512,12 +512,11 @@ fn direction_cost(tp: [f32; 2], tq: [f32; 2], dx: i32, dy: i32, len: f32) -> f32 if tp == [0.0; 2] || tq == [0.0; 2] { return NEUTRAL_DIRECTION; } - let mut l = [dx as f32 / len, dy as f32 / len]; - if tp[0] * l[0] + tp[1] * l[1] < 0.0 { - l = [-l[0], -l[1]]; - } - let dp = (tp[0] * l[0] + tp[1] * l[1]).clamp(-1.0, 1.0); - let dq = (l[0] * tq[0] + l[1] * tq[1]).clamp(-1.0, 1.0); + // An edge tangent is an axis, not a directed vector: reversing the strongest channel's + // gradient must not make an otherwise straight edge expensive to follow. + let l = [dx as f32 / len, dy as f32 / len]; + let dp = (tp[0] * l[0] + tp[1] * l[1]).abs().clamp(0.0, 1.0); + let dq = (l[0] * tq[0] + l[1] * tq[1]).abs().clamp(0.0, 1.0); 2.0 / (3.0 * PI) * (dp.acos() + dq.acos()) } diff --git a/crates/algo/src/magnetic/tests.rs b/crates/algo/src/magnetic/tests.rs index ccfc73bc04..ee187bc781 100644 --- a/crates/algo/src/magnetic/tests.rs +++ b/crates/algo/src/magnetic/tests.rs @@ -55,6 +55,39 @@ fn snaps_to_the_most_prominent_edge() { assert!((p[0] - 18.0).abs() < 0.01, "{p:?}"); } +#[test] +fn gradient_polarity_does_not_make_a_straight_edge_look_like_a_turn() { + // A gradient can reverse across adjacent pixels when another colour channel becomes the + // strongest one. The edge tangent is an axis, so its sign must not change the path cost. + let forward = direction_cost([1.0, 0.0], [1.0, 0.0], 1, 0, 1.0); + let reversed = direction_cost([1.0, 0.0], [-1.0, 0.0], 1, 0, 1.0); + assert!(forward < 1e-6, "aligned edge: {forward}"); + assert!(reversed < 1e-6, "same edge with reversed gradient polarity: {reversed}"); +} + +#[test] +fn traces_a_straight_edge_through_a_gradient_polarity_change() { + let read = Cell::new(0); + // The contrast stays high, but the edge changes from dark-to-light to light-to-dark halfway + // down. The horizontal seam intersects it; the guide stays on the vertical contour. + let mut f = image( + |x, y| { + let left = x < 32; + let top = y < 32; + let white = left != top; + grey(if white { 255 } else { 0 }) + }, + &read, + ); + let mut t = Tracer::new(Rect::new(0, 0, 64, 64)); + let path = t.trace(&mut f, [32.0, 5.0], [32.0, 59.0], &[[32.0, 32.0]], Settings::default()); + assert_eq!(path.first(), Some(&[32.0, 5.0])); + assert_eq!(path.last(), Some(&[32.0, 59.0])); + for p in &path { + assert!((p[0] - 32.0).abs() < 1.0, "left the high-contrast edge: {p:?}; {path:?}"); + } +} + #[test] fn trace_follows_a_curved_edge() { let read = Cell::new(0); diff --git a/crates/algo/src/matting.rs b/crates/algo/src/matting.rs index e40b687040..0901dab0c0 100644 --- a/crates/algo/src/matting.rs +++ b/crates/algo/src/matting.rs @@ -35,7 +35,9 @@ pub struct RefineParams { pub feather: f32, /// 0–100. pub contrast: f32, - /// −100–100. 100% moves the edge by `max(radius, 4)` pixels. Values between whole + /// −100–100. 100% moves the edge by its soft width: `max(radius, feather, 2 × smooth σ)`, + /// at least 1 px, so a hard edge moves a pixel (Photoshop's Shift Edge moves soft borders; + /// a band of several pixels around hard line art fills it in solid). Values between whole /// pixels blend the two neighbouring shifts, so the slider does not jump. pub shift_edge: f32, } @@ -60,9 +62,11 @@ impl RefineParams { fn feather_sigma(&self) -> f32 { self.feather.max(0.0) / 2.0 } - /// Signed shift in pixels, fractional. 100% is `max(radius, 4)` px, clamped to ±64. + /// Signed shift in pixels, fractional. 100% is the edge's soft width (at least 1 px), + /// clamped to ±64. fn shift_amount(&self) -> f32 { - let px = self.shift_edge.clamp(-100.0, 100.0) / 100.0 * self.radius.max(4.0); + let soft = self.radius.max(self.feather).max(2.0 * self.smooth_sigma()).max(1.0); + let px = self.shift_edge.clamp(-100.0, 100.0) / 100.0 * soft; if px.is_finite() { px.clamp(-64.0, 64.0) } else { 0.0 } } /// How many pixels the mask can move: the farther of the two integer radii a fractional @@ -238,7 +242,7 @@ pub fn morph(src: &[f32], w: usize, h: usize, r: usize, grow: bool) -> Vec mod brush; mod morphology; -pub use brush::{EdgeBrush, EdgeStroke, MAX_SWEPT_AREA, SIZE_RANGE as BRUSH_SIZE_RANGE, swept_area}; +pub use brush::{EdgeBrush, EdgeStroke, MAX_SWEPT_AREA, SIZE_RANGE as BRUSH_SIZE_RANGE, brush_surface, painted_tiles, stroke_area, swept_area}; /// Running max (or min) over a window of radius `r` (edge-clipped), van Herk / Gil–Werman: /// three comparisons per sample whatever the radius. @@ -413,6 +417,22 @@ where lo.into_iter().zip(hi).map(|(a, b)| a + (b - a) * frac).collect() } +/// Most pixels one refine should cover. Its result is a dense [`Region`] over [`refine_area`] +/// (about two bytes a pixel while it is assembled), so callers refuse a larger refine up front +/// rather than allocate it: 2³⁰ pixels, a 32k × 32k selection. +pub const MAX_REFINE_AREA: u64 = 1 << 30; + +/// The area [`refine_mask`] and [`refine_mask_brushed`] compute and allocate their result over: +/// `content` grown by how far `p` can spread it, plus where `brush` adds, within `canvas`. Empty +/// when `content` misses the canvas. +pub fn refine_area(content: Rect, canvas: Rect, p: &RefineParams, brush: Option<&EdgeBrush>) -> Rect { + let content = content.intersect(&canvas); + if content.is_empty() { + return Rect::EMPTY; + } + content.inflate(p.spread()).union(&brush.map_or(Rect::EMPTY, EdgeBrush::added_bounds)).intersect(&canvas) +} + /// Refines a selection mask. `mask(r)` returns coverage over `r` (row-major); `content` bounds /// its non-zero pixels; the result is clipped to `canvas`. `sampler` supplies the guide image /// (only read when `radius > 0`). Returns `None` when nothing remains selected. @@ -446,11 +466,10 @@ fn refine_mask_with( where F: Fn(&[f32], usize, usize, usize, bool) -> Vec + Copy + Sync + Send, { - let content = content.intersect(&canvas); - if content.is_empty() { + let bbox = refine_area(content, canvas, p, brush); + if bbox.is_empty() { return None; } - let bbox = content.inflate(p.spread()).union(&brush.map_or(Rect::EMPTY, EdgeBrush::added_bounds)).intersect(&canvas); let halo = p.halo() + brush.map_or(0, |b| i32::try_from(b.window() * 4).unwrap_or(i32::MAX)); let mut tiles = Vec::new(); let mut y = bbox.y0; @@ -750,22 +769,24 @@ mod tests { assert!(f[5 * w + 29] > 0.5 && f[5 * w + 29] < 0.8 && f[5 * w + 31] > 0.1); let c = refine_buffer(None, &f, w, h, &RefineParams { contrast: 100.0, ..Default::default() }); assert!(c[5 * w + 29] == 1.0 && c[5 * w + 31] == 0.0); + // A hard edge (no radius, feather or smooth) moves 1 px at 100%, so 50% is half a + // pixel. The pixel just outside fades in instead of snapping on. let grown = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: 100.0, ..Default::default() }); - assert_eq!(grown[5 * w + 33], 1.0); - assert_eq!(grown[5 * w + 34], 0.0); - let shrunk = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: -50.0, ..Default::default() }); - assert_eq!(shrunk[5 * w + 27], 1.0); - assert_eq!(shrunk[5 * w + 28], 0.0); - // Radius 0 scales 100% to 4 px, so 12.5% is half a pixel and 25% is one pixel. - // The pixel just outside a hard edge fades in instead of snapping on. - let half = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: 12.5, ..Default::default() }); - let one = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: 25.0, ..Default::default() }); assert_eq!(m[5 * w + 30], 0.0); + assert_eq!(grown[5 * w + 30], 1.0); + assert_eq!(grown[5 * w + 31], 0.0); + let shrunk = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: -100.0, ..Default::default() }); + assert_eq!(shrunk[5 * w + 28], 1.0); + assert_eq!(shrunk[5 * w + 29], 0.0); + let half = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: 50.0, ..Default::default() }); assert!((half[5 * w + 30] - 0.5).abs() < 0.02, "half pixel {}", half[5 * w + 30]); - assert_eq!(one[5 * w + 30], 1.0); assert_eq!(half[5 * w + 31], 0.0, "the blend must not jump a second pixel"); - let pulled = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: -12.5, ..Default::default() }); + let pulled = refine_buffer(None, &m, w, h, &RefineParams { shift_edge: -50.0, ..Default::default() }); assert!((pulled[5 * w + 29] - 0.5).abs() < 0.02, "half-pixel contract {}", pulled[5 * w + 29]); + // A softer edge moves farther: 100% is the feather, here 4 px. + let wide = refine_buffer(None, &m, w, h, &RefineParams { feather: 4.0, contrast: 100.0, shift_edge: 100.0, ..Default::default() }); + assert_eq!(wide[5 * w + 33], 1.0); + assert_eq!(wide[5 * w + 34], 0.0); let sm = refine_buffer(None, &m, w, h, &RefineParams { smooth: 50.0, ..Default::default() }); assert!(sm[5 * w + 10] == 1.0 && sm[5 * w + 50] == 0.0); } diff --git a/crates/algo/src/matting/brush.rs b/crates/algo/src/matting/brush.rs index f70342e6d7..24e4c82ad1 100644 --- a/crates/algo/src/matting/brush.rs +++ b/crates/algo/src/matting/brush.rs @@ -10,10 +10,17 @@ //! size), the colour-line model of A. Levin, D. Lischinski and Y. Weiss, "A Closed-Form Solution //! to Natural Image Matting" (CVPR 2006), with one foreground and one background colour per window. -use photocraft_geom::Rect; +use std::collections::{BTreeMap, BTreeSet}; + +use photocraft_color::PixelFormat; +use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; +use photocraft_raster::Surface; use super::box_mean; use crate::segment::RgbImage; +use crate::selection::{MAX_PAINTED_TILES, clip_segment}; + +const TS: usize = TILE_SIZE as usize; /// One Refine Edge Brush stroke, in document pixels. #[derive(Clone, Debug, PartialEq)] @@ -29,26 +36,42 @@ pub struct EdgeStroke { /// Brush diameters the strokes may use. pub const SIZE_RANGE: std::ops::RangeInclusive = 1.0..=5000.0; -/// The painted area: signed coverage (`+1` refined, `−1` restored, `0` untouched) over `bbox`. +/// The painted area: signed coverage (`+1` refined, `−1` restored, `0` untouched), kept only for +/// the tiles the strokes paint, so a thin stroke across a huge canvas costs those tiles, not its +/// bounding box (#8783). #[derive(Clone, Debug, PartialEq)] pub struct EdgeBrush { + /// Where the strokes can paint. bbox: Rect, - /// Coverage × 127, row-major over `bbox`. - v: Vec, + /// Coverage × 127 per painted tile, row-major over the tile. + tiles: BTreeMap>, /// Bounds of the added (positive) coverage. added: Rect, /// Box radius of the colour estimate (pixels). window: usize, } -/// Coverage (0–1) of a round brush of radius `r` swept from `a` to `b`, at pixel centre `p`. -fn capsule(p: (f32, f32), a: (f32, f32), b: (f32, f32), r: f32) -> f32 { +/// Distance from pixel centre `p` to the segment from `a` to `b`. +fn segment_distance(p: (f32, f32), a: (f32, f32), b: (f32, f32)) -> f32 { let (dx, dy) = (b.0 - a.0, b.1 - a.1); let len2 = dx * dx + dy * dy; let t = if len2 > 0.0 { (((p.0 - a.0) * dx + (p.1 - a.1) * dy) / len2).clamp(0.0, 1.0) } else { 0.0 }; let (cx, cy) = (a.0 + t * dx, a.1 + t * dy); - let d = ((p.0 - cx).powi(2) + (p.1 - cy).powi(2)).sqrt(); - (r + 0.5 - d).clamp(0.0, 1.0) + ((p.0 - cx).powi(2) + (p.1 - cy).powi(2)).sqrt() +} + +/// Coverage (0–1) of a round brush of radius `r` swept from `a` to `b`, at pixel centre `p`. +fn capsule(p: (f32, f32), a: (f32, f32), b: (f32, f32), r: f32) -> f32 { + (r + 0.5 - segment_distance(p, a, b)).clamp(0.0, 1.0) +} + +/// Coverage (0–1) at distance `d` from a brush's path, for radius `r` and a solid core of radius +/// `core` (hardness × `r`): full inside the core, then a smooth fall to zero half a pixel past `r` +/// (a hard brush keeps just the one-pixel anti-aliased rim). +fn soft_coverage(d: f32, r: f32, core: f32) -> f32 { + let ramp = r + 0.5 - core; + let t = ((r + 0.5 - d) / ramp.max(1.0)).clamp(0.0, 1.0); + if ramp > 1.0 { t * t * (3.0 - 2.0 * t) } else { t } } /// The pixels a disc of radius `r` at `p` can touch, as a (possibly empty) rectangle. @@ -64,27 +87,113 @@ pub const MAX_SWEPT_AREA: f64 = (1u64 << 28) as f64; /// The area `strokes` sweep (see [`MAX_SWEPT_AREA`]); non-finite strokes count as infinite. pub fn swept_area(strokes: &[EdgeStroke]) -> f64 { - strokes - .iter() - .map(|s| { - let size = f64::from(s.size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end())); - let len: f64 = s.points.windows(2).filter_map(|w| Some(f64::from((w.get(1)?.0 - w.first()?.0).hypot(w.get(1)?.1 - w.first()?.1)))).sum(); - if len.is_finite() { len * size + size * size } else { f64::INFINITY } - }) - .sum() + strokes.iter().map(|s| stroke_area(&s.points, s.size)).sum() } -/// The stroke's path as short segments: points closer than a quarter of the radius to the last -/// kept one are dropped, and long segments are split at most `r` long, so each segment's box -/// stays close to the area it paints. -fn segments(points: &[(f32, f32)], r: f32) -> Vec<((f32, f32), (f32, f32))> { - let Some(&first) = points.first() else { return Vec::new() }; +/// The area one stroke of `points` sweeps with a brush `size` wide: length × size + size²; +/// non-finite strokes count as infinite. +pub fn stroke_area(points: &[(f32, f32)], size: f32) -> f64 { + let size = f64::from(size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end())); + let len: f64 = points.windows(2).filter_map(|w| Some(f64::from((w.get(1)?.0 - w.first()?.0).hypot(w.get(1)?.1 - w.first()?.1)))).sum(); + if len.is_finite() { len * size + size * size } else { f64::INFINITY } +} + +/// The tiles of `canvas` a round brush `size` px wide along `points` can paint, in order; `None` +/// when that is more than `limit` (a thin stroke sweeps little but touches a whole tile wherever +/// it passes, so this, not [`MAX_SWEPT_AREA`], bounds what painting it allocates; #8783). +pub fn painted_tiles(points: &[(f32, f32)], size: f32, canvas: Rect, limit: usize) -> Option> { + let r = size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end()) / 2.0; + let mut set = BTreeSet::new(); + // Consecutive pieces mostly fall in the same tiles. + let mut last: Option = None; + for (a, b) in segments(points, r, canvas) { + let seg = dab_bounds(a, r).union(&dab_bounds(b, r)).intersect(&canvas); + // The tile coordinates `seg` spans (inclusive). + let span = + Rect::new(seg.x0.div_euclid(TILE_SIZE), seg.y0.div_euclid(TILE_SIZE), (seg.x1 - 1).div_euclid(TILE_SIZE), (seg.y1 - 1).div_euclid(TILE_SIZE)); + if seg.is_empty() || last == Some(span) { + continue; + } + last = Some(span); + for tc in seg.tiles() { + set.insert(tc); + if set.len() > limit { + return None; + } + } + } + Some(set) +} + +/// One stroke's coverage (0–255) per tile of `canvas`: the most `cov(pixel centre, a, b)` gives +/// over the path's pieces, so a stroke never paints past full. Only painted tiles are kept; +/// `None` past `limit` tiles (see [`painted_tiles`]). +fn paint_tiles( + points: &[(f32, f32)], + size: f32, + canvas: Rect, + limit: usize, + cov: impl Fn((f32, f32), (f32, f32), (f32, f32)) -> f32, +) -> Option>> { + let r = size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end()) / 2.0; + let mut tiles: BTreeMap> = painted_tiles(points, size, canvas, limit)?.into_iter().map(|tc| (tc, vec![0u8; TS * TS])).collect(); + for (a, b) in segments(points, r, canvas) { + let seg = dab_bounds(a, r).union(&dab_bounds(b, r)).intersect(&canvas); + for tc in seg.tiles() { + let o = tc.rect(); + let Some(buf) = tiles.get_mut(&tc) else { continue }; + let tr = o.intersect(&seg); + for y in tr.y0..tr.y1 { + for x in tr.x0..tr.x1 { + let c = (cov((x as f32 + 0.5, y as f32 + 0.5), a, b).clamp(0.0, 1.0) * 255.0).round() as u8; + if let Some(slot) = buf.get_mut((y - o.y0) as usize * TS + (x - o.x0) as usize) + && c > *slot + { + *slot = c; + } + } + } + } + } + tiles.retain(|_, b| b.iter().any(|v| *v > 0)); + Some(tiles) +} + +/// Select and Mask's Brush: the coverage one stroke of a round brush `size` px wide with +/// `hardness` (0–100) paints along `points`, clipped to `canvas`, as a GRAY8 selection holding +/// only the tiles it paints. Overlapping dabs keep the most coverage any of them gives, so a +/// stroke never paints past full. `None` when it paints nothing, has non-finite points, sweeps +/// more than [`MAX_SWEPT_AREA`] or paints more than [`MAX_PAINTED_TILES`] tiles. +pub fn brush_surface(points: &[(f32, f32)], size: f32, hardness: f32, canvas: Rect) -> Option { + if points.is_empty() || !size.is_finite() || !hardness.is_finite() || points.iter().any(|p| !p.0.is_finite() || !p.1.is_finite()) { + return None; + } + if stroke_area(points, size) > MAX_SWEPT_AREA { + return None; + } + let r = size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end()) / 2.0; + let core = r * hardness.clamp(0.0, 100.0) / 100.0; + let tiles = paint_tiles(points, size, canvas, MAX_PAINTED_TILES, |p, a, b| soft_coverage(segment_distance(p, a, b), r, core))?; + let mut s = Surface::new(PixelFormat::GRAY8); + for (tc, bytes) in &tiles { + s.write_interleaved(tc.rect(), bytes); + } + (s.tile_count() > 0).then_some(s) +} + +/// The stroke's path as short segments within reach of `clip`: points closer than a quarter of +/// the radius to the last kept one are dropped, each segment is cut to `clip` grown by the brush +/// (what lies beyond can't paint inside it), and long ones are split at most `r` long, so each +/// piece's box stays close to the area it paints. The pieces are made as they are painted, never +/// collected, and only where they can paint: a 1 px stroke zigzagging across 2 million px is over +/// 100 million pieces in all, within [`MAX_SWEPT_AREA`] (the #8783 review). +fn segments(points: &[(f32, f32)], r: f32, clip: Rect) -> impl Iterator { let min_step = (r / 4.0).max(0.5); let max_step = r.max(2.0); - let mut kept = vec![first]; - for p in points.iter().skip(1) { - let last = kept.last().copied().unwrap_or(first); - if (p.0 - last.0).hypot(p.1 - last.1) >= min_step { + let reach = clip.inflate((r + 2.0).ceil().clamp(0.0, 1e7) as i32); + let mut kept: Vec<(f32, f32)> = Vec::new(); + for p in points { + if kept.last().is_none_or(|l| (p.0 - l.0).hypot(p.1 - l.1) >= min_step) { kept.push(*p); } } @@ -93,25 +202,22 @@ fn segments(points: &[(f32, f32)], r: f32) -> Vec<((f32, f32), (f32, f32))> { { kept.push(end); } - if kept.len() == 1 { - return vec![(first, first)]; - } - let mut out = Vec::new(); - for w in kept.windows(2) { - let (Some(&a), Some(&b)) = (w.first(), w.get(1)) else { continue }; + // A single point is one dab. + let dab = if kept.len() == 1 { kept.first().map(|p| (*p, *p)) } else { None }; + let pairs: Vec<((f32, f32), (f32, f32))> = kept.windows(2).filter_map(|w| clip_segment(*w.first()?, *w.get(1)?, reach)).collect(); + dab.into_iter().chain(pairs.into_iter().flat_map(move |(a, b)| { let pieces = ((b.0 - a.0).hypot(b.1 - a.1) / max_step).ceil().clamp(1.0, 1e6) as usize; - let at = |k: usize| { + let at = move |k: usize| { let t = k as f32 / pieces as f32; (a.0 + (b.0 - a.0) * t, a.1 + (b.1 - a.1) * t) }; - out.extend((0..pieces).map(|k| (at(k), at(k + 1)))); - } - out + (0..pieces).map(move |k| (at(k), at(k + 1))) + })) } impl EdgeBrush { - /// Rasterises `strokes` (clipped to `canvas`). `None` when they paint nothing or sweep more - /// than [`MAX_SWEPT_AREA`]. + /// Rasterises `strokes` (clipped to `canvas`), tile by tile. `None` when they paint nothing, + /// sweep more than [`MAX_SWEPT_AREA`] or paint more than [`MAX_PAINTED_TILES`] tiles. pub fn new(strokes: &[EdgeStroke], canvas: Rect) -> Option { let usable = |s: &&EdgeStroke| s.size.is_finite() && !s.points.is_empty() && s.points.iter().all(|p| p.0.is_finite() && p.1.is_finite()); if swept_area(strokes) > MAX_SWEPT_AREA { @@ -126,61 +232,43 @@ impl EdgeBrush { if bbox.is_empty() { return None; } - let bw = bbox.width() as usize; - let mut v = vec![0i8; bw.checked_mul(bbox.height() as usize)?]; - let mut cov: Vec = Vec::new(); + let mut tiles: BTreeMap> = BTreeMap::new(); let mut max_add = 0.0f32; for s in strokes { - let sb = stroke_bounds(s); - if sb.is_empty() { - continue; - } let r = s.size.clamp(*SIZE_RANGE.start(), *SIZE_RANGE.end()) / 2.0; if s.add { max_add = max_add.max(2.0 * r); } - let sw = sb.width() as usize; - cov.clear(); - cov.resize(sw * sb.height() as usize, 0.0); // The stroke's coverage: the most any of its segments gives (a single dab for one point). - for (a, b) in segments(&s.points, r) { - let seg = dab_bounds(a, r).union(&dab_bounds(b, r)).intersect(&sb); - for y in seg.y0..seg.y1 { - for x in seg.x0..seg.x1 { - let c = capsule((x as f32 + 0.5, y as f32 + 0.5), a, b, r); - if let Some(slot) = cov.get_mut((y - sb.y0) as usize * sw + (x - sb.x0) as usize) - && c > *slot - { - *slot = c; - } - } - } - } + let cov = paint_tiles(&s.points, s.size, canvas, MAX_PAINTED_TILES, |p, a, b| capsule(p, a, b, r))?; let target = if s.add { 1.0 } else { -1.0 }; - for y in sb.y0..sb.y1 { - for x in sb.x0..sb.x1 { - let c = cov.get((y - sb.y0) as usize * sw + (x - sb.x0) as usize).copied().unwrap_or(0.0); - if c <= 0.0 { - continue; - } - if let Some(slot) = v.get_mut((y - bbox.y0) as usize * bw + (x - bbox.x0) as usize) { - let cur = f32::from(*slot) / 127.0; + for (tc, c) in cov { + let v = tiles.entry(tc).or_insert_with(|| vec![0i8; TS * TS]); + for (slot, c) in v.iter_mut().zip(c) { + if c > 0 { + let (cur, c) = (f32::from(*slot) / 127.0, f32::from(c) / 255.0); *slot = ((cur + (target - cur) * c) * 127.0).round().clamp(-127.0, 127.0) as i8; } } } + if tiles.len() > MAX_PAINTED_TILES { + return None; + } } - if v.iter().all(|c| *c == 0) { + tiles.retain(|_, v| v.iter().any(|c| *c != 0)); + if tiles.is_empty() { return None; } - let (mut x0, mut y0, mut x1, mut y1) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN); - for (row, line) in v.chunks_exact(bw).enumerate() { - let (Some(first), Some(last)) = (line.iter().position(|c| *c > 0), line.iter().rposition(|c| *c > 0)) else { continue }; - let y = bbox.y0 + row as i32; - (x0, x1, y0, y1) = (x0.min(bbox.x0 + first as i32), x1.max(bbox.x0 + last as i32 + 1), y0.min(y), y1.max(y + 1)); + let mut added = Rect::EMPTY; + for (tc, v) in &tiles { + let o = tc.rect(); + for (row, line) in v.as_chunks::().0.iter().enumerate() { + let (Some(first), Some(last)) = (line.iter().position(|c| *c > 0), line.iter().rposition(|c| *c > 0)) else { continue }; + let y = o.y0 + row as i32; + added = added.union(&Rect::new(o.x0 + first as i32, y, o.x0 + last as i32 + 1, y + 1)); + } } - let added = if x0 < x1 { Rect::new(x0, y0, x1, y1) } else { Rect::EMPTY }; - Some(EdgeBrush { bbox, v, added, window: max_add.ceil().clamp(4.0, 64.0) as usize }) + Some(EdgeBrush { bbox, tiles, added, window: max_add.ceil().clamp(4.0, 64.0) as usize }) } /// Where the strokes paint (added or restored). @@ -200,16 +288,32 @@ impl EdgeBrush { /// Signed coverage at document pixel `(x, y)` (zero outside the painted area). pub fn at(&self, x: i32, y: i32) -> f32 { - if !self.bbox.contains(x, y) { - return 0.0; - } - let i = (y - self.bbox.y0) as usize * self.bbox.width() as usize + (x - self.bbox.x0) as usize; - self.v.get(i).map_or(0.0, |c| f32::from(*c) / 127.0) + let (tc, o) = (TileCoord::containing(x, y), (y.rem_euclid(TILE_SIZE) as usize) * TS + x.rem_euclid(TILE_SIZE) as usize); + self.tiles.get(&tc).and_then(|v| v.get(o)).map_or(0.0, |c| f32::from(*c) / 127.0) } /// Signed coverage over `r` (row-major). pub fn read(&self, r: Rect) -> Vec { - (r.y0..r.y1).flat_map(|y| (r.x0..r.x1).map(move |x| self.at(x, y))).collect() + let w = r.width() as usize; + let mut out = vec![0.0f32; w * r.height() as usize]; + for tc in r.tiles() { + let Some(v) = self.tiles.get(&tc) else { continue }; + let (o, tr) = (tc.rect(), tc.rect().intersect(&r)); + for y in tr.y0..tr.y1 { + let src = (y - o.y0) as usize * TS; + let dst = (y - r.y0) as usize * w; + let (Some(src), Some(dst)) = ( + v.get(src + (tr.x0 - o.x0) as usize..src + (tr.x1 - o.x0) as usize), + out.get_mut(dst + (tr.x0 - r.x0) as usize..dst + (tr.x1 - r.x0) as usize), + ) else { + continue; + }; + for (d, c) in dst.iter_mut().zip(src) { + *d = f32::from(*c) / 127.0; + } + } + } + out } } @@ -276,6 +380,10 @@ mod tests { use crate::segment::ImageSampler; use crate::selection::Region; + fn at(s: &Surface, x: i32, y: i32) -> f32 { + s.pixel(x, y).first().copied().unwrap_or(0.0) + } + /// Strands of the subject's colour (every 4th column from x = 60) beyond the selection's /// edge at x = 40: brushing over them adds them, Alt-brushing the edge keeps it as it was. #[test] @@ -330,4 +438,73 @@ mod tests { let empty = EdgeStroke { points: vec![], size: 10.0, add: true }; assert!(EdgeBrush::new(&[off, nan, empty], canvas).is_none()); } + #[test] + fn brush_surface_paints_hard_and_soft_strokes() { + let canvas = Rect::new(0, 0, 100, 50); + let hard = brush_surface(&[(10.0, 25.0), (90.0, 25.0)], 20.0, 100.0, canvas).expect("painted"); + assert!(at(&hard, 50, 25) > 0.99 && at(&hard, 50, 33) > 0.99, "solid inside the radius"); + assert!(at(&hard, 50, 36) == 0.0 && at(&hard, 50, 10) == 0.0, "nothing past it"); + // A soft brush fades from the centre line to the rim. + let soft = brush_surface(&[(10.0, 25.0), (90.0, 25.0)], 20.0, 0.0, canvas).expect("painted"); + let (centre, mid, rim) = (at(&soft, 50, 25), at(&soft, 50, 30), at(&soft, 50, 34)); + assert!(centre > 0.95 && mid > 0.2 && mid < 0.8 && rim < 0.1, "{centre} {mid} {rim}"); + // A single point is one dab; clipped to the canvas. + let dab = brush_surface(&[(0.0, 0.0)], 10.0, 100.0, canvas).expect("painted"); + let b = dab.content_bounds(); + assert_eq!((b.x0, b.y0), (0, 0)); + assert!(at(&dab, 1, 1) > 0.99); + } + + #[test] + fn brush_surface_paints_nothing_for_hostile_strokes() { + let canvas = Rect::new(0, 0, 100, 50); + assert!(brush_surface(&[], 10.0, 100.0, canvas).is_none()); + assert!(brush_surface(&[(f32::NAN, 1.0)], 10.0, 100.0, canvas).is_none()); + assert!(brush_surface(&[(1.0, 1.0)], f32::INFINITY, 100.0, canvas).is_none()); + assert!(brush_surface(&[(1.0, 1.0)], 10.0, f32::NAN, canvas).is_none()); + assert!(brush_surface(&[(-500.0, -500.0)], 10.0, 100.0, canvas).is_none(), "off the canvas"); + assert!(brush_surface(&[(0.0, 0.0), (1e7, 1e7)], 5000.0, 100.0, canvas).is_none(), "sweeps too much"); + // Sizes and hardness outside their ranges are clamped, not rejected. + assert!(brush_surface(&[(50.0, 25.0)], 0.0, 500.0, canvas).is_some()); + } + /// A 1 px stroke zigzagging across 2 million px stays within [`MAX_SWEPT_AREA`] yet splits + /// into over 100 million pieces: painting it must not collect them all first (it allocated + /// about 2 GB up front; the #8783 review). + #[test] + fn a_long_thin_zigzag_paints_without_collecting_its_pieces() { + let canvas = Rect::new(0, 0, 64, 64); + let points: Vec<(f32, f32)> = (0..135).map(|i| (if i % 2 == 0 { 0.0 } else { 2_000_000.0 }, 32.0 + (i % 3) as f32)).collect(); + assert!(stroke_area(&points, 1.0) <= MAX_SWEPT_AREA); + // Only the pieces within reach of the canvas are made. + assert!(segments(&points, 0.5, canvas).count() < 134 * 40, "{}", segments(&points, 0.5, canvas).count()); + let r = brush_surface(&points, 1.0, 100.0, canvas).expect("it paints its row"); + assert!(at(&r, 10, 32) > 0.5); + let strokes = [EdgeStroke { points, size: 1.0, add: true }]; + assert!(EdgeBrush::new(&strokes, canvas).is_some_and(|b| b.at(10, 32) > 0.5)); + } + + /// A 1 px diagonal across a 100,000 px canvas sweeps about 141,000 px, well within + /// [`MAX_SWEPT_AREA`], but its bounding box is 10 billion pixels: painting it must cost the + /// tiles it crosses, not that box (it allocated 40 GB; the #8783 review). + #[test] + fn a_thin_diagonal_across_a_huge_canvas_paints_only_its_tiles() { + let canvas = Rect::new(0, 0, 100_000, 100_000); + let diagonal = [(0.0, 0.0), (99_999.0, 99_999.0)]; + assert!(stroke_area(&diagonal, 1.0) <= MAX_SWEPT_AREA); + let tiles = painted_tiles(&diagonal, 1.0, canvas, MAX_PAINTED_TILES).expect("within the limit"); + assert!(tiles.len() < 1500, "{}", tiles.len()); + let s = brush_surface(&diagonal, 1.0, 100.0, canvas).expect("painted"); + assert!(s.tile_count() <= tiles.len()); + assert!(at(&s, 50_000, 50_000) > 0.5 && at(&s, 50_000, 40_000) == 0.0); + let strokes = [EdgeStroke { points: diagonal.to_vec(), size: 1.0, add: true }]; + let b = EdgeBrush::new(&strokes, canvas).expect("painted"); + assert!(b.at(50_000, 50_000) > 0.5 && b.at(50_000, 40_000) == 0.0); + assert!(b.read(Rect::new(49_990, 49_990, 50_010, 50_010)).iter().any(|v| *v > 0.5)); + // A zigzag crossing more tiles than the limit is refused rather than allocated. + let zigzag: Vec<(f32, f32)> = (0..20).map(|i| (if i % 2 == 0 { 0.0 } else { 99_999.0 }, i as f32 * 5000.0)).collect(); + assert!(stroke_area(&zigzag, 1.0) <= MAX_SWEPT_AREA); + assert!(painted_tiles(&zigzag, 1.0, canvas, MAX_PAINTED_TILES).is_none()); + assert!(brush_surface(&zigzag, 1.0, 100.0, canvas).is_none()); + assert!(EdgeBrush::new(&[EdgeStroke { points: zigzag, size: 1.0, add: true }], canvas).is_none()); + } } diff --git a/crates/algo/src/oil.rs b/crates/algo/src/oil.rs index 5a40b056a6..a894efc2ca 100644 --- a/crates/algo/src/oil.rs +++ b/crates/algo/src/oil.rs @@ -29,6 +29,24 @@ pub(crate) struct OilSpec { fn stroke_len(stylization: f32) -> f32 { 1.5 + stylization.clamp(0.1, 10.0) * 2.0 } + +/// `f32::round` (half away from zero) as an integer, without the `roundf` call `f32::round` +/// makes on the baseline x86-64 target. The sum is taken in `f64`, where `v ± 0.5` is exact for +/// every `f32` the cast does not saturate (an `f32` sum is not: `0.49999997 + 0.5` rounds up to +/// `1.0`), so the result equals `v.round() as i32` for every `f32`, NaN (0) and the saturating +/// ends included. +#[inline] +fn round_i32(v: f32) -> i32 { + let v = f64::from(v); + (v + 0.5f64.copysign(v)) as i32 +} + +/// [`round_i32`] clamped into `0..n`. +#[inline] +fn round_index(v: f32, n: usize) -> usize { + round_i32(v).clamp(0, n as i32 - 1) as usize +} + fn tensor_sigma(scale: f32) -> f32 { 0.8 + scale.clamp(0.1, 10.0) * 0.6 } @@ -93,7 +111,7 @@ pub(crate) fn oil_paint(src: &Image, out: Rect, ctx: &Ctx, spec: &OilSpec) -> Ve let sample = |x: f32, y: f32, acc: &mut [f32], k: f32| { let x = x.clamp(0.0, (ww - 1) as f32); let y = y.clamp(0.0, (wh - 1) as f32); - let (x0, y0) = (x.floor() as usize, y.floor() as usize); + let (x0, y0) = (x as usize, y as usize); let (x1, y1) = ((x0 + 1).min(ww - 1), (y0 + 1).min(wh - 1)); let (ax, ay) = (x - x0 as f32, y - y0 as f32); let w00 = (1.0 - ax) * (1.0 - ay) * k; @@ -119,7 +137,7 @@ pub(crate) fn oil_paint(src: &Image, out: Rect, ctx: &Ctx, spec: &OilSpec) -> Ve let (mut qx, mut qy) = (dx, dy); let (mut px, mut py) = (0.0f32, 0.0f32); for k in 1..=steps { - let ti = (qy.round().clamp(0.0, (wh - 1) as f32) as usize) * ww + qx.round().clamp(0.0, (ww - 1) as f32) as usize; + let ti = round_index(qy, wh) * ww + round_index(qx, ww); let (mut tx, mut ty) = tangent[ti]; if k == 1 { tx *= dir; @@ -135,7 +153,7 @@ pub(crate) fn oil_paint(src: &Image, out: Rect, ctx: &Ctx, spec: &OilSpec) -> Ve let wk = 1.0 - k as f32 / (steps + 1) as f32; sample(qx, qy, &mut acc[..n], wk); wsum += wk; - nsum += wk * hash01(win.x0 + qx.round() as i32, win.y0 + qy.round() as i32, 71, 0); + nsum += wk * hash01(win.x0 + round_i32(qx), win.y0 + round_i32(qy), 71, 0); } } let o = (ry * rw + rx) * n; @@ -197,3 +215,59 @@ pub(crate) fn oil_paint(src: &Image, out: Rect, ctx: &Ctx, spec: &OilSpec) -> Ve } res } + +#[cfg(test)] +mod tests { + use super::{round_i32, round_index}; + + /// The LIC looks up tangents and bristle noise by rounded sample position; the replacements + /// for `f32::round` must agree with it over every position a tile can produce, and agree with + /// `f32::clamp` at the ends, including non-finite input. + #[test] + fn rounding_matches_the_standard_library() { + let mut v = -40.0f32; + while v <= 2100.0 { + assert_eq!(round_i32(v), v.round() as i32, "round_i32({v})"); + for n in [1usize, 2, 33, 256, 2048] { + let want = (v.round().clamp(0.0, (n - 1) as f32)) as usize; + assert_eq!(round_index(v, n), want, "round_index({v}, {n})"); + } + v += 0.25; + } + // The binade crossings where an `f32` sum `v + 0.5` rounds up (0.5 - 2^-25), the ties, and + // the largest non-integer `f32`. + let below_half = f32::from_bits(0x3eff_ffff); + for v in [below_half, -below_half, 0.5, -0.5, 1.5, -1.5, 2.5, -2.5, 8_388_607.5, -8_388_607.5, 8_388_608.0, 3.0e9, -3.0e9] { + assert_eq!(round_i32(v), v.round() as i32, "round_i32({v:e})"); + for n in [1usize, 2, 33] { + let want = (v.round().clamp(0.0, (n - 1) as f32)) as usize; + assert_eq!(round_index(v, n), want, "round_index({v:e}, {n})"); + } + } + assert_eq!(round_i32(f32::NAN), f32::NAN.round() as i32); + for n in [1usize, 8] { + assert_eq!(round_index(f32::NEG_INFINITY, n), (f32::NEG_INFINITY.clamp(0.0, (n - 1) as f32)) as usize); + assert_eq!(round_index(f32::INFINITY, n), ((n - 1) as f32) as usize); + } + } + + /// Every `f32` bit pattern in [-4096, 4096] (both signs, about 2.3e9 values, split across + /// threads) rounds exactly like `f32::round`. + #[test] + fn rounding_matches_the_standard_library_for_every_f32_up_to_4096() { + use rayon::prelude::*; + let last = 4096.0f32.to_bits(); + let bad = (0..=last >> 20) + .into_par_iter() + .filter_map(|hi| { + let lo = hi << 20; + let hi_end = (lo | 0xf_ffff).min(last); + (lo..=hi_end).find_map(|bits| { + let v = f32::from_bits(bits); + [v, -v].into_iter().find(|v| round_i32(*v) != v.round() as i32) + }) + }) + .min_by(|a, b| a.abs().total_cmp(&b.abs())); + assert_eq!(bad, None, "round_i32 differs from f32::round"); + } +} diff --git a/crates/algo/src/other2.rs b/crates/algo/src/other2.rs index b5a16f06ea..aa76fb814a 100644 --- a/crates/algo/src/other2.rs +++ b/crates/algo/src/other2.rs @@ -8,7 +8,8 @@ use crate::image::Image; use crate::{Ctx, HsbModel}; /// Custom: `Σ kernel·pixel / scale + offset/255` per colour channel (alpha -/// kept). Kernels shorter than 25 entries are zero-padded. +/// kept). Kernels shorter than 25 entries are zero-padded. CMYK samples are +/// ink, so there it runs on `1 - ink` and a positive offset brightens. pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f32, offset: f32) -> Vec { let n = src.ch; let cc = ncol(ctx, n); @@ -16,6 +17,7 @@ pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f (0..25).filter_map(|i| kernel.get(i).copied().filter(|v| *v != 0.0).map(|v| (i as i32 % 5 - 2, i as i32 / 5 - 2, v))).collect(); let s = if scale == 0.0 { 1.0 } else { scale }; let off = offset / 255.0; + let sub = crate::fxutil::subtractive(ctx); let mut res = src.crop(out); if k.is_empty() { return res; @@ -23,8 +25,10 @@ pub(crate) fn custom(src: &Image, out: Rect, ctx: &Ctx, kernel: &[f32], scale: f for (i, px) in res.chunks_exact_mut(n).enumerate() { let (x, y) = xy(out, i); for (c, v) in px.iter_mut().enumerate().take(cc) { - let sum: f32 = k.iter().map(|&(dx, dy, w)| src.get(x + dx, y + dy, c) * w).sum(); - *v = sum / s + off; + // CMYK: convolve brightness (1 - ink), then back to ink. + let lum = |v: f32| if sub { 1.0 - v } else { v }; + let sum: f32 = k.iter().map(|&(dx, dy, w)| lum(src.get(x + dx, y + dy, c)) * w).sum(); + *v = lum(sum / s + off); } } res @@ -120,6 +124,179 @@ pub(crate) fn hsb_hsl(src: &Image, out: Rect, ctx: &Ctx, input: HsbModel, output #[cfg(test)] mod tests { use super::*; + use photocraft_color::{PixelFormat, SampleType}; + use photocraft_raster::Surface; + + const DEPTHS: [SampleType; 3] = [SampleType::U8, SampleType::U16, SampleType::F32]; + + fn custom_fixture(depth: SampleType, mode: ColorMode, alpha: bool) -> (Surface, Rect) { + let b = Rect::new(-2, -1, 3, 4); + let format = PixelFormat::new(mode, depth, alpha); + let mut src = Surface::new(format); + let mut pixels = Vec::new(); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + for c in 0..format.channels() { + pixels.push(((x - b.x0) + 5 * (y - b.y0) + 3 * c as i32) as f32 / 40.0); + } + } + } + src.write_region(b, &pixels); + (src, b) + } + + fn assert_sample(actual: f32, expected: f32, depth: SampleType) { + let (expected, tolerance) = match depth { + SampleType::U8 => (expected.clamp(0.0, 1.0), 1.0 / 255.0), + SampleType::U16 => (expected.clamp(0.0, 1.0), 1.0 / 65535.0), + SampleType::F32 => (expected, 1e-6), + }; + assert!((actual - expected).abs() <= tolerance, "{depth:?}: {actual} != {expected}"); + } + + fn run_custom(src: &Surface, b: Rect, kernel: Vec, scale: f32, offset: f32) -> Surface { + crate::apply_in(src, &crate::FilterParams::Custom { kernel, scale, offset }, b, b, None, b) + } + + #[test] + fn custom_all_25_taps_use_row_major_offsets_and_repeat_canvas_edges() { + for depth in DEPTHS { + let (src, b) = custom_fixture(depth, ColorMode::Rgb, true); + for tap in 0..25 { + let mut kernel = vec![0.0; 25]; + kernel[tap] = 1.0; + let out = run_custom(&src, b, kernel, 1.0, 0.0); + let (dx, dy) = (tap as i32 % 5 - 2, tap as i32 / 5 - 2); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let expected = src.pixel((x + dx).clamp(b.x0, b.x1 - 1), (y + dy).clamp(b.y0, b.y1 - 1)); + let actual = out.pixel(x, y); + for c in 0..3 { + assert_sample(actual[c], expected[c], depth); + } + assert_eq!(actual[3], src.pixel(x, y)[3], "tap={tap}, alpha must not shift"); + } + } + } + } + } + + #[test] + fn custom_short_kernel_zero_pads_and_ignores_entries_after_25() { + for depth in DEPTHS { + let (src, b) = custom_fixture(depth, ColorMode::Rgb, true); + let mut short = vec![0.0; 13]; + short[0] = 0.25; + short[12] = 0.75; + let mut padded = short.clone(); + padded.resize(25, 0.0); + let mut long = padded.clone(); + long.extend([1e6, -1e6, f32::NAN]); + let out = run_custom(&src, b, short, 1.0, 0.0); + assert_eq!(out.read_region(b), run_custom(&src, b, padded, 1.0, 0.0).read_region(b)); + assert_eq!(out.read_region(b), run_custom(&src, b, long, 1.0, 0.0).read_region(b)); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let corner = src.pixel((x - 2).max(b.x0), (y - 2).max(b.y0)); + let centre = src.pixel(x, y); + let actual = out.pixel(x, y); + for c in 0..3 { + assert_sample(actual[c], 0.25 * corner[c] + 0.75 * centre[c], depth); + } + } + } + } + } + + #[test] + fn custom_scale_zero_is_one_and_offset_is_in_255ths() { + for depth in DEPTHS { + let (src, b) = custom_fixture(depth, ColorMode::Rgb, true); + let before = src.read_region(b); + let mut kernel = vec![0.0; 25]; + kernel[12] = 2.0; + let out = run_custom(&src, b, kernel.clone(), 0.0, -51.0); + assert_eq!(out.read_region(b), run_custom(&src, b, kernel, 1.0, -51.0).read_region(b)); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let original = src.pixel(x, y); + let actual = out.pixel(x, y); + for c in 0..3 { + assert_sample(actual[c], 2.0 * original[c] - 0.2, depth); + } + assert_eq!(actual[3], original[3]); + } + } + assert_eq!(src.read_region(b), before); + } + } + + #[test] + fn custom_neighbourhood_convolution_preserves_each_pixels_alpha() { + for depth in DEPTHS { + for mode in [ColorMode::Rgb, ColorMode::Cmyk] { + let (src, b) = custom_fixture(depth, mode, true); + let out = run_custom(&src, b, vec![1.0; 25], 25.0, 0.0); + let alpha = src.channels() - 1; + assert_ne!(out.pixel(b.x0, b.y0)[0], src.pixel(b.x0, b.y0)[0], "fixture must change colour"); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + assert_eq!(out.pixel(x, y)[alpha], src.pixel(x, y)[alpha], "{depth:?}, {mode:?}, {x},{y}"); + } + } + } + } + } + + #[test] + fn custom_cmyk_convolves_brightness_including_black_with_or_without_alpha() { + for depth in DEPTHS { + for alpha in [false, true] { + let (src, b) = custom_fixture(depth, ColorMode::Cmyk, alpha); + for offset in [-51.0, 51.0] { + let mut kernel = vec![0.0; 25]; + kernel[12] = 2.0; + let out = run_custom(&src, b, kernel, 1.0, offset); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let original = src.pixel(x, y); + let actual = out.pixel(x, y); + for c in 0..4 { + // Twice the reflected brightness, plus offset, then back to ink. + assert_sample(actual[c], 2.0 * original[c] - 1.0 - offset / 255.0, depth); + } + if alpha { + assert_eq!(actual[4], original[4]); + } + } + } + } + } + } + } + + #[test] + fn custom_float_retains_hdr_while_integer_storage_clamps() { + let b = Rect::new(0, 0, 1, 1); + for depth in DEPTHS { + let mut src = Surface::new(PixelFormat::new(ColorMode::Rgb, depth, true)); + src.fill_rect(b, &[0.0, 0.5, 1.0, 0.4]); + let mut kernel = vec![0.0; 25]; + kernel[12] = 2.0; + let out = run_custom(&src, b, kernel, 1.0, -51.0); + let actual = out.pixel(0, 0); + let original = src.pixel(0, 0); + for c in 0..3 { + assert_sample(actual[c], 2.0 * original[c] - 0.2, depth); + } + assert_eq!(actual[3], original[3]); + if depth == SampleType::F32 { + assert!(actual[0] < 0.0 && actual[2] > 1.0); + } else { + assert_eq!((actual[0], actual[2]), (0.0, 1.0)); + } + } + } #[test] fn hsb_hsl_round_trip() { diff --git a/crates/algo/src/paint.rs b/crates/algo/src/paint.rs index c32f28c648..4862304786 100644 --- a/crates/algo/src/paint.rs +++ b/crates/algo/src/paint.rs @@ -5,6 +5,14 @@ use photocraft_geom::Rect; use photocraft_raster::{Surface, from_rgba_into}; use serde::{Deserialize, Serialize}; +/// How a fill lays down its colour: blended with a mode, or Clear, which erases what it covers +/// (the Paint Bucket's Mode › Clear). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FillMode { + Blend(BlendMode), + Clear, +} + /// Gradient tool shape. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Default)] #[serde(rename_all = "camelCase")] @@ -59,7 +67,7 @@ pub fn sample_stops(stops: &[(f32, [f32; 4])], t: f32) -> [f32; 4] { fn composite_area( s: &mut Surface, area: Rect, - blend_mode: BlendMode, + mode: FillMode, coverage: impl Fn(i32, i32) -> f32 + Sync, color: impl Fn(i32, i32) -> [f32; 4] + Sync, lock_transparency: bool, @@ -89,15 +97,18 @@ fn composite_area( let i = ((y - r.y0) as usize * w + (x - r.x0) as usize) * n; let px = &mut region[i..i + n]; let d = photocraft_raster::to_rgba(&fmt, px); - let mut o = blend::composite(blend_mode, d, color(x, y), k); - if lock_transparency { - o[3] = d[3]; - } + let o = match mode { + FillMode::Blend(m) if lock_transparency => blend::composite_atop(m, d, color(x, y), k), + FillMode::Blend(m) => blend::composite(m, d, color(x, y), k), + // Locked transparency keeps the alpha, so Clear leaves the pixel as it is. + FillMode::Clear if lock_transparency => d, + FillMode::Clear => [d[0], d[1], d[2], d[3] * (1.0 - k.min(1.0))], + }; from_rgba_into(&fmt, o, &mut enc); // A colour picked as CMYK inks replaces fully covered pixels of a CMYK target as it is. if let Some(i) = inks && fmt.mode == photocraft_color::ColorMode::Cmyk - && blend_mode == BlendMode::Normal + && mode == FillMode::Blend(BlendMode::Normal) && k >= 1.0 { enc[..4].copy_from_slice(&i); @@ -138,7 +149,7 @@ pub fn paint_gradient( composite_area( s, area, - blend_mode, + FillMode::Blend(blend_mode), |x, y| opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), |x, y| { let t = tool_gradient_t(shape, from, to, x as f32 + 0.5, y as f32 + 0.5); @@ -190,11 +201,12 @@ pub fn bucket_fill_src( lock_transparency: bool, src: impl Fn(i32, i32) -> [f32; 4] + Sync, ) -> bool { - bucket_fill_inks(s, area, seed, tolerance, contiguous, anti_alias, opacity, selection, lock_transparency, src, None) + bucket_fill_inks(s, area, seed, tolerance, contiguous, anti_alias, opacity, selection, lock_transparency, src, None, FillMode::Blend(BlendMode::Normal)) } /// [`bucket_fill_src`] where `inks` (CMYK, 0..1) are what a fully covered pixel of a CMYK target -/// gets instead of `src`'s sRGB stand-in (a colour that was picked as CMYK). +/// gets instead of `src`'s sRGB stand-in (a colour that was picked as CMYK), laid down with `mode` +/// (the options bar's Mode). #[allow(clippy::too_many_arguments)] pub fn bucket_fill_inks( s: &mut Surface, @@ -208,6 +220,7 @@ pub fn bucket_fill_inks( lock_transparency: bool, src: impl Fn(i32, i32) -> [f32; 4] + Sync, inks: Option<[f32; 4]>, + mode: FillMode, ) -> bool { if !area.contains(seed.0, seed.1) { return false; @@ -215,7 +228,7 @@ pub fn bucket_fill_inks( let img = crate::selection::rgba8_image(s, area); let Some(region) = crate::selection::wand_region(&img, area, seed, tolerance, contiguous, anti_alias) else { return false }; drop(img); - bucket_fill_region_inks(s, ®ion, opacity, selection, lock_transparency, src, inks); + bucket_fill_region_inks(s, ®ion, opacity, selection, lock_transparency, src, inks, mode); true } @@ -229,10 +242,11 @@ pub fn bucket_fill_region_src( lock_transparency: bool, src: impl Fn(i32, i32) -> [f32; 4] + Sync, ) { - bucket_fill_region_inks(s, region, opacity, selection, lock_transparency, src, None); + bucket_fill_region_inks(s, region, opacity, selection, lock_transparency, src, None, FillMode::Blend(BlendMode::Normal)); } -/// [`bucket_fill_region_src`] with the CMYK `inks` of [`bucket_fill_inks`]. +/// [`bucket_fill_region_src`] with the CMYK `inks` and the `mode` of [`bucket_fill_inks`]. +#[allow(clippy::too_many_arguments)] pub fn bucket_fill_region_inks( s: &mut Surface, region: &crate::selection::Region, @@ -241,18 +255,11 @@ pub fn bucket_fill_region_inks( lock_transparency: bool, src: impl Fn(i32, i32) -> [f32; 4] + Sync, inks: Option<[f32; 4]>, + mode: FillMode, ) { // Composite only over the filled region's box. let b = region.bbox; - composite_area( - s, - b, - BlendMode::Normal, - |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), - src, - lock_transparency, - inks, - ); + composite_area(s, b, mode, |x, y| region.at(x, y) * opacity * selection.map_or(1.0, |m| m.sample_channel(x, y, 0)), src, lock_transparency, inks); } #[cfg(test)] @@ -296,6 +303,22 @@ mod tests { assert_eq!(s.pixel(5, 1)[3], 0.0, "outside the selection"); } + #[test] + fn locked_gradient_blends_source_atop_on_semi_transparent_pixels() { + // Locked half-transparent white, flat black gradient at 50%: colour halfway, alpha kept (#5783). + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for mode in [BlendMode::Normal, BlendMode::Multiply] { + let mut s = Surface::new(fmt); + let a = Rect::new(0, 0, 4, 1); + s.fill_rect(a, &[1.0, 1.0, 1.0, 0.5]); + let black = [0.0, 0.0, 0.0, 1.0]; + paint_gradient(&mut s, a, (0.0, 0.0), (4.0, 0.0), GradientShape::Linear, &[(0.0, black), (1.0, black)], false, 0.5, mode, false, None, true); + let p = s.rgba(2, 0); + assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{fmt:?} {mode:?} {p:?}"); + } + } + } + #[test] fn bucket_fills_contiguous_region() { let mut s = Surface::new(PixelFormat::RGBA8); @@ -320,4 +343,24 @@ mod tests { assert_eq!(s.rgba(-1, 4), [0.0; 4], "outside the selection"); assert_eq!(s.rgba(-4, 4), [0.0; 4], "outside the region"); } + + /// The Paint Bucket's Mode: Multiply darkens what's there, Clear erases it (half at 50 %). + #[test] + fn bucket_mode_blends_or_clears() { + let a = Rect::new(0, 0, 4, 1); + let grey = [0.5, 0.5, 0.5, 1.0]; + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let mut s = Surface::new(fmt); + s.fill_rect(a, &grey); + let red = |_, _| [1.0, 0.0, 0.0, 1.0]; + assert!(bucket_fill_inks(&mut s, a, (0, 0), 0.0, true, false, 1.0, None, false, red, None, FillMode::Blend(BlendMode::Multiply))); + let px = s.rgba(1, 0); + assert!((px[0] - 0.5).abs() < 0.01 && px[1] < 0.01 && (px[3] - 1.0).abs() < 1e-3, "{fmt:?} multiply: {px:?}"); + assert!(bucket_fill_inks(&mut s, a, (0, 0), 0.0, true, false, 0.5, None, false, red, None, FillMode::Clear)); + assert!((s.rgba(1, 0)[3] - 0.5).abs() < 0.01, "{fmt:?} clear at 50 %"); + // Locked transparency keeps the alpha, so Clear does nothing. + assert!(bucket_fill_inks(&mut s, a, (0, 0), 0.0, true, false, 1.0, None, true, red, None, FillMode::Clear)); + assert!((s.rgba(1, 0)[3] - 0.5).abs() < 0.01, "{fmt:?} locked"); + } + } } diff --git a/crates/algo/src/panorama.rs b/crates/algo/src/panorama.rs index fd7ccbc591..dc44f06c1f 100644 --- a/crates/algo/src/panorama.rs +++ b/crates/algo/src/panorama.rs @@ -35,9 +35,15 @@ #![allow(clippy::needless_range_loop)] // index loops mirror the maths (pixels × channels) use crate::features::{self, Descriptor, Model}; -use crate::photo_util::{Normal, Rng, par_map, solve_dense}; +use crate::photo_util::{Normal, Rng, par_map, par_rows, solve_dense}; use crate::transform::Homography; +mod blend; + +pub use blend::{Geometry, Photometric, RoiImage, blend_levels, multiband, photometric, seam_labels}; +#[cfg(test)] +use blend::{expand, multiband_batched, push_pull, reduce}; + /// Photomerge layouts. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Layout { @@ -720,506 +726,6 @@ fn align_layout(feats: &[ImageFeatures], matches: &[PairMatch], opts: &AlignOpti Some(Alignment { layout, reference, placements, focal, k1, pairs: edges.iter().map(|e| (e.0, e.1, e.2)).collect(), rms }) } -// ---------- photometric ---------- - -/// One image warped into the panorama: an axis-aligned region with colour channels and coverage. -#[derive(Clone, Debug)] -pub struct RoiImage { - pub x0: i32, - pub y0: i32, - pub w: usize, - pub h: usize, - /// Colour channels per pixel (no alpha). - pub ch: usize, - pub px: Vec, - /// Coverage `0..=1`. - pub alpha: Vec, -} - -impl RoiImage { - fn at(&self, x: i32, y: i32) -> Option { - let (lx, ly) = (x - self.x0, y - self.y0); - (lx >= 0 && ly >= 0 && (lx as usize) < self.w && (ly as usize) < self.h).then(|| ly as usize * self.w + lx as usize) - } -} - -/// Placements and source sizes (for the vignetting radius). -pub type Geometry<'a> = (&'a [Placement], &'a [(usize, usize)]); - -/// Gains (per image, per channel) and the vignetting model found by [`photometric`]. -#[derive(Clone, Debug, PartialEq)] -pub struct Photometric { - pub gains: Vec>, - /// `log V(r) = a·r² + b·r⁴`, `r` = distance from the image centre over the half diagonal. - pub vignette: [f64; 2], -} - -impl Photometric { - /// Multiplier that corrects a pixel of image `i` at normalised squared radius `r2`. - pub fn factor(&self, i: usize, c: usize, r2: f64) -> f32 { - let v = (self.vignette[0] * r2 + self.vignette[1] * r2 * r2).exp(); - (1.0 / (self.gains[i][c] as f64 * v)) as f32 - } -} - -/// Estimates exposure gains (`gain`) and a shared vignetting falloff (`vignette`) from the -/// overlaps of `imgs` (`placements`/`sizes` give each panorama pixel's source radius). -/// Without `geometry` (placements and source sizes) the vignetting model is not estimated. -pub fn photometric(imgs: &[RoiImage], geometry: Option, gain: bool, vignette: bool) -> Photometric { - let vignette = vignette && geometry.is_some(); - let n = imgs.len(); - let ch = imgs.first().map_or(1, |i| i.ch); - let identity = Photometric { gains: vec![vec![1.0; ch]; n], vignette: [0.0, 0.0] }; - if n == 0 || (!gain && !vignette) { - return identity; - } - // Overlap samples on a grid: (a, b, colours, radii²). - let (mut x0, mut y0, mut x1, mut y1) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN); - for im in imgs { - x0 = x0.min(im.x0); - y0 = y0.min(im.y0); - x1 = x1.max(im.x0 + im.w as i32); - y1 = y1.max(im.y0 + im.h as i32); - } - let step = (((x1 - x0) as i64 * (y1 - y0) as i64 / 40_000).max(1) as f64).sqrt().ceil() as i32; - let r2_of = |i: usize, u: i32, v: i32| -> Option { - let Some((placements, sizes)) = geometry else { return Some(0.0) }; - let (sx, sy) = placements[i].inverse(u as f64 + 0.5, v as f64 + 0.5)?; - let (w, h) = (sizes[i].0 as f64, sizes[i].1 as f64); - let (dx, dy) = (sx - w / 2.0, sy - h / 2.0); - Some((dx * dx + dy * dy) / (w * w / 4.0 + h * h / 4.0)) - }; - type Sample = (usize, usize, Vec, Vec, f64, f64); - let mut samples: Vec = Vec::new(); - let mut y = y0; - while y < y1 { - let mut x = x0; - while x < x1 { - let cover: Vec<(usize, usize)> = (0..n).filter_map(|i| imgs[i].at(x, y).filter(|&k| imgs[i].alpha[k] > 0.999).map(|k| (i, k))).collect(); - for p in 0..cover.len() { - for q in p + 1..cover.len() { - let ((a, ka), (b, kb)) = (cover[p], cover[q]); - let ca = imgs[a].px[ka * ch..(ka + 1) * ch].to_vec(); - let cb = imgs[b].px[kb * ch..(kb + 1) * ch].to_vec(); - if ca.iter().chain(&cb).any(|v| !(0.02..=0.98).contains(v)) { - continue; - } - // Disagreeing content (parallax, moving objects) says nothing about exposure. - if ca.iter().zip(&cb).any(|(x, y)| (x / y).ln().abs() > 1.0) { - continue; - } - let (Some(ra), Some(rb)) = (r2_of(a, x, y), r2_of(b, x, y)) else { continue }; - samples.push((a, b, ca, cb, ra, rb)); - } - } - x += step; - } - y += step; - } - if samples.len() < 20 { - return identity; - } - // Unknowns: log gain per image (per channel), then a, b (shared, from mean log intensity). - let sigma_n = 0.05; - let sigma_g = if gain { 0.15 } else { 1e-4 }; - let mut out = identity.clone(); - let nv = if vignette { 2 } else { 0 }; - // Vignetting is solved on the channel mean, then gains per channel with it fixed. - let mut ne = Normal::new(n + nv); - for (a, b, ca, cb, ra, rb) in &samples { - let la = ca.iter().map(|v| (*v as f64).ln()).sum::() / ch as f64; - let lb = cb.iter().map(|v| (*v as f64).ln()).sum::() / ch as f64; - let mut row = vec![(*a, 1.0), (*b, -1.0)]; - if vignette { - row.push((n, ra - rb)); - row.push((n + 1, ra * ra - rb * rb)); - } - ne.add(&row, la - lb, 1.0 / sigma_n); - } - for i in 0..n { - ne.add(&[(i, 1.0)], 0.0, 1.0 / sigma_g); - } - if vignette { - ne.add(&[(n, 1.0)], 0.0, 0.5); - ne.add(&[(n + 1, 1.0)], 0.0, 0.5); - } - let Some(sol) = ne.solve() else { return out }; - if vignette { - // Vignetting only darkens; clamp a runaway brightening fit. - out.vignette = [sol[n].clamp(-2.0, 0.5), sol[n + 1].clamp(-2.0, 2.0)]; - } - if gain { - for c in 0..ch { - let mut ne = Normal::new(n); - for (a, b, ca, cb, ra, rb) in &samples { - let va = out.vignette[0] * ra + out.vignette[1] * ra * ra; - let vb = out.vignette[0] * rb + out.vignette[1] * rb * rb; - ne.add(&[(*a, 1.0), (*b, -1.0)], ((ca[c] as f64).ln() - va) - ((cb[c] as f64).ln() - vb), 1.0 / sigma_n); - } - for i in 0..n { - ne.add(&[(i, 1.0)], 0.0, 1.0 / sigma_g); - } - if let Some(g) = ne.solve() { - for i in 0..n { - out.gains[i][c] = g[i].exp().clamp(0.2, 5.0) as f32; - } - } - } - } - out -} - -// ---------- seams ---------- - -/// Seam labels on a `w × h` grid (`-1` = no image): images are added in `order`; each new image -/// takes the part of its overlap with the composite that lies on its side of the cheapest path -/// through the overlap (cost = colour difference). -pub fn seam_labels(w: usize, h: usize, luma: &[Vec], cover: &[Vec], order: &[usize]) -> Vec { - let mut label = vec![-1i32; w * h]; - for &img in order { - let cov = &cover[img]; - let mut overlap = vec![false; w * h]; - let (mut sx, mut sy, mut no) = (0.0f64, 0.0f64, 0usize); - let (mut nx, mut ny, mut nn) = (0.0f64, 0.0f64, 0usize); - for i in 0..w * h { - if !cov[i] { - continue; - } - nx += (i % w) as f64; - ny += (i / w) as f64; - nn += 1; - if label[i] >= 0 { - overlap[i] = true; - sx += (i % w) as f64; - sy += (i / w) as f64; - no += 1; - } else { - label[i] = img as i32; - } - } - if no == 0 || nn == no { - continue; - } - let (ox, oy) = (sx / no as f64, sy / no as f64); - let (cx, cy) = (nx / nn as f64, ny / nn as f64); - let vertical = (cx - ox).abs() >= (cy - oy).abs(); - // Image on the increasing side along the seam's cross axis? - let positive = if vertical { cx > ox } else { cy > oy }; - let cost = |i: usize| -> f32 { 0.002 + (luma[img][i] - luma[label[i] as usize][i]).abs() }; - let (lines, len) = if vertical { (h, w) } else { (w, h) }; - let idx = |line: usize, k: usize| if vertical { line * w + k } else { k * w + line }; - // DP over lines (rows for a vertical seam), within the overlap. - let mut acc = vec![f32::INFINITY; w * h]; - let mut from = vec![usize::MAX; w * h]; - let mut prev_line: Option = None; - for line in 0..lines { - let any = (0..len).any(|k| overlap[idx(line, k)]); - if !any { - continue; - } - for k in 0..len { - let i = idx(line, k); - if !overlap[i] { - continue; - } - let mut best = (0.0f32, usize::MAX); - if let Some(pl) = prev_line.filter(|pl| *pl + 1 == line) { - best = (f32::INFINITY, usize::MAX); - for dk in [-1i64, 0, 1] { - let kk = k as i64 + dk; - if kk < 0 || kk >= len as i64 { - continue; - } - let j = idx(pl, kk as usize); - if acc[j] < best.0 { - best = (acc[j], kk as usize); - } - } - if best.1 == usize::MAX { - best = (0.0, usize::MAX); - } - } - acc[i] = best.0 + cost(i); - from[i] = best.1; - } - prev_line = Some(line); - } - // Backtrack from the cheapest end; lines not reached keep their own best cell. - let mut seam = vec![usize::MAX; lines]; - let mut line = lines; - while line > 0 { - line -= 1; - if seam[line] != usize::MAX { - continue; - } - let Some(k) = (0..len).filter(|&k| overlap[idx(line, k)]).min_by(|&a, &b| acc[idx(line, a)].total_cmp(&acc[idx(line, b)])) else { continue }; - let (mut l, mut k) = (line, k); - loop { - seam[l] = k; - let f = from[idx(l, k)]; - if f == usize::MAX || l == 0 { - break; - } - l -= 1; - if seam[l] != usize::MAX { - break; - } - k = f; - } - } - for line in 0..lines { - let s = seam[line]; - if s == usize::MAX { - continue; - } - for k in 0..len { - let i = idx(line, k); - if overlap[i] && ((positive && k > s) || (!positive && k < s)) { - label[i] = img as i32; - } - } - } - } - label -} - -// ---------- multi-band blending ---------- - -/// Number of pyramid levels used by [`multiband`] for a seam mask of this size. -pub fn blend_levels(w: usize, h: usize) -> usize { - let mut s = w.min(h); - let mut n = 1; - while s > 16 && n < 7 { - s /= 2; - n += 1; - } - n -} - -fn reduce(w: usize, h: usize, ch: usize, src: &[f32]) -> (usize, usize, Vec) { - let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); - let k = [1.0f32, 4.0, 6.0, 4.0, 1.0]; - // Separable 5-tap binomial, then decimate. - let mut tmp = vec![0.0f32; nw * h * ch]; - for y in 0..h { - for x in 0..nw { - for c in 0..ch { - let mut s = 0.0; - for (t, kv) in k.iter().enumerate() { - let xx = (2 * x as i64 + t as i64 - 2).clamp(0, w as i64 - 1) as usize; - s += kv * src[(y * w + xx) * ch + c]; - } - tmp[(y * nw + x) * ch + c] = s / 16.0; - } - } - } - let mut out = vec![0.0f32; nw * nh * ch]; - for y in 0..nh { - for x in 0..nw { - for c in 0..ch { - let mut s = 0.0; - for (t, kv) in k.iter().enumerate() { - let yy = (2 * y as i64 + t as i64 - 2).clamp(0, h as i64 - 1) as usize; - s += kv * tmp[(yy * nw + x) * ch + c]; - } - out[(y * nw + x) * ch + c] = s / 16.0; - } - } - } - (nw, nh, out) -} - -fn expand(sw: usize, sh: usize, ch: usize, src: &[f32], w: usize, h: usize) -> Vec { - // Bilinear upsampling at half-pixel-aligned positions. - let mut out = vec![0.0f32; w * h * ch]; - for y in 0..h { - let fy = ((y as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sh - 1) as f32); - let (y0, ty) = (fy.floor() as usize, fy - fy.floor()); - let y1 = (y0 + 1).min(sh - 1); - for x in 0..w { - let fx = ((x as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sw - 1) as f32); - let (x0, tx) = (fx.floor() as usize, fx - fx.floor()); - let x1 = (x0 + 1).min(sw - 1); - for c in 0..ch { - let p = |xx: usize, yy: usize| src[(yy * sw + xx) * ch + c]; - let top = p(x0, y0) + (p(x1, y0) - p(x0, y0)) * tx; - let bot = p(x0, y1) + (p(x1, y1) - p(x0, y1)) * tx; - out[(y * w + x) * ch + c] = top + (bot - top) * ty; - } - } - } - out -} - -/// Fills pixels with zero `wt` from their surroundings (push-pull), so pyramid levels don't -/// pull in black from outside an image. -fn push_pull(w: usize, h: usize, ch: usize, px: &mut [f32], wt: &[f32]) { - if w <= 1 && h <= 1 { - return; - } - let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); - let mut cpx = vec![0.0f32; nw * nh * ch]; - let mut cw = vec![0.0f32; nw * nh]; - for y in 0..h { - for x in 0..w { - let (i, j) = (y * w + x, (y / 2) * nw + x / 2); - let a = wt[i]; - cw[j] += a; - for c in 0..ch { - cpx[j * ch + c] += a * px[i * ch + c]; - } - } - } - for j in 0..nw * nh { - if cw[j] > 0.0 { - for c in 0..ch { - cpx[j * ch + c] /= cw[j]; - } - cw[j] = 1.0; - } - } - if cw.contains(&0.0) && cw.iter().any(|v| *v > 0.0) { - push_pull(nw, nh, ch, &mut cpx, &cw); - } - for y in 0..h { - for x in 0..w { - let i = y * w + x; - if wt[i] < 1.0 { - let j = (y / 2) * nw + x / 2; - let a = wt[i]; - for c in 0..ch { - px[i * ch + c] = a * px[i * ch + c] + (1.0 - a) * cpx[j * ch + c]; - } - } - } - } -} - -/// Multi-band blend of `imgs` into a `w × h` panorama (origin 0,0) with one weight map per image -/// over the same grid as its ROI (`weights[i]` has `w_i × h_i` entries). Returns the colour -/// channels (`ch` per pixel) and the total coverage. -pub fn multiband(w: usize, h: usize, imgs: &[RoiImage], weights: &[Vec], levels: usize) -> (Vec, Vec) { - let ch = imgs.first().map_or(1, |i| i.ch); - let levels = levels.max(1); - let align = 1i32 << (levels - 1); - // Level sizes of the panorama. - let mut sizes = vec![(w, h)]; - for _ in 1..levels { - let Some(&(pw, ph)) = sizes.last() else { break }; - sizes.push((pw.div_ceil(2), ph.div_ceil(2))); - } - let mut acc: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh * ch]).collect(); - let mut wsum: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh]).collect(); - let margin = align * 4; - // Each image on its ROI grown by a margin and aligned to the coarsest level's grid. - let bands = par_map(imgs.len(), |k| { - let im = &imgs[k]; - let gx0 = ((im.x0 - margin).max(0) / align) * align; - let gy0 = ((im.y0 - margin).max(0) / align) * align; - let gx1 = (im.x0 + im.w as i32 + margin).min(w as i32); - let gy1 = (im.y0 + im.h as i32 + margin).min(h as i32); - let (gw, gh) = ((gx1 - gx0).max(1) as usize, (gy1 - gy0).max(1) as usize); - let mut px = vec![0.0f32; gw * gh * ch]; - let mut cov = vec![0.0f32; gw * gh]; - let mut wt = vec![0.0f32; gw * gh]; - for y in 0..im.h { - for x in 0..im.w { - let (gx, gy) = (im.x0 + x as i32 - gx0, im.y0 + y as i32 - gy0); - if gx < 0 || gy < 0 || gx as usize >= gw || gy as usize >= gh { - continue; - } - let (i, g) = (y * im.w + x, gy as usize * gw + gx as usize); - cov[g] = im.alpha[i]; - wt[g] = weights[k][i] * im.alpha[i].min(1.0); - px[g * ch..(g + 1) * ch].copy_from_slice(&im.px[i * ch..(i + 1) * ch]); - } - } - let bin: Vec = cov.iter().map(|a| if *a > 0.5 { 1.0 } else { 0.0 }).collect(); - push_pull(gw, gh, ch, &mut px, &bin); - // Laplacian of the colours, Gaussian of the weights. - let mut lap: Vec<(usize, usize, Vec)> = Vec::new(); - let mut gau: Vec> = Vec::new(); - let (mut cw, mut chh, mut cur, mut cwt) = (gw, gh, px, wt); - for l in 0..levels { - if l + 1 == levels { - lap.push((cw, chh, cur.clone())); - gau.push(cwt.clone()); - break; - } - let (nw, nh, down) = reduce(cw, chh, ch, &cur); - let up = expand(nw, nh, ch, &down, cw, chh); - lap.push((cw, chh, cur.iter().zip(&up).map(|(a, b)| a - b).collect())); - gau.push(cwt.clone()); - let (_, _, wdown) = reduce(cw, chh, 1, &cwt); - cw = nw; - chh = nh; - cur = down; - cwt = wdown; - } - (gx0, gy0, lap, gau) - }); - for (gx0, gy0, lap, gau) in &bands { - for (l, ((lw, lh, band), g)) in lap.iter().zip(gau).enumerate() { - let (ox, oy) = ((gx0 >> l) as usize, (gy0 >> l) as usize); - let (pw, ph) = sizes[l]; - for y in 0..*lh { - let py = oy + y; - if py >= ph { - break; - } - for x in 0..*lw { - let px = ox + x; - if px >= pw { - break; - } - let (i, p) = (y * lw + x, py * pw + px); - let k = g[i]; - if k <= 0.0 { - continue; - } - wsum[l][p] += k; - for c in 0..ch { - acc[l][p * ch + c] += k * band[i * ch + c]; - } - } - } - } - } - for l in 0..levels { - let (pw, ph) = sizes[l]; - for p in 0..pw * ph { - let k = wsum[l][p]; - for c in 0..ch { - acc[l][p * ch + c] = if k > 1e-8 { acc[l][p * ch + c] / k } else { 0.0 }; - } - } - } - // Collapse. - // `levels ≥ 1`, so there is always a coarsest level. - let Some(mut cur) = acc.pop() else { return (vec![0.0; w * h * ch], vec![0.0; w * h]) }; - for l in (0..levels - 1).rev() { - let (pw, ph) = sizes[l]; - let (sw, sh) = sizes[l + 1]; - let up = expand(sw, sh, ch, &cur, pw, ph); - cur = acc[l].iter().zip(&up).map(|(a, b)| a + b).collect(); - } - // Coverage: union of the images. - let mut cov = vec![0.0f32; w * h]; - for im in imgs { - for y in 0..im.h { - for x in 0..im.w { - let (px, py) = (im.x0 + x as i32, im.y0 + y as i32); - if px < 0 || py < 0 || px as usize >= w || py as usize >= h { - continue; - } - let p = py as usize * w + px as usize; - cov[p] = cov[p].max(im.alpha[y * im.w + x]); - } - } - } - (cur, cov) -} - #[cfg(test)] mod tests { use super::*; @@ -1430,4 +936,61 @@ mod tests { assert!(max_jump < 0.1, "max step {max_jump}"); assert!(cov.iter().all(|c| *c == 1.0)); } + + #[test] + fn an_image_wholly_inside_the_composite_still_gets_its_side() { + // #5745: the first image covers the whole 120×40 grid, the second only its right part + // (x ≥ 70), all of it overlap. It used to be skipped, so the first image kept everything. + let (w, h) = (120usize, 40usize); + let luma = vec![vec![0.5f32; w * h], vec![0.5f32; w * h]]; + let cover: Vec> = vec![vec![true; w * h], (0..w * h).map(|i| i % w >= 70).collect()]; + let labels = seam_labels(w, h, &luma, &cover, &[0, 1]); + for y in 0..h { + assert_eq!(labels[y * w + 5], 0, "row {y}"); + assert_eq!(labels[y * w + w - 1], 1, "row {y}"); + // The second image's region is one run at the right of its seam. + let row = &labels[y * w..(y + 1) * w]; + let first = row.iter().position(|l| *l == 1).unwrap(); + assert!(first >= 70 && row[first..].iter().all(|l| *l == 1), "row {y}: {row:?}"); + } + // The same on the left (x < 50) and on top (y < 15): the seam stays inside the overlap + // instead of on the image's outer edge, which used to leave it nothing. + let left: Vec> = vec![vec![true; w * h], (0..w * h).map(|i| i % w < 50).collect()]; + let labels = seam_labels(w, h, &luma, &left, &[0, 1]); + for y in 0..h { + let row = &labels[y * w..(y + 1) * w]; + assert_eq!(row[0], 1, "row {y}: {row:?}"); + assert_eq!(row[w - 1], 0, "row {y}"); + let last = row.iter().rposition(|l| *l == 1).unwrap(); + assert!(last < 50 && row[..=last].iter().all(|l| *l == 1), "row {y}: {row:?}"); + } + let (tw, th) = (40usize, 120usize); + let tluma = vec![vec![0.5f32; tw * th], vec![0.5f32; tw * th]]; + let top: Vec> = vec![vec![true; tw * th], (0..tw * th).map(|i| i / tw < 50).collect()]; + let labels = seam_labels(tw, th, &tluma, &top, &[0, 1]); + for x in 0..tw { + assert_eq!(labels[x], 1, "column {x}"); + assert_eq!(labels[(th - 1) * tw + x], 0, "column {x}"); + } + // One column or row wide: the seam can only run through it, and the image keeps it. + for col in [0, w - 1] { + let strip: Vec> = vec![vec![true; w * h], (0..w * h).map(|i| i % w == col).collect()]; + let labels = seam_labels(w, h, &luma, &strip, &[0, 1]); + assert!(labels.iter().enumerate().all(|(i, l)| *l == i32::from(i % w == col)), "column {col}: {labels:?}"); + } + let strip: Vec> = vec![vec![true; tw * th], (0..tw * th).map(|i| i / tw == 0).collect()]; + let labels = seam_labels(tw, th, &tluma, &strip, &[0, 1]); + assert!(labels.iter().enumerate().all(|(i, l)| *l == i32::from(i / tw == 0)), "{labels:?}"); + // An image centred on the composite has no side: the first image keeps the overlap. + let centred: Vec> = vec![vec![true; w * h], (0..w * h).map(|i| (20..100).contains(&(i % w))).collect()]; + assert!(seam_labels(w, h, &luma, ¢red, &[0, 1]).iter().all(|l| *l == 0)); + // Two images covering the same cells, likewise. + let same = vec![vec![true; w * h]; 2]; + assert!(seam_labels(w, h, &luma, &same, &[0, 1]).iter().all(|l| *l == 0)); + } } + +/// The row-parallel `reduce` / `expand` / `push_pull` / `multiband` against the serial code they +/// replaced, kept below verbatim as the reference. Every output must stay bit for bit the same. +#[cfg(test)] +mod parallel_tests; diff --git a/crates/algo/src/panorama/blend.rs b/crates/algo/src/panorama/blend.rs new file mode 100644 index 0000000000..034e5ed96d --- /dev/null +++ b/crates/algo/src/panorama/blend.rs @@ -0,0 +1,692 @@ +use super::{Normal, Placement, par_map, par_rows}; + +// ---------- photometric ---------- + +/// One image warped into the panorama: an axis-aligned region with colour channels and coverage. +#[derive(Clone, Debug)] +pub struct RoiImage { + pub x0: i32, + pub y0: i32, + pub w: usize, + pub h: usize, + /// Colour channels per pixel (no alpha). + pub ch: usize, + pub px: Vec, + /// Coverage `0..=1`. + pub alpha: Vec, +} + +impl RoiImage { + fn at(&self, x: i32, y: i32) -> Option { + let (lx, ly) = (x - self.x0, y - self.y0); + (lx >= 0 && ly >= 0 && (lx as usize) < self.w && (ly as usize) < self.h).then(|| ly as usize * self.w + lx as usize) + } +} + +/// Placements and source sizes (for the vignetting radius). +pub type Geometry<'a> = (&'a [Placement], &'a [(usize, usize)]); + +/// Gains (per image, per channel) and the vignetting model found by [`photometric`]. +#[derive(Clone, Debug, PartialEq)] +pub struct Photometric { + pub gains: Vec>, + /// `log V(r) = a·r² + b·r⁴`, `r` = distance from the image centre over the half diagonal. + pub vignette: [f64; 2], +} + +impl Photometric { + /// Multiplier that corrects a pixel of image `i` at normalised squared radius `r2`. + pub fn factor(&self, i: usize, c: usize, r2: f64) -> f32 { + let v = (self.vignette[0] * r2 + self.vignette[1] * r2 * r2).exp(); + (1.0 / (self.gains[i][c] as f64 * v)) as f32 + } +} + +/// Estimates exposure gains (`gain`) and a shared vignetting falloff (`vignette`) from the +/// overlaps of `imgs` (`placements`/`sizes` give each panorama pixel's source radius). +/// Without `geometry` (placements and source sizes) the vignetting model is not estimated. +pub fn photometric(imgs: &[RoiImage], geometry: Option, gain: bool, vignette: bool) -> Photometric { + let vignette = vignette && geometry.is_some(); + let n = imgs.len(); + let ch = imgs.first().map_or(1, |i| i.ch); + let identity = Photometric { gains: vec![vec![1.0; ch]; n], vignette: [0.0, 0.0] }; + if n == 0 || (!gain && !vignette) { + return identity; + } + // Overlap samples on a grid: (a, b, colours, radii²). + let (mut x0, mut y0, mut x1, mut y1) = (i32::MAX, i32::MAX, i32::MIN, i32::MIN); + for im in imgs { + x0 = x0.min(im.x0); + y0 = y0.min(im.y0); + x1 = x1.max(im.x0 + im.w as i32); + y1 = y1.max(im.y0 + im.h as i32); + } + let step = (((x1 - x0) as i64 * (y1 - y0) as i64 / 40_000).max(1) as f64).sqrt().ceil() as i32; + let r2_of = |i: usize, u: i32, v: i32| -> Option { + let Some((placements, sizes)) = geometry else { return Some(0.0) }; + let (sx, sy) = placements[i].inverse(u as f64 + 0.5, v as f64 + 0.5)?; + let (w, h) = (sizes[i].0 as f64, sizes[i].1 as f64); + let (dx, dy) = (sx - w / 2.0, sy - h / 2.0); + Some((dx * dx + dy * dy) / (w * w / 4.0 + h * h / 4.0)) + }; + type Sample = (usize, usize, Vec, Vec, f64, f64); + let mut samples: Vec = Vec::new(); + let mut y = y0; + while y < y1 { + let mut x = x0; + while x < x1 { + let cover: Vec<(usize, usize)> = (0..n).filter_map(|i| imgs[i].at(x, y).filter(|&k| imgs[i].alpha[k] > 0.999).map(|k| (i, k))).collect(); + for p in 0..cover.len() { + for q in p + 1..cover.len() { + let ((a, ka), (b, kb)) = (cover[p], cover[q]); + let ca = imgs[a].px[ka * ch..(ka + 1) * ch].to_vec(); + let cb = imgs[b].px[kb * ch..(kb + 1) * ch].to_vec(); + if ca.iter().chain(&cb).any(|v| !(0.02..=0.98).contains(v)) { + continue; + } + // Disagreeing content (parallax, moving objects) says nothing about exposure. + if ca.iter().zip(&cb).any(|(x, y)| (x / y).ln().abs() > 1.0) { + continue; + } + let (Some(ra), Some(rb)) = (r2_of(a, x, y), r2_of(b, x, y)) else { continue }; + samples.push((a, b, ca, cb, ra, rb)); + } + } + x += step; + } + y += step; + } + if samples.len() < 20 { + return identity; + } + // Unknowns: log gain per image (per channel), then a, b (shared, from mean log intensity). + let sigma_n = 0.05; + let sigma_g = if gain { 0.15 } else { 1e-4 }; + let mut out = identity.clone(); + let nv = if vignette { 2 } else { 0 }; + // Vignetting is solved on the channel mean, then gains per channel with it fixed. + let mut ne = Normal::new(n + nv); + for (a, b, ca, cb, ra, rb) in &samples { + let la = ca.iter().map(|v| (*v as f64).ln()).sum::() / ch as f64; + let lb = cb.iter().map(|v| (*v as f64).ln()).sum::() / ch as f64; + let mut row = vec![(*a, 1.0), (*b, -1.0)]; + if vignette { + row.push((n, ra - rb)); + row.push((n + 1, ra * ra - rb * rb)); + } + ne.add(&row, la - lb, 1.0 / sigma_n); + } + for i in 0..n { + ne.add(&[(i, 1.0)], 0.0, 1.0 / sigma_g); + } + if vignette { + ne.add(&[(n, 1.0)], 0.0, 0.5); + ne.add(&[(n + 1, 1.0)], 0.0, 0.5); + } + let Some(sol) = ne.solve() else { return out }; + if vignette { + // Vignetting only darkens; clamp a runaway brightening fit. + out.vignette = [sol[n].clamp(-2.0, 0.5), sol[n + 1].clamp(-2.0, 2.0)]; + } + if gain { + for c in 0..ch { + let mut ne = Normal::new(n); + for (a, b, ca, cb, ra, rb) in &samples { + let va = out.vignette[0] * ra + out.vignette[1] * ra * ra; + let vb = out.vignette[0] * rb + out.vignette[1] * rb * rb; + ne.add(&[(*a, 1.0), (*b, -1.0)], ((ca[c] as f64).ln() - va) - ((cb[c] as f64).ln() - vb), 1.0 / sigma_n); + } + for i in 0..n { + ne.add(&[(i, 1.0)], 0.0, 1.0 / sigma_g); + } + if let Some(g) = ne.solve() { + for i in 0..n { + out.gains[i][c] = g[i].exp().clamp(0.2, 5.0) as f32; + } + } + } + } + out +} + +// ---------- seams ---------- + +/// Seam labels on a `w × h` grid (`-1` = no image): images are added in `order`; each new image +/// takes the part of its overlap with the composite that lies on its side of the cheapest path +/// through the overlap (cost = colour difference). +pub fn seam_labels(w: usize, h: usize, luma: &[Vec], cover: &[Vec], order: &[usize]) -> Vec { + let mut label = vec![-1i32; w * h]; + for &img in order { + let cov = &cover[img]; + let mut overlap = vec![false; w * h]; + let (mut sx, mut sy, mut no) = (0.0f64, 0.0f64, 0usize); + let (mut nx, mut ny, mut nn) = (0.0f64, 0.0f64, 0usize); + for i in 0..w * h { + if !cov[i] { + continue; + } + nx += (i % w) as f64; + ny += (i / w) as f64; + nn += 1; + if label[i] >= 0 { + overlap[i] = true; + sx += (i % w) as f64; + sy += (i / w) as f64; + no += 1; + } else { + label[i] = img as i32; + } + } + if no == 0 { + continue; + } + let (cx, cy) = (nx / nn as f64, ny / nn as f64); + let contained = nn == no; + let (ox, oy) = if contained { + // Wholly inside the composite (#5745: two photos in one document, the second moved + // by Auto-Align so the canvas crops it to the part over the first). Its side of the + // seam is the one it lies towards from the composite's centre; an image centred on + // the composite has no side, and the earlier images keep it. + let (mut px, mut py, mut pn) = (0.0f64, 0.0f64, 0usize); + for (i, l) in label.iter().enumerate() { + if *l >= 0 { + px += (i % w) as f64; + py += (i / w) as f64; + pn += 1; + } + } + let (px, py) = (px / pn.max(1) as f64, py / pn.max(1) as f64); + if (cx - px).abs() < 0.5 && (cy - py).abs() < 0.5 { + continue; + } + (px, py) + } else { + (sx / no as f64, sy / no as f64) + }; + let vertical = (cx - ox).abs() >= (cy - oy).abs(); + // Image on the increasing side along the seam's cross axis? + let positive = if vertical { cx > ox } else { cy > oy }; + // A contained image has no pixels of its own, so a seam on its outer edge would leave it + // nothing: break cost ties away from its side (towards larger k when it lies on the + // decreasing side) so it keeps its side of the overlap. + let prefer_high = contained && !positive; + let cost = |i: usize| -> f32 { 0.002 + (luma[img][i] - luma[label[i] as usize][i]).abs() }; + let (lines, len) = if vertical { (h, w) } else { (w, h) }; + let idx = |line: usize, k: usize| if vertical { line * w + k } else { k * w + line }; + // DP over lines (rows for a vertical seam), within the overlap. + let mut acc = vec![f32::INFINITY; w * h]; + let mut from = vec![usize::MAX; w * h]; + let mut prev_line: Option = None; + for line in 0..lines { + let any = (0..len).any(|k| overlap[idx(line, k)]); + if !any { + continue; + } + for k in 0..len { + let i = idx(line, k); + if !overlap[i] { + continue; + } + let mut best = (0.0f32, usize::MAX); + if let Some(pl) = prev_line.filter(|pl| *pl + 1 == line) { + best = (f32::INFINITY, usize::MAX); + let dks = if prefer_high { [1i64, 0, -1] } else { [-1i64, 0, 1] }; + for dk in dks { + let kk = k as i64 + dk; + if kk < 0 || kk >= len as i64 { + continue; + } + let j = idx(pl, kk as usize); + if acc[j] < best.0 { + best = (acc[j], kk as usize); + } + } + if best.1 == usize::MAX { + best = (0.0, usize::MAX); + } + } + acc[i] = best.0 + cost(i); + from[i] = best.1; + } + prev_line = Some(line); + } + // Backtrack from the cheapest end; lines not reached keep their own best cell. + let mut seam = vec![usize::MAX; lines]; + let mut line = lines; + while line > 0 { + line -= 1; + if seam[line] != usize::MAX { + continue; + } + let Some(k) = (0..len) + .filter(|&k| overlap[idx(line, k)]) + .min_by(|&a, &b| acc[idx(line, a)].total_cmp(&acc[idx(line, b)]).then(if prefer_high { b.cmp(&a) } else { a.cmp(&b) })) + else { + continue; + }; + let (mut l, mut k) = (line, k); + loop { + seam[l] = k; + let f = from[idx(l, k)]; + if f == usize::MAX || l == 0 { + break; + } + l -= 1; + if seam[l] != usize::MAX { + break; + } + k = f; + } + } + for line in 0..lines { + let s = seam[line]; + if s == usize::MAX { + continue; + } + let on_side = |k: usize| if positive { k > s } else { k < s }; + // A contained image has no other cells, so where the overlap has nothing on its side + // of the seam (a line one cell wide) it takes the seam cell rather than nothing. + let take_seam = contained && !(0..len).any(|k| overlap[idx(line, k)] && on_side(k)); + for k in 0..len { + let i = idx(line, k); + if overlap[i] && (on_side(k) || (take_seam && k == s)) { + label[i] = img as i32; + } + } + } + } + label +} + +// ---------- multi-band blending ---------- + +/// Number of pyramid levels used by [`multiband`] for a seam mask of this size. +pub fn blend_levels(w: usize, h: usize) -> usize { + let mut s = w.min(h); + let mut n = 1; + while s > 16 && n < 7 { + s /= 2; + n += 1; + } + n +} + +/// `a[i] ⊕ b[i]` for every `i` (the shorter length wins, like `zip`), in parallel on native +/// targets. Purely elementwise, so the result is the serial result bit for bit. +fn par_zip(a: &[f32], b: &[f32], f: impl Fn(f32, f32) -> f32 + Sync + Send) -> Vec { + const CHUNK: usize = 1 << 14; + let mut out = vec![0.0f32; a.len().min(b.len())]; + let fill = |o: &mut [f32], a: &[f32], b: &[f32]| { + for ((o, &a), &b) in o.iter_mut().zip(a).zip(b) { + *o = f(a, b); + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(CHUNK).zip(a.par_chunks(CHUNK)).zip(b.par_chunks(CHUNK)).for_each(|((o, a), b)| fill(o, a, b)); + } + #[cfg(target_arch = "wasm32")] + out.chunks_mut(CHUNK).zip(a.chunks(CHUNK)).zip(b.chunks(CHUNK)).for_each(|((o, a), b)| fill(o, a, b)); + out +} + +/// `f(a[i])` for every `i`, in parallel on native targets (elementwise, so bit for bit serial). +fn par_map1(a: &[f32], f: impl Fn(f32) -> f32 + Sync + Send) -> Vec { + par_zip(a, a, |x, _| f(x)) +} + +// `reduce`, `expand` and `push_pull` run one pyramid level for one image at a time, and the +// images themselves only fill part of a many-core machine, so each of them also runs its rows +// in parallel. Every output element is computed by the same expression, in the same order, as +// the one-thread loops they replace (rows never read each other's output), so the result is +// bit for bit unchanged. + +pub(super) fn reduce(w: usize, h: usize, ch: usize, src: &[f32]) -> (usize, usize, Vec) { + let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); + let k = [1.0f32, 4.0, 6.0, 4.0, 1.0]; + // Separable 5-tap binomial, then decimate. + let mut tmp = vec![0.0f32; nw * h * ch]; + par_rows(&mut tmp, nw, ch, |y, row| { + let line = &src[y * w * ch..(y + 1) * w * ch]; + for x in 0..nw { + // The clamped source column of each tap is the same for every channel. + let xs: [usize; 5] = std::array::from_fn(|t| (2 * x as i64 + t as i64 - 2).clamp(0, w as i64 - 1) as usize * ch); + for c in 0..ch { + let mut s = 0.0; + for (kv, xx) in k.iter().zip(xs) { + s += kv * line[xx + c]; + } + row[x * ch + c] = s / 16.0; + } + } + }); + let mut out = vec![0.0f32; nw * nh * ch]; + par_rows(&mut out, nw, ch, |y, row| { + // Each output row is five source rows added in tap order: the same sum per element as + // before (`0 + k0·a + k1·b + …`), but over whole rows so it vectorises. + for (t, kv) in k.iter().enumerate() { + let yy = (2 * y as i64 + t as i64 - 2).clamp(0, h as i64 - 1) as usize; + for (o, &v) in row.iter_mut().zip(&tmp[yy * nw * ch..(yy + 1) * nw * ch]) { + *o += kv * v; + } + } + for o in row.iter_mut() { + *o /= 16.0; + } + }); + (nw, nh, out) +} + +pub(super) fn expand(sw: usize, sh: usize, ch: usize, src: &[f32], w: usize, h: usize) -> Vec { + // Bilinear upsampling at half-pixel-aligned positions. + let mut out = vec![0.0f32; w * h * ch]; + // Source columns and weights depend on x only: worked out once instead of for every row. + // The clamped position is never negative, so truncating is `floor` (and spares the libm call). + let cols: Vec<(usize, usize, f32)> = (0..w) + .map(|x| { + let fx = ((x as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sw - 1) as f32); + let x0 = fx as usize; + (x0 * ch, (x0 + 1).min(sw - 1) * ch, fx - x0 as f32) + }) + .collect(); + par_rows(&mut out, w, ch, |y, row| { + let fy = ((y as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sh - 1) as f32); + let y0 = fy as usize; + let ty = fy - y0 as f32; + let y1 = (y0 + 1).min(sh - 1); + let (r0, r1) = (&src[y0 * sw * ch..(y0 + 1) * sw * ch], &src[y1 * sw * ch..(y1 + 1) * sw * ch]); + for (px, &(x0, x1, tx)) in row.chunks_exact_mut(ch.max(1)).zip(&cols) { + for (c, o) in px.iter_mut().enumerate() { + let top = r0[x0 + c] + (r0[x1 + c] - r0[x0 + c]) * tx; + let bot = r1[x0 + c] + (r1[x1 + c] - r1[x0 + c]) * tx; + *o = top + (bot - top) * ty; + } + } + }); + out +} + +/// Fills pixels with zero `wt` from their surroundings (push-pull), so pyramid levels don't +/// pull in black from outside an image. +pub(super) fn push_pull(w: usize, h: usize, ch: usize, px: &mut [f32], wt: &[f32]) { + if w <= 1 && h <= 1 { + return; + } + let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); + // Coarse sums. A coarse row is fed by two fine rows; walking them in raster order adds each + // coarse cell's (up to) four fine pixels in the order the serial scatter-add did, from zero. + let mut cpx = vec![0.0f32; nw * nh * ch]; + let mut cw = vec![0.0f32; nw * nh]; + par_rows(&mut cw, nw, 1, |cy, row| { + for y in 2 * cy..(2 * cy + 2).min(h) { + for (x, &a) in wt[y * w..(y + 1) * w].iter().enumerate() { + row[x / 2] += a; + } + } + }); + let px_in: &[f32] = px; + par_rows(&mut cpx, nw, ch, |cy, row| { + for y in 2 * cy..(2 * cy + 2).min(h) { + for x in 0..w { + let (i, j) = (y * w + x, x / 2); + let a = wt[i]; + for c in 0..ch { + row[j * ch + c] += a * px_in[i * ch + c]; + } + } + } + }); + // Normalise by the weight (this reads `cw` before it is reset to 0 / 1 below). + par_rows(&mut cpx, nw, ch, |cy, row| { + for cx in 0..nw { + let wj = cw[cy * nw + cx]; + if wj > 0.0 { + for c in 0..ch { + row[cx * ch + c] /= wj; + } + } + } + }); + for v in cw.iter_mut() { + if *v > 0.0 { + *v = 1.0; + } + } + if cw.contains(&0.0) && cw.iter().any(|v| *v > 0.0) { + push_pull(nw, nh, ch, &mut cpx, &cw); + } + par_rows(px, w, ch, |y, row| { + for x in 0..w { + let i = y * w + x; + if wt[i] < 1.0 { + let j = (y / 2) * nw + x / 2; + let a = wt[i]; + for c in 0..ch { + row[x * ch + c] = a * row[x * ch + c] + (1.0 - a) * cpx[j * ch + c]; + } + } + } + }); +} + +/// One image's pyramid in [`multiband`]: the grid origin `(gx0, gy0)` at level 0, the Laplacian +/// levels `(w, h, colours)` and the Gaussian weight levels. +type PyramidBand = (i32, i32, Vec<(usize, usize, Vec)>, Vec>); + +/// The part of `band` on panorama row `py` of level `l`: the panorama column its first pixel +/// lands on, its width, its weight row and its colour row. `None` when it misses the row. +fn band_row(band: &PyramidBand, l: usize, ch: usize, py: usize) -> Option<(usize, usize, &[f32], &[f32])> { + let (ox, oy) = ((band.0 >> l) as usize, (band.1 >> l) as usize); + let (lw, lh, colours) = &band.2[l]; + (py >= oy && py - oy < *lh).then(|| { + let y = py - oy; + (ox, *lw, &band.3[l][y * lw..(y + 1) * lw], &colours[y * lw * ch..(y + 1) * lw * ch]) + }) +} + +/// Estimated bytes of band construction [`multiband`] keeps in flight at once. Images are built +/// and accumulated in batches under it, so its peak memory doesn't grow with the image count +/// (#7758). Four 24 MP RGB sources (~1.2 GB each) fit in one batch: building bands side by side +/// uses a many-core machine better than one band's row-parallel work, and one at a time made the +/// usual 2–4 image Photomerge 12–36% slower on a 32-thread desktop. +#[cfg(not(target_arch = "wasm32"))] +const BAND_BUDGET: usize = 5 << 30; + +/// Without threads the bands of a batch are built one after another anyway, so each is added as +/// soon as it is built (the browser's address space is 4 GB). +#[cfg(target_arch = "wasm32")] +const BAND_BUDGET: usize = 0; + +/// Multi-band blend of `imgs` into a `w × h` panorama (origin 0,0) with one weight map per image +/// over the same grid as its ROI (`weights[i]` has `w_i × h_i` entries). Returns the colour +/// channels (`ch` per pixel) and the total coverage. +pub fn multiband(w: usize, h: usize, imgs: &[RoiImage], weights: &[Vec], levels: usize) -> (Vec, Vec) { + multiband_batched(w, h, imgs, weights, levels, BAND_BUDGET) +} + +/// [`multiband`], building at most `budget` estimated bytes of bands at a time (at least one +/// image). Each pixel still adds the bands in image order, so the result doesn't depend on it. +pub(super) fn multiband_batched(w: usize, h: usize, imgs: &[RoiImage], weights: &[Vec], levels: usize, budget: usize) -> (Vec, Vec) { + let ch = imgs.first().map_or(1, |i| i.ch); + let levels = levels.max(1); + let align = 1i32 << (levels - 1); + // Level sizes of the panorama. + let mut sizes = vec![(w, h)]; + for _ in 1..levels { + let Some(&(pw, ph)) = sizes.last() else { break }; + sizes.push((pw.div_ceil(2), ph.div_ceil(2))); + } + let mut acc: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh * ch]).collect(); + let mut wsum: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh]).collect(); + let margin = align * 4; + // Each image on its ROI grown by a margin and aligned to the coarsest level's grid. + let grid = |im: &RoiImage| { + let gx0 = ((im.x0 - margin).max(0) / align) * align; + let gy0 = ((im.y0 - margin).max(0) / align) * align; + let gx1 = (im.x0 + im.w as i32 + margin).min(w as i32); + let gy1 = (im.y0 + im.h as i32 + margin).min(h as i32); + (gx0, gy0, (gx1 - gx0).max(1) as usize, (gy1 - gy0).max(1) as usize) + }; + // Building one band holds its grid's colour and weight planes about three times over: the + // pyramids are 4/3 of a plane, and the grid copy, coverage and `expand` scratch come on top. + let cost = |im: &RoiImage| { + let (_, _, gw, gh) = grid(im); + gw.saturating_mul(gh).saturating_mul(ch + 1).saturating_mul(4 * 3) + }; + let band = |k: usize| -> PyramidBand { + let im = &imgs[k]; + let (gx0, gy0, gw, gh) = grid(im); + // Each grid pixel takes the image pixel that lands on it (every image pixel lands on at + // most one), so rows of the grid are independent and fill in parallel. + let src_of = |gx: usize, gy: usize| -> Option { + let (x, y) = (gx as i64 + gx0 as i64 - im.x0 as i64, gy as i64 + gy0 as i64 - im.y0 as i64); + (x >= 0 && y >= 0 && (x as usize) < im.w && (y as usize) < im.h).then(|| y as usize * im.w + x as usize) + }; + let mut px = vec![0.0f32; gw * gh * ch]; + par_rows(&mut px, gw, ch, |gy, row| { + for gx in 0..gw { + if let Some(i) = src_of(gx, gy) { + row[gx * ch..(gx + 1) * ch].copy_from_slice(&im.px[i * ch..(i + 1) * ch]); + } + } + }); + let mut cov = vec![0.0f32; gw * gh]; + par_rows(&mut cov, gw, 1, |gy, row| { + for (gx, o) in row.iter_mut().enumerate() { + if let Some(i) = src_of(gx, gy) { + *o = im.alpha[i]; + } + } + }); + let mut wt = vec![0.0f32; gw * gh]; + par_rows(&mut wt, gw, 1, |gy, row| { + for (gx, o) in row.iter_mut().enumerate() { + if let Some(i) = src_of(gx, gy) { + *o = weights[k][i] * im.alpha[i].min(1.0); + } + } + }); + let bin: Vec = par_map1(&cov, |a| if a > 0.5 { 1.0 } else { 0.0 }); + drop(cov); + push_pull(gw, gh, ch, &mut px, &bin); + drop(bin); + // Laplacian of the colours, Gaussian of the weights. Planes move into the pyramids rather + // than being copied: with one image built at a time, a copy is a serial pass over it. + let mut lap: Vec<(usize, usize, Vec)> = Vec::new(); + let mut gau: Vec> = Vec::new(); + let (mut cw, mut chh, mut cur, mut cwt) = (gw, gh, px, wt); + for l in 0..levels { + if l + 1 == levels { + lap.push((cw, chh, cur)); + gau.push(cwt); + break; + } + let (nw, nh, down) = reduce(cw, chh, ch, &cur); + let up = expand(nw, nh, ch, &down, cw, chh); + lap.push((cw, chh, par_zip(&cur, &up, |a, b| a - b))); + let (_, _, wdown) = reduce(cw, chh, 1, &cwt); + gau.push(cwt); + cw = nw; + chh = nh; + cur = down; + cwt = wdown; + } + (gx0, gy0, lap, gau) + }; + // Build the bands a batch at a time and add each batch before building the next. Each pixel + // adds the bands in the same order as the serial loop (per level, bands one after another; + // batches follow each other in image order), but rows of a level are independent, so they + // run in parallel: every row walks the batch's bands itself. + let mut start = 0; + while start < imgs.len() { + let mut end = start + 1; + let mut bytes = cost(&imgs[start]); + while let Some(im) = imgs.get(end) + && bytes.saturating_add(cost(im)) <= budget + { + bytes = bytes.saturating_add(cost(im)); + end += 1; + } + let bands = par_map(end - start, |j| band(start + j)); + for l in 0..levels { + let pw = sizes[l].0; + par_rows(&mut wsum[l], pw, 1, |py, row| { + for b in &bands { + let Some((ox, lw, g, _)) = band_row(b, l, ch, py) else { continue }; + for x in 0..lw { + let px = ox + x; + if px >= pw { + break; + } + let k = g[x]; + if k <= 0.0 { + continue; + } + row[px] += k; + } + } + }); + par_rows(&mut acc[l], pw, ch, |py, row| { + for b in &bands { + let Some((ox, lw, g, band)) = band_row(b, l, ch, py) else { continue }; + for x in 0..lw { + let px = ox + x; + if px >= pw { + break; + } + let k = g[x]; + if k <= 0.0 { + continue; + } + for c in 0..ch { + row[px * ch + c] += k * band[x * ch + c]; + } + } + } + }); + } + start = end; + } + for l in 0..levels { + let pw = sizes[l].0; + let ws = &wsum[l]; + par_rows(&mut acc[l], pw, ch, |py, row| { + for x in 0..pw { + let k = ws[py * pw + x]; + for c in 0..ch { + row[x * ch + c] = if k > 1e-8 { row[x * ch + c] / k } else { 0.0 }; + } + } + }); + } + // Collapse. + // `levels ≥ 1`, so there is always a coarsest level. + let Some(mut cur) = acc.pop() else { return (vec![0.0; w * h * ch], vec![0.0; w * h]) }; + for l in (0..levels - 1).rev() { + let (pw, ph) = sizes[l]; + let (sw, sh) = sizes[l + 1]; + let up = expand(sw, sh, ch, &cur, pw, ph); + cur = par_zip(&acc[l], &up, |a, b| a + b); + } + // Coverage: union of the images. Rows are independent; each walks the images in order, so a + // pixel sees the same sequence of `max` calls as in the serial loop. + let mut cov = vec![0.0f32; w * h]; + par_rows(&mut cov, w, 1, |py, row| { + for im in imgs { + let y = py as i64 - im.y0 as i64; + if y < 0 || y as usize >= im.h { + continue; + } + for x in 0..im.w { + let px = im.x0 + x as i32; + if px < 0 || px as usize >= w { + continue; + } + let p = px as usize; + row[p] = row[p].max(im.alpha[y as usize * im.w + x]); + } + } + }); + (cur, cov) +} diff --git a/crates/algo/src/panorama/parallel_tests.rs b/crates/algo/src/panorama/parallel_tests.rs new file mode 100644 index 0000000000..77c25b0f98 --- /dev/null +++ b/crates/algo/src/panorama/parallel_tests.rs @@ -0,0 +1,402 @@ +use super::*; + +fn mix(mut z: u64) -> u64 { + z = z.wrapping_add(0x9E37_79B9_7F4A_7C15); + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) +} + +/// Deterministic data from integer arithmetic only (no libm): mostly `0..1`, with negatives, +/// signed zeros and large values so a different order of operations cannot hide. +fn noise(len: usize, seed: u64) -> Vec { + (0..len as u64) + .map(|i| { + let z = mix(seed.wrapping_mul(0x1234_5678_9ABC).wrapping_add(i)); + let v = (z >> 40) as f32 / 16_777_216.0; + match z & 31 { + 0 => -v, + 1 => -0.0, + 2 => v * 1000.0, + 3 => 0.0, + _ => v, + } + }) + .collect() +} + +/// Weights with exact zeros, exact ones and partial values, in blobs and noise. +fn weights(w: usize, h: usize, seed: u64) -> Vec { + (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + let z = mix(seed + i as u64); + if (x / 5 + y / 3) % 3 == 0 { + 0.0 + } else if z.is_multiple_of(7) { + ((z >> 20) % 100) as f32 / 100.0 + } else { + 1.0 + } + }) + .collect() +} + +fn bits(v: &[f32]) -> Vec { + v.iter().map(|x| x.to_bits()).collect() +} + +fn fnv(parts: &[&[f32]]) -> u64 { + let mut h = 0xcbf2_9ce4_8422_2325u64; + for p in parts { + for x in *p { + h ^= x.to_bits() as u64; + h = h.wrapping_mul(0x0000_0100_0000_01b3); + } + } + h +} + +/// Sizes that cross the odd / even and tiny cases (1 wide, 1 high, 1×1, odd, power of two + 1). +const SIZES: [(usize, usize); 12] = [(1, 1), (1, 9), (9, 1), (2, 2), (3, 5), (4, 4), (17, 13), (64, 40), (65, 41), (97, 61), (130, 7), (7, 130)]; + +fn ref_reduce(w: usize, h: usize, ch: usize, src: &[f32]) -> (usize, usize, Vec) { + let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); + let k = [1.0f32, 4.0, 6.0, 4.0, 1.0]; + // Separable 5-tap binomial, then decimate. + let mut tmp = vec![0.0f32; nw * h * ch]; + for y in 0..h { + for x in 0..nw { + for c in 0..ch { + let mut s = 0.0; + for (t, kv) in k.iter().enumerate() { + let xx = (2 * x as i64 + t as i64 - 2).clamp(0, w as i64 - 1) as usize; + s += kv * src[(y * w + xx) * ch + c]; + } + tmp[(y * nw + x) * ch + c] = s / 16.0; + } + } + } + let mut out = vec![0.0f32; nw * nh * ch]; + for y in 0..nh { + for x in 0..nw { + for c in 0..ch { + let mut s = 0.0; + for (t, kv) in k.iter().enumerate() { + let yy = (2 * y as i64 + t as i64 - 2).clamp(0, h as i64 - 1) as usize; + s += kv * tmp[(yy * nw + x) * ch + c]; + } + out[(y * nw + x) * ch + c] = s / 16.0; + } + } + } + (nw, nh, out) +} + +fn ref_expand(sw: usize, sh: usize, ch: usize, src: &[f32], w: usize, h: usize) -> Vec { + // Bilinear upsampling at half-pixel-aligned positions. + let mut out = vec![0.0f32; w * h * ch]; + for y in 0..h { + let fy = ((y as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sh - 1) as f32); + let (y0, ty) = (fy.floor() as usize, fy - fy.floor()); + let y1 = (y0 + 1).min(sh - 1); + for x in 0..w { + let fx = ((x as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (sw - 1) as f32); + let (x0, tx) = (fx.floor() as usize, fx - fx.floor()); + let x1 = (x0 + 1).min(sw - 1); + for c in 0..ch { + let p = |xx: usize, yy: usize| src[(yy * sw + xx) * ch + c]; + let top = p(x0, y0) + (p(x1, y0) - p(x0, y0)) * tx; + let bot = p(x0, y1) + (p(x1, y1) - p(x0, y1)) * tx; + out[(y * w + x) * ch + c] = top + (bot - top) * ty; + } + } + } + out +} + +fn ref_push_pull(w: usize, h: usize, ch: usize, px: &mut [f32], wt: &[f32]) { + if w <= 1 && h <= 1 { + return; + } + let (nw, nh) = (w.div_ceil(2), h.div_ceil(2)); + let mut cpx = vec![0.0f32; nw * nh * ch]; + let mut cw = vec![0.0f32; nw * nh]; + for y in 0..h { + for x in 0..w { + let (i, j) = (y * w + x, (y / 2) * nw + x / 2); + let a = wt[i]; + cw[j] += a; + for c in 0..ch { + cpx[j * ch + c] += a * px[i * ch + c]; + } + } + } + for j in 0..nw * nh { + if cw[j] > 0.0 { + for c in 0..ch { + cpx[j * ch + c] /= cw[j]; + } + cw[j] = 1.0; + } + } + if cw.contains(&0.0) && cw.iter().any(|v| *v > 0.0) { + ref_push_pull(nw, nh, ch, &mut cpx, &cw); + } + for y in 0..h { + for x in 0..w { + let i = y * w + x; + if wt[i] < 1.0 { + let j = (y / 2) * nw + x / 2; + let a = wt[i]; + for c in 0..ch { + px[i * ch + c] = a * px[i * ch + c] + (1.0 - a) * cpx[j * ch + c]; + } + } + } + } +} + +fn ref_multiband(w: usize, h: usize, imgs: &[RoiImage], weights: &[Vec], levels: usize) -> (Vec, Vec) { + let ch = imgs.first().map_or(1, |i| i.ch); + let levels = levels.max(1); + let align = 1i32 << (levels - 1); + // Level sizes of the panorama. + let mut sizes = vec![(w, h)]; + for _ in 1..levels { + let Some(&(pw, ph)) = sizes.last() else { break }; + sizes.push((pw.div_ceil(2), ph.div_ceil(2))); + } + let mut acc: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh * ch]).collect(); + let mut wsum: Vec> = sizes.iter().map(|&(lw, lh)| vec![0.0; lw * lh]).collect(); + let margin = align * 4; + // Each image on its ROI grown by a margin and aligned to the coarsest level's grid. + let bands = par_map(imgs.len(), |k| { + let im = &imgs[k]; + let gx0 = ((im.x0 - margin).max(0) / align) * align; + let gy0 = ((im.y0 - margin).max(0) / align) * align; + let gx1 = (im.x0 + im.w as i32 + margin).min(w as i32); + let gy1 = (im.y0 + im.h as i32 + margin).min(h as i32); + let (gw, gh) = ((gx1 - gx0).max(1) as usize, (gy1 - gy0).max(1) as usize); + let mut px = vec![0.0f32; gw * gh * ch]; + let mut cov = vec![0.0f32; gw * gh]; + let mut wt = vec![0.0f32; gw * gh]; + for y in 0..im.h { + for x in 0..im.w { + let (gx, gy) = (im.x0 + x as i32 - gx0, im.y0 + y as i32 - gy0); + if gx < 0 || gy < 0 || gx as usize >= gw || gy as usize >= gh { + continue; + } + let (i, g) = (y * im.w + x, gy as usize * gw + gx as usize); + cov[g] = im.alpha[i]; + wt[g] = weights[k][i] * im.alpha[i].min(1.0); + px[g * ch..(g + 1) * ch].copy_from_slice(&im.px[i * ch..(i + 1) * ch]); + } + } + let bin: Vec = cov.iter().map(|a| if *a > 0.5 { 1.0 } else { 0.0 }).collect(); + ref_push_pull(gw, gh, ch, &mut px, &bin); + // Laplacian of the colours, Gaussian of the weights. + let mut lap: Vec<(usize, usize, Vec)> = Vec::new(); + let mut gau: Vec> = Vec::new(); + let (mut cw, mut chh, mut cur, mut cwt) = (gw, gh, px, wt); + for l in 0..levels { + if l + 1 == levels { + lap.push((cw, chh, cur.clone())); + gau.push(cwt.clone()); + break; + } + let (nw, nh, down) = ref_reduce(cw, chh, ch, &cur); + let up = ref_expand(nw, nh, ch, &down, cw, chh); + lap.push((cw, chh, cur.iter().zip(&up).map(|(a, b)| a - b).collect())); + gau.push(cwt.clone()); + let (_, _, wdown) = ref_reduce(cw, chh, 1, &cwt); + cw = nw; + chh = nh; + cur = down; + cwt = wdown; + } + (gx0, gy0, lap, gau) + }); + for (gx0, gy0, lap, gau) in &bands { + for (l, ((lw, lh, band), g)) in lap.iter().zip(gau).enumerate() { + let (ox, oy) = ((gx0 >> l) as usize, (gy0 >> l) as usize); + let (pw, ph) = sizes[l]; + for y in 0..*lh { + let py = oy + y; + if py >= ph { + break; + } + for x in 0..*lw { + let px = ox + x; + if px >= pw { + break; + } + let (i, p) = (y * lw + x, py * pw + px); + let k = g[i]; + if k <= 0.0 { + continue; + } + wsum[l][p] += k; + for c in 0..ch { + acc[l][p * ch + c] += k * band[i * ch + c]; + } + } + } + } + } + for l in 0..levels { + let (pw, ph) = sizes[l]; + for p in 0..pw * ph { + let k = wsum[l][p]; + for c in 0..ch { + acc[l][p * ch + c] = if k > 1e-8 { acc[l][p * ch + c] / k } else { 0.0 }; + } + } + } + // Collapse. + // `levels ≥ 1`, so there is always a coarsest level. + let Some(mut cur) = acc.pop() else { return (vec![0.0; w * h * ch], vec![0.0; w * h]) }; + for l in (0..levels - 1).rev() { + let (pw, ph) = sizes[l]; + let (sw, sh) = sizes[l + 1]; + let up = ref_expand(sw, sh, ch, &cur, pw, ph); + cur = acc[l].iter().zip(&up).map(|(a, b)| a + b).collect(); + } + // Coverage: union of the images. + let mut cov = vec![0.0f32; w * h]; + for im in imgs { + for y in 0..im.h { + for x in 0..im.w { + let (px, py) = (im.x0 + x as i32, im.y0 + y as i32); + if px < 0 || py < 0 || px as usize >= w || py as usize >= h { + continue; + } + let p = py as usize * w + px as usize; + cov[p] = cov[p].max(im.alpha[y * im.w + x]); + } + } + } + (cur, cov) +} + +#[test] +fn reduce_and_expand_match_the_serial_code() { + for (n, &(w, h)) in SIZES.iter().enumerate() { + for ch in [1usize, 3, 4] { + let src = noise(w * h * ch, n as u64 * 7 + ch as u64); + let (nw, nh, got) = reduce(w, h, ch, &src); + let (rw, rh, want) = ref_reduce(w, h, ch, &src); + assert_eq!((nw, nh), (rw, rh)); + assert_eq!(bits(&got), bits(&want), "reduce {w}x{h}x{ch}"); + // Expand back to the original size and to sizes that are not twice the source's. + for (ew, eh) in [(w, h), (2 * nw, 2 * nh), (2 * nw - 1, 2 * nh + 1), (w + 3, h + 5)] { + let got = expand(nw, nh, ch, &want, ew, eh); + let want_up = ref_expand(nw, nh, ch, &want, ew, eh); + assert_eq!(bits(&got), bits(&want_up), "expand {nw}x{nh}x{ch} to {ew}x{eh}"); + } + } + } +} + +#[test] +fn push_pull_matches_the_serial_code() { + for (n, &(w, h)) in SIZES.iter().enumerate() { + for ch in [1usize, 3] { + for variant in 0..3u64 { + let wt = match variant { + 0 => weights(w, h, n as u64), + 1 => vec![1.0; w * h], + // A single covered pixel: every level of the recursion has to fill from it. + _ => (0..w * h).map(|i| if i == (w * h) / 3 { 1.0 } else { 0.0 }).collect(), + }; + let px = noise(w * h * ch, 100 + n as u64); + let (mut got, mut want) = (px.clone(), px); + push_pull(w, h, ch, &mut got, &wt); + ref_push_pull(w, h, ch, &mut want, &wt); + assert_eq!(bits(&got), bits(&want), "push_pull {w}x{h}x{ch} variant {variant}"); + } + } + } +} + +/// ROIs of assorted sizes and offsets (some sticking out of the canvas), with holes in the alpha. +fn rois(w: usize, h: usize, ch: usize, seed: u64) -> (Vec, Vec>) { + let specs = + [(0i32, 0i32, w * 2 / 3, h), (w as i32 / 3, -(h as i32) / 5, w * 2 / 3 + 1, h * 3 / 4 + 1), (-(w as i32) / 4, h as i32 / 2, w / 2 + 1, h / 2 + 3)]; + let imgs: Vec = specs + .iter() + .enumerate() + .map(|(k, &(x0, y0, iw, ih))| { + let (iw, ih) = (iw.max(1), ih.max(1)); + let mut alpha = vec![1.0f32; iw * ih]; + for (i, a) in alpha.iter_mut().enumerate() { + let z = mix(seed + k as u64 * 1000 + i as u64); + if (i % iw) / 4 % 5 == 2 && (i / iw) % 6 < 3 { + *a = 0.0; + } else if z.is_multiple_of(11) { + *a = 0.3; + } + } + RoiImage { x0, y0, w: iw, h: ih, ch, px: noise(iw * ih * ch, seed + k as u64), alpha } + }) + .collect(); + let wts = imgs.iter().enumerate().map(|(k, r)| weights(r.w, r.h, seed + 50 + k as u64)).collect(); + (imgs, wts) +} + +#[test] +fn multiband_matches_the_serial_code() { + for (n, &(w, h)) in [(1usize, 1usize), (5, 3), (40, 30), (97, 61), (130, 70), (64, 64), (201, 129)].iter().enumerate() { + for ch in [1usize, 3] { + let (imgs, wts) = rois(w, h, ch, n as u64 * 31 + ch as u64); + for levels in [1usize, 2, 3, 5, 7] { + let got = multiband(w, h, &imgs, &wts, levels); + let want = ref_multiband(w, h, &imgs, &wts, levels); + assert_eq!(bits(&got.0), bits(&want.0), "colours {w}x{h}x{ch} levels {levels}"); + assert_eq!(bits(&got.1), bits(&want.1), "coverage {w}x{h}x{ch} levels {levels}"); + } + } + } + // No images at all, and zero levels. + assert_eq!(multiband(9, 7, &[], &[], 3).0.len(), ref_multiband(9, 7, &[], &[], 3).0.len()); +} + +/// Batching the bands (#7758) changes when they are built, not the result. Budgets from none +/// (one image per batch) up to everything in steps of a fifth pass through every split of the +/// three images. +#[test] +fn multiband_batches_match_the_serial_code() { + let budgets: Vec = + std::iter::successors(Some(1024.0f64), |b| Some(b * 1.2)).take_while(|b| *b < 4e6).map(|b| b as usize).chain([0, usize::MAX]).collect(); + for (n, &(w, h)) in [(40usize, 30usize), (97, 61), (201, 129)].iter().enumerate() { + for ch in [1usize, 3] { + let (imgs, wts) = rois(w, h, ch, n as u64 * 17 + ch as u64); + for levels in [1usize, 3, 5] { + let want = ref_multiband(w, h, &imgs, &wts, levels); + for &budget in &budgets { + let got = multiband_batched(w, h, &imgs, &wts, levels, budget); + assert_eq!(bits(&got.0), bits(&want.0), "colours {w}x{h}x{ch} levels {levels} budget {budget}"); + assert_eq!(bits(&got.1), bits(&want.1), "coverage {w}x{h}x{ch} levels {levels} budget {budget}"); + } + } + } + } +} + +/// Hashes recorded from the code before it was made parallel (plain f32 arithmetic only, so +/// they hold on every platform). +#[test] +fn multiband_hashes_recorded_from_the_serial_code() { + let (imgs, wts) = rois(97, 61, 3, 5); + let (px, cov) = multiband(97, 61, &imgs, &wts, 5); + assert_eq!(fnv(&[&px, &cov]), 0xf89a5726082ebb6c, "97x61 rgb, 5 levels"); + let (imgs, wts) = rois(130, 70, 1, 9); + let (px, cov) = multiband(130, 70, &imgs, &wts, 7); + assert_eq!(fnv(&[&px, &cov]), 0xb7d7ddc59d201730, "130x70 grey, 7 levels"); + let wt = weights(65, 41, 3); + let mut px = noise(65 * 41 * 3, 4); + push_pull(65, 41, 3, &mut px, &wt); + assert_eq!(fnv(&[&px]), 0x50ea36a84ca9a68f, "push_pull 65x41 rgb"); + let (nw, nh, r) = reduce(65, 41, 3, &px); + assert_eq!(fnv(&[&r, &expand(nw, nh, 3, &r, 65, 41)]), 0x51823acfb4206dd8, "reduce + expand 65x41 rgb"); +} diff --git a/crates/algo/src/params_ext.rs b/crates/algo/src/params_ext.rs index 3a9572fb46..2349604196 100644 --- a/crates/algo/src/params_ext.rs +++ b/crates/algo/src/params_ext.rs @@ -182,6 +182,9 @@ pub enum DepthSource { None, Transparency, LayerMask, + /// An alpha channel of the document (Photoshop lists them after Layer Mask), supplied by the + /// caller like the layer mask. + Channel, } /// Built-in Shape Blur kernels. diff --git a/crates/algo/src/photo_util.rs b/crates/algo/src/photo_util.rs index bf65bc4459..96c6f7b434 100644 --- a/crates/algo/src/photo_util.rs +++ b/crates/algo/src/photo_util.rs @@ -62,68 +62,213 @@ impl Normal { } } -/// A sparse least-squares problem `min Σ w²(row·x − rhs)²`, solved by conjugate gradients on the -/// normal equations (CGLS). Used for mesh warps with thousands of unknowns. -#[derive(Default)] -pub(crate) struct SparseLs { - pub n: usize, - rows: Vec<(Vec<(usize, f64)>, f64)>, +/// A compressed sparse row matrix (symmetric, used by CG). +pub(crate) struct Csr { + pub rows: Vec, + pub cols: Vec, + pub vals: Vec, + pub diag: Vec, } -impl SparseLs { - pub fn new(n: usize) -> Self { - SparseLs { n, rows: Vec::new() } - } - pub fn add(&mut self, row: Vec<(usize, f64)>, rhs: f64, w: f64) { - self.rows.push((row.into_iter().map(|(i, c)| (i, c * w)).collect(), rhs * w)); - } - /// CGLS from `x0`; at most `iters` iterations. - pub fn solve(&self, x0: &[f64], iters: usize) -> Vec { - let n = self.n; - let mut x = x0.to_vec(); - let ax = |x: &[f64]| -> Vec { self.rows.iter().map(|(r, _)| r.iter().map(|&(i, c)| c * x[i]).sum()).collect() }; - let atv = |v: &[f64]| -> Vec { - let mut out = vec![0.0; n]; - for ((r, _), vi) in self.rows.iter().zip(v) { - for &(i, c) in r { - out[i] += c * vi; +impl Csr { + pub fn from_triplets(n: usize, mut t: Vec<(usize, usize, f64)>) -> Csr { + t.sort_by_key(|a| (a.0, a.1)); + let mut rows = vec![0usize; n + 1]; + let mut cols = Vec::with_capacity(t.len()); + let mut vals: Vec = Vec::with_capacity(t.len()); + let mut last = (usize::MAX, usize::MAX); + for (i, j, v) in t { + if (i, j) == last { + *vals.last_mut().unwrap_or(&mut 0.0) += v; + continue; + } + last = (i, j); + cols.push(j); + vals.push(v); + rows[i + 1] = cols.len(); + } + for i in 0..n { + rows[i + 1] = rows[i + 1].max(rows[i]); + } + let mut diag = vec![1.0; n]; + for (i, d) in diag.iter_mut().enumerate() { + for k in rows[i]..rows[i + 1] { + if cols[k] == i && vals[k] > 0.0 { + *d = vals[k]; } } - out - }; - let mut r: Vec = self.rows.iter().zip(ax(&x)).map(|((_, b), a)| b - a).collect(); - let mut s = atv(&r); - let mut p = s.clone(); - let mut gamma: f64 = s.iter().map(|v| v * v).sum(); - let g0 = gamma.max(1e-30); - for _ in 0..iters { - if gamma <= g0 * 1e-20 { + } + Csr { rows, cols, vals, diag } + } + + pub fn mul(&self, x: &[f64], out: &mut [f64]) { + for (i, o) in out.iter_mut().enumerate() { + let mut s = 0.0; + for k in self.rows[i]..self.rows[i + 1] { + s += self.vals[k] * x[self.cols[k]]; + } + *o = s; + } + } + + /// Jacobi-preconditioned conjugate gradients, warm-started from `x`. + pub fn solve(&self, b: &[f64], x: &mut [f64]) { + let n = b.len(); + let mut r = vec![0.0; n]; + self.mul(x, &mut r); + for i in 0..n { + r[i] = b[i] - r[i]; + } + let bn: f64 = b.iter().map(|v| v * v).sum::().sqrt().max(1e-30); + let mut z: Vec = r.iter().zip(&self.diag).map(|(r, d)| r / d).collect(); + let mut p = z.clone(); + let mut rz: f64 = r.iter().zip(&z).map(|(a, b)| a * b).sum(); + let mut ap = vec![0.0; n]; + for _ in 0..(4 * n).clamp(50, 2000) { + if r.iter().map(|v| v * v).sum::().sqrt() <= 1e-12 * bn { break; } - let q = ax(&p); - let qq: f64 = q.iter().map(|v| v * v).sum(); - if qq <= 1e-300 { + self.mul(&p, &mut ap); + let pap: f64 = p.iter().zip(&ap).map(|(a, b)| a * b).sum(); + if pap.abs() < 1e-300 { break; } - let alpha = gamma / qq; + let alpha = rz / pap; for i in 0..n { x[i] += alpha * p[i]; + r[i] -= alpha * ap[i]; } - for (ri, qi) in r.iter_mut().zip(&q) { - *ri -= alpha * qi; - } - s = atv(&r); - let g1: f64 = s.iter().map(|v| v * v).sum(); - let beta = g1 / gamma; - gamma = g1; for i in 0..n { - p[i] = s[i] + beta * p[i]; + z[i] = r[i] / self.diag[i]; + } + let rz2: f64 = r.iter().zip(&z).map(|(a, b)| a * b).sum(); + let beta = rz2 / rz; + rz = rz2; + for i in 0..n { + p[i] = z[i] + beta * p[i]; } } - x } } +/// Banded Cholesky factor `A = L Lᵀ` (the mesh numbers its vertices in reverse Cuthill–McKee +/// order, so the system's bandwidth is about one breadth-first front): factored once per pin +/// set, then each global step is two triangular solves. +pub(crate) struct BandChol { + n: usize, + p: usize, + /// Row `i` holds `L[i][i - p ..= i]`. + l: Vec, +} + +impl BandChol { + /// `None` when the band would be too large (or the matrix isn't positive definite). + pub fn new(a: &Csr) -> Option { + let n = a.diag.len(); + let mut p = 0; + for i in 0..n { + for k in a.rows[i]..a.rows[i + 1] { + p = p.max(i.abs_diff(a.cols[k])); + } + } + if n.saturating_mul(p + 1) > 40_000_000 { + return None; + } + let w = p + 1; + let mut l = vec![0.0f64; n * w]; + for i in 0..n { + for k in a.rows[i]..a.rows[i + 1] { + let j = a.cols[k]; + if j <= i { + l[i * w + (j + p - i)] = a.vals[k]; + } + } + } + for i in 0..n { + let lo = i.saturating_sub(p); + for j in lo..=i { + let jlo = j.saturating_sub(p).max(lo); + let mut s = l[i * w + (j + p - i)]; + for k in jlo..j { + s -= l[i * w + (k + p - i)] * l[j * w + (k + p - j)]; + } + if i == j { + if s <= 0.0 || !s.is_finite() { + return None; + } + l[i * w + p] = s.sqrt(); + } else { + l[i * w + (j + p - i)] = s / l[j * w + p]; + } + } + } + Some(BandChol { n, p, l }) + } + + #[allow(clippy::needless_range_loop)] // band index arithmetic reads clearer with indices + pub fn solve(&self, b: &[f64], x: &mut [f64]) { + let (n, p, w) = (self.n, self.p, self.p + 1); + let mut y = b.to_vec(); + for i in 0..n { + let mut s = y[i]; + for k in i.saturating_sub(p)..i { + s -= self.l[i * w + (k + p - i)] * y[k]; + } + y[i] = s / self.l[i * w + p]; + } + for i in (0..n).rev() { + let mut s = y[i]; + for k in i + 1..(i + p + 1).min(n) { + s -= self.l[k * w + (i + p - k)] * x[k]; + } + x[i] = s / self.l[i * w + p]; + } + } +} + +/// Preconditioned conjugate gradients on the SPD system `A x = b`, warm-started from `x`. +/// `a(v, out)` writes `A·v` and `m(r, out)` writes `M⁻¹·r`. Stops when `‖r‖ ≤ tol·‖b‖`, when the +/// system stops being usable (non-finite or non-positive curvature), or after `max_iter` +/// iterations. Returns the iterations used. +pub(crate) fn pcg(a: impl Fn(&[f64], &mut [f64]), m: impl Fn(&[f64], &mut [f64]), b: &[f64], x: &mut [f64], tol: f64, max_iter: usize) -> usize { + let dot = |u: &[f64], v: &[f64]| -> f64 { u.iter().zip(v).map(|(p, q)| p * q).sum() }; + let n = b.len().min(x.len()); + let mut r = vec![0.0; n]; + a(x, &mut r); + for (ri, bi) in r.iter_mut().zip(b) { + *ri = bi - *ri; + } + let stop = tol * dot(b, b).sqrt().max(1e-30); + let mut z = vec![0.0; n]; + m(&r, &mut z); + let mut p = z.clone(); + let mut rz = dot(&r, &z); + let mut ap = vec![0.0; n]; + for it in 0..max_iter { + if dot(&r, &r).sqrt() <= stop || rz <= 1e-300 || !rz.is_finite() { + return it; + } + a(&p, &mut ap); + let pap = dot(&p, &ap); + if pap <= 1e-300 || !pap.is_finite() { + return it; + } + let alpha = rz / pap; + for i in 0..n { + x[i] += alpha * p[i]; + r[i] -= alpha * ap[i]; + } + m(&r, &mut z); + let rz2 = dot(&r, &z); + let beta = rz2 / rz; + rz = rz2; + for i in 0..n { + p[i] = z[i] + beta * p[i]; + } + } + max_iter +} + /// Runs `f(y, row)` for every row of a `w × h × ch` buffer, in parallel on native targets. pub(crate) fn par_rows(buf: &mut [T], w: usize, ch: usize, f: impl Fn(usize, &mut [T]) + Sync + Send) { let stride = (w * ch).max(1); @@ -159,15 +304,21 @@ pub(crate) fn par_map(n: usize, f: impl Fn(usize) -> T + Sync + Send) - (0..n).map(f).collect() } +/// Advances `state` one SplitMix64 step and returns the mixed output. +pub(crate) fn splitmix64(state: &mut u64) -> u64 { + *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = *state; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) +} + /// Deterministic 64-bit generator (SplitMix64). pub(crate) struct Rng(pub u64); impl Rng { + /// The next value of the SplitMix64 sequence. pub fn next(&mut self) -> u64 { - self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); - let mut z = self.0; - z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); - z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); - z ^ (z >> 31) + splitmix64(&mut self.0) } pub fn below(&mut self, n: usize) -> usize { (self.next() % n.max(1) as u64) as usize @@ -183,15 +334,7 @@ pub(crate) fn hash01(x: i64, y: i64, seed: u64) -> f32 { (z >> 40) as f32 / (1u64 << 24) as f32 } -/// sRGB transfer function, decode (extended linearly below 0 and as a power above 1). -pub(crate) fn srgb_to_linear(v: f32) -> f32 { - if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } -} - -/// sRGB transfer function, encode. -pub(crate) fn linear_to_srgb(v: f32) -> f32 { - if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } -} +pub(crate) use photocraft_color::convert::{linear_to_srgb, srgb_to_linear}; /// Bilinear sample of channel `c` of a `w × h × ch` buffer, clamped to the edges. pub(crate) fn bilinear(buf: &[f32], w: usize, h: usize, ch: usize, c: usize, x: f32, y: f32) -> f32 { @@ -217,23 +360,86 @@ pub(crate) fn catmull_rom(t: f32) -> [f32; 4] { mod tests { use super::*; + /// The shared step and [`Rng`] reproduce the inlined copies they replaced, bit for bit. #[test] - fn dense_and_sparse_solvers_agree() { + fn splitmix64_output_is_unchanged() { + /// The SplitMix64 step as each generator inlined it before. + fn inlined(state: &mut u64) -> u64 { + *state = state.wrapping_add(0x9E37_79B9_7F4A_7C15); + let mut z = *state; + z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); + z ^ (z >> 31) + } + let mut zero = 0; + assert_eq!([0; 3].map(|_| splitmix64(&mut zero)), [0xE220_A839_7B1D_CDAF, 0x6E78_9E6A_A1B9_65F4, 0x06C4_5D18_8009_454F]); + for seed in [0, 1, 42, 0x2545_F491_4F6C_DD1D, u64::MAX] { + let (mut shared, mut copy, mut rng) = (seed, seed, Rng(seed)); + for _ in 0..10_000 { + let want = inlined(&mut copy); + assert_eq!(splitmix64(&mut shared), want); + assert_eq!(rng.next(), want); + } + } + } + + /// The shared sRGB functions match the removed copies bit for bit. + #[test] + fn srgb_transfer_output_is_unchanged() { + let decode = |v: f32| if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }; + let encode = |v: f32| if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 }; + for i in -2000..=6000 { + let v = i as f32 / 4000.0; + assert_eq!(srgb_to_linear(v).to_bits(), decode(v).to_bits(), "decode {v}"); + assert_eq!(linear_to_srgb(v).to_bits(), encode(v).to_bits(), "encode {v}"); + } + } + + #[test] + fn dense_solver_fits_a_line_and_rejects_singular_systems() { // Overdetermined line fit y = 2x + 1. let mut ne = Normal::new(2); - let mut sp = SparseLs::new(2); for x in 0..10 { - let y = 2.0 * x as f64 + 1.0; - ne.add(&[(0, x as f64), (1, 1.0)], y, 1.0); - sp.add(vec![(0, x as f64), (1, 1.0)], y, 1.0); + ne.add(&[(0, x as f64), (1, 1.0)], 2.0 * x as f64 + 1.0, 1.0); } let a = ne.solve().unwrap(); - let b = sp.solve(&[0.0, 0.0], 50); assert!((a[0] - 2.0).abs() < 1e-9 && (a[1] - 1.0).abs() < 1e-9); - assert!((b[0] - 2.0).abs() < 1e-6 && (b[1] - 1.0).abs() < 1e-6, "{b:?}"); assert!(solve_dense(2, vec![1.0, 2.0, 2.0, 4.0], vec![1.0, 2.0]).is_none()); } + /// A tridiagonal SPD system: identity preconditioning needs many iterations, the exact + /// banded Cholesky preconditioner one or two; both match the dense solve. + #[test] + fn pcg_matches_the_dense_solve_and_an_exact_preconditioner_converges_at_once() { + let n = 40; + let mut trip = Vec::new(); + for i in 0..n { + trip.push((i, i, 2.5)); + if i + 1 < n { + trip.push((i, i + 1, -1.0)); + trip.push((i + 1, i, -1.0)); + } + } + let k = Csr::from_triplets(n, trip); + let mut dense = vec![0.0; n * n]; + for i in 0..n { + for e in k.rows[i]..k.rows[i + 1] { + dense[i * n + k.cols[e]] = k.vals[e]; + } + } + let b: Vec = (0..n).map(|i| (i as f64 * 0.7).sin()).collect(); + let want = solve_dense(n, dense, b.clone()).unwrap(); + let mul = |v: &[f64], out: &mut [f64]| k.mul(v, out); + let mut x = vec![0.0; n]; + let slow = pcg(mul, |r, z| z.copy_from_slice(r), &b, &mut x, 1e-12, 500); + assert!(x.iter().zip(&want).all(|(a, b)| (a - b).abs() < 1e-8), "identity-preconditioned result differs"); + let chol = BandChol::new(&k).unwrap(); + let mut y = vec![0.0; n]; + let fast = pcg(mul, |r, z| chol.solve(r, z), &b, &mut y, 1e-12, 500); + assert!(y.iter().zip(&want).all(|(a, b)| (a - b).abs() < 1e-8), "cholesky-preconditioned result differs"); + assert!(fast <= 2 && slow > fast, "iterations: exact {fast}, identity {slow}"); + } + #[test] fn srgb_round_trip_and_sampling() { for v in [0.0f32, 0.01, 0.2, 0.5, 1.0] { diff --git a/crates/algo/src/pixelate.rs b/crates/algo/src/pixelate.rs index df6edb5fff..12eb232bb2 100644 --- a/crates/algo/src/pixelate.rs +++ b/crates/algo/src/pixelate.rs @@ -5,6 +5,7 @@ use photocraft_geom::Rect; +pub(crate) mod cells; mod facet; #[cfg(test)] mod reference; @@ -120,7 +121,7 @@ pub(crate) fn color_halftone(src: &Image, out: Rect, ctx: &Ctx, max_radius: f32, /// being distressed) keeps hard edges and its weight instead of greying where cells straddle /// an edge. A cell whose point falls outside the image takes the (premultiplied) average /// colour of its pixels. -pub(crate) fn crystallize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32) -> Vec { +fn crystallize_direct(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32) -> Vec { let n = src.ch; let cell = cell_size.max(2.0); let b = ctx.bounds; @@ -320,7 +321,7 @@ pub(crate) fn mezzotint(src: &Image, out: Rect, ctx: &Ctx, kind: MezzotintType, /// Pointillize: randomly sized dots, coloured from the image at their centre, /// on a canvas of the background colour. -pub(crate) fn pointillize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32, background: [f32; 4]) -> Vec { +fn pointillize_direct(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32, background: [f32; 4]) -> Vec { let n = src.ch; let cell = cell_size.max(2.0); let b = ctx.bounds; @@ -374,3 +375,11 @@ pub(crate) fn pointillize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, see } res } + +pub(crate) fn crystallize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32) -> Vec { + cells::crystallize(src, out, ctx, cell_size, seed).unwrap_or_else(|| crystallize_direct(src, out, ctx, cell_size, seed)) +} + +pub(crate) fn pointillize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32, background: [f32; 4]) -> Vec { + cells::pointillize(src, out, ctx, cell_size, seed, background).unwrap_or_else(|| pointillize_direct(src, out, ctx, cell_size, seed, background)) +} diff --git a/crates/algo/src/pixelate/cells.rs b/crates/algo/src/pixelate/cells.rs new file mode 100644 index 0000000000..0d88fbc77e --- /dev/null +++ b/crates/algo/src/pixelate/cells.rs @@ -0,0 +1,596 @@ +//! Reuse the jittered lattice in cell space. Raster work stays in the original +//! row/candidate order, including f32 ties and boundary-cell average additions. +use super::{Ctx, Image, MAXC, Rect, cell_point, hash01, native, ncol, subtractive}; + +#[derive(Clone, Copy)] +struct Site { + point: (f32, f32), + color: [f32; MAXC], + radius: f32, + sampled: bool, +} + +struct Grid { + x0: i32, + y0: i32, + width: usize, + sites: Vec, +} + +fn key(p: i32, origin: f32, cell: f32) -> i32 { + ((p as f32 + 0.5 - origin) / cell).floor() as i32 +} + +impl Grid { + fn new(src: &Image, area: Rect, ctx: &Ctx, cell: f32, seed: u32, dots: bool) -> Option { + if src.data.len() != (src.rect.width() as usize).checked_mul(src.rect.height() as usize)?.checked_mul(src.ch)? { + return None; + } + Self::new_with(area, ctx, src.ch, cell, seed, dots, |sx, sy, color| { + for (c, value) in color.iter_mut().enumerate() { + *value = src.get(sx, sy, c); + } + src.rect.contains(sx, sy) && ctx.bounds.contains(sx, sy) + }) + } + + #[allow(clippy::too_many_arguments)] + fn new_with(area: Rect, ctx: &Ctx, n: usize, cell: f32, seed: u32, dots: bool, sample: impl Fn(i32, i32, &mut [f32]) -> bool) -> Option { + if area.is_empty() || !cell.is_finite() || cell < 2.0 || n == 0 || n > MAXC { + return None; + } + let origin = (ctx.bounds.x0 as f32, ctx.bounds.y0 as f32); + let x0 = key(area.x0, origin.0, cell).checked_sub(1)?; + let y0 = key(area.y0, origin.1, cell).checked_sub(1)?; + let x1 = key(area.x1.checked_sub(1)?, origin.0, cell).checked_add(1)?; + let y1 = key(area.y1.checked_sub(1)?, origin.1, cell).checked_add(1)?; + let width = usize::try_from(x1.checked_sub(x0)?.checked_add(1)?).ok()?; + let height = usize::try_from(y1.checked_sub(y0)?.checked_add(1)?).ok()?; + let len = width.checked_mul(height)?; + // Optional tile-local scratch, at most 16 MiB even for a direct kernel call. + if len.checked_mul(std::mem::size_of::())? > 16 * 1024 * 1024 { + return None; + } + let mut sites = Vec::new(); + sites.try_reserve_exact(len).ok()?; + let cc = ncol(ctx, n); + let sub = subtractive(ctx); + for gy in y0..=y1 { + for gx in x0..=x1 { + let point = cell_point(gx, gy, cell, seed, origin); + let (sx, sy) = (point.0.floor() as i32, point.1.floor() as i32); + let mut color = [0.0; MAXC]; + let sampled = sample(sx, sy, color.get_mut(..n)?); + let radius = if dots { + let j = (hash01(gx, gy, 17, seed) - 0.5) * 0.08; + for value in color.iter_mut().take(cc) { + *value = (*value + if sub { -j } else { j }).clamp(0.0, 1.0); + } + cell * (0.45 + 0.35 * hash01(gx, gy, 13, seed)) + } else { + 0.0 + }; + sites.push(Site { point, color, radius, sampled }); + } + } + Some(Self { x0, y0, width, sites }) + } + + fn neighbours(&self, cx: i32, cy: i32) -> Option<[usize; 9]> { + let x = usize::try_from(cx.checked_sub(1)?.checked_sub(self.x0)?).ok()?; + let y = usize::try_from(cy.checked_sub(1)?.checked_sub(self.y0)?).ok()?; + if x.checked_add(2)? >= self.width { + return None; + } + let start = y.checked_mul(self.width)?.checked_add(x)?; + let last = start.checked_add(2 * self.width)?.checked_add(2)?; + self.sites.get(last)?; + Some(std::array::from_fn(|i| start + i / 3 * self.width + i % 3)) + } +} + +/// Contiguous x runs with the same base cell. Evaluate the old f32 mapping once +/// per column, rather than use an inverse mapping that could round differently. +fn runs(area: Rect, origin: f32, cell: f32) -> Vec<(i32, i32, i32)> { + let mut runs: Vec<(i32, i32, i32)> = Vec::new(); + for x in area.x0..area.x1 { + let cx = key(x, origin, cell); + if let Some(last) = runs.last_mut() + && last.0 == cx + { + last.2 = x + 1; + } else { + runs.push((cx, x, x + 1)); + } + } + runs +} + +fn nearest(grid: &Grid, neighbours: &[usize; 9], x: i32, y: i32) -> Option { + let (fx, fy) = (x as f32 + 0.5, y as f32 + 0.5); + let mut best = (f32::MAX, *neighbours.get(4)?); + for &i in neighbours { + let p = grid.sites.get(i)?.point; + let d = (p.0 - fx).powi(2) + (p.1 - fy).powi(2); + if d < best.0 { + best = (d, i); + } + } + Some(best.1) +} + +/// Bounds on the *same f32 score* used by the scalar comparison. Along a +/// scanline, squared x distance has its maximum at an endpoint and its lower +/// bound at the clamped site centre. IEEE subtraction, square, addition and +/// sqrt are monotone on these nonnegative inputs, so these bounds also cover +/// rounding. No estimated bisectors or epsilon decide ownership. +fn score_range(site: &Site, x0: i32, x1: i32, y: i32) -> (f32, f32) { + let left = x0 as f32 + 0.5; + let right = (x1 - 1) as f32 + 0.5; + let dy2 = (site.point.1 - (y as f32 + 0.5)).powi(2); + let near = (site.point.0 - site.point.0.clamp(left, right)).powi(2) + dy2; + let far = (site.point.0 - left).powi(2).max((site.point.0 - right).powi(2)) + dy2; + if DOTS { (site.radius - far.sqrt(), site.radius - near.sqrt()) } else { (-far, -near) } +} + +fn score(site: &Site, x: i32, y: i32) -> f32 { + let d = (site.point.0 - (x as f32 + 0.5)).powi(2) + (site.point.1 - (y as f32 + 0.5)).powi(2); + if DOTS { site.radius - d.sqrt() } else { -d } +} + +/// Certify a whole run when one candidate's worst score beats every other's +/// best. Ambiguous runs split down to scalar pixels. Depth is bounded by the +/// output tile width (at most 11 splits for a 2048-pixel application tile). +fn certified_span(grid: &Grid, ids: &[usize; 9], x0: i32, y: i32, owners: &mut [u32], depths: &mut [f32]) -> Option<()> { + let len = owners.len(); + if len == 0 { + return Some(()); + } + let x1 = x0.checked_add(i32::try_from(len).ok()?)?; + let middle = x0 + (len / 2) as i32; + let mut best = (if DOTS { -0.5 } else { -f32::MAX }, None); + for &i in ids { + let value = score::(grid.sites.get(i)?, middle, y); + if value > best.0 { + best = (value, Some(i)); + } + } + if let Some(i) = best.1 { + let site = grid.sites.get(i)?; + let lower = score_range::(site, x0, x1, y).0; + let certified = (!DOTS || lower > -0.5) + && ids.iter().filter(|&&j| j != i).all(|&j| grid.sites.get(j).is_some_and(|other| lower > score_range::(other, x0, x1, y).1)); + if len == 1 || certified { + owners.fill(i as u32); + if DOTS { + if lower >= 0.5 { + depths.fill(0.5); + } else { + for (offset, depth) in depths.iter_mut().enumerate() { + *depth = score::(site, x0 + offset as i32, y); + } + } + } + return Some(()); + } + } else if len == 1 || ids.iter().all(|&i| grid.sites.get(i).is_some_and(|site| score_range::(site, x0, x1, y).1 <= -0.5)) { + owners.fill(u32::MAX); + return Some(()); + } + let half = len / 2; + let (left, right) = owners.split_at_mut(half); + let (ld, rd) = depths.split_at_mut(half); + certified_span::(grid, ids, x0, y, left, ld)?; + certified_span::(grid, ids, x0 + half as i32, y, right, rd) +} + +pub(super) fn crystallize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32) -> Option> { + let cell = cell_size.max(2.0); + let n = src.ch; + let b = ctx.bounds; + // Match the scalar averaging window, including floor/ceil at negative origins. + // Decline extreme geometry before its i32 arithmetic; the scalar fallback owns it. + let c0 = ( + (((i64::from(out.x0) - i64::from(b.x0)) as f32 / cell).floor() as i32).checked_sub(2)?, + (((i64::from(out.y0) - i64::from(b.y0)) as f32 / cell).floor() as i32).checked_sub(2)?, + ); + let c1 = ( + (((i64::from(out.x1) - i64::from(b.x0)) as f32 / cell).floor() as i32).checked_add(2)?, + (((i64::from(out.y1) - i64::from(b.y0)) as f32 / cell).floor() as i32).checked_add(2)?, + ); + let win = Rect::new( + b.x0.checked_add((c0.0 as f32 * cell).floor() as i32)?, + b.y0.checked_add((c0.1 as f32 * cell).floor() as i32)?, + b.x0.checked_add((c1.0.checked_add(1)? as f32 * cell).ceil() as i32)?, + b.y0.checked_add((c1.1.checked_add(1)? as f32 * cell).ceil() as i32)?, + ) + .intersect(&src.rect) + .intersect(&b); + let mut grid = Grid::new(src, out.union(&win), ctx, cell, seed, false)?; + let origin = (b.x0 as f32, b.y0 as f32); + if grid.sites.iter().any(|s| !s.sampled) { + let mut acc = Vec::new(); + let count = grid.sites.len().checked_mul(n.checked_add(1)?)?; + acc.try_reserve_exact(count).ok()?; + acc.resize(count, 0.0f32); + let columns = runs(win, origin.0, cell); + for y in win.y0..win.y1 { + let cy = key(y, origin.1, cell); + for &(cx, x0, x1) in &columns { + let neighbours = grid.neighbours(cx, cy)?; + // An interior cell's average is never used. Skip the entire run if + // all nine possible winners have an on-image colour sample. + if neighbours.iter().all(|&i| grid.sites.get(i).is_some_and(|s| s.sampled)) { + continue; + } + for x in x0..x1 { + let i = nearest(&grid, &neighbours, x, y)?; + if grid.sites.get(i)?.sampled { + continue; + } + let gx = grid.x0.checked_add((i % grid.width) as i32)?; + let gy = grid.y0.checked_add((i / grid.width) as i32)?; + if gx < c0.0 || gy < c0.1 || gx > c1.0 || gy > c1.1 { + continue; + } + let sum = acc.get_mut(i.checked_mul(n + 1)?..i.checked_add(1)?.checked_mul(n + 1)?)?; + let alpha = if ctx.alpha { src.get(x, y, n - 1) } else { 1.0 }; + for (c, value) in sum.iter_mut().enumerate().take(n) { + let s = src.get(x, y, c); + *value += if ctx.alpha && c + 1 < n { s * alpha } else { s }; + } + *sum.get_mut(n)? += 1.0; + } + } + } + for (site, sum) in grid.sites.iter_mut().zip(acc.chunks_exact(n + 1)) { + let count = *sum.get(n)?; + if !site.sampled && count > 0.0 { + for (dst, value) in site.color.iter_mut().zip(sum).take(n) { + *dst = *value / count; + } + crate::fxutil::unpremul_px(site.color.get_mut(..n)?, ctx.alpha); + site.sampled = true; + } + } + } + crystallize_grid(src, out, ctx, cell, &grid) +} + +fn crystallize_grid(src: &Image, out: Rect, ctx: &Ctx, cell: f32, grid: &Grid) -> Option> { + let n = src.ch; + let b = ctx.bounds; + let origin = (b.x0 as f32, b.y0 as f32); + let mut result = if src.rect == out { src.data.clone() } else { src.crop(out) }; + let width = out.width() as usize; + let stride = width.checked_mul(n)?; + let columns = runs(out, origin.0, cell); + let mut distances = vec![f32::MAX; width]; + let mut owners = vec![0u32; width]; + for (row, pixels) in result.chunks_exact_mut(stride).enumerate() { + let y = out.y0.checked_add(i32::try_from(row).ok()?)?; + let fy = y as f32 + 0.5; + let cy = key(y, origin.1, cell); + distances.fill(f32::MAX); + for &(cx, x0, x1) in &columns { + let neighbours = grid.neighbours(cx, cy)?; + let start = usize::try_from(x0.checked_sub(out.x0)?).ok()?; + let end = usize::try_from(x1.checked_sub(out.x0)?).ok()?; + if cell >= 32.0 && out.width() <= 2048 { + certified_span::(grid, &neighbours, x0, y, owners.get_mut(start..end)?, distances.get_mut(start..end)?)?; + continue; + } + owners.get_mut(start..end)?.fill(*neighbours.get(4)? as u32); + for &i in &neighbours { + let site = grid.sites.get(i)?; + let dy2 = (site.point.1 - fy).powi(2); + for (offset, (distance, owner)) in distances.get_mut(start..end)?.iter_mut().zip(owners.get_mut(start..end)?).enumerate() { + let fx = (x0 + offset as i32) as f32 + 0.5; + let d = (site.point.0 - fx).powi(2) + dy2; + if d < *distance { + *distance = d; + *owner = i as u32; + } + } + } + } + for (x, (pixel, &owner)) in pixels.chunks_exact_mut(n).zip(&owners).enumerate() { + if !b.contains(out.x0 + x as i32, y) { + continue; + } + let site = grid.sites.get(owner as usize)?; + if site.sampled { + pixel.copy_from_slice(site.color.get(..n)?); + } + } + } + Some(result) +} + +/// A site may only compete in its own base column and its two neighbours. +/// Derive the inclusive pixel runs from the exact forward f32 cell mapping. +fn influence_columns(out: Rect, origin: f32, cell: f32, grid: &Grid) -> Option> { + let columns = runs(out, origin, cell); + (0..grid.width) + .map(|column| { + let gx = grid.x0.checked_add(i32::try_from(column).ok()?)?; + let lo = columns.partition_point(|&(cx, _, _)| cx < gx - 1); + let hi = columns.partition_point(|&(cx, _, _)| cx <= gx + 1); + if lo == hi { + return Some((out.x0, out.x0)); + } + Some((columns.get(lo)?.1, columns.get(hi - 1)?.2)) + }) + .collect() +} + +pub(super) fn pointillize(src: &Image, out: Rect, ctx: &Ctx, cell_size: f32, seed: u32, background: [f32; 4]) -> Option> { + let cell = cell_size.max(2.0); + // This path uses integer pixel centres and conservative one-pixel support + // margins. Beyond exact half-integer f32 coordinates use the scalar kernel. + if cell > 4096.0 || [out.x0, out.y0, out.x1, out.y1].iter().any(|v| v.unsigned_abs() > 4_000_000) { + return None; + } + let grid = Grid::new(src, out, ctx, cell, seed, true)?; + pointillize_grid(src, out, ctx, cell, background, &grid) +} + +fn pointillize_grid(src: &Image, out: Rect, ctx: &Ctx, cell: f32, background: [f32; 4], grid: &Grid) -> Option> { + let origin = (ctx.bounds.x0 as f32, ctx.bounds.y0 as f32); + let bg = native(ctx, background); + let n = src.ch; + let mut result = if src.rect == out { src.data.clone() } else { src.crop(out) }; + let columns = influence_columns(out, origin.0, cell, grid)?; + let base_runs = runs(out, origin.0, cell); + let width = out.width() as usize; + let stride = width.checked_mul(n)?; + let mut depths = vec![-0.5f32; width]; + let mut owners = vec![u32::MAX; width]; + for (row, pixels) in result.chunks_exact_mut(stride).enumerate() { + let y = out.y0.checked_add(i32::try_from(row).ok()?)?; + let cy = key(y, origin.1, cell); + depths.fill(-0.5); + owners.fill(u32::MAX); + if cell >= 32.0 && out.width() <= 2048 { + certified_row(grid, &base_runs, out, cy, y, &mut owners, &mut depths)?; + } else { + scan_row(grid, &columns, out, cy, y, &mut owners, &mut depths)?; + } + composite_row(pixels, &depths, &owners, grid, ctx, bg)?; + } + Some(result) +} + +/// Resolve one output row's dot owners through the certified run spans. +fn certified_row(grid: &Grid, base_runs: &[(i32, i32, i32)], out: Rect, cy: i32, y: i32, owners: &mut [u32], depths: &mut [f32]) -> Option<()> { + for &(cx, x0, x1) in base_runs { + let ids = grid.neighbours(cx, cy)?; + let start = usize::try_from(x0.checked_sub(out.x0)?).ok()?; + let end = usize::try_from(x1.checked_sub(out.x0)?).ok()?; + certified_span::(grid, &ids, x0, y, owners.get_mut(start..end)?, depths.get_mut(start..end)?)?; + } + Some(()) +} + +/// Resolve one output row's dot owners by scanning the 3x3 neighbouring cells. +fn scan_row(grid: &Grid, columns: &[(i32, i32)], out: Rect, cy: i32, y: i32, owners: &mut [u32], depths: &mut [f32]) -> Option<()> { + let fy = y as f32 + 0.5; + for gy in cy.checked_sub(1)?..=cy.checked_add(1)? { + let row = usize::try_from(gy.checked_sub(grid.y0)?).ok()?.checked_mul(grid.width)?; + // Global gy/gx order is the original dy/dx tie order at every pixel. + for (column, &span) in columns.iter().enumerate() { + if span.0 >= span.1 { + continue; + } + let i = row.checked_add(column)?; + scan_site(grid, i, fy, span, out, owners, depths)?; + } + } + Some(()) +} + +/// Let one site claim the pixels of the current row it reaches within `[x0, x1)`. +fn scan_site(grid: &Grid, i: usize, fy: f32, (x0, x1): (i32, i32), out: Rect, owners: &mut [u32], depths: &mut [f32]) -> Option<()> { + let site = grid.sites.get(i)?; + let dy2 = (site.point.1 - fy).powi(2); + let reach = f64::from(site.radius) + 1.5; + let square = reach * reach - f64::from(dy2); + if square < 0.0 { + return Some(()); + } + // The extra pixel covers subtraction/square/sqrt rounding for + // the bounded coordinates/radii above. The exact old depth + // comparison still decides every candidate that survives. + let reach_x = square.max(0.0).sqrt() + 1.0; + let lo = ((f64::from(site.point.0) - 0.5 - reach_x).floor() as i32).max(x0); + let hi = ((f64::from(site.point.0) - 0.5 + reach_x).ceil() as i32).min(x1); + if lo >= hi { + return Some(()); + } + let start = usize::try_from(lo.checked_sub(out.x0)?).ok()?; + let end = usize::try_from(hi.checked_sub(out.x0)?).ok()?; + for (offset, (depth, owner)) in depths.get_mut(start..end)?.iter_mut().zip(owners.get_mut(start..end)?).enumerate() { + let fx = (lo + offset as i32) as f32 + 0.5; + let d = ((site.point.0 - fx).powi(2) + dy2).sqrt(); + let candidate = site.radius - d; + if candidate > *depth { + *depth = candidate; + *owner = i as u32; + } + } + Some(()) +} + +/// Blend each pixel of a row with the background and its owning dot colour. +fn composite_row(pixels: &mut [f32], depths: &[f32], owners: &[u32], grid: &Grid, ctx: &Ctx, bg: [f32; 8]) -> Option<()> { + let n = pixels.len() / depths.len().max(1); + for ((pixel, &depth), &owner) in pixels.chunks_exact_mut(n).zip(depths).zip(owners) { + let a_orig = if ctx.alpha { *pixel.get(n - 1)? } else { 1.0 }; + let mut canvas = bg; + if ctx.alpha { + *canvas.get_mut(n - 1)? = a_orig.max(bg.get(n - 1)?.min(1.0)); + } + if owner != u32::MAX { + let i = owner as usize; + let k = (depth + 0.5).clamp(0.0, 1.0); + let dot = grid.sites.get(i)?; + for (c, value) in canvas.iter_mut().enumerate().take(n) { + *value += (*dot.color.get(c)? - *value) * k; + } + } + pixel.copy_from_slice(canvas.get(..n)?); + } + Some(()) +} + +/// The filters sample cell centres, not a dense neighbourhood. On ordinary +/// tiles read those sparse samples directly; keep only the original output +/// pixels for alpha and selection mixing. Crystallize boundary tiles still +/// use the dense path, whose row-order f32 averages are part of its result. +#[allow(clippy::too_many_arguments)] +pub(crate) fn tile( + surface: &photocraft_raster::Surface, + out: Rect, + ctx: &Ctx, + cell: f32, + seed: u32, + background: Option<[f32; 4]>, + extent: Option, +) -> Option<(Image, Vec)> { + if !cell.is_finite() + || !(2.0..=300.0).contains(&cell) + || out.is_empty() + || out.width() > 2048 + || out.height() > 2048 + || [out.x0, out.y0, out.x1, out.y1].iter().any(|v| v.unsigned_abs() > 4_000_000) + || extent.is_some_and(|e| e.is_empty() || out.intersect(&e) != out) + { + return None; + } + let dots = background.is_some(); + let grid = Grid::new_with(out, ctx, surface.channels(), cell, seed, dots, |sx, sy, color| { + let (x, y) = match extent { + Some(e) => (sx.clamp(e.x0, e.x1 - 1), sy.clamp(e.y0, e.y1 - 1)), + None => (sx, sy), + }; + surface.read_pixel(x, y, color); + ctx.bounds.contains(sx, sy) + })?; + if !dots && grid.sites.iter().any(|s| !s.sampled) { + return None; + } + let original = Image::read(surface, out); + let data = match background { + Some(bg) => pointillize_grid(&original, out, ctx, cell, bg, &grid)?, + None => crystallize_grid(&original, out, ctx, cell, &grid)?, + }; + Some((original, data)) +} + +/// Same rounding and native byte order as Surface::write_region, before the +/// worker hands its result back. Bounds the allocation and retains float data +/// if an optional encode buffer cannot be reserved. +pub(crate) fn encode(format: photocraft_color::PixelFormat, samples: &[f32]) -> Option> { + use photocraft_color::SampleType; + let mut bytes = Vec::new(); + let len = samples.len().checked_mul(format.sample.bytes())?; + bytes.try_reserve_exact(len).ok()?; + bytes.resize(len, 0); + match format.sample { + SampleType::U8 => { + for (dst, value) in bytes.iter_mut().zip(samples) { + *dst = (value.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + } + SampleType::U16 => { + for (dst, value) in bytes.as_chunks_mut::<2>().0.iter_mut().zip(samples) { + dst.copy_from_slice(&((value.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16).to_ne_bytes()); + } + } + SampleType::F32 => { + for (dst, value) in bytes.as_chunks_mut::<4>().0.iter_mut().zip(samples) { + dst.copy_from_slice(&value.to_ne_bytes()); + } + } + } + Some(bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + use photocraft_color::{ColorMode, PixelFormat, SampleType}; + use photocraft_raster::Surface; + + #[test] + fn span_certificates_keep_discrete_winners_and_antialias_coverage() { + for origin in [-16_777_210, -3_999_000, -37, 0, 3_999_000, 16_777_210] { + let out = Rect::new(origin, -3, origin + 601, 4); + let ctx = Ctx { bounds: out, mode: ColorMode::Rgb, alpha: true }; + let src = Image::new(out, 4); + for cell in [32.0, 33.25, 100.0, 300.0, 4096.0] { + for seed in 0..32 { + let grid = Grid::new(&src, out, &ctx, cell, seed, true).unwrap(); + for y in out.y0..out.y1 { + let cy = key(y, ctx.bounds.y0 as f32, cell); + for (cx, x0, x1) in runs(out, ctx.bounds.x0 as f32, cell) { + let ids = grid.neighbours(cx, cy).unwrap(); + for dots in [false, true] { + let mut owners = vec![u32::MAX; (x1 - x0) as usize]; + let mut depths = vec![-0.5; owners.len()]; + if dots { + certified_span::(&grid, &ids, x0, y, &mut owners, &mut depths).unwrap(); + } else { + certified_span::(&grid, &ids, x0, y, &mut owners, &mut depths).unwrap(); + } + for (offset, (&owner, &depth)) in owners.iter().zip(&depths).enumerate() { + let x = x0 + offset as i32; + let mut best = (if dots { -0.5 } else { -f32::MAX }, u32::MAX); + for &i in &ids { + let value = if dots { score::(&grid.sites[i], x, y) } else { score::(&grid.sites[i], x, y) }; + if value > best.0 { + best = (value, i as u32); + } + } + assert_eq!(owner, best.1, "x={x} y={y} cell={cell} seed={seed}"); + if dots && owner != u32::MAX { + assert_eq!((depth + 0.5).clamp(0.0, 1.0).to_bits(), (best.0 + 0.5).clamp(0.0, 1.0).to_bits()); + } + } + } + } + } + } + } + } + } + + #[test] + fn worker_encoding_is_surface_write_region_at_every_depth() { + let values = [f32::NAN, f32::NEG_INFINITY, -100.0, -0.0, f32::from_bits(1), 0.5 / 65535.0, 0.5 / 255.0, 0.5, 1.0, 1.1, f32::INFINITY]; + let area = Rect::new(-17, -9, 281, 2); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + for alpha in [false, true] { + let format = PixelFormat::new(mode, sample, alpha); + let data: Vec<_> = (0..area.width() as usize * area.height() as usize * format.channels()).map(|i| values[i % values.len()]).collect(); + let mut surface = Surface::new(format); + surface.write_region(area, &data); + assert_eq!(encode(format, &data).unwrap(), surface.to_interleaved(area)); + } + } + } + } + + #[test] + fn optional_grid_rejects_unbounded_or_incomplete_scratch() { + let area = Rect::new(0, 0, 100_000, 100_000); + let ctx = Ctx { bounds: area, mode: ColorMode::Rgb, alpha: true }; + let image = Image { rect: area, ch: 4, data: Vec::new() }; + assert!(Grid::new(&image, area, &ctx, 2.0, 0, true).is_none()); + assert!(Grid::new_with(area, &ctx, 4, 2.0, 0, true, |_, _, _| false).is_none()); + assert!(Grid::new_with(area, &ctx, 0, 2.0, 0, true, |_, _, _| false).is_none()); + assert!(Grid::new_with(area, &ctx, MAXC + 1, 2.0, 0, true, |_, _, _| false).is_none()); + assert!(Grid::new_with(area, &ctx, 4, f32::INFINITY, 0, true, |_, _, _| false).is_none()); + } +} diff --git a/crates/algo/src/pixelate/tests.rs b/crates/algo/src/pixelate/tests.rs index c82d747326..f0339e19c9 100644 --- a/crates/algo/src/pixelate/tests.rs +++ b/crates/algo/src/pixelate/tests.rs @@ -191,3 +191,79 @@ fn facet_scratch_rejects_large_or_incomplete_storage() { let src = Image { rect: out.inflate(3), ch: 4, data: Vec::new() }; assert!(facet::run(&src, out, &ctx).is_none()); } + +#[test] +fn cell_filters_match_scalar_models_edges_and_fractional_cells() { + let bounds = Rect::new(-19, -13, 32, 29); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab, ColorMode::Multichannel] { + for alpha in [false, true] { + let ctx = Ctx { bounds, mode, alpha }; + for pattern in 0..5 { + for halo in [0, 7] { + let src = fixture(bounds.inflate(halo), &ctx, pattern); + for out in [bounds, Rect::new(-23, -15, 37, 31), Rect::new(-7, 2, 18, 13)] { + for cell in [2.0, 3.75, 10.0, 100.0, 300.0] { + for seed in [0, 37, u32::MAX] { + let got = cells::crystallize(&src, out, &ctx, cell, seed).unwrap(); + assert_bits(&got, &crystallize_direct(&src, out, &ctx, cell, seed)); + for bg in [[1.0; 4], [0.1, 0.8, 0.4, 0.25]] { + let got = cells::pointillize(&src, out, &ctx, cell, seed, bg).unwrap(); + assert_bits(&got, &pointillize_direct(&src, out, &ctx, cell, seed, bg)); + } + } + } + } + } + } + } + } +} + +#[test] +fn cell_filters_tiled_storage_matches_scalar_with_soft_selection() { + use crate::{FilterParams, apply_tiled}; + let bounds = Rect::new(-17, -9, 86, 78); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for alpha in [false, true] { + let ctx = Ctx { bounds, mode, alpha }; + let src = fixture(bounds, &ctx, 2); + let mut surface = Surface::new(PixelFormat::new(mode, depth, alpha)); + surface.write_region(bounds, &src.data); + let mut selection = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false)); + let coverage: Vec<_> = (0..bounds.width() * bounds.height()).map(|i| (i % 7) as f32 / 6.0).collect(); + selection.write_region(bounds, &coverage); + for params in [ + FilterParams::Crystallize { cell_size: 3.75, seed: 37 }, + FilterParams::Crystallize { cell_size: 10.0, seed: 37 }, + FilterParams::Pointillize { cell_size: 3.75, seed: 37, background: [1.0; 4] }, + FilterParams::Pointillize { cell_size: 10.0, seed: 37, background: [0.1, 0.8, 0.4, 0.25] }, + ] { + for extent in [None, Some(bounds)] { + assert!(matches!(params.halo(), crate::Halo::Radius(_))); + let halo = match params.halo() { + crate::Halo::Radius(r) => r, + _ => 0, + }; + let image = match extent { + Some(e) => Image::read_clamped(&surface, bounds.inflate(halo), e), + None => Image::read(&surface, bounds.inflate(halo)), + }; + let mut expected_data = match params { + FilterParams::Crystallize { cell_size, seed } => crystallize_direct(&image, bounds, &ctx, cell_size, seed), + FilterParams::Pointillize { cell_size, seed, background } => pointillize_direct(&image, bounds, &ctx, cell_size, seed, background), + _ => Vec::new(), + }; + crate::mix_selection(&mut expected_data, bounds, &selection, &image, ctx.alpha); + let mut expected = surface.clone(); + expected.write_region(bounds, &expected_data); + for tile in [1, 7, 17, 256] { + let got = apply_tiled(&surface, ¶ms, bounds, bounds, Some(&selection), tile, extent); + assert_bits(&got.read_region(bounds), &expected.read_region(bounds)); + } + } + } + } + } + } +} diff --git a/crates/algo/src/puppet.rs b/crates/algo/src/puppet.rs index c3c5c3693c..a1b8cbd91d 100644 --- a/crates/algo/src/puppet.rs +++ b/crates/algo/src/puppet.rs @@ -26,6 +26,7 @@ use photocraft_geom::Rect; use photocraft_raster::Surface; use serde::{Deserialize, Serialize}; +use crate::photo_util::{BandChol, Csr}; use crate::transform::Interp; #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] @@ -744,170 +745,6 @@ fn band_order(pos: &[[f64; 2]], tris: &[[usize; 3]]) -> Vec { order } -/// A compressed sparse row matrix (symmetric, used by CG). -struct Csr { - rows: Vec, - cols: Vec, - vals: Vec, - diag: Vec, -} - -impl Csr { - fn from_triplets(n: usize, mut t: Vec<(usize, usize, f64)>) -> Csr { - t.sort_by_key(|a| (a.0, a.1)); - let mut rows = vec![0usize; n + 1]; - let mut cols = Vec::with_capacity(t.len()); - let mut vals: Vec = Vec::with_capacity(t.len()); - let mut last = (usize::MAX, usize::MAX); - for (i, j, v) in t { - if (i, j) == last { - *vals.last_mut().unwrap_or(&mut 0.0) += v; - continue; - } - last = (i, j); - cols.push(j); - vals.push(v); - rows[i + 1] = cols.len(); - } - for i in 0..n { - rows[i + 1] = rows[i + 1].max(rows[i]); - } - let mut diag = vec![1.0; n]; - for (i, d) in diag.iter_mut().enumerate() { - for k in rows[i]..rows[i + 1] { - if cols[k] == i && vals[k] > 0.0 { - *d = vals[k]; - } - } - } - Csr { rows, cols, vals, diag } - } - - fn mul(&self, x: &[f64], out: &mut [f64]) { - for (i, o) in out.iter_mut().enumerate() { - let mut s = 0.0; - for k in self.rows[i]..self.rows[i + 1] { - s += self.vals[k] * x[self.cols[k]]; - } - *o = s; - } - } - - /// Jacobi-preconditioned conjugate gradients, warm-started from `x`. - fn solve(&self, b: &[f64], x: &mut [f64]) { - let n = b.len(); - let mut r = vec![0.0; n]; - self.mul(x, &mut r); - for i in 0..n { - r[i] = b[i] - r[i]; - } - let bn: f64 = b.iter().map(|v| v * v).sum::().sqrt().max(1e-30); - let mut z: Vec = r.iter().zip(&self.diag).map(|(r, d)| r / d).collect(); - let mut p = z.clone(); - let mut rz: f64 = r.iter().zip(&z).map(|(a, b)| a * b).sum(); - let mut ap = vec![0.0; n]; - for _ in 0..(4 * n).clamp(50, 2000) { - if r.iter().map(|v| v * v).sum::().sqrt() <= 1e-12 * bn { - break; - } - self.mul(&p, &mut ap); - let pap: f64 = p.iter().zip(&ap).map(|(a, b)| a * b).sum(); - if pap.abs() < 1e-300 { - break; - } - let alpha = rz / pap; - for i in 0..n { - x[i] += alpha * p[i]; - r[i] -= alpha * ap[i]; - } - for i in 0..n { - z[i] = r[i] / self.diag[i]; - } - let rz2: f64 = r.iter().zip(&z).map(|(a, b)| a * b).sum(); - let beta = rz2 / rz; - rz = rz2; - for i in 0..n { - p[i] = z[i] + beta * p[i]; - } - } - } -} - -/// Banded Cholesky factor `A = L Lᵀ` (the mesh numbers its vertices in reverse Cuthill–McKee -/// order, so the system's bandwidth is about one breadth-first front): factored once per pin -/// set, then each global step is two triangular solves. -struct BandChol { - n: usize, - p: usize, - /// Row `i` holds `L[i][i - p ..= i]`. - l: Vec, -} - -impl BandChol { - /// `None` when the band would be too large (or the matrix isn't positive definite). - fn new(a: &Csr) -> Option { - let n = a.diag.len(); - let mut p = 0; - for i in 0..n { - for k in a.rows[i]..a.rows[i + 1] { - p = p.max(i.abs_diff(a.cols[k])); - } - } - if n.saturating_mul(p + 1) > 40_000_000 { - return None; - } - let w = p + 1; - let mut l = vec![0.0f64; n * w]; - for i in 0..n { - for k in a.rows[i]..a.rows[i + 1] { - let j = a.cols[k]; - if j <= i { - l[i * w + (j + p - i)] = a.vals[k]; - } - } - } - for i in 0..n { - let lo = i.saturating_sub(p); - for j in lo..=i { - let jlo = j.saturating_sub(p).max(lo); - let mut s = l[i * w + (j + p - i)]; - for k in jlo..j { - s -= l[i * w + (k + p - i)] * l[j * w + (k + p - j)]; - } - if i == j { - if s <= 0.0 || !s.is_finite() { - return None; - } - l[i * w + p] = s.sqrt(); - } else { - l[i * w + (j + p - i)] = s / l[j * w + p]; - } - } - } - Some(BandChol { n, p, l }) - } - - #[allow(clippy::needless_range_loop)] // band index arithmetic reads clearer with indices - fn solve(&self, b: &[f64], x: &mut [f64]) { - let (n, p, w) = (self.n, self.p, self.p + 1); - let mut y = b.to_vec(); - for i in 0..n { - let mut s = y[i]; - for k in i.saturating_sub(p)..i { - s -= self.l[i * w + (k + p - i)] * y[k]; - } - y[i] = s / self.l[i * w + p]; - } - for i in (0..n).rev() { - let mut s = y[i]; - for k in i + 1..(i + p + 1).min(n) { - s -= self.l[k * w + (i + p - k)] * x[k]; - } - x[i] = s / self.l[i * w + p]; - } - } -} - /// A pin bound to the mesh: barycentric weights on a triangle's vertices. #[derive(Clone, Debug)] struct Bound { diff --git a/crates/algo/src/quantize.rs b/crates/algo/src/quantize.rs index 589c9f196d..0eef6b98ca 100644 --- a/crates/algo/src/quantize.rs +++ b/crates/algo/src/quantize.rs @@ -61,8 +61,15 @@ pub enum Dither { Noise, } +/// `(v.clamp(0, 1) * 255).round() as u8`, without the libm `roundf` call per channel (that call +/// was about a tenth of the palette scan on a large image). `x` is in 0..=255, so truncating +/// with an integer cast and adding one when the exact remainder is at least a half is +/// round-half-away-from-zero, the same as `f32::round`; NaN gives 0 in both. The +/// `to8_matches_round_for_every_float` test checks every input. fn to8(v: f32) -> u8 { - (v.clamp(0.0, 1.0) * 255.0).round() as u8 + let x = v.clamp(0.0, 1.0) * 255.0; + let t = x as u32; + (t + u32::from(x - t as f32 >= 0.5)) as u8 } fn rgb8(p: &[f32; 4]) -> Rgb8 { @@ -166,46 +173,141 @@ fn forced_colors(f: Forced) -> Vec { } } -/// Unique 8-bit colours with counts (opaque pixels only). +/// Unique 8-bit colours with counts (opaque pixels only), sorted by colour. fn histogram(px: &[[f32; 4]]) -> Vec<(Rgb8, u32)> { - let mut m: std::collections::HashMap = std::collections::HashMap::new(); - for p in px.iter().filter(|p| p[3] >= 0.5) { - *m.entry(rgb8(p)).or_default() += 1; + // Pack each opaque pixel into one u32 (r, g, b from the top, so the numeric order is the + // `[u8; 3]` order), sort the plain integers and count the runs. This replaces a SipHash + // `HashMap` insert per pixel: counts are integers, so any order of work gives the same + // result, and equal colours sit next to each other after the sort. One 4-byte value per + // pixel is the only large allocation (no per-thread tables). + let pack = |p: &[f32; 4]| { + let [r, g, b] = rgb8(p); + u32::from(r) << 16 | u32::from(g) << 8 | u32::from(b) + }; + #[cfg(not(target_arch = "wasm32"))] + let keys = { + use rayon::prelude::*; + let mut keys: Vec = px.par_iter().filter(|p| p[3] >= 0.5).map(pack).collect(); + keys.par_sort_unstable(); + keys + }; + #[cfg(target_arch = "wasm32")] + let keys = { + let mut keys: Vec = px.iter().filter(|p| p[3] >= 0.5).map(pack).collect(); + keys.sort_unstable(); + keys + }; + let mut out: Vec<(Rgb8, u32)> = Vec::new(); + for k in keys { + let c = [(k >> 16) as u8, (k >> 8) as u8, k as u8]; + match out.last_mut() { + Some((last, n)) if *last == c => *n = n.saturating_add(1), + _ => out.push((c, 1)), + } + } + out +} + +/// Widest axis of `b` and its split score (weighted extent), or `None` for a box that cannot +/// be split. Every score is bit for bit what the old per-iteration scan gave. +fn split_score(b: &[([f32; 3], u32)]) -> Option<(usize, f32)> { + if b.len() <= 1 { + return None; + } + // One pass per axis, then the weight sum: the same folds the old per-iteration scan ran + // (measured faster than a single fused pass, which the compiler does not vectorise). + let mut best = (0usize, -1.0f32); + for a in 0..3 { + let (lo, hi) = b.iter().fold((f32::MAX, f32::MIN), |(lo, hi), p| (lo.min(p.0[a]), hi.max(p.0[a]))); + if hi - lo > best.1 { + best = (a, hi - lo); + } + } + let w: f32 = b.iter().map(|p| p.1 as f32).sum(); + Some((best.0, best.1 * w.sqrt())) +} + +/// Maps an f32 to a u32 whose unsigned order is exactly `f32::total_cmp` order. +fn total_order_key(v: f32) -> u32 { + let bits = v.to_bits(); + // Negative: flip every bit (reverses their order, puts them below). Positive: set the sign bit. + bits ^ ((((bits as i32) >> 31) as u32) | 0x8000_0000) +} + +/// Stable LSD radix sort of `b` along `axis`, in the order of `f32::total_cmp`. Stable like +/// `sort_by`, so ties keep their original order and the result is element for element the same; +/// it is several times faster than a comparison sort on millions of points. +fn radix_sort_along(b: &mut Vec<([f32; 3], u32)>, axis: usize) { + let n = b.len(); + let Some(&fill) = b.first() else { return }; + if n < 2 { + return; + } + // Three digits of 11, 11 and 10 bits; count all of them in one read. + let mut counts = [[0u32; 2048]; 3]; + for e in b.iter() { + let k = total_order_key(e.0[axis]); + counts[0][(k & 0x7ff) as usize] += 1; + counts[1][((k >> 11) & 0x7ff) as usize] += 1; + counts[2][(k >> 22) as usize] += 1; + } + let mut src = std::mem::take(b); + let mut dst = vec![fill; n]; + for (pass, count) in counts.iter_mut().enumerate() { + // A digit that is the same for every point would not move anything. + if count.iter().any(|&c| c as usize == n) { + continue; + } + let mut sum = 0u32; + for c in count.iter_mut() { + let here = *c; + *c = sum; + sum += here; + } + let shift = 11 * pass as u32; + for e in &src { + let d = ((total_order_key(e.0[axis]) >> shift) & 0x7ff) as usize; + dst[count[d] as usize] = *e; + count[d] += 1; + } + std::mem::swap(&mut src, &mut dst); + } + *b = src; +} + +/// Stable sort of `b` along `axis`. Boxes this small are quicker with `sort_by` than with the +/// radix sort's fixed counting cost. +fn sort_along(b: &mut Vec<([f32; 3], u32)>, axis: usize) { + if b.len() >= 256 { + radix_sort_along(b, axis); + } else { + b.sort_by(|p, q| p.0[axis].total_cmp(&q.0[axis])); } - let mut v: Vec<(Rgb8, u32)> = m.into_iter().collect(); - v.sort_unstable(); - v } /// Median cut over `pts` (feature vectors + weights) into at most `n` boxes; returns box means. -fn median_cut(pts: &[([f32; 3], u32)], n: usize) -> Vec<[f32; 3]> { +pub(crate) fn median_cut(pts: &[([f32; 3], u32)], n: usize) -> Vec<[f32; 3]> { if pts.is_empty() || n == 0 { return Vec::new(); } let mut boxes: Vec> = vec![pts.to_vec()]; + // Each box's (axis, score), kept in lockstep with `boxes` (same swap_remove and pushes). + // Only the two boxes made by a split change, so only those are scanned again; rescanning + // every box on every iteration was most of the time here. + let mut scores: Vec> = vec![split_score(pts)]; while boxes.len() < n { - // Split the box with the largest weighted extent. - let (bi, axis, score) = boxes + // Split the box with the largest weighted extent (first one wins a tie, as before). + let (bi, axis, score) = scores .iter() .enumerate() - .filter(|(_, b)| b.len() > 1) - .map(|(i, b)| { - let mut best = (0usize, -1.0f32); - for a in 0..3 { - let (lo, hi) = b.iter().fold((f32::MAX, f32::MIN), |(lo, hi), p| (lo.min(p.0[a]), hi.max(p.0[a]))); - if hi - lo > best.1 { - best = (a, hi - lo); - } - } - let w: f32 = b.iter().map(|p| p.1 as f32).sum(); - (i, best.0, best.1 * w.sqrt()) - }) + .filter_map(|(i, s)| s.map(|(axis, score)| (i, axis, score))) .fold((usize::MAX, 0, -1.0f32), |acc, x| if x.2 > acc.2 { x } else { acc }); if bi == usize::MAX || score <= 0.0 { break; } let mut b = boxes.swap_remove(bi); - b.sort_by(|p, q| p.0[axis].total_cmp(&q.0[axis])); + scores.swap_remove(bi); + sort_along(&mut b, axis); let total: u64 = b.iter().map(|p| u64::from(p.1)).sum(); let mut acc = 0u64; let mut cut = 1; @@ -217,6 +319,8 @@ fn median_cut(pts: &[([f32; 3], u32)], n: usize) -> Vec<[f32; 3]> { } } let rest = b.split_off(cut); + scores.push(split_score(&b)); + scores.push(split_score(&rest)); boxes.push(b); boxes.push(rest); } @@ -230,7 +334,7 @@ fn median_cut(pts: &[([f32; 3], u32)], n: usize) -> Vec<[f32; 3]> { } /// Lloyd refinement of `centers` over weighted points. -fn kmeans(pts: &[([f32; 3], u32)], centers: &mut [[f32; 3]], iters: usize) { +pub(crate) fn kmeans(pts: &[([f32; 3], u32)], centers: &mut [[f32; 3]], iters: usize) { if centers.is_empty() { // No room left (the forced colours fill the palette): nothing to refine. return; @@ -239,8 +343,18 @@ fn kmeans(pts: &[([f32; 3], u32)], centers: &mut [[f32; 3]], iters: usize) { let lookup = Nearest::new(centers.iter().copied()); let mut sum = vec![[0.0f64; 3]; centers.len()]; let mut cnt = vec![0.0f64; centers.len()]; - for (p, w) in pts { - let i = lookup.find(*p); + // The nearest-centre search is the expensive part and has no cross-point state, so it + // runs in parallel. The f64 sums below stay sequential in the original point order: + // float addition is not associative, and this keeps every bit of the result unchanged. + #[cfg(not(target_arch = "wasm32"))] + let nearest: Vec = { + use rayon::prelude::*; + pts.par_iter().map(|(p, _)| lookup.find(*p) as u32).collect() + }; + #[cfg(target_arch = "wasm32")] + let nearest: Vec = pts.iter().map(|(p, _)| lookup.find(*p) as u32).collect(); + for ((p, w), &i) in pts.iter().zip(&nearest) { + let i = i as usize; for a in 0..3 { sum[i][a] += f64::from(p[a]) * f64::from(*w); } @@ -258,6 +372,20 @@ fn lab_to_rgb8(lab: [f32; 3]) -> Rgb8 { photocraft_color::convert::lab_to_srgb(lab).map(to8) } +/// Histogram colours as CIE Lab points with their counts. Each point depends only on its own +/// colour and `collect` keeps the order, so the parallel map gives the same list; the log/pow +/// in `srgb_to_lab` made this a visible serial cost with millions of unique colours. +fn lab_points(hist: &[(Rgb8, u32)]) -> Vec<([f32; 3], u32)> { + let lab = |(c, n): &(Rgb8, u32)| (srgb_to_lab(c.map(|v| f32::from(v) / 255.0)), *n); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + hist.par_iter().map(lab).collect() + } + #[cfg(target_arch = "wasm32")] + hist.iter().map(lab).collect() +} + /// Builds the palette for `px`. `colors` is the requested count (2..=256) for the computed /// kinds; forced colours are included first and count toward it. pub fn build_palette(px: &[[f32; 4]], kind: PaletteKind, colors: usize, forced: Forced) -> Result, String> { @@ -296,7 +424,7 @@ pub fn build_palette(px: &[[f32; 4]], kind: PaletteKind, colors: usize, forced: if hist.len() <= room { hist.iter().map(|h| h.0).collect() } else { - let pts: Vec<([f32; 3], u32)> = hist.iter().map(|(c, n)| (srgb_to_lab(c.map(|v| f32::from(v) / 255.0)), *n)).collect(); + let pts = lab_points(&hist); let mut centers = median_cut(&pts, room); kmeans(&pts, &mut centers, 4); let mut v: Vec = centers.iter().map(|c| lab_to_rgb8(*c)).collect(); @@ -511,215 +639,200 @@ fn spot(shape: HalftoneShape, u: f32, v: f32) -> f32 { /// Converts gray values (0..=1, row-major `w` wide) to 0/1 in place. pub fn to_bitmap(gray: &mut [f32], w: usize, method: BitmapMethod) { - let h = gray.len().checked_div(w).unwrap_or(0); - match method { - BitmapMethod::Threshold => gray.iter_mut().for_each(|v| *v = if *v >= 0.5 { 1.0 } else { 0.0 }), - BitmapMethod::Pattern => { - for (i, v) in gray.iter_mut().enumerate() { - *v = if *v > bayer8(i % w, i / w) + 0.5 { 1.0 } else { 0.0 }; + BitmapStrips::new(w, method).convert(gray); +} + +/// Image › Mode › Bitmap fed in strips of whole rows, top to bottom, so a huge image never needs a +/// full-size gray plane (#8477): [`BitmapStrips::convert`] turns each strip to 0/1 in place, +/// carrying error diffusion's error into the next strip. The result is the same however the image +/// is cut into strips. +pub struct BitmapStrips<'a> { + w: usize, + kind: StripKind<'a>, + /// Image row of the next strip's first row. + y: usize, +} + +enum StripKind<'a> { + Threshold, + Pattern, + /// Floyd–Steinberg: the error each pixel of the last converted row passes down. + Diffusion { + errs: Vec, + }, + /// The spot function's values over a cell, sorted (its CDF ranks coverage by tone). + Halftone { + cell: f32, + sin: f32, + cos: f32, + shape: HalftoneShape, + table: Vec, + }, + /// Custom Pattern's threshold map, `mw` x `mh`, tiled from the top-left corner. + Custom { + map: &'a [f32], + mw: usize, + mh: usize, + }, +} + +impl<'a> BitmapStrips<'a> { + pub fn new(w: usize, method: BitmapMethod) -> Self { + let kind = match method { + BitmapMethod::Threshold => StripKind::Threshold, + BitmapMethod::Pattern => StripKind::Pattern, + BitmapMethod::Diffusion => StripKind::Diffusion { errs: Vec::new() }, + BitmapMethod::Halftone { cell, angle, shape } => { + let (sin, cos) = angle.to_radians().sin_cos(); + // Rank the spot function over the cell so coverage is proportional to the tone. + let mut table: Vec = + (0..32 * 32).map(|k| spot(shape, ((k % 32) as f32 + 0.5) / 16.0 - 1.0, ((k / 32) as f32 + 0.5) / 16.0 - 1.0)).collect(); + table.sort_by(f32::total_cmp); + StripKind::Halftone { cell: cell.max(2.0), sin, cos, shape, table } } + }; + Self { w, kind, y: 0 } + } + + /// Bitmap's Custom Pattern: each gray value turns white where it reaches the threshold `map` + /// (`mw` x `mh`, from [`custom_pattern_thresholds`]) tiled from the image's top-left corner, + /// black elsewhere. A map that doesn't fit `mw` x `mh` thresholds at 50%. + pub fn custom(w: usize, map: &'a [f32], mw: usize, mh: usize) -> Self { + let fits = mw > 0 && mh > 0 && mw.checked_mul(mh) == Some(map.len()); + let kind = if fits { StripKind::Custom { map, mw, mh } } else { StripKind::Threshold }; + Self { w, kind, y: 0 } + } + + /// Converts the next rows (gray 0..=1, `w` wide; a short last row is allowed) to 0/1 in place. + pub fn convert(&mut self, gray: &mut [f32]) { + let (w, y0) = (self.w, self.y); + if w == 0 { + // No rows to lay a pattern or diffuse along: a 50% threshold. + gray.iter_mut().for_each(|v| *v = if *v >= 0.5 { 1.0 } else { 0.0 }); + return; } - BitmapMethod::Diffusion => { - let mut buf: Vec = gray.to_vec(); - for y in 0..h { - for x in 0..w { - let i = y * w + x; - let old = buf[i]; - let new = if old >= 0.5 { 1.0 } else { 0.0 }; - let e = old - new; - gray[i] = new; - if x + 1 < w { - buf[i + 1] += e * 7.0 / 16.0; - } - if y + 1 < h { - if x > 0 { - buf[i + w - 1] += e * 3.0 / 16.0; - } - buf[i + w] += e * 5.0 / 16.0; - if x + 1 < w { - buf[i + w + 1] += e / 16.0; - } - } + self.y = y0.saturating_add(gray.len().div_ceil(w)); + // Every method but diffusion is pointwise: rows convert in parallel. + let par = gray.len() >= 1 << 16; + match &mut self.kind { + StripKind::Threshold => crate::par::chunks_mut(gray, w, par, |_, row| { + row.iter_mut().for_each(|v| *v = if *v >= 0.5 { 1.0 } else { 0.0 }); + }), + StripKind::Pattern => crate::par::chunks_mut(gray, w, par, |r, row| { + for (x, v) in row.iter_mut().enumerate() { + *v = if *v > bayer8(x, y0 + r) + 0.5 { 1.0 } else { 0.0 }; + } + }), + StripKind::Diffusion { errs } => { + // The previous strip's last row passes its error down to this strip's first. + let first = w.min(gray.len()); + pass_down(errs, gray.get_mut(..first).unwrap_or_default()); + let mut rest = gray; + while !rest.is_empty() { + let n = w.min(rest.len()); + let (row, tail) = std::mem::take(&mut rest).split_at_mut(n); + diffuse_row(row, errs); + let below = w.min(tail.len()); + pass_down(errs, tail.get_mut(..below).unwrap_or_default()); + rest = tail; } } - } - BitmapMethod::Halftone { cell, angle, shape } => { - let cell = cell.max(2.0); - let (s, c) = angle.to_radians().sin_cos(); - // Rank the spot function over the cell so coverage is proportional to the tone. - let mut table: Vec = (0..32 * 32).map(|k| spot(shape, ((k % 32) as f32 + 0.5) / 16.0 - 1.0, ((k / 32) as f32 + 0.5) / 16.0 - 1.0)).collect(); - table.sort_by(f32::total_cmp); - let cdf = |x: f32| table.partition_point(|t| *t < x) as f32 / table.len() as f32; - for (i, v) in gray.iter_mut().enumerate() { - let (x, y) = ((i % w) as f32 + 0.5, (i / w) as f32 + 0.5); - let (rx, ry) = (x * c + y * s, -x * s + y * c); - let u = (rx / cell).rem_euclid(1.0) * 2.0 - 1.0; - let vv = (ry / cell).rem_euclid(1.0) * 2.0 - 1.0; - // Ink (black) covers where the spot function exceeds the gray level. - *v = if cdf(spot(shape, u, vv)) >= *v { 0.0 } else { 1.0 }; + StripKind::Halftone { cell, sin, cos, shape, table } => { + let (cell, s, c, shape, table) = (*cell, *sin, *cos, *shape, &*table); + let cdf = |x: f32| table.partition_point(|t| *t < x) as f32 / table.len() as f32; + crate::par::chunks_mut(gray, w, par, |r, row| { + let y = (y0 + r) as f32 + 0.5; + for (x, v) in row.iter_mut().enumerate() { + let x = x as f32 + 0.5; + let (rx, ry) = (x * c + y * s, -x * s + y * c); + let u = (rx / cell).rem_euclid(1.0) * 2.0 - 1.0; + let vv = (ry / cell).rem_euclid(1.0) * 2.0 - 1.0; + // Ink (black) covers where the spot function exceeds the gray level. + *v = if cdf(spot(shape, u, vv)) >= *v { 0.0 } else { 1.0 }; + } + }); + } + StripKind::Custom { map, mw, mh } => { + let (map, mw, mh) = (&**map, *mw, *mh); + crate::par::chunks_mut(gray, w, par, |r, row| { + let base = (y0 + r) % mh * mw; + for (x, v) in row.iter_mut().enumerate() { + let t = map.get(base + x % mw).copied().unwrap_or(0.5); + *v = if *v >= t { 1.0 } else { 0.0 }; + } + }); } } } } +/// One Floyd–Steinberg row, in place: each pixel takes its error from the left (7/16), and +/// `errs` gets the error it passes down to the next row ([`pass_down`]). +fn diffuse_row(row: &mut [f32], errs: &mut Vec) { + errs.clear(); + for x in 0..row.len() { + let old = row[x]; + let new = if old >= 0.5 { 1.0 } else { 0.0 }; + let e = old - new; + row[x] = new; + if let Some(next) = row.get_mut(x + 1) { + *next += e * 7.0 / 16.0; + } + errs.push(e); + } +} + +/// Adds the error `errs` of a row to the row below: 1/16 from the pixel up-left, 5/16 from the +/// one above, 3/16 from the one up-right, in that order (the order the row's pixels were +/// converted in), so the result is the same whether or not the rows are in the same strip. +fn pass_down(errs: &[f32], below: &mut [f32]) { + for (t, v) in below.iter_mut().enumerate() { + if let Some(e) = t.checked_sub(1).and_then(|l| errs.get(l)) { + *v += e / 16.0; + } + if let Some(e) = errs.get(t) { + *v += e * 5.0 / 16.0; + } + if let Some(e) = errs.get(t + 1) { + *v += e * 3.0 / 16.0; + } + } +} + +/// The threshold map of Bitmap's Custom Pattern from a pattern's lightness (0..=1, row-major): +/// pattern pixels ranked by lightness, ties sharing their average rank, the lightest pixel +/// thresholding lowest (it turns white first). A map of `n` pixels thus spreads `n` evenly +/// spaced levels `(k + 0.5) / n`, whatever the pattern's own contrast, as Photoshop's does +/// (measured: a 2x2 pattern of 32/96/160/224, a 3x1 of 50/128/200 and a 7x1 ramp all give +/// exactly those levels). +pub fn custom_pattern_thresholds(lightness: &[f32]) -> Vec { + let n = lightness.len(); + let key = |i: usize| lightness.get(i).copied().filter(|v| v.is_finite()).unwrap_or(0.0); + let mut order: Vec = (0..n).collect(); + order.sort_by(|a, b| key(*a).total_cmp(&key(*b))); + let mut out = vec![0.5; n]; + let mut start = 0; + while start < n { + let v = order.get(start).map(|i| key(*i)); + let end = (start..n).find(|&j| order.get(j).map(|i| key(*i)) != v).unwrap_or(n); + // Ranks start..end share their mean. + let rank = (start + end - 1) as f32 / 2.0; + let t = 1.0 - (rank + 0.5) / n as f32; + for i in order.get(start..end).unwrap_or_default() { + if let Some(o) = out.get_mut(*i) { + *o = t; + } + } + start = end; + } + out +} + +/// Bitmap's Custom Pattern over a whole image (gray 0..=1, row-major `w` wide), see +/// [`BitmapStrips::custom`]. An image `w` of 0 thresholds at 50%. +pub fn to_bitmap_custom(gray: &mut [f32], w: usize, map: &[f32], mw: usize, mh: usize) { + BitmapStrips::custom(w, map, mw, mh).convert(gray); +} + #[cfg(test)] -mod tests { - use super::*; - - fn gradient(w: usize, h: usize) -> Vec<[f32; 4]> { - (0..w * h).map(|i| [(i % w) as f32 / (w - 1) as f32, (i / w) as f32 / (h - 1) as f32, 0.3, 1.0]).collect() - } - - #[test] - fn fixed_palettes_have_expected_sizes() { - assert_eq!(web_palette().len(), 216); - assert_eq!(mac_palette().len(), 256); - assert_eq!(windows_palette().len(), 256); - assert_eq!(uniform_palette(27).len(), 27); - let mut m = mac_palette(); - m.sort_unstable(); - m.dedup(); - assert_eq!(m.len(), 256, "mac palette entries are unique"); - } - - #[test] - fn exact_palette_and_failure() { - let px = vec![[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 1.0]]; - assert_eq!(build_palette(&px, PaletteKind::Exact, 256, Forced::None).unwrap().len(), 2); - assert!(build_palette(&gradient(64, 64), PaletteKind::Exact, 256, Forced::None).is_err()); - } - - #[test] - fn adaptive_and_perceptual_respect_count_and_forced() { - let px = gradient(40, 40); - for kind in [PaletteKind::Adaptive, PaletteKind::Perceptual, PaletteKind::Selective] { - let pal = build_palette(&px, kind, 16, Forced::BlackWhite).unwrap(); - assert!(pal.len() <= 16 && pal.len() >= 8, "{kind:?} {}", pal.len()); - assert_eq!(&pal[..2], &[[0, 0, 0], [255, 255, 255]]); - } - } - - #[test] - fn forced_colours_filling_the_count_do_not_panic() { - // 2 colours + Black and White (or 8 + Primaries) leaves no room for - // computed entries; the median cut used to return no centres and - // k-means indexed into the empty list. - let px = vec![[1.0, 1.0, 1.0, 1.0]; 16]; - for kind in [PaletteKind::Adaptive, PaletteKind::Perceptual, PaletteKind::Selective] { - let pal = build_palette(&px, kind, 2, Forced::BlackWhite).unwrap(); - assert_eq!(pal, vec![[0, 0, 0], [255, 255, 255]], "{kind:?}"); - let pal = build_palette(&gradient(8, 8), kind, 8, Forced::Primaries).unwrap(); - assert_eq!(pal.len(), 8, "{kind:?}"); - } - } - - #[test] - #[ignore = "24 MP release timing comparison; run with --release --ignored --nocapture"] - fn indexed_diffusion_24mp_release_comparison() { - let source = gradient(6000, 4000); - let pal = web_palette(); - for run in 0..4 { - let mut linear = source.clone(); - let mut accelerated = source.clone(); - let mut before = None; - let mut after = None; - // Reverse the order on alternating rounds; exclude the first round as warmup. - for old in if run % 2 == 0 { [true, false] } else { [false, true] } { - let start = std::time::Instant::now(); - if old { - let indices = quantize_with(&mut linear, 6000, &pal, Dither::Diffusion, 0.75, None, |c| linear_rgb_nearest(&pal, c)); - before = Some((start.elapsed().as_secs_f64() * 1000.0, indices)); - } else { - let indices = quantize(&mut accelerated, 6000, &pal, Dither::Diffusion, 0.75, None); - after = Some((start.elapsed().as_secs_f64() * 1000.0, indices)); - } - } - let (before_ms, before_indices) = before.unwrap(); - let (after_ms, after_indices) = after.unwrap(); - assert_eq!(before_indices.iter().zip(&after_indices).position(|(a, b)| a != b), None, "first differing index"); - assert_eq!(linear.iter().zip(&accelerated).position(|(a, b)| a != b), None, "first differing pixel"); - println!( - "{}", - serde_json::json!({"run":run,"warmup":run==0,"pixels":24_000_000,"palette_entries":216,"linear_ms":before_ms,"accelerated_ms":after_ms}) - ); - } - } - - fn linear_rgb_nearest(pal: &[Rgb8], c: [f32; 3]) -> usize { - let mut best = (f32::MAX, 0); - for (i, e) in pal.iter().enumerate() { - let d: f32 = (0..3).map(|k| (c[k] * 255.0 - f32::from(e[k])).powi(2)).sum(); - if d < best.0 { - best = (d, i); - } - } - best.1 - } - - #[test] - fn all_dithers_match_the_original_linear_palette_search_exactly() { - let mut source = gradient(93, 61); - for (i, p) in source.iter_mut().enumerate() { - p[2] = (i % 71) as f32 / 70.0; - p[3] = [0.0, 0.2, 0.7, 1.0][i % 4]; - } - for count in [8, 16, 32, 64, 128, 256] { - let pal = build_palette(&source, PaletteKind::Selective, count, Forced::BlackWhite).unwrap(); - for dither in [Dither::None, Dither::Diffusion, Dither::Pattern, Dither::Noise] { - for transparent in [None, Some(1)] { - let mut actual = source.clone(); - let mut expected = source.clone(); - let indices = quantize(&mut actual, 93, &pal, dither, 0.75, transparent); - let old_indices = quantize_with(&mut expected, 93, &pal, dither, 0.75, transparent, |c| linear_rgb_nearest(&pal, c)); - assert_eq!(indices, old_indices, "{count} {dither:?} {transparent:?}"); - assert_eq!(actual, expected, "{count} {dither:?} {transparent:?}"); - } - } - } - } - - #[test] - fn quantize_maps_to_palette_with_each_dither() { - let pal = vec![[0, 0, 0], [255, 255, 255]]; - for d in [Dither::None, Dither::Diffusion, Dither::Pattern, Dither::Noise] { - let mut px: Vec<[f32; 4]> = (0..64) - .map(|i| [i as f32 / 63.0; 4]) - .map(|mut p| { - p[3] = 1.0; - p - }) - .collect(); - let idx = quantize(&mut px, 8, &pal, d, 1.0, None); - assert!(px.iter().all(|p| p[0] == 0.0 || p[0] == 1.0), "{d:?}"); - assert_eq!(idx.len(), 64); - // Mean brightness is preserved by dithering (not by plain thresholding). - if d == Dither::Diffusion { - let mean = px.iter().map(|p| p[0]).sum::() / 64.0; - assert!((mean - 0.5).abs() < 0.08, "{mean}"); - } - } - let mut px = vec![[1.0, 1.0, 1.0, 0.2]]; - let idx = quantize(&mut px, 1, &pal, Dither::None, 1.0, Some(1)); - assert_eq!((idx[0], px[0][3]), (1, 0.0)); - } - - #[test] - fn bitmap_methods_are_binary_and_track_tone() { - for m in [ - BitmapMethod::Threshold, - BitmapMethod::Pattern, - BitmapMethod::Diffusion, - BitmapMethod::Halftone { cell: 6.0, angle: 45.0, shape: HalftoneShape::Round }, - ] { - let mut g = vec![0.25f32; 32 * 32]; - to_bitmap(&mut g, 32, m); - assert!(g.iter().all(|v| *v == 0.0 || *v == 1.0)); - let mean = g.iter().sum::() / g.len() as f32; - if m != BitmapMethod::Threshold { - assert!((mean - 0.25).abs() < 0.15, "{m:?} {mean}"); - } else { - assert_eq!(mean, 0.0); - } - } - } -} +mod tests; diff --git a/crates/algo/src/quantize/tests.rs b/crates/algo/src/quantize/tests.rs new file mode 100644 index 0000000000..2bf4713917 --- /dev/null +++ b/crates/algo/src/quantize/tests.rs @@ -0,0 +1,549 @@ +use super::*; + +fn gradient(w: usize, h: usize) -> Vec<[f32; 4]> { + (0..w * h).map(|i| [(i % w) as f32 / (w - 1) as f32, (i / w) as f32 / (h - 1) as f32, 0.3, 1.0]).collect() +} + +fn to8_reference(v: f32) -> u8 { + (v.clamp(0.0, 1.0) * 255.0).round() as u8 +} + +#[test] +fn to8_matches_round_on_samples_and_edges() { + // Every half-way point and its neighbours, plus a dense sweep of the unit range. + for k in 0..=256u32 { + let mid = (k as f32 + 0.5) / 255.0; + for d in -3i32..=3 { + let v = f32::from_bits((mid.to_bits() as i32 + d) as u32); + assert_eq!(to8(v), to8_reference(v), "{v:e}"); + } + } + for i in 0..200_000u32 { + let v = f32::from_bits(i * 20_000); + assert_eq!(to8(v), to8_reference(v), "{v:e}"); + } + for v in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, -0.0, 0.0, 1.0, -1.0, 2.0, 1e30, -1e30, f32::MIN_POSITIVE, 1e-45] { + assert_eq!(to8(v), to8_reference(v), "{v:e}"); + } +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +#[ignore = "checks all 2^32 floats, a few seconds in release; run with --release --ignored"] +fn to8_matches_round_for_every_float() { + use rayon::prelude::*; + let bad = (0..=u32::MAX).into_par_iter().filter(|&b| to8(f32::from_bits(b)) != to8_reference(f32::from_bits(b))).count(); + assert_eq!(bad, 0); +} + +/// A repeatable 96x64 image: a gradient with up to 31 levels of integer-hash noise and a +/// few see-through pixels. +fn golden_image() -> Vec<[f32; 4]> { + let (w, h) = (96usize, 64usize); + (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as f32, (i / w) as f32); + let noise = |salt: u32| ((((i as u32).wrapping_mul(2_654_435_761).wrapping_add(salt.wrapping_mul(40_503))) >> 13) & 31) as f32 / 255.0; + let alpha = if i % 17 == 0 { 0.25 } else { 1.0 }; + [x / (w - 1) as f32 * 0.8 + noise(1), y / (h - 1) as f32 * 0.7 + noise(2), ((x + y) / (w + h) as f32) * 0.6 + noise(3), alpha] + }) + .collect() +} + +/// Palettes written down from the version before the speed-up (hash-map histogram, every box +/// rescanned each split, comparison sort, serial k-means). Any change to how the palettes +/// are built has to give these exact colours. +#[test] +fn palettes_match_the_recorded_originals() { + let px = golden_image(); + #[rustfmt::skip] + let cases: Vec<(PaletteKind, usize, Forced, &[Rgb8])> = vec![ + ( + PaletteKind::Adaptive, + 16, + Forced::None, + &[ + [40, 173, 80], [196, 175, 150], [92, 173, 103], [92, 81, 71], [195, 83, 118], [39, 82, 49], + [142, 82, 95], [195, 37, 101], [143, 36, 79], [40, 128, 64], [196, 129, 133], [92, 127, 87], + [91, 36, 56], [38, 35, 33], [144, 128, 111], [144, 174, 127], + ], + ), + ( + PaletteKind::Perceptual, + 16, + Forced::None, + &[ + [104, 111, 87], [51, 129, 68], [48, 178, 79], [110, 74, 77], [165, 46, 93], [205, 50, 107], + [200, 154, 143], [44, 54, 42], [190, 101, 121], [61, 32, 44], [155, 115, 111], [48, 93, 56], + [119, 42, 71], [84, 172, 98], [126, 165, 115], [172, 168, 135], + ], + ), + ( + PaletteKind::Selective, + 24, + Forced::BlackWhite, + &[ + [0, 0, 0], [255, 255, 255], [102, 107, 85], [65, 69, 55], [87, 133, 89], [109, 73, 76], + [164, 42, 92], [204, 51, 102], [199, 153, 142], [54, 170, 84], [199, 107, 126], [65, 33, 45], + [153, 102, 102], [187, 180, 145], [40, 106, 54], [100, 176, 107], [138, 174, 123], [50, 188, 80], + [32, 37, 33], [47, 139, 69], [144, 143, 116], [41, 80, 49], [108, 33, 64], [151, 70, 94], + ], + ), + ( + PaletteKind::Adaptive, + 8, + Forced::Primaries, + &[ + [0, 0, 0], [255, 255, 255], [255, 0, 0], [0, 255, 0], [0, 0, 255], [0, 255, 255], + [255, 0, 255], [255, 255, 0], + ], + ), + ( + PaletteKind::Perceptual, + 12, + Forced::BlackWhite, + &[ + [0, 0, 0], [255, 255, 255], [104, 106, 85], [137, 43, 79], [148, 170, 126], [181, 144, 132], + [66, 172, 87], [88, 49, 60], [57, 118, 67], [42, 54, 41], [183, 101, 118], [196, 50, 105], + ], + ), + ]; + for (kind, colors, forced, expected) in cases { + assert_eq!(build_palette(&px, kind, colors, forced).unwrap(), expected, "{kind:?} {colors} {forced:?}"); + } +} + +/// The pre-speed-up median cut and k-means, kept as the oracle for the faster ones. +fn median_cut_reference(pts: &[([f32; 3], u32)], n: usize) -> Vec<[f32; 3]> { + if pts.is_empty() || n == 0 { + return Vec::new(); + } + let mut boxes: Vec> = vec![pts.to_vec()]; + while boxes.len() < n { + let (bi, axis, score) = boxes + .iter() + .enumerate() + .filter(|(_, b)| b.len() > 1) + .map(|(i, b)| { + let mut best = (0usize, -1.0f32); + for a in 0..3 { + let (lo, hi) = b.iter().fold((f32::MAX, f32::MIN), |(lo, hi), p| (lo.min(p.0[a]), hi.max(p.0[a]))); + if hi - lo > best.1 { + best = (a, hi - lo); + } + } + let w: f32 = b.iter().map(|p| p.1 as f32).sum(); + (i, best.0, best.1 * w.sqrt()) + }) + .fold((usize::MAX, 0, -1.0f32), |acc, x| if x.2 > acc.2 { x } else { acc }); + if bi == usize::MAX || score <= 0.0 { + break; + } + let mut b = boxes.swap_remove(bi); + b.sort_by(|p, q| p.0[axis].total_cmp(&q.0[axis])); + let total: u64 = b.iter().map(|p| u64::from(p.1)).sum(); + let mut acc = 0u64; + let mut cut = 1; + for (i, p) in b.iter().enumerate() { + acc += u64::from(p.1); + if acc * 2 >= total { + cut = (i + 1).clamp(1, b.len() - 1); + break; + } + } + let rest = b.split_off(cut); + boxes.push(b); + boxes.push(rest); + } + boxes + .iter() + .map(|b| { + let w: f64 = b.iter().map(|p| f64::from(p.1)).sum(); + std::array::from_fn(|a| (b.iter().map(|p| f64::from(p.0[a]) * f64::from(p.1)).sum::() / w) as f32) + }) + .collect() +} + +fn kmeans_reference(pts: &[([f32; 3], u32)], centers: &mut [[f32; 3]], iters: usize) { + for _ in 0..iters { + let lookup = Nearest::new(centers.iter().copied()); + let mut sum = vec![[0.0f64; 3]; centers.len()]; + let mut cnt = vec![0.0f64; centers.len()]; + for (p, w) in pts { + let i = lookup.find(*p); + for a in 0..3 { + sum[i][a] += f64::from(p[a]) * f64::from(*w); + } + cnt[i] += f64::from(*w); + } + for (i, c) in centers.iter_mut().enumerate() { + if cnt[i] > 0.0 { + *c = std::array::from_fn(|a| (sum[i][a] / cnt[i]) as f32); + } + } + } +} + +/// Points with many repeated coordinates (ties), negative values, -0.0 and weights up to +/// a few thousand, so the stable-sort and float-order rules are all exercised. +fn tie_heavy_points(n: usize, seed: u64) -> Vec<([f32; 3], u32)> { + let mut s = seed; + let mut next = move || { + s = s.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); + (s >> 33) as u32 + }; + (0..n) + .map(|_| { + let mut c = |range: u32| match next() % 40 { + 0 => -0.0, + 1 => 0.0, + _ => (next() % range) as f32 * 0.5 - (range / 4) as f32, + }; + ([c(24), c(300), c(150)], 1 + next() % 3000) + }) + .collect() +} + +#[test] +fn faster_median_cut_and_kmeans_equal_the_originals_bit_for_bit() { + // 40 000 points cross the 256-point radix threshold on the first splits. + for (n, boxes, seed) in [(40_000, 256, 1), (40_000, 37, 2), (3_000, 256, 3), (200, 64, 4), (2, 8, 5)] { + let pts = tie_heavy_points(n, seed); + let new = median_cut(&pts, boxes); + let old = median_cut_reference(&pts, boxes); + assert_eq!(new.len(), old.len()); + for (a, b) in new.iter().zip(&old) { + assert_eq!(a.map(f32::to_bits), b.map(f32::to_bits), "median cut, n={n} boxes={boxes}"); + } + let (mut a, mut b) = (new, old); + kmeans(&pts, &mut a, 3); + kmeans_reference(&pts, &mut b, 3); + for (x, y) in a.iter().zip(&b) { + assert_eq!(x.map(f32::to_bits), y.map(f32::to_bits), "kmeans, n={n} boxes={boxes}"); + } + } +} + +#[test] +fn radix_sort_is_the_stable_total_cmp_sort() { + for axis in 0..3 { + let mut pts = tie_heavy_points(5_000, 9 + axis as u64); + // The weight field doubles as an id: equal keys must keep their input order. + for (i, p) in pts.iter_mut().enumerate() { + p.1 = i as u32; + } + pts[10].0[axis] = f32::NAN; + pts[11].0[axis] = -f32::NAN; + pts[12].0[axis] = f32::INFINITY; + pts[13].0[axis] = f32::NEG_INFINITY; + let mut expected = pts.clone(); + expected.sort_by(|p, q| p.0[axis].total_cmp(&q.0[axis])); + radix_sort_along(&mut pts, axis); + let bits = |v: &[([f32; 3], u32)]| v.iter().map(|p| (p.0.map(f32::to_bits), p.1)).collect::>(); + assert_eq!(bits(&pts), bits(&expected), "axis {axis}"); + } +} + +#[test] +fn histogram_counts_opaque_pixels_sorted() { + let px = [[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.5], [1.0, 0.0, 0.0, 0.49], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]; + assert_eq!(histogram(&px), vec![([0, 0, 0], 1), ([0, 0, 255], 2), ([255, 0, 0], 2)]); + assert!(histogram(&[]).is_empty()); +} + +#[test] +fn fixed_palettes_have_expected_sizes() { + assert_eq!(web_palette().len(), 216); + assert_eq!(mac_palette().len(), 256); + assert_eq!(windows_palette().len(), 256); + assert_eq!(uniform_palette(27).len(), 27); + let mut m = mac_palette(); + m.sort_unstable(); + m.dedup(); + assert_eq!(m.len(), 256, "mac palette entries are unique"); +} + +#[test] +fn exact_palette_and_failure() { + let px = vec![[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 1.0]]; + assert_eq!(build_palette(&px, PaletteKind::Exact, 256, Forced::None).unwrap().len(), 2); + assert!(build_palette(&gradient(64, 64), PaletteKind::Exact, 256, Forced::None).is_err()); +} + +#[test] +fn adaptive_and_perceptual_respect_count_and_forced() { + let px = gradient(40, 40); + for kind in [PaletteKind::Adaptive, PaletteKind::Perceptual, PaletteKind::Selective] { + let pal = build_palette(&px, kind, 16, Forced::BlackWhite).unwrap(); + assert!(pal.len() <= 16 && pal.len() >= 8, "{kind:?} {}", pal.len()); + assert_eq!(&pal[..2], &[[0, 0, 0], [255, 255, 255]]); + } +} + +#[test] +fn forced_colours_filling_the_count_do_not_panic() { + // 2 colours + Black and White (or 8 + Primaries) leaves no room for + // computed entries; the median cut used to return no centres and + // k-means indexed into the empty list. + let px = vec![[1.0, 1.0, 1.0, 1.0]; 16]; + for kind in [PaletteKind::Adaptive, PaletteKind::Perceptual, PaletteKind::Selective] { + let pal = build_palette(&px, kind, 2, Forced::BlackWhite).unwrap(); + assert_eq!(pal, vec![[0, 0, 0], [255, 255, 255]], "{kind:?}"); + let pal = build_palette(&gradient(8, 8), kind, 8, Forced::Primaries).unwrap(); + assert_eq!(pal.len(), 8, "{kind:?}"); + } +} + +#[test] +#[ignore = "24 MP release timing comparison; run with --release --ignored --nocapture"] +fn indexed_diffusion_24mp_release_comparison() { + let source = gradient(6000, 4000); + let pal = web_palette(); + for run in 0..4 { + let mut linear = source.clone(); + let mut accelerated = source.clone(); + let mut before = None; + let mut after = None; + // Reverse the order on alternating rounds; exclude the first round as warmup. + for old in if run % 2 == 0 { [true, false] } else { [false, true] } { + let start = std::time::Instant::now(); + if old { + let indices = quantize_with(&mut linear, 6000, &pal, Dither::Diffusion, 0.75, None, |c| linear_rgb_nearest(&pal, c)); + before = Some((start.elapsed().as_secs_f64() * 1000.0, indices)); + } else { + let indices = quantize(&mut accelerated, 6000, &pal, Dither::Diffusion, 0.75, None); + after = Some((start.elapsed().as_secs_f64() * 1000.0, indices)); + } + } + let (before_ms, before_indices) = before.unwrap(); + let (after_ms, after_indices) = after.unwrap(); + assert_eq!(before_indices.iter().zip(&after_indices).position(|(a, b)| a != b), None, "first differing index"); + assert_eq!(linear.iter().zip(&accelerated).position(|(a, b)| a != b), None, "first differing pixel"); + println!( + "{}", + serde_json::json!({"run":run,"warmup":run==0,"pixels":24_000_000,"palette_entries":216,"linear_ms":before_ms,"accelerated_ms":after_ms}) + ); + } +} + +fn linear_rgb_nearest(pal: &[Rgb8], c: [f32; 3]) -> usize { + let mut best = (f32::MAX, 0); + for (i, e) in pal.iter().enumerate() { + let d: f32 = (0..3).map(|k| (c[k] * 255.0 - f32::from(e[k])).powi(2)).sum(); + if d < best.0 { + best = (d, i); + } + } + best.1 +} + +#[test] +fn all_dithers_match_the_original_linear_palette_search_exactly() { + let mut source = gradient(93, 61); + for (i, p) in source.iter_mut().enumerate() { + p[2] = (i % 71) as f32 / 70.0; + p[3] = [0.0, 0.2, 0.7, 1.0][i % 4]; + } + for count in [8, 16, 32, 64, 128, 256] { + let pal = build_palette(&source, PaletteKind::Selective, count, Forced::BlackWhite).unwrap(); + for dither in [Dither::None, Dither::Diffusion, Dither::Pattern, Dither::Noise] { + for transparent in [None, Some(1)] { + let mut actual = source.clone(); + let mut expected = source.clone(); + let indices = quantize(&mut actual, 93, &pal, dither, 0.75, transparent); + let old_indices = quantize_with(&mut expected, 93, &pal, dither, 0.75, transparent, |c| linear_rgb_nearest(&pal, c)); + assert_eq!(indices, old_indices, "{count} {dither:?} {transparent:?}"); + assert_eq!(actual, expected, "{count} {dither:?} {transparent:?}"); + } + } + } +} + +#[test] +fn quantize_maps_to_palette_with_each_dither() { + let pal = vec![[0, 0, 0], [255, 255, 255]]; + for d in [Dither::None, Dither::Diffusion, Dither::Pattern, Dither::Noise] { + let mut px: Vec<[f32; 4]> = (0..64) + .map(|i| [i as f32 / 63.0; 4]) + .map(|mut p| { + p[3] = 1.0; + p + }) + .collect(); + let idx = quantize(&mut px, 8, &pal, d, 1.0, None); + assert!(px.iter().all(|p| p[0] == 0.0 || p[0] == 1.0), "{d:?}"); + assert_eq!(idx.len(), 64); + // Mean brightness is preserved by dithering (not by plain thresholding). + if d == Dither::Diffusion { + let mean = px.iter().map(|p| p[0]).sum::() / 64.0; + assert!((mean - 0.5).abs() < 0.08, "{mean}"); + } + } + let mut px = vec![[1.0, 1.0, 1.0, 0.2]]; + let idx = quantize(&mut px, 1, &pal, Dither::None, 1.0, Some(1)); + assert_eq!((idx[0], px[0][3]), (1, 0.0)); +} + +#[test] +fn bitmap_methods_are_binary_and_track_tone() { + for m in [ + BitmapMethod::Threshold, + BitmapMethod::Pattern, + BitmapMethod::Diffusion, + BitmapMethod::Halftone { cell: 6.0, angle: 45.0, shape: HalftoneShape::Round }, + ] { + let mut g = vec![0.25f32; 32 * 32]; + to_bitmap(&mut g, 32, m); + assert!(g.iter().all(|v| *v == 0.0 || *v == 1.0)); + let mean = g.iter().sum::() / g.len() as f32; + if m != BitmapMethod::Threshold { + assert!((mean - 0.25).abs() < 0.15, "{m:?} {mean}"); + } else { + assert_eq!(mean, 0.0); + } + } +} + +/// The first 8-bit gray level (of a 0..=255 ramp) that `t` turns white. +fn first_white(t: f32) -> u32 { + (0..=255).find(|v| *v as f32 / 255.0 >= t).unwrap_or(256) +} + +#[test] +fn custom_pattern_thresholds_match_photoshop() { + // Photoshop (Image › Mode › Bitmap, Custom Pattern) on a 0..255 ramp: the first white + // level per pattern pixel, measured against defined patterns. + let l = |v: &[u8]| v.iter().map(|v| f32::from(*v) / 255.0).collect::>(); + let ramp7 = custom_pattern_thresholds(&l(&[0, 40, 80, 120, 160, 200, 240])); + assert_eq!(ramp7.iter().map(|t| first_white(*t)).collect::>(), [237, 201, 164, 128, 92, 55, 19]); + let five = custom_pattern_thresholds(&l(&[10, 200, 90, 250, 150])); + assert_eq!(five.iter().map(|t| first_white(*t)).collect::>(), [230, 77, 179, 26, 128]); + // Equal pixels share their rank: 100, 100 both sit at 50%. + let ties = custom_pattern_thresholds(&l(&[100, 100, 200, 50])); + assert_eq!(ties.iter().map(|t| first_white(*t)).collect::>(), [128, 128, 32, 224]); + assert!(custom_pattern_thresholds(&[]).is_empty()); + assert_eq!(custom_pattern_thresholds(&[f32::NAN, 0.3]).len(), 2); +} + +#[test] +fn custom_pattern_tiles_from_the_top_left() { + // A 2x1 map: even columns need 75%, odd ones 25%. + let map = [0.75, 0.25]; + let mut g = vec![0.5; 6]; + to_bitmap_custom(&mut g, 3, &map, 2, 1); + assert_eq!(g, [0.0, 1.0, 0.0, 0.0, 1.0, 0.0]); + // A map that doesn't match its size, or an empty image width, is a 50% threshold. + let mut g = vec![0.4, 0.6]; + to_bitmap_custom(&mut g, 2, &map, 3, 1); + assert_eq!(g, [0.0, 1.0]); + let mut g = vec![0.6]; + to_bitmap_custom(&mut g, 0, &map, 2, 1); + assert_eq!(g, [1.0]); +} + +/// `gray` converted by `conv` in strips of `rows` rows each, cycling through `rows`. +fn in_strips(gray: &[f32], w: usize, rows: &[usize], mut conv: BitmapStrips<'_>) -> Vec { + let mut out = gray.to_vec(); + let mut rest = out.as_mut_slice(); + for r in rows.iter().cycle() { + if rest.is_empty() { + break; + } + let (strip, tail) = rest.split_at_mut((r * w).min(rest.len())); + conv.convert(strip); + rest = tail; + } + out +} + +#[test] +fn bitmap_strips_match_the_whole_image_however_it_is_cut() { + // #8477: strip boundaries must preserve diffusion state and keep patterns/screens anchored. + let (w, h) = (37, 29); + let gray: Vec = (0..w * h).map(|i| ((i % w) as f32 / w as f32 * 0.7 + (i / w) as f32 / h as f32 * 0.3).fract()).collect(); + let map = custom_pattern_thresholds(&[0.1, 0.9, 0.4, 0.6, 0.2, 0.8]); + let methods = [ + BitmapMethod::Threshold, + BitmapMethod::Pattern, + BitmapMethod::Diffusion, + BitmapMethod::Halftone { cell: 5.5, angle: 30.0, shape: HalftoneShape::Ellipse }, + ]; + for rows in [&[1][..], &[3], &[5, 2, 7], &[h]] { + for method in methods { + let mut whole = gray.clone(); + to_bitmap(&mut whole, w, method); + assert!(whole.iter().all(|v| *v == 0.0 || *v == 1.0), "{method:?}"); + assert_eq!(in_strips(&gray, w, rows, BitmapStrips::new(w, method)), whole, "{method:?} in strips of {rows:?}"); + } + let mut whole = gray.clone(); + to_bitmap_custom(&mut whole, w, &map, 3, 2); + assert_eq!(in_strips(&gray, w, rows, BitmapStrips::custom(w, &map, 3, 2)), whole, "custom in strips of {rows:?}"); + } +} + +#[test] +fn bitmap_strips_match_the_whole_image_with_a_ragged_last_row() { + // A short final row still receives the error from the row above, including in its own strip. + let w = 37; + let gray: Vec = (0..w * 5 + 11).map(|i| ((i % w) as f32 / w as f32 * 0.7 + (i / w) as f32 * 0.13).fract()).collect(); + for method in [BitmapMethod::Diffusion, BitmapMethod::Threshold, BitmapMethod::Pattern] { + let mut whole = gray.clone(); + to_bitmap(&mut whole, w, method); + for rows in [&[1][..], &[5], &[2, 3], &[5, 1]] { + assert_eq!(in_strips(&gray, w, rows, BitmapStrips::new(w, method)), whole, "{method:?} in strips of {rows:?}"); + } + } +} + +/// Floyd–Steinberg over the whole image as `to_bitmap` did before strips (#8477). +fn diffusion_reference(gray: &mut [f32], w: usize) { + let h = gray.len() / w; + let mut buf = gray.to_vec(); + for y in 0..h { + for x in 0..w { + let i = y * w + x; + let old = buf[i]; + let new = if old >= 0.5 { 1.0 } else { 0.0 }; + let error = old - new; + gray[i] = new; + if x + 1 < w { + buf[i + 1] += error * 7.0 / 16.0; + } + if y + 1 < h { + if x > 0 { + buf[i + w - 1] += error * 3.0 / 16.0; + } + buf[i + w] += error * 5.0 / 16.0; + if x + 1 < w { + buf[i + w + 1] += error / 16.0; + } + } + } + } +} + +#[test] +fn bitmap_diffusion_in_strips_is_bit_identical_to_one_pass() { + // Keep error accumulation order identical; one rounding change can flip many later pixels. + let (w, h) = (173, 61); + let gray: Vec = (0..w * h).map(|i| ((i % w) as f32 * 0.013 + (i / w) as f32 * 0.007 + ((i * 7919) % 101) as f32 * 0.001).fract()).collect(); + let mut expected = gray.clone(); + diffusion_reference(&mut expected, w); + for rows in [&[1][..], &[4, 9], &[h]] { + assert_eq!(in_strips(&gray, w, rows, BitmapStrips::new(w, BitmapMethod::Diffusion)), expected, "strips of {rows:?}"); + } +} + +#[test] +fn bitmap_strips_handle_empty_and_ragged_input() { + // A zero width thresholds at 50%, and a short final row is converted too. + for method in [BitmapMethod::Pattern, BitmapMethod::Diffusion, BitmapMethod::Halftone { cell: 4.0, angle: 0.0, shape: HalftoneShape::Round }] { + let mut gray = vec![0.4, 0.6]; + to_bitmap(&mut gray, 0, method); + assert_eq!(gray, [0.0, 1.0], "{method:?}"); + let mut gray = vec![0.3; 7]; + to_bitmap(&mut gray, 3, method); + assert!(gray.iter().all(|v| *v == 0.0 || *v == 1.0), "{method:?}: {gray:?}"); + to_bitmap(&mut [], 3, method); + } +} diff --git a/crates/algo/src/render.rs b/crates/algo/src/render.rs index baba88f93e..2a1867dac8 100644 --- a/crates/algo/src/render.rs +++ b/crates/algo/src/render.rs @@ -1,4 +1,4 @@ -//! Render: Fibers, Lens Flare, Lighting Effects. +//! Render: Clouds, Difference Clouds, Fibers, Lens Flare, Lighting Effects. use photocraft_geom::Rect; @@ -18,6 +18,57 @@ fn value_noise(x: f32, y: f32, seed: u32) -> f32 { top + (bot - top) * sy } +fn cloud_hash(x: i32, y: i32, seed: u32) -> f32 { + let mut h = (x as u32).wrapping_mul(0x8da6_b343) ^ (y as u32).wrapping_mul(0xd816_3841) ^ seed.wrapping_mul(0xcb1a_b31f); + h ^= h >> 13; + h = h.wrapping_mul(0x5bd1_e995); + h ^= h >> 15; + (h & 0x00ff_ffff) as f32 / 0x00ff_ffff as f32 +} + +/// Smooth value noise, summed over octaves (fBm), in 0..1. +fn clouds_value(x: f32, y: f32, base: f32, seed: u32) -> f32 { + let (mut sum, mut amp, mut freq, mut norm) = (0.0, 1.0, 1.0 / base, 0.0); + for o in 0..8u32 { + let (fx, fy) = (x * freq, y * freq); + let (ix, iy) = (fx.floor() as i32, fy.floor() as i32); + let (tx, ty) = (fx - ix as f32, fy - iy as f32); + let (sx, sy) = (tx * tx * (3.0 - 2.0 * tx), ty * ty * (3.0 - 2.0 * ty)); + let s = seed.wrapping_add(o * 7919); + let top = cloud_hash(ix, iy, s) + (cloud_hash(ix + 1, iy, s) - cloud_hash(ix, iy, s)) * sx; + let bot = cloud_hash(ix, iy + 1, s) + (cloud_hash(ix + 1, iy + 1, s) - cloud_hash(ix, iy + 1, s)) * sx; + sum += (top + (bot - top) * sy) * amp; + norm += amp; + amp *= 0.5; + freq *= 2.0; + } + // Stretch the bell-shaped sum so the clouds use the full foreground→background range. + ((sum / norm - 0.5) * 2.2 + 0.5).clamp(0.0, 1.0) +} + +/// Clouds between the foreground and background colours; Difference Clouds subtracts them from +/// the pixels (alpha kept). The cloud size is a quarter of the bounds' longer side, and the +/// pattern is anchored at the bounds' corner (so it moves with a smart object). +pub(crate) fn clouds(src: &Image, out: Rect, ctx: &Ctx, seed: u32, difference: bool, colours: ([f32; 4], [f32; 4])) -> Vec { + let n = src.ch; + let (fg, bg) = colours; + let base = (ctx.bounds.width().max(ctx.bounds.height()) as f32 / 4.0).clamp(16.0, 512.0); + let mut res = src.crop(out); + for (i, px) in res.chunks_exact_mut(n).enumerate() { + let (x, y) = xy(out, i); + let (x, y) = (x - ctx.bounds.x0, y - ctx.bounds.y0); + let t = clouds_value(x as f32, y as f32, base, seed); + let c: [f32; 3] = std::array::from_fn(|k| fg[k] + (bg[k] - fg[k]) * t); + if difference { + let p = rgba(ctx, px); + set_rgba(ctx, px, [(p[0] - c[0]).abs(), (p[1] - c[1]).abs(), (p[2] - c[2]).abs(), p[3]]); + } else { + set_rgba(ctx, px, [c[0], c[1], c[2], 1.0]); + } + } + res +} + /// Fibers: vertical fibrous streaks between the foreground and background /// colours. Variance sets how quickly the colour varies across fibres (and /// how short they are); strength stretches each fibre vertically. @@ -32,8 +83,9 @@ pub(crate) fn fibers(src: &Image, out: Rect, ctx: &Ctx, variance: f32, strength: let fy = fx / (2.0 + st * 1.5) * (0.5 + var / 32.0); let mut res = src.crop(out); for (i, px) in res.chunks_exact_mut(n).enumerate() { + // Anchored at the bounds' corner, so the fibres move with a smart object. let (x, y) = xy(out, i); - let (xf, yf) = (x as f32, y as f32); + let (xf, yf) = ((x - ctx.bounds.x0) as f32, (y - ctx.bounds.y0) as f32); let mut sum = 0.0; let mut amp = 1.0; let mut norm = 0.0; @@ -146,8 +198,9 @@ pub(crate) fn lens_flare(src: &Image, out: Rect, ctx: &Ctx, brightness: f32, cen let mut c = rgba(ctx, px); for k in 0..3 { let a = (f[k] * bright).max(0.0); - // Screen keeps highlights below white for display-referred values. - c[k] = 1.0 - (1.0 - c[k]) * (1.0 - a.min(1.0)); + // Screen (c + a·(1 − c)) keeps display-referred values below white; a 32-bit + // value already above white is left as is rather than pulled down towards it. + c[k] += a.min(1.0) * (1.0 - c[k]).max(0.0); } set_rgba(ctx, px, c); } diff --git a/crates/algo/src/render2.rs b/crates/algo/src/render2.rs index ed6567ca5c..cf1585115c 100644 --- a/crates/algo/src/render2.rs +++ b/crates/algo/src/render2.rs @@ -274,15 +274,13 @@ pub fn composite(surface: &mut Surface, prims: &[Prim], clip: Rect, selection: O pub struct Rng(u64); impl Rng { + /// A generator whose sequence is fixed by `seed` and `salt`. pub fn new(seed: u32, salt: u64) -> Rng { Rng(((seed as u64) << 32) ^ salt.wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ 0x2545_F491_4F6C_DD1D) } + /// The next value of the SplitMix64 sequence. pub fn next_u64(&mut self) -> u64 { - self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); - let mut z = self.0; - z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); - z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); - z ^ (z >> 31) + crate::photo_util::splitmix64(&mut self.0) } /// Uniform in [0, 1). pub fn f(&mut self) -> f32 { @@ -1238,256 +1236,320 @@ fn star(out: &mut Vec, c: (f32, f32), r: f32, points: usize, col: [f32; 4] } } -/// Builds picture-frame primitives inside `canvas`. -pub fn picture_frame(spec: &FrameSpec, canvas: Rect) -> Vec { - let mut out = Vec::new(); - let short = (canvas.width().min(canvas.height()) as f32).max(8.0); - let margin = short * spec.margin.clamp(0.0, 30.0) / 100.0; - let unit = short * (0.008 + spec.size.clamp(1.0, 100.0) / 100.0 * 0.05); - let thick = (unit * 0.04 + short * 0.00012 * spec.thickness.clamp(1.0, 100.0)).max(0.6); - let spacing = unit * (4.0 - 3.2 * spec.arrangement.clamp(1.0, 100.0) / 100.0); - let inset = margin + unit; - let b = Border { - x0: canvas.x0 as f32 + inset, - y0: canvas.y0 as f32 + inset, - w: (canvas.width() as f32 - 2.0 * inset).max(1.0), - h: (canvas.height() as f32 - 2.0 * inset).max(1.0), - }; - let total = b.len(); - let count = (total / spacing).round().max(4.0) as usize; - let step = total / count as f32; - let mut rng = Rng::new(spec.seed, 0xF8A3E + spec.frame as u64); - let (vine, fl, lf) = (spec.vine_color, spec.flower_color, spec.leaf_color); - let style = FRAME_STYLES[(spec.frame.clamp(1, FRAME_STYLES.len() as u32) - 1) as usize]; - let lines = spec.lines.clamp(1, 5) as usize; - let white = [1.0, 1.0, 1.0, 1.0]; - let yellow = [0.98, 0.82, 0.2, 1.0]; - // A wavy vine along the border. - let vine_path = |out: &mut Vec, amp: f32, wl: f32, col: [f32; 4], r: f32| { - let n = (total / 3.0).ceil().max(8.0) as usize; +/// Resolved geometry and palette shared by every picture-frame style. +struct FrameLayout { + b: Border, + unit: f32, + thick: f32, + spacing: f32, + total: f32, + count: usize, + step: f32, + lines: usize, + vine: [f32; 4], + fl: [f32; 4], + lf: [f32; 4], + style: &'static str, +} + +const FRAME_WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0]; +const FRAME_YELLOW: [f32; 4] = [0.98, 0.82, 0.2, 1.0]; + +fn wave_off(s: f32, amp: f32, wl: f32) -> f32 { + amp * (s / wl * std::f32::consts::TAU).sin() +} + +impl FrameLayout { + fn new(spec: &FrameSpec, canvas: Rect) -> Self { + let short = (canvas.width().min(canvas.height()) as f32).max(8.0); + let margin = short * spec.margin.clamp(0.0, 30.0) / 100.0; + let unit = short * (0.008 + spec.size.clamp(1.0, 100.0) / 100.0 * 0.05); + let thick = (unit * 0.04 + short * 0.00012 * spec.thickness.clamp(1.0, 100.0)).max(0.6); + let spacing = unit * (4.0 - 3.2 * spec.arrangement.clamp(1.0, 100.0) / 100.0); + let inset = margin + unit; + let b = Border { + x0: canvas.x0 as f32 + inset, + y0: canvas.y0 as f32 + inset, + w: (canvas.width() as f32 - 2.0 * inset).max(1.0), + h: (canvas.height() as f32 - 2.0 * inset).max(1.0), + }; + let total = b.len(); + let count = (total / spacing).round().max(4.0) as usize; + Self { + b, + unit, + thick, + spacing, + total, + count, + step: total / count as f32, + lines: spec.lines.clamp(1, 5) as usize, + vine: spec.vine_color, + fl: spec.flower_color, + lf: spec.leaf_color, + style: FRAME_STYLES[(spec.frame.clamp(1, FRAME_STYLES.len() as u32) - 1) as usize], + } + } + + /// A wavy vine along the border. + fn vine_path(&self, out: &mut Vec, amp: f32, wl: f32, col: [f32; 4], r: f32) { + let n = (self.total / 3.0).ceil().max(8.0) as usize; let mut prev: Option<(f32, f32)> = None; for k in 0..=n { - let s = k as f32 / n as f32 * total; - let (p, _, nn) = b.at(s); - let o = amp * (s / wl * std::f32::consts::TAU).sin(); + let s = k as f32 / n as f32 * self.total; + let (p, _, nn) = self.b.at(s); + let o = wave_off(s, amp, wl); let q = (p.0 + nn.0 * o, p.1 + nn.1 * o); if let Some(pp) = prev { out.push(Prim::capsule(pp, q, r, r, col, col)); } prev = Some(q); } - }; - let wave_off = |s: f32, amp: f32, wl: f32| amp * (s / wl * std::f32::consts::TAU).sin(); - match style { - "vineWithFlowers" | "vineWithLeaves" | "ivy" | "roses" | "daisies" | "berries" => { - let amp = unit * 0.5; - let wl = step * 2.0; - vine_path(&mut out, amp, wl, vine, thick); - for k in 0..count { - let s = k as f32 * step + step * 0.5; - let (p, t, nn) = b.at(s); - let o = wave_off(s, amp, wl); - let q = (p.0 + nn.0 * o, p.1 + nn.1 * o); - let side = if k % 2 == 0 { 1.0 } else { -1.0 }; - let ta = t.1.atan2(t.0); - let la = ta + side * 0.9 + rng.sym() * 0.3; - let ll = unit * rng.range(0.9, 1.2); - match style { - "vineWithFlowers" => { - leaf(&mut out, q, la, ll, lf); - if k % 2 == 0 { - let c = (q.0 + nn.0 * unit * 0.7 * side, q.1 + nn.1 * unit * 0.7 * side); - flower(&mut out, c, unit * 0.65, 5, fl, yellow, rng.f() * 6.3); - } - } - "vineWithLeaves" => { - leaf(&mut out, q, la, ll, lf); - leaf(&mut out, q, ta - side * 0.9, ll * 0.8, lf); - } - "ivy" => { - for j in 0..3 { - let a = la + (j as f32 - 1.0) * 0.7; - leaf(&mut out, q, a, ll * 0.75, shade([lf[0], lf[1], lf[2]], 0.75 + 0.15 * j as f32)); - } - } - "roses" => { - leaf(&mut out, q, la, ll, lf); - if k % 3 == 0 { - let r = unit * 0.7; - for (j, f) in [1.0f32, 0.75, 0.5, 0.28].iter().enumerate() { - let c = shade([fl[0], fl[1], fl[2]], 0.75 + 0.1 * j as f32); - out.push(Prim::ellipse(q, r * f, r * f * 0.9, rng.f() * 3.0, c, shade([fl[0], fl[1], fl[2]], 0.55))); - } - } - } - "daisies" => { - leaf(&mut out, q, la, ll * 0.8, lf); - if k % 2 == 1 { - flower(&mut out, q, unit * 0.6, 12, white, yellow, rng.f()); - } - } - _ => { - leaf(&mut out, q, la, ll, lf); - for _ in 0..3 { - let c = (q.0 + rng.sym() * unit * 0.4, q.1 + rng.sym() * unit * 0.4); - out.push(Prim::ellipse(c, unit * 0.2, unit * 0.2, 0.0, shade([fl[0], fl[1], fl[2]], 1.2), shade([fl[0], fl[1], fl[2]], 0.6))); - } - } - } - } - } - "bamboo" => { - for edge in 0..4 { - let (s0, l) = match edge { - 0 => (0.0, b.w), - 1 => (b.w, b.h), - 2 => (b.w + b.h, b.w), - _ => (2.0 * b.w + b.h, b.h), - }; - let (a, _, _) = b.at(s0 + 0.01); - let (z, _, _) = b.at(s0 + l - 0.01); - let joints = (l / (spacing * 2.0)).round().max(1.0) as usize; - for j in 0..joints { - let f0 = j as f32 / joints as f32; - let f1 = (j + 1) as f32 / joints as f32; - let p0 = (a.0 + (z.0 - a.0) * f0, a.1 + (z.1 - a.1) * f0); - let p1 = (a.0 + (z.0 - a.0) * f1, a.1 + (z.1 - a.1) * f1); - out.push(Prim::capsule(p0, p1, unit * 0.35, unit * 0.35, vine, shade([vine[0], vine[1], vine[2]], 0.8))); - out.push(Prim::disc(p1, unit * 0.4, shade([vine[0], vine[1], vine[2]], 0.7))); - } - // A few leaves per edge. - for _ in 0..(joints / 2).max(1) { - let f = rng.f(); - let p = (a.0 + (z.0 - a.0) * f, a.1 + (z.1 - a.1) * f); - leaf(&mut out, p, rng.f() * 6.3, unit * 1.4, lf); - } - } - } - "waves" | "zigzag" | "doubleLine" | "rope" => { - for li in 0..lines { - let off = (li as f32 - (lines - 1) as f32 / 2.0) * thick * 4.0; - let n = (total / 2.0).ceil().max(8.0) as usize; - let mut prev: Option<(f32, f32)> = None; - for k in 0..=n { - let s = k as f32 / n as f32 * total; - let (p, _, nn) = b.at(s); - let o = off - + match style { - "waves" => wave_off(s + li as f32 * step * 0.25, unit * 0.4, step), - "zigzag" => { - let ph = (s / step).fract(); - unit * 0.4 * (4.0 * (ph - 0.5).abs() - 1.0) - } - _ => 0.0, - }; - let q = (p.0 + nn.0 * o, p.1 + nn.1 * o); - if style == "rope" { - if k % 2 == 0 { - let (_, t, _) = b.at(s); - out.push(Prim::ellipse( - q, - unit * 0.45, - unit * 0.2, - t.1.atan2(t.0) + 0.7, - shade([vine[0], vine[1], vine[2]], 1.1), - shade([vine[0], vine[1], vine[2]], 0.65), - )); - } - } else if let Some(pp) = prev { - out.push(Prim::capsule(pp, q, thick, thick, vine, vine)); - } - prev = Some(q); - } - if style == "doubleLine" { - // A second rule inside each line and corner squares. - let inner = Border { - x0: b.x0 + unit * 0.6 + off, - y0: b.y0 + unit * 0.6 + off, - w: b.w - 2.0 * (unit * 0.6 + off), - h: b.h - 2.0 * (unit * 0.6 + off), - }; - for e in 0..4 { - let (p, _, _) = inner.at([0.0, inner.w, inner.w + inner.h, 2.0 * inner.w + inner.h][e]); - let (q, _, _) = inner.at([inner.w, inner.w + inner.h, 2.0 * inner.w + inner.h, inner.len() - 0.001][e]); - out.push(Prim::capsule(p, q, thick * 0.6, thick * 0.6, vine, vine)); - } - } - } - if style == "doubleLine" { - for e in 0..4 { - let (p, _, _) = b.at([0.0, b.w, b.w + b.h, 2.0 * b.w + b.h][e]); - out.push(Prim::ellipse(p, unit * 0.45, unit * 0.45, std::f32::consts::FRAC_PI_4, fl, fl)); - } - } - } - "dots" | "scallops" | "snowflakes" | "stars" | "hearts" => { - for k in 0..count { - let s = k as f32 * step; - let (p, t, nn) = b.at(s); - let ta = t.1.atan2(t.0); - match style { - "dots" => { - let r = unit * if k % 2 == 0 { 0.4 } else { 0.22 }; - out.push(Prim::ellipse(p, r, r, 0.0, shade([fl[0], fl[1], fl[2]], 1.15), shade([fl[0], fl[1], fl[2]], 0.75))); - } - "scallops" => { - let (c0, _, _) = b.at(s - step * 0.5); - let mid = ((c0.0 + p.0) / 2.0, (c0.1 + p.1) / 2.0); - let n = 8; - for j in 0..n { - let a0 = std::f32::consts::PI * j as f32 / n as f32; - let a1 = std::f32::consts::PI * (j + 1) as f32 / n as f32; - let r = step * 0.5; - let pt = |a: f32| (mid.0 - t.0 * r * a.cos() - nn.0 * r * a.sin(), mid.1 - t.1 * r * a.cos() - nn.1 * r * a.sin()); - out.push(Prim::capsule(pt(a0), pt(a1), thick, thick, vine, vine)); - } - } - "snowflakes" => { - let r = unit * 0.6; - let c = [0.85, 0.92, 1.0, 1.0]; - for j in 0..6 { - let a = ta + j as f32 * std::f32::consts::FRAC_PI_3; - let e = (p.0 + a.cos() * r, p.1 + a.sin() * r); - out.push(Prim::capsule(p, e, (thick * 0.6).max(0.6), (thick * 0.4).max(0.5), c, c)); - let m = (p.0 + a.cos() * r * 0.6, p.1 + a.sin() * r * 0.6); - for sd in [-0.7f32, 0.7] { - let b2 = (m.0 + (a + sd).cos() * r * 0.3, m.1 + (a + sd).sin() * r * 0.3); - out.push(Prim::capsule(m, b2, (thick * 0.4).max(0.5), 0.4, c, c)); - } - } - } - "stars" => star(&mut out, p, unit * 0.55, 5, if k % 2 == 0 { yellow } else { fl }, ta - std::f32::consts::FRAC_PI_2), - _ => { - // Heart: two lobes and a point towards the inside. - let r = unit * 0.3; - let (tx, ty) = t; - let l = (p.0 - tx * r * 0.8 - nn.0 * r * 0.3, p.1 - ty * r * 0.8 - nn.1 * r * 0.3); - let rr = (p.0 + tx * r * 0.8 - nn.0 * r * 0.3, p.1 + ty * r * 0.8 - nn.1 * r * 0.3); - let tip = (p.0 + nn.0 * r * 1.6, p.1 + nn.1 * r * 1.6); - out.push(Prim::capsule(l, tip, r, 0.6, fl, fl)); - out.push(Prim::capsule(rr, tip, r, 0.6, fl, fl)); - } - } - } - } - _ => {} } - // Fade: lower the opacity of ornaments that reach further inside. - let fade = spec.fade.clamp(0.0, 100.0) / 100.0; - if fade > 0.0 { + + /// Vine styles: a wavy stem with a leaf cluster or bloom at each step. + fn vine_styles(&self, out: &mut Vec, rng: &mut Rng) { + let (unit, step, fl, lf) = (self.unit, self.step, self.fl, self.lf); + let amp = unit * 0.5; + let wl = step * 2.0; + self.vine_path(out, amp, wl, self.vine, self.thick); + for k in 0..self.count { + let s = k as f32 * step + step * 0.5; + let (p, t, nn) = self.b.at(s); + let o = wave_off(s, amp, wl); + let q = (p.0 + nn.0 * o, p.1 + nn.1 * o); + let side = if k % 2 == 0 { 1.0 } else { -1.0 }; + let ta = t.1.atan2(t.0); + let la = ta + side * 0.9 + rng.sym() * 0.3; + let ll = unit * rng.range(0.9, 1.2); + match self.style { + "vineWithFlowers" => { + leaf(out, q, la, ll, lf); + if k % 2 == 0 { + let c = (q.0 + nn.0 * unit * 0.7 * side, q.1 + nn.1 * unit * 0.7 * side); + flower(out, c, unit * 0.65, 5, fl, FRAME_YELLOW, rng.f() * 6.3); + } + } + "vineWithLeaves" => { + leaf(out, q, la, ll, lf); + leaf(out, q, ta - side * 0.9, ll * 0.8, lf); + } + "ivy" => { + for j in 0..3 { + let a = la + (j as f32 - 1.0) * 0.7; + leaf(out, q, a, ll * 0.75, shade([lf[0], lf[1], lf[2]], 0.75 + 0.15 * j as f32)); + } + } + "roses" => { + leaf(out, q, la, ll, lf); + if k % 3 == 0 { + let r = unit * 0.7; + for (j, f) in [1.0f32, 0.75, 0.5, 0.28].iter().enumerate() { + let c = shade([fl[0], fl[1], fl[2]], 0.75 + 0.1 * j as f32); + out.push(Prim::ellipse(q, r * f, r * f * 0.9, rng.f() * 3.0, c, shade([fl[0], fl[1], fl[2]], 0.55))); + } + } + } + "daisies" => { + leaf(out, q, la, ll * 0.8, lf); + if k % 2 == 1 { + flower(out, q, unit * 0.6, 12, FRAME_WHITE, FRAME_YELLOW, rng.f()); + } + } + _ => { + leaf(out, q, la, ll, lf); + for _ in 0..3 { + let c = (q.0 + rng.sym() * unit * 0.4, q.1 + rng.sym() * unit * 0.4); + out.push(Prim::ellipse(c, unit * 0.2, unit * 0.2, 0.0, shade([fl[0], fl[1], fl[2]], 1.2), shade([fl[0], fl[1], fl[2]], 0.6))); + } + } + } + } + } + + /// Bamboo: jointed canes along each edge with a few leaves. + fn bamboo(&self, out: &mut Vec, rng: &mut Rng) { + let (b, unit, vine) = (&self.b, self.unit, self.vine); + for edge in 0..4 { + let (s0, l) = match edge { + 0 => (0.0, b.w), + 1 => (b.w, b.h), + 2 => (b.w + b.h, b.w), + _ => (2.0 * b.w + b.h, b.h), + }; + let (a, _, _) = b.at(s0 + 0.01); + let (z, _, _) = b.at(s0 + l - 0.01); + let joints = (l / (self.spacing * 2.0)).round().max(1.0) as usize; + for j in 0..joints { + let f0 = j as f32 / joints as f32; + let f1 = (j + 1) as f32 / joints as f32; + let p0 = (a.0 + (z.0 - a.0) * f0, a.1 + (z.1 - a.1) * f0); + let p1 = (a.0 + (z.0 - a.0) * f1, a.1 + (z.1 - a.1) * f1); + out.push(Prim::capsule(p0, p1, unit * 0.35, unit * 0.35, vine, shade([vine[0], vine[1], vine[2]], 0.8))); + out.push(Prim::disc(p1, unit * 0.4, shade([vine[0], vine[1], vine[2]], 0.7))); + } + // A few leaves per edge. + for _ in 0..(joints / 2).max(1) { + let f = rng.f(); + let p = (a.0 + (z.0 - a.0) * f, a.1 + (z.1 - a.1) * f); + leaf(out, p, rng.f() * 6.3, unit * 1.4, self.lf); + } + } + } + + /// Continuous rule styles: waves, zigzag, double line and rope. + fn rule_styles(&self, out: &mut Vec) { + let (b, unit, thick, step, vine) = (&self.b, self.unit, self.thick, self.step, self.vine); + for li in 0..self.lines { + let off = (li as f32 - (self.lines - 1) as f32 / 2.0) * thick * 4.0; + let n = (self.total / 2.0).ceil().max(8.0) as usize; + let mut prev: Option<(f32, f32)> = None; + for k in 0..=n { + let s = k as f32 / n as f32 * self.total; + let (p, _, nn) = b.at(s); + let o = off + + match self.style { + "waves" => wave_off(s + li as f32 * step * 0.25, unit * 0.4, step), + "zigzag" => { + let ph = (s / step).fract(); + unit * 0.4 * (4.0 * (ph - 0.5).abs() - 1.0) + } + _ => 0.0, + }; + let q = (p.0 + nn.0 * o, p.1 + nn.1 * o); + if self.style == "rope" { + if k % 2 == 0 { + let (_, t, _) = b.at(s); + out.push(Prim::ellipse( + q, + unit * 0.45, + unit * 0.2, + t.1.atan2(t.0) + 0.7, + shade([vine[0], vine[1], vine[2]], 1.1), + shade([vine[0], vine[1], vine[2]], 0.65), + )); + } + } else if let Some(pp) = prev { + out.push(Prim::capsule(pp, q, thick, thick, vine, vine)); + } + prev = Some(q); + } + if self.style == "doubleLine" { + self.inner_rule(out, off); + } + } + if self.style == "doubleLine" { + for e in 0..4 { + let (p, _, _) = b.at([0.0, b.w, b.w + b.h, 2.0 * b.w + b.h][e]); + out.push(Prim::ellipse(p, unit * 0.45, unit * 0.45, std::f32::consts::FRAC_PI_4, self.fl, self.fl)); + } + } + } + + /// A second rule inside each double line. + fn inner_rule(&self, out: &mut Vec, off: f32) { + let (b, unit) = (&self.b, self.unit); + let inner = Border { x0: b.x0 + unit * 0.6 + off, y0: b.y0 + unit * 0.6 + off, w: b.w - 2.0 * (unit * 0.6 + off), h: b.h - 2.0 * (unit * 0.6 + off) }; + for e in 0..4 { + let (p, _, _) = inner.at([0.0, inner.w, inner.w + inner.h, 2.0 * inner.w + inner.h][e]); + let (q, _, _) = inner.at([inner.w, inner.w + inner.h, 2.0 * inner.w + inner.h, inner.len() - 0.001][e]); + out.push(Prim::capsule(p, q, self.thick * 0.6, self.thick * 0.6, self.vine, self.vine)); + } + } + + /// Evenly spaced motif styles: dots, scallops, snowflakes, stars and hearts. + fn motif_styles(&self, out: &mut Vec) { + let (unit, fl) = (self.unit, self.fl); + for k in 0..self.count { + let s = k as f32 * self.step; + let (p, t, nn) = self.b.at(s); + let ta = t.1.atan2(t.0); + match self.style { + "dots" => { + let r = unit * if k % 2 == 0 { 0.4 } else { 0.22 }; + out.push(Prim::ellipse(p, r, r, 0.0, shade([fl[0], fl[1], fl[2]], 1.15), shade([fl[0], fl[1], fl[2]], 0.75))); + } + "scallops" => self.scallop(out, s, p, t, nn), + "snowflakes" => self.snowflake(out, p, ta), + "stars" => star(out, p, unit * 0.55, 5, if k % 2 == 0 { FRAME_YELLOW } else { fl }, ta - std::f32::consts::FRAC_PI_2), + _ => self.heart(out, p, t, nn), + } + } + } + + fn scallop(&self, out: &mut Vec, s: f32, p: (f32, f32), t: (f32, f32), nn: (f32, f32)) { + let (c0, _, _) = self.b.at(s - self.step * 0.5); + let mid = ((c0.0 + p.0) / 2.0, (c0.1 + p.1) / 2.0); + let n = 8; + for j in 0..n { + let a0 = std::f32::consts::PI * j as f32 / n as f32; + let a1 = std::f32::consts::PI * (j + 1) as f32 / n as f32; + let r = self.step * 0.5; + let pt = |a: f32| (mid.0 - t.0 * r * a.cos() - nn.0 * r * a.sin(), mid.1 - t.1 * r * a.cos() - nn.1 * r * a.sin()); + out.push(Prim::capsule(pt(a0), pt(a1), self.thick, self.thick, self.vine, self.vine)); + } + } + + fn snowflake(&self, out: &mut Vec, p: (f32, f32), ta: f32) { + let thick = self.thick; + let r = self.unit * 0.6; + let c = [0.85, 0.92, 1.0, 1.0]; + for j in 0..6 { + let a = ta + j as f32 * std::f32::consts::FRAC_PI_3; + let e = (p.0 + a.cos() * r, p.1 + a.sin() * r); + out.push(Prim::capsule(p, e, (thick * 0.6).max(0.6), (thick * 0.4).max(0.5), c, c)); + let m = (p.0 + a.cos() * r * 0.6, p.1 + a.sin() * r * 0.6); + for sd in [-0.7f32, 0.7] { + let b2 = (m.0 + (a + sd).cos() * r * 0.3, m.1 + (a + sd).sin() * r * 0.3); + out.push(Prim::capsule(m, b2, (thick * 0.4).max(0.5), 0.4, c, c)); + } + } + } + + /// Heart: two lobes and a point towards the inside. + fn heart(&self, out: &mut Vec, p: (f32, f32), t: (f32, f32), nn: (f32, f32)) { + let r = self.unit * 0.3; + let (tx, ty) = t; + let l = (p.0 - tx * r * 0.8 - nn.0 * r * 0.3, p.1 - ty * r * 0.8 - nn.1 * r * 0.3); + let rr = (p.0 + tx * r * 0.8 - nn.0 * r * 0.3, p.1 + ty * r * 0.8 - nn.1 * r * 0.3); + let tip = (p.0 + nn.0 * r * 1.6, p.1 + nn.1 * r * 1.6); + out.push(Prim::capsule(l, tip, r, 0.6, self.fl, self.fl)); + out.push(Prim::capsule(rr, tip, r, 0.6, self.fl, self.fl)); + } + + /// Fade: lower the opacity of ornaments that reach further inside. + fn apply_fade(&self, out: &mut [Prim], fade: f32, canvas: Rect) { let cx = canvas.x0 as f32 + canvas.width() as f32 / 2.0; let cy = canvas.y0 as f32 + canvas.height() as f32 / 2.0; - for p in &mut out { + for p in out { let c = match p.shape { Shape::Capsule { a, .. } => a, Shape::Ellipse { c, .. } => c, }; - let edge = ((c.0 - cx).abs() / (b.w / 2.0)).max((c.1 - cy).abs() / (b.h / 2.0)).clamp(0.0, 1.0); + let edge = ((c.0 - cx).abs() / (self.b.w / 2.0)).max((c.1 - cy).abs() / (self.b.h / 2.0)).clamp(0.0, 1.0); let k = 1.0 - fade * (1.0 - edge) * 8.0; p.c0[3] *= k.clamp(0.0, 1.0); p.c1[3] *= k.clamp(0.0, 1.0); } } +} + +/// Builds picture-frame primitives inside `canvas`. +pub fn picture_frame(spec: &FrameSpec, canvas: Rect) -> Vec { + let mut out = Vec::new(); + let layout = FrameLayout::new(spec, canvas); + let mut rng = Rng::new(spec.seed, 0xF8A3E + spec.frame as u64); + match layout.style { + "vineWithFlowers" | "vineWithLeaves" | "ivy" | "roses" | "daisies" | "berries" => layout.vine_styles(&mut out, &mut rng), + "bamboo" => layout.bamboo(&mut out, &mut rng), + "waves" | "zigzag" | "doubleLine" | "rope" => layout.rule_styles(&mut out), + "dots" | "scallops" | "snowflakes" | "stars" | "hearts" => layout.motif_styles(&mut out), + _ => {} + } + let fade = spec.fade.clamp(0.0, 100.0) / 100.0; + if fade > 0.0 { + layout.apply_fade(&mut out, fade, canvas); + } out } diff --git a/crates/algo/src/resample.rs b/crates/algo/src/resample.rs index bd728e379d..4dcf0b6944 100644 --- a/crates/algo/src/resample.rs +++ b/crates/algo/src/resample.rs @@ -159,8 +159,37 @@ fn cover(o: i32, scale: f64) -> (i32, i32) { (a, ((o + 1) as f64 / scale + 1e-9).floor().max(a as f64 + 1.0) as i32) } -/// Unclamped weights of output sample `o` over source samples `lo..lo + len`. -fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>) -> (i32, Vec) { +/// The source samples worth a weight each: the edge, or the content with one sample of slack on +/// each side to carry the weight of everything beyond it (read as the default pixel). +fn keep_range(edge: Option<(i32, i32)>, content: (i32, i32)) -> (i64, i64) { + let (a, b) = edge.unwrap_or((content.0.saturating_sub(1), content.1.saturating_add(1))); + (i64::from(a), i64::from(b.max(a))) +} + +/// Adds `v` spread evenly over the source samples `[a, b)` to `w`, which holds the samples +/// from `lo`: samples beyond either end of it add to that end slot instead of needing a slot. +fn spread(w: &mut [f64], lo: i64, a: i64, b: i64, v: f64) { + let Some(last) = w.len().checked_sub(1) else { return }; + let each = v / (b - a).max(1) as f64; + let end = lo + w.len() as i64; + let (ia, ib) = (a.max(lo), b.min(end)); + if ia < ib { + for slot in w.get_mut((ia - lo) as usize..(ib - lo) as usize).into_iter().flatten() { + *slot += each; + } + } + if a < lo { + w[0] += each * (b.min(lo) - a) as f64; + } + if b > end { + w[last] += each * (b - a.max(end)) as f64; + } +} + +/// Weights of output sample `o` over source samples `lo..lo + len`, unclamped except that +/// reductions fold what lies beyond the edge (or the `content`) onto its ends, so a tiny scale +/// doesn't cost a weight per source sample it covers. +fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>, content: (i32, i32)) -> (i32, Vec) { match kernel { Kernel::Nearest => { let u = (o as f64 + 0.5) / scale - 0.5; @@ -168,7 +197,11 @@ fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>) -> (i32 } Kernel::Bilinear if scale < 0.5 => { let (a, b) = cover(o, scale); - (a, vec![1.0 / (b - a) as f64; (b - a) as usize]) + let (ka, kb) = keep_range(edge, content); + let (lo, hi) = (i64::from(a).clamp(ka, kb), (i64::from(b) - 1).clamp(ka, kb)); + let mut w = vec![0.0; (hi - lo + 1) as usize]; + spread(&mut w, lo, i64::from(a), i64::from(b), 1.0); + (lo as i32, w) } Kernel::Bilinear => { let u = position(o, scale, edge); @@ -179,18 +212,18 @@ fn weights(o: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32)>) -> (i32 // Averaged down to four times the target first (pixels of `inner`), then filtered. let inner = 4.0 * scale; let inner_edge = edge.map(|(a, b)| ((a as f64 * inner - 1e-9).ceil() as i32, ((b + 1) as f64 * inner - 1e-9).ceil() as i32 - 1)); - let (k0, kw) = weights(o, 0.25, Kernel::Keys { a, stretch }, inner_edge); + let (k0, kw) = weights(o, 0.25, Kernel::Keys { a, stretch }, inner_edge, (i32::MIN / 4, i32::MAX / 4)); let clamp = |k: i32| inner_edge.map_or(k, |(a, b)| k.clamp(a, b.max(a))); let boxes: Vec<(i32, i32)> = (k0..).zip(&kw).map(|(k, _)| cover(clamp(k), inner)).collect(); - let lo = boxes.iter().map(|b| b.0).min().unwrap_or(0); - let hi = boxes.iter().map(|b| b.1).max().unwrap_or(lo); - let mut w = vec![0.0; (hi - lo).max(0) as usize]; + let (ka, kb) = keep_range(edge, content); + let lo = i64::from(boxes.iter().map(|b| b.0).min().unwrap_or(0)); + let hi = i64::from(boxes.iter().map(|b| b.1).max().unwrap_or(lo as i32)); + let (klo, khi) = (lo.clamp(ka, kb), (hi - 1).clamp(ka, kb)); + let mut w = vec![0.0; (khi - klo + 1) as usize]; for ((a, b), v) in boxes.into_iter().zip(kw) { - for slot in w.get_mut((a - lo) as usize..(b - lo) as usize).into_iter().flatten() { - *slot += v / (b - a) as f64; - } + spread(&mut w, klo, i64::from(a), i64::from(b), v); } - (lo, w) + (klo as i32, w) } Kernel::Keys { a, stretch } => { let u = position(o, scale, edge); @@ -225,7 +258,7 @@ fn taps(dst0: i32, dst1: i32, scale: f64, kernel: Kernel, edge: Option<(i32, i32 let kernel = if scale == 1.0 { Kernel::Nearest } else { kernel }; (dst0..dst1) .map(|o| { - let (lo, w) = weights(o, scale, kernel, edge); + let (lo, w) = weights(o, scale, kernel, edge, content); let hi = lo + w.len() as i32 - 1; // Fold the weights of taps beyond the edge onto the edge sample. let start = clamp(lo); @@ -481,391 +514,4 @@ pub fn crop_surface(s: &Surface, keep: Rect) -> Surface { } #[cfg(test)] -mod tests { - use super::*; - use photocraft_color::{ColorMode, PixelFormat, SampleType}; - - fn ramp(sample: SampleType) -> Surface { - let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, sample, true)); - let r = Rect::new(0, 0, 64, 32); - let v: Vec = (0..32).flat_map(|_| (0..64).flat_map(|x| [x as f32 / 63.0, 0.5, 1.0 - x as f32 / 63.0, 1.0])).collect(); - s.write_region(r, &v); - s - } - - #[test] - fn fixed_channel_rows_match_dynamic_resampling() { - fn check() { - for scale in [0.12, 0.5, 1.0, 2.3] { - for filter in [Kernel::Nearest, Kernel::Bilinear, keys(Resample::Bicubic, scale), keys(Resample::BicubicSharper, scale), Kernel::Lanczos] { - for clamp in [None, Some((-3, 31))] { - let ts = taps(-5, 41, scale, filter, clamp, (-3, 31)); - let row: Vec = (0..35 * N).map(|i| ((i * 73) % 251) as f32 / 250.0).collect(); - let def: Vec = (0..N).map(|i| i as f32 / 7.0).collect(); - let mut expected = vec![0.0; ts.len() * N]; - let mut actual = expected.clone(); - horizontal_dynamic(&row, &mut expected, &ts, -3, &def, N); - horizontal_fixed::(&row, &mut actual, &ts, -3, &def); - assert_eq!(actual, expected, "N={N} scale={scale} filter={filter:?} clamp={clamp:?}"); - } - } - } - } - check::<1>(); - check::<2>(); - check::<3>(); - check::<4>(); - check::<5>(); - } - - /// Photoshop 25.4's Image Size of a 16-bit greyscale pattern, in levels of 32768. Covers the - /// filters and their 2-D rules: Sharper reducing the width while the height grows is designed - /// for the height (not widened) and runs the width first; Bicubic Automatic splits a mixed - /// resize into Sharper and Smoother; Bicubic widens for the height's 0.6 while the width - /// grows; Smoother below ¼ averages down first; Bilinear below ½ averages whole pixels. On black - /// and white the overshoot is clipped between the passes, so their order shows. - #[test] - fn matches_photoshop_image_size() { - // (black and white pattern, filter, size, new size, Photoshop's levels) - type Probe = (bool, Resample, (i32, i32), (i32, i32), &'static [u16]); - let cases: [Probe; 9] = [ - ( - false, - Resample::BicubicSharper, - (12, 10), - (7, 16), - &[ - 3777, 13165, 22219, 16176, 10460, 19488, 29643, 11249, 12634, 21696, 13330, 10319, 18965, 29120, 21280, 11204, 20279, 16184, 8200, 17548, - 27703, 21824, 10182, 19259, 26967, 5238, 16528, 26683, 17366, 8895, 17922, 32768, 2402, 15190, 25345, 17493, 7834, 16903, 31316, 1748, - 14172, 24326, 24718, 6368, 15746, 30390, 0, 13107, 23169, 31215, 4790, 14456, 27639, 5080, 11597, 21880, 29649, 3864, 13507, 24136, 20600, - 10264, 20931, 28009, 0, 12446, 21385, 28735, 8754, 19642, 26154, 4049, 10895, 20832, 23523, 7620, 18485, 23204, 20126, 8787, 19818, 21447, - 6173, 17467, 20390, 31862, 6616, 18480, 24094, 6500, 16129, 19749, 27472, 5810, 17460, 28281, 7655, 15108, 18394, 25515, 4427, 16043, - 28749, 7026, 13691, 17871, 24992, 3904, 15520, 27200, 6075, 13168, - ], - ), - ( - false, - Resample::BicubicAutomatic, - (12, 10), - (7, 16), - &[ - 4201, 12337, 27677, 15894, 4816, 20021, 29631, 10348, 9882, 27304, 14492, 3766, 19574, 29046, 18743, 5944, 25299, 16116, 4147, 17829, - 27787, 19399, 4206, 21790, 23748, 7966, 15117, 26569, 15941, 2695, 19445, 28341, 10104, 13050, 25465, 16251, 2613, 18023, 26770, 8432, - 11951, 24281, 22800, 7141, 15351, 24790, 5953, 11004, 23130, 28322, 10921, 12440, 24940, 9659, 9368, 22012, 27611, 10006, 10488, 26419, - 18657, 7071, 20807, 25492, 6937, 9239, 26574, 22363, 5435, 19688, 24808, 9136, 8184, 25295, 19708, 3939, 18677, 25053, 18234, 7120, 24061, - 17941, 2545, 17568, 24869, 23748, 6081, 21401, 21862, 6018, 15056, 23602, 22177, 4859, 17816, 28450, 11911, 11653, 22253, 20263, 3595, - 15790, 29734, 12969, 9683, 21668, 19678, 3010, 15424, 28429, 11728, 9300, - ], - ), - ( - false, - Resample::Bicubic, - (12, 10), - (20, 6), - &[ - 11517, 10000, 8617, 9364, 12272, 16362, 21396, 25508, 25506, 19474, 10887, 5289, 5842, 10286, 14986, 18566, 21910, 25511, 28611, 29922, - 24788, 17949, 8497, 4593, 7309, 12594, 17150, 20945, 24892, 26196, 22979, 16123, 9691, 7483, 10061, 14653, 18841, 22420, 25514, 26824, - 25702, 23396, 17586, 11378, 8127, 8882, 12310, 17039, 22345, 25871, 24700, 18125, 9951, 5493, 6802, 11215, 15576, 19259, 22384, 23694, - 26692, 26985, 22732, 14270, 6889, 4950, 8348, 13583, 17529, 21570, 25966, 27275, 22817, 14643, 8068, 6911, 10694, 15930, 19443, 20705, - 21499, 23413, 25601, 24336, 18665, 11686, 7800, 8947, 12974, 18067, 22707, 25285, 23077, 16645, 9789, 6586, 8147, 12329, 15935, 17465, - 17962, 20462, 24615, 25377, 20036, 11458, 5421, 5415, 9528, 14134, 17782, 22482, 26926, 27479, 21881, 13308, 7533, 7734, 11549, 13992, - ], - ), - ( - false, - Resample::BicubicSmoother, - (40, 3), - (9, 3), - &[ - 11315, 18740, 17475, 15974, 16503, 16042, 18215, 14712, 16238, 18482, 14351, 14664, 18012, 20260, 13338, 15511, 18468, 18275, 16790, 19168, - 14745, 14998, 20341, 18150, 13819, 16493, 16432, - ], - ), - ( - false, - Resample::Bilinear, - (12, 10), - (5, 4), - &[11316, 14541, 17920, 10855, 24679, 19763, 9933, 23757, 6247, 20070, 25601, 5325, 19149, 17306, 15463, 20992, 16384, 14541, 23143, 10854], - ), - ( - false, - Resample::Nearest, - (12, 10), - (7, 16), - &[ - 1638, 12698, 23757, 3482, 9011, 20070, 31130, 1638, 12698, 23757, 3482, 9011, 20070, 31130, 31130, 10854, 21914, 1638, 7168, 18227, 29286, - 29286, 9011, 20070, 31130, 5325, 16384, 27443, 29286, 9011, 20070, 31130, 5325, 16384, 27443, 27443, 7168, 18227, 29286, 3482, 14541, - 25600, 25600, 5325, 16384, 27443, 1638, 12698, 23757, 25600, 5325, 16384, 27443, 1638, 12698, 23757, 23757, 3482, 14541, 25600, 31130, - 10854, 21914, 23757, 3482, 14541, 25600, 31130, 10854, 21914, 21914, 1638, 12698, 23757, 29286, 9011, 20070, 20070, 31130, 10854, 21914, - 27443, 7168, 18227, 20070, 31130, 10854, 21914, 27443, 7168, 18227, 18227, 29286, 9011, 20070, 25600, 5325, 16384, 16384, 27443, 7168, - 18227, 23757, 3482, 14541, 16384, 27443, 7168, 18227, 23757, 3482, 14541, - ], - ), - ( - true, - Resample::Bicubic, - (12, 10), - (20, 6), - &[ - 9960, 6313, 993, 0, 4525, 14807, 26472, 32768, 32768, 23510, 10514, 0, 0, 0, 10110, 22576, 31764, 32768, 32768, 32768, 26136, 18594, 6164, - 0, 0, 2565, 14499, 26983, 32768, 32768, 26358, 14709, 3969, 0, 0, 6602, 17734, 27952, 32768, 32768, 31590, 27808, 18307, 7429, 0, 0, 3712, - 14435, 26473, 32768, 31131, 20665, 7535, 0, 0, 2177, 13238, 25049, 32768, 32768, 32768, 32768, 26136, 14174, 2548, 0, 0, 6190, 18705, - 30273, 32768, 32768, 25517, 13238, 2177, 0, 0, 7250, 19418, 26624, 32768, 32768, 32768, 28203, 18522, 7200, 0, 0, 3285, 14794, 26862, - 32768, 29913, 19013, 6667, 0, 0, 4443, 14506, 20751, 17344, 23066, 31075, 32361, 23949, 10738, 0, 0, 0, 10110, 22657, 32768, 32768, 32768, - 22658, 10192, 1004, 0, 0, 0, - ], - ), - ( - true, - Resample::BicubicSharper, - (12, 10), - (7, 16), - &[ - 801, 3050, 32489, 16260, 0, 29819, 32767, 9161, 1781, 29578, 14350, 0, 32768, 32767, 21132, 0, 25409, 17862, 0, 26925, 32767, 22769, 0, - 24839, 27566, 0, 9397, 32768, 19813, 0, 27795, 32768, 0, 0, 32768, 20744, 0, 25797, 32768, 0, 0, 32768, 27555, 0, 11184, 32768, 0, 0, - 32768, 32768, 0, 0, 32768, 5110, 0, 30386, 32768, 0, 0, 32768, 20385, 0, 23267, 32768, 0, 0, 32768, 29130, 0, 19191, 32768, 2670, 0, 31290, - 25387, 0, 21263, 32767, 20198, 0, 21586, 24510, 0, 23714, 32768, 32768, 0, 14165, 27804, 1007, 14254, 32768, 30149, 0, 16077, 32112, 2886, - 1884, 21043, 29607, 0, 16384, 32768, 3566, 0, 12683, 29616, 0, 16384, 32768, 3196, 0, - ], - ), - ( - true, - Resample::BicubicSharper, - (12, 10), - (20, 6), - &[ - 9273, 5043, 0, 0, 2698, 14484, 28464, 32768, 32768, 24841, 10877, 0, 0, 0, 9684, 22346, 31492, 32768, 32768, 32768, 25960, 18430, 4978, 0, - 0, 977, 15047, 28475, 32768, 32768, 27939, 14157, 1813, 0, 0, 5296, 17828, 28439, 32768, 32768, 32768, 27849, 17576, 6209, 0, 0, 2169, - 15076, 28935, 32768, 32768, 22167, 7399, 0, 0, 211, 12977, 24638, 32768, 32768, 32768, 32768, 25960, 14244, 824, 0, 0, 5093, 18802, 31993, - 32768, 32768, 26467, 12977, 211, 0, 0, 6300, 19418, 26712, 32768, 32768, 32768, 27907, 17874, 5820, 0, 0, 1853, 16055, 29518, 32768, 32768, - 20889, 5880, 0, 0, 2879, 13798, 20185, 19010, 25565, 32768, 32302, 25431, 11478, 0, 0, 0, 9684, 23083, 32768, 32768, 32768, 23084, 10422, - 1276, 0, 0, 0, - ], - ), - ]; - for (binary, filter, (w, h), (wo, ho), want) in cases { - let mut s = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::U16, false)); - let canvas = Rect::new(0, 0, w, h); - let level = |x: i32, y: i32| match ((x * 37 + y * 101) % 17) as f32 { - k if binary => { - if k > 8.0 { - 32767.0 / 32768.0 - } else { - 0.0 - } - } - k => ((k / 16.0 * 0.9 + 0.05) * 32768.0).round() / 32768.0, - }; - let px: Vec = (0..h).flat_map(|y| (0..w).map(move |x| level(x, y))).collect(); - s.write_region(canvas, &px); - let o = resize_surface_in_canvas(&s, wo as f64 / w as f64, ho as f64 / h as f64, filter, canvas); - let got = o.read_region(Rect::new(0, 0, wo, ho)); - assert_eq!(got.len(), want.len(), "{filter:?}"); - let worst = got.iter().zip(want).map(|(g, &p)| (g * 32768.0 - p as f32).abs()).fold(0.0f32, f32::max); - assert!(worst <= 8.0, "{filter:?} {w}x{h} to {wo}x{ho}: {worst} levels of 32768 from Photoshop"); - } - } - - /// Photoshop's Bicubic doubles with the Keys cubic at a = −0.75 (Catmull-Rom is −0.5): an - /// impulse spreads as −9, −27, 67, 225 /256 and back, as Photoshop 25.4 gives it. - #[test] - fn bicubic_is_photoshops_keys_cubic() { - let mut s = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false)); - let mut row = [0.25; 30]; - row[10] = 0.75; - s.write_region(Rect::new(0, 0, 30, 1), &row); - let o = resize_surface(&s, 2.0, 1.0, Resample::Bicubic); - let got: Vec = o.read_region(Rect::new(17, 0, 25, 1)).iter().map(|v| (v - 0.25) * 512.0).collect(); - for (g, want) in got.iter().zip([-9.0, -27.0, 67.0, 225.0, 225.0, 67.0, -27.0, -9.0]) { - assert!((g - want).abs() < 1e-3, "{got:?}"); - } - } - - #[test] - fn identity_scale_is_lossless() { - for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { - let s = ramp(SampleType::U16); - let o = resize_surface(&s, 1.0, 1.0, f); - let r = Rect::new(0, 0, 64, 32); - let (a, b) = (s.read_region(r), o.read_region(r)); - let d = a.iter().zip(&b).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); - assert!(d < 1e-4, "{f:?} {d}"); - } - } - - #[test] - fn half_and_double_sizes() { - for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos, Resample::PreserveDetails] { - let s = ramp(SampleType::F32); - let half = resize_surface(&s, 0.5, 0.5, f); - assert_eq!(half.content_bounds(), Rect::new(0, 0, 32, 16), "{f:?}"); - let p = half.pixel(16, 8); - assert!((p[1] - 0.5).abs() < 0.02 && (p[3] - 1.0).abs() < 1e-3, "{f:?} {p:?}"); - let dbl = resize_surface(&s, 2.0, 2.0, f); - assert_eq!(dbl.content_bounds(), Rect::new(0, 0, 128, 64), "{f:?}"); - // Mid-gray channel and the ramp's monotonicity are preserved. - let row: Vec = (8..120).map(|x| dbl.pixel(x, 30)[0]).collect(); - assert!(row.windows(2).all(|w| w[1] >= w[0] - 0.02), "{f:?}"); - } - } - - #[test] - fn downscale_averages_checkerboard() { - let mut s = Surface::new(PixelFormat::RGBA8); - for y in 0..64 { - for x in 0..64 { - let v = ((x + y) % 2) as f32; - s.write_pixel(x, y, &[v, v, v, 1.0]); - } - } - let o = resize_surface(&s, 0.25, 0.25, Resample::Bicubic); - let p = o.pixel(8, 8); - assert!((p[0] - 0.5).abs() < 0.05, "{p:?}"); - } - - #[test] - fn transparent_edges_do_not_darken() { - let mut s = Surface::new(PixelFormat::RGBA32F); - s.fill_rect(Rect::new(10, 10, 20, 20), &[1.0, 0.0, 0.0, 1.0]); - let o = resize_surface(&s, 1.7, 1.7, Resample::Lanczos); - let edge = o.pixel(17, 25); - assert!(edge[3] > 0.0 && (edge[0] - 1.0).abs() < 0.05 && edge[1].abs() < 0.05, "{edge:?}"); - } - - /// Image Size of a layer that fills the canvas: its edges stay opaque and keep their colour, - /// up or down, at every depth; a layer away from the edges keeps its soft border. - #[test] - fn canvas_edges_repeat_instead_of_fading() { - let canvas = Rect::new(0, 0, 30, 20); - let colour = [0.2, 0.6, 1.0, 1.0]; - let filters = [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos, Resample::PreserveDetails]; - for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { - let fmt = PixelFormat::new(ColorMode::Rgb, sample, true); - let mut s = Surface::new(fmt); - s.fill_rect(canvas, &colour); - for f in filters { - for k in [2.0, 1.37, 0.5] { - let o = resize_surface_in_canvas(&s, k, k, f, canvas); - let dst = scaled_rect(canvas, k, k); - assert_eq!(o.content_bounds(), dst, "{sample:?} {f:?} {k}"); - for (x, y) in [(0, 0), (dst.x1 - 1, 0), (0, dst.y1 - 1), (dst.x1 - 1, dst.y1 - 1), (dst.x1 / 2, 0), (0, dst.y1 / 2)] { - let p = o.pixel(x, y); - let off = p.iter().zip(colour).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(off < 0.01, "{sample:?} {f:?} {k} ({x},{y}): {p:?}"); - } - } - } - // Inside the canvas the default pixel is still what lies around a layer. - let mut inner = Surface::new(fmt); - inner.fill_rect(Rect::new(10, 5, 20, 15), &[1.0, 0.0, 0.0, 1.0]); - let r = Rect::new(0, 0, 60, 40); - let a = resize_surface_in_canvas(&inner, 1.7, 1.7, Resample::Lanczos, canvas); - assert_eq!(a.read_region(r), resize_surface(&inner, 1.7, 1.7, Resample::Lanczos).read_region(r), "{sample:?}"); - } - } - - /// Two tiles in opposite corners of a 100000 px square: the content bounds are huge but - /// almost nothing is stored. Shrinking it to one pixel must neither allocate a window - /// proportional to the scale (#713) nor read the empty middle. - #[test] - fn tiny_target_of_a_huge_sparse_source() { - let mut s = Surface::new(PixelFormat::RGBA32F); - s.fill_rect(Rect::new(0, 0, 256, 256), &[1.0, 1.0, 1.0, 1.0]); - s.fill_rect(Rect::new(99_840, 99_840, 100_096, 100_096), &[1.0, 1.0, 1.0, 1.0]); - let k = 1.0 / 100_096.0; - let o = resize_surface(&s, k, k, Resample::Bicubic); - assert_eq!(o.content_bounds(), Rect::new(0, 0, 1, 1)); - let p = o.pixel(0, 0); - // Two 256² tiles out of 100096²: nearly transparent, and white where it isn't. - assert!(p[3] >= 0.0 && p[3] < 1e-3 && p.iter().all(|v| v.is_finite()), "{p:?}"); - // Squashed to one row but kept wide: the whole height feeds one band of output rows, - // which must not hold a full-height intermediate. - let o = resize_surface(&s, 1000.0 / 100_096.0, k, Resample::Bicubic); - let b = o.content_bounds(); - assert!(b.height() == 1 && (1000..=1001).contains(&b.width()), "{b:?}"); - assert!(o.pixel(0, 0)[3] > 0.0 && o.pixel(500, 0)[3] == 0.0, "{:?} {:?}", o.pixel(0, 0), o.pixel(500, 0)); - } - - /// Tile rows with nothing stored are not read; they count as rows of the default pixel, - /// exactly as reading them would (and within rounding for a non-zero default). - #[test] - fn skipped_empty_rows_match_reading_them() { - for (fmt, def, tol) in [(PixelFormat::RGBA32F, vec![0.0, 0.0, 0.0, 0.0], 0.0), (PixelFormat::GRAY8, vec![1.0], 1e-6)] { - let mut sparse = Surface::with_default(fmt, &def); - sparse.fill_rect(Rect::new(0, 0, 300, 40), &vec![0.5; def.len()]); - sparse.fill_rect(Rect::new(100, 1500, 400, 1540), &vec![0.25; def.len()]); - // The same pixels with the empty rows stored as tiles of the default pixel. - let mut dense = sparse.clone(); - dense.fill_rect(Rect::new(0, 40, 400, 1500), &def); - assert!(dense.tile_count() > sparse.tile_count()); - for f in [Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { - for (kx, ky) in [(0.013, 0.013), (0.37, 0.37), (1.0, 0.01)] { - let (a, b) = (resize_surface(&sparse, kx, ky, f), resize_surface(&dense, kx, ky, f)); - let r = a.content_bounds().union(&b.content_bounds()); - let d = a.read_region(r).iter().zip(b.read_region(r)).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); - assert!(d <= tol, "{fmt:?} {f:?} {kx} {ky}: {d}"); - } - } - } - } - - /// Reading the source in row chunks gives exactly what one read of the whole window gives. - #[test] - fn chunked_reads_match_a_single_read() { - let mut s = ramp(SampleType::F32); - s.fill_rect(Rect::new(600, 700, 640, 720), &[0.3, 0.9, 0.1, 0.5]); - let mut mask = Surface::with_default(PixelFormat::GRAY8, &[1.0]); - mask.fill_rect(Rect::new(0, 0, 40, 30), &[0.0]); - mask.fill_rect(Rect::new(500, 900, 530, 940), &[0.25]); - for src in [&s, &mask] { - for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { - for k in [0.013, 0.37, 1.6] { - let whole = resize_with_budget(src, k, k, f, None, usize::MAX); - let chunked = resize_with_budget(src, k, k, f, None, 1); - let r = whole.content_bounds().union(&chunked.content_bounds()); - assert_eq!(whole.read_region(r), chunked.read_region(r), "{f:?} {k}"); - } - } - } - } - - #[test] - fn masks_keep_default_pixel() { - let fmt = PixelFormat::GRAY8; - let mut m = Surface::with_default(fmt, &[1.0]); - m.fill_rect(Rect::new(0, 0, 10, 10), &[0.0]); - let o = resize_surface(&m, 2.0, 2.0, Resample::Bilinear); - assert_eq!(o.default_pixel(), vec![1.0]); - assert!(o.pixel(5, 5)[0] < 0.01); - } - - #[test] - fn translate_and_crop() { - let s = ramp(SampleType::U8); - let t = translate_surface(&s, 5, -3); - assert_eq!(t.pixel(15, 7), s.pixel(10, 10)); - let c = crop_surface(&s, Rect::new(0, 0, 10, 10)); - assert_eq!(c.content_bounds(), Rect::new(0, 0, 10, 10)); - } - - /// #1114: nearest neighbor reduction must sample single source pixels, never average. - #[test] - fn nearest_neighbor_reduction_does_not_average() { - let mut s = Surface::new(PixelFormat::GRAY8); - for y in 0..8 { - for x in 0..8 { - if (x + y) % 2 == 0 { - s.fill_rect(Rect::new(x, y, x + 1, y + 1), &[1.0]); - } - } - } - let reduced = resize_surface(&s, 0.5, 0.5, Resample::Nearest); - for y in 0..4 { - for x in 0..4 { - let p = reduced.pixel(x, y)[0]; - assert!(p == 0.0 || p == 1.0, "nearest produced an averaged value {p} at ({x}, {y})"); - } - } - } -} +mod tests; diff --git a/crates/algo/src/resample/tests.rs b/crates/algo/src/resample/tests.rs new file mode 100644 index 0000000000..0ee08847e1 --- /dev/null +++ b/crates/algo/src/resample/tests.rs @@ -0,0 +1,402 @@ +use super::*; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; + +fn ramp(sample: SampleType) -> Surface { + let mut s = Surface::new(PixelFormat::new(ColorMode::Rgb, sample, true)); + let r = Rect::new(0, 0, 64, 32); + let v: Vec = (0..32).flat_map(|_| (0..64).flat_map(|x| [x as f32 / 63.0, 0.5, 1.0 - x as f32 / 63.0, 1.0])).collect(); + s.write_region(r, &v); + s +} + +#[test] +fn fixed_channel_rows_match_dynamic_resampling() { + fn check() { + for scale in [0.12, 0.5, 1.0, 2.3] { + for filter in [Kernel::Nearest, Kernel::Bilinear, keys(Resample::Bicubic, scale), keys(Resample::BicubicSharper, scale), Kernel::Lanczos] { + for clamp in [None, Some((-3, 31))] { + let ts = taps(-5, 41, scale, filter, clamp, (-3, 31)); + let row: Vec = (0..35 * N).map(|i| ((i * 73) % 251) as f32 / 250.0).collect(); + let def: Vec = (0..N).map(|i| i as f32 / 7.0).collect(); + let mut expected = vec![0.0; ts.len() * N]; + let mut actual = expected.clone(); + horizontal_dynamic(&row, &mut expected, &ts, -3, &def, N); + horizontal_fixed::(&row, &mut actual, &ts, -3, &def); + assert_eq!(actual, expected, "N={N} scale={scale} filter={filter:?} clamp={clamp:?}"); + } + } + } + } + check::<1>(); + check::<2>(); + check::<3>(); + check::<4>(); + check::<5>(); +} + +/// Photoshop 25.4's Image Size of a 16-bit greyscale pattern, in levels of 32768. Covers the +/// filters and their 2-D rules: Sharper reducing the width while the height grows is designed +/// for the height (not widened) and runs the width first; Bicubic Automatic splits a mixed +/// resize into Sharper and Smoother; Bicubic widens for the height's 0.6 while the width +/// grows; Smoother below ¼ averages down first; Bilinear below ½ averages whole pixels. On black +/// and white the overshoot is clipped between the passes, so their order shows. +#[test] +fn matches_photoshop_image_size() { + // (black and white pattern, filter, size, new size, Photoshop's levels) + type Probe = (bool, Resample, (i32, i32), (i32, i32), &'static [u16]); + let cases: [Probe; 9] = [ + ( + false, + Resample::BicubicSharper, + (12, 10), + (7, 16), + &[ + 3777, 13165, 22219, 16176, 10460, 19488, 29643, 11249, 12634, 21696, 13330, 10319, 18965, 29120, 21280, 11204, 20279, 16184, 8200, 17548, + 27703, 21824, 10182, 19259, 26967, 5238, 16528, 26683, 17366, 8895, 17922, 32768, 2402, 15190, 25345, 17493, 7834, 16903, 31316, 1748, 14172, + 24326, 24718, 6368, 15746, 30390, 0, 13107, 23169, 31215, 4790, 14456, 27639, 5080, 11597, 21880, 29649, 3864, 13507, 24136, 20600, 10264, + 20931, 28009, 0, 12446, 21385, 28735, 8754, 19642, 26154, 4049, 10895, 20832, 23523, 7620, 18485, 23204, 20126, 8787, 19818, 21447, 6173, + 17467, 20390, 31862, 6616, 18480, 24094, 6500, 16129, 19749, 27472, 5810, 17460, 28281, 7655, 15108, 18394, 25515, 4427, 16043, 28749, 7026, + 13691, 17871, 24992, 3904, 15520, 27200, 6075, 13168, + ], + ), + ( + false, + Resample::BicubicAutomatic, + (12, 10), + (7, 16), + &[ + 4201, 12337, 27677, 15894, 4816, 20021, 29631, 10348, 9882, 27304, 14492, 3766, 19574, 29046, 18743, 5944, 25299, 16116, 4147, 17829, 27787, + 19399, 4206, 21790, 23748, 7966, 15117, 26569, 15941, 2695, 19445, 28341, 10104, 13050, 25465, 16251, 2613, 18023, 26770, 8432, 11951, 24281, + 22800, 7141, 15351, 24790, 5953, 11004, 23130, 28322, 10921, 12440, 24940, 9659, 9368, 22012, 27611, 10006, 10488, 26419, 18657, 7071, 20807, + 25492, 6937, 9239, 26574, 22363, 5435, 19688, 24808, 9136, 8184, 25295, 19708, 3939, 18677, 25053, 18234, 7120, 24061, 17941, 2545, 17568, + 24869, 23748, 6081, 21401, 21862, 6018, 15056, 23602, 22177, 4859, 17816, 28450, 11911, 11653, 22253, 20263, 3595, 15790, 29734, 12969, 9683, + 21668, 19678, 3010, 15424, 28429, 11728, 9300, + ], + ), + ( + false, + Resample::Bicubic, + (12, 10), + (20, 6), + &[ + 11517, 10000, 8617, 9364, 12272, 16362, 21396, 25508, 25506, 19474, 10887, 5289, 5842, 10286, 14986, 18566, 21910, 25511, 28611, 29922, 24788, + 17949, 8497, 4593, 7309, 12594, 17150, 20945, 24892, 26196, 22979, 16123, 9691, 7483, 10061, 14653, 18841, 22420, 25514, 26824, 25702, 23396, + 17586, 11378, 8127, 8882, 12310, 17039, 22345, 25871, 24700, 18125, 9951, 5493, 6802, 11215, 15576, 19259, 22384, 23694, 26692, 26985, 22732, + 14270, 6889, 4950, 8348, 13583, 17529, 21570, 25966, 27275, 22817, 14643, 8068, 6911, 10694, 15930, 19443, 20705, 21499, 23413, 25601, 24336, + 18665, 11686, 7800, 8947, 12974, 18067, 22707, 25285, 23077, 16645, 9789, 6586, 8147, 12329, 15935, 17465, 17962, 20462, 24615, 25377, 20036, + 11458, 5421, 5415, 9528, 14134, 17782, 22482, 26926, 27479, 21881, 13308, 7533, 7734, 11549, 13992, + ], + ), + ( + false, + Resample::BicubicSmoother, + (40, 3), + (9, 3), + &[ + 11315, 18740, 17475, 15974, 16503, 16042, 18215, 14712, 16238, 18482, 14351, 14664, 18012, 20260, 13338, 15511, 18468, 18275, 16790, 19168, + 14745, 14998, 20341, 18150, 13819, 16493, 16432, + ], + ), + ( + false, + Resample::Bilinear, + (12, 10), + (5, 4), + &[11316, 14541, 17920, 10855, 24679, 19763, 9933, 23757, 6247, 20070, 25601, 5325, 19149, 17306, 15463, 20992, 16384, 14541, 23143, 10854], + ), + ( + false, + Resample::Nearest, + (12, 10), + (7, 16), + &[ + 1638, 12698, 23757, 3482, 9011, 20070, 31130, 1638, 12698, 23757, 3482, 9011, 20070, 31130, 31130, 10854, 21914, 1638, 7168, 18227, 29286, + 29286, 9011, 20070, 31130, 5325, 16384, 27443, 29286, 9011, 20070, 31130, 5325, 16384, 27443, 27443, 7168, 18227, 29286, 3482, 14541, 25600, + 25600, 5325, 16384, 27443, 1638, 12698, 23757, 25600, 5325, 16384, 27443, 1638, 12698, 23757, 23757, 3482, 14541, 25600, 31130, 10854, 21914, + 23757, 3482, 14541, 25600, 31130, 10854, 21914, 21914, 1638, 12698, 23757, 29286, 9011, 20070, 20070, 31130, 10854, 21914, 27443, 7168, 18227, + 20070, 31130, 10854, 21914, 27443, 7168, 18227, 18227, 29286, 9011, 20070, 25600, 5325, 16384, 16384, 27443, 7168, 18227, 23757, 3482, 14541, + 16384, 27443, 7168, 18227, 23757, 3482, 14541, + ], + ), + ( + true, + Resample::Bicubic, + (12, 10), + (20, 6), + &[ + 9960, 6313, 993, 0, 4525, 14807, 26472, 32768, 32768, 23510, 10514, 0, 0, 0, 10110, 22576, 31764, 32768, 32768, 32768, 26136, 18594, 6164, 0, + 0, 2565, 14499, 26983, 32768, 32768, 26358, 14709, 3969, 0, 0, 6602, 17734, 27952, 32768, 32768, 31590, 27808, 18307, 7429, 0, 0, 3712, 14435, + 26473, 32768, 31131, 20665, 7535, 0, 0, 2177, 13238, 25049, 32768, 32768, 32768, 32768, 26136, 14174, 2548, 0, 0, 6190, 18705, 30273, 32768, + 32768, 25517, 13238, 2177, 0, 0, 7250, 19418, 26624, 32768, 32768, 32768, 28203, 18522, 7200, 0, 0, 3285, 14794, 26862, 32768, 29913, 19013, + 6667, 0, 0, 4443, 14506, 20751, 17344, 23066, 31075, 32361, 23949, 10738, 0, 0, 0, 10110, 22657, 32768, 32768, 32768, 22658, 10192, 1004, 0, 0, + 0, + ], + ), + ( + true, + Resample::BicubicSharper, + (12, 10), + (7, 16), + &[ + 801, 3050, 32489, 16260, 0, 29819, 32767, 9161, 1781, 29578, 14350, 0, 32768, 32767, 21132, 0, 25409, 17862, 0, 26925, 32767, 22769, 0, 24839, + 27566, 0, 9397, 32768, 19813, 0, 27795, 32768, 0, 0, 32768, 20744, 0, 25797, 32768, 0, 0, 32768, 27555, 0, 11184, 32768, 0, 0, 32768, 32768, 0, + 0, 32768, 5110, 0, 30386, 32768, 0, 0, 32768, 20385, 0, 23267, 32768, 0, 0, 32768, 29130, 0, 19191, 32768, 2670, 0, 31290, 25387, 0, 21263, + 32767, 20198, 0, 21586, 24510, 0, 23714, 32768, 32768, 0, 14165, 27804, 1007, 14254, 32768, 30149, 0, 16077, 32112, 2886, 1884, 21043, 29607, + 0, 16384, 32768, 3566, 0, 12683, 29616, 0, 16384, 32768, 3196, 0, + ], + ), + ( + true, + Resample::BicubicSharper, + (12, 10), + (20, 6), + &[ + 9273, 5043, 0, 0, 2698, 14484, 28464, 32768, 32768, 24841, 10877, 0, 0, 0, 9684, 22346, 31492, 32768, 32768, 32768, 25960, 18430, 4978, 0, 0, + 977, 15047, 28475, 32768, 32768, 27939, 14157, 1813, 0, 0, 5296, 17828, 28439, 32768, 32768, 32768, 27849, 17576, 6209, 0, 0, 2169, 15076, + 28935, 32768, 32768, 22167, 7399, 0, 0, 211, 12977, 24638, 32768, 32768, 32768, 32768, 25960, 14244, 824, 0, 0, 5093, 18802, 31993, 32768, + 32768, 26467, 12977, 211, 0, 0, 6300, 19418, 26712, 32768, 32768, 32768, 27907, 17874, 5820, 0, 0, 1853, 16055, 29518, 32768, 32768, 20889, + 5880, 0, 0, 2879, 13798, 20185, 19010, 25565, 32768, 32302, 25431, 11478, 0, 0, 0, 9684, 23083, 32768, 32768, 32768, 23084, 10422, 1276, 0, 0, + 0, + ], + ), + ]; + for (binary, filter, (w, h), (wo, ho), want) in cases { + let mut s = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::U16, false)); + let canvas = Rect::new(0, 0, w, h); + let level = |x: i32, y: i32| match ((x * 37 + y * 101) % 17) as f32 { + k if binary => { + if k > 8.0 { + 32767.0 / 32768.0 + } else { + 0.0 + } + } + k => ((k / 16.0 * 0.9 + 0.05) * 32768.0).round() / 32768.0, + }; + let px: Vec = (0..h).flat_map(|y| (0..w).map(move |x| level(x, y))).collect(); + s.write_region(canvas, &px); + let o = resize_surface_in_canvas(&s, wo as f64 / w as f64, ho as f64 / h as f64, filter, canvas); + let got = o.read_region(Rect::new(0, 0, wo, ho)); + assert_eq!(got.len(), want.len(), "{filter:?}"); + let worst = got.iter().zip(want).map(|(g, &p)| (g * 32768.0 - p as f32).abs()).fold(0.0f32, f32::max); + assert!(worst <= 8.0, "{filter:?} {w}x{h} to {wo}x{ho}: {worst} levels of 32768 from Photoshop"); + } +} + +/// Photoshop's Bicubic doubles with the Keys cubic at a = −0.75 (Catmull-Rom is −0.5): an +/// impulse spreads as −9, −27, 67, 225 /256 and back, as Photoshop 25.4 gives it. +#[test] +fn bicubic_is_photoshops_keys_cubic() { + let mut s = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false)); + let mut row = [0.25; 30]; + row[10] = 0.75; + s.write_region(Rect::new(0, 0, 30, 1), &row); + let o = resize_surface(&s, 2.0, 1.0, Resample::Bicubic); + let got: Vec = o.read_region(Rect::new(17, 0, 25, 1)).iter().map(|v| (v - 0.25) * 512.0).collect(); + for (g, want) in got.iter().zip([-9.0, -27.0, 67.0, 225.0, 225.0, 67.0, -27.0, -9.0]) { + assert!((g - want).abs() < 1e-3, "{got:?}"); + } +} + +#[test] +fn identity_scale_is_lossless() { + for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { + let s = ramp(SampleType::U16); + let o = resize_surface(&s, 1.0, 1.0, f); + let r = Rect::new(0, 0, 64, 32); + let (a, b) = (s.read_region(r), o.read_region(r)); + let d = a.iter().zip(&b).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); + assert!(d < 1e-4, "{f:?} {d}"); + } +} + +#[test] +fn half_and_double_sizes() { + for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos, Resample::PreserveDetails] { + let s = ramp(SampleType::F32); + let half = resize_surface(&s, 0.5, 0.5, f); + assert_eq!(half.content_bounds(), Rect::new(0, 0, 32, 16), "{f:?}"); + let p = half.pixel(16, 8); + assert!((p[1] - 0.5).abs() < 0.02 && (p[3] - 1.0).abs() < 1e-3, "{f:?} {p:?}"); + let dbl = resize_surface(&s, 2.0, 2.0, f); + assert_eq!(dbl.content_bounds(), Rect::new(0, 0, 128, 64), "{f:?}"); + // Mid-gray channel and the ramp's monotonicity are preserved. + let row: Vec = (8..120).map(|x| dbl.pixel(x, 30)[0]).collect(); + assert!(row.windows(2).all(|w| w[1] >= w[0] - 0.02), "{f:?}"); + } +} + +#[test] +fn downscale_averages_checkerboard() { + let mut s = Surface::new(PixelFormat::RGBA8); + for y in 0..64 { + for x in 0..64 { + let v = ((x + y) % 2) as f32; + s.write_pixel(x, y, &[v, v, v, 1.0]); + } + } + let o = resize_surface(&s, 0.25, 0.25, Resample::Bicubic); + let p = o.pixel(8, 8); + assert!((p[0] - 0.5).abs() < 0.05, "{p:?}"); +} + +/// A reduction by a tiny scale covers millions of source samples per output sample; only the +/// ones in the content get a weight, the rest fold onto its ends or into `outside`. +#[test] +fn tiny_scales_do_not_get_a_weight_per_covered_sample() { + for kernel in [Kernel::Bilinear, Kernel::Keys { a: -0.5, stretch: 1.0 }] { + for edge in [None, Some((0, 99))] { + let t = taps(0, 3, 1e-6, kernel, edge, (0, 99)); + for t in &t { + assert!(t.weights.len() <= 100, "{}", t.weights.len()); + let total = t.weights.iter().sum::() + t.outside; + assert!((total - 1.0).abs() < 1e-3, "{total}"); + } + } + } +} + +#[test] +fn transparent_edges_do_not_darken() { + let mut s = Surface::new(PixelFormat::RGBA32F); + s.fill_rect(Rect::new(10, 10, 20, 20), &[1.0, 0.0, 0.0, 1.0]); + let o = resize_surface(&s, 1.7, 1.7, Resample::Lanczos); + let edge = o.pixel(17, 25); + assert!(edge[3] > 0.0 && (edge[0] - 1.0).abs() < 0.05 && edge[1].abs() < 0.05, "{edge:?}"); +} + +/// Image Size of a layer that fills the canvas: its edges stay opaque and keep their colour, +/// up or down, at every depth; a layer away from the edges keeps its soft border. +#[test] +fn canvas_edges_repeat_instead_of_fading() { + let canvas = Rect::new(0, 0, 30, 20); + let colour = [0.2, 0.6, 1.0, 1.0]; + let filters = [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos, Resample::PreserveDetails]; + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let fmt = PixelFormat::new(ColorMode::Rgb, sample, true); + let mut s = Surface::new(fmt); + s.fill_rect(canvas, &colour); + for f in filters { + for k in [2.0, 1.37, 0.5] { + let o = resize_surface_in_canvas(&s, k, k, f, canvas); + let dst = scaled_rect(canvas, k, k); + assert_eq!(o.content_bounds(), dst, "{sample:?} {f:?} {k}"); + for (x, y) in [(0, 0), (dst.x1 - 1, 0), (0, dst.y1 - 1), (dst.x1 - 1, dst.y1 - 1), (dst.x1 / 2, 0), (0, dst.y1 / 2)] { + let p = o.pixel(x, y); + let off = p.iter().zip(colour).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(off < 0.01, "{sample:?} {f:?} {k} ({x},{y}): {p:?}"); + } + } + } + // Inside the canvas the default pixel is still what lies around a layer. + let mut inner = Surface::new(fmt); + inner.fill_rect(Rect::new(10, 5, 20, 15), &[1.0, 0.0, 0.0, 1.0]); + let r = Rect::new(0, 0, 60, 40); + let a = resize_surface_in_canvas(&inner, 1.7, 1.7, Resample::Lanczos, canvas); + assert_eq!(a.read_region(r), resize_surface(&inner, 1.7, 1.7, Resample::Lanczos).read_region(r), "{sample:?}"); + } +} + +/// Two tiles in opposite corners of a 100000 px square: the content bounds are huge but +/// almost nothing is stored. Shrinking it to one pixel must neither allocate a window +/// proportional to the scale (#713) nor read the empty middle. +#[test] +fn tiny_target_of_a_huge_sparse_source() { + let mut s = Surface::new(PixelFormat::RGBA32F); + s.fill_rect(Rect::new(0, 0, 256, 256), &[1.0, 1.0, 1.0, 1.0]); + s.fill_rect(Rect::new(99_840, 99_840, 100_096, 100_096), &[1.0, 1.0, 1.0, 1.0]); + let k = 1.0 / 100_096.0; + let o = resize_surface(&s, k, k, Resample::Bicubic); + assert_eq!(o.content_bounds(), Rect::new(0, 0, 1, 1)); + let p = o.pixel(0, 0); + // Two 256² tiles out of 100096²: nearly transparent, and white where it isn't. + assert!(p[3] >= 0.0 && p[3] < 1e-3 && p.iter().all(|v| v.is_finite()), "{p:?}"); + // Squashed to one row but kept wide: the whole height feeds one band of output rows, + // which must not hold a full-height intermediate. + let o = resize_surface(&s, 1000.0 / 100_096.0, k, Resample::Bicubic); + let b = o.content_bounds(); + assert!(b.height() == 1 && (1000..=1001).contains(&b.width()), "{b:?}"); + assert!(o.pixel(0, 0)[3] > 0.0 && o.pixel(500, 0)[3] == 0.0, "{:?} {:?}", o.pixel(0, 0), o.pixel(500, 0)); +} + +/// Tile rows with nothing stored are not read; they count as rows of the default pixel, +/// exactly as reading them would (and within rounding for a non-zero default). +#[test] +fn skipped_empty_rows_match_reading_them() { + for (fmt, def, tol) in [(PixelFormat::RGBA32F, vec![0.0, 0.0, 0.0, 0.0], 0.0), (PixelFormat::GRAY8, vec![1.0], 1e-6)] { + let mut sparse = Surface::with_default(fmt, &def); + sparse.fill_rect(Rect::new(0, 0, 300, 40), &vec![0.5; def.len()]); + sparse.fill_rect(Rect::new(100, 1500, 400, 1540), &vec![0.25; def.len()]); + // The same pixels with the empty rows stored as tiles of the default pixel. + let mut dense = sparse.clone(); + dense.fill_rect(Rect::new(0, 40, 400, 1500), &def); + assert!(dense.tile_count() > sparse.tile_count()); + for f in [Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { + for (kx, ky) in [(0.013, 0.013), (0.37, 0.37), (1.0, 0.01)] { + let (a, b) = (resize_surface(&sparse, kx, ky, f), resize_surface(&dense, kx, ky, f)); + let r = a.content_bounds().union(&b.content_bounds()); + let d = a.read_region(r).iter().zip(b.read_region(r)).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max); + assert!(d <= tol, "{fmt:?} {f:?} {kx} {ky}: {d}"); + } + } + } +} + +/// Reading the source in row chunks gives exactly what one read of the whole window gives. +#[test] +fn chunked_reads_match_a_single_read() { + let mut s = ramp(SampleType::F32); + s.fill_rect(Rect::new(600, 700, 640, 720), &[0.3, 0.9, 0.1, 0.5]); + let mut mask = Surface::with_default(PixelFormat::GRAY8, &[1.0]); + mask.fill_rect(Rect::new(0, 0, 40, 30), &[0.0]); + mask.fill_rect(Rect::new(500, 900, 530, 940), &[0.25]); + for src in [&s, &mask] { + for f in [Resample::Nearest, Resample::Bilinear, Resample::Bicubic, Resample::Lanczos] { + for k in [0.013, 0.37, 1.6] { + let whole = resize_with_budget(src, k, k, f, None, usize::MAX); + let chunked = resize_with_budget(src, k, k, f, None, 1); + let r = whole.content_bounds().union(&chunked.content_bounds()); + assert_eq!(whole.read_region(r), chunked.read_region(r), "{f:?} {k}"); + } + } + } +} + +#[test] +fn masks_keep_default_pixel() { + let fmt = PixelFormat::GRAY8; + let mut m = Surface::with_default(fmt, &[1.0]); + m.fill_rect(Rect::new(0, 0, 10, 10), &[0.0]); + let o = resize_surface(&m, 2.0, 2.0, Resample::Bilinear); + assert_eq!(o.default_pixel(), vec![1.0]); + assert!(o.pixel(5, 5)[0] < 0.01); +} + +#[test] +fn translate_and_crop() { + let s = ramp(SampleType::U8); + let t = translate_surface(&s, 5, -3); + assert_eq!(t.pixel(15, 7), s.pixel(10, 10)); + let c = crop_surface(&s, Rect::new(0, 0, 10, 10)); + assert_eq!(c.content_bounds(), Rect::new(0, 0, 10, 10)); +} + +/// #1114: nearest neighbor reduction must sample single source pixels, never average. +#[test] +fn nearest_neighbor_reduction_does_not_average() { + let mut s = Surface::new(PixelFormat::GRAY8); + for y in 0..8 { + for x in 0..8 { + if (x + y) % 2 == 0 { + s.fill_rect(Rect::new(x, y, x + 1, y + 1), &[1.0]); + } + } + } + let reduced = resize_surface(&s, 0.5, 0.5, Resample::Nearest); + for y in 0..4 { + for x in 0..4 { + let p = reduced.pixel(x, y)[0]; + assert!(p == 0.0 || p == 1.0, "nearest produced an averaged value {p} at ({x}, {y})"); + } + } +} diff --git a/crates/algo/src/retouch.rs b/crates/algo/src/retouch.rs index f7c1c18557..78b588f787 100644 --- a/crates/algo/src/retouch.rs +++ b/crates/algo/src/retouch.rs @@ -124,12 +124,13 @@ pub fn local_blur(data: &[f32], w: usize, h: usize, ch: usize, alpha: Option = (-r..=r).map(|i| (-((i * i) as f32) / (2.0 * sigma * sigma).max(1e-6)).exp()).collect(); let ks: f32 = k.iter().sum(); let k: Vec = k.into_iter().map(|v| v / ks).collect(); - // Horizontal pass over the window rows extended vertically by r. + // Horizontal pass over the window rows extended vertically by r. Rows are independent, so + // both passes run them in parallel: a 2500 px Blur dab is ~10⁹ multiply-adds (#5426). let th = oh + 2 * r as usize; let stride = ow * ch; let mut tmp = vec![0.0f32; stride * th]; - let mut row = vec![0.0f32; (ow + 2 * r as usize) * ch]; - for ty in 0..th { + let scratch = || vec![0.0f32; (ow + 2 * r as usize) * ch]; + let horizontal = |row: &mut Vec, (ty, dst): (usize, &mut [f32])| { let y = (y0 as i32 + ty as i32 - r).clamp(0, h as i32 - 1) as usize; // Clamp and premultiply each source pixel once, rather than once for every kernel tap. for (rx, px) in row.chunks_exact_mut(ch).enumerate() { @@ -145,7 +146,6 @@ pub fn local_blur(data: &[f32], w: usize, h: usize, ch: usize, alpha: Option 1e-6 { out[o + c] / av } else { data[((y0 + oy) * w + x0 + ox) * ch + c] }; + *v = if av > 1e-6 { *v / av } else { data[((y0 + oy) * w + x0 + ox) * ch + c] }; } } } } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(stride).enumerate().for_each(vertical); + } + #[cfg(target_arch = "wasm32")] + { + out.chunks_mut(stride).enumerate().for_each(vertical); } out } @@ -193,34 +210,80 @@ pub fn local_sharpen( protect_detail: bool, ) -> Vec { let (x0, y0, x1, y1) = win; - let ow = x1 - x0; + let (ow, oh) = (x1 - x0, y1 - y0); + if ow == 0 || oh == 0 || ch == 0 { + return Vec::new(); + } let blur = local_blur(data, w, h, ch, alpha, win, 1.0); let mut out = vec![0.0f32; blur.len()]; - for y in y0..y1 { - for x in x0..x1 { - let o = ((y - y0) * ow + (x - x0)) * ch; + let stride = ow * ch; + + // Rows are independent, so process them in parallel with rayon (#5426). + let process_row = |(oy, dst): (usize, &mut [f32])| { + let y = y0 + oy; + let blur_row = &blur[oy * stride..(oy + 1) * stride]; + for ox in 0..ow { + let x = x0 + ox; + let o = ox * ch; let i = (y * w + x) * ch; + + // Precompute 3×3 neighbourhood offsets once per pixel if protecting detail. + let offsets: [usize; 8] = if protect_detail { + if x > 0 && x + 1 < w && y > 0 && y + 1 < h { + let w_ch = w * ch; + [i - w_ch - ch, i - w_ch, i - w_ch + ch, i - ch, i + ch, i + w_ch - ch, i + w_ch, i + w_ch + ch] + } else { + let get_n_idx = |dx: i32, dy: i32| -> usize { + let nx = (x as i32 + dx).clamp(0, w as i32 - 1) as usize; + let ny = (y as i32 + dy).clamp(0, h as i32 - 1) as usize; + (ny * w + nx) * ch + }; + [ + get_n_idx(-1, -1), + get_n_idx(0, -1), + get_n_idx(1, -1), + get_n_idx(-1, 0), + get_n_idx(1, 0), + get_n_idx(-1, 1), + get_n_idx(0, 1), + get_n_idx(1, 1), + ] + } + } else { + [0; 8] + }; + for c in 0..ch { let v = data[i + c]; if Some(c) == alpha { - out[o + c] = v; + dst[o + c] = v; continue; } - let mut s = v + amount * (v - blur[o + c]); + let mut s = v + amount * (v - blur_row[o + c]); if protect_detail { let (mut lo, mut hi) = (v, v); - for (dx, dy) in [(-1i32, -1i32), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] { - let (nx, ny) = ((x as i32 + dx).clamp(0, w as i32 - 1) as usize, (y as i32 + dy).clamp(0, h as i32 - 1) as usize); - let nv = data[(ny * w + nx) * ch + c]; + for &n_idx in &offsets { + let nv = data[n_idx + c]; lo = lo.min(nv); hi = hi.max(nv); } s = s.clamp(lo, hi); } - out[o + c] = s; + dst[o + c] = s; } } + }; + + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(stride).enumerate().for_each(process_row); } + #[cfg(target_arch = "wasm32")] + { + out.chunks_mut(stride).enumerate().for_each(process_row); + } + out } diff --git a/crates/algo/src/seam.rs b/crates/algo/src/seam.rs index 97619c7406..ebf7d5a705 100644 --- a/crates/algo/src/seam.rs +++ b/crates/algo/src/seam.rs @@ -9,16 +9,26 @@ //! //! A protect map (`0..=1`, e.g. an alpha channel or detected skin tones) adds a large energy so //! seams avoid protected pixels. Buffers are interleaved `w × h × ch` normalised floats of any -//! colour model and depth (alpha is carried like any other channel). Deterministic. +//! colour model and depth (alpha is carried like any other channel). Deterministic on every +//! platform: an energy that isn't a number (from NaN samples, or infinite ones next to each other +//! or at the border) is the highest energy, and NaN protection protects nothing, so no NaN (whose +//! sign and payload arithmetic does not keep consistently across builds) reaches the seam search. use photocraft_raster::{Cancelled, Interrupt}; /// Energy added per unit of protection: far above any gradient magnitude. const PROTECT_ENERGY: f32 = 1.0e4; -/// Keep full horizontal detail while reducing the height used to find vertical seams. At a -/// 24 MP working size this cuts the dynamic-programming work by about four times without -/// quantising the output pixels or changing the requested output dimensions. +/// The energy protection `p` (`0..=1`) adds. NaN protects nothing. +#[inline] +fn protection(p: f32) -> f32 { + if p.is_nan() { 0.0 } else { p.clamp(0.0, 1.0) * PROTECT_ENERGY } +} + +/// Keep full horizontal detail while reducing the height used to find vertical seams. On a +/// 6000×4000 image this cuts the dynamic-programming work eight times for a width change (4000 +/// rows to 500) and twelve times for a height change, without quantising the output pixels or +/// changing the requested output dimensions. const SEAM_SEARCH_MAX_HEIGHT: usize = 512; const PROXY_SHRINK_MIN_HEIGHT: usize = 2_048; const PROXY_SHRINK_MIN_SEAMS: usize = 64; @@ -33,7 +43,7 @@ pub fn energy(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { } let row = |(y, out): (usize, &mut [f32])| { for (x, v) in out.iter_mut().enumerate() { - *v = energy_at(img, w, w, h, ch, x, y); + *v = energy_at(img, w, w, h, ch, x, y, |xx, _| xx); } }; #[cfg(not(target_arch = "wasm32"))] @@ -46,20 +56,25 @@ pub fn energy(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { e } -/// [`energy`] of one pixel of a `cw`-wide image whose rows are `stride` pixels apart. Shared by -/// the full map and the per-seam updates so both give bit-identical values. +/// [`energy`] of pixel `x` of row `y` of a `cw`-wide, `h`-tall image whose pixel `xx` of row `yy` +/// is column `col(xx, yy)` of `img` (`w` pixels per row). Shared by the full map and the carver's +/// per-seam updates, so both give bit-identical values. An energy that isn't a number is the +/// highest energy (`+inf`). +#[allow(clippy::too_many_arguments)] #[inline] -fn energy_at(img: &[f32], stride: usize, cw: usize, h: usize, ch: usize, x: usize, y: usize) -> f32 { +fn energy_at(img: &[f32], w: usize, cw: usize, h: usize, ch: usize, x: usize, y: usize, col: impl Fn(usize, usize) -> usize) -> f32 { let (xl, xr) = (x.saturating_sub(1), (x + 1).min(cw - 1)); let (yu, yd) = (y.saturating_sub(1), (y + 1).min(h - 1)); - let at = |xx: usize, yy: usize, c: usize| img[(yy * stride + xx) * ch + c]; + let at = |xx: usize, yy: usize| (yy * w + col(xx, yy)) * ch; + let (centre, right, left, down, up) = (at(x, y), at(xr, y), at(xl, y), at(x, yd), at(x, yu)); let mut s = 0.0; for c in 0..ch { - let v = at(x, y, c); - s += (at(xr, y, c) - v).abs() + (v - at(xl, y, c)).abs(); - s += (at(x, yd, c) - v).abs() + (v - at(x, yu, c)).abs(); + let v = img[centre + c]; + s += (img[right + c] - v).abs() + (v - img[left + c]).abs(); + s += (img[down + c] - v).abs() + (v - img[up + c]).abs(); } - s * 0.5 + let e = s * 0.5; + if e.is_nan() { f32::INFINITY } else { e } } /// The lowest-energy vertical seam: one x per row. @@ -67,57 +82,178 @@ pub fn find_seam(w: usize, h: usize, energy: &[f32]) -> Vec { if w == 0 || h == 0 || energy.len() < w * h { return vec![0; h]; } - Dp::default().seam(w, w, h, energy) + let off = vec![0; h]; + let rows = Rows { stride: w, cw: w, off: &off }; + let mut dp = Dp::default(); + dp.full(rows, energy); + dp.seam(rows) } -/// Scratch buffers for the seam search, reused across seams. +/// Where the `cw` pixels in use of each of a carver's rows are, in buffers whose rows are +/// `stride` apart: row y's are `off[y]..off[y] + cw` of its `stride` slots. +#[derive(Clone, Copy)] +struct Rows<'o> { + stride: usize, + cw: usize, + off: &'o [usize], +} + +impl Rows<'_> { + fn h(&self) -> usize { + self.off.len() + } + + /// Row y's pixels in use, as a range of its buffers. + #[inline] + fn of(&self, y: usize) -> std::ops::Range { + let start = y * self.stride + self.off[y]; + start..start + self.cw + } +} + +/// The seam search's dynamic programme over an energy map laid out as [`Rows`] (the carver's own +/// layout, so a removal shifts it like the other buffers): for every pixel, the cost of the +/// cheapest 8-connected path down to it from the top row. [`Carver`] keeps it across seams and +/// recomputes only what each removal changed ([`Dp::update`]). #[derive(Default)] struct Dp { + /// Cost of the cheapest path from the top row to each pixel. cost: Vec, - next: Vec, - /// Per pixel, where the cheapest path came from: 0 = up-left, 1 = up, 2 = up-right. - from: Vec, +} + +/// Pixel `x` of a `cw`-wide row with energy `e`, given the path costs of the row above (`up`): +/// its path cost and where that path came from (0 = up-left, 1 = up, 2 = up-right). Ties go to +/// the pixel straight up, then up-left, then up-right. The cost doesn't depend on the tie-break +/// and the direction doesn't depend on `e`, so the seam can be traced back from the costs alone. +/// [`row_costs`] uses it at the ends of a row and repeats its selection in between, and +/// [`Dp::seam`] traces seams with it, so every way of filling the map gives the same bits (costs +/// are never NaN: see [`energy_at`] and [`protection`]). +#[inline] +fn cell(up: &[f32], cw: usize, x: usize, e: f32) -> (f32, u8) { + let (b, d) = if x == 0 { + if cw > 1 && up[1] < up[0] { (up[1], 2) } else { (up[0], 1) } + } else if x + 1 == cw { + if up[x - 1] < up[x] { (up[x - 1], 0) } else { (up[x], 1) } + } else { + let (b, d) = if up[x - 1] < up[x] { (up[x - 1], 0) } else { (up[x], 1) }; + if up[x + 1] < b { (up[x + 1], 2) } else { (b, d) } + }; + (b + e, d) +} + +/// Path costs of pixels `lo..lo + out.len()` of a `cw`-wide row with energies `e` (the whole +/// row's), from the path costs of the row above (`up`), into `out`. [`cell`] at the ends of the +/// row; in between, the same selection without edge tests or directions, so it vectorises. +#[inline] +fn row_costs(up: &[f32], e: &[f32], cw: usize, lo: usize, out: &mut [f32]) { + let Some(last) = (lo + out.len()).checked_sub(1) else { return }; + let (mut a, mut b) = (lo, last); + if a == 0 { + out[0] = cell(up, cw, 0, e[0]).0; + a = 1; + } + if b + 1 == cw && b >= a { + out[b - lo] = cell(up, cw, b, e[b]).0; + b -= 1; + } + if a > b { + return; + } + // Every pixel in a..=b has all three parents: up-left, up and up-right. + let parents = up[a - 1..b].iter().zip(&up[a..=b]).zip(&up[a + 1..=b + 1]); + for ((n, &ev), ((&l, &u), &r)) in out[a - lo..=b - lo].iter_mut().zip(&e[a..=b]).zip(parents) { + let m = if l < u { l } else { u }; + let m = if r < m { r } else { m }; + *n = m + ev; + } } impl Dp { - /// The lowest-energy seam of a `cw`-wide, `h`-tall energy map with rows `stride` apart. - /// Ties go to the pixel straight up, then up-left, then up-right; at the bottom, to the - /// leftmost end. - fn seam(&mut self, stride: usize, cw: usize, h: usize, energy: &[f32]) -> Vec { - self.cost.clear(); - self.cost.extend_from_slice(&energy[..cw]); - self.next.resize(cw, 0.0); - self.from.resize(cw * h, 0); - for y in 1..h { - let (cost, next) = (&self.cost[..cw], &mut self.next[..cw]); - let e = &energy[y * stride..y * stride + cw]; - let from = &mut self.from[y * cw..(y + 1) * cw]; - if cw == 1 { - (next[0], from[0]) = (cost[0] + e[0], 1); - } else { - let (b, d) = if cost[1] < cost[0] { (cost[1], 2) } else { (cost[0], 1) }; - (next[0], from[0]) = (b + e[0], d); - // The interior has all three parents: no edge tests in the hot loop. - let inner = next[1..cw - 1].iter_mut().zip(&mut from[1..cw - 1]).zip(&e[1..cw - 1]).zip(cost.windows(3)); - for (((n, f), &ev), c) in inner { - let (b, d) = if c[0] < c[1] { (c[0], 0) } else { (c[1], 1) }; - let (b, d) = if c[2] < b { (c[2], 2) } else { (b, d) }; - (*n, *f) = (b + ev, d); - } - let (b, d) = if cost[cw - 2] < cost[cw - 1] { (cost[cw - 2], 0) } else { (cost[cw - 1], 1) }; - (next[cw - 1], from[cw - 1]) = (b + e[cw - 1], d); - } - std::mem::swap(&mut self.cost, &mut self.next); + /// Fill the map for the pixels in use of every row of `energy`. + fn full(&mut self, rows: Rows, energy: &[f32]) { + let (cw, h) = (rows.cw, rows.h()); + self.cost.resize(rows.stride * h, 0.0); + if cw == 0 || h == 0 { + return; } - let cost = &self.cost[..cw]; - let mut x = (0..cw).min_by(|a, b| cost[*a].total_cmp(&cost[*b])).unwrap_or(0); + let first = rows.of(0); + self.cost[first.clone()].copy_from_slice(&energy[first]); + for y in 1..h { + let (above, rest) = self.cost.split_at_mut(y * rows.stride); + let (r, base) = (rows.of(y), y * rows.stride); + row_costs(&above[rows.of(y - 1)], &energy[r.clone()], cw, 0, &mut rest[r.start - base..r.end - base]); + } + } + + /// Bring the map up to date after `seam` (one x per row, counted before the removal) was + /// removed from every row of the map and of `energy`, leaving `rows.cw` pixels per row. + /// + /// A pixel's path cost depends only on its energy and on the costs of its parents in the row + /// above. Where a pixel and all its parents are on the same side of the seam in their rows, + /// the removal moved them together; that leaves, in row y, only the pixels from one left of + /// min(seam[y-1], seam[y]) to max(seam[y-1], seam[y]) with new parents. The energy changed + /// only where [`Carver::update_energy`] recomputed it: from one left of the leftmost of + /// `seam[y-1..=y+1]` to the rightmost, which covers those pixels too. Every other pixel needs + /// recomputing only if a parent's cost changed, so the work follows the changed costs down + /// the map and narrows wherever they settle, instead of covering the whole map. Recomputed + /// pixels go through [`row_costs`], like a [`Dp::full`] pass's, so the map matches one over + /// the same energy bit for bit. + fn update(&mut self, rows: Rows, energy: &[f32], seam: &[usize]) { + let (cw, h) = (rows.cw, rows.h()); + if cw == 0 || h == 0 || seam.len() < h { + return; + } + // Pixels of the row above whose cost changed: `lo..=hi`, or none when `lo > hi`. + let (mut changed_lo, mut changed_hi) = (usize::MAX, 0); + let mut fresh = Vec::new(); + for y in 0..h { + let near = &seam[y.saturating_sub(1)..(y + 2).min(h)]; + let mut lo = near.iter().min().map_or(0, |&s| s.saturating_sub(1)); + let mut hi = near.iter().max().map_or(0, |&s| s.min(cw - 1)); + if changed_lo <= changed_hi { + lo = lo.min(changed_lo.saturating_sub(1)); + hi = hi.max((changed_hi + 1).min(cw - 1)); + } + let (above, rest) = self.cost.split_at_mut(y * rows.stride); + let (r, base) = (rows.of(y), y * rows.stride); + let e = &energy[r.clone()]; + if fresh.len() < hi + 1 - lo { + fresh.resize(hi + 1 - lo, 0.0); + } + let fresh = &mut fresh[..hi + 1 - lo]; + match y.checked_sub(1) { + Some(u) => row_costs(&above[rows.of(u)], e, cw, lo, fresh), + None => fresh.copy_from_slice(&e[lo..=hi]), + } + // Store only what changed, and remember where that was for the row below. + let row = &mut rest[r.start - base + lo..r.start - base + hi + 1]; + let differs = |(a, b): (&f32, &f32)| a.to_bits() != b.to_bits(); + (changed_lo, changed_hi) = (usize::MAX, 0); + if let Some(first) = fresh.iter().zip(row.iter()).position(differs) { + let last = fresh.iter().zip(row.iter()).rposition(differs).unwrap_or(first); + row[first..=last].copy_from_slice(&fresh[first..=last]); + (changed_lo, changed_hi) = (lo + first, lo + last); + } + } + } + + /// The lowest-energy seam of the map: one x per row. At the bottom, ties go to the leftmost + /// end; going up, [`cell`] tells where each pixel's path came from. + fn seam(&self, rows: Rows) -> Vec { + let (cw, h) = (rows.cw, rows.h()); let mut seam = vec![0usize; h]; + if cw == 0 || h == 0 { + return seam; + } + let last = &self.cost[rows.of(h - 1)]; + let mut x = (0..cw).min_by(|a, b| last[*a].total_cmp(&last[*b])).unwrap_or(0); for y in (0..h).rev() { seam[y] = x; if y > 0 { - x = match self.from[y * cw + x] { - 0 => x - 1, - 2 => x + 1, + // `cell` never points past either end of the row; saturate rather than trust it. + x = match cell(&self.cost[rows.of(y - 1)], cw, x, 0.0).1 { + 0 => x.saturating_sub(1), + 2 => (x + 1).min(cw - 1), _ => x, }; } @@ -126,121 +262,223 @@ impl Dp { } } -/// Shrinks an image one seam at a time in place. Rows keep their original stride, so removing a -/// seam shifts only the rest of each row (in parallel), and the energy map is updated only next -/// to the removed seam instead of being recomputed: the gradient of a pixel changes only when a -/// neighbour does, which happens within a pixel of the seam in its own row and the rows around it. -struct Carver { - stride: usize, - cw: usize, +/// Shrinks an image one seam at a time without moving its pixels: the carver keeps which source +/// column each remaining pixel is (`idx`) and reads pixels through it. A removal shifts, in each +/// row, whichever side of the seam is shorter in the three per-pixel buffers (in parallel over +/// rows), and nothing is recomputed in full: the gradient of a pixel changes only when a +/// neighbour does, which happens within a pixel of the seam in its own row and the rows around +/// it, and the seam search's path costs are updated from there down ([`Dp::update`]). +struct Carver<'a> { + /// The image, `w × h × ch`, and the protection, one value per pixel: as given, never moved. + src: &'a [f32], + prot: Option<&'a [f32]>, + w: usize, h: usize, ch: usize, - img: Vec, - prot: Option>, + /// Pixels left per row. + cw: usize, + /// Where each row's first remaining pixel is in the per-pixel buffers (see [`Rows`]). + off: Vec, + /// Source column of each remaining pixel (rows `w` apart; layer areas are at most i32::MAX + /// wide, so `u32` holds any column). + idx: Vec, /// Energy plus protection, as the seam search sees it. e: Vec, - /// Original column of each pixel, when the caller needs to know which ones were removed. - idx: Option>, + /// The seam search over `e`, kept up to date. dp: Dp, } -impl Carver { - fn new(w: usize, h: usize, ch: usize, img: Vec, prot: Option>, track: bool) -> Self { - let mut e = energy(w, h, ch, &img); - if let Some(p) = &prot { - for (e, p) in e.iter_mut().zip(p) { - *e += p.clamp(0.0, 1.0) * PROTECT_ENERGY; +impl<'a> Carver<'a> { + fn new(w: usize, h: usize, ch: usize, src: &'a [f32], prot: Option<&'a [f32]>) -> Self { + let mut e = energy(w, h, ch, src); + if let Some(p) = prot { + for (e, &p) in e.iter_mut().zip(p) { + *e += protection(p); } } - let idx = track.then(|| (0..h).flat_map(|_| 0..w).collect()); - Carver { stride: w, cw: w, h, ch, img, prot, e, idx, dp: Dp::default() } + let mut idx = Vec::with_capacity(w * h); + for _ in 0..h { + idx.extend((0..w).map(|x| u32::try_from(x).unwrap_or(u32::MAX))); + } + let off = vec![0; h]; + let mut dp = Dp::default(); + dp.full(Rows { stride: w, cw: w, off: &off }, &e); + Carver { src, prot, w, h, ch, cw: w, off, idx, e, dp } } - /// Remove the lowest-energy seam. Returns, per row, the removed column (the original column - /// when tracking). + /// Remove the lowest-energy seam. Returns, per row, the source column removed. fn remove_seam(&mut self) -> Vec { - let (stride, cw, h, ch) = (self.stride, self.cw, self.h, self.ch); - let seam = self.dp.seam(stride, cw, h, &self.e); - let removed = match &self.idx { - Some(idx) => seam.iter().enumerate().map(|(y, &x)| idx[y * stride + x]).collect(), - None => seam.clone(), - }; - shift_rows(&mut self.img, stride * ch, &seam, ch, cw); - shift_rows(&mut self.e, stride, &seam, 1, cw); - if let Some(p) = &mut self.prot { - shift_rows(p, stride, &seam, 1, cw); - } - if let Some(idx) = &mut self.idx { - shift_rows(idx, stride, &seam, 1, cw); + if self.cw == 0 { + return Vec::new(); } + let rows = Rows { stride: self.w, cw: self.cw, off: &self.off }; + let seam = self.dp.seam(rows); + let removed = seam.iter().enumerate().map(|(y, &x)| self.idx[rows.of(y).start + x] as usize).collect(); + self.shift(&seam); self.cw -= 1; self.update_energy(&seam); + self.dp.update(Rows { stride: self.w, cw: self.cw, off: &self.off }, &self.e, &seam); removed } + /// Remove `seam[y]` from each row of the per-pixel buffers. + fn shift(&mut self, seam: &[usize]) { + let (w, cw) = (self.w, self.cw); + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + self.e + .par_chunks_mut(w) + .zip(self.idx.par_chunks_mut(w)) + .zip(self.dp.cost.par_chunks_mut(w)) + .zip(self.off.par_iter_mut()) + .zip(seam.par_iter()) + .for_each(|((((e, idx), cost), off), &x)| shift_row(e, idx, cost, off, x, cw)); + } + #[cfg(target_arch = "wasm32")] + self.e + .chunks_mut(w) + .zip(self.idx.chunks_mut(w)) + .zip(self.dp.cost.chunks_mut(w)) + .zip(self.off.iter_mut()) + .zip(seam) + .for_each(|((((e, idx), cost), off), &x)| shift_row(e, idx, cost, off, x, cw)); + } + /// Recompute the energy of the pixels whose neighbours changed when `seam` was removed: in /// row y, from one left of the leftmost of the seam's x in rows y-1..=y+1 to the rightmost. fn update_energy(&mut self, seam: &[usize]) { - let (stride, cw, h, ch) = (self.stride, self.cw, self.h, self.ch); + let (w, cw, h, ch) = (self.w, self.cw, self.h, self.ch); if cw == 0 { return; } - let (img, prot) = (&self.img, self.prot.as_deref()); + let (src, prot, off, idx) = (self.src, self.prot, &self.off, &self.idx); + let col = |xx: usize, yy: usize| idx[yy * w + off[yy] + xx] as usize; let row = |(y, e): (usize, &mut [f32])| { let near = &seam[y.saturating_sub(1)..(y + 2).min(h)]; let lo = near.iter().min().map_or(0, |&s| s.saturating_sub(1)); let hi = near.iter().max().map_or(0, |&s| s.min(cw - 1)); for x in lo..=hi { - let mut v = energy_at(img, stride, cw, h, ch, x, y); + let mut v = energy_at(src, w, cw, h, ch, x, y, col); if let Some(p) = prot { - v += p[y * stride + x].clamp(0.0, 1.0) * PROTECT_ENERGY; + v += protection(p[y * w + col(x, y)]); } - e[x] = v; + e[off[y] + x] = v; + } + }; + // A few pixels per row (seams move at most one column per row), but each waits on reads + // from three rows of the source: rows in parallel overlap the waits. + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + self.e.par_chunks_mut(w).enumerate().for_each(row); + } + #[cfg(target_arch = "wasm32")] + self.e.chunks_mut(w).enumerate().for_each(row); + } + + /// The carved pixels, `cw × h × ch`, rows packed. + fn into_pixels(self) -> Vec { + let (w, cw, ch) = (self.w, self.cw, self.ch); + let mut out = vec![0.0f32; cw * self.h * ch]; + if cw == 0 { + return out; + } + let (src, off, idx) = (self.src, &self.off, &self.idx); + let row = |(y, out): (usize, &mut [f32])| { + let cols = &idx[y * w + off[y]..y * w + off[y] + cw]; + for (px, &x) in out.chunks_exact_mut(ch).zip(cols) { + let s = (y * w + x as usize) * ch; + px.copy_from_slice(&src[s..s + ch]); } }; #[cfg(not(target_arch = "wasm32"))] { use rayon::prelude::*; - self.e.par_chunks_mut(stride).enumerate().for_each(row); + out.par_chunks_mut(cw * ch).enumerate().for_each(row); } #[cfg(target_arch = "wasm32")] - self.e.chunks_mut(stride).enumerate().for_each(row); - } - - /// The carved pixels, `cw × h × ch`, rows packed. - fn into_pixels(self) -> Vec { - let (row, used) = (self.stride * self.ch, self.cw * self.ch); - if row == used { - return self.img; - } - self.img.chunks(row).flat_map(|r| &r[..used]).copied().collect() + out.chunks_mut(cw * ch).enumerate().for_each(row); + out } } -/// Remove pixel `seam[y]` (of `n` values) from each `row_len`-long row whose first `cw` pixels -/// are in use, by shifting the rest of the row left. -fn shift_rows(buf: &mut [T], row_len: usize, seam: &[usize], n: usize, cw: usize) { - let shift = |(r, &x): (&mut [T], &usize)| r.copy_within((x + 1) * n..cw * n, x * n); - #[cfg(not(target_arch = "wasm32"))] - { - use rayon::prelude::*; - buf.par_chunks_mut(row_len).zip(seam.par_iter()).for_each(shift); +/// Remove pixel `x` of the `cw` in use, from `off` on, of one row of a carver's per-pixel +/// buffers, moving whichever side of it holds fewer pixels (the row then starts one later when +/// that is the left side). +fn shift_row(e: &mut [f32], idx: &mut [u32], cost: &mut [f32], off: &mut usize, x: usize, cw: usize) { + let o = *off; + if x < cw - 1 - x { + e.copy_within(o..o + x, o + 1); + idx.copy_within(o..o + x, o + 1); + cost.copy_within(o..o + x, o + 1); + *off = o + 1; + } else { + e.copy_within(o + x + 1..o + cw, o + x); + idx.copy_within(o + x + 1..o + cw, o + x); + cost.copy_within(o + x + 1..o + cw, o + x); } - #[cfg(target_arch = "wasm32")] - buf.chunks_mut(row_len).zip(seam).for_each(shift); } fn transpose(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { let mut out = vec![0.0f32; img.len()]; - for y in 0..h { - for x in 0..w { - let (s, d) = ((y * w + x) * ch, (x * h + y) * ch); - out[d..d + ch].copy_from_slice(&img[s..s + ch]); - } + if w == 0 || h == 0 || ch == 0 { + return out; } + // Output rows (source columns) in blocks, so each source cache line serves several of them. + const BLOCK: usize = 16; + let block = |(b, out): (usize, &mut [f32])| { + let x0 = b * BLOCK; + let n = out.len() / (h * ch); + for y in 0..h { + for i in 0..n { + let (s, d) = ((y * w + x0 + i) * ch, (i * h + y) * ch); + out[d..d + ch].copy_from_slice(&img[s..s + ch]); + } + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(BLOCK * h * ch).enumerate().for_each(block); + } + #[cfg(target_arch = "wasm32")] + out.chunks_mut(BLOCK * h * ch).enumerate().for_each(block); out } +/// Each `step` rows of `src` (`h` rows of `row_len` values) averaged into one, the last group +/// possibly shorter: `h.div_ceil(step)` rows. +fn average_rows(src: &[f32], row_len: usize, h: usize, step: usize) -> Vec { + let mut out = vec![0.0; h.div_ceil(step) * row_len]; + if row_len == 0 { + return out; + } + let group = |(py, out_row): (usize, &mut [f32])| { + let y0 = py * step; + let y1 = (y0 + step).min(h); + let divisor = (y1 - y0) as f32; + for y in y0..y1 { + for (out, &sample) in out_row.iter_mut().zip(&src[y * row_len..(y + 1) * row_len]) { + *out += sample / divisor; + } + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_exact_mut(row_len).enumerate().for_each(group); + } + #[cfg(target_arch = "wasm32")] + out.chunks_exact_mut(row_len).enumerate().for_each(group); + out +} + +/// No pixels or channels to work with, or `img` holds fewer than `w × h × ch` values. +fn unusable(w: usize, h: usize, ch: usize, img: &[f32]) -> bool { + w == 0 || h == 0 || ch == 0 || w.checked_mul(h).and_then(|n| n.checked_mul(ch)).is_none_or(|n| img.len() < n) +} + /// Change the width of `img` to `new_w` by removing or duplicating vertical seams. pub fn carve_width(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize) -> Vec { // Never cancelled; the fallback is unreachable. @@ -250,15 +488,23 @@ pub fn carve_width(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<& /// [`carve_width`] that checks `ctl` before every seam and reports progress per seam. pub fn carve_width_with(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize, ctl: &Interrupt) -> Result, Cancelled> { let new_w = new_w.max(1); - if new_w == w || w == 0 || h == 0 { + // Nothing to carve: no pixels or channels, or fewer values than the size says. + if unusable(w, h, ch, img) { return Ok(img.to_vec()); } + // Values past the size are not part of the image. + let img = img.get(..w * h * ch).unwrap_or(img); + if new_w == w { + return Ok(img.to_vec()); + } + // A protection map that doesn't cover the image protects nothing. + let protect = protect.filter(|p| p.len() >= w * h); let seams = new_w.abs_diff(w).max(1) as f32; if new_w < w { if h >= PROXY_SHRINK_MIN_HEIGHT && w - new_w >= PROXY_SHRINK_MIN_SEAMS { return carve_width_proxy_height(w, h, ch, img, protect, new_w, ctl); } - let mut carver = Carver::new(w, h, ch, img.to_vec(), protect.map(<[f32]>::to_vec), false); + let mut carver = Carver::new(w, h, ch, img, protect); while carver.cw > new_w { ctl.check()?; ctl.progress((w - carver.cw) as f32 / seams); @@ -272,7 +518,7 @@ pub fn carve_width_with(w: usize, h: usize, ch: usize, img: &[f32], protect: Opt while cw < new_w { let k = (new_w - cw).min((cw / 2).max(1)); // Find k seams on a shrinking copy, mapping each back to original columns. - let mut carver = Carver::new(cw, h, ch, cur.clone(), prot.clone(), true); + let mut carver = Carver::new(cw, h, ch, &cur, prot.as_deref()); let mut dup: Vec> = vec![Vec::with_capacity(k); h]; for _ in 0..k { // A 1 px wide image's only seam is its one column: take it, so it is duplicated. @@ -329,37 +575,11 @@ fn carve_width_proxy_height(w: usize, h: usize, ch: usize, img: &[f32], protect: let step = h.div_ceil(SEAM_SEARCH_MAX_HEIGHT); let proxy_h = h.div_ceil(step); let row_len = w * ch; - let proxy_row_len = proxy_h * row_len; - let mut proxy = vec![0.0; proxy_row_len]; - for (py, out_row) in proxy.chunks_exact_mut(row_len).enumerate() { - let y0 = py * step; - let y1 = (y0 + step).min(h); - let divisor = (y1 - y0) as f32; - for y in y0..y1 { - let in_row = &img[y * row_len..(y + 1) * row_len]; - for (out, &sample) in out_row.iter_mut().zip(in_row) { - *out += sample / divisor; - } - } - } - let proxy_protect = protect.map(|mask| { - let mut reduced = vec![0.0; proxy_h * w]; - for (py, out_row) in reduced.chunks_exact_mut(w).enumerate() { - let y0 = py * step; - let y1 = (y0 + step).min(h); - let divisor = (y1 - y0) as f32; - for y in y0..y1 { - let in_row = &mask[y * w..(y + 1) * w]; - for (out, &sample) in out_row.iter_mut().zip(in_row) { - *out += sample / divisor; - } - } - } - reduced - }); + let proxy = average_rows(img, row_len, h, step); + let proxy_protect = protect.map(|mask| average_rows(mask, w, h, step)); let seam_count = w - new_w; - let mut carver = Carver::new(w, proxy_h, ch, proxy, proxy_protect, true); + let mut carver = Carver::new(w, proxy_h, ch, &proxy, proxy_protect.as_deref()); let mut removed = Vec::with_capacity(seam_count); while carver.cw > new_w { ctl.check()?; @@ -373,31 +593,44 @@ fn carve_width_proxy_height(w: usize, h: usize, ch: usize, img: &[f32], protect: columns.sort_unstable(); removed_by_row.push(columns); } - let mut out = Vec::with_capacity(new_w * h * ch); - let mut columns = vec![0; seam_count]; - for y in 0..h { + // Each full-resolution row removes the columns interpolated between its two proxy rows'. + let rebuild = |columns: &mut Vec, (y, out): (usize, &mut [f32])| -> Result<(), Cancelled> { ctl.check()?; let proxy_y = (y / step).min(proxy_h - 1); let next_proxy_y = (proxy_y + 1).min(proxy_h - 1); let blend = (y % step) as f32 / step as f32; let lower = &removed_by_row[proxy_y]; let upper = &removed_by_row[next_proxy_y]; + columns.clear(); let mut min_x = 0; - for (i, x) in columns.iter_mut().enumerate() { + for i in 0..seam_count { let interpolated = lower[i] as f32 + (upper[i] as f32 - lower[i] as f32) * blend; let max_x = w - (seam_count - i); - *x = (interpolated.round() as usize).clamp(min_x, max_x); + let x = (interpolated.round() as usize).clamp(min_x, max_x); + columns.push(x); min_x = x.saturating_add(1); } - let mut next = 0; + // The columns rise strictly within the row: copy the runs between them. let row = &img[y * row_len..(y + 1) * row_len]; - for (x, pixel) in row.chunks_exact(ch).enumerate() { - if columns.get(next) == Some(&x) { - next += 1; - } else { - out.extend_from_slice(pixel); - } + let (mut from, mut at) = (0, 0); + for &x in columns.iter() { + let run = (x - from) * ch; + out[at..at + run].copy_from_slice(&row[from * ch..x * ch]); + (from, at) = (x + 1, at + run); } + out[at..].copy_from_slice(&row[from * ch..]); + Ok(()) + }; + let mut out = vec![0.0f32; new_w * h * ch]; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_exact_mut(new_w * ch).enumerate().try_for_each_init(|| Vec::with_capacity(seam_count), rebuild)?; + } + #[cfg(target_arch = "wasm32")] + { + let mut columns = Vec::with_capacity(seam_count); + out.chunks_exact_mut(new_w * ch).enumerate().try_for_each(|r| rebuild(&mut columns, r))?; } ctl.progress(1.0); Ok(out) @@ -423,6 +656,11 @@ pub fn carve_with( ctl: &Interrupt, ) -> Result, Cancelled> { let (new_w, new_h) = (new_w.max(1), new_h.max(1)); + if unusable(w, h, ch, img) { + return Ok(img.to_vec()); + } + let img = img.get(..w * h * ch).unwrap_or(img); + let protect = protect.filter(|p| p.len() >= w * h); // Split progress between the passes by their seam counts. let (sw, sh) = (new_w.abs_diff(w) as f32, new_h.abs_diff(h) as f32); let split = if sw + sh > 0.0 { sw / (sw + sh) } else { 1.0 }; @@ -432,21 +670,40 @@ pub fn carve_with( if new_h == h { return Ok(wide); } - // Protection follows the width change by plain resampling (it only steers seams). - let prot_t = protect.map(|p| transpose(new_w, h, 1, &resize_bilinear(w, h, 1, p, new_w, h))); + let prot_t = protect.map(|p| transpose(new_w, h, 1, &resample_protection(w, h, p, new_w))); let t = transpose(new_w, h, ch, &wide); let second = |f: f32| ctl.progress(split + f * (1.0 - split)); let tall = carve_width_with(h, new_w, ch, &t, prot_t.as_deref(), new_h, &Interrupt::new(&cancel, &second))?; Ok(transpose(new_h, new_w, ch, &tall)) } +/// The protection map `p` (`w × h`) for the height pass, after the width changed to `new_w`: +/// plainly resampled (it only steers seams). Where values out of `0..=1` resample to NaN +/// (infinities, or neighbours whose difference overflows), the map clamped to `0..=1` (NaN to 0) +/// resampled instead, so an infinite protection still protects. +fn resample_protection(w: usize, h: usize, p: &[f32], new_w: usize) -> Vec { + let mut out = resize_bilinear(w, h, 1, p, new_w, h); + if out.iter().any(|v| v.is_nan()) { + let clamped: Vec = p.iter().map(|&v| if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }).collect(); + for (v, c) in out.iter_mut().zip(resize_bilinear(w, h, 1, &clamped, new_w, h)) { + if v.is_nan() { + *v = c; + } + } + } + out +} + /// Plain bilinear resize (the non-content-aware part of the Amount blend). pub fn resize_bilinear(w: usize, h: usize, ch: usize, img: &[f32], nw: usize, nh: usize) -> Vec { - let mut out = vec![0.0f32; nw * nh * ch]; - if w == 0 || h == 0 { + let Some(len) = nw.checked_mul(nh).and_then(|n| n.checked_mul(ch)) else { + return Vec::new(); + }; + let mut out = vec![0.0f32; len]; + if len == 0 || unusable(w, h, ch, img) { return out; } - for y in 0..nh { + let row = |(y, out): (usize, &mut [f32])| { let fy = ((y as f32 + 0.5) * h as f32 / nh as f32 - 0.5).clamp(0.0, (h - 1) as f32); let (y0, ty) = (fy.floor() as usize, fy.fract()); let y1 = (y0 + 1).min(h - 1); @@ -458,10 +715,17 @@ pub fn resize_bilinear(w: usize, h: usize, ch: usize, img: &[f32], nw: usize, nh let p = |xx: usize, yy: usize| img[(yy * w + xx) * ch + c]; let top = p(x0, y0) + (p(x1, y0) - p(x0, y0)) * tx; let bot = p(x0, y1) + (p(x1, y1) - p(x0, y1)) * tx; - out[(y * nw + x) * ch + c] = top + (bot - top) * ty; + out[x * ch + c] = top + (bot - top) * ty; } } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.par_chunks_mut(nw * ch).enumerate().for_each(row); } + #[cfg(target_arch = "wasm32")] + out.chunks_mut(nw * ch).enumerate().for_each(row); out } @@ -484,391 +748,4 @@ pub fn skin_mask(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { } #[cfg(test)] -mod tests { - use super::*; - - /// The straightforward implementation the per-seam carver replaced (full energy map, fresh - /// seam search and a new buffer per seam), kept as the oracle for `carver_matches_reference`. - mod reference { - use super::super::{PROTECT_ENERGY, resize_bilinear, transpose}; - - pub(super) fn energy(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { - let mut e = vec![0.0f32; w * h]; - for y in 0..h { - for x in 0..w { - let (xl, xr) = (x.saturating_sub(1), (x + 1).min(w - 1)); - let (yu, yd) = (y.saturating_sub(1), (y + 1).min(h - 1)); - let at = |xx: usize, yy: usize, c: usize| img[(yy * w + xx) * ch + c]; - let mut s = 0.0; - for c in 0..ch { - let v = at(x, y, c); - s += (at(xr, y, c) - v).abs() + (v - at(xl, y, c)).abs(); - s += (at(x, yd, c) - v).abs() + (v - at(x, yu, c)).abs(); - } - e[y * w + x] = s * 0.5; - } - } - e - } - pub(super) fn find_seam(w: usize, h: usize, energy: &[f32]) -> Vec { - let mut cost = energy[..w].to_vec(); - let mut from = vec![0u8; w * h]; // 0 = up-left, 1 = up, 2 = up-right - let mut next = vec![0.0f32; w]; - for y in 1..h { - for x in 0..w { - let mut best = (cost[x], 1u8); - if x > 0 && cost[x - 1] < best.0 { - best = (cost[x - 1], 0); - } - if x + 1 < w && cost[x + 1] < best.0 { - best = (cost[x + 1], 2); - } - next[x] = best.0 + energy[y * w + x]; - from[y * w + x] = best.1; - } - std::mem::swap(&mut cost, &mut next); - } - let mut x = (0..w).min_by(|a, b| cost[*a].total_cmp(&cost[*b])).unwrap_or(0); - let mut seam = vec![0usize; h]; - for y in (0..h).rev() { - seam[y] = x; - if y > 0 { - x = match from[y * w + x] { - 0 => x - 1, - 2 => x + 1, - _ => x, - }; - } - } - seam - } - fn remove_seam(w: usize, h: usize, ch: usize, img: &[f32], seam: &[usize]) -> Vec { - let mut out = Vec::with_capacity((w - 1) * h * ch); - for (y, &sx) in seam.iter().enumerate().take(h) { - let row = &img[y * w * ch..(y + 1) * w * ch]; - out.extend_from_slice(&row[..sx * ch]); - out.extend_from_slice(&row[(sx + 1) * ch..]); - } - out - } - pub(super) fn carve_width(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize) -> Vec { - let new_w = new_w.max(1); - if new_w == w || w == 0 || h == 0 { - return img.to_vec(); - } - let protect_e = |e: &mut [f32], p: Option<&Vec>| { - if let Some(p) = p { - for (e, p) in e.iter_mut().zip(p) { - *e += p.clamp(0.0, 1.0) * PROTECT_ENERGY; - } - } - }; - if new_w < w { - let (mut cur, mut cw) = (img.to_vec(), w); - let mut prot = protect.map(<[f32]>::to_vec); - while cw > new_w { - let mut e = energy(cw, h, ch, &cur); - protect_e(&mut e, prot.as_ref()); - let seam = find_seam(cw, h, &e); - cur = remove_seam(cw, h, ch, &cur, &seam); - prot = prot.map(|p| remove_seam(cw, h, 1, &p, &seam)); - cw -= 1; - } - return cur; - } - // Enlarge in rounds of at most half the current width, each duplicating the k lowest seams. - let (mut cur, mut cw) = (img.to_vec(), w); - let mut prot = protect.map(<[f32]>::to_vec); - while cw < new_w { - let k = (new_w - cw).min((cw / 2).max(1)); - // Find k seams on a shrinking copy, mapping each back to original columns. - let mut idx: Vec> = (0..h).map(|_| (0..cw).collect()).collect(); - let (mut tmp, mut tw) = (cur.clone(), cw); - let mut tprot = prot.clone(); - let mut dup: Vec> = vec![Vec::with_capacity(k); h]; - for _ in 0..k { - // A 1 px wide image's only seam is its one column: take it, so it is duplicated. - if tw == 0 { - break; - } - let mut e = energy(tw, h, ch, &tmp); - protect_e(&mut e, tprot.as_ref()); - let seam = find_seam(tw, h, &e); - for (y, &sx) in seam.iter().enumerate() { - dup[y].push(idx[y].remove(sx)); - } - tmp = remove_seam(tw, h, ch, &tmp, &seam); - tprot = tprot.map(|p| remove_seam(tw, h, 1, &p, &seam)); - tw -= 1; - } - let added = dup[0].len(); - if added == 0 { - break; - } - let nw = cw + added; - let mut out = Vec::with_capacity(nw * h * ch); - let mut nprot = prot.as_ref().map(|_| Vec::with_capacity(nw * h)); - for (y, d) in dup.iter_mut().enumerate() { - d.sort_unstable(); - let row = &cur[y * cw * ch..(y + 1) * cw * ch]; - let mut di = 0; - for x in 0..cw { - out.extend_from_slice(&row[x * ch..(x + 1) * ch]); - if let (Some(np), Some(p)) = (nprot.as_mut(), prot.as_ref()) { - np.push(p[y * cw + x]); - } - while di < d.len() && d[di] == x { - let xr = (x + 1).min(cw - 1); - for c in 0..ch { - out.push((row[x * ch + c] + row[xr * ch + c]) * 0.5); - } - if let (Some(np), Some(p)) = (nprot.as_mut(), prot.as_ref()) { - np.push(p[y * cw + x]); - } - di += 1; - } - } - } - cur = out; - prot = nprot; - cw = nw; - } - cur - } - - pub(super) fn carve(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize, new_h: usize) -> Vec { - let (new_w, new_h) = (new_w.max(1), new_h.max(1)); - let wide = carve_width(w, h, ch, img, protect, new_w); - if new_h == h { - return wide; - } - let prot_t = protect.map(|p| transpose(new_w, h, 1, &resize_bilinear(w, h, 1, p, new_w, h))); - let t = transpose(new_w, h, ch, &wide); - let tall = carve_width(h, new_w, ch, &t, prot_t.as_deref(), new_h); - transpose(new_h, new_w, ch, &tall) - } - } - - /// The carver gives bit-identical output to the reference implementation it replaced, over - /// random shapes (including 1 px wide and tall), channel counts, tie-heavy quantised noise, - /// protection, shrinking, enlarging past half the width and both axes at once. - #[test] - fn carver_matches_reference() { - let mut seed = 1u32; - let mut next = |n: usize| { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - seed as usize % n - }; - for case in 0..400u32 { - let (w, h, ch) = (1 + next(24), 1 + next(18), [1, 3, 4][next(3)]); - let levels = [2, 5, 255, 65_536][next(4)]; - let img = noise(w * h * ch, levels, 11 + case); - let protect = (next(3) == 0).then(|| noise(w * h, 3, 101 + case)); - let (nw, nh) = (1 + next(2 * w + 2), 1 + next(2 * h + 2)); - let got = carve(w, h, ch, &img, protect.as_deref(), nw, nh); - let want = reference::carve(w, h, ch, &img, protect.as_deref(), nw, nh); - let bits = |v: &[f32]| v.iter().map(|f| f.to_bits()).collect::>(); - assert_eq!(bits(&got), bits(&want), "case {case}: {w}x{h}x{ch} -> {nw}x{nh}, levels {levels}, protect {}", protect.is_some()); - } - // The seam search alone, on raw energy maps full of ties. - for case in 0..200u32 { - let (w, h) = (1 + next(40), 1 + next(30)); - let e = noise(w * h, [2, 3, 1000][next(3)], 7 + case); - assert_eq!(find_seam(w, h, &e), reference::find_seam(w, h, &e), "seam case {case}: {w}x{h}"); - let img = noise(w * h * 3, 7, 3 + case); - let bits = |v: Vec| v.into_iter().map(f32::to_bits).collect::>(); - assert_eq!(bits(energy(w, h, 3, &img)), bits(reference::energy(w, h, 3, &img)), "energy case {case}"); - } - } - - #[test] - fn cancelled_carve_stops() { - let (w, h) = (40, 20); - let img = vec![0.5f32; w * h * 3]; - let yes = || true; - assert_eq!(carve_with(w, h, 3, &img, None, 30, 15, &Interrupt::cancel_only(&yes)), Err(Cancelled)); - assert_eq!(carve_with(w, h, 3, &img, None, 30, 15, &Interrupt::NONE).unwrap(), carve(w, h, 3, &img, None, 30, 15)); - } - - /// A flat grey image with a salient vertical bar at x = 10..14 (red/blue columns). - fn bar(w: usize, h: usize) -> Vec { - let mut v = vec![0.5f32; w * h * 3]; - for y in 0..h { - for x in 10..14 { - let c = if x % 2 == 0 { [1.0, 0.0, 0.0] } else { [0.0, 0.0, 1.0] }; - v[(y * w + x) * 3..(y * w + x) * 3 + 3].copy_from_slice(&c); - } - } - v - } - - /// Columns that are pure red or pure blue top to bottom. - fn red_columns(w: usize, h: usize, img: &[f32]) -> usize { - let pure = |p: &[f32]| p[1] < 0.05 && ((p[0] > 0.95 && p[2] < 0.05) || (p[2] > 0.95 && p[0] < 0.05)); - (0..w).filter(|x| (0..h).all(|y| pure(&img[(y * w + x) * 3..(y * w + x) * 3 + 3]))).count() - } - - #[test] - fn seam_follows_low_energy() { - let (w, h) = (5, 4); - let mut e = vec![9.0f32; w * h]; - for y in 0..h { - e[y * w + 2] = 0.0; - } - assert_eq!(find_seam(w, h, &e), vec![2; 4]); - } - - #[test] - fn shrinking_keeps_the_salient_bar() { - let (w, h) = (40, 12); - let img = bar(w, h); - let out = carve(w, h, 3, &img, None, 24, h); - assert_eq!(out.len(), 24 * h * 3); - assert_eq!(red_columns(24, h, &out), 4, "the bar survives intact"); - // Plain resampling squashes it instead. - let plain = resize_bilinear(w, h, 3, &img, 24, h); - assert!(red_columns(24, h, &plain) < 4); - } - - #[test] - fn tall_proxy_shrink_preserves_full_resolution_salient_and_protected_pixels() { - let (w, h, ch, new_w) = (512, PROXY_SHRINK_MIN_HEIGHT, 3, 384); - let mut img = bar(w, h); - let mut protect = vec![0.0; w * h]; - for y in 0..h { - let i = (y * w + 256) * ch; - img[i..i + ch].copy_from_slice(&[0.03, 0.87, 0.42]); - protect[y * w + 256] = 1.0; - } - - let out = carve_width_with(w, h, ch, &img, Some(&protect), new_w, &Interrupt::NONE).unwrap(); - - assert_eq!(out.len(), new_w * h * ch); - assert_eq!(red_columns(new_w, h, &out), 4, "the full-resolution bar survives"); - for y in 0..h { - let row = &out[y * new_w * ch..(y + 1) * new_w * ch]; - assert!(row.chunks_exact(ch).any(|px| px == [0.03, 0.87, 0.42]), "protected mark survives row {y}"); - } - } - - #[test] - fn tall_proxy_shrink_observes_cancellation_before_modifying_source() { - let (w, h, ch) = (128, PROXY_SHRINK_MIN_HEIGHT, 3); - let img = vec![0.5; w * h * ch]; - let cancelled = || true; - assert_eq!(carve_width_with(w, h, ch, &img, None, 64, &Interrupt::cancel_only(&cancelled)), Err(Cancelled)); - } - - #[test] - fn enlarging_duplicates_flat_areas() { - let (w, h) = (30, 8); - let img = bar(w, h); - let out = carve(w, h, 3, &img, None, 50, h); - assert_eq!(out.len(), 50 * h * 3); - assert_eq!(red_columns(50, h, &out), 4); - } - - #[test] - fn height_and_protection() { - let (w, h) = (20, 30); - // Horizontal bar: carve height. - let mut img = vec![0.2f32; w * h]; - for x in 0..w { - img[12 * w + x] = 1.0; - } - let out = carve(w, h, 1, &img, None, w, 18); - assert_eq!(out.len(), w * 18); - assert_eq!((0..18).filter(|y| (0..w).all(|x| out[y * w + x] > 0.9)).count(), 1); - // Protecting a flat region keeps it: the seams go elsewhere. - let flat = vec![0.5f32; 16 * 4]; - let mut protect = vec![0.0f32; 16 * 4]; - for y in 0..4 { - protect[y * 16 + 3] = 1.0; - } - let mut marked = flat.clone(); - for y in 0..4 { - marked[y * 16 + 3] = 0.51; - } - let out = carve(16, 4, 1, &marked, Some(&protect), 8, 4); - assert!((0..4).all(|y| out[y * 8..y * 8 + 8].iter().any(|v| (*v - 0.51).abs() < 1e-6))); - } - - /// Noise quantised to `levels` values, so the energy map is full of exact ties. - fn noise(len: usize, levels: u32, seed: u32) -> Vec { - let mut s = seed | 1; - (0..len) - .map(|_| { - s ^= s << 13; - s ^= s >> 17; - s ^= s << 5; - (s % levels) as f32 / (levels - 1) as f32 - }) - .collect() - } - - fn fnv(v: &[f32]) -> u64 { - v.iter().flat_map(|f| f.to_bits().to_le_bytes()).fold(0xcbf2_9ce4_8422_2325, |h, b| (h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3)) - } - - /// The exact output bits of a spread of carves (shrink, enlarge past half the width, both - /// axes, protection, 1/3/4 channels, tie-heavy and smooth images), pinned so that speed work - /// can't change which seams are chosen or how pixels are blended. - #[test] - fn output_bits_are_pinned() { - let cases: [(usize, usize, usize, u32, bool, usize, usize); 8] = [ - (61, 47, 3, 2, false, 40, 47), - (61, 47, 3, 255, false, 61, 30), - (61, 47, 4, 4, true, 35, 29), - (33, 20, 1, 3, false, 80, 20), - (33, 20, 3, 1000, true, 50, 45), - (48, 36, 4, 7, false, 70, 18), - (5, 3, 1, 2, false, 1, 1), - (40, 40, 3, 2, true, 39, 41), - ]; - let got: Vec = cases - .iter() - .enumerate() - .map(|(i, &(w, h, ch, levels, prot, nw, nh))| { - let img = noise(w * h * ch, levels, 7 + i as u32); - let protect = prot.then(|| noise(w * h, 3, 99 + i as u32)); - let out = carve(w, h, ch, &img, protect.as_deref(), nw, nh); - assert_eq!(out.len(), nw * nh * ch); - fnv(&out) - }) - .collect(); - assert_eq!(got, PINNED); - } - - const PINNED: [u64; 8] = [ - 0x24ea_9353_02c2_7c58, - 0x5a6e_8554_a0cb_ebb1, - 0x8b18_d63f_396e_21c9, - 0x1db2_23ad_3e6a_a7c5, - 0x721d_6cc5_34e0_3fdf, - 0x961a_ea6d_e512_ec7d, - 0x4d25_767f_9dce_13f5, - 0x2157_9f86_27d6_b835, - ]; - - #[test] - fn one_pixel_lines_enlarge_by_repeating() { - // Width 1 has no seam to spare: its one column is repeated. Height 1 likewise (through - // the transposed pass), alone or with the other axis. - let col = [0.1f32, 0.2, 0.3, 0.4, 0.5]; - assert_eq!(carve(1, 5, 1, &col, None, 4, 5), col.iter().flat_map(|v| [*v; 4]).collect::>()); - assert_eq!(carve(5, 1, 1, &col, None, 5, 3), col.repeat(3)); - assert_eq!(carve(1, 1, 3, &[0.7, 0.8, 0.9], None, 3, 2), [0.7, 0.8, 0.9].repeat(6)); - for (w, h, nw, nh) in [(1, 5, 6, 10), (1, 17, 6, 34), (16, 1, 21, 2), (7, 1, 3, 8), (2, 1, 1, 8)] { - let img = vec![0.5f32; w * h * 4]; - assert_eq!(carve(w, h, 4, &img, Some(&vec![1.0; w * h]), nw, nh).len(), nw * nh * 4, "{w}x{h} -> {nw}x{nh}"); - } - } - - #[test] - fn skin_rule() { - let px = [0.86f32, 0.62, 0.5, 0.2, 0.4, 0.8]; - assert_eq!(skin_mask(2, 1, 3, &px), vec![1.0, 0.0]); - } -} +mod tests; diff --git a/crates/algo/src/seam/tests.rs b/crates/algo/src/seam/tests.rs new file mode 100644 index 0000000000..5788b03ce2 --- /dev/null +++ b/crates/algo/src/seam/tests.rs @@ -0,0 +1,569 @@ +use super::*; + +/// The straightforward implementation the per-seam carver replaced (full energy map, fresh +/// seam search and a new buffer per seam), kept as the oracle for `carver_matches_reference`. +mod reference { + use super::super::{protection, resample_protection, transpose}; + + pub(super) fn energy(w: usize, h: usize, ch: usize, img: &[f32]) -> Vec { + let mut e = vec![0.0f32; w * h]; + for y in 0..h { + for x in 0..w { + let (xl, xr) = (x.saturating_sub(1), (x + 1).min(w - 1)); + let (yu, yd) = (y.saturating_sub(1), (y + 1).min(h - 1)); + let at = |xx: usize, yy: usize, c: usize| img[(yy * w + xx) * ch + c]; + let mut s = 0.0; + for c in 0..ch { + let v = at(x, y, c); + s += (at(xr, y, c) - v).abs() + (v - at(xl, y, c)).abs(); + s += (at(x, yd, c) - v).abs() + (v - at(x, yu, c)).abs(); + } + let v = s * 0.5; + e[y * w + x] = if v.is_nan() { f32::INFINITY } else { v }; + } + } + e + } + pub(super) fn find_seam(w: usize, h: usize, energy: &[f32]) -> Vec { + let mut cost = energy[..w].to_vec(); + let mut from = vec![0u8; w * h]; // 0 = up-left, 1 = up, 2 = up-right + let mut next = vec![0.0f32; w]; + for y in 1..h { + for x in 0..w { + let mut best = (cost[x], 1u8); + if x > 0 && cost[x - 1] < best.0 { + best = (cost[x - 1], 0); + } + if x + 1 < w && cost[x + 1] < best.0 { + best = (cost[x + 1], 2); + } + next[x] = best.0 + energy[y * w + x]; + from[y * w + x] = best.1; + } + std::mem::swap(&mut cost, &mut next); + } + let mut x = (0..w).min_by(|a, b| cost[*a].total_cmp(&cost[*b])).unwrap_or(0); + let mut seam = vec![0usize; h]; + for y in (0..h).rev() { + seam[y] = x; + if y > 0 { + x = match from[y * w + x] { + 0 => x - 1, + 2 => x + 1, + _ => x, + }; + } + } + seam + } + fn remove_seam(w: usize, h: usize, ch: usize, img: &[f32], seam: &[usize]) -> Vec { + let mut out = Vec::with_capacity((w - 1) * h * ch); + for (y, &sx) in seam.iter().enumerate().take(h) { + let row = &img[y * w * ch..(y + 1) * w * ch]; + out.extend_from_slice(&row[..sx * ch]); + out.extend_from_slice(&row[(sx + 1) * ch..]); + } + out + } + pub(super) fn carve_width(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize) -> Vec { + let new_w = new_w.max(1); + if new_w == w || w == 0 || h == 0 { + return img.to_vec(); + } + let protect_e = |e: &mut [f32], p: Option<&Vec>| { + if let Some(p) = p { + for (e, &p) in e.iter_mut().zip(p) { + *e += protection(p); + } + } + }; + if new_w < w { + let (mut cur, mut cw) = (img.to_vec(), w); + let mut prot = protect.map(<[f32]>::to_vec); + while cw > new_w { + let mut e = energy(cw, h, ch, &cur); + protect_e(&mut e, prot.as_ref()); + let seam = find_seam(cw, h, &e); + cur = remove_seam(cw, h, ch, &cur, &seam); + prot = prot.map(|p| remove_seam(cw, h, 1, &p, &seam)); + cw -= 1; + } + return cur; + } + // Enlarge in rounds of at most half the current width, each duplicating the k lowest seams. + let (mut cur, mut cw) = (img.to_vec(), w); + let mut prot = protect.map(<[f32]>::to_vec); + while cw < new_w { + let k = (new_w - cw).min((cw / 2).max(1)); + // Find k seams on a shrinking copy, mapping each back to original columns. + let mut idx: Vec> = (0..h).map(|_| (0..cw).collect()).collect(); + let (mut tmp, mut tw) = (cur.clone(), cw); + let mut tprot = prot.clone(); + let mut dup: Vec> = vec![Vec::with_capacity(k); h]; + for _ in 0..k { + // A 1 px wide image's only seam is its one column: take it, so it is duplicated. + if tw == 0 { + break; + } + let mut e = energy(tw, h, ch, &tmp); + protect_e(&mut e, tprot.as_ref()); + let seam = find_seam(tw, h, &e); + for (y, &sx) in seam.iter().enumerate() { + dup[y].push(idx[y].remove(sx)); + } + tmp = remove_seam(tw, h, ch, &tmp, &seam); + tprot = tprot.map(|p| remove_seam(tw, h, 1, &p, &seam)); + tw -= 1; + } + let added = dup[0].len(); + if added == 0 { + break; + } + let nw = cw + added; + let mut out = Vec::with_capacity(nw * h * ch); + let mut nprot = prot.as_ref().map(|_| Vec::with_capacity(nw * h)); + for (y, d) in dup.iter_mut().enumerate() { + d.sort_unstable(); + let row = &cur[y * cw * ch..(y + 1) * cw * ch]; + let mut di = 0; + for x in 0..cw { + out.extend_from_slice(&row[x * ch..(x + 1) * ch]); + if let (Some(np), Some(p)) = (nprot.as_mut(), prot.as_ref()) { + np.push(p[y * cw + x]); + } + while di < d.len() && d[di] == x { + let xr = (x + 1).min(cw - 1); + for c in 0..ch { + out.push((row[x * ch + c] + row[xr * ch + c]) * 0.5); + } + if let (Some(np), Some(p)) = (nprot.as_mut(), prot.as_ref()) { + np.push(p[y * cw + x]); + } + di += 1; + } + } + } + cur = out; + prot = nprot; + cw = nw; + } + cur + } + + pub(super) fn carve(w: usize, h: usize, ch: usize, img: &[f32], protect: Option<&[f32]>, new_w: usize, new_h: usize) -> Vec { + let (new_w, new_h) = (new_w.max(1), new_h.max(1)); + let wide = carve_width(w, h, ch, img, protect, new_w); + if new_h == h { + return wide; + } + let prot_t = protect.map(|p| transpose(new_w, h, 1, &resample_protection(w, h, p, new_w))); + let t = transpose(new_w, h, ch, &wide); + let tall = carve_width(h, new_w, ch, &t, prot_t.as_deref(), new_h); + transpose(new_h, new_w, ch, &tall) + } +} + +/// The carver gives bit-identical output to the reference implementation it replaced, over +/// random shapes (including 1 px wide and tall), channel counts, tie-heavy quantised noise, +/// protection, shrinking, enlarging past half the width and both axes at once. +#[test] +fn carver_matches_reference() { + let mut seed = 1u32; + let mut next = |n: usize| { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + seed as usize % n + }; + for case in 0..400u32 { + let (w, h, ch) = (1 + next(24), 1 + next(18), [1, 3, 4][next(3)]); + let levels = [2, 5, 255, 65_536][next(4)]; + let img = noise(w * h * ch, levels, 11 + case); + let protect = (next(3) == 0).then(|| noise(w * h, 3, 101 + case)); + let (nw, nh) = (1 + next(2 * w + 2), 1 + next(2 * h + 2)); + let got = carve(w, h, ch, &img, protect.as_deref(), nw, nh); + let want = reference::carve(w, h, ch, &img, protect.as_deref(), nw, nh); + let bits = |v: &[f32]| v.iter().map(|f| f.to_bits()).collect::>(); + assert_eq!(bits(&got), bits(&want), "case {case}: {w}x{h}x{ch} -> {nw}x{nh}, levels {levels}, protect {}", protect.is_some()); + } + // The seam search alone, on raw energy maps full of ties. + for case in 0..200u32 { + let (w, h) = (1 + next(40), 1 + next(30)); + let e = noise(w * h, [2, 3, 1000][next(3)], 7 + case); + assert_eq!(find_seam(w, h, &e), reference::find_seam(w, h, &e), "seam case {case}: {w}x{h}"); + let img = noise(w * h * 3, 7, 3 + case); + let bits = |v: Vec| v.into_iter().map(f32::to_bits).collect::>(); + assert_eq!(bits(energy(w, h, 3, &img)), bits(reference::energy(w, h, 3, &img)), "energy case {case}"); + } +} + +/// After every removal, the carver's incrementally updated seam search matches a full pass +/// over its current energy bit for bit (path costs and the seam traced from them), on images +/// where seams run through the edges, through flat areas full of ties and through protection. +/// Its energy matches a fresh map of the pixels left, and so do the pixels. +#[test] +fn incremental_dp_matches_full_dp() { + let mut ramp = vec![0.0f32; 120 * 90]; + for (i, v) in ramp.iter_mut().enumerate() { + let (x, y) = ((i % 120) as f32, (i / 120) as f32); + *v = ((x * 0.07).sin() * (y * 0.05).cos() * 0.5 + 0.5).clamp(0.0, 1.0); + } + // NaN and infinite samples and NaN protection: infinite energies, never NaN costs. + let mut odd = noise(40 * 30 * 3, 255, 11); + odd.iter_mut().step_by(13).for_each(|v| *v = f32::NAN); + odd.iter_mut().skip(5).step_by(17).for_each(|v| *v = f32::INFINITY); + let mut odd_prot = noise(40 * 30, 3, 12); + odd_prot.iter_mut().step_by(7).for_each(|v| *v = -f32::NAN); + // (w, h, channels, image, protection, seams to remove) + type Case = (usize, usize, usize, Vec, Option>, usize); + let cases: Vec = vec![ + (120, 90, 1, ramp, None, 40), + (97, 64, 3, noise(97 * 64 * 3, 2, 5), None, 90), + (80, 70, 4, noise(80 * 70 * 4, 1000, 6), Some(noise(80 * 70, 3, 7)), 60), + (64, 50, 3, bar(64, 50), None, 63), + (33, 1, 1, noise(33, 5, 8), None, 32), + (2, 40, 3, noise(2 * 40 * 3, 9, 9), None, 2), + (50, 40, 1, vec![0.5; 50 * 40], Some(noise(50 * 40, 2, 10)), 49), + (40, 30, 3, odd, Some(odd_prot), 25), + ]; + let bits = |v: &[f32]| v.iter().map(|f| f.to_bits()).collect::>(); + for (case, (w, h, ch, img, prot, seams)) in cases.into_iter().enumerate() { + let mut c = Carver::new(w, h, ch, &img, prot.as_deref()); + for step in 0..=seams { + let rows = Rows { stride: c.w, cw: c.cw, off: &c.off }; + let mut fresh = Dp::default(); + fresh.full(rows, &c.e); + for y in 0..h { + assert_eq!(bits(&c.dp.cost[rows.of(y)]), bits(&fresh.cost[rows.of(y)]), "case {case} step {step} row {y}: costs"); + } + assert_eq!(c.dp.seam(rows), fresh.seam(rows), "case {case} step {step}: seam"); + // The pixels left, packed, and their energy (plus protection) from scratch. + let left: Vec = + (0..h).flat_map(|y| c.idx[rows.of(y)].iter().flat_map(move |&x| (0..ch).map(move |k| (y * w + x as usize) * ch + k))).map(|i| img[i]).collect(); + let mut want = energy(c.cw, h, ch, &left); + if let Some(p) = &prot { + let kept = (0..h).flat_map(|y| c.idx[rows.of(y)].iter().map(move |&x| y * w + x as usize)); + for (e, i) in want.iter_mut().zip(kept) { + *e += protection(p[i]); + } + } + let have: Vec = (0..h).flat_map(|y| c.e[rows.of(y)].to_vec()).collect(); + assert_eq!(bits(&have), bits(&want), "case {case} step {step}: energy"); + if step == seams { + assert_eq!(bits(&c.into_pixels()), bits(&left), "case {case}: pixels"); + break; + } + c.remove_seam(); + } + } +} + +/// Hostile but well-formed shapes return early instead of panicking: no channels, empty +/// sizes (also through the height pass), and images or protection maps shorter than their +/// size says (a short map protects nothing). +#[test] +fn degenerate_inputs_do_not_panic() { + assert!(carve(3, 3, 0, &[], None, 2, 2).is_empty()); + assert!(carve(3, 3, 0, &[], None, 5, 5).is_empty()); + assert!(transpose(3, 3, 0, &[]).is_empty()); + assert!(resize_bilinear(3, 3, 0, &[], 2, 2).is_empty()); + assert!(resize_bilinear(3, 3, 1, &[0.5; 9], 0, 2).is_empty()); + assert!(resize_bilinear(3, 3, 1, &[0.5; 9], 2, 0).is_empty()); + assert_eq!(resize_bilinear(3, 3, 1, &[0.5; 4], 2, 2), vec![0.0; 4]); + assert!(carve(0, 5, 3, &[], None, 3, 7).is_empty()); + assert!(carve(0, 5, 3, &[], None, 3, 2).is_empty()); + let img = noise(10 * 5, 7, 3); + assert_eq!(carve(10, 5, 1, &img, Some(&[0.0; 3]), 4, 5), carve(10, 5, 1, &img, None, 4, 5)); + assert_eq!(carve(10, 5, 1, &img, Some(&[0.0; 3]), 4, 3), carve(10, 5, 1, &img, None, 4, 3)); + assert_eq!(carve(10, 5, 3, &[0.5; 20], None, 4, 5), vec![0.5; 20]); + assert_eq!(carve_width(usize::MAX, 2, 4, &[0.5; 8], None, 1), vec![0.5; 8]); + // Values past the size are ignored, through both passes. + let long = noise(20, 7, 4); + assert_eq!(carve(5, 3, 1, &long, None, 3, 3), carve(5, 3, 1, &long[..15], None, 3, 3)); + assert_eq!(carve(5, 3, 1, &long, None, 5, 2), carve(5, 3, 1, &long[..15], None, 5, 2)); +} + +/// Infinite samples, and finite ones whose differences overflow, give energies that aren't +/// numbers; those are the highest energy (pinned deliberately: the code before NaN-free costs +/// fed NaN to the seam search and chose differently). +#[test] +fn infinite_samples_carve_deterministically() { + let inf = f32::INFINITY; + assert_eq!(carve(2, 1, 1, &[inf, 0.5], None, 1, 1), [0.5]); + assert_eq!(carve(2, 1, 1, &[f32::MAX, f32::MAX], None, 6, 1), [f32::MAX, inf, inf, inf, inf, f32::MAX]); + let mut img = noise(30 * 20 * 3, 255, 41); + img.iter_mut().step_by(11).for_each(|v| *v = inf); + img.iter_mut().skip(4).step_by(23).for_each(|v| *v = -inf); + let out = carve(30, 20, 3, &img, None, 21, 14); + assert_eq!(out.len(), 21 * 14 * 3); + assert_eq!(out, carve(30, 20, 3, &img, None, 21, 14)); +} + +/// Infinite protection protects like full protection in the height pass too, where the map +/// is resampled (and an infinity alone would resample to NaN). +#[test] +fn infinite_protection_protects_in_both_passes() { + let (w, h) = (6, 6); + let img: Vec = (0..w * h).map(|i| (i * 7 % 5) as f32 / 4.0).collect(); + let row = |v: f32| (0..w * h).map(|i| if i / w == 2 { v } else { 0.0 }).collect::>(); + let (full, inf) = (row(1.0), row(f32::INFINITY)); + for (nw, nh) in [(6, 3), (4, 3), (3, 6)] { + assert_eq!(carve(w, h, 1, &img, Some(&inf), nw, nh), carve(w, h, 1, &img, Some(&full), nw, nh), "{nw}x{nh}"); + } + let kept = carve(w, h, 1, &img, Some(&inf), 6, 3); + assert!(kept.chunks(6).any(|r| r == &img[12..18]), "the protected row survives"); +} + +/// A cancel that arrives after the seam search, during the tall-image rebuild, still stops +/// the carve. +#[test] +fn proxy_rebuild_observes_cancellation() { + use std::sync::atomic::{AtomicUsize, Ordering}; + let (w, h, ch, new_w) = (128, PROXY_SHRINK_MIN_HEIGHT, 3, 64); + let img = noise(w * h * ch, 7, 5); + let calls = AtomicUsize::new(0); + // One check per seam passes; the first one after the search cancels. + let cancel = || calls.fetch_add(1, Ordering::Relaxed) >= w - new_w; + assert_eq!(carve_width_with(w, h, ch, &img, None, new_w, &Interrupt::cancel_only(&cancel)), Err(Cancelled)); + assert!(calls.load(Ordering::Relaxed) > w - new_w, "the rebuild checked for cancellation"); +} + +/// NaN samples and NaN protection steer seams the same whatever their sign and payload +/// (which arithmetic does not preserve consistently across builds): the same pixels are kept. +#[test] +fn nan_payloads_do_not_steer_seams() { + let (w, h, ch) = (24, 18, 3); + let (base, prot_base) = (noise(w * h * ch, 255, 21), noise(w * h, 3, 22)); + let carve_with_nans = |pixel: f32, prot: f32| { + let (mut img, mut p) = (base.clone(), prot_base.clone()); + img.iter_mut().step_by(37).for_each(|v| *v = pixel); + p.iter_mut().step_by(29).for_each(|v| *v = prot); + carve(w, h, ch, &img, Some(&p), 15, 11) + }; + let canonical = |v: Vec| v.into_iter().map(|f| if f.is_nan() { u32::MAX } else { f.to_bits() }).collect::>(); + let nans = [f32::NAN, -f32::NAN, f32::from_bits(0x7fc0_1234), f32::from_bits(0xffc0_0001)]; + let want = canonical(carve_with_nans(nans[0], nans[0])); + for &a in &nans { + for &b in &nans { + assert_eq!(canonical(carve_with_nans(a, b)), want, "pixel NaN {:#x}, protection NaN {:#x}", a.to_bits(), b.to_bits()); + } + } +} + +#[test] +fn cancelled_carve_stops() { + let (w, h) = (40, 20); + let img = vec![0.5f32; w * h * 3]; + let yes = || true; + assert_eq!(carve_with(w, h, 3, &img, None, 30, 15, &Interrupt::cancel_only(&yes)), Err(Cancelled)); + assert_eq!(carve_with(w, h, 3, &img, None, 30, 15, &Interrupt::NONE).unwrap(), carve(w, h, 3, &img, None, 30, 15)); +} + +/// A flat grey image with a salient vertical bar at x = 10..14 (red/blue columns). +fn bar(w: usize, h: usize) -> Vec { + let mut v = vec![0.5f32; w * h * 3]; + for y in 0..h { + for x in 10..14 { + let c = if x % 2 == 0 { [1.0, 0.0, 0.0] } else { [0.0, 0.0, 1.0] }; + v[(y * w + x) * 3..(y * w + x) * 3 + 3].copy_from_slice(&c); + } + } + v +} + +/// Columns that are pure red or pure blue top to bottom. +fn red_columns(w: usize, h: usize, img: &[f32]) -> usize { + let pure = |p: &[f32]| p[1] < 0.05 && ((p[0] > 0.95 && p[2] < 0.05) || (p[2] > 0.95 && p[0] < 0.05)); + (0..w).filter(|x| (0..h).all(|y| pure(&img[(y * w + x) * 3..(y * w + x) * 3 + 3]))).count() +} + +#[test] +fn seam_follows_low_energy() { + let (w, h) = (5, 4); + let mut e = vec![9.0f32; w * h]; + for y in 0..h { + e[y * w + 2] = 0.0; + } + assert_eq!(find_seam(w, h, &e), vec![2; 4]); +} + +#[test] +fn shrinking_keeps_the_salient_bar() { + let (w, h) = (40, 12); + let img = bar(w, h); + let out = carve(w, h, 3, &img, None, 24, h); + assert_eq!(out.len(), 24 * h * 3); + assert_eq!(red_columns(24, h, &out), 4, "the bar survives intact"); + // Plain resampling squashes it instead. + let plain = resize_bilinear(w, h, 3, &img, 24, h); + assert!(red_columns(24, h, &plain) < 4); +} + +#[test] +fn tall_proxy_shrink_preserves_full_resolution_salient_and_protected_pixels() { + let (w, h, ch, new_w) = (512, PROXY_SHRINK_MIN_HEIGHT, 3, 384); + let mut img = bar(w, h); + let mut protect = vec![0.0; w * h]; + for y in 0..h { + let i = (y * w + 256) * ch; + img[i..i + ch].copy_from_slice(&[0.03, 0.87, 0.42]); + protect[y * w + 256] = 1.0; + } + + let out = carve_width_with(w, h, ch, &img, Some(&protect), new_w, &Interrupt::NONE).unwrap(); + + assert_eq!(out.len(), new_w * h * ch); + assert_eq!(red_columns(new_w, h, &out), 4, "the full-resolution bar survives"); + for y in 0..h { + let row = &out[y * new_w * ch..(y + 1) * new_w * ch]; + assert!(row.chunks_exact(ch).any(|px| px == [0.03, 0.87, 0.42]), "protected mark survives row {y}"); + } +} + +#[test] +fn tall_proxy_shrink_observes_cancellation_before_modifying_source() { + let (w, h, ch) = (128, PROXY_SHRINK_MIN_HEIGHT, 3); + let img = vec![0.5; w * h * ch]; + let cancelled = || true; + assert_eq!(carve_width_with(w, h, ch, &img, None, 64, &Interrupt::cancel_only(&cancelled)), Err(Cancelled)); +} + +#[test] +fn enlarging_duplicates_flat_areas() { + let (w, h) = (30, 8); + let img = bar(w, h); + let out = carve(w, h, 3, &img, None, 50, h); + assert_eq!(out.len(), 50 * h * 3); + assert_eq!(red_columns(50, h, &out), 4); +} + +#[test] +fn height_and_protection() { + let (w, h) = (20, 30); + // Horizontal bar: carve height. + let mut img = vec![0.2f32; w * h]; + for x in 0..w { + img[12 * w + x] = 1.0; + } + let out = carve(w, h, 1, &img, None, w, 18); + assert_eq!(out.len(), w * 18); + assert_eq!((0..18).filter(|y| (0..w).all(|x| out[y * w + x] > 0.9)).count(), 1); + // Protecting a flat region keeps it: the seams go elsewhere. + let flat = vec![0.5f32; 16 * 4]; + let mut protect = vec![0.0f32; 16 * 4]; + for y in 0..4 { + protect[y * 16 + 3] = 1.0; + } + let mut marked = flat.clone(); + for y in 0..4 { + marked[y * 16 + 3] = 0.51; + } + let out = carve(16, 4, 1, &marked, Some(&protect), 8, 4); + assert!((0..4).all(|y| out[y * 8..y * 8 + 8].iter().any(|v| (*v - 0.51).abs() < 1e-6))); +} + +/// Noise quantised to `levels` values, so the energy map is full of exact ties. +fn noise(len: usize, levels: u32, seed: u32) -> Vec { + let mut s = seed | 1; + (0..len) + .map(|_| { + s ^= s << 13; + s ^= s >> 17; + s ^= s << 5; + (s % levels) as f32 / (levels - 1) as f32 + }) + .collect() +} + +fn fnv(v: &[f32]) -> u64 { + v.iter().flat_map(|f| f.to_bits().to_le_bytes()).fold(0xcbf2_9ce4_8422_2325, |h, b| (h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3)) +} + +/// The exact output bits of a spread of carves (shrink, enlarge past half the width, both +/// axes, protection, 1/3/4 channels, tie-heavy and smooth images), pinned so that speed work +/// can't change which seams are chosen or how pixels are blended. +#[test] +fn output_bits_are_pinned() { + let cases: [(usize, usize, usize, u32, bool, usize, usize); 8] = [ + (61, 47, 3, 2, false, 40, 47), + (61, 47, 3, 255, false, 61, 30), + (61, 47, 4, 4, true, 35, 29), + (33, 20, 1, 3, false, 80, 20), + (33, 20, 3, 1000, true, 50, 45), + (48, 36, 4, 7, false, 70, 18), + (5, 3, 1, 2, false, 1, 1), + (40, 40, 3, 2, true, 39, 41), + ]; + let got: Vec = cases + .iter() + .enumerate() + .map(|(i, &(w, h, ch, levels, prot, nw, nh))| { + let img = noise(w * h * ch, levels, 7 + i as u32); + let protect = prot.then(|| noise(w * h, 3, 99 + i as u32)); + let out = carve(w, h, ch, &img, protect.as_deref(), nw, nh); + assert_eq!(out.len(), nw * nh * ch); + fnv(&out) + }) + .collect(); + assert_eq!(got, PINNED); +} + +const PINNED: [u64; 8] = [ + 0x24ea_9353_02c2_7c58, + 0x5a6e_8554_a0cb_ebb1, + 0x8b18_d63f_396e_21c9, + 0x1db2_23ad_3e6a_a7c5, + 0x721d_6cc5_34e0_3fdf, + 0x961a_ea6d_e512_ec7d, + 0x4d25_767f_9dce_13f5, + 0x2157_9f86_27d6_b835, +]; + +/// [`output_bits_are_pinned`] for the reduced-height seam search of tall images (rows at +/// least [`PROXY_SHRINK_MIN_HEIGHT`], at least [`PROXY_SHRINK_MIN_SEAMS`] seams): widths and, +/// through the transposed pass, heights, with and without protection, on tie-heavy and +/// fine-grained noise. +#[test] +fn tall_proxy_output_bits_are_pinned() { + let cases: [(usize, usize, usize, u32, bool, usize, usize); 5] = [ + (96, PROXY_SHRINK_MIN_HEIGHT, 3, 2, false, 30, PROXY_SHRINK_MIN_HEIGHT), + (130, 2_100, 4, 255, true, 64, 2_100), + (80, 2_600, 1, 65_536, false, 16, 2_600), + (PROXY_SHRINK_MIN_HEIGHT, 90, 3, 5, false, PROXY_SHRINK_MIN_HEIGHT, 20), + (2_050, 100, 4, 1000, true, 2_050, 36), + ]; + let got: Vec = cases + .iter() + .enumerate() + .map(|(i, &(w, h, ch, levels, prot, nw, nh))| { + let img = noise(w * h * ch, levels, 31 + i as u32); + let protect = prot.then(|| noise(w * h, 3, 131 + i as u32)); + let out = carve(w, h, ch, &img, protect.as_deref(), nw, nh); + assert_eq!(out.len(), nw * nh * ch); + fnv(&out) + }) + .collect(); + assert_eq!(got, TALL_PINNED); +} + +const TALL_PINNED: [u64; 5] = [0x5657_83ac_cb26_56a5, 0x81a6_b5f7_e299_fdfb, 0xcdf2_9df9_9b83_def5, 0xa713_544a_4dcc_d8d8, 0x3557_de9f_ffb0_881e]; + +#[test] +fn one_pixel_lines_enlarge_by_repeating() { + // Width 1 has no seam to spare: its one column is repeated. Height 1 likewise (through + // the transposed pass), alone or with the other axis. + let col = [0.1f32, 0.2, 0.3, 0.4, 0.5]; + assert_eq!(carve(1, 5, 1, &col, None, 4, 5), col.iter().flat_map(|v| [*v; 4]).collect::>()); + assert_eq!(carve(5, 1, 1, &col, None, 5, 3), col.repeat(3)); + assert_eq!(carve(1, 1, 3, &[0.7, 0.8, 0.9], None, 3, 2), [0.7, 0.8, 0.9].repeat(6)); + for (w, h, nw, nh) in [(1, 5, 6, 10), (1, 17, 6, 34), (16, 1, 21, 2), (7, 1, 3, 8), (2, 1, 1, 8)] { + let img = vec![0.5f32; w * h * 4]; + assert_eq!(carve(w, h, 4, &img, Some(&vec![1.0; w * h]), nw, nh).len(), nw * nh * 4, "{w}x{h} -> {nw}x{nh}"); + } +} + +#[test] +fn skin_rule() { + let px = [0.86f32, 0.62, 0.5, 0.2, 0.4, 0.8]; + assert_eq!(skin_mask(2, 1, 3, &px), vec![1.0, 0.0]); +} diff --git a/crates/algo/src/segment/mod.rs b/crates/algo/src/segment.rs similarity index 91% rename from crates/algo/src/segment/mod.rs rename to crates/algo/src/segment.rs index b30ce909b7..958d3d0f6a 100644 --- a/crates/algo/src/segment/mod.rs +++ b/crates/algo/src/segment.rs @@ -252,14 +252,28 @@ pub fn grid_cut_cancellable( weight: impl Fn(f32) -> f32, cancel: &dyn Fn() -> bool, ) -> Option> { - grid_cut_impl::(img, cost_fg, cost_bg, fixed, weight, cancel) + grid_cut_impl::(img, cost_fg, cost_bg, fixed, weight, cancel, false) +} + +/// [`grid_cut_cancellable`] solved with Boykov–Kolmogorov instead of push/relabel: faster when +/// the data costs come from colour models (GrabCut and the band re-cut), where terminal links +/// are few and paths short. The minimum cut value is the same. +pub fn grid_cut_bk_cancellable( + img: &RgbImage, + cost_fg: &[f32], + cost_bg: &[f32], + fixed: &[u8], + weight: impl Fn(f32) -> f32, + cancel: &dyn Fn() -> bool, +) -> Option> { + grid_cut_impl::(img, cost_fg, cost_bg, fixed, weight, cancel, true) } mod edge_starts; // The const parameter also runs the previous full scan in before/after workflow tests. fn grid_cut_weighted(img: &RgbImage, cost_fg: &[f32], cost_bg: &[f32], fixed: &[u8], weight: impl Fn(f32) -> f32) -> Vec { - grid_cut_impl::(img, cost_fg, cost_bg, fixed, weight, &|| false).unwrap_or_default() + grid_cut_impl::(img, cost_fg, cost_bg, fixed, weight, &|| false, false).unwrap_or_default() } fn grid_cut_impl( @@ -269,6 +283,7 @@ fn grid_cut_impl( fixed: &[u8], weight: impl Fn(f32) -> f32, cancel: &dyn Fn() -> bool, + bk: bool, ) -> Option> { let (w, h) = (img.w as i32, img.h as i32); let Some(n) = img.w.checked_mul(img.h).filter(|&n| n < u32::MAX as usize - 2) else { return Some(Vec::new()) }; @@ -336,7 +351,9 @@ fn grid_cut_impl( if cancel() { return None; } - if OPTIMIZED { + if OPTIMIZED && bk { + g.maxflow_bk_with(cancel)?; + } else if OPTIMIZED { g.maxflow_with(cancel)?; } else { g.maxflow_bk(); @@ -524,23 +541,24 @@ fn components4(mask: &[bool], w: usize, h: usize) -> (Vec, Vec) { /// rule as the magic wand's anti-aliasing). pub fn antialias_u8(mask: &mut [u8], w: usize, h: usize) { let src = mask.to_vec(); - let at = |x: i32, y: i32, v: u8| if x < 0 || y < 0 || x >= w as i32 || y >= h as i32 { v } else { src[y as usize * w + x as usize] }; + // A span whose 3×3 neighbourhood holds one value has no edge pixel, so it is only compared: + // most of a full-resolution mask is far from its boundary. 64 pixels keep the three row + // compares cheap per pixel, and a span that does reach the boundary costs few extra pixels. + const SPAN: usize = 64; + let uniform = |y: usize, x0: usize, x1: usize| { + let (a, b, v) = (x0.saturating_sub(1), (x1 + 1).min(w), src[y * w + x0]); + (y.saturating_sub(1)..(y + 2).min(h)).all(|ny| src[ny * w + a..ny * w + b].iter().all(|n| *n == v)) + }; let row = |y: usize, out: &mut [u8]| { - for (x, o) in out.iter_mut().enumerate() { - let v = src[y * w + x]; - let mut sum = 0u32; - let mut edge = false; - for dy in -1..=1 { - for dx in -1..=1 { - let n = at(x as i32 + dx, y as i32 + dy, v); - edge |= n != v; - sum += n as u32; - } + for (c, span) in out.chunks_mut(SPAN).enumerate() { + let x0 = c * SPAN; + if uniform(y, x0, x0 + span.len()) { + continue; } - if edge { - let avg = sum as f32 / (9.0 * 255.0); - let f = if v > 127 { 0.5 + 0.5 * avg } else { 0.5 * avg }; - *o = (f * 255.0).round() as u8; + for (i, o) in span.iter_mut().enumerate() { + if let Some(v) = edge_value(&src, w, h, x0 + i, y) { + *o = v; + } } } }; @@ -553,6 +571,27 @@ pub fn antialias_u8(mask: &mut [u8], w: usize, h: usize) { mask.chunks_mut(w).enumerate().for_each(|(y, r)| row(y, r)); } +/// The anti-aliased value of pixel `(x, y)` of `src` (`w` × `h`), or `None` when every neighbour +/// in its 3×3 neighbourhood has its value (neighbours outside the mask count as the pixel itself). +fn edge_value(src: &[u8], w: usize, h: usize, x: usize, y: usize) -> Option { + let v = src[y * w + x]; + let mut sum = 0u32; + let mut edge = false; + for dy in -1..=1 { + for dx in -1..=1 { + let (nx, ny) = (x as i32 + dx, y as i32 + dy); + let n = if nx < 0 || ny < 0 || nx >= w as i32 || ny >= h as i32 { v } else { src[ny as usize * w + nx as usize] }; + edge |= n != v; + sum += n as u32; + } + } + edge.then(|| { + let avg = sum as f32 / (9.0 * 255.0); + let f = if v > 127 { 0.5 + 0.5 * avg } else { 0.5 * avg }; + (f * 255.0).round() as u8 + }) +} + /// Shrinks a region's box to its non-zero pixels (`None` if empty). pub fn trim_region(r: Region) -> Option { let w = r.bbox.width() as usize; @@ -711,7 +750,7 @@ fn refine_band( } let beta = contrast_beta(&img); // `grid_cut`'s weight, with the cut stopping on cancel. - let cut = grid_cut_cancellable(&img, &cfg, &cbg, &fixed, |d2| 50.0 * (-beta * d2).exp(), &cancel).ok_or(Cancelled)?; + let cut = grid_cut_bk_cancellable(&img, &cfg, &cbg, &fixed, |d2| 50.0 * (-beta * d2).exp(), &cancel).ok_or(Cancelled)?; let (cw, ch) = (t.width() as usize, t.height() as usize); let mut core = Vec::with_capacity(cw * ch); for yy in 0..ch { diff --git a/crates/algo/src/segment/grabcut.rs b/crates/algo/src/segment/grabcut.rs index 573c09079e..249763ad49 100644 --- a/crates/algo/src/segment/grabcut.rs +++ b/crates/algo/src/segment/grabcut.rs @@ -10,7 +10,7 @@ use photocraft_geom::Rect; use photocraft_raster::{Cancelled, Interrupt}; use super::gmm::{DEFAULT_REG, Gmm}; -use super::{FREE, HARD_BG, HARD_FG, Region, RgbImage, Sampler, contrast_beta, data_costs, grid_cut_cancellable, subsample}; +use super::{FREE, HARD_BG, HARD_FG, Region, RgbImage, Sampler, contrast_beta, data_costs, grid_cut_bk_cancellable, subsample}; /// Definite background. pub const BG: u8 = 0; @@ -67,7 +67,7 @@ pub fn grabcut_with(img: &RgbImage, trimap: &mut [u8], iters: usize, ctl: &Inter }; let (cf, cb) = data_costs(img, &fg, &bg); // `grid_cut`'s weight, with the cut stopping on cancel. - let cut = grid_cut_cancellable(img, &cf, &cb, &fixed, |d2| GAMMA * (-beta * d2).exp(), &cancel).ok_or(Cancelled)?; + let cut = grid_cut_bk_cancellable(img, &cf, &cb, &fixed, |d2| GAMMA * (-beta * d2).exp(), &cancel).ok_or(Cancelled)?; let mut changed = 0usize; for (t, fgnd) in trimap.iter_mut().zip(&cut) { if *t == PR_BG || *t == PR_FG { @@ -126,7 +126,9 @@ pub fn object_select(sampler: &dyn Sampler, canvas: Rect, rect: Rect, work_px: u } let (fg, bg) = grabcut(&img, &mut trimap, 8)?; let low: Vec = trimap.iter().map(|t| is_fg(*t)).collect(); - let low = super::clean_mask(&low, lw, lh, 0.1, 0.002); + let low = super::clean_mask(&low, lw, lh, 0.1, 0.0); + // Every enclosed hole, however small, fills only when it looks like the object, so background seen through it stays open. + let low = super::fill_holes_by_model(&low, &img, &fg, &bg, 0.0, 0.05); super::finish_region(sampler, window, step, &low, lw, lh, Some((&fg, &bg))) } @@ -139,11 +141,15 @@ pub const OBJECT_AT_WORK_PX: usize = 60_000; /// A heuristic, not a semantic segmenter (that is #2788): on a working-resolution copy of the /// canvas the area around the point is grown over pixels of similar colour (CIELAB, bounded both /// by the distance to the seed's running mean and by the local edge step), so it stops at the -/// object's outline. A region that grows over most of the canvas is the background: nothing is -/// found. Otherwise GrabCut refines it inside its padded bounding box (the grown area probable +/// object's outline. A region covering most of the canvas boundary is treated as background. +/// Otherwise GrabCut refines it inside its padded bounding box (the grown area probable /// foreground, the cursor definite foreground, outside the box definite background), the part /// connected to the cursor is kept and the boundary is re-cut at full resolution, as the /// rectangle mode does. +/// +/// When the whole-canvas pass finds nothing in a large document (an object only a few working +/// pixels wide blurs into its surroundings), the search is repeated at finer steps in windows of +/// the same working size around the point, down to full resolution. pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: usize) -> Option { if canvas.width() < 4 || canvas.height() < 4 || !x.is_finite() || !y.is_finite() { return None; @@ -152,7 +158,30 @@ pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: u if !canvas.contains(px, py) { return None; } - let step = super::scale_for(canvas, work_px.max(1024)); + let work_px = work_px.max(1024); + let mut step = super::scale_for(canvas, work_px); + if let Some(r) = object_in(sampler, canvas, px, py, step) { + return Some(r); + } + // Side (working pixels) of the square windows of the finer passes. + let side = (work_px as f64).sqrt() as usize; + while step > 1 { + step = (step / 4).max(1); + let half = (side.saturating_mul(step) / 2).min(i32::MAX as usize / 4) as i32; + let window = Rect::new(px.saturating_sub(half), py.saturating_sub(half), px.saturating_add(half), py.saturating_add(half)).intersect(&canvas); + if window.width() < 4 || window.height() < 4 { + continue; + } + if let Some(r) = object_in(sampler, window, px, py, step) { + return Some(r); + } + } + None +} + +/// One [`object_at`] pass over `canvas` (the document or a window of it) at working step `step`; +/// `(px, py)` lies inside `canvas`. +fn object_in(sampler: &dyn Sampler, canvas: Rect, px: i32, py: i32, step: usize) -> Option { let img = sampler.rgb_scaled(canvas, step); let (w, h) = (img.w, img.h); if w < 2 || h < 2 { @@ -164,8 +193,9 @@ pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: u let sy = (((py - canvas.y0) as usize) / step).min(h - 1); let grown = grow_similar(&lab, w, h, sx, sy); let count = grown.iter().filter(|g| **g).count(); - // Background (or a flat image): no single object under the cursor. - if count * 10 > w * h * 6 || count < 4 { + let boundary = grown.iter().enumerate().filter(|(i, on)| **on && (i % w == 0 || i % w + 1 == w || i / w == 0 || i / w + 1 == h)).count(); + // shortcut: cropped objects covering most of the frame need a semantic segmenter (#2788). + if boundary * 2 >= 2 * w + 2 * h - 4 || count < 4 { return None; } let (mut x0, mut y0, mut x1, mut y1) = (w, h, 0usize, 0usize); @@ -209,7 +239,9 @@ pub fn object_at(sampler: &dyn Sampler, canvas: Rect, x: f32, y: f32, work_px: u let mut seeds = vec![false; w * h]; seeds[sy * w + sx] = true; let low = super::keep_seeded(&low, &seeds, w, h); - let low = super::clean_mask(&low, w, h, 0.1, 0.01); + let low = super::clean_mask(&low, w, h, 0.1, 0.0); + // As in the rectangle mode: enclosed holes fill only when they look like the object. + let low = super::fill_holes_by_model(&low, &img, &fg, &bg, 0.0, 0.05); if !low.iter().any(|v| *v) { return None; } diff --git a/crates/algo/src/segment/maxflow.rs b/crates/algo/src/segment/maxflow.rs index dcde4ebdfb..6fef7f8faf 100644 --- a/crates/algo/src/segment/maxflow.rs +++ b/crates/algo/src/segment/maxflow.rs @@ -140,11 +140,22 @@ impl Graph { self.bk(cancel) } + /// [`Graph::maxflow_with`] that always runs Boykov–Kolmogorov. It is the faster solver on + /// the 8-connected colour-model grids of GrabCut (about 2.3x on a 107k-pixel working image); + /// push/relabel wins on Quick Selection's many area-cost terminals, which keep + /// [`Graph::maxflow_with`]. + pub fn maxflow_bk_with(&mut self, cancel: &dyn Fn() -> bool) -> Option { + self.bk(cancel) + } + pub(super) fn maxflow_bk(&mut self) -> f64 { self.bk(&never).unwrap_or(self.flow) } fn bk(&mut self, cancel: &dyn Fn() -> bool) -> Option { + if cancel() { + return None; + } let n = self.node_count(); let mut queue: VecDeque = VecDeque::new(); let mut orphans: VecDeque = VecDeque::new(); @@ -219,7 +230,7 @@ impl Graph { self.adopt(o as usize, time, &mut queue, &mut orphans); } } - Some(self.flow) + if cancel() { None } else { Some(self.flow) } } fn augment(&mut self, m: usize, orphans: &mut VecDeque) { @@ -408,3 +419,51 @@ impl Graph { s } } + +#[cfg(test)] +mod cancellation_tests { + use super::{CANCEL_EVERY, Graph}; + use std::cell::Cell; + + #[test] + fn bk_checks_cancellation_on_short_runs() { + let mut graph = Graph::with_capacity(2, 0); + graph.add_tweights(0, 1.0, 0.0); + graph.add_tweights(1, 0.0, 1.0); + assert!(graph.maxflow_bk_with(&|| true).is_none()); + + let calls = Cell::new(0); + assert!( + graph + .maxflow_bk_with(&|| { + calls.set(calls.get() + 1); + calls.get() == 2 + }) + .is_none() + ); + assert_eq!(calls.get(), 2, "short run must check before returning"); + } + + #[test] + fn bk_checks_cancellation_during_a_long_run() { + let mut graph = Graph::with_capacity(CANCEL_EVERY as usize + 1, 0); + for i in 0..graph.node_count() { + graph.add_tweights(i, 1.0, 0.0); + } + let calls = Cell::new(0); + assert!( + graph + .maxflow_bk_with(&|| { + calls.set(calls.get() + 1); + calls.get() == 2 + }) + .is_none() + ); + // The first check is at entry; more than CANCEL_EVERY active nodes ensure the + // second is the periodic poll, before the solver can finish. + assert_eq!(calls.get(), 2); + // Without the periodic poll the final check would be the second call. The poll stops the + // run with the last queued node unprocessed, still marked active. + assert!(graph.active[CANCEL_EVERY as usize], "periodic cancel must stop before the last queued node is processed"); + } +} diff --git a/crates/algo/src/segment/quick.rs b/crates/algo/src/segment/quick.rs index 8306b6e662..6ff0dfe398 100644 --- a/crates/algo/src/segment/quick.rs +++ b/crates/algo/src/segment/quick.rs @@ -265,7 +265,7 @@ fn cut_levels( let (w, h) = (img.w, img.h); let zeros = vec![0.0f32; w * h]; if w * h <= direct_px || w < 8 || h < 8 { - return super::grid_cut_impl::(img, cost_fg, &zeros, fixed, |d2| scale * weight(d2), cancel); + return super::grid_cut_impl::(img, cost_fg, &zeros, fixed, |d2| scale * weight(d2), cancel, false); } let (cw, ch) = (w.div_ceil(2), h.div_ceil(2)); let coarse_img = img.downsample(2); @@ -310,7 +310,7 @@ fn cut_levels( } }) .collect(); - super::grid_cut_impl::(img, cost_fg, &zeros, &banded, |d2| scale * weight(d2), cancel) + super::grid_cut_impl::(img, cost_fg, &zeros, &banded, |d2| scale * weight(d2), cancel, false) } /// Pixels within `radius` (working pixels) of the polyline `pts`, plus each point's own pixel. diff --git a/crates/algo/src/segment/tests.rs b/crates/algo/src/segment/tests.rs index 743c3dc460..07f52b815b 100644 --- a/crates/algo/src/segment/tests.rs +++ b/crates/algo/src/segment/tests.rs @@ -288,6 +288,65 @@ fn grabcut_trimap_on_disc() { assert!(iou > 0.95, "IoU {iou}"); } +#[test] +fn grabcut_small_disc_mask_is_pinned() { + // The mask GrabCut produced with the push/relabel solver, before the switch to BK. Min-cut + // ties may break differently between solvers, so any drift here is a visible change. + let (img, _) = disc_scene(32, 24, 16.0, 12.0, 8.0, 3); + let mut tri = vec![grabcut::BG; img.w * img.h]; + for y in 2..22 { + for x in 6..26 { + tri[y * img.w + x] = grabcut::PR_FG; + } + } + grabcut::grabcut(&img, &mut tri, 8).unwrap(); + let got: Vec = tri.chunks(img.w).map(|r| r.iter().map(|t| if *t == grabcut::PR_FG || *t == grabcut::FG { '#' } else { '.' }).collect()).collect(); + let want = [ + "................................", + "................................", + "................................", + "................................", + ".............######.............", + "...........##########...........", + "..........############..........", + ".........##############.........", + ".........##############.........", + "........################........", + "........################........", + "........################........", + "........################........", + "........################........", + "........################........", + ".........##############.........", + ".........##############.........", + "..........############..........", + "...........##########...........", + ".............######.............", + "................................", + "................................", + "................................", + "................................", + ]; + assert_eq!(got, want); +} + +#[test] +fn grabcut_large_disc_mask_is_pinned() { + // 5168 free nodes with terminal links both ways, so the pre-BK solver was push/relabel here. + // The count and FNV-1a hash are of the mask that push/relabel produced at the merge base. + let (img, _) = disc_scene(96, 80, 48.0, 40.0, 26.0, 3); + let mut tri = vec![grabcut::BG; img.w * img.h]; + for y in 6..74 { + for x in 10..86 { + tri[y * img.w + x] = grabcut::PR_FG; + } + } + grabcut::grabcut(&img, &mut tri, 8).unwrap(); + let fg: Vec = tri.iter().map(|t| *t == grabcut::PR_FG || *t == grabcut::FG).collect(); + let hash = fg.iter().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(*b)).wrapping_mul(0x0100_0000_01b3)); + assert_eq!((fg.iter().filter(|b| **b).count(), hash), (2128, 0x62fd_c64a_0ee4_4e75)); +} + #[test] fn object_select_disc_full_resolution() { let (img, truth) = disc_scene(200, 160, 100.0, 80.0, 50.0, 2); @@ -751,6 +810,38 @@ fn object_at_finds_the_disc_under_the_cursor() { assert!(v > 0.9, "IoU {v}"); } +#[test] +fn object_at_keeps_large_foregrounds_but_not_the_surrounding_background() { + let (w, h) = (400, 300); + for margin in [30, 20, 8] { + let inside = |x: usize, y: usize| x >= margin && x < w - margin && y >= margin && y < h - margin; + let img = RgbImage::from_fn(w, h, |x, y| { + let noise = ((x * 13 + y * 7) % 17) as f32 / 850.0; + if inside(x, y) { [0.8 + noise, 0.2, 0.1] } else { [0.1, 0.2, 0.8 + noise] } + }); + let truth: Vec<_> = (0..w * h).map(|i| inside(i % w, i / w)).collect(); + let sampler = ImageSampler { img: &img, origin: (-40, 25) }; + let canvas = Rect::new(-40, 25, w as i32 - 40, h as i32 + 25); + let region = grabcut::object_at(&sampler, canvas, 160.0, 175.0, grabcut::OBJECT_AT_WORK_PX) + .unwrap_or_else(|| panic!("missing large foreground with margin {margin}")); + let local = Region { bbox: region.bbox.translate(40, -25), mask: region.mask }; + assert!(iou(&local, &truth, w, h) > 0.95, "margin {margin}"); + assert!(grabcut::object_at(&sampler, canvas, -39.0, 26.0, grabcut::OBJECT_AT_WORK_PX).is_none(), "background margin {margin}"); + } +} + +#[test] +fn object_at_allows_a_foreground_touching_one_edge_but_rejects_a_flat_image() { + let img = RgbImage::from_fn(400, 300, |x, y| if x < 360 && (20..280).contains(&y) { [0.8, 0.2, 0.1] } else { [0.1, 0.2, 0.8] }); + let sampler = ImageSampler { img: &img, origin: (0, 0) }; + let canvas = Rect::new(0, 0, 400, 300); + let region = grabcut::object_at(&sampler, canvas, 200.0, 150.0, grabcut::OBJECT_AT_WORK_PX).unwrap(); + assert!(region.at(0, 150) > 0.9 && region.at(350, 150) > 0.9); + assert!(region.at(380, 150) < 0.1 && region.at(200, 5) < 0.1); + let flat = RgbImage::from_fn(400, 300, |_, _| [0.5; 3]); + assert!(grabcut::object_at(&ImageSampler { img: &flat, origin: (0, 0) }, canvas, 200.0, 150.0, grabcut::OBJECT_AT_WORK_PX).is_none()); +} + #[test] fn object_at_on_the_background_or_off_canvas_finds_nothing() { let (img, _) = disc_scene(320, 240, 160.0, 120.0, 70.0, 7); @@ -762,6 +853,28 @@ fn object_at_on_the_background_or_off_canvas_finds_nothing() { assert!(grabcut::object_at(&s, canvas, 400.0, 10.0, 0).is_none()); } +/// #6576: a 24 px object in a 4000×3000 document is found (a 1000×800 one still is). +#[test] +fn object_at_finds_small_object_in_large_canvas() { + let (w, h) = (4000usize, 3000usize); + let canvas = Rect::new(0, 0, w as i32, h as i32); + for (ox, oy, ow, oh) in [(188usize, 138usize, 24usize, 24usize), (1500, 1100, 1000, 800)] { + let inside = |x: usize, y: usize| x >= ox && x < ox + ow && y >= oy && y < oy + oh; + let img = RgbImage::from_fn(w, h, |x, y| if inside(x, y) { [0.8, 0.1, 0.1] } else { [0.9, 0.9, 0.9] }); + let truth: Vec = (0..w * h).map(|i| inside(i % w, i / w)).collect(); + let s = ImageSampler { img: &img, origin: (0, 0) }; + let (cx, cy) = ((ox + ow / 2) as f32, (oy + oh / 2) as f32); + let reg = grabcut::object_at(&s, canvas, cx, cy, grabcut::OBJECT_AT_WORK_PX); + let reg = reg.unwrap_or_else(|| panic!("the {ow}×{oh} object is not found")); + let v = iou(®, &truth, w, h); + assert!(v > 0.8, "{ow}×{oh}: IoU {v}"); + } + // The flat background around the small object is still nothing. + let img = RgbImage::from_fn(w, h, |x, y| if (188..212).contains(&x) && (138..162).contains(&y) { [0.8, 0.1, 0.1] } else { [0.9, 0.9, 0.9] }); + let s = ImageSampler { img: &img, origin: (0, 0) }; + assert!(grabcut::object_at(&s, canvas, 400.0, 300.0, grabcut::OBJECT_AT_WORK_PX).is_none()); +} + // ---------- Select Subject on the cases reported in #2788 ---------- /// A figure whose upper part is close to the background colour (a cream shirt on a near-white @@ -849,6 +962,50 @@ fn select_subject_leaves_a_background_coloured_gap_open() { assert!(v > 0.9, "IoU {v}"); } +#[test] +fn antialias_blends_edge_pixels_with_their_neighbourhood() { + // A lone selected pixel keeps most of its coverage and its neighbours get half of a ninth; + // neighbours outside the mask count as the pixel itself. + let mut m = vec![0, 0, 0, 0, 255, 0, 0, 0, 0]; + antialias_u8(&mut m, 3, 3); + assert_eq!(m, [14, 14, 14, 14, 142, 14, 14, 14, 14]); +} + +#[test] +fn antialias_skips_only_spans_without_edge_pixels() { + // Every pixel through the per-pixel rule, no span skipped. + let every_pixel = |mask: &mut [u8], w: usize, h: usize| { + let src = mask.to_vec(); + for y in 0..h { + for x in 0..w { + if let Some(v) = edge_value(&src, w, h, x, y) { + mask[y * w + x] = v; + } + } + } + }; + let mut rng = Rng::new(11); + // Widths around the 64-pixel span, single rows and columns, discs, noise and stripes. + for (w, h) in [(1usize, 1usize), (1, 9), (9, 1), (63, 4), (64, 3), (65, 5), (127, 2), (129, 70), (300, 200)] { + let (cx, cy, r) = (w as f32 * 0.6, h as f32 * 0.4, w.min(h) as f32 / 3.0); + let masks: Vec> = vec![ + vec![255; w * h], + vec![0; w * h], + (0..w * h).map(|i| if ((i % w) as f32 - cx).hypot((i / w) as f32 - cy) < r { 255 } else { 0 }).collect(), + (0..w * h).map(|_| if rng.f32() < 0.02 { 255 } else { 0 }).collect(), + (0..w * h).map(|_| if rng.f32() < 0.5 { 255 } else { 0 }).collect(), + (0..w * h).map(|i| if (i % w) / 64 % 2 == 0 { 255 } else { 0 }).collect(), + (0..w * h).map(|i| if i % w == w - 1 || i / w == h / 2 { 255 } else { 0 }).collect(), + ]; + for (k, m) in masks.into_iter().enumerate() { + let (mut fast, mut slow) = (m.clone(), m); + antialias_u8(&mut fast, w, h); + every_pixel(&mut slow, w, h); + assert!(fast == slow, "{w}×{h} mask {k}"); + } + } +} + /// Timing on a 6 MP figure, for before/after numbers (`cargo test --release -- --ignored select_subject_timing --nocapture`). #[test] #[ignore = "timing only"] @@ -912,3 +1069,145 @@ fn select_subject_dump_pictures() { write("ring", &img, None); write("ring-selected", &img, reg.as_ref()); } + +/// A dark square on a light background with a square hole that shows the background colour. +fn square_with_hole(hole: usize) -> RgbImage { + let (w, h) = (400usize, 300usize); + let mut rng = Rng::new(11); + let (x0, y0) = (100usize, 50usize); + RgbImage::from_fn(w, h, |x, y| { + let n = 0.02 * rng.normal(); + let in_square = x >= x0 && x < x0 + 200 && y >= y0 && y < y0 + 200; + let in_hole = x >= x0 + 96 && x < x0 + 96 + hole && y >= y0 + 96 && y < y0 + 96 + hole; + let c = if in_square && !in_hole { [0.1, 0.12, 0.15] } else { [0.9, 0.9, 0.85] }; + c.map(|v: f32| (v + n).clamp(0.0, 1.0)) + }) +} + +#[test] +fn object_select_keeps_small_background_hole_open() { + let img = square_with_hole(8); + let s = ImageSampler { img: &img, origin: (0, 0) }; + let canvas = Rect::new(0, 0, 400, 300); + let reg = grabcut::object_select(&s, canvas, Rect::new(80, 30, 320, 270), 1_000_000).unwrap(); + assert!(reg.at(150, 100) >= 0.5, "the square itself is selected"); + assert!(reg.at(200, 150) < 0.5, "background seen through the object stays unselected"); +} + +#[test] +fn object_at_keeps_small_background_hole_open() { + let img = square_with_hole(16); + let s = ImageSampler { img: &img, origin: (0, 0) }; + let canvas = Rect::new(0, 0, 400, 300); + let reg = grabcut::object_at(&s, canvas, 130.0, 80.0, 1_000_000).unwrap(); + assert!(reg.at(150, 100) >= 0.5, "the square itself is selected"); + assert!(reg.at(203, 153) < 0.5, "background seen through the object stays unselected"); +} + +#[test] +fn hole_fill_without_specks_fills_object_coloured_holes_only() { + // The hole check Object Selection uses: no hole fills just for being small, a speck of the object's colour still does. + let (w, h) = (40usize, 40usize); + let mut rng = Rng::new(5); + let in_square = |x: usize, y: usize| (5..35).contains(&x) && (5..35).contains(&y); + let bg_hole = |x: usize, y: usize| (10..12).contains(&x) && (10..12).contains(&y); + let fg_hole = |x: usize, y: usize| (25..27).contains(&x) && (25..27).contains(&y); + let img = RgbImage::from_fn(w, h, |x, y| { + let n = 0.02 * rng.normal(); + let c = if in_square(x, y) && !bg_hole(x, y) { [0.1, 0.12, 0.15] } else { [0.9, 0.9, 0.85] }; + c.map(|v: f32| (v + n).clamp(0.0, 1.0)) + }); + let mask: Vec = (0..w * h).map(|i| in_square(i % w, i / w) && !bg_hole(i % w, i / w) && !fg_hole(i % w, i / w)).collect(); + // The colour models, as GrabCut would fit them: the object colour vs. the backdrop colour. + let dark: Vec = (0..w * h).map(|i| in_square(i % w, i / w) && !bg_hole(i % w, i / w)).collect(); + let (fs, bs): (Vec<_>, Vec<_>) = img.px.iter().zip(&dark).partition(|(_, m)| **m); + let fit = |v: Vec<(&[f32; 3], &bool)>| Gmm::fit(&v.iter().map(|(p, _)| **p).collect::>(), 3, gmm::DEFAULT_REG).unwrap(); + let (fg, bg) = (fit(fs), fit(bs)); + let out = fill_holes_by_model(&mask, &img, &fg, &bg, 0.0, 0.05); + assert!(out[25 * w + 25], "a speck of the object's colour fills"); + assert!(!out[10 * w + 10], "a speck of the background's colour stays open"); +} + +/// Energy of a labelling: data costs plus the contrast weights of separated 8-neighbours. +fn grid_energy(img: &RgbImage, cf: &[f32], cb: &[f32], cut: &[bool], weight: impl Fn(f32) -> f32) -> f64 { + let (w, h) = (img.w as i32, img.h as i32); + let mut e = 0.0f64; + for y in 0..h { + for x in 0..w { + let i = (y * w + x) as usize; + e += f64::from(if cut[i] { cf[i] } else { cb[i] }); + for (dx, dy, len) in NB { + let (nx, ny) = (x + dx, y + dy); + if nx >= 0 && nx < w && ny < h { + let j = (ny * w + nx) as usize; + if cut[i] != cut[j] { + e += f64::from(weight(d2(img.px[i], img.px[j])) / len); + } + } + } + } + } + e +} + +#[test] +fn bk_and_default_grid_cut_tie_reaches_the_same_energy() { + // Hard background down the left edge, hard foreground down the right, uniform pixels and equal + // data costs: a vertical cut at any column separates the borders at the same energy, so only + // the solvers' tie-breaking decides the labelling. + let (w, h) = (120usize, 100usize); + let img = RgbImage::from_fn(w, h, |_, _| [0.5, 0.5, 0.5]); + let (cf, cb) = (vec![1.0f32; w * h], vec![1.0f32; w * h]); + let fixed: Vec = (0..w * h) + .map(|i| match i % w { + 0 => HARD_BG, + x if x == w - 1 => HARD_FG, + _ => FREE, + }) + .collect(); + let weight = |_: f32| 2.0; + // Two distinct candidate labellings (vertical cuts at different columns) tie at the minimum. + let split = |col: usize| -> Vec { (0..w * h).map(|i| i % w >= col).collect() }; + let (e30, e90) = (grid_energy(&img, &cf, &cb, &split(30), weight), grid_energy(&img, &cf, &cb, &split(90), weight)); + assert!((e30 - e90).abs() <= 1e-6 * e30.abs().max(1.0), "candidates must tie: {e30} vs {e90}"); + assert_ne!(split(30), split(90)); + let a = grid_cut_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("default cut"); + let b = grid_cut_bk_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("bk cut"); + let (ea, eb) = (grid_energy(&img, &cf, &cb, &a, weight), grid_energy(&img, &cf, &cb, &b, weight)); + assert!((ea - e30).abs() <= 1e-3 * e30.abs().max(1.0), "default {ea} is not the tied minimum {e30}"); + assert!((eb - e30).abs() <= 1e-3 * e30.abs().max(1.0), "bk {eb} is not the tied minimum {e30}"); + // The solvers may pick different minimum cuts here; only the energy is guaranteed. + assert!(a.iter().any(|&l| l) && b.iter().any(|&l| l), "both label the foreground border"); +} + +#[test] +fn bk_and_default_grid_cut_reach_the_same_minimum_energy() { + // Large enough (>= 4096 free nodes, both terminals) that the default solver is push/relabel. + let (w, h) = (120usize, 100usize); + let mut rng = Rng::new(7); + let img = RgbImage::from_fn(w, h, |x, y| { + let inside = (x as f32 - 60.0).powi(2) + (y as f32 - 50.0).powi(2) < 1600.0; + let base = if inside { [0.8, 0.3, 0.2] } else { [0.2, 0.4, 0.7] }; + base.map(|v| v + 0.08 * rng.normal()) + }); + let cf: Vec = img.px.iter().map(|p| 4.0 * d2(*p, [0.8, 0.3, 0.2])).collect(); + let cb: Vec = img.px.iter().map(|p| 4.0 * d2(*p, [0.2, 0.4, 0.7])).collect(); + let fixed: Vec = (0..w * h) + .map(|i| { + if i % w == 0 { + HARD_BG + } else if i == (50 * w + 60) { + HARD_FG + } else { + FREE + } + }) + .collect(); + let weight = |d: f32| 50.0 * (-8.0 * d).exp(); + let a = grid_cut_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("default cut"); + let b = grid_cut_bk_cancellable(&img, &cf, &cb, &fixed, weight, &|| false).expect("bk cut"); + let (ea, eb) = (grid_energy(&img, &cf, &cb, &a, weight), grid_energy(&img, &cf, &cb, &b, weight)); + assert!((ea - eb).abs() <= 1e-3 * ea.abs().max(1.0), "default {ea} vs bk {eb}"); + assert_eq!(a, b, "same labelling, not just the same energy"); + assert!(grid_cut_bk_cancellable(&img, &cf, &cb, &fixed, weight, &|| true).is_none(), "BK honours cancel"); +} diff --git a/crates/algo/src/segment/workflow_tests.rs b/crates/algo/src/segment/workflow_tests.rs index ca3be5b40f..96c6caeb8f 100644 --- a/crates/algo/src/segment/workflow_tests.rs +++ b/crates/algo/src/segment/workflow_tests.rs @@ -1,4 +1,28 @@ use super::*; + +#[test] +#[ignore = "release Object Finder timing on a 24 MP document"] +#[allow(clippy::assertions_on_constants)] +fn object_finder_large_foreground_release_timing() { + assert!(!cfg!(debug_assertions), "run with --release"); + let (w, h) = (6000, 4000); + let img = RgbImage::from_fn(w, h, |x, y| if (300..5700).contains(&x) && (200..3800).contains(&y) { [0.8, 0.2, 0.1] } else { [0.1, 0.2, 0.8] }); + let sampler = ImageSampler { img: &img, origin: (0, 0) }; + let canvas = Rect::new(0, 0, w as i32, h as i32); + for (name, x, y) in [("foreground", 3000.0, 2000.0), ("background", 10.0, 10.0)] { + let start = std::time::Instant::now(); + let region = grabcut::object_at(&sampler, canvas, x, y, grabcut::OBJECT_AT_WORK_PX); + println!("OBJECT_FINDER {w}x{h} {name}: {:.1} ms", start.elapsed().as_secs_f64() * 1000.0); + if name == "foreground" { + let region = region.unwrap(); + assert!(region.at(3000, 2000) > 0.9 && region.at(310, 210) > 0.9); + assert!(region.at(10, 10) < 0.1); + } else { + assert!(region.is_none()); + } + } +} + #[test] #[ignore = "release comparison of actual Quick Selection strokes on 24-36 MP documents"] #[allow(clippy::assertions_on_constants)] diff --git a/crates/algo/src/selection.rs b/crates/algo/src/selection.rs index 94f744d0be..a86fc52966 100644 --- a/crates/algo/src/selection.rs +++ b/crates/algo/src/selection.rs @@ -134,18 +134,30 @@ pub fn mask_to_surface(m: &[f32], area: Rect) -> Surface { surface_from_masks(area, m, None, |_, v| v) } +mod combine_mask; +pub use combine_mask::combine_band; + /// Combines `new` with `old` by `mode`. Returns `None` when nothing is selected. pub fn combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMode) -> Option { if area.is_empty() { return None; } + // Replace already writes straight from the input mask to tiles; it needs no old mask. + if mode == SelectionMode::Replace || (old.is_none() && mode == SelectionMode::Add) { + let s = mask_to_surface(new, area); + return (s.tile_count() > 0).then_some(s); + } + let old = old?; + if old.format() == PixelFormat::GRAY8 { + return combine_mask::gray8(Some(old), new, area, mode, false); + } // Quantize to the 8-bit grid selections are stored on, so combining a mask with itself is // exact. Below half an 8-bit step is nothing. let q = |v: f32| f32::from(coverage8(v)) / 255.0; let s = match mode { SelectionMode::Replace => surface_from_masks(area, new, None, |_, v| v), _ => { - let prev = mask_from_surface(old, area); + let prev = mask_from_surface(Some(old), area); match mode { SelectionMode::Add => surface_from_masks(area, new, Some(&prev), |p, v| p.max(q(v))), SelectionMode::Subtract => surface_from_masks(area, new, Some(&prev), |p, v| (p - q(v)).max(0.0)), @@ -381,6 +393,9 @@ pub fn combine_region(old: Option<&Surface>, new: Option<&Region>, mode: Selecti let empty = Region { bbox: Rect::EMPTY, mask: Vec::new() }; let new = new.unwrap_or(&empty); let b = new.bbox; + if old.is_none_or(|s| s.format() == PixelFormat::GRAY8) { + return combine_mask::gray8_bytes(old, &new.mask, b, mode); + } let prev_u8 = |s: &Surface| -> Vec { let mut v = Vec::new(); s.read_region_into(b, &mut v); @@ -761,24 +776,27 @@ pub fn tone_range(px: &[[f32; 4]], lo: f32, hi: f32, falloff: f32) -> Vec { } mod distance; +mod inverse; +mod lasso; mod marquee; +mod morphology; pub use distance::edt; -pub use marquee::{combine_surfaces, ellipse_surface}; +pub use inverse::inverse_surface; +pub(crate) use lasso::clip_segment; +pub use lasso::{MAX_PAINTED_TILES, polygon_edge_tiles, polygon_surface}; +pub use marquee::{combine_sparse, combine_surfaces, ellipse_surface}; /// Grows the selection by `r` pixels. +/// Invalid dimensions or nonfinite/negative radii return an empty buffer. pub fn expand(m: &[f32], w: usize, h: usize, r: f32) -> Vec { - let inside: Vec = m.iter().map(|v| *v >= 0.5).collect(); - let d = edt(&inside, w, h); - // A pixel whose centre is `d` away from the nearest selected centre is - // covered when d ≤ r (fractional radii give partial coverage). - m.iter().zip(d).map(|(v, d)| v.max((r + 1.0 - d).clamp(0.0, 1.0))).collect() + morphology::modify(m, w, h, r, morphology::Kind::Expand) } /// Shrinks the selection by `r` pixels. +/// Invalid dimensions or nonfinite/negative radii return an empty buffer. pub fn contract(m: &[f32], w: usize, h: usize, r: f32) -> Vec { - let inv: Vec = m.iter().map(|v| 1.0 - v).collect(); - expand(&inv, w, h, r).into_iter().map(|v| 1.0 - v).collect() + morphology::modify(m, w, h, r, morphology::Kind::Contract) } /// Select › Modify › Border: a soft band `2r` wide centred on the selection edge, as in @@ -787,14 +805,9 @@ pub fn contract(m: &[f32], w: usize, h: usize, r: f32) -> Vec { /// either side (measured on Photoshop 27.11, Border 4: 63, 127, 191, 255 | 255, 191, 127, 63 /// across a straight edge, the same along the inside of a corner). It used to be a hard band /// `r` wide. +/// Invalid dimensions or nonfinite/negative radii return an empty buffer. pub fn border(m: &[f32], w: usize, h: usize, r: f32) -> Vec { - let inside: Vec = m.iter().map(|v| *v >= 0.5).collect(); - let outside: Vec = inside.iter().map(|v| !v).collect(); - let (to_inside, to_outside) = (edt(&inside, w, h), edt(&outside, w, h)); - if r <= 0.0 || to_inside.len() != m.len() || to_outside.len() != m.len() { - return vec![0.0; m.len()]; - } - inside.iter().zip(to_inside.iter().zip(&to_outside)).map(|(&sel, (&di, &dout))| (1.0 - ((if sel { dout } else { di }) - 1.0) / r).clamp(0.0, 1.0)).collect() + morphology::modify(m, w, h, r, morphology::Kind::Border) } /// Smooth: removes specks and rounds corners (box average of radius `round(r)`, edges @@ -810,18 +823,35 @@ pub fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec { crate::selection_blur::feather(m, w, h, radius) } +/// How far [`feather`] spreads, in pixels (`⌈3σ⌉` with σ = `radius`, 0 when it changes nothing). +/// Samples farther than this from every nonzero sample stay 0, so a working window grown by this +/// much gives the same result as the whole canvas. +pub fn feather_reach(radius: f32) -> usize { + crate::selection_blur::feather_reach(radius) +} + /// Anti-aliased polygon (even-odd) coverage over `area`: exact horizontal /// coverage, 4 sub-rows per pixel row (or 1 when not anti-aliased). Bands of /// rows are filled in parallel, and each sub-row tests only the edges that /// cross it (an active edge list over the edges sorted by their top). pub fn polygon(points: &[(f32, f32)], area: Rect, anti_alias: bool) -> Vec { + polygon_window(points, area, area, anti_alias) +} + +/// Rasterizes only `window` intersected with `canvas`, retaining the coordinate arithmetic of +/// [`polygon`] over the whole canvas. Keeping that origin avoids rounding changes in fractional +/// edge coverage when a feathered lasso uses a smaller working mask. +pub fn polygon_window(points: &[(f32, f32)], canvas: Rect, window: Rect, anti_alias: bool) -> Vec { + let area = window.intersect(&canvas); let (w, h) = (area.width() as usize, area.height() as usize); let mut m = vec![0.0f32; w * h]; if points.len() < 3 || w == 0 { return m; } let sub = if anti_alias { 4 } else { 1 }; - let sample_y = |py: usize, s: usize| area.y0 as f32 + py as f32 + (s as f32 + 0.5) / sub as f32; + let dx = (i64::from(area.x0) - i64::from(canvas.x0)) as usize; + let dy = (i64::from(area.y0) - i64::from(canvas.y0)) as usize; + let sample_y = |py: usize, s: usize| canvas.y0 as f32 + (dy + py) as f32 + (s as f32 + 0.5) / sub as f32; // A sub-row at y crosses the edge a→b iff top <= y < bottom; horizontal and NaN edges // never cross one. struct Edge { @@ -868,12 +898,13 @@ pub fn polygon(points: &[(f32, f32)], area: Rect, anti_alias: bool) -> Vec xs.extend(active.iter().map(|&&Edge { a, b, .. }| a.0 + (y - a.1) / (b.1 - a.1) * (b.0 - a.0))); xs.sort_by(|a, b| a.total_cmp(b)); for span in xs.as_chunks::<2>().0 { - let (x0, x1) = ((span[0] - area.x0 as f32).max(0.0), (span[1] - area.x0 as f32).min(w as f32)); + let (x0, x1) = ((span[0] - canvas.x0 as f32).max(0.0), (span[1] - canvas.x0 as f32).min(canvas.width() as f32)); if x1 <= x0 { continue; } - let (c0, c1) = (x0.floor() as usize, (x1.ceil() as usize).min(w)); + let (c0, c1) = ((x0.floor() as usize).saturating_sub(dx), (x1.ceil() as usize).saturating_sub(dx).min(w)); for (cx, v) in row.iter_mut().enumerate().take(c1).skip(c0) { + let cx = cx + dx; let cov = if anti_alias { ((cx + 1) as f32).min(x1) - (cx as f32).max(x0) } else { @@ -893,668 +924,4 @@ pub fn polygon(points: &[(f32, f32)], area: Rect, anti_alias: bool) -> Vec } #[cfg(test)] -mod tests { - use super::*; - - fn img(w: usize, h: usize, f: impl Fn(usize, usize) -> [f32; 4]) -> Vec<[f32; 4]> { - (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| f(x, y)).collect() - } - - #[test] - fn wand_contiguous_vs_global() { - // Two separate red squares on white. - let px = img(20, 10, |x, _| if (2..6).contains(&x) || (12..16).contains(&x) { [1.0, 0.0, 0.0, 1.0] } else { [1.0; 4] }); - let a = Rect::new(0, 0, 20, 10); - let c = magic_wand(&px, a, (3, 3), 10.0, true, false); - assert_eq!(c.iter().filter(|v| **v > 0.0).count(), 40); - let g = magic_wand(&px, a, (3, 3), 10.0, false, false); - assert_eq!(g.iter().filter(|v| **v > 0.0).count(), 80); - let aa = magic_wand(&px, a, (3, 3), 10.0, true, true); - assert!(aa[3 * 20 + 6] > 0.0 && aa[3 * 20 + 6] < 1.0); - assert!(magic_wand(&px, a, (50, 3), 10.0, true, false).iter().all(|v| *v == 0.0)); - } - - #[test] - fn wand_tolerance() { - let px = img(10, 1, |x, _| [x as f32 * 10.0 / 255.0, 0.0, 0.0, 1.0]); - let m = magic_wand(&px, Rect::new(0, 0, 10, 1), (0, 0), 25.0, true, false); - assert_eq!(m.iter().filter(|v| **v > 0.0).count(), 3); - } - - #[test] - fn wand_ignores_hidden_rgb_only_when_both_pixels_are_fully_transparent() { - let area = Rect::new(0, 0, 6, 1); - let pixels = [[255, 0, 0, 0], [0, 255, 0, 0], [255, 0, 0, 128], [253, 0, 0, 128], [0, 0, 255, 0], [255, 0, 0, 255]]; - let floats: Vec<_> = pixels.iter().map(|p| p.map(|v| f32::from(v) / 255.0)).collect(); - for (seed, tolerance, contiguous, expected) in [ - ((0, 0), 0.0, true, [1.0, 1.0, 0.0, 0.0, 0.0, 0.0]), - ((0, 0), 0.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]), - ((4, 0), 32.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]), - ((2, 0), 0.0, false, [0.0, 0.0, 1.0, 0.0, 0.0, 0.0]), - ((2, 0), 2.0, true, [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]), - ((5, 0), 32.0, false, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]), - ] { - assert_eq!(magic_wand(&floats, area, seed, tolerance, contiguous, false), expected); - let region = wand_region(&pixels, area, seed, tolerance, contiguous, false).unwrap(); - for (x, value) in expected.into_iter().enumerate() { - assert_eq!(region.at(x as i32, 0), value, "seed={seed:?}, tolerance={tolerance}, x={x}"); - } - } - // Hidden RGB cannot introduce extra edges into the anti-aliased selection either. - let plain: Vec<_> = pixels.iter().map(|p| if p[3] == 0 { [0; 4] } else { *p }).collect(); - for contiguous in [false, true] { - assert_eq!(wand_region(&pixels, area, (0, 0), 0.0, contiguous, true), wand_region(&plain, area, (0, 0), 0.0, contiguous, true)); - } - } - - #[test] - fn skin_tones_selects_skin_and_not_cream_or_white() { - let px = |rgb: [u8; 3]| [rgb[0], rgb[1], rgb[2]].map(|v| f32::from(v) / 255.0); - let rgba = |rgb: [u8; 3]| { - let [r, g, b] = px(rgb); - [r, g, b, 1.0] - }; - // Skin in light and shadow, then a cream shirt, a near-white wall, blue denim and black. - let skin = - [[183, 119, 81], [177, 132, 93], [180, 121, 84], [120, 76, 52], [173, 107, 75], [255, 219, 172], [194, 140, 104], [141, 85, 36], [92, 51, 23]] - .map(rgba); - let m = skin_tones(&skin, 40.0); - assert!(m.iter().all(|&v| v > 0.95), "skin {m:?}"); - let other = [[231, 225, 202], [252, 253, 247], [40, 60, 110], [5, 5, 5], [128, 128, 128], [220, 40, 40], [200, 160, 40]].map(rgba); - let m = skin_tones(&other, 40.0); - assert!(m.iter().all(|&v| v < 0.05), "not skin {m:?}"); - // Fuzziness widens the region; transparent pixels are never selected. - let pale = [rgba([230, 215, 200])]; - assert!(skin_tones(&pale, 200.0)[0] > skin_tones(&pale, 0.0)[0]); - assert_eq!(skin_tones(&[[0.7, 0.45, 0.3, 0.0]], 40.0), vec![0.0]); - } - - #[test] - fn color_range_falloff() { - let px = vec![[1.0, 0.0, 0.0, 1.0], [0.9, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0]]; - let m = color_range(&px, [1.0, 0.0, 0.0], 40.0); - assert_eq!(m[0], 1.0); - assert!(m[1] > 0.3 && m[1] < 1.0); - assert_eq!(m[2], 0.0); - assert_eq!(m[3], 0.0); - } - - #[test] - fn color_range_several_samples_and_localized() { - let red = [1.0, 0.0, 0.0, 1.0]; - let blue = [0.0, 0.0, 1.0, 1.0]; - let green = [0.0, 1.0, 0.0, 1.0]; - // 4 × 1: red, blue, green, red. - let px = vec![red, blue, green, red]; - let s = |c: [f32; 4], at: Option<(f32, f32)>| RangeSample { color: [c[0], c[1], c[2]], at }; - let m = color_range_samples(&px, 4, &[s(red, None), s(blue, None)], 10.0, None); - assert_eq!(m, vec![1.0, 1.0, 0.0, 1.0]); - // One sample is exactly the single-colour Color Range. - assert_eq!(color_range_samples(&px, 4, &[s(red, None)], 40.0, None), color_range(&px, [1.0, 0.0, 0.0], 40.0)); - // Localized at x = 0 with a 2 px range: the far red pixel (x = 3) drops out, x = 1 would be half. - let m = color_range_samples(&px, 4, &[s(red, Some((0.0, 0.0)))], 10.0, Some(2.0)); - assert_eq!(m, vec![1.0, 0.0, 0.0, 0.0]); - let reds = vec![red; 4]; - let m = color_range_samples(&reds, 4, &[s(red, Some((0.0, 0.0)))], 10.0, Some(2.0)); - assert_eq!(m, vec![1.0, 0.5, 0.0, 0.0]); - // No samples: nothing selected. - assert!(color_range_samples(&px, 4, &[], 10.0, None).iter().all(|v| *v == 0.0)); - } - - /// Photoshop 2024's own Color Range coverage around the sample Lab (60, 25, 19.9961), read - /// from its selections on a 16-bit Lab document: (Lab in Photoshop's internal scale, where - /// a/b = (v16 − 32768)/256, fuzziness, coverage). - const PHOTOSHOP_COVERAGE: &[([f32; 3], f32, f32)] = &[ - ([61.9989, 25.5977, 20.5742], 40.0, 0.9686), - ([64.9989, 25.5977, 20.5742], 40.0, 0.7961), - ([67.9988, 25.5977, 20.5742], 40.0, 0.4824), - ([71.9997, 25.5977, 20.5742], 40.0, 0.1098), - ([79.9985, 25.5977, 20.5742], 40.0, 0.0), - ([71.9997, 25.5977, 20.5742], 100.0, 0.8118), - ([79.9985, 25.5977, 20.5742], 100.0, 0.4824), - ([60.0, 13.5508, 20.5742], 40.0, 0.0432), - ([60.0, 19.5742, 20.5742], 40.0, 0.6706), - ([60.0, 22.582, 20.5742], 40.0, 0.9333), - ([60.0, 28.6055, 20.5742], 40.0, 0.8549), - ([60.0, 31.6211, 20.5742], 40.0, 0.5098), - ([60.0, 37.6445, 20.5742], 40.0, 0.0078), - ([60.0, 13.5508, 20.5742], 100.0, 0.7647), - ([60.0, 37.6445, 20.5742], 100.0, 0.7137), - ([60.0, 25.5977, 10.5352], 40.0, 0.1725), - ([60.0, 25.5977, 16.5586], 40.0, 0.8667), - ([60.0, 25.5977, 24.5898], 40.0, 0.7647), - ([60.0, 25.5977, 30.6133], 40.0, 0.0863), - ([60.0, 25.5977, 10.5352], 100.0, 0.8392), - ([60.0, 25.5977, 30.6133], 100.0, 0.8), - ]; - - #[test] - fn lab_range_matches_photoshop() { - let r = LabRange::point([60.0, 25.0, 19.9961]); - for &(lab, fz, ps) in PHOTOSHOP_COVERAGE { - let got = lab_range_coverage(lab, &r, fz); - assert!((got - ps).abs() <= 2.0 / 255.0, "{lab:?} at fuzziness {fz}: {got} vs Photoshop {ps}"); - } - // Lightness is three times as forgiving as hue: the same 6-unit step along L and along a. - assert!(lab_range_coverage([66.0, 25.0, 20.0], &r, 40.0) > lab_range_coverage([60.0, 31.0, 20.0], &r, 40.0)); - // The sampled colour itself is always fully selected. - for fz in [1.0, 10.0, 40.0] { - assert_eq!(lab_range_coverage([60.0, 25.0, 19.9961], &r, fz), 1.0); - } - assert_eq!(quantize_lab8([60.1, 25.4, -19.6]), [(153.0f32 / 2.55), 25.0, -20.0]); - } - - #[test] - fn lab_range_box_and_edges() { - let r = LabRange::spanning(&[[50.0, 10.0, 0.0], [60.0, 20.0, 5.0]]).unwrap(); - assert_eq!(r, LabRange { lo: [50.0, 10.0, 0.0], hi: [60.0, 20.0, 5.0] }); - assert!(LabRange::spanning(&[]).is_none()); - // Everything inside the box is fully selected, whatever the fuzziness. - for fz in [1.0, 40.0, 200.0] { - assert_eq!(lab_range_coverage([55.0, 15.0, 2.0], &r, fz), 1.0); - } - // Coverage falls off outside it and is gone a fuzziness away. - let near = lab_range_coverage([55.0, 22.0, 2.0], &r, 40.0); - assert!(near > 0.0 && near < 1.0, "{near}"); - assert_eq!(lab_range_coverage([55.0, 40.0, 2.0], &r, 40.0), 0.0); - // Fuzziness 0: exact colours only. - let p = LabRange::point([50.0, 10.0, -5.0]); - assert_eq!(lab_range_coverage([50.0, 10.0, -5.0], &p, 0.0), 1.0); - assert_eq!(lab_range_coverage([50.0, 11.0, -5.0], &p, 0.0), 0.0); - // Hostile numbers never panic and never select. - for v in [f32::NAN, f32::INFINITY, -f32::INFINITY] { - for fz in [v, 40.0] { - let k = lab_range_coverage([v, 0.0, 0.0], &p, fz); - assert!((0.0..=1.0).contains(&k), "{k}"); - } - } - } - - /// Boxes Photoshop 2024's Color Range dialog returned after Subtract from Sample, at - /// fuzziness 40 unless noted: (lo, hi, subtracted value, fuzziness, new lo, new hi) on one axis. - const PHOTOSHOP_SUBTRACT_A: &[(f32, f32, f32, f32, f32, f32)] = &[ - (0.0, 20.0, 5.0, 40.0, 5.0, 20.0), - (0.0, 20.0, 10.0, 40.0, 0.0, 10.0), - (0.0, 20.0, 15.0, 40.0, 0.0, 15.0), - (0.0, 20.0, 0.0, 40.0, 2.68, 20.0), - (0.0, 20.0, -2.0, 40.0, 0.68, 20.0), - (0.0, 20.0, -3.0, 40.0, 2.36, 20.0), - (0.0, 20.0, -8.0, 40.0, 0.04, 20.0), - (0.0, 20.0, -11.0, 40.0, 2.4, 20.0), - (0.0, 20.0, -13.0, 40.0, 0.4, 20.0), - (0.0, 20.0, -14.0, 40.0, 0.0, 20.0), - (0.0, 20.0, 20.0, 40.0, 0.0, 17.32), - (0.0, 20.0, 23.0, 40.0, 0.0, 17.64), - (0.0, 20.0, 33.0, 40.0, 0.0, 19.6), - (0.0, 20.0, 0.0, 80.0, 5.35, 20.0), - (0.0, 20.0, -12.0, 80.0, 4.05, 20.0), - (0.0, 20.0, -22.0, 80.0, 4.76, 20.0), - (0.0, 20.0, -30.0, 80.0, 0.0, 20.0), - (0.0, 6.0, 4.0, 40.0, 0.0, 4.0), - (0.0, 2.0, 0.0, 40.0, 2.0, 2.0), - (0.0, 2.0, 3.0, 40.0, 0.0, 0.32), - ]; - - #[test] - fn subtract_reshapes_the_box_like_photoshop() { - for &(lo, hi, m, fz, want_lo, want_hi) in PHOTOSHOP_SUBTRACT_A { - let r = LabRange { lo: [50.0, lo, 0.0], hi: [50.0, hi, 0.0] }.subtract(&[50.0, m, 0.0], fz); - assert!( - (r.lo[1] - want_lo).abs() < 0.03 && (r.hi[1] - want_hi).abs() < 0.03, - "a [{lo}, {hi}] − {m} at {fz}: [{}, {}] vs Photoshop [{want_lo}, {want_hi}]", - r.lo[1], - r.hi[1] - ); - } - // Lightness (box 40–60, fuzziness 40): Photoshop gave 43.14, 41.17, 40.79 and 45.1. - for (m, want) in [(40.0, 43.14), (34.902, 41.17), (25.098, 40.79), (45.098, 45.1)] { - let r = LabRange { lo: [40.0, 0.0, 0.0], hi: [60.0, 0.0, 0.0] }.subtract(&[m, 0.0, 0.0], 40.0); - assert!((r.lo[0] - want).abs() < 0.05, "L − {m}: {} vs Photoshop {want}", r.lo[0]); - } - // All three axes at once (Photoshop: two samples, then one colour outside the box). - let r = LabRange { lo: [53.58, 0.0, 0.0], hi: [59.05, 30.56, 25.03] }.subtract(&[53.33, -28.71, 36.45], 40.0); - let want = LabRange { lo: [56.47, 0.0, 0.0], hi: [59.05, 30.56, 23.05] }; - for k in 0..3 { - assert!((r.lo[k] - want.lo[k]).abs() < 0.05 && (r.hi[k] - want.hi[k]).abs() < 0.05, "{r:?} vs {want:?}"); - } - // A single sample can't be subtracted from itself; hostile numbers change nothing. - let p = LabRange::point([50.0, 10.0, 10.0]); - assert_eq!(p.subtract(&[50.0, 10.0, 10.0], 40.0), p); - assert_eq!(p.subtract(&[f32::NAN, f32::INFINITY, -f32::INFINITY], f32::NAN), p); - assert_eq!(LabRange::spanning(&[[50.0, 0.0, 0.0]]).unwrap().grow(&[60.0, -5.0, 3.0]), LabRange { lo: [50.0, -5.0, 0.0], hi: [60.0, 0.0, 3.0] }); - } - - #[test] - fn eight_bit_grid_steps_are_uneven_like_photoshop() { - let r = LabRange::point([50.0, 10.0, -20.0]); - // The sampled colour itself is fully selected at any fuzziness. - for fz in [1.0, 5.0, 10.0, 40.0] { - assert_eq!(lab_range_coverage_8bit([50.0, 10.0, -20.0], &r, fz), 1.0); - } - // One step above on a counts as 1.5 units, one below as 0.5 (×3 weight): at fuzziness 40 - // (width 40.16) that is t = 4.5/40.16 and 1.5/40.16. - let up = lab_range_coverage_8bit([50.0, 11.0, -20.0], &r, 40.0); - let down = lab_range_coverage_8bit([50.0, 9.0, -20.0], &r, 40.0); - let p = |d: f32| (((1.0 - 2.0 * (d / (40.0 * 256.0 / 255.0)).powi(2)) * 256.0).floor().min(255.0)) / 255.0; - assert_eq!((up, down), (p(4.5), p(1.5))); - assert!(down > up); - // Lightness has no such offset; the exact (non-8-bit) coverage is symmetric. - assert_eq!(lab_range_coverage_8bit([51.0, 10.0, -20.0], &r, 40.0), lab_range_coverage_8bit([49.0, 10.0, -20.0], &r, 40.0)); - assert_eq!(lab_range_coverage([50.0, 11.0, -20.0], &r, 40.0), lab_range_coverage([50.0, 9.0, -20.0], &r, 40.0)); - let px = [[50.0, 11.0, -20.0, 1.0], [50.0, 9.0, -20.0, 1.0]]; - assert_eq!(color_range_lab(&px, 2, &r, 40.0, true, None), vec![up, down]); - } - - #[test] - fn color_range_lab_alpha_and_localized() { - let r = LabRange::point([50.0, 0.0, 0.0]); - let px = img(10, 1, |x, _| [50.0, 0.0, 0.0, if x == 0 { 0.0 } else { 1.0 }]); - let m = color_range_lab(&px, 10, &r, 40.0, false, None); - assert_eq!(m[0], 0.0, "transparent pixels are never selected"); - assert!(m[1..].iter().all(|v| *v > 0.9)); - let at = [(1.0, 0.0)]; - let l = color_range_lab(&px, 10, &r, 40.0, false, Some((4.0, &at))); - assert!(l[1] > l[3] && l[3] > 0.0 && l[6] == 0.0, "{l:?}"); - // No positions: localized clusters have nothing to centre on and change nothing. - assert_eq!(color_range_lab(&px, 10, &r, 40.0, false, Some((4.0, &[]))), m); - assert!(color_range_lab(&[], 0, &r, 40.0, false, None).is_empty()); - } - - #[test] - fn hue_families() { - let px = vec![ - [1.0, 0.0, 0.0, 1.0], // red - [1.0, 0.5, 0.0, 1.0], // orange: between red and yellow - [0.5, 0.5, 0.5, 1.0], // grey - [1.0, 0.5, 0.5, 1.0], // pink: a muted red - [0.0, 0.0, 1.0, 1.0], // blue - [1.0, 0.0, 0.0, 0.0], // transparent - ]; - let reds = hue_range(&px, 0.0); - assert_eq!(reds[0], 1.0); - assert!((reds[1] - 0.5).abs() < 1e-5); - assert_eq!(reds[2], 0.0); - assert!((reds[3] - 0.5).abs() < 1e-5); - assert_eq!(reds[4], 0.0); - assert_eq!(reds[5], 0.0); - let yellows = hue_range(&px, 60.0); - assert!((yellows[1] - 0.5).abs() < 1e-5); - assert_eq!(hue_range(&px, 240.0)[4], 1.0); - // Magentas wrap around 360°. - assert_eq!(hue_range(&[[1.0, 0.0, 1.0, 1.0]], 300.0)[0], 1.0); - assert!((hue_range(&[[1.0, 0.0, 0.5, 1.0]], 300.0)[0] - 0.5).abs() < 1e-5); - } - - #[test] - fn hue_families_weigh_dark_colours_like_photoshop() { - // Photoshop 2025's Blues on an 8-bit sRGB document (selection levels 0..255, #5243): the - // muted navy #3d3f54, a dark blue (0, 0, 84) and a mid blue (61, 61, 153). - let c = |r: u8, g: u8, b: u8| [f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0, 1.0]; - let blues = hue_range(&[c(0x3d, 0x3f, 0x54), c(0, 0, 84), c(61, 61, 153)], 240.0); - for (got, want) in blues.iter().zip([35.0, 140.0, 129.0]) { - assert!((got * 255.0 - want).abs() <= 1.0, "{} vs Photoshop {want}", got * 255.0); - } - } - - #[test] - fn tonal_ranges() { - let g = |v: f32| [v / 255.0, v / 255.0, v / 255.0, 1.0]; - let px = vec![g(0.0), g(60.0), g(75.0), g(128.0), g(200.0), g(255.0)]; - let near = |a: &[f32], b: &[f32]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-4); - // Shadows up to 65, fading over 20 levels. - let sh = tone_range(&px, 0.0, 65.0, 20.0); - assert!(near(&sh, &[1.0, 1.0, 0.5, 0.0, 0.0, 0.0]), "{sh:?}"); - // Highlights from 190. - let h = tone_range(&px, 190.0, 255.0, 20.0); - assert!(near(&h, &[0.0, 0.0, 0.0, 0.0, 1.0, 1.0]), "{h:?}"); - // Midtones 105..150. - let m = tone_range(&px, 105.0, 150.0, 20.0); - assert!(near(&m, &[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]), "{m:?}"); - } - - fn square(w: usize, h: usize) -> Vec { - (0..w * h).map(|i| if (5..15).contains(&(i % w)) && (5..15).contains(&(i / w)) { 1.0 } else { 0.0 }).collect() - } - - #[test] - fn expand_contract_border() { - let m = square(20, 20); - let e = expand(&m, 20, 20, 2.0); - assert_eq!(e[10 * 20 + 3], 1.0); - assert_eq!(e[10 * 20 + 2], 0.0); - let c = contract(&m, 20, 20, 2.0); - assert_eq!(c[10 * 20 + 6], 0.0); - assert_eq!(c[10 * 20 + 7], 1.0); - let b = border(&m, 20, 20, 4.0); - assert_eq!(b[10 * 20 + 10], 0.0); - assert!(b[10 * 20 + 5] > 0.5 && b[10 * 20 + 4] > 0.5); - } - - /// Photoshop 27.11, measured: a 32×32 rectangle at (16,16) in 64×64, Select › Modify › - /// Border 4 px, filled white. Row y = 32 for x = 8..=24, and the diagonal (16,16)..(24,24). - #[test] - fn border_matches_photoshop() { - let (w, h) = (64, 64); - let m: Vec = (0..w * h).map(|i| if (16..48).contains(&(i % w)) && (16..48).contains(&(i / w)) { 1.0 } else { 0.0 }).collect(); - let b = border(&m, w, h, 4.0); - let v = |x: usize, y: usize| (b[y * w + x] * 255.0).round() as i32; - let row: Vec = (8..=24).map(|x| v(x, 32)).collect(); - let diag: Vec = (16..=24).map(|d| v(d, d)).collect(); - let near = |got: &[i32], want: &[i32]| got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1); - let want_row = [0, 0, 0, 0, 63, 127, 191, 255, 255, 191, 127, 63, 0, 0, 0, 0, 0]; - assert!(near(&row, &want_row), "{row:?}"); - assert!(near(&diag, &[255, 191, 127, 63, 0, 0, 0, 0, 0]), "{diag:?}"); - } - - #[test] - fn smooth_removes_specks_and_feather_softens() { - let mut m = square(20, 20); - m[2 * 20 + 2] = 1.0; - let s = smooth(&m, 20, 20, 1.0); - assert_eq!(s[2 * 20 + 2], 0.0); - assert_eq!(s[10 * 20 + 10], 1.0); - // Radius 2 (σ 2) keeps the blur inside the 20×20 canvas, so no coverage is lost. - let f = feather(&square(20, 20), 20, 20, 2.0); - let edge = f[10 * 20 + 5]; - assert!(edge > 0.3 && edge < 0.8, "{edge}"); - let sum: f32 = f.iter().sum(); - assert!((sum - 100.0).abs() < 1.0); - } - - #[test] - fn polygon_area_and_aa() { - let a = Rect::new(0, 0, 20, 20); - let sq = polygon(&[(2.0, 2.0), (12.0, 2.0), (12.0, 12.0), (2.0, 12.0)], a, true); - let area: f32 = sq.iter().sum(); - assert!((area - 100.0).abs() < 1e-3, "{area}"); - let tri = polygon(&[(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)], a, true); - let area: f32 = tri.iter().sum(); - assert!((area - 50.0).abs() < 1.0, "{area}"); - assert!(tri.iter().any(|v| *v > 0.0 && *v < 1.0)); - let hard = polygon(&[(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)], a, false); - assert!(hard.iter().all(|v| *v == 0.0 || *v == 1.0)); - assert!(polygon(&[(0.0, 0.0), (1.0, 1.0)], a, true).iter().all(|v| *v == 0.0)); - } - - /// The previous polygon fill (every edge tested on every sub-row), kept as the oracle. - fn old_polygon(points: &[(f32, f32)], area: Rect, anti_alias: bool) -> Vec { - let (w, h) = (area.width() as usize, area.height() as usize); - let mut m = vec![0.0f32; w * h]; - if points.len() < 3 { - return m; - } - let sub = if anti_alias { 4 } else { 1 }; - let mut xs: Vec = Vec::new(); - for py in 0..h { - for s in 0..sub { - let y = area.y0 as f32 + py as f32 + (s as f32 + 0.5) / sub as f32; - xs.clear(); - for i in 0..points.len() { - let (a, b) = (points[i], points[(i + 1) % points.len()]); - if (a.1 <= y && b.1 > y) || (b.1 <= y && a.1 > y) { - xs.push(a.0 + (y - a.1) / (b.1 - a.1) * (b.0 - a.0)); - } - } - xs.sort_by(|a, b| a.total_cmp(b)); - for span in xs.as_chunks::<2>().0 { - let (x0, x1) = ((span[0] - area.x0 as f32).max(0.0), (span[1] - area.x0 as f32).min(w as f32)); - if x1 <= x0 { - continue; - } - let (c0, c1) = (x0.floor() as usize, (x1.ceil() as usize).min(w)); - for cx in c0..c1 { - let cov = if anti_alias { - ((cx + 1) as f32).min(x1) - (cx as f32).max(x0) - } else { - let c = cx as f32 + 0.5; - if c >= x0 && c < x1 { 1.0 } else { 0.0 } - }; - m[py * w + cx] += cov.max(0.0) / sub as f32; - } - } - } - } - for v in &mut m { - *v = v.min(1.0); - } - m - } - - #[test] - fn polygon_matches_the_every_edge_fill_bit_for_bit() { - let mut seed = 0x2545_f491_u32; - let mut rnd = move || { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - seed as f32 / u32::MAX as f32 - }; - // Taller than one 64-row band, offset, and a 1-pixel-wide area. - let areas = [Rect::new(0, 0, 90, 150), Rect::new(-13, 7, 60, 211), Rect::new(5, -40, 6, 30)]; - let mut cases: Vec> = vec![ - // Self-intersecting star, horizontal edges, repeated points, collinear runs. - (0..11) - .map(|i| { - let t = i as f32 * 4.0 * std::f32::consts::PI / 11.0; - (40.0 + 35.0 * t.cos(), 80.0 + 70.0 * t.sin()) - }) - .collect(), - vec![(10.0, 10.0), (50.0, 10.0), (50.0, 10.0), (50.0, 60.0), (30.0, 60.0), (30.0, 30.0), (10.0, 30.0)], - vec![(0.0, 0.0), (20.0, 0.0), (40.0, 0.0), (40.0, 0.0), (0.0, 0.0)], - // Vertices on the sub-row centres and the pixel edges. - vec![(3.125, 2.375), (70.5, 2.375), (70.875, 140.625), (3.0, 140.0), (35.0, 64.0), (35.0, 64.125)], - // Entirely outside, and straddling every side. - vec![(-500.0, -500.0), (-400.0, -500.0), (-450.0, -300.0)], - vec![(-80.0, -60.0), (300.0, 20.0), (60.0, 400.0)], - // Non-finite coordinates and a negative zero. - vec![(10.0, 10.0), (f32::NAN, 40.0), (60.0, 70.0), (20.0, f32::INFINITY), (-0.0, -0.0), (0.0, 0.0)], - ]; - for n in [3, 7, 40, 400] { - for spread in [1.0, 2.0] { - cases.push((0..n).map(|_| (rnd() * 100.0 * spread - 50.0 * (spread - 1.0), rnd() * 220.0 * spread - 60.0 * spread)).collect()); - // Lasso-like: a random walk snapped to quarter pixels. - let (mut x, mut y) = (45.0f32, 75.0f32); - cases.push( - (0..n) - .map(|_| { - x = ((x + (rnd() - 0.5) * 12.0 * spread) * 4.0).round() / 4.0; - y = ((y + (rnd() - 0.5) * 12.0 * spread) * 4.0).round() / 4.0; - (x, y) - }) - .collect(), - ); - } - } - for pts in &cases { - for &area in &areas { - for aa in [true, false] { - let (new, old) = (polygon(pts, area, aa), old_polygon(pts, area, aa)); - assert_eq!(new.len(), old.len()); - let diff = new.iter().zip(&old).position(|(a, b)| a.to_bits() != b.to_bits()); - assert!(diff.is_none(), "{pts:?} {area:?} aa={aa}: first difference at {diff:?}"); - } - } - } - } - - #[test] - fn combine_modes() { - let a = Rect::new(0, 0, 4, 1); - let old = mask_to_surface(&[1.0, 1.0, 0.0, 0.0], a); - let new = [0.0, 1.0, 1.0, 0.0]; - let v = |s: Option| mask_from_surface(s.as_ref(), a); - assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Replace)), vec![0.0, 1.0, 1.0, 0.0]); - assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Add)), vec![1.0, 1.0, 1.0, 0.0]); - assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Subtract)), vec![1.0, 0.0, 0.0, 0.0]); - assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Intersect)), vec![0.0, 1.0, 0.0, 0.0]); - assert!(combine(Some(&old), &[1.0; 4], a, SelectionMode::Subtract).is_none()); - assert_eq!(SelectionMode::parse("add"), SelectionMode::Add); - } - - /// The previous (per-sample) conversions, kept as the oracle for the tiled fast paths. - fn old_mask_to_surface(m: &[f32], area: Rect) -> Surface { - let mut s = Surface::new(PixelFormat::GRAY8); - if !area.is_empty() { - s.write_region(area, m); - } - s.prune(); - s - } - - fn old_combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMode) -> Option { - let mut prev = Vec::new(); - match old { - Some(s) => s.read_region_into(area, &mut prev), - None => prev = vec![0.0; new.len()], - } - let q: Vec = new.iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() / 255.0).collect(); - let out: Vec = match mode { - SelectionMode::Replace => q, - SelectionMode::Add => prev.iter().zip(&q).map(|(a, b)| a.max(*b)).collect(), - SelectionMode::Subtract => prev.iter().zip(&q).map(|(a, b)| (a - b).max(0.0)).collect(), - SelectionMode::Intersect => prev.iter().zip(&q).map(|(a, b)| a.min(*b)).collect(), - }; - let out: Vec = out.into_iter().map(|v| if v < 0.5 / 255.0 { 0.0 } else { v }).collect(); - if out.iter().all(|v| *v <= 0.0) { - return None; - } - Some(old_mask_to_surface(&out, area)) - } - - #[test] - fn tiled_conversions_match_per_sample_ones() { - let mut rng = crate::photo_util::Rng(7); - let mut u = move || (rng.next() >> 11) as f32 / (1u64 << 53) as f32; - // Off-grid areas spanning several tiles, including negative origins. - // The last one holds whole tiles, which fully selected masks fill (shared tiles). - for area in - [Rect::new(0, 0, 300, 20), Rect::new(-37, 250, 530, 263), Rect::new(255, -3, 258, 600), Rect::new(5, 5, 6, 6), Rect::new(-37, -300, 530, 263)] - { - let n = area.width() as usize * area.height() as usize; - let old_m: Vec = (0..n).map(|i| if i % 97 < 40 { u() } else { 0.0 }).collect(); - let old = old_mask_to_surface(&old_m, area); - let fast = mask_to_surface(&old_m, area); - assert_eq!(fast, old, "{area:?}"); - let mut slow_read = Vec::new(); - old.read_region_into(area, &mut slow_read); - assert_eq!(mask_from_surface(Some(&old), area), slow_read); - for kind in 0..4 { - let new: Vec = (0..n) - .map(|i| match kind { - 0 => u() * 1.2 - 0.1, - 1 => { - if i % 131 < 50 { - 1.0 - } else { - 0.0 - } - } - 2 => 0.001, - _ => f32::from(u8::from(i >= 1000)), - }) - .collect(); - for mode in [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect] { - for prev in [None, Some(&old)] { - assert_eq!(combine(prev, &new, area, mode), old_combine(prev, &new, area, mode), "{area:?} {kind} {mode:?}"); - } - } - } - } - // A short mask buffer reads as zeros instead of panicking. - let a = Rect::new(0, 0, 10, 10); - assert!(combine(None, &[1.0; 5], a, SelectionMode::Replace).is_some()); - assert_eq!(mask_to_surface(&[], a).tile_count(), 0); - assert!(combine(None, &[], Rect::EMPTY, SelectionMode::Replace).is_none()); - } - - fn blobs(w: i32, h: i32) -> Vec<[u8; 4]> { - // Deterministic blotchy image with gradients and hard edges. - (0..w * h) - .map(|i| { - let (x, y) = (i % w, i / w); - let v = ((x * 7 + y * 3) % 23 + if (x / 5 + y / 4) % 3 == 0 { 120 } else { 0 }) as u8; - [v, v / 2, 255 - v, if x % 11 == 0 { 128 } else { 255 }] - }) - .collect() - } - - #[test] - fn magic_wand_rejects_incomplete_and_overflowing_pixel_inputs() { - let area = Rect::new(0, 0, 4, 4); - let short = vec![[255u8, 0, 0, 255]; 4]; - let floats: Vec<[f32; 4]> = short.iter().map(|p| p.map(|v| f32::from(v) / 255.0)).collect(); - assert_eq!(magic_wand(&floats, area, (1, 1), 0.0, true, false), Vec::::new()); - assert_eq!(wand_region(&short, area, (1, 1), 0.0, true, false), None); - - let huge = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); - assert!(magic_wand(&floats, huge, (0, 0), 0.0, false, false).is_empty()); - assert_eq!(wand_region(&short, huge, (0, 0), 0.0, false, false), None); - - let valid = Rect::new(0, 0, 2, 2); - assert_eq!(magic_wand(&floats, valid, (0, 0), 0.0, false, false), vec![1.0; 4]); - assert!(wand_region(&short, valid, (0, 0), 0.0, false, false).is_some()); - } - - #[test] - fn region_coverage_outside_a_short_mask_is_zero() { - let region = Region { bbox: Rect::new(-2, -3, 1, -1), mask: vec![255, 128] }; - assert_eq!(region.at(-2, -3), 1.0); - assert!((region.at(-1, -3) - 128.0 / 255.0).abs() < 1e-6); - assert_eq!(region.at(0, -3), 0.0); - assert_eq!(region.at(-2, -2), 0.0); - assert_eq!(region.at(-3, -3), 0.0); - - let extreme = Region { bbox: Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX), mask: vec![] }; - assert_eq!(extreme.at(i32::MAX - 1, i32::MAX - 1), 0.0); - assert_eq!(extreme.at(i32::MIN, i32::MIN), 0.0); - } - - #[test] - fn wand_region_matches_reference_magic_wand() { - let (w, h) = (37, 29); - let area = Rect::new(3, -2, 3 + w, -2 + h); - let img = blobs(w, h); - let f: Vec<[f32; 4]> = img.iter().map(|p| p.map(|v| v as f32 / 255.0)).collect(); - for (seed, tol, contiguous, aa) in [ - ((10, 5), 10.0, true, true), - ((10, 5), 10.0, true, false), - ((20, 20), 40.0, false, true), - ((4, -2), 0.0, true, true), - ((30, 10), 255.0, true, true), - ] { - let reference = magic_wand(&f, area, seed, tol, contiguous, aa); - let r = wand_region(&img, area, seed, tol, contiguous, aa).unwrap(); - for y in area.y0..area.y1 { - for x in area.x0..area.x1 { - let want = (reference[((y - area.y0) * w + x - area.x0) as usize] * 255.0).round(); - assert!((r.at(x, y) * 255.0 - want).abs() <= 1.0, "{seed:?} {tol} {contiguous} {aa} at {x},{y}: {} vs {want}", r.at(x, y) * 255.0); - } - } - } - assert!(wand_region(&img, area, (0, 0), 10.0, true, true).is_none()); - } - - #[test] - fn combine_region_matches_combine() { - let area = Rect::new(0, 0, 40, 30); - let mut old = Surface::new(PixelFormat::GRAY8); - old.fill_rect(Rect::new(5, 5, 25, 20), &[1.0]); - old.fill_rect(Rect::new(8, 8, 10, 10), &[0.4]); - let img = blobs(40, 30); - let region = wand_region(&img, area, (12, 12), 30.0, true, true).unwrap(); - let full: Vec = (0..area.height()).flat_map(|y| (0..area.width()).map(move |x| (x, y))).map(|(x, y)| region.at(x as i32, y as i32)).collect(); - for mode in [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect] { - let a = combine(Some(&old), &full, area, mode); - let b = combine_region(Some(&old), Some(®ion), mode); - assert_eq!(a.is_some(), b.is_some(), "{mode:?}"); - if let (Some(a), Some(b)) = (a, b) { - for y in 0..30 { - for x in 0..40 { - assert!((a.pixel(x, y)[0] - b.pixel(x, y)[0]).abs() < 1.5 / 255.0, "{mode:?} {x},{y}"); - } - } - } - } - assert!(combine_region(None, None, SelectionMode::Subtract).is_none()); - } -} +mod tests; diff --git a/crates/algo/src/selection/combine_mask.rs b/crates/algo/src/selection/combine_mask.rs new file mode 100644 index 0000000000..bbe4c65738 --- /dev/null +++ b/crates/algo/src/selection/combine_mask.rs @@ -0,0 +1,112 @@ +//! Float mask inputs meet stored GRAY8 selections at the tile boundary. No dense copy of +//! the previous selection or of the combined result is needed. + +use super::{SelectionMode, combine, coverage8}; +use crate::photo_util::par_map; +use photocraft_color::PixelFormat; +use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; +use photocraft_raster::{Surface, Tile}; +use std::sync::Arc; + +/// Combines a mask covering only `band` (zero elsewhere). Add and Subtract retain the old +/// selection outside it, including pixels sharing a tile with the band's edges. Replace +/// and Intersect return only the selection inside the band. Missing mask samples read zero. +pub fn combine_band(old: Option<&Surface>, mask: &[f32], band: Rect, mode: SelectionMode) -> Option { + let keep = matches!(mode, SelectionMode::Add | SelectionMode::Subtract); + if !keep || old.is_none() { + return combine(old, mask, band, mode); + } + if band.is_empty() { + return old.cloned(); + } + if old.is_none_or(|s| s.format() == PixelFormat::GRAY8) { + return gray8(old, mask, band, mode, true); + } + // Preserve the format and conversion behavior of unusual selection surfaces. The normal + // GRAY8 path above never materializes these band-sized buffers. + let inside = combine(old, mask, band, mode); + let mut out = old.cloned()?; + let values = inside.map_or_else(|| vec![0.0; band.width() as usize * band.height() as usize], |s| s.read_region(band)); + if out.channels() == 1 { + out.write_region(band, &values); + } else { + // A selection uses its first channel as coverage. Broadcast it when writing back to + // an unusual multichannel surface, rather than passing a short buffer to write_region. + let pixels: Vec = values.iter().flat_map(|v| std::iter::repeat_n(*v, out.channels())).collect(); + out.write_region(band, &pixels); + } + out.prune(); + (out.tile_count() > 0).then_some(out) +} + +pub(super) fn gray8(old: Option<&Surface>, mask: &[f32], area: Rect, mode: SelectionMode, keep_outside: bool) -> Option { + build(old, area, mode, keep_outside, |i| coverage8(mask.get(i).copied().unwrap_or(0.0))) +} + +pub(super) fn gray8_bytes(old: Option<&Surface>, mask: &[u8], area: Rect, mode: SelectionMode) -> Option { + let keep = matches!(mode, SelectionMode::Add | SelectionMode::Subtract); + if area.is_empty() { + return if keep { old.cloned().filter(|s| !s.content_bounds().is_empty()) } else { None }; + } + build(old, area, mode, keep, |i| mask.get(i).copied().unwrap_or(0)) +} + +fn build(old: Option<&Surface>, area: Rect, mode: SelectionMode, keep_outside: bool, sample: impl Fn(usize) -> u8 + Sync) -> Option { + let mut out = if keep_outside { old.cloned().unwrap_or_else(|| Surface::new(PixelFormat::GRAY8)) } else { Surface::new(PixelFormat::GRAY8) }; + let default = old.and_then(|s| s.default_bytes().first()).copied().unwrap_or(0); + let out_default = out.default_bytes().first().copied().unwrap_or(0); + let (blank, full) = (out.solid_tile(&[0.0]), out.solid_tile(&[1.0])); + let untouched = out.solid_tile(&[f32::from(out_default) / 255.0]); + let coords: Vec = area.tiles().collect(); + let width = area.width() as usize; + let ts = TILE_SIZE as usize; + let built = par_map(coords.len(), |k| { + let tc = *coords.get(k)?; + let rect = tc.rect(); + let cut = rect.intersect(&area); + let previous = old.and_then(|s| s.tile(tc)); + let mut tile = Tile::clone(if keep_outside { previous.unwrap_or(&untouched) } else { &blank }); + for y in cut.y0..cut.y1 { + let offset = y.abs_diff(rect.y0) as usize * ts + cut.x0.abs_diff(rect.x0) as usize; + let len = cut.width() as usize; + let Some(dst) = tile.bytes_mut().get_mut(offset..offset + len) else { continue }; + let src = previous.and_then(|t| t.bytes().get(offset..offset + len)); + let start = (y.abs_diff(area.y0) as usize).checked_mul(width).and_then(|i| i.checked_add(cut.x0.abs_diff(area.x0) as usize)); + for (i, d) in dst.iter_mut().enumerate() { + let new = start.and_then(|j| j.checked_add(i)).map(&sample).unwrap_or(0); + let prev = src.and_then(|s| s.get(i)).copied().unwrap_or(default); + *d = match mode { + SelectionMode::Replace => new, + SelectionMode::Add => prev.max(new), + SelectionMode::Subtract => prev.saturating_sub(new), + SelectionMode::Intersect => prev.min(new), + }; + } + } + let (lo, hi) = tile.bytes().iter().fold((u8::MAX, 0u8), |(lo, hi), b| (lo.min(*b), hi.max(*b))); + let tile = if lo == out_default && hi == out_default { + None + } else if previous.is_some_and(|p| p.bytes() == tile.bytes()) { + previous.cloned() + } else if lo == u8::MAX { + Some(Arc::clone(&full)) + } else { + Some(Arc::new(tile)) + }; + Some((tc, tile)) + }); + for (tc, tile) in built.into_iter().flatten() { + match tile { + Some(tile) => out.put_tiles([(tc, tile)]), + None if keep_outside && out.tile(tc).is_some() => drop(out.take_tiles(tc.rect())), + None => {} + } + } + if keep_outside { + out.prune(); + } + (out.tile_count() > 0).then_some(out) +} + +#[cfg(test)] +mod tests; diff --git a/crates/algo/src/selection/combine_mask/tests.rs b/crates/algo/src/selection/combine_mask/tests.rs new file mode 100644 index 0000000000..5c55d9d90a --- /dev/null +++ b/crates/algo/src/selection/combine_mask/tests.rs @@ -0,0 +1,179 @@ +use super::*; +use crate::selection::{Region, combine_region, mask_from_surface, surface_from_masks}; +use photocraft_color::{ColorMode, SampleType}; + +const MODES: [SelectionMode; 4] = [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect]; + +// The pre-refactor float formulas, including quantization before the binary operation. +fn reference(old: Option<&Surface>, mask: &[f32], area: Rect, mode: SelectionMode) -> Option { + if area.is_empty() { + return None; + } + if mode == SelectionMode::Replace { + let s = surface_from_masks(area, mask, None, |_, v| v); + return (s.tile_count() > 0).then_some(s); + } + let prev = mask_from_surface(old, area); + let s = surface_from_masks(area, mask, Some(&prev), |p, v| { + let q = f32::from(coverage8(v)) / 255.0; + match mode { + SelectionMode::Replace => v, + SelectionMode::Add => p.max(q), + SelectionMode::Subtract => (p - q).max(0.0), + SelectionMode::Intersect => p.min(q), + } + }); + (s.tile_count() > 0).then_some(s) +} + +fn reference_band(old: Option<&Surface>, mask: &[f32], area: Rect, mode: SelectionMode) -> Option { + let inside = reference(old, mask, area, mode); + let Some(old) = old.filter(|_| matches!(mode, SelectionMode::Add | SelectionMode::Subtract)) else { return inside }; + if area.is_empty() { + return Some(old.clone()); + } + let values = inside.map_or_else(|| vec![0.0; area.width() as usize * area.height() as usize], |s| s.read_region(area)); + let mut out = old.clone(); + out.write_region(area, &values); + out.prune(); + (out.tile_count() > 0).then_some(out) +} + +#[test] +fn every_byte_pair_matches_float_combining() { + let area = Rect::new(0, 0, 256, 256); + let prev: Vec = (0..65536).map(|i| (i / 256) as f32 / 255.0).collect(); + let mask: Vec = (0..65536).map(|i| (i % 256) as f32 / 255.0).collect(); + let old = crate::selection::mask_to_surface(&prev, area); + for mode in MODES { + assert_eq!(combine(Some(&old), &mask, area, mode), reference(Some(&old), &mask, area, mode), "{mode:?}"); + } +} + +#[test] +fn cropped_bands_match_dense_writeback_with_defaults_and_short_masks() { + let values = [f32::NAN, f32::NEG_INFINITY, -0.2, 0.0, 0.49 / 255.0, 0.5 / 255.0, 0.51 / 255.0, 0.5, 1.0, 1.2, f32::INFINITY]; + for area in [Rect::new(-259, -2, 259, 3), Rect::new(255, 255, 258, 259), Rect::new(0, 0, 512, 256), Rect::EMPTY] { + let n = area.width() as usize * area.height() as usize; + let mask: Vec = (0..n).map(|i| values[i % values.len()]).collect(); + for default in [0.0, 0.5, 1.0] { + let mut old = Surface::with_default(PixelFormat::GRAY8, &[default]); + old.fill_rect(Rect::new(-300, -20, 300, 270), &[0.75]); + old.write_pixel(1024, 1024, &[1.0]); + old.write_pixel(-257, 0, &[0.0]); + let snapshot = old.clone(); + for prev in [None, Some(&old)] { + for len in [0, n / 2, n] { + for mode in MODES { + let mask = &mask[..len]; + assert_eq!(combine(prev, mask, area, mode), reference(prev, mask, area, mode), "{area:?} {mode:?} {default} {len}"); + assert_eq!(combine_band(prev, mask, area, mode), reference_band(prev, mask, area, mode), "band {area:?} {mode:?} {default} {len}"); + } + } + } + assert_eq!(old, snapshot, "combining must leave undo snapshots unchanged"); + } + } +} + +#[test] +fn unusual_selection_formats_keep_their_float_behavior() { + let area = Rect::new(-2, -1, 3, 2); + for sample in SampleType::ALL { + for color in [ColorMode::Grayscale, ColorMode::Rgb] { + let mut old = Surface::with_default(PixelFormat::new(color, sample, false), &vec![0.123; if color == ColorMode::Rgb { 3 } else { 1 }]); + old.write_pixel(0, 0, &vec![0.754; old.channels()]); + for mode in MODES { + let mask = [0.5; 15]; + assert_eq!(combine(Some(&old), &mask, area, mode), reference(Some(&old), &mask, area, mode)); + if color == ColorMode::Grayscale { + assert_eq!(combine_band(Some(&old), &mask, area, mode), reference_band(Some(&old), &mask, area, mode)); + } else { + let out = combine_band(Some(&old), &mask, area, mode).unwrap(); + let inside = reference(Some(&old), &mask, area, mode).unwrap(); + let expected = inside.pixel(0, 0)[0]; + let encoded = Surface::with_default(out.format(), &vec![expected; out.channels()]); + assert_eq!(out.pixel(0, 0), encoded.pixel(0, 0)); + if matches!(mode, SelectionMode::Add | SelectionMode::Subtract) { + assert_eq!(out.pixel(20, 20), old.pixel(20, 20)); + } + } + } + } + } +} + +#[test] +fn region_bytes_match_float_bands() { + let bbox = Rect::new(-257, 255, 258, 258); + let mask: Vec = (0..bbox.width() * bbox.height()).map(|i| (i % 256) as u8).collect(); + let floats: Vec = mask.iter().map(|b| f32::from(*b) / 255.0).collect(); + let region = Region { bbox, mask }; + let mut old = Surface::new(PixelFormat::GRAY8); + old.fill_rect(Rect::new(-300, 250, 300, 270), &[0.75]); + for prev in [None, Some(&old)] { + for mode in MODES { + assert_eq!(combine_region(prev, Some(®ion), mode), reference_band(prev, &floats, bbox, mode)); + assert_eq!(combine_region(prev, None, mode), reference_band(prev, &[], Rect::EMPTY, mode)); + } + } +} + +#[test] +fn tiles_stay_shared_or_are_pruned() { + let area = Rect::new(0, 0, 768, 256); + let mut old = Surface::new(PixelFormat::GRAY8); + old.fill_rect(area, &[1.0]); + let tc = TileCoord::new(0, 0); + let unchanged = combine(Some(&old), &[], area, SelectionMode::Add).unwrap(); + assert!(Arc::ptr_eq(unchanged.tile(tc).unwrap(), old.tile(tc).unwrap())); + let band = Rect::new(257, 1, 260, 3); + let changed = combine_band(Some(&old), &[1.0; 6], band, SelectionMode::Subtract).unwrap(); + assert!(Arc::ptr_eq(changed.tile(tc).unwrap(), old.tile(tc).unwrap())); + assert_eq!(changed.pixel(256, 1), vec![1.0]); + assert_eq!(changed.pixel(257, 1), vec![0.0]); + assert_eq!(changed.pixel(260, 1), vec![1.0]); + let full = combine(None, &vec![1.0; 768 * 256], area, SelectionMode::Replace).unwrap(); + assert!(Arc::ptr_eq(full.tile(tc).unwrap(), full.tile(TileCoord::new(1, 0)).unwrap())); + assert!(combine_band(Some(&old), &vec![1.0; 768 * 256], area, SelectionMode::Subtract).is_none()); + assert!(combine(None, &[], area, SelectionMode::Replace).is_none()); +} + +/// Run only in release on CI: compares the former implementation, including dense reads and +/// band writeback, against the byte path with identical 36 MP inputs and exact output checks. +#[test] +#[ignore = "36 MP release performance comparison; run on GitHub Actions"] +fn gray8_combining_release_comparison() { + use std::{hint::black_box, time::Instant}; + let area = Rect::new(0, 0, 6000, 6000); + let mut old = Surface::new(PixelFormat::GRAY8); + old.fill_rect(Rect::new(100, 100, 5900, 5900), &[0.75]); + println!("36 MP; median of three release runs including allocation; exact output checks; previous dense mask alone = 144000000 bytes"); + for (case, size) in [("sparse", 50), ("dense", 4000), ("full", 6000)] { + let mut mask = vec![0.0; 6000 * 6000]; + let start = (6000 - size) / 2; + for row in mask.as_chunks_mut::<6000>().0.iter_mut().skip(start).take(size) { + row[start..start + size].fill(0.5); + } + for mode in MODES { + for band in [false, true] { + let baseline = if band { reference_band } else { reference }; + let direct = if band { combine_band } else { combine }; + assert_eq!(direct(Some(&old), &mask, area, mode), baseline(Some(&old), &mask, area, mode)); + let mut medians = [0.0; 2]; + for (median, op) in medians.iter_mut().zip([baseline, direct]) { + let mut runs = [0.0; 3]; + for ms in &mut runs { + let start = Instant::now(); + let out = black_box(op(Some(black_box(&old)), black_box(&mask), area, mode)); + *ms = start.elapsed().as_secs_f64() * 1000.0; + drop(out); + } + runs.sort_by(f64::total_cmp); + *median = runs[1]; + } + println!("{case} {mode:?} band={band}: old={:.2}ms new={:.2}ms speedup={:.2}x exact=true", medians[0], medians[1], medians[0] / medians[1]); + } + } + } +} diff --git a/crates/algo/src/selection/distance.rs b/crates/algo/src/selection/distance.rs index 2dad2d2cbb..73b5b8f2a4 100644 --- a/crates/algo/src/selection/distance.rs +++ b/crates/algo/src/selection/distance.rs @@ -3,9 +3,72 @@ const NO_SEED: u32 = u32::MAX; // Keep the public API's finite result for a mask with no selected pixel. -const EMPTY_DISTANCE: f32 = 1e10; +pub(super) const EMPTY_DISTANCE: f32 = 1e10; const BAND_ROWS: usize = 32; +/// Exact distances within `reach` (at most six pixels), EMPTY_DISTANCE beyond it. +/// Two contiguous vertical sweeps and a short horizontal stencil avoid envelopes, +/// transposes and task-local buffers. One byte per pixel stores capped vertical distances. +pub(super) fn edt_bounded(seeds: &[bool], w: usize, h: usize, reach: f32) -> Vec { + if w == 0 || h == 0 || w.checked_mul(h) != Some(seeds.len()) || !(0.0..=6.0).contains(&reach) { + return Vec::new(); + } + // Select All and empty complements need no sweeps, just as in the general EDT. + if !seeds.iter().any(|seed| *seed) { + return vec![EMPTY_DISTANCE; seeds.len()]; + } + if seeds.iter().all(|seed| *seed) { + return vec![0.0; seeds.len()]; + } + let radius = reach.ceil() as usize; + let cap = radius as u8 + 1; + let mut vertical: Vec = seeds.iter().map(|s| if *s { 0 } else { cap }).collect(); + for y in 1..h { + let (before, after) = vertical.split_at_mut(y * w); + if let (Some(prev), Some(row)) = (before.get((y - 1) * w..), after.get_mut(..w)) { + for (d, p) in row.iter_mut().zip(prev) { + *d = (*d).min(p.saturating_add(1)); + } + } + } + for y in (1..h).rev() { + let (before, after) = vertical.split_at_mut(y * w); + if let (Some(row), Some(next)) = (before.get_mut((y - 1) * w..), after.get(..w)) { + for (d, n) in row.iter_mut().zip(next) { + *d = (*d).min(n.saturating_add(1)); + } + } + } + let mut out = vec![EMPTY_DISTANCE; seeds.len()]; + for_chunks(&mut out, w, |y, dst| { + let Some(row) = vertical.get(y * w..(y + 1) * w) else { return }; + if row.iter().all(|d| *d == cap) { + return; + } + for (x, d) in dst.iter_mut().enumerate() { + if row.get(x) == Some(&0) { + *d = 0.0; + continue; + } + let left = x.saturating_sub(radius); + let right = x.saturating_add(radius).saturating_add(1).min(w); + let Some(span) = row.get(left..right) else { continue }; + let mut best = (radius + 1).pow(2); + for (i, &dy) in span.iter().enumerate() { + best = best.min((left + i).abs_diff(x).pow(2) + usize::from(dy).pow(2)); + if best == 1 { + break; + } + } + let distance = (best as f64).sqrt() as f32; + if distance <= reach { + *d = distance; + } + } + }); + out +} + /// Euclidean distance to the nearest `true` pixel, in row-major order. /// /// Empty or inconsistent dimensions return an empty buffer. A mask with no seeds returns diff --git a/crates/algo/src/selection/inverse.rs b/crates/algo/src/selection/inverse.rs new file mode 100644 index 0000000000..e92cc918d2 --- /dev/null +++ b/crates/algo/src/selection/inverse.rs @@ -0,0 +1,104 @@ +//! Tile-wise selection inversion, with shared solid tiles and no canvas-sized buffers. + +use std::collections::HashMap; +use std::sync::{Arc, OnceLock}; + +use photocraft_color::{PixelFormat, read_sample}; +use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; +use photocraft_raster::{Surface, Tile}; + +use crate::photo_util::par_map; + +use super::coverage8; + +/// Invert coverage within `area`, returning a zero-default GRAY8 selection with nothing +/// outside it. Missing tiles become shared solid tiles; selected solid tiles disappear. +/// Each distinct interior source tile is inverted only once, preserving sharing and snapshots. +/// Selections are GRAY8; other formats use their first channel, quantized to selection coverage. +pub fn inverse_surface(source: &Surface, area: Rect) -> Surface { + let mut out = Surface::new(PixelFormat::GRAY8); + if area.is_empty() { + return out; + } + let (blank, full) = (out.solid_tile(&[0.0]), out.solid_tile(&[1.0])); + let default = coverage8(1.0 - source.default_pixel().first().copied().unwrap_or(0.0)); + let missing = match default { + 0 => None, + 255 => Some(Arc::clone(&full)), + v => Some(out.solid_tile(&[f32::from(v) / 255.0])), + }; + // Solid fills share one payload across many coordinates. Cache by payload, rather than + // scanning/copying the same 64 KiB for every tile. OnceLock allows parallel tile workers. + let cache: HashMap<_, OnceLock>>> = + source.tiles().filter(|(c, _)| area.contains_rect(&c.rect())).map(|(_, t)| (Arc::as_ptr(t) as usize, OnceLock::new())).collect(); + let format = source.format(); + let value = |t: &Tile, i: usize| { + if format == PixelFormat::GRAY8 { + t.bytes().get(i).map_or(default, |b| 255 - b) + } else { + let bpp = format.bytes_per_pixel(); + t.bytes().get(i * bpp..(i + 1) * bpp).map_or(default, |pixel| coverage8(1.0 - read_sample(pixel, format.sample, 0))) + } + }; + let coords: Vec = area.tiles().collect(); + let built = par_map(coords.len(), |k| { + let tc = *coords.get(k)?; + let rect = tc.rect(); + let cut = rect.intersect(&area); + let input = source.tile(tc); + // At i32::MAX the coordinate rectangle is saturated and omits the tile's last + // column/row, so it must still take the clipped path. + if cut == rect && cut.width() == TILE_SIZE as u32 && cut.height() == TILE_SIZE as u32 { + let Some(input) = input else { return missing.clone().map(|t| (tc, t)) }; + let cell = cache.get(&(Arc::as_ptr(input) as usize))?; + let tile = cell.get_or_init(|| { + if format == PixelFormat::GRAY8 { + if input.bytes() == full.bytes() { + return None; + } + if input.bytes() == blank.bytes() { + return Some(Arc::clone(&full)); + } + let mut tile = Tile::clone(input); + for b in tile.bytes_mut() { + *b = 255 - *b; + } + return Some(Arc::new(tile)); + } + let mut tile = Tile::clone(&blank); + for (i, b) in tile.bytes_mut().iter_mut().enumerate() { + *b = value(input, i); + } + canonical(tile, &blank, &full) + }); + return tile.clone().map(|t| (tc, t)); + } + // Boundary tiles start at zero so no inverted coverage leaks past the canvas. + let mut tile = Tile::clone(&blank); + let (x0, x1) = ((cut.x0 - rect.x0) as usize, (cut.x1 - rect.x0) as usize); + for y in (cut.y0 - rect.y0) as usize..(cut.y1 - rect.y0) as usize { + let start = y * TILE_SIZE as usize + x0; + if let Some(row) = tile.bytes_mut().get_mut(start..start + x1 - x0) { + for (i, b) in (start..).zip(row) { + *b = input.map_or(default, |t| value(t, i)); + } + } + } + canonical(tile, &blank, &full).map(|t| (tc, t)) + }); + out.put_tiles(built.into_iter().flatten()); + out +} + +fn canonical(tile: Tile, blank: &Arc, full: &Arc) -> Option> { + if tile.bytes() == blank.bytes() { + None + } else if tile.bytes() == full.bytes() { + Some(Arc::clone(full)) + } else { + Some(Arc::new(tile)) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/algo/src/selection/inverse/tests.rs b/crates/algo/src/selection/inverse/tests.rs new file mode 100644 index 0000000000..2a7d94ab07 --- /dev/null +++ b/crates/algo/src/selection/inverse/tests.rs @@ -0,0 +1,85 @@ +use super::*; +use photocraft_color::{ColorMode, SampleType}; + +fn reference(source: &Surface, area: Rect) -> Surface { + let data: Vec = source.read_region(area).chunks_exact(source.channels()).map(|p| 1.0 - p[0]).collect(); + let mut out = Surface::new(PixelFormat::GRAY8); + out.write_region(area, &data); + out.prune(); + out +} + +#[test] +fn inverse_matches_float_reference_and_clips_boundary_tiles() { + // Negative, misaligned and empty bounds, including stored pixels beyond the canvas. + for area in [Rect::new(-17, -11, 530, 270), Rect::new(0, 0, 512, 256), Rect::new(3, 5, 4, 6), Rect::EMPTY] { + for sample in SampleType::ALL { + for default in [0.0, 0.25, 1.0] { + let fmt = PixelFormat::new(ColorMode::Grayscale, sample, false); + let mut source = Surface::with_default(fmt, &[default]); + source.fill_rect(Rect::new(-100, -100, 260, 120), &[1.0]); + // Every encoded GRAY8 value, including feathered coverage at tile seams. + let values: Vec = (0..256).map(|v| v as f32 / 255.0).collect(); + source.write_region(Rect::new(240, 125, 496, 126), &values); + source.write_pixel(750, 280, &[0.7]); + let got = inverse_surface(&source, area); + let want = reference(&source, area); + let window = Rect::new(-30, -20, 800, 290); + assert_eq!(got.to_interleaved(window), want.to_interleaved(window), "{sample:?} {default} {area:?}"); + assert_eq!(got.default_bytes(), &[0]); + assert_eq!(got.tile_count(), want.tile_count(), "zero tiles must be pruned"); + } + } + } +} + +#[test] +fn inverse_preserves_sparse_storage_and_shared_payloads() { + let area = Rect::new(0, 0, 2048, 1024); + let empty = Surface::new(PixelFormat::GRAY8); + let selected = inverse_surface(&empty, area); + assert_eq!(selected.tile_count(), 32); + let full = selected.tile(TileCoord::new(0, 0)).unwrap(); + assert!(selected.tiles().all(|(_, t)| Arc::ptr_eq(t, full)), "missing tiles share one solid payload"); + let cleared = inverse_surface(&selected, area); + assert_eq!(cleared.tile_count(), 0, "fully selected tiles disappear"); + + let mut mixed = Surface::new(PixelFormat::GRAY8); + mixed.write_pixel(10, 11, &[0.25]); + let payload = mixed.tile(TileCoord::new(0, 0)).unwrap().clone(); + mixed.put_tiles(area.tiles().map(|c| (c, payload.clone()))); + let snapshot = mixed.clone(); + let inverted = inverse_surface(&mixed, area); + let result = inverted.tile(TileCoord::new(0, 0)).unwrap(); + assert!(inverted.tiles().all(|(_, t)| Arc::ptr_eq(t, result)), "shared mixed tiles are inverted once"); + assert!(!Arc::ptr_eq(result, &payload)); + assert_eq!(mixed.to_interleaved(area), snapshot.to_interleaved(area)); + assert_eq!(inverse_surface(&inverted, area).to_interleaved(area), mixed.to_interleaved(area)); +} + +#[test] +fn inverse_of_unpruned_zero_tiles_is_shared_full() { + let mut source = Surface::new(PixelFormat::GRAY8); + let area = Rect::new(0, 0, 768, 256); + source.fill_rect(Rect::new(0, 0, 256, 256), &[0.0]); + source.fill_rect(Rect::new(256, 0, 512, 256), &[1.0]); + let out = inverse_surface(&source, area); + assert_eq!(out.tile_count(), 2); + assert!(out.tile(TileCoord::new(1, 0)).is_none()); + assert!(Arc::ptr_eq(out.tile(TileCoord::new(0, 0)).unwrap(), out.tile(TileCoord::new(2, 0)).unwrap())); +} + +#[test] +fn inverse_clips_at_coordinate_limits_and_uses_first_channel() { + for area in [Rect::new(i32::MAX - 255, 0, i32::MAX, 256), Rect::new(0, i32::MAX - 255, 256, i32::MAX), Rect::new(i32::MIN, -1, i32::MIN + 3, 2)] { + let source = Surface::new(PixelFormat::GRAY8); + let out = inverse_surface(&source, area); + assert_eq!(out.to_interleaved(area), reference(&source, area).to_interleaved(area)); + assert_eq!(out.sample_channel(area.x1, area.y0, 0), 0.0); + assert_eq!(out.sample_channel(area.x0, area.y1, 0), 0.0); + } + let area = Rect::new(0, 0, 513, 260); + let mut source = Surface::with_default(PixelFormat::RGBA32F, &[0.25, 0.5, 0.75, 1.0]); + source.fill_rect(Rect::new(10, 10, 400, 200), &[0.75, 1.0, 0.0, 0.5]); + assert_eq!(inverse_surface(&source, area).to_interleaved(area), reference(&source, area).to_interleaved(area)); +} diff --git a/crates/algo/src/selection/lasso.rs b/crates/algo/src/selection/lasso.rs new file mode 100644 index 0000000000..2471a379b5 --- /dev/null +++ b/crates/algo/src/selection/lasso.rs @@ -0,0 +1,291 @@ +//! Lasso shapes built straight into tiles (#8783): only the tiles an edge crosses are rasterised, +//! and every tile wholly inside the polygon shares one solid tile, so a thin lasso across a huge +//! canvas costs the tiles along its edges, not its bounding box. The bytes are those +//! [`super::polygon`] gives over the canvas. + +use std::collections::BTreeSet; +use std::sync::Arc; + +use photocraft_color::PixelFormat; +use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; +use photocraft_raster::{Surface, Tile}; + +use crate::photo_util::par_map; + +const TS: usize = TILE_SIZE as usize; + +/// Most partly covered tiles one painted shape (a lasso, a brush stroke) may hold: 2²⁸ pixels' +/// worth, 256 MB as GRAY8. A thin shape touches a whole tile wherever it passes, so this, not +/// its area, bounds what painting it allocates. +pub const MAX_PAINTED_TILES: usize = (1 << 28) / (TS * TS); + +/// The part of the segment `a`–`b` inside `r` (Liang–Barsky), or `None` when it misses it. +pub(crate) fn clip_segment(a: (f32, f32), b: (f32, f32), r: Rect) -> Option<((f32, f32), (f32, f32))> { + let (dx, dy) = (b.0 - a.0, b.1 - a.1); + let (mut t0, mut t1) = (0.0f32, 1.0f32); + for (p, q) in [(-dx, a.0 - r.x0 as f32), (dx, r.x1 as f32 - a.0), (-dy, a.1 - r.y0 as f32), (dy, r.y1 as f32 - a.1)] { + if p == 0.0 { + if q < 0.0 { + return None; + } + } else { + let t = q / p; + if p < 0.0 { + t0 = t0.max(t); + } else { + t1 = t1.min(t); + } + } + } + (t0 <= t1 && t0.is_finite() && t1.is_finite()).then_some(((a.0 + dx * t0, a.1 + dy * t0), (a.0 + dx * t1, a.1 + dy * t1))) +} + +/// The pixels the polygon through `points` can touch within `clip` (its bounding box, grown by a +/// pixel, as the Lasso has always rasterised it). +fn polygon_area(points: &[(f32, f32)], clip: Rect) -> Rect { + let (mut x0, mut y0, mut x1, mut y1) = (f32::INFINITY, f32::INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY); + for p in points { + (x0, y0, x1, y1) = (x0.min(p.0), y0.min(p.1), x1.max(p.0), y1.max(p.1)); + } + if points.len() < 3 || !(x0 <= x1 && y0 <= y1) { + return Rect::EMPTY; + } + let clamp = |v: f32| v.clamp(-1e7, 1e7) as i32; + Rect::new(clamp(x0.floor()) - 1, clamp(y0.floor()) - 1, clamp(x1.ceil()) + 1, clamp(y1.ceil()) + 1).intersect(&clip) +} + +/// The tiles of `clip` the edges of the polygon through `points` cross (with a pixel's margin), +/// in order: the only ones whose pixels can differ. `None` when there are more than `limit`. +pub fn polygon_edge_tiles(points: &[(f32, f32)], clip: Rect, limit: usize) -> Option> { + let area = polygon_area(points, clip); + let mut set = BTreeSet::new(); + if area.is_empty() { + return Some(set); + } + let step = TILE_SIZE as f32 / 4.0; + for (&a, &b) in points.iter().zip(points.iter().cycle().skip(1)) { + let Some((a, b)) = clip_segment(a, b, area.inflate(2)) else { continue }; + let pieces = ((b.0 - a.0).hypot(b.1 - a.1) / step).ceil().clamp(1.0, 1e6) as usize; + let at = |k: usize| { + let t = k as f32 / pieces as f32; + (a.0 + (b.0 - a.0) * t, a.1 + (b.1 - a.1) * t) + }; + for k in 0..pieces { + let (p, q) = (at(k), at(k + 1)); + let lo = |u: f32, v: f32| u.min(v).floor() as i32 - 1; + let hi = |u: f32, v: f32| u.max(v).floor() as i32 + 2; + for tc in Rect::new(lo(p.0, q.0), lo(p.1, q.1), hi(p.0, q.0), hi(p.1, q.1)).intersect(&area).tiles() { + set.insert(tc); + if set.len() > limit { + return None; + } + } + } + } + Some(set) +} + +/// One polygon edge, as [`super::polygon`] keeps it. +struct Edge { + top: f32, + bottom: f32, + a: (f32, f32), + b: (f32, f32), +} + +/// The anti-aliased (even-odd) polygon through `points`, cut to `clip`, as a GRAY8 selection: the +/// bytes of [`super::polygon`] over `clip`, quantised. Edge tiles are rasterised, tiles wholly +/// inside share one solid tile. `None` when the edges cross more than [`MAX_PAINTED_TILES`] tiles. +pub fn polygon_surface(points: &[(f32, f32)], clip: Rect) -> Option { + let mut s = Surface::new(PixelFormat::GRAY8); + let area = polygon_area(points, clip); + if area.is_empty() { + return Some(s); + } + let edge_tiles = polygon_edge_tiles(points, clip, MAX_PAINTED_TILES)?; + const SUB: usize = 4; + // Sample rows as `polygon` over `clip` takes them, so the bytes match. + let sample_y = |y: i32, k: usize| clip.y0 as f32 + (y - clip.y0) as f32 + (k as f32 + 0.5) / SUB as f32; + let mut edges: Vec = points + .iter() + .zip(points.iter().cycle().skip(1)) + .filter_map(|(&a, &b)| { + if a.1 < b.1 { + Some(Edge { top: a.1, bottom: b.1, a, b }) + } else if b.1 < a.1 { + Some(Edge { top: b.1, bottom: a.1, a, b }) + } else { + None + } + }) + .collect(); + edges.sort_by(|p, q| p.top.total_cmp(&q.top)); + let crossings = |e: &[&Edge], y: f32, xs: &mut Vec| { + xs.clear(); + xs.extend(e.iter().map(|&&Edge { a, b, .. }| a.0 + (y - a.1) / (b.1 - a.1) * (b.0 - a.0))); + xs.sort_by(f32::total_cmp); + }; + let full = s.solid_tile(&[1.0]); + let blank = s.solid_tile(&[0.0]); + let bands: Vec = Rect::new(area.x0, area.y0, area.x0 + 1, area.y1).tiles().map(|tc| tc.ty).collect(); + let built = par_map(bands.len(), |bi| -> Vec<(TileCoord, Arc)> { + let Some(&ty) = bands.get(bi) else { return Vec::new() }; + let rows = Rect::new(area.x0, ty * TILE_SIZE, area.x1, (ty + 1) * TILE_SIZE).intersect(&area); + // In `x` order (`TileCoord` orders by `tx` first). + let cols: Vec = edge_tiles.iter().filter(|tc| tc.ty == ty).copied().collect(); + let mut out = Vec::new(); + let (first, last) = (sample_y(rows.y0, 0), sample_y(rows.y1 - 1, SUB - 1)); + let reach = edges.partition_point(|e| e.top <= last); + let band_edges = edges.get(..reach).unwrap_or_default(); + let mut xs: Vec = Vec::new(); + // Tiles no edge crosses are wholly in or out: one sample row decides them all. + let y_mid = sample_y(rows.y0 + rows.height() as i32 / 2, 0); + let mid: Vec<&Edge> = band_edges.iter().filter(|e| e.top <= y_mid && e.bottom > y_mid).collect(); + crossings(&mid, y_mid, &mut xs); + for tc in rows.tiles() { + if cols.binary_search(&tc).is_ok() { + continue; + } + let tr = tc.rect().intersect(&rows); + let cx = (tr.x0 + tr.x1) as f32 / 2.0; + if xs.partition_point(|x| *x <= cx) % 2 == 0 { + continue; + } + if tr == tc.rect() { + out.push((tc, Arc::clone(&full))); + } else { + let mut tile = Tile::clone(&blank); + fill(&mut tile, tc, tr, u8::MAX); + out.push((tc, Arc::new(tile))); + } + } + if cols.is_empty() { + return out; + } + // The edge tiles: `polygon`'s scanline, accumulated only over their columns. + let x_of = |tc: &TileCoord| tc.rect().intersect(&rows); + let spans: Vec = cols.iter().map(x_of).collect(); + let mut acc = vec![0.0f32; cols.len() * TS]; + let mut tiles: Vec = cols.iter().map(|_| Tile::clone(&blank)).collect(); + let mut pending = band_edges.iter().filter(|e| e.bottom > first).peekable(); + let mut active: Vec<&Edge> = Vec::new(); + let origin = clip.x0 as f32; + for y in rows.y0..rows.y1 { + acc.fill(0.0); + for k in 0..SUB { + let sy = sample_y(y, k); + while let Some(e) = pending.next_if(|e| e.top <= sy) { + active.push(e); + } + active.retain(|e| e.bottom > sy); + if active.is_empty() { + continue; + } + crossings(&active, sy, &mut xs); + for pair in xs.as_chunks::<2>().0 { + let (x0, x1) = ((pair[0] - origin).max(0.0), (pair[1] - origin).min(clip.width() as f32)); + if x1 <= x0 { + continue; + } + let (c0, c1) = (clip.x0 + x0.floor() as i32, clip.x0 + x1.ceil() as i32); + let start = spans.partition_point(|r| r.x1 <= c0); + for (ci, r) in spans.iter().enumerate().skip(start).take_while(|(_, r)| r.x0 < c1) { + for x in c0.max(r.x0)..c1.min(r.x1) { + let cx = (x - clip.x0) as f32; + let cov = (cx + 1.0).min(x1) - cx.max(x0); + if let Some(v) = acc.get_mut(ci * TS + (x - r.x0) as usize) { + *v += cov.max(0.0) / SUB as f32; + } + } + } + } + } + for ((r, tile), row) in spans.iter().zip(&mut tiles).zip(acc.as_chunks::().0) { + let base = y.rem_euclid(TILE_SIZE) as usize * TS + r.x0.rem_euclid(TILE_SIZE) as usize; + let Some(dst) = tile.bytes_mut().get_mut(base..base + r.width() as usize) else { continue }; + for (d, v) in dst.iter_mut().zip(row) { + *d = (v.min(1.0).clamp(0.0, 1.0) * 255.0).round() as u8; + } + } + } + for (tc, tile) in cols.into_iter().zip(tiles) { + let (lo, hi) = tile.bytes().iter().fold((u8::MAX, 0u8), |(lo, hi), v| (lo.min(*v), hi.max(*v))); + if hi > 0 { + out.push((tc, if lo == u8::MAX { Arc::clone(&full) } else { Arc::new(tile) })); + } + } + out + }); + s.put_tiles(built.into_iter().flatten()); + Some(s) +} + +/// Sets `tr` (within tile `tc`) to `v`. +fn fill(tile: &mut Tile, tc: TileCoord, tr: Rect, v: u8) { + let o = tc.rect(); + for y in tr.y0..tr.y1 { + let base = (y - o.y0) as usize * TS; + if let Some(row) = tile.bytes_mut().get_mut(base + (tr.x0 - o.x0) as usize..base + (tr.x1 - o.x0) as usize) { + row.fill(v); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The dense rasteriser over `clip`, quantised as the Lasso stores it. + fn dense(points: &[(f32, f32)], clip: Rect) -> Surface { + let m: Vec = super::super::polygon(points, clip, true).iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() as u8).collect(); + let mut s = Surface::from_interleaved(PixelFormat::GRAY8, clip, &m); + s.prune(); + s + } + + #[test] + fn matches_the_dense_polygon() { + let clip = Rect::new(-40, 10, 1300, 900); + let shapes: [&[(f32, f32)]; 6] = [ + &[(10.5, 20.25), (1200.0, 40.0), (600.0, 880.7)], + // Whole tiles inside, edges exactly on tile boundaries. + &[(0.0, 256.0), (768.0, 256.0), (768.0, 768.0), (0.0, 768.0)], + // Self-intersecting (even-odd), past the clip. + &[(-300.0, -50.0), (1500.0, 950.0), (1500.0, -50.0), (-300.0, 950.0)], + &[(100.0, 100.0), (101.0, 100.0), (100.5, 700.0)], + &[(5.0, 15.0), (5.0, 15.0), (5.0, 15.0)], + &[(2000.0, 2000.0), (2100.0, 2000.0), (2000.0, 2100.0)], + ]; + for pts in shapes { + let s = polygon_surface(pts, clip).expect("within the tile limit"); + let d = dense(pts, clip); + let area = clip.union(&s.tile_bounds()); + assert!(s.read_region(area) == d.read_region(area), "{pts:?}"); + } + } + + /// A thin sliver across a 100,000 px canvas: only the tiles along it are built (the dense + /// rasteriser allocated its 10-billion-pixel bounding box; the #8783 review). + #[test] + fn a_thin_sliver_across_a_huge_canvas_costs_its_edge_tiles() { + let clip = Rect::new(0, 0, 100_000, 100_000); + let sliver = [(0.0, 0.0), (99_999.0, 99_999.0), (0.0, 1.5)]; + let tiles = polygon_edge_tiles(&sliver, clip, MAX_PAINTED_TILES).expect("within the limit"); + assert!(tiles.len() < 2000, "{}", tiles.len()); + let s = polygon_surface(&sliver, clip).expect("within the limit"); + assert!(s.tile_count() <= tiles.len()); + assert!(s.pixel(50_000, 50_000)[0] > 0.0 && s.pixel(50_000, 40_000)[0] == 0.0); + // A zigzag that crosses more tiles than the limit is refused. + let zigzag: Vec<(f32, f32)> = (0..200).map(|i| (if i % 2 == 0 { 0.0 } else { 99_999.0 }, i as f32 * 500.0)).collect(); + assert!(polygon_edge_tiles(&zigzag, clip, MAX_PAINTED_TILES).is_none()); + assert!(polygon_surface(&zigzag, clip).is_none()); + } + + #[test] + fn a_huge_polygon_shares_one_tile_inside() { + let clip = Rect::new(0, 0, 51_200, 51_200); + let s = polygon_surface(&[(-10.0, -10.0), (60_000.0, -10.0), (60_000.0, 60_000.0), (-10.0, 60_000.0)], clip).expect("no edge tiles"); + let shared = s.tiles().map(|(_, t)| Arc::as_ptr(t)).collect::>(); + assert!(s.tile_count() == 200 * 200 && shared.len() == 1, "{} tiles, {} distinct", s.tile_count(), shared.len()); + } +} diff --git a/crates/algo/src/selection/marquee.rs b/crates/algo/src/selection/marquee.rs index 7dd861164a..c4830ae134 100644 --- a/crates/algo/src/selection/marquee.rs +++ b/crates/algo/src/selection/marquee.rs @@ -236,6 +236,60 @@ pub fn combine_surfaces(a: &Surface, b: &Surface, area: Rect, f: impl Fn(f32, f3 out } +/// [`combine_surfaces`] for an `f` that keeps `a` wherever `b` reads 0 (Add, Subtract), visiting +/// only `b`'s tiles within `area`: the rest of `a` (its other tiles and its untouched value) is +/// kept as it is, so painting a thin stroke across a huge canvas costs the tiles it paints, not +/// the canvas (#8783). Falls back to [`combine_surfaces`] over `area` for other inputs. +pub fn combine_sparse(a: &Surface, b: &Surface, area: Rect, f: impl Fn(f32, f32) -> f32) -> Surface { + let gray = PixelFormat::GRAY8; + let lut: Vec = (0..=u16::MAX).map(|i| encode8(f(decode8((i >> 8) as u8), decode8(i as u8)))).collect(); + let pair = |x: u8, y: u8| lut.get(usize::from(x) << 8 | usize::from(y)).copied().unwrap_or(0); + if a.format() != gray || b.format() != gray || b.default_bytes() != [0] || !(0..=255u8).all(|v| pair(v, 0) == v) { + return combine_surfaces(a, b, area, f); + } + let da = a.default_bytes().first().copied().unwrap_or(0); + let mut out = a.clone(); + let (full, untouched) = (out.solid_tile(&[1.0]), out.solid_tile(&[decode8(da)])); + let coords: Vec = b.tiles().map(|(c, _)| *c).filter(|c| !c.rect().intersect(&area).is_empty()).collect(); + // Each tile: `Some(tile)` to store, `None` when it reads as `a`'s untouched value. + let built = par_map(coords.len(), |k| { + let tc = *coords.get(k)?; + let rect = tc.rect(); + let tr = rect.intersect(&area); + let tb = b.tile(tc)?; + let mut tile = Tile::clone(a.tile(tc).unwrap_or(&untouched)); + let data = tile.bytes_mut(); + let (x0, x1) = ((tr.x0 - rect.x0) as usize, (tr.x1 - rect.x0) as usize); + for ly in (tr.y0 - rect.y0) as usize..(tr.y1 - rect.y0) as usize { + let (s, e) = (ly * TS + x0, ly * TS + x1); + let (Some(dst), Some(src)) = (data.get_mut(s..e), tb.bytes().get(s..e)) else { continue }; + for (d, y) in dst.iter_mut().zip(src) { + *d = pair(*d, *y); + } + } + let (lo, hi) = tile.bytes().iter().fold((u8::MAX, 0u8), |(lo, hi), v| (lo.min(*v), hi.max(*v))); + Some(( + tc, + if lo == da && hi == da { + None + } else if lo == u8::MAX { + Some(Arc::clone(&full)) + } else { + Some(Arc::new(tile)) + }, + )) + }); + for (tc, tile) in built.into_iter().flatten() { + match tile { + Some(t) => out.put_tiles([(tc, t)]), + // `take_tiles` only allocates for a tile that is missing; this one is there. + None if out.tile(tc).is_some() => drop(out.take_tiles(tc.rect())), + None => {} + } + } + out +} + #[cfg(test)] mod tests { use super::*; @@ -311,4 +365,30 @@ mod tests { } } } + + /// Add and Subtract through only the new shape's tiles give the bytes of the whole-area + /// combination, keep the old selection's other tiles shared, and keep an untouched value + /// (a layer mask's reveal-all) outside them. + #[test] + fn sparse_combination_matches_the_whole_area_one() { + let area = Rect::new(0, 0, 1100, 700); + let mut rect = Surface::new(PixelFormat::GRAY8); + rect.fill_rect(Rect::from_xywh(200, 100, 600, 520), &[1.0]); + let ell = old_ellipse(Rect::from_xywh(-60, 250, 700, 500), area, true); + let far = ellipse_surface(Rect::from_xywh(900, 20, 150, 150), area, true); + let ops: [fn(f32, f32) -> f32; 2] = [|a, b| a.max(b), |a, b| (a - b).max(0.0)]; + for (a, b) in [(&rect, &ell), (&ell, &rect), (&ell, &ell), (&rect, &far)] { + for f in ops { + let sparse = combine_sparse(a, b, area, f); + assert!(sparse.read_region(area) == old_combine(a, b, area, f).read_region(area)); + assert!(sparse.tiles().all(|(_, t)| t.bytes().iter().any(|v| *v != 0)), "no empty tiles kept"); + } + } + let kept = combine_sparse(&rect, &far, area, |a, b| a.max(b)); + let tc = TileCoord::new(1, 1); + assert!(Arc::ptr_eq(kept.tile(tc).expect("kept"), rect.tile(tc).expect("there")), "untouched tiles stay shared"); + let reveal = Surface::with_default(PixelFormat::GRAY8, &[1.0]); + let cut = combine_sparse(&reveal, &far, area, |a, b| (a - b).max(0.0)); + assert!(cut.pixel(5000, 5000)[0] == 1.0 && cut.pixel(975, 95)[0] == 0.0); + } } diff --git a/crates/algo/src/selection/morphology.rs b/crates/algo/src/selection/morphology.rs new file mode 100644 index 0000000000..4340ceb26b --- /dev/null +++ b/crates/algo/src/selection/morphology.rs @@ -0,0 +1,101 @@ +//! Radius-limited selection morphology. Only the nonzero bounds plus a halo need distances; +//! the flat public API still requires a canvas-sized output. Canvas edges are not implicit +//! seeds: the engine supplies padding when Apply effect at canvas bounds is requested. + +use super::distance::{EMPTY_DISTANCE, edt, edt_bounded}; + +#[derive(Clone, Copy)] +pub(super) enum Kind { + Expand, + Contract, + Border, +} + +#[derive(Debug, PartialEq)] +struct Window { + x0: usize, + x1: usize, + y0: usize, + y1: usize, +} + +fn window(m: &[f32], w: usize, h: usize, r: f32) -> Option { + let mut b = Window { x0: w, x1: 0, y0: h, y1: 0 }; + for (y, row) in m.chunks_exact(w).enumerate() { + if let Some(x) = row.iter().position(|v| *v != 0.0) { + b.x0 = b.x0.min(x); + b.y0 = b.y0.min(y); + b.y1 = y + 1; + b.x1 = b.x1.max(row.iter().rposition(|v| *v != 0.0).unwrap_or(x) + 1); + } + } + if b.x1 == 0 { + // Preserve the general EDT's finite no-seed sentinel even for library callers + // passing radii far beyond the command's 500 px limit. + return (r >= EMPTY_DISTANCE).then_some(Window { x0: 0, x1: w, y0: 0, y1: h }); + } + // Coverage reaches r + 1 centres; the extra pixel also supplies real outside seeds + // for erosion. Saturating arithmetic handles radii larger than the canvas. + let halo = (r.ceil() as usize).saturating_add(1); + Some(Window { x0: b.x0.saturating_sub(halo), x1: b.x1.saturating_add(halo).min(w), y0: b.y0.saturating_sub(halo), y1: b.y1.saturating_add(halo).min(h) }) +} + +pub(super) fn modify(m: &[f32], w: usize, h: usize, r: f32, kind: Kind) -> Vec { + if w == 0 || h == 0 || w.checked_mul(h) != Some(m.len()) || m.len() > isize::MAX as usize / 4 || !r.is_finite() || r < 0.0 { + return Vec::new(); + } + // The window includes every nonzero sample, including fractional coverage, so all + // pixels beyond it are zero for every operation. No canvas-sized copy is needed. + let mut out = vec![0.0; m.len()]; + if matches!(kind, Kind::Border) && r == 0.0 { + return out; + } + let Some(b) = window(m, w, h, r) else { return out }; + let (cw, ch) = (b.x1 - b.x0, b.y1 - b.y0); + let mut seeds = Vec::with_capacity(cw * ch); + for row in m.chunks_exact(w).skip(b.y0).take(ch) { + if let Some(span) = row.get(b.x0..b.x1) { + seeds.extend(span.iter().map(|v| match kind { + // Do not replace this with v < 0.5: the old complement includes ties, + // including values that round to 0.5 after subtraction. + Kind::Contract => 1.0 - v >= 0.5, + _ => *v >= 0.5, + })); + } + } + // A uniform binary mask has no edge inside the supplied canvas. Preserve the + // finite no-seed sentinel behavior for enormous library radii, but skip both + // distance planes and coverage passes at normal radii (including Select All). + if matches!(kind, Kind::Border) && r < EMPTY_DISTANCE && seeds.first().is_some_and(|first| seeds.iter().all(|seed| seed == first)) { + return out; + } + let distance = |seeds: &[bool]| { + if r <= 5.0 { edt_bounded(seeds, cw, ch, r + 1.0) } else { edt(seeds, cw, ch) } + }; + let passes = if matches!(kind, Kind::Border) { 2 } else { 1 }; + for pass in 0..passes { + // Drop each distance plane before the next pass. Border never keeps two float + // distance planes or two seed masks alive together. + let d = distance(&seeds); + for (y, (src, dst)) in m.chunks_exact(w).zip(out.chunks_exact_mut(w)).skip(b.y0).take(ch).enumerate() { + let (Some(src), Some(dst), Some(dist)) = (src.get(b.x0..b.x1), dst.get_mut(b.x0..b.x1), d.get(y * cw..(y + 1) * cw)) else { continue }; + for ((v, out), d) in src.iter().zip(dst).zip(dist) { + *out = match kind { + Kind::Expand => v.max((r + 1.0 - d).clamp(0.0, 1.0)), + Kind::Contract => 1.0 - (1.0 - v).max((r + 1.0 - d).clamp(0.0, 1.0)), + Kind::Border if (*v >= 0.5) == (pass == 1) => (1.0 - (d - 1.0) / r).clamp(0.0, 1.0), + Kind::Border => *out, + }; + } + } + if pass + 1 < passes { + for seed in &mut seeds { + *seed = !*seed; + } + } + } + out +} + +#[cfg(test)] +mod tests; diff --git a/crates/algo/src/selection/morphology/tests.rs b/crates/algo/src/selection/morphology/tests.rs new file mode 100644 index 0000000000..51db628476 --- /dev/null +++ b/crates/algo/src/selection/morphology/tests.rs @@ -0,0 +1,177 @@ +use super::*; +use crate::selection::{border, contract, expand}; + +fn oracle(m: &[f32], w: usize, h: usize, r: f32, kind: Kind) -> Vec { + let inside: Vec = m.iter().map(|v| *v >= 0.5).collect(); + match kind { + Kind::Expand => m.iter().zip(edt(&inside, w, h)).map(|(v, d)| v.max((r + 1.0 - d).clamp(0.0, 1.0))).collect(), + Kind::Contract => { + let inv: Vec = m.iter().map(|v| 1.0 - v).collect(); + oracle(&inv, w, h, r, Kind::Expand).iter().map(|v| 1.0 - v).collect() + } + Kind::Border => { + if r == 0.0 { + return vec![0.0; m.len()]; + } + let outside: Vec = inside.iter().map(|v| !v).collect(); + let di = edt(&inside, w, h); + let dout = edt(&outside, w, h); + inside.iter().zip(di.iter().zip(&dout)).map(|(sel, (di, dout))| (1.0 - ((if *sel { dout } else { di }) - 1.0) / r).clamp(0.0, 1.0)).collect() + } + } +} + +#[test] +fn matches_full_canvas_formulas() { + for (w, h) in [(1usize, 1usize), (1, 31), (37, 1), (39, 27)] { + for case in 0..9 { + let m: Vec = (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + match case { + 0 => 0.0, + 1 => 1.0, + 2 => { + if x == w / 2 && y == h / 2 { + 1.0 + } else { + 0.0 + } + } + 3 => { + if x < w / 2 && y < h / 2 { + 1.0 + } else { + 0.0 + } + } + 4 => { + if x.abs_diff(w / 2) < 3 && y.abs_diff(h / 2) < 3 { + 1.0 + } else { + 0.0 + } + } + 5 => { + if x.abs_diff(w / 2) < 5 && y.abs_diff(h / 2) < 5 && (x.abs_diff(w / 2) > 2 || y.abs_diff(h / 2) > 2) { + 1.0 + } else { + 0.0 + } + } + 6 => [0.0, 0.1, 0.499_999_97, 0.5, 0.6, 1.0][(i * 31 + i / 7) % 6], + 7 => 0.25, + _ => { + if x == 0 || x + 1 == w || y == 0 || y + 1 == h { + 1.0 + } else { + 0.0 + } + } + } + }) + .collect(); + for r in [0.0, 0.25, 1.0, 1.5, 2.0, 4.25, 5.0, 5.01, 12.0, 100.0] { + for kind in [Kind::Expand, Kind::Contract, Kind::Border] { + assert_eq!(modify(&m, w, h, r, kind), oracle(&m, w, h, r, kind), "{w}x{h} case {case} radius {r}"); + } + } + } + } +} + +#[test] +fn tiny_selection_uses_tiny_distance_window() { + let (w, h) = (1024, 768); + let mut m = vec![0.0; w * h]; + for y in 300..350 { + m[y * w + 400..y * w + 450].fill(1.0); + } + let b = window(&m, w, h, 5.0).unwrap(); + assert_eq!(b, Window { x0: 394, x1: 456, y0: 294, y1: 356 }); + assert_eq!((b.x1 - b.x0) * (b.y1 - b.y0), 62 * 62); +} + +#[test] +fn invalid_dimensions_and_radii_are_safe() { + for op in [expand, contract, border] { + for (w, h) in [(0, 1), (1, 0), (2, 2), (usize::MAX, 2)] { + assert!(op(&[1.0], w, h, 2.0).is_empty()); + } + for r in [f32::NAN, f32::INFINITY, -1.0] { + assert!(op(&[1.0], 1, 1, r).is_empty()); + } + } +} + +#[test] +fn bounded_distances_match_edt_within_reach() { + for (w, h) in [(1, 17), (23, 1), (31, 29)] { + for pattern in 0..4 { + let seeds: Vec = (0..w * h) + .map(|i| match pattern { + 0 => false, + 1 => true, + 2 => i == w * h / 2, + _ => (i * 73 + i / w * 11) % 19 == 0, + }) + .collect(); + let exact = edt(&seeds, w, h); + for reach in [1.0, 1.25, 2.0, 4.5, 6.0] { + let got = edt_bounded(&seeds, w, h, reach); + for (g, e) in got.iter().zip(&exact) { + if *e <= reach { assert_eq!(g, e) } else { assert!(*g > reach) } + } + } + } + } +} + +#[test] +fn every_three_by_three_mask_matches_with_a_cropped_halo() { + let (w, h) = (17, 19); + for bits in 0..512 { + let mut m = vec![0.0; w * h]; + for y in 0..3 { + for x in 0..3 { + if bits & (1 << (y * 3 + x)) != 0 { + m[(y + 7) * w + x + 6] = 1.0; + } + } + } + // Retain faint pixels beyond the binary selection's bounds too. + m[3 * w + 2] = 0.125; + for r in [0.0, 0.414_213_54, 0.414_213_66, 1.5, 3.0, 5.0, 5.5] { + for kind in [Kind::Expand, Kind::Contract, Kind::Border] { + assert_eq!(modify(&m, w, h, r, kind), oracle(&m, w, h, r, kind), "mask {bits} radius {r}"); + } + } + } +} + +#[test] +fn enormous_finite_radii_keep_the_no_seed_sentinel_behavior() { + for m in [vec![0.0; 9], vec![1.0; 9], vec![0.25; 9]] { + for r in [1e10, 2e10, f32::MAX] { + for kind in [Kind::Expand, Kind::Contract, Kind::Border] { + assert_eq!(modify(&m, 3, 3, r, kind), oracle(&m, 3, 3, r, kind)); + } + } + } +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn bounded_and_general_paths_are_worker_count_independent() { + let (w, h) = (400, 300); + let m: Vec = (0..w * h).map(|i| if (i * 73 + i / w * 11) % 19 == 0 { 1.0 } else { 0.0 }).collect(); + for r in [3.0, 9.0] { + for kind in [Kind::Expand, Kind::Contract, Kind::Border] { + let expected = oracle(&m, w, h, r, kind); + for workers in [1, 2, 4] { + let pool = rayon::ThreadPoolBuilder::new().num_threads(workers).build().unwrap(); + assert_eq!(pool.install(|| modify(&m, w, h, r, kind)), expected); + } + } + } +} diff --git a/crates/algo/src/selection/tests.rs b/crates/algo/src/selection/tests.rs new file mode 100644 index 0000000000..226a4c5a68 --- /dev/null +++ b/crates/algo/src/selection/tests.rs @@ -0,0 +1,689 @@ +use super::*; + +fn img(w: usize, h: usize, f: impl Fn(usize, usize) -> [f32; 4]) -> Vec<[f32; 4]> { + (0..h).flat_map(|y| (0..w).map(move |x| (x, y))).map(|(x, y)| f(x, y)).collect() +} + +#[test] +fn wand_contiguous_vs_global() { + // Two separate red squares on white. + let px = img(20, 10, |x, _| if (2..6).contains(&x) || (12..16).contains(&x) { [1.0, 0.0, 0.0, 1.0] } else { [1.0; 4] }); + let a = Rect::new(0, 0, 20, 10); + let c = magic_wand(&px, a, (3, 3), 10.0, true, false); + assert_eq!(c.iter().filter(|v| **v > 0.0).count(), 40); + let g = magic_wand(&px, a, (3, 3), 10.0, false, false); + assert_eq!(g.iter().filter(|v| **v > 0.0).count(), 80); + let aa = magic_wand(&px, a, (3, 3), 10.0, true, true); + assert!(aa[3 * 20 + 6] > 0.0 && aa[3 * 20 + 6] < 1.0); + assert!(magic_wand(&px, a, (50, 3), 10.0, true, false).iter().all(|v| *v == 0.0)); +} + +#[test] +fn wand_tolerance() { + let px = img(10, 1, |x, _| [x as f32 * 10.0 / 255.0, 0.0, 0.0, 1.0]); + let m = magic_wand(&px, Rect::new(0, 0, 10, 1), (0, 0), 25.0, true, false); + assert_eq!(m.iter().filter(|v| **v > 0.0).count(), 3); +} + +#[test] +fn wand_ignores_hidden_rgb_only_when_both_pixels_are_fully_transparent() { + let area = Rect::new(0, 0, 6, 1); + let pixels = [[255, 0, 0, 0], [0, 255, 0, 0], [255, 0, 0, 128], [253, 0, 0, 128], [0, 0, 255, 0], [255, 0, 0, 255]]; + let floats: Vec<_> = pixels.iter().map(|p| p.map(|v| f32::from(v) / 255.0)).collect(); + for (seed, tolerance, contiguous, expected) in [ + ((0, 0), 0.0, true, [1.0, 1.0, 0.0, 0.0, 0.0, 0.0]), + ((0, 0), 0.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]), + ((4, 0), 32.0, false, [1.0, 1.0, 0.0, 0.0, 1.0, 0.0]), + ((2, 0), 0.0, false, [0.0, 0.0, 1.0, 0.0, 0.0, 0.0]), + ((2, 0), 2.0, true, [0.0, 0.0, 1.0, 1.0, 0.0, 0.0]), + ((5, 0), 32.0, false, [0.0, 0.0, 0.0, 0.0, 0.0, 1.0]), + ] { + assert_eq!(magic_wand(&floats, area, seed, tolerance, contiguous, false), expected); + let region = wand_region(&pixels, area, seed, tolerance, contiguous, false).unwrap(); + for (x, value) in expected.into_iter().enumerate() { + assert_eq!(region.at(x as i32, 0), value, "seed={seed:?}, tolerance={tolerance}, x={x}"); + } + } + // Hidden RGB cannot introduce extra edges into the anti-aliased selection either. + let plain: Vec<_> = pixels.iter().map(|p| if p[3] == 0 { [0; 4] } else { *p }).collect(); + for contiguous in [false, true] { + assert_eq!(wand_region(&pixels, area, (0, 0), 0.0, contiguous, true), wand_region(&plain, area, (0, 0), 0.0, contiguous, true)); + } +} + +#[test] +fn skin_tones_selects_skin_and_not_cream_or_white() { + let px = |rgb: [u8; 3]| [rgb[0], rgb[1], rgb[2]].map(|v| f32::from(v) / 255.0); + let rgba = |rgb: [u8; 3]| { + let [r, g, b] = px(rgb); + [r, g, b, 1.0] + }; + // Skin in light and shadow, then a cream shirt, a near-white wall, blue denim and black. + let skin = [[183, 119, 81], [177, 132, 93], [180, 121, 84], [120, 76, 52], [173, 107, 75], [255, 219, 172], [194, 140, 104], [141, 85, 36], [92, 51, 23]] + .map(rgba); + let m = skin_tones(&skin, 40.0); + assert!(m.iter().all(|&v| v > 0.95), "skin {m:?}"); + let other = [[231, 225, 202], [252, 253, 247], [40, 60, 110], [5, 5, 5], [128, 128, 128], [220, 40, 40], [200, 160, 40]].map(rgba); + let m = skin_tones(&other, 40.0); + assert!(m.iter().all(|&v| v < 0.05), "not skin {m:?}"); + // Fuzziness widens the region; transparent pixels are never selected. + let pale = [rgba([230, 215, 200])]; + assert!(skin_tones(&pale, 200.0)[0] > skin_tones(&pale, 0.0)[0]); + assert_eq!(skin_tones(&[[0.7, 0.45, 0.3, 0.0]], 40.0), vec![0.0]); +} + +#[test] +fn color_range_falloff() { + let px = vec![[1.0, 0.0, 0.0, 1.0], [0.9, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0]]; + let m = color_range(&px, [1.0, 0.0, 0.0], 40.0); + assert_eq!(m[0], 1.0); + assert!(m[1] > 0.3 && m[1] < 1.0); + assert_eq!(m[2], 0.0); + assert_eq!(m[3], 0.0); +} + +#[test] +fn color_range_several_samples_and_localized() { + let red = [1.0, 0.0, 0.0, 1.0]; + let blue = [0.0, 0.0, 1.0, 1.0]; + let green = [0.0, 1.0, 0.0, 1.0]; + // 4 × 1: red, blue, green, red. + let px = vec![red, blue, green, red]; + let s = |c: [f32; 4], at: Option<(f32, f32)>| RangeSample { color: [c[0], c[1], c[2]], at }; + let m = color_range_samples(&px, 4, &[s(red, None), s(blue, None)], 10.0, None); + assert_eq!(m, vec![1.0, 1.0, 0.0, 1.0]); + // One sample is exactly the single-colour Color Range. + assert_eq!(color_range_samples(&px, 4, &[s(red, None)], 40.0, None), color_range(&px, [1.0, 0.0, 0.0], 40.0)); + // Localized at x = 0 with a 2 px range: the far red pixel (x = 3) drops out, x = 1 would be half. + let m = color_range_samples(&px, 4, &[s(red, Some((0.0, 0.0)))], 10.0, Some(2.0)); + assert_eq!(m, vec![1.0, 0.0, 0.0, 0.0]); + let reds = vec![red; 4]; + let m = color_range_samples(&reds, 4, &[s(red, Some((0.0, 0.0)))], 10.0, Some(2.0)); + assert_eq!(m, vec![1.0, 0.5, 0.0, 0.0]); + // No samples: nothing selected. + assert!(color_range_samples(&px, 4, &[], 10.0, None).iter().all(|v| *v == 0.0)); +} + +/// Photoshop 2024's own Color Range coverage around the sample Lab (60, 25, 19.9961), read +/// from its selections on a 16-bit Lab document: (Lab in Photoshop's internal scale, where +/// a/b = (v16 − 32768)/256, fuzziness, coverage). +const PHOTOSHOP_COVERAGE: &[([f32; 3], f32, f32)] = &[ + ([61.9989, 25.5977, 20.5742], 40.0, 0.9686), + ([64.9989, 25.5977, 20.5742], 40.0, 0.7961), + ([67.9988, 25.5977, 20.5742], 40.0, 0.4824), + ([71.9997, 25.5977, 20.5742], 40.0, 0.1098), + ([79.9985, 25.5977, 20.5742], 40.0, 0.0), + ([71.9997, 25.5977, 20.5742], 100.0, 0.8118), + ([79.9985, 25.5977, 20.5742], 100.0, 0.4824), + ([60.0, 13.5508, 20.5742], 40.0, 0.0432), + ([60.0, 19.5742, 20.5742], 40.0, 0.6706), + ([60.0, 22.582, 20.5742], 40.0, 0.9333), + ([60.0, 28.6055, 20.5742], 40.0, 0.8549), + ([60.0, 31.6211, 20.5742], 40.0, 0.5098), + ([60.0, 37.6445, 20.5742], 40.0, 0.0078), + ([60.0, 13.5508, 20.5742], 100.0, 0.7647), + ([60.0, 37.6445, 20.5742], 100.0, 0.7137), + ([60.0, 25.5977, 10.5352], 40.0, 0.1725), + ([60.0, 25.5977, 16.5586], 40.0, 0.8667), + ([60.0, 25.5977, 24.5898], 40.0, 0.7647), + ([60.0, 25.5977, 30.6133], 40.0, 0.0863), + ([60.0, 25.5977, 10.5352], 100.0, 0.8392), + ([60.0, 25.5977, 30.6133], 100.0, 0.8), +]; + +#[test] +fn lab_range_matches_photoshop() { + let r = LabRange::point([60.0, 25.0, 19.9961]); + for &(lab, fz, ps) in PHOTOSHOP_COVERAGE { + let got = lab_range_coverage(lab, &r, fz); + assert!((got - ps).abs() <= 2.0 / 255.0, "{lab:?} at fuzziness {fz}: {got} vs Photoshop {ps}"); + } + // Lightness is three times as forgiving as hue: the same 6-unit step along L and along a. + assert!(lab_range_coverage([66.0, 25.0, 20.0], &r, 40.0) > lab_range_coverage([60.0, 31.0, 20.0], &r, 40.0)); + // The sampled colour itself is always fully selected. + for fz in [1.0, 10.0, 40.0] { + assert_eq!(lab_range_coverage([60.0, 25.0, 19.9961], &r, fz), 1.0); + } + assert_eq!(quantize_lab8([60.1, 25.4, -19.6]), [(153.0f32 / 2.55), 25.0, -20.0]); +} + +#[test] +fn lab_range_box_and_edges() { + let r = LabRange::spanning(&[[50.0, 10.0, 0.0], [60.0, 20.0, 5.0]]).unwrap(); + assert_eq!(r, LabRange { lo: [50.0, 10.0, 0.0], hi: [60.0, 20.0, 5.0] }); + assert!(LabRange::spanning(&[]).is_none()); + // Everything inside the box is fully selected, whatever the fuzziness. + for fz in [1.0, 40.0, 200.0] { + assert_eq!(lab_range_coverage([55.0, 15.0, 2.0], &r, fz), 1.0); + } + // Coverage falls off outside it and is gone a fuzziness away. + let near = lab_range_coverage([55.0, 22.0, 2.0], &r, 40.0); + assert!(near > 0.0 && near < 1.0, "{near}"); + assert_eq!(lab_range_coverage([55.0, 40.0, 2.0], &r, 40.0), 0.0); + // Fuzziness 0: exact colours only. + let p = LabRange::point([50.0, 10.0, -5.0]); + assert_eq!(lab_range_coverage([50.0, 10.0, -5.0], &p, 0.0), 1.0); + assert_eq!(lab_range_coverage([50.0, 11.0, -5.0], &p, 0.0), 0.0); + // Hostile numbers never panic and never select. + for v in [f32::NAN, f32::INFINITY, -f32::INFINITY] { + for fz in [v, 40.0] { + let k = lab_range_coverage([v, 0.0, 0.0], &p, fz); + assert!((0.0..=1.0).contains(&k), "{k}"); + } + } +} + +/// Boxes Photoshop 2024's Color Range dialog returned after Subtract from Sample, at +/// fuzziness 40 unless noted: (lo, hi, subtracted value, fuzziness, new lo, new hi) on one axis. +const PHOTOSHOP_SUBTRACT_A: &[(f32, f32, f32, f32, f32, f32)] = &[ + (0.0, 20.0, 5.0, 40.0, 5.0, 20.0), + (0.0, 20.0, 10.0, 40.0, 0.0, 10.0), + (0.0, 20.0, 15.0, 40.0, 0.0, 15.0), + (0.0, 20.0, 0.0, 40.0, 2.68, 20.0), + (0.0, 20.0, -2.0, 40.0, 0.68, 20.0), + (0.0, 20.0, -3.0, 40.0, 2.36, 20.0), + (0.0, 20.0, -8.0, 40.0, 0.04, 20.0), + (0.0, 20.0, -11.0, 40.0, 2.4, 20.0), + (0.0, 20.0, -13.0, 40.0, 0.4, 20.0), + (0.0, 20.0, -14.0, 40.0, 0.0, 20.0), + (0.0, 20.0, 20.0, 40.0, 0.0, 17.32), + (0.0, 20.0, 23.0, 40.0, 0.0, 17.64), + (0.0, 20.0, 33.0, 40.0, 0.0, 19.6), + (0.0, 20.0, 0.0, 80.0, 5.35, 20.0), + (0.0, 20.0, -12.0, 80.0, 4.05, 20.0), + (0.0, 20.0, -22.0, 80.0, 4.76, 20.0), + (0.0, 20.0, -30.0, 80.0, 0.0, 20.0), + (0.0, 6.0, 4.0, 40.0, 0.0, 4.0), + (0.0, 2.0, 0.0, 40.0, 2.0, 2.0), + (0.0, 2.0, 3.0, 40.0, 0.0, 0.32), +]; + +#[test] +fn subtract_reshapes_the_box_like_photoshop() { + for &(lo, hi, m, fz, want_lo, want_hi) in PHOTOSHOP_SUBTRACT_A { + let r = LabRange { lo: [50.0, lo, 0.0], hi: [50.0, hi, 0.0] }.subtract(&[50.0, m, 0.0], fz); + assert!( + (r.lo[1] - want_lo).abs() < 0.03 && (r.hi[1] - want_hi).abs() < 0.03, + "a [{lo}, {hi}] − {m} at {fz}: [{}, {}] vs Photoshop [{want_lo}, {want_hi}]", + r.lo[1], + r.hi[1] + ); + } + // Lightness (box 40–60, fuzziness 40): Photoshop gave 43.14, 41.17, 40.79 and 45.1. + for (m, want) in [(40.0, 43.14), (34.902, 41.17), (25.098, 40.79), (45.098, 45.1)] { + let r = LabRange { lo: [40.0, 0.0, 0.0], hi: [60.0, 0.0, 0.0] }.subtract(&[m, 0.0, 0.0], 40.0); + assert!((r.lo[0] - want).abs() < 0.05, "L − {m}: {} vs Photoshop {want}", r.lo[0]); + } + // All three axes at once (Photoshop: two samples, then one colour outside the box). + let r = LabRange { lo: [53.58, 0.0, 0.0], hi: [59.05, 30.56, 25.03] }.subtract(&[53.33, -28.71, 36.45], 40.0); + let want = LabRange { lo: [56.47, 0.0, 0.0], hi: [59.05, 30.56, 23.05] }; + for k in 0..3 { + assert!((r.lo[k] - want.lo[k]).abs() < 0.05 && (r.hi[k] - want.hi[k]).abs() < 0.05, "{r:?} vs {want:?}"); + } + // A single sample can't be subtracted from itself; hostile numbers change nothing. + let p = LabRange::point([50.0, 10.0, 10.0]); + assert_eq!(p.subtract(&[50.0, 10.0, 10.0], 40.0), p); + assert_eq!(p.subtract(&[f32::NAN, f32::INFINITY, -f32::INFINITY], f32::NAN), p); + assert_eq!(LabRange::spanning(&[[50.0, 0.0, 0.0]]).unwrap().grow(&[60.0, -5.0, 3.0]), LabRange { lo: [50.0, -5.0, 0.0], hi: [60.0, 0.0, 3.0] }); +} + +#[test] +fn eight_bit_grid_steps_are_uneven_like_photoshop() { + let r = LabRange::point([50.0, 10.0, -20.0]); + // The sampled colour itself is fully selected at any fuzziness. + for fz in [1.0, 5.0, 10.0, 40.0] { + assert_eq!(lab_range_coverage_8bit([50.0, 10.0, -20.0], &r, fz), 1.0); + } + // One step above on a counts as 1.5 units, one below as 0.5 (×3 weight): at fuzziness 40 + // (width 40.16) that is t = 4.5/40.16 and 1.5/40.16. + let up = lab_range_coverage_8bit([50.0, 11.0, -20.0], &r, 40.0); + let down = lab_range_coverage_8bit([50.0, 9.0, -20.0], &r, 40.0); + let p = |d: f32| (((1.0 - 2.0 * (d / (40.0 * 256.0 / 255.0)).powi(2)) * 256.0).floor().min(255.0)) / 255.0; + assert_eq!((up, down), (p(4.5), p(1.5))); + assert!(down > up); + // Lightness has no such offset; the exact (non-8-bit) coverage is symmetric. + assert_eq!(lab_range_coverage_8bit([51.0, 10.0, -20.0], &r, 40.0), lab_range_coverage_8bit([49.0, 10.0, -20.0], &r, 40.0)); + assert_eq!(lab_range_coverage([50.0, 11.0, -20.0], &r, 40.0), lab_range_coverage([50.0, 9.0, -20.0], &r, 40.0)); + let px = [[50.0, 11.0, -20.0, 1.0], [50.0, 9.0, -20.0, 1.0]]; + assert_eq!(color_range_lab(&px, 2, &r, 40.0, true, None), vec![up, down]); +} + +#[test] +fn color_range_lab_alpha_and_localized() { + let r = LabRange::point([50.0, 0.0, 0.0]); + let px = img(10, 1, |x, _| [50.0, 0.0, 0.0, if x == 0 { 0.0 } else { 1.0 }]); + let m = color_range_lab(&px, 10, &r, 40.0, false, None); + assert_eq!(m[0], 0.0, "transparent pixels are never selected"); + assert!(m[1..].iter().all(|v| *v > 0.9)); + let at = [(1.0, 0.0)]; + let l = color_range_lab(&px, 10, &r, 40.0, false, Some((4.0, &at))); + assert!(l[1] > l[3] && l[3] > 0.0 && l[6] == 0.0, "{l:?}"); + // No positions: localized clusters have nothing to centre on and change nothing. + assert_eq!(color_range_lab(&px, 10, &r, 40.0, false, Some((4.0, &[]))), m); + assert!(color_range_lab(&[], 0, &r, 40.0, false, None).is_empty()); +} + +#[test] +fn hue_families() { + let px = vec![ + [1.0, 0.0, 0.0, 1.0], // red + [1.0, 0.5, 0.0, 1.0], // orange: between red and yellow + [0.5, 0.5, 0.5, 1.0], // grey + [1.0, 0.5, 0.5, 1.0], // pink: a muted red + [0.0, 0.0, 1.0, 1.0], // blue + [1.0, 0.0, 0.0, 0.0], // transparent + ]; + let reds = hue_range(&px, 0.0); + assert_eq!(reds[0], 1.0); + assert!((reds[1] - 0.5).abs() < 1e-5); + assert_eq!(reds[2], 0.0); + assert!((reds[3] - 0.5).abs() < 1e-5); + assert_eq!(reds[4], 0.0); + assert_eq!(reds[5], 0.0); + let yellows = hue_range(&px, 60.0); + assert!((yellows[1] - 0.5).abs() < 1e-5); + assert_eq!(hue_range(&px, 240.0)[4], 1.0); + // Magentas wrap around 360°. + assert_eq!(hue_range(&[[1.0, 0.0, 1.0, 1.0]], 300.0)[0], 1.0); + assert!((hue_range(&[[1.0, 0.0, 0.5, 1.0]], 300.0)[0] - 0.5).abs() < 1e-5); +} + +#[test] +fn hue_families_weigh_dark_colours_like_photoshop() { + // Photoshop 2025's Blues on an 8-bit sRGB document (selection levels 0..255, #5243): the + // muted navy #3d3f54, a dark blue (0, 0, 84) and a mid blue (61, 61, 153). + let c = |r: u8, g: u8, b: u8| [f32::from(r) / 255.0, f32::from(g) / 255.0, f32::from(b) / 255.0, 1.0]; + let blues = hue_range(&[c(0x3d, 0x3f, 0x54), c(0, 0, 84), c(61, 61, 153)], 240.0); + for (got, want) in blues.iter().zip([35.0, 140.0, 129.0]) { + assert!((got * 255.0 - want).abs() <= 1.0, "{} vs Photoshop {want}", got * 255.0); + } +} + +#[test] +fn tonal_ranges() { + let g = |v: f32| [v / 255.0, v / 255.0, v / 255.0, 1.0]; + let px = vec![g(0.0), g(60.0), g(75.0), g(128.0), g(200.0), g(255.0)]; + let near = |a: &[f32], b: &[f32]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-4); + // Shadows up to 65, fading over 20 levels. + let sh = tone_range(&px, 0.0, 65.0, 20.0); + assert!(near(&sh, &[1.0, 1.0, 0.5, 0.0, 0.0, 0.0]), "{sh:?}"); + // Highlights from 190. + let h = tone_range(&px, 190.0, 255.0, 20.0); + assert!(near(&h, &[0.0, 0.0, 0.0, 0.0, 1.0, 1.0]), "{h:?}"); + // Midtones 105..150. + let m = tone_range(&px, 105.0, 150.0, 20.0); + assert!(near(&m, &[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]), "{m:?}"); +} + +fn square(w: usize, h: usize) -> Vec { + (0..w * h).map(|i| if (5..15).contains(&(i % w)) && (5..15).contains(&(i / w)) { 1.0 } else { 0.0 }).collect() +} + +#[test] +fn expand_contract_border() { + let m = square(20, 20); + let e = expand(&m, 20, 20, 2.0); + assert_eq!(e[10 * 20 + 3], 1.0); + assert_eq!(e[10 * 20 + 2], 0.0); + let c = contract(&m, 20, 20, 2.0); + assert_eq!(c[10 * 20 + 6], 0.0); + assert_eq!(c[10 * 20 + 7], 1.0); + let b = border(&m, 20, 20, 4.0); + assert_eq!(b[10 * 20 + 10], 0.0); + assert!(b[10 * 20 + 5] > 0.5 && b[10 * 20 + 4] > 0.5); +} + +/// Photoshop 27.11, measured: a 32×32 rectangle at (16,16) in 64×64, Select › Modify › +/// Border 4 px, filled white. Row y = 32 for x = 8..=24, and the diagonal (16,16)..(24,24). +#[test] +fn border_matches_photoshop() { + let (w, h) = (64, 64); + let m: Vec = (0..w * h).map(|i| if (16..48).contains(&(i % w)) && (16..48).contains(&(i / w)) { 1.0 } else { 0.0 }).collect(); + let b = border(&m, w, h, 4.0); + let v = |x: usize, y: usize| (b[y * w + x] * 255.0).round() as i32; + let row: Vec = (8..=24).map(|x| v(x, 32)).collect(); + let diag: Vec = (16..=24).map(|d| v(d, d)).collect(); + let near = |got: &[i32], want: &[i32]| got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1); + let want_row = [0, 0, 0, 0, 63, 127, 191, 255, 255, 191, 127, 63, 0, 0, 0, 0, 0]; + assert!(near(&row, &want_row), "{row:?}"); + assert!(near(&diag, &[255, 191, 127, 63, 0, 0, 0, 0, 0]), "{diag:?}"); +} + +#[test] +fn smooth_removes_specks_and_feather_softens() { + let mut m = square(20, 20); + m[2 * 20 + 2] = 1.0; + let s = smooth(&m, 20, 20, 1.0); + assert_eq!(s[2 * 20 + 2], 0.0); + assert_eq!(s[10 * 20 + 10], 1.0); + // Radius 2 (σ 2) keeps the blur inside the 20×20 canvas, so no coverage is lost. + let f = feather(&square(20, 20), 20, 20, 2.0); + let edge = f[10 * 20 + 5]; + assert!(edge > 0.3 && edge < 0.8, "{edge}"); + let sum: f32 = f.iter().sum(); + assert!((sum - 100.0).abs() < 1.0); +} + +#[test] +fn polygon_area_and_aa() { + let a = Rect::new(0, 0, 20, 20); + let sq = polygon(&[(2.0, 2.0), (12.0, 2.0), (12.0, 12.0), (2.0, 12.0)], a, true); + let area: f32 = sq.iter().sum(); + assert!((area - 100.0).abs() < 1e-3, "{area}"); + let tri = polygon(&[(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)], a, true); + let area: f32 = tri.iter().sum(); + assert!((area - 50.0).abs() < 1.0, "{area}"); + assert!(tri.iter().any(|v| *v > 0.0 && *v < 1.0)); + let hard = polygon(&[(0.0, 0.0), (10.0, 0.0), (0.0, 10.0)], a, false); + assert!(hard.iter().all(|v| *v == 0.0 || *v == 1.0)); + assert!(polygon(&[(0.0, 0.0), (1.0, 1.0)], a, true).iter().all(|v| *v == 0.0)); +} + +/// The previous polygon fill (every edge tested on every sub-row), kept as the oracle. +fn old_polygon(points: &[(f32, f32)], area: Rect, anti_alias: bool) -> Vec { + let (w, h) = (area.width() as usize, area.height() as usize); + let mut m = vec![0.0f32; w * h]; + if points.len() < 3 { + return m; + } + let sub = if anti_alias { 4 } else { 1 }; + let mut xs: Vec = Vec::new(); + for py in 0..h { + for s in 0..sub { + let y = area.y0 as f32 + py as f32 + (s as f32 + 0.5) / sub as f32; + xs.clear(); + for i in 0..points.len() { + let (a, b) = (points[i], points[(i + 1) % points.len()]); + if (a.1 <= y && b.1 > y) || (b.1 <= y && a.1 > y) { + xs.push(a.0 + (y - a.1) / (b.1 - a.1) * (b.0 - a.0)); + } + } + xs.sort_by(|a, b| a.total_cmp(b)); + for span in xs.as_chunks::<2>().0 { + let (x0, x1) = ((span[0] - area.x0 as f32).max(0.0), (span[1] - area.x0 as f32).min(w as f32)); + if x1 <= x0 { + continue; + } + let (c0, c1) = (x0.floor() as usize, (x1.ceil() as usize).min(w)); + for cx in c0..c1 { + let cov = if anti_alias { + ((cx + 1) as f32).min(x1) - (cx as f32).max(x0) + } else { + let c = cx as f32 + 0.5; + if c >= x0 && c < x1 { 1.0 } else { 0.0 } + }; + m[py * w + cx] += cov.max(0.0) / sub as f32; + } + } + } + } + for v in &mut m { + *v = v.min(1.0); + } + m +} + +#[test] +fn polygon_matches_the_every_edge_fill_bit_for_bit() { + let mut seed = 0x2545_f491_u32; + let mut rnd = move || { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + seed as f32 / u32::MAX as f32 + }; + // Taller than one 64-row band, offset, and a 1-pixel-wide area. + let areas = [Rect::new(0, 0, 90, 150), Rect::new(-13, 7, 60, 211), Rect::new(5, -40, 6, 30)]; + let mut cases: Vec> = vec![ + // Self-intersecting star, horizontal edges, repeated points, collinear runs. + (0..11) + .map(|i| { + let t = i as f32 * 4.0 * std::f32::consts::PI / 11.0; + (40.0 + 35.0 * t.cos(), 80.0 + 70.0 * t.sin()) + }) + .collect(), + vec![(10.0, 10.0), (50.0, 10.0), (50.0, 10.0), (50.0, 60.0), (30.0, 60.0), (30.0, 30.0), (10.0, 30.0)], + vec![(0.0, 0.0), (20.0, 0.0), (40.0, 0.0), (40.0, 0.0), (0.0, 0.0)], + // Vertices on the sub-row centres and the pixel edges. + vec![(3.125, 2.375), (70.5, 2.375), (70.875, 140.625), (3.0, 140.0), (35.0, 64.0), (35.0, 64.125)], + // Entirely outside, and straddling every side. + vec![(-500.0, -500.0), (-400.0, -500.0), (-450.0, -300.0)], + vec![(-80.0, -60.0), (300.0, 20.0), (60.0, 400.0)], + // Non-finite coordinates and a negative zero. + vec![(10.0, 10.0), (f32::NAN, 40.0), (60.0, 70.0), (20.0, f32::INFINITY), (-0.0, -0.0), (0.0, 0.0)], + ]; + for n in [3, 7, 40, 400] { + for spread in [1.0, 2.0] { + cases.push((0..n).map(|_| (rnd() * 100.0 * spread - 50.0 * (spread - 1.0), rnd() * 220.0 * spread - 60.0 * spread)).collect()); + // Lasso-like: a random walk snapped to quarter pixels. + let (mut x, mut y) = (45.0f32, 75.0f32); + cases.push( + (0..n) + .map(|_| { + x = ((x + (rnd() - 0.5) * 12.0 * spread) * 4.0).round() / 4.0; + y = ((y + (rnd() - 0.5) * 12.0 * spread) * 4.0).round() / 4.0; + (x, y) + }) + .collect(), + ); + } + } + for pts in &cases { + for &area in &areas { + for aa in [true, false] { + let (new, old) = (polygon(pts, area, aa), old_polygon(pts, area, aa)); + assert_eq!(new.len(), old.len()); + let diff = new.iter().zip(&old).position(|(a, b)| a.to_bits() != b.to_bits()); + assert!(diff.is_none(), "{pts:?} {area:?} aa={aa}: first difference at {diff:?}"); + } + } + } +} + +#[test] +fn combine_modes() { + let a = Rect::new(0, 0, 4, 1); + let old = mask_to_surface(&[1.0, 1.0, 0.0, 0.0], a); + let new = [0.0, 1.0, 1.0, 0.0]; + let v = |s: Option| mask_from_surface(s.as_ref(), a); + assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Replace)), vec![0.0, 1.0, 1.0, 0.0]); + assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Add)), vec![1.0, 1.0, 1.0, 0.0]); + assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Subtract)), vec![1.0, 0.0, 0.0, 0.0]); + assert_eq!(v(combine(Some(&old), &new, a, SelectionMode::Intersect)), vec![0.0, 1.0, 0.0, 0.0]); + assert!(combine(Some(&old), &[1.0; 4], a, SelectionMode::Subtract).is_none()); + assert_eq!(SelectionMode::parse("add"), SelectionMode::Add); +} + +/// The previous (per-sample) conversions, kept as the oracle for the tiled fast paths. +fn old_mask_to_surface(m: &[f32], area: Rect) -> Surface { + let mut s = Surface::new(PixelFormat::GRAY8); + if !area.is_empty() { + s.write_region(area, m); + } + s.prune(); + s +} + +fn old_combine(old: Option<&Surface>, new: &[f32], area: Rect, mode: SelectionMode) -> Option { + let mut prev = Vec::new(); + match old { + Some(s) => s.read_region_into(area, &mut prev), + None => prev = vec![0.0; new.len()], + } + let q: Vec = new.iter().map(|v| (v.clamp(0.0, 1.0) * 255.0).round() / 255.0).collect(); + let out: Vec = match mode { + SelectionMode::Replace => q, + SelectionMode::Add => prev.iter().zip(&q).map(|(a, b)| a.max(*b)).collect(), + SelectionMode::Subtract => prev.iter().zip(&q).map(|(a, b)| (a - b).max(0.0)).collect(), + SelectionMode::Intersect => prev.iter().zip(&q).map(|(a, b)| a.min(*b)).collect(), + }; + let out: Vec = out.into_iter().map(|v| if v < 0.5 / 255.0 { 0.0 } else { v }).collect(); + if out.iter().all(|v| *v <= 0.0) { + return None; + } + Some(old_mask_to_surface(&out, area)) +} + +#[test] +fn tiled_conversions_match_per_sample_ones() { + let mut rng = crate::photo_util::Rng(7); + let mut u = move || (rng.next() >> 11) as f32 / (1u64 << 53) as f32; + // Off-grid areas spanning several tiles, including negative origins. + // The last one holds whole tiles, which fully selected masks fill (shared tiles). + for area in [Rect::new(0, 0, 300, 20), Rect::new(-37, 250, 530, 263), Rect::new(255, -3, 258, 600), Rect::new(5, 5, 6, 6), Rect::new(-37, -300, 530, 263)] { + let n = area.width() as usize * area.height() as usize; + let old_m: Vec = (0..n).map(|i| if i % 97 < 40 { u() } else { 0.0 }).collect(); + let old = old_mask_to_surface(&old_m, area); + let fast = mask_to_surface(&old_m, area); + assert_eq!(fast, old, "{area:?}"); + let mut slow_read = Vec::new(); + old.read_region_into(area, &mut slow_read); + assert_eq!(mask_from_surface(Some(&old), area), slow_read); + for kind in 0..4 { + let new: Vec = (0..n) + .map(|i| match kind { + 0 => u() * 1.2 - 0.1, + 1 => { + if i % 131 < 50 { + 1.0 + } else { + 0.0 + } + } + 2 => 0.001, + _ => f32::from(u8::from(i >= 1000)), + }) + .collect(); + for mode in [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect] { + for prev in [None, Some(&old)] { + assert_eq!(combine(prev, &new, area, mode), old_combine(prev, &new, area, mode), "{area:?} {kind} {mode:?}"); + } + } + } + } + // A short mask buffer reads as zeros instead of panicking. + let a = Rect::new(0, 0, 10, 10); + assert!(combine(None, &[1.0; 5], a, SelectionMode::Replace).is_some()); + assert_eq!(mask_to_surface(&[], a).tile_count(), 0); + assert!(combine(None, &[], Rect::EMPTY, SelectionMode::Replace).is_none()); +} + +fn blobs(w: i32, h: i32) -> Vec<[u8; 4]> { + // Deterministic blotchy image with gradients and hard edges. + (0..w * h) + .map(|i| { + let (x, y) = (i % w, i / w); + let v = ((x * 7 + y * 3) % 23 + if (x / 5 + y / 4) % 3 == 0 { 120 } else { 0 }) as u8; + [v, v / 2, 255 - v, if x % 11 == 0 { 128 } else { 255 }] + }) + .collect() +} + +#[test] +fn magic_wand_rejects_incomplete_and_overflowing_pixel_inputs() { + let area = Rect::new(0, 0, 4, 4); + let short = vec![[255u8, 0, 0, 255]; 4]; + let floats: Vec<[f32; 4]> = short.iter().map(|p| p.map(|v| f32::from(v) / 255.0)).collect(); + assert_eq!(magic_wand(&floats, area, (1, 1), 0.0, true, false), Vec::::new()); + assert_eq!(wand_region(&short, area, (1, 1), 0.0, true, false), None); + + let huge = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); + assert!(magic_wand(&floats, huge, (0, 0), 0.0, false, false).is_empty()); + assert_eq!(wand_region(&short, huge, (0, 0), 0.0, false, false), None); + + let valid = Rect::new(0, 0, 2, 2); + assert_eq!(magic_wand(&floats, valid, (0, 0), 0.0, false, false), vec![1.0; 4]); + assert!(wand_region(&short, valid, (0, 0), 0.0, false, false).is_some()); +} + +#[test] +fn region_coverage_outside_a_short_mask_is_zero() { + let region = Region { bbox: Rect::new(-2, -3, 1, -1), mask: vec![255, 128] }; + assert_eq!(region.at(-2, -3), 1.0); + assert!((region.at(-1, -3) - 128.0 / 255.0).abs() < 1e-6); + assert_eq!(region.at(0, -3), 0.0); + assert_eq!(region.at(-2, -2), 0.0); + assert_eq!(region.at(-3, -3), 0.0); + + let extreme = Region { bbox: Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX), mask: vec![] }; + assert_eq!(extreme.at(i32::MAX - 1, i32::MAX - 1), 0.0); + assert_eq!(extreme.at(i32::MIN, i32::MIN), 0.0); +} + +#[test] +fn wand_region_matches_reference_magic_wand() { + let (w, h) = (37, 29); + let area = Rect::new(3, -2, 3 + w, -2 + h); + let img = blobs(w, h); + let f: Vec<[f32; 4]> = img.iter().map(|p| p.map(|v| v as f32 / 255.0)).collect(); + for (seed, tol, contiguous, aa) in + [((10, 5), 10.0, true, true), ((10, 5), 10.0, true, false), ((20, 20), 40.0, false, true), ((4, -2), 0.0, true, true), ((30, 10), 255.0, true, true)] + { + let reference = magic_wand(&f, area, seed, tol, contiguous, aa); + let r = wand_region(&img, area, seed, tol, contiguous, aa).unwrap(); + for y in area.y0..area.y1 { + for x in area.x0..area.x1 { + let want = (reference[((y - area.y0) * w + x - area.x0) as usize] * 255.0).round(); + assert!((r.at(x, y) * 255.0 - want).abs() <= 1.0, "{seed:?} {tol} {contiguous} {aa} at {x},{y}: {} vs {want}", r.at(x, y) * 255.0); + } + } + } + assert!(wand_region(&img, area, (0, 0), 10.0, true, true).is_none()); +} + +#[test] +fn combine_region_matches_combine() { + let area = Rect::new(0, 0, 40, 30); + let mut old = Surface::new(PixelFormat::GRAY8); + old.fill_rect(Rect::new(5, 5, 25, 20), &[1.0]); + old.fill_rect(Rect::new(8, 8, 10, 10), &[0.4]); + let img = blobs(40, 30); + let region = wand_region(&img, area, (12, 12), 30.0, true, true).unwrap(); + let full: Vec = (0..area.height()).flat_map(|y| (0..area.width()).map(move |x| (x, y))).map(|(x, y)| region.at(x as i32, y as i32)).collect(); + for mode in [SelectionMode::Replace, SelectionMode::Add, SelectionMode::Subtract, SelectionMode::Intersect] { + let a = combine(Some(&old), &full, area, mode); + let b = combine_region(Some(&old), Some(®ion), mode); + assert_eq!(a.is_some(), b.is_some(), "{mode:?}"); + if let (Some(a), Some(b)) = (a, b) { + for y in 0..30 { + for x in 0..40 { + assert!((a.pixel(x, y)[0] - b.pixel(x, y)[0]).abs() < 1.5 / 255.0, "{mode:?} {x},{y}"); + } + } + } + } + assert!(combine_region(None, None, SelectionMode::Subtract).is_none()); +} + +#[test] +fn polygon_window_keeps_full_canvas_fractional_coverage() { + for canvas in [Rect::new(0, 0, 110_000, 12), Rect::new(-123, -7, 109_877, 5)] { + let points = [(100_003.12, -2.3), (100_043.71, 2.19), (100_032.39, 9.2), (100_001.52, 4.17)]; + for aa in [false, true] { + let full = polygon(&points, canvas, aa); + for requested in [Rect::new(99_998, -10, 100_048, 15), Rect::new(100_010, -3, 100_030, 6), Rect::EMPTY] { + let area = requested.intersect(&canvas); + let local = polygon_window(&points, canvas, requested, aa); + let mut crop = Vec::new(); + for y in area.y0..area.y1 { + let start = (y - canvas.y0) as usize * canvas.width() as usize + (area.x0 - canvas.x0) as usize; + crop.extend_from_slice(&full[start..start + area.width() as usize]); + } + assert_eq!(local, crop, "canvas={canvas:?}, requested={requested:?}, aa={aa}"); + } + } + } +} + +#[test] +fn feather_leaves_everything_beyond_its_reach_at_zero() { + for radius in [0.05, 0.5, 2.5, 7.0, 40.0] { + let reach = feather_reach(radius); + let w = 3 * reach + 20; + let mut m = vec![0.0; w]; + m[0] = 1.0; + let out = feather(&m, w, 1, radius); + assert!(out[0] > 0.0, "radius {radius}"); + assert!(out[reach + 1..].iter().all(|v| *v == 0.0), "radius {radius}: nonzero past reach {reach}"); + } +} diff --git a/crates/algo/src/selection_blur.rs b/crates/algo/src/selection_blur.rs index 4c64250a68..1262e9b77f 100644 --- a/crates/algo/src/selection_blur.rs +++ b/crates/algo/src/selection_blur.rs @@ -542,13 +542,9 @@ impl CosKernel { } } -/// Select › Modify › Feather: Gaussian blur of the mask with `σ = radius`, the kernel truncated -/// at `⌈3σ⌉` and normalized, zero beyond the canvas. Photoshop's feather radius is the Gaussian's -/// σ (measured on Photoshop 27.11; the same mapping the mask feather uses, fitted on the -/// psd-tools corpus in `photocraft_compose::masks::feather_sigma`). It was `radius / 2`: half -/// as wide as Photoshop's. -pub(crate) fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec { - let n = w.saturating_mul(h); +/// The feather's Gaussian σ for `radius`, capped at [`MAX_SIGMA`]. Below 0.1 the feather leaves +/// the mask as it is. +fn feather_sigma(radius: f32) -> f32 { let sigma = if radius.is_finite() { radius.max(0.0) } else if radius > 0.0 { @@ -556,11 +552,28 @@ pub(crate) fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec { } else { 0.0 }; + sigma.min(MAX_SIGMA) +} + +/// How far [`feather`] spreads, in pixels: its kernel's truncation radius `⌈3σ⌉`, or 0 when it +/// leaves the mask as it is. Samples farther than this from every nonzero sample stay 0. +pub(crate) fn feather_reach(radius: f32) -> usize { + let sigma = feather_sigma(radius); + if sigma < 0.1 { 0 } else { (sigma * 3.0).ceil() as usize } +} + +/// Select › Modify › Feather: Gaussian blur of the mask with `σ = radius`, the kernel truncated +/// at `⌈3σ⌉` and normalized, zero beyond the canvas. Photoshop's feather radius is the Gaussian's +/// σ (measured on Photoshop 27.11; the same mapping the mask feather uses, fitted on the +/// psd-tools corpus in `photocraft_compose::masks::feather_sigma`). It was `radius / 2`: half +/// as wide as Photoshop's. +pub(crate) fn feather(m: &[f32], w: usize, h: usize, radius: f32) -> Vec { + let n = w.saturating_mul(h); + let sigma = feather_sigma(radius); if sigma < 0.1 || m.len() != n || n == 0 { return m.to_vec(); } - let sigma = sigma.min(MAX_SIGMA); - let r = (sigma * 3.0).ceil() as usize; + let r = feather_reach(radius); let Some(b) = nonzero_bounds(m, w, h) else { return m.to_vec(); }; diff --git a/crates/algo/src/separable.rs b/crates/algo/src/separable.rs new file mode 100644 index 0000000000..9ab6220135 --- /dev/null +++ b/crates/algo/src/separable.rs @@ -0,0 +1,191 @@ +//! Separable blurs described as data, so another device (the GPU compositor's layer operators, +//! #5772) runs exactly the blur the CPU runs: the kernel weights or box widths come from here, +//! and [`crate::blur`] builds its own passes from the same description, so the two can't drift. +//! +//! [`OperatorRegion`] is the region rule that goes with it: which source pixels an output area +//! reads, with the edge pixels repeated at the extent as the CPU filters do. + +use photocraft_geom::Rect; + +use crate::{FilterParams, blur}; + +/// A blur made of passes along each axis, horizontal first, as the CPU runs it. +#[derive(Clone, Debug, PartialEq)] +pub enum Separable { + /// One pass of this normalised kernel (odd length, centred) along each axis. + Kernel(Vec), + /// Running-mean box passes of these odd widths, in turn, along each axis. + Boxes(Vec), +} + +impl Separable { + /// The blur `params` runs on the CPU, or `None` for a filter that isn't one (only Gaussian + /// Blur so far). Small radii use the exact kernel, larger ones three box passes. + pub fn of(params: &FilterParams) -> Option { + match *params { + FilterParams::GaussianBlur { radius } => Some(match blur::box_widths(params) { + Some(boxes) => Separable::Boxes(boxes), + None => Separable::Kernel(blur::gaussian_blur_kernel(radius)), + }), + _ => None, + } + } + + /// How far outside an output pixel the blur reads, along each axis. + pub fn reach(&self) -> i32 { + let reach = match self { + Separable::Kernel(k) => k.len() / 2, + Separable::Boxes(boxes) => boxes.iter().fold(0usize, |sum, w| sum.saturating_add(w / 2)), + }; + i32::try_from(reach).unwrap_or(i32::MAX) + } +} + +/// The area a neighbourhood filter on a layer repeats its edge pixels at: the canvas, or past it +/// where the layer has pixels off the canvas, as Photoshop does. +pub fn edge_extent(canvas: Rect, content: Rect) -> Rect { + canvas.union(&content) +} + +/// Which source pixels an output area of a blur reads. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct OperatorRegion { + /// How far the blur reads outside an output pixel ([`Separable::reach`]). + pub reach: i32, + /// Where the source's edge pixels repeat ([`edge_extent`]): nothing outside it is read. + pub extent: Rect, +} + +impl OperatorRegion { + /// The source pixels output `out` reads: `out` grown by the reach, inside the extent. Pixels + /// it needs beyond the extent repeat the edge of this area, so they need no source of their + /// own. Empty when `out` is empty or doesn't come within reach of the extent. + pub fn input(&self, out: Rect) -> Rect { + if out.is_empty() { + return Rect::EMPTY; + } + out.inflate(self.reach.max(0)).intersect(&self.extent) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Halo; + use photocraft_color::{ColorMode, PixelFormat, SampleType}; + use photocraft_raster::{Interrupt, Surface}; + + const RADII: [f32; 11] = [0.01, 0.05, 0.1, 0.3, 0.8, 2.0, 4.0, 4.5, 12.0, 40.0, 250.0]; + + /// The CPU blur's own choice: the exact kernel up to radius 4, boxes beyond; and its halo + /// (how far the CPU grows an output area) covers the reach. + #[test] + fn the_description_is_the_cpus_blur() { + for radius in RADII { + let params = FilterParams::GaussianBlur { radius }; + let blur = Separable::of(¶ms).unwrap(); + match &blur { + Separable::Kernel(k) => { + assert!(radius <= 4.0, "radius {radius}"); + assert_eq!(k, &blur::gaussian_blur_kernel(radius)); + assert_eq!(k.len() % 2, 1); + assert!((k.iter().sum::() - 1.0).abs() < 1e-5); + } + Separable::Boxes(b) => { + assert!(radius > 4.0, "radius {radius}"); + assert_eq!(Some(b.clone()), blur::box_widths(¶ms)); + assert!(b.iter().all(|w| w % 2 == 1)); + } + } + let Halo::Radius(halo) = params.halo() else { panic!("a Gaussian has a radius halo") }; + assert!(blur.reach() <= halo, "radius {radius}: reach {} past the halo {halo}", blur.reach()); + } + assert_eq!(Separable::of(&FilterParams::BoxBlur { radius: 9.0 }), None, "not a Gaussian (yet)"); + assert_eq!(Separable::Boxes(vec![usize::MAX, 3]).reach(), i32::MAX, "saturates"); + } + + /// One opaque pixel in a transparent layer, blurred on the CPU, spreads no further than the + /// reach (beyond it the box passes' running sums leave only float residue, far below a + /// 16-bit level), and the exact kernel spreads exactly that far. + #[test] + fn an_impulse_spreads_no_further_than_the_reach() { + for radius in RADII { + let params = FilterParams::GaussianBlur { radius }; + let reach = Separable::of(¶ms).unwrap().reach(); + let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::F32, true); + let size = 2 * reach + 41; + let canvas = Rect::new(0, 0, size, size); + let mut s = Surface::new(fmt); + let dot = Rect::new(size / 2, size / 2, size / 2 + 1, size / 2 + 1); + s.fill_rect(dot, &[1.0, 1.0, 1.0, 1.0]); + let out = crate::apply_in_with(&s, ¶ms, canvas, canvas, None, canvas, &Interrupt::NONE).unwrap(); + let mut px = Vec::new(); + out.read_region_into(canvas, &mut px); + let touched = px + .as_chunks::<4>() + .0 + .iter() + .enumerate() + // The box passes' running sums leave float residue (~1e-8) after the impulse. + .filter(|(_, p)| p[3] > 1e-6) + .fold(Rect::EMPTY, |r, (i, _)| { + let (x, y) = ((i as i32) % size, (i as i32) / size); + r.union(&Rect::new(x, y, x + 1, y + 1)) + }); + // Nothing past the reach; the exact kernel reaches all of it. (A single pixel's box + // tails are smaller than the running sums' residue, so they aren't checked for that.) + assert!(dot.inflate(reach).contains_rect(&touched), "radius {radius}: {touched:?} past the reach {reach}"); + if let Some(Separable::Kernel(k)) = Separable::of(¶ms) { + // Calibrated subpixel kernels have tails below the visibility threshold. + let center = k.len() / 2; + let visible = k.iter().enumerate().filter(|(_, w)| **w * k[center] > 1e-6).map(|(i, _)| i.abs_diff(center)).max().unwrap_or(0); + assert_eq!(touched, dot.inflate(visible as i32), "radius {radius}"); + } + } + } + + /// Pixels outside [`OperatorRegion::input`] don't change an output area's result, and the + /// edge repeats at the extent: a layer's area next to the extent's edge blurs the same + /// whatever lies beyond it. + #[test] + fn output_depends_only_on_the_input_region() { + let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::F32, true); + let extent = Rect::new(0, 0, 300, 200); + let noise = |seed: u32| { + let mut s = Surface::new(fmt); + for y in 0..200 { + for x in 0..300 { + let h = (x as u32).wrapping_mul(73_856_093) ^ (y as u32).wrapping_mul(19_349_663) ^ seed; + let v = (h % 1000) as f32 / 999.0; + s.fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, 1.0 - v, (h % 7) as f32 / 6.0, (h % 5) as f32 / 4.0]); + } + } + s + }; + let a = noise(1); + for radius in [0.8, 4.0, 12.0] { + let params = FilterParams::GaussianBlur { radius }; + let region = OperatorRegion { reach: Separable::of(¶ms).unwrap().reach(), extent }; + for out in [Rect::new(100, 80, 140, 120), Rect::new(0, 0, 30, 25), Rect::new(270, 170, 300, 200)] { + let input = region.input(out); + assert!(extent.contains_rect(&input) && input.contains_rect(&out)); + // Everything outside the input region replaced by other pixels. + let mut b = noise(2); + let mut inside = Vec::new(); + a.read_region_into(input, &mut inside); + b.write_region(input, &inside); + let blur = |s: &Surface| { + let r = crate::apply_in_with(s, ¶ms, out, extent, None, extent, &Interrupt::NONE).unwrap(); + let mut px = Vec::new(); + r.read_region_into(out, &mut px); + px + }; + assert_eq!(blur(&a), blur(&b), "radius {radius}, {out:?}"); + } + } + assert_eq!(OperatorRegion { reach: 5, extent }.input(Rect::EMPTY), Rect::EMPTY); + assert_eq!(OperatorRegion { reach: 5, extent }.input(Rect::new(400, 400, 410, 410)), Rect::EMPTY, "out of reach of the extent"); + assert_eq!(OperatorRegion { reach: -3, extent }.input(Rect::new(10, 10, 20, 20)), Rect::new(10, 10, 20, 20)); + assert_eq!(edge_extent(extent, Rect::new(-10, 50, 20, 60)), Rect::new(-10, 0, 300, 200)); + } +} diff --git a/crates/algo/src/shape_blur.rs b/crates/algo/src/shape_blur.rs index 26e9ad15bc..5b262afdcb 100644 --- a/crates/algo/src/shape_blur.rs +++ b/crates/algo/src/shape_blur.rs @@ -51,63 +51,14 @@ impl Prepared { if prefix_len > MAX_SAMPLES || output_len > MAX_SAMPLES { return None; } - let mut prefix = Vec::new(); - prefix.try_reserve_exact(prefix_len).ok()?; - prefix.resize(prefix_len, 0.0f64); - if input_row != 0 { - for (s, d) in src.data.chunks_exact(input_row).zip(prefix.chunks_exact_mut(stride)) { - let mut sum = [0.0f64; crate::fxutil::MAXC]; - for (px, dst) in s.chunks_exact(n).zip(d.get_mut(n..)?.chunks_exact_mut(n)) { - let alpha = if ctx.alpha { *px.last()? } else { 1.0 }; - for (c, ((v, sum), dst)) in px.iter().zip(sum.iter_mut()).zip(dst).enumerate() { - // Multiplication stays f32, exactly as premul_window. - let v = if ctx.alpha && c + 1 < n { *v * alpha } else { *v }; - *sum += f64::from(v); - *dst = *sum; - } - } - } - } + let prefix = Prefix::build(src, ctx, n, stride, prefix_len)?; let mut result = Vec::new(); result.try_reserve_exact(output_len).ok()?; let mut acc = Vec::new(); acc.try_reserve_exact(row_len).ok()?; acc.resize(row_len, 0.0f64); - let x = i64::from(out.x0) - i64::from(src.rect.x0); for y in out.y0..out.y1 { - acc.fill(0.0); - for &(dy, a, b) in &self.spans.rows { - let yy = i64::from(y) - i64::from(src.rect.y0) + i64::from(dy); - let Ok(yy) = usize::try_from(yy) else { - continue; - }; - if yy >= h { - continue; - } - let row = prefix.get(yy.checked_mul(stride)?..yy.checked_add(1)?.checked_mul(stride)?)?; - let lo = x + i64::from(a); - let hi = x + i64::from(b) + 1; - let end = out.width() as i64; - if lo >= 0 && hi + end <= w as i64 { - let start_lo = usize::try_from(lo).ok()?.checked_mul(n)?; - let start_hi = usize::try_from(hi).ok()?.checked_mul(n)?; - let low = row.get(start_lo..start_lo.checked_add(row_len)?)?; - let high = row.get(start_hi..start_hi.checked_add(row_len)?)?; - for ((acc, hi), lo) in acc.iter_mut().zip(high).zip(low) { - *acc += hi - lo; - } - } else { - // Direct kernel callers may omit the halo or request pixels outside src. - // Retain the scalar kernel's zero extension and fixed normalization. - for (i, px) in acc.chunks_exact_mut(n).enumerate() { - let low = (lo + i as i64).clamp(0, w as i64) as usize * n; - let high = (hi + i as i64).clamp(0, w as i64) as usize * n; - for ((acc, hi), lo) in px.iter_mut().zip(row.get(high..high + n)?).zip(row.get(low..low + n)?) { - *acc += hi - lo; - } - } - } - } + self.accumulate_row(&prefix, src, out, y, &mut acc)?; for px in acc.chunks_exact(n) { let mut sample = [0.0f32; crate::fxutil::MAXC]; let sample = sample.get_mut(..n)?; @@ -120,4 +71,88 @@ impl Prepared { } Some(result) } + + /// Sum every kernel row's window for output row `y` into `acc`. + fn accumulate_row(&self, prefix: &Prefix, src: &Image, out: Rect, y: i32, acc: &mut [f64]) -> Option<()> { + let x = i64::from(out.x0) - i64::from(src.rect.x0); + acc.fill(0.0); + for &(dy, a, b) in &self.spans.rows { + let yy = i64::from(y) - i64::from(src.rect.y0) + i64::from(dy); + let Ok(yy) = usize::try_from(yy) else { + continue; + }; + if yy >= prefix.h { + continue; + } + let row = prefix.row(yy)?; + prefix.add_window(row, acc, x + i64::from(a), x + i64::from(b) + 1, out.width() as i64)?; + } + Some(()) + } +} + +/// Per-row running channel sums (premultiplied when alpha is present), one +/// extra leading column per row so any window is a difference of two entries. +struct Prefix { + data: Vec, + stride: usize, + n: usize, + w: usize, + h: usize, +} + +impl Prefix { + fn build(src: &Image, ctx: &Ctx, n: usize, stride: usize, len: usize) -> Option { + let w = src.rect.width() as usize; + let h = src.rect.height() as usize; + let input_row = w.checked_mul(n)?; + let mut data = Vec::new(); + data.try_reserve_exact(len).ok()?; + data.resize(len, 0.0f64); + if input_row != 0 { + for (s, d) in src.data.chunks_exact(input_row).zip(data.chunks_exact_mut(stride)) { + let mut sum = [0.0f64; crate::fxutil::MAXC]; + for (px, dst) in s.chunks_exact(n).zip(d.get_mut(n..)?.chunks_exact_mut(n)) { + let alpha = if ctx.alpha { *px.last()? } else { 1.0 }; + for (c, ((v, sum), dst)) in px.iter().zip(sum.iter_mut()).zip(dst).enumerate() { + // Multiplication stays f32, exactly as premul_window. + let v = if ctx.alpha && c + 1 < n { *v * alpha } else { *v }; + *sum += f64::from(v); + *dst = *sum; + } + } + } + } + Some(Self { data, stride, n, w, h }) + } + + fn row(&self, y: usize) -> Option<&[f64]> { + self.data.get(y.checked_mul(self.stride)?..y.checked_add(1)?.checked_mul(self.stride)?) + } + + /// Add the `[lo, hi)` window difference of `row` for `acc.len() / n` output pixels. + fn add_window(&self, row: &[f64], acc: &mut [f64], lo: i64, hi: i64, end: i64) -> Option<()> { + let (n, w) = (self.n, self.w); + let row_len = acc.len(); + if lo >= 0 && hi + end <= w as i64 { + let start_lo = usize::try_from(lo).ok()?.checked_mul(n)?; + let start_hi = usize::try_from(hi).ok()?.checked_mul(n)?; + let low = row.get(start_lo..start_lo.checked_add(row_len)?)?; + let high = row.get(start_hi..start_hi.checked_add(row_len)?)?; + for ((acc, hi), lo) in acc.iter_mut().zip(high).zip(low) { + *acc += hi - lo; + } + } else { + // Direct kernel callers may omit the halo or request pixels outside src. + // Retain the scalar kernel's zero extension and fixed normalization. + for (i, px) in acc.chunks_exact_mut(n).enumerate() { + let low = (lo + i as i64).clamp(0, w as i64) as usize * n; + let high = (hi + i as i64).clamp(0, w as i64) as usize * n; + for ((acc, hi), lo) in px.iter_mut().zip(row.get(high..high + n)?).zip(row.get(low..low + n)?) { + *acc += hi - lo; + } + } + } + Some(()) + } } diff --git a/crates/algo/src/sharpen/exact.rs b/crates/algo/src/sharpen/exact.rs index 7d74bc538f..07ecba08bc 100644 --- a/crates/algo/src/sharpen/exact.rs +++ b/crates/algo/src/sharpen/exact.rs @@ -77,58 +77,94 @@ pub(crate) fn run(src: &Image, out: Rect, ctx: &Ctx, amount: f32, radius: f32, t let row_len = ow.checked_mul(n)?; let th = oh.checked_add(2 * r)?; let ww = ow.checked_add(2 * r)?; - let (x0, x1, y0) = (out.x0.checked_sub(r as i32)?, out.x1.checked_add(r as i32)?, out.y0.checked_sub(r as i32)?); + let pass = Pass { src, out, ctx, kernel: &kernel, ctl, n, row_len }; let mut row = zeros(ww.checked_mul(n)?)?; let mut tmp = zeros(row_len.checked_mul(th)?)?; let mut result = zeros(row_len.checked_mul(oh)?)?; - for (y, dst) in tmp.chunks_exact_mut(row_len).enumerate() { - if ctl.cancelled() { - return None; - } - read_row(src, x0, x1, y0.checked_add(y as i32)?, &mut row)?; - premultiply(&mut row, n, ctx.alpha); - // Tap-major traversal exposes contiguous independent samples to LLVM. No - // reduction, tap merging, symmetry or fused multiply-add changes the sums. - for (i, weight) in kernel.iter().enumerate() { - let start = i.checked_mul(n)?; - let samples = row.get(start..start.checked_add(row_len)?)?; - for (d, s) in dst.iter_mut().zip(samples) { - *d += s * weight; + pass.horizontal(&mut row, &mut tmp)?; + pass.vertical(&tmp, &mut row, &mut result, amount / 100.0, threshold / 255.0)?; + Some(result) +} + +/// The shared inputs of the two separable blur passes. +struct Pass<'a> { + src: &'a Image, + out: Rect, + ctx: &'a Ctx, + kernel: &'a [f32], + ctl: &'a Interrupt<'a>, + n: usize, + row_len: usize, +} + +impl Pass<'_> { + /// Blur horizontally every source row the output (plus kernel halo) needs. + fn horizontal(&self, row: &mut [f32], tmp: &mut [f32]) -> Option<()> { + let (n, row_len) = (self.n, self.row_len); + let r = self.kernel.len() / 2; + let (x0, x1, y0) = (self.out.x0.checked_sub(r as i32)?, self.out.x1.checked_add(r as i32)?, self.out.y0.checked_sub(r as i32)?); + for (y, dst) in tmp.chunks_exact_mut(row_len).enumerate() { + if self.ctl.cancelled() { + return None; + } + read_row(self.src, x0, x1, y0.checked_add(y as i32)?, row)?; + premultiply(row, n, self.ctx.alpha); + // Tap-major traversal exposes contiguous independent samples to LLVM. No + // reduction, tap merging, symmetry or fused multiply-add changes the sums. + for (i, weight) in self.kernel.iter().enumerate() { + let start = i.checked_mul(n)?; + let samples = row.get(start..start.checked_add(row_len)?)?; + for (d, s) in dst.iter_mut().zip(samples) { + *d += s * weight; + } } } + Some(()) } - let (amount, threshold) = (amount / 100.0, threshold / 255.0); - let cc = n - usize::from(ctx.alpha); - for (y, dst) in result.chunks_exact_mut(row_len).enumerate() { - if ctl.cancelled() { - return None; - } - for (i, weight) in kernel.iter().enumerate() { - let start = y.checked_add(i)?.checked_mul(row_len)?; - let samples = tmp.get(start..start.checked_add(row_len)?)?; - for (d, s) in dst.iter_mut().zip(samples) { - *d += s * weight; + + /// Blur vertically and blend each output row against its original pixels. + /// `originals` is scratch space reused for the source row being compared. + fn vertical(&self, tmp: &[f32], originals: &mut [f32], result: &mut [f32], amount: f32, threshold: f32) -> Option<()> { + let row_len = self.row_len; + for (y, dst) in result.chunks_exact_mut(row_len).enumerate() { + if self.ctl.cancelled() { + return None; } + for (i, weight) in self.kernel.iter().enumerate() { + let start = y.checked_add(i)?.checked_mul(row_len)?; + let samples = tmp.get(start..start.checked_add(row_len)?)?; + for (d, s) in dst.iter_mut().zip(samples) { + *d += s * weight; + } + } + let originals = originals.get_mut(..row_len)?; + read_row(self.src, self.out.x0, self.out.x1, self.out.y0.checked_add(y as i32)?, originals)?; + self.sharpen_row(dst, originals, amount, threshold)?; } - let originals = row.get_mut(..row_len)?; - read_row(src, out.x0, out.x1, out.y0.checked_add(y as i32)?, originals)?; - for (blurred, original) in dst.chunks_exact_mut(n).zip(originals.chunks_exact(n)) { - if ctx.alpha && *original.last()? <= 0.0 { + Some(()) + } + + /// Replace each blurred pixel by the thresholded unsharp-mask value. + fn sharpen_row(&self, blurred_row: &mut [f32], originals: &[f32], amount: f32, threshold: f32) -> Option<()> { + let (n, alpha) = (self.n, self.ctx.alpha); + let cc = n - usize::from(alpha); + for (blurred, original) in blurred_row.chunks_exact_mut(n).zip(originals.chunks_exact(n)) { + if alpha && *original.last()? <= 0.0 { blurred.copy_from_slice(original); continue; } - let a = if ctx.alpha { *blurred.last()? } else { 1.0 }; + let a = if alpha { *blurred.last()? } else { 1.0 }; for (b, o) in blurred.iter_mut().zip(original).take(cc) { // Keep the original unpremultiply cut-off and division, followed by // the threshold epsilon and inclusive comparison, at every depth. - let b_value = if ctx.alpha { if a > 1e-7 { *b / a } else { 0.0 } } else { *b }; + let b_value = if alpha { if a > 1e-7 { *b / a } else { 0.0 } } else { *b }; let d = o - b_value; *b = if d.abs() + 1e-6 >= threshold { o + amount * d } else { *o }; } - if ctx.alpha { + if alpha { *blurred.last_mut()? = *original.last()?; } } + Some(()) } - Some(result) } diff --git a/crates/algo/src/sharpen/fft.rs b/crates/algo/src/sharpen/fft.rs index 6fdf18c8b0..57758b06d2 100644 --- a/crates/algo/src/sharpen/fft.rs +++ b/crates/algo/src/sharpen/fft.rs @@ -128,9 +128,9 @@ fn run(surface: &Surface, area: Rect, amount: f32, radius: f32, selection: Optio let resident = samples.checked_mul(4)?; let mut planner = FftPlanner::::new(); let horizontal = Plan::new(w.checked_add(2 * r)?, &kernel, &mut planner)?; - let vertical = Plan::new(h.checked_add(2 * r)?, &kernel, &mut planner)?; + let vertical_plan = Plan::new(h.checked_add(2 * r)?, &kernel, &mut planner)?; // A conservative allowance includes FFT plans/twiddles in addition to spectra. - let plans = horizontal.size.checked_add(vertical.size)?.checked_mul(256)?; + let plans = horizontal.size.checked_add(vertical_plan.size)?.checked_mul(256)?; let resident = resident.checked_add(plans)?; let workers = rayon::current_num_threads().max(1); let horizontal_worker = read_width.checked_mul(n)?.checked_mul(4)?.checked_add(horizontal.size.checked_add(horizontal.scratch)?.checked_mul(16)?)?; @@ -139,7 +139,7 @@ fn run(surface: &Surface, area: Rect, amount: f32, radius: f32, selection: Optio .checked_mul(n)? .checked_mul(4)? .checked_add(STRIPE.checked_mul(h)?.checked_mul(n)?.checked_mul(12)?)? - .checked_add(vertical.size.checked_add(vertical.scratch)?.checked_mul(16)?)?; + .checked_add(vertical_plan.size.checked_add(vertical_plan.scratch)?.checked_mul(16)?)?; let group = group_size(resident, horizontal_worker.max(vertical_worker), w.div_ceil(STRIPE), workers)?; let mut tmp = buffer(samples, 0.0f32)?; let finished = AtomicUsize::new(0); @@ -210,98 +210,29 @@ fn run(surface: &Surface, area: Rect, amount: f32, radius: f32, selection: Optio } let mut out = surface.clone(); let count = w.div_ceil(STRIPE); + let vertical = VerticalPass { + surface, + selection, + tmp: &tmp, + plan: &vertical_plan, + ctl, + area, + n, + w, + h, + rows, + row_len, + r, + y0, + read_y0, + read_y1, + alpha, + amount, + kernel_len: kernel.len(), + }; for first in (0..count).step_by(group) { let end = first.checked_add(group)?.min(count); - let results: Option> = (first..end) - .into_par_iter() - .map(|index| { - if ctl.cancelled() { - return None; - } - let x = index.checked_mul(STRIPE)?; - let width = STRIPE.min(w.checked_sub(x)?); - let mut columns = buffer(width.checked_mul(rows)?.checked_mul(n)?, 0.0f32)?; - for (y, row) in tmp.chunks_exact(row_len).enumerate() { - if y % 64 == 0 && ctl.cancelled() { - return None; - } - for (c, px) in row.get(x * n..(x + width) * n)?.chunks_exact(n).enumerate() { - let start = (c * rows + y) * n; - columns.get_mut(start..start + n)?.copy_from_slice(px); - } - } - let rect = Rect::new(area.x0.checked_add(x as i32)?, area.y0, area.x0.checked_add((x + width) as i32)?, area.y1); - let len = width.checked_mul(h)?.checked_mul(n)?; - let mut result = buffer(len, 0.0f32)?; - let mut original = buffer(len, 0.0f32)?; - surface.read_region_into(rect, &mut original); - let (mut data, mut scratch) = vertical.work()?; - for (c, samples) in columns.chunks_exact(rows * n).enumerate() { - if ctl.cancelled() { - return None; - } - let mut scales = [0.0f64; crate::fxutil::MAXC]; - for px in samples.chunks_exact(n) { - for (s, v) in scales.iter_mut().zip(px) { - *s = s.max(f64::from(v.abs())); - } - } - for channel in (0..n).step_by(2) { - if ctl.cancelled() { - return None; - } - data.fill(Number::default()); - for (i, d) in data.iter_mut().take(h + 2 * r).enumerate() { - let sy = y0.checked_add(i as i32)?.clamp(read_y0, read_y1.checked_sub(1)?); - let start = usize::try_from(sy - read_y0).ok()?.checked_mul(n)?; - let px = samples.get(start..start + n)?; - let sample = |c: usize| -> f64 { - match (px.get(c), scales.get(c)) { - (Some(v), Some(s)) if *s > 0.0 => f64::from(*v) / s, - _ => 0.0, - } - }; - *d = Number::new(sample(channel), sample(channel + 1)); - } - vertical.convolve(&mut data, &mut scratch)?; - for (y, d) in data.get(2 * r..2 * r + h)?.iter().enumerate() { - let start = (y * width + c) * n; - *result.get_mut(start + channel)? = (d.re * scales.get(channel)?) as f32; - if let Some(v) = result.get_mut(start + channel + 1).filter(|_| channel + 1 < n) { - *v = (d.im * scales.get(channel + 1)?) as f32; - } - } - } - } - for (px, o) in result.chunks_exact_mut(n).zip(original.chunks_exact(n)) { - if alpha && *o.last()? <= 0.0 { - px.copy_from_slice(o); - continue; - } - let a = if alpha { *px.last()? } else { 1.0 }; - // Near the original unpremultiply cutoff, cancellation/FFT noise - // or f32 sum order can expose colour. Decline the whole fast path. - let uncertainty = 4.0 * kernel.len() as f32 * f32::EPSILON; - if alpha && a <= 1e-7 + uncertainty { - return None; - } - for (v, o) in px.iter_mut().zip(o).take(n - usize::from(alpha)) { - let b = if alpha { *v / a } else { *v }; - *v = o + amount / 100.0 * (o - b); - if !v.is_finite() { - return None; - } - } - if alpha { - *px.last_mut()? = *o.last()?; - } - } - if let Some(sel) = selection { - crate::mix_selection(&mut result, rect, sel, &Image { rect, ch: n, data: original }, alpha); - } - Some((rect, result)) - }) - .collect(); + let results: Option> = (first..end).into_par_iter().map(|index| vertical.stripe(index)).collect(); for (rect, data) in results? { if ctl.cancelled() { return None; @@ -321,5 +252,145 @@ fn run(surface: &Surface, area: Rect, amount: f32, radius: f32, selection: Optio Some(out) } +/// Everything the vertical pass needs to turn one stripe of horizontally +/// convolved columns into finished, sharpened output pixels. +struct VerticalPass<'a> { + surface: &'a Surface, + selection: Option<&'a Surface>, + tmp: &'a [f32], + plan: &'a Plan, + ctl: &'a Interrupt<'a>, + area: Rect, + n: usize, + w: usize, + h: usize, + rows: usize, + row_len: usize, + r: usize, + y0: i32, + read_y0: i32, + read_y1: i32, + alpha: bool, + amount: f32, + kernel_len: usize, +} + +/// Channel `c` of `px` scaled into [-1, 1], or zero when the column is all zero. +fn normalized(px: &[f32], scales: &[f64], c: usize) -> f64 { + match (px.get(c), scales.get(c)) { + (Some(v), Some(s)) if *s > 0.0 => f64::from(*v) / s, + _ => 0.0, + } +} + +impl VerticalPass<'_> { + /// Process the `index`th stripe of columns, returning its rect and pixels. + fn stripe(&self, index: usize) -> Option<(Rect, Vec)> { + let (ctl, n, h) = (self.ctl, self.n, self.h); + if ctl.cancelled() { + return None; + } + let x = index.checked_mul(STRIPE)?; + let width = STRIPE.min(self.w.checked_sub(x)?); + let columns = self.gather_columns(x, width)?; + let rect = Rect::new(self.area.x0.checked_add(x as i32)?, self.area.y0, self.area.x0.checked_add((x + width) as i32)?, self.area.y1); + let len = width.checked_mul(h)?.checked_mul(n)?; + let mut result = buffer(len, 0.0f32)?; + let mut original = buffer(len, 0.0f32)?; + self.surface.read_region_into(rect, &mut original); + let (mut data, mut scratch) = self.plan.work()?; + for (c, samples) in columns.chunks_exact(self.rows * n).enumerate() { + if ctl.cancelled() { + return None; + } + self.convolve_column(samples, c, width, (&mut data, &mut scratch), &mut result)?; + } + self.sharpen(&mut result, &original)?; + if let Some(sel) = self.selection { + crate::mix_selection(&mut result, rect, sel, &Image { rect, ch: n, data: original }, self.alpha); + } + Some((rect, result)) + } + + /// Transpose the stripe at `x` of the horizontal results into column-major order. + fn gather_columns(&self, x: usize, width: usize) -> Option> { + let (n, rows) = (self.n, self.rows); + let mut columns = buffer(width.checked_mul(rows)?.checked_mul(n)?, 0.0f32)?; + for (y, row) in self.tmp.chunks_exact(self.row_len).enumerate() { + if y % 64 == 0 && self.ctl.cancelled() { + return None; + } + for (c, px) in row.get(x * n..(x + width) * n)?.chunks_exact(n).enumerate() { + let start = (c * rows + y) * n; + columns.get_mut(start..start + n)?.copy_from_slice(px); + } + } + Some(columns) + } + + /// Convolve one gathered column, two channels per complex FFT, into `result`. + fn convolve_column(&self, samples: &[f32], c: usize, width: usize, work: (&mut [Number], &mut [Number]), result: &mut [f32]) -> Option<()> { + let (n, h, r) = (self.n, self.h, self.r); + let (data, scratch) = work; + let mut scales = [0.0f64; crate::fxutil::MAXC]; + for px in samples.chunks_exact(n) { + for (s, v) in scales.iter_mut().zip(px) { + *s = s.max(f64::from(v.abs())); + } + } + for channel in (0..n).step_by(2) { + if self.ctl.cancelled() { + return None; + } + data.fill(Number::default()); + for (i, d) in data.iter_mut().take(h + 2 * r).enumerate() { + let sy = self.y0.checked_add(i as i32)?.clamp(self.read_y0, self.read_y1.checked_sub(1)?); + let start = usize::try_from(sy - self.read_y0).ok()?.checked_mul(n)?; + let px = samples.get(start..start + n)?; + *d = Number::new(normalized(px, &scales, channel), normalized(px, &scales, channel + 1)); + } + self.plan.convolve(data, scratch)?; + for (y, d) in data.get(2 * r..2 * r + h)?.iter().enumerate() { + let start = (y * width + c) * n; + *result.get_mut(start + channel)? = (d.re * scales.get(channel)?) as f32; + if let Some(v) = result.get_mut(start + channel + 1).filter(|_| channel + 1 < n) { + *v = (d.im * scales.get(channel + 1)?) as f32; + } + } + } + Some(()) + } + + /// Turn the blurred stripe into the unsharp-masked result against `original`. + /// Declines (`None`) wherever the fast path cannot match the exact kernel. + fn sharpen(&self, result: &mut [f32], original: &[f32]) -> Option<()> { + let (n, alpha) = (self.n, self.alpha); + for (px, o) in result.chunks_exact_mut(n).zip(original.chunks_exact(n)) { + if alpha && *o.last()? <= 0.0 { + px.copy_from_slice(o); + continue; + } + let a = if alpha { *px.last()? } else { 1.0 }; + // Near the original unpremultiply cutoff, cancellation/FFT noise + // or f32 sum order can expose colour. Decline the whole fast path. + let uncertainty = 4.0 * self.kernel_len as f32 * f32::EPSILON; + if alpha && a <= 1e-7 + uncertainty { + return None; + } + for (v, o) in px.iter_mut().zip(o).take(n - usize::from(alpha)) { + let b = if alpha { *v / a } else { *v }; + *v = o + self.amount / 100.0 * (o - b); + if !v.is_finite() { + return None; + } + } + if alpha { + *px.last_mut()? = *o.last()?; + } + } + Some(()) + } +} + #[cfg(test)] mod tests; diff --git a/crates/algo/src/stack.rs b/crates/algo/src/stack.rs index 2eec57e598..52e67a3b71 100644 --- a/crates/algo/src/stack.rs +++ b/crates/algo/src/stack.rs @@ -23,149 +23,245 @@ pub enum Stat { Variance, } -fn stat(v: &mut [f32], s: Stat) -> f32 { +/// Reusable per-chunk scratch for [`Stat::Entropy`]: an 8-bit histogram plus the list of bins the +/// current pixel touched. Allocated once per parallel chunk instead of one `HashMap` per channel +/// per pixel (the old cost: ~4.6 s for 3 × 24 MP). +struct EntropyScratch { + counts: [u32; 256], + touched: Vec, +} + +impl EntropyScratch { + fn new() -> Self { + Self { counts: [0; 256], touched: Vec::new() } + } +} + +/// One shared table per combine, never per pixel or worker. Rows cover the contributing sample +/// count, which can change at every pixel because of transparency or unequal layer lengths. +/// The triangular layout stores log2(n) followed by -p*log2(p) for counts 1..=n. Keeping the +/// division after the sum (rather than normalising each term) preserves the old output bits. +struct EntropyTerms { + values: Vec, +} + +impl EntropyTerms { + // At most 130 KiB, independent of image size and total layer count. Deeper stacks retain + // the original calculation; pixels with fewer contributors can still use a cached row. + const MAX_SAMPLES: usize = 256; + + fn new(samples: usize, pixels: usize) -> Self { + let mut values = Vec::new(); + // Tiny images do not amortise preparation, especially with hundreds of layers. + if samples < 2 || pixels < 256 { + return Self { values }; + } + let max = samples.min(Self::MAX_SAMPLES); + // max is capped at 256 before any size arithmetic or allocation. + let len = (max + 1) * (max + 2) / 2; + if values.try_reserve_exact(len).is_err() { + return Self { values }; + } + values.extend_from_slice(&[0.0; 3]); // Unused rows for zero and one sample. + for samples in 2..=max { + let n = samples as f32; + values.push(n.log2()); + for count in 1..=samples { + let p = count as f32 / n; + values.push(-p * p.log2()); + } + } + Self { values } + } + + fn row(&self, samples: usize) -> Option<&[f32]> { + if !(2..=Self::MAX_SAMPLES).contains(&samples) { + return None; + } + let start = samples * (samples + 1) / 2; + self.values.get(start..start + samples + 1) + } +} + +/// Shannon entropy (bits) of the 8-bit values in `v`, over log2(N). `v.len() >= 2`. +fn entropy(v: &[f32], scratch: &mut EntropyScratch, terms: Option<&[f32]>) -> f32 { + let n = v.len() as f32; + for x in v { + let bin = (x.clamp(0.0, 1.0) * 255.0).round() as u8; + let c = &mut scratch.counts[usize::from(bin)]; + if *c == 0 { + scratch.touched.push(bin); + } + *c += 1; + } + // Sum the bins in ascending order. The old HashMap summed in its (randomly seeded) iteration + // order, and float addition is not associative, so the old result could differ in the last + // bit from run to run; a fixed order makes it deterministic. Sorting only the few touched + // bins keeps this O(samples), not O(256), per pixel. + scratch.touched.sort_unstable(); + let h: f32 = scratch + .touched + .iter() + .map(|&bin| { + let count = scratch.counts[usize::from(bin)]; + terms.and_then(|row| row.get(count as usize)).copied().unwrap_or_else(|| { + let p = count as f32 / n; + -p * p.log2() + }) + }) + .sum(); + // Reset only the bins used, so the 256-entry array is never cleared wholesale. + for &bin in &scratch.touched { + scratch.counts[usize::from(bin)] = 0; + } + scratch.touched.clear(); + h / terms.and_then(|row| row.first()).copied().unwrap_or_else(|| n.log2()) +} + +/// `x` to the power `k` by repeated squaring. Every step is one IEEE 754 multiplication, so the +/// result has the same bits on every target. On Windows (MSVC), `f32::powi` with an exponent the +/// compiler cannot see as a constant calls the C runtime's `powf`, which rounds differently. +fn ipow(x: f32, k: u32) -> f32 { + let (mut base, mut k, mut acc) = (x, k, 1.0f32); + loop { + if k & 1 != 0 { + acc *= base; + } + k >>= 1; + if k == 0 { + return acc; + } + base *= base; + } +} + +fn stat(v: &mut [f32], s: Stat, scratch: &mut EntropyScratch) -> f32 { let n = v.len() as f32; if v.is_empty() { return 0.0; } + // Modes that need neither the mean nor the moments return before computing them (the old code + // computed the mean for every mode and threw it away; skipping it changes no result). + match s { + Stat::Summation => return v.iter().sum::(), + Stat::Minimum => return v.iter().copied().fold(f32::MAX, f32::min), + Stat::Maximum => return v.iter().copied().fold(f32::MIN, f32::max), + Stat::Range => return v.iter().copied().fold(f32::MIN, f32::max) - v.iter().copied().fold(f32::MAX, f32::min), + Stat::Median => { + let k = v.len(); + // Nonempty above, so k / 2 is in bounds. Selection puts the upper middle value in + // place in O(k); the lower partition need not be sorted. Use the same total order + // for its maximum to preserve signed zeros, infinities and NaNs. + let (lower, middle, _) = v.select_nth_unstable_by(k / 2, f32::total_cmp); + return if k % 2 == 1 { + *middle + } else { + // For even k >= 2 the lower partition contains the other middle value. + lower.iter().max_by(|a, b| a.total_cmp(b)).map_or(*middle, |lo| (*lo + *middle) / 2.0) + }; + } + Stat::Entropy => { + if v.len() < 2 { + return 0.0; + } + return entropy(v, scratch, None); + } + _ => {} + } let mean = v.iter().sum::() / n; - let m = |k: i32| v.iter().map(|x| (x - mean).powi(k)).sum::() / n; + let m = |k: u32| v.iter().map(|x| ipow(x - mean, k)).sum::() / n; match s { Stat::Mean => mean, - Stat::Summation => v.iter().sum::(), - Stat::Minimum => v.iter().copied().fold(f32::MAX, f32::min), - Stat::Maximum => v.iter().copied().fold(f32::MIN, f32::max), - Stat::Range => v.iter().copied().fold(f32::MIN, f32::max) - v.iter().copied().fold(f32::MAX, f32::min), - Stat::Median => { - v.sort_by(f32::total_cmp); - let k = v.len(); - if k % 2 == 1 { v[k / 2] } else { (v[k / 2 - 1] + v[k / 2]) / 2.0 } - } Stat::Variance => m(2), Stat::StandardDeviation => m(2).sqrt(), Stat::Skewness => { let sd = m(2).sqrt(); // Map −2..2 to 0..1 (0.5 = symmetric). - if sd < 1e-6 { 0.5 } else { (m(3) / sd.powi(3) / 4.0 + 0.5).clamp(0.0, 1.0) } + if sd < 1e-6 { 0.5 } else { (m(3) / ipow(sd, 3) / 4.0 + 0.5).clamp(0.0, 1.0) } } Stat::Kurtosis => { let var = m(2); // 0..10 → 0..1. if var < 1e-12 { 0.0 } else { (m(4) / (var * var) / 10.0).clamp(0.0, 1.0) } } - Stat::Entropy => { - if v.len() < 2 { - return 0.0; - } - let mut counts: std::collections::HashMap = std::collections::HashMap::new(); - for x in v.iter() { - *counts.entry((x.clamp(0.0, 1.0) * 255.0).round() as u8).or_default() += 1; - } - let h: f32 = counts - .values() - .map(|&c| { - let p = c as f32 / n; - -p * p.log2() - }) - .sum(); - h / n.log2() - } + // Handled above; a no-op keeps the match exhaustive without a panic path. + Stat::Summation | Stat::Minimum | Stat::Maximum | Stat::Range | Stat::Median | Stat::Entropy => 0.0, } } +/// Output pixels per parallel task: big enough to amortise the per-task scratch buffers and +/// rayon's scheduling, small enough (6000 tasks for 24 MP) to balance across many cores. +const CHUNK: usize = 4096; + /// Combines `layers` (each `len` pixels) with statistic `s`. Colour results retain their float /// range; quantisation/clipping belongs to the destination surface, not the statistic. pub fn combine(layers: &[Vec<[f32; 4]>], s: Stat) -> Vec<[f32; 4]> { + // Pick the statistic once, outside the pixel loop: each arm passes `stat` a constant mode, so + // the compiler builds one loop per statistic with the `match` folded away. Matching on `s` + // three times per pixel inside one shared loop made one thread 5–15% slower than the old + // serial loop (Mean, Variance, StdDev, Skewness, Kurtosis); this alone cuts Mean's + // instructions by a fifth. + match s { + Stat::Entropy => { + let pixels = layers.iter().map(Vec::len).max().unwrap_or(0); + let terms = EntropyTerms::new(layers.len(), pixels); + combine_with(layers, |v, sc| if v.len() < 2 { 0.0 } else { entropy(v, sc, terms.row(v.len())) }) + } + Stat::Kurtosis => combine_with(layers, |v, sc| stat(v, Stat::Kurtosis, sc)), + Stat::Maximum => combine_with(layers, |v, sc| stat(v, Stat::Maximum, sc)), + Stat::Mean => combine_with(layers, |v, sc| stat(v, Stat::Mean, sc)), + Stat::Median => combine_with(layers, |v, sc| stat(v, Stat::Median, sc)), + Stat::Minimum => combine_with(layers, |v, sc| stat(v, Stat::Minimum, sc)), + Stat::Range => combine_with(layers, |v, sc| stat(v, Stat::Range, sc)), + Stat::Skewness => combine_with(layers, |v, sc| stat(v, Stat::Skewness, sc)), + Stat::StandardDeviation => combine_with(layers, |v, sc| stat(v, Stat::StandardDeviation, sc)), + Stat::Summation => combine_with(layers, |v, sc| stat(v, Stat::Summation, sc)), + Stat::Variance => combine_with(layers, |v, sc| stat(v, Stat::Variance, sc)), + } +} + +fn combine_with(layers: &[Vec<[f32; 4]>], f: impl Fn(&mut [f32], &mut EntropyScratch) -> f32 + Sync + Send) -> Vec<[f32; 4]> { let len = layers.iter().map(Vec::len).max().unwrap_or(0); let mut out = vec![[0.0f32; 4]; len]; - let mut samples: [Vec; 3] = Default::default(); - for (i, o) in out.iter_mut().enumerate() { - for v in samples.iter_mut() { - v.clear(); - } - let mut alpha = 0.0f32; - for l in layers { - if let Some(p) = l.get(i) - && p[3] > 0.0 - { - for c in 0..3 { - samples[c].push(p[c]); + // Every output pixel depends only on the input pixels at the same index, so chunks of the + // output are independent: `par_rows` over a `CHUNK`-wide "row" with one channel (it runs + // serially on wasm). The per-pixel arithmetic is unchanged, hence the result is bit-identical + // to the serial loop for every mode (Entropy: see [`entropy`]). + crate::photo_util::par_rows(&mut out, CHUNK, 1, |chunk, outs| { + // Scratch is per chunk, not per pixel: the old loop's three `Vec`s were already reused + // across pixels, so keep that, and give each task its own set. + // Sized to the layer count up front and filled by index (`k` samples per pixel): a + // `push` per sample re-checked the capacity and stored the length every time; indexing + // cuts the loop's instructions by another third. + let mut samples: [Vec; 3] = [0, 1, 2].map(|_| vec![0.0; layers.len()]); + let mut scratch = EntropyScratch::new(); + let base = chunk * CHUNK; + for (j, o) in outs.iter_mut().enumerate() { + let i = base + j; + let mut k = 0; + let mut alpha = 0.0f32; + for l in layers { + if let Some(p) = l.get(i) + && p[3] > 0.0 + { + for c in 0..3 { + samples[c][k] = p[c]; + } + k += 1; + alpha = alpha.max(p[3]); } - alpha = alpha.max(p[3]); } + if alpha <= 0.0 { + continue; + } + for c in 0..3 { + o[c] = f(&mut samples[c][..k], &mut scratch); + } + o[3] = alpha; } - if alpha <= 0.0 { - continue; - } - for c in 0..3 { - o[c] = stat(&mut samples[c], s); - } - o[3] = alpha; - } + }); out } #[cfg(test)] -mod tests { - use super::*; - - fn px(v: f32) -> Vec<[f32; 4]> { - vec![[v, v, v, 1.0]] - } - - #[test] - fn basic_statistics() { - let layers = vec![px(0.1), px(0.9), px(0.2), px(0.3), px(0.1)]; - let at = |s| combine(&layers, s)[0][0]; - assert!((at(Stat::Mean) - 0.32).abs() < 1e-6); - assert!((at(Stat::Median) - 0.2).abs() < 1e-6); - assert_eq!(at(Stat::Minimum), 0.1); - assert_eq!(at(Stat::Maximum), 0.9); - assert!((at(Stat::Range) - 0.8).abs() < 1e-6); - assert!((at(Stat::Summation) - 1.6).abs() < 1e-6); - let var = at(Stat::Variance); - assert!((at(Stat::StandardDeviation) - var.sqrt()).abs() < 1e-6); - assert!(at(Stat::Skewness) > 0.5, "right-skewed"); - assert!(at(Stat::Entropy) > 0.0 && at(Stat::Entropy) <= 1.0); - assert!(at(Stat::Kurtosis) > 0.0); - } - - #[test] - fn statistics_preserve_hdr_and_signed_samples() { - let layers = vec![px(-2.0), px(4.0)]; - for (mode, expected) in [ - (Stat::Mean, 1.0), - (Stat::Median, 1.0), - (Stat::Minimum, -2.0), - (Stat::Maximum, 4.0), - (Stat::Range, 6.0), - (Stat::Summation, 2.0), - (Stat::Variance, 9.0), - (Stat::StandardDeviation, 3.0), - ] { - assert_eq!(combine(&layers, mode)[0], [expected, expected, expected, 1.0], "{mode:?}"); - } - for mode in [Stat::Mean, Stat::Median] { - assert_eq!(combine(&[px(2.0), px(4.0)], mode)[0], [3.0, 3.0, 3.0, 1.0]); - assert_eq!(combine(&[px(-2.0), px(-4.0)], mode)[0], [-3.0, -3.0, -3.0, 1.0]); - } - // These display-oriented statistics still apply their own normalisation. - for mode in [Stat::Entropy, Stat::Skewness, Stat::Kurtosis] { - assert!((0.0..=1.0).contains(&combine(&layers, mode)[0][0]), "{mode:?}"); - } - } - - #[test] - fn transparent_samples_are_ignored() { - let layers = vec![vec![[0.5, 0.5, 0.5, 1.0]], vec![[1.0, 1.0, 1.0, 0.0]]]; - assert_eq!(combine(&layers, Stat::Mean)[0], [0.5, 0.5, 0.5, 1.0]); - let empty = vec![vec![[1.0, 1.0, 1.0, 0.0]]]; - assert_eq!(combine(&empty, Stat::Mean)[0][3], 0.0); - } - - #[test] - fn median_removes_outlier() { - let layers = vec![px(0.5), px(0.5), px(1.0)]; - assert_eq!(combine(&layers, Stat::Median)[0][0], 0.5); - } -} +mod tests; diff --git a/crates/algo/src/stack/tests.rs b/crates/algo/src/stack/tests.rs new file mode 100644 index 0000000000..d9f041124d --- /dev/null +++ b/crates/algo/src/stack/tests.rs @@ -0,0 +1,477 @@ +use super::*; + +fn px(v: f32) -> Vec<[f32; 4]> { + vec![[v, v, v, 1.0]] +} + +#[test] +fn basic_statistics() { + let layers = vec![px(0.1), px(0.9), px(0.2), px(0.3), px(0.1)]; + let at = |s| combine(&layers, s)[0][0]; + assert!((at(Stat::Mean) - 0.32).abs() < 1e-6); + assert!((at(Stat::Median) - 0.2).abs() < 1e-6); + assert_eq!(at(Stat::Minimum), 0.1); + assert_eq!(at(Stat::Maximum), 0.9); + assert!((at(Stat::Range) - 0.8).abs() < 1e-6); + assert!((at(Stat::Summation) - 1.6).abs() < 1e-6); + let var = at(Stat::Variance); + assert!((at(Stat::StandardDeviation) - var.sqrt()).abs() < 1e-6); + assert!(at(Stat::Skewness) > 0.5, "right-skewed"); + assert!(at(Stat::Entropy) > 0.0 && at(Stat::Entropy) <= 1.0); + assert!(at(Stat::Kurtosis) > 0.0); +} + +#[test] +fn statistics_preserve_hdr_and_signed_samples() { + let layers = vec![px(-2.0), px(4.0)]; + for (mode, expected) in [ + (Stat::Mean, 1.0), + (Stat::Median, 1.0), + (Stat::Minimum, -2.0), + (Stat::Maximum, 4.0), + (Stat::Range, 6.0), + (Stat::Summation, 2.0), + (Stat::Variance, 9.0), + (Stat::StandardDeviation, 3.0), + ] { + assert_eq!(combine(&layers, mode)[0], [expected, expected, expected, 1.0], "{mode:?}"); + } + for mode in [Stat::Mean, Stat::Median] { + assert_eq!(combine(&[px(2.0), px(4.0)], mode)[0], [3.0, 3.0, 3.0, 1.0]); + assert_eq!(combine(&[px(-2.0), px(-4.0)], mode)[0], [-3.0, -3.0, -3.0, 1.0]); + } + // These display-oriented statistics still apply their own normalisation. + for mode in [Stat::Entropy, Stat::Skewness, Stat::Kurtosis] { + assert!((0.0..=1.0).contains(&combine(&layers, mode)[0][0]), "{mode:?}"); + } +} + +#[test] +fn transparent_samples_are_ignored() { + let layers = vec![vec![[0.5, 0.5, 0.5, 1.0]], vec![[1.0, 1.0, 1.0, 0.0]]]; + assert_eq!(combine(&layers, Stat::Mean)[0], [0.5, 0.5, 0.5, 1.0]); + let empty = vec![vec![[1.0, 1.0, 1.0, 0.0]]]; + assert_eq!(combine(&empty, Stat::Mean)[0][3], 0.0); +} + +#[test] +fn median_removes_outlier() { + let layers = vec![px(0.5), px(0.5), px(1.0)]; + assert_eq!(combine(&layers, Stat::Median)[0][0], 0.5); +} + +fn sorted_median(v: &mut [f32]) -> f32 { + v.sort_unstable_by(f32::total_cmp); + let k = v.len(); + if k == 0 { + 0.0 + } else if k % 2 == 1 { + v[k / 2] + } else { + (v[k / 2 - 1] + v[k / 2]) / 2.0 + } +} + +fn assert_median_matches_sort(mut values: Vec) { + let expected = sorted_median(&mut values.clone()); + let actual = stat(&mut values, Stat::Median, &mut EntropyScratch::new()); + if expected.is_nan() { + // IEEE arithmetic does not specify which NaN payload an even median propagates. + assert!(actual.is_nan()); + } else { + assert_eq!(actual.to_bits(), expected.to_bits(), "{values:?}"); + } +} + +#[test] +fn median_selection_matches_sort_for_varied_stack_sizes_and_orders() { + for len in 0..=257 { + let ascending: Vec = (0..len).map(|i| i as f32 / 16.0 - 8.0).collect(); + assert_median_matches_sort(ascending.clone()); + assert_median_matches_sort(ascending.into_iter().rev().collect()); + assert_median_matches_sort(vec![0.25; len]); + assert_median_matches_sort((0..len).map(|i| ((i * 31) % 7) as f32 - 3.0).collect()); + // Deterministic shuffled samples, including float bit patterns outside 0..=1. + let mut seed = 0x9e37_79b9u32; + assert_median_matches_sort( + (0..len) + .map(|_| { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + f32::from_bits(seed) + }) + .collect(), + ); + } +} + +#[test] +fn median_selection_preserves_total_float_order() { + let special = [f32::from_bits(0xffc0_0001), f32::NEG_INFINITY, -f32::MAX, -1.0, -0.0, 0.0, 1.0, f32::MAX, f32::INFINITY, f32::from_bits(0x7fc0_0001)]; + for &a in &special { + // An odd median returns the selected NaN's bits without doing arithmetic. + assert_eq!(stat(&mut [a], Stat::Median, &mut EntropyScratch::new()).to_bits(), a.to_bits()); + for &b in &special { + assert_median_matches_sort(vec![a, b]); + for &c in &special { + let mut values = [a, b, c]; + let expected = sorted_median(&mut values.clone()); + assert_eq!(stat(&mut values, Stat::Median, &mut EntropyScratch::new()).to_bits(), expected.to_bits()); + } + } + } +} + +#[test] +fn deep_median_stack_uses_only_visible_samples_per_channel() { + for count in [32, 33, 128, 129] { + let layers: Vec> = (0..count) + .map(|i| { + let pixel = [((i * 31) % 137) as f32, -(i as f32), (i % 5) as f32, if i % 3 == 0 { 0.0 } else { 0.5 }]; + if i % 7 == 0 { vec![pixel] } else { vec![pixel, [0.0; 4]] } + }) + .collect(); + let actual = combine(&layers, Stat::Median); + for channel in 0..3 { + let mut samples: Vec = layers.iter().filter(|l| l[0][3] > 0.0).map(|l| l[0][channel]).collect(); + assert_eq!(actual[0][channel].to_bits(), sorted_median(&mut samples).to_bits()); + } + assert_eq!(actual[0][3], 0.5); + assert_eq!(actual[1], [0.0; 4]); + } +} + +/// Five layers over six pixels (the last layer is short). Covers opaque, partly transparent, +/// fully transparent, HDR / negative, single-sample and tied-value pixels. +fn fixture() -> Vec> { + vec![ + vec![[0.10, 0.20, 0.30, 1.0], [0.5, 0.5, 0.5, 1.0], [0.9, 0.9, 0.9, 0.0], [-2.0, 4.0, 0.0, 1.0], [0.7, 0.2, 0.1, 0.5], [0.25, 0.25, 0.25, 1.0]], + vec![[0.90, 0.60, 0.30, 1.0], [0.0, 0.0, 0.0, 0.0], [0.8, 0.8, 0.8, 0.0], [3.0, -1.0, 0.5, 0.75], [0.0, 0.0, 0.0, 0.0], [0.25, 0.75, 0.25, 1.0]], + vec![[0.20, 0.90, 0.30, 0.5], [0.1, 0.2, 0.3, 1.0], [0.7, 0.7, 0.7, 0.0], [0.5, 0.5, 0.5, 1.0], [0.0, 0.0, 0.0, 0.0], [0.5, 0.75, 0.25, 0.25]], + vec![[0.30, 0.05, 0.30, 0.25], [0.3, 0.4, 0.5, 0.5], [0.6, 0.6, 0.6, 0.0], [1.5, 1.5, 1.5, 1.0], [0.0, 0.0, 0.0, 0.0], [0.25, 0.75, 0.5, 1.0]], + vec![[0.10, 0.70, 0.99, 1.0], [0.9, 0.1, 0.2, 1.0], [0.5, 0.5, 0.5, 0.0], [0.0, 0.0, 0.0, 1.0]], + ] +} + +fn bits(px: &[[f32; 4]]) -> Vec<[u32; 4]> { + px.iter().map(|p| p.map(f32::to_bits)).collect() +} + +/// Every mode except Entropy is bit-for-bit what the old serial loop produced. The expected +/// bit patterns were recorded by running the unmodified `combine` on [`fixture`]. +#[test] +fn statistics_are_bit_identical_to_the_serial_implementation() { + let layers = fixture(); + let expected: Vec<(Stat, Vec<[u32; 4]>)> = vec![ + ( + Stat::Kurtosis, + [ + [0x3e96956b, 0x3e182e3f, 0x3ea66664, 0x3f800000], + [0x3e3d004c, 0x3e0b4396, 0x3e030e1f, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x3e594eb2, 0x3e6ef420, 0x3e7ffffe, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x3f000000], + [0x3e6eeeee, 0x3e6eeeee, 0x3e6eeeee, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Maximum, + [ + [0x3f666666, 0x3f666666, 0x3f7d70a4, 0x3f800000], + [0x3f666666, 0x3f000000, 0x3f000000, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x40400000, 0x40800000, 0x3fc00000, 0x3f800000], + [0x3f333333, 0x3e4ccccd, 0x3dcccccd, 0x3f000000], + [0x3f000000, 0x3f400000, 0x3f000000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Mean, + [ + [0x3ea3d70a, 0x3efae148, 0x3ee0418a, 0x3f800000], + [0x3ee66666, 0x3e99999a, 0x3ec00000, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x3f19999a, 0x3f800000, 0x3f000000, 0x3f800000], + [0x3f333333, 0x3e4ccccd, 0x3dcccccd, 0x3f000000], + [0x3ea00000, 0x3f200000, 0x3ea00000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Median, + [ + [0x3e4ccccd, 0x3f19999a, 0x3e99999a, 0x3f800000], + [0x3ecccccd, 0x3e99999a, 0x3ecccccd, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x3f000000, 0x3f000000, 0x3f000000, 0x3f800000], + [0x3f333333, 0x3e4ccccd, 0x3dcccccd, 0x3f000000], + [0x3e800000, 0x3f400000, 0x3e800000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Minimum, + [ + [0x3dcccccd, 0x3d4ccccd, 0x3e99999a, 0x3f800000], + [0x3dcccccd, 0x3dcccccd, 0x3e4ccccd, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0xc0000000, 0xbf800000, 0x00000000, 0x3f800000], + [0x3f333333, 0x3e4ccccd, 0x3dcccccd, 0x3f000000], + [0x3e800000, 0x3e800000, 0x3e800000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Range, + [ + [0x3f4ccccc, 0x3f599999, 0x3f30a3d7, 0x3f800000], + [0x3f4ccccc, 0x3ecccccd, 0x3e99999a, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x40a00000, 0x40a00000, 0x3fc00000, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x3f000000], + [0x3e800000, 0x3f000000, 0x3e800000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Skewness, + [ + [0x3f522026, 0x3ee7cf76, 0x3f600000, 0x3f800000], + [0x3f1bd152, 0x3efffffe, 0x3ee4a11b, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x3eedb65a, 0x3f2ea754, 0x3f3a6c7a, 0x3f800000], + [0x3f000000, 0x3f000000, 0x3f000000, 0x3f000000], + [0x3f49e69e, 0x3e58658a, 0x3f49e69e, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::StandardDeviation, + [ + [0x3e99421e, 0x3ea23b6b, 0x3e8d4fdf, 0x3f800000], + [0x3e97739e, 0x3e21e89b, 0x3e050581, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x3fd3e0b1, 0x3fd9f9e5, 0x3f0c378c, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x3f000000], + [0x3dddb3d7, 0x3e5db3d7, 0x3dddb3d7, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Summation, + [ + [0x3fcccccd, 0x401ccccd, 0x400c28f6, 0x3f800000], + [0x3fe66666, 0x3f99999a, 0x3fc00000, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x40400000, 0x40a00000, 0x40200000, 0x3f800000], + [0x3f333333, 0x3e4ccccd, 0x3dcccccd, 0x3f000000], + [0x3fa00000, 0x40200000, 0x3fa00000, 0x3f800000], + ] + .to_vec(), + ), + ( + Stat::Variance, + [ + [0x3db78033, 0x3dcd9e83, 0x3d9c022a, 0x3f800000], + [0x3db33332, 0x3ccccccd, 0x3c8a3d70, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x00000000], + [0x402f5c29, 0x4039999a, 0x3e99999a, 0x3f800000], + [0x00000000, 0x00000000, 0x00000000, 0x3f000000], + [0x3c400000, 0x3d400000, 0x3c400000, 0x3f800000], + ] + .to_vec(), + ), + ]; + for (mode, want) in expected { + assert_eq!(bits(&combine(&layers, mode)), want, "{mode:?}"); + } +} + +/// Entropy against values worked out by hand (bits of the 8-bit histogram, over log2(N)). +#[test] +fn entropy_matches_hand_computed_values() { + let out = combine(&fixture(), Stat::Entropy); + let h = |ps: &[f64]| -> f64 { + let n: f64 = ps.iter().sum(); + ps.iter().map(|c| -(c / n) * (c / n).log2()).sum::() / n.log2() + }; + // Pixel 1: four distinct values -> 2 bits / log2(4) = exactly 1. + assert_eq!(out[1][0], 1.0); + // Pixel 2: every sample transparent -> untouched. + assert_eq!(out[2], [0.0; 4]); + // Pixel 3, red: -2 and 0 land in bin 0, 3 and 1.5 clamp into bin 255, 0.5 is alone. + assert!((f64::from(out[3][0]) - h(&[2.0, 2.0, 1.0])).abs() < 1e-6, "{}", out[3][0]); + // Pixel 4: a single contributing sample has no entropy. + assert_eq!(out[4], [0.0, 0.0, 0.0, 0.5]); + // Pixel 5: three samples share a bin, one is alone, in every channel. + for (c, v) in out[5].iter().take(3).enumerate() { + assert!((f64::from(*v) - h(&[3.0, 1.0])).abs() < 1e-6, "channel {c}: {v}"); + } +} + +/// The old HashMap summed in a random order, so Entropy could differ by an ulp between runs. +/// Now the bins are summed in ascending order: repeated runs (and any thread count) agree. +#[test] +fn entropy_is_deterministic() { + let layers: Vec> = (0..7) + .map(|k| (0..3 * CHUNK + 17).map(|i| [((i * 7 + k * 13) % 11) as f32 / 10.0, ((i + k) % 5) as f32 / 4.0, ((i * k) % 9) as f32 / 8.0, 1.0]).collect()) + .collect(); + let first = bits(&combine(&layers, Stat::Entropy)); + for _ in 0..8 { + assert_eq!(bits(&combine(&layers, Stat::Entropy)), first); + } +} + +/// The pre-lookup calculation, summing the 8-bit bins in ascending order. This deliberately +/// does not call `entropy` or use its lookup/scratch, so it catches changes in rounding too. +fn entropy_reference(v: &[f32]) -> f32 { + if v.len() < 2 { + return 0.0; + } + let mut counts = [0u32; 256]; + for x in v { + counts[(x.clamp(0.0, 1.0) * 255.0).round() as usize] += 1; + } + let n = v.len() as f32; + let h: f32 = counts + .iter() + .filter(|&&count| count > 0) + .map(|&count| { + let p = count as f32 / n; + -p * p.log2() + }) + .sum(); + h / n.log2() +} + +#[test] +fn entropy_terms_preserve_each_operation_and_bound_storage() { + let terms = EntropyTerms::new(usize::MAX, usize::MAX); + let max = EntropyTerms::MAX_SAMPLES; + assert_eq!(terms.values.len(), (max + 1) * (max + 2) / 2); + assert!(terms.values.len() * size_of::() < 130 * 1024); + for samples in 2..=max { + let row = terms.row(samples).unwrap(); + let n = samples as f32; + assert_eq!(row[0].to_bits(), n.log2().to_bits()); + for (count, &term) in row.iter().enumerate().skip(1) { + let p = count as f32 / n; + assert_eq!(term.to_bits(), (-p * p.log2()).to_bits(), "{count}/{samples}"); + } + } + for samples in [0, 1, max + 1, usize::MAX] { + assert!(terms.row(samples).is_none()); + } + assert!(EntropyTerms::new(1, usize::MAX).values.is_empty()); + assert!(EntropyTerms::new(usize::MAX, 255).values.is_empty()); + assert!(EntropyTerms { values: Vec::new() }.row(2).is_none()); +} + +#[test] +fn entropy_lookup_and_fallback_match_reference_bits() { + let terms = EntropyTerms::new(EntropyTerms::MAX_SAMPLES, CHUNK); + let mut scratch = EntropyScratch::new(); + let special = [f32::NAN, f32::NEG_INFINITY, f32::INFINITY, -3.0, -0.0, 0.0, 0.5 / 255.0, 0.5, 1.0, 4.0]; + // Move repeatedly between counts and distributions to exercise scratch reset, duplicate + // bins, every cached denominator and the uncached side of the table boundary. + for samples in (2..=EntropyTerms::MAX_SAMPLES + 2).chain([513, 3, 256, 2]) { + for pattern in 0..4 { + let v: Vec = (0..samples) + .map(|i| match pattern { + 0 => 0.5, + 1 => (i % 256) as f32 / 255.0, + 2 => ((i * 137 + samples * 29) % 65536) as f32 / 65535.0, + _ => special[i % special.len()], + }) + .collect(); + let expected = entropy_reference(&v).to_bits(); + assert_eq!(entropy(&v, &mut scratch, terms.row(samples)).to_bits(), expected, "cached: n={samples}, pattern={pattern}"); + assert_eq!(entropy(&v, &mut scratch, None).to_bits(), expected, "fallback: n={samples}, pattern={pattern}"); + } + } +} + +#[test] +fn entropy_combine_matches_reference_with_changing_contributor_counts() { + for (count, len) in [(19, 2 * CHUNK + 17), (EntropyTerms::MAX_SAMPLES + 1, 513)] { + let layers: Vec> = (0..count) + .map(|k| { + (0..len.saturating_sub(k % 13)) + .map(|i| { + [ + ((i * 31 + k * 7) % 256) as f32 / 255.0, + ((i * 17 + k) % 65536) as f32 / 65535.0, + if (i + k).is_multiple_of(23) { f32::NAN } else { (i % 7) as f32 - 2.0 }, + if k > i % (count + 1) { 0.0 } else { 0.5 }, + ] + }) + .collect() + }) + .collect(); + let expected = bits(&combine_with(&layers, |v, _| entropy_reference(v))); + assert_eq!(bits(&combine(&layers, Stat::Entropy)), expected); + #[cfg(not(target_arch = "wasm32"))] + for threads in [1, 3] { + let pool = rayon::ThreadPoolBuilder::new().num_threads(threads).build().unwrap(); + assert_eq!(pool.install(|| bits(&combine(&layers, Stat::Entropy))), expected, "{threads} threads"); + } + } +} + +/// Chunking is invisible: a multi-chunk image (with a ragged last chunk and layers of unequal +/// length) gives, pixel by pixel, what combining that pixel on its own gives. +#[test] +fn chunked_result_equals_per_pixel_result() { + let len = 2 * CHUNK + 5; + let layer = |k: usize, n: usize| -> Vec<[f32; 4]> { + (0..n) + .map(|i| { + [ + ((i * 31 + k * 7) % 101) as f32 / 100.0, + ((i * 17 + k) % 53) as f32 / 52.0 - 0.2, + ((i + 3 * k) % 29) as f32 / 28.0, + if (i + k).is_multiple_of(4) { 0.0 } else { 0.5 + ((i + k) % 3) as f32 / 4.0 }, + ] + }) + .collect() + }; + let layers = vec![layer(1, len), layer(2, CHUNK + 3), layer(3, len - 1), layer(4, 0), layer(5, len)]; + for mode in [ + Stat::Entropy, + Stat::Kurtosis, + Stat::Maximum, + Stat::Mean, + Stat::Median, + Stat::Minimum, + Stat::Range, + Stat::Skewness, + Stat::StandardDeviation, + Stat::Summation, + Stat::Variance, + ] { + let whole = combine(&layers, mode); + assert_eq!(whole.len(), len); + for i in (0..len).step_by(97).chain([CHUNK - 1, CHUNK, CHUNK + 1, 2 * CHUNK - 1, 2 * CHUNK, len - 1]) { + let one: Vec> = layers.iter().map(|l| l.get(i).map(|p| vec![*p]).unwrap_or_default()).collect(); + assert_eq!(whole[i].map(f32::to_bits), combine(&one, mode)[0].map(f32::to_bits), "{mode:?} pixel {i}"); + } + } +} + +#[test] +fn no_layers_gives_an_empty_result() { + assert!(combine(&[], Stat::Median).is_empty()); +} + +/// [`ipow`] multiplies in the order `x·x²` for the cube and `x²·x²` for the fourth power. +#[test] +fn ipow_squares_and_multiplies() { + for x in [0.58f32, -0.12, -2.6, 2.4, -0.6, 1.0e-3, 0.0] { + assert_eq!(ipow(x, 0).to_bits(), 1.0f32.to_bits()); + assert_eq!(ipow(x, 1).to_bits(), x.to_bits()); + assert_eq!(ipow(x, 2).to_bits(), (x * x).to_bits()); + assert_eq!(ipow(x, 3).to_bits(), (x * (x * x)).to_bits()); + assert_eq!(ipow(x, 4).to_bits(), ((x * x) * (x * x)).to_bits()); + } +} diff --git a/crates/algo/src/stylize2/extrude.rs b/crates/algo/src/stylize2/extrude.rs index 78a3df273b..ae70351c09 100644 --- a/crates/algo/src/stylize2/extrude.rs +++ b/crates/algo/src/stylize2/extrude.rs @@ -20,7 +20,6 @@ pub(super) fn blocks(src: &Image, out: Rect, ctx: &Ctx, spec: &ExtrudeSpec) -> O if [b.x0, b.y0, b.x1, b.y1, out.x0, out.y0, out.x1, out.y1].iter().any(|v| v.unsigned_abs() > MAX_COORD as u32) { return None; } - let n = src.ch; let w = out.width() as usize; let count = w.checked_mul(out.height() as usize)?; if count > MAX_PIXELS { @@ -44,111 +43,190 @@ pub(super) fn blocks(src: &Image, out: Rect, ctx: &Ctx, spec: &ExtrudeSpec) -> O if i64::from(cx1 - cx0 + 1).checked_mul(i64::from(cy1 - cy0 + 1))? > MAX_CELLS { return None; } - let (bcx, bcy) = ((b.x0 + b.x1) as f32 / 2.0, (b.y0 + b.y1) as f32 / 2.0); - let half_diag = ((b.width() as f32).hypot(b.height() as f32) / 2.0).max(1.0); - let half = cs as f32 / 2.0; - let cc = ncol(ctx, n); - let sub = subtractive(ctx); + let pass = BlockPass { + src, + out, + ctx, + spec, + b, + area, + cs, + k, + reach, + centre: ((b.x0 + b.x1) as f32 / 2.0, (b.y0 + b.y1) as f32 / 2.0), + half_diag: ((b.width() as f32).hypot(b.height() as f32) / 2.0).max(1.0), + half: cs as f32 / 2.0, + }; // Ascending gy, then gx, is the tie order of every pixel's old candidate search. for cy in cy0..=cy1 { for cx in cx0..=cx1 { - let r = Rect::new(b.x0 + cx * cs, b.y0 + cy * cs, b.x0 + (cx + 1) * cs, b.y0 + (cy + 1) * cs); - let clipped = r.intersect(&b); - if spec.mask_incomplete && clipped != r { - continue; - } - let mut col = [0.0f32; MAXC]; - let mut cnt = 0.0; - let step = (cs / 8).max(1) as usize; - for yy in (clipped.y0..clipped.y1).step_by(step) { - for xx in (clipped.x0..clipped.x1).step_by(step) { - let a = if ctx.alpha { src.get(xx, yy, n - 1) } else { 1.0 }; - for (c, v) in col.iter_mut().enumerate().take(n) { - let s = src.get(xx, yy, c); - *v += if ctx.alpha && c < n - 1 { s * a } else { s }; - } - cnt += 1.0; - } - } - if cnt > 0.0 { - for v in col.iter_mut().take(n) { - *v /= cnt; - } - } - unpremul_px(&mut col[..n], ctx.alpha); - let h = if spec.level_based { luma(ctx, &col[..n]).clamp(0.0, 1.0) } else { hash01(cx, cy, 21, spec.seed) }; - if !h.is_finite() { - return None; - } - let (mx, my) = (r.x0 as f32 + half, r.y0 as f32 + half); - let off = reach * h / half_diag; - let (ox, oy) = ((mx - bcx) * off, (my - bcy) * off); - // Two extra pixels conservatively cover rounding in the unchanged interval test. - let sweep = Rect::new( - (mx.min(mx + ox) - half).floor() as i32 - 2, - (my.min(my + oy) - half).floor() as i32 - 2, - (mx.max(mx + ox) + half).ceil() as i32 + 2, - (my.max(my + oy) + half).ceil() as i32 + 2, - ) - .intersect(&area); - for y in sweep.y0..sweep.y1 { - let gy = (y - b.y0).div_euclid(cs); - if (cy - gy).abs() > k { - continue; - } - let fy = y as f32 + 0.5; - let Some(iy) = interval(fy, my, oy, half) else { continue }; - for x in sweep.x0..sweep.x1 { - if (cx - (x - b.x0).div_euclid(cs)).abs() > k { - continue; - } - let i = (y - out.y0) as usize * w + (x - out.x0) as usize; - let height = heights.get_mut(i)?; - if *height >= h { - continue; - } - let fx = x as f32 + 0.5; - let Some(ix) = interval(fx, mx, ox, half) else { continue }; - let (lo, hi) = (ix.0.max(iy.0), ix.1.min(iy.1)); - if lo > hi { - continue; - } - *height = h; - let face = if hi >= 1.0 { - 0 - } else if ix.1 < iy.1 { - if ox > 0.0 { 1 } else { 2 } - } else if oy > 0.0 { - 3 - } else { - 4 - }; - let px = res.get_mut(i * n..(i + 1) * n)?; - if face == 0 && !spec.solid_front { - let (sx, sy) = ((fx - (mx - bcx) * off).floor() as i32, (fy - (my - bcy) * off).floor() as i32); - read_px(src, sx.clamp(r.x0, r.x1 - 1), sy.clamp(r.y0, r.y1 - 1), px); - } else { - let shade = match face { - 0 => 1.0, - 1 => 0.85, - 2 => 0.55, - 3 => 0.95, - _ => 0.45, - }; - for (v, c) in px.iter_mut().zip(&col).take(cc) { - *v = if sub { 1.0 - (1.0 - *c) * shade } else { *c * shade }.clamp(0.0, 1.0); - } - if ctx.alpha { - *px.last_mut()? = col.get(n - 1).copied()?; - } - } - } - } + pass.paint_cell(cx, cy, &mut heights, &mut res)?; } } Some(res) } +/// The constants shared by every swept block of one `blocks` call. +struct BlockPass<'a> { + src: &'a Image, + out: Rect, + ctx: &'a Ctx, + spec: &'a ExtrudeSpec, + b: Rect, + area: Rect, + cs: i32, + k: i32, + reach: f32, + centre: (f32, f32), + half_diag: f32, + half: f32, +} + +/// One cell's flat colour and where its extruded block sweeps to. +struct Block { + r: Rect, + cx: i32, + m: (f32, f32), + o: (f32, f32), + off: f32, + h: f32, + col: [f32; MAXC], +} + +impl BlockPass<'_> { + /// Average the (premultiplied) source over `clipped`, sampling every `step` pixels. + fn cell_colour(&self, clipped: Rect) -> [f32; MAXC] { + let (src, ctx, n) = (self.src, self.ctx, self.src.ch); + let mut col = [0.0f32; MAXC]; + let mut cnt = 0.0; + let step = (self.cs / 8).max(1) as usize; + for yy in (clipped.y0..clipped.y1).step_by(step) { + for xx in (clipped.x0..clipped.x1).step_by(step) { + let a = if ctx.alpha { src.get(xx, yy, n - 1) } else { 1.0 }; + for (c, v) in col.iter_mut().enumerate().take(n) { + let s = src.get(xx, yy, c); + *v += if ctx.alpha && c < n - 1 { s * a } else { s }; + } + cnt += 1.0; + } + } + if cnt > 0.0 { + for v in col.iter_mut().take(n) { + *v /= cnt; + } + } + unpremul_px(&mut col[..n], ctx.alpha); + col + } + + /// Rasterize the block of grid cell (`cx`, `cy`) over the pixels it can hit. + fn paint_cell(&self, cx: i32, cy: i32, heights: &mut [f32], res: &mut [f32]) -> Option<()> { + let (b, cs, half) = (self.b, self.cs, self.half); + let r = Rect::new(b.x0 + cx * cs, b.y0 + cy * cs, b.x0 + (cx + 1) * cs, b.y0 + (cy + 1) * cs); + let clipped = r.intersect(&b); + if self.spec.mask_incomplete && clipped != r { + return Some(()); + } + let col = self.cell_colour(clipped); + let n = self.src.ch; + let h = if self.spec.level_based { luma(self.ctx, &col[..n]).clamp(0.0, 1.0) } else { hash01(cx, cy, 21, self.spec.seed) }; + if !h.is_finite() { + return None; + } + let (mx, my) = (r.x0 as f32 + half, r.y0 as f32 + half); + let off = self.reach * h / self.half_diag; + let o = ((mx - self.centre.0) * off, (my - self.centre.1) * off); + // Two extra pixels conservatively cover rounding in the unchanged interval test. + let sweep = Rect::new( + (mx.min(mx + o.0) - half).floor() as i32 - 2, + (my.min(my + o.1) - half).floor() as i32 - 2, + (mx.max(mx + o.0) + half).ceil() as i32 + 2, + (my.max(my + o.1) + half).ceil() as i32 + 2, + ) + .intersect(&self.area); + let block = Block { r, cx, m: (mx, my), o, off, h, col }; + for y in sweep.y0..sweep.y1 { + let gy = (y - b.y0).div_euclid(cs); + if (cy - gy).abs() > self.k { + continue; + } + for x in sweep.x0..sweep.x1 { + self.paint_pixel(&block, x, y, heights, res)?; + } + } + Some(()) + } + + /// Paint pixel (`x`, `y`) if `block` covers it and is taller than what is there. + fn paint_pixel(&self, block: &Block, x: i32, y: i32, heights: &mut [f32], res: &mut [f32]) -> Option<()> { + let (b, cs, half, n) = (self.b, self.cs, self.half, self.src.ch); + let (mx, my) = block.m; + let (ox, oy) = block.o; + if (block.cx - (x - b.x0).div_euclid(cs)).abs() > self.k { + return Some(()); + } + let (fx, fy) = (x as f32 + 0.5, y as f32 + 0.5); + let i = (y - self.out.y0) as usize * self.out.width() as usize + (x - self.out.x0) as usize; + let height = heights.get_mut(i)?; + if *height >= block.h { + return Some(()); + } + let Some(iy) = interval(fy, my, oy, half) else { return Some(()) }; + let Some(ix) = interval(fx, mx, ox, half) else { return Some(()) }; + let (lo, hi) = (ix.0.max(iy.0), ix.1.min(iy.1)); + if lo > hi { + return Some(()); + } + *height = block.h; + let face = block_face(hi, ix.1 < iy.1, ox, oy); + let px = res.get_mut(i * n..(i + 1) * n)?; + self.write_face(block, face, (fx, fy), px) + } + + /// Write `face` of `block` into pixel `px` centred at `(fx, fy)`: the front + /// face shows the shifted source, every face can instead be flat-shaded. + fn write_face(&self, block: &Block, face: u8, (fx, fy): (f32, f32), px: &mut [f32]) -> Option<()> { + let n = self.src.ch; + if face == 0 && !self.spec.solid_front { + let (sx, sy) = ((fx - (block.m.0 - self.centre.0) * block.off).floor() as i32, (fy - (block.m.1 - self.centre.1) * block.off).floor() as i32); + read_px(self.src, sx.clamp(block.r.x0, block.r.x1 - 1), sy.clamp(block.r.y0, block.r.y1 - 1), px); + } else { + let shade = face_shade(face); + let sub = subtractive(self.ctx); + for (v, c) in px.iter_mut().zip(&block.col).take(ncol(self.ctx, n)) { + *v = if sub { 1.0 - (1.0 - *c) * shade } else { *c * shade }.clamp(0.0, 1.0); + } + if self.ctx.alpha { + *px.last_mut()? = block.col.get(n - 1).copied()?; + } + } + Some(()) + } +} + +/// Which face of the swept block a pixel shows: 0 front, 1/2 right/left, 3/4 bottom/top. +fn block_face(hi: f32, x_exits_first: bool, ox: f32, oy: f32) -> u8 { + if hi >= 1.0 { + 0 + } else if x_exits_first { + if ox > 0.0 { 1 } else { 2 } + } else if oy > 0.0 { + 3 + } else { + 4 + } +} + +fn face_shade(face: u8) -> f32 { + match face { + 0 => 1.0, + 1 => 0.85, + 2 => 0.55, + 3 => 0.95, + _ => 0.45, + } +} + fn interval(p: f32, m: f32, o: f32, half: f32) -> Option<(f32, f32)> { if o.abs() < 1e-6 { return ((p - m).abs() <= half).then_some((0.0, 1.0)); diff --git a/crates/algo/src/stylize2/tests.rs b/crates/algo/src/stylize2/tests.rs index 3d7f6b5235..4751511e62 100644 --- a/crates/algo/src/stylize2/tests.rs +++ b/crates/algo/src/stylize2/tests.rs @@ -26,17 +26,35 @@ fn bits(v: Vec) -> Vec { const MODELS: [ColorMode; 8] = [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab, ColorMode::Bitmap, ColorMode::Duotone, ColorMode::Indexed, ColorMode::Multichannel]; -#[test] -fn swept_blocks_match_the_pixel_search() { +// Keep the same daily/full-sweep cases, but expose four independent tests to nextest so one +// large reference comparison cannot hold a runner after its other tests have finished. +fn swept_blocks_match_the_pixel_search_part(part: u64) { let bounds = Rect::new(-13, -9, 31, 26); + // Unless the heights are level-based, the colour mode reaches extrude only through the channel + // count and whether the samples are CMYK ink, so modes that agree on both repeat those cases. + // A run takes a quarter of the rest, a different quarter each day. + let mut seen = std::collections::HashSet::new(); + let sweep = photocraft_testkit::Sweep::new(4); + let mut case = 0; + let mut selected = 0; for mode in MODELS { for alpha in [false, true] { let n = PixelFormat::new(mode, SampleType::F32, alpha).channels(); let ctx = Ctx { bounds, mode, alpha }; + let repeat = !seen.insert((n, alpha, mode == ColorMode::Cmyk)); for kind in 0..5 { let src = image(bounds.inflate(20), n, kind); for (size, depth) in [(2.0, 0.0), (2.0, 255.0), (3.5, 190.0), (8.0, 64.0), (22.0, 255.0), (255.0, 100.0)] { - for flags in 0..8 { + for flags in (0..8).filter(|f| !repeat || f & 1 != 0) { + case += 1; + if !sweep.includes(case) { + continue; + } + let assigned = selected % 4; + selected += 1; + if assigned != part { + continue; + } let spec = ExtrudeSpec { kind: ExtrudeType::Blocks, size, @@ -59,6 +77,26 @@ fn swept_blocks_match_the_pixel_search() { } } +#[test] +fn swept_blocks_match_the_pixel_search_part_0() { + swept_blocks_match_the_pixel_search_part(0); +} + +#[test] +fn swept_blocks_match_the_pixel_search_part_1() { + swept_blocks_match_the_pixel_search_part(1); +} + +#[test] +fn swept_blocks_match_the_pixel_search_part_2() { + swept_blocks_match_the_pixel_search_part(2); +} + +#[test] +fn swept_blocks_match_the_pixel_search_part_3() { + swept_blocks_match_the_pixel_search_part(3); +} + #[test] fn streak_scatter_matches_the_pixel_search() { let bounds = Rect::new(-17, -9, 39, 14); diff --git a/crates/algo/src/tests.rs b/crates/algo/src/tests.rs index 31def5f99d..57b7428330 100644 --- a/crates/algo/src/tests.rs +++ b/crates/algo/src/tests.rs @@ -1,5 +1,8 @@ use super::*; use photocraft_color::{PixelFormat, SampleType}; +mod ext; +mod mosaic; +mod selection_mix; fn fmt(s: SampleType) -> PixelFormat { PixelFormat::new(ColorMode::Rgb, s, true) @@ -90,6 +93,27 @@ fn radial_blur_quality_defaults_for_older_serialized_parameters() { assert_eq!(decoded, FilterParams::RadialBlur { amount: 20.0, method: RadialMethod::Spin, quality: RadialQuality::Good, center_x: 0.5, center_y: 0.5 }); } +#[test] +fn nonpositive_tile_sizes_match_one_pixel_tiles() { + let area = Rect::new(0, 0, 4, 3); + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let source = pattern(depth, area); + for params in all_filters().into_iter().chain([FilterParams::ShapeBlur { radius: 3.0, shape: BlurShape::Star }]) { + let expected = apply_tiled(&source, ¶ms, area, area, None, 1, Some(area)); + for tile in [0, -64, i32::MIN] { + let actual = apply_tiled(&source, ¶ms, area, area, None, tile, Some(area)); + assert_eq!(actual.read_region(area), expected.read_region(area), "{}: {depth:?}, tile={tile}", params.label()); + let cancellable = apply_tiled_with(&source, ¶ms, area, area, None, tile, Some(area), &photocraft_raster::Interrupt::NONE) + .expect("a nonpositive tile size is normalized, not rejected"); + assert_eq!(cancellable.read_region(area), expected.read_region(area), "{}: {depth:?}, tile={tile}", params.label()); + let cancelled = photocraft_raster::Interrupt::cancel_only(&|| true); + assert!(apply_tiled_with(&source, ¶ms, area, area, None, tile, Some(area), &cancelled).is_none()); + } + } + assert_eq!(source.read_region(area), pattern(depth, area).read_region(area)); + } +} + #[test] fn every_filter_is_tile_independent() { let s = pattern(SampleType::U8, R); @@ -248,6 +272,32 @@ fn minimum_maximum_spread() { assert!((mn.pixel(10, 10)[0] - 0.5).abs() < 1e-6); } +/// CMYK samples are ink, so Maximum spreads paper white and Minimum spreads ink, as Photoshop +/// does: Maximum r 1 removes a 1-px C 100% line on white, Minimum widens it to 3 px. Alpha keeps +/// the usual polarity (Maximum grows opaque areas, Minimum grows transparent ones). +#[test] +fn minimum_maximum_treat_cmyk_as_ink() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut s = Surface::new(PixelFormat::new(ColorMode::Cmyk, depth, true)); + s.fill_rect(R, &[0.0, 0.0, 0.0, 0.0, 1.0]); + s.fill_rect(Rect::new(20, 0, 21, 30), &[1.0, 0.0, 0.0, 0.0, 1.0]); + s.write_pixel(5, 5, &[0.0, 0.0, 0.0, 0.0, 0.0]); + for preserve in [Preserve::Squareness, Preserve::Roundness] { + let mx = run(&s, &FilterParams::Maximum { radius: 1.0, preserve }); + for x in 17..24 { + assert!(mx.pixel(x, 15)[0].abs() < 1e-6, "{depth:?} {preserve:?} Maximum C at x {x}: {:?}", mx.pixel(x, 15)); + } + assert!((mx.pixel(5, 5)[4] - 1.0).abs() < 1e-6, "{depth:?} {preserve:?} Maximum grows alpha"); + let mn = run(&s, &FilterParams::Minimum { radius: 1.0, preserve }); + for x in 17..24 { + let want = if (19..=21).contains(&x) { 1.0 } else { 0.0 }; + assert!((mn.pixel(x, 15)[0] - want).abs() < 1e-6, "{depth:?} {preserve:?} Minimum C at x {x}: {:?}", mn.pixel(x, 15)); + } + assert!(mn.pixel(6, 5)[4].abs() < 1e-6, "{depth:?} {preserve:?} Minimum shrinks alpha"); + } + } +} + #[test] fn maximum_roundness_grows_a_point_into_a_disc() { // One white pixel on black: Maximum spreads it over the structuring element itself. @@ -414,6 +464,24 @@ fn twirl_keeps_centre_and_outside() { assert_ne!(out.pixel(20, 12), s.pixel(20, 12)); } +/// #5643: on a wide image Twirl, Pinch and Spherize fill the ellipse inscribed in the bounds, as +/// Photoshop's do, not just a circle fitted to the height: a point near the left and right edges +/// on the middle row changes, the corners don't, and a square still uses its inscribed circle. +#[test] +fn round_distortions_fill_the_inscribed_ellipse() { + let b = Rect::new(0, 0, 80, 40); + let s = pattern(SampleType::F32, b); + for p in [FilterParams::Twirl { angle: 180.0 }, FilterParams::Pinch { amount: 80.0 }, FilterParams::Spherize { amount: 80.0, mode: SpherizeMode::Normal }] { + let out = apply(&s, &p, b, b, None); + // 30 px left of centre on the middle row: outside the old 20 px circle, inside the ellipse. + assert_ne!(out.pixel(10, 21), s.pixel(10, 21), "{p:?}"); + assert_ne!(out.pixel(69, 18), s.pixel(69, 18), "{p:?}"); + for (x, y) in [(0, 0), (79, 0), (0, 39), (79, 39)] { + assert_eq!(out.pixel(x, y), s.pixel(x, y), "{p:?}: corner ({x}, {y}) is outside the ellipse"); + } + } +} + #[test] fn motion_blur_spreads_along_angle_only() { let mut s = flat(SampleType::F32, R, [0.0, 0.0, 0.0, 1.0]); @@ -575,3 +643,12 @@ fn mix_selection_reads_the_selection_once_per_tile() { assert!(a.iter().zip(&b).all(|(x, y)| x.to_bits() == y.to_bits()), "alpha {alpha}: results differ"); } } + +#[test] +fn blur_halos_follow_the_clamped_radius_and_never_overflow() { + let smart = |radius| FilterParams::SmartBlur { radius, threshold: 25.0, quality: BlurQuality::default(), mode: SmartBlurMode::default() }; + assert_eq!(smart(1e9).halo(), Halo::Radius(101)); + assert_eq!(smart(f32::INFINITY).halo(), Halo::Radius(101)); + let gaussian = FilterParams::GaussianBlur { radius: f32::INFINITY }; + assert_eq!(gaussian.halo(), Halo::Radius(i32::MAX)); +} diff --git a/crates/algo/src/tests_ext.rs b/crates/algo/src/tests/ext.rs similarity index 92% rename from crates/algo/src/tests_ext.rs rename to crates/algo/src/tests/ext.rs index fd35f2742a..7c28485f1f 100644 --- a/crates/algo/src/tests_ext.rs +++ b/crates/algo/src/tests/ext.rs @@ -435,6 +435,35 @@ fn lens_flare_is_brightest_at_its_centre() { assert!(out.pixel(38, 2)[0] >= 0.2 - 1e-6, "screen never darkens"); } +#[test] +fn lens_flare_never_darkens_32_bit_values_above_one() { + // #5785: the screen formula turned 2.0 into 1.5 (and 1.0 at the core). + let p = FilterParams::LensFlare { brightness: 150.0, center_x: 0.25, center_y: 0.5, lens: LensType::Zoom }; + let out = run(&flat(SampleType::F32, R, [2.0, 2.0, 2.0, 1.0]), &p); + for px in out.read_region(R).as_chunks::<4>().0 { + assert!(px[..3].iter().all(|&v| v >= 2.0), "{px:?}"); + } +} + +#[test] +fn lens_flare_8_bit_result_is_the_screen_blend() { + // The flare amount per pixel, read from a black 32-bit image (screen over 0 is the amount). + let p = FilterParams::LensFlare { brightness: 150.0, center_x: 0.25, center_y: 0.5, lens: LensType::Zoom }; + let amount = run(&flat(SampleType::F32, R, [0.0, 0.0, 0.0, 1.0]), &p).read_region(R); + let src = pattern(SampleType::U8, R); + let base = src.read_region(R); + let expected: Vec = base + .as_chunks::<4>() + .0 + .iter() + .zip(amount.as_chunks::<4>().0) + .flat_map(|(c, a)| [0, 1, 2].map(|k| 1.0 - (1.0 - c[k]) * (1.0 - a[k].min(1.0))).into_iter().chain([c[3]])) + .collect(); + let mut want = Surface::new(fmt(SampleType::U8)); + want.write_region(R, &expected); + assert_eq!(run(&src, &p).read_region(R), want.read_region(R)); +} + #[test] fn lighting_point_light_falls_off() { let s = flat(SampleType::F32, R, [0.8, 0.8, 0.8, 1.0]); @@ -608,6 +637,28 @@ fn custom_kernel_shifts_and_offsets() { assert!((off.pixel(5, 5)[0] - 0.4).abs() < 1e-5); } +/// CMYK samples are ink: Custom's positive offset brightens (removes ink), as in Photoshop +/// (identity, scale 1, offset +50: white stays white, C=100% becomes C≈80%). Alpha is untouched. +/// Photoshop's CMYK depths; float is unclamped on both polarities (RGB white + offset exceeds 1). +#[test] +fn custom_offset_brightens_cmyk() { + let mut id = vec![0.0; 25]; + id[12] = 1.0; + let p = FilterParams::Custom { kernel: id, scale: 1.0, offset: 50.0 }; + for depth in [SampleType::U8, SampleType::U16] { + let f = PixelFormat::new(ColorMode::Cmyk, depth, true); + let mut s = Surface::new(f); + s.fill_rect(R, &[0.0, 0.0, 0.0, 0.0, 0.6]); + let w = run(&s, &p).pixel(5, 5); + assert!(w[..4].iter().all(|v| v.abs() < 1e-6) && (w[4] - 0.6).abs() < 0.01, "{depth:?} white: {w:?}"); + let mut s = Surface::new(f); + s.fill_rect(R, &[1.0, 0.5, 0.0, 0.0, 1.0]); + let c = run(&s, &p).pixel(5, 5); + let want = [1.0 - 50.0 / 255.0, 0.5 - 50.0 / 255.0, 0.0, 0.0, 1.0]; + assert!(c.iter().zip(want).all(|(a, b)| (a - b).abs() < 0.01), "{depth:?} C100: {c:?}"); + } +} + #[test] fn hsb_filter_encodes_and_decodes() { let s = flat(SampleType::F32, R, [1.0, 0.0, 0.0, 1.0]); diff --git a/crates/algo/src/tests_mosaic.rs b/crates/algo/src/tests/mosaic.rs similarity index 100% rename from crates/algo/src/tests_mosaic.rs rename to crates/algo/src/tests/mosaic.rs diff --git a/crates/algo/src/tests_selection_mix.rs b/crates/algo/src/tests/selection_mix.rs similarity index 100% rename from crates/algo/src/tests_selection_mix.rs rename to crates/algo/src/tests/selection_mix.rs diff --git a/crates/algo/src/tone.rs b/crates/algo/src/tone.rs index 2632d4a29a..5a0f752fac 100644 --- a/crates/algo/src/tone.rs +++ b/crates/algo/src/tone.rs @@ -65,6 +65,35 @@ pub fn replace_color(px: &mut [[f32; 4]], color: [f32; 3], fuzziness: f32, hue: mask } +/// Replace Color's Result swatch: `color` after the hue (±180°), saturation and lightness (±100) +/// shift, as a fully matched pixel of that colour becomes. +pub fn replace_color_result(color: [f32; 3], hue: f32, saturation: f32, lightness: f32) -> [f32; 3] { + let mut px = [[color[0], color[1], color[2], 1.0]]; + replace_color_masked(&mut px, &[1.0], hue, saturation, lightness); + [px[0][0], px[0][1], px[0][2]] +} + +/// The hue (±180°), saturation and lightness (±100) shift that turns `from` into `to`, as +/// Photoshop's Result Color Picker sets the sliders. A shift the sliders can't reach (a grey has +/// no hue to turn, black or white no saturation to give) goes as far as they do. +pub fn replace_color_shift(from: [f32; 3], to: [f32; 3]) -> (f32, f32, f32) { + let [fh, fs, fl] = rgb_to_hsl(from); + let [th, ts, tl] = rgb_to_hsl(to); + // Hue turns the short way round; a grey target keeps the hue where it is. + let hue = if ts <= 0.0 { 0.0 } else { ((th - fh + 0.5).rem_euclid(1.0) - 0.5) * 360.0 }; + // Inverse of the shift in `replace_color_masked`: up moves toward 1, down toward 0. + let amount = |from: f32, to: f32| { + if to >= from { + if from >= 1.0 { 0.0 } else { (to - from) / (1.0 - from) * 100.0 } + } else if from <= 0.0 { + 0.0 + } else { + (to / from - 1.0) * 100.0 + } + }; + (hue.clamp(-180.0, 180.0), amount(fs, ts).clamp(-100.0, 100.0), amount(fl, tl).clamp(-100.0, 100.0)) +} + /// Replace Color's hue, saturation and lightness shift, applied to each pixel as far as `mask` /// (`0..=1`, one per pixel: the colour match, localized or not) covers it. pub fn replace_color_masked(px: &mut [[f32; 4]], mask: &[f32], hue: f32, saturation: f32, lightness: f32) { @@ -277,6 +306,28 @@ pub fn hdr_toning(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning) { mod tests { use super::*; + /// #6278: the Result swatch is the sampled colour after the shift, and a picked Result sets + /// the sliders that give it. + #[test] + fn replace_color_result_and_shift_round_trip() { + let from = [0.8, 0.2, 0.2]; + let near = |a: [f32; 3], b: [f32; 3]| a.iter().zip(b).all(|(x, y)| (x - y).abs() < 2e-3); + assert!(near(replace_color_result(from, 0.0, 0.0, 0.0), from), "no shift, no change"); + for to in [[0.2, 0.3, 0.9], [0.9, 0.85, 0.1], [0.4, 0.1, 0.1], [0.95, 0.6, 0.6], [0.1, 0.6, 0.3]] { + let (h, s, l) = replace_color_shift(from, to); + assert!((-180.0..=180.0).contains(&h) && (-100.0..=100.0).contains(&s) && (-100.0..=100.0).contains(&l)); + assert!(near(replace_color_result(from, h, s, l), to), "{to:?}: ({h}, {s}, {l}) gives {:?}", replace_color_result(from, h, s, l)); + } + // Red to blue turns the short way (−120°, not +240°). + assert!((replace_color_shift([1.0, 0.0, 0.0], [0.0, 0.0, 1.0]).0 + 120.0).abs() < 0.5); + // A grey target: saturation all the way down, the hue left alone. + let (h, s, _) = replace_color_shift(from, [0.5, 0.5, 0.5]); + assert_eq!((h, s), (0.0, -100.0)); + // Black has no saturation to raise: the slider stays put rather than dividing by zero. + let (h, s, l) = replace_color_shift([0.0, 0.0, 0.0], [0.2, 0.6, 0.3]); + assert!(h.is_finite() && s.is_finite() && l > 0.0); + } + fn ramp(w: usize, h: usize) -> Vec<[f32; 4]> { (0..w * h) .map(|i| { diff --git a/crates/algo/src/transform.rs b/crates/algo/src/transform.rs index 050d08c322..1ed0ec7c04 100644 --- a/crates/algo/src/transform.rs +++ b/crates/algo/src/transform.rs @@ -231,21 +231,61 @@ pub fn quad_to_rect(q: &[[f64; 2]; 4], w: f64, h: f64) -> Option { /// The destination rectangle [`warp_surface`] fills for `src_rect` under `h`: the warped corners /// plus a pixel for filter support, each side clamped to ±2²⁰. `None` when a corner maps to -/// infinity (the result is then empty). Lets a caller price a warp before allocating it. +/// infinity (the result is then empty). Use [`warp_output_size`] to price the full result before +/// admitting a transform: clipping these bounds must not hide an excessive scale. pub fn warp_output_bounds(src_rect: Rect, h: &Homography) -> Option { + corner_bounds(&warp_corners(src_rect, h)?) +} + +/// Unclipped output dimensions, including the same filter support as [`warp_output_bounds`]. +/// Position does not affect the price, so translating a small layer far away stays cheap. +/// Unrepresentable or non-finite dimensions saturate at `u64::MAX`, allowing a caller to refuse +/// the warp with a saturating pixel-budget comparison. Empty and singular inputs have no output. +pub fn warp_output_size(src_rect: Rect, h: &Homography) -> Option<(u64, u64)> { + let corners = warp_corners(src_rect, h)?; + if !corners.iter().all(|(x, y)| x.is_finite() && y.is_finite()) { + return Some((u64::MAX, u64::MAX)); + } + let x0 = corners.iter().map(|c| c.0).fold(f64::MAX, f64::min).floor(); + let y0 = corners.iter().map(|c| c.1).fold(f64::MAX, f64::min).floor(); + let x1 = corners.iter().map(|c| c.0).fold(f64::MIN, f64::max).ceil(); + let y1 = corners.iter().map(|c| c.1).fold(f64::MIN, f64::max).ceil(); + // Float-to-integer casts saturate, including when the finite endpoints' difference overflows. + Some((((x1 - x0) as u64).saturating_add(2), ((y1 - y0) as u64).saturating_add(2))) +} + +/// The farthest from the origin (px) [`warp_surface`] places a pixel: each side of +/// [`warp_output_bounds`] is clamped to ±2²⁰. +pub const WARP_REACH: i32 = 1 << 20; + +/// Would [`warp_surface`] clip the result of warping `src_rect` by `h`: does a warped corner land +/// past [`WARP_REACH`], whether the result is that large or only placed that far out? A caller +/// refuses such a warp rather than lose the pixels beyond it. Empty and singular inputs have no +/// output to clip. +pub fn warp_output_clipped(src_rect: Rect, h: &Homography) -> bool { + let Some(corners) = warp_corners(src_rect, h) else { return false }; + let lim = f64::from(WARP_REACH); + let within = |v: f64| v.is_finite() && v.floor() >= -lim && v.ceil() <= lim; + !corners.iter().all(|&(x, y)| within(x) && within(y)) +} + +fn warp_corners(src_rect: Rect, h: &Homography) -> Option<[(f64, f64); 4]> { if src_rect.is_empty() || h.inverse().is_none() { return None; } - let corners = [(src_rect.x0, src_rect.y0), (src_rect.x1, src_rect.y0), (src_rect.x1, src_rect.y1), (src_rect.x0, src_rect.y1)] - .map(|(x, y)| h.apply(x as f64, y as f64)); - corner_bounds(&corners) + Some( + [(src_rect.x0, src_rect.y0), (src_rect.x1, src_rect.y0), (src_rect.x1, src_rect.y1), (src_rect.x0, src_rect.y1)] + .map(|(x, y)| h.apply(x as f64, y as f64)), + ) } +/// The pixel rectangle over the warped `corners` plus a pixel for filter support, each side +/// clamped to [`WARP_REACH`]; `None` when a corner is not finite. fn corner_bounds(corners: &[(f64, f64); 4]) -> Option { if !corners.iter().all(|c| c.0.is_finite() && c.1.is_finite()) { return None; } - let lim = (1 << 20) as f64; + let lim = f64::from(WARP_REACH); let bx0 = corners.iter().map(|c| c.0).fold(f64::MAX, f64::min).floor().max(-lim) as i32 - 1; let by0 = corners.iter().map(|c| c.1).fold(f64::MAX, f64::min).floor().max(-lim) as i32 - 1; let bx1 = corners.iter().map(|c| c.0).fold(f64::MIN, f64::max).ceil().min(lim) as i32 + 1; @@ -271,7 +311,7 @@ fn warp( converted = src.try_convert(fmt)?; &converted }; - let mut out = Surface::new(fmt); + let out = Surface::new(fmt); if src_rect.is_empty() || clip.is_some_and(|c| c.is_empty()) { return Ok(out); } @@ -310,7 +350,12 @@ fn warp( // A source straddling the horizon draws only its front side: the inverse w at a pixel has the // sign of the forward w at the source point it samples, and the other side would show as a // mirrored copy wrapped round infinity (#4863). - let front = side.is_none().then(|| h.front_side([src_rect.x0 as f64, src_rect.y0 as f64, src_rect.x1 as f64, src_rect.y1 as f64])); + // A clip wholly on one side of the inverse horizon only samples that side, so that side is drawn + // (a Perspective Crop low on a converging photo has most of the layer beyond its horizon, #7557). + let clip_w = clip.map(|c| [(c.x0, c.y0), (c.x1, c.y0), (c.x1, c.y1), (c.x0, c.y1)].map(|(x, y)| inv.w(x as f64, y as f64))); + let clip_side = clip_w.and_then(|cw| [1.0, -1.0].into_iter().find(|&s| cw.iter().all(|&w| s * w > 0.0))); + let front = + side.is_none().then(|| clip_side.unwrap_or_else(|| h.front_side([src_rect.x0 as f64, src_rect.y0 as f64, src_rect.x1 as f64, src_rect.y1 as f64]))); let bounds = corner_bounds(&corners); let dst = match (clip, bounds) { // The warped corners only bound the result while the whole source is in front of the horizon. @@ -338,7 +383,7 @@ fn warp( return Ok(None); } let k = finished.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; - // The last few percent are the writes. + // Short of 100 %: the result is still pruned and handed back after the last tile. ctl.progress(0.98 * k as f32 / total as f32); // Source footprint of this tile (inverse-mapped corners of its sampled part), padded for the filter. let tc = [(t.x0, t.y0), (t.x1, t.y0), (t.x1, t.y1), (t.x0, t.y1)].map(|(x, y)| (x as f64, y as f64)); @@ -467,310 +512,43 @@ fn warp( any = true; let o = ((y - t.y0) as usize * w + (x - t.x0) as usize) * n; for c in 0..a { - // Integer storage clips on write; float colours retain their signed/HDR range. - outp[o + c] = (acc[c] / al) as f32; + // Un-premultiply by the unclamped alpha: bicubic overshoots it past 1 next to + // partly transparent pixels, and dividing by the clamped 1 would lighten the + // colour by the overshoot (#6203). Integer storage clips on write; float + // colours retain their signed/HDR range. + outp[o + c] = (acc[c] / acc[a]) as f32; } outp[o + a] = al as f32; } } Ok(any.then_some((*t, outp))) }; - let result_bytes = tiles.len().checked_mul(std::mem::size_of::<(Rect, Vec)>()).ok_or(photocraft_raster::AllocationError::SizeOverflow)?; - let mut done = Vec::new(); - done.try_reserve_exact(tiles.len()).map_err(|_| photocraft_raster::AllocationError::NotEnoughMemory { bytes: result_bytes })?; - let done = std::sync::Mutex::new(done); + // Each tile is written as soon as it is warped, so only the tiles in flight hold their `f32` + // pixels: kept until the last one was done, they cost 16 bytes a pixel of an RGBA result on + // top of the result's own (#7807). + let out = std::sync::Mutex::new(out); + let warp_and_write = |tile: &Rect| -> Result<(), photocraft_raster::AllocationError> { + if let Some((r, v)) = work(tile)? { + out.lock().unwrap_or_else(std::sync::PoisonError::into_inner).try_write_region(r, &v)?; + } + Ok(()) + }; #[cfg(not(target_arch = "wasm32"))] { use rayon::prelude::*; - tiles.into_par_iter().try_for_each(|tile| -> Result<(), photocraft_raster::AllocationError> { - if let Some(result) = work(&tile)? { - done.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(result); - } - Ok(()) - })?; + tiles.into_par_iter().try_for_each(|tile| warp_and_write(&tile))?; } #[cfg(target_arch = "wasm32")] for tile in tiles { - if let Some(result) = work(&tile)? { - done.lock().unwrap_or_else(std::sync::PoisonError::into_inner).push(result); - } + warp_and_write(&tile)?; } - let done = done.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner); if ctl.cancelled() { - return Ok(out); - } - for (r, v) in done { - out.try_write_region(r, &v)?; + return Ok(Surface::new(fmt)); } + let mut out = out.into_inner().unwrap_or_else(std::sync::PoisonError::into_inner); out.prune(); Ok(out) } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn the_interruptible_warp_reports_progress_and_stops_when_cancelled() { - let mut s = Surface::new(PixelFormat::RGBA8); - s.fill_rect(Rect::new(0, 0, 600, 400), &[0.2, 0.4, 0.6, 1.0]); - let r = s.content_bounds(); - let h = Homography::rect_to_quad([0.0, 0.0, 600.0, 400.0], [[10.0, 0.0], [590.0, 20.0], [600.0, 400.0], [0.0, 380.0]]).unwrap(); - let reported = std::sync::Mutex::new(0.0f32); - let progress = |p: f32| { - let mut m = reported.lock().unwrap(); - *m = m.max(p); - }; - let never = || false; - let out = warp_surface_with(&s, r, &h, Interp::Bicubic, &Interrupt::new(&never, &progress)).unwrap().expect("not cancelled"); - assert_eq!(out.read_region(r), warp_surface(&s, r, &h, Interp::Bicubic).unwrap().read_region(r)); - assert!(*reported.lock().unwrap() > 0.9); - let always = || true; - assert!(warp_surface_with(&s, r, &h, Interp::Bicubic, &Interrupt::cancel_only(&always)).unwrap().is_none()); - } - - /// Perspective Crop's map lands each quad corner exactly on its rectangle corner; degenerate, - /// self-crossing, counter-clockwise and non-finite quads have none. - #[test] - fn quad_to_rect_maps_corners_and_refuses_bad_quads() { - let q = [[30.0, 12.0], [140.0, 25.0], [128.0, 110.0], [14.0, 95.0]]; - let h = quad_to_rect(&q, 100.0, 80.0).unwrap(); - for (c, want) in q.iter().zip([[0.0, 0.0], [100.0, 0.0], [100.0, 80.0], [0.0, 80.0]]) { - let (x, y) = h.apply(c[0], c[1]); - assert!((x - want[0]).abs() < 1e-9 && (y - want[1]).abs() < 1e-9, "{c:?} -> ({x}, {y})"); - } - let (w, ht) = quad_size(&q); - assert!((w - 112.874).abs() < 1e-3 && (ht - 85.185).abs() < 1e-3, "{w} x {ht}"); - for bad in [ - [[0.0, 0.0], [50.0, 0.0], [100.0, 0.0], [0.0, 50.0]], - [[0.0, 0.0], [50.0, 50.0], [50.0, 0.0], [0.0, 50.0]], - [[0.0, 0.0], [0.0, 50.0], [50.0, 50.0], [50.0, 0.0]], - [[0.0, 0.0], [f64::NAN, 0.0], [50.0, 50.0], [0.0, 50.0]], - [[0.0, 0.0]; 4], - ] { - assert!(quad_to_rect(&bad, 10.0, 10.0).is_none(), "{bad:?}"); - } - assert!(quad_to_rect(&q, 0.0, 10.0).is_none()); - } - - fn rgba() -> Surface { - Surface::new(PixelFormat::RGBA8) - } - - /// The "pixel stretch" (#3773): a 1 px column scaled 100x stays solid across the stretch, as in - /// Photoshop. Samples past the source edge repeat the edge, and only the outline is antialiased. - #[test] - fn stretched_single_column_stays_opaque() { - let mut s = rgba(); - s.fill_rect(Rect::new(0, 0, 1, 4), &[0.0, 1.0, 0.0, 1.0]); - for interp in [Interp::Bicubic, Interp::Bilinear] { - let h = Homography::rect_to_quad([0.0, 0.0, 1.0, 4.0], [[0.0, 0.0], [100.0, 0.0], [100.0, 4.0], [0.0, 4.0]]).unwrap(); - let w = warp_surface(&s, Rect::new(0, 0, 1, 4), &h, interp).unwrap(); - assert_eq!(w.content_bounds(), Rect::new(0, 0, 100, 4), "{interp:?}"); - for x in [0, 1, 25, 50, 75, 99] { - for y in 0..4 { - assert!((w.sample_channel(x, y, 3) - 1.0).abs() < 1e-6, "{interp:?} alpha at ({x}, {y}) = {}", w.sample_channel(x, y, 3)); - } - } - } - } - - /// Photoshop 27.11's Free Transform, Bicubic, 200 % about the centre of a 64 px image whose left - /// half is grey 64 and right half grey 192: the row across the edge, columns 26..36. - #[test] - fn bicubic_upscale_matches_photoshop() { - let mut src = rgba(); - let g = |v: f32| v / 255.0; - src.fill_rect(Rect::new(0, 0, 32, 64), &[g(64.0), g(64.0), g(64.0), 1.0]); - src.fill_rect(Rect::new(32, 0, 64, 64), &[g(192.0), g(192.0), g(192.0), 1.0]); - let q = [[-32.0, -32.0], [96.0, -32.0], [96.0, 96.0], [-32.0, 96.0]]; - let h = Homography::rect_to_quad([0.0, 0.0, 64.0, 64.0], q).unwrap(); - let out = warp_surface(&src, Rect::new(0, 0, 64, 64), &h, Interp::Bicubic).unwrap(); - let photoshop = [64.0, 64.0, 64.0, 60.0, 51.0, 93.0, 163.0, 206.0, 197.0, 192.0]; - for (i, want) in photoshop.iter().enumerate() { - let got = out.pixel(26 + i as i32, 50)[0] * 255.0; - assert!((got - want).abs() <= 1.5, "column {}: {got} vs {want}", 26 + i); - } - } - - #[test] - fn homography_maps_rect_corners_and_inverts() { - let r = [10.0, 20.0, 110.0, 70.0]; - let q = [[0.0, 0.0], [200.0, 10.0], [180.0, 150.0], [-20.0, 120.0]]; - let h = Homography::rect_to_quad(r, q).unwrap(); - for (p, e) in [(10.0, 20.0), (110.0, 20.0), (110.0, 70.0), (10.0, 70.0)].iter().zip(q) { - let (x, y) = h.apply(p.0, p.1); - assert!((x - e[0]).abs() < 1e-9 && (y - e[1]).abs() < 1e-9, "{p:?} → {x},{y} vs {e:?}"); - } - let inv = h.inverse().unwrap(); - let (x, y) = inv.apply(90.0, 60.0); - let (bx, by) = h.apply(x, y); - assert!((bx - 90.0).abs() < 1e-9 && (by - 60.0).abs() < 1e-9); - assert!(Homography::rect_to_quad(r, [[0.0, 0.0]; 4]).is_none() || Homography::rect_to_quad(r, [[0.0, 0.0]; 4]).unwrap().inverse().is_none()); - } - - #[test] - fn steep_distort_keeps_every_pixel_inside_the_quad() { - // Corner C is almost flat, so the horizon of the inverse map crosses the 256-px tiles that - // also hold quad pixels; those tiles must still read every source pixel that lands in them. - let mut s = rgba(); - s.fill_rect(Rect::new(0, 0, 100, 100), &[1.0, 0.0, 0.0, 1.0]); - let r = s.content_bounds(); - let h = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [155.0, 155.0], [0.0, 300.0]]).unwrap(); - let w = warp_surface(&s, r, &h, Interp::Nearest).unwrap(); - for (x, y) in [(20, 20), (252, 14), (252, 42), (14, 252), (42, 252)] { - let a = w.read_region(Rect::new(x, y, x + 1, y + 1))[3]; - assert!(a > 0.99, "({x},{y}) inside the quad is transparent: {a}"); - } - for (x, y) in [(200, 200), (280, 100), (100, 280)] { - assert_eq!(w.read_region(Rect::new(x, y, x + 1, y + 1))[3], 0.0, "({x},{y}) outside the quad is painted"); - } - } - - /// #4863: a Distort with the bottom corners pulled in and one dragged past the horizon (a - /// concave quad) leaves part of the source behind the horizon. That part maps to the far side, - /// where it showed as a mirrored copy wrapped round infinity; only the front side is drawn. - #[test] - fn a_distort_past_the_horizon_draws_only_its_front_side() { - let mut s = rgba(); - s.fill_rect(Rect::new(0, 0, 100, 100), &[0.0, 0.5, 1.0, 1.0]); - let r = s.content_bounds(); - let h = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [60.0, 60.0], [0.0, 300.0]]).unwrap(); - let corners = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)].map(|(x, y)| h.w(x, y)); - assert!(corners.iter().any(|w| *w > 0.0) && corners.iter().any(|w| *w < 0.0), "the source straddles the horizon: {corners:?}"); - let front = h.front_side([0.0, 0.0, 100.0, 100.0]); - let inv = h.inverse().unwrap(); - let clip = Rect::new(-400, -400, 700, 700); - for interp in [Interp::Nearest, Interp::Bilinear] { - let w = warp_surface_clipped(&s, r, &h, interp, clip).unwrap(); - let px = w.read_region(clip); - let (mut drawn, mut behind) = (0, 0); - for y in clip.y0..clip.y1 { - for x in clip.x0..clip.x1 { - let i = ((y - clip.y0) * clip.width() as i32 + (x - clip.x0)) as usize * 4; - if px[i + 3] > 0.0 { - drawn += 1; - // The source point this pixel samples is on the far side of the horizon. - if front * inv.w(x as f64 + 0.5, y as f64 + 0.5) <= 0.0 { - behind += 1; - } - } - } - } - assert!(drawn > 1000, "{interp:?}: the front side is drawn ({drawn} px)"); - assert_eq!(behind, 0, "{interp:?}: {behind} pixels come from behind the horizon"); - } - // A quad wholly in front of the horizon is drawn exactly as before. - let ok = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [155.0, 155.0], [0.0, 300.0]]).unwrap(); - assert_eq!(ok.front_side([0.0, 0.0, 100.0, 100.0]), ok.w(50.0, 50.0).signum()); - } - - #[test] - fn identity_and_translation_are_exact() { - let mut s = rgba(); - s.fill_rect(Rect::new(4, 4, 20, 12), &[1.0, 0.5, 0.0, 1.0]); - let r = s.content_bounds(); - for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { - let h = Homography([1.0, 0.0, 7.0, 0.0, 1.0, -3.0, 0.0, 0.0, 1.0]); - let o = warp_surface(&s, r, &h, interp).unwrap(); - assert_eq!(o.content_bounds(), Rect::new(11, 1, 27, 9), "{interp:?}"); - assert_eq!(o.pixel(15, 5), vec![1.0, 128.0 / 255.0, 0.0, 1.0], "{interp:?}"); - } - } - - #[test] - fn translation_preserves_colour_range_at_every_depth() { - use photocraft_color::{ColorMode, SampleType}; - - let h = Homography([1.0, 0.0, 7.0, 0.0, 1.0, -3.0, 0.0, 0.0, 1.0]); - for sample in SampleType::ALL { - for mode in [ColorMode::Rgb, ColorMode::Grayscale] { - for alpha in [false, true] { - let mut s = Surface::new(PixelFormat::new(mode, sample, alpha)); - let mut pixel = if mode == ColorMode::Rgb { vec![2.0, -0.25, 0.5] } else { vec![2.0] }; - if alpha { - pixel.push(0.5); - } - s.fill_rect(Rect::new(4, 4, 8, 8), &pixel); - let mut expected = s.pixel(5, 5); - if !alpha { - expected.push(1.0); - } - for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { - let out = warp_surface(&s, s.content_bounds(), &h, interp).unwrap(); - assert_eq!(out.pixel(12, 2), expected, "{sample:?}/{mode:?}/alpha={alpha}/{interp:?}"); - assert_eq!(out.pixel(0, 0).last(), Some(&0.0)); - } - } - } - } - } - - #[test] - fn bilinear_translation_interpolates_signed_hdr_colours() { - let mut s = Surface::new(PixelFormat::RGBA32F); - s.write_pixel(0, 0, &[2.0, -0.5, 0.25, 0.5]); - s.write_pixel(1, 0, &[4.0, -0.25, 0.75, 0.5]); - let h = Homography([1.0, 0.0, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); - let out = warp_surface(&s, s.content_bounds(), &h, Interp::Bilinear).unwrap(); - assert_eq!(out.pixel(1, 0), vec![3.0, -0.375, 0.5, 0.5]); - } - - #[test] - fn scale_doubles_size_and_rotation_keeps_area() { - let mut s = rgba(); - s.fill_rect(Rect::new(0, 0, 40, 20), &[0.2, 0.4, 0.6, 1.0]); - let r = s.content_bounds(); - let up = warp_surface( - &s, - r, - &Homography::rect_to_quad([0.0, 0.0, 40.0, 20.0], [[0.0, 0.0], [80.0, 0.0], [80.0, 40.0], [0.0, 40.0]]).unwrap(), - Interp::Bicubic, - ) - .unwrap(); - let b = up.content_bounds(); - assert!(b.width() >= 80 && b.width() <= 82 && b.height() >= 40 && b.height() <= 42, "{b:?}"); - // 90° rotation about (20, 10): a 40×20 box becomes 20×40. - let q = [[30.0, -10.0], [30.0, 30.0], [10.0, 30.0], [10.0, -10.0]]; - let rot = warp_surface(&s, r, &Homography::rect_to_quad([0.0, 0.0, 40.0, 20.0], q).unwrap(), Interp::Bilinear).unwrap(); - let b = rot.content_bounds(); - assert!(b.width().abs_diff(20) <= 2 && b.height().abs_diff(40) <= 2, "{b:?}"); - let p = rot.pixel(20, 10); - assert!((p[0] - 0.2).abs() < 0.01 && p[3] > 0.99, "{p:?}"); - } - - #[test] - fn large_downscale_is_prefiltered() { - // A 1px checkerboard shrunk 8× must come out ~50% grey, not aliased black/white. - let mut s = rgba(); - for y in 0..64 { - for x in 0..64 { - let v = ((x + y) % 2) as f32; - s.fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, v, v, 1.0]); - } - } - let h = Homography::rect_to_quad([0.0, 0.0, 64.0, 64.0], [[0.0, 0.0], [8.0, 0.0], [8.0, 8.0], [0.0, 8.0]]).unwrap(); - let o = warp_surface(&s, s.content_bounds(), &h, Interp::Bicubic).unwrap(); - let p = o.pixel(4, 4); - assert!((p[0] - 0.5).abs() < 0.1, "{p:?}"); - } - - #[test] - fn surfaces_without_alpha_gain_it() { - let mut s = Surface::new(PixelFormat::new(photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8, false)); - s.fill_rect(Rect::new(0, 0, 10, 10), &[1.0, 0.0, 0.0]); - let h = Homography([1.0, 0.0, 5.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); - let o = warp_surface(&s, Rect::new(0, 0, 10, 10), &h, Interp::Nearest).unwrap(); - assert!(o.format().alpha); - assert_eq!(o.pixel(2, 2)[3], 0.0); - assert_eq!(o.pixel(7, 2), vec![1.0, 0.0, 0.0, 1.0]); - } - - #[test] - fn homography_is_identity() { - assert!(Homography::IDENTITY.is_identity()); - assert!(Homography([2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0]).is_identity()); - assert!(!Homography([1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]).is_identity()); - assert!(!Homography([1.0, 0.1, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]).is_identity()); - } -} +mod tests; diff --git a/crates/algo/src/transform/tests.rs b/crates/algo/src/transform/tests.rs new file mode 100644 index 0000000000..b70bc74c3d --- /dev/null +++ b/crates/algo/src/transform/tests.rs @@ -0,0 +1,344 @@ +use super::*; + +#[test] +fn output_size_prices_the_unclipped_extent_independently_of_position() { + let src = Rect::new(0, 0, 24, 16); + for dx in [0.0, 900_000.0, 10_000_000.0] { + let h = Homography([1.0, 0.0, dx, 0.0, 1.0, -dx, 0.0, 0.0, 1.0]); + assert_eq!(warp_output_size(src, &h), Some((26, 18))); + } + for sx in [1e7, -1e7] { + let h = Homography([sx, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + assert_eq!(warp_output_size(src, &h), Some((240_000_002, 18))); + } +} + +/// The unclipped size matches the clipped bounds where nothing is clipped, saturates when it +/// can't be represented, and is `None` for empty or singular inputs. +#[test] +fn output_size_saturates_overflows_and_preserves_filter_support() { + let src = Rect::new(0, 0, 24, 16); + let fractional = Homography([0.5, 0.0, 0.25, 0.0, 1.0, 0.75, 0.0, 0.0, 1.0]); + let bounds = warp_output_bounds(src, &fractional).unwrap(); + assert_eq!(warp_output_size(src, &fractional), Some((u64::from(bounds.width()), u64::from(bounds.height())))); + let huge = Homography([1e308, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + assert_eq!(warp_output_size(src, &huge), Some((u64::MAX, u64::MAX))); + assert_eq!(warp_output_size(Rect::EMPTY, &Homography::IDENTITY), None); + assert_eq!(warp_output_size(src, &Homography([0.0; 9])), None); +} + +/// [`warp_output_clipped`] says yes exactly when [`warp_output_bounds`] clamps a side (#7807). +#[test] +fn output_clipped_matches_the_clamped_bounds() { + let src = Rect::new(0, 0, 24, 16); + let at = |dx: f64| Homography([1.0, 0.0, dx, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + let edge = f64::from(WARP_REACH) - 24.0; + assert!(!warp_output_clipped(src, &at(edge)), "the right edge lands on the reach"); + assert_eq!(warp_output_bounds(src, &at(edge)).map(|b| b.x1), Some(WARP_REACH + 1)); + assert!(warp_output_clipped(src, &at(edge + 1.0)), "one pixel past it"); + assert_eq!(warp_output_bounds(src, &at(edge + 1.0)).map(|b| b.x1), Some(WARP_REACH + 1), "clamped"); + assert!(warp_output_clipped(src, &at(-f64::from(WARP_REACH) - 1.0))); + assert!(!warp_output_clipped(Rect::EMPTY, &at(1e12)), "nothing to clip"); +} + +#[test] +fn the_interruptible_warp_reports_progress_and_stops_when_cancelled() { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 600, 400), &[0.2, 0.4, 0.6, 1.0]); + let r = s.content_bounds(); + let h = Homography::rect_to_quad([0.0, 0.0, 600.0, 400.0], [[10.0, 0.0], [590.0, 20.0], [600.0, 400.0], [0.0, 380.0]]).unwrap(); + let reported = std::sync::Mutex::new(0.0f32); + let progress = |p: f32| { + let mut m = reported.lock().unwrap(); + *m = m.max(p); + }; + let never = || false; + let out = warp_surface_with(&s, r, &h, Interp::Bicubic, &Interrupt::new(&never, &progress)).unwrap().expect("not cancelled"); + assert_eq!(out.read_region(r), warp_surface(&s, r, &h, Interp::Bicubic).unwrap().read_region(r)); + assert!(*reported.lock().unwrap() > 0.9); + let always = || true; + assert!(warp_surface_with(&s, r, &h, Interp::Bicubic, &Interrupt::cancel_only(&always)).unwrap().is_none()); +} + +/// Perspective Crop's map lands each quad corner exactly on its rectangle corner; degenerate, +/// self-crossing, counter-clockwise and non-finite quads have none. +#[test] +fn quad_to_rect_maps_corners_and_refuses_bad_quads() { + let q = [[30.0, 12.0], [140.0, 25.0], [128.0, 110.0], [14.0, 95.0]]; + let h = quad_to_rect(&q, 100.0, 80.0).unwrap(); + for (c, want) in q.iter().zip([[0.0, 0.0], [100.0, 0.0], [100.0, 80.0], [0.0, 80.0]]) { + let (x, y) = h.apply(c[0], c[1]); + assert!((x - want[0]).abs() < 1e-9 && (y - want[1]).abs() < 1e-9, "{c:?} -> ({x}, {y})"); + } + let (w, ht) = quad_size(&q); + assert!((w - 112.874).abs() < 1e-3 && (ht - 85.185).abs() < 1e-3, "{w} x {ht}"); + for bad in [ + [[0.0, 0.0], [50.0, 0.0], [100.0, 0.0], [0.0, 50.0]], + [[0.0, 0.0], [50.0, 50.0], [50.0, 0.0], [0.0, 50.0]], + [[0.0, 0.0], [0.0, 50.0], [50.0, 50.0], [50.0, 0.0]], + [[0.0, 0.0], [f64::NAN, 0.0], [50.0, 50.0], [0.0, 50.0]], + [[0.0, 0.0]; 4], + ] { + assert!(quad_to_rect(&bad, 10.0, 10.0).is_none(), "{bad:?}"); + } + assert!(quad_to_rect(&q, 0.0, 10.0).is_none()); +} + +fn rgba() -> Surface { + Surface::new(PixelFormat::RGBA8) +} + +/// The "pixel stretch" (#3773): a 1 px column scaled 100x stays solid across the stretch, as in +/// Photoshop. Samples past the source edge repeat the edge, and only the outline is antialiased. +#[test] +fn stretched_single_column_stays_opaque() { + let mut s = rgba(); + s.fill_rect(Rect::new(0, 0, 1, 4), &[0.0, 1.0, 0.0, 1.0]); + for interp in [Interp::Bicubic, Interp::Bilinear] { + let h = Homography::rect_to_quad([0.0, 0.0, 1.0, 4.0], [[0.0, 0.0], [100.0, 0.0], [100.0, 4.0], [0.0, 4.0]]).unwrap(); + let w = warp_surface(&s, Rect::new(0, 0, 1, 4), &h, interp).unwrap(); + assert_eq!(w.content_bounds(), Rect::new(0, 0, 100, 4), "{interp:?}"); + for x in [0, 1, 25, 50, 75, 99] { + for y in 0..4 { + assert!((w.sample_channel(x, y, 3) - 1.0).abs() < 1e-6, "{interp:?} alpha at ({x}, {y}) = {}", w.sample_channel(x, y, 3)); + } + } + } +} + +/// Photoshop 27.11's Free Transform, Bicubic, 200 % about the centre of a 64 px image whose left +/// half is grey 64 and right half grey 192: the row across the edge, columns 26..36. +#[test] +fn bicubic_upscale_matches_photoshop() { + let mut src = rgba(); + let g = |v: f32| v / 255.0; + src.fill_rect(Rect::new(0, 0, 32, 64), &[g(64.0), g(64.0), g(64.0), 1.0]); + src.fill_rect(Rect::new(32, 0, 64, 64), &[g(192.0), g(192.0), g(192.0), 1.0]); + let q = [[-32.0, -32.0], [96.0, -32.0], [96.0, 96.0], [-32.0, 96.0]]; + let h = Homography::rect_to_quad([0.0, 0.0, 64.0, 64.0], q).unwrap(); + let out = warp_surface(&src, Rect::new(0, 0, 64, 64), &h, Interp::Bicubic).unwrap(); + let photoshop = [64.0, 64.0, 64.0, 60.0, 51.0, 93.0, 163.0, 206.0, 197.0, 192.0]; + for (i, want) in photoshop.iter().enumerate() { + let got = out.pixel(26 + i as i32, 50)[0] * 255.0; + assert!((got - want).abs() <= 1.5, "column {}: {got} vs {want}", 26 + i); + } +} + +#[test] +fn homography_maps_rect_corners_and_inverts() { + let r = [10.0, 20.0, 110.0, 70.0]; + let q = [[0.0, 0.0], [200.0, 10.0], [180.0, 150.0], [-20.0, 120.0]]; + let h = Homography::rect_to_quad(r, q).unwrap(); + for (p, e) in [(10.0, 20.0), (110.0, 20.0), (110.0, 70.0), (10.0, 70.0)].iter().zip(q) { + let (x, y) = h.apply(p.0, p.1); + assert!((x - e[0]).abs() < 1e-9 && (y - e[1]).abs() < 1e-9, "{p:?} → {x},{y} vs {e:?}"); + } + let inv = h.inverse().unwrap(); + let (x, y) = inv.apply(90.0, 60.0); + let (bx, by) = h.apply(x, y); + assert!((bx - 90.0).abs() < 1e-9 && (by - 60.0).abs() < 1e-9); + assert!(Homography::rect_to_quad(r, [[0.0, 0.0]; 4]).is_none() || Homography::rect_to_quad(r, [[0.0, 0.0]; 4]).unwrap().inverse().is_none()); +} + +#[test] +fn steep_distort_keeps_every_pixel_inside_the_quad() { + // Corner C is almost flat, so the horizon of the inverse map crosses the 256-px tiles that + // also hold quad pixels; those tiles must still read every source pixel that lands in them. + let mut s = rgba(); + s.fill_rect(Rect::new(0, 0, 100, 100), &[1.0, 0.0, 0.0, 1.0]); + let r = s.content_bounds(); + let h = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [155.0, 155.0], [0.0, 300.0]]).unwrap(); + let w = warp_surface(&s, r, &h, Interp::Nearest).unwrap(); + for (x, y) in [(20, 20), (252, 14), (252, 42), (14, 252), (42, 252)] { + let a = w.read_region(Rect::new(x, y, x + 1, y + 1))[3]; + assert!(a > 0.99, "({x},{y}) inside the quad is transparent: {a}"); + } + for (x, y) in [(200, 200), (280, 100), (100, 280)] { + assert_eq!(w.read_region(Rect::new(x, y, x + 1, y + 1))[3], 0.0, "({x},{y}) outside the quad is painted"); + } +} + +/// #4863: a Distort with the bottom corners pulled in and one dragged past the horizon (a +/// concave quad) leaves part of the source behind the horizon. That part maps to the far side, +/// where it showed as a mirrored copy wrapped round infinity; only the front side is drawn. +#[test] +fn a_distort_past_the_horizon_draws_only_its_front_side() { + let mut s = rgba(); + s.fill_rect(Rect::new(0, 0, 100, 100), &[0.0, 0.5, 1.0, 1.0]); + let r = s.content_bounds(); + let h = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [60.0, 60.0], [0.0, 300.0]]).unwrap(); + let corners = [(0.0, 0.0), (100.0, 0.0), (100.0, 100.0), (0.0, 100.0)].map(|(x, y)| h.w(x, y)); + assert!(corners.iter().any(|w| *w > 0.0) && corners.iter().any(|w| *w < 0.0), "the source straddles the horizon: {corners:?}"); + let front = h.front_side([0.0, 0.0, 100.0, 100.0]); + let inv = h.inverse().unwrap(); + let clip = Rect::new(-400, -400, 700, 700); + for interp in [Interp::Nearest, Interp::Bilinear] { + let w = warp_surface_clipped(&s, r, &h, interp, clip).unwrap(); + let px = w.read_region(clip); + let (mut drawn, mut behind) = (0, 0); + for y in clip.y0..clip.y1 { + for x in clip.x0..clip.x1 { + let i = ((y - clip.y0) * clip.width() as i32 + (x - clip.x0)) as usize * 4; + if px[i + 3] > 0.0 { + drawn += 1; + // The source point this pixel samples is on the far side of the horizon. + if front * inv.w(x as f64 + 0.5, y as f64 + 0.5) <= 0.0 { + behind += 1; + } + } + } + } + assert!(drawn > 1000, "{interp:?}: the front side is drawn ({drawn} px)"); + assert_eq!(behind, 0, "{interp:?}: {behind} pixels come from behind the horizon"); + } + // A quad wholly in front of the horizon is drawn exactly as before. + let ok = Homography::rect_to_quad([0.0, 0.0, 100.0, 100.0], [[0.0, 0.0], [300.0, 0.0], [155.0, 155.0], [0.0, 300.0]]).unwrap(); + assert_eq!(ok.front_side([0.0, 0.0, 100.0, 100.0]), ok.w(50.0, 50.0).signum()); +} + +/// #7557: a Perspective Crop quad low on a strongly converging photo puts the horizon across +/// the layer with most of it (and its centre) on the far side. The clip maps wholly into the +/// quad, so its side is the one drawn, not the source's majority. +#[test] +fn a_clip_wholly_on_one_side_draws_that_side() { + let mut s = rgba(); + s.fill_rect(Rect::new(0, 0, 100, 100), &[1.0, 0.0, 0.0, 1.0]); + let q = [[40.0, 70.0], [60.0, 70.0], [90.0, 95.0], [10.0, 95.0]]; + let h = quad_to_rect(&q, 50.0, 39.0).unwrap(); + assert!(h.front_side([0.0, 0.0, 100.0, 100.0]) * h.w(50.0, 80.0) < 0.0, "the source's majority is behind the quad's side"); + for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { + let w = warp_surface_clipped(&s, s.content_bounds(), &h, interp, Rect::new(0, 0, 50, 39)).unwrap(); + assert_eq!(w.rgba(25, 20), [1.0, 0.0, 0.0, 1.0], "{interp:?}"); + } +} + +#[test] +fn identity_and_translation_are_exact() { + let mut s = rgba(); + s.fill_rect(Rect::new(4, 4, 20, 12), &[1.0, 0.5, 0.0, 1.0]); + let r = s.content_bounds(); + for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { + let h = Homography([1.0, 0.0, 7.0, 0.0, 1.0, -3.0, 0.0, 0.0, 1.0]); + let o = warp_surface(&s, r, &h, interp).unwrap(); + assert_eq!(o.content_bounds(), Rect::new(11, 1, 27, 9), "{interp:?}"); + assert_eq!(o.pixel(15, 5), vec![1.0, 128.0 / 255.0, 0.0, 1.0], "{interp:?}"); + } +} + +#[test] +fn translation_preserves_colour_range_at_every_depth() { + use photocraft_color::{ColorMode, SampleType}; + + let h = Homography([1.0, 0.0, 7.0, 0.0, 1.0, -3.0, 0.0, 0.0, 1.0]); + for sample in SampleType::ALL { + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for alpha in [false, true] { + let mut s = Surface::new(PixelFormat::new(mode, sample, alpha)); + let mut pixel = if mode == ColorMode::Rgb { vec![2.0, -0.25, 0.5] } else { vec![2.0] }; + if alpha { + pixel.push(0.5); + } + s.fill_rect(Rect::new(4, 4, 8, 8), &pixel); + let mut expected = s.pixel(5, 5); + if !alpha { + expected.push(1.0); + } + for interp in [Interp::Nearest, Interp::Bilinear, Interp::Bicubic] { + let out = warp_surface(&s, s.content_bounds(), &h, interp).unwrap(); + assert_eq!(out.pixel(12, 2), expected, "{sample:?}/{mode:?}/alpha={alpha}/{interp:?}"); + assert_eq!(out.pixel(0, 0).last(), Some(&0.0)); + } + } + } + } +} + +#[test] +fn bilinear_translation_interpolates_signed_hdr_colours() { + let mut s = Surface::new(PixelFormat::RGBA32F); + s.write_pixel(0, 0, &[2.0, -0.5, 0.25, 0.5]); + s.write_pixel(1, 0, &[4.0, -0.25, 0.75, 0.5]); + let h = Homography([1.0, 0.0, 0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + let out = warp_surface(&s, s.content_bounds(), &h, Interp::Bilinear).unwrap(); + assert_eq!(out.pixel(1, 0), vec![3.0, -0.375, 0.5, 0.5]); +} + +#[test] +fn scale_doubles_size_and_rotation_keeps_area() { + let mut s = rgba(); + s.fill_rect(Rect::new(0, 0, 40, 20), &[0.2, 0.4, 0.6, 1.0]); + let r = s.content_bounds(); + let up = + warp_surface(&s, r, &Homography::rect_to_quad([0.0, 0.0, 40.0, 20.0], [[0.0, 0.0], [80.0, 0.0], [80.0, 40.0], [0.0, 40.0]]).unwrap(), Interp::Bicubic) + .unwrap(); + let b = up.content_bounds(); + assert!(b.width() >= 80 && b.width() <= 82 && b.height() >= 40 && b.height() <= 42, "{b:?}"); + // 90° rotation about (20, 10): a 40×20 box becomes 20×40. + let q = [[30.0, -10.0], [30.0, 30.0], [10.0, 30.0], [10.0, -10.0]]; + let rot = warp_surface(&s, r, &Homography::rect_to_quad([0.0, 0.0, 40.0, 20.0], q).unwrap(), Interp::Bilinear).unwrap(); + let b = rot.content_bounds(); + assert!(b.width().abs_diff(20) <= 2 && b.height().abs_diff(40) <= 2, "{b:?}"); + let p = rot.pixel(20, 10); + assert!((p[0] - 0.2).abs() < 0.01 && p[3] > 0.99, "{p:?}"); +} + +/// #6203: a bicubic rotation of a sprite whose edge pixels are partly transparent overshoots +/// its alpha past 1. The colour must stay the sprite's, not grow with the overshoot into a +/// light border. +#[test] +fn rotation_keeps_the_colour_of_a_soft_edged_sprite() { + let mut s = rgba(); + s.fill_rect(Rect::new(10, 10, 30, 30), &[0.8, 0.6, 0.4, 0.5]); + s.fill_rect(Rect::new(11, 11, 29, 29), &[0.8, 0.6, 0.4, 1.0]); + let (c, k) = (20.0, std::f64::consts::FRAC_1_SQRT_2 * 10.0); + let q = [[c, c - 2.0 * k], [c + 2.0 * k, c], [c, c + 2.0 * k], [c - 2.0 * k, c]]; + let h = Homography::rect_to_quad([10.0, 10.0, 30.0, 30.0], q).unwrap(); + for interp in [Interp::Bicubic, Interp::Bilinear] { + let out = warp_surface(&s, s.content_bounds(), &h, interp).unwrap(); + let b = out.content_bounds(); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let p = out.pixel(x, y); + if p[3] > 0.0 { + let off = (0..3).map(|i| (p[i] - [0.8, 0.6, 0.4][i]).abs()).fold(0.0f32, f32::max); + assert!(off <= 1.0 / 255.0, "{interp:?} ({x}, {y}): {p:?}"); + } + } + } + } +} + +#[test] +fn large_downscale_is_prefiltered() { + // A 1px checkerboard shrunk 8× must come out ~50% grey, not aliased black/white. + let mut s = rgba(); + for y in 0..64 { + for x in 0..64 { + let v = ((x + y) % 2) as f32; + s.fill_rect(Rect::new(x, y, x + 1, y + 1), &[v, v, v, 1.0]); + } + } + let h = Homography::rect_to_quad([0.0, 0.0, 64.0, 64.0], [[0.0, 0.0], [8.0, 0.0], [8.0, 8.0], [0.0, 8.0]]).unwrap(); + let o = warp_surface(&s, s.content_bounds(), &h, Interp::Bicubic).unwrap(); + let p = o.pixel(4, 4); + assert!((p[0] - 0.5).abs() < 0.1, "{p:?}"); +} + +#[test] +fn surfaces_without_alpha_gain_it() { + let mut s = Surface::new(PixelFormat::new(photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8, false)); + s.fill_rect(Rect::new(0, 0, 10, 10), &[1.0, 0.0, 0.0]); + let h = Homography([1.0, 0.0, 5.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); + let o = warp_surface(&s, Rect::new(0, 0, 10, 10), &h, Interp::Nearest).unwrap(); + assert!(o.format().alpha); + assert_eq!(o.pixel(2, 2)[3], 0.0); + assert_eq!(o.pixel(7, 2), vec![1.0, 0.0, 0.0, 1.0]); +} + +#[test] +fn homography_is_identity() { + assert!(Homography::IDENTITY.is_identity()); + assert!(Homography([2.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 2.0]).is_identity()); + assert!(!Homography([1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]).is_identity()); + assert!(!Homography([1.0, 0.1, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]).is_identity()); +} diff --git a/crates/algo/src/vanishing.rs b/crates/algo/src/vanishing.rs index 77b7bb107c..25ae5609ef 100644 --- a/crates/algo/src/vanishing.rs +++ b/crates/algo/src/vanishing.rs @@ -324,50 +324,158 @@ pub fn paste(scene: &Scene, i: usize, src: &Surface, src_rect: Rect, at: [f64; 2 Ok(Some(out)) } +/// The fixed frame of one clone stroke: the source and destination planes, the source offset in +/// plane-metric units and the brush radius, all set at the stroke's anchor (its first dab). +struct CloneFrame { + hs: Homography, + ls: (f64, f64), + hd: Homography, + hd_inv: Homography, + ld: (f64, f64), + offset: [f64; 2], + r_m: f64, +} + +impl CloneFrame { + fn new(scene: &Scene, source: [f64; 2], anchor: [f64; 2], size: f64) -> Option { + let (ps, pd) = (scene.plane_at(source)?, scene.plane_at(anchor)?); + let ((hs, lus, lvs), (hd, lud, lvd)) = (scene.frame(ps)?, scene.frame(pd)?); + let (hs_inv, hd_inv) = (hs.inverse()?, hd.inverse()?); + let ms = metric(&hs_inv, (lus, lvs), source); + let md0 = metric(&hd_inv, (lud, lvd), anchor); + // Brush radius in metric units (from the image size at the anchor). + let probe = metric(&hd_inv, (lud, lvd), [anchor[0] + size / 2.0, anchor[1]]); + let r_m = ((probe[0] - md0[0]).hypot(probe[1] - md0[1])).max(1e-9); + let offset = [ms[0] - md0[0], ms[1] - md0[1]]; + (offset.iter().all(|v| v.is_finite()) && r_m.is_finite()).then_some(CloneFrame { hs, ls: (lus, lvs), hd, hd_inv, ld: (lud, lvd), offset, r_m }) + } + + /// The source point (image px) that destination `p` copies from. + fn source_of(&self, p: [f64; 2]) -> [f64; 2] { + let m = metric(&self.hd_inv, self.ld, p); + let (x, y) = self.hs.apply((m[0] + self.offset[0]) / self.ls.0, (m[1] + self.offset[1]) / self.ls.1); + [x, y] + } +} + +/// Image point → plane-metric coordinates through the plane's inverse homography. +fn metric(inv: &Homography, l: (f64, f64), p: [f64; 2]) -> [f64; 2] { + let (u, v) = inv.apply(p[0], p[1]); + [u * l.0, v * l.1] +} + /// Perspective clone stamp: one stroke of dabs at image points `points` on the destination /// plane, copying from `source` (image px) with the offset fixed in plane-metric units at the /// first dab. `size` is the brush diameter in px at the first dab; `hardness` 0..=1. pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points: &[[f64; 2]], size: f64, hardness: f64, opacity: f32) -> usize { let Some(first) = points.first() else { return 0 }; - let (Some(ps), Some(pd)) = (scene.plane_at(source), scene.plane_at(*first)) else { return 0 }; - let (Some((hs, lus, lvs)), Some((hd, lud, lvd))) = (scene.frame(ps), scene.frame(pd)) else { return 0 }; - let (Some(hs_inv), Some(hd_inv)) = (hs.inverse(), hd.inverse()) else { return 0 }; - let metric = |inv: &Homography, lu: f64, lv: f64, p: [f64; 2]| { - let (u, v) = inv.apply(p[0], p[1]); - [u * lu, v * lv] - }; - let ms = metric(&hs_inv, lus, lvs, source); - let md0 = metric(&hd_inv, lud, lvd, *first); - let offset = [ms[0] - md0[0], ms[1] - md0[1]]; - // Brush radius in metric units (from the image size at the first dab). - let probe = metric(&hd_inv, lud, lvd, [first[0] + size / 2.0, first[1]]); - let r_m = ((probe[0] - md0[0]).hypot(probe[1] - md0[1])).max(1e-9); + clone_dabs(scene, surf, source, *first, points, size, hardness, opacity, None).0 +} + +/// Where a clone stroke that starts at `anchor` copying from `source` samples for destination +/// `p`: the same plane-metric offset. A live preview paints a stroke in pieces with it, and +/// Aligned strokes keep the first stroke's offset. `None` when `source` or `anchor` is off the +/// planes. +pub fn clone_source(scene: &Scene, source: [f64; 2], anchor: [f64; 2], p: [f64; 2]) -> Option<[f64; 2]> { + let s = CloneFrame::new(scene, source, anchor, 1.0)?.source_of(p); + s.iter().all(|v| v.is_finite()).then_some(s) +} + +/// [`clone_stroke`]'s dabs at `points` for a stroke anchored at `anchor` (its first dab, which +/// fixes the offset and the brush's metric size). Samples a snapshot taken at this call. +/// For incremental painting use [`clone_dabs_from`] with one snapshot per stroke. +/// With `clip`, every dab is cut to it before any read or write (a document passes its canvas, +/// so far off-canvas points allocate no tiles). Returns the dabs painted and the area they changed. +#[allow(clippy::too_many_arguments)] +pub fn clone_dabs( + scene: &Scene, + surf: &mut Surface, + source: [f64; 2], + anchor: [f64; 2], + points: &[[f64; 2]], + size: f64, + hardness: f64, + opacity: f32, + clip: Option, +) -> (usize, Rect) { let src_copy = surf.clone(); + clone_dabs_from(scene, surf, &src_copy, source, anchor, points, size, hardness, opacity, clip) +} + +/// Paint a batch of one stroke using the same immutable source snapshot for every batch. +/// Source and destination must have the same pixel format; otherwise no dabs are painted. +/// `clip` as in [`clone_dabs`]. +#[allow(clippy::too_many_arguments)] +pub fn clone_dabs_from( + scene: &Scene, + surf: &mut Surface, + src_copy: &Surface, + source: [f64; 2], + anchor: [f64; 2], + points: &[[f64; 2]], + size: f64, + hardness: f64, + opacity: f32, + clip: Option, +) -> (usize, Rect) { + if surf.format() != src_copy.format() { + return (0, Rect::EMPTY); + } + let Some(f) = CloneFrame::new(scene, source, anchor, size) else { return (0, Rect::EMPTY) }; + let CloneFrame { hs, ls: (lus, lvs), hd, hd_inv, ld: (lud, lvd), offset, r_m } = f; let fmt = surf.format(); let n = fmt.channels(); + // Scratch for `sample`: the dab's source pixels, the four bilinear corners, the result. + let mut win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() }; + let (mut corners, mut s) = (vec![0.0f32; 4 * n], vec![0.0f32; n]); let mut dabs = 0; + let mut changed_area = Rect::EMPTY; for p in points { - let mc = metric(&hd_inv, lud, lvd, *p); + let mc = metric(&hd_inv, (lud, lvd), *p); // Image-space footprint: map a metric circle's bounding points. - let mut bb = [f64::MAX, f64::MAX, f64::MIN, f64::MIN]; - for k in 0..16 { - let a = k as f64 / 16.0 * std::f64::consts::TAU; - let (u, v) = ((mc[0] + r_m * a.cos()) / lud, (mc[1] + r_m * a.sin()) / lvd); - let (x, y) = hd.apply(u, v); - bb = [bb[0].min(x), bb[1].min(y), bb[2].max(x), bb[3].max(y)]; - } - let area = Rect::new(bb[0].floor() as i32, bb[1].floor() as i32, (bb[2].ceil() as i32).saturating_add(1), (bb[3].ceil() as i32).saturating_add(1)); + let footprint = |h: &Homography, lu: f64, lv: f64, c: [f64; 2]| { + let mut bb = [f64::MAX, f64::MAX, f64::MIN, f64::MIN]; + for k in 0..16 { + let a = k as f64 / 16.0 * std::f64::consts::TAU; + let (x, y) = h.apply((c[0] + r_m * a.cos()) / lu, (c[1] + r_m * a.sin()) / lv); + bb = [bb[0].min(x), bb[1].min(y), bb[2].max(x), bb[3].max(y)]; + } + if bb.iter().any(|v| !v.is_finite()) { + return Rect::EMPTY; + } + Rect::new(bb[0].floor() as i32, bb[1].floor() as i32, (bb[2].ceil() as i32).saturating_add(1), (bb[3].ceil() as i32).saturating_add(1)) + }; + let area = footprint(&hd, lud, lvd, mc); if area.is_empty() || area.width() > 4096 || area.height() > 4096 { continue; } + let area = clip.map_or(area, |c| area.intersect(&c)); + if area.is_empty() { + continue; + } + // The source footprint, read once; `sample` reads any pixel outside it from the surface. + // Skipped when it dwarfs the destination (cloning from near to far in strong perspective): + // decoding it would cost more than the few per-pixel reads it saves. + win.area = footprint(&hs, lus, lvs, [mc[0] + offset[0], mc[1] + offset[1]]).inflate(2); + let dest_px = u64::from(area.width()) * u64::from(area.height()); + if win.area.is_empty() + || win.area.width() > 4096 + || win.area.height() > 4096 + || u64::from(win.area.width()) * u64::from(win.area.height()) > 4 * dest_px + { + win.area = Rect::new(0, 0, 0, 0); + win.data.clear(); + } else { + src_copy.read_region_into(win.area, &mut win.data); + } let mut data = surf.read_region(area); let w = area.width() as usize; let mut changed = false; for (i, px) in data.chunks_exact_mut(n).enumerate() { let (x, y) = (area.x0 as f64 + (i % w) as f64 + 0.5, area.y0 as f64 + (i / w) as f64 + 0.5); - let m = metric(&hd_inv, lud, lvd, [x, y]); + let m = metric(&hd_inv, (lud, lvd), [x, y]); let d = (m[0] - mc[0]).hypot(m[1] - mc[1]) / r_m; - if d >= 1.0 { + if d >= 1.0 || d.is_nan() { continue; } let hard = hardness.clamp(0.0, 0.99); @@ -376,7 +484,7 @@ pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points: // Source pixel at the same metric offset. let sm = [m[0] + offset[0], m[1] + offset[1]]; let (sx, sy) = hs.apply(sm[0] / lus, sm[1] / lvs); - let s = sample(&src_copy, sx - 0.5, sy - 0.5, n); + sample(src_copy, &win, sx - 0.5, sy - 0.5, &mut corners, &mut s); for c in 0..n { px[c] += (s[c] - px[c]) * k; } @@ -385,16 +493,52 @@ pub fn clone_stroke(scene: &Scene, surf: &mut Surface, source: [f64; 2], points: if changed { surf.write_region(area, &data); dabs += 1; + changed_area = if changed_area.is_empty() { area } else { changed_area.union(&area) }; } } - dabs + (dabs, changed_area) } -fn sample(s: &Surface, x: f64, y: f64, n: usize) -> Vec { +/// Pixels of `area` read from the source surface (`read_region` layout). +struct Window { + area: Rect, + data: Vec, +} + +/// Bilinear sample of `s` at (`x`, `y`) into `out` (one pixel), with `corners` (resized to four +/// pixels) as reused scratch. A footprint inside `win` is read from it, any other with one region +/// read from `s`. +fn sample(s: &Surface, win: &Window, x: f64, y: f64, corners: &mut Vec, out: &mut [f32]) { + let n = out.len(); + if !x.is_finite() || !y.is_finite() { + out.fill(0.0); + return; + } let (x0, y0) = (x.floor() as i32, y.floor() as i32); let (tx, ty) = ((x - x0 as f64) as f32, (y - y0 as f64) as f32); - let r = s.read_region(Rect::new(x0, y0, x0 + 2, y0 + 2)); - (0..n).map(|c| (r[c] * (1.0 - tx) + r[n + c] * tx) * (1.0 - ty) + (r[2 * n + c] * (1.0 - tx) + r[3 * n + c] * tx) * ty).collect() + let (x1, y1) = (x0.saturating_add(1), y0.saturating_add(1)); + let foot = Rect::new(x0, y0, x1.saturating_add(1), y1.saturating_add(1)); + if !win.area.contains_rect(&foot) && foot.width() == 2 && foot.height() == 2 { + // Outside the window (or none): one region read looks the tile up once, where four + // `read_pixel` calls would look it up four times. Same corner order as below. + s.read_region_into(foot, corners); + } else { + corners.resize(4 * n, 0.0); + let w = win.area.width() as usize; + for (k, (px, py)) in [(x0, y0), (x1, y0), (x0, y1), (x1, y1)].into_iter().enumerate() { + let dst = &mut corners[k * n..(k + 1) * n]; + if win.area.contains(px, py) { + let o = ((py - win.area.y0) as usize * w + (px - win.area.x0) as usize) * n; + dst.copy_from_slice(&win.data[o..o + n]); + } else { + s.read_pixel(px, py, dst); + } + } + } + let r = corners; + for (c, o) in out.iter_mut().enumerate() { + *o = (r[c] * (1.0 - tx) + r[n + c] * tx) * (1.0 - ty) + (r[2 * n + c] * (1.0 - tx) + r[3 * n + c] * tx) * ty; + } } #[cfg(test)] @@ -486,5 +630,84 @@ mod tests { let n = clone_stroke(&sc, &mut s, [sx, sy], &[[dx, dy]], 10.0, 0.8, 1.0); assert_eq!(n, 1); assert!(s.rgba(dx as i32, dy as i32)[0] > 0.8, "{:?}", s.rgba(dx as i32, dy as i32)); + // The stroke's anchor copies from the source itself. + let back = clone_source(&sc, [sx, sy], [dx, dy], [dx, dy]).unwrap(); + assert!((back[0] - sx).abs() < 1e-6 && (back[1] - sy).abs() < 1e-6, "{back:?}"); + } + #[test] + fn incremental_clone_uses_one_source_snapshot_per_stroke() { + let sc = Scene::new(VpPlane { corners: [[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0]] }, [50.0, 50.0], None, 800.0); + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let mut original = Surface::new(fmt); + for x in 0..100 { + original.fill_rect(Rect::new(x, 0, x + 1, 100), &[x as f32 / 100.0, (x % 7) as f32 / 6.0, 0.3, 1.0]); + } + let points = [[40.0, 50.0], [44.0, 50.0], [48.0, 50.0], [52.0, 50.0]]; + let mut whole = original.clone(); + assert_eq!(clone_stroke(&sc, &mut whole, [36.0, 50.0], &points, 12.0, 0.8, 1.0), 4); + let mut pieces = original.clone(); + for part in points.chunks(2) { + clone_dabs_from(&sc, &mut pieces, &original, [36.0, 50.0], points[0], part, 12.0, 0.8, 1.0, None); + } + assert_eq!(whole.read_region(Rect::new(0, 0, 100, 100)), pieces.read_region(Rect::new(0, 0, 100, 100))); + } + } + + /// A clip cuts every dab before it reads or writes: dabs far outside it allocate no tiles. + #[test] + fn clipped_clone_allocates_no_tiles_outside_the_clip() { + let sc = Scene::new(VpPlane { corners: [[0.0, 0.0], [100.0, 0.0], [100.0, 100.0], [0.0, 100.0]] }, [50.0, 50.0], None, 800.0); + let clip = Rect::new(0, 0, 100, 100); + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(clip, &[0.5, 0.5, 0.5, 1.0]); + let tiles = s.tiles().count(); + let points = [[50.0, 50.0], [-40.0, 50.0], [5000.0, 5000.0], [-90_000.0, 70_000.0]]; + let (dabs, area) = clone_dabs(&sc, &mut s, [40.0, 50.0], points[0], &points, 20.0, 0.8, 1.0, Some(clip)); + assert_eq!(dabs, 1); + assert!(clip.contains_rect(&area), "{area:?}"); + assert_eq!(s.tiles().count(), tiles); + } + + #[test] + fn sample_at_the_edge_of_i32_does_not_overflow() { + let s = Surface::new(PixelFormat::RGBA8); + let win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() }; + let (mut corners, mut out) = (Vec::new(), [1.0; 4]); + sample(&s, &win, 3e9, 3e9, &mut corners, &mut out); + assert_eq!(out, [0.0; 4]); + } + + #[test] + fn sample_interpolates_across_tile_edges() { + let mut s = Surface::new(PixelFormat::RGBA8); + let (a, b) = ([1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]); + // Left of x = 256 is red, right is blue; the 2×2 footprint straddles the tile edge. + s.fill_rect(Rect::new(0, 0, 256, 512), &a); + s.fill_rect(Rect::new(256, 0, 512, 512), &b); + let (mut corners, mut out) = (Vec::new(), [0.0; 4]); + let want = [0.75, 0.0, 0.25, 1.0]; + // From the surface, from a window holding the footprint, and from one holding half of it. + for area in [Rect::new(0, 0, 0, 0), Rect::new(250, 250, 260, 260), Rect::new(250, 250, 256, 260)] { + let mut win = Window { area, data: Vec::new() }; + s.read_region_into(area, &mut win.data); + sample(&s, &win, 255.25, 255.5, &mut corners, &mut out); + assert!(out.iter().zip(want).all(|(o, w)| (o - w).abs() < 1e-6), "{area:?}: {out:?}"); + } + // Four different corners, one per tile, so both axes of the corner order count. + let mut q = Surface::new(PixelFormat::RGBA8); + for (x, y, px) in [(255, 255, [1.0, 0.0, 0.0, 1.0]), (256, 255, [0.0, 1.0, 0.0, 1.0]), (255, 256, [0.0, 0.0, 1.0, 1.0]), (256, 256, [1.0; 4])] { + q.fill_rect(Rect::new(x, y, x + 1, y + 1), &px); + } + let want = [0.375, 0.25, 0.75, 1.0]; + for area in [Rect::new(0, 0, 0, 0), Rect::new(250, 250, 260, 260)] { + let mut win = Window { area, data: Vec::new() }; + q.read_region_into(area, &mut win.data); + sample(&q, &win, 255.25, 255.75, &mut corners, &mut out); + assert!(out.iter().zip(want).all(|(o, w)| (o - w).abs() < 1e-6), "{area:?}: {out:?}"); + } + // Outside every tile, the default (transparent) pixel. + let win = Window { area: Rect::new(0, 0, 0, 0), data: Vec::new() }; + sample(&s, &win, -10.5, -10.5, &mut corners, &mut out); + assert_eq!(out, [0.0; 4]); } } diff --git a/crates/algo/src/video.rs b/crates/algo/src/video.rs index bbd093b0a8..512768fb7a 100644 --- a/crates/algo/src/video.rs +++ b/crates/algo/src/video.rs @@ -88,3 +88,162 @@ pub(crate) fn ntsc(src: &Image, out: Rect, ctx: &Ctx) -> Vec { } res } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{FilterParams, apply}; + use photocraft_color::{PixelFormat, SampleType}; + use photocraft_raster::Surface; + + const DEPTHS: [SampleType; 3] = [SampleType::U8, SampleType::U16, SampleType::F32]; + + fn tolerance(depth: SampleType) -> f32 { + match depth { + SampleType::U8 => 1.0 / 255.0, + SampleType::U16 => 1.0 / 65535.0, + SampleType::F32 => 1e-6, + } + } + + fn rows(depth: SampleType, bounds: Rect, pixels: &[[f32; 4]]) -> Surface { + let mut src = Surface::new(PixelFormat::new(ColorMode::Rgb, depth, true)); + for (row, px) in pixels.iter().enumerate() { + let y = bounds.y0 + row as i32; + src.fill_rect(Rect::new(bounds.x0, y, bounds.x1, y + 1), px); + } + src + } + + #[test] + fn deinterlace_all_field_and_interpolation_choices_use_bounds_relative_rows() { + // Odd document origin: parity is relative to the bounds, not absolute y. + let b = Rect::new(-2, 11, 0, 16); + let pixels = [0.0, 0.2, 0.6, 0.8, 1.0].map(|v| [v, 1.0 - v, v / 2.0, 1.0]); + for depth in DEPTHS { + let src = rows(depth, b, &pixels); + let before = src.read_region(b); + for (eliminate_even, interpolate, neighbours) in [ + (false, false, [(1, 1), (1, 1), (1, 1), (3, 3), (3, 3)]), + (false, true, [(1, 1), (1, 1), (1, 3), (3, 3), (3, 3)]), + (true, false, [(0, 0), (0, 0), (2, 2), (2, 2), (4, 4)]), + (true, true, [(0, 0), (0, 2), (2, 2), (2, 4), (4, 4)]), + ] { + let out = apply(&src, &FilterParams::DeInterlace { eliminate_even, interpolate }, b, b, None); + for (row, (a, c)) in neighbours.into_iter().enumerate() { + for x in b.x0..b.x1 { + let (pa, pc) = (src.pixel(x, b.y0 + a), src.pixel(x, b.y0 + c)); + let actual = out.pixel(x, b.y0 + row as i32); + for channel in 0..4 { + let expected = (pa[channel] + pc[channel]) / 2.0; + assert!( + (actual[channel] - expected).abs() <= tolerance(depth), + "{depth:?}, even={eliminate_even}, interp={interpolate}, row={row}: {actual:?}" + ); + } + } + } + } + assert_eq!(src.read_region(b), before); + } + } + + #[test] + fn deinterlace_interpolation_weights_colour_by_alpha() { + let b = Rect::new(0, 0, 1, 3); + for depth in DEPTHS { + for [a, c] in [[0.25, 0.75], [1.0, 0.0], [0.0, 0.0]] { + let src = rows(depth, b, &[[1.0, 0.0, 0.0, a], [0.0, 1.0, 0.0, 1.0], [0.0, 0.0, 1.0, c]]); + let out = apply(&src, &FilterParams::DeInterlace { eliminate_even: true, interpolate: true }, b, b, None); + let (a, c) = (src.pixel(0, 0)[3], src.pixel(0, 2)[3]); + let total = a + c; + let expected = if total > 0.0 { [a / total, 0.0, c / total, total / 2.0] } else { [0.0; 4] }; + let actual = out.pixel(0, 1); + for (v, want) in actual.iter().zip(expected) { + assert!((v - want).abs() <= tolerance(depth), "{depth:?}: {actual:?} != {expected:?}"); + } + assert_eq!(out.pixel(0, 0), src.pixel(0, 0)); + assert_eq!(out.pixel(0, 2), src.pixel(0, 2)); + } + } + } + + #[test] + fn deinterlace_one_and_two_row_images_without_alpha_handle_missing_neighbours() { + for depth in DEPTHS { + for height in [1, 2] { + let b = Rect::new(0, -3, 1, -3 + height); + let mut src = Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false)); + src.fill_rect(b, &[0.2]); + if height == 2 { + src.fill_rect(Rect::new(0, -2, 1, -1), &[0.8]); + } + for eliminate_even in [false, true] { + for interpolate in [false, true] { + let out = apply(&src, &FilterParams::DeInterlace { eliminate_even, interpolate }, b, b, None); + let kept_y = if height == 2 && !eliminate_even { -2 } else { -3 }; + for y in b.y0..b.y1 { + assert_eq!(out.pixel(0, y), src.pixel(0, kept_y), "{depth:?}, height={height}, even={eliminate_even}, interp={interpolate}"); + } + } + } + } + } + } + + #[test] + fn deinterlace_leaves_pixels_outside_reference_bounds_unchanged() { + let area = Rect::new(0, 0, 1, 5); + let b = Rect::new(0, 1, 1, 4); + let src = rows(SampleType::F32, area, &[[0.1; 4], [0.2; 4], [0.4; 4], [0.8; 4], [1.0; 4]]); + for eliminate_even in [false, true] { + for interpolate in [false, true] { + let out = apply(&src, &FilterParams::DeInterlace { eliminate_even, interpolate }, area, b, None); + assert_eq!(out.pixel(0, 0), src.pixel(0, 0)); + assert_eq!(out.pixel(0, 4), src.pixel(0, 4)); + } + } + } + + #[test] + fn ntsc_safe_colours_and_alpha_are_bit_exact_at_all_depths() { + let b = Rect::new(0, 0, 1, 5); + for depth in DEPTHS { + let src = rows(depth, b, &[[0.0, 0.0, 0.0, 0.0], [0.5, 0.5, 0.5, 0.25], [1.0, 1.0, 1.0, 0.75], [0.2, 0.3, 0.4, 1.0], [1.0, 0.0, 1.0, 1.0]]); + let out = apply(&src, &FilterParams::NtscColors, b, b, None); + assert_eq!(out.read_region(b), src.read_region(b), "{depth:?}"); + } + } + + fn luma_chroma(p: &[f32]) -> (f32, f32) { + let y = 0.299 * p[0] + 0.587 * p[1] + 0.114 * p[2]; + let i = 0.595716 * p[0] - 0.274453 * p[1] - 0.321263 * p[2]; + let q = 0.211456 * p[0] - 0.522591 * p[1] + 0.311135 * p[2]; + (y, i.hypot(q)) + } + + #[test] + fn ntsc_saturated_colours_keep_luma_alpha_and_meet_composite_limits() { + // Independent signal checks: -20..120 IRE relative to the 7.5..100 IRE luma range. + let (lo, hi) = ((-20.0 - 7.5) / 92.5, (120.0 - 7.5) / 92.5); + let colours = [[1.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.25], [1.0, 1.0, 0.0, 0.5], [0.0, 1.0, 1.0, 0.75]]; + let b = Rect::new(0, 0, 1, colours.len() as i32); + for depth in DEPTHS { + let src = rows(depth, b, &colours); + let out = apply(&src, &FilterParams::NtscColors, b, b, None); + // The rounded YIQ inverse contributes < 0.0003; integer writes add one sample step. + let eps = 0.0003 + 2.0 * tolerance(depth); + for y in b.y0..b.y1 { + let (before, after) = (src.pixel(0, y), out.pixel(0, y)); + let (y0, c0) = luma_chroma(&before); + let (y1, c1) = luma_chroma(&after); + assert!(y0 - c0 < lo || y0 + c0 > hi, "fixture must require correction"); + assert!(c1 < c0, "chroma not reduced: {depth:?} {after:?}"); + assert!((y1 - y0).abs() <= eps, "luma changed: {depth:?} {before:?} -> {after:?}"); + assert!(y1 - c1 >= lo - eps && y1 + c1 <= hi + eps, "signal out of gamut: {depth:?} {after:?}"); + assert!(after[..3].iter().all(|v| v.is_finite() && *v >= 0.0 && *v <= 1.0)); + assert_eq!(after[3], before[3], "alpha changed: {depth:?}"); + } + } + } +} diff --git a/crates/algo/src/warp.rs b/crates/algo/src/warp.rs index 188de07d47..87c6bde2c3 100644 --- a/crates/algo/src/warp.rs +++ b/crates/algo/src/warp.rs @@ -14,8 +14,9 @@ //! later (lower-right) cell wins. Work is split per destination tile (parallel on native). use photocraft_color::PixelFormat; -use photocraft_geom::Rect; -use photocraft_raster::Surface; +use photocraft_geom::{Rect, TileCoord}; +use photocraft_raster::{Interrupt, Surface, Tile}; +use std::sync::Arc; use crate::transform::{Interp, bicubic}; @@ -74,6 +75,16 @@ pub fn place_source_projective( /// coordinates → destination document coordinates). Output has `src`'s format with alpha /// added; pixels outside the warped area are transparent. pub fn warp_mesh_surface(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp) -> Surface { + mesh(src, src_rect, f, interp, &Interrupt::NONE) +} + +/// [`warp_mesh_surface`] reporting progress per destination tile; `None` once `ctl` is cancelled. +pub fn warp_mesh_surface_with(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp, ctl: &Interrupt) -> Option { + let out = mesh(src, src_rect, f, interp, ctl); + (!ctl.cancelled()).then_some(out) +} + +fn mesh(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync), interp: Interp, ctl: &Interrupt) -> Surface { if src_rect.is_empty() { let fmt = src.format(); return Surface::new(PixelFormat::new(fmt.mode, fmt.sample, true)); @@ -103,7 +114,7 @@ pub fn warp_mesh_surface(src: &Surface, src_rect: Rect, f: &(dyn Fn(f64, f64) -> tris.push([a, a + w1 + 1, a + w1]); } } - warp_triangles(src, src_rect, &verts, &tris, interp) + triangles(src, src_rect, &verts, &tris, interp, ctl) } /// Probe lines per axis [`mapped_lengths`] follows through the map. @@ -148,6 +159,17 @@ fn mapped_lengths(src_rect: Rect, f: &(dyn Fn(f64, f64) -> (f64, f64) + Sync)) - /// interpolation of its source positions. Later triangles in `tris` draw over earlier ones (the /// order is the depth order). Output has `src`'s format with alpha added. pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris: &[[usize; 3]], interp: Interp) -> Surface { + triangles(src, src_rect, verts, tris, interp, &Interrupt::NONE) +} + +/// [`warp_triangles`], reporting progress per destination tile to `ctl`. A cancelled `ctl` skips +/// the remaining tiles; the caller discards the result. +/// Destination tiles one triangle render may keep bookkeeping for (a 1M-tile render is 69 Gpx). +const MAX_DST_TILES: u64 = 1 << 20; +/// Source pixels one destination tile may read densely. +const MAX_FOOTPRINT_PIXELS: u64 = 1 << 25; + +fn triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2])], tris: &[[usize; 3]], interp: Interp, ctl: &Interrupt) -> Surface { let mut fmt = src.format(); let converted; let src = if fmt.alpha { @@ -173,6 +195,12 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 y1 = y1.max(v.0[1]); } let dst = Rect::new(x0.max(-lim).floor() as i32 - 1, y0.max(-lim).floor() as i32 - 1, x1.min(lim).ceil() as i32 + 1, y1.min(lim).ceil() as i32 + 1); + // Bookkeeping is kept per destination tile, so a mesh flung across the whole coordinate range + // would ask for millions of entries before drawing anything: draw nothing instead. + let ts_px = photocraft_geom::TILE_SIZE as u64; + if u64::from(dst.width()).div_ceil(ts_px).saturating_add(1).saturating_mul(u64::from(dst.height()).div_ceil(ts_px).saturating_add(1)) > MAX_DST_TILES { + return out; + } // Triangles: (vertex indices), binned per destination tile. let tiles: Vec = dst.tiles().map(|tc| tc.rect().intersect(&dst)).filter(|r| !r.is_empty()).collect(); let mut bins: Vec> = vec![Vec::new(); tiles.len()]; @@ -207,7 +235,16 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 } let n = fmt.channels(); let a = n - 1; - let work = |(t, tris): (&Rect, &Vec<[usize; 3]>)| -> Option<(Rect, Vec)> { + // Tiles finished so far, for progress reported per tile (from any worker thread). + let finished = std::sync::atomic::AtomicUsize::new(0); + let total = tiles.len().max(1); + let work = |(t, tris): (&Rect, &Vec<[usize; 3]>)| -> Option<(TileCoord, Arc)> { + if ctl.cancelled() { + return None; + } + let k = finished.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + // The last few percent are putting the tiles in place. + ctl.progress(0.98 * k as f32 / total as f32); if tris.is_empty() { return None; } @@ -278,7 +315,11 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 return None; } let foot = Rect::new(fx0.floor() as i32 - 3, fy0.floor() as i32 - 3, fx1.ceil() as i32 + 3, fy1.ceil() as i32 + 3).intersect(&src_rect); - if foot.is_empty() || !src.has_tiles_in(foot) { + // Sparse components far apart in the source can land in one tile: the dense window + // between them is not read (the tile stays empty). The pixel-area cap is checked first: + // `has_tiles_in` walks the source tile grid, so an oversized footprint must return + // without paying for that scan. + if foot.is_empty() || foot.size().area() > MAX_FOOTPRINT_PIXELS || !src.has_tiles_in(foot) { return None; } let mut px = src.read_region(foot); @@ -296,6 +337,11 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 f64::from(px[((y - foot.y0) as usize * fw + (x - foot.x0) as usize) * n + c]) } }; + // Offset of pixel (x, y) when the `taps` x `taps` block from it lies inside `foot`, so + // the samplers can index `px` directly instead of bounds-checking every tap. + let base = |x: i32, y: i32, taps: i32| -> Option { + (x >= foot.x0 && y >= foot.y0 && x + taps <= foot.x1 && y + taps <= foot.y1).then(|| ((y - foot.y0) as usize * fw + (x - foot.x0) as usize) * n) + }; let mut outp = vec![0.0f32; w * h * n]; let mut any = false; let mut acc = [0.0f64; 8]; @@ -313,6 +359,17 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 Interp::Bilinear => { let (ix, iy) = (u.floor() as i32, v.floor() as i32); let (fx, fy) = (u - f64::from(ix), v - f64::from(iy)); + if let Some(o) = base(ix, iy, 2) { + for (dy, wy) in [(0, 1.0 - fy), (1, fy)] { + for (dx, wx) in [(0, 1.0 - fx), (1, fx)] { + let p = &px[o + (dy * fw + dx) * n..][..n]; + for (s, v) in acc.iter_mut().zip(p) { + *s += f64::from(*v) * wx * wy; + } + } + } + return; + } for (dy, wy) in [(0, 1.0 - fy), (1, fy)] { for (dx, wx) in [(0, 1.0 - fx), (1, fx)] { for (c, s) in acc.iter_mut().enumerate().take(n) { @@ -327,6 +384,21 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 let (u, v) = (snap(u), snap(v)); let (ix, iy) = (u.floor() as i32, v.floor() as i32); let (wx, wy) = (bicubic(u - f64::from(ix)), bicubic(v - f64::from(iy))); + if let Some(o) = base(ix - 1, iy - 1, 4) { + for (j, wyj) in wy.iter().enumerate() { + for (i, wxi) in wx.iter().enumerate() { + let k = wxi * wyj; + if k == 0.0 { + continue; + } + let p = &px[o + (j * fw + i) * n..][..n]; + for (s, v) in acc.iter_mut().zip(p) { + *s += f64::from(*v) * k; + } + } + } + return; + } for (j, wyj) in wy.iter().enumerate() { for (i, wxi) in wx.iter().enumerate() { let k = wxi * wyj; @@ -374,23 +446,35 @@ pub fn warp_triangles(src: &Surface, src_rect: Rect, verts: &[([f64; 2], [f64; 2 any = true; let b = o * n; for c in 0..a { - outp[b + c] = (acc[c] / al).clamp(0.0, 1.0) as f32; + // The unclamped alpha, so a bicubic overshoot past 1 doesn't lighten the colour (#6203). + outp[b + c] = (acc[c] / acc[a]).clamp(0.0, 1.0) as f32; } outp[b + a] = al as f32; } - any.then_some((*t, outp)) + if !any { + return None; + } + // Encoded (and dropped if fully transparent) here, in parallel, rather than one tile at + // a time once all are done. `t` lies in one tile. + let mut one = Surface::new(fmt); + one.write_region(*t, &outp); + one.prune(); + if one.tile_count() == 0 { + return None; + } + one.take_tiles(*t).pop() }; #[cfg(not(target_arch = "wasm32"))] - let done: Vec<(Rect, Vec)> = { + let done: Vec<(TileCoord, Arc)> = { use rayon::prelude::*; tiles.par_iter().zip(bins.par_iter()).filter_map(work).collect() }; #[cfg(target_arch = "wasm32")] - let done: Vec<(Rect, Vec)> = tiles.iter().zip(bins.iter()).filter_map(work).collect(); - for (r, v) in done { - out.write_region(r, &v); + let done: Vec<(TileCoord, Arc)> = tiles.iter().zip(bins.iter()).filter_map(work).collect(); + if ctl.cancelled() { + return out; } - out.prune(); + out.put_tiles(done); out } @@ -441,6 +525,18 @@ mod tests { surf } + #[test] + fn a_mesh_flung_across_the_coordinate_range_gets_no_per_tile_bookkeeping() { + // About 8000 × 8000 destination tiles from three vertices. + let src = sample(SampleType::U8); + let far = 1_000_000.0; + let verts = [([-far, -far], [0.0, 0.0]), ([far, -far], [40.0, 0.0]), ([0.0, far], [0.0, 24.0])]; + let t = std::time::Instant::now(); + let out = warp_triangles(&src, Rect::new(0, 0, 40, 24), &verts, &[[0, 1, 2]], Interp::Nearest); + assert!(out.content_bounds().is_empty()); + assert!(t.elapsed().as_secs() < 5); + } + #[test] fn a_thin_layer_stretched_into_a_curve_keeps_the_curve() { // #5793: a 3 px wide column stretched 100x and bent into an arch. The grid was sized in @@ -463,6 +559,28 @@ mod tests { assert!(alpha(100, 55) > 0.99 && alpha(100, 20) < 0.01, "third {} {}", alpha(100, 55), alpha(100, 20)); } + #[test] + fn the_interruptible_mesh_warp_reports_progress_and_stops_when_cancelled() { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 600, 400), &[0.2, 0.4, 0.6, 1.0]); + let r = s.content_bounds(); + let w = Warp::preset(WarpStyle::Arc, 30.0, [0.0, 0.0, 600.0, 400.0]); + let map = |x: f64, y: f64| w.map(x, y); + let reported = std::sync::Mutex::new(0.0f32); + let progress = |p: f32| { + let mut m = reported.lock().unwrap(); + *m = m.max(p); + }; + let never = || false; + let out = warp_mesh_surface_with(&s, r, &map, Interp::Bicubic, &Interrupt::new(&never, &progress)).expect("not cancelled"); + let want = warp_mesh_surface(&s, r, &map, Interp::Bicubic); + assert_eq!(out.content_bounds(), want.content_bounds()); + assert_eq!(out.read_region(want.content_bounds()), want.read_region(want.content_bounds())); + assert!(*reported.lock().unwrap() > 0.9); + let always = || true; + assert!(warp_mesh_surface_with(&s, r, &map, Interp::Bicubic, &Interrupt::cancel_only(&always)).is_none()); + } + #[test] fn a_mesh_warp_far_off_canvas_writes_only_the_two_regions() { // #995: the old and warped regions were cleared as one dense `src ∪ warped` buffer, about @@ -498,6 +616,28 @@ mod tests { } } + #[test] + fn a_rotated_soft_edged_sprite_keeps_its_colour() { + // #6203, as in Free Transform: the bicubic alpha overshoots past 1 next to the half + // transparent edge, and must not lighten the colour. + let mut src = Surface::new(PixelFormat::new(ColorMode::Rgb, SampleType::U8, true)); + src.fill_rect(Rect::new(10, 10, 30, 30), &[0.8, 0.6, 0.4, 0.5]); + src.fill_rect(Rect::new(11, 11, 29, 29), &[0.8, 0.6, 0.4, 1.0]); + let k = std::f64::consts::FRAC_1_SQRT_2; + let rotate = |x: f64, y: f64| (20.0 + (x - 20.0) * k - (y - 20.0) * k, 20.0 + (x - 20.0) * k + (y - 20.0) * k); + let out = warp_mesh_surface(&src, src.content_bounds(), &rotate, Interp::Bicubic); + let b = out.content_bounds(); + for y in b.y0..b.y1 { + for x in b.x0..b.x1 { + let p = out.pixel(x, y); + if p[3] > 0.0 { + let off = (0..3).map(|i| (p[i] - [0.8, 0.6, 0.4][i]).abs()).fold(0.0f32, f32::max); + assert!(off <= 1.0 / 255.0, "({x}, {y}): {p:?}"); + } + } + } + } + #[test] fn translation_and_known_arc_points() { let src = sample(SampleType::U8); diff --git a/crates/algo/src/wideangle.rs b/crates/algo/src/wideangle.rs index ba3fafcc71..19da93fc24 100644 --- a/crates/algo/src/wideangle.rs +++ b/crates/algo/src/wideangle.rs @@ -22,7 +22,7 @@ use photocraft_geom::Rect; use photocraft_raster::Surface; use serde::{Deserialize, Serialize}; -use crate::photo_util::SparseLs; +use crate::photo_util::{BandChol, Csr, par_rows, pcg, solve_dense}; use crate::transform::Interp; /// Lens projection of the source image. @@ -234,95 +234,293 @@ impl WideMesh { } } -/// Solves the content-preserving warp for `p` over `frame`. -pub fn solve(p: &WideAngle, frame: Rect) -> WideMesh { - let cam = Camera::new(p, frame); - let (fw, fh) = (frame.width().max(1) as f64, frame.height().max(1) as f64); - let cells = 48.0; - let cell = fw.max(fh) / cells; - let nx = ((fw / cell).round() as usize).clamp(2, 96); - let ny = ((fh / cell).round() as usize).clamp(2, 96); - let (cw, ch) = (fw / nx as f64, fh / ny as f64); - let w1 = nx + 1; - let nv = w1 * (ny + 1); - let src: Vec<[f64; 2]> = (0..nv).map(|k| [frame.x0 as f64 + (k % w1) as f64 * cw, frame.y0 as f64 + (k / w1) as f64 * ch]).collect(); - let curves: Vec> = p.constraints.iter().map(|c| cam.arc(c.a, c.b, 24)).collect(); - let mut x: Vec = src.iter().flat_map(|s| [s[0], s[1]]).collect(); - // Bilinear weights of a source point in the grid. - let weights = |q: [f64; 2]| -> [(usize, f64); 4] { - let gx = ((q[0] - frame.x0 as f64) / cw).clamp(0.0, nx as f64 - 1e-9); - let gy = ((q[1] - frame.y0 as f64) / ch).clamp(0.0, ny as f64 - 1e-9); +/// Weight of the line rows (applied squared in the normal equations). +const LINE_WEIGHT: f64 = 10.0; +/// Weight of the weak anchoring to the source (it fixes the global similarity). +const ANCHOR_WEIGHT: f64 = 0.02; +/// Grid cells along the longer side of the frame. +const GRID_CELLS: f64 = 48.0; + +/// A sparse row `Σ coef·x[idx]`. +type Row = Vec<(usize, f64)>; +/// A solver for `(K + LᵀL) x = rhs` given the grid, the line rows `L` and a warm start. +type LinSolve<'a> = dyn FnMut(&Grid, &[Row], &mut Vec) + 'a; + +/// The warp's grid over a frame; the unknowns are the output positions of its vertices, +/// interleaved `[x0, y0, x1, y1, …]`. +struct Grid { + frame: Rect, + nx: usize, + ny: usize, + cw: f64, + ch: f64, + w1: usize, + nv: usize, + /// Source position of each vertex. + src: Vec<[f64; 2]>, +} + +impl Grid { + fn new(frame: Rect, cells: f64) -> Grid { + let (fw, fh) = (frame.width().max(1) as f64, frame.height().max(1) as f64); + let cell = fw.max(fh) / cells; + let nx = ((fw / cell).round() as usize).clamp(2, 96); + let ny = ((fh / cell).round() as usize).clamp(2, 96); + let (cw, ch) = (fw / nx as f64, fh / ny as f64); + let w1 = nx + 1; + let nv = w1 * (ny + 1); + let src = (0..nv).map(|k| [frame.x0 as f64 + (k % w1) as f64 * cw, frame.y0 as f64 + (k / w1) as f64 * ch]).collect(); + Grid { frame, nx, ny, cw, ch, w1, nv, src } + } + + /// Bilinear weights of a source point in the grid. + fn weights(&self, q: [f64; 2]) -> [(usize, f64); 4] { + let gx = ((q[0] - self.frame.x0 as f64) / self.cw).clamp(0.0, self.nx as f64 - 1e-9); + let gy = ((q[1] - self.frame.y0 as f64) / self.ch).clamp(0.0, self.ny as f64 - 1e-9); let (i, j) = (gx.floor() as usize, gy.floor() as usize); let (tx, ty) = (gx - i as f64, gy - j as f64); - let a = j * w1 + i; - [(a, (1.0 - tx) * (1.0 - ty)), (a + 1, tx * (1.0 - ty)), (a + w1, (1.0 - tx) * ty), (a + w1 + 1, tx * ty)] - }; - let tris = { - let mut t = Vec::new(); - for j in 0..ny { - for i in 0..nx { - let a = j * w1 + i; - t.push([a, a + 1, a + w1 + 1]); - t.push([a, a + w1 + 1, a + w1]); - // Both diagonals so the shape term is symmetric. - t.push([a, a + 1, a + w1]); - t.push([a + 1, a + w1 + 1, a + w1]); + let a = j * self.w1 + i; + [(a, (1.0 - tx) * (1.0 - ty)), (a + 1, tx * (1.0 - ty)), (a + self.w1, (1.0 - tx) * ty), (a + self.w1 + 1, tx * ty)] + } + + /// The anchoring right-hand side `ANCHOR_WEIGHT² · src`, interleaved. + fn anchor_rhs(&self) -> Vec { + self.src.iter().flat_map(|s| [ANCHOR_WEIGHT * ANCHOR_WEIGHT * s[0], ANCHOR_WEIGHT * ANCHOR_WEIGHT * s[1]]).collect() + } +} + +/// The two shape rows (x then y) that pin vertex `i0` to `v1 + u (v2 − v1) + v R90 (v2 − v1)`, +/// with `(u, v)` taken from the source positions. Vertex ids are whatever the caller numbers its +/// unknowns by; the rows' right-hand sides are zero. +fn shape_rows(i: [usize; 3], p: [[f64; 2]; 3]) -> [[(usize, f64); 5]; 2] { + let (e, d) = ([p[2][0] - p[1][0], p[2][1] - p[1][1]], [p[0][0] - p[1][0], p[0][1] - p[1][1]]); + let l2 = e[0] * e[0] + e[1] * e[1]; + let u = (d[0] * e[0] + d[1] * e[1]) / l2; + let v = (d[0] * -e[1] + d[1] * e[0]) / l2; + let (i0, i1, i2) = (i[0], i[1], i[2]); + [ + // x: v0x − v1x − u (v2x − v1x) + v (v2y − v1y) = 0 + [(2 * i0, 1.0), (2 * i1, -1.0 + u), (2 * i2, -u), (2 * i2 + 1, v), (2 * i1 + 1, -v)], + // y: v0y − v1y − u (v2y − v1y) − v (v2x − v1x) = 0 + [(2 * i0 + 1, 1.0), (2 * i1 + 1, -1.0 + u), (2 * i2 + 1, -u), (2 * i2, -v), (2 * i1, v)], + ] +} + +/// The four triangles of a cell over its corners `[a, a + 1, a + w1, a + w1 + 1]` (as 0..4); +/// both diagonals are used so the shape term is symmetric. +const CELL_TRIS: [[usize; 3]; 4] = [[0, 1, 3], [0, 3, 2], [0, 1, 2], [1, 3, 2]]; + +/// Every shape row of the grid (with global vertex ids), for reference solves. +#[cfg(test)] +fn all_shape_rows(g: &Grid) -> Vec { + let mut out = Vec::new(); + for j in 0..g.ny { + for i in 0..g.nx { + let a = j * g.w1 + i; + let corner = [a, a + 1, a + g.w1, a + g.w1 + 1]; + for t in CELL_TRIS { + for rot in 0..3 { + let ids = [corner[t[rot]], corner[t[(rot + 1) % 3]], corner[t[(rot + 2) % 3]]]; + for row in shape_rows(ids, ids.map(|k| g.src[k])) { + out.push(row.to_vec()); + } + } } } - t - }; + } + out +} + +/// The line rows (already multiplied by `LINE_WEIGHT`): every sample of a constraint's curve is +/// tied to the curve's first point along the constraint's normal. +fn line_rows(g: &Grid, curves: &[Vec<[f64; 2]>], active: &[usize], normals: &[[f64; 2]], p: &WideAngle) -> Vec { + let mut rows = Vec::new(); + for (ci, &k) in active.iter().enumerate() { + let Some(c) = curves.get(k) else { continue }; + let n = match p.constraints.get(k).map(|c| c.orientation) { + Some(Orientation::Horizontal) => [0.0, 1.0], + Some(Orientation::Vertical) => [1.0, 0.0], + _ => normals.get(ci).copied().unwrap_or([0.0, 1.0]), + }; + let Some(first) = c.first() else { continue }; + let w0 = g.weights(*first); + for q in c.iter().skip(1) { + let mut row: Row = Vec::with_capacity(16); + for &(i, w) in &g.weights(*q) { + row.push((2 * i, LINE_WEIGHT * n[0] * w)); + row.push((2 * i + 1, LINE_WEIGHT * n[1] * w)); + } + for &(i, w) in &w0 { + row.push((2 * i, -LINE_WEIGHT * n[0] * w)); + row.push((2 * i + 1, -LINE_WEIGHT * n[1] * w)); + } + rows.push(row); + } + } + rows +} + +/// Above this many line rows the low-rank update's dense `rows × rows` system gets too big and +/// the solve falls back to conjugate gradients. +const MAX_UPDATE_ROWS: usize = 256; + +/// The part of the normal equations that doesn't depend on the constraints: the shape and +/// anchor terms `K = Σ rᵀr + ANCHOR_WEIGHT²·I`, assembled and factored once per solve. +struct StaticSystem { + k: Csr, + chol: Option, + rhs: Vec, + /// `K⁻¹ · rhs`, the solution without any line rows. + base: Vec, +} + +impl StaticSystem { + fn new(g: &Grid) -> StaticSystem { + let n = 2 * g.nv; + let mut trip: Vec<(usize, usize, f64)> = Vec::with_capacity(g.nx * g.ny * 64 + n); + for j in 0..g.ny { + for i in 0..g.nx { + let a = j * g.w1 + i; + let corner = [a, a + 1, a + g.w1, a + g.w1 + 1]; + // The cell's 24 shape rows only involve its own 8 unknowns: accumulate their + // 8 × 8 normal block (unknown `2·c + axis` is corner `c`'s coordinate). + let mut m = [[0.0f64; 8]; 8]; + for t in CELL_TRIS { + for rot in 0..3 { + let local = [t[rot], t[(rot + 1) % 3], t[(rot + 2) % 3]]; + for row in shape_rows(local, local.map(|c| g.src[corner[c]])) { + for &(si, ca) in &row { + for &(sj, cb) in &row { + m[si][sj] += ca * cb; + } + } + } + } + } + for (si, mi) in m.iter().enumerate() { + for (sj, &v) in mi.iter().enumerate() { + trip.push((2 * corner[si / 2] + si % 2, 2 * corner[sj / 2] + sj % 2, v)); + } + } + } + } + for d in 0..n { + trip.push((d, d, ANCHOR_WEIGHT * ANCHOR_WEIGHT)); + } + let k = Csr::from_triplets(n, trip); + let chol = BandChol::new(&k); + let rhs = g.anchor_rhs(); + let mut base = vec![0.0; n]; + if let Some(c) = &chol { + c.solve(&rhs, &mut base); + } + StaticSystem { k, chol, rhs, base } + } + + /// Solves `(K + LᵀL) x = rhs` for the line rows `L = lines`. With `K`'s factor this is the + /// exact low-rank update `x = K⁻¹b − K⁻¹Lᵀ (I + L K⁻¹ Lᵀ)⁻¹ L K⁻¹b` (one parallel solve per + /// line row). Without it, or with very many rows, conjugate gradients warm-started from `x`. + /// A solve that goes non-finite leaves `x` as it was. + fn solve(&self, lines: &[Row], x: &mut Vec) { + let n = self.rhs.len(); + let updated = match &self.chol { + Some(chol) if lines.len() <= MAX_UPDATE_ROWS => self.solve_update(chol, lines), + _ => None, + }; + let solved = match updated { + Some(v) => v, + None => { + let mut v = x.clone(); + self.solve_cg(lines, &mut v); + v + } + }; + if solved.len() == n && solved.iter().all(|v| v.is_finite()) { + *x = solved; + } + } + + fn solve_update(&self, chol: &BandChol, lines: &[Row]) -> Option> { + let (n, m) = (self.rhs.len(), lines.len()); + if m == 0 { + return Some(self.base.clone()); + } + // Column `j` of `K⁻¹Lᵀ` is `K⁻¹` applied to line row `j`. + let mut cols = vec![0.0; n * m]; + par_rows(&mut cols, n, 1, |j, col| { + let mut dense = vec![0.0; n]; + for &(i, c) in lines.get(j).into_iter().flatten() { + if let Some(d) = dense.get_mut(i) { + *d += c; + } + } + chol.solve(&dense, col); + }); + let dot_row = |row: &Row, v: &[f64]| -> f64 { row.iter().map(|&(i, c)| c * v.get(i).copied().unwrap_or(0.0)).sum() }; + // S = I + L K⁻¹ Lᵀ, and t = L K⁻¹ b. + let mut s = vec![0.0; m * m]; + for (a, row) in lines.iter().enumerate() { + for (b, col) in cols.chunks(n).enumerate() { + s[a * m + b] = dot_row(row, col) + if a == b { 1.0 } else { 0.0 }; + } + } + let t: Vec = lines.iter().map(|row| dot_row(row, &self.base)).collect(); + let z = solve_dense(m, s, t)?; + let mut x = self.base.clone(); + for (zj, col) in z.iter().zip(cols.chunks(n)) { + for (xi, ci) in x.iter_mut().zip(col) { + *xi -= zj * ci; + } + } + Some(x) + } + + fn solve_cg(&self, lines: &[Row], x: &mut [f64]) { + let op = |v: &[f64], out: &mut [f64]| { + self.k.mul(v, out); + for row in lines { + let t: f64 = row.iter().map(|&(i, c)| c * v.get(i).copied().unwrap_or(0.0)).sum(); + for &(i, c) in row { + if let Some(o) = out.get_mut(i) { + *o += c * t; + } + } + } + }; + match &self.chol { + Some(chol) => { + pcg(op, |r, z| chol.solve(r, z), &self.rhs, x, 1e-8, 500); + } + // The band was too wide to factor: Jacobi-preconditioned, and many more iterations. + None => { + let diag = &self.k.diag; + pcg(op, |r, z| z.iter_mut().zip(r.iter().zip(diag)).for_each(|(z, (r, d))| *z = r / d), &self.rhs, x, 1e-8, 4000); + } + } + } +} + +/// Solves the content-preserving warp for `p` over `frame`. +pub fn solve(p: &WideAngle, frame: Rect) -> WideMesh { + let mut system: Option = None; + solve_with(p, frame, GRID_CELLS, &mut |g, lines, x| system.get_or_insert_with(|| StaticSystem::new(g)).solve(lines, x)) +} + +/// [`solve`] with the grid resolution and the linear solver for `(K + LᵀL) x = rhs` (given the +/// current line rows and a warm start) chosen by the caller. +fn solve_with(p: &WideAngle, frame: Rect, cells: f64, linsolve: &mut LinSolve<'_>) -> WideMesh { + let cam = Camera::new(p, frame); + let g = Grid::new(frame, cells); + let (nx, ny, nv, src) = (g.nx, g.ny, g.nv, &g.src); + let curves: Vec> = p.constraints.iter().map(|c| cam.arc(c.a, c.b, 24)).collect(); + let mut x: Vec = src.iter().flat_map(|s| [s[0], s[1]]).collect(); let active: Vec = (0..curves.len()).filter(|&k| curves[k].len() >= 3).collect(); if !active.is_empty() { let mut normals: Vec<[f64; 2]> = active.iter().map(|_| [0.0, 1.0]).collect(); for round in 0..4 { - let mut ls = SparseLs::new(2 * nv); - // Shape: v0 = v1 + u (v2 − v1) + v R90 (v2 − v1), with (u, v) from the source. - for t in &tris { - for rot in 0..3 { - let (i0, i1, i2) = (t[rot], t[(rot + 1) % 3], t[(rot + 2) % 3]); - let (p0, p1, p2) = (src[i0], src[i1], src[i2]); - let e = [p2[0] - p1[0], p2[1] - p1[1]]; - let l2 = e[0] * e[0] + e[1] * e[1]; - let d = [p0[0] - p1[0], p0[1] - p1[1]]; - let u = (d[0] * e[0] + d[1] * e[1]) / l2; - let v = (d[0] * -e[1] + d[1] * e[0]) / l2; - // x: v0x − v1x − u (v2x − v1x) + v (v2y − v1y) = 0 - ls.add(vec![(2 * i0, 1.0), (2 * i1, -1.0 + u), (2 * i2, -u), (2 * i2 + 1, v), (2 * i1 + 1, -v)], 0.0, 1.0); - // y: v0y − v1y − u (v2y − v1y) − v (v2x − v1x) = 0 - ls.add(vec![(2 * i0 + 1, 1.0), (2 * i1 + 1, -1.0 + u), (2 * i2 + 1, -u), (2 * i2, -v), (2 * i1, v)], 0.0, 1.0); - } - } - // Weak anchoring to the source (fixes the global similarity). - for (k, s) in src.iter().enumerate() { - ls.add(vec![(2 * k, 1.0)], s[0], 0.02); - ls.add(vec![(2 * k + 1, 1.0)], s[1], 0.02); - } - // Lines. - for (ci, &k) in active.iter().enumerate() { - let c = &curves[k]; - let n = match p.constraints[k].orientation { - Orientation::Horizontal => [0.0, 1.0], - Orientation::Vertical => [1.0, 0.0], - Orientation::Free => normals[ci], - }; - let w0 = weights(c[0]); - for q in &c[1..] { - let wq = weights(*q); - let mut row: Vec<(usize, f64)> = Vec::with_capacity(16); - for &(i, w) in &wq { - row.push((2 * i, n[0] * w)); - row.push((2 * i + 1, n[1] * w)); - } - for &(i, w) in &w0 { - row.push((2 * i, -n[0] * w)); - row.push((2 * i + 1, -n[1] * w)); - } - ls.add(row, 0.0, 10.0); - } - } - x = ls.solve(&x, 400 + 200 * round); + linsolve(&g, &line_rows(&g, &curves, &active, &normals, p), &mut x); // Re-estimate free normals from the current end points. - let pos = |q: [f64; 2]| -> [f64; 2] { weights(q).iter().fold([0.0, 0.0], |acc, &(i, w)| [acc[0] + w * x[2 * i], acc[1] + w * x[2 * i + 1]]) }; + let pos = |q: [f64; 2]| -> [f64; 2] { g.weights(q).iter().fold([0.0, 0.0], |acc, &(i, w)| [acc[0] + w * x[2 * i], acc[1] + w * x[2 * i + 1]]) }; let mut changed = false; for (ci, &k) in active.iter().enumerate() { let c = &curves[k]; @@ -350,7 +548,7 @@ pub fn solve(p: &WideAngle, frame: Rect) -> WideMesh { let mut mesh = WideMesh { nx, ny, verts, curves, residual: 0.0 }; // Straightness residual: distance of curve samples from the chord of their mapped ends. let mut worst = 0.0f64; - for (k, cv) in mesh.curves.iter().enumerate() { + for cv in mesh.curves.iter() { if cv.len() < 3 { continue; } @@ -361,7 +559,6 @@ pub fn solve(p: &WideAngle, frame: Rect) -> WideMesh { for q in &pts { worst = worst.max(((q[0] - a[0]) * d[1] - (q[1] - a[1]) * d[0]).abs() / l); } - let _ = k; } mesh.residual = worst; mesh @@ -433,6 +630,108 @@ mod tests { assert!((c[0] - 300.0).abs() < 80.0 && (c[1] - 250.0).abs() < 40.0, "{c:?}"); } + /// The exact solution of the same normal equations (dense, small grids only). + fn dense_linsolve(g: &Grid, lines: &[Row], x: &mut Vec) { + use crate::photo_util::Normal; + let mut ne = Normal::new(2 * g.nv); + for row in all_shape_rows(g) { + ne.add(&row, 0.0, 1.0); + } + for k in 0..2 * g.nv { + ne.add(&[(k, 1.0)], g.src[k / 2][k % 2], ANCHOR_WEIGHT); + } + for row in lines { + ne.add(row, 0.0, 1.0); + } + *x = ne.solve().expect("the anchored system is positive definite"); + } + + #[test] + fn direct_solve_matches_the_dense_reference() { + let frame = Rect::new(0, 0, 240, 160); + for constraints in [ + vec![ + Constraint { a: [30.0, 40.0], b: [210.0, 40.0], orientation: Orientation::Horizontal }, + Constraint { a: [40.0, 30.0], b: [45.0, 130.0], orientation: Orientation::Free }, + ], + vec![ + Constraint { a: [20.0, 120.0], b: [220.0, 110.0], orientation: Orientation::Free }, + Constraint { a: [200.0, 20.0], b: [205.0, 140.0], orientation: Orientation::Vertical }, + ], + ] { + let p = WideAngle { model: WideModel::Fisheye, focal_length: 8.0, constraints, ..Default::default() }; + let mut system: Option = None; + let fast = solve_with(&p, frame, 12.0, &mut |g, lines, x| system.get_or_insert_with(|| StaticSystem::new(g)).solve(lines, x)); + let exact = solve_with(&p, frame, 12.0, &mut dense_linsolve); + assert_eq!(fast.verts.len(), exact.verts.len()); + let worst = fast.verts.iter().zip(&exact.verts).map(|(a, b)| (a.0[0] - b.0[0]).abs().max((a.0[1] - b.0[1]).abs())).fold(0.0, f64::max); + assert!(worst < 0.05, "worst vertex difference {worst} px"); + assert!(fast.residual <= exact.residual + 0.05, "residual {} vs exact {}", fast.residual, exact.residual); + } + } + + /// More line rows than the low-rank update takes falls back to conjugate gradients, which + /// still agrees with the exact solution. + #[test] + fn many_constraints_use_the_conjugate_gradient_fallback() { + let frame = Rect::new(0, 0, 240, 160); + let constraints: Vec = (0..12) + .map(|k| Constraint { + a: [20.0 + 4.0 * k as f64, 15.0 + 10.0 * k as f64], + b: [210.0 - 3.0 * k as f64, 18.0 + 10.0 * k as f64], + orientation: Orientation::Horizontal, + }) + .collect(); + let p = WideAngle { model: WideModel::Fisheye, focal_length: 8.0, constraints, ..Default::default() }; + let mut rows = 0; + let mut system: Option = None; + let fast = solve_with(&p, frame, 12.0, &mut |g, lines, x| { + rows = lines.len(); + system.get_or_insert_with(|| StaticSystem::new(g)).solve(lines, x) + }); + assert!(rows > MAX_UPDATE_ROWS, "{rows} line rows"); + let exact = solve_with(&p, frame, 12.0, &mut dense_linsolve); + let worst = fast.verts.iter().zip(&exact.verts).map(|(a, b)| (a.0[0] - b.0[0]).abs().max((a.0[1] - b.0[1]).abs())).fold(0.0, f64::max); + assert!(worst < 0.05, "worst vertex difference {worst} px"); + } + + /// Without a banded factor (band too wide) the Jacobi-preconditioned fallback still solves. + #[test] + fn unfactored_system_falls_back_to_jacobi_cg() { + let frame = Rect::new(0, 0, 240, 160); + let p = WideAngle { + model: WideModel::Fisheye, + focal_length: 8.0, + constraints: vec![Constraint { a: [30.0, 40.0], b: [210.0, 40.0], orientation: Orientation::Horizontal }], + ..Default::default() + }; + let mut system: Option = None; + let jacobi = solve_with(&p, frame, 12.0, &mut |g, lines, x| { + let sys = system.get_or_insert_with(|| { + let mut sys = StaticSystem::new(g); + sys.chol = None; + sys + }); + sys.solve(lines, x) + }); + let exact = solve_with(&p, frame, 12.0, &mut dense_linsolve); + let worst = jacobi.verts.iter().zip(&exact.verts).map(|(a, b)| (a.0[0] - b.0[0]).abs().max((a.0[1] - b.0[1]).abs())).fold(0.0, f64::max); + assert!(worst < 0.1, "worst vertex difference {worst} px"); + } + + #[test] + fn hostile_frames_and_constraints_solve_to_finite_meshes() { + let wild = Constraint { a: [-1e6, 5e5], b: [1e6, -5e5], orientation: Orientation::Free }; + let same = Constraint { a: [3.0, 3.0], b: [3.0, 3.0], orientation: Orientation::Free }; + let nan = Constraint { a: [f64::NAN, 1.0], b: [2.0, f64::INFINITY], orientation: Orientation::Horizontal }; + for frame in [Rect::new(0, 0, 1, 1), Rect::new(0, 0, 2, 200), Rect::new(0, 0, 400, 3), Rect::new(-50, -20, 90, 60)] { + let p = + WideAngle { model: WideModel::Fisheye, focal_length: 8.0, constraints: vec![wild.clone(), same.clone(), nan.clone()], ..Default::default() }; + let mesh = solve(&p, frame); + assert!(mesh.verts.iter().all(|(o, s)| o.iter().chain(s).all(|v| v.is_finite())), "{frame:?}"); + } + } + #[test] fn no_constraints_is_identity_and_renders() { use photocraft_color::PixelFormat; diff --git a/crates/algo/tests/it/liquify_freeze_formats.rs b/crates/algo/tests/it/liquify_freeze_formats.rs new file mode 100644 index 0000000000..e5def7106f --- /dev/null +++ b/crates/algo/tests/it/liquify_freeze_formats.rs @@ -0,0 +1,107 @@ +//! Final-render coverage for #7989, extending the boundary fix in #8012. +use photocraft_algo::liquify::{LiquifyField, LiquifyStroke, LiquifyTool, apply_liquify}; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_geom::Rect; +use photocraft_raster::Surface; + +fn bounds() -> Rect { + // Cross the tile boundary at x=256, with a nonzero origin on both axes. + Rect::new(192, -32, 320, 96) +} + +fn source(mode: ColorMode, sample: SampleType) -> Surface { + let format = PixelFormat::new(mode, sample, true); + let mut pixels = Vec::with_capacity(128 * 128 * format.channels()); + for y in 0..128 { + for x in 0..128 { + let alpha = if (48..52).contains(&x) { 0.0 } else { ((x + 3 * y) % 5 + 1) as f32 / 5.0 }; + for channel in 0..mode.color_channels() { + pixels.push(if alpha == 0.0 { 0.0 } else { ((x * 19 + y * 7 + channel * 43) % 251 + 2) as f32 / 255.0 }); + } + pixels.push(alpha); + } + } + let mut source = Surface::new(format); + source.write_region(bounds(), &pixels); + source +} + +fn warp(points: &[[f64; 2]], size: f64, pressure: f64) -> LiquifyStroke { + let mut stroke = LiquifyStroke::new(LiquifyTool::ForwardWarp, size); + stroke.pressure = pressure; + stroke.points = points.iter().map(|point| point.to_vec()).collect(); + stroke +} + +fn freeze_left() -> LiquifyStroke { + let mut stroke = LiquifyStroke::new(LiquifyTool::LassoMask, 1.0); + stroke.amount = Some(1.0); + stroke.points = vec![vec![192.0, -32.0], vec![256.0, -32.0], vec![256.0, 96.0], vec![192.0, 96.0]]; + stroke +} + +fn check_frozen_output(prior_warp: bool) { + for mode in [ColorMode::Grayscale, ColorMode::Rgb, ColorMode::Cmyk] { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let source = source(mode, sample); + for cell in [2.5, 4.0] { + let mut strokes = Vec::new(); + if prior_warp { + strokes.push(warp(&[[238.0, 20.0], [261.0, 28.0]], 64.0, 55.0)); + } + strokes.push(freeze_left()); + let mut field = LiquifyField::from_strokes(bounds(), cell, &strokes); + // #8012 establishes the bilinear mapping when the mask is applied. + // This test checks later edits, not exact pre-mask cubic preservation. + let before = apply_liquify(&source, &field); + assert_eq!(before.format(), source.format()); + assert_eq!(before == source, !prior_warp, "the fixture must retain an earlier warp and keep a fresh mask-only operation unchanged"); + strokes.push(warp(&[[268.0, 32.0], [252.0, 32.0]], 48.0, 80.0)); + field.apply_stroke(&strokes[strokes.len() - 1]); + let after = apply_liquify(&source, &field); + assert_eq!(after.format(), source.format()); + + let (mut protected, mut fractional_alpha, mut unfrozen_changes) = (0, 0, 0); + let mut frozen_warped = 0; + for y in bounds().y0..bounds().y1 { + for x in bounds().x0..bounds().x1 { + let pixel = before.pixel(x, y); + if field.freeze_at(f64::from(x) + 0.5, f64::from(y) + 0.5) == 1.0 { + protected += 1; + frozen_warped += usize::from(pixel != source.pixel(x, y)); + let alpha = pixel[mode.color_channels()]; + fractional_alpha += usize::from(alpha > 0.0 && alpha < 1.0); + // An unchanged F32 pixel can round on premultiply/unpremultiply. + // Integer-depth pixels must remain exactly equal. + let tolerance = if sample == SampleType::F32 { 4.0 * f32::EPSILON } else { 0.0 }; + for (channel, (actual, expected)) in after.pixel(x, y).iter().zip(&pixel).enumerate() { + assert!( + (actual - expected).abs() <= tolerance, + "{mode:?}/{sample:?}, mesh {cell}, prior warp {prior_warp}: frozen channel {channel} changed at {x},{y}: {expected} -> {actual}" + ); + } + } else if field.freeze_at(f64::from(x) + 0.5, f64::from(y) + 0.5) == 0.0 { + unfrozen_changes += usize::from(after.pixel(x, y) != pixel); + } + } + } + assert!(protected > 7_000 && fractional_alpha > 3_000, "the fixture must cover frozen pixels with fractional alpha"); + assert_eq!(frozen_warped > 0, prior_warp, "the fixture must retain the earlier warp inside the fully frozen region"); + assert!(unfrozen_changes > 0, "the adjacent unfrozen region must still move"); + let replay = LiquifyField::from_strokes(bounds(), cell, &strokes); + assert!(field == replay, "stroke replay must reproduce the field exactly"); + assert!(apply_liquify(&source, &replay) == after, "stroke replay must reproduce the final pixels exactly"); + } + } + } +} + +#[test] +fn fresh_frozen_output_preserves_color_and_alpha_across_formats() { + check_frozen_output(false); +} + +#[test] +fn existing_frozen_mapping_survives_adjacent_warps_across_formats() { + check_frozen_output(true); +} diff --git a/crates/algo/tests/it/liquify_source_footprint.rs b/crates/algo/tests/it/liquify_source_footprint.rs new file mode 100644 index 0000000000..759567827f --- /dev/null +++ b/crates/algo/tests/it/liquify_source_footprint.rs @@ -0,0 +1,108 @@ +//! Liquify source reads must depend on the mapping, not the output tile phase. +use photocraft_algo::liquify::{LiquifyField, LiquifyStroke, LiquifyTool, apply_liquify}; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_geom::Rect; +use photocraft_raster::Surface; + +/// Keeps the document size fixed while moving its edges relative to global tiles. +fn bounds(shift: i32) -> Rect { + Rect::new(shift, 0, shift + 1024, 512) +} + +/// Builds translated copies of RGB ramps and alternating columns so missing +/// interpolation taps change the sampled colors. +fn source(sample: SampleType, shift: i32) -> Surface { + let mut surface = Surface::new(PixelFormat::new(ColorMode::Rgb, sample, false)); + let mut row = vec![0.0; 1024 * 3]; + for y in 0..512 { + for (x, pixel) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() { + *pixel = [x as f32 / 1024.0, (x % 2) as f32, y as f32 / 512.0]; + } + surface.write_region(Rect::new(shift, y, shift + 1024, y + 1), &row); + } + surface +} + +/// Creates matching brush paths in document-relative coordinates, optionally +/// beside a frozen region that gives the displacement field a sharp boundary. +fn strokes(shift: i32, masked: bool) -> Vec { + let offset = f64::from(shift); + let mut strokes = Vec::new(); + if masked { + let mut freeze = LiquifyStroke::new(LiquifyTool::LassoMask, 1.0); + freeze.amount = Some(1.0); + freeze.points = vec![vec![offset, 0.0], vec![offset + 508.0, 0.0], vec![offset + 508.0, 512.0], vec![offset, 512.0]]; + strokes.push(freeze); + } + let mut warp = LiquifyStroke::new(LiquifyTool::ForwardWarp, 512.0); + warp.density = 100.0; + warp.points = vec![vec![offset + 512.0, 256.5], vec![offset + 499.2, 256.5]]; + strokes.extend(std::iter::repeat_n(warp, 8)); + for stroke in &strokes { + stroke.validate().expect("the fixture uses supported brush parameters"); + } + strokes +} + +/// Checks that a half-tile translation preserves editable output across sample +/// depths and mesh sizes when image content and brush paths move together. +fn check_tile_translation(masked: bool) { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let original = source(sample, 0); + let translated = source(sample, 128); + for cell in [2.0, 4.0] { + let field = LiquifyField::from_strokes(bounds(0), cell, &strokes(0, masked)); + let shifted_field = LiquifyField::from_strokes(bounds(128), cell, &strokes(128, masked)); + if masked { + assert!(field.d == shifted_field.d, "translation must preserve the nodal displacement values"); + } + // The reported pixels lie entirely on the editable side of the mask. + assert_eq!(field.freeze_at(509.5, 220.5), 0.0); + assert_eq!(shifted_field.freeze_at(637.5, 220.5), 0.0); + let output = apply_liquify(&original, &field); + let shifted_output = apply_liquify(&translated, &shifted_field); + assert_ne!(output.pixel(509, 220), original.pixel(509, 220), "the fixture must actually warp the inspected strip"); + let mut worst = 0.0f32; + // Translation changes which output tile contains this strip, while + // preserving the relative source image, brush path and document edges. + for y in 220..290 { + for x in 509..512 { + let a = output.pixel(x, y); + let b = shifted_output.pixel(x + 128, y); + for (a, b) in a.iter().zip(&b) { + worst = worst.max((a - b).abs()); + } + } + } + // Coordinate translation can perturb the last few F32 arithmetic bits. + // This is far smaller than the clipped source-stencil error. + let tolerance = if sample == SampleType::F32 { 2e-6 } else { 0.0 }; + assert!(worst <= tolerance, "{sample:?}, meshSize {cell}, masked={masked}: moving the document by half a tile changed output by {worst}"); + } + } +} + +#[test] +fn cubic_source_footprint_is_independent_of_output_tile_phase() { + check_tile_translation(true); +} + +#[test] +fn ordinary_unmasked_warp_is_independent_of_output_tile_phase() { + check_tile_translation(false); +} + +#[test] +fn source_reads_still_clamp_at_the_document_edge() { + let mut warp = LiquifyStroke::new(LiquifyTool::ForwardWarp, 256.0); + warp.density = 100.0; + warp.points = vec![vec![1008.0, 256.5], vec![944.0, 256.5]]; + warp.validate().expect("the edge fixture uses supported brush parameters"); + let field = LiquifyField::from_strokes(bounds(0), 4.0, &[warp]); + assert!(field.sample(1023.5, 256.5)[0] > 4.0, "the fixture must sample beyond the right document edge"); + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let original = source(sample, 0); + let output = apply_liquify(&original, &field); + assert_eq!(output.pixel(1023, 256), original.pixel(1023, 256), "{sample:?}: an out-of-document source read must repeat the true document edge"); + } +} diff --git a/crates/algo/tests/it/main.rs b/crates/algo/tests/it/main.rs new file mode 100644 index 0000000000..1f26e46b8e --- /dev/null +++ b/crates/algo/tests/it/main.rs @@ -0,0 +1,7 @@ +//! The crate's integration tests, linked into one test binary. + +mod liquify_freeze_formats; +mod liquify_source_footprint; +mod retouch_blur_exact; +mod retouch_sharpen_exact; +mod warp_clipped; diff --git a/crates/algo/tests/retouch_blur_exact.rs b/crates/algo/tests/it/retouch_blur_exact.rs similarity index 100% rename from crates/algo/tests/retouch_blur_exact.rs rename to crates/algo/tests/it/retouch_blur_exact.rs diff --git a/crates/algo/tests/it/retouch_sharpen_exact.rs b/crates/algo/tests/it/retouch_sharpen_exact.rs new file mode 100644 index 0000000000..9e75a261ab --- /dev/null +++ b/crates/algo/tests/it/retouch_sharpen_exact.rs @@ -0,0 +1,82 @@ +//! Frozen scalar sharpen from main, used as an exact-output oracle. +use photocraft_algo::retouch::{local_blur, local_sharpen}; + +#[test] +fn matches_scalar_sharpen_bit_for_bit_across_layouts_edges_and_hdr() { + for (w, h) in [(1, 1), (3, 7), (37, 29)] { + for ch in 1..=5 { + for alpha in std::iter::once(None).chain((0..ch).map(Some)) { + let data: Vec = (0..w * h * ch) + .map(|i| if Some(i % ch) == alpha { [0.0, 1e-8, 0.25, 0.75, 1.0][(i / ch) % 5] } else { ((i * 73 + 19) % 113) as f32 / 17.0 - 2.0 }) + .collect(); + for win in [(0, 0, w, h), (w / 2, h / 2, w, h), (0, 0, 1, 1), (0, 0, 0, h)] { + for amount in [0.0, 0.5, 1.0, 2.5] { + for protect_detail in [false, true] { + let expected = scalar_local_sharpen(&data, w, h, ch, alpha, win, amount, protect_detail); + let actual = local_sharpen(&data, w, h, ch, alpha, win, amount, protect_detail); + assert_eq!(actual.len(), expected.len()); + for (i, (a, b)) in actual.iter().zip(&expected).enumerate() { + assert_eq!( + a.to_bits(), + b.to_bits(), + "{w}x{h} ch={ch} alpha={alpha:?} win={win:?} amount={amount} protect={protect_detail} sample={i}" + ); + } + } + } + } + } + } + } +} + +#[test] +fn empty_channel_layout_remains_empty() { + assert!(local_sharpen(&[], 5, 5, 0, None, (0, 0, 5, 5), 1.0, true).is_empty()); +} + +#[allow(clippy::too_many_arguments)] +fn scalar_local_sharpen( + data: &[f32], + w: usize, + h: usize, + ch: usize, + alpha: Option, + win: (usize, usize, usize, usize), + amount: f32, + protect_detail: bool, +) -> Vec { + let (x0, y0, x1, y1) = win; + let (ow, oh) = (x1 - x0, y1 - y0); + if ow == 0 || oh == 0 || ch == 0 { + return Vec::new(); + } + let blur = local_blur(data, w, h, ch, alpha, win, 1.0); + let mut out = vec![0.0f32; blur.len()]; + for y in y0..y1 { + for x in x0..x1 { + let o = ((y - y0) * ow + (x - x0)) * ch; + let i = (y * w + x) * ch; + for c in 0..ch { + let v = data[i + c]; + if Some(c) == alpha { + out[o + c] = v; + continue; + } + let mut s = v + amount * (v - blur[o + c]); + if protect_detail { + let (mut lo, mut hi) = (v, v); + for (dx, dy) in [(-1i32, -1i32), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] { + let (nx, ny) = ((x as i32 + dx).clamp(0, w as i32 - 1) as usize, (y as i32 + dy).clamp(0, h as i32 - 1) as usize); + let nv = data[(ny * w + nx) * ch + c]; + lo = lo.min(nv); + hi = hi.max(nv); + } + s = s.clamp(lo, hi); + } + out[o + c] = s; + } + } + } + out +} diff --git a/crates/algo/tests/warp_clipped.rs b/crates/algo/tests/it/warp_clipped.rs similarity index 100% rename from crates/algo/tests/warp_clipped.rs rename to crates/algo/tests/it/warp_clipped.rs diff --git a/crates/automation/Cargo.toml b/crates/automation/Cargo.toml index fb718e5668..ba073af2ff 100644 --- a/crates/automation/Cargo.toml +++ b/crates/automation/Cargo.toml @@ -10,8 +10,8 @@ rust-version.workspace = true workspace = true [dependencies] -photocraft-geom = { workspace = true } photocraft-doc = { workspace = true } +photocraft-raster = { workspace = true } photocraft-compose = { workspace = true } photocraft-codecs = { workspace = true } photocraft-io = { workspace = true } @@ -22,11 +22,18 @@ serde_json = { workspace = true } thiserror = { workspace = true } rmcp = { version = "3.5", default-features = false, features = ["server", "macros", "transport-io"] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-std", "sync", "net", "io-util", "time"] } -base64 = "0.22" +base64 = "0.23" schemars = "1" getrandom = { workspace = true } cap-std = "4" +clap = { workspace = true } [dev-dependencies] +# Synthetic camera raws (#7789). +photocraft-raw = { workspace = true, features = ["testgen"] } rmcp = { version = "3.5", default-features = false, features = ["client", "server", "macros", "transport-io"] } tokio = { version = "1", features = ["rt-multi-thread", "macros", "io-std", "sync", "net", "io-util", "time"] } + +# Reading back an extended attribute the write path should have preserved (#7095). +[target.'cfg(unix)'.dev-dependencies] +rustix = { version = "1.1", default-features = false, features = ["std", "fs"] } diff --git a/crates/automation/src/args.rs b/crates/automation/src/args.rs new file mode 100644 index 0000000000..5c6dd666a7 --- /dev/null +++ b/crates/automation/src/args.rs @@ -0,0 +1,123 @@ +//! The control and automation launch options, as clap types: `photocraft-cli mcp` and `serve` +//! flatten them, and so does the desktop app (#7071), so each flag and its environment variable +//! are declared once. A flag wins over its variable. +//! +//! Parse with [`blank_env_is_unset`] applied to the arguments, so an empty variable counts as unset, +//! and pass a parse error through [`name_env_vars`], so a bad value names the variable it came from. +//! Check the token pair with [`conflict`] only where a token is used, so a stale exported pair +//! doesn't stop a launch that needs none. + +use std::ffi::OsString; +use std::path::PathBuf; + +use clap::builder::NonEmptyStringValueParser; +use clap::error::{ContextKind, ContextValue, ErrorKind}; +use clap::parser::ValueSource; +use clap::{Arg, ArgMatches, Args, Command}; + +use crate::{AuthorizedWorkspace, AutomationError}; + +/// The control token, for a server or for a client of one. +#[derive(Args)] +pub struct ControlArgs { + /// The control token (64 hex digits) + #[arg(long, value_name = "64-HEX", env = "PHOTOCRAFT_CONTROL_TOKEN", hide_env_values = true, value_parser = NonEmptyStringValueParser::new())] + pub control_token: Option, + /// A file holding the control token, used instead of --control-token (a server creates it + /// when missing) + #[arg(long, value_name = "PATH", env = "PHOTOCRAFT_CONTROL_TOKEN_FILE")] + pub control_token_file: Option, +} + +/// The folders automation may touch. Without a root, that direction of access is denied. No +/// variable stands in for them here: one exported for the desktop app must not widen headless +/// `photocraft-cli` access (#1551); the desktop app adds its own. +#[derive(Args)] +pub struct AutomationArgs { + /// The folder automation may read from (none without it) + #[arg(long, value_name = "DIR")] + pub automation_read_root: Option, + /// The folder automation may write to (none without it) + #[arg(long, value_name = "DIR")] + pub automation_write_root: Option, +} + +impl AutomationArgs { + /// The roots, opened as capabilities. + pub fn workspace(&self) -> Result { + AuthorizedWorkspace::new(self.automation_read_root.as_deref(), self.automation_write_root.as_deref()) + } +} + +/// A control port, for clap's `value_parser`: `photocraft --control`, `photocraft-cli serve --port` +/// and PHOTOCRAFT_CONTROL_PORT, which may carry spaces. +pub fn control_port(s: &str) -> Result { + s.trim().parse().map_err(|_| NotAPort) +} + +/// A [`control_port`] value that isn't a port. +#[derive(Debug, PartialEq, Eq, thiserror::Error)] +#[error("expected a port, 0-65535")] +pub struct NotAPort; + +/// For `Command::mut_args`: an empty or blank variable counts as unset, so `PHOTOCRAFT_CONTROL_PORT=` +/// turns a fallback off without `unset` (#2491). clap would pass the empty value to the parser. +pub fn blank_env_is_unset(arg: Arg) -> Arg { + let blank = arg.get_env().and_then(std::env::var_os).is_some_and(|v| v.to_str().is_some_and(|v| v.trim().is_empty())); + if blank { arg.env(None::<&str>) } else { arg } +} + +/// clap names the flag when a value from its variable is bad or conflicts with another, as if the +/// flag had been given: name the variable instead when the flag isn't among `args` (without the +/// binary name; a first one naming a subcommand selects whose arguments to look in) (#2491). +pub fn name_env_vars(mut error: clap::Error, cmd: &Command, args: &[OsString]) -> clap::Error { + // `--flag value` or `--flag=value`. + let given = |long: &str| args.iter().filter_map(|a| a.to_str()?.strip_prefix("--")?.strip_prefix(long)).any(|r| r.is_empty() || r.starts_with('=')); + let sub = args.first().and_then(|name| cmd.find_subcommand(name)).unwrap_or(cmd); + for kind in [ContextKind::InvalidArg, ContextKind::PriorArg] { + let Some(ContextValue::String(shown)) = error.get(kind) else { continue }; + let Some(arg) = sub.get_arguments().find(|a| a.to_string() == *shown) else { continue }; + let (Some(long), Some(var)) = (arg.get_long(), arg.get_env()) else { continue }; + if !given(long) { + error.insert(kind, ContextValue::String(var.to_string_lossy().into_owned())); + } + } + error +} + +/// clap's either/or usage error when `m`, parsed by `cmd`, has both `a` and `b`, each named as it was +/// given: the flag, or its variable. For a conflict that depends on the mode, such as the token pair +/// (only a server or bridge reads a token), which clap's `conflicts_with` can't express. +pub fn conflict(cmd: &mut Command, m: &ArgMatches, a: &str, b: &str) -> Result<(), clap::Error> { + if m.value_source(a).is_none() || m.value_source(b).is_none() { + return Ok(()); + } + let shown = |id: &str| { + let arg = cmd.get_arguments().find(|arg| arg.get_id() == id); + match (m.value_source(id), arg.and_then(Arg::get_env)) { + (Some(ValueSource::EnvVariable), Some(var)) => var.to_string_lossy().into_owned(), + _ => arg.map_or_else(|| id.to_owned(), |arg| arg.to_string()), + } + }; + let message = format!("the argument '{}' cannot be used with '{}'", shown(a), shown(b)); + Err(cmd.error(ErrorKind::ArgumentConflict, message)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn control_port_accepts_valid_numbers() { + assert_eq!(control_port("50494"), Ok(50494)); + assert_eq!(control_port(" 8080 "), Ok(8080)); + assert_eq!(control_port("0"), Ok(0)); + } + + #[test] + fn control_port_rejects_anything_else() { + for bad in ["78787", "-1", "", "x"] { + assert_eq!(control_port(bad), Err(NotAPort), "{bad:?}"); + } + } +} diff --git a/crates/automation/src/files.rs b/crates/automation/src/files.rs index cd34ab3410..0b8ff103aa 100644 --- a/crates/automation/src/files.rs +++ b/crates/automation/src/files.rs @@ -19,7 +19,8 @@ pub struct Opened { /// Decode a document that was read through a filesystem capability. pub fn open_bytes(name: &str, bytes: &[u8]) -> Result { if Path::new(name).extension().is_some_and(|extension| extension.eq_ignore_ascii_case(photocraft_format::EXTENSION)) { - return Ok(Opened { document: photocraft_format::load_from_bytes(bytes)?, warnings: Vec::new(), source_read_only: false }); + let document = photocraft_format::load_from_bytes(bytes)?; + return Ok(Opened { warnings: photocraft_io::open_warnings(&document), document, source_read_only: false }); } let r = photocraft_io::import(name, bytes)?; Ok(Opened { document: r.document, warnings: r.warnings, source_read_only: r.source_read_only }) @@ -29,11 +30,26 @@ pub fn open_bytes(name: &str, bytes: &[u8]) -> Result { /// natively; everything else goes through `photocraft-io` (PSD, PNG, …). pub fn open(path: &Path) -> Result { if path.is_dir() { - return Ok(Opened { document: photocraft_format::load_path(path)?, warnings: Vec::new(), source_read_only: false }); + let document = photocraft_format::load_path(path)?; + return Ok(Opened { warnings: photocraft_io::open_warnings(&document), document, source_read_only: false }); } - let bytes = photocraft_format::read_file(path).map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); - open_bytes(&name, &bytes) + // A PSD/PSB is parsed from the open file (channel data read as it is decoded), so a CLI + // convert or info of a 2 GB file does not hold the file in memory next to the document. + #[cfg(any(unix, windows))] + { + let r = + photocraft_io::import_file(&name, path, &photocraft_io::ImportOptions::default(), &photocraft_raster::Interrupt::NONE).map_err(|e| match e { + photocraft_io::IoError::Read(msg) => AutomationError::Io(msg), + e => e.into(), + })?; + Ok(Opened { document: r.document, warnings: r.warnings, source_read_only: r.source_read_only }) + } + #[cfg(not(any(unix, windows)))] + { + let bytes = photocraft_format::read_file(path).map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; + open_bytes(&name, &bytes) + } } /// Previews for a `.pcraft` bundle. diff --git a/crates/automation/src/headless.rs b/crates/automation/src/headless.rs index d6b3c17102..6722c5c00b 100644 --- a/crates/automation/src/headless.rs +++ b/crates/automation/src/headless.rs @@ -72,6 +72,22 @@ impl Headless { } } + /// Run `body` with document `index` active (the active one when `None`), then make the + /// previously active document active again, on success and on error alike: an `index` + /// targets one call and must not retarget the next automation command (#7025). + fn on_doc(&mut self, index: Option, body: impl FnOnce(&mut Self) -> Result) -> Result { + let i = self.doc_index(index)?; + let previously_active = self.session.active_index(); + if previously_active != Some(i) && !self.session.set_active(i) { + return Err(AutomationError::BadRequest(format!("no document at index {i}"))); + } + let result = body(self); + if let Some(active) = previously_active.filter(|&active| self.session.active_index() != Some(active)) { + self.session.set_active(active); + } + result + } + /// Open a file and make it the active document. pub fn open(&mut self, path: &Path) -> Result { let requested = path.to_str().ok_or_else(|| AutomationError::BadRequest("automation paths must be valid UTF-8".into()))?; @@ -84,6 +100,8 @@ impl Headless { files::open_bytes(name, &bytes)? } }; + // Preferences › Raw Defaults › Resolution, as the app's File › Open applies it. + file_cmds::raw_defaults(&self.session, &o.warnings, &mut o.document); let path = match file_cmds::template_name(&self.session, requested) { Some(untitled) => { o.document.name = untitled; @@ -113,27 +131,47 @@ impl Headless { /// automation (#2810). `path` resolves beneath the read root the way `open` resolves one, /// and `params` carries the command's own options (`scale`, `fit`, `center`). pub fn place(&mut self, index: Option, path: &str, params: &Value) -> Result { - let i = self.doc_index(index)?; - if self.session.active_index() != Some(i) && !self.session.set_active(i) { - return Err(AutomationError::BadRequest(format!("no document at index {i}"))); - } - let mut opts = params.as_object().cloned().unwrap_or_default(); - opts.insert("path".into(), json!(path)); - // Where ambient paths are allowed the command reads the file itself, as everywhere else. - if matches!(&self.filesystem, Filesystem::TrustedLocal) { - return self.command_run("file.placeEmbedded", Value::Object(opts)); - } - let bytes = match &self.filesystem { - Filesystem::Workspace(workspace) => workspace.read(path)?, - _ => { - return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())); + self.on_doc(index, |this| { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("path".into(), json!(path)); + // Where ambient paths are allowed the command reads the file itself, as everywhere else. + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + return this.command_run("file.placeEmbedded", Value::Object(opts)); } - }; - let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path); - self.sync_jobs(); - // The bytes are already in memory and the path resolved beneath the root capability, so - // placing them needs no filesystem check of its own (`place_bytes` is the command body). - Ok(file_cmds::place_bytes(&mut self.session, name, bytes, None, &Value::Object(opts))?) + let bytes = match &this.filesystem { + Filesystem::Workspace(workspace) => workspace.read(path)?, + _ => { + return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())); + } + }; + let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path); + this.sync_jobs(); + // The bytes are already in memory and the path resolved beneath the root capability, so + // placing them needs no filesystem check of its own (`place_bytes` is the command body). + Ok(file_cmds::place_bytes(&mut this.session, name, bytes, None, &Value::Object(opts))?) + }) + } + + /// Replace a smart object's contents with a file from the read root: the capability-scoped + /// `layer.smartObjects.replaceContents`, which takes an ambient `path` and is refused in + /// automation (#8061). `params` is the command's own (`layer`). + pub fn replace_smart_object(&mut self, index: Option, path: &str, params: &Value) -> Result { + self.on_doc(index, |this| { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("path".into(), json!(path)); + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + return this.command_run("layer.smartObjects.replaceContents", Value::Object(opts)); + } + let bytes = match &this.filesystem { + Filesystem::Workspace(workspace) => workspace.read(path)?, + _ => { + return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())); + } + }; + let name = Path::new(path).file_name().and_then(|name| name.to_str()).unwrap_or(path); + this.sync_jobs(); + Ok(photocraft_engine::smart_cmds::replace_contents_from_bytes(&mut this.session, &Value::Object(opts), name, bytes)?) + }) } /// Export each exported layer of a document as its own file beneath the write root: the @@ -143,25 +181,29 @@ impl Headless { /// already exists, where the ambient command would, and it creates the directory before the /// export runs, so a refused export can leave an empty directory behind — never a file. pub fn export_layers(&mut self, index: Option, dir: &str, params: &Value) -> Result { - let i = self.doc_index(index)?; - if self.session.active_index() != Some(i) && !self.session.set_active(i) { - return Err(AutomationError::BadRequest(format!("no document at index {i}"))); - } - if matches!(&self.filesystem, Filesystem::TrustedLocal) { - let mut opts = params.as_object().cloned().unwrap_or_default(); - opts.insert("dir".into(), json!(dir)); - return self.command_run("file.export.layersToFiles", Value::Object(opts)); - } - self.sync_jobs(); - let workspace = match &self.filesystem { - Filesystem::Workspace(workspace) => workspace, - _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: write authority is absent".into())), - }; - let mut outputs = workspace.export_sequence(dir)?; - let written = file_cmds::export_layers_with(&self.session, params, |name, bytes| { - outputs.write(name, bytes).map_err(|e| photocraft_engine::EngineError::Other(e.to_string())) - }); - exported(dir, written, &mut outputs) + self.on_doc(index, |this| { + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("dir".into(), json!(dir)); + return this.command_run("file.export.layersToFiles", Value::Object(opts)); + } + this.sync_jobs(); + let workspace = match &this.filesystem { + Filesystem::Workspace(workspace) => workspace, + _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: write authority is absent".into())), + }; + let mut outputs = workspace.export_sequence(dir)?; + let report = file_cmds::export_layers_report_with(&this.session, params, |name, bytes| { + outputs.write(name, bytes).map_err(|e| photocraft_engine::EngineError::Other(e.to_string())) + }); + let (written, skipped) = match report { + Ok(report) => (Ok(report.files), report.skipped), + Err(error) => (Err(error), Vec::new()), + }; + let mut result = exported(dir, written, &mut outputs)?; + result["skipped"] = json!(skipped); + Ok(result) + }) } /// Export each data set of a document as its own flattened file beneath the write root: the @@ -169,25 +211,23 @@ impl Headless { /// batch is planned first and colliding names are refused before any file is written; the /// directory itself is created before that, so a refused batch can leave it empty behind. pub fn export_data_sets(&mut self, index: Option, dir: &str, params: &Value) -> Result { - let i = self.doc_index(index)?; - if self.session.active_index() != Some(i) && !self.session.set_active(i) { - return Err(AutomationError::BadRequest(format!("no document at index {i}"))); - } - if matches!(&self.filesystem, Filesystem::TrustedLocal) { - let mut opts = params.as_object().cloned().unwrap_or_default(); - opts.insert("dir".into(), json!(dir)); - return self.command_run("file.export.dataSetsAsFiles", Value::Object(opts)); - } - self.sync_jobs(); - let workspace = match &self.filesystem { - Filesystem::Workspace(workspace) => workspace, - _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: write authority is absent".into())), - }; - let mut outputs = workspace.export_sequence(dir)?; - let written = photocraft_engine::export_data_sets_with(&self.session, params, |name, bytes| { - outputs.write(name, bytes).map_err(|e| photocraft_engine::EngineError::Other(e.to_string())) - }); - exported(dir, written, &mut outputs) + self.on_doc(index, |this| { + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("dir".into(), json!(dir)); + return this.command_run("file.export.dataSetsAsFiles", Value::Object(opts)); + } + this.sync_jobs(); + let workspace = match &this.filesystem { + Filesystem::Workspace(workspace) => workspace, + _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: write authority is absent".into())), + }; + let mut outputs = workspace.export_sequence(dir)?; + let written = photocraft_engine::export_data_sets_with(&this.session, params, |name, bytes| { + outputs.write(name, bytes).map_err(|e| photocraft_engine::EngineError::Other(e.to_string())) + }); + exported(dir, written, &mut outputs) + }) } /// Import a data set CSV from the read root: the capability-scoped @@ -195,24 +235,22 @@ impl Headless { /// in automation (#3216). The file is read beneath the root the way `open` reads one and /// handed to the command's own parser over the text it already has. pub fn import_data_sets(&mut self, index: Option, path: &str, params: &Value) -> Result { - let i = self.doc_index(index)?; - if self.session.active_index() != Some(i) && !self.session.set_active(i) { - return Err(AutomationError::BadRequest(format!("no document at index {i}"))); - } - if matches!(&self.filesystem, Filesystem::TrustedLocal) { - let mut opts = params.as_object().cloned().unwrap_or_default(); - opts.insert("path".into(), json!(path)); - return self.command_run("file.import.variableDataSets", Value::Object(opts)); - } - let text = match &self.filesystem { - Filesystem::Workspace(workspace) => { - let bytes = workspace.read(path)?; - String::from_utf8(bytes).map_err(|e| AutomationError::BadRequest(format!("automation read `{path}` is not valid UTF-8: {e}")))? + self.on_doc(index, |this| { + if matches!(&this.filesystem, Filesystem::TrustedLocal) { + let mut opts = params.as_object().cloned().unwrap_or_default(); + opts.insert("path".into(), json!(path)); + return this.command_run("file.import.variableDataSets", Value::Object(opts)); } - _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())), - }; - self.sync_jobs(); - Ok(photocraft_engine::import_data_sets_text(&mut self.session, &text, params)?) + let text = match &this.filesystem { + Filesystem::Workspace(workspace) => { + let bytes = workspace.read(path)?; + String::from_utf8(bytes).map_err(|e| AutomationError::BadRequest(format!("automation read `{path}` is not valid UTF-8: {e}")))? + } + _ => return Err(AutomationError::BadRequest("automation filesystem access is not granted: read authority is absent".into())), + }; + this.sync_jobs(); + Ok(photocraft_engine::import_data_sets_text(&mut this.session, &text, params)?) + }) } /// Save (`.pcraft` or any export format by extension). With no path, @@ -251,7 +289,10 @@ impl Headless { files::save(&doc, &target, format, opts, Some(writer))? } Filesystem::Workspace(workspace) => { - let (bytes, warnings) = files::save_bytes(&doc, target_text, format, opts)?; + // Only the target is granted: a linked smart object's stored path is document + // data, so PSD export writes that layer as pixels instead of embedding the file. + let opts = ExportOptions { read_linked_files: false, ..opts.clone() }; + let (bytes, warnings) = files::save_bytes(&doc, target_text, format, &opts)?; workspace.write(target_text, &bytes)?; warnings } @@ -266,14 +307,12 @@ impl Headless { format.map(|f| f.trim_start_matches('.').to_ascii_lowercase()).or_else(|| target.extension().map(|e| e.to_string_lossy().to_ascii_lowercase())); if ext.as_deref().is_some_and(|e| file_cmds::holds(&doc, e)) { // Saving by index must not retarget the next automation command. - let previously_active = self.session.active_index(); - self.session.set_active(i); - if let Some(st) = self.session.active_mut() { - st.saved_to(target.to_string_lossy().into_owned()); - } - if let Some(active) = previously_active { - self.session.set_active(active); - } + self.on_doc(Some(i), |this| { + if let Some(st) = this.session.active_mut() { + st.saved_to(target.to_string_lossy().into_owned()); + } + Ok(()) + })?; } Ok(json!({ "path": target.to_string_lossy(), "warnings": warnings })) } diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs index f1e7049d0d..b4691723b9 100644 --- a/crates/automation/src/lib.rs +++ b/crates/automation/src/lib.rs @@ -16,6 +16,7 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] +pub mod args; pub mod bridge; pub mod budgets; pub mod files; diff --git a/crates/automation/src/rpc.rs b/crates/automation/src/rpc.rs index 15a0e24e30..92ece2b943 100644 --- a/crates/automation/src/rpc.rs +++ b/crates/automation/src/rpc.rs @@ -10,6 +10,8 @@ //! - `session.list`, `doc.open {path}`, `doc.new {…file.new params}`, //! `doc.place {path, scale?, fit?, center?, index?}` (#2810: the read root is the scope, //! the counterpart of the refused ambient `file.placeEmbedded`), +//! `doc.replaceSmartObject {path, layer?, index?}` (#8061: the same scope for +//! `layer.smartObjects.replaceContents`), //! `doc.exportLayers {dir, format?, prefix?, visibleOnly?, quality?, index?}`, //! `doc.exportDataSets {dir, format?, dataSets?, naming?, index?}`, //! `doc.importDataSets {path, delimiter?, index?}` (#3216: the same treatment for the export @@ -22,7 +24,9 @@ use std::io::{BufRead, Write}; use std::net::TcpListener; use std::path::PathBuf; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, PoisonError}; +use std::time::{Duration, Instant}; use base64::Engine as _; use serde_json::{Value, json}; @@ -43,6 +47,7 @@ pub const METHODS: &[&str] = &[ "session.list", "doc.open", "doc.place", + "doc.replaceSmartObject", "doc.new", "doc.save", "doc.exportLayers", @@ -81,6 +86,9 @@ impl Headless { match method { "engine.execute" => { let id = str_of(&p, "command").ok_or_else(|| bad("engine.execute needs `command`"))?; + if METHODS.contains(&id) && photocraft_engine::commands::find(id).is_none() { + return Err(bad(photocraft_engine::command_hints::protocol_method_as_command(id))); + } let wait = p.get("wait").and_then(Value::as_bool).unwrap_or(true); self.command_start(id, p.get("params").cloned().unwrap_or(Value::Null), wait) } @@ -108,16 +116,33 @@ impl Headless { opts.remove("index"); self.place(index_of(&p)?, path, &Value::Object(opts)) } + "doc.replaceSmartObject" => { + let path = str_of(&p, "path").ok_or_else(|| bad("doc.replaceSmartObject needs `path`"))?; + let mut opts = p.as_object().cloned().unwrap_or_default(); + opts.remove("path"); + opts.remove("index"); + self.replace_smart_object(index_of(&p)?, path, &Value::Object(opts)) + } "doc.new" => self.command_run("file.new", p), "doc.save" => { let tga_bits = photocraft_engine::file_cmds::tga_bits_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?; let mut opts = photocraft_io::ExportOptions { tiff_layers: p.get("tiffLayers").and_then(Value::as_bool).unwrap_or(false), tga_bits, ..Default::default() }; opts.encode.dpx_bits = photocraft_engine::file_cmds::dpx_bits_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?; - if let Some(q) = p.get("quality").and_then(Value::as_u64) { - opts.encode.jpeg_quality = q.clamp(1, 100) as u8; - opts.encode.webp_quality = q.clamp(1, 100) as u8; - opts.encode.webp_lossless = false; + opts.encode.dds = photocraft_engine::file_cmds::dds_param(&p, "doc.save").map_err(|e| bad(e.to_string()))?.unwrap_or_default(); + match p.get("quality") { + None | Some(Value::Null) => {} + Some(v) => { + // Like the CLI's `--quality`: never ignore or clamp a bad value. + let q = v + .as_u64() + .filter(|q| (1..=100).contains(q)) + .and_then(|q| u8::try_from(q).ok()) + .ok_or_else(|| bad(format!("`quality` must be a whole number from 1 to 100, got {v}")))?; + opts.encode.jpeg_quality = q; + opts.encode.webp_quality = q; + opts.encode.webp_lossless = false; + } } let path = str_of(&p, "path").map(PathBuf::from); self.save(index_of(&p)?, path.as_deref(), str_of(&p, "format"), &opts) @@ -164,7 +189,7 @@ impl Headless { "doc.close" => self.close(index_of(&p)?), "batch" => self.batch(&p), "methods" => Ok(json!(METHODS)), - other => Err(bad(format!("unknown method `{other}` (try `methods`)"))), + other => Err(bad(format!("{} (try `methods`)", photocraft_engine::command_hints::unknown_method(other, METHODS)))), } } @@ -305,644 +330,68 @@ fn serve_tcp_connection(h: &Mutex, stream: std::net::TcpStream, token: } } +/// Headless TCP daemons with no clients should eventually release their listening port (#3682). +const TCP_IDLE_TIMEOUT: Duration = Duration::from_secs(30 * 60); + /// Serve authenticated JSON lines on a loopback TCP address with one shared session. -/// Refuses non-loopback addresses and caps active connections and request bytes. +/// Refuses non-loopback addresses, caps connections and request bytes, and closes the listener +/// after an idle period with no connections. Active clients are never evicted by this timer. pub fn serve_tcp(addr: &str, h: Arc>, token: String, ready: impl FnOnce(std::net::SocketAddr)) -> Result<(), AutomationError> { + serve_tcp_until_idle(addr, h, token, ready, TCP_IDLE_TIMEOUT) +} + +fn serve_tcp_until_idle( + addr: &str, + h: Arc>, + token: String, + ready: impl FnOnce(std::net::SocketAddr), + idle: Duration, +) -> Result<(), AutomationError> { let listener = TcpListener::bind(addr).map_err(|e| AutomationError::Io(format!("bind {addr}: {e}")))?; let local = listener.local_addr().map_err(|e| AutomationError::Io(e.to_string()))?; if !local.ip().is_loopback() { return Err(bad(format!("{addr} is not a loopback address"))); } + listener.set_nonblocking(true).map_err(|e| AutomationError::Io(e.to_string()))?; ready(local); let limiter = ConnectionLimiter::new(MAX_CONNECTIONS); let token = Arc::new(token); - for stream in listener.incoming() { - let Ok(mut stream) = stream else { continue }; - let Some(permit) = limiter.try_acquire() else { - let _ = configure_stream(&stream); - let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "connection limit reached"})); - continue; - }; - let h = h.clone(); - let token = Arc::clone(&token); - std::thread::spawn(move || { - let _permit = permit; - let _ = serve_tcp_connection(&h, stream, &token, AUTH_DEADLINE); - }); + let active = Arc::new(AtomicUsize::new(0)); + let mut last_active = Instant::now(); + loop { + match listener.accept() { + Ok((mut stream, _)) => { + last_active = Instant::now(); + let Some(permit) = limiter.try_acquire() else { + let _ = configure_stream(&stream); + let _ = writeln!(stream, "{}", json!({"id": null, "ok": false, "error": "connection limit reached"})); + continue; + }; + // The listener is nonblocking; individual requests still use blocking socket I/O. + let _ = stream.set_nonblocking(false); + let h = h.clone(); + let token = Arc::clone(&token); + let active = Arc::clone(&active); + active.fetch_add(1, Ordering::AcqRel); + std::thread::spawn(move || { + let _permit = permit; + let _ = serve_tcp_connection(&h, stream, &token, AUTH_DEADLINE); + active.fetch_sub(1, Ordering::AcqRel); + }); + } + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + if active.load(Ordering::Acquire) != 0 { + last_active = Instant::now(); + } else if last_active.elapsed() >= idle { + return Ok(()); + } + std::thread::sleep(Duration::from_millis(100)); + } + Err(e) if matches!(e.kind(), std::io::ErrorKind::Interrupted | std::io::ErrorKind::ConnectionAborted) => {} + Err(e) => return Err(AutomationError::Io(e.to_string())), + } } - Ok(()) } #[cfg(test)] -mod tests { - use super::*; - - fn session() -> Mutex { - Mutex::new(Headless::trusted_local()) - } - - #[test] - fn execute_reports_invalid_filter_parameters_without_changing_history() { - let h = session(); - assert_eq!(respond(&h, r#"{"method":"doc.new","params":{"width":8,"height":8}}"#)["ok"], true); - let before = h.lock().unwrap().inspect(None).unwrap(); - for (params, key) in - [(json!({"radius":"big"}), "radius"), (json!({"radus":3}), "radus"), (json!({"radius":5000}), "radius"), (json!({"radius":-4}), "radius")] - { - for wait in [true, false] { - let request = json!({"method":"engine.execute","params":{"command":"filter.blur.gaussianBlur","params":params,"wait":wait}}); - let reply = respond(&h, &request.to_string()); - assert_eq!(reply["ok"], false); - let error = reply["error"].as_str().unwrap(); - assert!(error.contains("filter.blur.gaussianBlur") && error.contains(key), "{error}"); - assert_eq!(h.lock().unwrap().inspect(None).unwrap(), before); - } - } - } - - /// Pins the reply shape documented in docs/control-protocol.md › Command list replies - /// (#3684): a bare JSON array (not `{"commands": [...]}`), one object per command with - /// exactly these keys (plus an optional `keywords` string), from `engine.commands` (filtered or not) and `command.list`. - #[test] - fn command_list_replies_are_documented_array_of_objects() { - let h = session(); - let mut g = h.lock().unwrap(); - let all = g.handle("engine.commands", json!({})).unwrap(); - let filtered = g.handle("engine.commands", json!({"filter": "BLUR"})).unwrap(); - let engine = g.handle("engine.execute", json!({"command": "command.list"})).unwrap(); - for (name, reply) in [("engine.commands", &all), ("engine.commands {filter}", &filtered), ("command.list", &engine)] { - let items = reply.as_array().unwrap_or_else(|| panic!("{name} must reply with a bare array, got {reply}")); - assert!(!items.is_empty(), "{name}"); - for c in items { - // `keywords` is optional: only commands with other search names carry it (#5060). - let mut keys: Vec<&str> = c.as_object().unwrap().keys().map(String::as_str).filter(|k| *k != "keywords").collect(); - keys.sort_unstable(); - assert!(c.get("keywords").is_none_or(Value::is_string), "{name}: {c}"); - assert_eq!(keys, ["disabledReason", "enabled", "id", "label", "menu", "needsDocument", "params", "shortcut"], "{name}: {c}"); - assert!(c["id"].is_string() && c["label"].is_string() && c["params"].is_string() && c["enabled"].is_boolean(), "{c}"); - assert!(c["needsDocument"].is_boolean(), "{c}"); - assert_eq!(c["disabledReason"].is_null(), c["enabled"] == true, "disabledReason is null exactly when enabled: {c}"); - assert!(c["menu"].as_array().unwrap().iter().all(Value::is_string), "{c}"); - assert!(c["shortcut"].is_string() || c["shortcut"].is_null(), "{c}"); - } - } - let n_all = all.as_array().unwrap().len(); - assert_eq!(engine.as_array().unwrap().len(), n_all); - let filtered = filtered.as_array().unwrap(); - assert!(filtered.len() < n_all); - assert!(filtered.iter().all(|c| format!("{} {}", c["id"], c["label"]).to_lowercase().contains("blur"))); - let new = all.as_array().unwrap().iter().find(|c| c["id"] == "file.new").unwrap(); - assert_eq!((&new["menu"], &new["shortcut"], &new["enabled"]), (&json!(["File"]), &json!("Cmd+N"), &json!(true)), "{new}"); - assert_eq!((&new["disabledReason"], &new["needsDocument"]), (&Value::Null, &json!(false)), "{new}"); - } - - #[test] - fn lines_round_trip_and_errors() { - let h = session(); - let input = concat!( - r#"{"id":1,"method":"doc.new","params":{"width":64,"height":32,"background":"white"}}"#, - "\n", - "\n", - r#"{"id":2,"method":"engine.execute","params":{"command":"layer.new.layer","params":{"name":"Ink"}}}"#, - "\n", - r#"{"id":3,"method":"doc.inspect"}"#, - "\n", - r#"{"id":4,"method":"nope"}"#, - "\n", - "not json\n", - ); - let mut out = Vec::new(); - serve_lines(&h, input.as_bytes(), &mut out).unwrap(); - let replies: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); - assert_eq!(replies.len(), 5); - assert!(replies[0]["ok"].as_bool().unwrap()); - assert_eq!(replies[1]["id"], 2); - assert!(replies[2]["result"].to_string().contains("Ink")); - assert!(!replies[3]["ok"].as_bool().unwrap()); - assert!(replies[3]["error"].as_str().unwrap().contains("unknown method")); - assert_eq!(replies[4]["id"], Value::Null); - } - - /// Regression: after a panic while the session lock was held, every later request - /// failed with "session lock poisoned"; the server now keeps serving. - #[test] - fn poisoned_session_keeps_serving() { - let h = session(); - let _ = std::thread::scope(|sc| { - sc.spawn(|| { - let _g = h.lock().unwrap(); - panic!("poison the session lock"); - }) - .join() - }); - assert!(h.is_poisoned()); - let r = respond(&h, r#"{"id":1,"method":"doc.new","params":{"width":8,"height":8}}"#); - assert_eq!(r["ok"], true, "{r}"); - } - - #[test] - fn batch_stops_on_error_unless_told_not_to() { - let h = session(); - let mut g = h.lock().unwrap(); - g.handle("doc.new", json!({"width": 16, "height": 16})).unwrap(); - let steps = json!([ - {"command": "layer.new.layer", "params": {"name": "A"}}, - {"command": "no.such.command"}, - {"command": "layer.new.layer", "params": {"name": "B"}}, - {"method": "doc.inspect"} - ]); - let r = g.handle("batch", json!({"steps": steps})).unwrap(); - assert_eq!(r["completed"], 1); - assert_eq!(r["failed"], 1); - assert_eq!(r["results"].as_array().unwrap().len(), 2); - let r = g.handle("batch", json!({"steps": steps, "stopOnError": false})).unwrap(); - assert_eq!(r["completed"], 3); - let insp = &r["results"][3]["result"]; - assert!(insp.to_string().contains("\"B\"")); - } - - #[test] - fn batch_rejects_too_many_steps() { - let h = session(); - let mut g = h.lock().unwrap(); - let steps = vec![json!({"command": "command.list"}); MAX_BATCH_STEPS + 1]; - let error = g.handle("batch", json!({"steps": steps})).unwrap_err(); - assert!(error.to_string().contains("maximum is 256")); - } - - #[test] - fn render_to_file_and_base64() { - let h = session(); - let mut g = h.lock().unwrap(); - g.handle("doc.new", json!({"width": 40, "height": 20, "background": "black"})).unwrap(); - let b = g.handle("doc.render", json!({"maxSide": 10})).unwrap(); - let png = base64::engine::general_purpose::STANDARD.decode(b["base64"].as_str().unwrap()).unwrap(); - let img = photocraft_codecs::decode(&png).unwrap(); - assert_eq!(img.dimensions(), (10, 5)); - let path = std::env::temp_dir().join(format!("pc-rpc-{}.png", std::process::id())); - let r = g.handle("doc.render", json!({"path": path.to_string_lossy(), "maxSide": 0})).unwrap(); - assert!(r["bytes"].as_u64().unwrap() > 0); - assert_eq!(photocraft_codecs::decode(&std::fs::read(&path).unwrap()).unwrap().dimensions(), (40, 20)); - let _ = std::fs::remove_file(path); - } - - #[test] - fn background_jobs_list_wait_and_cancel() { - let mut h = Headless::new(); - h.handle("doc.new", json!({"width": 600, "height": 400})).unwrap(); - h.handle("engine.execute", json!({"command": "layer.new.layer"})).unwrap(); - h.handle("engine.execute", json!({"command": "edit.fill", "params": {"color": "#808080"}})).unwrap(); - // Default: waits, the result is the command's. - let r = h.handle("engine.execute", json!({"command": "filter.noise.addNoise", "params": {"amount": 20}})).unwrap(); - assert!(r["filter"].is_object(), "{r}"); - // wait: false returns a job id at once. - let r = h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false})).unwrap(); - let job = r["job"].as_u64().unwrap(); - let listed = h.handle("jobs.list", json!({})).unwrap(); - assert!(listed["jobs"].as_array().unwrap().iter().any(|j| j["id"] == job), "{listed}"); - // Wait for it via jobs.list (which applies finished jobs). - let t = std::time::Instant::now(); - loop { - let l = h.handle("jobs.list", json!({})).unwrap(); - let state = l["jobs"].as_array().unwrap().iter().find(|j| j["id"] == job).map(|j| j["state"].clone()); - if state == Some(json!("done")) { - break; - } - assert!(t.elapsed().as_secs() < 60, "{l}"); - std::thread::sleep(std::time::Duration::from_millis(5)); - } - // Cancel: a second job, cancelled at once, leaves the document unchanged. - let before = h.session.active().unwrap().doc.clone(); - let r = h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false})).unwrap(); - let job = r["job"].as_u64().unwrap(); - h.handle("jobs.cancel", json!({"job": job})).unwrap(); - assert!(h.handle("jobs.cancel", json!({"job": "nope"})).is_err()); - let l = h.handle("jobs.list", json!({})).unwrap(); - assert_eq!(l["jobs"].as_array().unwrap().iter().find(|j| j["id"] == job).unwrap()["state"], "cancelled"); - assert!(std::sync::Arc::ptr_eq(&h.session.active().unwrap().doc, &before)); - } - - #[test] - fn batch_steps_can_start_background_jobs() { - let mut h = Headless::new(); - h.handle("doc.new", json!({"width": 600, "height": 400})).unwrap(); - let steps = json!([ - {"command": "layer.new.layer"}, - {"command": "edit.fill", "params": {"color": "#808080"}}, - {"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false} - ]); - let r = h.handle("batch", json!({"steps": steps})).unwrap(); - assert_eq!(r["completed"], 3, "{r}"); - let job = r["results"][2]["result"]["job"].as_u64().unwrap_or_else(|| panic!("no job id: {r}")); - assert_eq!(r["results"][2]["result"]["pending"], true); - let listed = h.handle("jobs.list", json!({})).unwrap(); - assert!(listed["jobs"].as_array().unwrap().iter().any(|j| j["id"] == job), "{listed}"); - let t = std::time::Instant::now(); - while h.session.jobs().iter().any(|j| j.id.0 == job) { - assert!(t.elapsed().as_secs() < 60); - std::thread::sleep(std::time::Duration::from_millis(5)); - h.handle("jobs.list", json!({})).unwrap(); - } - } - - /// Regression (#503): a finished `wait: false` job was only applied by a later command or - /// `jobs.list`, so save, inspect, render and `session.list` still saw the document without it. - #[test] - fn every_request_sees_a_finished_background_job() { - let dir = std::env::temp_dir().join(format!("pc-rpc-job-sync-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); - let (before, after) = (dir.join("before.png"), dir.join("after.png")); - let mut h = Headless::trusted_local(); - h.handle("doc.new", json!({"width": 64, "height": 48})).unwrap(); - h.handle("engine.execute", json!({"command": "filter.noise.addNoise", "params": {"amount": 50}})).unwrap(); - h.handle("doc.save", json!({"path": before.to_string_lossy()})).unwrap(); - let render = |h: &mut Headless| h.handle("doc.render", json!({"maxSide": 0})).unwrap()["base64"].clone(); - let unblurred = render(&mut h); - let revision = |h: &mut Headless| h.handle("session.list", json!({})).unwrap()["documents"][0]["revision"].clone(); - let start = revision(&mut h); - h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 4}, "wait": false})).unwrap(); - // Only reading requests from here on: no command and no `jobs.list`. - let t = std::time::Instant::now(); - while revision(&mut h) == start { - assert!(t.elapsed().as_secs() < 60, "session.list never saw the finished job"); - std::thread::sleep(std::time::Duration::from_millis(5)); - } - let history = h.handle("doc.inspect", json!({})).unwrap()["history"].clone(); - assert_eq!(history.as_array().unwrap().last().unwrap(), "Gaussian Blur", "{history}"); - h.handle("doc.save", json!({"path": after.to_string_lossy()})).unwrap(); - let decode = |p: &std::path::Path| photocraft_codecs::decode(&std::fs::read(p).unwrap()).unwrap().to_rgba8(); - assert_ne!(decode(&before), decode(&after), "the export holds the blur"); - assert_ne!(render(&mut h), unblurred, "the preview holds the blur"); - std::fs::remove_dir_all(dir).unwrap(); - } - - #[test] - fn automation_preview_rejects_oversized_full_size_and_numeric_wraparound() { - let mut h = Headless::new(); - h.handle("doc.new", json!({"width": 2049, "height": 1})).unwrap(); - for max in [json!(0), json!(2049), json!(u64::from(u32::MAX) + 1), json!(-1), json!(1.5)] { - assert!(h.handle("doc.render", json!({"maxSide": max})).is_err()); - } - // A smaller preview still works, and rejection leaves the source document intact. - let rendered = h.handle("doc.render", json!({"maxSide": 32})).unwrap(); - let png = base64::engine::general_purpose::STANDARD.decode(rendered["base64"].as_str().unwrap()).unwrap(); - assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (32, 1)); - assert_eq!(h.session.active().unwrap().doc.size.width, 2049); - } - - #[test] - fn stdio_rejects_oversized_frames_before_dispatch() { - let h = Mutex::new(Headless::new()); - let input = " ".repeat(MAX_REQUEST_BYTES + 1); - let mut out = Vec::new(); - serve_lines(&h, input.as_bytes(), &mut out).unwrap(); - let reply: Value = serde_json::from_slice(&out).unwrap(); - assert_eq!(reply["ok"], false); - assert!(reply["error"].as_str().unwrap().contains("request exceeds")); - assert!(h.lock().unwrap().session.documents().is_empty()); - } - - /// Regression (#505): an over-long or non-UTF-8 line ended the stdio session, losing every - /// open document. Now it gets one error reply, the rest of the line is skipped (never - /// dispatched), and the next request is served. - #[test] - fn stdio_skips_a_rejected_line_and_keeps_the_session() { - let h = Mutex::new(Headless::new()); - let mut input = b"{\"id\":1,\"method\":\"doc.new\",\"params\":{\"width\":8,\"height\":8}}\n".to_vec(); - input.extend(" ".repeat(MAX_REQUEST_BYTES + 1).into_bytes()); - input.extend(b"{\"id\":9,\"method\":\"doc.close\"}\n"); - input.extend(b"\xff\xfe{\"id\":8,\"method\":\"doc.close\"}\n"); - input.extend(b"{\"id\":2,\"method\":\"session.list\"}\n"); - let mut out = Vec::new(); - serve_lines(&h, input.as_slice(), &mut out).unwrap(); - let replies: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); - assert_eq!(replies.len(), 4, "{replies:?}"); - assert_eq!(replies[0]["ok"], true); - assert!(replies[1]["error"].as_str().unwrap().contains("request exceeds"), "{}", replies[1]); - assert!(replies[2]["error"].as_str().unwrap().contains("not valid UTF-8"), "{}", replies[2]); - assert_eq!(replies[3]["id"], 2); - assert_eq!(replies[3]["result"]["documents"].as_array().unwrap().len(), 1, "the document stays open"); - } - - #[test] - fn preview_rejection_precedes_output_file_creation() { - let root = std::env::temp_dir().join(format!("pc-preview-budget-{}", std::process::id())); - std::fs::create_dir_all(&root).unwrap(); - let workspace = crate::AuthorizedWorkspace::new(None, Some(&root)).unwrap(); - let mut h = Headless::with_workspace(workspace); - h.handle("doc.new", json!({"width": 16, "height": 8})).unwrap(); - let result = h.handle("doc.render", json!({"maxSide": 2049, "path": "rejected.png"})); - assert!(result.unwrap_err().to_string().contains("preview side exceeds")); - assert!(!root.join("rejected.png").exists()); - assert!(h.handle("doc.render", json!({"maxSide": 16, "path": "accepted.png"})).is_ok()); - assert!(root.join("accepted.png").exists()); - // Release directory capabilities before cleanup on Windows. - drop(h); - std::fs::remove_file(root.join("accepted.png")).unwrap(); - std::fs::remove_dir(root).unwrap(); - } - - #[test] - fn doc_place_reads_through_the_read_root_and_never_outside_it() { - let base = std::env::temp_dir().join(format!("pc-doc-place-{}", std::process::id())); - let root = base.join("workspace"); - let outside = base.join("outside"); - for dir in [&root, &outside] { - std::fs::create_dir_all(dir).unwrap(); - } - let img = photocraft_codecs::Image::from_u8(8, 6, photocraft_codecs::ChannelLayout::Rgb, vec![42; 8 * 6 * 3]).unwrap(); - let png = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap(); - std::fs::write(root.join("logo.png"), &png).unwrap(); - std::fs::write(outside.join("logo.png"), &png).unwrap(); - let absolute = outside.join("logo.png").to_string_lossy().into_owned(); - - let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); - h.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); - let placed = h.handle("doc.place", json!({"path": "logo.png", "scale": 50})).unwrap(); - assert!(placed["layer"].as_u64().is_some(), "{placed}"); - assert_eq!(placed["scale"].as_f64(), Some(50.0), "the options reach the command: {placed}"); - let inspected = h.handle("doc.inspect", json!({})).unwrap(); - assert_eq!(inspected["layers"].as_array().map(Vec::len), Some(2), "the placed layer is in the document: {inspected}"); - // A path outside the read root is refused the way the ambient command is (#2810). - for (path, reason) in [("../outside/logo.png", "parent traversal"), (absolute.as_str(), "must be inside the automation root")] { - let refused = h.handle("doc.place", json!({"path": path})).unwrap_err().to_string(); - assert!(refused.contains(reason), "{refused}"); - let inspected = h.handle("doc.inspect", json!({})).unwrap(); - assert_eq!(inspected["layers"].as_array().map(Vec::len), Some(2), "nothing was placed: {refused}"); - } - // Trusted local keeps the ambient command: that caller may read the path itself. - let mut t = Headless::trusted_local(); - t.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); - let trusted = t.handle("doc.place", json!({"path": root.join("logo.png").to_string_lossy()})).unwrap(); - assert!(trusted["layer"].as_u64().is_some(), "{trusted}"); - // Release directory capabilities before cleanup on Windows. - drop(t); - drop(h); - std::fs::remove_dir_all(base).unwrap(); - } - - #[test] - fn doc_export_layers_writes_only_beneath_the_write_root() { - let base = std::env::temp_dir().join(format!("pc-doc-export-layers-{}", std::process::id())); - let root = base.join("workspace"); - std::fs::create_dir_all(&root).unwrap(); - let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); - h.handle("doc.new", json!({"width": 12, "height": 8, "background": "white", "name": "card"})).unwrap(); - h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {"name": "Badge"}})).unwrap(); - - let out = root.join("out"); - let exported = h.handle("doc.exportLayers", json!({"dir": out.to_string_lossy(), "format": "png", "prefix": "card"})).unwrap(); - assert_eq!(exported["count"].as_u64(), Some(2), "{exported}"); - let mut names: Vec = std::fs::read_dir(&out).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect(); - names.sort(); - assert_eq!(names.len(), 2, "{exported}"); - for name in names { - assert!(name.starts_with("card_") && name.ends_with(".png"), "{name}"); - let bytes = std::fs::read(out.join(&name)).unwrap(); - assert_eq!(photocraft_codecs::decode(&bytes).unwrap().dimensions(), (12, 8), "{name}"); - } - // Outside the write root nothing is created, not even the directory. - let escaped = base.join("escaped"); - let refused = h.handle("doc.exportLayers", json!({"dir": escaped.to_string_lossy()})).unwrap_err().to_string(); - assert!(refused.contains("must be inside the automation root"), "{refused}"); - assert!(!escaped.exists()); - // The ambient command keeps its refusal for the very same directory. - let ambient = - h.handle("engine.execute", json!({"command": "file.export.layersToFiles", "params": {"dir": out.to_string_lossy()}})).unwrap_err().to_string(); - assert!(ambient.contains("ambient filesystem paths"), "{ambient}"); - // Release directory capabilities before cleanup on Windows. - drop(h); - std::fs::remove_dir_all(base).unwrap(); - } - - #[test] - fn doc_export_data_sets_refuse_collisions_and_import_reads_the_root() { - let base = std::env::temp_dir().join(format!("pc-doc-data-sets-{}", std::process::id())); - let root = base.join("workspace"); - std::fs::create_dir_all(&root).unwrap(); - let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); - h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "card"})).unwrap(); - let inspected = h.handle("doc.inspect", json!({})).unwrap(); - let layer = inspected["layers"][0]["id"].as_u64().expect("the layer id"); - h.handle("engine.execute", json!({"command": "image.variables.define", "params": {"defs": [{"name": "show", "layer": layer, "type": "visibility"}]}})) - .unwrap(); - h.handle( - "engine.execute", - json!({"command": "image.variables.dataSets", "params": {"dataSets": [ - {"name": "one", "values": [{"variable": "show", "kind": "visibility", "value": true}]}, - {"name": "two", "values": [{"variable": "show", "kind": "visibility", "value": false}]}, - ]}}), - ) - .unwrap(); - - let sets = root.join("sets"); - let exported = h.handle("doc.exportDataSets", json!({"dir": sets.to_string_lossy(), "format": "png"})).unwrap(); - assert_eq!(exported["count"].as_u64(), Some(2), "{exported}"); - assert_eq!(std::fs::read_dir(&sets).unwrap().count(), 2, "{exported}"); - // Colliding names are refused as in the command, before any file is written. - let clash = root.join("clash"); - let refused = h.handle("doc.exportDataSets", json!({"dir": clash.to_string_lossy(), "naming": "{document}"})).unwrap_err().to_string(); - assert!(refused.contains("{index}"), "{refused}"); - assert_eq!(std::fs::read_dir(&clash).unwrap().count(), 0, "no file of the refused batch"); - // A CSV from the read root, and one outside it. - std::fs::write(root.join("sets.csv"), "Data Set,show\none,true\ntwo,false\n").unwrap(); - let imported = h.handle("doc.importDataSets", json!({"path": "sets.csv"})).unwrap(); - assert_eq!(imported["imported"].as_u64(), Some(2), "{imported}"); - std::fs::write(base.join("outside.csv"), "Data Set,show\none,true\n").unwrap(); - let outside = h.handle("doc.importDataSets", json!({"path": base.join("outside.csv").to_string_lossy()})).unwrap_err().to_string(); - assert!(outside.contains("must be inside the automation root"), "{outside}"); - // Release directory capabilities before cleanup on Windows. - drop(h); - std::fs::remove_dir_all(base).unwrap(); - } - - #[test] - fn trusted_local_render_keeps_full_size_behavior() { - let mut h = Headless::trusted_local(); - h.handle("doc.new", json!({"width": 2049, "height": 1})).unwrap(); - let rendered = h.handle("doc.render", json!({"maxSide": 0})).unwrap(); - let png = base64::engine::general_purpose::STANDARD.decode(rendered["base64"].as_str().unwrap()).unwrap(); - assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (2049, 1)); - } - - #[test] - fn methods_lists_the_job_methods_and_every_listed_name_is_served() { - let mut h = Headless::new(); - let listed = h.handle("methods", Value::Null).unwrap(); - let listed: Vec<&str> = listed.as_array().unwrap().iter().map(|m| m.as_str().unwrap()).collect(); - assert_eq!(listed, METHODS); - // The job methods `engine.execute {"wait": false}` depends on are discoverable (#414). - assert!(listed.contains(&"jobs.list") && listed.contains(&"jobs.cancel")); - for m in METHODS { - if let Err(e) = h.handle(m, Value::Null) { - assert!(!e.to_string().contains("unknown method"), "{m} is listed but not served: {e}"); - } - } - assert!(h.handle("jobs.nope", Value::Null).unwrap_err().to_string().contains("unknown method")); - } - - #[test] - fn batch_response_budget_stops_later_steps_even_when_stop_on_error_is_false() { - let mut h = Headless::new(); - let mut steps = vec![json!({"command": "command.list"}); MAX_BATCH_STEPS - 1]; - steps.push(json!({"command": "file.new", "params": {"width": 8, "height": 8}})); - let result = h.batch(&json!({"steps": steps, "stopOnError": false})).unwrap(); - assert_eq!(result["failed"], 1); - assert!(result["results"].as_array().unwrap().last().unwrap()["error"].as_str().unwrap().contains("batch response budget exceeded")); - assert!(h.session.documents().is_empty(), "later edit must not execute after the quota is exhausted"); - assert!(crate::budgets::json_bytes(&result).is_ok()); - assert!(h.handle("methods", Value::Null).is_ok()); - } - - #[test] - fn tcp_serves_loopback() { - use std::io::{BufReader, Write as _}; - const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; - let h = Arc::new(Mutex::new(Headless::new())); - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); - }); - let addr = rx.recv().unwrap(); - let mut s = std::net::TcpStream::connect(addr).unwrap(); - writeln!(s, r#"{{"id":"auth","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); - let mut reader = BufReader::new(s.try_clone().unwrap()); - let mut line = String::new(); - reader.read_line(&mut line).unwrap(); - let auth: Value = serde_json::from_str(&line).unwrap(); - assert_eq!(auth["result"]["authenticated"], true); - writeln!(s, r#"{{"id":"a","method":"methods"}}"#).unwrap(); - line.clear(); - reader.read_line(&mut line).unwrap(); - let v: Value = serde_json::from_str(&line).unwrap(); - assert_eq!(v["id"], "a"); - assert!(v["result"].as_array().unwrap().iter().any(|m| m == "batch")); - } - - /// #4965: a connection that keeps sending without authenticating is closed at the deadline, - /// and one that authenticated in time keeps being served after it. - #[test] - fn tcp_connections_must_authenticate_before_the_deadline_whatever_they_send() { - use std::io::{BufReader, Write as _}; - use std::time::{Duration, Instant}; - const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; - let listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = listener.local_addr().unwrap(); - let server = std::thread::spawn(move || { - let h = Mutex::new(Headless::new()); - let mut results = Vec::new(); - for _ in 0..2 { - let (stream, _) = listener.accept().unwrap(); - let started = Instant::now(); - let r = serve_tcp_connection(&h, stream, TOKEN, Duration::from_millis(300)); - results.push((r.map_err(|e| e.kind()), started.elapsed())); - } - results - }); - - // Never a newline, a byte every 20 ms: each read succeeds long before the idle timeout. - let mut slow = std::net::TcpStream::connect(addr).unwrap(); - for _ in 0..150 { - if slow.write_all(b" ").is_err() { - break; - } - std::thread::sleep(Duration::from_millis(20)); - } - drop(slow); - - // Authenticates at once, then asks again 300 ms after the deadline. - let mut s = std::net::TcpStream::connect(addr).unwrap(); - s.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); - writeln!(s, r#"{{"id":"auth","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); - let mut reader = BufReader::new(s.try_clone().unwrap()); - let mut line = String::new(); - reader.read_line(&mut line).unwrap(); - assert_eq!(serde_json::from_str::(&line).unwrap()["result"]["authenticated"], true); - std::thread::sleep(Duration::from_millis(600)); - writeln!(s, r#"{{"id":"a","method":"methods"}}"#).unwrap(); - line.clear(); - reader.read_line(&mut line).unwrap(); - assert_eq!(serde_json::from_str::(&line).unwrap()["id"], "a"); - drop(reader); - drop(s); - - let results = server.join().unwrap(); - let (slow_result, slow_time) = &results[0]; - assert!(matches!(slow_result, Err(std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock)), "{slow_result:?}"); - assert!(*slow_time < Duration::from_secs(3), "the trickle did not extend the deadline: {slow_time:?}"); - assert_eq!(results[1].0, Ok(()), "the authenticated connection ended with its client"); - } - - #[test] - fn tcp_repeat_auth_reports_already_authenticated_and_keeps_serving() { - use std::io::{BufReader, Write as _}; - const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; - let h = Arc::new(Mutex::new(Headless::new())); - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); - }); - let addr = rx.recv().unwrap(); - let mut s = std::net::TcpStream::connect(addr).unwrap(); - let mut reader = BufReader::new(s.try_clone().unwrap()); - let mut line = String::new(); - for id in ["auth", "again"] { - writeln!(s, r#"{{"id":"{id}","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); - line.clear(); - reader.read_line(&mut line).unwrap(); - } - let again: Value = serde_json::from_str(&line).unwrap(); - assert_eq!(again["id"], "again"); - assert_eq!(again["ok"], false); - assert_eq!(again["error"], "already authenticated"); - writeln!(s, r#"{{"id":"m","method":"methods"}}"#).unwrap(); - line.clear(); - reader.read_line(&mut line).unwrap(); - let v: Value = serde_json::from_str(&line).unwrap(); - assert_eq!(v["id"], "m"); - assert_eq!(v["ok"], true); - } - - #[test] - fn tcp_rejects_requests_before_authentication() { - use std::io::{BufReader, Write as _}; - const TOKEN: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - let h = Arc::new(Mutex::new(Headless::new())); - let (tx, rx) = std::sync::mpsc::channel(); - std::thread::spawn(move || { - let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); - }); - let addr = rx.recv().unwrap(); - let mut s = std::net::TcpStream::connect(addr).unwrap(); - writeln!(s, r#"{{"id":1,"method":"methods"}}"#).unwrap(); - let mut line = String::new(); - BufReader::new(s).read_line(&mut line).unwrap(); - let v: Value = serde_json::from_str(&line).unwrap(); - assert_eq!(v["ok"], false); - assert_eq!(v["error"], "authentication required"); - assert!(v.get("result").is_none()); - } - - #[test] - fn invalid_document_index_is_rejected_not_the_active_document() { - let h = session(); - for _ in 0..2 { - assert_eq!(respond(&h, r#"{"method":"doc.new","params":{"width":8,"height":8}}"#)["ok"], true); - } - let count = |h: &Mutex| h.lock().unwrap().session_list()["documents"].as_array().unwrap().len(); - for index in [json!("0"), json!(-1), json!(0.5), json!(true)] { - for method in ["doc.close", "doc.inspect", "doc.select"] { - let reply = respond(&h, &json!({"method": method, "params": {"index": index}}).to_string()); - assert_eq!(reply["ok"], false, "{method} {index}: {reply}"); - assert!(reply["error"].as_str().unwrap().contains("`index` must be a non-negative integer"), "{reply}"); - } - assert_eq!(count(&h), 2, "index {index} closed a document"); - } - // Null means "the active document", like a missing `index`. - assert_eq!(respond(&h, r#"{"method":"doc.inspect","params":{"index":null}}"#)["ok"], true); - // A valid index still closes that document. - let reply = respond(&h, r#"{"method":"doc.close","params":{"index":0}}"#); - assert_eq!(reply["ok"], true, "{reply}"); - assert_eq!(count(&h), 1); - } -} +mod tests; diff --git a/crates/automation/src/rpc/tests.rs b/crates/automation/src/rpc/tests.rs new file mode 100644 index 0000000000..131df72413 --- /dev/null +++ b/crates/automation/src/rpc/tests.rs @@ -0,0 +1,1030 @@ +use super::*; + +fn session() -> Mutex { + Mutex::new(Headless::trusted_local()) +} + +#[test] +fn document_activation_reply_survives_engine_rpc() { + let h = session(); + let mut g = h.lock().unwrap(); + for _ in 0..2 { + g.handle("engine.execute", json!({"command": "file.new", "params": {"width": 4, "height": 4}})).unwrap(); + } + let reply = g.handle("engine.execute", json!({"command": "document.activate", "params": {"offset": -1}})).unwrap(); + assert_eq!(reply, json!({"document": 0})); + let reply = g.handle("engine.execute", json!({"command": "document.activate", "params": {"document": 1}})).unwrap(); + assert_eq!(reply, json!({"document": 1})); +} + +#[test] +fn execute_reports_invalid_filter_parameters_without_changing_history() { + let h = session(); + assert_eq!(respond(&h, r#"{"method":"doc.new","params":{"width":8,"height":8}}"#)["ok"], true); + let before = h.lock().unwrap().inspect(None).unwrap(); + for (params, key) in + [(json!({"radius":"big"}), "radius"), (json!({"radus":3}), "radus"), (json!({"radius":5000}), "radius"), (json!({"radius":-4}), "radius")] + { + for wait in [true, false] { + let request = json!({"method":"engine.execute","params":{"command":"filter.blur.gaussianBlur","params":params,"wait":wait}}); + let reply = respond(&h, &request.to_string()); + assert_eq!(reply["ok"], false); + let error = reply["error"].as_str().unwrap(); + assert!(error.contains("filter.blur.gaussianBlur") && error.contains(key), "{error}"); + assert_eq!(h.lock().unwrap().inspect(None).unwrap(), before); + } + } +} + +/// Pins the reply shape documented in docs/control-protocol.md › Command list replies +/// (#3684): a bare JSON array (not `{"commands": [...]}`), one object per command with +/// exactly these keys (plus an optional `keywords` string), from `engine.commands` (filtered or not) and `command.list`. +#[test] +fn command_list_replies_are_documented_array_of_objects() { + let h = session(); + let mut g = h.lock().unwrap(); + let all = g.handle("engine.commands", json!({})).unwrap(); + let filtered = g.handle("engine.commands", json!({"filter": "BLUR"})).unwrap(); + let engine = g.handle("engine.execute", json!({"command": "command.list"})).unwrap(); + for (name, reply) in [("engine.commands", &all), ("engine.commands {filter}", &filtered), ("command.list", &engine)] { + let items = reply.as_array().unwrap_or_else(|| panic!("{name} must reply with a bare array, got {reply}")); + assert!(!items.is_empty(), "{name}"); + for c in items { + // `keywords` is optional: only commands with other search names carry it (#5060). + let mut keys: Vec<&str> = c.as_object().unwrap().keys().map(String::as_str).filter(|k| *k != "keywords").collect(); + keys.sort_unstable(); + assert!(c.get("keywords").is_none_or(Value::is_string), "{name}: {c}"); + assert_eq!(keys, ["disabledReason", "enabled", "id", "label", "menu", "needsDocument", "params", "shortcut"], "{name}: {c}"); + assert!(c["id"].is_string() && c["label"].is_string() && c["params"].is_string() && c["enabled"].is_boolean(), "{c}"); + assert!(c["needsDocument"].is_boolean(), "{c}"); + assert_eq!(c["disabledReason"].is_null(), c["enabled"] == true, "disabledReason is null exactly when enabled: {c}"); + assert!(c["menu"].as_array().unwrap().iter().all(Value::is_string), "{c}"); + assert!(c["shortcut"].is_string() || c["shortcut"].is_null(), "{c}"); + } + } + let n_all = all.as_array().unwrap().len(); + assert_eq!(engine.as_array().unwrap().len(), n_all); + let filtered = filtered.as_array().unwrap(); + assert!(filtered.len() < n_all); + assert!(filtered.iter().all(|c| format!("{} {}", c["id"], c["label"]).to_lowercase().contains("blur"))); + let new = all.as_array().unwrap().iter().find(|c| c["id"] == "file.new").unwrap(); + assert_eq!((&new["menu"], &new["shortcut"], &new["enabled"]), (&json!(["File"]), &json!("Cmd+N"), &json!(true)), "{new}"); + assert_eq!((&new["disabledReason"], &new["needsDocument"]), (&Value::Null, &json!(false)), "{new}"); +} + +#[test] +fn lines_round_trip_and_errors() { + let h = session(); + let input = concat!( + r#"{"id":1,"method":"doc.new","params":{"width":64,"height":32,"background":"white"}}"#, + "\n", + "\n", + r#"{"id":2,"method":"engine.execute","params":{"command":"layer.new.layer","params":{"name":"Ink"}}}"#, + "\n", + r#"{"id":3,"method":"doc.inspect"}"#, + "\n", + r#"{"id":4,"method":"nope"}"#, + "\n", + "not json\n", + ); + let mut out = Vec::new(); + serve_lines(&h, input.as_bytes(), &mut out).unwrap(); + let replies: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(replies.len(), 5); + assert!(replies[0]["ok"].as_bool().unwrap()); + assert_eq!(replies[1]["id"], 2); + assert!(replies[2]["result"].to_string().contains("Ink")); + assert!(!replies[3]["ok"].as_bool().unwrap()); + assert!(replies[3]["error"].as_str().unwrap().contains("unknown method")); + assert_eq!(replies[4]["id"], Value::Null); +} + +/// Regression: after a panic while the session lock was held, every later request +/// failed with "session lock poisoned"; the server now keeps serving. +#[test] +fn poisoned_session_keeps_serving() { + let h = session(); + let _ = std::thread::scope(|sc| { + sc.spawn(|| { + let _g = h.lock().unwrap(); + panic!("poison the session lock"); + }) + .join() + }); + assert!(h.is_poisoned()); + let r = respond(&h, r#"{"id":1,"method":"doc.new","params":{"width":8,"height":8}}"#); + assert_eq!(r["ok"], true, "{r}"); +} + +#[test] +fn batch_stops_on_error_unless_told_not_to() { + let h = session(); + let mut g = h.lock().unwrap(); + g.handle("doc.new", json!({"width": 16, "height": 16})).unwrap(); + let steps = json!([ + {"command": "layer.new.layer", "params": {"name": "A"}}, + {"command": "no.such.command"}, + {"command": "layer.new.layer", "params": {"name": "B"}}, + {"method": "doc.inspect"} + ]); + let r = g.handle("batch", json!({"steps": steps})).unwrap(); + assert_eq!(r["completed"], 1); + assert_eq!(r["failed"], 1); + assert_eq!(r["results"].as_array().unwrap().len(), 2); + let r = g.handle("batch", json!({"steps": steps, "stopOnError": false})).unwrap(); + assert_eq!(r["completed"], 3); + let insp = &r["results"][3]["result"]; + assert!(insp.to_string().contains("\"B\"")); +} + +#[test] +fn batch_rejects_too_many_steps() { + let h = session(); + let mut g = h.lock().unwrap(); + let steps = vec![json!({"command": "command.list"}); MAX_BATCH_STEPS + 1]; + let error = g.handle("batch", json!({"steps": steps})).unwrap_err(); + assert!(error.to_string().contains("maximum is 256")); +} + +#[test] +fn render_to_file_and_base64() { + let h = session(); + let mut g = h.lock().unwrap(); + g.handle("doc.new", json!({"width": 40, "height": 20, "background": "black"})).unwrap(); + let b = g.handle("doc.render", json!({"maxSide": 10})).unwrap(); + let png = base64::engine::general_purpose::STANDARD.decode(b["base64"].as_str().unwrap()).unwrap(); + let img = photocraft_codecs::decode(&png).unwrap(); + assert_eq!(img.dimensions(), (10, 5)); + let path = std::env::temp_dir().join(format!("pc-rpc-{}.png", std::process::id())); + let r = g.handle("doc.render", json!({"path": path.to_string_lossy(), "maxSide": 0})).unwrap(); + assert!(r["bytes"].as_u64().unwrap() > 0); + assert_eq!(photocraft_codecs::decode(&std::fs::read(&path).unwrap()).unwrap().dimensions(), (40, 20)); + let _ = std::fs::remove_file(path); +} + +#[test] +fn background_jobs_list_wait_and_cancel() { + let mut h = Headless::new(); + h.handle("doc.new", json!({"width": 600, "height": 400})).unwrap(); + h.handle("engine.execute", json!({"command": "layer.new.layer"})).unwrap(); + h.handle("engine.execute", json!({"command": "edit.fill", "params": {"color": "#808080"}})).unwrap(); + // Default: waits, the result is the command's. + let r = h.handle("engine.execute", json!({"command": "filter.noise.addNoise", "params": {"amount": 20}})).unwrap(); + assert!(r["filter"].is_object(), "{r}"); + // wait: false returns a job id at once. + let r = h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false})).unwrap(); + let job = r["job"].as_u64().unwrap(); + let listed = h.handle("jobs.list", json!({})).unwrap(); + assert!(listed["jobs"].as_array().unwrap().iter().any(|j| j["id"] == job), "{listed}"); + // Wait for it via jobs.list (which applies finished jobs). + let t = std::time::Instant::now(); + loop { + let l = h.handle("jobs.list", json!({})).unwrap(); + let state = l["jobs"].as_array().unwrap().iter().find(|j| j["id"] == job).map(|j| j["state"].clone()); + if state == Some(json!("done")) { + break; + } + assert!(t.elapsed().as_secs() < 60, "{l}"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + // Cancel: a second job, cancelled at once, leaves the document unchanged. + let before = h.session.active().unwrap().doc.clone(); + let r = h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false})).unwrap(); + let job = r["job"].as_u64().unwrap(); + h.handle("jobs.cancel", json!({"job": job})).unwrap(); + assert!(h.handle("jobs.cancel", json!({"job": "nope"})).is_err()); + let l = h.handle("jobs.list", json!({})).unwrap(); + assert_eq!(l["jobs"].as_array().unwrap().iter().find(|j| j["id"] == job).unwrap()["state"], "cancelled"); + assert!(std::sync::Arc::ptr_eq(&h.session.active().unwrap().doc, &before)); +} + +#[test] +fn batch_steps_can_start_background_jobs() { + let mut h = Headless::new(); + h.handle("doc.new", json!({"width": 600, "height": 400})).unwrap(); + let steps = json!([ + {"command": "layer.new.layer"}, + {"command": "edit.fill", "params": {"color": "#808080"}}, + {"command": "filter.blur.gaussianBlur", "params": {"radius": 40}, "wait": false} + ]); + let r = h.handle("batch", json!({"steps": steps})).unwrap(); + assert_eq!(r["completed"], 3, "{r}"); + let job = r["results"][2]["result"]["job"].as_u64().unwrap_or_else(|| panic!("no job id: {r}")); + assert_eq!(r["results"][2]["result"]["pending"], true); + let listed = h.handle("jobs.list", json!({})).unwrap(); + assert!(listed["jobs"].as_array().unwrap().iter().any(|j| j["id"] == job), "{listed}"); + let t = std::time::Instant::now(); + while h.session.jobs().iter().any(|j| j.id.0 == job) { + assert!(t.elapsed().as_secs() < 60); + std::thread::sleep(std::time::Duration::from_millis(5)); + h.handle("jobs.list", json!({})).unwrap(); + } +} + +/// Regression (#503): a finished `wait: false` job was only applied by a later command or +/// `jobs.list`, so save, inspect, render and `session.list` still saw the document without it. +#[test] +fn every_request_sees_a_finished_background_job() { + let dir = std::env::temp_dir().join(format!("pc-rpc-job-sync-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (before, after) = (dir.join("before.png"), dir.join("after.png")); + let mut h = Headless::trusted_local(); + h.handle("doc.new", json!({"width": 64, "height": 48})).unwrap(); + h.handle("engine.execute", json!({"command": "filter.noise.addNoise", "params": {"amount": 50}})).unwrap(); + h.handle("doc.save", json!({"path": before.to_string_lossy()})).unwrap(); + let render = |h: &mut Headless| h.handle("doc.render", json!({"maxSide": 0})).unwrap()["base64"].clone(); + let unblurred = render(&mut h); + let revision = |h: &mut Headless| h.handle("session.list", json!({})).unwrap()["documents"][0]["revision"].clone(); + let start = revision(&mut h); + h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 4}, "wait": false})).unwrap(); + // Only reading requests from here on: no command and no `jobs.list`. + let t = std::time::Instant::now(); + while revision(&mut h) == start { + assert!(t.elapsed().as_secs() < 60, "session.list never saw the finished job"); + std::thread::sleep(std::time::Duration::from_millis(5)); + } + let history = h.handle("doc.inspect", json!({})).unwrap()["history"].clone(); + assert_eq!(history.as_array().unwrap().last().unwrap(), "Gaussian Blur", "{history}"); + h.handle("doc.save", json!({"path": after.to_string_lossy()})).unwrap(); + let decode = |p: &std::path::Path| photocraft_codecs::decode(&std::fs::read(p).unwrap()).unwrap().to_rgba8(); + assert_ne!(decode(&before), decode(&after), "the export holds the blur"); + assert_ne!(render(&mut h), unblurred, "the preview holds the blur"); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// Regression: `quality` 85.0 or "85" was ignored (a lossless WebP, reply ok) and 0 or 500 was +/// clamped, where the CLI refuses anything but a whole number from 1 to 100. +#[test] +fn doc_save_rejects_a_bad_quality_instead_of_ignoring_or_clamping_it() { + let dir = std::env::temp_dir().join(format!("pc-rpc-save-quality-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("x.webp"); + let mut h = Headless::trusted_local(); + h.handle("doc.new", json!({"width": 16, "height": 16})).unwrap(); + for q in [json!(85.0), json!("85"), json!(0), json!(500), json!(-5), json!(true)] { + let err = h.handle("doc.save", json!({"path": path.to_string_lossy(), "quality": q})).unwrap_err().to_string(); + assert!(err.contains("quality"), "{q}: {err}"); + assert!(!path.exists(), "{q} wrote a file"); + } + h.handle("doc.save", json!({"path": path.to_string_lossy(), "quality": 85})).unwrap(); + let bytes = std::fs::read(&path).unwrap(); + // The lossy chunk; an extended WebP (one carrying a colour profile) puts `VP8X` first. + assert!(bytes.windows(4).any(|c| c == b"VP8 ") && !bytes.windows(4).any(|c| c == b"VP8L"), "a WebP saved with a quality is lossy"); + std::fs::remove_dir_all(dir).unwrap(); +} + +#[test] +fn automation_preview_rejects_oversized_full_size_and_numeric_wraparound() { + let mut h = Headless::new(); + h.handle("doc.new", json!({"width": 2049, "height": 1})).unwrap(); + for max in [json!(0), json!(2049), json!(u64::from(u32::MAX) + 1), json!(-1), json!(1.5)] { + assert!(h.handle("doc.render", json!({"maxSide": max})).is_err()); + } + // A smaller preview still works, and rejection leaves the source document intact. + let rendered = h.handle("doc.render", json!({"maxSide": 32})).unwrap(); + let png = base64::engine::general_purpose::STANDARD.decode(rendered["base64"].as_str().unwrap()).unwrap(); + assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (32, 1)); + assert_eq!(h.session.active().unwrap().doc.size.width, 2049); +} + +#[test] +fn stdio_rejects_oversized_frames_before_dispatch() { + let h = Mutex::new(Headless::new()); + let input = " ".repeat(MAX_REQUEST_BYTES + 1); + let mut out = Vec::new(); + serve_lines(&h, input.as_bytes(), &mut out).unwrap(); + let reply: Value = serde_json::from_slice(&out).unwrap(); + assert_eq!(reply["ok"], false); + assert!(reply["error"].as_str().unwrap().contains("request exceeds")); + assert!(h.lock().unwrap().session.documents().is_empty()); +} + +/// Regression (#505): an over-long or non-UTF-8 line ended the stdio session, losing every +/// open document. Now it gets one error reply, the rest of the line is skipped (never +/// dispatched), and the next request is served. +#[test] +fn stdio_skips_a_rejected_line_and_keeps_the_session() { + let h = Mutex::new(Headless::new()); + let mut input = b"{\"id\":1,\"method\":\"doc.new\",\"params\":{\"width\":8,\"height\":8}}\n".to_vec(); + input.extend(" ".repeat(MAX_REQUEST_BYTES + 1).into_bytes()); + input.extend(b"{\"id\":9,\"method\":\"doc.close\"}\n"); + input.extend(b"\xff\xfe{\"id\":8,\"method\":\"doc.close\"}\n"); + input.extend(b"{\"id\":2,\"method\":\"session.list\"}\n"); + let mut out = Vec::new(); + serve_lines(&h, input.as_slice(), &mut out).unwrap(); + let replies: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(replies.len(), 4, "{replies:?}"); + assert_eq!(replies[0]["ok"], true); + assert!(replies[1]["error"].as_str().unwrap().contains("request exceeds"), "{}", replies[1]); + assert!(replies[2]["error"].as_str().unwrap().contains("not valid UTF-8"), "{}", replies[2]); + assert_eq!(replies[3]["id"], 2); + assert_eq!(replies[3]["result"]["documents"].as_array().unwrap().len(), 1, "the document stays open"); +} + +#[test] +fn preview_rejection_precedes_output_file_creation() { + let root = std::env::temp_dir().join(format!("pc-preview-budget-{}", std::process::id())); + std::fs::create_dir_all(&root).unwrap(); + let workspace = crate::AuthorizedWorkspace::new(None, Some(&root)).unwrap(); + let mut h = Headless::with_workspace(workspace); + h.handle("doc.new", json!({"width": 16, "height": 8})).unwrap(); + let result = h.handle("doc.render", json!({"maxSide": 2049, "path": "rejected.png"})); + assert!(result.unwrap_err().to_string().contains("preview side exceeds")); + assert!(!root.join("rejected.png").exists()); + assert!(h.handle("doc.render", json!({"maxSide": 16, "path": "accepted.png"})).is_ok()); + assert!(root.join("accepted.png").exists()); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_file(root.join("accepted.png")).unwrap(); + std::fs::remove_dir(root).unwrap(); +} + +#[test] +fn doc_place_reads_through_the_read_root_and_never_outside_it() { + let base = std::env::temp_dir().join(format!("pc-doc-place-{}", std::process::id())); + let root = base.join("workspace"); + let outside = base.join("outside"); + for dir in [&root, &outside] { + std::fs::create_dir_all(dir).unwrap(); + } + let img = photocraft_codecs::Image::from_u8(8, 6, photocraft_codecs::ChannelLayout::Rgb, vec![42; 8 * 6 * 3]).unwrap(); + let png = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + std::fs::write(root.join("logo.png"), &png).unwrap(); + std::fs::write(outside.join("logo.png"), &png).unwrap(); + let absolute = outside.join("logo.png").to_string_lossy().into_owned(); + + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); + let placed = h.handle("doc.place", json!({"path": "logo.png", "scale": 50})).unwrap(); + assert!(placed["layer"].as_u64().is_some(), "{placed}"); + assert_eq!(placed["scale"].as_f64(), Some(50.0), "the options reach the command: {placed}"); + let inspected = h.handle("doc.inspect", json!({})).unwrap(); + assert_eq!(inspected["layers"].as_array().map(Vec::len), Some(2), "the placed layer is in the document: {inspected}"); + // A path outside the read root is refused the way the ambient command is (#2810). + for (path, reason) in [("../outside/logo.png", "parent traversal"), (absolute.as_str(), "must be inside the automation root")] { + let refused = h.handle("doc.place", json!({"path": path})).unwrap_err().to_string(); + assert!(refused.contains(reason), "{refused}"); + let inspected = h.handle("doc.inspect", json!({})).unwrap(); + assert_eq!(inspected["layers"].as_array().map(Vec::len), Some(2), "nothing was placed: {refused}"); + } + // Trusted local keeps the ambient command: that caller may read the path itself. + let mut t = Headless::trusted_local(); + t.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); + let trusted = t.handle("doc.place", json!({"path": root.join("logo.png").to_string_lossy()})).unwrap(); + assert!(trusted["layer"].as_u64().is_some(), "{trusted}"); + // Release directory capabilities before cleanup on Windows. + drop(t); + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +/// A save or render never turns a file PhotoCraft doesn't write (source, settings, notes) +/// into image bytes, whatever `format` says; images and new files are written as before (#9439). +#[test] +fn workspace_writes_refuse_to_replace_a_file_that_is_not_an_image() { + let root = std::env::temp_dir().join(format!("pc-no-replace-foreign-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(root.join("src")).unwrap(); + let foreign = [("src/main.rs", "fn main() {}\n"), ("Cargo.toml", "[package]\n"), (".env", "TOKEN=x\n"), ("Makefile", "all:\n")]; + for (name, text) in foreign { + std::fs::write(root.join(name), text).unwrap(); + } + std::fs::write(root.join("old.png"), b"not yet a png").unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + for (name, text) in foreign { + let saved = h.handle("doc.save", json!({"path": name, "format": "png"})).unwrap_err().to_string(); + assert!(saved.contains("already exists"), "doc.save over {name}: {saved}"); + let rendered = h.handle("doc.render", json!({"path": name})).unwrap_err().to_string(); + assert!(rendered.contains("already exists"), "doc.render over {name}: {rendered}"); + assert_eq!(std::fs::read_to_string(root.join(name)).unwrap(), text, "{name} is untouched"); + } + // An existing image is replaced, and a new file of any name is created. + h.handle("doc.save", json!({"path": "old.png"})).unwrap(); + assert!(std::fs::read(root.join("old.png")).unwrap().starts_with(b"\x89PNG")); + h.handle("doc.save", json!({"path": "fresh.txt", "format": "png"})).unwrap(); + assert!(std::fs::read(root.join("fresh.txt")).unwrap().starts_with(b"\x89PNG")); + h.handle("doc.render", json!({"path": "old.png"})).unwrap(); + std::fs::write(root.join("OLD.TIF"), b"x").unwrap(); + h.handle("doc.save", json!({"path": "OLD.TIF"})).unwrap(); + // An image saved under a name without an image extension is recognised by its bytes. + h.handle("doc.save", json!({"path": "out", "format": "png"})).unwrap(); + h.handle("doc.save", json!({"path": "out", "format": "png"})).unwrap(); + h.handle("doc.render", json!({"path": "fresh.txt"})).unwrap(); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(root).unwrap(); +} + +#[test] +fn doc_replace_smart_object_reads_through_the_read_root_and_never_outside_it() { + let base = std::env::temp_dir().join(format!("pc-doc-replace-so-{}", std::process::id())); + let root = base.join("workspace"); + let outside = base.join("outside"); + for dir in [&root, &outside] { + std::fs::create_dir_all(dir).unwrap(); + } + let png = |w: u32, h: u32, v: u8| { + let img = photocraft_codecs::Image::from_u8(w, h, photocraft_codecs::ChannelLayout::Rgb, vec![v; (w * h * 3) as usize]).unwrap(); + photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap() + }; + std::fs::write(root.join("logo.png"), png(8, 6, 42)).unwrap(); + std::fs::write(root.join("red.png"), png(8, 6, 200)).unwrap(); + std::fs::write(outside.join("red.png"), png(8, 6, 200)).unwrap(); + let absolute = outside.join("red.png").to_string_lossy().into_owned(); + + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); + let layer = h.handle("doc.place", json!({"path": "logo.png"})).unwrap()["layer"].clone(); + // The smart object's file name and the composite at its centre: what Replace Contents changes. + let contents = |h: &mut Headless| { + let inspected = h.handle("doc.inspect", json!({})).unwrap(); + let layers = inspected["layers"].as_array().unwrap(); + let so = layers.iter().find(|l| l["id"] == layer).unwrap_or_else(|| panic!("{inspected}")); + let name = so["smartSource"]["fileName"].as_str().unwrap_or_default().to_owned(); + let rendered = h.handle("doc.render", json!({"maxSide": 0})).unwrap(); + let png = base64::engine::general_purpose::STANDARD.decode(rendered["base64"].as_str().unwrap()).unwrap(); + let img = photocraft_codecs::decode(&png).unwrap(); + let (w, ht) = img.dimensions(); + let data = img.as_u8().unwrap(); + let channels = data.len() / (w * ht) as usize; + (name, data[((ht / 2 * w + w / 2) as usize) * channels]) + }; + assert_eq!(contents(&mut h), ("logo.png".to_owned(), 42), "the placed contents"); + let replaced = h.handle("doc.replaceSmartObject", json!({"path": "red.png", "layer": layer})).unwrap(); + assert_eq!(replaced["layer"], layer, "{replaced}"); + assert_eq!(contents(&mut h), ("red.png".to_owned(), 200), "the contents were replaced"); + h.handle("engine.execute", json!({"command": "edit.undo"})).unwrap(); + assert_eq!(contents(&mut h), ("logo.png".to_owned(), 42), "one undo step restores the old contents"); + h.handle("engine.execute", json!({"command": "edit.redo"})).unwrap(); + assert_eq!(contents(&mut h), ("red.png".to_owned(), 200), "redo replaces them again"); + // Outside the read root, a bad layer, a missing path or a file that is no image: refused, nothing changed. + for (params, reason) in [ + (json!({"path": "../outside/red.png"}), "parent traversal"), + (json!({"path": absolute}), "must be inside the automation root"), + (json!({"path": "logo.png", "layer": 999_999}), "no such layer"), + (json!({}), "needs `path`"), + ] { + let refused = h.handle("doc.replaceSmartObject", params).unwrap_err().to_string(); + assert!(refused.contains(reason), "{refused}"); + assert_eq!(contents(&mut h), ("red.png".to_owned(), 200), "nothing changed: {refused}"); + } + std::fs::write(root.join("junk.png"), b"not an image").unwrap(); + assert!(h.handle("doc.replaceSmartObject", json!({"path": "junk.png", "layer": layer})).is_err()); + assert_eq!(contents(&mut h), ("red.png".to_owned(), 200), "a file that is no image changes nothing"); + // The ambient command keeps its refusal. + let ambient = h.handle("engine.execute", json!({"command": "layer.smartObjects.replaceContents", "params": {"path": "red.png"}})); + assert!(ambient.unwrap_err().to_string().contains("ambient filesystem paths")); + // Trusted local keeps the ambient command. + let mut t = Headless::trusted_local(); + t.handle("doc.new", json!({"width": 40, "height": 30})).unwrap(); + let tl = t.handle("doc.place", json!({"path": root.join("logo.png").to_string_lossy()})).unwrap()["layer"].clone(); + let trusted = t.handle("doc.replaceSmartObject", json!({"path": root.join("red.png").to_string_lossy(), "layer": tl})).unwrap(); + assert_eq!(trusted["layer"], tl, "{trusted}"); + drop(t); + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn doc_export_layers_reports_skips_in_workspace_and_trusted_sessions() { + for trusted in [false, true] { + let base = std::env::temp_dir().join(format!("pc-export-skips-{}-{trusted}", std::process::id())); + let _ = std::fs::remove_dir_all(&base); + std::fs::create_dir_all(&base).unwrap(); + let mut h = + if trusted { Headless::trusted_local() } else { Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&base), Some(&base)).unwrap()) }; + h.handle("doc.new", json!({"width": 4, "height": 4, "background": "white", "name": "card"})).unwrap(); + let adjustment = h.handle("engine.execute", json!({"command": "layer.newAdjustmentLayer.levels"})).unwrap()["layer"].clone(); + h.handle("engine.execute", json!({"command": "layer.setProps", "params": {"name": "Levels"}})).unwrap(); + let hidden = h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {"name": "Hidden"}})).unwrap()["layer"].clone(); + h.handle("engine.execute", json!({"command": "layer.setProps", "params": {"visible": false}})).unwrap(); + // Exercise the actual JSON-lines serve envelope, not only direct dispatch. + let h = Mutex::new(h); + let out_dir = base.join("out").to_string_lossy().into_owned(); + let request = json!({"id": 8491, "method": "doc.exportLayers", "params": {"dir": out_dir}}).to_string() + "\n"; + let mut output = Vec::new(); + serve_lines(&h, request.as_bytes(), &mut output).unwrap(); + let reply: Value = serde_json::from_slice(&output).unwrap(); + assert_eq!(reply["id"], 8491); + assert_eq!(reply["ok"], true, "{reply}"); + assert_eq!(reply["result"]["files"], json!([format!("{out_dir}/card_0001_Background.png")])); + if !trusted { + assert_eq!(reply["result"]["count"], 1); + } + assert_eq!( + reply["result"]["skipped"], + json!([ + {"layer": hidden, "name": "Hidden", "reason": "hidden by visibleOnly"}, + {"layer": adjustment, "name": "Levels", "reason": "adjustment layer has no pixels of its own"} + ]) + ); + assert_eq!(std::fs::read_dir(base.join("out")).unwrap().count(), 1); + let mut h = h.into_inner().unwrap(); + let all_dir = base.join("all").to_string_lossy().into_owned(); + let all = h.export_layers(None, &all_dir, &json!({"visibleOnly": false})).unwrap(); + assert_eq!(all["files"].as_array().unwrap().len(), 2); + assert_eq!(all["skipped"].as_array().unwrap().len(), 1); + // Workspace no-overwrite errors still fail and remove only newly created files. + if !trusted { + let collision = base.join("collision"); + std::fs::create_dir_all(&collision).unwrap(); + std::fs::write(collision.join("card_0002_Background.png"), b"keep").unwrap(); + assert!(h.export_layers(None, &collision.to_string_lossy(), &json!({"visibleOnly": false})).is_err()); + assert_eq!(std::fs::read_dir(&collision).unwrap().count(), 1); + assert_eq!(std::fs::read(collision.join("card_0002_Background.png")).unwrap(), b"keep"); + } + drop(h); + std::fs::remove_dir_all(base).unwrap(); + } +} + +#[test] +fn doc_export_layers_writes_only_beneath_the_write_root() { + let base = std::env::temp_dir().join(format!("pc-doc-export-layers-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 12, "height": 8, "background": "white", "name": "card"})).unwrap(); + h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {"name": "Badge"}})).unwrap(); + + let out = root.join("out"); + let exported = h.handle("doc.exportLayers", json!({"dir": out.to_string_lossy(), "format": "png", "prefix": "card"})).unwrap(); + assert_eq!(exported["count"].as_u64(), Some(2), "{exported}"); + let mut names: Vec = std::fs::read_dir(&out).unwrap().map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned()).collect(); + names.sort(); + assert_eq!(names.len(), 2, "{exported}"); + for name in names { + assert!(name.starts_with("card_") && name.ends_with(".png"), "{name}"); + let bytes = std::fs::read(out.join(&name)).unwrap(); + assert_eq!(photocraft_codecs::decode(&bytes).unwrap().dimensions(), (12, 8), "{name}"); + } + // Outside the write root nothing is created, not even the directory. + let escaped = base.join("escaped"); + let refused = h.handle("doc.exportLayers", json!({"dir": escaped.to_string_lossy()})).unwrap_err().to_string(); + assert!(refused.contains("must be inside the automation root"), "{refused}"); + assert!(!escaped.exists()); + // The ambient command keeps its refusal for the very same directory. + let ambient = + h.handle("engine.execute", json!({"command": "file.export.layersToFiles", "params": {"dir": out.to_string_lossy()}})).unwrap_err().to_string(); + assert!(ambient.contains("ambient filesystem paths"), "{ambient}"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn doc_export_data_sets_refuse_collisions_and_import_reads_the_root() { + let base = std::env::temp_dir().join(format!("pc-doc-data-sets-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "card"})).unwrap(); + let inspected = h.handle("doc.inspect", json!({})).unwrap(); + let layer = inspected["layers"][0]["id"].as_u64().expect("the layer id"); + h.handle("engine.execute", json!({"command": "image.variables.define", "params": {"defs": [{"name": "show", "layer": layer, "type": "visibility"}]}})) + .unwrap(); + h.handle( + "engine.execute", + json!({"command": "image.variables.dataSets", "params": {"dataSets": [ + {"name": "one", "values": [{"variable": "show", "kind": "visibility", "value": true}]}, + {"name": "two", "values": [{"variable": "show", "kind": "visibility", "value": false}]}, + ]}}), + ) + .unwrap(); + + let sets = root.join("sets"); + let exported = h.handle("doc.exportDataSets", json!({"dir": sets.to_string_lossy(), "format": "png"})).unwrap(); + assert_eq!(exported["count"].as_u64(), Some(2), "{exported}"); + assert_eq!(std::fs::read_dir(&sets).unwrap().count(), 2, "{exported}"); + // Colliding names are refused as in the command, before any file is written. + let clash = root.join("clash"); + let refused = h.handle("doc.exportDataSets", json!({"dir": clash.to_string_lossy(), "naming": "{document}"})).unwrap_err().to_string(); + assert!(refused.contains("{index}"), "{refused}"); + assert_eq!(std::fs::read_dir(&clash).unwrap().count(), 0, "no file of the refused batch"); + // A CSV from the read root, and one outside it. + std::fs::write(root.join("sets.csv"), "Data Set,show\none,true\ntwo,false\n").unwrap(); + let imported = h.handle("doc.importDataSets", json!({"path": "sets.csv"})).unwrap(); + assert_eq!(imported["imported"].as_u64(), Some(2), "{imported}"); + std::fs::write(base.join("outside.csv"), "Data Set,show\none,true\n").unwrap(); + let outside = h.handle("doc.importDataSets", json!({"path": base.join("outside.csv").to_string_lossy()})).unwrap_err().to_string(); + assert!(outside.contains("must be inside the automation root"), "{outside}"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn indexed_place_replace_and_data_set_methods_keep_the_active_document() { + let base = std::env::temp_dir().join(format!("pc-doc-index-active-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let img = photocraft_codecs::Image::from_u8(4, 4, photocraft_codecs::ChannelLayout::Rgb, vec![42; 4 * 4 * 3]).unwrap(); + std::fs::write(root.join("logo.png"), photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); + std::fs::write(root.join("sets.csv"), "Data Set,show\none,true\ntwo,false\n").unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "first"})).unwrap(); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "second"})).unwrap(); + let layer = h.handle("doc.inspect", json!({})).unwrap()["layers"][0]["id"].as_u64().expect("the layer id"); + h.handle("engine.execute", json!({"command": "image.variables.define", "params": {"defs": [{"name": "show", "layer": layer, "type": "visibility"}]}})) + .unwrap(); + h.handle("doc.select", json!({"index": 0})).unwrap(); + + let steps = [ + ("doc.place", json!({"path": "logo.png", "index": 1}), true), + // Document 1's active layer is now the placed Smart Object. + ("doc.replaceSmartObject", json!({"path": "logo.png", "index": 1}), true), + ("doc.replaceSmartObject", json!({"path": "missing.png", "index": 1}), false), + ("doc.importDataSets", json!({"path": "sets.csv", "index": 1}), true), + ("doc.exportLayers", json!({"dir": "layers", "index": 1}), true), + ("doc.exportDataSets", json!({"dir": "sets", "index": 1}), true), + // A failing call restores the active document too. + ("doc.importDataSets", json!({"path": "missing.csv", "index": 1}), false), + ]; + for (method, params, succeeds) in steps { + let result = h.handle(method, params.clone()); + assert_eq!(result.is_ok(), succeeds, "{method} {params}: {result:?}"); + assert_eq!(h.session.active_index(), Some(0), "{method} {params} must not select document 1"); + } + assert_eq!(h.session.documents()[0].doc.layer_count(), 1, "nothing was placed into the active document"); + assert_eq!(h.session.documents()[1].doc.layer_count(), 2, "the placed layer is in document 1"); + assert_eq!(std::fs::read_dir(root.join("sets")).unwrap().count(), 2, "document 1's data sets were exported"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn doc_export_data_sets_judges_pixel_replacement_paths_like_apply() { + let base = std::env::temp_dir().join(format!("pc-doc-data-set-pixels-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let red = photocraft_codecs::Image::from_u8(4, 4, photocraft_codecs::ChannelLayout::Rgb, [255, 0, 0].repeat(16)).unwrap(); + let photo = base.join("photo.png"); + std::fs::write(&photo, photocraft_codecs::encode(&red, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.new", json!({"width": 8, "height": 8, "background": "white", "name": "card"})).unwrap(); + let layer = h.handle("engine.execute", json!({"command": "layer.new.layer", "params": {}})).unwrap()["layer"].clone(); + h.handle( + "engine.execute", + json!({"command": "image.variables.define", "params": {"defs": [{"name": "photo", "layer": layer, "type": "pixelReplacement"}]}}), + ) + .unwrap(); + // Storing the path is allowed; reading it is what the gate judges. + h.handle( + "engine.execute", + json!({"command": "image.variables.dataSets", "params": {"dataSets": [ + {"name": "swap", "values": [{"variable": "photo", "kind": "pixels", "value": photo.to_string_lossy()}]}, + ]}}), + ) + .unwrap(); + let applied = h.handle("engine.execute", json!({"command": "image.applyDataSet", "params": {"name": "swap"}})).unwrap_err().to_string(); + assert!(applied.contains("ambient filesystem paths"), "{applied}"); + let sets = root.join("sets"); + let exported = h.handle("doc.exportDataSets", json!({"dir": sets.to_string_lossy(), "format": "png"})).unwrap_err().to_string(); + assert!(exported.contains("ambient filesystem paths"), "{exported}"); + assert_eq!(std::fs::read_dir(&sets).map_or(0, Iterator::count), 0, "nothing of the refused batch is written"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn workspace_psd_saves_embed_no_file_a_linked_smart_object_names() { + use photocraft_engine::doc::{LayerContent, SmartSource}; + + // An image PhotoCraft can open (other linked files are never embedded): an embedded copy + // carries these exact bytes, while pixels written for the layer don't. + let image = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, (0..192u8).collect()).unwrap(); + let marker = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + let has_marker = |bytes: &[u8]| bytes.windows(marker.len()).any(|w| w == marker.as_slice()); + let base = std::env::temp_dir().join(format!("pc-psd-linked-embed-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let outside = base.join("outside.png"); + std::fs::write(&outside, &marker).unwrap(); + // A document whose smart object names a file outside the roots as its source. + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + session.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let state = session.active_mut().unwrap(); + let LayerContent::Smart(smart) = &mut std::sync::Arc::make_mut(&mut state.doc).layers[0].content else { panic!("smart object") }; + smart.source = SmartSource::Linked { path: outside.to_string_lossy().into_owned() }; + std::fs::write(root.join("in.pcraft"), photocraft_engine::smart_cmds::encode_source(&state.doc).unwrap()).unwrap(); + // A trusted export embeds the linked file, as before. + assert!(has_marker(&photocraft_io::export(&state.doc, "psd", &Default::default()).unwrap().bytes)); + + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.open", json!({"path": "in.pcraft"})).unwrap(); + let saved = h.handle("doc.save", json!({"path": "out.psd"})).unwrap(); + let warnings = saved["warnings"].to_string(); + assert!(warnings.contains("written as pixels"), "{warnings}"); + assert!(!has_marker(&std::fs::read(root.join("out.psd")).unwrap()), "the linked file outside the roots is not embedded"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn allowed_filters_do_not_rerender_a_smart_object_from_its_linked_file() { + use photocraft_engine::doc::{LayerContent, SmartSource}; + + let base = std::env::temp_dir().join(format!("pc-linked-rerender-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let red = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, [255, 0, 0].repeat(64)).unwrap(); + let outside = base.join("red.png"); + std::fs::write(&outside, photocraft_codecs::encode(&red, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); + // A document whose white smart object names a file outside the roots as its source. + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + session.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let state = session.active_mut().unwrap(); + let LayerContent::Smart(smart) = &mut std::sync::Arc::make_mut(&mut state.doc).layers[0].content else { panic!("smart object") }; + smart.source = SmartSource::Linked { path: outside.to_string_lossy().into_owned() }; + std::fs::write(root.join("p.pcraft"), photocraft_engine::smart_cmds::encode_source(&state.doc).unwrap()).unwrap(); + + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.open", json!({"path": "p.pcraft"})).unwrap(); + h.handle("engine.execute", json!({"command": "filter.blur.gaussianBlur", "params": {"radius": 0.5}})).unwrap(); + let doc = &h.session.active().unwrap().doc; + let LayerContent::Smart(smart) = &doc.layers[0].content else { panic!("smart object") }; + assert_eq!(smart.smart_filters.len(), 1, "the filter is still recorded"); + assert_eq!(smart.cache.as_ref().unwrap().rgba(4, 4), [1.0, 1.0, 1.0, 1.0], "the linked file outside the roots is not read"); + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn workspace_layer_exports_keep_linked_smart_objects_as_pixels() { + use photocraft_engine::doc::{LayerContent, SmartSource}; + + const MARKER: &[u8] = b"PHOTOCRAFT-LINKED-0002"; + let has_marker = |bytes: &[u8]| bytes.windows(MARKER.len()).any(|w| w == MARKER); + let base = std::env::temp_dir().join(format!("pc-export-layers-linked-{}", std::process::id())); + let root = base.join("workspace"); + std::fs::create_dir_all(&root).unwrap(); + let outside = base.join("outside.txt"); + std::fs::write(&outside, MARKER).unwrap(); + // A document whose smart object names a file outside the roots as its source. + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + session.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let state = session.active_mut().unwrap(); + let LayerContent::Smart(smart) = &mut std::sync::Arc::make_mut(&mut state.doc).layers[0].content else { panic!("smart object") }; + smart.source = SmartSource::Linked { path: outside.to_string_lossy().into_owned() }; + std::fs::write(root.join("in.pcraft"), photocraft_engine::smart_cmds::encode_source(&state.doc).unwrap()).unwrap(); + + let mut h = Headless::with_workspace(crate::AuthorizedWorkspace::new(Some(&root), Some(&root)).unwrap()); + h.handle("doc.open", json!({"path": "in.pcraft"})).unwrap(); + let out = root.join("layers"); + for format in ["psd", "psb"] { + let exported = h.handle("doc.exportLayers", json!({"dir": out.to_string_lossy(), "format": format})).unwrap(); + assert_eq!(exported["count"], 1, "{exported}"); + } + let files: Vec<_> = std::fs::read_dir(&out).unwrap().map(|e| e.unwrap().path()).collect(); + assert_eq!(files.len(), 2, "{files:?}"); + for file in files { + assert!(!has_marker(&std::fs::read(&file).unwrap()), "{} embeds no file outside the roots", file.display()); + } + // Release directory capabilities before cleanup on Windows. + drop(h); + std::fs::remove_dir_all(base).unwrap(); +} + +#[test] +fn trusted_local_render_keeps_full_size_behavior() { + let mut h = Headless::trusted_local(); + h.handle("doc.new", json!({"width": 2049, "height": 1})).unwrap(); + let rendered = h.handle("doc.render", json!({"maxSide": 0})).unwrap(); + let png = base64::engine::general_purpose::STANDARD.decode(rendered["base64"].as_str().unwrap()).unwrap(); + assert_eq!(photocraft_codecs::decode(&png).unwrap().dimensions(), (2049, 1)); +} + +#[test] +fn methods_lists_the_job_methods_and_every_listed_name_is_served() { + let mut h = Headless::new(); + let listed = h.handle("methods", Value::Null).unwrap(); + let listed: Vec<&str> = listed.as_array().unwrap().iter().map(|m| m.as_str().unwrap()).collect(); + assert_eq!(listed, METHODS); + // The job methods `engine.execute {"wait": false}` depends on are discoverable (#414). + assert!(listed.contains(&"jobs.list") && listed.contains(&"jobs.cancel")); + for m in METHODS { + if let Err(e) = h.handle(m, Value::Null) { + assert!(!e.to_string().contains("unknown method"), "{m} is listed but not served: {e}"); + } + } + assert!(h.handle("jobs.nope", Value::Null).unwrap_err().to_string().contains("unknown method")); +} + +/// #3674: a guessed name gets a pointer to the right one, in both directions. +#[test] +fn unknown_names_suggest_the_right_method_or_command() { + let mut h = Headless::new(); + let err = |h: &mut Headless, m: &str, p: Value| h.handle(m, p).unwrap_err().to_string(); + let e = err(&mut h, "engine.execute", json!({"command": "engine.commands"})); + assert!(e.contains("did you mean `command.list`") && e.contains("protocol method"), "{e}"); + let e = err(&mut h, "engine.execute", json!({"command": "doc.inspect"})); + assert!(e.contains("`doc.inspect` is a protocol method"), "{e}"); + let e = err(&mut h, "engine.execute", json!({"command": "edit.undoo"})); + assert!(e.contains("did you mean `edit.undo`"), "{e}"); + let e = err(&mut h, "doc.inspct", Value::Null); + assert!(e.contains("did you mean `doc.inspect`") && e.contains("(try `methods`)"), "{e}"); + let e = err(&mut h, "edit.undo", Value::Null); + assert!(e.contains("engine.execute {\"command\": \"edit.undo\"}"), "{e}"); +} + +#[test] +fn batch_response_budget_stops_later_steps_even_when_stop_on_error_is_false() { + let mut h = Headless::new(); + let mut steps = vec![json!({"command": "command.list"}); MAX_BATCH_STEPS - 1]; + steps.push(json!({"command": "file.new", "params": {"width": 8, "height": 8}})); + let result = h.batch(&json!({"steps": steps, "stopOnError": false})).unwrap(); + assert_eq!(result["failed"], 1); + assert!(result["results"].as_array().unwrap().last().unwrap()["error"].as_str().unwrap().contains("batch response budget exceeded")); + assert!(h.session.documents().is_empty(), "later edit must not execute after the quota is exhausted"); + assert!(crate::budgets::json_bytes(&result).is_ok()); + assert!(h.handle("methods", Value::Null).is_ok()); +} + +#[test] +fn idle_tcp_daemon_releases_its_port_but_keeps_active_clients() { + use std::io::BufReader; + const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let h = Arc::new(Mutex::new(Headless::new())); + let (ready_tx, ready_rx) = std::sync::mpsc::channel(); + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let server = std::thread::spawn(move || { + let result = serve_tcp_until_idle("127.0.0.1:0", h, TOKEN.to_owned(), move |addr| ready_tx.send(addr).unwrap(), Duration::from_millis(200)); + done_tx.send(result).unwrap(); + }); + let addr = ready_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let mut client = std::net::TcpStream::connect(addr).unwrap(); + client.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); + let mut reader = BufReader::new(client.try_clone().unwrap()); + writeln!(client, r#"{{"id":"auth","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + let auth: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(auth["result"]["authenticated"], true); + // The port stays owned well past the idle limit while a client is connected. + assert!(matches!(done_rx.recv_timeout(Duration::from_millis(450)), Err(std::sync::mpsc::RecvTimeoutError::Timeout)), "daemon exited with a live client"); + drop(reader); + drop(client); + assert!(done_rx.recv_timeout(Duration::from_secs(3)).unwrap().is_ok(), "idle daemon did not exit cleanly"); + server.join().unwrap(); + assert!(TcpListener::bind(addr).is_ok(), "idle listener still owns its port"); +} + +#[test] +fn tcp_serves_loopback() { + use std::io::{BufReader, Write as _}; + const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let h = Arc::new(Mutex::new(Headless::new())); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); + }); + let addr = rx.recv().unwrap(); + let mut s = std::net::TcpStream::connect(addr).unwrap(); + writeln!(s, r#"{{"id":"auth","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); + let mut reader = BufReader::new(s.try_clone().unwrap()); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + let auth: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(auth["result"]["authenticated"], true); + writeln!(s, r#"{{"id":"a","method":"methods"}}"#).unwrap(); + line.clear(); + reader.read_line(&mut line).unwrap(); + let v: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(v["id"], "a"); + assert!(v["result"].as_array().unwrap().iter().any(|m| m == "batch")); +} + +/// #4965: a connection that keeps sending without authenticating is closed at the deadline, +/// and one that authenticated in time keeps being served after it. +#[test] +fn tcp_connections_must_authenticate_before_the_deadline_whatever_they_send() { + use std::io::{BufReader, Write as _}; + use std::time::{Duration, Instant}; + const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let server = std::thread::spawn(move || { + let h = Mutex::new(Headless::new()); + let mut results = Vec::new(); + for _ in 0..2 { + let (stream, _) = listener.accept().unwrap(); + let started = Instant::now(); + let r = serve_tcp_connection(&h, stream, TOKEN, Duration::from_millis(300)); + results.push((r.map_err(|e| e.kind()), started.elapsed())); + } + results + }); + + // Never a newline, a byte every 20 ms: each read succeeds long before the idle timeout. + let mut slow = std::net::TcpStream::connect(addr).unwrap(); + for _ in 0..150 { + if slow.write_all(b" ").is_err() { + break; + } + std::thread::sleep(Duration::from_millis(20)); + } + drop(slow); + + // Authenticates at once, then asks again 300 ms after the deadline. + let mut s = std::net::TcpStream::connect(addr).unwrap(); + s.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + writeln!(s, r#"{{"id":"auth","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); + let mut reader = BufReader::new(s.try_clone().unwrap()); + let mut line = String::new(); + reader.read_line(&mut line).unwrap(); + assert_eq!(serde_json::from_str::(&line).unwrap()["result"]["authenticated"], true); + std::thread::sleep(Duration::from_millis(600)); + writeln!(s, r#"{{"id":"a","method":"methods"}}"#).unwrap(); + line.clear(); + reader.read_line(&mut line).unwrap(); + assert_eq!(serde_json::from_str::(&line).unwrap()["id"], "a"); + drop(reader); + drop(s); + + let results = server.join().unwrap(); + let (slow_result, slow_time) = &results[0]; + assert!(matches!(slow_result, Err(std::io::ErrorKind::TimedOut | std::io::ErrorKind::WouldBlock)), "{slow_result:?}"); + assert!(*slow_time < Duration::from_secs(3), "the trickle did not extend the deadline: {slow_time:?}"); + assert_eq!(results[1].0, Ok(()), "the authenticated connection ended with its client"); +} + +#[test] +fn tcp_repeat_auth_reports_already_authenticated_and_keeps_serving() { + use std::io::{BufReader, Write as _}; + const TOKEN: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + let h = Arc::new(Mutex::new(Headless::new())); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); + }); + let addr = rx.recv().unwrap(); + let mut s = std::net::TcpStream::connect(addr).unwrap(); + let mut reader = BufReader::new(s.try_clone().unwrap()); + let mut line = String::new(); + for id in ["auth", "again"] { + writeln!(s, r#"{{"id":"{id}","method":"auth","params":{{"token":"{TOKEN}"}}}}"#).unwrap(); + line.clear(); + reader.read_line(&mut line).unwrap(); + } + let again: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(again["id"], "again"); + assert_eq!(again["ok"], false); + assert_eq!(again["error"], "already authenticated"); + writeln!(s, r#"{{"id":"m","method":"methods"}}"#).unwrap(); + line.clear(); + reader.read_line(&mut line).unwrap(); + let v: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(v["id"], "m"); + assert_eq!(v["ok"], true); +} + +#[test] +fn tcp_rejects_requests_before_authentication() { + use std::io::{BufReader, Write as _}; + const TOKEN: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + let h = Arc::new(Mutex::new(Headless::new())); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let _ = serve_tcp("127.0.0.1:0", h, TOKEN.into(), move |a| tx.send(a).unwrap()); + }); + let addr = rx.recv().unwrap(); + let mut s = std::net::TcpStream::connect(addr).unwrap(); + writeln!(s, r#"{{"id":1,"method":"methods"}}"#).unwrap(); + let mut line = String::new(); + BufReader::new(s).read_line(&mut line).unwrap(); + let v: Value = serde_json::from_str(&line).unwrap(); + assert_eq!(v["ok"], false); + assert_eq!(v["error"], "authentication required"); + assert!(v.get("result").is_none()); +} + +#[test] +fn invalid_document_index_is_rejected_not_the_active_document() { + let h = session(); + for _ in 0..2 { + assert_eq!(respond(&h, r#"{"method":"doc.new","params":{"width":8,"height":8}}"#)["ok"], true); + } + let count = |h: &Mutex| h.lock().unwrap().session_list()["documents"].as_array().unwrap().len(); + for index in [json!("0"), json!(-1), json!(0.5), json!(true)] { + for method in ["doc.close", "doc.inspect", "doc.select"] { + let reply = respond(&h, &json!({"method": method, "params": {"index": index}}).to_string()); + assert_eq!(reply["ok"], false, "{method} {index}: {reply}"); + assert!(reply["error"].as_str().unwrap().contains("`index` must be a non-negative integer"), "{reply}"); + } + assert_eq!(count(&h), 2, "index {index} closed a document"); + } + // Null means "the active document", like a missing `index`. + assert_eq!(respond(&h, r#"{"method":"doc.inspect","params":{"index":null}}"#)["ok"], true); + // A valid index still closes that document. + let reply = respond(&h, r#"{"method":"doc.close","params":{"index":0}}"#); + assert_eq!(reply["ok"], true, "{reply}"); + assert_eq!(count(&h), 1); +} diff --git a/crates/automation/src/security.rs b/crates/automation/src/security.rs index 7928420b12..7fff71c67a 100644 --- a/crates/automation/src/security.rs +++ b/crates/automation/src/security.rs @@ -2,7 +2,7 @@ use std::fs::OpenOptions; use std::io::{BufRead, Read, Write}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::{Duration, Instant}; @@ -97,11 +97,15 @@ pub fn validate_token(token: &str) -> Result<(), AutomationError> { } /// Compare fixed-width tokens without an early exit on a mismatching byte. +/// +/// Hex letters match in either case: [`validate_token`] accepts uppercase, and the server keeps +/// the token lowercased, so a client sending the token exactly as it was configured must still +/// authenticate (#6425). Only `A`–`Z` fold, so a non-hex byte never matches a hex digit. pub fn token_matches(expected: &str, supplied: &str) -> bool { if expected.len() != TOKEN_HEX_LEN || supplied.len() != TOKEN_HEX_LEN { return false; } - expected.bytes().zip(supplied.bytes()).fold(0u8, |different, (a, b)| different | (a ^ b)) == 0 + expected.bytes().zip(supplied.bytes()).fold(0u8, |different, (a, b)| different | (a.to_ascii_lowercase() ^ b.to_ascii_lowercase())) == 0 } /// Validate the first TCP frame without exposing any control method before authentication. @@ -135,7 +139,27 @@ pub fn repeat_auth_reply(line: &str) -> Option { } fn read_token_file(path: &Path) -> Result { - let token = std::fs::read_to_string(path).map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; + // Validate what we actually opened, not just the name: an existing control-token file must be + // a regular file that no other local principal can read. A freshly created one is 0600 + // (`create_token_file`); an operator-supplied one is checked here so a group- or world-readable + // token is refused rather than trusted as the control credential (#4966). + let mut file = std::fs::File::open(path).map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; + let meta = file.metadata().map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; + if !meta.is_file() { + return Err(AutomationError::Io(format!("{}: control token file is not a regular file", path.display()))); + } + #[cfg(unix)] + { + use std::os::unix::fs::MetadataExt; + if meta.mode() & 0o077 != 0 { + return Err(AutomationError::Io(format!( + "{}: control token file is readable by group or others (mode {:o}); restrict it to its owner (e.g. chmod 600)", + path.display(), + meta.mode() & 0o777 + ))); + } + } + let token = std::io::read_to_string(&mut file).map_err(|e| AutomationError::Io(format!("{}: {e}", path.display())))?; let token = token.trim().to_owned(); validate_token(&token)?; Ok(token) @@ -284,13 +308,6 @@ impl Read for AuthDeadlineStream { } } -/// Environment-aware token inputs shared by the desktop app and CLI. -pub fn token_inputs(supplied: Option, token_file: Option) -> (Option, Option) { - let supplied = supplied.or_else(|| std::env::var("PHOTOCRAFT_CONTROL_TOKEN").ok()); - let token_file = token_file.or_else(|| std::env::var_os("PHOTOCRAFT_CONTROL_TOKEN_FILE").map(PathBuf::from)); - (supplied, token_file) -} - #[cfg(test)] mod tests { use super::*; @@ -348,6 +365,45 @@ mod tests { assert_eq!(reply["result"]["authenticated"], true); } + fn auth_line(token: &str) -> String { + json!({"id": 1, "method": AUTH_METHOD, "params": {"token": token}}).to_string() + } + + /// #6425: `validate_token` accepts uppercase hex and the server keeps the token lowercased, so a + /// client sending the token exactly as it was configured was refused. + #[test] + fn a_token_authenticates_in_any_letter_case() { + let configured = "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; + let mixed = "0123456789AbCdEf0123456789aBcDeF0123456789ABCDEF0123456789abcdef"; + let expected = server_token(Some(configured), None).unwrap(); + for supplied in [configured, &configured.to_ascii_lowercase(), mixed] { + let (reply, authenticated) = authentication_reply(&auth_line(supplied), &expected); + assert!(authenticated, "{supplied}: {reply}"); + assert_eq!(reply["result"]["authenticated"], true); + } + // The comparison is case-insensitive in both directions. + assert!(token_matches(configured, &configured.to_ascii_lowercase())); + + // A different token, or a non-hex character that only folds to a hex one, still fails. + let other = "1123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; + let (_, authenticated) = authentication_reply(&auth_line(other), &expected); + assert!(!authenticated); + let not_hex = "0123456789ABCDEG0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; + assert!(!token_matches(&expected, not_hex)); + } + + /// #6425: the same for a token file whose contents are uppercase. + #[test] + fn an_uppercase_token_file_authenticates_its_exact_contents() { + let path = std::env::temp_dir().join(format!("photocraft-control-token-upper-{}-{}.txt", std::process::id(), generate_token().unwrap())); + let contents = generate_token().unwrap().to_ascii_uppercase(); + create_token_file(&path, &contents).unwrap(); + let expected = server_token(None, Some(&path)); + std::fs::remove_file(&path).unwrap(); + let (reply, authenticated) = authentication_reply(&auth_line(&contents), &expected.unwrap()); + assert!(authenticated, "{reply}"); + } + #[test] fn repeat_auth_on_an_authenticated_connection_says_so() { let line = json!({"id": 3, "method": AUTH_METHOD, "params": {"token": "anything"}}).to_string(); @@ -417,22 +473,50 @@ mod tests { #[test] fn an_authenticated_connection_outlives_the_authentication_deadline() { let (server, mut client) = tcp_pair(); + // The test thread writes the first line before the deadline starts. The sender writes the + // second line once the connection has authenticated and the deadline has passed. + client.write_all(b"one\n").unwrap(); + let limit = Duration::from_secs(1); + let mut reader = std::io::BufReader::new(AuthDeadlineStream::new(server, limit)); + let deadline = Instant::now() + limit; + let (tx, rx) = std::sync::mpsc::channel(); let sender = std::thread::spawn(move || { - client.write_all(b"one\n").unwrap(); - std::thread::sleep(Duration::from_millis(500)); + rx.recv().unwrap(); + std::thread::sleep((deadline + Duration::from_millis(300)).saturating_duration_since(Instant::now())); client.write_all(b"two\n").unwrap(); }); - let mut reader = std::io::BufReader::new(AuthDeadlineStream::new(server, Duration::from_millis(200))); let mut line = String::new(); assert_eq!(read_bounded_line(&mut reader, &mut line).unwrap(), LineRead::Line); assert_eq!(line, "one\n"); reader.get_mut().authenticated().unwrap(); + tx.send(()).unwrap(); // Sent 300 ms after the deadline: only the idle timeout applies now. assert_eq!(read_bounded_line(&mut reader, &mut line).unwrap(), LineRead::Line); assert_eq!(line, "two\n"); sender.join().unwrap(); } + #[cfg(unix)] + #[test] + fn an_existing_token_file_must_be_owner_only() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("photocraft-token-perm-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("token.txt"); + let token = "0123456789abcdef".repeat(4); // 64 hex chars, passes validate_token + std::fs::write(&path, &token).unwrap(); + + // A group- or world-readable token is refused: the control credential must not be shared (#4966). + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + assert!(read_token_file(&path).is_err(), "a 0644 token file must be refused"); + + // Owner-only is opened, read and validated. + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap(); + assert_eq!(read_token_file(&path).unwrap(), token, "a 0600 token file must be read"); + + std::fs::remove_dir_all(&dir).ok(); + } + #[test] fn connection_limiter_releases_capacity() { let limiter = ConnectionLimiter::new(1); diff --git a/crates/automation/src/server.rs b/crates/automation/src/server.rs index f52376a9c5..544dbc90f1 100644 --- a/crates/automation/src/server.rs +++ b/crates/automation/src/server.rs @@ -71,6 +71,20 @@ pub struct PlaceParams { pub center: Option<[f64; 2]>, } +#[derive(Debug, Default, Deserialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ReplaceSmartObjectParams { + /// Path beneath the configured automation read root: relative (forward slashes) or absolute. + pub path: String, + /// Headless mode only: document index from `session_list` (default: the active document). + /// Bridge mode rejects `index`. + #[serde(default)] + pub index: Option, + /// The Smart Object layer: an id or a name (default: the active layer). + #[serde(default)] + pub layer: Option, +} + #[derive(Debug, Default, Deserialize, JsonSchema)] #[serde(deny_unknown_fields)] pub struct ExportLayersParams { @@ -170,6 +184,13 @@ pub struct SaveParams { /// DPX: 8, 10, 12 or 16 bits per sample. Unset: 8 for an 8-bit document, else 16. #[serde(default, rename = "dpxBits")] pub dpx_bits: Option, + /// DDS: "none" (uncompressed, the default), "bc1" (DXT1: alpha on or off), "bc2" (DXT3) or + /// "bc3" (DXT5). + #[serde(default, rename = "ddsCompression")] + pub dds_compression: Option, + /// DDS: also write the full mip chain, each level a box filter of the one above. + #[serde(default, rename = "ddsMipmaps")] + pub dds_mipmaps: bool, #[serde(default)] pub index: Option, } @@ -308,7 +329,7 @@ pub struct MenuParams { pub struct UiSetParams { /// Fields for the control method `ui.set`: tool, panels, dock, dockTabs, dockWidth, colorPanel /// ({background: bool} picks which swatch the Color panel edits), maskTarget, - /// vectorMaskTarget, selectionMode, zoom, center, rotation (view angle in degrees), fit, theme (pro, proMedium, studio, + /// vectorMaskTarget, filterMaskTarget (paint the active smart object's filter mask), selectionMode, zoom, center, rotation (view angle in degrees), fit, theme (pro, proMedium, studio, /// studioLight, classic, solarizedDark, adwaita, adwaitaDark, breezeLight, breezeDark, softLight, softDark), brushSection, brushTab, /// brushesView, brushPicker ([x, y] opens the Brush Preset picker there, null closes it), /// brushPickerName, brushPickerStroke, brushPickerTip (the picker card's parts, at least one @@ -321,7 +342,9 @@ pub struct UiSetParams { /// cropOverlayOrientation (0..3), cropShield (an object patching show_cropped_area, enabled, /// color ("matchCanvas" or "custom"), custom_color ([r, g, b]), opacity (0..100), /// auto_adjust, classic_mode and auto_center_preview), cropFill (background or contentAware, - /// the Crop tool's Fill), shapeStroke (an object patching width, align, cap, join, miterLimit, + /// the Crop tool's Fill), penMode (path or shape, the Pen tools' mode), penPathOp (newLayer, + /// combine, subtract, intersect or exclude: how the next Pen path joins the selected shape + /// layer or path), shapeStroke (an object patching width, align, cap, join, miterLimit, /// dashes, dashOffset and opacity; null disables the stroke). Shape stroke fields use /// `shape.edit` names and units; colour comes from `tools.setColors`. camTransformOnDrop (bool, /// the Content-Aware Move tool's Transform On Drop). tileGrid (bool, a developer overlay of the @@ -329,7 +352,14 @@ pub struct UiSetParams { /// "mode"), kinds (a list of layer kinds), name (matches part of the layer name) and mode (a /// blend mode name) for the Layers panel filter). strokeTrace (true, false, "stderr" or "off": /// the stroke latency trace at runtime; a CSV file is set at launch with --stroke-trace, not - /// over this channel). Other fields are an error. + /// over this channel). updatesOpen (bool, the Help › Check for Updates window). + /// histogramPanel (an object patching view ("compact", "expanded" or "allChannels"), channel + /// ("composite", "red", "green", "blue", "cyan", "magenta", "yellow", "black", "a", "b", + /// "luminosity" or "colors"), showChannelsInColor, showStatistics and source ("entireImage", + /// "selectedLayer" or "adjustmentComposite") for the Histogram panel), histogramRange ([from, + /// to] levels 0..255 whose Count and Percentile the panel shows, or null) and histogramRefresh + /// (true: an Uncached Refresh); `ui.inspect` reports them and the statistics under + /// `histogram`. Other fields are an error. pub fields: Value, } @@ -405,6 +435,14 @@ fn strict_node(v: &mut Value, defs: &serde_json::Map, depth: usiz } } +/// Both the bare engine list and older bridge wrappers own their rows already. +fn into_command_rows(mut reply: Value) -> Vec { + match reply { + Value::Array(rows) => rows, + _ => reply.get_mut("commands").and_then(Value::as_array_mut).map(std::mem::take).unwrap_or_default(), + } +} + fn ok_json(v: &Value) -> CallToolResult { let text = match json_bytes(v).and_then(|bytes| String::from_utf8(bytes).map_err(|error| AutomationError::Other(error.to_string()))) { Ok(text) => text, @@ -630,7 +668,33 @@ impl PhotocraftMcp { } #[tool( - description = "Export each exported layer of the document as its own file beneath the write root (the capability-scoped Layers to Files). A file that already exists is an error, not an overwrite." + description = "Replace a Smart Object's contents with the file at `path` beneath the read root, keeping its transform and filters (the capability-scoped Replace Contents). `layer` is the Smart Object's id or name (default: the active layer)." + )] + async fn doc_replace_smart_object(&self, Parameters(p): Parameters) -> Result { + let mut opts = serde_json::Map::new(); + if let Some(v) = p.layer { + opts.insert("layer".into(), v); + } + let opts = Value::Object(opts); + // Bridge mode runs the app on this machine, which reads the path itself like any command. + let mut bridge_params = opts.clone(); + if let Some(o) = bridge_params.as_object_mut() { + o.insert("path".into(), Value::String(p.path.clone())); + } + let (path, index) = (p.path, p.index); + if let Some(r) = self.headless_op(move |h| h.replace_smart_object(index, &path, &opts)).await { + return to_result(r); + } + // The app's command runs on its active document: an `index` it cannot honour is refused + // rather than dropped, or another document's contents would be replaced. + if index.is_some() { + return Ok(fail("`index` is only supported in headless mode; in bridge mode select the document in the app first")); + } + self.run_command("layer.smartObjects.replaceContents".into(), bridge_params, true).await + } + + #[tool( + description = "Export top-level layers as files (groups merged). Returns files and skipped: [{layer, name, reason}] for hidden layers (visible_only) and adjustments with no pixels of their own. Workspace exports resolve beneath the write root, also return count of written files, and reject existing files instead of overwriting them. Trusted-local/bridge exports use the engine command's file access policy." )] async fn doc_export_layers(&self, Parameters(p): Parameters) -> Result { let mut opts = serde_json::Map::new(); @@ -820,10 +884,7 @@ impl PhotocraftMcp { }; let needle = p.filter.map(|f| f.to_lowercase()); let enabled_only = p.enabled_only.unwrap_or(false); - let items: Vec = all - .as_array() - .cloned() - .unwrap_or_else(|| all.get("commands").and_then(Value::as_array).cloned().unwrap_or_default()) + let items: Vec = into_command_rows(all) .into_iter() .filter(|c| { needle.as_ref().is_none_or(|n| photocraft_engine::commands::list_entry_matches(c, n)) @@ -1001,10 +1062,18 @@ impl PhotocraftMcp { Some((_, Some(bits))) => Some(bits), Some((b, None)) => return Ok(fail(format!("`dpxBits` must be 8, 10, 12 or 16, got {b}"))), }; + if let Some(q) = p.quality.filter(|q| !(1..=100).contains(q)) { + return Ok(fail(format!("`quality` must be a whole number from 1 to 100, got {q}"))); + } + let dds_compression = match p.dds_compression.as_deref().map(|c| (c, photocraft_io::DdsCompression::from_name(c))) { + None => None, + Some((_, Some(c))) => Some(c), + Some((c, None)) => return Ok(fail(format!("`ddsCompression` must be none, bc1, bc2 or bc3, got {c:?}"))), + }; if let Some(b) = self.bridge_client() { // The bridge forwards to the running app's `app.save`, which takes only an optional // path (without one it writes back to the document's own layered file, as headless - // does) and the Targa bits, so the other options are headless-only. Saying so beats + // does), the Targa bits and the DDS options, so the other options are headless-only. Saying so beats // saving with defaults while the caller believes their quality or format was applied. let unsupported: Vec<&str> = [ p.format.is_some().then_some("format"), @@ -1026,15 +1095,22 @@ impl PhotocraftMcp { if let Some(bits) = tga_bits { params["tgaBits"] = json!(bits.bits()); } + if let Some(c) = dds_compression { + params["ddsCompression"] = json!(c.name()); + } + if p.dds_mipmaps { + params["ddsMipmaps"] = json!(true); + } return to_result(b.call("app.save", params).await); } let Some(r) = self .headless_op(move |h| { let mut opts = photocraft_io::ExportOptions { tiff_layers: p.tiff_layers, tga_bits, ..Default::default() }; opts.encode.dpx_bits = dpx_bits; + opts.encode.dds = photocraft_io::DdsOptions { compression: dds_compression.unwrap_or_default(), mipmaps: p.dds_mipmaps }; if let Some(q) = p.quality { - opts.encode.jpeg_quality = q.clamp(1, 100); - opts.encode.webp_quality = q.clamp(1, 100); + opts.encode.jpeg_quality = q; + opts.encode.webp_quality = q; opts.encode.webp_lossless = false; } let path = p.path.map(PathBuf::from); @@ -1061,6 +1137,29 @@ pub fn render_document_png(doc: &photocraft_doc::Document, max_side: u32) -> Res mod tests { use super::*; + #[test] + fn command_rows_keep_bare_and_wrapped_replies_compatible() { + let rows = json!([{"id":"first","params":{"nested":[1,2]}}, {"id":"second","enabled":false}]); + assert_eq!(into_command_rows(rows.clone()), rows.as_array().unwrap().clone()); + assert_eq!(into_command_rows(json!({"commands":rows,"other":"ignored"})), rows.as_array().unwrap().clone()); + for reply in [json!(null), json!(false), json!(3), json!("invalid"), json!({}), json!({"commands":null}), json!({"commands":{}}), json!([])] { + assert!(into_command_rows(reply).is_empty()); + } + } + + #[test] + fn command_rows_reuse_the_owned_array_and_nested_strings() { + for wrapped in [false, true] { + let rows = json!([{"id":"first","params":"documentation".repeat(64)}]); + let array = rows.as_array().unwrap().as_ptr(); + let text = rows[0]["params"].as_str().unwrap().as_ptr(); + let reply = if wrapped { Value::Object([("commands".into(), rows)].into_iter().collect()) } else { rows }; + let moved = into_command_rows(reply); + assert_eq!(moved.as_ptr(), array, "wrapped={wrapped}: move the original allocation"); + assert_eq!(moved[0]["params"].as_str().unwrap().as_ptr(), text, "wrapped={wrapped}: retain nested allocations"); + } + } + #[test] fn strict_schema_inlines_refs_and_stops_on_recursive_definitions() { // A definition that refers to itself: inlining stops at a depth instead of looping. @@ -1175,7 +1274,7 @@ mod tests { break; } assert!(t.elapsed().as_secs() < 60, "doc_inspect never saw the finished job: {inspected}"); - std::thread::sleep(std::time::Duration::from_millis(5)); + tokio::time::sleep(std::time::Duration::from_millis(5)).await; } } } diff --git a/crates/automation/src/server/conventions.rs b/crates/automation/src/server/conventions.rs index 6e2740041d..b48c41db87 100644 --- a/crates/automation/src/server/conventions.rs +++ b/crates/automation/src/server/conventions.rs @@ -1,7 +1,10 @@ //! Catalog metadata, strict tool envelopes and live resources. -use super::*; +use super::{Arc, Backend, BatchParams, DocIndex, ListParams, McpError, Parameters, PhotocraftMcp, RunParams, ServerHandler, Value, json}; use rmcp::RoleServer; -use rmcp::model::*; +use rmcp::model::{ + CallToolRequestParams, CallToolResponse, Implementation, ListResourcesResult, ListToolsResult, PaginatedRequestParams, ReadResourceRequestParams, + ReadResourceResponse, ReadResourceResult, ServerCapabilities, ServerConfig, Tool, ToolAnnotations, +}; use rmcp::service::RequestContext; const DOCUMENT: &str = "photocraft://document"; diff --git a/crates/automation/src/server/progress.rs b/crates/automation/src/server/progress.rs index 4182743083..1844203f8e 100644 --- a/crates/automation/src/server/progress.rs +++ b/crates/automation/src/server/progress.rs @@ -1,5 +1,5 @@ //! Render Video runs on the existing blocking worker; rmcp remains available for ping/cancel. -use super::*; +use super::{CallToolResult, PhotocraftMcp, Value, fail, no_backend, ok_json}; use rmcp::RoleServer; use rmcp::model::ProgressNotificationParam; use rmcp::service::RequestContext; diff --git a/crates/automation/src/server/transport.rs b/crates/automation/src/server/transport.rs index 44ba4596ba..87a98e0305 100644 --- a/crates/automation/src/server/transport.rs +++ b/crates/automation/src/server/transport.rs @@ -1,5 +1,5 @@ //! Recover malformed JSON lines before passing valid messages to rmcp. -use super::*; +use super::{AutomationError, PhotocraftMcp, ServiceExt, Value, json}; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; use crate::security::MAX_REQUEST_BYTES; @@ -7,7 +7,7 @@ use crate::security::MAX_REQUEST_BYTES; /// Read a complete request before allowing any of its bytes to reach rmcp. `lines()` /// buffers without a limit, while forwarding partial input can make a valid JSON prefix /// executable at EOF after an oversized suffix is rejected. -async fn bounded_line(source: &mut BufReader, maximum: usize) -> std::io::Result> { +async fn bounded_line(source: &mut BufReader, maximum: usize) -> std::io::Result>> { let mut bytes = Vec::new(); loop { let available = source.fill_buf().await?; @@ -15,7 +15,7 @@ async fn bounded_line(source: &mut BufReader if bytes.is_empty() { return Ok(None); } - return String::from_utf8(bytes).map(Some).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)); + return Ok(Some(bytes)); } let newline = available.iter().position(|&byte| byte == b'\n'); let length = newline.map_or(available.len(), |at| at + 1); @@ -29,7 +29,7 @@ async fn bounded_line(source: &mut BufReader if bytes.last() == Some(&b'\r') { bytes.pop(); } - return String::from_utf8(bytes).map(Some).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)); + return Ok(Some(bytes)); } } } @@ -48,11 +48,16 @@ impl PhotocraftMcp { let reader = tokio::spawn(async move { let mut lines = BufReader::new(input); while let Some(line) = bounded_line(&mut lines, MAX_REQUEST_BYTES).await? { - if line.trim().is_empty() { + if std::str::from_utf8(&line).is_ok_and(|text| text.trim().is_empty()) { continue; } - match serde_json::from_str::(&line) { - Ok(_) => to_service.write_all(format!("{line}\n").as_bytes()).await?, + // Bytes that are not UTF-8 are malformed JSON too: they get the -32700 reply + // like any other bad line instead of ending the session. + match serde_json::from_slice::(&line) { + Ok(_) => { + to_service.write_all(&line).await?; + to_service.write_all(b"\n").await?; + } Err(e) => { let reply = json!({"jsonrpc":"2.0", "id":null, "error":{"code":-32700, "message":format!("parse error: {e}")}}); if errors.send(reply.to_string()).await.is_err() { @@ -99,8 +104,8 @@ mod tests { #[tokio::test] async fn bounded_line_accepts_exact_limit_and_final_line() { let mut input = BufReader::new(std::io::Cursor::new(b"1234\nlast!".to_vec())); - assert_eq!(bounded_line(&mut input, 5).await.unwrap().as_deref(), Some("1234")); - assert_eq!(bounded_line(&mut input, 5).await.unwrap().as_deref(), Some("last!")); + assert_eq!(bounded_line(&mut input, 5).await.unwrap().as_deref(), Some(&b"1234"[..])); + assert_eq!(bounded_line(&mut input, 5).await.unwrap().as_deref(), Some(&b"last!"[..])); assert!(bounded_line(&mut input, 5).await.unwrap().is_none()); let mut oversized_eof = BufReader::new(std::io::Cursor::new(b"longer".to_vec())); @@ -143,4 +148,42 @@ mod tests { let documents = observer.headless_op(|h| Ok(h.session.documents().len())).await.unwrap().unwrap(); assert_eq!(documents, 0, "the oversized tool call must not run"); } + + async fn next_reply(replies: &mut tokio::io::Lines>) -> Value { + let line = tokio::time::timeout(std::time::Duration::from_secs(5), replies.next_line()).await.expect("a reply in time").unwrap().expect("a reply line"); + serde_json::from_str(&line).unwrap() + } + + #[tokio::test] + async fn a_line_that_is_not_utf8_gets_a_parse_error_and_the_session_keeps_serving() { + let (mut client, input) = tokio::io::duplex(1 << 16); + let (output, replies) = tokio::io::duplex(1 << 16); + let server = PhotocraftMcp::headless(); + let observer = server.clone(); + let session = tokio::spawn(server.serve_io(input, output)); + let mut replies = BufReader::new(replies).lines(); + + let initialize = json!({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { + "protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "utf8-test", "version": "1"} + }}); + client.write_all(format!("{initialize}\n").as_bytes()).await.unwrap(); + assert_eq!(next_reply(&mut replies).await["id"], 1); + client.write_all(b"{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}\n").await.unwrap(); + + client.write_all(b"{\"jsonrpc\":\"2.0\",\"id\":\xff\xfe}\n").await.unwrap(); + let error = next_reply(&mut replies).await; + assert_eq!(error["error"]["code"], -32700, "{error}"); + assert_eq!(error["id"], Value::Null, "{error}"); + + let call = json!({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "doc_new", "arguments": {"width": 8, "height": 8}}}); + client.write_all(format!("{call}\n").as_bytes()).await.unwrap(); + let reply = next_reply(&mut replies).await; + assert_eq!(reply["id"], 2, "{reply}"); + assert_ne!(reply["result"]["isError"], true, "{reply}"); + let documents = observer.headless_op(|h| Ok(h.session.documents().len())).await.unwrap().unwrap(); + assert_eq!(documents, 1, "the call after the bad line must run"); + + drop(client); + session.await.unwrap().unwrap(); + } } diff --git a/crates/automation/src/workspace.rs b/crates/automation/src/workspace.rs index f73a702385..2334a2ff89 100644 --- a/crates/automation/src/workspace.rs +++ b/crates/automation/src/workspace.rs @@ -1,7 +1,7 @@ //! Capability-based filesystem policy for untrusted automation paths. use std::io::{Read, Write}; -use std::path::{Component, Path, PathBuf}; +use std::path::{Component, Components, Path, PathBuf, Prefix}; use std::sync::Arc; use cap_std::ambient_authority; @@ -16,16 +16,18 @@ const DENIED: &str = "automation filesystem access is not granted"; #[derive(Clone)] struct RootCapability { dir: Arc, - /// The root as an absolute path (trusted launch configuration), to recognise absolute request + /// The canonical root path (trusted launch configuration), to recognise absolute request /// paths that name a file beneath it. path: PathBuf, + /// The trusted launch spelling also names the same held capability, even through a link. + configured_path: PathBuf, } /// Separate directory capabilities for automation reads and writes. /// /// Root paths are trusted launch-time configuration. Request paths are always /// untrusted, forward-slash relative paths resolved by `cap-std` beneath the -/// held directory handle, or absolute paths whose text lies beneath the root +/// held directory handle, or absolute paths beneath the canonical or configured root spelling /// (translated to relative ones, see [`request_path`]). Parent traversal, /// alternate separators, Windows prefixes and empty components are rejected /// before I/O. @@ -74,6 +76,13 @@ impl AuthorizedWorkspace { /// as [`photocraft_format::atomic_write`], through the directory capability). On failure /// the previous file is untouched and the temporary file is removed. /// + /// Replacing a file carries its extended attributes — macOS Finder tags, Linux `user.*` — + /// onto the file that takes its place (#7095), through the two open descriptors rather than + /// their paths, so nothing here reaches outside the write root to finish a write. The rest + /// of what [`photocraft_format::atomic_write`] preserves does not follow: its permission + /// step is tracked in #4963, and the Windows `ReplaceFileW` swap is path-based, which this + /// path may not use. + /// /// The parent directory must already exist. `cap-std` performs path /// resolution and file creation relative to the held directory handle, so /// a non-existent final target is supported without ambient path access. @@ -81,14 +90,43 @@ impl AuthorizedWorkspace { let relative = request_path(path, self.write.as_ref())?; let root = self.write.as_ref().ok_or_else(|| AutomationError::BadRequest(format!("{DENIED}: write authority is absent")))?; let leaf = relative.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + // The bytes are an image or document whatever the name says (`format` picks them), so + // only a file of a kind PhotoCraft writes is replaced: never source, settings or notes + // that share the root (`src/main.rs`, `.env`; #9439). A new file may have any name, and + // an image saved under one (`out` with `format: png`) can be saved over again. + let exists = root.dir.symlink_metadata(&relative).is_ok(); + // Only a regular file is ever opened below (its head here, its attributes on Unix): a + // FIFO would block that open until a writer appears, and with it every other request. + // Links are followed for this check; a dangling one falls through to the name rule. + if exists && root.dir.metadata(&relative).is_ok_and(|m| !m.is_file()) { + return Err(AutomationError::BadRequest(format!("automation write refused: `{path}` already exists and is not a regular file"))); + } + if exists && !written_kind(&leaf) && !holds_image(&root.dir, &relative) { + return Err(AutomationError::BadRequest(format!( + "automation write refused: `{path}` already exists and is not an image or document file PhotoCraft writes; save to a new name" + ))); + } let parent = relative.parent().map(Path::to_path_buf).unwrap_or_default(); let tmp = parent.join(photocraft_format::atomic::temp_name(&leaf)); let mut options = OpenOptions::new(); options.write(true).create_new(true); - let written = root.dir.open_with(&tmp, &options).and_then(|mut file| { + let written = root.dir.open_with(&tmp, &options).and_then(|file| { + let mut file = file.into_std(); file.write_all(bytes)?; + #[cfg(unix)] + { + use std::os::fd::AsFd; + // Best effort, and only when there is a file being replaced: a save still + // succeeds for a document whose Finder tag could not come along. + if let Ok(old) = root.dir.open(&relative) { + let old = old.into_std(); + let _ = photocraft_format::atomic::meta::copy_xattrs(old.as_fd(), file.as_fd()); + // The creation time too (#9388), through the same handles. + let _ = photocraft_format::atomic::meta::copy_created_file(&old, &file); + } + } // Falls back to a plain fsync on shares that refuse a full flush (#1336). - photocraft_format::atomic::sync_file(&file.into_std()) + photocraft_format::atomic::sync_file(&file) }); let renamed = written.and_then(|()| photocraft_format::atomic::retry_rename(RenameRetry::platform(), || root.dir.rename(&tmp, &root.dir, &relative))); if let Err(e) = renamed { @@ -107,6 +145,23 @@ impl AuthorizedWorkspace { } } +/// Whether `name`'s extension is one of the formats PhotoCraft saves or exports. +fn written_kind(name: &str) -> bool { + let Some((_, ext)) = name.rsplit_once('.') else { return false }; + let ext = ext.to_ascii_lowercase(); + ["psd", "psb", "ora", "pdf", photocraft_format::EXTENSION].contains(&ext.as_str()) || photocraft_codecs::from_extension(&ext).is_some() +} + +/// Whether the file at `relative` starts like an image PhotoCraft reads (PSD/PSB or a codec's +/// magic). Containers that also hold other things (zip, PDF) are judged by name only. +fn holds_image(dir: &Dir, relative: &Path) -> bool { + let mut head = [0u8; 64]; + let Ok(mut file) = dir.open(relative) else { return false }; + let Ok(n) = file.read(&mut head) else { return false }; + let head = &head[..n]; + head.starts_with(b"8BPS") || photocraft_codecs::detect(head).is_some() +} + fn read_to_observed_length(reader: &mut impl Read, observed_length: u64) -> Result, AutomationError> { // Do not reserve from a potentially stale or attacker-controlled metadata length. Read in // chunks, and reject growth beyond the length observed on the opened file before appending. @@ -140,35 +195,60 @@ fn open_root(path: Option<&Path>, authority: &str) -> Result) -> Result { - let Some(root) = root.filter(|_| Path::new(raw).is_absolute()) else { return relative_path(raw) }; - let mut request = Path::new(raw).components(); - for want in root.path.components() { - let same = match (request.next(), want) { - (Some(Component::Prefix(a)), Component::Prefix(b)) => a.as_os_str().eq_ignore_ascii_case(b.as_os_str()), - (Some(got), want) => got == want, - (None, _) => false, + let path = Path::new(raw); + let Some(root) = root.filter(|_| path.is_absolute()) else { return relative_path(raw) }; + // Prefer the trusted launch spelling: it can itself contain `..`. Only that + // exact granted prefix is exempt; traversal in the remaining request still fails below. + let request = path_beneath_root(path, &root.configured_path) + .or_else(|| path_beneath_root(path, &root.path)) + .ok_or_else(|| path_error(raw, "absolute paths must be inside the automation root"))?; + let rest = request.map(|c| c.as_os_str().to_str().ok_or_else(|| path_error(raw, "path is not UTF-8"))).collect::, _>>()?; + relative_path(&rest.join("/")) +} + +/// Strip a granted root prefix without resolving the client path with ambient authority. +/// Windows ASCII case variants name the same grant; the unstripped suffix keeps its case. +fn path_beneath_root<'a>(path: &'a Path, root: &Path) -> Option> { + let mut request = path.components(); + for want in root.components() { + let same = match (request.next()?, want) { + (Component::Prefix(a), Component::Prefix(b)) => match (a.kind(), b.kind()) { + // std::fs::canonicalize uses verbatim paths on Windows. Accept their ordinary + // drive/UNC spelling too, without treating a device prefix as a disk prefix. + (Prefix::Disk(a) | Prefix::VerbatimDisk(a), Prefix::Disk(b) | Prefix::VerbatimDisk(b)) => a.eq_ignore_ascii_case(&b), + (Prefix::UNC(a, b) | Prefix::VerbatimUNC(a, b), Prefix::UNC(c, d) | Prefix::VerbatimUNC(c, d)) => { + a.eq_ignore_ascii_case(c) && b.eq_ignore_ascii_case(d) + } + _ => a.as_os_str().eq_ignore_ascii_case(b.as_os_str()), + }, + #[cfg(windows)] + (Component::Normal(a), Component::Normal(b)) => a.eq_ignore_ascii_case(b), + (got, want) => got == want, }; if !same { - return Err(path_error(raw, "absolute paths must be inside the automation root")); + return None; } } - let rest: Vec<&str> = request.map(|c| c.as_os_str().to_str().unwrap_or("\u{FFFD}")).collect(); - relative_path(&rest.join("/")) + Some(request) } fn relative_path(raw: &str) -> Result { @@ -204,11 +284,14 @@ fn relative_path(raw: &str) -> Result { Ok(PathBuf::from(raw)) } +/// At least every name cap-std refuses to open on Windows (`COM0`–`COM9`, `COM¹`–`COM³`, the same +/// for `LPT`, spaces before the extension ignored), so nothing written is unreadable (#9424). fn is_windows_device_name(component: &str) -> bool { - let stem = component.split('.').next().unwrap_or(component).to_ascii_uppercase(); + let stem = component.split('.').next().unwrap_or(component).trim_end().to_ascii_uppercase(); + let port = |n: &str| matches!(n, "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "¹" | "²" | "³"); matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") - || stem.strip_prefix("COM").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) - || stem.strip_prefix("LPT").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) + || stem.strip_prefix("COM").is_some_and(port) + || stem.strip_prefix("LPT").is_some_and(port) } fn path_error(path: &str, reason: &str) -> AutomationError { @@ -323,6 +406,8 @@ fn params_contain_ambient_path(id: &str, params: &Value) -> bool { } "image.mode.rgb" | "image.mode.grayscale" | "image.mode.cmyk" | "image.mode.lab" => &["profile"], "edit.assignProfile" | "edit.convertToProfile" | "edit.profileInfo" | "view.proofSetup" | "view.gamutWarning" => &["profile"], + // New Document's Color Profile (#321) can name an .icc file. + "file.new" => &["profile"], "edit.colorSettings" => &["workingRgb", "workingCmyk", "workingGray"], _ => &[], }; @@ -339,6 +424,13 @@ fn profile_command_may_read_ambient(id: &str, params: &Value) -> bool { if matches!(id, "color.profileMismatch" | "edit.colorSettings" | "edit.installedProfiles" | "view.proofSetup") { return true; } + // New Document skips exact `working` and `none`; the other working aliases resolve + // the configured working space, which may be an .icc path. + if id == "file.new" { + return params.get("profile").and_then(Value::as_str).is_some_and(|profile| { + matches!(profile, "default" | "working-cmyk" | "workingCmyk" | "working-rgb" | "workingRgb" | "working-gray" | "workingGray") + }); + } matches!(id, "edit.assignProfile" | "edit.convertToProfile" | "edit.profileInfo") && params.get("profile").and_then(Value::as_str).is_some_and(|profile| { matches!(profile, "working" | "default" | "working-cmyk" | "workingCmyk" | "working-rgb" | "workingRgb" | "working-gray" | "workingGray") @@ -357,18 +449,28 @@ fn preferences_may_grant_ambient_paths(id: &str, params: &Value) -> bool { fn preference_uses_ambient_filesystem(path: &str) -> bool { // The engine skips empty segments (`.scriptEvents.enabled` sets `scriptEvents.enabled`), so // judge the first non-empty one; no segment at all is the whole-preferences update. - let Some(section) = path.split('.').find(|segment| !segment.is_empty()) else { return true }; + let mut segments = path.split('.').filter(|segment| !segment.is_empty()); + let Some(section) = segments.next() else { return true }; + // File Handling's LUT folder (#6558) is listed from disk; the rest of that section is not. matches!(section, "colorSettings" | "scriptEvents" | "historyLog" | "plugIns" | "scratchDisks") + || (section == "fileHandling" && segments.next().is_none_or(|key| key == "lutFolder")) } fn command_uses_ambient_path(id: &str, params: &Value) -> bool { match id { - "actions.load" | "brush.presets.importAbr" | "brush.presets.importImage" | "gradient.presets.importGrd" | "plugin.install" | "swatches.import" => { + "actions.load" + | "brush.presets.importAbr" + | "brush.presets.importImage" + | "gradient.presets.importGrd" + | "plugin.install" + | "swatches.import" + | "style.presets.importAsl" + | "layer.layerStyle.contours.load" => { // `data` wins over `path` in these commands; any `path` without it reads the filesystem. params.get("data").is_none() && params.get("path").is_some() } // With a `path` the file is written there; without one the bytes come back as `data`. - "actions.save" | "swatches.export" => params.get("path").is_some(), + "actions.save" | "swatches.export" | "style.presets.exportAsl" | "layer.layerStyle.contours.save" => params.get("path").is_some(), // Shortcut sets: a .kys read or written by path, or a Summarize page written by path. "edit.keyboardShortcuts" => { params.get("path").is_some() || ["importKys", "exportKys", "summarize"].iter().any(|k| params.get(*k).and_then(|v| v.get("path")).is_some()) @@ -447,534 +549,4 @@ impl Drop for ExportSequence { } #[cfg(test)] -mod tests { - use super::*; - - fn roots(name: &str) -> (PathBuf, PathBuf, AuthorizedWorkspace) { - let base = std::env::temp_dir().join(format!("photocraft-workspace-{}-{name}", std::process::id())); - let inside = base.join("inside"); - let outside = base.join("outside"); - let _ = std::fs::remove_dir_all(&base); - std::fs::create_dir_all(&inside).unwrap(); - std::fs::create_dir_all(&outside).unwrap(); - let workspace = AuthorizedWorkspace::new(Some(&inside), Some(&inside)).unwrap(); - (inside, outside, workspace) - } - - #[test] - fn export_cleanup_owns_only_created_files_even_during_unwind() { - let (inside, _, workspace) = roots("export-cleanup"); - std::fs::create_dir(inside.join("frames")).unwrap(); - std::fs::write(inside.join("frames/clip_0009.png"), b"old").unwrap(); - let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - let mut export = workspace.export_sequence("frames").unwrap(); - export.write("clip_0000.png", b"new").unwrap(); - assert!(export.write("clip_0009.png", b"overwrite").is_err()); - assert!(export.write("../escape.png", b"escape").is_err()); - panic!("rendering panic"); - })); - assert!(panic.is_err()); - assert_eq!(std::fs::read_dir(inside.join("frames")).unwrap().count(), 1); - assert_eq!(std::fs::read(inside.join("frames/clip_0009.png")).unwrap(), b"old"); - assert!(AuthorizedWorkspace::default().export_sequence("frames").is_err()); - } - - #[cfg(unix)] - #[test] - fn export_directory_symlinks_cannot_escape_the_granted_root() { - let (inside, outside, workspace) = roots("export-symlink"); - std::os::unix::fs::symlink(&outside, inside.join("escape")).unwrap(); - assert!(workspace.export_sequence("escape").is_err()); - assert_eq!(std::fs::read_dir(outside).unwrap().count(), 0); - } - - #[test] - fn valid_read_and_nonexistent_output_write_stay_in_root() { - let (inside, _, workspace) = roots("valid"); - std::fs::write(inside.join("read.txt"), b"canary").unwrap(); - assert_eq!(workspace.read("read.txt").unwrap(), b"canary"); - workspace.write("new-output.txt", b"created").unwrap(); - assert_eq!(std::fs::read(inside.join("new-output.txt")).unwrap(), b"created"); - } - - #[test] - fn large_observed_length_does_not_trigger_a_fixed_limit_or_upfront_reservation() { - let mut shortened = std::io::Cursor::new(b"psb"); - assert_eq!(read_to_observed_length(&mut shortened, (1 << 30) + 1).unwrap(), b"psb"); - } - - #[test] - fn read_growth_is_rejected_at_the_observed_length() { - let mut growing = std::io::repeat(b'x').take(65); - let error = read_to_observed_length(&mut growing, 64).unwrap_err().to_string(); - assert!(error.contains("grew beyond its observed length of 64 bytes"), "{error}"); - let mut allowed = std::io::repeat(b'x').take(64); - assert_eq!(read_to_observed_length(&mut allowed, 64).unwrap().len(), 64); - let mut empty = std::io::Cursor::new(b"x"); - assert!(read_to_observed_length(&mut empty, 0).is_err()); - } - - #[test] - fn open_pcraft_loads_a_directory_below_the_read_root() { - let (inside, _, workspace) = roots("pcraft-dir"); - let doc = photocraft_doc::Document::new("Native", photocraft_doc::Size::new(16, 12), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8); - photocraft_format::PcraftWriter::new().save_dir(&doc, &inside.join("native.pcraft"), &photocraft_format::SaveOptions::default()).unwrap(); - let loaded = workspace.open_pcraft("native.pcraft").unwrap().expect("the directory form loads"); - assert_eq!(loaded.name, doc.name); - assert_eq!((loaded.size.width, loaded.size.height), (16, 12)); - } - - #[test] - fn open_pcraft_reports_zip_files_and_missing_paths_clearly() { - let (inside, _, workspace) = roots("pcraft-errors"); - let doc = photocraft_doc::Document::new("Zipped", photocraft_doc::Size::new(4, 4), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8); - let zip = inside.join("zipped.pcraft"); - photocraft_format::PcraftWriter::new().save_path(&doc, &zip, &photocraft_format::SaveOptions::default()).unwrap(); - assert!(zip.is_file()); - assert!(workspace.open_pcraft("zipped.pcraft").unwrap().is_none(), "the zip form falls back to the bytes path"); - let error = workspace.open_pcraft("missing.pcraft").unwrap_err().to_string(); - assert!(error.contains("failed"), "a missing path propagates its io error: {error}"); - assert!(AuthorizedWorkspace::default().open_pcraft("x.pcraft").is_err(), "no read authority fails closed"); - } - - #[cfg(unix)] - #[test] - fn open_pcraft_rejects_non_regular_entries_and_escaping_symlinks() { - let (inside, _, workspace) = roots("pcraft-nonregular"); - let bundle = inside.join("native.pcraft"); - std::fs::create_dir_all(bundle.join("manifest.json")).unwrap(); - let error = workspace.open_pcraft("native.pcraft").unwrap_err().to_string(); - assert!(error.contains("not a regular file"), "a directory entry is refused, not streamed: {error}"); - - let (inside2, _, workspace2) = roots("pcraft-symlink"); - let bundle2 = inside2.join("native.pcraft"); - std::fs::create_dir(&bundle2).unwrap(); - std::os::unix::fs::symlink("/dev/zero", bundle2.join("manifest.json")).unwrap(); - assert!(workspace2.open_pcraft("native.pcraft").is_err(), "an escaping symlink entry is refused"); - } - - fn temp_files(dir: &Path) -> Vec { - std::fs::read_dir(dir).unwrap().flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".tmp")).collect() - } - - #[test] - fn write_replaces_atomically_and_leaves_no_temp_file() { - let (inside, _, workspace) = roots("atomic"); - std::fs::create_dir_all(inside.join("sub")).unwrap(); - workspace.write("sub/out.psd", b"original").unwrap(); - workspace.write("sub/out.psd", b"replacement").unwrap(); - assert_eq!(std::fs::read(inside.join("sub/out.psd")).unwrap(), b"replacement"); - assert!(temp_files(&inside.join("sub")).is_empty()); - // Renaming over a directory fails: nothing is left behind and the directory survives. - std::fs::create_dir_all(inside.join("adir")).unwrap(); - assert!(workspace.write("adir", b"x").is_err()); - assert!(inside.join("adir").is_dir()); - assert!(temp_files(&inside).is_empty()); - } - - #[cfg(unix)] - #[test] - fn write_into_read_only_folder_keeps_the_original() { - use std::os::unix::fs::PermissionsExt; - let (inside, _, workspace) = roots("readonly"); - let ro = inside.join("ro"); - std::fs::create_dir_all(&ro).unwrap(); - std::fs::write(ro.join("doc.psd"), b"original").unwrap(); - std::fs::set_permissions(&ro, std::fs::Permissions::from_mode(0o555)).unwrap(); - let root_user = std::fs::File::create(ro.join("probe")).is_ok(); - if !root_user { - assert!(workspace.write("ro/doc.psd", b"new").is_err()); - assert_eq!(std::fs::read(ro.join("doc.psd")).unwrap(), b"original"); - assert!(temp_files(&ro).is_empty()); - } - std::fs::set_permissions(&ro, std::fs::Permissions::from_mode(0o755)).unwrap(); - } - - #[test] - fn rejects_absolute_traversal_mixed_prefix_and_malformed_paths_without_panicking() { - let (_, _, workspace) = roots("reject"); - for path in [ - "", - "/absolute", - "../escape", - "a/../escape", - "a\\..\\escape", - "a\\b", - "C:/escape", - "a//b", - "./a", - "name:stream", - "NUL", - "con.txt", - "folder/COM1.log", - ] { - assert!(workspace.read(path).is_err(), "read accepted {path:?}"); - assert!(workspace.write(path, b"x").is_err(), "write accepted {path:?}"); - } - } - - #[test] - fn read_and_write_authority_are_separate() { - let (inside, _, _) = roots("separate"); - std::fs::write(inside.join("read.txt"), b"canary").unwrap(); - let read_only = AuthorizedWorkspace::new(Some(&inside), None).unwrap(); - assert_eq!(read_only.read("read.txt").unwrap(), b"canary"); - assert!(read_only.write("blocked.txt", b"x").is_err()); - let write_only = AuthorizedWorkspace::new(None, Some(&inside)).unwrap(); - assert!(write_only.read("read.txt").is_err()); - write_only.write("written.txt", b"ok").unwrap(); - } - - #[test] - fn missing_parent_is_a_stable_error_before_any_file_is_created() { - let (inside, _, workspace) = roots("missing-parent"); - let error = workspace.write("missing/output.txt", b"x").unwrap_err().to_string(); - assert!(error.contains("automation write"), "{error}"); - assert!(!inside.join("missing").exists()); - } - - #[cfg(unix)] - #[test] - fn symlink_escape_is_rejected() { - use std::os::unix::fs::symlink; - - let (inside, outside, workspace) = roots("symlink"); - std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); - symlink(&outside, inside.join("link")).unwrap(); - assert!(workspace.read("link/secret.txt").is_err()); - assert!(workspace.write("link/new.txt", b"blocked").is_err()); - assert!(!outside.join("new.txt").exists()); - } - - #[cfg(windows)] - #[test] - fn windows_symlink_escape_is_rejected_when_supported() { - use std::os::windows::fs::symlink_dir; - - let (inside, outside, workspace) = roots("windows-link"); - std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); - if symlink_dir(&outside, inside.join("link")).is_err() { - return; - } - assert!(workspace.read("link/secret.txt").is_err()); - assert!(workspace.write("link/new.txt", b"blocked").is_err()); - assert!(!outside.join("new.txt").exists()); - } - - #[cfg(windows)] - #[test] - fn windows_junction_escape_is_rejected_when_supported() { - use std::process::Command; - - let (inside, outside, workspace) = roots("windows-junction"); - std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); - let link = inside.join("junction"); - let status = Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(&link).arg(&outside).status(); - if !status.is_ok_and(|status| status.success()) { - return; - } - assert!(workspace.read("junction/secret.txt").is_err()); - assert!(workspace.write("junction/new.txt", b"blocked").is_err()); - assert!(!outside.join("new.txt").exists()); - let _ = std::fs::remove_dir(link); - } - - #[test] - fn filesystem_commands_fail_closed() { - for id in [ - "file.open", - "file.openAs", - "file.save", - "file.saveAs", - "file.saveACopy", - "file.export.saveForWebLegacy", - "pattern.import", - "layer.smartObjects.exportContents", - "layer.smartObjects.replaceContents", - "layer.smartObjects.relinkToFile", - "layer.smartObjects.convertToLinked", - "measurementLog.export", - "layer.videoLayers.reloadFrame", - "edit.colorSettings", - ] { - assert!(authorize_engine_command(id, &serde_json::json!({})).is_err()); - } - assert!(authorize_engine_command("file.new", &serde_json::json!({})).is_ok()); - assert!(authorize_engine_command("file.automate.cropAndStraightenPhotos", &serde_json::json!({})).is_ok()); - assert!(authorize_desktop_engine_command("file.automate.cropAndStraightenPhotos", &serde_json::json!({})).is_ok()); - for id in ["layer.smartObjects.editContents", "layer.smartObjects.convertToLayers", "layer.smartObjects.saveContents"] { - assert!(authorize_desktop_engine_command(id, &serde_json::json!({"layer": 19})).is_ok(), "{id}"); - } - for id in ["layer.smartObjects.editContents", "layer.smartObjects.convertToLayers"] { - assert!(authorize_engine_command(id, &serde_json::json!({"path": "/outside/source.psb"})).is_err(), "{id}"); - } - // The UI-level examples in docs/control-protocol.md. - for id in ["view.zoomIn", "window.theme.pro", "edit.search"] { - assert!(authorize_desktop_engine_command(id, &serde_json::json!({})).is_ok(), "{id}"); - } - assert!(authorize_engine_command("image.mode.cmyk", &serde_json::json!({})).is_ok()); - assert!(authorize_desktop_engine_command("image.mode.cmyk", &serde_json::json!({})).is_err()); - assert!(authorize_engine_command("image.mode.rgb", &serde_json::json!({"profile": "/outside/profile.icc"})).is_err()); - assert!(authorize_engine_command("image.mode.rgb", &serde_json::json!({"profile": "srgb"})).is_ok()); - // Shortcut sets: text in and out is fine; a path reads or writes the filesystem. - let ks = |p: serde_json::Value| authorize_engine_command("edit.keyboardShortcuts", &p); - assert!(ks(serde_json::json!({"exportKys": true, "summarize": true, "importKys": ""})).is_ok()); - for key in ["importKys", "exportKys", "summarize"] { - assert!(ks(serde_json::json!({ key: {"path": "/outside/set.kys"} })).is_err(), "{key}"); - } - assert!(authorize_engine_command("filter.distort.displace", &serde_json::json!({"mapPath": "outside.png"})).is_err()); - assert!(authorize_engine_command("layer.setAdjustment", &serde_json::json!({"file": "outside.cube"})).is_err()); - assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "colorSettings.workingRgb", "value": "outside.icc"})).is_err()); - assert!(authorize_engine_step("file.open", &serde_json::json!({})).is_err()); - assert!(authorize_engine_step("actions.play", &serde_json::json!({})).is_ok()); - // An allowed command can't reach a denied one by running it on its own behalf. - let mut session = photocraft_engine::Session::new(); - session.execute("file.new", serde_json::json!({"width": 4, "height": 4})).unwrap(); - session.authorize = Some(authorize_desktop_engine_step); - let params = serde_json::json!({"to": "grayscale"}); - assert!(authorize_desktop_engine_command("file.automate.conditionalModeChange", ¶ms).is_ok()); - assert!(session.execute("file.automate.conditionalModeChange", params).is_err()); - assert_eq!(session.active().unwrap().doc.mode, photocraft_engine::doc::ColorMode::Rgb); - assert!(authorize_desktop_engine_step("file.saveACopy", &serde_json::json!({})).is_err()); - } - - #[test] - fn apply_data_set_is_judged_by_the_values_it_applies() { - let mut headless = crate::Headless::new(); - headless.command_run("file.new", serde_json::json!({"width": 8, "height": 8})).unwrap(); - let layer = headless.command_run("layer.new.layer", serde_json::json!({})).unwrap()["layer"].clone(); - let defs = [ - serde_json::json!({"name": "shown", "layer": layer, "type": "visibility"}), - serde_json::json!({"name": "photo", "layer": layer, "type": "pixelReplacement"}), - ]; - headless.command_run("image.variables.define", serde_json::json!({"defs": defs})).unwrap(); - let sets = serde_json::json!({"dataSets": [ - {"name": "hidden", "values": [{"variable": "shown", "kind": "visibility", "value": false}]}, - {"name": "swap", "values": [ - {"variable": "shown", "kind": "visibility", "value": false}, - {"variable": "photo", "kind": "pixels", "value": "/outside/photo.png"}, - ]}, - ]}); - headless.command_run("image.variables.dataSets", sets).unwrap(); - let visible = |headless: &crate::Headless| headless.session.active().unwrap().doc.layers.iter().all(|l| l.visible); - let refused = headless.command_run("image.applyDataSet", serde_json::json!({"name": "swap"})).unwrap_err(); - assert!(refused.to_string().contains("ambient filesystem paths"), "{refused}"); - assert!(visible(&headless), "a refused data set applies none of its values"); - assert!(headless.command_run("image.applyDataSet", serde_json::json!({"name": "hidden"})).is_ok()); - assert!(!visible(&headless)); - } - - #[test] - fn automation_rejects_ambient_path_commands_and_preferences() { - for (id, params) in [ - ("brush.presets.importAbr", serde_json::json!({"path": "/outside/set.abr"})), - ("brush.presets.importImage", serde_json::json!({"path": "/outside/tip.png"})), - ("gradient.presets.importGrd", serde_json::json!({"path": "/outside/set.grd"})), - ("plugin.install", serde_json::json!({"path": "/outside/plugin.wasm"})), - ("plugin.reload", serde_json::json!({"path": "/outside/plugins"})), - ("plugin.reload", serde_json::json!({})), - ("plugin.install", serde_json::json!({"path": " "})), - ("swatches.import", serde_json::json!({"path": "/outside/set.aco"})), - ("swatches.export", serde_json::json!({"path": "/outside/set.ase"})), - ("actions.load", serde_json::json!({"path": "/outside/set.atn"})), - ("actions.save", serde_json::json!({"path": "/outside/set.atn"})), - ] { - assert!(authorize_engine_command(id, ¶ms).is_err(), "{id}: {params}"); - } - for (id, params) in [ - ("brush.presets.importAbr", serde_json::json!({"data": "QUJD"})), - ("brush.presets.importImage", serde_json::json!({"data": "QUJD"})), - ("gradient.presets.importGrd", serde_json::json!({"data": "QUJD"})), - ("plugin.install", serde_json::json!({"data": "QUJD"})), - ("swatches.import", serde_json::json!({"data": "QUJD"})), - ("swatches.export", serde_json::json!({"format": "ase"})), - ("actions.load", serde_json::json!({"data": "QUJD"})), - ("actions.save", serde_json::json!({"set": "S"})), - ] { - assert!(authorize_engine_command(id, ¶ms).is_ok(), "{id}: {params}"); - } - for path in [ - "", - "colorSettings", - "colorSettings.workingRgb", - "scriptEvents", - "scriptEvents.enabled", - ".scriptEvents", - ".scriptEvents.enabled", - "historyLog.filePath", - ".historyLog.filePath", - "plugIns.additionalPluginsFolder", - "scratchDisks.disks", - ".", - "..historyLog.filePath", - ] { - assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": path, "value": {}})).is_err(), "{path}"); - } - assert!( - authorize_engine_command("prefs.set", &serde_json::json!({"values": {"interface.language": "fr", "historyLog.filePath": "/outside/log"}})).is_err() - ); - assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "interface.language", "value": "fr"})).is_ok()); - } - - #[test] - fn registry_filesystem_path_params_are_classified() { - let probe = serde_json::json!({ - "path": "/outside/photocraft-probe", - "file": "/outside/photocraft-probe.icc", - "mapPath": "/outside/photocraft-probe.png", - "profile": "/outside/photocraft-probe.icc", - "workingRgb": "/outside/photocraft-probe.icc", - "workingCmyk": "/outside/photocraft-probe.icc", - "workingGray": "/outside/photocraft-probe.icc", - "input": "/outside/photocraft-input", - "output": "/outside/photocraft-output", - "paths": ["/outside/photocraft-probe.psd"], - }); - let preference_probe = serde_json::json!({"path": ".scriptEvents", "value": {}}); - let unclassified: Vec<_> = photocraft_engine::command_specs() - .iter() - .filter(|spec| documents_filesystem_path_params(spec.id, spec.params)) - .filter(|spec| { - let params = if spec.id == "prefs.set" { &preference_probe } else { &probe }; - authorize_engine_command(spec.id, params).is_ok() - }) - .map(|spec| spec.id) - .collect(); - assert!(unclassified.is_empty(), "filesystem path parameters in the command registry need an automation policy: {unclassified:?}"); - } - - fn documents_filesystem_path_params(id: &str, params: &str) -> bool { - if matches!(id, "prefs.get" | "prefs.reset") { - return false; - } - // These registry descriptions refer to document vector paths, not host files. - if matches!( - id, - "filter.blurGallery.pathBlur" - | "filter.render.flame" - | "shape.create" - | "shape.edit" - | "shape.info" - | "shape.presets.list" - | "shape.presets.new" - | "path.list" - | "path.info" - | "path.set" - | "path.transform" - | "path.moveAnchors" - | "path.moveHandle" - | "path.bendSegment" - | "path.convertPoint" - | "path.addAnchor" - | "path.deleteAnchor" - | "path.clippingPath.set" - | "path.rename" - | "path.stroke" - | "type.attachToPath" - | "type.detachFromPath" - | "select.toWorkPath" - | "layer.vectorMask.add" - | "layer.vectorMask.edit" - | "layer.vectorMask.info" - | "paint.symmetryFromPath" - | "edit.defineCustomShape" - | "layer.combineShapes.unite" - | "layer.combineShapes.subtractFrontShape" - | "layer.combineShapes.intersectShapeAreas" - | "layer.combineShapes.excludeOverlappingShapes" - | "layer.combineShapes.mergeShapeComponents" - ) || id.starts_with("layer.newFillLayer.") - || id.starts_with("layer.newAdjustmentLayer.") - { - // New fill and adjustment layers take a vector path as their vector mask (#1419). - return false; - } - params.to_ascii_lowercase().contains("path") - } - - #[test] - fn whole_preferences_update_cannot_enable_automation_script_events() { - let mut headless = crate::headless::Headless::new(); - let result = headless.command_run( - "prefs.set", - serde_json::json!({ - "path": "", - "value": { - "scriptEvents": { - "enabled": true, - "bindings": [{ - "event": "newDocument", - "steps": [["file.saveACopy", {"path": "/outside/canary.psd"}]] - }] - } - } - }), - ); - assert!(result.is_err()); - - for (id, params) in [ - ("brush.presets.importAbr", serde_json::json!({"path": "/outside/set.abr"})), - ("gradient.presets.importGrd", serde_json::json!({"path": "/outside/set.grd"})), - ("plugin.install", serde_json::json!({"path": "/outside/plugin.wasm"})), - ("plugin.reload", serde_json::json!({"path": "/outside/plugins"})), - ("prefs.set", serde_json::json!({"path": "historyLog.filePath", "value": "/outside/log"})), - ("prefs.set", serde_json::json!({"values": {"interface.language": "fr", "historyLog.filePath": "/outside/log"}})), - ] { - assert!(headless.command_run(id, params).is_err(), "{id}"); - } - headless.command_run("file.new", serde_json::json!({"width": 5, "height": 5})).unwrap(); - assert!(headless.session.file_menu.event_log.is_empty()); - } - - /// #2176: an absolute path beneath the root names the same file as its relative path; every - /// other absolute path is refused before any file effect. - #[test] - fn absolute_paths_beneath_the_root_are_translated_and_others_refused() { - let (inside, outside, workspace) = roots("absolute-paths"); - let abs = |p: &Path| p.to_string_lossy().into_owned(); - - workspace.write(&abs(&inside.join("a.bin")), b"one").unwrap(); - assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); - assert_eq!(workspace.read(&abs(&inside.join("a.bin"))).unwrap(), b"one"); - // Forward slashes too (on Windows the native spelling uses `\`). - assert_eq!(workspace.read(&abs(&inside.join("a.bin")).replace('\\', "/")).unwrap(), b"one"); - std::fs::create_dir(inside.join("sub")).unwrap(); - workspace.write(&abs(&inside.join("sub").join("b.bin")), b"two").unwrap(); - assert_eq!(std::fs::read(inside.join("sub/b.bin")).unwrap(), b"two"); - assert_eq!(workspace.read("sub/b.bin").unwrap(), b"two", "relative paths still work"); - - let refused = |path: String| { - let error = workspace.write(&path, b"x").unwrap_err().to_string(); - assert!(error.contains("automation path rejected"), "{path}: {error}"); - }; - refused(abs(&outside.join("escape.bin"))); - refused(abs(&inside.join("..").join("outside").join("traversal.bin"))); - refused(abs(&inside)); - refused(abs(&inside.join("CON"))); - refused(format!("{}:hidden", abs(&inside.join("a.bin")))); - // A sibling whose name merely starts with the root's name is not beneath it. - let sibling = inside.with_file_name("inside-sibling"); - std::fs::create_dir_all(&sibling).unwrap(); - refused(abs(&sibling.join("lookalike.bin"))); - refused(format!("{}-sibling/lookalike.bin", abs(&inside))); - // Climbing back out through a subdirectory is refused too. - refused(abs(&inside.join("sub").join("..").join("..").join("outside").join("climb.bin"))); - assert!(!sibling.join("lookalike.bin").exists()); - assert!(!outside.join("climb.bin").exists()); - assert!(!outside.join("escape.bin").exists()); - assert!(!outside.join("traversal.bin").exists()); - assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); - } - - /// Each authority is checked against its own root, and none granted refuses absolute paths. - #[test] - fn absolute_paths_are_checked_against_the_authority_s_own_root() { - let (inside, outside, _) = roots("absolute-authority"); - let workspace = AuthorizedWorkspace::new(Some(&outside), Some(&inside)).unwrap(); - let target = inside.join("written.bin"); - workspace.write(&target.to_string_lossy(), b"w").unwrap(); - assert!(workspace.read(&target.to_string_lossy()).is_err(), "not under the read root"); - let none = AuthorizedWorkspace::default(); - assert!(none.read(&target.to_string_lossy()).is_err()); - } -} +mod tests; diff --git a/crates/automation/src/workspace/tests.rs b/crates/automation/src/workspace/tests.rs new file mode 100644 index 0000000000..4cafd976f9 --- /dev/null +++ b/crates/automation/src/workspace/tests.rs @@ -0,0 +1,880 @@ +use super::*; + +fn roots(name: &str) -> (PathBuf, PathBuf, AuthorizedWorkspace) { + let base = std::env::temp_dir().join(format!("photocraft-workspace-{}-{name}", std::process::id())); + let inside = base.join("inside"); + let outside = base.join("outside"); + let _ = std::fs::remove_dir_all(&base); + std::fs::create_dir_all(&inside).unwrap(); + std::fs::create_dir_all(&outside).unwrap(); + let workspace = AuthorizedWorkspace::new(Some(&inside), Some(&inside)).unwrap(); + (inside, outside, workspace) +} + +#[test] +fn export_cleanup_owns_only_created_files_even_during_unwind() { + let (inside, _, workspace) = roots("export-cleanup"); + std::fs::create_dir(inside.join("frames")).unwrap(); + std::fs::write(inside.join("frames/clip_0009.png"), b"old").unwrap(); + let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let mut export = workspace.export_sequence("frames").unwrap(); + export.write("clip_0000.png", b"new").unwrap(); + assert!(export.write("clip_0009.png", b"overwrite").is_err()); + assert!(export.write("../escape.png", b"escape").is_err()); + panic!("rendering panic"); + })); + assert!(panic.is_err()); + assert_eq!(std::fs::read_dir(inside.join("frames")).unwrap().count(), 1); + assert_eq!(std::fs::read(inside.join("frames/clip_0009.png")).unwrap(), b"old"); + assert!(AuthorizedWorkspace::default().export_sequence("frames").is_err()); +} + +#[cfg(unix)] +#[test] +fn export_directory_symlinks_cannot_escape_the_granted_root() { + let (inside, outside, workspace) = roots("export-symlink"); + std::os::unix::fs::symlink(&outside, inside.join("escape")).unwrap(); + assert!(workspace.export_sequence("escape").is_err()); + assert_eq!(std::fs::read_dir(outside).unwrap().count(), 0); +} + +#[test] +fn valid_read_and_nonexistent_output_write_stay_in_root() { + let (inside, _, workspace) = roots("valid"); + std::fs::write(inside.join("read.txt"), b"canary").unwrap(); + assert_eq!(workspace.read("read.txt").unwrap(), b"canary"); + workspace.write("new-output.txt", b"created").unwrap(); + assert_eq!(std::fs::read(inside.join("new-output.txt")).unwrap(), b"created"); +} + +#[test] +fn large_observed_length_does_not_trigger_a_fixed_limit_or_upfront_reservation() { + let mut shortened = std::io::Cursor::new(b"psb"); + assert_eq!(read_to_observed_length(&mut shortened, (1 << 30) + 1).unwrap(), b"psb"); +} + +#[test] +fn read_growth_is_rejected_at_the_observed_length() { + let mut growing = std::io::repeat(b'x').take(65); + let error = read_to_observed_length(&mut growing, 64).unwrap_err().to_string(); + assert!(error.contains("grew beyond its observed length of 64 bytes"), "{error}"); + let mut allowed = std::io::repeat(b'x').take(64); + assert_eq!(read_to_observed_length(&mut allowed, 64).unwrap().len(), 64); + let mut empty = std::io::Cursor::new(b"x"); + assert!(read_to_observed_length(&mut empty, 0).is_err()); +} + +#[test] +fn open_pcraft_loads_a_directory_below_the_read_root() { + let (inside, _, workspace) = roots("pcraft-dir"); + let doc = photocraft_doc::Document::new("Native", photocraft_doc::Size::new(16, 12), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8); + photocraft_format::PcraftWriter::new().save_dir(&doc, &inside.join("native.pcraft"), &photocraft_format::SaveOptions::default()).unwrap(); + let loaded = workspace.open_pcraft("native.pcraft").unwrap().expect("the directory form loads"); + assert_eq!(loaded.name, doc.name); + assert_eq!((loaded.size.width, loaded.size.height), (16, 12)); +} + +#[test] +fn open_pcraft_reports_zip_files_and_missing_paths_clearly() { + let (inside, _, workspace) = roots("pcraft-errors"); + let doc = photocraft_doc::Document::new("Zipped", photocraft_doc::Size::new(4, 4), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8); + let zip = inside.join("zipped.pcraft"); + photocraft_format::PcraftWriter::new().save_path(&doc, &zip, &photocraft_format::SaveOptions::default()).unwrap(); + assert!(zip.is_file()); + assert!(workspace.open_pcraft("zipped.pcraft").unwrap().is_none(), "the zip form falls back to the bytes path"); + let error = workspace.open_pcraft("missing.pcraft").unwrap_err().to_string(); + assert!(error.contains("failed"), "a missing path propagates its io error: {error}"); + assert!(AuthorizedWorkspace::default().open_pcraft("x.pcraft").is_err(), "no read authority fails closed"); +} + +#[cfg(unix)] +#[test] +fn open_pcraft_rejects_non_regular_entries_and_escaping_symlinks() { + let (inside, _, workspace) = roots("pcraft-nonregular"); + let bundle = inside.join("native.pcraft"); + std::fs::create_dir_all(bundle.join("manifest.json")).unwrap(); + let error = workspace.open_pcraft("native.pcraft").unwrap_err().to_string(); + assert!(error.contains("not a regular file"), "a directory entry is refused, not streamed: {error}"); + + let (inside2, _, workspace2) = roots("pcraft-symlink"); + let bundle2 = inside2.join("native.pcraft"); + std::fs::create_dir(&bundle2).unwrap(); + std::os::unix::fs::symlink("/dev/zero", bundle2.join("manifest.json")).unwrap(); + assert!(workspace2.open_pcraft("native.pcraft").is_err(), "an escaping symlink entry is refused"); +} + +fn temp_files(dir: &Path) -> Vec { + std::fs::read_dir(dir).unwrap().flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".tmp")).collect() +} + +#[test] +fn write_replaces_atomically_and_leaves_no_temp_file() { + let (inside, _, workspace) = roots("atomic"); + std::fs::create_dir_all(inside.join("sub")).unwrap(); + workspace.write("sub/out.psd", b"original").unwrap(); + workspace.write("sub/out.psd", b"replacement").unwrap(); + assert_eq!(std::fs::read(inside.join("sub/out.psd")).unwrap(), b"replacement"); + assert!(temp_files(&inside.join("sub")).is_empty()); + // Renaming over a directory fails: nothing is left behind and the directory survives. + std::fs::create_dir_all(inside.join("adir")).unwrap(); + assert!(workspace.write("adir", b"x").is_err()); + assert!(inside.join("adir").is_dir()); + assert!(temp_files(&inside).is_empty()); +} + +/// A directory at the target is refused before anything is opened, even under an image name. +#[test] +fn write_refuses_a_directory_target_before_opening_it() { + let (inside, _, workspace) = roots("dir-target"); + for name in ["adir", "adir.png"] { + std::fs::create_dir_all(inside.join(name)).unwrap(); + let error = workspace.write(name, b"x").unwrap_err().to_string(); + assert!(error.contains("is not a regular file"), "{name}: {error}"); + assert!(inside.join(name).is_dir()); + } + assert!(temp_files(&inside).is_empty()); +} + +/// A FIFO at the target is refused at once: opening it to sniff its head or copy its +/// attributes would block until a writer appears, stalling every other request. +#[cfg(unix)] +#[test] +fn write_refuses_a_fifo_target_without_blocking() { + let (inside, _, workspace) = roots("fifo-target"); + for name in ["pipe", "out.png"] { + let made = std::process::Command::new("mkfifo").arg(inside.join(name)).status(); + if !made.is_ok_and(|s| s.success()) { + return; // no mkfifo on this system: nothing to assert + } + } + std::os::unix::fs::symlink("pipe", inside.join("link")).unwrap(); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + for name in ["pipe", "out.png", "link"] { + let _ = tx.send((name, workspace.write(name, b"x").map_err(|e| e.to_string()))); + } + }); + for _ in 0..3 { + let (name, result) = rx.recv_timeout(std::time::Duration::from_secs(5)).expect("write blocked on a FIFO"); + let error = result.unwrap_err(); + assert!(error.contains("is not a regular file"), "{name}: {error}"); + } + assert!(temp_files(&inside).is_empty()); +} + +/// #9401 review: replacing a file through the automation path keeps its creation time, the +/// same as a save through `atomic_write` does on macOS (#9388). +#[cfg(target_os = "macos")] +#[test] +fn write_keeps_the_replaced_files_creation_time() { + use std::os::macos::fs::FileTimesExt; + let (inside, _, workspace) = roots("created"); + let path = inside.join("doc.psd"); + workspace.write("doc.psd", b"original").unwrap(); + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&path).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + workspace.write("doc.psd", b"replacement").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"replacement"); + assert_eq!(std::fs::metadata(&path).unwrap().created().unwrap(), born, "the creation time survives the automation write"); +} + +/// #7095: replacing a file through the automation path keeps the old file's extended +/// attributes, the same as a save through `atomic_write` does. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +#[test] +fn write_keeps_the_replaced_files_extended_attributes() { + #[cfg(target_os = "macos")] + const TAG: &str = "com.apple.metadata:_kMDItemUserTags"; + #[cfg(not(target_os = "macos"))] + const TAG: &str = "user.photocraft.test"; + + let (inside, _, workspace) = roots("xattr"); + let path = inside.join("doc.psd"); + workspace.write("doc.psd", b"original").unwrap(); + if rustix::fs::setxattr(&path, TAG, b"tagged", rustix::fs::XattrFlags::empty()).is_err() { + return; // a file system without extended attributes: nothing to assert + } + + workspace.write("doc.psd", b"replacement, longer").unwrap(); + + assert_eq!(std::fs::read(&path).unwrap(), b"replacement, longer"); + let mut buf = [0u8; 64]; + let len = rustix::fs::getxattr(&path, TAG, buf.as_mut_slice()).expect("the attribute was dropped"); + assert_eq!(&buf[..len], b"tagged"); + assert!(temp_files(&inside).is_empty()); +} + +#[cfg(unix)] +#[test] +fn write_into_read_only_folder_keeps_the_original() { + use std::os::unix::fs::PermissionsExt; + let (inside, _, workspace) = roots("readonly"); + let ro = inside.join("ro"); + std::fs::create_dir_all(&ro).unwrap(); + std::fs::write(ro.join("doc.psd"), b"original").unwrap(); + std::fs::set_permissions(&ro, std::fs::Permissions::from_mode(0o555)).unwrap(); + let root_user = std::fs::File::create(ro.join("probe")).is_ok(); + if !root_user { + assert!(workspace.write("ro/doc.psd", b"new").is_err()); + assert_eq!(std::fs::read(ro.join("doc.psd")).unwrap(), b"original"); + assert!(temp_files(&ro).is_empty()); + } + std::fs::set_permissions(&ro, std::fs::Permissions::from_mode(0o755)).unwrap(); +} + +#[test] +fn rejects_absolute_traversal_mixed_prefix_and_malformed_paths_without_panicking() { + let (_, _, workspace) = roots("reject"); + for path in + ["", "/absolute", "../escape", "a/../escape", "a\\..\\escape", "a\\b", "C:/escape", "a//b", "./a", "name:stream", "NUL", "con.txt", "folder/COM1.log"] + { + assert!(workspace.read(path).is_err(), "read accepted {path:?}"); + assert!(workspace.write(path, b"x").is_err(), "write accepted {path:?}"); + } +} + +#[test] +fn every_device_name_cap_std_refuses_to_open_is_refused_before_any_write() { + // cap-std refuses these when it opens a file on Windows, so a file written under one of + // them could not be read back (#9424). + let (inside, _, workspace) = roots("devices"); + std::fs::create_dir(inside.join("sub")).unwrap(); + for path in ["COM0.txt", "LPT0", "COM¹.png", "com².png", "COM³", "LPT¹.txt", "LPT².txt", "lpt³", "NUL .png", "Aux .txt", "sub/CON .psd", "COM¹/frame.png"] + { + let error = workspace.write(path, b"x").unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "write {path:?}: {error}"); + let error = workspace.read(path).unwrap_err().to_string(); + assert!(error.contains("reserved device names"), "read {path:?}: {error}"); + } + for dir in ["COM¹", "LPT0", "sub/PRN .x"] { + let error = workspace.export_sequence(dir).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(error.contains("reserved device names"), "export {dir:?}: {error}"); + } + assert_eq!(std::fs::read_dir(&inside).unwrap().count(), 1, "only `sub` may exist"); + assert_eq!(std::fs::read_dir(inside.join("sub")).unwrap().count(), 0); + // Look-alikes are ordinary names. + for path in ["COM10.txt", "COM¹0.txt", "LPT00", "console.txt", "NULL.txt", "nul_.txt", " NUL.txt"] { + workspace.write(path, b"x").unwrap_or_else(|e| panic!("write {path:?}: {e}")); + assert_eq!(workspace.read(path).unwrap(), b"x", "{path:?}"); + } +} + +#[test] +fn read_and_write_authority_are_separate() { + let (inside, _, _) = roots("separate"); + std::fs::write(inside.join("read.txt"), b"canary").unwrap(); + let read_only = AuthorizedWorkspace::new(Some(&inside), None).unwrap(); + assert_eq!(read_only.read("read.txt").unwrap(), b"canary"); + assert!(read_only.write("blocked.txt", b"x").is_err()); + let write_only = AuthorizedWorkspace::new(None, Some(&inside)).unwrap(); + assert!(write_only.read("read.txt").is_err()); + write_only.write("written.txt", b"ok").unwrap(); +} + +#[test] +fn missing_parent_is_a_stable_error_before_any_file_is_created() { + let (inside, _, workspace) = roots("missing-parent"); + let error = workspace.write("missing/output.txt", b"x").unwrap_err().to_string(); + assert!(error.contains("automation write"), "{error}"); + assert!(!inside.join("missing").exists()); +} + +#[cfg(unix)] +#[test] +fn symlink_escape_is_rejected() { + use std::os::unix::fs::symlink; + + let (inside, outside, workspace) = roots("symlink"); + std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); + symlink(&outside, inside.join("link")).unwrap(); + assert!(workspace.read("link/secret.txt").is_err()); + assert!(workspace.write("link/new.txt", b"blocked").is_err()); + assert!(!outside.join("new.txt").exists()); +} + +#[cfg(windows)] +#[test] +fn windows_symlink_escape_is_rejected_when_supported() { + use std::os::windows::fs::symlink_dir; + + let (inside, outside, workspace) = roots("windows-link"); + std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); + if symlink_dir(&outside, inside.join("link")).is_err() { + return; + } + assert!(workspace.read("link/secret.txt").is_err()); + assert!(workspace.write("link/new.txt", b"blocked").is_err()); + assert!(!outside.join("new.txt").exists()); +} + +#[cfg(windows)] +#[test] +fn windows_junction_escape_is_rejected_when_supported() { + use std::process::Command; + + let (inside, outside, workspace) = roots("windows-junction"); + std::fs::write(outside.join("secret.txt"), b"outside").unwrap(); + let link = inside.join("junction"); + let status = Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(&link).arg(&outside).status(); + if !status.is_ok_and(|status| status.success()) { + return; + } + assert!(workspace.read("junction/secret.txt").is_err()); + assert!(workspace.write("junction/new.txt", b"blocked").is_err()); + assert!(!outside.join("new.txt").exists()); + let _ = std::fs::remove_dir(link); +} + +#[test] +fn filesystem_commands_fail_closed() { + for id in [ + "file.open", + "file.openAs", + "file.save", + "file.saveAs", + "file.saveACopy", + "file.export.saveForWebLegacy", + "pattern.import", + "layer.smartObjects.exportContents", + "layer.smartObjects.replaceContents", + "layer.smartObjects.relinkToFile", + "layer.smartObjects.convertToLinked", + "measurementLog.export", + "layer.videoLayers.reloadFrame", + "edit.colorSettings", + ] { + assert!(authorize_engine_command(id, &serde_json::json!({})).is_err()); + } + assert!(authorize_engine_command("file.new", &serde_json::json!({})).is_ok()); + assert!(authorize_engine_command("file.automate.cropAndStraightenPhotos", &serde_json::json!({})).is_ok()); + assert!(authorize_desktop_engine_command("file.automate.cropAndStraightenPhotos", &serde_json::json!({})).is_ok()); + for id in ["layer.smartObjects.editContents", "layer.smartObjects.convertToLayers", "layer.smartObjects.saveContents"] { + assert!(authorize_desktop_engine_command(id, &serde_json::json!({"layer": 19})).is_ok(), "{id}"); + } + for id in ["layer.smartObjects.editContents", "layer.smartObjects.convertToLayers"] { + assert!(authorize_engine_command(id, &serde_json::json!({"path": "/outside/source.psb"})).is_err(), "{id}"); + } +} + +#[test] +fn filesystem_command_paths_and_profiles_fail_closed() { + // The UI-level examples in docs/control-protocol.md. + for id in ["view.zoomIn", "window.theme.pro", "edit.search"] { + assert!(authorize_desktop_engine_command(id, &serde_json::json!({})).is_ok(), "{id}"); + } + assert!(authorize_engine_command("image.mode.cmyk", &serde_json::json!({})).is_ok()); + assert!(authorize_desktop_engine_command("image.mode.cmyk", &serde_json::json!({})).is_err()); + assert!(authorize_engine_command("image.mode.rgb", &serde_json::json!({"profile": "/outside/profile.icc"})).is_err()); + assert!(authorize_engine_command("image.mode.rgb", &serde_json::json!({"profile": "srgb"})).is_ok()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "/outside/profile.icc"})).is_err()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "adobe-rgb-compat"})).is_ok()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "working"})).is_ok()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "none"})).is_ok()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "default"})).is_err()); + assert!(authorize_engine_command("file.new", &serde_json::json!({"profile": "working-cmyk"})).is_err()); + // Shortcut sets: text in and out is fine; a path reads or writes the filesystem. + let ks = |p: serde_json::Value| authorize_engine_command("edit.keyboardShortcuts", &p); + assert!(ks(serde_json::json!({"exportKys": true, "summarize": true, "importKys": ""})).is_ok()); + for key in ["importKys", "exportKys", "summarize"] { + assert!(ks(serde_json::json!({ key: {"path": "/outside/set.kys"} })).is_err(), "{key}"); + } + assert!(authorize_engine_command("filter.distort.displace", &serde_json::json!({"mapPath": "outside.png"})).is_err()); + assert!(authorize_engine_command("layer.setAdjustment", &serde_json::json!({"file": "outside.cube"})).is_err()); + assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "colorSettings.workingRgb", "value": "outside.icc"})).is_err()); + assert!(authorize_engine_step("file.open", &serde_json::json!({})).is_err()); + assert!(authorize_engine_step("actions.play", &serde_json::json!({})).is_ok()); + // An allowed command can't reach a denied one by running it on its own behalf. + let mut session = photocraft_engine::Session::new(); + session.execute("file.new", serde_json::json!({"width": 4, "height": 4})).unwrap(); + session.authorize = Some(authorize_desktop_engine_step); + let params = serde_json::json!({"to": "grayscale"}); + assert!(authorize_desktop_engine_command("file.automate.conditionalModeChange", ¶ms).is_ok()); + assert!(session.execute("file.automate.conditionalModeChange", params).is_err()); + assert_eq!(session.active().unwrap().doc.mode, photocraft_engine::doc::ColorMode::Rgb); + assert!(authorize_desktop_engine_step("file.saveACopy", &serde_json::json!({})).is_err()); +} + +// Regression test for #3763: `edit.installedProfiles` scans the OS and user ICC folders but +// takes no path parameter, so automation let it through until it was listed as ambient. +#[test] +fn installed_profiles_listing_is_refused_for_automation() { + for params in [serde_json::json!({}), serde_json::json!({"class": "output", "colorSpace": "cmyk"})] { + assert!(authorize_engine_command("edit.installedProfiles", ¶ms).is_err(), "authorize_engine_command allowed it with {params}"); + assert!(authorize_desktop_engine_command("edit.installedProfiles", ¶ms).is_err(), "authorize_desktop_engine_command allowed it with {params}"); + assert!(authorize_engine_step("edit.installedProfiles", ¶ms).is_err(), "authorize_engine_step allowed it with {params}"); + } + // The sandboxed session refuses it before the engine scans any folder. + let mut headless = crate::Headless::new(); + let refused = headless.command_run("edit.installedProfiles", serde_json::json!({})).unwrap_err(); + assert!(refused.to_string().contains("ambient filesystem paths"), "{refused}"); +} + +#[test] +fn apply_data_set_is_judged_by_the_values_it_applies() { + let mut headless = crate::Headless::new(); + headless.command_run("file.new", serde_json::json!({"width": 8, "height": 8})).unwrap(); + let layer = headless.command_run("layer.new.layer", serde_json::json!({})).unwrap()["layer"].clone(); + let defs = [ + serde_json::json!({"name": "shown", "layer": layer, "type": "visibility"}), + serde_json::json!({"name": "photo", "layer": layer, "type": "pixelReplacement"}), + ]; + headless.command_run("image.variables.define", serde_json::json!({"defs": defs})).unwrap(); + let sets = serde_json::json!({"dataSets": [ + {"name": "hidden", "values": [{"variable": "shown", "kind": "visibility", "value": false}]}, + {"name": "swap", "values": [ + {"variable": "shown", "kind": "visibility", "value": false}, + {"variable": "photo", "kind": "pixels", "value": "/outside/photo.png"}, + ]}, + ]}); + headless.command_run("image.variables.dataSets", sets).unwrap(); + let visible = |headless: &crate::Headless| headless.session.active().unwrap().doc.layers.iter().all(|l| l.visible); + let refused = headless.command_run("image.applyDataSet", serde_json::json!({"name": "swap"})).unwrap_err(); + assert!(refused.to_string().contains("ambient filesystem paths"), "{refused}"); + assert!(visible(&headless), "a refused data set applies none of its values"); + assert!(headless.command_run("image.applyDataSet", serde_json::json!({"name": "hidden"})).is_ok()); + assert!(!visible(&headless)); +} + +#[test] +fn automation_rejects_ambient_path_commands_and_preferences() { + for (id, params) in [ + ("brush.presets.importAbr", serde_json::json!({"path": "/outside/set.abr"})), + ("brush.presets.importImage", serde_json::json!({"path": "/outside/tip.png"})), + ("gradient.presets.importGrd", serde_json::json!({"path": "/outside/set.grd"})), + ("plugin.install", serde_json::json!({"path": "/outside/plugin.wasm"})), + ("plugin.reload", serde_json::json!({"path": "/outside/plugins"})), + ("plugin.reload", serde_json::json!({})), + ("plugin.install", serde_json::json!({"path": " "})), + ("swatches.import", serde_json::json!({"path": "/outside/set.aco"})), + ("swatches.export", serde_json::json!({"path": "/outside/set.ase"})), + ("actions.load", serde_json::json!({"path": "/outside/set.atn"})), + ("actions.save", serde_json::json!({"path": "/outside/set.atn"})), + ("style.presets.importAsl", serde_json::json!({"path": "/outside/set.asl"})), + ("style.presets.exportAsl", serde_json::json!({"path": "/outside/set.asl"})), + ("style.presets.exportAsl", serde_json::json!({"path": 5})), + ("layer.layerStyle.contours.load", serde_json::json!({"path": "/outside/set.shc"})), + ("layer.layerStyle.contours.save", serde_json::json!({"path": "/outside/set.shc"})), + ] { + assert!(authorize_engine_command(id, ¶ms).is_err(), "{id}: {params}"); + } + for (id, params) in [ + ("brush.presets.importAbr", serde_json::json!({"data": "QUJD"})), + ("brush.presets.importImage", serde_json::json!({"data": "QUJD"})), + ("gradient.presets.importGrd", serde_json::json!({"data": "QUJD"})), + ("plugin.install", serde_json::json!({"data": "QUJD"})), + ("swatches.import", serde_json::json!({"data": "QUJD"})), + ("swatches.export", serde_json::json!({"format": "ase"})), + ("actions.load", serde_json::json!({"data": "QUJD"})), + ("actions.save", serde_json::json!({"set": "S"})), + ("style.presets.importAsl", serde_json::json!({"data": "QUJD"})), + ("style.presets.exportAsl", serde_json::json!({"group": "Basics"})), + ("layer.layerStyle.contours.load", serde_json::json!({"data": "QUJD"})), + ("layer.layerStyle.contours.save", serde_json::json!({"names": ["a"]})), + ] { + assert!(authorize_engine_command(id, ¶ms).is_ok(), "{id}: {params}"); + } + for path in [ + "", + "colorSettings", + "colorSettings.workingRgb", + "scriptEvents", + "scriptEvents.enabled", + ".scriptEvents", + ".scriptEvents.enabled", + "historyLog.filePath", + ".historyLog.filePath", + "plugIns.additionalPluginsFolder", + "fileHandling.lutFolder", + "fileHandling", + "scratchDisks.disks", + ".", + "..historyLog.filePath", + ] { + assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": path, "value": {}})).is_err(), "{path}"); + } + assert!( + authorize_engine_command("prefs.set", &serde_json::json!({"values": {"interface.language": "fr", "historyLog.filePath": "/outside/log"}})).is_err() + ); + assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "interface.language", "value": "fr"})).is_ok()); + assert!(authorize_engine_command("prefs.set", &serde_json::json!({"path": "fileHandling.autosave", "value": false})).is_ok()); +} + +#[test] +fn registry_filesystem_path_params_are_classified() { + let probe = serde_json::json!({ + "path": "/outside/photocraft-probe", + "file": "/outside/photocraft-probe.icc", + "mapPath": "/outside/photocraft-probe.png", + "profile": "/outside/photocraft-probe.icc", + "workingRgb": "/outside/photocraft-probe.icc", + "workingCmyk": "/outside/photocraft-probe.icc", + "workingGray": "/outside/photocraft-probe.icc", + "input": "/outside/photocraft-input", + "output": "/outside/photocraft-output", + "paths": ["/outside/photocraft-probe.psd"], + }); + let preference_probe = serde_json::json!({"path": ".scriptEvents", "value": {}}); + let unclassified: Vec<_> = photocraft_engine::command_specs() + .iter() + .filter(|spec| documents_filesystem_path_params(spec.id, spec.params)) + .filter(|spec| { + let params = if spec.id == "prefs.set" { &preference_probe } else { &probe }; + authorize_engine_command(spec.id, params).is_ok() + }) + .map(|spec| spec.id) + .collect(); + assert!(unclassified.is_empty(), "filesystem path parameters in the command registry need an automation policy: {unclassified:?}"); +} + +fn documents_filesystem_path_params(id: &str, params: &str) -> bool { + // These commands return settings or installed-profile paths; they do not accept a host + // filesystem path as an automation input. + if matches!(id, "prefs.get" | "prefs.reset" | "edit.installedProfiles") { + return false; + } + // These registry descriptions refer to document vector paths, not host files. + if matches!( + id, + "filter.blurGallery.pathBlur" + | "filter.render.flame" + | "shape.create" + | "shape.edit" + | "shape.info" + | "shape.presets.list" + | "shape.presets.new" + | "path.list" + | "path.info" + | "path.set" + | "path.transform" + | "path.moveAnchors" + | "path.moveHandle" + | "path.bendSegment" + | "path.convertPoint" + | "path.addAnchor" + | "path.deleteAnchor" + | "path.mergeShapeComponents" + | "path.setOperation" + | "path.clippingPath.set" + | "path.rename" + | "path.stroke" + | "type.attachToPath" + | "type.fillPath" + | "type.detachFromPath" + | "select.toWorkPath" + | "layer.vectorMask.add" + | "layer.vectorMask.edit" + | "layer.vectorMask.info" + | "paint.symmetryFromPath" + | "edit.defineCustomShape" + | "layer.combineShapes.unite" + | "layer.combineShapes.subtractFrontShape" + | "layer.combineShapes.intersectShapeAreas" + | "layer.combineShapes.excludeOverlappingShapes" + | "layer.combineShapes.mergeShapeComponents" + ) || id.starts_with("layer.newFillLayer.") + || id.starts_with("layer.newAdjustmentLayer.") + { + // New fill and adjustment layers take a vector path as their vector mask (#1419). + return false; + } + params.to_ascii_lowercase().contains("path") +} + +#[test] +fn whole_preferences_update_cannot_enable_automation_script_events() { + let mut headless = crate::headless::Headless::new(); + let result = headless.command_run( + "prefs.set", + serde_json::json!({ + "path": "", + "value": { + "scriptEvents": { + "enabled": true, + "bindings": [{ + "event": "newDocument", + "steps": [["file.saveACopy", {"path": "/outside/canary.psd"}]] + }] + } + } + }), + ); + assert!(result.is_err()); + + for (id, params) in [ + ("brush.presets.importAbr", serde_json::json!({"path": "/outside/set.abr"})), + ("gradient.presets.importGrd", serde_json::json!({"path": "/outside/set.grd"})), + ("plugin.install", serde_json::json!({"path": "/outside/plugin.wasm"})), + ("plugin.reload", serde_json::json!({"path": "/outside/plugins"})), + ("style.presets.importAsl", serde_json::json!({"path": "/outside/set.asl"})), + ("style.presets.exportAsl", serde_json::json!({"path": "/outside/set.asl"})), + ("prefs.set", serde_json::json!({"path": "historyLog.filePath", "value": "/outside/log"})), + ("prefs.set", serde_json::json!({"values": {"interface.language": "fr", "historyLog.filePath": "/outside/log"}})), + ] { + assert!(headless.command_run(id, params).is_err(), "{id}"); + } + headless.command_run("file.new", serde_json::json!({"width": 5, "height": 5})).unwrap(); + assert!(headless.session.file_menu.event_log.is_empty()); +} + +/// #2176: an absolute path beneath the root names the same file as its relative path; every +/// other absolute path is refused before any file effect. +#[test] +fn absolute_paths_beneath_the_root_are_translated_and_others_refused() { + let (inside, outside, workspace) = roots("absolute-paths"); + let abs = |p: &Path| p.to_string_lossy().into_owned(); + + workspace.write(&abs(&inside.join("a.bin")), b"one").unwrap(); + assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); + assert_eq!(workspace.read(&abs(&inside.join("a.bin"))).unwrap(), b"one"); + // Forward slashes too (on Windows the native spelling uses `\`). + assert_eq!(workspace.read(&abs(&inside.join("a.bin")).replace('\\', "/")).unwrap(), b"one"); + std::fs::create_dir(inside.join("sub")).unwrap(); + workspace.write(&abs(&inside.join("sub").join("b.bin")), b"two").unwrap(); + assert_eq!(std::fs::read(inside.join("sub/b.bin")).unwrap(), b"two"); + assert_eq!(workspace.read("sub/b.bin").unwrap(), b"two", "relative paths still work"); + + let refused = |path: String| { + let error = workspace.write(&path, b"x").unwrap_err().to_string(); + assert!(error.contains("automation path rejected"), "{path}: {error}"); + }; + refused(abs(&outside.join("escape.bin"))); + refused(abs(&inside.join("..").join("outside").join("traversal.bin"))); + refused(abs(&inside)); + refused(abs(&inside.join("CON"))); + refused(format!("{}:hidden", abs(&inside.join("a.bin")))); + // A sibling whose name merely starts with the root's name is not beneath it. + let sibling = inside.with_file_name("inside-sibling"); + std::fs::create_dir_all(&sibling).unwrap(); + refused(abs(&sibling.join("lookalike.bin"))); + refused(format!("{}-sibling/lookalike.bin", abs(&inside))); + // Climbing back out through a subdirectory is refused too. + refused(abs(&inside.join("sub").join("..").join("..").join("outside").join("climb.bin"))); + assert!(!sibling.join("lookalike.bin").exists()); + assert!(!outside.join("climb.bin").exists()); + assert!(!outside.join("escape.bin").exists()); + assert!(!outside.join("traversal.bin").exists()); + assert_eq!(std::fs::read(inside.join("a.bin")).unwrap(), b"one"); +} + +/// Equivalent drive, UNC and ASCII-case root spellings need no ambient path resolution. +#[cfg(windows)] +#[test] +fn canonical_windows_prefixes_match_without_ambient_resolution() { + for (request, root) in [ + (r"c:\root\new.bin", r"\\?\C:\root"), + (r"\\?\C:\root\new.bin", r"C:\root"), + (r"\\server\share\root\new.bin", r"\\?\UNC\SERVER\SHARE\root"), + (r"c:\ROOT\new.bin", r"\\?\C:\root"), + (r"\\?\C:\ROOT\new.bin", r"c:\root"), + (r"\\server\share\ROOT\new.bin", r"\\?\UNC\SERVER\SHARE\root"), + ] { + let remaining = path_beneath_root(Path::new(request), Path::new(root)).unwrap(); + assert_eq!(remaining.as_path(), Path::new("new.bin")); + } + for request in [r"C:\root-sibling\new.bin", r"D:\root\new.bin", r"\\.\C:\root\new.bin", r"\\other\share\root\new.bin"] { + assert!(path_beneath_root(Path::new(request), Path::new(r"\\?\C:\root")).is_none()); + } +} + +/// Case variants of the granted Windows prefix still use its held directory capability. +#[cfg(windows)] +#[test] +fn windows_root_case_variants_keep_io_inside_the_grant() { + let (inside, outside, workspace) = roots("root-case"); + let configured = PathBuf::from(inside.to_string_lossy().to_ascii_uppercase()); + let canonical = PathBuf::from(std::fs::canonicalize(&inside).unwrap().to_string_lossy().to_ascii_uppercase()); + for spelling in [&configured, &canonical] { + let path = spelling.join("new.png"); + workspace.write(&path.to_string_lossy(), b"inside").unwrap(); + assert_eq!(workspace.read(&path.to_string_lossy()).unwrap(), b"inside"); + assert!(workspace.write(&spelling.join("..").join("outside").join("blocked.bin").to_string_lossy(), b"no").is_err()); + } + assert_eq!(std::fs::read(inside.join("new.png")).unwrap(), b"inside"); + assert!(workspace.write(&configured.with_file_name("INSIDE-SIBLING").join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("blocked.bin").exists()); + // Case normalization must not bypass the refusal to replace unrelated existing files. + std::fs::write(inside.join("notes.bin"), b"keep").unwrap(); + assert!(workspace.write(&configured.join("notes.bin").to_string_lossy(), b"no").is_err()); + assert_eq!(std::fs::read(inside.join("notes.bin")).unwrap(), b"keep"); +} + +/// Unix roots with different case must not become aliases of the granted prefix. +#[cfg(unix)] +#[test] +fn unix_root_prefixes_remain_case_sensitive() { + assert!(path_beneath_root(Path::new("/Root/new.bin"), Path::new("/root")).is_none()); +} + +#[test] +fn configured_parent_components_accept_resolved_absolute_paths() { + let (inside, outside, _) = roots("resolved-parent-root"); + let configured = inside.join("..").join("inside"); + let workspace = AuthorizedWorkspace::new(Some(&configured), Some(&configured)).unwrap(); + let resolved = std::fs::canonicalize(&inside).unwrap(); + workspace.write(&resolved.join("new.bin").to_string_lossy(), b"new").unwrap(); + assert_eq!(workspace.read(&resolved.join("new.bin").to_string_lossy()).unwrap(), b"new"); + workspace.write(&configured.join("launch-spelling.bin").to_string_lossy(), b"trusted prefix").unwrap(); + assert_eq!(workspace.read(&configured.join("launch-spelling.bin").to_string_lossy()).unwrap(), b"trusted prefix"); + assert_eq!(workspace.read(&resolved.join("launch-spelling.bin").to_string_lossy()).unwrap(), b"trusted prefix"); + assert!(workspace.write(&configured.join("..").join("outside/escaped.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("escaped.bin").exists()); + assert!(workspace.write(&outside.join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(!outside.join("blocked.bin").exists()); +} + +#[test] +fn relative_configured_root_accepts_resolved_absolute_paths() { + let relative = PathBuf::from("target").join(format!("automation-relative-root-{}", std::process::id())); + std::fs::create_dir_all(&relative).unwrap(); + let workspace = AuthorizedWorkspace::new(Some(&relative), Some(&relative)).unwrap(); + let resolved = std::fs::canonicalize(&relative).unwrap(); + workspace.write(&resolved.join("new.bin").to_string_lossy(), b"relative").unwrap(); + assert_eq!(workspace.read(&resolved.join("new.bin").to_string_lossy()).unwrap(), b"relative"); + std::fs::remove_file(resolved.join("new.bin")).unwrap(); + drop(workspace); + std::fs::remove_dir(resolved).unwrap(); +} + +#[cfg(unix)] +#[test] +fn trusted_root_names_are_not_validated_as_relative_request_names() { + let (inside, outside, _) = roots("trusted-root-names"); + let root = inside.join("aux"); + std::fs::create_dir(&root).unwrap(); + let alias = outside.join("CON"); + link_directory(&root, &alias); + let workspace = AuthorizedWorkspace::new(Some(&alias), Some(&alias)).unwrap(); + for (index, spelling) in [&root, &alias].into_iter().enumerate() { + let fresh = spelling.join(format!("new-{index}.bin")); + workspace.write(&fresh.to_string_lossy(), b"trusted").unwrap(); + assert_eq!(workspace.read(&fresh.to_string_lossy()).unwrap(), b"trusted"); + assert!(workspace.write(&fresh.to_string_lossy(), b"replacement").is_err(), "unknown existing files remain protected"); + assert!(workspace.write(&spelling.join("NUL").to_string_lossy(), b"no").is_err()); + } + assert!(!root.join("NUL").exists()); + remove_directory_link(&alias); +} + +// Windows junctions exercise the same root alias without requiring Developer Mode. +#[cfg(any(unix, windows))] +fn link_directory(target: &Path, link: &Path) { + #[cfg(unix)] + std::os::unix::fs::symlink(target, link).unwrap(); + #[cfg(windows)] + assert!(std::process::Command::new("cmd.exe").args(["/D", "/C", "mklink", "/J"]).arg(link).arg(target).output().unwrap().status.success()); +} + +#[cfg(any(unix, windows))] +fn remove_directory_link(link: &Path) { + #[cfg(unix)] + std::fs::remove_file(link).unwrap(); + #[cfg(windows)] + std::fs::remove_dir(link).unwrap(); +} + +#[cfg(any(unix, windows))] +#[test] +fn linked_root_accepts_both_spellings_and_new_nested_exports() { + let (inside, outside, _) = roots("linked-root"); + let link = outside.join("root-link"); + link_directory(&inside, &link); + let workspace = AuthorizedWorkspace::new(Some(&link), Some(&link)).unwrap(); + let resolved = std::fs::canonicalize(&inside).unwrap(); + for (i, spelling) in [resolved, link.clone()].iter().enumerate() { + let name = format!("new-{i}.bin"); + workspace.write(&spelling.join(&name).to_string_lossy(), b"new").unwrap(); + assert_eq!(workspace.read(&spelling.join(&name).to_string_lossy()).unwrap(), b"new"); + assert_eq!(workspace.read(&name).unwrap(), b"new"); + let mut export = workspace.export_sequence(&spelling.join(format!("frames-{i}")).join("nested").to_string_lossy()).unwrap(); + export.write("frame.png", b"frame").unwrap(); + export.commit(); + assert_eq!(std::fs::read(inside.join(format!("frames-{i}/nested/frame.png"))).unwrap(), b"frame"); + } + remove_directory_link(&link); +} + +/// TEMP can name the same directory differently from canonicalize (8.3 on Windows). +/// A junction makes that mismatch deterministic even when 8.3 names are disabled. +#[cfg(windows)] +#[test] +fn root_alias_tests_work_with_an_aliased_windows_temp() { + let (inside, outside, _) = roots("aliased-temp"); + let temp_alias = outside.join("temp-alias"); + link_directory(&inside, &temp_alias); + let exe = std::env::current_exe().unwrap(); + for test in [ + "workspace::tests::ungranted_absolute_aliases_and_link_escapes_are_refused", + "workspace::tests::retargeting_root_alias_does_not_regrant_the_held_authority", + ] { + // Set TEMP only in the child; changing the process environment races other tests. + let output = std::process::Command::new(&exe).args(["--exact", test, "--nocapture"]).env("TEMP", &temp_alias).env("TMP", &temp_alias).output().unwrap(); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!(output.status.success(), "{test}: {stdout}\n{stderr}"); + assert!(stdout.contains("1 passed;"), "child must run the requested test: {stdout}"); + } + remove_directory_link(&temp_alias); +} + +#[cfg(any(unix, windows))] +#[test] +fn ungranted_absolute_aliases_and_link_escapes_are_refused() { + let (inside, outside, workspace) = roots("request-alias"); + // TEMP may use an 8.3 name or a junction. A grant through `alias` below recognises + // that configured alias and the canonical target, not every other name for `inside`. + let resolved_inside = std::fs::canonicalize(&inside).unwrap(); + let alias = outside.join("inside-alias"); + link_directory(&inside, &alias); + workspace.write("new.bin", b"inside").unwrap(); + assert!(workspace.read(&alias.join("new.bin").to_string_lossy()).is_err()); + assert!(workspace.write(&alias.join("ungranted.bin").to_string_lossy(), b"no").is_err()); + assert!(!inside.join("ungranted.bin").exists()); + std::fs::write(outside.join("secret.bin"), b"outside").unwrap(); + let escape = inside.join("escape"); + link_directory(&outside, &escape); + assert!(workspace.read(&escape.join("secret.bin").to_string_lossy()).is_err()); + assert!(workspace.write(&escape.join("blocked.bin").to_string_lossy(), b"no").is_err()); + assert!(workspace.export_sequence(&escape.join("frames").join("nested").to_string_lossy()).is_err()); + assert!(!outside.join("blocked.bin").exists()); + assert!(!outside.join("frames").exists()); + assert_eq!(std::fs::read(outside.join("secret.bin")).unwrap(), b"outside"); + let read_only = AuthorizedWorkspace::new(Some(&alias), Some(&outside)).unwrap(); + assert_eq!(read_only.read(&resolved_inside.join("new.bin").to_string_lossy()).unwrap(), b"inside"); + assert!(read_only.write(&alias.join("denied.bin").to_string_lossy(), b"no").is_err()); + assert!(!inside.join("denied.bin").exists()); + remove_directory_link(&escape); + remove_directory_link(&alias); +} + +#[cfg(any(unix, windows))] +#[test] +fn retargeting_root_alias_does_not_regrant_the_held_authority() { + let (inside, outside, _) = roots("retargeted-root"); + let resolved_inside = std::fs::canonicalize(&inside).unwrap(); + let alias = outside.join("root-alias"); + link_directory(&inside, &alias); + let workspace = AuthorizedWorkspace::new(Some(&alias), Some(&alias)).unwrap(); + remove_directory_link(&alias); + link_directory(&outside, &alias); + std::fs::write(outside.join("secret.bin"), b"secret").unwrap(); + workspace.write("held.bin", b"held").unwrap(); + assert_eq!(workspace.read(&resolved_inside.join("held.bin").to_string_lossy()).unwrap(), b"held"); + assert!(workspace.read(&alias.join("secret.bin").to_string_lossy()).is_err()); + workspace.write(&alias.join("held-alias.bin").to_string_lossy(), b"original grant").unwrap(); + assert_eq!(std::fs::read(inside.join("held-alias.bin")).unwrap(), b"original grant"); + assert!(!outside.join("held-alias.bin").exists()); + assert_eq!(std::fs::read(outside.join("secret.bin")).unwrap(), b"secret"); + remove_directory_link(&alias); +} + +/// Each authority is checked against its own root, and none granted refuses absolute paths. +#[test] +fn absolute_paths_are_checked_against_the_authority_s_own_root() { + let (inside, outside, _) = roots("absolute-authority"); + let workspace = AuthorizedWorkspace::new(Some(&outside), Some(&inside)).unwrap(); + let target = inside.join("written.bin"); + workspace.write(&target.to_string_lossy(), b"w").unwrap(); + assert!(workspace.read(&target.to_string_lossy()).is_err(), "not under the read root"); + let none = AuthorizedWorkspace::default(); + assert!(none.read(&target.to_string_lossy()).is_err()); +} diff --git a/crates/automation/tests/agent_tasks.rs b/crates/automation/tests/it/agent_tasks.rs similarity index 100% rename from crates/automation/tests/agent_tasks.rs rename to crates/automation/tests/it/agent_tasks.rs diff --git a/crates/automation/tests/it/agent_tasks_extended.rs b/crates/automation/tests/it/agent_tasks_extended.rs new file mode 100644 index 0000000000..4f950ed1a8 --- /dev/null +++ b/crates/automation/tests/it/agent_tasks_extended.rs @@ -0,0 +1,184 @@ +//! M11 acceptance, part two: ten more scripted edit tasks (21–30) over MCP (roadmap M11 DoD). +//! +//! Same contract as `agent_tasks.rs`: each task uses only MCP tools (`doc_*`, `command_run`) the +//! way an agent would, and checks the outcome through `doc_inspect`, `document.pixel` or the +//! exported files, never engine internals. + +use photocraft_automation::{AuthorizedWorkspace, PhotocraftMcp}; +use rmcp::model::{CallToolRequestParams, CallToolResult, ClientConfig}; +use rmcp::service::RunningService; +use rmcp::{ClientHandler, RoleClient, ServiceExt}; +use serde_json::{Value, json}; + +#[derive(Clone, Default)] +struct Client; +impl ClientHandler for Client { + fn get_info(&self) -> ClientConfig { + ClientConfig::default() + } +} + +type Conn = RunningService; +type Server = tokio::task::JoinHandle<()>; + +async fn connect(root: &std::path::Path) -> (Conn, Server) { + let (s, c) = tokio::io::duplex(1 << 20); + let workspace = AuthorizedWorkspace::new(Some(root), Some(root)).expect("test workspace"); + let server = tokio::spawn(async move { + if let Ok(running) = PhotocraftMcp::headless_with_workspace(workspace).serve(s).await { + let _ = running.waiting().await; + } + }); + (Client.serve(c).await.expect("client init"), server) +} + +async fn shutdown(c: Conn, server: Server) { + c.cancel().await.unwrap(); + server.await.unwrap(); +} + +fn text(r: &CallToolResult) -> String { + r.content.iter().filter_map(|c| c.as_text()).map(|t| t.text.clone()).collect::>().join("\n") +} + +/// Call a tool and parse its JSON result, failing the test on a tool error. +async fn tool(c: &Conn, name: &str, args: Value) -> Value { + let mut p = CallToolRequestParams::new(name.to_owned()); + if let Value::Object(m) = args { + p = p.with_arguments(m); + } + let r = c.call_tool(p).await.expect("call_tool transport"); + assert_ne!(r.is_error, Some(true), "{name} failed: {}", text(&r)); + serde_json::from_str(&text(&r)).unwrap_or(Value::String(text(&r))) +} + +async fn run(c: &Conn, id: &str, params: Value) -> Value { + tool(c, "command_run", json!({"id": id, "params": params})).await +} + +async fn inspect(c: &Conn) -> Value { + tool(c, "doc_inspect", json!({})).await +} + +async fn pixel(c: &Conn, x: i32, y: i32) -> Vec { + let v = run(c, "document.pixel", json!({"x": x, "y": y})).await; + let arr = if v.is_array() { v } else { v.get("result").cloned().unwrap_or(v) }; + arr.as_array().unwrap_or_else(|| panic!("pixel: {arr}")).iter().map(|x| x.as_f64().unwrap()).collect() +} + +fn top_layer(doc: &Value) -> &Value { + &doc["layers"][0] +} + +fn tmp(name: &str) -> std::path::PathBuf { + let d = std::env::temp_dir().join(format!("pc-agent-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +fn close(a: &[f64], b: &[f64], tol: f64) -> bool { + a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol) +} + +/// The next ten acceptance tasks (21–30): fill layers, adjustment layers with masks, grouping, +/// merging, gradients of edits, channel round trips, JPEG export and a native save round trip +/// that preserves a group. +#[tokio::test(flavor = "multi_thread")] +async fn agent_completes_ten_extended_tasks() { + let dir = tmp("tasks3"); + let (c, server) = connect(&dir).await; + + // 21. Paint a red background and cover it with a solid-colour fill layer. + tool(&c, "doc_new", json!({"width": 64, "height": 64, "background": "white"})).await; + run(&c, "select.all", json!({})).await; + run(&c, "edit.fill", json!({"color": "#ff0000"})).await; + run(&c, "select.deselect", json!({})).await; + run(&c, "layer.newFillLayer.solidColor", json!({"color": "#00ffff"})).await; + assert_eq!(top_layer(&inspect(&c).await)["kind"], "Fill", "task 21"); + let cyan = pixel(&c, 32, 32).await; + assert!(cyan[1] > 0.99 && cyan[2] > 0.99 && cyan[0] < 0.01, "task 21: the fill covers red {cyan:?}"); + + // 22. An Invert adjustment flips it to red; deleting the adjustment restores cyan. + run(&c, "layer.newAdjustmentLayer.invert", json!({})).await; + assert_eq!(top_layer(&inspect(&c).await)["kind"], "Adjustment", "task 22"); + let inverted = pixel(&c, 32, 32).await; + assert!(inverted[0] > 0.99 && inverted[1] < 0.01 && inverted[2] < 0.01, "task 22: cyan inverts to red {inverted:?}"); + run(&c, "layer.delete", json!({})).await; + assert!(close(&pixel(&c, 32, 32).await, &cyan, 1e-4), "task 22: deleting the adjustment restores cyan"); + + // 23. Hide a rectangular selection behind the layer's mask. + run(&c, "layer.delete", json!({})).await; // drop the fill so the red background shows + run(&c, "select.rect", json!({"x": 16, "y": 16, "width": 32, "height": 32})).await; + run(&c, "layer.layerMask.hideSelection", json!({})).await; + let doc = inspect(&c).await; + assert_eq!(top_layer(&doc)["hasMask"], true, "task 23"); + let hidden = pixel(&c, 32, 32).await; + let kept = pixel(&c, 4, 4).await; + assert!(hidden[3] < 0.01, "task 23: the selection is masked away {hidden:?}"); + assert!(kept[3] > 0.99 && kept[0] > 0.99, "task 23: outside keeps the red {kept:?}"); + run(&c, "layer.layerMask.delete", json!({})).await; + + // 24. Group every layer; the pixels are unchanged. + let before = pixel(&c, 32, 32).await; + run(&c, "select.allLayers", json!({})).await; + run(&c, "layer.groupLayers", json!({})).await; + let doc = inspect(&c).await; + assert_eq!(top_layer(&doc)["kind"], "Group", "task 24: {doc}"); + assert!(close(&pixel(&c, 32, 32).await, &before, 1e-4), "task 24: grouping is visual only"); + + // 25. Flatten the grouped document to one layer. + run(&c, "layer.flattenImage", json!({})).await; + assert_eq!(inspect(&c).await["layers"].as_array().unwrap().len(), 1, "task 25"); + + // 26. Duplicate the layer and delete the copy. + run(&c, "layer.duplicate", json!({})).await; + assert_eq!(inspect(&c).await["layers"].as_array().unwrap().len(), 2, "task 26"); + run(&c, "layer.delete", json!({})).await; + assert_eq!(inspect(&c).await["layers"].as_array().unwrap().len(), 1, "task 26"); + + // 27. A Curves adjustment darkens the red; deleting it restores the original. + run(&c, "layer.newAdjustmentLayer.curves", json!({"points": [[0, 0], [128, 32], [255, 128]]})).await; + let dark = pixel(&c, 32, 32).await; + assert!(dark[0] < 0.9, "task 27: the red channel deepens {dark:?}"); + run(&c, "layer.delete", json!({})).await; + assert!(close(&pixel(&c, 32, 32).await, &before, 1e-4), "task 27: deleting restores the red"); + + // 28. Hue/Saturation at -100 desaturates the red to gray. + run(&c, "layer.newAdjustmentLayer.hueSaturation", json!({"saturation": -100})).await; + let gray = pixel(&c, 32, 32).await; + assert!((gray[0] - gray[1]).abs() < 0.05 && (gray[1] - gray[2]).abs() < 0.05, "task 28: saturated red goes gray {gray:?}"); + run(&c, "layer.delete", json!({})).await; + + // 29. Save a selection to a channel, load it back and paint through it. + run(&c, "select.rect", json!({"x": 20, "y": 20, "width": 24, "height": 24})).await; + run(&c, "select.saveSelection", json!({"name": "Box"})).await; + run(&c, "select.deselect", json!({})).await; + run(&c, "select.loadSelection", json!({"channel": "Box"})).await; + run(&c, "edit.fill", json!({"color": "#0000ff"})).await; + let painted = pixel(&c, 32, 32).await; + let outside = pixel(&c, 4, 4).await; + assert!(painted[2] > 0.99 && painted[0] < 0.01, "task 29: the loaded channel fills blue {painted:?}"); + assert!(outside[0] > 0.99 && outside[2] < 0.01, "task 29: outside keeps red {outside:?}"); + run(&c, "select.deselect", json!({})).await; + + // 30. Save natively with a group, reopen and find the group and its content intact. + run(&c, "layer.new.layer", json!({"name": "Badge"})).await; + run(&c, "select.rect", json!({"x": 40, "y": 40, "width": 16, "height": 16})).await; + run(&c, "edit.fill", json!({"color": "#00ff00"})).await; + run(&c, "select.deselect", json!({})).await; + run(&c, "select.allLayers", json!({})).await; + run(&c, "layer.groupLayers", json!({"name": "Set"})).await; + tool(&c, "doc_save", json!({"path": "grouped.pcraft"})).await; + tool(&c, "doc_close", json!({})).await; + tool(&c, "doc_open", json!({"path": "grouped.pcraft"})).await; + let doc = inspect(&c).await; + assert_eq!(top_layer(&doc)["kind"], "Group", "task 30: {doc}"); + assert_eq!(top_layer(&doc)["name"], "Set", "task 30: {doc}"); + let green = pixel(&c, 48, 48).await; + assert!(green[1] > 0.99 && green[0] < 0.01, "task 30: the badge inside the group survives {green:?}"); + tool(&c, "doc_close", json!({})).await; + + shutdown(c, server).await; + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/automation/tests/agent_tasks_more.rs b/crates/automation/tests/it/agent_tasks_more.rs similarity index 100% rename from crates/automation/tests/agent_tasks_more.rs rename to crates/automation/tests/it/agent_tasks_more.rs diff --git a/crates/automation/tests/bridge.rs b/crates/automation/tests/it/bridge.rs similarity index 100% rename from crates/automation/tests/bridge.rs rename to crates/automation/tests/it/bridge.rs diff --git a/crates/automation/tests/inactive_save.rs b/crates/automation/tests/it/inactive_save.rs similarity index 100% rename from crates/automation/tests/inactive_save.rs rename to crates/automation/tests/it/inactive_save.rs diff --git a/crates/automation/tests/krita.rs b/crates/automation/tests/it/krita.rs similarity index 95% rename from crates/automation/tests/krita.rs rename to crates/automation/tests/it/krita.rs index 85bde4f5fc..44de53ff2a 100644 --- a/crates/automation/tests/krita.rs +++ b/crates/automation/tests/it/krita.rs @@ -4,14 +4,15 @@ use photocraft_automation::Headless; use serde_json::json; -/// A tiled pixel file with one raw 64x64 tile of `bgra` at (0, 0). +/// A tiled pixel file with one raw 64x64 tile of `bgra` at (0, 0). Krita writes a raw tile +/// (flag 0) as plain interleaved pixels; only LZF tiles are split into byte planes. fn tiles(bgra: [u8; 4]) -> Vec { let n = 64 * 64; let mut out = b"VERSION 2\nTILEWIDTH 64\nTILEHEIGHT 64\nPIXELSIZE 4\nDATA 1\n".to_vec(); out.extend(format!("0,0,LZF,{}\n", 1 + n * 4).into_bytes()); out.push(0); - for b in bgra { - out.extend(std::iter::repeat_n(b, n)); + for _ in 0..n { + out.extend(bgra); } out } diff --git a/crates/automation/tests/layered_save_dirty.rs b/crates/automation/tests/it/layered_save_dirty.rs similarity index 100% rename from crates/automation/tests/layered_save_dirty.rs rename to crates/automation/tests/it/layered_save_dirty.rs diff --git a/crates/automation/tests/it/main.rs b/crates/automation/tests/it/main.rs new file mode 100644 index 0000000000..f701bd705b --- /dev/null +++ b/crates/automation/tests/it/main.rs @@ -0,0 +1,16 @@ +//! The crate's integration tests, linked into one test binary. + +mod agent_tasks; +mod agent_tasks_extended; +mod agent_tasks_more; +mod bridge; +mod inactive_save; +mod krita; +mod layered_save_dirty; +mod mcp; +mod mcp_wire; +mod missing_font_warnings; +mod native_save_name; +mod openraster; +mod raw_defaults; +mod smart_filter_authorization; diff --git a/crates/automation/tests/mcp.rs b/crates/automation/tests/it/mcp.rs similarity index 88% rename from crates/automation/tests/mcp.rs rename to crates/automation/tests/it/mcp.rs index 7386f3d58c..e18df63c4a 100644 --- a/crates/automation/tests/mcp.rs +++ b/crates/automation/tests/it/mcp.rs @@ -80,6 +80,7 @@ async fn lists_expected_tools() { "doc_open", "doc_new", "doc_place", + "doc_replace_smart_object", "doc_export_layers", "doc_export_data_sets", "doc_import_data_sets", @@ -272,12 +273,13 @@ async fn command_ids_in_tool_schemas_exist() { client.cancel().await.unwrap(); } -/// `UI_SET_FIELDS` in `crates/ui-egui/src/control.rs`, read from source. The desktop shell owns the +/// `UI_SET_FIELDS` in `crates/ui-egui/src/control/params.rs`, read from source. The desktop shell owns the /// list; this crate describes it over MCP without depending on the UI crate (both are L6). /// Anchored on the declaration, not on the doc comment that mentions it first, and checked against /// the declared array length so a mis-parse fails loudly instead of testing the wrong names. fn ui_set_fields() -> Vec { - let src = std::fs::read_to_string(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../ui-egui/src/control.rs")).expect("ui-egui control.rs"); + let src = + std::fs::read_to_string(std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../ui-egui/src/control/params.rs")).expect("ui-egui control/params.rs"); let (_, rest) = src.split_once("UI_SET_FIELDS: [&str; ").expect("UI_SET_FIELDS is declared"); let declared: usize = rest.split_once(']').expect("declared length").0.parse().expect("declared length"); let body = rest.split_once('[').expect("field array").1.split_once("];").expect("terminated array").0; @@ -309,6 +311,10 @@ async fn errors_are_tool_errors_not_crashes() { let r = call(&client, "command_run", json!({"id": "no.such.command"})).await; assert_eq!(r.is_error, Some(true)); assert!(text(&r).contains("unknown command")); + // #3674: the error names the nearest registered id. + let r = call(&client, "command_run", json!({"id": "engine.commands"})).await; + assert_eq!(r.is_error, Some(true)); + assert!(text(&r).contains("did you mean `command.list`"), "{}", text(&r)); let r = call(&client, "doc_inspect", json!({"index": 0})).await; assert_eq!(r.is_error, Some(true)); let r = call(&client, "doc_open", json!({"path": "/definitely/missing.png"})).await; @@ -318,6 +324,10 @@ async fn errors_are_tool_errors_not_crashes() { assert!(text(&r).contains("bridge")); let r = call(&client, "doc_export", json!({})).await; assert_eq!(r.is_error, Some(true)); + // Quality 0 is refused, not clamped to 1 (serde already refuses 500, -1 and "85"). + let r = call(&client, "doc_save", json!({"path": "q.jpg", "quality": 0})).await; + assert_eq!(r.is_error, Some(true)); + assert!(text(&r).contains("from 1 to 100"), "{}", text(&r)); // Server still alive. json_of(&call(&client, "session_list", json!({})).await); client.cancel().await.unwrap(); @@ -415,6 +425,45 @@ async fn headless_doc_save_writes_targa_bits() { cleanup(&dir); } +/// `ddsCompression` and `ddsMipmaps` reach the app's `app.save` through the bridge; an unknown +/// compression is refused before anything is sent. +#[tokio::test(flavor = "multi_thread")] +async fn bridge_doc_save_forwards_dds_options() { + let (addr, app) = fake_app().await; + let client = connect(PhotocraftMcp::bridge(&addr, CONTROL_TOKEN).unwrap()).await; + let saved = json_of(&call(&client, "doc_save", json!({"path": "out.dds", "ddsCompression": "DXT5", "ddsMipmaps": true})).await); + assert_eq!(saved, json!({"saved": {"path": "out.dds", "ddsCompression": "bc3", "ddsMipmaps": true}})); + let r = call(&client, "doc_save", json!({"path": "out.dds", "ddsCompression": "bc7"})).await; + assert_eq!(r.is_error, Some(true), "{}", text(&r)); + assert!(text(&r).contains("ddsCompression"), "{}", text(&r)); + client.cancel().await.unwrap(); + app.abort(); +} + +/// Headless `doc_save`/`doc_export` write the DDS compression (the FourCC, bytes 84–87) and mip +/// count (bytes 28–31) asked for; unset, an uncompressed texture without mipmaps. +#[tokio::test(flavor = "multi_thread")] +async fn headless_doc_save_writes_dds_options() { + let dir = tmp("dds-options"); + let client = connect(headless_in(&dir)).await; + call(&client, "doc_new", json!({"width": 16, "height": 8, "background": "white"})).await; + for (tool, params, fourcc, mips) in [ + ("doc_export", json!({"path": "plain.dds"}), [0; 4], 1), + ("doc_export", json!({"path": "bc1.dds", "ddsCompression": "bc1"}), *b"DXT1", 1), + ("doc_save", json!({"path": "bc3.dds", "ddsCompression": "bc3", "ddsMipmaps": true}), *b"DXT5", 5), + ] { + let r = call(&client, tool, params.clone()).await; + assert_ne!(r.is_error, Some(true), "{params}: {}", text(&r)); + let bytes = std::fs::read(dir.join(params["path"].as_str().unwrap())).unwrap(); + assert_eq!((&bytes[84..88], u32::from_le_bytes(bytes[28..32].try_into().unwrap())), (&fourcc[..], mips), "{params}"); + } + let r = call(&client, "doc_export", json!({"path": "bad.dds", "ddsCompression": "bc7"})).await; + assert_eq!(r.is_error, Some(true), "{}", text(&r)); + assert!(!dir.join("bad.dds").exists()); + client.cancel().await.unwrap(); + cleanup(&dir); +} + #[tokio::test(flavor = "multi_thread")] async fn bridge_response_budget_drops_connection_without_retrying_the_operation() { use photocraft_automation::{BridgeClient, budgets::MAX_RESPONSE_BYTES}; @@ -663,6 +712,36 @@ async fn doc_place_is_capability_scoped() { cleanup(&base); } +/// #8061: Replace Contents reads from the read root through `doc_replace_smart_object`; the +/// ambient `layer.smartObjects.replaceContents` stays refused. +#[tokio::test(flavor = "multi_thread")] +async fn doc_replace_smart_object_is_capability_scoped() { + let base = tmp("replace-so"); + let root = base.join("workspace"); + let outside = base.join("outside"); + std::fs::create_dir_all(&root).unwrap(); + std::fs::create_dir_all(&outside).unwrap(); + let img = photocraft_codecs::Image::from_u8(8, 6, photocraft_codecs::ChannelLayout::Rgb, vec![42; 144]).unwrap(); + let bytes = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + std::fs::write(root.join("inside.png"), &bytes).unwrap(); + std::fs::write(outside.join("outside.png"), bytes).unwrap(); + let client = connect(headless_in(&root)).await; + + json_of(&call(&client, "doc_new", json!({"width": 40, "height": 30})).await); + let placed = json_of(&call(&client, "doc_place", json!({"path": "inside.png"})).await); + let layer = placed["layer"].clone(); + let replaced = call(&client, "doc_replace_smart_object", json!({"path": "inside.png", "layer": layer})).await; + assert_ne!(replaced.is_error, Some(true), "{}", text(&replaced)); + let refused = call(&client, "doc_replace_smart_object", json!({"path": outside.join("outside.png").to_string_lossy(), "layer": layer})).await; + assert_eq!(refused.is_error, Some(true)); + assert!(text(&refused).contains("must be inside the automation root"), "{}", text(&refused)); + let ambient = call(&client, "command_run", json!({"id": "layer.smartObjects.replaceContents", "params": {"path": "inside.png", "layer": layer}})).await; + assert_eq!(ambient.is_error, Some(true)); + assert!(text(&ambient).contains("ambient filesystem paths"), "{}", text(&ambient)); + client.cancel().await.unwrap(); + cleanup(&base); +} + /// #3216: the export half of #2810. The write root scopes both exports, the read root the CSV, /// and the three ambient commands keep their refusal. #[tokio::test(flavor = "multi_thread")] @@ -725,17 +804,55 @@ async fn doc_exports_and_data_set_import_are_capability_scoped() { cleanup(&base); } +/// Skipped-layer details survive the MCP tool result, including the all-skipped case. +#[tokio::test(flavor = "multi_thread")] +async fn doc_export_layers_reports_skipped_layers() { + let base = tmp("skipped-layers"); + let client = connect(headless_in(&base)).await; + json_of(&call(&client, "doc_new", json!({"width": 4, "height": 4, "background": "white", "name": "card"})).await); + let background = json_of(&call(&client, "doc_inspect", json!({})).await)["layers"][0]["id"].clone(); + let adjustment = json_of(&call(&client, "command_run", json!({"id": "layer.newAdjustmentLayer.levels"})).await)["layer"].clone(); + json_of(&call(&client, "command_run", json!({"id": "layer.setProps", "params": {"name": "Levels/α"}})).await); + let hidden = json_of(&call(&client, "command_run", json!({"id": "layer.new.layer", "params": {"name": "Hidden"}})).await)["layer"].clone(); + json_of(&call(&client, "command_run", json!({"id": "layer.setProps", "params": {"visible": false}})).await); + let result = json_of(&call(&client, "doc_export_layers", json!({"dir": "layers"})).await); + assert_eq!(result["count"], 1); + assert_eq!(result["files"], json!(["layers/card_0001_Background.png"])); + assert_eq!( + result["skipped"], + json!([ + {"layer": hidden, "name": "Hidden", "reason": "hidden by visibleOnly"}, + {"layer": adjustment, "name": "Levels/α", "reason": "adjustment layer has no pixels of its own"} + ]) + ); + let all = json_of(&call(&client, "doc_export_layers", json!({"dir": "all", "visible_only": false})).await); + assert_eq!(all["count"], 2); + assert_eq!(all["skipped"].as_array().unwrap().len(), 1); + assert_eq!(std::fs::read_dir(base.join("layers")).unwrap().count(), 1); + assert_eq!(std::fs::read_dir(base.join("all")).unwrap().count(), 2); + // Every top-level layer is still accounted for when none can be exported. + json_of(&call(&client, "command_run", json!({"id": "layer.setProps", "params": {"layer": background, "visible": false}})).await); + let none = json_of(&call(&client, "doc_export_layers", json!({"dir": "none"})).await); + assert_eq!(none["files"], json!([])); + assert_eq!(none["count"], 0); + assert_eq!(none["skipped"].as_array().unwrap().len(), 3); + assert_eq!(std::fs::read_dir(base.join("none")).unwrap().count(), 0); + client.cancel().await.unwrap(); + cleanup(&base); +} + /// The template workflow stays in memory, including saving nested contents back to the parent. #[tokio::test(flavor = "multi_thread")] async fn embedded_smart_objects_can_be_edited_saved_and_unpacked_without_file_authority() { let client = connect(PhotocraftMcp::headless()).await; let run = async |id: &str, params: Value| json_of(&call(&client, "command_run", json!({"id": id, "params": params})).await); json_of(&call(&client, "doc_new", json!({"width": 16, "height": 12, "background": "white"})).await); - run("layer.smartObjects.convertToSmartObject", json!({})).await; let outer = run("layer.smartObjects.convertToSmartObject", json!({})).await["layer"].clone(); // An explicit target works even with a regular pixel layer selected. run("layer.new.layer", json!({"name": "Other layer"})).await; assert_eq!(run("layer.smartObjects.editContents", json!({"layer": outer})).await["document"], 1); + // A smart object cannot be converted again, so nest one inside the contents instead. + run("layer.smartObjects.convertToSmartObject", json!({})).await; assert_eq!(run("layer.smartObjects.editContents", json!({})).await["document"], 2); run("layer.setProps", json!({"name": "Edited photo", "opacity": 0.5})).await; assert_eq!(run("layer.smartObjects.saveContents", json!({})).await["updated"], true); @@ -999,6 +1116,26 @@ async fn bridge_previews_reject_document_indices() { assert_eq!(schema.get("additionalProperties"), Some(&json!(false))); } +/// Bridge mode runs Replace Contents on the app's active document, so an `index` is refused +/// before anything reaches the app rather than dropped (it would replace another document's +/// contents); without one the command is forwarded as before. +#[tokio::test(flavor = "multi_thread")] +async fn bridge_replace_smart_object_rejects_a_document_index() { + let (addr, app) = fake_app().await; + let client = connect(PhotocraftMcp::bridge(&addr, CONTROL_TOKEN).unwrap()).await; + let refused = call(&client, "doc_replace_smart_object", json!({"path": "new.png", "index": 1})).await; + assert_eq!(refused.is_error, Some(true), "{}", text(&refused)); + assert!(text(&refused).contains("index"), "{}", text(&refused)); + let forwarded = call(&client, "doc_replace_smart_object", json!({"path": "new.png", "layer": 7})).await; + assert_ne!(forwarded.is_error, Some(true), "{}", text(&forwarded)); + client.cancel().await.unwrap(); + let seen = tokio::time::timeout(std::time::Duration::from_secs(5), app).await.unwrap().unwrap(); + let executed: Vec<&Value> = seen.iter().filter(|request| request["method"] == "engine.execute").collect(); + assert_eq!(executed.len(), 1, "the indexed call must not reach the app: {seen:?}"); + assert_eq!(executed[0]["params"]["command"], "layer.smartObjects.replaceContents"); + assert_eq!(executed[0]["params"]["params"], json!({"path": "new.png", "layer": 7})); +} + #[tokio::test(flavor = "multi_thread")] async fn headless_previews_preserve_document_indices() { let client = connect(PhotocraftMcp::headless()).await; diff --git a/crates/automation/tests/mcp_wire.rs b/crates/automation/tests/it/mcp_wire.rs similarity index 100% rename from crates/automation/tests/mcp_wire.rs rename to crates/automation/tests/it/mcp_wire.rs diff --git a/crates/automation/tests/missing_font_warnings.rs b/crates/automation/tests/it/missing_font_warnings.rs similarity index 100% rename from crates/automation/tests/missing_font_warnings.rs rename to crates/automation/tests/it/missing_font_warnings.rs diff --git a/crates/automation/tests/native_save_name.rs b/crates/automation/tests/it/native_save_name.rs similarity index 100% rename from crates/automation/tests/native_save_name.rs rename to crates/automation/tests/it/native_save_name.rs diff --git a/crates/automation/tests/openraster.rs b/crates/automation/tests/it/openraster.rs similarity index 100% rename from crates/automation/tests/openraster.rs rename to crates/automation/tests/it/openraster.rs diff --git a/crates/automation/tests/it/raw_defaults.rs b/crates/automation/tests/it/raw_defaults.rs new file mode 100644 index 0000000000..0f12e06c5e --- /dev/null +++ b/crates/automation/tests/it/raw_defaults.rs @@ -0,0 +1,20 @@ +//! #7789: the headless (MCP) `doc.open` opens a developed raw at Preferences › Raw Defaults › +//! Resolution, as the app's File › Open does. + +use photocraft_automation::Headless; +use photocraft_raw::testgen::{DngSpec, mosaic, scene}; +use serde_json::json; + +#[test] +fn doc_open_gives_a_raw_the_raw_defaults_resolution() { + let dir = std::env::temp_dir().join(format!("pc-raw-defaults-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (w, h) = (24, 16); + let path = dir.join("IMG_0001.dng"); + std::fs::write(&path, DngSpec::cfa(w, h, mosaic(&scene(w, h), w, [0, 1, 1, 2], 0, 65535)).build()).unwrap(); + let mut headless = Headless::trusted_local(); + headless.handle("engine.execute", json!({"command": "prefs.set", "params": {"path": "rawDefaults.resolution", "value": 240}})).unwrap(); + headless.handle("doc.open", json!({"path": path.to_str().unwrap()})).unwrap(); + assert_eq!(headless.session.active().unwrap().doc.resolution_dpi, 240.0); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/automation/tests/smart_filter_authorization.rs b/crates/automation/tests/it/smart_filter_authorization.rs similarity index 63% rename from crates/automation/tests/smart_filter_authorization.rs rename to crates/automation/tests/it/smart_filter_authorization.rs index 30f17b795b..0a39df85f5 100644 --- a/crates/automation/tests/smart_filter_authorization.rs +++ b/crates/automation/tests/it/smart_filter_authorization.rs @@ -1,94 +1,15 @@ //! Stored smart-filter parameters must not grant automation ambient filesystem access. -use std::path::{Path, PathBuf}; -use std::sync::{ - Arc, - atomic::{AtomicU64, Ordering}, -}; +use std::path::Path; +use std::sync::Arc; -use photocraft_automation::{AuthorizedWorkspace, Headless, workspace::authorize_desktop_engine_step}; -use photocraft_doc::{Document, LayerContent, LayerId}; +#[path = "../support/smart_filter_fixture.rs"] +mod fixture; + +use fixture::{Fixture, assert_denied, cube, path_params}; +use photocraft_automation::{Headless, workspace::authorize_desktop_engine_step}; +use photocraft_doc::LayerContent; use photocraft_engine::{Session, actions_cmds::Action, jobs::Started, smart_cmds}; -use serde_json::{Value, json}; - -struct Fixture(PathBuf); - -impl Fixture { - fn new() -> Self { - static NEXT: AtomicU64 = AtomicU64::new(0); - let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let base = std::env::temp_dir().join(format!("pc-filter-auth-{}-{stamp}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))); - std::fs::create_dir(&base).unwrap(); - std::fs::create_dir(base.join("workspace")).unwrap(); - std::fs::create_dir(base.join("outside")).unwrap(); - let f = Self(base); - std::fs::write(f.root().join("stripes.png"), png(|x| if (x / 2) % 2 == 0 { 0 } else { 255 })).unwrap(); - std::fs::write(f.outside("black.png"), png(|_| 0)).unwrap(); - std::fs::write(f.outside("white.png"), png(|_| 255)).unwrap(); - std::fs::write(f.outside("black.cube"), cube(0)).unwrap(); - std::fs::write(f.outside("white.cube"), cube(1)).unwrap(); - f - } - - fn root(&self) -> PathBuf { - self.0.join("workspace") - } - fn outside(&self, name: &str) -> PathBuf { - self.0.join("outside").join(name) - } - fn headless(&self) -> Headless { - Headless::with_workspace(AuthorizedWorkspace::new(Some(&self.root()), Some(&self.root())).unwrap()) - } - fn setup(&self, lookup: bool) -> (Headless, LayerId) { - let mut h = self.headless(); - h.handle("doc.new", json!({"width": 32, "height": 32, "background": "#808080"})).unwrap(); - h.handle("doc.new", json!({"width": 32, "height": 32})).unwrap(); - let layer = LayerId(h.handle("doc.place", json!({"path": "stripes.png"})).unwrap()["layer"].as_u64().unwrap()); - let (id, params) = if lookup { - ("image.adjustments.colorLookup", json!({"layer": layer.0, "lut": "none"})) - } else { - ("filter.distort.displace", json!({"layer": layer.0, "mapDocument": 0, "horizontal": 50, "vertical": 0})) - }; - h.session.execute(id, params).unwrap(); - (h, layer) - } - fn saved_filter(&self, lookup: bool) -> (Document, LayerId) { - let (h, layer) = self.setup(lookup); - let mut doc = (*h.session.active().unwrap().doc).clone(); - let LayerContent::Smart(sm) = &mut doc.layer_mut(layer).unwrap().content else { panic!("smart object") }; - sm.smart_filters[0].params = path_params(lookup, &self.outside(if lookup { "white.cube" } else { "white.png" })); - let bytes = photocraft_format::save_to_bytes(&doc, &Default::default()).unwrap(); - std::fs::write(self.root().join("stored.pcraft"), bytes).unwrap(); - (doc, layer) - } -} -impl Drop for Fixture { - fn drop(&mut self) { - let _ = std::fs::remove_dir_all(&self.0); - } -} - -fn png(value: impl Fn(u32) -> u8) -> Vec { - let mut pixels = Vec::new(); - for _ in 0..32 { - for x in 0..32 { - pixels.extend([value(x); 3]); - } - } - let image = photocraft_codecs::Image::from_u8(32, 32, photocraft_codecs::ChannelLayout::Rgb, pixels).unwrap(); - photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap() -} -fn cube(value: u8) -> String { - format!("LUT_3D_SIZE 2\n{}", format!("{value} {value} {value}\n").repeat(8)) -} -fn path_params(lookup: bool, path: &Path) -> Value { - if lookup { json!({"file": path}) } else { json!({"mapPath": path, "mapDocument": null}) } -} -fn assert_denied(result: Result) { - match result { - Err(error) => assert!(error.to_string().contains("ambient filesystem"), "{error}"), - Ok(value) => panic!("ambient file access was accepted: {value:?}"), - } -} +use serde_json::json; #[test] fn parameter_edits_reject_outside_images_and_luts_without_changing_the_document() { @@ -243,17 +164,25 @@ fn a_refresh_outside_command_execution_surfaces_the_denial_instead_of_rendering_ } } +/// Choosing another Displace map through `setParams` embeds it (#7423), so the new map is read +/// before the edit: a path outside the workspace is refused before that read, and the filter +/// keeps the map it had. #[test] -fn an_ambient_map_or_lut_never_renders_unfiltered_pixels_into_the_stack_cache() { - // The denial path must fail the render before any stage is cached process-wide. - for lookup in [false, true] { - let f = Fixture::new(); - let (doc, layer) = f.saved_filter(lookup); - let mut s = Session::new(); - s.add_document(doc, None); - s.authorize = Some(authorize_desktop_engine_step); - let bytes = smart_cmds::stack_cache_bytes(); - assert_denied(s.execute("layer.smartFilter.setVisible", json!({"layer": layer.0, "visible": true}))); - assert_eq!(bytes, smart_cmds::stack_cache_bytes(), "a denied render cached no stage"); - } +fn a_new_displace_map_outside_the_workspace_is_refused_before_it_is_read() { + let f = Fixture::new(); + let (mut h, layer) = f.setup(false); + let params = |h: &Headless| { + let d = h.session.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(layer).unwrap().content else { panic!("smart object") }; + sm.smart_filters[0].params.clone() + }; + let before = params(&h); + let map = photocraft_io::smart_map::displace_embedded_map(&before).expect("the open document's map, embedded"); + assert!(map.data.iter().all(|v| *v < 0.9), "the grey map"); + let doc = h.session.active().unwrap().doc.clone(); + let edit = json!({"layer": layer.0, "params": {"mapPath": f.outside("white.png")}}); + assert_denied(h.session.execute("layer.smartFilter.setParams", edit.clone())); + assert_denied(h.handle("engine.execute", json!({"command": "layer.smartFilter.setParams", "params": edit}))); + assert!(Arc::ptr_eq(&doc, &h.session.active().unwrap().doc)); + assert_eq!(params(&h), before, "no white map was read into the filter"); } diff --git a/crates/automation/tests/smart_filter_stack_cache.rs b/crates/automation/tests/smart_filter_stack_cache.rs new file mode 100644 index 0000000000..f7698a4db1 --- /dev/null +++ b/crates/automation/tests/smart_filter_stack_cache.rs @@ -0,0 +1,25 @@ +//! One integration-test process keeps the global smart filter stage cache isolated from the +//! parallel tests in `tests/it`, so a denied render can be shown to have cached nothing. +use photocraft_automation::workspace::authorize_desktop_engine_step; +use photocraft_engine::{Session, smart_cmds}; +use serde_json::json; + +#[path = "support/smart_filter_fixture.rs"] +mod fixture; + +use fixture::{Fixture, assert_denied}; + +#[test] +fn an_ambient_map_or_lut_never_renders_unfiltered_pixels_into_the_stack_cache() { + // The denial path must fail the render before any stage is cached process-wide. + for lookup in [false, true] { + let f = Fixture::new(); + let (doc, layer) = f.saved_filter(lookup); + let mut s = Session::new(); + s.add_document(doc, None); + s.authorize = Some(authorize_desktop_engine_step); + let bytes = smart_cmds::stack_cache_bytes(); + assert_denied(s.execute("layer.smartFilter.setVisible", json!({"layer": layer.0, "visible": true}))); + assert_eq!(bytes, smart_cmds::stack_cache_bytes(), "a denied render cached no stage"); + } +} diff --git a/crates/automation/tests/support/smart_filter_fixture.rs b/crates/automation/tests/support/smart_filter_fixture.rs new file mode 100644 index 0000000000..26d5cab27b --- /dev/null +++ b/crates/automation/tests/support/smart_filter_fixture.rs @@ -0,0 +1,89 @@ +//! The smart filter authorization tests' fixture: a workspace with a striped image to place and +//! maps and LUTs outside it (`#[path]`-included by `it/smart_filter_authorization.rs` and +//! `smart_filter_stack_cache.rs`). +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +use photocraft_automation::{AuthorizedWorkspace, Headless}; +use photocraft_doc::{Document, LayerContent, LayerId}; +use serde_json::{Value, json}; + +pub struct Fixture(PathBuf); + +impl Fixture { + pub fn new() -> Self { + static NEXT: AtomicU64 = AtomicU64::new(0); + let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); + let base = std::env::temp_dir().join(format!("pc-filter-auth-{}-{stamp}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))); + std::fs::create_dir(&base).unwrap(); + std::fs::create_dir(base.join("workspace")).unwrap(); + std::fs::create_dir(base.join("outside")).unwrap(); + let f = Self(base); + std::fs::write(f.root().join("stripes.png"), png(|x| if (x / 2) % 2 == 0 { 0 } else { 255 })).unwrap(); + std::fs::write(f.outside("black.png"), png(|_| 0)).unwrap(); + std::fs::write(f.outside("white.png"), png(|_| 255)).unwrap(); + std::fs::write(f.outside("black.cube"), cube(0)).unwrap(); + std::fs::write(f.outside("white.cube"), cube(1)).unwrap(); + f + } + + pub fn root(&self) -> PathBuf { + self.0.join("workspace") + } + pub fn outside(&self, name: &str) -> PathBuf { + self.0.join("outside").join(name) + } + pub fn headless(&self) -> Headless { + Headless::with_workspace(AuthorizedWorkspace::new(Some(&self.root()), Some(&self.root())).unwrap()) + } + pub fn setup(&self, lookup: bool) -> (Headless, LayerId) { + let mut h = self.headless(); + h.handle("doc.new", json!({"width": 32, "height": 32, "background": "#808080"})).unwrap(); + h.handle("doc.new", json!({"width": 32, "height": 32})).unwrap(); + let layer = LayerId(h.handle("doc.place", json!({"path": "stripes.png"})).unwrap()["layer"].as_u64().unwrap()); + let (id, params) = if lookup { + ("image.adjustments.colorLookup", json!({"layer": layer.0, "lut": "none"})) + } else { + ("filter.distort.displace", json!({"layer": layer.0, "mapDocument": 0, "horizontal": 50, "vertical": 0})) + }; + h.session.execute(id, params).unwrap(); + (h, layer) + } + pub fn saved_filter(&self, lookup: bool) -> (Document, LayerId) { + let (h, layer) = self.setup(lookup); + let mut doc = (*h.session.active().unwrap().doc).clone(); + let LayerContent::Smart(sm) = &mut doc.layer_mut(layer).unwrap().content else { panic!("smart object") }; + sm.smart_filters[0].params = path_params(lookup, &self.outside(if lookup { "white.cube" } else { "white.png" })); + let bytes = photocraft_format::save_to_bytes(&doc, &Default::default()).unwrap(); + std::fs::write(self.root().join("stored.pcraft"), bytes).unwrap(); + (doc, layer) + } +} +impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +pub fn png(value: impl Fn(u32) -> u8) -> Vec { + let mut pixels = Vec::new(); + for _ in 0..32 { + for x in 0..32 { + pixels.extend([value(x); 3]); + } + } + let image = photocraft_codecs::Image::from_u8(32, 32, photocraft_codecs::ChannelLayout::Rgb, pixels).unwrap(); + photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap() +} +pub fn cube(value: u8) -> String { + format!("LUT_3D_SIZE 2\n{}", format!("{value} {value} {value}\n").repeat(8)) +} +pub fn path_params(lookup: bool, path: &Path) -> Value { + if lookup { json!({"file": path}) } else { json!({"mapPath": path, "mapDocument": null}) } +} +pub fn assert_denied(result: Result) { + match result { + Err(error) => assert!(error.to_string().contains("ambient filesystem"), "{error}"), + Ok(value) => panic!("ambient file access was accepted: {value:?}"), + } +} diff --git a/crates/cms/README.md b/crates/cms/README.md index c48d5366fd..b67eb6e8a8 100644 --- a/crates/cms/README.md +++ b/crates/cms/README.md @@ -20,11 +20,15 @@ t.eval(&[1.0, 0.0, 0.0], &mut out); // exact single colou * `Transform::proof(src, proof, display, intent, bpc, simulate_paper)`: soft-proof chain. * `GamutCheck::new(src, proof, ΔE)`: out-of-gamut test (gamut warning). * `Lut3d::from_transform(&t, 33).to_rgba16f_bytes()`: display transform as a 3D texture. +* `Transform::gpu_matrix_shaper()`: accurate RGB input curves + matrix + output curves for + display shaders. Float tables retain near-black detail; unsupported chains return `None` + for the 3D LUT fallback. No GPU dependency is required by this crate. * `transform::cached(src, dst, opts)`: process-wide transform cache. * `Profile::to_bytes()`: original bytes of parsed profiles (byte-exact), or an ICC v4.3 encoding. * `Profile::same_colors(&other)`: the same colour space whatever the encoding (every sample of a device grid within ΔE 0.5 through both), e.g. Photoshop's sRGB IEC61966-2.1 and the built-in - sRGB. `content_hash()` compares bytes and suits cache keys only. + sRGB. `same_bytes(&other)` compares the ICC bytes. `content_hash()` is a 64-bit hash of them + that another profile can share: a cache key, confirmed with `same_bytes`. ### Evaluation @@ -36,6 +40,10 @@ t.eval(&[1.0, 0.0, 0.0], &mut out); // exact single colou tetrahedral interpolation (4D: linear on the first input between two tetrahedra); purely analytic chains (e.g. Lab ↔ RGB) are evaluated exactly. Buffers ≥ 16k pixels run on rayon (native only). +* GPU matrix/TRC display transforms reuse the 4097-entry input curves, 3×3 matrix plus + offsets, and 4096-entry output curves indexed by `x^(1/4)`. Each channel is interpolated + separately using float texels; SDR input/output are clamped to `[0, 1]`. A final RGB + 33³ table cannot resolve steep inverse gamma curves near black, even with float texels. * Rendering intents: AToB0/1/2 and BToA0/1/2 by intent with AToB0/BToA0 fallback; absolute colorimetric scales by media white (`wtpt`; v2 display profiles treated as D50; a zero, negative or non-finite white is refused with `CmsError::Invalid`); BPC uses @@ -68,7 +76,7 @@ sampled gamut boundary. `profiles/photocraft-coated-cmyk.icc` (237 KB, AToB 11 21³ lut16) is that output, dedicated to the public domain (CC0-1.0). Regenerate with ```sh -PHOTOCRAFT_REGEN_PROFILES=1 cargo test -p photocraft-cms --release --test regen +PHOTOCRAFT_REGEN_PROFILES=1 cargo test -p photocraft-cms --release --test it -- regen:: ``` It is a plausible coated-offset profile, not a measured characterisation: use your printer's diff --git a/crates/cms/src/builtin.rs b/crates/cms/src/builtin.rs index 8f3dc88a15..30a31aceb5 100644 --- a/crates/cms/src/builtin.rs +++ b/crates/cms/src/builtin.rs @@ -261,13 +261,60 @@ fn lab() -> Profile { /// with `PHOTOCRAFT_REGEN_PROFILES=1 cargo test -p photocraft-cms --release regen`). pub const COATED_CMYK_ICC: &[u8] = include_bytes!("../profiles/photocraft-coated-cmyk.icc"); +/// Rec. 709 / sRGB primaries (xy). +const BT709: [[f64; 2]; 3] = [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06]]; +/// DCI-P3 primaries (xy), used with D65 by Display P3. +const P3: [[f64; 2]; 3] = [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]]; +/// Rec. 2020 primaries (xy). +const BT2020: [[f64; 2]; 3] = [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]]; + +/// Rec. 709 OETF inverse: V = 1.099 L^0.45 − 0.099 (L ≥ 0.018), 4.5 L below. +fn rec709_trc() -> Curve { + let a = 1.099; + Curve::Parametric { kind: 3, p: [1.0 / 0.45, 1.0 / a, (a - 1.0) / a, 1.0 / 4.5, 0.081, 0.0, 0.0] } +} + +/// The RGB profile for an ITU-T H.273 (CICP) colour description, as a HEIF `nclx` or a video +/// stream declares it: BT.709, BT.2020 or P3-D65 primaries (codes 1, 9, 12) with the sRGB, +/// BT.709, linear or gamma 2.2/2.8 transfer (13; 1, 6, 14, 15; 8; 4; 5). `linear` means the +/// samples were already decoded to linear light, which is how PQ and HLG (16, 18) images are +/// held, since no ICC curve represents those transfers. `None` for anything else. +pub fn cicp_rgb(primaries: u16, transfer: u16, linear: bool) -> Option { + let (prim, name) = match primaries { + 1 => (BT709, "BT.709"), + 9 => (BT2020, "Rec. ITU-R BT.2020"), + 12 => (P3, "Display P3"), + _ => return None, + }; + let transfer = if linear { 8 } else { transfer }; + let builtin = match (primaries, transfer) { + (1, 13) => Some(Builtin::Srgb), + (1, 8) => Some(Builtin::LinearSrgb), + (12, 13) => Some(Builtin::DisplayP3), + (9, 1 | 6 | 14 | 15) => Some(Builtin::Rec2020), + _ => None, + }; + if let Some(b) = builtin { + return Some(b.profile().clone()); + } + let (trc, curve) = match transfer { + 8 => (Curve::Gamma(1.0), "linear"), + 13 => (srgb_trc(), "sRGB curve"), + 1 | 6 | 14 | 15 => (rec709_trc(), "BT.709 curve"), + 4 => (Curve::Gamma(2.2), "gamma 2.2"), + 5 => (Curve::Gamma(2.8), "gamma 2.8"), + _ => return None, + }; + Some(rgb(&format!("{name} {curve} (PhotoCraft)"), prim, d65(), trc)) +} + fn build(b: Builtin) -> Profile { let (srgb_p, p3, adobe, romm, bt2020, ap1) = ( - [[0.64, 0.33], [0.30, 0.60], [0.15, 0.06]], - [[0.680, 0.320], [0.265, 0.690], [0.150, 0.060]], + BT709, + P3, [[0.64, 0.33], [0.21, 0.71], [0.15, 0.06]], [[0.7347, 0.2653], [0.1596, 0.8404], [0.0366, 0.0001]], - [[0.708, 0.292], [0.170, 0.797], [0.131, 0.046]], + BT2020, [[0.713, 0.293], [0.165, 0.830], [0.128, 0.044]], ); match b { @@ -279,12 +326,7 @@ fn build(b: Builtin) -> Profile { // ACEScg is scene-linear on the AP1 primaries with the ACES white point (~D60, // xy 0.32168/0.33767, the ACES 2065-1 neutral). Builtin::AcesCg => rgb(b.description(), ap1, math::xy_to_xyz([0.32168, 0.33767]), Curve::Gamma(1.0)), - Builtin::Rec2020 => { - // Rec. 709 OETF inverse: V = 1.099 L^0.45 − 0.099 (L ≥ 0.018), 4.5 L below. - let a = 1.099; - let trc = Curve::Parametric { kind: 3, p: [1.0 / 0.45, 1.0 / a, (a - 1.0) / a, 1.0 / 4.5, 0.081, 0.0, 0.0] }; - rgb(b.description(), bt2020, d65(), trc) - } + Builtin::Rec2020 => rgb(b.description(), bt2020, d65(), rec709_trc()), Builtin::GrayGamma22 => gray(b.description(), Curve::Gamma(2.2)), Builtin::SGray => gray(b.description(), srgb_trc()), Builtin::DotGain20Compat => gray(b.description(), Curve::Gamma(1.732)), @@ -294,3 +336,30 @@ fn build(b: Builtin) -> Profile { Builtin::CoatedCmyk => Profile::parse(COATED_CMYK_ICC).unwrap_or_else(|_| crate::synth::coated_cmyk()), } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cicp_maps_onto_the_builtins_and_linear_bt2020() { + let desc = |p: Option| p.map(|p| p.description); + assert_eq!(desc(cicp_rgb(1, 13, false)), Some(Builtin::Srgb.description().to_string())); + assert_eq!(desc(cicp_rgb(12, 13, false)), Some(Builtin::DisplayP3.description().to_string())); + assert_eq!(desc(cicp_rgb(9, 14, false)), Some(Builtin::Rec2020.description().to_string())); + assert_eq!(desc(cicp_rgb(1, 18, true)), Some(Builtin::LinearSrgb.description().to_string())); + + // An HLG BT.2020 image decoded to linear light: BT.2020's colorants, a linear curve. + let hlg = cicp_rgb(9, 18, true).unwrap(); + assert_eq!(hlg.trc, Some([Curve::Gamma(1.0), Curve::Gamma(1.0), Curve::Gamma(1.0)])); + assert_eq!(hlg.matrix, Builtin::Rec2020.profile().matrix); + assert!(hlg.description.contains("BT.2020") && hlg.description.contains("linear"), "{}", hlg.description); + assert!(Profile::parse(&hlg.to_bytes()).is_ok(), "encodes as a valid ICC profile"); + + // HLG and PQ have no ICC curve; unknown primaries have no profile. + assert!(cicp_rgb(9, 18, false).is_none()); + assert!(cicp_rgb(9, 16, false).is_none()); + assert!(cicp_rgb(2, 13, false).is_none()); + assert!(cicp_rgb(u16::MAX, u16::MAX, true).is_none()); + } +} diff --git a/crates/cms/src/clut.rs b/crates/cms/src/clut.rs index 186cd3f9f1..38992a2ed4 100644 --- a/crates/cms/src/clut.rs +++ b/crates/cms/src/clut.rs @@ -172,11 +172,16 @@ impl Clut { } let o = self.outputs; out[..o].iter_mut().for_each(|v| *v = 0.0); - for corner in 0..(1usize << n) { + // Only dimensions with more than one grid point contribute a cube axis. Degenerate + // dimensions have step 0 and ratio 0, so they always multiply the corner weight by 1 + // and never change the offset; enumerating 2^n corners over all declared inputs would + // do 2^(degenerate) redundant iterations. Interpolate over the effective dims only. + let eff: Vec = (0..n).filter(|&d| s[d] != 0).collect(); + for corner in 0..(1usize << eff.len()) { let mut w = 1.0f32; let mut off = base; - for d in 0..n { - if corner & (1 << d) != 0 { + for (bit, &d) in eff.iter().enumerate() { + if corner & (1 << bit) != 0 { w *= r[d]; off += s[d]; } else { @@ -245,4 +250,20 @@ mod tests { c.eval(&[0.5, 0.25], &mut o); assert!((o[0] - 0.75).abs() < 1e-6); } + + #[test] + fn degenerate_dimensions_do_not_change_the_result() { + // Dimensions with a single grid point contribute nothing, but the old code still + // enumerated 2^inputs corners over them. Interpolating over effective dims only must + // give the same answer as a reference built without the degenerate axes. + let f = |p: &[f32], o: &mut [f32]| o[0] = 0.25 * p[0] + 0.5 * p[2] + 0.125 * p[4]; + // Axes 1 and 3 have a single grid point (degenerate); axes 0, 2, 4 vary. + let c = Clut::sample(vec![3, 1, 4, 1, 5], 1, f); + let mut got = [0.0]; + for p in [[0.2f32, 0.0, 0.7, 0.0, 0.4], [1.0, 0.0, 0.0, 0.0, 1.0], [0.5, 0.0, 0.5, 0.0, 0.5]] { + c.eval_multilinear(&p, &mut got); + let want = 0.25 * p[0] + 0.5 * p[2] + 0.125 * p[4]; + assert!((got[0] - want).abs() < 1e-5, "got {} want {want}", got[0]); + } + } } diff --git a/crates/cms/src/curve.rs b/crates/cms/src/curve.rs index fe23e6e059..963ae3cdb5 100644 --- a/crates/cms/src/curve.rs +++ b/crates/cms/src/curve.rs @@ -99,6 +99,20 @@ impl Curve { (0..n).map(|i| self.eval64(i as f64 / (n - 1) as f64) as f32).collect() } + /// Inverse of the curve at each `y` in `ys`. For sampled-table curves the flat-run edge + /// indices are computed once for the whole batch instead of rescanning the table on every + /// call, so inverting a long constant table many times stays linear. + pub fn inverse_table(&self, ys: impl Iterator) -> Vec { + match self { + Curve::Table(t) if t.len() >= 2 => { + let increasing = t[t.len() - 1] >= t[0]; + let (edge_start, edge_end) = (edge_index(t, true), edge_index(t, false)); + ys.map(|y| table_inverse_with(t, y as f32, increasing, edge_start, edge_end)).collect() + } + _ => ys.map(|y| self.eval_inverse64(y) as f32).collect(), + } + } + /// `true` when the curve never decreases over `[0, 1]`. pub fn is_monotonic(&self) -> bool { let s = self.sample(1024); @@ -244,13 +258,24 @@ fn table_inverse(t: &[f32], y: f32) -> f32 { return y; } let increasing = t[n - 1] >= t[0]; + table_inverse_with(t, y, increasing, edge_index(t, true), edge_index(t, false)) +} + +/// [`table_inverse`] with the flat-run edge indices supplied. Computing those edges scans the +/// whole table, and transform construction inverts the same table thousands of times, so the +/// edges are computed once and reused across a batch (see [`Curve::inverse_table`]). +fn table_inverse_with(t: &[f32], y: f32, increasing: bool, edge_start: f32, edge_end: f32) -> f32 { + let n = t.len(); + if n < 2 { + return y; + } let (lo_v, hi_v) = if increasing { (t[0], t[n - 1]) } else { (t[n - 1], t[0]) }; if y <= lo_v { // Flat start: choose the last index still at the lowest value. - return if increasing { edge_index(t, true) } else { edge_index(t, false) }; + return if increasing { edge_start } else { edge_end }; } if y >= hi_v { - return if increasing { edge_index(t, false) } else { edge_index(t, true) }; + return if increasing { edge_end } else { edge_start }; } let (mut lo, mut hi) = (0usize, n - 1); while hi - lo > 1 { @@ -268,15 +293,22 @@ fn table_inverse(t: &[f32], y: f32) -> f32 { /// For a flat run at the table start (`start = true`) the last index of the run, or for a flat /// run at the end the first index of it, as a normalised input. +/// +/// A table that is inverted is monotonic, so the run is found by bisection, not by a scan: a long +/// constant table is inverted once per pixel, and scanning it each time made the cost the curve's +/// length times the image's. fn edge_index(t: &[f32], start: bool) -> f32 { let n = t.len(); if start { let v = t[0]; - let i = t.iter().position(|x| (*x - v).abs() > 1e-7).map_or(n - 1, |i| i - 1); + // The first value that differs, given they do so from there on. + let first_other = t.partition_point(|x| (*x - v).abs() <= 1e-7); + let i = if first_other >= n { n - 1 } else { first_other.saturating_sub(1) }; i as f32 / (n - 1) as f32 } else { let v = t[n - 1]; - let i = t.iter().rposition(|x| (*x - v).abs() > 1e-7).map_or(0, |i| i + 1); + // The start of the trailing run equal to the last value. + let i = t.partition_point(|x| (*x - v).abs() > 1e-7); i as f32 / (n - 1) as f32 } } @@ -336,4 +368,39 @@ mod tests { let g = Curve::Gamma(2.2); assert!((g.eval64(-0.5) + 0.5f64.powf(2.2)).abs() < 1e-12); } + + #[test] + fn inverting_a_long_flat_table_does_not_rescan_it_for_every_pixel() { + // Four million zeros and a one: a scan per call would be 8·10¹¹ steps for these calls. + let mut t = vec![0.0f32; 4_000_000]; + t.push(1.0); + let c = Curve::Table(t); + let started = std::time::Instant::now(); + let mut last = 0.0; + for _ in 0..200_000 { + last = c.eval_inverse(0.0); + } + assert!(last > 0.99, "the flat start ends at the last zero: {last}"); + assert!(started.elapsed().as_secs() < 10, "{:?}", started.elapsed()); + } + + #[test] + fn inverse_table_matches_per_call_including_flat_runs() { + // Batch inverse must equal calling eval_inverse64 per point. Include a table with flat + // runs at both ends (the case that used to rescan the table on every call). + let tables = [ + Curve::Table(vec![0.0, 0.0, 0.0, 0.25, 0.5, 1.0, 1.0, 1.0]), + Curve::Table(vec![0.0, 0.2, 0.4, 0.6, 0.8, 1.0]), + Curve::Table(vec![1.0, 0.5, 0.0]), + Curve::Gamma(2.2), + ]; + let ys: Vec = (0..=40).map(|i| i as f64 / 40.0).collect(); + for c in &tables { + let batch = c.inverse_table(ys.iter().copied()); + for (i, &y) in ys.iter().enumerate() { + let one = c.eval_inverse64(y) as f32; + assert!((batch[i] - one).abs() < 1e-6, "y={y}: batch {} vs per-call {one}", batch[i]); + } + } + } } diff --git a/crates/cms/src/gpu_matrix_shaper.rs b/crates/cms/src/gpu_matrix_shaper.rs new file mode 100644 index 0000000000..09631bf98d --- /dev/null +++ b/crates/cms/src/gpu_matrix_shaper.rs @@ -0,0 +1,131 @@ +//! Separable RGB display transforms without a coarse 3D interpolation near black. + +/// RGB input curves, a 3×3 matrix with offsets, and RGB output curves for a GPU display +/// transform. Values are floats independent of the document's pixel depth. +/// +/// The input table is sampled linearly at `clamp(rgb, 0, 1) * INPUT_INTERVALS`. Apply +/// [`Self::matrix_rows`] to that result, then sample the output table at +/// `clamp(linear_rgb, 0, 1)^(1/4) * (OUTPUT_ENTRIES - 1)`. Interpolate each channel +/// separately between its two adjacent table entries. These nonuniform output samples +/// retain shadow detail where a pure gamma curve's inverse has an infinite slope. +/// +/// Tables contain RGB values and a padding alpha of 1, suitable for RGBA32Float texels. +/// Use unfiltered texture loads and manual interpolation if float filtering is unavailable. +/// This representation covers SDR display output; input and output are clamped to `[0, 1]`. +/// Construction validates all sizes and values; arbitrary CLUT/proofing chains retain the +/// [`crate::Lut3d`] fallback. +#[derive(Clone, Debug, PartialEq)] +pub struct GpuMatrixShaper { + matrix: [[f32; 4]; 3], + input: Vec<[f32; 4]>, + output: Vec<[f32; 4]>, +} + +impl GpuMatrixShaper { + /// The input table has one more entry than this number of equal intervals. + pub const INPUT_INTERVALS: usize = 4096; + /// Output entries are spaced uniformly in the fourth root of the linear value. + pub const OUTPUT_ENTRIES: usize = 4096; + + /// Row-major matrix: each row contains three RGB coefficients followed by its offset. + pub fn matrix_rows(&self) -> [[f32; 4]; 3] { + self.matrix + } + + /// RGB input curves at `i / INPUT_INTERVALS`, with padding alpha 1. + pub fn input_table(&self) -> &[[f32; 4]] { + &self.input + } + + /// RGB output curves at `(i / (OUTPUT_ENTRIES - 1))^4`, with padding alpha 1. + pub fn output_table(&self) -> &[[f32; 4]] { + &self.output + } + + pub(crate) fn from_parts(matrix: [[f32; 4]; 3], input: &[Vec], output: Option<&[Vec]>) -> Option { + if !matrix.iter().flatten().all(|v| v.is_finite()) { + return None; + } + let input = pack_table(if input.is_empty() { None } else { Some(input) }, Self::INPUT_INTERVALS + 1, |u| u)?; + let output = pack_table(output, Self::OUTPUT_ENTRIES, |u| u * u * u * u)?; + let maxima: [f64; 3] = std::array::from_fn(|channel| input.iter().map(|entry| f64::from(entry[channel]).abs()).fold(0.0, f64::max)); + for row in matrix { + let magnitude = + row[..3].iter().zip(maxima).map(|(coefficient, maximum)| f64::from(*coefficient).abs() * maximum).sum::() + f64::from(row[3]).abs(); + if magnitude > f64::from(f32::MAX) { + return None; + } + } + Some(Self { matrix, input, output }) + } +} + +fn pack_table(tables: Option<&[Vec]>, entries: usize, identity: impl Fn(f32) -> f32) -> Option> { + if let Some(tables) = tables + && (tables.len() != 3 || tables.iter().any(|t| t.len() != entries)) + { + return None; + } + let mut packed: Vec<[f32; 4]> = Vec::new(); + // Both lengths are fixed constants, but a failed allocation still means the caller + // should retain its existing fallback rather than abort while opening a document. + packed.try_reserve_exact(entries).ok()?; + for i in 0..entries { + let rgb = match tables { + Some(tables) => [*tables.first()?.get(i)?, *tables.get(1)?.get(i)?, *tables.get(2)?.get(i)?], + None => [identity(i as f32 / (entries - 1) as f32); 3], + }; + if !rgb.iter().all(|v| v.is_finite()) { + return None; + } + if let Some(previous) = packed.last() + && rgb.iter().zip(previous).any(|(next, before)| !(*next - *before).is_finite()) + { + return None; + } + packed.push([rgb[0], rgb[1], rgb[2], 1.0]); + } + Some(packed) +} + +#[cfg(test)] +mod tests { + use super::*; + + const IDENTITY: [[f32; 4]; 3] = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]]; + + #[test] + fn rejects_invalid_tables_and_nonfinite_values() { + let inputs = vec![vec![0.0; GpuMatrixShaper::INPUT_INTERVALS + 1]; 3]; + let outputs = vec![vec![0.0; GpuMatrixShaper::OUTPUT_ENTRIES]; 3]; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&outputs)).is_some()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs[..2], Some(&outputs)).is_none()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&outputs[..2])).is_none()); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&[])).is_none()); + let mut invalid = inputs.clone(); + invalid[0].pop(); + assert!(GpuMatrixShaper::from_parts(IDENTITY, &invalid, Some(&outputs)).is_none()); + for v in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] { + let mut invalid = inputs.clone(); + invalid[1][123] = v; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &invalid, Some(&outputs)).is_none()); + let mut invalid = outputs.clone(); + invalid[2][321] = v; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, Some(&invalid)).is_none()); + let mut invalid = IDENTITY; + invalid[1][3] = v; + assert!(GpuMatrixShaper::from_parts(invalid, &inputs, Some(&outputs)).is_none()); + } + } + + #[test] + fn rejects_finite_data_that_overflows_shader_arithmetic() { + let mut matrix = IDENTITY; + matrix[0] = [f32::MAX; 4]; + assert!(GpuMatrixShaper::from_parts(matrix, &[], None).is_none()); + let mut inputs = vec![vec![0.0; GpuMatrixShaper::INPUT_INTERVALS + 1]; 3]; + inputs[0][100] = -f32::MAX; + inputs[0][101] = f32::MAX; + assert!(GpuMatrixShaper::from_parts(IDENTITY, &inputs, None).is_none()); + } +} diff --git a/crates/cms/src/lib.rs b/crates/cms/src/lib.rs index f93b00372b..259d51bf77 100644 --- a/crates/cms/src/lib.rs +++ b/crates/cms/src/lib.rs @@ -8,6 +8,7 @@ //! * [`builtin`] ships sRGB, Display P3, Adobe RGB (1998)-compatible, ProPhoto-compatible, //! linear sRGB, Rec. 2020, Gray 2.2, sGray, Lab D50 and a synthetic coated CMYK profile. //! * [`Lut3d`] samples a display transform into a 3D texture for the GPU canvas. +//! * [`GpuMatrixShaper`] preserves near-black precision for separable RGB display transforms. //! //! Implemented from the public ICC.1:2010 (v4.3) and ICC.1:2001-04 (v2) specifications, //! CIE 15, and Adobe's published black point compensation paper. This crate depends on no @@ -19,6 +20,7 @@ pub mod builtin; pub mod clut; pub mod curve; pub mod gamut; +mod gpu_matrix_shaper; pub mod lut3d; pub mod lutfile; pub mod math; @@ -32,6 +34,7 @@ pub use builtin::Builtin; pub use clut::Clut; pub use curve::Curve; pub use gamut::GamutCheck; +pub use gpu_matrix_shaper::GpuMatrixShaper; pub use lut3d::Lut3d; pub use profile::{ColorSpace, Pcs, Profile, ProfileClass}; pub use transform::{SampleKind, Transform, TransformOptions, black_point, cached}; diff --git a/crates/cms/src/lutfile.rs b/crates/cms/src/lutfile.rs index 47ff164fda..d547f064ed 100644 --- a/crates/cms/src/lutfile.rs +++ b/crates/cms/src/lutfile.rs @@ -87,16 +87,63 @@ impl LutFile { } } +/// A LUT file format. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Format { + Cube, + ThreeDl, + Look, +} + +impl Format { + /// The format [`parse`] reads `bytes` as: from `file_name`'s extension, else sniffed + /// (XML is `.look`, anything else `.cube`). + pub fn detect(file_name: &str, bytes: &[u8]) -> Self { + let lower = file_name.to_ascii_lowercase(); + if lower.ends_with(".3dl") { + Format::ThreeDl + } else if lower.ends_with(".look") || decode_text(bytes).trim_start().starts_with('<') { + Format::Look + } else { + Format::Cube + } + } + + /// The file extension, without the dot. + pub fn extension(self) -> &'static str { + match self { + Format::Cube => "cube", + Format::ThreeDl => "3dl", + Format::Look => "look", + } + } +} + /// Parses a LUT, picking the format from `file_name`'s extension (falls back to sniffing). pub fn parse(file_name: &str, bytes: &[u8]) -> Result { - let lower = file_name.to_ascii_lowercase(); - let text = String::from_utf8_lossy(bytes); - if lower.ends_with(".3dl") { - parse_3dl(&text) - } else if lower.ends_with(".look") || text.trim_start().starts_with('<') { - parse_look(&text) - } else { - parse_cube(&text) + parse_as(Format::detect(file_name, bytes), bytes) +} + +/// Parses a LUT in a known format. +pub fn parse_as(format: Format, bytes: &[u8]) -> Result { + let text = decode_text(bytes); + match format { + Format::Cube => parse_cube(&text), + Format::ThreeDl => parse_3dl(&text), + Format::Look => parse_look(&text), + } +} + +/// UTF-16 with a byte-order mark (Photoshop loads such `.look` files, #9118), else UTF-8. +fn decode_text(bytes: &[u8]) -> std::borrow::Cow<'_, str> { + let utf16 = |rest: &[u8], from: fn([u8; 2]) -> u16| { + let units: Vec = rest.as_chunks::<2>().0.iter().map(|&c| from(c)).collect(); + String::from_utf16_lossy(&units).into() + }; + match bytes { + [0xFF, 0xFE, rest @ ..] => utf16(rest, u16::from_le_bytes), + [0xFE, 0xFF, rest @ ..] => utf16(rest, u16::from_be_bytes), + _ => String::from_utf8_lossy(bytes), } } @@ -107,7 +154,14 @@ pub fn parse_cube(text: &str) -> Result { let (mut dmin, mut dmax) = ([0.0f32; 3], [1.0f32; 3]); let mut rows: Vec = Vec::new(); for line in text.lines() { - let line = line.split('#').next().unwrap_or("").trim(); + // A '#' inside a quoted TITLE is part of the title, not a comment. + let raw = line.trim_start(); + let skip = if raw.starts_with("TITLE") { raw.find('"').and_then(|q| raw[q + 1..].find('"').map(|e| q + e + 2)) } else { None }.unwrap_or(0); + let line = match raw[skip..].find('#') { + Some(i) => &raw[..skip + i], + None => raw, + } + .trim(); if line.is_empty() { continue; } @@ -171,29 +225,89 @@ pub fn parse_cube(text: &str) -> Result { err("no LUT_3D_SIZE in .cube file") } -/// Lustre `.3dl`: an optional shaper line of input values, then integer RGB rows with **blue** -/// varying fastest; the output bit depth is inferred from the largest value. +/// Lustre `.3dl`: an optional `3DMESH` magic and `Mesh ` line, an optional +/// shaper line of input values, then integer RGB rows with **blue** varying fastest. The +/// `Mesh` line declares the cube edge (2^inBits + 1) and the output code range (up to +/// 2^outBits − 1), and Photoshop honours both (#9073, #9078); without it the edge is the cube +/// root of the row count and the range is inferred from the largest value (#9121). pub fn parse_3dl(text: &str) -> Result { + let mut mesh: Option<(u32, u32)> = None; let mut rows: Vec<[f64; 3]> = Vec::new(); + // How many numbers the first numeric line carried: a shaper of exactly `n` values (n ≤ 3) + // is otherwise indistinguishable from a node row. + let mut first_len: Option = None; + // Any text line before the rows is a header: a headerless table peaking at 255 or 1023 is + // on the 12-bit scale, as Photoshop 27.11 reads it (#9121). + let mut header = false; for line in text.lines() { let line = line.split('#').next().unwrap_or("").trim(); - if line.is_empty() || line.starts_with(|c: char| c.is_ascii_alphabetic()) { + if line.is_empty() { + continue; + } + if line.starts_with(|c: char| c.is_ascii_alphabetic()) { + header = true; + // The only header keyword that carries data. A malformed or out-of-range `Mesh` + // line is ignored, as every text line always was. + let mut it = line.split_whitespace(); + if it.next().is_some_and(|k| k.eq_ignore_ascii_case("Mesh")) { + // A declaration is exactly two integer fields after the keyword. Anything else + // (a non-numeric field, too few or too many) is not a declaration: `mesh` stays + // unset so headerless inference applies, rather than recovering numbers from + // the wrong positions (#9078). + let fields: Vec<&str> = it.collect(); + if let [a, b] = fields.as_slice() + && let (Ok(n), Ok(bits)) = (a.parse::(), b.parse::()) + && (1..=7).contains(&n) + && (1..=16).contains(&bits) + { + mesh = Some((n, bits)); + } + } continue; } let v: Vec = line.split_whitespace().filter_map(|s| s.parse().ok()).collect(); + if v.is_empty() { + // The `3DMESH` magic carries no numbers, but it is a header all the same: a table + // behind it reads a 1023 peak on the 10-bit scale (#9121). + header = true; + continue; + } + first_len.get_or_insert(v.len()); if v.len() == 3 { rows.push([v[0], v[1], v[2]]); } // Lines with more than three numbers are the input shaper; it is assumed linear. } - let n = (rows.len() as f64).cbrt().round() as usize; - if n < 2 || n * n * n != rows.len() { - return err(format!("{} rows is not a cube", rows.len())); - } - let max = rows.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); - let scale = if max <= 1.0 { 1.0 } else { [1023.0, 4095.0, 16383.0, 65535.0].into_iter().find(|&s| max <= s).unwrap_or(max) }; + let (n, nodes, scale) = match mesh { + Some((in_bits, out_bits)) => { + let n = 2usize.pow(in_bits) + 1; + let expected = n.checked_pow(3).ok_or_else(|| LutError(format!("unsupported 3dl cube size {n}")))?; + // One row more than the declared cube, and the first row has exactly `n` values: + // that row is the shaper, not a 28th node of a 3³ cube (#9078). + let nodes = if rows.len() == expected + 1 && first_len == Some(n) { &rows[1..] } else { &rows[..] }; + if nodes.len() != expected { + return err(format!("expected {expected} rows for the declared {n}³ cube, found {}", nodes.len())); + } + // Under a valid declaration the node codes are integers in the declared range, so + // the declaration wins for every in-range value — including a small max, where 1 is + // 1/declared of full output, not full output itself (#9073). Only a node beyond the + // declared range is a wrong declaration, and the old inference keeps such a file + // loading as before. + let max = nodes.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); + let declared = 2f64.powi(i32::try_from(out_bits).unwrap_or(16)) - 1.0; + (n, nodes, if max > declared { inferred_3dl_scale(max, header) } else { declared }) + } + None => { + let n = (rows.len() as f64).cbrt().round() as usize; + if n < 2 || n * n * n != rows.len() { + return err(format!("{} rows is not a cube", rows.len())); + } + let max = rows.iter().flatten().fold(0.0f64, |a, &b| a.max(b)); + (n, &rows[..], inferred_3dl_scale(max, header)) + } + }; let mut data = vec![0.0f32; n * n * n * 3]; - for (i, row) in rows.iter().enumerate() { + for (i, row) in nodes.iter().enumerate() { let (r, g, b) = (i / (n * n), (i / n) % n, i % n); let at = ((b * n + g) * n + r) * 3; for k in 0..3 { @@ -203,9 +317,30 @@ pub fn parse_3dl(text: &str) -> Result { LutFile { title: String::new(), size: n, data, domain_min: [0.0; 3], domain_max: [1.0; 3] }.check() } +/// The code range a `.3dl` node table without a declared output depth was written with: the +/// smallest common integer depth that covers the largest value; a table of 0..1 floats passes +/// through. A headerless table has no 10-bit reading — 255 and 1023 are on the 12-bit scale, as +/// Photoshop 27.11 reads them (#9121). +fn inferred_3dl_scale(max: f64, header: bool) -> f64 { + if max <= 1.0 { + return 1.0; + } + let depths: &[f64] = if header { &[1023.0, 4095.0, 16383.0, 65535.0] } else { &[4095.0, 16383.0, 65535.0] }; + depths.iter().copied().find(|&s| max <= s).unwrap_or(max) +} + /// SpeedGrade `.look`: `` and a hex `` string of little-endian float32 RGB triplets /// (red fastest). pub fn parse_look(text: &str) -> Result { + // Comments may hold tag-like text, so drop them before looking for tags (#9109). + let mut clean = String::with_capacity(text.len()); + let mut rest = text; + while let Some(i) = rest.find("").map_or("", |e| &rest[i + 4 + e + 3..]); + } + clean.push_str(rest); + let text = clean.as_str(); let tag = |name: &str| -> Option { let open = format!("<{name}>"); let start = text.find(&open)? + open.len(); @@ -214,7 +349,14 @@ pub fn parse_look(text: &str) -> Result { }; let size: usize = tag("size").and_then(|s| s.parse().ok()).ok_or_else(|| LutError("no in .look".into()))?; let (entries, _) = checked_data_len(size)?; - let hex: Vec = tag("data").ok_or_else(|| LutError("no in .look".into()))?.bytes().filter(u8::is_ascii_hexdigit).collect(); + let hex: Vec = tag("data").ok_or_else(|| LutError("no in .look".into()))?.bytes().filter(|b| !b.is_ascii_whitespace()).collect(); + if hex.iter().any(|b| !b.is_ascii_hexdigit()) { + return err("invalid hexadecimal in .look"); + } + // Each float needs eight hex digits; as_chunks would silently discard a partial value. + if !hex.len().is_multiple_of(8) { + return err("incomplete float in .look "); + } let nib = |c: u8| (c as char).to_digit(16).unwrap_or(0) as u8; let bytes: Vec = hex.as_chunks::<2>().0.iter().map(|p| nib(p[0]) << 4 | nib(p[1])).collect(); let floats: Vec = bytes.as_chunks::<4>().0.iter().map(|b| f32::from_le_bytes([b[0], b[1], b[2], b[3]])).collect(); @@ -230,6 +372,10 @@ pub fn write_cube(l: &LutFile) -> String { s.push_str(&format!("TITLE \"{}\"\n", l.title.replace('"', "'"))); } s.push_str(&format!("LUT_3D_SIZE {}\n", l.size)); + if l.domain_min != [0.0; 3] || l.domain_max != [1.0; 3] { + s.push_str(&format!("DOMAIN_MIN {} {} {}\n", l.domain_min[0], l.domain_min[1], l.domain_min[2])); + s.push_str(&format!("DOMAIN_MAX {} {} {}\n", l.domain_max[0], l.domain_max[1], l.domain_max[2])); + } for c in l.data.as_chunks::<3>().0 { s.push_str(&format!("{:.6} {:.6} {:.6}\n", c[0], c[1], c[2])); } @@ -301,6 +447,32 @@ pub fn builtin(id: &str) -> Option { mod tests { use super::*; + #[test] + fn cube_title_with_hash_round_trips() { + let l = LutFile::from_fn("Look #3", 2, |c| c); + assert_eq!(parse_cube(&write_cube(&l)).unwrap().title, "Look #3"); + let t = "TITLE \"A #b\" # note\nLUT_3D_SIZE 2 # c\n# comment\n"; + let rows = "0 0 0 # x\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; + let p = parse_cube(&format!("{t}{rows}")).unwrap(); + assert_eq!(p.title, "A #b"); + } + + #[test] + fn write_cube_keeps_non_default_domain() { + let mut l = LutFile::identity(2); + l.domain_min = [0.1; 3]; + l.domain_max = [0.9; 3]; + let back = parse_cube(&write_cube(&l)).unwrap(); + assert_eq!(back.domain_min, [0.1; 3]); + assert_eq!(back.domain_max, [0.9; 3]); + l.domain_min = [0.0, 0.0, 0.5]; + l.domain_max = [1.0; 3]; + let back = parse_cube(&write_cube(&l)).unwrap(); + assert_eq!(back.domain_min, [0.0, 0.0, 0.5]); + assert_eq!(back.domain_max, [1.0; 3]); + assert!(!write_cube(&LutFile::identity(2)).contains("DOMAIN")); + } + #[test] fn cube_domain_is_kept_and_validated() { let base = "TITLE \"t\" @@ -396,6 +568,26 @@ DOMAIN_MAX 0.5 1 1 assert!(parse_cube(&text).is_err()); } + #[test] + fn three_dl_without_a_header_reads_low_nodes_as_12_bit() { + let table = |peak: u32| -> String { + let mut text = String::from("0 1023\n"); + for i in 0..8 { + let on = |bit: u32| if i & bit != 0 { peak } else { 0 }; + text.push_str(&format!("{} {} {}\n", on(4), on(2), on(1))); + } + text + }; + for peak in [255, 1023, 4095] { + let white = parse_3dl(&table(peak)).unwrap().data[21..24].to_vec(); + assert_eq!(white, [peak as f32 / 4095.0; 3], "peak {peak}"); + } + // A header keeps the inferred 10-bit depth. The bare `3DMESH` magic is a header without a + // cube declaration; `Mesh 1 10` would declare a 3³ cube and refuse this 2³ table (#9078). + let white = parse_3dl(&format!("3DMESH\n{}", table(1023))).unwrap().data[21..24].to_vec(); + assert_eq!(white, [1.0; 3]); + } + #[test] fn three_dl_blue_fastest_and_bit_depth() { let mut text = String::from("0 1023\n"); @@ -411,6 +603,130 @@ DOMAIN_MAX 0.5 1 1 assert_eq!(l.data, LutFile::identity(2).data); } + /// A `size`³ inverse-identity `.3dl` (blue fastest): node (r,g,b) outputs `max - max·v/(size-1)` + /// per channel, with a uniform shaper of `size` codes up to 1023. + fn mesh_3dl(n_bits: u32, out_bits: u32, size: usize, max: f64, shaper: bool) -> String { + let mut text = format!("3DMESH\nMesh {n_bits} {out_bits}\n"); + if shaper { + for i in 0..size { + if i > 0 { + text.push(' '); + } + text.push_str(&format!("{}", (1023.0 * i as f64 / (size - 1) as f64).round())); + } + text.push('\n'); + } + for r in 0..size { + for g in 0..size { + for b in 0..size { + let v = [r, g, b].map(|c| (max - max * c as f64 / (size - 1) as f64).round()); + text.push_str(&format!("{} {} {}\n", v[0], v[1], v[2])); + } + } + } + text + } + + #[test] + fn three_dl_mesh_three_point_shaper_is_not_a_node() { + // #9078: Photoshop loads a 3³ cube whose shaper line has three values; counting it as a + // 28th node rejected the whole file. + let l = parse_3dl(&mesh_3dl(1, 12, 3, 4095.0, true)).unwrap(); + assert_eq!(l.size, 3); + assert_eq!(l.data.len(), 3 * 3 * 3 * 3); + // The inverse identity: node (0,0,0) is full, node (2,2,2) black. + assert!((l.data[0] - 1.0).abs() < 1e-6 && (l.data[1] - 1.0).abs() < 1e-6 && (l.data[2] - 1.0).abs() < 1e-6); + let last = &l.data[l.data.len() - 3..]; + assert_eq!(last, [0.0, 0.0, 0.0]); + // The same 27 nodes without the shaper row still load (the node-only control). + assert_eq!(parse_3dl(&mesh_3dl(1, 12, 3, 4095.0, false)).unwrap().data, l.data); + } + + #[test] + fn three_dl_mesh_declares_the_output_depth() { + // #9073: identical node codes divide by the declared range, not by the largest value. + let mut text_10 = String::new(); + let mut text_12 = String::new(); + for (n_bits, out_bits, dst) in [(2, 10, &mut text_10), (2, 12, &mut text_12)] { + // A 5³ identity scaled to 10-bit codes (max 1023). + *dst = format!("3DMESH\nMesh {n_bits} {out_bits}\n0 256 512 768 1023\n"); + for r in 0..5 { + for g in 0..5 { + for b in 0..5 { + dst.push_str(&format!("{} {} {}\n", r * 255, g * 255, b * 255)); + } + } + } + } + let white_10 = parse_3dl(&text_10).unwrap(); + let white_12 = parse_3dl(&text_12).unwrap(); + assert_eq!(white_10.size, 5); + let last_10 = &white_10.data[white_10.data.len() - 3..]; + let last_12 = &white_12.data[white_12.data.len() - 3..]; + assert!((last_10[0] - 1020.0 / 1023.0).abs() < 1e-6, "{last_10:?}"); + // 1020/4095 ≈ 0.249: white renders grey, as Photoshop measures it. + assert!((last_12[0] - 1020.0 / 4095.0).abs() < 1e-6, "{last_12:?}"); + } + + #[test] + fn three_dl_mesh_header_mismatch_is_rejected() { + // A Mesh header that disagrees with the row count is refused, not silently re-inferred. + assert!(parse_3dl("3DMESH\nMesh 1 12\n0 0 0\n0 0 1\n").is_err()); + // A node beyond the declared range is a wrong declaration: the old inference from the + // largest value keeps the file loading as it did before the header was honoured. + let mut text = String::from("3DMESH\nMesh 1 10\n"); + for r in 0..3 { + for g in 0..3 { + for b in 0..3 { + text.push_str(&format!("{} {} {}\n", r * 2047, g * 2047, b * 2047)); + } + } + } + let l = parse_3dl(&text).unwrap(); + let last = &l.data[l.data.len() - 3..]; + assert!((last[0] - 4094.0 / 4095.0).abs() < 1e-6, "{last:?}"); + } + + #[test] + fn three_dl_malformed_mesh_header_falls_back_to_inference() { + // A `Mesh` line that is not exactly two integer fields is not a declaration: it is + // ignored and the table is inferred as if headerless, rather than recovering numbers + // from the wrong positions (#9078). A 2³ cube would otherwise be read as a 3³ cube. + for header in ["Mesh invalid 1 12", "Mesh 1 12 13", "Mesh 1 x", "Mesh 1"] { + let mut text = format!("3DMESH\n{header}\n"); + for r in 0..2 { + for g in 0..2 { + for b in 0..2 { + text.push_str(&format!("{} {} {}\n", r * 1023, g * 1023, b * 1023)); + } + } + } + let l = parse_3dl(&text).unwrap(); + assert_eq!(l.size, 2, "{header}"); + assert_eq!(l.data, LutFile::identity(2).data, "{header}"); + } + } + + #[test] + fn three_dl_mesh_uses_declared_range_for_small_integer_codes() { + // #9073: under a valid `Mesh` declaration the node codes are integers in the declared + // range, so a max value of 1 is 1/4095 of full output — not full output itself. + let mut text = String::from("3DMESH\nMesh 1 12\n"); + for r in 0..3 { + for g in 0..3 { + for b in 0..3 { + let v = u8::from(r == 0 && g == 0 && b == 0); + text.push_str(&format!("{v} {v} {v}\n")); + } + } + } + let l = parse_3dl(&text).unwrap(); + assert_eq!(l.size, 3); + // blue fastest: node (0,0,0) is the first entry, code 1 → 1/4095. + assert!((l.data[0] - 1.0 / 4095.0).abs() < 1e-6, "{:?}", &l.data[0..3]); + assert!(l.data[3..].iter().all(|&v| v == 0.0), "{:?}", l.data); + } + #[test] fn look_hex_floats() { let id = LutFile::identity(2); @@ -419,12 +735,81 @@ DOMAIN_MAX 0.5 1 1 assert_eq!(parse("a.look", xml.as_bytes()).unwrap().data, id.data); } + #[test] + fn look_reads_utf16_with_either_byte_order() { + let id = LutFile::identity(2); + let hex: String = id.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + let xml = format!("\"2\"\"{hex}\""); + let le: Vec = [0xFF, 0xFE].into_iter().chain(xml.encode_utf16().flat_map(u16::to_le_bytes)).collect(); + let be: Vec = [0xFE, 0xFF].into_iter().chain(xml.encode_utf16().flat_map(u16::to_be_bytes)).collect(); + assert_eq!(parse("a.look", &le).unwrap().data, id.data); + assert_eq!(parse("a.look", &be).unwrap().data, id.data); + // Sniffed as XML through the byte-order mark too, so the recorded format is the one parsed. + assert_eq!(Format::detect("a.lut", &le), Format::Look); + assert_eq!(parse_as(Format::Look, &be).unwrap().data, id.data); + } + + #[test] + fn look_ignores_tags_inside_xml_comments() { + let hex = |l: &LutFile| -> String { l.data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect() }; + let inverse = LutFile::from_fn("", 2, |[r, g, b]| [1.0 - r, 1.0 - g, 1.0 - b]); + let xml = format!( + "\"2\"\"{}\"\n\n", + t = html_escape(title) + ); + for (j, mut tds) in by_row.into_iter().enumerate() { + tds.sort_by_key(|(c, _)| *c); + s.push_str("\t\n"); + for (_, td) in tds { + s.push_str(&td); + } + if let Some(sp) = spacer { + s.push_str(&format!("\t\t\n", html_escape(sp), ys[j + 1] - ys[j])); + } + s.push_str("\t\n"); + } + if let Some(sp) = spacer { + s.push_str("\t\n"); + for i in 0..nc { + s.push_str(&format!("\t\t\n", html_escape(sp), xs[i + 1] - xs[i])); + } + s.push_str("\t\t\n\t\n"); + } + s.push_str("
\n\t\t\t\"\"
\n\t\t\t\"\"
\n\n\n\n"); + s +} + +/// File › Export › Save for Web (Legacy). +pub(super) fn save_for_web(s: &mut Session, p: &Value) -> Result { + save_for_web_impl(s, p, &mut write_file, true) +} + +/// Save for Web through a platform writer (native files or browser downloads). +/// The writer receives every image, spacer and HTML file. A write error stops the export; +/// only a complete export remembers its settings and fires the export event. A custom writer +/// that targets the local filesystem must enforce its own path confinement. +pub fn save_for_web_with_writer(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u8]) -> Result<()>) -> Result { + save_for_web_impl(s, p, write, false) +} + +/// Settings to seed the Save for Web dialog from on the next launch: `file_menu` is session +/// state, so a complete export also writes the preferences' remembered dialog settings (like +/// Fill, Liquify and the filter dialogs). The destination is per export, never remembered. +fn remember_web_settings(s: &mut Session, cmd: &str, settings: &Value) { + let mut m = settings.clone(); + if let Some(o) = m.as_object_mut() { + for k in ["path", "dir", "trustedSliceHtml"] { + o.remove(k); + } + } + s.prefs.edit(|p| { + p.dialogs.insert(cmd.to_string(), m); + }); +} + +fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u8]) -> Result<()>, native_paths: bool) -> Result { + let cmd = "file.export.saveForWebLegacy"; + if let Some(authorize) = s.authorize { + authorize(cmd, p)?; + } + // Settings are checked first, so a refused export leaves a floating selection where it is. + let st = WebSettings::from_params(p, cmd)?; + // Direct platform calls must export the moved selection as it appears on the canvas, + // just as Session::execute does before dispatching the native command. + crate::float_cmds::before_command(s, cmd)?; + let d = s.active().ok_or(EngineError::NoDocument)?; + let doc = d.doc.clone(); + let (wdoc, sx, sy) = web_document(&doc, p, &st)?; + let buf = photocraft_compose::flatten(&wdoc); + let bw = wdoc.size.width as usize; + // An untagged document is treated as sRGB (see [`web_document`]); with Embed Color Profile + // on, the file says so, as Photoshop's does (#9325). + let srgb = (st.embed_icc && wdoc.icc_profile.is_none()).then(|| photocraft_cms::Builtin::Srgb.profile().to_bytes().to_vec()); + let icc = wdoc.icc_profile.as_deref().map(|v| v.as_slice()).or(srgb.as_deref()); + let xmp = web_xmp(&doc, st.metadata); + let exif = web_exif(&doc, st.metadata); + let dpi = wdoc.resolution_dpi; + let warnings = web_warnings(&st, exif, dpi); + let all = slices::resolve(&doc); + let pick = p.get("slices").and_then(Value::as_str).unwrap_or("all"); + let numbers: Option> = p.get("numbers").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).collect()); + let chosen: Vec<&ResolvedSlice> = all + .iter() + .filter(|r| match (&numbers, pick) { + (Some(n), _) => n.contains(&(r.number as u64)), + (None, "user") => r.origin != photocraft_doc::SliceOrigin::Auto, + _ => true, + }) + .collect(); + let single = p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty()); + let dir = p.get("dir").and_then(Value::as_str).filter(|v| !v.is_empty()); + let mut last_web = p.clone(); + // A trusted-markup decision applies to this export only, never to another document opened + // with the dialog's remembered settings. + if let Some(last) = last_web.as_object_mut() { + last.remove("trustedSliceHtml"); + } + // Estimate only (the dialog's annotations): the whole image, or each chosen slice. + if single.is_none() && dir.is_none() { + let o = optimize(&buf.px, bw, wdoc.bounds(), &st, icc, exif, xmp.as_deref(), dpi, false)?; + return Ok( + json!({"format": o.ext, "bytes": o.bytes.len(), "width": o.width, "height": o.height, "colors": o.colors, "colorTable": o.color_table.iter().map(|c| rgb_hex(*c)).collect::>(), "transparentIndex": o.transparent_index}), + ); + } + if let Some(path) = single.filter(|_| dir.is_none() && (all.len() == 1 || p.get("slices").is_none())) { + let o = optimize(&buf.px, bw, wdoc.bounds(), &st, icc, exif, xmp.as_deref(), dpi, false)?; + write(path, &o.bytes)?; + remember_web_settings(s, cmd, &last_web); + s.file_menu.last_web = Some(last_web); + crate::automate_cmds::fire_event(s, "export"); + return Ok(json!({"files": [path], "bytes": o.bytes.len(), "width": o.width, "height": o.height, "colors": o.colors, "warnings": warnings})); + } + let dir = + dir.map(str::to_string).or_else(|| single.and_then(|f| std::path::Path::new(f).parent().map(|d| d.to_string_lossy().into_owned()))).unwrap_or_default(); + let html = p.get("html").and_then(Value::as_bool).unwrap_or(false); + let images = web_images_folder(p.get("imagesFolder").and_then(Value::as_str).unwrap_or("images"), cmd)?; + let trusted_slice_html = p.get("trustedSliceHtml").and_then(Value::as_bool).unwrap_or(false); + let base = portable_generated_stem(single.map(stem).unwrap_or_else(|| slices::base_name(&doc))); + let mut files = Vec::new(); + let mut cells: Vec<(Rect, String, String)> = Vec::new(); + let mut total = 0usize; + // Output names are unique (case-insensitively, for macOS and Windows file systems): two + // slices named alike, or names that sanitize alike, must not overwrite each other. The HTML + // page's spacer image is reserved. + let mut used: std::collections::HashSet = std::collections::HashSet::new(); + if html { + used.insert("spacer.gif".into()); + } + let mut unique = |stem: &str, ext: &str| { + let mut name = format!("{stem}.{ext}"); + let mut k = 2u32; + while !used.insert(name.to_lowercase()) { + name = format!("{stem}_{k}.{ext}"); + k = k.saturating_add(1); + } + name + }; + // Slices never overlap here: one covered by later slices comes as its visible pieces (see + // `slices::resolve`), each its own image and cell, as Photoshop's subslices. A no-image cell's + // text goes into its first piece only. + let mut texted = std::collections::HashSet::new(); + for r in &chosen { + let piece = scale_rect(r.rect, sx, sy).intersect(&wdoc.bounds()); + if piece.is_empty() { + continue; + } + let stored = r.id.and_then(|id| doc.slices.get(id)); + if r.kind == SliceKind::NoImage { + let text = stored + .filter(|s| texted.insert(s.id)) + .map(|s| if trusted_slice_html && s.cell_text_is_html { s.cell_text.clone() } else { html_escape(&s.cell_text) }) + .unwrap_or_default(); + let bg = stored.and_then(|s| s.background).map(|c| format!(" bgcolor=\"#{:02X}{:02X}{:02X}\"", c[1], c[2], c[3])).unwrap_or_default(); + cells.push((piece, bg, text)); + continue; + } + let o = optimize(&buf.px, bw, piece, &st, icc, exif, xmp.as_deref(), dpi, false)?; + let name = unique(&slice_file(&r.name), o.ext); + let rel = if images.is_empty() { name.clone() } else { format!("{images}/{name}") }; + let out = join(&dir, &rel); + write(&web_derived_path(&dir, &rel, native_paths)?, &o.bytes)?; + total += o.bytes.len(); + files.push(out); + let alt = stored.map_or(String::new(), |s| html_escape(&s.alt)); + let img = format!("\n\t\t\t\"{alt}\"", html_escape(&rel), piece.width(), piece.height()); + let body = match stored.filter(|s| safe_slice_url(&s.url)) { + Some(s) => { + let target = if s.target.is_empty() { String::new() } else { format!(" target=\"{}\"", html_escape(&s.target)) }; + format!("\n\t\t\t{img}", html_escape(&s.url)) + } + None => img, + }; + cells.push((piece, String::new(), body)); + } + let mut html_path = None; + if html { + let grid = cells.len() > 1; + let spacer = grid.then(|| if images.is_empty() { "spacer.gif".to_string() } else { format!("{images}/spacer.gif") }); + if let Some(sp) = &spacer { + let gif = photocraft_codecs::web::encode_gif_indexed(1, 1, &[0], &[[0, 0, 0]], Some(0), false).map_err(other)?; + write(&web_derived_path(&dir, sp, native_paths)?, &gif)?; + } + let page = html_table(&base, wdoc.size.width, wdoc.size.height, &cells, spacer.as_deref()); + let hp = join(&dir, &format!("{base}.html")); + write(&web_derived_path(&dir, &format!("{base}.html"), native_paths)?, page.as_bytes())?; + html_path = Some(hp); + } + remember_web_settings(s, cmd, &last_web); + s.file_menu.last_web = Some(last_web); + crate::automate_cmds::fire_event(s, "export"); + Ok( + json!({"files": files, "html": html_path, "bytes": total, "slices": chosen.len(), "width": wdoc.size.width, "height": wdoc.size.height, "warnings": warnings}), + ) +} diff --git a/crates/engine/src/cmd/web_cmds/settings.rs b/crates/engine/src/cmd/web_cmds/settings.rs new file mode 100644 index 0000000000..053604a389 --- /dev/null +++ b/crates/engine/src/cmd/web_cmds/settings.rs @@ -0,0 +1,319 @@ +use super::optimizer::to8; +use super::{Dither, PaletteKind, Result, Value, bad, json}; + +// ---------- settings ---------- + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum WebFormat { + Gif, + Png8, + Png24, + Jpeg, + Wbmp, + /// WebP, lossy (our VP8 encoder) or lossless. Not in Photoshop's legacy dialog, but the + /// format people prepare web images in now (#2415, #6391). + Webp, +} + +impl WebFormat { + pub fn from_id(s: &str) -> Option { + Some(match s.to_ascii_lowercase().as_str() { + "gif" => WebFormat::Gif, + "png8" | "png-8" => WebFormat::Png8, + "png24" | "png-24" | "png" | "png32" => WebFormat::Png24, + "jpeg" | "jpg" => WebFormat::Jpeg, + "wbmp" => WebFormat::Wbmp, + "webp" => WebFormat::Webp, + _ => return None, + }) + } + pub fn ext(self) -> &'static str { + match self { + WebFormat::Gif => "gif", + WebFormat::Png8 | WebFormat::Png24 => "png", + WebFormat::Jpeg => "jpg", + WebFormat::Wbmp => "wbmp", + WebFormat::Webp => "webp", + } + } + pub fn indexed(self) -> bool { + matches!(self, WebFormat::Gif | WebFormat::Png8) + } +} + +/// Which metadata Save for Web embeds (JPEG only, like Photoshop's legacy exporter). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum WebMetadata { + None, + Copyright, + CopyrightAndContact, + /// All the XMP, none of the photo's EXIF (camera, date taken, GPS). + AllExceptCameraInfo, + /// All the XMP and the photo's EXIF. + All, +} + +/// One optimisation preset (the right-hand side of the Save for Web dialog). +#[derive(Clone, Debug, PartialEq)] +pub struct WebSettings { + pub format: WebFormat, + pub palette: PaletteKind, + pub colors: usize, + /// Fixed opaque source palette in index order. + pub color_table: Option>, + /// Replacements applied after quantization, preserving pixel indices. + pub color_table_edits: std::collections::BTreeMap, + pub dither: Dither, + /// 0..=1. + pub dither_amount: f32, + pub transparency: bool, + /// Edge / background colour (0..=1 RGB); None = no matte (hard alpha threshold). + pub matte: Option<[f32; 3]>, + pub interlaced: bool, + /// Web Snap: palette entries within this distance (0..=1 of the channel range) snap to the + /// web-safe cube. + pub web_snap: f32, + /// JPEG and lossy WebP quality 0..=100. + pub quality: u8, + /// WebP: lossless instead of lossy at `quality`. + pub lossless: bool, + pub progressive: bool, + pub optimized: bool, + pub embed_icc: bool, + pub metadata: WebMetadata, + pub convert_to_srgb: bool, +} + +impl Default for WebSettings { + fn default() -> Self { + WebSettings { + format: WebFormat::Png24, + palette: PaletteKind::Selective, + colors: 256, + color_table: None, + color_table_edits: Default::default(), + dither: Dither::Diffusion, + dither_amount: 0.88, + transparency: true, + matte: Some([1.0, 1.0, 1.0]), + interlaced: false, + web_snap: 0.0, + quality: 60, + lossless: false, + progressive: false, + optimized: true, + embed_icc: false, + metadata: WebMetadata::Copyright, + convert_to_srgb: true, + } + } +} + +fn hex3(s: &str) -> Option<[f32; 3]> { + let s = s.trim_start_matches('#'); + let b = |i: usize| u8::from_str_radix(s.get(i..i + 2)?, 16).ok().map(|v| f32::from(v) / 255.0); + (s.len() == 6).then_some(())?; + Some([b(0)?, b(2)?, b(4)?]) +} + +impl WebSettings { + /// Settings from command parameters (missing keys keep the defaults; Photoshop's preset + /// names such as "GIF 128 Dithered" or "JPEG High" are accepted as `"preset"`). + /// + /// Without a preset there is no matte, as in Photoshop's scripted Save for Web + /// (`ExportOptionsSaveForWeb` left at its defaults): a GIF or PNG-8 keeps the colour of its + /// partly transparent pixels instead of lightening them towards white (#4115). Formats + /// without transparency still go over white. + pub fn from_params(p: &Value, cmd: &str) -> Result { + let mut st = WebSettings { matte: None, ..WebSettings::default() }; + if let Some(preset) = p.get("preset").and_then(Value::as_str) { + st = preset_settings(preset).ok_or_else(|| bad(cmd, format!("unknown preset `{preset}`")))?; + } + let s = |k: &str| p.get(k).and_then(Value::as_str); + if let Some(f) = s("format") { + st.format = WebFormat::from_id(f).ok_or_else(|| bad(cmd, format!("unknown format `{f}` (gif|png8|png24|jpeg|wbmp|webp)")))?; + } + if let Some(k) = s("palette") { + st.palette = match k { + "restrictive" | "web" => PaletteKind::Web, + k => PaletteKind::from_id(k).ok_or_else(|| { + bad(cmd, format!("unknown palette `{k}` (perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform)")) + })?, + }; + } + if let Some(n) = crate::commands::int(p, "colors").filter(|v| *v > 0).map(|v| v as u64) { + st.colors = (n as usize).clamp(2, 256); + } + if let Some(d) = s("dither") { + st.dither = match d { + "none" | "noDither" => Dither::None, + "diffusion" => Dither::Diffusion, + "pattern" => Dither::Pattern, + "noise" => Dither::Noise, + d => return Err(bad(cmd, format!("unknown dither `{d}` (none|diffusion|pattern|noise)"))), + }; + } + if let Some(a) = p.get("ditherAmount").and_then(Value::as_f64) { + st.dither_amount = (a as f32 / 100.0).clamp(0.0, 1.0); + } + // An on/off option given as anything but true or false is an error, not its default. + let flag = |k: &str| match p.get(k) { + None | Some(Value::Null) => Ok(None), + Some(Value::Bool(b)) => Ok(Some(*b)), + Some(_) => Err(bad(cmd, format!("`{k}` must be true or false"))), + }; + if let Some(b) = flag("transparency")? { + st.transparency = b; + } + match s("matte") { + Some("none") => st.matte = None, + Some(c) => st.matte = Some(hex3(c).ok_or_else(|| bad(cmd, "matte is \"none\" or \"#rrggbb\""))?), + None => {} + } + for (k, slot) in [ + ("interlaced", &mut st.interlaced), + ("progressive", &mut st.progressive), + ("lossless", &mut st.lossless), + ("optimized", &mut st.optimized), + ("embedIcc", &mut st.embed_icc), + ("convertToSrgb", &mut st.convert_to_srgb), + ] { + if let Some(b) = flag(k)? { + *slot = b; + } + } + if let Some(w) = p.get("webSnap").and_then(Value::as_f64) { + st.web_snap = (w as f32 / 100.0).clamp(0.0, 1.0); + } + if let Some(q) = p.get("quality").and_then(Value::as_f64) { + st.quality = q.clamp(0.0, 100.0).round() as u8; + } + if let Some(m) = s("metadata") { + st.metadata = match m { + "none" => WebMetadata::None, + "copyright" => WebMetadata::Copyright, + "copyrightAndContact" | "contact" => WebMetadata::CopyrightAndContact, + "allExceptCameraInfo" => WebMetadata::AllExceptCameraInfo, + "all" => WebMetadata::All, + m => return Err(bad(cmd, format!("unknown metadata `{m}` (none|copyright|copyrightAndContact|allExceptCameraInfo|all)"))), + }; + } + if let Some(table) = p.get("colorTable") { + let table = table.as_array().filter(|a| (1..=256).contains(&a.len())).ok_or_else(|| bad(cmd, "colorTable must contain 1–256 #rrggbb colours"))?; + st.color_table = Some( + table + .iter() + .map(|v| v.as_str().and_then(hex3).map(|c| c.map(to8)).ok_or_else(|| bad(cmd, "colorTable entries must be #rrggbb colours"))) + .collect::>>()?, + ); + } + if let Some(edits) = p.get("colorTableEdits") { + let edits = edits + .as_object() + .filter(|m| m.len() <= 256) + .ok_or_else(|| bad(cmd, "colorTableEdits must be an object of palette indices to #rrggbb colours"))?; + let table = st.color_table.as_ref().ok_or_else(|| bad(cmd, "colorTableEdits requires colorTable"))?; + for (index, value) in edits { + let index = index.parse::().ok().filter(|i| *i < table.len()).ok_or_else(|| bad(cmd, "colorTableEdits index is outside colorTable"))?; + let color = value.as_str().and_then(hex3).map(|c| c.map(to8)).ok_or_else(|| bad(cmd, "colorTableEdits values must be #rrggbb colours"))?; + st.color_table_edits.insert(index, color); + } + } + Ok(st) + } +} + +impl WebSettings { + /// The settings as command parameters (the inverse of [`WebSettings::from_params`]). + pub fn to_params(&self) -> Value { + let format = match self.format { + WebFormat::Gif => "gif", + WebFormat::Png8 => "png8", + WebFormat::Png24 => "png24", + WebFormat::Jpeg => "jpeg", + WebFormat::Wbmp => "wbmp", + WebFormat::Webp => "webp", + }; + let palette = match self.palette { + PaletteKind::Exact => "exact", + PaletteKind::SystemMac => "systemMac", + PaletteKind::SystemWindows => "systemWindows", + PaletteKind::Web => "restrictive", + PaletteKind::Uniform => "uniform", + PaletteKind::Perceptual => "perceptual", + PaletteKind::Selective => "selective", + PaletteKind::Adaptive => "adaptive", + }; + let dither = match self.dither { + Dither::None => "none", + Dither::Diffusion => "diffusion", + Dither::Pattern => "pattern", + Dither::Noise => "noise", + }; + let metadata = match self.metadata { + WebMetadata::None => "none", + WebMetadata::Copyright => "copyright", + WebMetadata::CopyrightAndContact => "copyrightAndContact", + WebMetadata::AllExceptCameraInfo => "allExceptCameraInfo", + WebMetadata::All => "all", + }; + let matte = self.matte.map_or("none".to_string(), |c| format!("#{:02x}{:02x}{:02x}", to8(c[0]), to8(c[1]), to8(c[2]))); + let mut p = json!({"format": format, "palette": palette, "colors": self.colors, "dither": dither, "ditherAmount": (self.dither_amount * 100.0).round(), "transparency": self.transparency, "matte": matte, "interlaced": self.interlaced, "webSnap": (self.web_snap * 100.0).round(), "quality": self.quality, "lossless": self.lossless, "progressive": self.progressive, "optimized": self.optimized, "embedIcc": self.embed_icc, "metadata": metadata, "convertToSrgb": self.convert_to_srgb}); + if let Some(table) = &self.color_table { + p["colorTable"] = json!(table.iter().map(|c| rgb_hex(*c)).collect::>()); + } + if !self.color_table_edits.is_empty() { + p["colorTableEdits"] = + json!(self.color_table_edits.iter().map(|(i, c)| (i.to_string(), rgb_hex(*c))).collect::>()); + } + p + } +} + +/// A palette colour in command/control-channel notation. +pub fn rgb_hex(c: [u8; 3]) -> String { + format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2]) +} + +/// Save for Web's named presets. +pub const PRESETS: [&str; 12] = [ + "GIF 128 Dithered", + "GIF 128 No Dither", + "GIF 32 Dithered", + "GIF 32 No Dither", + "GIF 64 Dithered", + "GIF 64 No Dither", + "GIF Restrictive", + "JPEG High", + "JPEG Low", + "JPEG Medium", + "PNG-24", + "PNG-8 128 Dithered", +]; + +pub fn preset_settings(name: &str) -> Option { + let d = WebSettings::default(); + let gif = |colors: usize, dither: bool| WebSettings { + format: WebFormat::Gif, + colors, + dither: if dither { Dither::Diffusion } else { Dither::None }, + ..WebSettings::default() + }; + Some(match name { + "GIF 128 Dithered" => gif(128, true), + "GIF 128 No Dither" => gif(128, false), + "GIF 32 Dithered" => gif(32, true), + "GIF 32 No Dither" => gif(32, false), + "GIF 64 Dithered" => gif(64, true), + "GIF 64 No Dither" => gif(64, false), + "GIF Restrictive" => WebSettings { palette: PaletteKind::Web, ..gif(256, true) }, + "JPEG High" => WebSettings { format: WebFormat::Jpeg, quality: 60, ..d }, + "JPEG Low" => WebSettings { format: WebFormat::Jpeg, quality: 10, ..d }, + "JPEG Medium" => WebSettings { format: WebFormat::Jpeg, quality: 30, ..d }, + "JPEG Maximum" => WebSettings { format: WebFormat::Jpeg, quality: 100, ..d }, + "JPEG Very High" => WebSettings { format: WebFormat::Jpeg, quality: 80, ..d }, + "PNG-24" => WebSettings { format: WebFormat::Png24, ..d }, + "PNG-8 128 Dithered" => WebSettings { format: WebFormat::Png8, colors: 128, ..d }, + _ => return None, + }) +} diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/cmd/web_cmds/tests.rs similarity index 53% rename from crates/engine/src/web_cmds/tests.rs rename to crates/engine/src/cmd/web_cmds/tests.rs index c3ffe6edce..590ea0d7ad 100644 --- a/crates/engine/src/web_cmds/tests.rs +++ b/crates/engine/src/cmd/web_cmds/tests.rs @@ -167,6 +167,19 @@ fn reserved_document_and_slice_names_export_with_portable_names() { assert!(std::path::Path::new(result["html"].as_str().unwrap()).is_file()); } +#[test] +fn a_dotted_device_file_name_exports_without_the_device_stem() { + // `nul.v2_` would still be the NUL device on Windows; `nul_.v2` is a file. + let dir = tmp("reserved-dotted-name"); + let mut s = session(8); + s.execute("slice.new", json!({"rect": [0, 0, 16, 16], "name": "logo"})).unwrap(); + let path = format!("{dir}/nul.v2.png"); + let result = s.execute("file.export.saveForWebLegacy", json!({"path": path, "slices": "all", "format": "png24", "html": true})).unwrap(); + let html = result["html"].as_str().unwrap(); + assert!(html.ends_with("/nul_.v2.html"), "{html}"); + assert!(std::path::Path::new(html).is_file()); +} + #[test] fn platform_writer_failure_stops_export_and_preserves_saved_settings() { let mut s = session(8); @@ -188,7 +201,7 @@ fn estimates_every_format_at_every_depth() { for depth in [8, 16, 32] { let mut s = session(depth); let mut sizes = Vec::new(); - for f in ["gif", "png8", "png24", "jpeg", "wbmp"] { + for f in ["gif", "png8", "png24", "jpeg", "wbmp", "webp"] { let r = s.execute("file.export.saveForWebLegacy", json!({"format": f, "colors": 16})).unwrap(); assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(64), Some(48)), "{f}"); assert!(r["bytes"].as_u64().unwrap() > 20, "{f}"); @@ -286,6 +299,107 @@ fn jpeg_quality_progressive_and_metadata() { assert!(decode(&lo).meta.xmp.is_none()); } +/// WebP (#2415, #6391): lossy at the chosen quality or lossless, keeping translucency unless +/// transparency is off, with the chosen metadata and an optional colour profile. +#[test] +fn webp_lossy_lossless_transparency_and_metadata() { + let dir = tmp("webp"); + let mut s = session(16); + s.execute("file.fileInfo", json!({"copyright": "© Example", "description": "secret notes"})).unwrap(); + s.execute("edit.assignProfile", json!({"profile": "srgb"})).unwrap(); + let path = |name: &str| format!("{dir}/{name}"); + let mut export = |name: &str, p: Value| { + let mut p = p; + p["format"] = json!("webp"); + p["path"] = json!(path(name)); + s.execute("file.export.saveForWebLegacy", p).unwrap(); + std::fs::read(path(name)).unwrap() + }; + let lo = export("lo.webp", json!({"quality": 5})); + let hi = export("hi.webp", json!({"quality": 95, "embedIcc": true})); + let lossless = export("lossless.webp", json!({"lossless": true, "metadata": "none"})); + let opaque = export("opaque.webp", json!({"transparency": false, "matte": "#00ff00"})); + for bytes in [&lo, &hi, &lossless, &opaque] { + assert_eq!((&bytes[0..4], &bytes[8..12]), (&b"RIFF"[..], &b"WEBP"[..])); + } + assert!(lo.len() < hi.len(), "quality drives the size: {} vs {}", lo.len(), hi.len()); + let has = |b: &[u8], fourcc: &[u8]| b.windows(4).any(|w| w == fourcc); + assert!(has(&hi, b"VP8 ") && has(&hi, b"ALPH"), "lossy keeps translucency in an alpha chunk"); + assert!(has(&lossless, b"VP8L") && !has(&lossless, b"VP8 ")); + assert!(!has(&opaque, b"ALPH"), "transparency off composites over the matte"); + + // Lossless is the PNG-24 export's pixels exactly. + s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "path": path("same.png")})).unwrap(); + let (a, b) = (decode(&path("lossless.webp")), decode(&path("same.png"))); + let rgba = |i: &photocraft_codecs::Image| i.convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8).data().to_vec(); + assert_eq!(rgba(&a), rgba(&b)); + // The transparent corner stays transparent; with transparency off it is the matte. + assert_eq!(rgba(&a)[3], 0); + let o = rgba(&decode(&path("opaque.webp"))); + assert!(o[3] == 255 && o[1] > 200 && o[0] < 60, "{:?}", &o[..4]); + + let meta = decode(&path("hi.webp")); + let xmp = meta.meta.xmp.unwrap_or_default(); + assert!(xmp.contains("Example") && !xmp.contains("secret"), "copyright only"); + assert!(meta.icc.is_some(), "Embed Color Profile"); + assert!(decode(&path("lo.webp")).icc.is_none()); + assert!(a.meta.xmp.is_none(), "metadata none"); +} + +/// Sizes WebP cannot hold are an error, not a crash. +#[test] +fn webp_too_large_is_an_error() { + let mut s = session(8); + for lossless in [false, true] { + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "webp", "lossless": lossless, "width": 20000, "height": 2})); + assert!(r.is_err(), "lossless {lossless}: {r:?}"); + } +} + +#[test] +fn platform_writer_checks_settings_before_dropping_floating_pixels() { + let mut s = session(8); + s.execute("select.rect", json!({"x": 8, "y": 8, "width": 4, "height": 4})).unwrap(); + s.execute("select.float", json!({"dx": 32})).unwrap(); + let rev = s.active().unwrap().revision; + let params = json!({"format": "webp", "path": "moved.webp", "lossless": "yes"}); + let mut output = Vec::new(); + let refused = save_for_web_with_writer(&mut s, ¶ms, &mut |_, bytes| { + output.extend_from_slice(bytes); + Ok(()) + }); + assert!(refused.unwrap_err().to_string().contains("lossless")); + assert!(output.is_empty()); + assert!(s.file_menu.last_web.is_none()); + assert_eq!(s.active().unwrap().revision, rev, "the document is untouched"); + assert!(crate::float_cmds::floating(s.active().unwrap()).is_some(), "a refused export must not commit a floating move"); +} + +/// An on/off option that isn't true or false is refused (`"lossless": "true"` would otherwise +/// write lossy WebP), and the refused export writes and remembers nothing. +#[test] +fn non_boolean_flags_are_refused_without_changing_anything() { + let dir = tmp("flags"); + let mut s = session(8); + let out = format!("{dir}/out.webp"); + let flags = ["transparency", "interlaced", "progressive", "lossless", "optimized", "embedIcc", "convertToSrgb"]; + for k in flags { + for v in [json!("true"), json!(1), json!([true])] { + let rev = s.active().unwrap().revision; + let mut p = json!({"format": "webp", "path": out}); + p[k] = v.clone(); + let err = s.execute("file.export.saveForWebLegacy", p).unwrap_err().to_string(); + assert!(err.contains(k), "{k}: {v}: {err}"); + assert_eq!(s.active().unwrap().revision, rev, "{k}: the document is untouched"); + assert!(!std::path::Path::new(&out).exists(), "{k}: nothing written"); + assert!(s.file_menu.last_web.is_none(), "{k}: nothing remembered"); + } + } + // null is the same as leaving it out. + s.execute("file.export.saveForWebLegacy", json!({"format": "webp", "path": out, "lossless": null})).unwrap(); + assert!(std::path::Path::new(&out).exists()); +} + #[test] fn slices_export_with_html_table() { let dir = tmp("slices"); @@ -437,6 +551,78 @@ fn export_preferences_drive_quick_export() { s.execute("prefs.reset", json!({"path": "export"})).unwrap(); } +/// #6599: with Location = Same Folder, a document opened from `photo.png` would be exported over +/// `photo.png` itself. Without a `path` that is refused and the file is left alone; named +/// explicitly (the save dialog's answer) it is written. +#[test] +fn quick_export_never_replaces_the_documents_own_file_unasked() { + let dir = tmp("quick_own_file"); + let mut s = session(8); + s.execute("file.export.exportPreferences", json!({"quickExportLocation": "sameFolder", "quickExportFormat": "png"})).unwrap(); + let own = format!("{dir}/photo.png"); + std::fs::write(&own, b"original").unwrap(); + s.active_mut().unwrap().path = Some(own.clone()); + assert_eq!(quick_export_target(&s), Some(own.clone())); + assert!(quick_export_target_is_own_file(&s)); + let e = s.execute("file.export.quickExport", json!({})).unwrap_err().to_string(); + assert!(e.contains("own file"), "{e}"); + assert_eq!(std::fs::read(&own).unwrap(), b"original", "the source is untouched"); + s.execute("file.export.quickExport", json!({"path": own})).unwrap(); + assert_eq!(decode(&own).dimensions(), (64, 48)); + // Another format, or a PSD's earlier export beside it, is still written without asking. + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "jpg"})).unwrap(); + assert!(!quick_export_target_is_own_file(&s)); + assert_eq!(s.execute("file.export.quickExport", json!({})).unwrap()["path"], json!(format!("{dir}/photo.jpg"))); + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "png"})).unwrap(); + s.active_mut().unwrap().path = Some(format!("{dir}/photo.psd")); + assert!(!quick_export_target_is_own_file(&s)); + assert_eq!(s.execute("file.export.quickExport", json!({})).unwrap()["path"], json!(own)); + s.execute("prefs.reset", json!({"path": "export"})).unwrap(); +} + +/// #8519: Quick Export as PNG renamed to `photo.jpeg` in the save dialog wrote PNG bytes under a +/// JPEG name, which upload checks refuse. A Quick Export extension picks the format; another +/// image extension is refused; no extension keeps Export Preferences' format. +#[test] +fn quick_export_writes_the_format_its_path_names() { + let dir = tmp("quick_ext"); + let mut s = session(8); + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "png"})).unwrap(); + for (name, format, magic) in [ + ("a.jpeg", "jpg", &b"\xFF\xD8\xFF"[..]), + ("b.JPG", "jpg", b"\xFF\xD8\xFF"), + ("c.gif", "gif", b"GIF8"), + ("d.webp", "webp", b"RIFF"), + ("e", "png", b"\x89PNG"), + ] { + let path = format!("{dir}/{name}"); + let r = s.execute("file.export.quickExport", json!({"path": path})).unwrap(); + assert_eq!(r["format"], json!(format), "{name}"); + assert!(std::fs::read(&path).unwrap().starts_with(magic), "{name} holds {format} data"); + } + let tif = format!("{dir}/f.tif"); + let e = s.execute("file.export.quickExport", json!({"path": tif})).unwrap_err().to_string(); + assert!(e.contains("Export As"), "{e}"); + assert!(!std::path::Path::new(&tif).exists()); + s.execute("prefs.reset", json!({"path": "export"})).unwrap(); +} + +#[test] +fn same_file_sees_through_other_names_for_one_file() { + let dir = tmp("same_file"); + let (a, b, c) = (format!("{dir}/a.png"), format!("{dir}/b.png"), format!("{dir}/c.png")); + std::fs::write(&a, b"a").unwrap(); + std::fs::write(&c, b"c").unwrap(); + assert!(crate::file_cmds::same_file(&a, &a)); + assert!(crate::file_cmds::same_file(&a, format!("{dir}/./a.png"))); + assert!(!crate::file_cmds::same_file(&a, &c)); + assert!(!crate::file_cmds::same_file(&a, &b), "a missing file is not the same file"); + std::fs::hard_link(&a, &b).unwrap(); + assert!(crate::file_cmds::same_file(&a, &b), "a hard link is the same file"); + #[cfg(windows)] + assert!(crate::file_cmds::same_file(&a, format!("{dir}/A.PNG")), "NTFS names ignore case"); +} + #[test] fn webp_export_preferences_select_lossless_or_lossy_output() { let dir = tmp("quick_webp_quality"); @@ -485,6 +671,20 @@ fn webp_export_preferences_select_lossless_or_lossy_output() { } } +#[test] +fn generator_spaced_size_keeps_only_the_file_name() { + for (name, w, h, file) in [ + ("2 x 2 2 icon.png", 2.0, 2.0, "2 icon.png"), + ("1in x 1in in.png", 72.0, 72.0, "in.png"), + ("2px x 1px x.png", 2.0, 1.0, "x.png"), + ("2in x 1in label name.png", 144.0, 72.0, "label name.png"), + ] { + let a = parse_asset_name(name, 72.0); + assert_eq!(a.len(), 1, "{name}"); + assert_eq!((a[0].width, a[0].height, a[0].file.as_str()), (Some(w), Some(h), file), "{name}"); + } +} + #[test] fn generator_naming_grammar() { let a = parse_asset_name("200% foo@2x.png, 48x48 icons/bar.png8 + photo.jpg8", 72.0); @@ -511,10 +711,35 @@ fn generator_naming_grammar() { assert!(parse_defaults("defaults are nice", 72.0).is_none()); } +#[test] +fn image_assets_needs_the_generator_preference() { + let dir = tmp("generator_pref"); + let mut s = session(8); + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.renameLayer", json!({"layer": id.0, "name": "logo.png"})).unwrap(); + assert!(!s.prefs().plug_ins.generator_enabled, "off by default, as in Photoshop"); + assert!(!s.is_enabled("file.generate.imageAssets"), "unavailable until it is enabled"); + assert!(s.execute("file.generate.imageAssets", json!({"dir": dir})).is_err()); + + s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": true})).unwrap(); + assert!(s.is_enabled("file.generate.imageAssets")); + assert_eq!(s.execute("file.generate.imageAssets", json!({"dir": dir})).unwrap()["enabled"], true); + s.active_mut().unwrap().path = Some(format!("{dir}/doc.psd")); + assert!(crate::automate_cmds::document_saved(&mut s, 0).is_some(), "assets are written on save"); + + // Turning it off again takes the menu item away and stops the export, even for a document + // left switched on. + s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": false})).unwrap(); + assert!(!s.is_enabled("file.generate.imageAssets")); + assert!(crate::automate_cmds::document_saved(&mut s, 0).is_none(), "no assets while Generator is off"); +} + #[test] fn image_assets_are_generated_now_and_on_save() { let dir = tmp("assets"); let mut s = session(8); + // Preferences ▸ Plug-ins ▸ Enable Generator: the feature is opt-in. + s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": true})).unwrap(); let id = s.active().unwrap().active_layer.unwrap(); s.execute("layer.renameLayer", json!({"layer": id.0, "name": "logo.png, 50% small/logo.gif, logo.jpg80%"})).unwrap(); let r = s.execute("file.generate.imageAssets", json!({"dir": dir})).unwrap(); @@ -537,7 +762,10 @@ fn image_assets_are_generated_now_and_on_save() { let s2 = session(8); let doc = s2.active().unwrap().doc.clone(); let mut d = (*doc).clone(); - d.layers.push(Layer::new("default 200% @2x", LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None }))); + d.layers.push(Layer::new( + "default 200% @2x", + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), + )); let lid = d.layers[1].id; d.layer_mut(lid).unwrap().name = "icon.png".into(); let (files, errors) = generate_assets(&d, &dir); @@ -546,6 +774,117 @@ fn image_assets_are_generated_now_and_on_save() { assert_eq!(decode(&files[0]).dimensions(), (128, 80)); } +/// Photoshop's Generator exports an artboard named like an asset at the board's size, transparent +/// edges included (generator-assets renderer: the canvas bounds are the artboard's), with no +/// preference. Layers inside the board are still trimmed to their pixels. +#[test] +fn image_assets_keep_full_artboards() { + let dir = tmp("asset-artboards"); + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48, "name": "boards.psd", "background": "transparent"})).unwrap(); + s.edit("board", |doc, _| { + let fmt = doc.pixel_format(); + let mut logo = Layer::raster("logo.png", fmt); + logo.surface_mut().unwrap().fill_rect(Rect::new(12, 10, 20, 16), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + let board = + photocraft_doc::Artboard { rect: Rect::new(4, 6, 44, 36), background: photocraft_doc::ArtboardBackground::Transparent, preset: String::new() }; + let group = photocraft_doc::Group { children: vec![logo], expanded: true, artboard: Some(board), frame_stroke: None }; + doc.layers = vec![Layer::new("board.png", LayerContent::Group(group))]; + Ok(()) + }) + .unwrap(); + + s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": true})).unwrap(); + let r = s.execute("file.generate.imageAssets", json!({"on": true, "dir": dir})).unwrap(); + assert!(r["errors"].as_array().unwrap().is_empty(), "{r}"); + let board = decode(&format!("{dir}/board.png")); + assert_eq!(board.dimensions(), (40, 30), "the whole board, not its trimmed pixels"); + let px = board.to_rgba8(); + let at = |x: usize, y: usize| &px[(y * 40 + x) * 4..(y * 40 + x) * 4 + 4]; + assert_eq!(at(0, 0)[3], 0, "the board's empty corner stays transparent"); + assert_eq!(at(39, 29)[3], 0); + // The logo keeps its place on the board: document (12, 10) is board (8, 4). + assert!(at(8, 4)[0] > 250 && at(8, 4)[3] == 255, "{:?}", at(8, 4)); + assert_eq!(at(7, 4)[3], 0); + assert_eq!(decode(&format!("{dir}/logo.png")).dimensions(), (8, 6), "a layer on the board is still trimmed"); +} + +/// An artboard asset with nothing on the board (empty, or its contents all outside it) is +/// refused like an empty layer, not written as a blank board: Photoshop's Generator refuses a +/// zero-bounds pixmap before it pads to the board. +#[test] +fn image_assets_refuse_empty_artboards() { + let dir = tmp("asset-empty-artboards"); + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48, "name": "boards.psd", "background": "transparent"})).unwrap(); + s.edit("boards", |doc, _| { + let fmt = doc.pixel_format(); + let mut outside = Layer::raster("outside", fmt); + outside.surface_mut().unwrap().fill_rect(Rect::new(50, 40, 60, 46), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + let board = |x0| photocraft_doc::Artboard { + rect: Rect::new(x0, 0, x0 + 20, 20), + background: photocraft_doc::ArtboardBackground::Transparent, + preset: String::new(), + }; + let empty = photocraft_doc::Group { children: vec![], expanded: true, artboard: Some(board(0)), frame_stroke: None }; + let off = photocraft_doc::Group { children: vec![outside], expanded: true, artboard: Some(board(24)), frame_stroke: None }; + doc.layers = vec![Layer::new("empty.png", LayerContent::Group(empty)), Layer::new("off.png", LayerContent::Group(off))]; + Ok(()) + }) + .unwrap(); + s.execute("prefs.set", json!({"path": "plugIns.generatorEnabled", "value": true})).unwrap(); + let r = s.execute("file.generate.imageAssets", json!({"on": true, "dir": dir})).unwrap(); + assert_eq!(r["files"], json!([]), "{r}"); + let errors = r["errors"].as_array().unwrap(); + assert_eq!(errors.len(), 2, "{r}"); + assert!(errors.iter().all(|e| e["error"].as_str().unwrap().contains("the layer is empty")), "{r}"); + assert!(!std::path::Path::new(&format!("{dir}/empty.png")).exists()); + assert!(!std::path::Path::new(&format!("{dir}/off.png")).exists()); +} + +/// #6614: two layers, or two default variants, naming one file never overwrite each other: the +/// layer higher in the Layers panel keeps the file, whatever the case of its name, and the +/// others are reported. +#[test] +fn image_assets_never_write_one_file_twice() { + let dir = tmp("asset-collisions"); + let mut s = session(8); + s.edit("paint", |doc, active| { + let fmt = doc.pixel_format(); + let mut l = Layer::raster("Top", fmt); + l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 4, 4), &photocraft_raster::from_rgba(&fmt, [0.0, 0.0, 1.0, 1.0])); + let id = doc.insert_above(None, l); + *active = Some(id); + Ok(()) + }) + .unwrap(); + let mut d = (*s.active().unwrap().doc).clone(); + let id = |d: &photocraft_doc::Document, name: &str| d.walk().into_iter().find(|(_, _, l)| l.name == name).unwrap().2.id; + let (top, logo) = (id(&d, "Top"), id(&d, "Logo")); + d.layer_mut(top).unwrap().name = "Logo.PNG".into(); + d.layer_mut(logo).unwrap().name = "logo.png, logo.png".into(); + let (files, errors) = generate_assets(&d, &dir); + assert_eq!(files.iter().map(|p| std::path::PathBuf::from(p.as_str())).collect::>(), vec![std::path::Path::new(&dir).join("Logo.png")]); + assert_eq!(decode(&files[0]).dimensions(), (4, 4), "the top layer keeps the file"); + assert_eq!(errors.len(), 2, "{errors:?}"); + assert!(errors.iter().all(|e| e["error"].as_str().unwrap().contains("another layer already writes this file")), "{errors:?}"); + assert_eq!(std::fs::read_dir(&dir).unwrap().count(), 1, "one file, not a second one under another case"); + + // Two default variants that land on one name: the first keeps it. + let dir = tmp("asset-variant-collisions"); + d.layer_mut(top).unwrap().name = "icon.png".into(); + d.layer_mut(logo).unwrap().name = "Logo".into(); + d.layers.push(Layer::new( + "default 50% + 200% + 200% @2x", + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), + )); + let (files, errors) = generate_assets(&d, &dir); + let names: Vec<_> = files.iter().map(|p| std::path::Path::new(p).file_name().unwrap().to_string_lossy().into_owned()).collect(); + assert_eq!(names, ["icon.png", "icon@2x.png"]); + assert_eq!(decode(&files[0]).dimensions(), (2, 2), "the 50% variant keeps icon.png"); + assert_eq!(errors.len(), 1, "{errors:?}"); +} + #[test] fn image_assets_reject_traversal_from_names_and_defaults() { let base = tmp("asset-path-guard"); @@ -566,7 +905,7 @@ fn image_assets_reject_traversal_from_names_and_defaults() { d.layer_mut(id).unwrap().name = "safe.png".into(); d.layers.push(Layer::new( "default ../ + /outside/ + ..\\bad/ + C:\\drive/ + 50% nested/", - LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None }), + LayerContent::Group(photocraft_doc::Group { children: vec![], expanded: false, artboard: None, frame_stroke: None }), )); let (files, errors) = generate_assets(&d, &dir); assert_eq!( @@ -621,6 +960,9 @@ fn web_settings_from_params() { assert!((st.web_snap - 0.5).abs() < 1e-6); assert!(WebSettings::from_params(&json!({"preset": "Nope"}), "x").is_err()); assert!(WebSettings::from_params(&json!({"palette": "weird"}), "x").is_err()); + let webp = WebSettings::from_params(&json!({"format": "webp", "lossless": true, "quality": 70}), "x").unwrap(); + assert_eq!((webp.format, webp.lossless, webp.quality), (WebFormat::Webp, true, 70)); + assert_eq!(WebSettings::from_params(&webp.to_params(), "x").unwrap(), webp); for p in PRESETS { let st = preset_settings(p).unwrap(); assert_eq!(WebSettings::from_params(&st.to_params(), "x").unwrap(), st, "{p} round-trips through params"); @@ -633,9 +975,9 @@ fn previews_show_the_optimised_pixels() { let doc = s.active().unwrap().doc.clone(); let (wd, _, _) = web_document(&doc, &json!({}), &WebSettings::default()).unwrap(); let buf = photocraft_compose::flatten(&wd); - for f in ["jpeg", "gif", "png8", "png24", "wbmp"] { + for f in ["jpeg", "gif", "png8", "png24", "wbmp", "webp"] { let st = WebSettings::from_params(&json!({"format": f, "quality": 90, "transparency": false}), "x").unwrap(); - let o = optimize(&buf.px, 64, wd.bounds(), &st, None, None, 72.0, true).unwrap(); + let o = optimize(&buf.px, 64, wd.bounds(), &st, None, None, None, 72.0, true).unwrap(); assert_eq!(o.preview.len(), 64 * 48 * 4, "{f}"); // Red square centre stays red (WBMP: dark). let i = (16 * 64 + 16) * 4; @@ -645,7 +987,7 @@ fn previews_show_the_optimised_pixels() { } else { assert!(px[0] > 200 && px[1] < 60 && px[2] < 60, "{f}: {px:?}"); } - let none = optimize(&buf.px, 64, wd.bounds(), &st, None, None, 72.0, false).unwrap(); + let none = optimize(&buf.px, 64, wd.bounds(), &st, None, None, None, 72.0, false).unwrap(); assert!(none.preview.is_empty()); assert_eq!(none.bytes, o.bytes); } @@ -656,7 +998,7 @@ fn jpeg_preview_at_odd_sizes() { for (w, h, q) in [(640usize, 431usize, 90), (640, 430, 60), (333, 222, 30)] { let px: Vec<[f32; 4]> = (0..w * h).map(|i| [((i % w) as f32 / w as f32), 0.3, ((i / w) as f32 / h as f32), 1.0]).collect(); let st = WebSettings::from_params(&json!({"format": "jpeg", "quality": q}), "x").unwrap(); - let o = optimize(&px, w, Rect::new(0, 0, w as i32, h as i32), &st, None, None, 72.0, true).unwrap(); + let o = optimize(&px, w, Rect::new(0, 0, w as i32, h as i32), &st, None, None, None, 72.0, true).unwrap(); for &(x, y) in &[(10usize, 10usize), (w - 10, h / 2), (w / 2, h - 5)] { let i = (y * w + x) * 4; let want = [(x as f32 / w as f32 * 255.0) as i32, 76, (y as f32 / h as f32 * 255.0) as i32]; @@ -685,6 +1027,21 @@ fn slices_with_the_same_name_get_distinct_files() { assert!(html.contains("images/tile.gif") && html.contains("images/tile_2.gif")); } +#[test] +fn overlapping_slices_export_each_pixel_once() { + // #7770: without HTML too, a slice covered by a later one is written as its visible pieces. + let dir = tmp("overlap_images"); + let mut s = session(8); + s.execute("slice.new", json!({"rect": [0, 0, 40, 30], "name": "under"})).unwrap(); + s.execute("slice.new", json!({"rect": [20, 15, 40, 30], "name": "over"})).unwrap(); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "slices": "user"})).unwrap(); + let mut files: Vec = r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_string()).collect(); + let dims: Vec<(u32, u32)> = files.iter().map(|f| decode(f).dimensions()).collect(); + files.iter_mut().for_each(|f| *f = f.rsplit('/').next().unwrap().to_string()); + assert_eq!(files, ["under.png", "under-03.png", "over.png"]); + assert_eq!(dims, [(40, 15), (20, 15), (40, 30)]); +} + #[test] fn overlapping_slices_keep_the_later_slice_in_html() { // #909: the later slice is on top; the earlier one shows only where it isn't covered. @@ -709,8 +1066,254 @@ fn overlapping_slices_keep_the_later_slice_in_html() { }) .sum(); assert_eq!(area, 64 * 48); - assert!(files.iter().any(|f| f.ends_with("images/under_01.png")), "the covered slice is cut into pieces: {files:?}"); + // As Photoshop names subslices: the first piece keeps the name, the others add their number. + let under = files.iter().find(|f| f.ends_with("images/under.png")).unwrap(); + assert_eq!(decode(under).dimensions(), (40, 15), "the covered slice is cut into pieces: {files:?}"); + assert!(files.iter().any(|f| f.ends_with("images/under-03.png")), "{files:?}"); } #[path = "color_table_tests.rs"] mod color_table_tests; + +/// A little-endian camera EXIF: IFD0 with Make, Orientation 6, 300 ppi and pointers to an Exif +/// IFD (DateTimeOriginal) and a GPS IFD (GPSLatitudeRef); IFD0 links to an IFD1 thumbnail. +fn camera_exif() -> Vec { + let entry = |v: &mut Vec, tag: u16, ty: u16, count: u32, value: u32| { + v.extend_from_slice(&tag.to_le_bytes()); + v.extend_from_slice(&ty.to_le_bytes()); + v.extend_from_slice(&count.to_le_bytes()); + v.extend_from_slice(&value.to_le_bytes()); + }; + let (ifd0, data0) = (8u32, 8 + 2 + 7 * 12 + 4); + let (make, xres, yres) = (data0, data0 + 10, data0 + 18); + let exif_ifd = data0 + 26; + let date = exif_ifd + 2 + 12 + 4; + let gps_ifd = date + 20; + let ifd1 = gps_ifd + 2 + 12 + 4; + let mut v = b"II*\0".to_vec(); + v.extend_from_slice(&ifd0.to_le_bytes()); + v.extend_from_slice(&7u16.to_le_bytes()); + entry(&mut v, 0x010F, 2, 9, make); + entry(&mut v, 0x0112, 3, 1, 6); + entry(&mut v, 282, 5, 1, xres); + entry(&mut v, 283, 5, 1, yres); + entry(&mut v, 296, 3, 1, 2); + entry(&mut v, 0x8769, 4, 1, exif_ifd); + entry(&mut v, 0x8825, 4, 1, gps_ifd); + v.extend_from_slice(&ifd1.to_le_bytes()); + v.extend_from_slice(b"TestMake\0\0"); + for _ in 0..2 { + v.extend_from_slice(&300u32.to_le_bytes()); + v.extend_from_slice(&1u32.to_le_bytes()); + } + v.extend_from_slice(&1u16.to_le_bytes()); + entry(&mut v, 0x9003, 2, 20, date); + v.extend_from_slice(&0u32.to_le_bytes()); + v.extend_from_slice(b"2024:05:01 10:30:00\0"); + v.extend_from_slice(&1u16.to_le_bytes()); + entry(&mut v, 0x0001, 2, 2, u32::from_le_bytes(*b"N\0\0\0")); + v.extend_from_slice(&0u32.to_le_bytes()); + v.extend_from_slice(&1u16.to_le_bytes()); + entry(&mut v, 0x0103, 3, 1, 6); + v.extend_from_slice(&0u32.to_le_bytes()); + assert_eq!(v.len(), ifd1 as usize + 18); + v +} + +/// The payload (after `Exif\0\0`) of the file's EXIF APP1 segment, read from the markers. +fn exif_app1(bytes: &[u8]) -> Option> { + let mut i = 2; + while i + 4 <= bytes.len() && bytes[i] == 0xFF && bytes[i + 1] != 0xDA { + let len = u16::from_be_bytes([bytes[i + 2], bytes[i + 3]]) as usize; + let body = &bytes[i + 4..i + 2 + len]; + if bytes[i + 1] == 0xE1 && body.starts_with(b"Exif\0\0") { + return Some(body[6..].to_vec()); + } + i += 2 + len; + } + None +} + +/// IFD0's entries (tag → inline value) and its link to the next IFD, little-endian only. +fn ifd0(tiff: &[u8]) -> (std::collections::HashMap, u32) { + let u16_at = |o: usize| u16::from_le_bytes([tiff[o], tiff[o + 1]]); + let u32_at = |o: usize| u32::from_le_bytes([tiff[o], tiff[o + 1], tiff[o + 2], tiff[o + 3]]); + assert_eq!(&tiff[..4], b"II*\0"); + let at = u32_at(4) as usize; + let n = u16_at(at) as usize; + let tags = (0..n) + .map(|k| { + let e = at + 2 + k * 12; + let value = if u16_at(e + 2) == 3 { u32::from(u16_at(e + 8)) } else { u32_at(e + 8) }; + (u16_at(e), value) + }) + .collect(); + (tags, u32_at(at + 2 + n * 12)) +} + +fn photo_session(exif: Vec) -> Session { + let mut s = session(8); + s.execute("file.fileInfo", json!({"copyright": "© Example"})).unwrap(); + s.edit("exif", |doc, _| { + doc.metadata.exif = Some(std::sync::Arc::new(exif)); + Ok(()) + }) + .unwrap(); + s +} + +/// #7436: Save for Web's "All" writes the photo's EXIF, as Save a Copy does: upright +/// (Orientation 1), without the source's thumbnail and at the exported resolution. Every other +/// choice, "All Except Camera Info" included, leaves the camera data (and GPS) out. +#[test] +fn save_for_web_all_writes_the_photos_exif() { + let dir = tmp("exif"); + let mut s = photo_session(camera_exif()); + let export = |s: &mut Session, metadata: &str| { + let path = format!("{dir}/{metadata}.jpg"); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "jpeg", "metadata": metadata, "path": path})).unwrap(); + (std::fs::read(&path).unwrap(), r) + }; + let (all, r) = export(&mut s, "all"); + let exif = exif_app1(&all).expect("an EXIF APP1 segment"); + let camera = photocraft_algo::exif::read(&exif); + assert_eq!(camera.make.as_deref(), Some("TestMake")); + assert_eq!(camera.date_taken.as_deref(), Some("2024:05:01 10:30:00")); + let (tags, next) = ifd0(&exif); + assert_eq!(tags.get(&0x0112), Some(&1), "the exported pixels are already upright"); + assert!(tags.contains_key(&0x8825), "GPS kept"); + assert_eq!(next, 0, "the source's thumbnail is not linked"); + assert_eq!(photocraft_codecs::exif_resolution(&exif), Some((72.0, 72.0)), "the document's resolution, not the source's 300"); + assert!(decode(&format!("{dir}/all.jpg")).meta.xmp.unwrap_or_default().contains("Example")); + for m in ["none", "copyright", "copyrightAndContact", "allExceptCameraInfo"] { + let (bytes, r) = export(&mut s, m); + assert_eq!(exif_app1(&bytes), None, "{m}"); + assert_eq!(r["warnings"], json!([]), "{m}"); + } + assert_eq!(r["warnings"], json!([]), "it fits"); + let xmp = decode(&format!("{dir}/allExceptCameraInfo.jpg")).meta.xmp.unwrap_or_default(); + assert!(xmp.contains("Example"), "all the XMP"); + assert_eq!(WebSettings::from_params(&json!({"metadata": "allExceptCameraInfo"}), "x").unwrap().to_params()["metadata"], "allExceptCameraInfo"); +} + +/// WebP holds EXIF in both modes: "All" keeps an EXIF-only photo's camera data (no XMP needed to +/// trigger the metadata), every other choice leaves it out. +#[test] +fn save_for_web_webp_all_keeps_an_exif_only_photos_exif() { + let dir = tmp("webp_exif"); + let mut s = session(8); + s.edit("exif", |doc, _| { + doc.metadata.exif = Some(std::sync::Arc::new(camera_exif())); + Ok(()) + }) + .unwrap(); + for lossless in [false, true] { + for metadata in ["all", "none", "copyright", "allExceptCameraInfo"] { + let path = format!("{dir}/{metadata}-{lossless}.webp"); + s.execute("file.export.saveForWebLegacy", json!({"format": "webp", "lossless": lossless, "metadata": metadata, "path": path})).unwrap(); + let meta = decode(&path).meta; + if metadata == "all" { + let exif = meta.exif.unwrap_or_else(|| panic!("EXIF kept (lossless {lossless})")); + assert_eq!(photocraft_algo::exif::read(&exif).make.as_deref(), Some("TestMake"), "lossless {lossless}"); + assert!(meta.xmp.is_none(), "the photo had no XMP"); + } else { + assert!(meta.exif.is_none(), "{metadata} leaves the camera data out (lossless {lossless})"); + } + } + } +} + +#[test] +fn quick_export_all_writes_the_photos_exif() { + let dir = tmp("quick_exif"); + let mut s = photo_session(camera_exif()); + let export = |s: &mut Session, metadata: &str| { + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "jpg", "metadata": metadata})).unwrap(); + let path = format!("{dir}/{metadata}.jpg"); + let r = s.execute("file.export.quickExport", json!({"path": path})).unwrap(); + (std::fs::read(&path).unwrap(), r) + }; + let (all, r) = export(&mut s, "all"); + assert_eq!(photocraft_algo::exif::read(&exif_app1(&all).expect("EXIF")).make.as_deref(), Some("TestMake")); + assert_eq!(r["warnings"], json!([])); + assert_eq!(exif_app1(&export(&mut s, "copyright").0), None); + assert_eq!(exif_app1(&export(&mut s, "none").0), None); + s.execute("prefs.reset", json!({"path": "export"})).unwrap(); +} + +/// EXIF too large for its one segment is left out and said so; a truncated one doesn't panic. +#[test] +fn web_exif_that_does_not_fit_is_reported_and_bad_exif_is_harmless() { + let dir = tmp("exif_bad"); + let mut big = camera_exif(); + big.resize(70_000, 0); + let mut s = photo_session(big); + let path = format!("{dir}/big.jpg"); + let r = s.execute("file.export.saveForWebLegacy", json!({"format": "jpeg", "metadata": "all", "path": path})).unwrap(); + assert_eq!(exif_app1(&std::fs::read(&path).unwrap()), None); + assert!(r["warnings"][0].as_str().is_some_and(|w| w.contains("EXIF (70000 bytes)")), "{r}"); + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "jpg", "metadata": "all"})).unwrap(); + let r = s.execute("file.export.quickExport", json!({"path": format!("{dir}/quick.jpg")})).unwrap(); + assert!(r["warnings"][0].as_str().is_some_and(|w| w.contains("EXIF")), "{r}"); + s.execute("prefs.reset", json!({"path": "export"})).unwrap(); + + for cut in [3, 9, 40, 100] { + let mut s = photo_session(camera_exif()[..cut].to_vec()); + let path = format!("{dir}/cut{cut}.jpg"); + s.execute("file.export.saveForWebLegacy", json!({"format": "jpeg", "metadata": "all", "path": path})).unwrap(); + assert_eq!(decode(&path).dimensions(), (64, 48)); + } +} + +#[test] +fn quick_export_exr_preserves_linear_hdr_and_alpha() { + use photocraft_codecs::{ChannelLayout, Format, Image, SampleType}; + let dir = tmp("quick_exr_hdr"); + let samples = [-0.25, 2.0, 0.125, 1.0, 4.0, 0.25, 0.5, 0.5]; + let image = Image::from_f32(2, 1, ChannelLayout::Rgba, &samples).unwrap(); + let bytes = photocraft_codecs::encode(&image, Format::OpenExr, &Default::default()).unwrap(); + let doc = photocraft_io::import("hdr.exr", &bytes).unwrap().document; + let mut s = Session::new(); + s.add_document(doc, Some(format!("{dir}/source.psd"))); + for convert in [true, false] { + s.execute("file.export.exportPreferences", json!({"quickExportFormat": "exr", "quickExportLocation": "sameFolder", "convertToSrgb": convert})).unwrap(); + let result = s.execute("file.export.quickExport", json!({})).unwrap(); + assert_eq!(result["format"], "exr"); + let path = format!("{dir}/source.exr"); + assert_eq!(result["path"], path); + assert_eq!(result["bytes"].as_u64(), Some(std::fs::metadata(&path).unwrap().len())); + let decoded = decode(&path); + assert_eq!(decoded.sample_type(), SampleType::F32); + let actual = decoded.convert(ChannelLayout::Rgba, SampleType::F32).to_f32_samples().unwrap(); + for (got, expected) in actual.iter().zip(samples) { + assert!((got - expected).abs() < 0.0001, "EXR preserves scene values: {actual:?}"); + } + assert_eq!(actual.len(), samples.len()); + } + // The format preference must never silently overwrite the source EXR. + let own = format!("{dir}/source.exr"); + s.active_mut().unwrap().path = Some(own.clone()); + let before = std::fs::read(&own).unwrap(); + assert!(s.execute("file.export.quickExport", json!({})).is_err()); + assert_eq!(std::fs::read(&own).unwrap(), before); + s.execute("file.export.quickExport", json!({"path": format!("{dir}/explicit.exr")})).unwrap(); + assert_eq!(decode(&format!("{dir}/explicit.exr")).dimensions(), (2, 1)); +} + +/// #9325: an untagged document is treated as sRGB, and with Embed Color Profile on the file +/// says so, as Photoshop's Save for Web does; off, it still carries none. +#[test] +fn embed_color_profile_tags_an_untagged_document_srgb() { + let mut s = session(8); + assert!(s.active().unwrap().doc.icc_profile.is_none(), "a new document is untagged"); + let dir = tmp("assumed-icc"); + let profile = |path: &str| decode(path).icc.map(|b| photocraft_cms::Profile::parse(&b).unwrap().description); + for (format, ext) in [("jpeg", "jpg"), ("png24", "png")] { + let on = format!("{dir}/on.{ext}"); + s.execute("file.export.saveForWebLegacy", json!({"format": format, "embedIcc": true, "path": on})).unwrap(); + assert_eq!(profile(&on).as_deref(), Some(photocraft_cms::Builtin::Srgb.description()), "{format}"); + let off = format!("{dir}/off.{ext}"); + s.execute("file.export.saveForWebLegacy", json!({"format": format, "embedIcc": false, "path": off})).unwrap(); + assert_eq!(profile(&off), None, "{format}"); + } +} diff --git a/crates/engine/src/wia_cmds.rs b/crates/engine/src/cmd/wia_cmds.rs similarity index 100% rename from crates/engine/src/wia_cmds.rs rename to crates/engine/src/cmd/wia_cmds.rs diff --git a/crates/engine/src/command_hints.rs b/crates/engine/src/command_hints.rs new file mode 100644 index 0000000000..2cc210ea25 --- /dev/null +++ b/crates/engine/src/command_hints.rs @@ -0,0 +1,144 @@ +//! "Did you mean …?" hints for unknown command ids and protocol method names (#3674), so an agent +//! that guesses a name gets something to correct from instead of re-listing the whole registry. +//! Only computed on the error path; the registry is small enough to scan. + +use crate::commands; + +/// Names that are documented elsewhere and can't be found by spelling alone: a protocol method +/// mistaken for the engine command that does the same thing. +const ALIASES: &[(&str, &str)] = &[("engine.commands", "command.list"), ("document.undo", "edit.undo"), ("document.redo", "edit.redo")]; + +/// At most this many suggestions per error. +const MAX_SUGGESTIONS: usize = 3; + +/// Up to [`MAX_SUGGESTIONS`] `candidates` close to `name`, best first: a different case or a few +/// typos (edit distance), then names that `name` is the start of (`filter.blur.gaussian`), the +/// same last segment in another namespace (`document.undo` → `edit.undo`), and the same namespace +/// with a longer last segment (`image.size` → `image.imageSize`). Deterministic: within a tier +/// the closest spelling wins, then the name. +pub fn near_matches<'a>(name: &str, candidates: impl IntoIterator) -> Vec<&'a str> { + let lower = name.to_lowercase(); + let max = (lower.chars().count() / 4).clamp(1, 4); + let dotted = lower.split_once('.'); + let tail = |s: &str| s.rsplit('.').next().map(str::to_owned).unwrap_or_default(); + let name_tail = tail(&lower); + let long_tail = name_tail.chars().count() >= 3; + let mut scored: Vec<(usize, usize, &'a str)> = candidates + .into_iter() + .filter(|c| *c != name) + .filter_map(|c| { + let cl = c.to_lowercase(); + if let Some(d) = photocraft_text::spell::edit_distance(&lower, &cl, max) { + return Some((0, d, c)); + } + let (namespace, _) = dotted?; + let cl_tail = tail(&cl); + let tier = if cl.starts_with(&lower) { + 1 + } else if long_tail && cl_tail == name_tail { + 2 + } else if long_tail && cl.split_once('.').is_some_and(|(ns, _)| ns == namespace) && cl_tail.contains(&name_tail) { + 3 + } else { + return None; + }; + Some((tier, photocraft_text::spell::edit_distance(&lower, &cl, 64).unwrap_or(usize::MAX), c)) + }) + .collect(); + scored.sort_unstable(); + scored.dedup(); + scored.into_iter().take(MAX_SUGGESTIONS).map(|(.., c)| c).collect() +} + +/// `"; did you mean `a`?"` / `"; did you mean `a`, `b` or `c`?"`, or `""` for no suggestions. +pub fn did_you_mean(names: &[&str]) -> String { + let quoted: Vec = names.iter().map(|n| format!("`{n}`")).collect(); + match quoted.split_last() { + None => String::new(), + Some((last, [])) => format!("; did you mean {last}?"), + Some((last, rest)) => format!("; did you mean {} or {last}?", rest.join(", ")), + } +} + +/// The hint appended to "unknown command `id`": registered ids close to `id`, or `""`. +pub fn unknown_command_hint(id: &str) -> String { + let mut names: Vec<&str> = ALIASES.iter().filter(|(from, _)| *from == id).map(|(_, to)| *to).collect(); + for n in near_matches(id, commands::command_specs().iter().map(|s| s.id)) { + if names.len() < MAX_SUGGESTIONS && !names.contains(&n) { + names.push(n); + } + } + did_you_mean(&names) +} + +/// The error for `engine.execute` given `id`, a control-protocol method rather than an engine +/// command (shared by the headless server and the desktop control server). +pub fn protocol_method_as_command(id: &str) -> String { + format!("{}; `{id}` is a protocol method: send it as the request's method, not through engine.execute", crate::EngineError::UnknownCommand(id.into())) +} + +/// The error for an unknown protocol request `method`, given the server's `methods`: an engine +/// command id points at `engine.execute`, anything else gets the nearest method names. +pub fn unknown_method(method: &str, methods: &[&str]) -> String { + if commands::find(method).is_some() { + return format!("unknown method `{method}`; `{method}` is an engine command: run it with engine.execute {{\"command\": \"{method}\"}}"); + } + format!("unknown method `{method}`{}", did_you_mean(&near_matches(method, methods.iter().copied()))) +} + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::*; + use crate::{EngineError, Session}; + + fn unknown(id: &str) -> String { + EngineError::UnknownCommand(id.into()).to_string() + } + + #[test] + fn protocol_listing_method_points_at_command_list() { + assert_eq!(unknown("engine.commands"), "unknown command `engine.commands`; did you mean `command.list`?"); + let mut s = Session::new(); + let e = s.execute("engine.commands", json!({})).unwrap_err().to_string(); + assert!(e.contains("did you mean `command.list`"), "{e}"); + assert!(s.disabled_reason("engine.commands").unwrap().contains("`command.list`")); + } + + #[test] + fn near_misses_suggest_registered_ids() { + // Typo, case, and the same verb in another namespace. + assert!(unknown("edit.undoo").contains("did you mean `edit.undo`"), "{}", unknown("edit.undoo")); + assert!(unknown("Edit.Undo").contains("did you mean `edit.undo`"), "{}", unknown("Edit.Undo")); + let e = unknown("document.undo"); + assert!(e.contains("did you mean `edit.undo`"), "{e}"); + assert!(unknown("document.redo").contains("did you mean `edit.redo`")); + // A namespace prefix, a truncated id, a shorter last segment. + assert!(unknown("layer.new").contains("`layer.new.layer`"), "{}", unknown("layer.new")); + assert!(unknown("filter.blur.gaussian").contains("did you mean `filter.blur.gaussianBlur`")); + assert!(unknown("image.size").contains("did you mean `image.imageSize`"), "{}", unknown("image.size")); + assert!(e.len() < 200, "a few suggestions, not a listing: {e}"); + // Nothing close: the plain error. + assert_eq!(unknown("zzzz.qqqq"), "unknown command `zzzz.qqqq`"); + assert_eq!(unknown(""), "unknown command ``"); + } + + #[test] + fn suggestions_are_capped_and_ordered() { + let c = ["a.bc", "a.bd", "a.be", "a.bf", "x.y"]; + assert_eq!(near_matches("a.bb", c), ["a.bc", "a.bd", "a.be"]); + assert_eq!(did_you_mean(&[]), ""); + assert_eq!(did_you_mean(&["a"]), "; did you mean `a`?"); + assert_eq!(did_you_mean(&["a", "b", "c"]), "; did you mean `a`, `b` or `c`?"); + } + + #[test] + fn protocol_method_hints() { + let methods = ["engine.execute", "doc.inspect", "doc.open", "methods"]; + assert_eq!(unknown_method("doc.inspct", &methods), "unknown method `doc.inspct`; did you mean `doc.inspect`?"); + assert!(unknown_method("edit.undo", &methods).contains("engine.execute {\"command\": \"edit.undo\"}")); + assert_eq!(unknown_method("zzzz", &methods), "unknown method `zzzz`"); + assert!(protocol_method_as_command("doc.inspect").contains("`doc.inspect` is a protocol method")); + } +} diff --git a/crates/engine/src/crop_fill.rs b/crates/engine/src/crop_fill.rs index 6ef0698cea..e67ff25844 100644 --- a/crates/engine/src/crop_fill.rs +++ b/crates/engine/src/crop_fill.rs @@ -8,9 +8,15 @@ //! layers keep their transparency there, as after a plain crop. Each connected piece of the //! uncovered area (each corner, each edge strip) is filled on its own, with Photoshop's sampling //! window around that piece, and only pixels the image covers are sampled. +//! +//! Pixels an earlier crop with Delete Cropped Pixels off hid past the canvas edges are still the +//! layer's. Under a turned frame they count as covered, so they are kept (and sampled) rather than +//! filled over; a straight crop fills all of the area past the old canvas, them included, as +//! Photoshop does (#8060). use photocraft_doc::{Document, LayerId}; use photocraft_geom::Rect; +use photocraft_raster::Surface; use rayon::prelude::*; use crate::{EngineError, Result}; @@ -22,7 +28,7 @@ const SEED: u64 = 1; const MAX_PIXELS: u64 = 400_000_000; /// Where the cropped canvas came from in the original document. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug)] pub(crate) struct Coverage { /// Cropped canvas → original document: `[a, b, c, d, e, f]` with /// `x = a·u + b·v + e`, `y = c·u + d·v + f`. @@ -32,12 +38,23 @@ pub(crate) struct Coverage { pub height: f64, /// Whether the map rotates (its resampled edge is antialiased against the background colour). pub rotated: bool, + /// The filled layer's pixels before a turned crop (original document px), set by + /// [`Coverage::holding`]: where they lie past the old canvas they are hidden image, not + /// uncovered area. A straight crop has none: Photoshop fills over hidden pixels there. + pub held: Option, } impl Coverage { /// An unrotated crop to `r` (original document px). pub fn crop(r: Rect, width: u32, height: u32) -> Self { - Coverage { map: [1.0, 0.0, 0.0, 1.0, f64::from(r.x0), f64::from(r.y0)], width: f64::from(width), height: f64::from(height), rotated: false } + Coverage { map: [1.0, 0.0, 0.0, 1.0, f64::from(r.x0), f64::from(r.y0)], width: f64::from(width), height: f64::from(height), rotated: false, held: None } + } + + /// Takes the pixels of the layer the fill will go to from `doc` before a turned crop (the + /// surface is shared, not copied), so hidden pixels past the old canvas count as covered. + pub fn holding(mut self, doc: &Document, active: Option) -> Self { + self.held = target_layer(doc, active).ok().and_then(|id| doc.layer(id)).and_then(|l| l.surface()).cloned(); + self } /// Whether the cropped canvas's pixel (u, v) is the original image's: its centre maps inside @@ -48,7 +65,27 @@ impl Coverage { let [a, b, c, d, e, f] = self.map; let (cu, cv) = (f64::from(u) + 0.5, f64::from(v) + 0.5); let (x, y) = (a * cu + b * cv + e, c * cu + d * cv + f); - x >= margin && y >= margin && x <= self.width - margin && y <= self.height - margin + (x >= margin && y >= margin && x <= self.width - margin && y <= self.height - margin) || self.holds(x, y) + } + + /// Whether the layer held an image pixel at original document point (x, y) past the canvas + /// (#8060): mostly opaque there and one pixel around it too (the turned edge is blended with + /// the background colour, as at the canvas edge). Without an alpha channel + /// hidden pixels can't be told from empty ones: nothing is held. + fn holds(&self, x: f64, y: f64) -> bool { + let Some(surf) = &self.held else { return false }; + let format = surf.format(); + if !format.alpha || !(x.is_finite() && y.is_finite()) { + return false; + } + let a = surf.channels().saturating_sub(1); + let at = |x: f64, y: f64| { + let (fx, fy) = (x.floor(), y.floor()); + let limit = f64::from(i32::MAX); + fx.abs() < limit && fy.abs() < limit && surf.sample_channel(fx as i32, fy as i32, a) > 0.5 + }; + const ROUND: [(f64, f64); 5] = [(0.0, 0.0), (-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0)]; + ROUND.iter().all(|&(dx, dy)| at(x + dx, y + dy)) } fn margin(&self) -> f64 { diff --git a/crates/engine/src/display_color.rs b/crates/engine/src/display_color.rs index 1e4aafc909..55a9c83ed9 100644 --- a/crates/engine/src/display_color.rs +++ b/crates/engine/src/display_color.rs @@ -1,5 +1,5 @@ //! Colour-managed canvas display: the document's composite shown through document profile → -//! monitor profile, on the GPU canvas (folded into the display 3D LUT) and the CPU canvas +//! monitor profile, on the GPU canvas (matrix/shaper or display 3D LUT) and the CPU canvas //! (an 8-bit transform of the composite). //! //! * The composite is in [`composite_profile`] (the document profile for RGB, its gray curve as @@ -464,5 +464,4 @@ impl ColorState { } #[cfg(test)] -#[path = "display_color_tests.rs"] mod tests; diff --git a/crates/engine/src/display_color_tests.rs b/crates/engine/src/display_color/tests.rs similarity index 100% rename from crates/engine/src/display_color_tests.rs rename to crates/engine/src/display_color/tests.rs diff --git a/crates/engine/src/doc_path.rs b/crates/engine/src/doc_path.rs new file mode 100644 index 0000000000..b51d164e74 --- /dev/null +++ b/crates/engine/src/doc_path.rs @@ -0,0 +1,58 @@ +//! Paths stored in a document reach the file system only through [`resolve`] (built on +//! [`photocraft_io::doc_path::resolve`]): linked smart objects, the Displace map and Color Lookup +//! file a smart filter re-reads, data-set images and video footage. A refused path is handled like +//! a missing file: the layer keeps its last rendering, and commands that need the file say why. +//! +//! Smart-object rendering has no `Session` argument, so the path of the document being worked on +//! travels in a thread-local scope, entered where commands run ([`crate::Session::edit`], command +//! dispatch, background jobs). Outside a scope there is no document: relative paths are refused +//! and only local absolute paths are followed. + +use std::cell::RefCell; +use std::path::{Path, PathBuf}; + +pub use photocraft_io::doc_path::NotFollowed; + +thread_local! { + static DOCUMENT: RefCell> = const { RefCell::new(None) }; +} + +/// Restores the previous document path when dropped. +pub(crate) struct DocumentScope(Option); + +impl Drop for DocumentScope { + fn drop(&mut self) { + let previous = self.0.take(); + DOCUMENT.with(|d| *d.borrow_mut() = previous); + } +} + +/// Resolve stored paths against `document` (the path of the document being worked on, `None` +/// for one without a file) until the returned scope is dropped. +pub(crate) fn scope(document: Option<&Path>) -> DocumentScope { + let next = document.filter(|d| !d.as_os_str().is_empty()).map(Path::to_path_buf); + DocumentScope(DOCUMENT.with(|d| d.replace(next))) +} + +/// The document path of the current scope (to carry it to another thread). +pub(crate) fn current() -> Option { + DOCUMENT.with(|d| d.borrow().clone()) +} + +/// The file to open for `stored`, a path stored in the document of the current scope. +pub fn resolve(stored: &str) -> Result { + resolve_in(stored, current().as_deref()) +} + +/// The file to open for `stored`, a path stored in the document saved at `document`. +pub fn resolve_in(stored: &str, document: Option<&Path>) -> Result { + photocraft_io::doc_path::resolve(stored, document) +} + +/// The message for a stored path that is not followed. +pub(crate) fn refusal(what: &str, stored: &str, why: NotFollowed) -> crate::EngineError { + crate::EngineError::Other(format!("{what} {stored} is not opened: {why}")) +} + +#[cfg(test)] +mod tests; diff --git a/crates/engine/src/doc_path/tests.rs b/crates/engine/src/doc_path/tests.rs new file mode 100644 index 0000000000..1c9bc88874 --- /dev/null +++ b/crates/engine/src/doc_path/tests.rs @@ -0,0 +1,350 @@ +//! Each kind of place that follows a path stored in a document goes through [`super::resolve`]: a +//! link to another computer is never followed, and a relative link next to the document is. + +use std::path::{Path, PathBuf}; + +use photocraft_doc::{LayerContent, LayerId, SmartSource}; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::Session; + +/// A link to a share on a TEST-NET-1 address, which never answers. +const REMOTE: &str = r"\\192.0.2.1\share\x.png"; + +/// An empty folder of its own (tests run in parallel). +fn dir(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("photocraft-doc-path-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d +} + +/// A 32×32 PNG whose pixel at column `x` is `rgb(x)`. +fn png(path: &Path, rgb: impl Fn(u32) -> [u8; 3]) { + let mut px = Vec::new(); + for _ in 0..32 { + for x in 0..32 { + px.extend(rgb(x)); + } + } + let image = photocraft_codecs::Image::from_u8(32, 32, photocraft_codecs::ChannelLayout::Rgb, px).unwrap(); + std::fs::write(path, photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); +} + +/// On Unix `//` followed by an absolute path names that same local file, while the resolver sees a +/// path on another computer: a place that skipped the resolver would read the file. +#[cfg(unix)] +fn network_alias(p: &Path) -> String { + format!("/{}", p.display()) +} + +/// A 32×32 white document saved (as far as its links know) at `d/doc.pcraft`, with `file` placed +/// linked → (session, layer id). +fn document(d: &Path, file: &Path) -> (Session, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 32, "height": 32})).unwrap(); + s.active_mut().unwrap().path = Some(d.join("doc.pcraft").to_string_lossy().into_owned()); + s.execute("file.placeLinked", json!({"path": file.to_string_lossy()})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + (s, id) +} + +/// Point smart object `id` at `path` without re-rendering it. +fn relink(s: &mut Session, id: LayerId, path: &str) { + s.edit("Relink", |doc, _| { + if let Some(LayerContent::Smart(sm)) = doc.layer_mut(id).map(|l| &mut l.content) { + sm.source = SmartSource::Linked { path: path.into() }; + } + Ok(()) + }) + .unwrap(); +} + +fn centre(s: &Session) -> [f32; 3] { + let p = photocraft_compose::render(&s.active().unwrap().doc, Rect::new(16, 16, 17, 17)).px[0]; + [p[0], p[1], p[2]] +} + +const RED: [f32; 3] = [1.0, 0.0, 0.0]; +const BLUE: [f32; 3] = [0.0, 0.0, 1.0]; + +fn update(s: &mut Session) -> Result { + s.execute("layer.smartObjects.updateModifiedContent", json!({})).map_err(|e| e.to_string()) +} + +#[test] +fn linked_smart_objects_follow_only_local_files() { + let d = dir("links"); + png(&d.join("red.png"), |_| [255, 0, 0]); + png(&d.join("blue.png"), |_| [0, 0, 255]); + let (mut s, id) = document(&d, &d.join("red.png")); + assert_eq!(centre(&s), RED); + + relink(&mut s, id, REMOTE); + s.start_link_scan(); + assert!(s.link_scan.is_none(), "no look at a file on another computer"); + assert_eq!(s.execute("layer.smartObjects.updateChanged", json!({})).unwrap(), json!({"updated": []})); + let err = update(&mut s).unwrap_err(); + assert!(err.contains("is not opened: the path is on another computer"), "{err}"); + assert_eq!(centre(&s), RED, "the layer keeps its rendering"); + #[cfg(unix)] + { + relink(&mut s, id, &network_alias(&d.join("blue.png"))); + assert!(update(&mut s).is_err()); + assert_eq!(centre(&s), RED, "the file was not read"); + } + + // A relative link is taken from the document's folder. + relink(&mut s, id, "blue.png"); + update(&mut s).unwrap(); + assert_eq!(centre(&s), BLUE); + s.start_link_scan(); + assert!(s.link_scan.is_some(), "a followed link is looked at"); +} + +#[test] +fn package_copies_only_followed_links() { + let d = dir("package"); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, id) = document(&d, &d.join("red.png")); + relink(&mut s, id, "red.png"); + #[allow(unused_mut)] + let mut remote = vec![REMOTE.to_string()]; + #[cfg(unix)] + remote.push(network_alias(&d.join("red.png"))); + for path in &remote { + s.execute("file.placeLinked", json!({"path": d.join("red.png").to_string_lossy()})).unwrap(); + let other = s.active().unwrap().active_layer.unwrap(); + relink(&mut s, other, path); + } + let r = s.execute("file.package", json!({"dir": d.join("pkg").to_string_lossy()})).unwrap(); + let mut missing: Vec = serde_json::from_value(r["missing"].clone()).unwrap(); + missing.sort(); + remote.sort(); + assert_eq!(missing, remote); + let links = r["links"].as_array().unwrap(); + assert_eq!(links.len(), 1); + assert!(links[0].as_str().unwrap().ends_with("red.png"), "{links:?}"); +} + +#[test] +fn psd_saves_embed_only_followed_links() { + let d = dir("psd"); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, id) = document(&d, &d.join("red.png")); + relink(&mut s, id, "red.png"); + s.execute("file.placeLinked", json!({"path": d.join("red.png").to_string_lossy()})).unwrap(); + let other = s.active().unwrap().active_layer.unwrap(); + relink(&mut s, other, REMOTE); + let warnings = |r: serde_json::Value| r["warnings"].as_array().unwrap().iter().map(|w| w.as_str().unwrap().to_string()).collect::>(); + let saved = warnings(s.execute("file.saveACopy", json!({"path": d.join("copy.psd").to_string_lossy()})).unwrap()); + assert!(saved.iter().any(|w| w.contains("red.png was embedded")), "{saved:?}"); + assert!(saved.iter().any(|w| w.contains("is not opened: the path is on another computer")), "{saved:?}"); + // Package as PSD writes the copy of the document, whose remote link stays as it was. + let packaged = warnings(s.execute("file.package", json!({"dir": d.join("pkg").to_string_lossy(), "format": "psd"})).unwrap()); + assert!(packaged.iter().any(|w| w.contains("is not opened: the path is on another computer")), "{packaged:?}"); +} + +/// The pixels of the active document after its smart object `id` re-renders with its first smart +/// filter's params set to `params`. +fn replayed(s: &mut Session, id: LayerId, params: serde_json::Value) -> Vec<[f32; 4]> { + s.edit("Params", |doc, _| { + if let Some(LayerContent::Smart(sm)) = doc.layer_mut(id).map(|l| &mut l.content) { + sm.smart_filters[0].params = params; + } + crate::smart_cmds::refresh(doc, id).map(|_| ()) + }) + .unwrap(); + let doc = &s.active().unwrap().doc; + photocraft_compose::render(doc, doc.bounds()).px +} + +#[test] +fn smart_filters_reread_only_followed_files() { + let d = dir("filters"); + png(&d.join("ramp.png"), |x| [(x * 8) as u8, 0, 0]); + png(&d.join("map.png"), |_| [255, 255, 255]); + // An inverting look: corner (r, g, b) of the cube maps to (1-r, 1-g, 1-b), red fastest. + let corners = (0..8).map(|i| format!("{} {} {}\n", 1 - (i & 1), 1 - ((i >> 1) & 1), 1 - ((i >> 2) & 1))).collect::(); + std::fs::write(d.join("invert.cube"), format!("LUT_3D_SIZE 2\n{corners}")).unwrap(); + let path = |name: &str| d.join(name).to_string_lossy().into_owned(); + + // Displace re-reads its `mapPath`. + let (mut s, id) = document(&d, &d.join("ramp.png")); + s.execute("filter.distort.displace", json!({"layer": id.0, "mapPath": path("map.png"), "horizontal": 50, "vertical": 0})).unwrap(); + let displaced = replayed(&mut s, id, json!({"mapPath": path("map.png"), "horizontal": 50, "vertical": 0})); + let unfiltered = replayed(&mut s, id, json!({"mapPath": REMOTE, "horizontal": 50, "vertical": 0})); + assert_ne!(displaced, unfiltered, "the map moves the pixels"); + assert_eq!(replayed(&mut s, id, json!({"mapPath": "map.png", "horizontal": 50, "vertical": 0})), displaced, "a relative map next to the document"); + #[cfg(unix)] + assert_eq!(replayed(&mut s, id, json!({"mapPath": network_alias(&d.join("map.png")), "horizontal": 50, "vertical": 0})), unfiltered); + + // Color Lookup re-reads its LUT `file`. + let (mut s, id) = document(&d, &d.join("ramp.png")); + s.execute("image.adjustments.colorLookup", json!({"layer": id.0, "lut": "none"})).unwrap(); + let inverted = replayed(&mut s, id, json!({"file": path("invert.cube")})); + let unfiltered = replayed(&mut s, id, json!({"file": REMOTE})); + assert_ne!(inverted, unfiltered, "the look changes the pixels"); + assert_eq!(replayed(&mut s, id, json!({"file": "invert.cube"})), inverted, "a relative look next to the document"); + #[cfg(unix)] + assert_eq!(replayed(&mut s, id, json!({"file": network_alias(&d.join("invert.cube"))})), unfiltered); +} + +/// #7423 (review on #7799): Displace on a Smart Object keeps a `mapPath` as a path, as before +/// (only a map from an open document is embedded). A path relative to the saved document, set +/// through the filter's own edit, displaces as the absolute one did, and the dialog's "None" +/// (`mapDocument: -1`) names no map instead of failing. +#[test] +fn smart_displace_keeps_a_relative_map_path() { + let d = dir("displace-relative"); + png(&d.join("ramp.png"), |x| [(x * 8) as u8, 0, 0]); + png(&d.join("map.png"), |_| [255, 255, 255]); + let (mut s, id) = document(&d, &d.join("ramp.png")); + let path = d.join("map.png").to_string_lossy().into_owned(); + let unfiltered = photocraft_compose::render(&s.active().unwrap().doc, s.active().unwrap().doc.bounds()).px; + s.execute("filter.distort.displace", json!({"layer": id.0, "mapPath": path, "horizontal": 50, "vertical": 0})).unwrap(); + let state = |s: &Session| { + let doc = &s.active().unwrap().doc; + let Some(LayerContent::Smart(sm)) = doc.layer(id).map(|l| &l.content) else { panic!("a smart object") }; + (sm.smart_filters[0].params.clone(), photocraft_compose::render(doc, doc.bounds()).px) + }; + let (params, displaced) = state(&s); + assert_eq!(params["mapPath"], json!(path)); + assert!(photocraft_io::smart_map::displace_embedded_map(¶ms).is_none(), "a path, not an embedded map"); + assert_ne!(displaced, unfiltered, "the map moves the pixels"); + s.execute("layer.smartFilter.setParams", json!({"layer": id.0, "index": 0, "params": {"mapPath": "map.png"}})).unwrap(); + let (params, relative) = state(&s); + assert_eq!(params["mapPath"], json!("map.png")); + assert_eq!(relative, displaced, "a relative map next to the document"); + s.execute("layer.smartFilter.setParams", json!({"layer": id.0, "index": 0, "params": {"mapDocument": -1}})).unwrap(); + assert_eq!(state(&s).1, displaced, "\"None\" changes nothing"); +} + +#[test] +fn data_sets_and_footage_read_only_followed_files() { + let d = dir("datasets"); + png(&d.join("blue.png"), |_| [0, 0, 255]); + let mut s = Session::new(); + s.execute("file.new", json!({"width": 32, "height": 32, "background": "#ff0000"})).unwrap(); + s.active_mut().unwrap().path = Some(d.join("doc.pcraft").to_string_lossy().into_owned()); + let layer = s.active().unwrap().active_layer.unwrap(); + s.execute("image.variables.define", json!({"defs": [{"name": "img", "layer": layer.0, "type": "pixelReplacement", "method": "conform"}]})).unwrap(); + let set = |name: &str, path: &str| json!({"name": name, "values": [{"variable": "img", "kind": "pixels", "value": path}]}); + s.execute("image.variables.dataSets", json!({"dataSets": [set("remote", REMOTE), set("relative", "blue.png")]})).unwrap(); + let err = s.execute("image.applyDataSet", json!({"name": "remote"})).unwrap_err().to_string(); + assert!(err.contains("is not opened: the path is on another computer"), "{err}"); + assert_eq!(centre(&s), RED); + s.execute("image.applyDataSet", json!({"name": "relative"})).unwrap(); + assert_eq!(centre(&s), BLUE); + + // Reload Frame re-reads the video layer's footage. + s.execute("layer.videoLayers.newVideoLayerFromFile", json!({"path": d.join("blue.png").to_string_lossy()})).unwrap(); + let footage = |s: &mut Session, path: &str| { + let id = s.active().unwrap().active_layer.unwrap(); + s.edit("Footage", |doc, _| { + if let Some(v) = doc.layer_mut(id).and_then(|l| l.video.as_mut()) { + v.source = photocraft_doc::VideoSource::File { path: path.into() }; + } + Ok(()) + }) + .unwrap(); + s.execute("layer.videoLayers.reloadFrame", json!({})).map_err(|e| e.to_string()) + }; + let err = footage(&mut s, REMOTE).unwrap_err(); + assert!(err.contains("is not opened: the path is on another computer"), "{err}"); + footage(&mut s, "blue.png").unwrap(); +} + +/// A link to a file PhotoCraft can't place (a text or settings file) is never embedded: a saved +/// PSD would otherwise carry bytes no reader can show as the layer's contents. The layer is +/// written as pixels; Convert to Embedded and Relink refuse. +#[test] +fn links_to_files_that_are_not_images_are_never_embedded() { + const TEXT: &[u8] = b"plain text, not an image +photocraft-text-marker +"; + let d = dir("not-image"); + std::fs::create_dir_all(d.join("notes")).unwrap(); + std::fs::create_dir_all(d.join("Downloads")).unwrap(); + std::fs::write(d.join("notes").join("readme"), TEXT).unwrap(); + std::fs::write(d.join("Downloads").join(".settings"), TEXT).unwrap(); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, id) = document(&d, &d.join("red.png")); + s.active_mut().unwrap().path = Some(d.join("Downloads").join("doc.pcraft").to_string_lossy().into_owned()); + let has_text = |bytes: &[u8]| bytes.windows(TEXT.len()).any(|w| w == TEXT); + for (n, path) in ["../notes/readme", ".settings"].into_iter().enumerate() { + relink(&mut s, id, path); + for ext in ["psd", "psb", "tif"] { + let out = d.join(format!("copy{n}.{ext}")); + let r = s.execute("file.saveACopy", json!({"path": out.to_string_lossy(), "tiffLayers": true})).unwrap(); + assert!(!has_text(&std::fs::read(&out).unwrap()), "{path} embedded in the {ext}: {r}"); + let warnings = r["warnings"].to_string(); + assert!(warnings.contains("written as pixels"), "{path} {ext}: {warnings}"); + } + assert!(s.execute("layer.smartObjects.convertToEmbedded", json!({"layer": id.0})).is_err(), "{path}"); + let abs = d.join("notes").join("readme"); + assert!(s.execute("layer.smartObjects.relinkToFile", json!({"layer": id.0, "path": abs.to_string_lossy()})).is_err(), "{path}"); + } +} + +/// Duplicates share their contents identity, so a linked and an embedded copy of the same bytes +/// meet in the export's conversion cache: each keeps its own rule, whichever is written first. +#[test] +fn a_linked_and_an_embedded_copy_of_the_same_bytes_keep_their_own_rule() { + const TEXT: &[u8] = b"plain text, not an image +photocraft-shared-marker +"; + let d = dir("shared-contents"); + std::fs::write(d.join("readme"), TEXT).unwrap(); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, linked) = document(&d, &d.join("red.png")); + s.execute("layer.duplicate", json!({})).unwrap(); + let embedded = s.active().unwrap().active_layer.unwrap(); + assert_ne!(linked, embedded); + relink(&mut s, linked, "readme"); + s.edit("Embed", |doc, _| { + if let Some(LayerContent::Smart(sm)) = doc.layer_mut(embedded).map(|l| &mut l.content) { + sm.source = SmartSource::Embedded { file_name: "readme".into(), bytes: std::sync::Arc::new(TEXT.to_vec()) }; + } + Ok(()) + }) + .unwrap(); + for ext in ["psd", "psb", "tif"] { + let out = d.join(format!("copy.{ext}")); + let r = s.execute("file.saveACopy", json!({"path": out.to_string_lossy(), "tiffLayers": true})).unwrap(); + let bytes = std::fs::read(&out).unwrap(); + let copies = bytes.windows(TEXT.len()).filter(|w| *w == TEXT).count(); + assert_eq!(copies, 1, "{ext}: only the embedded copy is kept: {r}"); + assert!(r["warnings"].to_string().contains("written as pixels"), "{ext}: {r}"); + } +} + +/// Package copies only linked files PhotoCraft can open: a link to a text file is listed as +/// missing and no copy of it is written into the package's Links folder. +#[test] +fn package_copies_only_links_to_images() { + const TEXT: &[u8] = b"plain text, not an image +photocraft-package-marker +"; + let d = dir("package-not-image"); + std::fs::write(d.join("readme"), TEXT).unwrap(); + png(&d.join("red.png"), |_| [255, 0, 0]); + let (mut s, id) = document(&d, &d.join("red.png")); + relink(&mut s, id, "readme"); + let pkg = d.join("pkg"); + let r = s.execute("file.package", json!({"dir": pkg.to_string_lossy()})); + let copies: Vec<_> = walkdir(&pkg).into_iter().filter(|f| std::fs::read(f).is_ok_and(|b| b == TEXT)).collect(); + assert!(copies.is_empty(), "copied into the package: {copies:?} ({r:?})"); + let r = r.unwrap(); + assert_eq!(r["missing"], json!(["readme"]), "{r}"); +} + +fn walkdir(d: &Path) -> Vec { + let mut out = Vec::new(); + for e in std::fs::read_dir(d).into_iter().flatten().flatten() { + let p = e.path(); + if p.is_dir() { out.extend(walkdir(&p)) } else { out.push(p) } + } + out +} diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs new file mode 100644 index 0000000000..afdf86f4fa --- /dev/null +++ b/crates/engine/src/doc_state.rs @@ -0,0 +1,276 @@ +use super::{Arc, Document, History, LayerId, LayerTarget, channel_cmds, float_cmds, history_cmds, layer_multi_cmds, symmetry_cmds}; + +/// Per-document editing state. +#[derive(Clone, Debug)] +pub struct DocState { + pub doc: Arc, + pub history: History, + /// The primary ("key") layer: what single-layer commands act on. Always a member of + /// `selected_layers` when set. + pub active_layer: Option, + /// Every selected layer in the Layers panel (⌘/⇧-click), in selection order. Like + /// `active_layer`, selecting is not a history step, but undo and redo restore the layers each + /// state targeted when the next step was taken; see [`DocState::selected_layers`]. + pub selected_layers: Vec, + /// Anchor of ⇧-click range selection (the last plainly or ⌘-clicked layer). + pub layer_anchor: Option, + pub path: Option, + /// An Affinity document (or its preview) must not acquire the native source as its Save path. + pub source_read_only: bool, + /// Increments on every change; UIs re-render when it moves. + pub revision: u64, + pub saved_revision: u64, + /// Native save cache shared with this document's workers, released with the editing state. + pub pcraft_cache: photocraft_io::PcraftExportCache, + /// How much of `revision` came from view-only changes (channel eyes, targeting): pixel caches can ignore those. + pub view_revision: u64, + /// Area changed by the latest revision (None = assume everything changed). + pub last_damage: Option, + /// Coalescing key of the latest history step (see [`Session::execute`]'s `coalesce` param). + pub coalesce: Option, + /// Channels panel: targeted channel and eye toggles (view state, not history). + pub channel_view: channel_cmds::ChannelView, + /// Select › Isolate Layers: the Layers panel lists only these layers (empty = off; view state). + pub isolated_layers: Vec, + /// Painting symmetry preset or sampled path (tool state, not document pixels). + pub symmetry: Option, + /// Layers panel: layers whose effects list is collapsed under their row (the fx triangle; + /// view state, not history). Effects lists start open. + pub fx_collapsed: Vec, + /// ⌥-click on a layer's eye (`layer.showOnly`): the layer shown alone and every layer's + /// visibility before, so the next ⌥-click restores it (view state, not history). + pub show_only: Option<(LayerId, Vec<(LayerId, bool)>)>, + /// A floating selection (`select.float`): the cut piece and where it floats, until dropped + /// (view state: the document is unchanged until `select.drop`). + pub floating: Option, + /// Color Sampler points (`view.colorSamplers.*`, #1046): numbered sample positions in document + /// pixel coordinates. Per-document view state: discarded when the document closes, never saved + /// to `.pcraft` or PSD, and not a history step. + pub color_samplers: Vec<[f64; 2]>, + /// History panel snapshots, oldest first (`history.newSnapshot`, #2966): the first (id 0) is + /// the document as opened. Kept for the session only, as in Photoshop: not history states, + /// and not saved with the document. + pub snapshots: Vec, + /// The id the next snapshot gets; ids are never reused, so deleting one keeps the others. + pub(crate) next_snapshot: u64, + /// The snapshot set as the History Brush's source (`history.setSource {snapshot}`); `None`: + /// the source is a state, kept by the history ([`photocraft_ops::History::brush_source`]). + pub(crate) source_snapshot: Option, +} + +impl DocState { + /// Trim the history to its budget, counting the tiles only History panel snapshots hold + /// against it too (#4530). Returns how many states were dropped. + pub fn trim_history(&mut self) -> usize { + let snaps: Vec<&Document> = self.snapshots.iter().map(|s| &*s.doc).collect(); + self.history.trim_with(&self.doc, &snaps) + } + + /// Pixel bytes of the document, the tiles only its history holds and the tiles only its + /// History panel snapshots hold; embedded smart object contents count too. + pub fn memory_bytes(&self) -> usize { + let snaps: Vec<&Document> = self.snapshots.iter().map(|s| &*s.doc).collect(); + self.history.pixel_bytes(&self.doc).saturating_add(self.history.extra_bytes(&self.doc, &snaps)) + } + + pub fn new(doc: Document, path: Option) -> Self { + let active_layer = doc.top_layer(); + let mut history = History::default(); + let target = LayerTarget { active: active_layer, selected: active_layer.into_iter().collect() }; + history.set_current_layers(target.clone()); + let doc = Arc::new(doc); + // Photoshop's first snapshot: the document as opened, named after it, so the History + // panel's top row can always bring it back (#2966). + let first = history_cmds::Snapshot { id: 0, name: doc.name.clone(), doc: doc.clone(), layers: target }; + Self { + doc, + history, + active_layer, + selected_layers: active_layer.into_iter().collect(), + layer_anchor: active_layer, + path, + source_read_only: false, + revision: 1, + saved_revision: 1, + pcraft_cache: Default::default(), + view_revision: 0, + last_damage: None, + coalesce: None, + channel_view: Default::default(), + isolated_layers: Vec::new(), + symmetry: None, + fx_collapsed: Vec::new(), + show_only: None, + floating: None, + color_samplers: Vec::new(), + snapshots: vec![first], + next_snapshot: 1, + source_snapshot: None, + } + } + /// The selected layers in bottom-to-top document order, always including the active layer. + /// Robust against stale state: ids no longer in the document are skipped, and if the active + /// layer was changed without updating the set, the selection is just the active layer. + pub fn selected_layers(&self) -> Vec { + let Some(active) = self.active_layer else { return Vec::new() }; + let set: &[LayerId] = if self.selected_layers.contains(&active) { &self.selected_layers } else { std::slice::from_ref(&active) }; + self.doc.walk().into_iter().map(|(_, _, l)| l.id).filter(|id| set.contains(id)).collect() + } + pub fn is_layer_selected(&self, id: LayerId) -> bool { + self.active_layer == Some(id) || (self.active_layer.is_some_and(|a| self.selected_layers.contains(&a)) && self.selected_layers.contains(&id)) + } + pub fn is_dirty(&self) -> bool { + self.revision != self.saved_revision + } + /// Whether the step from `before` to the current document leaves a clean document clean: + /// it changed nothing but the selection (#6865). Making, changing or dropping a selection + /// (marquee, lasso, Magic Wand, Quick / Object Selection, Select › …) is a history step but + /// not an edit, so it neither marks the file modified nor makes closing ask to save. + pub(crate) fn keeps_clean(&self, before: &Document) -> bool { + self.saved_revision == self.revision && layer_multi_cmds::selection_only_change(before, &self.doc) + } + /// Move to the next revision; `keep_clean` (from [`Self::keeps_clean`]) moves the saved + /// revision with it. + pub(crate) fn bump_revision(&mut self, keep_clean: bool) { + self.revision += 1; + if keep_clean { + self.saved_revision = self.revision; + } + } + /// The targeted layers, as history stores them with each state. + pub(crate) fn layer_target(&self) -> LayerTarget { + LayerTarget { active: self.active_layer, selected: self.selected_layers.clone() } + } +} + +/// App-wide tool state that commands read (foreground colour, brush…). +#[derive(Clone, Debug)] +pub struct ToolState { + pub foreground: [f32; 4], + pub background: [f32; 4], + pub brush: photocraft_paint::BrushSettings, + /// Brush presets (built-ins plus user presets; see `brush.presets.*`). + pub presets: Vec, + /// Bumped whenever `presets` changes ([`Session::brush_presets_changed`]); the preset store + /// syncs when it moves. + pub presets_rev: u64, + /// Mixer Brush paint carried between strokes. + pub mixer: photocraft_paint::mixer::MixerState, + /// The coalescing key of the running `tools.setBrush` gesture and the brush before it, so the + /// gesture journals as one call ([`brush_cmds::coalesce_journal`]). + pub brush_gesture: Option<(String, photocraft_paint::BrushSettings)>, + /// Name of the preset the current brush was last picked from. Kept across later edits (the + /// preset stays "the current brush" so `brush.presets.update` can overwrite it); cleared when + /// the brush is reset or the preset is gone. + pub current_preset: Option, + /// Bumped whenever `brush` or `current_preset` changes ([`ToolState::brush_changed`]). The + /// last-brush persistence watches it instead of comparing the brush itself, so an unchanged + /// brush costs a counter compare per frame, not a clone and serialise. + pub brush_rev: u64, + /// The revision whose value is in `Preferences::last_brush` (a restored one counts as saved): + /// [`brush_rev`](Self::brush_rev) == `brush_saved_rev` means there is nothing new to write. + pub brush_saved_rev: u64, + /// A layer mask is the edit target ([`ToolState::target_mask`]). + pub mask_targeted: bool, + /// The foreground/background pair of the edit target that isn't current: the layer pixels' + /// colours while a mask is targeted, the mask's while the pixels are (#2166). + pub other_colors: [[f32; 4]; 2], + /// Colours picked as CMYK, newest last: the sRGB colour that stands for them and the ink + /// values chosen. The tool colours are stored as sRGB, and an sRGB round trip does not give + /// back what was typed (100/0/0/0 cyan is outside sRGB), so a CMYK document is filled with + /// the inks the person picked (#4196). Matched by colour, so it follows a swap, a reset or a + /// mask pair without bookkeeping. + pub cmyk_picks: Vec<([f32; 3], [f32; 4])>, +} + +impl ToolState { + /// How many CMYK picks are remembered. + const CMYK_PICKS: usize = 8; + + /// Remember that sRGB colour `rgb` was picked as the inks `cmyk` (each 0..1). + pub fn remember_cmyk(&mut self, rgb: [f32; 3], cmyk: [f32; 4]) { + self.cmyk_picks.retain(|(c, _)| *c != rgb); + self.cmyk_picks.push((rgb, cmyk)); + let extra = self.cmyk_picks.len().saturating_sub(Self::CMYK_PICKS); + self.cmyk_picks.drain(..extra); + } + + /// The inks `rgb` was picked as, if it was picked as CMYK. + pub fn picked_cmyk(&self, rgb: [f32; 3]) -> Option<[f32; 4]> { + self.cmyk_picks.iter().rev().find(|(c, _)| *c == rgb).map(|(_, k)| *k) + } + + /// Photoshop's colour pair for a layer mask that has none yet: white foreground, black + /// background (reveal-paint first). + pub const MASK_COLORS: [[f32; 4]; 2] = [[1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]; + + /// Note that `brush` or `current_preset` changed. Every setter of either (commands and the + /// shell's tool-switch paths) calls this; the last-brush persistence only serialises when the + /// revision has moved since the stored value ([`Self::brush_saved_rev`]). + pub fn brush_changed(&mut self) { + self.brush_rev = self.brush_rev.wrapping_add(1); + } + + /// Switch the edit target between layer pixels and a layer mask. Like Photoshop, the mask and + /// the pixels keep separate app-wide foreground/background pairs: changing the target puts + /// the current pair aside and brings back the other one, so colours picked while a mask is + /// targeted are remembered for masks (#2166). The mask pair starts at [`Self::MASK_COLORS`]. + /// Returns whether the target changed. + pub fn target_mask(&mut self, mask: bool) -> bool { + if self.mask_targeted == mask { + return false; + } + self.mask_targeted = mask; + let [fg, bg] = std::mem::replace(&mut self.other_colors, [self.foreground, self.background]); + self.foreground = fg; + self.background = bg; + true + } +} + +impl Default for ToolState { + fn default() -> Self { + Self { + foreground: [0.0, 0.0, 0.0, 1.0], + background: [1.0, 1.0, 1.0, 1.0], + // Photoshop's Brush tool starts at 10 % Smoothing. + brush: photocraft_paint::BrushSettings { + smoothing: photocraft_paint::brush::Smoothing { amount: 0.1, ..Default::default() }, + ..Default::default() + }, + presets: photocraft_paint::presets::builtin(), + presets_rev: 0, + mixer: Default::default(), + brush_gesture: None, + current_preset: None, + brush_rev: 0, + brush_saved_rev: 0, + mask_targeted: false, + other_colors: Self::MASK_COLORS, + cmyk_picks: Vec::new(), + } + } +} + +/// Layer › Layer Style › Copy Layer Style: the effects and the Blending Options, as Photoshop +/// copies them (Paste Layer Style puts them all on each target). +#[derive(Clone, Debug)] +pub struct CopiedStyle { + pub effects: photocraft_doc::Effects, + pub blend: photocraft_color::BlendMode, + pub fill: f32, + pub advanced: photocraft_doc::AdvancedBlending, + pub blend_if: photocraft_doc::BlendIf, + pub excluded_channels: u32, + /// The colour mode it was copied in: Blend If's channel ranges and the channel restrictions + /// only carry over to a document in the same mode. + pub mode: photocraft_color::ColorMode, +} + +/// The commands whose [`Session::precheck`] is complete: it accepts what `execute` does (Free +/// Transform applies its box before these, and only when they will run), but for the group +/// nesting limit ([`photocraft_doc::MAX_GROUP_DEPTH`]): grouping layers already that deep is +/// refused only as the edit is made. Layer via Copy and +/// Cut aren't: whether they have anything to copy (a selection over transparent pixels, a group +/// or fill layer) is known only when they run. +pub const PRECHECKED: &[&str] = &["layer.new.layer", "layer.new.group", "layer.new.groupFromLayers", "layer.new.layerFromBackground"]; diff --git a/crates/engine/src/edit_cmds.rs b/crates/engine/src/edit_cmds.rs deleted file mode 100644 index dcb89aa980..0000000000 --- a/crates/engine/src/edit_cmds.rs +++ /dev/null @@ -1,2317 +0,0 @@ -//! Everyday Photoshop commands: clipboard (Cut/Copy/Copy Merged/Paste/Paste in Place), Layer via -//! Copy/Cut, Merge Visible, Auto Tone/Contrast/Color, Toggle Last State and Transform Again. - -use photocraft_color::{ColorMode, PixelFormat}; -use photocraft_doc::adjust::LevelsChannel; -use photocraft_doc::{Adjustment, Document, Layer, LayerContent, LayerId}; -use photocraft_geom::Rect; -use photocraft_raster::Surface; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -/// The paste commands' `"target"` param (for `concat!` into their params docs). -macro_rules! paste_target { - () => { - r#""target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (not pixels: the luminosity pasted into that mask or channel)"# - }; -} -pub(crate) use paste_target; - -/// Pixels on the session clipboard, with where they came from (for Paste in Place). -#[derive(Clone, Debug)] -pub struct Clip { - pub surface: Surface, - pub bounds: Rect, - /// The copied layer itself, when Copy took a whole layer that Paste should recreate rather - /// than rasterize (see [`whole_layer`]): a scratch document in the source's colour holding it. - /// `surface` still holds its pixels, for Paste Into, masks, channels and other apps. - pub layers: Option>, - /// The source document's resolution (ppi): File › New from the clipboard takes it, as - /// Photoshop's Clipboard preset does. None for an image from another app. - pub resolution: Option, -} - -/// Photoshop's layer-based copy (CC 2018 on): with no selection, Copy takes the active layer -/// whole when it is a smart object (#2428), a type or shape layer, or a group (its whole subtree) -/// (#2670), so Paste recreates it instead of its pixels. Pixel layers still copy their pixels; -/// several selected layers are [`multi_copy_ids`]. -fn takes_whole(d: &crate::DocState, id: LayerId) -> bool { - d.doc.selection.is_none() - && d.selected_layers().len() <= 1 - && crate::layer_copy_cmds::layered(d.doc.mode) - && d.doc - .layer(id) - .is_some_and(|l| matches!(l.content, LayerContent::Smart(_) | LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Group(_))) -} - -/// A scratch document in the source's colour, to hold copied layers. -fn scratch_doc(doc: &Document) -> Document { - let mut copies = Document::new("", doc.size, doc.mode, doc.depth); - copies.icc_profile = doc.icc_profile.clone(); - copies.resolution_dpi = doc.resolution_dpi; - copies.patterns = doc.patterns.clone(); - copies -} - -/// Scope linked records to the copied layer tree: a PSD block can also hold files belonging -/// to unselected or hidden layers, which must not follow this copy into a native save. -fn copy_linked_sources(source: &Document, copies: &mut Document) -> Result<()> { - fn collect<'a>(layers: &'a [Layer], depth: usize, paths: &mut std::collections::HashSet<&'a str>) -> Result<()> { - if depth > photocraft_doc::MAX_GROUP_DEPTH { - return Err(EngineError::Other("can't copy linked smart objects from layers nested too deeply".into())); - } - for layer in layers { - if let LayerContent::Smart(sm) = &layer.content - && let photocraft_doc::SmartSource::Linked { path } = &sm.source - { - paths.try_reserve(1).map_err(|_| EngineError::Other("not enough memory to copy smart-object source IDs".into()))?; - paths.insert(path); - } - if let Some(children) = layer.children().filter(|children| !children.is_empty()) { - collect(children, depth + 1, paths)?; - } - } - Ok(()) - } - let mut paths = std::collections::HashSet::new(); - collect(&copies.layers, 0, &mut paths)?; - if paths.is_empty() { - return Ok(()); - } - for (signature, key, data) in &source.metadata.psd_global_blocks { - if !matches!(key, b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE") { - continue; - } - let selected = photocraft_io::linked::select_items(data, &|uuid, _| paths.contains(uuid)) - .map_err(|e| EngineError::Other(format!("Could not copy smart-object contents: {e}; the clipboard was not changed")))?; - let selected = selected.map(std::sync::Arc::new).unwrap_or_else(|| data.clone()); - if !selected.is_empty() { - copies.metadata.psd_global_blocks.try_reserve(1).map_err(|_| EngineError::Other("not enough memory to copy smart-object source records".into()))?; - copies.metadata.psd_global_blocks.push((*signature, *key, selected)); - } - } - Ok(()) -} - -/// The layer Copy takes whole ([`takes_whole`]): a scratch document in the source's colour -/// holding it. -fn whole_layer(d: &crate::DocState, id: LayerId) -> Option> { - let l = d.doc.layer(id).filter(|_| takes_whole(d, id))?; - let mut copies = scratch_doc(&d.doc); - copies.layers.push(l.clone()); - Some(Box::new(copies)) -} - -/// The layers Copy takes when several are selected and nothing is selected in the pixels -/// (Photoshop, #2478): the selected layers that no selected group already carries, bottom-to-top. -/// Empty otherwise, and for a single layer ([`takes_whole`] decides that case). -fn multi_copy_ids(d: &crate::DocState) -> Vec { - if d.doc.selection.is_some() || !crate::layer_copy_cmds::layered(d.doc.mode) { - return Vec::new(); - } - let selected = d.selected_layers(); - if selected.len() < 2 { - return Vec::new(); - } - crate::layer_multi_cmds::top_level(&d.doc, &selected) -} - -/// Every selected layer, whole ([`multi_copy_ids`]), in a scratch document: Paste recreates them -/// all, each with its mask, styles and content. The Background becomes an ordinary layer, as in -/// Photoshop's Duplicate Layer. -fn whole_layers(d: &crate::DocState, ids: &[LayerId]) -> Option> { - let mut copies = scratch_doc(&d.doc); - for id in ids { - let mut l = d.doc.layer(*id)?.clone(); - if d.doc.is_background(*id) { - l.name = "Layer 0".into(); - l.locks = Default::default(); - } - copies.layers.push(l); - } - Some(Box::new(copies)) -} - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} - -/// Paste and New from Clipboard need only a clipboard: with no document open, they make one. -fn has_clip_only(s: &Session) -> std::result::Result<(), String> { - s.clipboard.as_ref().map(|_| ()).ok_or_else(|| "the clipboard is empty".into()) -} - -fn active_layer(s: &Session) -> std::result::Result<&Layer, String> { - let d = s.active().ok_or("no document open")?; - d.active_layer.and_then(|id| d.doc.layer(id)).ok_or_else(|| "no active layer".into()) -} - -/// Cut and Layer via Cut edit the pixels, so they need a pixel layer. -fn has_pixels(s: &Session) -> std::result::Result<(), String> { - match active_layer(s)?.content { - LayerContent::Raster(_) => Ok(()), - LayerContent::Smart(_) => Err("the smart object is not directly editable".into()), - _ => Err("the active layer has no pixels".into()), - } -} - -/// Copy only reads: any layer that shows pixels will do, so a smart object, type or shape layer -/// copies what it shows (Photoshop). -fn has_layer_pixels(s: &Session) -> std::result::Result<(), String> { - active_layer(s)?.surface().map(|_| ()).ok_or_else(|| "the active layer has no pixels".into()) -} - -/// Copy: a targeted alpha channel (#2307), a layer with pixels, or one taken whole (a group with -/// no selection, #2670). -fn can_copy(s: &Session) -> std::result::Result<(), String> { - if crate::channel_clip::copies_alpha_channel(s) || s.active().is_some_and(|d| !multi_copy_ids(d).is_empty()) { - return Ok(()); - } - has_layer_pixels(s).or_else(|e| s.active().and_then(|d| d.active_layer.filter(|id| takes_whole(d, *id))).map(|_| ()).ok_or(e)) -} - -fn has_clip(s: &Session) -> std::result::Result<(), String> { - has_doc(s)?; - s.clipboard.as_ref().map(|_| ()).ok_or_else(|| "the clipboard is empty".into()) -} - -fn active_id(s: &Session) -> Result { - s.active().and_then(|d| d.active_layer).ok_or(EngineError::Other("no active layer".into())) -} - -/// The pixels of `src` inside the selection (or all of them), as a straight-alpha surface. -/// -/// Built tile by tile (#4516). A source tile that the copy takes whole (inside the canvas, fully -/// selected or no selection, already in the clip's format) is shared copy-on-write, so a plain -/// Copy or Select All, Copy costs a pointer per tile; only the other tiles are read and written, -/// one at a time, instead of the whole area as one `f32` buffer. -fn lift(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Result { - let fmt = src.format(); - let with_alpha = PixelFormat::new(fmt.mode, fmt.sample, true); - let area = match sel { - Some(m) => m.content_bounds().intersect(&canvas), - None => src.content_bounds().intersect(&canvas), - }; - let mut out = Surface::new(with_alpha); - if area.is_empty() { - return Ok(Clip { surface: out, bounds: Rect::EMPTY, layers: None, resolution: None }); - } - // Only the selected area is read: converting the whole layer (a 24 MP Background gaining - // alpha) cost about a second per Layer via Copy (#668). - let mut shared = Vec::new(); - for tc in area.tiles() { - let tr = tc.rect().intersect(&area); - if fmt.alpha - && tr == tc.rect() - && let Some(tile) = src.tile(tc) - && sel.is_none_or(|m| crate::fill_cmds::Mask::of(m, tc).full(tr)) - { - // Reading and writing it back would give the same bytes. - shared.push((tc, tile.clone())); - continue; - } - let px = lift_region(src, sel, tr, with_alpha.channels())?; - out.try_write_region(tr, &px).map_err(|e| EngineError::Other(format!("not enough memory while copying pixels ({e}); the document was not changed")))?; - } - out.put_tiles(shared); - out.prune(); - let bounds = out.content_bounds(); - Ok(Clip { surface: out, bounds, layers: None, resolution: None }) -} - -/// [`lift`] over one rectangle: `src`'s pixels with alpha (`n` channels), the selection -/// multiplied into it. -fn lift_region(src: &Surface, sel: Option<&Surface>, r: Rect, n: usize) -> Result> { - let fmt = src.format(); - let mut px = if fmt.alpha { - crate::allocation::read_region(src, r, "copying pixels")? - } else { - let opaque = crate::allocation::read_region(src, r, "copying pixels")?; - let k = fmt.channels(); - let len = opaque - .len() - .checked_div(k.max(1)) - .and_then(|pixels| pixels.checked_mul(n)) - .ok_or_else(|| EngineError::Other("not enough memory for copying pixels (requested size overflowed); the document was not changed".into()))?; - let mut v = crate::allocation::filled(len, 0.0, "copying pixels")?; - let mut at = 0; - for p in opaque.chunks_exact(k.max(1)) { - let Some(dst) = v.get_mut(at..at + k.max(1)) else { break }; - dst.copy_from_slice(p); - at += k.max(1); - if let Some(alpha) = v.get_mut(at) { - *alpha = 1.0; - } - at += 1; - } - v - }; - if let Some(m) = sel { - let mask = crate::allocation::read_region(m, r, "applying the selection to copied pixels")?; - let mk = m.format().channels().max(1); - for (p, a) in px.chunks_exact_mut(n.max(1)).zip(mask.chunks_exact(mk)) { - if let Some(alpha) = p.last_mut() { - *alpha *= a[0]; - } - } - } - Ok(px) -} - -/// Merged composite of the visible document as a surface in the document's format. -fn merged_surface(doc: &Document) -> Surface { - let fmt = doc.pixel_format(); - photocraft_compose::flatten_to_surface(doc, PixelFormat::new(fmt.mode, fmt.sample, true), None) -} - -fn copy(s: &mut Session, merged: bool) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let canvas = d.doc.bounds(); - let clip = if merged { - lift(&merged_surface(&d.doc), d.doc.selection.as_ref(), canvas)? - } else { - let id = d.active_layer.ok_or(EngineError::Other("no active layer".into()))?; - let several = multi_copy_ids(d); - let mut layers = if several.is_empty() { whole_layer(d, id) } else { whole_layers(d, &several) }; - if let Some(copies) = &mut layers { - copy_linked_sources(&d.doc, copies)?; - } - match (d.doc.layer(id).and_then(Layer::surface).filter(|_| several.is_empty()), &layers) { - (Some(surf), _) => Clip { layers, ..lift(surf, d.doc.selection.as_ref(), canvas)? }, - // A group has no pixels of its own: the clipboard pixels are what it shows. - (None, Some(copies)) => { - let mut shown = (**copies).clone(); - for l in &mut shown.layers { - l.visible = true; - } - Clip { layers, ..lift(&merged_surface(&shown), None, canvas)? } - } - (None, None) => return Err(EngineError::Other("the active layer has no pixels".into())), - } - }; - if clip.bounds.is_empty() { - return Err(EngineError::Other("Could not copy: the selected area is empty".into())); - } - let b = clip.bounds; - let clip = Clip { resolution: Some(d.doc.resolution_dpi), ..clip }; - s.clipboard = Some(clip); - Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()]})) -} - -/// Copy with the layer mask targeted (`"target":"mask"`, #2928): the mask's values as a -/// grayscale clip, inside the selection or the whole canvas — an untouched mask copies at its -/// default value, so a plain Reveal All / Hide All mask transfers too. Paste then writes into -/// another layer's mask (targeted, #1035), in this or another document. -fn copy_mask(s: &mut Session) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let canvas = d.doc.bounds(); - let id = d.active_layer.ok_or_else(|| EngineError::Other("no active layer".into()))?; - let mask = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?.mask.as_ref().ok_or_else(|| EngineError::Other("the active layer has no layer mask".into()))?; - let sel = d.doc.selection.as_ref(); - let area = sel.map_or(canvas, |m| m.content_bounds().intersect(&canvas)); - if area.is_empty() { - return Err(EngineError::Other("Could not copy: the selected area is empty".into())); - } - let n = mask.surface.format().channels().max(1); - let values = crate::allocation::read_region(&mask.surface, area, "copying the mask")?; - let len = values - .len() - .checked_div(n) - .and_then(|pixels| pixels.checked_mul(2)) - .ok_or_else(|| EngineError::Other("not enough memory for copying the mask (requested size overflowed); the document was not changed".into()))?; - let cover = sel.map(|m| (m.read_region(area), m.format().channels().max(1))); - let mut px = crate::allocation::filled(len, 0.0, "copying the mask")?; - for (i, p) in values.chunks_exact(n).enumerate() { - px[i * 2] = p.first().copied().unwrap_or(0.0); - px[i * 2 + 1] = cover.as_ref().map_or(1.0, |(m, mk)| m.get(i * mk).copied().unwrap_or(0.0)); - } - let fmt = PixelFormat::new(ColorMode::Grayscale, mask.surface.format().sample, true); - let mut surface = Surface::new(fmt); - surface - .try_write_region(area, &px) - .map_err(|e| EngineError::Other(format!("not enough memory while copying the mask ({e}); the document was not changed")))?; - surface.prune(); - let bounds = surface.content_bounds(); - if bounds.is_empty() { - return Err(EngineError::Other("Could not copy: the selected area is empty".into())); - } - let b = bounds; - let resolution = Some(d.doc.resolution_dpi); - s.clipboard = Some(Clip { surface, bounds: b, layers: None, resolution }); - Ok(json!({"bounds": [b.x0, b.y0, b.width(), b.height()], "target": "mask"})) -} - -fn clear_selected(doc: &mut Document, id: LayerId, background: [f32; 4]) -> Result<()> { - let sel = doc.selection.clone(); - let canvas = doc.bounds(); - let area = sel.as_ref().map_or(canvas, |m| m.content_bounds().intersect(&canvas)); - clear_area(doc, id, area, sel.as_ref(), background) -} - -/// Cut / Clear on a layer: makes the selected pixels transparent. The Background can't hold -/// transparency, so there the area is filled with the background colour instead (Photoshop). -pub(crate) fn clear_area(doc: &mut Document, id: LayerId, area: Rect, sel: Option<&Surface>, background: [f32; 4]) -> Result<()> { - doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let bg = doc.is_background(id); - let surf = crate::commands::paint_surface(doc, id, &Value::Null)?; - if bg { - crate::pixels::try_fill_surface(surf, area, background, sel, true)?; - } else { - crate::pixels::try_clear_surface(surf, area, sel)?; - } - surf.prune(); - Ok(()) -} - -/// Paste as a new layer (or into the targeted mask or channel: [`paste_to_target`]). `in_place` keeps the original position; otherwise the pixels are centred -/// on `center` (the view centre from the UI) or the canvas, unless they already overlap the canvas. -fn paste(s: &mut Session, p: &Value, in_place: bool) -> Result { - crate::allocation::checkpoint("pasting pixels")?; - let clip = s.clipboard.clone().ok_or(EngineError::Other("the clipboard is empty".into()))?; - let Some(d) = s.active() else { - // Nothing open to paste into: the clipboard becomes a document of its own (#368). - return new_from_clipboard(s); - }; - if crate::channel_cmds::target_of(p) != crate::channel_cmds::Target::Pixels { - return paste_to_target(s, p, in_place, None, "Paste"); - } - // A targeted colour channel takes the paste instead of a new layer (#2307). - if let Some(k) = crate::channel_clip::paste_color_target(s, p) { - return crate::channel_clip::paste(s, p, k, in_place, None, "Paste"); - } - let canvas = d.doc.bounds(); - let fmt = d.doc.pixel_format(); - let (dx, dy) = paste_offset(&clip, canvas, p, in_place); - if let Some(layers) = clip.layers.as_deref().filter(|_| crate::layer_copy_cmds::layered(d.doc.mode)) { - return paste_layers(s, layers, dx, dy); - } - let moved = shifted(&clip.surface, dx, dy); - let target = PixelFormat::new(fmt.mode, fmt.sample, true); - let surf = if moved.format() == target { moved } else { moved.convert(target) }; - let before = d.doc.clone(); - let id = s.edit("Paste", |doc, active| { - let mut l = Layer::raster(doc.next_layer_name("Layer"), target); - *crate::pixels_mut(&mut l)? = surf; - let id = doc.insert_above(*active, l); - *active = Some(id); - doc.selection = None; - Ok(id) - })?; - // Only where the pasted pixels land needs recompositing, not the whole document (#2901). - crate::layer_multi_cmds::note_insert(s, &before); - Ok(json!({"layer": id.0, "offset": [dx, dy]})) -} - -/// Paste of a whole copied layer ([`whole_layer`]): fresh copies (a group's children too), -/// converted to the document's colour, moved as the pixels would be, above the active layer. A pasted smart object gets its -/// own contents, independent of the copied one (as across documents). -fn paste_layers(s: &mut Session, layers: &Document, dx: i32, dy: i32) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let mut copies = layers.clone(); - for l in &mut copies.layers { - *l = l.duplicate(); - } - crate::layer_copy_cmds::adapt_copies(s, &mut copies, &d.doc)?; - crate::layer_copy_cmds::translate_copies(&mut copies, dx, dy); - let patterns: Vec<_> = copies.patterns.iter().filter(|pat| !d.doc.patterns.iter().any(|q| q.id == pat.id)).cloned().collect(); - // A copied smart object may name a PSD-embedded linked file; land its `lnk` block too (#3584). - let mut linked = Vec::new(); - for (signature, key, data) in copies.metadata.psd_global_blocks.iter().filter(|(_, k, _)| matches!(k, b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE")) { - if d.doc.metadata.psd_global_blocks.iter().any(|(_, dk, dd)| dk == key && std::sync::Arc::ptr_eq(dd, data)) { - continue; - } - // The clipboard may contain a subset of a destination block. Compare complete records, - // so copying one layer in the same document does not duplicate large embedded files. - let missing = photocraft_io::linked::select_items(data, &|_, raw| { - !d.doc.metadata.psd_global_blocks.iter().any(|(_, dk, dd)| dk == key && photocraft_io::linked::contains_item(dd, raw)) - }) - .map_err(|e| EngineError::Other(format!("Could not paste smart-object contents: {e}; the document was not changed")))?; - let missing = missing.map(std::sync::Arc::new).unwrap_or_else(|| data.clone()); - if !missing.is_empty() { - linked.try_reserve(1).map_err(|_| EngineError::Other("not enough memory to paste smart-object source records".into()))?; - linked.push((*signature, *key, missing)); - } - } - let id = s.edit("Paste", |doc, active| { - doc.patterns.extend(patterns); - doc.metadata.psd_global_blocks.extend(linked); - for l in copies.layers { - *active = Some(doc.insert_above(*active, l)); - } - doc.selection = None; - active.ok_or(EngineError::Other("nothing to paste".into())) - })?; - Ok(json!({"layer": id.0, "offset": [dx, dy]})) -} - -/// How far a paste moves the clipboard pixels: not at all in place (or when they lie on the -/// canvas and no `center` is given), else onto `center` (the view centre from the UI) or the -/// canvas centre. -pub(crate) fn paste_offset(clip: &Clip, canvas: Rect, p: &Value, in_place: bool) -> (i32, i32) { - if in_place || (clip.bounds.intersect(&canvas) == clip.bounds && p.get("center").is_none()) { - return (0, 0); - } - let c = p.get("center").and_then(Value::as_array).filter(|a| a.len() >= 2).map(|a| (a[0].as_f64().unwrap_or(0.0), a[1].as_f64().unwrap_or(0.0))); - let (cx, cy) = c.unwrap_or(((canvas.x0 + canvas.x1) as f64 / 2.0, (canvas.y0 + canvas.y1) as f64 / 2.0)); - let b = clip.bounds; - ((cx - (b.x0 + b.x1) as f64 / 2.0).round() as i32, (cy - (b.y0 + b.y1) as f64 / 2.0).round() as i32) -} - -pub(crate) fn shifted(surface: &Surface, dx: i32, dy: i32) -> Surface { - if dx == 0 && dy == 0 { surface.clone() } else { photocraft_algo::resample::translate_surface(surface, dx, dy) } -} - -/// Paste into the targeted layer mask, alpha channel or Quick Mask (`"target"`) instead of as a -/// new layer (Photoshop, #1035): the pasted pixels' luminosity, placed as a paste places them, -/// over what was there. Their transparency, and `limit` (Paste Into / Outside), let it show -/// through. One history step. -pub(crate) fn paste_to_target(s: &mut Session, p: &Value, in_place: bool, limit: Option<&Surface>, label: &str) -> Result { - crate::allocation::checkpoint("pasting pixels")?; - let clip = s.clipboard.clone().ok_or(EngineError::Other("the clipboard is empty".into()))?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let canvas = d.doc.bounds(); - let layer = d.active_layer; - let (dx, dy) = paste_offset(&clip, canvas, p, in_place); - let src = shifted(&clip.surface, dx, dy); - let area = src.content_bounds().intersect(&canvas); - s.edit(label, |doc, _| { - let (surf, _) = crate::channel_cmds::target_surface(doc, layer, p)?; - crate::fill_cmds::composite_over(surf, &src, area, limit)?; - surf.prune(); - doc.selection = None; - Ok(()) - })?; - Ok(json!({"target": p.get("target"), "offset": [dx, dy]})) -} - -/// A new document the size of the clipboard image, holding it as its one layer, in the pixel -/// format it was copied in (#368). -fn new_from_clipboard(s: &mut Session) -> Result { - let clip = s.clipboard.clone().ok_or(EngineError::Other("the clipboard is empty".into()))?; - let b = clip.bounds; - let (w, h) = (b.width(), b.height()); - if w == 0 || h == 0 { - return Err(EngineError::Other("the clipboard image has no size".into())); - } - let (Some(dx), Some(dy)) = (b.x0.checked_neg(), b.y0.checked_neg()) else { - return Err(EngineError::Other("the clipboard origin is outside the coordinate range for a new document".into())); - }; - let fmt = clip.surface.format(); - let target = PixelFormat::new(fmt.mode, fmt.sample, true); - let moved = if dx == 0 && dy == 0 { clip.surface } else { photocraft_algo::resample::translate_surface(&clip.surface, dx, dy) }; - let mut doc = Document::new("Untitled", photocraft_geom::Size::new(w, h), fmt.mode, fmt.sample); - if let Some(r) = clip.resolution.filter(|r| r.is_finite() && *r > 0.0) { - doc.resolution_dpi = r; - } - let mut l = Layer::raster(doc.next_layer_name("Layer"), target); - *crate::pixels_mut(&mut l)? = if moved.format() == target { moved } else { moved.convert(target) }; - doc.layers.push(l); - let i = s.add_document(doc, None); - // Clipboard content has never been saved; closing this tab must ask before discarding it. - if let Some(st) = s.active_mut() { - st.saved_revision = 0; - } - Ok(json!({"document": i, "width": w, "height": h})) -} - -fn layer_via(s: &mut Session, cut: bool) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let id = active_id(s)?; - let has_sel = d.doc.selection.is_some(); - if !has_sel && !cut && crate::layer_multi_cmds::multi(s, &Value::Null) { - // No selection, several layers selected: copy every one of them (Photoshop, #2777). - return crate::layer_multi_cmds::duplicate_selected(s, true, "Layer Via Copy"); - } - if !has_sel && !cut { - // No selection: Layer via Copy duplicates the whole layer (Photoshop). - let nid = s.edit("Layer Via Copy", |doc, active| { - let src = doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let mut dup = src.duplicate(); - dup.name = doc.copy_name(&src.name); - dup.locks = Default::default(); - let nid = doc.insert_above(Some(id), dup); - *active = Some(nid); - Ok(nid) - })?; - return Ok(json!({"layer": nid.0})); - } - let surf = d.doc.layer(id).and_then(Layer::surface).ok_or(EngineError::Other("the active layer has no pixels".into()))?; - let clip = lift(surf, d.doc.selection.as_ref(), d.doc.bounds())?; - if clip.bounds.is_empty() { - return Err(EngineError::Other("Could not complete the command: the selected area is empty".into())); - } - let bg = s.tools.background; - let label = if cut { "Layer Via Cut" } else { "Layer Via Copy" }; - let nid = s.edit(label, |doc, active| { - if cut { - clear_selected(doc, id, bg)?; - } - let mut l = Layer::raster(doc.next_layer_name("Layer"), clip.surface.format()); - *crate::pixels_mut(&mut l)? = clip.surface; - let nid = doc.insert_above(Some(id), l); - *active = Some(nid); - // Photoshop deselects: a following Free Transform moves the whole new layer (#1096). - doc.selection = None; - Ok(nid) - })?; - Ok(json!({"layer": nid.0})) -} - -fn merge_visible(s: &mut Session) -> Result { - s.edit("Merge Visible", |doc, active| { - let visible: Vec = doc.layers.iter().enumerate().filter(|(_, l)| l.visible).map(|(i, _)| i).collect(); - if visible.len() < 2 && !visible.iter().any(|i| doc.layers[*i].is_group()) { - return Err(EngineError::Other("there are not enough visible layers to merge".into())); - } - let lowest = visible[0]; - let base = doc.layers[lowest].clone(); - let is_background = lowest == 0 && doc.is_background(base.id); - // Composite of visible layers only (hidden ones stay where they are). - let mut solo = doc.clone(); - solo.layers.retain(|l| l.visible); - let fmt = doc.pixel_format(); - let fmt = if is_background { fmt } else { PixelFormat::new(fmt.mode, fmt.sample, true) }; - let mut merged = Layer::raster(base.name.clone(), fmt); - merged.locks = base.locks; - *crate::pixels_mut(&mut merged)? = crate::pixels::composite_layers(&solo, fmt, is_background.then_some([1.0, 1.0, 1.0])); - let mid = merged.id; - let mut out = Vec::with_capacity(doc.layers.len()); - for (i, l) in doc.layers.drain(..).enumerate() { - if i == lowest { - out.push(merged.clone()); - } else if !l.visible { - out.push(l); - } - } - doc.layers = out; - *active = Some(mid); - Ok(()) - })?; - Ok(Value::Null) -} - -/// Histogram of a pixel layer (8-bit bins per channel: R, G, B) inside the selection. -fn histograms(surf: &Surface, area: Rect) -> [[u64; 256]; 3] { - let mut h = [[0u64; 256]; 3]; - let step = ((area.width() as u64 * area.height() as u64) / 4_000_000).max(1) as usize; // sample big images - let mut row = vec![[0u8; 4]; area.width() as usize]; - for y in (area.y0..area.y1).step_by(step.isqrt().max(1)) { - surf.read_rgba8_into(Rect::new(area.x0, y, area.x1, y + 1), &mut row); - for p in row.iter().step_by(step.isqrt().max(1)) { - if p[3] == 0 { - continue; - } - for c in 0..3 { - h[c][p[c] as usize] += 1; - } - } - } - h -} - -/// Black and white points with `clip` (fraction) clipped at each end. -fn clip_points(h: &[u64; 256], clip: f64) -> (f32, f32) { - let total: u64 = h.iter().sum(); - if total == 0 { - return (0.0, 1.0); - } - let lim = (total as f64 * clip) as u64; - let (mut acc, mut lo) = (0u64, 0usize); - while lo < 254 && acc + h[lo] <= lim { - acc += h[lo]; - lo += 1; - } - let (mut acc, mut hi) = (0u64, 255usize); - while hi > lo + 1 && acc + h[hi] <= lim { - acc += h[hi]; - hi -= 1; - } - (lo as f32 / 255.0, hi as f32 / 255.0) -} - -fn median(h: &[u64; 256]) -> f32 { - let total: u64 = h.iter().sum(); - let mut acc = 0; - for (i, v) in h.iter().enumerate() { - acc += v; - if acc * 2 >= total { - return i as f32 / 255.0; - } - } - 0.5 -} - -/// Image › Auto Tone / Auto Contrast / Auto Color (0.1% clipping, Photoshop's defaults). -fn auto_adjust(s: &mut Session, kind: &str) -> Result { - let id = active_id(s)?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let surf = d.doc.layer(id).and_then(Layer::surface).ok_or(EngineError::Other("the active layer has no pixels".into()))?; - let area = d.doc.selection.as_ref().map_or(d.doc.bounds(), |m| m.content_bounds()).intersect(&d.doc.bounds()); - let h = histograms(surf, area); - let clip = 0.001; - let mut per = [LevelsChannel::default(), LevelsChannel::default(), LevelsChannel::default()]; - match kind { - "contrast" => { - // Same points for all channels (preserves colour), from the combined histogram. - let mut all = [0u64; 256]; - for c in &h { - for (a, v) in all.iter_mut().zip(c) { - *a += v; - } - } - let (lo, hi) = clip_points(&all, clip); - for p in &mut per { - (p.in_black, p.in_white) = (lo, hi); - } - } - _ => { - for (c, p) in per.iter_mut().enumerate() { - (p.in_black, p.in_white) = clip_points(&h[c], clip); - } - if kind == "color" { - // Neutralise midtones: move each channel's median onto the average median. - let meds: Vec = (0..3).map(|c| median(&h[c])).collect(); - let target = meds.iter().sum::() / 3.0; - for (c, p) in per.iter_mut().enumerate() { - let t = ((meds[c] - p.in_black) / (p.in_white - p.in_black).max(1e-3)).clamp(0.02, 0.98); - let want = ((target - p.in_black) / (p.in_white - p.in_black).max(1e-3)).clamp(0.02, 0.98); - // out = t^(1/g) = want → g = ln t / ln want - p.gamma = (t.ln() / want.ln()).clamp(0.1, 9.99); - } - } - } - } - let adj = Adjustment::Levels { master: LevelsChannel::default(), per_channel: per, space: Default::default(), black: LevelsChannel::default() }; - let label = match kind { - "contrast" => "Auto Contrast", - "color" => "Auto Color", - _ => "Auto Tone", - }; - s.edit(label, |doc, _| { - let sel = doc.selection.clone(); - let mode = doc.mode; - let surf = crate::commands::paint_surface(doc, id, &Value::Null)?; - crate::pixels::adjust_surface(surf, &adj, sel.as_ref(), mode); - Ok(()) - })?; - Ok(Value::Null) -} - -fn toggle_last_state(s: &mut Session) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let redo = st.history.can_redo(); - Ok(json!(if redo { s.redo() } else { s.undo() })) -} - -/// Edit › Transform › Again: replay the last `edit.transform` on the active layer. -fn transform_again(s: &mut Session) -> Result { - let p = transform_again_params(s)?; - s.execute("edit.transform", p) -} - -/// Transform Again on a copy (⌥⇧⌘T, no menu item): duplicates the active layer and repeats the -/// last transform on the copy, as one history step, so pressing it again steps and repeats -/// (#352). The transform is worked out before anything changes, so a refusal leaves no copy. -fn transform_again_copy(s: &mut Session) -> Result { - let p = transform_again_params(s)?; - let id = active_id(s)?; - let journal = s.journal.len(); - // The steps are recorded (Again on a Copy is not replayable itself): with one layer selected, - // "duplicate the current layer" plays back on any document, a layer id only on this one. - let dup = if crate::layer_multi_cmds::multi(s, &Value::Null) { json!({"layer": id.0}) } else { json!({}) }; - s.execute("layer.duplicate", dup)?; - match s.execute("edit.transform", p) { - Ok(r) => { - let st = s.active_mut().ok_or(EngineError::NoDocument)?; - st.history.purge_last(); - st.history.set_current_label("Transform Again"); - Ok(r) - } - Err(e) => { - // The copy (e.g. of a layer whose position is locked) couldn't be transformed: take - // it back, leaving nothing to redo and no Duplicate step to record. - s.undo(); - s.journal.truncate(journal); - if let Some(st) = s.active_mut() { - st.history.clear_redo(); - } - Err(e) - } - } -} - -/// The `edit.transform` params that repeat the last transform on the active layer. -fn transform_again_params(s: &Session) -> Result { - let (_, last) = - s.journal.iter().rev().find(|(id, _)| id == "edit.transform").cloned().ok_or(EngineError::Other("there is no transform to repeat".into()))?; - let (rect, quad) = (last.get("rect").cloned(), last.get("quad").cloned()); - let mut p = json!({"interpolation": last.get("interpolation").cloned().unwrap_or(json!("bicubic"))}); - match (rect, quad, last.get("matrix").cloned()) { - (Some(r), Some(q), _) => { - let v = |x: &Value| -> Vec { x.as_array().map(|a| a.iter().filter_map(Value::as_f64).collect()).unwrap_or_default() }; - let r = v(&r); - let q: Vec> = q.as_array().map(|a| a.iter().map(v).collect()).unwrap_or_default(); - if r.len() != 4 || q.len() != 4 || q.iter().any(|row| row.len() < 2) { - return Err(EngineError::Other("the last transform can't be repeated".into())); - } - let h = photocraft_algo::transform::Homography::rect_to_quad( - [r[0], r[1], r[2], r[3]], - [[q[0][0], q[0][1]], [q[1][0], q[1][1]], [q[2][0], q[2][1]], [q[3][0], q[3][1]]], - ) - .ok_or(EngineError::Other("the last transform can't be repeated".into()))?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let l = d.doc.layer(active_id(s)?).ok_or(EngineError::Other("no active layer".into()))?; - let b = crate::transform_cmds::transform_bounds(&d.doc, l); - if b.is_empty() { - return Err(EngineError::Other("nothing to transform".into())); - } - let nr = [b.x0 as f64, b.y0 as f64, b.x1 as f64, b.y1 as f64]; - let corners = [(nr[0], nr[1]), (nr[2], nr[1]), (nr[2], nr[3]), (nr[0], nr[3])].map(|(x, y)| { - let (a, b) = h.apply(x, y); - [a, b] - }); - p["rect"] = json!(nr); - p["quad"] = json!(corners); - } - (_, _, Some(m)) => p["matrix"] = m, - _ => return Err(EngineError::Other("the last transform can't be repeated".into())), - } - Ok(p) -} - -/// Guides further than this from the canvas origin are refused: far past any document (300000 px) -/// yet well within f32 precision. -const MAX_GUIDE_PX: f64 = 1_000_000.0; - -/// A guide `position` in document pixels: a number of pixels, or text with a unit as in Photoshop's -/// New Guide dialog ("50%", "2.5 cm", "12 pt"). Percent is of the document's width for a vertical -/// guide and its height for a horizontal one; points and picas follow Units & Rulers › Point Size. -/// `None` when there is no `position`. -fn guide_position(s: &Session, p: &Value, vertical: bool) -> Result> { - let Some(v) = p.get("position") else { return Ok(None) }; - let px = match (v.as_f64(), v.as_str()) { - (Some(n), _) => n, - (None, Some(text)) => { - let (n, unit) = crate::prefs::Unit::parse_length(text).ok_or_else(|| { - EngineError::Other(format!("`position` {text:?} is not a length: give a number of pixels, or a number with px, in, cm, mm, pt, pc or %")) - })?; - let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; - let extent = if vertical { doc.size.width } else { doc.size.height }; - unit.unwrap_or(crate::prefs::Unit::Pixels).to_px(n, doc.resolution_dpi as f64, extent as f64, s.prefs().units_and_rulers.point_size.per_inch()) - } - _ => return Err(EngineError::Other("`position` must be a number of pixels or text with a unit".into())), - }; - if !px.is_finite() || px.abs() > MAX_GUIDE_PX { - return Err(EngineError::Other(format!("`position` is out of range (at most {MAX_GUIDE_PX} px from the canvas origin)"))); - } - Ok(Some(px)) -} - -/// Photoshop's guide colours, in its order, by the names `view.newGuide` takes: New Guide's and -/// New Guide Layout's Color lists. Cyan (the default guide colour in Preferences, so a guide -/// dragged from a ruler reads as Cyan), Magenta and Yellow are the values New Guide Layout first -/// shipped with; the rest approximate Photoshop's on screen, rounded to web-safe steps. -pub const GUIDE_COLORS: [(&str, [u8; 3]); 9] = [ - ("lightBlue", [0x33, 0x66, 0xff]), - ("lightRed", [0xff, 0x33, 0x33]), - ("green", [0x33, 0xff, 0x33]), - ("mediumBlue", [0x33, 0x33, 0xff]), - ("yellow", [0xff, 0xcc, 0x00]), - ("magenta", [0xff, 0x00, 0xff]), - ("cyan", [0x4a, 0xff, 0xff]), - ("lightGray", [0xe7, 0xe7, 0xe7]), - ("black", [0x00, 0x00, 0x00]), -]; - -/// A guide `color`: one of [`GUIDE_COLORS`]' names or `"#rrggbb"`. `None` when there is no `color` -/// (the guide uses Preferences › Guides, Grid & Slices › Guide colour). -fn guide_color(p: &Value) -> Result> { - let Some(v) = p.get("color") else { return Ok(None) }; - let text = v.as_str().ok_or(EngineError::Other("`color` must be a colour name or \"#rrggbb\"".into()))?; - GUIDE_COLORS.iter().find(|(name, ..)| name.eq_ignore_ascii_case(text)).map(|(.., rgb)| *rgb).or_else(|| crate::prefs::parse_hex(text)).map(Some).ok_or_else( - || EngineError::Other(format!("`color` {text:?} is not a guide colour: use \"#rrggbb\" or one of {}", GUIDE_COLORS.map(|c| c.0).join(", "))), - ) -} - -/// Edit Selected Guides (the guide context menu): change one guide's orientation, position and -/// colour in one history step. A guide that keeps its orientation keeps its index; one that turns -/// goes to the end of the other list. Without `color` it keeps its own colour, or none. -fn edit_guide(s: &mut Session, p: &Value) -> Result { - let orientation = |key: &str| match p.get(key).map(|v| v.as_str()) { - None => Ok(None), - Some(Some("horizontal")) => Ok(Some(false)), - Some(Some("vertical")) => Ok(Some(true)), - Some(_) => Err(EngineError::Other(format!("`{key}` must be \"horizontal\" or \"vertical\""))), - }; - let from = orientation("orientation")?.unwrap_or(false); - let to = orientation("newOrientation")?.unwrap_or(from); - let index = p.get("index").and_then(Value::as_u64).ok_or(EngineError::Other("missing `index`".into()))?; - let pos = guide_position(s, p, to)?; - let color = guide_color(p)?; - s.edit("Edit Guide", |doc, _| { - let guides = &mut doc.guides; - let list = if from { &guides.vertical } else { &guides.horizontal }; - let i = usize::try_from(index).ok().filter(|i| *i < list.len()).ok_or(EngineError::Other("no such guide".into()))?; - let old = list[i]; - let at = pos.unwrap_or(old); - let rgb = color.or_else(|| guides.color(from, old)); - if from == to { - let list = if from { &mut guides.vertical } else { &mut guides.horizontal }; - list[i] = at; - } else { - (if from { &mut guides.vertical } else { &mut guides.horizontal }).remove(i); - (if to { &mut guides.vertical } else { &mut guides.horizontal }).push(at); - } - guides.prune_colors(); - if rgb.is_some() { - guides.set_color(to, at, rgb); - } - Ok(()) - })?; - Ok(Value::Null) -} - -/// View › New Guide / guide moves (undoable, like Photoshop's "New Guide"/"Move Guide" states). -fn guide_cmd(s: &mut Session, p: &Value, op: &str) -> Result { - let vertical = p.get("orientation").and_then(Value::as_str).unwrap_or("horizontal") == "vertical"; - let pos = guide_position(s, p, vertical)?; - let color = if op == "new" { guide_color(p)? } else { None }; - let index = p.get("index").and_then(Value::as_u64).map(|v| v as usize); - let label = match op { - "new" => "New Guide", - "move" => "Move Guide", - "delete" => "Delete Guide", - _ => "Clear Guides", - }; - s.edit(label, |doc, _| { - let guides = &mut doc.guides; - let g = if vertical { &mut guides.vertical } else { &mut guides.horizontal }; - match op { - "new" => { - let at = pos.ok_or(EngineError::Other("missing `position`".into()))?; - g.push(at); - if color.is_some() { - guides.set_color(vertical, at, color); - } - } - "move" => { - let i = index.filter(|i| *i < g.len()).ok_or(EngineError::Other("no such guide".into()))?; - let to = pos.ok_or(EngineError::Other("missing `position`".into()))?; - let from = std::mem::replace(&mut g[i], to); - // The guide takes its colour along; another guide left at the old position keeps it. - if let Some(rgb) = guides.color(vertical, from) { - guides.set_color(vertical, to, Some(rgb)); - } - guides.prune_colors(); - } - "delete" => { - let i = index.filter(|i| *i < g.len()).ok_or(EngineError::Other("no such guide".into()))?; - g.remove(i); - guides.prune_colors(); - } - _ => { - guides.horizontal.clear(); - guides.vertical.clear(); - guides.colors.clear(); - } - } - Ok(()) - })?; - Ok(Value::Null) -} - -/// Farthest the ruler zero point may sit from the image, in document pixels. -const MAX_RULER_ORIGIN: f64 = 1.0e7; - -/// Move the rulers' zero point (`view.setRulerOrigin`), or put it back at the image's top-left -/// (`view.resetRulerOrigin`, `p` = None). Undoable like the guides. -fn ruler_origin_cmd(s: &mut Session, p: Option<&Value>) -> Result { - let origin = match p { - None => [0.0, 0.0], - Some(p) => { - let axis = |k: &str| { - p.get(k) - .and_then(Value::as_f64) - .filter(|v| v.is_finite() && v.abs() <= MAX_RULER_ORIGIN) - .ok_or_else(|| EngineError::Other(format!("`{k}` must be a number of pixels within ±{MAX_RULER_ORIGIN}"))) - }; - [axis("x")?, axis("y")?] - } - }; - let label = if p.is_some() { "Set Ruler Origin" } else { "Reset Ruler Origin" }; - s.edit(label, |doc, _| { - doc.ruler_origin = origin; - Ok(()) - })?; - Ok(json!({"x": origin[0], "y": origin[1]})) -} - -pub fn specs() -> Vec { - macro_rules! spec { - ($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } - }; - } - vec![ - spec!("edit.cut", "Cut", &["Edit"], Some("Cmd+X"), "{} (a targeted colour channel cuts that channel only)", has_pixels, |s, p| { - // Refuse a locked layer before copying, so a refused Cut leaves the clipboard alone. - let id = active_id(s)?; - crate::commands::check_pixels_unlocked(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?; - // A targeted colour channel cuts that channel only (#2698). - if let Some(k) = crate::channel_clip::paste_color_target(s, p) { - return crate::channel_clip::cut(s, id, k); - } - let r = copy(s, false)?; - let bg = s.tools.background; - s.edit("Cut Pixels", |doc, _| clear_selected(doc, id, bg))?; - Ok(r) - }), - spec!( - "edit.copy", - "Copy", - &["Edit"], - Some("Cmd+C"), - r##"{"target":"mask"? = the targeted mask, copied as grayscale (#2928)} (a targeted colour or alpha channel copies that channel, as grayscale)"##, - can_copy, - |s, p| { - if crate::commands::is_mask_target(p) { - return copy_mask(s); - } - match crate::channel_clip::copy(s)? { - Some(r) => Ok(r), - None => copy(s, false), - } - } - ), - spec!("edit.copyMerged", "Copy Merged", &["Edit"], Some("Cmd+Shift+C"), "{}", has_doc, |s, _| copy(s, true)), - spec!( - "edit.paste", - "Paste", - &["Edit"], - Some("Cmd+V"), - concat!( - r#"{"center":[x,y]? (view centre; default keeps the position when it overlaps the canvas),"#, - paste_target!(), - "} (with no document open: a new document from the clipboard)" - ), - has_clip_only, - |s, p| paste(s, p, false) - ), - spec!( - "file.newFromClipboard", - "New from Clipboard", - &["File"], - None, - "{} (a new document the size of the clipboard image, holding it as one layer)", - has_clip_only, - |s, _| new_from_clipboard(s) - ), - spec!( - "edit.pasteSpecial.pasteInPlace", - "Paste in Place", - &["Edit", "Paste Special"], - Some("Cmd+Shift+V"), - concat!("{", paste_target!(), "}"), - has_clip, - |s, p| paste(s, p, true) - ), - spec!("layer.new.layerViaCopy", "Layer via Copy", &["Layer", "New"], Some("Cmd+J"), "{}", has_doc, |s, _| layer_via(s, false)), - spec!("layer.new.layerViaCut", "Layer via Cut", &["Layer", "New"], Some("Cmd+Shift+J"), "{}", has_pixels, |s, _| layer_via(s, true)), - spec!("layer.mergeVisible", "Merge Visible", &["Layer"], Some("Cmd+Shift+E"), "{}", has_doc, |s, _| merge_visible(s)), - spec!("image.autoTone", "Auto Tone", &["Image"], Some("Cmd+Shift+L"), "{}", has_pixels, |s, _| auto_adjust(s, "tone")), - spec!("image.autoContrast", "Auto Contrast", &["Image"], Some("Cmd+Alt+Shift+L"), "{}", has_pixels, |s, _| auto_adjust(s, "contrast")), - spec!("image.autoColor", "Auto Color", &["Image"], Some("Cmd+Shift+B"), "{}", has_pixels, |s, _| auto_adjust(s, "color")), - spec!("edit.toggleLastState", "Toggle Last State", &["Edit"], Some("Cmd+Alt+Z"), "{}", has_doc, |s, _| toggle_last_state(s)), - spec!("edit.transform.again", "Again", &["Edit", "Transform"], Some("Cmd+Shift+T"), "{}", has_doc, |s, _| transform_again(s)), - spec!( - "edit.transform.againCopy", - "Transform Again on a Copy", - &[], - Some("Cmd+Alt+Shift+T"), - "{} (duplicates the active layer and repeats the last transform on the copy: step and repeat)", - has_doc, - |s, _| transform_again_copy(s) - ), - spec!( - "view.newGuide", - "New Guide…", - &["View"], - None, - r##"{"orientation":"horizontal|vertical","position":px or "","color":"lightBlue|lightRed|green|mediumBlue|yellow|magenta|cyan|lightGray|black" or "#rrggbb"?} (units px, in, cm, mm, pt, pc or %; % is of the width for a vertical guide, the height for a horizontal one; without a color the guide uses the guide colour in Preferences)"##, - has_doc, - |s, p| guide_cmd(s, p, "new") - ), - spec!( - "view.moveGuide", - "Move Guide", - &[], - None, - r##"{"orientation":"horizontal|vertical","index":n,"position":px or ""} (units as for view.newGuide)"##, - has_doc, - |s, p| guide_cmd(s, p, "move") - ), - spec!( - "view.editGuide", - "Edit Guide", - &[], - None, - r##"{"orientation":"horizontal|vertical","index":n,"newOrientation":"horizontal|vertical"?,"position":px or ""?,"color":name or "#rrggbb"?} (Edit Selected Guides: units and colours as for view.newGuide; % is of the new orientation's extent; whatever is left out stays as it was)"##, - has_doc, - edit_guide - ), - spec!("view.deleteGuide", "Delete Guide", &[], None, r##"{"orientation":"horizontal|vertical","index":n}"##, has_doc, |s, p| guide_cmd(s, p, "delete")), - spec!("view.clearGuides", "Clear Guides", &["View"], None, "{}", has_doc, |s, p| guide_cmd(s, p, "clear")), - spec!( - "view.setRulerOrigin", - "Set Ruler Origin", - &[], - None, - r##"{"x":px,"y":px} (the rulers' zero point in document pixels; the rulers and Info panel measure from it)"##, - has_doc, - |s, p| ruler_origin_cmd(s, Some(p)) - ), - spec!("view.resetRulerOrigin", "Reset Ruler Origin", &[], None, "{} (zero point back at the image's top-left)", has_doc, |s, _| ruler_origin_cmd( - s, None - )), - ] -} - -#[cfg(test)] -mod tests { - use super::*; - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, a| { - doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 50, 30), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - #[test] - fn new_clipboard_document_rejects_an_unrepresentable_origin() { - let mut s = Session::new(); - let mut surface = Surface::new(PixelFormat::RGBA8); - surface.write_pixel(i32::MIN, 0, &[1.0, 0.0, 0.0, 1.0]); - s.clipboard = Some(Clip { surface, bounds: Rect::from_xywh(i32::MIN, 0, 1, 1), layers: None, resolution: None }); - let result = new_from_clipboard(&mut s); - assert!(result.unwrap_err().to_string().contains("clipboard origin")); - assert!(s.execute("file.newFromClipboard", json!({})).is_err()); - assert!(s.documents().is_empty()); - assert_eq!(s.clipboard.as_ref().unwrap().surface.rgba(i32::MIN, 0), [1.0, 0.0, 0.0, 1.0]); - } - - #[test] - fn injected_clear_buffer_failure_leaves_the_document_unchanged() { - let mut s = session(); - let st = s.active().unwrap(); - let id = st.active_layer.unwrap(); - let before = st.doc.layer(id).unwrap().surface().unwrap().clone(); - let revision = st.revision; - - crate::allocation::fail_next_for_test(); - let result = s.execute("edit.clear", json!({})); - - assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for erasing pixels"))); - let st = s.active().unwrap(); - assert_eq!(st.doc.layer(id).unwrap().surface(), Some(&before)); - assert_eq!(st.revision, revision); - } - - #[test] - fn injected_copy_buffer_failure_does_not_change_document_or_clipboard() { - let mut s = session(); - let st = s.active().unwrap(); - let id = st.active_layer.unwrap(); - let before = st.doc.layer(id).unwrap().surface().unwrap().clone(); - let revision = st.revision; - - crate::allocation::fail_next_for_test(); - let result = s.execute("edit.copy", json!({})); - - assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for copying pixels"))); - let st = s.active().unwrap(); - assert_eq!(st.doc.layer(id).unwrap().surface(), Some(&before)); - assert_eq!(st.revision, revision); - assert!(s.clipboard.is_none()); - } - - #[test] - fn injected_fill_result_reservation_failure_keeps_pixels_and_history() { - let mut s = session(); - let st = s.active().unwrap(); - let id = st.active_layer.unwrap(); - let before = st.doc.layer(id).unwrap().surface().unwrap().clone(); - let revision = st.revision; - let history_len = st.history.past_len(); - - crate::allocation::fail_next_for_test(); - let result = s.execute("edit.fill", json!({"color": "#00ff00"})); - - assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for blending pixels"))); - let st = s.active().unwrap(); - assert_eq!(st.doc.layer(id).unwrap().surface(), Some(&before)); - assert_eq!(st.revision, revision); - assert_eq!(st.history.past_len(), history_len); - } - - #[test] - fn injected_paste_failure_does_not_change_document_or_clipboard() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 8, "height": 8})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - let before_clip = s.clipboard.clone(); - let st = s.active().unwrap(); - let before_doc = (*st.doc).clone(); - let revision = st.revision; - let history_len = st.history.past_len(); - - crate::allocation::fail_next_for_test(); - let result = s.execute("edit.paste", json!({})); - - assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for pasting pixels"))); - let st = s.active().unwrap(); - assert_eq!(*st.doc, before_doc); - assert_eq!(st.revision, revision); - assert_eq!(st.history.past_len(), history_len); - let after_clip = s.clipboard.as_ref().expect("clipboard remains available"); - let before_clip = before_clip.as_ref().expect("clipboard was populated"); - assert_eq!(after_clip.bounds, before_clip.bounds); - assert_eq!(after_clip.surface, before_clip.surface); - } - - #[test] - fn injected_transform_failure_keeps_pixels_and_history() { - let mut s = session(); - let st = s.active().unwrap(); - let id = st.active_layer.unwrap(); - let before = st.doc.layer(id).unwrap().surface().unwrap().clone(); - let revision = st.revision; - let history_len = st.history.past_len(); - - crate::allocation::fail_next_for_test(); - let result = s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 20, 30]})); - - assert!(result.as_ref().is_err_and(|e| e.to_string().contains("not enough memory for transforming pixels"))); - let st = s.active().unwrap(); - assert_eq!(st.doc.layer(id).unwrap().surface(), Some(&before)); - assert_eq!(st.revision, revision); - assert_eq!(st.history.past_len(), history_len); - } - - fn active_bounds(s: &Session) -> Rect { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds() - } - - /// New from Clipboard takes the copied document's resolution (Photoshop's Clipboard preset), - /// so a later Image Size by resolution scales from the right size. - #[test] - fn new_from_clipboard_keeps_the_source_resolution() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 60, "height": 40, "resolution": 300})).unwrap(); - s.execute("select.all", json!({})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("file.newFromClipboard", json!({})).unwrap(); - assert_eq!(s.active().unwrap().doc.resolution_dpi, 300.0); - } - - /// #368: the clipboard as a document of its own, from New from Clipboard or a Paste with - /// nothing open, at every depth. - #[test] - fn new_from_clipboard_and_paste_with_no_document() { - for depth in [8, 16, 32] { - let mut s = Session::new(); - assert!(!s.is_enabled("file.newFromClipboard") && !s.is_enabled("edit.paste"), "nothing copied yet"); - assert!(s.execute("file.newFromClipboard", json!({})).is_err()); - s.execute("file.new", json!({"width": 100, "height": 100, "depth": depth})).unwrap(); - assert!(!s.active().unwrap().is_dirty(), "an untouched blank document is clean"); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 20, "width": 30, "height": 15})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - // With a document open: a second document, sized to the copy, its pixels at the origin. - let r = s.execute("file.newFromClipboard", json!({})).unwrap(); - assert_eq!((r["width"].as_u64(), r["height"].as_u64()), (Some(30), Some(15)), "{r}"); - assert_eq!(s.documents().len(), 2); - assert!(s.active().unwrap().is_dirty(), "a document made from clipboard pixels needs saving (#4665)"); - let d = &s.active().unwrap().doc; - assert_eq!((d.size.width, d.size.height, d.layers.len()), (30, 15, 1)); - assert_eq!(d.pixel_format().sample, s.documents()[0].doc.pixel_format().sample, "{depth}-bit kept"); - assert_eq!(active_bounds(&s), Rect::new(0, 0, 30, 15)); - let px = d.layers[0].surface().unwrap().rgba(0, 0); - assert!(px[0] > 0.99 && px[1] < 0.01 && px[3] > 0.99, "{px:?}"); - // With nothing open, Paste makes the document too. - while s.active().is_some() { - s.execute("file.close", json!({})).unwrap(); - } - assert!(s.is_enabled("edit.paste") && !s.is_enabled("edit.pasteSpecial.pasteInPlace")); - s.execute("edit.paste", json!({})).unwrap(); - assert_eq!(s.documents().len(), 1); - assert!(s.active().unwrap().is_dirty(), "Paste with no tab also needs a save prompt"); - assert_eq!(active_bounds(&s), Rect::new(0, 0, 30, 15)); - assert!(s.execute("edit.pasteSpecial.pasteInPlace", json!({})).is_ok(), "into the new document"); - } - } - - #[test] - fn copy_paste_in_place_and_centred() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 30)); - assert!(s.active().unwrap().doc.selection.is_none()); - s.execute("edit.paste", json!({"center": [70, 70]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(60, 60, 80, 80)); - assert_eq!(s.active().unwrap().doc.layers.len(), 4); - } - - #[test] - fn paste_with_malformed_center_does_not_panic() { - // A short/empty `center` array must not panic (Rule 9) — it falls back to the canvas centre. - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - for center in [json!([5]), json!([]), json!("nope"), json!([1, 2, 3])] { - // Fresh clipboard each time is unnecessary; copy persists. Must return Ok, never panic. - assert!(s.execute("edit.paste", json!({ "center": center })).is_ok(), "center={center}"); - } - } - - #[test] - fn copy_paste_between_documents() { - // Copy in one tab, paste into another: the clipboard is shared across documents. - let mut s = session(); // doc A (100x100): red rect at (10,10)-(50,30) - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 40, "height": 20})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - // Open a second document (a new tab), which becomes active. - s.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); - assert_eq!(s.active_index().unwrap(), 1, "the new document is the active tab"); - let before = s.active().unwrap().doc.layers.len(); - s.execute("edit.paste", json!({})).unwrap(); - assert_eq!(s.active_index().unwrap(), 1, "paste stays in the second tab"); - let d = &s.active().unwrap().doc; - assert_eq!(d.layers.len(), before + 1, "pasted layer added to the second document"); - let pasted = d.layers.last().unwrap().surface().unwrap(); - let cb = pasted.content_bounds(); - let (cx, cy) = (cb.x0 + cb.width() as i32 / 2, cb.y0 + cb.height() as i32 / 2); - let mut px = [[0.0f32; 4]; 1]; - pasted.read_rgba_into(Rect::new(cx, cy, cx + 1, cy + 1), &mut px); - assert!(px[0][0] > 0.9 && px[0][1] < 0.1 && px[0][2] < 0.1, "pasted content is red: {:?}", px[0]); - } - - /// #2901 (P10/P11): Paste, Paste in Place and Paste Into recomposite only where the pasted - /// layer draws, not the whole document; nothing outside that changes, and undo brings back - /// exactly the old pixels. - #[test] - fn paste_damages_only_the_pasted_layer() { - let render = |s: &Session| photocraft_compose::render(&s.active().unwrap().doc, s.active().unwrap().doc.bounds()); - for depth in [8, 16, 32] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 60, "height": 50, "depth": depth, "background": "white"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 8})).unwrap(); - s.execute("edit.fill", json!({"color": "#3366cc"})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - // Half transparent, so an opaque copy pasted in place over it shows. - s.execute("layer.setProps", json!({"opacity": 0.5})).unwrap(); - for (id, p) in [ - ("edit.paste", json!({"center": [40, 30]})), - ("edit.pasteSpecial.pasteInPlace", json!({})), - ("edit.pasteSpecial.pasteInto", json!({"center": [40, 30]})), - ] { - s.execute("select.rect", json!({"x": 36, "y": 27, "width": 6, "height": 4})).unwrap(); - let shown = render(&s); - s.execute(id, p).unwrap(); - let st = s.active().unwrap(); - let area = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); - let damage = st.last_damage.unwrap_or_else(|| panic!("{depth}-bit {id}: bounded damage")); - assert!(!damage.is_empty() && damage.intersect(&area) == damage, "{depth}-bit {id}: {damage:?} within {area:?}"); - let now = render(&s); - assert_ne!(now, shown, "{depth}-bit {id}: the paste shows"); - let w = shown.rect.width() as usize; - for (i, (a, b)) in shown.px.iter().zip(&now.px).enumerate() { - if !damage.contains((i % w) as i32, (i / w) as i32) { - assert_eq!(a, b, "{depth}-bit {id}: pixel {i} outside the damage changed"); - } - } - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(render(&s), shown, "{depth}-bit {id}: undo"); - } - } - } - - /// #1035: a `depth`-bit document with a blue-grey square copied from layer `src` (at - /// (5,5)-(15,15)) and an active layer `m` above it with a hide-all mask. - fn mask_target_session(depth: u32) -> (Session, LayerId, LayerId) { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 40, "depth": depth})).unwrap(); - let src = LayerId(s.execute("layer.new.layer", json!({"name": "src"})).unwrap()["layer"].as_u64().unwrap()); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); - s.execute("edit.fill", json!({"color": "#3366cc"})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); - let m = LayerId(s.execute("layer.new.layer", json!({"name": "m"})).unwrap()["layer"].as_u64().unwrap()); - s.execute("layer.layerMask.hideAll", json!({})).unwrap(); - (s, src, m) - } - - /// The luminosity of `src`'s pixel at (`x`, `y`): what a paste into a mask writes there. - fn luminosity(s: &Session, src: LayerId, x: i32, y: i32) -> f32 { - let p = s.active().unwrap().doc.layer(src).unwrap().surface().unwrap().rgba(x, y); - photocraft_color::convert::rgb_to_gray([p[0], p[1], p[2]]) - } - - fn mask_at(s: &Session, id: LayerId, x: i32, y: i32) -> f32 { - s.active().unwrap().doc.layer(id).unwrap().mask.as_ref().unwrap().value(x, y) - } - - #[test] - fn paste_into_a_targeted_mask_writes_the_luminosity_in_one_step() { - for depth in [8, 16, 32] { - let (mut s, src, m) = mask_target_session(depth); - let lum = luminosity(&s, src, 10, 10); - assert!(lum > 0.05 && lum < 0.95, "{depth}-bit: a mid grey ({lum})"); - let (layers, steps) = (s.active().unwrap().doc.layer_count(), s.active().unwrap().history.past_len()); - s.execute("edit.pasteSpecial.pasteInPlace", json!({"target": "mask"})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(st.doc.layer_count(), layers, "{depth}-bit: no new layer"); - assert_eq!(st.history.past_len(), steps + 1, "{depth}-bit: one history step"); - assert_eq!(st.active_layer, Some(m)); - assert!((mask_at(&s, m, 10, 10) - lum).abs() < 2.0 / 255.0, "{depth}-bit: {} vs {lum}", mask_at(&s, m, 10, 10)); - assert_eq!(mask_at(&s, m, 20, 20), 0.0, "{depth}-bit: the mask outside the paste is unchanged"); - assert!(st.doc.layer(m).unwrap().surface().unwrap().content_bounds().is_empty(), "{depth}-bit: the pixels are untouched"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(mask_at(&s, m, 10, 10), 0.0, "{depth}-bit: undo restores the mask"); - } - } - - #[test] - fn copy_with_the_mask_targeted_takes_the_mask_as_grayscale() { - for depth in [8, 16, 32] { - let (mut s, _, m) = mask_target_session(depth); - s.edit("paint mask", |doc, _| { - doc.layer_mut(m).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(6, 6, 16, 12), &[0.75]); - Ok(()) - }) - .unwrap(); - let steps = s.active().unwrap().history.past_len(); - let r = s.execute("edit.copy", json!({"target": "mask"})).unwrap(); - assert_eq!(r["bounds"], json!([0, 0, 40, 40]), "{depth}-bit: a mask copies whole, as in Photoshop"); - assert_eq!(s.active().unwrap().history.past_len(), steps, "{depth}-bit: copy adds no history step"); - let clip = s.clipboard.clone().unwrap(); - let px = clip.surface.rgba(10, 10); - assert!((px[0] - 0.75).abs() < 2.0 / 255.0, "{depth}-bit: {} vs 0.75", px[0]); - assert!(clip.surface.rgba(30, 30)[0] < 0.01, "{depth}-bit: the hidden area copies as black"); - assert!(px[3] > 0.99, "{depth}-bit: the copy is opaque"); - // Paste it into another layer's reveal-all mask: it replaces that mask (the request - // in #2928): the square, and the black around it, land. - let m2 = LayerId(s.execute("layer.new.layer", json!({"name": "m2"})).unwrap()["layer"].as_u64().unwrap()); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({"target": "mask"})).unwrap(); - assert!((mask_at(&s, m2, 10, 10) - 0.75).abs() < 2.0 / 255.0, "{depth}-bit: the copied mask lands"); - assert!(mask_at(&s, m2, 30, 30) < 0.01, "{depth}-bit: the copied black replaces the reveal-all"); - } - } - - #[test] - fn an_untouched_mask_copies_the_whole_canvas_at_its_default() { - let (mut s, _, _) = mask_target_session(8); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - let r = s.execute("edit.copy", json!({"target": "mask"})).unwrap(); - assert_eq!(r["bounds"], json!([0, 0, 40, 40]), "a plain Reveal All mask still transfers"); - assert!(s.clipboard.clone().unwrap().surface.rgba(20, 20)[0] > 0.99); - } - - #[test] - fn mask_copy_takes_only_the_selection() { - let (mut s, _, m) = mask_target_session(8); - s.edit("paint mask", |doc, _| { - doc.layer_mut(m).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(6, 6, 16, 12), &[0.75]); - Ok(()) - }) - .unwrap(); - s.execute("select.rect", json!({"x": 8, "y": 8, "width": 4, "height": 4})).unwrap(); - let r = s.execute("edit.copy", json!({"target": "mask"})).unwrap(); - assert_eq!(r["bounds"], json!([8, 8, 4, 4])); - let clip = s.clipboard.clone().unwrap(); - assert!((clip.surface.rgba(9, 9)[0] - 0.75).abs() < 2.0 / 255.0); - assert!(clip.surface.rgba(5, 5)[3] < 0.01, "nothing outside the selection"); - } - - #[test] - fn mask_copy_needs_a_mask_and_fails_gracefully() { - let mut s = Session::new(); - assert!(s.execute("edit.copy", json!({"target": "mask"})).is_err(), "no document"); - s.execute("file.new", json!({"width": 20, "height": 20})).unwrap(); - assert!(s.execute("edit.copy", json!({"target": "mask"})).is_err(), "the layer has no mask"); - assert!(s.clipboard.is_none(), "a refused copy leaves the clipboard alone"); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - assert!(s.execute("edit.copy", json!({"target": "mask"})).is_ok()); - // An adjustment layer has no pixels of its own: Copy is only enabled while its mask is - // targeted (#2928). - s.execute("file.new", json!({"width": 20, "height": 20})).unwrap(); - let a = s.execute("layer.newAdjustmentLayer.levels", json!({})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.select", json!({"layer": a})).unwrap(); - assert!(!s.is_enabled("edit.copy"), "no pixels to copy"); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - assert!(s.is_enabled_with("edit.copy", &json!({"target": "mask"})), "the mask can be copied"); - } - - #[test] - fn a_copied_mask_travels_to_another_document() { - let (mut s, _, m) = mask_target_session(8); - s.edit("paint mask", |doc, _| { - doc.layer_mut(m).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(6, 6, 16, 12), &[0.75]); - Ok(()) - }) - .unwrap(); - s.execute("edit.copy", json!({"target": "mask"})).unwrap(); - s.execute("file.new", json!({"width": 40, "height": 40})).unwrap(); - let n = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({"target": "mask"})).unwrap(); - assert!((mask_at(&s, n, 10, 10) - 0.75).abs() < 2.0 / 255.0, "the mask arrived in the second document"); - assert!(mask_at(&s, n, 30, 30) < 0.01, "it replaced the reveal-all mask there"); - } - - #[test] - fn paste_follows_the_mask_view_and_is_placed_like_a_paste() { - let (mut s, src, m) = mask_target_session(8); - let lum = luminosity(&s, src, 10, 10); - // ⌥-click the mask thumbnail: the canvas shows the mask, and pasting targets it. - s.execute(crate::mask_view_cmds::ID, json!({"layer": m.0, "mode": "gray"})).unwrap(); - let r = s.execute("edit.paste", json!({"center": [30, 30]})).unwrap(); - assert_eq!(r["offset"], json!([20, 20])); - assert!((mask_at(&s, m, 30, 30) - lum).abs() < 2.0 / 255.0); - assert_eq!(mask_at(&s, m, 10, 10), 0.0); - assert_eq!(s.active().unwrap().doc.layers.len(), 3, "Background, src, m"); - } - - #[test] - fn paste_into_and_outside_a_targeted_mask_keep_to_the_selection() { - let (mut s, src, m) = mask_target_session(16); - let lum = luminosity(&s, src, 10, 10); - // The paste is centred on the selection, so lands in place: only the selection takes it. - s.execute("select.rect", json!({"x": 8, "y": 8, "width": 4, "height": 4})).unwrap(); - s.execute("edit.pasteSpecial.pasteInto", json!({"target": "mask"})).unwrap(); - assert!((mask_at(&s, m, 9, 9) - lum).abs() < 2.0 / 255.0); - assert_eq!(mask_at(&s, m, 7, 7), 0.0, "pasted, but outside the selection"); - assert!(s.active().unwrap().doc.selection.is_none()); - assert_eq!(s.active().unwrap().doc.layers.len(), 3, "no new layer"); - s.execute("layer.layerMask.hideAll", json!({})).unwrap(); - s.execute("select.rect", json!({"x": 8, "y": 8, "width": 4, "height": 4})).unwrap(); - s.execute("edit.pasteSpecial.pasteOutside", json!({"target": "mask"})).unwrap(); - assert_eq!(mask_at(&s, m, 9, 9), 0.0, "Paste Outside leaves the selection alone"); - assert!((mask_at(&s, m, 7, 7) - lum).abs() < 2.0 / 255.0); - } - - #[test] - fn paste_follows_a_targeted_alpha_channel() { - let (mut s, src, _) = mask_target_session(8); - let lum = luminosity(&s, src, 10, 10); - s.execute("channel.new", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({"target": {"channel": 0}})).unwrap(); - let c = &s.active().unwrap().doc.channels[0].surface; - assert!((c.sample_channel(10, 10, 0) - lum).abs() < 2.0 / 255.0); - assert_eq!(s.active().unwrap().doc.layers.len(), 3, "no new layer"); - } - - #[test] - fn paste_into_a_missing_mask_or_channel_is_an_error() { - let (mut s, _, m) = mask_target_session(8); - s.edit("drop mask", |doc, _| { - doc.layer_mut(m).unwrap().mask = None; - Ok(()) - }) - .unwrap(); - let layers = s.active().unwrap().doc.layer_count(); - for target in [json!("mask"), json!({"channel": 7}), json!("quickMask")] { - for id in ["edit.paste", "edit.pasteSpecial.pasteInPlace"] { - assert!(s.execute(id, json!({ "target": target })).is_err(), "{id} {target}"); - } - } - assert_eq!(s.active().unwrap().doc.layer_count(), layers, "nothing pasted as a layer"); - } - - #[test] - fn paste_between_different_color_modes() { - // Copy from an RGB document, paste into a Grayscale one: the clipboard converts to the - // target document's format (a realistic between-tabs case that must not panic). - let mut s = session(); // RGB, red rect - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 40, "height": 20})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("file.new", json!({"width": 120, "height": 120, "mode": "gray"})).unwrap(); - s.execute("edit.paste", json!({})).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.mode, photocraft_doc::ColorMode::Grayscale); - let pasted = d.layers.last().unwrap().surface().unwrap(); - assert_eq!(pasted.format().mode, photocraft_color::ColorMode::Grayscale, "pasted layer is grayscale"); - assert!(!pasted.content_bounds().is_empty(), "pasted content exists"); - } - - #[test] - fn transform_scale_grows_content() { - let mut s = session(); // red rect (10,10)-(50,30): 40x20 - s.execute("edit.transform", json!({"matrix": [2, 0, 0, 2, 0, 0]})).unwrap(); - let d = &s.active().unwrap().doc; - let cb = d.layers.last().unwrap().surface().unwrap().content_bounds(); - // Scaled ~2x about the origin (≈ 80x40 at ≈ (20,20)); allow ~1px bilinear edge bleed. - assert!((cb.x0 - 20).abs() <= 2 && (cb.y0 - 20).abs() <= 2, "origin ~ (20,20): {cb:?}"); - assert!((cb.width() as i32 - 80).abs() <= 3 && (cb.height() as i32 - 40).abs() <= 3, "size ~ 80x40: {cb:?}"); - } - - #[test] - fn cut_and_clear_on_the_background_fill_with_the_background_colour() { - // The Background keeps its transparency lock: cutting fills with the background colour. - let fresh = || { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); - s.tools.background = [1.0, 0.0, 0.0, 1.0]; - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); - s - }; - let px = |s: &Session, i: usize, x: i32, y: i32| s.active().unwrap().doc.layers[i].surface().unwrap().pixel(x, y); - for cmd in ["edit.cut", "edit.clear", "layer.new.layerViaCut"] { - let mut s = fresh(); - s.execute(cmd, json!({})).unwrap(); - let d = &s.active().unwrap().doc; - assert!(d.is_background(d.layers[0].id), "{cmd}: still the Background"); - assert_eq!(px(&s, 0, 8, 8), vec![1.0, 0.0, 0.0, 1.0], "{cmd}: cut area is the background colour"); - assert_eq!(px(&s, 0, 30, 20), vec![1.0, 1.0, 1.0, 1.0], "{cmd}: outside untouched"); - } - // Layer via Cut still lifts the original pixels onto the new layer. - let mut s = fresh(); - s.execute("layer.new.layerViaCut", json!({})).unwrap(); - assert_eq!(px(&s, 1, 8, 8), vec![1.0, 1.0, 1.0, 1.0]); - // Once it's a normal layer, cutting makes transparency again. - let mut s = fresh(); - s.execute("layer.new.layerFromBackground", json!({})).unwrap(); - s.execute("edit.clear", json!({})).unwrap(); - assert_eq!(px(&s, 0, 8, 8)[3], 0.0); - } - - #[test] - fn layer_via_copy_of_a_background_keeps_colour_and_partial_selection() { - // #668: lift reads only the selected area now; the result must be what converting the - // whole layer gave: the colour, with the selection's coverage as alpha. - for depth in [8, 16, 32] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30, "depth": depth, "background": "#336699"})).unwrap(); - s.edit("partial selection", |doc, _| { - let mut m = photocraft_raster::Surface::new(PixelFormat::GRAY8); - m.fill_rect(Rect::new(5, 5, 15, 15), &[1.0]); - m.fill_rect(Rect::new(15, 5, 20, 15), &[0.5]); - doc.selection = Some(m); - Ok(()) - }) - .unwrap(); - let id = s.execute("layer.new.layerViaCopy", json!({})).unwrap()["layer"].as_u64().unwrap(); - let d = s.active().unwrap(); - let surf = d.doc.layer(LayerId(id)).unwrap().surface().unwrap(); - assert!(surf.format().alpha, "@{depth}"); - let full = surf.read_region(Rect::new(6, 6, 7, 7)); - let half = surf.read_region(Rect::new(16, 6, 17, 7)); - for (got, want) in full.iter().zip([0.2, 0.4, 0.6, 1.0]) { - assert!((got - want).abs() < 0.01, "@{depth}: {full:?}"); - } - assert!((half[3] - 0.5).abs() < 0.01 && (half[0] - 0.2).abs() < 0.01, "@{depth}: {half:?}"); - assert_eq!(surf.content_bounds(), Rect::new(5, 5, 20, 15), "@{depth}"); - } - } - - #[test] - fn cut_clears_and_layer_via_copy_cut() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 20})).unwrap(); - s.execute("edit.cut", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(20, 10, 50, 30)); - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 20})).unwrap(); - s.execute("layer.new.layerViaCut", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 20, 30)); - let d = &s.active().unwrap().doc; - assert_eq!(d.layers[1].surface().unwrap().content_bounds(), Rect::new(20, 10, 50, 30)); - // Without a selection ⌘J duplicates the layer. - let mut s = session(); - s.execute("layer.new.layerViaCopy", json!({})).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.layers.len(), 3); - assert!(d.layers[2].name.ends_with("copy")); - } - - /// #1096: Layer via Copy and Cut deselect, as Photoshop does, so a following Free Transform - /// moves the whole new layer; one undo brings back both the selection and the old layers. - #[test] - fn layer_via_copy_and_cut_deselect_in_one_step() { - for cmd in ["layer.new.layerViaCopy", "layer.new.layerViaCut"] { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 20})).unwrap(); - let (layers, steps) = (s.active().unwrap().doc.layers.len(), s.active().unwrap().history.past_len()); - s.execute(cmd, json!({})).unwrap(); - let st = s.active().unwrap(); - assert!(st.doc.selection.is_none(), "{cmd}: deselected"); - assert_eq!((st.doc.layers.len(), st.history.past_len()), (layers + 1, steps + 1), "{cmd}"); - assert!(s.undo()); - let st = s.active().unwrap(); - assert_eq!(st.doc.layers.len(), layers, "{cmd}: undo removes the layer"); - assert!(st.doc.selection.is_some(), "{cmd}: and restores the selection"); - } - } - - /// #2777: with several layers selected and no pixel selection, Layer via Copy (Cmd+J) copies - /// every selected layer in one step, each copy above its original and the copies selected. - /// With a pixel selection it still copies the active layer's selected pixels only. - #[test] - fn layer_via_copy_duplicates_every_selected_layer() { - let mut s = session(); - let a = s.active().unwrap().active_layer.unwrap(); - let b = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": b.0, "mode": "toggle"})).unwrap(); - let steps = s.active().unwrap().history.past_len(); - s.execute("layer.new.layerViaCopy", json!({})).unwrap(); - let st = s.active().unwrap(); - let ids: Vec = st.doc.layers.iter().map(|l| l.id).collect(); - assert_eq!(ids.len(), 5, "background, a, a copy, b, b copy"); - assert_eq!((ids[1], ids[3]), (a, b), "each copy sits above its original"); - let mut selected = st.selected_layers(); - selected.sort_by_key(|l| l.0); - assert_eq!(selected, vec![ids[2], ids[4]], "the copies become the selection"); - assert_eq!(st.history.past_len(), steps + 1, "one undo step"); - assert!(s.undo()); - s.execute("layer.select", json!({"layer": b.0})).unwrap(); - s.execute("layer.select", json!({"layer": a.0, "mode": "toggle"})).unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("layer.new.layerViaCopy", json!({})).unwrap(); - assert_eq!(s.active().unwrap().doc.layers.len(), 4, "a pixel selection copies one layer's pixels"); - } - - fn active_content(s: &Session) -> LayerContent { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().content.clone() - } - - /// #541, #2428: with no selection, Copy takes a smart object whole and Paste makes a smart - /// object again, with contents of its own; with a selection, the selected pixels paste as - /// pixels. Cut edits pixels, so it's refused; Layer via Copy and Duplicate Layer copy the layer. - #[test] - fn copy_a_smart_object_layer() { - let mut s = session(); - s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); - let LayerContent::Smart(original) = active_content(&s) else { panic!("smart object") }; - assert!(s.is_enabled("edit.copy")); - assert_eq!(s.execute("edit.copy", json!({})).unwrap()["bounds"], json!([10, 10, 40, 20]), "no selection: the whole layer"); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - let LayerContent::Smart(pasted) = active_content(&s) else { panic!("the paste is a smart object (#2428)") }; - assert_eq!(pasted.source, original.source, "the same contents"); - assert_ne!(pasted.contents_id, original.contents_id, "but not linked to the copied one"); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 50, 30)); - s.execute("edit.paste", json!({"center": [70, 70]})).unwrap(); - assert!(matches!(active_content(&s), LayerContent::Smart(_))); - assert_eq!(active_bounds(&s), Rect::new(50, 60, 90, 80), "placed as pasted pixels are"); - s.undo(); - s.undo(); - assert!(matches!(active_content(&s), LayerContent::Smart(_))); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); - assert_eq!(s.execute("edit.copy", json!({})).unwrap()["bounds"], json!([10, 10, 10, 10]), "only the selected part"); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - assert!(matches!(active_content(&s), LayerContent::Raster(_)), "a selection copies pixels"); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 20, 20)); - s.undo(); - let cut = s.execute("edit.cut", json!({})).unwrap_err().to_string(); - assert!(cut.contains("not directly editable"), "{cut}"); - assert!(matches!(active_content(&s), LayerContent::Smart(_))); - s.execute("select.deselect", json!({})).unwrap(); - s.execute("layer.new.layerViaCopy", json!({})).unwrap(); - assert!(matches!(active_content(&s), LayerContent::Smart(_)), "⌘J without a selection duplicates the smart object"); - s.execute("layer.duplicate", json!({})).unwrap(); - assert!(matches!(active_content(&s), LayerContent::Smart(_))); - assert_eq!(s.active().unwrap().doc.layers.len(), 4); - } - - /// #2478: with several layers selected and no pixel selection, Copy takes every one of them - /// and Paste recreates them all, in order, as one undo step; with a pixel selection Copy still - /// takes the active layer's selected pixels only. - #[test] - fn copy_and_paste_several_selected_layers() { - let mut s = session(); - let a = s.active().unwrap().active_layer.unwrap(); - let b = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.edit("paint", |doc, _| { - doc.layer_mut(b).unwrap().surface_mut().unwrap().fill_rect(Rect::new(60, 60, 80, 90), &[0.0, 0.0, 1.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": b.0, "mode": "toggle"})).unwrap(); - assert!(s.is_enabled("edit.copy")); - assert_eq!(s.execute("edit.copy", json!({})).unwrap()["bounds"], json!([10, 10, 70, 80]), "both layers' pixels"); - let before = s.active().unwrap().doc.layers.len(); - let steps = s.active().unwrap().history.past_len(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(st.doc.layers.len(), before + 2, "every selected layer is pasted"); - assert_eq!(st.history.past_len(), steps + 1, "one undo step"); - let n = st.doc.layers.len(); - let (pa, pb) = (st.doc.layers[n - 2].surface().unwrap(), st.doc.layers[n - 1].surface().unwrap()); - assert_eq!(pa.content_bounds(), Rect::new(10, 10, 50, 30), "the lower layer first"); - assert_eq!(pb.content_bounds(), Rect::new(60, 60, 80, 90)); - assert!(s.undo()); - assert_eq!(s.active().unwrap().doc.layers.len(), before); - - // A pixel selection takes the active layer's selected pixels, as a single layer. - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 30, "height": 30})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - assert_eq!(s.active().unwrap().doc.layers.len(), before + 1); - } - - /// #2478, #3588: several layers copied in one document paste into another, in that document's - /// bit depth and with their own pixels, in order, as one undo step. - #[test] - fn paste_several_layers_into_another_document() { - for depth in [16, 32] { - let mut s = session(); - let a = s.active().unwrap().active_layer.unwrap(); - let b = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.edit("paint", |doc, _| { - doc.layer_mut(b).unwrap().surface_mut().unwrap().fill_rect(Rect::new(60, 60, 80, 90), &[0.0, 0.0, 1.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": b.0, "mode": "toggle"})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - - s.execute("file.new", json!({"width": 120, "height": 120, "depth": depth})).unwrap(); - assert_eq!(s.active_index().unwrap(), 1, "the new document is the active tab"); - let (before, steps) = (s.active().unwrap().doc.layers.len(), s.active().unwrap().history.past_len()); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(st.doc.layers.len(), before + 2, "{depth}-bit: both layers arrive"); - assert_eq!(st.history.past_len(), steps + 1, "{depth}-bit: one undo step"); - let n = st.doc.layers.len(); - let (pa, pb) = (st.doc.layers[n - 2].surface().unwrap(), st.doc.layers[n - 1].surface().unwrap()); - assert_eq!(pa.format().sample, st.doc.depth, "{depth}-bit: converted to the target depth"); - assert_eq!(pb.format().sample, st.doc.depth, "{depth}-bit: converted to the target depth"); - assert_eq!(pa.content_bounds(), Rect::new(10, 10, 50, 30), "{depth}-bit: the lower layer first"); - assert_eq!(pb.content_bounds(), Rect::new(60, 60, 80, 90)); - let (mut red, mut blue) = ([[0.0f32; 4]; 1], [[0.0f32; 4]; 1]); - pa.read_rgba_into(Rect::new(20, 20, 21, 21), &mut red); - pb.read_rgba_into(Rect::new(70, 70, 71, 71), &mut blue); - assert!(red[0][0] > 0.9 && red[0][1] < 0.1 && red[0][2] < 0.1, "{depth}-bit: red kept: {:?}", red[0]); - assert!(blue[0][2] > 0.9 && blue[0][0] < 0.1 && blue[0][1] < 0.1, "{depth}-bit: blue kept: {:?}", blue[0]); - assert!(s.undo()); - assert_eq!(s.active().unwrap().doc.layers.len(), before, "{depth}-bit: undo removes both"); - assert_eq!(s.documents().len(), 2, "the source document is untouched"); - } - } - - /// #2478: the Background in a multi-layer copy pastes as an ordinary, unlocked layer. - #[test] - fn copying_the_background_with_another_layer_pastes_an_ordinary_layer() { - let mut s = session(); - let a = s.active().unwrap().active_layer.unwrap(); - let bg = s.active().unwrap().doc.layers[0].id; - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": bg.0, "mode": "toggle"})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(st.doc.layers.len(), 4); - let pasted = &st.doc.layers[st.doc.layers.len() - 2]; - assert!(!st.doc.is_background(pasted.id)); - assert_eq!(pasted.locks, Default::default()); - assert!(st.doc.is_background(st.doc.layers[0].id), "the real Background is untouched"); - } - - /// #2428: a smart object copied in one document pastes into another as a smart object, in - /// that document's bit depth. - #[test] - fn paste_a_smart_object_into_another_document() { - let mut s = session(); - s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("file.new", json!({"width": 60, "height": 40, "depth": 16})).unwrap(); - s.execute("edit.paste", json!({})).unwrap(); - let LayerContent::Smart(sm) = active_content(&s) else { panic!("the paste is a smart object") }; - assert_eq!(sm.cache.map(|c| c.format().sample), Some(s.active().unwrap().doc.depth), "converted to 16 bits"); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 50, 30), "on the canvas: in place"); - } - - /// #3584: a PSD-embedded linked smart object (its bytes in the document's `lnk2` block, its - /// source a uuid) can still be opened after Copy + Paste into another document. - #[test] - fn paste_keeps_a_psd_embedded_smart_object_openable() { - use std::sync::Arc; - let mut s = session(); - s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); - let outer = s.active().unwrap().active_layer.unwrap(); - s.edit("embedded linked source", |doc, _| { - let LayerContent::Smart(sm) = &mut doc.layer_mut(outer).ok_or(EngineError::NoDocument)?.content else { - return Err(EngineError::Other("not a smart object".into())); - }; - sm.source = photocraft_doc::SmartSource::Linked { path: "uuid-1".into() }; - let linked = photocraft_io::linked::LinkedFile { uuid: "uuid-1".into(), file_name: "in.png".into(), bytes: vec![1, 2, 3, 4] }; - doc.metadata.psd_global_blocks.push((*b"8BIM", *b"lnk2", Arc::new(photocraft_io::linked::encode_linked_file(&linked)))); - Ok(()) - }) - .unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); - s.execute("edit.paste", json!({})).unwrap(); - let LayerContent::Smart(sm) = active_content(&s) else { panic!("the paste is a smart object") }; - assert!( - crate::smart_cmds::source_bytes(&s.active().unwrap().doc.metadata, &sm.source).is_some(), - "the pasted PSD-embedded linked source must resolve (#3584)" - ); - } - - /// Type and shape layers copy their rendered pixels too; groups and adjustments have none. - #[test] - fn copy_reads_any_layer_that_shows_pixels() { - let mut s = session(); - s.execute("type.create", json!({"text": "Hi", "size": 30, "x": 20, "y": 60})).unwrap(); - assert!(!s.is_enabled("edit.cut")); - assert!(s.execute("edit.copy", json!({})).is_ok()); - s.execute("layer.groupLayers", json!({})).unwrap(); - assert!(s.is_enabled("edit.copy"), "no selection: the group is copied whole (#2670)"); - s.execute("select.all", json!({})).unwrap(); - assert!(!s.is_enabled("edit.copy")); - } - - /// #2670: with no selection, Copy takes a type layer, a shape layer or a group whole and - /// Paste recreates it, into another document too (with fresh ids, in its bit depth); with a - /// selection, Copy takes pixels. - #[test] - fn copy_paste_keeps_type_shape_and_group_layers() { - let mut s = session(); - s.execute("type.create", json!({"text": "Hi", "size": 30, "x": 20, "y": 60})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("edit.pasteSpecial.pasteInPlace", json!({})).unwrap(); - let LayerContent::Text(t) = active_content(&s) else { panic!("the paste is a type layer") }; - assert_eq!(t.text, "Hi"); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 100, "height": 100})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("edit.paste", json!({})).unwrap(); - assert!(matches!(active_content(&s), LayerContent::Raster(_)), "a selection copies pixels"); - s.execute("shape.create", json!({"kind": "rect", "rect": [10, 10, 40, 20], "fill": "#ff0000"})).unwrap(); - s.execute("layer.groupLayers", json!({})).unwrap(); - let LayerContent::Group(src) = active_content(&s) else { panic!("a group") }; - s.execute("edit.copy", json!({})).unwrap(); - s.execute("file.new", json!({"width": 60, "height": 40, "depth": 16})).unwrap(); - s.execute("edit.paste", json!({})).unwrap(); - let LayerContent::Group(g) = active_content(&s) else { panic!("the paste is a group") }; - let [shape] = g.children.as_slice() else { panic!("one child") }; - assert!(matches!(shape.content, LayerContent::Shape(_)), "the shape stays a shape"); - assert_ne!(shape.id, src.children[0].id, "fresh ids"); - assert_eq!(shape.surface().map(|c| c.format().sample), Some(s.active().unwrap().doc.depth), "converted to 16 bits"); - } - - #[test] - fn merge_visible_keeps_hidden_layers() { - let mut s = session(); - s.execute("layer.new.layer", json!({})).unwrap(); - let hidden = s.active().unwrap().active_layer.unwrap(); - s.execute("layer.setProps", json!({"layer": hidden.0, "visible": false})).unwrap(); - s.execute("layer.mergeVisible", json!({})).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.layers.len(), 2); - assert_eq!(d.layers[0].name, "Background"); - assert_eq!(d.layers[1].id, hidden); - assert_eq!(d.layers[0].surface().unwrap().pixel(20, 20)[..3], [1.0, 0.0, 0.0]); - } - - #[test] - fn auto_tone_stretches_a_low_contrast_layer() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 64})).unwrap(); - s.edit("grey ramp", |doc, _| { - let surf = doc.layers[0].surface_mut().unwrap(); - for x in 0..64 { - let v = 0.3 + 0.4 * x as f32 / 63.0; - surf.fill_rect(Rect::new(x, 0, x + 1, 64), &[v, v * 0.9, v * 0.8, 1.0]); - } - Ok(()) - }) - .unwrap(); - for cmd in ["image.autoTone", "image.autoContrast", "image.autoColor"] { - s.execute(cmd, json!({})).unwrap(); - let d = &s.active().unwrap().doc; - let (a, b) = (d.layers[0].surface().unwrap().pixel(0, 0), d.layers[0].surface().unwrap().pixel(63, 0)); - // Auto Contrast keeps colour (shared points): only the extreme channels reach 0 and 1. - let (lo, hi) = if cmd == "image.autoContrast" { (a[2], b[0]) } else { (a[0], b[0]) }; - assert!(lo < 0.05 && hi > 0.95, "{cmd}: {a:?} {b:?}"); - s.undo(); - } - } - - /// #352: ⌥⇧⌘T steps and repeats: each press adds a copy one transform further on, and each - /// is one undo step. - #[test] - fn transform_again_on_a_copy_steps_and_repeats() { - let mut s = session(); - assert!(s.execute("edit.transform.againCopy", json!({})).is_err(), "nothing to repeat yet"); - assert_eq!(s.active().unwrap().doc.layers.len(), 2, "a refusal leaves no copy"); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 10, 0]})).unwrap(); - let steps = s.active().unwrap().history.past_len(); - for (i, x) in [30, 40].into_iter().enumerate() { - s.execute("edit.transform.againCopy", json!({})).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.layers.len(), 3 + i); - assert_eq!(active_bounds(&s), Rect::new(x, 10, x + 40, 30), "the copy moved one step further"); - assert_eq!(s.active().unwrap().history.past_len(), steps + 1 + i, "one history step per press"); - } - assert_eq!(s.active().unwrap().history.undo_label(), Some("Transform Again")); - // The original and the first copy stay where they were. - let d = &s.active().unwrap().doc; - let b = |i: usize| d.layers[i].surface().unwrap().content_bounds(); - assert_eq!((b(1), b(2)), (Rect::new(20, 10, 60, 30), Rect::new(30, 10, 70, 30))); - // Undo removes the copy and its move together. - s.undo(); - assert_eq!(s.active().unwrap().doc.layers.len(), 3); - assert_eq!(active_bounds(&s), Rect::new(30, 10, 70, 30)); - // A copy that can't move (position locked) is taken back. - let id = s.active().unwrap().active_layer.unwrap(); - s.execute("layer.setProps", json!({"layer": id.0, "locks": {"position": true}})).unwrap(); - let layers = s.active().unwrap().doc.layers.len(); - assert!(s.execute("edit.transform.againCopy", json!({})).unwrap_err().to_string().contains("locked")); - assert_eq!(s.active().unwrap().doc.layers.len(), layers); - assert!(!s.active().unwrap().history.can_redo(), "nothing to redo"); - } - - #[test] - fn toggle_last_state_and_transform_again() { - let mut s = session(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 10, 0]})).unwrap(); - s.execute("edit.transform.again", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(30, 10, 70, 30)); - s.execute("edit.toggleLastState", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(20, 10, 60, 30)); - s.execute("edit.toggleLastState", json!({})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(30, 10, 70, 30)); - } - - #[test] - fn guides_are_undoable() { - let mut s = session(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 40})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "horizontal", "position": 12.5})).unwrap(); - s.execute("view.moveGuide", json!({"orientation": "vertical", "index": 0, "position": 60})).unwrap(); - let g = &s.active().unwrap().doc.guides; - assert_eq!((g.vertical.clone(), g.horizontal.clone()), (vec![60.0], vec![12.5])); - s.execute("view.deleteGuide", json!({"orientation": "horizontal", "index": 0})).unwrap(); - s.undo(); - assert_eq!(s.active().unwrap().doc.guides.horizontal, vec![12.5]); - s.execute("view.clearGuides", json!({})).unwrap(); - assert!(s.active().unwrap().doc.guides.vertical.is_empty()); - assert!(s.execute("view.moveGuide", json!({"orientation": "vertical", "index": 3, "position": 1})).is_err()); - } - - /// #2955: the rulers' zero point is set and reset by command, undoable, and bad params are - /// refused without touching the document. - #[test] - fn ruler_origin_is_set_reset_and_undoable() { - let mut s = session(); - assert_eq!(s.active().unwrap().doc.ruler_origin, [0.0, 0.0]); - assert_eq!(s.execute("view.setRulerOrigin", json!({"x": 40, "y": 12.5})).unwrap(), json!({"x": 40.0, "y": 12.5})); - assert_eq!(s.active().unwrap().doc.ruler_origin, [40.0, 12.5]); - s.execute("view.resetRulerOrigin", json!({})).unwrap(); - assert_eq!(s.active().unwrap().doc.ruler_origin, [0.0, 0.0]); - s.undo(); - assert_eq!(s.active().unwrap().doc.ruler_origin, [40.0, 12.5]); - for bad in [json!({}), json!({"x": 1}), json!({"x": "a", "y": 2}), json!({"x": 1e300, "y": 0}), json!({"x": -2e7, "y": 0}), json!(null), json!([1, 2])] - { - assert!(s.execute("view.setRulerOrigin", bad.clone()).is_err(), "{bad} is refused"); - } - assert_eq!(s.active().unwrap().doc.ruler_origin, [40.0, 12.5]); - let mut empty = Session::new(); - assert!(empty.execute("view.setRulerOrigin", json!({"x": 1, "y": 2})).is_err()); - assert!(empty.execute("view.resetRulerOrigin", json!({})).is_err()); - } - - /// The last guide added in `orientation` for `position`. - fn new_guide(s: &mut Session, orientation: &str, position: Value) -> f64 { - s.execute("view.newGuide", json!({"orientation": orientation, "position": position})).unwrap(); - let g = &s.active().unwrap().doc.guides; - *if orientation == "vertical" { &g.vertical } else { &g.horizontal }.last().unwrap() - } - - #[test] - fn guide_positions_take_photoshop_units() { - let mut s = Session::new(); - // 4 × 2 inches at 100 ppi. - s.execute("file.new", json!({"width": 400, "height": 200, "resolution": 100})).unwrap(); - // Percent follows the guide's own axis: the width for a vertical guide, the height for a horizontal one. - assert_eq!(new_guide(&mut s, "vertical", json!("50%")), 200.0); - assert_eq!(new_guide(&mut s, "horizontal", json!("50%")), 100.0); - for one_inch in ["1in", "1 in", "2.54cm", "25.4 mm", "72pt", "6pc", "100px", "100", "1,0 inch"] { - assert!((new_guide(&mut s, "vertical", json!(one_inch)) - 100.0).abs() < 1e-3, "{one_inch}"); - } - // Fractional and negative positions are kept; a plain number is pixels, as before. - assert_eq!(new_guide(&mut s, "horizontal", json!("12.5px")), 12.5); - assert_eq!(new_guide(&mut s, "vertical", json!("-10px")), -10.0); - assert_eq!(new_guide(&mut s, "vertical", json!(40)), 40.0); - // Points and picas follow Units & Rulers › Point Size. - s.execute("prefs.set", json!({"path": "unitsAndRulers.pointSize", "value": "traditional"})).unwrap(); - assert!((new_guide(&mut s, "vertical", json!("72.27pt")) - 100.0).abs() < 1e-3); - // A guide moved with a unit lands where a new one would. - s.execute("view.moveGuide", json!({"orientation": "horizontal", "index": 0, "position": "25%"})).unwrap(); - assert_eq!(s.active().unwrap().doc.guides.horizontal[0], 50.0); - } - - #[test] - fn new_guides_keep_their_own_colour_through_moves_deletes_and_undo() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200})).unwrap(); - let color = |s: &Session, vertical: bool, p: f64| s.active().unwrap().doc.guides.color(vertical, p); - // Without a colour a guide uses the preference colour, as ruler-drag guides always did. - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 10})).unwrap(); - assert_eq!(color(&s, true, 10.0), None); - assert!(s.active().unwrap().doc.guides.colors.is_empty()); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 20, "color": "magenta"})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "horizontal", "position": 20, "color": "#123456"})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 30, "color": "LightBlue"})).unwrap(); - assert_eq!(color(&s, true, 20.0), Some([0xff, 0x00, 0xff])); - assert_eq!(color(&s, false, 20.0), Some([0x12, 0x34, 0x56]), "the same position across is a different guide"); - assert_eq!(color(&s, true, 30.0), Some([0x33, 0x66, 0xff]), "names are case-insensitive"); - // Moving takes the colour along. - s.execute("view.moveGuide", json!({"orientation": "vertical", "index": 1, "position": 25})).unwrap(); - assert_eq!((color(&s, true, 20.0), color(&s, true, 25.0)), (None, Some([0xff, 0x00, 0xff]))); - // Undo restores it where it was. - s.undo(); - assert_eq!((color(&s, true, 20.0), color(&s, true, 25.0)), (Some([0xff, 0x00, 0xff]), None)); - // Deleting drops it; clearing drops them all. - s.execute("view.deleteGuide", json!({"orientation": "vertical", "index": 2})).unwrap(); - assert_eq!(color(&s, true, 30.0), None); - assert_eq!(s.active().unwrap().doc.guides.colors.len(), 2); - s.execute("view.clearGuides", json!({})).unwrap(); - assert!(s.active().unwrap().doc.guides.colors.is_empty()); - } - - #[test] - fn moving_one_of_two_coloured_guides_at_one_position_leaves_the_other_its_colour() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200})).unwrap(); - for _ in 0..2 { - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 50, "color": "green"})).unwrap(); - } - s.execute("view.moveGuide", json!({"orientation": "vertical", "index": 0, "position": 60})).unwrap(); - let g = &s.active().unwrap().doc.guides; - assert_eq!((g.color(true, 50.0), g.color(true, 60.0)), (Some([0x33, 0xff, 0x33]), Some([0x33, 0xff, 0x33]))); - } - - #[test] - fn cyan_is_the_default_guide_colour_so_ruler_guides_read_as_cyan() { - let default = crate::prefs::Preferences::default().guides_grid_and_slices.guide_color; - let cyan = GUIDE_COLORS.iter().find(|(name, ..)| *name == "cyan").map(|(.., rgb)| *rgb); - assert_eq!(cyan, crate::prefs::parse_hex(&default)); - } - - #[test] - fn bad_guide_colours_are_refused_without_a_guide() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200})).unwrap(); - let undo_before = s.active().unwrap().history.can_undo(); - for bad in [json!("puce"), json!("#12345"), json!("#gggggg"), json!(""), json!(7), json!(null), json!(["cyan"])] { - assert!(s.execute("view.newGuide", json!({"orientation": "vertical", "position": 10, "color": bad.clone()})).is_err(), "{bad}"); - } - let g = &s.active().unwrap().doc.guides; - assert!(g.vertical.is_empty() && g.colors.is_empty()); - assert_eq!(s.active().unwrap().history.can_undo(), undo_before); - } - - #[test] - fn editing_a_guide_changes_what_is_given_in_one_undoable_step() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 10})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 20, "color": "magenta"})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 30})).unwrap(); - let guides = |s: &Session| s.active().unwrap().doc.guides.clone(); - // Position only: the guide keeps its index and its own colour. - s.execute("view.editGuide", json!({"orientation": "vertical", "index": 1, "position": "50%"})).unwrap(); - let g = guides(&s); - assert_eq!(g.vertical, vec![10.0, 200.0, 30.0]); - assert_eq!((g.color(true, 20.0), g.color(true, 200.0)), (None, Some([0xff, 0x00, 0xff]))); - // Colour only: an uncoloured guide gets one; the position stays. - s.execute("view.editGuide", json!({"orientation": "vertical", "index": 0, "color": "#123456"})).unwrap(); - assert_eq!(guides(&s).color(true, 10.0), Some([0x12, 0x34, 0x56])); - // Turning it: % is of the new orientation's extent, and the colour goes along. - s.execute("view.editGuide", json!({"orientation": "vertical", "index": 1, "newOrientation": "horizontal", "position": "25%"})).unwrap(); - let g = guides(&s); - assert_eq!((g.vertical.clone(), g.horizontal.clone()), (vec![10.0, 30.0], vec![50.0])); - assert_eq!((g.color(true, 200.0), g.color(false, 50.0)), (None, Some([0xff, 0x00, 0xff]))); - // One step each: undo restores the vertical guide with its colour. - s.undo(); - let g = guides(&s); - assert_eq!((g.vertical.clone(), g.horizontal.is_empty()), (vec![10.0, 200.0, 30.0], true)); - assert_eq!(g.color(true, 200.0), Some([0xff, 0x00, 0xff])); - } - - #[test] - fn bad_guide_edits_are_refused_without_a_history_step() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": 10, "color": "green"})).unwrap(); - let before = s.active().unwrap().doc.guides.clone(); - for bad in [ - json!({}), - json!({"orientation": "vertical"}), - json!({"orientation": "vertical", "index": 1}), - json!({"orientation": "horizontal", "index": 0}), - json!({"orientation": "vertical", "index": -1}), - json!({"orientation": "vertical", "index": "0"}), - json!({"orientation": "vertical", "index": u64::MAX}), - json!({"orientation": "diagonal", "index": 0}), - json!({"orientation": "vertical", "index": 0, "newOrientation": 3}), - json!({"orientation": "vertical", "index": 0, "position": "far"}), - json!({"orientation": "vertical", "index": 0, "position": 1e12}), - json!({"orientation": "vertical", "index": 0, "color": "puce"}), - ] { - assert!(s.execute("view.editGuide", bad.clone()).is_err(), "{bad}"); - } - assert_eq!(s.active().unwrap().doc.guides, before); - assert_eq!(s.active().unwrap().history.undo_label(), Some("New Guide")); - } - - #[test] - fn bad_guide_positions_are_refused_without_a_guide() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 200, "resolution": 100})).unwrap(); - let undo_before = s.active().unwrap().history.can_undo(); - for bad in [ - json!("abc"), - json!("50 furlongs"), - json!(""), - json!("%"), - json!("NaN"), - json!("inf"), - json!("1e400"), - json!("2000000px"), - json!(1e300), - json!(true), - json!([1]), - ] { - assert!(s.execute("view.newGuide", json!({"orientation": "vertical", "position": bad.clone()})).is_err(), "{bad}"); - } - assert!(s.execute("view.newGuide", json!({"orientation": "vertical"})).is_err(), "no position"); - let g = &s.active().unwrap().doc.guides; - assert!(g.vertical.is_empty() && g.horizontal.is_empty()); - assert_eq!(s.active().unwrap().history.can_undo(), undo_before, "a refused guide records no history step"); - } - - /// Guides keep their exact place (#2918): 1 cm at 300 ppi through New Guide and .pcraft, and - /// a 1/32 px PSD position past 2^19 px, where f32 has no 1/32 steps left. - #[test] - fn guides_keep_sub_pixel_positions_through_pcraft_and_psd() { - let (cm, far) = (300.0 / 2.54, 600_000.031_25); - let mut s = session(); - s.execute("view.newGuide", json!({"orientation": "horizontal", "position": cm})).unwrap(); - s.execute("view.newGuide", json!({"orientation": "vertical", "position": far})).unwrap(); - fn wide(v: impl Into) -> f64 { - v.into() - } - let exact = |g: &photocraft_doc::Guides| (wide(g.horizontal[0]), wide(g.vertical[0])); - let doc = &s.active().unwrap().doc; - assert_eq!(exact(&doc.guides), (cm, far)); - let pcraft = photocraft_format::save_to_bytes(doc, &Default::default()).unwrap(); - assert_eq!(exact(&photocraft_format::load_from_bytes(&pcraft).unwrap().guides), (cm, far)); - let psd = photocraft_io::export(doc, "psd", &Default::default()).unwrap().bytes; - let back = photocraft_io::import("g.psd", &psd).unwrap().document.guides; - assert_eq!(wide(back.vertical[0]), far, "1032 stores 1/32 px"); - } - - /// `lift` as it was before #4516: the whole area through one `f32` buffer. The oracle for - /// the tiled version. - fn lift_reference(src: &Surface, sel: Option<&Surface>, canvas: Rect) -> Clip { - let fmt = src.format(); - let with_alpha = PixelFormat::new(fmt.mode, fmt.sample, true); - let area = match sel { - Some(m) => m.content_bounds().intersect(&canvas), - None => src.content_bounds().intersect(&canvas), - }; - let mut out = Surface::new(with_alpha); - if area.is_empty() { - return Clip { surface: out, bounds: Rect::EMPTY, layers: None, resolution: None }; - } - let px = lift_region(src, sel, area, with_alpha.channels()).unwrap(); - out.try_write_region(area, &px).unwrap(); - out.prune(); - let bounds = out.content_bounds(); - Clip { surface: out, bounds, layers: None, resolution: None } - } - - fn noise(fmt: PixelFormat, r: Rect, seed: u32) -> Surface { - let mut s = Surface::new(fmt); - let n = r.width() as usize * r.height() as usize * fmt.channels(); - let data: Vec = (0..n as u32) - .map(|i| { - let mut h = seed.wrapping_mul(0x9e37_79b9) ^ i.wrapping_mul(0x85eb_ca6b); - h ^= h >> 13; - h = h.wrapping_mul(0xc2b2_ae35); - (h >> 16) as f32 / 65535.0 - }) - .collect(); - s.write_region(r, &data); - s - } - - fn assert_same_clip(a: &Clip, b: &Clip, what: &str) { - assert_eq!(a.bounds, b.bounds, "{what}: bounds"); - assert_eq!(a.surface.format(), b.surface.format(), "{what}: format"); - let r = a.bounds.union(&b.bounds); - if !r.is_empty() { - let (pa, pb) = (a.surface.read_region(r), b.surface.read_region(r)); - assert!(pa.iter().zip(&pb).all(|(x, y)| x.to_bits() == y.to_bits()), "{what}: pixels differ"); - } - } - - /// The tiled `lift` gives exactly the old result (#4516): with and without alpha, at every - /// depth, with no selection, Select All, a partial feathered selection and a selection off - /// the tile grid, and for content reaching past the canvas. - #[test] - fn tiled_lift_matches_the_whole_area_lift() { - use photocraft_color::{ColorMode, SampleType}; - let canvas = Rect::new(0, 0, 700, 600); - for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { - for alpha in [true, false] { - let fmt = PixelFormat::new(ColorMode::Rgb, sample, alpha); - let src = noise(fmt, Rect::new(-40, -30, 760, 640), 7); - let mut all = Surface::new(PixelFormat::GRAY8); - all.fill_rect(canvas, &[1.0]); - let mut partial = Surface::new(PixelFormat::GRAY8); - partial.fill_rect(Rect::new(100, 50, 600, 550), &[1.0]); - partial.write_region(Rect::new(90, 40, 610, 50), &vec![0.5; 520 * 10]); - let off_grid = noise(PixelFormat::GRAY8, Rect::new(13, 270, 333, 290), 9); - for (name, sel) in [("no selection", None), ("select all", Some(&all)), ("partial", Some(&partial)), ("off grid", Some(&off_grid))] { - let what = format!("{sample:?} alpha {alpha} {name}"); - let new = lift(&src, sel, canvas).unwrap(); - assert_same_clip(&new, &lift_reference(&src, sel, canvas), &what); - } - } - } - } - - /// Copy shares the layer's whole tiles instead of copying them. - #[test] - fn copy_shares_whole_tiles_with_the_layer() { - let src = noise(PixelFormat::RGBA8, Rect::new(0, 0, 1024, 768), 3); - let canvas = Rect::new(0, 0, 1024, 768); - let mut all = Surface::new(PixelFormat::GRAY8); - all.fill_rect(canvas, &[1.0]); - for sel in [None, Some(&all)] { - let clip = lift(&src, sel, canvas).unwrap(); - let shared = src.tiles().filter(|(c, t)| clip.surface.tile(**c).is_some_and(|o| std::sync::Arc::ptr_eq(o, t))).count(); - assert_eq!(shared, src.tile_count(), "every tile is shared"); - } - } -} diff --git a/crates/engine/src/edit_menu_cmds.rs b/crates/engine/src/edit_menu_cmds.rs deleted file mode 100644 index 11e2273601..0000000000 --- a/crates/engine/src/edit_menu_cmds.rs +++ /dev/null @@ -1,1241 +0,0 @@ -//! The rest of Photoshop's Edit menu: Fade, Purge, Content-Aware Fill and Scale, Define Brush -//! Preset / Custom Shape, Find and Replace Text, the Preset Manager and preset export/import. -//! Auto-Align and Auto-Blend Layers live in [`crate::align_cmds`]. -//! -//! Check Spelling is not implemented: it needs a word list per language, and no permissively -//! licensed dictionary small enough to bundle (or a pure-Rust checker with one) was available. - -use photocraft_color::{BlendMode, PixelFormat}; -use photocraft_doc::vector::Path; -use photocraft_doc::{DocId, Document, Layer, LayerContent, LayerId}; -use photocraft_geom::Rect; -use photocraft_raster::{Surface, from_rgba_into, to_rgba}; -use serde::{Deserialize, Serialize}; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, blend_from_str, int, layer_param}; -use crate::{EngineError, Result, Session}; - -// ------------------------------------------------------------------ state - -/// What Edit › Fade would fade: the result of the last filter, adjustment or paint command. -#[derive(Clone, Debug, PartialEq)] -pub struct FadeSource { - pub doc: DocId, - /// Document revision right after the command; any later change disables Fade. - pub revision: u64, - pub layer: LayerId, - pub mask: bool, - pub label: String, -} - -/// A custom shape (Edit › Define Custom Shape), usable by the Custom Shape tool. -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -pub struct CustomShape { - pub name: String, - pub path: Path, -} - -/// Edit-menu state kept by the session. -#[derive(Clone, Debug, Default)] -pub struct EditState { - pub fade: Option, - /// Custom shape library (the vector tools read it). - pub custom_shapes: Vec, - /// Find and Replace Text position: (layer, byte offset after the current match). - pub find_cursor: Option<(LayerId, usize, usize)>, -} - -/// Commands whose result Edit › Fade can blend back against the previous state. -fn fadeable(id: &str) -> bool { - id.starts_with("filter.") - || id.starts_with("image.adjustments.") - || id.starts_with("paint.") - || matches!(id, "edit.fill" | "edit.stroke" | "edit.contentAwareFill" | "image.autoTone" | "image.autoContrast" | "image.autoColor") -} - -/// Called after every successful command: remember a fadeable result. -pub(crate) fn after_command(s: &mut Session, id: &str) { - if !fadeable(id) { - return; - } - let Some(st) = s.active() else { return }; - let Some(layer) = st.active_layer else { return }; - let label = st.history.undo_label().unwrap_or(id).to_string(); - let mask = st.doc.layer(layer).is_some_and(|l| l.mask.is_some()) - && st.history.state(st.history.past_len().wrapping_sub(1)).is_some_and(|prev| { - // The mask changed and the pixels didn't: the command painted the mask. - let now = st.doc.layer(layer); - let was = prev.layer(layer); - let surf_eq = |a: Option<&Surface>, b: Option<&Surface>| match (a, b) { - (Some(a), Some(b)) => crate_fingerprint(a) == crate_fingerprint(b), - (None, None) => true, - _ => false, - }; - surf_eq(now.and_then(|l| l.surface()), was.and_then(|l| l.surface())) - && !surf_eq(now.and_then(|l| l.mask.as_ref().map(|m| &m.surface)), was.and_then(|l| l.mask.as_ref().map(|m| &m.surface))) - }); - s.edit_state.fade = Some(FadeSource { doc: st.doc.id, revision: st.revision, layer, mask, label }); -} - -/// Tile identity of a surface (pointer equality of its COW tiles). -fn crate_fingerprint(s: &Surface) -> u64 { - s.tiles().fold(s.tile_count() as u64, |h, (c, t)| { - (h ^ std::sync::Arc::as_ptr(t) as usize as u64 ^ ((c.tx as u64) << 32 | c.ty as u32 as u64)).wrapping_mul(0x100_0000_01b3) - }) -} - -// ------------------------------------------------------------------ helpers - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -fn f32_or(p: &Value, k: &str, d: f32) -> f32 { - p.get(k).and_then(Value::as_f64).map_or(d, |v| v as f32) -} - -fn str_or<'a>(p: &'a Value, k: &str, d: &'a str) -> &'a str { - p.get(k).and_then(Value::as_str).unwrap_or(d) -} - -fn bool_or(p: &Value, k: &str, d: bool) -> bool { - p.get(k).and_then(Value::as_bool).unwrap_or(d) -} - -fn always(_: &Session) -> std::result::Result<(), String> { - Ok(()) -} - -fn pixel_layer(s: &Session) -> std::result::Result { - let d = s.active().ok_or("no document open")?; - let id = d.active_layer.ok_or("no active layer")?; - let l = d.doc.layer(id).ok_or("no active layer")?; - if !matches!(l.content, LayerContent::Raster(_)) { - return Err(format!("active layer is {} {} layer, not a pixel layer", l.content.article(), l.content.kind_name())); - } - let locks = d.doc.effective_locks(id); - if locks.all || locks.pixels { - return Err(format!("the layer \"{}\" is locked", l.name)); - } - Ok(id) -} - -fn writable_surface(doc: &mut Document, id: LayerId) -> Result<&mut Surface> { - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.all || locks.pixels { - return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", l.name))); - } - l.surface_mut().ok_or_else(|| EngineError::Other("not a pixel layer".into())) -} - -// ------------------------------------------------------------------ Fade - -fn can_fade(s: &Session) -> std::result::Result<(), String> { - let st = s.active().ok_or("no document open")?; - let f = s.edit_state.fade.as_ref().ok_or("nothing to fade")?; - if f.doc != st.doc.id || f.revision != st.revision || !st.history.can_undo() { - return Err("Fade is only available right after a filter, adjustment or painting step".into()); - } - if st.doc.layer(f.layer).is_none() { - return Err("the faded layer no longer exists".into()); - } - Ok(()) -} - -fn surface_of(doc: &Document, id: LayerId, mask: bool) -> Option<&Surface> { - let l = doc.layer(id)?; - if mask { l.mask.as_ref().map(|m| &m.surface) } else { l.surface() } -} - -/// Blend `after` back towards `before` over `area`: `before + opacity × (B(before, after) − before)` -/// with blend mode `mode` (premultiplied, so alpha changes fade too). -pub fn fade_surface(before: &Surface, after: &mut Surface, area: Rect, mode: BlendMode, opacity: f32) -> Result<()> { - let fmt = after.format(); - let n = fmt.channels(); - if area.is_empty() || n == 0 { - return Ok(()); - } - let b = crate::allocation::read_region(before, area, "fading pixels")?; - let mut a = crate::allocation::read_region(after, area, "fading pixels")?; - let k = opacity.clamp(0.0, 1.0); - let lerp_premul = |pb: &[f32], pa: &[f32], out: &mut [f32], alpha: bool| { - let m = out.len(); - if !alpha { - for c in 0..m { - out[c] = pb[c] + (pa[c] - pb[c]) * k; - } - return; - } - let (ab, aa) = (pb[m - 1], pa[m - 1]); - let ao = ab + (aa - ab) * k; - for c in 0..m - 1 { - let pm = pb[c] * ab + (pa[c] * aa - pb[c] * ab) * k; - out[c] = if ao > 1e-9 { pm / ao } else { 0.0 }; - } - out[m - 1] = ao; - }; - let mut tmp = crate::allocation::filled(n, 0.0f32, "fading pixels")?; - for (pa, pb) in a.chunks_exact_mut(n).zip(b.chunks_exact(n)) { - if mode == BlendMode::Normal || n == 1 { - lerp_premul(pb, pa, &mut tmp, fmt.alpha); - pa.copy_from_slice(&tmp); - } else { - let rb = to_rgba(&fmt, pb); - let ra = to_rgba(&fmt, pa); - let blended = photocraft_color::blend::composite(mode, rb, ra, 1.0); - let mut o = [0.0f32; 4]; - lerp_premul(&rb, &blended, &mut o, true); - let mut enc = [0.0f32; 8]; - from_rgba_into(&fmt, o, &mut enc); - pa.copy_from_slice(&enc[..n]); - } - } - after.try_write_region(area, &a)?; - Ok(()) -} - -fn fade(s: &mut Session, p: &Value) -> Result { - let cmd = "edit.fade"; - let src = s.edit_state.fade.clone().ok_or_else(|| bad(cmd, "nothing to fade"))?; - let opacity = f32_or(p, "opacity", 100.0).clamp(0.0, 100.0) / 100.0; - let mode = blend_from_str(str_or(p, "mode", "normal")).ok_or_else(|| bad(cmd, "unknown blend mode"))?; - let st = s.active().ok_or(EngineError::NoDocument)?; - let prev = st.history.state(st.history.past_len() - 1).ok_or_else(|| bad(cmd, "no previous state"))?; - let before = surface_of(&prev, src.layer, src.mask).cloned().ok_or_else(|| EngineError::Other("the layer did not exist before this step".into()))?; - let now = surface_of(&st.doc, src.layer, src.mask).ok_or(EngineError::NoLayer(src.layer))?; - if now.format() != before.format() { - return Err(EngineError::Other("can't fade a step that changed the layer's pixel format".into())); - } - let area = before.content_bounds().union(&now.content_bounds()); - let label = format!("Fade {}", src.label); - s.edit(&label, |doc, _| { - let l = doc.layer_mut(src.layer).ok_or(EngineError::NoLayer(src.layer))?; - let surf = if src.mask { l.mask.as_mut().map(|m| &mut m.surface) } else { l.surface_mut() }.ok_or(EngineError::NoLayer(src.layer))?; - fade_surface(&before, surf, area, mode, opacity)?; - surf.prune(); - Ok(()) - })?; - Ok(json!({"faded": src.label, "opacity": opacity * 100.0, "mode": mode.label()})) -} - -// ------------------------------------------------------------------ Purge - -fn history_bytes(s: &Session, all: bool) -> usize { - let docs: Vec<&crate::DocState> = if all { s.documents().iter().collect() } else { s.active().into_iter().collect() }; - // Purge keeps the snapshots, so the tiles they share with the states aren't freed. - docs.iter() - .map(|d| { - let snaps: Vec<&photocraft_doc::Document> = d.snapshots.iter().map(|s| &*s.doc).collect(); - d.history.unique_bytes_besides(&d.doc, &snaps) - }) - .sum() -} - -fn clip_bytes(s: &Session) -> usize { - s.clipboard.as_ref().map_or(0, |c| c.surface.tiles().map(|(_, t)| t.bytes().len()).sum()) -} - -fn purge(s: &mut Session, what: &str) -> Result { - let mut freed = 0usize; - let mut items: Vec<&str> = Vec::new(); - if matches!(what, "undo" | "all") && s.active().is_some_and(|d| d.history.can_undo()) { - let before = history_bytes(s, false); - if let Some(st) = s.active_mut() { - st.history.purge_last(); - } - freed += before.saturating_sub(history_bytes(s, false)); - items.push("undo"); - } - if matches!(what, "clipboard" | "all") && (s.clipboard.is_some() || s.style_clipboard.is_some()) { - freed += clip_bytes(s); - s.clipboard = None; - s.style_clipboard = None; - items.push("clipboard"); - } - if matches!(what, "histories" | "all") { - let b = history_bytes(s, true); - if s.documents().iter().any(|d| d.history.can_undo() || d.history.can_redo()) { - for st in &mut s.docs { - st.history.clear(); - st.coalesce = None; - } - freed += b; - items.push("histories"); - } - } - if what == "all" { - let fx = photocraft_compose::purge_effect_cache(); - if fx > 0 { - freed += fx; - items.push("effect cache"); - } - let masks = photocraft_compose::masks::purge_cache(); - if masks > 0 { - freed = freed.saturating_add(masks); - items.push("mask cache"); - } - let stages = crate::smart_cmds::purge_stack_cache(); - if stages > 0 { - freed = freed.saturating_add(stages); - items.push("smart filter cache"); - } - let strokes = photocraft_compose::shape_split::purge_cache(); - if strokes > 0 { - freed = freed.saturating_add(strokes); - items.push("shape stroke cache"); - } - } - s.edit_state.fade = None; - let msg = if items.is_empty() { "nothing to purge".to_string() } else { format!("purged {}", items.join(", ")) }; - Ok(json!({"purged": items, "bytes": freed, "message": msg})) -} - -fn can_purge_undo(s: &Session) -> std::result::Result<(), String> { - s.active().filter(|d| d.history.can_undo()).map(|_| ()).ok_or_else(|| "nothing to purge".into()) -} -fn can_purge_clipboard(s: &Session) -> std::result::Result<(), String> { - if s.clipboard.is_some() || s.style_clipboard.is_some() { Ok(()) } else { Err("the clipboard is empty".into()) } -} -fn can_purge_histories(s: &Session) -> std::result::Result<(), String> { - if s.documents().iter().any(|d| d.history.can_undo() || d.history.can_redo()) { Ok(()) } else { Err("no history to purge".into()) } -} -fn can_purge_all(s: &Session) -> std::result::Result<(), String> { - can_purge_histories(s).or_else(|_| can_purge_clipboard(s)).or_else(|_| { - if photocraft_compose::effect_cache_bytes() > 0 || photocraft_compose::masks::cache_bytes() > 0 || crate::smart_cmds::stack_cache_bytes() > 0 { - Ok(()) - } else { - Err("nothing to purge".into()) - } - }) -} - -// ------------------------------------------------------------------ Content-Aware Fill - -fn can_caf(s: &Session) -> std::result::Result<(), String> { - pixel_layer(s)?; - let d = s.active().ok_or("no document open")?; - if d.doc.selection.as_ref().is_none_or(|m| m.content_bounds().is_empty()) { - return Err("make a selection around what to remove".into()); - } - Ok(()) -} - -/// Mask surface (gray) of an alpha channel by index or name. -fn channel_mask<'a>(doc: &'a Document, v: &Value) -> Option<&'a Surface> { - match v { - Value::Number(n) => doc.channels.get(n.as_u64()? as usize).map(|c| &c.surface), - Value::String(name) => doc.channels.iter().find(|c| c.name == *name).map(|c| &c.surface), - _ => None, - } -} - -/// `key` as an `[x, y, w, h]` rectangle. The saturating float→int casts bound each component; -/// the additions saturate too — `area: [1e30, 0, 1e30, 10]` is a whole-canvas window, not an -/// overflow (it panicked in debug builds before the `saturating_add`). -fn rect_param(p: &Value, key: &str, cmd: &str) -> Result { - let a = p.get(key).and_then(Value::as_array).ok_or_else(|| bad(cmd, format!("`{key}` must be [x, y, w, h]")))?; - let n = |i: usize| { - a.get(i) - .and_then(Value::as_f64) - .filter(|f| f.is_finite()) - .map(|v| v.round() as i32) - .ok_or_else(|| bad(cmd, format!("`{key}` must be four finite numbers"))) - }; - let (x, y, w, h) = (n(0)?, n(1)?, n(2)?, n(3)?); - Ok(Rect::new(x, y, x.saturating_add(w.max(0)), y.saturating_add(h.max(0)))) -} - -fn content_aware_fill(s: &mut Session, p: &Value) -> Result { - content_aware_fill_as(s, p, "edit.contentAwareFill", "Content-Aware Fill") -} - -/// Delete and Fill Selection (#1286): Photoshop's one-click removal from the selection-tool -/// context menu. Content-Aware Fill into the layer, no dialog, sampling as Edit › Fill does (the -/// whole window, measured on Photoshop 25.4) with the default colour adaptation. -fn delete_and_fill(s: &mut Session, _: &Value) -> Result { - content_aware_fill_as(s, &json!({"sampling": "rectangular"}), "edit.deleteAndFillSelection", "Delete and Fill Selection") -} - -fn content_aware_fill_as(s: &mut Session, p: &Value, cmd: &'static str, label: &'static str) -> Result { - use photocraft_algo::content_aware::{FillOptions, auto_sampling_with, color_level, fill_owned, rotation_level}; - let id = pixel_layer(s).map_err(EngineError::Other)?; - let st = s.active().ok_or(EngineError::NoDocument)?; - let doc = st.doc.clone(); - let sel = doc.selection.clone().ok_or_else(|| bad(cmd, "no selection"))?; - let hb = sel.content_bounds().intersect(&doc.bounds()); - if hb.is_empty() { - return Err(bad(cmd, "the selection is outside the canvas")); - } - let sampling = str_or(p, "sampling", "auto").to_string(); - let custom_mask: Option = p.get("channel").and_then(|v| channel_mask(&doc, v)).cloned(); - let custom_rect = if p.get("area").is_some() { Some(rect_param(p, "area", cmd)?) } else { None }; - // Photoshop's window (`content_aware::sampling_window`) for Auto and Rectangular; `margin` - // asks for a rectangle that far around the selection instead. - let window = match sampling.as_str() { - "auto" => crate::fill_cmds::sampling_window(hb, doc.bounds()), - "rectangular" => match int(p, "margin") { - Some(m) => hb.inflate(m.clamp(0, 100_000) as i32), - None => crate::fill_cmds::sampling_window(hb, doc.bounds()), - }, - "custom" => { - let r = match (custom_rect, custom_mask.as_ref()) { - (Some(r), _) => r, - (None, Some(m)) => m.content_bounds(), - (None, None) => return Err(bad(cmd, "custom sampling needs `area` [x,y,w,h] or `channel` (alpha channel index or name)")), - }; - r.union(&hb).inflate(4) - } - other => return Err(bad(cmd, format!("unknown sampling `{other}` (auto|rectangular|custom)"))), - } - .intersect(&doc.bounds()); - let opts = FillOptions { - color_adaptation: color_level(str_or(p, "colorAdaptation", "default")), - rotations: rotation_level(str_or(p, "rotationAdaptation", "none")), - scale: bool_or(p, "scale", false), - mirror: bool_or(p, "mirror", false), - seed: p.get("seed").and_then(Value::as_u64).unwrap_or(1), - }; - let output = str_or(p, "output", "current").to_string(); - if !matches!(output.as_str(), "current" | "new" | "duplicate") { - return Err(bad(cmd, "`output` must be current|new|duplicate")); - } - let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?; - let fmt = surf.format(); - // Photoshop's Sample All Layers checkbox is off unless the caller asks. When the active - // layer has no pixels in the window there is nothing to sample, so that case uses the - // visible composite (#4026). An explicit false still reads only this layer. - let sample_all = match p.get("sampleAllLayers").and_then(Value::as_bool) { - Some(v) => v, - None => surf.content_bounds().intersect(&window).is_empty(), - }; - let n = fmt.channels(); - // Refuse a window too large to read before allocating for it, whatever the sampling (#963). - let (w, h) = crate::fill_cmds::window_size(window)?; - // A background job when started with `Session::start` (#210): reading the window and the - // PatchMatch fill run on a worker against the document snapshot, cancellable per row band. - crate::jobs::run( - s, - label, - true, - move |ctx| { - use rayon::prelude::*; - - use crate::fill_cmds::{read_window, read_window_cover, read_window_mask}; - ctx.progress(0.0, label); - let cancel = || ctx.cancelled(); - let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?; - let img = if sample_all { - if cancel() { - return Err(EngineError::Cancelled); - } - let region = crate::retouch_cmds::composite_region(&doc, None, crate::retouch_cmds::SampleLayers::All, window, fmt); - if cancel() { - return Err(EngineError::Cancelled); - } - region.data - } else { - read_window(surf, window, &cancel).ok_or(EngineError::Cancelled)? - }; - let hole = read_window_cover(&sel, window, &cancel).ok_or(EngineError::Cancelled)?; - let mut source = match (sampling.as_str(), custom_rect, custom_mask.as_ref()) { - ("custom", Some(r), _) => (0..w * h).into_par_iter().map(|i| r.contains(window.x0 + (i % w) as i32, window.y0 + (i / w) as i32)).collect(), - ("custom", None, Some(m)) => read_window_mask(m, window, &cancel).ok_or(EngineError::Cancelled)?.par_iter().map(|v| *v > 0.5).collect(), - _ => vec![true; w * h], - }; - ctx.check()?; - // Auto: only what looks like the selection's surroundings. - if sampling == "auto" { - source = auto_sampling_with(w, h, n, &img, &hole, &photocraft_raster::Interrupt::cancel_only(&cancel)).map_err(|_| EngineError::Cancelled)?; - } - ctx.check()?; - // The fill changes only the selected pixels: keep the original and the coverage over - // their bounds for the apply step, and fill the window's pixels themselves. - let (bx0, by0, bx1, by1) = mask_bounds(w, h, &hole); - let original: Vec = (by0..by1).flat_map(|y| img[(y * w + bx0) * n..(y * w + bx1) * n].iter().copied()).collect(); - let at = |x: usize, y: usize| (window.x0 + x as i32, window.y0 + y as i32); - let ((x0, y0), (x1, y1)) = (at(bx0, by0), at(bx1, by1)); - let cover = read_window_mask(&sel, Rect::new(x0, y0, x1, y1), &cancel).ok_or(EngineError::Cancelled)?; - let n_hole = cover.iter().filter(|c| **c > 0.0).count(); - let filled = ctx.stage(0.05, 1.0, label, |ctl| fill_owned(w, h, n, img, &hole, &source, &opts, ctl)).map_err(|_| EngineError::Cancelled)?; - Ok(FilledWindow { filled, bounds: (bx0, by0, bx1, by1), original, cover, n_hole }) - }, - move |s, done| apply_content_aware_fill(s, label, id, &output, fmt, window, done), - ) -} - -/// A Content-Aware Fill's result for its apply step: the filled window (the same as the -/// original outside the selected pixels) and, over the bounds of the selected pixels (in window -/// coordinates), the original pixels and the selection's coverage. -struct FilledWindow { - filled: Vec, - bounds: (usize, usize, usize, usize), - original: Vec, - cover: Vec, - n_hole: usize, -} - -/// `f(window index, index over the bounds, coverage)` for each selected pixel of a -/// [`FilledWindow`] (`w` wide). -fn each_selected(w: usize, (bx0, by0, bx1, by1): (usize, usize, usize, usize), cover: &[f32], mut f: impl FnMut(usize, usize, f32)) { - let bw = bx1.saturating_sub(bx0); - for y in by0..by1 { - for x in bx0..bx1 { - let k = (y - by0) * bw + (x - bx0); - if let Some(&c) = cover.get(k) - && c > 0.0 - { - f(y * w + x, k, c); - } - } - } -} - -/// Bounds `(x0, y0, x1, y1)` of the set cells of a `w × h` mask (all zero when there are none). -fn mask_bounds(w: usize, h: usize, m: &[bool]) -> (usize, usize, usize, usize) { - use rayon::prelude::*; - if w == 0 { - return (0, 0, 0, 0); - } - let rows: Vec> = - m.par_chunks(w).take(h).map(|row| Some((row.iter().position(|v| *v)?, row.iter().rposition(|v| *v)? + 1))).collect(); - let mut b: Option<(usize, usize, usize, usize)> = None; - for (y, span) in rows.into_iter().enumerate() { - if let Some((x0, x1)) = span { - b = Some(b.map_or((x0, y, x1, y + 1), |(bx0, by0, bx1, _)| (bx0.min(x0), by0, bx1.max(x1), y + 1))); - } - } - b.unwrap_or((0, 0, 0, 0)) -} - -/// Write a Content-Aware Fill result (the job's apply step): into the layer, a new layer or a -/// duplicate, as one undo step. -fn apply_content_aware_fill(s: &mut Session, label: &str, id: LayerId, output: &str, fmt: PixelFormat, window: Rect, done: FilledWindow) -> Result { - let n = fmt.channels(); - let (w, h) = (window.width() as usize, window.height() as usize); - let FilledWindow { mut filled, bounds, original, cover, n_hole } = done; - let layer = s.edit(label, |doc, active| { - let target = match output { - "new" => { - let mut l = Layer::raster(doc.next_layer_name("Content-Aware Fill"), PixelFormat { alpha: true, ..fmt }); - let mut px = vec![0.0f32; w * h * n.max(1)]; - let lf = l.surface().map(|s| s.format()).unwrap_or(fmt); - let ln = lf.channels(); - px.resize(w * h * ln, 0.0); - each_selected(w, bounds, &cover, |i, _, c| { - let rgba = to_rgba(&fmt, &filled[i * n..(i + 1) * n]); - let mut enc = [0.0f32; 8]; - from_rgba_into(&lf, [rgba[0], rgba[1], rgba[2], rgba[3] * c], &mut enc); - px[i * ln..(i + 1) * ln].copy_from_slice(&enc[..ln]); - }); - if let Some(s) = l.surface_mut() { - s.write_region(window, &px); - s.prune(); - } - let nid = doc.insert_above(Some(id), l); - *active = Some(nid); - return Ok(nid); - } - "duplicate" => { - let mut dup = doc.layer(id).ok_or(EngineError::NoLayer(id))?.duplicate(); - dup.name = doc.copy_name(&dup.name); - let nid = doc.insert_above(Some(id), dup); - *active = Some(nid); - nid - } - _ => id, - }; - let surf = writable_surface(doc, target)?; - // The fill blended in by the coverage (elsewhere it is the original). - each_selected(w, bounds, &cover, |i, k, c| { - for ch in 0..n { - let (j, o) = (i * n + ch, k * n + ch); - filled[j] = original[o] + (filled[j] - original[o]) * c; - } - }); - surf.write_region(window, &filled); - Ok(target) - })?; - Ok(json!({"layer": layer.0, "filled": n_hole, "window": [window.x0, window.y0, window.width(), window.height()]})) -} - -// ------------------------------------------------------------------ Content-Aware Scale - -/// Reach of Content-Aware Scale's `x`/`y`: the largest document side. -const CAS_POSITION_LIMIT: f64 = 300_000.0; - -/// What Content-Aware Scale does, read from its params against the active document: the -/// command and the transform box's live preview ([`ContentAwareScalePreview`]) share it. -struct CasPlan { - doc: std::sync::Arc, - layer: LayerId, - /// The area scaled: the selection's bounds, else the layer's content. - region: Rect, - /// Where the result goes: `x`, `y` (default: the region's corner), `width`, `height`. - dst: Rect, - amount: f32, - protect: Option, - skin_tones: bool, -} - -fn cas_plan(s: &Session, p: &Value) -> Result { - let cmd = "edit.contentAwareScale"; - let id = pixel_layer(s).map_err(EngineError::Other)?; - let st = s.active().ok_or(EngineError::NoDocument)?; - let doc = st.doc.clone(); - let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?; - let region = match &doc.selection { - Some(sel) => sel.content_bounds().intersect(&doc.bounds()), - None => surf.content_bounds(), - }; - if region.is_empty() { - return Err(EngineError::Other("nothing to scale: the layer is empty".into())); - } - let (w, h) = (region.width() as usize, region.height() as usize); - let dim = |k: &str, pct: &str, cur: usize| -> Result { - let v = match (p.get(k).and_then(Value::as_f64), p.get(pct).and_then(Value::as_f64)) { - (Some(px), _) => px, - (None, Some(pc)) => cur as f64 * pc / 100.0, - (None, None) => cur as f64, - }; - if !(1.0..=60_000.0).contains(&v) { - return Err(bad(cmd, format!("`{k}` must be 1..60000 px"))); - } - Ok(v.round() as u32) - }; - let (nw, nh) = (dim("width", "scaleX", w)?, dim("height", "scaleY", h)?); - let at = |k: &str, cur: i32| -> Result { - match p.get(k).filter(|v| !v.is_null()) { - None => Ok(cur), - Some(v) => match v.as_f64() { - Some(x) if (-CAS_POSITION_LIMIT..=CAS_POSITION_LIMIT).contains(&x) => Ok(x.round() as i32), - _ => Err(bad(cmd, format!("`{k}` must be a number of px within ±300000"))), - }, - } - }; - let dst = Rect::from_xywh(at("x", region.x0)?, at("y", region.y0)?, nw, nh); - let amount = f32_or(p, "amount", 100.0).clamp(0.0, 100.0) / 100.0; - // Protection: an alpha channel and/or skin tones. - let protect = match p.get("protect").filter(|v| !v.is_null() && v.as_str() != Some("none")) { - Some(v) => Some(channel_mask(&doc, v).cloned().ok_or_else(|| bad(cmd, "`protect` must name an alpha channel (index or name) or \"none\""))?), - None => None, - }; - let skin_tones = bool_or(p, "protectSkinTones", false); - Ok(CasPlan { doc, layer: id, region, dst, amount: if amount.is_finite() { amount } else { 1.0 }, protect, skin_tones }) -} - -/// The scaled pixels: `img` (`w`×`h`, `fmt`) seam-carved to `nw`×`nh` where the protection -/// (`protect`, one value per pixel, and the skin tones when asked) steers the seams, blended with -/// a plain resample by `amount` (0..1). Checks `ctx` for cancellation before every seam. -#[allow(clippy::too_many_arguments)] -fn cas_pixels( - ctx: &crate::jobs::JobCtx, - label: &str, - img: &[f32], - (w, h): (usize, usize), - fmt: PixelFormat, - mut protect: Option>, - skin_tones: bool, - (nw, nh): (usize, usize), - amount: f32, -) -> Result> { - use photocraft_algo::seam::{carve_with, resize_bilinear, skin_mask}; - let n = fmt.channels(); - if skin_tones { - let rgba: Vec = img.chunks_exact(n).flat_map(|px| to_rgba(&fmt, px)).collect(); - let skin = skin_mask(w, h, 4, &rgba); - protect = Some(match protect { - Some(pm) => pm.iter().zip(&skin).map(|(a, b)| a.max(*b)).collect(), - None => skin, - }); - } - ctx.check()?; - let carved = if amount > 0.0 { - ctx.stage(0.05, 0.95, label, |ctl| carve_with(w, h, n, img, protect.as_deref(), nw, nh, ctl)).map_err(|_| EngineError::Cancelled)? - } else { - Vec::new() - }; - let plain = if amount < 1.0 { resize_bilinear(w, h, n, img, nw, nh) } else { Vec::new() }; - Ok(match (carved.is_empty(), plain.is_empty()) { - (false, true) => carved, - (true, false) => plain, - _ => carved.iter().zip(&plain).map(|(c, l)| l + (c - l) * amount).collect::>(), - }) -} - -fn content_aware_scale(s: &mut Session, p: &Value) -> Result { - let CasPlan { doc, layer: id, region, dst, amount, protect: protect_mask, skin_tones } = cas_plan(s, p)?; - let (w, h) = (region.width() as usize, region.height() as usize); - let (nw, nh) = (dst.width() as usize, dst.height() as usize); - let label = "Content-Aware Scale"; - // A background job when started with `Session::start` (#210): seam carving runs on a worker - // against the document snapshot and checks for cancellation before every seam. - crate::jobs::run( - s, - label, - true, - move |ctx| { - ctx.progress(0.0, label); - let surf = doc.layer(id).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(id))?; - let img = surf.read_region(region); - let protect: Option> = - protect_mask.map(|m| (0..w * h).map(|i| m.sample_channel(region.x0 + (i % w) as i32, region.y0 + (i / w) as i32, 0)).collect()); - cas_pixels(ctx, label, &img, (w, h), surf.format(), protect, skin_tones, (nw, nh), amount) - }, - move |s, out| { - s.edit(label, |doc, _| { - let surf = writable_surface(doc, id)?; - crate::pixels::try_clear_surface(surf, region, None)?; - surf.write_region(dst, &out); - surf.prune(); - Ok(()) - })?; - Ok(json!({"from": [w, h], "to": [nw, nh], "bounds": [dst.x0, dst.y0, nw, nh]})) - }, - ) -} - -/// Content-Aware Scale on a reduced copy, for the transform box's live preview (#3096): the -/// command's own carving, on the area scaled down so the longer of its sides before and after -/// is at most `max_side` px. Prepared from the session ([`ContentAwareScalePreview::new`]) and -/// [`run`](ContentAwareScalePreview::run) on any thread. -pub struct ContentAwareScalePreview { - plan: CasPlan, - max_side: u32, -} - -impl ContentAwareScalePreview { - /// `p` takes `edit.contentAwareScale`'s params; the same ones fail the same way. - pub fn new(s: &Session, p: &Value, max_side: u32) -> Result { - Ok(Self { plan: cas_plan(s, p)?, max_side: max_side.max(1) }) - } - - /// The area scaled, in document px. - pub fn region(&self) -> Rect { - self.plan.region - } - - /// The reduced sizes of the area before and after: `[w, h, new w, new h]`. - fn reduced(&self) -> [usize; 4] { - let (region, dst) = (self.plan.region, self.plan.dst); - let (w, h, nw, nh) = (region.width(), region.height(), dst.width(), dst.height()); - let k = (f64::from(w.max(h).max(nw).max(nh)) / f64::from(self.max_side)).max(1.0); - let side = |v: u32| ((f64::from(v) / k).round() as usize).max(1); - [side(w), side(h), side(nw), side(nh)] - } - - /// The size of [`run`](Self::run)'s result, px. - pub fn size(&self) -> [u32; 2] { - let [_, _, w, h] = self.reduced(); - [w as u32, h as u32] - } - - /// The scaled pixels at the reduced size ([`size`](Self::size)), in the layer's format, as a - /// surface with its corner at the origin. Checks `ctx` for cancellation before every seam. - pub fn run(&self, ctx: &crate::jobs::JobCtx) -> Result { - let CasPlan { doc, layer, region, dst: _, amount, protect, skin_tones } = &self.plan; - let surf = doc.layer(*layer).and_then(|l| l.surface()).ok_or(EngineError::NoLayer(*layer))?; - let fmt = surf.format(); - let n = fmt.channels(); - let (w, h) = (region.width(), region.height()); - let [pw, ph, pnw, pnh] = self.reduced(); - // Nearest samples of the area (and of the protecting channel) at the reduced size. - let src_x: Vec = (0..pw).map(|i| region.x0 + (((i as f64 + 0.5) * f64::from(w) / pw as f64) as i32).min(w as i32 - 1)).collect(); - let src_y = |j: usize| region.y0 + (((j as f64 + 0.5) * f64::from(h) / ph as f64) as i32).min(h as i32 - 1); - let mut img = Vec::with_capacity(pw * ph * n); - for j in 0..ph { - ctx.check()?; - let row = surf.read_region(Rect::from_xywh(region.x0, src_y(j), w, 1)); - for &x in &src_x { - let at = (x - region.x0) as usize * n; - img.extend_from_slice(row.get(at..at + n).ok_or_else(|| EngineError::Other("preview read out of bounds".into()))?); - } - } - let protect = protect.as_ref().map(|m| (0..pw * ph).map(|i| m.sample_channel(src_x[i % pw], src_y(i / pw), 0)).collect()); - let out = cas_pixels(ctx, "Content-Aware Scale", &img, (pw, ph), fmt, protect, *skin_tones, (pnw, pnh), *amount)?; - let mut s = Surface::new(fmt); - s.write_region(Rect::from_xywh(0, 0, pnw as u32, pnh as u32), &out); - Ok(s) - } -} - -// ------------------------------------------------------------------ Define Brush / Custom Shape - -fn define_brush_preset(s: &mut Session, p: &Value) -> Result { - let name = match p.get("name").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()) { - Some(n) => n.to_string(), - None => { - let n = s.tools.presets.iter().filter(|b| b.name.starts_with("Sampled Brush")).count() + 1; - format!("Sampled Brush {n}") - } - }; - s.execute("brush.defineFromSelection", json!({"name": name})) -} - -/// The path Define Custom Shape uses: `"work"`, a saved path name, else the work path, the -/// active shape layer, the active layer's vector mask, or the last saved path. -pub(crate) fn current_path(doc: &Document, active: Option, spec: Option<&str>) -> Option { - let ok = |p: &Path| !p.subpaths.is_empty(); - match spec { - Some("work") => return doc.work_path.clone().filter(ok), - Some(name) => return doc.paths.iter().find(|n| n.name == name).map(|n| n.path.clone()).filter(ok), - None => {} - } - if let Some(p) = doc.work_path.clone().filter(ok) { - return Some(p); - } - if let Some(l) = active.and_then(|id| doc.layer(id)) { - if let LayerContent::Shape(sh) = &l.content - && ok(&sh.path) - { - return Some(sh.path.clone()); - } - if let Some(vm) = l.vector_mask.as_ref().filter(|v| ok(&v.path)) { - return Some(vm.path.clone()); - } - } - doc.paths.last().map(|n| n.path.clone()).filter(ok) -} - -fn can_define_shape(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - current_path(&d.doc, d.active_layer, None).map(|_| ()).ok_or_else(|| "select a path or shape layer first".into()) -} - -fn define_custom_shape(s: &mut Session, p: &Value) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let path = current_path(&st.doc, st.active_layer, p.get("path").and_then(Value::as_str)).ok_or_else(|| bad("edit.defineCustomShape", "no such path"))?; - let name = p - .get("name") - .and_then(Value::as_str) - .map(str::trim) - .filter(|n| !n.is_empty()) - .map(str::to_string) - .unwrap_or_else(|| format!("Shape {}", s.edit_state.custom_shapes.len() + 1)); - let shapes = &mut s.edit_state.custom_shapes; - match shapes.iter_mut().find(|c| c.name == name) { - Some(c) => c.path = path, - None => shapes.push(CustomShape { name: name.clone(), path }), - } - Ok(json!({"name": name, "count": shapes.len()})) -} - -// ------------------------------------------------------------------ Find and Replace Text - -/// Byte ranges of `find` in `text` (case-insensitive unless `case`; whole words only with `whole`). -pub fn find_matches(text: &str, find: &str, case: bool, whole: bool) -> Vec<(usize, usize)> { - if find.is_empty() { - return Vec::new(); - } - let norm = |c: char| -> String { if case { c.to_string() } else { c.to_lowercase().collect() } }; - let needle: String = find.chars().map(norm).collect(); - let mut out = Vec::new(); - let mut at = 0; - while at < text.len() { - // Compare char by char so case folding that changes byte lengths stays aligned. - let mut hay = String::new(); - let mut end = at; - for (i, c) in text[at..].char_indices() { - if hay.len() >= needle.len() { - break; - } - hay.push_str(&norm(c)); - end = at + i + c.len_utf8(); - } - if hay == needle { - let word = |c: Option| c.is_some_and(|c| c.is_alphanumeric() || c == '_'); - let before = text[..at].chars().next_back(); - let after = text[end..].chars().next(); - if !whole || (!word(before) && !word(after)) { - out.push((at, end)); - at = end; - continue; - } - } - at += text[at..].chars().next().map_or(1, char::len_utf8); - } - out -} - -fn type_layers(doc: &Document, forward: bool) -> Vec { - // Layers panel order: top first. - let mut ids: Vec = - doc.walk().into_iter().rev().filter(|(p, _, l)| matches!(l.content, LayerContent::Text(_)) && !doc.locks_at(p).all).map(|(_, _, l)| l.id).collect(); - if !forward { - ids.reverse(); - } - ids -} - -fn has_type_layer(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - if type_layers(&d.doc, true).is_empty() { Err("the document has no type layers".into()) } else { Ok(()) } -} - -fn find_replace(s: &mut Session, p: &Value) -> Result { - let cmd = "edit.findAndReplaceText"; - let find = p.get("find").and_then(Value::as_str).filter(|f| !f.is_empty()).ok_or_else(|| bad(cmd, "missing `find`"))?.to_string(); - let replace = str_or(p, "replace", "").to_string(); - let (case, whole, forward, all_layers) = - (bool_or(p, "caseSensitive", false), bool_or(p, "wholeWord", false), bool_or(p, "forward", true), bool_or(p, "allLayers", true)); - let action = str_or(p, "action", "changeAll").to_string(); - let st = s.active().ok_or(EngineError::NoDocument)?; - let mut layers = type_layers(&st.doc, forward); - if !all_layers { - let active = layer_param(s, p).ok(); - layers.retain(|id| Some(*id) == active); - } - let text_of = |doc: &Document, id: LayerId| -> Option { - match &doc.layer(id)?.content { - LayerContent::Text(t) => Some(t.text.clone()), - _ => None, - } - }; - match action.as_str() { - "changeAll" => { - let doc = st.doc.clone(); - let plan: Vec<(LayerId, Vec<(usize, usize)>)> = - layers.iter().filter_map(|id| Some((*id, find_matches(&text_of(&doc, *id)?, &find, case, whole)))).filter(|(_, m)| !m.is_empty()).collect(); - let count: usize = plan.iter().map(|(_, m)| m.len()).sum(); - if count == 0 { - return Ok(json!({"count": 0, "layers": 0})); - } - s.edit("Replace All Text", |doc, _| { - let snapshot = doc.clone(); - for (id, matches) in &plan { - if let Some(LayerContent::Text(t)) = doc.layer_mut(*id).map(|l| &mut l.content) { - for (a, b) in matches.iter().rev() { - crate::type_cmds::replace_text(t, *a, *b, &replace); - } - crate::type_cmds::refresh(&snapshot, t); - } - } - Ok(()) - })?; - s.edit_state.find_cursor = None; - Ok(json!({"count": count, "layers": plan.len()})) - } - "find" | "change" | "changeFind" => { - let doc = st.doc.clone(); - let cursor = s.edit_state.find_cursor; - let mut changed = None; - if action != "find" - && let Some((id, a, b)) = cursor - && text_of(&doc, id).is_some_and(|t| find_matches(&t, &find, case, whole).contains(&(a, b))) - { - crate::type_cmds::check_text_unlocked(&doc, id)?; - s.edit("Replace Text", |doc, _| { - let snapshot = doc.clone(); - if let Some(LayerContent::Text(t)) = doc.layer_mut(id).map(|l| &mut l.content) { - crate::type_cmds::replace_text(t, a, b, &replace); - crate::type_cmds::refresh(&snapshot, t); - } - Ok(()) - })?; - let end = a + replace.len(); - // Collapse the cursor on the side the search moves away from, so the next search - // skips the replaced text even when the replacement still matches. - let c = if forward { end } else { a }; - s.edit_state.find_cursor = Some((id, c, c)); - changed = Some((id, a, end)); - if action == "change" { - return Ok(json!({"changed": {"layer": id.0, "start": a, "end": end}})); - } - } - // Find the next match after the cursor (wrapping through the layers). - let doc = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let cursor = s.edit_state.find_cursor; - let start = cursor.and_then(|(id, _, _)| layers.iter().position(|l| *l == id)).unwrap_or(0); - for k in 0..=layers.len() { - // `allLayers: false` with a non-type layer active leaves no layers to search (#703). - let Some(&id) = layers.get((start + k) % layers.len().max(1)) else { break }; - let Some(text) = text_of(&doc, id) else { continue }; - let ms = find_matches(&text, &find, case, whole); - let hit = match (k, cursor) { - (0, Some((cid, a, b))) if cid == id => { - if forward { - ms.into_iter().find(|m| m.0 >= b) - } else { - ms.into_iter().rev().find(|m| m.1 <= a) - } - } - _ => { - if forward { - ms.into_iter().next() - } else { - ms.into_iter().next_back() - } - } - }; - if let Some((a, b)) = hit { - s.edit_state.find_cursor = Some((id, a, b)); - let _ = s.select_layer(id); - return Ok( - json!({"found": {"layer": id.0, "start": a, "end": b, "text": &text[a..b]}, "changed": changed.map(|(l, a, b)| json!({"layer": l.0, "start": a, "end": b}))}), - ); - } - } - s.edit_state.find_cursor = None; - Ok(json!({"found": null, "changed": changed.map(|(l, a, b)| json!({"layer": l.0, "start": a, "end": b}))})) - } - other => Err(bad(cmd, format!("unknown action `{other}` (find|change|changeFind|changeAll)"))), - } -} - -// ------------------------------------------------------------------ presets - -/// Libraries the Preset Manager edits (patterns export as `.pat` with `pattern.export`). -const PRESET_KINDS: [&str; 3] = ["brushes", "customShapes", "patterns"]; - -fn preset_names(s: &Session, kind: &str) -> Option> { - Some(match kind { - "brushes" => s.tools.presets.iter().map(|b| b.name.clone()).collect(), - "customShapes" => s.edit_state.custom_shapes.iter().map(|c| c.name.clone()).collect(), - "patterns" => s.patterns.items.iter().map(|p| p.name.clone()).collect(), - _ => return None, - }) -} - -fn preset_index(s: &Session, kind: &str, p: &Value) -> Result { - let names = preset_names(s, kind).ok_or_else(|| bad("edit.presets.presetManager", format!("unknown preset kind `{kind}` ({})", PRESET_KINDS.join("|"))))?; - if let Some(i) = p.get("index").and_then(Value::as_u64) { - return if (i as usize) < names.len() { Ok(i as usize) } else { Err(bad("edit.presets.presetManager", format!("no preset {i}"))) }; - } - let name = p.get("name").and_then(Value::as_str).ok_or_else(|| bad("edit.presets.presetManager", "give `index` or `name`"))?; - names.iter().position(|n| n == name).ok_or_else(|| bad("edit.presets.presetManager", format!("no preset named `{name}`"))) -} - -fn preset_manager(s: &mut Session, p: &Value) -> Result { - let cmd = "edit.presets.presetManager"; - let action = str_or(p, "action", "list"); - if action == "list" { - let kinds: Vec<&str> = match p.get("kind").and_then(Value::as_str) { - Some(k) => vec![k], - None => PRESET_KINDS.to_vec(), - }; - let mut out = serde_json::Map::new(); - for k in kinds { - out.insert(k.to_string(), json!(preset_names(s, k).ok_or_else(|| bad(cmd, format!("unknown preset kind `{k}`")))?)); - } - return Ok(Value::Object(out)); - } - let kind = p.get("kind").and_then(Value::as_str).ok_or_else(|| bad(cmd, "missing `kind`"))?.to_string(); - let i = preset_index(s, &kind, p)?; - match action { - "rename" => { - let new = - p.get("newName").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty()).ok_or_else(|| bad(cmd, "missing `newName`"))?.to_string(); - match kind.as_str() { - "brushes" => { - // A renamed built-in is the user's preset now (and persists as one). - let b = &mut s.tools.presets[i]; - b.name = new; - b.builtin = false; - } - "patterns" => s.patterns.items[i].name = new, - _ => s.edit_state.custom_shapes[i].name = new, - } - } - "delete" => match kind.as_str() { - "brushes" => drop(s.tools.presets.remove(i)), - "patterns" => drop(s.patterns.items.remove(i)), - _ => drop(s.edit_state.custom_shapes.remove(i)), - }, - "move" => { - let to = p.get("to").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "missing `to`"))? as usize; - match kind.as_str() { - "brushes" => crate::move_item(&mut s.tools.presets, i, to), - "patterns" => crate::move_item(&mut s.patterns.items, i, to), - _ => crate::move_item(&mut s.edit_state.custom_shapes, i, to), - }; - } - other => return Err(bad(cmd, format!("unknown action `{other}` (list|rename|delete|move)"))), - } - if kind == "brushes" { - s.brush_presets_changed(); - } - Ok(json!({kind.clone(): preset_names(s, &kind)})) -} - -/// Exported preset file (`.pcpresets`, JSON). -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -#[serde(default, rename_all = "camelCase")] -pub struct PresetFile { - pub format: String, - pub version: u32, - pub brushes: Vec, - pub custom_shapes: Vec, -} - -pub const PRESET_FORMAT: &str = "photocraft-presets"; - -fn export_import(s: &mut Session, p: &Value) -> Result { - let cmd = "edit.presets.exportImportPresets"; - let kinds: Vec = p - .get("kinds") - .and_then(Value::as_array) - .map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()) - .unwrap_or_else(|| ["brushes", "customShapes"].iter().map(|k| k.to_string()).collect()); - let want = |k: &str| kinds.iter().any(|x| x == k); - match str_or(p, "action", "export") { - "export" => { - let file = PresetFile { - format: PRESET_FORMAT.into(), - version: 1, - brushes: if want("brushes") { - let mut out: Vec = - s.tools.presets.iter().filter(|b| !b.builtin || bool_or(p, "includeBuiltins", false)).cloned().collect(); - // The file embeds every bitmap: load the tips the preset store keeps (#1843). - for b in &mut out { - s.load_brush_tips(&mut b.brush).map_err(|e| bad(cmd, format!("brush preset `{}`: {e}", b.name)))?; - } - out - } else { - Vec::new() - }, - custom_shapes: if want("customShapes") { s.edit_state.custom_shapes.clone() } else { Vec::new() }, - }; - Ok(json!({"data": file, "brushes": file.brushes.len(), "customShapes": file.custom_shapes.len()})) - } - "import" => { - let data = p.get("data").cloned().ok_or_else(|| bad(cmd, "missing `data` (an exported preset file)"))?; - let data = match data { - Value::String(text) => serde_json::from_str(&text).map_err(|e| bad(cmd, format!("not a preset file: {e}")))?, - v => v, - }; - let file: PresetFile = serde_json::from_value(data).map_err(|e| bad(cmd, format!("not a preset file: {e}")))?; - if file.format != PRESET_FORMAT { - return Err(bad(cmd, format!("not a preset file (format `{}`)", file.format))); - } - let (mut nb, mut ns) = (0, 0); - if want("brushes") { - for mut b in file.brushes { - // Nested folders as the commands bound them (a hand-edited file can't build an - // absurd tree). - b.folder.retain(|f| !f.trim().is_empty()); - b.folder.truncate(crate::brush_preset_cmds::MAX_FOLDER_DEPTH); - match s.tools.presets.iter_mut().find(|x| x.name == b.name) { - Some(x) => *x = b, - None => s.tools.presets.push(b), - } - nb += 1; - } - s.brush_presets_changed(); - } - if want("customShapes") { - for c in file.custom_shapes { - match s.edit_state.custom_shapes.iter_mut().find(|x| x.name == c.name) { - Some(x) => *x = c, - None => s.edit_state.custom_shapes.push(c), - } - ns += 1; - } - } - Ok(json!({"brushes": nb, "customShapes": ns})) - } - other => Err(bad(cmd, format!("unknown action `{other}` (export|import)"))), - } -} - -// ------------------------------------------------------------------ specs - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -pub fn specs() -> Vec { - vec![ - spec!( - "edit.fade", - "Fade…", - ["Edit"], - Some("Cmd+Shift+F"), - r##"{"opacity":0..100=100,"mode":"normal|multiply|screen|overlay|softLight|hardLight|darken|lighten|difference|color|luminosity"}"##, - can_fade, - fade - ), - spec!("edit.purge.undo", "Undo", ["Edit", "Purge"], None, "{}", can_purge_undo, |s, _| purge(s, "undo")), - spec!("edit.purge.clipboard", "Clipboard", ["Edit", "Purge"], None, "{}", can_purge_clipboard, |s, _| purge(s, "clipboard")), - spec!("edit.purge.histories", "Histories", ["Edit", "Purge"], None, "{}", can_purge_histories, |s, _| purge(s, "histories")), - spec!("edit.purge.videoCache", "Video Cache", ["Edit", "Purge"], None, "{}", always, |_, _| Ok( - json!({"purged": [], "bytes": 0, "message": "nothing to purge (no video layers)"}) - )), - spec!("edit.purge.all", "All", ["Edit", "Purge"], None, "{}", can_purge_all, |s, _| purge(s, "all")), - spec!( - "edit.contentAwareFill", - "Content-Aware Fill…", - ["Edit"], - None, - r##"{"sampling":"auto|rectangular|custom","margin":px?,"area":[x,y,w,h]?,"channel":index|name?,"colorAdaptation":"default|none|high|veryHigh","rotationAdaptation":"none|low|medium|high|full","scale":bool=false,"mirror":bool=false,"sampleAllLayers":bool=false,"output":"current|new|duplicate","seed":u64=1}"##, - can_caf, - content_aware_fill - ), - spec!("edit.deleteAndFillSelection", "Delete and Fill Selection", [], None, "{}", can_caf, delete_and_fill), - spec!( - "edit.contentAwareScale", - "Content-Aware Scale", - ["Edit"], - Some("Cmd+Alt+Shift+C"), - r##"{"width":px?,"height":px?,"scaleX":1..400=100,"scaleY":1..400=100,"x":px?,"y":px?,"amount":0..100=100,"protect":"none"|channel index|name,"protectSkinTones":bool=false}"##, - |s| pixel_layer(s).map(|_| ()), - content_aware_scale - ), - spec!( - "edit.defineBrushPreset", - "Define Brush Preset…", - ["Edit"], - None, - r##"{"name":text}"##, - |s| if s.is_enabled("brush.defineFromSelection") { Ok(()) } else { Err("select pixels to define a brush from".into()) }, - define_brush_preset - ), - spec!( - "edit.defineCustomShape", - "Define Custom Shape…", - ["Edit"], - None, - r##"{"name":text,"path":"work|"?}"##, - can_define_shape, - define_custom_shape - ), - spec!( - "edit.findAndReplaceText", - "Find and Replace Text…", - ["Edit"], - None, - r##"{"find":text,"replace":text,"action":"changeAll|find|change|changeFind","caseSensitive":bool=false,"wholeWord":bool=false,"forward":bool=true,"allLayers":bool=true}"##, - has_type_layer, - find_replace - ), - spec!( - "edit.presets.presetManager", - "Preset Manager…", - ["Edit", "Presets"], - None, - r##"{"action":"list|rename|delete|move","kind":"brushes|customShapes|patterns","index":n?,"name":str?,"newName":str?,"to":n?}"##, - always, - preset_manager - ), - spec!( - "edit.presets.exportImportPresets", - "Export/Import Presets…", - ["Edit", "Presets"], - None, - r##"{"action":"export|import","kinds":["brushes","customShapes"]?,"data":json (import),"includeBuiltins":bool=false}"##, - always, - export_import - ), - ] -} - -/// The custom shape library (for the Custom Shape tool). -pub fn custom_shapes(s: &Session) -> &[CustomShape] { - &s.edit_state.custom_shapes -} - -#[cfg(test)] -#[path = "edit_menu_cmds/tests.rs"] -mod tests; diff --git a/crates/engine/src/error.rs b/crates/engine/src/error.rs new file mode 100644 index 0000000000..323d3f5203 --- /dev/null +++ b/crates/engine/src/error.rs @@ -0,0 +1,34 @@ +use super::LayerId; +use crate::command_hints; + +#[derive(Debug, thiserror::Error)] +pub enum EngineError { + /// Displays with the nearest registered ids, if any ([`command_hints`], #3674). + #[error("unknown command `{id}`{hint}", id = .0, hint = command_hints::unknown_command_hint(.0))] + UnknownCommand(String), + #[error("command `{0}` is not available right now: {1}")] + Disabled(String, String), + #[error("invalid parameters for `{cmd}`: {msg}")] + BadParams { cmd: String, msg: String }, + #[error("no active document")] + NoDocument, + #[error("no such layer {0:?}")] + NoLayer(LayerId), + /// [`Self::NoLayer`] as a command reports it: with the layers the document does have, as + /// ids are not positions (#5058). + #[error("no such layer {}; {known}", id.0)] + UnknownLayer { id: LayerId, known: String }, + #[error("{0}")] + Other(String), + /// A background job was cancelled (see [`jobs`]); nothing changed. + #[error("cancelled")] + Cancelled, +} + +pub type Result = std::result::Result; + +impl From for EngineError { + fn from(error: photocraft_raster::AllocationError) -> Self { + Self::Other(format!("not enough memory for this operation ({error}); the document was not changed")) + } +} diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs deleted file mode 100644 index 43828b76b1..0000000000 --- a/crates/engine/src/file_cmds.rs +++ /dev/null @@ -1,1764 +0,0 @@ -//! File-menu commands beyond New/Close: Close All / Others, Revert, Save a Copy, Open As, Place -//! Embedded / Linked, File Info, the Automate and Scripts items (Fit Image, Conditional Mode -//! Change, Batch, Image Processor, Load Files into Stack, Flatten All Layer Effects / Masks), -//! Export › Layers to Files and Color Lookup Tables, plus the document-level guide layouts of the -//! View menu (New Guide Layout, New Guides From Shape, Clear Canvas Guides). -//! -//! Commands that touch the file system take explicit paths (the UI asks for them with its own -//! pickers) and are unavailable on the web, where there is no file system; their byte-level -//! cores ([`place_bytes`], [`open_bytes_as`]) work everywhere. - -use std::sync::Arc; - -use photocraft_algo::resample::{Resample, resize_surface, translate_surface}; -use photocraft_color::{ColorMode, PixelFormat}; -use photocraft_doc::{Affine, Document, Layer, LayerContent, LayerId, SmartObject, SmartSource}; -use photocraft_geom::Rect; -use photocraft_raster::{Surface, from_rgba_into}; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, layer_param}; -use crate::{EngineError, Result, Session}; - -mod batch_naming; - -// ---------- predicates ---------- - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} - -/// The file system is reachable (not on the web). -pub(crate) fn native(_: &Session) -> std::result::Result<(), String> { - if cfg!(target_arch = "wasm32") { Err("not available on the web (no file system)".into()) } else { Ok(()) } -} - -pub(crate) fn native_doc(s: &Session) -> std::result::Result<(), String> { - native(s)?; - has_doc(s) -} - -fn can_revert(s: &Session) -> std::result::Result<(), String> { - native(s)?; - let d = s.active().ok_or("no document open")?; - let path = d.path.as_deref().ok_or("the document has never been saved")?; - if !is_file(path) { - return Err(format!("{path} is not a file on disk")); - } - Ok(()) -} - -fn has_adjustments(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - if d.doc.layers.iter().any(|l| l.visible && matches!(l.content, LayerContent::Adjustment(_))) { - Ok(()) - } else { - Err("the document has no visible adjustment layers to export".into()) - } -} - -fn has_shape(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - match d.active_layer.and_then(|id| d.doc.layer(id)).map(|l| &l.content) { - Some(LayerContent::Shape(_)) => Ok(()), - _ => Err("the active layer is not a shape layer".into()), - } -} - -// ---------- file system (native only) ---------- - -#[cfg(not(target_arch = "wasm32"))] -fn is_file(path: &str) -> bool { - std::path::Path::new(path).is_file() -} -#[cfg(target_arch = "wasm32")] -fn is_file(_path: &str) -> bool { - false -} - -#[cfg(not(target_arch = "wasm32"))] -/// Every file the engine opens is read here: in bounded reads, with a clear error when it does -/// not fit in memory (see [`photocraft_format::read`]). -pub(crate) fn read_file(path: &str) -> Result> { - photocraft_format::read_file(std::path::Path::new(path)).map_err(|e| EngineError::Other(format!("{path}: {e}"))) -} -#[cfg(target_arch = "wasm32")] -pub(crate) fn read_file(path: &str) -> Result> { - Err(EngineError::Other(format!("cannot read {path}: no file system on the web"))) -} - -#[cfg(not(target_arch = "wasm32"))] -/// Every file the engine writes goes through here: crash-safe (temp file + fsync + rename, see -/// [`photocraft_format::atomic`]), so a failed save never destroys the previous file. -pub(crate) fn write_file(path: &str, bytes: &[u8]) -> Result<()> { - photocraft_format::atomic_write(std::path::Path::new(path), bytes).map_err(|e| EngineError::Other(e.to_string())) -} -#[cfg(target_arch = "wasm32")] -pub(crate) fn write_file(path: &str, _bytes: &[u8]) -> Result<()> { - Err(EngineError::Other(format!("cannot write {path}: no file system on the web"))) -} - -/// Files of a folder that look like images we can open, sorted by name. -#[cfg(not(target_arch = "wasm32"))] -pub fn list_images(dir: &str) -> Result> { - let rd = std::fs::read_dir(dir).map_err(|e| EngineError::Other(format!("{dir}: {e}")))?; - let mut out: Vec = rd - .filter_map(|e| e.ok()) - .map(|e| e.path()) - .filter(|p| p.is_file() && p.extension().and_then(|x| x.to_str()).is_some_and(|x| OPENABLE.contains(&x.to_ascii_lowercase().as_str()))) - .map(|p| p.to_string_lossy().into_owned()) - .collect(); - out.sort(); - Ok(out) -} -#[cfg(target_arch = "wasm32")] -pub fn list_images(dir: &str) -> Result> { - Err(EngineError::Other(format!("cannot list {dir}: no file system on the web"))) -} - -/// Extensions the batch commands pick up from a folder. -#[cfg(not(target_arch = "wasm32"))] -const OPENABLE: &[&str] = &[ - "pdn", "ora", "kra", "krz", "psd", "psb", "pcraft", "png", "jpg", "jpeg", "tif", "tiff", "webp", "gif", "bmp", "tga", "exr", "hdr", "qoi", "ico", "pnm", - "ppm", "pgm", "fits", "fit", "fts", "dpx", "heic", "heif", "hif", "dng", "cr2", "nef", "nrw", "arw", "pef", "svg", "svgz", "af", "afdesign", "afphoto", - "afpub", -]; - -/// Whether saving `doc` as a TIFF writes Photoshop layer data (anything beyond a lone -/// Background layer). The UI asks before such a save when the "ask before saving layered TIFF" -/// preference is on; see `photocraft_io::tiff_layers`. -pub fn tiff_would_write_layers(doc: &Document) -> bool { - photocraft_io::tiff_layers::would_write_layers(doc) -} - -pub(crate) fn file_name(path: &str) -> String { - path.rsplit(['/', '\\']).next().unwrap_or(path).to_string() -} - -impl crate::DocState { - /// Complete a successful document save. File identity is not a history step; callers must - /// finish the write first and must not call this for a copy or export. - pub fn saved_to(&mut self, path: String) { - let name = file_name(&path); - if self.doc.name != name { - Arc::make_mut(&mut self.doc).name = name; - } - self.path = Some(path); - self.saved_revision = self.revision; - } -} - -/// The lower-case extension of the file name in `path` (none for `.hidden` or `name`). -pub fn extension(path: &str) -> Option { - file_name(path).rsplit_once('.').filter(|(base, ext)| !base.is_empty() && !ext.is_empty()).map(|(_, ext)| ext.to_ascii_lowercase()) -} - -/// Whether a save without a new path may write back to `path` as far as its format goes: only -/// layered files (PSD, PSB, OpenRaster, .pcraft). A flat file goes through Save As instead, so it -/// is never flattened over the original. Whether this document fits is [`holds_at`]. -pub fn saves_in_place(path: &str) -> bool { - extension(path).is_some_and(|ext| layered_extension(&ext)) -} - -/// Whether the lower-case extension `ext` names a layered document format (PSD, PSB, OpenRaster, -/// .pcraft). A save to one becomes the document's file; any other format is a copy for automation -/// saves, desktop and headless alike (#1547, #2579). -pub fn layered_extension(ext: &str) -> bool { - matches!(ext, "psd" | "psb" | "pcraft" | "ora") -} - -/// Whether a file in format `ext` (lower-case) holds the whole of `doc`, so it can be the -/// document's own file: PSD, PSB and .pcraft always; OpenRaster only when it drops nothing (no -/// adjustment layers, styles, masks…, [`photocraft_io::ora_losses`]). A save to a format that -/// doesn't hold the document is a copy, and File › Save goes through Save As, so an editable -/// original is never overwritten with less than it had (#3806). -pub fn holds(doc: &photocraft_doc::Document, ext: &str) -> bool { - match ext { - "psd" | "psb" | "pcraft" => true, - "ora" => photocraft_io::ora_losses(doc).is_empty(), - _ => false, - } -} - -/// [`holds`] for the file at `path`. -pub fn holds_at(doc: &photocraft_doc::Document, path: &str) -> bool { - extension(path).is_some_and(|ext| holds(doc, &ext)) -} - -/// What a save of `doc` to `path` would leave out when its format can't hold it: OpenRaster's -/// [`photocraft_io::ora_losses`]; empty for other formats. -pub fn losses_at(doc: &photocraft_doc::Document, path: &str) -> Vec { - match extension(path).as_deref() { - Some("ora") => photocraft_io::ora_losses(doc), - _ => Vec::new(), - } -} - -/// Whether `path` names a document template (.psdt). A template opens as a new untitled document -/// without its path, so a save never writes over the template. -pub fn is_template(path: &str) -> bool { - extension(path).as_deref() == Some("psdt") -} - -/// The first "`base`-N" that isn't `taken`. -pub fn untitled_name(base: &str, taken: impl Fn(&str) -> bool) -> String { - (1..).map(|i| format!("{base}-{i}")).find(|n| !taken(n)).unwrap_or_default() -} - -/// The name a document opened from `path` gets when `path` is a template: the first "Untitled-N" -/// no open document has. -pub fn template_name(s: &Session, path: &str) -> Option { - is_template(path).then(|| untitled_name("Untitled", |n| s.documents().iter().any(|d| d.doc.name == n))) -} - -pub(crate) fn stem(path: &str) -> String { - let n = file_name(path); - match n.rfind('.') { - Some(i) if i > 0 => n[..i].to_string(), - _ => n, - } -} - -pub(crate) fn join(dir: &str, name: &str) -> String { - if dir.is_empty() { - name.to_string() - } else if dir.ends_with('/') || dir.ends_with('\\') { - format!("{dir}{name}") - } else { - format!("{dir}/{name}") - } -} - -/// A file-name-safe version of a layer name. -pub(crate) fn sanitize(name: &str) -> String { - let s: String = name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_' | ' ' | '.') { c } else { '_' }).collect(); - let s = s.trim().trim_matches('.').to_string(); - if s.is_empty() { "layer".into() } else { s } -} - -pub(crate) fn import(name: &str, bytes: &[u8]) -> Result { - crate::allocation::checkpoint("importing document")?; - let r = photocraft_io::import(name, bytes).map_err(|e| EngineError::Other(format!("{name}: {e}")))?; - // Auxiliary imports return only a document and cannot surface the preview's fidelity warning. - // Open has its own warning-preserving path; never silently place or process a thumbnail. - if r.preview_only { - return Err(EngineError::Other(format!( - "{name}: only this Affinity file's embedded preview could be read; open it with File › Open to see the warning, or export PSD or PNG from Affinity before using it here" - ))); - } - Ok(r.document) -} - -/// What a headless save writes beyond the format: JPEG quality, TIFF layers, Targa bits, PSD -/// previews and the PSD merged image. -#[derive(Debug, Clone, Copy)] -pub(crate) struct SaveOpts { - /// Photoshop's 0–12 JPEG scale. - pub quality: Option, - /// TIFF: keep the layers. Off unless a command's params ask (`"tiffLayers": true`). - pub tiff_layers: bool, - /// Targa: 24 or 32 bits per pixel (`"tgaBits"`); unset, 32 with an alpha channel or - /// transparency, else 24. - pub tga_bits: Option, - /// DPX: 8, 10, 12 or 16 bits per sample (`"dpxBits"`); unset, 8 for an 8-bit document, else 16. - pub dpx_bits: Option, - /// PSD/PSB: embed the thumbnail resource (Preferences › File Handling › Image Previews). - pub image_previews: bool, - /// PSD/PSB: write the real merged image (Preferences › File Handling › Maximize PSD and PSB - /// File Compatibility). - pub psd_maximize_compatibility: bool, -} - -impl Default for SaveOpts { - fn default() -> Self { - SaveOpts { quality: None, tiff_layers: false, tga_bits: None, dpx_bits: None, image_previews: true, psd_maximize_compatibility: true } - } -} - -impl SaveOpts { - /// `quality`, `tiffLayers` and `tgaBits` from command `cmd`'s params (a `tgaBits` other than - /// 24 or 32 is an error); the image previews and the PSD merged image from the session's File - /// Handling preferences (Ask When Saving has no dialog here, so it saves them, as Photoshop's - /// Save As checkbox and its Maximize Compatibility checkbox both start on). - pub(crate) fn from_params(s: &Session, p: &Value, cmd: &str) -> Result { - let file_handling = &s.prefs().file_handling; - Ok(SaveOpts { - quality: f64_param(p, "quality"), - tiff_layers: p.get("tiffLayers").and_then(Value::as_bool).unwrap_or(false), - tga_bits: tga_bits_param(p, cmd)?, - dpx_bits: dpx_bits_param(p, cmd)?, - image_previews: file_handling.image_previews != crate::prefs::Ask::Never, - psd_maximize_compatibility: file_handling.maximize_psd_compatibility != crate::prefs::Ask::Never, - }) - } - - pub(crate) fn or_quality(mut self, q: f64) -> Self { - self.quality = self.quality.or(Some(q)); - self - } -} - -impl From> for SaveOpts { - fn from(quality: Option) -> Self { - SaveOpts { quality, ..Default::default() } - } -} - -/// The Targa bits per pixel in `p` (`"tgaBits": 24 | 32`; absent or null: the automatic choice). -/// Anything else is a bad parameter of command `cmd`. Shared by every save that takes it (the -/// engine's commands, the app's Save As and control channel, the headless server). -pub fn tga_bits_param(p: &Value, cmd: &str) -> Result> { - match p.get("tgaBits") { - None | Some(Value::Null) => Ok(None), - Some(v) => v - .as_u64() - .and_then(photocraft_io::TgaBits::from_bits) - .map(Some) - .ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: format!("`tgaBits` must be 24 or 32, got {v}") }), - } -} - -/// The DPX bits per sample in `p` (`"dpxBits": 8 | 10 | 12 | 16`; absent or null: the document's -/// depth). Anything else is a bad parameter of command `cmd`. -pub fn dpx_bits_param(p: &Value, cmd: &str) -> Result> { - match p.get("dpxBits") { - None | Some(Value::Null) => Ok(None), - Some(v) => v - .as_u64() - .and_then(photocraft_io::DpxBits::from_bits) - .map(Some) - .ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: format!("`dpxBits` must be 8, 10, 12 or 16, got {v}") }), - } -} - -/// Encodes `doc` for `path`'s extension. -pub(crate) fn encode(doc: &Document, path: &str, save: impl Into) -> Result<(Vec, Vec)> { - crate::allocation::checkpoint("saving document")?; - let save = save.into(); - let mut opts = photocraft_io::ExportOptions { - tiff_layers: save.tiff_layers, - tga_bits: save.tga_bits, - image_previews: save.image_previews, - psd_maximize_compatibility: save.psd_maximize_compatibility, - ..Default::default() - }; - // No quality chosen: write the file the way it was opened (a JPEG at its own quality, a WebP - // lossy or lossless as it was), as Photoshop's Save does. - photocraft_io::apply_file_encoding(doc, &mut opts.encode); - opts.encode.dpx_bits = save.dpx_bits; - if let Some(q) = save.quality { - let q = (q.clamp(0.0, 12.0) / 12.0 * 99.0 + 1.0).round() as u8; - opts.encode.jpeg_quality = q; - // A quality on a WebP save asks for the lossy encoder; the default WebP stays lossless. - opts.encode.webp_quality = q; - opts.encode.webp_lossless = false; - } - photocraft_io::export(doc, path, &opts).map(|r| (r.bytes, r.warnings)).map_err(|e| EngineError::Other(format!("{path}: {e}"))) -} - -pub(crate) fn save_doc(doc: &Document, path: &str, save: impl Into) -> Result> { - let (bytes, warnings) = encode(doc, path, save)?; - write_file(path, &bytes)?; - Ok(warnings) -} - -pub(crate) fn str_param<'a>(p: &'a Value, key: &str, cmd: &str) -> Result<&'a str> { - p.get(key).and_then(Value::as_str).filter(|v| !v.is_empty()).ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: format!("missing \"{key}\"") }) -} - -pub(crate) fn f64_param(p: &Value, key: &str) -> Option { - p.get(key).and_then(Value::as_f64).filter(|v| v.is_finite()) -} - -/// A size parameter; zero or negative means "not set" (dialogs send 0 for empty fields). -fn size_param(p: &Value, key: &str) -> Option { - f64_param(p, key).filter(|v| *v > 0.0) -} - -// ---------- pixels ---------- - -/// A composite buffer as a surface in `fmt` (straight RGBA → the format's model and depth). -pub(crate) fn buffer_surface(buf: &photocraft_compose::Buffer, fmt: PixelFormat) -> Surface { - let n = fmt.channels(); - let mut data = vec![0.0f32; buf.px.len() * n]; - for (p, out) in buf.px.iter().zip(data.chunks_exact_mut(n)) { - from_rgba_into(&fmt, *p, out); - } - let mut s = Surface::new(fmt); - if !buf.rect.is_empty() { - s.write_region(buf.rect, &data); - } - s.prune(); - s -} - -/// The flattened image of `doc` as a surface in `fmt`, placed at the origin. -pub(crate) fn flattened(doc: &Document, fmt: PixelFormat) -> Surface { - photocraft_compose::flatten_to_surface(doc, fmt, None) -} - -// ---------- close / revert / save a copy / open as ---------- - -fn close_all(s: &mut Session) -> Result { - let n = s.documents().len(); - while !s.documents().is_empty() { - s.close(s.documents().len() - 1); - } - Ok(json!({"closed": n})) -} - -fn close_others(s: &mut Session, p: &Value) -> Result { - let keep = p.get("document").and_then(Value::as_u64).map(|v| v as usize).or(s.active_index()).ok_or(EngineError::NoDocument)?; - if keep >= s.documents().len() { - return Err(EngineError::BadParams { cmd: "file.closeOthers".into(), msg: format!("no document {keep}") }); - } - let n = s.documents().len() - 1; - for i in (0..s.documents().len()).rev() { - if i != keep { - s.close(i); - } - } - s.set_active(0); - Ok(json!({"closed": n})) -} - -/// File › Revert: reload the saved file as one history step (undoable, like Photoshop's -/// "Revert" history state); the document is clean afterwards. -fn revert(s: &mut Session) -> Result { - let path = s.active().and_then(|d| d.path.clone()).ok_or(EngineError::Other("the document has never been saved".into()))?; - let bytes = read_file(&path)?; - let fresh = import(&file_name(&path), &bytes)?; - s.edit("Revert", |doc, active| { - let (id, name) = (doc.id, doc.name.clone()); - *doc = fresh; - // Keep the identity (colour state, views and caches are keyed by it) and the tab name. - doc.id = id; - doc.name = name; - *active = doc.top_layer(); - Ok(()) - })?; - let st = s.active_mut().ok_or(EngineError::NoDocument)?; - st.saved_revision = st.revision; - Ok(json!({"path": path, "layers": st.doc.layers.len()})) -} - -fn save_a_copy(s: &mut Session, p: &Value) -> Result { - let path = str_param(p, "path", "file.saveACopy")?.to_string(); - let d = s.active().ok_or(EngineError::NoDocument)?; - let mut doc = (*d.doc).clone(); - if p.get("layers").and_then(Value::as_bool) == Some(false) { - // "Layers" unchecked in Save a Copy: write the flattened image. - let fmt = doc.pixel_format(); - let px = flattened(&doc, fmt); - doc.layers = vec![Layer::new("Background", LayerContent::Raster(px))]; - } - let warnings = save_doc(&doc, &path, SaveOpts::from_params(s, p, "file.saveACopy")?)?; - Ok(json!({"path": path, "warnings": warnings})) -} - -/// Open a file's bytes, decoding them as the format `as_ext` (Open As) when given. -/// EXR/HDR rarely states its colour space, so io tags it with the linear-sRGB default. -/// With File Handling's ACEScg option on, such an EXR opens as ACEScg instead (AP1, -/// linear) — the working space ACES render pipelines write. Only the io default is -/// replaced; an embedded or EXIF-declared profile is kept untouched. -fn tag_exr_colorspace(s: &Session, name: &str, doc: &mut Document) { - if !name.to_ascii_lowercase().ends_with(".exr") || !s.prefs.get().file_handling.exr_aces_cg { - return; - } - let linear = photocraft_cms::Builtin::LinearSrgb.profile().to_bytes(); - if doc.icc_profile.as_ref().map(|v| v.as_slice()) == Some(linear.as_slice()) { - doc.icc_profile = Some(photocraft_cms::Builtin::AcesCg.profile().to_bytes()); - } -} - -pub fn open_bytes_as(s: &mut Session, name: &str, bytes: &[u8], as_ext: Option<&str>, path: Option) -> Result { - crate::allocation::checkpoint("importing document")?; - let decode_name = match as_ext { - Some(ext) => format!("{}.{}", stem(name), ext.trim_start_matches('.')), - None => name.to_string(), - }; - let r = photocraft_io::import(&decode_name, bytes).map_err(|e| EngineError::Other(format!("{decode_name}: {e}")))?; - let mut doc = r.document; - tag_exr_colorspace(s, &decode_name, &mut doc); - let path = match template_name(s, name) { - Some(untitled) => { - doc.name = untitled; - None - } - None => { - doc.name = file_name(name); - path - } - }; - // Color Settings policies (preserve / convert / discard the embedded profile). - let (i, color) = s.open_document(doc, path.filter(|_| !r.source_read_only)); - if let Some(st) = s.active_mut() { - st.source_read_only = r.source_read_only; - } - // Import notes (e.g. how a camera raw was developed, or that only its preview opened). - Ok(json!({"document": i, "color": color, "warnings": r.warnings})) -} - -fn open_as(s: &mut Session, p: &Value) -> Result { - let path = str_param(p, "path", "file.openAs")?.to_string(); - let bytes = read_file(&path)?; - let as_ext = p.get("as").or_else(|| p.get("format")).and_then(Value::as_str); - open_bytes_as(s, &path, &bytes, as_ext, Some(path.clone())) -} - -// ---------- place ---------- - -/// History label of an embedded place. -pub const PLACE_EMBEDDED: &str = "Place Embedded"; - -/// Place a file's bytes as a smart object layer, centred, and (with Preferences ▸ General ▸ -/// Resize Image During Place, on by default) scaled down to fit when larger than the canvas. -/// `linked` makes it a linked smart object that refers to that path instead of embedding the bytes. -/// With Preferences ▸ General ▸ Always Create Smart Objects When Placing off, an embedded place -/// lands as a plain pixel layer instead (an explicit `"smartObject": false` asks for one). -pub fn place_bytes(s: &mut Session, name: &str, bytes: Vec, linked: Option, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let (cw, ch) = (d.doc.size.width as f64, d.doc.size.height as f64); - let fmt = d.doc.pixel_format(); - let vector = photocraft_io::svg::is_svg(&bytes); - let max_svg_group_depth = s.prefs().file_handling.rasterize_svg_groups_deeper_than as usize; - let imported = photocraft_io::import_with_svg_group_depth(name, &bytes, max_svg_group_depth).map_err(|e| EngineError::Other(format!("{name}: {e}")))?; - if imported.preview_only { - return Err(EngineError::Other(format!( - "{name}: only this Affinity file's embedded preview could be read; open it with File › Open to see the warning, or export PSD or PNG from Affinity before using it here" - ))); - } - let src = imported.document; - let (w, h) = (src.size.width as f64, src.size.height as f64); - // The implicit `fit` is Preferences ▸ General ▸ Resize Image During Place (on by default). - let fit = p.get("fit").and_then(Value::as_bool).unwrap_or(s.prefs().general.resize_image_during_place); - let scale = match f64_param(p, "scale") { - Some(k) => (k / 100.0).max(1e-4), - None if fit && (w > cw || h > ch) => (cw / w).min(ch / h), - None => 1.0, - }; - let center = match p.get("center").and_then(Value::as_array) { - Some(c) if c.len() == 2 => (c[0].as_f64().unwrap_or(cw / 2.0), c[1].as_f64().unwrap_or(ch / 2.0)), - _ => (cw / 2.0, ch / 2.0), - }; - let (dx, dy) = ((center.0 - w * scale / 2.0).round(), (center.1 - h * scale / 2.0).round()); - let mut px = flattened(&src, fmt); - if (scale - 1.0).abs() > 1e-9 { - px = resize_surface(&px, scale, scale, Resample::Bicubic); - } - let px = translate_surface(&px, dx as i32, dy as i32); - // Preferences ▸ General ▸ Always Create Smart Objects When Placing (on by default): off, an - // embedded place lands as a plain pixel layer. A linked place is always a smart object. - let smart = linked.is_some() || p.get("smartObject").and_then(Value::as_bool).unwrap_or(s.prefs().general.always_create_smart_objects_when_placing); - let source = match linked { - Some(path) => SmartSource::Linked { path }, - None => SmartSource::Embedded { file_name: file_name(name), bytes: Arc::new(bytes) }, - }; - let layer_name = stem(name); - let label = if matches!(&source, SmartSource::Linked { .. }) { "Place Linked" } else { PLACE_EMBEDDED }; - let id = s.edit(label, |doc, active| { - let layer = if smart { - let mut so = SmartObject::new(source, Affine { m: [scale, 0.0, 0.0, scale, dx, dy] }, Some(px)); - // A vector source (SVG) renders at its placement scale instead of as a resampled raster. - if vector && let Some(sharp) = crate::smart_cmds::render(doc, &so)? { - so.cache = Some(sharp); - } - Layer::new(layer_name, LayerContent::Smart(so)) - } else { - Layer::new(layer_name, LayerContent::Raster(px)) - }; - let id = doc.insert_above(*active, layer); - *active = Some(id); - Ok(id) - })?; - Ok(json!({"layer": id.0, "scale": scale * 100.0, "bounds": [dx, dy, dx + w * scale, dy + h * scale], "smartObject": smart})) -} - -fn place(s: &mut Session, p: &Value, linked: bool) -> Result { - let cmd = if linked { "file.placeLinked" } else { "file.placeEmbedded" }; - let path = str_param(p, "path", cmd)?.to_string(); - let bytes = read_file(&path)?; - place_bytes(s, &path, bytes, linked.then(|| path.clone()), p) -} - -// ---------- File Info (XMP) ---------- - -/// The File Info fields we edit, as (JSON key, XMP property, container kind). -const INFO_FIELDS: [(&str, &str, &str); 6] = [ - ("title", "dc:title", "Alt"), - ("author", "dc:creator", "Seq"), - ("description", "dc:description", "Alt"), - ("keywords", "dc:subject", "Bag"), - ("copyright", "dc:rights", "Alt"), - ("authorTitle", "photoshop:AuthorsPosition", ""), -]; - -fn xml_escape(s: &str) -> String { - s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) -} - -fn xml_unescape(s: &str) -> String { - s.replace("<", "<").replace(">", ">").replace(""", "\"").replace("'", "'").replace("&", "&") -} - -/// The element `…` (first occurrence) as (start, end, inner). -fn find_element<'a>(xmp: &'a str, tag: &str) -> Option<(usize, usize, &'a str)> { - let open = format!("<{tag}"); - let mut from = 0; - while let Some(i) = xmp[from..].find(&open).map(|i| i + from) { - let after = xmp[i + open.len()..].chars().next(); - if matches!(after, Some('>' | ' ' | '/' | '\n' | '\r' | '\t')) { - let gt = xmp[i..].find('>')? + i; - if xmp[..gt].ends_with('/') { - return Some((i, gt + 1, "")); - } - let close = format!(""); - let c = xmp[gt..].find(&close)? + gt; - return Some((i, c + close.len(), &xmp[gt + 1..c])); - } - from = i + open.len(); - } - None -} - -/// `rdf:li` values of a container (or the element text for simple properties). -fn li_values(inner: &str) -> Vec { - let mut out = Vec::new(); - let mut rest = inner; - while let Some((a, b, v)) = find_element(rest, "rdf:li") { - out.push(xml_unescape(v.trim())); - rest = &rest[b..]; - let _ = a; - } - if out.is_empty() && !inner.contains('<') && !inner.trim().is_empty() { - out.push(xml_unescape(inner.trim())); - } - out -} - -/// Attribute form `prop="value"` on an `rdf:Description`. -fn find_attr<'a>(xmp: &'a str, prop: &str) -> Option<(usize, usize, &'a str)> { - let key = format!(" {prop}=\""); - let i = xmp.find(&key)?; - let v0 = i + key.len(); - let v1 = xmp[v0..].find('"')? + v0; - Some((i, v1 + 1, &xmp[v0..v1])) -} - -/// File Info fields read from an XMP packet. -pub fn read_file_info(xmp: Option<&str>) -> Value { - let mut m = serde_json::Map::new(); - let x = xmp.unwrap_or(""); - for (key, prop, kind) in INFO_FIELDS { - let vals = match find_element(x, prop) { - Some((_, _, inner)) => li_values(inner), - None => find_attr(x, prop).map(|(_, _, v)| vec![xml_unescape(v)]).unwrap_or_default(), - }; - let v = if kind == "Bag" { - json!(vals) - } else if kind == "Seq" { - json!(vals.join("; ")) - } else { - json!(vals.into_iter().next().unwrap_or_default()) - }; - m.insert(key.into(), v); - } - let marked = - find_element(x, "xmpRights:Marked").map(|(_, _, v)| v.trim().to_string()).or_else(|| find_attr(x, "xmpRights:Marked").map(|(_, _, v)| v.to_string())); - m.insert( - "copyrightStatus".into(), - json!(match marked.as_deref() { - Some("True") => "copyrighted", - Some("False") => "publicDomain", - _ => "unknown", - }), - ); - let url = find_element(x, "xmpRights:WebStatement") - .map(|(_, _, v)| xml_unescape(v.trim())) - .or_else(|| find_attr(x, "xmpRights:WebStatement").map(|(_, _, v)| xml_unescape(v))); - m.insert("copyrightUrl".into(), json!(url.unwrap_or_default())); - Value::Object(m) -} - -/// File Info's read-only Camera Data, as (result key, English label) in display order. -pub const CAMERA_FIELDS: [(&str, &str); 9] = [ - ("make", "Make"), - ("model", "Model"), - ("dateTaken", "Date Taken"), - ("exposure", "Exposure"), - ("fNumber", "F-Stop"), - ("iso", "ISO Speed"), - ("focalLength", "Focal Length"), - ("focalLength35mm", "Focal Length (35 mm)"), - ("lens", "Lens"), -]; - -/// `v` with at most `decimals` decimals and no trailing zeros (33.10 → "33.1", 8.0 → "8"). -fn short_num(v: f64, decimals: usize) -> String { - let s = format!("{v:.decimals$}"); - if s.contains('.') { s.trim_end_matches('0').trim_end_matches('.').to_string() } else { s } -} - -/// Exposure time as cameras print it: "1/250 s" for fractions of a second, "0.4 s" or "2 s" -/// when the reciprocal isn't a whole number or the exposure is a second or longer. -fn exposure_text(t: f64) -> Option { - if !(t.is_finite() && t > 0.0) { - return None; - } - let r = 1.0 / t; - if t < 1.0 && (r - r.round()).abs() <= r * 0.01 { - return Some(format!("1/{} s", r.round())); - } - Some(format!("{} s", short_num(t, if t < 1.0 { 2 } else { 1 }))) -} - -/// EXIF (`2024:02:17 12:40:19`) or XMP (`2024-02-17T12:40:19.00+01:00`) dates as -/// `2024-02-17 12:40:19`; anything else is shown as stored. -fn date_text(s: &str) -> String { - let b = s.trim().as_bytes(); - let digits = |r: &[usize]| r.iter().all(|&i| b.get(i).is_some_and(u8::is_ascii_digit)); - let sep = |i: usize, set: &[u8]| b.get(i).is_some_and(|c| set.contains(c)); - let ch = |i: usize| b.get(i).map_or('0', |&c| c as char); - let part = |r: std::ops::Range| r.map(ch).collect::(); - if !(digits(&[0, 1, 2, 3, 5, 6, 8, 9]) && sep(4, b":-") && sep(7, b":-")) { - return s.trim().to_string(); - } - let date = format!("{}-{}-{}", part(0..4), part(5..7), part(8..10)); - if digits(&[11, 12, 14, 15, 17, 18]) && sep(10, b" T") && sep(13, b":") && sep(16, b":") { format!("{date} {}", part(11..19)) } else { date } -} - -/// A single XMP value: the first `rdf:li` of a container, element text or attribute. -fn xmp_value(x: &str, prop: &str) -> Option { - let v = match find_element(x, prop) { - Some((_, _, inner)) => li_values(inner).into_iter().next(), - None => find_attr(x, prop).map(|(_, _, v)| xml_unescape(v)), - }?; - let v = v.trim().to_string(); - (!v.is_empty()).then_some(v) -} - -/// An XMP number: a rational "331/10" or a decimal. -fn xmp_number(x: &str, prop: &str) -> Option { - let v = xmp_value(x, prop)?; - let n = match v.split_once('/') { - Some((a, b)) => { - let (a, b) = (a.trim().parse::().ok()?, b.trim().parse::().ok()?); - (b != 0.0).then(|| a / b)? - } - None => v.parse::().ok()?, - }; - n.is_finite().then_some(n) -} - -/// File Info's read-only Camera Data (keys of [`CAMERA_FIELDS`], present ones only), formatted -/// for display. Read from the EXIF block (JPEG APP1, PSD resource 1058, …); properties it lacks -/// fall back to the XMP `tiff:`/`exif:`/`aux:` copies (files without an EXIF block). -pub fn camera_data(exif: Option<&[u8]>, xmp: Option<&str>) -> Value { - let e = exif.map(photocraft_algo::exif::read).unwrap_or_default(); - let x = xmp.unwrap_or(""); - let text = |v: Option, props: &[&str]| v.or_else(|| props.iter().find_map(|p| xmp_value(x, p))); - let num = - |v: Option, props: &[&str]| v.filter(|v| v.is_finite() && *v > 0.0).or_else(|| props.iter().find_map(|p| xmp_number(x, p))).filter(|v| *v > 0.0); - let values = [ - text(e.make, &["tiff:Make"]), - text(e.model, &["tiff:Model"]), - text(e.date_taken, &["exif:DateTimeOriginal"]).map(|d| date_text(&d)), - num(e.exposure_time, &["exif:ExposureTime"]).and_then(exposure_text), - num(e.f_number, &["exif:FNumber"]).map(|n| format!("f/{}", short_num(n, 1))), - num(e.iso, &["exif:ISOSpeedRatings", "exifEX:PhotographicSensitivity"]).map(|n| format!("ISO {}", n.round())), - num(e.focal_length, &["exif:FocalLength"]).map(|f| format!("{} mm", short_num(f, 1))), - num(e.focal_length_35mm, &["exif:FocalLengthIn35mmFilm"]).map(|f| format!("{} mm", short_num(f, 1))), - text(e.lens, &["exifEX:LensModel", "aux:Lens"]), - ]; - let mut m = serde_json::Map::new(); - for ((key, _), v) in CAMERA_FIELDS.into_iter().zip(values) { - // Hostile files can carry megabytes of "lens name"; the dialog shows a line. - if let Some(v) = v.map(|v| v.chars().take(200).collect::()).filter(|v| !v.is_empty()) { - m.insert(key.into(), json!(v)); - } - } - Value::Object(m) -} - -/// Rewrites only changed File Info fields of an XMP packet (creating one when there is none), -/// keeping the other properties verbatim. Edited properties move into their own -/// `rdf:Description` (several descriptions per packet are valid XMP). -pub fn write_file_info(xmp: Option<&str>, info: &Value) -> String { - let cur = read_file_info(xmp); - // The dialog submits all fields, including unchanged language alternatives. - let changed = |key: &str| info.get(key).is_some_and(|v| cur.get(key) != Some(v)); - let mut x = xmp.filter(|s| s.contains("")).map(str::to_string).unwrap_or_else(|| { - "\n\n \n \n\n".to_string() - }); - for (key, prop, _) in INFO_FIELDS.into_iter().chain([("copyrightStatus", "xmpRights:Marked", ""), ("copyrightUrl", "xmpRights:WebStatement", "")]) { - if !changed(key) { - continue; - } - while let Some((a, b, _)) = find_element(&x, prop) { - x.replace_range(a..b, ""); - } - while let Some((a, b, _)) = find_attr(&x, prop) { - x.replace_range(a..b, ""); - } - } - let get = |k: &str| info.get(k).filter(|_| changed(k)).cloned().unwrap_or(Value::Null); - let mut body = String::new(); - for (key, prop, kind) in INFO_FIELDS { - let vals: Vec = match get(key) { - Value::Array(a) => a.iter().filter_map(|v| v.as_str()).map(str::to_string).filter(|v| !v.is_empty()).collect(), - Value::String(s) if kind == "Bag" || kind == "Seq" => s.split([';', ',']).map(|v| v.trim().to_string()).filter(|v| !v.is_empty()).collect(), - Value::String(s) if !s.is_empty() => vec![s], - _ => Vec::new(), - }; - if vals.is_empty() { - continue; - } - let items: String = vals - .iter() - .map(|v| { - if kind == "Alt" { format!("{}", xml_escape(v)) } else { format!("{}", xml_escape(v)) } - }) - .collect(); - if kind.is_empty() { - body.push_str(&format!(" <{prop}>{}\n", xml_escape(&vals[0]))); - } else { - body.push_str(&format!(" <{prop}>{items}\n")); - } - } - match get("copyrightStatus").as_str() { - Some("copyrighted") => body.push_str(" True\n"), - Some("publicDomain") => body.push_str(" False\n"), - _ => {} - } - if let Some(u) = get("copyrightUrl").as_str().filter(|u| !u.is_empty()) { - body.push_str(&format!(" {}\n", xml_escape(u))); - } - if !body.is_empty() { - let desc = format!( - " \n{body} \n " - ); - let at = x.rfind("").unwrap_or(x.len()); - x.insert_str(at, &desc); - } - x -} - -fn file_info(s: &mut Session, p: &Value) -> Result { - let keys = ["title", "author", "authorTitle", "description", "keywords", "copyright", "copyrightStatus", "copyrightUrl"]; - let edits = p.as_object().is_some_and(|m| m.keys().any(|k| keys.contains(&k.as_str()))); - if edits { - let xmp = s.active().ok_or(EngineError::NoDocument)?.doc.metadata.xmp.clone(); - let new = write_file_info(xmp.as_deref(), p); - s.edit("File Info", |doc, _| { - doc.metadata.xmp = Some(new); - Ok(()) - })?; - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let mut info = read_file_info(d.doc.metadata.xmp.as_deref()); - if let Value::Object(m) = &mut info { - m.insert("camera".into(), camera_data(d.doc.metadata.exif.as_deref().map(Vec::as_slice), d.doc.metadata.xmp.as_deref())); - } - Ok(info) -} - -// ---------- Automate ---------- - -fn fit_image(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let (w, h) = (d.doc.size.width as f64, d.doc.size.height as f64); - let bw = f64_param(p, "width").unwrap_or(w).max(1.0); - let bh = f64_param(p, "height").unwrap_or(h).max(1.0); - let k = (bw / w).min(bh / h); - if (k > 1.0 && p.get("dontEnlarge").and_then(Value::as_bool).unwrap_or(false)) || ((w * k).round() == w && (h * k).round() == h) { - return Ok(json!({"width": w, "height": h, "changed": false})); - } - let (nw, nh) = ((w * k).round().max(1.0), (h * k).round().max(1.0)); - let resample = p.get("resample").and_then(Value::as_str).unwrap_or("bicubic"); - s.execute("image.imageSize", json!({"width": nw, "height": nh, "resample": resample}))?; - Ok(json!({"width": nw, "height": nh, "changed": true})) -} - -fn mode_name(m: ColorMode) -> &'static str { - match m { - ColorMode::Bitmap => "bitmap", - ColorMode::Grayscale => "grayscale", - ColorMode::Duotone => "duotone", - ColorMode::Indexed => "indexed", - ColorMode::Rgb => "rgb", - ColorMode::Cmyk => "cmyk", - ColorMode::Lab => "lab", - ColorMode::Multichannel => "multichannel", - } -} - -fn conditional_mode_change(s: &mut Session, p: &Value) -> Result { - let cmd = "file.automate.conditionalModeChange"; - let to = str_param(p, "to", cmd)?.to_ascii_lowercase(); - let to = match to.as_str() { - "gray" | "grayscale" => "grayscale", - "rgb" => "rgb", - "cmyk" => "cmyk", - "lab" => "lab", - other => return Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("unsupported target mode \"{other}\" (rgb, grayscale, cmyk, lab)") }), - }; - let cur = mode_name(s.active().ok_or(EngineError::NoDocument)?.doc.mode); - let from: Vec = match p.get("from") { - Some(Value::Array(a)) => { - a.iter().filter_map(Value::as_str).map(|v| v.to_ascii_lowercase().replace("gray", "grayscale").replace("grayscalescale", "grayscale")).collect() - } - Some(Value::String(v)) if v != "any" => vec![v.to_ascii_lowercase()], - _ => vec!["any".into()], - }; - if cur == to || !(from.iter().any(|f| f == "any" || f == cur)) { - return Ok(json!({"changed": false, "mode": cur})); - } - s.execute(&format!("image.mode.{to}"), json!({}))?; - Ok(json!({"changed": true, "mode": to})) -} - -/// One `[id, params]` / `{command, params}` / `{id, params}` step of an action. -fn parse_steps(v: &Value) -> Result> { - let bad = |m: &str| EngineError::BadParams { cmd: "file.automate.batch".into(), msg: m.into() }; - let arr = v.as_array().ok_or_else(|| bad("\"steps\" must be an array"))?; - arr.iter() - .map(|st| match st { - Value::Array(a) if !a.is_empty() => { - Ok((a[0].as_str().ok_or_else(|| bad("step id must be a string"))?.to_string(), a.get(1).cloned().unwrap_or(json!({})))) - } - Value::Object(o) => { - let id = o.get("command").or_else(|| o.get("id")).and_then(Value::as_str).ok_or_else(|| bad("step needs \"command\""))?; - Ok((id.to_string(), o.get("params").cloned().unwrap_or(json!({})))) - } - Value::String(id) => Ok((id.clone(), json!({}))), - _ => Err(bad("each step is [id, params] or {\"command\", \"params\"}")), - }) - .collect() -} - -/// Inputs of a batch command: `"input"` is a folder or an array of files. -pub(crate) fn batch_inputs(p: &Value, cmd: &str) -> Result> { - match p.get("input").or_else(|| p.get("files")) { - Some(Value::Array(a)) => Ok(a.iter().filter_map(Value::as_str).map(str::to_string).collect()), - Some(Value::String(dir)) => list_images(dir), - _ => Err(EngineError::BadParams { cmd: cmd.into(), msg: "missing \"input\" (folder or array of files)".into() }), - } -} - -/// One input of a batch run: the file, and the folder it sits in relative to the input folder -/// (`""` at the top level, `"a/b"` two levels down), which its result keeps under `output`. -#[derive(Clone, Debug, PartialEq)] -pub(crate) struct BatchInput { - pub path: String, - pub rel_dir: String, -} - -/// Subfolders below a batch input folder are walked at most this deep ("Include All Subfolders"). -pub(crate) const MAX_SUBFOLDER_DEPTH: usize = 32; -/// A batch over subfolders lists at most this many files; a larger tree is an error, not a run -/// that silently stops partway. -pub(crate) const MAX_SUBFOLDER_FILES: usize = 100_000; - -/// [`batch_inputs`] for the commands whose Photoshop dialog has "Include All Subfolders" (Batch, -/// Image Processor): with `"includeSubfolders": true` a folder input also yields the images in its -/// subfolders, each tagged with its relative folder. `output` (when inside the input folder, as -/// the dialog's default `/batch` is) is not walked, so a rerun doesn't process its results. -pub(crate) fn batch_inputs_tree(p: &Value, cmd: &str, output: &str) -> Result> { - let recurse = match p.get("includeSubfolders") { - None | Some(Value::Null) => false, - Some(Value::Bool(b)) => *b, - Some(_) => return Err(EngineError::BadParams { cmd: cmd.into(), msg: "\"includeSubfolders\" must be true or false".into() }), - }; - match p.get("input").or_else(|| p.get("files")) { - Some(Value::String(dir)) if recurse => list_images_tree(dir, output, cmd), - _ => Ok(batch_inputs(p, cmd)?.into_iter().map(|path| BatchInput { path, rel_dir: String::new() }).collect()), - } -} - -/// The images under `dir` and its subfolders: each folder's files sorted by name, then its -/// subfolders in name order (depth first). Bounded by [`MAX_SUBFOLDER_DEPTH`] and -/// [`MAX_SUBFOLDER_FILES`]; a folder reached twice (a symlink loop) is walked once, and `skip` -/// (the output folder) is left out. -#[cfg(not(target_arch = "wasm32"))] -pub(crate) fn list_images_tree(dir: &str, skip: &str, cmd: &str) -> Result> { - use std::path::{Path, PathBuf}; - let canon = |p: &Path| std::fs::canonicalize(p).ok(); - let root = Path::new(dir); - let root_canon = canon(root).ok_or_else(|| EngineError::Other(format!("{dir}: cannot open the folder")))?; - let skip = if skip.is_empty() { None } else { canon(Path::new(skip)) }; - let mut seen = std::collections::HashSet::new(); - seen.insert(root_canon); - let mut out = Vec::new(); - // Depth first with an explicit stack (no recursion): (folder, relative folder, depth). - let mut stack: Vec<(PathBuf, String, usize)> = vec![(root.to_path_buf(), String::new(), 0)]; - while let Some((folder, rel, depth)) = stack.pop() { - let shown = folder.to_string_lossy().into_owned(); - // The top folder must be readable; a subfolder that isn't is skipped. - let files = match list_images(&shown) { - Ok(f) => f, - Err(e) if depth == 0 => return Err(e), - Err(_) => continue, - }; - if out.len().saturating_add(files.len()) > MAX_SUBFOLDER_FILES { - return Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("{dir} holds more than {MAX_SUBFOLDER_FILES} images with its subfolders") }); - } - out.extend(files.into_iter().map(|path| BatchInput { path, rel_dir: rel.clone() })); - if depth >= MAX_SUBFOLDER_DEPTH { - continue; - } - let Ok(rd) = std::fs::read_dir(&folder) else { continue }; - let mut subs: Vec<(String, PathBuf)> = rd - .filter_map(|e| e.ok()) - .map(|e| e.path()) - .filter(|p| p.is_dir()) - .filter_map(|p| Some((p.file_name()?.to_string_lossy().into_owned(), p))) - .collect(); - subs.sort(); - // Pushed in reverse so the stack pops them in name order. - for (name, sub) in subs.into_iter().rev() { - let Some(c) = canon(&sub) else { continue }; - if skip.as_ref() == Some(&c) || !seen.insert(c) { - continue; - } - let rel = if rel.is_empty() { name } else { format!("{rel}/{name}") }; - stack.push((sub, rel, depth + 1)); - } - } - Ok(out) -} -#[cfg(target_arch = "wasm32")] -pub(crate) fn list_images_tree(dir: &str, _skip: &str, _cmd: &str) -> Result> { - Err(EngineError::Other(format!("cannot list {dir}: no file system on the web"))) -} - -/// Opens each input in a scratch session, runs `f` on it and saves it to `output` as `format` -/// (`"same"` keeps the input's extension). Errors per file are collected, not fatal. -pub(crate) fn process_files(inputs: &[String], output: &str, format: &str, save: SaveOpts, suffix: &str, f: &dyn Fn(&mut Session) -> Result<()>) -> Value { - let inputs: Vec = inputs.iter().map(|path| BatchInput { path: path.clone(), rel_dir: String::new() }).collect(); - process_tree(&inputs, output, format, save, suffix, f) -} - -/// [`process_files`] for inputs from subfolders: each result goes to its input's relative folder -/// under `output` (created as needed), so `a/x.png` and `b/x.png` don't collide. -pub(crate) fn process_tree(inputs: &[BatchInput], output: &str, format: &str, save: SaveOpts, suffix: &str, f: &dyn Fn(&mut Session) -> Result<()>) -> Value { - let outputs = inputs.iter().map(|BatchInput { path, rel_dir }| { - let ext = - if format == "same" { path.rsplit('.').next().unwrap_or("png").to_ascii_lowercase() } else { format.trim_start_matches('.').to_ascii_lowercase() }; - (path.as_str(), join(&join(output, rel_dir), &format!("{}{suffix}.{ext}", stem(path)))) - }); - process_outputs(outputs, save, f) -} - -fn process_outputs<'a>(outputs: impl IntoIterator, save: SaveOpts, f: &dyn Fn(&mut Session) -> Result<()>) -> Value { - let mut files = Vec::new(); - let mut errors = Vec::new(); - let mut written = OutputClaims::default(); - for (path, out) in outputs { - let r = (|| -> Result<()> { - written.check(&out).map_err(EngineError::Other)?; - let bytes = read_file(path)?; - let mut scratch = Session::new(); - let doc = import(&file_name(path), &bytes)?; - scratch.add_document(doc, Some(path.to_string())); - f(&mut scratch)?; - let d = scratch.active().ok_or(EngineError::NoDocument)?; - save_doc(&d.doc, &out, save)?; - Ok(()) - })(); - match r { - Ok(()) => { - written.record(&out, path); - files.push(out); - } - Err(e) => errors.push(json!({"file": path, "error": e.to_string()})), - } - } - json!({"files": files, "errors": errors}) -} - -/// The output paths a batch run has written, so a later input whose output name matches an -/// earlier one's (`a.png` and `a.jpg` saved as JPEG, or the same name in two input folders) is -/// reported instead of silently replacing that result (#420, #422). Names are compared ignoring -/// case, because macOS and Windows file systems do. -#[derive(Default)] -pub struct OutputClaims(std::collections::HashMap); - -impl OutputClaims { - /// `Err` (naming the earlier input) when `out` was already written in this run. - pub fn check(&self, out: &str) -> std::result::Result<(), String> { - match self.0.get(&out.to_lowercase()) { - Some(first) => Err(format!("not written: {out} already holds the result of {first} from this run (same output name)")), - None => Ok(()), - } - } - /// Remember that `input`'s result was written to `out`. - pub fn record(&mut self, out: &str, input: &str) { - self.0.insert(out.to_lowercase(), input.to_string()); - } -} - -fn batch(s: &mut Session, p: &Value) -> Result { - let cmd = "file.automate.batch"; - let mut steps = - parse_steps(p.get("steps").or_else(|| p.get("action")).ok_or_else(|| EngineError::BadParams { cmd: cmd.into(), msg: "missing \"steps\"".into() })?)?; - // Each result goes to the destination folder, which overrides the action's recorded Save / - // Save As steps (Photoshop's "Override Action 'Save As' Commands"). Recorded view steps - // (zoom, fit) don't change the output, so they are skipped (#2752). - steps.retain(|(id, _)| !crate::actions_cmds::shell_save_command(id) && !crate::actions_cmds::shell_view_command(id) && id != "file.export.quickExport"); - if let Some((id, _)) = steps.iter().find(|(id, _)| crate::commands::find(id).is_none()) { - return Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("unknown command `{id}` in the action") }); - } - let output = str_param(p, "output", cmd)?.to_string(); - let inputs = batch_inputs_tree(p, cmd, &output)?; - let format = p.get("format").and_then(Value::as_str).unwrap_or("same").to_string(); - // Steps that call another action play it from the caller's actions; the playback stack comes - // along so an action that batches itself is caught as a recursive call. - let list = match p.get("calledActions") { - None | Some(Value::Null) => s.actions.list.clone(), - // A droplet brings the actions it calls (#2794) and runs with those alone. - Some(v) => { - let list: Vec = - serde_json::from_value(v.clone()).map_err(|e| EngineError::BadParams { cmd: cmd.into(), msg: format!("\"calledActions\": {e}") })?; - if list.len() > 4096 { - return Err(EngineError::BadParams { cmd: cmd.into(), msg: "\"calledActions\" holds more than 4096 actions".into() }); - } - list - } - }; - let actions = crate::actions_cmds::ActionState { list, playing: s.actions.playing, playback_stack: s.actions.playback_stack.clone(), ..Default::default() }; - let names = batch_naming::plan(&inputs, &output, &format, p)?; - let save = SaveOpts::from_params(s, p, cmd)?; - let run = |scratch: &mut Session| { - scratch.actions = actions.clone(); - for (id, params) in &steps { - let r = scratch.execute(id, params.clone())?; - // A failed step inside a called action makes this file an error, not a saved result. - if let Some(error) = crate::actions_cmds::nested_failure(id, &r) { - return Err(EngineError::Other(error)); - } - } - Ok(()) - }; - let r = match names { - Some(names) => process_outputs(inputs.iter().map(|i| i.path.as_str()).zip(names), save, &run), - None => process_tree(&inputs, &output, &format, save, "", &run), - }; - Ok(r) -} - -fn image_processor(s: &mut Session, p: &Value) -> Result { - let cmd = "file.scripts.imageProcessor"; - let output = str_param(p, "output", cmd)?.to_string(); - let inputs = batch_inputs_tree(p, cmd, &output)?; - let format = p.get("format").and_then(Value::as_str).unwrap_or("jpg").to_string(); - let fit = match (size_param(p, "width"), size_param(p, "height")) { - (None, None) => None, - (w, h) => Some(json!({"width": w.unwrap_or(1e9), "height": h.unwrap_or(1e9), "dontEnlarge": true})), - }; - let to_srgb = p.get("convertToSrgb").and_then(Value::as_bool).unwrap_or(false); - let r = process_tree(&inputs, &output, &format, SaveOpts::from_params(s, p, cmd)?.or_quality(8.0), "", &|scratch| { - if to_srgb && scratch.active().is_some_and(|d| d.doc.mode != ColorMode::Rgb) { - scratch.execute("image.mode.rgb", json!({}))?; - } - if let Some(fp) = &fit { - fit_image(scratch, fp)?; - } - Ok(()) - }); - Ok(r) -} - -fn load_files_into_stack(s: &mut Session, p: &Value) -> Result { - let cmd = "file.scripts.loadFilesIntoStack"; - let paths: Vec = match p.get("paths").or_else(|| p.get("input")) { - Some(Value::Array(a)) => a.iter().filter_map(Value::as_str).map(str::to_string).collect(), - Some(Value::String(dir)) => list_images(dir)?, - _ => return Err(EngineError::BadParams { cmd: cmd.into(), msg: "missing \"paths\"".into() }), - }; - if paths.is_empty() { - return Err(EngineError::BadParams { cmd: cmd.into(), msg: "no files to load".into() }); - } - let mut docs = Vec::new(); - for path in &paths { - let bytes = read_file(path)?; - docs.push((file_name(path), import(&file_name(path), &bytes)?)); - } - let first = &docs[0].1; - let w = docs.iter().map(|(_, d)| d.size.width).max().unwrap_or(1); - let h = docs.iter().map(|(_, d)| d.size.height).max().unwrap_or(1); - let mut stack = Document::new(stem(&paths[0]), photocraft_doc::Size::new(w, h), first.mode, first.depth); - stack.resolution_dpi = first.resolution_dpi; - stack.icc_profile = first.icc_profile.clone(); - let fmt = stack.pixel_format(); - // First file at the bottom, like Photoshop's script. - for (name, d) in &docs { - stack.layers.push(Layer::new(name.clone(), LayerContent::Raster(flattened(d, fmt)))); - } - let n = stack.layers.len(); - let smart = p.get("createSmartObject").and_then(Value::as_bool).unwrap_or(false); - // "Attempt to Automatically Align Source Images": Auto-Align Layers on the loaded layers, before - // they go into the smart object. - let align = p.get("align").and_then(Value::as_bool).unwrap_or(false) && n > 1; - if smart && !align { - // "Create Smart Object after Loading Layers": the layers go inside one smart object, ready - // for Layer › Smart Objects › Stack Mode. Like Photoshop, they sit at the top level of the - // contents, with no wrapper group. - let layers = std::mem::take(&mut stack.layers); - let smart = crate::smart_cmds::layers_to_smart(&stack, &stem(&paths[0]), layers)?; - stack.layers = vec![smart]; - } - let i = s.add_document(stack, None); - if align { - s.execute("select.allLayers", json!({}))?; - s.execute("edit.autoAlignLayers", json!({"projection": "auto"}))?; - if smart { - let name = s.active().map(|d| d.doc.name.clone()).unwrap_or_default(); - s.edit("Convert to Smart Object", |doc, active| { - let layers = std::mem::take(&mut doc.layers); - let smart = crate::smart_cmds::layers_to_smart(doc, &name, layers)?; - *active = Some(smart.id); - doc.layers = vec![smart]; - Ok(()) - })?; - if let Some(st) = s.active_mut() { - st.selected_layers = st.active_layer.into_iter().collect(); - } - } - } - Ok(json!({"document": i, "layers": n})) -} - -// ---------- Scripts: flatten effects / masks ---------- - -fn has_live_effects(l: &Layer) -> bool { - l.effects.enabled && l.effects.items.iter().any(|e| e.enabled()) -} - -/// The layer rendered alone (keeping what `keep` says), as a surface in `fmt`. With `apply_mask` -/// off the layer's pixel and vector masks are left out of the render, so a caller that keeps them -/// live on the baked raster does not mask the pixels twice. -fn bake(l: &Layer, canvas: Rect, fmt: PixelFormat, keep_effects: bool, apply_mask: bool) -> Surface { - let mut tmp = l.clone(); - tmp.opacity = 1.0; - tmp.blend = photocraft_color::BlendMode::Normal; - tmp.visible = true; - tmp.clipped = false; - if !keep_effects { - tmp.effects.items.clear(); - tmp.fill_opacity = 1.0; - } - if !apply_mask { - tmp.mask = None; - tmp.vector_mask = None; - } - // Render over the union of the canvas and the content so off-canvas pixels survive. - let area = l.surface().map_or(canvas, |s| s.content_bounds().union(&canvas)); - let area = if keep_effects { area.inflate(256) } else { area }; - buffer_surface(&photocraft_compose::render_layer(&tmp, area), fmt) -} - -fn walk_ids(doc: &Document) -> Vec { - doc.walk().into_iter().map(|(_, _, l)| l.id).collect() -} - -fn flatten_all_effects(s: &mut Session) -> Result { - let mut n = 0; - s.edit("Flatten All Layer Effects", |doc, _| { - let canvas = doc.bounds(); - let fmt = doc.pixel_format(); - for id in walk_ids(doc) { - let Some(l) = doc.layer(id) else { continue }; - if !has_live_effects(l) || matches!(l.content, LayerContent::Group(_) | LayerContent::Adjustment(_)) { - continue; - } - // The pixel and vector masks stay on the layer (baking must not destroy them), so the - // pixels are baked without them and each mask then applies once to the baked raster. - let px = bake(l, canvas, fmt, true, false); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - l.content = LayerContent::Raster(px); - l.effects = Default::default(); - l.fill_opacity = 1.0; - l.fill_cache = None; - l.psd_blocks.retain(|(k, _)| !matches!(k, b"TySh" | b"SoLd" | b"PlLd" | b"SoLE" | b"vmsk" | b"vsms" | b"vogk" | b"vscg" | b"vstk")); - n += 1; - } - Ok(()) - })?; - Ok(json!({"flattened": n})) -} - -fn flatten_all_masks(s: &mut Session) -> Result { - let (mut n, mut skipped) = (0, 0); - s.edit("Flatten All Masks", |doc, _| { - let canvas = doc.bounds(); - let fmt = doc.pixel_format(); - for id in walk_ids(doc) { - let Some(l) = doc.layer(id) else { continue }; - let masked = l.mask.as_ref().is_some_and(|m| m.enabled) || l.vector_mask.is_some(); - if !masked { - continue; - } - if !matches!(l.content, LayerContent::Raster(_)) { - // Masks on groups, adjustment, type, shape and smart layers have no pixels to - // fold into; Photoshop's script leaves them too. - skipped += 1; - continue; - } - let px = bake(l, canvas, fmt, false, true); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - l.content = LayerContent::Raster(px); - l.mask = None; - l.vector_mask = None; - l.psd_blocks.retain(|(k, _)| !matches!(k, b"vmsk" | b"vsms")); - n += 1; - } - Ok(()) - })?; - Ok(json!({"flattened": n, "skipped": skipped})) -} - -// ---------- Export ---------- - -fn layers_to_files(s: &mut Session, p: &Value) -> Result { - let cmd = "file.export.layersToFiles"; - let dir = str_param(p, "dir", cmd).or_else(|_| str_param(p, "output", cmd))?.to_string(); - let names = export_layers_with(s, p, |name, bytes| write_file(&join(&dir, name), bytes))?; - let files: Vec = names.iter().map(|name| join(&dir, name)).collect(); - Ok(json!({"files": files})) -} - -/// The bodies of `file.export.layersToFiles`, streamed to `write` as a single-component filename -/// plus its bytes: one file per exported layer (top to bottom, like Photoshop's numbering), -/// encoded but never touched as a path. Automation writes exactly these bytes beneath its write -/// root (#3216), so the two paths cannot drift. Returns the filenames written, in order. -pub fn export_layers_with(s: &Session, p: &Value, mut write: impl FnMut(&str, &[u8]) -> Result<()>) -> Result> { - let cmd = "file.export.layersToFiles"; - let format = p.get("format").and_then(Value::as_str).unwrap_or("png").trim_start_matches('.').to_ascii_lowercase(); - let visible_only = p.get("visibleOnly").and_then(Value::as_bool).unwrap_or(true); - let save = SaveOpts::from_params(s, p, cmd)?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let prefix = p.get("prefix").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| stem(&d.doc.name)); - let doc = d.doc.clone(); - let mut written = Vec::new(); - // Top-level layers (groups export merged), top to bottom like Photoshop's numbering. - let layers: Vec<&Layer> = doc.layers.iter().rev().filter(|l| l.visible || !visible_only).collect(); - for (i, l) in layers.iter().enumerate() { - if matches!(l.content, LayerContent::Adjustment(_)) { - continue; - } - let mut one = (*doc).clone(); - let mut only = (*l).clone(); - only.visible = true; - only.clipped = false; - one.layers = vec![only]; - let name = format!("{}_{:04}_{}.{format}", sanitize(&prefix), i, sanitize(&l.name)); - let (bytes, _warnings) = encode(&one, &name, save)?; - write(&name, &bytes)?; - written.push(name); - } - Ok(written) -} - -/// Adjustment layers that can be represented as an RGB-only LUT, in document stack order. -/// A LUT has no spatial coordinates, so masks and clipping geometry are deliberately stripped -/// at bake time. Group adjustments need their enclosing group's compositing semantics and are -/// not silently flattened as independent layers. -fn lut_layers<'a>(doc: &'a Document, ids: Option<&[LayerId]>) -> Result> { - const CMD: &str = "file.export.colorLookupTables"; - let mut selected = Vec::new(); - if let Some(ids) = ids { - if ids.is_empty() { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: "select at least one adjustment layer".into() }); - } - for id in ids { - if selected.contains(id) { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: format!("duplicate layer id {}", id.0) }); - } - let Some(layer) = doc.layer(*id) else { return Err(EngineError::NoLayer(*id)) }; - if !matches!(layer.content, LayerContent::Adjustment(_)) { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: format!("layer {} is not an adjustment layer", id.0) }); - } - if !layer.visible { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: format!("layer {} is hidden", id.0) }); - } - if !doc.layers.iter().any(|root| root.id == *id) { - return Err(EngineError::BadParams { - cmd: CMD.into(), - msg: format!("layer {} is nested in a group; export top-level adjustment layers", id.0), - }); - } - selected.push(*id); - } - } - let mut layers = Vec::new(); - for l in &doc.layers { - if l.visible && matches!(l.content, LayerContent::Adjustment(_)) && (ids.is_none() || selected.contains(&l.id)) { - layers.push(l); - } - } - if layers.is_empty() { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: "no visible adjustment layers to export".into() }); - } - Ok(layers) -} - -/// Bake the chosen adjustment stack against an identity RGB lattice. -/// Source order always follows the document stack, even if an explicit list is reversed. -fn bake_cube_layers(size: usize, title: &str, layers: &[&Layer]) -> String { - let n = size; - let (w, h) = ((n * n) as u32, n as u32); - let mut lattice = Document::new("lut", photocraft_doc::Size::new(w, h), ColorMode::Rgb, photocraft_color::SampleType::F32); - let fmt = lattice.pixel_format(); - let step = 1.0 / (n - 1) as f32; - let mut data = Vec::with_capacity((w * h) as usize * fmt.channels()); - for g in 0..n { - for b in 0..n { - for r in 0..n { - let mut px = [0.0f32; 8]; - let k = from_rgba_into(&fmt, [r as f32 * step, g as f32 * step, b as f32 * step, 1.0], &mut px); - data.extend_from_slice(&px[..k]); - } - } - } - let mut surf = Surface::new(fmt); - surf.write_region(Rect::new(0, 0, w as i32, h as i32), &data); - lattice.layers.push(Layer::new("Lattice", LayerContent::Raster(surf))); - for l in layers { - let mut a = (*l).clone(); - // Masks and clipping are spatial; a LUT is the adjustment stack's colour mapping. - a.mask = None; - a.vector_mask = None; - a.clipped = false; - lattice.layers.push(a); - } - let out = photocraft_compose::flatten(&lattice); - let mut s = format!("TITLE \"{}\"\n# Created by Photocraft\nLUT_3D_SIZE {n}\nDOMAIN_MIN 0.0 0.0 0.0\nDOMAIN_MAX 1.0 1.0 1.0\n", title.replace('"', "'")); - // .cube order: red changes fastest, then green, then blue. - for b in 0..n { - for g in 0..n { - for r in 0..n { - let (x, y) = (b * n + r, g); - let px = out.px[y * w as usize + x]; - s.push_str(&format!("{:.6} {:.6} {:.6}\n", px[0].clamp(0.0, 1.0), px[1].clamp(0.0, 1.0), px[2].clamp(0.0, 1.0))); - } - } - } - s -} - -/// Backwards-compatible helper for exporting all visible top-level adjustments. -pub fn bake_cube(doc: &Document, size: usize, title: &str) -> String { - let layers: Vec<&Layer> = doc.layers.iter().filter(|l| l.visible && matches!(l.content, LayerContent::Adjustment(_))).collect(); - bake_cube_layers(size.clamp(2, photocraft_cms::lutfile::MAX_SIZE), title, &layers) -} - -/// Export either the whole visible stack (backward-compatible default), the Layers panel's -/// current selection, or an explicit list supplied by scripts/CLI/automation. -fn color_lookup_tables(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "file.export.colorLookupTables"; - let d = s.active().ok_or(EngineError::NoDocument)?; - let size = match p.get("size") { - None | Some(Value::Null) => 33, - Some(v) => v.as_u64().ok_or_else(|| EngineError::BadParams { cmd: CMD.into(), msg: "size must be an integer".into() })?, - }; - if !(2..=photocraft_cms::lutfile::MAX_SIZE as u64).contains(&size) { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: format!("size must be 2..={}", photocraft_cms::lutfile::MAX_SIZE) }); - } - let scope = match p.get("scope") { - None | Some(Value::Null) => "all", - Some(Value::String(s)) => s.as_str(), - Some(_) => return Err(EngineError::BadParams { cmd: CMD.into(), msg: "scope must be all or selected".into() }), - }; - if !matches!(scope, "all" | "selected") { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: "scope must be all or selected".into() }); - } - if p.get("layers").is_some() && scope == "selected" { - return Err(EngineError::BadParams { cmd: CMD.into(), msg: "use either layers or scope=selected, not both".into() }); - } - let explicit: Option> = if let Some(v) = p.get("layers") { - let Some(a) = v.as_array() else { return Err(EngineError::BadParams { cmd: CMD.into(), msg: "layers must be an array of layer IDs".into() }) }; - let mut out = Vec::with_capacity(a.len()); - for id in a { - let Some(id) = id.as_u64() else { return Err(EngineError::BadParams { cmd: CMD.into(), msg: "layer IDs must be unsigned integers".into() }) }; - out.push(LayerId(id)); - } - Some(out) - } else if scope == "selected" { - Some(d.selected_layers()) - } else { - None - }; - let layers = lut_layers(&d.doc, explicit.as_deref())?; - let count = layers.len(); - let title = p.get("title").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| stem(&d.doc.name)); - let cube = bake_cube_layers(size as usize, &title, &layers); - match p.get("path").and_then(Value::as_str) { - Some(path) => { - write_file(path, cube.as_bytes())?; - Ok(json!({"path": path, "size": size, "layerCount": count})) - } - None => Ok(json!({"cube": cube, "size": size, "layerCount": count})), - } -} - -// ---------- guides ---------- - -fn push_unique(v: &mut Vec, x: f64) { - if !v.iter().any(|g| (g - x).abs() < 0.01) { - v.push(x); - } -} - -fn new_guide_layout(s: &mut Session, p: &Value) -> Result { - crate::guide_layout::apply(s, p) -} - -fn guides_from_shape(s: &mut Session, p: &Value) -> Result { - let id = layer_param(s, p)?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let LayerContent::Shape(sh) = &l.content else { return Err(EngineError::Other("the layer is not a shape layer".into())) }; - let (x0, y0, x1, y1) = sh.path.control_bounds().ok_or(EngineError::Other("the shape has no path".into()))?; - s.edit("New Guides From Shape", |doc, _| { - for x in [x0, (x0 + x1) / 2.0, x1] { - push_unique(&mut doc.guides.vertical, x); - } - for y in [y0, (y0 + y1) / 2.0, y1] { - push_unique(&mut doc.guides.horizontal, y); - } - Ok(()) - })?; - Ok(json!({"bounds": [x0, y0, x1, y1]})) -} - -fn clear_canvas_guides(s: &mut Session) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - // Guides inside an artboard belong to it (View › Clear Selected Artboard Guides); the rest - // are canvas guides. Without artboards every guide is a canvas guide. - let boards: Vec = d.doc.artboards().iter().map(|b| b.2.rect).collect(); - let in_x = move |x: f64| boards.iter().any(|r| x > f64::from(r.x0) && x < f64::from(r.x1)); - let boards_y: Vec = d.doc.artboards().iter().map(|b| b.2.rect).collect(); - let in_y = move |y: f64| boards_y.iter().any(|r| y > f64::from(r.y0) && y < f64::from(r.y1)); - let n = d.doc.guides.vertical.iter().filter(|x| !in_x(**x)).count() + d.doc.guides.horizontal.iter().filter(|y| !in_y(**y)).count(); - if n == 0 { - return Ok(json!({"cleared": 0})); - } - s.edit("Clear Canvas Guides", |doc, _| { - doc.guides.vertical.retain(|x| in_x(*x)); - doc.guides.horizontal.retain(|y| in_y(*y)); - doc.guides.prune_colors(); - Ok(()) - })?; - Ok(json!({"cleared": n})) -} - -// ---------- registry ---------- - -pub fn specs() -> Vec { - macro_rules! spec { - ($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } - }; - } - vec![ - spec!("file.closeAll", "Close All", &["File"], Some("Cmd+Alt+W"), "{}", has_doc, |s, _| close_all(s)), - spec!( - "file.closeOthers", - "Close Others", - &["File"], - Some("Cmd+Alt+P"), - r##"{"document":index? (the one to keep; default active)}"##, - has_doc, - close_others - ), - // The document tab's menu (UI-217-6): the tab's own file in the platform file manager. - // Not a Photoshop menu-bar item; enabled when the target document has a saved path. - spec!("file.revealInFinder", "Reveal in Finder", &[], None, r##"{"document":index? (default active),"dryRun":bool=false}"##, native_doc, |s, p| { - let i = match p.get("document") { - Some(v) => v - .as_u64() - .and_then(|v| usize::try_from(v).ok()) - .ok_or_else(|| EngineError::BadParams { cmd: "file.revealInFinder".into(), msg: "`document` must be an index".into() })?, - None => s.active_index().ok_or(EngineError::NoDocument)?, - }; - let path = s - .documents() - .get(i) - .and_then(|d| d.path.clone()) - .ok_or_else(|| EngineError::BadParams { cmd: "file.revealInFinder".into(), msg: format!("document {i} has no saved file") })?; - crate::layer_menu_cmds::reveal(&path, p.get("dryRun").and_then(Value::as_bool).unwrap_or(false)) - }), - spec!("file.revert", "Revert", &["File"], Some("F12"), "{} (reloads the saved file as one undoable step)", can_revert, |s, _| revert(s)), - spec!( - "file.saveACopy", - "Save a Copy…", - &["File"], - Some("Cmd+Alt+S"), - r##"{"path":str (format from the extension),"quality":0..12? (JPEG),"layers":bool=true,"tiffLayers":bool=false (TIFF: keep the layers; flat by default),"tgaBits":24|32? (TGA: 32 writes the alpha channel, or else the transparency, as the alpha; unset, 32 with an alpha channel or transparency, else 24),"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16)}"##, - native_doc, - save_a_copy - ), - spec!( - "file.openAs", - "Open As…", - &["File"], - Some("Cmd+Alt+Shift+O"), - r##"{"path":str,"as":"psd|png|jpg|tiff|…"? (decode as this format)}"##, - native, - open_as - ), - spec!( - "file.placeEmbedded", - "Place Embedded…", - &["File"], - None, - r##"{"path":str,"scale":%? (default: fit when larger than the canvas),"fit":bool? (default: Preferences > General > Resize Image During Place),"smartObject":bool? (default: Preferences > General > Always Create Smart Objects When Placing),"center":[x,y]?}"##, - native_doc, - |s, p| place(s, p, false) - ), - spec!( - "file.placeLinked", - "Place Linked…", - &["File"], - None, - r##"{"path":str,"scale":%?,"fit":bool? (default: Preferences > General > Resize Image During Place),"center":[x,y]?}"##, - native_doc, - |s, p| place(s, p, true) - ), - spec!( - "file.fileInfo", - "File Info…", - &["File"], - Some("Cmd+Alt+Shift+I"), - r##"{"title":str?,"author":str?,"authorTitle":str?,"description":str?,"keywords":[str]|"a; b"?,"copyright":str?,"copyrightStatus":"unknown|copyrighted|publicDomain"?,"copyrightUrl":str?} (no keys: read; the result also has read-only "camera":{"make","model","dateTaken","exposure","fNumber","iso","focalLength","focalLength35mm","lens"}, present keys only)"##, - has_doc, - file_info - ), - spec!( - "file.automate.fitImage", - "Fit Image…", - &["File", "Automate"], - None, - r##"{"width":px,"height":px,"dontEnlarge":bool=false,"resample":"automatic|preserveDetails|bicubicSmoother|bicubicSharper|bicubicAutomatic|bicubic|nearest|bilinear|lanczos"="bicubic"}"##, - has_doc, - fit_image - ), - spec!( - "file.automate.conditionalModeChange", - "Conditional Mode Change…", - &["File", "Automate"], - None, - r##"{"from":["rgb","grayscale","cmyk","lab","indexed","bitmap",…]|"any"="any","to":"rgb|grayscale|cmyk|lab"}"##, - has_doc, - conditional_mode_change - ), - spec!( - "file.automate.batch", - "Batch…", - &["File", "Automate"], - None, - r##"{"steps":[[commandId,params]|{"command":id,"params":{}}…] (a recorded action),"input":folder|[paths],"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; each result keeps its subfolder under output),"output":folder,"format":"same|png|jpg|psd|tiff|…"="same","quality":0..12?,"naming":string? (blank keeps source names; {name}, {serial} padded to 3 digits, {date} as YYYY-MM-DD UTC; extension added automatically),"startingSerial":1..4294967295=1 (input position, including failed inputs),"tiffLayers":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"calledActions":[{"name","steps"}…]? (the actions Play Action steps call; default: the session's)} → {files, errors} (an input whose output name was already written in the run goes to errors)"##, - native, - batch - ), - spec!( - "file.scripts.imageProcessor", - "Image Processor…", - &["File", "Scripts"], - None, - r##"{"input":folder|[paths],"includeSubfolders":bool=false (a folder input also takes the images in its subfolders, up to 32 levels and 100000 files; each result keeps its subfolder under output),"output":folder,"format":"jpg|png|psd|tiff|…"="jpg","quality":0..12=8,"tiffLayers":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16),"width":px?,"height":px? (fit, never enlarge),"convertToSrgb":bool=false} → {files, errors} (an input whose output name was already written in the run goes to errors)"##, - native, - image_processor - ), - spec!( - "file.scripts.loadFilesIntoStack", - "Load Files into Stack…", - &["File", "Scripts"], - None, - r##"{"paths":[str]|folder,"align":bool=false (Auto-Align Layers on them first),"createSmartObject":bool=false} → new document with one layer per file (inside one smart object with createSmartObject)"##, - native, - load_files_into_stack - ), - spec!("file.scripts.flattenAllLayerEffects", "Flatten All Layer Effects", &["File", "Scripts"], None, "{}", has_doc, |s, _| flatten_all_effects(s)), - spec!( - "file.scripts.flattenAllMasks", - "Flatten All Masks", - &["File", "Scripts"], - None, - "{} (pixel layers; masks on other layer kinds are left)", - has_doc, - |s, _| flatten_all_masks(s) - ), - spec!( - "file.export.layersToFiles", - "Layers to Files…", - &["File", "Export"], - None, - r##"{"dir":folder,"format":"png|jpg|psd|tiff|…"="png","prefix":str=document name,"visibleOnly":bool=true,"quality":0..12?,"tiffLayers":bool=false,"tgaBits":24|32?,"dpxBits":8|10|12|16? (DPX: bits per sample; unset, 8 for an 8-bit document, else 16)} → {files}"##, - native_doc, - layers_to_files - ), - spec!( - "file.export.colorLookupTables", - "Color Lookup Tables…", - &["File", "Export"], - None, - r##"{"path":str? (.cube; omit to return the text),"size":2..129=33,"title":str?,"scope":"all|selected"="all","layers":[id,…]?} (selected layer IDs use original document stacking; masks are spatial and omitted)"##, - has_adjustments, - color_lookup_tables - ), - spec!( - "view.newGuideLayout", - "New Guide Layout…", - &["View"], - None, - r##"{"columns":n=0,"width":px?,"gutter":px=0,"rows":n=0,"height":px?,"rowGutter":px=gutter,"margin":px|[top,left,bottom,right]=0,"centerColumns":bool=false,"clearExisting":bool=false,"target":"document|selectedArtboards"="document"}"##, - has_doc, - new_guide_layout - ), - spec!("view.newGuidesFromShape", "New Guides From Shape", &["View"], None, r##"{"layer":id?}"##, has_shape, guides_from_shape), - spec!("view.clearCanvasGuides", "Clear Canvas Guides", &["View"], None, "{}", has_doc, |s, _| clear_canvas_guides(s)), - ] -} - -#[cfg(test)] -#[path = "file_cmds/tests.rs"] -mod tests; - -#[cfg(test)] -mod aces_exr_tests { - use super::*; - - /// File Handling › "Open color-less EXR as ACEScg": off, an EXR opens on io's linear-sRGB - /// default; on, it opens as ACEScg — the working space ACES render pipelines write. - #[test] - fn exr_aces_cg_preference_retags_the_opened_document() { - let image = photocraft_codecs::Image::from_f32( - 4, - 4, - photocraft_codecs::ChannelLayout::Rgb, - &[ - 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, - 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, 0.25, 0.5, 0.75, 1.0, - ], - ) - .unwrap(); - let bytes = photocraft_codecs::encode(&image, photocraft_codecs::Format::OpenExr, &photocraft_codecs::EncodeOptions::default()).unwrap(); - - let linear = photocraft_cms::Builtin::LinearSrgb.profile().to_bytes(); - let aces = photocraft_cms::Builtin::AcesCg.profile().to_bytes(); - - // Off (the default): io's linear-sRGB tagging stays. - let mut s = Session::new(); - open_bytes_as(&mut s, "shot.exr", &bytes, None, None).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.icc_profile.as_deref().map(|v| v.as_slice()), Some(linear.as_slice())); - - // On: the same file opens as ACEScg. - let mut s = Session::new(); - s.prefs.edit(|p| p.file_handling.exr_aces_cg = true); - open_bytes_as(&mut s, "shot.exr", &bytes, None, None).unwrap(); - let d = &s.active().unwrap().doc; - assert_eq!(d.icc_profile.as_deref().map(|v| v.as_slice()), Some(aces.as_slice())); - } -} diff --git a/crates/engine/src/filters.rs b/crates/engine/src/filters.rs index e40be7b6ac..ed709515ee 100644 --- a/crates/engine/src/filters.rs +++ b/crates/engine/src/filters.rs @@ -234,7 +234,10 @@ pub(crate) fn has_filterable_layer(s: &Session) -> std::result::Result<(), Strin pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result { validate_params(id, p)?; // Session state some filters read (colours) becomes explicit params; external inputs resolve here. - let prepared = crate::filters_ext::prepare(s, id, p); + let mut prepared = crate::filters_ext::prepare(s, id, p); + if id == "filter.distort.displace" { + prepared = crate::filters_ext::displace_request(&prepared); + } let p = &prepared; let mut fp = params_for(id, p).ok_or_else(|| EngineError::Other(format!("unknown filter {id}")))?; crate::filters_ext::resolve(s, &mut fp, p)?; @@ -262,15 +265,16 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result }; let selection = doc.selection.clone(); let sel_bounds = selection.as_ref().map(photocraft_raster::Surface::content_bounds); - // Distortions centre on the selection when there is one, else the canvas. + // Distortions centre on the selection when there is one, else the canvas. Clouds and + // Fibers place and size their pattern by the canvas either way; the selection masks them. let doc_bounds = doc.bounds(); - let bounds = sel_bounds.filter(|b| !b.is_empty()).unwrap_or(doc_bounds); + let bounds = if fp.sizes_by_canvas() { doc_bounds } else { sel_bounds.filter(|b| !b.is_empty()).unwrap_or(doc_bounds) }; // Neighbourhood filters repeat edge pixels at the canvas edge (or past it, where the // layer has off-canvas pixels) instead of fading in transparency, as Photoshop does. if let Some(surf) = crate::channel_cmds::channel_surface_for_filter(doc, Some(layer), &p)? { let content = doc_bounds.union(&surf.content_bounds()); let area = algo::output_area(&fp, content, bounds, sel_bounds); - *surf = filter(surf, &fp, area, bounds, selection.as_ref(), doc_bounds.union(&content))?; + *surf = filter(surf, &fp, area, bounds, selection.as_ref(), algo::separable::edge_extent(doc_bounds, content))?; return Ok(fp.clone()); } // Filters that make pixels transparent (Color to Alpha) turn the Background into a @@ -292,9 +296,29 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result let surf = match &mut l.content { LayerContent::Raster(surf) => surf, LayerContent::Smart(_) => { + // A smart filter re-renders from its params alone, without the document's + // channels, so Lens Blur's alpha-channel depth would be lost (#7400). + if matches!(fp, FilterParams::LensBlur { depth: algo::DepthSource::Channel, .. }) { + return Err(EngineError::Other("Lens Blur can't read an alpha channel as depth on a Smart Object: rasterize the layer first".into())); + } + // A smart filter re-renders from its params alone: a Displace map read from an + // open document, which the re-render can't read again, is kept in them, as + // Photoshop embeds it (#7423). A `mapPath` stays a path, read next to the + // document on each re-render. + let params = match &fp { + FilterParams::Displace { map: Some(map), .. } + if photocraft_io::smart_map::displace_embedded_map(¶ms).is_none() && crate::filters_ext::map_path(¶ms).is_none() => + { + photocraft_io::smart_map::displace_embed_map(¶ms, map).ok_or_else(|| { + EngineError::Other( + "Displace: the displacement map is too large to keep in a Smart Object's filter: rasterize the layer first".into(), + ) + })? + } + _ => params.clone(), + }; // Non-destructive: record the filter and re-render the smart object from its source. - let sf = - SmartFilter { command: id.clone(), params: params.clone(), blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; + let sf = SmartFilter { command: id.clone(), params, blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; return crate::smart_cmds::add_smart_filter(doc, layer, sf, selection.as_ref()).map(|()| fp); } _ => return Err(EngineError::Other("not a pixel layer".into())), @@ -302,9 +326,9 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result let before = locks.transparency.then(|| surf.clone()); let content = surf.content_bounds(); let area = algo::output_area(&fp, content, bounds, sel_bounds); - *surf = filter(surf, &fp, area, bounds, selection.as_ref(), doc_bounds.union(&content))?; + *surf = filter(surf, &fp, area, bounds, selection.as_ref(), algo::separable::edge_extent(doc_bounds, content))?; if let Some(before) = &before { - keep_alpha(before, surf); + keep_alpha(before, surf, &|| ctx.cancelled())?; } Ok(fp) }, @@ -313,25 +337,56 @@ pub(crate) fn run_filter(s: &mut Session, id: &str, p: &Value) -> Result } /// Lock transparency: put the layer's alpha back after a filter, in the tiles it changed. -fn keep_alpha(old: &photocraft_raster::Surface, new: &mut photocraft_raster::Surface) { +/// +/// The tiles are independent, so they are fixed in parallel, each in a scratch surface of its +/// own, and `cancelled` is asked before each one: on a 24 MP layer (a Background, whose +/// transparency is locked) a serial pass took about 400 ms that a cancelled job had to sit out +/// (#6205). `Err(Cancelled)` leaves `new` holding all its tiles, some of them not yet fixed. +pub(crate) fn keep_alpha(old: &photocraft_raster::Surface, new: &mut photocraft_raster::Surface, cancelled: &(dyn Fn() -> bool + Sync)) -> Result<()> { + use photocraft_raster::Surface; + use rayon::prelude::*; if old.format() != new.format() || !new.format().alpha { - return; + return Ok(()); } let mut coords: Vec<_> = new.tiles().filter(|(c, t)| old.tile(**c).is_none_or(|o| !std::sync::Arc::ptr_eq(o, t))).map(|(c, _)| *c).collect(); coords.extend(old.tiles().filter(|(c, _)| new.tile(**c).is_none()).map(|(c, _)| *c)); - let n = new.channels(); - for c in coords { - let r = c.rect(); - let o = old.read_region(r); - let mut v = new.read_region(r); - for (pn, po) in v.chunks_exact_mut(n).zip(o.chunks_exact(n)) { - if let (Some(a), Some(b)) = (pn.last_mut(), po.last()) { - *a = *b; + let (fmt, n) = (new.format(), new.channels()); + let default = new.default_pixel(); + // Each changed tile alone in a surface (a tile `new` lacks is taken as its default pixel, as + // `write_region` would create it). + let mut tiles: Vec<(photocraft_geom::Rect, Surface)> = coords + .iter() + .map(|c| { + let mut s = Surface::with_default(fmt, &default); + s.put_tiles(new.take_tiles(c.rect())); + (c.rect(), s) + }) + .collect(); + let fixed = tiles + .par_iter_mut() + .map(|(r, s)| { + if cancelled() { + return false; } - } - new.write_region(r, &v); + let o = old.read_region(*r); + let mut v = s.read_region(*r); + for (pn, po) in v.chunks_exact_mut(n).zip(o.chunks_exact(n)) { + if let (Some(a), Some(b)) = (pn.last_mut(), po.last()) { + *a = *b; + } + } + s.write_region(*r, &v); + true + }) + .reduce(|| true, |a, b| a && b); + for (_, s) in tiles { + new.put_tiles(s.tiles().map(|(c, t)| (*c, t.clone()))); + } + if !fixed { + return Err(EngineError::Cancelled); } new.prune(); + Ok(()) } macro_rules! filter_cmd { @@ -443,389 +498,4 @@ pub fn last_filter(s: &Session) -> Option<(String, Value)> { } #[cfg(test)] -mod tests { - use super::*; - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 48, "height": 32})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("edit.fill", json!({"color": "#3366cc"})).ok(); - s - } - - fn active_pixels(s: &Session) -> Vec { - let d = s.active().unwrap(); - let l = d.doc.layer(d.active_layer.unwrap()).unwrap(); - l.surface().unwrap().read_region(photocraft_geom::Rect::new(0, 0, 48, 32)) - } - - fn mask_session(reveal_selection: bool) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 16, "height": 16, "background": "#ff0000"})).unwrap(); - if reveal_selection { - s.execute("select.all", json!({})).unwrap(); - s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - } else { - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - } - s - } - - fn mask_pixels(s: &Session) -> Vec { - let d = s.active().unwrap(); - let mask = d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap(); - mask.surface.read_region(d.doc.bounds()) - } - - fn canvas_pixels(s: &Session) -> Vec { - let d = s.active().unwrap(); - d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(d.doc.bounds()) - } - - fn paint_pattern(s: &mut Session) { - s.edit("pattern", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - let surf = l.surface_mut().unwrap(); - for y in 0..32 { - for x in 0..48 { - let v = ((x * 5 + y * 3) % 17) as f32 / 16.0; - surf.write_pixel(x, y, &[v, 1.0 - v, (x % 3) as f32 / 2.0, 1.0]); - } - } - Ok(()) - }) - .unwrap(); - } - - const ALL: [&str; 24] = [ - "filter.blur.gaussianBlur", - "filter.blur.boxBlur", - "filter.blur.motionBlur", - "filter.blur.radialBlur", - "filter.blur.surfaceBlur", - "filter.sharpen.unsharpMask", - "filter.sharpen.smartSharpen", - "filter.noise.addNoise", - "filter.noise.median", - "filter.noise.dustAndScratches", - "filter.pixelate.mosaic", - "filter.stylize.emboss", - "filter.stylize.findEdges", - "filter.stylize.solarize", - "filter.distort.twirl", - "filter.distort.pinch", - "filter.distort.spherize", - "filter.distort.wave", - "filter.distort.ripple", - "filter.distort.polarCoordinates", - "filter.other.highPass", - "filter.other.minimum", - "filter.other.maximum", - "filter.other.offset", - ]; - - #[test] - fn every_filter_command_runs_changes_pixels_and_undoes() { - for id in ALL { - let mut s = session(); - paint_pattern(&mut s); - let before = active_pixels(&s); - let params = match id { - "filter.other.offset" => json!({"horizontal": 5, "vertical": 2}), - "filter.noise.dustAndScratches" => json!({"radius": 2, "threshold": 0}), - "filter.noise.median" => json!({"radius": 2}), - _ => json!({}), - }; - let r = s.execute(id, params).unwrap_or_else(|e| panic!("{id}: {e}")); - assert!(r.get("filter").is_some(), "{id}"); - assert_ne!(active_pixels(&s), before, "{id} changed nothing"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(active_pixels(&s), before, "{id} undo"); - } - } - - #[test] - fn preset_filters_record_their_own_name() { - // #528: one-step presets run a generic algorithm but name the step after themselves; - // the generic commands keep the algorithm's name. - let specs = specs(); - for (id, name) in [ - ("filter.blur.blur", "Blur"), - ("filter.blur.blurMore", "Blur More"), - ("filter.sharpen.sharpen", "Sharpen"), - ("filter.sharpen.sharpenMore", "Sharpen More"), - ("filter.sharpen.sharpenEdges", "Sharpen Edges"), - ("filter.noise.despeckle", "Despeckle"), - ("filter.blur.gaussianBlur", "Gaussian Blur"), - ("filter.sharpen.unsharpMask", "Unsharp Mask"), - ("filter.blur.surfaceBlur", "Surface Blur"), - ] { - let mut s = session(); - let steps = s.active().unwrap().history.past_len(); - s.execute(id, json!({})).unwrap(); - let h = &s.active().unwrap().history; - assert_eq!((h.past_len(), h.undo_label()), (steps + 1, Some(name)), "{id}"); - let label = specs.iter().find(|c| c.id == id).unwrap().label; - assert_eq!(label.trim_end_matches('…'), name, "{id}: the step is named like the command"); - } - } - - #[test] - fn specs_are_complete_and_documented() { - let specs = specs(); - for id in ALL { - let spec = specs.iter().find(|c| c.id == id).unwrap_or_else(|| panic!("missing {id}")); - assert!(spec.params.starts_with('{'), "{id}"); - assert!(!spec.menu.is_empty(), "{id}"); - } - } - - #[test] - fn disabled_without_pixel_layer() { - let mut s = Session::new(); - assert!(!s.is_enabled("filter.blur.gaussianBlur")); - s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); - s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); - assert!(matches!(s.execute("filter.blur.gaussianBlur", json!({})), Err(EngineError::Disabled(..)))); - } - - #[test] - fn selection_limits_the_filter() { - let mut s = session(); - paint_pattern(&mut s); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap(); - let has_sel = s.active().unwrap().doc.selection.is_some(); - assert!(has_sel); - let before = active_pixels(&s); - s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap(); - let after = active_pixels(&s); - if has_sel { - // A pixel far from the selection is unchanged. - let i = (30 * 48 + 40) * 4; - assert_eq!(after[i..i + 4], before[i..i + 4]); - let j = (5 * 48 + 5) * 4; - assert_ne!(after[j..j + 4], before[j..j + 4]); - } - } - - #[test] - fn solarize_and_add_noise_process_untouched_reveal_all_masks() { - for (id, params) in [ - ("filter.stylize.solarize", json!({"target": "mask"})), - ("filter.noise.addNoise", json!({"amount": 100, "seed": 1, "monochromatic": true, "target": "mask"})), - ] { - let mut reveal_all = mask_session(false); - let mut reveal_selection = mask_session(true); - let all_mask = reveal_all.active().unwrap().doc.layer(reveal_all.active().unwrap().active_layer.unwrap()).unwrap().mask.as_ref().unwrap(); - assert!(all_mask.surface.content_bounds().is_empty(), "{id}: Reveal All stores its white canvas in the default pixel"); - let initial_all = mask_pixels(&reveal_all); - let initial_selection = mask_pixels(&reveal_selection); - assert_eq!(initial_all, initial_selection, "{id}: masks start with equal white values"); - assert_eq!(initial_all, vec![1.0; 16 * 16], "{id}: the full canvas starts revealed"); - let pixels_before = canvas_pixels(&reveal_all); - - reveal_all.execute(id, params.clone()).unwrap(); - reveal_selection.execute(id, params).unwrap(); - - let actual = mask_pixels(&reveal_all); - let expected = mask_pixels(&reveal_selection); - assert!( - actual == expected, - "{id}: Reveal All differs from Reveal Selection: first={:?}/{:?}, changed pixels={}/{}", - actual[0], - expected[0], - actual.iter().filter(|&&v| v != 1.0).count(), - expected.iter().filter(|&&v| v != 1.0).count() - ); - assert_eq!(canvas_pixels(&reveal_all), pixels_before, "{id}: the mask target must leave layer pixels unchanged"); - if id == "filter.stylize.solarize" { - assert!(actual.iter().all(|&v| v == 0.0), "Solarize maps the white mask to black"); - } else { - assert!(actual.iter().any(|&v| v != 1.0), "seeded Add Noise changes the Reveal All mask"); - } - } - } - - #[test] - fn minimum_signed_offset_matches_its_wrapped_distance() { - for depth in [8, 16, 32] { - let make_session = || { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 48, "height": 32, "depth": depth})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - paint_pattern(&mut s); - s - }; - let mut actual = make_session(); - let before = active_pixels(&actual); - actual.execute("filter.other.offset", json!({"horizontal": i32::MIN, "vertical": i32::MIN})).unwrap(); - let mut expected = make_session(); - expected.execute("filter.other.offset", json!({"horizontal": i32::MIN.rem_euclid(48), "vertical": i32::MIN.rem_euclid(32)})).unwrap(); - let after = active_pixels(&actual); - assert_eq!(after, active_pixels(&expected), "depth {depth}"); - assert_ne!(after, before); - actual.execute("edit.undo", json!({})).unwrap(); - assert_eq!(active_pixels(&actual), before); - actual.execute("edit.redo", json!({})).unwrap(); - assert_eq!(active_pixels(&actual), after); - } - } - #[test] - fn huge_offsets_clamp_instead_of_wrapping() { - // 3e9 wrapped to a negative offset through `as i32`; it saturates now, so it - // equals the maximal in-range offset instead of shifting the other way. - let mut a = session(); - a.execute("filter.other.offset", json!({"horizontal": 3_000_000_000_i64, "vertical": 0})).unwrap(); - let mut b = session(); - b.execute("filter.other.offset", json!({"horizontal": 2_147_483_647_i64, "vertical": 0})).unwrap(); - assert_eq!(active_pixels(&a), active_pixels(&b)); - } - - #[test] - fn last_filter_repeats_with_same_params() { - let mut s = session(); - paint_pattern(&mut s); - assert!(!s.is_enabled("filter.lastFilter")); - s.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); - let once = active_pixels(&s); - s.execute("filter.lastFilter", json!({})).unwrap(); - let twice = active_pixels(&s); - assert_ne!(once, twice); - // Offsetting 3 twice equals offsetting 6 once. - let mut s2 = session(); - paint_pattern(&mut s2); - s2.execute("filter.other.offset", json!({"horizontal": 6})).unwrap(); - assert_eq!(active_pixels(&s2), twice); - } - - #[test] - fn recorded_last_filter_replays_the_filter_it_repeated() { - // #3089: the action holds the filter with its settings, not a "repeat whatever ran - // last" step that fails in a new session. - let mut s = session(); - paint_pattern(&mut s); - s.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); - s.execute("actions.record", json!({"name": "Again"})).unwrap(); - s.execute("filter.lastFilter", json!({})).unwrap(); - s.execute("actions.stop", json!({})).unwrap(); - let steps = s.execute("actions.get", json!({"action": "Again"})).unwrap()["steps"].clone(); - assert_eq!(steps, json!([["filter.other.offset", {"horizontal": 3}]])); - let action = s.actions.list[0].clone(); - - let mut fresh = session(); - paint_pattern(&mut fresh); - fresh.actions.list.push(action); - let played = fresh.execute("actions.play", json!({"action": "Again"})).unwrap(); - assert!(played.get("failed").is_none(), "{played}"); - let mut direct = session(); - paint_pattern(&mut direct); - direct.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); - assert_eq!(active_pixels(&fresh), active_pixels(&direct)); - } - - #[test] - fn last_filter_applies_to_the_active_layer() { - let mut s = session(); - paint_pattern(&mut s); - let first = s.active().unwrap().active_layer.unwrap(); - // Dialogs (Filter Gallery, Camera Raw…) record the layer they ran on. - s.execute("filter.other.offset", json!({"horizontal": 3, "layer": first.0})).unwrap(); - let first_once = active_pixels(&s); - s.execute("layer.new.layer", json!({})).unwrap(); - paint_pattern(&mut s); - let before = active_pixels(&s); - s.execute("filter.lastFilter", json!({})).unwrap(); - assert_ne!(active_pixels(&s), before, "the active layer is filtered"); - let d = s.active().unwrap(); - let l = d.doc.layer(first).unwrap(); - assert_eq!(l.surface().unwrap().read_region(photocraft_geom::Rect::new(0, 0, 48, 32)), first_once, "the first layer is left alone"); - } - - #[test] - fn params_map_to_algorithm_units() { - assert_eq!(params_for("filter.blur.gaussianBlur", &json!({"radius": 4.5})), Some(FilterParams::GaussianBlur { radius: 4.5 })); - assert_eq!( - params_for("filter.blur.radialBlur", &json!({"quality": "best"})), - Some(FilterParams::RadialBlur { amount: 10.0, method: RadialMethod::Spin, quality: RadialQuality::Best, center_x: 0.5, center_y: 0.5 }) - ); - assert_eq!( - params_for("filter.noise.addNoise", &json!({"amount": 10, "distribution": "gaussian", "monochromatic": true})), - Some(FilterParams::AddNoise { amount: 10.0, distribution: Distribution::Gaussian, monochromatic: true, seed: 0 }) - ); - assert_eq!(params_for("filter.blur.gaussianBlur", &json!({"radius": -3})), Some(FilterParams::GaussianBlur { radius: 0.1 })); - assert!(params_for("filter.nope", &json!({})).is_none()); - } - - #[test] - fn minimum_maximum_preserve_roundness() { - for depth in [8, 16, 32] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 48, "height": 32, "depth": depth})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - // A white dot on black. - s.edit("dot", |doc, active| { - let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); - surf.fill_rect(photocraft_geom::Rect::new(0, 0, 48, 32), &[0.0, 0.0, 0.0, 1.0]); - surf.write_pixel(24, 16, &[1.0, 1.0, 1.0, 1.0]); - Ok(()) - }) - .unwrap(); - let before = active_pixels(&s); - let lit = |s: &Session, x: usize, y: usize| active_pixels(s)[(y * 48 + x) * 4] > 0.5; - s.execute("filter.other.maximum", json!({"radius": 4, "preserve": "roundness"})).unwrap(); - // Grown into a disc of radius 4: (4, 0) and (2, 3) are in, the square's corner isn't. - assert!(lit(&s, 28, 16) && lit(&s, 26, 19), "depth {depth}"); - assert!(!lit(&s, 28, 20) && !lit(&s, 27, 19), "depth {depth}"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(active_pixels(&s), before); - // The default is Squareness: the corner is taken. - s.execute("filter.other.maximum", json!({"radius": 4})).unwrap(); - assert!(lit(&s, 28, 20), "depth {depth}"); - // Minimum with roundness: only the centre of that 9×9 square holds a whole disc. - s.execute("filter.other.minimum", json!({"radius": 4, "preserve": "roundness"})).unwrap(); - assert!(!lit(&s, 20, 12) && lit(&s, 24, 16), "depth {depth}"); - } - assert_eq!( - params_for("filter.other.minimum", &json!({"radius": 2, "preserve": "roundness"})), - Some(FilterParams::Minimum { radius: 2.0, preserve: Preserve::Roundness }) - ); - assert_eq!(params_for("filter.other.maximum", &json!({})), Some(FilterParams::Maximum { radius: 1.0, preserve: Preserve::Squareness })); - } - - #[test] - fn smart_object_records_smart_filter() { - use photocraft_doc::{Layer, SmartObject, SmartSource}; - let mut s = session(); - s.edit("smart", |doc, active| { - let mut cache = photocraft_raster::Surface::new(doc.pixel_format()); - cache.fill_rect(photocraft_geom::Rect::new(0, 0, 10, 10), &[1.0, 0.0, 0.0, 1.0]); - let l = Layer::new( - "so", - LayerContent::Smart(SmartObject { - contents_id: photocraft_doc::SmartContentsId::fresh(), - source: SmartSource::Linked { path: String::new() }, - transform: photocraft_geom::Affine::IDENTITY, - smart_filters: vec![], - cache: Some(cache), - psd_raw: None, - filters_enabled: true, - filter_mask: None, - warp: None, - stack_mode: None, - perspective: None, - }), - ); - *active = Some(doc.insert_above(*active, l)); - Ok(()) - }) - .unwrap(); - s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); - let d = s.active().unwrap(); - let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!() }; - assert_eq!(sm.smart_filters.len(), 1); - assert_eq!(sm.smart_filters[0].command, "filter.blur.gaussianBlur"); - assert_eq!(sm.smart_filters[0].params["radius"], json!(2)); - assert!(sm.cache.as_ref().unwrap().pixel(10, 5)[3] > 0.0, "cache was blurred"); - } -} +mod tests; diff --git a/crates/engine/src/filters/tests.rs b/crates/engine/src/filters/tests.rs new file mode 100644 index 0000000000..5b3f3a3703 --- /dev/null +++ b/crates/engine/src/filters/tests.rs @@ -0,0 +1,515 @@ +use super::*; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 48, "height": 32})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#3366cc"})).ok(); + s +} + +fn active_pixels(s: &Session) -> Vec { + let d = s.active().unwrap(); + let l = d.doc.layer(d.active_layer.unwrap()).unwrap(); + l.surface().unwrap().read_region(photocraft_geom::Rect::new(0, 0, 48, 32)) +} + +fn mask_session(reveal_selection: bool) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16, "background": "#ff0000"})).unwrap(); + if reveal_selection { + s.execute("select.all", json!({})).unwrap(); + s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); + } else { + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + } + s +} + +fn mask_pixels(s: &Session) -> Vec { + let d = s.active().unwrap(); + let mask = d.doc.layer(d.active_layer.unwrap()).unwrap().mask.as_ref().unwrap(); + mask.surface.read_region(d.doc.bounds()) +} + +fn canvas_pixels(s: &Session) -> Vec { + let d = s.active().unwrap(); + d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(d.doc.bounds()) +} + +fn paint_pattern(s: &mut Session) { + s.edit("pattern", |doc, active| { + let l = doc.layer_mut(active.unwrap()).unwrap(); + let surf = l.surface_mut().unwrap(); + for y in 0..32 { + for x in 0..48 { + let v = ((x * 5 + y * 3) % 17) as f32 / 16.0; + surf.write_pixel(x, y, &[v, 1.0 - v, (x % 3) as f32 / 2.0, 1.0]); + } + } + Ok(()) + }) + .unwrap(); +} + +const ALL: [&str; 24] = [ + "filter.blur.gaussianBlur", + "filter.blur.boxBlur", + "filter.blur.motionBlur", + "filter.blur.radialBlur", + "filter.blur.surfaceBlur", + "filter.sharpen.unsharpMask", + "filter.sharpen.smartSharpen", + "filter.noise.addNoise", + "filter.noise.median", + "filter.noise.dustAndScratches", + "filter.pixelate.mosaic", + "filter.stylize.emboss", + "filter.stylize.findEdges", + "filter.stylize.solarize", + "filter.distort.twirl", + "filter.distort.pinch", + "filter.distort.spherize", + "filter.distort.wave", + "filter.distort.ripple", + "filter.distort.polarCoordinates", + "filter.other.highPass", + "filter.other.minimum", + "filter.other.maximum", + "filter.other.offset", +]; + +#[test] +fn every_filter_command_runs_changes_pixels_and_undoes() { + for id in ALL { + let mut s = session(); + paint_pattern(&mut s); + let before = active_pixels(&s); + let params = match id { + "filter.other.offset" => json!({"horizontal": 5, "vertical": 2}), + "filter.noise.dustAndScratches" => json!({"radius": 2, "threshold": 0}), + "filter.noise.median" => json!({"radius": 2}), + _ => json!({}), + }; + let r = s.execute(id, params).unwrap_or_else(|e| panic!("{id}: {e}")); + assert!(r.get("filter").is_some(), "{id}"); + assert_ne!(active_pixels(&s), before, "{id} changed nothing"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(active_pixels(&s), before, "{id} undo"); + } +} + +#[test] +fn preset_filters_record_their_own_name() { + // #528: one-step presets run a generic algorithm but name the step after themselves; + // the generic commands keep the algorithm's name. + let specs = specs(); + for (id, name) in [ + ("filter.blur.blur", "Blur"), + ("filter.blur.blurMore", "Blur More"), + ("filter.sharpen.sharpen", "Sharpen"), + ("filter.sharpen.sharpenMore", "Sharpen More"), + ("filter.sharpen.sharpenEdges", "Sharpen Edges"), + ("filter.noise.despeckle", "Despeckle"), + ("filter.blur.gaussianBlur", "Gaussian Blur"), + ("filter.sharpen.unsharpMask", "Unsharp Mask"), + ("filter.blur.surfaceBlur", "Surface Blur"), + ] { + let mut s = session(); + let steps = s.active().unwrap().history.past_len(); + s.execute(id, json!({})).unwrap(); + let h = &s.active().unwrap().history; + assert_eq!((h.past_len(), h.undo_label()), (steps + 1, Some(name)), "{id}"); + let label = specs.iter().find(|c| c.id == id).unwrap().label; + assert_eq!(label.trim_end_matches('…'), name, "{id}: the step is named like the command"); + } +} + +#[test] +fn specs_are_complete_and_documented() { + let specs = specs(); + for id in ALL { + let spec = specs.iter().find(|c| c.id == id).unwrap_or_else(|| panic!("missing {id}")); + assert!(spec.params.starts_with('{'), "{id}"); + assert!(!spec.menu.is_empty(), "{id}"); + } +} + +#[test] +fn disabled_without_pixel_layer() { + let mut s = Session::new(); + assert!(!s.is_enabled("filter.blur.gaussianBlur")); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + assert!(matches!(s.execute("filter.blur.gaussianBlur", json!({})), Err(EngineError::Disabled(..)))); +} + +#[test] +fn selection_limits_the_filter() { + let mut s = session(); + paint_pattern(&mut s); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap(); + let has_sel = s.active().unwrap().doc.selection.is_some(); + assert!(has_sel); + let before = active_pixels(&s); + s.execute("filter.blur.gaussianBlur", json!({"radius": 3})).unwrap(); + let after = active_pixels(&s); + if has_sel { + // A pixel far from the selection is unchanged. + let i = (30 * 48 + 40) * 4; + assert_eq!(after[i..i + 4], before[i..i + 4]); + let j = (5 * 48 + 5) * 4; + assert_ne!(after[j..j + 4], before[j..j + 4]); + } +} + +#[test] +fn solarize_and_add_noise_process_untouched_reveal_all_masks() { + for (id, params) in [ + ("filter.stylize.solarize", json!({"target": "mask"})), + ("filter.noise.addNoise", json!({"amount": 100, "seed": 1, "monochromatic": true, "target": "mask"})), + ] { + let mut reveal_all = mask_session(false); + let mut reveal_selection = mask_session(true); + let all_mask = reveal_all.active().unwrap().doc.layer(reveal_all.active().unwrap().active_layer.unwrap()).unwrap().mask.as_ref().unwrap(); + assert!(all_mask.surface.content_bounds().is_empty(), "{id}: Reveal All stores its white canvas in the default pixel"); + let initial_all = mask_pixels(&reveal_all); + let initial_selection = mask_pixels(&reveal_selection); + assert_eq!(initial_all, initial_selection, "{id}: masks start with equal white values"); + assert_eq!(initial_all, vec![1.0; 16 * 16], "{id}: the full canvas starts revealed"); + let pixels_before = canvas_pixels(&reveal_all); + + reveal_all.execute(id, params.clone()).unwrap(); + reveal_selection.execute(id, params).unwrap(); + + let actual = mask_pixels(&reveal_all); + let expected = mask_pixels(&reveal_selection); + assert!( + actual == expected, + "{id}: Reveal All differs from Reveal Selection: first={:?}/{:?}, changed pixels={}/{}", + actual[0], + expected[0], + actual.iter().filter(|&&v| v != 1.0).count(), + expected.iter().filter(|&&v| v != 1.0).count() + ); + assert_eq!(canvas_pixels(&reveal_all), pixels_before, "{id}: the mask target must leave layer pixels unchanged"); + if id == "filter.stylize.solarize" { + assert!(actual.iter().all(|&v| v == 0.0), "Solarize maps the white mask to black"); + } else { + assert!(actual.iter().any(|&v| v != 1.0), "seeded Add Noise changes the Reveal All mask"); + } + } +} + +#[test] +fn minimum_signed_offset_matches_its_wrapped_distance() { + for depth in [8, 16, 32] { + let make_session = || { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 48, "height": 32, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + paint_pattern(&mut s); + s + }; + let mut actual = make_session(); + let before = active_pixels(&actual); + actual.execute("filter.other.offset", json!({"horizontal": i32::MIN, "vertical": i32::MIN})).unwrap(); + let mut expected = make_session(); + expected.execute("filter.other.offset", json!({"horizontal": i32::MIN.rem_euclid(48), "vertical": i32::MIN.rem_euclid(32)})).unwrap(); + let after = active_pixels(&actual); + assert_eq!(after, active_pixels(&expected), "depth {depth}"); + assert_ne!(after, before); + actual.execute("edit.undo", json!({})).unwrap(); + assert_eq!(active_pixels(&actual), before); + actual.execute("edit.redo", json!({})).unwrap(); + assert_eq!(active_pixels(&actual), after); + } +} +#[test] +fn huge_offsets_clamp_instead_of_wrapping() { + // 3e9 wrapped to a negative offset through `as i32`; it saturates now, so it + // equals the maximal in-range offset instead of shifting the other way. + let mut a = session(); + a.execute("filter.other.offset", json!({"horizontal": 3_000_000_000_i64, "vertical": 0})).unwrap(); + let mut b = session(); + b.execute("filter.other.offset", json!({"horizontal": 2_147_483_647_i64, "vertical": 0})).unwrap(); + assert_eq!(active_pixels(&a), active_pixels(&b)); +} + +#[test] +fn last_filter_repeats_with_same_params() { + let mut s = session(); + paint_pattern(&mut s); + assert!(!s.is_enabled("filter.lastFilter")); + s.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); + let once = active_pixels(&s); + s.execute("filter.lastFilter", json!({})).unwrap(); + let twice = active_pixels(&s); + assert_ne!(once, twice); + // Offsetting 3 twice equals offsetting 6 once. + let mut s2 = session(); + paint_pattern(&mut s2); + s2.execute("filter.other.offset", json!({"horizontal": 6})).unwrap(); + assert_eq!(active_pixels(&s2), twice); +} + +#[test] +fn recorded_last_filter_replays_the_filter_it_repeated() { + // #3089: the action holds the filter with its settings, not a "repeat whatever ran + // last" step that fails in a new session. + let mut s = session(); + paint_pattern(&mut s); + s.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); + s.execute("actions.record", json!({"name": "Again"})).unwrap(); + s.execute("filter.lastFilter", json!({})).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + let steps = s.execute("actions.get", json!({"action": "Again"})).unwrap()["steps"].clone(); + assert_eq!(steps, json!([["filter.other.offset", {"horizontal": 3}]])); + let action = s.actions.list[0].clone(); + + let mut fresh = session(); + paint_pattern(&mut fresh); + fresh.actions.list.push(action); + let played = fresh.execute("actions.play", json!({"action": "Again"})).unwrap(); + assert!(played.get("failed").is_none(), "{played}"); + let mut direct = session(); + paint_pattern(&mut direct); + direct.execute("filter.other.offset", json!({"horizontal": 3})).unwrap(); + assert_eq!(active_pixels(&fresh), active_pixels(&direct)); +} + +#[test] +fn last_filter_applies_to_the_active_layer() { + let mut s = session(); + paint_pattern(&mut s); + let first = s.active().unwrap().active_layer.unwrap(); + // Dialogs (Filter Gallery, Camera Raw…) record the layer they ran on. + s.execute("filter.other.offset", json!({"horizontal": 3, "layer": first.0})).unwrap(); + let first_once = active_pixels(&s); + s.execute("layer.new.layer", json!({})).unwrap(); + paint_pattern(&mut s); + let before = active_pixels(&s); + s.execute("filter.lastFilter", json!({})).unwrap(); + assert_ne!(active_pixels(&s), before, "the active layer is filtered"); + let d = s.active().unwrap(); + let l = d.doc.layer(first).unwrap(); + assert_eq!(l.surface().unwrap().read_region(photocraft_geom::Rect::new(0, 0, 48, 32)), first_once, "the first layer is left alone"); +} + +#[test] +fn params_map_to_algorithm_units() { + assert_eq!(params_for("filter.blur.gaussianBlur", &json!({"radius": 4.5})), Some(FilterParams::GaussianBlur { radius: 4.5 })); + assert_eq!( + params_for("filter.blur.radialBlur", &json!({"quality": "best"})), + Some(FilterParams::RadialBlur { amount: 10.0, method: RadialMethod::Spin, quality: RadialQuality::Best, center_x: 0.5, center_y: 0.5 }) + ); + assert_eq!( + params_for("filter.noise.addNoise", &json!({"amount": 10, "distribution": "gaussian", "monochromatic": true})), + Some(FilterParams::AddNoise { amount: 10.0, distribution: Distribution::Gaussian, monochromatic: true, seed: 0 }) + ); + assert_eq!(params_for("filter.blur.gaussianBlur", &json!({"radius": -3})), Some(FilterParams::GaussianBlur { radius: 0.1 })); + assert!(params_for("filter.nope", &json!({})).is_none()); +} + +#[test] +fn minimum_maximum_preserve_roundness() { + for depth in [8, 16, 32] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 48, "height": 32, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + // A white dot on black. + s.edit("dot", |doc, active| { + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + surf.fill_rect(photocraft_geom::Rect::new(0, 0, 48, 32), &[0.0, 0.0, 0.0, 1.0]); + surf.write_pixel(24, 16, &[1.0, 1.0, 1.0, 1.0]); + Ok(()) + }) + .unwrap(); + let before = active_pixels(&s); + let lit = |s: &Session, x: usize, y: usize| active_pixels(s)[(y * 48 + x) * 4] > 0.5; + s.execute("filter.other.maximum", json!({"radius": 4, "preserve": "roundness"})).unwrap(); + // Grown into a disc of radius 4: (4, 0) and (2, 3) are in, the square's corner isn't. + assert!(lit(&s, 28, 16) && lit(&s, 26, 19), "depth {depth}"); + assert!(!lit(&s, 28, 20) && !lit(&s, 27, 19), "depth {depth}"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(active_pixels(&s), before); + // The default is Squareness: the corner is taken. + s.execute("filter.other.maximum", json!({"radius": 4})).unwrap(); + assert!(lit(&s, 28, 20), "depth {depth}"); + // Minimum with roundness: only the centre of that 9×9 square holds a whole disc. + s.execute("filter.other.minimum", json!({"radius": 4, "preserve": "roundness"})).unwrap(); + assert!(!lit(&s, 20, 12) && lit(&s, 24, 16), "depth {depth}"); + } + assert_eq!( + params_for("filter.other.minimum", &json!({"radius": 2, "preserve": "roundness"})), + Some(FilterParams::Minimum { radius: 2.0, preserve: Preserve::Roundness }) + ); + assert_eq!(params_for("filter.other.maximum", &json!({})), Some(FilterParams::Maximum { radius: 1.0, preserve: Preserve::Squareness })); +} + +#[test] +fn smart_object_records_smart_filter() { + use photocraft_doc::{Layer, SmartObject, SmartSource}; + let mut s = session(); + s.edit("smart", |doc, active| { + let mut cache = photocraft_raster::Surface::new(doc.pixel_format()); + cache.fill_rect(photocraft_geom::Rect::new(0, 0, 10, 10), &[1.0, 0.0, 0.0, 1.0]); + let l = Layer::new( + "so", + LayerContent::Smart(SmartObject { + contents_id: photocraft_doc::SmartContentsId::fresh(), + source: SmartSource::Linked { path: String::new() }, + transform: photocraft_geom::Affine::IDENTITY, + smart_filters: vec![], + cache: Some(cache), + psd_raw: None, + filters_enabled: true, + filter_mask: None, + warp: None, + stack_mode: None, + perspective: None, + }), + ); + *active = Some(doc.insert_above(*active, l)); + Ok(()) + }) + .unwrap(); + s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); + let d = s.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!() }; + assert_eq!(sm.smart_filters.len(), 1); + assert_eq!(sm.smart_filters[0].command, "filter.blur.gaussianBlur"); + assert_eq!(sm.smart_filters[0].params["radius"], json!(2)); + assert!(sm.cache.as_ref().unwrap().pixel(10, 5)[3] > 0.0, "cache was blurred"); +} + +/// The serial `keep_alpha` before #6205, kept as the oracle for the parallel one. +fn keep_alpha_serial(old: &photocraft_raster::Surface, new: &mut photocraft_raster::Surface) { + if old.format() != new.format() || !new.format().alpha { + return; + } + let mut coords: Vec<_> = new.tiles().filter(|(c, t)| old.tile(**c).is_none_or(|o| !std::sync::Arc::ptr_eq(o, t))).map(|(c, _)| *c).collect(); + coords.extend(old.tiles().filter(|(c, _)| new.tile(**c).is_none()).map(|(c, _)| *c)); + let n = new.channels(); + for c in coords { + let r = c.rect(); + let o = old.read_region(r); + let mut v = new.read_region(r); + for (pn, po) in v.chunks_exact_mut(n).zip(o.chunks_exact(n)) { + if let (Some(a), Some(b)) = (pn.last_mut(), po.last()) { + *a = *b; + } + } + new.write_region(r, &v); + } + new.prune(); +} + +/// A layer before and after a filter: tiles the filter changed, one it left shared, one it +/// emptied (only in `old`) and one it added (only in `new`). +fn alpha_pair(sample: photocraft_color::SampleType) -> (photocraft_raster::Surface, photocraft_raster::Surface) { + use photocraft_geom::Rect; + use photocraft_raster::Surface; + let fmt = photocraft_color::PixelFormat { mode: photocraft_color::ColorMode::Rgb, sample, alpha: true }; + let pattern = |r: Rect, k: f32| -> Vec { + (0..r.width() as usize * r.height() as usize * 4).map(|i| ((i as f32 * 0.37 + k).sin() * 0.5 + 0.5).clamp(0.0, 1.0)).collect() + }; + let mut old = Surface::new(fmt); + let r = Rect::new(-40, -20, 700, 300); + old.write_region(r, &pattern(r, 0.0)); + let mut new = old.clone(); + let changed = Rect::new(-40, -20, 400, 300); + new.write_region(changed, &pattern(changed, 1.3)); + let emptied = photocraft_geom::TileCoord::containing(600, 100).rect(); + drop(new.take_tiles(emptied)); + let added = Rect::new(800, 0, 900, 50); + new.write_region(added, &pattern(added, 2.1)); + (old, new) +} + +#[test] +fn keep_alpha_matches_the_serial_pass_at_every_depth() { + use photocraft_color::SampleType; + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let (old, new) = alpha_pair(sample); + let mut want = new.clone(); + keep_alpha_serial(&old, &mut want); + let mut got = new.clone(); + keep_alpha(&old, &mut got, &|| false).unwrap(); + assert!(got == want, "{sample:?}: parallel keep_alpha differs from the serial pass"); + assert_ne!(got, new, "{sample:?}: alpha was put back"); + } +} + +#[test] +fn keep_alpha_stops_when_cancelled_and_keeps_the_tiles() { + let (old, new) = alpha_pair(photocraft_color::SampleType::U8); + let mut got = new.clone(); + let calls = std::sync::atomic::AtomicUsize::new(0); + let cancel = || calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst) >= 2; + assert!(matches!(keep_alpha(&old, &mut got, &cancel), Err(EngineError::Cancelled))); + // The cancel was seen inside the pass (after some tiles), and no tile went missing. + assert!(calls.load(std::sync::atomic::Ordering::SeqCst) > 2); + assert!(new.tiles().all(|(c, _)| got.tile(*c).is_some())); +} + +/// #7423: Displace on a Smart Object keeps its map (read from an open document) in the smart +/// filter, as Photoshop embeds it, so the filter displaces and keeps displacing when the +/// object re-renders. A map chosen again replaces it. +#[test] +fn displace_on_a_smart_object_embeds_its_map() { + let mut s = Session::new(); + // Documents 0 and 1: a white and a black displacement map. + s.execute("file.new", json!({"width": 8, "height": 8, "background": "white"})).unwrap(); + s.execute("file.new", json!({"width": 8, "height": 8, "background": "black"})).unwrap(); + s.execute("file.new", json!({"width": 48, "height": 32, "background": "transparent"})).unwrap(); + s.edit("square", |doc, active| { + let fmt = doc.pixel_format(); + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + surf.fill_rect(photocraft_geom::Rect::new(10, 10, 20, 12), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + Ok(()) + }) + .unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let original = { + let d = s.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!("not a smart object") }; + sm.cache.as_ref().unwrap().read_region(d.doc.bounds()) + }; + let smart = |s: &Session| { + let d = s.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!("not a smart object") }; + (sm.smart_filters[0].params.clone(), sm.cache.as_ref().unwrap().read_region(d.doc.bounds())) + }; + let first_value = |params: &Value| photocraft_io::smart_map::displace_embedded_map(params).expect("an embedded map").data[0]; + + s.execute("filter.distort.displace", json!({"horizontal": 10, "vertical": 0, "mapDocument": 0})).unwrap(); + let (params, displaced) = smart(&s); + assert!(params.get("mapDocument").is_none(), "{params}"); + assert_eq!(first_value(¶ms), 1.0, "the white map"); + assert_ne!(displaced, original, "the smart object is displaced"); + // Re-rendered from its params alone (the filter hidden and shown again): still displaced. + s.execute("layer.smartFilter.setVisible", json!({"index": 0, "visible": false})).unwrap(); + assert_eq!(smart(&s).1, original); + s.execute("layer.smartFilter.setVisible", json!({"index": 0, "visible": true})).unwrap(); + assert_eq!(smart(&s).1, displaced); + + // Another map chosen for the filter replaces the embedded one. + s.execute("layer.smartFilter.setParams", json!({"index": 0, "params": {"mapDocument": 1}})).unwrap(); + let (params, again) = smart(&s); + assert!(params.get("mapDocument").is_none(), "{params}"); + assert_eq!(first_value(¶ms), 0.0, "the black map"); + assert_ne!(again, displaced, "displaced the other way"); + + // The stored params passed back whole (the old embedded map included) with another map + // named: the named map wins, for the filter's own edit and for a new Displace alike. + let mut back = params.as_object().unwrap().clone(); + back.insert("mapDocument".into(), json!(0)); + s.execute("layer.smartFilter.setParams", json!({"index": 0, "params": back.clone()})).unwrap(); + assert_eq!(first_value(&smart(&s).0), 1.0, "the white map, not the embedded black one"); + back.insert("mapDocument".into(), json!(1)); + s.execute("filter.distort.displace", Value::Object(back)).unwrap(); + let d = s.active().unwrap(); + let LayerContent::Smart(sm) = &d.doc.layer(d.active_layer.unwrap()).unwrap().content else { panic!("not a smart object") }; + assert_eq!(first_value(&sm.smart_filters[1].params), 0.0, "the black map, not the passed-back white one"); +} diff --git a/crates/engine/src/filters/validation.rs b/crates/engine/src/filters/validation.rs index 068d37e9da..2744524a85 100644 --- a/crates/engine/src/filters/validation.rs +++ b/crates/engine/src/filters/validation.rs @@ -123,9 +123,11 @@ fn choices(id: &str) -> Option<&'static [(&'static str, &'static str)]> { "filter.distort.zigZag" => &[("style", "pondRipples|outFromCenter|aroundCenter")], "filter.render.lensFlare" => &[("lens", "zoom|prime35|prime105|moviePrime")], "filter.render.lightingEffects" => &[("lightType", "spot|point|infinite"), ("texture", "none|red|green|blue|alpha|luminance")], - "filter.blur.lensBlur" => { - &[("shape", "hexagon|triangle|square|pentagon|heptagon|octagon"), ("depthMap", "none|transparency|layerMask"), ("distribution", "uniform|gaussian")] - } + "filter.blur.lensBlur" => &[ + ("shape", "hexagon|triangle|square|pentagon|heptagon|octagon"), + ("depthMap", "none|transparency|layerMask|channel"), + ("distribution", "uniform|gaussian"), + ], "filter.other.hsbHsl" => &[("inputMode", "rgb|hsb|hsl"), ("rowOrder", "hsb|hsl|rgb")], "filter.video.deInterlace" => &[("eliminate", "oddFields|evenFields"), ("createBy", "interpolation|duplication")], _ => return None, @@ -159,9 +161,9 @@ pub(crate) fn validate_params(id: &str, params: &Value) -> Result<()> { "layer" => (value.as_u64().is_some(), "an unsigned layer id".into()), "coalesce" => (value.is_string(), "a string".into()), "target" => ( - value.as_str().is_some_and(|s| matches!(s, "pixels" | "mask" | "quickMask")) + value.as_str().is_some_and(|s| matches!(s, "pixels" | "mask" | "filterMask" | "quickMask")) || value.as_object().is_some_and(|o| o.len() == 1 && o.get("channel").and_then(Value::as_u64).is_some()), - "pixels, mask, quickMask or {\"channel\":index}".into(), + "pixels, mask, filterMask, quickMask or {\"channel\":index}".into(), ), _ => return Err(bad(format!("unknown parameter `{key}`"))), } @@ -174,368 +176,4 @@ pub(crate) fn validate_params(id: &str, params: &Value) -> Result<()> { } #[cfg(test)] -mod tests { - use std::sync::Arc; - - use photocraft_geom::Rect; - use serde_json::json; - - use super::*; - use crate::Session; - - fn session(depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width":16,"height":12,"depth":depth})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("pattern", |doc, active| { - let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); - surface.fill_rect(Rect::new(0, 0, 8, 12), &[0.2, 0.6, 0.8, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - fn rejected(s: &mut Session, id: &str, key: &str, value: Value) { - let doc = s.active().unwrap().doc.clone(); - let history = s.active().unwrap().history.entries(); - let journal = s.journal.clone(); - let active = s.active().unwrap().active_layer; - let error = s.execute(id, json!({key:value})).unwrap_err(); - assert!(matches!(error, EngineError::BadParams { .. }), "{id}: {error}"); - let error = error.to_string(); - assert!(error.contains(id) && error.contains(key), "{error}"); - assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc), "{id}: {key}"); - assert_eq!(s.active().unwrap().history.entries(), history); - assert_eq!(s.active().unwrap().active_layer, active); - assert_eq!(s.journal, journal); - assert!(s.jobs().is_empty()); - } - - #[test] - fn every_core_filter_rejects_unknown_keys_without_editing() { - for depth in [8, 16, 32] { - let mut s = session(depth); - for spec in crate::filters::specs().into_iter().filter(|s| s.id != "filter.lastFilter") { - rejected(&mut s, spec.id, "radus", json!(3)); - } - } - } - - #[test] - fn core_filter_numbers_reject_wrong_types_and_documented_range_violations() { - // Each numeric field in the core registry, independently of the validation table. - let cases = [ - ("blur.gaussianBlur", "radius", 0.1, 1000.0), - ("blur.boxBlur", "radius", 1.0, 2000.0), - ("blur.motionBlur", "angle", -360.0, 360.0), - ("blur.motionBlur", "distance", 1.0, 2000.0), - ("blur.radialBlur", "amount", 1.0, 100.0), - ("blur.radialBlur", "centerX", 0.0, 1.0), - ("blur.radialBlur", "centerY", 0.0, 1.0), - ("blur.surfaceBlur", "radius", 1.0, 100.0), - ("blur.surfaceBlur", "threshold", 2.0, 255.0), - ("sharpen.unsharpMask", "amount", 1.0, 500.0), - ("sharpen.unsharpMask", "radius", 0.1, 1000.0), - ("sharpen.unsharpMask", "threshold", 0.0, 255.0), - ("sharpen.smartSharpen", "amount", 1.0, 500.0), - ("sharpen.smartSharpen", "radius", 0.1, 64.0), - ("sharpen.smartSharpen", "reduceNoise", 0.0, 100.0), - ("noise.addNoise", "amount", 0.1, 400.0), - ("noise.median", "radius", 1.0, 500.0), - ("noise.dustAndScratches", "radius", 1.0, 500.0), - ("noise.dustAndScratches", "threshold", 0.0, 255.0), - ("pixelate.mosaic", "cellSize", 2.0, 200.0), - ("stylize.emboss", "angle", -180.0, 180.0), - ("stylize.emboss", "height", 1.0, 100.0), - ("stylize.emboss", "amount", 1.0, 500.0), - ("distort.twirl", "angle", -999.0, 999.0), - ("distort.pinch", "amount", -100.0, 100.0), - ("distort.spherize", "amount", -100.0, 100.0), - ("distort.wave", "generators", 1.0, 999.0), - ("distort.wave", "wavelengthMin", 1.0, 998.0), - ("distort.wave", "wavelengthMax", 2.0, 999.0), - ("distort.wave", "amplitudeMin", 1.0, 998.0), - ("distort.wave", "amplitudeMax", 1.0, 999.0), - ("distort.ripple", "amount", -999.0, 999.0), - ("other.highPass", "radius", 0.1, 1000.0), - ("other.minimum", "radius", 0.2, 500.0), - ("other.maximum", "radius", 0.2, 500.0), - ]; - for depth in [8, 16, 32] { - let mut s = session(depth); - for (suffix, key, min, max) in cases { - let id = format!("filter.{suffix}"); - for value in [json!("big"), json!(true), Value::Null, json!([]), json!({}), json!(min - 0.01), json!(max + 0.01)] { - rejected(&mut s, &id, key, value); - } - // Large limits are validated without allocating a large blur kernel. - for value in [min, max] { - validate_params(&id, &json!({key:value})).unwrap(); - } - } - for key in ["horizontal", "vertical"] { - rejected(&mut s, "filter.other.offset", key, json!("4")); - } - } - } - - #[test] - fn core_filter_choices_booleans_seeds_and_shared_fields_are_validated() { - let mut s = session(8); - for (suffix, key, choices) in [ - ("blur.radialBlur", "method", "spin|zoom"), - ("blur.radialBlur", "quality", "draft|good|best"), - ("noise.addNoise", "distribution", "uniform|gaussian"), - ("distort.spherize", "mode", "normal|horizontalOnly|verticalOnly"), - ("distort.wave", "type", "sine|triangle|square"), - ("distort.wave", "undefinedAreas", "wrap|repeat"), - ("distort.ripple", "size", "small|medium|large"), - ("distort.polarCoordinates", "mode", "rectangularToPolar|polarToRectangular"), - ("other.minimum", "preserve", "squareness|roundness"), - ("other.maximum", "preserve", "squareness|roundness"), - ("other.offset", "undefinedAreas", "wrap|repeat|transparent"), - ] { - let id = format!("filter.{suffix}"); - for value in [json!("typo"), json!(false), json!(1), Value::Null] { - rejected(&mut s, &id, key, value); - } - for value in choices.split('|') { - validate_params(&id, &json!({key:value})).unwrap(); - } - } - for value in [json!("true"), json!(0), Value::Null] { - rejected(&mut s, "filter.noise.addNoise", "monochromatic", value); - } - for id in ["filter.noise.addNoise", "filter.distort.wave"] { - for value in [json!(-1), json!(4_294_967_296_u64), json!(0.5), json!("12")] { - rejected(&mut s, id, "seed", value); - } - // A valid u32 seed must not be saturated through an i32 helper. - let result = s.execute(id, json!({"seed":u32::MAX})).unwrap(); - assert_eq!(result["filter"]["seed"], json!(u32::MAX)); - } - for (key, value) in [("layer", json!("2")), ("target", json!("typo")), ("target", json!({"channel":-1})), ("coalesce", json!(false))] { - rejected(&mut s, "filter.blur.gaussianBlur", key, value); - } - } - - #[test] - fn extended_filter_choices_match_their_specs_and_reject_unlisted_values() { - let mut s = session(8); - let specs = crate::filters_ext::specs(); - let ids = [ - "filter.pixelate.mezzotint", - "filter.stylize.diffuse", - "filter.stylize.extrude", - "filter.stylize.tiles", - "filter.stylize.traceContour", - "filter.stylize.wind", - "filter.distort.displace", - "filter.distort.shear", - "filter.distort.zigZag", - "filter.render.lensFlare", - "filter.render.lightingEffects", - "filter.blur.lensBlur", - "filter.other.hsbHsl", - "filter.video.deInterlace", - ]; - for id in ids { - let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; - for &(key, allowed) in choices(id).unwrap() { - // The table repeats the registry's params doc, so the two cannot drift apart. - assert!(doc.contains(&format!("\"{key}\":\"{allowed}\"")), "{id}.{key}"); - for value in [json!("zzbogus"), json!(false), json!(1), Value::Null] { - rejected(&mut s, id, key, value); - } - for value in allowed.split('|') { - validate_params(id, &json!({key:value})).unwrap(); - } - } - } - // Keys `prepare` injects, and undocumented ones, stay accepted as before. - s.execute("filter.stylize.tiles", json!({"count":4,"foreground":[1,0,0,1]})).unwrap(); - s.execute("filter.stylize.wind", json!({"direction":"fromLeft"})).unwrap(); - } - - const STRICT_EXTENDED: [&str; 3] = ["filter.blur.smartBlur", "filter.blur.shapeBlur", "filter.noise.reduceNoise"]; - - #[test] - fn strict_extended_filters_reject_bad_fields_without_editing() { - let numbers = [ - ("filter.blur.smartBlur", "radius", 0.1, 100.0), - ("filter.blur.smartBlur", "threshold", 0.1, 100.0), - ("filter.blur.shapeBlur", "radius", 5.0, 1000.0), - ("filter.noise.reduceNoise", "strength", 0.0, 10.0), - ("filter.noise.reduceNoise", "preserveDetails", 0.0, 100.0), - ("filter.noise.reduceNoise", "reduceColorNoise", 0.0, 100.0), - ("filter.noise.reduceNoise", "sharpenDetails", 0.0, 100.0), - ]; - let specs = crate::filters_ext::specs(); - for depth in [8, 16, 32] { - let mut s = session(depth); - for (id, key, min, max) in numbers { - let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; - assert!(doc.contains(&format!("\"{key}\":{min}..{max}=")), "{id}.{key}"); - for value in [json!("3"), json!(true), Value::Null, json!([]), json!({}), json!(min - 0.01), json!(max + 0.01), json!(1e300)] { - rejected(&mut s, id, key, value); - } - // Validate large endpoints without allocating large blur kernels. - for value in [min, max] { - validate_params(id, &json!({key:value})).unwrap(); - } - } - for id in STRICT_EXTENDED { - for (key, value) in [("radus", json!(3)), ("layer", json!("2")), ("target", json!("typo")), ("coalesce", json!(false))] { - rejected(&mut s, id, key, value); - } - for value in [json!([]), json!(false), json!(3), json!("params")] { - assert!(matches!(validate_params(id, &value), Err(EngineError::BadParams { .. }))); - } - } - for value in [json!("true"), json!(1), Value::Null, json!([]), json!({})] { - rejected(&mut s, "filter.noise.reduceNoise", "removeJpegArtifact", value); - } - } - for value in [false, true] { - validate_params("filter.noise.reduceNoise", &json!({"removeJpegArtifact":value})).unwrap(); - } - for (id, key, allowed) in [ - ("filter.blur.smartBlur", "quality", "high|medium|low"), - ("filter.blur.smartBlur", "mode", "normal|edgeOnly|overlayEdge"), - ("filter.blur.shapeBlur", "shape", "circle|ring|square|diamond|triangle|hexagon|star|heart|cross"), - ] { - let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; - assert!(doc.contains(&format!("\"{key}\":\"{allowed}\"")), "{id}.{key}"); - let mut s = session(8); - for value in [json!("typo"), json!(false), json!(1), Value::Null] { - rejected(&mut s, id, key, value); - } - for value in allowed.split('|') { - validate_params(id, &json!({key:value})).unwrap(); - } - } - } - - #[test] - fn strict_extended_filters_validate_before_floating_selection_commit_and_jobs() { - let mut s = session(16); - s.execute("select.rect", json!({"x":0,"y":0,"width":4,"height":4})).unwrap(); - s.execute("select.float", json!({"dx":8})).unwrap(); - for id in STRICT_EXTENDED { - rejected(&mut s, id, "radus", json!(3)); - let doc = s.active().unwrap().doc.clone(); - let history = s.active().unwrap().history.entries(); - let journal = s.journal.clone(); - let params = json!({"radus":3}); - assert!(matches!(s.start(id, params.clone()), Err(EngineError::BadParams { .. }))); - let spec = crate::commands::find(id).unwrap(); - assert!(matches!((spec.run)(&mut s, ¶ms), Err(EngineError::BadParams { .. }))); - assert!(crate::float_cmds::floating(s.active().unwrap()).is_some()); - assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc)); - assert_eq!(s.active().unwrap().history.entries(), history); - assert_eq!(s.journal, journal); - assert!(s.jobs().is_empty()); - } - } - - #[test] - fn strict_extended_filters_keep_defaults_targets_history_and_last_filter() { - for depth in [8, 16, 32] { - for id in STRICT_EXTENDED { - let mut s = session(depth); - let layer = s.active().unwrap().active_layer.unwrap(); - for params in [Value::Null, json!({}), json!({"layer":layer.0,"target":"pixels","coalesce":"extended"})] { - let before = s.active().unwrap().doc.clone(); - let history = s.active().unwrap().history.past_len(); - s.execute(id, params).unwrap(); - assert_eq!(s.active().unwrap().history.past_len(), history + 1); - let after = s.active().unwrap().doc.clone(); - s.undo(); - assert_eq!(*s.active().unwrap().doc, *before); - s.redo(); - assert_eq!(*s.active().unwrap().doc, *after); - } - s.execute("filter.lastFilter", json!({})).unwrap(); - // The session injects this target when editing an alpha channel. - s.execute("channel.new", json!({"fill":"white"})).unwrap(); - s.execute(id, json!({})).unwrap(); - } - } - } - - #[test] - fn stored_extended_filter_records_still_decode_tolerantly() { - use photocraft_algo::{BlurQuality, BlurShape, FilterParams, SmartBlurMode}; - let smart = - crate::filters::params_for("filter.blur.smartBlur", &json!({"radius":-2,"threshold":900,"quality":"old","mode":false,"extra":true})).unwrap(); - assert!(matches!(smart, FilterParams::SmartBlur { radius: 0.1, threshold: 100.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal })); - let shape = crate::filters::params_for("filter.blur.shapeBlur", &json!({"radius":2000,"shape":"old"})).unwrap(); - assert!(matches!(shape, FilterParams::ShapeBlur { radius: 1000.0, shape: BlurShape::Circle })); - let noise = crate::filters::params_for("filter.noise.reduceNoise", &json!({"strength":-1,"preserveDetails":101,"removeJpegArtifact":"yes"})).unwrap(); - assert!(matches!(noise, FilterParams::ReduceNoise { strength: 0.0, preserve_details: 100.0, remove_jpeg_artifact: false, .. })); - } - - #[test] - fn invalid_filters_leave_floating_selection_and_background_jobs_alone() { - let mut s = session(16); - s.execute("select.rect", json!({"x":0,"y":0,"width":4,"height":4})).unwrap(); - s.execute("select.float", json!({"dx":8})).unwrap(); - let doc = s.active().unwrap().doc.clone(); - let history = s.active().unwrap().history.entries(); - for params in [json!({"radius":"big"}), json!({"radus":3}), json!({"radius":5000}), json!({"radius":-4})] { - assert!(s.start("filter.blur.gaussianBlur", params.clone()).is_err()); - let spec = crate::commands::find("filter.blur.gaussianBlur").unwrap(); - assert!((spec.run)(&mut s, ¶ms).is_err()); - assert!(crate::float_cmds::floating(s.active().unwrap()).is_some()); - assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc)); - assert_eq!(s.active().unwrap().history.entries(), history); - assert!(s.jobs().is_empty()); - } - } - - #[test] - fn valid_filters_keep_defaults_explicit_targets_and_undo_redo() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let layer = s.active().unwrap().active_layer.unwrap(); - let pixels = |s: &Session| s.active().unwrap().doc.layer(layer).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 16, 12)); - let before = pixels(&s); - for params in [Value::Null, json!({})] { - let result = s.execute("filter.blur.gaussianBlur", params).unwrap(); - assert_eq!(result["filter"]["radius"], json!(1.0)); - assert_ne!(pixels(&s), before); - s.undo(); - assert_eq!(pixels(&s), before); - } - s.execute("layer.new.layer", json!({})).unwrap(); - let other = s.active().unwrap().active_layer.unwrap(); - let result = s.execute("filter.blur.gaussianBlur", json!({"radius":3,"layer":layer.0,"target":"pixels","coalesce":"blur"})).unwrap(); - assert_eq!(result["filter"]["radius"], json!(3.0)); - let after = pixels(&s); - assert_ne!(after, before); - assert_eq!(s.active().unwrap().doc.layer(other).unwrap().surface().unwrap().tile_count(), 0); - s.undo(); - assert_eq!(pixels(&s), before); - s.redo(); - assert_eq!(pixels(&s), after); - } - } - - #[test] - fn injected_channel_targets_and_extended_filter_colours_keep_working() { - let mut s = session(8); - s.execute("channel.new", json!({"fill":"white"})).unwrap(); - let pixels = s.active().unwrap().doc.layers.clone(); - s.execute("filter.noise.addNoise", json!({"amount":40,"seed":2})).unwrap(); - assert_eq!(s.active().unwrap().doc.layers, pixels); - assert!(s.active().unwrap().doc.channels[0].surface.read_region(Rect::new(0, 0, 16, 12)).iter().any(|&v| v < 0.99)); - s.execute("channel.target", json!({"channel":"composite"})).unwrap(); - s.tools.foreground = [0.0, 0.0, 1.0, 1.0]; - s.tools.background = [1.0, 0.0, 0.0, 1.0]; - let params = crate::filters_ext::prepare(&s, "filter.render.fibers", &json!({})); - assert_eq!(params["foreground"], json!(s.tools.foreground)); - assert_eq!(params["background"], json!(s.tools.background)); - s.execute("filter.render.fibers", params).unwrap(); - } -} +mod tests; diff --git a/crates/engine/src/filters/validation/tests.rs b/crates/engine/src/filters/validation/tests.rs new file mode 100644 index 0000000000..408a55b365 --- /dev/null +++ b/crates/engine/src/filters/validation/tests.rs @@ -0,0 +1,362 @@ +use std::sync::Arc; + +use photocraft_geom::Rect; +use serde_json::json; + +use super::*; +use crate::Session; + +fn session(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width":16,"height":12,"depth":depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.edit("pattern", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + surface.fill_rect(Rect::new(0, 0, 8, 12), &[0.2, 0.6, 0.8, 1.0]); + Ok(()) + }) + .unwrap(); + s +} + +fn rejected(s: &mut Session, id: &str, key: &str, value: Value) { + let doc = s.active().unwrap().doc.clone(); + let history = s.active().unwrap().history.entries(); + let journal = s.journal.clone(); + let active = s.active().unwrap().active_layer; + let error = s.execute(id, json!({key:value})).unwrap_err(); + assert!(matches!(error, EngineError::BadParams { .. }), "{id}: {error}"); + let error = error.to_string(); + assert!(error.contains(id) && error.contains(key), "{error}"); + assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc), "{id}: {key}"); + assert_eq!(s.active().unwrap().history.entries(), history); + assert_eq!(s.active().unwrap().active_layer, active); + assert_eq!(s.journal, journal); + assert!(s.jobs().is_empty()); +} + +#[test] +fn every_core_filter_rejects_unknown_keys_without_editing() { + for depth in [8, 16, 32] { + let mut s = session(depth); + for spec in crate::filters::specs().into_iter().filter(|s| s.id != "filter.lastFilter") { + rejected(&mut s, spec.id, "radus", json!(3)); + } + } +} + +#[test] +fn core_filter_numbers_reject_wrong_types_and_documented_range_violations() { + // Each numeric field in the core registry, independently of the validation table. + let cases = [ + ("blur.gaussianBlur", "radius", 0.1, 1000.0), + ("blur.boxBlur", "radius", 1.0, 2000.0), + ("blur.motionBlur", "angle", -360.0, 360.0), + ("blur.motionBlur", "distance", 1.0, 2000.0), + ("blur.radialBlur", "amount", 1.0, 100.0), + ("blur.radialBlur", "centerX", 0.0, 1.0), + ("blur.radialBlur", "centerY", 0.0, 1.0), + ("blur.surfaceBlur", "radius", 1.0, 100.0), + ("blur.surfaceBlur", "threshold", 2.0, 255.0), + ("sharpen.unsharpMask", "amount", 1.0, 500.0), + ("sharpen.unsharpMask", "radius", 0.1, 1000.0), + ("sharpen.unsharpMask", "threshold", 0.0, 255.0), + ("sharpen.smartSharpen", "amount", 1.0, 500.0), + ("sharpen.smartSharpen", "radius", 0.1, 64.0), + ("sharpen.smartSharpen", "reduceNoise", 0.0, 100.0), + ("noise.addNoise", "amount", 0.1, 400.0), + ("noise.median", "radius", 1.0, 500.0), + ("noise.dustAndScratches", "radius", 1.0, 500.0), + ("noise.dustAndScratches", "threshold", 0.0, 255.0), + ("pixelate.mosaic", "cellSize", 2.0, 200.0), + ("stylize.emboss", "angle", -180.0, 180.0), + ("stylize.emboss", "height", 1.0, 100.0), + ("stylize.emboss", "amount", 1.0, 500.0), + ("distort.twirl", "angle", -999.0, 999.0), + ("distort.pinch", "amount", -100.0, 100.0), + ("distort.spherize", "amount", -100.0, 100.0), + ("distort.wave", "generators", 1.0, 999.0), + ("distort.wave", "wavelengthMin", 1.0, 998.0), + ("distort.wave", "wavelengthMax", 2.0, 999.0), + ("distort.wave", "amplitudeMin", 1.0, 998.0), + ("distort.wave", "amplitudeMax", 1.0, 999.0), + ("distort.ripple", "amount", -999.0, 999.0), + ("other.highPass", "radius", 0.1, 1000.0), + ("other.minimum", "radius", 0.2, 500.0), + ("other.maximum", "radius", 0.2, 500.0), + ]; + for depth in [8, 16, 32] { + let mut s = session(depth); + for (suffix, key, min, max) in cases { + let id = format!("filter.{suffix}"); + for value in [json!("big"), json!(true), Value::Null, json!([]), json!({}), json!(min - 0.01), json!(max + 0.01)] { + rejected(&mut s, &id, key, value); + } + // Large limits are validated without allocating a large blur kernel. + for value in [min, max] { + validate_params(&id, &json!({key:value})).unwrap(); + } + } + for key in ["horizontal", "vertical"] { + rejected(&mut s, "filter.other.offset", key, json!("4")); + } + } +} + +#[test] +fn core_filter_choices_booleans_seeds_and_shared_fields_are_validated() { + let mut s = session(8); + for (suffix, key, choices) in [ + ("blur.radialBlur", "method", "spin|zoom"), + ("blur.radialBlur", "quality", "draft|good|best"), + ("noise.addNoise", "distribution", "uniform|gaussian"), + ("distort.spherize", "mode", "normal|horizontalOnly|verticalOnly"), + ("distort.wave", "type", "sine|triangle|square"), + ("distort.wave", "undefinedAreas", "wrap|repeat"), + ("distort.ripple", "size", "small|medium|large"), + ("distort.polarCoordinates", "mode", "rectangularToPolar|polarToRectangular"), + ("other.minimum", "preserve", "squareness|roundness"), + ("other.maximum", "preserve", "squareness|roundness"), + ("other.offset", "undefinedAreas", "wrap|repeat|transparent"), + ] { + let id = format!("filter.{suffix}"); + for value in [json!("typo"), json!(false), json!(1), Value::Null] { + rejected(&mut s, &id, key, value); + } + for value in choices.split('|') { + validate_params(&id, &json!({key:value})).unwrap(); + } + } + for value in [json!("true"), json!(0), Value::Null] { + rejected(&mut s, "filter.noise.addNoise", "monochromatic", value); + } + for id in ["filter.noise.addNoise", "filter.distort.wave"] { + for value in [json!(-1), json!(4_294_967_296_u64), json!(0.5), json!("12")] { + rejected(&mut s, id, "seed", value); + } + // A valid u32 seed must not be saturated through an i32 helper. + let result = s.execute(id, json!({"seed":u32::MAX})).unwrap(); + assert_eq!(result["filter"]["seed"], json!(u32::MAX)); + } + for (key, value) in [("layer", json!("2")), ("target", json!("typo")), ("target", json!({"channel":-1})), ("coalesce", json!(false))] { + rejected(&mut s, "filter.blur.gaussianBlur", key, value); + } +} + +#[test] +fn extended_filter_choices_match_their_specs_and_reject_unlisted_values() { + let mut s = session(8); + let specs = crate::filters_ext::specs(); + let ids = [ + "filter.pixelate.mezzotint", + "filter.stylize.diffuse", + "filter.stylize.extrude", + "filter.stylize.tiles", + "filter.stylize.traceContour", + "filter.stylize.wind", + "filter.distort.displace", + "filter.distort.shear", + "filter.distort.zigZag", + "filter.render.lensFlare", + "filter.render.lightingEffects", + "filter.blur.lensBlur", + "filter.other.hsbHsl", + "filter.video.deInterlace", + ]; + for id in ids { + let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; + for &(key, allowed) in choices(id).unwrap() { + // The table repeats the registry's params doc, so the two cannot drift apart. + assert!(doc.contains(&format!("\"{key}\":\"{allowed}\"")), "{id}.{key}"); + for value in [json!("zzbogus"), json!(false), json!(1), Value::Null] { + rejected(&mut s, id, key, value); + } + for value in allowed.split('|') { + validate_params(id, &json!({key:value})).unwrap(); + } + } + } + // Keys `prepare` injects, and undocumented ones, stay accepted as before. + s.execute("filter.stylize.tiles", json!({"count":4,"foreground":[1,0,0,1]})).unwrap(); + s.execute("filter.stylize.wind", json!({"direction":"fromLeft"})).unwrap(); +} + +const STRICT_EXTENDED: [&str; 3] = ["filter.blur.smartBlur", "filter.blur.shapeBlur", "filter.noise.reduceNoise"]; + +#[test] +fn strict_extended_filters_reject_bad_fields_without_editing() { + let numbers = [ + ("filter.blur.smartBlur", "radius", 0.1, 100.0), + ("filter.blur.smartBlur", "threshold", 0.1, 100.0), + ("filter.blur.shapeBlur", "radius", 5.0, 1000.0), + ("filter.noise.reduceNoise", "strength", 0.0, 10.0), + ("filter.noise.reduceNoise", "preserveDetails", 0.0, 100.0), + ("filter.noise.reduceNoise", "reduceColorNoise", 0.0, 100.0), + ("filter.noise.reduceNoise", "sharpenDetails", 0.0, 100.0), + ]; + let specs = crate::filters_ext::specs(); + for depth in [8, 16, 32] { + let mut s = session(depth); + for (id, key, min, max) in numbers { + let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; + assert!(doc.contains(&format!("\"{key}\":{min}..{max}=")), "{id}.{key}"); + for value in [json!("3"), json!(true), Value::Null, json!([]), json!({}), json!(min - 0.01), json!(max + 0.01), json!(1e300)] { + rejected(&mut s, id, key, value); + } + // Validate large endpoints without allocating large blur kernels. + for value in [min, max] { + validate_params(id, &json!({key:value})).unwrap(); + } + } + for id in STRICT_EXTENDED { + for (key, value) in [("radus", json!(3)), ("layer", json!("2")), ("target", json!("typo")), ("coalesce", json!(false))] { + rejected(&mut s, id, key, value); + } + for value in [json!([]), json!(false), json!(3), json!("params")] { + assert!(matches!(validate_params(id, &value), Err(EngineError::BadParams { .. }))); + } + } + for value in [json!("true"), json!(1), Value::Null, json!([]), json!({})] { + rejected(&mut s, "filter.noise.reduceNoise", "removeJpegArtifact", value); + } + } + for value in [false, true] { + validate_params("filter.noise.reduceNoise", &json!({"removeJpegArtifact":value})).unwrap(); + } + for (id, key, allowed) in [ + ("filter.blur.smartBlur", "quality", "high|medium|low"), + ("filter.blur.smartBlur", "mode", "normal|edgeOnly|overlayEdge"), + ("filter.blur.shapeBlur", "shape", "circle|ring|square|diamond|triangle|hexagon|star|heart|cross"), + ] { + let doc = specs.iter().find(|spec| spec.id == id).unwrap().params; + assert!(doc.contains(&format!("\"{key}\":\"{allowed}\"")), "{id}.{key}"); + let mut s = session(8); + for value in [json!("typo"), json!(false), json!(1), Value::Null] { + rejected(&mut s, id, key, value); + } + for value in allowed.split('|') { + validate_params(id, &json!({key:value})).unwrap(); + } + } +} + +#[test] +fn strict_extended_filters_validate_before_floating_selection_commit_and_jobs() { + let mut s = session(16); + s.execute("select.rect", json!({"x":0,"y":0,"width":4,"height":4})).unwrap(); + s.execute("select.float", json!({"dx":8})).unwrap(); + for id in STRICT_EXTENDED { + rejected(&mut s, id, "radus", json!(3)); + let doc = s.active().unwrap().doc.clone(); + let history = s.active().unwrap().history.entries(); + let journal = s.journal.clone(); + let params = json!({"radus":3}); + assert!(matches!(s.start(id, params.clone()), Err(EngineError::BadParams { .. }))); + let spec = crate::commands::find(id).unwrap(); + assert!(matches!((spec.run)(&mut s, ¶ms), Err(EngineError::BadParams { .. }))); + assert!(crate::float_cmds::floating(s.active().unwrap()).is_some()); + assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc)); + assert_eq!(s.active().unwrap().history.entries(), history); + assert_eq!(s.journal, journal); + assert!(s.jobs().is_empty()); + } +} + +#[test] +fn strict_extended_filters_keep_defaults_targets_history_and_last_filter() { + for depth in [8, 16, 32] { + for id in STRICT_EXTENDED { + let mut s = session(depth); + let layer = s.active().unwrap().active_layer.unwrap(); + for params in [Value::Null, json!({}), json!({"layer":layer.0,"target":"pixels","coalesce":"extended"})] { + let before = s.active().unwrap().doc.clone(); + let history = s.active().unwrap().history.past_len(); + s.execute(id, params).unwrap(); + assert_eq!(s.active().unwrap().history.past_len(), history + 1); + let after = s.active().unwrap().doc.clone(); + s.undo(); + assert_eq!(*s.active().unwrap().doc, *before); + s.redo(); + assert_eq!(*s.active().unwrap().doc, *after); + } + s.execute("filter.lastFilter", json!({})).unwrap(); + // The session injects this target when editing an alpha channel. + s.execute("channel.new", json!({"fill":"white"})).unwrap(); + s.execute(id, json!({})).unwrap(); + } + } +} + +#[test] +fn stored_extended_filter_records_still_decode_tolerantly() { + use photocraft_algo::{BlurQuality, BlurShape, FilterParams, SmartBlurMode}; + let smart = crate::filters::params_for("filter.blur.smartBlur", &json!({"radius":-2,"threshold":900,"quality":"old","mode":false,"extra":true})).unwrap(); + assert!(matches!(smart, FilterParams::SmartBlur { radius: 0.1, threshold: 100.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal })); + let shape = crate::filters::params_for("filter.blur.shapeBlur", &json!({"radius":2000,"shape":"old"})).unwrap(); + assert!(matches!(shape, FilterParams::ShapeBlur { radius: 1000.0, shape: BlurShape::Circle })); + let noise = crate::filters::params_for("filter.noise.reduceNoise", &json!({"strength":-1,"preserveDetails":101,"removeJpegArtifact":"yes"})).unwrap(); + assert!(matches!(noise, FilterParams::ReduceNoise { strength: 0.0, preserve_details: 100.0, remove_jpeg_artifact: false, .. })); +} + +#[test] +fn invalid_filters_leave_floating_selection_and_background_jobs_alone() { + let mut s = session(16); + s.execute("select.rect", json!({"x":0,"y":0,"width":4,"height":4})).unwrap(); + s.execute("select.float", json!({"dx":8})).unwrap(); + let doc = s.active().unwrap().doc.clone(); + let history = s.active().unwrap().history.entries(); + for params in [json!({"radius":"big"}), json!({"radus":3}), json!({"radius":5000}), json!({"radius":-4})] { + assert!(s.start("filter.blur.gaussianBlur", params.clone()).is_err()); + let spec = crate::commands::find("filter.blur.gaussianBlur").unwrap(); + assert!((spec.run)(&mut s, ¶ms).is_err()); + assert!(crate::float_cmds::floating(s.active().unwrap()).is_some()); + assert!(Arc::ptr_eq(&s.active().unwrap().doc, &doc)); + assert_eq!(s.active().unwrap().history.entries(), history); + assert!(s.jobs().is_empty()); + } +} + +#[test] +fn valid_filters_keep_defaults_explicit_targets_and_undo_redo() { + for depth in [8, 16, 32] { + let mut s = session(depth); + let layer = s.active().unwrap().active_layer.unwrap(); + let pixels = |s: &Session| s.active().unwrap().doc.layer(layer).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 16, 12)); + let before = pixels(&s); + for params in [Value::Null, json!({})] { + let result = s.execute("filter.blur.gaussianBlur", params).unwrap(); + assert_eq!(result["filter"]["radius"], json!(1.0)); + assert_ne!(pixels(&s), before); + s.undo(); + assert_eq!(pixels(&s), before); + } + s.execute("layer.new.layer", json!({})).unwrap(); + let other = s.active().unwrap().active_layer.unwrap(); + let result = s.execute("filter.blur.gaussianBlur", json!({"radius":3,"layer":layer.0,"target":"pixels","coalesce":"blur"})).unwrap(); + assert_eq!(result["filter"]["radius"], json!(3.0)); + let after = pixels(&s); + assert_ne!(after, before); + assert_eq!(s.active().unwrap().doc.layer(other).unwrap().surface().unwrap().tile_count(), 0); + s.undo(); + assert_eq!(pixels(&s), before); + s.redo(); + assert_eq!(pixels(&s), after); + } +} + +#[test] +fn injected_channel_targets_and_extended_filter_colours_keep_working() { + let mut s = session(8); + s.execute("channel.new", json!({"fill":"white"})).unwrap(); + let pixels = s.active().unwrap().doc.layers.clone(); + s.execute("filter.noise.addNoise", json!({"amount":40,"seed":2})).unwrap(); + assert_eq!(s.active().unwrap().doc.layers, pixels); + assert!(s.active().unwrap().doc.channels[0].surface.read_region(Rect::new(0, 0, 16, 12)).iter().any(|&v| v < 0.99)); + s.execute("channel.target", json!({"channel":"composite"})).unwrap(); + s.tools.foreground = [0.0, 0.0, 1.0, 1.0]; + s.tools.background = [1.0, 0.0, 0.0, 1.0]; + let params = crate::filters_ext::prepare(&s, "filter.render.fibers", &json!({})); + assert_eq!(params["foreground"], json!(s.tools.foreground)); + assert_eq!(params["background"], json!(s.tools.background)); + s.execute("filter.render.fibers", params).unwrap(); +} diff --git a/crates/engine/src/filters_ext.rs b/crates/engine/src/filters_ext.rs index dae5e9b98b..1afc11220e 100644 --- a/crates/engine/src/filters_ext.rs +++ b/crates/engine/src/filters_ext.rs @@ -66,341 +66,414 @@ fn list(p: &Value, k: &str) -> Option> { const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0]; const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0]; +/// The value `options` pairs with param `k` (default name `d`), or `other` for a name it doesn't list. +fn choice(p: &Value, k: &str, d: &str, options: &[(&str, T)], other: T) -> T { + let name = s(p, k, d); + options.iter().find(|(n, _)| *n == name).map_or(other, |&(_, v)| v) +} + +const MEZZOTINT_TYPES: &[(&str, MezzotintType)] = &[ + ("fineDots", MezzotintType::FineDots), + ("mediumDots", MezzotintType::MediumDots), + ("grainyDots", MezzotintType::GrainyDots), + ("coarseDots", MezzotintType::CoarseDots), + ("shortLines", MezzotintType::ShortLines), + ("mediumLines", MezzotintType::MediumLines), + ("longLines", MezzotintType::LongLines), + ("shortStrokes", MezzotintType::ShortStrokes), + ("mediumStrokes", MezzotintType::MediumStrokes), + ("longStrokes", MezzotintType::LongStrokes), +]; +const DIFFUSE_MODES: &[(&str, DiffuseMode)] = &[ + ("normal", DiffuseMode::Normal), + ("darkenOnly", DiffuseMode::DarkenOnly), + ("lightenOnly", DiffuseMode::LightenOnly), + ("anisotropic", DiffuseMode::Anisotropic), +]; +const TILE_FILLS: &[(&str, TileFill)] = + &[("background", TileFill::Background), ("foreground", TileFill::Foreground), ("inverse", TileFill::Inverse), ("unaltered", TileFill::Unaltered)]; +const WIND_METHODS: &[(&str, WindMethod)] = &[("wind", WindMethod::Wind), ("blast", WindMethod::Blast), ("stagger", WindMethod::Stagger)]; +const ZIGZAG_STYLES: &[(&str, ZigZagStyle)] = + &[("pondRipples", ZigZagStyle::PondRipples), ("outFromCenter", ZigZagStyle::OutFromCenter), ("aroundCenter", ZigZagStyle::AroundCenter)]; +const LENS_TYPES: &[(&str, LensType)] = + &[("zoom", LensType::Zoom), ("prime35", LensType::Prime35), ("prime105", LensType::Prime105), ("moviePrime", LensType::MoviePrime)]; +const LIGHT_KINDS: &[(&str, LightKind)] = &[("spot", LightKind::Spot), ("point", LightKind::Point), ("infinite", LightKind::Infinite)]; +const TEXTURE_CHANNELS: &[(&str, TextureChannel)] = &[ + ("none", TextureChannel::None), + ("red", TextureChannel::Red), + ("green", TextureChannel::Green), + ("blue", TextureChannel::Blue), + ("alpha", TextureChannel::Alpha), + ("luminance", TextureChannel::Luminance), +]; +const BLUR_QUALITIES: &[(&str, BlurQuality)] = &[("high", BlurQuality::High), ("low", BlurQuality::Low), ("medium", BlurQuality::Medium)]; +const SMART_BLUR_MODES: &[(&str, SmartBlurMode)] = + &[("normal", SmartBlurMode::Normal), ("edgeOnly", SmartBlurMode::EdgeOnly), ("overlayEdge", SmartBlurMode::OverlayEdge)]; +const IRIS_BLADES: &[(&str, u32)] = &[("triangle", 3), ("square", 4), ("pentagon", 5), ("hexagon", 6), ("heptagon", 7), ("octagon", 8)]; +const DEPTH_SOURCES: &[(&str, DepthSource)] = + &[("none", DepthSource::None), ("transparency", DepthSource::Transparency), ("layerMask", DepthSource::LayerMask), ("channel", DepthSource::Channel)]; +const BLUR_SHAPES: &[(&str, BlurShape)] = &[ + ("circle", BlurShape::Circle), + ("ring", BlurShape::Ring), + ("square", BlurShape::Square), + ("diamond", BlurShape::Diamond), + ("triangle", BlurShape::Triangle), + ("hexagon", BlurShape::Hexagon), + ("star", BlurShape::Star), + ("heart", BlurShape::Heart), + ("cross", BlurShape::Cross), +]; +const HSB_MODELS: &[(&str, HsbModel)] = &[("rgb", HsbModel::Rgb), ("hsb", HsbModel::Hsb), ("hsl", HsbModel::Hsl)]; + +type Build = fn(&Value) -> FilterParams; + +const PARAMS: &[(&str, Build)] = &[ + // Pixelate + ("filter.pixelate.colorHalftone", |p| FilterParams::ColorHalftone { + max_radius: f(p, "maxRadius", 8.0).clamp(4.0, 127.0), + angles: [f(p, "channel1", 108.0), f(p, "channel2", 162.0), f(p, "channel3", 90.0), f(p, "channel4", 45.0)], + }), + ("filter.pixelate.crystallize", |p| FilterParams::Crystallize { cell_size: f(p, "cellSize", 10.0).clamp(3.0, 300.0), seed: seed(p) }), + ("filter.pixelate.facet", |_| FilterParams::Facet), + ("filter.pixelate.fragment", |_| FilterParams::Fragment), + ("filter.pixelate.mezzotint", |p| FilterParams::Mezzotint { kind: choice(p, "type", "fineDots", MEZZOTINT_TYPES, MezzotintType::FineDots), seed: seed(p) }), + ("filter.pixelate.pointillize", |p| FilterParams::Pointillize { + cell_size: f(p, "cellSize", 5.0).clamp(3.0, 300.0), + seed: seed(p), + background: colour(p, "background", WHITE), + }), + // Stylize + ("filter.stylize.diffuse", |p| FilterParams::Diffuse { mode: choice(p, "mode", "normal", DIFFUSE_MODES, DiffuseMode::Normal), seed: seed(p) }), + ("filter.stylize.extrude", extrude), + ("filter.stylize.oilPaint", oil_paint), + ("filter.stylize.tiles", tiles), + ("filter.stylize.traceContour", |p| FilterParams::TraceContour { level: f(p, "level", 128.0).clamp(0.0, 255.0), upper: s(p, "edge", "lower") == "upper" }), + ("filter.stylize.wind", |p| FilterParams::Wind { + method: choice(p, "method", "wind", WIND_METHODS, WindMethod::Wind), + from_right: s(p, "direction", "fromRight") != "fromLeft", + seed: seed(p), + }), + // Distort + ("filter.distort.displace", displace), + ("filter.distort.shear", shear), + ("filter.distort.zigZag", |p| FilterParams::ZigZag { + amount: f(p, "amount", 10.0).clamp(-100.0, 100.0), + ridges: f(p, "ridges", 5.0).clamp(0.0, 20.0), + style: choice(p, "style", "pondRipples", ZIGZAG_STYLES, ZigZagStyle::PondRipples), + }), + // Render + ("filter.render.clouds", |p| FilterParams::Clouds { + seed: seed(p), + difference: false, + foreground: colour(p, "foreground", BLACK), + background: colour(p, "background", WHITE), + }), + ("filter.render.differenceClouds", |p| FilterParams::Clouds { + seed: seed(p), + difference: true, + foreground: colour(p, "foreground", BLACK), + background: colour(p, "background", WHITE), + }), + ("filter.render.fibers", |p| FilterParams::Fibers { + variance: f(p, "variance", 16.0).clamp(1.0, 64.0), + strength: f(p, "strength", 4.0).clamp(1.0, 64.0), + seed: seed(p), + foreground: colour(p, "foreground", BLACK), + background: colour(p, "background", WHITE), + }), + ("filter.render.lensFlare", lens_flare), + ("filter.render.relight", |p| FilterParams::Relight { + angle: f(p, "angle", 45.0).clamp(-180.0, 180.0), + elevation: f(p, "elevation", 40.0).clamp(0.0, 90.0), + intensity: f(p, "intensity", 40.0).clamp(0.0, 100.0), + ambient: f(p, "ambient", 55.0).clamp(0.0, 100.0), + warmth: f(p, "warmth", 0.0).clamp(-100.0, 100.0), + softness: f(p, "softness", 25.0).clamp(1.0, 100.0), + }), + ("filter.render.lightingEffects", lighting_effects), + // Noise + ("filter.noise.reduceNoise", |p| FilterParams::ReduceNoise { + strength: f(p, "strength", 6.0).clamp(0.0, 10.0), + preserve_details: f(p, "preserveDetails", 60.0).clamp(0.0, 100.0), + reduce_color_noise: f(p, "reduceColorNoise", 45.0).clamp(0.0, 100.0), + sharpen_details: f(p, "sharpenDetails", 25.0).clamp(0.0, 100.0), + remove_jpeg_artifact: b(p, "removeJpegArtifact", false), + }), + // Blur + ("filter.blur.smartBlur", smart_blur), + ("filter.blur.lensBlur", lens_blur), + ("filter.blur.shapeBlur", |p| FilterParams::ShapeBlur { + radius: f(p, "radius", 10.0).clamp(5.0, 1000.0), + shape: choice(p, "shape", "circle", BLUR_SHAPES, BlurShape::Circle), + }), + // Blur Gallery + ("filter.blurGallery.tiltShift", tilt_shift), + ("filter.blurGallery.irisBlur", iris_blur), + ("filter.blurGallery.fieldBlur", |p| FilterParams::FieldBlur { + pins: list::(p, "pins").unwrap_or_else(|| vec![FieldPin { x: f(p, "centerX", 0.5), y: f(p, "centerY", 0.5), blur: gallery_blur(p) }]), + }), + ("filter.blurGallery.spinBlur", spin_blur), + ("filter.blurGallery.pathBlur", path_blur), + // Other + ("filter.other.custom", custom), + ("filter.other.hsbHsl", |p| FilterParams::HsbHsl { + input: choice(p, "inputMode", "rgb", HSB_MODELS, HsbModel::Rgb), + output: choice(p, "rowOrder", "hsb", HSB_MODELS, HsbModel::Rgb), + }), + (crate::color_to_alpha_cmds::ID, crate::color_to_alpha_cmds::params), + // Video + ("filter.video.deInterlace", |p| FilterParams::DeInterlace { + eliminate_even: s(p, "eliminate", "oddFields") == "evenFields", + interpolate: s(p, "createBy", "interpolation") != "duplication", + }), + ("filter.video.ntscColors", |_| FilterParams::NtscColors), +]; + /// Algorithm parameters for the ids this module owns (Photoshop dialog units). pub fn params_for(id: &str, p: &Value) -> Option { - let blur = |d: f32| f(p, "blur", d).clamp(0.0, 500.0); - Some(match id { - // ---- Pixelate ---- - "filter.pixelate.colorHalftone" => FilterParams::ColorHalftone { - max_radius: f(p, "maxRadius", 8.0).clamp(4.0, 127.0), - angles: [f(p, "channel1", 108.0), f(p, "channel2", 162.0), f(p, "channel3", 90.0), f(p, "channel4", 45.0)], - }, - "filter.pixelate.crystallize" => FilterParams::Crystallize { cell_size: f(p, "cellSize", 10.0).clamp(3.0, 300.0), seed: seed(p) }, - "filter.pixelate.facet" => FilterParams::Facet, - "filter.pixelate.fragment" => FilterParams::Fragment, - "filter.pixelate.mezzotint" => FilterParams::Mezzotint { - kind: match s(p, "type", "fineDots") { - "mediumDots" => MezzotintType::MediumDots, - "grainyDots" => MezzotintType::GrainyDots, - "coarseDots" => MezzotintType::CoarseDots, - "shortLines" => MezzotintType::ShortLines, - "mediumLines" => MezzotintType::MediumLines, - "longLines" => MezzotintType::LongLines, - "shortStrokes" => MezzotintType::ShortStrokes, - "mediumStrokes" => MezzotintType::MediumStrokes, - "longStrokes" => MezzotintType::LongStrokes, - _ => MezzotintType::FineDots, - }, - seed: seed(p), - }, - "filter.pixelate.pointillize" => { - FilterParams::Pointillize { cell_size: f(p, "cellSize", 5.0).clamp(3.0, 300.0), seed: seed(p), background: colour(p, "background", WHITE) } - } - // ---- Stylize ---- - "filter.stylize.diffuse" => FilterParams::Diffuse { - mode: match s(p, "mode", "normal") { - "darkenOnly" => DiffuseMode::DarkenOnly, - "lightenOnly" => DiffuseMode::LightenOnly, - "anisotropic" => DiffuseMode::Anisotropic, - _ => DiffuseMode::Normal, - }, - seed: seed(p), - }, - "filter.stylize.extrude" => FilterParams::Extrude { - kind: if s(p, "type", "blocks") == "pyramids" { ExtrudeType::Pyramids } else { ExtrudeType::Blocks }, - size: f(p, "size", 30.0).clamp(2.0, 255.0), - depth: f(p, "depth", 30.0).clamp(1.0, 255.0), - level_based: s(p, "depthMode", "random") == "levelBased", - solid_front: b(p, "solidFrontFaces", false), - mask_incomplete: b(p, "maskIncompleteBlocks", false), - seed: seed(p), - }, - "filter.stylize.oilPaint" => FilterParams::OilPaint { - stylization: f(p, "stylization", 4.0).clamp(0.1, 10.0), - cleanliness: f(p, "cleanliness", 5.0).clamp(0.0, 10.0), - scale: f(p, "scale", 1.0).clamp(0.1, 10.0), - bristle_detail: f(p, "bristleDetail", 5.0).clamp(0.0, 10.0), - lighting: b(p, "lighting", true), - angle: f(p, "angle", -60.0), - shine: f(p, "shine", 1.0).clamp(0.0, 10.0), - }, - "filter.stylize.tiles" => FilterParams::Tiles { - count: i(p, "count", 10).clamp(1, 99) as u32, - max_offset: f(p, "maxOffset", 10.0).clamp(1.0, 99.0), - fill: match s(p, "fill", "background") { - "foreground" => TileFill::Foreground, - "inverse" => TileFill::Inverse, - "unaltered" => TileFill::Unaltered, - _ => TileFill::Background, - }, - foreground: colour(p, "foreground", BLACK), - background: colour(p, "background", WHITE), - seed: seed(p), - }, - "filter.stylize.traceContour" => FilterParams::TraceContour { level: f(p, "level", 128.0).clamp(0.0, 255.0), upper: s(p, "edge", "lower") == "upper" }, - "filter.stylize.wind" => FilterParams::Wind { - method: match s(p, "method", "wind") { - "blast" => WindMethod::Blast, - "stagger" => WindMethod::Stagger, - _ => WindMethod::Wind, - }, - from_right: s(p, "direction", "fromRight") != "fromLeft", - seed: seed(p), - }, - // ---- Distort ---- - "filter.distort.displace" => FilterParams::Displace { - horizontal: f(p, "horizontal", 10.0).clamp(-999.0, 999.0), - vertical: f(p, "vertical", 10.0).clamp(-999.0, 999.0), - stretch: s(p, "fit", "stretch") != "tile", - undefined: undefined(p, "repeat"), - // A Displace imported from PSD carries its map; `mapPath` / `mapDocument` resolve later. - map: photocraft_io::smart_map::displace_embedded_map(p), - }, - "filter.distort.shear" => FilterParams::Shear { - points: list::<[f32; 2]>(p, "points").unwrap_or_else(|| { - let a = f(p, "amount", 0.0).clamp(-100.0, 100.0) / 100.0; - vec![[0.0, 0.0], [0.5, a], [1.0, 0.0]] - }), - undefined: undefined(p, "wrap"), - }, - "filter.distort.zigZag" => FilterParams::ZigZag { - amount: f(p, "amount", 10.0).clamp(-100.0, 100.0), - ridges: f(p, "ridges", 5.0).clamp(0.0, 20.0), - style: match s(p, "style", "pondRipples") { - "outFromCenter" => ZigZagStyle::OutFromCenter, - "aroundCenter" => ZigZagStyle::AroundCenter, - _ => ZigZagStyle::PondRipples, - }, - }, - // ---- Render ---- - "filter.render.fibers" => FilterParams::Fibers { - variance: f(p, "variance", 16.0).clamp(1.0, 64.0), - strength: f(p, "strength", 4.0).clamp(1.0, 64.0), - seed: seed(p), - foreground: colour(p, "foreground", BLACK), - background: colour(p, "background", WHITE), - }, - "filter.render.lensFlare" => FilterParams::LensFlare { - brightness: f(p, "brightness", 100.0).clamp(10.0, 300.0), - center_x: f(p, "centerX", 0.5).clamp(-0.5, 1.5), - center_y: f(p, "centerY", 0.5).clamp(-0.5, 1.5), - lens: match s(p, "lens", "zoom") { - "prime35" => LensType::Prime35, - "prime105" => LensType::Prime105, - "moviePrime" => LensType::MoviePrime, - _ => LensType::Zoom, - }, - }, - "filter.render.relight" => FilterParams::Relight { - angle: f(p, "angle", 45.0).clamp(-180.0, 180.0), - elevation: f(p, "elevation", 40.0).clamp(0.0, 90.0), - intensity: f(p, "intensity", 40.0).clamp(0.0, 100.0), - ambient: f(p, "ambient", 55.0).clamp(0.0, 100.0), - warmth: f(p, "warmth", 0.0).clamp(-100.0, 100.0), - softness: f(p, "softness", 25.0).clamp(1.0, 100.0), - }, - "filter.render.lightingEffects" => FilterParams::LightingEffects { - lights: list::(p, "lights").unwrap_or_else(|| { - vec![Light { - kind: match s(p, "lightType", "spot") { - "point" => LightKind::Point, - "infinite" => LightKind::Infinite, - _ => LightKind::Spot, - }, - x: f(p, "lightX", 0.25), - y: f(p, "lightY", 0.2), - z: f(p, "lightZ", 0.6).clamp(0.0, 10.0), - target_x: f(p, "targetX", 0.5), - target_y: f(p, "targetY", 0.55), - angle: f(p, "angle", 135.0), - elevation: f(p, "elevation", 45.0).clamp(0.0, 90.0), - color: { - let c = colour(p, "color", WHITE); - [c[0], c[1], c[2]] - }, - intensity: f(p, "intensity", 75.0).clamp(-100.0, 100.0), - cone: f(p, "cone", 45.0).clamp(1.0, 89.0), - hotspot: f(p, "hotspot", 50.0).clamp(0.0, 100.0), - radius: f(p, "radius", 0.8).clamp(0.01, 10.0), - }] - }), - gloss: f(p, "gloss", 0.0).clamp(-100.0, 100.0), - metallic: f(p, "metallic", 0.0).clamp(-100.0, 100.0), - exposure: f(p, "exposure", 0.0).clamp(-100.0, 100.0), - ambience: f(p, "ambience", 8.0).clamp(-100.0, 100.0), - texture: match s(p, "texture", "none") { - "red" => TextureChannel::Red, - "green" => TextureChannel::Green, - "blue" => TextureChannel::Blue, - "alpha" => TextureChannel::Alpha, - "luminance" => TextureChannel::Luminance, - _ => TextureChannel::None, - }, - height: f(p, "height", 50.0).clamp(0.0, 100.0), - white_is_high: b(p, "whiteIsHigh", true), - }, - // ---- Noise ---- - "filter.noise.reduceNoise" => FilterParams::ReduceNoise { - strength: f(p, "strength", 6.0).clamp(0.0, 10.0), - preserve_details: f(p, "preserveDetails", 60.0).clamp(0.0, 100.0), - reduce_color_noise: f(p, "reduceColorNoise", 45.0).clamp(0.0, 100.0), - sharpen_details: f(p, "sharpenDetails", 25.0).clamp(0.0, 100.0), - remove_jpeg_artifact: b(p, "removeJpegArtifact", false), - }, - // ---- Blur ---- - "filter.blur.smartBlur" => FilterParams::SmartBlur { - radius: f(p, "radius", 3.0).clamp(0.1, 100.0), - threshold: f(p, "threshold", 25.0).clamp(0.1, 100.0), - quality: match s(p, "quality", "high") { - "low" => BlurQuality::Low, - "medium" => BlurQuality::Medium, - _ => BlurQuality::High, - }, - mode: match s(p, "mode", "normal") { - "edgeOnly" => SmartBlurMode::EdgeOnly, - "overlayEdge" => SmartBlurMode::OverlayEdge, - _ => SmartBlurMode::Normal, - }, - }, - "filter.blur.lensBlur" => FilterParams::LensBlur { - radius: f(p, "radius", 15.0).clamp(0.0, 100.0), - blades: match s(p, "shape", "hexagon") { - "triangle" => 3, - "square" => 4, - "pentagon" => 5, - "heptagon" => 7, - "octagon" => 8, - _ => 6, - }, - curvature: f(p, "bladeCurvature", 0.0).clamp(0.0, 100.0), - rotation: f(p, "rotation", 0.0), - depth: match s(p, "depthMap", "none") { - "transparency" => DepthSource::Transparency, - "layerMask" => DepthSource::LayerMask, - _ => DepthSource::None, - }, - focal_distance: f(p, "focalDistance", 0.0).clamp(0.0, 255.0), - invert_depth: b(p, "invert", false), - brightness: f(p, "brightness", 0.0).clamp(0.0, 100.0), - threshold: f(p, "threshold", 255.0).clamp(0.0, 255.0), - noise: f(p, "noise", 0.0).clamp(0.0, 100.0), - distribution: if s(p, "distribution", "uniform") == "gaussian" { Distribution::Gaussian } else { Distribution::Uniform }, - monochromatic: b(p, "monochromatic", false), - seed: seed(p), - depth_map: None, - }, - "filter.blur.shapeBlur" => FilterParams::ShapeBlur { - radius: f(p, "radius", 10.0).clamp(5.0, 1000.0), - shape: match s(p, "shape", "circle") { - "ring" => BlurShape::Ring, - "square" => BlurShape::Square, - "diamond" => BlurShape::Diamond, - "triangle" => BlurShape::Triangle, - "hexagon" => BlurShape::Hexagon, - "star" => BlurShape::Star, - "heart" => BlurShape::Heart, - "cross" => BlurShape::Cross, - _ => BlurShape::Circle, - }, - }, - // ---- Blur Gallery ---- - "filter.blurGallery.tiltShift" => FilterParams::TiltShift { - blur: blur(15.0), - center_x: f(p, "centerX", 0.5), - center_y: f(p, "centerY", 0.5), - angle: f(p, "angle", 0.0), - focus: f(p, "focus", 0.1).clamp(0.0, 2.0), - transition: f(p, "transition", 0.15).clamp(0.001, 2.0), - }, - "filter.blurGallery.irisBlur" => FilterParams::IrisBlur { - pins: list::(p, "pins").unwrap_or_else(|| { - vec![IrisPin { + match PARAMS.iter().find(|(k, _)| *k == id) { + Some((_, build)) => Some(build(p)), + None => crate::gallery_cmds::params_for(id, p), + } +} + +/// The blur amount the Blur Gallery filters share. +fn gallery_blur(p: &Value) -> f32 { + f(p, "blur", 15.0).clamp(0.0, 500.0) +} + +/// Filter › Stylize › Extrude parameters. +fn extrude(p: &Value) -> FilterParams { + FilterParams::Extrude { + kind: if s(p, "type", "blocks") == "pyramids" { ExtrudeType::Pyramids } else { ExtrudeType::Blocks }, + size: f(p, "size", 30.0).clamp(2.0, 255.0), + depth: f(p, "depth", 30.0).clamp(1.0, 255.0), + level_based: s(p, "depthMode", "random") == "levelBased", + solid_front: b(p, "solidFrontFaces", false), + mask_incomplete: b(p, "maskIncompleteBlocks", false), + seed: seed(p), + } +} + +/// Filter › Stylize › Oil Paint parameters. +fn oil_paint(p: &Value) -> FilterParams { + FilterParams::OilPaint { + stylization: f(p, "stylization", 4.0).clamp(0.1, 10.0), + cleanliness: f(p, "cleanliness", 5.0).clamp(0.0, 10.0), + scale: f(p, "scale", 1.0).clamp(0.1, 10.0), + bristle_detail: f(p, "bristleDetail", 5.0).clamp(0.0, 10.0), + lighting: b(p, "lighting", true), + angle: f(p, "angle", -60.0), + shine: f(p, "shine", 1.0).clamp(0.0, 10.0), + } +} + +/// Filter › Stylize › Tiles parameters. +fn tiles(p: &Value) -> FilterParams { + FilterParams::Tiles { + count: i(p, "count", 10).clamp(1, 99) as u32, + max_offset: f(p, "maxOffset", 10.0).clamp(1.0, 99.0), + fill: choice(p, "fill", "background", TILE_FILLS, TileFill::Background), + foreground: colour(p, "foreground", BLACK), + background: colour(p, "background", WHITE), + seed: seed(p), + } +} + +/// Filter › Distort › Displace parameters. +fn displace(p: &Value) -> FilterParams { + FilterParams::Displace { + horizontal: f(p, "horizontal", 10.0).clamp(-999.0, 999.0), + vertical: f(p, "vertical", 10.0).clamp(-999.0, 999.0), + stretch: s(p, "fit", "stretch") != "tile", + undefined: undefined(p, "repeat"), + // PSD imports carry the map, and `mapPath` or `mapDocument` resolve later. + map: photocraft_io::smart_map::displace_embedded_map(p), + } +} + +/// Filter › Distort › Shear parameters: the listed curve points, or one bend from `amount`. +fn shear(p: &Value) -> FilterParams { + FilterParams::Shear { + points: list::<[f32; 2]>(p, "points").unwrap_or_else(|| { + let a = f(p, "amount", 0.0).clamp(-100.0, 100.0) / 100.0; + vec![[0.0, 0.0], [0.5, a], [1.0, 0.0]] + }), + undefined: undefined(p, "wrap"), + } +} + +/// Filter › Render › Lens Flare parameters. +fn lens_flare(p: &Value) -> FilterParams { + FilterParams::LensFlare { + brightness: f(p, "brightness", 100.0).clamp(10.0, 300.0), + center_x: f(p, "centerX", 0.5).clamp(-0.5, 1.5), + center_y: f(p, "centerY", 0.5).clamp(-0.5, 1.5), + lens: choice(p, "lens", "zoom", LENS_TYPES, LensType::Zoom), + } +} + +/// The one light Lighting Effects uses when the params list no `lights`. +fn single_light(p: &Value) -> Light { + let c = colour(p, "color", WHITE); + Light { + kind: choice(p, "lightType", "spot", LIGHT_KINDS, LightKind::Spot), + x: f(p, "lightX", 0.25), + y: f(p, "lightY", 0.2), + z: f(p, "lightZ", 0.6).clamp(0.0, 10.0), + target_x: f(p, "targetX", 0.5), + target_y: f(p, "targetY", 0.55), + angle: f(p, "angle", 135.0), + elevation: f(p, "elevation", 45.0).clamp(0.0, 90.0), + color: [c[0], c[1], c[2]], + intensity: f(p, "intensity", 75.0).clamp(-100.0, 100.0), + cone: f(p, "cone", 45.0).clamp(1.0, 89.0), + hotspot: f(p, "hotspot", 50.0).clamp(0.0, 100.0), + radius: f(p, "radius", 0.8).clamp(0.01, 10.0), + } +} + +/// Filter › Render › Lighting Effects parameters. +fn lighting_effects(p: &Value) -> FilterParams { + FilterParams::LightingEffects { + lights: list::(p, "lights").unwrap_or_else(|| vec![single_light(p)]), + gloss: f(p, "gloss", 0.0).clamp(-100.0, 100.0), + metallic: f(p, "metallic", 0.0).clamp(-100.0, 100.0), + exposure: f(p, "exposure", 0.0).clamp(-100.0, 100.0), + ambience: f(p, "ambience", 8.0).clamp(-100.0, 100.0), + texture: choice(p, "texture", "none", TEXTURE_CHANNELS, TextureChannel::None), + height: f(p, "height", 50.0).clamp(0.0, 100.0), + white_is_high: b(p, "whiteIsHigh", true), + } +} + +/// Filter › Blur › Smart Blur parameters. +fn smart_blur(p: &Value) -> FilterParams { + FilterParams::SmartBlur { + radius: f(p, "radius", 3.0).clamp(0.1, 100.0), + threshold: f(p, "threshold", 25.0).clamp(0.1, 100.0), + quality: choice(p, "quality", "high", BLUR_QUALITIES, BlurQuality::High), + mode: choice(p, "mode", "normal", SMART_BLUR_MODES, SmartBlurMode::Normal), + } +} + +/// Filter › Blur › Lens Blur parameters, with the depth map resolved later. +fn lens_blur(p: &Value) -> FilterParams { + FilterParams::LensBlur { + radius: f(p, "radius", 15.0).clamp(0.0, 100.0), + blades: choice(p, "shape", "hexagon", IRIS_BLADES, 6), + curvature: f(p, "bladeCurvature", 0.0).clamp(0.0, 100.0), + rotation: f(p, "rotation", 0.0), + depth: choice(p, "depthMap", "none", DEPTH_SOURCES, DepthSource::None), + focal_distance: f(p, "focalDistance", 0.0).clamp(0.0, 255.0), + invert_depth: b(p, "invert", false), + brightness: f(p, "brightness", 0.0).clamp(0.0, 100.0), + threshold: f(p, "threshold", 255.0).clamp(0.0, 255.0), + noise: f(p, "noise", 0.0).clamp(0.0, 100.0), + distribution: if s(p, "distribution", "uniform") == "gaussian" { Distribution::Gaussian } else { Distribution::Uniform }, + monochromatic: b(p, "monochromatic", false), + seed: seed(p), + depth_map: None, + } +} + +/// Blur Gallery › Tilt-Shift parameters. +fn tilt_shift(p: &Value) -> FilterParams { + FilterParams::TiltShift { + blur: gallery_blur(p), + center_x: f(p, "centerX", 0.5), + center_y: f(p, "centerY", 0.5), + angle: f(p, "angle", 0.0), + focus: f(p, "focus", 0.1).clamp(0.0, 2.0), + transition: f(p, "transition", 0.15).clamp(0.001, 2.0), + } +} + +/// Blur Gallery › Iris Blur parameters: the listed pins, or one pin from the flat params. +fn iris_blur(p: &Value) -> FilterParams { + FilterParams::IrisBlur { + pins: list::(p, "pins").unwrap_or_else(|| { + vec![IrisPin { + x: f(p, "centerX", 0.5), + y: f(p, "centerY", 0.5), + radius_x: f(p, "radiusX", 0.35).max(0.001), + radius_y: f(p, "radiusY", 0.25).max(0.001), + angle: f(p, "angle", 0.0), + roundness: f(p, "roundness", 0.0).clamp(0.0, 100.0), + feather: f(p, "feather", 0.5).clamp(0.0, 0.99), + blur: gallery_blur(p), + }] + }), + } +} + +/// `a` clamped to `lo..=360`, or 0 when it isn't finite. +fn spin_angle(a: f32, lo: f32) -> f32 { + if a.is_finite() { a.clamp(lo, 360.0) } else { 0.0 } +} + +/// Blur Gallery › Spin Blur parameters: the listed pins with bounded angles, or one pin from the flat params. +fn spin_blur(p: &Value) -> FilterParams { + FilterParams::SpinBlur { + pins: list::(p, "pins") + .map(|mut v| { + for pin in &mut v { + pin.angle = spin_angle(pin.angle, -360.0); + pin.blur_angle = spin_angle(pin.blur_angle, 0.0); + } + v + }) + .unwrap_or_else(|| { + vec![SpinPin { x: f(p, "centerX", 0.5), y: f(p, "centerY", 0.5), - radius_x: f(p, "radiusX", 0.35).max(0.001), - radius_y: f(p, "radiusY", 0.25).max(0.001), - angle: f(p, "angle", 0.0), - roundness: f(p, "roundness", 0.0).clamp(0.0, 100.0), - feather: f(p, "feather", 0.5).clamp(0.0, 0.99), - blur: blur(15.0), + radius_x: f(p, "radiusX", 0.3).max(0.001), + radius_y: f(p, "radiusY", 0.3).max(0.001), + angle: spin_angle(f(p, "angle", 0.0), -360.0), + blur_angle: f(p, "blurAngle", 15.0).clamp(0.0, 360.0), }] }), - }, - "filter.blurGallery.fieldBlur" => FilterParams::FieldBlur { - pins: list::(p, "pins").unwrap_or_else(|| vec![FieldPin { x: f(p, "centerX", 0.5), y: f(p, "centerY", 0.5), blur: blur(15.0) }]), - }, - "filter.blurGallery.spinBlur" => FilterParams::SpinBlur { - pins: list::(p, "pins") - .map(|mut v| { - for pin in &mut v { - pin.angle = if pin.angle.is_finite() { pin.angle.clamp(-360.0, 360.0) } else { 0.0 }; - pin.blur_angle = if pin.blur_angle.is_finite() { pin.blur_angle.clamp(0.0, 360.0) } else { 0.0 }; - } - v - }) - .unwrap_or_else(|| { - let a = f(p, "angle", 0.0); - vec![SpinPin { - x: f(p, "centerX", 0.5), - y: f(p, "centerY", 0.5), - radius_x: f(p, "radiusX", 0.3).max(0.001), - radius_y: f(p, "radiusY", 0.3).max(0.001), - angle: if a.is_finite() { a.clamp(-360.0, 360.0) } else { 0.0 }, - blur_angle: f(p, "blurAngle", 15.0).clamp(0.0, 360.0), - }] - }), - }, - "filter.blurGallery.pathBlur" => FilterParams::PathBlur { - paths: list::(p, "paths") - .map(|mut v| { - // Entry speeds bypass the scalar clamp above; the halo - // casts one to i32 and adds, so an unclamped value - // overflows (#708). - for b in &mut v { - b.speed = b.speed.clamp(0.0, 500.0); - } - v - }) - .unwrap_or_else(|| { - vec![BlurPath { - points: vec![[f(p, "startX", 0.2), f(p, "startY", 0.5)], [f(p, "endX", 0.8), f(p, "endY", 0.5)]], - speed: f(p, "speed", 50.0).clamp(0.0, 500.0), - taper: f(p, "taper", 0.0).clamp(0.0, 100.0), - }] - }), - }, - // ---- Other ---- - "filter.other.custom" => FilterParams::Custom { - kernel: p - .get("kernel") - .and_then(Value::as_array) - .map(|a| a.iter().take(25).map(|v| v.as_f64().unwrap_or(0.0).clamp(-999.0, 999.0) as f32).collect()) - .unwrap_or_else(|| (0..25).map(|k| if k == 12 { 1.0 } else { 0.0 }).collect()), - scale: f(p, "scale", 1.0).clamp(1.0, 9999.0), - offset: f(p, "offset", 0.0).clamp(-9999.0, 9999.0), - }, - "filter.other.hsbHsl" => { - let m = |k: &str, d: &str| match s(p, k, d) { - "hsb" => HsbModel::Hsb, - "hsl" => HsbModel::Hsl, - _ => HsbModel::Rgb, - }; - FilterParams::HsbHsl { input: m("inputMode", "rgb"), output: m("rowOrder", "hsb") } - } - crate::color_to_alpha_cmds::ID => crate::color_to_alpha_cmds::params(p), - // ---- Video ---- - "filter.video.deInterlace" => FilterParams::DeInterlace { - eliminate_even: s(p, "eliminate", "oddFields") == "evenFields", - interpolate: s(p, "createBy", "interpolation") != "duplication", - }, - "filter.video.ntscColors" => FilterParams::NtscColors, - _ => return crate::gallery_cmds::params_for(id, p), - }) + } +} + +/// Blur Gallery › Path Blur parameters: the listed paths with bounded speeds, or one straight path from the flat params. +fn path_blur(p: &Value) -> FilterParams { + FilterParams::PathBlur { + paths: list::(p, "paths") + .map(|mut v| { + // The halo casts speeds to i32, so unclamped entries overflow (#708). + for b in &mut v { + b.speed = b.speed.clamp(0.0, 500.0); + } + v + }) + .unwrap_or_else(|| { + vec![BlurPath { + points: vec![[f(p, "startX", 0.2), f(p, "startY", 0.5)], [f(p, "endX", 0.8), f(p, "endY", 0.5)]], + speed: f(p, "speed", 50.0).clamp(0.0, 500.0), + taper: f(p, "taper", 0.0).clamp(0.0, 100.0), + }] + }), + } +} + +/// Filter › Other › Custom parameters: up to 25 kernel weights (the identity by default), scale and offset. +fn custom(p: &Value) -> FilterParams { + FilterParams::Custom { + kernel: p + .get("kernel") + .and_then(Value::as_array) + .map(|a| a.iter().take(25).map(|v| v.as_f64().unwrap_or(0.0).clamp(-999.0, 999.0) as f32).collect()) + .unwrap_or_else(|| (0..25).map(|k| if k == 12 { 1.0 } else { 0.0 }).collect()), + scale: f(p, "scale", 1.0).clamp(1.0, 9999.0), + offset: f(p, "offset", 0.0).clamp(-9999.0, 9999.0), + } } /// Ids whose rendering uses the foreground/background colours. fn uses_colours(id: &str) -> bool { - matches!(id, "filter.render.fibers" | "filter.stylize.tiles" | "filter.pixelate.pointillize") || crate::gallery_cmds::uses_colours(id) + matches!(id, "filter.render.clouds" | "filter.render.differenceClouds" | "filter.render.fibers" | "filter.stylize.tiles" | "filter.pixelate.pointillize") + || crate::gallery_cmds::uses_colours(id) } /// Fills in session state a filter reads (the current colours) as explicit params, so the @@ -429,8 +502,19 @@ fn map_from_document(doc: &Document, layer: Option) -> Option } /// Resolves inputs that live outside the target layer: Displace's map (`mapPath`, or -/// `mapDocument` index with optional `mapLayer` id). +/// `mapDocument` index with optional `mapLayer` id), and the alpha channel Lens Blur reads its +/// depth from (`depthChannel`, an index or a name). pub(crate) fn resolve(s: &Session, fp: &mut FilterParams, p: &Value) -> Result<()> { + if let FilterParams::LensBlur { depth: DepthSource::Channel, depth_map, .. } = fp { + let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; + let channel = p + .get("depthChannel") + .ok_or_else(|| EngineError::Other("Lens Blur: `depthMap` channel needs `depthChannel` (an alpha channel's index or name)".into()))?; + let surface = crate::edit_menu_cmds::channel_mask(doc, channel) + .ok_or_else(|| EngineError::Other(format!("Lens Blur: the document has no alpha channel {channel}")))?; + *depth_map = Some(Arc::new(algo::Image::read(surface, doc.bounds()))); + return Ok(()); + } let FilterParams::Displace { map, .. } = fp else { return Ok(()) }; if map.is_some() { return Ok(()); @@ -438,7 +522,7 @@ pub(crate) fn resolve(s: &Session, fp: &mut FilterParams, p: &Value) -> Result<( let layer = p.get("mapLayer").and_then(Value::as_u64); let img = if let Some(path) = map_path(p) { map_from_path(path, layer)? - } else if let Some(d) = p.get("mapDocument") { + } else if let Some(d) = map_document(p) { let docs = s.documents(); let found = match d { Value::String(name) => docs.iter().find(|ds| ds.doc.name == *name || ds.path.as_deref() == Some(name.as_str())), @@ -453,10 +537,23 @@ pub(crate) fn resolve(s: &Session, fp: &mut FilterParams, p: &Value) -> Result<( Ok(()) } -fn map_path(p: &Value) -> Option<&str> { +/// Displace's params as a request for its map: a map named in them (`mapPath` or `mapDocument`) +/// wins over the one already embedded, which is read only when none is named. Otherwise a dialog +/// or agent passing the filter's stored params back with a new map got the old map again. +pub(crate) fn displace_request(p: &Value) -> Value { + if map_path(p).is_some() || map_document(p).is_some() { photocraft_io::smart_map::displace_without_map(p) } else { p.clone() } +} + +pub(crate) fn map_path(p: &Value) -> Option<&str> { p.get("mapPath").and_then(Value::as_str).filter(|p| !p.is_empty()) } +/// Displace's `mapDocument`: an index or a name. Null and a negative index (the dialog's "None") +/// name no document. +pub(crate) fn map_document(p: &Value) -> Option<&Value> { + p.get("mapDocument").filter(|d| !d.is_null() && d.as_i64().is_none_or(|i| i >= 0)) +} + /// Displace's map read from a file (flattened, or its `mapLayer`). fn map_from_path(path: &str, layer: Option) -> Result> { let bytes = std::fs::read(path).map_err(|e| EngineError::Other(format!("cannot read displacement map `{path}`: {e}")))?; @@ -466,13 +563,15 @@ fn map_from_path(path: &str, layer: Option) -> Result> /// The session-free part of [`resolve`]: Displace's `mapPath`. Smart filters record params, not /// the map, so their re-render reads the map again here; without it the filter passed the pixels -/// through. `mapDocument` needs the session and is left unresolved. +/// through. The path is stored in the document, so it is followed only as +/// [`crate::doc_path::resolve`] allows. `mapDocument` needs the session and is left unresolved. pub(crate) fn resolve_map_path(fp: &mut FilterParams, p: &Value) -> Result<()> { if let FilterParams::Displace { map, .. } = fp && map.is_none() && let Some(path) = map_path(p) { - *map = map_from_path(path, p.get("mapLayer").and_then(Value::as_u64))?.map(Arc::new); + let file = crate::doc_path::resolve(path).map_err(|why| crate::doc_path::refusal("the displacement map", path, why))?; + *map = map_from_path(&file.to_string_lossy(), p.get("mapLayer").and_then(Value::as_u64))?.map(Arc::new); } Ok(()) } @@ -614,6 +713,8 @@ pub fn specs() -> Vec { r##"{"amount":-100..100=10,"ridges":0..20=5,"style":"pondRipples|outFromCenter|aroundCenter"}"## ), // Render + cmd!("filter.render.clouds", "Clouds", ["Filter", "Render"], r##"{"seed":u32=0,"foreground":json,"background":json}"##), + cmd!("filter.render.differenceClouds", "Difference Clouds", ["Filter", "Render"], r##"{"seed":u32=0,"foreground":json,"background":json}"##), cmd!( "filter.render.fibers", "Fibers…", @@ -660,7 +761,7 @@ pub fn specs() -> Vec { "filter.blur.lensBlur", "Lens Blur…", ["Filter", "Blur"], - r##"{"radius":0..100=15,"shape":"hexagon|triangle|square|pentagon|heptagon|octagon","bladeCurvature":0..100=0,"rotation":0..360=0,"depthMap":"none|transparency|layerMask","focalDistance":0..255=0,"invert":bool,"brightness":0..100=0,"threshold":0..255=255,"noise":0..100=0,"distribution":"uniform|gaussian","monochromatic":bool,"seed":u32=0}"## + r##"{"radius":0..100=15,"shape":"hexagon|triangle|square|pentagon|heptagon|octagon","bladeCurvature":0..100=0,"rotation":0..360=0,"depthMap":"none|transparency|layerMask|channel","depthChannel":index|name?,"focalDistance":0..255=0,"invert":bool,"brightness":0..100=0,"threshold":0..255=255,"noise":0..100=0,"distribution":"uniform|gaussian","monochromatic":bool,"seed":u32=0}"## ), cmd!( "filter.blur.shapeBlur", diff --git a/crates/engine/src/filters_ext/tests.rs b/crates/engine/src/filters_ext/tests.rs index 0f32c5367d..490568e87c 100644 --- a/crates/engine/src/filters_ext/tests.rs +++ b/crates/engine/src/filters_ext/tests.rs @@ -220,6 +220,51 @@ fn lens_blur_uses_the_layer_mask_as_depth() { assert_ne!(pixels(&s), before); } +/// #7400: an alpha channel is a Lens Blur depth source, by name or index, as Photoshop lists +/// the document's channels in Depth Map › Source. A missing channel is an error that changes +/// nothing, and on a Smart Object (whose smart filters re-render without the channels) it is +/// refused instead of silently blurring without depth. +#[test] +fn lens_blur_reads_an_alpha_channel_as_depth() { + let mut s = session(); + // A white channel: depth 1 everywhere. Creating it targets it; the filter is for the layer. + s.execute("channel.new", json!({"name": "Depth Map", "fill": "white"})).unwrap(); + s.execute("channel.target", json!({"channel": "composite"})).unwrap(); + let before = pixels(&s); + for channel in [json!("Depth Map"), json!(0)] { + let p = |focal: f64| json!({"radius": 6, "depthMap": "channel", "depthChannel": channel, "focalDistance": focal}); + // Focal distance 255 puts depth 1 in focus: nothing blurs. + s.execute("filter.blur.lensBlur", p(255.0)).unwrap(); + assert_eq!(pixels(&s), before, "{channel}: in focus"); + s.execute("filter.blur.lensBlur", p(0.0)).unwrap(); + assert_ne!(pixels(&s), before, "{channel}: out of focus blurs"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(pixels(&s), before); + } + for bad in [ + json!({"radius": 6, "depthMap": "channel"}), + json!({"radius": 6, "depthMap": "channel", "depthChannel": "Nope"}), + json!({"radius": 6, "depthMap": "channel", "depthChannel": 7}), + ] { + let doc = s.active().unwrap().doc.clone(); + let past = s.active().unwrap().history.past_len(); + let e = s.execute("filter.blur.lensBlur", bad.clone()).unwrap_err().to_string(); + assert!(e.contains("Lens Blur"), "{bad}: {e}"); + assert_eq!(pixels(&s), before, "{bad}: nothing changed"); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&doc, &st.doc), "{bad}: the document was replaced"); + assert_eq!(st.history.past_len(), past, "{bad}: a history step was recorded"); + } + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let doc = s.active().unwrap().doc.clone(); + let past = s.active().unwrap().history.past_len(); + let e = s.execute("filter.blur.lensBlur", json!({"radius": 6, "depthMap": "channel", "depthChannel": "Depth Map"})).unwrap_err().to_string(); + assert!(e.contains("Smart Object"), "{e}"); + let st = s.active().unwrap(); + assert!(std::sync::Arc::ptr_eq(&doc, &st.doc), "no smart filter was added"); + assert_eq!(st.history.past_len(), past, "no history step"); +} + #[test] fn params_map_to_algorithm_units() { assert_eq!(params_for("filter.pixelate.crystallize", &json!({"cellSize": 1})), Some(FilterParams::Crystallize { cell_size: 3.0, seed: 0 })); diff --git a/crates/engine/src/float_cmds/tests.rs b/crates/engine/src/float_cmds/tests.rs deleted file mode 100644 index bb2ba5b91c..0000000000 --- a/crates/engine/src/float_cmds/tests.rs +++ /dev/null @@ -1,191 +0,0 @@ -use photocraft_geom::Rect; -use serde_json::json; - -use super::*; - -/// An 80×60 transparent document whose layer is painted red over (10..30)²; (10..30)² selected. -fn session() -> (Session, LayerId) { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 80, "height": 60, "background": "transparent"})).unwrap(); - let id = s.active().unwrap().active_layer.unwrap(); - s.edit("paint", |doc, _| { - doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); - (s, id) -} - -fn alpha(d: &Document, id: LayerId, x: i32, y: i32) -> f32 { - d.layer(id).unwrap().surface().unwrap().rgba(x, y)[3] -} - -#[test] -fn float_moves_a_cut_piece_without_touching_the_document_until_dropped() { - let (mut s, id) = session(); - let steps = s.active().unwrap().history.past_len(); - assert_eq!(s.execute("select.float", json!({"dx": 15, "dy": 0})).unwrap()["offset"], json!([15, 0])); - // Moving it again doesn't cut anything new. - s.execute("select.float", json!({"dx": 0, "dy": 10})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(floating(st).unwrap().offset, (15, 10)); - assert_eq!(alpha(&st.doc, id, 12, 12), 1.0, "the document is untouched while floating"); - assert_eq!(st.history.past_len(), steps); - let shown = displayed(st, (0, 0)).unwrap(); - assert!(alpha(&shown, id, 12, 12) == 0.0 && alpha(&shown, id, 27, 22) == 1.0, "shown cut and moved"); - // Drop: one history step, pixels and selection where they were shown. - s.execute("select.drop", json!({})).unwrap(); - let st = s.active().unwrap(); - assert!(floating(st).is_none()); - assert_eq!(st.history.past_len(), steps + 1); - for (x, y) in [(12, 12), (27, 22), (44, 39), (40, 40)] { - assert_eq!(alpha(&st.doc, id, x, y), alpha(&shown, id, x, y), "dropped as shown at ({x}, {y})"); - } - assert_eq!(st.doc.selection.as_ref().unwrap().content_bounds(), Rect::new(25, 20, 45, 40)); - // Undo takes the whole move back. - s.undo(); - assert_eq!(alpha(&s.active().unwrap().doc, id, 12, 12), 1.0); -} - -#[test] -fn any_other_command_drops_it_and_undo_puts_it_back() { - let (mut s, id) = session(); - let steps = s.active().unwrap().history.past_len(); - s.execute("select.float", json!({"dx": 20, "dy": 0})).unwrap(); - // Undo while floating: the piece goes back; nothing in the document or history changed. - assert_eq!(s.execute("edit.undo", json!({})).unwrap()["floating"], json!("returned")); - let st = s.active().unwrap(); - assert!(floating(st).is_none() && alpha(&st.doc, id, 12, 12) == 1.0 && st.history.past_len() == steps); - // Float again, then deselect: dropped first (where it was shown), then deselected. - s.execute("select.float", json!({"dx": 20, "dy": 0})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); - let st = s.active().unwrap(); - assert!(st.doc.selection.is_none() && floating(st).is_none()); - assert!(alpha(&st.doc, id, 12, 12) == 0.0 && alpha(&st.doc, id, 35, 20) == 1.0); -} - -#[test] -fn needs_a_selection_and_an_unlocked_pixel_layer() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 20})).unwrap(); - assert!(!s.is_enabled("select.float"), "no selection"); - s.execute("select.all", json!({})).unwrap(); - assert!(s.is_enabled("select.float"), "the Background's selected pixels move"); - s.execute("layer.new.layer", json!({})).unwrap(); - assert!(s.is_enabled("select.float")); - s.execute("layer.lockLayers", json!({"position": true})).unwrap(); - assert!(!s.is_enabled("select.float"), "a position-locked layer"); -} - -/// On the Background, which can't hold transparency, the hole takes the background colour. -#[test] -fn on_the_background_the_hole_takes_the_background_colour() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 40})).unwrap(); - s.tools.background = [0.0, 0.0, 1.0, 1.0]; - let id = s.active().unwrap().active_layer.unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("select.float", json!({"dx": 15, "dy": 0})).unwrap(); - s.execute("select.drop", json!({})).unwrap(); - let px = |x, y| s.active().unwrap().doc.layer(id).unwrap().surface().unwrap().rgba(x, y); - assert_eq!(px(12, 12), [0.0, 0.0, 1.0, 1.0], "the hole"); - assert_eq!(px(27, 12), [1.0, 1.0, 1.0, 1.0], "the moved pixels"); - assert_eq!(px(5, 5), [1.0, 1.0, 1.0, 1.0], "the rest"); -} - -/// Copying leaves the original pixels, at every depth; the move is one undo step. -#[test] -fn a_copy_leaves_the_original_at_every_depth_in_one_undo_step() { - for depth in [8, 16, 32] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 80, "height": 60, "background": "transparent", "depth": depth})).unwrap(); - let id = s.active().unwrap().active_layer.unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 20})).unwrap(); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - let steps = s.active().unwrap().history.past_len(); - s.execute("select.float", json!({"dx": 30, "dy": 0, "copy": true})).unwrap(); - s.execute("select.drop", json!({})).unwrap(); - let st = s.active().unwrap(); - assert!(alpha(&st.doc, id, 12, 12) == 1.0 && alpha(&st.doc, id, 42, 12) == 1.0, "{depth}-bit: both"); - assert_eq!((st.history.past_len(), st.history.undo_label()), (steps + 1, Some("Move")), "{depth}-bit"); - s.undo(); - let st = s.active().unwrap(); - assert!(alpha(&st.doc, id, 12, 12) == 1.0 && alpha(&st.doc, id, 42, 12) == 0.0, "{depth}-bit: undone"); - } -} - -/// `cargo test --release -p photocraft-engine float_drag_bench -- --ignored --nocapture` -#[test] -#[ignore] -fn float_drag_bench() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 6000, "height": 4000, "background": "transparent"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, a| { - doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 6000, 4000), &[0.2, 0.4, 0.6, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("select.rect", json!({"x": 1000, "y": 1000, "width": 500, "height": 400})).unwrap(); - let t = std::time::Instant::now(); - s.execute("select.float", json!({})).unwrap(); - let cut = t.elapsed(); - let t = std::time::Instant::now(); - for i in 0..20 { - displayed(s.active().unwrap(), (i * 7, i * 3)).unwrap(); - } - eprintln!("6000×4000 layer, 500×400 piece: cut {cut:?}, then {:?} per pointer move", t.elapsed() / 20); -} - -#[test] -fn copy_floats_a_duplicate_and_leaves_the_original() { - let (mut s, id) = session(); - s.execute("select.float", json!({"dx": 30, "dy": 0, "copy": true})).unwrap(); - let shown = displayed(s.active().unwrap(), (0, 0)).unwrap(); - assert!(alpha(&shown, id, 12, 12) == 1.0 && alpha(&shown, id, 42, 12) == 1.0, "original kept, copy shown"); - s.execute("select.drop", json!({})).unwrap(); - let doc = &s.active().unwrap().doc; - assert!(alpha(doc, id, 12, 12) == 1.0 && alpha(doc, id, 42, 12) == 1.0, "dropped: both"); -} - -#[test] -fn float_with_the_mask_target_moves_the_mask_not_the_layer() { - let (mut s, id) = session(); - s.edit("mask", |doc, _| { - let l = doc.layer_mut(id).unwrap(); - let mut m = photocraft_doc::LayerMask::reveal_all(); - m.surface.fill_rect(Rect::new(10, 10, 30, 30), &[0.0]); - l.mask = Some(m); - Ok(()) - }) - .unwrap(); - let mask_at = |s: &Session, x, y| s.active().unwrap().doc.layer(id).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0); - s.execute("select.float", json!({"dx": 25, "dy": 0, "copy": true, "target": "mask"})).unwrap(); - s.execute("select.drop", json!({})).unwrap(); - assert_eq!(mask_at(&s, 12, 12), 0.0, "the copy leaves the mask where it was"); - assert_eq!(mask_at(&s, 37, 12), 0.0, "the mask's selected part moved"); - assert_eq!(mask_at(&s, 60, 12), 1.0); - assert_eq!(alpha(&s.active().unwrap().doc, id, 37, 12), 0.0, "the layer's pixels did not move"); - assert_eq!(alpha(&s.active().unwrap().doc, id, 12, 12), 1.0); -} - -#[test] -fn cutting_an_untouched_part_of_the_mask_moves_it_and_leaves_the_background_colour() { - let (mut s, id) = session(); - s.edit("mask", |doc, _| { - let mut m = photocraft_doc::LayerMask::hide_all(); - m.surface.fill_rect(Rect::new(50, 10, 70, 30), &[1.0]); - doc.layer_mut(id).unwrap().mask = Some(m); - Ok(()) - }) - .unwrap(); - s.tools.background = [0.5, 0.5, 0.5, 1.0]; - let mask_at = |s: &Session, x, y| s.active().unwrap().doc.layer(id).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0); - // (10..30)² is the mask's untouched black; it moves over the white at (50..70, 10..30). - s.execute("select.float", json!({"dx": 40, "dy": 0, "target": "mask"})).unwrap(); - s.execute("select.drop", json!({})).unwrap(); - assert_eq!(mask_at(&s, 55, 12), 0.0, "the untouched black moved"); - assert!((mask_at(&s, 12, 12) - 0.5).abs() < 0.01, "the hole takes the background colour"); - assert_eq!(mask_at(&s, 40, 12), 0.0); -} diff --git a/crates/engine/src/group_options.rs b/crates/engine/src/group_options.rs new file mode 100644 index 0000000000..dd2ddee02f --- /dev/null +++ b/crates/engine/src/group_options.rs @@ -0,0 +1,264 @@ +//! The options of Photoshop's New Group dialog (Name, Color, Mode, Opacity), shared by +//! Layer › New › Group… (`layer.new.group`) and Group from Layers (`layer.new.groupFromLayers`, +//! `layer.groupLayers`), so the dialog's OK is a single history step. + +use photocraft_color::BlendMode; +use photocraft_doc::{LabelColor, Layer}; +use serde_json::Value; + +use crate::{EngineError, Result}; + +/// New Group options parsed from command params; an absent (or `null`) key keeps the default. +#[derive(Debug, Default)] +pub(crate) struct GroupOptions { + /// `None` (or an empty name) takes the next "Group N". + pub name: Option, + color: Option, + blend: Option, + opacity: Option, +} + +impl GroupOptions { + /// Parses `name`, `color` (a layer colour id), `mode` (a blend mode, Pass Through included) + /// and `opacity` (percent, clamped to 0..100). Validates everything before any edit runs. + pub(crate) fn parse(cmd: &str, p: &Value) -> Result { + let bad = |msg: &str| EngineError::BadParams { cmd: cmd.into(), msg: msg.into() }; + let get = |key: &str| p.get(key).filter(|v| !v.is_null()); + let name = match get("name") { + None => None, + Some(v) => Some(v.as_str().ok_or_else(|| bad("`name` must be a string"))?.to_string()).filter(|n| !n.trim().is_empty()), + }; + let color = match get("color") { + None => None, + Some(v) => Some(v.as_str().and_then(LabelColor::from_id).ok_or_else(|| { + bad("`color` must be a layer colour id (none, red, orange, yellow, green, seafoam, blue, indigo, magenta, fuchsia, violet, gray)") + })?), + }; + let blend = match get("mode") { + None => None, + Some(v) => Some( + v.as_str() + .and_then(crate::commands::blend_from_str) + .ok_or_else(|| bad("`mode` must be a blend mode such as \"passThrough\" or \"multiply\""))?, + ), + }; + let opacity = match get("opacity") { + None => None, + Some(v) => { + let o = v.as_f64().filter(|o| o.is_finite()).ok_or_else(|| bad("`opacity` must be a number (percent, 0..100)"))?; + Some((o.clamp(0.0, 100.0) / 100.0) as f32) + } + }; + Ok(Self { name, color, blend, opacity }) + } + + /// Sets the mode, opacity and colour of a new `group`. A colour also reaches every layer inside + /// it, as Layer › Layer Color does on a group; "No Color" leaves the layers' own colours alone. + pub(crate) fn apply(&self, group: &mut Layer) { + if let Some(blend) = self.blend { + group.blend = blend; + } + if let Some(opacity) = self.opacity { + group.opacity = opacity; + } + match self.color { + Some(LabelColor::None) | None => {} + Some(color) => { + // Explicit stack: documents can nest groups deeply. + let mut stack = vec![group]; + while let Some(l) = stack.pop() { + l.label = color; + if let Some(children) = l.children_mut() { + stack.extend(children.iter_mut()); + } + } + } + } + } +} + +/// `layer.new.group`'s `position`: `below` (the Layers panel's ⌘/Ctrl-click) or `above` (the default). +pub(crate) fn below(cmd: &str, p: &Value) -> Result { + match p.get("position").filter(|v| !v.is_null()) { + None => Ok(false), + Some(v) => match v.as_str() { + Some("above") => Ok(false), + Some("below") => Ok(true), + _ => Err(EngineError::BadParams { cmd: cmd.into(), msg: "`position` must be above or below".into() }), + }, + } +} + +#[cfg(test)] +mod tests { + use photocraft_color::BlendMode; + use photocraft_doc::{Document, LabelColor, Layer, LayerContent, LayerId}; + use serde_json::{Value, json}; + + use crate::Session; + + /// Background, then `A` and `B` (`B` active). + fn session() -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + let a = id(s.execute("layer.new.layer", json!({"name": "A"})).unwrap()); + let b = id(s.execute("layer.new.layer", json!({"name": "B"})).unwrap()); + (s, a, b) + } + + fn id(v: Value) -> LayerId { + LayerId(v["layer"].as_u64().unwrap()) + } + + fn doc(s: &Session) -> &Document { + &s.active().unwrap().doc + } + + fn layer(s: &Session, id: LayerId) -> &Layer { + doc(s).layer(id).unwrap() + } + + fn expanded(s: &Session, id: LayerId) -> bool { + matches!(&layer(s, id).content, LayerContent::Group(g) if g.expanded) + } + + fn steps(s: &Session) -> usize { + s.active().unwrap().history.entries().len() + } + + fn top(s: &Session) -> Vec { + doc(s).layers.iter().map(|l| l.id).collect() + } + + #[test] + fn new_group_defaults_and_dialog_options() { + let (mut s, _, _) = session(); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + let l = layer(&s, g); + assert_eq!((l.name.as_str(), l.blend, l.opacity, l.label), ("Group 1", BlendMode::PassThrough, 1.0, LabelColor::None)); + assert!(expanded(&s, g), "an empty new group stays open"); + let before = steps(&s); + let g = id(s.execute("layer.new.group", json!({"name": "Sky", "color": "red", "mode": "multiply", "opacity": 40})).unwrap()); + let l = layer(&s, g); + assert_eq!((l.name.as_str(), l.blend, l.label), ("Sky", BlendMode::Multiply, LabelColor::Red)); + assert!((l.opacity - 0.4).abs() < 1e-6); + assert_eq!(steps(&s), before + 1, "the dialog's OK is one history step"); + // Labels as shown in the dialog work too; opacity is clamped to 0..100 %, a blank name is "Group N". + let g = id(s.execute("layer.new.group", json!({"name": " ", "mode": "Linear Dodge (Add)", "opacity": 250})).unwrap()); + let l = layer(&s, g); + assert_eq!((l.name.as_str(), l.blend, l.opacity), ("Group 2", BlendMode::LinearDodge, 1.0)); + let g = id(s.execute("layer.new.group", json!({"opacity": -5, "name": null, "color": null, "mode": null, "position": null})).unwrap()); + assert_eq!(layer(&s, g).opacity, 0.0); + } + + #[test] + fn group_from_layers_is_collapsed_and_takes_the_dialog_options() { + let (mut s, a, b) = session(); + s.execute("layer.setLabelColor", json!({"layer": a.0, "color": "red"})).unwrap(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap(); + let before = steps(&s); + let g = id(s.execute("layer.new.groupFromLayers", json!({"name": "Pair", "color": "none", "mode": "normal", "opacity": 50})).unwrap()); + assert_eq!(steps(&s), before + 1, "one history step"); + let l = layer(&s, g); + assert_eq!((l.name.as_str(), l.blend, l.opacity, l.label), ("Pair", BlendMode::Normal, 0.5, LabelColor::None)); + assert!(!expanded(&s, g), "Photoshop shows a group made from layers closed"); + assert_eq!(layer(&s, a).label, LabelColor::Red, "No Color leaves the layers' own colours alone"); + let inspect = s.execute("document.inspect", json!({})).unwrap(); + let row = inspect["layers"].as_array().unwrap().iter().find(|l| l["id"] == json!(g.0)).unwrap().clone(); + assert_eq!(row["expanded"], false, "document.inspect reports it"); + // Undo removes the group; redo brings it back closed. + s.undo(); + assert!(doc(&s).layer(g).is_none()); + s.redo(); + assert!(!expanded(&s, g)); + // ⌘G with a colour labels the group and every layer in it, like Layer › Layer Color. + s.undo(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap(); + let g = id(s.execute("layer.groupLayers", json!({"color": "green"})).unwrap()); + assert!(!expanded(&s, g)); + for l in [g, a, b] { + assert_eq!(layer(&s, l).label, LabelColor::Green); + } + } + + #[test] + fn below_puts_the_new_group_under_the_active_layer() { + let (mut s, a, b) = session(); + let bg = top(&s)[0]; + let g = id(s.execute("layer.new.group", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s), vec![bg, a, g, b]); + // Inside a group it stays inside, under the active layer. + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + let outer = id(s.execute("layer.groupLayers", json!({})).unwrap()); + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + let inner = id(s.execute("layer.new.group", json!({"position": "below"})).unwrap()); + let children: Vec = layer(&s, outer).children().unwrap().iter().map(|l| l.id).collect(); + assert_eq!(children, vec![inner, b]); + // Nothing goes under the Background: the group goes above it instead. + s.execute("layer.select", json!({"layer": bg.0})).unwrap(); + let g2 = id(s.execute("layer.new.group", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s)[..2], [bg, g2]); + // With no active layer it goes to the top, like a plain New Group. + s.execute("select.deselectLayers", json!({})).unwrap(); + let g3 = id(s.execute("layer.new.group", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s).last(), Some(&g3)); + } + + /// A group added inside a clipping mask (⌘-click under a clipped layer, or a plain New Group + /// on the base) joins the mask, as in Photoshop: the clipped layers keep their base and stay + /// visible, and nothing needs recompositing. + #[test] + fn a_group_added_inside_a_clipping_mask_joins_it() { + let (mut s, a, b) = session(); + s.execute("layer.setProps", json!({"layer": b.0, "clipped": true})).unwrap(); + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + let g = id(s.execute("layer.new.group", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s)[1..], [a, g, b]); + assert!(layer(&s, g).clipped, "under a clipped layer the group is clipped too"); + assert_eq!(s.active().unwrap().last_damage, Some(photocraft_geom::Rect::EMPTY), "B still clips to A"); + // On the base itself: the new group goes between A and its clipped layers. + s.undo(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + assert_eq!(top(&s)[1..], [a, g, b]); + assert!(layer(&s, g).clipped); + // Outside a clipping mask nothing changes. + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + assert!(!layer(&s, g).clipped); + } + + #[test] + fn bad_options_fail_without_touching_the_document() { + let (mut s, a, b) = session(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap(); + for cmd in ["layer.new.group", "layer.new.groupFromLayers", "layer.groupLayers"] { + let mut bad = vec![ + json!({"name": 7}), + json!({"color": 5}), + json!({"color": "pink"}), + json!({"color": "Red"}), + json!({"mode": ""}), + json!({"mode": 3}), + json!({"mode": "sparkle"}), + json!({"opacity": "x"}), + json!({"opacity": [50]}), + ]; + if cmd == "layer.new.group" { + bad.push(json!({"position": "yes"})); + } + for p in bad { + let before = (doc(&s).clone(), s.active().unwrap().revision, steps(&s)); + assert!(s.execute(cmd, p.clone()).is_err(), "{cmd} {p} must be refused"); + assert_eq!((doc(&s).clone(), s.active().unwrap().revision, steps(&s)), before, "{cmd} {p} changed nothing"); + } + // A huge but finite opacity clamps to 100 % instead of failing. + let g = id(s.execute(cmd, json!({"opacity": f64::MAX})).unwrap()); + assert_eq!(layer(&s, g).opacity, 1.0); + s.undo(); + } + } +} diff --git a/crates/engine/src/guide_layout.rs b/crates/engine/src/guide_layout.rs index 4a4261faff..6c4eb89c00 100644 --- a/crates/engine/src/guide_layout.rs +++ b/crates/engine/src/guide_layout.rs @@ -4,6 +4,7 @@ use photocraft_doc::Guides; use photocraft_geom::Rect; use serde_json::Value; +use crate::prefs::Unit; use crate::{EngineError, Result, Session}; const COMMAND: &str = "view.newGuideLayout"; @@ -14,10 +15,46 @@ fn bad(message: impl Into) -> EngineError { EngineError::BadParams { cmd: COMMAND.into(), msg: message.into() } } -fn number(p: &Value, key: &str, default: f64) -> Result { +/// A length as given: a number of pixels, or text with a unit as typed in Photoshop's New Guide +/// Layout fields ("2 cm", "0.25in", "12 pt", "10%"), resolved against each target (#4984). +#[derive(Clone, Copy, Debug, PartialEq)] +enum Length { + Px(f64), + In(f64, Unit), +} + +impl Length { + /// Pixels at the document's `dpi`; percent is of `extent` (the target's width for column + /// sizes and left/right margins, its height for row sizes and top/bottom margins). + fn px(self, key: &str, dpi: f64, extent: f64, ppi: f64) -> Result { + let v = match self { + Length::Px(v) => v, + Length::In(v, unit) => unit.to_px(v, dpi, extent, ppi), + }; + if v.is_finite() && v >= 0.0 { Ok(v) } else { Err(bad(format!("{key} must be a finite non-negative length"))) } + } +} + +fn length_of(key: &str, v: &Value) -> Result { + let invalid = || bad(format!("{key} must be a non-negative number of pixels, or text with px, in, cm, mm, pt, pc or %")); + let length = match (v.as_f64(), v.as_str()) { + (Some(n), _) => Length::Px(n), + (None, Some(text)) => match Unit::parse_length(text).ok_or_else(invalid)? { + (n, None | Some(Unit::Pixels)) => Length::Px(n), + (n, Some(unit)) => Length::In(n, unit), + }, + _ => return Err(invalid()), + }; + match length { + Length::Px(n) | Length::In(n, _) if n.is_finite() && n >= 0.0 => Ok(length), + _ => Err(invalid()), + } +} + +fn number(p: &Value, key: &str, default: Length) -> Result { match p.get(key) { None | Some(Value::Null) => Ok(default), - Some(v) => v.as_f64().filter(|n| n.is_finite() && *n >= 0.0).ok_or_else(|| bad(format!("{key} must be a finite non-negative number"))), + Some(v) => length_of(key, v), } } @@ -110,9 +147,10 @@ pub fn calculate(s: &Session, p: &Value) -> Result { let st = s.active().ok_or(EngineError::NoDocument)?; let cols = count(p, "columns")?; let rows = count(p, "rows")?; - let width = number(p, "width", 0.0)?; - let height = number(p, "height", 0.0)?; - let gap = number(p, "gutter", 0.0)?; + let zero = Length::Px(0.0); + let width = number(p, "width", zero)?; + let height = number(p, "height", zero)?; + let gap = number(p, "gutter", zero)?; let row_gap = number(p, "rowGutter", gap)?; let center = flag(p, "centerColumns")?; let clear = flag(p, "clearExisting")?; @@ -121,15 +159,16 @@ pub fn calculate(s: &Session, p: &Value) -> Result { if a.len() != 4 { return Err(bad("margin must contain top, left, bottom and right")); } - let mut m = [0.0; 4]; + let mut m = [zero; 4]; for (out, value) in m.iter_mut().zip(a) { - *out = value.as_f64().filter(|v| v.is_finite() && *v >= 0.0).ok_or_else(|| bad("margins must be finite non-negative numbers"))?; + *out = length_of("margin", value)?; } m } - _ => [number(p, "margin", 0.0)?; 4], + _ => [number(p, "margin", zero)?; 4], }; - let [top, left, bottom, right] = margins; + let dpi = f64::from(st.doc.resolution_dpi); + let ppi = s.prefs().units_and_rulers.point_size.per_inch(); let bounds = targets(s, p)?; // Bound the aggregate work as well as each count, including multi-artboard requests. if (cols + rows + 4).saturating_mul(2).saturating_mul(bounds.len() as u64) > MAX_EDGES { @@ -137,16 +176,22 @@ pub fn calculate(s: &Session, p: &Value) -> Result { } let mut added = Guides::default(); for rect in &bounds { + let (across, down) = (f64::from(rect.width()), f64::from(rect.height())); + let [top, left, bottom, right] = + [(margins[0], down), (margins[1], across), (margins[2], down), (margins[3], across)].map(|(m, extent)| m.px("margin", dpi, extent, ppi)); + let (top, left, bottom, right) = (top?, left?, bottom?, right?); let (x0, y0, x1, y1) = (f64::from(rect.x0) + left, f64::from(rect.y0) + top, f64::from(rect.x1) - right, f64::from(rect.y1) - bottom); if x0 >= x1 || y0 >= y1 { return Err(bad("margins must leave space inside the target")); } - if margins.iter().any(|x| *x != 0.0) { + if [top, left, bottom, right].iter().any(|x| *x != 0.0) { push_unique(&mut added.vertical, x0)?; push_unique(&mut added.vertical, x1)?; push_unique(&mut added.horizontal, y0)?; push_unique(&mut added.horizontal, y1)?; } + let (width, gap) = (width.px("width", dpi, across, ppi)?, gap.px("gutter", dpi, across, ppi)?); + let (height, row_gap) = (height.px("height", dpi, down, ppi)?, row_gap.px("rowGutter", dpi, down, ppi)?); axis(x0, x1, cols, width, gap, center, &mut added.vertical)?; axis(y0, y1, rows, height, row_gap, center, &mut added.horizontal)?; } diff --git a/crates/engine/src/hidden_target.rs b/crates/engine/src/hidden_target.rs index 249317354a..977d642f21 100644 --- a/crates/engine/src/hidden_target.rs +++ b/crates/engine/src/hidden_target.rs @@ -32,6 +32,7 @@ const GATED: &[&str] = &[ "paint.sharpen", "paint.smudge", "paint.historyBrush", + "paint.artHistoryBrush", "layer.translate", ]; diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rs deleted file mode 100644 index 3e870aa686..0000000000 --- a/crates/engine/src/image_cmds.rs +++ /dev/null @@ -1,1502 +0,0 @@ -//! Image menu: Image Size, Canvas Size, Crop, Trim, Mode (colour model and -//! bit depth) conversions, Duplicate. - -use photocraft_algo::resample::{Resample, crop_surface, resize_surface_in_canvas, translate_surface}; -use photocraft_color::{ColorMode, SampleType}; -use photocraft_doc::{Document, Effect, Effects, FxPaint, Layer, LayerContent, LayerId, Size}; -use photocraft_geom::Rect; -use photocraft_raster::Surface; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} - -/// Applies `f` to every pixel surface of a layer tree: content (raster or -/// cached text/shape/smart pixels), fill caches, and (if `masks`) masks. -pub(crate) fn for_each_surface(layers: &mut [Layer], masks: bool, f: &mut dyn FnMut(&mut Surface, bool)) { - for l in layers { - if masks && let Some(m) = &mut l.mask { - f(&mut m.surface, true); - } - if let Some(fc) = &mut l.fill_cache { - f(&mut fc.surface, false); - } - match &mut l.content { - LayerContent::Raster(s) => f(s, false), - LayerContent::Text(t) => { - if let Some(c) = &mut t.cache { - f(c, false) - } - } - LayerContent::Shape(sh) => { - if let Some(c) = &mut sh.cache { - f(c, false) - } - } - LayerContent::Smart(sm) => { - if let Some(c) = &mut sm.cache { - f(c, false) - } - // Selection-derived smart-filter masks live in document coordinates. Every - // canvas resize/translation must carry them with the smart object's cache; - // otherwise toggling a filter recomputes its coverage at stale positions (#3077). - if masks && let Some(mask) = &mut sm.filter_mask { - f(&mut mask.surface, true); - } - } - LayerContent::Group(g) => for_each_surface(&mut g.children, masks, f), - _ => {} - } - } -} - -/// Converts every pixel surface of a layer tree (masks included) to `depth`. -pub(crate) fn convert_layers_depth(layers: &mut [Layer], depth: SampleType) { - for_each_surface(layers, true, &mut |surf, _| { - let f = surf.format().with_sample(depth); - *surf = surf.convert(f); - }); -} - -pub(crate) fn for_each_layer(layers: &mut [Layer], f: &mut dyn FnMut(&mut Layer)) { - for l in layers { - f(l); - if let Some(ch) = l.children_mut() { - for_each_layer(ch, f); - } - } -} - -/// Assign new layer ids to a copied document and update every saved layer reference. -/// -/// A document copy needs fresh identities, but its comps and variable definitions must -/// still target the copied layers. Deliberately leave references to already-deleted -/// layers untouched so their missing-layer warnings remain meaningful. The same applies -/// to Last Document State. -pub(crate) fn reidentify_copied_layers(doc: &mut Document) { - let mut ids = std::collections::HashMap::new(); - for_each_layer(&mut doc.layers, &mut |layer| { - let previous = layer.id; - layer.id = LayerId::fresh(); - ids.insert(previous, layer.id); - }); - - let remap = |comp: &mut photocraft_doc::LayerComp| { - for state in &mut comp.states { - if let Some(&new_id) = ids.get(&state.layer) { - state.layer = new_id; - } - } - }; - for comp in &mut doc.layer_comps { - remap(comp); - } - if let Some(last) = &mut doc.last_document_state { - remap(last); - } - for def in &mut doc.variables.defs { - if let Some(&new_id) = ids.get(&def.layer) { - def.layer = new_id; - } - } -} - -/// Scale every size and distance of a style by `k` (Image Size's Scale Styles, Layer Style › -/// Scale Effects, and previews that draw a style at a reduced resolution). -pub fn scale_effects(fx: &mut Effects, k: f32) { - for e in &mut fx.items { - match e { - Effect::DropShadow(s) | Effect::InnerShadow(s) => { - s.distance *= k; - s.size *= k; - } - Effect::OuterGlow(g) | Effect::InnerGlow(g) => g.size *= k, - Effect::Stroke(s) => s.size *= k, - Effect::Satin(s) => { - s.distance *= k; - s.size *= k; - } - Effect::BevelEmboss(b) => { - b.size *= k; - b.soften *= k; - } - Effect::PatternOverlay { scale, .. } => *scale *= k, - Effect::GradientOverlay { .. } | Effect::ColorOverlay { .. } => {} - } - if let Effect::Stroke(s) = e - && let FxPaint::Pattern { scale, .. } = &mut s.paint - { - *scale *= k; - } - } - // Effects changed: the preserved PSD block no longer applies. - if k != 1.0 { - fx.psd_raw = None; - } -} - -/// Image Size's resampling methods; `None` changes the resolution only. -#[derive(Clone, Copy)] -enum Method { - None, - Filter(Resample), - /// Photoshop's Automatic: Preserve Details where the image grows, Bicubic Sharper otherwise. - Automatic, -} - -fn parse_resample(s: &str) -> Method { - Method::Filter(match s { - "none" => return Method::None, - "automatic" => return Method::Automatic, - "nearest" | "nearestNeighbor" => Resample::Nearest, - "bilinear" => Resample::Bilinear, - "bicubicSmoother" => Resample::BicubicSmoother, - "bicubicSharper" => Resample::BicubicSharper, - "bicubicAutomatic" => Resample::BicubicAutomatic, - "lanczos" => Resample::Lanczos, - "preserveDetails" => Resample::PreserveDetails, - _ => Resample::Bicubic, - }) -} - -/// The largest raster Image Size resamples to: the decoders' default allocation budget. -const MAX_RESAMPLE_BYTES: u64 = if cfg!(target_pointer_width = "64") { 8 << 30 } else { 2 << 30 }; - -/// Image → Image Size. -fn image_size(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let (ow, oh) = (d.doc.size.width, d.doc.size.height); - let resample = parse_resample(p.get("resample").and_then(Value::as_str).unwrap_or("bicubic")); - let mut pw = p.get("width").and_then(Value::as_f64); - let mut ph = p.get("height").and_then(Value::as_f64); - // `percent` scales both sides (what a Photoshop action records as "200%"); an explicit side wins. - if let Some(pc) = p.get("percent") { - let pc = pc.as_f64().filter(|v| v.is_finite() && *v > 0.0).ok_or_else(|| bad("image.imageSize", "`percent` must be a positive number"))?; - pw = pw.or(Some(ow as f64 * pc / 100.0)); - ph = ph.or(Some(oh as f64 * pc / 100.0)); - } - // `keepPrintSize`: only the resolution given, the pixels follow it (Photoshop's Image Size - // with Resample on, as an action records it). - if p.get("keepPrintSize").and_then(Value::as_bool) == Some(true) - && pw.is_none() - && ph.is_none() - && let Some(r) = p.get("resolution").and_then(Value::as_f64) - { - let r = r.clamp(1.0, 10_000.0); - let old = f64::from(d.doc.resolution_dpi).max(1.0); - pw = Some(ow as f64 * r / old); - ph = Some(oh as f64 * r / old); - } - // Missing dimension keeps the aspect ratio. - let (nw, nh) = match (pw, ph) { - (Some(w), Some(h)) => (w, h), - (Some(w), None) => (w, w * oh as f64 / ow.max(1) as f64), - (None, Some(h)) => (h * ow as f64 / oh.max(1) as f64, h), - (None, None) => (ow as f64, oh as f64), - }; - let (nw, nh) = (nw.round().clamp(1.0, 300_000.0) as u32, nh.round().clamp(1.0, 300_000.0) as u32); - // Each side may reach 300000 px, but resampling writes real pixels: refuse a canvas whose - // raster alone would pass the budget, before allocating any of it (#1544). - if !matches!(resample, Method::None) && (nw, nh) != (ow, oh) { - check_resample_budget("image.imageSize", &d.doc, nw, nh, ", or turn Resample off to change only the resolution")?; - } - let dpi = p.get("resolution").and_then(Value::as_f64).map(|v| v as f32); - if !matches!(resample, Method::None) && (nw, nh) != (ow, oh) { - // Color Sampler points scale with the pixels (same map `transform_geometry` applies). - let (sx, sy) = (f64::from(nw) / f64::from(ow.max(1)), f64::from(nh) / f64::from(oh.max(1))); - s.set_sampler_map(photocraft_geom::Affine { m: [sx, 0.0, 0.0, sy, 0.0, 0.0] }); - } - s.edit("Image Size", |doc, _| { - if let Some(r) = dpi { - doc.resolution_dpi = r.clamp(1.0, 10_000.0); - } - let filter = match resample { - Method::None => return Ok(()), - Method::Filter(f) => f, - Method::Automatic if nw > ow || nh > oh => Resample::PreserveDetails, - Method::Automatic => Resample::BicubicSharper, - }; - resample_doc(doc, nw, nh, filter); - Ok(()) - })?; - // The size the document has now: without resampling `width`/`height` are ignored (#1815). - let size = s.active().ok_or(EngineError::NoDocument)?.doc.size; - Ok(json!({ "width": size.width, "height": size.height })) -} - -/// Refuses to resample `doc` to `nw` x `nh` px when the raster alone would pass the budget, before -/// allocating any of it (#1544). `hint` ends the message with what the caller can do instead. -pub(crate) fn check_resample_budget(cmd: &str, doc: &Document, nw: u32, nh: u32, hint: &str) -> Result<()> { - let bpp = doc.pixel_format().bytes_per_pixel().max(1) as u64; - let bytes = u64::from(nw).saturating_mul(u64::from(nh)).saturating_mul(bpp); - if bytes <= MAX_RESAMPLE_BYTES { - return Ok(()); - } - let mp = u64::from(nw).saturating_mul(u64::from(nh)) / 1_000_000; - let max_mp = MAX_RESAMPLE_BYTES / bpp / 1_000_000; - Err(bad(cmd, format!("{nw} x {nh} px is {mp} megapixels; resampling at this bit depth is limited to {max_mp} megapixels. Choose a smaller size{hint}"))) -} - -/// Resamples the whole document (pixels, masks, channels, selection, effects, vectors, guides) to -/// `nw` x `nh` px with `filter`: the body of Image Size, shared by the Crop tool's W x H x Resolution -/// mode. Callers check [`check_resample_budget`] first. -fn resample_doc(doc: &mut Document, nw: u32, nh: u32, filter: Resample) { - let (ow, oh) = (doc.size.width, doc.size.height); - let (sx, sy) = (nw as f64 / ow.max(1) as f64, nh as f64 / oh.max(1) as f64); - if (sx - 1.0).abs() < 1e-12 && (sy - 1.0).abs() < 1e-12 { - return; - } - // Content that reaches the canvas edge keeps it: a Background stays opaque to the border. - let canvas = Rect::from_xywh(0, 0, ow, oh); - for_each_surface(&mut doc.layers, true, &mut |surf, is_mask| { - *surf = resize_surface_in_canvas(surf, sx, sy, if is_mask { Resample::Bilinear } else { filter }, canvas); - }); - let k = ((sx + sy) / 2.0) as f32; - for_each_layer(&mut doc.layers, &mut |l| scale_effects(&mut l.effects, k)); - for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) { - ch.surface = resize_surface_in_canvas(&ch.surface, sx, sy, Resample::Bilinear, canvas); - } - if let Some(sel) = &doc.selection { - doc.selection = Some(resize_surface_in_canvas(sel, sx, sy, Resample::Bilinear, canvas)); - } - // Vector geometry, guides and marks scale with the pixels; vectors re-render sharp. - crate::canvas_geom::transform_geometry(doc, &photocraft_geom::Affine { m: [sx, 0.0, 0.0, sy, 0.0, 0.0] }); - doc.size = Size::new(nw, nh); - crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::All); -} - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -/// Moves every surface, channel, selection and guide by `(dx, dy)`. -/// -/// Fails, before moving anything, when some pixels would land outside the i32 coordinate -/// range: the surface copy saturates its target rectangle there, which no longer matches -/// the pixel data and panics (#959). -pub(crate) fn translate_doc(doc: &mut Document, cmd: &str, dx: i32, dy: i32) -> Result<()> { - if dx == 0 && dy == 0 { - return Ok(()); - } - let fits = |r: Rect| { - r.is_empty() || (r.x0.checked_add(dx).is_some() && r.x1.checked_add(dx).is_some() && r.y0.checked_add(dy).is_some() && r.y1.checked_add(dy).is_some()) - }; - let mut ok = true; - for_each_surface(&mut doc.layers, true, &mut |surf, _| ok = ok && fits(surf.content_bounds())); - ok = ok - && doc.channels.iter().chain(doc.quick_mask.as_ref()).all(|ch| fits(ch.surface.content_bounds())) - && doc.selection.as_ref().is_none_or(|sel| fits(sel.content_bounds())); - if !ok { - return Err(bad(cmd, format!("moving the document by ({dx}, {dy}) would push its pixels outside the 32-bit coordinate range"))); - } - for_each_surface(&mut doc.layers, true, &mut |surf, _| *surf = translate_surface(surf, dx, dy)); - for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) { - ch.surface = translate_surface(&ch.surface, dx, dy); - } - if let Some(sel) = &doc.selection { - doc.selection = Some(translate_surface(sel, dx, dy)); - } - // Type, shapes, smart objects, vector masks, paths, guides, slices, notes… (caches above - // are already translated exactly, so nothing needs re-rendering for the move itself). - crate::canvas_geom::transform_geometry(doc, &photocraft_geom::Affine::translate(f64::from(dx), f64::from(dy))); - Ok(()) -} - -/// Crops the document to `r` (in current document coordinates). -/// A frame that extends past the canvas fills the Background's new area with `extension` (the -/// background colour, as Photoshop's Crop tool does); other layers get transparent pixels there. -fn crop_doc(doc: &mut Document, cmd: &str, r: Rect, delete_pixels: bool, extension: [f32; 4]) -> Result<()> { - // The origin moves to (0, 0); `-i32::MIN` has no i32 value (#959). - let (Some(dx), Some(dy)) = (r.x0.checked_neg(), r.y0.checked_neg()) else { - return Err(bad(cmd, format!("the crop origin ({}, {}) can't be moved to (0, 0) within the 32-bit coordinate range", r.x0, r.y0))); - }; - let old_canvas = doc.bounds(); - let extends = r.x0 < old_canvas.x0 || r.y0 < old_canvas.y0 || r.x1 > old_canvas.x1 || r.y1 > old_canvas.y1; - if delete_pixels { - for_each_surface(&mut doc.layers, false, &mut |surf, _| *surf = crop_surface(surf, r)); - } - translate_doc(doc, cmd, dx, dy)?; - doc.size = Size::new(r.width(), r.height()); - // A Background cannot hold transparent pixels: like an opaque Canvas Size extension, the area - // an extending Crop adds is filled with the extension (background) colour. - if extends - && let Some(bg) = doc.background_mut() - && let LayerContent::Raster(surf) = &mut bg.content - { - let old = old_canvas.translate(dx, dy); - let fmt = surf.format(); - let px = photocraft_raster::from_rgba(&fmt, [extension[0], extension[1], extension[2], 1.0]); - let keep = surf.content_bounds().intersect(&old); - let saved = surf.to_interleaved(keep); - surf.fill_rect(Rect::from_xywh(0, 0, r.width(), r.height()), &px); - if !keep.is_empty() { - surf.write_interleaved(keep, &saved); - } - } - crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes); - Ok(()) -} - -/// The nine documented canvas-size anchors, in the order the command lists them, each mapped to -/// its (x, y) factor (0.0 = top/left edge, 0.5 = centre, 1.0 = bottom/right edge). -const ANCHORS: [(&str, (f64, f64)); 9] = [ - ("topLeft", (0.0, 0.0)), - ("top", (0.5, 0.0)), - ("topRight", (1.0, 0.0)), - ("left", (0.0, 0.5)), - ("center", (0.5, 0.5)), - ("right", (1.0, 0.5)), - ("bottomLeft", (0.0, 1.0)), - ("bottom", (0.5, 1.0)), - ("bottomRight", (1.0, 1.0)), -]; - -/// Resolves an anchor name to its (x, y) factors, matching the nine documented values exactly. -/// Anything else is an error naming `anchor` and listing them, rather than resolving the two axes -/// independently (which let a misspelling like `"topleft"` land between two valid anchors, #2946). -fn anchor_factors(cmd: &str, a: &str) -> Result<(f64, f64)> { - ANCHORS.iter().find(|(name, _)| *name == a).map(|&(_, f)| f).ok_or_else(|| { - let names = ANCHORS.iter().map(|&(n, _)| n).collect::>().join("|"); - bad(cmd, format!("`anchor` must be one of {names}")) - }) -} - -fn extension_color(s: &Session, p: &Value) -> [f32; 4] { - match p.get("extensionColor").and_then(Value::as_str).unwrap_or("background") { - "foreground" => s.tools.foreground, - "white" => [1.0; 4], - "black" => [0.0, 0.0, 0.0, 1.0], - "transparent" => [0.0; 4], - hex if hex.starts_with('#') => { - let h = hex.trim_start_matches('#'); - let c = |i: usize| h.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map_or(0.0, |v| f32::from(v) / 255.0); - [c(0), c(2), c(4), 1.0] - } - _ => s.tools.background, - } -} - -/// Image → Canvas Size. -fn canvas_size(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let (ow, oh) = (d.doc.size.width as f64, d.doc.size.height as f64); - let relative = p.get("relative").and_then(Value::as_bool).unwrap_or(false); - let w = p.get("width").and_then(Value::as_f64).unwrap_or(if relative { 0.0 } else { ow }); - let h = p.get("height").and_then(Value::as_f64).unwrap_or(if relative { 0.0 } else { oh }); - let (nw, nh) = if relative { (ow + w, oh + h) } else { (w, h) }; - let (nw, nh) = (nw.round().clamp(1.0, 300_000.0) as u32, nh.round().clamp(1.0, 300_000.0) as u32); - let (ax, ay) = anchor_factors("image.canvasSize", p.get("anchor").and_then(Value::as_str).unwrap_or("center"))?; - let dx = ((nw as f64 - ow) * ax).round() as i32; - let dy = ((nh as f64 - oh) * ay).round() as i32; - let ext = extension_color(s, p); - s.set_sampler_map(photocraft_geom::Affine::translate(f64::from(dx), f64::from(dy))); - s.edit("Canvas Size", |doc, _| { - translate_doc(doc, "image.canvasSize", dx, dy)?; - doc.size = Size::new(nw, nh); - crate::canvas_geom::refresh(doc, crate::canvas_geom::Refresh::Shapes); - let canvas = doc.bounds(); - let old = Rect::from_xywh(dx, dy, ow as u32, oh as u32); - // A Background cannot contain transparent pixels. When a transparent - // extension adds canvas area, make it a normal paintable layer first. - // A shrink or no-op resize must not silently convert the Background. - if ext[3] == 0.0 - && (nw > ow as u32 || nh > oh as u32) - && let Some(bg) = doc.background_mut() - { - crate::extra_cmds::unlock_background(bg); - } - // The locked Background layer is extended with the extension colour. - if ext[3] > 0.0 - && let Some(bg) = doc.background_mut() - && let LayerContent::Raster(surf) = &mut bg.content - { - let fmt = surf.format(); - let px = photocraft_raster::from_rgba(&fmt, ext); - let keep = surf.content_bounds().intersect(&old); - let saved = surf.to_interleaved(keep); - surf.fill_rect(canvas, &px); - if !keep.is_empty() { - surf.write_interleaved(keep, &saved); - } - } - Ok(()) - })?; - Ok(json!({ "width": nw, "height": nh, "offset": [dx, dy] })) -} - -/// The largest side an explicit crop may give the canvas (the `file.new` and Canvas Size limit). -const MAX_CROP_SIDE: i32 = 300_000; - -/// Image → Crop (to the selection bounds). -fn crop(s: &mut Session, p: &Value) -> Result { - let delete = p.get("deleteCroppedPixels").and_then(Value::as_bool).unwrap_or(true); - // The Crop tool's Fill: Content-Aware Fill of what the crop uncovers (#3823). - let content_aware = match p.get("contentAware") { - None | Some(Value::Null) => false, - Some(Value::Bool(b)) => *b, - Some(v) => return Err(bad("image.crop", format!("`contentAware` must be true or false, not {v}"))), - }; - // Values beyond i32 would wrap through the narrowing casts into a rectangle unrelated to - // the numbers passed; reject them (see `commands::int_i32`). - let explicit = match ( - crate::commands::int_i32("image.crop", p, "x")?, - crate::commands::int_i32("image.crop", p, "y")?, - crate::commands::int_i32("image.crop", p, "width")?, - crate::commands::int_i32("image.crop", p, "height")?, - ) { - (Some(x), Some(y), Some(w), Some(h)) if w > 0 && h > 0 => { - // The new canvas size: refuse what File › New, Image Size and Canvas Size won't make, - // before anything changes. Unbounded, a following full-canvas flatten (Trim, Duplicate - // Merged) aborted on allocation (#960). - if w > MAX_CROP_SIDE || h > MAX_CROP_SIDE { - return Err(bad("image.crop", format!("{w} x {h} exceeds the {MAX_CROP_SIDE} px limit per side"))); - } - // A far edge past i32::MAX used to saturate, silently cropping less than asked (#959). - let end = |o: i32, len: i32, ko: &str, kl: &str| { - o.checked_add(len) - .ok_or_else(|| bad("image.crop", format!("`{ko}` + `{kl}` = {} is outside the 32-bit coordinate range", i64::from(o) + i64::from(len)))) - }; - Some(Rect::new(x, y, end(x, w, "x", "width")?, end(y, h, "y", "height")?)) - } - _ => None, - }; - let target = crop_target(p)?; - if let Some((tw, th)) = target.size { - let d = s.active().ok_or(EngineError::NoDocument)?; - check_resample_budget("image.crop", &d.doc, tw, th, "")?; - } - if let Some(a) = crop_angle(p)? { - let rect = explicit.ok_or_else(|| bad("image.crop", "an `angle` needs an explicit `x`, `y`, `width` and `height`"))?; - return rotated_crop(s, rect, a, delete, content_aware, &target); - } - // An explicit rectangle (the Crop tool) may extend past the canvas; a selection crop is clamped. - let r = explicit - .or_else(|| s.active().and_then(|d| d.doc.selection.as_ref().map(|sel| sel.content_bounds().intersect(&d.doc.bounds())))) - .unwrap_or(Rect::EMPTY); - if r.is_empty() { - return Err(EngineError::Other("nothing to crop: pass x/y/width/height or make a selection".into())); - } - s.set_sampler_map(photocraft_geom::Affine::translate(-f64::from(r.x0), -f64::from(r.y0))); - let extension = s.tools.background; - s.edit("Crop", |doc, active| { - let original = doc.size; - crop_doc(doc, "image.crop", r, delete, extension)?; - if content_aware { - crate::crop_fill::fill_uncovered(doc, *active, &crate::crop_fill::Coverage::crop(r, original.width, original.height))?; - } - doc.selection = None; - target.apply(doc); - Ok(()) - })?; - Ok(crop_result(s, json!({ "x": r.x0, "y": r.y0, "width": r.width(), "height": r.height() }))) -} - -/// The Crop tool's W x H x Resolution: the size (px) the cropped area is resampled to and the -/// resolution the document gets, both optional. -struct CropTarget { - size: Option<(u32, u32)>, - resolution: Option, - filter: Resample, -} - -impl CropTarget { - /// Applies the target to a document that has just been cropped (inside the crop's history step). - fn apply(&self, doc: &mut Document) { - if let Some((w, h)) = self.size { - resample_doc(doc, w, h, self.filter); - } - if let Some(r) = self.resolution { - doc.resolution_dpi = r; - } - } -} - -/// The highest resolution (ppi) a crop may set, as for Image Size. -const MAX_CROP_RESOLUTION: f64 = 10_000.0; - -/// `image.crop`'s optional `targetWidth`/`targetHeight` (whole px, both or neither), `resolution` -/// (ppi) and `resample` (as for `image.imageSize`; "none" is refused with a target size). -fn crop_target(p: &Value) -> Result { - const CMD: &str = "image.crop"; - let side = |key: &str| -> Result> { - match p.get(key) { - None | Some(Value::Null) => Ok(None), - Some(v) => v - .as_u64() - .and_then(|n| u32::try_from(n).ok()) - .filter(|n| (1..=MAX_CROP_SIDE as u32).contains(n)) - .map(Some) - .ok_or_else(|| bad(CMD, format!("`{key}` = {v} must be a whole number of pixels from 1 to {MAX_CROP_SIDE}"))), - } - }; - let size = match (side("targetWidth")?, side("targetHeight")?) { - (Some(w), Some(h)) => Some((w, h)), - (None, None) => None, - _ => return Err(bad(CMD, "`targetWidth` and `targetHeight` go together")), - }; - let resolution = match p.get("resolution") { - None | Some(Value::Null) => None, - Some(v) => { - let r = v.as_f64().filter(|r| r.is_finite() && (1.0..=MAX_CROP_RESOLUTION).contains(r)); - Some(r.ok_or_else(|| bad(CMD, format!("`resolution` = {v} must be a number of pixels per inch from 1 to {MAX_CROP_RESOLUTION}")))? as f32) - } - }; - let filter = match p.get("resample") { - None | Some(Value::Null) => Some(Resample::Bicubic), - Some(v) => match parse_resample(v.as_str().ok_or_else(|| bad(CMD, "`resample` must be a string"))?) { - Method::None => None, - Method::Filter(f) => Some(f), - // The direction is known only once the frame is: bicubic's own automatic choice. - Method::Automatic => Some(Resample::BicubicAutomatic), - }, - }; - let filter = match (filter, size) { - (Some(f), _) => f, - (None, None) => Resample::Bicubic, - (None, Some(_)) => return Err(bad(CMD, "a `targetWidth` x `targetHeight` crop needs resampling: `resample` can't be \"none\"")), - }; - Ok(CropTarget { size, resolution, filter }) -} - -/// A crop's result: the frame it cropped to, plus the document's size and resolution afterwards. -fn crop_result(s: &Session, mut frame: Value) -> Value { - if let (Some(d), Some(o)) = (s.active(), frame.as_object_mut()) { - o.insert("documentWidth".into(), json!(d.doc.size.width)); - o.insert("documentHeight".into(), json!(d.doc.size.height)); - o.insert("resolution".into(), json!(d.doc.resolution_dpi)); - } - frame -} - -/// The largest crop angle accepted (degrees), as for Image Rotation › Arbitrary. -const MAX_CROP_ANGLE: f64 = 3600.0; - -/// `image.crop`'s optional `angle` in degrees, normalised to (-180, 180]; `None` when it is absent, -/// null or (after normalising) zero, so an unrotated crop takes the plain, resampling-free path. -fn crop_angle(p: &Value) -> Result> { - let a = match p.get("angle") { - None | Some(Value::Null) => return Ok(None), - Some(v) => v.as_f64().ok_or_else(|| bad("image.crop", "`angle` must be a number of degrees"))?, - }; - if !a.is_finite() || a.abs() > MAX_CROP_ANGLE { - return Err(bad("image.crop", format!("`angle` must be a finite number of degrees within ±{MAX_CROP_ANGLE}"))); - } - let a = a.rem_euclid(360.0); - let a = if a > 180.0 { a - 360.0 } else { a }; - Ok((a.abs() >= 1e-9).then_some(a)) -} - -/// The Crop tool's rotated frame: `r` (document px) turned clockwise on screen by `angle` degrees -/// about its centre. The document is rotated counter-clockwise by `angle` (Image Rotation › -/// Arbitrary: the canvas grows to fit, the Background fills what the rotation uncovers with the -/// background colour, other layers keep transparency there and every pixel of theirs), which makes -/// the frame upright, then cropped to it (`deleteCroppedPixels` as for an unrotated crop), all in -/// one undo step. With `content_aware`, the corners the rotation uncovers (and any area past the -/// rotated image) are then filled from their surroundings, in the same step (#3823). -fn rotated_crop(s: &mut Session, r: Rect, angle: f64, delete: bool, content_aware: bool, crop_target: &CropTarget) -> Result { - const CMD: &str = "image.crop"; - let size = s.active().ok_or(EngineError::NoDocument)?.doc.size; - let (w, h) = (f64::from(r.width()), f64::from(r.height())); - let (cx, cy) = (f64::from(r.x0) + w / 2.0, f64::from(r.y0) + h / 2.0); - // Where the frame's centre lands: the rotation `image.rotation.arbitrary` applies, clockwise - // by `-angle` (x' = c·x − s·y, y' = s·x + c·y) about the old and new canvas centres. - let new = crate::mode_cmds::rotated_size(size, -angle); - let (sn, cs) = (-angle).to_radians().sin_cos(); - let (dx, dy) = (cx - f64::from(size.width) / 2.0, cy - f64::from(size.height) / 2.0); - let (ncx, ncy) = (f64::from(new.width) / 2.0 + cs * dx - sn * dy, f64::from(new.height) / 2.0 + sn * dx + cs * dy); - let (x, y) = ((ncx - w / 2.0).round(), (ncy - h / 2.0).round()); - let far = || bad(CMD, "the rotated frame is outside the 32-bit coordinate range"); - let limit = f64::from(i32::MAX / 2); - if !(x.is_finite() && y.is_finite()) || x.abs() > limit || y.abs() > limit { - return Err(far()); - } - let (x, y) = (x as i32, y as i32); - let end = |o: i32, len: u32| i32::try_from(len).ok().and_then(|l| o.checked_add(l)); - let (Some(x1), Some(y1)) = (end(x, r.width()), end(y, r.height())) else { return Err(far()) }; - let target = Rect::new(x, y, x1, y1); - // The cropped canvas back to the original document: undo the crop's offset, then the rotation - // (x = c·(x' − nx) + s·(y' − ny) + cx, y = −s·(x' − nx) + c·(y' − ny) + cy). - let (ox, oy) = (f64::from(target.x0) - f64::from(new.width) / 2.0, f64::from(target.y0) - f64::from(new.height) / 2.0); - let (ocx, ocy) = (f64::from(size.width) / 2.0, f64::from(size.height) / 2.0); - let coverage = crate::crop_fill::Coverage { - map: [cs, sn, -sn, cs, cs * ox + sn * oy + ocx, -sn * ox + cs * oy + ocy], - width: f64::from(size.width), - height: f64::from(size.height), - rotated: true, - }; - crate::analysis_cmds::compound(s, "Crop", |s| { - crate::mode_cmds::rotate_arbitrary(s, &json!({"angle": angle, "direction": "ccw"}))?; - // Color Sampler points follow the crop's origin shift too (the rotation moved them above). - s.set_sampler_map(photocraft_geom::Affine::translate(-f64::from(target.x0), -f64::from(target.y0))); - let extension = s.tools.background; - s.edit("Crop", |doc, active| { - crop_doc(doc, CMD, target, delete, extension)?; - if content_aware { - crate::crop_fill::fill_uncovered(doc, *active, &coverage)?; - } - doc.selection = None; - crop_target.apply(doc); - Ok(()) - }) - })?; - Ok(crop_result(s, json!({ "x": r.x0, "y": r.y0, "width": r.width(), "height": r.height(), "angle": angle }))) -} - -/// Image → Trim. -fn trim(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let canvas = d.doc.bounds(); - let buf = photocraft_compose::flatten(&d.doc); - let based = p.get("basedOn").and_then(Value::as_str).unwrap_or("transparent"); - let w = canvas.width() as usize; - let reference = match based { - "topLeft" => Some(buf.px[0]), - "bottomRight" => buf.px.last().copied(), - _ => None, - }; - let differs = |q: [f32; 4]| match reference { - None => q[3] > 0.0, - Some(r) => (0..4).any(|c| (q[c] * q[3] - r[c] * r[3]).abs() > 0.5 / 255.0 || (c == 3 && (q[3] - r[3]).abs() > 0.5 / 255.0)), - }; - let mut b = Rect::EMPTY; - for (i, q) in buf.px.iter().enumerate() { - if differs(*q) { - let (x, y) = ((i % w) as i32, (i / w) as i32); - b = if b.is_empty() { Rect::new(x, y, x + 1, y + 1) } else { b.union(&Rect::new(x, y, x + 1, y + 1)) }; - } - } - if b.is_empty() { - return Err(EngineError::Other("nothing to trim to".into())); - } - let side = |k: &str| p.get(k).and_then(Value::as_bool).unwrap_or(true); - let r = Rect::new( - if side("left") { b.x0 } else { canvas.x0 }, - if side("top") { b.y0 } else { canvas.y0 }, - if side("right") { b.x1 } else { canvas.x1 }, - if side("bottom") { b.y1 } else { canvas.y1 }, - ); - s.set_sampler_map(photocraft_geom::Affine::translate(-f64::from(r.x0), -f64::from(r.y0))); - let extension = s.tools.background; - s.edit("Trim", |doc, _| crop_doc(doc, "image.trim", r, true, extension))?; - Ok(json!({ "x": r.x0, "y": r.y0, "width": r.width(), "height": r.height() })) -} - -/// Image → Mode → colour model, through the CMS (see [`crate::color_cmds::convert_mode`]). -fn convert_mode(s: &mut Session, mode: ColorMode, p: &Value) -> Result { - crate::color_cmds::convert_mode(s, mode, p) -} - -/// Image → Mode → 8/16/32 Bits/Channel. -fn convert_depth(s: &mut Session, depth: SampleType) -> Result { - let current = s.active().ok_or(EngineError::NoDocument)?.doc.depth; - if current == depth { - // History::record clears redo even when an edit closure leaves the document unchanged. - return Ok(Value::Null); - } - s.edit("Bit Depth", |doc, _| { - // 32-bit documents hold linear light (as Photoshop's do: grey 128 becomes 0.2159, measured - // in Photoshop 27.11), so the colour samples cross the document's tone curve when the - // depth crosses 32 bits: decoded after widening, encoded before narrowing (so no linear - // value is quantised to 8 or 16 bits). Masks, alpha and channels are not colour. - let crosses = (current == SampleType::F32) != (depth == SampleType::F32) && matches!(doc.mode, ColorMode::Rgb | ColorMode::Grayscale); - let to_linear = depth == SampleType::F32; - let transfer = photocraft_compose::adjust::Transfer::for_document(doc.mode, if to_linear { current } else { depth }); - if crosses && !to_linear { - for_each_surface(&mut doc.layers, false, &mut |surf, _| surf.map_color(|v| transfer.encode(v).clamp(0.0, 1.0))); - } - convert_layers_depth(&mut doc.layers, depth); - if crosses && to_linear { - for_each_surface(&mut doc.layers, false, &mut |surf, _| surf.map_color(|v| transfer.decode(v))); - } - for ch in doc.channels.iter_mut().chain(doc.quick_mask.as_mut()) { - let f = ch.surface.format().with_sample(depth); - ch.surface = ch.surface.convert(f); - } - doc.depth = depth; - Ok(()) - })?; - Ok(Value::Null) -} - -/// Image → Duplicate. -fn duplicate(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let mut doc = (*d.doc).clone(); - doc.id = photocraft_doc::DocId::fresh(); - doc.name = p.get("name").and_then(Value::as_str).map(str::to_string).unwrap_or_else(|| format!("{} copy", d.doc.name)); - if p.get("mergedOnly").and_then(Value::as_bool).unwrap_or(false) { - let buf = photocraft_compose::flatten(&doc); - let fmt = doc.pixel_format(); - let mut surf = Surface::new(fmt); - let vals: Vec = buf.px.iter().flat_map(|q| photocraft_raster::from_rgba(&fmt, *q)).collect(); - surf.write_region(doc.bounds(), &vals); - surf.prune(); - doc.layers = vec![Layer::new("Background", LayerContent::Raster(surf))]; - // A merged-only copy has no original layers for saved comps to restore. - // Don't carry dangling comp state into a single-layer flattened document. - doc.layer_comps.clear(); - doc.last_applied_comp = None; - doc.last_document_state = None; - // Likewise, variables drove template layers that no longer exist: drop the whole - // binding set (defs, data sets and the active index) on the flattened copy. - doc.variables = photocraft_doc::Variables::default(); - } - reidentify_copied_layers(&mut doc); - let name = doc.name.clone(); - let index = s.add_document(doc, None); - Ok(json!({ "document": index, "name": name })) -} - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $params:literal, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: None, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -/// Image menu command specs. -pub fn specs() -> Vec { - vec![ - spec!( - "image.imageSize", - "Image Size…", - ["Image"], - r##"{"width":px,"height":px,"percent":%? (both sides; an explicit side wins),"resolution":ppi,"keepPrintSize":bool=false (with only a resolution: resample the pixels to keep the print size),"resample":"automatic|preserveDetails|bicubicSmoother|bicubicSharper|bicubicAutomatic|bicubic|nearest|bilinear|lanczos|none"="bicubic"} → {width,height} (the document's size after the command; "none" changes the resolution only)"##, - has_doc, - image_size - ), - spec!( - "image.canvasSize", - "Canvas Size…", - ["Image"], - r##"{"width":px,"height":px,"relative":bool=false,"anchor":"topLeft|top|topRight|left|center|right|bottomLeft|bottom|bottomRight"="center","extensionColor":"background|foreground|white|black|transparent|#rrggbb"="background"}"##, - has_doc, - canvas_size - ), - spec!( - "image.crop", - "Crop", - ["Image"], - r##"{"x":px,"y":px,"width":px,"height":px,"angle":-3600..3600=0,"deleteCroppedPixels":bool=true,"contentAware":bool=false,"targetWidth":px,"targetHeight":px,"resolution":ppi,"resample":"bicubic|bilinear|nearest|lanczos|preserveDetails"="bicubic"} → {x,y,width,height,documentWidth,documentHeight,resolution} (targetWidth/targetHeight, both or neither: the cropped area is resampled to exactly that size, as Image Size, in the same undo step; resolution: the document's new ppi; contentAware: the Crop tool's Fill › Content-Aware Fill, the area a rotated or extending crop uncovers filled from its surroundings on the Background, else the active pixel layer)"##, - has_doc, - crop - ), - spec!( - "image.trim", - "Trim…", - ["Image"], - r##"{"basedOn":"transparent|topLeft|bottomRight"="transparent","top":bool=true,"bottom":bool=true,"left":bool=true,"right":bool=true}"##, - has_doc, - trim - ), - spec!( - "image.mode.rgb", - "RGB Color", - ["Image", "Mode"], - r##"{"profile":"|working|/path/to/profile.icc"=working,"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true}"##, - has_doc, - |s, p| convert_mode(s, ColorMode::Rgb, p) - ), - spec!( - "image.mode.grayscale", - "Grayscale", - ["Image", "Mode"], - r##"{"profile":"|working|/path/to/profile.icc"=working,"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true}"##, - has_doc, - |s, p| convert_mode(s, ColorMode::Grayscale, p) - ), - spec!( - "image.mode.cmyk", - "CMYK Color", - ["Image", "Mode"], - r##"{"profile":"|working|/path/to/profile.icc"=working,"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true}"##, - has_doc, - |s, p| convert_mode(s, ColorMode::Cmyk, p) - ), - spec!( - "image.mode.lab", - "Lab Color", - ["Image", "Mode"], - r##"{"profile":"|working|/path/to/profile.icc"=working,"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true}"##, - has_doc, - |s, p| convert_mode(s, ColorMode::Lab, p) - ), - spec!("image.mode.bits8", "8 Bits/Channel", ["Image", "Mode"], "{}", has_doc, |s, _| convert_depth(s, SampleType::U8)), - spec!("image.mode.bits16", "16 Bits/Channel", ["Image", "Mode"], "{}", has_doc, |s, _| convert_depth(s, SampleType::U16)), - spec!("image.mode.bits32", "32 Bits/Channel", ["Image", "Mode"], "{}", has_doc, |s, _| convert_depth(s, SampleType::F32)), - spec!("image.duplicate", "Duplicate…", ["Image"], r##"{"name":str,"mergedOnly":bool=false}"##, has_doc, duplicate), - ] -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn crop_rejects_rectangles_that_would_wrap() { - let mut s = session(); - // 2^32 + 100 wrapped to `x = 100` through the narrowing casts: a crop rectangle - // somewhere unrelated to the numbers passed. - let err = s.execute("image.crop", json!({"x": 4_294_967_396_i64, "y": 0, "width": 10, "height": 10})).unwrap_err(); - assert!(err.to_string().contains("32-bit"), "{err}"); - // In-range rectangles past the canvas still work (clamped by the crop itself). - s.execute("image.crop", json!({"x": -10, "y": -10, "width": 1000, "height": 1000})).unwrap(); - } - - /// A crop whose translation to the origin can't be represented in i32 is rejected before - /// anything changes (#959): `-i32::MIN` overflowed the negation in `crop_doc`, `x + width` - /// past i32::MAX silently shortened the crop, and an origin one past i32::MIN moved the - /// layer pixels beyond i32::MAX (a length-mismatch panic while translating surfaces). - #[test] - fn crop_rejects_origins_that_cannot_move_to_zero() { - const MIN: i64 = i32::MIN as i64; - const MAX: i64 = i32::MAX as i64; - let cases = [ - json!({"x": MIN, "y": 0, "width": 1, "height": 1}), - json!({"x": 0, "y": MIN, "width": 1, "height": 1}), - json!({"x": MIN, "y": MIN, "width": 1, "height": 1}), - json!({"x": MIN, "y": 0, "width": 1, "height": 1, "deleteCroppedPixels": false}), - json!({"x": MAX - 5, "y": 0, "width": 10, "height": 1}), - json!({"x": 0, "y": MAX - 5, "width": 1, "height": 10}), - json!({"x": MIN + 1, "y": 0, "width": 1, "height": 1, "deleteCroppedPixels": false}), - json!({"x": 0, "y": MIN + 1, "width": 1, "height": 1, "deleteCroppedPixels": false}), - ]; - for p in cases { - let mut s = session(); - let before = s.active().unwrap().doc.clone(); - let (rev, steps) = (s.active().unwrap().revision, s.active().unwrap().history.entries().len()); - let err = s.execute("image.crop", p.clone()).unwrap_err(); - assert!(matches!(err, EngineError::BadParams { .. }), "{p}: {err}"); - assert!(err.to_string().contains("32-bit"), "{p}: {err}"); - let st = s.active().unwrap(); - assert!(std::sync::Arc::ptr_eq(&st.doc, &before), "{p}: document changed"); - assert_eq!(st.doc.size, Size::new(40, 20), "{p}"); - assert_eq!(st.doc.layers[1].surface().unwrap().content_bounds(), Rect::new(10, 5, 20, 15), "{p}"); - assert_eq!((st.revision, st.history.entries().len()), (rev, steps), "{p}: history step recorded"); - } - // Control: a legal negative origin past the canvas still crops (and extends) the canvas. - let mut s = session(); - s.execute("image.crop", json!({"x": -10, "y": -5, "width": 60, "height": 30})).unwrap(); - assert_eq!(doc(&s).size, Size::new(60, 30)); - assert_eq!(doc(&s).layers[1].surface().unwrap().pixel(20, 10), vec![1.0, 0.0, 0.0, 1.0]); - // A far-off crop that deletes the (non-overlapping) pixels has nothing left to move. - let mut s = session(); - s.execute("image.crop", json!({"x": MIN + 1, "y": 0, "width": 1, "height": 1})).unwrap(); - assert_eq!(doc(&s).size, Size::new(1, 1)); - } - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 20})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - l.surface_mut().unwrap().fill_rect(Rect::new(10, 5, 20, 15), &[1.0, 0.0, 0.0, 1.0]); - l.mask = Some(photocraft_doc::LayerMask::reveal_all()); - l.effects.items.push(Effect::default_drop_shadow()); - Ok(()) - }) - .unwrap(); - s - } - - fn doc(s: &Session) -> &Document { - &s.active().unwrap().doc - } - - /// Resample on with only a new resolution keeps the print size, as Photoshop's Image Size - /// does (an action's `imageSize` with `Rslt` alone); without `keepPrintSize` only the - /// resolution changes. - #[test] - fn image_size_keep_print_size_resamples_to_the_resolution() { - let mut s = session(); - s.execute("image.imageSize", json!({"resolution": 144, "resample": "bicubic"})).unwrap(); - assert_eq!((doc(&s).size, doc(&s).resolution_dpi), (Size::new(40, 20), 144.0)); - s.execute("image.imageSize", json!({"resolution": 288, "resample": "bicubic", "keepPrintSize": true})).unwrap(); - assert_eq!((doc(&s).size, doc(&s).resolution_dpi), (Size::new(80, 40), 288.0)); - // An explicit side still wins. - s.execute("image.imageSize", json!({"width": 20, "resolution": 72, "resample": "bicubic", "keepPrintSize": true})).unwrap(); - assert_eq!(doc(&s).size, Size::new(20, 10)); - } - - #[test] - fn image_size_scales_everything() { - for resample in ["bicubic", "bicubicSmoother", "bicubicSharper", "bicubicAutomatic", "automatic", "bilinear", "nearest", "lanczos", "preserveDetails"] { - let mut s = session(); - s.execute("image.imageSize", json!({"width": 80, "resample": resample})).unwrap(); - let d = doc(&s); - assert_eq!(d.size, Size::new(80, 40), "{resample}"); - let l = &d.layers[1]; - let b = l.surface().unwrap().content_bounds(); - assert!((b.x0 - 20).abs() <= 1 && (b.x1 - 40).abs() <= 1, "{resample} {b:?}"); - let Effect::DropShadow(sh) = &l.effects.items[0] else { panic!() }; - assert_eq!(sh.distance, 10.0); - assert_eq!(l.mask.as_ref().unwrap().surface.default_pixel(), vec![1.0]); - } - } - - /// The canvas border stays opaque after Image Size (it used to fade into transparency, so a - /// Background or a 200 % export got a translucent frame), and a full selection stays full. - /// #1544: 64 x 48 to 300000 x 300000 (90 billion pixels) started allocating resampled bands - /// without a budget. It is refused before any allocation and the document is untouched; a - /// change of resolution only, and an ordinary resize, still work. - #[test] - fn image_size_refuses_a_raster_past_the_budget() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); - let before = (doc(&s).size, s.active().unwrap().history.past_len()); - let err = s.execute("image.imageSize", json!({"width": 300_000, "height": 300_000})).unwrap_err().to_string(); - assert!(err.contains("megapixels"), "{err}"); - assert_eq!((doc(&s).size, s.active().unwrap().history.past_len()), before); - s.execute("image.imageSize", json!({"width": 300_000, "height": 300_000, "resample": "none", "resolution": 300})).unwrap(); - s.execute("image.imageSize", json!({"width": 128, "height": 96})).unwrap(); - assert_eq!(doc(&s).size, Size::new(128, 96)); - } - - #[test] - fn image_size_keeps_canvas_edges_opaque() { - for depth in [8, 16, 32] { - for (w, resample) in [ - (60, "bicubic"), - (41, "lanczos"), - (15, "bilinear"), - (77, "preserveDetails"), - (21, "bicubicSharper"), - (47, "automatic"), - (9, "bicubicAutomatic"), - ] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 30, "height": 20, "depth": depth, "background": "#336699"})).unwrap(); - s.execute("select.all", json!({})).unwrap(); - s.execute("image.imageSize", json!({"width": w, "resample": resample})).unwrap(); - let d = doc(&s); - let flat = photocraft_compose::flatten(d); - let (w, h) = (flat.rect.width() as usize, flat.rect.height() as usize); - let mid = flat.px[(h / 2) * w + w / 2]; - assert!((mid[3] - 1.0).abs() < 1e-3, "{depth} {resample}: {mid:?}"); - for p in &flat.px { - let off = p.iter().zip(mid).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); - assert!(off < 0.01, "{depth} {resample} {w}: {p:?} vs {mid:?}"); - } - let sel = d.selection.as_ref().unwrap(); - let (x1, y1) = (d.size.width as i32 - 1, d.size.height as i32 - 1); - for (x, y) in [(0, 0), (x1, 0), (0, y1), (x1, y1)] { - assert!(sel.pixel(x, y)[0] > 0.99, "{depth} {resample}: selection at ({x},{y})"); - } - } - } - } - - #[test] - fn resolution_only_change() { - let mut s = session(); - let r = s.execute("image.imageSize", json!({"resolution": 300, "resample": "none", "width": 999})).unwrap(); - assert_eq!(doc(&s).size, Size::new(40, 20)); - assert_eq!(doc(&s).resolution_dpi, 300.0); - // #1815: the result is the size the document has, not the ignored request. - assert_eq!(r, json!({"width": 40, "height": 20})); - } - - #[test] - fn image_size_reports_the_size_it_applied() { - // #1815: every path reports the document's size afterwards, at every depth. - for depth in [8, 16, 32] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth})).unwrap(); - let size = |s: &Session| json!({"width": doc(s).size.width, "height": doc(s).size.height}); - for p in [ - json!({"width": 999, "height": 777, "resample": "none", "resolution": 150}), - json!({"width": 32}), - json!({"height": 96, "resample": "nearest"}), - json!({"width": 96, "height": 72, "resample": "none"}), - json!({}), - ] { - let r = s.execute("image.imageSize", p.clone()).unwrap(); - assert_eq!(r, size(&s), "{depth}-bit {p}"); - } - // 64×48 → (none) → 32×24 → 128×96 (aspect kept) → (none) → unchanged. - assert_eq!(doc(&s).size, Size::new(128, 96), "{depth}-bit"); - } - } - - /// #1114: nearest neighbor reduction keeps source values without averaging. - #[test] - fn nearest_neighbor_reduction_keeps_source_values() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); - s.execute("tools.setColors", json!({"foreground": "#000000", "background": "#ffffff"})).unwrap(); - for y in 0..8 { - for x in 0..8 { - if (x + y) % 2 == 0 { - s.execute("select.rect", json!({"x": x, "y": y, "width": 1, "height": 1})).unwrap(); - s.execute("edit.fill", json!({"contents": "foreground"})).unwrap(); - } - } - } - s.execute("select.deselect", json!({})).unwrap(); - let st = s.active().unwrap(); - let before = st.doc.layers[0].surface().unwrap().rgba(0, 0); - assert!(before[0] < 0.01, "setup: (0,0) should be black, got {before:?}"); - s.execute("image.imageSize", json!({"width": 4, "height": 4, "resample": "nearest"})).unwrap(); - let st = s.active().unwrap(); - let surf = st.doc.layers[0].surface().unwrap(); - for (x, y) in [(0, 0), (1, 1), (2, 3)] { - let v = surf.rgba(x, y)[0]; - assert!(v.is_finite() && !(0.01..=0.99).contains(&v), "nearest produced an averaged value {v} at ({x},{y})"); - } - } - - #[test] - fn canvas_size_anchors_and_extends_background() { - let mut s = session(); - s.execute("image.canvasSize", json!({"width": 60, "height": 30, "anchor": "topLeft", "extensionColor": "#00ff00"})).unwrap(); - let d = doc(&s); - assert_eq!(d.size, Size::new(60, 30)); - assert_eq!(d.layers[1].surface().unwrap().content_bounds(), Rect::new(10, 5, 20, 15)); - let bg = d.layers[0].surface().unwrap(); - assert_eq!(bg.pixel(50, 25), vec![0.0, 1.0, 0.0, 1.0]); - assert_eq!(bg.pixel(5, 5), vec![1.0, 1.0, 1.0, 1.0]); - let mut s = session(); - s.execute("image.canvasSize", json!({"width": 20, "height": 10, "relative": true, "anchor": "center"})).unwrap(); - assert_eq!(doc(&s).layers[1].surface().unwrap().content_bounds(), Rect::new(20, 10, 30, 20)); - } - - #[test] - fn canvas_size_anchor_offsets_and_rejects_unlisted() { - // 64x48 -> 100x80: dx spans 36 and dy spans 32, so each anchor factor lands on a known - // offset. All nine documented values must anchor exactly where they do today (#2946). - let expected = [ - ("topLeft", [0, 0]), - ("top", [18, 0]), - ("topRight", [36, 0]), - ("left", [0, 16]), - ("center", [18, 16]), - ("right", [36, 16]), - ("bottomLeft", [0, 32]), - ("bottom", [18, 32]), - ("bottomRight", [36, 32]), - ]; - for (anchor, off) in expected { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48, "background": "white"})).unwrap(); - let r = s.execute("image.canvasSize", json!({"width": 100, "height": 80, "anchor": anchor})).unwrap(); - assert_eq!(r["offset"], json!(off), "anchor {anchor}"); - } - // An omitted anchor still defaults to center. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48, "background": "white"})).unwrap(); - assert_eq!(s.execute("image.canvasSize", json!({"width": 100, "height": 80})).unwrap()["offset"], json!([18, 16])); - // A misspelling or unknown value is a BadParams error naming `anchor`, never an anchor - // between two of the nine (the old independent-axis matching produced top-centre etc.). - for anchor in ["topleft", "TopLeft", "TOPLEFT", "bottomleft", "bottomright", "middle", "", "NOPE"] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48, "background": "white"})).unwrap(); - let err = s.execute("image.canvasSize", json!({"width": 100, "height": 80, "anchor": anchor})).unwrap_err(); - assert!(matches!(err, EngineError::BadParams { .. }), "anchor {anchor:?} must be rejected, got {err:?}"); - assert!(err.to_string().contains("anchor"), "the error for {anchor:?} must name `anchor`: {err}"); - } - } - - #[test] - fn crop_to_selection_and_trim() { - let mut s = session(); - assert!(s.execute("image.crop", json!({})).is_err(), "no selection, no rect"); - s.execute("select.rect", json!({"x": 5, "y": 2, "width": 20, "height": 10})).unwrap(); - s.execute("image.crop", json!({})).unwrap(); - let d = doc(&s); - assert_eq!(d.size, Size::new(20, 10)); - assert!(d.selection.is_none()); - assert_eq!(d.layers[1].surface().unwrap().content_bounds(), Rect::new(5, 3, 15, 10)); - // Trim transparent: hide the background, trim to the red square. - let mut s = session(); - s.execute("layer.setProps", json!({"layer": doc(&s).layers[0].id.0, "visible": false})).unwrap(); - s.execute("image.trim", json!({"basedOn": "transparent"})).unwrap(); - // The red square (10×10) plus its drop shadow (distance 5, size 5). - let size = doc(&s).size; - assert!(size.width > 10 && size.width < 30 && size.height > 10 && size.height < 25, "{size:?}"); - } - - #[test] - fn crop_to_explicit_rect_can_extend_canvas() { - let mut s = session(); - s.execute("image.crop", json!({"x": 10, "y": 5, "width": 10, "height": 10})).unwrap(); - assert_eq!(doc(&s).size, Size::new(10, 10)); - assert_eq!(doc(&s).layers[1].surface().unwrap().pixel(0, 0), vec![1.0, 0.0, 0.0, 1.0]); - let mut s = session(); - s.execute("image.crop", json!({"x": -10, "y": 0, "width": 60, "height": 20})).unwrap(); - assert_eq!(doc(&s).size, Size::new(60, 20)); - assert_eq!(doc(&s).layers[1].surface().unwrap().pixel(20, 5), vec![1.0, 0.0, 0.0, 1.0]); - } - - #[test] - fn crop_extension_fills_the_background_with_the_background_colour() { - let mut shrink = Session::new(); - shrink.execute("file.new", json!({"width": 16, "height": 16, "background": "#ff0000"})).unwrap(); - shrink.execute("image.crop", json!({"x": 2, "y": 0, "width": 12, "height": 16})).unwrap(); - let layer = &doc(&shrink).layers[0]; - assert_eq!(layer.name, "Background", "a shrink-only crop keeps the Background"); - assert!(layer.locks.transparency && layer.locks.position); - - let mut s = Session::new(); - s.execute("file.new", json!({"width": 16, "height": 16, "background": "#ff0000"})).unwrap(); - s.tools.background = [0.0, 1.0, 0.0, 1.0]; - s.execute("image.crop", json!({"x": -4, "y": 0, "width": 20, "height": 16})).unwrap(); - let layer = &doc(&s).layers[0]; - assert_eq!(layer.name, "Background", "an extending crop keeps the Background (#3081)"); - assert!(layer.locks.transparency && layer.locks.position); - assert_eq!(layer.surface().unwrap().rgba(1, 8), [0.0, 1.0, 0.0, 1.0], "the added border takes the background colour"); - assert_eq!(layer.surface().unwrap().rgba(8, 8), [1.0, 0.0, 0.0, 1.0], "the original pixels stay"); - - s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); - let layer = &doc(&s).layers[0]; - assert_eq!(layer.surface().unwrap().rgba(1, 8), [0.0, 0.0, 1.0, 1.0], "Fill paints the extended border"); - assert_eq!(layer.surface().unwrap().rgba(8, 8), [0.0, 0.0, 1.0, 1.0]); - - s.execute("edit.undo", json!({})).unwrap(); - s.execute("edit.undo", json!({})).unwrap(); - let layer = &doc(&s).layers[0]; - assert_eq!(doc(&s).size, Size::new(16, 16)); - assert_eq!(layer.surface().unwrap().rgba(8, 8), [1.0, 0.0, 0.0, 1.0]); - assert_eq!(layer.surface().unwrap().content_bounds(), Rect::new(0, 0, 16, 16), "undo removes the filled border"); - - // A normal layer gets transparent pixels in the added area. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 16, "height": 16, "background": "#ff0000"})).unwrap(); - s.execute("layer.new.layerFromBackground", json!({})).unwrap(); - s.execute("image.crop", json!({"x": -4, "y": 0, "width": 20, "height": 16})).unwrap(); - assert_eq!(doc(&s).layers[0].surface().unwrap().rgba(1, 8), [0.0; 4]); - } - - /// A document (`mode`, `depth`) with a white Background and a layer holding a red pixel at - /// (12, 8) and a blue one at (30, 3). - fn marked(mode: &str, depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 20, "mode": mode, "depth": depth, "background": "white"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - for (x, y, c) in [(12, 8, "#ff0000"), (30, 3, "#0000ff")] { - s.execute("select.rect", json!({"x": x, "y": y, "width": 1, "height": 1})).unwrap(); - s.execute("edit.fill", json!({"color": c})).unwrap(); - } - s.execute("select.deselect", json!({})).unwrap(); - s - } - - fn layer_rgba(s: &Session, layer: usize, x: i32, y: i32) -> [f32; 4] { - doc(s).layers[layer].surface().unwrap().rgba(x, y) - } - - /// #1792: a frame turned 90° clockwise crops what Image Rotation › Arbitrary 90° CCW and a - /// plain crop give, in one undo step that restores the document, at 8, 16 and 32 bits. - #[test] - fn crop_at_90_degrees_is_rotate_then_crop_in_one_step() { - for depth in [8, 16, 32] { - let mut s = marked("rgb", depth); - let before = doc(&s).clone(); - let past = s.active().unwrap().history.past_len(); - // The 10 × 20 frame at (10, 0), turned 90° about (15, 10), covers x 5..25, y 5..15. - let r = s.execute("image.crop", json!({"x": 10, "y": 0, "width": 10, "height": 20, "angle": 90})).unwrap(); - assert_eq!(r["angle"], json!(90.0), "{depth}"); - assert_eq!(doc(&s).size, Size::new(10, 20), "{depth}"); - assert_eq!(s.active().unwrap().history.past_len(), past + 1, "{depth}: one undo step"); - // (12, 8) is 2.5 left of and 1.5 above the centre: in the upright frame, 1.5 left of - // and 2.5 below its centre (5, 10). - let red = layer_rgba(&s, 1, 3, 12); - assert!(red[0] > 0.9 && red[1] < 0.1 && red[3] > 0.9, "{depth}: {red:?}"); - let mut reference = marked("rgb", depth); - reference.execute("image.rotation.arbitrary", json!({"angle": 90, "direction": "ccw"})).unwrap(); - reference.execute("image.crop", json!({"x": 5, "y": 15, "width": 10, "height": 20})).unwrap(); - for l in 0..2 { - for y in 0..20 { - for x in 0..10 { - let (a, b) = (layer_rgba(&s, l, x, y), layer_rgba(&reference, l, x, y)); - assert!(a.iter().zip(b).all(|(p, q)| (p - q).abs() < 1e-3), "{depth} layer {l} ({x}, {y}): {a:?} vs {b:?}"); - } - } - } - s.execute("edit.undo", json!({})).unwrap(); - let d = doc(&s); - assert_eq!(d.size, before.size, "{depth}"); - assert_eq!(d.depth, before.depth); - for y in 0..20 { - for x in 0..40 { - assert_eq!(layer_rgba(&s, 1, x, y), before.layers[1].surface().unwrap().rgba(x, y), "{depth} undo at ({x}, {y})"); - } - } - } - } - - /// A slightly turned frame keeps its own size, the pixel under its centre and the Background's - /// colour; the turn's sign matters (a counter-clockwise frame is a negative angle). - #[test] - fn crop_at_a_small_angle_keeps_the_frame_size_in_every_mode() { - for (mode, depth) in [("rgb", 8), ("rgb", 16), ("rgb", 32), ("grayscale", 16), ("cmyk", 8), ("lab", 16)] { - for angle in [5.0, -7.5, 365.0] { - let mut s = marked(mode, depth); - s.execute("image.crop", json!({"x": 2, "y": 1, "width": 21, "height": 15, "angle": angle})).unwrap(); - let d = doc(&s); - assert_eq!(d.size, Size::new(21, 15), "{mode} {depth} {angle}"); - assert_eq!(d.depth.bytes() * 8, depth as usize, "{mode} {depth}"); - assert_eq!(format!("{:?}", d.mode).to_lowercase(), mode, "{mode} {depth}"); - assert_eq!(d.layers.len(), 2); - // The frame's centre (12.5, 8.5) is the red pixel (the layer's only paint nearby); - // it stays at the centre, its colour kept. - let want = layer_rgba(&marked(mode, depth), 1, 12, 8); - let red = layer_rgba(&s, 1, 10, 7); - assert!(red[3] > 0.5 && (0..3).all(|c| (red[c] - want[c]).abs() < 0.2), "{mode} {depth} {angle}: {red:?} vs {want:?}"); - // The Background stays opaque white inside the canvas it came from. - let bg = layer_rgba(&s, 0, 10, 7); - assert!(bg.iter().all(|v| (v - 1.0).abs() < 0.02), "{mode} {depth} {angle}: {bg:?}"); - } - } - } - - /// Angle 0 (or a whole turn) is the plain crop: no resampling, the same document. - #[test] - fn crop_at_angle_zero_is_the_plain_crop() { - let mut plain = marked("rgb", 16); - plain.execute("image.crop", json!({"x": 3, "y": 2, "width": 30, "height": 10})).unwrap(); - for angle in [json!(0), json!(0.0), json!(360), json!(-720), json!(null)] { - let mut s = marked("rgb", 16); - let r = s.execute("image.crop", json!({"x": 3, "y": 2, "width": 30, "height": 10, "angle": angle})).unwrap(); - assert!(r.get("angle").is_none(), "{angle}: {r}"); - assert_eq!(doc(&s).size, Size::new(30, 10)); - for l in 0..2 { - for y in 0..10 { - for x in 0..30 { - assert_eq!(layer_rgba(&s, l, x, y), layer_rgba(&plain, l, x, y), "{angle} layer {l} ({x}, {y})"); - } - } - } - } - } - - /// Bad angles fail cleanly, before anything changes. - #[test] - fn crop_angle_rejects_bad_values_without_changing_the_document() { - let bad = [ - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": "NaN"}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": "12"}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": [1]}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": true}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": 1e308}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": -3601}), - json!({"x": 0, "y": 0, "width": 10, "height": 10, "angle": f64::MAX}), - // An angle needs the frame: a selection crop has none. - json!({"angle": 10}), - json!({"x": 0, "y": 0, "width": 0, "height": 10, "angle": 10}), - json!({"x": 0, "y": 0, "width": 300_001, "height": 10, "angle": 10}), - json!({"x": i32::MAX - 5, "y": 0, "width": 10, "height": 10, "angle": 10}), - ]; - for p in bad { - let mut s = marked("rgb", 8); - s.execute("select.all", json!({})).unwrap(); - let before = doc(&s).clone(); - let past = s.active().unwrap().history.past_len(); - assert!(s.execute("image.crop", p.clone()).is_err(), "{p}"); - assert_eq!(doc(&s).size, before.size, "{p}"); - assert_eq!(s.active().unwrap().history.past_len(), past, "{p}"); - } - // Far-off but representable frames turn and crop without panicking. - for (x, y) in [(-1_000_000, 0), (0, 1_000_000), (i32::MIN / 4, i32::MAX / 4)] { - let mut s = marked("rgb", 8); - s.execute("image.crop", json!({"x": x, "y": y, "width": 4, "height": 3, "angle": 33.3})).unwrap(); - assert_eq!(doc(&s).size, Size::new(4, 3)); - } - } - - #[test] - fn crop_refuses_a_canvas_side_past_the_new_document_limit() { - // #960: an unbounded explicit crop left a canvas that a later Trim or Duplicate Merged - // could only abort on allocating. - for (w, h) in [(100_000_000, 100_000_000), (300_001, 10), (10, 300_001)] { - let mut s = session(); - let (size, past) = (doc(&s).size, s.active().unwrap().history.past_len()); - let err = s.execute("image.crop", json!({"x": 0, "y": 0, "width": w, "height": h})).unwrap_err(); - assert!(err.to_string().contains("300000 px limit"), "{w} x {h}: {err}"); - assert_eq!((doc(&s).size, s.active().unwrap().history.past_len()), (size, past), "{w} x {h}: nothing changed"); - } - // At the limit it still extends the canvas (the tiles stay lazy). - let mut s = session(); - s.execute("image.crop", json!({"x": 0, "y": 0, "width": 300_000, "height": 2})).unwrap(); - assert_eq!(doc(&s).size, Size::new(300_000, 2)); - } - - /// #2443: the Crop tool's W x H x Resolution crops, resamples the cropped area to exactly the - /// target size and sets the resolution, in one undo step, at 8 and 16 bits. - #[test] - fn crop_to_a_target_size_and_resolution_is_one_step() { - for depth in [8, 16] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 300, "resolution": 72, "depth": depth})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(100, 100, 200, 225), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - let past = s.active().unwrap().history.past_len(); - // 4 x 5 in at 300 ppi from a 100 x 125 px frame. - let r = s - .execute("image.crop", json!({"x": 100, "y": 100, "width": 100, "height": 125, "targetWidth": 1200, "targetHeight": 1500, "resolution": 300})) - .unwrap(); - assert_eq!((r["documentWidth"].as_u64(), r["documentHeight"].as_u64()), (Some(1200), Some(1500)), "{depth}: {r}"); - assert_eq!(r["resolution"].as_f64(), Some(300.0), "{depth}"); - let d = doc(&s); - assert_eq!(d.size, Size::new(1200, 1500), "{depth}"); - assert_eq!(d.resolution_dpi, 300.0, "{depth}"); - assert_eq!(d.layers[0].surface().unwrap().format().sample, if depth == 8 { SampleType::U8 } else { SampleType::U16 }); - // The red frame filled the crop; it fills the result. - let red = d.layers[1].surface().unwrap(); - for (x, y) in [(0, 0), (600, 750), (1199, 1499)] { - assert_eq!(red.pixel(x, y), vec![1.0, 0.0, 0.0, 1.0], "{depth}: ({x}, {y})"); - } - assert_eq!(s.active().unwrap().history.past_len(), past + 1, "{depth}: one history step"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!((doc(&s).size, doc(&s).resolution_dpi), (Size::new(400, 300), 72.0), "{depth}: undo restores the original"); - } - } - - #[test] - fn crop_target_resamples_after_a_turned_crop_and_resolution_alone_keeps_the_pixels() { - let mut s = session(); - let past = s.active().unwrap().history.past_len(); - s.execute("image.crop", json!({"x": 10, "y": 0, "width": 10, "height": 20, "angle": 90, "targetWidth": 30, "targetHeight": 60, "resample": "nearest"})) - .unwrap(); - assert_eq!(doc(&s).size, Size::new(30, 60)); - assert_eq!(s.active().unwrap().history.past_len(), past + 1, "rotate, crop and resample: one step"); - let mut s = session(); - s.execute("image.crop", json!({"x": 2, "y": 3, "width": 30, "height": 10, "resolution": 150})).unwrap(); - assert_eq!((doc(&s).size, doc(&s).resolution_dpi), (Size::new(30, 10), 150.0)); - } - - #[test] - fn crop_target_rejects_bad_params_without_changing_the_document() { - let rect = json!({"x": 0, "y": 0, "width": 10, "height": 10}); - let bad_params = [ - json!({"targetWidth": 100}), - json!({"targetHeight": 100}), - json!({"targetWidth": 0, "targetHeight": 10}), - json!({"targetWidth": -5, "targetHeight": 10}), - json!({"targetWidth": 10.5, "targetHeight": 10}), - json!({"targetWidth": "10", "targetHeight": 10}), - json!({"targetWidth": 300_001, "targetHeight": 10}), - json!({"targetWidth": 4_294_967_306_u64, "targetHeight": 10}), - json!({"targetWidth": 300_000, "targetHeight": 300_000}), - json!({"targetWidth": 10, "targetHeight": 10, "resample": "none"}), - json!({"targetWidth": 10, "targetHeight": 10, "resample": 3}), - json!({"resolution": 0}), - json!({"resolution": -72}), - json!({"resolution": 1e9}), - json!({"resolution": "300"}), - ]; - for extra in bad_params { - let mut s = session(); - let (size, dpi, past) = (doc(&s).size, doc(&s).resolution_dpi, s.active().unwrap().history.past_len()); - let mut p = rect.clone(); - if let (Some(o), Some(e)) = (p.as_object_mut(), extra.as_object()) { - o.extend(e.clone()); - } - assert!(s.execute("image.crop", p.clone()).is_err(), "{p}"); - assert_eq!((doc(&s).size, doc(&s).resolution_dpi, s.active().unwrap().history.past_len()), (size, dpi, past), "{p}: nothing changed"); - } - } - - #[test] - fn trim_based_on_top_left_colour() { - let mut s = session(); - s.edit("no fx", |doc, _| { - doc.layers[1].effects.items.clear(); - Ok(()) - }) - .unwrap(); - s.execute("image.trim", json!({"basedOn": "topLeft"})).unwrap(); - assert_eq!(doc(&s).size, Size::new(10, 10)); - } - - #[test] - fn thirty_two_bit_holds_linear_light() { - // Photoshop 27.11: an 8-bit sRGB grey 128 becomes 0.2159 in 32 bits (and 32 → 0.0144), - // and comes back unchanged; alpha keeps its value. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 4, "height": 1, "background": "white"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 2, 1), &[128.0 / 255.0, 32.0 / 255.0, 1.0, 0.5]); - Ok(()) - }) - .unwrap(); - s.execute("image.mode.bits32", json!({})).unwrap(); - let px = doc(&s).layers[1].surface().unwrap().pixel(0, 0); - for (got, want) in px.iter().zip([0.2159, 0.0144, 1.0, 128.0 / 255.0]) { - assert!((got - want).abs() < 2e-4, "{px:?}"); - } - s.execute("image.mode.bits8", json!({})).unwrap(); - let px = doc(&s).layers[1].surface().unwrap().pixel(0, 0); - for (got, want) in px.iter().zip([128.0 / 255.0, 32.0 / 255.0, 1.0, 128.0 / 255.0]) { - assert!((got - want).abs() < 0.6 / 255.0, "{px:?}"); - } - } - - #[test] - fn grayscale_conversion_matches_photoshop() { - // Photoshop 27.11, default Color Settings (working gray Dot Gain 20 %): Image > Mode > - // Grayscale turns sRGB 64 / 128 / 192 grey and pure red into 46 / 105 / 177 / 107. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 4, "height": 1, "background": "white"})).unwrap(); - s.edit("paint", |doc, _| { - let l = doc.layers.first_mut().unwrap(); - let surf = l.surface_mut().unwrap(); - for (x, c) in [[64.0, 64.0, 64.0], [128.0, 128.0, 128.0], [192.0, 192.0, 192.0], [255.0, 0.0, 0.0]].iter().enumerate() { - surf.fill_rect(Rect::new(x as i32, 0, x as i32 + 1, 1), &[c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0]); - } - Ok(()) - }) - .unwrap(); - s.execute("image.mode.grayscale", json!({})).unwrap(); - let surf = doc(&s).layers[0].surface().unwrap(); - for (x, want) in [46.0, 105.0, 177.0, 107.0].iter().enumerate() { - let got = surf.pixel(x as i32, 0)[0] * 255.0; - assert!((got - want).abs() <= 1.5, "pixel {x}: {got} vs {want}"); - } - } - - #[test] - fn mode_and_depth_conversions() { - let mut s = session(); - s.execute("image.mode.grayscale", json!({})).unwrap(); - assert_eq!(doc(&s).mode, ColorMode::Grayscale); - assert_eq!(doc(&s).layers[1].surface().unwrap().format().mode, ColorMode::Grayscale); - s.execute("image.mode.bits16", json!({})).unwrap(); - assert_eq!(doc(&s).depth, SampleType::U16); - assert_eq!(doc(&s).layers[1].surface().unwrap().format().sample, SampleType::U16); - assert_eq!(doc(&s).layers[1].mask.as_ref().unwrap().surface.format().sample, SampleType::U16); - s.execute("image.mode.cmyk", json!({})).unwrap(); - s.execute("image.mode.bits32", json!({})).unwrap(); - s.execute("image.mode.lab", json!({})).unwrap(); - s.execute("image.mode.rgb", json!({})).unwrap(); - s.execute("image.mode.bits8", json!({})).unwrap(); - let d = doc(&s); - assert_eq!((d.mode, d.depth), (ColorMode::Rgb, SampleType::U8)); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(doc(&s).depth, SampleType::F32); - let revision = s.active().unwrap().revision; - s.execute("image.mode.bits32", json!({})).unwrap(); - assert_eq!(s.active().unwrap().revision, revision, "an unchanged bit depth must preserve the history branch"); - assert!(s.redo(), "selecting the current depth must not discard the undone conversion"); - assert_eq!(doc(&s).depth, SampleType::U8); - } - - #[test] - fn duplicate_document() { - let mut s = session(); - let r = s.execute("image.duplicate", json!({"mergedOnly": true})).unwrap(); - assert_eq!(r["document"], json!(1)); - assert_eq!(s.documents().len(), 2); - assert_eq!(doc(&s).layers.len(), 1); - assert!(doc(&s).name.ends_with("copy")); - } -} diff --git a/crates/engine/src/inspect.rs b/crates/engine/src/inspect.rs index 6068503aec..dc681b7d8e 100644 --- a/crates/engine/src/inspect.rs +++ b/crates/engine/src/inspect.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use photocraft_doc::{Layer, LayerContent, SmartContentsId, SmartSource}; +use photocraft_doc::{Adjustment, Layer, LayerContent, SmartContentsId, SmartSource}; use serde_json::{Value, json}; use crate::{DocState, Session}; @@ -21,6 +21,8 @@ pub fn session(s: &Session) -> Value { })).collect::>(), "foreground": s.tools.foreground, "background": s.tools.background, + // History panel › History Options… (`history.options`). + "historyOptions": crate::history_options_cmds::current(s), }) } @@ -43,6 +45,7 @@ pub fn document(d: &DocState) -> Value { "selectionBounds": doc.selection.as_ref().map(|s| { let r = s.content_bounds(); [r.x0, r.y0, r.width() as i32, r.height() as i32] }), "layers": doc.layers.iter().rev().map(|l| layer_sel(l, &selected, &instances, Some(&doc.metadata))).collect::>(), "history": d.history.entries(), + "historySource": crate::history_source_cmds::source_json(d), "snapshots": d.snapshots.iter().map(|snap| json!({"id": snap.id, "name": snap.name})).collect::>(), "canUndo": d.history.can_undo(), "canRedo": d.history.can_redo(), @@ -105,6 +108,7 @@ fn layer_sel(l: &Layer, selected: &[photocraft_doc::LayerId], instances: &HashMa "bounds": bounds, "selected": selected.contains(&l.id), "linkGroup": l.link_group, + "hasXmp": l.xmp.is_some(), }); match &l.content { LayerContent::Group(g) => { @@ -118,6 +122,11 @@ fn layer_sel(l: &Layer, selected: &[photocraft_doc::LayerId], instances: &HashMa // The same kind and parameters the adjustment commands take (`inBlack` 0..255, …), // not the internal model. v["adjustment"] = json!({"kind": crate::commands::adjustment_kind(a), "params": crate::adjust_params::to_params(a)}); + // Kept as imported and never rendered (#6541): agents and the Layers panel can tell. + if let Adjustment::Unsupported { psd_key, .. } = a { + v["adjustment"]["unsupported"] = json!(true); + v["adjustment"]["psdKey"] = json!(psd_key); + } } LayerContent::Smart(sm) => { v["smartFilters"] = Value::Array( @@ -210,6 +219,32 @@ mod tests { assert!(!adj.to_string().contains('_'), "{adj}"); } + /// #6541: an adjustment PhotoCraft keeps but cannot render is reported as such, not as an Invert. + #[test] + fn an_unsupported_adjustment_layer_is_reported_as_kept_not_rendered() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 4, "height": 4})).unwrap(); + s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + s.edit("unsupported fixture", |doc, active| { + let l = Layer::new("Mystery", LayerContent::Adjustment(Adjustment::Unsupported { psd_key: "xyzw".into(), raw: vec![1, 2, 3].into() })); + let id = l.id; + doc.layers.push(l); + *active = Some(id); + Ok(()) + }) + .unwrap(); + let info = s.execute("document.inspect", json!({})).unwrap(); + let layers = info["layers"].as_array().unwrap(); + let unsupported = &layers[0]["adjustment"]; + assert_eq!(unsupported["kind"], "unsupported", "{unsupported}"); + assert_eq!(unsupported["unsupported"], json!(true)); + assert_eq!(unsupported["psdKey"], "xyzw"); + // A real Invert reports neither flag. + let invert = &layers[1]["adjustment"]; + assert_eq!(invert["kind"], "invert"); + assert!(invert.get("unsupported").is_none(), "{invert}"); + } + #[test] fn document_inspect_reports_raster_and_vector_masks_when_nested() { let cases = [(false, None), (true, None), (false, Some(true)), (false, Some(false)), (true, Some(true))]; diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs index de2afd1556..d5c8746062 100644 --- a/crates/engine/src/jobs.rs +++ b/crates/engine/src/jobs.rs @@ -66,6 +66,11 @@ pub(crate) fn authorize_render_command(id: &str, params: &Value) -> Result<()> { } } +/// Whether the work on this thread is done for an untrusted session (a gate is installed). +pub(crate) fn authorization_installed() -> bool { + RENDER_AUTHORIZATION.with(std::cell::Cell::get).is_some() +} + /// Identifies one background job for the session's lifetime. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, serde::Deserialize)] pub struct JobId(pub u64); @@ -212,6 +217,11 @@ struct Running { label: String, params: Value, journal: bool, + /// The recording (`ActionState::take`) running when the job started, if any (set by + /// `dispatch`, once recording resumes). Only that recording gets the job's step: one started + /// later must not pick up an edit made before it. + recorded_by: Option, + pre_selected: Option>, doc: Option, /// The document as the job saw it: applying fails if it was edited meanwhile. snapshot: Option>, @@ -219,12 +229,37 @@ struct Running { started_ms: f64, coalesce: Option, color_restrict: Option, + filter_mask_layer: Option, + /// The document's layer selection when the job started: applying targets it, not whatever + /// got selected meanwhile (a Type click does that without editing the document, #7366). + layers: Option, rx: std::sync::mpsc::Receiver>, apply: Option, #[cfg(not(target_arch = "wasm32"))] handle: Option>, } +/// A document's active layer, selected layers and selection anchor. +#[derive(Clone, PartialEq)] +struct LayerSelection { + active: Option, + selected: Vec, + anchor: Option, +} + +impl LayerSelection { + fn of(st: &crate::DocState) -> Self { + Self { active: st.active_layer, selected: st.selected_layers.clone(), anchor: st.layer_anchor } + } + + fn set(self, st: &mut crate::DocState) { + st.active_layer = self.active; + st.selected_layers = self.selected; + st.layer_anchor = self.anchor; + crate::channel_cmds::fix_view(st); + } +} + impl Running { fn info(&self) -> JobInfo { JobInfo { @@ -296,14 +331,25 @@ fn independent_of_document(id: &str) -> bool { } #[cfg(not(target_arch = "wasm32"))] -fn read_path(path: &str) -> Result> { - // Bounded reads, and a clear error for a file larger than memory (#375). - photocraft_format::read_file(std::path::Path::new(path)).map_err(|e| EngineError::Other(format!("{path}: {e}"))) +fn import_path( + name: &str, + path: &str, + opts: &photocraft_io::ImportOptions, + ctl: &photocraft_raster::Interrupt, +) -> std::result::Result<(photocraft_io::ImportResult, bool), photocraft_io::IoError> { + // Bounded reads, and a clear error for a file larger than memory (#375); a PSD/PSB is + // parsed from the open file instead of being read whole. + photocraft_io::import_file_for_open_with(name, std::path::Path::new(path), opts, ctl) } #[cfg(target_arch = "wasm32")] -fn read_path(path: &str) -> Result> { - Err(EngineError::Other(format!("cannot open paths on the web: {path}"))) +fn import_path( + _: &str, + path: &str, + _: &photocraft_io::ImportOptions, + _: &photocraft_raster::Interrupt, +) -> std::result::Result<(photocraft_io::ImportResult, bool), photocraft_io::IoError> { + Err(photocraft_io::IoError::Read(format!("cannot open paths on the web: {path}"))) } /// Run a job-capable command's heavy part. `work` runs on a worker thread when the command was @@ -323,6 +369,9 @@ pub fn run( ) -> Result { let authorization = s.authorize.or_else(|| RENDER_AUTHORIZATION.with(std::cell::Cell::get)); let _authorization = AuthorizationScope::enter(authorization); + // The job's document: stored paths it follows are resolved against it on any thread. + let document = s.active().and_then(|d| d.path.as_deref()).map(std::path::PathBuf::from); + let _document = crate::doc_path::scope(document.as_deref()); if !s.jobs.spawn || cfg!(target_arch = "wasm32") { let t = work(&s.jobs.inline.clone().unwrap_or_default())?; return apply(s, t); @@ -330,15 +379,17 @@ pub fn run( if s.jobs.pending.is_some() { return Err(EngineError::Other(format!("`{label}` started a second background job in one command"))); } - let (doc, snapshot) = match (lock_document, s.active()) { - (true, Some(st)) => (Some(st.doc.id), Some(Arc::downgrade(&st.doc))), + let (doc, snapshot, layers) = match (lock_document, s.active()) { + (true, Some(st)) => (Some(st.doc.id), Some(Arc::downgrade(&st.doc)), Some(LayerSelection::of(st))), (true, None) => return Err(EngineError::NoDocument), - (false, _) => (None, None), + (false, _) => (None, None, None), }; let ctx = JobCtx::new(); let (tx, rx) = std::sync::mpsc::channel::>(); + let apply_document = document.clone(); let erased: Apply = Box::new(move |s, any| { let _authorization = AuthorizationScope::enter(authorization); + let _document = crate::doc_path::scope(apply_document.as_deref()); let t = any.downcast::().map_err(|_| EngineError::Other("internal error: job result type mismatch".into()))?; apply(s, *t) }); @@ -352,6 +403,7 @@ pub fn run( // no way to get it back, so the error is reported instead. let spawned = std::thread::Builder::new().name(format!("photocraft-job-{}", id.0)).spawn(move || { let _authorization = AuthorizationScope::enter(authorization); + let _document = crate::doc_path::scope(document.as_deref()); let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| work(&wctx).map(|t| Box::new(t) as Payload))) .unwrap_or_else(|_| Err(EngineError::Other(format!("`{label_w}` failed with an internal error (logged); the document is unchanged")))); // The receiver is gone when the job was cancelled: nothing to report. @@ -370,12 +422,16 @@ pub fn run( label: label.to_string(), params: Value::Null, journal: true, + recorded_by: None, + pre_selected: None, doc, snapshot, ctx, started_ms: now_ms(), coalesce: s.coalesce_request.clone(), color_restrict: s.color_restrict, + filter_mask_layer: s.filter_mask_layer, + layers, rx, apply: Some(erased), #[cfg(not(target_arch = "wasm32"))] @@ -453,6 +509,14 @@ pub enum OpenSource { /// The command id open jobs report (there is no engine command: opening is the shell's). pub const OPEN_JOB: &str = "file.open"; +/// The import settings files are opened and placed with. +pub fn import_options(prefs: &crate::prefs::Preferences) -> photocraft_io::ImportOptions { + photocraft_io::ImportOptions { + max_group_depth: prefs.file_handling.rasterize_svg_groups_deeper_than as usize, + demosaic: prefs.raw_defaults.demosaic.method(), + } +} + impl Session { /// Run the heavy part of inline (not background) job commands under `ctx`, so another thread /// can cancel them: a live preview whose parameters changed stops its stale computation @@ -471,15 +535,30 @@ impl Session { /// Open a file in the background: read (for a path) and decode on a worker, with progress /// (per layer for PSD/PSB) and cancellation, then add the document like /// [`Session::open_document`] (Color Settings policies apply). `name` names the document. - /// The job's result is `{document, name, warnings, color}`; on wasm it runs inline. When + /// The job's result is `{document, docId, name, warnings, color}`; on wasm it runs inline. + /// `document` is the tab index at apply time; resolve `docId` before handling a delayed + /// completion, since earlier handlers may have moved or closed tabs. When /// Color Settings ask about the file's profile, the document is held back instead and the /// result is `{held, name, warnings, color}` (`color` the question): take it with /// [`Session::take_held_open`] once answered. pub fn start_open(&mut self, name: &str, source: OpenSource) -> Result { + self.start_open_inner(name, source, false) + } + + /// Interactive opening: hold a damaged PSD until the user answers a damage + /// confirmation, without changing import or subsequent save behaviour. + /// Headless callers keep using [`Self::start_open`]. + pub fn start_open_for_user(&mut self, name: &str, source: OpenSource) -> Result { + self.start_open_inner(name, source, true) + } + + /// Decodes a file in a job and optionally holds damaged PSD content for + /// interactive confirmation while leaving headless opens non-interactive. + fn start_open_inner(&mut self, name: &str, source: OpenSource, confirm_damage: bool) -> Result { let label = format!("Opening {name}"); let name_w = name.to_string(); let name_a = name.to_string(); - let max_svg_group_depth = self.prefs().file_handling.rasterize_svg_groups_deeper_than as usize; + let opts = import_options(self.prefs()); self.start_job( OPEN_JOB, json!({"name": name}), @@ -487,35 +566,52 @@ impl Session { false, move |ctx| { ctx.progress(0.0, "Reading"); - let bytes = match source { - OpenSource::Bytes(b) => b, - OpenSource::Path(p) => Arc::new(read_path(&p)?), - }; - ctx.check()?; - ctx.progress(0.02, "Decoding"); - let imported = ctx - .stage(0.02, 0.95, "Decoding", |ctl| photocraft_io::import_with_svg_group_depth_and_interrupt(&name_w, &bytes, max_svg_group_depth, ctl)) - .map_err(|e| match e { - photocraft_io::IoError::Cancelled => EngineError::Cancelled, - e => EngineError::Other(e.to_string()), - })?; + let imported = match source { + OpenSource::Bytes(bytes) => { + ctx.check()?; + ctx.progress(0.02, "Decoding"); + ctx.stage(0.02, 0.95, "Decoding", |ctl| photocraft_io::import_for_open_with(&name_w, &bytes, &opts, ctl)) + } + // A PSD/PSB is decoded straight from the file, which is never read whole. + OpenSource::Path(p) => ctx.stage(0.0, 0.95, "Decoding", |ctl| import_path(&name_w, &p, &opts, ctl)), + } + .map_err(|e| match e { + photocraft_io::IoError::Cancelled => EngineError::Cancelled, + e => EngineError::Other(e.to_string()), + })?; ctx.progress(0.95, "Preparing layers"); - warm_layer_bounds(&imported.document, || ctx.check())?; + warm_layer_bounds(&imported.0.document, || ctx.check())?; + // Type layers need the shared text engine, whose first use waits for the + // installed-font scan started at launch (minutes on some machines). Wait here, + // so the shell's missing-font check after the open finds it ready instead of + // freezing the window (#7332). + if crate::type_extra_cmds::has_type_layers(&imported.0.document) { + ctx.progress(0.97, "Loading fonts"); + let _ = photocraft_text::shared(); + ctx.check()?; + } Ok(imported) }, - move |s, r: photocraft_io::ImportResult| { + move |s, (mut r, damaged): (photocraft_io::ImportResult, bool)| { + // File Handling's ACEScg option, before the colour prompt looks at the profile. + crate::file_cmds::tag_exr_colorspace(s, &name_a, &mut r.document); + crate::file_cmds::raw_defaults(s, &r.warnings, &mut r.document); // Ask When Opening: held back until the prompt is answered (`take_held_open`). A // developed raw has no embedded profile to ask about. let raw = r.warnings.iter().any(|w| w.contains(photocraft_io::raw::DEVELOPED_NOTE)); - if let Some(question) = s.color.open_question(&r.document).filter(|_| !raw) { + let damaged = confirm_damage && damaged; + let question = s.color.open_question(&r.document).filter(|_| !raw); + if damaged || question.is_some() { let held = s.hold_open(r.document, r.source_read_only); - return Ok(json!({"held": held, "name": name_a, "warnings": r.warnings, "color": question})); + return Ok(json!({"held": held, "name": name_a, "warnings": r.warnings, "color": question, "damaged": damaged})); } let (index, color) = s.open_document(r.document, None); + // Persisted IDs can collide: report the identity the session actually admitted. + let id = s.documents().get(index).ok_or(EngineError::NoDocument)?.doc.id; if let Some(st) = s.active_mut() { st.source_read_only = r.source_read_only; } - Ok(json!({"document": index, "name": name_a, "warnings": r.warnings, "color": color})) + Ok(json!({"document": index, "docId": id.0, "name": name_a, "warnings": r.warnings, "color": color})) }, ) } @@ -543,7 +639,7 @@ impl Session { job.ctx.cancel(); Err(e) } - (Err(e), None) => Err(e), + (Err(e), None) => Err(self.explain_missing_layer(e)), (Ok(_), Some(mut job)) => { job.command = command.to_string(); job.params = params; @@ -681,6 +777,9 @@ impl Session { /// Run the command `id`: inline, or (with `background`) as a job when it is one. pub(crate) fn dispatch(&mut self, id: &str, params: Value, background: bool) -> Result { + // The offer belongs to this one dispatch. Early errors drop it locally, including + // authorization, validation, floating-selection and admission failures. + let live_commit = self.live_commit.take(); let spec = crate::commands::find(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; // Params are named fields: anything but an object (or null, meaning none) is a caller // mistake, not "use every default". @@ -694,6 +793,7 @@ impl Session { gate(id, ¶ms)?; } let _authorization = AuthorizationScope::enter(self.authorize); + let _document = crate::doc_path::scope(self.active().and_then(|d| d.path.as_deref()).map(std::path::Path::new)); // Last Filter runs as the filter it repeats: journaled, recorded in an action, faded and // run as a job like that filter, so a recorded step replays the same filter anywhere // (#3089), as Transform Again records its transform (#1440). With no filter yet it falls @@ -714,48 +814,119 @@ impl Session { } // Pixel commands follow the Channels panel target unless the caller names one. let run_params = self.admit(id, spec, ¶ms)?; + // This UI-only disclosure preference applies to both `execute` and `start`: + // desktop UI normally calls `start`, even for synchronous Layer Style commands. + // The layer the command will change, named as the command accepts it (id or name), else the + // active one. `layer.layerStyle.clear` needs no case: it leaves no effects, so nothing expands. + // Transfer Effects changes its destination (`to`), not the layer it takes the effects from. + let new_fx_target = if id.starts_with("layer.layerStyle.") { + let target = if id == "layer.layerStyle.transferEffects" { + crate::commands::layer_param(self, &json!({"layer": params.get("to").cloned().unwrap_or(Value::Null)})) + } else { + crate::commands::layer_param(self, ¶ms) + } + .ok(); + self.active().and_then(|st| { + let target = target?; + (st.doc.layer(target)?.effects.items.is_empty()).then_some((st.doc.id, target)) + }) + } else { + None + }; + let expand_new_fx = crate::commands::layer_panel_option(self, "expandNewEffects", true); self.coalesce_request = params.get("coalesce").and_then(Value::as_str).map(str::to_string); self.color_restrict = crate::channel_cmds::color_restriction(self, id, &run_params); + self.filter_mask_layer = crate::channel_cmds::filter_mask_layer(self, id, &run_params); self.jobs.spawn = background; // Commands this one runs on its own behalf journal after this mark (#3067). let nested_from = self.journal.len(); + let pre_selected = if self.actions.recording.is_some() { self.active().map(|st| st.selected_layers()) } else { None }; // A replayable command is recorded as one step, so recording pauses while it runs: an // action it plays (a script step) must not flush the steps it ran so far into the action // being recorded, nor move the recording mark past the entries it is about to replace. let recording = if spec.journal && crate::actions_cmds::replayable(id) { self.actions.recording.take() } else { None }; + let before = self.active().map(|st| st.doc.clone()); // Last-resort guard (AGENTS.md, Never crash): a command that panics anyway fails with an // error instead of taking the app down. `edit` only commits a document after its closure // returns, so the documents are unchanged. + self.live_commit = live_commit.filter(|live| live.cmd == id); let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (spec.run)(self, &run_params))) .unwrap_or_else(|_| Err(EngineError::Other(format!("`{id}` failed with an internal error (logged); the document is unchanged")))); + self.live_commit = None; if self.actions.recording.is_none() { self.actions.recording = recording; } self.jobs.spawn = false; self.coalesce_request = None; self.color_restrict = None; + self.filter_mask_layer = None; + #[cfg(any(test, debug_assertions))] + { + self.hint_before = None; + } let pending = self.jobs.pending.take(); match (r, pending) { (Err(e), Some(job)) => { job.ctx.cancel(); Err(e) } - (Err(e), None) => Err(e), + (Err(e), None) => Err(self.explain_missing_layer(e)), (Ok(_), Some(mut job)) => { job.command = id.to_string(); job.params = params; job.journal = spec.journal; + job.recorded_by = self.actions.recording.map(|_| self.actions.take); + job.pre_selected = pre_selected.clone(); let jid = job.id; self.jobs.running.push(job); Ok(Started::Job(jid)) } (Ok(v), None) => { - self.after_command(id, params, spec.journal, Some(nested_from)); + if let Some(before) = before { + self.add_relayout_damage(&before); + } + if let Some((doc_id, layer_id)) = new_fx_target + && let Some(st) = self.active_mut() + && st.doc.id == doc_id + && st.doc.layer(layer_id).is_some_and(|l| !l.effects.items.is_empty()) + { + if expand_new_fx { + st.fx_collapsed.retain(|id| *id != layer_id); + } else if !st.fx_collapsed.contains(&layer_id) { + st.fx_collapsed.push(layer_id); + } + } + self.after_command(id, params, spec.journal, Some(nested_from), pre_selected.as_deref()); Ok(Started::Done(v)) } } } + /// Grows the damage a command reported for the pixels it changed by what effects laid out + /// over a layer's frame repaint when it moved ([`photocraft_compose::relayout_damage`]): a + /// stroke on a layer with a Gradient Overlay reports the stroke, but the gradient is laid out + /// again over the whole grown layer (#4053). `before` is the active document before. + fn add_relayout_damage(&mut self, before: &Arc) { + let Some(st) = self.active_mut().filter(|st| st.doc.id == before.id && !Arc::ptr_eq(&st.doc, before)) else { return }; + let Some(d) = st.last_damage.filter(|d| !d.is_empty()) else { return }; + let extra = photocraft_compose::relayout_damage(before, &st.doc); + if !extra.is_empty() { + st.last_damage = Some(d.union(&extra)); + } + } + + /// A command that failed on a layer id the document doesn't have says which ids it does have + /// (#5058): ids are not positions (an opened PNG's Background is not layer 1), and an agent + /// should not need `document.inspect` to recover. + fn explain_missing_layer(&self, e: EngineError) -> EngineError { + match (e, self.active()) { + (EngineError::NoLayer(id), Some(st)) if st.doc.layer(id).is_none() => { + EngineError::UnknownLayer { id, known: crate::commands::layer_listing(&st.doc) } + } + (e, _) => e, + } + } + /// Bookkeeping after a command (or a job's apply) succeeded. /// /// `nested_from` is the journal length when the command started. A replayable command stands @@ -763,12 +934,12 @@ impl Session { /// dropped, so a recorded action replays the command once, not its steps as well (#3067). /// Edit › Transform › Again (and Again on a Copy) is not replayable and keeps the concrete /// steps it ran instead. - fn after_command(&mut self, id: &str, params: Value, journal: bool, nested_from: Option) { + fn after_command(&mut self, id: &str, params: Value, journal: bool, nested_from: Option, pre_selected: Option<&[LayerId]>) { // A layer-mask view ends when another layer becomes active (#196). if let Some(st) = self.active_mut() { crate::mask_view_cmds::fix(st); } - crate::edit_menu_cmds::after_command(self, id); + crate::edit_menu_cmds::after_command(self, id, ¶ms); crate::automate_cmds::after_command(self, id); self.sync_preset_store(); if !journal { @@ -778,8 +949,13 @@ impl Session { self.journal.truncate(from); } if !crate::brush_cmds::coalesce_journal(self, id, ¶ms) { - let params = - if id == "layer.select" && self.actions.recording.is_some() { crate::layer_multi_cmds::recorded_selection(self, params) } else { params }; + let params = match id { + _ if self.actions.recording.is_none() => params, + "layer.duplicate" => crate::artboard_cmds::recorded_duplicate(self, crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected)), + "layerComp.apply" => crate::comps_cmds::recorded_apply(self, params), + _ => crate::layer_multi_cmds::recorded_step(self, id, params, pre_selected), + }; + let params = crate::actions_cmds::recorded_params(self, id, params); self.journal.push((id.to_string(), params)); } } @@ -801,7 +977,14 @@ impl Session { } fn apply_job(&mut self, job: &Running, apply: Option, payload: Payload) -> Result { - let Running { label, doc, snapshot, coalesce, color_restrict, command, params, journal, .. } = job; + let Running { label, doc, snapshot, coalesce, color_restrict, filter_mask_layer, layers, command, params, journal, recorded_by, pre_selected, .. } = + job; + // A recording started after the job (#7289) keeps what it has so far, and the job's step + // goes into the journal past its start, so that recording doesn't get it. + let foreign = self.actions.recording.is_some() && *recorded_by != Some(self.actions.take); + if foreign { + crate::actions_cmds::flush_recording(self)?; + } let prev_active = self.active; let target = match doc { Some(d) => { @@ -812,12 +995,27 @@ impl Session { return Err(EngineError::Other(format!("`{label}`: the document changed while it ran; the result was discarded"))); } self.active = Some(i); + self.note_active_change(); Some(i) } None => None, }; + // Selecting a layer doesn't change the document, so the snapshot check above lets it + // through. Apply against the layers the job started on: the colour restriction and any + // `active`-based edit land on them (#7366). The user's selection is put back below. + let user_layers = match (target, layers) { + (Some(i), Some(started)) => self.docs.get_mut(i).and_then(|st| { + let now = LayerSelection::of(st); + (now != *started).then(|| { + started.clone().set(st); + now + }) + }), + _ => None, + }; self.coalesce_request = coalesce.clone(); self.color_restrict = *color_restrict; + self.filter_mask_layer = *filter_mask_layer; // Apply-time hooks can dispatch commands too. Keep the same single-step recording // contract as synchronous dispatch without truncating work done while the worker ran. let nested_from = self.journal.len(); @@ -832,21 +1030,38 @@ impl Session { } self.coalesce_request = None; self.color_restrict = None; + self.filter_mask_layer = None; // Finish successful command bookkeeping on the document the job edited. // Restoring the viewed document first would attribute Fade and slice updates // to that document instead, when the user switched tabs mid-job. if r.is_ok() { - self.after_command(command, params.clone(), *journal, Some(nested_from)); + self.after_command(command, params.clone(), *journal, Some(nested_from), pre_selected.as_deref()); + if foreign && let Some((start, _)) = self.actions.recording.as_mut() { + *start = self.journal.len(); + } + } + // Give the user back the layers they selected while the job ran, unless the job chose a + // selection itself (a new layer, say) or one of their layers is gone. + if let (Some(started), Some(user), Some(st)) = (layers, user_layers, target.and_then(|i| self.docs.get_mut(i))) { + let kept = LayerSelection::of(st) == *started; + let exist = user.active.iter().chain(&user.selected).all(|id| st.doc.layer(*id).is_some()); + if (r.is_err() || kept) && exist { + user.set(st); + } } // Keep the user's active document unless the job opened a new one. // This also restores the selection when the apply step returned an error. if target.is_some() && self.active == target { self.active = prev_active.filter(|i| *i < self.docs.len()).or(self.active); + self.note_active_change(); } r } fn end_job(&mut self, job: &Running, outcome: JobOutcome) { + // Every terminal path invalidates a preview, including worker errors, cancellation + // and discarded results whose document was closed or changed. + self.live_commit = None; let (state, result, error) = match &outcome { JobOutcome::Done(v) => ("done", Some(v.clone()), None), JobOutcome::Failed(e) => ("failed", None, Some(e.clone())), @@ -912,5 +1127,54 @@ pub fn specs() -> Vec { } #[cfg(test)] -#[path = "jobs_tests.rs"] mod tests; + +#[cfg(test)] +#[path = "../../io/tests/support/recovery_psd.rs"] +mod psd_open_fixture; + +#[cfg(test)] +mod psd_open_confirmation_tests { + use super::*; + + /// Collects either an inline open result or the completed background-job result. + fn result(s: &mut Session, started: Started) -> Value { + match started { + Started::Done(v) => v, + Started::Job(id) => s.wait_job(id).unwrap(), + } + } + + /// Verifies headless opens return readable PSD content and warnings without a held prompt. + #[test] + fn headless_open_keeps_best_effort_psd_import() { + let mut s = Session::new(); + let started = s.start_open("damaged.psd", OpenSource::Bytes(Arc::new(super::psd_open_fixture::make_psd(true)))).unwrap(); + let v = result(&mut s, started); + assert!(v.get("document").is_some(), "{v}"); + assert!(v.get("held").is_none(), "no new prompt for a headless caller"); + assert!(!s.active().unwrap().source_read_only); + assert!(v["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().contains("could not be decoded"))); + } + + /// Verifies interactive opens hold damaged PSD files but admit valid files immediately. + #[test] + fn interactive_open_holds_only_a_damaged_psd() { + for corrupt in [false, true] { + let mut s = Session::new(); + let started = s.start_open_for_user("file.psd", OpenSource::Bytes(Arc::new(super::psd_open_fixture::make_psd(corrupt)))).unwrap(); + let v = result(&mut s, started); + if corrupt { + assert_eq!(v["damaged"], true); + assert!(s.documents().is_empty()); + let held = v["held"].as_u64().unwrap(); + let (doc, read_only) = s.take_held_open(held).unwrap(); + assert_eq!(doc.layers.len(), 2); + assert!(!read_only); + } else { + assert!(v.get("document").is_some(), "{v}"); + assert!(!s.active().unwrap().source_read_only); + } + } + } +} diff --git a/crates/engine/src/jobs_tests.rs b/crates/engine/src/jobs/tests.rs similarity index 76% rename from crates/engine/src/jobs_tests.rs rename to crates/engine/src/jobs/tests.rs index 2bcdda76fc..0974a5c17a 100644 --- a/crates/engine/src/jobs_tests.rs +++ b/crates/engine/src/jobs/tests.rs @@ -94,6 +94,36 @@ fn free_transform_runs_as_a_job_with_the_inline_result() { assert_eq!(pixels(&s), pixels(&inline), "same pixels as the synchronous command"); } +#[test] +fn warp_runs_as_a_job_with_the_inline_result() { + let arc = json!({"style": "arc", "bend": 30}); + let mut inline = session(300, 200); + inline.execute("edit.transform.warp", arc.clone()).unwrap(); + + let mut s = session(300, 200); + let steps = s.active().unwrap().history.past_len(); + let id = job(s.start("edit.transform.warp", arc).unwrap()); + let e = wait_event(&mut s, id); + assert!(matches!(e.outcome, JobOutcome::Done(_)), "{e:?}"); + assert_eq!(e.label, "Warp"); + assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "one undo step"); + assert_eq!(pixels(&s), pixels(&inline), "same pixels as the synchronous command"); +} + +#[test] +fn a_cancelled_warp_leaves_the_document_unchanged() { + let mut s = session(1600, 1200); + let doc_before = s.active().unwrap().doc.clone(); + let steps = s.active().unwrap().history.past_len(); + let id = job(s.start("edit.transform.warp", json!({"style": "flag", "bend": 40})).unwrap()); + assert!(s.cancel_job(id)); + assert_eq!(wait_event(&mut s, id).outcome, JobOutcome::Cancelled); + s.join_cancelled_jobs(); + let st = s.active().unwrap(); + assert!(Arc::ptr_eq(&st.doc, &doc_before), "the document snapshot was not replaced"); + assert_eq!(st.history.past_len(), steps); +} + #[test] fn cancel_leaves_the_document_byte_identical() { let mut s = session(1600, 1200); @@ -322,6 +352,56 @@ fn background_job_finishes_bookkeeping_on_edited_document_after_tab_switch() { assert!(s.is_enabled("edit.fade"), "Fade must be available on the document that was edited"); } +/// #7382: a background filter on a targeted alpha channel or the Quick Mask, finished while another +/// document is shown, leaves Fade on that channel of the edited document: at 0 % it restores the +/// channel exactly and leaves the layer alone. +#[test] +fn background_filter_on_a_channel_fades_that_channel_after_a_tab_switch() { + use crate::edit_menu_cmds::FadeTarget; + for quick_mask in [false, true] { + let mut s = session(32, 24); + let edited_doc = s.active().unwrap().doc.id; + if quick_mask { + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + } else { + s.execute("channel.new", json!({"fill": "black"})).unwrap(); + s.execute("channel.target", json!({"channel": 0})).unwrap(); + } + // A pattern for the blur to change. + s.edit("pattern", |doc, _| { + let c = if quick_mask { doc.quick_mask.as_mut() } else { doc.channels.first_mut() }; + let c = c.ok_or_else(|| EngineError::Other("no channel".into()))?; + c.surface.fill_rect(photocraft_geom::Rect::new(0, 0, 32, 24), &[0.0]); + c.surface.fill_rect(photocraft_geom::Rect::new(8, 6, 20, 16), &[1.0]); + Ok(()) + }) + .unwrap(); + let channel = |s: &Session, d: usize| { + let doc = &s.documents()[d].doc; + let c = if quick_mask { doc.quick_mask.as_ref() } else { doc.channels.first() }; + let c = c.unwrap(); + (0..24).flat_map(|y| (0..32).map(move |x| (x, y))).map(|(x, y)| c.surface.pixel(x, y)[0]).collect::>() + }; + let layer = |s: &Session| { + let st = &s.documents()[0]; + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(5, 5) + }; + let (pattern, pixels) = (channel(&s, 0), layer(&s)); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap()); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let event = wait_event(&mut s, id); + assert!(matches!(event.outcome, JobOutcome::Done(_)), "{event:?}"); + assert_ne!(channel(&s, 0), pattern, "quick mask {quick_mask}: the blur changed the channel"); + let fade = s.edit_state.fade.clone().unwrap(); + assert_eq!(fade.doc, edited_doc); + assert_eq!(fade.target, if quick_mask { FadeTarget::QuickMask } else { FadeTarget::Channel(0) }); + assert!(s.set_active(0)); + s.execute("edit.fade", json!({"opacity": 0})).unwrap(); + assert_eq!(channel(&s, 0), pattern, "quick mask {quick_mask}: Fade at 0 % restores the channel"); + assert_eq!(layer(&s), pixels, "quick mask {quick_mask}: the layer is left alone"); + } +} + #[test] fn closing_the_document_cancels_its_job() { let mut s = session(1600, 1200); @@ -605,3 +685,100 @@ fn an_inline_job_ctx_lets_another_thread_cancel_inline_work() { s.set_inline_job_ctx(None); s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); } + +/// The command ids an action holds. +fn action_steps(s: &mut Session, name: &str) -> Vec { + let a = s.execute("actions.get", json!({"action": name})).unwrap(); + a["steps"].as_array().unwrap().iter().map(|step| step[0].as_str().unwrap().to_string()).collect() +} + +/// A job's result is recorded only by the recording it was started in: one started later keeps +/// its own steps around it but doesn't pick up the older edit (#7289). +#[test] +fn a_job_lands_in_the_recording_it_was_started_in_only() { + // Started while recording A, applied after A stopped and B started. + let mut s = session(40, 30); + s.execute("actions.record", json!({"name": "A"})).unwrap(); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.execute("actions.stop", json!({})).unwrap(); + s.execute("actions.record", json!({"name": "B"})).unwrap(); + // B's own steps before and after the result lands stay in B (a new document, so the job's + // document is unchanged and its result applies). + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.wait_job(id).unwrap(); + s.execute("image.adjustments.desaturate", json!({})).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert!(action_steps(&mut s, "A").is_empty()); + assert_eq!(action_steps(&mut s, "B"), ["file.new", "image.adjustments.desaturate"]); + + // Started before any recording, applied while B records. + let mut s = session(40, 30); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.execute("actions.record", json!({"name": "B"})).unwrap(); + s.wait_job(id).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert!(action_steps(&mut s, "B").is_empty()); + + // Started and applied while A records: A has it, as before. + let mut s = session(40, 30); + s.execute("actions.record", json!({"name": "A"})).unwrap(); + let id = job(s.start("filter.blur.gaussianBlur", json!({"radius": 1})).unwrap()); + s.wait_job(id).unwrap(); + s.execute("actions.stop", json!({})).unwrap(); + assert_eq!(action_steps(&mut s, "A"), ["filter.blur.gaussianBlur"]); +} + +/// Exercise six terminal job routes so prepared paint snapshots cannot outlive a completed or discarded job. +#[test] +fn every_job_terminal_path_drops_a_live_commit_stroke() { + use crate::brush_cmds::mixer_live::LiveMixer; + use std::sync::atomic::{AtomicBool, Ordering}; + + for outcome in 0..6 { + let mut s = session(32, 32); + let live = LiveMixer::begin(&s, &json!({"points": [[16, 16]], "size": 4})).unwrap(); + live.prepare_commit(&mut s).unwrap(); + let gate = Arc::new(AtomicBool::new(false)); + let worker_gate = gate.clone(); + let id = job(s + .start_job( + "test.mixer_cleanup", + json!({}), + "Mixer cleanup", + true, + move |ctx| { + while !worker_gate.load(Ordering::Relaxed) { + ctx.check()?; + std::thread::sleep(Duration::from_millis(1)); + } + if outcome == 1 { Err(EngineError::Other("worker failed".into())) } else { Ok(()) } + }, + move |_, ()| { + if outcome == 4 { Err(EngineError::Other("apply failed".into())) } else { Ok(json!({})) } + }, + ) + .unwrap()); + assert!(s.live_commit.is_some()); + if outcome == 2 { + assert!(s.cancel_job(id)); + s.join_cancelled_jobs(); + } else { + if outcome == 3 { + // Simulate replacement outside command dispatch, which itself clears offers. + let doc = (*s.active().unwrap().doc).clone(); + s.active_mut().unwrap().doc = Arc::new(doc); + } else if outcome == 5 { + s.docs.clear(); + s.active = None; + } + gate.store(true, Ordering::Relaxed); + } + let event = wait_event(&mut s, id); + match outcome { + 0 => assert!(matches!(event.outcome, JobOutcome::Done(_))), + 2 => assert!(matches!(event.outcome, JobOutcome::Cancelled)), + _ => assert!(matches!(event.outcome, JobOutcome::Failed(_))), + } + assert!(s.live_commit.is_none(), "terminal path {outcome} retained the preview"); + } +} diff --git a/crates/engine/src/kys.rs b/crates/engine/src/kys.rs index 6a0ff99c84..1406e0a384 100644 --- a/crates/engine/src/kys.rs +++ b/crates/engine/src/kys.rs @@ -49,6 +49,18 @@ pub struct KysSet { pub windows: bool, } +/// The set name in the root `filename` attribute (`$$$/.../{name}={name}`). The name may itself +/// contain `/` or `=`, so split on the key prefix and the doubled name, not on the last separator. +fn filename_to_name(f: &str) -> &str { + let Some(rest) = f.strip_prefix("$$$/") else { return f.rsplit(['=', '/']).next().unwrap_or(f).trim() }; + let rest = rest.strip_prefix("FileName/Presets/KeyboardCustomization/").unwrap_or(rest); + let mid = rest.len() / 2; + if rest.len() % 2 == 1 && rest.is_char_boundary(mid) && rest.as_bytes()[mid] == b'=' && rest[..mid] == rest[mid + 1..] { + return rest[..mid].trim(); + } + rest.split_once('=').map_or(rest, |(_, n)| n).trim() +} + /// Parse a `.kys` file (or Photoshop's live `Keyboard Shortcuts.psp`, the same XML). Rows /// without a name or a shortcut are skipped. pub fn parse(text: &str) -> Result { @@ -56,12 +68,12 @@ pub fn parse(text: &str) -> Result { return Err("not a keyboard shortcut set (.kys): larger than 4 MiB".into()); } let text = text.trim_start_matches('\u{feff}'); - let doc = roxmltree::Document::parse(text).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?; + let doc = photocraft_io::xml::parse(text, roxmltree::ParsingOptions::default()).map_err(|e| format!("not a keyboard shortcut set (.kys): {e}"))?; let root = doc.root_element(); if root.tag_name().name() != "photoshop-keyboard-shortcuts" { return Err("not a keyboard shortcut set (.kys): no root".into()); } - let name = root.attribute("filename").map(|f| f.rsplit(['=', '/']).next().unwrap_or(f).trim()).filter(|n| !n.is_empty()).map(str::to_string); + let name = root.attribute("filename").map(filename_to_name).filter(|n| !n.is_empty()).map(str::to_string); let mut set = KysSet { name, ..Default::default() }; for node in root.children().filter(|n| n.is_element()) { match node.tag_name().name() { @@ -119,6 +131,7 @@ pub fn to_ours(set: &KysSet, sc: &str) -> Option { } let (mods, key) = match sc.strip_suffix("++") { Some(m) => (m, "+"), + None if sc == "+" => ("", "+"), None => sc.rsplit_once('+').unwrap_or(("", sc)), }; let mods: Vec<&str> = mods @@ -262,6 +275,7 @@ pub fn photoshop_key(sc: &str) -> Option { // The key is the last part; `Cmd++` ends in an empty part, so take it from the string. let (mods, key) = match n.strip_suffix("++") { Some(m) => (m, "+"), + None if n == "+" => ("", "+"), None => n.rsplit_once('+').unwrap_or(("", n.as_str())), }; let has = |m: &str| mods.split('+').any(|x| x == m); @@ -309,6 +323,13 @@ pub fn write(name: &str, entries: &[Entry]) -> String { mod tests { use super::*; + #[test] + fn set_name_with_separators_round_trips() { + for n in ["Retouch 1/2", "a=b", "x/y=z"] { + assert_eq!(parse(&write(n, &[])).unwrap().name.as_deref(), Some(n)); + } + } + const SET: &str = "\u{feff} \tCmd+L @@ -323,6 +344,28 @@ mod tests { Candidate { id: id.into(), label: label.into(), default: default.map(str::to_string) } } + #[test] + fn a_bare_plus_key_is_kept_when_saving() { + assert_eq!(photoshop_key("+").as_deref(), Some("+")); + assert_eq!(photoshop_key("Cmd++").as_deref(), Some("Cmd++")); + let e = Entry { id: "tools.zoom".into(), label: "Zoom Tool".into(), tool: true, keys: vec!["+".into()] }; + assert!(write("Mine", &[e]).contains(">+")); + } + + #[test] + fn a_bare_plus_command_survives_a_windows_set_with_a_ctrl_row() { + let xml = "\ + Ctrl+C\ + +\ + "; + let set = parse(xml).unwrap(); + assert!(set.windows); + assert_eq!(to_ours(&set, "+").as_deref(), Some("+")); + let p = plan([c("edit.copy", "Copy", Some("Cmd+C")), c("view.zoomIn", "Zoom In", None)], &set); + assert_eq!(p.set.get("view.zoomIn").map(String::as_str), Some("+")); + assert!(p.unreadable.is_empty()); + } + #[test] fn rows_match_by_label_and_the_default_holder_wins_a_shared_label() { let set = parse(SET).unwrap(); @@ -342,6 +385,15 @@ mod tests { assert_eq!(p.alternate_keys.get("image.levels"), Some(&Vec::new())); } + /// A set nested 20 000 deep overflowed the XML parser's recursion (an abort nothing can + /// catch); it is an error now. The small stack makes a regression abort instead of passing. + #[test] + fn deeply_nested_sets_are_an_error_not_a_stack_overflow() { + let xml = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || parse(&xml).err()).unwrap().join().unwrap(); + assert!(e.as_deref().is_some_and(|m| m.contains("nested")), "{e:?}"); + } + #[test] fn a_row_with_the_default_and_a_function_key_keeps_both() { let xml = diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rs deleted file mode 100644 index 1b71e966a4..0000000000 --- a/crates/engine/src/layer_multi_cmds.rs +++ /dev/null @@ -1,2728 +0,0 @@ -//! Multi-layer selection and the commands that act on it: ⌘/⇧-click selection, Select › All / -//! Deselect / Find Layers, Layer › Align and Distribute, Link Layers, Merge Layers, Group from -//! Layers, Arrange › Reverse, Lock Layers and Rename Layer. -//! -//! The selection itself lives in [`DocState::selected_layers`] (UI state, like the active layer): -//! changing it never adds a history step. Every structural change below is one history step. - -use photocraft_color::PixelFormat; -use photocraft_doc::{Document, Layer, LayerContent, LayerId}; -use photocraft_geom::Rect; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, int_i32}; -use crate::{DocState, EngineError, Result, Session}; - -// ---------- selection state ---------- - -fn doc_state(s: &Session) -> std::result::Result<&DocState, String> { - s.active().ok_or_else(|| "no document open".into()) -} - -/// Selected layers of the active document, bottom-to-top. -pub fn selected(s: &Session) -> Vec { - s.active().map(DocState::selected_layers).unwrap_or_default() -} - -/// The layers a layer command acts on: the `layer` param when given, else every selected layer -/// (bottom-to-top), else the active layer. A single layer behaves exactly as before. -pub(crate) fn targets(s: &Session, p: &serde_json::Value) -> Result> { - if p.get("layer").is_some() { - return Ok(vec![crate::commands::layer_param(s, p)?]); - } - match selected(s) { - sel if sel.is_empty() => Ok(vec![crate::commands::layer_param(s, p)?]), - sel => Ok(sel), - } -} - -/// [`targets`] for a command that adds a layer mask: with several layers selected, those that can -/// take one (no layer mask yet, not the Background); one layer is taken as given. -pub(crate) fn mask_targets(s: &Session, p: &serde_json::Value) -> Result> { - let ids = targets(s, p)?; - if ids.len() < 2 { - return Ok(ids); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let ids: Vec = ids.into_iter().filter(|id| d.doc.layer(*id).is_some_and(|l| l.mask.is_none() && !d.doc.is_background(l.id))).collect(); - if ids.is_empty() { - return Err(EngineError::Other("none of the selected layers can take a layer mask".into())); - } - Ok(ids) -} - -/// Ids in `ids` that have no ancestor also in `ids` (a selected group already carries its -/// selected children), bottom-to-top. -pub(crate) fn top_level(doc: &Document, ids: &[LayerId]) -> Vec { - let walk = doc.walk(); - let paths: Vec<_> = walk.iter().filter(|(_, _, l)| ids.contains(&l.id)).map(|(p, _, l)| (p.clone(), l.id)).collect(); - paths.iter().filter(|(p, _)| !paths.iter().any(|(q, _)| q.len() < p.len() && p.starts_with(q))).map(|(_, id)| *id).collect() -} - -/// Replace the layer selection without a history step (like [`Session::select_layer`]). -pub fn set_selection(s: &mut Session, ids: Vec, active: Option, anchor: Option) -> Result<()> { - let st = s.active_mut().ok_or(EngineError::NoDocument)?; - if let Some(bad) = ids.iter().chain(active.iter()).find(|id| st.doc.layer(**id).is_none()) { - return Err(EngineError::NoLayer(*bad)); - } - let active = active.or_else(|| ids.last().copied()); - st.selected_layers = ids; - if let Some(a) = active - && !st.selected_layers.contains(&a) - { - st.selected_layers.push(a); - } - st.active_layer = active; - st.layer_anchor = anchor.or(active); - crate::fix_selection(st); - // Selecting layers is not an edit: keep a clean document clean. - let clean = st.saved_revision == st.revision; - st.revision += 1; - if clean { - st.saved_revision = st.revision; - } - st.last_damage = Some(Rect::EMPTY); - Ok(()) -} - -/// After a structural edit, make `ids` the selection (dropping any that no longer exist), which -/// is also what the edit's history state targets. -pub(crate) fn reselect(s: &mut Session, ids: Vec, active: Option) { - if let Some(st) = s.active_mut() { - st.selected_layers = ids; - if let Some(a) = active { - st.active_layer = Some(a); - st.layer_anchor = Some(a); - } - crate::fix_selection(st); - let layers = st.layer_target(); - st.history.set_current_layers(layers); - } -} - -/// Capture a selection target while its name is still known. Later renames/deletions must not -/// change an earlier recorded selection. Direct, non-recording calls retain their numeric IDs. -pub(crate) fn recorded_selection(s: &Session, mut p: Value) -> Value { - let Some(id) = p.get("layer").and_then(Value::as_u64).map(LayerId) else { return p }; - let Some(st) = s.active() else { return p }; - let Some(layer) = st.doc.layer(id) else { return p }; - let matches: Vec<_> = st.doc.walk().into_iter().filter(|(_, _, l)| l.name == layer.name).map(|(_, _, l)| l.id).collect(); - let Some(occurrence) = matches.iter().position(|candidate| *candidate == id) else { return p }; - if let Some(fields) = p.as_object_mut() { - fields.remove("layer"); - fields.insert("name".into(), json!(layer.name)); - if matches.len() > 1 { - fields.insert("occurrence".into(), json!(occurrence)); - } - } - p -} - -fn selection_target(s: &Session, p: &Value) -> Result { - let Some(name) = p.get("name") else { - if p.get("occurrence").is_some() { - return Err(bad("layer.select", "`occurrence` requires `name`")); - } - return crate::commands::explicit_layer(s, p)?.ok_or_else(|| bad("layer.select", "needs `layer` or `name`")); - }; - if p.get("layer").is_some() { - return Err(bad("layer.select", "use either `layer` or `name`, not both")); - } - let name = name.as_str().ok_or_else(|| bad("layer.select", "`name` must be a string"))?; - let occurrence = p - .get("occurrence") - .map(|v| v.as_u64().and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad("layer.select", "`occurrence` must be a non-negative integer"))) - .transpose()?; - let st = s.active().ok_or(EngineError::NoDocument)?; - let matches: Vec<_> = st.doc.walk().into_iter().filter(|(_, _, l)| l.name == name).map(|(_, _, l)| l.id).collect(); - if matches.len() > 1 && occurrence.is_none() { - return Err(bad("layer.select", format!("multiple layers named `{name}`; specify `occurrence`"))); - } - let index = occurrence.unwrap_or(0); - matches.get(index).copied().ok_or_else(|| bad("layer.select", format!("no layer named `{name}` at occurrence {index}"))) -} - -/// Layer rows follow the Layers panel's visual order, not Document::walk's storage order. -/// Closed groups hide their children unless both endpoints were selected programmatically. -fn range_order(layers: &[Layer], include_collapsed: bool, out: &mut Vec) { - for layer in layers.iter().rev() { - out.push(layer.id); - if let LayerContent::Group(group) = &layer.content - && (group.expanded || include_collapsed) - { - range_order(&group.children, include_collapsed, out); - } - } -} - -/// `layer.select`: `{"layer": id, "mode": "replace|toggle|range|add|remove"}`. -pub fn select(s: &mut Session, p: &Value) -> Result { - let id = selection_target(s, p)?; - let mode = p.get("mode").and_then(Value::as_str).unwrap_or("replace"); - let st = s.active().ok_or(EngineError::NoDocument)?; - if st.doc.layer(id).is_none() { - return Err(EngineError::NoLayer(id)); - } - let mut sel = st.selected_layers(); - match mode { - "replace" => s.select_layer(id)?, - "add" => { - if !sel.contains(&id) { - sel.push(id); - } - set_selection(s, sel, Some(id), Some(id))?; - } - "toggle" => { - if sel.contains(&id) { - if sel.len() == 1 { - // Photoshop keeps at least the clicked layer targeted. - return Ok(json!({"selected": [id.0]})); - } - sel.retain(|x| *x != id); - let active = if st.active_layer == Some(id) { sel.last().copied() } else { st.active_layer }; - set_selection(s, sel, active, active)?; - } else { - sel.push(id); - set_selection(s, sel, Some(id), Some(id))?; - } - } - // Toggle's removing half: a layer that isn't selected stays unselected (Photoshop's - // `removeFromSelection`, recorded by actions). - "remove" => { - if sel.contains(&id) && sel.len() > 1 { - sel.retain(|x| *x != id); - let active = if st.active_layer == Some(id) { sel.last().copied() } else { st.active_layer }; - set_selection(s, sel, active, active)?; - } - } - "range" => { - let anchor = st.layer_anchor.filter(|a| st.doc.layer(*a).is_some()).or(st.active_layer).unwrap_or(id); - let mut order = Vec::new(); - range_order(&st.doc.layers, false, &mut order); - // Engine clients may target a child of a collapsed group that the panel cannot - // click. Retain that capability by using expanded visual order for such calls. - if !order.contains(&anchor) || !order.contains(&id) { - order.clear(); - range_order(&st.doc.layers, true, &mut order); - } - let (a, b) = (order.iter().position(|x| *x == anchor).unwrap_or(0), order.iter().position(|x| *x == id).unwrap_or(0)); - let range = order[a.min(b)..=a.max(b)].to_vec(); - set_selection(s, range, Some(id), Some(anchor))?; - } - other => return Err(bad("layer.select", format!("unknown mode `{other}`"))), - } - Ok(json!({"selected": selected(s).iter().map(|l| l.0).collect::>()})) -} - -fn select_all_layers(s: &mut Session) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let ids: Vec = d.doc.walk().into_iter().filter(|(_, _, l)| !d.doc.is_background(l.id)).map(|(_, _, l)| l.id).collect(); - if ids.is_empty() { - return Err(EngineError::Other("there are no layers to select".into())); - } - let active = d.active_layer.filter(|a| ids.contains(a)).or(ids.last().copied()); - let n = ids.len(); - set_selection(s, ids, active, None)?; - Ok(json!({"selected": n})) -} - -fn find_layers(s: &mut Session, p: &Value) -> Result { - let needle = p.get("name").and_then(Value::as_str).ok_or_else(|| bad("select.findLayers", "missing `name`"))?.to_lowercase(); - let d = s.active().ok_or(EngineError::NoDocument)?; - let ids: Vec = d.doc.walk().into_iter().filter(|(_, _, l)| l.name.to_lowercase().contains(&needle)).map(|(_, _, l)| l.id).collect(); - if ids.is_empty() { - return Err(EngineError::Other(format!("no layer name contains \"{needle}\""))); - } - let out = ids.iter().map(|l| l.0).collect::>(); - set_selection(s, ids, None, None)?; - Ok(json!({"selected": out})) -} - -fn select_linked(s: &mut Session) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let groups = link_groups(&d.doc, &d.selected_layers()); - let ids: Vec = d.doc.walk().into_iter().filter(|(_, _, l)| l.link_group.is_some_and(|g| groups.contains(&g))).map(|(_, _, l)| l.id).collect(); - let active = d.active_layer.filter(|a| ids.contains(a)).or(ids.last().copied()); - let n = ids.len(); - set_selection(s, ids, active, active)?; - Ok(json!({"selected": n})) -} - -// ---------- enabled predicates ---------- - -fn has_layer(s: &Session) -> std::result::Result<(), String> { - if selected(s).is_empty() { Err("no layer selected".into()) } else { Ok(()) } -} -fn has_doc(s: &Session) -> std::result::Result<(), String> { - doc_state(s).map(|_| ()) -} -fn two_plus(s: &Session) -> std::result::Result<(), String> { - if selected(s).len() >= 2 { Ok(()) } else { Err("select two or more layers".into()) } -} -fn can_align(s: &Session) -> std::result::Result<(), String> { - let d = doc_state(s)?; - match d.selected_layers().len() { - 0 => Err("no layer selected".into()), - // Photoshop aligns to an active selection however many layers are selected; without one, - // a single layer aligns to the canvas and 2+ align to each other. - _ => Ok(()), - } -} -fn can_distribute(s: &Session) -> std::result::Result<(), String> { - if selected(s).len() >= 3 { Ok(()) } else { Err("select three or more layers to distribute".into()) } -} -fn can_link(s: &Session) -> std::result::Result<(), String> { - let d = doc_state(s)?; - let sel = d.selected_layers(); - match sel.len() { - 0 => Err("no layer selected".into()), - 1 if d.doc.layer(sel[0]).is_none_or(|l| l.link_group.is_none()) => Err("select two or more layers to link".into()), - _ => Ok(()), - } -} -fn has_linked(s: &Session) -> std::result::Result<(), String> { - let d = doc_state(s)?; - if link_groups(&d.doc, &d.selected_layers()).is_empty() { Err("the selected layers are not linked".into()) } else { Ok(()) } -} - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -// ---------- moving layers ---------- - -fn link_groups(doc: &Document, ids: &[LayerId]) -> Vec { - let mut g: Vec = ids.iter().filter_map(|id| doc.layer(*id)?.link_group).collect(); - g.sort_unstable(); - g.dedup(); - g -} - -/// `ids` plus every layer linked to one of them. -pub(crate) fn with_links(doc: &Document, ids: &[LayerId]) -> Vec { - let groups = link_groups(doc, ids); - let mut out = ids.to_vec(); - if !groups.is_empty() { - for (_, _, l) in doc.walk() { - if l.link_group.is_some_and(|g| groups.contains(&g)) && !out.contains(&l.id) { - out.push(l.id); - } - } - } - out -} - -/// Move layers by whole pixels the way the Move tool does: pixels, linked masks, type and -/// vector content (shapes, linked vector masks) all move together. `before` is `doc` as it was -/// before any of the moves (the edit's starting document), read when type and shapes re-render. -pub(crate) fn move_layers(doc: &mut Document, before: &Document, moves: &[(LayerId, i32, i32)]) -> Result<()> { - for &(id, dx, dy) in moves { - if dx == 0 && dy == 0 { - continue; - } - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.position || locks.all { - return Err(EngineError::Other(format!("layer \"{}\" is position-locked", l.name))); - } - crate::commands::translate_layer(before, l, dx, dy); - crate::vector_cmds::translate_vectors(before, l, dx as f64, dy as f64); - } - Ok(()) -} - -/// Does a move aimed at the layer mask (`"target":"mask"`) move `l`'s mask alone? Only an -/// unlinked mask does (the chain off); a linked one moves with its layer, as in Photoshop. -pub fn moves_mask_alone(l: &Layer) -> bool { - l.mask.as_ref().is_some_and(|m| !m.linked) -} - -/// `layer.translate`: the explicit layer, or every selected layer, plus their linked layers. -/// With `"target":"mask"` a layer whose mask is unlinked moves that mask alone. -pub fn translate(s: &mut Session, p: &Value) -> Result { - let (dx, dy) = match p.get("units").and_then(Value::as_str) { - None | Some("px") => (int_i32("layer.translate", p, "dx")?.unwrap_or(0), int_i32("layer.translate", p, "dy")?.unwrap_or(0)), - // Points (1/72 in), as a Photoshop action records a nudge: whole pixels at this - // document's resolution. - Some("pt") => { - let dpi = s.active().map_or(72.0, |d| f64::from(d.doc.resolution_dpi)); - let to_px = |key: &str| -> Result { - let pt = match p.get(key) { - None => 0.0, - Some(v) => v.as_f64().filter(|f| f.is_finite()).ok_or_else(|| bad("layer.translate", format!("`{key}` must be a number")))?, - }; - let px = (pt * dpi / 72.0).round(); - if px.abs() > f64::from(i32::MAX) { - return Err(bad("layer.translate", format!("`{key}` = {pt} pt is outside the 32-bit coordinate range"))); - } - Ok(px as i32) - }; - (to_px("dx")?, to_px("dy")?) - } - Some(other) => return Err(bad("layer.translate", format!("unknown units `{other}` (px|pt)"))), - }; - if dx == 0 && dy == 0 { - return Ok(Value::Null); - } - let roots = match crate::commands::explicit_layer(s, p)? { - Some(id) => vec![id], - None => selected(s), - }; - if roots.is_empty() { - return Err(EngineError::Other("no active layer".into())); - } - let mask_target = p.get("target").and_then(Value::as_str) == Some("mask"); - let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let ids = s.edit("Move", |doc, _| { - let (alone, rest): (Vec, Vec) = roots.iter().partition(|id| mask_target && doc.layer(**id).is_some_and(moves_mask_alone)); - let mut ids = move_targets(doc, &rest); - if ids.is_empty() && alone.is_empty() { - return Err(EngineError::NoLayer(roots[0])); - } - let moves: Vec<_> = ids.iter().map(|&id| (id, dx, dy)).collect(); - move_layers(doc, &before, &moves)?; - for id in alone { - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.position || locks.all { - return Err(EngineError::Other(format!("layer \"{}\" is position-locked", l.name))); - } - if let Some(m) = &mut l.mask { - m.surface = shift_surface(&m.surface, dx, dy); - } - ids.push(id); - } - // A board moved past any canvas edge grows the canvas, as a new one does (#1531, #3070). - if ids.iter().any(|id| doc.layer(*id).is_some_and(|l| l.artboard().is_some())) { - crate::artboard_cmds::fit_canvas(doc); - } - Ok(ids) - })?; - note_damage(s, &before, &ids); - Ok(Value::Null) -} - -/// The layers a move of `roots` takes along: their linked layers, without layers whose group -/// is also moving (the group carries them). -pub fn move_targets(doc: &Document, roots: &[LayerId]) -> Vec { - top_level(doc, &with_links(doc, roots)) -} - -/// `doc` with the layers `ids` ([`move_targets`]) moved by whole pixels, as `layer.translate` -/// leaves them: the Move tool's live preview while it drags. Type, shape and smart-object pixels -/// shift as they are instead of re-rendering (the commit re-renders them), so a drag frame costs -/// copying the moving pixels only. -pub fn moved(doc: &Document, ids: &[LayerId], dx: i32, dy: i32) -> Result { - moved_target(doc, ids, dx, dy, false) -} - -/// [`moved`] for a drag aimed at the layer mask (`mask_target`): unlinked masks move alone. -pub fn moved_target(doc: &Document, ids: &[LayerId], dx: i32, dy: i32, mask_target: bool) -> Result { - let mut out = doc.clone(); - for &id in ids { - let locks = doc.effective_locks(id); - let l = out.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.position || locks.all { - return Err(EngineError::Other(format!("layer \"{}\" is position-locked", l.name))); - } - if mask_target && moves_mask_alone(l) { - if let Some(m) = &mut l.mask { - m.surface = shift_surface(&m.surface, dx, dy); - } - continue; - } - shift_shown(doc, l, dx, dy); - } - Ok(out) -} - -/// `photocraft_algo::resample::translate_surface` with the content scan cached per tile (a Move -/// drag shifts the same layers every frame; scanning them each time cost more than the copy). -pub(crate) fn shift_surface(s: &photocraft_raster::Surface, dx: i32, dy: i32) -> photocraft_raster::Surface { - s.translated(dx, dy, photocraft_compose::bounds::content_bounds(s)) -} - -/// [`crate::commands::translate_layer`] plus the vector side, without re-rendering anything. -fn shift_shown(doc: &Document, l: &mut Layer, dx: i32, dy: i32) { - use self::shift_surface as translate_surface; - let a = photocraft_geom::Affine::translate(f64::from(dx), f64::from(dy)); - if let Some(r) = &mut l.effects.reference { - *r = (r.0 + f64::from(dx), r.1 + f64::from(dy)); - } - if let Some(m) = l.mask.as_mut().filter(|m| m.linked) { - m.surface = translate_surface(&m.surface, dx, dy); - } - if let Some(vm) = l.vector_mask.as_mut().filter(|v| v.linked) { - vm.path = vm.path.transform(&a); - } - match &mut l.content { - LayerContent::Raster(s) => *s = translate_surface(s, dx, dy), - LayerContent::Text(t) => { - t.transform = a.mul(&t.transform); - if let Some(c) = &mut t.cache { - *c = translate_surface(c, dx, dy); - } - } - LayerContent::Shape(sh) => { - // The rendered shape is cut at the canvas: one that reaches past it renders again. - let canvas = doc.bounds(); - let was_inside = sh.cache.as_ref().is_some_and(|c| { - let b = photocraft_compose::bounds::content_bounds(c); - b.is_empty() || (b.x0 > canvas.x0 && b.y0 > canvas.y0 && b.x1 < canvas.x1 && b.y1 < canvas.y1) - }); - crate::vector_cmds::transform_shape(sh, &a); - let stroke_pad = sh.stroke.as_ref().filter(|s| s.opacity > 0.0).map_or(0.0, |s| f64::from(s.width.ceil())); - let stays_inside = sh.path.control_bounds().is_some_and(|(x0, y0, x1, y1)| { - (x0 - stroke_pad) > canvas.x0 as f64 - && (y0 - stroke_pad) > canvas.y0 as f64 - && (x1 + stroke_pad) < canvas.x1 as f64 - && (y1 + stroke_pad) < canvas.y1 as f64 - }); - match &mut sh.cache { - Some(c) if was_inside && stays_inside => *c = translate_surface(c, dx, dy), - _ => crate::vector_cmds::refresh_shape(doc, sh), - } - } - LayerContent::Smart(sm) => crate::smart_cmds::shift_smart(sm, dx, dy), - LayerContent::Group(g) => { - if let Some(ab) = &mut g.artboard { - ab.rect = ab.rect.translate(dx, dy); - } - g.children.iter_mut().for_each(|c| shift_shown(doc, c, dx, dy)); - } - LayerContent::Adjustment(_) | LayerContent::Fill(_) => {} - } -} - -/// The document pixels an edit of the layers `ids` (moved, shown, hidden or restyled; contents -/// otherwise as they were) can have changed from `before` to `after`, or `None` for anywhere. -pub fn layers_damage(before: &Document, after: &Document, ids: &[LayerId]) -> Option { - let mut out = Rect::EMPTY; - for d in [before, after] { - let canvas = d.bounds(); - for id in ids { - // A layer gone from one side (never for a move) changes who knows what. - let b = photocraft_compose::change_bounds(d.layer(*id)?, canvas)?; - if !b.is_empty() { - out = if out.is_empty() { b } else { out.union(&b) }; - } - } - } - Some(out) -} - -/// Report [`layers_damage`] as the active document's last change, so the canvas recomposites -/// only that (grown by the effect reach around it) instead of everything. -pub(crate) fn note_damage(s: &mut Session, before: &Document, ids: &[LayerId]) { - let Some(st) = s.active_mut() else { return }; - if before.size != st.doc.size { - return; - } - st.last_damage = layers_damage(before, &st.doc, ids); -} - -/// Whether two surfaces hold identical pixels: same format and default pixel, and the same -/// copy-on-write tile pointers at the same coordinates. Both documents stay alive during the -/// comparison, so tile addresses cannot be reused under it. -fn surfaces_equal(a: &photocraft_raster::Surface, b: &photocraft_raster::Surface) -> bool { - a.format() == b.format() - && a.default_pixel() == b.default_pixel() - && a.tile_count() == b.tile_count() - && a.tiles().zip(b.tiles()).all(|((ca, ta), (cb, tb))| ca == cb && std::sync::Arc::ptr_eq(ta, tb)) -} - -/// Byte blobs kept in `Arc`s (PSD raw blocks, embedded smart-object sources): pointer-identical -/// or content-equal (they are small compared to pixels, and undo steps rarely re-parse them). -fn blob_equal(x: &std::sync::Arc>, y: &std::sync::Arc>) -> bool { - std::sync::Arc::ptr_eq(x, y) || (x.len() == y.len() && x.as_slice() == y.as_slice()) -} - -fn blobs_equal(a: &Option>>, b: &Option>>) -> bool { - match (a, b) { - (None, None) => true, - (Some(x), Some(y)) => blob_equal(x, y), - _ => false, - } -} - -/// Layer masks (also the smart-filter mask): scalars plus tile-pointer surface identity. -fn masks_equal(a: &Option, b: &Option) -> bool { - match (a, b) { - (None, None) => true, - (Some(x), Some(y)) => { - x.enabled == y.enabled && x.linked == y.linked && x.density == y.density && x.feather == y.feather && surfaces_equal(&x.surface, &y.surface) - } - _ => false, - } -} - -fn texts_equal(a: &photocraft_doc::TextLayer, b: &photocraft_doc::TextLayer) -> bool { - a.text == b.text - && a.font_family == b.font_family - && a.size_pt == b.size_pt - && a.color == b.color - && a.transform == b.transform - && a.runs == b.runs - && a.paragraphs == b.paragraphs - && a.shape == b.shape - && a.orientation == b.orientation - && a.antialias == b.antialias - && a.warp == b.warp - && match (&a.cache, &b.cache) { - (None, None) => true, - (Some(x), Some(y)) => surfaces_equal(x, y), - _ => false, - } - && blobs_equal(&a.psd_raw, &b.psd_raw) -} - -fn smarts_equal(a: &photocraft_doc::SmartObject, b: &photocraft_doc::SmartObject) -> bool { - a.filters_enabled == b.filters_enabled - && a.transform == b.transform - && a.smart_filters == b.smart_filters - && a.stack_mode == b.stack_mode - && a.perspective == b.perspective - && a.warp == b.warp - && match (&a.source, &b.source) { - (photocraft_doc::SmartSource::Embedded { file_name: x, bytes: xb }, photocraft_doc::SmartSource::Embedded { file_name: y, bytes: yb }) => { - x == y && blob_equal(xb, yb) - } - (photocraft_doc::SmartSource::Linked { path: x }, photocraft_doc::SmartSource::Linked { path: y }) => x == y, - _ => false, - } - && match (&a.cache, &b.cache) { - (None, None) => true, - (Some(x), Some(y)) => surfaces_equal(x, y), - _ => false, - } - && blobs_equal(&a.psd_raw, &b.psd_raw) - && masks_equal(&a.filter_mask, &b.filter_mask) -} - -/// Extra alpha/spot channels (the image itself in Multichannel mode). -fn alpha_channels_equal(a: &photocraft_doc::AlphaChannel, b: &photocraft_doc::AlphaChannel) -> bool { - a.name == b.name && a.spot == b.spot && a.color == b.color && a.opacity == b.opacity && a.indicates == b.indicates && surfaces_equal(&a.surface, &b.surface) -} - -/// Whether two layers render to identical pixels in place. Everything the compositor reads is -/// compared structurally — no hashing, a hash collision would under-report damage; pixels are -/// identified by their copy-on-write tile pointers. Group children are deliberately *not* -/// compared here: the caller walks into them, so an edit inside a group damages only the -/// edited child's extent, not the whole group's. The destructure is exhaustive, so a new -/// `Layer` field fails to compile until it is classified: compared, or explicitly ignored -/// (name, locks, label, `psd_blocks`, `psd_id`, link group — none of them composite; video -/// bails out earlier in the walk). -fn layer_pixels_equal(a: &Layer, b: &Layer) -> bool { - let Layer { - id, - name: _, - visible, - locks: _, - blend, - opacity, - fill_opacity, - clipped, - mask, - vector_mask, - effects, - label: _, - content, - psd_blocks: _, - psd_id: _, - fill_cache, - link_group: _, - excluded_channels, - blend_if, - advanced, - video: _, - } = a; - *id == b.id - && *visible == b.visible - && *blend == b.blend - && *opacity == b.opacity - && *fill_opacity == b.fill_opacity - && *clipped == b.clipped - && masks_equal(mask, &b.mask) - && *vector_mask == b.vector_mask - && *effects == b.effects - && *excluded_channels == b.excluded_channels - && *blend_if == b.blend_if - && *advanced == b.advanced - && match (content, &b.content) { - (LayerContent::Raster(x), LayerContent::Raster(y)) => surfaces_equal(x, y), - // Children are walked separately; only the group's own clipping frame counts. - (LayerContent::Group(x), LayerContent::Group(y)) => x.artboard == y.artboard, - (LayerContent::Fill(x), LayerContent::Fill(y)) => x == y, - (LayerContent::Adjustment(x), LayerContent::Adjustment(y)) => x == y, - (LayerContent::Text(x), LayerContent::Text(y)) => texts_equal(x, y), - (LayerContent::Shape(x), LayerContent::Shape(y)) => x == y, - (LayerContent::Smart(x), LayerContent::Smart(y)) => smarts_equal(x, y), - _ => false, - } - && match (fill_cache, &b.fill_cache) { - (None, None) => true, - (Some(x), Some(y)) => x.fill == y.fill && surfaces_equal(&x.surface, &y.surface), - _ => false, - } -} - -/// Document-level fields the compositor reads: all must match for a bounded damage claim. The -/// destructure is exhaustive, so a new `Document` field fails to compile until it is classified -/// here — compared when it composites, or explicitly ignored (the pixel-irrelevant allowlist: -/// identity, name, selection, guides, paths, layer comps, annotations, automation state). -fn doc_pixels_equal(a: &Document, b: &Document) -> bool { - let Document { - id: _, - name: _, - size, - resolution_dpi, - mode, - depth, - icc_profile, - layers: _, - channels, - guides: _, - ruler_origin: _, - selection: _, - metadata: _, - global_light, - paths: _, - work_path: _, - clipping_path: _, - quick_mask, - quick_mask_unselected: _, - patterns, - color_table, - duotone, - layer_comps: _, - last_applied_comp: _, - last_document_state: _, - measurement: _, - notes: _, - text_styles: _, - slices: _, - variables: _, - timeline: _, - file_encoding: _, - } = a; - *size == b.size - && resolution_dpi.to_bits() == b.resolution_dpi.to_bits() - && *mode == b.mode - && *depth == b.depth - && icc_profile.as_deref() == b.icc_profile.as_deref() - && channels.len() == b.channels.len() - && channels.iter().zip(&b.channels).all(|(x, y)| alpha_channels_equal(x, y)) - && *global_light == b.global_light - && match (quick_mask, &b.quick_mask) { - (None, None) => true, - (Some(x), Some(y)) => alpha_channels_equal(x, y), - _ => false, - } - && *patterns == b.patterns - && *color_table == b.color_table - && *duotone == b.duotone -} - -/// Whether `a` and `b` composite to the same pixels: they differ at most in fields the -/// compositor never reads (selection, guides, paths, names, …), by the same classification as -/// [`step_damage`] but without computing any bounds. Cheap: pixels compare by tile pointer. -/// Conservative (`false`) past a nesting depth no real document reaches. -pub fn same_pixels(a: &Document, b: &Document) -> bool { - fn walk(xs: &[Layer], ys: &[Layer], depth: usize) -> bool { - depth < 256 - && xs.len() == ys.len() - && xs.iter().zip(ys).all(|(x, y)| { - x.video.is_none() - && y.video.is_none() - && layer_pixels_equal(x, y) - && match (x.children(), y.children()) { - (Some(xc), Some(yc)) => walk(xc, yc, depth + 1), - (None, None) => true, - _ => false, - } - }) - } - doc_pixels_equal(a, b) && walk(&a.layers, &b.layers, 0) -} - -/// Pixels a single history step can have changed between `a` and `b` (the documents on either -/// side of an edit, its undo or its redo): the union of the change bounds of every layer whose -/// pixels differ, so a local edit recomposites only that area instead of the whole canvas. A -/// layer only one side has (a duplicate, a paste, a deleted layer) counts where it draws (see -/// [`one_sided_bounds`]); one that draws nothing ([`inert_at`]) changes nothing. `None` -/// (composite everything) when the step touched anything the layer walk cannot bound: -/// document-level fields the compositor reads (canvas size, mode, depth, profile, global light, -/// patterns, channels, palettes), layer order and kind changes, layers added or removed that -/// reach outside their own bounds, layer properties that do (clipping, excluded channels — -/// what `layer.setProps` already reports as unbounded), and video layers. Inserting a copy of a -/// 12 MP layer used to recomposite every layer of the document (#2883, P8/P9 in -/// `perf/budgets.toml`). -pub fn step_damage(a: &Document, b: &Document) -> Option { - if !doc_pixels_equal(a, b) { - return None; - } - fn walk(xs: &[Layer], ys: &[Layer], a: &Document, b: &Document, out: &mut Rect) -> bool { - // Pair the layers by id, stepping over layers only one side has: one that draws nothing - // changes no pixels (a new empty layer or group, #1771), any other only where it draws. - let one_sided = |layers: &[Layer], k: usize, canvas: Rect, out: &mut Rect| { - let Some(r) = one_sided_bounds(layers, k, canvas) else { return false }; - if !r.is_empty() { - *out = if out.is_empty() { r } else { out.union(&r) }; - } - true - }; - let (mut i, mut j) = (0, 0); - loop { - let (x, y) = match (xs.get(i), ys.get(j)) { - (None, None) => return true, - (Some(x), Some(y)) if x.id == y.id => (x, y), - _ if inert_at(ys, j) => { - j += 1; - continue; - } - _ if inert_at(xs, i) => { - i += 1; - continue; - } - (_, Some(y)) if xs.iter().all(|x| x.id != y.id) => { - if !one_sided(ys, j, b.bounds(), out) { - return false; - } - j += 1; - continue; - } - (Some(x), _) if ys.iter().all(|y| y.id != x.id) => { - if !one_sided(xs, i, a.bounds(), out) { - return false; - } - i += 1; - continue; - } - // Both sides have the layer, in another order. - _ => return false, - }; - i += 1; - j += 1; - if x.video.is_some() || y.video.is_some() { - return false; - } - // Clipping and channel exclusion change how *other* layers render, outside this - // layer's own bounds; they have no bounded damage. - if x.clipped != y.clipped || x.excluded_channels != y.excluded_channels { - return false; - } - if !layer_pixels_equal(x, y) { - // Both sides' extents: a move changes pixels where it left and where it landed. - let (Some(bx), Some(by)) = (photocraft_compose::change_bounds(x, a.bounds()), photocraft_compose::change_bounds(y, b.bounds())) else { - return false; - }; - *out = if out.is_empty() { bx.union(&by) } else { out.union(&bx).union(&by) }; - } - match (x.children(), y.children()) { - (Some(xc), Some(yc)) => { - if !walk(xc, yc, a, b, out) { - return false; - } - } - (None, None) => {} - _ => return false, - } - } - } - let mut out = Rect::EMPTY; - if walk(&a.layers, &b.layers, a, b, &mut out) { Some(out) } else { None } -} - -/// The pixels adding or removing `layers[k]` can change, when that is bounded: its change bounds -/// (`photocraft_compose::change_bounds`: not for an adjustment, or a pass-through group holding -/// one), provided it isn't the clipping base of clipped layers above it (they would change base), -/// and neither it nor anything inside it is a video layer or excludes channels (both reach -/// outside the layer's bounds, see [`step_damage`]). -fn one_sided_bounds(layers: &[Layer], k: usize, canvas: Rect) -> Option { - fn contained(l: &Layer, depth: usize) -> bool { - depth < 256 && l.video.is_none() && l.excluded_channels == 0 && l.children().is_none_or(|c| c.iter().all(|c| contained(c, depth + 1))) - } - let l = layers.get(k)?; - let new_base = !l.clipped && layers.get(k + 1).is_some_and(|above| above.clipped); - if new_base || !contained(l, 0) { - return None; - } - photocraft_compose::change_bounds(l, canvas) -} - -/// Whether `layers[k]` draws nothing wherever it sits, so adding or removing it leaves the -/// composite as it was: an empty pixel layer (no tiles, transparent default) or an empty group, -/// in a Normal or Pass Through blend with no mask, vector mask, effects or Advanced Blending. -/// A clipping base that would gain or lose the clipped layers above it is not inert. -fn inert_at(layers: &[Layer], k: usize) -> bool { - let Some(l) = layers.get(k) else { return false }; - let empty = match &l.content { - LayerContent::Raster(s) => s.tile_count() == 0 && s.format().alpha && s.default_bytes().iter().all(|b| *b == 0), - LayerContent::Group(g) => g.children.is_empty() && g.artboard.is_none(), - _ => false, - }; - empty - && matches!(l.blend, photocraft_doc::BlendMode::Normal | photocraft_doc::BlendMode::PassThrough) - && l.mask.is_none() - && l.vector_mask.is_none() - && !photocraft_compose::effects::has_effects(l) - && l.fill_cache.is_none() - && l.video.is_none() - && l.advanced == photocraft_doc::AdvancedBlending::default() - && (l.clipped || layers.get(k + 1).is_none_or(|above| !above.clipped)) -} - -/// Report that adding layer `id` to the active document changed no pixels when it draws -/// nothing ([`inert_at`]), so the canvas doesn't recomposite every layer for it (#1771). Looks -/// only at the new layer and its neighbour, never at other layers' tiles. -pub(crate) fn note_inert_insert(s: &mut Session, id: LayerId) { - let Some(st) = s.active_mut() else { return }; - let Some(path) = st.doc.path_of(id) else { return }; - let Some((&k, parent)) = path.split_last() else { return }; - let siblings = if parent.is_empty() { Some(st.doc.layers.as_slice()) } else { st.doc.layer_at(parent).and_then(Layer::children) }; - if siblings.is_some_and(|sib| inert_at(sib, k)) { - st.last_damage = Some(Rect::EMPTY); - } -} - -/// Report the pixels an edit from `before` that added layers changed ([`step_damage`]), for -/// commands made of more than one edit, so the canvas recomposites only where the new layers -/// draw instead of the whole document. -pub(crate) fn note_insert(s: &mut Session, before: &Document) { - let Some(st) = s.active_mut() else { return }; - st.last_damage = step_damage(before, &st.doc); -} - -/// Content bounds used by Align/Distribute: the layer's pixels (type and shape layers use their -/// rendered pixels), or the union of a group's children. Fill and adjustment layers have none. -pub fn layer_bounds(l: &Layer) -> Option { - let r = match &l.content { - LayerContent::Group(g) => g.children.iter().filter_map(layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b)), - _ => l.surface().map(|s| s.content_bounds()).unwrap_or(Rect::EMPTY), - }; - (!r.is_empty()).then_some(r) -} - -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -enum Edge { - Top, - VCenter, - Bottom, - Left, - HCenter, - Right, -} - -impl Edge { - fn parse(k: &str) -> Option { - Some(match k { - "topEdges" => Edge::Top, - "verticalCenters" => Edge::VCenter, - "bottomEdges" => Edge::Bottom, - "leftEdges" => Edge::Left, - "horizontalCenters" => Edge::HCenter, - "rightEdges" => Edge::Right, - _ => return None, - }) - } - fn vertical(self) -> bool { - matches!(self, Edge::Top | Edge::VCenter | Edge::Bottom) - } - /// The edge's coordinate, doubled so centres stay integral. - fn value2(self, r: &Rect) -> i64 { - let (a, b) = if self.vertical() { (r.y0, r.y1) } else { (r.x0, r.x1) }; - match self { - Edge::Top | Edge::Left => 2 * a as i64, - Edge::Bottom | Edge::Right => 2 * b as i64, - Edge::VCenter | Edge::HCenter => a as i64 + b as i64, - } - } - fn delta(self, d: i32) -> (i32, i32) { - if self.vertical() { (0, d) } else { (d, 0) } - } -} - -/// Selected top-level layers that have content bounds, bottom-to-top. -fn bounded_selection(doc: &Document, sel: &[LayerId]) -> Vec<(LayerId, Rect)> { - top_level(doc, sel).into_iter().filter_map(|id| Some((id, layer_bounds(doc.layer(id)?)?))).collect() -} - -fn align(s: &mut Session, p: &Value, kind: &str) -> Result { - let cmd = format!("layer.align.{kind}"); - let edge = Edge::parse(kind).ok_or_else(|| bad(&cmd, "unknown edge"))?; - let sel = selected(s); - let to = p.get("to").and_then(Value::as_str).unwrap_or("auto").to_string(); - let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let moved = s.edit("Align", |doc, _| { - let items = bounded_selection(doc, &sel); - if items.is_empty() { - return Err(EngineError::Other("the selected layers have no content to align".into())); - } - let target = match to.as_str() { - "canvas" => doc.bounds(), - "selection" => doc.selection.as_ref().map(|m| m.content_bounds()).ok_or_else(|| EngineError::Other("there is no selection to align to".into()))?, - "auto" | "layers" => match (&doc.selection, sel.len()) { - // Photoshop: with an active selection, the layers align to its bounds (Layer › Align - // Layers to Selection), however many are selected. - (Some(m), _) if to == "auto" && !m.content_bounds().is_empty() => m.content_bounds(), - // One layer with no selection aligns to the canvas (otherwise it would align to itself). - (None, 1) if to == "auto" => doc.bounds(), - _ => items.iter().fold(Rect::EMPTY, |a, (_, b)| a.union(b)), - }, - other => return Err(bad(&cmd, format!("`to` must be auto|layers|selection|canvas, not `{other}`"))), - }; - if target.is_empty() { - return Err(EngineError::Other("nothing to align to".into())); - } - let t2 = edge.value2(&target); - let moves: Vec<(LayerId, i32, i32)> = items - .iter() - .flat_map(|(id, r)| { - let d = (t2 - edge.value2(r)).div_euclid(2) as i32; - let (dx, dy) = edge.delta(d); - // Linked layers travel with the layer they are linked to. - with_links(doc, &[*id]).into_iter().map(move |l| (l, dx, dy)) - }) - .collect(); - let moves = dedup_moves(doc, moves); - move_layers(doc, &before, &moves)?; - Ok(moves.iter().filter(|m| m.1 != 0 || m.2 != 0).count()) - })?; - Ok(json!({"moved": moved})) -} - -/// One move per layer (first wins), skipping layers nested in another moved layer. -fn dedup_moves(doc: &Document, moves: Vec<(LayerId, i32, i32)>) -> Vec<(LayerId, i32, i32)> { - let mut out: Vec<(LayerId, i32, i32)> = Vec::new(); - for m in moves { - if !out.iter().any(|o| o.0 == m.0) { - out.push(m); - } - } - let ids: Vec = out.iter().map(|m| m.0).collect(); - let keep = top_level(doc, &ids); - out.retain(|m| keep.contains(&m.0)); - out -} - -fn distribute(s: &mut Session, kind: &str) -> Result { - let sel = selected(s); - let kind = kind.to_string(); - let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let moved = s.edit("Distribute", |doc, _| { - let mut items = bounded_selection(doc, &sel); - if items.len() < 3 { - return Err(EngineError::Other("distributing needs three or more layers with content".into())); - } - let n = items.len(); - let mut moves = Vec::new(); - match kind.as_str() { - "horizontally" | "vertically" => { - // Equal gaps between neighbours; the outermost layers stay put. - let v = kind == "vertically"; - let lo = |r: &Rect| if v { r.y0 } else { r.x0 } as i64; - let hi = |r: &Rect| if v { r.y1 } else { r.x1 } as i64; - items.sort_by_key(|(_, r)| (lo(r) + hi(r), lo(r))); - let span = hi(&items[n - 1].1) - lo(&items[0].1); - let total: i64 = items.iter().map(|(_, r)| hi(r) - lo(r)).sum(); - let gap = (span - total) as f64 / (n - 1) as f64; - let mut cursor = lo(&items[0].1) as f64; - for (id, r) in &items { - let want = cursor.round() as i64; - let d = (want - lo(r)) as i32; - moves.push(if v { (*id, 0, d) } else { (*id, d, 0) }); - cursor += (hi(r) - lo(r)) as f64 + gap; - } - } - k => { - let edge = Edge::parse(k).ok_or_else(|| bad("layer.distribute", format!("unknown kind `{k}`")))?; - items.sort_by_key(|(_, r)| edge.value2(r)); - let (first, last) = (edge.value2(&items[0].1) as f64, edge.value2(&items[n - 1].1) as f64); - for (i, (id, r)) in items.iter().enumerate() { - let want2 = first + (last - first) * i as f64 / (n - 1) as f64; - let d = ((want2 - edge.value2(r) as f64) / 2.0).round() as i32; - let (dx, dy) = edge.delta(d); - moves.push((*id, dx, dy)); - } - } - } - let moves: Vec<_> = moves.into_iter().flat_map(|(id, dx, dy)| with_links(doc, &[id]).into_iter().map(move |l| (l, dx, dy))).collect(); - let moves = dedup_moves(doc, moves); - move_layers(doc, &before, &moves)?; - Ok(moves.iter().filter(|m| m.1 != 0 || m.2 != 0).count()) - })?; - Ok(json!({"moved": moved})) -} - -/// Reorder one or multiple layers to a single destination in one undoable edit. -/// -/// Explicit `layers` are ordered according to the source document, not caller ordering. -/// Selected descendants of a moved group remain inside their parent; only top-level -/// selected nodes are detached, and every reference is validated before editing. -pub fn move_to(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.moveTo"; - let target = LayerId(p.get("target").and_then(Value::as_u64).ok_or_else(|| bad(CMD, "missing `target`"))?); - let pos = p.get("position").and_then(Value::as_str).unwrap_or("above"); - if !matches!(pos, "above" | "below" | "into") { - return Err(bad(CMD, "position must be above, below, or into")); - } - // ⌥-drag in the Layers panel: duplicates land at the target and the layers stay put. - let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false); - - let batch = p.get("layers"); - if batch.is_some() && p.get("layer").is_some() { - return Err(bad(CMD, "give either `layer` or `layers`, not both")); - } - let requested = if let Some(raw) = batch { - let arr = raw.as_array().filter(|a| !a.is_empty()).ok_or_else(|| bad(CMD, "`layers` must be a nonempty array of ids"))?; - let mut ids = Vec::with_capacity(arr.len()); - for item in arr { - let id = LayerId(item.as_u64().ok_or_else(|| bad(CMD, "every layer id must be an unsigned integer"))?); - if ids.contains(&id) { - return Err(bad(CMD, "duplicate layer id")); - } - ids.push(id); - } - ids - } else { - vec![crate::commands::layer_param(s, p)?] - }; - let st = s.active().ok_or(EngineError::NoDocument)?; - let doc = &st.doc; - let target_path = doc.path_of(target).ok_or(EngineError::NoLayer(target))?; - for &id in &requested { - if doc.layer(id).is_none() { - return Err(EngineError::NoLayer(id)); - } - } - let moved_ids = top_level(doc, &requested); - if moved_ids.len() == 1 && moved_ids[0] == target && batch.is_none() && !copy { - // Preserve single-layer drag-to-self semantics. - return Ok(Value::Null); - } - // Also rejects dragging a selected group onto any of its descendants. - for id in &moved_ids { - let path = doc.path_of(*id).ok_or(EngineError::NoLayer(*id))?; - // A copy may land beside its own source (not inside it). - let beside_itself = copy && *id == target && pos != "into"; - if target_path.starts_with(&path) && !beside_itself { - return Err(bad(CMD, "target cannot be one of the moved layers or its descendant")); - } - } - if pos == "into" && doc.layer(target).is_none_or(|l| !l.is_group()) { - return Err(bad(CMD, "target is not a group")); - } - - let original_active = st.active_layer; - let background = crate::background_cmds::background_id(doc); - let (count, copies) = s.edit(if copy { "Duplicate Layer" } else { "Reorder Layers" }, |doc, active| { - let before = if copy { Default::default() } else { crate::clip_group::Before::read(doc, &moved_ids) }; - let mut moved = Vec::with_capacity(moved_ids.len()); - for &id in &moved_ids { - moved.push(if copy { crate::commands::layer_copy(doc, id)? } else { doc.remove(id).ok_or(EngineError::NoLayer(id))? }); - } - let copies: Vec = if copy { moved.iter().map(|l| l.id).collect() } else { Vec::new() }; - // Look up the destination AFTER removing every source: its indices may have shifted. - let path = doc.path_of(target).ok_or(EngineError::NoLayer(target))?; - if pos == "into" { - let children = doc.layer_at_mut(&path).and_then(Layer::children_mut).ok_or_else(|| bad(CMD, "target is not a group"))?; - children.extend(moved); - } else { - let (&idx, parent) = path.split_last().ok_or(EngineError::NoLayer(target))?; - let siblings = if parent.is_empty() { - &mut doc.layers - } else { - doc.layer_at_mut(parent).and_then(Layer::children_mut).ok_or_else(|| bad(CMD, "target's parent is not a group"))? - }; - let at = if pos == "below" { idx } else { idx + 1 }; - siblings.splice(at..at, moved); - } - check_group_depth(doc, "Reorder Layer")?; - crate::background_cmds::keep_background_at_bottom(background, doc)?; - crate::clip_group::settle(doc, if copy { &copies } else { &moved_ids }, &before, false); - *active = if copy { - copies.last().copied() - } else if batch.is_some() { - original_active.filter(|id| requested.contains(id)).or_else(|| requested.last().copied()) - } else { - moved_ids.first().copied() - }; - Ok((moved_ids.len(), copies)) - })?; - if copy { - let ids: Vec = copies.iter().map(|id| id.0).collect(); - if batch.is_some() { - let active = copies.last().copied(); - reselect(s, copies, active); - return Ok(json!({"moved": count, "layers": ids})); - } - return Ok(json!({"layer": ids.first()})); - } - if batch.is_some() { - let active = original_active.filter(|id| requested.contains(id)).or_else(|| requested.last().copied()); - reselect(s, requested, active); - Ok(json!({"moved": count})) - } else { - Ok(Value::Null) - } -} - -// ---------- structural commands ---------- - -fn link_layers(s: &mut Session) -> Result { - let sel = selected(s); - let linked = s.edit("Link Layers", |doc, _| { - let groups: Vec> = sel.iter().map(|id| doc.layer(*id).and_then(|l| l.link_group)).collect(); - // Already one link group (or a single linked layer): unlink, like Photoshop's toggle. - let first = groups.first().copied().flatten(); - let unlink = first.is_some() && groups.iter().all(|g| *g == first); - // A loaded file can store any id up to u64::MAX, so the new id must not be a bare - // max + 1 (#961). Unlinking needs no new id at all. - let next = if unlink { None } else { Some(doc.unused_link_group().ok_or_else(|| bad("layer.linkLayers", "no free link group id is left"))?) }; - for id in &sel { - let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?; - l.link_group = next; - } - // A group left with a single member is no longer a link. - let mut counts = std::collections::HashMap::::new(); - for (_, _, l) in doc.walk() { - if let Some(g) = l.link_group { - *counts.entry(g).or_default() += 1; - } - } - let lonely: Vec = - doc.walk().iter().filter(|(_, _, l)| l.link_group.is_some_and(|g| counts.get(&g).is_some_and(|&c| c < 2))).map(|(_, _, l)| l.id).collect(); - for id in lonely { - if let Some(l) = doc.layer_mut(id) { - l.link_group = None; - } - } - Ok(!unlink) - })?; - Ok(json!({"linked": linked})) -} - -fn lock_layers(s: &mut Session, p: &Value) -> Result { - let sel = selected(s); - let keys = ["transparency", "pixels", "position", "artboard", "all"]; - let given: Vec<(&str, bool)> = keys.iter().filter_map(|k| Some((*k, p.get(*k)?.as_bool()?))).collect(); - s.edit("Lock Layers", |doc, _| { - // No explicit locks: toggle "lock all" (on unless every selected layer is already locked). - let all_locked = sel.iter().all(|id| doc.layer(*id).is_some_and(|l| l.locks.all)); - let given: Vec<(&str, bool)> = if given.is_empty() { vec![("all", !all_locked)] } else { given.clone() }; - for id in &sel { - let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?; - for (k, v) in &given { - l.locks.set(k, *v); - } - } - Ok(()) - })?; - Ok(json!({"layers": sel.len()})) -} - -fn rename_layer(s: &mut Session, p: &Value) -> Result { - let name = p.get("name").and_then(Value::as_str).filter(|n| !n.trim().is_empty()).ok_or_else(|| bad("layer.renameLayer", "missing `name`"))?.to_string(); - let id = crate::commands::layer_param(s, p)?; - s.edit("Rename Layer", |doc, _| { - doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.name = name; - Ok(()) - })?; - Ok(Value::Null) -} - -fn reverse(s: &mut Session) -> Result { - let sel = selected(s); - s.edit("Reverse", |doc, _| { - let background = crate::background_cmds::background_id(doc); - let ids = top_level(doc, &sel); - if ids.len() < 2 { - return Err(EngineError::Other("select two or more layers to reverse".into())); - } - let paths: Vec> = ids.iter().map(|id| doc.path_of(*id).ok_or(EngineError::NoLayer(*id))).collect::>()?; - let layers: Vec = ids.iter().map(|id| doc.layer(*id).cloned().ok_or(EngineError::NoLayer(*id))).collect::>()?; - // Slots are disjoint (no layer is nested in another), so writing whole layers back to the - // same paths never shifts another slot. - for (path, l) in paths.iter().zip(layers.into_iter().rev()) { - *doc.layer_at_mut(path).ok_or_else(|| EngineError::Other("layer tree changed".into()))? = l; - } - crate::background_cmds::keep_background_at_bottom(background, doc) - })?; - Ok(Value::Null) -} - -/// Refuses an edit that left `doc` nested deeper than [`photocraft_doc::MAX_GROUP_DEPTH`] -/// groups. Called at the end of a `Session::edit` closure, so an `Err` leaves the document and -/// history untouched. -pub(crate) fn check_group_depth(doc: &Document, what: &str) -> Result<()> { - if doc.max_group_depth() > photocraft_doc::MAX_GROUP_DEPTH { - return Err(EngineError::Other(format!("{what} would nest layers deeper than {} groups", photocraft_doc::MAX_GROUP_DEPTH))); - } - Ok(()) -} - -/// Group Layers (⌘G) / Group from Layers: the explicit layer, or every selected layer, moves -/// into a new group placed where the top-most of them was. Bottom-to-top order is preserved. -pub fn group_layers(s: &mut Session, p: &Value) -> Result { - let ids = match crate::commands::explicit_layer(s, p)? { - Some(id) => vec![id], - None => selected(s), - }; - if ids.is_empty() { - return Err(EngineError::Other("no layer selected".into())); - } - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - let gid = s.edit("Group Layers", |doc, active| { - let ids = top_level(doc, &ids); - let top = *ids.last().ok_or(EngineError::NoLayer(LayerId(0)))?; - let name = name.unwrap_or_else(|| doc.next_layer_name("Group")); - let gid = doc.insert_above(Some(top), Layer::group(name, vec![])); - let mut children = Vec::with_capacity(ids.len()); - for id in &ids { - children.push(doc.remove(*id).ok_or(EngineError::NoLayer(*id))?); - } - *doc.layer_mut(gid).and_then(Layer::children_mut).ok_or(EngineError::NoLayer(gid))? = children; - check_group_depth(doc, "Group Layers")?; - *active = Some(gid); - Ok(gid) - })?; - reselect(s, vec![gid], Some(gid)); - Ok(json!({ "layer": gid.0 })) -} - -fn merge_layers(s: &mut Session) -> Result { - let sel = selected(s); - if sel.len() < 2 { - // ⌘E on a lone group is Photoshop's Merge Group (#3832); on a layer, Merge Down. - if let Some(&id) = sel.first() - && s.active().and_then(|d| d.doc.layer(id)).is_some_and(|l| matches!(l.content, LayerContent::Group(_))) - { - return s.execute("layer.mergeGroup", json!({ "layer": id.0 })); - } - return s.execute("layer.mergeDown", json!({})); - } - let mid = s.edit("Merge Layers", |doc, active| { - let ids = top_level(doc, &sel); - let top_id = *ids.last().ok_or_else(|| EngineError::Other("Merge Layers needs two or more layers".into()))?; - let top = doc.layer(top_id).ok_or(EngineError::NoLayer(top_id))?.clone(); - let mut solo = doc.clone(); - solo.layers = ids.iter().filter_map(|id| doc.layer(*id)).filter(|l| l.visible).cloned().collect(); - if solo.layers.is_empty() { - return Err(EngineError::Other("the selected layers are all hidden".into())); - } - let fmt = doc.pixel_format(); - let fmt = PixelFormat::new(fmt.mode, fmt.sample, true); - let mut merged = Layer::raster(top.name.clone(), fmt); - merged.locks = top.locks; - merged.label = top.label; - // Clipped layers of one chain merge into a layer still clipped to their base. Clipped - // layers of different chains don't: the result, at the top one's slot, would clip the - // lower chain's pixels to the upper chain's base. - let base = crate::clip_group::base_of(doc, top_id); - merged.clipped = base.is_some() && solo.layers.iter().all(|l| l.clipped && crate::clip_group::base_of(doc, l.id) == base); - *crate::pixels_mut(&mut merged)? = crate::pixels::composite_layers_uncropped(&solo, fmt); - let mid = merged.id; - // Hidden selected layers are discarded, as in Photoshop. - for id in &ids[..ids.len() - 1] { - doc.remove(*id); - } - *doc.layer_mut(top_id).ok_or(EngineError::NoLayer(top_id))? = merged; - *active = Some(mid); - Ok(mid) - })?; - reselect(s, vec![mid], Some(mid)); - Ok(json!({ "layer": mid.0 })) -} - -/// Next visible layer row below the deleted target, then rows above in reverse order. -/// Group headers precede their children in the UI, unlike Document::walk's storage order. -pub(crate) fn deletion_neighbours(doc: &Document, target: LayerId) -> Vec { - let rows = |expand_all: bool| { - let mut stack: Vec<&Layer> = doc.layers.iter().collect(); - let mut order = Vec::new(); - while let Some(layer) = stack.pop() { - order.push(layer.id); - if let LayerContent::Group(group) = &layer.content - && (expand_all || group.expanded) - { - stack.extend(group.children.iter()); - } - } - order - }; - let mut order = rows(false); - if !order.contains(&target) { - order = rows(true); - } - let Some(index) = order.iter().position(|id| *id == target) else { - return Vec::new(); - }; - order.iter().skip(index + 1).chain(order.iter().take(index).rev()).copied().collect() -} - -/// Delete Layer with several layers selected. -pub fn delete_selected(s: &mut Session) -> Result { - let sel = selected(s); - s.edit("Delete Layers", |doc, active| { - let ids = top_level(doc, &sel); - let neighbours = active.map(|id| deletion_neighbours(doc, id)).unwrap_or_default(); - let before = crate::clip_group::Before::read(doc, &ids); - for id in &ids { - doc.remove(*id).ok_or(EngineError::NoLayer(*id))?; - } - crate::clip_group::settle(doc, &[], &before, false); - if doc.layers.is_empty() { - return Err(EngineError::Other("a document must keep at least one layer".into())); - } - *active = neighbours.into_iter().find(|candidate| doc.layer(*candidate).is_some()); - Ok(()) - })?; - Ok(json!({"deleted": sel.len()})) -} - -/// Duplicate Layer with several layers selected: each copy goes above its original and the -/// copies become the selection. Artboard copies go beside their boards unless `in_place` -/// ([`crate::artboard_cmds::place_copy`]). `label` names the history step (Layer via Copy reuses -/// this path, #2777). -pub fn duplicate_selected(s: &mut Session, in_place: bool, label: &str) -> Result { - let sel = selected(s); - let before = s.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let old_active = s.active().and_then(|d| d.active_layer); - let (copies, active) = s.edit(label, |doc, active| { - let mut copies = Vec::new(); - let mut new_active = None; - for id in top_level(doc, &sel) { - // Match the single-layer duplicate path: Background copies become - // ordinary layers, while normal copies retain their original locks. - let dup = crate::commands::layer_copy(doc, id)?; - let nid = doc.insert_above(Some(id), dup); - if !in_place { - crate::artboard_cmds::place_copy(doc, nid)?; - } - if Some(id) == old_active { - new_active = Some(nid); - } - copies.push(nid); - } - let a = new_active.or(copies.last().copied()); - *active = a; - Ok((copies, a)) - })?; - note_insert(s, &before); - let out = copies.iter().map(|l| l.0).collect::>(); - reselect(s, copies, active); - Ok(json!({"layers": out, "layer": active.map(|l| l.0)})) -} - -/// Show/hide every selected layer in one step. -pub fn set_visible_selected(s: &mut Session, visible: bool) -> Result { - let sel = selected(s); - s.edit(if visible { "Show Layers" } else { "Hide Layers" }, |doc, _| { - for id in &sel { - doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?.visible = visible; - } - Ok(()) - })?; - Ok(Value::Null) -} - -/// Is more than one layer selected (so single-layer commands should act on the whole set)? -pub fn multi(s: &Session, p: &Value) -> bool { - p.get("layer").is_none() && selected(s).len() > 1 -} - -// ---------- registry ---------- - -pub fn specs() -> Vec { - macro_rules! spec { - ($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } - }; - } - const ALIGN: &str = r##"{"to":"auto|layers|selection|canvas"="auto"} (auto: the active selection's bounds when there is one, however many layers are selected; else one layer aligns to the canvas and several to their combined bounds)"##; - vec![ - spec!("select.allLayers", "All Layers", &["Select"], Some("Cmd+Alt+A"), "{}", has_doc, |s, _| select_all_layers(s)), - spec!("select.deselectLayers", "Deselect Layers", &["Select"], None, "{}", has_doc, |s, _| { - set_selection(s, Vec::new(), None, None)?; - if let Some(st) = s.active_mut() { - st.active_layer = None; - st.selected_layers.clear(); - } - Ok(Value::Null) - }), - spec!("select.findLayers", "Find Layers", &["Select"], Some("Cmd+Alt+Shift+F"), r##"{"name":str (case-insensitive substring)}"##, has_doc, find_layers), - spec!("layer.selectLinkedLayers", "Select Linked Layers", &["Layer"], None, "{}", has_linked, |s, _| select_linked(s)), - spec!("layer.align.topEdges", "Top Edges", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "topEdges")), - spec!("layer.align.verticalCenters", "Vertical Centers", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "verticalCenters")), - spec!("layer.align.bottomEdges", "Bottom Edges", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "bottomEdges")), - spec!("layer.align.leftEdges", "Left Edges", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "leftEdges")), - spec!("layer.align.horizontalCenters", "Horizontal Centers", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "horizontalCenters")), - spec!("layer.align.rightEdges", "Right Edges", &["Layer", "Align"], None, ALIGN, can_align, |s, p| align(s, p, "rightEdges")), - spec!("layer.distribute.topEdges", "Top Edges", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute(s, "topEdges")), - spec!("layer.distribute.verticalCenters", "Vertical Centers", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute( - s, - "verticalCenters" - )), - spec!("layer.distribute.bottomEdges", "Bottom Edges", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute(s, "bottomEdges")), - spec!("layer.distribute.leftEdges", "Left Edges", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute(s, "leftEdges")), - spec!("layer.distribute.horizontalCenters", "Horizontal Centers", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute( - s, - "horizontalCenters" - )), - spec!("layer.distribute.rightEdges", "Right Edges", &["Layer", "Distribute"], None, "{}", can_distribute, |s, _| distribute(s, "rightEdges")), - spec!( - "layer.distribute.horizontally", - "Horizontally", - &["Layer", "Distribute"], - None, - "{} (equal horizontal gaps)", - can_distribute, - |s, _| distribute(s, "horizontally") - ), - spec!("layer.distribute.vertically", "Vertically", &["Layer", "Distribute"], None, "{} (equal vertical gaps)", can_distribute, |s, _| distribute( - s, - "vertically" - )), - spec!("layer.linkLayers", "Link Layers", &["Layer"], None, "{} (toggles: unlinks when the selection is already one link group)", can_link, |s, _| { - link_layers(s) - }), - spec!("layer.mergeLayers", "Merge Layers", &["Layer"], Some("Cmd+E"), "{} (one layer selected: Merge Down)", has_layer, |s, _| merge_layers(s)), - spec!( - "layer.new.groupFromLayers", - "Group from Layers…", - &["Layer", "New"], - None, - r##"{"layer":id?,"name":str?} (no layer: every selected layer)"##, - has_layer, - group_layers - ), - spec!("layer.arrange.reverse", "Reverse", &["Layer", "Arrange"], None, "{}", two_plus, |s, _| reverse(s)), - spec!( - "layer.lockLayers", - "Lock Layers…", - &["Layer"], - None, - r##"{"transparency":bool?,"pixels":bool?,"position":bool?,"artboard":bool?,"all":bool?} (none given: toggle lock all)"##, - has_layer, - lock_layers - ), - spec!("layer.renameLayer", "Rename Layer", &["Layer"], None, r##"{"layer":id?,"name":str}"##, has_layer, rename_layer), - ] -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn duplicates_are_numbered_not_stacked() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 20})).unwrap(); - s.execute("layer.new.layer", json!({"name": "Layer 1"})).unwrap(); - let name = |s: &Session| { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().name.clone() - }; - s.execute("layer.duplicate", json!({})).unwrap(); - assert_eq!(name(&s), "Layer 1 copy"); - // Duplicating the copy (the active layer) numbers it rather than adding "copy" again. - s.execute("layer.duplicate", json!({})).unwrap(); - assert_eq!(name(&s), "Layer 1 copy 2"); - s.execute("layer.duplicate", json!({})).unwrap(); - assert_eq!(name(&s), "Layer 1 copy 3"); - let doc = &s.active().unwrap().doc; - assert_eq!(doc.copy_name("Layer 1"), "Layer 1 copy 4"); - // Names that merely contain "copy" keep it. - assert_eq!(doc.copy_name("Copywriting"), "Copywriting copy"); - assert_eq!(doc.copy_name("A copyedit"), "A copyedit copy"); - } - - fn session(depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 80, "depth": depth})).unwrap(); - s - } - - fn doc(s: &Session) -> &Document { - &s.active().unwrap().doc - } - - /// New pixel layer with an opaque red rectangle. - fn rect_layer(s: &mut Session, r: Rect) -> LayerId { - let id = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.edit("paint", |doc, _| { - doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - id - } - - fn bounds(s: &Session, id: LayerId) -> Rect { - layer_bounds(doc(s).layer(id).unwrap()).unwrap() - } - - fn sel(s: &Session) -> Vec { - selected(s) - } - - fn damage(s: &Session) -> Option { - s.active().unwrap().last_damage - } - - /// A Photoshop action's nudge in points moves whole pixels at the document's resolution: - /// 0.36 pt is 1 px at 200 ppi and 1.5 → 2 px at 300 ppi. - #[test] - fn translate_in_points_follows_the_resolution() { - for (res, moved) in [(200, 1), (300, 2), (72, 0)] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 80, "resolution": res})).unwrap(); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - s.execute("layer.translate", json!({"dx": 0.36, "dy": -0.36, "units": "pt"})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(10 + moved, 10 - moved, 20 + moved, 20 - moved), "{res} ppi"); - } - let mut s = session(8); - rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - for bad in [json!({"dx": 1, "units": "in"}), json!({"dx": "x", "units": "pt"}), json!({"dx": 1e300, "units": "pt"})] { - assert!(s.execute("layer.translate", bad.clone()).is_err(), "{bad}"); - } - } - - /// `remove` deselects a selected layer, leaves an unselected one alone and never empties the - /// selection. - #[test] - fn select_remove_takes_a_layer_out_of_the_selection() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let b = rect_layer(&mut s, Rect::new(30, 10, 40, 20)); - let c = rect_layer(&mut s, Rect::new(50, 10, 60, 20)); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": b.0, "mode": "add"})).unwrap(); - s.execute("layer.select", json!({"layer": c.0, "mode": "remove"})).unwrap(); - assert_eq!(sel(&s).len(), 2, "an unselected layer stays out"); - s.execute("layer.select", json!({"layer": b.0, "mode": "remove"})).unwrap(); - assert_eq!(sel(&s), vec![a]); - s.execute("layer.select", json!({"layer": a.0, "mode": "remove"})).unwrap(); - assert_eq!(sel(&s), vec![a], "the last selected layer stays"); - } - - /// A mask with its chain off stays put while the layer moves; a linked one moves along. - #[test] - fn translate_leaves_an_unlinked_mask_in_place() { - for depth in [8, 16] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.layerMask.hideAll", json!({})).unwrap(); - s.edit("mask", |doc, _| { - doc.layer_mut(a).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(12, 12, 18, 18), &[1.0]); - Ok(()) - }) - .unwrap(); - let mval = |s: &Session, x, y| doc(s).layer(a).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0); - s.execute("layer.layerMask.linked", json!({"linked": false})).unwrap(); - s.execute("layer.translate", json!({"dx": 30, "dy": 0})).unwrap(); - assert!(mval(&s, 14, 14) > 0.9, "{depth}-bit: unlinked mask stays"); - assert!(mval(&s, 44, 14) < 0.1); - s.execute("edit.undo", json!({})).unwrap(); - s.execute("layer.layerMask.linked", json!({"linked": true})).unwrap(); - s.execute("layer.translate", json!({"dx": 30, "dy": 0})).unwrap(); - assert!(mval(&s, 44, 14) > 0.9, "{depth}-bit: linked mask moves"); - } - } - - /// Aimed at the mask, a move shifts an unlinked mask alone and leaves a linked one with its layer. - #[test] - fn translate_with_the_mask_targeted_moves_an_unlinked_mask_alone() { - for depth in [8, 16] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.layerMask.hideAll", json!({})).unwrap(); - s.edit("mask", |doc, _| { - doc.layer_mut(a).unwrap().mask.as_mut().unwrap().surface.fill_rect(Rect::new(12, 12, 18, 18), &[1.0]); - Ok(()) - }) - .unwrap(); - let mval = |s: &Session, x, y| doc(s).layer(a).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0); - // Linked: the layer and its mask move together. - s.execute("layer.translate", json!({"dx": 30, "dy": 0, "target": "mask"})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(40, 10, 50, 20), "{depth}-bit"); - assert!(mval(&s, 44, 14) > 0.9); - s.execute("layer.layerMask.linked", json!({"linked": false})).unwrap(); - s.execute("layer.translate", json!({"dx": -30, "dy": 0, "target": "mask"})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(40, 10, 50, 20), "{depth}-bit: the layer stays"); - assert!(mval(&s, 14, 14) > 0.9, "the mask moved"); - assert!(mval(&s, 44, 14) < 0.1); - s.execute("edit.undo", json!({})).unwrap(); - assert!(mval(&s, 44, 14) > 0.9, "undo puts the mask back"); - } - } - - #[test] - fn moves_and_layer_props_report_only_their_area() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.translate", json!({"dx": 5, "dy": -3})).unwrap(); - assert_eq!(damage(&s), Some(Rect::new(10, 7, 25, 20)), "where the layer was and is"); - s.execute("layer.setProps", json!({"layer": a.0, "visible": false})).unwrap(); - assert_eq!(damage(&s), Some(Rect::new(15, 7, 25, 17))); - s.execute("layer.setProps", json!({"layer": a.0, "opacity": 0.5, "blend": "Multiply"})).unwrap(); - assert_eq!(damage(&s), Some(Rect::new(15, 7, 25, 17))); - // Clipping changes reach the layers around: everything. - s.execute("layer.setProps", json!({"layer": a.0, "clipped": true})).unwrap(); - assert_eq!(damage(&s), None); - // An adjustment layer changes everything beneath it. - let adj = s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.setProps", json!({"layer": adj, "visible": false})).unwrap(); - assert_eq!(damage(&s), None); - // The Background (opaque everywhere): everything. - let bg = doc(&s).layers[0].id; - s.execute("layer.setProps", json!({"layer": bg.0, "opacity": 0.5})).unwrap(); - let all = doc(&s).bounds(); - assert!(damage(&s).is_none_or(|r| r.contains_rect(&all)), "{:?}", damage(&s)); - // Bad params still fail cleanly. - assert!(s.execute("layer.setProps", json!({"layer": u64::MAX, "visible": true})).is_err()); - assert!(s.execute("layer.translate", json!({"layer": u64::MAX, "dx": 1})).is_err()); - } - - /// #1771: a new empty layer or group changes no pixels, so it (and undoing or redoing it) - /// must not make the canvas recomposite every layer of the document. - #[test] - fn new_empty_layers_and_groups_damage_nothing() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let composite = |s: &Session| photocraft_compose::render(doc(s), doc(s).bounds()); - let shown = composite(&s); - s.execute("layer.new.layer", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY), "{depth}-bit new layer"); - assert_eq!(composite(&s), shown, "an empty layer changes no pixels"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY), "{depth}-bit undo New Layer"); - s.execute("edit.redo", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY), "{depth}-bit redo New Layer"); - s.execute("layer.new.group", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY), "{depth}-bit new group"); - // Inside a group too. - let child = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); - let g = s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.select", json!({"layer": child})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY), "{depth}-bit new layer in a group"); - assert!(doc(&s).layer(LayerId(g)).and_then(Layer::children).is_some_and(|c| c.len() == 2)); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY)); - // Undoing a step that painted still reports that area, not nothing. - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.translate", json!({"dx": 5, "dy": 0})).unwrap(); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 25, 20))); - } - } - - #[test] - fn new_layers_that_change_clipping_recomposite_everything() { - let mut s = session(8); - let base = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let clipped = rect_layer(&mut s, Rect::new(0, 0, 100, 80)); - s.edit("green", |doc, _| { - doc.layer_mut(clipped).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 100, 80), &[0.0, 1.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.setProps", json!({"layer": clipped.0, "clipped": true})).unwrap(); - // New Layer between a clipping base and its clipped layer joins their clip group (#2203), - // so it draws nothing; an empty layer there that stayed unclipped would become their base. - s.execute("layer.select", json!({"layer": base.0})).unwrap(); - let shown = photocraft_compose::render(doc(&s), doc(&s).bounds()); - let before = doc(&s).clone(); - s.execute("layer.new.layer", json!({})).unwrap(); - assert_eq!(damage(&s), Some(Rect::EMPTY)); - assert_eq!(photocraft_compose::render(doc(&s), doc(&s).bounds()), shown); - let mut split = before.clone(); - split.insert_above(Some(base), Layer::raster("split", split.pixel_format())); - assert_ne!(photocraft_compose::render(&split, split.bounds()), shown); - assert_eq!(step_damage(&before, &split), None); - assert_eq!(step_damage(&split, &before), None); - s.execute("edit.undo", json!({})).unwrap(); - // A step that adds a layer with pixels (Layer via Copy, paste…) changes where it draws. - let before = doc(&s).clone(); - let mut after = before.clone(); - let mut l = Layer::raster("painted", after.pixel_format()); - l.surface_mut().unwrap().fill_rect(Rect::new(1, 1, 2, 2), &[0.0, 1.0, 0.0, 1.0]); - after.insert_above(None, l); - assert_eq!(step_damage(&before, &after), Some(Rect::new(1, 1, 2, 2))); - assert_eq!(step_damage(&after, &before), Some(Rect::new(1, 1, 2, 2))); - // Unless it becomes the base of clipped layers above it. - let mut based = before.clone(); - let mut l = Layer::raster("base", based.pixel_format()); - l.surface_mut().unwrap().fill_rect(Rect::new(1, 1, 2, 2), &[0.0, 1.0, 0.0, 1.0]); - let under = based.layers.iter().position(|l| l.id == clipped).unwrap(); - based.layers.insert(under, l); - assert_eq!(step_damage(&before, &based), None); - assert_eq!(step_damage(&based, &before), None); - // An adjustment reaches every pixel below it. - let mut adjusted = before.clone(); - adjusted.insert_above(None, Layer::new("inv", LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); - assert_eq!(step_damage(&before, &adjusted), None); - assert_eq!(step_damage(&adjusted, &before), None); - // Empty layers draw nothing, in any blend mode. - let mut styled = before.clone(); - let mut l = Layer::raster("styled", styled.pixel_format()); - l.blend = photocraft_doc::BlendMode::Multiply; - styled.insert_above(None, l); - assert_eq!(step_damage(&before, &styled), Some(Rect::EMPTY)); - let mut plain = before.clone(); - plain.insert_above(None, Layer::raster("plain", plain.pixel_format())); - assert_eq!(step_damage(&before, &plain), Some(Rect::EMPTY)); - assert_eq!(step_damage(&plain, &before), Some(Rect::EMPTY)); - } - - /// #2883 (P8/P9): a duplicate shares the source's tiles (no pixel copy) and recomposites only - /// where it draws; undo brings back exactly the old pixels. A copy that becomes the clipping - /// base of the layers above still recomposites everything. - #[test] - fn duplicate_shares_tiles_and_damages_only_the_copy() { - let render = |s: &Session| photocraft_compose::render(doc(s), doc(s).bounds()); - let tiles = - |s: &Session, id: LayerId| doc(s).layer(id).unwrap().surface().unwrap().tiles().map(|(c, t)| (*c, std::sync::Arc::as_ptr(t))).collect::>(); - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - s.execute("layer.setProps", json!({"layer": a.0, "opacity": 0.5})).unwrap(); - let shown = render(&s); - let copy = LayerId(s.execute("layer.duplicate", json!({"layer": a.0})).unwrap()["layer"].as_u64().unwrap()); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 20, 20)), "{depth}-bit: only where the copy draws"); - assert!(!tiles(&s, a).is_empty()); - assert_eq!(tiles(&s, copy), tiles(&s, a), "{depth}-bit: the copy shares the source's tiles"); - let now = render(&s); - assert_ne!(now, shown, "the half-transparent copy darkens its area"); - let w = shown.rect.width() as usize; - for (i, (p, q)) in shown.px.iter().zip(&now.px).enumerate() { - if !Rect::new(10, 10, 20, 20).contains((i % w) as i32, (i / w) as i32) { - assert_eq!(p, q, "{depth}-bit: pixel {i} outside the damage changed"); - } - } - s.execute("edit.undo", json!({})).unwrap(); - assert!(doc(&s).layer(copy).is_none()); - assert_eq!(render(&s), shown); - // A clipping base's copy goes between it and its clipped layer and becomes their base. - let clipped = rect_layer(&mut s, Rect::new(0, 0, 50, 50)); - s.execute("layer.setProps", json!({"layer": clipped.0, "clipped": true})).unwrap(); - s.execute("layer.duplicate", json!({"layer": a.0})).unwrap(); - assert_eq!(damage(&s), None, "{depth}-bit: a new clipping base"); - } - } - - /// The composite of `after` equals `before`'s outside `damage`. - fn unchanged_outside(before: &Document, after: &Document, damage: Rect) -> bool { - let (x, y) = (photocraft_compose::render(before, before.bounds()), photocraft_compose::render(after, after.bounds())); - let w = before.bounds().width() as i32; - x.px.iter().zip(&y.px).enumerate().all(|(i, (p, q))| p == q || damage.contains(i as i32 % w, i as i32 / w)) - } - - /// Duplicate, paste and delete, and their undo and redo, recomposite only where the layer - /// draws; undoing a duplicate used to recomposite the whole document. - #[test] - fn duplicate_paste_and_delete_report_where_the_layer_draws() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let full = doc(&s).bounds(); - let step = |s: &mut Session, id: &str, p: serde_json::Value| { - let before = doc(s).clone(); - s.execute(id, p).unwrap(); - let d = damage(s).unwrap_or_else(|| panic!("{id}: unbounded damage")); - assert!(d.width() < full.width(), "{id}: {d:?}"); - assert!(unchanged_outside(&before, doc(s), d), "{id}: pixels changed outside {d:?}"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(damage(s), Some(d), "undo {id}"); - s.execute("edit.redo", json!({})).unwrap(); - assert_eq!(damage(s), Some(d), "redo {id}"); - }; - step(&mut s, "layer.duplicate", json!({"layer": a.0})); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("select.rect", json!({"x": 12, "y": 12, "width": 4, "height": 4})).unwrap(); - s.execute("edit.copy", json!({})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); - step(&mut s, "edit.paste", json!({})); - step(&mut s, "layer.delete", json!({"layer": a.0})); - } - - #[test] - fn the_move_preview_matches_the_move() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let before = s.active().unwrap().doc.clone(); - let ids = move_targets(&before, &[a]); - let shown = moved(&before, &ids, 7, 4).unwrap(); - s.execute("layer.translate", json!({"layer": a.0, "dx": 7, "dy": 4})).unwrap(); - assert_eq!(layer_bounds(shown.layer(a).unwrap()), Some(bounds(&s, a))); - // Locked layers don't move in the preview either. - let mut locked = (*before).clone(); - locked.layer_mut(a).unwrap().locks.position = true; - assert!(moved(&locked, &ids, 1, 1).is_err()); - assert!(moved(&before, &[LayerId(u64::MAX)], 1, 1).is_err()); - } - - #[test] - fn move_stroked_shape_across_canvas_edge_matches_fresh_render() { - let mut s = session(8); - let id = LayerId( - s.execute("shape.create", json!({"kind": "rect", "rect": [10, 10, 30, 30], "stroke": {"width": 4, "color": "#ff0000"}})).unwrap()["layer"] - .as_u64() - .unwrap(), - ); - let before = doc(&s).clone(); - let ids = move_targets(&before, &[id]); - - // Drag the stroked shape across the left canvas boundary: - let moved_doc = moved(&before, &ids, -25, 0).unwrap(); - let moved_layer = moved_doc.layer(id).unwrap(); - let LayerContent::Shape(sh) = &moved_layer.content else { panic!("not a shape") }; - let cached = sh.cache.as_ref().expect("cached render"); - - let mut fresh_sh = sh.clone(); - crate::vector_cmds::refresh_shape(&moved_doc, &mut fresh_sh); - let fresh = fresh_sh.cache.as_ref().expect("fresh render"); - - assert_eq!(cached, fresh, "dragged shape cached render must match fresh render across canvas edge"); - - // Drag it back inside the canvas: - let moved_back = moved(&moved_doc, &ids, 30, 0).unwrap(); - let back_layer = moved_back.layer(id).unwrap(); - let LayerContent::Shape(back_sh) = &back_layer.content else { panic!("not a shape") }; - let back_cached = back_sh.cache.as_ref().expect("cached render"); - - let mut fresh_back = back_sh.clone(); - crate::vector_cmds::refresh_shape(&moved_back, &mut fresh_back); - let fresh_back_render = fresh_back.cache.as_ref().expect("fresh render"); - - assert_eq!(back_cached, fresh_back_render, "cached render must match fresh render when dragged back"); - } - - fn select_all(s: &mut Session, ids: &[LayerId]) { - s.execute("layer.select", json!({"layer": ids[0].0})).unwrap(); - for id in &ids[1..] { - s.execute("layer.select", json!({"layer": id.0, "mode": "toggle"})).unwrap(); - } - } - - #[test] - fn toggle_and_range_selection() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let c = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let d = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - assert_eq!(sel(&s), vec![d]); - s.execute("layer.select", json!({"layer": b.0, "mode": "toggle"})).unwrap(); - assert_eq!(sel(&s), vec![b, d]); - assert_eq!(s.active().unwrap().active_layer, Some(b)); - // Toggling the active layer off hands "active" to a remaining member. - s.execute("layer.select", json!({"layer": b.0, "mode": "toggle"})).unwrap(); - assert_eq!(sel(&s), vec![d]); - assert_eq!(s.active().unwrap().active_layer, Some(d)); - // The last selected layer can't be toggled off. - s.execute("layer.select", json!({"layer": d.0, "mode": "toggle"})).unwrap(); - assert_eq!(sel(&s), vec![d]); - // Range from the anchor (a) to c. - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": c.0, "mode": "range"})).unwrap(); - assert_eq!(sel(&s), vec![a, b, c]); - // A second ⇧-click re-ranges from the same anchor. - s.execute("layer.select", json!({"layer": b.0, "mode": "range"})).unwrap(); - assert_eq!(sel(&s), vec![a, b]); - assert!(s.execute("layer.select", json!({"layer": b.0, "mode": "bogus"})).is_err()); - // Selection changes are not edits. - let history = s.active().unwrap().history.entries().len(); - s.execute("select.allLayers", json!({})).unwrap(); - assert_eq!(sel(&s), vec![a, b, c, d], "Select All Layers skips the locked Background"); - s.execute("select.deselectLayers", json!({})).unwrap(); - assert!(sel(&s).is_empty()); - assert_eq!(s.active().unwrap().history.entries().len(), history); - // Nothing selected: a no-op, not an error (#2457). - s.execute("select.deselectLayers", json!({})).unwrap(); - assert_eq!(s.active().unwrap().history.entries().len(), history); - // An explicit layer needs no active layer. - s.execute("layer.hideLayers", json!({"layer": a.0})).unwrap(); - assert!(!s.active().unwrap().doc.layer(a).unwrap().visible); - s.execute("layer.renameLayer", json!({"layer": a.0, "name": "x"})).unwrap(); - assert_eq!(s.active().unwrap().doc.layer(a).unwrap().name, "x"); - s.execute("layer.showLayers", json!({"layer": a.0})).unwrap(); - assert!(s.active().unwrap().doc.layer(a).unwrap().visible); - assert!(s.execute("layer.hideLayers", json!({"layer": u64::MAX})).is_err()); - assert!(s.execute("layer.hideLayers", json!({})).is_err()); - assert!(!s.is_enabled("layer.align.topEdges")); - } - - #[test] - fn find_layers_by_name() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - s.execute("layer.renameLayer", json!({"layer": a.0, "name": "Sky photo"})).unwrap(); - s.execute("layer.renameLayer", json!({"layer": b.0, "name": "Photo of sea"})).unwrap(); - assert_eq!(doc(&s).layer(a).unwrap().name, "Sky photo"); - let r = s.execute("select.findLayers", json!({"name": "PHOTO"})).unwrap(); - assert_eq!(r["selected"], json!([a.0, b.0])); - assert!(s.execute("select.findLayers", json!({"name": "nothing"})).is_err()); - assert!(s.execute("layer.renameLayer", json!({})).is_err()); - } - - #[test] - fn rename_layer_keeps_surrounding_spaces() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - s.execute("layer.renameLayer", json!({"layer": a.0, "name": " black skin bw "})).unwrap(); - assert_eq!(doc(&s).layer(a).unwrap().name, " black skin bw "); - assert!(s.execute("layer.renameLayer", json!({"layer": a.0, "name": " "})).is_err()); - assert_eq!(doc(&s).layer(a).unwrap().name, " black skin bw "); - } - - #[test] - fn align_each_edge() { - for depth in [8, 16, 32] { - let cases: [(&str, [Rect; 2]); 6] = [ - ("topEdges", [Rect::new(10, 5, 20, 15), Rect::new(40, 5, 50, 25)]), - ("bottomEdges", [Rect::new(10, 20, 20, 30), Rect::new(40, 10, 50, 30)]), - // Union 5..30: centre 17.5; half-pixel offsets round towards the top. - ("verticalCenters", [Rect::new(10, 12, 20, 22), Rect::new(40, 7, 50, 27)]), - ("leftEdges", [Rect::new(10, 5, 20, 15), Rect::new(10, 10, 50, 30)]), - ("rightEdges", [Rect::new(40, 5, 50, 15), Rect::new(10, 10, 50, 30)]), - ("horizontalCenters", [Rect::new(25, 5, 35, 15), Rect::new(10, 10, 50, 30)]), - ]; - for (kind, want) in cases { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 5, 20, 15)); - let b = rect_layer(&mut s, Rect::new(40, 10, 50, 30)); - // For the horizontal cases make b wide so it defines the target bounds. - if !matches!(kind, "topEdges" | "bottomEdges" | "verticalCenters") { - s.edit("wide", |doc, _| { - doc.layer_mut(b).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 50, 30), &[0.0, 1.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - } - select_all(&mut s, &[a, b]); - s.execute(&format!("layer.align.{kind}"), json!({})).unwrap(); - assert_eq!([bounds(&s, a), bounds(&s, b)], want, "{kind} at {depth}-bit"); - // Pixels moved, not just bounds. - let r = want[0]; - assert_eq!(doc(&s).layer(a).unwrap().surface().unwrap().pixel(r.x0, r.y0)[..4], [1.0, 0.0, 0.0, 1.0]); - // One undo restores the original positions. - s.undo(); - assert_eq!(bounds(&s, a), Rect::new(10, 5, 20, 15)); - } - } - } - - #[test] - fn align_single_layer_to_selection() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - assert!(s.is_enabled("layer.align.leftEdges"), "one layer aligns to the canvas"); - s.execute("select.rect", json!({"x": 50, "y": 40, "width": 30, "height": 30})).unwrap(); - assert!(s.is_enabled("layer.align.leftEdges")); - s.execute("layer.align.rightEdges", json!({})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(70, 10, 80, 20)); - s.execute("layer.align.verticalCenters", json!({})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(70, 50, 80, 60)); - s.execute("layer.align.topEdges", json!({"to": "canvas"})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(70, 0, 80, 10)); - } - } - - #[test] - fn align_single_layer_to_canvas() { - // Photoshop: a single selected layer with no active selection aligns to the canvas. - let mut s = session(8); // 100 x 80 - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); // 10 x 10 - assert!(s.is_enabled("layer.align.horizontalCenters")); - s.execute("layer.align.horizontalCenters", json!({})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(45, 10, 55, 20), "centred on the 100px-wide canvas"); - s.execute("layer.align.verticalCenters", json!({})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(45, 35, 55, 45), "centred on the 80px-tall canvas"); - s.execute("layer.align.leftEdges", json!({})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(0, 35, 10, 45), "left edge to the canvas"); - } - - #[test] - fn distribute_uneven_layers() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(12, 0, 32, 10)); // 20 wide - let c = rect_layer(&mut s, Rect::new(70, 0, 100, 10)); // 30 wide - select_all(&mut s, &[a, b, c]); - // Undo targets the layers its state had when it was created (#495): reselect. - assert!(s.is_enabled("layer.distribute.leftEdges")); - s.execute("layer.distribute.leftEdges", json!({})).unwrap(); - assert_eq!(bounds(&s, b), Rect::new(35, 0, 55, 10), "left edges 0, 35, 70"); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.horizontalCenters", json!({})).unwrap(); - // centres 5 and 85 → middle centre 45 - assert_eq!(bounds(&s, b), Rect::new(35, 0, 55, 10)); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.rightEdges", json!({})).unwrap(); - // right edges 10 and 100 → 55 - assert_eq!(bounds(&s, b), Rect::new(35, 0, 55, 10)); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.horizontally", json!({})).unwrap(); - // span 100, widths 60 → gaps of 20: b at 30..50 - assert_eq!(bounds(&s, b), Rect::new(30, 0, 50, 10)); - assert_eq!(bounds(&s, a), Rect::new(0, 0, 10, 10)); - assert_eq!(bounds(&s, c), Rect::new(70, 0, 100, 10)); - s.undo(); - assert_eq!(bounds(&s, b), Rect::new(12, 0, 32, 10)); - } - // Vertical: tall layers with uneven heights. - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(0, 50, 10, 54)); - let c = rect_layer(&mut s, Rect::new(0, 60, 10, 80)); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.vertically", json!({})).unwrap(); - // span 80, heights 34 → gaps 23: b at 33..37 - assert_eq!(bounds(&s, b), Rect::new(0, 33, 10, 37)); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.topEdges", json!({})).unwrap(); - assert_eq!(bounds(&s, b), Rect::new(0, 30, 10, 34)); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.bottomEdges", json!({})).unwrap(); - assert_eq!(bounds(&s, b), Rect::new(0, 41, 10, 45)); - s.undo(); - select_all(&mut s, &[a, b, c]); - s.execute("layer.distribute.verticalCenters", json!({})).unwrap(); - // centres 5 and 70 → 37.5, b is 4 tall → top 35.5 → rounds to 36 (35 for ties down) - let y0 = bounds(&s, b).y0; - assert!((35..=36).contains(&y0), "{y0}"); - // Two layers can't be distributed. - select_all(&mut s, &[a, b]); - assert!(!s.is_enabled("layer.distribute.leftEdges")); - } - - #[test] - fn merge_keeps_pixels_outside_the_canvas() { - // #5067: the merged layer was cropped to the 100x80 canvas. - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10)); - let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120)); - select_all(&mut s, &[a, b]); - let m = LayerId(s.execute("layer.mergeLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); - let ml = doc(&s).layer(m).unwrap(); - assert_eq!(layer_bounds(ml).unwrap(), Rect::new(-30, -20, 140, 120)); - assert_eq!(ml.surface().unwrap().pixel(-10, -5)[3], 1.0); - assert_eq!(ml.surface().unwrap().pixel(130, 110)[3], 1.0); - } - - #[test] - fn merge_down_and_merge_group_keep_pixels_outside_the_canvas() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10)); - let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120)); - s.execute("layer.mergeDown", json!({"layer": b.0})).unwrap(); - assert_eq!(layer_bounds(doc(&s).layer(a).unwrap()).unwrap(), Rect::new(-30, -20, 140, 120)); - - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(-30, -20, 10, 10)); - let b = rect_layer(&mut s, Rect::new(90, 70, 140, 120)); - select_all(&mut s, &[a, b]); - let g = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); - s.execute("layer.mergeGroup", json!({"layer": g.0})).unwrap(); - assert_eq!(layer_bounds(doc(&s).layer(g).unwrap()).unwrap(), Rect::new(-30, -20, 140, 120)); - } - - #[test] - fn merge_selected_into_top_most() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(20, 0, 30, 10)); - let c = rect_layer(&mut s, Rect::new(40, 0, 50, 10)); - s.execute("layer.renameLayer", json!({"layer": c.0, "name": "Top"})).unwrap(); - select_all(&mut s, &[a, c]); - let before = doc(&s).layer_count(); - let m = LayerId(s.execute("layer.mergeLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); - let d = doc(&s); - assert_eq!(d.layer_count(), before - 1); - let ml = d.layer(m).unwrap(); - assert_eq!(ml.name, "Top"); - assert_eq!(layer_bounds(ml).unwrap(), Rect::new(0, 0, 50, 10)); - assert_eq!(ml.surface().unwrap().pixel(5, 5)[..4], [1.0, 0.0, 0.0, 1.0]); - assert_eq!(ml.surface().unwrap().pixel(25, 5)[3], 0.0, "b was not merged"); - assert!(d.layer(b).is_some() && d.layer(a).is_none()); - assert_eq!(d.layers.last().unwrap().id, m, "merged layer sits where the top-most was"); - assert_eq!(sel(&s), vec![m]); - s.undo(); - assert_eq!(doc(&s).layer_count(), before); - assert_eq!(sel(&s), vec![a, c], "undo selects the layers that were merged"); - // One layer selected: Merge Down. - s.execute("layer.select", json!({"layer": c.0})).unwrap(); - s.execute("layer.mergeLayers", json!({})).unwrap(); - assert_eq!(doc(&s).layer_count(), before - 1); - } - } - - #[test] - fn merging_group_children_preserves_their_composite() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let lower = rect_layer(&mut s, Rect::new(10, 12, 40, 42)); - let upper = rect_layer(&mut s, Rect::new(25, 20, 55, 50)); - select_all(&mut s, &[lower, upper]); - let group = LayerId(s.execute("layer.groupLayers", json!({"name": "Merge test group"})).unwrap()["layer"].as_u64().unwrap()); - let doc_before = doc(&s); - let expected = photocraft_compose::render(doc_before, doc_before.bounds()); - let children = doc_before.layer(group).unwrap().children().unwrap().iter().map(|layer| layer.id).collect::>(); - - select_all(&mut s, &children); - s.execute("layer.mergeLayers", json!({})).unwrap(); - let doc_after = doc(&s); - let actual = photocraft_compose::render(doc_after, doc_after.bounds()); - assert_eq!(actual, expected, "depth {depth}: group merge changed rendered pixels"); - assert_eq!(doc_after.layer(group).unwrap().children().unwrap().len(), 1); - } - } - - #[test] - fn merging_keeps_layer_effects_outside_the_pixels() { - // A drop shadow reaches past the layer's pixels; the merge must render that far. - for depth in [8, 16, 32] { - let mut s = session(depth); - let lower = rect_layer(&mut s, Rect::new(10, 10, 30, 30)); - let upper = rect_layer(&mut s, Rect::new(40, 20, 60, 40)); - s.execute("layer.layerStyle.dropShadow", json!({"layer": upper.0, "blend": "normal", "distance": 12, "size": 4})).unwrap(); - let before = photocraft_compose::render(doc(&s), doc(&s).bounds()); - select_all(&mut s, &[lower, upper]); - s.execute("layer.mergeLayers", json!({})).unwrap(); - let after = photocraft_compose::render(doc(&s), doc(&s).bounds()); - let worst = before.px.iter().zip(&after.px).map(|(a, b)| a.iter().zip(b).map(|(x, y)| (x - y).abs()).fold(0.0f32, f32::max)).fold(0.0f32, f32::max); - assert!(worst <= 1.5 / 255.0, "depth {depth}: merge layers dropped the shadow (off by {worst})"); - } - } - - #[test] - fn merging_a_group_down_preserves_the_composite() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let lower = rect_layer(&mut s, Rect::new(10, 12, 50, 52)); - let child_a = rect_layer(&mut s, Rect::new(24, 20, 64, 60)); - let child_b = rect_layer(&mut s, Rect::new(35, 28, 75, 68)); - select_all(&mut s, &[child_a, child_b]); - let group = LayerId(s.execute("layer.groupLayers", json!({"name": "Merge down group"})).unwrap()["layer"].as_u64().unwrap()); - let doc_before = doc(&s); - let expected = photocraft_compose::render(doc_before, doc_before.bounds()); - - s.execute("layer.mergeDown", json!({"layer": group.0})).unwrap(); - let doc_after = doc(&s); - let actual = photocraft_compose::render(doc_after, doc_after.bounds()); - assert_eq!(actual, expected, "depth {depth}: group merge down changed rendered pixels"); - assert!(doc_after.layer(lower).is_some()); - assert!(doc_after.layer(group).is_none()); - } - } - - #[test] - fn a_locked_group_locks_its_contents() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let g = s.execute("layer.new.groupFromLayers", json!({})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.setProps", json!({"layer": g, "locked": true})).unwrap(); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - for (cmd, p) in [ - ("layer.translate", json!({"dx": 1})), - ("paint.stroke", json!({"points": [[2, 2], [4, 4]]})), - ("edit.fill", json!({"color": "#00ff00"})), - ("edit.transform.flipHorizontal", json!({})), - ] { - assert!(s.execute(cmd, p).is_err(), "{cmd} edited a layer in a locked group"); - } - // Unlocking the group releases its contents. - s.execute("layer.setProps", json!({"layer": g, "locked": false})).unwrap(); - s.execute("layer.translate", json!({"dx": 1})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(1, 0, 6, 5)); - } - - #[test] - fn a_locked_group_keeps_a_grouped_background_locked() { - let mut s = session(8); - let bg = doc(&s).layers[0].id; - s.execute("layer.select", json!({"layer": bg.0})).unwrap(); - let g = s.execute("layer.new.groupFromLayers", json!({})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.setProps", json!({"layer": g, "locked": true})).unwrap(); - s.execute("layer.select", json!({"layer": bg.0})).unwrap(); - for (cmd, p) in [ - ("edit.transform.flipHorizontal", json!({})), - ("edit.transform.warp", json!({"style": "flag", "bend": 50})), - ("edit.puppetWarp", json!({"pins": [{"src": [30, 25], "dst": [36, 29]}]})), - ] { - let e = s.execute(cmd, p).unwrap_err().to_string(); - assert!(e.contains("locked"), "{cmd}: {e}"); - assert_eq!(doc(&s).layer(bg).unwrap().name, "Background", "{cmd}"); - } - } - - #[test] - fn group_from_layers_preserves_order() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let c = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let d = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[c, a]); - let g = LayerId(s.execute("layer.new.groupFromLayers", json!({"name": "Pair"})).unwrap()["layer"].as_u64().unwrap()); - let docu = doc(&s); - let gl = docu.layer(g).unwrap(); - assert_eq!(gl.name, "Pair"); - assert_eq!(gl.children().unwrap().iter().map(|l| l.id).collect::>(), vec![a, c]); - // Root (bottom-to-top): Background, b, Pair (where c was), d. - assert_eq!(docu.layers.iter().map(|l| l.id).skip(1).collect::>(), vec![b, g, d]); - assert_eq!(sel(&s), vec![g]); - s.undo(); - assert_eq!(doc(&s).layers.len(), 5); - // ⌘G with several selected groups them too. - select_all(&mut s, &[b, d]); - s.execute("layer.groupLayers", json!({})).unwrap(); - assert_eq!(doc(&s).layers.len(), 4); - } - - #[test] - fn grouping_is_capped_at_the_document_nesting_limit() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[a]); - for _ in 0..photocraft_doc::MAX_GROUP_DEPTH { - s.execute("layer.groupLayers", json!({})).unwrap(); - } - assert_eq!(doc(&s).max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH); - // One more wrapping group would pass the cap: rejected, document untouched. - let err = s.execute("layer.groupLayers", json!({})).unwrap_err(); - assert!(err.to_string().contains("deeper than 100"), "{err}"); - assert_eq!(doc(&s).max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH); - // The rejected call recorded no history step: undo still lands one grouping earlier. - s.undo(); - assert_eq!(doc(&s).max_group_depth(), photocraft_doc::MAX_GROUP_DEPTH - 1); - } - - #[test] - fn select_linked_layers_excludes_an_unlinked_active_layer() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(20, 20, 30, 30)); - let other = rect_layer(&mut s, Rect::new(50, 50, 60, 60)); - select_all(&mut s, &[a, b]); - s.execute("layer.linkLayers", json!({})).unwrap(); - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.select", json!({"layer": other.0, "mode": "toggle"})).unwrap(); - assert_eq!(sel(&s), vec![a, other]); - assert_eq!(s.active().unwrap().active_layer, Some(other)); - let past = s.active().unwrap().history.past_len(); - let dirty = s.active().unwrap().is_dirty(); - - let result = s.execute("layer.selectLinkedLayers", json!({})).unwrap(); - assert_eq!(sel(&s), vec![a, b]); - assert_eq!(result["selected"], sel(&s).len()); - assert_eq!(s.active().unwrap().active_layer, Some(b)); - assert_eq!(s.active().unwrap().layer_anchor, Some(b)); - assert_eq!(s.active().unwrap().history.past_len(), past); - assert_eq!(s.active().unwrap().is_dirty(), dirty); - - // A following multi-layer delete must leave the unrelated layer intact. - s.execute("layer.delete", json!({})).unwrap(); - assert!(doc(&s).layer(a).is_none() && doc(&s).layer(b).is_none()); - assert!(doc(&s).layer(other).is_some()); - s.undo(); - assert!(doc(&s).layer(a).is_some() && doc(&s).layer(b).is_some()); - assert!(doc(&s).layer(other).is_some()); - } - } - - #[test] - fn select_linked_layers_preserves_a_linked_active_layer() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(20, 20, 30, 30)); - select_all(&mut s, &[a, b]); - s.execute("layer.linkLayers", json!({})).unwrap(); - for active in [a, b] { - s.execute("layer.select", json!({"layer": active.0})).unwrap(); - let st = s.active_mut().unwrap(); - st.saved_revision = st.revision; - let past = st.history.past_len(); - for _ in 0..2 { - let result = s.execute("layer.selectLinkedLayers", json!({})).unwrap(); - assert_eq!(sel(&s), vec![a, b]); - assert_eq!(result["selected"], sel(&s).len()); - assert_eq!(s.active().unwrap().active_layer, Some(active)); - assert_eq!(s.active().unwrap().layer_anchor, Some(active)); - assert_eq!(s.active().unwrap().history.past_len(), past); - assert!(!s.active().unwrap().is_dirty()); - } - } - } - } - - #[test] - fn select_linked_layers_requires_a_linked_selection() { - let mut s = Session::new(); - assert!(!s.is_enabled("layer.selectLinkedLayers")); - assert!(s.execute("layer.selectLinkedLayers", json!({})).is_err()); - let mut s = session(8); - let before = sel(&s); - assert!(!s.is_enabled("layer.selectLinkedLayers")); - assert!(s.execute("layer.selectLinkedLayers", json!({})).is_err()); - assert_eq!(sel(&s), before); - } - - #[test] - fn translate_rejects_offsets_that_would_wrap() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[a]); - // 2^32 + 50 wrapped to `dx = 50` and 3e9 to a negative offset through `as i32`. - for dx in [4_294_967_346_i64, 3_000_000_000_i64] { - let err = s.execute("layer.translate", json!({"dx": dx, "dy": 0})).unwrap_err(); - assert!(err.to_string().contains("32-bit"), "{err}"); - } - assert_eq!(bounds(&s, a), Rect::new(0, 0, 5, 5), "the layer never moved"); - // Large in-range offsets still work. - s.execute("layer.translate", json!({"dx": -200_000, "dy": 200_000})).unwrap(); - } - - #[test] - fn link_layers_never_reuses_a_group_when_the_largest_id_is_u64_max() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(20, 20, 30, 30)); - let c = rect_layer(&mut s, Rect::new(50, 50, 60, 60)); - let d = rect_layer(&mut s, Rect::new(70, 70, 80, 80)); - // A stored document can carry any id; `max + 1` once overflowed here. - let st = s.active_mut().unwrap(); - for id in [c, d] { - std::sync::Arc::make_mut(&mut st.doc).layer_mut(id).unwrap().link_group = Some(u64::MAX); - } - select_all(&mut s, &[a, b]); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true); - let g = doc(&s).layer(a).unwrap().link_group; - assert_eq!(g, Some(1)); - assert_eq!(doc(&s).layer(b).unwrap().link_group, g); - for id in [c, d] { - assert_eq!(doc(&s).layer(id).unwrap().link_group, Some(u64::MAX), "the existing group is untouched"); - } - } - - #[test] - fn undo_and_redo_report_only_the_edited_area() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - select_all(&mut s, &[a]); - // A pixel edit on one layer is bounded by that layer's extent on both sides. - s.edit("paint", |doc, _| { - doc.layer_mut(a).unwrap().surface_mut().unwrap().fill_rect(Rect::new(12, 12, 14, 14), &[0.0, 0.0, 1.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.undo(); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 20, 20))); - s.redo(); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 20, 20))); - // A move reports where the layer was and where it landed. - s.execute("layer.translate", json!({"dx": 5, "dy": 0})).unwrap(); - s.undo(); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 25, 20)), "union of where the layer was and is"); - s.redo(); - assert_eq!(damage(&s), Some(Rect::new(10, 10, 25, 20))); - } - - #[test] - fn undo_of_document_wide_steps_still_composites_everything() { - let mut s = session(8); - let _ = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - // A canvas change has no bounded damage. - s.execute("image.crop", json!({"x": 0, "y": 0, "width": 30, "height": 25})).unwrap(); - assert_ne!(doc(&s).size, photocraft_doc::Size::new(100, 80)); - s.undo(); - assert_eq!(damage(&s), None, "a size change recomposites the whole canvas"); - // A layer removed and restored changes only where it draws. - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[b]); - s.execute("layer.delete", json!({})).unwrap(); - s.undo(); - assert_eq!(damage(&s), Some(Rect::new(0, 0, 5, 5))); - } - - #[test] - fn undo_of_compositor_read_document_fields_composites_everything() { - let mut s = session(8); - let _ = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - // Global Light changes every effect's angle across the document. - s.execute("layer.layerStyle.globalLight", json!({"angle": 90.0})).unwrap(); - s.undo(); - assert_eq!(damage(&s), None, "a global-light change recomposites everything"); - // A mode change that keeps the pixel format (RGB → Indexed maps to RGB) neither. - s.execute("image.mode.indexedColor", json!({})).unwrap(); - s.undo(); - assert_eq!(damage(&s), None, "a mode change recomposites everything"); - } - - #[test] - fn undo_of_clipping_or_channel_exclusion_composites_everything() { - let mut s = session(8); - let _ = rect_layer(&mut s, Rect::new(10, 10, 20, 20)); - let b = rect_layer(&mut s, Rect::new(12, 12, 30, 30)); - select_all(&mut s, &[b]); - // Clipping reaches the layers clipped above, outside this layer's bounds. - s.execute("layer.setProps", json!({"clipped": true})).unwrap(); - s.undo(); - assert_eq!(damage(&s), None, "a clipping change recomposites everything"); - s.execute("layer.setProps", json!({"channels": [true, false, true]})).unwrap(); - s.undo(); - assert_eq!(damage(&s), None, "an excluded-channel change recomposites everything"); - } - - #[test] - fn linked_layers_move_together_and_undo() { - for depth in [8, 16, 32] { - let mut s = session(depth); - let a = rect_layer(&mut s, Rect::new(0, 0, 10, 10)); - let b = rect_layer(&mut s, Rect::new(20, 20, 30, 30)); - let c = rect_layer(&mut s, Rect::new(50, 50, 60, 60)); - assert!(!s.is_enabled("layer.linkLayers")); - select_all(&mut s, &[a, b]); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true); - let g = doc(&s).layer(a).unwrap().link_group; - assert!(g.is_some() && doc(&s).layer(b).unwrap().link_group == g); - // Move only a: b follows, c stays. - s.execute("layer.select", json!({"layer": a.0})).unwrap(); - s.execute("layer.translate", json!({"dx": 5, "dy": 3})).unwrap(); - assert_eq!(bounds(&s, a), Rect::new(5, 3, 15, 13)); - assert_eq!(bounds(&s, b), Rect::new(25, 23, 35, 33)); - assert_eq!(bounds(&s, c), Rect::new(50, 50, 60, 60)); - s.undo(); - assert_eq!(bounds(&s, a), Rect::new(0, 0, 10, 10)); - assert_eq!(bounds(&s, b), Rect::new(20, 20, 30, 30)); - // Select Linked Layers from just a. - s.execute("layer.selectLinkedLayers", json!({})).unwrap(); - assert_eq!(sel(&s), vec![a, b]); - // Multi-selection translate moves all selected (c added, unlinked). - s.execute("layer.select", json!({"layer": c.0, "mode": "toggle"})).unwrap(); - s.execute("layer.translate", json!({"dx": -1, "dy": 0})).unwrap(); - assert_eq!(bounds(&s, c), Rect::new(49, 50, 59, 60)); - assert_eq!(bounds(&s, a), Rect::new(-1, 0, 9, 10)); - // Toggle: linking the same group again unlinks. - select_all(&mut s, &[a, b]); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); - assert!(doc(&s).layer(a).unwrap().link_group.is_none()); - } - } - - /// Plain layers carrying stored link groups, as a loaded `.pcraft` or PSD can hold (#961). - fn stored_groups(s: &mut Session, groups: &[Option]) -> Vec { - let ids: Vec = groups.iter().map(|_| rect_layer(s, Rect::new(0, 0, 4, 4))).collect(); - s.edit("load groups", |doc, _| { - for (id, g) in ids.iter().zip(groups) { - doc.layer_mut(*id).unwrap().link_group = *g; - } - Ok(()) - }) - .unwrap(); - ids - } - - fn group(s: &Session, id: LayerId) -> Option { - doc(s).layer(id).unwrap().link_group - } - - /// Links `ids`, then checks they share one group no other layer uses (#961). - fn link_fresh(s: &mut Session, ids: &[LayerId]) -> u64 { - select_all(s, ids); - let past = s.active().unwrap().history.past_len(); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], true); - assert_eq!(s.active().unwrap().history.past_len(), past + 1); - let g = group(s, ids[0]).expect("linked"); - assert!(ids.iter().all(|id| group(s, *id) == Some(g))); - let members = doc(s).walk().iter().filter(|(_, _, l)| l.link_group == Some(g)).count(); - assert_eq!(members, ids.len(), "group {g} is shared with unselected layers"); - g - } - - #[test] - fn link_layers_beside_a_maximal_stored_group() { - // The issue's repro: one layer stored at u64::MAX, link it with an unlinked one. - let mut s = session(8); - let ids = stored_groups(&mut s, &[Some(u64::MAX), None]); - assert_ne!(link_fresh(&mut s, &ids), u64::MAX); - // Unlink and relink the pair: with no groups left, numbering starts over at 1. - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); - assert!(ids.iter().all(|id| group(&s, *id).is_none())); - assert_eq!(link_fresh(&mut s, &ids), 1); - - // Two other layers linked beside an untouched maximal pair. - let mut s = session(8); - let ids = stored_groups(&mut s, &[Some(u64::MAX), Some(u64::MAX), None, None]); - let g = link_fresh(&mut s, &ids[2..]); - assert_ne!(g, u64::MAX); - assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(u64::MAX), Some(u64::MAX))); - } - - #[test] - fn unlink_a_maximal_stored_group() { - let mut s = session(8); - let ids = stored_groups(&mut s, &[Some(u64::MAX), Some(u64::MAX), None]); - select_all(&mut s, &ids[..2]); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); - assert!(ids.iter().all(|id| group(&s, *id).is_none())); - s.undo(); - assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(u64::MAX), Some(u64::MAX))); - // Relinking all three gets a working group. - link_fresh(&mut s, &ids); - } - - #[test] - fn link_layers_when_every_group_is_huge() { - // Unselected groups at the top of the range, including the 0 a wrapped id would join. - let top = [u64::MAX, u64::MAX - 1, u64::MAX - 2, 0, 1, 2]; - let stored: Vec> = top.iter().flat_map(|g| [Some(*g), Some(*g)]).chain([None, None, None, None]).collect(); - let mut s = session(8); - let ids = stored_groups(&mut s, &stored); - let n = top.len() * 2; - let g = link_fresh(&mut s, &ids[n..n + 2]); - assert!(!top.contains(&g), "{g} collides with a stored group"); - // A second link gets yet another group, and existing groups never change. - let h = link_fresh(&mut s, &ids[n + 2..]); - assert!(!top.contains(&h) && h != g); - for (i, t) in top.iter().enumerate() { - assert_eq!((group(&s, ids[2 * i]), group(&s, ids[2 * i + 1])), (Some(*t), Some(*t))); - } - // Moving a newly linked layer leaves the group-0 pair alone. - assert_eq!(with_links(doc(&s), &[ids[n]]), vec![ids[n], ids[n + 1]]); - } - - #[test] - fn link_layers_near_the_maximum() { - // max + 1 still fits: the new group is u64::MAX itself. - let mut s = session(8); - let ids = stored_groups(&mut s, &[Some(u64::MAX - 1), Some(u64::MAX - 1), None, None, None, None]); - assert_eq!(link_fresh(&mut s, &ids[2..4]), u64::MAX); - // The next link no longer fits above the maximum and takes a free id instead. - let g = link_fresh(&mut s, &ids[4..]); - assert!(g != u64::MAX && g != u64::MAX - 1); - } - - #[test] - fn link_layers_assigns_working_groups() { - // Control: ordinary documents keep getting max + 1, starting at 1. - let mut s = session(8); - let ids = stored_groups(&mut s, &[None, None, None, None]); - assert_eq!(link_fresh(&mut s, &ids[..2]), 1); - assert_eq!(link_fresh(&mut s, &ids[2..]), 2); - assert_eq!(s.execute("layer.linkLayers", json!({})).unwrap()["linked"], false); - assert_eq!((group(&s, ids[2]), group(&s, ids[3])), (None, None)); - assert_eq!((group(&s, ids[0]), group(&s, ids[1])), (Some(1), Some(1))); - } - - #[test] - fn selection_pruned_after_delete_and_undo() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let c = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[a, c]); - // Deleting one layer explicitly prunes it from the set. - s.execute("layer.delete", json!({"layer": a.0})).unwrap(); - assert_eq!(sel(&s), vec![c]); - s.undo(); - assert!(sel(&s).iter().all(|id| doc(&s).layer(*id).is_some())); - // Deleting with several selected removes them all, then selects the layer below. - select_all(&mut s, &[a, c]); - s.execute("layer.delete", json!({})).unwrap(); - assert!(doc(&s).layer(a).is_none() && doc(&s).layer(c).is_none()); - assert_eq!(doc(&s).layer_count(), 2); - assert_eq!(sel(&s).len(), 1); - assert!(s.active().unwrap().selected_layers.iter().all(|id| doc(&s).layer(*id).is_some())); - s.undo(); - assert_eq!(doc(&s).layer_count(), 4); - let _ = b; - // Redo then undo again keeps the set valid. - s.redo(); - assert!(sel(&s).iter().all(|id| doc(&s).layer(*id).is_some())); - } - - #[test] - fn duplicate_hide_lock_and_reverse_selected() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let c = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[a, c]); - let r = s.execute("layer.duplicate", json!({})).unwrap(); - assert_eq!(r["layers"].as_array().unwrap().len(), 2); - assert_eq!(doc(&s).layer_count(), 6); - assert_eq!(sel(&s).len(), 2); - assert!(sel(&s).iter().all(|id| *id != a && *id != c)); - s.undo(); - select_all(&mut s, &[a, b]); - s.execute("layer.hideLayers", json!({})).unwrap(); - assert!(!doc(&s).layer(a).unwrap().visible && !doc(&s).layer(b).unwrap().visible && doc(&s).layer(c).unwrap().visible); - s.execute("layer.showLayers", json!({})).unwrap(); - assert!(doc(&s).layer(a).unwrap().visible); - s.execute("layer.lockLayers", json!({})).unwrap(); - assert!(doc(&s).layer(a).unwrap().locks.all && doc(&s).layer(b).unwrap().locks.all); - assert!(s.execute("layer.translate", json!({"dx": 1})).is_err(), "locked layers don't move"); - s.execute("layer.lockLayers", json!({})).unwrap(); - assert!(!doc(&s).layer(a).unwrap().locks.all); - s.execute("layer.lockLayers", json!({"position": true})).unwrap(); - assert!(doc(&s).layer(b).unwrap().locks.position && !doc(&s).layer(c).unwrap().locks.position); - // Reverse a, b, c. - select_all(&mut s, &[a, b, c]); - s.execute("layer.arrange.reverse", json!({})).unwrap(); - assert_eq!(doc(&s).layers.iter().skip(1).map(|l| l.id).collect::>(), vec![c, b, a]); - assert_eq!(s.active().unwrap().history.entries().last().map(|e| e.as_str()), Some("Reverse")); - } - - /// #4892: Lock Image Pixels includes Lock Transparent Pixels, so the two are never both on: - /// locking the pixels clears the transparency lock, which stays off while they are locked, - /// through both `layer.setProps` and `layer.lockLayers`. - #[test] - fn locking_pixels_clears_and_holds_off_the_transparency_lock() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); - let id = LayerId(s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap()); - let locks = |s: &Session| doc(s).layer(id).unwrap().locks; - s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap(); - assert!(locks(&s).transparency); - s.execute("layer.setProps", json!({"locks": {"pixels": true}})).unwrap(); - assert!(locks(&s).pixels && !locks(&s).transparency, "locking the pixels clears the transparency lock"); - s.execute("layer.setProps", json!({"locks": {"transparency": true}})).unwrap(); - assert!(!locks(&s).transparency, "and it stays off while the pixels are locked"); - s.execute("layer.setProps", json!({"locks": {"transparency": true, "pixels": true}})).unwrap(); - assert!(locks(&s).pixels && !locks(&s).transparency, "both at once: the pixel lock wins"); - s.execute("layer.lockLayers", json!({"pixels": false})).unwrap(); - s.execute("layer.lockLayers", json!({"transparency": true})).unwrap(); - assert!(locks(&s).transparency && !locks(&s).pixels, "with the pixels unlocked it can be set again"); - s.execute("layer.lockLayers", json!({"pixels": true})).unwrap(); - assert!(locks(&s).pixels && !locks(&s).transparency); - assert!(s.execute("layer.setProps", json!({"locks": {"nope": true}})).is_err()); - } - - #[test] - fn inspect_reports_selection() { - let mut s = session(8); - let a = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - let b = rect_layer(&mut s, Rect::new(0, 0, 5, 5)); - select_all(&mut s, &[a, b]); - let v = s.execute("document.inspect", json!({})).unwrap(); - assert_eq!(v["selectedLayers"], json!([a.0, b.0])); - assert_eq!(v["layers"][0]["selected"], true); - } -} diff --git a/crates/engine/src/layer_place.rs b/crates/engine/src/layer_place.rs new file mode 100644 index 0000000000..3cb9c1e854 --- /dev/null +++ b/crates/engine/src/layer_place.rs @@ -0,0 +1,113 @@ +//! Where a new layer goes, as in Photoshop. Above the active layer, or as the top layer inside the +//! active group when that group is open (an artboard, open or closed, when the layer's content is +//! on the board); with nothing active, at the top of the stack. A layer that lands inside a clipping mask joins it: it is clipped when the layer +//! just above its slot is clipped, the rule Photoshop follows on every insert (#2203), so a new +//! layer (or group) between a base and its clipped layers never becomes their (empty) base. +//! Artboards are never nested or clipped: a new one goes to the top level. + +use photocraft_doc::{Document, Layer, LayerContent, LayerId, LayerPath, MAX_GROUP_DEPTH}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Place { + /// New Layer, New Group, new adjustment and fill layers: inside the anchor when it is an open + /// group (or an artboard the content is on), else directly above it. A locked group takes the + /// layer too, as in Photoshop (checked with Lock All): the layer is locked through it. + Auto, + /// [`Place::Auto`] for a layer that is edited right after it is made (Type, Shape, Place's + /// transform box, Paste, the Gradient tool's live gradient, video layers, Render and Vanishing + /// Point drawing, a new frame): never inside a group with a lock of its own (all, pixels, + /// transparency or position) that would refuse that edit; the layer goes above the group. + AutoEditable, + /// Directly above the anchor, never inside it: a layer made from another layer, or the second + /// and later of several new layers. + Above, + /// Directly below the anchor (⌘-click on the Layers panel's New Layer or New Group button). Never + /// under the Background, where it goes above instead. + Below, +} + +/// The slot `layer` would take: its parent's path (empty for the root) and its index among the +/// parent's children (bottom to top). A missing or stale `anchor` means the top of the stack. +pub fn insertion_point(doc: &Document, anchor: Option, layer: &Layer, place: Place) -> (LayerPath, usize) { + let root_top = (Vec::new(), doc.layers.len()); + let Some(path) = anchor.and_then(|a| doc.path_of(a)) else { return root_top }; + // An artboard stays at the top level: beside the anchor's top-level layer. + let path = if layer.artboard().is_some() { path.get(..1).map(<[usize]>::to_vec).unwrap_or_default() } else { path }; + let (Some((&k, parent)), Some(target)) = (path.split_last(), doc.layer_at(&path)) else { return root_top }; + let locks = &target.locks; + let editable = !(locks.all || locks.pixels || locks.transparency || locks.position); + match place { + Place::Auto | Place::AutoEditable if takes_new_layers(target, path.len(), layer) && (place == Place::Auto || editable) => { + (path.clone(), target.children().map_or(0, <[Layer]>::len)) + } + Place::Below if crate::background_cmds::background_id(doc) != Some(target.id) => (parent.to_vec(), k), + _ => (parent.to_vec(), k + 1), + } +} + +/// Inserts `layer` at its [`insertion_point`], clipped when it lands inside a clipping mask (a +/// flag it already has is kept unless nothing is below it to clip to; an artboard is never +/// clipped); returns its id. Never fails: a slot that can't be reached puts +/// the layer on top of the stack. +pub fn insert_new(doc: &mut Document, anchor: Option, mut layer: Layer, place: Place) -> LayerId { + let id = layer.id; + let (parent, index) = insertion_point(doc, anchor, &layer, place); + let sib = if parent.is_empty() { Some(&mut doc.layers) } else { doc.layer_at_mut(&parent).and_then(Layer::children_mut) }; + if let Some(sib) = sib { + let mut index = index.min(sib.len()); + if layer.artboard().is_some() { + // Past the clipped layers above the slot, so their clipping mask stays whole. + while sib.get(index).is_some_and(|above| above.clipped) { + index += 1; + } + layer.clipped = false; + } else if sib.get(index).is_some_and(|above| above.clipped) { + layer.clipped = true; + } else if index == 0 { + // Nothing below to clip to (a copied clipped layer pasted into an empty group). + layer.clipped = false; + } + sib.insert(index, layer); + } else { + doc.layers.push(layer); + } + id +} + +/// Does the group `target` at depth `depth` (its path length) take a new `layer` as its top +/// child? An open group or an artboard does, locked or not (Photoshop puts a new layer inside a +/// group locked with Lock All), unless nesting would pass [`MAX_GROUP_DEPTH`]. An artboard +/// takes a layer with content only when that content is centred on the board, as callers place +/// it for the canvas (Place, Paste, a Type or Shape click): elsewhere the board would clip it. +fn takes_new_layers(target: &Layer, depth: usize, layer: &Layer) -> bool { + let LayerContent::Group(g) = &target.content else { return false }; + let on_board = |rect: photocraft_geom::Rect| { + crate::layer_multi_cmds::layer_bounds(layer).is_none_or(|b| { + let (cx, cy) = (b.x0 / 2 + b.x1 / 2, b.y0 / 2 + b.y1 / 2); + rect.contains(cx, cy) + }) + }; + let open = match &g.artboard { + Some(board) => on_board(board.rect), + None => g.expanded, + }; + open && layer.artboard().is_none() && depth + subtree_depth(layer) <= MAX_GROUP_DEPTH +} + +/// Group nesting inside `layer` itself: 0 for a layer or an empty group, as +/// [`Document::max_group_depth`] counts it. Explicit stack: pasted groups can be deep. +fn subtree_depth(layer: &Layer) -> usize { + let mut max = 0; + let mut stack: Vec<(&Layer, usize)> = vec![(layer, 0)]; + while let Some((l, depth)) = stack.pop() { + if let Some(children) = l.children().filter(|c| !c.is_empty()) { + max = max.max(depth + 1); + stack.extend(children.iter().map(|c| (c, depth + 1))); + } + } + max +} + +#[cfg(test)] +#[path = "layer_place_tests.rs"] +mod tests; diff --git a/crates/engine/src/layer_place_tests.rs b/crates/engine/src/layer_place_tests.rs new file mode 100644 index 0000000000..5a84035426 --- /dev/null +++ b/crates/engine/src/layer_place_tests.rs @@ -0,0 +1,357 @@ +use photocraft_doc::{Document, Layer, LayerContent, LayerId, MAX_GROUP_DEPTH}; +use serde_json::{Value, json}; + +use super::*; +use crate::Session; + +/// Background, then `A` and `B` (`B` active). +fn session() -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + let a = id(s.execute("layer.new.layer", json!({"name": "A"})).unwrap()); + let b = id(s.execute("layer.new.layer", json!({"name": "B"})).unwrap()); + (s, a, b) +} + +fn id(v: Value) -> LayerId { + LayerId(v["layer"].as_u64().unwrap()) +} + +fn doc(s: &Session) -> &Document { + &s.active().unwrap().doc +} + +fn top(s: &Session) -> Vec { + doc(s).layers.iter().map(|l| l.id).collect() +} + +fn children(s: &Session, group: LayerId) -> Vec { + doc(s).layer(group).unwrap().children().unwrap().iter().map(|l| l.id).collect() +} + +fn set_expanded(s: &mut Session, group: LayerId, expanded: bool) { + s.execute("layer.setExpanded", json!({"layer": group.0, "expanded": expanded})).unwrap(); +} + +fn select(s: &mut Session, layer: LayerId) { + s.execute("layer.select", json!({"layer": layer.0})).unwrap(); +} + +#[test] +fn a_new_layer_goes_inside_an_open_active_group_and_above_a_closed_one() { + let (mut s, a, b) = session(); + select(&mut s, a); + let g = id(s.execute("layer.groupLayers", json!({})).unwrap()); + // A group made from layers starts closed: the new layer goes above it. + let above = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(top(&s)[1..], [g, above, b]); + assert_eq!(children(&s, g), vec![a]); + // Opened, it takes the new layer as its top layer. + select(&mut s, g); + set_expanded(&mut s, g, true); + let inside = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(children(&s, g), vec![a, inside]); + // A new empty group is open, so the next new layer goes into it (New Group, then New Layer). + s.execute("select.deselectLayers", json!({})).unwrap(); + let empty = id(s.execute("layer.new.group", json!({})).unwrap()); + let first = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(children(&s, empty), vec![first]); + // The same goes for the other new layers: adjustment, fill, type and shape layers. + select(&mut s, empty); + for (cmd, p) in [ + ("layer.newAdjustmentLayer.invert", json!({})), + ("layer.newFillLayer.solidColor", json!({"color": "#ff0000"})), + ("type.create", json!({"x": 2, "y": 10, "text": "Hi"})), + ("shape.create", json!({"kind": "rect", "rect": [1, 1, 5, 5]})), + ] { + select(&mut s, empty); + let l = id(s.execute(cmd, p).unwrap()); + assert_eq!(children(&s, empty).last(), Some(&l), "{cmd} goes inside the open group"); + } +} + +#[test] +fn artboards_and_locked_groups_take_new_layers() { + let (mut s, _, b) = session(); + let board = id(s.execute("layer.new.artboard", json!({"rect": [0, 0, 16, 16]})).unwrap()); + set_expanded(&mut s, board, false); + select(&mut s, board); + let l = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(doc(&s).artboard_of(l), Some(board), "an artboard takes the layer even when closed"); + // Photoshop puts a new layer inside an open group locked with Lock All too; the layer is + // locked through the group. + select(&mut s, b); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + s.execute("layer.setProps", json!({"layer": g.0, "locks": {"all": true}})).unwrap(); + select(&mut s, g); + let inside = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(children(&s, g), vec![inside]); + assert!(doc(&s).effective_locks(inside).all); +} + +#[test] +fn below_puts_the_new_layer_under_the_active_layer() { + let (mut s, a, b) = session(); + let bg = top(&s)[0]; + let l = id(s.execute("layer.new.layer", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s), vec![bg, a, l, b]); + // Never under the Background, and never inside an open group. + select(&mut s, bg); + let l2 = id(s.execute("layer.new.layer", json!({"position": "below"})).unwrap()); + assert_eq!(top(&s)[..2], [bg, l2]); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + let l3 = id(s.execute("layer.new.layer", json!({"position": "below"})).unwrap()); + assert!(children(&s, g).is_empty()); + let root = top(&s); + assert_eq!(root.iter().position(|x| *x == l3).unwrap() + 1, root.iter().position(|x| *x == g).unwrap()); + assert!(s.execute("layer.new.layer", json!({"position": "yes"})).is_err()); +} + +/// The clipping rule Photoshop follows on every insert (#2203): a new layer is clipped exactly +/// when the layer just above its slot is clipped. +#[test] +fn a_new_layer_joins_the_clipping_mask_it_lands_in() { + let (mut s, base, c1) = session(); + let c2 = id(s.execute("layer.new.layer", json!({"name": "C2"})).unwrap()); + for c in [c1, c2] { + s.execute("layer.setProps", json!({"layer": c.0, "clipped": true})).unwrap(); + } + let clipped = |s: &Session, l: LayerId| doc(s).layer(l).unwrap().clipped; + // On the base: between the base and its clipped layers. + select(&mut s, base); + let on_base = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert!(clipped(&s, on_base)); + // In the middle of the chain. + select(&mut s, c1); + let mid = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert!(clipped(&s, mid)); + // Above the top clipped layer it is outside the mask. + select(&mut s, c2); + let above = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert!(!clipped(&s, above)); + // ⌘-click New Layer under a clipped layer joins it too; under the base it does not. + select(&mut s, c2); + let l = id(s.execute("layer.new.layer", json!({"position": "below"})).unwrap()); + assert!(clipped(&s, l)); + select(&mut s, base); + let l = id(s.execute("layer.new.layer", json!({"position": "below"})).unwrap()); + assert!(!clipped(&s, l)); + // A group joins as well (Photoshop clips groups too), and so do the other new layers. + select(&mut s, base); + let l = id(s.execute("layer.new.group", json!({})).unwrap()); + assert!(clipped(&s, l)); + select(&mut s, base); + let l = id(s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap()); + assert!(clipped(&s, l)); + // A layer made from a clipped layer (Layer via Copy) stays in its mask. + select(&mut s, c1); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap(); + s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); + let l = id(s.execute("layer.new.layerViaCopy", json!({})).unwrap()); + assert!(clipped(&s, l)); +} + +#[test] +fn a_pasted_layer_goes_into_an_open_group() { + let (mut s, a, _) = session(); + // A whole pixel layer on the clipboard (no selection). + select(&mut s, a); + s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + let into = id(s.execute("layer.new.group", json!({"name": "Into"})).unwrap()); + let pasted = id(s.execute("edit.paste", json!({})).unwrap()); + assert_eq!(children(&s, into), vec![pasted]); +} + +/// The second and later of several new layers stack above the first, even when the first is an +/// open group: it must not swallow them. +#[test] +fn later_layers_of_a_batch_go_above_the_first() { + let (s, _, b) = session(); + let mut d = doc(&s).clone(); + let first = insert_new(&mut d, Some(b), Layer::group("open", vec![]), Place::Auto); + let next = Layer::raster("next", d.pixel_format()); + let second = insert_new(&mut d, Some(first), next, Place::Above); + let root: Vec = d.layers.iter().map(|l| l.id).collect(); + assert_eq!(root[root.len() - 2..], [first, second]); + assert!(d.layer(first).unwrap().children().unwrap().is_empty()); +} + +#[test] +fn nesting_stops_at_the_group_depth_limit() { + let mut doc = Document::new("deep", photocraft_doc::Size::new(8, 8), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + // A chain of open groups as deep as allowed, with a layer at the bottom. + let mut inner = Layer::raster("deep", doc.pixel_format()); + let deep_id = inner.id; + let mut ids = Vec::new(); + for i in 0..MAX_GROUP_DEPTH { + let g = Layer::group(format!("g{i}"), vec![inner]); + ids.push(g.id); + inner = g; + } + doc.layers.push(inner); + assert_eq!(doc.max_group_depth(), MAX_GROUP_DEPTH); + let innermost = *ids.first().unwrap(); + // A plain layer fits beside `deep`, inside the innermost group (the depth stays the same)… + let layer = Layer::raster("fits", doc.pixel_format()); + let l = insert_new(&mut doc, Some(innermost), layer, Place::Auto); + assert_eq!(doc.max_group_depth(), MAX_GROUP_DEPTH); + assert!(doc.layer(innermost).unwrap().children().unwrap().iter().any(|c| c.id == l)); + // …but a group with a layer in it would go one deeper: it goes beside the group instead. + let nested = Layer::group("nested", vec![Layer::raster("x", doc.pixel_format())]); + let n = insert_new(&mut doc, Some(innermost), nested, Place::Auto); + assert_eq!(doc.max_group_depth(), MAX_GROUP_DEPTH); + assert!(!doc.layer(innermost).unwrap().children().unwrap().iter().any(|c| c.id == n)); + assert!(doc.layer(deep_id).is_some()); +} + +#[test] +fn a_missing_or_stale_anchor_means_the_top_of_the_stack() { + let (s, _, _) = session(); + let mut d = doc(&s).clone(); + for anchor in [None, Some(LayerId(u64::MAX))] { + for place in [Place::Auto, Place::Above, Place::Below] { + let layer = Layer::raster("x", d.pixel_format()); + let l = insert_new(&mut d, anchor, layer, place); + assert_eq!(d.layers.last().map(|x| x.id), Some(l), "{anchor:?} {place:?}"); + } + } + assert!(matches!(d.layers.last().unwrap().content, LayerContent::Raster(_))); +} + +/// Any lock on an open group still lets New Layer, New Group and new adjustment and fill layers in, +/// as in Photoshop (checked with Lock All). A layer that is edited right after it is made (Type, +/// Shape, Paste, Render…) goes above a group whose lock would refuse that edit, +/// so it never comes out locked. +#[test] +fn locked_groups_take_new_layers_but_not_the_ones_edited_at_once() { + for locks in [json!({"all": true}), json!({"pixels": true}), json!({"transparency": true}), json!({"position": true})] { + let (mut s, _, b) = session(); + select(&mut s, b); + s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); + s.execute("select.all", json!({})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + let g = id(s.execute("layer.new.group", json!({})).unwrap()); + s.execute("layer.setProps", json!({"layer": g.0, "locks": locks})).unwrap(); + for (cmd, p) in [ + ("layer.new.layer", json!({})), + ("layer.new.group", json!({})), + ("layer.newAdjustmentLayer.invert", json!({})), + ("layer.newFillLayer.solidColor", json!({"color": "#ff0000"})), + ] { + select(&mut s, g); + let l = id(s.execute(cmd, p).unwrap()); + assert_eq!(children(&s, g).last(), Some(&l), "{cmd} goes inside the group locked with {locks}"); + } + for (cmd, p) in [ + ("type.create", json!({"x": 2, "y": 10, "text": "Hi"})), + ("shape.create", json!({"kind": "rect", "rect": [1, 1, 5, 5]})), + ("edit.paste", json!({})), + ("filter.render.tree", json!({})), + ("gradient.fill.create", json!({"from": [0, 0], "to": [16, 16]})), + ] { + select(&mut s, g); + let l = id(s.execute(cmd, p).unwrap_or_else(|e| panic!("{cmd} with {locks}: {e}"))); + assert!(!children(&s, g).contains(&l), "{cmd} stays out of the group locked with {locks}"); + assert_eq!(top(&s).iter().position(|x| *x == l), top(&s).iter().position(|x| *x == g).map(|i| i + 1), "{cmd} directly above the group"); + assert!(!doc(&s).effective_locks(l).any(), "{cmd}: the new layer is not locked"); + if cmd == "filter.render.tree" { + assert!(doc(&s).layer(l).unwrap().surface().unwrap().tile_count() > 0, "Render drew the tree with {locks}"); + } + } + } + // An open group inside a locked one still takes New Layer (the lock reaches it through the outer group). + let (mut s, _, b) = session(); + select(&mut s, b); + let outer = id(s.execute("layer.new.group", json!({})).unwrap()); + let inner = id(s.execute("layer.new.group", json!({})).unwrap()); + assert_eq!(children(&s, outer), vec![inner]); + s.execute("layer.setProps", json!({"layer": outer.0, "locks": {"pixels": true}})).unwrap(); + select(&mut s, inner); + let plain = id(s.execute("layer.new.layer", json!({})).unwrap()); + assert_eq!(children(&s, inner), vec![plain]); +} + +/// Stamp Visible goes directly above an open active group, never inside it: the stamp is the +/// composite already, and inside it the group's opacity, mode or mask would apply to it again. +/// With a layer inside a group active, it still lands above that layer, inside the group. +#[test] +fn stamp_visible_goes_above_an_open_group() { + let (mut s, a, b) = session(); + select(&mut s, a); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + let g = id(s.execute("layer.groupLayers", json!({})).unwrap()); + set_expanded(&mut s, g, true); + s.execute("layer.setProps", json!({"layer": g.0, "opacity": 0.5})).unwrap(); + select(&mut s, g); + let stamp = id(s.execute("layer.stampVisible", json!({})).unwrap()); + assert_eq!(children(&s, g), vec![a], "the stamp stays out of the group"); + assert_eq!(top(&s)[1..], [g, stamp, b]); + // A layer inside the group active: the stamp goes above it, inside the group. + select(&mut s, a); + let inside = id(s.execute("layer.stampVisible", json!({})).unwrap()); + assert_eq!(children(&s, g), vec![a, inside]); +} + +/// An artboard takes a new layer whose content is centred on it, and a new empty layer; content +/// placed elsewhere (Place and Paste centre on the canvas) stays at the top level instead of +/// being clipped away by the board. +#[test] +fn an_artboard_takes_only_content_that_is_on_it() { + let (mut s, _, _) = session(); + let board = id(s.execute("layer.new.artboard", json!({"rect": [0, 0, 8, 8]})).unwrap()); + let mut d = doc(&s).clone(); + let raster = |d: &Document, r: photocraft_geom::Rect| { + let mut l = Layer::raster("content", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(r, &[1.0, 0.0, 0.0, 1.0]); + l + }; + let on = raster(&d, photocraft_geom::Rect::new(1, 1, 5, 5)); + let on = insert_new(&mut d, Some(board), on, Place::Auto); + assert_eq!(d.artboard_of(on), Some(board)); + let off = raster(&d, photocraft_geom::Rect::new(10, 10, 16, 16)); + let off = insert_new(&mut d, Some(board), off, Place::Auto); + assert_eq!(d.artboard_of(off), None); + let root: Vec = d.layers.iter().map(|l| l.id).collect(); + assert_eq!(root.iter().position(|x| *x == off), root.iter().position(|x| *x == board).map(|i| i + 1)); +} + +/// An artboard is never nested or clipped: a copied board goes beside the anchor's top-level +/// layer, even after a first copy went into a group, and never joins a clipping mask. +#[test] +fn artboards_stay_at_the_top_level_and_unclipped() { + let (mut s, a, b) = session(); + let board = id(s.execute("layer.new.artboard", json!({"rect": [0, 0, 8, 8]})).unwrap()); + s.execute("layer.setProps", json!({"layer": b.0, "clipped": true})).unwrap(); + let mut d = doc(&s).clone(); + let copy = d.layer(board).unwrap().duplicate(); + // On a clipping base: at the top level above its clipping mask (B keeps A as its base), not + // clipped. + let c = insert_new(&mut d, Some(a), copy.duplicate(), Place::Auto); + let root: Vec = d.layers.iter().map(|l| l.id).collect(); + let at = |id: LayerId| root.iter().position(|x| *x == id).unwrap(); + assert_eq!((at(b), at(c)), (at(a) + 1, at(a) + 2)); + assert!(!d.layer(c).unwrap().clipped); + // After a layer that went into an open group: beside the group, not inside it. + let g = insert_new(&mut d, Some(a), Layer::group("open", vec![]), Place::Above); + let first = Layer::raster("first", d.pixel_format()); + let first = insert_new(&mut d, Some(g), first, Place::Auto); + assert!(d.layer(g).unwrap().children().unwrap().iter().any(|l| l.id == first)); + let second = insert_new(&mut d, Some(first), copy, Place::Above); + assert!(d.layers.iter().any(|l| l.id == second), "the board is not nested in the group"); + assert_eq!(d.artboards().len(), 3); +} + +/// A copied clipped layer pasted into an empty group has nothing below it to clip to. +#[test] +fn a_clipped_layer_with_nothing_below_is_released() { + let (s, _, b) = session(); + let mut d = doc(&s).clone(); + let g = insert_new(&mut d, Some(b), Layer::group("empty", vec![]), Place::Auto); + let mut l = Layer::raster("was clipped", d.pixel_format()); + l.clipped = true; + let l = insert_new(&mut d, Some(g), l, Place::Auto); + assert!(d.layer(g).unwrap().children().unwrap().iter().any(|c| c.id == l)); + assert!(!d.layer(l).unwrap().clipped); +} diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rs index 95b5476850..176f08b264 100644 --- a/crates/engine/src/layer_style.rs +++ b/crates/engine/src/layer_style.rs @@ -12,6 +12,8 @@ use crate::commands::{CommandSpec, blend_from_str}; use crate::presets::{always, bad}; use crate::{EngineError, Result, Session}; +mod gradient_overlay; + /// Parses a gradient style name (`linear`, `radial`, `angle`, `reflected`, `diamond`). pub fn gradient_style(s: &str) -> GradientStyle { match s.to_ascii_lowercase().as_str() { @@ -45,6 +47,9 @@ fn color_of(p: &Value, k: &str) -> Option { let h = |i: usize| s.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map_or(0.0, |v| f32::from(v) / 255.0); (s.len() >= 6).then_some(Color::rgb(h(0), h(2), h(4))) } + Some(v @ Value::Object(_)) => serde_json::from_value::(v.clone()) + .ok() + .filter(|c| c.c.iter().all(|v| v.is_finite()) && c.alpha.is_finite() && (0.0..=1.0).contains(&c.alpha)), _ => None, } } @@ -64,17 +69,79 @@ fn gradient_stops(p: &Value, from: Color, to: Color) -> Vec<(f32, Color)> { None => stops, } } +/// The gradient of a stroke or overlay's params. Bad `stops` are ignored here, as bad values of +/// any other key are; the commands refuse them first ([`check_gradient_params`]). fn gradient(p: &Value) -> Gradient { + let stops = match stops_param(p, "layer.layerStyle").ok().flatten() { + Some(stops) => stops, + None => gradient_stops(p, color(p, "from", [0.0; 3]), color(p, "to", [1.0; 3])), + }; Gradient { - stops: gradient_stops(p, color(p, "from", [0.0; 3]), color(p, "to", [1.0; 3])), + stops, + opacity_stops: opacity_stops_param(p, "layer.layerStyle").ok().flatten().unwrap_or_default(), style: gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear")), angle: f(p, "angle", 90.0), - scale: f(p, "scale", 100.0) / 100.0, + scale: (f(p, "scale", 100.0) / 100.0).clamp(0.1, 1.5), reverse: b(p, "reverse", false), ..Gradient::default() } } +/// `Err` when `stops` or `opacityStops` are present but malformed, so a command refuses them +/// instead of silently keeping the old gradient. +pub fn check_gradient_params(p: &Value, cmd: &str) -> Result<()> { + stops_param(p, cmd)?; + opacity_stops_param(p, cmd)?; + Ok(()) +} + +/// Most stops a gradient parameter may carry. +const MAX_STOPS: usize = 256; + +/// Explicit colour stops (#5177): `"stops": [[position 0..1, "#rrggbb" | [r, g, b]], …]`, at +/// least 2 and at most [`MAX_STOPS`], returned sorted by position. `None` without the key. +fn stops_param(p: &Value, cmd: &str) -> Result>> { + let Some(v) = p.get("stops") else { return Ok(None) }; + let bad = |msg: String| EngineError::BadParams { cmd: cmd.into(), msg }; + let a = v.as_array().ok_or_else(|| bad("\"stops\" must be an array of [position, colour]".into()))?; + if a.len() < 2 { + return Err(bad("\"stops\" needs at least 2 stops".into())); + } + if a.len() > MAX_STOPS { + return Err(bad(format!("\"stops\": at most {MAX_STOPS} stops"))); + } + let mut stops = Vec::with_capacity(a.len()); + for (i, s) in a.iter().enumerate() { + let t = s.get(0).and_then(Value::as_f64).filter(|t| t.is_finite()).ok_or_else(|| bad(format!("\"stops\"[{i}] must be [position 0..1, colour]")))?; + let c = s.get(1).and_then(|c| color_of(&json!({"c": c}), "c")).ok_or_else(|| bad(format!("\"stops\"[{i}]: no colour (\"#rrggbb\" or [r, g, b])")))?; + stops.push((t.clamp(0.0, 1.0) as f32, c)); + } + stops.sort_by(|a, b| a.0.total_cmp(&b.0)); + Ok(Some(stops)) +} + +/// Explicit transparency stops: `"opacityStops": [[position 0..1, opacity 0..100], …]`; an empty +/// array means fully opaque. `None` without the key. +fn opacity_stops_param(p: &Value, cmd: &str) -> Result>> { + let Some(v) = p.get("opacityStops") else { return Ok(None) }; + let bad = |msg: String| EngineError::BadParams { cmd: cmd.into(), msg }; + let a = v.as_array().ok_or_else(|| bad("\"opacityStops\" must be an array of [position, opacity]".into()))?; + if a.len() > MAX_STOPS { + return Err(bad(format!("\"opacityStops\": at most {MAX_STOPS} stops"))); + } + if a.len() == 1 { + return Err(bad("\"opacityStops\" needs at least 2 stops (or none)".into())); + } + let mut stops = Vec::with_capacity(a.len()); + for (i, s) in a.iter().enumerate() { + let num = |k: usize| s.get(k).and_then(Value::as_f64).filter(|x| x.is_finite()); + let (t, o) = num(0).zip(num(1)).ok_or_else(|| bad(format!("\"opacityStops\"[{i}] must be [position 0..1, opacity 0..100]")))?; + stops.push((t.clamp(0.0, 1.0) as f32, (o / 100.0).clamp(0.0, 1.0) as f32)); + } + stops.sort_by(|a, b| a.0.total_cmp(&b.0)); + Ok(Some(stops)) +} + /// Factory defaults for each effect kind, in the command's parameter units /// (what the Layer Style dialog starts a new effect from). pub fn effect_defaults(kind: &str) -> Value { @@ -86,19 +153,19 @@ pub fn effect_defaults(kind: &str) -> Value { json!({"blend": "Multiply", "color": "#000000", "opacity": 75, "angle": 120, "useGlobalLight": true, "distance": 5, "choke": 0, "size": 5, "contour": "Linear", "antiAlias": false, "noise": 0}) } "outerGlow" => { - json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "spread": 0, "size": 5, "range": 50, "contour": "Linear", "antiAlias": false, "noise": 0}) + json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "technique": "softer", "spread": 0, "size": 5, "range": 50, "contour": "Linear", "antiAlias": false, "noise": 0}) } "innerGlow" => { - json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "source": "edge", "choke": 0, "size": 5, "contour": "Linear", "antiAlias": false, "noise": 0}) + json!({"blend": "Screen", "opacity": 75, "color": "#ffffbe", "technique": "softer", "source": "edge", "choke": 0, "size": 5, "range": 50, "contour": "Linear", "antiAlias": false, "noise": 0}) } - "stroke" => json!({"size": 3, "position": "outside", "blend": "Normal", "opacity": 100, "color": "#000000"}), + "stroke" => json!({"size": 1, "position": "inside", "blend": "Normal", "opacity": 100, "overprint": false, "color": "#000000"}), "colorOverlay" => json!({"blend": "Normal", "color": "#ff0000", "opacity": 100}), "gradientOverlay" => { - json!({"blend": "Normal", "opacity": 100, "from": "#000000", "to": "#ffffff", "reverse": false, "style": "linear", "angle": 90, "scale": 100}) + json!({"blend": "Normal", "opacity": 100, "from": "#000000", "to": "#ffffff", "reverse": false, "style": "linear", "angle": 90, "scale": 100, "method": "custom"}) } "patternOverlay" => json!({"blend": "Normal", "opacity": 100, "pattern": "", "angle": 0, "scale": 100, "link": true}), "bevelEmboss" => { - json!({"style": "inner", "technique": "smooth", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "useGlobalLight": true, "altitude": 30, "glossContour": "Linear", "contour": false, "contourName": "Linear", "contourRange": 50, "contourAntiAlias": false}) + json!({"style": "inner", "technique": "smooth", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "useGlobalLight": true, "altitude": 30, "glossContour": "Linear", "glossAntiAlias": false, "highlightBlend": "Screen", "highlightColor": "#ffffff", "highlightOpacity": 75, "shadowBlend": "Multiply", "shadowColor": "#000000", "shadowOpacity": 75, "contour": false, "contourName": "Linear", "contourRange": 50, "contourAntiAlias": false}) } "satin" => { json!({"blend": "Multiply", "color": "#000000", "opacity": 50, "angle": 19, "distance": 11, "size": 14, "contour": "Linear", "antiAlias": true, "invert": true}) @@ -460,16 +527,27 @@ pub fn contours_from_shc(bytes: &[u8]) -> Result> { /// An `.shc` file holding `contour` (Contour Editor › Save…). pub fn contour_to_shc(contour: &Contour) -> Result> { - let (name, points) = match contour { - Contour::Linear => ("Linear".to_string(), vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 1.0, output: 1.0 }]), - Contour::Custom { name, points, .. } => (name.clone(), points.clone()), - }; - let points = points + contour_to_shc_all(&[contour]) +} + +/// An `.shc` file holding every contour of `list` (Save Contours…). +pub fn contour_to_shc_all(list: &[&Contour]) -> Result> { + let file: Vec = list .iter() - .enumerate() - .map(|(i, p)| photocraft_psd::shc::ShcPoint { input: p.input * 255.0, output: p.output * 255.0, continuous: !contour.is_corner(i) }) + .map(|contour| { + let (name, points) = match contour { + Contour::Linear => ("Linear".to_string(), vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 1.0, output: 1.0 }]), + Contour::Custom { name, points, .. } => (name.clone(), points.clone()), + }; + let points = points + .iter() + .enumerate() + .map(|(i, p)| photocraft_psd::shc::ShcPoint { input: p.input * 255.0, output: p.output * 255.0, continuous: !contour.is_corner(i) }) + .collect(); + photocraft_psd::shc::ShcContour { name, points } + }) .collect(); - photocraft_psd::shc::write(&[photocraft_psd::shc::ShcContour { name, points }]).map_err(|e| EngineError::Other(format!("can't write the contour: {e}"))) + photocraft_psd::shc::write(&file).map_err(|e| EngineError::Other(format!("can't write the contour: {e}"))) } /// An effect of `kind` with its factory settings (before params are applied). @@ -507,12 +585,15 @@ fn fresh_effect(kind: &str) -> Option { "innerGlow" => Effect::InnerGlow(glow()), "stroke" => Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, 1.0), - size: 3.0, - position: StrokePosition::Outside, - paint: FxPaint::Color(Color::rgb(1.0, 0.0, 0.0)), + size: 1.0, + position: StrokePosition::Inside, + paint: FxPaint::Color(Color::BLACK), + overprint: false, }), "colorOverlay" => Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), color: Color::rgb(1.0, 0.0, 0.0) }, - "gradientOverlay" => Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: Gradient::default(), dither: false }, + "gradientOverlay" => { + Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: Gradient::default(), dither: false, interpolation: None } + } "patternOverlay" => Effect::PatternOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), name: String::new(), @@ -544,6 +625,7 @@ fn fresh_effect(kind: &str) -> Option { altitude: 30.0, use_global_light: true, gloss_contour: Contour::Linear, + gloss_anti_alias: false, highlight: FxCommon::new(BlendMode::Screen, 0.75), highlight_color: Color::WHITE, shadow: FxCommon::new(BlendMode::Multiply, 0.75), @@ -555,11 +637,18 @@ fn fresh_effect(kind: &str) -> Option { }) } -/// Edits an existing gradient in place: only the end colours and geometry. -/// Intermediate stops, opacity stops, alignment and offset survive a round trip. +/// Edits an existing gradient in place: explicit `stops` / `opacityStops` replace the lists; +/// otherwise only the end colours and geometry change, and intermediate stops, opacity stops, +/// alignment and offset survive a round trip. fn overlay_gradient(g: &mut Gradient, p: &Value) { + if let Some(stops) = stops_param(p, "layer.layerStyle").ok().flatten() { + g.stops = stops; + } + if let Some(os) = opacity_stops_param(p, "layer.layerStyle").ok().flatten() { + g.opacity_stops = os; + } // A method rebuilds the gradient from its end colours (the stops between are the old curve). - if p.get("method").and_then(Value::as_str).is_some_and(|m| photocraft_io::GradientMethod::from_name(m).is_some()) { + if p.get("stops").is_none() && p.get("method").and_then(Value::as_str).is_some_and(|m| photocraft_io::GradientMethod::from_name(m).is_some()) { let ends = (g.stops.first().map(|s| s.1), g.stops.last().map(|s| s.1)); let from = color_of(p, "from").or(ends.0).unwrap_or(Color::BLACK); let to = color_of(p, "to").or(ends.1).unwrap_or(Color::WHITE); @@ -582,7 +671,7 @@ fn overlay_gradient(g: &mut Gradient, p: &Value) { g.angle = x; } if let Some(x) = f_of(p, "scale") { - g.scale = (x / 100.0).clamp(0.01, 10.0); + g.scale = (x / 100.0).clamp(0.1, 1.5); } if let Some(x) = b_of(p, "reverse") { g.reverse = x; @@ -593,234 +682,267 @@ fn overlay_gradient(g: &mut Gradient, p: &Value) { /// parameter units. Absent (or unparseable) keys leave the effect unchanged, so /// a dialog round trip never touches what it doesn't model: an imported /// gradient stroke keeps its gradient while its opacity is edited. +fn overlay_color(p: &Value, k: &str, c: &mut Color) { + if let Some(x) = color_of(p, k) { + *c = x; + } +} + +fn overlay_contour(p: &Value, key: &str, c: &mut Contour) { + if let Some(x) = p.get(key).and_then(contour_from_param) { + *c = x; + } +} + +fn overlay_bool(p: &Value, key: &str, b: &mut bool) { + if let Some(x) = b_of(p, key) { + *b = x; + } +} + +fn overlay_common(c: &mut FxCommon, p: &Value) { + if let Some(x) = b_of(p, "enabled") { + c.enabled = x; + } + if let Some(x) = p.get("blend").and_then(Value::as_str).and_then(blend_from_str) { + c.blend = x; + } + if let Some(x) = f_of(p, "opacity") { + c.opacity = (x / 100.0).clamp(0.0, 1.0); + } +} + +fn overlay_shadow(s: &mut Shadow, p: &Value, inner: bool) { + overlay_common(&mut s.common, p); + overlay_color(p, "color", &mut s.color); + if let Some(x) = f_of(p, "angle") { + s.angle = x; + } + if let Some(x) = b_of(p, "useGlobalLight") { + s.use_global_light = x; + } + if let Some(x) = f_of(p, "distance") { + s.distance = x; + } + if let Some(x) = f_of(p, if inner { "choke" } else { "spread" }) { + s.spread = (x / 100.0).clamp(0.0, 1.0); + } + if let Some(x) = f_of(p, "size") { + s.size = x; + } + overlay_contour(p, "contour", &mut s.contour); + overlay_bool(p, "antiAlias", &mut s.anti_alias); + if let Some(x) = f_of(p, "noise") { + s.noise = (x / 100.0).clamp(0.0, 1.0); + } + if !inner && let Some(x) = b_of(p, "knocksOut") { + s.knocks_out = x; + } +} + +fn overlay_glow(g: &mut Glow, p: &Value, inner: bool) { + overlay_common(&mut g.common, p); + if let Some(c) = color_of(p, "color") { + g.paint = FxPaint::Color(c); + } + if let Some(t) = p.get("technique").and_then(Value::as_str) { + g.technique = if t == "precise" { GlowTechnique::Precise } else { GlowTechnique::Softer }; + } + if let Some(x) = f_of(p, if inner { "choke" } else { "spread" }) { + g.spread = (x / 100.0).clamp(0.0, 1.0); + } + if let Some(x) = f_of(p, "size") { + g.size = x; + } + overlay_contour(p, "contour", &mut g.contour); + overlay_bool(p, "antiAlias", &mut g.anti_alias); + if let Some(x) = f_of(p, "range") { + g.range = (x / 100.0).clamp(0.01, 1.0); + } + if let Some(x) = f_of(p, "noise") { + g.noise = (x / 100.0).clamp(0.0, 1.0); + } + if inner && let Some(s) = p.get("source").and_then(Value::as_str) { + g.source = if s == "center" { GlowSource::Center } else { GlowSource::Edge }; + } +} + +fn overlay_stroke(s: &mut StrokeFx, p: &Value) { + overlay_common(&mut s.common, p); + if let Some(x) = f_of(p, "size") { + s.size = x; + } + if let Some(pos) = p.get("position").and_then(Value::as_str) { + s.position = match pos { + "inside" => StrokePosition::Inside, + "center" => StrokePosition::Center, + _ => StrokePosition::Outside, + }; + } + overlay_bool(p, "overprint", &mut s.overprint); + if p.get("from").is_some() || p.get("stops").is_some() { + s.paint = FxPaint::Gradient(gradient(p)); + } else if let Some(c) = color_of(p, "color") { + s.paint = FxPaint::Color(c); + } +} + +fn overlay_pattern(fx: &mut Effect, p: &Value) { + let Effect::PatternOverlay { common, name, id, scale, angle, link, phase } = fx else { return }; + overlay_common(common, p); + if let Some(x) = p.get("pattern").and_then(Value::as_str) { + *name = x.to_string(); + *id = String::new(); + } + if let Some(x) = f_of(p, "scale") { + *scale = (x / 100.0).clamp(0.01, 10.0); + } + if let Some(x) = f_of(p, "angle") { + *angle = x; + } + if let Some(x) = b_of(p, "link") { + *link = x; + } + if let Some(x) = f_of(p, "phaseX") { + phase.0 = x; + } + if let Some(x) = f_of(p, "phaseY") { + phase.1 = x; + } +} + +fn overlay_satin(s: &mut Satin, p: &Value) { + overlay_common(&mut s.common, p); + overlay_color(p, "color", &mut s.color); + if let Some(x) = f_of(p, "angle") { + s.angle = x; + } + if let Some(x) = f_of(p, "distance") { + s.distance = x; + } + if let Some(x) = f_of(p, "size") { + s.size = x; + } + if let Some(x) = b_of(p, "invert") { + s.invert = x; + } + overlay_contour(p, "contour", &mut s.contour); + overlay_bool(p, "antiAlias", &mut s.anti_alias); +} + +fn overlay_bevel(bv: &mut Bevel, p: &Value) { + if let Some(x) = b_of(p, "enabled") { + bv.enabled = x; + } + if let Some(s) = p.get("style").and_then(Value::as_str) { + bv.style = match s { + "outer" => BevelStyle::OuterBevel, + "emboss" => BevelStyle::Emboss, + "pillow" => BevelStyle::PillowEmboss, + "stroke" => BevelStyle::StrokeEmboss, + _ => BevelStyle::InnerBevel, + }; + } + if let Some(t) = p.get("technique").and_then(Value::as_str) { + bv.technique = match t { + "chiselHard" => BevelTechnique::ChiselHard, + "chiselSoft" => BevelTechnique::ChiselSoft, + _ => BevelTechnique::Smooth, + }; + } + if let Some(x) = f_of(p, "depth") { + bv.depth = x / 100.0; + } + if let Some(d) = p.get("direction").and_then(Value::as_str) { + bv.up = d != "down"; + } + if let Some(x) = f_of(p, "size") { + bv.size = x; + } + if let Some(x) = f_of(p, "soften") { + bv.soften = x; + } + if let Some(x) = f_of(p, "angle") { + bv.angle = x; + } + if let Some(x) = f_of(p, "altitude") { + bv.altitude = x; + } + if let Some(x) = b_of(p, "useGlobalLight") { + bv.use_global_light = x; + } + overlay_contour(p, "glossContour", &mut bv.gloss_contour); + overlay_bool(p, "glossAntiAlias", &mut bv.gloss_anti_alias); + if let Some(blend) = p.get("highlightBlend").and_then(Value::as_str).and_then(blend_from_str) { + bv.highlight.blend = blend; + } + if let Some(blend) = p.get("shadowBlend").and_then(Value::as_str).and_then(blend_from_str) { + bv.shadow.blend = blend; + } + overlay_color(p, "highlightColor", &mut bv.highlight_color); + overlay_color(p, "shadowColor", &mut bv.shadow_color); + if let Some(x) = f_of(p, "highlightOpacity") { + bv.highlight.opacity = (x / 100.0).clamp(0.0, 1.0); + } + if let Some(x) = f_of(p, "shadowOpacity") { + bv.shadow.opacity = (x / 100.0).clamp(0.0, 1.0); + } + match b_of(p, "contour") { + Some(false) => bv.contour = None, + Some(true) if bv.contour.is_none() => bv.contour = Some(photocraft_doc::BevelContour::default()), + _ => {} + } + if let Some(c) = &mut bv.contour { + overlay_contour(p, "contourName", &mut c.contour); + if let Some(x) = f_of(p, "contourRange") { + c.range = (x / 100.0).clamp(0.01, 1.0); + } + overlay_bool(p, "contourAntiAlias", &mut c.anti_alias); + } + if let Some(t) = p.get("texture").and_then(Value::as_str).filter(|t| !t.is_empty()) { + bv.texture = Some(photocraft_doc::BevelTexture { + name: t.to_string(), + id: t.to_string(), + scale: (f(p, "textureScale", 100.0) / 100.0).clamp(0.01, 10.0), + depth: (f(p, "textureDepth", 100.0) / 100.0).clamp(-10.0, 10.0), + invert: b(p, "textureInvert", false), + link: b(p, "textureLink", true), + phase: (0.0, 0.0), + }); + } +} + pub fn overlay_effect(fx: &mut Effect, p: &Value) { - fn set_color(p: &Value, k: &str, c: &mut Color) { - if let Some(x) = color_of(p, k) { - *c = x; - } + if crate::commands::check_f32_values("layer.layerStyle", p).is_err() { + return; } - fn set_contour(p: &Value, key: &str, c: &mut Contour) { - if let Some(x) = p.get(key).and_then(contour_from_param) { - *c = x; - } - } - fn set_bool(p: &Value, key: &str, b: &mut bool) { - if let Some(x) = b_of(p, key) { - *b = x; - } - } - fn common(c: &mut FxCommon, p: &Value) { - if let Some(x) = b_of(p, "enabled") { - c.enabled = x; - } - if let Some(x) = p.get("blend").and_then(Value::as_str).and_then(blend_from_str) { - c.blend = x; - } - if let Some(x) = f_of(p, "opacity") { - c.opacity = (x / 100.0).clamp(0.0, 1.0); - } - } - let inner_shadow = matches!(fx, Effect::InnerShadow(_)); - let inner_glow = matches!(fx, Effect::InnerGlow(_)); match fx { - Effect::DropShadow(s) | Effect::InnerShadow(s) => { - let inner = inner_shadow; - common(&mut s.common, p); - set_color(p, "color", &mut s.color); - if let Some(x) = f_of(p, "angle") { - s.angle = x; - } - if let Some(x) = b_of(p, "useGlobalLight") { - s.use_global_light = x; - } - if let Some(x) = f_of(p, "distance") { - s.distance = x; - } - if let Some(x) = f_of(p, if inner { "choke" } else { "spread" }) { - s.spread = (x / 100.0).clamp(0.0, 1.0); - } - if let Some(x) = f_of(p, "size") { - s.size = x; - } - set_contour(p, "contour", &mut s.contour); - set_bool(p, "antiAlias", &mut s.anti_alias); - if let Some(x) = f_of(p, "noise") { - s.noise = (x / 100.0).clamp(0.0, 1.0); - } - if !inner && let Some(x) = b_of(p, "knocksOut") { - s.knocks_out = x; - } + Effect::DropShadow(s) => overlay_shadow(s, p, false), + Effect::InnerShadow(s) => overlay_shadow(s, p, true), + Effect::OuterGlow(g) => overlay_glow(g, p, false), + Effect::InnerGlow(g) => overlay_glow(g, p, true), + Effect::Stroke(s) => overlay_stroke(s, p), + Effect::ColorOverlay { common, color } => { + overlay_common(common, p); + overlay_color(p, "color", color); } - Effect::OuterGlow(g) | Effect::InnerGlow(g) => { - let inner = inner_glow; - common(&mut g.common, p); - if let Some(c) = color_of(p, "color") { - g.paint = FxPaint::Color(c); - } - if let Some(t) = p.get("technique").and_then(Value::as_str) { - g.technique = if t == "precise" { GlowTechnique::Precise } else { GlowTechnique::Softer }; - } - if let Some(x) = f_of(p, if inner { "choke" } else { "spread" }) { - g.spread = (x / 100.0).clamp(0.0, 1.0); - } - if let Some(x) = f_of(p, "size") { - g.size = x; - } - set_contour(p, "contour", &mut g.contour); - set_bool(p, "antiAlias", &mut g.anti_alias); - if let Some(x) = f_of(p, "range") { - g.range = (x / 100.0).clamp(0.01, 1.0); - } - if let Some(x) = f_of(p, "noise") { - g.noise = (x / 100.0).clamp(0.0, 1.0); - } - if inner && let Some(s) = p.get("source").and_then(Value::as_str) { - g.source = if s == "center" { GlowSource::Center } else { GlowSource::Edge }; - } - } - Effect::Stroke(s) => { - common(&mut s.common, p); - if let Some(x) = f_of(p, "size") { - s.size = x; - } - if let Some(pos) = p.get("position").and_then(Value::as_str) { - s.position = match pos { - "inside" => StrokePosition::Inside, - "center" => StrokePosition::Center, - _ => StrokePosition::Outside, - }; - } - if p.get("from").is_some() { - s.paint = FxPaint::Gradient(gradient(p)); - } else if let Some(c) = color_of(p, "color") { - s.paint = FxPaint::Color(c); - } - } - Effect::ColorOverlay { common: c, color } => { - common(c, p); - set_color(p, "color", color); - } - Effect::GradientOverlay { common: c, gradient: g, dither } => { - common(c, p); - overlay_gradient(g, p); + Effect::GradientOverlay { common, gradient, dither, interpolation } => { + overlay_common(common, p); + gradient_overlay::overlay(gradient, interpolation, p); if let Some(x) = b_of(p, "dither") { *dither = x; } } - Effect::PatternOverlay { common: c, name, id, scale, angle, link, phase } => { - common(c, p); - if let Some(x) = p.get("pattern").and_then(Value::as_str) { - *name = x.to_string(); - *id = String::new(); - } - if let Some(x) = f_of(p, "scale") { - *scale = (x / 100.0).clamp(0.01, 10.0); - } - if let Some(x) = f_of(p, "angle") { - *angle = x; - } - if let Some(x) = b_of(p, "link") { - *link = x; - } - if let Some(x) = f_of(p, "phaseX") { - phase.0 = x; - } - if let Some(x) = f_of(p, "phaseY") { - phase.1 = x; - } - } - Effect::Satin(s) => { - common(&mut s.common, p); - set_color(p, "color", &mut s.color); - if let Some(x) = f_of(p, "angle") { - s.angle = x; - } - if let Some(x) = f_of(p, "distance") { - s.distance = x; - } - if let Some(x) = f_of(p, "size") { - s.size = x; - } - if let Some(x) = b_of(p, "invert") { - s.invert = x; - } - set_contour(p, "contour", &mut s.contour); - set_bool(p, "antiAlias", &mut s.anti_alias); - } - Effect::BevelEmboss(bv) => { - if let Some(x) = b_of(p, "enabled") { - bv.enabled = x; - } - if let Some(s) = p.get("style").and_then(Value::as_str) { - bv.style = match s { - "outer" => BevelStyle::OuterBevel, - "emboss" => BevelStyle::Emboss, - "pillow" => BevelStyle::PillowEmboss, - "stroke" => BevelStyle::StrokeEmboss, - _ => BevelStyle::InnerBevel, - }; - } - if let Some(t) = p.get("technique").and_then(Value::as_str) { - bv.technique = match t { - "chiselHard" => BevelTechnique::ChiselHard, - "chiselSoft" => BevelTechnique::ChiselSoft, - _ => BevelTechnique::Smooth, - }; - } - if let Some(x) = f_of(p, "depth") { - bv.depth = x / 100.0; - } - if let Some(d) = p.get("direction").and_then(Value::as_str) { - bv.up = d != "down"; - } - if let Some(x) = f_of(p, "size") { - bv.size = x; - } - if let Some(x) = f_of(p, "soften") { - bv.soften = x; - } - if let Some(x) = f_of(p, "angle") { - bv.angle = x; - } - if let Some(x) = f_of(p, "altitude") { - bv.altitude = x; - } - if let Some(x) = b_of(p, "useGlobalLight") { - bv.use_global_light = x; - } - set_contour(p, "glossContour", &mut bv.gloss_contour); - // The Contour element: `contour` turns it on/off; its curve, Range and - // Anti-aliased edit the element in place, so an imported curve survives. - match b_of(p, "contour") { - Some(false) => bv.contour = None, - Some(true) if bv.contour.is_none() => bv.contour = Some(photocraft_doc::BevelContour::default()), - _ => {} - } - if let Some(c) = &mut bv.contour { - set_contour(p, "contourName", &mut c.contour); - if let Some(x) = f_of(p, "contourRange") { - c.range = (x / 100.0).clamp(0.01, 1.0); - } - set_bool(p, "contourAntiAlias", &mut c.anti_alias); - } - if let Some(t) = p.get("texture").and_then(Value::as_str).filter(|t| !t.is_empty()) { - bv.texture = Some(photocraft_doc::BevelTexture { - name: t.to_string(), - id: t.to_string(), - scale: (f(p, "textureScale", 100.0) / 100.0).clamp(0.01, 10.0), - depth: (f(p, "textureDepth", 100.0) / 100.0).clamp(-10.0, 10.0), - invert: b(p, "textureInvert", false), - link: b(p, "textureLink", true), - phase: (0.0, 0.0), - }); - } - } + Effect::PatternOverlay { .. } => overlay_pattern(fx, p), + Effect::Satin(s) => overlay_satin(s, p), + Effect::BevelEmboss(bv) => overlay_bevel(bv, p), } } /// Builds an effect of `kind` from JSON params (Photoshop units: opacity, /// spread, range in percent; sizes in px; angles in degrees). pub fn effect_from_params(kind: &str, p: &Value) -> Option { + crate::commands::check_f32_values("layer.layerStyle", p).ok()?; let mut fx = fresh_effect(kind)?; overlay_effect(&mut fx, p); Some(fx) @@ -856,6 +978,7 @@ const MAX_EFFECTS: usize = 256; /// mismatches and malformed snapshots are errors, never panics. fn replace_effects(s: &mut Session, p: &Value) -> Result { const CMD: &str = "layer.layerStyle.replace"; + crate::commands::check_f32_values(CMD, p)?; let id = crate::commands::layer_param(s, p)?; let entries = p.get("effects").and_then(Value::as_array).ok_or_else(|| bad(CMD, "need `effects`: [{kind, params, fx?}]"))?; if entries.len() > MAX_EFFECTS { @@ -865,7 +988,7 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result { let mut pats = Vec::new(); for e in entries { let kind = e.get("kind").and_then(Value::as_str).ok_or_else(|| bad(CMD, "each effect needs a `kind` string"))?; - let params = match e.get("params") { + let mut params = match e.get("params") { None | Some(Value::Null) => json!({}), Some(Value::Object(_)) => e.get("params").cloned().unwrap_or_default(), Some(_) => return Err(bad(CMD, "`params` must be an object")), @@ -876,10 +999,27 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result { if kind_of(&fx) != kind { return Err(bad(CMD, format!("effect snapshot is a {}, not `{kind}`", kind_of(&fx)))); } + if let Effect::GradientOverlay { gradient, interpolation, .. } = &fx { + let stops = interpolation.as_ref().map_or(&gradient.stops, |s| &s.stops); + if exact_stops(¶ms).as_ref() == Some(stops) + && let Some(o) = params.as_object_mut() + { + o.remove("stops"); + } + if opacity_stops_param(¶ms, CMD).ok().flatten().as_ref() == Some(&gradient.opacity_stops) + && let Some(o) = params.as_object_mut() + { + o.remove("opacityStops"); + } + } + check_gradient_params(¶ms, CMD)?; overlay_effect(&mut fx, ¶ms); fx } - None => effect_from_params(kind, ¶ms).ok_or_else(|| bad(CMD, format!("unknown effect {kind}")))?, + None => { + check_gradient_params(¶ms, CMD)?; + effect_from_params(kind, ¶ms).ok_or_else(|| bad(CMD, format!("unknown effect {kind}")))? + } }; let (fx, pat) = crate::pattern_cmds::resolve_effect(s, fx)?; if let Some(pat) = pat { @@ -894,6 +1034,9 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result { crate::pattern_cmds::ensure_in_doc(doc, pat); } let mode = doc.mode; + if !list.is_empty() { + crate::extra_cmds::background_to_layer(doc, id); + } let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; // The list's master switch (`layer.setEffectsVisible`, the Layers panel's "Effects" eye) // survives the edit: the dialog models per-effect state, not the switch (#3648). A layer @@ -905,6 +1048,8 @@ fn replace_effects(s: &mut Session, p: &Value) -> Result { } fn set_effect(s: &mut Session, p: &Value, kind: &str) -> Result { + crate::commands::check_f32_values(&format!("layer.layerStyle.{kind}"), p)?; + check_gradient_params(p, &format!("layer.layerStyle.{kind}"))?; let fx = effect_from_params(kind, p).ok_or_else(|| EngineError::Other(format!("unknown effect {kind}")))?; let (fx, pattern) = crate::pattern_cmds::resolve_effect(s, fx)?; let label = format!("Layer Style: {}", fx.label()); @@ -916,6 +1061,7 @@ fn set_effect(s: &mut Session, p: &Value, kind: &str) -> Result { } // Colours in the document's mode, as Image › Mode keeps them. let fx = fx.in_mode(doc.mode); + crate::extra_cmds::background_to_layer(doc, id); let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; // Adding or replacing one effect leaves the list's master switch alone (#3648); a fresh // effects list already has the default `enabled: true`, so a first effect still shows. @@ -936,6 +1082,7 @@ fn make_default(s: &mut Session, p: &Value) -> Result { Some(v) if v.is_object() => v.clone(), _ => return Err(bad(CMD, "`params` must be an object")), }; + crate::commands::check_f32_values(CMD, ¶ms)?; s.presets.layer_defaults.insert(kind.to_string(), params); s.presets_changed(); Ok(json!({"kind": kind})) @@ -995,23 +1142,23 @@ pub fn specs() -> Vec { style_cmd!( "outerGlow", "Outer Glow…", - r##"{"color":"#rrggbb","opacity":0..100=75,"blend":str="screen","technique":"softer|precise","spread":0..100,"size":px,"range":0..100,"contour":name|curve,"antiAlias":bool,"noise":0..100,"add":bool}"## + r##"{"color":"#rrggbb","opacity":0..100=75,"blend":str="screen","technique":"softer|precise","spread":0..100,"size":px,"range":1..100=50,"contour":name|curve,"antiAlias":bool,"noise":0..100,"add":bool}"## ), style_cmd!( "innerGlow", "Inner Glow…", - r##"{"color":"#rrggbb","opacity":0..100=75,"blend":str="screen","technique":"softer|precise","source":"edge|center","choke":0..100,"size":px,"contour":name|curve,"antiAlias":bool,"noise":0..100,"add":bool}"## + r##"{"color":"#rrggbb","opacity":0..100=75,"blend":str="screen","technique":"softer|precise","source":"edge|center","choke":0..100,"size":px,"range":1..100=50,"contour":name|curve,"antiAlias":bool,"noise":0..100,"add":bool}"## ), style_cmd!( "stroke", "Stroke…", - r##"{"size":px=3,"position":"outside|inside|center","color":"#rrggbb","from":"#rrggbb","to":"#rrggbb","method":"classic|perceptual|linear|smooth"?,"smoothness":0..100=100,"style":str,"angle":deg,"opacity":0..100,"blend":str,"add":bool}"## + r##"{"size":px=1,"position":"inside|outside|center","color":"#rrggbb","from":"#rrggbb","to":"#rrggbb","method":"classic|perceptual|linear|smooth"?,"smoothness":0..100=100,"style":str,"angle":deg,"opacity":0..100,"blend":str,"overprint":bool,"add":bool}"## ), style_cmd!("colorOverlay", "Color Overlay…", r##"{"color":"#rrggbb","opacity":0..100=100,"blend":str,"add":bool}"##), style_cmd!( "gradientOverlay", "Gradient Overlay…", - r##"{"from":"#rrggbb","to":"#rrggbb","method":"classic|perceptual|linear|smooth"? (Photoshop's interpolation; omitted: straight stops),"smoothness":0..100=100,"style":"linear|radial|angle|reflected|diamond","angle":deg=90,"scale":10..150=100,"reverse":bool,"opacity":0..100,"blend":str,"add":bool}"## + r##"{"from":"#rrggbb","to":"#rrggbb","stops":[[0..1,"#rrggbb"],.]? (2..256 source stops),"opacityStops":[[0..1,0..100],.]?,"method":"classic|perceptual|linear|smooth"? (Photoshop's interpolation; omitted on new effects: straight stops; edits retain the recorded method),"smoothness":0..100=100,"style":"linear|radial|angle|reflected|diamond","angle":deg=90,"scale":10..150=100,"reverse":bool,"opacity":0..100,"blend":str,"add":bool}"## ), style_cmd!( "patternOverlay", @@ -1021,7 +1168,7 @@ pub fn specs() -> Vec { style_cmd!( "bevelEmboss", "Bevel & Emboss…", - r##"{"style":"inner|outer|emboss|pillow|stroke","technique":"smooth|chiselHard|chiselSoft","contour":bool,"contourName":name|curve,"contourRange":1..100=50,"contourAntiAlias":bool,"glossContour":name|curve,"texture":pattern id|name?,"textureScale":1..1000=100,"textureDepth":-1000..1000=100,"textureInvert":bool,"textureLink":bool=true,"depth":1..1000=100,"direction":"up|down","size":px=5,"soften":px,"angle":deg,"altitude":deg,"add":bool}"## + r##"{"style":"inner|outer|emboss|pillow|stroke","technique":"smooth|chiselHard|chiselSoft","contour":bool,"contourName":name|curve,"contourRange":1..100=50,"contourAntiAlias":bool,"glossContour":name|curve,"glossAntiAlias":bool,"texture":pattern id|name?,"textureScale":1..1000=100,"textureDepth":-1000..1000=100,"textureInvert":bool,"textureLink":bool=true,"depth":1..1000=100,"direction":"up|down","size":px=5,"soften":px,"angle":deg,"altitude":deg,"add":bool}"## ), style_cmd!( "satin", @@ -1109,456 +1256,16 @@ fn delete_effect(s: &mut Session, p: &Value, effect: &Value) -> Result { } #[cfg(test)] -mod tests { - use super::*; - use serde_json::json; +mod tests; - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 32, "height": 32})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s - } - - fn effects(s: &Session) -> Vec { - let d = s.active().unwrap(); - d.doc.layer(d.active_layer.unwrap()).unwrap().effects.items.clone() - } - - #[test] - fn bevel_command_sets_technique_contour_and_texture() { - let mut s = session(); - s.execute("layer.layerStyle.bevelEmboss", json!({"style": "pillow", "technique": "chiselHard", "contour": true, "contourRange": 70, "texture": "Bubbles", "textureDepth": -200, "textureInvert": true})).unwrap(); - let fx = effects(&s); - let Effect::BevelEmboss(b) = &fx[0] else { panic!("{fx:?}") }; - assert_eq!((b.style, b.technique), (photocraft_doc::BevelStyle::PillowEmboss, BevelTechnique::ChiselHard)); - assert!((b.contour.as_ref().unwrap().range - 0.7).abs() < 1e-6); - let t = b.texture.as_ref().unwrap(); - assert_eq!((t.name.as_str(), t.depth, t.invert), ("Bubbles", -2.0, true)); - } - - #[test] - fn every_style_command_adds_its_effect() { - for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "bevelEmboss", "satin"] { - let mut s = session(); - s.execute(&format!("layer.layerStyle.{kind}"), json!({})).unwrap(); - let fx = effects(&s); - assert_eq!(fx.len(), 1, "{kind}"); - assert!(fx[0].enabled()); - } - } - - /// #1543: a Satin or Bevel & Emboss size or softness far past Photoshop's range rendered a - /// blur kernel of 2^62 taps ("capacity overflow"), or ran for minutes at 1e6. The blur width - /// is capped at the effect reach, so the document still renders, quickly. - #[test] - fn oversized_satin_and_bevel_sizes_render() { - for (cmd, p) in [ - ("satin", json!({"size": 1e308})), - ("satin", json!({"size": 1_000_000})), - ("bevelEmboss", json!({"size": 1e308})), - ("bevelEmboss", json!({"soften": 1e308})), - ("bevelEmboss", json!({"size": 1_000_000, "soften": 1_000_000})), - ] { - let mut s = session(); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - s.execute(&format!("layer.layerStyle.{cmd}"), p.clone()).unwrap(); - let start = std::time::Instant::now(); - s.execute("document.pixel", json!({"x": 0, "y": 0})).unwrap_or_else(|e| panic!("{cmd} {p}: {e}")); - assert!(start.elapsed().as_secs() < 20, "{cmd} {p}: {:?}", start.elapsed()); - } - } - - #[test] - fn params_are_applied_and_replace_or_add() { - let mut s = session(); - s.execute("layer.layerStyle.stroke", json!({"size": 7, "position": "inside", "color": "#00ff00", "opacity": 50})).unwrap(); - match &effects(&s)[0] { - Effect::Stroke(st) => { - assert_eq!(st.size, 7.0); - assert_eq!(st.position, StrokePosition::Inside); - assert_eq!(st.common.opacity, 0.5); - assert_eq!(st.paint, FxPaint::Color(Color::rgb(0.0, 1.0, 0.0))); - } - other => panic!("{other:?}"), - } - s.execute("layer.layerStyle.stroke", json!({"size": 2})).unwrap(); - assert_eq!(effects(&s).len(), 1, "replaces"); - s.execute("layer.layerStyle.stroke", json!({"size": 4, "add": true})).unwrap(); - assert_eq!(effects(&s).len(), 2, "adds a second instance"); - s.execute("layer.layerStyle.dropShadow", json!({"blend": "normal", "distance": 12})).unwrap(); - assert!(matches!(&effects(&s)[2], Effect::DropShadow(sh) if sh.common.blend == BlendMode::Normal && sh.distance == 12.0)); - } - - #[test] - fn clear_and_undo() { - let mut s = session(); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); - s.execute("layer.layerStyle.clear", json!({})).unwrap(); - assert!(effects(&s).is_empty()); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(effects(&s).len(), 1); - } - - fn pixel(s: &mut Session) -> Vec { - serde_json::from_value(s.execute("document.pixel", json!({"x": 3, "y": 3})).unwrap()).unwrap() - } - - fn list_enabled(s: &Session) -> bool { - let d = s.active().unwrap(); - d.doc.layer(d.active_layer.unwrap()).unwrap().effects.enabled - } - - /// #3648: the Layers panel's "Effects" eye (`layer.setEffectsVisible`) must survive a Layer - /// Style dialog OK: the dialog's whole-list replace re-creates the items but leaves the - /// list's master switch alone. - #[test] - fn replacing_the_list_keeps_a_disabled_effect_list_disabled() { - let mut s = session(); - s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap(); - let shown = pixel(&mut s); - s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap(); - let hidden = pixel(&mut s); - assert_ne!(hidden, shown, "the eye really hides the style"); - let r = s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "colorOverlay", "params": {"color": "#00ff00"}}]})).unwrap(); - assert_eq!(r["effects"], 1, "the list itself is updated"); - assert!(!list_enabled(&s), "the whole-list switch survives the edit"); - assert!(effects(&s)[0].enabled(), "the effect's own state is updated"); - assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing"); - s.execute("layer.setEffectsVisible", json!({"visible": true})).unwrap(); - assert_ne!(pixel(&mut s), hidden, "re-showing the list draws the edited effect"); - } - - /// #3648: adding or replacing one effect (`layer.layerStyle.`) also leaves the master - /// switch as the user left it. - #[test] - fn set_effect_keeps_a_disabled_effect_list_disabled() { - let mut s = session(); - s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap(); - s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap(); - let hidden = pixel(&mut s); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap(); - assert!(!list_enabled(&s), "the whole-list switch survives the edit"); - assert!(effects(&s)[0].enabled(), "the effect's own state is updated"); - assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing"); - } - - /// A layer that never had effects keeps the default: its first effect shows. - #[test] - fn a_first_effect_shows_on_a_layer_that_never_had_effects() { - let mut s = session(); - s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); - let plain = pixel(&mut s); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap(); - assert!(list_enabled(&s), "a fresh effects list defaults to enabled"); - assert_ne!(pixel(&mut s), plain, "the first effect renders"); - } - - /// #3102: an effect row dropped on the Delete button deletes only that effect, in one step. - #[test] - fn clear_one_effect_by_index() { - let mut s = session(); - s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); - let layer = s.active().unwrap().active_layer.unwrap().0; - s.execute("layer.layerStyle.clear", json!({"layer": layer, "effect": 0})).unwrap(); - assert!(matches!(effects(&s)[..], [Effect::ColorOverlay { .. }])); - for bad in [json!({"effect": 5}), json!({"effect": -1}), json!({"effect": "x"}), json!({"effect": 0, "layer": u64::MAX})] { - assert!(s.execute("layer.layerStyle.clear", bad).is_err()); - } - assert_eq!(effects(&s).len(), 1, "bad params change nothing"); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(effects(&s).len(), 2); - } - - #[test] - fn color_overlay_renders() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); - s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); - let px: Vec = serde_json::from_value(s.execute("document.pixel", json!({"x": 3, "y": 3})).unwrap()).unwrap(); - assert!(px[0] > 0.99 && px[1] < 0.01, "{px:?}"); - } - - #[test] - fn gradient_styles_parse() { - assert_eq!(gradient_style("Radial"), GradientStyle::Radial); - assert_eq!(gradient_style("diamond"), GradientStyle::Diamond); - assert_eq!(gradient_style("?"), GradientStyle::Linear); - } - - fn snapshot(fx: &Effect) -> Value { - serde_json::to_value(fx).unwrap() - } - - #[test] - fn every_kind_has_factory_defaults() { - for kind in - ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "patternOverlay", "satin", "bevelEmboss"] - { - assert!(effect_defaults(kind).as_object().is_some_and(|o| !o.is_empty()), "{kind}"); - assert!(effect_from_params(kind, &json!({})).is_some(), "{kind}"); - } - assert!(effect_from_params("nope", &json!({})).is_none()); - } - - #[test] - fn replace_builds_edits_and_preserves_instances() { - let mut s = session(); - // Two strokes: the first edits a snapshot (params overlay), the second is fresh. - let first = Effect::Stroke(StrokeFx { - common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), - size: 7.0, - position: StrokePosition::Outside, - paint: FxPaint::Gradient(Gradient { stops: vec![(0.0, Color::rgb(0.0, 0.0, 1.0)), (1.0, Color::WHITE)], ..Gradient::default() }), - }); - let r = s - .execute( - "layer.layerStyle.replace", - json!({"effects": [ - {"kind": "stroke", "fx": snapshot(&first), "params": {"opacity": 50}}, - {"kind": "stroke", "params": {"size": 2, "color": "#ffffff"}}, - ]}), - ) - .unwrap(); - assert_eq!(r["effects"], 2); - let fx = effects(&s); - assert_eq!(fx.len(), 2, "order preserved"); - match (&fx[0], &fx[1]) { - (Effect::Stroke(a), Effect::Stroke(b)) => { - assert_eq!(a.common.opacity, 0.5); - // The overlay must not clobber the gradient paint it doesn't model. - assert!(matches!(&a.paint, FxPaint::Gradient(g) if g.stops.len() == 2 && g.stops[0].1 == Color::rgb(0.0, 0.0, 1.0))); - assert_eq!(b.size, 2.0); - assert_eq!(b.paint, FxPaint::Color(Color::rgb(1.0, 1.0, 1.0))); - } - other => panic!("{other:?}"), - } - // Replacing with an empty list clears the layer. - s.execute("layer.layerStyle.replace", json!({"effects": []})).unwrap(); - assert!(effects(&s).is_empty()); - } - - #[test] - fn replace_rejects_bad_entries_gracefully() { - let mut s = session(); - let stroke = Effect::Stroke(StrokeFx { - common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), - size: 3.0, - position: StrokePosition::Outside, - paint: FxPaint::Color(Color::BLACK), - }); - for p in [ - json!({}), - json!({"effects": "nope"}), - json!({"effects": [{"params": {}}]}), - json!({"effects": [{"kind": "nope"}]}), - json!({"effects": [{"kind": "stroke", "fx": {"Bogus": {}}}]}), - json!({"effects": [{"kind": "stroke", "fx": 3}]}), - json!({"effects": [{"kind": "dropShadow", "fx": snapshot(&stroke)}]}), - json!({"effects": [{"kind": "stroke", "fx": snapshot(&stroke), "params": "nope"}]}), - ] { - assert!(s.execute("layer.layerStyle.replace", p).is_err()); - } - // A long list is refused, not run. - let many: Vec<_> = (0..257).map(|_| json!({"kind": "stroke"})).collect(); - assert!(s.execute("layer.layerStyle.replace", json!({"effects": many})).is_err()); - assert!(effects(&s).is_empty(), "nothing was applied"); - } - - #[test] - fn contour_and_noise_params_edit_existing_effects() { - let mut s = session(); - s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); - let before = effects(&s); - let Effect::DropShadow(_) = &before[0] else { panic!("{before:?}") }; - let fx0 = snapshot(&before[0]); - // Unknown contour names are carried, not clobbered to Linear. - s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "dropShadow", "fx": fx0, "params": {"contour": "Cone", "noise": 30}}]})).unwrap(); - let fx = effects(&s); - let Effect::DropShadow(sh) = &fx[0] else { panic!("{fx:?}") }; - assert_eq!(sh.contour, builtin_contour("Cone").unwrap(), "known preset applies"); - assert!((sh.noise - 0.3).abs() < 1e-6, "noise in percent"); - // An imported contour with an unknown name survives the same round trip. - let imported = Contour::Custom { - name: "Photoshop contour".into(), - points: vec![photocraft_doc::adjust::CurvePoint { input: 0.0, output: 1.0 }, photocraft_doc::adjust::CurvePoint { input: 1.0, output: 0.0 }], - corners: vec![], - }; - let mut carried = before[0].clone(); - if let Effect::DropShadow(c) = &mut carried { - c.contour = imported.clone(); - } - overlay_effect(&mut carried, &json!({"contour": "Photoshop contour"})); - let Effect::DropShadow(after) = &carried else { panic!() }; - assert_eq!(after.contour, imported); - // Linear applies by name. - let mut back = carried; - overlay_effect(&mut back, &json!({"contour": "Linear"})); - let Effect::DropShadow(lin) = &back else { panic!() }; - assert_eq!(lin.contour, Contour::Linear); - } - - #[test] - fn contour_presets_match_photoshop() { - // Photoshop 27.11, measured through Satin: Cone and Cone − Inverted Satins are identical, - // and Gaussian reads 0.36 at 0.5 through Satin but 0.64 as the preset curve (glows, gloss). - let lut = |name: &str| { - let Some(Contour::Custom { points, corners, .. }) = builtin_contour(name) else { panic!("{name}") }; - photocraft_compose::adjust::contour_curve_lut(&points, &corners) - }; - let satin = |name: &str| photocraft_compose::effects::satin_lut(&builtin_contour(name).unwrap()).unwrap(); - let at = |l: &[f32], x: f32| l[(x * (l.len() - 1) as f32).round() as usize]; - let (cone, inverted) = (satin("Cone"), satin("Cone - Inverted")); - for x in [0.1, 0.25, 0.5, 0.8] { - assert!((at(&cone, x) - at(&inverted, x)).abs() < 0.03, "{x}: {} vs {}", at(&cone, x), at(&inverted, x)); - } - assert!((at(&satin("Gaussian"), 0.5) - 0.36).abs() < 0.04, "{}", at(&satin("Gaussian"), 0.5)); - assert!((at(&lut("Gaussian"), 0.5) - 0.64).abs() < 0.03, "{}", at(&lut("Gaussian"), 0.5)); - assert!((at(&lut("Cove - Deep"), 0.0) - 0.26).abs() < 0.02, "Cove - Deep starts above zero"); - } - - #[test] - fn gradient_overlay_method_matches_photoshop() { - // Photoshop 27.11, Classic Gradient Overlay #000000 → #ffffff (Smoothness 100 %): 0.037 at - // t = 1/16 and 0.5 in the middle; straight stops would give 0.0625. - let mut s = session(); - s.execute("layer.layerStyle.gradientOverlay", json!({"from": "#000000", "to": "#ffffff", "method": "classic"})).unwrap(); - let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; - let at = |t: f32| { - let w = gradient.stops.windows(2).find(|w| t >= w[0].0 && t <= w[1].0).unwrap(); - let u = (t - w[0].0) / (w[1].0 - w[0].0).max(1e-6); - w[0].1.to_rgb()[0] + (w[1].1.to_rgb()[0] - w[0].1.to_rgb()[0]) * u - }; - assert!((at(1.0 / 16.0) - 0.037).abs() < 0.004, "{}", at(1.0 / 16.0)); - assert!((at(0.5) - 0.5).abs() < 0.004, "{}", at(0.5)); - // Without a method the two stops stay as they are. - s.execute("layer.layerStyle.gradientOverlay", json!({"from": "#000000", "to": "#ffffff", "add": true})).unwrap(); - let plain = effects(&s).iter().filter(|e| matches!(e, Effect::GradientOverlay { gradient, .. } if gradient.stops.len() == 2)).count(); - assert_eq!(plain, 1, "the added overlay keeps its two stops"); - } - - #[test] - fn contour_presets_and_custom_curves_apply_in_one_undo_step() { - let mut s = session(); - // Picking Cone: the preset's own curve, its points corners, in one undo step. - s.execute("layer.layerStyle.dropShadow", json!({"contour": "Cone", "antiAlias": true})).unwrap(); - let Effect::DropShadow(sh) = &effects(&s)[0] else { panic!() }; - assert_eq!(sh.contour, builtin_contour("Cone").unwrap()); - assert!(sh.contour.is_corner(1) && sh.anti_alias); - let Contour::Custom { points, corners, .. } = &sh.contour else { panic!() }; - let lut = photocraft_compose::adjust::contour_curve_lut(points, corners); - let at = |x: f32| lut[(x * (lut.len() - 1) as f32).round() as usize]; - // Photoshop's measured Cone: 0 at both ends, a 0.99 peak at 0.5, symmetric. - assert!(at(0.0).abs() < 1e-3 && at(1.0).abs() < 1e-3 && at(0.5) > 0.98, "a Λ: {} {} {}", at(0.0), at(0.5), at(1.0)); - assert!((at(0.25) - at(0.75)).abs() < 0.03, "symmetric: {} vs {}", at(0.25), at(0.75)); - assert_eq!(contour_param(&sh.contour), json!("Cone")); - s.execute("edit.undo", json!({})).unwrap(); - assert!(effects(&s).is_empty(), "one undo removes it"); - - // An edited curve (the Contour Editor's param) round trips exactly, in every effect. - let curve = json!({"name": "Mine", "points": [[0.0, 0.2], [0.4, 0.9], [1.0, 0.1]], "corners": [false, true, false]}); - for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "satin"] { - let mut fx = fresh_effect(kind).unwrap(); - overlay_effect(&mut fx, &json!({"contour": curve})); - let v = values(&fx, "contour"); - assert_eq!(v, curve, "{kind}"); - } - let mut bevel = fresh_effect("bevelEmboss").unwrap(); - overlay_effect(&mut bevel, &json!({"contour": true, "contourName": curve, "glossContour": "Ring"})); - let Effect::BevelEmboss(b) = &bevel else { panic!() }; - assert_eq!(contour_param(&b.contour.as_ref().unwrap().contour), curve); - assert_eq!(b.gloss_contour, builtin_contour("Ring").unwrap()); - // Malformed curves leave the contour alone. - for bad in [json!({"points": [[0.5, 0.5]]}), json!({"points": [[0.0, "x"], [1.0, 1.0]]}), json!({"points": "no"}), json!(7)] { - let mut fx = fresh_effect("dropShadow").unwrap(); - overlay_effect(&mut fx, &json!({"contour": bad})); - assert_eq!(values(&fx, "contour"), json!("Linear")); - } - - // `.shc` round trip of a curve with a corner. - let mine = contour_from_param(&curve).unwrap(); - let back = contours_from_shc(&contour_to_shc(&mine).unwrap()).unwrap(); - let back = contour_from_param(&back[0]).unwrap(); - let Contour::Custom { points, corners, .. } = &back else { panic!() }; - assert_eq!(corners, &vec![false, true, false]); - assert!((points[1].input - 0.4).abs() < 0.003 && (points[1].output - 0.9).abs() < 0.003, "{points:?}"); - } - - fn values(fx: &Effect, key: &str) -> Value { - let c = match fx { - Effect::DropShadow(s) | Effect::InnerShadow(s) => &s.contour, - Effect::OuterGlow(g) | Effect::InnerGlow(g) => &g.contour, - Effect::Satin(s) => &s.contour, - _ => panic!("{key}: no contour"), - }; - contour_param(c) - } - - #[test] - fn make_default_then_reset_restores_it() { - let mut s = session(); - // Factory default first: Reset without a saved default gives the factory set. - let r = s.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); - assert_eq!(r["user"], json!(false)); - assert_eq!(r["params"]["size"], json!(3)); - s.execute( - "layer.layerStyle.makeDefault", - json!({"kind": "stroke", "params": {"size": 9, "position": "inside", "blend": "Normal", "opacity": 100, "color": "#000000"}}), - ) - .unwrap(); - let r = s.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); - assert_eq!(r["user"], json!(true)); - assert_eq!(r["params"]["size"], json!(9)); - // Bad kinds and params are graceful errors. - assert!(s.execute("layer.layerStyle.makeDefault", json!({"kind": "nope", "params": {}})).is_err()); - assert!(s.execute("layer.layerStyle.makeDefault", json!({"kind": "stroke", "params": "x"})).is_err()); - assert!(s.execute("layer.layerStyle.defaultFor", json!({"kind": "nope"})).is_err()); - } - - #[test] - fn layer_style_defaults_persist_with_presets() { - let mut s = session(); - s.execute("layer.layerStyle.makeDefault", json!({"kind": "stroke", "params": {"size": 9}})).unwrap(); - let blob = s.presets.to_json(&s); - let mut t = Session::new(); - t.load_presets_json(blob); - let r = t.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); - assert_eq!(r["user"], json!(true)); - assert_eq!(r["params"]["size"], json!(9)); - } - - #[test] - fn double_stroke_bands_render_and_survive_an_edit() { - // The manga/typesetting case (#157): a 7 px white stroke under a 3 px black one. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 48, "height": 48})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("select.rect", json!({"x": 16, "y": 16, "width": 16, "height": 16})).unwrap(); - s.execute("edit.fill", json!({"color": "#000000"})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); - s.execute("layer.layerStyle.stroke", json!({"size": 3, "color": "#000000", "position": "outside"})).unwrap(); - s.execute("layer.layerStyle.stroke", json!({"size": 7, "color": "#ffffff", "position": "outside", "add": true})).unwrap(); - let px = |s: &mut Session, x: i32, y: i32| -> [f32; 4] { - let mut v: Vec = serde_json::from_value(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()).unwrap(); - v.resize(4, 1.0); - [v[0], v[1], v[2], v[3]] - }; - // Bands from the square edge outward: 0..3 px black (the first stroke renders on top), - // 3..7 px white, then the white background. - assert!(px(&mut s, 24, 24)[0] < 0.1, "the square is black"); - assert!(px(&mut s, 24, 14)[0] < 0.1, "the first stroke covers the wider one"); - assert!(px(&mut s, 24, 11)[0] > 0.9, "the second stroke shows outside it"); - assert!(px(&mut s, 24, 5)[0] > 0.9, "background beyond both"); - // A dialog round trip (replace with the same snapshots) keeps both bands. - let before = px(&mut s, 24, 14); - let st = s.active().unwrap(); - let id = st.active_layer.unwrap(); - let items = st.doc.layer(id).unwrap().effects.items.clone(); - let entries: Vec = items.iter().map(|fx| json!({"kind": "stroke", "fx": serde_json::to_value(fx).unwrap(), "params": {}})).collect(); - s.execute("layer.layerStyle.replace", json!({"layer": id.0, "effects": entries})).unwrap(); - assert_eq!(px(&mut s, 24, 14), before, "both strokes render identically after the round trip"); - } +fn exact_stops(p: &Value) -> Option> { + p.get("stops")? + .as_array()? + .iter() + .map(|s| { + let t = s.get(0)?.as_f64()? as f32; + let color = color_of(&json!({"c":s.get(1)?}), "c")?; + Some((t, color)) + }) + .collect() } diff --git a/crates/engine/src/layer_style/gradient_overlay.rs b/crates/engine/src/layer_style/gradient_overlay.rs new file mode 100644 index 0000000000..ee88cb447d --- /dev/null +++ b/crates/engine/src/layer_style/gradient_overlay.rs @@ -0,0 +1,269 @@ +//! Keep editable stops separate from the dense stops used by the compositors. + +use photocraft_doc::effects::GradientInterpolation; +use photocraft_doc::{Color, Gradient}; +use serde_json::Value; + +use super::{color_of, f_of, overlay_gradient}; + +fn edit_endpoint(stop: Option<&mut (f32, Color)>, params: &Value, key: &str) { + if let (Some(stop), Some(color)) = (stop, color_of(params, key)) { + // The dialog displays hex colours. Opening it must not quantize the saved source. + let [r, g, b, _] = color.to_rgba8(); + let [sr, sg, sb, _] = stop.1.to_rgba8(); + if !params.get(key).is_some_and(Value::is_string) || [r, g, b] != [sr, sg, sb] { + let mut edited = color.in_mode(stop.1.mode); + edited.alpha = stop.1.alpha; + stop.1 = edited; + } + } +} + +/// `params` without a `from`/`to` hex string that only repeats the gradient's current end as the +/// dialog shows it (8-bit): confirming the dialog unchanged must not quantize a baked or +/// imported curve's exact ends (as `edit_endpoint` guards the recorded source). +fn without_unchanged_ends(gradient: &Gradient, params: &Value) -> Value { + let mut params = params.clone(); + for (key, end) in [("from", gradient.stops.first()), ("to", gradient.stops.last())] { + let shown = |c: &Color| { + let [r, g, b, _] = c.to_rgba8(); + [r, g, b] + }; + let same = params.get(key).is_some_and(Value::is_string) && end.zip(color_of(¶ms, key)).is_some_and(|(end, color)| shown(&end.1) == shown(&color)); + if same && let Some(object) = params.as_object_mut() { + object.remove(key); + } + } + params +} + +/// Edits recorded source stops, initializing legacy curves from their end colours. +pub(super) fn overlay(gradient: &mut Gradient, interpolation: &mut Option, params: &Value) { + // Reset to Default restores the factory's unbaked interpolation too. + if params.get("method").and_then(Value::as_str) == Some("custom") { + if let Some(source) = interpolation.take() { + gradient.stops = source.stops; + } + overlay_gradient(gradient, &without_unchanged_ends(gradient, params)); + return; + } + let requested = params.get("method").and_then(Value::as_str).filter(|name| photocraft_io::GradientMethod::from_name(name).is_some()); + if interpolation.is_none() && requested.is_none() { + overlay_gradient(gradient, &without_unchanged_ends(gradient, params)); + return; + } + let previous = interpolation.clone(); + let source = interpolation.get_or_insert_with(|| GradientInterpolation { + method: requested.unwrap_or("classic").to_ascii_lowercase(), + smoothness: 100.0, + // Without a source record, intermediate stops may be baked samples (PSD import or + // older method commands). Keep the legacy rebuild-from-ends behaviour on first choice. + stops: vec![(0.0, gradient.stops.first().map_or(Color::BLACK, |s| s.1)), (1.0, gradient.stops.last().map_or(Color::WHITE, |s| s.1))], + }); + if let Some(method) = requested { + source.method = method.to_ascii_lowercase(); + } + if let Some(smoothness) = f_of(params, "smoothness").filter(|n| n.is_finite()) { + source.smoothness = smoothness.clamp(0.0, 100.0); + } + if let Ok(Some(stops)) = super::stops_param(params, "layer.layerStyle.gradientOverlay") { + source.stops = stops; + } + edit_endpoint(source.stops.first_mut(), params, "from"); + edit_endpoint(source.stops.last_mut(), params, "to"); + if previous.as_ref() != Some(source) + && let Some(method) = photocraft_io::GradientMethod::from_name(&source.method) + { + // Missing midpoint entries mean 50 %, including for multi-stop source gradients. + gradient.stops = photocraft_io::bake_gradient(source.stops.clone(), &[], source.smoothness / 100.0, method); + } + // Geometry edits must not replace just the ends of the baked curve. + let mut geometry = params.clone(); + if let Some(object) = geometry.as_object_mut() { + for key in ["method", "smoothness", "from", "to", "stops"] { + object.remove(key); + } + } + overlay_gradient(gradient, &geometry); +} + +#[cfg(test)] +mod tests { + use super::*; + use photocraft_color::BlendMode; + use photocraft_doc::{Effect, FxCommon}; + use photocraft_io::{GradientMethod, bake_gradient}; + use serde_json::json; + + #[test] + fn choosing_a_method_on_a_baked_overlay_rebuilds_from_its_end_colours() { + let ends = vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]; + for imported_method in [GradientMethod::Perceptual, GradientMethod::Smooth] { + let gradient = Gradient { + stops: bake_gradient(ends.clone(), &[0.5], 1.0, imported_method), + opacity_stops: vec![(0.0, 0.25), (0.6, 0.8), (1.0, 0.5)], + align: false, + offset: (0.2, -0.1), + ..Gradient::default() + }; + assert!(gradient.stops.len() > 2); + let source_ends = vec![(0.0, gradient.stops.first().unwrap().1), (1.0, gradient.stops.last().unwrap().1)]; + let mut fx = + Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: gradient.clone(), dither: true, interpolation: None }; + super::super::overlay_effect(&mut fx, &json!({"method": "linear"})); + let mut expected = gradient; + expected.stops = bake_gradient(source_ends.clone(), &[0.5], 1.0, GradientMethod::Linear); + assert_eq!( + fx, + Effect::GradientOverlay { + common: FxCommon::new(BlendMode::Normal, 1.0), + gradient: expected, + dither: true, + interpolation: Some(GradientInterpolation { method: "linear".into(), smoothness: 100.0, stops: source_ends }), + } + ); + } + } + + #[test] + fn endpoint_edits_after_selecting_a_method_recolour_the_whole_baked_overlay() { + for same_edit in [false, true] { + let mut fx = Effect::GradientOverlay { + common: FxCommon::new(BlendMode::Normal, 1.0), + gradient: Gradient { + stops: bake_gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], &[0.5], 1.0, GradientMethod::Smooth), + ..Gradient::default() + }, + dither: false, + interpolation: None, + }; + if same_edit { + super::super::overlay_effect(&mut fx, &json!({"method": "classic", "from": "#ff0000", "to": "#0000ff"})); + } else { + super::super::overlay_effect(&mut fx, &json!({"method": "classic"})); + super::super::overlay_effect(&mut fx, &json!({"from": "#ff0000", "to": "#0000ff"})); + } + let ends = vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))]; + let Effect::GradientOverlay { gradient, interpolation, .. } = fx else { panic!("expected overlay") }; + assert_eq!(gradient.stops, bake_gradient(ends.clone(), &[0.5], 1.0, GradientMethod::Classic)); + assert_eq!(interpolation.unwrap().stops, ends); + } + } + + #[test] + fn changing_method_keeps_all_source_stops_and_transparency() { + let mut gradient = Gradient { + stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (0.3, Color::rgb(0.0, 1.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], + opacity_stops: vec![(0.0, 0.25), (1.0, 0.8)], + align: false, + offset: (0.2, -0.1), + ..Gradient::default() + }; + let original = gradient.clone(); + let mut source = Some(GradientInterpolation { method: "classic".into(), smoothness: 100.0, stops: original.stops.clone() }); + gradient.stops = bake_gradient(original.stops.clone(), &[0.5, 0.5], 1.0, GradientMethod::Classic); + for method in ["classic", "smooth", "perceptual", "linear", "classic"] { + overlay(&mut gradient, &mut source, &json!({"method": method})); + let source = source.as_ref().unwrap(); + assert_eq!(source.method, method); + assert_eq!(source.stops, original.stops); + let expected = photocraft_io::bake_gradient(original.stops.clone(), &[0.5, 0.5], 1.0, photocraft_io::GradientMethod::from_name(method).unwrap()); + assert_eq!(gradient.stops, expected); + assert_eq!(gradient.opacity_stops, original.opacity_stops); + assert_eq!(gradient.align, original.align); + assert_eq!(gradient.offset, original.offset); + } + overlay(&mut gradient, &mut source, &json!({"from": "#112233", "to": "#445566"})); + let source = source.unwrap(); + assert_eq!(source.stops.len(), 3); + assert_eq!(source.stops[1], original.stops[1], "endpoint edits preserve the middle source stop"); + assert_eq!(gradient.stops, bake_gradient(source.stops, &[0.5, 0.5], 1.0, GradientMethod::Classic)); + } + + #[test] + fn colour_edit_rebakes_the_curve_and_geometry_edits_leave_it_exact() { + let mut gradient = Gradient::default(); + let mut source = None; + overlay(&mut gradient, &mut source, &json!({"method": "smooth", "from": "#ff0000", "to": "#0000ff"})); + let before = gradient.stops.clone(); + overlay(&mut gradient, &mut source, &json!({"from": "#00ff00", "angle": 45})); + assert_ne!(gradient.stops, before); + let expected = photocraft_io::bake_gradient( + vec![(0.0, Color::rgb(0.0, 1.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], + &[0.5], + 1.0, + photocraft_io::GradientMethod::Smooth, + ); + assert_eq!(gradient.stops, expected); + overlay(&mut gradient, &mut source, &json!({"angle": 90, "reverse": true, "scale": 75})); + assert_eq!(gradient.stops, expected); + assert_eq!(gradient.angle, 90.0); + assert!(gradient.reverse); + assert_eq!(gradient.scale, 0.75); + } + + #[test] + fn reopening_does_not_quantize_source_colours_or_rebake() { + let mut gradient = Gradient::default(); + gradient.stops[0].1.alpha = 0.37; + let mut source = None; + overlay(&mut gradient, &mut source, &json!({"from": [0.123456, 0.234567, 0.345678], "method": "perceptual"})); + let before = (gradient.clone(), source.clone()); + assert_eq!(source.as_ref().unwrap().stops[0].1.alpha, 0.37); + let [r, g, b, _] = source.as_ref().unwrap().stops[0].1.to_rgba8(); + overlay(&mut gradient, &mut source, &json!({"from": format!("#{r:02x}{g:02x}{b:02x}"), "to": "#ffffff", "method": "perceptual", "smoothness": 100})); + assert_eq!((gradient, source), before); + } + + #[test] + fn old_gradients_are_unchanged_until_a_method_is_chosen() { + let mut gradient = Gradient::default(); + gradient.stops.insert(1, (0.4, Color::rgb(0.1, 0.9, 0.2))); + let original = gradient.clone(); + let mut source = None; + overlay(&mut gradient, &mut source, &json!({"method": "custom", "opacity": 50})); + assert_eq!(gradient, original); + assert!(source.is_none()); + } + + #[test] + fn endpoint_edits_keep_the_source_colour_model() { + for mode in [photocraft_doc::ColorMode::Grayscale, photocraft_doc::ColorMode::Cmyk, photocraft_doc::ColorMode::Lab] { + let mut gradient = Gradient { stops: vec![(0.0, Color::BLACK.in_mode(mode)), (1.0, Color::WHITE.in_mode(mode))], ..Gradient::default() }; + let mut source = None; + overlay(&mut gradient, &mut source, &json!({"method": "classic"})); + overlay(&mut gradient, &mut source, &json!({"from": "#ff0000"})); + assert!(source.as_ref().unwrap().stops.iter().all(|s| s.1.mode == mode)); + } + } + + #[test] + fn reset_to_factory_defaults_removes_the_recorded_method() { + let mut gradient = Gradient::default(); + let mut source = None; + overlay(&mut gradient, &mut source, &json!({"method": "smooth", "from": "#ff0000"})); + overlay(&mut gradient, &mut source, &super::super::effect_defaults("gradientOverlay")); + assert!(source.is_none()); + assert_eq!(gradient, Gradient::default()); + } +} + +#[cfg(test)] +mod integration_stop_tests { + use super::*; + use serde_json::json; + #[test] + fn arbitrary_source_edits_rebake_and_preserve_opacity() { + let mut gradient = Gradient::default(); + let mut source = None; + overlay( + &mut gradient, + &mut source, + &json!({"method":"smooth","stops":[[0.0,"#ff0000"],[0.3,"#00ff00"],[1.0,"#0000ff"]],"opacityStops":[[0.0,20.0],[1.0,80.0]]}), + ); + let stops = source.as_ref().unwrap().stops.clone(); + assert_eq!(stops.len(), 3); + assert_eq!(gradient.stops, photocraft_io::bake_gradient(stops, &[], 1.0, photocraft_io::GradientMethod::Smooth)); + assert_eq!(gradient.opacity_stops, vec![(0.0, 0.2), (1.0, 0.8)]); + } +} diff --git a/crates/engine/src/layer_style/tests.rs b/crates/engine/src/layer_style/tests.rs new file mode 100644 index 0000000000..89591e6883 --- /dev/null +++ b/crates/engine/src/layer_style/tests.rs @@ -0,0 +1,641 @@ +use super::*; +use serde_json::json; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 32, "height": 32})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s +} + +fn effects(s: &Session) -> Vec { + let d = s.active().unwrap(); + d.doc.layer(d.active_layer.unwrap()).unwrap().effects.items.clone() +} + +#[test] +fn bevel_command_sets_technique_contour_and_texture() { + let mut s = session(); + s.execute("layer.layerStyle.bevelEmboss", json!({"style": "pillow", "technique": "chiselHard", "contour": true, "contourRange": 70, "texture": "Bubbles", "textureDepth": -200, "textureInvert": true})).unwrap(); + let fx = effects(&s); + let Effect::BevelEmboss(b) = &fx[0] else { panic!("{fx:?}") }; + assert_eq!((b.style, b.technique), (photocraft_doc::BevelStyle::PillowEmboss, BevelTechnique::ChiselHard)); + assert!((b.contour.as_ref().unwrap().range - 0.7).abs() < 1e-6); + let t = b.texture.as_ref().unwrap(); + assert_eq!((t.name.as_str(), t.depth, t.invert), ("Bubbles", -2.0, true)); +} + +/// #6698 review: a glow's Range runs 1..100 %, as its slider and Photoshop's do. A 0 is +/// held at the 1 % floor, and 100 is kept. +#[test] +fn glow_range_is_held_to_one_through_one_hundred() { + for kind in ["outerGlow", "innerGlow"] { + for (sent, kept) in [(0.0, 0.01), (-5.0, 0.01), (1.0, 0.01), (50.0, 0.5), (100.0, 1.0), (250.0, 1.0)] { + let mut s = session(); + s.execute(&format!("layer.layerStyle.{kind}"), json!({"range": sent})).unwrap(); + let range = match &effects(&s)[0] { + Effect::OuterGlow(g) | Effect::InnerGlow(g) => g.range, + other => panic!("{other:?}"), + }; + assert!((range - kept).abs() < 1e-6, "{kind} range {sent} kept as {range}, not {kept}"); + } + } + let doc = |id: &str| specs().into_iter().find(|c| c.id == id).map(|c| c.params).unwrap_or_default(); + assert!(doc("layer.layerStyle.outerGlow").contains(r#""range":1..100=50"#)); + assert!(doc("layer.layerStyle.innerGlow").contains(r#""range":1..100=50"#)); +} + +#[test] +fn bevel_gloss_anti_alias_is_a_param() { + let mut s = session(); + s.execute("layer.layerStyle.bevelEmboss", json!({"glossContour": "Ring - Double", "glossAntiAlias": true})).unwrap(); + let Effect::BevelEmboss(b) = &effects(&s)[0] else { panic!() }; + assert!(b.gloss_anti_alias); + s.execute("layer.layerStyle.bevelEmboss", json!({"glossAntiAlias": false})).unwrap(); + let Effect::BevelEmboss(b) = &effects(&s)[0] else { panic!() }; + assert!(!b.gloss_anti_alias); +} + +#[test] +fn stroke_overprint_is_a_param() { + let mut s = session(); + s.execute("layer.layerStyle.stroke", json!({"size": 4, "overprint": true})).unwrap(); + let Effect::Stroke(st) = &effects(&s)[0] else { panic!() }; + assert!(st.overprint); + s.execute("layer.layerStyle.stroke", json!({"overprint": false})).unwrap(); + let Effect::Stroke(st) = &effects(&s)[0] else { panic!() }; + assert!(!st.overprint); +} + +#[test] +fn bevel_shading_fields_are_independent_and_preserved_on_partial_edits() { + let mut s = session(); + s.execute( + "layer.layerStyle.bevelEmboss", + json!({ + "highlightBlend": "Normal", "highlightColor": "#ff0000", "highlightOpacity": 35, + "shadowBlend": "Color Burn", "shadowColor": "#0000ff", "shadowOpacity": 60 + }), + ) + .unwrap(); + let fx = effects(&s); + let Effect::BevelEmboss(bevel) = &fx[0] else { panic!("{fx:?}") }; + assert_eq!(bevel.highlight.blend, BlendMode::Normal); + assert_eq!(bevel.highlight_color, Color::rgb(1.0, 0.0, 0.0)); + assert!((bevel.highlight.opacity - 0.35).abs() < 0.001); + assert_eq!(bevel.shadow.blend, BlendMode::ColorBurn); + assert_eq!(bevel.shadow_color, Color::rgb(0.0, 0.0, 1.0)); + assert!((bevel.shadow.opacity - 0.60).abs() < 0.001); + + let mut changed = fx[0].clone(); + overlay_effect(&mut changed, &json!({"size": 12})); + let Effect::BevelEmboss(partial) = &changed else { panic!("expected bevel") }; + assert_eq!(partial.highlight, bevel.highlight); + assert_eq!(partial.highlight_color, bevel.highlight_color); + assert_eq!(partial.shadow, bevel.shadow); + assert_eq!(partial.shadow_color, bevel.shadow_color); + + overlay_effect(&mut changed, &json!({"highlightOpacity": 120, "shadowOpacity": -30})); + let Effect::BevelEmboss(limits) = &changed else { panic!("expected bevel") }; + assert_eq!((limits.highlight.opacity, limits.shadow.opacity), (1.0, 0.0)); +} + +#[test] +fn every_style_command_adds_its_effect() { + for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "bevelEmboss", "satin"] { + let mut s = session(); + s.execute(&format!("layer.layerStyle.{kind}"), json!({})).unwrap(); + let fx = effects(&s); + assert_eq!(fx.len(), 1, "{kind}"); + assert!(fx[0].enabled()); + } +} + +/// #1543: a Satin or Bevel & Emboss size or softness far past Photoshop's range rendered a +/// blur kernel of 2^62 taps ("capacity overflow"), or ran for minutes at 1e6. The blur width +/// is capped at the effect reach, so the document still renders, quickly. +#[test] +fn oversized_satin_and_bevel_sizes_stay_saveable_and_render() { + for (cmd, p, accepted) in [ + ("satin", json!({"size": 1e308}), false), + ("satin", json!({"size": 1e30}), true), + ("satin", json!({"size": 1_000_000}), true), + ("bevelEmboss", json!({"size": 1e308}), false), + ("bevelEmboss", json!({"soften": 1e308}), false), + ("bevelEmboss", json!({"size": 1e30, "soften": 1e30}), true), + ("bevelEmboss", json!({"size": 1_000_000, "soften": 1_000_000}), true), + ] { + let mut s = session(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + let result = s.execute(&format!("layer.layerStyle.{cmd}"), p.clone()); + if accepted { + result.unwrap(); + } else { + assert!(matches!(result, Err(EngineError::BadParams { .. }))); + } + photocraft_format::save_to_bytes(&s.active().unwrap().doc, &Default::default()).unwrap(); + let start = std::time::Instant::now(); + s.execute("document.pixel", json!({"x": 0, "y": 0})).unwrap_or_else(|e| panic!("{cmd} {p}: {e}")); + assert!(start.elapsed().as_secs() < 20, "{cmd} {p}: {:?}", start.elapsed()); + } +} + +#[test] +fn params_are_applied_and_replace_or_add() { + let mut s = session(); + s.execute("layer.layerStyle.stroke", json!({"size": 7, "position": "inside", "color": "#00ff00", "opacity": 50})).unwrap(); + match &effects(&s)[0] { + Effect::Stroke(st) => { + assert_eq!(st.size, 7.0); + assert_eq!(st.position, StrokePosition::Inside); + assert_eq!(st.common.opacity, 0.5); + assert_eq!(st.paint, FxPaint::Color(Color::rgb(0.0, 1.0, 0.0))); + } + other => panic!("{other:?}"), + } + s.execute("layer.layerStyle.stroke", json!({"size": 2})).unwrap(); + assert_eq!(effects(&s).len(), 1, "replaces"); + s.execute("layer.layerStyle.stroke", json!({"size": 4, "add": true})).unwrap(); + assert_eq!(effects(&s).len(), 2, "adds a second instance"); + s.execute("layer.layerStyle.dropShadow", json!({"blend": "normal", "distance": 12})).unwrap(); + assert!(matches!(&effects(&s)[2], Effect::DropShadow(sh) if sh.common.blend == BlendMode::Normal && sh.distance == 12.0)); +} + +/// The gradient overlay and the gradient stroke keep their scale in Photoshop's 10-150 %. +#[test] +fn gradient_overlay_and_stroke_scale_stay_in_range() { + let scale = |s: &Session| match &effects(s)[0] { + Effect::GradientOverlay { gradient, .. } => gradient.scale, + Effect::Stroke(st) => match &st.paint { + FxPaint::Gradient(g) => g.scale, + other => panic!("{other:?}"), + }, + other => panic!("{other:?}"), + }; + for (cmd, p, want) in [ + ("gradientOverlay", json!({"scale": 1000}), 1.5), + ("gradientOverlay", json!({"scale": 1}), 0.1), + ("gradientOverlay", json!({"scale": 80}), 0.8), + ("stroke", json!({"from": "#000000", "to": "#ffffff", "scale": -50}), 0.1), + ("stroke", json!({"from": "#000000", "to": "#ffffff", "scale": 0}), 0.1), + ("stroke", json!({"from": "#000000", "to": "#ffffff", "scale": 400}), 1.5), + ] { + let mut s = session(); + s.execute(&format!("layer.layerStyle.{cmd}"), p.clone()).unwrap(); + assert!((scale(&s) - want).abs() < 1e-6, "{cmd} {p}: {}", scale(&s)); + } + // Beyond f32 range is refused before any edit (commands.rs), not clamped. + let err = session().execute("layer.layerStyle.gradientOverlay", json!({"scale": 1e39})).unwrap_err(); + assert!(format!("{err:?}").contains("finite 32-bit"), "{err:?}"); +} + +#[test] +fn clear_and_undo() { + let mut s = session(); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); + s.execute("layer.layerStyle.clear", json!({})).unwrap(); + assert!(effects(&s).is_empty()); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(effects(&s).len(), 1); +} + +fn pixel(s: &mut Session) -> Vec { + serde_json::from_value(s.execute("document.pixel", json!({"x": 3, "y": 3})).unwrap()).unwrap() +} + +fn list_enabled(s: &Session) -> bool { + let d = s.active().unwrap(); + d.doc.layer(d.active_layer.unwrap()).unwrap().effects.enabled +} + +/// #3648: the Layers panel's "Effects" eye (`layer.setEffectsVisible`) must survive a Layer +/// Style dialog OK: the dialog's whole-list replace re-creates the items but leaves the +/// list's master switch alone. +#[test] +fn replacing_the_list_keeps_a_disabled_effect_list_disabled() { + let mut s = session(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap(); + let shown = pixel(&mut s); + s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap(); + let hidden = pixel(&mut s); + assert_ne!(hidden, shown, "the eye really hides the style"); + let r = s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "colorOverlay", "params": {"color": "#00ff00"}}]})).unwrap(); + assert_eq!(r["effects"], 1, "the list itself is updated"); + assert!(!list_enabled(&s), "the whole-list switch survives the edit"); + assert!(effects(&s)[0].enabled(), "the effect's own state is updated"); + assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing"); + s.execute("layer.setEffectsVisible", json!({"visible": true})).unwrap(); + assert_ne!(pixel(&mut s), hidden, "re-showing the list draws the edited effect"); +} + +/// #3648: adding or replacing one effect (`layer.layerStyle.`) also leaves the master +/// switch as the user left it. +#[test] +fn set_effect_keeps_a_disabled_effect_list_disabled() { + let mut s = session(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#0000ff"})).unwrap(); + s.execute("layer.setEffectsVisible", json!({"visible": false})).unwrap(); + let hidden = pixel(&mut s); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap(); + assert!(!list_enabled(&s), "the whole-list switch survives the edit"); + assert!(effects(&s)[0].enabled(), "the effect's own state is updated"); + assert_eq!(pixel(&mut s), hidden, "a disabled list still draws nothing"); +} + +/// A layer that never had effects keeps the default: its first effect shows. +#[test] +fn a_first_effect_shows_on_a_layer_that_never_had_effects() { + let mut s = session(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + let plain = pixel(&mut s); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})).unwrap(); + assert!(list_enabled(&s), "a fresh effects list defaults to enabled"); + assert_ne!(pixel(&mut s), plain, "the first effect renders"); +} + +/// #3102: an effect row dropped on the Delete button deletes only that effect, in one step. +#[test] +fn clear_one_effect_by_index() { + let mut s = session(); + s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); + let layer = s.active().unwrap().active_layer.unwrap().0; + s.execute("layer.layerStyle.clear", json!({"layer": layer, "effect": 0})).unwrap(); + assert!(matches!(effects(&s)[..], [Effect::ColorOverlay { .. }])); + for bad in [json!({"effect": 5}), json!({"effect": -1}), json!({"effect": "x"}), json!({"effect": 0, "layer": u64::MAX})] { + assert!(s.execute("layer.layerStyle.clear", bad).is_err()); + } + assert_eq!(effects(&s).len(), 1, "bad params change nothing"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(effects(&s).len(), 2); +} + +#[test] +fn color_overlay_renders() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.execute("layer.layerStyle.colorOverlay", json!({"color": "#ff0000"})).unwrap(); + let px: Vec = serde_json::from_value(s.execute("document.pixel", json!({"x": 3, "y": 3})).unwrap()).unwrap(); + assert!(px[0] > 0.99 && px[1] < 0.01, "{px:?}"); +} + +#[test] +fn gradient_styles_parse() { + assert_eq!(gradient_style("Radial"), GradientStyle::Radial); + assert_eq!(gradient_style("diamond"), GradientStyle::Diamond); + assert_eq!(gradient_style("?"), GradientStyle::Linear); +} + +fn snapshot(fx: &Effect) -> Value { + serde_json::to_value(fx).unwrap() +} + +#[test] +fn every_kind_has_factory_defaults() { + for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "patternOverlay", "satin", "bevelEmboss"] { + assert!(effect_defaults(kind).as_object().is_some_and(|o| !o.is_empty()), "{kind}"); + assert!(effect_from_params(kind, &json!({})).is_some(), "{kind}"); + } + assert!(effect_from_params("nope", &json!({})).is_none()); +} + +#[test] +fn replace_builds_edits_and_preserves_instances() { + let mut s = session(); + // Two strokes: the first edits a snapshot (params overlay), the second is fresh. + let first = Effect::Stroke(StrokeFx { + common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), + size: 7.0, + position: StrokePosition::Outside, + paint: FxPaint::Gradient(Gradient { stops: vec![(0.0, Color::rgb(0.0, 0.0, 1.0)), (1.0, Color::WHITE)], ..Gradient::default() }), + overprint: false, + }); + let r = s + .execute( + "layer.layerStyle.replace", + json!({"effects": [ + {"kind": "stroke", "fx": snapshot(&first), "params": {"opacity": 50}}, + {"kind": "stroke", "params": {"size": 2, "color": "#ffffff"}}, + ]}), + ) + .unwrap(); + assert_eq!(r["effects"], 2); + let fx = effects(&s); + assert_eq!(fx.len(), 2, "order preserved"); + match (&fx[0], &fx[1]) { + (Effect::Stroke(a), Effect::Stroke(b)) => { + assert_eq!(a.common.opacity, 0.5); + // The overlay must not clobber the gradient paint it doesn't model. + assert!(matches!(&a.paint, FxPaint::Gradient(g) if g.stops.len() == 2 && g.stops[0].1 == Color::rgb(0.0, 0.0, 1.0))); + assert_eq!(b.size, 2.0); + assert_eq!(b.paint, FxPaint::Color(Color::rgb(1.0, 1.0, 1.0))); + } + other => panic!("{other:?}"), + } + // Replacing with an empty list clears the layer. + s.execute("layer.layerStyle.replace", json!({"effects": []})).unwrap(); + assert!(effects(&s).is_empty()); +} + +#[test] +fn replace_rejects_bad_entries_gracefully() { + let mut s = session(); + let stroke = Effect::Stroke(StrokeFx { + common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), + size: 3.0, + position: StrokePosition::Outside, + paint: FxPaint::Color(Color::BLACK), + overprint: false, + }); + for p in [ + json!({}), + json!({"effects": "nope"}), + json!({"effects": [{"params": {}}]}), + json!({"effects": [{"kind": "nope"}]}), + json!({"effects": [{"kind": "stroke", "fx": {"Bogus": {}}}]}), + json!({"effects": [{"kind": "stroke", "fx": 3}]}), + json!({"effects": [{"kind": "dropShadow", "fx": snapshot(&stroke)}]}), + json!({"effects": [{"kind": "stroke", "fx": snapshot(&stroke), "params": "nope"}]}), + ] { + assert!(s.execute("layer.layerStyle.replace", p).is_err()); + } + // A long list is refused, not run. + let many: Vec<_> = (0..257).map(|_| json!({"kind": "stroke"})).collect(); + assert!(s.execute("layer.layerStyle.replace", json!({"effects": many})).is_err()); + assert!(effects(&s).is_empty(), "nothing was applied"); +} + +#[test] +fn contour_and_noise_params_edit_existing_effects() { + let mut s = session(); + s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); + let before = effects(&s); + let Effect::DropShadow(_) = &before[0] else { panic!("{before:?}") }; + let fx0 = snapshot(&before[0]); + // Unknown contour names are carried, not clobbered to Linear. + s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "dropShadow", "fx": fx0, "params": {"contour": "Cone", "noise": 30}}]})).unwrap(); + let fx = effects(&s); + let Effect::DropShadow(sh) = &fx[0] else { panic!("{fx:?}") }; + assert_eq!(sh.contour, builtin_contour("Cone").unwrap(), "known preset applies"); + assert!((sh.noise - 0.3).abs() < 1e-6, "noise in percent"); + // An imported contour with an unknown name survives the same round trip. + let imported = Contour::Custom { + name: "Photoshop contour".into(), + points: vec![photocraft_doc::adjust::CurvePoint { input: 0.0, output: 1.0 }, photocraft_doc::adjust::CurvePoint { input: 1.0, output: 0.0 }], + corners: vec![], + }; + let mut carried = before[0].clone(); + if let Effect::DropShadow(c) = &mut carried { + c.contour = imported.clone(); + } + overlay_effect(&mut carried, &json!({"contour": "Photoshop contour"})); + let Effect::DropShadow(after) = &carried else { panic!() }; + assert_eq!(after.contour, imported); + // Linear applies by name. + let mut back = carried; + overlay_effect(&mut back, &json!({"contour": "Linear"})); + let Effect::DropShadow(lin) = &back else { panic!() }; + assert_eq!(lin.contour, Contour::Linear); +} + +#[test] +fn contour_presets_match_photoshop() { + // Photoshop 27.11, measured through Satin: Cone and Cone − Inverted Satins are identical, + // and Gaussian reads 0.36 at 0.5 through Satin but 0.64 as the preset curve (glows, gloss). + let lut = |name: &str| { + let Some(Contour::Custom { points, corners, .. }) = builtin_contour(name) else { panic!("{name}") }; + photocraft_compose::adjust::contour_curve_lut(&points, &corners) + }; + let satin = |name: &str| photocraft_compose::effects::satin_lut(&builtin_contour(name).unwrap()).unwrap(); + let at = |l: &[f32], x: f32| l[(x * (l.len() - 1) as f32).round() as usize]; + let (cone, inverted) = (satin("Cone"), satin("Cone - Inverted")); + for x in [0.1, 0.25, 0.5, 0.8] { + assert!((at(&cone, x) - at(&inverted, x)).abs() < 0.03, "{x}: {} vs {}", at(&cone, x), at(&inverted, x)); + } + assert!((at(&satin("Gaussian"), 0.5) - 0.36).abs() < 0.04, "{}", at(&satin("Gaussian"), 0.5)); + assert!((at(&lut("Gaussian"), 0.5) - 0.64).abs() < 0.03, "{}", at(&lut("Gaussian"), 0.5)); + assert!((at(&lut("Cove - Deep"), 0.0) - 0.26).abs() < 0.02, "Cove - Deep starts above zero"); +} + +/// #5177: a Gradient Overlay (and a gradient stroke) takes any number of colour stops and +/// transparency stops, replacing From/To; bad lists are refused, not silently dropped. +#[test] +fn gradient_overlay_takes_explicit_stops() { + let mut s = session(); + let stops = json!([[0.0, "#ff0000"], [0.5, "#00ff00"], [1.0, "#0000ff"]]); + s.execute("layer.layerStyle.gradientOverlay", json!({"stops": stops, "opacityStops": [[0, 100], [1, 50]]})).unwrap(); + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + assert_eq!(gradient.stops.len(), 3); + assert_eq!(gradient.stops[1], (0.5, photocraft_color::Color::rgb(0.0, 1.0, 0.0))); + assert_eq!(gradient.opacity_stops, vec![(0.0, 1.0), (1.0, 0.5)]); + // Editing the same overlay: new stops replace the list (sorted), unsorted input is sorted, + // and a geometry-only edit keeps the three stops. + s.execute("layer.layerStyle.gradientOverlay", json!({"stops": [[1.0, "#ffffff"], [0.25, [0.0, 0.0, 0.0]]], "opacityStops": []})).unwrap(); + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + assert_eq!(gradient.stops.iter().map(|s| s.0).collect::>(), vec![0.25, 1.0]); + assert!(gradient.opacity_stops.is_empty(), "[] means opaque"); + s.execute("layer.layerStyle.gradientOverlay", json!({"stops": stops})).unwrap(); + // The dialog's path: the snapshot plus a geometry edit keeps the three stops. + let fx = serde_json::to_value(&effects(&s)[0]).unwrap(); + s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "gradientOverlay", "fx": fx, "params": {"angle": 45}}]})).unwrap(); + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + assert_eq!((gradient.stops.len(), gradient.angle), (3, 45.0)); + let e = s.execute("layer.layerStyle.replace", json!({"effects": [{"kind": "gradientOverlay", "params": {"stops": [[0, "#ff0000"]]}}]})).unwrap_err(); + assert!(matches!(e, EngineError::BadParams { .. }), "{e}"); + // A gradient stroke takes stops too. + s.execute("layer.layerStyle.stroke", json!({"size": 4, "stops": [[0, "#ff0000"], [0.5, "#00ff00"], [1, "#0000ff"]]})).unwrap(); + assert!(effects(&s).iter().any(|e| matches!(e, Effect::Stroke(st) if matches!(&st.paint, FxPaint::Gradient(g) if g.stops.len() == 3)))); + // Malformed lists are errors and change nothing. + for bad in [ + json!({"stops": "red"}), + json!({"stops": [[0, "#ff0000"]]}), + json!({"stops": [[0, "#ff0000"], ["x", "#0000ff"]]}), + json!({"stops": [[0, "#ff0000"], [1, 7]]}), + json!({"stops": [[0, "#ff0000"], [f64::NAN, "#0000ff"]]}), + json!({"stops": vec![json!([0, "#ff0000"]); 257]}), + json!({"opacityStops": [[0, 100]]}), + json!({"opacityStops": [[0, "x"], [1, 1]]}), + ] { + let e = s.execute("layer.layerStyle.gradientOverlay", bad.clone()).unwrap_err(); + assert!(matches!(e, EngineError::BadParams { .. }), "{bad}: {e}"); + } + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + assert_eq!(gradient.stops.len(), 3, "a refused edit keeps the gradient"); + // Positions outside 0..1 are clamped. + s.execute("layer.layerStyle.gradientOverlay", json!({"stops": [[-1, "#000000"], [2, "#ffffff"]]})).unwrap(); + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + assert_eq!(gradient.stops.iter().map(|s| s.0).collect::>(), vec![0.0, 1.0]); +} + +#[test] +fn gradient_overlay_method_matches_photoshop() { + // Photoshop 27.11, Classic Gradient Overlay #000000 → #ffffff (Smoothness 100 %): 0.037 at + // t = 1/16 and 0.5 in the middle; straight stops would give 0.0625. + let mut s = session(); + s.execute("layer.layerStyle.gradientOverlay", json!({"from": "#000000", "to": "#ffffff", "method": "classic"})).unwrap(); + let Effect::GradientOverlay { gradient, .. } = &effects(&s)[0] else { panic!() }; + let at = |t: f32| { + let w = gradient.stops.windows(2).find(|w| t >= w[0].0 && t <= w[1].0).unwrap(); + let u = (t - w[0].0) / (w[1].0 - w[0].0).max(1e-6); + w[0].1.to_rgb()[0] + (w[1].1.to_rgb()[0] - w[0].1.to_rgb()[0]) * u + }; + assert!((at(1.0 / 16.0) - 0.037).abs() < 0.004, "{}", at(1.0 / 16.0)); + assert!((at(0.5) - 0.5).abs() < 0.004, "{}", at(0.5)); + // Without a method the two stops stay as they are. + s.execute("layer.layerStyle.gradientOverlay", json!({"from": "#000000", "to": "#ffffff", "add": true})).unwrap(); + let plain = effects(&s).iter().filter(|e| matches!(e, Effect::GradientOverlay { gradient, .. } if gradient.stops.len() == 2)).count(); + assert_eq!(plain, 1, "the added overlay keeps its two stops"); +} + +#[test] +fn contour_presets_and_custom_curves_apply_in_one_undo_step() { + let mut s = session(); + // Picking Cone: the preset's own curve, its points corners, in one undo step. + s.execute("layer.layerStyle.dropShadow", json!({"contour": "Cone", "antiAlias": true})).unwrap(); + let Effect::DropShadow(sh) = &effects(&s)[0] else { panic!() }; + assert_eq!(sh.contour, builtin_contour("Cone").unwrap()); + assert!(sh.contour.is_corner(1) && sh.anti_alias); + let Contour::Custom { points, corners, .. } = &sh.contour else { panic!() }; + let lut = photocraft_compose::adjust::contour_curve_lut(points, corners); + let at = |x: f32| lut[(x * (lut.len() - 1) as f32).round() as usize]; + // Photoshop's measured Cone: 0 at both ends, a 0.99 peak at 0.5, symmetric. + assert!(at(0.0).abs() < 1e-3 && at(1.0).abs() < 1e-3 && at(0.5) > 0.98, "a Λ: {} {} {}", at(0.0), at(0.5), at(1.0)); + assert!((at(0.25) - at(0.75)).abs() < 0.03, "symmetric: {} vs {}", at(0.25), at(0.75)); + assert_eq!(contour_param(&sh.contour), json!("Cone")); + s.execute("edit.undo", json!({})).unwrap(); + assert!(effects(&s).is_empty(), "one undo removes it"); + + // An edited curve (the Contour Editor's param) round trips exactly, in every effect. + let curve = json!({"name": "Mine", "points": [[0.0, 0.2], [0.4, 0.9], [1.0, 0.1]], "corners": [false, true, false]}); + for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "satin"] { + let mut fx = fresh_effect(kind).unwrap(); + overlay_effect(&mut fx, &json!({"contour": curve})); + let v = values(&fx, "contour"); + assert_eq!(v, curve, "{kind}"); + } + let mut bevel = fresh_effect("bevelEmboss").unwrap(); + overlay_effect(&mut bevel, &json!({"contour": true, "contourName": curve, "glossContour": "Ring"})); + let Effect::BevelEmboss(b) = &bevel else { panic!() }; + assert_eq!(contour_param(&b.contour.as_ref().unwrap().contour), curve); + assert_eq!(b.gloss_contour, builtin_contour("Ring").unwrap()); + // Malformed curves leave the contour alone. + for bad in [json!({"points": [[0.5, 0.5]]}), json!({"points": [[0.0, "x"], [1.0, 1.0]]}), json!({"points": "no"}), json!(7)] { + let mut fx = fresh_effect("dropShadow").unwrap(); + overlay_effect(&mut fx, &json!({"contour": bad})); + assert_eq!(values(&fx, "contour"), json!("Linear")); + } + + // `.shc` round trip of a curve with a corner. + let mine = contour_from_param(&curve).unwrap(); + let back = contours_from_shc(&contour_to_shc(&mine).unwrap()).unwrap(); + let back = contour_from_param(&back[0]).unwrap(); + let Contour::Custom { points, corners, .. } = &back else { panic!() }; + assert_eq!(corners, &vec![false, true, false]); + assert!((points[1].input - 0.4).abs() < 0.003 && (points[1].output - 0.9).abs() < 0.003, "{points:?}"); +} + +fn values(fx: &Effect, key: &str) -> Value { + let c = match fx { + Effect::DropShadow(s) | Effect::InnerShadow(s) => &s.contour, + Effect::OuterGlow(g) | Effect::InnerGlow(g) => &g.contour, + Effect::Satin(s) => &s.contour, + _ => panic!("{key}: no contour"), + }; + contour_param(c) +} + +#[test] +fn make_default_then_reset_restores_it() { + let mut s = session(); + // Factory default first: Reset without a saved default gives the factory set. + let r = s.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); + assert_eq!(r["user"], json!(false)); + assert_eq!(r["params"]["size"], json!(1)); + s.execute( + "layer.layerStyle.makeDefault", + json!({"kind": "stroke", "params": {"size": 9, "position": "inside", "blend": "Normal", "opacity": 100, "color": "#000000"}}), + ) + .unwrap(); + let r = s.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); + assert_eq!(r["user"], json!(true)); + assert_eq!(r["params"]["size"], json!(9)); + // Bad kinds and params are graceful errors. + assert!(s.execute("layer.layerStyle.makeDefault", json!({"kind": "nope", "params": {}})).is_err()); + assert!(s.execute("layer.layerStyle.makeDefault", json!({"kind": "stroke", "params": "x"})).is_err()); + assert!(s.execute("layer.layerStyle.defaultFor", json!({"kind": "nope"})).is_err()); +} + +/// #7765: a stroke built without params was red, while the dialog's factory default was +/// black. Both now give Photoshop's Reset to Default: 1 px, inside, Normal, 100 %, black. +#[test] +fn fresh_effects_match_the_dialog_factory_defaults() { + for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "patternOverlay", "satin", "bevelEmboss"] { + let bare = effect_from_params(kind, &json!({})).unwrap(); + let dialog = effect_from_params(kind, &effect_defaults(kind)).unwrap(); + assert_eq!(bare, dialog, "{kind}"); + } + let Some(Effect::Stroke(st)) = effect_from_params("stroke", &json!({})) else { panic!("no stroke") }; + assert_eq!((st.size, st.position, st.common.blend, st.common.opacity), (1.0, StrokePosition::Inside, BlendMode::Normal, 1.0)); + assert_eq!(st.paint, FxPaint::Color(Color::BLACK)); +} + +#[test] +fn layer_style_defaults_persist_with_presets() { + let mut s = session(); + s.execute("layer.layerStyle.makeDefault", json!({"kind": "stroke", "params": {"size": 9}})).unwrap(); + let blob = s.presets.to_json(&s); + let mut t = Session::new(); + t.load_presets_json(blob); + let r = t.execute("layer.layerStyle.defaultFor", json!({"kind": "stroke"})).unwrap(); + assert_eq!(r["user"], json!(true)); + assert_eq!(r["params"]["size"], json!(9)); +} + +#[test] +fn double_stroke_bands_render_and_survive_an_edit() { + // The manga/typesetting case (#157): a 7 px white stroke under a 3 px black one. + let mut s = Session::new(); + s.execute("file.new", json!({"width": 48, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 16, "y": 16, "width": 16, "height": 16})).unwrap(); + s.execute("edit.fill", json!({"color": "#000000"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("layer.layerStyle.stroke", json!({"size": 3, "color": "#000000", "position": "outside"})).unwrap(); + s.execute("layer.layerStyle.stroke", json!({"size": 7, "color": "#ffffff", "position": "outside", "add": true})).unwrap(); + let px = |s: &mut Session, x: i32, y: i32| -> [f32; 4] { + let mut v: Vec = serde_json::from_value(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()).unwrap(); + v.resize(4, 1.0); + [v[0], v[1], v[2], v[3]] + }; + // Bands from the square edge outward: 0..3 px black (the first stroke renders on top), + // 3..7 px white, then the white background. + assert!(px(&mut s, 24, 24)[0] < 0.1, "the square is black"); + assert!(px(&mut s, 24, 14)[0] < 0.1, "the first stroke covers the wider one"); + assert!(px(&mut s, 24, 11)[0] > 0.9, "the second stroke shows outside it"); + assert!(px(&mut s, 24, 5)[0] > 0.9, "background beyond both"); + // A dialog round trip (replace with the same snapshots) keeps both bands. + let before = px(&mut s, 24, 14); + let st = s.active().unwrap(); + let id = st.active_layer.unwrap(); + let items = st.doc.layer(id).unwrap().effects.items.clone(); + let entries: Vec = items.iter().map(|fx| json!({"kind": "stroke", "fx": serde_json::to_value(fx).unwrap(), "params": {}})).collect(); + s.execute("layer.layerStyle.replace", json!({"layer": id.0, "effects": entries})).unwrap(); + assert_eq!(px(&mut s, 24, 14), before, "both strokes render identically after the round trip"); +} + +#[test] +fn unchanged_dense_imported_stops_roundtrip_without_edit_limit_regression() { + let mut s = session(); + let gradient = Gradient { stops: (0..300).map(|i| (i as f32 / 299.0, Color::rgb(i as f32 / 777.0, 0.123456, 0.9))).collect(), ..Gradient::default() }; + let fx = Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: gradient.clone(), dither: false, interpolation: None }; + let stops: Vec<_> = gradient.stops.iter().map(|(t, c)| json!([t, c])).collect(); + s.execute("layer.layerStyle.replace", json!({"effects":[{"kind":"gradientOverlay","fx":fx,"params":{"stops":stops}}]})).unwrap(); + let doc = s.active().unwrap(); + let saved = &doc.doc.layer(doc.active_layer.unwrap()).unwrap().effects.items[0]; + assert_eq!(saved, &fx); + let mut edited = stops; + edited[1] = json!([0.01, "#ffffff"]); + assert!(s.execute("layer.layerStyle.replace", json!({"effects":[{"kind":"gradientOverlay","fx":fx,"params":{"stops":edited}}]})).is_err()); +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index b8d6167c3d..fdcbe24120 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1,4 +1,4 @@ -//! The Photocraft engine façade: open documents, history, and the command registry. +//! The PhotoCraft engine façade: open documents, history, and the command registry. //! //! Every user-visible action is a command with a stable id (Photoshop-style, such as //! `layer.newAdjustmentLayer.invert`) and JSON parameters. Every frontend goes through the same @@ -7,130 +7,40 @@ #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] -pub mod actions_cmds; -pub mod actions_edit_cmds; -pub mod adjust_cmds; pub mod adjust_params; -pub mod align_cmds; mod allocation; -pub mod analysis_cmds; -pub mod artboard_cmds; -pub mod atn_cmds; -pub mod automate_cmds; -pub mod background_cmds; -pub mod brush_cmds; -pub mod brush_key_cmds; -pub mod brush_preset_cmds; pub mod build_info; mod canvas_geom; mod channel_clip; -pub mod channel_cmds; mod clip_group; -pub mod color_cmds; -pub mod color_to_alpha_cmds; -pub mod commands; -pub mod comps_cmds; mod crop_fill; -pub mod cutout_cmds; pub mod display_color; -pub mod distort_cmds; -pub mod document_preset_cmds; -pub mod edit_cmds; -pub mod edit_menu_cmds; -pub mod eraser_cmds; -pub mod export_preset_cmds; -pub mod exr_cmds; -pub mod extra_cmds; -pub mod file_cmds; -pub mod fill_cmds; -pub mod fill_key_cmds; +pub mod doc_path; pub mod filters; pub mod filters_ext; -pub mod float_cmds; -mod frame_cmds; -pub mod fx_view_cmds; -pub mod fx_visibility_cmds; -pub mod gallery_cmds; -pub mod gradient_fill_cmds; -pub mod group_view_cmds; +mod group_options; pub mod guide_layout; pub mod hidden_target; -pub mod history_cmds; -pub mod history_select_cmds; -pub mod image_cmds; pub mod inspect; pub mod installed_profiles; pub mod jobs; pub mod kys; -pub mod layer_copy_cmds; -pub mod layer_label_cmds; -pub mod layer_list_cmds; -pub mod layer_mask_props_cmds; -pub mod layer_menu_cmds; -pub mod layer_multi_cmds; -pub mod layer_nav_cmds; +pub mod layer_place; pub mod layer_style; -pub mod lens_cmds; pub mod link_watch; -pub mod magnetic_cmds; -pub mod mask_view_cmds; -mod migrate_cmds; -pub mod mode_cmds; -pub mod multichannel_cmds; pub mod new_layer; -pub mod notes_cmds; -pub mod paint_cmds; mod param_check; -mod path_edit_cmds; -pub mod pattern_cmds; -pub mod pattern_fill_cmds; -pub mod perspective_crop_cmds; -pub mod photo_cmds; -pub mod pick_cmds; mod pixels; -pub mod plugin_cmds; pub mod prefs; -pub mod preset_import_cmds; pub mod preset_store; pub mod presets; -pub mod print_cmds; pub mod proof_sim; -pub mod rasterize_style_cmds; -pub mod redeye_cmds; -pub mod render_cmds; -pub mod retouch_cmds; -pub mod sample_cmds; -pub mod sampler_cmds; -pub mod select_extra_cmds; -pub mod selection_cmds; +pub mod select_mask_edits; pub mod shell_commands; pub mod shortcut_sets; -pub mod slice_cmds; -pub mod smart_cmds; -pub mod smartselect_cmds; pub mod snap; -pub mod solid_fill_cmds; -pub mod stamp_cmds; -pub mod swatch_cmds; -pub mod symmetry_cmds; -mod timeline_cmds; -pub mod transform_cmds; -mod trap_cmds; -pub mod type_caret_cmds; -pub mod type_cmds; -pub mod type_extra_cmds; -pub mod type_mask_cmds; -pub mod type_spell_cmds; -pub mod type_styles_cmds; -mod variables_cmds; pub use variables_cmds::{export_data_sets_with, import_data_sets_text}; -pub mod vector_cmds; -mod video_cmds; pub use video_cmds::render_video_with; -pub mod vp_cmds; -pub mod warp_cmds; -pub mod web_cmds; -mod wia_cmds; use std::sync::Arc; @@ -143,32 +53,120 @@ pub use photocraft_doc as doc; pub use photocraft_paint as paint; pub use photocraft_paint::BrushSettings; -#[derive(Debug, thiserror::Error)] -pub enum EngineError { - #[error("unknown command `{0}`")] - UnknownCommand(String), - #[error("command `{0}` is not available right now: {1}")] - Disabled(String, String), - #[error("invalid parameters for `{cmd}`: {msg}")] - BadParams { cmd: String, msg: String }, - #[error("no active document")] - NoDocument, - #[error("no such layer {0:?}")] - NoLayer(LayerId), - #[error("{0}")] - Other(String), - /// A background job was cancelled (see [`jobs`]); nothing changed. - #[error("cancelled")] - Cancelled, -} +pub mod cmd; +mod doc_state; +mod error; +mod session; -pub type Result = std::result::Result; - -impl From for EngineError { - fn from(error: photocraft_raster::AllocationError) -> Self { - Self::Other(format!("not enough memory for this operation ({error}); the document was not changed")) - } -} +pub use cmd as commands; +pub use cmd::actions_cmds; +pub use cmd::actions_edit_cmds; +pub use cmd::adjust_cmds; +pub use cmd::align_cmds; +pub use cmd::analysis_cmds; +pub use cmd::artboard_cmds; +pub use cmd::atn_cmds; +pub use cmd::automate_cmds; +pub use cmd::background_cmds; +pub use cmd::brush_cmds; +pub use cmd::brush_key_cmds; +pub use cmd::brush_preset_cmds; +pub use cmd::channel_cmds; +pub use cmd::color_cmds; +pub use cmd::color_to_alpha_cmds; +pub use cmd::comps_cmds; +pub use cmd::contour_lib_cmds; +pub use cmd::cutout_cmds; +pub use cmd::distort_cmds; +pub use cmd::document_preset_cmds; +pub use cmd::edit_cmds; +pub use cmd::edit_menu_cmds; +pub use cmd::eraser_cmds; +pub use cmd::export_preset_cmds; +pub use cmd::exr_cmds; +pub use cmd::extra_cmds; +pub use cmd::face_cmds; +pub use cmd::file_cmds; +pub use cmd::fill_cmds; +pub use cmd::fill_key_cmds; +pub use cmd::float_cmds; +pub use cmd::font_library_cmds; +use cmd::frame_cmds; +pub use cmd::fx_view_cmds; +pub use cmd::fx_visibility_cmds; +pub use cmd::gallery_cmds; +pub use cmd::gradient_fill_cmds; +pub use cmd::group_view_cmds; +pub use cmd::history_cmds; +pub use cmd::history_menu_cmds; +pub use cmd::history_options_cmds; +pub use cmd::history_select_cmds; +pub use cmd::history_source_cmds; +pub use cmd::image_cmds; +pub use cmd::layer_copy_cmds; +pub use cmd::layer_label_cmds; +pub use cmd::layer_list_cmds; +pub use cmd::layer_mask_props_cmds; +pub use cmd::layer_menu_cmds; +pub use cmd::layer_multi_cmds; +pub use cmd::layer_nav_cmds; +pub use cmd::layer_xmp_cmds; +pub use cmd::lens_cmds; +pub use cmd::magnetic_cmds; +pub use cmd::mask_view_cmds; +use cmd::migrate_cmds; +pub use cmd::mode_cmds; +pub use cmd::multichannel_cmds; +pub use cmd::notes_cmds; +pub use cmd::paint_cmds; +use cmd::path_edit_cmds; +pub use cmd::pattern_cmds; +pub use cmd::pattern_fill_cmds; +pub use cmd::perspective_crop_cmds; +pub use cmd::photo_cmds; +pub use cmd::pick_cmds; +pub use cmd::plugin_cmds; +pub use cmd::preset_import_cmds; +pub use cmd::print_cmds; +pub use cmd::rasterize_style_cmds; +pub use cmd::redeye_cmds; +pub use cmd::render_cmds; +pub use cmd::retouch_cmds; +pub use cmd::sample_cmds; +pub use cmd::sampler_cmds; +pub use cmd::select_extra_cmds; +pub use cmd::selection_cmds; +pub use cmd::slice_cmds; +pub use cmd::smart_cmds; +pub use cmd::smartselect_cmds; +pub use cmd::solid_fill_cmds; +pub use cmd::stamp_cmds; +pub use cmd::style_asl_cmds; +pub use cmd::swatch_cmds; +pub use cmd::symmetry_cmds; +use cmd::timeline_cmds; +pub use cmd::transform_cmds; +use cmd::trap_cmds; +pub use cmd::type_area_cmds; +pub use cmd::type_caret_cmds; +pub use cmd::type_cmds; +pub use cmd::type_extra_cmds; +pub use cmd::type_mask_cmds; +pub use cmd::type_spell_cmds; +pub use cmd::type_styles_cmds; +pub use cmd::updates_cmds; +use cmd::variables_cmds; +pub use cmd::vector_cmds; +use cmd::video_cmds; +pub use cmd::vp_cmds; +pub use cmd::warp_cmds; +pub use cmd::web_cmds; +use cmd::wia_cmds; +pub use doc_state::{CopiedStyle, DocState, PRECHECKED, ToolState}; +pub use error::{EngineError, Result}; +pub use session::move_item; +use session::restore_target; +pub(crate) use session::{carry_visibility, fix_selection, visibility_changes}; /// The pixels of a raster layer (typically one a command just created), as an error instead /// of a panic if the layer has none. @@ -183,257 +181,16 @@ pub(crate) fn active_layer_of(s: &Session) -> std::result::Result<&photocraft_do d.active_layer.and_then(|id| d.doc.layer(id)).ok_or_else(|| "no active layer".into()) } -/// Per-document editing state. -#[derive(Clone, Debug)] -pub struct DocState { - pub doc: Arc, - pub history: History, - /// The primary ("key") layer: what single-layer commands act on. Always a member of - /// `selected_layers` when set. - pub active_layer: Option, - /// Every selected layer in the Layers panel (⌘/⇧-click), in selection order. Like - /// `active_layer`, selecting is not a history step, but undo and redo restore the layers each - /// state targeted when the next step was taken; see [`DocState::selected_layers`]. - pub selected_layers: Vec, - /// Anchor of ⇧-click range selection (the last plainly or ⌘-clicked layer). - pub layer_anchor: Option, - pub path: Option, - /// An Affinity document (or its preview) must not acquire the native source as its Save path. - pub source_read_only: bool, - /// Increments on every change; UIs re-render when it moves. - pub revision: u64, - pub saved_revision: u64, - /// How much of `revision` came from view-only changes (channel eyes, targeting): pixel caches can ignore those. - pub view_revision: u64, - /// Area changed by the latest revision (None = assume everything changed). - pub last_damage: Option, - /// Coalescing key of the latest history step (see [`Session::execute`]'s `coalesce` param). - pub coalesce: Option, - /// Channels panel: targeted channel and eye toggles (view state, not history). - pub channel_view: channel_cmds::ChannelView, - /// Select › Isolate Layers: the Layers panel lists only these layers (empty = off; view state). - pub isolated_layers: Vec, - /// Painting symmetry preset or sampled path (tool state, not document pixels). - pub symmetry: Option, - /// Layers panel: layers whose effects list is collapsed under their row (the fx triangle; - /// view state, not history). Effects lists start open. - pub fx_collapsed: Vec, - /// ⌥-click on a layer's eye (`layer.showOnly`): the layer shown alone and every layer's - /// visibility before, so the next ⌥-click restores it (view state, not history). - pub show_only: Option<(LayerId, Vec<(LayerId, bool)>)>, - /// A floating selection (`select.float`): the cut piece and where it floats, until dropped - /// (view state: the document is unchanged until `select.drop`). - pub floating: Option, - /// Color Sampler points (`view.colorSamplers.*`, #1046): numbered sample positions in document - /// pixel coordinates. Per-document view state: discarded when the document closes, never saved - /// to `.pcraft` or PSD, and not a history step. - pub color_samplers: Vec<[f64; 2]>, - /// History panel snapshots, oldest first (`history.newSnapshot`, #2966). Kept for the - /// session only, as in Photoshop: not history states, and not saved with the document. - pub snapshots: Vec, - /// The id the next snapshot gets; ids are never reused, so deleting one keeps the others. - next_snapshot: u64, -} - -impl DocState { - /// Trim the history to its budget, counting the tiles only History panel snapshots hold - /// against it too (#4530). Returns how many states were dropped. - pub fn trim_history(&mut self) -> usize { - let snaps: Vec<&Document> = self.snapshots.iter().map(|s| &*s.doc).collect(); - self.history.trim_with(&self.doc, &snaps) - } - - /// Pixel bytes of the document, the tiles only its history holds and the tiles only its - /// History panel snapshots hold. - pub fn memory_bytes(&self) -> usize { - let snaps: Vec<&Document> = self.snapshots.iter().map(|s| &*s.doc).collect(); - self.history.pixel_bytes(&self.doc).saturating_add(self.history.extra_bytes(&self.doc, &snaps)) - } - - pub fn new(doc: Document, path: Option) -> Self { - let active_layer = doc.top_layer(); - let mut history = History::default(); - history.set_current_layers(LayerTarget { active: active_layer, selected: active_layer.into_iter().collect() }); - Self { - doc: Arc::new(doc), - history, - active_layer, - selected_layers: active_layer.into_iter().collect(), - layer_anchor: active_layer, - path, - source_read_only: false, - revision: 1, - saved_revision: 1, - view_revision: 0, - last_damage: None, - coalesce: None, - channel_view: Default::default(), - isolated_layers: Vec::new(), - symmetry: None, - fx_collapsed: Vec::new(), - show_only: None, - floating: None, - color_samplers: Vec::new(), - snapshots: Vec::new(), - next_snapshot: 1, - } - } - /// The selected layers in bottom-to-top document order, always including the active layer. - /// Robust against stale state: ids no longer in the document are skipped, and if the active - /// layer was changed without updating the set, the selection is just the active layer. - pub fn selected_layers(&self) -> Vec { - let Some(active) = self.active_layer else { return Vec::new() }; - let set: &[LayerId] = if self.selected_layers.contains(&active) { &self.selected_layers } else { std::slice::from_ref(&active) }; - self.doc.walk().into_iter().map(|(_, _, l)| l.id).filter(|id| set.contains(id)).collect() - } - pub fn is_layer_selected(&self, id: LayerId) -> bool { - self.active_layer == Some(id) || (self.active_layer.is_some_and(|a| self.selected_layers.contains(&a)) && self.selected_layers.contains(&id)) - } - pub fn is_dirty(&self) -> bool { - self.revision != self.saved_revision - } - /// The targeted layers, as history stores them with each state. - fn layer_target(&self) -> LayerTarget { - LayerTarget { active: self.active_layer, selected: self.selected_layers.clone() } - } -} - -/// App-wide tool state that commands read (foreground colour, brush…). -#[derive(Clone, Debug)] -pub struct ToolState { - pub foreground: [f32; 4], - pub background: [f32; 4], - pub brush: photocraft_paint::BrushSettings, - /// Brush presets (built-ins plus user presets; see `brush.presets.*`). - pub presets: Vec, - /// Bumped whenever `presets` changes ([`Session::brush_presets_changed`]); the preset store - /// syncs when it moves. - pub presets_rev: u64, - /// Mixer Brush paint carried between strokes. - pub mixer: photocraft_paint::mixer::MixerState, - /// The coalescing key of the running `tools.setBrush` gesture and the brush before it, so the - /// gesture journals as one call ([`brush_cmds::coalesce_journal`]). - pub brush_gesture: Option<(String, photocraft_paint::BrushSettings)>, - /// Name of the preset the current brush was last picked from. Kept across later edits (the - /// preset stays "the current brush" so `brush.presets.update` can overwrite it); cleared when - /// the brush is reset or the preset is gone. - pub current_preset: Option, - /// Bumped whenever `brush` or `current_preset` changes ([`ToolState::brush_changed`]). The - /// last-brush persistence watches it instead of comparing the brush itself, so an unchanged - /// brush costs a counter compare per frame, not a clone and serialise. - pub brush_rev: u64, - /// The revision whose value is in `Preferences::last_brush` (a restored one counts as saved): - /// [`brush_rev`](Self::brush_rev) == `brush_saved_rev` means there is nothing new to write. - pub brush_saved_rev: u64, - /// A layer mask is the edit target ([`ToolState::target_mask`]). - pub mask_targeted: bool, - /// The foreground/background pair of the edit target that isn't current: the layer pixels' - /// colours while a mask is targeted, the mask's while the pixels are (#2166). - pub other_colors: [[f32; 4]; 2], - /// Colours picked as CMYK, newest last: the sRGB colour that stands for them and the ink - /// values chosen. The tool colours are stored as sRGB, and an sRGB round trip does not give - /// back what was typed (100/0/0/0 cyan is outside sRGB), so a CMYK document is filled with - /// the inks the person picked (#4196). Matched by colour, so it follows a swap, a reset or a - /// mask pair without bookkeeping. - pub cmyk_picks: Vec<([f32; 3], [f32; 4])>, -} - -impl ToolState { - /// How many CMYK picks are remembered. - const CMYK_PICKS: usize = 8; - - /// Remember that sRGB colour `rgb` was picked as the inks `cmyk` (each 0..1). - pub fn remember_cmyk(&mut self, rgb: [f32; 3], cmyk: [f32; 4]) { - self.cmyk_picks.retain(|(c, _)| *c != rgb); - self.cmyk_picks.push((rgb, cmyk)); - let extra = self.cmyk_picks.len().saturating_sub(Self::CMYK_PICKS); - self.cmyk_picks.drain(..extra); - } - - /// The inks `rgb` was picked as, if it was picked as CMYK. - pub fn picked_cmyk(&self, rgb: [f32; 3]) -> Option<[f32; 4]> { - self.cmyk_picks.iter().rev().find(|(c, _)| *c == rgb).map(|(_, k)| *k) - } - - /// Photoshop's colour pair for a layer mask that has none yet: white foreground, black - /// background (reveal-paint first). - pub const MASK_COLORS: [[f32; 4]; 2] = [[1.0, 1.0, 1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]; - - /// Note that `brush` or `current_preset` changed. Every setter of either (commands and the - /// shell's tool-switch paths) calls this; the last-brush persistence only serialises when the - /// revision has moved since the stored value ([`Self::brush_saved_rev`]). - pub fn brush_changed(&mut self) { - self.brush_rev = self.brush_rev.wrapping_add(1); - } - - /// Switch the edit target between layer pixels and a layer mask. Like Photoshop, the mask and - /// the pixels keep separate app-wide foreground/background pairs: changing the target puts - /// the current pair aside and brings back the other one, so colours picked while a mask is - /// targeted are remembered for masks (#2166). The mask pair starts at [`Self::MASK_COLORS`]. - /// Returns whether the target changed. - pub fn target_mask(&mut self, mask: bool) -> bool { - if self.mask_targeted == mask { - return false; - } - self.mask_targeted = mask; - let [fg, bg] = std::mem::replace(&mut self.other_colors, [self.foreground, self.background]); - self.foreground = fg; - self.background = bg; - true - } -} - -impl Default for ToolState { - fn default() -> Self { - Self { - foreground: [0.0, 0.0, 0.0, 1.0], - background: [1.0, 1.0, 1.0, 1.0], - // Photoshop's Brush tool starts at 10 % Smoothing. - brush: photocraft_paint::BrushSettings { - smoothing: photocraft_paint::brush::Smoothing { amount: 0.1, ..Default::default() }, - ..Default::default() - }, - presets: photocraft_paint::presets::builtin(), - presets_rev: 0, - mixer: Default::default(), - brush_gesture: None, - current_preset: None, - brush_rev: 0, - brush_saved_rev: 0, - mask_targeted: false, - other_colors: Self::MASK_COLORS, - cmyk_picks: Vec::new(), - } - } -} - -/// Layer › Layer Style › Copy Layer Style: the effects and the Blending Options, as Photoshop -/// copies them (Paste Layer Style puts them all on each target). -#[derive(Clone, Debug)] -pub struct CopiedStyle { - pub effects: photocraft_doc::Effects, - pub blend: photocraft_color::BlendMode, - pub fill: f32, - pub advanced: photocraft_doc::AdvancedBlending, - pub blend_if: photocraft_doc::BlendIf, - pub excluded_channels: u32, - /// The colour mode it was copied in: Blend If's channel ranges and the channel restrictions - /// only carry over to a document in the same mode. - pub mode: photocraft_color::ColorMode, -} - -/// The commands whose [`Session::precheck`] is complete: it accepts what `execute` does (Free -/// Transform applies its box before these, and only when they will run), but for the group -/// nesting limit ([`photocraft_doc::MAX_GROUP_DEPTH`]): grouping layers already that deep is -/// refused only as the edit is made. Layer via Copy and -/// Cut aren't: whether they have anything to copy (a selection over transparent pixels, a group -/// or fill layer) is known only when they run. -pub const PRECHECKED: &[&str] = &["layer.new.layer", "layer.new.group", "layer.new.groupFromLayers", "layer.new.layerFromBackground"]; - #[derive(Default)] pub struct Session { docs: Vec, active: Option, + /// Monotonic counter, bumped whenever a different document becomes active (including a + /// switch away and back inside one pass). UI work that must not cross documents (control + /// screenshots, automation input) binds to it. + active_generation: u64, + /// The document id [`Self::active_generation`] was last bumped for. + active_doc_for_generation: Option, pub tools: ToolState, /// Log of executed commands (for action recording and debugging). pub journal: Vec<(String, Value)>, @@ -457,8 +214,13 @@ pub struct Session { pub quick_mask_options: channel_cmds::QuickMaskOptions, /// Colour channel the running command may change (a single colour channel is targeted). color_restrict: Option, + /// Smart object whose filter mask the running command edits (`"target":"filterMask"`): each + /// edit re-renders it, in the same history step. + filter_mask_layer: Option, /// Edit › Preferences, keyboard shortcuts, menu and toolbar customisation (see `prefs`). pub prefs: prefs::PrefsStore, + /// Release checks use an injected platform transport; no network I/O in the engine. + pub updates: updates_cmds::Updates, /// Edit menu state: Fade source, custom shape library (see `edit_menu_cmds`). pub edit_state: edit_menu_cmds::EditState, /// The pattern library (Window › Patterns; see `pattern.*`). @@ -480,6 +242,12 @@ pub struct Session { /// the same check a top-level request sees. `None` runs everything, which is what a local /// UI and `photocraft-cli run` do. pub authorize: Option Result<()>>, + /// A further route for File › Print when neither `lp` nor a CUPS server over IPP is + /// reachable (the XDG Print portal in a Flatpak); see `print_cmds::Spooler`. + pub print_spooler: Option, + /// The printers `file.printers`, `file.print.printers` and the Print dialog list in place of + /// the system's (tests); see `print_cmds::PrinterSource`. `None` asks the spooler. + pub printer_source: Option, /// Background jobs (see [`jobs`]). jobs: jobs::Jobs, /// Color Sampler points dropped by the last canvas-size change (crop, Canvas Size, Image Size, @@ -488,6 +256,9 @@ pub struct Session { /// The canvas-geometry map the next [`Session::edit`] applies to the Color Sampler points (crop, /// Canvas Size, Image Size, rotation set it just before their edit). Consumed by the edit. sampler_map: Option, + /// The same edit's map in edge coordinates, for the painting symmetry (Image Rotation maps the + /// samplers' pixel centres instead). Consumed by the edit. + symmetry_map: Option, /// Each open document's linked smart objects' files: size and modification time when last /// seen ([`link_watch`]). pub link_stamps: std::collections::HashMap<(u64, String), link_watch::Stamp>, @@ -506,502 +277,15 @@ pub struct Session { /// document, source read-only)`. The shell takes them with [`Session::take_held_open`]. held_opens: Vec<(u64, Document, bool)>, next_held_open: u64, -} - -/// Move item `i` of `v` to position `to`, clamped to the end. Returns where it went; `None` when -/// `i` is out of range. -pub fn move_item(v: &mut Vec, i: usize, to: usize) -> Option { - if i >= v.len() { - return None; - } - let x = v.remove(i); - let to = to.min(v.len()); - v.insert(to, x); - Some(to) -} - -impl Session { - pub fn new() -> Self { - Self::default() - } - - pub fn documents(&self) -> &[DocState] { - &self.docs - } - pub fn active_index(&self) -> Option { - self.active - } - pub fn active(&self) -> Option<&DocState> { - self.active.and_then(|i| self.docs.get(i)) - } - pub fn active_mut(&mut self) -> Option<&mut DocState> { - self.active.and_then(|i| self.docs.get_mut(i)) - } - pub fn set_active(&mut self, index: usize) -> bool { - if index < self.docs.len() { - self.active = Some(index); - self.sync_tools_mask_target(); - true - } else { - false - } - } - - /// Color Sampler points dropped by the last canvas-size change (crop, Canvas Size, Image Size, - /// rotation) since this was last called, for the status message. See [`edit`](Self::edit). - pub fn take_samplers_removed(&mut self) -> usize { - std::mem::take(&mut self.samplers_removed) - } - - /// Move the Color Sampler points through `a` in the next [`Session::edit`] (crop, Canvas Size, - /// Image Size, rotation), dropping the ones that land outside the new canvas. View state: the - /// points stay pinned to their pixels, no history step. Cleared by the edit it is set for. - pub(crate) fn set_sampler_map(&mut self, a: photocraft_geom::Affine) { - self.sampler_map = Some(a); - } - - pub fn sync_tools_mask_target(&mut self) { - let mask = self.active().is_some_and(|st| st.channel_view.target == crate::channel_cmds::ChannelTarget::LayerMask); - self.tools.target_mask(mask); - } - - /// Move the document at `from` to tab position `to` (clamped to the last), keeping the active - /// document active. Returns its new index; `None` when `from` is out of range. - pub fn move_document(&mut self, from: usize, to: usize) -> Option { - let active = self.active().map(|d| d.doc.id); - let to = move_item(&mut self.docs, from, to)?; - self.active = active.and_then(|id| self.docs.iter().position(|d| d.doc.id == id)); - Some(to) - } - - /// Add a document (from File → New, an import, etc.) and make it active. - /// Preserve its identity unless another open document already owns it. - /// Identity-dependent callers must read the admitted document at the returned index. - pub fn add_document(&mut self, mut doc: Document, path: Option) -> usize { - // Persisted IDs can overlap the allocator, so check every replacement too. - while self.docs.iter().any(|st| st.doc.id == doc.id) { - doc.id = photocraft_doc::DocId::fresh(); - } - let mut st = DocState::new(doc, path); - st.history.max_states = self.prefs.get().performance.history_states.max(1) as usize; - st.history.max_bytes = self.prefs.get().performance.history_budget_bytes(); - self.docs.push(st); - let i = self.docs.len() - 1; - self.active = Some(i); - i - } - - /// Holds a decoded document back from opening (see `held_opens`); returns its id. - pub(crate) fn hold_open(&mut self, doc: Document, source_read_only: bool) -> u64 { - self.next_held_open = self.next_held_open.wrapping_add(1); - self.held_opens.push((self.next_held_open, doc, source_read_only)); - self.next_held_open - } - - /// Takes a document a background open held back for the profile prompt: the document and - /// whether its source is read-only. `None` when there is no such document (already taken). - pub fn take_held_open(&mut self, id: u64) -> Option<(Document, bool)> { - let i = self.held_opens.iter().position(|(h, _, _)| *h == id)?; - let (_, doc, read_only) = self.held_opens.remove(i); - Some((doc, read_only)) - } - - pub fn close(&mut self, index: usize) -> Option { - if index >= self.docs.len() { - return None; - } - smart_cmds::on_close(self, index); - if let Some(id) = self.docs.get(index).map(|d| d.doc.id) { - self.cancel_jobs_on(id); - self.subject_cache.forget_doc(id); - self.object_finder.forget_doc(id); - } - let d = self.docs.remove(index); - self.active = if self.docs.is_empty() { - None - } else { - Some(match self.active { - Some(active) if active > index => active - 1, - Some(active) if active < index => active, - _ => index.min(self.docs.len() - 1), - }) - }; - Some(d) - } - - /// Run a command by id with JSON params. Returns a JSON result. - /// - /// Any command accepts an optional `"coalesce": ""` param: consecutive edits with the same - /// key (and no other edit, undo or redo in between) share one history step, like Photoshop's - /// single "Edit Type Layer" step for a whole typing session or one step per slider drag. - /// - /// Runs synchronously, including job-capable commands (see [`jobs`]; [`Session::start`] runs - /// those in the background). - pub fn execute(&mut self, id: &str, params: Value) -> Result { - match self.dispatch(id, params, false)? { - jobs::Started::Done(v) => Ok(v), - // Not reached: inline dispatch never starts a job. Waiting is still correct. - jobs::Started::Job(j) => self.wait_job(j), - } - } - - /// Offer a finished live stroke to the next command: a `paint.stroke` / `paint.pencil` whose - /// params match it exactly takes its pixels instead of rendering the stroke again (its result - /// then says `"fromLive": true`); any other command drops the offer. The command still runs - /// through the normal dispatch, so authorization, the journal and history are unchanged. - pub fn offer_live_stroke(&mut self, live: brush_cmds::LiveStroke) -> Result<()> { - self.live_commit = Some(live.into_commit()?); - Ok(()) - } - - /// [`Self::offer_live_stroke`] for a live Clone Stamp or History Brush stroke, taken by the - /// `paint.cloneStamp` / `paint.historyBrush` it was drawn for. Refused for the Healing Brush - /// (it blends on release) and when the brush, layer or clone source changed during the stroke. - pub fn offer_live_retouch(&mut self, live: retouch_cmds::LiveRetouch) -> Result<()> { - let commit = live.into_commit(self)?; - self.live_commit = Some(commit); - Ok(()) - } - - /// Withdraw an offer the command didn't take. - pub fn clear_live_stroke(&mut self) { - self.live_commit = None; - } - - /// Check a call without running it: the command exists, its params have the shape - /// [`Self::execute`] requires (New Layer's fully), and it can run now. For a caller that does - /// something first only when the command will go through: Free Transform applies its box - /// before a new layer is made. - /// - /// Complete for the commands in [`PRECHECKED`]: there it accepts what `execute` accepts, but - /// for the group nesting limit (a test holds the two together). For others it checks only what they share. - pub fn precheck(&mut self, id: &str, params: &Value) -> Result<()> { - let spec = crate::commands::find(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; - if !(params.is_object() || params.is_null()) { - return Err(EngineError::BadParams { cmd: id.to_string(), msg: "params must be a JSON object".into() }); - } - crate::param_check::check(spec, params)?; - crate::filters::validate_params(id, params)?; - if id == "layer.new.layer" { - crate::new_layer::parse(params)?; - } - // A command that takes `"layer"` (an id or a name, as it resolves it) fails on one the - // document doesn't have. - if spec.params.contains("\"layer\":id") - && let Some(l) = commands::explicit_layer(self, params)? - && self.active().is_none_or(|st| st.doc.layer(l).is_none()) - { - return Err(EngineError::BadParams { cmd: id.to_string(), msg: format!("no layer {}", l.0) }); - } - self.admit(id, spec, params).map(|_| ()) - } - - /// What `dispatch` checks once the params are valid and a floating selection has dropped: - /// the precondition (on a named layer for commands that take one), a job running on the - /// document, a hidden target. The run params (the Channels panel's target filled in) when it - /// may run. - pub(crate) fn admit(&mut self, id: &str, spec: &commands::CommandSpec, params: &Value) -> Result { - let run_params = channel_cmds::inject_target(self, id, commands::inject_kind(id, params.clone())); - if let Err(why) = self.explicit_layer_enabled(spec, params).unwrap_or_else(|| self.precondition(spec, &run_params)) { - return Err(EngineError::Disabled(id.to_string(), why)); - } - if let Some(why) = self.job_conflict(id, spec.journal) { - return Err(EngineError::Disabled(id.to_string(), why)); - } - // Painting on or moving a hidden layer is refused, as in Photoshop (#571). - if let Some(why) = hidden_target::refusal(self, id, params) { - return Err(EngineError::Other(why.into())); - } - Ok(run_params) - } - - /// Is the command currently runnable? (drives menu enablement) - pub fn is_enabled(&self, id: &str) -> bool { - self.is_enabled_with(id, &Value::Null) - } - - /// [`Self::is_enabled`] for a call with `params`: their `"target"` can enable a command, as a - /// targeted layer mask enables Image › Adjustments › Invert on an adjustment layer (#780). - pub fn is_enabled_with(&self, id: &str, params: &Value) -> bool { - self.disabled_reason_with(id, params).is_none() - } - - /// Why the command can't run now (None = it can): its own precondition, or a background job - /// running on the active document. - pub fn disabled_reason(&self, id: &str) -> Option { - self.disabled_reason_with(id, &Value::Null) - } - - /// [`Self::disabled_reason`] for a call with `params`. - pub fn disabled_reason_with(&self, id: &str, params: &Value) -> Option { - let Some(s) = commands::find(id) else { return Some(format!("unknown command `{id}`")) }; - self.precondition(s, &channel_cmds::inject_target(self, id, params.clone())).err().or_else(|| self.job_conflict(id, s.journal)) - } - - /// The command's own precondition for a call with `params` (their target filled in). - fn precondition(&self, spec: &commands::CommandSpec, params: &Value) -> std::result::Result<(), String> { - if self.names_existing_layer(spec.id, params) { - return Ok(()); - } - // `ifPresent`: with no Background there is nothing to do, which isn't an error. - if spec.id == "layer.new.layerFromBackground" && params.get("ifPresent").and_then(Value::as_bool) == Some(true) { - return self.active().map(|_| ()).ok_or_else(|| "no document open".into()); - } - channel_cmds::mask_target_enabled(self, spec.id, params).unwrap_or_else(|| (spec.enabled)(self)) - } - - /// Whether `id` is a layer command whose `"layer"` param names an existing layer, so it needs no - /// active layer (#2457). Matches how `layer_param` resolves the layer when the command runs. - fn names_existing_layer(&self, id: &str, params: &Value) -> bool { - matches!(id, "layer.hideLayers" | "layer.showLayers" | "layer.renameLayer") - && params.get("layer").and_then(Value::as_u64).is_some_and(|l| self.active().is_some_and(|st| st.doc.layer(LayerId(l)).is_some())) - } - - /// The precondition of a command whose params document `"layer":id` (it acts on that layer), - /// when `params` name an existing layer of the active document: checked on that layer rather - /// than on the active one, which stays active (#466). A script, action or agent can then - /// Replace Contents, Rasterize or Merge Down a layer without selecting it first. `None` for - /// other commands, or without such a layer. - pub(crate) fn explicit_layer_enabled(&mut self, spec: &commands::CommandSpec, params: &Value) -> Option> { - if !spec.params.contains("\"layer\":id") { - return None; - } - let target = LayerId(params.get("layer")?.as_u64()?); - let d = self.active_mut()?; - d.doc.layer(target)?; - let active = d.active_layer.replace(target); - let r = (spec.enabled)(self); - if let Some(d) = self.active_mut() { - d.active_layer = active; - } - Some(r) - } - - /// Apply an undoable edit to the active document. - pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { - // Every mutation path (linked-file refresh, transform, place) re-renders smart objects - // through this entry; the requesting session's authority must reach those renders. - let _authorization = crate::jobs::render_authorization_scope(self.authorize); - let restrict = self.color_restrict; - let sampler_map = self.sampler_map.take(); - let st = self.active_mut().ok_or(EngineError::NoDocument)?; - let before = st.doc.clone(); - // Selecting layers is not a step, so the state this edit leaves behind targets what was - // selected just before it (#1356): undoing a stroke keeps the painted layer active. - let prior = st.layer_target(); - let mut doc = (*before).clone(); - let mut active = st.active_layer; - let r = f(&mut doc, &mut active)?; - if let (Some(k), Some(id)) = (restrict, active) { - channel_cmds::restrict_to_color(&before, &mut doc, id, k); - } - st.doc = Arc::new(doc); - st.active_layer = active; - fix_selection(st); - channel_cmds::fix_view(st); - // A canvas-size change (crop, Canvas Size, Image Size, rotation) moves the Color Sampler - // points with their pixels and drops the ones that land outside the new canvas. View - // state: not a history step. A mapped edit (the four above) gives the exact map; any other - // size change falls back to dropping whatever is now outside. - let mut samplers_removed = 0; - if let Some(a) = sampler_map { - let bounds = st.doc.bounds(); - let n = st.color_samplers.len(); - st.color_samplers.retain_mut(|p| { - let q = a.apply(photocraft_geom::Point::new(p[0], p[1])); - *p = [q.x, q.y]; - bounds.contains(q.x.floor() as i32, q.y.floor() as i32) - }); - samplers_removed = n - st.color_samplers.len(); - } else if st.doc.size != before.size { - let bounds = st.doc.bounds(); - let n = st.color_samplers.len(); - st.color_samplers.retain(|p| bounds.contains(p[0].floor() as i32, p[1].floor() as i32)); - samplers_removed = n - st.color_samplers.len(); - } - // The pixels the edit can have changed, as its undo would report them: nothing for a - // selection, guides or a layer's name (a new lasso selection used to recomposite a large - // document, seconds on the CPU path, #1773), a new or deleted layer's bounds (a - // duplicate or a paste), and everything when that can't be bounded. A command may still - // report its own, tighter damage after this. - let damage = layer_multi_cmds::step_damage(&before, &st.doc); - let key = self.coalesce_request.clone(); - let st = self.active_mut().ok_or(EngineError::NoDocument)?; - let layers = st.layer_target(); - if key.is_none() || st.coalesce != key || !st.history.can_undo() { - st.history.set_current_layers(prior); - st.history.record(label, before, layers); - st.trim_history(); - } else { - st.history.set_current_layers(layers); - } - st.coalesce = key; - st.revision += 1; - st.last_damage = damage; - self.samplers_removed += samplers_removed; - if damage.is_none_or(|d| !d.is_empty()) { - // Free the mask early; a stale entry would only miss (it is keyed by pixels). - self.subject_cache = Default::default(); - self.object_finder = Default::default(); - } - Ok(r) - } - - /// Replace the active document's active layer without creating a history step. - pub fn select_layer(&mut self, id: LayerId) -> Result<()> { - let st = self.active_mut().ok_or(EngineError::NoDocument)?; - if st.doc.layer(id).is_none() { - return Err(EngineError::NoLayer(id)); - } - st.active_layer = Some(id); - st.selected_layers = vec![id]; - st.layer_anchor = Some(id); - channel_cmds::fix_view(st); - // Selecting a layer is not an edit: keep a clean document clean. - let clean = st.saved_revision == st.revision; - st.revision += 1; - if clean { - st.saved_revision = st.revision; - } - st.last_damage = Some(photocraft_geom::Rect::EMPTY); - Ok(()) - } - - /// Re-renders the pixels of type layers whose font arrived after they were drawn (the web - /// build fetches served fonts on demand, `photocraft_text::served`). The layers' model is - /// unchanged, so this is no history step and leaves a clean document clean. Unknown documents - /// and layers are skipped. A document a background job is computing from must not move under - /// it: its layers are returned, to try again later. - pub fn refresh_type_layers(&mut self, layers: &[(photocraft_doc::DocId, LayerId)]) -> Vec<(photocraft_doc::DocId, LayerId)> { - let mut busy = Vec::new(); - let mut docs: Vec = Vec::new(); - for (d, _) in layers { - if !docs.contains(d) { - docs.push(*d); - } - } - for doc_id in docs { - let mine = layers.iter().filter(|(d, _)| *d == doc_id); - if self.job_on(doc_id).is_some() { - busy.extend(mine); - continue; - } - let Some(st) = self.docs.iter_mut().find(|st| st.doc.id == doc_id) else { continue }; - let snapshot = st.doc.clone(); - let mut doc = (*snapshot).clone(); - let mut changed = false; - for (_, id) in mine { - if let Some(photocraft_doc::Layer { content: photocraft_doc::LayerContent::Text(t), .. }) = doc.layer_mut(*id) { - type_cmds::refresh(&snapshot, t); - changed = true; - } - } - if changed { - let clean = st.saved_revision == st.revision; - st.doc = Arc::new(doc); - st.revision += 1; - if clean { - st.saved_revision = st.revision; - } - st.last_damage = None; - } - } - busy - } - - pub fn undo(&mut self) -> bool { - // A background job is computing from the current state: it must not move under it. - if self.active_job().is_some() { - return false; - } - let Some(st) = self.active_mut() else { return false }; - st.coalesce = None; - match st.history.undo(st.doc.clone()) { - Some((mut d, layers)) => { - // Save As changes file identity outside history, just like the saved path. - if d.name != st.doc.name { - Arc::make_mut(&mut d).name.clone_from(&st.doc.name); - } - // Pixels this step can have touched, so the canvas recomposites only that - // (it recomposited everything before). - let damage = layer_multi_cmds::step_damage(&d, &st.doc); - st.doc = d; - restore_target(st, layers); - st.revision += 1; - st.last_damage = damage; - true - } - None => false, - } - } - - pub fn redo(&mut self) -> bool { - if self.active_job().is_some() { - return false; - } - let Some(st) = self.active_mut() else { return false }; - st.coalesce = None; - match st.history.redo(st.doc.clone()) { - Some((mut d, layers)) => { - if d.name != st.doc.name { - Arc::make_mut(&mut d).name.clone_from(&st.doc.name); - } - let damage = layer_multi_cmds::step_damage(&st.doc, &d); - st.doc = d; - restore_target(st, layers); - st.revision += 1; - st.last_damage = damage; - true - } - None => false, - } - } -} - -/// Undo / redo: target the layers the restored state targeted, as far as they still exist. -fn restore_target(st: &mut DocState, layers: LayerTarget) { - // Deselect Layers is a valid history target, not a missing layer to replace with the top - // layer. An edit after deselection must undo back to no active or selected layers (#3042). - if layers.active.is_none() && layers.selected.is_empty() { - st.active_layer = None; - st.selected_layers.clear(); - st.layer_anchor = None; - channel_cmds::fix_view(st); - return; - } - if layers.active.is_some_and(|id| st.doc.layer(id).is_some()) { - st.active_layer = layers.active; - st.selected_layers = layers.selected; - } - fix_active(st); -} - -fn fix_active(st: &mut DocState) { - if st.active_layer.is_none_or(|id| st.doc.layer(id).is_none()) { - st.active_layer = st.doc.top_layer(); - } - fix_selection(st); - channel_cmds::fix_view(st); -} - -/// Keep the layer selection valid after the document or the active layer changed: drop deleted -/// layers, and collapse to the active layer when a command made a non-member active. -pub(crate) fn fix_selection(st: &mut DocState) { - let Some(active) = st.active_layer else { - st.selected_layers.clear(); - return; - }; - if !st.selected_layers.contains(&active) { - st.selected_layers = vec![active]; - } else { - let live: std::collections::HashSet = st.doc.walk().into_iter().map(|(_, _, l)| l.id).collect(); - st.selected_layers.retain(|id| live.contains(id)); - let mut seen = std::collections::HashSet::new(); - st.selected_layers.retain(|id| seen.insert(*id)); - } - if st.layer_anchor.is_some_and(|a| !st.selected_layers.contains(&a)) { - st.layer_anchor = Some(active); - } + /// The document before the latest [`Session::edit`] of the running command, which + /// [`Session::hint_damage`] checks a hint against. Set by `edit`, taken by `hint_damage`, + /// dropped when the command ends. Debug and test builds only. + #[cfg(any(test, debug_assertions))] + hint_before: Option>, + /// Damage hints that missed a tile their edit changed ([`Session::hint_damage`]): each one a + /// command reporting too narrow a redraw area (#4053). Counted in debug and test builds only; + /// tests read it. + pub damage_misses: usize, } #[cfg(test)] @@ -1013,3 +297,5 @@ mod tests; #[cfg(test)] mod fx_mode_tests; + +pub mod command_hints; diff --git a/crates/engine/src/link_watch.rs b/crates/engine/src/link_watch.rs index 5c235fb919..453af7806b 100644 --- a/crates/engine/src/link_watch.rs +++ b/crates/engine/src/link_watch.rs @@ -16,6 +16,9 @@ //! keeps waiting for Update Modified Content, as in Photoshop); one that can't be read (another app //! is still writing it) is tried again once it changes again, not on every look. Only smart objects //! of the document itself follow their files, not ones nested in another smart object's contents. +//! While a background job runs on the document, its files aren't looked at: an update would replace +//! the snapshot the job works on and its result would be thrown away. The first look after the job +//! ends catches up. use std::collections::{BTreeMap, HashMap, HashSet}; use std::sync::{Arc, Mutex, PoisonError}; @@ -32,15 +35,17 @@ pub type Stamp = (u64, u128); /// ids, stamp)]). pub type LinkScan = Arc, Option)>)>>>; +/// Stamps `file`, a linked path resolved by [`crate::doc_path`]; `None` (not looked at, like a +/// missing file) when it is not followed. #[cfg(not(target_arch = "wasm32"))] -fn stamp(path: &str) -> Option { - let meta = std::fs::metadata(path).ok()?; +fn stamp(file: Option<&std::path::Path>) -> Option { + let meta = std::fs::metadata(file?).ok()?; let modified = meta.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?; Some((meta.len(), modified.as_nanos())) } #[cfg(target_arch = "wasm32")] -fn stamp(_: &str) -> Option { +fn stamp(_: Option<&std::path::Path>) -> Option { None } @@ -61,11 +66,24 @@ pub fn linked_files(d: &Document) -> Vec<(String, Vec)> { /// the layers of the ones that changed. pub(crate) fn update_changed(s: &mut Session, _: &Value) -> Result { let st = s.active().ok_or(EngineError::NoDocument)?; - let (doc, files) = (st.doc.id.0, linked_files(&st.doc)); - let now = files.into_iter().map(|(p, ids)| (p.clone(), ids, stamp(&p))).collect(); + let (doc, files) = (st.doc.id.0, resolved_files(st)); + let now = files.into_iter().map(|(p, file, ids)| (p, ids, stamp(file.as_deref()))).collect(); s.apply_link_stamps(doc, now) } +/// [`linked_files`] of `st`'s document with the file each path resolves to, or `None` for a path +/// that is not followed ([`crate::doc_path`]). +fn resolved_files(st: &crate::DocState) -> Vec<(String, Option, Vec)> { + let document = st.path.as_deref().map(std::path::Path::new); + linked_files(&st.doc) + .into_iter() + .map(|(p, ids)| { + let file = crate::doc_path::resolve_in(&p, document).ok(); + (p, file, ids) + }) + .collect() +} + impl Session { /// Start stamping the active document's linked files on a worker thread, unless a look is /// still running (or the build has no files to look at: the web app). @@ -74,14 +92,15 @@ impl Session { return; } let Some(st) = self.active() else { return }; - let (doc, files) = (st.doc.id.0, linked_files(&st.doc)); - if files.is_empty() { + let (doc, files) = (st.doc.id.0, resolved_files(st)); + // Nothing to look at: no links, or none that is followed. + if files.iter().all(|(_, file, _)| file.is_none()) { return; } let slot: LinkScan = Arc::default(); let out = slot.clone(); let spawned = std::thread::Builder::new().name("link-watch".into()).spawn(move || { - let stamps = files.into_iter().map(|(p, ids)| (p.clone(), ids, stamp(&p))).collect(); + let stamps = files.into_iter().map(|(p, file, ids)| (p, ids, stamp(file.as_deref()))).collect(); *out.lock().unwrap_or_else(PoisonError::into_inner) = Some((doc, stamps)); }); if spawned.is_ok() { @@ -103,6 +122,12 @@ impl Session { if self.active().map(|d| d.doc.id.0) != Some(doc) { return Ok(json!({"updated": []})); } + // A background job holds the document: refreshing now would replace the snapshot it works + // on and its result would be discarded. The stamps stay as they were, so the change is + // seen again by the first look after the job ends. + if self.active_job().is_some() { + return Ok(json!({"updated": []})); + } // This document's last stamps; those of closed documents are dropped. let open: HashSet = self.documents().iter().map(|d| d.doc.id.0).collect(); let mut before: HashMap = HashMap::new(); @@ -155,7 +180,7 @@ mod tests { use photocraft_geom::Rect; use serde_json::json; - use crate::Session; + use crate::{EngineError, Session}; fn dir(name: &str) -> std::path::PathBuf { let d = std::env::temp_dir().join(format!("photocraft-link-watch-{name}-{}", std::process::id())); @@ -280,4 +305,53 @@ mod tests { assert!(is(centre(&s), BLUE)); assert!(s.link_scan.is_none()); } + + #[test] + fn a_refresh_waits_for_a_running_job_on_the_document() { + let d = dir("job"); + let path = d.join("art.png"); + std::fs::write(&path, png(10, 10, "#ff0000")).unwrap(); + let (mut s, id) = placed(&path); + s.execute("layer.smartObjects.updateChanged", json!({})).unwrap(); + let (release, job) = gated_job(&mut s); + save(&path, &png(10, 10, "#0000ff")); + let snapshot = s.active().unwrap().doc.clone(); + // The path the desktop app's timer takes: it doesn't go through the command conflict check. + assert_eq!(super::update_changed(&mut s, &json!({})).unwrap(), json!({"updated": []})); + assert!(std::sync::Arc::ptr_eq(&s.active().unwrap().doc, &snapshot), "the job's snapshot must stay in place"); + release.send(()).unwrap(); + s.wait_job(job).unwrap(); + // The job finished; the change that came meanwhile is picked up exactly once. + assert_eq!(s.execute("layer.smartObjects.updateChanged", json!({})).unwrap(), json!({"updated": [id]})); + assert!(is(centre(&s), BLUE)); + assert_eq!(s.execute("layer.smartObjects.updateChanged", json!({})).unwrap(), json!({"updated": []})); + } + + #[test] + fn a_job_on_another_document_does_not_hold_up_a_refresh() { + let d = dir("job-other"); + let path = d.join("art.png"); + std::fs::write(&path, png(10, 10, "#ff0000")).unwrap(); + let (mut s, id) = placed(&path); + s.execute("layer.smartObjects.updateChanged", json!({})).unwrap(); + s.execute("file.new", json!({"width": 10, "height": 10})).unwrap(); + let (release, job) = gated_job(&mut s); + s.set_active(0); + save(&path, &png(10, 10, "#0000ff")); + assert_eq!(super::update_changed(&mut s, &json!({})).unwrap(), json!({"updated": [id]})); + assert!(is(centre(&s), BLUE)); + release.send(()).unwrap(); + s.wait_job(job).unwrap(); + } + + /// A background job that locks the active document until something is sent on the returned + /// sender. + fn gated_job(s: &mut Session) -> (std::sync::mpsc::Sender<()>, crate::jobs::JobId) { + let (release, gate) = std::sync::mpsc::channel::<()>(); + let r = s.start_job("test.gated", json!({}), "Gated job", true, move |_| gate.recv().map_err(|_| EngineError::Cancelled), |_, ()| Ok(json!(null))); + match r.unwrap() { + crate::jobs::Started::Job(j) => (release, j), + other => panic!("expected a background job, got {other:?}"), + } + } } diff --git a/crates/engine/src/new_layer.rs b/crates/engine/src/new_layer.rs index cb1b151aba..b102907ccc 100644 --- a/crates/engine/src/new_layer.rs +++ b/crates/engine/src/new_layer.rs @@ -35,10 +35,12 @@ pub(crate) struct NewLayer { name: Option, } -/// `layer.new.layer`: a pixel layer above the active one (below it with `position: below`, the -/// Layers panel's ⌘/Ctrl-click), joining the clip group it lands in. +/// `layer.new.layer`: a pixel layer where Photoshop puts a new one ([`crate::layer_place`]: above +/// the active layer, inside it when it is an open group, below it with `position: below`, the Layers +/// panel's ⌘/Ctrl-click), joining the clip group it lands in. pub fn run(s: &mut Session, p: &Value) -> Result { let NewLayer { clip, mode, opacity, label, neutral, below, name } = parse(p)?; + let place = if below { crate::layer_place::Place::Below } else { crate::layer_place::Place::Auto }; let id = s.edit("New Layer", |doc, active| { let name = name.clone().unwrap_or_else(|| doc.next_layer_name("Layer")); let mut layer = Layer::raster(name, doc.pixel_format()); @@ -51,16 +53,11 @@ pub fn run(s: &mut Session, p: &Value) -> Result { let px = photocraft_raster::from_rgba(&surface.format(), [rgb[0], rgb[1], rgb[2], 1.0]); surface.fill_rect(bounds, &px); } - // Nothing may sit under the Background: below it the layer lands above it instead. - let id = match *active { - Some(a) if below && doc.layer(a).is_some() && !doc.is_background(a) => doc.insert_below(Some(a), layer), - _ => doc.insert_above(*active, layer), - }; + let id = crate::layer_place::insert_new(doc, *active, layer, place); crate::clip_group::settle(doc, &[id], &Default::default(), clip); *active = Some(id); Ok(id) })?; - crate::layer_multi_cmds::note_inert_insert(s, id); Ok(json!({ "layer": id.0 })) } diff --git a/crates/engine/src/paint_cmds.rs b/crates/engine/src/paint_cmds.rs deleted file mode 100644 index 497d7f4fb1..0000000000 --- a/crates/engine/src/paint_cmds.rs +++ /dev/null @@ -1,489 +0,0 @@ -//! Paint helpers: Paint Bucket and Gradient tool. - -use photocraft_algo::paint::{GradientShape, bucket_fill_inks, bucket_fill_region_inks, bucket_fill_region_src, bucket_fill_src, paint_gradient}; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, blend_from_str}; -use crate::presets::gradients; -use crate::{EngineError, Result, Session}; - -fn f(p: &Value, k: &str, d: f32) -> f32 { - p.get(k).and_then(Value::as_f64).map_or(d, |v| v as f32) -} -fn b(p: &Value, k: &str, d: bool) -> bool { - p.get(k).and_then(Value::as_bool).unwrap_or(d) -} -fn color(v: Option<&Value>, d: [f32; 4]) -> [f32; 4] { - match v { - Some(Value::Array(a)) if a.len() >= 3 => { - let c: Vec = a.iter().map(|x| x.as_f64().unwrap_or(0.0) as f32).collect(); - [c[0], c[1], c[2], c.get(3).copied().unwrap_or(1.0)] - } - Some(Value::String(h)) => { - let h = h.trim_start_matches('#'); - let c = |i: usize| h.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map(|v| f32::from(v) / 255.0); - match (c(0), c(2), c(4)) { - (Some(r), Some(g), Some(b)) => [r, g, b, c(6).unwrap_or(1.0)], - _ => d, - } - } - _ => d, - } -} -fn point(p: &Value, k: &str) -> Option<(f32, f32)> { - let a = p.get(k)?.as_array()?; - Some((a.first()?.as_f64()? as f32, a.get(1)?.as_f64()? as f32)) -} - -/// `opacity` (1..100 %) as a coverage in 0.01..=1, clamped like the Magic Eraser's: a value -/// above 1 would overshoot the blend (#4167). -fn opacity(p: &Value, cmd: &str) -> Result { - Ok(crate::eraser_cmds::number(p, cmd, "opacity", 100.0, 1.0..=100.0)? as f32 / 100.0) -} - -fn bucket(s: &mut Session, p: &Value) -> Result { - let fg = s.tools.foreground; - let c = color(p.get("color"), fg); - let (x, y) = (f(p, "x", 0.0).floor() as i32, f(p, "y", 0.0).floor() as i32); - let (tol, contiguous, aa) = (f(p, "tolerance", 32.0), b(p, "contiguous", true), b(p, "antiAlias", true)); - let opacity = opacity(p, "paint.bucket")?; - let all = b(p, "sampleAllLayers", false) && crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Pixels; - // Other visible layers can define the region, but the fill still writes only to the target. - let region = if all { - let (area, img) = crate::selection_cmds::sample_rgba8(s, true)?; - let region = photocraft_algo::selection::wand_region(&img, area, (x, y), tol, contiguous, aa); - drop(img); - region - } else { - None - }; - // Fill source: foreground colour (default) or a pattern (the Paint Bucket "Fill" dropdown). - let source = p.get("contents").or_else(|| p.get("source")).and_then(Value::as_str).unwrap_or("foreground"); - let pattern = if source == "pattern" { - let pat = crate::pattern_cmds::resolve_param(s, "paint.bucket", p)?; - let tile = photocraft_compose::pattern::Tile::new(&pat) - .ok_or_else(|| EngineError::BadParams { cmd: "paint.bucket".into(), msg: "the pattern is empty or too large to paint".into() })?; - let (scale, angle, _, phase) = crate::pattern_cmds::placement(p); - Some((tile, scale, angle, phase)) - } else { - None - }; - // A foreground picked as CMYK fills a CMYK document with the picked inks (#4196). - let inks = s.tools.picked_cmyk([c[0], c[1], c[2]]).filter(|_| c[3] >= 1.0 && pattern.is_none()); - let filled = s.edit("Paint Bucket", |doc, active| { - let area = doc.bounds(); - let sel = doc.selection.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(doc, *active, p)?; - let ok = if all && region.is_none() { - false - } else if let Some((tile, scale, angle, phase)) = &pattern { - // Render the pattern over the canvas once, then sample it at each filled pixel. - let place = photocraft_compose::pattern::Placement::new(photocraft_geom::Rect::EMPTY, false, *phase, *scale, *angle); - let rendered = photocraft_compose::pattern::render(tile, &place, area); - let w = area.width() as usize; - let src = |px: i32, py: i32| rendered[(py - area.y0) as usize * w + (px - area.x0) as usize]; - if let Some(region) = ®ion { - bucket_fill_region_src(surf, region, opacity, sel.as_ref(), lock, src); - true - } else { - bucket_fill_src(surf, area, (x, y), tol, contiguous, aa, opacity, sel.as_ref(), lock, src) - } - } else if let Some(region) = ®ion { - bucket_fill_region_inks(surf, region, opacity, sel.as_ref(), lock, |_, _| c, inks); - true - } else { - bucket_fill_inks(surf, area, (x, y), tol, contiguous, aa, opacity, sel.as_ref(), lock, |_, _| c, inks) - }; - surf.prune(); - Ok(ok) - })?; - Ok(json!({ "filled": filled })) -} - -fn gradient(s: &mut Session, p: &Value) -> Result { - let from = point(p, "from").ok_or_else(|| EngineError::BadParams { cmd: "paint.gradient".into(), msg: "missing from".into() })?; - let to = point(p, "to").ok_or_else(|| EngineError::BadParams { cmd: "paint.gradient".into(), msg: "missing to".into() })?; - let shape = match p.get("style").and_then(Value::as_str).unwrap_or("linear") { - "radial" => GradientShape::Radial, - "angle" => GradientShape::Angle, - "reflected" => GradientShape::Reflected, - "diamond" => GradientShape::Diamond, - _ => GradientShape::Linear, - }; - let (fg, bg) = (s.tools.foreground, s.tools.background); - // A preset (`gradient`), explicit `stops`, or the current gradient (Gradients panel); the - // legacy `colors` list spaces its colours evenly. Explicit `transparency` applies to all. - let stops: Vec<(f32, [f32; 4])> = match gradients::tool_stops(s, p)? { - Some(st) => st, - None => { - let colors: Vec<[f32; 4]> = - p.get("colors").and_then(Value::as_array).map(|a| a.iter().map(|v| color(Some(v), fg)).collect()).unwrap_or_else(|| vec![fg, bg]); - let n = colors.len(); - let cs = colors.into_iter().enumerate().map(|(i, c)| (if n > 1 { i as f32 / (n - 1) as f32 } else { 0.0 }, c)).collect(); - gradients::apply_opacity(cs, &gradients::transparency_param(p, "paint.gradient")?.unwrap_or_default()) - } - }; - // Photoshop's interpolation (`method`, with `smoothness` 0..=100, default 100) baked into dense - // stops, as for Gradient Overlay: Photoshop 27.11's Classic black → white is 0.037 at t = 1/16 - // where straight stops give 0.0625. - let stops = match p.get("method").and_then(Value::as_str).and_then(photocraft_io::GradientMethod::from_name) { - Some(m) if stops.len() >= 2 => { - let colors = stops.iter().map(|(t, c)| (*t, photocraft_color::Color::rgba(c[0], c[1], c[2], c[3]))).collect(); - let mids = vec![0.5; stops.len() - 1]; - photocraft_io::bake_gradient(colors, &mids, (f(p, "smoothness", 100.0) / 100.0).clamp(0.0, 1.0), m) - .into_iter() - .map(|(t, c)| { - let rgb = c.to_rgb(); - (t, [rgb[0], rgb[1], rgb[2], c.alpha]) - }) - .collect() - } - _ => stops, - }; - let reverse = b(p, "reverse", false); - let opacity = opacity(p, "paint.gradient")?; - let blend = p.get("mode").and_then(Value::as_str).and_then(blend_from_str).unwrap_or(photocraft_color::BlendMode::Normal); - let dither = b(p, "dither", true); // Photoshop dithers gradients by default (reduces banding). - s.edit("Gradient", |doc, active| { - let sel = doc.selection.clone(); - let area = sel.as_ref().map(|m| m.content_bounds()).filter(|r| !r.is_empty()).unwrap_or_else(|| doc.bounds()).intersect(&doc.bounds()); - let (surf, lock) = crate::channel_cmds::target_surface(doc, *active, p)?; - paint_gradient(surf, area, from, to, shape, &stops, reverse, opacity, blend, dither, sel.as_ref(), lock); - Ok(()) - })?; - Ok(Value::Null) -} - -/// Paint helper command specs. -pub fn specs() -> Vec { - vec![ - CommandSpec { - id: "paint.bucket", - label: "Paint Bucket", - menu: &[], - shortcut: None, - params: r##"{"x":px,"y":px,"tolerance":0..255=32,"contiguous":bool=true,"antiAlias":bool=true,"sampleAllLayers":bool=false,"contents":"foreground|pattern"="foreground","color":"#rrggbb"=foreground,"pattern":id|name (contents=pattern),"scale":%=100,"angle":deg,"opacity":1..100=100,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##, - enabled: crate::commands::has_paintable, - run: bucket, - journal: true, - }, - CommandSpec { - id: "paint.gradient", - label: "Gradient", - menu: &[], - shortcut: None, - params: r##"{"from":[x,y],"to":[x,y],"style":"linear|radial|angle|reflected|diamond"="linear","colors":["#rrggbb",…]? (evenly spaced),"gradient":preset name?,"stops":[[t,"#rrggbb"|"foreground"|"background"],…]?,"transparency":[[t,0..100],…]? (default: the current gradient, see gradient.presets.select),"method":"classic|perceptual|linear|smooth"? (Photoshop's interpolation; omitted: straight stops),"smoothness":0..100=100,"reverse":bool=false,"dither":bool=true,"opacity":1..100=100,"mode":"normal|multiply|…"="normal","target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target}"##, - enabled: crate::commands::has_paintable, - run: gradient, - journal: true, - }, - ] -} - -#[cfg(test)] -mod tests { - use super::*; - use photocraft_geom::Rect; - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 10})).unwrap(); - s - } - - fn px(s: &Session, x: i32, y: i32) -> Vec { - let d = s.active().unwrap(); - d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().pixel(x, y) - } - - /// A visible wall on the Background with an empty, active layer above it. - fn bucket_layers(depth: u32) -> (Session, photocraft_doc::LayerId) { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 10, "depth": depth, "background": "white"})).unwrap(); - let bottom = s.active().unwrap().active_layer.unwrap(); - s.edit("wall", |doc, _| { - doc.layer_mut(bottom).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 0, 11, 10), &[0.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - (s, bottom) - } - - #[test] - fn bucket_sample_all_layers_uses_visible_boundaries_at_all_depths() { - for depth in [8, 16, 32] { - for all in [None, Some(false), Some(true)] { - let (mut s, bottom) = bucket_layers(depth); - let before = s.active().unwrap().doc.layer(bottom).unwrap().clone(); - let mut p = json!({"x": 2, "y": 2, "color": "#ff0000", "antiAlias": false, "tolerance": 0}); - if let Some(all) = all { - p["sampleAllLayers"] = json!(all); - } - assert_eq!(s.execute("paint.bucket", p.clone()).unwrap()["filled"], true); - assert_eq!(px(&s, 5, 5), vec![1.0, 0.0, 0.0, 1.0], "depth {depth}, all {all:?}"); - let beyond = if all == Some(true) { vec![0.0; 4] } else { vec![1.0, 0.0, 0.0, 1.0] }; - assert_eq!(px(&s, 15, 5), beyond, "depth {depth}, all {all:?}: sample flag controls the boundary"); - assert_eq!(s.active().unwrap().doc.layer(bottom).unwrap(), &before, "only the active layer is written"); - if all == Some(true) { - assert_eq!(px(&s, 10, 5), vec![0.0; 4], "wall itself is untouched"); - assert!(s.undo()); - p["contiguous"] = json!(false); - s.execute("paint.bucket", p).unwrap(); - assert_eq!(px(&s, 15, 5), vec![1.0, 0.0, 0.0, 1.0], "noncontiguous finds both white regions"); - assert_eq!(px(&s, 10, 5), vec![0.0; 4]); - assert!(s.undo()); - assert_eq!(s.execute("paint.bucket", json!({"x": -1, "y": 2, "sampleAllLayers": true})).unwrap()["filled"], false); - assert_eq!(px(&s, 5, 5), vec![0.0; 4], "an off-canvas sample does not fill the layer"); - } - } - } - } - - #[test] - fn bucket_sample_all_layers_ignores_hidden_layers() { - let (mut s, _) = bucket_layers(8); - s.edit("hidden wall", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - l.surface_mut().unwrap().fill_rect(Rect::new(4, 0, 5, 10), &[0.0, 0.0, 0.0, 1.0]); - l.visible = false; - Ok(()) - }) - .unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("paint.bucket", json!({"x": 2, "y": 2, "color": "#ff0000", "antiAlias": false, "tolerance": 0, "sampleAllLayers": true})).unwrap(); - assert_eq!(px(&s, 4, 5), vec![1.0, 0.0, 0.0, 1.0], "hidden wall does not bound the fill"); - assert_eq!(px(&s, 8, 5), vec![1.0, 0.0, 0.0, 1.0]); - assert_eq!(px(&s, 15, 5), vec![0.0; 4], "visible wall still bounds the fill"); - } - - #[test] - fn bucket_sample_all_layers_keeps_selection_opacity_and_history() { - for depth in [8, 16, 32] { - let (mut s, _) = bucket_layers(depth); - s.execute("select.rect", json!({"x": 0, "y": 2, "width": 20, "height": 6})).unwrap(); - s.edit("feather", |doc, _| { - doc.selection.as_mut().unwrap().fill_rect(Rect::new(0, 2, 4, 8), &[0.5]); - Ok(()) - }) - .unwrap(); - let selection = s.active().unwrap().doc.selection.clone(); - s.execute("paint.bucket", json!({"x": 2, "y": 5, "color": "#ff0000", "antiAlias": false, "tolerance": 0, "opacity": 50, "sampleAllLayers": true})) - .unwrap(); - let (feathered, full) = (px(&s, 2, 5), px(&s, 6, 5)); - assert!((feathered[3] - 0.25).abs() < 0.005 && (full[3] - 0.5).abs() < 0.005, "depth {depth}: selection coverage multiplies opacity"); - assert_eq!(full[..3], [1.0, 0.0, 0.0]); - assert_eq!(px(&s, 6, 1), vec![0.0; 4], "outside selection"); - assert_eq!(px(&s, 15, 5), vec![0.0; 4], "outside sampled region"); - assert_eq!(s.active().unwrap().history.undo_label(), Some("Paint Bucket")); - assert!(s.undo()); - assert_eq!(px(&s, 2, 5), vec![0.0; 4]); - assert_eq!(s.active().unwrap().doc.selection, selection); - assert!(s.redo()); - assert_eq!(px(&s, 2, 5), feathered); - assert_eq!(px(&s, 6, 5), full); - assert_eq!(s.active().unwrap().doc.selection, selection); - } - } - - #[test] - fn bucket_sample_all_layers_pattern_uses_the_same_region() { - let (mut s, _) = bucket_layers(8); - s.execute( - "paint.bucket", - json!({"x": 2, "y": 2, "contents": "pattern", "pattern": "Diagonal Lines", "antiAlias": false, "tolerance": 0, "sampleAllLayers": true}), - ) - .unwrap(); - let left: Vec = (0..10).map(|x| px(&s, x, 0)[0]).collect(); - assert!(left.iter().any(|&r| r < 0.3) && left.iter().any(|&r| r > 0.9), "pattern varies inside the region: {left:?}"); - assert_eq!(px(&s, 10, 5), vec![0.0; 4], "wall untouched"); - assert_eq!(px(&s, 15, 5), vec![0.0; 4], "region past the wall untouched"); - } - - #[test] - fn bucket_sample_all_layers_mask_and_channel_targets_keep_their_own_boundaries() { - for target in [json!("mask"), json!({"channel": 0}), json!("quickMask")] { - for all in [false, true] { - let (mut s, _) = bucket_layers(8); - match crate::channel_cmds::target_of(&json!({"target": target})) { - crate::channel_cmds::Target::Mask => { - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - } - crate::channel_cmds::Target::Alpha(_) => { - s.execute("channel.new", json!({"fill": "white"})).unwrap(); - } - crate::channel_cmds::Target::QuickMask => { - s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); - } - _ => panic!("expected a grayscale target"), - } - let p = json!({"x": 2, "y": 2, "color": "#000000", "antiAlias": false, "tolerance": 0, "target": target, "sampleAllLayers": all}); - s.edit("target wall", |doc, active| { - let area = doc.bounds(); - let (surf, _) = crate::channel_cmds::target_surface(doc, *active, &p).unwrap(); - surf.fill_rect(area, &[1.0]); - surf.fill_rect(Rect::new(5, 0, 6, 10), &[0.0]); - Ok(()) - }) - .unwrap(); - s.execute("paint.bucket", p.clone()).unwrap(); - let d = &s.active().unwrap().doc; - let surf = match crate::channel_cmds::target_of(&p) { - crate::channel_cmds::Target::Mask => &d.layer(s.active().unwrap().active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface, - crate::channel_cmds::Target::Alpha(i) => &d.channels[i].surface, - crate::channel_cmds::Target::QuickMask => &d.quick_mask.as_ref().unwrap().surface, - _ => panic!("expected a grayscale target"), - }; - assert_eq!(surf.sample_channel(2, 5, 0), 0.0, "{target}, all {all}: filled target"); - assert_eq!(surf.sample_channel(7, 5, 0), 1.0, "{target}, all {all}: stopped at target wall"); - assert_eq!(px(&s, 2, 5), vec![0.0; 4], "layer pixels remain untouched"); - } - } - } - - #[test] - fn bucket_fill_contiguous() { - let mut s = session(); - s.edit("wall", |doc, _| { - doc.layers[0].surface_mut().unwrap().fill_rect(Rect::new(10, 0, 11, 10), &[0.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("paint.bucket", json!({"x": 2, "y": 2, "color": "#ff0000", "antiAlias": false})).unwrap(); - assert_eq!(px(&s, 5, 5), vec![1.0, 0.0, 0.0, 1.0]); - assert_eq!(px(&s, 15, 5), vec![1.0, 1.0, 1.0, 1.0]); - s.execute("paint.bucket", json!({"x": 2, "y": 2, "color": "#0000ff", "contiguous": false, "antiAlias": false, "tolerance": 0})).unwrap(); - assert_eq!(px(&s, 5, 5), vec![0.0, 0.0, 1.0, 1.0]); - } - - /// #4196: the Paint Bucket and Edit > Stroke write a CMYK pick as its inks into a CMYK document, - /// not as its sRGB stand-in converted back (100/0/0/0 cyan would come out as about 100/28/0/17). - #[test] - fn bucket_and_stroke_write_a_cmyk_pick_as_its_inks() { - let cyan = [1.0f32, 0.0, 0.0, 0.0]; - let mut s = session(); - s.execute("image.mode.cmyk", json!({})).unwrap(); - let rgb = photocraft_color::convert::cmyk_to_rgb(cyan).map(|c| c.clamp(0.0, 1.0)); - s.execute("tools.setColors", json!({"foreground": rgb, "foregroundCmyk": cyan})).unwrap(); - let inks_at = |s: &Session, x: i32, y: i32| -> Vec { - let st = s.active().unwrap(); - let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - surf.read_region(Rect::new(x, y, x + 1, y + 1)).into_iter().take(4).collect() - }; - let is_cyan = |got: &[f32]| got.iter().zip(cyan).all(|(a, b)| (a - b).abs() < 0.02); - s.execute("paint.bucket", json!({"x": 2, "y": 2, "antiAlias": false})).unwrap(); - let got = inks_at(&s, 5, 5); - assert!(is_cyan(&got), "bucket: {got:?}"); - s.execute("select.all", json!({})).unwrap(); - s.execute("edit.stroke", json!({"width": 2, "location": "inside", "color": rgb})).unwrap(); - let got = inks_at(&s, 0, 5); - assert!(is_cyan(&got), "stroke: {got:?}"); - } - - #[test] - fn bucket_fills_with_a_pattern() { - // Paint Bucket with contents="pattern" flood-fills the region with a pattern, not a colour. - let mut s = session(); - s.edit("wall", |doc, _| { - doc.layers[0].surface_mut().unwrap().fill_rect(Rect::new(10, 0, 11, 10), &[0.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("paint.bucket", json!({"x": 2, "y": 2, "contents": "pattern", "pattern": "Diagonal Lines", "antiAlias": false, "tolerance": 0})).unwrap(); - // The filled region (left of the wall) now carries the pattern's light/dark variation, - // while the wall and the region past it are untouched. - let left: Vec<[f32; 4]> = (0..10) - .map(|x| { - let p = px(&s, x, 0); - [p[0], p[1], p[2], p[3]] - }) - .collect(); - assert!(left.iter().any(|p| p[0] < 0.3) && left.iter().any(|p| p[0] > 0.9), "pattern varies in the fill: {left:?}"); - assert_eq!(px(&s, 15, 5), vec![1.0, 1.0, 1.0, 1.0], "region past the wall untouched"); - } - - #[test] - fn gradient_tool_with_selection_and_reverse() { - let mut s = session(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [20, 0], "colors": ["#000000", "#ffffff"]})).unwrap(); - assert!(px(&s, 0, 5)[0] < 0.05 && px(&s, 19, 5)[0] > 0.95); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [20, 0], "colors": ["#000000", "#ffffff"], "reverse": true})).unwrap(); - assert!(px(&s, 0, 5)[0] > 0.95); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 5, "height": 10})).unwrap(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [0, 10], "colors": ["#ff0000"]})).unwrap(); - assert_eq!(px(&s, 2, 5)[..3], [1.0, 0.0, 0.0]); - assert!(px(&s, 10, 5)[1] > 0.4, "outside the selection untouched"); - assert!(s.execute("paint.gradient", json!({"from": [0, 0]})).is_err()); - } - - #[test] - fn gradient_dither_breaks_banding() { - // A flat gradient (same colour both ends) is perfectly smooth with dither off, and gains - // per-pixel noise with dither on (which breaks 8-bit banding). - let run = |dither: bool| { - let mut s = session(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [19, 0], "colors": [[0.5, 0.5, 0.5, 1.0], [0.5, 0.5, 0.5, 1.0]], "dither": dither})) - .unwrap(); - (0..19).map(|x| px(&s, x, 5)[0]).collect::>() - }; - let smooth = run(false); - assert!(smooth.iter().all(|&v| v == smooth[0]), "no dither: perfectly flat"); - let noisy = run(true); - assert!(noisy.windows(2).any(|w| w[0] != w[1]), "dither: per-pixel variation"); - } - - /// The two commands under test, each painting #808080 over a white document. - fn grey_fill(cmd: &str, opacity: Value) -> Value { - match cmd { - "paint.bucket" => json!({"x": 2, "y": 2, "color": "#808080", "opacity": opacity}), - _ => json!({"from": [0, 0], "to": [19, 0], "colors": ["#808080", "#808080"], "dither": false, "opacity": opacity}), - } - } - - fn white(depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 10, "depth": depth, "background": "white"})).unwrap(); - s - } - - /// Issue #4167: an opacity above 100 overshot the blend (200 painted near-black on white). - #[test] - fn bucket_and_gradient_clamp_opacity_like_the_magic_eraser() { - for cmd in ["paint.bucket", "paint.gradient"] { - for depth in [8, 16, 32] { - let mut full = white(depth); - full.execute(cmd, grey_fill(cmd, json!(100))).unwrap(); - let expected = px(&full, 5, 5); - assert!((expected[0] - 128.0 / 255.0).abs() < 0.01, "{cmd} depth {depth}: 100% paints #808080, got {expected:?}"); - for over in [200.0, 1e9] { - let mut s = white(depth); - s.execute(cmd, grey_fill(cmd, json!(over))).unwrap(); - assert_eq!(px(&s, 5, 5), expected, "{cmd} depth {depth}: opacity {over} clamps to 100"); - } - let (mut low, mut one) = (white(depth), white(depth)); - low.execute(cmd, grey_fill(cmd, json!(-50))).unwrap(); - one.execute(cmd, grey_fill(cmd, json!(1))).unwrap(); - assert_eq!(px(&low, 5, 5), px(&one, 5, 5), "{cmd} depth {depth}: opacity below 1 clamps to 1"); - assert!(px(&low, 5, 5).iter().all(|v| (0.0..=1.0).contains(v)), "{cmd} depth {depth}: no overshoot"); - } - } - } - - #[test] - fn bucket_and_gradient_reject_non_numeric_opacity_without_editing() { - for cmd in ["paint.bucket", "paint.gradient"] { - for bad in [json!("50"), json!(true), json!([50]), json!({"v": 50})] { - let mut s = white(8); - let (before, steps) = (px(&s, 5, 5), s.active().unwrap().history.past_len()); - let err = s.execute(cmd, grey_fill(cmd, bad.clone())).unwrap_err(); - assert!(matches!(err, EngineError::BadParams { .. }), "{cmd} {bad}: {err:?}"); - assert_eq!(px(&s, 5, 5), before, "{cmd} {bad}: pixels unchanged"); - assert_eq!(s.active().unwrap().history.past_len(), steps, "{cmd} {bad}: no history step"); - } - // An explicit null keeps the default (100%), like the Magic Eraser. - let mut s = white(8); - s.execute(cmd, grey_fill(cmd, Value::Null)).unwrap(); - assert!((px(&s, 5, 5)[0] - 128.0 / 255.0).abs() < 0.01, "{cmd}: null opacity is 100%"); - } - } -} diff --git a/crates/engine/src/pixels.rs b/crates/engine/src/pixels.rs index b046de03bd..71d7a2060a 100644 --- a/crates/engine/src/pixels.rs +++ b/crates/engine/src/pixels.rs @@ -58,20 +58,55 @@ pub fn composite_layers_uncropped(solo: &Document, format: photocraft_color::Pix photocraft_compose::composite_to_surface(solo, area, format, None) } +/// Most bytes of tiles an adjustment may materialize beyond the ones a surface stores: two distant +/// tiles make the content bounds a grid of millions of tiles that hold nothing. Written tiles are +/// copies, so the peak is about twice this. +const MAX_UNSTORED_BYTES: u64 = 1 << 30; + +/// Tiles in the grid that covers `r` (one extra per axis for a tile straddling its edge). +fn grid_tiles(r: Rect) -> u64 { + (u64::from(r.width()).div_ceil(TILE_SIZE as u64) + 1) * (u64::from(r.height()).div_ceil(TILE_SIZE as u64) + 1) +} + +/// Bytes the tiles of the grid over `r` take beyond the `stored` ones, in format `fmt`. +fn unstored_bytes(r: Rect, stored: u64, fmt: &photocraft_color::PixelFormat) -> u64 { + grid_tiles(r).saturating_sub(stored).saturating_mul((TILE_SIZE as u64).pow(2)).saturating_mul(fmt.bytes_per_pixel() as u64) +} + +/// The tile-sized pieces of `r`, each cut to `r`. +fn tile_rects(r: Rect) -> impl Iterator { + let t = i64::from(TILE_SIZE); + let cols = i64::from(r.x0).div_euclid(t)..=(i64::from(r.x1) - 1).div_euclid(t); + let rows = i64::from(r.y0).div_euclid(t)..=(i64::from(r.y1) - 1).div_euclid(t); + rows.flat_map(move |cy| cols.clone().map(move |cx| Rect::new((cx * t) as i32, (cy * t) as i32, ((cx + 1) * t) as i32, ((cy + 1) * t) as i32).intersect(&r))) +} + +const TOO_SPARSE: &str = "the adjustment would have to fill too large an area around the pixels the layer holds"; + /// Apply an adjustment destructively to a surface, weighted by an optional selection. /// Applies `adj` to a surface (any colour model and depth, via straight RGBA) through the /// selection; `mode` is the document's, for the tone transfer (e.g. Exposure in Grayscale). /// Works tile by tile in parallel, so a large layer needs one tile's buffers per thread rather /// than four full-layer copies, and the result is the same as one pass over the whole region. -pub fn adjust_surface(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface>, mode: photocraft_color::ColorMode) { +pub fn adjust_surface(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface>, mode: photocraft_color::ColorMode) -> Result<(), String> { use rayon::prelude::*; let r = s.content_bounds(); if r.is_empty() { - return; + return Ok(()); } let fmt = s.format(); - let done: Vec<_> = s - .take_tiles(r) + // Two distant tiles make the content bounds a grid of millions of tiles that hold nothing. + // Past a budget a transparent default needs only the stored tiles adjusted (the gaps stay + // transparent); an opaque default would change under the adjustment everywhere in that grid, + // so the adjustment is refused rather than left half applied. + let stored: Vec<_> = s.tiles().map(|(c, _)| *c).filter(|c| !c.rect().intersect(&r).is_empty()).collect(); + let transparent = fmt.alpha && s.default_pixel().last().is_some_and(|a| *a <= 0.0); + let over = unstored_bytes(r, stored.len() as u64, &fmt) > MAX_UNSTORED_BYTES; + if over && !transparent { + return Err(TOO_SPARSE.into()); + } + let taken = if over { stored.into_iter().flat_map(|c| s.take_tiles(c.rect())).collect() } else { s.take_tiles(r) }; + let done: Vec<_> = taken .into_par_iter() .flat_map_iter(|(tc, tile)| { // A one-tile surface, so `adjusted` reads and writes this tile only. @@ -84,38 +119,57 @@ pub fn adjust_surface(s: &mut Surface, adj: &Adjustment, selection: Option<&Surf }) .collect(); s.put_tiles(done); + Ok(()) } #[cfg(test)] -#[path = "flatten_cmyk_tests.rs"] mod flatten_cmyk_tests; #[cfg(test)] -#[path = "pixels_tests.rs"] mod tests; /// Applies `adj` to a layer mask through the selection. Untouched mask pixels count (they read as /// the mask's default value), and without a selection the default changes too, since a mask /// reaches past the canvas: Invert turns a reveal-all mask into a hide-all one (#780). -pub fn adjust_mask(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface>) { +pub fn adjust_mask(s: &mut Surface, adj: &Adjustment, selection: Option<&Surface>) -> Result<(), String> { let mode = photocraft_color::ColorMode::Grayscale; if let Some(sel) = selection { let r = sel.content_bounds(); - if !r.is_empty() { - let out = adjusted(s, r, adj, Some(sel), mode); - s.write_region(r, &out); + // Tile by tile, and only where the selection stores something (it is unselected, so + // unchanged, elsewhere): its content bounds can span a grid of mostly nothing. + let sparse = sel.default_pixel().iter().all(|v| *v == 0.0); + let tiles: Vec = if sparse { + sel.tiles().map(|(c, _)| c.rect().intersect(&r)).filter(|t| !t.is_empty()).collect() + } else if r.is_empty() { + Vec::new() + } else { + // Unselected pixels count too, so every tile of the span is adjusted, one at a time + // (not one read of the whole span), and the span is bounded by what that writes. + if unstored_bytes(r, 0, &s.format()) > MAX_UNSTORED_BYTES { + return Err(TOO_SPARSE.into()); + } + tile_rects(r).collect() + }; + for t in &tiles { + let out = adjusted(s, *t, adj, Some(sel), mode); + s.write_region(*t, &out); + } + if !tiles.is_empty() { s.prune(); } - return; + return Ok(()); } let fmt = s.format(); let unit = Rect::from_xywh(0, 0, 1, 1); let mut out = Surface::with_default(fmt, &adjusted(&Surface::with_default(fmt, &s.default_pixel()), unit, adj, None, mode)); - let r = s.tile_bounds(); - if !r.is_empty() { + // Everything the mask doesn't store reads as its default, which `out` already holds adjusted. + // So only the stored tiles are adjusted, not the (possibly huge) rectangle around them. + let stored: Vec = s.tiles().map(|(c, _)| c.rect()).collect(); + for r in stored { out.write_region(r, &adjusted(s, r, adj, None, mode)); } out.prune(); *s = out; + Ok(()) } /// `s`'s pixels over `r` (untouched ones included) with `adj` applied through the selection, encoded. @@ -223,7 +277,9 @@ pub(crate) fn mix_through_selection( sel.sample_row_strided(r.y0.saturating_add(j as i32), r.x0, 1, 0, &mut coverage); } for ((a, o), k) in edited_row.iter().zip(orig_row).zip(&coverage) { - let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k); + // `o·(1−k) + a·k` keeps the edit exact at full coverage, even over huge 32-bit + // values whose difference from it would round the edit away (#9483). + let mixed: [f32; 4] = std::array::from_fn(|c| o[c] * (1.0 - k) + a[c] * k); let mut enc = [0.0f32; 8]; let m = photocraft_raster::from_rgba_into(fmt, mixed, &mut enc); out.extend_from_slice(&enc[..m]); @@ -239,16 +295,21 @@ pub(crate) fn try_fill_surface(s: &mut Surface, area: Rect, color: [f32; 4], sel let fmt = s.format(); // Coverage 1 of an opaque colour replaces the colour (and the alpha, unless it is kept). let opaque = color[3] == 1.0 && (!lock_transparency || !fmt.alpha) && selection.is_none_or(|m| m.format().sample != photocraft_color::SampleType::F32); - let solid = opaque.then(|| { - let mut enc = [0.0f32; 8]; - let n = photocraft_raster::from_rgba_into(&fmt, [color[0], color[1], color[2], 1.0], &mut enc); - s.solid_tile(enc.get(..n).unwrap_or(&[])) - }); + let mut enc = [0.0f32; 8]; + let enc_len = opaque.then(|| photocraft_raster::from_rgba_into(&fmt, [color[0], color[1], color[2], 1.0], &mut enc)); + let solid = enc_len.map(|n| s.solid_tile(enc.get(..n).unwrap_or(&[]))); let whole = |full: bool| solid.as_ref().filter(|_| full).map(|t| TileEdit::Put(t.clone())); edit_tiles(s, area, selection, "filling pixels", whole, |k, px| { if k <= 0.0 { return false; } + if k >= 1.0 + && let Some(m) = enc_len + && let Some(src) = enc.get(..px.len().min(m)) + { + px.copy_from_slice(src); + return true; + } fill_pixel(&fmt, px, color, k, lock_transparency); true }) @@ -294,9 +355,31 @@ pub(crate) fn try_clear_surface(s: &mut Surface, area: Rect, selection: Option<& px.fill(0.0); } true - })?; - s.prune(); - Ok(()) + }) +} + +/// Take the coverage a floating selection lifts out of `s` over `area`: each pixel loses its +/// selection coverage k of alpha, down to none, so a selection made from the layer's own +/// transparency leaves nothing behind (#7655), where [`try_clear_surface`] would leave a ghost. +pub(crate) fn try_lift_coverage(s: &mut Surface, area: Rect, selection: &Surface) -> crate::Result<()> { + let fmt = s.format(); + let n = fmt.channels(); + if !fmt.alpha { + return Ok(()); + } + let empty_default = s.default_bytes().iter().all(|&b| b == 0); + let whole = |full: bool| (full && empty_default).then_some(TileEdit::Drop); + edit_tiles(s, area, Some(selection), "lifting pixels", whole, |k, px| { + if k <= 0.0 { + return false; + } + let Some(a) = px.get_mut(n - 1) else { return false }; + *a = (*a - k).max(0.0); + if *a <= 0.0 { + px.fill(0.0); + } + true + }) } /// A whole-tile shortcut of [`edit_tiles`]. @@ -331,6 +414,7 @@ fn edit_tiles( coords.extend(area.tiles()); // Untouched pixels read as the default where the surface has no tile. let blank = if coords.iter().any(|&tc| s.tile(tc).is_none()) { Some(s.try_default_tile().map_err(oom)?) } else { None }; + let dp = s.default_bytes(); let surf = &*s; let edits = coords .into_par_iter() @@ -371,7 +455,11 @@ fn edit_tiles( } } } - Ok(out.map(|t| (tc, Some(Arc::new(t))))) + Ok(match out { + Some(t) if bpp > 0 && t.bytes().chunks_exact(bpp).all(|p| p == dp) => current.map(|_| (tc, None)), + Some(t) => Some((tc, Some(Arc::new(t)))), + None => None, + }) }) .collect::>>()?; for (tc, tile) in edits.into_iter().flatten() { diff --git a/crates/engine/src/flatten_cmyk_tests.rs b/crates/engine/src/pixels/flatten_cmyk_tests.rs similarity index 100% rename from crates/engine/src/flatten_cmyk_tests.rs rename to crates/engine/src/pixels/flatten_cmyk_tests.rs diff --git a/crates/engine/src/pixels_tests.rs b/crates/engine/src/pixels/tests.rs similarity index 83% rename from crates/engine/src/pixels_tests.rs rename to crates/engine/src/pixels/tests.rs index 5af30d0e76..3461956566 100644 --- a/crates/engine/src/pixels_tests.rs +++ b/crates/engine/src/pixels/tests.rs @@ -110,7 +110,7 @@ fn tiled_adjustment_matches_one_pass_over_the_content() { for sel in [None, Some(&soft)] { let want = one_pass(&s, &adj, sel, fmt.mode); let mut got = s.clone(); - adjust_surface(&mut got, &adj, sel, fmt.mode); + adjust_surface(&mut got, &adj, sel, fmt.mode).unwrap(); assert!(want != s, "{kind} {fmt:?}: the adjustment changes the layer"); assert!(got == want, "{kind} {fmt:?} selection {}: tiled result differs", sel.is_some()); } @@ -121,7 +121,7 @@ fn tiled_adjustment_matches_one_pass_over_the_content() { #[test] fn an_empty_layer_is_left_alone() { let mut s = Surface::new(PixelFormat::RGBA8); - adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Rgb); + adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Rgb).unwrap(); assert_eq!(s.tile_count(), 0); } @@ -183,19 +183,29 @@ fn tiled_clear_and_fill_match_one_pass_over_the_area() { let sr = Rect::new(0, 0, 600, 400); soft.write_region(sr, &(0..sr.height()).flat_map(|_| (0..sr.width()).map(|x| (1.0 - (x as f32 - 256.0).max(0.0) / 300.0).max(0.0))).collect::>()); let canvas = Rect::new(0, 0, 700, 600); + // Pixel formats and fill settings are independent: a run takes a quarter of their combinations, + // a different quarter each day. + let sweep = photocraft_testkit::Sweep::new(4); + let mut case = 0; + let mut runs = || { + case += 1; + sweep.includes(case) + }; for fmt in formats { let s = clear_layer(fmt); for sel in [None, Some(&soft)] { let area = sel.map_or(canvas, |m| m.content_bounds()); let what = format!("{fmt:?} selection {}", sel.is_some()); - let mut got = s.clone(); - try_clear_surface(&mut got, area, sel).unwrap(); - assert!(same_pixels(&got, &reference_fill_or_clear(&s, area, sel, None)), "clear {what}"); - if fmt.alpha { - assert!(!same_pixels(&got, &s), "clear {what} changes the layer"); + if runs() { + let mut got = s.clone(); + try_clear_surface(&mut got, area, sel).unwrap(); + assert!(same_pixels(&got, &reference_fill_or_clear(&s, area, sel, None)), "clear {what}"); + if fmt.alpha { + assert!(!same_pixels(&got, &s), "clear {what} changes the layer"); + } } for color in [[1.0, 1.0, 1.0, 1.0], [0.3, 0.5, 0.9, 1.0], [0.3, 0.5, 0.9, 0.4]] { - for lock in [true, false] { + for lock in [true, false].into_iter().filter(|_| runs()) { let mut got = s.clone(); try_fill_surface(&mut got, area, color, sel, lock).unwrap(); assert!(same_pixels(&got, &reference_fill_or_clear(&s, area, sel, Some((color, lock)))), "fill {color:?} lock {lock} {what}"); @@ -253,7 +263,7 @@ fn invert_in_a_lab_document_mirrors_l_a_and_b() { let mut s = Surface::new(fmt); let r = Rect::new(0, 0, 2, 2); s.write_region(r, &(0..4).flat_map(|_| from_rgba(&fmt, [c[0], c[1], c[2], 1.0])).collect::>()); - adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Lab); + adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Lab).unwrap(); let px = s.read_region(Rect::new(1, 1, 2, 2)); let rgba = photocraft_raster::to_rgba(&fmt, &px); let got = srgb_to_lab([rgba[0], rgba[1], rgba[2]]); @@ -282,7 +292,7 @@ fn mixing_through_a_selection_matches_sampling_it_per_pixel() { let mut expected = Vec::new(); for (i, (a, o)) in edited.iter().zip(&orig).enumerate() { let k = sel.sample_channel(r.x0 + (i % w) as i32, r.y0 + (i / w) as i32, 0); - let mixed: [f32; 4] = std::array::from_fn(|c| o[c] + (a[c] - o[c]) * k); + let mixed: [f32; 4] = std::array::from_fn(|c| o[c] * (1.0 - k) + a[c] * k); let mut enc = [0.0f32; 8]; let m = photocraft_raster::from_rgba_into(&fmt, mixed, &mut enc); expected.extend_from_slice(&enc[..m]); @@ -308,7 +318,7 @@ fn adjusted_pixel(fmt: PixelFormat, ch: &[f32], adj: &Adjustment, k: Option if let Some(k) = k { sel.write_region(unit, &[k]); } - adjust_surface(&mut s, adj, k.map(|_| &sel), fmt.mode); + adjust_surface(&mut s, adj, k.map(|_| &sel), fmt.mode).unwrap(); s.read_region(unit) } @@ -394,3 +404,63 @@ fn hue_saturation_and_color_balance_keep_a_cmyk_layers_black() { assert!((0..4).all(|c| (half[c] - (ink[c] + (want[0][c] - ink[c]) * k)).abs() < 1e-3), "{}: half a selection", adj.label()); } } + +/// Two small blocks two billion pixels apart: the content bounds are a grid of 6·10¹³ tiles +/// that holds nothing, which no adjustment may materialize. +fn distant_blocks(fmt: PixelFormat, default: f32) -> Surface { + let mut s = Surface::with_default(fmt, &vec![default; fmt.channels()]); + s.fill_rect(Rect::new(0, 0, 10, 10), &vec![0.25; fmt.channels()]); + s.fill_rect(Rect::new(2_000_000_000, 2_000_000_000, 2_000_000_010, 2_000_000_010), &vec![0.25; fmt.channels()]); + s +} + +#[test] +fn adjusting_a_layer_of_distant_blocks_visits_only_its_tiles() { + let mut s = distant_blocks(PixelFormat::RGBA8, 0.0); + adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Rgb).unwrap(); + let p = s.read_region(Rect::new(2_000_000_005, 2_000_000_005, 2_000_000_006, 2_000_000_006)); + // Straight colour inverted, alpha untouched. + assert!((p[0] - 0.75).abs() < 0.01 && (p[3] - 0.25).abs() < 0.01, "{p:?}"); + assert_eq!(s.tile_count(), 2); +} + +#[test] +fn adjusting_a_mask_of_distant_blocks_visits_only_its_tiles() { + let mut m = distant_blocks(PixelFormat::GRAY8, 1.0); + adjust_mask(&mut m, &Adjustment::Invert, None).unwrap(); + assert!(m.default_pixel()[0] < 0.01, "the default is inverted: {:?}", m.default_pixel()); + let p = m.read_region(Rect::new(2_000_000_005, 2_000_000_005, 2_000_000_006, 2_000_000_006)); + assert!((p[0] - 0.75).abs() < 0.01, "{p:?}"); + // Through a selection of its own distant tiles, too. + let mut sel = Surface::new(PixelFormat::GRAY8); + sel.fill_rect(Rect::new(2_000_000_000, 2_000_000_000, 2_000_000_010, 2_000_000_010), &[1.0]); + adjust_mask(&mut m, &Adjustment::Invert, Some(&sel)).unwrap(); + let p = m.read_region(Rect::new(2_000_000_005, 2_000_000_005, 2_000_000_006, 2_000_000_006)); + assert!((p[0] - 0.25).abs() < 0.01, "{p:?}"); +} + +#[test] +fn an_opaque_layer_of_distant_blocks_is_refused_not_half_adjusted() { + let mut s = distant_blocks(PixelFormat::RGBA8, 1.0); + let before = s.clone(); + assert!(adjust_surface(&mut s, &Adjustment::Invert, None, ColorMode::Rgb).is_err()); + assert!(s == before, "a refused adjustment leaves the layer as it was"); +} + +#[test] +fn a_selection_with_a_nonzero_default_over_distant_blocks_is_refused() { + let mut m = Surface::with_default(PixelFormat::GRAY8, &[0.5]); + let sel = distant_blocks(PixelFormat::GRAY8, 1.0); + assert!(adjust_mask(&mut m, &Adjustment::Invert, Some(&sel)).is_err()); +} + +/// #9483: a destructive edit with full coverage writes its result exactly, even over huge 32-bit +/// values (`o + (a − o)·k` gave 0 for a 0.5 result over 1e8, and 0.125 for 0.1 over 1e6). +#[test] +fn full_coverage_keeps_the_edit_over_huge_hdr_values() { + let fmt = PixelFormat::RGBA32F; + for (o, a) in [(1.0e8_f32, 0.5_f32), (1.0e6, 0.1), (-1.0e6, 0.9)] { + let out = mix_through_selection(&fmt, Rect::new(0, 0, 1, 1), &[[o, o, o, 1.0]], &[[a, a, a, 1.0]], None); + assert_eq!(out[..3], [a, a, a], "{a} over {o}"); + } +} diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 2c2507226d..f2445a2a54 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -12,8 +12,9 @@ //! OS config directory; the web shell: browser storage) and restore it with //! [`Session::load_prefs_json`]. //! -//! Engine-side values are applied by [`Session::apply_prefs`]: the history state limit (every -//! open document), the effect-cache memory budget, and units for commands that report lengths. +//! Engine-side values are applied by [`Session::apply_prefs`]: the history state limit and +//! History Options › Allow Non-Linear History (every open document), the effect-cache memory +//! budget, and units for commands that report lengths. //! Colour settings (`colorSettings`) are stored with the colour state //! ([`crate::color_cmds::ColorSettings`]) and persisted in the same document. @@ -61,7 +62,7 @@ choice!(DarkTheme { Pro = "pro", ProMedium = "proMedium", Studio = "studio", Sol choice!(LightTheme { ProLight = "proLight", ProLightGray = "proLightGray", StudioLight = "studioLight", Classic = "classic", Adwaita = "adwaita", BreezeLight = "breezeLight", SoftLight = "softLight" } default ProLight); choice!(CanvasColor { Default = "default", Black = "black", DarkGray = "darkGray", MediumGray = "mediumGray", LightGray = "lightGray", Custom = "custom" } default Default); choice!(CanvasBorder { DropShadow = "dropShadow", Line = "line", None = "none" } default DropShadow); -choice!(UiScale { Auto = "auto", P75 = "75", P80 = "80", P85 = "85", P90 = "90", P95 = "95", P100 = "100", P125 = "125", P150 = "150", P175 = "175", P200 = "200", P250 = "250", P300 = "300" } default Auto); +choice!(UiScale { Auto = "auto", P75 = "75", P80 = "80", P85 = "85", P90 = "90", P95 = "95", P100 = "100", P105 = "105", P110 = "110", P115 = "115", P120 = "120", P125 = "125", P150 = "150", P175 = "175", P200 = "200", P250 = "250", P300 = "300" } default Auto); choice!( /// Graphics backend of the desktop app's window and GPU canvas (applies at next launch). /// `auto` lets PhotoCraft pick (DX12 for Intel adapters on Windows); `cpu` composites on the @@ -86,7 +87,7 @@ choice!(UiFontSize { Tiny = "tiny", Small = "small", Medium = "medium", Large = choice!(LogDestination { Metadata = "metadata", TextFile = "textFile", Both = "both" } default Metadata); choice!(LogDetail { SessionsOnly = "sessionsOnly", Concise = "concise", Detailed = "detailed" } default Concise); choice!(Ask { Always = "always", Never = "never", Ask = "ask" } default Always); -choice!(QuickExportFormat { Png = "png", Jpg = "jpg", Gif = "gif", Webp = "webp" } default Png); +choice!(QuickExportFormat { Png = "png", Jpg = "jpg", Gif = "gif", Webp = "webp", Exr = "exr" } default Png); choice!(ExportLocation { Ask = "ask", SameFolder = "sameFolder" } default Ask); choice!(ExportMetadata { None = "none", Copyright = "copyright", All = "all" } default Copyright); choice!( @@ -108,6 +109,12 @@ choice!(FontPreview { Off = "off", Small = "small", Medium = "medium", Large = " choice!(RawColorSpace { Srgb = "srgb", AdobeRgb = "adobeRgb", ProPhoto = "proPhoto", DisplayP3 = "displayP3" } default AdobeRgb); choice!(RawDepth { Eight = "8", Sixteen = "16" } default Sixteen); choice!(RawSharpen { None = "none", Screen = "screen", Glossy = "glossy", Matte = "matte" } default None); +choice!( + /// How a raw file's colour filter array is filled in when it is developed: AHD + /// (Hirakawa–Parks) keeps edges cleanest, Malvar–He–Cutler is sharp and fast, bilinear is the + /// fastest and softest. Photoshop's Camera Raw has no such choice. + RawDemosaic { Ahd = "ahd", Mhc = "mhc", Bilinear = "bilinear" } default Ahd +); impl Unit { /// Document pixels → this unit. `dpi` is the document resolution, `extent` the 100% length @@ -217,10 +224,13 @@ pub struct General { pub always_create_smart_objects_when_placing: bool, /// Scroll wheel zooms instead of panning. pub zoom_with_scroll_wheel: bool, + /// Wheel zoom eases over the frames of a notch; off, a notch is one instant ×1.1 step. pub animated_zoom: bool, pub zoom_resizes_windows: bool, pub use_legacy_free_transform: bool, pub auto_show_home_screen: bool, + /// Desktop: check published stable releases once in the background at launch. + pub automatically_check_updates: bool, } impl Default for General { @@ -237,6 +247,7 @@ impl Default for General { zoom_resizes_windows: false, use_legacy_free_transform: false, auto_show_home_screen: true, + automatically_check_updates: true, } } } @@ -286,6 +297,10 @@ pub struct Interface { /// own one-row title bar (tiling window managers, desktops that draw their own decorations; /// #1271, #1316). Read when the app starts. macOS always uses the system's. pub system_title_bar: bool, + /// Show the active document's file name in the title bar and the OS window title. The tabs + /// already name the file, so this can be turned off for a plain "PhotoCraft" title (#5037). + /// The unsaved-changes mark stays either way. + pub show_file_name_in_title: bool, /// Notices (the lower-right cards) hide themselves after /// [`Interface::notification_duration_seconds`] unless the pointer rests on them (#2022). pub notification_auto_hide: bool, @@ -317,6 +332,7 @@ impl Default for Interface { enable_animations: true, show_bounding_box_when_dragging_layer: false, system_title_bar: false, + show_file_name_in_title: true, notification_auto_hide: true, notification_duration_seconds: 6, } @@ -418,6 +434,25 @@ impl Default for Tools { /// since a whole-section update or a hand-edited preferences file can store anything. pub use photocraft_paint::ResponseCurve as PressureCurve; +mod cmds; +mod customize; +mod files; +mod shortcuts; +mod store; +mod validate; + +pub use cmds::specs; +#[cfg(test)] +pub(crate) use files::write_files_atomically; +pub use shortcuts::{ + ALTERNATE_SHORTCUTS, BindKind, MAX_ALTERNATES, TEMPORARY_TOOLS, alternate_keys, bindable_items, conflicts, default_alternates, normalize_shortcut, + reserved_shortcut, +}; +use store::read_lenient; +pub use store::{InvalidPref, PREFS_VERSION, PrefsStore}; +pub use validate::{HIDDEN_UNTIL_IMPLEMENTED, LINUX_ONLY, SECTIONS, choices, gpu_adapter_matches, is_color, is_hidden, parse_hex, range}; +use validate::{check_value, get_path, keyed, set_path}; + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct HistoryLog { @@ -439,6 +474,10 @@ impl Default for HistoryLog { pub struct FileHandling { pub image_previews: Ask, pub lowercase_extension: bool, + /// File › Save a Copy suggests the document's own name rather than " copy" (#8080). + pub do_not_append_copy: bool, + /// Saves run in the background while you keep working (on), or in the foreground under a + /// modal progress dialog that blocks the app until the file is written (off). pub save_in_background: bool, /// Save recovery information (crash recovery autosave). pub autosave: bool, @@ -458,6 +497,17 @@ pub struct FileHandling { pub recent_file_count: u32, /// Most recently opened files, newest first (File › Open Recent). pub recent_files: Vec, + /// A folder whose `.cube`, `.3dl` and `.look` files Color Lookup lists under its built-in + /// looks (native only, #6558). Empty lists none. + pub lut_folder: String, +} + +impl FileHandling { + /// What File › Save a Copy appends to the document's name: " copy", or nothing with Do not + /// append "copy" to filename when saving a copy (#8080). + pub fn copy_suffix(&self) -> &'static str { + if self.do_not_append_copy { "" } else { " copy" } + } } impl Default for FileHandling { @@ -465,6 +515,7 @@ impl Default for FileHandling { Self { image_previews: Ask::Always, lowercase_extension: true, + do_not_append_copy: false, save_in_background: true, autosave: true, autosave_minutes: 10, @@ -476,6 +527,7 @@ impl Default for FileHandling { rasterize_svg_groups_deeper_than: photocraft_doc::MAX_GROUP_DEPTH as u32, recent_file_count: 20, recent_files: Vec::new(), + lut_folder: String::new(), } } } @@ -507,15 +559,32 @@ impl Default for Export { } } +/// [`Performance::memory_usage_mb`]'s default. +pub const DEFAULT_MEMORY_USAGE_MB: u32 = 8192; + +/// The most Memory Usage the web build honours, in MB: a wasm module's linear memory tops out at +/// 4 GiB, so a larger allowance (the desktop default is 8 GiB) would never trim anything. +const WASM_MEMORY_USAGE_CAP_MB: u32 = 1536; + +/// The Memory Usage a build honours for a `stored` preference: capped on the web build (`web`). +/// Separate from the target check so native tests exercise both. +fn honoured_memory_usage_mb(stored: u32, web: bool) -> u32 { + if web { stored.min(WASM_MEMORY_USAGE_CAP_MB) } else { stored } +} + #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] pub struct Performance { /// Memory PhotoCraft may use, in MB: bounds each document's pixels plus its History (the - /// oldest states are dropped beyond it). + /// oldest states are dropped beyond it), and the result of one transform. pub memory_usage_mb: u32, /// Undo steps kept per document (History panel states). pub history_states: u32, - /// Display cache levels (mip levels kept for zoomed-out views). + /// Cache Levels, 1–8: the highest cache level the Histogram panel counts a large document at + /// (level n counts a proxy 2^(n-1) times smaller; 1 counts every pixel). Lower is more + /// accurate, higher faster. The panel picks the level from the document's size up to this; + /// the web build, which counts on the UI thread, reduces documents from a quarter of the size + /// the desktop does. Uncached Refresh always counts at level 1. pub cache_levels: u32, /// Side of the GPU canvas texture tiles, in pixels (applies at next launch). pub cache_tile_size: u32, @@ -525,6 +594,11 @@ pub struct Performance { pub rendering_mode: Option, /// Graphics backend (applies at next launch; see [`GpuBackend`]). pub gpu_backend: GpuBackend, + /// Which graphics adapter the window and canvas use on a machine with several (applies at + /// next launch): part of the adapter's name, matched case-insensitively ("nvidia", "arc"). Empty + /// is automatic. `PHOTOCRAFT_GPU_ADAPTER` overrides it for one launch. If nothing matches or the + /// match can't present to the window, startup falls back to automatic and says so. + pub gpu_adapter: String, /// Live previews of large documents (adjustment and filter dialogs, an adjustment layer's /// sliders while they drag) render on a reduced copy: fast, but blocky when zoomed in. Off: /// they render at full resolution. @@ -553,23 +627,31 @@ impl Performance { self.rendering_mode.unwrap_or_else(|| if !self.use_gpu || self.gpu_backend == GpuBackend::Cpu { RenderingMode::Cpu } else { RenderingMode::Auto }) } + /// [`Performance::memory_usage_mb`] as the build can honour it: capped on the web, where the + /// stored preference (a saved 8192 still loads) is larger than the module's address space. + pub fn effective_memory_usage_mb(&self) -> u32 { + honoured_memory_usage_mb(self.memory_usage_mb, cfg!(target_arch = "wasm32")) + } + /// Pixel memory a document and its History may hold (Memory Usage), in bytes: beyond it /// the oldest history states are dropped. pub fn history_budget_bytes(&self) -> usize { - (self.memory_usage_mb as usize).saturating_mul(1 << 20) + (self.effective_memory_usage_mb() as usize).saturating_mul(1 << 20) } } impl Default for Performance { + /// What a new install starts with, [`DEFAULT_MEMORY_USAGE_MB`] of Memory Usage among it. fn default() -> Self { Self { - memory_usage_mb: 8192, + memory_usage_mb: DEFAULT_MEMORY_USAGE_MB, history_states: 50, cache_levels: 4, cache_tile_size: 8192, use_gpu: true, rendering_mode: None, gpu_backend: GpuBackend::Auto, + gpu_adapter: String::new(), low_resolution_previews: true, low_latency_drawing: false, linux_display_server: LinuxDisplayServer::Auto, @@ -804,6 +886,9 @@ pub struct TypePrefs { pub font_preview: FontPreview, pub fill_new_type_layers_with_placeholder: bool, pub recent_fonts: u32, + /// Favourite font families, starred in the font menu (`type.font.favorite`), in the order + /// they were added. + pub favorite_fonts: Vec, } impl Default for TypePrefs { @@ -817,6 +902,7 @@ impl Default for TypePrefs { font_preview: FontPreview::Medium, fill_new_type_layers_with_placeholder: true, recent_fonts: 10, + favorite_fonts: Vec::new(), } } } @@ -858,6 +944,48 @@ pub struct RawDefaults { /// Photoshop does; off develops it with the defaults straight away. Automation opens never /// show the dialog. pub open_in_camera_raw: bool, + /// The demosaic method raws are developed with; the open-time Camera Raw dialog starts from it. + pub demosaic: RawDemosaic, + /// Keep a raw file's develop settings in a sidecar (`IMG_0001.xmp` beside `IMG_0001.CR2`), + /// as Camera Raw's "Save image settings in: Sidecar .xmp files"; the sidecar is read again + /// when the file is next opened. Off reads sidecars but never writes one. + pub write_xmp_sidecars: bool, +} + +impl RawColorSpace { + /// The built-in profile `edit.convertToProfile` knows this space as. + pub fn profile_id(self) -> &'static str { + match self { + RawColorSpace::Srgb => "srgb", + RawColorSpace::AdobeRgb => "adobe-rgb-compat", + RawColorSpace::ProPhoto => "prophoto-compat", + RawColorSpace::DisplayP3 => "display-p3", + } + } +} + +impl RawSharpen { + /// Camera Raw's output sharpening for this medium as the Detail panel's (amount, radius): + /// none, Screen (light, fine), Matte paper (stronger, wider: ink spreads), Glossy paper. + pub fn preset(self) -> Option<(f32, f32)> { + match self { + RawSharpen::None => None, + RawSharpen::Screen => Some((25.0, 1.0)), + RawSharpen::Matte => Some((35.0, 1.2)), + RawSharpen::Glossy => Some((40.0, 1.0)), + } + } +} + +impl RawDemosaic { + /// The developer's method. + pub fn method(self) -> photocraft_io::raw::Demosaic { + match self { + RawDemosaic::Ahd => photocraft_io::raw::Demosaic::Ahd, + RawDemosaic::Mhc => photocraft_io::raw::Demosaic::Mhc, + RawDemosaic::Bilinear => photocraft_io::raw::Demosaic::Bilinear, + } + } } impl Default for RawDefaults { @@ -870,6 +998,8 @@ impl Default for RawDefaults { open_as_smart_object: false, apply_auto_tone: false, open_in_camera_raw: true, + demosaic: RawDemosaic::Ahd, + write_xmp_sidecars: true, } } } @@ -889,6 +1019,24 @@ impl Default for Integrations { } } +/// The History panel's History Options… (not part of Edit › Preferences in Photoshop either). +/// Photoshop's snapshot options come with #3634. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct HistoryOptions { + /// Allow Non-Linear History: editing after selecting an earlier state keeps the later + /// states (the new step is appended at the end), and deleting a state deletes only it. + pub allow_non_linear_history: bool, + /// Make Layer Visibility Changes Undoable: showing or hiding a layer is a history state. + pub make_layer_visibility_changes_undoable: bool, +} + +impl Default for HistoryOptions { + fn default() -> Self { + Self { allow_non_linear_history: false, make_layer_visibility_changes_undoable: true } + } +} + /// Edit › Menus: hidden items and item colours, by command id. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] @@ -920,6 +1068,37 @@ pub struct ToolbarCustomization { pub order: Vec, } +/// Layer Mask Display Options (double-click the layer mask's row in the Channels panel, or Mask +/// Options… on its Layers thumbnail): the colour and opacity of the layer mask rubylith (`\`, +/// ⇧⌥-click the thumbnail). As in Photoshop, one setting for every mask, kept across restarts. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +pub struct LayerMaskOverlay { + /// `#rrggbb`. + pub color: String, + /// Percent, 0..=100. + pub opacity: f32, +} + +impl Default for LayerMaskOverlay { + /// Photoshop's: red at 50 %. + fn default() -> Self { + Self { color: "#ff0000".into(), opacity: 50.0 } + } +} + +impl LayerMaskOverlay { + /// The colour as 0..1 RGB; red if the stored value isn't a colour. + pub fn rgb(&self) -> [f32; 3] { + parse_hex(&self.color).unwrap_or([255, 0, 0]).map(|v| f32::from(v) / 255.0) + } + + /// The opacity as a fraction (0..1); 50 % if the stored value isn't a number. + pub fn alpha(&self) -> f32 { + if self.opacity.is_finite() { self.opacity.clamp(0.0, 100.0) / 100.0 } else { 0.5 } + } +} + /// All preferences. JSON keys are the `edit.preferences.
` ids. #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] @@ -943,6 +1122,8 @@ pub struct Preferences { pub enhanced_controls: EnhancedControls, pub raw_defaults: RawDefaults, pub integrations: Integrations, + /// History panel › History Options… (`history.options`). + pub history_options: HistoryOptions, /// Edit › Keyboard Shortcuts: command id → shortcut (`Cmd+Shift+N` notation); an empty /// string removes the default shortcut. pub shortcuts: BTreeMap, @@ -967,6 +1148,9 @@ pub struct Preferences { /// as the user changes it and restored at launch when Workspace › Remember Workspace /// Changes is on. JSON owned by the shell. pub panel_layout: Value, + /// The Color panel's options (what it shows, its ramp, Web colors, which chip it edits), + /// kept across restarts as Photoshop keeps them. JSON owned by the shell. + pub color_panel: Value, /// The last choices of dialogs that remember them across restarts, by command id (Edit › /// Fill…: `"edit.fill"` → its params). JSON owned by the shell. pub dialogs: BTreeMap, @@ -982,6 +1166,12 @@ pub struct Preferences { /// differ from their defaults, saved as the user changes them and restored at launch, as /// Photoshop remembers each tool's options between sessions. JSON owned by the shell. pub tool_options: Value, + /// Each painting tool's own brush (the Brush, the Eraser, the retouching tools…; see the + /// shell's `paint_mouse::sync_tool_brush`), saved as the user changes them and restored at + /// launch, as Photoshop keeps every tool's brush between sessions (#6212): `{"owner": tool, + /// "brushes": {tool: brush}}`, where `owner` is the tool whose brush `last_brush` holds and + /// the brushes are shaped by `brush_cmds::remembered_settings`. JSON owned by the shell. + pub tool_brushes: Value, /// Last selected tool, restored at launch. A tool-name string owned and validated by the /// shell; null in older files. Kept separate from tool options so neither overwrites the other. pub selected_tool: Value, @@ -991,279 +1181,8 @@ pub struct Preferences { pub view_extras: Value, /// File › Scripts › Script Events Manager: event → script bindings. pub script_events: crate::automate_cmds::ScriptEvents, -} - -/// Preferences dialog sections in Photoshop's order: (id, title). -pub const SECTIONS: [(&str, &str); 18] = [ - ("general", "General"), - ("interface", "Interface"), - ("workspace", "Workspace"), - ("tools", "Tools"), - ("historyLog", "History Log"), - ("fileHandling", "File Handling"), - ("export", "Export"), - ("performance", "Performance"), - ("scratchDisks", "Scratch Disks"), - ("cursors", "Cursors"), - ("transparencyAndGamut", "Transparency & Gamut"), - ("unitsAndRulers", "Units & Rulers"), - ("guidesGridAndSlices", "Guides, Grid & Slices"), - ("plugIns", "Plug-ins"), - ("type", "Type"), - ("enhancedControls", "Enhanced Controls"), - ("rawDefaults", "Camera Raw Defaults"), - ("integrations", "Integrations"), -]; - -/// Preferences that nothing reads yet, so Edit › Preferences doesn't show them: a control that -/// does nothing is worse than a missing one (https://github.com/storytold/photocraft/issues/204). -/// They still load, save and round-trip through `prefs.get` / `prefs.set` unchanged. -/// -/// When you implement one, remove it here; the `prefs_usage` test fails while a listed -/// preference is read anywhere outside `prefs.rs`, and while an unlisted one is read nowhere. -pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[ - "general.colorPicker", - "general.animatedZoom", - "general.zoomResizesWindows", - "workspace.autoShowHiddenPanels", - "workspace.enableFloatingDocumentWindowDocking", - "fileHandling.saveInBackground", - "performance.cacheLevels", - "performance.effectCacheMb", - "performance.legacyCompositing", - "scratchDisks.disks", - "unitsAndRulers.columnWidth", - "unitsAndRulers.gutter", - "plugIns.showExtensionPanels", - "plugIns.allowScriptsToConnect", - "plugIns.generatorEnabled", - "type.textEngine", - "type.recentFonts", - "enhancedControls.scrubbySliderAcceleration", - "enhancedControls.touchGestures", - "rawDefaults.colorSpace", - "rawDefaults.bitDepth", - "rawDefaults.resolution", - "rawDefaults.sharpenFor", - "rawDefaults.openAsSmartObject", - "rawDefaults.applyAutoTone", - // Agent access is governed by the launch flags (`--control`, the automation roots), not - // by these yet. - "integrations.allowAgentControl", - "integrations.controlPort", -]; - -/// Is the preference at `path` (`"section.key"`) hidden from the Preferences dialog? -pub fn is_hidden(path: &str) -> bool { - HIDDEN_UNTIL_IMPLEMENTED.contains(&path) || (!cfg!(target_os = "linux") && LINUX_ONLY.contains(&path)) -} - -/// Preferences that only do something on Linux; the dialog doesn't show them elsewhere. They -/// still load, save and round-trip on every platform. -pub const LINUX_ONLY: &[&str] = &["performance.linuxDisplayServer"]; - -/// Choices of an enumerated preference (dotted path, e.g. `"cursors.painting"`). -pub fn choices(path: &str) -> Option<&'static [&'static str]> { - Some(match path { - "general.colorPicker" => ColorPicker::NAMES, - "general.imageInterpolation" => Interpolation::NAMES, - "interface.theme" => Theme::NAMES, - "interface.appearanceMode" => AppearanceMode::NAMES, - "interface.darkTheme" => DarkTheme::NAMES, - "interface.lightTheme" => LightTheme::NAMES, - "interface.canvasColor" => CanvasColor::NAMES, - "interface.canvasBorder" => CanvasBorder::NAMES, - "interface.uiScale" => UiScale::NAMES, - "interface.uiFontSize" => UiFontSize::NAMES, - "historyLog.destination" => LogDestination::NAMES, - "historyLog.detail" => LogDetail::NAMES, - "fileHandling.imagePreviews" | "fileHandling.maximizePsdCompatibility" => Ask::NAMES, - "export.quickExportFormat" => QuickExportFormat::NAMES, - "export.quickExportLocation" => ExportLocation::NAMES, - "export.metadata" => ExportMetadata::NAMES, - "cursors.painting" => PaintingCursor::NAMES, - "cursors.other" => OtherCursor::NAMES, - "tools.rightClickWithPaintingTools" => RightClickPaint::NAMES, - "transparencyAndGamut.gridSize" => CheckerSize::NAMES, - "transparencyAndGamut.gridColors" => CheckerColors::NAMES, - "unitsAndRulers.rulers" | "guidesGridAndSlices.gridUnit" => Unit::NAMES, - "unitsAndRulers.typeUnits" => TypeUnit::NAMES, - "unitsAndRulers.pointSize" => PointSize::NAMES, - "guidesGridAndSlices.guideStyle" | "guidesGridAndSlices.gridStyle" => LineStyle::NAMES, - "type.textEngine" => TextEngine::NAMES, - "type.fontPreview" => FontPreview::NAMES, - "rawDefaults.colorSpace" => RawColorSpace::NAMES, - "rawDefaults.bitDepth" => RawDepth::NAMES, - "rawDefaults.sharpenFor" => RawSharpen::NAMES, - "performance.gpuBackend" => GpuBackend::NAMES, - "performance.linuxDisplayServer" => LinuxDisplayServer::NAMES, - "performance.renderingMode" => RenderingMode::NAMES, - _ => return None, - }) -} - -/// Valid range of a numeric preference. -pub fn range(path: &str) -> Option<(f64, f64)> { - Some(match path { - "fileHandling.autosaveMinutes" => (1.0, 240.0), - "fileHandling.recentFileCount" => (0.0, 100.0), - "tools.nudgeBundlePauseMs" => (100.0, 10_000.0), - "fileHandling.rasterizeSvgGroupsDeeperThan" => (0.0, photocraft_doc::MAX_GROUP_DEPTH as f64), - "interface.notificationDurationSeconds" => (1.0, 120.0), - "export.jpegQuality" | "export.webpQuality" => (1.0, 100.0), - "performance.memoryUsageMb" => (256.0, 1_048_576.0), - "performance.gpuMemoryMb" => (0.0, 1_048_576.0), - "performance.historyStates" => (1.0, 1000.0), - "performance.cacheLevels" => (1.0, 8.0), - "performance.cacheTileSize" => (256.0, 16384.0), - "performance.effectCacheMb" => (16.0, 65536.0), - "transparencyAndGamut.gamutWarningOpacity" => (1.0, 100.0), - "unitsAndRulers.columnWidth" | "unitsAndRulers.gutter" => (0.0, 10_000.0), - "unitsAndRulers.printResolution" | "unitsAndRulers.screenResolution" | "rawDefaults.resolution" => (1.0, 10_000.0), - "guidesGridAndSlices.gridlineEvery" => (0.001, 10_000.0), - "guidesGridAndSlices.subdivisions" => (1.0, 100.0), - "type.recentFonts" => (0.0, 50.0), - "integrations.controlPort" => (0.0, 65535.0), - _ => return None, - }) -} - -/// Is this preference a `#rrggbb` colour? -pub fn is_color(path: &str) -> bool { - if choices(path).is_some() || path.starts_with("shortcuts.") || path.starts_with("alternateShortcuts.") || path.starts_with("menus.") { - return false; - } - let leaf = path.rsplit('.').next().unwrap_or(path); - matches!( - leaf, - "guideColor" - | "smartGuideColor" - | "gridColor" - | "sliceColor" - | "canvasCustomColor" - | "brushPreviewColor" - | "gamutWarningColor" - | "customLight" - | "customDark" - ) -} - -pub fn parse_hex(s: &str) -> Option<[u8; 3]> { - let s = s.trim().trim_start_matches('#'); - if s.len() != 6 { - return None; - } - let b = |i: usize| s.get(i..i + 2).and_then(|s| u8::from_str_radix(s, 16).ok()); - Some([b(0)?, b(2)?, b(4)?]) -} - -// ------------------------------------------------------------------ JSON paths - -/// Map keys that are command ids (which contain dots): `shortcuts.`, -/// `alternateShortcuts.`, `menus.colors.`. -fn keyed(path: &str) -> Option<(&str, &str)> { - for prefix in ["shortcuts.", "alternateShortcuts.", "menus.colors."] { - if let Some(id) = path.strip_prefix(prefix) { - return Some((&prefix[..prefix.len() - 1], id)); - } - } - None -} - -fn get_path<'a>(root: &'a Value, path: &str) -> Option<&'a Value> { - if path.is_empty() { - return Some(root); - } - if let Some((map, id)) = keyed(path) { - return get_path(root, map)?.get(id); - } - path.split('.').try_fold(root, |v, k| v.get(k)) -} - -/// Set `value` at `path` (creating keys only inside free-form maps such as `shortcuts`). -fn set_path(root: &mut Value, path: &str, value: Value) -> std::result::Result<(), String> { - if let Some((map, id)) = keyed(path) { - let mut cur = &mut *root; - for k in map.split('.') { - cur = cur.get_mut(k).ok_or_else(|| format!("unknown preference `{path}`"))?; - } - cur.as_object_mut().ok_or_else(|| format!("`{map}` is not a map"))?.insert(id.to_string(), value); - return Ok(()); - } - let keys: Vec<&str> = path.split('.').filter(|k| !k.is_empty()).collect(); - let Some((last, parents)) = keys.split_last() else { return Err("empty path".into()) }; - let mut cur = root; - for k in parents { - cur = cur.get_mut(*k).ok_or_else(|| format!("unknown preference `{path}`"))?; - } - let free_map = matches!(parents.first(), Some(&"shortcuts")) || (parents.len() == 2 && parents[0] == "menus" && parents[1] == "colors"); - let obj = cur.as_object_mut().ok_or_else(|| format!("`{path}` is not inside a section"))?; - if !obj.contains_key(*last) && !free_map { - return Err(format!("unknown preference `{path}`")); - } - obj.insert(last.to_string(), value); - Ok(()) -} - -/// Validate one value for `path` before it is stored (choices, ranges, colours). -fn check_value(path: &str, v: &Value) -> std::result::Result<(), String> { - if path == "interface.favoriteThemes" { - let items = v.as_array().ok_or("`interface.favoriteThemes` must be a list of theme ids")?; - if items.len() > Theme::NAMES.len() { - return Err("`interface.favoriteThemes` contains too many themes".into()); - } - let mut seen = Vec::with_capacity(items.len()); - for item in items { - let theme = item.as_str().and_then(Theme::parse).ok_or_else(|| format!("unknown favorite theme `{item}`"))?; - if seen.contains(&theme) { - return Err(format!("duplicate favorite theme `{}`", theme.name())); - } - seen.push(theme); - } - } - if path == "performance.renderingMode" && v.is_null() { - return Ok(()); // Legacy policy, resolved from useGpu and gpuBackend. - } - if let Some(c) = choices(path) { - let s = v.as_str().ok_or_else(|| format!("`{path}` must be one of {}", c.join("|")))?; - if !c.contains(&s) { - return Err(format!("`{path}` must be one of {} (got `{s}`)", c.join("|"))); - } - } - if path == "interface.language" { - let ok = v.as_str().is_some_and(|s| !s.is_empty() && s.len() <= 16 && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')); - if !ok { - return Err("`interface.language` must be `auto` or a language code such as `en` or `ja`".into()); - } - } - if let Some((lo, hi)) = range(path) { - let x = v.as_f64().ok_or_else(|| format!("`{path}` must be a number"))?; - if !(lo..=hi).contains(&x) { - return Err(format!("`{path}` must be within {lo}..{hi} (got {x})")); - } - } - if is_color(path) && v.as_str().and_then(parse_hex).is_none() { - return Err(format!("`{path}` must be a #rrggbb colour")); - } - if path == "tools.pressureCurve" { - let pts = v.as_array().ok_or("`tools.pressureCurve` must be a list of [input, output] points")?; - if !(2..=PressureCurve::MAX_POINTS).contains(&pts.len()) { - return Err(format!("`tools.pressureCurve` needs 2..={} points (got {})", PressureCurve::MAX_POINTS, pts.len())); - } - for p in pts { - let ok = p.as_array().is_some_and(|a| a.len() == 2 && a.iter().all(|x| x.as_f64().is_some_and(|x| (0.0..=1.0).contains(&x)))); - if !ok { - return Err(format!("`tools.pressureCurve` points are [input, output] pairs within 0..1 (got {p})")); - } - } - } - if let Some(sc) = path.strip_prefix("shortcuts.") { - let s = v.as_str().ok_or_else(|| format!("shortcut for `{sc}` must be a string"))?; - if !s.is_empty() && normalize_shortcut(s).is_none() { - return Err(format!("`{s}` is not a valid shortcut (e.g. Cmd+Shift+N, F7)")); - } - } - Ok(()) + /// Layer Mask Display Options: the layer mask rubylith's colour and opacity. + pub layer_mask_overlay: LayerMaskOverlay, } impl Preferences { @@ -1346,9 +1265,15 @@ impl Preferences { self.shortcuts.clear(); return Ok(()); } + // The favourite fonts are the user's library, not a Type setting: resetting the Type + // section keeps them (only `type.favoriteFonts` itself, or Reset All, clears them). + let favorites = (path == "type").then(|| self.type_.favorite_fonts.clone()); let mut root = self.to_json(); set_path(&mut root, path, def)?; *self = serde_json::from_value(root).map_err(|e| e.to_string())?; + if let Some(favorites) = favorites { + self.type_.favorite_fonts = favorites; + } Ok(()) } @@ -1380,231 +1305,6 @@ impl Preferences { } } -/// Most further keys one command may hold; Photoshop's own defaults use one. -pub const MAX_ALTERNATES: usize = 8; - -/// Validate and normalise a command's alternate keys: a list of shortcut strings (a single -/// string is a one-key list, `""` none). -pub fn alternate_keys(v: &Value) -> std::result::Result, String> { - let items: Vec<&Value> = match v { - Value::String(s) if s.is_empty() => Vec::new(), - Value::String(_) => vec![v], - Value::Array(a) => a.iter().collect(), - _ => return Err("alternate shortcuts are a list of shortcuts (e.g. [\"F3\"])".into()), - }; - if items.len() > MAX_ALTERNATES { - return Err(format!("at most {MAX_ALTERNATES} alternate shortcuts (got {})", items.len())); - } - let mut out: Vec = Vec::with_capacity(items.len()); - for item in items { - let s = item.as_str().ok_or_else(|| format!("alternate shortcut `{item}` must be a string"))?; - let n = normalize_shortcut(s).ok_or_else(|| format!("`{s}` is not a valid shortcut (e.g. Cmd+Shift+N, F7)"))?; - if !out.contains(&n) { - out.push(n); - } - } - Ok(out) -} - -// ------------------------------------------------------------------ shortcuts - -/// Canonical spelling of a shortcut: modifiers in `Cmd+Ctrl+Alt+Shift` order, key last -/// (single characters upper-cased). `None` if it has no key or an unknown modifier. -pub fn normalize_shortcut(s: &str) -> Option { - let s = s.trim(); - if s.is_empty() { - return None; - } - let (mut cmd, mut ctrl, mut alt, mut shift) = (false, false, false, false); - let mut key: Option = None; - // A trailing "+" key is spelled "Cmd++". - let (body, plus_key) = if s.ends_with("++") || s == "+" { (s.trim_end_matches('+'), true) } else { (s, false) }; - for part in body.split('+').filter(|p| !p.is_empty()) { - match part.to_ascii_lowercase().as_str() { - "cmd" | "command" | "⌘" | "meta" | "super" => cmd = true, - "ctrl" | "control" | "⌃" => ctrl = true, - "alt" | "option" | "opt" | "⌥" => alt = true, - "shift" | "⇧" => shift = true, - _ => { - if key.is_some() { - return None; - } - let k = if part.chars().count() == 1 { part.to_uppercase() } else { capitalize(part) }; - key = Some(k); - } - } - } - if plus_key { - if key.is_some() { - return None; - } - key = Some("+".into()); - } - let key = key?; - let mut out = Vec::new(); - if cmd { - out.push("Cmd"); - } - if ctrl { - out.push("Ctrl"); - } - if alt { - out.push("Alt"); - } - if shift { - out.push("Shift"); - } - let mut s = out.join("+"); - if !s.is_empty() { - s.push('+'); - } - s.push_str(&key); - Some(s) -} - -fn capitalize(s: &str) -> String { - let mut c = s.chars(); - match c.next() { - Some(f) if f.is_ascii_alphabetic() => { - let rest: String = c.collect(); - // Function keys stay upper-case (F1…F24); named keys are Title case. - if f.eq_ignore_ascii_case(&'f') && rest.chars().all(|c| c.is_ascii_digit()) && !rest.is_empty() { - format!("F{rest}") - } else { - format!("{}{}", f.to_ascii_uppercase(), rest.to_ascii_lowercase()) - } - } - _ => s.to_string(), - } -} - -/// Hold-and-release gestures (#249): temporary tools that last while their key is held and give -/// the previous tool back on release. They are bound like shortcuts (Edit › Keyboard Shortcuts › -/// Tools › Temporary, `edit.keyboardShortcuts`) but held rather than pressed, so they are not -/// commands; the UI reads their keys through [`Preferences::shortcut`]. While a selection or -/// shape is being dragged, the Hand key repositions it instead of panning (Photoshop's Space). -/// The erase key keeps the Brush or Pencil and makes its strokes erase (Photoshop's `~`). -pub const TEMPORARY_TOOLS: &[(&str, &str, &str)] = &[ - ("tools.temporary.hand", "Hand Tool (hold)", "Space"), - ("tools.temporary.zoomIn", "Zoom In (hold, drag to scrub)", "Cmd+Space"), - ("tools.temporary.zoomOut", "Zoom Out (hold)", "Alt+Space"), - ("tools.temporary.erase", "Erase with Brush or Pencil (hold)", "`"), -]; - -/// Photoshop's second default keys (#3665): (command, key). Each command keeps its main key -/// (the one its menu shows) and answers these as well, as in Photoshop's default set: the -/// legacy function keys F2 Cut, F3 Copy, F4 Paste and Shift+F7 Inverse; Shift+Backspace -/// (Shift+Delete on a Mac) for Fill; and ⌘+ beside ⌘= for Zoom In. -pub const ALTERNATE_SHORTCUTS: &[(&str, &str)] = &[ - ("edit.cut", "F2"), - ("edit.copy", "F3"), - ("edit.paste", "F4"), - ("edit.fill", "Shift+Backspace"), - ("select.inverse", "Shift+F7"), - ("view.zoomIn", "Cmd++"), -]; - -/// A command's default alternate keys ([`ALTERNATE_SHORTCUTS`]). -pub fn default_alternates(id: &str) -> impl Iterator + '_ { - ALTERNATE_SHORTCUTS.iter().filter(move |(c, _)| *c == id).map(|(_, sc)| *sc) -} - -/// Keys the system takes before the app sees them (switching apps, closing the window, force -/// quit): `edit.keyboardShortcuts` won't bind them, as Photoshop won't. `sc` is in our notation, -/// where `Cmd` is Ctrl off the Mac. -pub fn reserved_shortcut(sc: &str) -> bool { - let Some(n) = normalize_shortcut(sc) else { return false }; - let reserved: &[&str] = if cfg!(target_os = "macos") { - &["Cmd+Tab", "Cmd+Shift+Tab", "Cmd+Alt+Escape", "Cmd+`"] - } else if cfg!(target_os = "windows") { - &["Alt+Tab", "Alt+Shift+Tab", "Alt+F4", "Cmd+Alt+Delete", "Cmd+Escape", "Cmd+Shift+Escape"] - } else { - // Linux and the BSDs: window switching and closing, and the virtual consoles. - &[ - "Alt+Tab", - "Alt+Shift+Tab", - "Alt+F4", - "Cmd+Alt+Delete", - "Cmd+Alt+F1", - "Cmd+Alt+F2", - "Cmd+Alt+F3", - "Cmd+Alt+F4", - "Cmd+Alt+F5", - "Cmd+Alt+F6", - "Cmd+Alt+F7", - "Cmd+Alt+F8", - "Cmd+Alt+F9", - "Cmd+Alt+F10", - "Cmd+Alt+F11", - "Cmd+Alt+F12", - ] - }; - reserved.iter().filter_map(|r| normalize_shortcut(r)).any(|r| r == n) -} - -/// What a bindable id is: a command (the engine's or the shell's), a tool, or a held tool. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum BindKind { - Command, - Tool, - Hold, -} - -/// Every bindable id: (id, label, menu path, default key, kind). The engine's commands, the -/// shell's ([`crate::shell_commands::SHELL_COMMANDS`]), the tools and the held temporary tools, -/// so Edit › Keyboard Shortcuts works the same with or without the app's UI. -pub fn bindable_items() -> impl Iterator, BindKind)> { - let engine = crate::command_specs().iter().map(|c| (c.id, c.label, c.menu, c.shortcut, BindKind::Command)); - let shell = crate::shell_commands::SHELL_COMMANDS.iter().map(|c| (c.0, c.1, c.2, c.3, BindKind::Command)); - let tools = crate::shell_commands::TOOLS.iter().map(|t| (t.0, t.1, &["Tools"][..], t.2, BindKind::Tool)); - let hold = TEMPORARY_TOOLS.iter().map(|t| (t.0, t.1, &["Tools", "Temporary"][..], Some(t.2), BindKind::Hold)); - engine.chain(shell).chain(tools).chain(hold) -} - -/// Every bindable id with its default. -fn bindable() -> impl Iterator)> { - bindable_items().map(|i| (i.0, i.3)) -} - -fn is_tool(id: &str) -> bool { - crate::shell_commands::tool_default(id).is_some() -} - -/// Shortcuts bound to more than one command: (shortcut, ids). `bindings` are (id, shortcut). -pub fn conflicts<'a>(bindings: impl IntoIterator) -> Vec<(String, Vec)> { - let mut by: BTreeMap> = BTreeMap::new(); - for (id, sc) in bindings { - if let Some(n) = normalize_shortcut(sc) { - by.entry(n).or_default().push(id.to_string()); - } - } - by.into_iter().filter(|(_, ids)| ids.len() > 1).collect() -} - -// ------------------------------------------------------------------ store - -/// The session's preferences plus a revision counter that moves on every change (frontends -/// persist when it moves). -#[derive(Clone, Debug, Default)] -pub struct PrefsStore { - values: Preferences, - rev: u64, -} - -impl PrefsStore { - pub fn get(&self) -> &Preferences { - &self.values - } - /// Change preferences; bumps the revision. - pub fn edit(&mut self, f: impl FnOnce(&mut Preferences) -> R) -> R { - self.rev += 1; - f(&mut self.values) - } - pub fn rev(&self) -> u64 { - self.rev - } -} - impl Session { /// The current preferences. pub fn prefs(&self) -> &Preferences { @@ -1618,18 +1318,22 @@ impl Session { r } - /// Push engine-relevant preferences into live state: every document's history limit, the - /// layer-effect cache budget and the text engine's missing-glyph protection. + /// Push engine-relevant preferences into live state: every document's history limit and + /// linearity, the transform and layer-effect cache budgets and the text engine's + /// missing-glyph protection. pub fn apply_prefs(&mut self) { let n = self.prefs().performance.history_states.max(1) as usize; let bytes = self.prefs().performance.history_budget_bytes(); let budget = self.prefs().performance.effect_cache_mb as usize; + let non_linear = self.prefs().history_options.allow_non_linear_history; for st in &mut self.docs { st.history.max_states = n; st.history.max_bytes = bytes; + st.history.non_linear = non_linear; st.trim_history(); } photocraft_compose::set_effect_cache_budget(budget << 20); + crate::transform_cmds::set_budget(u64::from(self.prefs().performance.effective_memory_usage_mb()) << 20); // Without waiting for the system font scan: this runs on the UI thread at launch. photocraft_text::set_shared_missing_glyph_protection(self.prefs().type_.missing_glyph_protection()); crate::plugin_cmds::sync_prefs(self); @@ -1645,7 +1349,7 @@ impl Session { let mut v = self.prefs().to_json(); if let Value::Object(m) = &mut v { m.insert("colorSettings".into(), serde_json::to_value(&self.color.settings).unwrap_or(Value::Null)); - m.insert("version".into(), json!(1)); + m.insert("version".into(), json!(PREFS_VERSION)); m.insert("presets".into(), self.presets.to_json(self)); } v @@ -1653,8 +1357,24 @@ impl Session { /// Restore preferences saved by [`Session::prefs_to_json`]. Missing keys keep their /// defaults and unknown keys are ignored, so files from older and newer versions load. - pub fn load_prefs_json(&mut self, s: &str) -> std::result::Result<(), String> { + /// A value of the wrong type (a hand-edited `"recentFileCount": "ten"`) keeps its default + /// and everything else still loads (#6611); the returned list names those values. Only + /// text that isn't a JSON object fails as a whole. + pub fn load_prefs_json(&mut self, s: &str) -> std::result::Result, String> { let mut v: Value = serde_json::from_str(s).map_err(|e| format!("preferences: {e}"))?; + if !v.is_object() { + return Err("preferences: expected a JSON object".into()); + } + // Files before version 2 hold Use Shift Key for Tool Switch off because that was the + // default when they were written: every key is saved, chosen or not. Like Photoshop it is + // on by default now (#3528), so those files take the new default (#5647: a tool letter + // pressed again kept cycling its group). A choice made since is saved as version 2. + if v.get("version").and_then(Value::as_u64).unwrap_or(0) < 2 + && let Some(tools) = v.get_mut("tools").and_then(Value::as_object_mut) + && tools.get("useShiftKeyForToolSwitch") == Some(&json!(false)) + { + tools.remove("useShiftKeyForToolSwitch"); + } // Saved preferences before appearance modes had one concrete theme. Preserve its // appearance instead of silently switching established users to Auto. if let Some(interface) = v.get_mut("interface").and_then(Value::as_object_mut) @@ -1675,9 +1395,10 @@ impl Session { } let color = v.as_object_mut().and_then(|m| m.remove("colorSettings")); let presets = v.as_object_mut().and_then(|m| m.remove("presets")); - let prefs: Preferences = serde_json::from_value(v).map_err(|e| format!("preferences: {e}"))?; + let mut invalid = Vec::new(); + let prefs: Preferences = read_lenient(v, &[], &mut invalid); if let Some(c) = color { - self.color.settings = serde_json::from_value(c).unwrap_or_default(); + self.color.settings = read_lenient(c, &["colorSettings"], &mut invalid); photocraft_compose::psblend::set_text_gamma(self.color.settings.blend_text_gamma); } self.prefs.edit(|p| *p = prefs); @@ -1685,7 +1406,7 @@ impl Session { self.load_presets_json(v); } self.apply_prefs(); - Ok(()) + Ok(invalid) } /// Full JSON view used by `prefs.get`: preferences plus `colorSettings`. @@ -1701,7 +1422,11 @@ impl Session { /// Batches defer colour validation until their complete candidate is staged. Returns /// whether colour settings were touched, including whole-section/root-object updates. fn set_pref(&mut self, path: &str, value: Value, validate_color: bool) -> std::result::Result { - if let Some(rest) = path.strip_prefix("colorSettings") { + // The section itself or a path inside it: `colorSettingsworkingRgb`, `colorSettings.` and + // `colorSettings..workingRgb` are none, and the authorization policy doesn't treat them as one. + if let Some(rest) = + path.strip_prefix("colorSettings").filter(|r| r.is_empty() || r.strip_prefix('.').is_some_and(|c| c.split('.').all(|part| !part.is_empty()))) + { let mut cur = serde_json::to_value(&self.color.settings).map_err(|e| e.to_string())?; let rest = rest.trim_start_matches('.'); if rest.is_empty() { @@ -1811,699 +1536,22 @@ impl Session { } } -// ------------------------------------------------------------------ commands - -fn always(_: &Session) -> std::result::Result<(), String> { - Ok(()) -} - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -fn prefs_get(s: &mut Session, p: &Value) -> Result { - let path = p.get("path").and_then(Value::as_str).unwrap_or(""); - let v = s.prefs_view(); - get_path(&v, path).cloned().ok_or_else(|| bad("prefs.get", format!("unknown preference `{path}`"))) -} - -fn prefs_set(s: &mut Session, p: &Value) -> Result { - let cmd = "prefs.set"; - let mut changes: Vec<(String, Value)> = Vec::new(); - if let Some(path) = p.get("path").and_then(Value::as_str) { - changes.push((path.to_string(), p.get("value").cloned().ok_or_else(|| bad(cmd, "missing `value`"))?)); - } - if let Some(m) = p.get("values").and_then(Value::as_object) { - changes.extend(m.iter().map(|(k, v)| (k.clone(), v.clone()))); - } - if changes.is_empty() { - return Err(bad(cmd, "give `path` and `value`, or `values: {path: value, …}`")); - } - // All or nothing: validate against a scratch copy first. - let (saved_prefs, saved_color) = (s.prefs().clone(), s.color.settings.clone()); - let mut color_changed = false; - for (path, v) in &changes { - match s.set_pref(path, v.clone(), false) { - Ok(touched) => color_changed |= touched, - Err(e) => { - s.prefs.edit(|p| *p = saved_prefs); - s.color.settings = saved_color; - return Err(bad(cmd, e)); - } - } - } - if color_changed && let Err(e) = crate::color_cmds::validate_settings(&s.color.settings) { - s.prefs.edit(|p| *p = saved_prefs); - s.color.settings = saved_color; - return Err(bad(cmd, e)); - } - // `colorSettings.blendTextGamma` lives in the compositor too, as `edit.colorSettings` sets it. - photocraft_compose::psblend::set_text_gamma(s.color.settings.blend_text_gamma); - s.apply_prefs(); - let view = s.prefs_view(); - let out: Map = changes.iter().map(|(k, _)| (k.clone(), get_path(&view, k).cloned().unwrap_or(Value::Null))).collect(); - Ok(Value::Object(out)) -} - -fn prefs_reset(s: &mut Session, p: &Value) -> Result { - let path = p.get("path").or_else(|| p.get("section")).and_then(Value::as_str); - match path { - Some("colorSettings") => s.color.settings = Default::default(), - // One colour setting: `Preferences` has no `colorSettings`, so take the default - // from `ColorSettings` and route it like `prefs.set` does. - Some(path) if path.starts_with("colorSettings.") => { - let defaults = serde_json::to_value(crate::color_cmds::ColorSettings::default()).map_err(|e| bad("prefs.reset", e.to_string()))?; - let key = path.strip_prefix("colorSettings.").unwrap_or(path); - let def = get_path(&defaults, key).cloned().ok_or_else(|| bad("prefs.reset", format!("unknown preference `{path}`")))?; - s.set_pref(path, def, true).map_err(|e| bad("prefs.reset", e))?; - } - None => { - s.color.settings = Default::default(); - s.edit_prefs(|p| p.reset(None)).map_err(|e| bad("prefs.reset", e))?; - } - Some(path) => { - let mut next = s.prefs().clone(); - next.reset(Some(path)).map_err(|e| bad("prefs.reset", e))?; - s.edit_prefs(|p| *p = next); - } - } - s.prefs.edit(|_| ()); - photocraft_compose::psblend::set_text_gamma(s.color.settings.blend_text_gamma); - s.apply_prefs(); - if path.is_some_and(|path| keyed(path).is_some()) { - // The removed override is absent, so reading its old path would report an error. - Ok(Value::Null) - } else { - prefs_get(s, &json!({"path": path.unwrap_or("")})) - } -} - -/// `edit.preferences.
`: the section's values (the GUI opens the dialog on it instead). -fn preferences_section(s: &mut Session, p: &Value) -> Result { - let section = p.get("__section").and_then(Value::as_str).unwrap_or("general").to_string(); - let title = SECTIONS.iter().find(|(id, _)| *id == section).map_or("General", |(_, t)| *t); - let values = s.prefs().get(§ion).unwrap_or(Value::Null); - Ok(json!({"section": section, "title": title, "values": values})) -} - -/// Edit › Keyboard Shortcuts (engine part): list, change and reset application shortcuts. -fn keyboard_shortcuts(s: &mut Session, p: &Value) -> Result { - let cmd = "edit.keyboardShortcuts"; - // Every change lands at once, after all of it validated: a bad key leaves the set as it was. - let mut next = s.prefs().clone(); - // A set loads first, so the call's other changes apply on top of it. - let mut changed = crate::shortcut_sets::load(&mut next, p).map_err(|e| bad(cmd, e))?; - if let Some(r) = p.get("reset") { - match r { - Value::Bool(true) => { - next.shortcuts.clear(); - next.alternate_shortcuts.clear(); - changed = true; - } - Value::Array(ids) => { - for id in ids.iter().filter_map(Value::as_str) { - next.shortcuts.remove(id); - next.alternate_shortcuts.remove(id); - } - changed = true; - } - _ => {} - } - } - // A Photoshop `.kys` set (`importKys`, its XML text) becomes `set` and `alternates` entries, - // matched by label (`crate::kys`); a key equal to the command's default restores the - // default, and keys given in `set` / `alternates` as well win over the file's. - let imported; - let mut import = None; - let p = match p.get("importKys") { - Some(v) => { - // The file's text, or {"path": …} to read it. - let read; - let xml = match v { - Value::String(s) => s.as_str(), - Value::Object(o) => { - let path = - o.get("path").and_then(Value::as_str).filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, "`importKys.path` is a .kys file"))?; - read = read_text_file(path, crate::kys::MAX_BYTES).map_err(|e| bad(cmd, format!("{path}: {e}")))?; - read.as_str() - } - _ => return Err(bad(cmd, "`importKys` is the text of a .kys file, or {\"path\": …}")), - }; - let set = crate::kys::parse(xml).map_err(|e| bad(cmd, e))?; - let plan = crate::kys::plan(crate::kys::command_candidates(), &set); - let mut merged = serde_json::Map::new(); - // A key the system reserves is left out (reported as `reserved`), not a failed import. - let mut skipped: Vec = Vec::new(); - for (id, sc) in plan.set.iter().filter(|(id, sc)| { - let bad = reserved_shortcut(sc); - if bad { - skipped.push(format!("{id}: {sc}")); - } - !bad - }) { - let default = crate::command_specs().iter().find(|c| c.id == id).and_then(|c| c.shortcut).and_then(normalize_shortcut); - merged.insert(id.clone(), if default.as_deref() == Some(sc.as_str()) { Value::Null } else { json!(sc) }); - } - // Tool keys: a tool's own letter restores it (and its Shift cycling). - let tools = crate::shell_commands::TOOLS.iter().map(|t| (t.0, t.1)); - let (tool_keys, unknown_tools) = crate::kys::plan_tools(tools, &set); - for (id, key) in &tool_keys { - let own = crate::shell_commands::tool_default(id).flatten().is_some_and(|d| d.eq_ignore_ascii_case(key)); - merged.insert(id.clone(), if own { Value::Null } else { json!(key) }); - } - merged.extend(p.get("set").and_then(Value::as_object).cloned().unwrap_or_default()); - let mut alternates = serde_json::Map::new(); - for (id, keys) in &plan.alternate_keys { - let keys: &Vec = &keys.iter().filter(|k| !reserved_shortcut(k)).cloned().collect(); - let main = plan.set.get(id).map(String::as_str); - let default = Preferences::default().alternates(id, main); - alternates.insert(id.clone(), if *keys == default { Value::Null } else { json!(keys) }); - } - alternates.extend(p.get("alternates").and_then(Value::as_object).cloned().unwrap_or_default()); - import = Some(json!({ - "name": set.name, - "imported": plan.set.len(), - "set": plan.set, - "unknown": plan.unknown, - "unreadable": plan.unreadable, - "alternates": plan.alternates, - "alternateKeys": plan.alternate_keys, - "toolKeys": set.tool_keys, - "tools": tool_keys, - "unknownTools": unknown_tools, - "reserved": skipped, - "windows": set.windows, - })); - let mut with_import = p.clone(); - with_import["set"] = Value::Object(merged); - with_import["alternates"] = Value::Object(alternates); - imported = with_import; - &imported - } - None => p, - }; - let empty = serde_json::Map::new(); - let set = match p.get("set") { - Some(Value::Object(m)) => m, - _ => &empty, - }; - let alts = match p.get("alternates") { - None | Some(Value::Null) => &empty, - Some(v) => v.as_object().ok_or_else(|| bad(cmd, "`alternates` maps command ids to lists of shortcuts"))?, - }; - if !set.is_empty() || !alts.is_empty() { - let allow_unknown = p.get("allowUnknown").and_then(Value::as_bool).unwrap_or(false); - changed = true; - // Reserved system keys can't be bound, by hand or from a file. - let reserved = |v: &Value| -> Option { - let keys = match v { - Value::String(s) => vec![s.clone()], - other => alternate_keys(other).unwrap_or_default(), - }; - keys.into_iter().find(|k| reserved_shortcut(k)) - }; - if let Some((id, k)) = set.iter().chain(alts.iter()).find_map(|(id, v)| reserved(v).map(|k| (id, k))) { - return Err(bad(cmd, format!("`{k}` (for `{id}`) is reserved by the system and can't be assigned"))); - } - for (id, v) in set { - if !bindable().any(|(b, _)| b == id) && !allow_unknown { - return Err(bad(cmd, format!("unknown command `{id}`"))); - } - next.set(&format!("shortcuts.{id}"), v.clone()).map_err(|e| bad(cmd, e))?; - } - for (id, v) in alts { - if !bindable().any(|(b, _)| b == id) && default_alternates(id).next().is_none() && !allow_unknown { - return Err(bad(cmd, format!("unknown command `{id}`"))); - } - next.set(&format!("alternateShortcuts.{id}"), v.clone()).map_err(|e| bad(cmd, e))?; - } - // Like Photoshop, a shortcut moved to a new command is taken away from the old one, - // whether the old one held it as its main key or as an alternate (#3665). - if p.get("removeConflicts").and_then(Value::as_bool).unwrap_or(true) { - let assigned: Vec<(&str, String)> = set - .iter() - .filter_map(|(id, v)| Some((id.as_str(), v.as_str().and_then(normalize_shortcut)?))) - .chain(alts.iter().flat_map(|(id, v)| alternate_keys(v).unwrap_or_default().into_iter().map(move |k| (id.as_str(), k)))) - .collect(); - for (id, sc) in assigned { - for (c, def) in bindable() { - // Tools may share a key: they cycle with Shift, as Photoshop's groups do. - if is_tool(id) && is_tool(c) { - continue; - } - // Never strip a command that this same call is assigning: - // two entries for one key are a clash inside the call, which - // the returned conflicts list reports (#719). Stripping - // each other here unbound both silently. - if c == id || set.contains_key(c) || alts.contains_key(c) { - continue; - } - let main = next.shortcut(c, def).map(str::to_string); - if main.as_deref().and_then(normalize_shortcut).as_deref() == Some(sc.as_str()) { - next.shortcuts.insert(c.to_string(), String::new()); - } - let held = next.alternates(c, main.as_deref()); - if held.contains(&sc) { - next.alternate_shortcuts.insert(c.to_string(), held.into_iter().filter(|k| *k != sc).collect()); - } - } - } - } - } - // Then the sets are saved, deleted or switched, from the bindings as they now stand. - changed |= crate::shortcut_sets::store(&mut next, p).map_err(|e| bad(cmd, e))?; - // The reply and the exports read the bindings as they will be; they are committed only once - // the exports are written, so a bad export target changes nothing. - let prefs = &next; - let filter = p.get("filter").and_then(Value::as_str).map(str::to_ascii_lowercase); - let list: Vec = bindable_items() - .filter(|c| filter.as_ref().is_none_or(|f| c.0.to_ascii_lowercase().contains(f) || c.1.to_ascii_lowercase().contains(f))) - .filter(|c| { - p.get("list").and_then(Value::as_bool).unwrap_or(false) - || filter.is_some() - || prefs.shortcuts.contains_key(c.0) - || prefs.alternate_shortcuts.contains_key(c.0) - }) - .map(|(id, label, menu, def, kind)| { - let main = prefs.shortcut(id, def); - let defaults: Vec<&str> = default_alternates(id).collect(); - let mut v = json!({"id": id, "label": label, "menu": menu, "default": def, "shortcut": main, "alternates": prefs.alternates(id, main)}); - if !defaults.is_empty() { - v["defaultAlternates"] = json!(defaults); - } - match kind { - BindKind::Hold => v["hold"] = json!(true), - BindKind::Tool => v["tool"] = json!(true), - BindKind::Command => {} - } - v - }) - .collect(); - let mut bindings: Vec<(&str, String)> = Vec::new(); - for (id, def) in bindable() { - let main = prefs.shortcut(id, def); - bindings.extend(main.map(|m| (id, m.to_string()))); - bindings.extend(prefs.alternates(id, main).into_iter().map(|k| (id, k))); - } - // Tools sharing a key are a cycling group, not a conflict. - let conflicts: Vec = conflicts(bindings.iter().map(|(id, k)| (*id, k.as_str()))) - .into_iter() - .filter(|(_, ids)| !ids.iter().all(|id| is_tool(id))) - .map(|(sc, ids)| json!({"shortcut": sc, "commands": ids})) - .collect(); - let mut out = json!({"overrides": prefs.shortcuts, "alternateOverrides": prefs.alternate_shortcuts, "commands": list, "conflicts": conflicts}); - if let (Some(o), Value::Object(sets)) = (out.as_object_mut(), crate::shortcut_sets::describe(prefs)) { - o.extend(sets); - } - // The bindings as a `.kys` file, or Photoshop's Summarize page: every bindable item (the - // engine's commands, the shell's commands and the tools). - let set_name = if prefs.shortcut_set.is_empty() { crate::shortcut_sets::DEFAULTS_NAME } else { prefs.shortcut_set.as_str() }; - // Both targets are resolved and their texts made before anything is written, and a path that - // can't be written is refused before either file is: a bad export changes nothing. - let kys_target = file_target(cmd, p.get("exportKys"))?; - let html_target = file_target(cmd, p.get("summarize"))?; - let mut files: Vec<(&str, String, String)> = Vec::new(); - if let Some(target) = kys_target { - let entries: Vec = - crate::shortcut_sets::bound(prefs).into_iter().map(|b| crate::kys::Entry { id: b.id, label: b.label, tool: b.tool, keys: b.keys }).collect(); - let text = crate::kys::write(set_name, &entries); - match target { - Some(path) => files.push(("kysPath", path, text)), - None => out["kys"] = json!(text), - } - } - if let Some(target) = html_target { - let v = p.get("summarize").cloned().unwrap_or(Value::Null); - let mac = v.get("mac").and_then(Value::as_bool).unwrap_or(cfg!(target_os = "macos")); - let mut sections: Vec = Vec::new(); - for crate::shortcut_sets::Bound { label, menu, keys, .. } in crate::shortcut_sets::bound(prefs) { - match sections.iter_mut().find(|(m, _)| *m == menu) { - Some((_, rows)) => rows.push((label, keys)), - None => sections.push((menu, vec![(label, keys)])), - } - } - let html = crate::shortcut_sets::summarize(set_name, §ions, mac); - match target { - Some(path) => files.push(("htmlPath", path, html)), - None => out["html"] = json!(html), - } - } - for (_, path, _) in &files { - writable(path).map_err(|e| bad(cmd, format!("{path}: {e}")))?; - } - let list: Vec<(&str, &str)> = files.iter().map(|(_, path, text)| (path.as_str(), text.as_str())).collect(); - write_files_atomically(&list, &rename_path).map_err(|e| bad(cmd, e))?; - for (key, path, _) in &files { - out[*key] = json!(path); - } - if changed { - s.edit_prefs(|p| *p = next); - } - if let Some(import) = import { - out["import"] = import; - } - Ok(out) -} - -/// An output parameter (`exportKys`, `summarize`): absent or false → `None`; true (or an object -/// without `path`) → `Some(None)`, the text comes back in the reply; `{"path": …}` → -/// `Some(Some(path))`, written there. -fn file_target(cmd: &str, v: Option<&Value>) -> Result>> { - match v { - None | Some(Value::Null) | Some(Value::Bool(false)) => Ok(None), - Some(Value::Bool(true)) => Ok(Some(None)), - Some(Value::Object(o)) => match o.get("path") { - None | Some(Value::Null) => Ok(Some(None)), - Some(Value::String(s)) if !s.trim().is_empty() => Ok(Some(Some(s.clone()))), - Some(_) => Err(bad(cmd, "`path` must be a non-empty string")), - }, - Some(_) => Err(bad(cmd, "expected true or {\"path\": …}")), - } -} - -#[cfg(not(target_arch = "wasm32"))] -fn read_text_file(path: &str, max: usize) -> std::result::Result { - let meta = std::fs::metadata(path).map_err(|e| e.to_string())?; - if !meta.is_file() || meta.len() > max as u64 { - return Err(format!("not a file of at most {} KiB", max / 1024)); - } - std::fs::read_to_string(path).map_err(|e| e.to_string()) -} - -#[cfg(target_arch = "wasm32")] -fn read_text_file(_: &str, _: usize) -> std::result::Result { - Err("paths can't be read on the web; pass the file's text instead".into()) -} - -/// Whether `path` can be written: not a folder, in a folder that exists. -#[cfg(not(target_arch = "wasm32"))] -fn writable(path: &str) -> std::result::Result<(), String> { - let p = std::path::Path::new(path); - if p.is_dir() { - return Err("is a folder".into()); - } - if std::fs::metadata(p).is_ok_and(|m| m.permissions().readonly()) { - return Err("is read-only".into()); - } - match p.parent().filter(|d| !d.as_os_str().is_empty()) { - Some(d) if !d.is_dir() => Err("its folder doesn't exist".into()), - _ => Ok(()), - } -} - -#[cfg(target_arch = "wasm32")] -fn writable(_: &str) -> std::result::Result<(), String> { - Err("paths can't be written on the web; omit `path` to get the text back".into()) -} - -#[cfg(not(target_arch = "wasm32"))] -fn rename_path(from: &std::path::Path, to: &std::path::Path) -> std::io::Result<()> { - std::fs::rename(from, to) -} - -#[cfg(target_arch = "wasm32")] -fn rename_path(_: &std::path::Path, _: &std::path::Path) -> std::io::Result<()> { - Err(std::io::Error::other("paths can't be written on the web")) -} - -/// Where `path` points, for telling whether two targets are one file: its folder resolved, its -/// name compared without case where the file system ignores it. -#[cfg(not(target_arch = "wasm32"))] -fn path_key(path: &str) -> String { - let p = std::path::Path::new(path); - let dir = p.parent().filter(|d| !d.as_os_str().is_empty()).unwrap_or(std::path::Path::new(".")); - let dir = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf()); - let key = dir.join(p.file_name().unwrap_or_default()).to_string_lossy().into_owned(); - if cfg!(any(target_os = "windows", target_os = "macos")) { key.to_lowercase() } else { key } -} - -/// A new, empty file beside `path` with a name nothing else uses (`create_new`), for staging -/// (`kind` "tmp") or a backup ("bak"). -#[cfg(not(target_arch = "wasm32"))] -fn sibling(path: &str, kind: &str) -> std::io::Result<(std::path::PathBuf, std::fs::File)> { - for n in 0..1000u32 { - let p = std::path::PathBuf::from(format!("{path}.{}-{n}.photocraft-{kind}", std::process::id())); - match std::fs::OpenOptions::new().write(true).create_new(true).open(&p) { - Ok(f) => return Ok((p, f)), - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, - Err(e) => return Err(e), - } - } - Err(std::io::Error::other("no free name beside it")) -} - -/// Write `files` ((path, text)) all or nothing. Each text is staged in a new file beside its -/// target; once every one is written, each existing target is moved aside to a backup and the -/// staged file moved in. Any failure moves the backups back and removes what was staged, so the -/// user's files end as they began; on success the backups go. Two targets naming one file are -/// refused. `rename` is `std::fs::rename` (a test fails it on purpose). -#[cfg(not(target_arch = "wasm32"))] -pub(crate) fn write_files_atomically( - files: &[(&str, &str)], - rename: &dyn Fn(&std::path::Path, &std::path::Path) -> std::io::Result<()>, -) -> std::result::Result<(), String> { - use std::io::Write; - use std::path::{Path, PathBuf}; - for (i, (a, _)) in files.iter().enumerate() { - if files.iter().skip(i + 1).any(|(b, _)| path_key(a) == path_key(b)) { - return Err(format!("{a}: two exports to the same file")); - } - } - let mut staged: Vec = Vec::new(); - let drop_all = |paths: &[PathBuf]| { - for p in paths { - let _ = std::fs::remove_file(p); - } - }; - for (path, text) in files { - let (tmp, mut f) = match sibling(path, "tmp") { - Ok(v) => v, - Err(e) => { - drop_all(&staged); - return Err(format!("{path}: {e}")); - } - }; - staged.push(tmp); - if let Err(e) = f.write_all(text.as_bytes()).and_then(|()| f.sync_all()) { - drop_all(&staged); - return Err(format!("{path}: {e}")); - } - } - // (target, its backup) of each file moved in so far. - let mut moved: Vec<(&Path, Option)> = Vec::new(); - // Put each backup back over its target (a target that had none is removed). What couldn't be - // put back is named, so the user can find their previous file. - let undo = |moved: &[(&Path, Option)]| -> String { - let mut stranded = Vec::new(); - for (target, backup) in moved.iter().rev() { - match backup { - Some(b) => { - if rename(b, target).is_err() { - stranded.push(format!("the previous {} is at {}", target.display(), b.display())); - } - } - None => { - let _ = std::fs::remove_file(target); - } - } - } - if stranded.is_empty() { String::new() } else { format!(" ({})", stranded.join("; ")) } - }; - for ((path, _), tmp) in files.iter().zip(&staged) { - let target = Path::new(*path); - let backup = if target.exists() { - let moved_aside = sibling(path, "bak").and_then(|(b, f)| { - drop(f); - match rename(target, &b) { - Ok(()) => Ok(b), - Err(e) => { - // The placeholder the backup name reserved goes too. - let _ = std::fs::remove_file(&b); - Err(e) - } - } - }); - match moved_aside { - Ok(b) => Some(b), - Err(e) => { - let stranded = undo(&moved); - drop_all(&staged); - return Err(format!("{path}: {e}{stranded}")); - } - } - } else { - None - }; - if let Err(e) = rename(tmp, target) { - let mut stranded = undo(&moved); - if let Some(b) = &backup - && rename(b, target).is_err() - { - stranded = format!("{stranded} (the previous {path} is at {})", b.display()); - } - drop_all(&staged); - return Err(format!("{path}: {e}{stranded}")); - } - moved.push((target, backup)); - } - for (_, backup) in moved { - if let Some(b) = backup { - let _ = std::fs::remove_file(b); - } - } - Ok(()) -} - -#[cfg(target_arch = "wasm32")] -pub(crate) fn write_files_atomically( - files: &[(&str, &str)], - _: &dyn Fn(&std::path::Path, &std::path::Path) -> std::io::Result<()>, -) -> std::result::Result<(), String> { - if files.is_empty() { Ok(()) } else { Err("paths can't be written on the web; omit `path` to get the text back".into()) } -} - -/// Edit › Menus: hide/show items and give them colours. -fn menus(s: &mut Session, p: &Value) -> Result { - let ids = |k: &str| -> Vec { - p.get(k).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default() - }; - const COLORS: [&str; 8] = ["none", "red", "orange", "yellow", "green", "blue", "violet", "gray"]; - if let Some(m) = p.get("color").and_then(Value::as_object) { - for v in m.values() { - if let Some(c) = v.as_str().filter(|c| !COLORS.contains(c)) { - return Err(bad("edit.menus", format!("unknown colour `{c}` ({})", COLORS.join("|")))); - } - } - } - let (hide, show) = (ids("hide"), ids("show")); - if let Some(id) = hide.iter().find(|id| !MenuCustomization::hideable(id)) { - return Err(bad("edit.menus", format!("`{id}` can't be hidden"))); - } - s.edit_prefs(|pr| { - if p.get("reset").and_then(Value::as_bool) == Some(true) { - pr.menus = MenuCustomization::default(); - } - for id in hide { - if !pr.menus.hidden.contains(&id) { - pr.menus.hidden.push(id); - } - } - pr.menus.hidden.retain(|h| !show.contains(h)); - if let Some(m) = p.get("color").and_then(Value::as_object) { - for (id, v) in m { - match v.as_str() { - Some(c) if c != "none" => { - pr.menus.colors.insert(id.clone(), c.to_string()); - } - _ => { - pr.menus.colors.remove(id); - } - } - } - } - }); - Ok(serde_json::to_value(&s.prefs().menus).unwrap_or_default()) -} - -/// Edit › Toolbar: hide tools and reorder the toolbar. -fn toolbar(s: &mut Session, p: &Value) -> Result { - let ids = |k: &str| -> Option> { p.get(k).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()) }; - s.edit_prefs(|pr| { - if p.get("reset").and_then(Value::as_bool) == Some(true) { - pr.toolbar = ToolbarCustomization::default(); - } - if let Some(h) = ids("hidden") { - pr.toolbar.hidden = h; - } - if let Some(o) = ids("order") { - pr.toolbar.order = o; - } - }); - Ok(serde_json::to_value(&s.prefs().toolbar).unwrap_or_default()) -} - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $run:expr, $journal:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: always, run: $run, journal: $journal } - }; -} - -macro_rules! section { - ($id:literal, $label:literal) => { - section!($id, $label, None) - }; - ($id:literal, $label:literal, $shortcut:expr) => { - CommandSpec { id: $id, label: $label, menu: &["Edit", "Preferences"], shortcut: $shortcut, params: r##"{}"##, enabled: always, run: |s, _| preferences_section(s, &json!({"__section": section_of($id)})), journal: false } - }; -} - -fn section_of(id: &str) -> &str { - id.rsplit('.').next().unwrap_or("general") -} - -/// Preference command specs. -pub fn specs() -> Vec { - vec![ - spec!( - "prefs.get", - "Get Preferences", - [], - None, - r##"{"path":"section.key"?=everything (e.g. "performance.historyStates", "colorSettings.workingRgb")}"##, - prefs_get, - false - ), - spec!( - "prefs.set", - "Set Preferences", - [], - None, - r##"{"path":"section.key","value":json} or {"values":{"section.key":json,…}} (validated; all or nothing)"##, - prefs_set, - false - ), - spec!("prefs.reset", "Reset Preferences", [], None, r##"{"path":"section|section.key"?=everything}"##, prefs_reset, false), - // Photoshop: ⌘K opens Preferences › General (⌘, is Layer › Hide Layers). - section!("edit.preferences.general", "General…", Some("Cmd+K")), - section!("edit.preferences.interface", "Interface…"), - section!("edit.preferences.workspace", "Workspace…"), - section!("edit.preferences.tools", "Tools…"), - section!("edit.preferences.historyLog", "History Log…"), - section!("edit.preferences.fileHandling", "File Handling…"), - section!("edit.preferences.export", "Export…"), - section!("edit.preferences.performance", "Performance…"), - section!("edit.preferences.scratchDisks", "Scratch Disks…"), - section!("edit.preferences.cursors", "Cursors…"), - section!("edit.preferences.transparencyAndGamut", "Transparency & Gamut…"), - section!("edit.preferences.unitsAndRulers", "Units & Rulers…"), - section!("edit.preferences.guidesGridAndSlices", "Guides, Grid & Slices…"), - section!("edit.preferences.plugIns", "Plug-ins…"), - section!("edit.preferences.type", "Type…"), - section!("edit.preferences.enhancedControls", "Enhanced Controls…"), - section!("edit.preferences.rawDefaults", "Camera Raw…"), - section!("edit.preferences.integrations", "Integrations…"), - spec!( - "edit.keyboardShortcuts", - "Keyboard Shortcuts…", - ["Edit"], - Some("Cmd+Alt+Shift+K"), - r##"{"set":{"|tools.temporary.hand|zoomIn|zoomOut":"Cmd+Shift+X"|""(remove)|null(default)}?,"alternates":{"":["F3",…]|[](none)|null(default)}?,"reset":true|["",…]?,"removeConflicts":bool=true,"filter":str?,"list":bool=false,"importKys":"<.kys XML>"|{"path":".kys"}?,"useSet":""|""(defaults)?,"saveSet":""?,"deleteSet":""?,"sets":{"":{"shortcuts":{…},"alternateShortcuts":{…}}}?,"activeSet":""|""?,"exportKys":true|{"path":".kys"}?,"summarize":true|{"mac":bool,"path":".html"}?} (with a path the file is written and the reply has kysPath/htmlPath instead of kys/html)"##, - keyboard_shortcuts, - true - ), - spec!( - "edit.menus", - "Menus…", - ["Edit"], - Some("Cmd+Alt+Shift+M"), - r##"{"hide":["",…]?,"show":["",…]?,"color":{"":"red|orange|yellow|green|blue|violet|gray|none"}?,"reset":bool=false}"##, - menus, - true - ), - spec!("edit.toolbar", "Toolbar…", ["Edit"], None, r##"{"hidden":["",…]?,"order":["",…]?,"reset":bool=false}"##, toolbar, true), - ] -} - #[cfg(test)] #[path = "prefs/tests.rs"] mod tests; + +#[cfg(test)] +mod color_settings_path_tests { + use crate::Session; + use serde_json::json; + + #[test] + fn a_path_that_only_starts_with_color_settings_is_not_in_that_section() { + let mut s = Session::new(); + let before = s.color.settings.clone(); + for path in ["colorSettingsworkingRgb", "colorSettings.", "colorSettings..workingRgb"] { + assert!(s.execute("prefs.set", json!({"path": path, "value": "/dev/zero"})).is_err(), "{path}"); + } + assert_eq!(s.color.settings, before); + } +} diff --git a/crates/engine/src/prefs/cmds.rs b/crates/engine/src/prefs/cmds.rs new file mode 100644 index 0000000000..982bf209ab --- /dev/null +++ b/crates/engine/src/prefs/cmds.rs @@ -0,0 +1,197 @@ +use super::customize::{keyboard_shortcuts, menus}; +use super::validate::{SECTIONS, get_path, keyed}; +use super::{CommandSpec, EngineError, Map, Result, Session, ToolbarCustomization, Value, json}; + +// ------------------------------------------------------------------ commands + +fn always(_: &Session) -> std::result::Result<(), String> { + Ok(()) +} + +pub(super) fn bad(cmd: &str, msg: impl Into) -> EngineError { + EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } +} + +fn prefs_get(s: &mut Session, p: &Value) -> Result { + let path = p.get("path").and_then(Value::as_str).unwrap_or(""); + let v = s.prefs_view(); + get_path(&v, path).cloned().ok_or_else(|| bad("prefs.get", format!("unknown preference `{path}`"))) +} + +fn prefs_set(s: &mut Session, p: &Value) -> Result { + let cmd = "prefs.set"; + let mut changes: Vec<(String, Value)> = Vec::new(); + if let Some(path) = p.get("path").and_then(Value::as_str) { + changes.push((path.to_string(), p.get("value").cloned().ok_or_else(|| bad(cmd, "missing `value`"))?)); + } + if let Some(m) = p.get("values").and_then(Value::as_object) { + changes.extend(m.iter().map(|(k, v)| (k.clone(), v.clone()))); + } + if changes.is_empty() { + return Err(bad(cmd, "give `path` and `value`, or `values: {path: value, …}`")); + } + // All or nothing: validate against a scratch copy first. + let (saved_prefs, saved_color) = (s.prefs().clone(), s.color.settings.clone()); + let mut color_changed = false; + for (path, v) in &changes { + match s.set_pref(path, v.clone(), false) { + Ok(touched) => color_changed |= touched, + Err(e) => { + s.prefs.edit(|p| *p = saved_prefs); + s.color.settings = saved_color; + return Err(bad(cmd, e)); + } + } + } + if color_changed && let Err(e) = crate::color_cmds::validate_settings(&s.color.settings) { + s.prefs.edit(|p| *p = saved_prefs); + s.color.settings = saved_color; + return Err(bad(cmd, e)); + } + // `colorSettings.blendTextGamma` lives in the compositor too, as `edit.colorSettings` sets it. + photocraft_compose::psblend::set_text_gamma(s.color.settings.blend_text_gamma); + s.apply_prefs(); + let view = s.prefs_view(); + let out: Map = changes.iter().map(|(k, _)| (k.clone(), get_path(&view, k).cloned().unwrap_or(Value::Null))).collect(); + Ok(Value::Object(out)) +} + +fn prefs_reset(s: &mut Session, p: &Value) -> Result { + let path = p.get("path").or_else(|| p.get("section")).and_then(Value::as_str); + match path { + Some("colorSettings") => s.color.settings = Default::default(), + // One colour setting: `Preferences` has no `colorSettings`, so take the default + // from `ColorSettings` and route it like `prefs.set` does. + Some(path) if path.starts_with("colorSettings.") => { + let defaults = serde_json::to_value(crate::color_cmds::ColorSettings::default()).map_err(|e| bad("prefs.reset", e.to_string()))?; + let key = path.strip_prefix("colorSettings.").unwrap_or(path); + let def = get_path(&defaults, key).cloned().ok_or_else(|| bad("prefs.reset", format!("unknown preference `{path}`")))?; + s.set_pref(path, def, true).map_err(|e| bad("prefs.reset", e))?; + } + None => { + s.color.settings = Default::default(); + s.edit_prefs(|p| p.reset(None)).map_err(|e| bad("prefs.reset", e))?; + } + Some(path) => { + let mut next = s.prefs().clone(); + next.reset(Some(path)).map_err(|e| bad("prefs.reset", e))?; + s.edit_prefs(|p| *p = next); + } + } + s.prefs.edit(|_| ()); + photocraft_compose::psblend::set_text_gamma(s.color.settings.blend_text_gamma); + s.apply_prefs(); + if path.is_some_and(|path| keyed(path).is_some()) { + // The removed override is absent, so reading its old path would report an error. + Ok(Value::Null) + } else { + prefs_get(s, &json!({"path": path.unwrap_or("")})) + } +} + +/// `edit.preferences.
`: the section's values (the GUI opens the dialog on it instead). +fn preferences_section(s: &mut Session, p: &Value) -> Result { + let section = p.get("__section").and_then(Value::as_str).unwrap_or("general").to_string(); + let title = SECTIONS.iter().find(|(id, _)| *id == section).map_or("General", |(_, t)| *t); + let values = s.prefs().get(§ion).unwrap_or(Value::Null); + Ok(json!({"section": section, "title": title, "values": values})) +} + +/// Edit › Toolbar: hide tools and reorder the toolbar. +fn toolbar(s: &mut Session, p: &Value) -> Result { + let ids = |k: &str| -> Option> { p.get(k).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()) }; + s.edit_prefs(|pr| { + if p.get("reset").and_then(Value::as_bool) == Some(true) { + pr.toolbar = ToolbarCustomization::default(); + } + if let Some(h) = ids("hidden") { + pr.toolbar.hidden = h; + } + if let Some(o) = ids("order") { + pr.toolbar.order = o; + } + }); + Ok(serde_json::to_value(&s.prefs().toolbar).unwrap_or_default()) +} + +macro_rules! spec { + ($id:literal, $label:literal, [$($m:literal),*], $sc:expr, $params:literal, $run:expr, $journal:expr) => { + CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: $sc, params: $params, enabled: always, run: $run, journal: $journal } + }; +} + +macro_rules! section { + ($id:literal, $label:literal) => { + section!($id, $label, None) + }; + ($id:literal, $label:literal, $shortcut:expr) => { + CommandSpec { id: $id, label: $label, menu: &["Edit", "Preferences"], shortcut: $shortcut, params: r##"{}"##, enabled: always, run: |s, _| preferences_section(s, &json!({"__section": section_of($id)})), journal: false } + }; +} + +fn section_of(id: &str) -> &str { + id.rsplit('.').next().unwrap_or("general") +} + +/// Preference command specs. +pub fn specs() -> Vec { + vec![ + spec!( + "prefs.get", + "Get Preferences", + [], + None, + r##"{"path":"section.key"?=everything (e.g. "performance.historyStates", "colorSettings.workingRgb")}"##, + prefs_get, + false + ), + spec!( + "prefs.set", + "Set Preferences", + [], + None, + r##"{"path":"section.key","value":json} or {"values":{"section.key":json,…}} (validated; all or nothing)"##, + prefs_set, + false + ), + spec!("prefs.reset", "Reset Preferences", [], None, r##"{"path":"section|section.key"?=everything}"##, prefs_reset, false), + // Photoshop: ⌘K opens Preferences › General (⌘, is Layer › Hide Layers). + section!("edit.preferences.general", "General…", Some("Cmd+K")), + section!("edit.preferences.interface", "Interface…"), + section!("edit.preferences.workspace", "Workspace…"), + section!("edit.preferences.tools", "Tools…"), + section!("edit.preferences.historyLog", "History Log…"), + section!("edit.preferences.fileHandling", "File Handling…"), + section!("edit.preferences.export", "Export…"), + section!("edit.preferences.performance", "Performance…"), + section!("edit.preferences.scratchDisks", "Scratch Disks…"), + section!("edit.preferences.cursors", "Cursors…"), + section!("edit.preferences.transparencyAndGamut", "Transparency & Gamut…"), + section!("edit.preferences.unitsAndRulers", "Units & Rulers…"), + section!("edit.preferences.guidesGridAndSlices", "Guides, Grid & Slices…"), + section!("edit.preferences.plugIns", "Plug-ins…"), + section!("edit.preferences.type", "Type…"), + section!("edit.preferences.enhancedControls", "Enhanced Controls…"), + section!("edit.preferences.rawDefaults", "Camera Raw…"), + section!("edit.preferences.integrations", "Integrations…"), + spec!( + "edit.keyboardShortcuts", + "Keyboard Shortcuts…", + ["Edit"], + Some("Cmd+Alt+Shift+K"), + r##"{"set":{"|tools.temporary.hand|zoomIn|zoomOut":"Cmd+Shift+X"|""(remove)|null(default)}?,"alternates":{"":["F3",…]|[](none)|null(default)}?,"reset":true|["",…]?,"removeConflicts":bool=true,"filter":str?,"list":bool=false,"importKys":"<.kys XML>"|{"path":".kys"}?,"useSet":""|""(defaults)?,"saveSet":""?,"deleteSet":""?,"sets":{"":{"shortcuts":{…},"alternateShortcuts":{…}}}?,"activeSet":""|""?,"exportKys":true|{"path":".kys"}?,"summarize":true|{"mac":bool,"path":".html"}?} (with a path the file is written and the reply has kysPath/htmlPath instead of kys/html)"##, + keyboard_shortcuts, + true + ), + spec!( + "edit.menus", + "Menus…", + ["Edit"], + Some("Cmd+Alt+Shift+M"), + r##"{"hide":["",…]?,"show":["",…]?,"color":{"":"red|orange|yellow|green|blue|violet|gray|none"}?,"reset":bool=false}"##, + menus, + true + ), + spec!("edit.toolbar", "Toolbar…", ["Edit"], None, r##"{"hidden":["",…]?,"order":["",…]?,"reset":bool=false}"##, toolbar, true), + ] +} diff --git a/crates/engine/src/prefs/customize.rs b/crates/engine/src/prefs/customize.rs new file mode 100644 index 0000000000..bfad131ba1 --- /dev/null +++ b/crates/engine/src/prefs/customize.rs @@ -0,0 +1,317 @@ +use super::cmds::bad; +use super::files::{file_target, read_text_file, rename_path, writable, write_files_atomically}; +use super::shortcuts::{BindKind, alternate_keys, bindable, bindable_items, conflicts, default_alternates, is_tool, normalize_shortcut, reserved_shortcut}; +use super::{MenuCustomization, Preferences, Result, Session, Value, json}; + +/// Edit › Keyboard Shortcuts (engine part): list, change and reset application shortcuts. +pub(super) fn keyboard_shortcuts(s: &mut Session, p: &Value) -> Result { + let cmd = "edit.keyboardShortcuts"; + // Every change lands at once, after all of it validated: a bad key leaves the set as it was. + let mut next = s.prefs().clone(); + // A set loads first, so the call's other changes apply on top of it. + let mut changed = crate::shortcut_sets::load(&mut next, p).map_err(|e| bad(cmd, e))?; + if let Some(r) = p.get("reset") { + match r { + Value::Bool(true) => { + next.shortcuts.clear(); + next.alternate_shortcuts.clear(); + changed = true; + } + Value::Array(ids) => { + for id in ids.iter().filter_map(Value::as_str) { + next.shortcuts.remove(id); + next.alternate_shortcuts.remove(id); + } + changed = true; + } + _ => {} + } + } + // A Photoshop `.kys` set (`importKys`, its XML text) becomes `set` and `alternates` entries, + // matched by label (`crate::kys`); a key equal to the command's default restores the + // default, and keys given in `set` / `alternates` as well win over the file's. + let imported; + let mut import = None; + let p = match p.get("importKys") { + Some(v) => { + // The file's text, or {"path": …} to read it. + let read; + let xml = match v { + Value::String(s) => s.as_str(), + Value::Object(o) => { + let path = + o.get("path").and_then(Value::as_str).filter(|s| !s.trim().is_empty()).ok_or_else(|| bad(cmd, "`importKys.path` is a .kys file"))?; + read = read_text_file(path, crate::kys::MAX_BYTES).map_err(|e| bad(cmd, format!("{path}: {e}")))?; + read.as_str() + } + _ => return Err(bad(cmd, "`importKys` is the text of a .kys file, or {\"path\": …}")), + }; + let set = crate::kys::parse(xml).map_err(|e| bad(cmd, e))?; + let plan = crate::kys::plan(crate::kys::command_candidates(), &set); + let mut merged = serde_json::Map::new(); + // A key the system reserves is left out (reported as `reserved`), not a failed import. + let mut skipped: Vec = Vec::new(); + for (id, sc) in plan.set.iter().filter(|(id, sc)| { + let bad = reserved_shortcut(sc); + if bad { + skipped.push(format!("{id}: {sc}")); + } + !bad + }) { + let default = crate::command_specs().iter().find(|c| c.id == id).and_then(|c| c.shortcut).and_then(normalize_shortcut); + merged.insert(id.clone(), if default.as_deref() == Some(sc.as_str()) { Value::Null } else { json!(sc) }); + } + // Tool keys: a tool's own letter restores it (and its Shift cycling). + let tools = crate::shell_commands::TOOLS.iter().map(|t| (t.0, t.1)); + let (tool_keys, unknown_tools) = crate::kys::plan_tools(tools, &set); + for (id, key) in &tool_keys { + let own = crate::shell_commands::tool_default(id).flatten().is_some_and(|d| d.eq_ignore_ascii_case(key)); + merged.insert(id.clone(), if own { Value::Null } else { json!(key) }); + } + merged.extend(p.get("set").and_then(Value::as_object).cloned().unwrap_or_default()); + let mut alternates = serde_json::Map::new(); + for (id, keys) in &plan.alternate_keys { + let keys: &Vec = &keys.iter().filter(|k| !reserved_shortcut(k)).cloned().collect(); + let main = plan.set.get(id).map(String::as_str); + let default = Preferences::default().alternates(id, main); + alternates.insert(id.clone(), if *keys == default { Value::Null } else { json!(keys) }); + } + alternates.extend(p.get("alternates").and_then(Value::as_object).cloned().unwrap_or_default()); + import = Some(json!({ + "name": set.name, + "imported": plan.set.len(), + "set": plan.set, + "unknown": plan.unknown, + "unreadable": plan.unreadable, + "alternates": plan.alternates, + "alternateKeys": plan.alternate_keys, + "toolKeys": set.tool_keys, + "tools": tool_keys, + "unknownTools": unknown_tools, + "reserved": skipped, + "windows": set.windows, + })); + let mut with_import = p.clone(); + with_import["set"] = Value::Object(merged); + with_import["alternates"] = Value::Object(alternates); + imported = with_import; + &imported + } + None => p, + }; + let empty = serde_json::Map::new(); + let set = match p.get("set") { + Some(Value::Object(m)) => m, + _ => &empty, + }; + let alts = match p.get("alternates") { + None | Some(Value::Null) => &empty, + Some(v) => v.as_object().ok_or_else(|| bad(cmd, "`alternates` maps command ids to lists of shortcuts"))?, + }; + changed |= apply_shortcut_changes(&mut next, p, set, alts, cmd)?; + // Then the sets are saved, deleted or switched, from the bindings as they now stand. + changed |= crate::shortcut_sets::store(&mut next, p).map_err(|e| bad(cmd, e))?; + // The reply and the exports read the bindings as they will be; they are committed only once + // the exports are written, so a bad export target changes nothing. + let prefs = &next; + let filter = p.get("filter").and_then(Value::as_str).map(str::to_ascii_lowercase); + let list: Vec = bindable_items() + .filter(|c| filter.as_ref().is_none_or(|f| c.0.to_ascii_lowercase().contains(f) || c.1.to_ascii_lowercase().contains(f))) + .filter(|c| { + p.get("list").and_then(Value::as_bool).unwrap_or(false) + || filter.is_some() + || prefs.shortcuts.contains_key(c.0) + || prefs.alternate_shortcuts.contains_key(c.0) + }) + .map(|(id, label, menu, def, kind)| { + let main = prefs.shortcut(id, def); + let defaults: Vec<&str> = default_alternates(id).collect(); + let mut v = json!({"id": id, "label": label, "menu": menu, "default": def, "shortcut": main, "alternates": prefs.alternates(id, main)}); + if !defaults.is_empty() { + v["defaultAlternates"] = json!(defaults); + } + match kind { + BindKind::Hold => v["hold"] = json!(true), + BindKind::Tool => v["tool"] = json!(true), + BindKind::Command => {} + } + v + }) + .collect(); + let mut bindings: Vec<(&str, String)> = Vec::new(); + for (id, def) in bindable() { + let main = prefs.shortcut(id, def); + bindings.extend(main.map(|m| (id, m.to_string()))); + bindings.extend(prefs.alternates(id, main).into_iter().map(|k| (id, k))); + } + // Tools sharing a key are a cycling group, not a conflict. + let conflicts: Vec = conflicts(bindings.iter().map(|(id, k)| (*id, k.as_str()))) + .into_iter() + .filter(|(_, ids)| !ids.iter().all(|id| is_tool(id))) + .map(|(sc, ids)| json!({"shortcut": sc, "commands": ids})) + .collect(); + let mut out = json!({"overrides": prefs.shortcuts, "alternateOverrides": prefs.alternate_shortcuts, "commands": list, "conflicts": conflicts}); + if let (Some(o), Value::Object(sets)) = (out.as_object_mut(), crate::shortcut_sets::describe(prefs)) { + o.extend(sets); + } + // The bindings as a `.kys` file, or Photoshop's Summarize page: every bindable item (the + // engine's commands, the shell's commands and the tools). + let set_name = if prefs.shortcut_set.is_empty() { crate::shortcut_sets::DEFAULTS_NAME } else { prefs.shortcut_set.as_str() }; + // Both targets are resolved and their texts made before anything is written, and a path that + // can't be written is refused before either file is: a bad export changes nothing. + let kys_target = file_target(cmd, p.get("exportKys"))?; + let html_target = file_target(cmd, p.get("summarize"))?; + let mut files: Vec<(&str, String, String)> = Vec::new(); + if let Some(target) = kys_target { + let entries: Vec = + crate::shortcut_sets::bound(prefs).into_iter().map(|b| crate::kys::Entry { id: b.id, label: b.label, tool: b.tool, keys: b.keys }).collect(); + let text = crate::kys::write(set_name, &entries); + match target { + Some(path) => files.push(("kysPath", path, text)), + None => out["kys"] = json!(text), + } + } + if let Some(target) = html_target { + let v = p.get("summarize").cloned().unwrap_or(Value::Null); + let mac = v.get("mac").and_then(Value::as_bool).unwrap_or(cfg!(target_os = "macos")); + let mut sections: Vec = Vec::new(); + for crate::shortcut_sets::Bound { label, menu, keys, .. } in crate::shortcut_sets::bound(prefs) { + match sections.iter_mut().find(|(m, _)| *m == menu) { + Some((_, rows)) => rows.push((label, keys)), + None => sections.push((menu, vec![(label, keys)])), + } + } + let html = crate::shortcut_sets::summarize(set_name, §ions, mac); + match target { + Some(path) => files.push(("htmlPath", path, html)), + None => out["html"] = json!(html), + } + } + for (_, path, _) in &files { + writable(path).map_err(|e| bad(cmd, format!("{path}: {e}")))?; + } + let list: Vec<(&str, &str)> = files.iter().map(|(_, path, text)| (path.as_str(), text.as_str())).collect(); + write_files_atomically(&list, &rename_path).map_err(|e| bad(cmd, e))?; + for (key, path, _) in &files { + out[*key] = json!(path); + } + if changed { + s.edit_prefs(|p| *p = next); + } + if let Some(import) = import { + out["import"] = import; + } + Ok(out) +} + +fn apply_shortcut_changes( + next: &mut Preferences, + p: &Value, + set: &serde_json::Map, + alts: &serde_json::Map, + cmd: &str, +) -> Result { + if set.is_empty() && alts.is_empty() { + return Ok(false); + } + let allow_unknown = p.get("allowUnknown").and_then(Value::as_bool).unwrap_or(false); + // Reserved system keys can't be bound, by hand or from a file. + let reserved = |v: &Value| -> Option { + let keys = match v { + Value::String(s) => vec![s.clone()], + other => alternate_keys(other).unwrap_or_default(), + }; + keys.into_iter().find(|k| reserved_shortcut(k)) + }; + if let Some((id, k)) = set.iter().chain(alts.iter()).find_map(|(id, v)| reserved(v).map(|k| (id, k))) { + return Err(bad(cmd, format!("`{k}` (for `{id}`) is reserved by the system and can't be assigned"))); + } + for (id, v) in set { + if !bindable().any(|(b, _)| b == id) && !allow_unknown { + return Err(bad(cmd, format!("unknown command `{id}`"))); + } + next.set(&format!("shortcuts.{id}"), v.clone()).map_err(|e| bad(cmd, e))?; + } + for (id, v) in alts { + if !bindable().any(|(b, _)| b == id) && default_alternates(id).next().is_none() && !allow_unknown { + return Err(bad(cmd, format!("unknown command `{id}`"))); + } + next.set(&format!("alternateShortcuts.{id}"), v.clone()).map_err(|e| bad(cmd, e))?; + } + // Like Photoshop, a shortcut moved to a new command is taken away from the old one, + // whether the old one held it as its main key or as an alternate (#3665). + if p.get("removeConflicts").and_then(Value::as_bool).unwrap_or(true) { + let assigned: Vec<(&str, String)> = set + .iter() + .filter_map(|(id, v)| Some((id.as_str(), v.as_str().and_then(normalize_shortcut)?))) + .chain(alts.iter().flat_map(|(id, v)| alternate_keys(v).unwrap_or_default().into_iter().map(move |k| (id.as_str(), k)))) + .collect(); + for (id, sc) in assigned { + for (c, def) in bindable() { + // Tools may share a key: they cycle with Shift, as Photoshop's groups do. + if is_tool(id) && is_tool(c) { + continue; + } + // Never strip a command that this same call is assigning: + // two entries for one key are a clash inside the call, which + // the returned conflicts list reports (#719). Stripping + // each other here unbound both silently. + if c == id || set.contains_key(c) || alts.contains_key(c) { + continue; + } + let main = next.shortcut(c, def).map(str::to_string); + if main.as_deref().and_then(normalize_shortcut).as_deref() == Some(sc.as_str()) { + next.shortcuts.insert(c.to_string(), String::new()); + } + let held = next.alternates(c, main.as_deref()); + if held.contains(&sc) { + next.alternate_shortcuts.insert(c.to_string(), held.into_iter().filter(|k| *k != sc).collect()); + } + } + } + } + Ok(true) +} + +/// Edit › Menus: hide/show items and give them colours. +pub(super) fn menus(s: &mut Session, p: &Value) -> Result { + let ids = |k: &str| -> Vec { + p.get(k).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).map(str::to_string).collect()).unwrap_or_default() + }; + const COLORS: [&str; 8] = ["none", "red", "orange", "yellow", "green", "blue", "violet", "gray"]; + if let Some(m) = p.get("color").and_then(Value::as_object) { + for v in m.values() { + if let Some(c) = v.as_str().filter(|c| !COLORS.contains(c)) { + return Err(bad("edit.menus", format!("unknown colour `{c}` ({})", COLORS.join("|")))); + } + } + } + let (hide, show) = (ids("hide"), ids("show")); + if let Some(id) = hide.iter().find(|id| !MenuCustomization::hideable(id)) { + return Err(bad("edit.menus", format!("`{id}` can't be hidden"))); + } + s.edit_prefs(|pr| { + if p.get("reset").and_then(Value::as_bool) == Some(true) { + pr.menus = MenuCustomization::default(); + } + for id in hide { + if !pr.menus.hidden.contains(&id) { + pr.menus.hidden.push(id); + } + } + pr.menus.hidden.retain(|h| !show.contains(h)); + if let Some(m) = p.get("color").and_then(Value::as_object) { + for (id, v) in m { + match v.as_str() { + Some(c) if c != "none" => { + pr.menus.colors.insert(id.clone(), c.to_string()); + } + _ => { + pr.menus.colors.remove(id); + } + } + } + } + }); + Ok(serde_json::to_value(&s.prefs().menus).unwrap_or_default()) +} diff --git a/crates/engine/src/prefs/files.rs b/crates/engine/src/prefs/files.rs new file mode 100644 index 0000000000..c58604e71d --- /dev/null +++ b/crates/engine/src/prefs/files.rs @@ -0,0 +1,199 @@ +use super::cmds::bad; +use super::{Result, Value}; + +/// An output parameter (`exportKys`, `summarize`): absent or false → `None`; true (or an object +/// without `path`) → `Some(None)`, the text comes back in the reply; `{"path": …}` → +/// `Some(Some(path))`, written there. +pub(super) fn file_target(cmd: &str, v: Option<&Value>) -> Result>> { + match v { + None | Some(Value::Null) | Some(Value::Bool(false)) => Ok(None), + Some(Value::Bool(true)) => Ok(Some(None)), + Some(Value::Object(o)) => match o.get("path") { + None | Some(Value::Null) => Ok(Some(None)), + Some(Value::String(s)) if !s.trim().is_empty() => Ok(Some(Some(s.clone()))), + Some(_) => Err(bad(cmd, "`path` must be a non-empty string")), + }, + Some(_) => Err(bad(cmd, "expected true or {\"path\": …}")), + } +} + +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn read_text_file(path: &str, max: usize) -> std::result::Result { + let meta = std::fs::metadata(path).map_err(|e| e.to_string())?; + if !meta.is_file() || meta.len() > max as u64 { + return Err(format!("not a file of at most {} KiB", max / 1024)); + } + std::fs::read_to_string(path).map_err(|e| e.to_string()) +} + +#[cfg(target_arch = "wasm32")] +pub(super) fn read_text_file(_: &str, _: usize) -> std::result::Result { + Err("paths can't be read on the web; pass the file's text instead".into()) +} + +/// Whether `path` can be written: not a folder, in a folder that exists. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn writable(path: &str) -> std::result::Result<(), String> { + let p = std::path::Path::new(path); + if p.is_dir() { + return Err("is a folder".into()); + } + if std::fs::metadata(p).is_ok_and(|m| m.permissions().readonly()) { + return Err("is read-only".into()); + } + match p.parent().filter(|d| !d.as_os_str().is_empty()) { + Some(d) if !d.is_dir() => Err("its folder doesn't exist".into()), + _ => Ok(()), + } +} + +#[cfg(target_arch = "wasm32")] +pub(super) fn writable(_: &str) -> std::result::Result<(), String> { + Err("paths can't be written on the web; omit `path` to get the text back".into()) +} + +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn rename_path(from: &std::path::Path, to: &std::path::Path) -> std::io::Result<()> { + std::fs::rename(from, to) +} + +#[cfg(target_arch = "wasm32")] +pub(super) fn rename_path(_: &std::path::Path, _: &std::path::Path) -> std::io::Result<()> { + Err(std::io::Error::other("paths can't be written on the web")) +} + +/// Where `path` points, for telling whether two targets are one file: its folder resolved, its +/// name compared without case where the file system ignores it. +#[cfg(not(target_arch = "wasm32"))] +fn path_key(path: &str) -> String { + let p = std::path::Path::new(path); + let dir = p.parent().filter(|d| !d.as_os_str().is_empty()).unwrap_or(std::path::Path::new(".")); + let dir = dir.canonicalize().unwrap_or_else(|_| dir.to_path_buf()); + let key = dir.join(p.file_name().unwrap_or_default()).to_string_lossy().into_owned(); + if cfg!(any(target_os = "windows", target_os = "macos")) { key.to_lowercase() } else { key } +} + +/// A new, empty file beside `path` with a name nothing else uses (`create_new`), for staging +/// (`kind` "tmp") or a backup ("bak"). +#[cfg(not(target_arch = "wasm32"))] +fn sibling(path: &str, kind: &str) -> std::io::Result<(std::path::PathBuf, std::fs::File)> { + for n in 0..1000u32 { + let p = std::path::PathBuf::from(format!("{path}.{}-{n}.photocraft-{kind}", std::process::id())); + match std::fs::OpenOptions::new().write(true).create_new(true).open(&p) { + Ok(f) => return Ok((p, f)), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(e) => return Err(e), + } + } + Err(std::io::Error::other("no free name beside it")) +} + +/// Write `files` ((path, text)) all or nothing. Each text is staged in a new file beside its +/// target; once every one is written, each existing target is moved aside to a backup and the +/// staged file moved in. Any failure moves the backups back and removes what was staged, so the +/// user's files end as they began; on success the backups go. Two targets naming one file are +/// refused. `rename` is `std::fs::rename` (a test fails it on purpose). +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn write_files_atomically( + files: &[(&str, &str)], + rename: &dyn Fn(&std::path::Path, &std::path::Path) -> std::io::Result<()>, +) -> std::result::Result<(), String> { + use std::io::Write; + use std::path::{Path, PathBuf}; + for (i, (a, _)) in files.iter().enumerate() { + if files.iter().skip(i + 1).any(|(b, _)| path_key(a) == path_key(b)) { + return Err(format!("{a}: two exports to the same file")); + } + } + let mut staged: Vec = Vec::new(); + let drop_all = |paths: &[PathBuf]| { + for p in paths { + let _ = std::fs::remove_file(p); + } + }; + for (path, text) in files { + let (tmp, mut f) = match sibling(path, "tmp") { + Ok(v) => v, + Err(e) => { + drop_all(&staged); + return Err(format!("{path}: {e}")); + } + }; + staged.push(tmp); + if let Err(e) = f.write_all(text.as_bytes()).and_then(|()| f.sync_all()) { + drop_all(&staged); + return Err(format!("{path}: {e}")); + } + } + // (target, its backup) of each file moved in so far. + let mut moved: Vec<(&Path, Option)> = Vec::new(); + // Put each backup back over its target (a target that had none is removed). What couldn't be + // put back is named, so the user can find their previous file. + let undo = |moved: &[(&Path, Option)]| -> String { + let mut stranded = Vec::new(); + for (target, backup) in moved.iter().rev() { + match backup { + Some(b) => { + if rename(b, target).is_err() { + stranded.push(format!("the previous {} is at {}", target.display(), b.display())); + } + } + None => { + let _ = std::fs::remove_file(target); + } + } + } + if stranded.is_empty() { String::new() } else { format!(" ({})", stranded.join("; ")) } + }; + for ((path, _), tmp) in files.iter().zip(&staged) { + let target = Path::new(*path); + let backup = if target.exists() { + let moved_aside = sibling(path, "bak").and_then(|(b, f)| { + drop(f); + match rename(target, &b) { + Ok(()) => Ok(b), + Err(e) => { + // The placeholder the backup name reserved goes too. + let _ = std::fs::remove_file(&b); + Err(e) + } + } + }); + match moved_aside { + Ok(b) => Some(b), + Err(e) => { + let stranded = undo(&moved); + drop_all(&staged); + return Err(format!("{path}: {e}{stranded}")); + } + } + } else { + None + }; + if let Err(e) = rename(tmp, target) { + let mut stranded = undo(&moved); + if let Some(b) = &backup + && rename(b, target).is_err() + { + stranded = format!("{stranded} (the previous {path} is at {})", b.display()); + } + drop_all(&staged); + return Err(format!("{path}: {e}{stranded}")); + } + moved.push((target, backup)); + } + for (_, backup) in moved { + if let Some(b) = backup { + let _ = std::fs::remove_file(b); + } + } + Ok(()) +} + +#[cfg(target_arch = "wasm32")] +pub(crate) fn write_files_atomically( + files: &[(&str, &str)], + _: &dyn Fn(&std::path::Path, &std::path::Path) -> std::io::Result<()>, +) -> std::result::Result<(), String> { + if files.is_empty() { Ok(()) } else { Err("paths can't be written on the web; omit `path` to get the text back".into()) } +} diff --git a/crates/engine/src/prefs/shortcuts.rs b/crates/engine/src/prefs/shortcuts.rs new file mode 100644 index 0000000000..337c8e3754 --- /dev/null +++ b/crates/engine/src/prefs/shortcuts.rs @@ -0,0 +1,213 @@ +use super::{BTreeMap, Value}; + +/// Most further keys one command may hold; Photoshop's own defaults use one. +pub const MAX_ALTERNATES: usize = 8; + +/// Validate and normalise a command's alternate keys: a list of shortcut strings (a single +/// string is a one-key list, `""` none). +pub fn alternate_keys(v: &Value) -> std::result::Result, String> { + let items: Vec<&Value> = match v { + Value::String(s) if s.is_empty() => Vec::new(), + Value::String(_) => vec![v], + Value::Array(a) => a.iter().collect(), + _ => return Err("alternate shortcuts are a list of shortcuts (e.g. [\"F3\"])".into()), + }; + if items.len() > MAX_ALTERNATES { + return Err(format!("at most {MAX_ALTERNATES} alternate shortcuts (got {})", items.len())); + } + let mut out: Vec = Vec::with_capacity(items.len()); + for item in items { + let s = item.as_str().ok_or_else(|| format!("alternate shortcut `{item}` must be a string"))?; + let n = normalize_shortcut(s).ok_or_else(|| format!("`{s}` is not a valid shortcut (e.g. Cmd+Shift+N, F7)"))?; + if !out.contains(&n) { + out.push(n); + } + } + Ok(out) +} + +// ------------------------------------------------------------------ shortcuts + +/// Canonical spelling of a shortcut: modifiers in `Cmd+Ctrl+Alt+Shift` order, key last +/// (single characters upper-cased). `None` if it has no key or an unknown modifier. +pub fn normalize_shortcut(s: &str) -> Option { + let s = s.trim(); + if s.is_empty() { + return None; + } + let (mut cmd, mut ctrl, mut alt, mut shift) = (false, false, false, false); + let mut key: Option = None; + // A trailing "+" key is spelled "Cmd++". + let (body, plus_key) = if s.ends_with("++") || s == "+" { (s.trim_end_matches('+'), true) } else { (s, false) }; + for part in body.split('+').filter(|p| !p.is_empty()) { + match part.to_ascii_lowercase().as_str() { + "cmd" | "command" | "⌘" | "meta" | "super" => cmd = true, + "ctrl" | "control" | "⌃" => ctrl = true, + "alt" | "option" | "opt" | "⌥" => alt = true, + "shift" | "⇧" => shift = true, + _ => { + if key.is_some() { + return None; + } + let k = if part.chars().count() == 1 { part.to_uppercase() } else { capitalize(part) }; + key = Some(k); + } + } + } + if plus_key { + if key.is_some() { + return None; + } + key = Some("+".into()); + } + let key = key?; + let mut out = Vec::new(); + if cmd { + out.push("Cmd"); + } + if ctrl { + out.push("Ctrl"); + } + if alt { + out.push("Alt"); + } + if shift { + out.push("Shift"); + } + let mut s = out.join("+"); + if !s.is_empty() { + s.push('+'); + } + s.push_str(&key); + Some(s) +} + +/// Key names spelled with more than one capital (egui's `Key::name`): Title case would turn +/// `PageUp` into `Pageup`, which no key matches, so a binding saved that way never fired (#6827). +const MULTI_WORD_KEYS: [&str; 8] = + ["PageUp", "PageDown", "OpenBracket", "CloseBracket", "OpenCurlyBracket", "CloseCurlyBracket", "BrowserBack", "IntlBackslash"]; + +fn capitalize(s: &str) -> String { + if let Some(k) = MULTI_WORD_KEYS.iter().find(|k| k.eq_ignore_ascii_case(s)) { + return (*k).to_string(); + } + let mut c = s.chars(); + match c.next() { + Some(f) if f.is_ascii_alphabetic() => { + let rest: String = c.collect(); + // Function keys stay upper-case (F1…F24); named keys are Title case. + if f.eq_ignore_ascii_case(&'f') && rest.chars().all(|c| c.is_ascii_digit()) && !rest.is_empty() { + format!("F{rest}") + } else { + format!("{}{}", f.to_ascii_uppercase(), rest.to_ascii_lowercase()) + } + } + _ => s.to_string(), + } +} + +/// Hold-and-release gestures (#249): temporary tools that last while their key is held and give +/// the previous tool back on release. They are bound like shortcuts (Edit › Keyboard Shortcuts › +/// Tools › Temporary, `edit.keyboardShortcuts`) but held rather than pressed, so they are not +/// commands; the UI reads their keys through [`Preferences::shortcut`]. While a selection or +/// shape is being dragged, the Hand key repositions it instead of panning (Photoshop's Space). +/// The erase key keeps the Brush or Pencil and makes its strokes erase (Photoshop's `~`). +pub const TEMPORARY_TOOLS: &[(&str, &str, &str)] = &[ + ("tools.temporary.hand", "Hand Tool (hold)", "Space"), + ("tools.temporary.zoomIn", "Zoom In (hold, drag to scrub)", "Cmd+Space"), + ("tools.temporary.zoomOut", "Zoom Out (hold)", "Alt+Space"), + ("tools.temporary.erase", "Erase with Brush or Pencil (hold)", "`"), +]; + +/// Photoshop's second default keys (#3665): (command, key). Each command keeps its main key +/// (the one its menu shows) and answers these as well, as in Photoshop's default set: the +/// legacy function keys F2 Cut, F3 Copy, F4 Paste and Shift+F7 Inverse; Shift+Backspace +/// (Shift+Delete on a Mac) for Fill; ⌘+ beside ⌘= for Zoom In; and off the Mac, Ctrl+F4 beside +/// Ctrl+W for Close (Windows' close-document key; macOS keeps Ctrl+F4 for window focus). +pub const ALTERNATE_SHORTCUTS: &[(&str, &str)] = &[ + ("edit.cut", "F2"), + ("edit.copy", "F3"), + ("edit.paste", "F4"), + ("edit.fill", "Shift+Backspace"), + ("select.inverse", "Shift+F7"), + ("view.zoomIn", "Cmd++"), + #[cfg(not(target_os = "macos"))] + ("file.close", "Cmd+F4"), +]; + +/// A command's default alternate keys ([`ALTERNATE_SHORTCUTS`]). +pub fn default_alternates(id: &str) -> impl Iterator + '_ { + ALTERNATE_SHORTCUTS.iter().filter(move |(c, _)| *c == id).map(|(_, sc)| *sc) +} + +/// Keys the system takes before the app sees them (switching apps, closing the window, force +/// quit): `edit.keyboardShortcuts` won't bind them, as Photoshop won't. `sc` is in our notation, +/// where `Cmd` is Ctrl off the Mac. +pub fn reserved_shortcut(sc: &str) -> bool { + let Some(n) = normalize_shortcut(sc) else { return false }; + let reserved: &[&str] = if cfg!(target_os = "macos") { + &["Cmd+Tab", "Cmd+Shift+Tab", "Cmd+Alt+Escape", "Cmd+`"] + } else if cfg!(target_os = "windows") { + &["Alt+Tab", "Alt+Shift+Tab", "Alt+F4", "Cmd+Alt+Delete", "Cmd+Escape", "Cmd+Shift+Escape"] + } else { + // Linux and the BSDs: window switching and closing, and the virtual consoles. + &[ + "Alt+Tab", + "Alt+Shift+Tab", + "Alt+F4", + "Cmd+Alt+Delete", + "Cmd+Alt+F1", + "Cmd+Alt+F2", + "Cmd+Alt+F3", + "Cmd+Alt+F4", + "Cmd+Alt+F5", + "Cmd+Alt+F6", + "Cmd+Alt+F7", + "Cmd+Alt+F8", + "Cmd+Alt+F9", + "Cmd+Alt+F10", + "Cmd+Alt+F11", + "Cmd+Alt+F12", + ] + }; + reserved.iter().filter_map(|r| normalize_shortcut(r)).any(|r| r == n) +} + +/// What a bindable id is: a command (the engine's or the shell's), a tool, or a held tool. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum BindKind { + Command, + Tool, + Hold, +} + +/// Every bindable id: (id, label, menu path, default key, kind). The engine's commands, the +/// shell's ([`crate::shell_commands::SHELL_COMMANDS`]), the tools and the held temporary tools, +/// so Edit › Keyboard Shortcuts works the same with or without the app's UI. +pub fn bindable_items() -> impl Iterator, BindKind)> { + let engine = crate::command_specs().iter().map(|c| (c.id, c.label, c.menu, c.shortcut, BindKind::Command)); + let shell = crate::shell_commands::SHELL_COMMANDS.iter().map(|c| (c.0, c.1, c.2, c.3, BindKind::Command)); + let tools = crate::shell_commands::TOOLS.iter().map(|t| (t.0, t.1, &["Tools"][..], t.2, BindKind::Tool)); + let hold = TEMPORARY_TOOLS.iter().map(|t| (t.0, t.1, &["Tools", "Temporary"][..], Some(t.2), BindKind::Hold)); + engine.chain(shell).chain(tools).chain(hold) +} + +/// Every bindable id with its default. +pub(super) fn bindable() -> impl Iterator)> { + bindable_items().map(|i| (i.0, i.3)) +} + +pub(super) fn is_tool(id: &str) -> bool { + crate::shell_commands::tool_default(id).is_some() +} + +/// Shortcuts bound to more than one command: (shortcut, ids). `bindings` are (id, shortcut). +pub fn conflicts<'a>(bindings: impl IntoIterator) -> Vec<(String, Vec)> { + let mut by: BTreeMap> = BTreeMap::new(); + for (id, sc) in bindings { + if let Some(n) = normalize_shortcut(sc) { + by.entry(n).or_default().push(id.to_string()); + } + } + by.into_iter().filter(|(_, ids)| ids.len() > 1).collect() +} diff --git a/crates/engine/src/prefs/store.rs b/crates/engine/src/prefs/store.rs new file mode 100644 index 0000000000..6ed7aa4a7c --- /dev/null +++ b/crates/engine/src/prefs/store.rs @@ -0,0 +1,123 @@ +use super::{Deserialize, Map, Preferences, Serialize, Value}; + +// ------------------------------------------------------------------ store + +/// The session's preferences plus a revision counter that moves on every change (frontends +/// persist when it moves). +#[derive(Clone, Debug, Default)] +pub struct PrefsStore { + values: Preferences, + rev: u64, +} + +impl PrefsStore { + pub fn get(&self) -> &Preferences { + &self.values + } + /// Change preferences; bumps the revision. + pub fn edit(&mut self, f: impl FnOnce(&mut Preferences) -> R) -> R { + self.rev += 1; + f(&mut self.values) + } + pub fn rev(&self) -> u64 { + self.rev + } +} + +/// The preferences file format written by [`Session::prefs_value`]. 2: Use Shift Key for Tool +/// Switch stored as chosen (before, a stored `false` was the old default; see `load_prefs_json`). +pub const PREFS_VERSION: u64 = 2; + +/// A saved preference [`Session::load_prefs_json`] couldn't read (a wrong type, an unknown +/// choice) and left at its default. +#[derive(Clone, Debug, PartialEq)] +pub struct InvalidPref { + /// JSON keys from the top of the preferences file (`["fileHandling", "recentFileCount"]`), + /// kept apart because keys such as command ids contain dots. + pub keys: Vec, + /// The value the file held there. + pub value: Value, +} + +impl InvalidPref { + /// The dotted path, as `prefs.get` and `prefs.set` take it. + pub fn path(&self) -> String { + self.keys.join(".") + } +} + +/// How deep [`read_lenient`] looks for the readable parts of an unreadable value. +const LENIENT_DEPTH: usize = 32; + +/// Read `v` as a `T`. When that fails, start from `T::default()` and lay the file's values over +/// it one at a time, keeping each one the whole still reads with and descending into objects +/// that don't, so one bad value resets only itself. The values left out are added to +/// `invalid`, their keys after `prefix`. +pub(super) fn read_lenient(v: Value, prefix: &[&str], invalid: &mut Vec) -> T +where + T: Default + Serialize + for<'de> Deserialize<'de>, +{ + if let Ok(t) = T::deserialize(&v) { + return t; + } + let prefix: Vec = prefix.iter().map(|k| (*k).to_string()).collect(); + let mut acc = serde_json::to_value(T::default()).unwrap_or(Value::Null); + let fits = |root: &Value| T::deserialize(root).is_ok(); + match v { + Value::Object(file) if acc.is_object() && fits(&acc) => { + lay_over(&mut acc, &mut Vec::new(), file, &fits, &prefix, invalid); + T::deserialize(&acc).unwrap_or_default() + } + value => { + invalid.push(InvalidPref { keys: prefix, value }); + T::default() + } + } +} + +/// [`read_lenient`]'s walk: lay `file` over the object at `at` inside `root`, key by key, so that +/// `root` always reads (`fits`). +fn lay_over( + root: &mut Value, + at: &mut Vec, + file: Map, + fits: &dyn Fn(&Value) -> bool, + prefix: &[String], + invalid: &mut Vec, +) { + for (k, fv) in file { + let Some(obj) = object_at(root, at) else { return }; + let old = obj.insert(k.clone(), fv.clone()); + if fits(root) { + continue; + } + // Back to the default. An object keeps its parts that read, and so does a new map entry + // (a shortcut set, a workspace) when it reads from an empty object. + let Some(obj) = object_at(root, at) else { return }; + let mut descend = fv.is_object() && at.len() < LENIENT_DEPTH && matches!(old, None | Some(Value::Object(_))); + match old { + Some(o) => obj.insert(k.clone(), o), + None if descend => obj.insert(k.clone(), Value::Object(Map::new())), + None => obj.remove(&k), + }; + at.push(k); + if descend && !fits(root) { + descend = false; + if let Some((key, parent)) = at.split_last() + && let Some(obj) = object_at(root, parent) + { + obj.remove(key); + } + } + match fv { + Value::Object(m) if descend => lay_over(root, at, m, fits, prefix, invalid), + value => invalid.push(InvalidPref { keys: prefix.iter().chain(at.iter()).cloned().collect(), value }), + } + at.pop(); + } +} + +/// The JSON object at `path` inside `root`. +fn object_at<'a>(root: &'a mut Value, path: &[String]) -> Option<&'a mut Map> { + path.iter().try_fold(root, |cur, k| cur.get_mut(k.as_str()))?.as_object_mut() +} diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs index 5314d1f197..fc96369ee4 100644 --- a/crates/engine/src/prefs/tests.rs +++ b/crates/engine/src/prefs/tests.rs @@ -22,6 +22,21 @@ fn defaults_match_photoshop() { } } +#[test] +fn intermediate_ui_scale_presets_survive_restart() { + let mut s = Session::new(); + assert_eq!(s.execute("prefs.get", json!({"path": "interface.uiScale"})).unwrap(), json!("auto")); + for scale in ["100", "105", "110", "115", "120", "125"] { + s.execute("prefs.set", json!({"path": "interface.uiScale", "value": scale})).unwrap(); + let mut restored = Session::new(); + restored.load_prefs_json(&s.prefs_to_json()).unwrap(); + assert_eq!(restored.execute("prefs.get", json!({"path": "interface.uiScale"})).unwrap(), json!(scale)); + } + // Presets remain discrete choices: arbitrary values must not silently replace a valid scale. + assert!(s.execute("prefs.set", json!({"path": "interface.uiScale", "value": "112"})).is_err()); + assert_eq!(s.execute("prefs.get", json!({"path": "interface.uiScale"})).unwrap(), json!("125")); +} + /// #2264: the Studio workspace dot pattern can be hidden without changing themes. #[test] fn canvas_dot_grid_can_be_disabled_persisted_and_reset() { @@ -343,6 +358,11 @@ fn shortcuts_normalise_and_detect_conflicts() { assert_eq!(normalize_shortcut("Cmd++").as_deref(), Some("Cmd++")); assert_eq!(normalize_shortcut("Cmd+Shift"), None); assert_eq!(normalize_shortcut("Cmd+A+B"), None); + // #6827: multi-word key names keep egui's spelling (Title case made `Pageup`, matching no key). + assert_eq!(normalize_shortcut("shift+pageup").as_deref(), Some("Shift+PageUp")); + assert_eq!(normalize_shortcut("Ctrl+Alt+PAGEDOWN").as_deref(), Some("Ctrl+Alt+PageDown")); + assert_eq!(normalize_shortcut("Cmd+openbracket").as_deref(), Some("Cmd+OpenBracket")); + assert_eq!(normalize_shortcut("home").as_deref(), Some("Home")); let c = conflicts([("a", "Cmd+J"), ("b", "cmd+j"), ("c", "Cmd+K")]); assert_eq!(c, vec![("Cmd+J".to_string(), vec!["a".to_string(), "b".to_string()])]); } @@ -493,6 +513,32 @@ fn right_click_with_painting_tools_pref() { assert_eq!(old.right_click_with_painting_tools, RightClickPaint::BrushPicker); } +#[test] +fn gpu_adapter_round_trips_and_defaults_to_automatic() { + let mut s = session(); + assert_eq!(s.prefs().performance.gpu_adapter, ""); + s.execute("prefs.set", json!({"path": "performance.gpuAdapter", "value": "NVIDIA"})).unwrap(); + assert_eq!(s.prefs().performance.gpu_adapter, "NVIDIA"); + assert!(s.execute("prefs.set", json!({"path": "performance.gpuAdapter", "value": 3})).is_err()); + assert_eq!(s.prefs().performance.gpu_adapter, "NVIDIA"); + let mut t = Session::new(); + t.load_prefs_json(&s.prefs_to_json()).unwrap(); + assert_eq!(t.prefs().performance.gpu_adapter, "NVIDIA"); + let mut u = Session::new(); + u.load_prefs_json(r#"{"performance": {"useGpu": true}}"#).unwrap(); + assert_eq!(u.prefs().performance.gpu_adapter, ""); +} + +#[test] +fn gpu_adapter_names_match_by_word_ignoring_case() { + let arc = "Intel(R) Arc(tm) A370M Graphics (DG2)"; + assert!(gpu_adapter_matches("NVIDIA GeForce RTX 5060 Ti", "nvidia")); + assert!(gpu_adapter_matches(arc, " ARC A370M ")); + assert!(!gpu_adapter_matches(arc, "arc a380")); + assert!(!gpu_adapter_matches(arc, "")); + assert!(!gpu_adapter_matches(arc, " ")); +} + #[test] fn gpu_backend_round_trips_and_validates() { let mut s = session(); @@ -553,7 +599,9 @@ fn notification_autohide_preferences() { fn linux_only_preferences_show_only_on_linux() { assert_eq!(is_hidden("performance.linuxDisplayServer"), !cfg!(target_os = "linux")); assert!(!is_hidden("performance.gpuBackend")); - assert!(LINUX_ONLY.iter().all(|p| choices(p).is_some()), "every Linux-only preference is a real one"); + assert!(LINUX_ONLY.contains(&"performance.gpuAdapter")); + let free_text = ["performance.gpuAdapter"]; + assert!(LINUX_ONLY.iter().all(|p| free_text.contains(p) || choices(p).is_some()), "every Linux-only preference is a real one"); } #[test] @@ -828,6 +876,29 @@ fn shift_key_tool_switch_is_on_by_default_and_a_stored_false_is_kept() { assert!(!stored.use_shift_key_for_tool_switch); } +/// #5647: a settings file from before version 2 stored the switch off only because that was the +/// old default, so it loads with the new default (on); one saved since keeps an explicit off. +#[test] +fn old_settings_files_take_the_shift_key_tool_switch_default() { + for old in + [json!({"tools": {"useShiftKeyForToolSwitch": false}}), json!({"version": 1, "tools": {"useShiftKeyForToolSwitch": false, "showTooltips": false}})] + { + let mut s = Session::new(); + s.load_prefs_json(&old.to_string()).unwrap(); + assert!(s.prefs().tools.use_shift_key_for_tool_switch, "{old}"); + } + let mut s = Session::new(); + s.load_prefs_json(&json!({"version": 1, "tools": {"showTooltips": false}}).to_string()).unwrap(); + assert!(!s.prefs().tools.show_tooltips, "other tool settings are kept"); + // Chosen off and saved now: kept on the next start. + s.edit_prefs(|p| p.tools.use_shift_key_for_tool_switch = false); + let saved = s.prefs_to_json(); + assert_eq!(serde_json::from_str::(&saved).unwrap()["version"], json!(PREFS_VERSION)); + let mut restored = Session::new(); + restored.load_prefs_json(&saved).unwrap(); + assert!(!restored.prefs().tools.use_shift_key_for_tool_switch); +} + /// Shortcut exports are all or nothing (#5801): a rename that fails after the first file was /// moved in puts the user's files back as they were and leaves nothing staged. #[test] @@ -915,3 +986,82 @@ fn a_stranded_backup_is_named_in_the_error() { assert_eq!(std::fs::read_to_string(&b).unwrap(), "old B"); let _ = std::fs::remove_dir_all(&dir); } + +#[test] +fn a_wrongly_typed_value_resets_only_that_field() { + // #6611: one bad value used to reset every preference. + let mut s = Session::new(); + let invalid = s + .load_prefs_json( + r#"{"general": {"beepWhenDone": true}, "fileHandling": {"recentFileCount": "ten", "autosaveMinutes": 5}, + "performance": {"historyStates": 20}, "shortcuts": {"edit.fill": "Cmd+Shift+F", "file.open": 7}}"#, + ) + .unwrap(); + assert!(s.prefs().general.beep_when_done); + assert_eq!(s.prefs().file_handling.autosave_minutes, 5, "a sibling in the same section survives"); + assert_eq!(s.prefs().file_handling.recent_file_count, 20, "the bad value takes the default"); + assert_eq!(s.prefs().performance.history_states, 20); + assert_eq!(s.prefs().shortcuts.get("edit.fill").map(String::as_str), Some("Cmd+Shift+F")); + assert!(!s.prefs().shortcuts.contains_key("file.open")); + let paths: Vec = invalid.iter().map(InvalidPref::path).collect(); + assert_eq!(paths, ["fileHandling.recentFileCount", "shortcuts.file.open"]); + assert_eq!(invalid[0].value, json!("ten")); + assert_eq!(invalid[1].keys, ["shortcuts", "file.open"]); +} + +#[test] +fn a_wrongly_typed_section_or_colour_setting_resets_only_that_part() { + let mut s = Session::new(); + let invalid = s + .load_prefs_json( + r#"{"general": "loud", "performance": {"historyStates": 20}, + "colorSettings": {"workingRgb": "display-p3", "askOnMismatch": "yes"}}"#, + ) + .unwrap(); + assert_eq!(s.prefs().general, General::default()); + assert_eq!(s.prefs().performance.history_states, 20); + assert_eq!(s.color.settings.working_rgb, "display-p3", "the rest of the colour settings load"); + let paths: Vec = invalid.iter().map(InvalidPref::path).collect(); + assert_eq!(paths, ["general", "colorSettings.askOnMismatch"]); + // A valid file reports nothing; one that isn't an object still fails as a whole. + assert!(s.load_prefs_json(&s.prefs_to_json()).unwrap().is_empty()); + for bad in ["[1, 2]", "42", "\"prefs\"", "null", "{", ""] { + assert!(s.load_prefs_json(bad).is_err(), "{bad}"); + } + // Deep nesting and odd values don't panic. + let deep = format!("{{\"general\": {}1{}}}", "{\"a\":".repeat(100), "}".repeat(100)); + assert!(s.load_prefs_json(&deep).is_ok()); + assert!(s.load_prefs_json(r#"{"interface": {"theme": 3, "uiScale": [1]}, "workspaces": {"x": 1}, "menus": 1}"#).is_ok()); +} + +/// A saved Memory Usage is kept as stored, and means the same on desktop; only the web build +/// caps what it honours. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn the_history_budget_is_the_stored_memory_usage_on_native() { + for mb in [0u32, 1, 1536, 8192, 65536, u32::MAX] { + let p = Performance { memory_usage_mb: mb, ..Performance::default() }; + assert_eq!(p.effective_memory_usage_mb(), mb); + assert_eq!(p.history_budget_bytes(), (mb as usize) << 20, "{mb} MB"); + } + let mut s = Session::new(); + s.load_prefs_json(r#"{"performance": {"memoryUsageMb": 8192}}"#).unwrap(); + assert_eq!(s.prefs().performance.memory_usage_mb, 8192); + assert_eq!(s.prefs().performance.history_budget_bytes(), 8192 << 20); +} + +/// The web build honours Memory Usage up to its cap: below and at it as stored, above it the cap. +#[test] +fn the_web_build_caps_memory_usage() { + for (stored, honoured) in [ + (0, 0), + (1024, 1024), + (WASM_MEMORY_USAGE_CAP_MB, WASM_MEMORY_USAGE_CAP_MB), + (WASM_MEMORY_USAGE_CAP_MB + 1, WASM_MEMORY_USAGE_CAP_MB), + (8192, WASM_MEMORY_USAGE_CAP_MB), + (u32::MAX, WASM_MEMORY_USAGE_CAP_MB), + ] { + assert_eq!(honoured_memory_usage_mb(stored, true), honoured, "web, {stored} MB"); + assert_eq!(honoured_memory_usage_mb(stored, false), stored, "desktop, {stored} MB"); + } +} diff --git a/crates/engine/src/prefs/validate.rs b/crates/engine/src/prefs/validate.rs new file mode 100644 index 0000000000..37d0fdf022 --- /dev/null +++ b/crates/engine/src/prefs/validate.rs @@ -0,0 +1,277 @@ +use super::shortcuts::normalize_shortcut; +use super::{ + AppearanceMode, Ask, CanvasBorder, CanvasColor, CheckerColors, CheckerSize, ColorPicker, DarkTheme, ExportLocation, ExportMetadata, FontPreview, + GpuBackend, Interpolation, LightTheme, LineStyle, LinuxDisplayServer, LogDestination, LogDetail, OtherCursor, PaintingCursor, PointSize, PressureCurve, + QuickExportFormat, RawColorSpace, RawDemosaic, RawDepth, RawSharpen, RenderingMode, RightClickPaint, TextEngine, Theme, TypeUnit, UiFontSize, UiScale, + Unit, Value, +}; + +/// Preferences dialog sections in Photoshop's order: (id, title). +pub const SECTIONS: [(&str, &str); 18] = [ + ("general", "General"), + ("interface", "Interface"), + ("workspace", "Workspace"), + ("tools", "Tools"), + ("historyLog", "History Log"), + ("fileHandling", "File Handling"), + ("export", "Export"), + ("performance", "Performance"), + ("scratchDisks", "Scratch Disks"), + ("cursors", "Cursors"), + ("transparencyAndGamut", "Transparency & Gamut"), + ("unitsAndRulers", "Units & Rulers"), + ("guidesGridAndSlices", "Guides, Grid & Slices"), + ("plugIns", "Plug-ins"), + ("type", "Type"), + ("enhancedControls", "Enhanced Controls"), + ("rawDefaults", "Camera Raw Defaults"), + ("integrations", "Integrations"), +]; + +/// Preferences that nothing reads yet, so Edit › Preferences doesn't show them: a control that +/// does nothing is worse than a missing one (https://github.com/storytold/photocraft/issues/204). +/// They still load, save and round-trip through `prefs.get` / `prefs.set` unchanged. +/// +/// When you implement one, remove it here; the `prefs_usage` test fails while a listed +/// preference is read anywhere outside `prefs.rs`, and while an unlisted one is read nowhere. +pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[ + "general.colorPicker", + "workspace.autoShowHiddenPanels", + "workspace.enableFloatingDocumentWindowDocking", + "performance.effectCacheMb", + "performance.legacyCompositing", + "scratchDisks.disks", + "unitsAndRulers.columnWidth", + "unitsAndRulers.gutter", + "plugIns.showExtensionPanels", + "plugIns.allowScriptsToConnect", + "type.textEngine", + "enhancedControls.scrubbySliderAcceleration", +]; + +/// Is the preference at `path` (`"section.key"`) hidden from the Preferences dialog? +pub fn is_hidden(path: &str) -> bool { + HIDDEN_UNTIL_IMPLEMENTED.contains(&path) || (!cfg!(target_os = "linux") && LINUX_ONLY.contains(&path)) +} + +/// Preferences that only do something on Linux; the dialog doesn't show them elsewhere. They +/// still load, save and round-trip on every platform. +pub const LINUX_ONLY: &[&str] = &["performance.linuxDisplayServer", "performance.gpuAdapter"]; + +/// Whether a graphics adapter called `name` is the one `performance.gpuAdapter` (`wanted`) asks +/// for: every whitespace-separated word of `wanted` appears in the name, ignoring case ("nvidia", +/// "arc a370m"). An empty `wanted` matches nothing. Startup and Preferences share this. +pub fn gpu_adapter_matches(name: &str, wanted: &str) -> bool { + let name = name.to_lowercase(); + let mut words = wanted.split_whitespace().peekable(); + words.peek().is_some() && words.all(|w| name.contains(&w.to_lowercase())) +} + +/// Choices of an enumerated preference (dotted path, e.g. `"cursors.painting"`). +pub fn choices(path: &str) -> Option<&'static [&'static str]> { + Some(match path { + "general.colorPicker" => ColorPicker::NAMES, + "general.imageInterpolation" => Interpolation::NAMES, + "interface.theme" => Theme::NAMES, + "interface.appearanceMode" => AppearanceMode::NAMES, + "interface.darkTheme" => DarkTheme::NAMES, + "interface.lightTheme" => LightTheme::NAMES, + "interface.canvasColor" => CanvasColor::NAMES, + "interface.canvasBorder" => CanvasBorder::NAMES, + "interface.uiScale" => UiScale::NAMES, + "interface.uiFontSize" => UiFontSize::NAMES, + "historyLog.destination" => LogDestination::NAMES, + "historyLog.detail" => LogDetail::NAMES, + "fileHandling.imagePreviews" | "fileHandling.maximizePsdCompatibility" => Ask::NAMES, + "export.quickExportFormat" => QuickExportFormat::NAMES, + "export.quickExportLocation" => ExportLocation::NAMES, + "export.metadata" => ExportMetadata::NAMES, + "cursors.painting" => PaintingCursor::NAMES, + "cursors.other" => OtherCursor::NAMES, + "tools.rightClickWithPaintingTools" => RightClickPaint::NAMES, + "transparencyAndGamut.gridSize" => CheckerSize::NAMES, + "transparencyAndGamut.gridColors" => CheckerColors::NAMES, + "unitsAndRulers.rulers" | "guidesGridAndSlices.gridUnit" => Unit::NAMES, + "unitsAndRulers.typeUnits" => TypeUnit::NAMES, + "unitsAndRulers.pointSize" => PointSize::NAMES, + "guidesGridAndSlices.guideStyle" | "guidesGridAndSlices.gridStyle" => LineStyle::NAMES, + "type.textEngine" => TextEngine::NAMES, + "type.fontPreview" => FontPreview::NAMES, + "rawDefaults.colorSpace" => RawColorSpace::NAMES, + "rawDefaults.bitDepth" => RawDepth::NAMES, + "rawDefaults.sharpenFor" => RawSharpen::NAMES, + "rawDefaults.demosaic" => RawDemosaic::NAMES, + "performance.gpuBackend" => GpuBackend::NAMES, + "performance.linuxDisplayServer" => LinuxDisplayServer::NAMES, + "performance.renderingMode" => RenderingMode::NAMES, + _ => return None, + }) +} + +/// Valid range of a numeric preference. +pub fn range(path: &str) -> Option<(f64, f64)> { + Some(match path { + "fileHandling.autosaveMinutes" => (1.0, 240.0), + "fileHandling.recentFileCount" => (0.0, 100.0), + "tools.nudgeBundlePauseMs" => (100.0, 10_000.0), + "fileHandling.rasterizeSvgGroupsDeeperThan" => (0.0, photocraft_doc::MAX_GROUP_DEPTH as f64), + "interface.notificationDurationSeconds" => (1.0, 120.0), + "export.jpegQuality" | "export.webpQuality" => (1.0, 100.0), + "performance.memoryUsageMb" => (256.0, 1_048_576.0), + "performance.gpuMemoryMb" => (0.0, 1_048_576.0), + "performance.historyStates" => (1.0, 1000.0), + "performance.cacheLevels" => (1.0, 8.0), + "performance.cacheTileSize" => (256.0, 16384.0), + "performance.effectCacheMb" => (16.0, 65536.0), + "transparencyAndGamut.gamutWarningOpacity" => (1.0, 100.0), + "unitsAndRulers.columnWidth" | "unitsAndRulers.gutter" => (0.0, 10_000.0), + "unitsAndRulers.printResolution" | "unitsAndRulers.screenResolution" | "rawDefaults.resolution" => (1.0, 10_000.0), + "guidesGridAndSlices.gridlineEvery" => (0.001, 10_000.0), + "guidesGridAndSlices.subdivisions" => (1.0, 100.0), + "layerMaskOverlay.opacity" => (0.0, 100.0), + "type.recentFonts" => (0.0, 50.0), + "integrations.controlPort" => (0.0, 65535.0), + _ => return None, + }) +} + +/// Is this preference a `#rrggbb` colour? +pub fn is_color(path: &str) -> bool { + if choices(path).is_some() || path.starts_with("shortcuts.") || path.starts_with("alternateShortcuts.") || path.starts_with("menus.") { + return false; + } + if path == "layerMaskOverlay.color" { + return true; + } + let leaf = path.rsplit('.').next().unwrap_or(path); + matches!( + leaf, + "guideColor" + | "smartGuideColor" + | "gridColor" + | "sliceColor" + | "canvasCustomColor" + | "brushPreviewColor" + | "gamutWarningColor" + | "customLight" + | "customDark" + ) +} + +pub fn parse_hex(s: &str) -> Option<[u8; 3]> { + let s = s.trim().trim_start_matches('#'); + if s.len() != 6 { + return None; + } + let b = |i: usize| s.get(i..i + 2).and_then(|s| u8::from_str_radix(s, 16).ok()); + Some([b(0)?, b(2)?, b(4)?]) +} + +// ------------------------------------------------------------------ JSON paths + +/// Map keys that are command ids (which contain dots): `shortcuts.`, +/// `alternateShortcuts.`, `menus.colors.`. +pub(super) fn keyed(path: &str) -> Option<(&str, &str)> { + for prefix in ["shortcuts.", "alternateShortcuts.", "menus.colors."] { + if let Some(id) = path.strip_prefix(prefix) { + return Some((&prefix[..prefix.len() - 1], id)); + } + } + None +} + +pub(super) fn get_path<'a>(root: &'a Value, path: &str) -> Option<&'a Value> { + if path.is_empty() { + return Some(root); + } + if let Some((map, id)) = keyed(path) { + return get_path(root, map)?.get(id); + } + path.split('.').try_fold(root, |v, k| v.get(k)) +} + +/// Set `value` at `path` (creating keys only inside free-form maps such as `shortcuts`). +pub(super) fn set_path(root: &mut Value, path: &str, value: Value) -> std::result::Result<(), String> { + if let Some((map, id)) = keyed(path) { + let mut cur = &mut *root; + for k in map.split('.') { + cur = cur.get_mut(k).ok_or_else(|| format!("unknown preference `{path}`"))?; + } + cur.as_object_mut().ok_or_else(|| format!("`{map}` is not a map"))?.insert(id.to_string(), value); + return Ok(()); + } + let keys: Vec<&str> = path.split('.').filter(|k| !k.is_empty()).collect(); + let Some((last, parents)) = keys.split_last() else { return Err("empty path".into()) }; + let mut cur = root; + for k in parents { + cur = cur.get_mut(*k).ok_or_else(|| format!("unknown preference `{path}`"))?; + } + let free_map = matches!(parents.first(), Some(&"shortcuts")) || (parents.len() == 2 && parents[0] == "menus" && parents[1] == "colors"); + let obj = cur.as_object_mut().ok_or_else(|| format!("`{path}` is not inside a section"))?; + if !obj.contains_key(*last) && !free_map { + return Err(format!("unknown preference `{path}`")); + } + obj.insert(last.to_string(), value); + Ok(()) +} + +/// Validate one value for `path` before it is stored (choices, ranges, colours). +pub(super) fn check_value(path: &str, v: &Value) -> std::result::Result<(), String> { + if path == "interface.favoriteThemes" { + let items = v.as_array().ok_or("`interface.favoriteThemes` must be a list of theme ids")?; + if items.len() > Theme::NAMES.len() { + return Err("`interface.favoriteThemes` contains too many themes".into()); + } + let mut seen = Vec::with_capacity(items.len()); + for item in items { + let theme = item.as_str().and_then(Theme::parse).ok_or_else(|| format!("unknown favorite theme `{item}`"))?; + if seen.contains(&theme) { + return Err(format!("duplicate favorite theme `{}`", theme.name())); + } + seen.push(theme); + } + } + if path == "performance.renderingMode" && v.is_null() { + return Ok(()); // Legacy policy, resolved from useGpu and gpuBackend. + } + if let Some(c) = choices(path) { + let s = v.as_str().ok_or_else(|| format!("`{path}` must be one of {}", c.join("|")))?; + if !c.contains(&s) { + return Err(format!("`{path}` must be one of {} (got `{s}`)", c.join("|"))); + } + } + if path == "interface.language" { + let ok = v.as_str().is_some_and(|s| !s.is_empty() && s.len() <= 16 && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')); + if !ok { + return Err("`interface.language` must be `auto` or a language code such as `en` or `ja`".into()); + } + } + if let Some((lo, hi)) = range(path) { + let x = v.as_f64().ok_or_else(|| format!("`{path}` must be a number"))?; + if !(lo..=hi).contains(&x) { + return Err(format!("`{path}` must be within {lo}..{hi} (got {x})")); + } + } + if is_color(path) && v.as_str().and_then(parse_hex).is_none() { + return Err(format!("`{path}` must be a #rrggbb colour")); + } + if path == "tools.pressureCurve" { + let pts = v.as_array().ok_or("`tools.pressureCurve` must be a list of [input, output] points")?; + if !(2..=PressureCurve::MAX_POINTS).contains(&pts.len()) { + return Err(format!("`tools.pressureCurve` needs 2..={} points (got {})", PressureCurve::MAX_POINTS, pts.len())); + } + for p in pts { + let ok = p.as_array().is_some_and(|a| a.len() == 2 && a.iter().all(|x| x.as_f64().is_some_and(|x| (0.0..=1.0).contains(&x)))); + if !ok { + return Err(format!("`tools.pressureCurve` points are [input, output] pairs within 0..1 (got {p})")); + } + } + } + if let Some(sc) = path.strip_prefix("shortcuts.") { + let s = v.as_str().ok_or_else(|| format!("shortcut for `{sc}` must be a string"))?; + if !s.is_empty() && normalize_shortcut(s).is_none() { + return Err(format!("`{s}` is not a valid shortcut (e.g. Cmd+Shift+N, F7)")); + } + } + Ok(()) +} diff --git a/crates/engine/src/preset_store.rs b/crates/engine/src/preset_store.rs index 83cb16a838..cc93556412 100644 --- a/crates/engine/src/preset_store.rs +++ b/crates/engine/src/preset_store.rs @@ -35,9 +35,17 @@ //! changes asynchronously. Gradient presets, including imported `.grd` groups, are small and persist with //! the preferences document (`presets.gradients`). //! +//! The pattern library (imported `.pat` and defined patterns, not the built-ins) lives there too: +//! `patterns.json` plus one `.pat` file per pattern, see [`patterns`]. +//! //! The Actions list (`actions.json`, see [`crate::actions_cmds`]) lives in the same directory. //! A missing file is an empty list. A corrupt or oversized file is skipped with a warning. //! Headless sessions have no store, so their actions stay in memory. +//! +//! Patterns use `patterns.json` and lazily loaded per-pattern assets. On first startup a +//! valid legacy `patterns.pat` is migrated by writing assets before the atomic index commit. +//! Legacy and corrupt data are preserved; an existing index is authoritative even when corrupt. +//! A failed migration leaves the legacy library usable and retries at the next startup. use std::collections::{BTreeMap, HashMap, HashSet}; use std::io::Read; @@ -49,6 +57,9 @@ use serde::{Deserialize, Serialize}; use crate::Session; +mod patterns; +pub use patterns::{Loaded as LoadedPattern, MAX_PATTERN_BYTES, PATTERNS_FILE, PREVIEW_SIDE as PATTERN_PREVIEW_SIDE}; + /// Group file extension. pub const GROUP_EXT: &str = "pcbrushes"; /// Tip file extension (inside [`TIPS_DIR`]). @@ -67,6 +78,10 @@ pub const MAX_INDEX_BYTES: u64 = 4 << 20; pub const ACTIONS_FILE: &str = "actions.json"; /// Largest Actions file read or written. pub const MAX_ACTIONS_BYTES: u64 = 16 << 20; +/// The user's pattern library (`.pat`). +pub const LEGACY_PATTERNS_FILE: &str = "patterns.pat"; +/// Largest pattern library read or written. +pub const MAX_PATTERNS_BYTES: u64 = 256 << 20; /// The whole store never grows beyond this; further groups are skipped with a warning. pub const MAX_STORE_BYTES: u64 = 2 << 30; /// Largest tip side accepted from disk. @@ -245,7 +260,8 @@ pub fn encode_tip(t: &GrayTile) -> Vec { out.push(if eight { 8 } else { 16 }); out.extend_from_slice(&t.width.to_le_bytes()); out.extend_from_slice(&t.height.to_le_bytes()); - let mut z = flate2::write::ZlibEncoder::new(out, flate2::Compression::fast()); + // Level 3: zlib-rs level 1 writes static Huffman blocks only, 13-30% bigger than miniz's level 1. + let mut z = flate2::write::ZlibEncoder::new(out, flate2::Compression::new(3)); let mut buf = Vec::with_capacity(8192); let mut ok = true; for chunk in t.data.chunks(4096) { @@ -550,9 +566,14 @@ pub struct PresetStore { actions_rev: u64, /// `actions.json` exists but failed to load: left on disk untouched, so it is never written. actions_skipped: bool, + /// The pattern files and index (`patterns.json`). + patterns: patterns::PatternFiles, warnings: Vec, /// Full tips loaded for brushes in use (behind a lock so `&Session` paths can load). cache: Mutex, + /// The stored pattern last read in full, so painting with it again doesn't re-read its file + /// (a texture can be tens of megabytes). One: the pattern in use. + pattern_cache: Mutex>, } /// A loaded store: the presets on disk plus what the session should hide. @@ -568,6 +589,8 @@ pub struct Opened { pub warnings: Vec, /// Recorded actions (`actions.json`). Empty when the file is missing or unreadable. pub actions: Vec, + /// Imported and defined patterns (`patterns.json`), in library order; large ones as previews. + pub patterns: Vec, } /// Load a store (any thread; the desktop app does this in the background at start). @@ -644,6 +667,7 @@ pub fn open(backend: Box) -> Opened { let loaded_actions = load_actions(backend.as_ref(), &mut warnings); let actions_skipped = loaded_actions.is_none(); let actions = loaded_actions.unwrap_or_default(); + let (patterns, pattern_files) = patterns::load(backend.as_ref(), &mut warnings); let mut store = PresetStore { backend, synced_rev: None, @@ -653,8 +677,10 @@ pub fn open(backend: Box) -> Opened { index: None, actions_rev: 0, actions_skipped, + patterns: pattern_files, warnings: Vec::new(), cache: Mutex::new(TipCache { limit: TIP_CACHE_BYTES, ..Default::default() }), + pattern_cache: Mutex::new(None), }; let mut presets = Vec::new(); for (file, size, g) in parsed { @@ -710,7 +736,7 @@ pub fn open(backend: Box) -> Opened { let hidden_builtins = index.as_ref().map(|i| i.hidden_builtins.clone()).unwrap_or_default(); let order = index.as_ref().map(|i| i.order.clone()).unwrap_or_default(); store.index = index; - Opened { store, presets, hidden_builtins, order, warnings, actions } + Opened { store, presets, hidden_builtins, order, warnings, actions, patterns } } /// `actions.json`: a missing file is an empty list. Anything else unreadable is a warning. @@ -1042,6 +1068,29 @@ impl PresetStore { Ok(()) } + /// Write the pattern library's changes (see [`patterns`]). + pub fn sync_patterns(&mut self, library: &crate::pattern_cmds::PatternLibrary) { + let changed = self.patterns.pixels_rev != library.pixels_rev; + patterns::sync(self.backend.as_ref(), &mut self.patterns, library, &mut self.warnings); + let cache = self.pattern_cache.get_mut().unwrap_or_else(PoisonError::into_inner); + if changed || cache.as_ref().is_some_and(|p| !self.patterns.index.iter().any(|e| e.id == p.id)) { + *cache = None; + } + } + + /// The full pixels of a stored pattern the library holds as a preview: the last one read is + /// kept, so only switching patterns reads a file. + pub fn load_pattern(&self, id: &str) -> Result { + let mut cache = self.pattern_cache.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(p) = cache.as_ref().filter(|p| p.id == id) { + return Ok(p.clone()); + } + let entry = self.patterns.index.iter().find(|e| e.id == id).ok_or_else(|| format!("pattern {id} is not in the preset store"))?; + let p = patterns::load_full(self.backend.as_ref(), entry)?; + *cache = Some(p.clone()); + Ok(p) + } + /// Write the Actions list when `rev` differs from the last load or write. /// An oversized list is warned once and not retried. An I/O error is warned and retried /// on the next command. @@ -1091,7 +1140,30 @@ impl Session { /// built-in of the same name; presets created before the store finished loading win), hide /// deleted built-ins, then sync. Returns the load warnings. pub fn attach_preset_store(&mut self, opened: Opened) -> Vec { - let Opened { store, presets, hidden_builtins, order, mut warnings, actions: disk } = opened; + let Opened { store, presets, hidden_builtins, order, mut warnings, actions: disk, patterns } = opened; + let builtins = crate::pattern_cmds::builtin(); + for l in patterns { + let id = l.pattern.id.clone(); + match self.patterns.items.iter_mut().find(|q| q.id == id) { + Some(q) if crate::pattern_cmds::is_pristine_builtin(q, &builtins) => { + *q = l.pattern; + if let Some(size) = l.full { + self.patterns.stored.insert(id, size); + } + } + Some(q) => { + if q.surface != l.pattern.surface { + self.patterns.pixels_rev += 1; + } + } + None => { + if let Some(size) = l.full { + self.patterns.stored.insert(id, size); + } + self.patterns.items.push(l.pattern); + } + } + } let lib = &mut self.tools.presets; lib.retain(|p| !(p.builtin && hidden_builtins.iter().any(|h| h.eq_ignore_ascii_case(&p.name)))); for p in presets { @@ -1136,7 +1208,7 @@ impl Session { warnings } - /// Write pending brush preset and Actions changes to the attached store (no-op without one). + /// Write pending brush preset, Actions and pattern changes to the attached store (no-op without one). pub fn sync_preset_store(&mut self) { let rev = self.tools.presets_rev; let actions_rev = self.actions.rev; @@ -1151,6 +1223,7 @@ impl Session { } } st.sync_actions(&self.actions.list, actions_rev); + st.sync_patterns(&self.patterns); } } diff --git a/crates/engine/src/preset_store/patterns.rs b/crates/engine/src/preset_store/patterns.rs new file mode 100644 index 0000000000..ff535ebcf6 --- /dev/null +++ b/crates/engine/src/preset_store/patterns.rs @@ -0,0 +1,380 @@ +//! The pattern library in the preset store: imported (`.pat`) and defined patterns survive a +//! restart, as Photoshop keeps its Patterns panel. +//! +//! - `pattern-.pat`: one pattern per file (a one-pattern Photoshop `.pat`), written once +//! when the pattern enters the library and deleted when it leaves it; +//! - `pattern-.preview.pat`: the same pattern shrunk to at most [`PREVIEW_SIDE`] px, for +//! patterns larger than that; +//! - `patterns.json`: the library order, names and full sizes (renaming rewrites only this file). +//! +//! Opening the store reads the index and the previews, never a large pattern's pixels: texture +//! libraries run to gigabytes (Debaser's colour libraries hold twenty 4500 × 4500 px patterns per +//! file). Such a pattern joins the library as its preview, with its full size in +//! [`crate::pattern_cmds::PatternLibrary::stored`], and its pixels are read when it is used +//! ([`super::PresetStore::load_pattern`]). +//! +//! Built-in patterns are never written. A pattern file or index that fails to load is left on +//! disk untouched and never overwritten or deleted; a missing index is an empty library. + +use std::collections::HashSet; + +use photocraft_doc::Pattern; +use photocraft_geom::Rect; +use photocraft_raster::Surface; +use serde::{Deserialize, Serialize}; + +use super::{PresetBackend, missing_file}; +use crate::pattern_cmds::PatternLibrary; + +/// Library order and names. +pub const PATTERNS_FILE: &str = "patterns.json"; +/// Largest `patterns.json` read or written. +pub const MAX_PATTERN_INDEX_BYTES: u64 = 4 << 20; +/// Largest pattern file read or written. +pub const MAX_PATTERN_BYTES: u64 = 256 << 20; +/// Most patterns loaded. +pub const MAX_PATTERNS: usize = 10_000; +/// Patterns larger than this (either side) are loaded as a preview of this size until used. +pub const PREVIEW_SIDE: u32 = 128; + +#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] +pub(super) struct Entry { + pub id: String, + pub name: String, + pub file: String, + #[serde(default)] + pub width: u32, + #[serde(default)] + pub height: u32, +} + +#[derive(Serialize, Deserialize)] +struct IndexFile { + version: u32, + patterns: Vec, +} + +/// The store's view of the pattern files it owns. +#[derive(Default)] +pub(super) struct PatternFiles { + /// The index as last loaded or written. + pub index: Vec, + /// `patterns.json` exists but failed to load: never written. + pub index_skipped: bool, + /// Pattern files that failed to load: never written or deleted. + pub skipped: HashSet, + /// Patterns that couldn't be written (too large, I/O error): not retried this session, so + /// one failure is one warning. + pub failed: HashSet, + pub pixels_rev: u64, + pub pending_removals: HashSet, +} + +/// A stored pattern as loaded: the pattern (a preview when `full` is set) and its full size. +pub struct Loaded { + pub pattern: Pattern, + pub full: Option<(u32, u32)>, +} + +fn hash(id: &str) -> u64 { + id.bytes().fold(0xcbf2_9ce4_8422_2325u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0100_0000_01b3)) +} + +/// The file a pattern is stored in (its id hashed: ids are UUIDs or content hashes, but may hold +/// any character). +fn file_name(id: &str) -> String { + format!("pattern-{:016x}.pat", hash(id)) +} + +fn preview_name(id: &str) -> String { + format!("pattern-{:016x}.preview.pat", hash(id)) +} + +#[cfg(test)] +pub(super) fn file_name_for_test(id: &str) -> String { + file_name(id) +} + +fn is_large(width: u32, height: u32) -> bool { + width.max(height) > PREVIEW_SIDE +} + +/// `p` shrunk to at most [`PREVIEW_SIDE`] px (nearest sample: a swatch, not a resample). +fn preview(p: &Pattern) -> Pattern { + let (w, h) = (p.width.max(1), p.height.max(1)); + let step = f64::from(w.max(h)) / f64::from(PREVIEW_SIDE); + let pw = ((f64::from(w) / step).round() as u32).clamp(1, PREVIEW_SIDE); + let ph = ((f64::from(h) / step).round() as u32).clamp(1, PREVIEW_SIDE); + let fmt = p.surface.format(); + let mut data = Vec::with_capacity(pw as usize * ph as usize * fmt.channels()); + for y in 0..ph { + for x in 0..pw { + let sx = ((f64::from(x) * step) as u32).min(w - 1); + let sy = ((f64::from(y) * step) as u32).min(h - 1); + data.extend(p.surface.pixel(sx as i32, sy as i32)); + } + } + let mut surface = Surface::new(fmt); + surface.write_region(Rect::new(0, 0, pw as i32, ph as i32), &data); + Pattern { id: p.id.clone(), name: p.name.clone(), width: pw, height: ph, surface } +} + +fn read_one(backend: &dyn PresetBackend, file: &str) -> Result { + let mut v = photocraft_io::pattern_map::read_pat(&backend.read(file, MAX_PATTERN_BYTES)?)?; + if v.is_empty() { Err("no pattern in the file".to_string()) } else { Ok(v.swap_remove(0)) } +} + +/// A stored pattern's full pixels (named and identified as in `entry`). +pub(super) fn load_full(backend: &dyn PresetBackend, entry: &Entry) -> Result { + let mut p = read_one(backend, &entry.file)?; + p.id = entry.id.clone(); + p.name = entry.name.clone(); + Ok(p) +} + +/// Load the stored patterns, in library order: small ones whole, large ones as previews. +pub(super) fn load(backend: &dyn PresetBackend, warnings: &mut Vec) -> (Vec, PatternFiles) { + let mut files = PatternFiles::default(); + let bytes = match backend.read(PATTERNS_FILE, MAX_PATTERN_INDEX_BYTES) { + Ok(b) => b, + Err(e) if missing_file(&e) => return migrate_legacy(backend, warnings), + Err(e) => { + warnings.push(format!("patterns: {PATTERNS_FILE} skipped: {e}")); + files.index_skipped = true; + return (Vec::new(), files); + } + }; + let index = match serde_json::from_slice::(&bytes) { + Ok(f) if f.version == 1 => f.patterns, + Ok(f) => { + warnings.push(format!("patterns: {PATTERNS_FILE} skipped: unsupported version {}", f.version)); + files.index_skipped = true; + return (Vec::new(), files); + } + Err(e) => { + warnings.push(format!("patterns: {PATTERNS_FILE} skipped: {e}")); + files.index_skipped = true; + return (Vec::new(), files); + } + }; + let mut ids = HashSet::new(); + let valid = index.len() <= MAX_PATTERNS && index.iter().all(|e| e.file == file_name(&e.id) && ids.insert(e.id.clone())); + if !valid { + warnings + .push("patterns: invalid/duplicate index entries; preserve files and recover patterns.json before saving (legacy PAT is not reimported)".into()); + files.index_skipped = true; + return (Vec::new(), files); + } + let mut patterns = Vec::new(); + let mut seen = HashSet::new(); + for e in index.into_iter().take(MAX_PATTERNS) { + // Only generated names: an edited index can't reach other files. + if e.file != file_name(&e.id) || !seen.insert(e.id.clone()) { + warnings.push(format!("patterns: entry \"{}\" skipped: bad file name", e.name)); + continue; + } + let loaded = if is_large(e.width, e.height) { + // The preview; without one (a failed write), the full file once to make it. + match read_one(backend, &preview_name(&e.id)) { + Ok(mut p) => { + p.id = e.id.clone(); + p.name = e.name.clone(); + Ok(Loaded { pattern: p, full: Some((e.width, e.height)) }) + } + Err(_) => load_full(backend, &e).map(|p| Loaded { pattern: preview(&p), full: Some((p.width, p.height)) }), + } + } else { + load_full(backend, &e).map(|p| Loaded { pattern: p, full: None }) + }; + match loaded { + Ok(l) => { + patterns.push(l); + files.index.push(e); + } + Err(err) => { + warnings.push(format!("patterns: {} (\"{}\") skipped: {err}", e.file, e.name)); + files.skipped.insert(e.file.clone()); + // Kept in the index, so a later write doesn't forget the file. + files.index.push(e); + } + } + } + (patterns, files) +} + +/// Write what changed: new patterns' files (and previews), the index when the order or a name +/// changed, and delete the files of patterns that left the library. Failures are warnings. +pub(super) fn sync(backend: &dyn PresetBackend, files: &mut PatternFiles, library: &PatternLibrary, warnings: &mut Vec) { + files.pending_removals.retain(|f| match backend.remove(f) { + Ok(()) => false, + Err(e) => { + warnings.push(format!("Couldn't delete pattern file {f}: {e}")); + true + } + }); + let builtins = crate::pattern_cmds::builtin(); + let pixels_changed = files.pixels_rev != library.pixels_rev; + let mut want: Vec = library + .items + .iter() + .filter(|p| !crate::pattern_cmds::is_pristine_builtin(p, &builtins)) + .map(|p| { + let (width, height) = library.stored.get(&p.id).copied().unwrap_or((p.width, p.height)); + Entry { id: p.id.clone(), name: p.name.clone(), file: file_name(&p.id), width, height } + }) + .collect(); + // Unreadable files stay listed (at the end) until the library takes their pattern back. + for e in &files.index { + if files.skipped.contains(&e.file) && !want.iter().any(|w| w.id == e.id) { + want.push(e.clone()); + } + } + if want == files.index && !pixels_changed { + return; + } + if files.index_skipped { + warnings.push(format!("Couldn't save patterns: {PATTERNS_FILE} could not be read, so it was left unchanged")); + files.index = want; + return; + } + let have: HashSet<&str> = files.index.iter().map(|e| e.id.as_str()).collect(); + let mut written = Vec::with_capacity(want.len()); + for e in want { + // Already on disk (a preview in the library included: its pixels never left the disk). + if (have.contains(e.id.as_str()) && !pixels_changed) || files.skipped.contains(&e.file) || library.stored.contains_key(&e.id) { + written.push(e); + continue; + } + let Some(p) = library.items.iter().find(|p| p.id == e.id) else { continue }; + match write_pattern(backend, p, &e, have.contains(e.id.as_str())) { + Ok(()) => written.push(e), + Err(err) => { + warnings.push(err); + files.failed.insert(e.id); + return; + } + } + } + if written.len() > MAX_PATTERNS { + warnings.push("patterns: index count limit exceeded".into()); + return; + } + let index = IndexFile { version: 1, patterns: written }; + let bytes = match serde_json::to_vec_pretty(&index) { + Ok(b) => b, + Err(e) => { + warnings.push(format!("Couldn't save patterns: {e}")); + return; + } + }; + if bytes.len() as u64 > MAX_PATTERN_INDEX_BYTES { + warnings.push("patterns: index byte limit exceeded".into()); + return; + } + if let Err(e) = backend.write(PATTERNS_FILE, &bytes) { + warnings.push(format!("Couldn't save patterns: {e}")); + return; + } + // Files of patterns that left the library (written by an earlier index) are deleted only + // once the new index no longer names them. + let keep: HashSet<&str> = index.patterns.iter().map(|e| e.file.as_str()).collect(); + for e in &files.index { + if !keep.contains(e.file.as_str()) && !files.skipped.contains(&e.file) { + for f in [e.file.clone(), preview_name(&e.id)] { + if let Err(err) = backend.remove(&f) { + warnings.push(format!("Couldn't delete pattern file {f}: {err}")); + files.pending_removals.insert(f); + } + } + } + } + files.index = index.patterns; + files.pixels_rev = library.pixels_rev; +} + +fn write_pattern(backend: &dyn PresetBackend, p: &Pattern, e: &Entry, owned: bool) -> Result<(), String> { + let bytes = photocraft_io::pattern_map::write_pat(std::slice::from_ref(p)).map_err(|err| format!("Couldn't save pattern \"{}\": {err}", p.name))?; + if bytes.len() as u64 > MAX_PATTERN_BYTES { + return Err(format!("pattern \"{}\" is too large to save ({} MB limit)", p.name, MAX_PATTERN_BYTES >> 20)); + } + if is_large(p.width, p.height) { + // The preview first: a full file without one still loads (by making it). + let small = photocraft_io::pattern_map::write_pat(&[preview(p)]).map_err(|err| format!("Couldn't save pattern \"{}\": {err}", p.name))?; + write_asset(backend, &preview_name(&e.id), &small, owned).map_err(|err| format!("Couldn't save pattern \"{}\": {err}", p.name))?; + } + write_asset(backend, &e.file, &bytes, owned).map_err(|err| format!("Couldn't save pattern \"{}\": {err}", p.name)) +} + +/// The index itself is the atomic migration commit. Never consult stale PAT when it exists, +/// including when corrupt. An interrupted asset write leaves the legacy bytes authoritative. +fn migrate_legacy(backend: &dyn PresetBackend, warnings: &mut Vec) -> (Vec, PatternFiles) { + let mut files = PatternFiles::default(); + let bytes = match backend.read(super::LEGACY_PATTERNS_FILE, super::MAX_PATTERNS_BYTES) { + Ok(b) => b, + Err(e) if missing_file(&e) => return (Vec::new(), files), + Err(e) => { + warnings.push(format!("patterns: legacy file preserved: {e}")); + files.index_skipped = true; + return (Vec::new(), files); + } + }; + let patterns = match photocraft_io::pattern_map::read_pat(&bytes) { + Ok(p) => p, + Err(e) => { + warnings.push(format!("patterns: legacy file preserved: {e}")); + files.index_skipped = true; + return (Vec::new(), files); + } + }; + let mut ids = HashSet::new(); + let mut names = HashSet::new(); + if patterns.len() > MAX_PATTERNS || patterns.iter().any(|p| !ids.insert(p.id.clone()) || !names.insert(file_name(&p.id))) { + warnings.push("patterns: legacy migration refused duplicate IDs, file hash collision or count limit; legacy preserved".into()); + files.index_skipped = true; + return (patterns.into_iter().map(|pattern| Loaded { pattern, full: None }).collect(), files); + } + let migration = (|| -> Result<(), String> { + let entries: Vec = + patterns.iter().map(|p| Entry { id: p.id.clone(), name: p.name.clone(), file: file_name(&p.id), width: p.width, height: p.height }).collect(); + let index = serde_json::to_vec_pretty(&IndexFile { version: 1, patterns: entries.clone() }).map_err(|e| e.to_string())?; + if index.len() as u64 > MAX_PATTERN_INDEX_BYTES { + return Err("index exceeds size limit".into()); + } + for (p, e) in patterns.iter().zip(&entries) { + // Never overwrite unknown files, even if a name collides. Identical orphan assets + // from an interrupted migration can be reused; the legacy file stays untouched. + let full = photocraft_io::pattern_map::write_pat(std::slice::from_ref(p))?; + if full.len() as u64 > MAX_PATTERN_BYTES { + return Err("pattern exceeds size limit".into()); + } + write_migration_asset(backend, &e.file, &full)?; + if is_large(p.width, p.height) { + let small = photocraft_io::pattern_map::write_pat(&[preview(p)])?; + write_migration_asset(backend, &preview_name(&p.id), &small)?; + } + } + // Atomic backend write last: no partial or failed migration can become authoritative. + backend.write(PATTERNS_FILE, &index)?; + Ok(()) + })(); + match migration { + Ok(()) => load(backend, warnings), + Err(e) => { + warnings.push(format!("patterns: migration not committed, legacy remains usable: {e}")); + files.index_skipped = true; + (patterns.into_iter().map(|pattern| Loaded { pattern, full: None }).collect(), files) + } + } +} +fn write_migration_asset(backend: &dyn PresetBackend, name: &str, bytes: &[u8]) -> Result<(), String> { + match backend.read(name, MAX_PATTERN_BYTES) { + Ok(existing) if existing == bytes => Ok(()), + Ok(_) => Err(format!("unrecognized existing asset {name} preserved")), + Err(e) if missing_file(&e) => backend.write(name, bytes), + Err(e) => Err(format!("existing asset {name} preserved: {e}")), + } +} + +fn write_asset(backend: &dyn PresetBackend, name: &str, bytes: &[u8], owned: bool) -> Result<(), String> { + if owned { backend.write(name, bytes) } else { write_migration_asset(backend, name, bytes) } +} diff --git a/crates/engine/src/preset_store/tests.rs b/crates/engine/src/preset_store/tests.rs index 860ebd4fc3..89be8bc681 100644 --- a/crates/engine/src/preset_store/tests.rs +++ b/crates/engine/src/preset_store/tests.rs @@ -703,3 +703,425 @@ fn an_actions_file_that_failed_to_load_is_not_overwritten() { assert_eq!(mem.files.lock().unwrap().get(ACTIONS_FILE), Some(&newer), "an actions file the store could not read was overwritten"); assert!(!s.preset_store.as_mut().unwrap().take_warnings().is_empty()); } + +/// A large pattern opens as a small preview (texture libraries run to gigabytes) and is read at +/// full size when it is used; its preview file goes with it on delete. +#[test] +fn large_patterns_load_on_use() { + let dir = TempDir::new("patterns-large"); + let (mut s, _) = session(&dir); + s.execute("file.new", json!({"width": 300, "height": 200})).unwrap(); + s.execute("edit.fill", json!({"color": "#ffffff"})).unwrap(); + s.execute("select.rect", json!({"x": 250, "y": 150, "width": 50, "height": 50})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("edit.definePattern", json!({"name": "Big"})).unwrap(); + assert_eq!(dir.files().iter().filter(|f| f.ends_with(".preview.pat")).count(), 1); + + let (mut t, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + let held = t.patterns.items.iter().find(|p| p.name == "Big").unwrap(); + assert!(held.width <= PATTERN_PREVIEW_SIDE && held.height <= PATTERN_PREVIEW_SIDE, "held as a preview: {}x{}", held.width, held.height); + let listed = t.execute("pattern.list", json!({})).unwrap(); + let big = listed.as_array().unwrap().iter().find(|p| p["name"] == "Big").unwrap(); + assert_eq!((big["width"].as_u64(), big["height"].as_u64()), (Some(300), Some(200)), "the list reports the full size"); + // Checks that only need the pattern's id (Pattern Stamp's enabled state) don't read its file. + let id = t.patterns.items.iter().find(|p| p.name == "Big").unwrap().id.clone(); + let file = dir.0.join(patterns::file_name_for_test(&id)); + let moved = file.with_extension("moved"); + std::fs::rename(&file, &moved).unwrap(); + assert_eq!(crate::pattern_cmds::resolve_id(&t, "Big"), Some(id.clone())); + assert!(crate::pattern_cmds::resolve(&t, "Big").is_none(), "the full pixels come from the file"); + std::fs::rename(&moved, &file).unwrap(); + let full = crate::pattern_cmds::resolve(&t, "Big").unwrap(); + assert_eq!((full.width, full.height), (300, 200)); + assert!(full.surface.pixel(280, 180)[1] < 0.01 && full.surface.pixel(10, 10)[1] > 0.99, "full pixels"); + // Once read, it is kept: using it again doesn't read the file. + std::fs::rename(&file, &moved).unwrap(); + assert_eq!(crate::pattern_cmds::resolve(&t, "Big").map(|p| p.width), Some(300)); + std::fs::rename(&moved, &file).unwrap(); + // A Pattern Fill layer gets the full pattern into the document. + t.execute("file.new", json!({"width": 40, "height": 40})).unwrap(); + t.execute("layer.newFillLayer.pattern", json!({"pattern": "Big"})).unwrap(); + let doc_pat = &t.active().unwrap().doc.patterns[0]; + assert_eq!((doc_pat.width, doc_pat.height), (300, 200)); + // Exporting the library writes full pixels too. + let out = dir.0.join("export.pat"); + t.execute("pattern.export", json!({"path": out.to_string_lossy()})).unwrap(); + let back = photocraft_io::pattern_map::read_pat(&std::fs::read(&out).unwrap()).unwrap(); + assert!(back.iter().any(|p| p.name == "Big" && p.width == 300)); + std::fs::remove_file(&out).unwrap(); + + t.execute("pattern.delete", json!({"pattern": "Big"})).unwrap(); + assert!(!dir.files().iter().any(|f| f.starts_with("pattern-")), "{:?}", dir.files()); +} + +/// A corrupt pattern file is skipped with a warning and left on disk; the others load, and later +/// saves never delete or overwrite it. A corrupt index is never overwritten. +#[test] +fn corrupt_pattern_files_are_skipped_and_kept() { + let dir = TempDir::new("patterns-corrupt"); + let (mut s, _) = session(&dir); + define_pattern(&mut s, "Grit", "#202020", 8); + define_pattern(&mut s, "Paper", "#f0e0c0", 12); + let grit = s.patterns.items.iter().find(|p| p.name == "Grit").unwrap().id.clone(); + let file = dir.0.join(patterns::file_name_for_test(&grit)); + std::fs::write(&file, b"not a pattern").unwrap(); + + let (mut t, w) = session(&dir); + assert!(w.iter().any(|w| w.contains("Grit")), "{w:?}"); + assert_eq!(user_patterns(&t), [("Paper".to_string(), 12)]); + define_pattern(&mut t, "Ink", "#000000", 4); + assert_eq!(std::fs::read(&file).unwrap(), b"not a pattern", "the unreadable file is left alone"); + let index = std::fs::read_to_string(dir.0.join(PATTERNS_FILE)).unwrap(); + assert!(index.contains("Grit") && index.contains("Ink"), "{index}"); + + std::fs::write(dir.0.join(PATTERNS_FILE), b"{").unwrap(); + let (mut u, w) = session(&dir); + assert!(w.iter().any(|w| w.contains(PATTERNS_FILE)), "{w:?}"); + assert!(user_patterns(&u).is_empty()); + define_pattern(&mut u, "Late", "#808080", 4); + assert_eq!(std::fs::read(dir.0.join(PATTERNS_FILE)).unwrap(), b"{", "an unreadable index is never overwritten"); +} + +/// Imported and defined patterns survive a restart: one file each, written once, plus the index; +/// a rename rewrites only the index, a delete removes the file. +#[test] +fn patterns_survive_a_restart() { + let dir = TempDir::new("patterns"); + let (mut s, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + define_pattern(&mut s, "Grit", "#202020", 8); + define_pattern(&mut s, "Paper", "#f0e0c0", 12); + let pattern_files = |d: &TempDir| d.files().into_iter().filter(|f| f.starts_with("pattern-")).collect::>(); + assert_eq!(pattern_files(&dir).len(), 2); + assert!(dir.files().contains(&PATTERNS_FILE.to_string())); + assert!(!dir.files().iter().any(|f| f.contains("Checkerboard")), "built-ins are never written"); + + let (mut t, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(user_patterns(&t), [("Grit".to_string(), 8), ("Paper".to_string(), 12)]); + // The pixels come back too. + let paper = t.patterns.items.iter().find(|p| p.name == "Paper").unwrap(); + let px = paper.surface.pixel(3, 3); + assert!((px[0] - 240.0 / 255.0).abs() < 0.01, "{px:?}"); + + let before = std::fs::metadata(dir.0.join(&pattern_files(&dir)[0])).unwrap().modified().unwrap(); + t.execute("pattern.rename", json!({"pattern": "Grit", "name": "Toner"})).unwrap(); + assert_eq!(std::fs::metadata(dir.0.join(&pattern_files(&dir)[0])).unwrap().modified().unwrap(), before, "a rename rewrites only the index"); + t.execute("pattern.delete", json!({"pattern": "Paper"})).unwrap(); + assert_eq!(pattern_files(&dir).len(), 1, "the deleted pattern's file is gone"); + + let (u, _) = session(&dir); + assert_eq!(user_patterns(&u), [("Toner".to_string(), 8)]); +} + +fn define_pattern(s: &mut Session, name: &str, color: &str, width: u32) { + s.execute("file.new", json!({"width": width, "height": 6})).unwrap(); + s.execute("edit.fill", json!({"color": color})).unwrap(); + s.execute("edit.definePattern", json!({"name": name})).unwrap(); +} + +fn user_patterns(s: &Session) -> Vec<(String, u32)> { + let builtins: Vec = crate::pattern_cmds::builtin().into_iter().map(|p| p.id).collect(); + s.patterns.items.iter().filter(|p| !builtins.contains(&p.id)).map(|p| (p.name.clone(), p.width)).collect() +} + +/// #9317: a defined or imported pattern survives a restart, as in Photoshop; the built-in +/// patterns are never written, and deleting the last user pattern removes the file. +#[test] +fn user_patterns_survive_a_restart() { + let dir = TempDir::new("patterns"); + let builtin = Session::new().patterns.items.len(); + let (mut s, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + s.execute("file.new", json!({"width": 12, "height": 10})).unwrap(); + s.execute("edit.fill", json!({"color": "#1ea05a"})).unwrap(); + let id = s.execute("edit.definePattern", json!({"name": "Green Tile"})).unwrap()["pattern"].as_str().unwrap().to_string(); + s.sync_preset_store(); + assert!(dir.files().contains(&PATTERNS_FILE.to_string()), "{:?}", dir.files()); + // Only the user's pattern is in the file. + let bytes = std::fs::read(dir.0.join(PATTERNS_FILE)).unwrap(); + let index: serde_json::Value = serde_json::from_slice(&bytes).unwrap(); + let saved: Vec = index["patterns"] + .as_array() + .unwrap() + .iter() + .map(|e| photocraft_io::pattern_map::read_pat(&std::fs::read(dir.0.join(e["file"].as_str().unwrap())).unwrap()).unwrap().remove(0)) + .collect(); + assert_eq!(saved.iter().map(|p| p.name.as_str()).collect::>(), ["Green Tile"]); + drop(s); + + let (mut t, w) = session(&dir); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(t.patterns.items.len(), builtin + 1); + let back = t.patterns.items.iter().find(|p| p.id == id).expect("the defined pattern is back after a restart"); + assert_eq!((back.name.as_str(), back.width, back.height), ("Green Tile", 12, 10)); + assert_eq!(back.surface, saved[0].surface, "with its pixels"); + // Renames are kept; deleting the last user pattern removes the file. + t.execute("pattern.rename", json!({"pattern": id, "name": "Moss"})).unwrap(); + t.sync_preset_store(); + let (u, _) = session(&dir); + assert!(u.patterns.items.iter().any(|p| p.id == id && p.name == "Moss")); + t.execute("pattern.delete", json!({"pattern": id})).unwrap(); + t.sync_preset_store(); + assert!( + serde_json::from_slice::(&std::fs::read(dir.0.join(PATTERNS_FILE)).unwrap()).unwrap()["patterns"].as_array().unwrap().is_empty(), + "empty authoritative index prevents resurrection" + ); +} + +#[test] +fn a_patterns_file_that_failed_to_load_is_not_overwritten() { + let mem = MemBackend::default(); + let unreadable = b"not a pattern file".to_vec(); + mem.files.lock().unwrap().insert(PATTERNS_FILE.into(), unreadable.clone()); + let mut s = Session::new(); + let w = s.attach_preset_store(open(Box::new(mem.clone()))); + assert!(w.iter().any(|w| w.contains(PATTERNS_FILE)), "{w:?}"); + s.execute("file.new", json!({"width": 4, "height": 4})).unwrap(); + s.execute("edit.definePattern", json!({"name": "New"})).unwrap(); + s.sync_preset_store(); + assert_eq!(mem.files.lock().unwrap().get(PATTERNS_FILE), Some(&unreadable), "a patterns file the store could not read was overwritten"); + assert!(!s.preset_store.as_mut().unwrap().take_warnings().is_empty()); +} + +/// A `.pat` of one 4×4 pattern with id `id`, filled with `rgba`. +fn pat_bytes(id: &str, rgba: [f32; 4]) -> String { + use photocraft_geom::Rect; + use photocraft_raster::Surface; + let mut surface = Surface::new(photocraft_color::PixelFormat::RGBA8); + surface.fill_rect(Rect::new(0, 0, 4, 4), &rgba); + let mut p = photocraft_doc::Pattern::new("Same", surface, 4, 4); + p.id = id.into(); + photocraft_paint::tile::b64_encode(&photocraft_io::pattern_map::write_pat(&[p]).unwrap()) +} + +/// #9318 review: re-importing a pattern under the same id, name and size but with other pixels +/// (an edited `.pat`) is written, so a restart doesn't bring the old pixels back. +#[test] +fn a_same_id_pattern_replacement_survives_a_restart() { + let mem = MemBackend::default(); + let mut s = Session::new(); + s.attach_preset_store(open(Box::new(mem.clone()))); + let id = "6f1c2c1e-0000-4000-8000-000000009318"; + s.execute("pattern.import", json!({"data": pat_bytes(id, [1.0, 0.0, 0.0, 1.0])})).unwrap(); + s.sync_preset_store(); + s.execute("pattern.import", json!({"data": pat_bytes(id, [0.0, 0.0, 1.0, 1.0])})).unwrap(); + s.sync_preset_store(); + let mut t = Session::new(); + t.attach_preset_store(open(Box::new(mem.clone()))); + let back = t.patterns.items.iter().find(|p| p.id == id).expect("the pattern is back"); + let want = s.patterns.items.iter().find(|p| p.id == id).unwrap(); + assert_eq!(back.surface, want.surface, "with the replacement's pixels, not the first import's"); +} + +/// A [`MemBackend`] whose removals fail while `fail` is set. +#[derive(Clone, Default)] +struct FlakyRemove { + mem: MemBackend, + fail: std::sync::Arc, +} + +impl PresetBackend for FlakyRemove { + fn list(&self) -> Result, String> { + self.mem.list() + } + fn read(&self, name: &str, max: u64) -> Result, String> { + self.mem.read(name, max) + } + fn write(&self, name: &str, bytes: &[u8]) -> Result<(), String> { + self.mem.write(name, bytes) + } + fn remove(&self, name: &str) -> Result<(), String> { + if self.fail.load(std::sync::atomic::Ordering::SeqCst) { + return Err(format!("{name}: busy")); + } + self.mem.remove(name) + } +} + +/// #9318 review: deleting the last user pattern while `patterns.pat` can't be removed is +/// warned and retried, so the deleted pattern doesn't come back after a restart. +#[test] +fn a_failed_patterns_removal_is_retried() { + let backend = FlakyRemove::default(); + let mut s = Session::new(); + s.attach_preset_store(open(Box::new(backend.clone()))); + s.execute("file.new", json!({"width": 4, "height": 4})).unwrap(); + let id = s.execute("edit.definePattern", json!({"name": "Gone"})).unwrap()["pattern"].as_str().unwrap().to_string(); + s.sync_preset_store(); + assert!(backend.mem.files.lock().unwrap().contains_key(PATTERNS_FILE)); + backend.fail.store(true, std::sync::atomic::Ordering::SeqCst); + s.execute("pattern.delete", json!({"pattern": id})).unwrap(); + s.sync_preset_store(); + assert!(!s.preset_store.as_mut().unwrap().take_warnings().is_empty(), "the failure is warned"); + backend.fail.store(false, std::sync::atomic::Ordering::SeqCst); + s.execute("file.new", json!({"width": 4, "height": 4})).unwrap(); + s.sync_preset_store(); + assert!(!backend.mem.files.lock().unwrap().keys().any(|f| f.starts_with("pattern-")), "owned asset removal is retried"); + assert!( + serde_json::from_slice::(backend.mem.files.lock().unwrap().get(PATTERNS_FILE).unwrap()).unwrap()["patterns"] + .as_array() + .unwrap() + .is_empty() + ); +} + +/// #9318 review: an imported `.pat` that replaces a built-in pattern's pixels (same id) is saved +/// and replaces the built-in after a restart, once; untouched built-ins are still never written. +#[test] +fn an_imported_builtin_replacement_survives_a_restart() { + let mem = MemBackend::default(); + let mut s = Session::new(); + s.attach_preset_store(open(Box::new(mem.clone()))); + let builtin = s.patterns.items[0].id.clone(); + s.execute("pattern.import", json!({"data": pat_bytes(&builtin, [0.9, 0.1, 0.4, 1.0])})).unwrap(); + s.sync_preset_store(); + let index: serde_json::Value = serde_json::from_slice(mem.files.lock().unwrap().get(PATTERNS_FILE).expect("written")).unwrap(); + let saved: Vec = index["patterns"] + .as_array() + .unwrap() + .iter() + .map(|e| photocraft_io::pattern_map::read_pat(mem.files.lock().unwrap().get(e["file"].as_str().unwrap()).unwrap()).unwrap().remove(0)) + .collect(); + assert_eq!(saved.iter().map(|p| p.id.as_str()).collect::>(), [builtin.as_str()], "only the replaced built-in"); + let mut t = Session::new(); + t.attach_preset_store(open(Box::new(mem.clone()))); + let back: Vec<_> = t.patterns.items.iter().filter(|p| p.id == builtin).collect(); + assert_eq!(back.len(), 1, "once"); + let want = s.patterns.items.iter().find(|p| p.id == builtin).unwrap(); + assert_eq!(back[0].surface, want.surface, "with the imported pixels"); +} + +/// #9318 review: a pattern imported before the store finished loading, with the id, name and +/// size of a saved one but other pixels, wins the merge and is written, so a restart keeps it. +#[test] +fn an_early_same_id_import_wins_and_is_saved() { + let mem = MemBackend::default(); + let id = "6f1c2c1e-0000-4000-8000-00000000e4a1"; + let mut s = Session::new(); + s.attach_preset_store(open(Box::new(mem.clone()))); + s.execute("pattern.import", json!({"data": pat_bytes(id, [1.0, 0.0, 0.0, 1.0])})).unwrap(); + s.sync_preset_store(); + // Next start: the same pattern with other pixels arrives before the store is attached. + let mut t = Session::new(); + t.execute("pattern.import", json!({"data": pat_bytes(id, [0.0, 1.0, 0.0, 1.0])})).unwrap(); + let early = t.patterns.items.iter().find(|p| p.id == id).unwrap().surface.clone(); + t.attach_preset_store(open(Box::new(mem.clone()))); + t.sync_preset_store(); + let mut u = Session::new(); + u.attach_preset_store(open(Box::new(mem.clone()))); + assert_eq!(u.patterns.items.iter().find(|p| p.id == id).unwrap().surface, early, "the early import's pixels were saved"); +} +#[derive(Clone)] +struct MigrationFail { + mem: MemBackend, + fail_name: String, +} +impl PresetBackend for MigrationFail { + fn list(&self) -> Result, String> { + self.mem.list() + } + fn read(&self, n: &str, m: u64) -> Result, String> { + self.mem.read(n, m) + } + fn write(&self, n: &str, b: &[u8]) -> Result<(), String> { + if n == self.fail_name { Err("injected migration write failure".into()) } else { self.mem.write(n, b) } + } + fn remove(&self, n: &str) -> Result<(), String> { + self.mem.remove(n) + } +} +fn legacy_store() -> (MemBackend, Vec, String) { + let mem = MemBackend::default(); + let id = "6f1c2c1e-0000-4000-8000-000000005176".to_string(); + let bytes = photocraft_paint::tile::b64_decode(&pat_bytes(&id, [0.2, 0.6, 0.8, 1.0])).unwrap(); + mem.write(LEGACY_PATTERNS_FILE, &bytes).unwrap(); + (mem, bytes, id) +} +#[test] +fn legacy_migration_commits_last_and_never_resurrects_renames_or_deletions() { + let (mem, old, id) = legacy_store(); + let mut s = Session::new(); + assert!(s.attach_preset_store(open(Box::new(mem.clone()))).is_empty()); + assert!(mem.read(PATTERNS_FILE, 1 << 20).is_ok()); + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(), old); + s.execute("pattern.rename", json!({"pattern":id,"name":"Migrated"})).unwrap(); + s.sync_preset_store(); + let mut t = Session::new(); + t.attach_preset_store(open(Box::new(mem.clone()))); + assert_eq!(t.patterns.items.iter().find(|p| p.id == id).unwrap().name, "Migrated"); + t.execute("pattern.delete", json!({"pattern":id})).unwrap(); + t.sync_preset_store(); + for _ in 0..2 { + let mut u = Session::new(); + u.attach_preset_store(open(Box::new(mem.clone()))); + assert!(!u.patterns.items.iter().any(|p| p.id == id)); + } + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(), old); +} +#[test] +fn interrupted_migration_preserves_legacy_and_reuses_only_identical_orphans() { + for fail_index in [false, true] { + let (mem, old, id) = legacy_store(); + let file = patterns::file_name_for_test(&id); + let fail_name = if fail_index { PATTERNS_FILE.to_string() } else { file.clone() }; + let opened = open(Box::new(MigrationFail { mem: mem.clone(), fail_name })); + assert!(!opened.warnings.is_empty()); + assert!(opened.patterns.iter().any(|p| p.pattern.id == id), "legacy remains usable"); + assert!(mem.read(PATTERNS_FILE, 1 << 20).is_err(), "no commit marker on failure"); + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(), old); + let retry = open(Box::new(mem.clone())); + assert!(retry.warnings.is_empty(), "{:?}", retry.warnings); + assert!(retry.patterns.iter().any(|p| p.pattern.id == id)); + } + let (mem, old, id) = legacy_store(); + let file = patterns::file_name_for_test(&id); + mem.write(&file, b"unknown content").unwrap(); + let opened = open(Box::new(mem.clone())); + assert!(!opened.warnings.is_empty()); + assert_eq!(mem.read(&file, 100).unwrap(), b"unknown content"); + assert!(mem.read(PATTERNS_FILE, 1 << 20).is_err()); + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(), old); +} +#[test] +fn malformed_authoritative_index_never_falls_back_to_stale_legacy() { + let (mem, _, id) = legacy_store(); + drop(open(Box::new(mem.clone()))); + for bytes in [b"{".as_slice(), b"{\"version\":1,\"patterns\":[{\"id\":\"x\",\"name\":\"bad\",\"file\":\"../secret\"}]}".as_slice()] { + mem.write(PATTERNS_FILE, bytes).unwrap(); + let opened = open(Box::new(mem.clone())); + assert!(!opened.warnings.is_empty()); + assert!(opened.patterns.is_empty()); + assert_eq!(mem.read(PATTERNS_FILE, 1 << 20).unwrap(), bytes); + assert!(!opened.patterns.iter().any(|p| p.pattern.id == id)); + } + let mem = MemBackend::default(); + mem.write(LEGACY_PATTERNS_FILE, b"broken PAT").unwrap(); + let opened = open(Box::new(mem.clone())); + assert!(!opened.warnings.is_empty()); + assert!(mem.read(PATTERNS_FILE, 1 << 20).is_err()); + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, 20).unwrap(), b"broken PAT"); +} +#[test] +fn migration_preserves_duplicate_names_but_refuses_duplicate_ids() { + let (mem, _, id) = legacy_store(); + let bytes = mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(); + let mut pats = photocraft_io::pattern_map::read_pat(&bytes).unwrap(); + let mut other = pats[0].clone(); + other.id = "6f1c2c1e-0000-4000-8000-000000005177".into(); + pats.push(other); + mem.write(LEGACY_PATTERNS_FILE, &photocraft_io::pattern_map::write_pat(&pats).unwrap()).unwrap(); + let opened = open(Box::new(mem.clone())); + assert!(opened.warnings.is_empty()); + assert_eq!(opened.patterns.len(), 2); + let mem = MemBackend::default(); + pats[1].id = id; + let old = photocraft_io::pattern_map::write_pat(&pats).unwrap(); + mem.write(LEGACY_PATTERNS_FILE, &old).unwrap(); + let opened = open(Box::new(mem.clone())); + assert!(!opened.warnings.is_empty()); + assert!(mem.read(PATTERNS_FILE, 1 << 20).is_err()); + assert_eq!(mem.read(LEGACY_PATTERNS_FILE, MAX_PATTERNS_BYTES).unwrap(), old); +} diff --git a/crates/engine/src/presets/mod.rs b/crates/engine/src/presets.rs similarity index 88% rename from crates/engine/src/presets/mod.rs rename to crates/engine/src/presets.rs index 6c722c62ef..efdb16cfc8 100644 --- a/crates/engine/src/presets/mod.rs +++ b/crates/engine/src/presets.rs @@ -91,6 +91,8 @@ pub struct PresetState { pub layer_defaults: BTreeMap, /// Swatches panel library. pub swatches: Vec>, + /// The user contour library of the Layer Style dialog (`layer.layerStyle.contours.*`). + pub contours: Vec, /// Bumped on every preset change (UIs key thumbnail caches on it). pub rev: u64, } @@ -111,6 +113,7 @@ impl Default for PresetState { clone: Default::default(), layer_defaults: BTreeMap::new(), swatches: swatches::builtin(), + contours: Vec::new(), rev: 0, } } @@ -144,6 +147,9 @@ struct Persisted { #[serde(default)] layer_defaults: Option>, swatches: Option>>, + // Read entry by entry: a bad contour must not discard the other presets. + #[serde(default)] + contours: Option, } impl PresetState { @@ -162,16 +168,37 @@ impl PresetState { custom_shapes: Some(s.edit_state.custom_shapes.clone()), layer_defaults: Some(self.layer_defaults.clone()), swatches: Some(self.swatches.clone()), + contours: Some(crate::contour_lib_cmds::to_json(&self.contours)), }; serde_json::to_value(p).unwrap_or(Value::Null) } } impl Session { - /// Restore presets saved by [`PresetState::to_json`]. Missing sets keep their defaults; a - /// malformed blob is ignored (presets are never worth refusing to start over). + /// Restore presets saved by [`PresetState::to_json`]. Missing or malformed sets keep their + /// defaults (presets are never worth refusing to start over). Each set loads on its own: the + /// shell writes back only the keys that changed, so one damaged set would otherwise discard + /// every other set, saved document presets included, at each launch (#5602). pub fn load_presets_json(&mut self, v: Value) { - let Ok(p) = serde_json::from_value::(v) else { return }; + let Value::Object(mut m) = v else { return }; + fn take(m: &mut serde_json::Map, key: &str) -> Option { + m.remove(key).filter(|v| !v.is_null()).and_then(|v| serde_json::from_value(v).ok()) + } + let p = Persisted { + gradients: take(&mut m, "gradients"), + gradient: take(&mut m, "gradient"), + styles: take(&mut m, "styles"), + shapes: take(&mut m, "shapes"), + pattern_groups: take(&mut m, "pattern_groups"), + tool_presets: take(&mut m, "tool_presets"), + documents: take(&mut m, "documents"), + recent_documents: take(&mut m, "recent_documents"), + export_presets: take(&mut m, "export_presets"), + custom_shapes: take(&mut m, "custom_shapes"), + layer_defaults: take(&mut m, "layer_defaults"), + swatches: take(&mut m, "swatches"), + contours: take(&mut m, "contours"), + }; let st = &mut self.presets; if let Some(g) = p.gradients { st.gradients = g; @@ -211,6 +238,9 @@ impl Session { crate::swatch_cmds::cap_library(&mut g); self.presets.swatches = g; } + if let Some(c) = p.contours.filter(Value::is_array) { + self.presets.contours = crate::contour_lib_cmds::from_json(&c); + } self.presets.rev += 1; } @@ -343,6 +373,9 @@ pub(crate) fn edit_groups(groups: &mut Vec>, action: &str, p: let g = req_str(p, "group", cmd)?; let to = req_str(p, "name", cmd)?.to_string(); let i = groups.iter().position(|x| x.name == g).ok_or_else(|| bad(cmd, format!("no group \"{g}\"")))?; + if g != to && groups.iter().any(|x| x.name == to) { + return Err(bad(cmd, format!("a group named \"{to}\" already exists"))); + } groups[i].name = to.clone(); Ok(json!({"group": to})) } diff --git a/crates/engine/src/presets/gradients.rs b/crates/engine/src/presets/gradients.rs index 7c92dfba97..20cb95d95b 100644 --- a/crates/engine/src/presets/gradients.rs +++ b/crates/engine/src/presets/gradients.rs @@ -441,15 +441,47 @@ fn active_gradient_fill(s: &Session) -> Option { matches!(d.doc.layer(id)?.content, LayerContent::Fill(Fill::Gradient { .. })).then_some(id) } +fn active_shape(s: &Session) -> Option { + let d = s.active()?; + let id = d.active_layer?; + matches!(d.doc.layer(id)?.content, LayerContent::Shape(_)).then_some(id) +} + fn select(s: &mut Session, p: &Value) -> Result { let g = preset_param(s, p, CMD_SELECT)?; + let apply = p.get("applyToLayer").and_then(Value::as_bool).unwrap_or(true); + let shape = active_shape(s).filter(|_| apply); + // A locked shape refuses before anything changes, the Gradient tool's gradient included. + if let (Some(id), Some(d)) = (shape, s.active()) { + let locks = d.doc.effective_locks(id); + if locks.all || locks.pixels { + return Err(bad(CMD_SELECT, "the selected shape layer is locked")); + } + } s.presets.gradient = g.clone(); s.presets_changed(); let mut out = json!({"current": g.to_json()}); + // Clicking a preset with a shape layer selected fills the shape with it (Photoshop, #5568); + // a gradient fill keeps its angle, style, scale and reverse. + if let Some(id) = shape { + let (stops_new, opacity_new) = g.fill_stops(s.tools.foreground, s.tools.background); + crate::vector_cmds::with_shape(s, id, "Edit Shape", |sh, _| { + let (angle, scale, style, reverse) = match &sh.fill { + Some(Fill::Gradient { angle, scale, style, reverse, .. }) => (*angle, *scale, *style, *reverse), + _ => (90.0, 1.0, photocraft_doc::GradientStyle::Linear, false), + }; + let mut fill = Fill::gradient(stops_new, angle, scale, style, reverse); + if let Fill::Gradient { opacity_stops, .. } = &mut fill { + *opacity_stops = opacity_new; + } + sh.fill = Some(fill); + Ok(()) + })?; + out["layer"] = json!(id.0); + return Ok(out); + } // Clicking a preset with a Gradient Fill layer selected changes that layer (Photoshop). - if p.get("applyToLayer").and_then(Value::as_bool).unwrap_or(true) - && let Some(id) = active_gradient_fill(s) - { + if apply && let Some(id) = active_gradient_fill(s) { let (stops_new, opacity_new) = g.fill_stops(s.tools.foreground, s.tools.background); s.edit("Change Gradient Fill", |doc, _| { let mode = doc.mode; @@ -603,13 +635,14 @@ fn apply(s: &mut Session, p: &Value) -> Result { const CMD: &str = "gradient.presets.apply"; let g = preset_param(s, p, CMD)?; let stops = g.doc_stops(s.tools.foreground, s.tools.background); - let angle = p.get("angle").and_then(Value::as_f64).unwrap_or(90.0) as f32; - let scale = (p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32 / 100.0).clamp(0.1, 1.5); + let angle = crate::gradient_fill_cmds::angle_param(CMD, p, 90.0)?; + let scale = crate::gradient_fill_cmds::clamp_scale(p.get("scale").and_then(Value::as_f64).unwrap_or(100.0) as f32 / 100.0); let style = crate::layer_style::gradient_style(p.get("style").and_then(Value::as_str).unwrap_or("linear")); let reverse = p.get("reverse").and_then(Value::as_bool).unwrap_or(false); let id = s.edit("New Gradient Fill Layer", |doc, active| { let fill = Fill::gradient(stops, angle, scale, style, reverse).in_mode(doc.mode); - let id = doc.insert_above(*active, Layer::new(doc.next_layer_name("Gradient Fill"), LayerContent::Fill(fill))); + let layer = Layer::new(doc.next_layer_name("Gradient Fill"), LayerContent::Fill(fill)); + let id = crate::layer_place::insert_new(doc, *active, layer, crate::layer_place::Place::Auto); *active = Some(id); Ok(id) })?; @@ -674,7 +707,7 @@ pub fn specs() -> Vec { menu: &[], shortcut: None, params: leak(format!( - r##"{{"preset":name|{stops},"group":name?,"applyToLayer":bool=true (also recolours a selected Gradient Fill layer)}} → {{current,layer?}}. The Gradient tool paints with it."## + r##"{{"preset":name|{stops},"group":name?,"applyToLayer":bool=true (also recolours a selected Gradient Fill layer, or fills a selected shape layer with it)}} → {{current,layer?}}. The Gradient tool paints with it."## )), enabled: always, run: select, @@ -696,7 +729,7 @@ pub fn specs() -> Vec { menu: &[], shortcut: None, params: leak(format!( - r##"{{"preset":name?=current|{stops},"angle":deg=90,"style":"linear|radial|angle|reflected|diamond"="linear","scale":10..150=100,"reverse":bool}} → {{layer}}"## + r##"{{"preset":name?=current|{stops},"angle":deg=90,"style":"linear|radial|angle|reflected|diamond"="linear","scale":1..1000=100,"reverse":bool}} → {{layer}}"## )), enabled: super::has_doc, run: apply, diff --git a/crates/engine/src/presets/patterns.rs b/crates/engine/src/presets/patterns.rs index 2425dd0900..a1d9ff2bf0 100644 --- a/crates/engine/src/presets/patterns.rs +++ b/crates/engine/src/presets/patterns.rs @@ -162,7 +162,7 @@ fn edit(s: &mut Session, p: &Value) -> Result { } "move" => { let key = req_str(p, "preset", CMD)?; - let id = crate::pattern_cmds::resolve(s, key).map(|q| q.id).ok_or_else(|| bad(CMD, format!("no pattern \"{key}\"")))?; + let id = crate::pattern_cmds::resolve_id(s, key).ok_or_else(|| bad(CMD, format!("no pattern \"{key}\"")))?; let mut q = p.clone(); q["preset"] = json!(id); edit_groups(&mut s.presets.pattern_groups, "move", &q, CMD)? diff --git a/crates/engine/src/presets/styles.rs b/crates/engine/src/presets/styles.rs index ee4607cf86..cac8fb158a 100644 --- a/crates/engine/src/presets/styles.rs +++ b/crates/engine/src/presets/styles.rs @@ -6,7 +6,7 @@ //! `layer.layerStyle.*`. use photocraft_color::{BlendMode, ColorMode, SampleType}; -use photocraft_doc::{Document, Effect, Layer, Size}; +use photocraft_doc::{Document, Effect, Layer, Pattern, Size}; use photocraft_geom::Rect; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; @@ -25,6 +25,9 @@ pub struct StylePreset { pub blend: Option, #[serde(default)] pub fill_opacity: Option, + /// Layer opacity saved with the blending options (0..1). + #[serde(default)] + pub opacity: Option, } impl Named for StylePreset { @@ -43,7 +46,7 @@ impl StylePreset { .as_array() .map(|a| a.iter().filter_map(|e| effect_from_params(e.get(0)?.as_str()?, e.get(1).unwrap_or(&Value::Null))).collect()) .unwrap_or_default(); - StylePreset { name: name.into(), effects, blend: None, fill_opacity: fill } + StylePreset { name: name.into(), effects, blend: None, fill_opacity: fill, opacity: None } } pub fn to_json(&self) -> Value { @@ -52,6 +55,7 @@ impl StylePreset { "effects": self.effects.iter().map(|e| e.label()).collect::>(), "blend": self.blend.map(|b| b.label()), "fillOpacity": self.fill_opacity.map(|f| (f * 100.0).round()), + "opacity": self.opacity.map(|f| (f * 100.0).round()), }) } } @@ -166,6 +170,51 @@ fn targets(s: &Session, p: &Value) -> Vec { crate::layer_multi_cmds::selected(s) } +/// Resolves each effect's pattern, plus the patterns that strokes and bevels name, copied from the library. +fn resolve_style_effects(s: &Session, fx_list: &[Effect]) -> Result<(Vec, Vec)> { + let mut effects = Vec::new(); + let mut pats = Vec::new(); + for fx in fx_list { + let (fx, pat) = crate::pattern_cmds::resolve_effect(s, fx.clone())?; + effects.push(fx); + pats.extend(pat); + } + // Pattern strokes and bevel textures name their pattern too: copy it when the library has it. + for (name, id) in photocraft_io::asl_map::referenced_patterns(&effects) { + if let Some(pat) = crate::pattern_cmds::resolve(s, if id.is_empty() { &name } else { &id }).filter(|_| !(id.is_empty() && name.is_empty())) { + pats.push(pat); + } + } + Ok((effects, pats)) +} + +/// Writes a preset onto one layer: replaces its effects, or appends to them when `add`. +fn apply_to_layer(l: &mut Layer, style: &StylePreset, effects: &[Effect], add: bool) { + if add { + l.effects.items.extend(effects.iter().cloned()); + } else { + l.effects.items = effects.to_vec(); + l.effects.psd_raw = None; + } + l.effects.psd_raw = None; + l.effects.enabled = true; + // Adding keeps the layer's blending unless the preset carries some: a blend mode, or + // an Opacity saved on its own (`style.presets.new` with `includeBlending: false`). + if !add || style.blend.is_some() || style.opacity.is_some() { + if let Some(b) = style.blend { + l.blend = b; + } + if let Some(f) = style.fill_opacity { + l.fill_opacity = f; + } else if !add { + l.fill_opacity = 1.0; + } + if let Some(o) = style.opacity { + l.opacity = o; + } + } +} + fn apply(s: &mut Session, p: &Value) -> Result { const CMD: &str = "style.presets.apply"; let style = find_style(s, p, CMD)?.clone(); @@ -175,13 +224,7 @@ fn apply(s: &mut Session, p: &Value) -> Result { return Err(bad(CMD, "no target layers")); } // Pattern overlays name their pattern; resolve to library patterns copied into the document. - let mut effects = Vec::new(); - let mut pats = Vec::new(); - for fx in style.effects { - let (fx, pat) = crate::pattern_cmds::resolve_effect(s, fx)?; - effects.push(fx); - pats.extend(pat); - } + let (effects, pats) = resolve_style_effects(s, &style.effects)?; let label = format!("Apply Style: {}", style.name); s.edit(&label, |doc, _| { for pat in &pats { @@ -194,24 +237,7 @@ fn apply(s: &mut Session, p: &Value) -> Result { // Styles can't sit on the Background: Photoshop turns it into a normal layer first. crate::extra_cmds::background_to_layer(doc, *id); let l = doc.layer_mut(*id).ok_or(EngineError::NoLayer(*id))?; - if add { - l.effects.items.extend(effects.iter().cloned()); - } else { - l.effects.items = effects.clone(); - l.effects.psd_raw = None; - } - l.effects.psd_raw = None; - l.effects.enabled = true; - if !add || style.blend.is_some() { - if let Some(b) = style.blend { - l.blend = b; - } - if let Some(f) = style.fill_opacity { - l.fill_opacity = f; - } else if !add { - l.fill_opacity = 1.0; - } - } + apply_to_layer(l, &style, &effects, add); } Ok(()) })?; @@ -256,6 +282,7 @@ fn new_preset(s: &mut Session, p: &Value) -> Result { }, blend: p.get("blend").and_then(Value::as_str).and_then(crate::commands::blend_from_str).or_else(|| with_blend.then_some(l.blend)), fill_opacity: p.get("fillOpacity").and_then(Value::as_f64).map(|v| (v / 100.0).clamp(0.0, 1.0) as f32).or_else(|| with_blend.then_some(l.fill_opacity)), + opacity: p.get("opacity").and_then(Value::as_f64).map(|v| (v / 100.0).clamp(0.0, 1.0) as f32).or_else(|| with_blend.then_some(l.opacity)), }; let gi = group_index(&mut s.presets.styles, str_param(p, "group")); let out = json!({"name": style.name, "group": s.presets.styles[gi].name, "effects": style.effects.len()}); @@ -336,7 +363,7 @@ pub fn specs() -> Vec { label: "Style Presets", menu: &[], shortcut: None, - params: "{} → {groups:[{name,presets:[{name,effects,blend,fillOpacity}]}]}", + params: "{} → {groups:[{name,presets:[{name,effects,blend,fillOpacity,opacity}]}]}", enabled: always, run: list, journal: false, @@ -356,7 +383,7 @@ pub fn specs() -> Vec { label: "New Style…", menu: &[], shortcut: None, - params: r##"{"name":str="Style","group":name?,"layer":id?,"includeEffects":bool=true,"includeBlending":bool=true,"effects":[[kind, params]]? (explicit list, e.g. the Layer Style dialog's pending state; overrides includeEffects),"blend":str?,"fillOpacity":0..100? (override the layer's blending options)}"##, + params: r##"{"name":str="Style","group":name?,"layer":id?,"includeEffects":bool=true,"includeBlending":bool=true,"effects":[[kind, params]]? (explicit list, e.g. the Layer Style dialog's pending state; overrides includeEffects),"blend":str?,"fillOpacity":0..100?,"opacity":0..100? (override the layer's blending options)}"##, enabled: has_layer, run: new_preset, journal: true, diff --git a/crates/engine/src/presets/tests.rs b/crates/engine/src/presets/tests.rs index dcfc4e94d8..b196117839 100644 --- a/crates/engine/src/presets/tests.rs +++ b/crates/engine/src/presets/tests.rs @@ -317,6 +317,23 @@ fn gradient_new_rename_move_delete_groups() { assert!(s.execute("gradient.presets.edit", json!({"action": "explode"})).is_err()); } +#[test] +fn gradient_rename_group_refuses_a_name_another_group_has() { + let mut s = Session::new(); + s.execute("gradient.presets.edit", json!({"action": "newGroup", "name": "Pastels"})).unwrap(); + let names = |s: &mut Session| -> Vec { + s.execute("gradient.presets.list", json!({})).unwrap()["groups"].as_array().unwrap().iter().map(|g| g["name"].clone()).collect() + }; + let before = names(&mut s); + assert!(before.contains(&json!("Blues"))); + let err = s.execute("gradient.presets.edit", json!({"action": "renameGroup", "group": "Pastels", "name": "Blues"})).unwrap_err(); + assert!(err.to_string().contains("already exists"), "{err}"); + assert_eq!(names(&mut s), before, "a refused rename leaves the groups unchanged"); + // Renaming a group to its own name is fine. + s.execute("gradient.presets.edit", json!({"action": "renameGroup", "group": "Pastels", "name": "Pastels"})).unwrap(); + assert_eq!(names(&mut s), before); +} + #[test] fn presets_persist_through_preferences_json() { let mut s = Session::new(); @@ -460,6 +477,25 @@ fn styles_apply_to_selected_layers_and_new_from_layer() { assert!(px[(24 * 48 + 24) * 4 + 3] == 255); } +/// A preset saved without blending options but with an explicit Opacity carries only that +/// opacity: ⇧-click (`add`) sets it and leaves the layer's blend mode and Fill alone. +#[test] +fn an_opacity_only_style_added_sets_just_the_opacity() { + let mut s = session(8); + let a = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().map(LayerId).unwrap_or_else(|| active(&s)); + s.execute("style.presets.new", json!({"name": "Faded", "includeBlending": false, "effects": [["stroke", {"size": 2}]], "opacity": 40})).unwrap(); + s.edit("Blending fixture", |doc, _| { + let l = doc.layer_mut(a).unwrap(); + (l.blend, l.fill_opacity, l.opacity) = (photocraft_doc::BlendMode::Multiply, 0.7, 0.9); + Ok(()) + }) + .unwrap(); + s.execute("style.presets.apply", json!({"preset": "Faded", "add": true, "layer": a.0})).unwrap(); + let l = s.active().unwrap().doc.layer(a).unwrap().clone(); + assert_eq!((l.blend, l.fill_opacity, l.opacity), (photocraft_doc::BlendMode::Multiply, 0.7, 0.4)); + assert_eq!(l.effects.items.len(), 1); +} + #[test] fn styles_with_pattern_overlay_copy_the_pattern() { let mut s = session(8); @@ -470,6 +506,7 @@ fn styles_with_pattern_overlay_copy_the_pattern() { effects: vec![crate::layer_style::effect_from_params("patternOverlay", &json!({"pattern": "Bricks"})).unwrap()], blend: None, fill_opacity: None, + opacity: None, }); s.execute("style.presets.apply", json!({"preset": "Bricky", "layer": id.0})).unwrap(); assert!(s.active().unwrap().doc.patterns.iter().any(|p| p.name == "Bricks")); @@ -748,3 +785,114 @@ fn clone_source_errors_and_overlay() { assert_eq!(v["width"], json!(1000.0)); assert_eq!(v["rotation"], json!(-90.0)); } + +fn shape_fill(s: &Session, id: LayerId) -> Option { + match &s.active().unwrap().doc.layer(id).unwrap().content { + LayerContent::Shape(sh) => sh.fill.clone(), + _ => panic!("not a shape layer"), + } +} + +/// #5568: clicking a gradient with a shape layer selected fills the shape with it in one undo +/// step; a gradient fill keeps its angle, style, scale and reverse. +#[test] +fn gradient_preset_click_fills_the_selected_shape_layer() { + let mut s = session(8); + let id = LayerId(s.execute("shape.create", json!({"rect": [8, 8, 40, 30], "fill": "#336699"})).unwrap()["layer"].as_u64().unwrap()); + let before = shape_fill(&s, id); + let r = s.execute("gradient.presets.select", json!({"preset": "Red 01"})).unwrap(); + assert_eq!(r["layer"], json!(id.0)); + let Some(Fill::Gradient { stops, angle, style, .. }) = shape_fill(&s, id) else { panic!("not a gradient") }; + // Red 01 starts at #7f0000; a new gradient fill is linear at 90°. + assert_eq!(stops[0].1.to_rgba8(), [0x7f, 0, 0, 0xff]); + assert_eq!((angle, style), (90.0, photocraft_doc::GradientStyle::Linear)); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(shape_fill(&s, id), before); + + s.execute("shape.edit", json!({"layer": id.0, "fill": {"gradient": {"angle": 30, "style": "radial", "scale": 80, "reverse": true}}})).unwrap(); + s.execute("gradient.presets.select", json!({"preset": "Green 01"})).unwrap(); + let Some(Fill::Gradient { angle, style, scale, reverse, .. }) = shape_fill(&s, id) else { panic!("not a gradient") }; + assert_eq!((angle, style, scale, reverse), (30.0, photocraft_doc::GradientStyle::Radial, 0.8, true)); +} + +/// A locked shape refuses the click before anything changes, the Gradient tool's gradient included. +#[test] +fn gradient_preset_click_on_a_locked_shape_changes_nothing() { + let mut s = session(8); + let id = LayerId(s.execute("shape.create", json!({"rect": [8, 8, 40, 30], "fill": "#336699"})).unwrap()["layer"].as_u64().unwrap()); + let (before, gradient) = (shape_fill(&s, id), s.presets.gradient.name.clone()); + s.execute("layer.lockLayers", json!({"all": true})).unwrap(); + let err = s.execute("gradient.presets.select", json!({"preset": "Red 01"})).unwrap_err().to_string(); + assert!(err.contains("locked"), "{err}"); + assert_eq!(shape_fill(&s, id), before); + assert_eq!(s.presets.gradient.name, gradient); +} + +// ------------------------------------------------------------------ persistence + +/// #5602: one damaged preset set in the preferences file must not discard the saved New +/// Document presets (or any other set) at the next launch. +#[test] +fn a_damaged_preset_set_keeps_the_other_sets() { + let mut s = Session::new(); + let settings = json!({"width": 1600, "height": 900, "resolution": 144}); + s.execute("document.presets.save", json!({"name": "Banner", "settings": settings})).unwrap(); + let export = json!({ + "format": "jpg", "quality": 70, "lossless": false, "transparency": false, "scale": 50, "width": 640, "height": 480, + "lockAspect": true, "sizing": "pixels", "metadata": "exif", "profile": "srgb", "embedProfile": false, "tgaBits": "24" + }); + s.execute("file.export.presets.save", json!({"name": "Web JPEG", "settings": export})).unwrap(); + let saved = s.presets.to_json(&s); + for key in + ["gradients", "gradient", "styles", "shapes", "pattern_groups", "recent_documents", "custom_shapes", "layer_defaults", "swatches", "export_presets"] + { + let mut blob = saved.clone(); + blob[key] = json!("not a preset set"); + let mut next = Session::new(); + next.load_presets_json(blob); + assert_eq!(next.presets.documents, s.presets.documents, "damaged `{key}`"); + assert_eq!(next.presets.tool_presets.len(), Session::new().presets.tool_presets.len(), "damaged `{key}`"); + if key != "export_presets" { + assert_eq!(next.presets.export_presets, s.presets.export_presets, "damaged `{key}`"); + } + } + let mut blob = saved.clone(); + blob["export_presets"] = json!("not a preset set"); + let mut next = Session::new(); + next.load_presets_json(blob); + assert!(next.presets.export_presets.is_empty()); + assert_eq!(next.presets.documents, s.presets.documents); + let mut blob = saved.clone(); + blob["documents"] = json!("not a preset set"); + let mut next = Session::new(); + next.load_presets_json(blob); + assert!(next.presets.documents.is_empty()); + assert_eq!(next.presets.export_presets, s.presets.export_presets); + // Null sets keep their defaults, as before. + let mut next = Session::new(); + next.load_presets_json( + json!({"tool_presets": null, "gradients": null, "documents": saved["documents"].clone(), "export_presets": saved["export_presets"].clone()}), + ); + assert_eq!(next.presets.tool_presets.len(), Session::new().presets.tool_presets.len()); + assert_eq!(next.presets.gradients.len(), Session::new().presets.gradients.len()); + assert_eq!(next.presets.documents, s.presets.documents); + assert_eq!(next.presets.export_presets, s.presets.export_presets); +} + +// ------------------------------------------------------------------ export presets + +#[test] +fn export_presets_survive_a_presets_json_round_trip() { + let mut s = Session::new(); + s.execute("file.export.presets.save", json!({"name": "Web", "settings": {"format": "jpg", "quality": 70}})).unwrap(); + assert_eq!(s.presets.export_presets.len(), 1); + + let saved = s.presets.to_json(&s); + assert_eq!(saved["export_presets"].as_array().map(Vec::len), Some(1)); + + let mut restored = Session::new(); + restored.load_presets_json(saved); + assert_eq!(restored.presets.export_presets.len(), 1); + assert_eq!(restored.presets.export_presets[0].name, "Web"); + assert_eq!(restored.presets.export_presets[0].settings.format, "jpg"); +} diff --git a/crates/engine/src/presets/tools.rs b/crates/engine/src/presets/tools.rs index 796a66623e..2746c1576e 100644 --- a/crates/engine/src/presets/tools.rs +++ b/crates/engine/src/presets/tools.rs @@ -37,7 +37,7 @@ impl Named for ToolPreset { pub const MAX_PRESETS: usize = 1000; /// A preset name: trimmed, 1–[`MAX_NAME`](crate::brush_preset_cmds::MAX_NAME) characters. -fn checked_name(name: &str, cmd: &str) -> Result { +pub(crate) fn checked_name(name: &str, cmd: &str) -> Result { let name = name.trim(); if name.is_empty() { return Err(bad(cmd, "the preset name is empty")); diff --git a/crates/engine/src/print_cmds.rs b/crates/engine/src/print_cmds.rs deleted file mode 100644 index 9a0af4a054..0000000000 --- a/crates/engine/src/print_cmds.rs +++ /dev/null @@ -1,819 +0,0 @@ -//! File › Print…, Print One Copy, File › Package… and File › Export › Paths to Illustrator…. -//! -//! Printing renders the flattened document into a PDF page (paper size, orientation, position, -//! scale, colour handling with printer profile / intent / black-point compensation, corner and -//! centre crop marks, registration marks, description and label) with a small PDF writer -//! (ISO 32000: one page, a Flate-compressed image XObject, vector marks, Helvetica text), then -//! hands its bytes to `craft-print` (CUPS on macOS/Linux, Windows PDF printing on Windows). -//! `"dryRun": true` reports the backend command without spooling; a PDF is kept only at an -//! explicit output path. Shared spooler transport files are private and automatically removed. -//! -//! Paths to Illustrator writes the document's paths as an Adobe Illustrator 3 compatible -//! PostScript file (`m`/`L`/`C` path construction, `n`/`N` unpainted closed/open paths, `*u`/`*U` -//! compound paths), following the public PostScript Language Reference and the AI3 format notes. - -use std::io::Write as _; - -mod windows_marks; - -use photocraft_color::ColorMode; -use photocraft_doc::{Document, LayerContent, SmartSource}; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::file_cmds::{file_name, join, native_doc, read_file, stem, write_file}; -use crate::{EngineError, Result, Session}; - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} -fn other(e: impl std::fmt::Display) -> EngineError { - EngineError::Other(e.to_string()) -} - -// ---------- PDF ---------- - -/// Paper sizes in points (portrait). -pub const PAPERS: [(&str, f64, f64); 8] = [ - ("letter", 612.0, 792.0), - ("legal", 612.0, 1008.0), - ("tabloid", 792.0, 1224.0), - ("a3", 841.89, 1190.55), - ("a4", 595.28, 841.89), - ("a5", 419.53, 595.28), - ("4x6", 288.0, 432.0), - ("5x7", 360.0, 504.0), -]; - -/// The image placed on a print page. -pub struct PrintImage { - pub width: u32, - pub height: u32, - /// 1 (gray), 3 (RGB) or 4 (CMYK, 1 = full ink), 8-bit interleaved. - pub channels: usize, - pub data: Vec, - pub icc: Option>, -} - -/// Printer marks around the image. -#[derive(Clone, Copy, Debug, Default, PartialEq)] -pub struct Marks { - pub corner_crop: bool, - pub center_crop: bool, - pub registration: bool, -} - -/// One page: paper (pt), image box (pt, origin bottom-left), marks and texts. -pub struct PrintPage { - pub paper: (f64, f64), - pub image: PrintImage, - pub rect: (f64, f64, f64, f64), - pub marks: Marks, - pub description: Option, - pub label: Option, - /// Photoshop's Functions › Emulsion Down: the whole page mirrored left to right, for transfer - /// printing (sublimation, iron-on, DTF, film read from the back). - pub emulsion_down: bool, -} - -fn pdf_string(s: &str) -> String { - let mut o = String::from("("); - for c in s.chars() { - match c { - '(' | ')' | '\\' => { - o.push('\\'); - o.push(c); - } - c if c.is_ascii() && !c.is_ascii_control() => o.push(c), - _ => o.push('?'), - } - } - o.push(')'); - o -} - -fn deflate(data: &[u8]) -> Vec { - // Fast: print PDFs are transient and large; level 1 is ~4× quicker than the default. - let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); - let _ = e.write_all(data); - e.finish().unwrap_or_default() -} - -/// Vector marks around `(x, y, w, h)`. -fn marks_ops(m: Marks, (x, y, w, h): (f64, f64, f64, f64)) -> String { - let mut s = String::from("q 0 G 0.3 w\n"); - let (gap, len) = (6.0, 18.0); - let line = |s: &mut String, x0: f64, y0: f64, x1: f64, y1: f64| s.push_str(&format!("{x0:.2} {y0:.2} m {x1:.2} {y1:.2} l S\n")); - if m.corner_crop { - for (cx, cy, dx, dy) in [(x, y, -1.0, -1.0), (x + w, y, 1.0, -1.0), (x, y + h, -1.0, 1.0), (x + w, y + h, 1.0, 1.0)] { - line(&mut s, cx + dx * gap, cy, cx + dx * (gap + len), cy); - line(&mut s, cx, cy + dy * gap, cx, cy + dy * (gap + len)); - } - } - if m.center_crop { - let (mx, my) = (x + w / 2.0, y + h / 2.0); - line(&mut s, mx, y - gap, mx, y - gap - len); - line(&mut s, mx, y + h + gap, mx, y + h + gap + len); - line(&mut s, x - gap, my, x - gap - len, my); - line(&mut s, x + w + gap, my, x + w + gap + len, my); - } - if m.registration { - let r = 5.0; - let off = gap + len / 2.0; - for (cx, cy) in [(x + w / 2.0, y - off), (x + w / 2.0, y + h + off), (x - off, y + h / 2.0), (x + w + off, y + h / 2.0)] { - // Circle (four Béziers) plus crosshair. - let k = 0.5523 * r; - s.push_str(&format!( - "{:.2} {cy:.2} m {:.2} {:.2} {:.2} {:.2} {cx:.2} {:.2} c {:.2} {:.2} {:.2} {:.2} {:.2} {cy:.2} c {:.2} {:.2} {:.2} {:.2} {cx:.2} {:.2} c {:.2} {:.2} {:.2} {:.2} {:.2} {cy:.2} c S\n", - cx + r, cx + r, cy + k, cx + k, cy + r, cy + r, cx - k, cy + r, cx - r, cy + k, cx - r, cx - r, cy - k, cx - k, cy - r, cy - r, cx + k, cy - r, cx + r, cy - k, cx + r - )); - line(&mut s, cx - r * 1.6, cy, cx + r * 1.6, cy); - line(&mut s, cx, cy - r * 1.6, cx, cy + r * 1.6); - } - } - s.push_str("Q\n"); - s -} - -/// A one-page PDF. -pub fn print_pdf(page: &PrintPage) -> Vec { - let mut out: Vec = b"%PDF-1.4\n%\xe2\xe3\xcf\xd3\n".to_vec(); - let mut offsets: Vec = Vec::new(); - let mut obj = |out: &mut Vec, body: &[u8]| { - offsets.push(out.len()); - out.extend_from_slice(format!("{} 0 obj\n", offsets.len()).as_bytes()); - out.extend_from_slice(body); - out.extend_from_slice(b"\nendobj\n"); - }; - let img = &page.image; - let (pw, ph) = page.paper; - let (x, y, w, h) = page.rect; - // 1 catalog, 2 pages, 3 page, 4 contents, 5 image, 6 font, 7 ICC (optional). - obj(&mut out, b"<< /Type /Catalog /Pages 2 0 R >>"); - obj(&mut out, b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>"); - obj( - &mut out, - format!( - "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 {pw:.2} {ph:.2}] /Resources << /XObject << /Im0 5 0 R >> /Font << /F1 6 0 R >> >> /Contents 4 0 R >>" - ) - .as_bytes(), - ); - // An explicit white page, so every viewer (and rasteriser) shows paper, not transparency. - let mut content = format!("q 1 g 0 0 {pw:.2} {ph:.2} re f Q\n"); - if page.emulsion_down { - content.push_str(&format!("q -1 0 0 1 {pw:.2} 0 cm\n")); - } - content.push_str(&format!("q {w:.3} 0 0 {h:.3} {x:.3} {y:.3} cm /Im0 Do Q\n")); - content.push_str(&marks_ops(page.marks, page.rect)); - let any_marks = page.marks.corner_crop || page.marks.center_crop || page.marks.registration; - let pad = if any_marks { 30.0 } else { 6.0 }; - if let Some(l) = page.label.as_deref().filter(|l| !l.is_empty()) { - content.push_str(&format!("BT /F1 8 Tf 0 g {:.2} {:.2} Td {} Tj ET\n", x, y + h + pad, pdf_string(l))); - } - if let Some(d) = page.description.as_deref().filter(|d| !d.is_empty()) { - content.push_str(&format!("BT /F1 8 Tf 0 g {:.2} {:.2} Td {} Tj ET\n", x, y - pad - 8.0, pdf_string(d))); - } - if page.emulsion_down { - content.push_str("Q\n"); - } - let c = deflate(content.as_bytes()); - let mut body = format!("<< /Length {} /Filter /FlateDecode >>\nstream\n", c.len()).into_bytes(); - body.extend_from_slice(&c); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - let device = match img.channels { - 1 => "/DeviceGray", - 4 => "/DeviceCMYK", - _ => "/DeviceRGB", - }; - let cs = if img.icc.is_some() { "[/ICCBased 7 0 R]".to_string() } else { device.to_string() }; - let z = deflate(&img.data); - let mut body = format!( - "<< /Type /XObject /Subtype /Image /Width {} /Height {} /ColorSpace {cs} /BitsPerComponent 8 /Filter /FlateDecode /Length {} >>\nstream\n", - img.width, - img.height, - z.len() - ) - .into_bytes(); - body.extend_from_slice(&z); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - obj(&mut out, b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>"); - if let Some(icc) = &img.icc { - let z = deflate(icc); - let mut body = format!("<< /N {} /Alternate {device} /Filter /FlateDecode /Length {} >>\nstream\n", img.channels, z.len()).into_bytes(); - body.extend_from_slice(&z); - body.extend_from_slice(b"\nendstream"); - obj(&mut out, &body); - } - let xref = out.len(); - out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", offsets.len() + 1).as_bytes()); - for o in &offsets { - out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes()); - } - out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", offsets.len() + 1).as_bytes()); - out -} - -// ---------- Print ---------- - -/// The flattened, colour-handled document as 8-bit samples in its print colour space. -fn print_image(doc: &Document, p: &Value, cmd: &str) -> Result<(PrintImage, Value)> { - let mut t = Session::new(); - t.add_document(doc.clone(), None); - let handling = p.get("colorHandling").and_then(Value::as_str).unwrap_or("printerManages"); - let mut info = json!({"colorHandling": handling}); - if !matches!(doc.mode, ColorMode::Rgb | ColorMode::Grayscale | ColorMode::Cmyk) { - t.execute("image.mode.rgb", json!({}))?; - } - match handling { - "photocraftManages" | "photoshopManages" => { - let profile = p.get("printerProfile").and_then(Value::as_str).ok_or_else(|| bad(cmd, "\"photocraftManages\" needs a \"printerProfile\""))?; - let intent = p.get("intent").and_then(Value::as_str).unwrap_or("relative"); - let bpc = p.get("bpc").and_then(Value::as_bool).unwrap_or(true); - t.execute("edit.convertToProfile", json!({"profile": profile, "intent": intent, "bpc": bpc}))?; - info["printerProfile"] = json!(profile); - info["intent"] = json!(intent); - info["bpc"] = json!(bpc); - } - "printerManages" | "noColorManagement" => {} - "separations" => return Err(bad(cmd, "Separations printing is not supported; use photocraftManages with a CMYK printer profile")), - h => return Err(bad(cmd, format!("unknown colorHandling `{h}` (printerManages|photocraftManages|noColorManagement)"))), - } - let d = &t.active().ok_or(EngineError::NoDocument)?.doc; - let icc = if handling == "noColorManagement" { None } else { d.icc_profile.as_ref().map(|v| v.to_vec()) }; - // RGB and Grayscale: the composite over white is the print image (no layer flatten). - if matches!(d.mode, ColorMode::Rgb | ColorMode::Grayscale) { - let gray = d.mode == ColorMode::Grayscale; - let buf = photocraft_compose::flatten(d).over_background([1.0, 1.0, 1.0]); - let to8 = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - let data: Vec = - if gray { buf.px.iter().map(|p| to8(p[0])).collect() } else { buf.px.iter().flat_map(|p| [to8(p[0]), to8(p[1]), to8(p[2])]).collect() }; - return Ok((PrintImage { width: d.size.width, height: d.size.height, channels: if gray { 1 } else { 3 }, data, icc }, info)); - } - t.execute("layer.flattenImage", json!({}))?; - let d = &t.active().ok_or(EngineError::NoDocument)?.doc; - let fmt = d.pixel_format(); - let k = fmt.mode.color_channels(); - let n = fmt.channels(); - let surf = d.layers.first().and_then(|l| l.surface()).ok_or_else(|| other("nothing to print"))?; - let vals = surf.read_region(d.bounds()); - let mut data = Vec::with_capacity(vals.len() / n * k); - for px in vals.chunks_exact(n) { - for v in &px[..k] { - data.push((v.clamp(0.0, 1.0) * 255.0).round() as u8); - } - } - Ok((PrintImage { width: d.size.width, height: d.size.height, channels: k, data, icc }, info)) -} - -fn paper(p: &Value, cmd: &str) -> Result<(f64, f64)> { - let (w, h) = match p.get("paper") { - None => (612.0, 792.0), - Some(Value::String(name)) => PAPERS - .iter() - .find(|(n, _, _)| n.eq_ignore_ascii_case(name)) - .map(|(_, w, h)| (*w, *h)) - .ok_or_else(|| bad(cmd, format!("unknown paper `{name}` ({})", PAPERS.map(|p| p.0).join("|"))))?, - Some(Value::Array(a)) if a.len() == 2 => (a[0].as_f64().unwrap_or(0.0), a[1].as_f64().unwrap_or(0.0)), - _ => return Err(bad(cmd, "paper is a name or [width, height] in points")), - }; - if !(w > 36.0 && h > 36.0) { - return Err(bad(cmd, "paper is too small")); - } - Ok(if p.get("orientation").and_then(Value::as_str) == Some("landscape") { (h.max(w), w.min(h)) } else { (w, h) }) -} - -/// Where the image lands on the paper. -#[derive(Clone, Copy, Debug, PartialEq)] -pub struct PrintLayout { - /// Paper size in points (after orientation). - pub paper: (f64, f64), - /// Image box in points, origin bottom-left. - pub rect: (f64, f64, f64, f64), - /// Print scale (1.0 = 100 %). - pub scale: f64, - pub marks: Marks, - /// Emulsion Down: the page prints mirrored (`rect` is where the image is before mirroring). - pub emulsion_down: bool, -} - -/// The print layout for `doc` with Print parameters `p` (used by the dialog's preview too). -pub fn layout(doc: &Document, p: &Value, cmd: &str) -> Result { - let (pw, ph) = paper(p, cmd)?; - let dpi = f64::from(doc.resolution_dpi.max(1.0)); - let (iw, ih) = (f64::from(doc.size.width) * 72.0 / dpi, f64::from(doc.size.height) * 72.0 / dpi); - let marks = Marks { - corner_crop: p.get("cornerCropMarks").and_then(Value::as_bool).unwrap_or(false), - center_crop: p.get("centerCropMarks").and_then(Value::as_bool).unwrap_or(false), - registration: p.get("registrationMarks").and_then(Value::as_bool).unwrap_or(false), - }; - let margin = if marks.corner_crop || marks.center_crop || marks.registration { 36.0 } else { 18.0 }; - let fit = p.get("scaleToFit").and_then(Value::as_bool).unwrap_or(false); - let scale = if fit { - ((pw - 2.0 * margin) / iw).min((ph - 2.0 * margin) / ih) - } else { - p.get("scale").and_then(Value::as_f64).filter(|v| *v > 0.0).unwrap_or(100.0) / 100.0 - }; - let (w, h) = (iw * scale, ih * scale); - let center = p.get("center").and_then(Value::as_bool).unwrap_or(true); - let (x, y) = if center { - ((pw - w) / 2.0, (ph - h) / 2.0) - } else { - // Top / left in inches from the paper's top-left corner (Photoshop's Position fields). - let top = p.get("top").and_then(Value::as_f64).unwrap_or(0.0) * 72.0; - let left = p.get("left").and_then(Value::as_f64).unwrap_or(0.0) * 72.0; - (left, ph - top - h) - }; - let emulsion_down = p.get("emulsionDown").and_then(Value::as_bool).unwrap_or(false); - Ok(PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down }) -} - -/// Lays out, renders and (unless dry-run) spools a print. Shared by Print and Print One Copy. -fn do_print(s: &mut Session, p: &Value, cmd: &str) -> Result { - let output = p.get("output").and_then(Value::as_str).filter(|v| !v.is_empty()); - let send = p.get("send").and_then(Value::as_bool).unwrap_or(output.is_none()); - let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); - if !dry && !send && output.is_none() { - return Err(bad(cmd, "send: false needs an output path or dryRun: true")); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let doc = d.doc.clone(); - let doc_id = doc.id; - let params = p.clone(); - let worker_params = params.clone(); - let command = cmd.to_string(); - if send && !dry { - // Session::execute remains synchronous; the desktop's Session::start snapshots here - // and runs PDF composition and the potentially slow driver on a worker. - return crate::jobs::run( - s, - "Print", - false, - move |ctx| { - ctx.progress(0.0, "Print"); - render_and_spool(&doc, &worker_params, &command) - }, - move |s, result| { - remember_print(s, ¶ms); - // A user can switch tabs while printing. Hooks belong to the source document; - // a closed source has no document left to modify, as for background saves. - let viewed = s.active().map(|st| st.doc.id); - if let Some(index) = s.documents().iter().position(|st| st.doc.id == doc_id) { - s.set_active(index); - crate::automate_cmds::fire_event(s, "print"); - if let Some(index) = viewed.and_then(|id| s.documents().iter().position(|st| st.doc.id == id)) { - s.set_active(index); - } - } - Ok(result) - }, - ); - } - let result = render_and_spool(&doc, p, cmd)?; - remember_print(s, p); - crate::automate_cmds::fire_event(s, "print"); - Ok(result) -} - -fn render_and_spool(doc: &Document, p: &Value, cmd: &str) -> Result { - let PrintLayout { paper: (pw, ph), rect: (x, y, w, h), scale, marks, emulsion_down } = layout(doc, p, cmd)?; - let (img, color) = print_image(doc, p, cmd)?; - let description = p - .get("description") - .and_then(Value::as_bool) - .unwrap_or(false) - .then(|| crate::file_cmds::read_file_info(doc.metadata.xmp.as_deref())["description"].as_str().unwrap_or_default().to_string()); - let label = p.get("labels").and_then(Value::as_bool).unwrap_or(false).then(|| doc.name.clone()); - let page = PrintPage { paper: (pw, ph), image: img, rect: (x, y, w, h), marks, description, label, emulsion_down }; - let output = p.get("output").and_then(Value::as_str).filter(|v| !v.is_empty()).map(str::to_string); - let send = p.get("send").and_then(Value::as_bool).unwrap_or(output.is_none()); - let copies = crate::commands::int(p, "copies").unwrap_or(1).clamp(1, 999) as u32; - let dry = p.get("dryRun").and_then(Value::as_bool).unwrap_or(false); - // Shared RGB transport handles compatible Windows pages; PDF-only content retains its route. - // Sending follows the shared command contract; fuzz tests explicitly request dryRun. - // The worker returns its result before repeat settings and hooks are applied once. - let handling = p.get("colorHandling").and_then(Value::as_str).unwrap_or("printerManages"); - if cfg!(windows) && send && !dry && output.is_none() && native_rgb_compatible(&page, handling) { - require_printing_enabled()?; - let printed = print_windows(&page, p, &doc.name, copies)?; - return Ok(json!({ - "pdf": Value::Null, "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, - "copies": copies, "command": Value::Null, "sent": true, "spooler": printed, "color": color, - })); - } - if cfg!(windows) && send && !dry && output.is_none() && printer_settings(p)?.is_some() { - return Err(other("the selected color profile or page text requires PDF printing. Save as PDF, or clear Print Settings to use PDF printing.")); - } - let pdf = print_pdf(&page); - let job = craft_print::Job { - printer: p.get("printer").and_then(Value::as_str).filter(|v| !v.is_empty()).map(str::to_string), - copies, - title: doc.name.clone(), - // PhotoCraft already placed and scaled the image on its physical sheet. Scaling again - // in the driver would change the preview, print resolution and crop-mark positions. - scaling: craft_print::Scaling::Exact, - ..Default::default() - }; - let command = if send { json!(craft_print::command_preview(&job).map_err(other)?) } else { Value::Null }; - let pdf_path = match output { - Some(path) => { - write_file(&path, &pdf)?; - Some(path) - } - None => None, - }; - let mut sent = false; - let mut spool = Value::Null; - if send && !dry { - require_printing_enabled()?; - spool = json!(craft_print::submit(&pdf, &job).map_err(other)?); - sent = true; - } - Ok( - json!({"pdf": pdf_path, "bytes": pdf.len(), "paper": [pw, ph], "imageRect": [x, y, w, h], "emulsionDown": emulsion_down, "scale": scale * 100.0, "copies": copies, "command": command, "sent": sent, "spooler": spool, "color": color}), - ) -} - -/// Keep CMYK output profiles and page text on the existing PDF composition path. GDI currently -/// draws RGB/gray and vector marks only; it must never silently omit requested page content. -fn native_rgb_compatible(page: &PrintPage, handling: &str) -> bool { - matches!(page.image.channels, 1 | 3) - && (page.image.icc.is_none() || matches!(handling, "photocraftManages" | "photoshopManages" | "noColorManagement")) - && page.description.as_deref().is_none_or(str::is_empty) - && page.label.as_deref().is_none_or(str::is_empty) -} - -/// Print One Copy repeats these. -fn remember_print(s: &mut Session, p: &Value) { - let mut remembered = p.clone(); - if let Some(o) = remembered.as_object_mut() { - o.remove("output"); - o.remove("dryRun"); - // Output and dry-run choices are not remembered, so a send override must not survive them. - o.remove("send"); - } - s.file_menu.last_print = Some(remembered); -} - -/// The `printer` parameter, or the system's default printer. -fn printer_name(p: &Value) -> Result { - if let Some(name) = p.get("printer").and_then(Value::as_str).filter(|v| !v.trim().is_empty()) { - return Ok(name.to_string()); - } - craft_print::rgb::default_printer().map_err(other)?.ok_or_else(|| other("no printer chosen and no default printer is set")) -} - -/// The driver settings chosen with Print Settings… (`printerSettings`, hex), when any. -fn printer_settings(p: &Value) -> Result> { - match p.get("printerSettings").and_then(Value::as_str).filter(|v| !v.is_empty()) { - Some(hex) => craft_print::rgb::DevMode::from_hex(hex).map(Some).map_err(|e| bad("file.print", e.to_string())), - None => Ok(None), - } -} - -/// The page's image as 8-bit RGB rows (gray expanded), mirrored for Emulsion Down. -pub fn page_rgb(page: &PrintPage) -> Result> { - let img = &page.image; - let (w, h) = (img.width as usize, img.height as usize); - let mut rgb = match img.channels { - 3 => img.data.clone(), - 1 => img.data.iter().flat_map(|g| [*g, *g, *g]).collect(), - _ => return Err(other("this printer profile is CMYK; Windows printers take RGB, so choose the printer's own (RGB) profile")), - }; - let row = w.checked_mul(3).ok_or_else(|| other("print image is too large"))?; - if row == 0 || row.checked_mul(h) != Some(rgb.len()) { - return Err(other("nothing to print")); - } - if page.emulsion_down { - for row in rgb.chunks_exact_mut(row) { - row.as_chunks_mut::<3>().0.reverse(); - } - } - Ok(rgb) -} - -/// The image box with its origin at the sheet's top-left (GDI's), mirrored for Emulsion Down. -pub fn page_rect_top_left(page: &PrintPage) -> (f64, f64, f64, f64) { - let (pw, ph) = page.paper; - let (x, y, w, h) = page.rect; - let x = if page.emulsion_down { pw - x - w } else { x }; - (x, ph - y - h, w, h) -} - -/// Prints `page` on a Windows printer. Corner/centre crop and registration marks are drawn with -/// GDI vector calls, matching the PDF path's geometry; description and label text are still -/// PDF-only (no font/text layout in the GDI path yet). -fn print_windows(page: &PrintPage, p: &Value, title: &str, copies: u32) -> Result { - let printer = printer_name(p)?; - let settings = printer_settings(p)?; - let device_page = craft_print::rgb::page(&printer, settings.as_ref()).map_err(other)?; - if (device_page.paper.0 - page.paper.0).abs() > 2.0 || (device_page.paper.1 - page.paper.1).abs() > 2.0 { - return Err(other("the driver paper or orientation differs from the print layout; choose the matching paper in Print Settings before printing")); - } - let rgb = page_rgb(page)?; - let image = craft_print::rgb::RgbImage { width: page.image.width, height: page.image.height, data: &rgb }; - let rect = page_rect_top_left(page); - let primitives = windows_marks::primitives(page.marks, rect); - let color_policy = match p.get("colorHandling").and_then(Value::as_str) { - Some("photocraftManages" | "photoshopManages") => craft_print::rgb::ColorPolicy::ApplicationManaged, - Some("noColorManagement") => craft_print::rgb::ColorPolicy::Unmanaged, - _ => craft_print::rgb::ColorPolicy::DriverManaged, - }; - let job = craft_print::rgb::Job { - printer: &printer, - settings: settings.as_ref(), - title, - output_file: None, - copies: copies.clamp(1, 999), - color_policy, - primitives: &primitives, - }; - let done = craft_print::rgb::print(&job, &image, rect).map_err(other)?; - Ok( - json!({"printer": printer, "dpi": [done.dpi.0, done.dpi.1], "deviceRect": [done.device_rect.0, done.device_rect.1, done.device_rect.2, done.device_rect.3]}), - ) -} - -fn printers(_: &mut Session, _: &Value) -> Result { - match craft_print::rgb::printers() { - Ok(list) => Ok(json!({"supported": true, "printers": list.iter().map(|p| json!({"name": p.name, "default": p.default})).collect::>()})), - Err(craft_print::rgb::PrintError::Unsupported) => Ok(json!({"supported": false, "printers": []})), - Err(e) => Err(other(e)), - } -} - -fn printer_page(_: &mut Session, p: &Value) -> Result { - let printer = printer_name(p)?; - let pg = craft_print::rgb::page(&printer, printer_settings(p)?.as_ref()).map_err(other)?; - Ok( - json!({"printer": printer, "paper": [pg.paper.0, pg.paper.1], "printable": [pg.printable.0, pg.printable.1, pg.printable.2, pg.printable.3], "dpi": [pg.dpi.0, pg.dpi.1]}), - ) -} - -/// Set once by test harnesses (the panic hunt) so no command in the process can reach a printer. -static PRINTING_DISABLED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); - -/// Refuse to spool anything from this process from now on (tests, fuzzing). The -/// `PHOTOCRAFT_NO_PRINT` environment variable does the same for a whole shell (CI). -pub fn disable_printing() { - PRINTING_DISABLED.store(true, std::sync::atomic::Ordering::Relaxed); -} - -/// Whether spooling is blocked by [`disable_printing`] or `PHOTOCRAFT_NO_PRINT`. -pub fn printing_disabled() -> bool { - PRINTING_DISABLED.load(std::sync::atomic::Ordering::Relaxed) || std::env::var_os("PHOTOCRAFT_NO_PRINT").is_some_and(|v| !v.is_empty() && v != "0") -} - -fn require_printing_enabled() -> Result<()> { - if printing_disabled() { - Err(other("printing is disabled in this process (photocraft_engine::print_cmds::disable_printing or PHOTOCRAFT_NO_PRINT); use dryRun: true")) - } else { - Ok(()) - } -} - -/// The printers the system spooler knows (Print's Printer menu): CUPS queues and driverless -/// destinations on macOS and Linux, `Win32_Printer` on Windows, none in the browser. -fn system_printers(_: &mut Session, _: &Value) -> Result { - let list = craft_print::printers(); - let default = list.iter().find(|p| p.default).map(|p| p.name.clone()); - let printers: Vec = list.into_iter().map(|p| json!({"name": p.name, "default": p.default})).collect(); - Ok(json!({"printers": printers, "default": default})) -} - -fn print(s: &mut Session, p: &Value) -> Result { - do_print(s, p, "file.print") -} - -fn print_one_copy(s: &mut Session, p: &Value) -> Result { - let mut q = s.file_menu.last_print.clone().unwrap_or_else(|| json!({})); - if let (Some(o), Some(extra)) = (q.as_object_mut(), p.as_object()) { - for (k, v) in extra { - o.insert(k.clone(), v.clone()); - } - } - q["copies"] = json!(1); - do_print(s, &q, "file.printOneCopy") -} - -// ---------- Package ---------- - -fn package(s: &mut Session, p: &Value) -> Result { - let cmd = "file.package"; - let dir = p.get("dir").and_then(Value::as_str).filter(|v| !v.is_empty()).ok_or_else(|| bad(cmd, "missing \"dir\" (where to create the package folder)"))?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let doc = (*d.doc).clone(); - let name = stem(&d.path.clone().unwrap_or_else(|| doc.name.clone())); - // The native format keeps external links; our PSD writer has no `lnkE` (external linked - // file) support yet, so a PSD package would embed or flatten them. - let ext = match p.get("format").and_then(Value::as_str) { - Some(f @ ("psd" | "psb" | "pcraft")) => f.to_string(), - Some(f) => return Err(bad(cmd, format!("unknown package format `{f}` (pcraft|psd|psb)"))), - None => "pcraft".into(), - }; - let folder = join(dir, &crate::file_cmds::sanitize(&name)); - let links_dir = join(&folder, "Links"); - // Copy every linked file once; relink the copy of the document to the copies. - let mut copied: Vec<(String, String)> = Vec::new(); - let mut relinks: Vec<(photocraft_doc::LayerId, String)> = Vec::new(); - let mut missing = Vec::new(); - for (_, _, l) in doc.walk() { - let LayerContent::Smart(so) = &l.content else { continue }; - let SmartSource::Linked { path } = &so.source else { continue }; - let target = match copied.iter().find(|(src, _)| src == path) { - Some((_, t)) => t.clone(), - None => { - let mut fname = file_name(path); - if copied.iter().any(|(_, t)| file_name(t) == fname) { - fname = format!("{}-{}", copied.len(), fname); - } - let t = join(&links_dir, &fname); - match read_file(path) { - Ok(bytes) => { - write_file(&t, &bytes)?; - copied.push((path.clone(), t.clone())); - t - } - Err(_) => { - missing.push(path.clone()); - continue; - } - } - } - }; - relinks.push((l.id, target)); - } - let mut t = Session::new(); - t.add_document(doc, None); - for (id, path) in &relinks { - t.execute("layer.smartObjects.relinkToFile", json!({"layer": id.0, "path": path}))?; - } - let out = join(&folder, &format!("{}.{ext}", crate::file_cmds::sanitize(&name))); - let pdoc = t.active().ok_or(EngineError::NoDocument)?.doc.clone(); - let warnings = crate::file_cmds::save_doc(&pdoc, &out, None)?; - Ok(json!({"folder": folder, "document": out, "links": copied.iter().map(|(_, t)| t).collect::>(), "missing": missing, "warnings": warnings})) -} - -// ---------- Paths to Illustrator ---------- - -/// The document's paths as an Illustrator 3 PostScript file. `which`: "all", "work" or a path -/// name. Coordinates are points (pixels at the document resolution), y up. -pub fn paths_to_ai(doc: &Document, which: &str) -> Result<(String, usize)> { - let dpi = f64::from(doc.resolution_dpi.max(1.0)); - let k = 72.0 / dpi; - let (w, h) = (f64::from(doc.size.width) * k, f64::from(doc.size.height) * k); - let mut chosen: Vec<(String, &photocraft_doc::Path)> = Vec::new(); - match which { - "all" => { - chosen.extend(doc.paths.iter().map(|p| (p.name.clone(), &p.path))); - if let Some(wp) = &doc.work_path { - chosen.push(("Work Path".into(), wp)); - } - } - "work" => chosen.extend(doc.work_path.iter().map(|wp| ("Work Path".to_string(), wp))), - name => chosen.extend(doc.paths.iter().filter(|p| p.name == name).map(|p| (p.name.clone(), &p.path))), - } - let mut s = String::new(); - s.push_str("%!PS-Adobe-2.0 EPSF-1.2\n%%Creator: PhotoCraft\n"); - s.push_str(&format!("%%Title: ({})\n", doc.name.replace([')', '('], "_"))); - s.push_str(&format!( - "%%BoundingBox: 0 0 {} {}\n%%HiResBoundingBox: 0 0 {w:.4} {h:.4}\n%AI3_Cropmarks: 0 0 {w:.4} {h:.4}\n", - w.ceil() as i64, - h.ceil() as i64 - )); - s.push_str("%%DocumentProcessColors: Black\n%%EndComments\n%%EndProlog\n%%BeginSetup\n%%EndSetup\n"); - let pt = |x: f64, y: f64| (x * k, h - y * k); - let mut n = 0; - for (name, path) in &chosen { - let subs: Vec<_> = path.subpaths.iter().filter(|sp| !sp.knots.is_empty()).collect(); - if subs.is_empty() { - continue; - } - n += 1; - s.push_str(&format!("%%Note: {name}\n")); - let compound = subs.len() > 1; - if compound { - s.push_str("*u\n"); - } - for sp in subs { - let (x, y) = pt(sp.knots[0].anchor.x, sp.knots[0].anchor.y); - s.push_str(&format!("{x:.4} {y:.4} m\n")); - for seg in sp.segments() { - let [a, c1, c2, b] = seg; - let straight = c1 == a && c2 == b; - let (bx, by) = pt(b.x, b.y); - if straight { - s.push_str(&format!("{bx:.4} {by:.4} L\n")); - } else { - let (x1, y1) = pt(c1.x, c1.y); - let (x2, y2) = pt(c2.x, c2.y); - s.push_str(&format!("{x1:.4} {y1:.4} {x2:.4} {y2:.4} {bx:.4} {by:.4} C\n")); - } - } - // Unpainted: `n` closes the path, `N` leaves it open. - s.push_str(if sp.closed { "n\n" } else { "N\n" }); - } - if compound { - s.push_str("*U\n"); - } - } - s.push_str("%%PageTrailer\n%%Trailer\n%%EOF\n"); - Ok((s, n)) -} - -fn paths_to_illustrator(s: &mut Session, p: &Value) -> Result { - let cmd = "file.export.pathsToIllustrator"; - let d = s.active().ok_or(EngineError::NoDocument)?; - let which = p.get("paths").and_then(Value::as_str).unwrap_or("all"); - let (text, n) = paths_to_ai(&d.doc, which)?; - if n == 0 { - return Err(bad(cmd, format!("no paths match `{which}`"))); - } - match p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty()) { - Some(path) => { - write_file(path, text.as_bytes())?; - Ok(json!({"path": path, "paths": n})) - } - None => Ok(json!({"ai": text, "paths": n})), - } -} - -fn has_paths(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - if d.doc.paths.is_empty() && d.doc.work_path.is_none() { Err("the document has no paths".into()) } else { Ok(()) } -} - -pub fn specs() -> Vec { - macro_rules! spec { - ($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } - }; - } - const PRINT_PARAMS: &str = r##"{"printerSettings":hex? (Windows driver settings),"printer":name? (default printer),"copies":1..999=1,"paper":"letter|legal|tabloid|a3|a4|a5|4x6|5x7"|[w,h] pt="letter","orientation":"portrait|landscape"="portrait","center":bool=true,"top":in?,"left":in?,"scale":%=100,"scaleToFit":bool=false,"colorHandling":"printerManages|photocraftManages|noColorManagement"="printerManages","printerProfile":profile? (photocraftManages),"intent":"perceptual|relative|saturation|absolute"="relative","bpc":bool=true,"cornerCropMarks":bool,"centerCropMarks":bool,"registrationMarks":bool,"description":bool,"labels":bool,"emulsionDown":bool=false (mirror the page left to right, for sublimation and other transfer printing),"output":pdf path? (print to PDF; then "send" defaults to false),"send":bool?,"dryRun":bool=false (render the PDF, report the backend command, don't spool; write a PDF only at an explicit output path)} → {pdf:path|null (null without explicit output), imageRect, command:diagnostic array|null, sent}"##; - - vec![ - spec!("file.print", "Print…", &["File"], Some("Cmd+P"), PRINT_PARAMS, native_doc, print), - CommandSpec { - id: "file.print.printers", - label: "Printers", - menu: &[], - shortcut: None, - params: r##"{} → {supported, printers: [{name, default}]} (supported: Windows; elsewhere Print spools with lp)"##, - enabled: |_| Ok(()), - journal: false, - run: printers, - }, - CommandSpec { - id: "file.print.page", - label: "Printer Page", - menu: &[], - shortcut: None, - params: r##"{"printer":name? (default printer),"printerSettings":hex?} → {printer, paper: [w,h] pt, printable: [x,y,w,h] pt from the top-left, dpi: [x,y]} (Windows)"##, - enabled: |_| Ok(()), - journal: false, - run: printer_page, - }, - CommandSpec { - id: "file.printers", - label: "Printers", - menu: &[], - shortcut: None, - params: r##"{} → {printers:[{name, default}], default:name|null} (the system spooler's printers; a name is Print's "printer")"##, - enabled: crate::commands::always, - journal: false, - run: system_printers, - }, - spec!( - "file.printOneCopy", - "Print One Copy", - &["File"], - Some("Cmd+Alt+Shift+P"), - "{} (the last Print settings, one copy; any Print key overrides)", - native_doc, - print_one_copy - ), - spec!( - "file.package", - "Package…", - &["File"], - None, - r##"{"dir":folder,"format":"pcraft|psd|psb"="pcraft"} → {folder, document, links, missing} (copies the document and its linked files into //, relinked to Links/)"##, - native_doc, - package - ), - spec!( - "file.export.pathsToIllustrator", - "Paths to Illustrator…", - &["File", "Export"], - None, - r##"{"path":str? (.ai; omit to return the text),"paths":"all|work|"="all"} → {path, paths}"##, - has_paths, - paths_to_illustrator - ), - ] -} - -#[cfg(test)] -#[path = "print_cmds/tests.rs"] -mod tests; diff --git a/crates/engine/src/print_cmds/tests.rs b/crates/engine/src/print_cmds/tests.rs deleted file mode 100644 index d012f6de41..0000000000 --- a/crates/engine/src/print_cmds/tests.rs +++ /dev/null @@ -1,436 +0,0 @@ -use super::*; -use photocraft_doc::{Knot, NamedPath, Path, Subpath}; - -fn tmp(name: &str) -> String { - let d = std::env::temp_dir().join(format!("photocraft-print-{}-{name}", std::process::id())); - let _ = std::fs::remove_dir_all(&d); - std::fs::create_dir_all(&d).unwrap(); - d.to_string_lossy().into_owned() -} - -fn session(mode: &str, depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 300, "height": 150, "mode": mode, "depth": depth, "name": "Print Me.psd", "background": "#3366cc"})).unwrap(); - s -} - -fn find(h: &[u8], n: &[u8], from: usize) -> usize { - from + h[from..].windows(n.len()).position(|w| w == n).unwrap() -} - -/// The (inflated) image stream of a print PDF: (width, height, colour space, samples). -fn pdf_image(pdf: &[u8]) -> (u32, u32, String, Vec) { - let i = find(pdf, b"/Subtype /Image", 0); - let end = find(pdf, b">>\nstream\n", i); - let dict = String::from_utf8_lossy(&pdf[i..end]).into_owned(); - let num = |k: &str| dict.split(k).nth(1).unwrap().split_whitespace().next().unwrap().parse::().unwrap(); - let cs = dict.split("/ColorSpace ").nth(1).unwrap().split(" /BitsPerComponent").next().unwrap().to_string(); - let len = num("/Length ") as usize; - let start = end + 10; - let mut out = Vec::new(); - std::io::Read::read_to_end(&mut flate2::read::ZlibDecoder::new(&pdf[start..start + len]), &mut out).unwrap(); - (num("/Width "), num("/Height "), cs, out) -} - -#[test] -fn print_dry_run_renders_a_pdf_and_reports_the_platform_backend() { - let dir = tmp("print"); - for depth in [8, 16, 32] { - let mut s = session("rgb", depth); - let out = format!("{dir}/rgb-{depth}.pdf"); - let r = s - .execute( - "file.print", - json!({"output": out, "send": true, "dryRun": true, "printer": "Office", "copies": 2, "paper": "a4", "cornerCropMarks": true, "registrationMarks": true, "labels": true}), - ) - .unwrap(); - let cmd: Vec = serde_json::from_value(r["command"].clone()).unwrap(); - #[cfg(unix)] - { - assert_eq!(&cmd[..5], &["lp", "-d", "Office", "-n", "2"]); - assert!(cmd.contains(&"fit-to-page=false".into())); - assert!(!cmd.contains(&out), "PDF bytes are streamed instead of passing a path"); - } - #[cfg(windows)] - assert_eq!(cmd.first().map(String::as_str), Some("xps-print")); - assert_eq!(r["sent"], false); - assert_eq!(r["pdf"], out); - let pdf = std::fs::read(&out).unwrap(); - assert!(pdf.starts_with(b"%PDF-1.4")); - assert!(String::from_utf8_lossy(&pdf).contains("/MediaBox [0 0 595.28 841.89]")); - let (w, h, cs, data) = pdf_image(&pdf); - assert_eq!((w, h), (300, 150)); - assert_eq!(cs, "/DeviceRGB"); - assert_eq!(&data[..3], &[0x33, 0x66, 0xcc]); - // 300 px at 72 ppi = 300 pt, centred. - let rect: Vec = serde_json::from_value(r["imageRect"].clone()).unwrap(); - assert!((rect[2] - 300.0).abs() < 1e-6 && (rect[0] - (595.28 - 300.0) / 2.0).abs() < 1e-6); - } - // Print to PDF: no spooling by default. - let mut s = session("gray", 8); - let out = format!("{dir}/out.pdf"); - let r = s.execute("file.print", json!({"output": out, "scaleToFit": true, "orientation": "landscape"})).unwrap(); - assert_eq!(r["sent"], false); - assert!(r["command"].is_null()); - let (_, _, cs, _) = pdf_image(&std::fs::read(&out).unwrap()); - assert_eq!(cs, "/DeviceGray"); - let scale = r["scale"].as_f64().unwrap(); - assert!(scale > 200.0, "fit to a landscape letter page: {scale}"); - assert!(s.execute("file.print", json!({"paper": "napkin", "dryRun": true})).is_err()); - assert!(s.execute("file.print", json!({"colorHandling": "photocraftManages", "dryRun": true})).is_err(), "needs a printer profile"); -} - -#[test] -fn photocraft_manages_colors_converts_to_the_printer_profile() { - let mut s = session("rgb", 8); - let out = format!("{}/managed.pdf", tmp("managed")); - let r = s - .execute( - "file.print", - json!({"output": out, "dryRun": true, "colorHandling": "photocraftManages", "printerProfile": "coated-cmyk", "intent": "perceptual", "bpc": false}), - ) - .unwrap(); - assert_eq!(r["color"]["intent"], "perceptual"); - let pdf = std::fs::read(&out).unwrap(); - let (_, _, cs, data) = pdf_image(&pdf); - assert_eq!(cs, "[/ICCBased 7 0 R]"); - assert!(String::from_utf8_lossy(&pdf).contains("/N 4 /Alternate /DeviceCMYK")); - assert_eq!(data.len(), 300 * 150 * 4); - // Blue: lots of cyan, little yellow. - assert!(data[0] > 150 && data[2] < 60, "{:?}", &data[..4]); - // The document itself is untouched. - assert_eq!(s.active().unwrap().doc.mode, ColorMode::Rgb); -} - -/// The inflated page content stream (object 4). -fn pdf_content(pdf: &[u8]) -> String { - let i = find(pdf, b"4 0 obj\n", 0); - let len_at = find(pdf, b"/Length ", i) + 8; - let len: usize = String::from_utf8_lossy(&pdf[len_at..len_at + 12]).split_whitespace().next().unwrap().parse().unwrap(); - let start = find(pdf, b"stream\n", i) + 7; - let mut out = String::new(); - std::io::Read::read_to_string(&mut flate2::read::ZlibDecoder::new(&pdf[start..start + len]), &mut out).unwrap(); - out -} - -#[test] -fn emulsion_down_mirrors_the_whole_page() { - let mut s = session("rgb", 8); - let dir = tmp("emulsion"); - let plain_path = format!("{dir}/plain.pdf"); - let mirrored_path = format!("{dir}/mirrored.pdf"); - let again_path = format!("{dir}/again.pdf"); - let plain = s - .execute("file.print", json!({"output": plain_path, "dryRun": true, "paper": "a4", "center": false, "left": 1.0, "top": 1.0, "labels": true})) - .unwrap(); - let mirrored = s - .execute( - "file.print", - json!({"output": mirrored_path, "dryRun": true, "paper": "a4", "center": false, "left": 1.0, "top": 1.0, "labels": true, "emulsionDown": true}), - ) - .unwrap(); - assert_eq!(plain["emulsionDown"], false); - assert_eq!(mirrored["emulsionDown"], true); - // Same layout; only the page is flipped. - assert_eq!(plain["imageRect"], mirrored["imageRect"]); - let (p, m) = (std::fs::read(plain["pdf"].as_str().unwrap()).unwrap(), std::fs::read(mirrored["pdf"].as_str().unwrap()).unwrap()); - let (pc, mc) = (pdf_content(&p), pdf_content(&m)); - assert!(!pc.contains("-1 0 0 1")); - // The flip wraps the image and the label, after the white paper, and is closed again. - let flip = mc.find("q -1 0 0 1 595.28 0 cm").expect("mirror transform"); - assert!(flip > mc.find("re f Q").unwrap() && flip < mc.find("/Im0 Do").unwrap() && flip < mc.find("Tj").unwrap()); - assert_eq!(mc.matches("q ").count() + mc.matches("q\n").count(), mc.matches("Q\n").count(), "balanced q/Q: {mc}"); - // The pixels themselves are not touched (the transform mirrors them). - assert_eq!(pdf_image(&p).3, pdf_image(&m).3); - // Print One Copy keeps it. - let again = s.execute("file.printOneCopy", json!({"output": again_path, "dryRun": true})).unwrap(); - assert_eq!(again["emulsionDown"], true); - for r in [plain, mirrored, again] { - let _ = std::fs::remove_file(r["pdf"].as_str().unwrap()); - } - let _ = std::fs::remove_dir_all(&dir); -} - -fn page_of(channels: usize, data: Vec, emulsion_down: bool) -> PrintPage { - PrintPage { - paper: (600.0, 800.0), - image: PrintImage { width: 2, height: 1, channels, data, icc: None }, - rect: (100.0, 200.0, 144.0, 72.0), - marks: Marks::default(), - description: None, - label: None, - emulsion_down, - } -} - -#[test] -fn windows_print_image_is_rgb_mirrored_for_emulsion_down() { - assert_eq!(page_rgb(&page_of(3, vec![1, 2, 3, 4, 5, 6], false)).unwrap(), [1, 2, 3, 4, 5, 6]); - assert_eq!(page_rgb(&page_of(3, vec![1, 2, 3, 4, 5, 6], true)).unwrap(), [4, 5, 6, 1, 2, 3]); - assert_eq!(page_rgb(&page_of(1, vec![7, 9], true)).unwrap(), [9, 9, 9, 7, 7, 7]); - assert!(page_rgb(&page_of(4, vec![0; 8], false)).is_err(), "CMYK can't go to a GDI printer"); - assert!(page_rgb(&page_of(3, vec![0; 5], false)).is_err(), "short data"); - // GDI's origin is the sheet's top-left; Emulsion Down moves the box to the mirrored side. - assert_eq!(page_rect_top_left(&page_of(3, vec![0; 6], false)), (100.0, 528.0, 144.0, 72.0)); - assert_eq!(page_rect_top_left(&page_of(3, vec![0; 6], true)), (356.0, 528.0, 144.0, 72.0)); -} - -#[test] -fn native_rgb_route_preserves_pdf_only_color_and_page_content() { - let mut page = page_of(3, vec![0; 6], false); - assert!(native_rgb_compatible(&page, "printerManages")); - page.label = Some("Print Me".into()); - assert!(!native_rgb_compatible(&page, "printerManages")); - page.label = None; - page.description = Some("Proof description".into()); - assert!(!native_rgb_compatible(&page, "printerManages")); - page.description = Some(String::new()); - assert!(native_rgb_compatible(&page, "printerManages")); - page.image.icc = Some(vec![1, 2, 3]); - assert!(!native_rgb_compatible(&page, "printerManages"), "preserve embedded source ICC in the PDF route"); - assert!(native_rgb_compatible(&page, "photocraftManages")); - assert!(native_rgb_compatible(&page, "photoshopManages")); - assert!(!native_rgb_compatible(&page_of(4, vec![0; 8], false), "photocraftManages")); - assert!(native_rgb_compatible(&page_of(1, vec![0; 2], false), "noColorManagement")); -} - -#[test] -fn printer_commands_answer_without_a_document_and_reject_bad_settings() { - let mut s = Session::new(); - let r = s.execute("file.print.printers", json!({})).unwrap(); - assert_eq!(r["supported"], cfg!(windows)); - assert!(r["printers"].is_array()); - assert!(s.execute("file.print.page", json!({"printer": "Anything", "printerSettings": "zz"})).is_err()); - let mut s = session("rgb", 8); - assert!(s.execute("file.print", json!({"printer": "Anything", "printerSettings": "0102", "send": true})).is_err()); -} - -#[test] -fn printer_profile_can_be_an_installed_icc_file() { - let dir = tmp("icc"); - let icc = format!("{dir}/My Paper.icc"); - let output = format!("{dir}/managed.pdf"); - std::fs::write(&icc, photocraft_cms::Builtin::CoatedCmyk.profile().to_bytes().as_slice()).unwrap(); - let mut s = session("rgb", 16); - let r = s - .execute("file.print", json!({"output": output, "dryRun": true, "colorHandling": "photocraftManages", "printerProfile": icc, "emulsionDown": true})) - .unwrap(); - assert_eq!(r["color"]["printerProfile"], json!(icc)); - let (_, _, cs, data) = pdf_image(&std::fs::read(r["pdf"].as_str().unwrap()).unwrap()); - assert_eq!(cs, "[/ICCBased 7 0 R]"); - assert_eq!(data.len(), 300 * 150 * 4); - assert!(s.execute("file.print", json!({"dryRun": true, "colorHandling": "photocraftManages", "printerProfile": format!("{dir}/missing.icc")})).is_err()); - let _ = std::fs::remove_file(r["pdf"].as_str().unwrap()); - let _ = std::fs::remove_dir_all(&dir); -} - -#[test] -fn print_one_copy_repeats_the_last_settings() { - let mut s = session("rgb", 8); - let first = s.execute("file.print", json!({"dryRun": true, "copies": 3, "paper": "a5", "scale": 50})).unwrap(); - assert!(first["pdf"].is_null()); - let r = s.execute("file.printOneCopy", json!({"dryRun": true})).unwrap(); - assert_eq!(r["copies"], 1); - assert_eq!(r["paper"], json!([419.53, 595.28])); - assert_eq!(r["scale"], 50.0); - let cmd: Vec = serde_json::from_value(r["command"].clone()).unwrap(); - #[cfg(unix)] - assert_eq!(&cmd[..3], &["lp", "-n", "1"]); - #[cfg(windows)] - assert_eq!(cmd.first().map(String::as_str), Some("xps-print")); - assert!(r["pdf"].is_null()); -} - -#[test] -fn package_copies_links_and_relinks() { - let dir = tmp("package"); - let link = format!("{dir}/art.png"); - let mut src = Session::new(); - src.execute("file.new", json!({"width": 20, "height": 20, "background": "#ff0000"})).unwrap(); - crate::file_cmds::save_doc(&src.active().unwrap().doc, &link, None).unwrap(); - let mut s = session("rgb", 8); - s.execute("file.placeLinked", json!({"path": link})).unwrap(); - let r = s.execute("file.package", json!({"dir": format!("{dir}/pkg")})).unwrap(); - let copied = format!("{dir}/pkg/Print Me/Links/art.png"); - assert_eq!(r["links"], json!([copied])); - assert!(std::path::Path::new(&copied).is_file()); - let docp = r["document"].as_str().unwrap(); - assert!(docp.ends_with("Print Me/Print Me.pcraft")); - let back = photocraft_io::import("Print Me.pcraft", &std::fs::read(docp).unwrap()).unwrap().document; - let linked: Vec = back - .walk() - .iter() - .filter_map(|(_, _, l)| match &l.content { - LayerContent::Smart(so) => match &so.source { - SmartSource::Linked { path } => Some(path.clone()), - _ => None, - }, - _ => None, - }) - .collect(); - assert_eq!(linked.len(), 1); - assert!(linked[0].ends_with("Links/art.png"), "{linked:?}"); - // The open document still points at the original. - let open = &s.active().unwrap().doc; - assert!( - open.walk() - .iter() - .any(|(_, _, l)| matches!(&l.content, LayerContent::Smart(so) if matches!(&so.source, SmartSource::Linked { path } if *path == link))) - ); -} - -#[test] -fn paths_export_as_illustrator_postscript() { - let mut s = session("rgb", 8); - assert!(!s.is_enabled("file.export.pathsToIllustrator")); - s.edit("paths", |d, _| { - let tri = Subpath::polygon(&[(10.0, 10.0), (100.0, 10.0), (50.0, 90.0)]); - let mut curve = Subpath::polyline(&[(0.0, 0.0), (300.0, 150.0)]); - curve.knots[0] = Knot::smooth(photocraft_geom::Point::new(0.0, 0.0), photocraft_geom::Point::new(0.0, 0.0), photocraft_geom::Point::new(50.0, 0.0)); - d.paths.push(NamedPath { name: "Shapes".into(), path: Path::new(vec![tri, curve]), psd_raw: None }); - Ok(()) - }) - .unwrap(); - let r = s.execute("file.export.pathsToIllustrator", json!({})).unwrap(); - let ai = r["ai"].as_str().unwrap(); - assert!(ai.starts_with("%!PS-Adobe-2.0 EPSF-1.2")); - assert!(ai.contains("%%BoundingBox: 0 0 300 150")); - assert!(ai.contains("*u\n10.0000 140.0000 m\n100.0000 140.0000 L\n"), "{ai}"); - assert!(ai.contains("\nn\n") && ai.contains("\nN\n*U\n")); - assert!(ai.contains(" C\n"), "curve segment"); - let dir = tmp("ai"); - let r = s.execute("file.export.pathsToIllustrator", json!({"path": format!("{dir}/p.ai"), "paths": "Shapes"})).unwrap(); - assert_eq!(r["paths"], 1); - assert!(s.execute("file.export.pathsToIllustrator", json!({"paths": "Nope"})).is_err()); -} - -#[test] -fn print_dry_run_without_output_retains_no_pdf() { - let mut s = session("rgb", 8); - let result = s.execute("file.print", json!({"dryRun": true})).unwrap(); - assert!(result["pdf"].is_null()); - assert_eq!(result["sent"], false); - assert!(result["bytes"].as_u64().unwrap() > 100); - assert!(result["command"].is_array()); - assert!(s.file_menu.last_print.as_ref().unwrap().get("dryRun").is_none()); -} - -#[test] -fn print_without_sending_requires_output_unless_dry_run_and_forgets_send_override() { - let mut s = session("rgb", 8); - let before = s.file_menu.last_print.clone(); - let err = s.execute("file.print", json!({"send": false})).unwrap_err(); - assert!(err.to_string().contains("needs an output path"), "{err}"); - assert_eq!(s.file_menu.last_print, before); - let result = s.execute("file.print", json!({"send": false, "dryRun": true})).unwrap(); - assert!(result["pdf"].is_null()); - assert!(result["command"].is_null()); - assert!(s.file_menu.last_print.as_ref().unwrap().get("send").is_none()); - let out = format!("{}/export.pdf", tmp("non-sending-export")); - let result = s.execute("file.print", json!({"output": out, "send": false})).unwrap(); - assert_eq!(result["sent"], false); - assert!(std::fs::read(&out).unwrap().starts_with(b"%PDF-1.4")); - assert!(s.file_menu.last_print.as_ref().unwrap().get("send").is_none()); - s.execute("file.print", json!({"output": out, "send": true, "dryRun": true})).unwrap(); - assert!(s.file_menu.last_print.as_ref().unwrap().get("send").is_none()); -} -/// parallel tests are untouched, and the real spool boundary still consumes the PDF bytes. -#[cfg(unix)] -#[test] -fn spool_transport_streams_pdf_and_propagates_refusal() { - const CHILD: &str = "PHOTOCRAFT_TEST_PRINT_TRANSPORT"; - if let Ok(dir) = std::env::var(CHILD) { - let mut s = session("rgb", 8); - let source = s.active().unwrap().doc.id; - s.execute("file.scripts.scriptEventsManager", json!({"add": {"event": "print", "steps": [["image.imageRotation.90cw", {}]]}})).unwrap(); - let crate::jobs::Started::Job(job) = s.start("file.print", json!({"printer": "Office", "copies": 3, "paper": "a4", "scale": 50})).unwrap() else { - panic!("desktop printing must start on a worker"); - }; - assert!(!s.cancel_job(job), "queue side effects must not be reported as cancelled"); - assert_eq!(s.execute("jobs.cancel", json!({})).unwrap()["cancelled"], 0); - assert!(s.execute("jobs.cancel", json!({"job": job.0})).unwrap_err().to_string().contains("system print queue")); - s.execute("file.new", json!({"width": 40, "height": 10})).unwrap(); - let viewed = s.active().unwrap().doc.id; - let journal_before_completion = s.journal.len(); - let out = s.wait_job(job).unwrap(); - assert_eq!(s.journal.len(), journal_before_completion + 1, "print hooks must not become duplicate recorded action steps"); - assert_eq!(s.journal.last().unwrap().0, "file.print"); - assert_eq!(s.active().unwrap().doc.id, viewed, "completion keeps the user's tab"); - assert_eq!(s.active().unwrap().doc.size.width, 40, "print hook must not act on the newly viewed document"); - let printed = &s.documents().iter().find(|st| st.doc.id == source).unwrap().doc; - assert_eq!((printed.size.width, printed.size.height), (150, 300), "print hook ran on the source document"); - assert_eq!(out["sent"], true); - assert_eq!(out["spooler"], "request id is Office-42"); - assert!(out["pdf"].is_null(), "spool-only printing leaves no diagnostic PDF"); - let pdf = std::fs::read(format!("{dir}/received.pdf")).unwrap(); - assert!(pdf.starts_with(b"%PDF-1.4")); - assert!(String::from_utf8_lossy(&pdf).contains("/MediaBox [0 0 595.28 841.89]")); - let args = std::fs::read_to_string(format!("{dir}/args")).unwrap(); - assert!(args.contains("fit-to-page=false")); - assert!(args.contains("-n\n3\n")); - assert!(!args.contains(".pdf")); - let remembered = s.file_menu.last_print.clone(); - let explicit = format!("{dir}/saved.pdf"); - let err = s.execute("file.print", json!({"printer": "Refuse", "output": explicit, "send": true})).unwrap_err(); - assert!(err.to_string().contains("queue refused"), "{err}"); - assert!(std::fs::read(&explicit).unwrap().starts_with(b"%PDF-1.4"), "explicit export survives a spool refusal"); - assert_eq!(s.file_menu.last_print, remembered, "failed printing does not replace repeat settings"); - // Export-only choices must not disable submission when Print One Copy repeats settings. - let export = format!("{dir}/export-only.pdf"); - s.execute("file.print", json!({"printer": "Office", "output": export, "send": false})).unwrap(); - let remembered = s.file_menu.last_print.as_ref().unwrap(); - assert!(remembered.get("send").is_none() && remembered.get("output").is_none()); - let repeat = s.execute("file.printOneCopy", json!({})).unwrap(); - assert_eq!(repeat["sent"], true); - assert_eq!(repeat["copies"], 1); - assert!(repeat["pdf"].is_null()); - assert!(!std::fs::read_to_string(format!("{dir}/args")).unwrap().contains(".pdf")); - return; - } - use std::os::unix::fs::PermissionsExt; - let dir = tempfile::tempdir().unwrap(); - let script = dir.path().join("lp"); - std::fs::write( - &script, - b"#!/bin/sh\nif [ \"$2\" = Refuse ]; then echo 'queue refused' >&2; exit 1; fi\nprintf '%s\\n' \"$@\" > \"$PHOTOCRAFT_TEST_PRINT_TRANSPORT/args\"\n/bin/cat > \"$PHOTOCRAFT_TEST_PRINT_TRANSPORT/received.pdf\"\necho 'request id is Office-42 (0 file(s))'\n", - ).unwrap(); - std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap(); - let result = std::process::Command::new(std::env::current_exe().unwrap()) - .args(["--exact", "print_cmds::tests::spool_transport_streams_pdf_and_propagates_refusal", "--nocapture"]) - .env(CHILD, dir.path()) - .env("PATH", dir.path()) - // The child spools into the fake `lp` above, so a shell that blocks real printing - // (`PHOTOCRAFT_NO_PRINT`, see `disabled_printing_never_spools`) must not stop it. - .env_remove("PHOTOCRAFT_NO_PRINT") - .output() - .unwrap(); - assert!(result.status.success(), "{}\n{}", String::from_utf8_lossy(&result.stdout), String::from_utf8_lossy(&result.stderr)); - assert!(String::from_utf8_lossy(&result.stdout).contains("1 passed"), "child must select the transport test"); -} - -#[test] -fn disabled_printing_never_spools() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); - super::disable_printing(); - assert!(super::printing_disabled()); - let e = s.execute("file.print", json!({})).unwrap_err().to_string(); - assert!(e.contains("printing is disabled"), "{e}"); - assert!(s.execute("file.printOneCopy", json!({})).is_err()); - // A dry run still renders and reports the command it would have run. - let r = s.execute("file.print", json!({"dryRun": true})).unwrap(); - assert_eq!(r["sent"], false); - assert!(r["command"].is_array(), "{r}"); -} - -#[test] -fn printers_lists_the_spooler_destinations_without_a_document() { - let mut s = Session::new(); - let r = s.execute("file.printers", json!({})).unwrap(); - let list = r["printers"].as_array().unwrap(); - assert!(list.iter().all(|p| p["name"].is_string() && p["default"].is_boolean())); - match r["default"].as_str() { - Some(d) => assert!(list.iter().any(|p| p["name"] == d && p["default"] == true)), - None => assert!(list.iter().all(|p| p["default"] == false)), - } -} diff --git a/crates/engine/src/proof_sim.rs b/crates/engine/src/proof_sim.rs index f225a5a7a9..6c7afc15d9 100644 --- a/crates/engine/src/proof_sim.rs +++ b/crates/engine/src/proof_sim.rs @@ -18,6 +18,7 @@ use photocraft_cms::{Builtin, Intent, Lut3d, Transform}; use photocraft_color::SampleType; +use photocraft_color::convert::{linear_to_srgb, srgb_to_linear}; use photocraft_doc::Document; use serde::{Deserialize, Serialize}; use serde_json::{Value, json}; @@ -91,14 +92,16 @@ impl Default for HdrPreview { } impl HdrPreview { + /// Whether the preview passes values through unchanged. pub fn is_identity(&self) -> bool { self.highlight_compression || (self.exposure == 0.0 && self.gamma == 1.0) } + /// One encoded channel value through the 32-bit preview's exposure and gamma. fn apply(&self, v: f32) -> f32 { if self.is_identity() { return v; } - let lin = srgb_decode(v) * 2f32.powf(self.exposure); + let lin = srgb_to_linear(v) * 2f32.powf(self.exposure); srgb_encode(lin.max(0.0).powf(1.0 / self.gamma).min(1.0)) } } @@ -115,13 +118,9 @@ pub fn signature(c: &ColorState, doc: &Document) -> String { format!("{:?} {:?}", pv.setup.kind, h) } -fn srgb_decode(v: f32) -> f32 { - if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } -} - +/// sRGB-encodes linear light after clamping it to 0..=1. fn srgb_encode(v: f32) -> f32 { - let v = v.clamp(0.0, 1.0); - if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } + linear_to_srgb(v.clamp(0.0, 1.0)) } /// Viénot et al. (1999): linear RGB → LMS. @@ -201,7 +200,7 @@ pub fn display_lut_with(c: &ColorState, doc: &Document, size: usize, include_hdr let protan = kind == ProofKind::Protanopia; Box::new(move |v| { let s = eval3(&to_srgb, &v); - let sim = dichromat(s.map(srgb_decode), protan).map(srgb_encode); + let sim = dichromat(s.map(srgb_to_linear), protan).map(srgb_encode); eval3(&to_mon, &sim) }) } @@ -262,6 +261,7 @@ fn run_kind(s: &mut Session, id: &str) -> Result { set_kind(s, kind) } +/// View › 32-bit Preview Options for the active document. fn preview_32(s: &mut Session, p: &Value) -> Result { const CMD: &str = "view.thirtyTwoBitPreviewOptions"; let id = s.active().ok_or(EngineError::NoDocument)?.doc.id; @@ -298,6 +298,7 @@ macro_rules! kind_spec { }; } +/// The View › Proof Setup commands. pub fn specs() -> Vec { vec![ kind_spec!("view.proofSetup.workingCyanPlate", "Working Cyan Plate"), @@ -325,6 +326,21 @@ pub fn specs() -> Vec { mod tests { use super::*; + /// The shared sRGB functions match the removed copies bit for bit. + #[test] + fn srgb_transfer_output_is_unchanged() { + let decode = |v: f32| if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) }; + let encode = |v: f32| { + let v = v.clamp(0.0, 1.0); + if v <= 0.003_130_8 { v * 12.92 } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 } + }; + for i in -2000..=6000 { + let v = i as f32 / 4000.0; + assert_eq!(srgb_to_linear(v).to_bits(), decode(v).to_bits(), "decode {v}"); + assert_eq!(srgb_encode(v).to_bits(), encode(v).to_bits(), "encode {v}"); + } + } + fn session(mode: &str, depth: u32) -> Session { let mut s = Session::new(); s.execute("file.new", json!({"width": 8, "height": 8, "mode": mode, "depth": depth})).unwrap(); diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs deleted file mode 100644 index 9ea7fd5975..0000000000 --- a/crates/engine/src/retouch_cmds.rs +++ /dev/null @@ -1,1375 +0,0 @@ -//! Retouching tools as commands: Clone Stamp, Pattern Stamp, Healing Brush, Spot Healing Brush, -//! Patch, Content-Aware Move, Remove, Dodge, Burn, Sponge, Blur, Sharpen, Smudge and History Brush. -//! -//! Every command takes a Photoshop-style brush (`points`, `size`, `hardness`, `opacity`, `flow`, -//! `spacing`, `layer`), respects the active selection as a mask and the layer's transparency lock, and -//! works at any depth (8/16/32f) and colour model. Like the Brush, each paints the targeted surface: -//! the layer's pixels, its mask (`"target":"mask"`), an alpha channel or the Quick Mask (`"target"`, -//! filled in from the Channels panel when absent). The pixel algorithms live in `photocraft-algo` -//! (`poisson`, `inpaint`, `retouch`) and the dab machinery in `photocraft-paint::retouch`; this module -//! only parses parameters and wires them together. - -use photocraft_algo::inpaint::{self, CompleteParams}; -use photocraft_algo::poisson; -use photocraft_algo::retouch::{ToneRange, dodge_burn, local_blur, local_sharpen, sponge}; -use photocraft_color::{BlendMode, PixelFormat, SampleType}; -use photocraft_doc::{Document, LayerContent, LayerId}; -use photocraft_geom::Rect; -use photocraft_paint::retouch::{Footprint, Region, Smudge, alpha_index, apply_coverage, apply_dab_stroke, stroke_coverage}; -use photocraft_paint::{BrushSettings, Stroke, StrokePoint}; -use photocraft_raster::{Surface, from_rgba_into, to_rgba}; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, blend_from_str}; -use crate::{EngineError, Result, Session}; - -mod content_aware_move; -mod patch; -mod pattern_stamp; -mod periodic; -mod remove; -pub use content_aware_move::{ContentAwarePreview, preview as content_aware_move_preview}; -pub use patch::preview as patch_preview; - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -/// A pixel layer, a layer with a mask, or a targeted alpha channel / Quick Mask (as the Brush). -fn has_pixel_layer(s: &Session) -> std::result::Result<(), String> { - if crate::channel_cmds::edits_channel(s) { - return Ok(()); - } - let d = s.active().ok_or("no document open")?; - let l = d.active_layer.and_then(|id| d.doc.layer(id)).ok_or("no active layer")?; - if matches!(l.content, LayerContent::Raster(_)) || l.mask.is_some() { - Ok(()) - } else { - Err(format!("active layer is {} {} layer, not a pixel layer", l.content.article(), l.content.kind_name())) - } -} - -/// The stroke paints layer pixels (not a mask or channel), so other layers can be sampled. -fn targets_pixels(p: &Value) -> bool { - crate::channel_cmds::target_of(p) == crate::channel_cmds::Target::Pixels -} - -fn num(p: &Value, k: &str, d: f32) -> f32 { - p.get(k).and_then(Value::as_f64).map_or(d, |v| v as f32) -} -fn flag(p: &Value, k: &str, d: bool) -> bool { - p.get(k).and_then(Value::as_bool).unwrap_or(d) -} -fn string<'a>(p: &'a Value, k: &str, d: &'a str) -> &'a str { - p.get(k).and_then(Value::as_str).unwrap_or(d) -} -fn point(p: &Value, k: &str) -> Option<(f64, f64)> { - let a = p.get(k)?.as_array()?; - Some((a.first()?.as_f64()?, a.get(1)?.as_f64()?)) -} - -/// The brush a retouching stroke paints with: the tool's brush (`base`, its tip, hardness and -/// shape dynamics) with the shared params of `p` applied. UI previews of a stroke (the Spot Healing -/// Brush's trail) render their coverage with it, so they match what the command changes. -pub fn stroke_brush(base: &BrushSettings, p: &Value) -> BrushSettings { - let pct = |k: &str, d: f32, lo: f32, hi: f32| num(p, k, d).clamp(lo, hi) / 100.0; - BrushSettings { - size: num(p, "size", base.size).max(1.0), - hardness: pct("hardness", base.hardness * 100.0, 0.0, 100.0), - opacity: pct("opacity", 100.0, 1.0, 100.0), - flow: pct("flow", 100.0, 1.0, 100.0), - // Brush Settings › Brush Tip Shape › Spacing, as the Brush uses it (#2140). - spacing: pct("spacing", base.spacing * 100.0, 1.0, 1000.0), - erase: false, - ..base.clone() - } -} - -/// Parse the shared brush params into a stroke on a layer (`None` when an alpha channel or the -/// Quick Mask is targeted). -fn parse_brush(s: &Session, p: &Value, cmd: &str) -> Result<(Stroke, Option)> { - let pts: Vec = p - .get("points") - .and_then(Value::as_array) - .ok_or_else(|| bad(cmd, "missing `points`"))? - .iter() - .filter_map(|v| { - let a = v.as_array()?; - Some(StrokePoint::new(a.first()?.as_f64()?, a.get(1)?.as_f64()?, a.get(2).and_then(Value::as_f64).unwrap_or(1.0) as f32)) - }) - .collect(); - if pts.is_empty() { - return Err(bad(cmd, "`points` is empty")); - } - crate::brush_cmds::check_coords(&pts, cmd)?; - let brush = stroke_brush(&s.tools.brush, p); - crate::brush_cmds::validate_brush(&brush, cmd)?; - if crate::channel_cmds::is_channel_target(p) { - return Ok((Stroke { brush, points: pts }, None)); - } - let id = match p.get("layer").and_then(Value::as_u64) { - Some(id) => LayerId(id), - None => s.active().and_then(|d| d.active_layer).ok_or(EngineError::Other("no active layer".into()))?, - }; - Ok((Stroke { brush, points: pts }, Some(id))) -} - -/// Run `f` on the targeted surface (layer pixels, layer mask, alpha channel or Quick Mask: the -/// Brush's paint target) inside one undoable step. `f` gets the pre-stroke document (a cheap -/// copy-on-write snapshot), the surface, the selection and the transparency lock. -fn run_stroke( - s: &mut Session, - label: &str, - id: Option, - p: &Value, - f: impl FnOnce(&Document, &mut Surface, Option<&Surface>, bool) -> Result, -) -> Result { - let dmg = s.edit(label, |doc, _| { - let pre = doc.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(doc, id, p)?; - // No prune: retouching only rewrites pixels inside the stroke, and scanning every tile of a - // large layer would dominate the cost of a dab. - f(&pre, surf, pre.selection.as_ref(), lock) - })?; - if let Some(st) = s.active_mut() { - st.last_damage = Some(dmg); - } - Ok(dmg) -} - -fn damage_json(r: Rect) -> Value { - json!([r.x0, r.y0, r.width(), r.height()]) -} - -// --------------------------------------------------------------------------------------------- -// Sampling sources -// --------------------------------------------------------------------------------------------- - -/// Which pixels Clone Stamp / Healing Brush sample. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum SampleLayers { - Current, - CurrentAndBelow, - All, -} - -fn sample_layers(p: &Value, cmd: &str) -> Result { - match string(p, "sampleLayer", "current") { - "current" => Ok(SampleLayers::Current), - "currentAndBelow" => Ok(SampleLayers::CurrentAndBelow), - "all" => Ok(SampleLayers::All), - o => Err(bad(cmd, format!("unknown sampleLayer `{o}` (current|currentAndBelow|all)"))), - } -} - -/// Composite pixels of `rect` in `fmt` (native channels): the whole document, or the target layer -/// and everything below it. -pub(crate) fn composite_region(pre: &Document, id: Option, which: SampleLayers, rect: Rect, fmt: PixelFormat) -> Region { - let mut doc = pre.clone(); - if which == SampleLayers::CurrentAndBelow - && let Some(id) = id - { - // Hide everything composited after (above) the target in the bottom-to-top walk. - let walk: Vec<(Vec, LayerId)> = pre.walk().into_iter().map(|(p, _, l)| (p, l.id)).collect(); - if let Some(pos) = walk.iter().position(|(_, l)| *l == id) { - for (path, _) in &walk[pos + 1..] { - if let Some(l) = doc.layer_at_mut(path) { - l.visible = false; - } - } - } - } - let buf = photocraft_compose::render(&doc, rect); - let n = fmt.channels(); - let mut out = Region::new(rect, n); - let mut enc = [0.0f32; 8]; - for (i, px) in buf.px.iter().enumerate() { - from_rgba_into(&fmt, *px, &mut enc); - out.data[i * n..(i + 1) * n].copy_from_slice(&enc[..n]); - } - out -} - -/// Pixels to sample at `rect` (document coordinates, already offset to the source position). -fn sample(pre: &Document, id: Option, surf: &Surface, which: SampleLayers, rect: Rect) -> Region { - match which { - SampleLayers::Current => Region::read(surf, rect), - _ => composite_region(pre, id, which, rect, surf.format()), - } -} - -/// Where a clone stroke samples: `offset` / `source` params, else the active Clone Source slot, -/// with the slot's (or the call's) scale, rotation and flips (see `presets::clone_source`). -fn clone_mapping(s: &mut Session, p: &Value, stroke: &Stroke, cmd: &str) -> Result { - let f = stroke.points[0]; - crate::presets::clone_source::mapping(s, p, (f.x, f.y), cmd) -} - -/// Source pixels for destination `rect`: a translated read, or a bilinear resample when the -/// clone source is scaled, rotated or flipped. -#[allow(clippy::too_many_arguments)] -fn clone_sample( - pre: &Document, - id: Option, - surf: &Surface, - which: SampleLayers, - rect: Rect, - map: &crate::presets::clone_source::Mapping, - wrap: bool, - cmd: &str, -) -> Result { - let read = |rect| { - if wrap { periodic::sample(pre, id, surf, which, rect, cmd) } else { Ok(sample(pre, id, surf, which, rect)) } - }; - if map.is_translation() { - let off = map.offset(); - let src = rect.translate(off.0, off.1); - // `translate` saturates at the i32 edges, so a far source (issue #1111) yields a window - // smaller than `rect`; relabelling that as `rect` would index past the sampled pixels. - if src.width() != rect.width() || src.height() != rect.height() { - return Err(bad(cmd, format!("source offset [{}, {}] is too far: the sample window leaves the coordinate range", off.0, off.1))); - } - let mut r = read(src)?; - r.rect = rect; - return Ok(r); - } - let src = read(map.source_rect(rect))?; - Ok(crate::presets::clone_source::resample(&src, rect, map, alpha_index(&surf.format()))) -} - -/// Read-only clone overlay. Shares the stroke sampler without anchoring a source or editing history. -/// Both destination and transformed source allocations are bounded for interactive use. -pub fn clone_preview(s: &Session, rect: Rect, map: &crate::presets::clone_source::Mapping, sample_layer: &str) -> Result { - clone_preview_periodic(s, rect, map, sample_layer, false) -} - -/// Clone-source overlay with optional source repetition for View > Pattern Preview. -pub fn clone_preview_periodic( - s: &Session, - rect: Rect, - map: &crate::presets::clone_source::Mapping, - sample_layer: &str, - wrap: bool, -) -> Result { - if ![map.source.0, map.source.1, map.anchor.0, map.anchor.1].into_iter().chain(map.m).all(|v| v.is_finite() && v.abs() < 1_000_000.0) { - return Err(bad("clone.preview", "invalid preview mapping")); - } - if ![rect.x0, rect.y0, rect.x1, rect.y1].into_iter().all(|v| v.abs_diff(0) < 1_000_000) - || ![(rect.x0, rect.y0), (rect.x1, rect.y1), (rect.x0, rect.y1), (rect.x1, rect.y0)].into_iter().all(|(x, y)| { - let (x, y) = map.map(f64::from(x), f64::from(y)); - x.is_finite() && y.is_finite() && x.abs() < 1_000_000.0 && y.abs() < 1_000_000.0 - }) - { - return Err(bad("clone.preview", "preview coordinates exceed their budget")); - } - let bounded = |r: Rect| r.width() > 0 && r.height() > 0 && r.width() <= 1024 && r.height() <= 1024; - if !bounded(rect) || !bounded(map.source_rect(rect)) { - return Err(bad("clone.preview", "preview region exceeds its budget")); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let id = d.active_layer.ok_or_else(|| bad("clone.preview", "no active layer"))?; - let surf = d.doc.layer(id).and_then(|l| l.surface()).ok_or_else(|| bad("clone.preview", "no pixel surface"))?; - let which = sample_layers(&json!({"sampleLayer": sample_layer}), "clone.preview")?; - let region = clone_sample(&d.doc, Some(id), surf, which, rect, map, wrap, "clone.preview")?; - let px = region.data.chunks_exact(surf.format().channels()).map(|p| to_rgba(&surf.format(), p)).collect(); - Ok(photocraft_compose::Buffer { rect, px }) -} - -/// Result JSON shared by Clone Stamp and Healing Brush: the offset used and where the next stroke -/// should sample from if the UI keeps Photoshop's "Aligned" semantics. -fn clone_result(dmg: Rect, off: (i32, i32), aligned: bool, p: &Value, stroke: &Stroke) -> Value { - let f = stroke.points[0]; - let source = point(p, "source").unwrap_or((f.x + off.0 as f64, f.y + off.1 as f64)); - // Aligned: the offset persists (next stroke samples at its own first point + offset). - // Non-aligned: every stroke restarts at the original source point. - json!({ "damage": damage_json(dmg), "offset": [off.0, off.1], "aligned": aligned, "nextSource": if aligned { Value::Null } else { json!([source.0, source.1]) } }) -} - -fn blend_param(p: &Value, cmd: &str) -> Result { - match p.get("mode").and_then(Value::as_str) { - None => Ok(BlendMode::Normal), - Some(m) => blend_from_str(m).ok_or_else(|| bad(cmd, format!("unknown blend mode `{m}`"))), - } -} - -/// Clamp results into the storable range: integer depths to 0..1, float depths only below at 0; -/// alpha always 0..1. -fn clamp_samples(fmt: &PixelFormat, data: &mut [f32]) { - let n = fmt.channels(); - let a = alpha_index(fmt); - let float = fmt.sample == SampleType::F32; - for px in data.chunks_exact_mut(n) { - for (c, v) in px.iter_mut().enumerate() { - *v = if Some(c) == a || !float { v.clamp(0.0, 1.0) } else { v.max(0.0) }; - } - } -} - -// --------------------------------------------------------------------------------------------- -// Clone Stamp / Healing Brush / History Brush -// --------------------------------------------------------------------------------------------- - -fn clone_stamp(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.cloneStamp"; - let (stroke, id) = parse_brush(s, p, CMD)?; - let which = sample_layers(p, CMD)?; - let mode = blend_param(p, CMD)?; - let aligned = flag(p, "aligned", true); - // The live stroke the canvas already drew for exactly this commit: take its pixels instead of - // rendering the stroke again (the hitch on release). - let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); - let from_live = live.is_some(); - let map = clone_mapping(s, p, &stroke, CMD)?; - let off = map.offset(); - let dmg = - run_stroke(s, "Clone Stamp", id, p, |pre, surf, sel, lock| { - if let Some(l) = live { - *surf = l.surface; - return Ok(l.bounds); - } - if flag(p, "wrap", false) { - let mut renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); - renderer.push(&stroke.points); - renderer.finish(); - let before = surf.clone(); - let bounds = renderer.bounds(); - return periodic::Composite { target: surf, before: &before, tile: pre.bounds(), opacity: stroke.brush.opacity, selection: sel, lock, mode } - .draw(bounds, &stroke.brush, &stroke.points, |r| clone_sample(pre, id, &before, which, r, &map, true, CMD)); - } - let (bounds, cov) = stroke_coverage(&stroke); - // Samples come from the pre-stroke state, so pixels painted earlier in the stroke are never re-cloned. - let paint = clone_sample(pre, id, surf, which, bounds, &map, flag(p, "wrap", false), CMD)?; - Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) - })?; - let mut r = clone_result(dmg, off, aligned, p, &stroke); - if from_live { - r["fromLive"] = json!(true); - } - Ok(r) -} - -/// What a live retouching stroke paints through its coverage. -enum LivePaint { - /// The clone source (Clone Stamp; the Healing Brush shows its texture while drawing and - /// blends it on release). - Clone { which: SampleLayers, map: crate::presets::clone_source::Mapping, cmd: &'static str }, - /// The History Brush's history state. - History(Surface), -} - -/// A retouching stroke shown while it is drawn: the dabs so far composited onto a copy of the -/// active document as the command with the same params commits them (the same coverage, -/// sampling and blend): Clone Stamp, History Brush, and the Healing Brush's texture. -pub struct LiveRetouch { - /// The active document with the stroke so far. - pub doc: std::sync::Arc, - cmd: String, - renderer: photocraft_paint::StrokeRenderer, - pre: std::sync::Arc, - pre_surf: Surface, - id: Option, - params: Value, - paint: LivePaint, - mode: BlendMode, - opacity: f32, - sel: Option, - lock: bool, - /// Where the doc shows what finishing the stroke now would add (a lone first dab, the - /// smoothing catch-up), redrawn on every step as the Brush's live stroke does. - tail: Rect, - /// The document and revision the stroke started on. - base: (photocraft_doc::DocId, u64), - brush: Box, - /// Every point rendered so far, in order. - points: Vec, -} - -impl LiveRetouch { - /// Start a live stroke of `cmd` (`paint.cloneStamp`, `paint.healingBrush` or - /// `paint.historyBrush`) from its params; their `points` are rendered. - pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { - has_pixel_layer(s).map_err(EngineError::Other)?; - let (stroke, id) = parse_brush(s, p, cmd)?; - let mode = blend_param(p, cmd)?; - let paint = match cmd { - "paint.cloneStamp" | "paint.healingBrush" => { - let f = *stroke.points.first().ok_or_else(|| bad(cmd, "`points` is empty"))?; - LivePaint::Clone { - which: sample_layers(p, cmd)?, - map: crate::presets::clone_source::resolve(s, p, (f.x, f.y), cmd)?.0, - cmd: if cmd == "paint.healingBrush" { "paint.healingBrush" } else { "paint.cloneStamp" }, - } - } - "paint.historyBrush" => LivePaint::History(history_source(s, id, p, cmd)?), - other => return Err(bad(other, "live retouching strokes are the Clone Stamp, Healing Brush and History Brush")), - }; - let st = s.active().ok_or(EngineError::NoDocument)?; - let (pre, base) = (st.doc.clone(), (st.doc.id, st.revision)); - let mut doc = (*pre).clone(); - let sel = doc.selection.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; - let pre_surf = surf.clone(); - // As `stroke_coverage`, which the commit uses. - let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0); - let opacity = stroke.brush.opacity; - let brush = Box::new(stroke.brush.clone()); - let paint = match paint { - LivePaint::History(src) if src.format() != pre_surf.format() => LivePaint::History(src.convert(pre_surf.format())), - other => other, - }; - let mut live = Self { - doc: std::sync::Arc::new(doc), - cmd: cmd.into(), - renderer, - pre, - pre_surf, - id, - params: p.clone(), - paint, - mode, - opacity, - sel, - lock, - tail: Rect::EMPTY, - base, - brush, - points: Vec::new(), - }; - live.push(&stroke.points)?; - Ok(live) - } - - /// Everything the stroke has touched so far. - pub fn bounds(&self) -> Rect { - let bounds = self.renderer.bounds().union(&self.tail); - if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { - periodic::project(bounds, self.pre.bounds()).into_iter().fold(Rect::EMPTY, |a, b| a.union(&b)) - } else { - bounds - } - } - - /// Every point rendered so far, in order. - pub fn points(&self) -> &[StrokePoint] { - &self.points - } - - /// The finished stroke as its commit would take it over ([`Session::offer_live_retouch`]). - /// Only the Clone Stamp and History Brush draw what they commit (the Healing Brush blends on - /// release), and only while the brush, layer and clone source are still the ones the stroke - /// started with: the document revision doesn't cover those. - pub(crate) fn into_commit(mut self, s: &Session) -> Result { - let (stroke, id) = parse_brush(s, &self.params, &self.cmd)?; - let source_unchanged = match &self.paint { - LivePaint::Clone { map, .. } if self.cmd == "paint.cloneStamp" => { - let f = stroke.points.first().ok_or_else(|| bad(&self.cmd, "`points` is empty"))?; - crate::presets::clone_source::resolve(s, &self.params, (f.x, f.y), &self.cmd)?.0 == *map - } - LivePaint::History(_) if self.cmd == "paint.historyBrush" => true, - _ => return Err(bad(&self.cmd, "only Clone Stamp and History Brush strokes commit their live pixels")), - }; - if !source_unchanged || *self.brush != stroke.brush || id != self.id { - return Err(bad(&self.cmd, "the brush, layer or clone source changed during the stroke")); - } - let bounds = self.bounds(); - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - let surface = surf.clone(); - Ok(crate::brush_cmds::LiveCommit { cmd: self.cmd, base: self.base, params: self.params, seed: None, points: self.points, surface, bounds }) - } - - /// Render more points; returns the rectangle that changed. Each changed area is redrawn from - /// the pre-stroke pixels with the stroke's coverage so far, so overlapping dabs build up as in - /// the commit, and pixels painted earlier in the stroke are never re-cloned. What finishing - /// the stroke now would add is drawn too (a lone first dab shows on the press), and redrawn on - /// every step. - /// Invalid coordinates reject the whole batch without changing the preview. - /// Only the position and pressure of `pts` are used: the commands take no tilt or times. - pub fn push(&mut self, pts: &[StrokePoint]) -> Result { - crate::brush_cmds::check_coords(pts, &self.cmd)?; - let pts: Vec<_> = pts.iter().map(|q| StrokePoint::new(q.x, q.y, q.pressure)).collect(); - self.points.extend_from_slice(&pts); - self.renderer.push(&pts); - if self.cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false) { - return self.push_periodic(); - } - let old = std::mem::replace(&mut self.tail, Rect::EMPTY); - let mut dmg = Rect::EMPTY; - if !old.is_empty() { - // Back to the stroke without the previous tail. - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - surf.write_region(old, &self.pre_surf.read_region(old)); - self.renderer.mark_dirty(old); - dmg = old; - } - let r = self.renderer.take_dirty_rect(); - let cov = self.renderer.coverage_in(r); - dmg = dmg.union(&self.composite(r, &cov)?); - if let Some(mut tail) = self.renderer.tail_preview() { - let r = tail.take_dirty_rect(); - let cov = tail.coverage_in(r); - self.tail = self.composite(r, &cov)?; - dmg = dmg.union(&self.tail); - } - Ok(dmg) - } - - fn push_periodic(&mut self) -> Result { - let mut dirty = self.renderer.take_dirty_rect().union(&self.tail); - let mut tail = self.renderer.tail_preview(); - self.tail = tail.as_mut().map_or(Rect::EMPTY, |t| t.take_dirty_rect()); - dirty = dirty.union(&self.tail); - let LivePaint::Clone { which, map, cmd } = &self.paint else { - return Err(bad(&self.cmd, "periodic painting requires a clone source")); - }; - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - periodic::Composite { - target: surf, - before: &self.pre_surf, - tile: self.pre.bounds(), - opacity: self.opacity, - selection: self.sel.as_ref(), - lock: self.lock, - mode: self.mode, - } - .draw(dirty, &self.brush, &self.points, |r| clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, true, cmd)) - } - - /// Redraw `r` from the pre-stroke pixels with the clone source through coverage `cov`. - fn composite(&mut self, r: Rect, cov: &[f32]) -> Result { - if r.is_empty() { - return Ok(r); - } - let paint = match &self.paint { - LivePaint::Clone { which, map, cmd } => { - clone_sample(&self.pre, self.id, &self.pre_surf, *which, r, map, *cmd == "paint.cloneStamp" && flag(&self.params, "wrap", false), cmd)? - } - LivePaint::History(src) => Region::read(src, r), - }; - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - surf.write_region(r, &self.pre_surf.read_region(r)); - Ok(apply_coverage(surf, r, cov, self.opacity, self.sel.as_ref(), self.lock, &paint, self.mode).union(&r)) - } -} - -/// Grow a stroke's coverage map by `m` pixels of zero coverage (a Dirichlet boundary for the solve). -fn pad_coverage(bounds: Rect, cov: &[f32], m: i32) -> (Rect, Vec) { - let g = bounds.inflate(m); - let (w, gw) = (bounds.width() as usize, g.width() as usize); - let mut out = vec![0.0f32; gw * g.height() as usize]; - for (i, c) in cov.iter().enumerate() { - let (x, y) = (i % w + m as usize, i / w + m as usize); - out[y * gw + x] = *c; - } - (g, out) -} - -/// Seamless-clone `src` into `dst` over the mask (both regions cover the same rect). The solve runs -/// only on the mask's bounding box plus a one-pixel Dirichlet ring; the rest is `dst`. -fn heal_region(fmt: &PixelFormat, src: &Region, dst: &Region, mask: &[bool]) -> Region { - let w = dst.width(); - let bbox = mask.iter().enumerate().filter(|(_, m)| **m).fold(Rect::EMPTY, |r, (i, _)| { - let (x, y) = (dst.rect.x0 + (i % w) as i32, dst.rect.y0 + (i / w) as i32); - r.union(&Rect::new(x, y, x + 1, y + 1)) - }); - let mut out = dst.clone(); - if bbox.is_empty() { - return out; - } - let r = bbox.inflate(1).intersect(&dst.rect); - let (s, d) = (src.crop(r), dst.crop(r)); - let m: Vec = (r.y0..r.y1).flat_map(|y| (r.x0..r.x1).map(move |x| (x, y))).map(|(x, y)| mask[dst.index(x, y)]).collect(); - let mut data = poisson::seamless_clone(d.width(), d.height(), d.ch, &s.data, &d.data, &m); - clamp_samples(fmt, &mut data); - let (n, rw) = (dst.ch, r.width() as usize); - for y in r.y0..r.y1 { - let o = out.index(r.x0, y) * n; - let i = (y - r.y0) as usize * rw * n; - out.data[o..o + rw * n].copy_from_slice(&data[i..i + rw * n]); - } - out -} - -fn healing_brush(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.healingBrush"; - let (stroke, id) = parse_brush(s, p, CMD)?; - let which = sample_layers(p, CMD)?; - let mode = blend_param(p, CMD)?; - let aligned = flag(p, "aligned", true); - let map = clone_mapping(s, p, &stroke, CMD)?; - let off = map.offset(); - let dmg = run_stroke(s, "Healing Brush", id, p, |pre, surf, sel, lock| { - let (bounds, cov) = stroke_coverage(&stroke); - let (g, cov) = pad_coverage(bounds, &cov, 2); - let fmt = surf.format(); - let src = clone_sample(pre, id, surf, which, g, &map, false, CMD)?; - // The destination the texture is fitted to: the layer itself, or what the user sees when - // sampling several layers (so healing onto an empty layer matches the composite). - let dst = if which == SampleLayers::Current { Region::read(surf, g) } else { composite_region(pre, id, which, g, fmt) }; - let mask: Vec = cov.iter().map(|c| *c > 0.0).collect(); - let healed = heal_region(&fmt, &src, &dst, &mask); - Ok(apply_coverage(surf, g, &cov, stroke.brush.opacity, sel, lock, &healed, mode)) - })?; - Ok(clone_result(dmg, off, aligned, p, &stroke)) -} - -/// The History Brush's source: the targeted surface (pixels, mask, channel) as it was in history -/// state `state` (default: the oldest state still held, the "Open" snapshot unless trimmed). -fn history_source(s: &Session, id: Option, p: &Value, cmd: &str) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let explicit = p.get("state").and_then(Value::as_u64).map(|v| v as usize); - let source_doc = match explicit { - Some(i) if i == st.history.past_len() => st.doc.clone(), - Some(i) => st.history.state(i).ok_or_else(|| bad(cmd, format!("no history state {i} (0..={})", st.history.past_len())))?, - None => st.history.state(0).unwrap_or_else(|| st.doc.clone()), - }; - crate::channel_cmds::read_target_surface(source_doc.as_ref(), id, p) - .cloned() - .map_err(|_| EngineError::Other("the target did not exist (as pixels, a mask or a channel) in that history state".into())) -} - -fn history_brush(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.historyBrush"; - let (stroke, id) = parse_brush(s, p, CMD)?; - let mode = blend_param(p, CMD)?; - let live = s.live_commit.take().filter(|l| l.matches(s, CMD, p, &stroke.points)); - let from_live = live.is_some(); - let src_surface = history_source(s, id, p, CMD)?; - let dmg = run_stroke(s, "History Brush", id, p, |_, surf, sel, lock| { - if let Some(l) = live { - *surf = l.surface; - return Ok(l.bounds); - } - let fmt = surf.format(); - let src = if src_surface.format() == fmt { src_surface } else { src_surface.convert(fmt) }; - let (bounds, cov) = stroke_coverage(&stroke); - let paint = Region::read(&src, bounds); - Ok(apply_coverage(surf, bounds, &cov, stroke.brush.opacity, sel, lock, &paint, mode)) - })?; - let mut r = json!({ "damage": damage_json(dmg) }); - if from_live { - r["fromLive"] = json!(true); - } - Ok(r) -} - -// --------------------------------------------------------------------------------------------- -// Spot Healing -// --------------------------------------------------------------------------------------------- - -/// Dilate a mask by `r` pixels (square structuring element). -fn dilate(w: usize, h: usize, m: &[bool], r: usize) -> Vec { - let mut tmp = vec![false; w * h]; - for y in 0..h { - for x in 0..w { - if m[y * w + x] { - for xx in x.saturating_sub(r)..(x + r + 1).min(w) { - tmp[y * w + xx] = true; - } - } - } - } - let mut out = vec![false; w * h]; - for y in 0..h { - for x in 0..w { - if tmp[y * w + x] { - for yy in y.saturating_sub(r)..(y + r + 1).min(h) { - out[yy * w + x] = true; - } - } - } - } - out -} - -/// Copy a translated Proximity Match patch from the *pre-healing* pixels. Reading -/// `out` here would make overlapping offsets repeatedly copy the pixels just written, -/// leaving streaks instead of the selected source texture. -fn proximity_source_patch(img: &[f32], w: usize, h: usize, ch: usize, mask: &[bool], dx: i32, dy: i32) -> Option> { - if w == 0 || h == 0 || ch == 0 || mask.len() != w.checked_mul(h)? || img.len() != mask.len().checked_mul(ch)? { - return None; - } - let mut out = img.to_vec(); - for (p, masked) in mask.iter().enumerate() { - if !masked { - continue; - } - let x = i32::try_from(p % w).ok()?.checked_add(dx)?; - let y = i32::try_from(p / w).ok()?.checked_add(dy)?; - if x < 0 || y < 0 || x >= i32::try_from(w).ok()? || y >= i32::try_from(h).ok()? { - return None; - } - let src = (usize::try_from(y).ok()?.checked_mul(w)? + usize::try_from(x).ok()?).checked_mul(ch)?; - let dst = p.checked_mul(ch)?; - out.get_mut(dst..dst + ch)?.copy_from_slice(img.get(src..src + ch)?); - } - Some(out) -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum SpotType { - ContentAware, - CreateTexture, - ProximityMatch, -} - -/// Fill the stroke area with no source, then gradient-domain blend it into its surroundings. With -/// `all` (Sample All Layers) it heals what is visible, so it works on an empty layer above the image. -/// -/// Photoshop 25.4 (measured): a dab changes exactly the brush's footprint, and Content-Aware takes -/// its texture from around it, as Edit › Fill does from its window -/// ([`crate::fill_cmds::sampling_window`] of the stroke's bounds), which is what this samples. -fn spot_heal_surface(surf: &mut Surface, pre: &Document, stroke: &Stroke, kind: SpotType, all: bool, sel: Option<&Surface>, lock: bool) -> Rect { - let (bounds, cov) = stroke_coverage(stroke); - let size = stroke.brush.size; - let region_rect = crate::fill_cmds::sampling_window(bounds, pre.bounds()); - if region_rect.is_empty() { - return Rect::EMPTY; - } - // Proximity Match looks for its source anywhere in that window. - let margin = (region_rect.width().max(region_rect.height()) / 2) as i32; - let fmt = surf.format(); - let img = if all { composite_region(pre, None, SampleLayers::All, region_rect, fmt) } else { Region::read(surf, region_rect) }; - let (w, h, ch) = (img.width(), img.height(), img.ch); - let bw = bounds.width() as usize; - let mut stroke_cov = vec![0.0f32; w * h]; - for (i, c) in cov.iter().enumerate() { - let (x, y) = (bounds.x0 + (i % bw) as i32, bounds.y0 + (i / bw) as i32); - if region_rect.contains(x, y) { - stroke_cov[img.index(x, y)] = *c; - } - } - let hole: Vec = stroke_cov.iter().map(|c| *c > 0.0).collect(); - if !hole.iter().any(|h| *h) { - return Rect::EMPTY; - } - // Synthesise over a slightly larger domain than the Poisson region, so the solve's boundary pixels - // carry synthesised values that differ from the untouched surroundings: that mismatch is what the - // membrane corrects (otherwise the Dirichlet data would be all zero and the blend a no-op). - let domain = dilate(w, h, &hole, 2); - let heal_mask = dilate(w, h, &hole, 1); - let content_aware = - |img: &[f32]| inpaint::complete(w, h, ch, img, &domain, &CompleteParams::default()).unwrap_or_else(|| poisson::membrane_fill(w, h, ch, img, &domain)); - let filled = match kind { - SpotType::ContentAware => content_aware(&img.data), - SpotType::CreateTexture => inpaint::synthesize(w, h, ch, &img.data, &domain, (size / 4.0).clamp(6.0, 32.0) as usize, 0x5eed), - SpotType::ProximityMatch => { - let ring = (size / 8.0).clamp(3.0, 16.0) as usize; - match inpaint::best_offset(w, h, ch, &img.data, &domain, ring, margin) { - Some((dx, dy)) => proximity_source_patch(&img.data, w, h, ch, &domain, dx, dy).unwrap_or_else(|| content_aware(&img.data)), - None => content_aware(&img.data), - } - } - }; - let src = Region { rect: region_rect, ch, data: filled }; - let healed = heal_region(&fmt, &src, &img, &heal_mask); - let dmg = bounds.intersect(®ion_rect); - let dmg_cov: Vec = (dmg.y0..dmg.y1).flat_map(|y| (dmg.x0..dmg.x1).map(move |x| (x, y))).map(|(x, y)| stroke_cov[img.index(x, y)]).collect(); - apply_coverage(surf, dmg, &dmg_cov, stroke.brush.opacity, sel, lock, &healed, BlendMode::Normal) -} - -fn spot_healing(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "paint.spotHealing"; - let (stroke, id) = parse_brush(s, p, CMD)?; - let kind = match string(p, "type", "contentAware") { - "contentAware" => SpotType::ContentAware, - "createTexture" => SpotType::CreateTexture, - "proximityMatch" => SpotType::ProximityMatch, - o => return Err(bad(CMD, format!("unknown type `{o}` (contentAware|createTexture|proximityMatch)"))), - }; - let all = sample_all_layers(p); - let dmg = run_stroke(s, "Spot Healing Brush", id, p, |pre, surf, sel, lock| Ok(spot_heal_surface(surf, pre, &stroke, kind, all, sel, lock)))?; - Ok(json!({ "damage": damage_json(dmg) })) -} - -// --------------------------------------------------------------------------------------------- -// Toning (Dodge / Burn / Sponge) and focus (Blur / Sharpen), Smudge -// --------------------------------------------------------------------------------------------- - -/// Per-dab strength such that one pass over a point totals roughly `e`, whatever the spacing -/// (about `1/spacing` dabs overlap each point on the stroke's centre line). -#[inline] -fn per_dab(e: f32, spacing: f32) -> f32 { - 1.0 - (1.0 - e.clamp(0.0, 1.0)).powf(spacing.clamp(0.01, 1.0)) -} - -/// Apply an RGB colour transform `f(rgb, strength)` to each covered pixel of a dab (through straight -/// RGBA, so it works for Gray/CMYK/Lab too; untouched pixels are never round-tripped). -fn color_dab(fmt: &PixelFormat, work: &mut Region, fp: &Footprint, strength: f32, spacing: f32, f: impl Fn([f32; 3], f32) -> [f32; 3]) { - let r = fp.rect.intersect(&work.rect); - let n = fmt.channels(); - let mut enc = [0.0f32; 8]; - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let k = per_dab(fp.at(x, y) * strength, spacing); - if k <= 0.0 { - continue; - } - let px = work.px_mut(x, y); - let rgba = to_rgba(fmt, px); - let o = f([rgba[0], rgba[1], rgba[2]], k); - from_rgba_into(fmt, [o[0], o[1], o[2], rgba[3]], &mut enc); - let a = alpha_index(fmt); - for c in 0..n { - if Some(c) != a { - px[c] = enc[c]; - } - } - } - } -} - -/// Dodge and Burn: a stroke tones each pixel once, from its original colour, mixed in by the -/// stroke's coverage there (`toned` is the full-exposure colour). Dabs build up coverage as in a -/// brush stroke, capped at full coverage, so Exposure caps a stroke the way Opacity caps a paint -/// stroke: overlapping dabs and passes within one stroke don't compound the tone curve, and a -/// new stroke builds on the last. -fn tone_stroke(fmt: PixelFormat, toned: impl Fn([f32; 3]) -> [f32; 3] + 'static) -> DabEffect { - /// Side of the cells that remember, per touched pixel, the coverage applied so far and the - /// original pixel (copied from the working pixels the first time a dab reaches it). Dense - /// cells rather than a map per pixel: a map lookup per covered pixel made long strokes with - /// large brushes over ten times slower. - const CELL: i32 = 64; - struct Cell { - cov: Vec, - orig: Vec, - } - let mut cells: std::collections::HashMap<(i32, i32), Cell> = std::collections::HashMap::new(); - let a = alpha_index(&fmt); - let n = fmt.channels(); - Box::new(move |work, fp| { - let r = fp.rect.intersect(&work.rect); - if r.is_empty() { - return; - } - let mut enc = [0.0f32; 8]; - for cy in r.y0.div_euclid(CELL)..=(r.y1 - 1).div_euclid(CELL) { - for cx in r.x0.div_euclid(CELL)..=(r.x1 - 1).div_euclid(CELL) { - let cr = Rect::new(cx * CELL, cy * CELL, cx * CELL + CELL, cy * CELL + CELL); - let part = r.intersect(&cr); - if part.is_empty() { - continue; - } - let len = (CELL * CELL) as usize; - // Coverage -1: not touched yet, so `orig` isn't filled in for that pixel. - let cell = cells.entry((cx, cy)).or_insert_with(|| Cell { cov: vec![-1.0; len], orig: vec![0.0; len * n] }); - for y in part.y0..part.y1 { - for x in part.x0..part.x1 { - let k = fp.at(x, y).clamp(0.0, 1.0); - if k <= 0.0 { - continue; - } - let i = ((y - cr.y0) * CELL + (x - cr.x0)) as usize; - let (Some(prev), Some(src)) = (cell.cov.get_mut(i), cell.orig.get_mut(i * n..i * n + n)) else { continue }; - if *prev < 0.0 { - // First touch: the working pixel is still the original. - src.copy_from_slice(work.px(x, y)); - *prev = 0.0; - } - // Dabs build up the stroke's coverage as a brush stroke's do (flow), up - // to full coverage; the tone is applied once, from the original. - let k = *prev + k * (1.0 - *prev); - if k <= *prev { - continue; - } - *prev = k; - let o = to_rgba(&fmt, src); - let t = toned([o[0], o[1], o[2]]); - let m = [o[0] + (t[0] - o[0]) * k, o[1] + (t[1] - o[1]) * k, o[2] + (t[2] - o[2]) * k]; - from_rgba_into(&fmt, [m[0], m[1], m[2], o[3]], &mut enc); - let px = work.px_mut(x, y); - for c in 0..n { - if Some(c) != a { - px[c] = enc[c]; - } - } - } - } - } - } - }) -} - -fn tone_range(p: &Value, cmd: &str) -> Result { - match string(p, "range", "midtones") { - "shadows" => Ok(ToneRange::Shadows), - "midtones" => Ok(ToneRange::Midtones), - "highlights" => Ok(ToneRange::Highlights), - o => Err(bad(cmd, format!("unknown range `{o}` (shadows|midtones|highlights)"))), - } -} - -/// A sequential-dab tool's settings (Dodge, Burn, Sponge, Blur, Sharpen, Smudge), parsed once and -/// turned into its per-dab effect, for the commit and the live preview alike. -#[derive(Clone, Debug)] -enum DabTool { - Tone { burn: bool, range: ToneRange, exposure: f32, protect: bool }, - Sponge { saturate: bool, vibrance: bool }, - Focus { sharpen: bool, strength: f32, protect: bool, sigma: f32 }, - Smudge { strength: f32, finger: Option<[f32; 4]>, max_size: f32 }, -} - -/// One dab's edit of the working copy. -pub(crate) type DabEffect = Box; - -impl DabTool { - /// The tool for `cmd` and its history label. - fn parse(s: &Session, cmd: &str, p: &Value, stroke: &Stroke) -> Result<(Self, &'static str)> { - Ok(match cmd { - "paint.dodge" | "paint.burn" => { - let burn = cmd == "paint.burn"; - let tool = Self::Tone { - burn, - range: tone_range(p, cmd)?, - exposure: num(p, "exposure", 50.0).clamp(1.0, 100.0) / 100.0, - protect: flag(p, "protectTones", true), - }; - (tool, if burn { "Burn Tool" } else { "Dodge Tool" }) - } - "paint.sponge" => { - let saturate = match string(p, "mode", "desaturate") { - "desaturate" => false, - "saturate" => true, - o => return Err(bad(cmd, format!("unknown mode `{o}` (desaturate|saturate)"))), - }; - (Self::Sponge { saturate, vibrance: flag(p, "vibrance", true) }, "Sponge Tool") - } - "paint.blur" | "paint.sharpen" => { - let sharpen = cmd == "paint.sharpen"; - let tool = Self::Focus { - sharpen, - strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, - protect: flag(p, "protectDetail", true), - sigma: (stroke.brush.size * 0.03).clamp(1.0, 4.0), - }; - (tool, if sharpen { "Sharpen Tool" } else { "Blur Tool" }) - } - "paint.smudge" => { - let finger = flag(p, "fingerPainting", false).then_some(s.tools.foreground); - (Self::Smudge { strength: num(p, "strength", 50.0).clamp(1.0, 100.0) / 100.0, finger, max_size: stroke.brush.size }, "Smudge Tool") - } - o => return Err(bad(o, "not a sequential-dab tool")), - }) - } - - /// How far around each dab the effect reads (Blur and Sharpen read a neighbourhood). - fn halo(&self) -> i32 { - match self { - Self::Focus { sigma, .. } => (sigma * 3.0).ceil() as i32 + 1, - _ => 0, - } - } - - /// The per-dab effect on a working copy in `fmt`, for a brush of `spacing`. - fn effect(&self, fmt: PixelFormat, spacing: f32) -> DabEffect { - let sp = spacing; - match self.clone() { - Self::Tone { burn, range, exposure, protect } => tone_stroke(fmt, move |c| dodge_burn(c, exposure, range, burn, protect)), - // Flow is already in each dab's coverage; a full-flow pass moves colours half-way. - Self::Sponge { saturate, vibrance } => Box::new(move |work, fp| color_dab(&fmt, work, fp, 0.5, sp, |c, k| sponge(c, k, saturate, vibrance))), - Self::Focus { sharpen, strength, protect, sigma } => { - let a = alpha_index(&fmt); - Box::new(move |work, fp| { - let r = fp.rect.intersect(&work.rect); - if r.is_empty() { - return; - } - let (w, h, n) = (work.width(), work.height(), work.ch); - let win = ((r.x0 - work.rect.x0) as usize, (r.y0 - work.rect.y0) as usize, (r.x1 - work.rect.x0) as usize, (r.y1 - work.rect.y0) as usize); - let out = if sharpen { local_sharpen(&work.data, w, h, n, a, win, 1.0, protect) } else { local_blur(&work.data, w, h, n, a, win, sigma) }; - let rw = r.width() as usize; - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let k = per_dab(fp.at(x, y) * strength, sp); - if k <= 0.0 { - continue; - } - let o = ((y - r.y0) as usize * rw + (x - r.x0) as usize) * n; - let px = work.px_mut(x, y); - for c in 0..n { - px[c] += (out[o + c] - px[c]) * k; - } - } - } - }) - } - Self::Smudge { strength, finger, max_size } => { - let finger_px = finger.map(|fg| photocraft_raster::from_rgba(&fmt, fg)); - // Premultiplied mixing: transparent pixels carry no colour, so no dark fringes. - let mut sm = Smudge::new(strength, finger_px, max_size).with_alpha(alpha_index(&fmt)); - Box::new(move |work, fp| sm.dab(work, fp)) - } - } - } -} - -/// Dodge, Burn, Sponge, Blur, Sharpen and Smudge: each dab edits a working copy in order -/// (`apply_dab_stroke`). Blur, Sharpen and Smudge can sample all layers. -fn dab_cmd(s: &mut Session, p: &Value, cmd: &str) -> Result { - let (stroke, id) = parse_brush(s, p, cmd)?; - let (tool, label) = DabTool::parse(s, cmd, p, &stroke)?; - let all = matches!(tool, DabTool::Focus { .. } | DabTool::Smudge { .. }) && sample_all_layers(p); - let dmg = run_stroke(s, label, id, p, |pre, surf, sel, lock| { - let fmt = surf.format(); - let mut effect = tool.effect(fmt, stroke.brush.spacing); - let mut sampler = all.then(|| AllLayers::new(pre, fmt)); - Ok(apply_dab_stroke(surf, &stroke, sel, lock, tool.halo(), |work, fp| match &mut sampler { - Some(all) => all.dab(work, fp, |w, f| effect(w, f)), - None => effect(work, fp), - })) - })?; - Ok(json!({ "damage": damage_json(dmg) })) -} - -/// A Dodge, Burn, Sponge, Blur, Sharpen or Smudge stroke shown while it is drawn: each new dab -/// runs the tool's effect on a working copy of the target, in order, and the changed area is -/// mixed back over the original at the brush opacity, as `apply_dab_stroke` does on release. -/// Sample All Layers isn't previewed (its composite spans the whole stroke). -pub struct LiveDab { - /// The active document with the stroke so far. - pub doc: std::sync::Arc, - cmd: String, - renderer: photocraft_paint::StrokeRenderer, - done: usize, - effect: DabEffect, - halo: i32, - pre_surf: Surface, - work: Surface, - id: Option, - params: Value, - opacity: f32, - sel: Option, - lock: bool, - bounds: Rect, - /// Show the dabs finishing the stroke would add, so the preview at the last point is the - /// commit. Only for effects that a repeated dab doesn't change (Color Replacement): the - /// tail runs on a scratch copy at every push, so an effect that carries paint from dab to - /// dab (Smudge) would drift. - preview_tail: bool, - /// Where the shown tail is. - tail: Rect, -} - -impl LiveDab { - /// Start a live stroke of `cmd` from its params; their `points` are rendered. - pub fn begin(s: &Session, cmd: &str, p: &Value) -> Result { - has_pixel_layer(s).map_err(EngineError::Other)?; - if sample_all_layers(p) { - return Err(bad(cmd, "Sample All Layers strokes aren't previewed live")); - } - let (stroke, id) = parse_brush(s, p, cmd)?; - let (tool, _) = DabTool::parse(s, cmd, p, &stroke)?; - let (halo, spacing) = (tool.halo(), stroke.brush.spacing); - Self::with_effect(s, cmd, p, stroke, id, halo, |pre| tool.effect(pre.format(), spacing)) - } - - /// Start a live stroke of `cmd` that runs `effect` (made from the target before the stroke) - /// at each dab of `stroke`, reading `halo` pixels around it. `p` gives the target. - pub(crate) fn with_effect( - s: &Session, - cmd: &str, - p: &Value, - stroke: Stroke, - id: Option, - halo: i32, - effect: impl FnOnce(&Surface) -> DabEffect, - ) -> Result { - let mut doc = (*s.active().ok_or(EngineError::NoDocument)?.doc).clone(); - let sel = doc.selection.clone(); - let (surf, lock) = crate::channel_cmds::target_surface(&mut doc, id, p)?; - let pre_surf = surf.clone(); - let effect = effect(&pre_surf); - // `apply_dab_stroke` generates the same dabs (`dabs`: zoom 1, no smoothing scale). - let renderer = photocraft_paint::StrokeRenderer::new(&stroke.brush, None, 1.0).record_dabs(); - let mut live = Self { - doc: std::sync::Arc::new(doc), - cmd: cmd.into(), - renderer, - done: 0, - effect, - halo, - work: pre_surf.clone(), - pre_surf, - id, - params: p.clone(), - opacity: stroke.brush.opacity, - sel, - lock, - bounds: Rect::EMPTY, - preview_tail: false, - tail: Rect::EMPTY, - }; - live.push(&stroke.points)?; - Ok(live) - } - - /// Also show the stroke's finishing dabs (see `preview_tail`). - pub(crate) fn with_tail_preview(mut self) -> Result { - self.preview_tail = true; - self.push(&[])?; - Ok(self) - } - - /// Everything the stroke has touched so far. - pub fn bounds(&self) -> Rect { - self.bounds - } - - /// Render more points; returns the rectangle that changed. - /// Invalid coordinates reject the whole batch without changing the preview. - pub fn push(&mut self, pts: &[StrokePoint]) -> Result { - crate::brush_cmds::check_coords(pts, &self.cmd)?; - self.renderer.push(pts); - let ctx = self.renderer.ctx.clone(); - let new: Vec<_> = self.renderer.dabs().get(self.done..).unwrap_or_default().to_vec(); - let mut dmg = Rect::EMPTY; - for (k, d) in new.iter().enumerate() { - let r = ctx.dab_rect(d, false); - if r.is_empty() { - continue; - } - let mut region = Region::read(&self.work, r.inflate(self.halo)); - (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); - region.crop(r).write(&mut self.work); - dmg = dmg.union(&r); - } - self.done += new.len(); - // The finishing dabs, run on a scratch copy of the working pixels. - let mut tail = None; - if self.preview_tail { - let ds = self.renderer.tail_dabs(); - let r = ds.iter().fold(Rect::EMPTY, |a, d| a.union(&ctx.dab_rect(d, false))); - if !r.is_empty() { - let mut region = Region::read(&self.work, r.inflate(self.halo)); - for (k, d) in ds.iter().enumerate() { - (self.effect)(&mut region, &ctx.footprint(d, self.done + k)); - } - tail = Some((r, region.crop(r))); - } - } - // Back to the stroke without the previous tail. - let old = std::mem::replace(&mut self.tail, Rect::EMPTY); - let shown = dmg.union(&old); - let tail_rect = tail.as_ref().map_or(Rect::EMPTY, |(r, _)| *r); - if shown.is_empty() && tail_rect.is_empty() { - return Ok(Rect::EMPTY); - } - self.bounds = self.bounds.union(&dmg).union(&tail_rect); - let (surf, _) = crate::channel_cmds::target_surface(std::sync::Arc::make_mut(&mut self.doc), self.id, &self.params)?; - if !shown.is_empty() { - let (orig, work) = (Region::read(&self.pre_surf, shown), Region::read(&self.work, shown)); - photocraft_paint::retouch::mix_back(surf, &orig, &work, shown, self.opacity, self.sel.as_ref(), self.lock); - } - if let Some((r, work)) = tail { - photocraft_paint::retouch::mix_back(surf, &Region::read(&self.pre_surf, r), &work, r, self.opacity, self.sel.as_ref(), self.lock); - self.tail = r; - } - Ok(shown.union(&tail_rect)) - } -} - -/// "Sample All Layers" for Spot Healing, Blur, Sharpen and Smudge (layer pixels only: a mask or -/// channel has nothing to sample from other layers). -fn sample_all_layers(p: &Value) -> bool { - flag(p, "sampleAllLayers", false) && targets_pixels(p) -} - -/// Sample All Layers for the sequential-dab tools: each dab's effect runs on a copy of the visible -/// composite (kept across dabs, so a dab sees the earlier ones), and the result is laid onto the -/// layer under the dab. An empty layer above the image so picks up the blurred or smudged picture, -/// and pixels the effect didn't change look as before (they are what was already visible). -struct AllLayers<'a> { - pre: &'a Document, - fmt: PixelFormat, - visible: Option, -} - -impl<'a> AllLayers<'a> { - fn new(pre: &'a Document, fmt: PixelFormat) -> Self { - Self { pre, fmt, visible: None } - } - - fn dab(&mut self, work: &mut Region, fp: &Footprint, effect: impl FnOnce(&mut Region, &Footprint)) { - let (pre, fmt) = (self.pre, self.fmt); - let visible = self.visible.get_or_insert_with(|| composite_region(pre, None, SampleLayers::All, work.rect, fmt)); - if visible.rect != work.rect || visible.ch != work.ch { - return effect(work, fp); - } - effect(visible, fp); - let r = fp.rect.intersect(&work.rect); - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - if fp.at(x, y) > 0.0 { - work.px_mut(x, y).copy_from_slice(visible.px(x, y)); - } - } - } - } -} - -// --------------------------------------------------------------------------------------------- -// Registry -// --------------------------------------------------------------------------------------------- - -/// The brush every retouching command shares, plus the tool's own params. -macro_rules! brush_params { - ($extra:literal) => { - concat!("{", r#""points":[[x,y,pressure?],…],"size":1..5000 px=tool size,"hardness":0..100=tool hardness,"opacity":1..100=100,"flow":1..100=100,"spacing":1..1000 (% of size)=Brush Settings spacing,"layer":id?=active,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target"#, $extra, "}") - }; -} - -/// Retouching command specs. -pub fn specs() -> Vec { - vec![ - CommandSpec { - id: "paint.cloneStamp", - label: "Clone Stamp", - menu: &[], - shortcut: None, - params: brush_params!( - r#","source":[sx,sy] (sampled under the first point) | "offset":[dx,dy],"aligned":bool=true,"sampleLayer":"current|currentAndBelow|all"="current","mode":"normal|multiply|…"="normal","wrap":bool=false (repeat source and stroke at document edges for Pattern Preview) → {"damage","offset","aligned","nextSource"}"# - ), - enabled: has_pixel_layer, - run: clone_stamp, - journal: true, - }, - CommandSpec { - id: "paint.patternStamp", - label: "Pattern Stamp", - menu: &[], - shortcut: None, - params: brush_params!( - r#","pattern":id|name?=Patterns panel current,"scale":1..1000 %=100,"angle":deg=0,"aligned":bool=true,"impressionist":bool=false,"mode":"normal|multiply|…"="normal","phase":[px,py]? → {"damage","phase","aligned","pattern"}"# - ), - enabled: pattern_stamp::enabled, - run: pattern_stamp::run, - journal: true, - }, - CommandSpec { - id: "paint.healingBrush", - label: "Healing Brush", - menu: &[], - shortcut: None, - params: brush_params!( - r#","source":[sx,sy] | "offset":[dx,dy],"aligned":bool=true,"sampleLayer":"current|currentAndBelow|all"="current","mode":"normal|…"="normal" → {"damage","offset","aligned","nextSource"}"# - ), - enabled: has_pixel_layer, - run: healing_brush, - journal: true, - }, - CommandSpec { - id: "paint.spotHealing", - label: "Spot Healing Brush", - menu: &[], - shortcut: None, - params: brush_params!(r#","type":"contentAware|createTexture|proximityMatch"="contentAware","sampleAllLayers":bool=false"#), - enabled: has_pixel_layer, - run: spot_healing, - journal: true, - }, - CommandSpec { - id: "paint.patch", - label: "Patch", - menu: &[], - shortcut: None, - params: r#"{"offset":[dx,dy] (how far the selection was dragged),"mode":"source|destination"="source","contentAware":bool=false (fill the selection content-aware from the dragged-to place; a background job),"structure":1..7=4 (content-aware: 3..7 copy the middle exactly, 1..2 re-synthesise more of it),"color":0..10=0 (content-aware: how far the copy's level adapts to the selection's surroundings),"sampleAllLayers":bool=false (content-aware),"layer":id?=active,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target} → {"damage","offset"}"#, - enabled: patch::enabled, - run: patch::patch, - journal: true, - }, - CommandSpec { - id: "paint.contentAwareMove", - label: "Content-Aware Move", - menu: &[], - shortcut: None, - params: r#"{"offset":[dx,dy] (how far the selection was dragged),"mode":"move|extend"="move","structure":1..7=4 (3..7 copy the content exactly but for a band about a patch wide along its edge; 2 widens the band, 1 re-synthesises the content too),"color":0..10=0 (0 keeps the content's level; higher values bring it towards its new place, by up to about 0.04 × color),"sampleAllLayers":bool=false,"scaleX":%=100,"scaleY":%=100 (Transform On Drop: 1..10000, negative flips),"rotate":deg=0 (Transform On Drop, clockwise; scale and rotation are about the selection's centre, before the offset; a transformed patch may reach past the canvas edge),"layer":id?=active,"target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target} → {"damage","offset","mode","transformed"?} (a background job; the selection moves, and is transformed, with the content)"#, - enabled: content_aware_move::enabled, - run: content_aware_move::content_aware_move, - journal: true, - }, - CommandSpec { - id: "paint.remove", - label: "Remove", - menu: &[], - shortcut: None, - params: brush_params!( - r#","strokes":[[[x,y,pressure?],…],…]? (several strokes removed together, as one job and one undo step; else "points"),"closeLoops":bool=true (a stroke around an object removes what it encloses),"sampleAllLayers":bool=false,"newLayer":bool=false (the fill goes to a new layer above the target, which stays as it was) → {"damage","layer"?} (a background job)"# - ), - enabled: has_pixel_layer, - run: remove::remove, - journal: true, - }, - CommandSpec { - id: "paint.dodge", - label: "Dodge", - menu: &[], - shortcut: None, - params: brush_params!(r#","range":"shadows|midtones|highlights"="midtones","exposure":1..100=50,"protectTones":bool=true"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.dodge"), - journal: true, - }, - CommandSpec { - id: "paint.burn", - label: "Burn", - menu: &[], - shortcut: None, - params: brush_params!(r#","range":"shadows|midtones|highlights"="midtones","exposure":1..100=50,"protectTones":bool=true"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.burn"), - journal: true, - }, - CommandSpec { - id: "paint.sponge", - label: "Sponge", - menu: &[], - shortcut: None, - params: brush_params!(r#","mode":"desaturate|saturate"="desaturate","vibrance":bool=true (flow = sponge strength)"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.sponge"), - journal: true, - }, - CommandSpec { - id: "paint.blur", - label: "Blur", - menu: &[], - shortcut: None, - params: brush_params!(r#","strength":1..100=50,"sampleAllLayers":bool=false"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.blur"), - journal: true, - }, - CommandSpec { - id: "paint.sharpen", - label: "Sharpen", - menu: &[], - shortcut: None, - params: brush_params!(r#","strength":1..100=50,"protectDetail":bool=true,"sampleAllLayers":bool=false"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.sharpen"), - journal: true, - }, - CommandSpec { - id: "paint.smudge", - label: "Smudge", - menu: &[], - shortcut: None, - params: brush_params!(r#","strength":1..100=50,"fingerPainting":bool=false (starts with the foreground colour),"sampleAllLayers":bool=false"#), - enabled: has_pixel_layer, - run: |s, p| dab_cmd(s, p, "paint.smudge"), - journal: true, - }, - CommandSpec { - id: "paint.historyBrush", - label: "History Brush", - menu: &[], - shortcut: None, - params: brush_params!(r#","state":index (into history.entries; default 0 = the oldest held state, normally the Open snapshot)"#), - enabled: has_pixel_layer, - run: history_brush, - journal: true, - }, - ] -} - -#[cfg(test)] -mod tests; diff --git a/crates/engine/src/sampler_cmds.rs b/crates/engine/src/sampler_cmds.rs deleted file mode 100644 index 472ecb672a..0000000000 --- a/crates/engine/src/sampler_cmds.rs +++ /dev/null @@ -1,475 +0,0 @@ -//! Color Sampler points (#1046): numbered colour readings pinned to canvas pixels and listed in -//! the Info panel, so a fixed position can be compared instead of chasing the live pointer. -//! -//! Points are per-document view state ([`crate::DocState::color_samplers`]): they belong to their -//! document, are discarded when it closes, and are not saved to `.pcraft` or PSD. Placing, moving -//! and removing them changes no pixels and adds no History step, so they are not journaled. Each -//! point reads one composite pixel (no averaging); its values match the Info panel's pointer -//! readout (RGB 0–255 and the naive device CMYK percentages, without an ICC profile). - -use photocraft_doc::Document; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{DocState, EngineError, Result, Session}; - -/// Photoshop's Color Sampler limit: at most 10 points per document. -pub const MAX_SAMPLERS: usize = 10; - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} - -/// Naive device CMYK (no ICC profile), in percent, as the Info panel shows it. 32-bit samples can -/// lie outside 0..1, so each channel is clamped first (NaN reads as 0), like the RGB readout. -pub fn device_cmyk(r: f32, g: f32, b: f32) -> [i32; 4] { - let unit = |v: f32| if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; - let (r, g, b) = (unit(r), unit(g), unit(b)); - let k = 1.0 - r.max(g).max(b); - let f = |v: f32| if k >= 1.0 { 0.0 } else { (1.0 - v - k) / (1.0 - k) }; - [f(r), f(g), f(b), k].map(|v| (v * 100.0).round() as i32) -} - -/// The Info-panel values for a sample point at document pixel `pos`: composite RGB 0–255 and -/// naive device CMYK percentages. One composite pixel, no averaging. -pub fn readout(doc: &Document, pos: [f64; 2]) -> ([i32; 3], [i32; 4]) { - let rgba = crate::sample_cmds::sample_color(doc, None, pos[0], pos[1], 1, crate::sample_cmds::SampleLayers::All); - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as i32; - ([q(rgba[0]), q(rgba[1]), q(rgba[2])], device_cmyk(rgba[0], rgba[1], rgba[2])) -} - -/// One point as the list command reports it. -pub fn point_json(doc: &Document, index: usize, pos: [f64; 2]) -> Value { - let (rgb, cmyk) = readout(doc, pos); - json!({"index": index, "number": index + 1, "position": [pos[0], pos[1]], "rgb": rgb, "cmyk": cmyk}) -} - -fn list_json(doc: &Document, points: &[[f64; 2]]) -> Value { - json!({ - "max": MAX_SAMPLERS, - "samplers": points.iter().enumerate().map(|(i, p)| point_json(doc, i, *p)).collect::>(), - }) -} - -fn coord(cmd: &str, p: &Value, key: &str) -> Result { - p.get(key).and_then(Value::as_f64).filter(|v| v.is_finite()).ok_or_else(|| bad(cmd, format!("`{key}` must be a finite number (document pixels)"))) -} - -/// The pixel a point at document coordinate `(x, y)` sits on, if it is inside the canvas. Whole -/// pixels only: the click's fractional position floors to the pixel it lands on. -fn pixel(doc: &Document, x: f64, y: f64) -> Option<[f64; 2]> { - let (px, py) = (x.floor(), y.floor()); - if px < f64::from(i32::MIN) || px > f64::from(i32::MAX) || py < f64::from(i32::MIN) || py > f64::from(i32::MAX) { - return None; - } - doc.bounds().contains(px as i32, py as i32).then_some([px, py]) -} - -fn index_param(cmd: &str, p: &Value) -> Result { - p.get("index").and_then(Value::as_u64).and_then(|n| usize::try_from(n).ok()).ok_or_else(|| bad(cmd, "pass `index` (0-based)")) -} - -/// Apply a Color Sampler change to the active document: view state, so no history step and no -/// pixels. Bumps the revision (keeping a clean document clean) so UIs refresh. -fn change(s: &mut Session, f: impl FnOnce(&mut DocState) -> Result) -> Result { - let st = s.active_mut().ok_or(EngineError::NoDocument)?; - let r = f(st)?; - let clean = st.saved_revision == st.revision; - st.revision += 1; - if clean { - st.saved_revision = st.revision; - } - st.last_damage = Some(photocraft_geom::Rect::EMPTY); - Ok(r) -} - -fn add(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "view.colorSamplers.add"; - let x = coord(CMD, p, "x")?; - let y = coord(CMD, p, "y")?; - change(s, |st| { - let pos = pixel(&st.doc, x, y).ok_or_else(|| EngineError::Other("the sample point is outside the canvas".into()))?; - if st.color_samplers.len() >= MAX_SAMPLERS { - return Err(EngineError::Other(format!("Color Sampler limit reached: at most {MAX_SAMPLERS} points per document"))); - } - st.color_samplers.push(pos); - let i = st.color_samplers.len() - 1; - Ok(json!({"index": i, "count": st.color_samplers.len(), "point": point_json(&st.doc, i, pos)})) - }) -} - -fn move_point(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "view.colorSamplers.move"; - let i = index_param(CMD, p)?; - let x = coord(CMD, p, "x")?; - let y = coord(CMD, p, "y")?; - change(s, |st| { - let n = st.color_samplers.len(); - if i >= n { - return Err(bad(CMD, format!("no sample point {i} (the document has {n})"))); - } - let pos = pixel(&st.doc, x, y).ok_or_else(|| EngineError::Other("the sample point is outside the canvas".into()))?; - st.color_samplers[i] = pos; - Ok(json!({"index": i, "point": point_json(&st.doc, i, pos)})) - }) -} - -fn delete(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "view.colorSamplers.delete"; - let i = index_param(CMD, p)?; - change(s, |st| { - let n = st.color_samplers.len(); - if i >= n { - return Err(bad(CMD, format!("no sample point {i} (the document has {n})"))); - } - st.color_samplers.remove(i); - Ok(json!({"deleted": 1, "remaining": st.color_samplers.len()})) - }) -} - -fn clear(s: &mut Session, _p: &Value) -> Result { - change(s, |st| { - let n = st.color_samplers.len(); - st.color_samplers.clear(); - Ok(json!({"deleted": n, "remaining": 0})) - }) -} - -fn list(s: &mut Session, _p: &Value) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - Ok(list_json(&st.doc, &st.color_samplers)) -} - -pub fn specs() -> Vec { - vec![ - CommandSpec { - id: "view.colorSamplers.add", - label: "Add Color Sampler", - menu: &[], - shortcut: None, - params: r##"{"x":f64,"y":f64} → the new point {index, number, position, rgb, cmyk}; errors at the 10-point limit or off the canvas"##, - enabled: has_doc, - run: add, - journal: false, - }, - CommandSpec { - id: "view.colorSamplers.move", - label: "Move Color Sampler", - menu: &[], - shortcut: None, - params: r##"{"index":n,"x":f64,"y":f64}"##, - enabled: has_doc, - run: move_point, - journal: false, - }, - CommandSpec { - id: "view.colorSamplers.delete", - label: "Delete Color Sampler", - menu: &[], - shortcut: None, - params: r##"{"index":n}"##, - enabled: has_doc, - run: delete, - journal: false, - }, - CommandSpec { - id: "view.colorSamplers.clear", - label: "Clear Color Samplers", - menu: &[], - shortcut: None, - params: "{}", - enabled: has_doc, - run: clear, - journal: false, - }, - CommandSpec { - id: "view.colorSamplers.list", - label: "Color Samplers", - menu: &[], - shortcut: None, - params: r##"{} → {max, samplers:[{index, number, position, rgb, cmyk}]}"##, - enabled: has_doc, - run: list, - journal: false, - }, - ] -} - -#[cfg(test)] -mod tests { - use super::*; - use photocraft_color::{ColorMode, SampleType}; - use photocraft_doc::{Layer, Size}; - use photocraft_geom::Rect; - - /// A 100×80 white document. - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 80, "background": "white"})).unwrap(); - s - } - - fn list(s: &mut Session) -> Value { - s.execute("view.colorSamplers.list", json!({})).unwrap() - } - - fn points(s: &mut Session) -> Vec { - list(s)["samplers"].as_array().cloned().unwrap_or_default() - } - - #[test] - fn add_reads_the_composite_pixel_and_lists_it() { - let mut s = session(); - let r = s.execute("view.colorSamplers.add", json!({"x": 5, "y": 6})).unwrap(); - assert_eq!(r["index"], 0); - assert_eq!(r["count"], 1); - assert_eq!(r["point"]["number"], 1); - assert_eq!(r["point"]["position"], json!([5.0, 6.0])); - assert_eq!(r["point"]["rgb"], json!([255, 255, 255])); - assert_eq!(r["point"]["cmyk"], json!([0, 0, 0, 0])); - let l = list(&mut s); - assert_eq!(l["max"], 10); - assert_eq!(l["samplers"].as_array().unwrap().len(), 1); - assert_eq!(l["samplers"][0]["position"], json!([5.0, 6.0])); - } - - #[test] - fn fractional_clicks_floor_to_the_pixel() { - let mut s = session(); - let r = s.execute("view.colorSamplers.add", json!({"x": 5.9, "y": 6.1})).unwrap(); - assert_eq!(r["point"]["position"], json!([5.0, 6.0])); - } - - #[test] - fn add_reads_an_actual_painted_pixel() { - // A black pixel at (3, 3) of an otherwise white document. - let mut s = session(); - s.edit("paint", |doc, _| { - let surf = doc.layers[0].surface_mut().unwrap(); - let black = photocraft_raster::from_rgba(&surf.format(), [0.0, 0.0, 0.0, 1.0]); - surf.fill_rect(Rect::new(3, 3, 4, 4), &black); - Ok(()) - }) - .unwrap(); - let r = s.execute("view.colorSamplers.add", json!({"x": 3, "y": 3})).unwrap(); - assert_eq!(r["point"]["rgb"], json!([0, 0, 0])); - assert_eq!(r["point"]["cmyk"], json!([0, 0, 0, 100])); - // Painting over the point changes its readout (the list is computed live). - s.edit("paint", |doc, _| { - let surf = doc.layers[0].surface_mut().unwrap(); - let red = photocraft_raster::from_rgba(&surf.format(), [1.0, 0.0, 0.0, 1.0]); - surf.fill_rect(Rect::new(3, 3, 4, 4), &red); - Ok(()) - }) - .unwrap(); - assert_eq!(points(&mut s)[0]["rgb"], json!([255, 0, 0])); - } - - #[test] - fn ten_points_then_the_limit() { - let mut s = session(); - for i in 0..10 { - s.execute("view.colorSamplers.add", json!({"x": i, "y": 0})).unwrap(); - } - assert_eq!(points(&mut s).len(), 10); - let err = s.execute("view.colorSamplers.add", json!({"x": 20, "y": 0})).unwrap_err().to_string(); - assert!(err.contains("limit"), "{err}"); - assert_eq!(points(&mut s).len(), 10); - } - - #[test] - fn move_delete_and_clear() { - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 5, "y": 5})).unwrap(); - s.execute("view.colorSamplers.add", json!({"x": 50, "y": 50})).unwrap(); - let r = s.execute("view.colorSamplers.move", json!({"index": 0, "x": 10, "y": 20})).unwrap(); - assert_eq!(r["point"]["position"], json!([10.0, 20.0])); - assert_eq!(points(&mut s)[0]["position"], json!([10.0, 20.0])); - s.execute("view.colorSamplers.delete", json!({"index": 1})).unwrap(); - assert_eq!(points(&mut s).len(), 1); - // The remaining point is the moved #1. - assert_eq!(points(&mut s)[0]["position"], json!([10.0, 20.0])); - let r = s.execute("view.colorSamplers.clear", json!({})).unwrap(); - assert_eq!(r["deleted"], 1); - assert!(points(&mut s).is_empty()); - } - - #[test] - fn placing_and_moving_add_no_history_and_change_no_pixels() { - let mut s = session(); - let before = s.active().unwrap().doc.layers[0].surface().unwrap().pixel(5, 5); - let past = s.active().unwrap().history.past_len(); - s.execute("view.colorSamplers.add", json!({"x": 5, "y": 5})).unwrap(); - s.execute("view.colorSamplers.move", json!({"index": 0, "x": 9, "y": 9})).unwrap(); - s.execute("view.colorSamplers.delete", json!({"index": 0})).unwrap(); - assert_eq!(s.active().unwrap().history.past_len(), past, "no history steps"); - assert_eq!(s.active().unwrap().doc.layers[0].surface().unwrap().pixel(5, 5), before, "no pixels changed"); - assert!(!s.active().unwrap().is_dirty(), "a clean document stays clean"); - } - - #[test] - fn points_belong_to_their_document() { - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 5, "y": 5})).unwrap(); - s.execute("file.new", json!({"width": 40, "height": 40, "background": "white"})).unwrap(); - assert!(points(&mut s).is_empty(), "the new document has no points"); - s.execute("document.activate", json!({"document": 0})).unwrap(); - assert_eq!(points(&mut s).len(), 1, "the first document kept its point"); - // Closing the second document leaves the first's points alone. - s.execute("file.close", json!({"document": 1})).unwrap(); - assert_eq!(points(&mut s).len(), 1); - // Closing the first discards its points with it. - s.execute("file.close", json!({"document": 0})).unwrap(); - assert!(s.execute("view.colorSamplers.list", json!({})).is_err(), "no document open"); - } - - #[test] - fn a_crop_drops_points_outside_the_new_canvas() { - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 5, "y": 5})).unwrap(); - s.execute("view.colorSamplers.add", json!({"x": 50, "y": 50})).unwrap(); - s.execute("image.crop", json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap(); - let p = points(&mut s); - assert_eq!(p.len(), 1, "the outside point was removed"); - assert_eq!(p[0]["position"], json!([5.0, 5.0])); - assert_eq!(s.take_samplers_removed(), 1, "one removed point reported"); - assert_eq!(s.take_samplers_removed(), 0, "reported once"); - } - - #[test] - fn a_crop_moves_points_with_their_pixels() { - // A crop from a non-zero origin moves the canvas: the point follows its pixel. - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap(); - s.execute("image.crop", json!({"x": 40, "y": 30, "width": 20, "height": 20})).unwrap(); - let p = points(&mut s); - assert_eq!(p.len(), 1); - assert_eq!(p[0]["position"], json!([10.0, 10.0]), "moved by (-40, -30)"); - assert_eq!(s.take_samplers_removed(), 0, "nothing removed"); - } - - #[test] - fn a_canvas_size_shift_moves_points() { - // Canvas Size anchored bottom-right shifts the canvas origin by (+20, +20). - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 30, "y": 30})).unwrap(); - s.execute("image.canvasSize", json!({"width": 120, "height": 100, "anchor": "bottomRight"})).unwrap(); - let p = points(&mut s); - assert_eq!(p.len(), 1); - assert_eq!(p[0]["position"], json!([50.0, 50.0]), "moved by the canvas shift"); - } - - #[test] - fn a_right_angle_rotation_moves_points() { - // 90° clockwise about the centre: the doc centre stays put. - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap(); - s.execute("image.rotation.arbitrary", json!({"angle": 90, "direction": "cw"})).unwrap(); - let p = points(&mut s); - assert_eq!(p.len(), 1); - let pos = p[0]["position"].as_array().unwrap(); - let (x, y) = (pos[0].as_f64().unwrap(), pos[1].as_f64().unwrap()); - // The new canvas is 80×100, so the centre is (40, 50). - assert!((x - 40.0).abs() < 1e-6 && (y - 50.0).abs() < 1e-6, "{pos:?}"); - } - - #[test] - fn a_rotated_crop_moves_points() { - // The crop frame is centred on the point, so the point lands at the new canvas centre. - let mut s = session(); - s.execute("view.colorSamplers.add", json!({"x": 50, "y": 40})).unwrap(); - s.execute("image.crop", json!({"x": 40, "y": 30, "width": 20, "height": 20, "angle": 90})).unwrap(); - let p = points(&mut s); - assert_eq!(p.len(), 1); - assert_eq!(p[0]["position"], json!([10.0, 10.0])); - } - - #[test] - fn bad_params_and_no_document_are_errors_not_panics() { - let mut s = Session::new(); - for id in ["view.colorSamplers.add", "view.colorSamplers.move", "view.colorSamplers.delete", "view.colorSamplers.clear", "view.colorSamplers.list"] { - assert!(s.execute(id, json!({})).is_err(), "{id} without a document"); - } - let mut s = session(); - for (id, p) in [ - ("view.colorSamplers.add", json!({})), - ("view.colorSamplers.add", json!({"x": "a", "y": 1})), - ("view.colorSamplers.add", json!({"x": 1e300, "y": 1})), - ("view.colorSamplers.add", json!({"x": 5, "y": f64::NAN})), - ("view.colorSamplers.add", json!({"x": -1, "y": 5})), - ("view.colorSamplers.add", json!({"x": 5, "y": 1000})), - ("view.colorSamplers.move", json!({"index": 0, "x": 1, "y": 1})), - ("view.colorSamplers.delete", json!({"index": 0})), - ("view.colorSamplers.delete", json!({})), - ("view.colorSamplers.move", json!({"index": 4_294_967_296u64, "x": 1, "y": 1})), - ] { - assert!(matches!(s.execute(id, p.clone()), Err(EngineError::BadParams { .. }) | Err(EngineError::Other(_))), "{id} {p}"); - } - // An off-canvas point is refused and places nothing. - assert!(points(&mut s).is_empty()); - } - - #[test] - fn device_cmyk_matches_the_info_panel_formula() { - assert_eq!(device_cmyk(1.0, 1.0, 1.0), [0, 0, 0, 0]); - assert_eq!(device_cmyk(0.0, 0.0, 0.0), [0, 0, 0, 100]); - assert_eq!(device_cmyk(1.0, 0.0, 0.0), [0, 100, 100, 0]); - assert_eq!(device_cmyk(0.5, 0.5, 0.5), [0, 0, 0, 50]); - } - - /// 32-bit samples can lie outside 0..1 (or be NaN); the CMYK readout clamps them like the RGB - /// readout does instead of reporting K:-50% or C:120% (#4191). - #[test] - fn device_cmyk_clamps_out_of_range_and_nan_samples() { - assert_eq!(device_cmyk(1.5, 1.5, 1.5), [0, 0, 0, 0]); - assert_eq!(device_cmyk(2.0, 0.0, 0.0), [0, 100, 100, 0]); - let in_range = |c: [i32; 4]| c.iter().all(|v| (0..=100).contains(v)); - for (r, g, b) in [(-0.2, 0.5, 0.5), (-1.0, -1.0, -1.0), (f32::NAN, 0.5, 0.5), (f32::NAN, f32::NAN, f32::NAN), (f32::INFINITY, f32::NEG_INFINITY, 0.5)] { - let c = device_cmyk(r, g, b); - assert!(in_range(c), "{r} {g} {b} -> {c:?}"); - } - assert_eq!(device_cmyk(f32::NAN, f32::NAN, f32::NAN), [0, 0, 0, 100]); - } - - #[test] - fn hdr_pixel_on_a_32_bit_document_reads_in_range() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 8, "depth": 32, "background": "white"})).unwrap(); - s.edit("paint", |doc, _| { - let surf = doc.layers[0].surface_mut().unwrap(); - let hdr = photocraft_raster::from_rgba(&surf.format(), [2.0, 2.0, 2.0, 1.0]); - surf.fill_rect(Rect::new(3, 3, 4, 4), &hdr); - Ok(()) - }) - .unwrap(); - let r = s.execute("view.colorSamplers.add", json!({"x": 3, "y": 3})).unwrap(); - assert_eq!(r["point"]["rgb"], json!([255, 255, 255])); - assert_eq!(r["point"]["cmyk"], json!([0, 0, 0, 0])); - } - - #[test] - fn points_work_at_every_depth_and_mode() { - for (mode, depth) in [("rgb", 8), ("rgb", 16), ("rgb", 32), ("grayscale", 8), ("cmyk", 8), ("lab", 8)] { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 20, "height": 20, "mode": mode, "depth": depth, "background": "white"})).unwrap(); - let r = s.execute("view.colorSamplers.add", json!({"x": 10, "y": 10})).unwrap(); - let rgb = r["point"]["rgb"].as_array().unwrap(); - assert_eq!(rgb.len(), 3); - assert!(rgb.iter().all(|v| v.as_i64().is_some_and(|n| (0..=255).contains(&n))), "{mode} {depth}: {rgb:?}"); - } - } - - /// The mode and depth only affect the composite the readout samples, not the point model. - #[test] - fn document_helpers_are_not_disturbed() { - let mut d = Document::new("t", Size::new(4, 4), ColorMode::Rgb, SampleType::U8); - d.layers.push(Layer::raster("Background", d.pixel_format())); - assert_eq!(pixel(&d, 0.0, 0.0), Some([0.0, 0.0])); - assert_eq!(pixel(&d, 3.9, 3.9), Some([3.0, 3.0])); - assert_eq!(pixel(&d, 4.0, 0.0), None); - } -} diff --git a/crates/engine/src/select_mask_edits.rs b/crates/engine/src/select_mask_edits.rs new file mode 100644 index 0000000000..9fd29a2757 --- /dev/null +++ b/crates/engine/src/select_mask_edits.rs @@ -0,0 +1,463 @@ +//! Select and Mask's own selection tools (#7069, #8148): the edits the workspace makes to the +//! selection before it refines the edge, carried as `select.refineEdge`'s `edits` param. +//! +//! Photoshop's Select and Mask has a Quick Selection tool, a Brush, Object Selection, Lasso and +//! Polygonal Lasso, a Select Subject button and Clear Selection / Invert. Here each use is one +//! [`SelectionEdit`], applied in order to the selection (or layer mask) Select and Mask starts +//! from. Keeping them as params rather than live edits of the document means Cancel discards +//! them, OK is one history step, and the UI, CLI, MCP and the control channel all drive them the +//! same way. [`apply`] is pure, so the workspace's preview runs exactly what OK commits. + +use std::collections::BTreeSet; + +use photocraft_algo::matting; +use photocraft_algo::segment::{grabcut, quick, subject}; +use photocraft_algo::selection::{self as sel, Region, SelectionMode, combine_region}; +use photocraft_doc::{Document, LayerId}; +use photocraft_geom::Rect; +use photocraft_raster::Surface; +use serde_json::Value; + +use crate::smartselect_cmds::with_doc_sampler; +use crate::{EngineError, Result}; + +const CMD: &str = "select.refineEdge"; + +/// Most edits one Select and Mask takes, and points across them. +pub const MAX_EDITS: usize = 4096; +pub const MAX_EDIT_POINTS: usize = 100_000; + +/// One use of a Select and Mask tool. `add: false` is the tool's subtract mode (Alt). +#[derive(Clone, Debug, PartialEq)] +pub enum SelectionEdit { + /// Quick Selection: grows a region from the stroke out to the image's edges. + Quick { points: Vec<(f32, f32)>, size: f32, add: bool, sample_all_layers: bool }, + /// Brush: paints the selection directly with a round brush. + Brush { points: Vec<(f32, f32)>, size: f32, hardness: f32, add: bool }, + /// Lasso and Polygonal Lasso: the polygon through `points`. + Lasso { points: Vec<(f32, f32)>, add: bool }, + /// Object Selection: the object in `rect` [x, y, w, h], or the one under `point`. + Object { area: ObjectArea, add: bool, sample_all_layers: bool }, + /// The Select Subject button. + Subject { mode: SelectionMode, sample_all_layers: bool }, + /// Global Refinements › Clear Selection. + Clear, + /// Global Refinements › Invert. + Invert, +} + +/// Where Object Selection looks. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ObjectArea { + Rect(Rect), + Point(f32, f32), +} + +fn bad(msg: &str) -> EngineError { + EngineError::BadParams { cmd: CMD.into(), msg: msg.into() } +} + +/// A finite coordinate within the range strokes may use. +fn coord(v: &Value) -> Option { + v.as_f64().filter(|v| v.is_finite() && v.abs() <= 1e7).map(|v| v as f32) +} + +fn point(v: &Value) -> Option<(f32, f32)> { + let xy = v.as_array().filter(|a| a.len() == 2)?; + Some((coord(xy.first()?)?, coord(xy.get(1)?)?)) +} + +fn points(o: &serde_json::Map, min: usize, total: &mut usize) -> Result> { + let list = o.get("points").and_then(Value::as_array).ok_or_else(|| bad("this edit needs a points array of [x,y] pairs"))?; + if list.len() < min { + return Err(bad(&format!("this edit needs at least {min} point{}", if min == 1 { "" } else { "s" }))); + } + *total = total.saturating_add(list.len()); + if *total > MAX_EDIT_POINTS { + return Err(bad(&format!("edits hold more than {MAX_EDIT_POINTS} points"))); + } + list.iter().map(point).collect::>>().ok_or_else(|| bad("edit points must be [x,y] pairs of finite numbers")) +} + +/// A number in `range`, `default` when absent; anything else is an error naming `key`. +fn number(o: &serde_json::Map, key: &str, default: f32, range: std::ops::RangeInclusive) -> Result { + match o.get(key) { + None => Ok(default), + Some(v) => v + .as_f64() + .map(|v| v as f32) + .filter(|v| v.is_finite() && range.contains(v)) + .ok_or_else(|| bad(&format!("edit {key} must be a number in {}..{}", range.start(), range.end()))), + } +} + +fn flag(o: &serde_json::Map, key: &str, default: bool) -> Result { + match o.get(key) { + None => Ok(default), + Some(v) => v.as_bool().ok_or_else(|| bad(&format!("edit {key} must be true or false"))), + } +} + +/// The tools' `mode`: `add` (the default) or `subtract`. +fn adds(o: &serde_json::Map) -> Result { + match o.get("mode").map(Value::as_str) { + None | Some(Some("add")) => Ok(true), + Some(Some("subtract")) => Ok(false), + Some(_) => Err(bad("edit mode must be add|subtract")), + } +} + +/// The `edits` of `p`, in order (none when absent or null). +pub fn parse(p: &Value) -> Result> { + let Some(v) = p.get("edits").filter(|v| !v.is_null()) else { return Ok(Vec::new()) }; + let list = v.as_array().ok_or_else(|| bad("edits must be an array of {\"tool\":…} objects"))?; + if list.len() > MAX_EDITS { + return Err(bad(&format!("edits holds more than {MAX_EDITS} edits"))); + } + let size_range = matting::BRUSH_SIZE_RANGE; + let mut total = 0usize; + // The area the Brush edits sweep in all, bounded as `refineStrokes`' is. + let mut swept = 0.0f64; + let mut edits = Vec::with_capacity(list.len()); + for e in list { + let o = e.as_object().ok_or_else(|| bad("each edit must be an object with a tool"))?; + let tool = o.get("tool").and_then(Value::as_str).ok_or_else(|| bad("each edit needs a tool: quick|brush|lasso|object|subject|clear|invert"))?; + let edit = match tool { + "quick" => SelectionEdit::Quick { + points: points(o, 1, &mut total)?, + size: number(o, "size", 30.0, size_range.clone())?, + add: adds(o)?, + sample_all_layers: flag(o, "sampleAllLayers", false)?, + }, + "brush" => { + let points = points(o, 1, &mut total)?; + let size = number(o, "size", 30.0, size_range.clone())?; + swept += matting::stroke_area(&points, size); + if swept > matting::MAX_SWEPT_AREA { + return Err(bad("brush edits paint too large an area")); + } + SelectionEdit::Brush { points, size, hardness: number(o, "hardness", 100.0, 0.0..=100.0)?, add: adds(o)? } + } + "lasso" => SelectionEdit::Lasso { points: points(o, 3, &mut total)?, add: adds(o)? }, + "object" => { + let area = match (o.get("rect"), o.get("point")) { + (Some(r), _) => { + let v = r.as_array().filter(|a| a.len() == 4).ok_or_else(|| bad("object rect must be [x,y,w,h]"))?; + let n = v.iter().map(coord).collect::>>().ok_or_else(|| bad("object rect must be four finite numbers"))?; + let (x, y, w, h) = (n.first().copied(), n.get(1).copied(), n.get(2).copied(), n.get(3).copied()); + let (Some(x), Some(y), Some(w), Some(h)) = (x, y, w, h) else { return Err(bad("object rect must be [x,y,w,h]")) }; + // Negative sizes (dragging up or left) are normalised. + let (x0, x1, y0, y1) = (x.min(x + w), x.max(x + w), y.min(y + h), y.max(y + h)); + ObjectArea::Rect(Rect::new(x0.floor() as i32, y0.floor() as i32, x1.ceil() as i32, y1.ceil() as i32)) + } + (None, Some(pt)) => { + let (x, y) = point(pt).ok_or_else(|| bad("object point must be [x,y]"))?; + ObjectArea::Point(x, y) + } + (None, None) => return Err(bad("object needs \"rect\": [x,y,w,h] or \"point\": [x,y]")), + }; + SelectionEdit::Object { area, add: adds(o)?, sample_all_layers: flag(o, "sampleAllLayers", false)? } + } + "subject" => { + let mode = match o.get("mode").map(Value::as_str) { + None | Some(Some("replace")) => SelectionMode::Replace, + Some(Some("add")) => SelectionMode::Add, + Some(Some("subtract")) => SelectionMode::Subtract, + Some(_) => return Err(bad("subject mode must be replace|add|subtract")), + }; + SelectionEdit::Subject { mode, sample_all_layers: flag(o, "sampleAllLayers", true)? } + } + "clear" => SelectionEdit::Clear, + "invert" => SelectionEdit::Invert, + other => return Err(bad(&format!("unknown edit tool `{other}`: quick|brush|lasso|object|subject|clear|invert"))), + }; + edits.push(edit); + } + Ok(edits) +} + +/// Whether `p` carries Select and Mask tool edits. +pub fn has_edits(p: &Value) -> bool { + p.get("edits").is_some_and(|v| !v.is_null() && v.as_array().is_none_or(|a| !a.is_empty())) +} + +fn mode(add: bool) -> SelectionMode { + if add { SelectionMode::Add } else { SelectionMode::Subtract } +} + +/// Most tiles the Brush and Lasso edits may paint in all: four times what one may +/// ([`sel::MAX_PAINTED_TILES`]), 1 GiB of GRAY8 tiles. Each is rasterised a tile at a time, so +/// this, not the canvas size, bounds what they allocate (the #8783 review). +pub const MAX_EDIT_TILES: usize = 4 * sel::MAX_PAINTED_TILES; + +/// Refuses Brush and Lasso edits that would paint too many tiles of `canvas`, one by one or in +/// all, before anything is painted. +pub fn check_size(edits: &[SelectionEdit], canvas: Rect) -> Result<()> { + let mut all = BTreeSet::new(); + for e in edits { + let tiles = match e { + SelectionEdit::Brush { points, size, .. } => matting::painted_tiles(points, *size, canvas, sel::MAX_PAINTED_TILES), + SelectionEdit::Lasso { points, .. } => sel::polygon_edge_tiles(points, canvas, sel::MAX_PAINTED_TILES), + _ => continue, + }; + all.extend(tiles.ok_or_else(|| bad("a brush or lasso edit crosses too many tiles of the canvas"))?); + if all.len() > MAX_EDIT_TILES { + return Err(bad("brush and lasso edits cross too many tiles of the canvas")); + } + } + Ok(()) +} + +/// Refuses Refine Edge Brush strokes that would paint more tiles of `canvas` than +/// [`matting::EdgeBrush`] holds. +pub fn check_strokes(strokes: &[matting::EdgeStroke], canvas: Rect) -> Result<()> { + let refused = || EngineError::BadParams { cmd: CMD.into(), msg: "refineStrokes cross too many tiles of the canvas".into() }; + let mut all = BTreeSet::new(); + for s in strokes { + all.extend(matting::painted_tiles(&s.points, s.size, canvas, sel::MAX_PAINTED_TILES).ok_or_else(refused)?); + if all.len() > sel::MAX_PAINTED_TILES { + return Err(refused()); + } + } + Ok(()) +} + +/// `base` inverted over `canvas`; `None` when that leaves nothing selected. Tile by tile: the +/// canvas takes the inverse of `base`'s untouched value (one tile shared by every fully covered +/// tile, as `fill_rect` stores it), then only the tiles `base` holds are inverted, so neither a +/// sparse selection nor a huge canvas costs a canvas-sized buffer (the #8783 review). +fn invert(base: Option<&Surface>, canvas: Rect) -> Option { + let outside = base.map_or(0.0, |b| b.default_pixel().first().copied().unwrap_or(0.0).clamp(0.0, 1.0)); + let mut s = Surface::new(photocraft_color::PixelFormat::GRAY8); + if outside < 1.0 { + s.fill_rect(canvas, &[1.0 - outside]); + } + if let Some(b) = base { + let ch = b.channels().max(1); + for (tc, _) in b.tiles() { + let r = tc.rect().intersect(&canvas); + if r.is_empty() { + continue; + } + let inverted: Vec = b.read_region(r).chunks_exact(ch).map(|px| 1.0 - px.first().copied().unwrap_or(0.0).clamp(0.0, 1.0)).collect(); + s.write_region(r, &inverted); + } + } + s.prune(); + (!s.content_bounds().is_empty()).then_some(s) +} + +/// One edit applied to `base` (the selection so far; `None` = nothing selected) in `doc`, the +/// sampling tools reading `layer` (or the composite with Sample All Layers). A tool that finds +/// nothing leaves the selection as it was, as the same tools do outside Select and Mask. +pub fn apply_one(doc: &Document, layer: Option, base: Option, edit: &SelectionEdit) -> Option { + let canvas = doc.bounds(); + let combine = |region: Option, m: SelectionMode| match region { + Some(r) => combine_region(base.as_ref(), Some(&r), m), + None => base.clone(), + }; + // A painted shape (tile-sparse) added or subtracted, a tile at a time. + let paint = |shape: Option, add: bool| match (shape.filter(|s| s.tile_count() > 0), base.as_ref()) { + (None, _) => base.clone(), + (Some(s), None) => add.then_some(s), + (Some(s), Some(o)) => { + let out = if add { sel::combine_sparse(o, &s, canvas, f32::max) } else { sel::combine_sparse(o, &s, canvas, |a, b| (a - b).max(0.0)) }; + (!out.content_bounds().is_empty()).then_some(out) + } + }; + match edit { + SelectionEdit::Quick { points, size, add, sample_all_layers } => { + let region = with_doc_sampler(doc, layer, *sample_all_layers, |smp, doc| quick::quick_select(smp, doc.bounds(), points, *size)); + combine(region, mode(*add)) + } + SelectionEdit::Brush { points, size, hardness, add } => paint(matting::brush_surface(points, *size, *hardness, canvas), *add), + SelectionEdit::Lasso { points, add } => paint(sel::polygon_surface(points, canvas), *add), + SelectionEdit::Object { area, add, sample_all_layers } => { + let region = with_doc_sampler(doc, layer, *sample_all_layers, |smp, doc| match *area { + ObjectArea::Rect(r) => grabcut::object_select(smp, doc.bounds(), r, 160_000), + ObjectArea::Point(x, y) => grabcut::object_at(smp, doc.bounds(), x, y, grabcut::OBJECT_AT_WORK_PX), + }); + combine(region, mode(*add)) + } + SelectionEdit::Subject { mode, sample_all_layers } => { + let region = with_doc_sampler(doc, layer, *sample_all_layers, |smp, doc| subject::select_subject(smp, doc.bounds())); + combine(region, *mode) + } + SelectionEdit::Clear => None, + SelectionEdit::Invert => invert(base.as_ref(), canvas), + } +} + +/// `edits` applied in order to `base`: the selection Select and Mask refines. +/// A run of Inverts is one Invert or none (pairs cancel exactly), so thousands of them cost at +/// most one pass each between other edits (the #8783 review). +pub fn apply(doc: &Document, layer: Option, base: Option, edits: &[SelectionEdit]) -> Option { + let mut sel = base; + let mut invert = false; + for e in edits { + if *e == SelectionEdit::Invert { + invert = !invert; + continue; + } + if std::mem::take(&mut invert) { + sel = apply_one(doc, layer, sel, &SelectionEdit::Invert); + } + sel = apply_one(doc, layer, sel, e); + } + if invert { + sel = apply_one(doc, layer, sel, &SelectionEdit::Invert); + } + sel +} + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn parses_every_tool() { + let p = json!({"edits": [ + {"tool": "quick", "points": [[1, 2]], "size": 12, "mode": "subtract", "sampleAllLayers": true}, + {"tool": "brush", "points": [[1, 2], [3, 4]], "hardness": 50}, + {"tool": "lasso", "points": [[0, 0], [10, 0], [0, 10]]}, + {"tool": "object", "rect": [10, 10, -5, -5]}, + {"tool": "object", "point": [3, 4], "mode": "subtract"}, + {"tool": "subject", "mode": "add"}, + {"tool": "clear"}, + {"tool": "invert"}, + ]}); + let edits = parse(&p).expect("valid edits"); + assert_eq!(edits.len(), 8); + assert_eq!(edits[0], SelectionEdit::Quick { points: vec![(1.0, 2.0)], size: 12.0, add: false, sample_all_layers: true }); + assert_eq!(edits[1], SelectionEdit::Brush { points: vec![(1.0, 2.0), (3.0, 4.0)], size: 30.0, hardness: 50.0, add: true }); + assert_eq!(edits[3], SelectionEdit::Object { area: ObjectArea::Rect(Rect::new(5, 5, 10, 10)), add: true, sample_all_layers: false }); + assert_eq!(edits[5], SelectionEdit::Subject { mode: SelectionMode::Add, sample_all_layers: true }); + assert!(parse(&json!({})).expect("none").is_empty()); + assert!(parse(&json!({"edits": null})).expect("none").is_empty()); + assert!(has_edits(&p) && !has_edits(&json!({"edits": []})) && !has_edits(&json!({}))); + // One long brush edit within the area limit is fine; two of them (rejects_hostile_edits) are not. + assert!(parse(&json!({"edits": [{"tool": "brush", "points": [[0, 0], [10_000_000, 0]], "size": 20}]})).is_ok()); + } + + #[test] + fn rejects_hostile_edits() { + let many_points = vec![json!([1, 1]); MAX_EDIT_POINTS + 1]; + let many_edits = vec![json!({"tool": "clear"}); MAX_EDITS + 1]; + for bad in [ + json!("x"), + json!([1]), + json!([{}]), + json!([{"tool": 7}]), + json!([{"tool": "wand"}]), + json!([{"tool": "quick"}]), + json!([{"tool": "quick", "points": []}]), + json!([{"tool": "quick", "points": [[1]]}]), + json!([{"tool": "quick", "points": [["a", 1]]}]), + json!([{"tool": "quick", "points": [[1e300, 1]]}]), + json!([{"tool": "quick", "points": [[1, 1]], "size": 0}]), + json!([{"tool": "quick", "points": [[1, 1]], "size": "big"}]), + json!([{"tool": "quick", "points": [[1, 1]], "mode": "intersect"}]), + json!([{"tool": "quick", "points": [[1, 1]], "sampleAllLayers": 1}]), + json!([{"tool": "brush", "points": [[1, 1]], "hardness": 101}]), + json!([{"tool": "lasso", "points": [[1, 1], [2, 2]]}]), + json!([{"tool": "object"}]), + json!([{"tool": "object", "rect": [1, 2, 3]}]), + json!([{"tool": "object", "point": [1, "y"]}]), + json!([{"tool": "subject", "mode": "intersect"}]), + json!([{"tool": "brush", "points": many_points}]), + json!(many_edits), + // Each within the limit, together past it. + json!(vec![json!({"tool": "brush", "points": [[0, 0], [10_000_000, 0]], "size": 20}); 2]), + ] { + assert!(parse(&json!({"edits": bad})).is_err(), "{bad}"); + } + } + + /// The dense inversion this replaced: the whole canvas read, inverted and written back. + fn dense_invert(base: Option<&Surface>, canvas: Rect) -> Vec { + let m: Vec = sel::mask_from_surface(base, canvas).into_iter().map(|v| 1.0 - v).collect(); + bytes(Some(&sel::mask_to_surface(&m, canvas)), canvas) + } + + fn bytes(s: Option<&Surface>, canvas: Rect) -> Vec { + sel::mask_from_surface(s, canvas).into_iter().map(|v| (v * 255.0).round() as u8).collect() + } + + /// Tile by tile, Invert gives exactly what the dense inversion gave: partial edge tiles, soft + /// values, a selection reaching past the canvas, a mask whose untouched pixels are white, and + /// nothing selected (everything, inverted). + #[test] + fn tiled_invert_matches_the_dense_one() { + let canvas = Rect::new(0, 0, 600, 333); + let mut soft = Surface::new(photocraft_color::PixelFormat::GRAY8); + soft.fill_rect(Rect::new(250, 20, 900, 300), &[1.0]); + soft.fill_rect(Rect::new(10, 10, 40, 30), &[0.4]); + let mut white = Surface::with_default(photocraft_color::PixelFormat::GRAY8, &[1.0]); + white.fill_rect(Rect::new(300, 100, 520, 140), &[0.0]); + for base in [None, Some(soft), Some(white)] { + let got = bytes(invert(base.as_ref(), canvas).as_ref(), canvas); + assert_eq!(got, dense_invert(base.as_ref(), canvas)); + } + // Inverting everything selects nothing. + let mut all = Surface::new(photocraft_color::PixelFormat::GRAY8); + all.fill_rect(canvas, &[1.0]); + assert!(invert(Some(&all), canvas).is_none()); + } + + /// A huge canvas inverts without a canvas-sized buffer, and a run of Inverts costs one. + #[test] + fn invert_on_a_huge_canvas_and_runs_of_inverts() { + let doc = Document::new("huge", photocraft_doc::Size::new(100_000, 100_000), photocraft_doc::ColorMode::Rgb, photocraft_color::SampleType::U8); + let mut small = Surface::new(photocraft_color::PixelFormat::GRAY8); + small.fill_rect(Rect::new(10, 10, 20, 20), &[1.0]); + let t = std::time::Instant::now(); + let one = apply(&doc, None, Some(small.clone()), &[SelectionEdit::Invert]).expect("almost everything selected"); + assert!(one.sample_channel(15, 15, 0) < 0.01 && one.sample_channel(99_999, 99_999, 0) > 0.99 && one.sample_channel(50_000, 5, 0) > 0.99); + let odd = apply(&doc, None, Some(small.clone()), &vec![SelectionEdit::Invert; 4095]).expect("inverted"); + assert!(odd.sample_channel(15, 15, 0) < 0.01 && odd.sample_channel(99_999, 99_999, 0) > 0.99); + let even = apply(&doc, None, Some(small.clone()), &vec![SelectionEdit::Invert; 4096]).expect("the selection itself"); + assert!(even.sample_channel(15, 15, 0) > 0.99 && even.sample_channel(50_000, 50_000, 0) < 0.01); + // Runs split by another edit invert on each side of it. + let split = [SelectionEdit::Invert, SelectionEdit::Invert, SelectionEdit::Invert, SelectionEdit::Clear, SelectionEdit::Invert]; + let all = apply(&doc, None, Some(small), &split).expect("cleared, then everything"); + assert!(all.sample_channel(15, 15, 0) > 0.99 && all.sample_channel(70_000, 3, 0) > 0.99); + assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed()); + } + + /// The #8783 review: a size-1 Brush edit from corner to corner of a 100,000 px canvas sweeps + /// about 141,000 px but its bounding box is 10 billion; it, a Lasso sliver along it and their + /// subtraction paint only the tiles they cross, and keep the selection there was. + #[test] + fn thin_edits_across_a_huge_canvas_paint_only_their_tiles() { + let doc = Document::new("huge", photocraft_doc::Size::new(100_000, 100_000), photocraft_doc::ColorMode::Rgb, photocraft_color::SampleType::U8); + let canvas = doc.bounds(); + let mut small = Surface::new(photocraft_color::PixelFormat::GRAY8); + small.fill_rect(Rect::new(10, 60, 20, 70), &[1.0]); + let diagonal = vec![(0.0, 0.0), (99_999.0, 99_999.0)]; + let brush = SelectionEdit::Brush { points: diagonal.clone(), size: 1.0, hardness: 100.0, add: true }; + let lasso = SelectionEdit::Lasso { points: vec![(0.0, 0.0), (99_999.0, 99_999.0), (0.0, 1.5)], add: true }; + assert!(check_size(&[brush.clone(), lasso.clone()], canvas).is_ok()); + let t = std::time::Instant::now(); + for edit in [&brush, &lasso] { + let s = apply(&doc, None, Some(small.clone()), std::slice::from_ref(edit)).expect("painted"); + assert!(s.tile_count() < 1500, "{}", s.tile_count()); + assert!(s.sample_channel(15, 65, 0) > 0.99 && s.sample_channel(50_000, 50_000, 0) > 0.0 && s.sample_channel(50_000, 40_000, 0) == 0.0); + } + let erased = + apply(&doc, None, Some(small.clone()), &[brush, SelectionEdit::Brush { points: diagonal, size: 9.0, hardness: 100.0, add: false }]).expect("kept"); + assert!(erased.sample_channel(15, 65, 0) > 0.99 && erased.sample_channel(50_000, 50_000, 0) == 0.0); + assert!(t.elapsed() < std::time::Duration::from_secs(20), "{:?}", t.elapsed()); + // A zigzag across more tiles than one edit may paint is refused before painting. + let zigzag: Vec<(f32, f32)> = (0..20).map(|i| (if i % 2 == 0 { 0.0 } else { 99_999.0 }, i as f32 * 5000.0)).collect(); + let over = [SelectionEdit::Brush { points: zigzag.clone(), size: 1.0, hardness: 100.0, add: true }]; + assert!(check_size(&over, canvas).is_err()); + assert!(check_size(&[SelectionEdit::Lasso { points: zigzag, add: true }], canvas).is_err()); + // So are many edits that each fit but cross too many tiles in all. + let rows: Vec = (0..48) + .map(|i| SelectionEdit::Brush { points: vec![(0.0, i as f32 * 2000.0), (99_999.0, i as f32 * 2000.0)], size: 1.0, hardness: 100.0, add: true }) + .collect(); + assert!(rows.iter().all(|e| check_size(std::slice::from_ref(e), canvas).is_ok())); + assert!(check_size(&rows, canvas).is_err()); + } +} diff --git a/crates/engine/src/selection_cmds.rs b/crates/engine/src/selection_cmds.rs deleted file mode 100644 index d6fc50aeea..0000000000 --- a/crates/engine/src/selection_cmds.rs +++ /dev/null @@ -1,1441 +0,0 @@ -//! Select menu and selection tools: Magic Wand, Color Range, Modify -//! (expand/contract/border/smooth/feather), Grow, Similar, Lasso. -//! Selections are grayscale coverage surfaces clipped to the canvas. - -use photocraft_algo::selection::{self as sel, SelectionMode}; -use photocraft_doc::Document; -use photocraft_geom::Rect; -use photocraft_raster::Surface; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} -fn has_selection(s: &Session) -> std::result::Result<(), String> { - s.active().filter(|d| d.doc.selection.is_some()).map(|_| ()).ok_or_else(|| "no selection".into()) -} - -fn f(p: &Value, k: &str, d: f32) -> f32 { - p.get(k).and_then(Value::as_f64).map_or(d, |v| v as f32) -} -fn b(p: &Value, k: &str, d: bool) -> bool { - p.get(k).and_then(Value::as_bool).unwrap_or(d) -} -fn mode(p: &Value) -> SelectionMode { - SelectionMode::parse(p.get("mode").and_then(Value::as_str).unwrap_or("replace")) -} - -/// A Color Range replacement is scoped to the selection that was active when it opened. -/// Explicit add/subtract/intersect operations retain their normal selection-combine semantics. -fn scope_color_range(mask: &mut [f32], doc: &Document, area: Rect, m: SelectionMode) { - if m != SelectionMode::Replace { - return; - } - if let Some(selection) = doc.selection.as_ref() { - let previous = sel::mask_from_surface(Some(selection), area); - for (value, bound) in mask.iter_mut().zip(previous) { - *value = value.min(bound); - } - } -} - -/// RGBA pixels the tools sample: the composite, or the active layer. -fn sample_pixels(s: &Session, all_layers: bool) -> Result<(Rect, Vec<[f32; 4]>)> { - let d = s.active().ok_or(EngineError::NoDocument)?; - let area = d.doc.bounds(); - // Match the decoders' raster budget before either the layer or composite path allocates. - let len = area.size().area(); - let max_bytes: u64 = if cfg!(target_pointer_width = "64") { 8 << 30 } else { 2 << 30 }; - if len > max_bytes / std::mem::size_of::<[f32; 4]>() as u64 { - return Err(EngineError::Other("the canvas is too large to sample for a selection".into())); - } - let layer = d.active_layer.and_then(|id| d.doc.layer(id)).and_then(|l| l.surface()); - match (all_layers, layer) { - (false, Some(surf)) => { - let len = usize::try_from(len).map_err(|_| EngineError::Other("the canvas is too large to sample for a selection".into()))?; - let mut px = crate::allocation::filled(len, [0.0f32; 4], "sampling pixels for a selection")?; - surf.read_rgba_into(area, &mut px); - Ok((area, px)) - } - _ => Ok((area, photocraft_compose::render(&d.doc, area).px)), - } -} - -fn set_selection(s: &mut Session, label: &str, area: Rect, mask: Vec, m: SelectionMode) -> Result { - let selected = s.edit(label, |doc, _| { - doc.selection = sel::combine(doc.selection.as_ref(), &mask, area, m); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "selected": selected })) -} - -fn current_mask(doc: &Document) -> (Rect, Vec) { - let area = doc.bounds(); - (area, sel::mask_from_surface(doc.selection.as_ref(), area)) -} - -/// Grayscale the canvas is actually showing, as opaque RGBA, when that is not the colour -/// composite: a gray layer-mask view, or — with every colour channel hidden — the first -/// visible alpha, Quick Mask, or the active layer's mask. The Magic Wand selects from this -/// so a white alpha stays one region instead of breaking up along the RGB underneath. -fn shown_gray(s: &Session) -> Option<(Rect, Vec<[u8; 4]>)> { - let st = s.active()?; - let doc = &st.doc; - let area = doc.bounds(); - let v = &st.channel_view; - let colors_off = v.visible_colors(crate::channel_cmds::color_count(doc)) == 0; - let surf = if let Some((mask, crate::mask_view_cmds::MaskViewMode::Gray)) = v.shown_layer_mask(doc) { - &mask.surface - } else if colors_off { - if let Some(i) = (0..doc.channels.len()).find(|i| v.alpha_shown(*i)) { - &doc.channels[i].surface - } else if let Some(q) = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden) { - &q.surface - } else if let Some((mask, _)) = v.shown_layer_mask(doc) { - &mask.surface - } else { - &st.active_layer.and_then(|id| doc.layer(id))?.mask.as_ref()?.surface - } - } else { - return None; - }; - Some((area, sel::rgba8_image(surf, area))) -} - -/// 8-bit RGBA of the active layer (or the composite with `all_layers`) over the canvas. -pub(crate) fn sample_rgba8(s: &Session, all_layers: bool) -> Result<(Rect, Vec<[u8; 4]>)> { - let d = s.active().ok_or(EngineError::NoDocument)?; - let area = d.doc.bounds(); - let layer = d.active_layer.and_then(|id| d.doc.layer(id)).and_then(|l| l.surface()); - match (all_layers, layer) { - (false, Some(surf)) => Ok((area, sel::rgba8_image(surf, area))), - _ => { - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - Ok((area, photocraft_compose::render(&d.doc, area).px.iter().map(|p| p.map(q)).collect())) - } - } -} - -fn magic_wand(s: &mut Session, p: &Value) -> Result { - let (x, y) = (f(p, "x", 0.0).floor() as i32, f(p, "y", 0.0).floor() as i32); - let (area, img) = match shown_gray(s) { - Some(plane) => plane, - None => sample_rgba8(s, b(p, "sampleAllLayers", false))?, - }; - let region = sel::wand_region(&img, area, (x, y), f(p, "tolerance", 32.0), b(p, "contiguous", true), b(p, "antiAlias", true)); - drop(img); - let m = mode(p); - let selected = s.edit("Magic Wand", |doc, _| { - doc.selection = sel::combine_region(doc.selection.as_ref(), region.as_ref(), m); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "selected": selected })) -} - -fn parse_color(p: &Value) -> [f32; 3] { - match p.get("color") { - Some(Value::Array(a)) if a.len() >= 3 => [0, 1, 2].map(|i| a[i].as_f64().unwrap_or(0.0) as f32), - Some(Value::String(h)) => { - let h = h.trim_start_matches('#'); - let c = |i: usize| h.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map_or(0.0, |v| f32::from(v) / 255.0); - [c(0), c(2), c(4)] - } - _ => [0.0; 3], - } -} - -/// Eyedropper samples from `key` (`[[x, y], …]` in document pixels): the colour under each point -/// and where it was picked (area-local, for Localized Color Clusters). -fn eyedropper_samples(p: &Value, key: &str, area: Rect, px: &[[f32; 4]], bad: &impl Fn(String) -> EngineError) -> Result> { - let Some(pts) = p.get(key) else { return Ok(Vec::new()) }; - let pts = pts.as_array().ok_or_else(|| bad(format!("`{key}` must be [[x, y], …]")))?; - let w = area.width() as usize; - let mut out = Vec::with_capacity(pts.len().min(4096)); - for pt in pts { - let xy = pt.as_array().and_then(|a| match a.as_slice() { - [x, y] => Some((x.as_f64()?, y.as_f64()?)), - _ => None, - }); - let Some((x, y)) = xy.filter(|(x, y)| x.is_finite() && y.is_finite()) else { - return Err(bad(format!("bad point {pt} in `{key}` (want [x, y])"))); - }; - let (xi, yi) = (x.floor() as i32, y.floor() as i32); - let q = if area.contains(xi, yi) { px.get((yi - area.y0) as usize * w + (xi - area.x0) as usize) } else { None }; - let Some(&[r, g, b, _]) = q else { - return Err(bad(format!("point [{x}, {y}] in `{key}` is outside the canvas"))); - }; - out.push(sel::RangeSample { color: [r, g, b], at: Some(((xi - area.x0) as f32, (yi - area.y0) as f32)) }); - } - Ok(out) -} - -/// Composite RGB (the compositor's output profile for the document) → CIE Lab D50. -fn lab_transform(s: &Session) -> Result> { - let d = s.active().ok_or(EngineError::NoDocument)?; - let src = crate::color_cmds::composite_profile(&d.doc); - photocraft_cms::cached(&src, photocraft_cms::Builtin::LabD50.profile(), photocraft_cms::TransformOptions::default()) - .map_err(|e| EngineError::Other(format!("colour management: {e}"))) -} - -/// The Lab profile's normalised encoding (ICC v4) → L 0–100, a and b −128–127. -fn decode_lab(v: [f32; 3]) -> [f32; 3] { - [v[0] * 100.0, v[1] * 255.0 - 128.0, v[2] * 255.0 - 128.0] -} - -fn colour_to_lab(t: &photocraft_cms::Transform, rgb: [f32; 3]) -> [f32; 3] { - let mut out = [0.0f32; 3]; - t.eval(&rgb.map(|v| v.clamp(0.0, 1.0)), &mut out); - decode_lab(out) -} - -/// RGBA pixels → Lab + alpha (L, a, b, alpha). -fn pixels_to_lab(t: &photocraft_cms::Transform, px: &[[f32; 4]]) -> Vec<[f32; 4]> { - let src: Vec<[f32; 4]> = px.iter().map(|p| [p[0].clamp(0.0, 1.0), p[1].clamp(0.0, 1.0), p[2].clamp(0.0, 1.0), p[3]]).collect(); - let mut out = vec![[0.0f32; 4]; px.len()]; - t.convert_f32(src.as_flattened(), 4, out.as_flattened_mut(), 4, true); - for p in &mut out { - let [l, a, b] = decode_lab([p[0], p[1], p[2]]); - *p = [l, a, b, p[3]]; - } - out -} - -/// Prepared Sampled Colors query shared by the command and its interactive preview. -/// Samples always come from the original document; reducing an image must not change them. -pub struct ColorRangeSamples { - to_lab: std::sync::Arc, - range: sel::LabRange, - grid: bool, - fuzziness: f32, - localized: Option, - plus_at: Vec<(f32, f32)>, -} - -impl ColorRangeSamples { - /// Prepare the query without allocating a full-resolution composite. Point samples use - /// exactly the same compositor/profile as the final selection. - pub fn new(s: &Session, p: &Value) -> Result { - let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; - let area = doc.bounds(); - let bad = |msg: String| EngineError::BadParams { cmd: "select.colorRange".into(), msg }; - let sample = |key: &str| -> Result> { - let Some(pts) = p.get(key) else { return Ok(Vec::new()) }; - let pts = pts.as_array().ok_or_else(|| bad(format!("`{key}` must be [[x, y], …]")))?; - let mut samples = Vec::new(); - for point in pts { - let q = point.as_array().and_then(|v| match v.as_slice() { - [x, y] => Some((x.as_f64()?, y.as_f64()?)), - _ => None, - }); - let Some((x, y)) = q.filter(|(x, y)| x.is_finite() && y.is_finite()) else { - return Err(bad(format!("bad point {point} in `{key}` (want [x, y])"))); - }; - let (x, y) = (x.floor() as i32, y.floor() as i32); - if !area.contains(x, y) { - return Err(bad(format!("point [{x}, {y}] in `{key}` is outside the canvas"))); - } - let r = Rect::from_xywh(x, y, 1, 1); - let pixel = if !b(p, "sampleAllLayers", true) { - s.active().and_then(|d| d.active_layer.and_then(|id| d.doc.layer(id))).and_then(|l| l.surface()).map(|surf| { - let mut px = [[0.0; 4]]; - surf.read_rgba_into(r, &mut px); - px[0] - }) - } else { - None - }; - let px = match pixel { - Some(px) => px, - None => photocraft_compose::render(doc, r).px.first().copied().unwrap_or([0.0; 4]), - }; - samples.push(sel::RangeSample { color: [px[0], px[1], px[2]], at: Some(((x - area.x0) as f32, (y - area.y0) as f32)) }); - } - Ok(samples) - }; - Self::with_samples(s, p, area, &sample("points")?, &sample("subtractPoints")?) - } - - fn with_samples(s: &Session, p: &Value, area: Rect, plus: &[sel::RangeSample], minus: &[sel::RangeSample]) -> Result { - let bad = |msg: String| EngineError::BadParams { cmd: "select.colorRange".into(), msg }; - let fuzz = |d: f32| f(p, "fuzziness", d).max(0.0); - // The order the eyedropper clicks came in ("+" a point, "-" a subtracted point): - // Photoshop applies them one after another. Without it, all additions come first. - let order: Vec = match p.get("order") { - None => std::iter::repeat_n(true, plus.len()).chain(std::iter::repeat_n(false, minus.len())).collect(), - Some(o) => { - let o = o.as_array().ok_or_else(|| bad("`order` must be a list of \"+\" and \"-\"".into()))?; - let order = o - .iter() - .map(|v| match v.as_str() { - Some("+") => Ok(true), - Some("-") => Ok(false), - _ => Err(bad(format!("bad entry {v} in `order` (want \"+\" or \"-\")"))), - }) - .collect::>>()?; - if order.iter().filter(|x| **x).count() != plus.len() || order.iter().filter(|x| !**x).count() != minus.len() { - return Err(bad("`order` must list each of `points` (\"+\") and `subtractPoints` (\"-\") once".into())); - } - order - } - }; - let mut fixed: Vec<[f32; 3]> = Vec::new(); - if let Some(cs) = p.get("colors") { - let cs = cs.as_array().ok_or_else(|| bad("`colors` must be a list of colours".into()))?; - fixed.extend(cs.iter().map(|c| parse_color(&json!({ "color": c })))); - } - if p.get("color").is_some() { - fixed.push(parse_color(p)); - } - // Localized Color Clusters: Range is a percentage of the canvas's longer side. - let plus_at: Vec<(f32, f32)> = plus.iter().filter_map(|x| x.at).collect(); - let localized = if b(p, "localized", false) { - if plus_at.is_empty() { - return Err(bad("localized clusters need eyedropper `points`".into())); - } - Some(f(p, "range", 100.0).clamp(0.0, 100.0) / 100.0 * area.width().max(area.height()) as f32) - } else { - None - }; - // Photoshop compares colours in Lab, through the document's profile - // (sel::lab_range_coverage). The samples span one Lab box; "Add to Sample" grows it, - // "Subtract from Sample" reshapes it (sel::LabRange::subtract). - let to_lab = lab_transform(s)?; - // 8-bit documents are compared on the 8-bit Lab grid, samples and pixels alike. - let grid = s.active().is_some_and(|d| d.doc.depth == photocraft_doc::SampleType::U8); - let q = |c: [f32; 3]| if grid { sel::quantize_lab8(c) } else { c }; - let colour_to_lab = |t: &photocraft_cms::Transform, c: [f32; 3]| q(colour_to_lab(t, c)); - let foreground = || { - let [r, g, b, _] = s.tools.foreground; - sel::LabRange::point(colour_to_lab(&to_lab, [r, g, b])) - }; - let fixed_lab: Vec<[f32; 3]> = fixed.iter().map(|c| colour_to_lab(&to_lab, *c)).collect(); - let mut range = sel::LabRange::spanning(&fixed_lab); - let (mut pi, mut mi) = (plus.iter(), minus.iter()); - for add in order { - if add { - if let Some(x) = pi.next() { - let c = colour_to_lab(&to_lab, x.color); - range = Some(range.map_or(sel::LabRange::point(c), |r| r.grow(&c))); - } - } else if let Some(x) = mi.next() { - range = Some(range.unwrap_or_else(foreground).subtract(&colour_to_lab(&to_lab, x.color), fuzz(40.0))); - } - } - let range = range.unwrap_or_else(foreground); - Ok(Self { to_lab, range, grid, fuzziness: fuzz(40.0), localized, plus_at }) - } - - /// Coverage of a composite rectangle. `scale` is the original document pixels represented - /// by one input pixel; `origin` is in original document pixels (for a visible canvas crop). - pub fn coverage(&self, px: &[[f32; 4]], width: usize, scale: f32, origin: [f32; 2]) -> Vec { - let mut lab = pixels_to_lab(&self.to_lab, px); - if self.grid { - for p in &mut lab { - let [l, a, b] = sel::quantize_lab8([p[0], p[1], p[2]]); - *p = [l, a, b, p[3]]; - } - } - let scale = if scale.is_finite() && scale > 0.0 { scale } else { 1.0 }; - let points: Vec<_> = self.plus_at.iter().map(|(x, y)| ((x - origin[0]) / scale, (y - origin[1]) / scale)).collect(); - sel::color_range_lab(&lab, width, &self.range, self.fuzziness, self.grid, self.localized.map(|r| (r / scale, points.as_slice()))) - } -} - -fn color_range(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "select.colorRange"; - let bad = |msg: String| EngineError::BadParams { cmd: CMD.into(), msg }; - let preset = p.get("select").map_or(Some("sampledColors"), Value::as_str).ok_or_else(|| bad("`select` must be a string".into()))?; - let invert = b(p, "invert", false); - let selection_mode = mode(p); - let fuzz = |d: f32| f(p, "fuzziness", d).max(0.0); - // Photoshop's Detect Faces needs a face detector, which there is none of yet (#4455). - if b(p, "detectFaces", false) { - return Err(bad("detectFaces: face detection isn't available yet; select without it".into())); - } - let (area, mut mask) = match preset { - "sampledColors" => { - let (area, px) = sample_pixels(s, b(p, "sampleAllLayers", true))?; - let w = area.width() as usize; - // The dialog's eyedroppers: colours picked on the image, at their positions. - // Like Photoshop's, they may sample anywhere on the canvas: an active selection only - // limits the result (scope_color_range), not where colours are picked (#5244). - let plus = eyedropper_samples(p, "points", area, &px, &bad)?; - let minus = eyedropper_samples(p, "subtractPoints", area, &px, &bad)?; - let query = ColorRangeSamples::with_samples(s, p, area, &plus, &minus)?; - let mask = query.coverage(&px, w, 1.0, [0.0, 0.0]); - (area, mask) - } - "reds" | "yellows" | "greens" | "cyans" | "blues" | "magentas" => { - let center = match preset { - "reds" => 0.0, - "yellows" => 60.0, - "greens" => 120.0, - "cyans" => 180.0, - "blues" => 240.0, - _ => 300.0, - }; - let (area, px) = sample_pixels(s, b(p, "sampleAllLayers", true))?; - (area, sel::hue_range(&px, center)) - } - "skinTones" => { - let (area, px) = sample_pixels(s, b(p, "sampleAllLayers", true))?; - (area, sel::skin_tones(&px, fuzz(40.0).min(200.0))) - } - "highlights" | "midtones" | "shadows" => { - // Range in levels (Highlights / Shadows: one value, Midtones: [low, high]); Fuzziness - // is a percentage of the tonal scale. - let level = |v: &Value| v.as_f64().filter(|x| (0.0..=255.0).contains(x)).map(|x| x as f32); - let r = p.get("tonalRange"); - let (lo, hi) = match (preset, r) { - ("shadows", None) => (0.0, 65.0), - ("highlights", None) => (190.0, 255.0), - (_, None) => (105.0, 150.0), - ("shadows", Some(v)) => (0.0, level(v).ok_or_else(|| bad(format!("tonalRange: want a level 0..255 (got {v})")))?), - ("highlights", Some(v)) => (level(v).ok_or_else(|| bad(format!("tonalRange: want a level 0..255 (got {v})")))?, 255.0), - (_, Some(v)) => v - .as_array() - .and_then(|a| match a.as_slice() { - [l, h] => Some((level(l)?, level(h)?)), - _ => None, - }) - .filter(|(l, h)| l <= h) - .ok_or_else(|| bad(format!("tonalRange: want [low, high] levels 0..255 (got {v})")))?, - }; - let (area, px) = sample_pixels(s, b(p, "sampleAllLayers", true))?; - (area, sel::tone_range(&px, lo, hi, fuzz(20.0).min(100.0) / 100.0 * 255.0)) - } - "outOfGamut" => { - // Pixels the current proof setup (View › Proof Setup) can't reproduce, as the gamut - // warning shows them: judged on the composite. - let d = s.active().ok_or(EngineError::NoDocument)?; - let pv = s.color.proof(d.doc.id); - let (m, _) = crate::color_cmds::gamut_mask(&d.doc, &pv.setup, pv.gamut_threshold)?; - (d.doc.bounds(), m.iter().map(|v| f32::from(*v) / 255.0).collect()) - } - other => { - return Err(bad(format!( - "unknown select `{other}` (sampledColors, reds, yellows, greens, cyans, blues, magentas, highlights, midtones, shadows, skinTones, outOfGamut)" - ))); - } - }; - if invert { - for v in &mut mask { - *v = 1.0 - *v; - } - } - // Invert the colour result first, then constrain it to the original selection. - // Add mode must still be able to extend an existing selection intentionally. - scope_color_range(&mut mask, &s.active().ok_or(EngineError::NoDocument)?.doc, area, selection_mode); - set_selection(s, "Color Range", area, mask, selection_mode) -} - -fn modify(s: &mut Session, p: &Value, op: &str) -> Result { - // The dialogs' ranges (Photoshop's limits); anything else is refused, never clamped silently. - let (min, max): (f32, f32) = match op { - "border" => (1.0, 200.0), - "feather" => (0.1, 1000.0), - _ => (1.0, 500.0), - }; - let r = f(p, "radius", 1.0); - // Below the dialog's minimum (a negative or zero radius, #993) or above its maximum is - // refused: a clamp to 0 left the selection alone (Expand, Contract) or cleared it (Border) - // while reporting success. - if !(min..=max).contains(&r) { - return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: format!("radius must be a number in {min}..{max}") }); - } - // Photoshop's "Apply effect at canvas bounds": when on, the canvas edge is a selection edge - // (Select All then Contract shrinks from the edges); when off, the selection is taken to - // continue past the canvas. Border has no such option: its band always follows the canvas edge, - // or a Select All would border to nothing. - let at_bounds = op == "border" || b(p, "applyAtCanvasBounds", false); - let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; - let area = doc.bounds(); - if area.size().area() > MAX_MODIFY_PIXELS as u64 { - return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() }); - } - let (_, m) = current_mask(doc); - let (w, h) = (area.width() as usize, area.height() as usize); - // Feather reaches 3σ = 3 r (σ = r, as in Photoshop); the others reach r. A margin past that - // keeps the canvas edge out of reach. A selection clear of the canvas edge needs none: both - // readings agree there. - let reach = if op == "feather" { r * 3.0 } else { r }; - let pad = if touches_edge(&m, w, h) { reach.ceil() as usize + 2 } else { 0 }; - let grown = |n: usize| pad.checked_mul(2).and_then(|p| n.checked_add(p)); - let (Some(pw), Some(ph)) = (grown(w), grown(h)) else { - return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() }); - }; - if pw.checked_mul(ph).is_none_or(|n| n > MAX_MODIFY_PIXELS) { - return Err(EngineError::BadParams { cmd: format!("select.modify.{op}"), msg: "the selection is too large to modify".into() }); - } - // Unpadded (the usual case) the mask is used as it is: two serial full-canvas copies were - // most of a 24 MP Feather or Smooth (#211). - let padded = if pad == 0 { m } else { pad_mask(&m, w, h, pad, at_bounds) }; - let out = match op { - "expand" => sel::expand(&padded, pw, ph, r), - "contract" => sel::contract(&padded, pw, ph, r), - "border" => sel::border(&padded, pw, ph, r), - "smooth" => sel::smooth(&padded, pw, ph, r), - _ => sel::feather(&padded, pw, ph, r), - }; - let out = if pad == 0 { out } else { crop_mask(&out, pw, pad, w, h) }; - let label = match op { - "expand" => "Expand Selection", - "contract" => "Contract Selection", - "border" => "Border Selection", - "smooth" => "Smooth Selection", - _ => "Feather Selection", - }; - set_selection(s, label, area, out, SelectionMode::Replace) -} - -/// The largest padded mask Modify works on (a 300000 × 300000 canvas is 9e10 pixels; this caps a -/// padded copy at ~4 GB of f32, beyond which the edit is refused rather than aborting on allocation). -const MAX_MODIFY_PIXELS: usize = 1 << 30; - -/// Whether any pixel on the outermost rows or columns of `m` (`w`×`h`) is selected. -fn touches_edge(m: &[f32], w: usize, h: usize) -> bool { - let sel = |x: usize, y: usize| m.get(y * w + x).is_some_and(|v| *v > 0.0); - (0..w).any(|x| sel(x, 0) || sel(x, h.saturating_sub(1))) || (0..h).any(|y| sel(0, y) || sel(w.saturating_sub(1), y)) -} - -/// `m` (`w`×`h`) with a `pad`-pixel margin: unselected when `outside_empty`, else the nearest -/// edge pixel repeated (the selection continues past the canvas). -fn pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> Vec { - let pw = w + 2 * pad; - let mut out = vec![0.0f32; pw * (h + 2 * pad)]; - if w == 0 || h == 0 { - return out; - } - // Row by row (a copy and two fills), not sample by sample (#211). - for (py, row) in out.chunks_exact_mut(pw).enumerate() { - if outside_empty && !(pad..pad + h).contains(&py) { - continue; - } - let y = py.saturating_sub(pad).min(h - 1); - let (Some(src), Some((left, rest))) = (m.get(y * w..(y + 1) * w), row.split_at_mut_checked(pad)) else { continue }; - let Some((mid, right)) = rest.split_at_mut_checked(w) else { continue }; - mid.copy_from_slice(src); - if !outside_empty { - left.fill(src.first().copied().unwrap_or(0.0)); - right.fill(src.last().copied().unwrap_or(0.0)); - } - } - out -} - -/// The `w`×`h` centre of a mask padded by `pad_mask`. -fn crop_mask(m: &[f32], pw: usize, pad: usize, w: usize, h: usize) -> Vec { - let mut out = Vec::with_capacity(w.saturating_mul(h)); - for row in m.chunks_exact(pw.max(1)).skip(pad).take(h) { - out.extend_from_slice(row.get(pad..pad + w).unwrap_or_default()); - } - out -} - -/// Grow (contiguous) / Similar (anywhere): pixels whose colour lies within -/// the selected pixels' colour range widened by the tolerance. -fn grow_similar(s: &mut Session, p: &Value, contiguous: bool) -> Result { - let tol = f(p, "tolerance", 32.0) / 255.0; - let (area, px) = sample_pixels(s, b(p, "sampleAllLayers", false))?; - let (_, m) = current_mask(&s.active().ok_or(EngineError::NoDocument)?.doc); - let (mut lo, mut hi) = ([f32::MAX; 4], [f32::MIN; 4]); - for (q, v) in px.iter().zip(&m) { - if *v >= 0.5 { - for c in 0..4 { - lo[c] = lo[c].min(q[c]); - hi[c] = hi[c].max(q[c]); - } - } - } - if lo[0] > hi[0] { - return Err(EngineError::Other("selection is empty".into())); - } - let inside = |q: &[f32; 4]| (0..4).all(|c| q[c] >= lo[c] - tol - 1e-6 && q[c] <= hi[c] + tol + 1e-6); - let (w, h) = (area.width() as usize, area.height() as usize); - let mut out: Vec = m.iter().map(|v| if *v >= 0.5 { 1.0 } else { 0.0 }).collect(); - if contiguous { - let mut stack: Vec = (0..out.len()).filter(|i| out[*i] > 0.0).collect(); - while let Some(i) = stack.pop() { - let (x, y) = (i % w, i / w); - let n = [(x > 0).then(|| i - 1), (x + 1 < w).then(|| i + 1), (y > 0).then(|| i - w), (y + 1 < h).then(|| i + w)]; - for j in n.into_iter().flatten() { - if out[j] == 0.0 && inside(&px[j]) { - out[j] = 1.0; - stack.push(j); - } - } - } - } else { - for (o, q) in out.iter_mut().zip(&px) { - if inside(q) { - *o = 1.0; - } - } - } - set_selection(s, if contiguous { "Grow" } else { "Similar" }, area, out, SelectionMode::Replace) -} - -fn lasso(s: &mut Session, p: &Value) -> Result { - let pts: Vec<(f32, f32)> = p - .get("points") - .and_then(Value::as_array) - .map(|a| a.iter().filter_map(|q| Some((q.get(0)?.as_f64()? as f32, q.get(1)?.as_f64()? as f32))).collect()) - .unwrap_or_default(); - if pts.len() < 3 { - return Err(EngineError::BadParams { cmd: "select.lasso".into(), msg: "needs at least 3 points".into() }); - } - polygon_selection(s, "select.lasso", "Lasso", &pts, p) -} - -/// Selects the polygon `pts` with the lasso tools' `mode`, `antiAlias` and `feather` params, as -/// one history step called `label`. -pub(crate) fn polygon_selection(s: &mut Session, cmd: &str, label: &str, pts: &[(f32, f32)], p: &Value) -> Result { - let feather = f(p, "feather", 0.0); - // Select › Modify › Feather's range. - if !(0.0..=1000.0).contains(&feather) { - return Err(EngineError::BadParams { cmd: cmd.into(), msg: "feather must be a number in 0..1000".into() }); - } - let area = s.active().ok_or(EngineError::NoDocument)?.doc.bounds(); - if feather > 0.0 { - // The blur spreads past the polygon and isn't bit-identical over a smaller window. - let mut mask = sel::polygon(pts, area, b(p, "antiAlias", true)); - mask = sel::feather(&mask, area.width() as usize, area.height() as usize, feather); - return set_selection(s, label, area, mask, mode(p)); - } - // Only the rows the polygon spans (#4532): rows outside it are empty, and full-width rows - // keep `polygon`'s coverage bit-identical (its x arithmetic is relative to the area's left - // edge; its y samples are whole rows plus the same sub-row fractions). - let band = polygon_rows(pts, area); - let mask = sel::polygon(pts, band, b(p, "antiAlias", true)); - let m = mode(p); - let selected = s.edit(label, |doc, _| { - doc.selection = combine_band(doc.selection.as_ref(), &mask, band, m); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "selected": selected })) -} - -/// The full-width rows of `area` that points `pts` span (empty when none are finite). -fn polygon_rows(pts: &[(f32, f32)], area: Rect) -> Rect { - let ys = pts.iter().map(|p| p.1).filter(|y| y.is_finite()); - let (lo, hi) = ys.fold((f32::INFINITY, f32::NEG_INFINITY), |(lo, hi), y| (lo.min(y), hi.max(y))); - if lo > hi { - return Rect::EMPTY; - } - let clamp = |v: f32| v.clamp(area.y0 as f32, area.y1 as f32) as i32; - let (y0, y1) = (clamp(lo.floor()), clamp(hi.ceil() + 1.0)); - if y0 >= y1 { Rect::EMPTY } else { Rect::new(area.x0, y0, area.x1, y1) } -} - -/// [`sel::combine`] of a mask covering only `band` (zero elsewhere), giving exactly what the -/// whole-canvas combine gives: Replace and Intersect are empty outside the band, and Add and -/// Subtract keep the old selection there. -fn combine_band(old: Option<&Surface>, mask: &[f32], band: Rect, m: SelectionMode) -> Option { - let inside = sel::combine(old, mask, band, m); - let Some(old) = old.filter(|_| matches!(m, SelectionMode::Add | SelectionMode::Subtract)) else { return inside }; - if band.is_empty() { - return Some(old.clone()); - } - let values = inside.map_or_else(|| vec![0.0; band.width() as usize * band.height() as usize], |w| w.read_region(band)); - let mut s = old.clone(); - s.write_region(band, &values); - s.prune(); - (s.tile_count() > 0).then_some(s) -} - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $params:literal, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: None, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -/// Selection command specs. -pub fn specs() -> Vec { - vec![ - spec!( - "select.magicWand", - "Magic Wand", - [], - r##"{"x":px,"y":px,"tolerance":0..255=32,"contiguous":bool=true,"antiAlias":bool=true,"sampleAllLayers":bool=false,"mode":"replace|add|subtract|intersect"="replace"} (samples the visible alpha, Quick Mask, or layer mask when that is the picture)"##, - has_doc, - magic_wand - ), - spec!( - "select.colorRange", - "Color Range…", - ["Select"], - r##"{"select":"sampledColors|reds|yellows|greens|cyans|blues|magentas|highlights|midtones|shadows|skinTones|outOfGamut"="sampledColors","color":"#rrggbb"=foreground,"colors":["#rrggbb",…]?,"points":[[x,y],…]? (eyedropper samples),"subtractPoints":[[x,y],…]? (minus eyedropper),"order":["+"|"-",…]? (click order of points and subtractPoints; default all points first),"fuzziness":0..200=40 (tones: 0..100 %=20),"detectFaces":bool=false (not available yet: refused),"localized":bool=false,"range":0..100=100 (% of the longer side),"tonalRange":level|[lo,hi] (shadows 65, highlights 190, midtones [105,150]),"invert":bool=false,"sampleAllLayers":bool=true,"mode":"replace|add|subtract|intersect"="replace"}"##, - has_doc, - color_range - ), - spec!("select.modify.border", "Border…", ["Select", "Modify"], r##"{"radius":1..200=1}"##, has_selection, |s, p| modify(s, p, "border")), - spec!("select.modify.smooth", "Smooth…", ["Select", "Modify"], r##"{"radius":1..500=1,"applyAtCanvasBounds":bool=false}"##, has_selection, |s, p| { - modify(s, p, "smooth") - }), - spec!("select.modify.expand", "Expand…", ["Select", "Modify"], r##"{"radius":1..500=1,"applyAtCanvasBounds":bool=false}"##, has_selection, |s, p| { - modify(s, p, "expand") - }), - spec!( - "select.modify.contract", - "Contract…", - ["Select", "Modify"], - r##"{"radius":1..500=1,"applyAtCanvasBounds":bool=false}"##, - has_selection, - |s, p| modify(s, p, "contract") - ), - spec!( - "select.modify.feather", - "Feather…", - ["Select", "Modify"], - r##"{"radius":0.1..1000=1,"applyAtCanvasBounds":bool=false}"##, - has_selection, - |s, p| modify(s, p, "feather") - ), - spec!("select.grow", "Grow", ["Select"], r##"{"tolerance":0..255=32,"sampleAllLayers":bool=false}"##, has_selection, |s, p| grow_similar(s, p, true)), - spec!("select.similar", "Similar", ["Select"], r##"{"tolerance":0..255=32,"sampleAllLayers":bool=false}"##, has_selection, |s, p| grow_similar( - s, p, false - )), - spec!( - "select.lasso", - "Lasso", - [], - r##"{"points":[[x,y],…],"mode":"replace|add|subtract|intersect"="replace","antiAlias":bool=true,"feather":px=0}"##, - has_doc, - lasso - ), - ] -} - -#[cfg(test)] -mod tests { - use super::*; - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - bg.fill_rect(Rect::new(5, 5, 15, 15), &[1.0, 0.0, 0.0, 1.0]); - bg.fill_rect(Rect::new(25, 5, 35, 15), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - fn coverage(s: &Session, x: i32, y: i32) -> f32 { - s.active().unwrap().doc.selection.as_ref().map_or(0.0, |m| m.sample_channel(x, y, 0)) - } - - fn clear_selection(s: &mut Session) { - if s.active().unwrap().doc.selection.is_some() { - s.execute("select.deselect", json!({})).unwrap(); - } - } - - #[test] - fn color_range_skin_tones_selects_skin_not_cream_and_refuses_detect_faces() { - let mut s = session(); - let rgb = |c: [u8; 3]| [f32::from(c[0]) / 255.0, f32::from(c[1]) / 255.0, f32::from(c[2]) / 255.0, 1.0]; - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - bg.fill_rect(Rect::new(0, 0, 40, 30), &rgb([252, 253, 247])); - bg.fill_rect(Rect::new(5, 5, 15, 15), &rgb([183, 119, 81])); - bg.fill_rect(Rect::new(25, 5, 35, 15), &rgb([231, 225, 202])); - Ok(()) - }) - .unwrap(); - s.execute("select.colorRange", json!({"select": "skinTones"})).unwrap(); - assert!(coverage(&s, 10, 10) > 0.95, "skin"); - assert!(coverage(&s, 30, 10) < 0.05, "cream"); - assert!(coverage(&s, 2, 2) < 0.05, "near-white background"); - // Subtracting skin from a selection of both squares keeps only the cream one. - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 30, "height": 10})).unwrap(); - s.execute("select.colorRange", json!({"select": "skinTones", "mode": "subtract"})).unwrap(); - assert!(coverage(&s, 10, 10) < 0.05 && coverage(&s, 30, 10) > 0.95, "{} {}", coverage(&s, 10, 10), coverage(&s, 30, 10)); - let err = s.execute("select.colorRange", json!({"select": "skinTones", "detectFaces": true})).unwrap_err(); - assert!(err.to_string().contains("detectFaces"), "{err}"); - } - - #[test] - fn selection_sampling_refuses_oversized_canvases_before_reading_pixels() { - let mut s = Session::new(); - s.add_document( - photocraft_doc::Document::new( - "large", - photocraft_geom::Size::new(300_000, 300_000), - photocraft_color::ColorMode::Rgb, - photocraft_color::SampleType::U8, - ), - None, - ); - for all_layers in [false, true] { - assert!(sample_pixels(&s, all_layers).unwrap_err().to_string().contains("too large to sample")); - } - assert!(!s.active().unwrap().history.can_undo()); - } - - #[test] - fn selection_sampling_allocation_failure_keeps_the_selection() { - let mut s = session(); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 4, "height": 4})).unwrap(); - let before = s.active().unwrap().doc.clone(); - let history = s.active().unwrap().history.entries(); - crate::allocation::fail_next_for_test(); - let result = s.execute("select.similar", json!({"sampleAllLayers": false})); - let _ = crate::allocation::capacity::(0, "reset failpoint"); - assert!(result.is_err()); - assert_eq!(*s.active().unwrap().doc, *before); - assert_eq!(s.active().unwrap().history.entries(), history); - } - - #[test] - fn modify_rejects_oversized_canvases_before_reading_the_mask() { - let mut s = Session::new(); - let mut doc = photocraft_doc::Document::new( - "large", - photocraft_geom::Size::new(300_000, 300_000), - photocraft_color::ColorMode::Rgb, - photocraft_color::SampleType::U8, - ); - let mut selection = photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8); - selection.write_pixel(0, 0, &[1.0]); - doc.selection = Some(selection); - s.add_document(doc, None); - let before = s.active().unwrap().doc.clone(); - for op in ["expand", "contract", "border", "smooth", "feather"] { - let error = s.execute(&format!("select.modify.{op}"), json!({"radius": 1})).unwrap_err(); - assert!(error.to_string().contains("too large to modify")); - assert_eq!(*s.active().unwrap().doc, *before); - assert!(!s.active().unwrap().history.can_undo()); - } - } - - #[test] - fn color_range_replace_is_bounded_by_existing_selection() { - for params in [json!({"color": "#ff0000", "fuzziness": 0}), json!({"select": "reds"})] { - let mut s = session(); - // Both red squares match, but only the left is inside the original selection. - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); - s.execute("select.colorRange", params).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 30, 10), 0.0); - assert_eq!(coverage(&s, 2, 2), 0.0); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 30, 10), 0.0); - } - } - - #[test] - fn color_range_invert_and_partial_masks_stay_within_prior_selection() { - let mut s = session(); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); - s.execute("select.colorRange", json!({"select": "highlights", "invert": true})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0, "inverted white excludes the white background"); - assert_eq!(coverage(&s, 20, 20), 0.0, "invert must not select outside the old boundary"); - - let mut s = session(); - s.edit("soft selection", |doc, _| { - let mut mask = vec![0.0; 40 * 30]; - mask[10 * 40 + 10] = 128.0 / 255.0; - doc.selection = Some(sel::mask_to_surface(&mask, doc.bounds())); - Ok(()) - }) - .unwrap(); - s.execute("select.colorRange", json!({"select": "reds"})).unwrap(); - assert!((coverage(&s, 10, 10) - 128.0 / 255.0).abs() < 0.001); - assert_eq!(coverage(&s, 30, 10), 0.0); - } - - #[test] - fn color_range_eyedropper_samples_anywhere_but_the_result_stays_in_the_selection() { - // #5244: as in Photoshop, a colour picked outside the active selection is accepted; only - // the result is limited to the selection. - let mut s = session(); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 30})).unwrap(); - let outside = json!({"points": [[30, 10]], "fuzziness": 0}); - assert!(ColorRangeSamples::new(&s, &outside).is_ok()); - s.execute("select.colorRange", outside).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0, "the red inside the selection is selected"); - assert_eq!(coverage(&s, 30, 10), 0.0, "the sampled red outside it stays out"); - assert_eq!(coverage(&s, 2, 2), 0.0, "white is not the sampled colour"); - } - - #[test] - fn color_range_eyedropper_respects_selection_but_add_can_extend_it() { - let mut s = session(); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 10, "height": 10})).unwrap(); - s.execute("select.colorRange", json!({"points": [[10, 10]], "fuzziness": 0})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 30, 10), 0.0); - - // Explicitly adding a matching colour is still allowed to grow the selection. - s.execute("select.colorRange", json!({"points": [[30, 10]], "fuzziness": 0, "mode": "add"})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 30, 10), 1.0); - - clear_selection(&mut s); - s.execute("select.colorRange", json!({"color": "#ff0000", "fuzziness": 0})).unwrap(); - assert_eq!(coverage(&s, 30, 10), 1.0, "an unbounded color range still searches the canvas"); - } - - #[test] - fn magic_wand_selects_the_visible_alpha_or_mask_not_the_rgb() { - let mut s = session(); - // Alpha covers the first red square and the pixels beside it. RGB would stop at the red. - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); - s.execute("channel.new", json!({"fill": "selection", "name": "Alpha"})).unwrap(); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 7, 7), 1.0); - assert_eq!(coverage(&s, 2, 2), 1.0, "the white alpha includes pixels that are not the red square"); - assert_eq!(coverage(&s, 30, 10), 0.0, "outside the alpha stays unselected"); - // Colour channels back on: the wand reads RGB again. - s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap(); - s.execute("channel.setVisible", json!({"channel": 0, "visible": false})).unwrap(); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 7, 7), 1.0); - assert_eq!(coverage(&s, 2, 2), 0.0, "RGB sampling stops at the red square"); - // Eyes off again, Alpha still the picture. - s.execute("channel.setVisible", json!({"channel": 0, "visible": true})).unwrap(); - s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 1.0); - - let mut s = session(); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); - s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 1.0, "gray mask view selects the mask"); - assert_eq!(coverage(&s, 30, 10), 0.0); - s.execute("view.layerMask", json!({"mode": "off"})).unwrap(); - for name in ["red", "green", "blue"] { - s.execute("channel.setVisible", json!({"channel": name, "visible": false})).unwrap(); - } - s.execute("select.magicWand", json!({"x": 7, "y": 7, "tolerance": 0, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 1.0, "a layer mask shown alone is what the wand reads"); - assert_eq!(coverage(&s, 30, 10), 0.0); - } - - #[test] - fn magic_wand_contiguous_and_modes() { - let mut s = session(); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 30, 10), 0.0); - s.execute("select.magicWand", json!({"x": 30, "y": 7, "mode": "add", "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 30, 10), 1.0); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "mode": "subtract", "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 0.0); - s.execute("select.magicWand", json!({"x": 7, "y": 7, "contiguous": false, "antiAlias": false})).unwrap(); - assert_eq!(coverage(&s, 30, 10), 1.0); - } - - #[test] - fn color_range_prepared_query_matches_the_command_at_all_depths() { - use photocraft_doc::{Color, ColorMode, SampleType, Size}; - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - for profile in [None, Some(photocraft_cms::Builtin::DisplayP3.profile().to_bytes())] { - let mut doc = Document::with_background("samples", Size::new(24, 12), ColorMode::Rgb, depth, Color::WHITE); - doc.icc_profile = profile; - let layer = doc.layers[0].surface_mut().unwrap(); - layer.fill_rect(Rect::new(0, 0, 8, 12), &[0.65, 0.3, 0.2, 1.0]); - layer.fill_rect(Rect::new(8, 0, 16, 12), &[0.2, 0.3, 0.65, 1.0]); - let mut s = Session::new(); - s.add_document(doc, None); - for p in [ - json!({"points": [[2, 3]], "fuzziness": 0}), - json!({"points": [[2, 3], [10, 3]], "subtractPoints": [[10, 3]], "order": ["+", "+", "-"], "fuzziness": 40}), - json!({"points": [[2, 3]], "localized": true, "range": 50, "fuzziness": 80}), - ] { - clear_selection(&mut s); - let q = ColorRangeSamples::new(&s, &p).unwrap(); - let doc = s.active().unwrap().doc.clone(); - let pixels = photocraft_compose::render(&doc, doc.bounds()); - let preview = q.coverage(&pixels.px, 24, 1.0, [0.0, 0.0]); - s.execute("select.colorRange", p).unwrap(); - let actual = current_mask(&s.active().unwrap().doc).1; - for (shown, selected) in preview.iter().zip(actual) { - let rounded = (shown.clamp(0.0, 1.0) * 255.0 + 0.5).floor() / 255.0; - assert_eq!(rounded, selected, "{depth:?}: original-pixel preview and OK differ"); - } - } - } - } - } - - #[test] - fn color_range_prepared_query_samples_visible_layers_not_the_active_layer() { - use photocraft_doc::{Layer, LayerContent}; - let mut s = session_colours(); - let doc = s.active().unwrap().doc.clone(); - let mut top = Layer::new("blue foreground", LayerContent::Raster(doc.layers[0].surface().unwrap().clone())); - top.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 40, 30), &[0.0, 0.0, 1.0, 1.0]); - let mut hidden = Layer::new("hidden green", LayerContent::Raster(top.surface().unwrap().clone())); - hidden.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 40, 30), &[0.0, 1.0, 0.0, 1.0]); - hidden.visible = false; - s.edit("layers", |doc, _| { - doc.layers.push(top); - doc.layers.push(hidden); - Ok(()) - }) - .unwrap(); - for all_layers in [false, true] { - clear_selection(&mut s); - let p = json!({"points": [[7, 7]], "sampleAllLayers": all_layers, "fuzziness": 0}); - let query = ColorRangeSamples::new(&s, &p).unwrap(); - let (area, px) = sample_pixels(&s, all_layers).unwrap(); - let shown = query.coverage(&px, area.width() as usize, 1.0, [0.0, 0.0]); - s.execute("select.colorRange", p).unwrap(); - assert_eq!(shown[7 * 40 + 7], coverage(&s, 7, 7)); - assert_eq!(shown[24 * 40 + 10], coverage(&s, 10, 24)); - } - for p in [json!({"points": [[-1, 0]]}), json!({"points": [[40, 1]]}), json!({"points": "wrong"}), json!({"points": [[null, 0]]})] { - assert!(ColorRangeSamples::new(&s, &p).is_err(), "{p}"); - } - } - - #[test] - fn color_range_selects_by_colour() { - let mut s = session(); - s.execute("select.colorRange", json!({"color": "#ff0000", "fuzziness": 40})).unwrap(); - // The sampled colour itself is fully selected (as with Photoshop's eyedropper). - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 20, 20), 0.0); - s.execute("select.colorRange", json!({"color": "#ff0000", "fuzziness": 100})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert_eq!(coverage(&s, 20, 20), 0.0); - } - - #[test] - fn color_range_compares_in_lab_like_photoshop() { - // Photoshop weighs hue (Lab a/b) three times as much as lightness; a per-channel RGB - // distance can't tell a lighter shade from a redder one. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 30, "height": 10})).unwrap(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - let v = |c: u8| f32::from(c) / 255.0; - bg.fill_rect(Rect::new(0, 0, 10, 10), &[v(200), v(140), v(120), 1.0]); // skin - bg.fill_rect(Rect::new(10, 0, 20, 10), &[v(220), v(160), v(140), 1.0]); // lighter skin - bg.fill_rect(Rect::new(20, 0, 30, 10), &[v(220), v(140), v(120), 1.0]); // redder skin - Ok(()) - }) - .unwrap(); - s.execute("select.colorRange", json!({"points": [[5, 5]], "fuzziness": 40})).unwrap(); - assert!(coverage(&s, 5, 5) > 0.98); - // Both differ from the sample by 20 levels in one RGB channel, yet the lighter one is - // kept clearly more (about 0.55 vs 0.31). - let (lighter, redder) = (coverage(&s, 15, 5), coverage(&s, 25, 5)); - assert!(lighter > redder + 0.15 && redder > 0.0, "lighter {lighter}, redder {redder}"); - } - - #[test] - fn color_range_fuzziness_zero_selects_exact_match_only() { - // Sampled Colors at Fuzziness 0 selects only the exact match (TOOL-214-3, #214). - let mut s = Session::new(); - s.execute("file.new", json!({"width": 50, "height": 10})).unwrap(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - let v = |c: u8| f32::from(c) / 255.0; - // Exact target color [200, 140, 120] - bg.fill_rect(Rect::new(0, 0, 10, 10), &[v(200), v(140), v(120), 1.0]); - // Off by 1 level in red [201, 140, 120] - bg.fill_rect(Rect::new(10, 0, 20, 10), &[v(201), v(140), v(120), 1.0]); - // Off by 1 level in green [200, 141, 120] - bg.fill_rect(Rect::new(20, 0, 30, 10), &[v(200), v(141), v(120), 1.0]); - // Off by 2 levels in blue [200, 140, 122] (crosses 8-bit Lab grid threshold) - bg.fill_rect(Rect::new(30, 0, 40, 10), &[v(200), v(140), v(122), 1.0]); - // Exact target color again at a different position - bg.fill_rect(Rect::new(40, 0, 50, 10), &[v(200), v(140), v(120), 1.0]); - Ok(()) - }) - .unwrap(); - - // Sampled via eyedropper point at [5, 5] with Fuzziness 0 - s.execute("select.colorRange", json!({"points": [[5, 5]], "fuzziness": 0})).unwrap(); - assert_eq!(coverage(&s, 5, 5), 1.0, "sampled pixel itself is fully selected"); - assert_eq!(coverage(&s, 45, 5), 1.0, "identical color elsewhere on canvas is fully selected"); - assert_eq!(coverage(&s, 15, 5), 0.0, "1 level off in red is unselected at fuzziness 0"); - assert_eq!(coverage(&s, 25, 5), 0.0, "1 level off in green is unselected at fuzziness 0"); - assert_eq!(coverage(&s, 35, 5), 0.0, "1 level off in blue is unselected at fuzziness 0"); - - // Sampled via hex color with Fuzziness 0 - s.execute("select.colorRange", json!({"color": "#c88c78", "fuzziness": 0})).unwrap(); - assert_eq!(coverage(&s, 5, 5), 1.0, "exact color match from hex is fully selected"); - assert_eq!(coverage(&s, 45, 5), 1.0, "identical color elsewhere on canvas is fully selected"); - assert_eq!(coverage(&s, 15, 5), 0.0, "1 level off in red is unselected for hex at fuzziness 0"); - assert_eq!(coverage(&s, 25, 5), 0.0, "1 level off in green is unselected for hex at fuzziness 0"); - assert_eq!(coverage(&s, 35, 5), 0.0, "1 level off in blue is unselected for hex at fuzziness 0"); - } - - /// Red squares (from `session`), a blue square, a black and a mid-grey strip on white. - fn session_colours() -> Session { - let mut s = session(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - bg.fill_rect(Rect::new(5, 20, 15, 28), &[0.0, 0.0, 1.0, 1.0]); - bg.fill_rect(Rect::new(20, 20, 25, 28), &[0.0, 0.0, 0.0, 1.0]); - bg.fill_rect(Rect::new(30, 20, 35, 28), &[0.5, 0.5, 0.5, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - #[test] - fn color_range_several_samples_points_and_invert() { - let mut s = session_colours(); - // Several samples span one Lab box (Photoshop's Minimum/Maximum): red and blue take - // both squares, black, grey and white stay out. - s.execute("select.colorRange", json!({"colors": ["#ff0000", "#0000ff"], "fuzziness": 100})).unwrap(); - assert_eq!((coverage(&s, 10, 10), coverage(&s, 30, 10), coverage(&s, 10, 24)), (1.0, 1.0, 1.0)); - assert_eq!((coverage(&s, 2, 2), coverage(&s, 22, 24), coverage(&s, 32, 24)), (0.0, 0.0, 0.0)); - // Eyedropper point on the blue square; `invert` flips the result. - clear_selection(&mut s); - s.execute("select.colorRange", json!({"points": [[7, 22]], "fuzziness": 100, "invert": true})).unwrap(); - assert_eq!((coverage(&s, 10, 24), coverage(&s, 10, 10), coverage(&s, 2, 2)), (0.0, 1.0, 1.0)); - // Localized clusters: picked on the left red square, a 25% range (10 px) leaves the right one. - clear_selection(&mut s); - s.execute("select.colorRange", json!({"points": [[10, 10]], "fuzziness": 100, "localized": true, "range": 25})).unwrap(); - assert_eq!(coverage(&s, 10, 10), 1.0); - assert!(coverage(&s, 13, 10) > 0.5); - assert_eq!(coverage(&s, 30, 10), 0.0); - // Undo restores the empty selection that preceded the most recent Color Range. - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 0.0); - } - - #[test] - fn color_range_subtract_follows_photoshop_and_the_click_order() { - // Grey strips from dark to light; samples at both ends span the whole run. - let mut s = Session::new(); - s.execute("file.new", json!({"width": 50, "height": 10})).unwrap(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - for (i, v) in [0.30f32, 0.40, 0.50, 0.60, 0.70].iter().enumerate() { - bg.fill_rect(Rect::new(i as i32 * 10, 0, i as i32 * 10 + 10, 10), &[*v, *v, *v, 1.0]); - } - Ok(()) - }) - .unwrap(); - let run = |s: &mut Session, extra: Value| { - clear_selection(s); - let mut p = json!({"points": [[5, 5], [45, 5]], "fuzziness": 10}); - p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone()); - s.execute("select.colorRange", p).unwrap(); - (0..5).map(|i| coverage(s, i * 10 + 5, 5)).collect::>() - }; - assert!(run(&mut s, json!({})).iter().all(|v| *v > 0.99), "both samples span every strip"); - // Subtracting a colour inside the range moves the nearer edge onto it: the strips beyond - // it drop out, the subtracted grey itself stays selected (Photoshop does the same). - let cut = run(&mut s, json!({"subtractPoints": [[15, 5]]})); - assert!(cut[0] < 0.5 && cut[1] > 0.99 && cut[4] > 0.99, "{cut:?}"); - // Order matters: adding the dark strip back after the subtraction restores it. - let readd = run(&mut s, json!({"points": [[5, 5], [45, 5], [5, 5]], "subtractPoints": [[15, 5]], "order": ["+", "+", "-", "+"]})); - assert!(readd[0] > 0.99, "{readd:?}"); - let sub_last = run(&mut s, json!({"points": [[5, 5], [45, 5], [5, 5]], "subtractPoints": [[15, 5]], "order": ["+", "+", "+", "-"]})); - assert!(sub_last[0] < 0.5, "{sub_last:?}"); - // A malformed or inconsistent order is an error, never a panic. - clear_selection(&mut s); - for bad in [json!(["+"]), json!(["+", "+", "x"]), json!("+-"), json!(["-", "-", "+"])] { - assert!(s.execute("select.colorRange", json!({"points": [[5, 5], [45, 5]], "subtractPoints": [[15, 5]], "order": bad})).is_err(), "{bad}"); - } - } - - #[test] - fn color_range_presets() { - let mut s = session_colours(); - s.execute("select.colorRange", json!({"select": "reds"})).unwrap(); - assert_eq!((coverage(&s, 10, 10), coverage(&s, 30, 10)), (1.0, 1.0)); - assert_eq!((coverage(&s, 10, 24), coverage(&s, 2, 2), coverage(&s, 32, 24)), (0.0, 0.0, 0.0)); - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "blues"})).unwrap(); - assert_eq!((coverage(&s, 10, 24), coverage(&s, 10, 10)), (1.0, 0.0)); - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "greens"})).unwrap(); - assert!(s.active().unwrap().doc.selection.is_none() || coverage(&s, 10, 10) == 0.0); - // Tones: black is a shadow, white a highlight, 50% grey (128) a midtone. - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "shadows"})).unwrap(); - assert_eq!((coverage(&s, 22, 24), coverage(&s, 2, 2), coverage(&s, 32, 24)), (1.0, 0.0, 0.0)); - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "highlights"})).unwrap(); - assert_eq!((coverage(&s, 22, 24), coverage(&s, 2, 2)), (0.0, 1.0)); - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "midtones", "fuzziness": 0})).unwrap(); - assert_eq!((coverage(&s, 32, 24), coverage(&s, 2, 2), coverage(&s, 22, 24)), (1.0, 0.0, 0.0)); - // A wider shadow range takes the grey too. - clear_selection(&mut s); - s.execute("select.colorRange", json!({"select": "shadows", "tonalRange": 140, "fuzziness": 0})).unwrap(); - assert_eq!(coverage(&s, 32, 24), 1.0); - // Modes combine as for the other selection commands. - s.execute("select.colorRange", json!({"select": "reds", "mode": "add"})).unwrap(); - assert_eq!((coverage(&s, 32, 24), coverage(&s, 10, 10)), (1.0, 1.0)); - } - - #[test] - fn color_range_out_of_gamut_matches_the_gamut_warning() { - let mut s = session(); - s.edit("paint", |doc, _| { - // Saturated sRGB blue is outside the default CMYK proof; mid grey is inside. - let bg = doc.layers[0].surface_mut().unwrap(); - bg.fill_rect(Rect::new(0, 20, 20, 30), &[0.0, 0.0, 1.0, 1.0]); - bg.fill_rect(Rect::new(20, 20, 40, 30), &[0.5, 0.5, 0.5, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("select.colorRange", json!({"select": "outOfGamut"})).unwrap(); - assert_eq!((coverage(&s, 5, 25), coverage(&s, 30, 25)), (1.0, 0.0)); - let warn = s.execute("view.gamutWarning", json!({"on": true})).unwrap(); - let sel = s.active().unwrap().doc.selection.clone().unwrap(); - let mut n = 0; - for y in 0..30 { - for x in 0..40 { - n += usize::from(sel.sample_channel(x, y, 0) > 0.5); - } - } - assert_eq!(warn["outOfGamut"].as_u64(), Some(n as u64)); - } - - #[test] - fn color_range_rejects_bad_params() { - let mut s = session_colours(); - for p in [ - json!({"select": "skin"}), - json!({"select": 3}), - json!({"points": [[100, 2]]}), - json!({"points": [[1]]}), - json!({"points": "here"}), - json!({"colors": "#ff0000"}), - json!({"localized": true}), - json!({"select": "midtones", "tonalRange": [200, 100]}), - json!({"select": "shadows", "tonalRange": 300}), - ] { - assert!(s.execute("select.colorRange", p.clone()).is_err(), "{p}"); - } - assert!(s.active().unwrap().doc.selection.is_none()); - } - - #[test] - fn modify_commands() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("select.modify.expand", json!({"radius": 2})).unwrap(); - assert_eq!(coverage(&s, 8, 15), 1.0); - s.execute("select.modify.contract", json!({"radius": 4})).unwrap(); - assert_eq!(coverage(&s, 11, 15), 0.0); - assert_eq!(coverage(&s, 15, 15), 1.0); - s.execute("select.modify.border", json!({"radius": 2})).unwrap(); - assert_eq!(coverage(&s, 15, 15), 0.0); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("select.modify.feather", json!({"radius": 4})).unwrap(); - let e = coverage(&s, 10, 15); - assert!(e > 0.2 && e < 0.8, "{e}"); - s.execute("select.modify.smooth", json!({"radius": 2})).unwrap(); - assert!(s.active().unwrap().doc.selection.is_some()); - assert!(!s.is_enabled("select.nothing")); - // Out-of-range or non-finite radii are refused and leave the selection alone. - for op in ["feather", "smooth", "expand", "contract", "border"] { - for r in [json!(1e300), json!(5000), json!(f64::MAX), json!(-5), json!(-0.5), json!(f64::MIN)] { - assert!(s.execute(&format!("select.modify.{op}"), json!({"radius": r})).is_err(), "{op} {r}"); - } - } - assert!(s.active().unwrap().doc.selection.is_some()); - } - - #[test] - fn modify_select_all_honours_canvas_bounds() { - let all = || { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); - s.execute("select.all", json!({})).unwrap(); - s - }; - // Off (Photoshop's default): the selection continues past the canvas, so the edges stay. - for op in ["contract", "smooth", "expand", "feather"] { - let mut s = all(); - s.execute(&format!("select.modify.{op}"), json!({"radius": 4})).unwrap(); - assert_eq!(coverage(&s, 0, 15), 1.0, "{op}"); - assert_eq!(coverage(&s, 20, 0), 1.0, "{op}"); - } - // On: the canvas edge is a selection edge (#1277). - let mut s = all(); - s.execute("select.modify.contract", json!({"radius": 4, "applyAtCanvasBounds": true})).unwrap(); - assert_eq!(coverage(&s, 2, 15), 0.0); - assert_eq!(coverage(&s, 20, 28), 0.0); - assert_eq!(coverage(&s, 20, 15), 1.0); - let mut s = all(); - s.execute("select.modify.feather", json!({"radius": 4, "applyAtCanvasBounds": true})).unwrap(); - let e = coverage(&s, 0, 15); - assert!(e > 0.2 && e < 0.8, "{e}"); - assert_eq!(coverage(&s, 20, 15), 1.0); - let mut s = all(); - s.execute("select.modify.smooth", json!({"radius": 4, "applyAtCanvasBounds": true})).unwrap(); - assert!(coverage(&s, 0, 0) < 1.0); - // Border always follows the canvas edge: a band inside it, not an empty selection. - let mut s = all(); - s.execute("select.modify.border", json!({"radius": 4})).unwrap(); - assert!(coverage(&s, 0, 15) > 0.0); - assert_eq!(coverage(&s, 20, 15), 0.0); - } - - #[test] - fn modify_away_from_the_canvas_edge_ignores_canvas_bounds() { - for at in [false, true] { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("select.modify.contract", json!({"radius": 4, "applyAtCanvasBounds": at})).unwrap(); - assert_eq!(coverage(&s, 11, 15), 0.0); - assert_eq!(coverage(&s, 15, 15), 1.0); - } - } - - #[test] - fn modify_refuses_radii_below_the_minimum() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - // Below each dialog's minimum (1 px, Feather 0.1 px) is refused, not clamped to 0: a - // negative Border radius used to clear the selection and report success (#993). - let below = [ - ("border", -5.0), - ("border", 0.5), - ("smooth", -5.0), - ("smooth", 0.0), - ("expand", -5.0), - ("expand", 0.0), - ("contract", -5.0), - ("contract", 0.9), - ("feather", -5.0), - ("feather", 0.0), - ("feather", 0.05), - ]; - let before = current_mask(&s.active().unwrap().doc).1; - for (op, r) in below { - let err = s.execute(&format!("select.modify.{op}"), json!({"radius": r})).unwrap_err(); - assert!(matches!(err, EngineError::BadParams { .. }), "{op} {r}: {err}"); - assert!(current_mask(&s.active().unwrap().doc).1 == before, "{op} {r}: the selection changed"); - } - // The minimum itself is still accepted. - s.execute("select.modify.feather", json!({"radius": 0.1})).unwrap(); - s.execute("select.modify.expand", json!({"radius": 1})).unwrap(); - assert_eq!(coverage(&s, 9, 15), 1.0); - } - - #[test] - fn geometry_params_that_would_wrap_are_rejected() { - let mut s = session(); - // 2^32 + 50 wrapped to `x = 50` and 3e9 to a negative coordinate through `as i32`; - // both are bad-params errors now, whatever the front door (UI, CLI, control, MCP). - for x in [4_294_967_346_i64, 3_000_000_000_i64] { - let err = s.execute("select.rect", json!({"x": x, "y": 0, "width": 10, "height": 10})).unwrap_err(); - assert!(err.to_string().contains("32-bit"), "{err}"); - } - let err = s.execute("document.pixel", json!({"x": 4_294_967_346_i64, "y": 0})).unwrap_err(); - assert!(err.to_string().contains("32 bits"), "{err}"); - // In-range coordinates, including negative and past-canvas ones, are unaffected. - s.execute("select.rect", json!({"x": -100, "y": -100, "width": 500, "height": 500})).unwrap(); - s.execute("document.pixel", json!({"x": 0, "y": 0})).unwrap(); - } - - #[test] - fn grow_and_similar() { - let mut s = session(); - s.execute("select.rect", json!({"x": 6, "y": 6, "width": 2, "height": 2})).unwrap(); - s.execute("select.grow", json!({"tolerance": 5})).unwrap(); - assert_eq!(coverage(&s, 14, 14), 1.0); - assert_eq!(coverage(&s, 30, 10), 0.0); - s.execute("select.similar", json!({"tolerance": 5})).unwrap(); - assert_eq!(coverage(&s, 30, 10), 1.0); - assert_eq!(coverage(&s, 20, 20), 0.0); - } - - #[test] - fn lasso_polygon() { - let mut s = session(); - s.execute("select.lasso", json!({"points": [[0, 0], [20, 0], [0, 20]]})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 1.0); - assert_eq!(coverage(&s, 18, 18), 0.0); - assert!(s.execute("select.lasso", json!({"points": [[0, 0], [1, 1]]})).is_err()); - s.execute("select.lasso", json!({"points": [[0, 0], [20, 0], [0, 20]], "mode": "subtract"})).unwrap(); - assert!(s.active().unwrap().doc.selection.is_none()); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(coverage(&s, 2, 2), 1.0); - } - - /// The previous per-sample padding and cropping, kept as the oracle for the row-wise ones. - fn old_pad_mask(m: &[f32], w: usize, h: usize, pad: usize, outside_empty: bool) -> Vec { - let pw = w + 2 * pad; - let mut out = vec![0.0f32; pw * (h + 2 * pad)]; - for (py, row) in out.chunks_exact_mut(pw).enumerate() { - let inside_y = (pad..pad + h).contains(&py); - if outside_empty && !inside_y { - continue; - } - let y = py.saturating_sub(pad).min(h - 1); - for (px, v) in row.iter_mut().enumerate() { - if outside_empty && !(pad..pad + w).contains(&px) { - continue; - } - *v = m.get(y * w + px.saturating_sub(pad).min(w - 1)).copied().unwrap_or(0.0); - } - } - out - } - - #[test] - fn row_wise_pad_and_crop_match_per_sample_ones() { - let mut seed = 0x2545_f491_4f6c_dd1du64; - let (w, h) = (37, 23); - let m: Vec = (0..w * h) - .map(|_| { - seed ^= seed << 13; - seed ^= seed >> 7; - seed ^= seed << 17; - (seed % 1000) as f32 / 999.0 - }) - .collect(); - for pad in [0, 1, 5, 40] { - for outside_empty in [false, true] { - let padded = pad_mask(&m, w, h, pad, outside_empty); - assert_eq!(padded, old_pad_mask(&m, w, h, pad, outside_empty), "pad {pad} {outside_empty}"); - assert_eq!(crop_mask(&padded, w + 2 * pad, pad, w, h), m, "pad {pad}"); - } - } - } - - /// The lasso over the polygon's rows gives exactly the whole-canvas result (#4532): random - /// polygons, partly off the canvas, with and without anti-aliasing, in every mode over an - /// existing selection. - #[test] - fn lasso_over_its_rows_matches_the_whole_canvas() { - let mut seed = 0x2545_f491_u32; - let mut rnd = |lo: f32, hi: f32| { - seed ^= seed << 13; - seed ^= seed >> 17; - seed ^= seed << 5; - lo + (seed % 10_000) as f32 / 10_000.0 * (hi - lo) - }; - for case in 0..24 { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 300, "height": 520})).unwrap(); - s.execute("select.rect", json!({"x": 40, "y": 60, "width": 200, "height": 300})).unwrap(); - let n = 3 + case % 7; - let pts: Vec<(f32, f32)> = (0..n).map(|_| (rnd(-30.0, 330.0), rnd(100.0, 420.0))).collect(); - let aa = case % 2 == 0; - let m = ["replace", "add", "subtract", "intersect"][case % 4]; - let doc = s.active().unwrap().doc.clone(); - let area = doc.bounds(); - let want = sel::combine(doc.selection.as_ref(), &sel::polygon(&pts, area, aa), area, SelectionMode::parse(m)); - let points: Vec<[f32; 2]> = pts.iter().map(|p| [p.0, p.1]).collect(); - s.execute("select.lasso", json!({"points": points, "antiAlias": aa, "mode": m})).unwrap(); - let got = s.active().unwrap().doc.selection.clone(); - let read = |x: &Option| x.as_ref().map(|s| s.read_region(area)); - assert_eq!(read(&got), read(&want), "case {case}: {m}, anti-alias {aa}, {pts:?}"); - } - } -} diff --git a/crates/engine/src/session.rs b/crates/engine/src/session.rs new file mode 100644 index 0000000000..d1f360e573 --- /dev/null +++ b/crates/engine/src/session.rs @@ -0,0 +1,14 @@ +use super::doc_state::DocState; +use super::error::{EngineError, Result}; +use super::{ + Arc, Document, LayerId, LayerTarget, Session, Value, brush_cmds, channel_cmds, commands, hidden_target, jobs, layer_multi_cmds, retouch_cmds, smart_cmds, + type_cmds, +}; + +mod documents; +mod execute; +mod history; +mod layers; + +pub use documents::move_item; +pub(crate) use history::{carry_visibility, fix_selection, restore_target, visibility_changes}; diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs new file mode 100644 index 0000000000..1ae07b35a8 --- /dev/null +++ b/crates/engine/src/session/documents.rs @@ -0,0 +1,166 @@ +use super::history::fix_selection; +use super::{DocState, Document, Session, channel_cmds, smart_cmds}; + +/// Move item `i` of `v` to position `to`, clamped to the end. Returns where it went; `None` when +/// `i` is out of range. +pub fn move_item(v: &mut Vec, i: usize, to: usize) -> Option { + if i >= v.len() { + return None; + } + let x = v.remove(i); + let to = to.min(v.len()); + v.insert(to, x); + Some(to) +} + +impl Session { + pub fn new() -> Self { + Self::default() + } + + pub fn documents(&self) -> &[DocState] { + &self.docs + } + + pub fn active_index(&self) -> Option { + self.active + } + + pub fn active(&self) -> Option<&DocState> { + self.active.and_then(|i| self.docs.get(i)) + } + + pub fn active_mut(&mut self) -> Option<&mut DocState> { + self.active.and_then(|i| self.docs.get_mut(i)) + } + + /// Monotonic active-document generation. It changes when a different document becomes + /// active, including a switch away and back between two observations; equal values therefore + /// mean the document was not switched in between. + pub fn active_generation(&self) -> u64 { + self.active_generation + } + + /// Bump [`Self::active_generation`] when the active document id changed. Call after every + /// assignment to `self.active`. + pub(crate) fn note_active_change(&mut self) { + let id = self.active().map(|st| st.doc.id); + if id != self.active_doc_for_generation { + self.active_doc_for_generation = id; + self.active_generation = self.active_generation.wrapping_add(1); + } + } + + pub fn set_active(&mut self, index: usize) -> bool { + if index < self.docs.len() { + self.active = Some(index); + self.note_active_change(); + self.sync_tools_mask_target(); + true + } else { + false + } + } + + /// Color Sampler points dropped by the last canvas-size change (crop, Canvas Size, Image Size, + /// rotation) since this was last called, for the status message. See [`edit`](Self::edit). + pub fn take_samplers_removed(&mut self) -> usize { + std::mem::take(&mut self.samplers_removed) + } + + /// Move the Color Sampler points through `a` in the next [`Session::edit`] (crop, Canvas Size, + /// Image Size, rotation), dropping the ones that land outside the new canvas. View state: the + /// points stay pinned to their pixels, no history step. Cleared by the edit it is set for. + /// The painting symmetry moves through `a` too. + pub(crate) fn set_sampler_map(&mut self, a: photocraft_geom::Affine) { + self.set_canvas_map(a, a); + } + + /// [`set_sampler_map`](Self::set_sampler_map) for an edit whose sampler map differs from its + /// geometry map (a flip or rotation maps pixel centres): `geometry` moves the painting symmetry. + pub(crate) fn set_canvas_map(&mut self, geometry: photocraft_geom::Affine, samplers: photocraft_geom::Affine) { + self.sampler_map = Some(samplers); + self.symmetry_map = Some(geometry); + } + + pub fn sync_tools_mask_target(&mut self) { + let mask = self.active().is_some_and(|st| st.channel_view.target == crate::channel_cmds::ChannelTarget::LayerMask); + self.tools.target_mask(mask); + } + + /// Move the document at `from` to tab position `to` (clamped to the last), keeping the active + /// document active. Returns its new index; `None` when `from` is out of range. + pub fn move_document(&mut self, from: usize, to: usize) -> Option { + let active = self.active().map(|d| d.doc.id); + let to = move_item(&mut self.docs, from, to)?; + self.active = active.and_then(|id| self.docs.iter().position(|d| d.doc.id == id)); + self.note_active_change(); + Some(to) + } + + /// Add a document (from File → New, an import, etc.) and make it active. + /// Preserve its identity unless another open document already owns it. + /// Identity-dependent callers must read the admitted document at the returned index. + pub fn add_document(&mut self, mut doc: Document, path: Option) -> usize { + // Persisted IDs can overlap the allocator, so check every replacement too. + while self.docs.iter().any(|st| st.doc.id == doc.id) { + doc.id = photocraft_doc::DocId::fresh(); + } + let mut st = DocState::new(doc, path); + st.history.max_states = self.prefs.get().performance.history_states.max(1) as usize; + st.history.max_bytes = self.prefs.get().performance.history_budget_bytes(); + st.history.non_linear = self.prefs.get().history_options.allow_non_linear_history; + self.docs.push(st); + let i = self.docs.len() - 1; + self.active = Some(i); + self.note_active_change(); + i + } + + /// Holds a decoded document back from opening (see `held_opens`); returns its id. + pub(crate) fn hold_open(&mut self, doc: Document, source_read_only: bool) -> u64 { + self.next_held_open = self.next_held_open.wrapping_add(1); + self.held_opens.push((self.next_held_open, doc, source_read_only)); + self.next_held_open + } + + /// Takes a document a background open held back for the profile prompt: the document and + /// whether its source is read-only. `None` when there is no such document (already taken). + pub fn take_held_open(&mut self, id: u64) -> Option<(Document, bool)> { + let i = self.held_opens.iter().position(|(h, _, _)| *h == id)?; + let (_, doc, read_only) = self.held_opens.remove(i); + Some((doc, read_only)) + } + + pub fn close(&mut self, index: usize) -> Option { + if index >= self.docs.len() { + return None; + } + smart_cmds::on_close(self, index); + if let Some(id) = self.docs.get(index).map(|d| d.doc.id) { + self.cancel_jobs_on(id); + self.subject_cache.forget_doc(id); + self.object_finder.forget_doc(id); + } + let d = self.docs.remove(index); + self.active = if self.docs.is_empty() { + None + } else { + Some(match self.active { + Some(active) if active > index => active - 1, + Some(active) if active < index => active, + _ => index.min(self.docs.len() - 1), + }) + }; + self.note_active_change(); + Some(d) + } +} + +pub(super) fn fix_active(st: &mut DocState) { + if st.active_layer.is_none_or(|id| st.doc.layer(id).is_none()) { + st.active_layer = st.doc.top_layer(); + } + fix_selection(st); + channel_cmds::fix_view(st); +} diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs new file mode 100644 index 0000000000..8ea18aa4e0 --- /dev/null +++ b/crates/engine/src/session/execute.rs @@ -0,0 +1,311 @@ +use super::history::{fix_selection, visibility_changes}; +use super::{ + Arc, Document, EngineError, LayerId, Result, Session, Value, brush_cmds, channel_cmds, commands, hidden_target, jobs, layer_multi_cmds, retouch_cmds, + smart_cmds, +}; + +impl Session { + /// Run a command by id with JSON params. Returns a JSON result. + /// + /// Any command accepts an optional `"coalesce": ""` param: consecutive edits with the same + /// key (and no other edit, undo or redo in between) share one history step, like Photoshop's + /// single "Edit Type Layer" step for a whole typing session or one step per slider drag. + /// + /// Runs synchronously, including job-capable commands (see [`jobs`]; [`Session::start`] runs + /// those in the background). + pub fn execute(&mut self, id: &str, params: Value) -> Result { + match self.dispatch(id, params, false)? { + jobs::Started::Done(v) => Ok(v), + // Not reached: inline dispatch never starts a job. Waiting is still correct. + jobs::Started::Job(j) => self.wait_job(j), + } + } + + /// Offer a finished live stroke to the next command: a `paint.stroke` / `paint.pencil` whose + /// params match it exactly takes its pixels instead of rendering the stroke again (its result + /// then says `"fromLive": true`); any other command drops the offer. The command still runs + /// through the normal dispatch, so authorization, the journal and history are unchanged. + pub fn offer_live_stroke(&mut self, live: brush_cmds::LiveStroke) -> Result<()> { + self.live_commit = Some(live.into_commit()?); + Ok(()) + } + + /// [`Self::offer_live_stroke`] for a live Clone Stamp or History Brush stroke, taken by the + /// `paint.cloneStamp` / `paint.historyBrush` it was drawn for. Refused for the Healing Brush + /// (it blends on release) and when the brush, layer or clone source changed during the stroke. + pub fn offer_live_retouch(&mut self, live: retouch_cmds::LiveRetouch) -> Result<()> { + let commit = live.into_commit(self)?; + self.live_commit = Some(commit); + Ok(()) + } + + /// [`Self::offer_live_stroke`] for a live Dodge, Burn, Sponge, Blur, Sharpen or Smudge stroke, + /// taken by the command it was drawn for: the stroke's finishing dabs are run first. Refused + /// when the brush, tool options or layer changed during the stroke. + pub fn offer_live_dab(&mut self, live: retouch_cmds::LiveDab) -> Result<()> { + // A refused offer withdraws the previous one too. + self.live_commit = None; + let commit = live.into_commit(self)?; + self.live_commit = Some(commit); + Ok(()) + } + + /// Withdraw an offer the command didn't take. + pub fn clear_live_stroke(&mut self) { + self.live_commit = None; + } + + /// Check a call without running it: the command exists, its params have the shape + /// [`Self::execute`] requires (New Layer's fully), and it can run now. For a caller that does + /// something first only when the command will go through: Free Transform applies its box + /// before a new layer is made. + /// + /// Complete for the commands in [`PRECHECKED`]: there it accepts what `execute` accepts, but + /// for the group nesting limit (a test holds the two together). For others it checks only what they share. + pub fn precheck(&mut self, id: &str, params: &Value) -> Result<()> { + let spec = crate::commands::find(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?; + if !(params.is_object() || params.is_null()) { + return Err(EngineError::BadParams { cmd: id.to_string(), msg: "params must be a JSON object".into() }); + } + crate::param_check::check(spec, params)?; + crate::filters::validate_params(id, params)?; + if id == "layer.new.layer" { + crate::new_layer::parse(params)?; + } + if id == "layer.new.group" { + crate::group_options::below(id, params)?; + } + if id == "layer.new.group" || id == "layer.new.groupFromLayers" { + crate::group_options::GroupOptions::parse(id, params)?; + } + // A command that takes `"layer"` (an id or a name, as it resolves it) fails on one the + // document doesn't have. + if spec.params.contains("\"layer\":id") + && let Some(l) = commands::explicit_layer(self, params)? + && self.active().is_none_or(|st| st.doc.layer(l).is_none()) + { + return Err(EngineError::BadParams { cmd: id.to_string(), msg: format!("no layer {}", l.0) }); + } + self.admit(id, spec, params).map(|_| ()) + } + + /// What `dispatch` checks once the params are valid and a floating selection has dropped: + /// the precondition (on a named layer for commands that take one), a job running on the + /// document, a hidden target. The run params (the Channels panel's target filled in) when it + /// may run. + pub(crate) fn admit(&mut self, id: &str, spec: &commands::CommandSpec, params: &Value) -> Result { + let run_params = channel_cmds::inject_target(self, id, commands::inject_kind(id, params.clone())); + if let Err(why) = self.explicit_layer_enabled(spec, params).unwrap_or_else(|| self.precondition(spec, &run_params)) { + return Err(EngineError::Disabled(id.to_string(), why)); + } + if let Some(why) = self.job_conflict(id, spec.journal) { + return Err(EngineError::Disabled(id.to_string(), why)); + } + // Painting on or moving a hidden layer is refused, as in Photoshop (#571). + if let Some(why) = hidden_target::refusal(self, id, params) { + return Err(EngineError::Other(why.into())); + } + Ok(run_params) + } + + /// Is the command currently runnable? (drives menu enablement) + pub fn is_enabled(&self, id: &str) -> bool { + self.is_enabled_with(id, &Value::Null) + } + + /// [`Self::is_enabled`] for a call with `params`: their `"target"` can enable a command, as a + /// targeted layer mask enables Image › Adjustments › Invert on an adjustment layer (#780). + pub fn is_enabled_with(&self, id: &str, params: &Value) -> bool { + self.disabled_reason_with(id, params).is_none() + } + + /// Why the command can't run now (None = it can): its own precondition, or a background job + /// running on the active document. + pub fn disabled_reason(&self, id: &str) -> Option { + self.disabled_reason_with(id, &Value::Null) + } + + /// [`Self::disabled_reason`] for a call with `params`. + pub fn disabled_reason_with(&self, id: &str, params: &Value) -> Option { + let Some(s) = commands::find(id) else { return Some(EngineError::UnknownCommand(id.into()).to_string()) }; + self.precondition(s, &channel_cmds::inject_target(self, id, params.clone())).err().or_else(|| self.job_conflict(id, s.journal)) + } + + /// The command's own precondition for a call with `params` (their target filled in). + fn precondition(&self, spec: &commands::CommandSpec, params: &Value) -> std::result::Result<(), String> { + if self.names_existing_layer(spec.id, params) { + return Ok(()); + } + // `ifPresent`: with no Background there is nothing to do, which isn't an error. + if spec.id == "layer.new.layerFromBackground" && params.get("ifPresent").and_then(Value::as_bool) == Some(true) { + return self.active().map(|_| ()).ok_or_else(|| "no document open".into()); + } + if spec.id == "layer.smartFilter.deleteFilterMask" && params.get("ifPresent").and_then(Value::as_bool) == Some(true) { + return smart_cmds::smart_layer_active(self); + } + channel_cmds::mask_target_enabled(self, spec.id, params).unwrap_or_else(|| (spec.enabled)(self)) + } + + /// Whether `id` is a layer command whose `"layer"` param names an existing layer, so it needs no + /// active layer (#2457). Matches how `layer_param` resolves the layer when the command runs. + fn names_existing_layer(&self, id: &str, params: &Value) -> bool { + (matches!(id, "layer.hideLayers" | "layer.showLayers" | "layer.renameLayer") + || (id == "layer.setProps" + && params.as_object().is_some_and(|o| o.contains_key("visible") && o.keys().all(|k| matches!(k.as_str(), "layer" | "visible" | "coalesce"))))) + && params.get("layer").and_then(Value::as_u64).is_some_and(|l| self.active().is_some_and(|st| st.doc.layer(LayerId(l)).is_some())) + } + + /// The precondition of a command whose params document `"layer":id` (it acts on that layer), + /// when `params` name an existing layer of the active document: checked on that layer rather + /// than on the active one, which stays active (#466). A script, action or agent can then + /// Replace Contents, Rasterize or Merge Down a layer without selecting it first. `None` for + /// other commands, or without such a layer. + pub(crate) fn explicit_layer_enabled(&mut self, spec: &commands::CommandSpec, params: &Value) -> Option> { + if !spec.params.contains("\"layer\":id") { + return None; + } + let target = LayerId(params.get("layer")?.as_u64()?); + let d = self.active_mut()?; + d.doc.layer(target)?; + let active = d.active_layer.replace(target); + let r = self.precondition(spec, params); + if let Some(d) = self.active_mut() { + d.active_layer = active; + } + Some(r) + } + + /// Apply an undoable edit to the active document. + pub fn edit(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + self.edit_with(label, true, f) + } + + /// Apply a change of layer visibility (and nothing else): a history state when History + /// Options › Make Layer Visibility Changes Undoable is on (the default, as in Photoshop), + /// otherwise the document changes without one, so the History panel and Undo skip it. + pub fn edit_visibility(&mut self, label: &str, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + let record = self.visibility_undoable(); + self.edit_with(label, record, f) + } + + /// History Options › Make Layer Visibility Changes Undoable. + pub fn visibility_undoable(&self) -> bool { + self.prefs().history_options.make_layer_visibility_changes_undoable + } + + /// [`Self::edit`], recording a history state only when `record` is set. An unrecorded change + /// still marks the document modified and leaves the redo states alone. + pub(crate) fn edit_with(&mut self, label: &str, record: bool, f: impl FnOnce(&mut Document, &mut Option) -> Result) -> Result { + // Every mutation path (linked-file refresh, transform, place) re-renders smart objects + // through this entry; the requesting session's authority must reach those renders. + let _authorization = crate::jobs::render_authorization_scope(self.authorize); + let _document = crate::doc_path::scope(self.active().and_then(|d| d.path.as_deref()).map(std::path::Path::new)); + let restrict = self.color_restrict; + let filter_mask_layer = self.filter_mask_layer; + let sampler_map = self.sampler_map.take(); + let symmetry_map = self.symmetry_map.take(); + let st = self.active_mut().ok_or(EngineError::NoDocument)?; + let before = st.doc.clone(); + // Selecting layers is not a step, so the state this edit leaves behind targets what was + // selected just before it (#1356): undoing a stroke keeps the painted layer active. + let prior = st.layer_target(); + let mut doc = (*before).clone(); + let mut active = st.active_layer; + let r = f(&mut doc, &mut active)?; + if let (Some(k), Some(id)) = (restrict, active) { + channel_cmds::restrict_to_color(&before, &mut doc, id, k); + } + if let Some(id) = filter_mask_layer { + smart_cmds::refresh_after_filter_mask_edit(&mut doc, id)?; + } + st.doc = Arc::new(doc); + st.active_layer = active; + fix_selection(st); + channel_cmds::fix_view(st); + // A canvas-size change (crop, Canvas Size, Image Size, rotation) moves the Color Sampler + // points with their pixels and drops the ones that land outside the new canvas. View + // state: not a history step. A mapped edit (the four above) gives the exact map; any other + // size change falls back to dropping whatever is now outside. + let mut samplers_removed = 0; + if let Some(a) = sampler_map { + let bounds = st.doc.bounds(); + let n = st.color_samplers.len(); + st.color_samplers.retain_mut(|p| { + let q = a.apply(photocraft_geom::Point::new(p[0], p[1])); + *p = [q.x, q.y]; + bounds.contains(q.x.floor() as i32, q.y.floor() as i32) + }); + samplers_removed = n - st.color_samplers.len(); + } else if st.doc.size != before.size { + let bounds = st.doc.bounds(); + let n = st.color_samplers.len(); + st.color_samplers.retain(|p| bounds.contains(p[0].floor() as i32, p[1].floor() as i32)); + samplers_removed = n - st.color_samplers.len(); + } + // The painting symmetry stays on its pixels too (Photoshop keeps it as a path). + if let Some(a) = symmetry_map { + st.symmetry = st.symmetry.take().and_then(|sym| sym.transform(&a)); + } + // The pixels the edit can have changed, as its undo would report them: nothing for a + // selection, guides or a layer's name (a new lasso selection used to recomposite a large + // document, seconds on the CPU path, #1773), a new or deleted layer's bounds (a + // duplicate or a paste), and everything when that can't be bounded. A command may still + // report its own, tighter damage after this. + let damage = layer_multi_cmds::step_damage(&before, &st.doc); + let keep_clean = st.keeps_clean(&before); + #[cfg(any(test, debug_assertions))] + let hint_before = before.clone(); + let key = self.coalesce_request.clone(); + let st = self.active_mut().ok_or(EngineError::NoDocument)?; + let layers = st.layer_target(); + if !record { + // Nothing to coalesce with: the next recorded step starts a state of its own. + st.coalesce = None; + } else if key.is_none() || st.coalesce != key || !st.history.can_undo() { + st.history.set_current_layers(prior); + st.history.record(label, before.clone(), layers); + st.history.add_current_visibility(&visibility_changes(&before, &st.doc)); + st.trim_history(); + st.coalesce = key; + } else { + st.history.set_current_layers(layers); + st.history.add_current_visibility(&visibility_changes(&before, &st.doc)); + st.coalesce = key; + } + st.bump_revision(keep_clean); + st.last_damage = damage; + self.samplers_removed += samplers_removed; + #[cfg(any(test, debug_assertions))] + { + self.hint_before = Some(hint_before); + } + if damage.is_none_or(|d| !d.is_empty()) { + // Free the mask early; a stale entry would only miss (it is keyed by pixels). + self.subject_cache = Default::default(); + self.object_finder = Default::default(); + } + Ok(r) + } + + /// A command's own rect for the pixels its latest [`Session::edit`] changed: sub-tile precise + /// for a brush dab, where the edit's own damage ([`layer_multi_cmds::step_damage`]) has tile + /// precision. The redraw area becomes the intersection of the two, so a hint can only narrow + /// it (or bound a change `step_damage` can't). In debug and test builds, a hint that misses a + /// tile the edit changed is counted in [`Session::damage_misses`] and logged; it never panics. + pub fn hint_damage(&mut self, rect: photocraft_geom::Rect) { + #[cfg(any(test, debug_assertions))] + if let Some(before) = self.hint_before.take() + && let Some(st) = self.active() + && st.doc.id == before.id + { + let misses = layer_multi_cmds::hint_misses(&before, &st.doc, rect); + if misses > 0 { + let step = st.history.undo_label().unwrap_or("?"); + eprintln!("warning: `{step}` hinted damage {rect:?} that misses {misses} changed tile(s); the canvas may not redraw them"); + self.damage_misses += misses; + } + } + let Some(st) = self.active_mut() else { return }; + st.last_damage = Some(st.last_damage.map_or(rect, |d| d.intersect(&rect))); + } +} diff --git a/crates/engine/src/session/history.rs b/crates/engine/src/session/history.rs new file mode 100644 index 0000000000..b6e81e64bc --- /dev/null +++ b/crates/engine/src/session/history.rs @@ -0,0 +1,189 @@ +use super::documents::fix_active; +use super::{Arc, DocState, Document, LayerId, LayerTarget, Session, channel_cmds, layer_multi_cmds}; + +impl Session { + pub fn undo(&mut self) -> bool { + // A background job is computing from the current state: it must not move under it. + if self.active_job().is_some() { + return false; + } + let keep_visibility = !self.visibility_undoable(); + let Some(st) = self.active_mut() else { return false }; + st.coalesce = None; + // The step being undone: the layers whose visibility it changed get it back. + let changed = st.history.current_visibility().to_vec(); + match st.history.undo(st.doc.clone()) { + // Undo Clear History: the list comes back, the document is the same one. Not an + // edit: keep a clean document clean (the revision still moves for the panels). + Some((d, layers)) if Arc::ptr_eq(&d, &st.doc) => { + restore_target(st, layers); + let clean = !st.is_dirty(); + st.revision += 1; + if clean { + st.saved_revision = st.revision; + } + st.last_damage = Some(photocraft_geom::Rect::EMPTY); + true + } + Some((mut d, layers)) => { + // Save As changes file identity outside history, just like the saved path. + if d.name != st.doc.name { + Arc::make_mut(&mut d).name.clone_from(&st.doc.name); + } + if keep_visibility { + carry_visibility(&st.doc, &mut d, &changed); + } + // Pixels this step can have touched, so the canvas recomposites only that + // (it recomposited everything before). + let damage = layer_multi_cmds::step_damage(&d, &st.doc); + let before = std::mem::replace(&mut st.doc, d); + let keep_clean = st.keeps_clean(&before); + restore_target(st, layers); + st.bump_revision(keep_clean); + st.last_damage = damage; + true + } + None => false, + } + } + + pub fn redo(&mut self) -> bool { + if self.active_job().is_some() { + return false; + } + let keep_visibility = !self.visibility_undoable(); + let Some(st) = self.active_mut() else { return false }; + st.coalesce = None; + match st.history.redo(st.doc.clone()) { + Some((mut d, layers)) => { + if d.name != st.doc.name { + Arc::make_mut(&mut d).name.clone_from(&st.doc.name); + } + if keep_visibility { + // The step being redone sets the visibility it changed again. + let changed = st.history.current_visibility().to_vec(); + carry_visibility(&st.doc, &mut d, &changed); + } + let damage = layer_multi_cmds::step_damage(&st.doc, &d); + let before = std::mem::replace(&mut st.doc, d); + let keep_clean = st.keeps_clean(&before); + restore_target(st, layers); + st.bump_revision(keep_clean); + st.last_damage = damage; + true + } + None => false, + } + } +} + +impl Session { + /// Take back the step a compound command just recorded, after its later part failed: the + /// document before the command is current again, nothing is left to redo, and in non-linear + /// history the later states that step moved stay where they were (an undo would land on the + /// last of them). Falls back to Undo plus forgetting the redo states when the history + /// changed since that step. Returns whether a step was taken back. + pub fn rollback(&mut self) -> bool { + if self.active_job().is_some() { + return false; + } + let keep_visibility = !self.visibility_undoable(); + let Some(st) = self.active_mut() else { return false }; + st.coalesce = None; + // The step taken back (and the later states it moved, in non-linear history): the layers + // whose visibility it changed get it back, the unrecorded changes stay, as for Undo. + let changed = st.history.current_visibility().to_vec(); + let Some((mut d, layers)) = st.history.rollback() else { + let undone = self.undo(); + if undone && let Some(st) = self.active_mut() { + st.history.clear_redo(); + } + return undone; + }; + if d.name != st.doc.name { + Arc::make_mut(&mut d).name.clone_from(&st.doc.name); + } + if keep_visibility { + carry_visibility(&st.doc, &mut d, &changed); + } + let damage = layer_multi_cmds::step_damage(&d, &st.doc); + let before = std::mem::replace(&mut st.doc, d); + // Taking back a selection-only step keeps a clean document clean, as Undo does (#6865). + let keep_clean = st.keeps_clean(&before); + restore_target(st, layers); + st.bump_revision(keep_clean); + st.last_damage = damage; + true + } +} + +/// History Options › Make Layer Visibility Changes Undoable off: showing or hiding a layer is not +/// a history state, so stepping through history must not bring the old visibility back either +/// (history states hold whole documents). Gives every layer of `to` that `from` also has the +/// visibility it has in `from`, except the `stepped` layers: a recorded step between the two +/// states changed their visibility (Apply Layer Comp, a snapshot, a visibility change recorded +/// while the option was on), so they keep the visibility `to` holds. Layers only one of them has +/// keep theirs. Copies `to` only when something differs. +pub(crate) fn carry_visibility(from: &Document, to: &mut Arc, stepped: &[LayerId]) { + let visible: std::collections::HashMap = from.walk().into_iter().map(|(_, _, l)| (l.id, l.visible)).collect(); + let changes: Vec<(LayerId, bool)> = to + .walk() + .into_iter() + .filter(|(_, _, l)| !stepped.contains(&l.id)) + .filter_map(|(_, _, l)| visible.get(&l.id).filter(|v| **v != l.visible).map(|v| (l.id, *v))) + .collect(); + if changes.is_empty() { + return; + } + let d = Arc::make_mut(to); + for (id, v) in changes { + if let Some(l) = d.layer_mut(id) { + l.visible = v; + } + } +} + +/// The layers (in both documents) whose visibility differs between `before` and `after`: what a +/// recorded step changed, for [`photocraft_ops::History::add_current_visibility`]. +pub(crate) fn visibility_changes(before: &Document, after: &Document) -> Vec { + let visible: std::collections::HashMap = before.walk().into_iter().map(|(_, _, l)| (l.id, l.visible)).collect(); + after.walk().into_iter().filter(|(_, _, l)| visible.get(&l.id).is_some_and(|v| *v != l.visible)).map(|(_, _, l)| l.id).collect() +} + +/// Undo / redo: target the layers the restored state targeted, as far as they still exist. +pub(crate) fn restore_target(st: &mut DocState, layers: LayerTarget) { + // Deselect Layers is a valid history target, not a missing layer to replace with the top + // layer. An edit after deselection must undo back to no active or selected layers (#3042). + if layers.active.is_none() && layers.selected.is_empty() { + st.active_layer = None; + st.selected_layers.clear(); + st.layer_anchor = None; + channel_cmds::fix_view(st); + return; + } + if layers.active.is_some_and(|id| st.doc.layer(id).is_some()) { + st.active_layer = layers.active; + st.selected_layers = layers.selected; + } + fix_active(st); +} + +/// Keep the layer selection valid after the document or the active layer changed: drop deleted +/// layers, and collapse to the active layer when a command made a non-member active. +pub(crate) fn fix_selection(st: &mut DocState) { + let Some(active) = st.active_layer else { + st.selected_layers.clear(); + return; + }; + if !st.selected_layers.contains(&active) { + st.selected_layers = vec![active]; + } else { + let live: std::collections::HashSet = st.doc.walk().into_iter().map(|(_, _, l)| l.id).collect(); + st.selected_layers.retain(|id| live.contains(id)); + let mut seen = std::collections::HashSet::new(); + st.selected_layers.retain(|id| seen.insert(*id)); + } + if st.layer_anchor.is_some_and(|a| !st.selected_layers.contains(&a)) { + st.layer_anchor = Some(active); + } +} diff --git a/crates/engine/src/session/layers.rs b/crates/engine/src/session/layers.rs new file mode 100644 index 0000000000..2e6009d16f --- /dev/null +++ b/crates/engine/src/session/layers.rs @@ -0,0 +1,92 @@ +use super::history::fix_selection; +use super::{Arc, EngineError, LayerId, Result, Session, channel_cmds, layer_multi_cmds, type_cmds}; + +impl Session { + /// Replace the active document's active layer without creating a history step. + pub fn select_layer(&mut self, id: LayerId) -> Result<()> { + let st = self.active_mut().ok_or(EngineError::NoDocument)?; + if st.doc.layer(id).is_none() { + return Err(EngineError::NoLayer(id)); + } + st.active_layer = Some(id); + st.selected_layers = vec![id]; + st.layer_anchor = Some(id); + channel_cmds::fix_view(st); + // Selecting a layer is not an edit: keep a clean document clean. + let clean = st.saved_revision == st.revision; + st.revision += 1; + if clean { + st.saved_revision = st.revision; + } + st.last_damage = Some(photocraft_geom::Rect::EMPTY); + Ok(()) + } + + /// Put back a layer selection (active layer, selected layers, anchor) that a step moved, such + /// as the undo ending an empty Type Mask, which restores the selection from before it. `None` + /// is no layer selected. Selected layers that are gone are dropped; an active layer that is + /// gone is an error and changes nothing. Like [`Self::select_layer`], not an edit. + pub fn restore_layer_selection(&mut self, active: Option, selected: &[LayerId], anchor: Option) -> Result<()> { + let st = self.active_mut().ok_or(EngineError::NoDocument)?; + if let Some(id) = active + && st.doc.layer(id).is_none() + { + return Err(EngineError::NoLayer(id)); + } + st.active_layer = active; + st.selected_layers = selected.to_vec(); + st.layer_anchor = anchor.filter(|_| active.is_some()); + fix_selection(st); + channel_cmds::fix_view(st); + let clean = st.saved_revision == st.revision; + st.revision += 1; + if clean { + st.saved_revision = st.revision; + } + st.last_damage = Some(photocraft_geom::Rect::EMPTY); + Ok(()) + } + + /// Re-renders the pixels of type layers whose font arrived after they were drawn (the web + /// build fetches served fonts on demand, `photocraft_text::served`). The layers' model is + /// unchanged, so this is no history step and leaves a clean document clean. Unknown documents + /// and layers are skipped. A document a background job is computing from must not move under + /// it: its layers are returned, to try again later. + pub fn refresh_type_layers(&mut self, layers: &[(photocraft_doc::DocId, LayerId)]) -> Vec<(photocraft_doc::DocId, LayerId)> { + let mut busy = Vec::new(); + let mut docs: Vec = Vec::new(); + for (d, _) in layers { + if !docs.contains(d) { + docs.push(*d); + } + } + for doc_id in docs { + let mine = layers.iter().filter(|(d, _)| *d == doc_id); + if self.job_on(doc_id).is_some() { + busy.extend(mine); + continue; + } + let Some(st) = self.docs.iter_mut().find(|st| st.doc.id == doc_id) else { continue }; + let snapshot = st.doc.clone(); + let mut doc = (*snapshot).clone(); + let mut changed = false; + for (_, id) in mine { + if let Some(photocraft_doc::Layer { content: photocraft_doc::LayerContent::Text(t), .. }) = doc.layer_mut(*id) { + type_cmds::refresh(&snapshot, t); + changed = true; + } + } + if changed { + let clean = st.saved_revision == st.revision; + st.doc = Arc::new(doc); + st.revision += 1; + if clean { + st.saved_revision = st.revision; + } + // Only the refreshed layers' caches changed: their changed tiles. + st.last_damage = layer_multi_cmds::step_damage(&snapshot, &st.doc); + } + } + busy + } +} diff --git a/crates/engine/src/shell_commands.rs b/crates/engine/src/shell_commands.rs index 2bd765673f..3b52c03a03 100644 --- a/crates/engine/src/shell_commands.rs +++ b/crates/engine/src/shell_commands.rs @@ -7,6 +7,7 @@ /// Shell commands: id, label, menu path, default shortcut. pub const SHELL_COMMANDS: &[(&str, &str, &[&str], Option<&str>)] = &[ + ("layer.selectUnderCursor", "Select Layer Under Cursor", &[], None), ("ui.symmetryTransform", "Transform Symmetry", &[], None), // Right-click the options bar's tool icon (#2825); `tool.resetAll` asks first. ("tool.reset", "Reset Tool", &[], None), @@ -18,6 +19,12 @@ pub const SHELL_COMMANDS: &[(&str, &str, &[&str], Option<&str>)] = &[ ("window.panel.dock", "Dock Panel", &[], None), ("window.panel.activate", "Activate Panel", &[], None), ("window.panel.close", "Close Panel", &[], None), + ("window.panel.closeGroup", "Close Tab Group", &[], None), + ("window.panel.closeOthers", "Close Other Panels in Group", &[], None), + ("window.panel.minimize", "Minimize Panel", &[], None), + ("window.panel.iconic", "Collapse to Icons", &[], None), + ("window.panel.window", "Open Panel in Own Window", &[], None), + ("ui.dock", "Arrange Panels", &[], None), ("file.open", "Open…", &["File"], Some("Cmd+O")), ("file.save", "Save", &["File"], Some("Cmd+S")), ("file.saveAs", "Save As…", &["File"], Some("Cmd+Shift+S")), @@ -67,20 +74,20 @@ pub const SHELL_COMMANDS: &[(&str, &str, &[&str], Option<&str>)] = &[ ("window.nextDocument", "Next Document", &[], Some("Ctrl+Tab")), ("window.previousDocument", "Previous Document", &[], Some("Ctrl+Shift+Tab")), ("window.theme.toggle", "Next Appearance Mode", &["Window"], None), - ("window.theme.pro", "Pro Theme", &["Window", "Theme"], None), - ("window.theme.proMedium", "Pro Medium Gray Theme", &["Window", "Theme"], None), - ("window.theme.proLightGray", "Pro Light Gray Theme", &["Window", "Theme"], None), - ("window.theme.proLight", "Pro Light Theme", &["Window", "Theme"], None), - ("window.theme.studio", "Studio Theme", &["Window", "Theme"], None), - ("window.theme.studioLight", "Studio Light Theme", &["Window", "Theme"], None), - ("window.theme.classic", "Classic Theme", &["Window", "Theme"], None), - ("window.theme.solarizedDark", "Solarized Dark Theme", &["Window", "Theme"], None), - ("window.theme.adwaita", "Adwaita Light Theme", &["Window", "Theme"], None), - ("window.theme.adwaitaDark", "Adwaita Dark Theme", &["Window", "Theme"], None), - ("window.theme.breezeLight", "Breeze Light Theme", &["Window", "Theme"], None), - ("window.theme.breezeDark", "Breeze Dark Theme", &["Window", "Theme"], None), - ("window.theme.softLight", "Soft Light Theme", &["Window", "Theme"], None), - ("window.theme.softDark", "Soft Dark Theme", &["Window", "Theme"], None), + ("window.theme.pro", "Pro", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.proMedium", "Pro Medium Gray", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.studio", "Studio", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.solarizedDark", "Solarized", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.adwaitaDark", "Adwaita", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.breezeDark", "Breeze", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.softDark", "Soft", &["Window", "Theme", "Dark Theme"], None), + ("window.theme.proLightGray", "Pro Light Gray", &["Window", "Theme", "Light Theme"], None), + ("window.theme.proLight", "Pro Light", &["Window", "Theme", "Light Theme"], None), + ("window.theme.studioLight", "Studio", &["Window", "Theme", "Light Theme"], None), + ("window.theme.classic", "Classic", &["Window", "Theme", "Light Theme"], None), + ("window.theme.adwaita", "Adwaita", &["Window", "Theme", "Light Theme"], None), + ("window.theme.breezeLight", "Breeze", &["Window", "Theme", "Light Theme"], None), + ("window.theme.softLight", "Soft", &["Window", "Theme", "Light Theme"], None), ("window.theme.system", "Sync with system", &["Window", "Theme"], None), ("edit.search", "Search…", &["Edit"], Some("Cmd+F")), ("help.discord", "Join the ArtCraft Discord…", &["Help"], None), @@ -138,6 +145,7 @@ pub const TOOLS: &[(&str, &str, Option<&str>)] = &[ ("tools.select.cloneStamp", "Clone Stamp Tool", Some("S")), ("tools.select.patternStamp", "Pattern Stamp Tool", Some("S")), ("tools.select.historyBrush", "History Brush Tool", Some("Y")), + ("tools.select.artHistoryBrush", "Art History Brush Tool", Some("Y")), ("tools.select.blur", "Blur Tool", None), ("tools.select.sharpen", "Sharpen Tool", None), ("tools.select.smudge", "Smudge Tool", None), diff --git a/crates/engine/src/shortcut_sets.rs b/crates/engine/src/shortcut_sets.rs index 26062def96..4b36de1b82 100644 --- a/crates/engine/src/shortcut_sets.rs +++ b/crates/engine/src/shortcut_sets.rs @@ -35,15 +35,31 @@ pub struct ShortcutSet { } impl ShortcutSet { - /// The bindings `p` uses now. - pub fn of(p: &Preferences) -> Self { - Self { shortcuts: p.shortcuts.clone(), alternate_shortcuts: p.alternate_shortcuts.clone() } + /// A set of these bindings, without the actions' function keys (they belong to the + /// actions, as in an `.atn` file, not to a set). + pub fn from_bindings(shortcuts: &BTreeMap, alternates: &BTreeMap>) -> Self { + let shortcuts = shortcuts.iter().filter(|(id, _)| !is_action_key(id)).map(|(id, k)| (id.clone(), k.clone())).collect(); + let alternate_shortcuts = alternates.iter().filter(|(id, _)| !is_action_key(id)).map(|(id, k)| (id.clone(), k.clone())).collect(); + Self { shortcuts, alternate_shortcuts } } - /// Use this set's bindings in `p`. + /// The bindings `p` uses now, without the actions' function keys. + pub fn of(p: &Preferences) -> Self { + Self::from_bindings(&p.shortcuts, &p.alternate_shortcuts) + } + + /// Put this set's bindings in place of `shortcuts` / `alternates`, keeping the actions' + /// function keys: switching sets with `useSet` and in the Keyboard Shortcuts dialog. + pub fn apply(&self, shortcuts: &mut BTreeMap, alternates: &mut BTreeMap>) { + shortcuts.retain(|id, _| is_action_key(id)); + shortcuts.extend(self.shortcuts.iter().filter(|(id, _)| !is_action_key(id)).map(|(id, k)| (id.clone(), k.clone()))); + alternates.retain(|id, _| is_action_key(id)); + alternates.extend(self.alternate_shortcuts.iter().filter(|(id, _)| !is_action_key(id)).map(|(id, k)| (id.clone(), k.clone()))); + } + + /// Use this set's bindings in `p`, keeping the actions' function keys. pub fn load_into(&self, p: &mut Preferences) { - p.shortcuts = self.shortcuts.clone(); - p.alternate_shortcuts = self.alternate_shortcuts.clone(); + self.apply(&mut p.shortcuts, &mut p.alternate_shortcuts); } /// Check every key (a set from a call or a hand-edited file must not hold garbage). @@ -60,6 +76,11 @@ impl ShortcutSet { } } +/// An action's function key (`actions.play:`): kept by the action, not by a set. +fn is_action_key(id: &str) -> bool { + id.starts_with(crate::actions_cmds::PLAY_SHORTCUT_PREFIX) +} + /// A set name as given, trimmed; an error for an empty, overlong or reserved name. pub fn set_name(name: &str) -> Result { let n = name.trim(); @@ -83,15 +104,22 @@ fn saved(p: &Preferences) -> Option<&ShortcutSet> { p.shortcut_sets.get(&p.shortcut_set) } -/// Do the bindings differ from the set in use? -pub fn modified(p: &Preferences) -> bool { - let now = ShortcutSet::of(p); - match saved(p) { - Some(s) => *s != now, +/// Do these bindings differ from the set `active` of `sets` ("" or a missing name: the +/// defaults)? The actions' function keys don't count, on either side (a set saved before they +/// were left out may still hold some). The Keyboard Shortcuts dialog's "(modified)" too. +pub fn differs(sets: &BTreeMap, active: &str, shortcuts: &BTreeMap, alternates: &BTreeMap>) -> bool { + let now = ShortcutSet::from_bindings(shortcuts, alternates); + match sets.get(active) { + Some(s) => ShortcutSet::from_bindings(&s.shortcuts, &s.alternate_shortcuts) != now, None => now != ShortcutSet::default(), } } +/// Do the bindings differ from the set in use? +pub fn modified(p: &Preferences) -> bool { + differs(&p.shortcut_sets, &p.shortcut_set, &p.shortcuts, &p.alternate_shortcuts) +} + /// Before the call's binding changes: `useSet` loads a set ("" or [`DEFAULTS_NAME`]: the /// defaults) and makes it the one in use. Returns whether it changed anything. pub fn load(next: &mut Preferences, p: &Value) -> Result { @@ -123,7 +151,8 @@ pub fn store(next: &mut Preferences, p: &Value) -> Result { let name = set_name(name)?; let set: ShortcutSet = serde_json::from_value(s.clone()).map_err(|e| format!("set `{name}`: {e}"))?; set.validate().map_err(|e| format!("set `{name}`: {e}"))?; - sets.insert(name, set); + // Like `saveSet`, a set holds no action keys. + sets.insert(name, ShortcutSet::from_bindings(&set.shortcuts, &set.alternate_shortcuts)); } next.shortcut_sets = sets; changed = true; @@ -292,6 +321,57 @@ mod tests { assert!(modified(&p), "F9 differs from the defaults"); } + #[test] + fn sets_from_the_dialog_or_an_old_file_hold_no_action_keys() { + // The dialog's OK sends its sets as `sets`; an action key in one is dropped. + let mut p = prefs(); + let sets = json!({"Mine": {"shortcuts": {"edit.copy": "F9", "actions.play:Sepia": "F2"}}}); + store(&mut p, &json!({"sets": sets, "activeSet": "Mine"})).unwrap(); + assert_eq!(p.shortcut_sets["Mine"].shortcuts, BTreeMap::from([("edit.copy".to_string(), "F9".to_string())])); + // A set saved before action keys were left out still holds one: it doesn't count. + p.shortcut_sets.get_mut("Mine").unwrap().shortcuts.insert("actions.play:Old".into(), "F5".into()); + p.shortcuts = BTreeMap::from([("edit.copy".to_string(), "F9".to_string()), ("actions.play:Sepia".to_string(), "F2".to_string())]); + assert!(!modified(&p), "neither side's action keys count"); + p.shortcuts.insert("edit.copy".into(), "F8".into()); + assert!(modified(&p), "a real edit does"); + } + + #[test] + fn switching_sets_keeps_action_function_keys() { + // An action's F-key belongs to the action (as in an .atn file), not to a shortcut set. + let mut p = prefs(); + store(&mut p, &json!({"saveSet": "Mine"})).unwrap(); + p.shortcuts.insert("actions.play:Sepia".into(), "F2".into()); + assert!(!modified(&p), "an action key doesn't modify the set"); + load(&mut p, &json!({"useSet": ""})).unwrap(); + assert_eq!(p.shortcuts.get("actions.play:Sepia").map(String::as_str), Some("F2"), "kept switching to the defaults"); + load(&mut p, &json!({"useSet": "Mine"})).unwrap(); + assert_eq!(p.shortcuts.get("actions.play:Sepia").map(String::as_str), Some("F2"), "kept switching to a named set"); + store(&mut p, &json!({"saveSet": "Other"})).unwrap(); + assert!(p.shortcut_sets.values().all(|s| s.shortcuts.is_empty()), "a saved set holds no action keys"); + } + + #[test] + fn switching_sets_keeps_action_alternate_keys() { + // An action key set as an alternate (`alternateShortcuts.actions.play:`) is the + // action's too: a saved set leaves it out and switching sets keeps it. + let mut p = prefs(); + p.alternate_shortcuts.insert("edit.copy".into(), vec!["F9".into()]); + p.alternate_shortcuts.insert("actions.play:Sepia".into(), vec!["F7".into()]); + store(&mut p, &json!({"saveSet": "Mine"})).unwrap(); + assert_eq!(p.shortcut_sets["Mine"].alternate_shortcuts, BTreeMap::from([("edit.copy".to_string(), vec!["F9".to_string()])])); + load(&mut p, &json!({"useSet": ""})).unwrap(); + assert_eq!(p.alternate_shortcuts, BTreeMap::from([("actions.play:Sepia".to_string(), vec!["F7".to_string()])]), "kept switching to the defaults"); + let sets = json!({"Other": {"alternateShortcuts": {"edit.cut": ["F8"], "actions.play:Old": ["F6"]}}}); + store(&mut p, &json!({"sets": sets})).unwrap(); + load(&mut p, &json!({"useSet": "Other"})).unwrap(); + assert_eq!( + p.alternate_shortcuts, + BTreeMap::from([("actions.play:Sepia".to_string(), vec!["F7".to_string()]), ("edit.cut".to_string(), vec!["F8".to_string()])]), + "a set from the dialog brings no action key and keeps the one in use" + ); + } + #[test] fn bad_names_sets_and_params_are_errors() { let mut p = prefs(); diff --git a/crates/engine/src/smart_cmds.rs b/crates/engine/src/smart_cmds.rs deleted file mode 100644 index 6ffc89e67d..0000000000 --- a/crates/engine/src/smart_cmds.rs +++ /dev/null @@ -1,1367 +0,0 @@ -//! Smart Objects and Smart Filters: Layer › Smart Objects, Layer › Smart Filter and -//! Filter › Convert for Smart Filters. -//! -//! A smart object keeps its **source** (embedded file bytes, or a linked path) plus a -//! `transform` and a stack of smart filters; its `cache` is the rendered appearance the -//! compositors draw. The engine owns re-rendering (compose stays a pure function of the -//! document): -//! -//! ```text -//! source bytes ──decode + composite (cached by content hash)──► source image (source pixels) -//! ──transform (exact shift, or bicubic warp)──────► placed (document pixels) -//! ──each visible smart filter, blended (blend/opacity)──► filtered -//! ──filter mask mix (unfiltered ↔ filtered)───────► cache -//! ``` -//! -//! The placed stage and the result after each filter are cached too (`stack_cache`), so a -//! re-render starts from the deepest stage it shares with an earlier one. -//! -//! Converting a layer embeds it as an in-memory `.pcraft` bundle of a nested document (exactly -//! lossless, layered, any depth/model). PSD placed layers keep their source in the preserved -//! global `lnk2` block (found by uuid), and keep Photoshop's rendering until something changes, -//! so an unedited PSD still round-trips byte for byte. PSD export (`photocraft_io::smart_map`) -//! writes every smart object back as one: its source embedded in `lnk2` (a `.pcraft` source as a -//! PSB), its smart filters in `filterFX` and its filter mask in `FEid`. - -use std::sync::{Arc, Mutex}; - -use photocraft_algo::resample::translate_surface; -use photocraft_algo::transform::Homography; -use photocraft_color::{BlendMode, PixelFormat}; -use photocraft_doc::{DocId, Document, Layer, LayerContent, LayerId, LayerMask, Metadata, SmartContentsId, SmartFilter, SmartObject, SmartSource}; -use photocraft_geom::{Affine, Rect, Size}; -use photocraft_raster::Surface; -use serde_json::{Value, json}; -use stack_cache::{Stage, StageKey}; - -use crate::commands::{CommandSpec, blend_from_str, layer_param}; -use crate::jobs::JobCtx; -use crate::{EngineError, Result, Session}; - -/// An open Edit Contents document and the shared contents it updates when saved or closed. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct SmartLink { - pub child: DocId, - pub parent: DocId, - pub layer: LayerId, - pub contents_id: SmartContentsId, -} - -/// PSD placed-layer keys that describe the smart object's source; stale once we change it. -const PLACED_KEYS: [&[u8; 4]; 3] = [b"SoLd", b"PlLd", b"SoLE"]; - -/// Command id of a Photoshop smart filter PhotoCraft does not implement (kept verbatim from PSD). -pub use photocraft_io::smart_map::UNSUPPORTED_FILTER; - -fn other(msg: impl Into) -> EngineError { - EngineError::Other(msg.into()) -} - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -// ---------- source encoding / decoding ---------- - -/// Encodes a document as embedded smart-object contents: an in-memory `.pcraft` bundle. -pub fn encode_source(doc: &Document) -> Result> { - photocraft_format::save_to_bytes(doc, &Default::default()).map_err(|e| other(format!("can't encode smart object contents: {e}"))) -} - -/// Decodes smart-object contents (a `.pcraft` bundle, PSD/PSB, or any flat image format). -pub fn decode_source(file_name: &str, bytes: &[u8]) -> Result { - crate::file_cmds::import(file_name, bytes).map_err(|e| other(format!("can't read smart object contents \"{file_name}\": {e}"))) -} - -fn base_name(path: &str) -> String { - path.rsplit(['/', '\\']).next().unwrap_or(path).to_string() -} - -fn read_file(path: &str) -> Option> { - if path.is_empty() { None } else { photocraft_format::read_file(std::path::Path::new(path)).ok() } -} - -/// The source file of a smart object: embedded bytes, the PSD's embedded linked-layer data (PSD -/// import keeps the placed layer's uuid as the path), or a linked file on disk. -pub fn source_bytes(meta: &Metadata, src: &SmartSource) -> Option<(String, Arc>)> { - embedded_source_bytes(meta, src).or_else(|| match src { - SmartSource::Linked { path } => read_file(path).map(|b| (base_name(path), Arc::new(b))), - SmartSource::Embedded { .. } => None, - }) -} - -/// Memory-only resolution also covers Photoshop's embedded files addressed by a `Linked` UUID. -fn embedded_source_bytes(meta: &Metadata, src: &SmartSource) -> Option<(String, Arc>)> { - match src { - SmartSource::Embedded { file_name, bytes } => Some((file_name.clone(), bytes.clone())), - SmartSource::Linked { path } => photocraft_io::linked::find_linked_file(meta, path).map(|f| (f.file_name, Arc::new(f.bytes))), - } -} - -/// The actual path is stored in the document, not in the command's params. Recheck it before -/// reading, reusing embedded PSD bytes rather than parsing their global blocks a second time. -fn authorized_source_bytes(s: &Session, cmd: &str, meta: &Metadata, src: &SmartSource) -> Result>)>> { - if let Some(source) = embedded_source_bytes(meta, src) { - return Ok(Some(source)); - } - let SmartSource::Linked { path } = src else { return Ok(None) }; - if let Some(gate) = s.authorize { - gate(cmd, &json!({"path": path}))?; - } - Ok(read_file(path).map(|b| (base_name(path), Arc::new(b)))) -} - -/// Recheck a source's actual path before an untrusted command reads it: the path lives in the -/// document, not in the command's params. Embedded PSD UUIDs never reach the filesystem. -fn authorize_source(s: &Session, cmd: &str, doc: &Document, src: &SmartSource) -> Result<()> { - if let (Some(gate), SmartSource::Linked { path }) = (s.authorize, src) - && embedded_source_bytes(&doc.metadata, src).is_none() - { - gate(cmd, &json!({"path": path}))?; - } - Ok(()) -} - -/// A decoded, composited source in its own pixel space (`bounds` starts at the origin). -#[derive(Clone, Debug)] -pub struct SourceImage { - pub surface: Arc, - pub bounds: Rect, -} - -type CacheKey = ([u8; 32], PixelFormat); - -struct CacheEntry { - key: CacheKey, - image: SourceImage, - decodes: u32, -} - -/// Decoded source composites by content hash, most recent last. Re-filtering or re-transforming -/// a smart object never decodes its contents again. -static SOURCE_CACHE: Mutex> = Mutex::new(Vec::new()); -const CACHE_ENTRIES: usize = 32; -const CACHE_BYTES: usize = 1 << 30; - -fn cache_key(bytes: &[u8], fmt: PixelFormat) -> CacheKey { - (*blake3::hash(bytes).as_bytes(), fmt) -} - -fn cache_get(key: &CacheKey) -> Option { - let mut c = SOURCE_CACHE.lock().ok()?; - let i = c.iter().position(|e| e.key == *key)?; - let e = c.remove(i); - let img = e.image.clone(); - c.push(e); - Some(img) -} - -fn cache_put(key: CacheKey, image: SourceImage) { - let Ok(mut c) = SOURCE_CACHE.lock() else { return }; - let decodes = match c.iter().position(|e| e.key == key) { - Some(i) => c.remove(i).decodes + 1, - None => 1, - }; - c.push(CacheEntry { key, image, decodes }); - let size = |e: &CacheEntry| e.image.bounds.width() as usize * e.image.bounds.height() as usize * e.key.1.channels() * 4; - while c.len() > CACHE_ENTRIES || (c.len() > 1 && c.iter().map(size).sum::() > CACHE_BYTES) { - c.remove(0); - } -} - -/// How many times these contents were decoded at this pixel format (0 = never / evicted). -pub fn source_decode_count(bytes: &[u8], fmt: PixelFormat) -> u32 { - let key = cache_key(bytes, fmt); - SOURCE_CACHE.lock().ok().and_then(|c| c.iter().find(|e| e.key == key).map(|e| e.decodes)).unwrap_or(0) -} - -fn buffer_to_surface(buf: &photocraft_compose::Buffer, fmt: PixelFormat) -> Surface { - let n = fmt.channels(); - let mut data = vec![0.0f32; buf.px.len() * n]; - for (p, out) in buf.px.iter().zip(data.chunks_exact_mut(n)) { - photocraft_raster::from_rgba_into(&fmt, *p, out); - } - let mut s = Surface::new(fmt); - if !buf.rect.is_empty() { - s.write_region(buf.rect, &data); - } - s.prune(); - s -} - -/// An SVG source rendered at `scale` times its own size (cached per scale), so a vector smart -/// object stays sharp however it is placed. -pub fn svg_source_image(bytes: &[u8], fmt: PixelFormat, scale: f64) -> Result { - let mut h = blake3::Hasher::new(); - h.update(bytes); - h.update(&scale.to_bits().to_le_bytes()); - let key = (*h.finalize().as_bytes(), fmt); - if let Some(img) = cache_get(&key) { - return Ok(img); - } - let buf = photocraft_io::svg::rasterize(bytes, scale as f32).map_err(|e| other(format!("can't render the SVG smart object: {e}")))?; - let img = SourceImage { surface: Arc::new(buffer_to_surface(&buf, fmt)), bounds: buf.rect }; - cache_put(key, img.clone()); - Ok(img) -} - -/// The scale a vector source is rasterised at for its placement: the larger axis scale of the -/// transform in 1/16 steps (so nudges reuse the cache), between 1/16 and 64. -fn vector_scale(sm: &SmartObject) -> f64 { - let [a, b, c, d, ..] = sm.transform.m; - let k = (a * a + b * b).sqrt().max((c * c + d * d).sqrt()); - if !k.is_finite() { - return 1.0; - } - ((k * 16.0).ceil() / 16.0).clamp(1.0 / 16.0, 64.0) -} - -/// The composited source image of `bytes` in pixel format `fmt` (decoded once, then cached). -pub fn source_image(file_name: &str, bytes: &[u8], fmt: PixelFormat) -> Result { - let key = cache_key(bytes, fmt); - if let Some(img) = cache_get(&key) { - return Ok(img); - } - let doc = decode_source(file_name, bytes)?; - let buf = photocraft_compose::flatten(&doc); - let img = SourceImage { surface: Arc::new(buffer_to_surface(&buf, fmt)), bounds: doc.bounds() }; - cache_put(key, img.clone()); - Ok(img) -} - -/// Layer › Smart Objects › Stack Mode: the source's layers (or, when it holds a single group, the -/// group's layers) combined per pixel with `mode`. Cached like [`source_image`], keyed by mode. -pub fn stack_image(file_name: &str, bytes: &[u8], fmt: PixelFormat, mode: photocraft_doc::StackMode) -> Result { - use photocraft_algo::stack::Stat; - let mut key = cache_key(bytes, fmt); - key.0 = *blake3::hash(&[&key.0[..], b"stack:", mode.id().as_bytes()].concat()).as_bytes(); - if let Some(img) = cache_get(&key) { - return Ok(img); - } - let doc = decode_source(file_name, bytes)?; - let layers: &[Layer] = match doc.layers.as_slice() { - [only] if only.is_group() => only.children().unwrap_or_default(), - all => all, - }; - let bounds = doc.bounds(); - let frames: Vec> = layers - .iter() - .filter(|l| l.visible) - .map(|l| { - let mut one = l.clone(); - one.blend = BlendMode::Normal; - one.clipped = false; - photocraft_compose::render_layer(&one, bounds).px - }) - .collect(); - if frames.is_empty() { - return Err(EngineError::Other("the smart object's contents have no visible layers".into())); - } - let stat = match mode { - photocraft_doc::StackMode::Entropy => Stat::Entropy, - photocraft_doc::StackMode::Kurtosis => Stat::Kurtosis, - photocraft_doc::StackMode::Maximum => Stat::Maximum, - photocraft_doc::StackMode::Mean => Stat::Mean, - photocraft_doc::StackMode::Median => Stat::Median, - photocraft_doc::StackMode::Minimum => Stat::Minimum, - photocraft_doc::StackMode::Range => Stat::Range, - photocraft_doc::StackMode::Skewness => Stat::Skewness, - photocraft_doc::StackMode::StandardDeviation => Stat::StandardDeviation, - photocraft_doc::StackMode::Summation => Stat::Summation, - photocraft_doc::StackMode::Variance => Stat::Variance, - }; - let buf = photocraft_compose::Buffer { rect: bounds, px: photocraft_algo::stack::combine(&frames, stat) }; - let img = SourceImage { surface: Arc::new(buffer_to_surface(&buf, fmt)), bounds }; - cache_put(key, img.clone()); - Ok(img) -} - -// ---------- rendering ---------- - -/// `top` blended over `base` with `mode` at `opacity` (a smart filter's blending options). -fn blend_surfaces(base: &Surface, top: &Surface, mode: BlendMode, opacity: f32) -> Surface { - let fmt = base.format(); - let area = base.content_bounds().union(&top.content_bounds()); - let mut out = base.clone(); - if area.is_empty() { - return out; - } - let n = fmt.channels(); - let (b, t) = (base.read_region(area), top.read_region(area)); - let mut o = vec![0.0f32; b.len()]; - for ((bp, tp), op) in b.chunks_exact(n).zip(t.chunks_exact(n)).zip(o.chunks_exact_mut(n)) { - let rgba = photocraft_color::blend::composite(mode, photocraft_raster::to_rgba(&fmt, bp), photocraft_raster::to_rgba(&fmt, tp), opacity); - photocraft_raster::from_rgba_into(&fmt, rgba, op); - } - out.write_region(area, &o); - out.prune(); - out -} - -/// Runs the smart filter stack on the placed (unfiltered) content. A filter the session's -/// authority denies surfaces its error (the trusted compositor path has no gate installed, so -/// this reads ambiently only when the caller established one and it refuses). -pub fn apply_smart_filters(placed: &Surface, sm: &SmartObject, canvas: Rect) -> Result { - if !sm.filters_enabled || !sm.smart_filters.iter().any(|f| f.visible) { - return Ok(placed.clone()); - } - let mut cur = placed.clone(); - // Edge pixels repeat at the canvas ∪ placed-content edge, as for layer filters. - let extent = canvas.union(&placed.content_bounds()); - for f in sm.smart_filters.iter().filter(|f| f.visible) { - cur = apply_smart_filter(f, &cur, canvas, extent)?; - } - Ok(match sm.filter_mask.as_ref().filter(|m| m.enabled) { - Some(m) => photocraft_compose::filter_mask::mix(placed, &cur, m), - None => cur, - }) -} - -/// One visible smart filter over `cur`, blended with its blend mode and opacity. `Err` when the -/// requesting session's authority denies the filter's params: callers surface the error instead of -/// rendering (and caching) unfiltered pixels. -fn apply_smart_filter(f: &SmartFilter, cur: &Surface, canvas: Rect, extent: Rect) -> Result { - let out = match crate::filters::apply_filter_to_surface(&f.command, &f.params, cur, canvas, None, extent)? { - // Unknown ids (e.g. Photoshop filters we don't implement) leave the pixels alone. - Some(out) => out, - None => cur.clone(), - }; - if f.blend == BlendMode::Normal && f.opacity >= 1.0 { Ok(out) } else { Ok(blend_surfaces(cur, &out, f.blend, f.opacity.clamp(0.0, 1.0))) } -} - -/// The source placed in document pixels, before any smart filter. -fn place(name: &str, bytes: &[u8], fmt: PixelFormat, sm: &SmartObject) -> Result { - // A vector source is rendered at the placement's scale, and the placement is divided by - // that scale so the rendered pixels land where the source units would. - if sm.stack_mode.is_none() && sm.warp.is_none() && photocraft_io::svg::is_svg(bytes) { - let k = vector_scale(sm); - let img = svg_source_image(bytes, fmt, k)?; - return Ok(match &sm.perspective { - Some(p) => { - let [h0, h1, h2, h3, h4, h5, h6, h7, h8] = *p; - let h = Homography([h0 / k, h1 / k, h2, h3 / k, h4 / k, h5, h6 / k, h7 / k, h8]); - photocraft_algo::warp::place_source_projective(&img.surface, img.bounds, &h, None)? - } - None => { - let [a, b, c, d, e, f] = sm.transform.m; - let t = Affine { m: [a / k, b / k, c / k, d / k, e, f] }; - photocraft_algo::warp::place_source(&img.surface, img.bounds, &t, None)? - } - }); - } - let img = match sm.stack_mode { - Some(mode) => stack_image(name, bytes, fmt, mode)?, - None => source_image(name, bytes, fmt)?, - }; - // Through the warp (source space) and the transform in one pass; whole-pixel moves are exact. - Ok(match &sm.perspective { - Some(p) => photocraft_algo::warp::place_source_projective(&img.surface, img.bounds, &Homography(*p), sm.warp.as_ref())?, - None => photocraft_algo::warp::place_source(&img.surface, img.bounds, &sm.transform, sm.warp.as_ref())?, - }) -} - -/// The box of a smart object's source in source pixels (what its warp's `us`/`vs` span), or `None` -/// when the source can't be read. -pub(crate) fn source_box(doc: &Document, sm: &SmartObject) -> Option<[f64; 4]> { - let (name, bytes) = source_bytes(&doc.metadata, &sm.source)?; - let fmt = doc.pixel_format(); - let img = match sm.stack_mode { - Some(mode) => stack_image(&name, &bytes, fmt, mode), - None => source_image(&name, &bytes, fmt), - } - .ok()?; - let r = img.bounds; - Some([f64::from(r.x0), f64::from(r.y0), f64::from(r.x1), f64::from(r.y1)]) -} - -/// Renders a smart object from its source. `Ok(None)` when the source is unavailable (a missing -/// linked file, or a PSD placed layer without embedded data): callers keep the existing cache. -pub fn render(doc: &Document, sm: &SmartObject) -> Result> { - render_in(doc, sm, &JobCtx::new()) -} - -/// [`render`], reporting progress per smart filter to `ctx` and stopping between filters once it -/// is cancelled. Gives the same pixels as [`apply_smart_filters`] on the placed source, but -/// starts from the deepest stage of the stack already rendered (see [`stack_cache`]). -fn render_in(doc: &Document, sm: &SmartObject, ctx: &JobCtx) -> Result> { - // Stored filters can come from a file or another layer. Check before looking up cached - // stages too: a trusted UI render may have populated this process-wide cache earlier. - for filter in sm.smart_filters.iter().filter(|f| sm.filters_enabled && f.visible) { - crate::jobs::authorize_render_command(&filter.command, &filter.params)?; - } - let Some((name, bytes)) = source_bytes(&doc.metadata, &sm.source) else { return Ok(None) }; - let (fmt, canvas) = (doc.pixel_format(), doc.bounds()); - let placed_key = stack_cache::placed_key(&bytes, fmt, sm, canvas); - let placed = || -> Result { - if let Some(stage) = stack_cache::get(&placed_key) { - return Ok(stage); - } - let surface = place(&name, &bytes, fmt, sm)?; - // Edge pixels repeat at the canvas ∪ placed-content edge, as for layer filters. - let stage = Stage { extent: canvas.union(&surface.content_bounds()), surface }; - stack_cache::put(placed_key, &stage); - Ok(stage) - }; - let filters: Vec<&SmartFilter> = sm.smart_filters.iter().filter(|f| sm.filters_enabled && f.visible).collect(); - if filters.is_empty() { - return Ok(Some(placed()?.surface)); - } - let keys: Vec = filters - .iter() - .scan(placed_key, |key, f| { - *key = stack_cache::filter_key(key, f); - Some(*key) - }) - .collect(); - let deepest = keys.iter().enumerate().rev().find_map(|(i, key)| stack_cache::get(key).map(|stage| (i + 1, stage))); - let (done, mut cur) = match deepest { - Some(hit) => hit, - None => (0, placed()?), - }; - let n = filters.len() as f32; - for (i, (f, key)) in filters.iter().zip(&keys).enumerate().skip(done) { - ctx.check()?; - ctx.progress(i as f32 / n, ""); - cur = Stage { surface: apply_smart_filter(f, &cur.surface, canvas, cur.extent)?, extent: cur.extent }; - stack_cache::put(*key, &cur); - } - ctx.progress(1.0, ""); - Ok(Some(match sm.filter_mask.as_ref().filter(|m| m.enabled) { - Some(m) => photocraft_compose::filter_mask::mix(&placed()?.surface, &cur.surface, m), - None => cur.surface, - })) -} - -/// Bytes held by rendered smart filter stages. -pub fn stack_cache_bytes() -> usize { - stack_cache::bytes() -} - -/// Drop every rendered smart filter stage (Edit › Purge › All). Returns the bytes released. -pub fn purge_stack_cache() -> usize { - stack_cache::purge() -} - -/// The smart object's pixels below smart filter `index` (the input that filter edits): the placed -/// source and the filters under it, without the filter mask. `Ok(None)` when the source is -/// unavailable. -pub fn render_below_filter(doc: &Document, sm: &SmartObject, index: usize) -> Result> { - let mut below = sm.clone(); - below.smart_filters.truncate(index); - below.filter_mask = None; - render(doc, &below) -} - -/// Re-renders smart layer `l` (which lives in `doc`) in place. Returns false if its source is -/// unavailable (the cache is then left alone). -pub fn refresh_layer(doc: &Document, l: &mut Layer) -> Result { - refresh_layer_in(doc, l, &JobCtx::new()) -} - -fn refresh_layer_in(doc: &Document, l: &mut Layer, ctx: &JobCtx) -> Result { - let LayerContent::Smart(sm) = &mut l.content else { return Ok(false) }; - match render_in(doc, sm, ctx)? { - Some(px) => { - sm.cache = Some(px); - Ok(true) - } - None => Ok(false), - } -} - -/// Re-renders the smart object `id` of `doc`. -pub fn refresh(doc: &mut Document, id: LayerId) -> Result { - refresh_in(doc, id, &JobCtx::new()) -} - -fn refresh_in(doc: &mut Document, id: LayerId, ctx: &JobCtx) -> Result { - let mut l = doc.layer(id).cloned().ok_or(EngineError::NoLayer(id))?; - if !matches!(l.content, LayerContent::Smart(_)) { - return Err(other("the layer is not a smart object")); - } - let ok = refresh_layer_in(doc, &mut l, ctx)?; - if ok && let Some(dst) = doc.layer_mut(id) { - dst.content = l.content; - } - Ok(ok) -} - -fn refresh_or_fail(doc: &mut Document, id: LayerId) -> Result<()> { - refresh_or_fail_in(doc, id, &JobCtx::new()) -} - -fn refresh_or_fail_in(doc: &mut Document, id: LayerId, ctx: &JobCtx) -> Result<()> { - if refresh_in(doc, id, ctx)? { - Ok(()) - } else { - Err(other("the smart object's contents are unavailable (missing linked file?), so it can't be re-rendered")) - } -} - -fn smart(doc: &Document, id: LayerId) -> Result<&SmartObject> { - match &doc.layer(id).ok_or(EngineError::NoLayer(id))?.content { - LayerContent::Smart(sm) => Ok(sm), - _ => Err(other("the layer is not a smart object")), - } -} - -fn smart_mut(doc: &mut Document, id: LayerId) -> Result<&mut SmartObject> { - match &mut doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.content { - LayerContent::Smart(sm) => Ok(sm), - _ => Err(other("the layer is not a smart object")), - } -} - -fn mask_from_selection(sel: &Surface) -> LayerMask { - LayerMask { surface: sel.clone(), enabled: true, linked: true, density: 1.0, feather: 0.0 } -} - -/// Appends a smart filter (from a `filter.*` command) and re-renders. A selection becomes the -/// filter mask, as in Photoshop. Without a renderable source the filter is applied to the cached -/// pixels (and still recorded). -pub(crate) fn add_smart_filter(doc: &mut Document, id: LayerId, sf: photocraft_doc::SmartFilter, selection: Option<&Surface>) -> Result<()> { - let canvas = doc.bounds(); - let renderable = source_bytes(&doc.metadata, &smart(doc, id)?.source).is_some(); - let sm = smart_mut(doc, id)?; - if renderable { - if let Some(sel) = selection - && sm.filter_mask.is_none() - { - sm.filter_mask = Some(mask_from_selection(sel)); - } - sm.smart_filters.push(sf); - return refresh_or_fail(doc, id); - } - let bounds = selection.map(Surface::content_bounds).filter(|b| !b.is_empty()).unwrap_or(canvas); - let cache = sm.cache.as_ref().ok_or_else(|| other("smart object has no pixels"))?; - let out = crate::filters::apply_filter_to_surface(&sf.command, &sf.params, cache, bounds, selection, canvas)? - .ok_or_else(|| other(format!("unknown filter {}", sf.command)))?; - sm.cache = Some(out); - sm.smart_filters.push(sf); - Ok(()) -} - -/// Rounds float noise off a composed transform (scale 0.1 then 10 should be exactly 1), so -/// repeated transforms land back on exact, lossless placements. -pub(crate) fn snap_affine(a: Affine) -> Affine { - Affine { m: a.m.map(|v| if (v - v.round()).abs() < 1e-9 { v.round() } else { v }) } -} - -/// Where the smart object's source pixels land in the document: its projective map (Distort, -/// Perspective) or its affine transform. -pub fn placement(sm: &SmartObject) -> Homography { - sm.perspective.map(Homography).unwrap_or_else(|| affine_homography(&sm.transform)) -} - -pub(crate) fn affine_homography(a: &Affine) -> Homography { - let [a, b, c, d, e, f] = a.m; - Homography([a, c, e, b, d, f, 0.0, 0.0, 1.0]) -} - -/// Sets where the source lands. An affine map is stored exactly in `transform` (no -/// `perspective`); a projective one in `perspective`, with `transform` its affine approximation at -/// the source origin (for code that only needs scale and position). -pub(crate) fn set_placement(sm: &mut SmartObject, h: Homography) { - let m = h.0; - let n = if m[8].abs() > 1e-12 { m.map(|v| v / m[8]) } else { m }; - if n[6].abs() < 1e-12 && n[7].abs() < 1e-12 { - sm.transform = snap_affine(Affine { m: [n[0], n[3], n[1], n[4], n[2], n[5]] }); - sm.perspective = None; - return; - } - let h = Homography(n); - let (o, x, y) = (h.apply(0.0, 0.0), h.apply(1.0, 0.0), h.apply(0.0, 1.0)); - sm.transform = Affine { m: [x.0 - o.0, x.1 - o.1, y.0 - o.0, y.1 - o.1, o.0, o.1] }; - sm.perspective = Some(n); -} - -/// Composes `a` (document → document) onto the smart object's placement. -pub(crate) fn transform_placement(sm: &mut SmartObject, a: &Affine) { - match sm.perspective { - None => sm.transform = snap_affine(a.mul(&sm.transform)), - Some(_) => set_placement(sm, affine_homography(a).mul(&placement(sm))), - } -} - -/// Moves a smart object by whole pixels without re-rendering. -pub(crate) fn shift_smart(sm: &mut SmartObject, dx: i32, dy: i32) { - transform_placement(sm, &Affine::translate(dx as f64, dy as f64)); - if let Some(c) = &mut sm.cache { - *c = crate::layer_multi_cmds::shift_surface(c, dx, dy); - } - if let Some(m) = &mut sm.filter_mask { - m.surface = crate::layer_multi_cmds::shift_surface(&m.surface, dx, dy); - } -} - -/// Forget PSD placed-layer data that no longer describes the smart object (its source changed): -/// PSD export then writes fresh placed-layer blocks and embeds the new source. -fn detach_psd(l: &mut Layer) { - if let LayerContent::Smart(sm) = &mut l.content { - sm.psd_raw = None; - } - l.psd_blocks.retain(|(k, _)| !PLACED_KEYS.contains(&k)); -} - -// ---------- conversion ---------- - -/// Moves a layer (any kind) by whole pixels without re-rendering anything. -fn shift_layer(l: &mut Layer, dx: i32, dy: i32) { - if dx == 0 && dy == 0 { - return; - } - let a = Affine::translate(dx as f64, dy as f64); - let mv = |s: &Surface| translate_surface(s, dx, dy); - if let Some(m) = &mut l.mask { - m.surface = mv(&m.surface); - } - if let Some(vm) = &mut l.vector_mask { - vm.path = vm.path.transform(&a); - } - if let Some(fc) = &mut l.fill_cache { - fc.surface = mv(&fc.surface); - } - match &mut l.content { - LayerContent::Raster(s) => *s = mv(s), - LayerContent::Text(t) => { - t.transform = a.mul(&t.transform); - if let Some(c) = &mut t.cache { - *c = mv(c); - } - } - LayerContent::Shape(sh) => { - crate::vector_cmds::transform_shape(sh, &a); - if let Some(c) = &mut sh.cache { - *c = mv(c); - } - } - LayerContent::Smart(sm) => shift_smart(sm, dx, dy), - LayerContent::Group(g) => g.children.iter_mut().for_each(|c| shift_layer(c, dx, dy)), - LayerContent::Adjustment(_) | LayerContent::Fill(_) => {} - } -} - -fn subtree_bounds(l: &Layer, canvas: Rect) -> Rect { - match &l.content { - LayerContent::Group(g) => g.children.iter().map(|c| subtree_bounds(c, canvas)).fold(Rect::EMPTY, |a, b| a.union(&b)), - LayerContent::Adjustment(_) | LayerContent::Fill(_) => canvas, - _ => l.surface().map_or(Rect::EMPTY, Surface::content_bounds), - } -} - -fn any_layer(l: &Layer, f: &dyn Fn(&Layer) -> bool) -> bool { - f(l) || l.children().is_some_and(|c| c.iter().any(|c| any_layer(c, f))) -} - -/// Builds the smart-object layer replacing `l` in `doc`: the layer (with its mask, effects and -/// fill opacity) becomes a nested document cropped to its rendered bounds; visibility, opacity, -/// blend mode, clipping and label stay on the smart layer. The cache is the exact composite, so -/// the document looks the same before and after. -pub fn layer_to_smart(doc: &Document, l: &Layer) -> Result { - let background = doc.is_background(l.id); - let mut inner = l.clone(); - inner.visible = true; - inner.opacity = 1.0; - inner.clipped = false; - inner.locks = Default::default(); - if !(inner.is_group() && inner.blend == BlendMode::PassThrough) { - inner.blend = BlendMode::Normal; - } - if background { - inner.name = "Layer 0".into(); - } - let mut out = layers_to_smart(doc, &l.name, vec![inner])?; - if background { - out.name = "Layer 0".into(); - } - out.visible = l.visible; - out.opacity = l.opacity; - out.blend = if l.blend == BlendMode::PassThrough { BlendMode::Normal } else { l.blend }; - out.clipped = l.clipped; - out.label = l.label; - if !background { - out.locks = l.locks; - } - Ok(out) -} - -/// Builds a smart-object layer named `name` whose nested document holds `layers` (bottom to top) -/// at its top level, as Photoshop does when several layers are converted: no wrapper group. The -/// nested document is cropped to the rendered bounds and the cache is the exact composite (the -/// same as the layers inside a pass-through group). The smart layer is visible, opaque and Normal. -pub fn layers_to_smart(doc: &Document, name: &str, layers: Vec) -> Result { - let canvas = doc.bounds(); - let mut sub = Document::new(format!("{name}.pcraft"), doc.size, doc.mode, doc.depth); - sub.resolution_dpi = doc.resolution_dpi; - sub.icc_profile = doc.icc_profile.clone(); - sub.global_light = doc.global_light; - // Pattern fills and pattern layer effects reference document-level patterns by ID. - // Preserve those resources in the embedded document before rendering or saving it. - sub.patterns = doc.patterns.clone(); - if layers.iter().any(|l| any_layer(l, &|x| matches!(x.content, LayerContent::Smart(_)))) { - // Nested PSD placed layers find their embedded files here. - sub.metadata.psd_global_blocks = doc.metadata.psd_global_blocks.clone(); - } - let has_fx = layers.iter().any(|l| any_layer(l, &|x| x.effects.enabled && !x.effects.items.is_empty())); - let region = layers.iter().map(|l| subtree_bounds(l, canvas)).fold(canvas, |a, b| a.union(&b)).inflate(if has_fx { 256 } else { 0 }); - sub.layers = layers; - - let buf = photocraft_compose::render(&sub, region); - let w = region.width() as usize; - let mut b = Rect::EMPTY; - for (i, p) in buf.px.iter().enumerate() { - if p[3] > 0.0 { - let (x, y) = (region.x0 + (i % w) as i32, region.y0 + (i / w) as i32); - b = b.union(&Rect::new(x, y, x + 1, y + 1)); - } - } - if b.is_empty() { - return Err(other("the layer is empty, so there is nothing to convert")); - } - let fmt = doc.pixel_format(); - let mut cache = buffer_to_surface(&buf, fmt); - cache = photocraft_algo::resample::crop_surface(&cache, b); - cache.prune(); - - sub.layers.iter_mut().for_each(|l| shift_layer(l, -b.x0, -b.y0)); - sub.size = Size::new(b.width(), b.height()); - let bytes = encode_source(&sub)?; - // Seed the decode cache with the composite we already have. - cache_put(cache_key(&bytes, fmt), SourceImage { surface: Arc::new(translate_surface(&cache, -b.x0, -b.y0)), bounds: sub.bounds() }); - - let source = SmartSource::Embedded { file_name: sub.name.clone(), bytes: Arc::new(bytes) }; - Ok(Layer::new(name, LayerContent::Smart(SmartObject::new(source, Affine::translate(b.x0 as f64, b.y0 as f64), Some(cache))))) -} - -fn convert(s: &mut Session, p: &Value) -> Result { - let ids = match p.get("layer").and_then(Value::as_u64) { - Some(id) => vec![LayerId(id)], - None => crate::layer_multi_cmds::selected(s), - }; - let new_id = s.edit("Convert to Smart Object", |doc, active| { - let ids = crate::layer_multi_cmds::top_level(doc, &ids); - let top = *ids.last().ok_or_else(|| other("no layer selected"))?; - let l = doc.layer(top).ok_or(EngineError::NoLayer(top))?; - let so = if ids.len() == 1 { - layer_to_smart(doc, l)? - } else { - let children = ids.iter().map(|id| doc.layer(*id).cloned().ok_or(EngineError::NoLayer(*id))).collect::>>()?; - layers_to_smart(doc, &l.name, children)? - }; - let new_id = so.id; - // As with Group Layers, place the result in the top selected root's parent. Insert - // before removal so paths shifting in other parents cannot discard unselected content. - doc.insert_above(Some(top), so); - for id in ids { - doc.remove(id).ok_or(EngineError::NoLayer(id))?; - } - *active = Some(new_id); - Ok(new_id) - })?; - crate::layer_multi_cmds::reselect(s, vec![new_id], Some(new_id)); - Ok(json!({"layer": new_id.0})) -} - -// ---------- contents ---------- - -fn via_copy(s: &mut Session, p: &Value) -> Result { - let id = layer_param(s, p)?; - s.edit("New Smart Object via Copy", |doc, active| { - let src = doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let mut copy = src.duplicate(); - copy.name = doc.copy_name(&src.name); - if let LayerContent::Smart(sm) = &mut copy.content { - // Independent contents: resolve to embedded bytes and drop the shared PSD uuid. - sm.contents_id = SmartContentsId::fresh(); - if let Some((file_name, bytes)) = source_bytes(&doc.metadata, &sm.source) { - sm.source = SmartSource::Embedded { file_name, bytes }; - } - } else { - return Err(other("the layer is not a smart object")); - } - detach_psd(&mut copy); - let new_id = doc.insert_above(Some(id), copy); - *active = Some(new_id); - Ok(json!({"layer": new_id.0})) - }) -} - -fn path_param<'a>(cmd: &str, p: &'a Value) -> Result<&'a str> { - p.get("path").and_then(Value::as_str).filter(|s| !s.is_empty()).ok_or_else(|| bad(cmd, "pass `path`")) -} - -/// Instances sharing contents in this document, including those inside groups. -fn instances(doc: &Document, contents_id: SmartContentsId) -> Vec { - doc.walk().into_iter().filter_map(|(_, _, l)| matches!(&l.content, LayerContent::Smart(sm) if sm.contents_id == contents_id).then_some(l.id)).collect() -} - -/// Swaps shared contents, keeping each instance's transform and filters, and re-renders. -fn set_source(s: &mut Session, p: &Value, label: &str, keep_psd: bool, make: impl FnOnce(&Metadata, &SmartSource) -> Result) -> Result { - let id = layer_param(s, p)?; - let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; - let new = make(&doc.metadata, &smart(doc, id)?.source)?; - set_shared_source(s, id, label, keep_psd, new) -} - -// Keep the shared-instance transaction out of the per-command source-builder instantiations. -fn set_shared_source(s: &mut Session, id: LayerId, label: &str, keep_psd: bool, new: SmartSource) -> Result { - let (parent, contents_id, changed) = s.edit(label, |doc, _| { - let contents_id = smart(doc, id)?.contents_id; - let changed = instances(doc, contents_id); - for &instance in &changed { - smart_mut(doc, instance)?.source = new.clone(); - if !keep_psd { - detach_psd(doc.layer_mut(instance).ok_or(EngineError::NoLayer(instance))?); - } - refresh_or_fail(doc, instance)?; - } - Ok((doc.id, contents_id, changed)) - })?; - // An old editor must not overwrite newly replaced/relinked contents on close. Keep its - // document open, but the next Edit Contents opens the replacement source. - s.smart_links.retain(|l| l.parent != parent || l.contents_id != contents_id); - // Every instance changed, not only `layer`: list them, so a caller doesn't repeat the - // command once per copy (#4075). - Ok(json!({"layer": id.0, "instances": changed.iter().map(|l| l.0).collect::>()})) -} - -fn replace_contents(s: &mut Session, p: &Value) -> Result { - let path = path_param("layer.smartObjects.replaceContents", p)?.to_string(); - let bytes = photocraft_format::read_file(std::path::Path::new(&path)).map_err(|e| other(format!("can't read {path}: {e}")))?; - let id = layer_param(s, p)?; - replace_contents_bytes(s, id, &base_name(&path), bytes) -} - -/// Replace Contents from a file's name and bytes: what the desktop and web file pickers hand over. -pub fn replace_contents_bytes(s: &mut Session, id: LayerId, file_name: &str, bytes: Vec) -> Result { - decode_source(file_name, &bytes)?; // fail before touching the document - set_shared_source(s, id, "Replace Contents", false, SmartSource::Embedded { file_name: file_name.to_string(), bytes: Arc::new(bytes) }) -} - -/// The file name and bytes of smart object `id`'s contents. -pub fn contents_of(doc: &Document, id: LayerId) -> Result<(String, Arc>)> { - source_bytes(&doc.metadata, &smart(doc, id)?.source).ok_or_else(|| other("the smart object's contents are unavailable")) -} - -/// Writes the smart object's file as it is. To a path with another extension (a `.png` for -/// `.psb` or `.pcraft` contents) the contents are opened and exported in that format instead. -fn export_contents(s: &mut Session, p: &Value) -> Result { - let path = path_param("layer.smartObjects.exportContents", p)?; - let id = layer_param(s, p)?; - let (name, bytes) = contents_of(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?; - let ext = |f: &str| { - let e = std::path::Path::new(f).extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase); - e.map(|e| match e.as_str() { - "jpeg" => "jpg".to_string(), - "tiff" => "tif".to_string(), - _ => e, - }) - }; - if ext(path).is_none() || ext(path) == ext(&name) { - crate::file_cmds::write_file(path, &bytes)?; - return Ok(json!({"path": path, "fileName": name, "bytes": bytes.len(), "converted": false})); - } - let contents = decode_source(&name, &bytes)?; - let r = photocraft_io::export(&contents, path, &Default::default()).map_err(|e| other(format!("can't export the contents as {}: {e}", base_name(path))))?; - crate::file_cmds::write_file(path, &r.bytes)?; - Ok(json!({"path": path, "fileName": name, "bytes": r.bytes.len(), "converted": true, "warnings": r.warnings})) -} - -fn edit_contents(s: &mut Session, p: &Value) -> Result { - let id = layer_param(s, p)?; - let st = s.active().ok_or(EngineError::NoDocument)?; - let parent = st.doc.id; - let contents_id = smart(&st.doc, id)?.contents_id; - // Already open for editing: switch to that document rather than opening another copy. - if let Some(index) = - s.smart_links.iter().find(|l| l.parent == parent && l.contents_id == contents_id).and_then(|l| s.documents().iter().position(|d| d.doc.id == l.child)) - { - s.set_active(index); - return Ok(json!({"document": index, "parentLayer": id.0})); - } - let source = &smart(&st.doc, id)?.source; - let (name, bytes) = authorized_source_bytes(s, "layer.smartObjects.editContents", &st.doc.metadata, source)? - .ok_or_else(|| other("the smart object's contents are unavailable"))?; - let mut child = decode_source(&name, &bytes)?; - // Bundles keep their document id; each open copy needs its own. - child.id = DocId::fresh(); - child.name = name; - let index = s.add_document(child, None); - // Admission may replace an ID already owned by another open document. - let child_id = s.documents().get(index).ok_or(EngineError::NoDocument)?.doc.id; - s.smart_links.retain(|l| l.child != child_id); - s.smart_links.push(SmartLink { child: child_id, parent, layer: id, contents_id }); - Ok(json!({"document": index, "parentLayer": id.0})) -} - -/// Writes an Edit Contents document back into all its parent instances (one undoable step in the -/// parent) and marks it saved. Returns false if `index` isn't an Edit Contents document. -pub fn commit_child(s: &mut Session, index: usize) -> Result { - let Some(st) = s.docs.get(index) else { return Ok(false) }; - let child = st.doc.clone(); - let Some(link) = s.smart_links.iter().find(|l| l.child == child.id).copied() else { return Ok(false) }; - let parent = s.docs.iter().position(|d| d.doc.id == link.parent).ok_or_else(|| other("the smart object's document was closed"))?; - let bytes = Arc::new(encode_source(&child)?); - let prev = s.active; - s.active = Some(parent); - let r = s.edit("Edit Contents", |doc, _| { - let ids = instances(doc, link.contents_id); - if ids.is_empty() { - return Err(other("the smart object's instances were removed")); - } - for id in ids { - let sm = smart_mut(doc, id)?; - let name = match &sm.source { - SmartSource::Embedded { file_name, .. } => file_name.as_str(), - SmartSource::Linked { .. } => child.name.as_str(), - }; - let stem = name.rsplit_once('.').map_or(name, |(stem, _)| stem); - sm.source = SmartSource::Embedded { file_name: format!("{stem}.pcraft"), bytes: bytes.clone() }; - detach_psd(doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?); - refresh_or_fail(doc, id)?; - } - Ok(()) - }); - s.active = prev; - r?; - if let Some(st) = s.docs.get_mut(index) { - st.saved_revision = st.revision; - } - Ok(true) -} - -/// Called before a document closes: an edited Edit Contents document updates its parent. -pub(crate) fn on_close(s: &mut Session, index: usize) { - let Some(st) = s.docs.get(index) else { return }; - let id = st.doc.id; - if st.is_dirty() && s.smart_links.iter().any(|l| l.child == id) { - let _ = commit_child(s, index); - } - s.smart_links.retain(|l| l.child != id && l.parent != id); -} - -fn is_smart_child(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - if s.smart_links.iter().any(|l| l.child == d.doc.id) { Ok(()) } else { Err("this document isn't smart object contents".into()) } -} - -fn update_all(s: &mut Session) -> Result { - let ids: Vec = s - .active() - .ok_or(EngineError::NoDocument)? - .doc - .walk() - .into_iter() - .filter(|(_, _, l)| matches!(&l.content, LayerContent::Smart(sm) if matches!(sm.source, SmartSource::Linked { .. }))) - .map(|(_, _, l)| l.id) - .collect(); - let mut updated = Vec::new(); - s.edit("Update All Modified Content", |doc, _| { - for id in &ids { - if refresh(doc, *id)? { - updated.push(id.0); - } - } - Ok(()) - })?; - Ok(json!({"updated": updated})) -} - -// ---------- smart filter editing ---------- - -/// Edits the smart filters of a smart object and re-renders it. The edit itself is instant, so its -/// errors (a bad index, say) are reported at once; the re-render runs as a background job when -/// started with `Session::start`, so the window keeps drawing (with progress, cancellable) while -/// a long filter stack recomputes. -fn edit_filters(s: &mut Session, p: &Value, label: &str, f: impl FnOnce(&mut SmartObject) -> Result<()>) -> Result { - let id = layer_param(s, p)?; - let mut edited = smart(&s.active().ok_or(EngineError::NoDocument)?.doc, id)?.clone(); - f(&mut edited)?; - crate::jobs::edit_job( - s, - label, - move |doc, _, ctx| { - *smart_mut(doc, id)? = edited; - refresh_or_fail_in(doc, id, ctx) - }, - move |()| json!({"layer": id.0}), - ) -} - -fn filter_index(cmd: &str, p: &Value, sm: &SmartObject) -> Result { - let n = sm.smart_filters.len(); - if n == 0 { - return Err(other("the smart object has no smart filters")); - } - match p.get("index") { - None => Ok(n - 1), - Some(v) => v.as_u64().map(|i| i as usize).filter(|i| *i < n).ok_or_else(|| bad(cmd, format!("`index` must be 0..{n} (0 = bottom filter)"))), - } -} - -fn set_filter_visible(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.smartFilter.setVisible"; - edit_filters(s, p, "Smart Filter Visibility", |sm| { - let i = filter_index(CMD, p, sm)?; - let f = &mut sm.smart_filters[i]; - f.visible = p.get("visible").and_then(Value::as_bool).unwrap_or(!f.visible); - Ok(()) - }) -} - -fn set_filter_params(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.smartFilter.setParams"; - let new = p.get("params").cloned().ok_or_else(|| bad(CMD, "pass `params` (merged into the filter's parameters)"))?; - let authorize = s.authorize; - edit_filters(s, p, "Edit Smart Filter", |sm| { - let i = filter_index(CMD, p, sm)?; - let f = &mut sm.smart_filters[i]; - if f.command == photocraft_io::smart_map::UNSUPPORTED_FILTER { - return Err(bad(CMD, "this smart filter isn't implemented in PhotoCraft: it is kept as is (it can be hidden, moved or deleted)")); - } - match (&mut f.params, new) { - (Value::Object(old), Value::Object(n)) => old.extend(n), - (slot, n) => *slot = n, - } - // Authorize the underlying command with the complete merged params, including paths - // already stored in the document, before committing the edit or starting a worker. - if let Some(gate) = authorize { - gate(&f.command, &f.params)?; - } - Ok(()) - }) -} - -fn blending_options(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.smartFilter.blendingOptions"; - let blend = match p.get("blend").and_then(Value::as_str) { - Some(b) => Some(blend_from_str(b).filter(|m| *m != BlendMode::PassThrough).ok_or_else(|| bad(CMD, format!("unknown blend mode `{b}`")))?), - None => None, - }; - let opacity = p.get("opacity").and_then(Value::as_f64).map(|o| if o > 1.0 { o / 100.0 } else { o } as f32); - edit_filters(s, p, "Smart Filter Blending Options", |sm| { - let i = filter_index(CMD, p, sm)?; - let f = &mut sm.smart_filters[i]; - if let Some(b) = blend { - f.blend = b; - } - if let Some(o) = opacity { - f.opacity = o.clamp(0.0, 1.0); - } - Ok(()) - }) -} - -fn delete_filter(s: &mut Session, p: &Value) -> Result { - edit_filters(s, p, "Delete Smart Filter", |sm| { - let i = filter_index("layer.smartFilter.delete", p, sm)?; - sm.smart_filters.remove(i); - Ok(()) - }) -} - -fn move_filter(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.smartFilter.move"; - edit_filters(s, p, "Move Smart Filter", |sm| { - let i = filter_index(CMD, p, sm)?; - let n = sm.smart_filters.len(); - let to = p.get("to").and_then(Value::as_u64).map(|t| t as usize).filter(|t| *t < n).ok_or_else(|| bad(CMD, format!("`to` must be 0..{n}")))?; - let f = sm.smart_filters.remove(i); - sm.smart_filters.insert(to, f); - Ok(()) - }) -} - -/// Dragging smart filters onto another smart object in the Layers panel (#4271): they move there, -/// or are copied with `copy` (⌥-drag in Photoshop). One filter (`filter`) lands on top of the -/// target's filters; all of them (the Smart Filters row) are added on top too, with the filter -/// mask when the target has none. Both smart objects are re-rendered in one undo step. -fn transfer_filters(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.smartFilter.transfer"; - let id_of = |key: &str| p.get(key).and_then(Value::as_u64).map(LayerId).ok_or_else(|| bad(CMD, format!("`{key}` must be a layer id"))); - let (from, to) = (id_of("from")?, id_of("to")?); - if from == to { - return Err(bad(CMD, "the smart filters are already on that layer")); - } - // The other `layer.smartFilter.*` commands name a filter `index`; here a guessed `index` - // would otherwise be dropped and every filter moved. - if p.get("index").is_some() { - return Err(bad(CMD, "name the filter with `filter` (no `filter`: all of them)")); - } - let copy = p.get("copy").and_then(Value::as_bool).unwrap_or(false); - let doc = &s.active().ok_or(EngineError::NoDocument)?.doc; - let src = smart(doc, from)?; - smart(doc, to).map_err(|_| bad(CMD, "smart filters can only be dropped on a smart object"))?; - let n = src.smart_filters.len(); - if n == 0 { - return Err(other("the smart object has no smart filters")); - } - let index = match p.get("filter") { - None | Some(Value::Null) => None, - Some(v) => Some(v.as_u64().map(|i| i as usize).filter(|i| *i < n).ok_or_else(|| bad(CMD, format!("`filter` must be 0..{n} (0 = bottom filter)")))?), - }; - let (filters, mask) = match index { - Some(i) => (vec![src.smart_filters[i].clone()], None), - None => (src.smart_filters.clone(), src.filter_mask.clone()), - }; - let label = match (copy, index.is_some()) { - (true, true) => "Copy Smart Filter", - (true, false) => "Copy Smart Filters", - (false, true) => "Move Smart Filter", - (false, false) => "Move Smart Filters", - }; - crate::jobs::edit_job( - s, - label, - move |doc, _, ctx| { - let dst = smart_mut(doc, to)?; - dst.smart_filters.extend(filters); - if dst.filter_mask.is_none() { - dst.filter_mask = mask; - } - refresh_or_fail_in(doc, to, ctx)?; - if !copy { - let src = smart_mut(doc, from)?; - match index { - Some(i) => { - src.smart_filters.remove(i); - } - None => { - src.smart_filters.clear(); - src.filter_mask = None; - } - } - refresh_or_fail_in(doc, from, ctx)?; - } - Ok(()) - }, - move |()| json!({"from": from.0, "to": to.0}), - ) -} - -// ---------- enablement ---------- - -fn active_smart(s: &Session) -> std::result::Result<&SmartObject, String> { - let d = s.active().ok_or("no document open")?; - let id = d.active_layer.ok_or("no active layer")?; - match &d.doc.layer(id).ok_or("no active layer")?.content { - LayerContent::Smart(sm) => Ok(sm), - other => Err(format!("the active layer is {} {} layer, not a smart object", other.article(), other.kind_name())), - } -} -fn has_smart(s: &Session) -> std::result::Result<(), String> { - active_smart(s).map(|_| ()) -} -fn has_linked(s: &Session) -> std::result::Result<(), String> { - match active_smart(s)?.source { - SmartSource::Linked { .. } => Ok(()), - _ => Err("the smart object is embedded".into()), - } -} -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} -fn has_smart_filters(s: &Session) -> std::result::Result<(), String> { - if active_smart(s)?.smart_filters.is_empty() { Err("the smart object has no smart filters".into()) } else { Ok(()) } -} -fn has_filter_mask(s: &Session) -> std::result::Result<(), String> { - active_smart(s)?.filter_mask.as_ref().map(|_| ()).ok_or_else(|| "the smart object has no filter mask".into()) -} -fn convertible(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - let id = d.active_layer.ok_or("no active layer")?; - d.doc.layer(id).map(|_| ()).ok_or_else(|| "no active layer".into()) -} -fn not_smart(s: &Session) -> std::result::Result<(), String> { - convertible(s)?; - if active_smart(s).is_ok() { Err("the layer is already a smart object".into()) } else { Ok(()) } -} - -macro_rules! spec { - ($id:literal, $label:literal, $menu:expr, $params:literal, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: None, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -const SO: &[&str] = &["Layer", "Smart Objects"]; -const SF: &[&str] = &["Layer", "Smart Filter"]; - -pub fn specs() -> Vec { - vec![ - spec!( - "layer.smartObjects.convertToSmartObject", - "Convert to Smart Object", - SO, - r##"{"layer":id?} (default: combine selected layers)"##, - convertible, - convert - ), - spec!( - "filter.convertForSmartFilters", - "Convert for Smart Filters", - &["Filter"], - r##"{"layer":id?} (default: combine selected layers)"##, - not_smart, - convert - ), - spec!("layer.smartObjects.newSmartObjectViaCopy", "New Smart Object via Copy", SO, r##"{"layer":id?}"##, has_smart, via_copy), - spec!("layer.smartObjects.rasterize", "Rasterize", SO, r##"{"layer":id?}"##, has_smart, |s, p| s.execute("layer.rasterize.smartObject", p.clone())), - spec!( - "layer.smartObjects.editContents", - "Edit Contents", - SO, - r##"{"layer":id?} → opens the contents as a new document; saving (layer.smartObjects.saveContents) or closing it updates the smart object"##, - has_smart, - edit_contents - ), - spec!("layer.smartObjects.saveContents", "Save Contents", &[], "{} (in an Edit Contents document)", is_smart_child, |s, _| { - let i = s.active_index().ok_or(EngineError::NoDocument)?; - Ok(json!({"updated": commit_child(s, i)?})) - }), - spec!("layer.smartObjects.replaceContents", "Replace Contents…", SO, r##"{"layer":id?,"path":str}"##, has_smart, replace_contents), - spec!( - "layer.smartObjects.exportContents", - "Export Contents…", - SO, - r##"{"layer":id?,"path":str (another extension than the contents' own exports them in that format)}"##, - has_smart, - export_contents - ), - spec!("layer.smartObjects.relinkToFile", "Relink to File…", SO, r##"{"layer":id?,"path":str}"##, has_smart, |s, p| { - let path = path_param("layer.smartObjects.relinkToFile", p)?.to_string(); - set_source(s, p, "Relink to File", false, |_, _| Ok(SmartSource::Linked { path })) - }), - spec!("layer.smartObjects.updateModifiedContent", "Update Modified Content", SO, r##"{"layer":id?}"##, has_linked, |s, p| { - let id = layer_param(s, p)?; - s.edit("Update Modified Content", |doc, _| refresh_or_fail(doc, id))?; - Ok(json!({"layer": id.0})) - }), - spec!( - "layer.smartObjects.updateChanged", - "Update Changed Linked Content", - &[], - "{} — re-render the active document's linked smart objects whose files changed on disk since they were last seen (size or modification time); the desktop app does this by itself every two seconds, as Photoshop does while a document is open. Files seen for the first time are only remembered. One Update Modified Content undo step per file → {updated: [layer ids]}", - |s| s.active().map(|_| ()).ok_or_else(|| "no document open".into()), - crate::link_watch::update_changed - ), - spec!( - "layer.smartObjects.updateAllModifiedContent", - "Update All Modified Content", - SO, - "{}", - |s| s.active().map(|_| ()).ok_or_else(|| "no document open".into()), - |s, _| update_all(s) - ), - spec!( - "layer.smartObjects.convertToLayers", - "Convert to Layers", - SO, - r##"{"layer":id?} (contents unpacked at the placement: one layer, or a group named after the smart object; smart filters are discarded)"##, - has_smart, - unpack::convert_to_layers - ), - spec!("layer.smartObjects.convertToEmbedded", "Convert to Embedded", SO, r##"{"layer":id?}"##, has_linked, |s, p| { - set_source(s, p, "Convert to Embedded", true, |meta, src| { - let (file_name, bytes) = source_bytes(meta, src).ok_or_else(|| other("the linked file can't be read"))?; - Ok(SmartSource::Embedded { file_name, bytes }) - }) - }), - spec!( - "layer.smartObjects.convertToLinked", - "Convert to Linked…", - SO, - r##"{"layer":id?,"path":str} (writes the contents there)"##, - has_smart, - |s, p| { - let path = path_param("layer.smartObjects.convertToLinked", p)?.to_string(); - set_source(s, p, "Convert to Linked", false, |meta, src| { - let (_, bytes) = source_bytes(meta, src).ok_or_else(|| other("the smart object's contents are unavailable"))?; - crate::file_cmds::write_file(&path, &bytes)?; - Ok(SmartSource::Linked { path }) - }) - } - ), - // Smart filters - spec!( - "layer.smartFilter.disableSmartFilters", - "Disable Smart Filters", - SF, - r##"{"layer":id?,"enabled":bool? (default: toggle)}"##, - has_smart_filters, - |s, p| { - edit_filters(s, p, "Disable Smart Filters", |sm| { - sm.filters_enabled = p.get("enabled").and_then(Value::as_bool).unwrap_or(!sm.filters_enabled); - Ok(()) - }) - } - ), - spec!("layer.smartFilter.deleteFilterMask", "Delete Filter Mask", SF, r##"{"layer":id?}"##, has_filter_mask, |s, p| { - edit_filters(s, p, "Delete Filter Mask", |sm| { - sm.filter_mask = None; - Ok(()) - }) - }), - spec!( - "layer.smartFilter.disableFilterMask", - "Disable Filter Mask", - SF, - r##"{"layer":id?,"enabled":bool? (default: toggle)}"##, - has_filter_mask, - |s, p| { - edit_filters(s, p, "Disable Filter Mask", |sm| { - if let Some(m) = &mut sm.filter_mask { - m.enabled = p.get("enabled").and_then(Value::as_bool).unwrap_or(!m.enabled); - } - Ok(()) - }) - } - ), - spec!( - "layer.smartFilter.blendingOptions", - "Blending Options…", - SF, - r##"{"layer":id?,"index":u32? (0 = bottom; default top),"blend":str?,"opacity":0..1?}"##, - has_smart_filters, - blending_options - ), - spec!("layer.smartFilter.clearSmartFilters", "Clear Smart Filters", SF, r##"{"layer":id?}"##, has_smart_filters, |s, p| { - edit_filters(s, p, "Clear Smart Filters", |sm| { - sm.smart_filters.clear(); - sm.filter_mask = None; - Ok(()) - }) - }), - spec!( - "layer.smartFilter.setVisible", - "Show/Hide Smart Filter", - &[], - r##"{"layer":id?,"index":u32?,"visible":bool? (default: toggle)}"##, - has_smart_filters, - set_filter_visible - ), - spec!( - "layer.smartFilter.setParams", - "Edit Smart Filter", - &[], - r##"{"layer":id?,"index":u32?,"params":{…} (merged)}"##, - has_smart_filters, - set_filter_params - ), - spec!("layer.smartFilter.delete", "Delete Smart Filter", &[], r##"{"layer":id?,"index":u32?}"##, has_smart_filters, delete_filter), - spec!("layer.smartFilter.move", "Move Smart Filter", &[], r##"{"layer":id?,"index":u32?,"to":u32}"##, has_smart_filters, move_filter), - spec!( - "layer.smartFilter.transfer", - "Move or Copy Smart Filters", - &[], - r##"{"from":id,"to":id,"filter":u32? (default: all, with the filter mask),"copy":bool=false}"##, - has_doc, - transfer_filters - ), - ] -} - -mod stack_cache; -mod unpack; - -#[cfg(test)] -mod tests; diff --git a/crates/engine/src/smartselect_cmds.rs b/crates/engine/src/smartselect_cmds.rs deleted file mode 100644 index 380b6a7200..0000000000 --- a/crates/engine/src/smartselect_cmds.rs +++ /dev/null @@ -1,1044 +0,0 @@ -//! Smart selection commands: Quick Selection, Object Selection, Select Subject, Select and Mask -//! (Refine Edge) and Focus Area. The algorithms live in `photocraft_algo::segment` and -//! `photocraft_algo::matting`; these commands pick the pixels to analyse (active layer or -//! composite), run them, and store the result as the selection (or a mask / new layer). - -use photocraft_algo::matting::{self, RefineParams}; -use photocraft_algo::segment::{Sampler, SurfaceSampler, focus, grabcut, quick, subject}; -use photocraft_algo::selection::{Region, SelectionMode, combine_region}; -use photocraft_doc::{Document, Layer, LayerContent, LayerId, LayerMask}; -use photocraft_geom::Rect; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} -fn has_selection_or_mask(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or_else(|| "no document open".to_string())?; - if d.doc.selection.is_some() { - return Ok(()); - } - if let Some(l) = d.active_layer.and_then(|id| d.doc.layer(id)) - && l.mask.is_some() - { - return Ok(()); - } - Err("no selection or layer mask".into()) -} - -fn f(p: &Value, k: &str, d: f32) -> f32 { - p.get(k).and_then(Value::as_f64).map_or(d, |v| v as f32) -} -fn b(p: &Value, k: &str, d: bool) -> bool { - p.get(k).and_then(Value::as_bool).unwrap_or(d) -} -fn mode(p: &Value, default: &str) -> SelectionMode { - let m = p.get("mode").and_then(Value::as_str).unwrap_or(default); - SelectionMode::parse(if m == "new" { "replace" } else { m }) -} -fn bad(cmd: &str, msg: &str) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -/// Composite of all visible layers. -struct CompositeSampler<'a>(&'a Document); - -impl Sampler for CompositeSampler<'_> { - fn rgba(&self, r: Rect) -> Vec<[f32; 4]> { - photocraft_compose::render(self.0, r).px - } -} - -/// Runs `f` with a sampler over the active layer (or the composite with `all_layers`, or when the -/// active layer has no pixels). -fn with_sampler(s: &Session, all_layers: bool, f: impl FnOnce(&dyn Sampler, &Document) -> R) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - Ok(with_doc_sampler(&d.doc, d.active_layer, all_layers, f)) -} - -/// Runs `f` with a sampler over `layer`'s pixels in `doc` (or the composite with `all_layers`, or -/// when the layer has no pixels). -pub(crate) fn with_doc_sampler(doc: &Document, layer: Option, all_layers: bool, f: impl FnOnce(&dyn Sampler, &Document) -> R) -> R { - match (all_layers, layer.and_then(|id| doc.layer(id)).and_then(Layer::surface)) { - (false, Some(surf)) => f(&SurfaceSampler(surf), doc), - _ => f(&CompositeSampler(doc), doc), - } -} - -/// Stores `region` combined with the current selection by `m` as one history step. -/// Finding nothing never destroys work: the selection (and history) is left alone and the -/// caller gets `changed: false`, whatever the mode. (Select Subject and Object Selection run -/// heuristics that often find nothing on busy photos; clearing the selection then looks like -/// a dead button.) -fn apply(s: &mut Session, label: &str, region: Option<&Region>, m: SelectionMode) -> Result { - if region.is_none() { - // Nothing found: leave the selection (and history) alone. - let selected = s.active().ok_or(EngineError::NoDocument)?.doc.selection.is_some(); - return Ok(json!({ "selected": selected, "changed": false })); - } - let bbox = region.map(|r| r.bbox); - let selected = s.edit(label, |doc, _| { - doc.selection = combine_region(doc.selection.as_ref(), region, m); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "selected": selected, "changed": true, "bounds": bbox.map(|r| [r.x0, r.y0, r.width() as i32, r.height() as i32]) })) -} - -fn points(p: &Value) -> Vec<(f32, f32)> { - p.get("points") - .and_then(Value::as_array) - .map(|a| a.iter().filter_map(|q| Some((q.get(0)?.as_f64()? as f32, q.get(1)?.as_f64()? as f32))).collect()) - .unwrap_or_default() -} - -/// Auto-Enhance for Quick Selection: a light edge refinement that flows the edge towards image -/// edges and removes blockiness. -const ENHANCE: RefineParams = RefineParams { radius: 4.0, smart_radius: true, smooth: 15.0, feather: 0.0, contrast: 25.0, shift_edge: 0.0 }; - -fn quick_selection(s: &mut Session, p: &Value) -> Result { - let pts = points(p); - if pts.is_empty() { - return Err(bad("select.quick", "needs at least one point")); - } - let size = f(p, "size", 30.0).clamp(1.0, 5000.0); - let m = match p.get("mode").and_then(Value::as_str).unwrap_or("add") { - "subtract" => SelectionMode::Subtract, - "replace" | "new" => SelectionMode::Replace, - _ => SelectionMode::Add, - }; - let enhance = b(p, "enhanceEdge", false); - let region = with_sampler(s, b(p, "sampleAllLayers", false), |smp, doc| { - let canvas = doc.bounds(); - let r = quick::quick_select(smp, canvas, &pts, size)?; - if enhance { matting::refine_mask(smp, &matting::region_reader(&r), r.bbox, canvas, &ENHANCE) } else { Some(r) } - })?; - apply(s, "Quick Selection", region.as_ref(), m) -} - -fn rect_param(p: &Value) -> Option { - let a = p.get("rect")?.as_array()?; - let v: Vec = a.iter().filter_map(Value::as_f64).collect(); - if v.len() < 4 { - return None; - } - // Negative sizes (dragging up/left) are normalised. - let (x0, x1) = (v[0].min(v[0] + v[2]), v[0].max(v[0] + v[2])); - let (y0, y1) = (v[1].min(v[1] + v[3]), v[1].max(v[1] + v[3])); - Some(Rect::new(x0.floor() as i32, y0.floor() as i32, x1.ceil() as i32, y1.ceil() as i32)) -} - -fn point_param(p: &Value) -> Option<(f32, f32)> { - let a = p.get("point")?.as_array()?; - let (x, y) = (a.first()?.as_f64()? as f32, a.get(1)?.as_f64()? as f32); - (x.is_finite() && y.is_finite()).then_some((x, y)) -} - -/// The layer [`with_doc_sampler`] samples for `layer` / `all_layers`: `None` = the composite. -pub fn sampled_target(doc: &Document, layer: Option, all_layers: bool) -> Option { - layer.filter(|id| !all_layers && doc.layer(*id).and_then(Layer::surface).is_some()) -} - -/// Object Finder (#4087): the object Object Selection would select at document point `(x, y)`, -/// sampling `layer` (or the composite). Pure, so the UI can run it on a worker thread while the -/// pointer hovers; `select.object` with `"point"` commits the same result. -pub fn find_object(doc: &Document, layer: Option, all_layers: bool, x: f32, y: f32) -> Option { - with_doc_sampler(doc, layer, all_layers, |smp, doc| grabcut::object_at(smp, doc.bounds(), x, y, grabcut::OBJECT_AT_WORK_PX)) -} - -/// Most objects remembered by [`ObjectFinderCache`]. -pub const OBJECT_FINDER_ENTRIES: usize = 12; - -/// Object Finder results for one document's pixels (#4087): hovering over an object already -/// found, or clicking it, answers at once. Keyed like [`SubjectCache`] (valid while the pixels -/// are the same, the document held weakly) plus the sampled layer; a lookup hits any cached -/// object that covers the point, so one computation serves the whole object. -#[derive(Debug, Default)] -pub struct ObjectFinderCache { - doc: std::sync::Weak, - doc_id: Option, - target: Option, - regions: Vec>, -} - -impl ObjectFinderCache { - fn valid(&self, doc: &Document, target: Option) -> bool { - self.doc_id == Some(doc.id) && self.target == target && self.doc.upgrade().is_some_and(|old| crate::layer_multi_cmds::same_pixels(&old, doc)) - } - - /// A cached object of `doc` (sampling `target`) covering `(x, y)`. - pub fn lookup(&self, doc: &Document, target: Option, x: f32, y: f32) -> Option> { - if !x.is_finite() || !y.is_finite() || !self.valid(doc, target) { - return None; - } - let (px, py) = (x.floor() as i32, y.floor() as i32); - self.regions.iter().find(|r| r.at(px, py) >= 0.5).cloned() - } - - /// Remembers `region`, found in `doc` sampling `target`. A different document or pixels - /// start over; the oldest object is dropped beyond [`OBJECT_FINDER_ENTRIES`]. - pub fn insert(&mut self, doc: &std::sync::Arc, target: Option, region: std::sync::Arc) { - if !self.valid(doc, target) { - self.regions.clear(); - } - self.doc = std::sync::Arc::downgrade(doc); - self.doc_id = Some(doc.id); - self.target = target; - if self.regions.len() >= OBJECT_FINDER_ENTRIES { - self.regions.remove(0); - } - self.regions.push(region); - } - - /// Number of remembered objects. - pub fn len(&self) -> usize { - self.regions.len() - } - - /// Whether nothing is remembered. - pub fn is_empty(&self) -> bool { - self.regions.is_empty() - } - - /// Forget the results if they were computed from the document `id` (it is closing). - pub(crate) fn forget_doc(&mut self, id: photocraft_doc::DocId) { - if self.doc_id == Some(id) { - *self = Self::default(); - } - } - - /// Follows `doc` past a selection-only edit (same pixels), so the next lookup still hits. - fn follow(&mut self, doc: &std::sync::Arc) { - if self.doc_id == Some(doc.id) && self.doc.upgrade().is_some_and(|old| crate::layer_multi_cmds::same_pixels(&old, doc)) { - self.doc = std::sync::Arc::downgrade(doc); - } - } -} - -/// Object Selection samples only the active layer by default: when that layer has no pixels in -/// `area`, say so instead of quietly finding nothing (#5246; scripts often add an empty layer -/// above the photo first). -fn check_sampled_pixels(doc: &Document, layer: Option, all_layers: bool, area: Rect) -> Result<()> { - let Some(surf) = sampled_target(doc, layer, all_layers).and_then(|id| doc.layer(id)).and_then(Layer::surface) else { return Ok(()) }; - if surf.content_bounds().intersect(&area).is_empty() { - let msg = if surf.content_bounds().is_empty() { "the active layer is empty" } else { "the active layer has no pixels in that area" }; - return Err(bad("select.object", &format!("{msg}; set \"sampleAllLayers\": true or select a layer with pixels"))); - } - Ok(()) -} - -/// `select.object` with `"point"`: the Object Finder's object under the point (cached). -fn object_at_point(s: &mut Session, p: &Value, (x, y): (f32, f32)) -> Result { - let all_layers = b(p, "sampleAllLayers", false); - let d = s.active().ok_or(EngineError::NoDocument)?; - check_sampled_pixels(&d.doc, d.active_layer, all_layers, Rect::from_xywh(x.floor() as i32, y.floor() as i32, 1, 1))?; - let target = sampled_target(&d.doc, d.active_layer, all_layers); - let region = match s.object_finder.lookup(&d.doc, target, x, y) { - Some(r) => Some(r), - None => { - let doc = std::sync::Arc::clone(&d.doc); - let found = find_object(&doc, d.active_layer, all_layers, x, y).map(std::sync::Arc::new); - if let Some(r) = &found { - s.object_finder.insert(&doc, target, std::sync::Arc::clone(r)); - } - found - } - }; - let out = apply(s, "Object Selection", region.as_deref(), mode(p, "replace"))?; - if let Some(d) = s.active() { - let doc = std::sync::Arc::clone(&d.doc); - s.object_finder.follow(&doc); - } - Ok(out) -} - -fn object_selection(s: &mut Session, p: &Value) -> Result { - if p.get("rect").is_none() - && let Some(pt) = point_param(p) - { - return object_at_point(s, p, pt); - } - let rect = rect_param(p).ok_or_else(|| bad("select.object", "needs \"rect\": [x, y, w, h] or \"point\": [x, y]"))?; - let all_layers = b(p, "sampleAllLayers", false); - let d = s.active().ok_or(EngineError::NoDocument)?; - check_sampled_pixels(&d.doc, d.active_layer, all_layers, rect)?; - let region = with_sampler(s, all_layers, |smp, doc| grabcut::object_select(smp, doc.bounds(), rect, 160_000))?; - apply(s, "Object Selection", region.as_ref(), mode(p, "replace")) -} - -/// Select Subject's last result (#2865), so running it again on unchanged pixels answers at -/// once instead of recomputing superpixels, saliency and GrabCut (P29). Keyed by the pixels it -/// was computed from: valid while [`same_pixels`](crate::layer_multi_cmds::same_pixels) (a -/// tile-pointer comparison) holds against that document, so selection-only edits such as -/// Deselect keep it. One entry per session; the document is held weakly, so the cache never -/// keeps a closed or edited document's tiles alive (the session also drops it on pixel edits -/// and when its document closes). Its memory is the mask: one byte per pixel of its bounds. -#[derive(Debug, Default)] -pub struct SubjectCache { - doc: std::sync::Weak, - doc_id: Option, - /// The sampled layer; `None` = the composite. - target: Option, - region: Option>, -} - -impl SubjectCache { - /// Forget the result if it was computed from the document `id` (it is closing). - pub(crate) fn forget_doc(&mut self, id: photocraft_doc::DocId) { - if self.doc_id == Some(id) { - *self = Self::default(); - } - } -} - -fn select_subject(s: &mut Session, p: &Value) -> Result { - let all_layers = b(p, "sampleAllLayers", true); - let d = s.active().ok_or(EngineError::NoDocument)?; - // What `with_doc_sampler` samples: the active layer's pixels, else the composite. - let target = d.active_layer.filter(|id| !all_layers && d.doc.layer(*id).and_then(Layer::surface).is_some()); - let c = &s.subject_cache; - let hit = c.target == target && c.doc.upgrade().is_some_and(|old| crate::layer_multi_cmds::same_pixels(&old, &d.doc)); - let region = if hit { - c.region.clone() - } else { - let doc = std::sync::Arc::clone(&d.doc); - let region = with_doc_sampler(&doc, d.active_layer, all_layers, |smp, doc| subject::select_subject(smp, doc.bounds())).map(std::sync::Arc::new); - s.subject_cache = SubjectCache { doc: std::sync::Arc::downgrade(&doc), doc_id: Some(doc.id), target, region: region.clone() }; - region - }; - let out = apply(s, "Select Subject", region.as_deref(), mode(p, "replace"))?; - // Follow the document past the selection edit just made (same pixels), so a short history - // dropping the older state doesn't turn the next repeat into a miss. - if let Some(d) = s.active() - && s.subject_cache.doc_id == Some(d.doc.id) - && s.subject_cache.doc.upgrade().is_some_and(|old| crate::layer_multi_cmds::same_pixels(&old, &d.doc)) - { - s.subject_cache.doc = std::sync::Arc::downgrade(&d.doc); - } - Ok(out) -} - -fn focus_area(s: &mut Session, p: &Value) -> Result { - let (range, noise) = (f(p, "range", 0.5).clamp(0.0, 1.0), f(p, "noise", 0.0).clamp(0.0, 1.0)); - let region = with_sampler(s, b(p, "sampleAllLayers", true), |smp, doc| focus::focus_area(smp, doc.bounds(), range, noise))?; - apply(s, "Focus Area", region.as_ref(), mode(p, "replace")) -} - -fn refine_params(p: &Value) -> RefineParams { - RefineParams { - radius: f(p, "radius", 0.0).clamp(0.0, 250.0), - smart_radius: b(p, "smartRadius", false), - smooth: f(p, "smooth", 0.0).clamp(0.0, 100.0), - feather: f(p, "feather", 0.0).clamp(0.0, 1000.0), - contrast: f(p, "contrast", 0.0).clamp(0.0, 100.0), - shift_edge: f(p, "shiftEdge", 0.0).clamp(-100.0, 100.0), - } -} - -/// Most Refine Edge Brush strokes, and points across them, one Select and Mask takes. -pub const MAX_STROKES: usize = 4096; -pub const MAX_STROKE_POINTS: usize = 100_000; - -/// The Refine Edge Brush strokes of `refineStrokes`: -/// `[{"points":[[x,y],…],"size":px,"mode":"add|subtract"}]`, in painting order. -fn refine_strokes(p: &Value) -> Result> { - const CMD: &str = "select.refineEdge"; - let Some(v) = p.get("refineStrokes").filter(|v| !v.is_null()) else { return Ok(Vec::new()) }; - let list = v.as_array().ok_or_else(|| bad(CMD, "refineStrokes must be an array of strokes"))?; - if list.len() > MAX_STROKES { - return Err(bad(CMD, &format!("refineStrokes holds more than {MAX_STROKES} strokes"))); - } - let mut total = 0usize; - let mut strokes = Vec::with_capacity(list.len()); - for stroke in list { - let o = stroke.as_object().ok_or_else(|| bad(CMD, "each refineStrokes entry must be {\"points\":[[x,y],…],\"size\":px,\"mode\":\"add|subtract\"}"))?; - let pts = o.get("points").and_then(Value::as_array).filter(|a| !a.is_empty()).ok_or_else(|| bad(CMD, "each stroke needs a non-empty points array"))?; - total = total.saturating_add(pts.len()); - if total > MAX_STROKE_POINTS { - return Err(bad(CMD, &format!("refineStrokes holds more than {MAX_STROKE_POINTS} points"))); - } - let points = pts - .iter() - .map(|pt| { - let xy = pt.as_array().filter(|a| a.len() == 2)?; - let (x, y) = (xy.first()?.as_f64()?, xy.get(1)?.as_f64()?); - let ok = |v: f64| v.is_finite() && v.abs() <= 1e7; - (ok(x) && ok(y)).then_some((x as f32, y as f32)) - }) - .collect::>>() - .ok_or_else(|| bad(CMD, "stroke points must be [x,y] pairs of finite numbers"))?; - let range = matting::BRUSH_SIZE_RANGE; - let size = match o.get("size") { - None => 30.0, - Some(v) => v - .as_f64() - .filter(|s| s.is_finite() && (f64::from(*range.start())..=f64::from(*range.end())).contains(s)) - .ok_or_else(|| bad(CMD, &format!("stroke size must be {}..{} px", range.start(), range.end())))? as f32, - }; - let add = match o.get("mode").map(|m| m.as_str()) { - None | Some(Some("add")) => true, - Some(Some("subtract")) => false, - Some(_) => return Err(bad(CMD, "stroke mode must be add|subtract")), - }; - strokes.push(matting::EdgeStroke { points, size, add }); - } - if matting::swept_area(&strokes) > matting::MAX_SWEPT_AREA { - return Err(bad(CMD, "refineStrokes paint too large an area")); - } - Ok(strokes) -} - -/// Select and Mask: refines the selection edge, then outputs to the selection, a layer mask on -/// the active layer, a new layer or a new document (each with or without a mask). -/// Decontaminating colours needs new pixels, so it upgrades `selection` / `layerMask` output to -/// `newLayerWithMask` (as Photoshop does). New-layer outputs hide the source layer; new-document -/// outputs leave the source document untouched. -fn refine_edge(s: &mut Session, p: &Value) -> Result { - let params = refine_params(p); - let strokes = refine_strokes(p)?; - let decontaminate = b(p, "decontaminate", false); - let amount = f(p, "amount", 100.0).clamp(0.0, 100.0); - let d = s.active().ok_or(EngineError::NoDocument)?; - let active = d.active_layer.filter(|id| d.doc.layer(*id).is_some()); - let default_output = - if d.doc.selection.is_none() && active.and_then(|id| d.doc.layer(id)).and_then(|l| l.mask.as_ref()).is_some() { "layerMask" } else { "selection" }; - let mut output = p.get("output").and_then(Value::as_str).unwrap_or(default_output).to_string(); - if !matches!(output.as_str(), "selection" | "layerMask" | "newLayer" | "newLayerWithMask" | "newDocument" | "newDocumentWithMask") { - return Err(bad("select.refineEdge", "output must be selection|layerMask|newLayer|newLayerWithMask|newDocument|newDocumentWithMask")); - } - if decontaminate && matches!(output.as_str(), "selection" | "layerMask") { - output = "newLayerWithMask".into(); - } - let region = with_sampler(s, b(p, "sampleAllLayers", false), |smp, doc| { - let mask_surf = active.and_then(|id| doc.layer(id)).and_then(|l| l.mask.as_ref()).map(|m| &m.surface); - let target_mask = p.get("target").and_then(Value::as_str) == Some("mask"); - let sel = if target_mask { mask_surf.or(doc.selection.as_ref()) } else { doc.selection.as_ref().or(mask_surf) }?; - let brush = matting::EdgeBrush::new(&strokes, doc.bounds()); - matting::refine_mask_brushed(smp, &matting::surface_reader(sel), sel.content_bounds(), doc.bounds(), ¶ms, brush.as_ref()) - })?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let active = d.active_layer.filter(|id| d.doc.layer(*id).is_some()); - match output.as_str() { - "selection" => { - let selected = s.edit("Select and Mask", |doc, _| { - doc.selection = region.as_ref().map(matting::region_surface); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "output": output, "selected": selected })) - } - "layerMask" => { - let id = active.ok_or_else(|| EngineError::Other("no active layer".into()))?; - s.edit("Select and Mask", |doc, _| { - let surface = - region.as_ref().map(matting::region_surface).unwrap_or_else(|| photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8)); - crate::extra_cmds::background_to_layer_for_mask(doc, id); - let existing = doc.layer(id).and_then(|l| l.mask.as_ref()).cloned().unwrap_or_else(LayerMask::reveal_all); - doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?.mask = Some(LayerMask { surface, ..existing }); - doc.selection = None; - Ok(()) - })?; - Ok(json!({ "output": output, "layer": id.0, "selected": false })) - } - _ => { - let id = active.ok_or_else(|| EngineError::Other("no active layer".into()))?; - let src_layer = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let src = src_layer.surface().ok_or_else(|| { - EngineError::Other(format!("the active layer is {} {} layer without pixels", src_layer.content.article(), src_layer.content.kind_name())) - })?; - let name = d.doc.copy_name(&src_layer.name); - let empty = Region { bbox: Rect::EMPTY, mask: Vec::new() }; - let reg = region.as_ref().unwrap_or(&empty); - let pixels = if decontaminate { matting::decontaminate(src, reg, params.radius, amount) } else { src.clone() }; - let with_mask = matches!(output.as_str(), "newLayerWithMask" | "newDocumentWithMask"); - let (content, mask) = if with_mask { - (pixels, Some(LayerMask { surface: matting::region_surface(reg), ..LayerMask::reveal_all() })) - } else { - (matting::masked_copy(&pixels, reg), None) - }; - if output.starts_with("newDocument") { - // Same canvas, colour mode, depth and profile, so the pixels need no conversion. - let mut doc = Document::new("Untitled", d.doc.size, d.doc.mode, d.doc.depth); - doc.resolution_dpi = d.doc.resolution_dpi; - doc.icc_profile = d.doc.icc_profile.clone(); - doc.color_table = d.doc.color_table.clone(); - doc.duotone = d.doc.duotone.clone(); - let mut layer = Layer::new(src_layer.name.clone(), LayerContent::Raster(content)); - layer.mask = mask; - let layer_id = layer.id; - doc.layers.push(layer); - let i = s.add_document(doc, None); - return Ok(json!({ "output": output, "document": i, "layer": layer_id.0, "selected": false })); - } - let new_id = s.edit("Select and Mask", |doc, act| { - let mut layer = Layer::new(name, LayerContent::Raster(content)); - layer.mask = mask; - let nid = doc.insert_above(Some(id), layer); - if let Some(l) = doc.layer_mut(id) { - l.visible = false; - } - doc.selection = None; - *act = Some(nid); - Ok(nid) - })?; - Ok(json!({ "output": output, "layer": new_id.0, "selected": false })) - } - } -} - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $params:literal, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: None, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -/// Smart selection command specs. -pub fn specs() -> Vec { - vec![ - spec!( - "select.quick", - "Quick Selection", - [], - r##"{"points":[[x,y],…],"size":px=30,"mode":"add|subtract|replace"="add","sampleAllLayers":bool=false,"enhanceEdge":bool=false}"##, - has_doc, - quick_selection - ), - spec!( - "select.object", - "Object Selection", - [], - r##"{"rect":[x,y,w,h] | "point":[x,y],"mode":"replace|add|subtract|intersect"="replace","sampleAllLayers":bool=false} (point: the Object Finder's object under the point, as hovering previews it)"##, - has_doc, - object_selection - ), - spec!( - "select.subject", - "Subject", - ["Select"], - r##"{"sampleAllLayers":bool=true,"mode":"replace|add|subtract|intersect"="replace"}"##, - has_doc, - select_subject - ), - spec!( - "select.refineEdge", - "Refine Edge", - [], - r##"{"radius":px=0,"smartRadius":bool=false,"smooth":0..100=0,"feather":px=0,"contrast":0..100=0,"shiftEdge":-100..100=0,"decontaminate":bool=false,"amount":0..100=100,"output":"selection|layerMask|newLayer|newLayerWithMask|newDocument|newDocumentWithMask"="selection","sampleAllLayers":bool=false,"refineStrokes":[{"points":[[x,y],…],"size":px=30,"mode":"add|subtract"="add"},…]=[]} (refineStrokes: the Refine Edge Brush, in painting order: add refines the painted area from the image, subtract keeps the original edge there)"##, - has_selection_or_mask, - refine_edge - ), - spec!( - "select.focusArea", - "Focus Area…", - ["Select"], - r##"{"range":0..1=0.5,"noise":0..1=0,"sampleAllLayers":bool=true,"mode":"replace|add|subtract|intersect"="replace"}"##, - has_doc, - focus_area - ), - ] -} - -#[cfg(test)] -mod tests { - use super::*; - use photocraft_algo::segment::Rng; - - /// A document whose background shows `px(x, y)` (RGB, 0–1), at the given depth. - fn session_with(w: u32, h: u32, depth: u32, px: impl Fn(u32, u32) -> [f32; 3]) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": w, "height": h, "depth": depth})).unwrap(); - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - let n = bg.channels(); - let mut data = Vec::with_capacity((w * h) as usize * n); - for y in 0..h { - for x in 0..w { - let c = px(x, y); - data.extend_from_slice(&c[..n.min(3)]); - if n == 4 { - data.push(1.0); - } - } - } - bg.write_region(Rect::new(0, 0, w as i32, h as i32), &data); - Ok(()) - }) - .unwrap(); - s - } - - fn noise_field(w: u32, h: u32, seed: u64) -> Vec { - let mut rng = Rng::new(seed); - (0..w * h).map(|_| rng.normal()).collect() - } - - /// Warm textured left part (x < 60), cool right part. - fn two_regions(depth: u32) -> Session { - let nz = noise_field(140, 100, 1); - session_with(140, 100, depth, move |x, y| { - let n = 0.04 * nz[(y * 140 + x) as usize]; - if x < 60 { [0.8 + n, 0.5 + n, 0.3] } else { [0.2, 0.35 + n, 0.6 + n] }.map(|v| v.clamp(0.0, 1.0)) - }) - } - - /// Textured disc (centre (100, 80), radius 45) on a noisy background. - fn disc(depth: u32) -> (Session, impl Fn(i32, i32) -> bool) { - let nz = noise_field(200, 160, 2); - let inside = |x: i32, y: i32| { - let (dx, dy) = (x as f32 + 0.5 - 100.0, y as f32 + 0.5 - 80.0); - dx * dx + dy * dy <= 45.0 * 45.0 - }; - let s = session_with(200, 160, depth, move |x, y| { - let n = 0.05 * nz[(y * 200 + x) as usize]; - if inside(x as i32, y as i32) { - let t = ((x + y) as f32 * 0.35).sin() * 0.5 + 0.5; - [0.85 + 0.05 * t + n, 0.2 + 0.3 * t + n, 0.15 + n] - } else { - [0.25 + n, 0.45 + n, 0.7 + n] - } - .map(|v| v.clamp(0.0, 1.0)) - }); - (s, inside) - } - - fn cov(s: &Session, x: i32, y: i32) -> f32 { - s.active().unwrap().doc.selection.as_ref().map_or(0.0, |m| m.sample_channel(x, y, 0)) - } - - fn sel_iou(s: &Session, w: i32, h: i32, truth: impl Fn(i32, i32) -> bool) -> f32 { - let (mut i, mut u) = (0, 0); - for y in 0..h { - for x in 0..w { - let (a, b) = (cov(s, x, y) >= 0.5, truth(x, y)); - i += (a && b) as i32; - u += (a || b) as i32; - } - } - i as f32 / u.max(1) as f32 - } - - #[test] - fn quick_selection_add_subtract_undo() { - for depth in [8, 16] { - let mut s = two_regions(depth); - let r = s.execute("select.quick", json!({"points": [[20, 50], [35, 50]], "size": 8})).unwrap(); - assert_eq!(r["selected"], true); - for y in (0..100).step_by(7) { - for x in (0..140).step_by(3) { - let c = cov(&s, x, y); - if x < 57 { - assert!(c >= 0.5, "depth {depth}: ({x},{y}) not selected"); - } else if x > 62 { - assert!(c < 0.5, "depth {depth}: leaked to ({x},{y})"); - } - } - } - // A second stroke on the right adds it. - s.execute("select.quick", json!({"points": [[100, 50]], "size": 8})).unwrap(); - assert!(cov(&s, 130, 90) >= 0.5 && cov(&s, 10, 10) >= 0.5); - // Subtract the left region again. - s.execute("select.quick", json!({"points": [[20, 50]], "size": 8, "mode": "subtract"})).unwrap(); - assert!(cov(&s, 10, 10) < 0.5 && cov(&s, 130, 90) >= 0.5); - s.execute("edit.undo", json!({})).unwrap(); - assert!(cov(&s, 10, 10) >= 0.5); - assert!(s.execute("select.quick", json!({"points": []})).is_err()); - } - } - - #[test] - fn quick_selection_subtract_from_select_all_and_enhance() { - let mut s = two_regions(8); - s.execute("select.all", json!({})).unwrap(); - s.execute("select.quick", json!({"points": [[100, 30], [110, 70]], "size": 10, "mode": "subtract"})).unwrap(); - assert!(cov(&s, 10, 50) >= 0.99); - assert!(cov(&s, 120, 20) < 0.01 && cov(&s, 70, 90) < 0.01); - s.execute("select.deselect", json!({})).unwrap(); - s.execute("select.quick", json!({"points": [[20, 50]], "size": 8, "enhanceEdge": true})).unwrap(); - assert!(cov(&s, 10, 10) >= 0.5 && cov(&s, 100, 50) < 0.5); - } - - #[test] - fn empty_result_keeps_the_selection_in_every_mode() { - // Finding nothing must never wipe the selection, whatever the mode: Select Subject - // and Object Selection run heuristics that often miss on busy photos, and a missed - // Quick Selection dab must not destroy the work either. - for m in ["add", "subtract", "replace"] { - let mut s = two_regions(8); - s.execute("select.all", json!({})).unwrap(); - let r = s.execute("select.quick", json!({"points": [[-5000, -5000]], "size": 8, "mode": m})).unwrap(); - assert_eq!(r["changed"], false, "mode {m}"); - assert_eq!(r["selected"], true, "mode {m}"); - assert!(cov(&s, 10, 50) >= 0.99, "mode {m}: Select All survived an empty result"); - } - // Select Subject on a flat field finds nothing and keeps the selection too. - let mut s = session_with(140, 100, 8, |_, _| [0.5, 0.5, 0.5]); - s.execute("select.all", json!({})).unwrap(); - let r = s.execute("select.subject", json!({})).unwrap(); - assert_eq!(r["changed"], false); - assert!(cov(&s, 10, 10) >= 0.99); - } - - #[test] - fn object_selection_point_selects_the_object_under_it() { - for depth in [8, 16, 32] { - let (mut s, inside) = disc(depth); - let r = s.execute("select.object", json!({"point": [100, 80]})).unwrap(); - assert_eq!(r["changed"], true, "depth {depth}"); - let v = sel_iou(&s, 200, 160, &inside); - assert!(v > 0.9, "depth {depth}: IoU {v}"); - } - // The background is no object: nothing changes. - let (mut s, _) = disc(8); - let r = s.execute("select.object", json!({"point": [3, 3]})).unwrap(); - assert_eq!(r["changed"], false); - assert!(s.active().unwrap().doc.selection.is_none()); - assert!(s.execute("select.object", json!({"point": ["a", 1]})).is_err()); - } - - #[test] - fn object_selection_point_honours_the_modes() { - let (mut s, _) = disc(8); - s.execute("select.all", json!({})).unwrap(); - s.execute("select.object", json!({"point": [100, 80], "mode": "subtract"})).unwrap(); - assert!(cov(&s, 100, 80) < 0.5 && cov(&s, 3, 3) >= 0.5); - s.execute("select.object", json!({"point": [100, 80], "mode": "add"})).unwrap(); - assert!(cov(&s, 100, 80) >= 0.5 && cov(&s, 3, 3) >= 0.5); - s.execute("select.object", json!({"point": [100, 80], "mode": "intersect"})).unwrap(); - assert!(cov(&s, 100, 80) >= 0.5 && cov(&s, 3, 3) < 0.5); - } - - #[test] - fn object_finder_cache_serves_the_whole_object_until_pixels_change() { - let (mut s, _) = disc(8); - let d = s.active().unwrap(); - let doc = std::sync::Arc::clone(&d.doc); - let target = sampled_target(&doc, d.active_layer, false); - assert!(s.object_finder.lookup(&doc, target, 100.0, 80.0).is_none()); - let r = std::sync::Arc::new(find_object(&doc, d.active_layer, false, 100.0, 80.0).unwrap()); - s.object_finder.insert(&doc, target, std::sync::Arc::clone(&r)); - // Any point on the object hits; the background and another target miss. - assert!(std::sync::Arc::ptr_eq(&s.object_finder.lookup(&doc, target, 120.0, 90.0).unwrap(), &r)); - assert!(s.object_finder.lookup(&doc, target, 3.0, 3.0).is_none()); - assert!(s.object_finder.lookup(&doc, Some(LayerId(u64::MAX)), 100.0, 80.0).is_none()); - // The click reuses it, and a selection-only edit keeps it. - s.execute("select.object", json!({"point": [110, 70]})).unwrap(); - assert_eq!(s.object_finder.len(), 1); - s.execute("select.deselect", json!({})).unwrap(); - let d = s.active().unwrap(); - assert!(s.object_finder.lookup(&std::sync::Arc::clone(&d.doc), target, 100.0, 80.0).is_some()); - // A pixel edit drops it. - s.execute("paint.pencil", json!({"points": [[5, 5], [30, 5]], "size": 4, "color": "#000000"})).unwrap(); - assert!(s.object_finder.is_empty()); - } - - #[test] - fn object_selection_disc() { - for depth in [8, 16, 32] { - let (mut s, inside) = disc(depth); - s.execute("select.object", json!({"rect": [40, 20, 120, 120]})).unwrap(); - let v = sel_iou(&s, 200, 160, &inside); - assert!(v > 0.95, "depth {depth}: IoU {v}"); - } - let (mut s, _) = disc(8); - assert!(s.execute("select.object", json!({"rect": [1, 2]})).is_err()); - // Negative width/height (drag up-left) work too. - s.execute("select.object", json!({"rect": [160, 140, -120, -120]})).unwrap(); - assert!(cov(&s, 100, 80) >= 0.5); - } - - #[test] - fn object_selection_on_an_empty_active_layer_says_so() { - // #5246: an empty layer above the photo used to find nothing without a word. - let (mut s, inside) = disc(8); - s.execute("layer.new.layer", json!({})).unwrap(); - for p in [json!({"rect": [40, 20, 120, 120]}), json!({"point": [100, 80]})] { - let e = s.execute("select.object", p.clone()).unwrap_err().to_string(); - assert!(e.contains("active layer is empty") && e.contains("sampleAllLayers"), "{p}: {e}"); - assert!(s.active().unwrap().doc.selection.is_none()); - } - // Sampling all layers finds the disc below. - s.execute("select.object", json!({"rect": [40, 20, 120, 120], "sampleAllLayers": true})).unwrap(); - assert!(sel_iou(&s, 200, 160, &inside) > 0.95); - // A layer with pixels elsewhere: the rectangle holds none of them. - s.execute("select.deselect", json!({})).unwrap(); - s.execute("paint.pencil", json!({"points": [[5, 5], [10, 5]], "size": 2, "color": "#000000"})).unwrap(); - let e = s.execute("select.object", json!({"rect": [60, 60, 100, 80]})).unwrap_err().to_string(); - assert!(e.contains("no pixels in that area"), "{e}"); - } - - #[test] - fn select_subject_centred_object() { - let (mut s, inside) = disc(8); - assert!(s.is_enabled("select.subject")); - s.execute("select.subject", json!({})).unwrap(); - let v = sel_iou(&s, 200, 160, inside); - assert!(v > 0.9, "IoU {v}"); - s.execute("edit.undo", json!({})).unwrap(); - assert!(s.active().unwrap().doc.selection.is_none()); - } - - /// #2865: a repeat on unchanged pixels reuses the last mask (an identical selection, even - /// after Deselect); sampling something else or a pixel edit computes it again. - #[test] - fn select_subject_repeat_reuses_the_mask_until_pixels_change() { - let (mut s, _) = disc(8); - let sel = |s: &Session| { - let mut v = Vec::new(); - s.active().unwrap().doc.selection.as_ref().unwrap().read_region_into(Rect::new(0, 0, 200, 160), &mut v); - v - }; - let cached = |s: &Session| s.subject_cache.region.clone().unwrap(); - s.execute("select.subject", json!({})).unwrap(); - let (first, mask) = (sel(&s), cached(&s)); - s.execute("select.deselect", json!({})).unwrap(); - s.execute("select.subject", json!({})).unwrap(); - assert!(std::sync::Arc::ptr_eq(&mask, &cached(&s)), "the repeat recomputed the mask"); - assert_eq!(sel(&s), first); - // Another sampled target (the layer rather than the composite) is a miss. - s.execute("select.subject", json!({"sampleAllLayers": false})).unwrap(); - assert!(!std::sync::Arc::ptr_eq(&mask, &cached(&s))); - // A pixel edit drops the entry; the next run recomputes it. - s.edit("paint", |doc, _| { - let bg = doc.layers[0].surface_mut().unwrap(); - let n = bg.channels(); - bg.write_region(Rect::new(0, 0, 1, 1), &vec![1.0; n]); - Ok(()) - }) - .unwrap(); - assert!(s.subject_cache.region.is_none()); - s.execute("select.subject", json!({})).unwrap(); - assert_eq!(sel(&s), first); - } - - /// A vertical Gaussian-blurred edge at x = 40 (warm left, cool right) with the left half selected. - fn blurred_edge_selected() -> Session { - blurred_edge_selected_at(8) - } - - fn blurred_edge_selected_at(depth: u32) -> Session { - let mut s = session_with(80, 40, depth, |x, _| { - let t = 1.0 / (1.0 + ((x as f32 + 0.5 - 40.0) / 1.8).exp()); - [0.9 * t + 0.1 * (1.0 - t), 0.8 * t + 0.2 * (1.0 - t), 0.2 * t + 0.6 * (1.0 - t)] - }); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 40, "height": 40})).unwrap(); - s - } - - fn soft_count(s: &photocraft_raster::Surface, y: i32) -> usize { - (0..80) - .filter(|x| { - let v = s.sample_channel(*x, y, 0); - v > 0.05 && v < 0.95 - }) - .count() - } - - #[test] - fn refine_edge_outputs_and_undo() { - // Selection output: the hard edge becomes a soft matte following the blur. - let mut s = blurred_edge_selected(); - assert!(s.is_enabled("select.refineEdge")); - s.execute("select.refineEdge", json!({"radius": 8})).unwrap(); - let sel = s.active().unwrap().doc.selection.clone().unwrap(); - assert!(soft_count(&sel, 20) >= 4, "selection edge not soft"); - assert!(sel.sample_channel(5, 20, 0) > 0.99 && sel.sample_channel(75, 20, 0) < 0.01); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(soft_count(s.active().unwrap().doc.selection.as_ref().unwrap(), 20), 0); - - // Layer mask output. - let layers_before = s.active().unwrap().doc.layer_count(); - let r = s.execute("select.refineEdge", json!({"radius": 8, "output": "layerMask"})).unwrap(); - let d = s.active().unwrap(); - let l = d.doc.layer(photocraft_doc::LayerId(r["layer"].as_u64().unwrap())).unwrap(); - let m = l.mask.as_ref().unwrap(); - assert!(soft_count(&m.surface, 20) >= 4); - assert!(m.value(5, 20) > 0.99 && m.value(75, 20) < 0.01); - assert!(d.doc.selection.is_none()); - assert_eq!(d.doc.layer_count(), layers_before); - s.execute("edit.undo", json!({})).unwrap(); - assert!(s.active().unwrap().doc.layers[0].mask.is_none()); - assert!(s.active().unwrap().doc.selection.is_some()); - - // Existing mask settings (density, feather, linked) are preserved - s.edit("set mask", |doc, _| { - doc.layers[0].mask = Some(photocraft_doc::LayerMask { - surface: photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8), - density: 0.75, - feather: 3.5, - linked: false, - enabled: true, - }); - Ok(()) - }) - .unwrap(); - let r = s.execute("select.refineEdge", json!({"radius": 8, "output": "layerMask"})).unwrap(); - let l = s.active().unwrap().doc.layer(photocraft_doc::LayerId(r["layer"].as_u64().unwrap())).unwrap(); - let m = l.mask.as_ref().unwrap(); - assert_eq!(m.density, 0.75); - assert_eq!(m.feather, 3.5); - assert!(!m.linked); - s.execute("edit.undo", json!({})).unwrap(); - s.edit("clear mask", |doc, _| { - doc.layers[0].mask = None; - Ok(()) - }) - .unwrap(); - - // New layer: pixels with the refined alpha; the source is hidden. - let r = s.execute("select.refineEdge", json!({"radius": 8, "feather": 1, "output": "newLayer"})).unwrap(); - let d = s.active().unwrap(); - let nid = photocraft_doc::LayerId(r["layer"].as_u64().unwrap()); - assert_eq!(d.active_layer, Some(nid)); - assert_eq!(d.doc.layer_count(), layers_before + 1); - let nl = d.doc.layer(nid).unwrap(); - let surf = nl.surface().unwrap(); - assert!(surf.format().alpha); - let a = |x: i32| surf.pixel(x, 20)[3]; - assert!(a(5) > 0.99 && a(75) == 0.0 && a(40) > 0.05 && a(40) < 0.95); - assert!(!d.doc.layers[0].visible); - s.execute("edit.undo", json!({})).unwrap(); - assert_eq!(s.active().unwrap().doc.layer_count(), layers_before); - assert!(s.active().unwrap().doc.layers[0].visible); - - // New layer with mask + decontamination (selection output upgrades to it). - let r = s.execute("select.refineEdge", json!({"radius": 8, "decontaminate": true, "amount": 100})).unwrap(); - assert_eq!(r["output"], "newLayerWithMask"); - let d = s.active().unwrap(); - let nl = d.doc.layer(photocraft_doc::LayerId(r["layer"].as_u64().unwrap())).unwrap(); - assert!(nl.mask.is_some()); - let px = nl.surface().unwrap().pixel(41, 20); - let orig = d.doc.layers[0].surface().unwrap().pixel(41, 20); - // The fringe colour moved towards the warm foreground (more red, less blue). - assert!(px[0] > orig[0] + 0.05 && px[2] < orig[2] - 0.05, "{px:?} vs {orig:?}"); - s.execute("edit.undo", json!({})).unwrap(); - assert!(s.execute("select.refineEdge", json!({"output": "bogus"})).is_err()); - s.execute("select.deselect", json!({})).unwrap(); - assert!(!s.is_enabled("select.refineEdge")); - } - - /// Output To New Document (with or without a layer mask) opens a document like the source, - /// at every depth, and leaves the source document as it was. - #[test] - fn refine_edge_outputs_to_a_new_document() { - for depth in [8, 16, 32] { - let mut s = blurred_edge_selected_at(depth); - s.edit("profile", |doc, _| { - doc.resolution_dpi = 300.0; - Ok(()) - }) - .unwrap(); - let before = s.active().unwrap().doc.clone(); - let r = s.execute("select.refineEdge", json!({"radius": 8, "output": "newDocument"})).unwrap(); - assert_eq!((r["output"].as_str(), r["document"].as_u64()), (Some("newDocument"), Some(1)), "depth {depth}"); - assert_eq!(s.active_index(), Some(1)); - let nd = &s.active().unwrap().doc; - assert_eq!((nd.size, nd.mode, nd.depth, nd.resolution_dpi), (before.size, before.mode, before.depth, 300.0)); - assert_eq!(nd.layers.len(), 1); - assert!(nd.selection.is_none() && nd.layers[0].mask.is_none()); - let surf = nd.layers[0].surface().unwrap(); - let a = |x: i32| surf.pixel(x, 20)[3]; - assert!(a(5) > 0.99 && a(75) == 0.0 && a(40) > 0.05 && a(40) < 0.95, "depth {depth}: {} {} {}", a(5), a(40), a(75)); - // The source is untouched: same layers, still visible, selection kept, no history step. - let src = &s.documents()[0]; - assert_eq!(src.doc.layers, before.layers); - assert_eq!(src.doc.selection, before.selection); - assert_eq!(src.history.undo_label(), Some("profile")); - - // With a layer mask: full pixels, the refined matte as the mask. - s.execute("file.close", json!({"document": 1})).unwrap(); - let r = s.execute("select.refineEdge", json!({"radius": 8, "output": "newDocumentWithMask"})).unwrap(); - assert_eq!(r["output"], "newDocumentWithMask"); - let nd = &s.active().unwrap().doc; - let l = &nd.layers[0]; - assert_eq!(l.surface().unwrap().pixel(75, 20), before.layers[0].surface().unwrap().pixel(75, 20), "depth {depth}"); - let m = l.mask.as_ref().unwrap(); - assert!(soft_count(&m.surface, 20) >= 4, "depth {depth}"); - assert!(m.value(5, 20) > 0.99 && m.value(75, 20) < 0.01, "depth {depth}"); - } - } - - #[test] - fn refine_edge_shift_and_contrast() { - let mut s = blurred_edge_selected(); - s.execute("select.refineEdge", json!({"shiftEdge": 100})).unwrap(); - assert!(cov(&s, 42, 20) > 0.99); - s.execute("edit.undo", json!({})).unwrap(); - s.execute("select.refineEdge", json!({"feather": 8, "contrast": 100})).unwrap(); - assert!(cov(&s, 38, 20) == 1.0 && cov(&s, 42, 20) == 0.0); - } - - /// The Refine Edge Brush adds strands beyond the radius band, at every depth, and rejects - /// malformed strokes. - #[test] - fn refine_edge_brush_strokes() { - for depth in [8, 16, 32] { - // Warm subject (x < 40) with warm strands every 4th column from x = 60, on blue. - let strand = |x: u32| x < 40 || ((60..84).contains(&x) && x.is_multiple_of(4)); - let mut s = session_with(120, 40, depth, |x, _| if strand(x) { [0.9, 0.7, 0.2] } else { [0.1, 0.3, 0.8] }); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 40, "height": 40})).unwrap(); - let stroke = json!([{"points": [[70, 5], [70, 35]], "size": 30}]); - s.execute("select.refineEdge", json!({"radius": 4, "refineStrokes": stroke})).unwrap(); - assert!(cov(&s, 64, 20) > 0.9 && cov(&s, 72, 20) > 0.9, "depth {depth}: strands {}", cov(&s, 64, 20)); - assert!(cov(&s, 66, 20) < 0.1 && cov(&s, 100, 20) < 0.01, "depth {depth}: background"); - s.execute("edit.undo", json!({})).unwrap(); - // Subtract over the same place paints the refinement out again. - let both = json!([{"points": [[70, 5], [70, 35]], "size": 30}, {"points": [[70, 5], [70, 35]], "size": 40, "mode": "subtract"}]); - s.execute("select.refineEdge", json!({"radius": 4, "refineStrokes": both})).unwrap(); - assert!(cov(&s, 64, 20) < 0.01, "depth {depth}"); - s.execute("edit.undo", json!({})).unwrap(); - for bad in [ - json!(7), - json!([5]), - json!([{"points": []}]), - json!([{"points": [[1, "x"]]}]), - json!([{"points": [[1, 2, 3]]}]), - json!([{"points": [[1, 2]], "size": 0}]), - json!([{"points": [[1, 2]], "size": 1e9}]), - json!([{"points": [[1, 2]], "mode": "xor"}]), - json!([{"points": [[0, 0], [1e6, 1e6], [0, 0], [1e6, 1e6]], "size": 5000}]), - ] { - assert!(s.execute("select.refineEdge", json!({"refineStrokes": bad})).is_err(), "{bad}"); - } - } - } - - #[test] - fn refine_edge_works_on_layer_mask_without_selection() { - let mut s = blurred_edge_selected(); - s.execute("layer.layerMask.revealSelection", json!({})).unwrap(); - assert!(s.active().unwrap().doc.selection.is_none()); - assert!(s.active().unwrap().doc.layers[0].mask.is_some()); - assert!(s.is_enabled("select.refineEdge")); - - let r = s.execute("select.refineEdge", json!({"radius": 8})).unwrap(); - assert_eq!(r["output"], "layerMask"); - let d = s.active().unwrap(); - let m = d.doc.layers[0].mask.as_ref().unwrap(); - assert!(soft_count(&m.surface, 20) >= 4); - } - - #[test] - fn focus_area_command() { - let nz = noise_field(160, 100, 3); - let mut blurred = vec![0.0f32; nz.len()]; - for y in 0..100i32 { - for x in 0..160i32 { - let mut a = 0.0; - for dy in -4..=4 { - for dx in -4..=4 { - a += nz[((y + dy).clamp(0, 99) * 160 + (x + dx).clamp(0, 159)) as usize]; - } - } - blurred[(y * 160 + x) as usize] = a / 81.0; - } - } - let mut s = session_with(160, 100, 8, move |x, y| { - let i = (y * 160 + x) as usize; - let v = 0.5 + 0.15 * if x < 80 { nz[i] } else { blurred[i] * 3.0 }; - [v, v, v].map(|c| c.clamp(0.0, 1.0)) - }); - s.execute("select.focusArea", json!({})).unwrap(); - assert!(cov(&s, 30, 50) >= 0.5 && cov(&s, 130, 50) < 0.5); - } -} diff --git a/crates/engine/src/snap.rs b/crates/engine/src/snap.rs index 3ab1ad73bf..78c8b9f2e8 100644 --- a/crates/engine/src/snap.rs +++ b/crates/engine/src/snap.rs @@ -89,10 +89,21 @@ pub fn layer_rect(doc: &Document, id: LayerId) -> Option<[f64; 4]> { } fn rect_of(doc: &Document, l: &photocraft_doc::Layer) -> Option<[f64; 4]> { - if l.is_group() || (matches!(l.content, LayerContent::Adjustment(_)) && l.mask.is_none()) { - return None; - } - let b = crate::transform_cmds::transform_bounds(doc, l); + snappable(l).then(|| crate::transform_cmds::transform_bounds(doc, l)).and_then(to_rect) +} + +/// Bounds of a layer as a Snap To Layers target: all of its content, whatever the selection +/// holds of it (#6290: clipped to the selection, every layer's edges and centre were the +/// selection's, so a moved selection snapped back to where it started). +fn target_rect_of(doc: &Document, l: &photocraft_doc::Layer) -> Option<[f64; 4]> { + snappable(l).then(|| crate::transform_cmds::whole_layer_bounds(doc, l)).and_then(to_rect) +} + +fn snappable(l: &photocraft_doc::Layer) -> bool { + !(l.is_group() || (matches!(l.content, LayerContent::Adjustment(_)) && l.mask.is_none())) +} + +fn to_rect(b: photocraft_geom::Rect) -> Option<[f64; 4]> { (!b.is_empty()).then_some([b.x0 as f64, b.y0 as f64, b.x1 as f64, b.y1 as f64]) } @@ -121,7 +132,14 @@ impl SnapTargets { if !l.visible || exclude.contains(&l.id) { continue; } - if let Some(r) = rect_of(doc, l) { + // An artboard is a group, but its edges and centre are the board's own. + if let Some(a) = l.artboard() { + if let Some(r) = to_rect(a.rect) { + t.add_rect(r, SnapKind::LayerEdge, SnapKind::LayerCenter); + } + continue; + } + if let Some(r) = target_rect_of(doc, l) { t.add_rect(r, SnapKind::LayerEdge, SnapKind::LayerCenter); } } @@ -365,6 +383,42 @@ mod tests { let t = SnapTargets::from_document(&doc, &SnapOptions { guides: false, ..opts }, &[id]); assert!(!t.x.iter().any(|t| t.kind == SnapKind::Guide || (t.kind == SnapKind::LayerEdge && t.pos == 30.0))); assert_eq!(layer_rect(&doc, id), Some([10.0, 20.0, 30.0, 60.0])); + // #6290: a selection doesn't move the layers' targets onto its own bounds. + let mut sel = photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8); + sel.fill_rect(photocraft_geom::Rect::new(64, 66, 124, 86), &[1.0]); + doc.selection = Some(sel); + let t = SnapTargets::from_document(&doc, &SnapOptions { selection: false, ..opts }, &[]); + assert!(t.x.iter().any(|t| t.kind == SnapKind::LayerEdge && t.pos == 30.0)); + assert!( + !t.x.iter() + .chain(&t.y) + .any(|t| matches!(t.kind, SnapKind::LayerEdge | SnapKind::LayerCenter) && [64.0, 66.0, 76.0, 86.0, 94.0, 124.0].contains(&t.pos)) + ); assert_eq!(union([[0.0, 0.0, 1.0, 1.0], [2.0, -1.0, 3.0, 0.5]]), Some([0.0, -1.0, 3.0, 1.0])); } + + /// An artboard (a group) offers the board's own edges and centre, so other layers, boards and + /// tools can snap to it; the board being moved doesn't snap to itself. + #[test] + fn an_artboard_offers_its_own_edges() { + let mut doc = Document::with_background("s", Size::new(200, 100), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut board = Layer::group("Artboard 1", vec![]); + if let photocraft_doc::LayerContent::Group(g) = &mut board.content { + g.artboard = + Some(photocraft_doc::Artboard { rect: photocraft_geom::Rect::new(20, 10, 120, 90), background: Default::default(), preset: String::new() }); + } + let id = doc.insert_above(None, board); + let opts = SnapOptions::default(); + let t = SnapTargets::from_document(&doc, &opts, &[]); + let has = |ts: &[Target], kind: SnapKind, pos: f64| ts.iter().any(|t| t.kind == kind && t.pos == pos); + for x in [20.0, 120.0] { + assert!(has(&t.x, SnapKind::LayerEdge, x), "x = {x}"); + } + for y in [10.0, 90.0] { + assert!(has(&t.y, SnapKind::LayerEdge, y), "y = {y}"); + } + assert!(has(&t.x, SnapKind::LayerCenter, 70.0) && has(&t.y, SnapKind::LayerCenter, 50.0)); + let t = SnapTargets::from_document(&doc, &opts, &[id]); + assert!(!has(&t.x, SnapKind::LayerEdge, 120.0) && !has(&t.x, SnapKind::LayerCenter, 70.0)); + } } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 04063d3073..b09c184c6d 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -158,6 +158,33 @@ fn file_new_keeps_defaults_and_supported_modes_and_depths() { } } +#[test] +fn file_new_tags_the_document_with_the_chosen_profile() { + // #321: New Document › Color Profile, as in Photoshop: another profile than the working space. + let desc = |s: &Session| { + let bytes = s.active().unwrap().doc.icc_profile.clone(); + bytes.map(|b| photocraft_cms::Profile::parse(&b).unwrap().description) + }; + let mut s = Session::new(); + s.execute("file.new", json!({"width": 4, "height": 4, "profile": "adobe-rgb-compat"})).unwrap(); + assert_eq!(desc(&s).as_deref(), Some(photocraft_cms::Builtin::AdobeRgbCompat.description())); + s.execute("file.new", json!({"width": 4, "height": 4, "mode": "cmyk", "profile": "coated-cmyk"})).unwrap(); + assert!(desc(&s).is_some()); + // The working space (the default, or asked for) leaves it as before. + for p in [json!({"width": 4, "height": 4}), json!({"width": 4, "height": 4, "profile": "working"})] { + s.execute("file.new", p).unwrap(); + assert_eq!(desc(&s), None); + } + // A profile that doesn't suit the mode, an unknown one or a wrong type opens nothing. + let open = s.documents().len(); + let (active, steps) = (s.active().unwrap().doc.id, s.active().unwrap().history.past_len()); + for bad in [json!("coated-cmyk"), json!("no-such-profile"), json!(""), json!(7), json!(null)] { + assert!(s.execute("file.new", json!({"width": 4, "height": 4, "profile": bad})).is_err(), "{bad}"); + assert_eq!(s.documents().len(), open, "{bad}"); + assert_eq!((s.active().unwrap().doc.id, s.active().unwrap().history.past_len()), (active, steps), "{bad}"); + } +} + #[test] fn file_new_variants() { let mut s = Session::new(); @@ -238,8 +265,9 @@ fn new_adjustment_and_fill_layers_are_masked_by_the_selection() { assert_eq!(mask.value(10, 10), 1.0, "{id} at {depth}-bit: the mask hides the selected area"); assert_eq!(mask.value(2, 2), 0.0, "{id} at {depth}-bit: the mask reveals outside the selection"); assert!(mask.linked && mask.enabled, "{id}"); - // The selection stays, as after Layer › Layer Mask › Reveal Selection. - assert!(d.doc.selection.is_some(), "{id} dropped the selection"); + // The selection went into the mask, so Photoshop drops it, as Layer › Layer Mask › + // Reveal Selection does (#5251, #8801). + assert!(d.doc.selection.is_none(), "{id} at {depth}-bit kept the selection"); // The white canvas is untouched outside the selection. assert_eq!(px(&mut s, 2, 2), vec![1.0, 1.0, 1.0, 1.0], "{id} at {depth}-bit changed pixels outside the selection"); if blackens { @@ -247,6 +275,29 @@ fn new_adjustment_and_fill_layers_are_masked_by_the_selection() { } s.execute("edit.undo", json!({})).unwrap(); assert!(s.active().unwrap().doc.layer(layer).is_none(), "{id}: undo keeps the layer"); + let sel = s.active().unwrap().doc.selection.as_ref().unwrap_or_else(|| panic!("{id}: undo does not bring the selection back")); + assert_eq!(sel.content_bounds(), photocraft_geom::Rect::new(8, 8, 24, 24), "{id}: undo restores the same selection"); + } + } +} + +#[test] +fn an_edit_after_a_new_fill_layer_on_a_selection_reaches_the_whole_layer() { + // #8801: the selection that masked a new fill or adjustment layer is gone, so the next + // pixel edit on another layer is not limited to it. + for (id, params) in [("layer.newFillLayer.solidColor", json!({"color": "#000000"})), ("layer.newAdjustmentLayer.invert", json!({}))] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 20})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#3366cc"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); + s.execute(id, params).unwrap(); + s.execute("layer.setProps", json!({"visible": false})).unwrap(); + s.execute("layer.selectBottom", json!({})).unwrap(); + s.execute("layer.duplicate", json!({})).unwrap(); + s.execute("image.adjustments.desaturate", json!({})).unwrap(); + for x in [5, 35] { + let p = px(&mut s, x, 10); + assert!((p[0] - p[1]).abs() < 1e-3 && (p[1] - p[2]).abs() < 1e-3, "{id}: ({x}, 10) is not grey: {p:?}"); } } } @@ -324,6 +375,7 @@ fn new_fill_and_adjustment_layers_take_the_active_path_as_their_vector_mask() { let want = if path_name == "work" { d.doc.work_path.clone().unwrap() } else { d.doc.paths[0].path.clone() }; assert_eq!(vm.path, want, "{id}: the vector mask is the path"); assert!(l.mask.is_none(), "{id}: the path wins over the selection"); + assert!(d.doc.selection.is_some(), "{id}: an unused selection stays"); for (x, y) in [(2, 2), (42, 32), (30, 30)] { assert_eq!(px(&mut s, x, y), vec![1.0, 1.0, 1.0, 1.0], "{id} ({path_name}) at {depth}-bit changed pixels outside the path at ({x}, {y})"); } @@ -406,6 +458,34 @@ fn marquee_dragged_past_the_canvas_stops_at_its_edge() { assert_eq!(bounds(&mut s), Value::Null); } +#[test] +fn subtract_clears_selection_when_only_disjoint_off_canvas_pixels_remain() { + use photocraft_geom::Rect; + use photocraft_raster::Surface; + let mut s = session_with_doc(); // 64 × 48 + let mut selection = Surface::new(photocraft_color::PixelFormat::GRAY8); + // Simulate a moved sparse selection: one remnant on either side of the canvas and one + // in-canvas region that the subtract marquee will remove. The off-canvas bounds straddle + // the canvas, so testing only their bounding-box intersection would incorrectly keep it. + selection.fill_rect(Rect::new(-4, 10, 0, 14), &[1.0]); + selection.fill_rect(Rect::new(20, 10, 24, 14), &[1.0]); + selection.fill_rect(Rect::new(64, 10, 68, 14), &[1.0]); + Arc::make_mut(&mut s.active_mut().unwrap().doc).selection = Some(selection); + + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 64, "height": 48, "mode": "subtract"})).unwrap(); + + assert!(s.active().unwrap().doc.selection.is_none()); +} + +#[test] +fn subtract_clears_selection_with_nonzero_default_when_canvas_is_empty() { + use photocraft_raster::Surface; + let mut s = session_with_doc(); + Arc::make_mut(&mut s.active_mut().unwrap().doc).selection = Some(Surface::with_default(photocraft_color::PixelFormat::GRAY8, &[1.0])); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 64, "height": 48, "mode": "subtract"})).unwrap(); + assert!(s.active().unwrap().doc.selection.is_none()); +} + #[test] fn marquee_steps_are_named_after_their_tool() { // #513: the elliptical marquee shares `select.rect` but records its own step name. @@ -479,6 +559,41 @@ fn undo_and_redo_restore_the_targeted_layers() { assert_eq!(target(&s), copies); } +#[test] +fn selection_inverse_preserves_coverage_history_and_empty_selection() { + for depth in [8, 16, 32] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 530, "height": 270, "depth": depth})).unwrap(); + s.execute("select.rect", json!({"x": 250, "y": 10, "width": 30, "height": 30, "ellipse": true})).unwrap(); + let before = s.active().unwrap().doc.clone(); + let area = before.bounds(); + let original = before.selection.as_ref().unwrap().to_interleaved(area); + s.execute("select.inverse", json!({})).unwrap(); + let after = s.active().unwrap(); + let inverted = after.doc.selection.as_ref().unwrap(); + assert_eq!(inverted.to_interleaved(area), original.iter().map(|b| 255 - b).collect::>()); + assert_eq!(inverted.sample_channel(530, 269, 0), 0.0); + assert_eq!(inverted.sample_channel(529, 270, 0), 0.0); + assert_eq!(after.last_damage, Some(photocraft_geom::Rect::EMPTY)); + assert_eq!(before.selection.as_ref().unwrap().to_interleaved(area), original); + assert!(s.undo()); + assert_eq!(s.active().unwrap().doc.selection.as_ref().unwrap().to_interleaved(area), original); + assert!(s.redo()); + s.execute("select.inverse", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.selection.as_ref().unwrap().to_interleaved(area), original); + + s.execute("select.all", json!({})).unwrap(); + s.execute("select.inverse", json!({})).unwrap(); + let empty = s.active().unwrap().doc.selection.as_ref().unwrap(); + assert_eq!(empty.tile_count(), 0); + // Keep Some(empty), as before: the next Inverse selects the whole canvas again. + s.execute("select.inverse", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(), area); + } + let mut s = Session::new(); + assert!(s.execute("select.inverse", json!({})).is_err()); +} + #[test] fn selection_modes() { let mut s = session_with_doc(); @@ -566,6 +681,10 @@ fn arrange_and_group() { let names: Vec = s.active().unwrap().doc.layers.iter().map(|l| l.name.clone()).collect(); assert_eq!(names, ["Background", "B", "A"]); assert!(s.execute("layer.arrange.bringForward", json!({})).is_err()); + // Bring to Front of the front layer does nothing: no error (an action plays it), no step. + let steps = s.active().unwrap().history.past_len(); + assert_eq!(s.execute("layer.arrange.bringToFront", json!({})).unwrap(), json!({"moved": false})); + assert_eq!(s.active().unwrap().history.past_len(), steps); s.execute("layer.groupLayers", json!({})).unwrap(); let doc = s.execute("document.inspect", json!({})).unwrap(); assert_eq!(doc["layers"][0]["kind"], "Group"); @@ -635,6 +754,33 @@ fn fill_layers_and_colors() { assert!(l < r); } +#[test] +fn gradient_fill_layer_angles_are_finite_and_normalised() { + // `angle` 1e39 overflowed f32 to infinity: the render went NaN and .pcraft refused to save. + let mut s = session_with_doc(); + let angle = |s: &Session| { + let st = s.active().unwrap(); + match &st.doc.layer(st.active_layer.unwrap()).unwrap().content { + photocraft_doc::LayerContent::Fill(photocraft_doc::Fill::Gradient { angle, .. }) => *angle, + _ => panic!("not a gradient fill"), + } + }; + for cmd in ["layer.newFillLayer.gradient", "gradient.presets.apply"] { + let layers = s.active().unwrap().doc.layers.len(); + for bad in [json!(1e39), json!(-1e300), json!("90")] { + assert!(s.execute(cmd, json!({"angle": bad})).is_err(), "{cmd} {bad}"); + } + assert_eq!(s.active().unwrap().doc.layers.len(), layers, "{cmd}: nothing added"); + // Normalised to (-180, 180] like gradient.fill.set. + for (a, want) in [(450.0, 90.0), (-90.0, -90.0), (-180.0, 180.0)] { + s.execute(cmd, json!({"angle": a})).unwrap(); + assert_eq!(angle(&s), want, "{cmd} {a}"); + } + s.execute(cmd, json!({})).unwrap(); + assert_eq!(angle(&s), 90.0, "{cmd}: default"); + } +} + #[test] fn default_colors_with_mask_targeted_resets_to_mask_colors() { let mut s = Session::new(); @@ -702,6 +848,38 @@ fn fill_layer_pixel_rewrites_refresh_the_effect_maps() { assert_eq!(after, fresh, "the effect maps track the cache surface, not the Fill spec alone"); } +#[test] +fn growing_the_canvas_re_renders_a_cached_fill_layer() { + // A PSD fill layer keeps its stored pixels as a fill cache covering only the old canvas. + // Canvas Size, an extending Crop and Reveal All must re-render the fill over the new area, + // as Photoshop does, instead of leaving the added bands transparent (#7362). + let grow: [(&str, Value); 2] = [ + ("image.canvasSize", json!({"width": 100, "anchor": "left"})), + ("image.crop", json!({"x": 0, "y": 0, "width": 100, "height": 48, "deleteCroppedPixels": false})), + ]; + for (cmd, params) in grow { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48, "background": "transparent"})).unwrap(); + s.execute("layer.newFillLayer.solidColor", json!({"color": "#ff0000"})).unwrap(); + s.edit("cache", |doc, active| { + let fmt = doc.pixel_format(); + let l = doc.layer_mut(active.unwrap()).unwrap(); + let photocraft_doc::LayerContent::Fill(f) = &l.content else { return Err(EngineError::Other("not a fill layer".into())) }; + let fill = f.clone(); + let mut surface = photocraft_raster::Surface::new(fmt); + surface.fill_rect(photocraft_geom::Rect::new(0, 0, 64, 48), &[1.0, 0.0, 0.0, 1.0]); + l.fill_cache = Some(photocraft_doc::FillCache { fill, surface }); + Ok(()) + }) + .unwrap(); + s.execute(cmd, params).unwrap(); + let d = &s.active().unwrap().doc; + assert_eq!(d.size.width, 100, "{cmd} grows the canvas"); + let px = photocraft_compose::render(d, d.bounds()).get(90, 20); + assert!(px[3] > 0.99 && px[0] > 0.99 && px[1] < 0.01, "{cmd}: the fill covers the added area, got {px:?}"); + } +} + #[test] fn group_effect_maps_follow_child_visibility_and_opacity() { // A styled group's maps come from its children's composite: hiding a child or changing its @@ -1074,7 +1252,8 @@ fn move_selected_layers_keeps_selected_group_children_and_supports_above_below() let b = s.execute("layer.new.layer", json!({"name": "B"})).unwrap()["layer"].as_u64().unwrap(); let c = s.execute("layer.new.layer", json!({"name": "C"})).unwrap()["layer"].as_u64().unwrap(); let group = s.execute("layer.new.group", json!({"name": "G"})).unwrap()["layer"].as_u64().unwrap(); - // Made before B moves into G: a new group goes above the active layer, which would then be inside G. + // Made from C, not from G: like Photoshop a new group goes inside an open active group. + s.execute("layer.select", json!({"layer": c})).unwrap(); let target = s.execute("layer.new.group", json!({"name": "Target"})).unwrap()["layer"].as_u64().unwrap(); s.execute("layer.moveTo", json!({"layer": b, "target": group, "position": "into"})).unwrap(); @@ -1129,6 +1308,55 @@ fn selecting_a_layer_does_not_dirty_the_document() { assert!(s.active().unwrap().is_dirty()); } +#[test] +fn selection_tools_do_not_dirty_the_document() { + // #6865: marquee, lasso, Select All / Inverse / Deselect and their undo and redo are + // history steps, but not edits, so a clean document stays clean. + for depth in [8, 16, 32] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth})).unwrap(); + let past = s.active().unwrap().history.past_len(); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 20, "height": 10})).unwrap(); + s.execute("select.rect", json!({"x": 30, "y": 20, "width": 8, "height": 8, "ellipse": true, "mode": "add"})).unwrap(); + s.execute("select.lasso", json!({"points": [[0, 0], [40, 0], [20, 30]], "mode": "subtract"})).unwrap(); + s.execute("select.inverse", json!({})).unwrap(); + s.execute("select.all", json!({})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + assert!(s.active().unwrap().history.past_len() > past, "selections are still history steps"); + assert!(!s.active().unwrap().is_dirty(), "depth {depth}: selecting is not an edit"); + assert!(s.undo() && s.undo()); + assert!(s.redo()); + assert!(!s.active().unwrap().is_dirty(), "depth {depth}: undoing a selection is not an edit"); + s.execute("history.selectState", json!({"state": past})).unwrap(); + assert!(!s.active().unwrap().is_dirty(), "depth {depth}: stepping back over selections is not an edit"); + // A real edit still dirties, with or without a selection, and a selection can't clean it. + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + s.execute("select.deselect", json!({})).unwrap(); + assert!(s.active().unwrap().is_dirty()); + assert!(s.undo()); + assert!(s.active().unwrap().is_dirty(), "the new layer is still unsaved"); + } +} + +#[test] +fn selections_keep_a_document_with_a_video_layer_clean() { + // #6865: video frames compare by tile pointer, so selecting in a document with a video + // layer doesn't mark it modified either; changing the frames still does. + let mut s = session_with_doc(); + s.execute("layer.videoLayers.newBlankVideoLayer", json!({})).unwrap(); + let st = s.active_mut().unwrap(); + st.saved_revision = st.revision; + s.execute("select.rect", json!({"x": 1, "y": 1, "width": 4, "height": 4})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + assert!(!s.active().unwrap().is_dirty(), "selecting is not an edit"); + assert!(s.undo()); + assert!(!s.active().unwrap().is_dirty(), "undoing a selection is not an edit"); + s.execute("layer.videoLayers.insertBlankFrame", json!({})).unwrap(); + assert!(s.active().unwrap().is_dirty(), "a new frame is an edit"); +} + #[test] fn coalesced_edits_share_one_history_step() { let mut s = Session::new(); @@ -1438,6 +1666,53 @@ fn document_inspect_and_activate_invalid_index() { assert_eq!(err_act.to_string(), "no document at index 9"); } +/// #7103: Photoshop's Select Document by offset (previous is -1, next +1) walks the open +/// documents in tab order from the active one. Past either end it is refused, not wrapped onto +/// another document, and bad params leave the active document as it was. +#[test] +fn document_activate_by_offset_walks_the_tabs() { + let mut s = Session::new(); + for name in ["a", "b", "c"] { + s.execute("file.new", json!({"width": 2, "height": 2, "name": name})).unwrap(); + } + let active = |s: &Session| s.active().unwrap().doc.name.clone(); + assert_eq!(active(&s), "c"); + assert_eq!(s.execute("document.activate", json!({"offset": -2})).unwrap(), json!({"document": 0})); + assert_eq!(active(&s), "a"); + assert_eq!(s.execute("document.activate", json!({"offset": 1})).unwrap(), json!({"document": 1})); + assert_eq!(active(&s), "b"); + s.execute("document.activate", json!({"offset": 0})).unwrap(); + assert_eq!(active(&s), "b"); + assert_eq!(s.execute("document.activate", json!({"document": 2})).unwrap(), json!({"document": 2})); + // A null key counts as omitted. + s.execute("document.activate", json!({"document": null, "offset": -1})).unwrap(); + assert_eq!(active(&s), "b"); + + let err = s.execute("document.activate", json!({"offset": 2})).unwrap_err().to_string(); + assert_eq!(err, "no document 2 after the active one, which is 2 of 3"); + let err = s.execute("document.activate", json!({"offset": -2})).unwrap_err().to_string(); + assert_eq!(err, "no document 2 before the active one, which is 2 of 3"); + for bad in [ + json!({}), + json!({"offset": null}), + json!({"offset": 1.5}), + json!({"offset": "1"}), + json!({"offset": i64::MAX}), + json!({"offset": i64::MIN}), + json!({"offset": u64::MAX}), + json!({"document": 0, "offset": 1}), + json!({"document": -1}), + ] { + assert!(s.execute("document.activate", bad.clone()).is_err(), "{bad}"); + assert_eq!(active(&s), "b", "{bad}"); + } + // Tabs, not the order the documents were opened in: with "c" dragged to the front, two tabs + // before "b" is "c". + s.execute("document.move", json!({"document": 2, "to": 0})).unwrap(); + s.execute("document.activate", json!({"offset": -2})).unwrap(); + assert_eq!(active(&s), "c"); +} + /// #2166, measured in Photoshop 25.1: a layer mask and the layer pixels keep separate /// foreground/background pairs, swapped when the edit target changes. #[test] @@ -1580,3 +1855,86 @@ fn merge_down_on_a_group_merges_the_group() { s.execute("edit.undo", json!({})).unwrap(); assert!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(group)).is_some()); } + +/// `restore_layer_selection` puts a multi-layer selection back, drops gone layers, keeps a clean +/// document clean, and refuses a gone active layer without changing anything. +#[test] +fn restore_layer_selection_puts_back_what_still_exists() { + let mut s = session_with_doc(); + let id = |v: Value| LayerId(v["layer"].as_u64().unwrap()); + let a = id(s.execute("layer.new.layer", json!({})).unwrap()); + let b = id(s.execute("layer.new.layer", json!({})).unwrap()); + let gone = LayerId(u64::MAX); + s.active_mut().unwrap().saved_revision = s.active().unwrap().revision; + s.restore_layer_selection(Some(b), &[a, gone, b], Some(a)).unwrap(); + let st = s.active().unwrap(); + assert_eq!((st.active_layer, st.selected_layers(), st.layer_anchor), (Some(b), vec![a, b], Some(a))); + assert_eq!(st.saved_revision, st.revision, "selection is not an edit"); + let snap = |s: &Session| { + let st = s.active().unwrap(); + (st.active_layer, st.selected_layers(), st.layer_anchor, st.revision, st.saved_revision) + }; + let before = snap(&s); + assert!(matches!(s.restore_layer_selection(Some(gone), &[gone], None), Err(EngineError::NoLayer(_)))); + assert_eq!(snap(&s), before, "an error changes nothing"); + s.restore_layer_selection(None, &[a], Some(a)).unwrap(); + let st = s.active().unwrap(); + assert_eq!((st.active_layer, st.selected_layers(), st.layer_anchor), (None, vec![], None), "no layer selected"); + assert!(Session::new().restore_layer_selection(None, &[], None).is_err(), "no document"); +} + +/// #4535: every edit snapshots the document into history. The large payloads it carries (the +/// XMP packet, Duotone's PSD data, an unsupported adjustment's bytes) are shared between the +/// snapshot and the document, not copied per state. +#[test] +fn history_states_share_large_document_payloads() { + use photocraft_doc::{Adjustment, Duotone, DuotoneInk, Layer, LayerContent}; + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); + let raw_id = { + let st = s.active_mut().unwrap(); + let doc = std::sync::Arc::make_mut(&mut st.doc); + doc.metadata.xmp = Some("x".repeat(1 << 20).into()); + doc.duotone = Some(Duotone { inks: vec![DuotoneInk::new("Black", [0.0; 3])], psd_raw: Some(vec![7u8; 1 << 16].into()) }); + let layer = Layer::new("kept", LayerContent::Adjustment(Adjustment::Unsupported { psd_key: "abcd".into(), raw: vec![9u8; 1 << 16].into() })); + let id = layer.id; + doc.layers.push(layer); + id + }; + s.execute("layer.new.layer", json!({})).unwrap(); + let st = s.active().unwrap(); + let before = st.history.state(st.history.past_len() - 1).unwrap(); + let (now, then) = (&st.doc, &before); + assert!(!std::sync::Arc::ptr_eq(now, then), "the edit made a new document"); + let xmp = |d: &Document| d.metadata.xmp.clone().unwrap(); + assert!(std::sync::Arc::ptr_eq(&xmp(now), &xmp(then)), "the XMP packet is shared"); + let duotone = |d: &Document| d.duotone.as_ref().and_then(|d| d.psd_raw.clone()).unwrap(); + assert!(std::sync::Arc::ptr_eq(&duotone(now), &duotone(then)), "the Duotone data is shared"); + let raw = |d: &Document| match &d.layer(raw_id).unwrap().content { + LayerContent::Adjustment(Adjustment::Unsupported { raw, .. }) => raw.clone(), + other => panic!("{other:?}"), + }; + assert!(std::sync::Arc::ptr_eq(&raw(now), &raw(then)), "the adjustment's bytes are shared"); +} + +#[test] +fn the_document_activation_generation_bumps_on_switch_and_back() { + let mut s = session_with_doc(); + let first = s.active().unwrap().doc.id; + let g1 = s.active_generation(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let second = s.active().unwrap().doc.id; + assert_ne!(second, first); + let g2 = s.active_generation(); + assert!(g2 > g1, "opening a document activates it"); + let i = s.documents().iter().position(|d| d.doc.id == first).expect("the first document"); + assert!(s.set_active(i)); + let g3 = s.active_generation(); + assert!(g3 > g2, "switching back is a new activation"); + // Re-selecting the already-active document is not a switch; neither is an edit. + assert!(s.set_active(i)); + assert_eq!(s.active_generation(), g3); + s.execute("layer.new.layer", json!({})).unwrap(); + assert_eq!(s.active_generation(), g3); + assert_eq!(s.active().unwrap().doc.id, first); +} diff --git a/crates/engine/src/transform_cmds.rs b/crates/engine/src/transform_cmds.rs deleted file mode 100644 index 1c0153a42e..0000000000 --- a/crates/engine/src/transform_cmds.rs +++ /dev/null @@ -1,1426 +0,0 @@ -//! Edit › Free Transform / Transform: projective transforms of layers (and the selection). - -use photocraft_algo::transform::{Homography, Interp, warp_output_bounds, warp_surface, warp_surface_with}; -use photocraft_color::PixelFormat; -use photocraft_doc::{Document, Layer, LayerContent, LayerId, Locks}; -use photocraft_geom::{Affine, Rect}; -use photocraft_raster::{Interrupt, Surface}; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -fn has_layer(s: &Session) -> std::result::Result<(), String> { - s.active().and_then(|d| d.active_layer).map(|_| ()).ok_or_else(|| "no active layer".into()) -} - -fn bad(msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: "edit.transform".into(), msg: msg.into() } -} - -/// Layers a transform moves as a whole, even with a selection: groups, and the kinds that keep the -/// transform instead of pixels (type, shapes, smart objects). Only pixel layers move just the -/// selected pixels. -pub fn moves_whole(layer: &Layer) -> bool { - matches!(layer.content, LayerContent::Group(_) | LayerContent::Text(_) | LayerContent::Shape(_) | LayerContent::Smart(_)) -} - -/// Document-space bounds a transform of `layer` starts from (what Free Transform frames): the -/// content within the selection, or all of it for a layer that [`moves_whole`] (#2630). -/// Content scans are cached per tile: snapping asks for every layer's bounds per Move drag. -pub fn transform_bounds(doc: &Document, layer: &Layer) -> Rect { - let content = whole_layer_bounds(layer); - match &doc.selection { - Some(sel) if !moves_whole(layer) => content.intersect(&sel.content_bounds()), - _ => content, - } -} - -/// The box Free Transform opens on `layer`, and the Move tool's transform controls show (#5297). -/// With a selection that limits the transform it is the selection's own bounds, as in Photoshop: -/// the box and the marching ants are one rectangle, however little of the selection the layer -/// fills. Empty, like [`transform_bounds`], when the selection holds none of the layer. -pub fn transform_frame(doc: &Document, layer: &Layer) -> Rect { - let b = transform_bounds(doc, layer); - if moves_whole(layer) { b } else { framed_by_selection(doc, b) } -} - -/// `bounds` (what a selection left of a transform's content), or the selection's own bounds. -fn framed_by_selection(doc: &Document, bounds: Rect) -> Rect { - match &doc.selection { - Some(sel) if !bounds.is_empty() => sel.content_bounds(), - _ => bounds, - } -} - -/// A group moves all its descendants, so its frame must not clip raster children to the selection. -fn whole_layer_bounds(layer: &Layer) -> Rect { - let content = match &layer.content { - LayerContent::Group(g) => g.children.iter().map(whole_layer_bounds).fold(Rect::EMPTY, |a, b| a.union(&b)), - _ => layer.surface().map_or(Rect::EMPTY, photocraft_compose::bounds::content_bounds), - }; - if content.is_empty() { layer.mask.as_ref().map_or(Rect::EMPTY, |m| photocraft_compose::bounds::content_bounds(&m.surface)) } else { content } -} - -/// Warp a surface whose pixels outside the content read as `default` (masks, selections): -/// warp the content with alpha, then flatten back onto the default value. -pub(crate) fn warp_gray(s: &Surface, h: &Homography, interp: Interp) -> Result { - let default = s.default_pixel().first().copied().unwrap_or(0.0); - let fmt = s.format(); - let src = s.content_bounds(); - let mut out = Surface::with_default(fmt, &[default]); - if src.is_empty() { - return Ok(out); - } - // Content-only copy (default 0 + alpha) so the "outside" is transparent during the warp. - let with_alpha = PixelFormat::new(fmt.mode, fmt.sample, true); - let mut tmp = Surface::new(with_alpha); - let v = crate::allocation::read_region(s, src, "transforming a mask or channel")?; - let mut lifted = - crate::allocation::filled(v.len().checked_mul(2).ok_or_else(|| bad("the mask is too large to transform"))?, 0.0, "transforming a mask or channel")?; - let (lifted_pairs, _) = lifted.as_chunks_mut::<2>(); - for (g, px) in v.iter().zip(lifted_pairs) { - px.copy_from_slice(&[*g, 1.0]); - } - tmp.try_write_region(src, &lifted)?; - let w = warp_surface(&tmp, src, h, interp)?; - // Clear the old content region, then composite the warped content over it. These are written as - // two sparse regions rather than one dense `src ∪ warped` block: a transform that moves the - // content far away (e.g. a huge translation) would otherwise allocate a buffer spanning both and - // hang/OOM. `Surface` is tile-sparse, so distant regions cost only their own tiles. - let old_len = (src.width() as usize).checked_mul(src.height() as usize).ok_or_else(|| bad("the mask is too large to transform"))?; - let old = crate::allocation::filled(old_len, default, "transforming a mask or channel")?; - out.try_write_region(src, &old)?; - let b = w.content_bounds(); - if !b.is_empty() { - let px = crate::allocation::read_region(&w, b, "flattening a transformed mask or channel")?; - let mut flat = crate::allocation::filled(px.len() / 2, 0.0, "flattening a transformed mask or channel")?; - for (p, f) in px.as_chunks::<2>().0.iter().zip(&mut flat) { - *f = p[0] * p[1] + default * (1.0 - p[1]); - } - out.try_write_region(b, &flat)?; - } - out.prune(); - Ok(out) -} - -/// Split a single-channel surface whose outside reads as its default (a mask, a channel) by a -/// selection: (the selected values as grey + alpha = selection, the surface with the selected -/// values replaced by the default). `None` for surfaces that aren't a single grey channel. -pub fn split_gray_selected(s: &Surface, sel: &Surface) -> Result> { - let fmt = s.format(); - if fmt.alpha || fmt.channels() != 1 { - return Ok(None); - } - let default = s.default_pixel().first().copied().unwrap_or(0.0); - let mut rest = s.clone(); - let mut lifted = Surface::new(PixelFormat::new(fmt.mode, fmt.sample, true)); - // Everything selected moves, including default-valued parts outside the painted content, so - // the moved region stays aligned with the moved pixels. - let area = sel.content_bounds(); - if area.is_empty() { - return Ok(Some((lifted, rest))); - } - let v = crate::allocation::read_region(s, area, "transforming a selected mask or channel")?; - let w = area.width() as usize; - let mut lp = crate::allocation::capacity( - v.len().checked_mul(2).ok_or_else(|| bad("the selected mask is too large to transform"))?, - "transforming a selected mask or channel", - )?; - let mut rp = crate::allocation::capacity(v.len(), "transforming a selected mask or channel")?; - for (i, g) in v.iter().enumerate() { - let (x, y) = (area.x0 + (i % w) as i32, area.y0 + (i / w) as i32); - let k = sel.sample_channel(x, y, 0); - lp.extend([*g, k]); - rp.push(g * (1.0 - k) + default * k); - } - lifted.try_write_region(area, &lp)?; - lifted.prune(); - rest.try_write_region(area, &rp)?; - rest.prune(); - Ok(Some((lifted, rest))) -} - -/// [`warp_gray`] limited to a selection: only the selected values move (the vacated area reads as -/// the default), as the selected pixels of the layer do. -pub(crate) fn warp_gray_selected(s: &Surface, sel: &Surface, h: &Homography, interp: Interp) -> Result { - move_gray_selected(s, sel, &|lifted, src| Ok(warp_surface(lifted, src, h, interp)?), &|s| warp_gray(s, h, interp)) -} - -/// [`warp_gray_selected`] with any geometric warp: `warp` moves the lifted grey + alpha values -/// (given their bounds), `whole` moves `s` when it isn't a single grey channel. -pub(crate) fn move_gray_selected( - s: &Surface, - sel: &Surface, - warp: &dyn Fn(&Surface, Rect) -> Result, - whole: &dyn Fn(&Surface) -> Result, -) -> Result { - let Some((lifted, mut out)) = split_gray_selected(s, sel)? else { return whole(s) }; - let src = lifted.content_bounds(); - if src.is_empty() { - return Ok(out); - } - let w = warp(&lifted, src)?; - let b = w.content_bounds(); - if !b.is_empty() { - let moved = crate::allocation::read_region(&w, b, "compositing transformed mask pixels")?; - let under = crate::allocation::read_region(&out, b, "compositing transformed mask pixels")?; - let mut flat = crate::allocation::filled(under.len(), 0.0, "compositing transformed mask pixels")?; - for (i, (p, u)) in moved.as_chunks::<2>().0.iter().zip(&under).enumerate() { - flat[i] = p[0] * p[1] + u * (1.0 - p[1]); - } - out.try_write_region(b, &flat)?; - } - out.prune(); - Ok(out) -} - -/// The surface Free Transform moves by itself when the params target one (`"target"`): an -/// unlinked layer mask, an alpha channel or the Quick Mask. `None` means the layer, with its -/// linked masks (a targeted *linked* mask moves together with its layer, as in Photoshop). -pub fn lone_target<'a>(doc: &'a Document, layer: Option, p: &Value) -> Result> { - use crate::channel_cmds::Target; - match crate::channel_cmds::target_of(p) { - Target::Pixels => Ok(None), - Target::Mask => { - let id = layer.ok_or_else(|| EngineError::Other("no active layer".into()))?; - let l = doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let m = l.mask.as_ref().ok_or_else(|| EngineError::Other("layer has no mask".into()))?; - Ok((!m.linked).then_some(&m.surface)) - } - Target::Alpha(i) => doc - .channels - .get(i) - .map(|c| Some(&c.surface)) - .ok_or_else(|| EngineError::Other(format!("no alpha channel {i} (document has {})", doc.channels.len()))), - Target::QuickMask => doc.quick_mask.as_ref().map(|c| Some(&c.surface)).ok_or_else(|| EngineError::Other("not in Quick Mask mode".into())), - } -} - -/// [`lone_target`], mutably. -pub fn lone_target_mut<'a>(doc: &'a mut Document, layer: Option, p: &Value) -> Result> { - if lone_target(doc, layer, p)?.is_none() { - return Ok(None); - } - crate::channel_cmds::target_surface(doc, layer, p).map(|(s, _)| Some(s)) -} - -/// The frame Free Transform starts from on a lone target: its painted content (the whole canvas -/// when nothing is painted), within the selection. -pub fn target_bounds(doc: &Document, surf: &Surface) -> Rect { - let content = surf.content_bounds(); - let content = if content.is_empty() { doc.bounds() } else { content }; - match &doc.selection { - Some(sel) => content.intersect(&sel.content_bounds()), - None => content, - } -} - -/// The box Free Transform opens on a lone target: like [`transform_frame`], the selection's own -/// bounds when there is one (#5297). -pub fn target_frame(doc: &Document, surf: &Surface) -> Rect { - framed_by_selection(doc, target_bounds(doc, surf)) -} - -/// The locks `id` inherits from the groups around it. -pub(crate) fn group_locks(doc: &Document, id: LayerId) -> Locks { - doc.path_of(id).and_then(|p| Some(doc.locks_at(p.split_last()?.1))).unwrap_or_default() -} - -/// The locks a transform of `l` meets: its own and `group`'s (those it inherits). The Background's -/// position lock doesn't count: Photoshop turns it into a normal layer first. -fn transform_locks(l: &Layer, group: Locks, background: bool) -> Locks { - let mut own = l.locks; - if background { - own.position = false; - own.transparency = false; - } - own.union(group) -} - -/// Would a transform of the layer `id` be refused because it, a group around it, or (for a group) -/// a layer inside it is locked in place? The same rule [`transform_layer`] applies at commit, so -/// Free Transform can refuse before its box opens, as Photoshop does. -pub fn locked_for_transform(doc: &Document, id: LayerId) -> bool { - let Some(l) = doc.layer(id) else { return false }; - // An explicit stack rather than recursion: documents can nest deeply. - let mut stack = vec![(l, group_locks(doc, id), doc.is_background(id))]; - while let Some((l, group, background)) = stack.pop() { - let locks = transform_locks(l, group, background); - if locks.position || locks.all { - return true; - } - if let LayerContent::Group(g) = &l.content { - stack.extend(g.children.iter().map(|c| (c, locks, false))); - } - } - false -} - -/// The largest result (px) a single Free Transform warp may build: 2³⁰, the bound Puppet Warp uses -/// too ([`photocraft_algo::puppet::MAX_WARP_PIXELS`]). Only the size of the result counts, not -/// where it lands, so moving a layer far off the canvas stays allowed. A scale past it is refused -/// rather than clipped (#3773): the warp would otherwise fill every tile of a result up to 2²¹ px -/// on a side before anything could fail. -pub const MAX_TRANSFORM_PIXELS: u64 = 1 << 30; - -/// Tests use a far lower budget, so proving a refusal never comes near allocating the real one. -#[cfg(not(test))] -const BUDGET: u64 = MAX_TRANSFORM_PIXELS; -#[cfg(test)] -const BUDGET: u64 = 1 << 20; - -/// Refuse a warp of the content inside `src` by `h` whose result would exceed the pixel budget, -/// before anything is built. `what` names the surface in the message. -pub(crate) fn check_budget(src: Rect, h: &Homography, what: &str) -> Result<()> { - let Some(b) = warp_output_bounds(src, h) else { return Ok(()) }; - let (w, ht) = (u64::from(b.width()), u64::from(b.height())); - if w.saturating_mul(ht) > BUDGET { - let detail = format!("the transformed {what} would be {w} x {ht} px, more than the {BUDGET} px limit"); - return Err(crate::allocation::error("transforming pixels", &detail)); - } - Ok(()) -} - -/// [`check_budget`] for every warp [`transform_layer`] would make of `l` (for a group, of every -/// layer inside it), so a transform that doesn't fit is refused before any layer is built and a -/// group that partly fits isn't half-transformed. -fn check_layer_budget(doc_sel: Option<&Surface>, l: &Layer, h: &Homography) -> Result<()> { - if h.is_identity() { - return Ok(()); - } - // An explicit stack rather than recursion: documents can nest deeply. - let mut stack = vec![(l, doc_sel)]; - while let Some((l, sel)) = stack.pop() { - match &l.content { - LayerContent::Group(g) => stack.extend(g.children.iter().map(|c| (c, None))), - LayerContent::Text(_) | LayerContent::Shape(_) => {} - LayerContent::Smart(sm) => { - if let Some(c) = &sm.cache { - check_budget(c.content_bounds(), h, "smart object")?; - } - if let Some(m) = &sm.filter_mask { - check_budget(m.surface.content_bounds(), h, "smart filter mask")?; - } - } - _ => { - if let Some(surf) = l.surface() { - // With a selection only the lifted pixels are warped (`split_selected`). - let src = match sel { - Some(sel) => surf.content_bounds().intersect(&sel.content_bounds()), - None => surf.content_bounds(), - }; - check_budget(src, h, &format!("layer \"{}\"", l.name))?; - } - } - } - if let Some(m) = &l.mask - && m.linked - { - // With a selection only its region of the mask moves (`warp_gray_selected`). - let src = match sel.filter(|_| !moves_whole(l)) { - Some(sel) => sel.content_bounds(), - None => m.surface.content_bounds(), - }; - check_budget(src, h, "layer mask")?; - } - } - Ok(()) -} - -/// `group` holds the locks `l` inherits from the groups around it ([`group_locks`]). A Background -/// is turned into a normal layer by the caller first ([`crate::extra_cmds::background_to_layer`]). -/// Refused, with `l` untouched, when a result would exceed [`MAX_TRANSFORM_PIXELS`]. -pub(crate) fn transform_layer(doc_sel: Option<&Surface>, group: Locks, l: &mut Layer, h: &Homography, affine: Option, interp: Interp) -> Result<()> { - transform_layer_with(doc_sel, group, l, h, affine, interp, &Interrupt::NONE) -} - -/// [`transform_layer`] with progress and cancellation for the pixel warp (the slow part on a -/// large layer, #1015). -fn transform_layer_with( - doc_sel: Option<&Surface>, - group: Locks, - l: &mut Layer, - h: &Homography, - affine: Option, - interp: Interp, - ctl: &Interrupt, -) -> Result<()> { - check_layer_budget(doc_sel, l, h)?; - apply_transform(doc_sel, group, l, h, affine, interp, ctl) -} - -fn apply_transform( - doc_sel: Option<&Surface>, - group: Locks, - l: &mut Layer, - h: &Homography, - affine: Option, - interp: Interp, - ctl: &Interrupt, -) -> Result<()> { - crate::allocation::checkpoint("transforming pixels")?; - let locks = transform_locks(l, group, false); - if locks.position || locks.all { - return Err(EngineError::Other(format!("layer \"{}\" is locked", l.name))); - } - if h.is_identity() { - if let LayerContent::Group(g) = &mut l.content { - for c in g.children.iter_mut() { - apply_transform(None, locks, c, h, affine, interp, ctl)?; - } - } - return Ok(()); - } - // With a selection only the selected pixels move, and so only the same region of a linked - // mask (#205). Groups, type, shapes and smart objects move whole. - let mask_sel = doc_sel.filter(|_| !moves_whole(l)); - match &mut l.content { - LayerContent::Group(g) => { - for c in g.children.iter_mut() { - apply_transform(None, locks, c, h, affine, interp, ctl)?; - } - } - LayerContent::Text(t) => { - let Some(a) = affine else { - return Err(EngineError::Other("Distort and Perspective need rasterized type (Type › Rasterize Type Layer)".into())); - }; - t.transform = a.mul(&t.transform); - } - LayerContent::Shape(sh) => match affine { - Some(a) => crate::vector_cmds::transform_shape(sh, &a), - // Distort and Perspective map the path and keep the shape (#3009). - None => crate::vector_cmds::map_shape(sh, Some(h), None)?, - }, - LayerContent::Smart(sm) => { - // Smart objects keep the transform and re-render from their source afterwards - // (`refresh_text`), so repeated transforms don't degrade the pixels. Distort and - // Perspective keep the full projective map, so the fourth corner survives re-renders. - match affine { - Some(a) => crate::smart_cmds::transform_placement(sm, &a), - None => crate::smart_cmds::set_placement(sm, h.mul(&crate::smart_cmds::placement(sm))), - } - // Fallback appearance for sources that can't be re-rendered. - if let Some(c) = &mut sm.cache { - let src = c.content_bounds(); - *c = warp_surface(c, src, h, interp)?; - } - if let Some(m) = &mut sm.filter_mask { - m.surface = warp_gray(&m.surface, h, interp)?; - } - } - _ => { - if let Some(surf) = l.surface_mut() { - *surf = match doc_sel { - // Only the selected pixels move: lift them, clear them, warp and paste back. The - // lifted pixels' own bounds are the edge a stretch repeats (#3773). - Some(sel) => { - let (lifted, mut rest) = split_selected(surf, sel)?; - let moved = warp_surface_with(&lifted, lifted.content_bounds(), h, interp, ctl)?.ok_or(EngineError::Cancelled)?; - composite_over(&mut rest, &moved); - rest.prune(); - rest - } - None => { - let src = surf.content_bounds(); - warp_surface_with(surf, src, h, interp, ctl)?.ok_or(EngineError::Cancelled)? - } - }; - } - } - } - if let Some(m) = l.mask.as_mut() - && m.linked - { - m.surface = match mask_sel { - Some(sel) => warp_gray_selected(&m.surface, sel, h, interp)?, - None => warp_gray(&m.surface, h, interp)?, - }; - } - if let Some(vm) = l.vector_mask.as_mut() - && vm.linked - { - vm.path = match affine { - Some(a) => vm.path.transform(&a), - None => crate::vector_cmds::map_path(&vm.path, Some(h), None)?, - }; - } - Ok(()) -} - -/// Split a layer surface by a selection: (selected pixels, everything else), both with alpha. -pub fn split_selected(surf: &Surface, sel: &Surface) -> Result<(Surface, Surface)> { - let fmt = surf.format(); - let with_alpha = PixelFormat::new(fmt.mode, fmt.sample, true); - let mut lifted = Surface::new(with_alpha); - let mut rest = surf.try_convert(with_alpha)?; - let src = surf.content_bounds(); - if src.is_empty() { - return Ok((lifted, rest)); - } - let n = with_alpha.channels(); - let px = crate::allocation::read_region(&rest, src, "transforming selected pixels")?; - let mut lp = crate::allocation::filled(px.len(), 0.0, "transforming selected pixels")?; - lp.copy_from_slice(&px); - let mut rp = px; - let w = src.width() as usize; - // A pixel the selection fully lifts out is reset to the surface's (transparent) default; - // keeping its colour would make `content_bounds` still count it, so the layer's bounds would - // include the area the pixels were moved from. - let mut rest_clear = rest.default_pixel(); - if rest_clear.last().is_some_and(|a| *a > 0.0) { - rest_clear.fill(0.0); - } - for (i, (l, r)) in lp.chunks_exact_mut(n).zip(rp.chunks_exact_mut(n)).enumerate() { - let (x, y) = (src.x0 + (i % w) as i32, src.y0 + (i / w) as i32); - let k = sel.sample_channel(x, y, 0); - l[n - 1] *= k; - r[n - 1] *= 1.0 - k; - // Likewise an unselected pixel leaves nothing in the lifted surface, so its bounds are the - // selected pixels' (what a stretch repeats at the edge, #3773). - if l[n - 1] <= 0.0 { - l.fill(0.0); - } - if r[n - 1] <= 0.0 { - r.iter_mut().zip(&rest_clear).for_each(|(d, c)| *d = *c); - } - } - lifted.try_write_region(src, &lp)?; - lifted.prune(); - rest.try_write_region(src, &rp)?; - Ok((lifted, rest)) -} - -pub(crate) fn refresh_text(doc: &Document, l: &mut Layer) { - match &mut l.content { - LayerContent::Text(t) => crate::type_cmds::refresh(doc, t), - LayerContent::Shape(sh) => crate::vector_cmds::refresh_shape(doc, sh), - LayerContent::Smart(_) => { - // Unavailable sources keep the warped cache. - let _ = crate::smart_cmds::refresh_layer(doc, l); - } - LayerContent::Group(g) => g.children.iter_mut().for_each(|c| refresh_text(doc, c)), - _ => {} - } -} - -/// Normal-blend `top` over `dst` (both straight alpha, same format). -pub(crate) fn composite_over(dst: &mut Surface, top: &Surface) { - let b = top.content_bounds(); - if b.is_empty() { - return; - } - let n = dst.format().channels(); - let a = n - 1; - let t = top.read_region(b); - let mut d = dst.read_region(b); - for (dp, tp) in d.chunks_exact_mut(n).zip(t.chunks_exact(n)) { - let (ta, da) = (tp[a], dp[a]); - let oa = ta + da * (1.0 - ta); - if oa > 0.0 { - for c in 0..a { - dp[c] = (tp[c] * ta + dp[c] * da * (1.0 - ta)) / oa; - } - } - dp[a] = oa; - } - dst.write_region(b, &d); -} - -/// The relative form (`scaleX`, `scaleY` in percent, `rotate` in degrees clockwise, `dx`, `dy` -/// in pixels), as Photoshop records a Free Transform: scale and rotate about the frame's -/// centre, then move. `None` when none of them is given. -fn relative_matrix(p: &Value, rect: [f64; 4]) -> Result> { - let num = |k: &str| -> Result> { - match p.get(k) { - None | Some(Value::Null) => Ok(None), - Some(v) => v.as_f64().filter(|f| f.is_finite()).map(Some).ok_or_else(|| bad(format!("`{k}` must be a finite number"))), - } - }; - let (sx, sy, rot, dx, dy) = (num("scaleX")?, num("scaleY")?, num("rotate")?, num("dx")?, num("dy")?); - if sx.is_none() && sy.is_none() && rot.is_none() && dx.is_none() && dy.is_none() { - return Ok(None); - } - let (sx, sy) = (sx.unwrap_or(100.0) / 100.0, sy.unwrap_or(100.0) / 100.0); - if sx.abs() < 1e-6 || sy.abs() < 1e-6 { - return Err(bad("`scaleX` and `scaleY` must not be 0")); - } - let (cx, cy) = ((rect[0] + rect[2]) / 2.0, (rect[1] + rect[3]) / 2.0); - let (sin, cos) = rot.unwrap_or(0.0).to_radians().sin_cos(); - // (x, y) → translate(-c) → scale → rotate → translate(c + d). - let (a, b, c, d) = (sx * cos, sx * sin, -sy * sin, sy * cos); - let e = cx + dx.unwrap_or(0.0) - a * cx - c * cy; - let f = cy + dy.unwrap_or(0.0) - b * cx - d * cy; - Ok(Some(Homography([a, c, e, b, d, f, 0.0, 0.0, 1.0]))) -} - -fn quad_param(p: &Value) -> Option<[[f64; 2]; 4]> { - let a = p.get("quad")?.as_array()?; - if a.len() != 4 { - return None; - } - let mut q = [[0.0; 2]; 4]; - for (i, v) in a.iter().enumerate() { - let c = v.as_array()?; - q[i] = [c.first()?.as_f64()?, c.get(1)?.as_f64()?]; - } - Some(q) -} - -/// `p`, with the Channels panel's targeted alpha channel or Quick Mask as its `"target"` when it -/// names neither a target nor a layer (#2835). -fn with_panel_target(s: &Session, p: &Value) -> Value { - let mut p = if p.is_object() { p.clone() } else { json!({}) }; - if p.get("target").is_none() - && p.get("layer").is_none() - && let (Some(m), Some(Value::Object(t))) = (p.as_object_mut(), crate::channel_cmds::targeted_channel(s)) - { - m.extend(t); - } - p -} - -fn transform(s: &mut Session, p: &Value) -> Result { - let p = &with_panel_target(s, p); - let st = s.active().ok_or(EngineError::NoDocument)?; - let id = crate::commands::explicit_layer(s, p)?.or(st.active_layer); - let lone = lone_target(&st.doc, id, p)?; - let id = match (id, lone.is_some() && crate::channel_cmds::is_channel_target(p)) { - (_, true) => None, - (Some(id), false) => Some(id), - (None, false) => return Err(EngineError::Other("no active layer".into())), - }; - let rect = match p.get("rect").and_then(Value::as_array) { - Some(r) if r.len() == 4 => { - let v: Vec = r.iter().map(|x| x.as_f64().unwrap_or(0.0)).collect(); - [v[0], v[1], v[2], v[3]] - } - _ => { - let b = match (lone, id) { - (Some(surf), _) => target_bounds(&st.doc, surf), - (None, Some(id)) => transform_bounds(&st.doc, st.doc.layer(id).ok_or(EngineError::NoLayer(id))?), - (None, None) => Rect::EMPTY, - }; - if b.is_empty() { - return Err(EngineError::Other("nothing to transform".into())); - } - [b.x0 as f64, b.y0 as f64, b.x1 as f64, b.y1 as f64] - } - }; - let h = if let Some(q) = quad_param(p) { - Homography::rect_to_quad(rect, q).ok_or_else(|| bad("degenerate quad"))? - } else if let Some(m) = p.get("matrix").and_then(Value::as_array) { - let v: Vec = m.iter().filter_map(Value::as_f64).collect(); - if v.len() != 6 { - return Err(bad("matrix must be [a, b, c, d, e, f]")); - } - // Affine [a b c d e f] maps (x, y) → (a·x + c·y + e, b·x + d·y + f). - Homography([v[0], v[2], v[4], v[1], v[3], v[5], 0.0, 0.0, 1.0]) - } else if let Some(m) = relative_matrix(p, rect)? { - m - } else { - return Err(bad("pass `quad` (where the rect's corners go), `matrix`, or `scaleX`/`scaleY`/`rotate`/`dx`/`dy`")); - }; - if h.inverse().is_none() { - return Err(bad("the transform collapses the layer")); - } - let m = h.0; - let affine = - (m[6].abs() < 1e-12 && m[7].abs() < 1e-12).then(|| Affine { m: [m[0] / m[8], m[3] / m[8], m[1] / m[8], m[4] / m[8], m[2] / m[8], m[5] / m[8]] }); - let interp = Interp::parse(p.get("interpolation").and_then(Value::as_str).unwrap_or("bicubic")); - let lone = lone.is_some(); - let p = p.clone(); - // A background job when started with `Session::start` (#1015): the warp of a large layer runs - // on a worker with progress, and can be cancelled. - crate::jobs::edit_job( - s, - "Free Transform", - move |doc, _, ctx| ctx.stage(0.0, 1.0, "Transforming", |ctl| transform_doc(doc, id, &p, lone, &h, affine, interp, ctl)), - move |()| json!({"rect": rect}), - ) -} - -/// The edit of [`transform`]: the layer (or a lone mask or channel) and the selection outline. -#[allow(clippy::too_many_arguments)] -fn transform_doc( - doc: &mut Document, - id: Option, - p: &Value, - lone: bool, - h: &Homography, - affine: Option, - interp: Interp, - ctl: &Interrupt, -) -> Result<()> { - let sel = doc.selection.clone(); - // The selection outline moves too: price it before anything is built. - if let Some(sel) = sel.as_ref().filter(|_| !h.is_identity()) { - check_budget(sel.content_bounds(), h, "selection")?; - } - if lone { - // A targeted unlinked mask, alpha channel or Quick Mask transforms by itself. - if !h.is_identity() { - let (surf, _) = crate::channel_cmds::target_surface(doc, id, p)?; - let src = sel.as_ref().map_or_else(|| surf.content_bounds(), Surface::content_bounds); - check_budget(src, h, "mask or channel")?; - *surf = match &sel { - Some(sel) => warp_gray_selected(surf, sel, h, interp)?, - None => warp_gray(surf, h, interp)?, - }; - if let Some(sel) = &doc.selection { - doc.selection = Some(warp_gray(sel, h, Interp::Bilinear)?).filter(|s| !s.content_bounds().is_empty()); - } - } - return Ok(()); - } - let id = id.ok_or_else(|| EngineError::Other("no active layer".into()))?; - let is_group = doc.layer(id).is_some_and(Layer::is_group); - let group = group_locks(doc, id); - // Photoshop turns the Background into a normal layer before transforming it. - crate::extra_cmds::background_to_layer(doc, id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - transform_layer_with(if is_group { None } else { sel.as_ref() }, group, l, h, affine, interp, ctl)?; - if !h.is_identity() { - // Type layers re-render from their new transform. - let snapshot = doc.clone(); - if let Some(l) = doc.layer_mut(id) { - refresh_text(&snapshot, l); - } - // The selection outline moves with the pixels. - if let Some(sel) = &doc.selection { - doc.selection = Some(warp_gray(sel, h, Interp::Bilinear)?).filter(|s| !s.content_bounds().is_empty()); - } - } - Ok(()) -} - -pub fn specs() -> Vec { - vec![CommandSpec { - id: "edit.transform", - label: "Free Transform", - menu: &[], - shortcut: None, - params: r##"{"layer":id?,"rect":[x0,y0,x1,y1]? (source frame; default = layer content ∩ selection),"quad":[[x,y]×4]? (where the frame's corners go, clockwise from top-left),"matrix":[a,b,c,d,e,f]? (affine alternative),"scaleX":%?,"scaleY":%?,"rotate":deg?,"dx":px?,"dy":px? (relative alternative: scale and rotate about the frame's centre, then move, as a Photoshop action records a Free Transform),"interpolation":"bicubic|bilinear|nearest"="bicubic","target":"pixels"|"mask"|"quickMask"|{"channel":i}=Channels panel target (an unlinked mask, an alpha channel or the Quick Mask transforms alone; a linked mask moves with its layer)}"##, - enabled: has_layer, - journal: true, - run: transform, - }] -} - -#[cfg(test)] -mod tests { - use super::*; - - fn session() -> Session { - session_depth(8) - } - - fn session_depth(depth: u32) -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 100, "depth": depth})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - l.surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 20), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - fn active_bounds(s: &Session) -> Rect { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds() - } - - // ---- Pixel budget (#3773). Tests run with a 2²⁰ px budget, so nothing here comes near the real one. ---- - - /// A 16 x 16 document whose active layer is a 1 x 8 column at x = 10. - fn column() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 16, "height": 16})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 2, 11, 10), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - /// Stretch x by `k` about x = 10 (the column's left edge). - fn stretch(k: f64) -> Value { - json!({"matrix": [k, 0, 0, 1, -10.0 * k, 0], "interpolation": "nearest"}) - } - - #[test] - fn a_scale_past_the_pixel_budget_is_refused_and_changes_nothing() { - let mut s = column(); - let st = s.active().unwrap(); - let (before, revision, steps) = (st.doc.clone(), st.revision, st.history.past_len()); - // 200000 x 10 px of output: twice the test budget. - let err = s.execute("edit.transform", stretch(2e5)).unwrap_err().to_string(); - assert!(err.starts_with("not enough memory for transforming pixels"), "{err}"); - assert!(err.contains("would be 200002 x 10 px") && err.ends_with("the document was not changed"), "{err}"); - let st = s.active().unwrap(); - assert!(st.doc == before, "the document is untouched"); - assert_eq!((st.revision, st.history.past_len()), (revision, steps)); - // Within the budget the same stretch still works, nothing clipped. - s.execute("edit.transform", stretch(1e4)).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(0, 2, 10_000, 10)); - } - - #[test] - fn a_group_with_one_layer_past_the_budget_is_not_half_transformed() { - let mut s = column(); - let wide = s.active().unwrap().active_layer.unwrap(); - // Over the column: a 1 px dot, which fits the budget stretched by the same amount. - s.execute("layer.new.layer", json!({})).unwrap(); - let dot = s.active().unwrap().active_layer.unwrap(); - s.edit("paint", |doc, _| { - doc.layer_mut(dot).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 12, 11, 13), &[0.0, 1.0, 0.0, 1.0]); - // Widen the column to 8 px (x 2..10), so it alone needs 400002 x 10 px. - doc.layer_mut(wide).unwrap().surface_mut().unwrap().fill_rect(Rect::new(2, 2, 10, 10), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.select", json!({"layer": wide.0})).unwrap(); - s.execute("layer.select", json!({"layer": dot.0, "mode": "add"})).unwrap(); - let group = LayerId(s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap()); - let st = s.active().unwrap(); - assert!(matches!(&st.doc.layer(group).unwrap().content, LayerContent::Group(g) if g.children.len() == 2)); - let (before, revision) = (st.doc.clone(), st.revision); - let k = 5e4; - assert!(s.execute("edit.transform", json!({"layer": group.0, "matrix": [k, 0, 0, 1, -10.0 * k, 0]})).is_err()); - let st = s.active().unwrap(); - assert!(st.doc == before && st.revision == revision, "nothing changed"); - // The layer-level check runs before any child is warped, so even the layer itself - // (not just the edit's copy of the document) is left as it was. - let mut l = st.doc.layer(group).unwrap().clone(); - let h = Homography([k, 0.0, -10.0 * k, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0]); - assert!(transform_layer(None, Locks::default(), &mut l, &h, None, Interp::Nearest).is_err()); - assert!(&l == st.doc.layer(group).unwrap(), "no child was transformed"); - } - - #[test] - fn moving_a_small_layer_far_off_the_canvas_is_within_budget() { - let mut s = session(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 900_000, -900_000]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(900_010, -899_990, 900_030, -899_980)); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, -900_000, 900_000]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 20), "and back, every pixel kept"); - } - - #[test] - fn selections_masks_and_channels_have_the_same_budget() { - // The selection outline of a layer transform. - let mut s = column(); - s.execute("select.rect", json!({"x": 10, "y": 2, "width": 1, "height": 8})).unwrap(); - let before = s.active().unwrap().doc.clone(); - let err = s.execute("edit.transform", stretch(2e5)).unwrap_err().to_string(); - assert!(err.contains("transformed selection would be"), "{err}"); - // Transform Selection. - let err = s.execute("select.transformSelection", stretch(2e5)).unwrap_err().to_string(); - assert!(err.contains("transformed selection would be"), "{err}"); - assert!(s.active().unwrap().doc == before); - // A targeted alpha channel. - let mut s = column(); - s.execute("channel.new", json!({})).unwrap(); - s.edit("paint channel", |doc, _| { - doc.channels[0].surface.fill_rect(Rect::new(10, 2, 11, 10), &[1.0]); - Ok(()) - }) - .unwrap(); - let before = s.active().unwrap().doc.clone(); - let mut p = stretch(2e5); - p["target"] = json!({"channel": 0}); - let err = s.execute("edit.transform", p).unwrap_err().to_string(); - assert!(err.contains("transformed mask or channel would be"), "{err}"); - assert!(s.active().unwrap().doc == before); - } - - #[test] - fn scale_via_quad_and_undo() { - let mut s = session(); - s.execute("edit.transform", json!({"quad": [[10, 10], [50, 10], [50, 30], [10, 30]]})).unwrap(); - let b = active_bounds(&s); - assert!(b.width().abs_diff(40) <= 2 && b.height().abs_diff(20) <= 2, "{b:?}"); - s.undo(); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 20)); - } - - #[test] - fn affine_matrix_and_perspective() { - let mut s = session(); - // Translate by (40, 50). - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(50, 60, 70, 70)); - // Perspective: top edge narrower than the bottom. - s.execute("edit.transform", json!({"quad": [[55, 60], [65, 60], [75, 70], [45, 70]]})).unwrap(); - let b = active_bounds(&s); - assert!(b.x0 <= 46 && b.x1 >= 74, "{b:?}"); - assert!(s.execute("edit.transform", json!({"quad": [[0, 0], [0, 0], [0, 0], [0, 0]]})).is_err()); - } - - #[test] - fn background_becomes_layer_0() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 40})).unwrap(); - s.execute("edit.transform", json!({"quad": [[0, 0], [20, 0], [20, 20], [0, 20]]})).unwrap(); - let st = s.active().unwrap(); - let l = &st.doc.layers[0]; - assert_eq!(l.name, "Layer 0"); - assert!(l.surface().unwrap().format().alpha); - assert_eq!(l.surface().unwrap().pixel(30, 30)[3], 0.0, "revealed area is transparent"); - } - - /// `locked_for_transform` (what Free Transform asks before its box opens, #2585) agrees with - /// what `edit.transform` refuses at commit: a position lock, its own or a group's; never the - /// Background's (it becomes Layer 0). - #[test] - fn locked_for_transform_matches_the_commit() { - let quad = json!({"quad": [[10, 10], [50, 10], [50, 30], [10, 30]]}); - let lock = |s: &mut Session, id: LayerId, on: bool| { - s.edit("lock", |doc, _| { - doc.layer_mut(id).unwrap().locks.position = on; - Ok(()) - }) - .unwrap(); - }; - let mut s = session(); - let layer = s.active().unwrap().active_layer.unwrap(); - lock(&mut s, layer, true); - assert!(locked_for_transform(&s.active().unwrap().doc, layer)); - assert!(s.execute("edit.transform", quad.clone()).is_err()); - lock(&mut s, layer, false); - let group = LayerId(s.execute("layer.groupLayers", json!({"layer": layer.0})).unwrap()["layer"].as_u64().unwrap()); - lock(&mut s, group, true); - s.execute("layer.select", json!({"layer": layer.0})).unwrap(); - assert!(locked_for_transform(&s.active().unwrap().doc, layer), "the group's lock reaches the layer"); - assert!(s.execute("edit.transform", quad.clone()).is_err()); - let bg = s.active().unwrap().doc.layers[0].id; - assert!(!locked_for_transform(&s.active().unwrap().doc, bg), "the Background transforms"); - } - - #[test] - fn with_selection_only_selected_pixels_move() { - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 50]})).unwrap(); - let st = s.active().unwrap(); - let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - assert_eq!(surf.pixel(15, 15)[3], 0.0, "lifted area is cleared"); - assert_eq!(surf.pixel(25, 15)[3], 1.0, "unselected part stays"); - assert_eq!(surf.pixel(15, 65)[3], 1.0, "moved pixels land"); - let sel = st.doc.selection.as_ref().unwrap().content_bounds(); - assert_eq!((sel.y0, sel.y1), (60, 70), "selection moves too"); - } - - #[test] - fn with_selection_bounds_drop_the_vacated_area() { - // #1097: moving every pixel through a selection left transparent but coloured pixels - // behind, so the layer bounds were the union of the old and new positions. - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 20, "height": 10})).unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 50, 0]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(60, 10, 80, 20)); - } - - #[test] - fn type_layers_transform_as_vectors() { - let mut s = session(); - let r = s.execute("type.create", json!({"x": 10, "y": 50, "text": "Hi", "size": 20})).unwrap(); - let id = r["layer"].as_u64().unwrap(); - let w0 = active_bounds(&s).width(); - s.execute("edit.transform", json!({"layer": id, "matrix": [2, 0, 0, 2, -10, -50]})).unwrap(); - let w1 = active_bounds(&s).width(); - assert!(w1 as f32 > w0 as f32 * 1.8, "{w0} → {w1}"); - let st = s.active().unwrap(); - let LayerContent::Text(t) = &st.doc.layer(LayerId(id)).unwrap().content else { panic!() }; - assert!((t.transform.m[0] - 2.0).abs() < 1e-9); - // Perspective on type is refused, like Photoshop. - assert!(s.execute("edit.transform", json!({"layer": id, "quad": [[0, 0], [10, 0], [12, 10], [-2, 10]]})).is_err()); - } - - #[test] - fn shapes_move_and_transform_as_vectors() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); - s.execute("shape.create", json!({"kind": "rect", "rect": [10, 10, 40, 20], "fill": "#ff0000"})).unwrap(); - s.execute("layer.translate", json!({"dx": 30, "dy": 5})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(40, 15, 80, 35)); - s.execute("edit.transform", json!({"matrix": [2, 0, 0, 2, -40, -15]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(40, 15, 120, 55)); - // Distort maps the path and keeps a live shape (#3009): the rectangle's corners go to the quad. - let quad = [[40.0, 15.0], [120.0, 15.0], [130.0, 55.0], [30.0, 55.0]]; - s.execute("edit.transform", json!({ "quad": quad })).unwrap(); - let st = s.active().unwrap(); - let LayerContent::Shape(sh) = &st.doc.layer(st.active_layer.unwrap()).unwrap().content else { panic!("still a shape layer") }; - let mut anchors: Vec<[f64; 2]> = sh.path.subpaths[0].knots.iter().map(|k| [k.anchor.x, k.anchor.y]).collect(); - let mut want = quad.to_vec(); - anchors.sort_by(|a, b| a.partial_cmp(b).unwrap()); - want.sort_by(|a, b| a.partial_cmp(b).unwrap()); - assert!(anchors.iter().zip(&want).all(|(a, b)| (a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6), "{anchors:?}"); - assert!(sh.live.is_none(), "the live rectangle is now a path"); - assert_eq!(active_bounds(&s), Rect::new(30, 15, 130, 55)); - // Warp bends the path too: the arc adds anchors along the curved edges. - s.execute("edit.transform.warp", json!({"style": "arc", "bend": 50})).unwrap(); - let st = s.active().unwrap(); - let LayerContent::Shape(sh) = &st.doc.layer(st.active_layer.unwrap()).unwrap().content else { panic!("still a shape layer") }; - assert!(sh.path.subpaths[0].knots.len() > 4, "{}", sh.path.subpaths[0].knots.len()); - assert!(!active_bounds(&s).is_empty()); - } - - #[test] - fn type_moves_with_the_move_command() { - let mut s = session(); - s.execute("type.create", json!({"x": 10, "y": 50, "text": "Hi", "size": 20})).unwrap(); - let b0 = active_bounds(&s); - s.execute("layer.translate", json!({"dx": 20, "dy": 10})).unwrap(); - let b1 = active_bounds(&s); - assert_eq!((b1.x0 - b0.x0, b1.y0 - b0.y0), (20, 10)); - } - - // ---- Layer masks and targets (#205) ---- - - /// `session()` plus a reveal-all mask with `hide` painted black (linked unless `linked` is false). - fn masked(hide: &[Rect], linked: bool) -> Session { - masked_depth(hide, linked, 8) - } - - fn masked_depth(hide: &[Rect], linked: bool, depth: u32) -> Session { - let mut s = session_depth(depth); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.edit("mask", |doc, active| { - let m = doc.layer_mut(active.unwrap()).unwrap().mask.as_mut().unwrap(); - for r in hide { - m.surface.fill_rect(*r, &[0.0]); - } - m.linked = linked; - Ok(()) - }) - .unwrap(); - s - } - - fn mask_at(s: &Session, x: i32, y: i32) -> f32 { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface.sample_channel(x, y, 0) - } - - fn alpha_at(s: &Session, x: i32, y: i32) -> f32 { - let st = s.active().unwrap(); - st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().pixel(x, y)[3] - } - - #[test] - fn linked_raster_mask_moves_with_the_pixels() { - let mut s = masked(&[Rect::new(10, 10, 20, 20)], true); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(50, 60, 70, 70)); - assert!(mask_at(&s, 55, 65) < 0.01, "the hidden half moved with the pixels"); - assert!(mask_at(&s, 65, 65) > 0.99); - assert!(mask_at(&s, 15, 15) > 0.99, "nothing hidden left behind"); - } - - #[test] - fn linked_mask_with_a_selection_moves_only_the_selected_region() { - // The selection covers the left half of the red rect; the mask hides its left quarter - // (selected) and its right quarter (not selected). - let mut s = masked(&[Rect::new(10, 10, 15, 20), Rect::new(25, 10, 30, 20)], true); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 50]})).unwrap(); - // Moved pixels and their mask line up. - assert_eq!(alpha_at(&s, 12, 65), 1.0); - assert!(mask_at(&s, 12, 65) < 0.01, "selected hidden quarter moved"); - assert!(mask_at(&s, 17, 65) > 0.99, "selected visible quarter moved"); - // The vacated area reads as revealed (default), with transparent pixels. - assert_eq!(alpha_at(&s, 12, 15), 0.0); - assert!(mask_at(&s, 12, 15) > 0.99); - // The unselected part of the mask stays, aligned with the unselected pixels. - assert_eq!(alpha_at(&s, 27, 15), 1.0); - assert!(mask_at(&s, 27, 15) < 0.01, "unselected mask stays put"); - assert!(mask_at(&s, 27, 65) > 0.99, "and isn't copied along"); - // Every pixel's mask value is where it was relative to the pixel. - for (x, y) in [(11, 61), (14, 69), (15, 61), (19, 69)] { - let want = if x < 15 { 0.0 } else { 1.0 }; - assert!((mask_at(&s, x, y) - want).abs() < 0.01, "({x},{y})"); - } - } - - #[test] - fn unlinked_mask_stays_put() { - let mut s = masked(&[Rect::new(10, 10, 20, 20)], false); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(50, 60, 70, 70)); - assert!(mask_at(&s, 15, 15) < 0.01); - assert!(mask_at(&s, 55, 65) > 0.99); - // With a selection too. - let mut s = masked(&[Rect::new(10, 10, 20, 20)], false); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 10, "height": 10})).unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 50]})).unwrap(); - assert!(mask_at(&s, 15, 15) < 0.01 && mask_at(&s, 15, 65) > 0.99); - } - - #[test] - fn targeted_unlinked_mask_transforms_alone() { - let mut s = masked(&[Rect::new(10, 10, 20, 20)], false); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50], "target": "mask"})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 20), "pixels untouched"); - assert!(mask_at(&s, 55, 65) < 0.01, "mask moved"); - assert!(mask_at(&s, 15, 15) > 0.99, "vacated area reads as the default"); - s.undo(); - assert!(mask_at(&s, 15, 15) < 0.01, "one undo step"); - // With a selection, only its part of the mask. - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 5, "height": 10})).unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 50], "target": "mask"})).unwrap(); - assert!(mask_at(&s, 12, 65) < 0.01 && mask_at(&s, 12, 15) > 0.99); - assert!(mask_at(&s, 17, 15) < 0.01, "unselected mask stays"); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 20), "pixels untouched"); - } - - #[test] - fn targeted_linked_mask_moves_with_its_layer() { - // Photoshop: a linked mask can't move without its layer. - let mut s = masked(&[Rect::new(10, 10, 20, 20)], true); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50], "target": "mask"})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(50, 60, 70, 70)); - assert!(mask_at(&s, 55, 65) < 0.01); - } - - #[test] - fn targeted_alpha_channel_transforms_alone() { - let mut s = session(); - s.execute("channel.new", json!({})).unwrap(); - s.edit("paint channel", |doc, _| { - doc.channels[0].surface.fill_rect(Rect::new(10, 10, 20, 20), &[1.0]); - Ok(()) - }) - .unwrap(); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 40, 50], "target": {"channel": 0}})).unwrap(); - let st = s.active().unwrap(); - let ch = &st.doc.channels[0].surface; - assert!(ch.sample_channel(55, 65, 0) > 0.99 && ch.sample_channel(15, 15, 0) < 0.01); - assert_eq!(active_bounds(&s), Rect::new(10, 10, 30, 20), "layer untouched"); - } - - /// The Channels panel targets an alpha channel: Edit › Transform › Flip Horizontal and a - /// 50% `edit.transform` (no `target` param) work on the channel, not the layer (BUG-207-4). - #[test] - fn transforms_follow_the_targeted_alpha_channel() { - let mut s = session(); - s.execute("channel.new", json!({})).unwrap(); - s.edit("paint channel", |doc, _| { - let ch = &mut doc.channels[0].surface; - ch.fill_rect(Rect::new(10, 10, 20, 20), &[1.0]); - ch.fill_rect(Rect::new(10, 10, 12, 20), &[0.5]); - Ok(()) - }) - .unwrap(); - s.execute("channel.target", json!({"channel": 0})).unwrap(); - let layer = |s: &Session| crate::active_layer_of(s).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 100, 100)); - let (pixels, past) = (layer(&s), s.active().unwrap().history.past_len()); - s.execute("edit.transform.flipHorizontal", json!({})).unwrap(); - // 8-bit channel: 0.5 is stored as 128/255. - let ch = |s: &Session, x, y| (s.active().unwrap().doc.channels[0].surface.sample_channel(x, y, 0) * 10.0).round() / 10.0; - assert_eq!((ch(&s, 19, 15), ch(&s, 10, 15)), (0.5, 1.0), "the channel flips within its frame"); - assert!(layer(&s) == pixels, "the layer is untouched"); - assert_eq!(s.active().unwrap().history.past_len(), past + 1, "one undo step"); - s.execute("edit.transform", json!({"matrix": [0.5, 0, 0, 0.5, 0, 0], "interpolation": "nearest"})).unwrap(); - assert_eq!((ch(&s, 7, 7), ch(&s, 15, 15)), (1.0, 0.0), "the channel scales to 50%"); - assert!(layer(&s) == pixels, "the layer is untouched"); - s.undo(); - s.undo(); - assert_eq!((ch(&s, 10, 15), ch(&s, 19, 15)), (0.5, 1.0)); - } - - #[test] - fn transform_targets_fail_gracefully() { - let mut s = session(); - let m = json!([1, 0, 0, 1, 5, 5]); - assert!(s.execute("edit.transform", json!({"matrix": m, "target": "mask"})).is_err(), "no mask"); - assert!(s.execute("edit.transform", json!({"matrix": m, "target": {"channel": 9}})).is_err()); - assert!(s.execute("edit.transform", json!({"matrix": m, "target": "quickMask"})).is_err()); - // Unknown target shapes fall back to the pixels. - s.execute("edit.transform", json!({"matrix": m, "target": 7})).unwrap(); - s.execute("edit.transform", json!({"matrix": m, "target": {"channel": "x"}})).unwrap(); - // Selections that miss the mask entirely transform nothing, without panicking. - let mut s = masked(&[Rect::new(10, 10, 20, 20)], false); - s.execute("select.rect", json!({"x": 80, "y": 80, "width": 5, "height": 5})).unwrap(); - let _ = s.execute("edit.transform", json!({"matrix": [1e9, 0, 0, 1e9, 0, 0], "target": "mask"})); - let _ = s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 1e12, 0], "target": "mask"})); - } - - #[test] - fn split_gray_selected_rejects_non_gray() { - let s = Surface::new(PixelFormat::RGBA8); - let mut sel = Surface::new(PixelFormat::GRAY8); - sel.fill_rect(Rect::new(0, 0, 4, 4), &[1.0]); - assert!(matches!(split_gray_selected(&s, &sel), Ok(None))); - } - - /// #2630: type can't be partly transformed, so a selection doesn't narrow the frame: Free - /// Transform (and the frame-based presets) act on the whole type layer. - #[test] - fn a_selection_does_not_narrow_a_type_layers_frame() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 200, "height": 120})).unwrap(); - s.execute("type.create", json!({"x": 20, "y": 60, "text": "Hello", "size": 36})).unwrap(); - let whole = active_bounds(&s); - s.execute("select.rect", json!({"x": whole.x0, "y": whole.y0, "width": whole.width() / 2, "height": whole.height()})).unwrap(); - let st = s.active().unwrap(); - let l = st.doc.layer(st.active_layer.unwrap()).unwrap(); - assert_eq!(transform_bounds(&st.doc, l), whole, "the frame is the whole type layer"); - // Corners of the frame moved 10 px right: a pure translation of the whole text. - let q = [[whole.x0 + 10, whole.y0], [whole.x1 + 10, whole.y0], [whole.x1 + 10, whole.y1], [whole.x0 + 10, whole.y1]]; - s.execute("edit.transform", json!({"quad": q})).unwrap(); - let moved = active_bounds(&s); - assert!(moved.x0.abs_diff(whole.x0 + 10) <= 1 && moved.width().abs_diff(whole.width()) <= 1, "{whole:?} -> {moved:?}"); - // Rotate 180° turns it about its own centre, not the selection's. - s.execute("edit.transform.rotate180", json!({})).unwrap(); - let turned = active_bounds(&s); - assert!(turned.x0.abs_diff(moved.x0) <= 2 && turned.x1.abs_diff(moved.x1) <= 2, "{moved:?} -> {turned:?}"); - // A pixel layer still frames only the selected pixels. - let mut s = session(); - s.execute("select.rect", json!({"x": 10, "y": 10, "width": 5, "height": 10})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(transform_bounds(&st.doc, st.doc.layer(st.active_layer.unwrap()).unwrap()), Rect::new(10, 10, 15, 20)); - } - - /// #5297: the box Free Transform opens with a selection is the selection's own rectangle, - /// not the part of it the layer fills; type still moves whole, and a selection that holds none - /// of the layer leaves nothing to transform. - #[test] - fn the_frame_is_the_selections_own_bounds() { - let mut s = session(); - let frame = |s: &Session| { - let st = s.active().unwrap(); - let l = st.doc.layer(st.active_layer.unwrap()).unwrap(); - (transform_frame(&st.doc, l), transform_bounds(&st.doc, l)) - }; - let content = Rect::new(10, 10, 30, 20); - assert_eq!(frame(&s), (content, content), "no selection: the content"); - s.execute("select.rect", json!({"x": 5, "y": 5, "width": 15, "height": 40})).unwrap(); - assert_eq!(frame(&s), (Rect::new(5, 5, 20, 45), Rect::new(10, 10, 20, 20))); - s.execute("select.rect", json!({"x": 60, "y": 60, "width": 10, "height": 10})).unwrap(); - assert!(frame(&s).0.is_empty(), "nothing of the layer is selected"); - - let mut s = Session::new(); - s.execute("file.new", json!({"width": 200, "height": 120})).unwrap(); - s.execute("type.create", json!({"x": 20, "y": 60, "text": "Hello", "size": 36})).unwrap(); - let whole = active_bounds(&s); - s.execute("select.rect", json!({"x": whole.x0, "y": whole.y0, "width": whole.width() / 2, "height": whole.height()})).unwrap(); - assert_eq!(frame(&s), (whole, whole), "type moves whole"); - } - - #[test] - fn group_transform_ignores_selections_inside_its_children() { - group_transform_with_selection(Rect::new(10, 10, 15, 15)); - } - - #[test] - fn group_transform_ignores_selections_outside_its_children() { - group_transform_with_selection(Rect::new(70, 70, 80, 80)); - } - - #[test] - fn group_transform_presets_rotate_about_the_whole_group() { - for depth in [8, 16, 32] { - for x in [10, 70] { - let mut s = masked_depth(&[Rect::new(10, 10, 15, 20)], true, depth); - let child = s.active().unwrap().active_layer.unwrap(); - s.execute("layer.groupLayers", json!({})).unwrap(); - s.execute("select.rect", json!({"x": x, "y": 10, "width": 5, "height": 5})).unwrap(); - s.execute("edit.transform.rotate180", json!({})).unwrap(); - let layer = s.active().unwrap().doc.layer(child).unwrap(); - assert_eq!(layer.surface().unwrap().content_bounds(), Rect::new(10, 10, 30, 20), "rotate about the group's centre, not the selection's"); - let mask = &layer.mask.as_ref().unwrap().surface; - assert_eq!(mask.sample_channel(27, 15, 0), 0.0); - assert_eq!(mask.sample_channel(12, 15, 0), 1.0); - } - } - } - - fn group_transform_with_selection(selection: Rect) { - for depth in [8, 16, 32] { - for nested in [false, true] { - let mut s = masked_depth(&[Rect::new(10, 10, 15, 20)], true, depth); - let child = s.active().unwrap().active_layer.unwrap(); - s.execute("layer.groupLayers", json!({})).unwrap(); - if nested { - s.execute("layer.groupLayers", json!({})).unwrap(); - } - s.execute("select.rect", json!({"x": selection.x0, "y": selection.y0, "width": selection.width(), "height": selection.height()})).unwrap(); - let steps = s.active().unwrap().history.entries().len(); - let result = s.execute("edit.transform", json!({"quad": [[10, 10], [50, 10], [50, 30], [10, 30]], "interpolation": "nearest"})).unwrap(); - assert_eq!(result["rect"], json!([10.0, 10.0, 30.0, 20.0]), "frame must include the whole group (nested={nested})"); - let st = s.active().unwrap(); - let layer = st.doc.layer(child).unwrap(); - assert_eq!(layer.surface().unwrap().content_bounds(), Rect::new(10, 10, 50, 30)); - let mask = &layer.mask.as_ref().unwrap().surface; - assert_eq!(mask.sample_channel(19, 20, 0), 0.0, "the linked mask scales with its complete layer"); - assert_eq!(mask.sample_channel(21, 20, 0), 1.0); - assert_eq!(st.history.entries().len(), steps + 1); - assert!(s.undo()); - let st = s.active().unwrap(); - let layer = st.doc.layer(child).unwrap(); - assert_eq!(layer.surface().unwrap().content_bounds(), Rect::new(10, 10, 30, 20)); - assert_eq!(layer.mask.as_ref().unwrap().surface.sample_channel(19, 15, 0), 1.0); - assert_eq!(st.doc.selection.as_ref().unwrap().content_bounds(), selection); - assert!(s.redo()); - assert_eq!(s.active().unwrap().doc.layer(child).unwrap().surface().unwrap().content_bounds(), Rect::new(10, 10, 50, 30)); - } - } - } - - #[test] - fn feathered_selection_identity_transform_preserves_pixels() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 16, "height": 16, "background": "transparent"})).unwrap(); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - s.execute("select.rect", json!({"x": 4, "y": 4, "width": 8, "height": 8})).unwrap(); - s.execute("select.modify.feather", json!({"radius": 1})).unwrap(); - - let mut before_px = Vec::new(); - for y in 0..16 { - for x in 0..16 { - before_px.push(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()); - } - } - - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 0], "interpolation": "nearest"})).unwrap(); - - let mut after_px = Vec::new(); - for y in 0..16 { - for x in 0..16 { - after_px.push(s.execute("document.pixel", json!({"x": x, "y": y})).unwrap()); - } - } - - assert_eq!(before_px, after_px, "feathered selection identity transform must preserve all pixels"); - let p4_8 = s.execute("document.pixel", json!({"x": 4, "y": 8})).unwrap(); - assert_eq!(p4_8, json!([1.0, 0.0, 0.0, 1.0]), "(4,8) must remain opaque red"); - - // Undo restores original document. - s.execute("edit.undo", json!({})).unwrap(); - let undo_p4_8 = s.execute("document.pixel", json!({"x": 4, "y": 8})).unwrap(); - assert_eq!(undo_p4_8, json!([1.0, 0.0, 0.0, 1.0])); - - // Locked layer still refuses transform even if identity. - s.execute("layer.setProps", json!({"locks": {"position": true}})).unwrap(); - assert!(s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 0, 0]})).is_err(), "locked layer refuses identity transform"); - } - - /// A 1 x 8 px column on a 256 x 256 canvas (#3773). - fn column_session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 256, "height": 256})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let l = doc.layer_mut(active.unwrap()).unwrap(); - l.surface_mut().unwrap().fill_rect(Rect::new(100, 100, 101, 108), &[0.0, 1.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s - } - - fn opaque_green(s: &Session, x: i32, y: i32) -> bool { - let st = s.active().unwrap(); - let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - (surf.sample_channel(x, y, 1) - 1.0).abs() < 1e-3 && (surf.sample_channel(x, y, 3) - 1.0).abs() < 1e-3 - } - - /// The "pixel stretch" (#3773): scales far past 1000% are taken, and the stretched column stays - /// solid along its whole length. - #[test] - fn a_one_pixel_column_stretches_5000_percent() { - let mut s = column_session(); - s.execute("edit.transform", json!({"quad": [[100, 100], [150, 100], [150, 108], [100, 108]]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(100, 100, 150, 108)); - for x in [100, 101, 125, 149] { - for y in 100..108 { - assert!(opaque_green(&s, x, y), "({x}, {y})"); - } - } - } - - /// The technique as done in Photoshop: select a 1 px column of an image and stretch it across. - #[test] - fn a_selected_column_stretches_solid_over_the_image() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 256, "height": 256})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); - surf.fill_rect(Rect::new(0, 0, 256, 256), &[1.0, 0.0, 0.0, 1.0]); - surf.fill_rect(Rect::new(10, 0, 11, 256), &[0.0, 1.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("select.rect", json!({"x": 10, "y": 0, "width": 1, "height": 256})).unwrap(); - s.execute("edit.transform", json!({"quad": [[10, 0], [250, 0], [250, 256], [10, 256]]})).unwrap(); - for x in [10, 11, 100, 200, 249] { - assert!(opaque_green(&s, x, 128), "x = {x}"); - } - let st = s.active().unwrap(); - let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - assert!(surf.sample_channel(250, 128, 0) > 0.999 && surf.sample_channel(9, 128, 0) > 0.999, "the image either side is untouched"); - } - - /// Free Transform keeps pixels moved off the canvas, as Photoshop does: an 8 x 8 layer moved - /// three canvas widths right and back comes back whole. - #[test] - fn a_layer_moved_far_off_the_canvas_and_back_keeps_its_pixels() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 64, "height": 64})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.edit("paint", |doc, active| { - let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); - for y in 0..8 { - for x in 0..8 { - surf.fill_rect(Rect::new(20 + x, 20 + y, 21 + x, 21 + y), &[x as f32 / 8.0, y as f32 / 8.0, 0.5, 1.0]); - } - } - Ok(()) - }) - .unwrap(); - let pixels = |s: &Session| { - let st = s.active().unwrap(); - let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); - (0..8 * 8 * 4).map(|i| surf.sample_channel(20 + i / 4 % 8, 20 + i / 32, (i % 4) as usize)).collect::>() - }; - let before = pixels(&s); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, 192, 0]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(212, 20, 220, 28)); - s.execute("edit.transform", json!({"matrix": [1, 0, 0, 1, -192, 0]})).unwrap(); - assert_eq!(active_bounds(&s), Rect::new(20, 20, 28, 28)); - assert_eq!(pixels(&s), before); - } - - #[test] - fn relative_params_scale_and_rotate_about_the_frame_centre() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 100, "height": 100})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("select.rect", json!({"x": 20, "y": 30, "width": 40, "height": 20})).unwrap(); - s.execute("edit.fill", json!({"contents": "black"})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); - // 50% about the centre (40, 40), then 10 px right: the content ends at x 40..60, y 35..45. - let r = s.execute("edit.transform", json!({"scaleX": 50, "scaleY": 50, "dx": 10})).unwrap(); - let rect: Vec = serde_json::from_value(r["rect"].clone()).unwrap(); - assert_eq!(rect, [20.0, 30.0, 60.0, 50.0], "the reported frame is the source frame"); - let bounds = |s: &Session| { - let d = s.active().unwrap(); - let b = transform_bounds(&d.doc, d.doc.layer(d.active_layer.unwrap()).unwrap()); - (b.x0, b.y0, b.x1, b.y1) - }; - assert_eq!(bounds(&s), (40, 35, 60, 45)); - // A quarter turn about the centre (50, 40) swaps the frame's sides. - s.execute("edit.transform", json!({"rotate": 90})).unwrap(); - assert_eq!(bounds(&s), (45, 30, 55, 50)); - for bad in [json!({"scaleX": 0}), json!({"rotate": "x"}), json!({"dx": f64::NAN})] { - assert!(s.execute("edit.transform", bad.clone()).is_err(), "{bad}"); - } - } -} diff --git a/crates/engine/src/vector_cmds.rs b/crates/engine/src/vector_cmds.rs deleted file mode 100644 index 14579e5954..0000000000 --- a/crates/engine/src/vector_cmds.rs +++ /dev/null @@ -1,1917 +0,0 @@ -//! Vector commands: shape layers (`shape.*`), document paths (`path.*`, Paths panel), selection ⇄ -//! path, and layer vector masks (`layer.vectorMask.*`). -//! -//! Coordinates are document pixels. Paths in parameters and results use a compact JSON form: -//! -//! ```json -//! {"subpaths":[{"closed":true,"op":"combine|subtract|intersect|exclude", -//! "knots":[[x,y], {"anchor":[x,y],"in":[x,y],"out":[x,y],"smooth":true}, …]}], -//! "fillRule":"nonzero|evenodd", "inverted":false} -//! ``` -//! -//! A knot given as `[x, y]` is a corner with retracted handles; `in`/`out` default to the -//! anchor. The serde form of `photocraft_doc::Path` is accepted too. Colours are `"#rrggbb"`, -//! `"#rrggbbaa"` or `[r,g,b,a]` (0..1); opacities are percentages (0..100). -//! -//! Every edit re-renders the shape layer's cache with `photocraft-vector` in one undoable step. -//! PSD blocks stay byte-identical while they still decode to the model (see `photocraft-io`). - -use photocraft_algo::selection::{self as sel, SelectionMode}; -use photocraft_color::{BlendMode, Color}; -use photocraft_doc::{ - ClippingPath, Document, Fill, FillRule, GradientStyle, Knot, Layer, LayerContent, LayerId, LineCap, LineJoin, LiveShape, NamedPath, Path, PathOp, - ShapeLayer, ShapeStroke, StrokeAlign, Subpath, VectorMask, -}; -use photocraft_geom::{Affine, Point, Rect}; -use photocraft_vector as vector; -use serde_json::{Value, json}; - -use crate::commands::{CommandSpec, blend_from_str}; -use crate::{EngineError, Result, Session}; - -/// Keys PSD uses for a shape layer's vector data; dropped when a shape is rasterized. -const SHAPE_BLOCKS: [&[u8; 4]; 8] = [b"vmsk", b"vsms", b"vogk", b"vstk", b"vscg", b"SoCo", b"GdFl", b"PtFl"]; - -pub(crate) fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} - -pub(crate) fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) -} - -fn has_layer(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - d.active_layer.filter(|id| d.doc.layer(*id).is_some()).map(|_| ()).ok_or_else(|| "no active layer".into()) -} - -fn has_selection(s: &Session) -> std::result::Result<(), String> { - s.active().filter(|d| d.doc.selection.is_some()).map(|_| ()).ok_or_else(|| "no selection".into()) -} - -fn active_shape(s: &Session) -> std::result::Result<&ShapeLayer, String> { - let d = s.active().ok_or("no document open")?; - let id = d.active_layer.ok_or("no active layer")?; - let layer = d.doc.layer(id).ok_or("no active layer")?; - match &layer.content { - LayerContent::Shape(shape) => Ok(shape), - _ => Err("active layer is not a shape layer".into()), - } -} - -fn has_shape_fill(s: &Session) -> std::result::Result<(), String> { - active_shape(s)?.fill.as_ref().map(|_| ()).ok_or_else(|| "shape has no fill".into()) -} - -fn has_shape_stroke(s: &Session) -> std::result::Result<(), String> { - active_shape(s)?.stroke.as_ref().map(|_| ()).ok_or_else(|| "shape has no stroke".into()) -} - -fn can_paste_shape_fill(s: &Session) -> std::result::Result<(), String> { - active_shape(s)?; - s.path_fill_clipboard.as_ref().map(|_| ()).ok_or_else(|| "no shape fill copied".into()) -} - -fn can_paste_shape_stroke(s: &Session) -> std::result::Result<(), String> { - active_shape(s)?; - s.path_stroke_clipboard.as_ref().map(|_| ()).ok_or_else(|| "no shape stroke copied".into()) -} - -fn copy_shape_fill(s: &mut Session, _p: &Value) -> Result { - let fill = active_shape(s).map_err(EngineError::Other)?.fill.clone().ok_or_else(|| EngineError::Other("shape has no fill".into()))?; - s.path_fill_clipboard = Some(fill); - Ok(Value::Null) -} - -fn copy_shape_stroke(s: &mut Session, _p: &Value) -> Result { - let stroke = active_shape(s).map_err(EngineError::Other)?.stroke.clone().ok_or_else(|| EngineError::Other("shape has no stroke".into()))?; - s.path_stroke_clipboard = Some(stroke); - Ok(Value::Null) -} - -fn paste_shape_fill(s: &mut Session, p: &Value) -> Result { - let fill = s.path_fill_clipboard.clone().ok_or_else(|| EngineError::Other("no shape fill copied".into()))?; - let id = layer_id(s, p)?; - with_shape(s, id, "Paste Fill", |shape, _| { - shape.fill = Some(fill); - Ok(()) - })?; - shape_info(s, id) -} - -fn paste_shape_stroke(s: &mut Session, p: &Value) -> Result { - let stroke = s.path_stroke_clipboard.clone().ok_or_else(|| EngineError::Other("no shape stroke copied".into()))?; - let id = layer_id(s, p)?; - with_shape(s, id, "Paste Complete Stroke", |shape, _| { - shape.stroke = Some(stroke); - Ok(()) - })?; - shape_info(s, id) -} - -pub(crate) fn layer_id(s: &Session, p: &Value) -> Result { - match p.get("layer").and_then(Value::as_u64) { - Some(id) => Ok(LayerId(id)), - None => s.active().and_then(|d| d.active_layer).ok_or(EngineError::Other("no active layer".into())), - } -} - -fn f64p(p: &Value, k: &str) -> Option { - p.get(k).and_then(Value::as_f64) -} - -pub(crate) fn pt(v: &Value) -> Option { - match v { - Value::Array(a) if a.len() >= 2 => Some(Point::new(a[0].as_f64()?, a[1].as_f64()?)), - Value::Object(_) => Some(Point::new(v.get("x")?.as_f64()?, v.get("y")?.as_f64()?)), - _ => None, - } -} - -pub(crate) fn nums(p: &Value, k: &str) -> Option<[f64; N]> { - let a = p.get(k)?.as_array()?; - if a.len() < N { - return None; - } - let mut out = [0.0; N]; - for (o, v) in out.iter_mut().zip(a) { - *o = v.as_f64()?; - } - Some(out) -} - -/// `[tl, tr, br, bl]` from an array or a single number. -fn radii(p: &Value) -> Option<[f64; 4]> { - match p.get("radii").or_else(|| p.get("radius"))? { - Value::Number(n) => n.as_f64().map(|r| [r; 4]), - _ => nums::<4>(p, "radii"), - } -} - -pub(crate) fn color(v: &Value) -> Option { - match v { - Value::Array(a) if a.len() >= 3 => { - let c: Vec = a.iter().map(|x| x.as_f64().unwrap_or(0.0) as f32).collect(); - Some(Color::rgba(c[0], c[1], c[2], c.get(3).copied().unwrap_or(1.0))) - } - Value::String(s) => { - let s = s.trim_start_matches('#'); - let h = |i: usize| s.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map(|v| f32::from(v) / 255.0); - match s.len() { - 6 => Some(Color::rgb(h(0)?, h(2)?, h(4)?)), - 8 => Some(Color::rgba(h(0)?, h(2)?, h(4)?, h(6)?)), - _ => None, - } - } - _ => None, - } -} - -fn hex(c: &Color) -> String { - let [r, g, b] = c.to_rgb(); - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - if c.alpha < 1.0 { format!("#{:02x}{:02x}{:02x}{:02x}", q(r), q(g), q(b), q(c.alpha)) } else { format!("#{:02x}{:02x}{:02x}", q(r), q(g), q(b)) } -} - -fn op_from(s: &str) -> Option { - Some(match s.to_ascii_lowercase().as_str() { - "combine" | "add" | "union" => PathOp::Combine, - "subtract" | "minus" => PathOp::Subtract, - "intersect" => PathOp::Intersect, - "exclude" | "xor" => PathOp::Exclude, - "join" => PathOp::Join, - _ => return None, - }) -} - -fn op_name(op: PathOp) -> &'static str { - match op { - PathOp::Combine => "combine", - PathOp::Subtract => "subtract", - PathOp::Intersect => "intersect", - PathOp::Exclude => "exclude", - PathOp::Join => "join", - } -} - -/// Parses a path from the compact JSON form (or the serde form). -pub fn parse_path(v: &Value) -> std::result::Result { - let subs = v.get("subpaths").and_then(Value::as_array).ok_or("path needs `subpaths`")?; - let mut out = Vec::with_capacity(subs.len()); - for (i, s) in subs.iter().enumerate() { - let knots_v = s.get("knots").and_then(Value::as_array).ok_or(format!("subpath {i} needs `knots`"))?; - let mut knots = Vec::with_capacity(knots_v.len()); - for (j, k) in knots_v.iter().enumerate() { - let knot = if k.is_array() { - let a = pt(k).ok_or(format!("knot {i}.{j}: expected [x, y]"))?; - Knot::corner(a.x, a.y) - } else { - let a = k.get("anchor").and_then(pt).ok_or(format!("knot {i}.{j}: missing anchor"))?; - let get = |keys: &[&str]| keys.iter().find_map(|key| k.get(*key).and_then(pt)); - Knot { - anchor: a, - in_ctrl: get(&["in", "inCtrl", "in_ctrl"]).unwrap_or(a), - out_ctrl: get(&["out", "outCtrl", "out_ctrl"]).unwrap_or(a), - smooth: k.get("smooth").and_then(Value::as_bool).unwrap_or(false), - } - }; - knots.push(knot); - } - let op = match s.get("op").and_then(Value::as_str) { - Some(o) => op_from(o).ok_or(format!("subpath {i}: unknown op `{o}`"))?, - None => PathOp::Combine, - }; - out.push(Subpath { closed: s.get("closed").and_then(Value::as_bool).unwrap_or(true), knots, op }); - } - let fill_rule = match v.get("fillRule").or_else(|| v.get("fill_rule")).and_then(Value::as_str).map(str::to_ascii_lowercase).as_deref() { - Some("evenodd" | "even-odd" | "even_odd") => FillRule::EvenOdd, - _ => FillRule::NonZero, - }; - Ok(Path { subpaths: out, fill_rule, inverted: v.get("inverted").and_then(Value::as_bool).unwrap_or(false) }) -} - -/// The compact JSON form of a path. -pub fn path_json(p: &Path) -> Value { - let a = |q: Point| json!([q.x, q.y]); - json!({ - "subpaths": p.subpaths.iter().map(|s| json!({ - "closed": s.closed, - "op": op_name(s.op), - "knots": s.knots.iter().map(|k| json!({ "anchor": a(k.anchor), "in": a(k.in_ctrl), "out": a(k.out_ctrl), "smooth": k.smooth })).collect::>(), - })).collect::>(), - "fillRule": if p.fill_rule == FillRule::EvenOdd { "evenodd" } else { "nonzero" }, - "inverted": p.inverted, - }) -} - -fn fill_json(f: &Fill) -> Value { - match f { - Fill::Solid(c) => json!(hex(c)), - Fill::Gradient { stops, angle, scale, style, reverse, .. } => json!({ "gradient": { - "stops": stops.iter().map(|(t, c)| json!([t, hex(c)])).collect::>(), - "angle": angle, "scale": scale * 100.0, "style": format!("{style:?}").to_ascii_lowercase(), "reverse": reverse } }), - Fill::Pattern { name, scale, .. } => json!({ "pattern": name, "scale": scale * 100.0 }), - } -} - -/// `"#rrggbb"` / `[r,g,b,a]` / `{"gradient":{"stops":[[t,"#hex"],…],"angle","scale","style","reverse"}}` / -/// `{"pattern":name,"scale"}`. `null` → `Ok(None)` (no fill). -fn parse_fill(v: &Value) -> std::result::Result, String> { - if v.is_null() { - return Ok(None); - } - if let Some(c) = color(v) { - return Ok(Some(Fill::Solid(c))); - } - if let Some(g) = v.get("gradient") { - let stops: Vec<(f32, Color)> = g - .get("stops") - .and_then(Value::as_array) - .map(|a| a.iter().filter_map(|s| Some((s.get(0)?.as_f64()? as f32, color(s.get(1)?)?))).collect()) - .unwrap_or_else(|| vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]); - let style = match g.get("style").and_then(Value::as_str).unwrap_or("linear") { - "radial" => GradientStyle::Radial, - "angle" => GradientStyle::Angle, - "reflected" => GradientStyle::Reflected, - "diamond" => GradientStyle::Diamond, - _ => GradientStyle::Linear, - }; - return Ok(Some(Fill::gradient( - stops, - f64p(g, "angle").unwrap_or(90.0) as f32, - f64p(g, "scale").unwrap_or(100.0) as f32 / 100.0, - style, - g.get("reverse").and_then(Value::as_bool).unwrap_or(false), - ))); - } - if let Some(name) = v.get("pattern").and_then(Value::as_str) { - return Ok(Some(Fill::Pattern { - name: name.into(), - scale: f64p(v, "scale").unwrap_or(100.0) as f32 / 100.0, - id: String::new(), - angle: 0.0, - link: true, - phase: (0.0, 0.0), - })); - } - Err(format!("unrecognised fill {v}")) -} - -fn stroke_json(s: &ShapeStroke) -> Value { - json!({ - "width": s.width, "fill": fill_json(&s.paint), "opacity": s.opacity * 100.0, - "align": format!("{:?}", s.align).to_ascii_lowercase(), "cap": format!("{:?}", s.cap).to_ascii_lowercase(), - "join": format!("{:?}", s.join).to_ascii_lowercase(), "miterLimit": s.miter_limit, "dashes": s.dashes, "dashOffset": s.dash_offset, - }) -} - -/// Applies stroke keys onto `base` (`null` → no stroke). -pub fn parse_stroke(v: &Value, base: Option, fg: Color) -> std::result::Result, String> { - if v.is_null() || v.as_bool() == Some(false) { - return Ok(None); - } - if !v.is_object() { - return Err("stroke must be an object or null".into()); - } - for (key, lo, hi) in [("width", 0.0, 1_000_000.0), ("opacity", 0.0, 100.0), ("miterLimit", 1.0, 500.0), ("dashOffset", -1_000_000.0, 1_000_000.0)] { - if let Some(value) = v.get(key) - && !value.as_f64().is_some_and(|n| n.is_finite() && (lo..=hi).contains(&n)) - { - return Err(format!("`{key}` must be a finite number between {lo} and {hi}")); - } - } - for key in ["align", "cap", "join"] { - if v.get(key).is_some_and(|value| !value.is_string()) { - return Err(format!("`{key}` must be a string")); - } - } - let mut s = base.unwrap_or(ShapeStroke { paint: Fill::Solid(fg), ..Default::default() }); - if let Some(w) = f64p(v, "width") { - s.width = w as f32; - } - if let Some(c) = v.get("color").or_else(|| v.get("fill")) { - s.paint = parse_fill(c)?.ok_or("stroke colour cannot be null")?; - } - if let Some(o) = f64p(v, "opacity") { - s.opacity = (o / 100.0) as f32; - } - if let Some(a) = v.get("align").and_then(Value::as_str) { - s.align = match a { - "inside" => StrokeAlign::Inside, - "outside" => StrokeAlign::Outside, - "center" | "centre" => StrokeAlign::Center, - o => return Err(format!("unknown align `{o}`")), - }; - } - if let Some(c) = v.get("cap").and_then(Value::as_str) { - s.cap = match c { - "butt" => LineCap::Butt, - "round" => LineCap::Round, - "square" => LineCap::Square, - o => return Err(format!("unknown cap `{o}`")), - }; - } - if let Some(j) = v.get("join").and_then(Value::as_str) { - s.join = match j { - "miter" => LineJoin::Miter, - "round" => LineJoin::Round, - "bevel" => LineJoin::Bevel, - o => return Err(format!("unknown join `{o}`")), - }; - } - if let Some(m) = f64p(v, "miterLimit") { - s.miter_limit = m as f32; - } - if let Some(d) = v.get("dashes") { - s.dashes = match d { - Value::Null => Vec::new(), - Value::Array(a) if a.len() <= 32 => a - .iter() - .map(|x| { - x.as_f64() - .filter(|n| n.is_finite() && (0.0..=10000.0).contains(n)) - .map(|x| x as f32) - .ok_or_else(|| "dash lengths must be finite numbers between 0 and 10000 widths".to_string()) - }) - .collect::, _>>()?, - _ => return Err("`dashes` must be an array (multiples of the width)".into()), - }; - } - if let Some(o) = f64p(v, "dashOffset") { - s.dash_offset = o as f32; - } - s.validate()?; - Ok(Some(s)) -} - -fn live_json(l: &LiveShape) -> Value { - serde_json::to_value(l).unwrap_or(Value::Null) -} - -/// Builds a live shape from `kind` + geometry params. -fn live_from_params(cmd: &str, kind: &str, p: &Value, prev: Option<&LiveShape>) -> Result { - let prev_rect = match prev { - Some(LiveShape::Rect { rect, .. } | LiveShape::Ellipse { rect } | LiveShape::Polygon { rect, .. }) => Some(*rect), - _ => None, - }; - let rect = || nums::<4>(p, "rect").or(prev_rect).ok_or_else(|| bad(cmd, "`rect`: [x, y, w, h] is required")); - Ok(match kind { - "rect" | "rectangle" => { - let r = match prev { - Some(LiveShape::Rect { radii, .. }) => *radii, - _ => [0.0; 4], - }; - LiveShape::Rect { rect: rect()?, radii: radii(p).unwrap_or(r) } - } - "roundedRect" | "roundRect" => { - let r = match prev { - Some(LiveShape::Rect { radii, .. }) if radii.iter().any(|v| *v > 0.0) => *radii, - _ => [10.0; 4], - }; - LiveShape::Rect { rect: rect()?, radii: radii(p).unwrap_or(r) } - } - "ellipse" => LiveShape::Ellipse { rect: rect()? }, - "polygon" | "star" => { - let (ps, pr) = match prev { - Some(LiveShape::Polygon { sides, star_ratio, .. }) => (*sides, *star_ratio), - _ => (5, if kind == "star" { 0.5 } else { 1.0 }), - }; - let sides = f64p(p, "sides").map_or(ps, |v| v.round().clamp(3.0, 100.0) as u32); - let ratio = f64p(p, "starRatio").map_or(if kind == "star" && pr >= 1.0 { 0.5 } else { pr }, |v| v.clamp(0.01, 1.0)); - LiveShape::Polygon { rect: rect()?, sides, star_ratio: if kind == "polygon" && f64p(p, "starRatio").is_none() { 1.0 } else { ratio } } - } - "line" => { - let (pf, pto, pw) = match prev { - Some(LiveShape::Line { from, to, weight }) => (Some(*from), Some(*to), *weight), - _ => (None, None, 1.0), - }; - LiveShape::Line { - from: nums::<2>(p, "from").or(pf).ok_or_else(|| bad(cmd, "`from`: [x, y] is required"))?, - to: nums::<2>(p, "to").or(pto).ok_or_else(|| bad(cmd, "`to`: [x, y] is required"))?, - weight: f64p(p, "weight").unwrap_or(pw).max(0.0), - } - } - o => return Err(bad(cmd, format!("unknown kind `{o}`"))), - }) -} - -/// The path `shape.create` draws for live-shape params `p` (the Shape tools preview it while dragging). -pub fn shape_path(p: &Value) -> Result { - let kind = p.get("kind").and_then(Value::as_str).unwrap_or("rect"); - Ok(vector::shapes::live_path(&live_from_params("shape.create", kind, p, None)?)) -} - -fn live_kind(l: &LiveShape) -> &'static str { - match l { - LiveShape::Rect { radii, .. } if radii.iter().any(|r| *r > 0.0) => "roundedRect", - LiveShape::Rect { .. } => "rect", - LiveShape::Ellipse { .. } => "ellipse", - LiveShape::Polygon { star_ratio, .. } if *star_ratio < 1.0 => "star", - LiveShape::Polygon { .. } => "polygon", - LiveShape::Line { .. } => "line", - } -} - -/// Re-renders a shape layer's cache for `doc` (canvas-clipped, document pixel format). -pub fn refresh_shape(doc: &Document, sh: &mut ShapeLayer) { - sh.cache = Some(vector::render_shape(sh, doc.pixel_format(), doc.bounds())); -} - -/// A staged stroke edit rendered with the same validation and cache refresh as `shape.edit`. -/// The caller owns this snapshot; the session and its history remain untouched. -pub fn preview_shape_stroke(doc: &Document, id: LayerId, params: &Value) -> Result { - let mut result = doc.clone(); - let layer = result.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let LayerContent::Shape(shape) = &mut layer.content else { - return Err(EngineError::Other("preview target is not a shape layer".into())); - }; - shape.stroke = parse_stroke(params, shape.stroke.clone(), Color::BLACK).map_err(|e| bad("shape.edit", e))?; - vector::flatten::validate_shape(&shape.path, shape.stroke.as_ref()).map_err(|e| bad("shape.edit", e))?; - refresh_shape(doc, shape); - Ok(result) -} - -/// Moves a layer's vector content by `(dx, dy)`: shape paths (re-rendered) and linked vector -/// masks, recursing into groups. For the Move tool / `layer.move`. -pub fn translate_vectors(doc: &Document, l: &mut Layer, dx: f64, dy: f64) { - let a = Affine::translate(dx, dy); - if let Some(vm) = l.vector_mask.as_mut() - && vm.linked - { - vm.path = vm.path.transform(&a); - } - match &mut l.content { - LayerContent::Shape(sh) => { - transform_shape(sh, &a); - refresh_shape(doc, sh); - } - LayerContent::Group(g) => { - for c in &mut g.children { - translate_vectors(doc, c, dx, dy); - } - } - _ => {} - } -} - -/// Applies an affine transform to a shape's path, keeping the live shape when the transform -/// is a translation plus axis-aligned scale (otherwise the shape stops being parametric). -pub fn transform_shape(sh: &mut ShapeLayer, a: &Affine) { - sh.path = sh.path.transform(a); - let [m0, m1, m2, m3, tx, ty] = a.m; - let axis_aligned = m1.abs() < 1e-12 && m2.abs() < 1e-12 && m0 > 0.0 && m3 > 0.0; - sh.live = match (sh.live.take(), axis_aligned) { - (Some(l), true) => { - let r = |r: [f64; 4]| [r[0] * m0 + tx, r[1] * m3 + ty, r[2] * m0, r[3] * m3]; - let pt = |p: [f64; 2]| [p[0] * m0 + tx, p[1] * m3 + ty]; - Some(match l { - LiveShape::Rect { rect, radii } => { - let k = m0.min(m3); - LiveShape::Rect { rect: r(rect), radii: radii.map(|v| v * k) } - } - LiveShape::Ellipse { rect } => LiveShape::Ellipse { rect: r(rect) }, - LiveShape::Polygon { rect, sides, star_ratio } => LiveShape::Polygon { rect: r(rect), sides, star_ratio }, - LiveShape::Line { from, to, weight } => { - // Live-shape edits regenerate the line's filled bar from its parameters. - // Keep the weight in sync with a positive scale of the path, or an - // unchanged endpoint edit unexpectedly restores the old thickness (#3063). - // A line's width scales perpendicular to its direction. - let (dx, dy) = (to[0] - from[0], to[1] - from[1]); - let old_length = dx.hypot(dy); - let new_length = (dx * m0).hypot(dy * m3); - let width_scale = if old_length > 0.0 && new_length > 0.0 { m0 * m3 * old_length / new_length } else { m0.min(m3) }; - LiveShape::Line { from: pt(from), to: pt(to), weight: weight * width_scale } - } - }) - } - // Flips and 90° turns keep rectangles, ellipses and lines live (Image Rotation): the new - // live box is the mapped box. Polygons/stars would change orientation, so they don't. - (Some(l), false) if (m1.abs() < 1e-12 && m2.abs() < 1e-12) || (m0.abs() < 1e-12 && m3.abs() < 1e-12) => { - let pt = |p: [f64; 2]| [m0 * p[0] + m2 * p[1] + tx, m1 * p[0] + m3 * p[1] + ty]; - let r = |r: [f64; 4]| { - let (p, q) = (pt([r[0], r[1]]), pt([r[0] + r[2], r[1] + r[3]])); - [p[0].min(q[0]), p[1].min(q[1]), (q[0] - p[0]).abs(), (q[1] - p[1]).abs()] - }; - let k = (m0.abs() + m1.abs()).min(m2.abs() + m3.abs()); - match l { - LiveShape::Rect { rect, radii } if radii.iter().all(|v| (v - radii[0]).abs() < 1e-9) => { - Some(LiveShape::Rect { rect: r(rect), radii: radii.map(|v| v * k) }) - } - LiveShape::Ellipse { rect } => Some(LiveShape::Ellipse { rect: r(rect) }), - LiveShape::Line { from, to, weight } => Some(LiveShape::Line { from: pt(from), to: pt(to), weight: weight * k }), - _ => None, - } - } - _ => None, - }; - sh.psd_raw = None; -} - -/// Is every anchor and control point of `path` a finite point? -fn finite_path(path: &Path) -> bool { - path.subpaths.iter().flat_map(|s| &s.knots).all(|k| [k.anchor, k.in_ctrl, k.out_ctrl].iter().all(|p| p.x.is_finite() && p.y.is_finite())) -} - -/// `path` through Distort / Perspective (`h`, a projective map of its points: exact for straight -/// segments, the end tangents of curves exact) or, with `warp`, Warp (split where the warp bends -/// it). Refused when the map sends a point to infinity (past a perspective's horizon). -pub(crate) fn map_path(path: &Path, h: Option<&photocraft_algo::transform::Homography>, warp: Option<&photocraft_geom::warp::Warp>) -> Result { - let out = match (h, warp) { - (_, Some(w)) => vector::map::map_path( - path, - |p| { - let (x, y) = w.map(p.x, p.y); - Point::new(x, y) - }, - 0.1, - ), - (Some(h), None) => vector::map::map_points(path, |p| { - let (x, y) = h.apply(p.x, p.y); - Point::new(x, y) - }), - (None, None) => path.clone(), - }; - if !finite_path(&out) { - return Err(EngineError::Other("the distortion sends part of the path out of bounds".into())); - } - Ok(out) -} - -/// Distort, Perspective or Warp of a shape layer (#3009): the shape stays a shape, its path -/// mapped ([`map_path`]), as in Photoshop. A live rectangle, ellipse, polygon or line becomes a -/// plain path, as its parameters can't describe the distorted outline. -pub(crate) fn map_shape(sh: &mut ShapeLayer, h: Option<&photocraft_algo::transform::Homography>, warp: Option<&photocraft_geom::warp::Warp>) -> Result<()> { - sh.path = map_path(&sh.path, h, warp)?; - sh.live = None; - sh.psd_raw = None; - Ok(()) -} - -fn shape_info(s: &Session, id: LayerId) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?; - let LayerContent::Shape(sh) = &l.content else { - return Err(EngineError::Other(format!("layer {} is {} {} layer, not a shape layer", id.0, l.content.article(), l.content.kind_name()))); - }; - let bounds = sh.cache.as_ref().map(|c| c.content_bounds()).filter(|r| !r.is_empty()).map(|r| json!([r.x0, r.y0, r.width(), r.height()])); - Ok(json!({ - "layer": id.0, - "name": l.name, - "path": path_json(&sh.path), - "fill": sh.fill.as_ref().map(fill_json), - "stroke": sh.stroke.as_ref().map(stroke_json), - "live": sh.live.as_ref().map(live_json), - "kind": sh.live.as_ref().map_or("path", live_kind), - "bounds": bounds, - })) -} - -pub(crate) fn with_shape(s: &mut Session, id: LayerId, label: &str, f: impl FnOnce(&mut ShapeLayer, &mut Layer) -> Result) -> Result { - if let Some(st) = s.active() { - let locks = st.doc.effective_locks(id); - if locks.all || locks.pixels { - return Err(EngineError::Other("shape layer is locked".into())); - } - } - s.edit(label, |doc, _| { - let snapshot = doc.clone(); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if !matches!(l.content, LayerContent::Shape(_)) { - return Err(EngineError::Other(format!("layer {} is {} {} layer, not a shape layer", id.0, l.content.article(), l.content.kind_name()))); - } - let LayerContent::Shape(mut sh) = std::mem::replace(&mut l.content, LayerContent::Fill(Fill::Solid(Color::BLACK))) else { - return Err(EngineError::Other(format!("layer {} is not a shape layer", id.0))); - }; - let r = f(&mut sh, l); - if r.is_ok() { - refresh_shape(&snapshot, &mut sh); - } - l.content = LayerContent::Shape(sh); - r - }) -} - -fn default_name(kind: &str) -> &'static str { - match kind { - "rect" | "rectangle" => "Rectangle", - "roundedRect" | "roundRect" => "Rounded Rectangle", - "ellipse" => "Ellipse", - "polygon" => "Polygon", - "star" => "Star", - "line" => "Line", - _ => "Shape", - } -} - -fn shape_create(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "shape.create"; - let kind = p.get("kind").and_then(Value::as_str).unwrap_or("rect"); - let (path, live) = if kind == "path" { - let v = p.get("path").ok_or_else(|| bad(CMD, "kind \"path\" needs `path`"))?; - (parse_path(v).map_err(|e| bad(CMD, e))?, None) - } else { - let l = live_from_params(CMD, kind, p, None)?; - (vector::shapes::live_path(&l), Some(l)) - }; - let fg = s.tools.foreground; - let fgc = Color::rgba(fg[0], fg[1], fg[2], fg[3]); - let fill = match p.get("fill") { - Some(v) => parse_fill(v).map_err(|e| bad(CMD, e))?, - None => Some(Fill::Solid(fgc)), - }; - let stroke = match p.get("stroke") { - Some(v) => parse_stroke(v, None, fgc).map_err(|e| bad(CMD, e))?, - None => None, - }; - vector::flatten::validate_shape(&path, stroke.as_ref()).map_err(|e| bad(CMD, e))?; - // Add to an existing shape layer with a path operation (Shape tool in combine/subtract… mode). - if let Some(target) = p.get("addTo").and_then(Value::as_u64) { - let op = p.get("op").and_then(Value::as_str).map_or(Some(PathOp::Combine), op_from).ok_or_else(|| bad(CMD, "unknown `op`"))?; - let id = LayerId(target); - with_shape(s, id, "Add Shape", |sh, _| { - for mut sp in path.subpaths { - sp.op = op; - sh.path.subpaths.push(sp); - } - sh.live = None; - sh.psd_raw = None; - vector::flatten::validate_shape(&sh.path, sh.stroke.as_ref()).map_err(|e| bad(CMD, e))?; - Ok(()) - })?; - return shape_info(s, id); - } - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - let mut sh = ShapeLayer { path, fill, stroke, live, ..Default::default() }; - let id = s.edit("New Shape Layer", |doc, active| { - refresh_shape(doc, &mut sh); - let name = name.unwrap_or_else(|| doc.next_layer_name(default_name(kind))); - let id = doc.insert_above(*active, Layer::new(name, LayerContent::Shape(sh))); - *active = Some(id); - Ok(id) - })?; - shape_info(s, id) -} - -fn shape_edit(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "shape.edit"; - let id = layer_id(s, p)?; - let fg = s.tools.foreground; - let fgc = Color::rgba(fg[0], fg[1], fg[2], fg[3]); - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - with_shape(s, id, "Edit Shape", |sh, layer| { - if let Some(v) = p.get("path") { - sh.path = parse_path(v).map_err(|e| bad(CMD, e))?; - sh.live = None; - sh.psd_raw = None; - } - let geom_keys = ["kind", "rect", "radii", "radius", "sides", "starRatio", "from", "to", "weight"]; - if geom_keys.iter().any(|k| p.get(*k).is_some()) { - let kind = p.get("kind").and_then(Value::as_str).map(str::to_string).or_else(|| sh.live.as_ref().map(|l| live_kind(l).to_string())); - let kind = kind.ok_or_else(|| bad(CMD, "this shape has no live-shape parameters; pass `kind` or `path`"))?; - // Explicit radii on a plain rectangle keep it a (now rounded) rectangle. - let kind = if kind == "rect" && radii(p).is_some_and(|r| r.iter().any(|v| *v > 0.0)) { "roundedRect".to_string() } else { kind }; - let l = live_from_params(CMD, &kind, p, sh.live.as_ref())?; - // A transformed live shape's path may carry nonuniform corner scaling - // that its scalar radii cannot represent. Re-entering unchanged values - // must not regenerate the outline and silently discard that geometry. - if sh.live.as_ref() != Some(&l) { - sh.path = vector::shapes::live_path(&l); - sh.live = Some(l); - sh.psd_raw = None; - } - } - if let Some(m) = nums::<6>(p, "transform") { - transform_shape(sh, &Affine { m }); - } - if let Some([dx, dy]) = nums::<2>(p, "move") { - transform_shape(sh, &Affine::translate(dx, dy)); - } - if let Some(op) = p.get("op").and_then(Value::as_str) { - // Operation of the selected subpath(s) (`subpath`: index, default all but the first). - let op = op_from(op).ok_or_else(|| bad(CMD, format!("unknown op `{op}`")))?; - match p.get("subpath").and_then(Value::as_u64) { - Some(i) => sh.path.subpaths.get_mut(i as usize).ok_or_else(|| bad(CMD, "no such subpath"))?.op = op, - None => sh.path.subpaths.iter_mut().skip(1).for_each(|sp| sp.op = op), - } - sh.live = None; - } - if let Some(r) = p.get("fillRule").and_then(Value::as_str) { - sh.path.fill_rule = if r.eq_ignore_ascii_case("evenodd") { FillRule::EvenOdd } else { FillRule::NonZero }; - } - if let Some(v) = p.get("fill") { - sh.fill = parse_fill(v).map_err(|e| bad(CMD, e))?; - } - if let Some(v) = p.get("stroke") { - sh.stroke = parse_stroke(v, sh.stroke.clone(), fgc).map_err(|e| bad(CMD, e))?; - } - if let Some(n) = name { - layer.name = n; - } - vector::flatten::validate_shape(&sh.path, sh.stroke.as_ref()).map_err(|e| bad(CMD, e))?; - Ok(()) - })?; - shape_info(s, id) -} - -fn shape_rasterize(s: &mut Session, p: &Value) -> Result { - let id = layer_id(s, p)?; - s.edit("Rasterize Shape", |doc, _| { - let snapshot = doc.clone(); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let LayerContent::Shape(sh) = &mut l.content else { - return Err(EngineError::Other(format!("layer {} is not a shape layer", id.0))); - }; - if sh.cache.is_none() { - refresh_shape(&snapshot, sh); - } - let fmt = snapshot.pixel_format(); - let surface = sh.cache.take().unwrap_or_else(|| photocraft_raster::Surface::new(fmt)); - let surface = if surface.format() == fmt { surface } else { surface.convert(fmt) }; - l.content = LayerContent::Raster(surface); - l.psd_blocks.retain(|(k, _)| !SHAPE_BLOCKS.contains(&k)); - Ok(()) - })?; - Ok(json!({ "layer": id.0 })) -} - -// --------------------------------------------------------------------------- -// Document paths -// --------------------------------------------------------------------------- - -pub(crate) fn is_work(name: Option<&str>) -> bool { - name.is_none_or(|n| n.is_empty() || n.eq_ignore_ascii_case("work") || n == "Work Path") -} - -/// The path a command targets: `"work"`/absent → the work path (falling back to the active -/// layer's shape path or vector mask when there is none), `"layer"` → the active layer's, -/// otherwise a saved path by name. -fn resolve_path(s: &Session, name: Option<&str>) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let layer_path = || -> Option { - let l = d.doc.layer(d.active_layer?)?; - match &l.content { - LayerContent::Shape(sh) => Some(sh.path.clone()), - _ => l.vector_mask.as_ref().map(|v| v.path.clone()), - } - }; - if name == Some("layer") { - return layer_path().ok_or_else(|| EngineError::Other("active layer has no shape path or vector mask".into())); - } - if is_work(name) { - return d.doc.work_path.clone().or_else(layer_path).ok_or_else(|| EngineError::Other("no work path".into())); - } - let n = name.unwrap_or_default(); - d.doc.paths.iter().find(|p| p.name == n).map(|p| p.path.clone()).ok_or_else(|| EngineError::Other(format!("no path named \"{n}\""))) -} - -/// The path an edit targets, except a shape layer's (edit those with [`with_shape`], which -/// re-renders them): `layer`'s vector mask when given, else the work path or a saved path. -pub(crate) fn path_mut<'a>(doc: &'a mut Document, name: &str, layer: Option) -> Result<&'a mut Path> { - if let Some(id) = layer { - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - return l.vector_mask.as_mut().map(|vm| &mut vm.path).ok_or_else(|| EngineError::Other("active layer has no shape path or vector mask".into())); - } - if is_work(Some(name)) { - return doc.work_path.as_mut().ok_or_else(|| EngineError::Other("no work path".into())); - } - doc.paths.iter_mut().find(|q| q.name == name).map(|q| &mut q.path).ok_or_else(|| EngineError::Other(format!("no path named \"{name}\""))) -} - -fn paths_list(s: &Session) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let doc = &d.doc; - let knots = |p: &Path| p.subpaths.iter().map(|s| s.knots.len()).sum::(); - let layer = d.active_layer.and_then(|id| doc.layer(id)).and_then(|l| match &l.content { - LayerContent::Shape(sh) => Some(json!({ "kind": "shape", "layer": l.id.0, "name": format!("{} Shape Path", l.name), "knots": knots(&sh.path) })), - _ => { - l.vector_mask.as_ref().map(|v| json!({ "kind": "vectorMask", "layer": l.id.0, "name": format!("{} Vector Mask", l.name), "knots": knots(&v.path) })) - } - }); - Ok(json!({ - "paths": doc.paths.iter().map(|p| json!({ "name": p.name, "knots": knots(&p.path), "subpaths": p.path.subpaths.len() })).collect::>(), - "workPath": doc.work_path.as_ref().map(|p| json!({ "knots": knots(p), "subpaths": p.subpaths.len() })), - "clippingPath": doc.clipping_path.as_ref().map(|c| json!({ "name": c.name, "flatness": c.flatness })), - "layerPath": layer, - })) -} - -fn path_set(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "path.set"; - let path = parse_path(p.get("path").ok_or_else(|| bad(CMD, "missing `path`"))?).map_err(|e| bad(CMD, e))?; - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - let append = p.get("op").and_then(Value::as_str).map(|o| op_from(o).ok_or_else(|| bad(CMD, format!("unknown op `{o}`")))).transpose()?; - s.edit(if is_work(name.as_deref()) { "Work Path" } else { "Save Path" }, |doc, _| { - let merge = |old: Option<&Path>, new: Path| match (old, append) { - (Some(o), Some(op)) => { - let mut o = o.clone(); - o.subpaths.extend(new.subpaths.into_iter().map(|mut sp| { - sp.op = op; - sp - })); - o - } - _ => new, - }; - if is_work(name.as_deref()) { - doc.work_path = Some(merge(doc.work_path.as_ref(), path)); - } else { - let n = name.clone().unwrap_or_default(); - match doc.paths.iter_mut().find(|q| q.name == n) { - Some(q) => { - q.path = merge(Some(&q.path), path); - } - None => doc.paths.push(NamedPath { name: n, path, psd_raw: None }), - } - } - Ok(()) - })?; - let path = resolve_path(s, name.as_deref())?; - Ok(json!({ "name": name.unwrap_or_else(|| "work".into()), "path": path_json(&path) })) -} - -fn path_delete(s: &mut Session, p: &Value) -> Result { - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - s.edit("Delete Path", |doc, _| { - if is_work(name.as_deref()) { - return doc.work_path.take().map(|_| ()).ok_or_else(|| EngineError::Other("no work path".into())); - } - let n = name.clone().unwrap_or_default(); - let before = doc.paths.len(); - doc.paths.retain(|q| q.name != n); - if doc.paths.len() == before { - return Err(EngineError::Other(format!("no path named \"{n}\""))); - } - if doc.clipping_path.as_ref().is_some_and(|c| c.name == n) { - doc.clipping_path = None; - } - Ok(()) - })?; - Ok(Value::Null) -} - -fn clipping_path_set(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "path.clippingPath.set"; - let name = p.get("name").and_then(Value::as_str).filter(|name| !name.is_empty()).ok_or_else(|| bad(CMD, "missing saved path `name`"))?.to_owned(); - let flatness = match p.get("flatness") { - Some(value) => { - value.as_f64().filter(|value| value.is_finite() && (0.0..=100.0).contains(value)).ok_or_else(|| bad(CMD, "`flatness` must be between 0 and 100"))? - as f32 - } - None => 0.0, - }; - s.edit("Clipping Path", |doc, _| { - if !doc.paths.iter().any(|path| path.name == name) { - return Err(EngineError::Other(format!("no saved path named \"{name}\""))); - } - doc.clipping_path = Some(ClippingPath { name: name.clone(), flatness }); - Ok(()) - })?; - Ok(json!({ "name": name, "flatness": flatness })) -} - -fn clipping_path_clear(s: &mut Session, _p: &Value) -> Result { - s.edit("Clear Clipping Path", |doc, _| { - doc.clipping_path.take().ok_or_else(|| EngineError::Other("no clipping path".into()))?; - Ok(()) - })?; - Ok(Value::Null) -} - -/// The path `path.transform` edits: `layer`'s shape path or vector mask, else the work path or a -/// saved path. -fn transform_target(s: &Session, name: &str, layer: Option) -> Result { - let Some(id) = layer else { return resolve_path(s, Some(name)) }; - let d = s.active().ok_or(EngineError::NoDocument)?; - let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?; - match &l.content { - LayerContent::Shape(sh) => Ok(sh.path.clone()), - _ => l.vector_mask.as_ref().map(|v| v.path.clone()).ok_or_else(|| EngineError::Other("target layer has no shape path or vector mask".into())), - } -} - -/// `n` numbers at `key` (`[x0, y0, x1, y1]` or `[[x, y] × 4]`, flattened): finite, magnitude at -/// most 1000000. `None` when `key` is absent. -fn coords(p: &Value, key: &str, n: usize) -> Result>> { - const CMD: &str = "path.transform"; - let Some(v) = p.get(key) else { return Ok(None) }; - let flat: Vec<&Value> = match v.as_array() { - Some(a) if a.iter().all(Value::is_array) => a.iter().filter_map(Value::as_array).flatten().collect(), - Some(a) => a.iter().collect(), - None => Vec::new(), - }; - let nums: Vec = flat.iter().filter_map(|v| v.as_f64()).filter(|x| x.is_finite() && x.abs() <= 1_000_000.0).collect(); - if flat.len() != n || nums.len() != n { - return Err(bad(CMD, format!("`{key}` must hold {n} finite numbers with magnitude at most 1000000"))); - } - Ok(Some(nums)) -} - -/// How `path.transform` moves the path. -enum PathMap { - Affine(Affine), - /// Distort / Perspective: the box's corners go to a quad. - Projective(photocraft_algo::transform::Homography), - Warp(Box), -} - -fn path_transform(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "path.transform"; - let name = p.get("name").and_then(Value::as_str).unwrap_or("work").to_owned(); - let layer_id = if name == "layer" { Some(layer_id(s, p)?) } else { None }; - // The box Distort, Perspective and Warp frame: `rect`, else the path's control bounds. - let frame = |s: &Session| -> Result<[f64; 4]> { - let r = match coords(p, "rect", 4)? { - Some(r) => [r[0], r[1], r[2], r[3]], - None => { - let (x0, y0, x1, y1) = transform_target(s, &name, layer_id)?.control_bounds().ok_or_else(|| bad(CMD, "path has no points to transform"))?; - [x0, y0, x1, y1] - } - }; - if r[2] - r[0] <= 0.0 || r[3] - r[1] <= 0.0 { - return Err(bad(CMD, "`rect` must have a positive width and height (a straight path needs a `rect`)")); - } - Ok(r) - }; - let map = if let Some(values) = p.get("matrix") { - let values = values.as_array().filter(|values| values.len() == 6).ok_or_else(|| bad(CMD, "`matrix` must contain six numbers"))?; - let mut m = [0.0; 6]; - for (slot, value) in m.iter_mut().zip(values) { - *slot = value - .as_f64() - .filter(|number| number.is_finite() && number.abs() <= 1_000_000.0) - .ok_or_else(|| bad(CMD, "matrix entries must be finite numbers with magnitude at most 1000000"))?; - } - PathMap::Affine(Affine { m }) - } else if let Some(q) = coords(p, "quad", 8)? { - // Distort and Perspective (#3027): where the frame's corners go, clockwise from top-left. - let quad = [[q[0], q[1]], [q[2], q[3]], [q[4], q[5]], [q[6], q[7]]]; - let h = photocraft_algo::transform::Homography::rect_to_quad(frame(s)?, quad) - .filter(|h| h.0.iter().all(|v| v.is_finite()) && h.inverse().is_some()) - .ok_or_else(|| bad(CMD, "degenerate quad"))?; - let [a, b, c, d, e, f, g, hh, i] = h.0; - if g.abs() < 1e-12 && hh.abs() < 1e-12 && i.abs() > 1e-12 { - // A parallelogram is affine: a live shape stays live. - PathMap::Affine(Affine { m: [a / i, d / i, b / i, e / i, c / i, f / i] }) - } else { - PathMap::Projective(h) - } - } else if ["warp", "style", "mesh"].iter().any(|key| p.get(*key).is_some()) { - let w = crate::warp_cmds::warp_from_params(CMD, p, frame(s)?)?; - if [w.bend, w.h_distort, w.v_distort].iter().chain(w.bounds.iter()).any(|v| !v.is_finite() || v.abs() > 1_000_000.0) { - return Err(bad(CMD, "warp values must be finite numbers with magnitude at most 1000000")); - } - PathMap::Warp(Box::new(w)) - } else { - let number = |key: &str, default: f64| -> Result { - match p.get(key) { - Some(value) => value - .as_f64() - .filter(|n| n.is_finite() && n.abs() <= 1_000_000.0) - .ok_or_else(|| bad(CMD, format!("`{key}` must be a finite number with magnitude at most 1000000"))), - None => Ok(default), - } - }; - if !["translateX", "translateY", "scaleX", "scaleY", "angle"].iter().any(|key| p.get(*key).is_some()) { - return Err(bad(CMD, "pass `matrix`, `quad`, a warp (`style`, `mesh` or `warp`) or translate/scale/angle parameters")); - } - let (tx, ty) = (number("translateX", 0.0)?, number("translateY", 0.0)?); - let (sx, sy) = (number("scaleX", 1.0)?, number("scaleY", 1.0)?); - let angle = number("angle", 0.0)?; - // An explicit `layer` parameter determines both the transformed path and its pivot (#3062). - let path = transform_target(s, &name, layer_id)?; - let (x0, y0, x1, y1) = path.control_bounds().ok_or_else(|| bad(CMD, "path has no points to transform"))?; - let (cx, cy) = ((x0 + x1) * 0.5, (y0 + y1) * 0.5); - PathMap::Affine( - Affine::translate(-cx, -cy) - .then(&Affine { m: [sx, 0.0, 0.0, sy, 0.0, 0.0] }) - .then(&Affine::rotate(angle.to_radians())) - .then(&Affine::translate(cx + tx, cy + ty)), - ) - }; - let out = match &map { - PathMap::Affine(a) => json!({ "name": name, "matrix": a.m }), - PathMap::Projective(h) => json!({ "name": name, "homography": h.0 }), - PathMap::Warp(_) => json!({ "name": name, "warp": true }), - }; - if let Some(id) = layer_id - && s.active().and_then(|d| d.doc.layer(id)).is_some_and(|layer| matches!(layer.content, LayerContent::Shape(_))) - { - with_shape(s, id, "Transform Path", |shape, _| { - match &map { - PathMap::Affine(a) => transform_shape(shape, a), - PathMap::Projective(h) => map_shape(shape, Some(h), None)?, - PathMap::Warp(w) => map_shape(shape, None, Some(w))?, - } - shape.psd_raw = None; - Ok(()) - })?; - return Ok(out); - } - s.edit("Transform Path", |doc, _| { - let path = path_mut(doc, &name, layer_id)?; - *path = match &map { - PathMap::Affine(a) => path.transform(a), - PathMap::Projective(h) => map_path(path, Some(h), None)?, - PathMap::Warp(w) => map_path(path, None, Some(w))?, - }; - Ok(()) - })?; - Ok(out) -} - -fn path_rename(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "path.rename"; - let name = p.get("name").and_then(Value::as_str).map(str::to_string); - let to = p.get("to").and_then(Value::as_str).filter(|t| !t.is_empty()).ok_or_else(|| bad(CMD, "missing `to`"))?.to_string(); - if is_work(Some(&to)) { - return Err(bad(CMD, "`to` cannot name the work path")); - } - s.edit(if is_work(name.as_deref()) { "Save Path" } else { "Rename Path" }, |doc, _| { - if doc.paths.iter().any(|q| q.name == to) { - return Err(EngineError::Other(format!("a path named \"{to}\" already exists"))); - } - if is_work(name.as_deref()) { - // Photoshop: saving the work path turns it into a named path. - let path = doc.work_path.take().ok_or_else(|| EngineError::Other("no work path".into()))?; - doc.paths.push(NamedPath { name: to.clone(), path, psd_raw: None }); - return Ok(()); - } - let n = name.clone().unwrap_or_default(); - let q = doc.paths.iter_mut().find(|q| q.name == n).ok_or_else(|| EngineError::Other(format!("no path named \"{n}\"")))?; - q.name = to.clone(); - if let Some(c) = doc.clipping_path.as_mut() - && c.name == n - { - c.name = to.clone(); - } - Ok(()) - })?; - Ok(json!({ "name": to })) -} - -fn path_to_selection(s: &mut Session, p: &Value) -> Result { - let path = resolve_path(s, p.get("name").and_then(Value::as_str))?; - let feather = f64p(p, "feather").unwrap_or(0.0) as f32; - let aa = p.get("antiAlias").and_then(Value::as_bool).unwrap_or(true); - let mode = SelectionMode::parse(p.get("mode").and_then(Value::as_str).unwrap_or("replace")); - let area = s.active().ok_or(EngineError::NoDocument)?.doc.bounds(); - let mut mask = vector::fill_rasterizer(&path, vector::DEFAULT_TOLERANCE).render(area); - if !aa { - for v in &mut mask { - *v = if *v >= 0.5 { 1.0 } else { 0.0 }; - } - } - if feather > 0.0 { - mask = sel::feather(&mask, area.width() as usize, area.height() as usize, feather); - } - let selected = s.edit("Make Selection", |doc, _| { - doc.selection = sel::combine(doc.selection.as_ref(), &mask, area, mode); - Ok(doc.selection.is_some()) - })?; - Ok(json!({ "selected": selected })) -} - -fn select_to_work_path(s: &mut Session, p: &Value) -> Result { - let tol = f64p(p, "tolerance").unwrap_or(2.0).clamp(0.5, 10.0); - let d = s.active().ok_or(EngineError::NoDocument)?; - let area = d.doc.bounds(); - let sel_surface = d.doc.selection.as_ref().ok_or_else(|| EngineError::Other("no selection".into()))?; - let mask = sel::mask_from_surface(Some(sel_surface), area); - let path = vector::trace::trace_mask(&mask, area, 0.0, tol); - let knots: usize = path.subpaths.iter().map(|s| s.knots.len()).sum(); - let subpaths = path.subpaths.len(); - s.edit("Make Work Path", |doc, _| { - doc.work_path = Some(path); - Ok(()) - })?; - Ok(json!({ "subpaths": subpaths, "knots": knots })) -} - -fn path_fill(s: &mut Session, p: &Value) -> Result { - let path = resolve_path(s, p.get("name").and_then(Value::as_str))?; - let fg = s.tools.foreground; - let c = p.get("color").and_then(color).unwrap_or(Color::rgba(fg[0], fg[1], fg[2], fg[3])); - let rgb = c.to_rgb(); - let src = [rgb[0], rgb[1], rgb[2], c.alpha]; - let opacity = (f64p(p, "opacity").unwrap_or(100.0) / 100.0).clamp(0.0, 1.0) as f32; - let mode = p.get("mode").and_then(Value::as_str).and_then(blend_from_str).unwrap_or(BlendMode::Normal); - let feather = f64p(p, "feather").unwrap_or(0.0) as f32; - let aa = p.get("antiAlias").and_then(Value::as_bool).unwrap_or(true); - let id = layer_id(s, p)?; - s.edit("Fill Path", |doc, _| { - let area = doc.bounds(); - let r = vector::fill_rasterizer(&path, vector::DEFAULT_TOLERANCE); - // The feather reaches 3σ = 3 × radius (σ = radius, as in Photoshop), plus a pixel. - let feather_pad = (feather.ceil() as i32).saturating_mul(3).saturating_add(1); - let area = r.pixel_bounds().map_or(area, |b| b.inflate(feather_pad).intersect(&area)); - if area.is_empty() { - return Ok(()); - } - let mut cov = r.render(area); - if !aa { - cov.iter_mut().for_each(|v| *v = if *v >= 0.5 { 1.0 } else { 0.0 }); - } - if feather > 0.0 { - cov = sel::feather(&cov, area.width() as usize, area.height() as usize, feather); - } - // An active selection limits the fill, soft edges included, as every other fill does (#2387). - if let Some(selection) = doc.selection.as_ref() { - let mask = sel::mask_from_surface(Some(selection), area); - cov.iter_mut().zip(&mask).for_each(|(c, m)| *c *= m); - } - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.pixels || locks.all { - return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", l.name))); - } - let lock = locks.transparency; - let surf = l.surface_mut().ok_or_else(|| EngineError::Other("Fill Path needs a pixel layer".into()))?; - paint_coverage(surf, area, &cov, src, opacity, mode, lock); - Ok(()) - })?; - Ok(Value::Null) -} - -/// Composites `src` × coverage onto a surface over `area` (any pixel format). -fn paint_coverage(surf: &mut photocraft_raster::Surface, area: Rect, cov: &[f32], src: [f32; 4], opacity: f32, mode: BlendMode, lock_alpha: bool) { - let fmt = surf.format(); - let ch = fmt.channels(); - let mut vals = surf.read_region(area); - for (px, c) in vals.chunks_exact_mut(ch).zip(cov) { - if *c <= 0.0 { - continue; - } - let dst = photocraft_raster::to_rgba(&fmt, px); - let mut out = photocraft_compose::psblend::composite(mode, dst, [src[0], src[1], src[2], src[3] * c], opacity); - if lock_alpha { - out[3] = dst[3]; - } - photocraft_raster::from_rgba_into(&fmt, out, px); - } - surf.write_region(area, &vals); - surf.prune(); -} - -/// Stroke Path's brush-based tools besides the Brush, Pencil and Eraser it renders itself: each -/// paints the path with its own `paint.*` command (Photoshop's Stroke Path dialog Tool list). -pub const STROKE_PATH_TOOLS: &[(&str, &str)] = &[ - ("backgroundEraser", "paint.backgroundEraser"), - ("cloneStamp", "paint.cloneStamp"), - ("patternStamp", "paint.patternStamp"), - ("healingBrush", "paint.healingBrush"), - ("spotHealingBrush", "paint.spotHealing"), - ("historyBrush", "paint.historyBrush"), - ("blur", "paint.blur"), - ("sharpen", "paint.sharpen"), - ("smudge", "paint.smudge"), - ("dodge", "paint.dodge"), - ("burn", "paint.burn"), - ("sponge", "paint.sponge"), - ("colorReplacement", "paint.colorReplacement"), - ("mixerBrush", "paint.mixerBrush"), -]; - -/// At most this many points per subpath when Simulate Pressure resamples it. -const MAX_PRESSURE_POINTS: f64 = 200_000.0; - -/// The stroke points along one flattened subpath. With `simulate` (Stroke Path's Simulate -/// Pressure) the subpath is resampled about every pixel and the pressure rises from 0 at its start -/// to full at the middle and falls back to 0 at its end, so a brush whose size or opacity follows -/// pen pressure tapers at both ends, as in Photoshop. Otherwise every point has full pressure. -fn stroke_points(pts: &[(f64, f64)], closed: bool, simulate: bool) -> Vec { - use photocraft_paint::StrokePoint; - let mut line = pts.to_vec(); - if closed && let Some(&first) = pts.first() { - line.push(first); - } - let full = |line: Vec<(f64, f64)>| line.into_iter().map(|(x, y)| StrokePoint::new(x, y, 1.0)).collect(); - if !simulate { - return full(line); - } - let seg = |a: &(f64, f64), b: &(f64, f64)| (b.0 - a.0).hypot(b.1 - a.1); - let total: f64 = line.iter().zip(line.iter().skip(1)).map(|(a, b)| seg(a, b)).sum(); - if !(total.is_finite() && total > 0.0) { - return full(line); - } - let step = (total / MAX_PRESSURE_POINTS).max(1.0); - let pressure = |d: f64| (d / total * std::f64::consts::PI).sin().clamp(0.0, 1.0) as f32; - let mut out = Vec::new(); - let mut walked = 0.0; - for (a, b) in line.iter().zip(line.iter().skip(1)) { - let len = seg(a, b); - let n = (len / step).ceil().clamp(1.0, MAX_PRESSURE_POINTS) as usize; - for i in 0..n { - let t = i as f64 / n as f64; - out.push(StrokePoint::new(a.0 + (b.0 - a.0) * t, a.1 + (b.1 - a.1) * t, pressure(walked + len * t))); - } - walked += len; - } - if let Some(&(x, y)) = line.last() { - out.push(StrokePoint::new(x, y, 0.0)); - } - out -} - -/// Stroke Path with a tool that has its own `paint.*` command: the command runs once per subpath, -/// with the caller's params (size, opacity, the tool's options) and the path's points, all in one -/// history step. Only `path.stroke` itself is journalled, so a recorded action replays it once. -fn stroke_with_command(s: &mut Session, p: &Value, cmd: &str, lines: &[vector::Polyline], simulate: bool) -> Result { - let mut params = p.as_object().cloned().unwrap_or_default(); - for k in ["name", "tool", "simulatePressure", "coalesce"] { - params.remove(k); - } - let journal = s.journal.len(); - let r = crate::analysis_cmds::compound(s, "Stroke Path", |s| { - // The union of what each subpath's command changed; None when one of them didn't say. - let mut dmg = Some(Rect::EMPTY); - for pl in lines { - let pts = stroke_points(&pl.pts, pl.closed, simulate); - if pts.is_empty() { - continue; - } - let mut call = params.clone(); - call.insert("points".into(), Value::Array(pts.iter().map(|q| json!([q.x, q.y, q.pressure])).collect())); - s.execute(cmd, Value::Object(call))?; - let r = s.active().and_then(|d| d.last_damage); - dmg = dmg.zip(r).map(|(a, b)| { - if a.is_empty() { - b - } else if b.is_empty() { - a - } else { - a.union(&b) - } - }); - } - Ok(dmg) - }); - s.journal.truncate(journal); - let dmg = r?; - if let Some(st) = s.active_mut() { - st.last_damage = dmg; - } - let dmg = dmg.unwrap_or_else(|| s.active().map_or(Rect::EMPTY, |d| d.doc.bounds())); - Ok(json!({ "damage": [dmg.x0, dmg.y0, dmg.width(), dmg.height()] })) -} - -fn path_stroke(s: &mut Session, p: &Value) -> Result { - let path = resolve_path(s, p.get("name").and_then(Value::as_str))?; - let tool = p.get("tool").and_then(Value::as_str).unwrap_or("brush"); - let simulate = match p.get("simulatePressure") { - None | Some(Value::Null) => false, - Some(v) => v.as_bool().ok_or_else(|| bad("path.stroke", "`simulatePressure` must be true or false"))?, - }; - if let Some(&(_, cmd)) = STROKE_PATH_TOOLS.iter().find(|(t, _)| *t == tool) { - let lines = vector::flatten_path(&path, 0.1); - return stroke_with_command(s, p, cmd, &lines, simulate); - } - let base = s.tools.brush.clone(); - let fg = s.tools.foreground; - let mut brush = photocraft_paint::BrushSettings { - size: f64p(p, "size").map_or(base.size, |v| v as f32), - hardness: f64p(p, "hardness").map_or(base.hardness, |v| v as f32), - opacity: f64p(p, "opacity").map_or(base.opacity, |v| (v / 100.0) as f32), - color: p.get("color").and_then(color).map_or(fg, |c| { - let v = c.to_rgb(); - [v[0], v[1], v[2], c.alpha] - }), - erase: tool == "eraser", - ..base - }; - // The path is the exact geometry: stroke smoothing (a hand-drawing aid) would cut its corners. - brush.smoothing.amount = 0.0; - match tool { - "pencil" => brush.hardness = 1.0, - "brush" | "eraser" => {} - o => { - let tools: Vec<&str> = ["brush", "pencil", "eraser"].into_iter().chain(STROKE_PATH_TOOLS.iter().map(|(t, _)| *t)).collect(); - return Err(bad("path.stroke", format!("unknown tool `{o}` ({})", tools.join("|")))); - } - } - crate::brush_cmds::validate_brush(&brush, "path.stroke")?; - let lines = vector::flatten_path(&path, 0.1); - let id = layer_id(s, p)?; - let bg = s.tools.background; - let dmg = s.edit("Stroke Path", |doc, _| { - let sel = doc.selection.clone(); - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - // Pixel and full locks (the layer's own or a parent group's) refuse the stroke, as Fill Path does (#2388). - if locks.pixels || locks.all { - return Err(EngineError::Other(format!("Could not complete your request because the layer \"{}\" is locked", l.name))); - } - let lock = locks.transparency; - let surf = l.surface_mut().ok_or_else(|| EngineError::Other("Stroke Path needs a pixel layer".into()))?; - let mut brush = brush.clone(); - if brush.erase && lock { - // Locked transparency (e.g. the Background): the Eraser paints the background colour (#76). - brush.erase = false; - brush.color = bg; - } - let mut dmg = Rect::EMPTY; - for pl in &lines { - let pts = stroke_points(&pl.pts, pl.closed, simulate); - if pts.is_empty() { - continue; - } - let r = photocraft_paint::apply_stroke(surf, &photocraft_paint::Stroke { brush: brush.clone(), points: pts }, sel.as_ref(), lock); - dmg = if dmg.is_empty() { r } else { dmg.union(&r) }; - } - Ok(dmg) - })?; - Ok(json!({ "damage": [dmg.x0, dmg.y0, dmg.width(), dmg.height()] })) -} - -// --------------------------------------------------------------------------- -// Vector masks -// --------------------------------------------------------------------------- - -fn vector_mask_info(s: &Session, id: LayerId) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let l = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?; - Ok(match &l.vector_mask { - Some(v) => { - json!({ "layer": id.0, "path": path_json(&v.path), "enabled": v.enabled, "linked": v.linked, "density": v.density * 100.0, "feather": v.feather }) - } - None => json!({ "layer": id.0, "vectorMask": null }), - }) -} - -fn vector_mask_add(s: &mut Session, p: &Value, from_path: bool) -> Result { - const CMD: &str = "layer.vectorMask.add"; - let id = layer_id(s, p)?; - let mut path = match p.get("path") { - Some(v) => parse_path(v).map_err(|e| bad(CMD, e))?, - None if from_path || p.get("name").is_some() => { - let name = p.get("name").and_then(Value::as_str); - // Never fall back to the layer's own path here. - if is_work(name) { - s.active().and_then(|d| d.doc.work_path.clone()).ok_or_else(|| EngineError::Other("no work path".into()))? - } else { - resolve_path(s, name)? - } - } - None => Path::default(), - }; - if p.get("hide").and_then(Value::as_bool).unwrap_or(false) { - path.inverted = !path.inverted; - } - s.edit("Add Vector Mask", |doc, _| { - let locked = doc.effective_locks(id).all; - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if matches!(l.content, LayerContent::Shape(_)) { - return Err(EngineError::Other("a shape layer's path is already its vector mask".into())); - } - if locked { - return Err(EngineError::Other(format!("layer \"{}\" is locked", l.name))); - } - l.vector_mask = Some(VectorMask::new(path)); - Ok(()) - })?; - vector_mask_info(s, id) -} - -fn vector_mask_edit(s: &mut Session, p: &Value) -> Result { - const CMD: &str = "layer.vectorMask.edit"; - let id = layer_id(s, p)?; - let path = p.get("path").map(parse_path).transpose().map_err(|e| bad(CMD, e))?; - // Same range as a pixel mask's feather (`layer.layerMask.edit`), checked before any edit (#4201). - let feather = p - .get("feather") - .map(|v| { - v.as_f64() - .filter(|n| n.is_finite() && (0.0..=1000.0).contains(n)) - .map(|n| n as f32) - .ok_or_else(|| bad(CMD, "`feather` must be a finite number within 0..1000")) - }) - .transpose()?; - s.edit("Edit Vector Mask", |doc, _| { - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let vm = l.vector_mask.as_mut().ok_or_else(|| EngineError::Other("layer has no vector mask".into()))?; - if let Some(pth) = path { - vm.path = pth; - } - if let Some(b) = p.get("enabled").and_then(Value::as_bool) { - vm.enabled = b; - } - if let Some(b) = p.get("linked").and_then(Value::as_bool) { - vm.linked = b; - } - if let Some(d) = f64p(p, "density") { - vm.density = (d / 100.0).clamp(0.0, 1.0) as f32; - } - if let Some(f) = feather { - vm.feather = f; - } - if let Some(b) = p.get("invert").and_then(Value::as_bool) - && b - { - vm.path.inverted = !vm.path.inverted; - } - Ok(()) - })?; - vector_mask_info(s, id) -} - -fn vector_mask_delete(s: &mut Session, p: &Value) -> Result { - let id = layer_id(s, p)?; - s.edit("Delete Vector Mask", |doc, _| { - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - l.vector_mask.take().map(|_| ()).ok_or_else(|| EngineError::Other("layer has no vector mask".into())) - })?; - Ok(Value::Null) -} - -/// Layer › Rasterize › Vector Mask: multiplies the vector mask into the pixel mask. When a feather -/// (either mask's) or the pixel mask's density shapes the rendered edge, the pixel mask instead -/// becomes exactly what the compositor showed, with those settings baked in (#992). -fn vector_mask_rasterize(s: &mut Session, p: &Value) -> Result { - use photocraft_compose::masks::{combined_mask, feather_sigma}; - let id = layer_id(s, p)?; - s.edit("Rasterize Vector Mask", |doc, _| { - let area = doc.bounds(); - let depth = doc.depth; - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - let soft = l.vector_mask.as_ref().is_some_and(|v| v.enabled) - && (l.vector_mask.as_ref().is_some_and(|v| feather_sigma(v.feather) > 0.0) - || l.mask.as_ref().is_some_and(|m| m.enabled && (feather_sigma(m.feather) > 0.0 || m.density < 1.0))); - let shown = if soft { combined_mask(l, area) } else { None }; - let vm = l.vector_mask.take().ok_or_else(|| EngineError::Other("layer has no vector mask".into()))?; - let mut mask = l.mask.take().unwrap_or_else(|| { - let mut m = photocraft_doc::LayerMask::reveal_all(); - m.surface = - photocraft_raster::Surface::with_default(photocraft_color::PixelFormat::new(photocraft_color::ColorMode::Grayscale, depth, false), &[1.0]); - m - }); - if let Some(shown) = shown { - // Past the canvas the pixel mask keeps its own (density-applied) default, as below. - let d = mask.surface.default_pixel().first().copied().unwrap_or(1.0); - let mut surface = photocraft_raster::Surface::with_default(mask.surface.format(), &[1.0 - mask.density * (1.0 - d)]); - surface.write_region(area, &shown.read_region(area)); - surface.prune(); - (mask.surface, mask.feather, mask.density) = (surface, 0.0, 1.0); - l.mask = Some(mask); - return Ok(()); - } - let vals = vector::vector_mask_values(&vm, area); - // Outside the canvas the vector mask is 0 unless it is empty/inverted; keep the old default there. - let old = mask.surface.read_region(area); - let merged: Vec = old.iter().zip(&vals).map(|(a, b)| a * b).collect(); - mask.surface.write_region(area, &merged); - mask.surface.prune(); - l.mask = Some(mask); - Ok(()) - })?; - Ok(json!({ "layer": id.0 })) -} - -/// `p` with `key` set (for alias commands). -fn with(p: &Value, key: &str, v: Value) -> Value { - let mut o = if p.is_object() { p.clone() } else { json!({}) }; - o[key] = v; - o -} - -fn toggle_vector_mask(s: &mut Session, p: &Value, key: &str) -> Result { - let id = layer_id(s, p)?; - let cur = { - let d = s.active().ok_or(EngineError::NoDocument)?; - let vm = d.doc.layer(id).ok_or(EngineError::NoLayer(id))?.vector_mask.as_ref().ok_or_else(|| EngineError::Other("layer has no vector mask".into()))?; - if key == "enabled" { vm.enabled } else { vm.linked } - }; - let v = p.get(key).and_then(Value::as_bool).unwrap_or(!cur); - vector_mask_edit(s, &json!({ "layer": id.0, key: v })) -} - -/// Traces the exact coverage of the shape's path back into plain outlines. -fn merge_components(s: &mut Session, p: &Value) -> Result { - let tol = f64p(p, "tolerance").unwrap_or(0.1).clamp(0.05, 10.0); - if let Some(ids) = combine_targets(s, p, &["layer"])? { - return merge_shape_layers(s, &ids, None, "Merge Shape Components", Some(tol)); - } - let id = layer_id(s, p)?; - with_shape(s, id, "Merge Shape Components", |sh, _| bake_components(sh, tol))?; - shape_info(s, id) -} - -/// Bakes `sh`'s path operations into plain combined outlines, traced from the exact coverage. -fn bake_components(sh: &mut ShapeLayer, tol: f64) -> Result<()> { - // Trace at 4× resolution so the fitted outline follows the anti-aliased edge closely. - const K: f64 = 4.0; - let up = sh.path.transform(&Affine::scale(K)); - let r = vector::fill_rasterizer(&up, vector::DEFAULT_TOLERANCE); - let Some(b) = r.pixel_bounds() else { - sh.path.subpaths.clear(); - return Ok(()); - }; - let b = b.inflate(2); - if u64::from(b.width()) * u64::from(b.height()) > 400_000_000 { - return Err(EngineError::Other("shape too large to merge".into())); - } - let cov = r.render(b); - let traced = vector::trace::trace_mask(&cov, b, 0.0, tol * K); - sh.path = traced.transform(&Affine::scale(1.0 / K)); - sh.live = None; - sh.psd_raw = None; - Ok(()) -} - -/// The shape layers Layer › Combine Shapes merges into one: with none of the `narrow` params -/// given and two or more shape layers selected, those layers bottom-to-top (other selected layers -/// are left alone). `None` keeps the single-layer behaviour (acting on one layer's components). -fn combine_targets(s: &Session, p: &Value, narrow: &[&str]) -> Result>> { - if narrow.iter().any(|k| p.get(*k).is_some()) { - return Ok(None); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let sel = d.selected_layers(); - let ids = crate::layer_multi_cmds::top_level(&d.doc, &sel); - let ids: Vec = ids.into_iter().filter(|id| d.doc.layer(*id).is_some_and(|l| matches!(l.content, LayerContent::Shape(_)))).collect(); - Ok((ids.len() > 1).then_some(ids)) -} - -/// Layer › Combine Shapes with several shape layers selected: Photoshop merges them into the -/// top-most one. The bottom layer's components keep their operations; every component of the -/// layers above takes `op` (front shapes subtract, intersect… in stacking order), or, with no -/// `op` (Merge Shape Components), keeps its own with each layer's first component combined, and -/// the result is then baked with tolerance `bake`. The other layers are removed; one history step. -fn merge_shape_layers(s: &mut Session, ids: &[LayerId], op: Option, label: &str, bake: Option) -> Result { - let (&keep, _) = ids.split_last().ok_or_else(|| EngineError::Other("Combine Shapes needs two or more shape layers".into()))?; - if let Some(st) = s.active() { - for id in ids { - let locks = st.doc.effective_locks(*id); - if locks.all || locks.pixels { - return Err(EngineError::Other("a selected shape layer is locked".into())); - } - } - } - s.edit(label, |doc, active| { - let mut path: Option = None; - for id in ids { - let l = doc.layer(*id).ok_or(EngineError::NoLayer(*id))?; - let LayerContent::Shape(sh) = &l.content else { - return Err(EngineError::Other(format!("layer {} is not a shape layer", id.0))); - }; - let Some(acc) = path.as_mut() else { - path = Some(sh.path.clone()); - continue; - }; - for (j, sp) in sh.path.subpaths.iter().enumerate() { - let mut sp = sp.clone(); - sp.op = match op { - Some(op) => op, - None if j == 0 => PathOp::Combine, - None => sp.op, - }; - acc.subpaths.push(sp); - } - } - let path = path.ok_or_else(|| EngineError::Other("Combine Shapes needs two or more shape layers".into()))?; - let snapshot = doc.clone(); - let l = doc.layer_mut(keep).ok_or(EngineError::NoLayer(keep))?; - let LayerContent::Shape(sh) = &mut l.content else { - return Err(EngineError::Other(format!("layer {} is not a shape layer", keep.0))); - }; - let mut merged = ShapeLayer { path, live: None, psd_raw: None, cache: None, ..sh.clone() }; - if let Some(tol) = bake { - bake_components(&mut merged, tol)?; - } - vector::flatten::validate_shape(&merged.path, merged.stroke.as_ref()).map_err(|e| bad(label, e))?; - refresh_shape(&snapshot, &mut merged); - *sh = merged; - for id in ids.iter().filter(|id| **id != keep) { - doc.remove(*id); - } - *active = Some(keep); - Ok(()) - })?; - crate::layer_multi_cmds::reselect(s, vec![keep], Some(keep)); - shape_info(s, keep) -} - -/// Layer › Combine Shapes › Unite / Subtract Front Shape / Intersect / Exclude: merges the -/// selected shape layers with `op` ([`merge_shape_layers`]), or with one layer (or an explicit -/// `layer`/`subpath`) sets the operation of that layer's components (`shape.edit`'s `op`). -fn combine_shapes(s: &mut Session, p: &Value, op: &str, label: &str) -> Result { - if let Some(ids) = combine_targets(s, p, &["layer", "subpath"])? { - let op = op_from(op).ok_or_else(|| EngineError::Other(format!("unknown op `{op}`")))?; - return merge_shape_layers(s, &ids, Some(op), label, None); - } - shape_edit(s, &with(p, "op", json!(op))) -} - -fn selection_to_shape(s: &mut Session, p: &Value) -> Result { - let tol = f64p(p, "tolerance").or_else(|| f64p(p, "simplifyTolerance")).unwrap_or(2.0).clamp(0.5, 10.0); - let d = s.active().ok_or(EngineError::NoDocument)?; - let area = d.doc.bounds(); - let mask = sel::mask_from_surface(d.doc.selection.as_ref(), area); - let path = vector::trace::trace_mask(&mask, area, 0.0, tol); - let mut q = json!({ "kind": "path", "path": path_json(&path) }); - for k in ["fill", "name", "stroke"] { - if let Some(v) = p.get(k) { - q[k] = v.clone(); - } - } - shape_create(s, &q) -} - -// --------------------------------------------------------------------------- -// Specs -// --------------------------------------------------------------------------- - -/// Params of the Layer › Combine Shapes operations. -const COMBINE_PARAMS: &str = r##"{"layer":id?,"subpath":index? (default all but the first)} (no layer/subpath and two or more shape layers selected: merges them into the top-most, whose components above the bottom layer's take the operation)"##; - -pub(crate) const PATH_FORM: &str = r##"path: {"subpaths":[{"closed":bool=true,"op":"combine|subtract|intersect|exclude","knots":[[x,y] | {"anchor":[x,y],"in":[x,y],"out":[x,y],"smooth":bool}]}],"fillRule":"nonzero|evenodd","inverted":bool}"##; - -macro_rules! spec { - ($id:literal, $label:literal, [$($m:literal),*], $params:expr, $en:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: &[$($m),*], shortcut: None, params: $params, enabled: $en, run: $run, journal: true } - }; -} - -pub(crate) fn leak(s: String) -> &'static str { - Box::leak(s.into_boxed_str()) -} - -pub fn specs() -> Vec { - let stroke = r##""stroke":{"width":px,"color":"#rrggbb"|fill,"opacity":0..100,"align":"inside|center|outside","cap":"butt|round|square","join":"miter|round|bevel","miterLimit":n,"dashes":[multiples of width],"dashOffset":n}|null"##; - let fill = r##""fill":"#rrggbb"|[r,g,b,a]|{"gradient":{"stops":[[t,"#hex"]],"angle":deg,"scale":%,"style":"linear|radial|angle|reflected|diamond","reverse":bool}}|{"pattern":name}|null (no fill)"##; - vec![ - spec!( - "shape.create", - "New Shape Layer", - ["Layer", "New"], - leak(format!( - r##"{{"kind":"rect|roundedRect|ellipse|polygon|star|line|path"="rect","rect":[x,y,w,h] (rect/roundedRect/ellipse/polygon/star),"radii":[tl,tr,br,bl]|r (roundedRect=10),"sides":3..100=5,"starRatio":0..1 (star=0.5),"from":[x,y],"to":[x,y],"weight":px=1 (line),"path":{{…}} (kind path),{fill}=foreground,{stroke}=none,"name":str?,"addTo":layerId? + "op":"combine|subtract|intersect|exclude" (append to an existing shape layer)}} → shape.info. {PATH_FORM}"## - )), - has_doc, - shape_create - ), - spec!( - "shape.edit", - "Edit Shape", - [], - leak(format!( - r##"{{"layer":id?,"path":{{…}}? (replaces; drops live shape),"kind":str?,"rect":[x,y,w,h]?,"radii":[tl,tr,br,bl]|r?,"sides":n?,"starRatio":r?,"from":[x,y]?,"to":[x,y]?,"weight":px? (live-shape params regenerate the path),"move":[dx,dy]?,"transform":[a,b,c,d,e,f]?,"op":"combine|…"? + "subpath":index? (default all but the first),"fillRule":"nonzero|evenodd"?,{fill}?,{stroke}? (merged into the current stroke),"name":str?}} → shape.info"## - )), - has_layer, - shape_edit - ), - CommandSpec { - id: "shape.info", - label: "Shape Layer Info", - menu: &[], - shortcut: None, - params: r##"{"layer":id?} → {layer,name,kind,path,fill,stroke,live,bounds:[x,y,w,h]}"##, - enabled: has_layer, - run: |s, p| { - let id = layer_id(s, p)?; - shape_info(s, id) - }, - journal: false, - }, - spec!("shape.rasterize", "Rasterize Shape", ["Layer", "Rasterize"], r##"{"layer":id?}"##, has_layer, shape_rasterize), - CommandSpec { - id: "path.list", - label: "List Paths", - menu: &[], - shortcut: None, - params: r##"{} → {paths:[{name,knots,subpaths}],workPath,clippingPath,layerPath (active layer's shape path / vector mask)}"##, - enabled: has_doc, - run: |s, _| paths_list(s), - journal: false, - }, - CommandSpec { - id: "path.info", - label: "Path Info", - menu: &[], - shortcut: None, - params: r##"{"name":str|"work"|"layer"="work"} → {name,path}"##, - enabled: has_doc, - run: |s, p| { - let name = p.get("name").and_then(Value::as_str); - let path = resolve_path(s, name)?; - Ok(json!({ "name": name.unwrap_or("work"), "path": path_json(&path) })) - }, - journal: false, - }, - spec!( - "path.set", - "Set Path", - [], - leak(format!( - r##"{{"name":str|"work"="work","path":{{…}},"op":"combine|subtract|intersect|exclude"? (append to the existing path with this op instead of replacing)}}. {PATH_FORM}"## - )), - has_doc, - path_set - ), - spec!("path.delete", "Delete Path", [], r##"{"name":str|"work"="work"}"##, has_doc, path_delete), - spec!( - "path.transform", - "Free Transform Path", - [], - r##"{"name":str|"work"|"layer"="work","layer":id? (with layer target),"matrix":[a,b,c,d,e,f] | "quad":[[x,y]×4] (Distort/Perspective: where the frame's corners go, clockwise from top-left) | "style"|"mesh"|"warp":… (Warp, as edit.transform.warp) | "translateX":px?,"translateY":px?,"scaleX":factor?,"scaleY":factor?,"angle":degrees?,"rect":[x0,y0,x1,y1]? (the quad's or warp's frame; default = path control bounds)} (numeric transforms pivot on path bounds center)"##, - has_doc, - path_transform - ), - spec!( - "path.clippingPath.set", - "Clipping Path", - [], - r##"{"name":savedPathName,"flatness":0..100=0} (PSD export clipping path)"##, - has_doc, - clipping_path_set - ), - spec!("path.clippingPath.clear", "Clear Clipping Path", [], r##"{}"##, has_doc, clipping_path_clear), - spec!("path.style.copyFill", "Copy Fill", [], r##"{} (copies active shape layer fill)"##, has_shape_fill, copy_shape_fill), - spec!("path.style.copyStroke", "Copy Complete Stroke", [], r##"{} (copies active shape layer stroke)"##, has_shape_stroke, copy_shape_stroke), - spec!( - "path.style.pasteFill", - "Paste Fill", - [], - r##"{"layer":id?} (pastes copied fill onto active shape layer)"##, - can_paste_shape_fill, - paste_shape_fill - ), - spec!( - "path.style.pasteStroke", - "Paste Complete Stroke", - [], - r##"{"layer":id?} (pastes copied stroke onto active shape layer)"##, - can_paste_shape_stroke, - paste_shape_stroke - ), - spec!("path.rename", "Rename Path", [], r##"{"name":str|"work"="work","to":str} (renaming the work path saves it)"##, has_doc, path_rename), - // ⌘↩ / Ctrl+Enter, as in Photoshop (the UI loads the path selected in the Paths panel, #306). - CommandSpec { - id: "path.toSelection", - label: "Make Selection from Path", - menu: &[], - shortcut: Some("Cmd+Enter"), - params: r##"{"name":str|"work"|"layer"="work","feather":px=0,"antiAlias":bool=true,"mode":"replace|add|subtract|intersect"="replace"}"##, - enabled: has_doc, - run: path_to_selection, - journal: true, - }, - spec!("select.toWorkPath", "Make Work Path", [], r##"{"tolerance":0.5..10 px=2} → {subpaths,knots}"##, has_selection, select_to_work_path), - spec!( - "path.fill", - "Fill Path", - [], - r##"{"name":str|"work"|"layer"="work","layer":id? (pixel layer, default active),"color":"#rrggbb"=foreground,"opacity":0..100=100,"mode":"normal|multiply|…"="normal","feather":px=0,"antiAlias":bool=true}"##, - has_layer, - path_fill - ), - spec!( - "path.stroke", - "Stroke Path", - [], - r##"{"name":str|"work"|"layer"="work","layer":id? (pixel layer),"tool":"brush|pencil|eraser|backgroundEraser|cloneStamp|patternStamp|healingBrush|spotHealingBrush|historyBrush|blur|sharpen|smudge|dodge|burn|sponge|colorReplacement|mixerBrush"="brush","simulatePressure":bool=false (taper both ends when the brush follows pen pressure),"size":0.5..5000 px?,"hardness":0..1?,"opacity":0..100?,"color":"#rrggbb"=foreground} (current brush settings otherwise; tools past the Eraser run their paint.* command along the path and take its params)"##, - has_layer, - path_stroke - ), - spec!( - "layer.vectorMask.add", - "Add Vector Mask", - ["Layer", "Vector Mask"], - r##"{"layer":id?,"path":{…}? | "name":str|"work"? (copy a document path),"hide":bool=false (Hide All = inverted)} → vector mask info. Without a path the mask reveals all."##, - has_layer, - |s, p| vector_mask_add(s, p, false) - ), - spec!( - "layer.vectorMask.fromPath", - "Vector Mask from Current Path", - ["Layer", "Vector Mask"], - r##"{"layer":id?,"name":str|"work"="work"}"##, - has_layer, - |s, p| vector_mask_add(s, p, true) - ), - spec!( - "layer.vectorMask.edit", - "Edit Vector Mask", - [], - leak(format!( - r##"{{"layer":id?,"path":{{…}}?,"enabled":bool?,"linked":bool?,"density":0..100?,"feather":0..1000? (pixels),"invert":true?}}. {PATH_FORM}"## - )), - has_layer, - vector_mask_edit - ), - spec!("layer.vectorMask.delete", "Delete Vector Mask", ["Layer", "Vector Mask"], r##"{"layer":id?}"##, has_layer, vector_mask_delete), - spec!( - "layer.rasterize.vectorMask", - "Rasterize Vector Mask", - [], - r##"{"layer":id?} (multiplied into the layer mask)"##, - has_layer, - vector_mask_rasterize - ), - // Menu ids of Photoshop's Layer › Vector Mask / Rasterize / Combine Shapes. - spec!("layer.vectorMask.revealAll", "Vector Mask: Reveal All", [], r##"{"layer":id?}"##, has_layer, |s, p| vector_mask_add( - s, - &with(p, "hide", json!(false)), - false - )), - spec!("layer.vectorMask.hideAll", "Vector Mask: Hide All", [], r##"{"layer":id?}"##, has_layer, |s, p| vector_mask_add( - s, - &with(p, "hide", json!(true)), - false - )), - spec!("layer.vectorMask.currentPath", "Vector Mask: Current Path", [], r##"{"layer":id?,"name":str|"work"="work"}"##, has_layer, |s, p| { - vector_mask_add(s, p, true) - }), - spec!("layer.vectorMask.enabled", "Enable Vector Mask", [], r##"{"layer":id?,"enabled":bool? (default: toggle)}"##, has_layer, |s, p| { - toggle_vector_mask(s, p, "enabled") - }), - spec!("layer.vectorMask.linked", "Link Vector Mask", [], r##"{"layer":id?,"linked":bool? (default: toggle)}"##, has_layer, |s, p| toggle_vector_mask( - s, p, "linked" - )), - // Without `layer`, every selected shape layer (the shared Rasterize logic calls back here with one). - spec!("layer.rasterize.shape", "Rasterize Shape", [], r##"{"layer":id?} (no layer: every selected shape layer)"##, has_layer, |s, p| { - if p.get("layer").is_some() { shape_rasterize(s, p) } else { crate::extra_cmds::rasterize(s, p, Some("shape")) } - }), - // With two or more shape layers selected (and no `layer`/`subpath`), these merge them into - // the top-most one, as in Photoshop; with one, they set the operation of its components. - spec!("layer.combineShapes.unite", "Unite Shapes", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes(s, p, "combine", "Unite Shapes")), - spec!("layer.combineShapes.subtractFrontShape", "Subtract Front Shape", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes( - s, - p, - "subtract", - "Subtract Front Shape" - )), - spec!("layer.combineShapes.intersectShapeAreas", "Intersect Shape Areas", [], COMBINE_PARAMS, has_layer, |s, p| combine_shapes( - s, - p, - "intersect", - "Intersect Shape Areas" - )), - spec!("layer.combineShapes.excludeOverlappingShapes", "Exclude Overlapping Shapes", [], COMBINE_PARAMS, has_layer, |s, p| { - combine_shapes(s, p, "exclude", "Exclude Overlapping Shapes") - }), - spec!( - "layer.combineShapes.mergeShapeComponents", - "Merge Shape Components", - [], - r##"{"layer":id?,"tolerance":px=0.1} (bakes the path operations into plain combined outlines, traced from the exact coverage; no layer and several shape layers selected: merges them into the top-most first)"##, - has_layer, - merge_components - ), - spec!("select.convertToShape", "Convert to Shape", [], r##"{"tolerance":0.5..10 px=2} → shape.info"##, has_selection, selection_to_shape), - CommandSpec { - id: "layer.vectorMask.info", - label: "Vector Mask Info", - menu: &[], - shortcut: None, - params: r##"{"layer":id?} → {layer,path,enabled,linked,density,feather}"##, - enabled: has_layer, - run: |s, p| { - let id = layer_id(s, p)?; - vector_mask_info(s, id) - }, - journal: false, - }, - ] -} - -#[cfg(test)] -mod tests; diff --git a/crates/engine/src/vp_cmds.rs b/crates/engine/src/vp_cmds.rs deleted file mode 100644 index 185930298a..0000000000 --- a/crates/engine/src/vp_cmds.rs +++ /dev/null @@ -1,285 +0,0 @@ -//! Filter › Vanishing Point: perspective planes (drawn, or torn off an edge of another plane), -//! pasting an image onto a plane and the perspective clone stamp, as one replayable command -//! ([`photocraft_algo::vanishing`]). The edit lands on the active pixel layer, or on a new layer -//! with `"newLayer": true` (Photoshop's advice for non-destructive Vanishing Point work). - -use photocraft_algo::vanishing::{Edge, PlaneCheck, Scene, VpPlane, clone_stroke, paste}; -use photocraft_doc::{Document, Layer, LayerContent, LayerId}; -use photocraft_geom::Rect; -use photocraft_raster::Surface; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::{EngineError, Result, Session}; - -pub const VP: &str = "filter.vanishingPoint"; - -fn bad(msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: VP.into(), msg: msg.into() } -} - -fn pt(v: &Value) -> Option<[f64; 2]> { - Some([v.get(0)?.as_f64()?, v.get(1)?.as_f64()?]) -} - -/// Builds the scene from `"planes"`. -pub fn scene(p: &Value, canvas: Rect) -> Result { - let planes = p - .get("planes") - .and_then(Value::as_array) - .filter(|a| !a.is_empty()) - .ok_or_else(|| bad("pass `planes`: [{\"corners\":[[x,y]×4]}, {\"from\":0,\"edge\":\"top\",\"angle\":90}]"))?; - let center = [(canvas.x0 + canvas.x1) as f64 / 2.0, (canvas.y0 + canvas.y1) as f64 / 2.0]; - let corners = |v: &Value| -> Option<[[f64; 2]; 4]> { - let a = v.get("corners")?.as_array()?; - Some([pt(a.first()?)?, pt(a.get(1)?)?, pt(a.get(2)?)?, pt(a.get(3)?)?]) - }; - // A drawn quad must be convex (Photoshop's red plane): a bow-tie or dented quad has no - // perspective to paste or clone along. - let drawn = |i: usize, c: [[f64; 2]; 4]| -> Result { - let pl = VpPlane { corners: c }; - if pl.check(center) == PlaneCheck::Invalid { - return Err(bad(format!("plane {i}: the four corners must form a convex quad"))); - } - Ok(pl) - }; - let first = planes.first().and_then(corners).ok_or_else(|| bad("the first plane needs four `corners`"))?; - let first = drawn(0, first)?; - let focal = p.get("focalLength").and_then(Value::as_f64).filter(|f| *f > 0.0); - let mut sc = Scene::new(first, center, focal, canvas.width().max(canvas.height()) as f64); - for (i, pl) in planes.iter().enumerate().skip(1) { - if let Some(c) = corners(pl) { - sc.planes.push(drawn(i, c)?); - continue; - } - let from = pl - .get("from") - .and_then(Value::as_u64) - .map(|v| v as usize) - .filter(|f| *f < sc.planes.len()) - .ok_or_else(|| bad(format!("plane {i}: `corners` or `from` (an earlier plane)")))?; - let edge = match pl.get("edge").and_then(Value::as_str).unwrap_or("top") { - "top" => Edge::Top, - "right" => Edge::Right, - "bottom" => Edge::Bottom, - "left" => Edge::Left, - e => return Err(bad(format!("plane {i}: unknown edge `{e}`"))), - }; - let angle = pl.get("angle").and_then(Value::as_f64).unwrap_or(90.0); - let depth = pl.get("depth").and_then(Value::as_f64).unwrap_or(1.0); - sc.tear_off(from, edge, angle, depth).ok_or_else(|| bad(format!("plane {i} would lie behind the camera")))?; - } - if sc.planes.iter().any(|p| p.homography().is_none()) { - return Err(bad("a plane is degenerate")); - } - Ok(sc) -} - -fn has_layer(s: &Session) -> std::result::Result<(), String> { - let d = s.active().ok_or("no document open")?; - match d.active_layer.and_then(|id| d.doc.layer(id)).map(|l| &l.content) { - Some(LayerContent::Raster(_)) => Ok(()), - Some(other) => Err(format!("Vanishing Point needs a pixel layer (active layer is {} {} layer)", other.article(), other.kind_name())), - None => Err("no active layer".into()), - } -} - -/// Composites `top` over `dst` (premultiplied source-over), same format. -fn over(dst: &mut Surface, top: &Surface) { - let b = top.content_bounds(); - if b.is_empty() { - return; - } - let top = if top.format() == dst.format() { top.clone() } else { top.convert(dst.format()) }; - let fmt = dst.format(); - let n = fmt.channels(); - let (t, mut d) = (top.read_region(b), dst.read_region(b)); - for (tp, dp) in t.chunks_exact(n).zip(d.chunks_exact_mut(n)) { - if !fmt.alpha { - dp.copy_from_slice(tp); - continue; - } - let (ta, da) = (tp[n - 1], dp[n - 1]); - let oa = ta + da * (1.0 - ta); - for c in 0..n - 1 { - dp[c] = if oa > 0.0 { (tp[c] * ta + dp[c] * da * (1.0 - ta)) / oa } else { 0.0 }; - } - dp[n - 1] = oa; - } - dst.write_region(b, &d); -} - -fn vanishing_point(s: &mut Session, p: &Value) -> Result { - let st = s.active().ok_or(EngineError::NoDocument)?; - let canvas = st.doc.bounds(); - let sc = scene(p, canvas)?; - // Paste sources resolved up front (layer pixels or the clipboard). - let mut pastes: Vec<(usize, Surface, Rect, [f64; 2], f64)> = Vec::new(); - for (k, item) in p.get("paste").and_then(Value::as_array).cloned().unwrap_or_default().iter().enumerate() { - let plane = item.get("plane").and_then(Value::as_u64).unwrap_or(0) as usize; - if plane >= sc.planes.len() { - return Err(bad(format!("paste {k}: no plane {plane}"))); - } - let (surf, rect) = match item.get("layer").and_then(Value::as_u64) { - Some(l) => { - let sf = st.doc.layer(LayerId(l)).and_then(Layer::surface).ok_or(EngineError::NoLayer(LayerId(l)))?; - (sf.clone(), sf.content_bounds()) - } - None => { - let clip = s.clipboard.as_ref().ok_or_else(|| bad(format!("paste {k}: nothing on the clipboard (or pass `layer`)")))?; - (clip.surface.clone(), clip.surface.content_bounds()) - } - }; - let at = item.get("at").and_then(pt).unwrap_or([0.25, 0.25]); - let width = item.get("width").and_then(Value::as_f64).unwrap_or(0.5).clamp(0.01, 10.0); - pastes.push((plane, surf, rect, at, width)); - } - let clones: Vec = p.get("clone").and_then(Value::as_array).cloned().unwrap_or_default(); - let new_layer = p.get("newLayer").and_then(Value::as_bool).unwrap_or(false); - let mut dabs = 0usize; - let target = s.edit("Vanishing Point", |doc: &mut Document, active| { - let id = if new_layer { - let l = Layer::raster(doc.next_layer_name("Vanishing Point"), doc.pixel_format()); - let nid = doc.insert_above(*active, l); - *active = Some(nid); - nid - } else { - active.ok_or_else(|| EngineError::Other("no active layer".into()))? - }; - // A new layer clones from the visible image; otherwise from the layer itself. - let base: Option = new_layer.then(|| crate::file_cmds::flattened(doc, doc.pixel_format())); - let locks = doc.effective_locks(id); - let l = doc.layer_mut(id).ok_or(EngineError::NoLayer(id))?; - if locks.all || locks.pixels { - return Err(EngineError::Other(format!("layer \"{}\" is locked", l.name))); - } - let surf = l.surface_mut().ok_or_else(|| EngineError::Other("Vanishing Point needs a pixel layer".into()))?; - for (plane, src, rect, at, width) in &pastes { - if let Some(out) = paste(&sc, *plane, src, *rect, *at, *width)? { - over(surf, &out); - } - } - for (k, c) in clones.iter().enumerate() { - let source = c.get("source").and_then(pt).ok_or_else(|| bad(format!("clone {k}: `source` [x,y]")))?; - let points: Vec<[f64; 2]> = c.get("points").and_then(Value::as_array).map(|a| a.iter().filter_map(pt).collect()).unwrap_or_default(); - let size = c.get("size").and_then(Value::as_f64).unwrap_or(40.0).clamp(1.0, 2000.0); - let hardness = c.get("hardness").and_then(Value::as_f64).unwrap_or(50.0).clamp(0.0, 100.0) / 100.0; - let opacity = (c.get("opacity").and_then(Value::as_f64).unwrap_or(100.0).clamp(0.0, 100.0) / 100.0) as f32; - // Sample from the original pixels (merged with what's been pasted). - let mut work = match &base { - Some(b) => { - let mut w = b.clone(); - over(&mut w, surf); - w - } - None => surf.clone(), - }; - let before = work.clone(); - dabs += clone_stroke(&sc, &mut work, source, &points, size, hardness, opacity); - // Copy back only what the stroke changed. - let b = work.content_bounds().union(&before.content_bounds()); - if !b.is_empty() { - let (nw, ow) = (work.read_region(b), before.read_region(b)); - let n = surf.format().channels(); - let mut cur = surf.read_region(b); - for i in 0..cur.len() / n { - if nw[i * n..(i + 1) * n] != ow[i * n..(i + 1) * n] { - cur[i * n..(i + 1) * n].copy_from_slice(&nw[i * n..(i + 1) * n]); - } - } - surf.write_region(b, &cur); - } - } - surf.prune(); - Ok(id) - })?; - let planes: Vec = sc - .planes - .iter() - .enumerate() - .map(|(i, pl)| { - let (lu, lv) = sc.lifted(i).lengths(); - json!({"corners": pl.corners, "aspect": lu / lv}) - }) - .collect(); - Ok(json!({"layer": target.0, "focal": sc.focal, "planes": planes, "pasted": pastes.len(), "dabs": dabs})) -} - -pub fn specs() -> Vec { - vec![CommandSpec { - id: VP, - label: "Vanishing Point…", - menu: &["Filter"], - shortcut: Some("Cmd+Alt+V"), - params: r##"{"planes":[{"corners":[[x,y]×4]} | {"from":plane,"edge":"top|right|bottom|left","angle":deg=90,"depth":1}],"focalLength":px=0,"paste":[{"plane":0,"layer":id? (else the clipboard),"at":[u,v]=[0.25,0.25],"width":0..1=0.5}],"clone":[{"source":[x,y],"points":[[x,y]],"size":px=40,"hardness":0..100=50,"opacity":0..100=100}],"newLayer":bool=false}"##, - enabled: has_layer, - run: vanishing_point, - journal: true, - }] -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn planes_paste_and_clone() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 400, "height": 300})).unwrap(); - s.execute("layer.new.layer", json!({"name": "floor"})).unwrap(); - s.execute("edit.fill", json!({"color": "#404040"})).unwrap(); - let floor = s.active().unwrap().active_layer.unwrap(); - // A trapezoid "floor" receding to the top. - let planes = json!([{"corners": [[120, 120], [280, 120], [360, 280], [40, 280]]}, {"from": 0, "edge": "top", "angle": 90}]); - s.execute("layer.new.layer", json!({"name": "sticker"})).unwrap(); - s.edit("sticker", |doc, active| { - doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(0, 0, 40, 20), &[1.0, 0.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - let sticker = s.active().unwrap().active_layer.unwrap(); - s.execute("layer.select", json!({"layer": floor.0})).unwrap(); - let r = s.execute(VP, json!({"planes": planes, "paste": [{"plane": 0, "layer": sticker.0, "at": [0.3, 0.4], "width": 0.4}]})).unwrap(); - assert_eq!(r["planes"].as_array().unwrap().len(), 2); - assert!(r["focal"].as_f64().unwrap() > 50.0, "{r}"); - let l = s.active().unwrap().doc.layer(floor).unwrap().surface().unwrap().clone(); - // The paste covers the plane's middle and not its far corner. - let h = VpPlane { corners: [[120.0, 120.0], [280.0, 120.0], [360.0, 280.0], [40.0, 280.0]] }.homography().unwrap(); - let (x, y) = h.apply(0.5, 0.41); - assert!(l.rgba(x as i32, y as i32)[0] > 0.9, "{:?}", l.rgba(x as i32, y as i32)); - assert!(l.rgba(130, 125)[0] < 0.5); - s.undo(); - // Clone: copy the bright stripe painted near the bottom to the top of the floor. - s.edit("stripe", |doc, _| { - doc.layer_mut(floor).unwrap().surface_mut().unwrap().fill_rect(Rect::new(180, 250, 220, 265), &[0.0, 1.0, 0.0, 1.0]); - Ok(()) - }) - .unwrap(); - s.execute("layer.select", json!({"layer": floor.0})).unwrap(); - let r = s - .execute(VP, json!({"planes": planes, "clone": [{"source": [200, 257], "points": [[200, 160], [205, 160]], "size": 12, "hardness": 80}]})) - .unwrap(); - assert!(r["dabs"].as_u64().unwrap() >= 1, "{r}"); - let l = s.active().unwrap().doc.layer(floor).unwrap().surface().unwrap(); - assert!(l.rgba(200, 160)[1] > 0.8, "{:?}", l.rgba(200, 160)); - assert!(s.execute(VP, json!({})).is_err()); - assert!(s.execute(VP, json!({"planes": [{"corners": [[0, 0], [1, 1]]}]})).is_err()); - let r = s.execute(VP, json!({"planes": planes, "newLayer": true})).unwrap(); - assert_ne!(r["layer"].as_u64().unwrap(), floor.0); - } - - #[test] - fn rejects_planes_that_are_not_convex() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 200, "height": 200, "depth": 16})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - let bowtie = json!([[20, 20], [180, 180], [180, 20], [20, 180]]); - let dent = json!([[20, 20], [180, 20], [60, 60], [20, 180]]); - let square = json!([[20, 20], [180, 20], [180, 180], [20, 180]]); - for planes in [json!([{"corners": bowtie}]), json!([{"corners": square}, {"corners": dent}])] { - let e = s.execute(VP, json!({"planes": planes})).unwrap_err().to_string(); - assert!(e.contains("convex"), "{e}"); - } - assert!(s.execute(VP, json!({"planes": [{"corners": square}]})).is_ok()); - } -} diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rs deleted file mode 100644 index 595ad21956..0000000000 --- a/crates/engine/src/web_cmds.rs +++ /dev/null @@ -1,1507 +0,0 @@ -//! File › Export › Save for Web (Legacy), File › Export › Export Preferences, the Quick Export -//! pipeline that honours them, and File › Generate › Image Assets (Photoshop's Generator). -//! -//! Save for Web optimises the flattened (optionally resized, sRGB-converted) image as GIF, -//! PNG-8, PNG-24, JPEG or WBMP, per slice, into a folder with an optional HTML table; without an -//! output it only reports the optimised size (the dialog's 2-Up/4-Up annotations). Colour -//! reduction and dithering reuse `algo::quantize`; GIF/WBMP/progressive JPEG writers live in -//! `codecs::web`. -//! -//! Image Assets implements the naming grammar of Adobe's public Generator documentation: a -//! layer or group named `foo.png`, `200% foo@2x.png, 48x48 icons/foo.png8`, `photo.jpg80%`, -//! `?x100 thumb.gif` is exported into `-assets/` whenever the document is saved while -//! the feature is on; a `default` layer (`default 50% low/ + 200% @2x`) adds variants. - -use photocraft_algo::quantize::{self, Dither, Forced, PaletteKind}; -use photocraft_doc::slices::{self, ResolvedSlice, SliceKind}; -use photocraft_doc::{DocId, Document, LayerContent, LayerId}; -use photocraft_geom::Rect; -use serde_json::{Value, json}; - -use crate::commands::CommandSpec; -use crate::file_cmds::{join, native_doc, stem, write_file}; -use crate::{EngineError, Result, Session}; - -fn bad(cmd: &str, msg: impl Into) -> EngineError { - EngineError::BadParams { cmd: cmd.into(), msg: msg.into() } -} -fn other(e: impl std::fmt::Display) -> EngineError { - EngineError::Other(e.to_string()) -} - -// ---------- settings ---------- - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum WebFormat { - Gif, - Png8, - Png24, - Jpeg, - Wbmp, -} - -impl WebFormat { - pub fn from_id(s: &str) -> Option { - Some(match s.to_ascii_lowercase().as_str() { - "gif" => WebFormat::Gif, - "png8" | "png-8" => WebFormat::Png8, - "png24" | "png-24" | "png" | "png32" => WebFormat::Png24, - "jpeg" | "jpg" => WebFormat::Jpeg, - "wbmp" => WebFormat::Wbmp, - _ => return None, - }) - } - pub fn ext(self) -> &'static str { - match self { - WebFormat::Gif => "gif", - WebFormat::Png8 | WebFormat::Png24 => "png", - WebFormat::Jpeg => "jpg", - WebFormat::Wbmp => "wbmp", - } - } - pub fn indexed(self) -> bool { - matches!(self, WebFormat::Gif | WebFormat::Png8) - } -} - -/// Which metadata Save for Web embeds (JPEG only, like Photoshop's legacy exporter). -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum WebMetadata { - None, - Copyright, - CopyrightAndContact, - All, -} - -/// One optimisation preset (the right-hand side of the Save for Web dialog). -#[derive(Clone, Debug, PartialEq)] -pub struct WebSettings { - pub format: WebFormat, - pub palette: PaletteKind, - pub colors: usize, - /// Fixed opaque source palette in index order. - pub color_table: Option>, - /// Replacements applied after quantization, preserving pixel indices. - pub color_table_edits: std::collections::BTreeMap, - pub dither: Dither, - /// 0..=1. - pub dither_amount: f32, - pub transparency: bool, - /// Edge / background colour (0..=1 RGB); None = no matte (hard alpha threshold). - pub matte: Option<[f32; 3]>, - pub interlaced: bool, - /// Web Snap: palette entries within this distance (0..=1 of the channel range) snap to the - /// web-safe cube. - pub web_snap: f32, - /// JPEG quality 0..=100. - pub quality: u8, - pub progressive: bool, - pub optimized: bool, - pub embed_icc: bool, - pub metadata: WebMetadata, - pub convert_to_srgb: bool, -} - -impl Default for WebSettings { - fn default() -> Self { - WebSettings { - format: WebFormat::Png24, - palette: PaletteKind::Selective, - colors: 256, - color_table: None, - color_table_edits: Default::default(), - dither: Dither::Diffusion, - dither_amount: 0.88, - transparency: true, - matte: Some([1.0, 1.0, 1.0]), - interlaced: false, - web_snap: 0.0, - quality: 60, - progressive: false, - optimized: true, - embed_icc: false, - metadata: WebMetadata::Copyright, - convert_to_srgb: true, - } - } -} - -fn hex3(s: &str) -> Option<[f32; 3]> { - let s = s.trim_start_matches('#'); - let b = |i: usize| u8::from_str_radix(s.get(i..i + 2)?, 16).ok().map(|v| f32::from(v) / 255.0); - (s.len() == 6).then_some(())?; - Some([b(0)?, b(2)?, b(4)?]) -} - -impl WebSettings { - /// Settings from command parameters (missing keys keep the defaults; Photoshop's preset - /// names such as "GIF 128 Dithered" or "JPEG High" are accepted as `"preset"`). - /// - /// Without a preset there is no matte, as in Photoshop's scripted Save for Web - /// (`ExportOptionsSaveForWeb` left at its defaults): a GIF or PNG-8 keeps the colour of its - /// partly transparent pixels instead of lightening them towards white (#4115). Formats - /// without transparency still go over white. - pub fn from_params(p: &Value, cmd: &str) -> Result { - let mut st = WebSettings { matte: None, ..WebSettings::default() }; - if let Some(preset) = p.get("preset").and_then(Value::as_str) { - st = preset_settings(preset).ok_or_else(|| bad(cmd, format!("unknown preset `{preset}`")))?; - } - let s = |k: &str| p.get(k).and_then(Value::as_str); - if let Some(f) = s("format") { - st.format = WebFormat::from_id(f).ok_or_else(|| bad(cmd, format!("unknown format `{f}` (gif|png8|png24|jpeg|wbmp)")))?; - } - if let Some(k) = s("palette") { - st.palette = match k { - "restrictive" | "web" => PaletteKind::Web, - k => PaletteKind::from_id(k).ok_or_else(|| { - bad(cmd, format!("unknown palette `{k}` (perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform)")) - })?, - }; - } - if let Some(n) = crate::commands::int(p, "colors").filter(|v| *v > 0).map(|v| v as u64) { - st.colors = (n as usize).clamp(2, 256); - } - if let Some(d) = s("dither") { - st.dither = match d { - "none" | "noDither" => Dither::None, - "diffusion" => Dither::Diffusion, - "pattern" => Dither::Pattern, - "noise" => Dither::Noise, - d => return Err(bad(cmd, format!("unknown dither `{d}` (none|diffusion|pattern|noise)"))), - }; - } - if let Some(a) = p.get("ditherAmount").and_then(Value::as_f64) { - st.dither_amount = (a as f32 / 100.0).clamp(0.0, 1.0); - } - if let Some(b) = p.get("transparency").and_then(Value::as_bool) { - st.transparency = b; - } - match s("matte") { - Some("none") => st.matte = None, - Some(c) => st.matte = Some(hex3(c).ok_or_else(|| bad(cmd, "matte is \"none\" or \"#rrggbb\""))?), - None => {} - } - for (k, slot) in [ - ("interlaced", &mut st.interlaced), - ("progressive", &mut st.progressive), - ("optimized", &mut st.optimized), - ("embedIcc", &mut st.embed_icc), - ("convertToSrgb", &mut st.convert_to_srgb), - ] { - if let Some(b) = p.get(k).and_then(Value::as_bool) { - *slot = b; - } - } - if let Some(w) = p.get("webSnap").and_then(Value::as_f64) { - st.web_snap = (w as f32 / 100.0).clamp(0.0, 1.0); - } - if let Some(q) = p.get("quality").and_then(Value::as_f64) { - st.quality = q.clamp(0.0, 100.0).round() as u8; - } - if let Some(m) = s("metadata") { - st.metadata = match m { - "none" => WebMetadata::None, - "copyright" => WebMetadata::Copyright, - "copyrightAndContact" | "contact" => WebMetadata::CopyrightAndContact, - "all" | "allExceptCameraInfo" => WebMetadata::All, - m => return Err(bad(cmd, format!("unknown metadata `{m}` (none|copyright|copyrightAndContact|all)"))), - }; - } - if let Some(table) = p.get("colorTable") { - let table = table.as_array().filter(|a| (1..=256).contains(&a.len())).ok_or_else(|| bad(cmd, "colorTable must contain 1–256 #rrggbb colours"))?; - st.color_table = Some( - table - .iter() - .map(|v| v.as_str().and_then(hex3).map(|c| c.map(to8)).ok_or_else(|| bad(cmd, "colorTable entries must be #rrggbb colours"))) - .collect::>>()?, - ); - } - if let Some(edits) = p.get("colorTableEdits") { - let edits = edits - .as_object() - .filter(|m| m.len() <= 256) - .ok_or_else(|| bad(cmd, "colorTableEdits must be an object of palette indices to #rrggbb colours"))?; - let table = st.color_table.as_ref().ok_or_else(|| bad(cmd, "colorTableEdits requires colorTable"))?; - for (index, value) in edits { - let index = index.parse::().ok().filter(|i| *i < table.len()).ok_or_else(|| bad(cmd, "colorTableEdits index is outside colorTable"))?; - let color = value.as_str().and_then(hex3).map(|c| c.map(to8)).ok_or_else(|| bad(cmd, "colorTableEdits values must be #rrggbb colours"))?; - st.color_table_edits.insert(index, color); - } - } - Ok(st) - } -} - -impl WebSettings { - /// The settings as command parameters (the inverse of [`WebSettings::from_params`]). - pub fn to_params(&self) -> Value { - let format = match self.format { - WebFormat::Gif => "gif", - WebFormat::Png8 => "png8", - WebFormat::Png24 => "png24", - WebFormat::Jpeg => "jpeg", - WebFormat::Wbmp => "wbmp", - }; - let palette = match self.palette { - PaletteKind::Exact => "exact", - PaletteKind::SystemMac => "systemMac", - PaletteKind::SystemWindows => "systemWindows", - PaletteKind::Web => "restrictive", - PaletteKind::Uniform => "uniform", - PaletteKind::Perceptual => "perceptual", - PaletteKind::Selective => "selective", - PaletteKind::Adaptive => "adaptive", - }; - let dither = match self.dither { - Dither::None => "none", - Dither::Diffusion => "diffusion", - Dither::Pattern => "pattern", - Dither::Noise => "noise", - }; - let metadata = match self.metadata { - WebMetadata::None => "none", - WebMetadata::Copyright => "copyright", - WebMetadata::CopyrightAndContact => "copyrightAndContact", - WebMetadata::All => "all", - }; - let matte = self.matte.map_or("none".to_string(), |c| format!("#{:02x}{:02x}{:02x}", to8(c[0]), to8(c[1]), to8(c[2]))); - let mut p = json!({"format": format, "palette": palette, "colors": self.colors, "dither": dither, "ditherAmount": (self.dither_amount * 100.0).round(), "transparency": self.transparency, "matte": matte, "interlaced": self.interlaced, "webSnap": (self.web_snap * 100.0).round(), "quality": self.quality, "progressive": self.progressive, "optimized": self.optimized, "embedIcc": self.embed_icc, "metadata": metadata, "convertToSrgb": self.convert_to_srgb}); - if let Some(table) = &self.color_table { - p["colorTable"] = json!(table.iter().map(|c| rgb_hex(*c)).collect::>()); - } - if !self.color_table_edits.is_empty() { - p["colorTableEdits"] = - json!(self.color_table_edits.iter().map(|(i, c)| (i.to_string(), rgb_hex(*c))).collect::>()); - } - p - } -} - -/// A palette colour in command/control-channel notation. -pub fn rgb_hex(c: [u8; 3]) -> String { - format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2]) -} - -/// Save for Web's named presets. -pub const PRESETS: [&str; 12] = [ - "GIF 128 Dithered", - "GIF 128 No Dither", - "GIF 32 Dithered", - "GIF 32 No Dither", - "GIF 64 Dithered", - "GIF 64 No Dither", - "GIF Restrictive", - "JPEG High", - "JPEG Low", - "JPEG Medium", - "PNG-24", - "PNG-8 128 Dithered", -]; - -pub fn preset_settings(name: &str) -> Option { - let d = WebSettings::default(); - let gif = |colors: usize, dither: bool| WebSettings { - format: WebFormat::Gif, - colors, - dither: if dither { Dither::Diffusion } else { Dither::None }, - ..WebSettings::default() - }; - Some(match name { - "GIF 128 Dithered" => gif(128, true), - "GIF 128 No Dither" => gif(128, false), - "GIF 32 Dithered" => gif(32, true), - "GIF 32 No Dither" => gif(32, false), - "GIF 64 Dithered" => gif(64, true), - "GIF 64 No Dither" => gif(64, false), - "GIF Restrictive" => WebSettings { palette: PaletteKind::Web, ..gif(256, true) }, - "JPEG High" => WebSettings { format: WebFormat::Jpeg, quality: 60, ..d }, - "JPEG Low" => WebSettings { format: WebFormat::Jpeg, quality: 10, ..d }, - "JPEG Medium" => WebSettings { format: WebFormat::Jpeg, quality: 30, ..d }, - "JPEG Maximum" => WebSettings { format: WebFormat::Jpeg, quality: 100, ..d }, - "JPEG Very High" => WebSettings { format: WebFormat::Jpeg, quality: 80, ..d }, - "PNG-24" => WebSettings { format: WebFormat::Png24, ..d }, - "PNG-8 128 Dithered" => WebSettings { format: WebFormat::Png8, colors: 128, ..d }, - _ => return None, - }) -} - -// ---------- the image ---------- - -/// The document as Save for Web sees it: RGB (converted to sRGB unless told otherwise), resized -/// by `"width"`/`"height"`/`"percent"`, flattened. Returns the document and the scale factors. -pub fn web_document(doc: &Document, p: &Value, st: &WebSettings) -> Result<(Document, f64, f64)> { - let mut tmp = Session::new(); - tmp.add_document(doc.clone(), None); - let mode = doc.mode; - if mode != photocraft_color::ColorMode::Rgb { - tmp.execute("image.mode.rgb", json!({}))?; - } - if st.convert_to_srgb && doc.icc_profile.is_some() { - // Documents without a profile are treated as sRGB already. - tmp.execute("edit.convertToProfile", json!({"profile": "srgb", "intent": "perceptual"}))?; - } - let (w0, h0) = (f64::from(doc.size.width), f64::from(doc.size.height)); - let pct = p.get("percent").and_then(Value::as_f64).filter(|v| *v > 0.0); - let w = p.get("width").and_then(Value::as_f64).filter(|v| *v > 0.0); - let h = p.get("height").and_then(Value::as_f64).filter(|v| *v > 0.0); - let (nw, nh) = match (pct, w, h) { - (Some(pc), _, _) => (w0 * pc / 100.0, h0 * pc / 100.0), - (_, Some(w), Some(h)) => (w, h), - (_, Some(w), None) => (w, h0 * w / w0), - (_, None, Some(h)) => (w0 * h / h0, h), - _ => (w0, h0), - }; - let (nw, nh) = (nw.round().max(1.0), nh.round().max(1.0)); - if (nw - w0).abs() >= 1.0 || (nh - h0).abs() >= 1.0 { - let resample = p.get("resample").and_then(Value::as_str).unwrap_or("bicubic"); - tmp.execute("image.imageSize", json!({"width": nw, "height": nh, "resample": resample}))?; - } - let out = (*tmp.active().ok_or(EngineError::NoDocument)?.doc).clone(); - Ok((out, nw / w0, nh / h0)) -} - -/// An optimised image. -#[derive(Clone, Debug)] -pub struct Optimized { - pub bytes: Vec, - pub ext: &'static str, - pub width: u32, - pub height: u32, - /// Colours in the palette (indexed formats). - pub colors: Option, - /// Opaque source palette before replacements (empty for non-indexed formats). - pub color_table: Vec<[u8; 3]>, - pub transparent_index: Option, - /// Indexed pixels for previews; empty for non-indexed formats or when preview is disabled. - pub indices: Vec, - /// What the optimised file looks like, straight RGBA8 (for the dialog's previews). - pub preview: Vec, -} - -fn to8(v: f32) -> u8 { - (v.clamp(0.0, 1.0) * 255.0).round() as u8 -} - -/// XMP to embed for the metadata choice. -pub fn web_xmp(doc: &Document, m: WebMetadata) -> Option { - let xmp = doc.metadata.xmp.as_deref()?; - match m { - WebMetadata::None => None, - WebMetadata::All => Some(xmp.to_string()), - WebMetadata::Copyright | WebMetadata::CopyrightAndContact => { - let info = crate::file_cmds::read_file_info(Some(xmp)); - let mut keep = serde_json::Map::new(); - let mut keys = vec!["copyright", "copyrightStatus", "copyrightUrl"]; - if m == WebMetadata::CopyrightAndContact { - keys.extend(["author", "authorTitle"]); - } - for k in keys { - if let Some(v) = info.get(k).filter(|v| v.as_str().is_some_and(|s| !s.is_empty()) || v.is_array()) { - keep.insert(k.into(), v.clone()); - } - } - (!keep.is_empty()).then(|| crate::file_cmds::write_file_info(None, &Value::Object(keep))) - } - } -} - -/// Optimises `rect` of `px` (straight RGBA, `bw` wide) with `st`. `preview` also returns what -/// the file looks like (the dialog's panes); exports skip it. -#[allow(clippy::too_many_arguments)] -pub fn optimize(px: &[[f32; 4]], bw: usize, rect: Rect, st: &WebSettings, icc: Option<&[u8]>, xmp: Option<&str>, dpi: f32, preview: bool) -> Result { - if bw == 0 || rect.x0 < 0 || rect.y0 < 0 || rect.is_empty() || rect.x1 as usize > bw || (rect.y1 as usize).checked_mul(bw).is_none_or(|end| end > px.len()) - { - return Err(other("slice is outside the pixel buffer")); - } - let (w, h) = (rect.width() as usize, rect.height() as usize); - let row = |y: i32| { - let o = y as usize * bw; - px.get(o + rect.x0 as usize..o + rect.x1 as usize).unwrap_or(&[]) - }; - let region = || (rect.y0..rect.y1).flat_map(row); - let matte = st.matte.unwrap_or([1.0, 1.0, 1.0]); - let over = |p: &[f32; 4]| -> [f32; 4] { - [p[0] * p[3] + matte[0] * (1.0 - p[3]), p[1] * p[3] + matte[1] * (1.0 - p[3]), p[2] * p[3] + matte[2] * (1.0 - p[3]), 1.0] - }; - let (ww, hh) = (w as u32, h as u32); - let e = |e: photocraft_codecs::CodecError| other(e); - let mut look: Vec = Vec::new(); - let mut color_table = Vec::new(); - let mut transparent_index = None; - let mut indices = Vec::new(); - let (bytes, colors) = match st.format { - WebFormat::Png24 => { - let alpha = st.transparency && region().any(|p| p[3] < 1.0); - let k = if alpha { 4 } else { 3 }; - let mut data = Vec::with_capacity(w * h * k); - for p in region() { - let q = if alpha { *p } else { over(p) }; - data.extend_from_slice(&[to8(q[0]), to8(q[1]), to8(q[2]), to8(q[3])][..k]); - } - if preview { - look = if alpha { data.clone() } else { data.as_chunks::<3>().0.iter().flat_map(|c| [c[0], c[1], c[2], 255]).collect() }; - } - let layout = if alpha { photocraft_codecs::ChannelLayout::Rgba } else { photocraft_codecs::ChannelLayout::Rgb }; - let meta = photocraft_codecs::Metadata { dpi: Some((dpi, dpi)), xmp: xmp.map(str::to_string), ..Default::default() }; - let img = photocraft_codecs::Image::from_u8(ww, hh, layout, data).map_err(e)?.with_icc(icc.map(<[u8]>::to_vec)).with_meta(meta); - let opts = photocraft_codecs::EncodeOptions { - png_interlaced: st.interlaced, - embed_icc: icc.is_some(), - embed_metadata: xmp.is_some(), - ..Default::default() - }; - (photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &opts).map_err(e)?, None) - } - WebFormat::Jpeg => { - let mut rgb = Vec::with_capacity(w * h * 3); - for p in region() { - let q = over(p); - rgb.extend_from_slice(&[to8(q[0]), to8(q[1]), to8(q[2])]); - } - // Save for Web's 0–100 quality → encoder 1–100. - let q = st.quality.max(1); - let bytes = photocraft_codecs::web::encode_jpeg_rgb8(ww, hh, &rgb, q, st.progressive, st.optimized, icc.filter(|_| st.embed_icc), Some(dpi), xmp) - .map_err(e)?; - if preview { - // The preview shows the compression artefacts. - look = match photocraft_codecs::decode(&bytes) { - Ok(img) => img.convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8).data().to_vec(), - Err(_) => rgb.as_chunks::<3>().0.iter().flat_map(|c| [c[0], c[1], c[2], 255]).collect(), - }; - } - (bytes, None) - } - WebFormat::Wbmp => { - let mut gray: Vec = region() - .map(|p| { - let q = over(p); - 0.299 * q[0] + 0.587 * q[1] + 0.114 * q[2] - }) - .collect(); - let method = match st.dither { - Dither::None => quantize::BitmapMethod::Threshold, - Dither::Pattern => quantize::BitmapMethod::Pattern, - _ => quantize::BitmapMethod::Diffusion, - }; - quantize::to_bitmap(&mut gray, w, method); - let white: Vec = gray.iter().map(|v| *v > 0.5).collect(); - if preview { - look = white.iter().flat_map(|b| if *b { [255, 255, 255, 255] } else { [0, 0, 0, 255] }).collect(); - } - (photocraft_codecs::web::encode_wbmp(ww, hh, &white).map_err(e)?, Some(2)) - } - WebFormat::Gif | WebFormat::Png8 => { - let transparent = st.transparency && region().any(|p| p[3] < 0.5); - // Partially transparent edges blend with the matte; with transparency off everything does. - let mut work: Vec<[f32; 4]> = region() - .map(|p| { - if st.transparency && p[3] < 0.5 { - [0.0, 0.0, 0.0, 0.0] - } else if st.transparency && st.matte.is_none() { - [p[0], p[1], p[2], 1.0] - } else { - over(p) - } - }) - .collect(); - let room = if transparent { st.colors.saturating_sub(1).max(2) } else { st.colors }; - let mut pal = if let Some(table) = &st.color_table { - if table.is_empty() || table.len() > if transparent { 255 } else { 256 } { - return Err(other("colorTable has too many entries for this image's transparency")); - } - table.clone() - } else { - // Large images: build adaptive palettes from an even ~1 MP sample (Exact needs all). - let n_opaque = work.iter().filter(|p| p[3] >= 0.5).count(); - let step = if n_opaque > 1 << 20 && st.palette != PaletteKind::Exact { n_opaque / (1 << 20) + 1 } else { 1 }; - let mut src: Vec<[f32; 4]> = work.iter().filter(|p| p[3] >= 0.5).step_by(step).copied().collect(); - if src.is_empty() { - src = work.clone(); - } - quantize::build_palette(&src, st.palette, room, Forced::None).map_err(other)? - }; - if st.web_snap > 0.0 && st.color_table.is_none() { - let tol = (st.web_snap * 255.0) as u8; - for c in &mut pal { - let snap = c.map(|x| ((f32::from(x) / 51.0).round() * 51.0) as u8); - if c.iter().zip(snap).all(|(a, b)| a.abs_diff(b) <= tol) { - *c = snap; - } - } - } - if transparent { - pal.truncate(255); - } - let first = pal.first().copied().ok_or_else(|| other("empty colour table"))?; - color_table = pal.clone(); - let t = transparent.then(|| { - pal.push(first); - pal.len() - 1 - }); - transparent_index = t; - let idx = quantize_rows(&mut work, w, &pal, st.dither, st.dither_amount, t); - for (index, color) in &st.color_table_edits { - let slot = pal.get_mut(*index).filter(|_| Some(*index) != t).ok_or_else(|| other("colour edit is outside the opaque palette"))?; - *slot = *color; - } - if preview { - look = idx - .iter() - .flat_map(|k| { - let c = pal.get(*k as usize).copied().unwrap_or(first); - [c[0], c[1], c[2], if Some(*k as usize) == t { 0 } else { 255 }] - }) - .collect(); - } - let tt = t.map(|v| v as u8); - let bytes = if st.format == WebFormat::Gif { - photocraft_codecs::web::encode_gif_indexed(ww, hh, &idx, &pal, tt, st.interlaced).map_err(e)? - } else { - photocraft_codecs::encode_png_indexed(ww, hh, &idx, &pal, tt, None).map_err(e)? - }; - if preview { - indices = idx; - } - (bytes, Some(pal.len())) - } - }; - Ok(Optimized { bytes, ext: st.format.ext(), width: ww, height: hh, colors, color_table, transparent_index, indices, preview: look }) -} - -/// Nearest palette entry through a lazily filled 6-bit-per-channel inverse colour map: each -/// 4×4×4 cell is resolved once (by its centre), so large images cost a table lookup per pixel -/// instead of a palette scan. `t` (the transparent index) is never returned. -struct InverseMap<'a> { - pal: &'a [[u8; 3]], - t: Option, - lut: Vec, -} - -impl<'a> InverseMap<'a> { - fn new(pal: &'a [[u8; 3]], t: Option) -> Self { - InverseMap { pal, t, lut: vec![u16::MAX; 1 << 18] } - } - #[inline] - fn get(&mut self, c: [f32; 3]) -> usize { - let q = c.map(|v| ((v.clamp(0.0, 1.0) * 255.0).round() as usize) >> 2); - let key = q[0] << 12 | q[1] << 6 | q[2]; - let v = self.lut[key]; - if v != u16::MAX { - return usize::from(v); - } - let center = q.map(|v| (v * 4 + 2) as f32); - let mut best = (f32::MAX, 0usize); - for (i, e) in self.pal.iter().enumerate() { - if Some(i) == self.t { - continue; - } - let d: f32 = (0..3).map(|k| (center[k] - f32::from(e[k])).powi(2)).sum(); - if d < best.0 { - best = (d, i); - } - } - self.lut[key] = best.1 as u16; - best.1 - } -} - -fn hash01(x: usize, y: usize) -> f32 { - let mut h = (x as u32).wrapping_mul(0x8da6_b343) ^ (y as u32).wrapping_mul(0xd816_3841) ^ 0x2545_f491; - h ^= h >> 13; - h = h.wrapping_mul(0x5bd1_e995); - h ^= h >> 15; - (h & 0xffff) as f32 / 65535.0 -} - -/// Palette mapping with dithering (Floyd–Steinberg diffusion, 8×8 Bayer pattern, noise), like -/// `algo::quantize::quantize` but through [`InverseMap`]. `y0` keeps patterns continuous across -/// bands. Pixels with alpha < ½ take index `t`. -fn quantize_band(px: &mut [[f32; 4]], w: usize, y0: usize, pal: &[[u8; 3]], dither: Dither, amount: f32, t: Option) -> Vec { - let h = px.len() / w.max(1); - let mut map = InverseMap::new(pal, t); - let mut idx = vec![0u8; px.len()]; - let opaque_n = pal.len() - usize::from(t.is_some()); - let spread = (1.0 / (opaque_n as f32).cbrt().max(1.0)).min(0.5) * amount.clamp(0.0, 1.0); - let a = amount.clamp(0.0, 1.0); - let mut cur = vec![[0.0f32; 3]; if dither == Dither::Diffusion { w + 2 } else { 0 }]; - let mut next = cur.clone(); - for y in 0..h { - if dither == Dither::Diffusion { - std::mem::swap(&mut cur, &mut next); - next.iter_mut().for_each(|e| *e = [0.0; 3]); - } - for x in 0..w { - let i = y * w + x; - let p = px[i]; - if p[3] < 0.5 - && let Some(t) = t - { - idx[i] = t as u8; - continue; - } - let mut c = [p[0], p[1], p[2]]; - match dither { - Dither::None => {} - Dither::Diffusion => (0..3).for_each(|k| c[k] += cur[x + 1][k]), - Dither::Pattern => { - let d = quantize::bayer8(x, y0 + y) * spread; - c = c.map(|v| v + d); - } - Dither::Noise => { - let d = (hash01(x, y0 + y) - 0.5) * spread; - c = c.map(|v| v + d); - } - } - let c = c.map(|v| v.clamp(0.0, 1.0)); - let k = map.get(c); - idx[i] = k as u8; - if dither == Dither::Diffusion { - let e = pal[k].map(|v| f32::from(v) / 255.0); - let d: [f32; 3] = std::array::from_fn(|q| (c[q] - e[q]) * a); - for q in 0..3 { - cur[x + 2][q] += d[q] * 7.0 / 16.0; - next[x][q] += d[q] * 3.0 / 16.0; - next[x + 1][q] += d[q] * 5.0 / 16.0; - next[x + 2][q] += d[q] / 16.0; - } - } - } - } - idx -} - -/// [`quantize_band`] over the whole image, in parallel bands of 64 rows when the dither is -/// position-independent. Error diffusion carries error down the image, so it stays sequential. -fn quantize_rows(px: &mut [[f32; 4]], w: usize, pal: &[[u8; 3]], dither: Dither, amount: f32, t: Option) -> Vec { - let h = px.len() / w.max(1); - if dither == Dither::Diffusion || h < 128 || cfg!(target_arch = "wasm32") { - return quantize_band(px, w, 0, pal, dither, amount, t); - } - #[cfg(not(target_arch = "wasm32"))] - { - use rayon::prelude::*; - let band = w * 64; - let mut idx = vec![0u8; px.len()]; - px.par_chunks_mut(band) - .zip(idx.par_chunks_mut(band)) - .enumerate() - .for_each(|(i, (p, out))| out.copy_from_slice(&quantize_band(p, w, i * 64, pal, dither, amount, t))); - idx - } - // Single-threaded on wasm (the early return above always takes this path there). - #[cfg(target_arch = "wasm32")] - quantize_band(px, w, 0, pal, dither, amount, t) -} - -/// A file-name-safe slice name. -fn slice_file(name: &str) -> String { - let s: String = name.chars().map(|c| if c.is_alphanumeric() || matches!(c, '-' | '_') { c } else { '_' }).collect(); - let s = if s.is_empty() { "slice".into() } else { s }; - portable_generated_stem(s) -} - -/// Generated filenames should remain usable on Windows even when a document or slice is named -/// after a reserved device. Caller-supplied path components are still rejected below. -fn portable_generated_stem(stem: String) -> String { - if is_windows_device_asset_name(&stem) { format!("{stem}_") } else { stem } -} - -fn html_escape(s: &str) -> String { - s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) -} - -/// Imported slice URLs are document data. Only ordinary web/contact links and relative links -/// become anchors; an unsupported scheme leaves the image unlinked. -fn safe_slice_url(url: &str) -> bool { - if url.is_empty() || url.trim() != url || url.chars().any(|c| c.is_control() || c == '\\') { - return false; - } - let delimiter = url.find(['/', '?', '#']).unwrap_or(url.len()); - let Some(colon) = url.find(':').filter(|pos| *pos < delimiter) else { - return true; - }; - let scheme = url.get(..colon).unwrap_or_default().to_ascii_lowercase(); - let rest = url.get(colon + 1..).unwrap_or_default(); - match scheme.as_str() { - "http" | "https" | "ftp" | "ftps" => rest.strip_prefix("//").is_some_and(|host| !host.is_empty() && !host.starts_with('/')), - "mailto" | "tel" => !rest.is_empty(), - _ => false, - } -} - -/// Keep the image folder relative to the selected export directory on every platform. -fn web_images_folder(images: &str, cmd: &str) -> Result { - if images.is_empty() { - return Ok(String::new()); - } - let trimmed = images.trim_end_matches(['/', '\\']); - if trimmed.is_empty() { - return Err(bad(cmd, "imagesFolder must be a relative folder name")); - } - let parts = asset_path_components(trimmed).map_err(|e| bad(cmd, format!("imagesFolder: {e}")))?; - Ok(parts.join("/")) -} - -fn web_output_path(dir: &str, relative: &str) -> Result { - let root = if dir.is_empty() { "." } else { dir }; - asset_output_path(root, relative).map(|(actual, _)| actual) -} - -fn web_derived_path(dir: &str, relative: &str, native_paths: bool) -> Result { - if native_paths { - web_output_path(dir, relative) - } else { - // Browser downloads use logical names, without a host filesystem to inspect. - asset_path_components(relative)?; - Ok(join(dir, relative)) - } -} - -fn scale_rect(r: Rect, sx: f64, sy: f64) -> Rect { - Rect::new( - (f64::from(r.x0) * sx).round() as i32, - (f64::from(r.y0) * sy).round() as i32, - (f64::from(r.x1) * sx).round() as i32, - (f64::from(r.y1) * sy).round() as i32, - ) -} - -/// The HTML table for `cells` (rect, td body) over a `w`×`h` image: one column per distinct x -/// edge, one row per y edge, colspan/rowspan per slice, and a spacer row fixing column widths. -fn html_table(title: &str, w: u32, h: u32, cells: &[(Rect, String, String)], spacer: Option<&str>) -> String { - let mut xs: Vec = cells.iter().flat_map(|(r, _, _)| [r.x0, r.x1]).collect(); - let mut ys: Vec = cells.iter().flat_map(|(r, _, _)| [r.y0, r.y1]).collect(); - xs.sort_unstable(); - xs.dedup(); - ys.sort_unstable(); - ys.dedup(); - let col = |x: i32| xs.iter().position(|v| *v == x).unwrap_or(0); - let row = |y: i32| ys.iter().position(|v| *v == y).unwrap_or(0); - let (nc, nr) = (xs.len().saturating_sub(1), ys.len().saturating_sub(1)); - let mut taken = vec![false; nc * nr]; - let mut by_row: Vec> = vec![Vec::new(); nr]; - for (r, attrs, body) in cells { - let (c0, c1, r0, r1) = (col(r.x0), col(r.x1), row(r.y0), row(r.y1)); - if (r0..r1).any(|j| (c0..c1).any(|i| taken[j * nc + i])) { - continue; // overlapped (the caller cuts slices into visible pieces, so this can't happen) - } - for j in r0..r1 { - for i in c0..c1 { - taken[j * nc + i] = true; - } - } - let mut td = String::from("\t\t 1 { - td.push_str(&format!(" colspan=\"{}\"", c1 - c0)); - } - if r1 - r0 > 1 { - td.push_str(&format!(" rowspan=\"{}\"", r1 - r0)); - } - td.push_str(attrs); - td.push('>'); - td.push_str(body); - td.push_str("
\n", - t = html_escape(title) - ); - for (j, mut tds) in by_row.into_iter().enumerate() { - tds.sort_by_key(|(c, _)| *c); - s.push_str("\t\n"); - for (_, td) in tds { - s.push_str(&td); - } - if let Some(sp) = spacer { - s.push_str(&format!("\t\t\n", html_escape(sp), ys[j + 1] - ys[j])); - } - s.push_str("\t\n"); - } - if let Some(sp) = spacer { - s.push_str("\t\n"); - for i in 0..nc { - s.push_str(&format!("\t\t\n", html_escape(sp), xs[i + 1] - xs[i])); - } - s.push_str("\t\t\n\t\n"); - } - s.push_str("
\n\t\t\t\"\"
\n\t\t\t\"\"
\n\n\n\n"); - s -} - -/// File › Export › Save for Web (Legacy). -fn save_for_web(s: &mut Session, p: &Value) -> Result { - save_for_web_impl(s, p, &mut write_file, true) -} - -/// Save for Web through a platform writer (native files or browser downloads). -/// The writer receives every image, spacer and HTML file. A write error stops the export; -/// only a complete export remembers its settings and fires the export event. A custom writer -/// that targets the local filesystem must enforce its own path confinement. -pub fn save_for_web_with_writer(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u8]) -> Result<()>) -> Result { - save_for_web_impl(s, p, write, false) -} - -/// Settings to seed the Save for Web dialog from on the next launch: `file_menu` is session -/// state, so a complete export also writes the preferences' remembered dialog settings (like -/// Fill, Liquify and the filter dialogs). The destination is per export, never remembered. -fn remember_web_settings(s: &mut Session, cmd: &str, settings: &Value) { - let mut m = settings.clone(); - if let Some(o) = m.as_object_mut() { - for k in ["path", "dir", "trustedSliceHtml"] { - o.remove(k); - } - } - s.prefs.edit(|p| { - p.dialogs.insert(cmd.to_string(), m); - }); -} - -fn save_for_web_impl(s: &mut Session, p: &Value, write: &mut dyn FnMut(&str, &[u8]) -> Result<()>, native_paths: bool) -> Result { - let cmd = "file.export.saveForWebLegacy"; - if let Some(authorize) = s.authorize { - authorize(cmd, p)?; - } - // Direct platform calls must export the moved selection as it appears on the canvas, - // just as Session::execute does before dispatching the native command. - crate::float_cmds::before_command(s, cmd)?; - let st = WebSettings::from_params(p, cmd)?; - let d = s.active().ok_or(EngineError::NoDocument)?; - let doc = d.doc.clone(); - let (wdoc, sx, sy) = web_document(&doc, p, &st)?; - let buf = photocraft_compose::flatten(&wdoc); - let bw = wdoc.size.width as usize; - let icc = wdoc.icc_profile.as_deref().map(|v| v.as_slice()); - let xmp = web_xmp(&doc, st.metadata); - let dpi = wdoc.resolution_dpi; - let all = slices::resolve(&doc); - let pick = p.get("slices").and_then(Value::as_str).unwrap_or("all"); - let numbers: Option> = p.get("numbers").and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_u64).collect()); - let chosen: Vec<&ResolvedSlice> = all - .iter() - .filter(|r| match (&numbers, pick) { - (Some(n), _) => n.contains(&(r.number as u64)), - (None, "user") => r.origin != photocraft_doc::SliceOrigin::Auto, - _ => true, - }) - .collect(); - let single = p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty()); - let dir = p.get("dir").and_then(Value::as_str).filter(|v| !v.is_empty()); - let mut last_web = p.clone(); - // A trusted-markup decision applies to this export only, never to another document opened - // with the dialog's remembered settings. - if let Some(last) = last_web.as_object_mut() { - last.remove("trustedSliceHtml"); - } - // Estimate only (the dialog's annotations): the whole image, or each chosen slice. - if single.is_none() && dir.is_none() { - let o = optimize(&buf.px, bw, wdoc.bounds(), &st, icc, xmp.as_deref(), dpi, false)?; - return Ok( - json!({"format": o.ext, "bytes": o.bytes.len(), "width": o.width, "height": o.height, "colors": o.colors, "colorTable": o.color_table.iter().map(|c| rgb_hex(*c)).collect::>(), "transparentIndex": o.transparent_index}), - ); - } - if let Some(path) = single.filter(|_| dir.is_none() && (all.len() == 1 || p.get("slices").is_none())) { - let o = optimize(&buf.px, bw, wdoc.bounds(), &st, icc, xmp.as_deref(), dpi, false)?; - write(path, &o.bytes)?; - remember_web_settings(s, cmd, &last_web); - s.file_menu.last_web = Some(last_web); - crate::automate_cmds::fire_event(s, "export"); - return Ok(json!({"files": [path], "bytes": o.bytes.len(), "width": o.width, "height": o.height, "colors": o.colors})); - } - let dir = - dir.map(str::to_string).or_else(|| single.and_then(|f| std::path::Path::new(f).parent().map(|d| d.to_string_lossy().into_owned()))).unwrap_or_default(); - let html = p.get("html").and_then(Value::as_bool).unwrap_or(false); - let images = web_images_folder(p.get("imagesFolder").and_then(Value::as_str).unwrap_or("images"), cmd)?; - let trusted_slice_html = p.get("trustedSliceHtml").and_then(Value::as_bool).unwrap_or(false); - let base = portable_generated_stem(single.map(stem).unwrap_or_else(|| slices::base_name(&doc))); - let mut files = Vec::new(); - let mut cells: Vec<(Rect, String, String)> = Vec::new(); - let mut total = 0usize; - // Output names are unique (case-insensitively, for macOS and Windows file systems): two - // slices named alike, or names that sanitize alike, must not overwrite each other. The HTML - // page's spacer image is reserved. - let mut used: std::collections::HashSet = std::collections::HashSet::new(); - if html { - used.insert("spacer.gif".into()); - } - let mut unique = |stem: &str, ext: &str| { - let mut name = format!("{stem}.{ext}"); - let mut k = 2u32; - while !used.insert(name.to_lowercase()) { - name = format!("{stem}_{k}.{ext}"); - k = k.saturating_add(1); - } - name - }; - // Stacking order: later stored slices are on top (auto slices never overlap anything). - let z = |r: &ResolvedSlice| r.id.and_then(|id| doc.slices.list.iter().position(|sl| sl.id == id)); - for r in &chosen { - let rect = scale_rect(r.rect, sx, sy).intersect(&wdoc.bounds()); - if rect.is_empty() { - continue; - } - // In the HTML table a slice shows only where no slice above covers it; that visible part - // is cut into rectangles, each its own cell (and image), as Photoshop's subslices. - let pieces = if html { - let above: Vec = chosen.iter().filter(|o| z(o) > z(r)).map(|o| scale_rect(o.rect, sx, sy).intersect(&wdoc.bounds())).collect(); - slices::auto_slices(rect, &above) - } else { - vec![rect] - }; - let split = pieces.len() > 1; - let stored = r.id.and_then(|id| doc.slices.get(id)); - for (k, piece) in pieces.into_iter().enumerate() { - if r.kind == SliceKind::NoImage { - let text = if k > 0 { - String::new() - } else { - stored.map(|s| if trusted_slice_html && s.cell_text_is_html { s.cell_text.clone() } else { html_escape(&s.cell_text) }).unwrap_or_default() - }; - let bg = stored.and_then(|s| s.background).map(|c| format!(" bgcolor=\"#{:02X}{:02X}{:02X}\"", c[1], c[2], c[3])).unwrap_or_default(); - cells.push((piece, bg, text)); - continue; - } - let o = optimize(&buf.px, bw, piece, &st, icc, xmp.as_deref(), dpi, false)?; - let stem = if split { format!("{}_{:02}", slice_file(&r.name), k + 1) } else { slice_file(&r.name) }; - let name = unique(&stem, o.ext); - let rel = if images.is_empty() { name.clone() } else { format!("{images}/{name}") }; - let out = join(&dir, &rel); - write(&web_derived_path(&dir, &rel, native_paths)?, &o.bytes)?; - total += o.bytes.len(); - files.push(out); - let alt = stored.map_or(String::new(), |s| html_escape(&s.alt)); - let img = format!("\n\t\t\t\"{alt}\"", html_escape(&rel), piece.width(), piece.height()); - let body = match stored.filter(|s| safe_slice_url(&s.url)) { - Some(s) => { - let target = if s.target.is_empty() { String::new() } else { format!(" target=\"{}\"", html_escape(&s.target)) }; - format!("\n\t\t\t{img}", html_escape(&s.url)) - } - None => img, - }; - cells.push((piece, String::new(), body)); - } - } - let mut html_path = None; - if html { - let grid = cells.len() > 1; - let spacer = grid.then(|| if images.is_empty() { "spacer.gif".to_string() } else { format!("{images}/spacer.gif") }); - if let Some(sp) = &spacer { - let gif = photocraft_codecs::web::encode_gif_indexed(1, 1, &[0], &[[0, 0, 0]], Some(0), false).map_err(other)?; - write(&web_derived_path(&dir, sp, native_paths)?, &gif)?; - } - let page = html_table(&base, wdoc.size.width, wdoc.size.height, &cells, spacer.as_deref()); - let hp = join(&dir, &format!("{base}.html")); - write(&web_derived_path(&dir, &format!("{base}.html"), native_paths)?, page.as_bytes())?; - html_path = Some(hp); - } - remember_web_settings(s, cmd, &last_web); - s.file_menu.last_web = Some(last_web); - crate::automate_cmds::fire_event(s, "export"); - Ok(json!({"files": files, "html": html_path, "bytes": total, "slices": chosen.len(), "width": wdoc.size.width, "height": wdoc.size.height})) -} - -// ---------- Export Preferences / Quick Export ---------- - -fn export_preferences(s: &mut Session, p: &Value) -> Result { - let cmd = "file.export.exportPreferences"; - let mut values = serde_json::Map::new(); - for (k, key) in [ - ("quickExportFormat", "export.quickExportFormat"), - ("quickExportLocation", "export.quickExportLocation"), - ("jpegQuality", "export.jpegQuality"), - ("webpLossless", "export.webpLossless"), - ("webpQuality", "export.webpQuality"), - ("metadata", "export.metadata"), - ("convertToSrgb", "export.convertToSrgb"), - ] { - if let Some(v) = p.get(k) { - values.insert(key.into(), v.clone()); - } - } - if !values.is_empty() { - s.execute("prefs.set", json!({"values": values})).map_err(|e| bad(cmd, e.to_string()))?; - } - Ok(json!({"section": "export", "values": s.prefs().get("export").unwrap_or(Value::Null)})) -} - -/// File › Export › Quick Export as : the format, quality, metadata, colour space and -/// location from Export Preferences. `path` overrides the location ("ask" needs one). -fn quick_export(s: &mut Session, p: &Value) -> Result { - let cmd = "file.export.quickExport"; - let prefs = s.prefs().export.clone(); - let fmt = serde_json::to_value(prefs.quick_export_format).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_else(|| "png".into()); - let d = s.active().ok_or(EngineError::NoDocument)?; - let doc = d.doc.clone(); - let path = match p.get("path").and_then(Value::as_str).filter(|v| !v.is_empty()) { - Some(v) => v.to_string(), - None => { - let same = serde_json::to_value(prefs.quick_export_location).ok().and_then(|v| v.as_str().map(str::to_string)).is_some_and(|v| v == "sameFolder"); - match (&d.path, same) { - (Some(dp), true) => { - let dir = std::path::Path::new(dp).parent().map(|d| d.to_string_lossy().into_owned()).unwrap_or_default(); - join(&dir, &format!("{}.{fmt}", stem(dp))) - } - _ => return Err(bad(cmd, "missing `path` (Export Preferences › Location is \"ask\", or the document was never saved)")), - } - } - }; - let metadata = serde_json::to_value(prefs.metadata).ok().and_then(|v| v.as_str().map(str::to_string)).unwrap_or_default(); - let mut o = serde_json::Map::new(); - o.insert( - "metadata".into(), - json!(if metadata == "all" { - "all" - } else if metadata == "none" { - "none" - } else { - "copyright" - }), - ); - o.insert("convertToSrgb".into(), json!(prefs.convert_to_srgb)); - o.insert("transparency".into(), json!(true)); - o.insert("matte".into(), json!("#ffffff")); - match fmt.as_str() { - "jpg" => { - o.insert("format".into(), json!("jpeg")); - o.insert("quality".into(), json!(prefs.jpeg_quality.min(100))); - } - "gif" => { - o.insert("format".into(), json!("gif")); - } - "webp" => { - // WebP uses the regular exporter. Its optional quality is in the Photoshop-style - // 0–12 save scale: invert that mapping so the codec receives the chosen 1–100. - // Keeping quality absent preserves the historical lossless Quick Export behavior. - let (wdoc, _, _) = web_document(&doc, &json!({}), &WebSettings { convert_to_srgb: prefs.convert_to_srgb, ..Default::default() })?; - let quality = if prefs.webp_lossless { None } else { Some((f64::from(prefs.webp_quality.clamp(1, 100)) - 1.0) * 12.0 / 99.0) }; - let warnings = crate::file_cmds::save_doc(&wdoc, &path, quality)?; - crate::automate_cmds::fire_event(s, "export"); - return Ok(json!({"path": path, "format": fmt, "warnings": warnings})); - } - _ => { - o.insert("format".into(), json!("png24")); - } - } - let q = Value::Object(o); - let st = WebSettings::from_params(&q, cmd)?; - let (wdoc, _, _) = web_document(&doc, &json!({}), &st)?; - let buf = photocraft_compose::flatten(&wdoc); - let xmp = web_xmp(&doc, st.metadata); - let opt = optimize( - &buf.px, - wdoc.size.width as usize, - wdoc.bounds(), - &st, - wdoc.icc_profile.as_deref().map(|v| v.as_slice()), - xmp.as_deref(), - wdoc.resolution_dpi, - false, - )?; - write_file(&path, &opt.bytes)?; - crate::automate_cmds::fire_event(s, "export"); - Ok(json!({"path": path, "format": fmt, "bytes": opt.bytes.len()})) -} - -// ---------- Generate › Image Assets ---------- - -/// One asset requested by a layer name. -#[derive(Clone, Debug, PartialEq)] -pub struct AssetSpec { - /// Relative output path (folders allowed), extension included. - pub file: String, - /// png8 | png24 | png32 | jpg | gif | webp - pub format: String, - /// JPEG quality 1..=100. - pub quality: Option, - /// Uniform scale (1.0 = 100 %). - pub scale: Option, - /// Target width / height in px (`?` = keep aspect). - pub width: Option, - pub height: Option, -} - -/// A `default` layer variant: size plus a folder (`low/`) or name suffix (`@2x`). -#[derive(Clone, Debug, Default, PartialEq)] -pub struct AssetDefault { - pub scale: Option, - pub width: Option, - pub height: Option, - pub folder: String, - pub suffix: String, -} - -fn unit_px(tok: &str, dpi: f64) -> Option> { - if tok == "?" { - return Some(None); - } - let (num, k) = if let Some(n) = tok.strip_suffix("px") { - (n, 1.0) - } else if let Some(n) = tok.strip_suffix("in") { - (n, dpi) - } else if let Some(n) = tok.strip_suffix("cm") { - (n, dpi / 2.54) - } else if let Some(n) = tok.strip_suffix("mm") { - (n, dpi / 25.4) - } else { - (tok, 1.0) - }; - num.trim().parse::().ok().filter(|v| *v > 0.0).map(|v| Some(v * k)) -} - -/// `200%` or `WxH` (units px/in/cm/mm, `?` wildcard) at the start of `item`; returns the size and -/// the rest. -fn parse_size(item: &str, dpi: f64) -> (Option, Option, Option, &str) { - let item = item.trim(); - let (first, rest) = item.split_once(char::is_whitespace).map_or((item, ""), |(a, b)| (a, b.trim())); - if let Some(pc) = first.strip_suffix('%').and_then(|n| n.parse::().ok()).filter(|v| *v > 0.0) { - return (Some(pc / 100.0), None, None, rest); - } - // "100x50", "2in x 1in", "?x100" - if let Some((w, h)) = first.split_once(['x', 'X']).filter(|(w, h)| !w.is_empty() && !h.is_empty()) - && let (Some(w), Some(h)) = (unit_px(w, dpi), unit_px(h, dpi)) - { - return (None, w, h, rest); - } - // Spaced form "2in x 1in name.png": three tokens. - let toks: Vec<&str> = item.split_whitespace().collect(); - if toks.len() >= 4 - && toks[1].eq_ignore_ascii_case("x") - && let (Some(w), Some(h)) = (unit_px(toks[0], dpi), unit_px(toks[2], dpi)) - { - let pos = item.find(toks[3]).unwrap_or(item.len()); - return (None, w, h, &item[pos..]); - } - (None, None, None, item) -} - -/// Parses a layer name into its asset specs (empty when it names no asset). -pub fn parse_asset_name(name: &str, dpi: f64) -> Vec { - let mut out = Vec::new(); - for item in name.split([',', '+']) { - let (scale, width, height, rest) = parse_size(item, dpi); - let file = rest.trim(); - let Some((stem_part, ext)) = file.rsplit_once('.') else { continue }; - if stem_part.is_empty() { - continue; - } - let ext_l = ext.to_ascii_lowercase(); - let (format, quality, out_ext) = if let Some(q) = ext_l.strip_prefix("jpeg").or_else(|| ext_l.strip_prefix("jpg")) { - let q = q.trim(); - let quality = if q.is_empty() { - None - } else if let Some(pc) = q.strip_suffix('%') { - pc.parse::().ok().map(|v| v.clamp(1, 100) as u8) - } else { - // 1–10 scale. - q.parse::().ok().map(|v| (v.clamp(1, 10) * 10) as u8) - }; - if !q.is_empty() && quality.is_none() { - continue; - } - ("jpg".to_string(), quality, if ext_l.starts_with("jpeg") { "jpeg" } else { "jpg" }) - } else { - match ext_l.as_str() { - "png" | "png32" => ("png32".to_string(), None, "png"), - "png24" => ("png24".to_string(), None, "png"), - "png8" => ("png8".to_string(), None, "png"), - "gif" => ("gif".to_string(), None, "gif"), - "webp" => ("webp".to_string(), None, "webp"), - _ => continue, - } - }; - out.push(AssetSpec { file: format!("{stem_part}.{out_ext}"), format, quality, scale, width, height }); - } - out -} - -/// Parses a `default …` layer name into its variants. -pub fn parse_defaults(name: &str, dpi: f64) -> Option> { - let rest = name.trim().strip_prefix("default")?; - if !rest.is_empty() && !rest.starts_with(char::is_whitespace) { - return None; - } - let mut v = Vec::new(); - for item in rest.split([',', '+']) { - let (scale, width, height, tail) = parse_size(item, dpi); - let tail = tail.trim(); - let (folder, suffix) = match tail.rfind('/') { - Some(i) => (tail[..=i].to_string(), tail[i + 1..].to_string()), - None => (String::new(), tail.to_string()), - }; - if scale.is_none() && width.is_none() && height.is_none() && folder.is_empty() && suffix.is_empty() { - continue; - } - v.push(AssetDefault { scale, width, height, folder, suffix }); - } - Some(v) -} - -fn asset_settings(spec: &AssetSpec) -> WebSettings { - let base = WebSettings { transparency: true, matte: None, metadata: WebMetadata::None, ..WebSettings::default() }; - match spec.format.as_str() { - "jpg" => WebSettings { format: WebFormat::Jpeg, quality: spec.quality.unwrap_or(90), matte: Some([1.0, 1.0, 1.0]), ..base }, - "png8" => WebSettings { format: WebFormat::Png8, dither: Dither::None, ..base }, - "png24" => WebSettings { format: WebFormat::Png24, transparency: false, matte: Some([1.0, 1.0, 1.0]), ..base }, - "gif" => WebSettings { format: WebFormat::Gif, dither: Dither::None, ..base }, - _ => WebSettings { format: WebFormat::Png24, ..base }, - } -} - -fn is_windows_device_asset_name(component: &str) -> bool { - let stem = component.split('.').next().unwrap_or(component).trim_end_matches(' ').to_ascii_uppercase(); - matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$") - || stem.strip_prefix("COM").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) - || stem.strip_prefix("LPT").is_some_and(|n| matches!(n, "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")) -} - -/// Layer names and `default` folders are document data, so reject unsafe output components. -fn asset_path_components(relative: &str) -> Result> { - // Split both separator styles even on Unix, where a backslash would otherwise be a legal - // filename. This also catches Windows drive, rooted, and UNC paths on every platform. - let mut parts = Vec::new(); - for part in relative.split(['/', '\\']) { - if part.is_empty() - || part == "." - || part == ".." - || part.ends_with('.') - || part.ends_with(' ') - || part.contains(':') - || part.contains('\0') - || is_windows_device_asset_name(part) - { - return Err(other(format!("asset path {relative:?} has an unsafe component {part:?}"))); - } - parts.push(part); - } - Ok(parts) -} - -/// Resolve a generated asset below the selected output directory. The atomic writer follows an -/// existing file symlink, so reject it and directory symlinks before writing. -fn asset_output_path(dir: &str, relative: &str) -> Result<(String, String)> { - if dir.is_empty() { - return Err(other("image assets need an output directory")); - } - let mut parts = asset_path_components(relative)?; - // Keep the caller's spelling in the result (for example /var on macOS), while writes use - // the canonical directory so a symlink in the selected root cannot move the destination. - let mut reported = std::path::PathBuf::from(dir); - for part in &parts { - reported.push(part); - } - let file = parts.pop().ok_or_else(|| other(format!("asset path {relative:?} is empty")))?; - let chosen_root: std::path::PathBuf = std::path::Path::new(dir).components().collect(); - std::fs::create_dir_all(&chosen_root).map_err(|e| other(format!("asset output directory {dir:?}: {e}")))?; - // The caller chose this root, so a symlink to an existing directory is intentional. All - // document-derived components below it are checked against its canonical target. - let root = std::fs::canonicalize(&chosen_root).map_err(|e| other(format!("asset output directory {dir:?}: {e}")))?; - let mut parent = root.clone(); - for part in parts { - parent.push(part); - match std::fs::symlink_metadata(&parent) { - Ok(meta) if meta.file_type().is_symlink() => { - return Err(other(format!("asset path {relative:?} crosses a symlink at {}", parent.display()))); - } - Ok(meta) if !meta.is_dir() => { - return Err(other(format!("asset path {relative:?} has a non-directory at {}", parent.display()))); - } - Ok(_) => {} - Err(e) if e.kind() == std::io::ErrorKind::NotFound => { - std::fs::create_dir(&parent).map_err(|e| other(format!("asset folder {}: {e}", parent.display())))?; - } - Err(e) => return Err(other(format!("asset folder {}: {e}", parent.display()))), - } - let actual = std::fs::canonicalize(&parent).map_err(|e| other(format!("asset folder {}: {e}", parent.display())))?; - if !actual.starts_with(&root) { - return Err(other(format!("asset path {relative:?} escapes output directory {dir:?}"))); - } - parent = actual; - } - let parent = std::fs::canonicalize(&parent).map_err(|e| other(format!("asset folder {}: {e}", parent.display())))?; - if !parent.starts_with(&root) { - return Err(other(format!("asset path {relative:?} escapes output directory {dir:?}"))); - } - let output = parent.join(file); - match std::fs::symlink_metadata(&output) { - Ok(meta) if meta.file_type().is_symlink() => { - return Err(other(format!("asset path {relative:?} points to a symlink"))); - } - Ok(meta) if !meta.is_file() => { - return Err(other(format!("asset path {relative:?} is not a file"))); - } - Ok(_) => {} - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => return Err(other(format!("asset path {relative:?}: {e}"))), - } - Ok((output.to_string_lossy().into_owned(), reported.to_string_lossy().into_owned())) -} - -/// Exports every asset named by `doc`'s layers into `dir`. Returns (files, errors). -pub fn generate_assets(doc: &Document, dir: &str) -> (Vec, Vec) { - let dpi = f64::from(doc.resolution_dpi.max(1.0)); - let mut defaults: Option> = None; - let mut jobs: Vec<(LayerId, AssetSpec)> = Vec::new(); - for (_, _, l) in doc.walk() { - if let Some(d) = parse_defaults(&l.name, dpi) { - defaults = Some(d); - continue; - } - if matches!(l.content, LayerContent::Adjustment(_)) { - continue; - } - for spec in parse_asset_name(&l.name, dpi) { - jobs.push((l.id, spec)); - } - } - let variants: Vec = match defaults { - Some(d) if !d.is_empty() => d, - _ => vec![AssetDefault::default()], - }; - let mut files = Vec::new(); - let mut errors = Vec::new(); - for (id, spec) in &jobs { - for v in &variants { - let r = (|| -> Result { - let (name_stem, ext) = spec.file.rsplit_once('.').unwrap_or((spec.file.as_str(), "png")); - let rel = format!("{}{}{}.{ext}", v.folder, name_stem, v.suffix); - asset_path_components(&rel)?; - let mut one = crate::layer_menu_cmds::layer_document(doc, *id)?; - one.metadata = Default::default(); - let (w0, h0) = (f64::from(one.size.width), f64::from(one.size.height)); - // The layer's own size spec wins; default variants scale on top of it. - let mut k = spec.scale.unwrap_or(1.0); - let (mut tw, mut th) = (w0 * k, h0 * k); - match (spec.width, spec.height) { - (Some(w), Some(h)) => (tw, th) = (w, h), - (Some(w), None) => (tw, th) = (w, h0 * w / w0), - (None, Some(h)) => (tw, th) = (w0 * h / h0, h), - _ => {} - } - k = v.scale.unwrap_or(1.0); - tw *= k; - th *= k; - match (v.width, v.height) { - (Some(w), Some(h)) => (tw, th) = (w, h), - (Some(w), None) => (tw, th) = (w, th * w / tw), - (None, Some(h)) => (tw, th) = (tw * h / th, h), - _ => {} - } - let st = asset_settings(spec); - let p = json!({"width": tw.round().max(1.0), "height": th.round().max(1.0)}); - let (wdoc, _, _) = web_document(&one, &p, &WebSettings { convert_to_srgb: true, ..st.clone() })?; - if spec.format == "webp" { - let (out, reported) = asset_output_path(dir, &rel)?; - crate::file_cmds::save_doc(&wdoc, &out, None)?; - return Ok(reported); - } - let buf = photocraft_compose::flatten(&wdoc); - let o = optimize(&buf.px, wdoc.size.width as usize, wdoc.bounds(), &st, None, None, wdoc.resolution_dpi, false)?; - let (out, reported) = asset_output_path(dir, &rel)?; - write_file(&out, &o.bytes)?; - Ok(reported) - })(); - match r { - Ok(f) => files.push(f), - Err(e) => errors.push(json!({"asset": spec.file, "error": e.to_string()})), - } - } - } - (files, errors) -} - -/// `/-assets`. -pub fn assets_dir(doc_path: &str) -> String { - let p = std::path::Path::new(doc_path); - let dir = p.parent().map(|d| d.to_string_lossy().into_owned()).unwrap_or_default(); - join(&dir, &format!("{}-assets", stem(doc_path))) -} - -fn image_assets(s: &mut Session, p: &Value) -> Result { - let d = s.active().ok_or(EngineError::NoDocument)?; - let id: DocId = d.doc.id; - let was = s.file_menu.image_assets.contains(&id); - let on = p.get("on").and_then(Value::as_bool).unwrap_or(!was); - if on { - if !was { - s.file_menu.image_assets.push(id); - } - } else { - s.file_menu.image_assets.retain(|x| *x != id); - } - let d = s.active().ok_or(EngineError::NoDocument)?; - let dir = p.get("dir").and_then(Value::as_str).map(str::to_string).or_else(|| d.path.as_deref().map(assets_dir)); - if !on { - return Ok(json!({"enabled": false})); - } - match dir { - Some(dir) if !cfg!(target_arch = "wasm32") => { - let (files, errors) = generate_assets(&d.doc, &dir); - Ok(json!({"enabled": true, "dir": dir, "files": files, "errors": errors})) - } - _ => Ok(json!({"enabled": true, "dir": null, "files": [], "note": "assets are written when the document is saved"})), - } -} - -/// After a save: regenerate the document's image assets when Generate › Image Assets is on. -pub fn on_saved(s: &Session, index: usize) -> Option { - let d = s.documents().get(index)?; - if !s.file_menu.image_assets.contains(&d.doc.id) || cfg!(target_arch = "wasm32") { - return None; - } - let dir = assets_dir(d.path.as_deref()?); - let (files, errors) = generate_assets(&d.doc, &dir); - Some(json!({"dir": dir, "files": files, "errors": errors})) -} - -pub fn specs() -> Vec { - macro_rules! spec { - ($id:expr, $label:expr, $menu:expr, $sc:expr, $params:expr, $enabled:expr, $run:expr) => { - CommandSpec { id: $id, label: $label, menu: $menu, shortcut: $sc, params: $params, enabled: $enabled, journal: true, run: $run } - }; - } - fn has_doc(s: &Session) -> std::result::Result<(), String> { - s.active().map(|_| ()).ok_or_else(|| "no document open".into()) - } - vec![ - spec!( - "file.export.saveForWebLegacy", - "Save for Web (Legacy)…", - &["File", "Export"], - Some("Cmd+Alt+Shift+S"), - r##"{"preset":"GIF 128 Dithered|JPEG High|PNG-24|…"?,"format":"gif|png8|png24|jpeg|wbmp"="png24","palette":"perceptual|selective|adaptive|restrictive|exact|systemMac|systemWindows|uniform"="selective","colors":2..256=256,"colorTable":["#rrggbb",…]? (fixed opaque source palette, 1–256 entries; 255 with transparency),"colorTableEdits":{"0":"#rrggbb",…}? (replace entries after indexing; requires colorTable),"dither":"none|diffusion|pattern|noise"="diffusion","ditherAmount":0..100=88,"transparency":bool=true,"matte":"#rrggbb|none"="none" (a preset's own: white; formats without transparency go over white),"interlaced":bool=false,"webSnap":0..100=0,"quality":0..100=60,"progressive":bool=false,"optimized":bool=true,"embedIcc":bool=false,"metadata":"none|copyright|copyrightAndContact|all"="copyright","convertToSrgb":bool=true,"width"|"height"|"percent"? (image size),"resample":"bicubic|bilinear|nearest"?,"path":file? (whole image),"dir":folder? (one file per slice in images/),"imagesFolder":relative-folder?="images","html":bool=false,"trustedSliceHtml":bool=false (only for trusted documents; emit raw no-image cell HTML),"slices":"all|user"="all","numbers":[n]?} → no path/dir: {bytes,width,height,colors,colorTable,transparentIndex} estimate; else {files, html, bytes}"##, - has_doc, - save_for_web - ), - spec!( - "file.export.exportPreferences", - "Export Preferences…", - &["File", "Export"], - None, - r##"{"quickExportFormat":"png|jpg|gif|webp"?,"quickExportLocation":"ask|sameFolder"?,"jpegQuality":1..100?,"webpLossless":bool?,"webpQuality":1..100?,"metadata":"none|copyright|all"?,"convertToSrgb":bool?} → {values}"##, - |_| Ok(()), - export_preferences - ), - spec!( - "file.export.quickExport", - "Quick Export", - &[], - None, - r##"{"path":str? (required unless Export Preferences › Location is "sameFolder" and the document is saved)} → {path, format, bytes}"##, - native_doc, - quick_export - ), - spec!( - "file.generate.imageAssets", - "Image Assets", - &["File", "Generate"], - None, - r##"{"on":bool? (default: toggle),"dir":folder? (default -assets next to the file)} → {enabled, files, errors}; layers named like "foo.png", "200% foo@2x.png", "48x48 icons/a.png8", "photo.jpg80%" are exported, now and after each save"##, - has_doc, - image_assets - ), - ] -} - -#[cfg(test)] -#[path = "web_cmds/tests.rs"] -mod tests; diff --git a/crates/engine/tests/cmyk_flatten_profile.rs b/crates/engine/tests/cmyk_flatten_profile.rs new file mode 100644 index 0000000000..28b3bf9afc --- /dev/null +++ b/crates/engine/tests/cmyk_flatten_profile.rs @@ -0,0 +1,176 @@ +//! Flatten Image, Stamp Visible, Background from Layer, Merge Down and Merge Visible keep the +//! inks of a CMYK document tagged with a profile other than the built-in one (#3778). +//! +//! A document whose composite is one plain pixel layer keeps its inks exactly, as in Photoshop +//! (black generation and inks outside the display gamut included). Merges of several layers are +//! composited in RGB and separated back through the profile the layers were read with, so they +//! keep the inks up to that round trip's residual instead of moving every pixel. + +use std::sync::Arc; + +use photocraft_color::convert::{CmykSpace, with_cmyk_space}; +use photocraft_engine::Session; +use photocraft_geom::Rect; +use serde_json::json; + +const W: i32 = 8; +const H: i32 = 2; + +/// RGB colours whose separations make the first row: mid tones, saturated colours, near-black. +const RGB: [[f32; 3]; 8] = + [[0.8, 0.3, 0.2], [0.1, 0.5, 0.9], [0.5, 0.5, 0.5], [0.08, 0.06, 0.05], [0.95, 0.9, 0.85], [0.9, 0.8, 0.1], [0.2, 0.7, 0.3], [0.6, 0.2, 0.6]]; + +/// Inks no separation produces, for the second row: K only, rich black, solid cyan (outside +/// sRGB), paper, 50 % K, a heavy four-colour mix, and two odd mixes. +const INKS: [[f32; 4]; 8] = [ + [0.0, 0.0, 0.0, 1.0], + [0.75, 0.68, 0.67, 0.9], + [1.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 0.5], + [0.9, 0.9, 0.9, 0.9], + [0.886, 0.329, 0.0, 0.0], + [0.1, 0.0, 0.6, 0.3], +]; + +fn profile() -> Arc> { + photocraft_cms::synth::cmyk_profile(&photocraft_cms::synth::CmykParams { + description: "Test Uncoated".into(), + tvi: [0.26, 0.26, 0.26, 0.3], + ..Default::default() + }) + .to_bytes() +} + +/// A one-layer (Background) CMYK document, tagged with `profile()` when `tag`: the first row is +/// separated from [`RGB`] through that profile (a separation read back and separated again a few +/// times, so the profile reproduces it), the second row holds [`INKS`]. +fn document(depth: u32, tag: bool) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": W, "height": H, "mode": "cmyk", "depth": depth, "background": "white"})).unwrap(); + let icc = profile(); + let space = CmykSpace::for_profile(Some(&icc)); + assert!(space.is_some(), "the fixture is not the built-in profile"); + s.edit("tag", |doc, _| { + if tag { + doc.icc_profile = Some(icc.clone()); + } + let bg = doc.layers[0].surface_mut().unwrap(); + let fmt = bg.format(); + with_cmyk_space(if tag { space.as_ref() } else { None }, || { + for (x, rgb) in RGB.iter().enumerate() { + let mut px = photocraft_raster::from_rgba(&fmt, [rgb[0], rgb[1], rgb[2], 1.0]); + for _ in 0..3 { + let back = photocraft_raster::to_rgba(&fmt, &px); + px = photocraft_raster::from_rgba(&fmt, back); + } + bg.write_pixel(x as i32, 0, &px); + } + }); + for (x, ink) in INKS.iter().enumerate() { + bg.write_pixel(x as i32, 1, &[ink[0], ink[1], ink[2], ink[3], 1.0]); + } + Ok(()) + }) + .unwrap(); + s +} + +/// The four ink values of every pixel of the active layer. +fn inks(s: &Session) -> Vec<[f32; 4]> { + let st = s.active().unwrap(); + let surf = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap(); + let n = surf.channels(); + surf.read_region(Rect::new(0, 0, W, H)).chunks_exact(n).map(|p| [p[0], p[1], p[2], p[3]]).collect() +} + +fn max_diff(a: &[[f32; 4]], b: &[[f32; 4]]) -> f32 { + a.iter().zip(b).flat_map(|(p, q)| (0..4).map(move |c| (p[c] - q[c]).abs())).fold(0.0, f32::max) +} + +/// Runs `run` on a document and returns how far the inks of `row` (0 = separations, 1 = any +/// inks) moved. +fn drift(depth: u32, tag: bool, row: usize, run: impl FnOnce(&mut Session)) -> f32 { + let mut s = document(depth, tag); + let before = inks(&s); + run(&mut s); + let after = inks(&s); + let r = row * W as usize..(row + 1) * W as usize; + max_diff(&before[r.clone()], &after[r]) +} + +fn exact(what: &str, run: impl Fn(&mut Session)) { + for depth in [8, 16] { + for tag in [true, false] { + for row in [0, 1] { + let d = drift(depth, tag, row, &run); + assert!(d == 0.0, "{depth}-bit {what} (tagged: {tag}, row {row}): inks moved by {d}"); + } + } + } +} + +#[test] +fn flatten_keeps_the_inks_of_a_single_layer_exactly() { + exact("Flatten Image", |s| { + s.execute("layer.flattenImage", json!({})).unwrap(); + }); +} + +#[test] +fn stamp_visible_keeps_the_inks_of_a_single_layer_exactly() { + exact("Stamp Visible", |s| { + s.execute("layer.stampVisible", json!({})).unwrap(); + }); +} + +#[test] +fn background_from_layer_keeps_the_inks_exactly() { + exact("Background from Layer", |s| { + s.execute("layer.new.layerFromBackground", json!({})).unwrap(); + s.execute("layer.new.backgroundFromLayer", json!({})).unwrap(); + }); +} + +#[test] +fn flatten_of_a_partly_transparent_layer_still_composites_over_white() { + for depth in [8, 16] { + let mut s = document(depth, true); + s.edit("hole", |doc, _| { + doc.layers[0].surface_mut().unwrap().write_pixel(0, 0, &[0.0, 0.0, 0.0, 1.0, 0.0]); + Ok(()) + }) + .unwrap(); + s.execute("layer.flattenImage", json!({})).unwrap(); + let px = inks(&s)[0]; + // Transparent over white paper: no ink, not the hidden K. + assert!(px.iter().all(|v| *v < 0.02), "{depth}-bit: {px:?}"); + } +} + +/// Merges of several layers go through the RGB composite: separations the profile reproduces +/// come back within this (the profile's own RGB round trip, a few levels), where reading with +/// the document's profile and separating with the built-in one moved them by tens of levels. +const ROUND_TRIP: f32 = 10.0 / 255.0; + +#[test] +fn merge_down_separates_through_the_document_profile() { + for depth in [8, 16] { + let d = drift(depth, true, 0, |s| { + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.mergeDown", json!({})).unwrap(); + }); + assert!(d <= ROUND_TRIP, "{depth}-bit Merge Down: inks moved by {d}"); + } +} + +#[test] +fn merge_visible_separates_through_the_document_profile() { + for depth in [8, 16] { + let d = drift(depth, true, 0, |s| { + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.mergeVisible", json!({})).unwrap(); + }); + assert!(d <= ROUND_TRIP, "{depth}-bit Merge Visible: inks moved by {d}"); + } +} diff --git a/crates/engine/tests/affinity.rs b/crates/engine/tests/it/affinity.rs similarity index 97% rename from crates/engine/tests/affinity.rs rename to crates/engine/tests/it/affinity.rs index 2d56fbf986..06c4bf4e66 100644 --- a/crates/engine/tests/affinity.rs +++ b/crates/engine/tests/it/affinity.rs @@ -12,7 +12,10 @@ use serde_json::json; fn fixture() -> Vec { let mut src = Session::new(); src.execute("file.new", json!({"width": 2, "height": 1, "background": "#dc283c"})).unwrap(); - let png = photocraft_io::export(&src.active().unwrap().doc, "preview.png", &Default::default()).unwrap().bytes; + // Affinity's previews carry no colour profile. + let mut opts = photocraft_io::ExportOptions::default(); + opts.encode.embed_icc = false; + let png = photocraft_io::export(&src.active().unwrap().doc, "preview.png", &opts).unwrap().bytes; // Observed v12 preview envelope, without a native object graph. let mut bytes = vec![0; 72]; bytes[..4].copy_from_slice(b"\x00\xffKA"); diff --git a/crates/engine/tests/artboard_export_names.rs b/crates/engine/tests/it/artboard_export_names.rs similarity index 100% rename from crates/engine/tests/artboard_export_names.rs rename to crates/engine/tests/it/artboard_export_names.rs diff --git a/crates/engine/tests/it/atn_crystallize_cell_size.rs b/crates/engine/tests/it/atn_crystallize_cell_size.rs new file mode 100644 index 0000000000..7b72a6ff70 --- /dev/null +++ b/crates/engine/tests/it/atn_crystallize_cell_size.rs @@ -0,0 +1,151 @@ +//! #7669: strict ATN export must not replace a native fractional Crystallize cell size. +use photocraft_engine::actions_cmds::Action; +use photocraft_engine::{Session, atn_cmds}; +use photocraft_psd::{atn, descriptor::Value as D}; +use serde_json::{Value, json}; + +const CRYSTALLIZE: &str = "filter.pixelate.crystallize"; + +/// Procedural colours expose cell-boundary changes without relying on external images. +fn fixture(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 24, "height": 18, "depth": depth})).unwrap(); + s.edit("procedural fixture", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let format = surface.format(); + let bytes: Vec<_> = + (0..432).flat_map(|i| photocraft_raster::from_rgba(&format, [(i % 11) as f32 / 10.0, (i % 7) as f32 / 6.0, (i % 5) as f32 / 4.0, 1.0])).collect(); + surface.write_region(photocraft_doc::Rect::from_xywh(0, 0, 24, 18), &bytes); + Ok(()) + }) + .unwrap(); + s +} + +/// Compare the actual flattened document rather than just the imported parameters. +fn pixels(s: &Session) -> Vec<[f32; 4]> { + photocraft_compose::flatten(&s.active().unwrap().doc).px +} + +/// Fractions remain valid native edits; strict integer ATN conversion must refuse them. +#[test] +fn fractional_cell_sizes_remain_native_and_are_refused_by_strict_export() { + let mut silently_exported = Vec::new(); + for depth in [8, 16, 32] { + for cell_size in [3.1, 5.4, 5.5, 5.6, 10.25, 299.9] { + let params = json!({"cellSize": cell_size}); + let action = Action::new("Fractional Crystallize", vec![(CRYSTALLIZE.into(), params.clone())]); + let mut direct = fixture(depth); + direct.execute(CRYSTALLIZE, params).unwrap(); + let expected = pixels(&direct); + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": "Fractional Crystallize"})).unwrap(); + assert_eq!(pixels(&native), expected); + + if cell_size == 5.4 { + let mut rounded = fixture(depth); + rounded.execute(CRYSTALLIZE, json!({"cellSize": 5})).unwrap(); + assert_ne!(pixels(&rounded), expected, "fixture must expose 5.4 vs 5: {depth}"); + } + if let Ok((bytes, skipped)) = atn_cmds::export("Crystallize", &[&action], &[], false) { + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(skipped.is_empty() && imported.unsupported.is_empty() && imported.warnings.is_empty()); + let exported = imported.actions[0].steps[0].1["cellSize"].clone(); + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Fractional Crystallize"})).unwrap(); + let changed = pixels(&replay) != expected; + if cell_size == 5.4 { + assert!(changed, "baseline must expose the reported pixel loss: {depth}"); + } + silently_exported.push(format!("depth {depth}: {cell_size} -> {exported}, pixels changed: {changed}")); + } + } + } + assert!(silently_exported.is_empty(), "strict export silently accepted fractional cell sizes:\n{}", silently_exported.join("\n")); +} + +/// Integer sizes, bounds and omitted defaults retain their binary descriptor and playback. +#[test] +fn integer_cell_sizes_and_defaults_preserve_playback() { + for depth in [8, 16, 32] { + let mut cases = vec![Value::Null, json!({}), json!({"target": "pixels"}), json!({"cellSize": 5.0})]; + cases.extend([3, 5, 10, 100, 300].map(|cell_size| json!({"cellSize": cell_size}))); + for params in cases { + let action = Action::new("Integer Crystallize", vec![(CRYSTALLIZE.into(), params.clone())]); + let mut direct = fixture(depth); + direct.execute(CRYSTALLIZE, params.clone()).unwrap(); + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": "Integer Crystallize"})).unwrap(); + assert_eq!(pixels(&native), pixels(&direct)); + + let (bytes, skipped) = atn_cmds::export("Crystallize", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let file = atn::parse(&bytes).unwrap(); + let descriptor = file.actions[0].items[0].descriptor.as_ref().unwrap(); + if let Some(cell_size) = params.get("cellSize").and_then(Value::as_f64) { + assert!(matches!(descriptor.get("ClSz"), Some(D::Integer(n)) if f64::from(*n) == cell_size)); + } + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty() && imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + assert_eq!(imported.actions[0].steps.len(), 1); + assert_eq!(imported.actions[0].steps[0].0, CRYSTALLIZE); + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Integer Crystallize"})).unwrap(); + assert_eq!(pixels(&replay), pixels(&direct), "depth {depth}: {params}"); + } + } +} + +/// Skipping is explicit and reports exactly which edits were lost, preserving supported order. +#[test] +fn skipping_fractional_cell_sizes_reports_action_and_step() { + let action = + Action::new("Mixed Crystallize", [5.0, 5.4, 10.0, 10.25].into_iter().map(|cell_size| (CRYSTALLIZE.into(), json!({"cellSize": cell_size}))).collect()); + let error = atn_cmds::export("Crystallize", &[&action], &[], false).unwrap_err(); + assert!(error.contains("step 2 of `Mixed Crystallize`"), "{error}"); + assert!(error.contains(CRYSTALLIZE) && error.contains("skipUnsupported"), "{error}"); + let (bytes, skipped) = atn_cmds::export("Crystallize", &[&action], &[], true).unwrap(); + assert_eq!( + skipped, + vec![ + atn_cmds::Skipped { action: "Mixed Crystallize".into(), step: 1, id: CRYSTALLIZE.into() }, + atn_cmds::Skipped { action: "Mixed Crystallize".into(), step: 3, id: CRYSTALLIZE.into() }, + ] + ); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty() && imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + let steps = &imported.actions[0].steps; + assert_eq!(steps.len(), 2); + for (step, cell_size) in steps.iter().zip([5.0, 10.0]) { + assert_eq!(step.0, CRYSTALLIZE); + assert_eq!(step.1["cellSize"].as_f64(), Some(cell_size)); + } +} + +/// The plain event still cannot carry mask/channel targets or nonnumeric cell sizes. +#[test] +fn non_pixel_targets_and_malformed_fields_are_still_refused() { + for params in [ + json!({"cellSize": 5, "target": "mask"}), + json!({"cellSize": 5, "target": "quickMask"}), + json!({"cellSize": 5, "target": "channel"}), + json!({"cellSize": 5, "target": null}), + json!({"cellSize": null}), + json!({"cellSize": "5.4"}), + json!({"cellSize": 2147483648_i64}), + json!({"cellSize": -2147483649_i64}), + json!({"cellSize": 1e100}), + ] { + let action = Action::new("Unsupported Crystallize", vec![(CRYSTALLIZE.into(), params.clone())]); + assert!(atn_cmds::export("Crystallize", &[&action], &[], false).is_err(), "{params}"); + let (bytes, skipped) = atn_cmds::export("Crystallize", &[&action], &[], true).unwrap(); + assert_eq!(skipped, vec![atn_cmds::Skipped { action: "Unsupported Crystallize".into(), step: 0, id: CRYSTALLIZE.into() }]); + assert!(atn_cmds::import(&bytes).unwrap().actions[0].steps.is_empty()); + } +} diff --git a/crates/engine/tests/it/atn_ripple_amount.rs b/crates/engine/tests/it/atn_ripple_amount.rs new file mode 100644 index 0000000000..b6bde88f7a --- /dev/null +++ b/crates/engine/tests/it/atn_ripple_amount.rs @@ -0,0 +1,158 @@ +//! #7670: integer ATN Ripple amounts must never silently replace native fractions. +use photocraft_engine::actions_cmds::Action; +use photocraft_engine::{Session, atn_cmds}; +use photocraft_psd::{atn, descriptor::Value as D}; +use serde_json::{Value, json}; + +const RIPPLE: &str = "filter.distort.ripple"; + +/// Build a deterministic RGB image at the requested depth that exposes Ripple rounding. +fn fixture(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 24, "height": 18, "depth": depth})).unwrap(); + s.edit("procedural fixture", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let format = surface.format(); + let bytes: Vec<_> = + (0..432).flat_map(|i| photocraft_raster::from_rgba(&format, [(i % 11) as f32 / 10.0, (i % 7) as f32 / 6.0, (i % 5) as f32 / 4.0, 1.0])).collect(); + surface.write_region(photocraft_doc::Rect::from_xywh(0, 0, 24, 18), &bytes); + Ok(()) + }) + .unwrap(); + s +} + +/// Flatten the active document to compare direct, native and ATN playback pixels. +fn pixels(s: &Session) -> Vec<[f32; 4]> { + photocraft_compose::flatten(&s.active().unwrap().doc).px +} + +/// Fractions are valid native edits, but the existing integer event cannot carry them. +#[test] +fn fractional_amounts_remain_native_and_are_refused_by_strict_export() { + let mut silently_exported = Vec::new(); + for depth in [8, 16, 32] { + for size in ["small", "medium", "large"] { + for amount in [81.4, -81.4, 0.5, -0.5, 998.9, -998.9] { + let params = json!({"amount": amount, "size": size}); + let action = Action::new("Fractional Ripple", vec![(RIPPLE.into(), params.clone())]); + let mut direct = fixture(depth); + direct.execute(RIPPLE, params).unwrap(); + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": "Fractional Ripple"})).unwrap(); + assert_eq!(pixels(&direct), pixels(&native)); + + if amount == 81.4 { + let mut rounded = fixture(depth); + rounded.execute(RIPPLE, json!({"amount": 81, "size": size})).unwrap(); + assert_ne!(pixels(&direct), pixels(&rounded), "fixture must expose rounding: {depth}, {size}"); + } + if let Ok((bytes, skipped)) = atn_cmds::export("Ripple", &[&action], &[], false) { + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(skipped.is_empty() && imported.unsupported.is_empty() && imported.warnings.is_empty()); + let exported = imported.actions[0].steps[0].1["amount"].clone(); + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Fractional Ripple"})).unwrap(); + let changed = pixels(&replay) != pixels(&direct); + if amount == 81.4 { + assert!(changed, "baseline must expose the reported pixel loss: {depth}, {size}"); + } + silently_exported.push(format!("depth {depth}, {size}: {amount} -> {exported}, pixels changed: {changed}")); + } + } + } + } + assert!(silently_exported.is_empty(), "strict export silently accepted fractional amounts:\n{}", silently_exported.join("\n")); +} + +/// Whole amounts and omitted defaults still round-trip through binary ATN and actual pixels. +#[test] +fn integer_amounts_sizes_and_defaults_preserve_playback() { + for depth in [8, 16, 32] { + let mut cases = vec![Value::Null, json!({}), json!({"target": "pixels"})]; + for size in ["small", "medium", "large"] { + for amount in [-999, -81, 0, 81, 999] { + cases.push(json!({"amount": amount, "size": size})); + } + } + for params in cases { + let action = Action::new("Integer Ripple", vec![(RIPPLE.into(), params.clone())]); + let mut direct = fixture(depth); + direct.execute(RIPPLE, params.clone()).unwrap(); + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": "Integer Ripple"})).unwrap(); + assert_eq!(pixels(&native), pixels(&direct)); + + let (bytes, skipped) = atn_cmds::export("Ripple", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let file = atn::parse(&bytes).unwrap(); + let descriptor = file.actions[0].items[0].descriptor.as_ref().unwrap(); + if let Some(amount) = params.get("amount").and_then(Value::as_i64) { + assert!(matches!(descriptor.get("Amnt"), Some(D::Integer(n)) if i64::from(*n) == amount)); + } + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty() && imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + assert_eq!(imported.actions[0].steps.len(), 1); + assert_eq!(imported.actions[0].steps[0].0, RIPPLE); + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Integer Ripple"})).unwrap(); + assert_eq!(pixels(&replay), pixels(&direct), "depth {depth}: {params}"); + } + } +} + +/// Opt-in skipping reports precisely the lost edits, keeping the supported steps in order. +#[test] +fn skipping_fractional_amounts_reports_action_and_step() { + let action = + Action::new("Mixed Ripple", [10.0, 81.4, -20.0, -0.5].into_iter().map(|amount| (RIPPLE.into(), json!({"amount": amount, "size": "small"}))).collect()); + let error = atn_cmds::export("Ripple", &[&action], &[], false).unwrap_err(); + assert!(error.contains("step 2 of `Mixed Ripple`"), "{error}"); + assert!(error.contains(RIPPLE) && error.contains("skipUnsupported"), "{error}"); + let (bytes, skipped) = atn_cmds::export("Ripple", &[&action], &[], true).unwrap(); + assert_eq!( + skipped, + vec![ + atn_cmds::Skipped { action: "Mixed Ripple".into(), step: 1, id: RIPPLE.into() }, + atn_cmds::Skipped { action: "Mixed Ripple".into(), step: 3, id: RIPPLE.into() }, + ] + ); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty() && imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + let steps = &imported.actions[0].steps; + assert_eq!(steps.len(), 2); + for (step, amount) in steps.iter().zip([10.0, -20.0]) { + assert_eq!(step.0, RIPPLE); + assert_eq!(step.1["amount"].as_f64(), Some(amount)); + assert_eq!(step.1["size"], "small"); + } +} + +/// A dedicated Ripple mapping must retain the plain-event target restriction. +#[test] +fn non_pixel_targets_and_malformed_fields_are_still_refused() { + for params in [ + json!({"amount": 81, "target": "mask"}), + json!({"amount": 81, "target": "quickMask"}), + json!({"amount": 81, "target": "channel"}), + json!({"amount": 81, "target": null}), + json!({"amount": null}), + json!({"amount": "81.4"}), + json!({"amount": 2147483648_i64}), + json!({"amount": -2147483649_i64}), + json!({"amount": 1e100}), + json!({"amount": 81, "size": "unknown"}), + ] { + let action = Action::new("Unsupported Ripple", vec![(RIPPLE.into(), params.clone())]); + assert!(atn_cmds::export("Ripple", &[&action], &[], false).is_err(), "{params}"); + let (bytes, skipped) = atn_cmds::export("Ripple", &[&action], &[], true).unwrap(); + assert_eq!(skipped, vec![atn_cmds::Skipped { action: "Unsupported Ripple".into(), step: 0, id: RIPPLE.into() }]); + assert!(atn_cmds::import(&bytes).unwrap().actions[0].steps.is_empty()); + } +} diff --git a/crates/engine/tests/it/atn_selective_color.rs b/crates/engine/tests/it/atn_selective_color.rs new file mode 100644 index 0000000000..454846f59e --- /dev/null +++ b/crates/engine/tests/it/atn_selective_color.rs @@ -0,0 +1,147 @@ +//! #7672: strict ATN export must preserve both accepted Selective Color parameter forms. +use photocraft_engine::actions_cmds::Action; +use photocraft_engine::{Session, atn_cmds}; +use serde_json::{Value, json}; + +const RANGES: [&str; 9] = ["reds", "yellows", "greens", "cyans", "blues", "magentas", "whites", "neutrals", "blacks"]; +const COMMANDS: [&str; 2] = ["image.adjustments.selectiveColor", "layer.newAdjustmentLayer.selectiveColor"]; + +fn fixture(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 10, "height": 2, "depth": depth})).unwrap(); + let palette = [ + [0.85, 0.10, 0.05, 1.0], + [0.85, 0.80, 0.05, 1.0], + [0.05, 0.85, 0.10, 1.0], + [0.05, 0.80, 0.85, 1.0], + [0.10, 0.05, 0.85, 1.0], + [0.85, 0.05, 0.80, 1.0], + [0.90, 0.93, 0.95, 1.0], + [0.45, 0.50, 0.55, 1.0], + [0.05, 0.08, 0.10, 1.0], + [0.65, 0.20, 0.70, 0.4], + ]; + s.edit("synthetic colours", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let format = surface.format(); + let bytes: Vec<_> = palette.iter().cycle().take(20).flat_map(|rgba| photocraft_raster::from_rgba(&format, *rgba)).collect(); + surface.write_region(doc_bounds(), &bytes); + Ok(()) + }) + .unwrap(); + s +} + +fn doc_bounds() -> photocraft_doc::Rect { + photocraft_doc::Rect::from_xywh(0, 0, 10, 2) +} + +fn pixels(s: &Session) -> Vec<[f32; 4]> { + photocraft_compose::flatten(&s.active().unwrap().doc).px +} + +fn check_cases(id: &str, cases: &[Value], require_effect: bool) { + let mut failures = Vec::new(); + for depth in [8, 16, 32] { + for params in cases { + let action = Action::new("Selective Color", vec![(id.into(), params.clone())]); + let mut direct = fixture(depth); + let before = pixels(&direct); + direct.execute(id, params.clone()).unwrap(); + let expected = pixels(&direct); + if require_effect { + assert_ne!(expected, before, "ineffective fixture: {id}, depth {depth}, {params}"); + } + + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": action.name})).unwrap(); + assert_eq!(pixels(&native), expected, "native replay: {id}, depth {depth}, {params}"); + + let (bytes, skipped) = atn_cmds::export("Regression", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty()); + assert!(imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + assert_eq!(imported.actions[0].steps.len(), 1); + assert_eq!(imported.actions[0].steps[0].0, id); + + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": action.name})).unwrap(); + if pixels(&replay) != expected { + failures.push(format!("{id}, depth {depth}, {params}")); + } + } + } + assert!(failures.is_empty(), "ATN playback changed the result:\n{}", failures.join("\n")); +} + +fn scalar_cases() -> Vec { + RANGES + .iter() + .flat_map(|range| { + ["relative", "absolute"].map(|method| json!({"method": method, "colors": range, "cyan": 35, "magenta": -12.5, "yellow": 17.75, "black": 9})) + }) + .collect() +} + +#[test] +fn scalar_selective_color_actions_preserve_pixels_at_every_depth() { + let mut cases = scalar_cases(); + cases.push(json!({"method": "absolute", "colors": "reds", "cyan": 35})); + check_cases(COMMANDS[0], &cases, true); +} + +#[test] +fn scalar_selective_color_adjustment_layers_preserve_pixels_at_every_depth() { + check_cases(COMMANDS[1], &scalar_cases(), true); +} + +#[test] +fn mixed_forms_method_precedence_nulls_and_clamping_keep_their_effect() { + let cases = [ + json!({"method": "absolute", "reds": [10, 20, 30, 40], "blues": [-10, 5, 0, 4], "colors": "reds", "cyan": 0, "yellow": -17.5}), + json!({"method": "relative", "relative": false, "colors": "reds", "cyan": 35}), + json!({"method": "absolute", "relative": true, "colors": "neutrals", "black": 15}), + json!({"colors": "blues", "magenta": -25}), + json!({"method": null, "relative": null, "reds": null, "colors": "reds", "cyan": 35, "black": null}), + json!({"method": "absolute", "colors": "reds", "cyan": 175, "magenta": -150}), + json!({"method": "absolute", "reds": [175, -150, 0, 0]}), + json!({"method": "absolute", "reds": [10, 20, 30, 40], "colors": "reds", "cyan": null}), + json!({}), + json!({"colors": "reds"}), + ]; + for id in COMMANDS { + check_cases(id, &cases, false); + } +} + +#[test] +fn per_range_array_controls_still_preserve_pixels() { + let cases: Vec<_> = RANGES.iter().map(|range| json!({"method": "absolute", *range: [35, -12.5, 17.75, 9]})).collect(); + for id in COMMANDS { + check_cases(id, &cases, true); + } +} + +#[test] +fn malformed_selective_color_params_cannot_be_silently_exported() { + for params in [ + json!({"colors": "purples", "cyan": 35}), + json!({"colors": 3}), + json!({"colors": "reds", "cyan": "35"}), + json!({"method": "sideways"}), + json!({"relative": "yes"}), + json!({"reds": [10, 20]}), + json!({"reds": [10, "20", 0, 0]}), + ] { + for id in COMMANDS { + let action = Action::new("Invalid", vec![(id.into(), params.clone())]); + assert!(atn_cmds::export("Regression", &[&action], &[], false).is_err(), "strict export accepted {id} {params}"); + let (_, skipped) = atn_cmds::export("Regression", &[&action], &[], true).unwrap(); + assert_eq!(skipped.len(), 1, "skipUnsupported must report {id} {params}"); + } + } +} diff --git a/crates/engine/tests/it/atn_stroke_width.rs b/crates/engine/tests/it/atn_stroke_width.rs new file mode 100644 index 0000000000..742359cf38 --- /dev/null +++ b/crates/engine/tests/it/atn_stroke_width.rs @@ -0,0 +1,167 @@ +//! #7671: exported Stroke widths must have the same effect as command execution. +use photocraft_engine::actions_cmds::Action; +use photocraft_engine::{Session, atn_cmds}; +use photocraft_psd::{atn, descriptor::Value as D}; +use serde_json::{Value, json}; + +/// Synthetic colours and partial transparency make lost edge pixels observable at every depth. +fn fixture(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 24, "height": 18, "depth": depth})).unwrap(); + s.edit("procedural fixture", |doc, active| { + let surface = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let format = surface.format(); + let bytes: Vec<_> = + (0..432).flat_map(|i| photocraft_raster::from_rgba(&format, [(i % 11) as f32 / 10.0, (i % 7) as f32 / 6.0, (i % 5) as f32 / 4.0, 0.6])).collect(); + surface.write_region(photocraft_doc::Rect::from_xywh(0, 0, 24, 18), &bytes); + Ok(()) + }) + .unwrap(); + s.execute("select.rect", json!({"x": 6, "y": 4, "width": 12, "height": 10})).unwrap(); + s +} + +/// Compare the flattened document, including alpha, rather than only imported JSON. +fn pixels(s: &Session) -> Vec<[f32; 4]> { + photocraft_compose::flatten(&s.active().unwrap().doc).px +} + +/// Direct execution, native playback and strict binary ATN playback must agree exactly. +fn check_pixels(widths: &[(f64, u32)]) { + let mut lost = Vec::new(); + for depth in [8, 16, 32] { + for location in ["inside", "center", "outside"] { + for &(width, effective) in widths { + let params = json!({"width": width, "color": "#d03060", "location": location, "opacity": 63, "mode": "multiply"}); + let action = Action::new("Stroke", vec![("edit.stroke".into(), params.clone())]); + let mut direct = fixture(depth); + let before = pixels(&direct); + direct.execute("edit.stroke", params.clone()).unwrap(); + let expected = pixels(&direct); + assert_ne!(expected, before, "ineffective fixture: {depth}, {params}"); + + let mut whole = fixture(depth); + let mut whole_params = params.clone(); + whole_params["width"] = json!(effective); + whole.execute("edit.stroke", whole_params).unwrap(); + assert_eq!(pixels(&whole), expected, "command width normalization: {depth}, {params}"); + if width == 2.4 { + let mut rounded = fixture(depth); + let mut rounded_params = params.clone(); + rounded_params["width"] = json!(2); + rounded.execute("edit.stroke", rounded_params).unwrap(); + assert_ne!(pixels(&rounded), expected, "2.4 must differ from 2: {depth}, {location}"); + } + + let mut native = fixture(depth); + native.actions.list.push(action.clone()); + native.execute("actions.play", json!({"action": "Stroke"})).unwrap(); + assert_eq!(pixels(&native), expected); + + let (bytes, skipped) = atn_cmds::export("Widths", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty()); + assert!(imported.warnings.is_empty()); + assert_eq!(imported.actions.len(), 1); + assert_eq!(imported.actions[0].steps.len(), 1); + assert_eq!(imported.actions[0].steps[0].0, "edit.stroke"); + let imported_width = imported.actions[0].steps[0].1["width"].clone(); + let mut replay = fixture(depth); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Stroke"})).unwrap(); + if pixels(&replay) != expected { + lost.push(format!("depth {depth}, {location}, width {width} -> {imported_width}, expected {effective}")); + } + } + } + } + assert!(lost.is_empty(), "ATN changed Stroke pixels:\n{}", lost.join("\n")); +} + +/// In particular 2.4 renders as 3 px, while the old exporter serialized 2 px. +#[test] +fn fractional_stroke_width_preserves_pixels_at_every_depth_and_location() { + check_pixels(&[(0.5, 1), (1.01, 2), (2.01, 3), (2.4, 3), (2.5, 3), (2.6, 3), (3.1, 4)]); +} + +/// Existing whole-pixel strokes remain supported, with their original rendering. +#[test] +fn integer_stroke_width_controls_still_preserve_pixels() { + check_pixels(&[(1.0, 1), (2.0, 2), (3.0, 3), (4.0, 4)]); +} + +/// Export the command's bounded/default width as an integer, retaining the other options. +#[test] +fn descriptor_width_matches_defaults_limits_and_other_stroke_options() { + for (width, effective) in [ + (None, 1), + (Some(Value::Null), 1), + (Some(json!("2.4")), 1), + (Some(json!(true)), 1), + (Some(json!([])), 1), + (Some(json!({})), 1), + (Some(json!(-100)), 1), + (Some(json!(0)), 1), + (Some(json!(249.01)), 250), + (Some(json!(249.5)), 250), + (Some(json!(250)), 250), + (Some(json!(250.1)), 250), + (Some(json!(1e300)), 250), + ] { + let mut params = + json!({"color": "#d03060", "location": "outside", "opacity": 63, "mode": "multiply", "preserveTransparency": true, "target": "pixels"}); + if let Some(width) = width { + params["width"] = width; + } + let action = Action::new("Stroke", vec![("edit.stroke".into(), params.clone())]); + let (bytes, skipped) = atn_cmds::export("Widths", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let file = atn::parse(&bytes).unwrap(); + let descriptor = file.actions[0].items[0].descriptor.as_ref().unwrap(); + assert_eq!(descriptor.get("Wdth"), Some(&D::Integer(effective)), "{params}"); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty()); + assert!(imported.warnings.is_empty()); + let p = &imported.actions[0].steps[0].1; + assert_eq!(p["width"].as_f64(), Some(f64::from(effective))); + for key in ["color", "location", "preserveTransparency"] { + assert_eq!(p[key], params[key]); + } + // Import uses the canonical blend-mode label ("Multiply"); execution accepts both cases. + assert!(p["mode"].as_str().unwrap().eq_ignore_ascii_case(params["mode"].as_str().unwrap())); + assert_eq!(p["opacity"].as_f64(), Some(63.0)); + let mut direct = fixture(8); + direct.execute("edit.stroke", params).unwrap(); + let mut replay = fixture(8); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Stroke"})).unwrap(); + assert_eq!(pixels(&replay), pixels(&direct)); + } + // A null parameter object is also a valid call to the command with its default width. + let action = Action::new("Default", vec![("edit.stroke".into(), Value::Null)]); + let (bytes, skipped) = atn_cmds::export("Widths", &[&action], &[], false).unwrap(); + assert!(skipped.is_empty()); + let imported = atn_cmds::import(&bytes).unwrap(); + assert!(imported.unsupported.is_empty()); + assert!(imported.warnings.is_empty()); + assert_eq!(imported.actions[0].steps[0].1["width"].as_f64(), Some(1.0)); + let mut direct = fixture(8); + direct.execute("edit.stroke", Value::Null).unwrap(); + let mut replay = fixture(8); + replay.actions.list = imported.actions; + replay.execute("actions.play", json!({"action": "Default"})).unwrap(); + assert_eq!(pixels(&replay), pixels(&direct)); +} + +/// A custom Stroke converter must retain the plain-event mask/channel target guard. +#[test] +fn stroke_mask_and_channel_targets_are_still_refused_and_reported() { + for target in [json!("mask"), json!("quickMask"), json!({"channel": 0})] { + let action = Action::new("Targeted", vec![("edit.stroke".into(), json!({"width": 2.4, "target": target}))]); + assert!(atn_cmds::export("Widths", &[&action], &[], false).is_err()); + let (bytes, skipped) = atn_cmds::export("Widths", &[&action], &[], true).unwrap(); + assert_eq!(skipped, vec![atn_cmds::Skipped { action: "Targeted".into(), step: 0, id: "edit.stroke".into() }]); + assert!(atn::parse(&bytes).unwrap().actions[0].items.is_empty()); + } +} diff --git a/crates/engine/tests/it/background_and_mask_depth.rs b/crates/engine/tests/it/background_and_mask_depth.rs new file mode 100644 index 0000000000..1fa400e244 --- /dev/null +++ b/crates/engine/tests/it/background_and_mask_depth.rs @@ -0,0 +1,102 @@ +use photocraft_color::SampleType; +use photocraft_engine::Session; +use serde_json::{Value, json}; + +const STYLES: [&str; 10] = + ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "patternOverlay", "bevelEmboss", "satin"]; +const VECTOR_MASKS: [&str; 3] = ["layer.vectorMask.add", "layer.vectorMask.revealAll", "layer.vectorMask.hideAll"]; + +fn background_only() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s +} + +fn has_background(s: &Session) -> bool { + s.active().unwrap().doc.background().is_some() +} + +fn converts_and_undoes(cmd: &str, params: Value) { + let mut s = background_only(); + s.execute(cmd, params).unwrap_or_else(|e| panic!("{cmd}: {e}")); + assert!(!has_background(&s), "{cmd} left a Background carrying the change"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(has_background(&s), "{cmd}: one undo restores the Background"); +} + +#[test] +fn layer_styles_and_vector_masks_turn_the_background_into_a_layer() { + for kind in STYLES { + converts_and_undoes(&format!("layer.layerStyle.{kind}"), json!({})); + } + for cmd in VECTOR_MASKS { + converts_and_undoes(cmd, json!({})); + } + converts_and_undoes("layer.layerStyle.replace", json!({"effects": [{"kind": "stroke"}]})); +} + +#[test] +fn clearing_a_style_keeps_the_background() { + let mut s = background_only(); + s.execute("layer.layerStyle.replace", json!({"effects": []})).unwrap(); + assert!(has_background(&s)); +} + +fn mask_sample(s: &Session) -> SampleType { + let st = s.active().unwrap(); + st.doc.walk().into_iter().find_map(|(_, _, l)| l.mask.as_ref().map(|m| m.surface.format().sample)).expect("a layer mask") +} + +fn painted(depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [30, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s +} + +#[test] +fn frame_and_remove_background_masks_match_the_document_depth() { + for depth in [16, 32] { + let want = if depth == 16 { SampleType::U16 } else { SampleType::F32 }; + for (cmd, params) in [ + ("layer.new.frameFromLayers", json!({})), + ("layer.new.frame", json!({"x": 4, "y": 4, "width": 16, "height": 12})), + ("layer.new.frame", json!({"x": 4, "y": 4, "width": 16, "height": 12, "shape": "ellipse"})), + ("layer.removeBackground", json!({})), + ] { + let mut s = painted(depth); + s.execute(cmd, params.clone()).unwrap_or_else(|e| panic!("{cmd} {params}: {e}")); + assert_eq!(mask_sample(&s), want, "{cmd} {params} in a {depth}-bit document"); + } + } +} + +fn copied_style_over_background() -> (Session, u64, u64) { + let mut s = background_only(); + let bg = s.active().unwrap().doc.background().unwrap().id.0; + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); + s.execute("layer.layerStyle.copyLayerStyle", json!({})).unwrap(); + let layer = s.active().unwrap().active_layer.unwrap().0; + (s, bg, layer) +} + +#[test] +fn pasting_a_style_onto_the_background_turns_it_into_a_layer() { + let (mut s, bg, _) = copied_style_over_background(); + s.execute("layer.layerStyle.pasteLayerStyle", json!({"layer": bg})).unwrap(); + assert!(!has_background(&s), "the pasted style landed on a Background"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(has_background(&s), "one undo restores the Background"); +} + +#[test] +fn pasting_a_style_with_no_layer_skips_the_background() { + let (mut s, bg, layer) = copied_style_over_background(); + s.execute("layer.select", json!({"layer": bg})).unwrap(); + s.execute("layer.select", json!({"layer": layer, "mode": "add"})).unwrap(); + s.execute("layer.layerStyle.pasteLayerStyle", json!({})).unwrap(); + assert!(has_background(&s)); + assert!(s.active().unwrap().doc.background().unwrap().effects.items.is_empty()); +} diff --git a/crates/engine/tests/background_layer_consistency.rs b/crates/engine/tests/it/background_layer_consistency.rs similarity index 100% rename from crates/engine/tests/background_layer_consistency.rs rename to crates/engine/tests/it/background_layer_consistency.rs diff --git a/crates/engine/tests/brush_smoothing.rs b/crates/engine/tests/it/brush_smoothing.rs similarity index 100% rename from crates/engine/tests/brush_smoothing.rs rename to crates/engine/tests/it/brush_smoothing.rs diff --git a/crates/engine/tests/it/camera_raw_exposure_clamp.rs b/crates/engine/tests/it/camera_raw_exposure_clamp.rs new file mode 100644 index 0000000000..ee624f7e28 --- /dev/null +++ b/crates/engine/tests/it/camera_raw_exposure_clamp.rs @@ -0,0 +1,40 @@ +//! `filter.cameraRaw` must clamp `exposure` to its documented -5..5 range. Before the fix, +//! `raw_params` passed it through, so an exposure of 128 or more overflowed `2^exposure` to +//! infinity and a 32-bit layer was left with non-finite samples. + +use photocraft_engine::Session; +use serde_json::json; + +#[test] +fn camera_raw_clamps_exposure() { + let hi = photocraft_engine::lens_cmds::raw_params("filter.cameraRaw", &json!({"exposure": 255.0})).expect("params parse"); + assert_eq!(hi.exposure, 5.0, "exposure 255 must clamp to 5"); + let lo = photocraft_engine::lens_cmds::raw_params("filter.cameraRaw", &json!({"exposure": -255.0})).expect("params parse"); + assert_eq!(lo.exposure, -5.0, "exposure -255 must clamp to -5"); + let mid = photocraft_engine::lens_cmds::raw_params("filter.cameraRaw", &json!({"exposure": 1.5})).expect("params parse"); + assert_eq!(mid.exposure, 1.5, "in-range exposure stays untouched"); +} + +#[test] +fn camera_raw_out_of_range_exposure_keeps_32_bit_layer_finite() { + for exposure in [255.0, -255.0] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8, "depth": 32})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("filter.cameraRaw", json!({"exposure": exposure})).unwrap(); + let st = s.active().unwrap(); + let layer = st.active_layer.and_then(|id| st.doc.layer(id)).unwrap(); + let surf = layer.surface().unwrap(); + let mut bad = Vec::new(); + for y in 0..8 { + for x in 0..8 { + let px = surf.rgba(x, y); + if px.iter().any(|v| !v.is_finite()) { + bad.push(px); + } + } + } + assert!(bad.is_empty(), "exposure {exposure}: {} non-finite pixels, e.g. {:?}", bad.len(), bad.first()); + } +} diff --git a/crates/engine/tests/camera_raw_noise_clamp.rs b/crates/engine/tests/it/camera_raw_noise_clamp.rs similarity index 100% rename from crates/engine/tests/camera_raw_noise_clamp.rs rename to crates/engine/tests/it/camera_raw_noise_clamp.rs diff --git a/crates/engine/tests/clipping_semantics.rs b/crates/engine/tests/it/clipping_semantics.rs similarity index 100% rename from crates/engine/tests/clipping_semantics.rs rename to crates/engine/tests/it/clipping_semantics.rs diff --git a/crates/engine/tests/close_document.rs b/crates/engine/tests/it/close_document.rs similarity index 100% rename from crates/engine/tests/close_document.rs rename to crates/engine/tests/it/close_document.rs diff --git a/crates/engine/tests/command_lookup.rs b/crates/engine/tests/it/command_lookup.rs similarity index 100% rename from crates/engine/tests/command_lookup.rs rename to crates/engine/tests/it/command_lookup.rs diff --git a/crates/engine/tests/it/convert_keeps_references.rs b/crates/engine/tests/it/convert_keeps_references.rs new file mode 100644 index 0000000000..a9859d912e --- /dev/null +++ b/crates/engine/tests/it/convert_keeps_references.rs @@ -0,0 +1,47 @@ +use photocraft_doc::{LayerId, SliceOrigin}; +use photocraft_engine::Session; +use serde_json::json; + +fn art() -> (Session, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Art"})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s.execute("layer.newLayerBasedSlice", json!({})).unwrap(); + s.execute("image.variables.define", json!({"defs": [{"name": "v", "layer": id.0, "type": "visibility"}]})).unwrap(); + (s, id) +} + +fn references_point_at(s: &Session, id: LayerId) -> bool { + let doc = &s.active().unwrap().doc; + let slice = doc.slices.list.iter().any(|sl| sl.origin == SliceOrigin::Layer && sl.layer == Some(id)); + let comps = doc.layer_comps.iter().all(|c| c.states.iter().any(|st| st.layer == id)); + let vars = doc.variables.defs.iter().all(|d| d.layer == id); + slice && comps && vars +} + +#[test] +fn converting_a_layer_keeps_its_slice_comps_and_variables() { + for cmd in ["layer.smartObjects.convertToSmartObject", "filter.convertForSmartFilters"] { + let (mut s, id) = art(); + s.execute("layerComp.new", json!({"name": "A", "visibility": true, "position": true, "appearance": true})).unwrap(); + s.execute(cmd, json!({})).unwrap(); + let smart = s.active().unwrap().active_layer.unwrap(); + assert!(references_point_at(&s, smart), "{cmd} left references on the old layer"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(references_point_at(&s, id), "{cmd}: undo restores the original references"); + } +} + +#[test] +fn a_comp_does_not_put_the_converted_layers_style_on_the_smart_object() { + let (mut s, _) = art(); + s.execute("layer.layerStyle.dropShadow", json!({})).unwrap(); + let r = s.execute("layerComp.new", json!({"name": "A", "visibility": true, "position": true, "appearance": true})).unwrap(); + let comp = r["comp"].as_u64().unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let smart = s.active().unwrap().active_layer.unwrap(); + s.execute("layerComp.apply", json!({"comp": comp})).unwrap(); + assert!(s.active().unwrap().doc.layer(smart).unwrap().effects.items.is_empty(), "the style stays inside the smart object"); +} diff --git a/crates/engine/tests/it/copy_shape_placement.rs b/crates/engine/tests/it/copy_shape_placement.rs new file mode 100644 index 0000000000..ee6d016984 --- /dev/null +++ b/crates/engine/tests/it/copy_shape_placement.rs @@ -0,0 +1,74 @@ +//! Copy placement must use source-space bounds before destination cache clipping. +use photocraft_doc::LayerContent; +use photocraft_engine::Session; +use serde_json::json; + +/// Cover source clipping, groups, missing caches, destination depth, placement and Undo/Redo. +#[test] +fn copying_shapes_to_smaller_documents_preserves_requested_center() { + for x in [90, 700] { + for grouped in [false, true] { + for missing_cache in [false, true] { + for depth in [8, 16] { + for at in [None, Some([40, 60])] { + let mut s = Session::new(); + s.execute("file.new", json!({"width":800,"height":600,"background":"transparent"})).unwrap(); + let mut ids = Vec::new(); + for offset in if grouped { vec![0, 30] } else { vec![0] } { + s.execute("shape.create", json!({"kind":"rect","rect":[x+offset,20,20,20],"fill":"#ff0000"})).unwrap(); + s.execute("layer.layerStyle.dropShadow", json!({"size":4,"distance":3})).unwrap(); + ids.push(s.active().unwrap().active_layer.unwrap()); + } + if grouped { + s.active_mut().unwrap().selected_layers = ids.clone(); + s.execute("layer.groupLayers", json!({})).unwrap(); + } + if missing_cache { + s.edit("drop cached shapes", |doc, _| { + for id in &ids { + let LayerContent::Shape(shape) = &mut doc.layer_mut(*id).unwrap().content else { panic!("shape") }; + shape.cache = None; + } + Ok(()) + }) + .unwrap(); + } + s.execute("file.new", json!({"width":100,"height":100,"depth":depth,"background":"transparent"})).unwrap(); + let before = s.active().unwrap().doc.clone(); + let mut params = json!({"source":0,"document":1,"center":true}); + if let Some(at) = at { + params["center"] = json!(false); + params["at"] = json!(at); + } + let result = s.execute("layer.copyToDocument", params).unwrap(); + let target = at.unwrap_or([50, 50]); + let dx = target[0] - (x + if grouped { 25 } else { 10 }); + let dy = target[1] - 30; + assert_eq!(result["offset"], json!([dx, dy]), "x={x}, grouped={grouped}, cache missing={missing_cache}"); + let st = s.active().unwrap(); + let shapes: Vec<_> = st + .doc + .walk() + .into_iter() + .filter_map(|(_, _, layer)| if let LayerContent::Shape(shape) = &layer.content { Some(shape) } else { None }) + .collect(); + assert_eq!(shapes.len(), if grouped { 2 } else { 1 }); + for shape in shapes { + let fresh = photocraft_vector::render_shape(shape, st.doc.pixel_format(), st.doc.bounds()); + assert!( + shape.cache.as_ref() == Some(&fresh), + "cache mismatch: x={x}, grouped={grouped}, cache missing={missing_cache}, depth={depth}, at={at:?}" + ); + assert!(!fresh.content_bounds().is_empty()); + } + let after = st.doc.clone(); + assert!(s.undo()); + assert_eq!(s.active().unwrap().doc, before); + assert!(s.redo()); + assert_eq!(s.active().unwrap().doc, after); + } + } + } + } + } +} diff --git a/crates/engine/tests/it/corpus_atn.rs b/crates/engine/tests/it/corpus_atn.rs new file mode 100644 index 0000000000..afc11cbc2a --- /dev/null +++ b/crates/engine/tests/it/corpus_atn.rs @@ -0,0 +1,132 @@ +//! Corpus test (feature `corpus`; run with `cargo xtask test-corpus`): every `.atn` set under +//! `corpus/atn/` loads through `actions.load`, exports through `actions.save`, and the export +//! loads back to the same actions. Steps without a PhotoCraft equivalent are counted against a +//! ceiling, so a mapping regression (or a new unmapped event in the corpus) shows up here. +#![cfg(feature = "corpus")] + +use std::path::{Path, PathBuf}; + +use photocraft_engine::Session; +use photocraft_engine::actions_cmds::Action; +use serde_json::{Value, json}; + +fn collect(dir: &Path, out: &mut Vec) { + // An unreadable folder fails the test rather than shrinking the corpus. + let rd = std::fs::read_dir(dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())); + for e in rd { + let p = e.unwrap_or_else(|e| panic!("{}: {e}", dir.display())).path(); + if p.is_dir() { + collect(&p, out); + } else if p.extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("atn")) { + out.push(p); + } + } +} + +/// Sets in the corpus, and the most steps that may still be kept Photoshop steps across all of +/// them (2026-10-12: 517 of 5 296; 404 are generative AI (`generativeUpscale`, `syntheticFill`, +/// `syntheticGenHarmonize`: deferred by decision) and `AdobeScriptAutomation` scripts, the rest +/// shape layers with strokes, snapshots, smart-filter and channel edits, frame animation). +/// Raise the first, lower the second, never the reverse. +const FLOOR_FILES: usize = 16; +const CEILING_UNSUPPORTED: usize = 540; + +/// Save writes a compound `layer.setProps` (name, opacity, blend) as one Set per property, because +/// Photoshop applies only the rename of a combined one (#8304); loading gives one step per +/// property. Merge consecutive `layer.setProps` steps of one target so both sides compare as the +/// same actions. +fn merge_set_props(actions: &[Action]) -> Vec { + actions + .iter() + .map(|a| { + let mut out = a.clone(); + out.steps.clear(); + out.disabled.clear(); + out.dialogs.clear(); + for (i, (id, params)) in a.steps.iter().enumerate() { + let merged = match (out.steps.last_mut(), params.as_object()) { + (Some((last_id, Value::Object(last))), Some(p)) + if id == "layer.setProps" + && last_id == id + && last.get("layer") == p.get("layer") + && p.keys().all(|k| k == "layer" || !last.contains_key(k)) => + { + last.extend(p.iter().map(|(k, v)| (k.clone(), v.clone()))); + true + } + _ => false, + }; + if !merged { + out.steps.push((id.clone(), params.clone())); + } + let at = out.steps.len() - 1; + if a.disabled.contains(&i) { + out.disabled.insert(at); + } + if a.dialogs.contains(&i) { + out.dialogs.insert(at); + } + } + out + }) + .collect() +} + +#[test] +fn atn_corpus_loads_saves_and_reloads() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/atn"); + assert!(root.is_dir(), "{} is missing: run `cargo xtask corpus --atn`", root.display()); + let mut files = Vec::new(); + collect(&root, &mut files); + files.sort(); + let mut failures = Vec::new(); + let (mut steps, mut unsupported) = (0usize, 0usize); + for p in &files { + let name = p.strip_prefix(&root).unwrap_or(p).display().to_string(); + let data = photocraft_engine::paint::tile::b64_encode(&std::fs::read(p).unwrap()); + let mut s = Session::new(); + let loaded = match s.execute("actions.load", json!({"data": data})) { + Ok(v) => v, + Err(e) => { + failures.push(format!("{name}: load: {e}")); + continue; + } + }; + let n_steps = loaded["steps"].as_u64().unwrap_or(0) as usize; + let n_unsupported = loaded["unsupported"].as_array().map_or(0, Vec::len); + let events: Vec = + loaded["unsupported"].as_array().map_or(Vec::new(), |u| u.iter().filter_map(|x| x["event"].as_str().map(String::from)).collect()); + steps += n_steps; + unsupported += n_unsupported; + println!("{name}: {} actions, {n_steps} steps, {n_unsupported} kept Photoshop steps {events:?}", loaded["actions"].as_array().map_or(0, Vec::len)); + let first = s.actions.list.clone(); + let saved = match s.execute("actions.save", json!({"set": loaded["set"]})) { + Ok(v) => v, + Err(e) => { + failures.push(format!("{name}: save: {e}")); + continue; + } + }; + let bytes = photocraft_engine::paint::tile::b64_decode(saved["data"].as_str().unwrap_or("")).unwrap_or_default(); + let mut t = Session::new(); + match t.execute("actions.load", json!({"data": photocraft_engine::paint::tile::b64_encode(&bytes)})) { + Ok(_) if merge_set_props(&t.actions.list) == merge_set_props(&first) => {} + Ok(_) => { + // Name the first step that differs, so a mapping asymmetry is quick to find. + let detail = first + .iter() + .zip(&t.actions.list) + .flat_map(|(a, b)| a.steps.iter().zip(&b.steps).enumerate().map(move |(i, (x, y))| (a.name.as_str(), i, x, y))) + .find(|(_, _, x, y)| x != y) + .map(|(action, i, x, y)| format!("`{action}` step {}: {} {} → {} {}", i + 1, x.0, x.1, y.0, y.1)) + .unwrap_or_else(|| "the actions differ in count, flags or names".into()); + failures.push(format!("{name}: the saved set loads to different actions: {detail}")); + } + Err(e) => failures.push(format!("{name}: reload: {e}")), + } + } + println!("{} files, {steps} steps, {unsupported} kept Photoshop steps", files.len()); + assert!(files.len() >= FLOOR_FILES, "only {} .atn files; the corpus floor is {FLOOR_FILES}", files.len()); + assert!(unsupported <= CEILING_UNSUPPORTED, "{unsupported} steps have no PhotoCraft equivalent; the ceiling is {CEILING_UNSUPPORTED}"); + assert!(failures.is_empty(), "{} failures:\n{}", failures.len(), failures.join("\n")); +} diff --git a/crates/engine/tests/crop_content_aware.rs b/crates/engine/tests/it/crop_content_aware.rs similarity index 71% rename from crates/engine/tests/crop_content_aware.rs rename to crates/engine/tests/it/crop_content_aware.rs index f41d46a9b5..0cc3dbdeca 100644 --- a/crates/engine/tests/crop_content_aware.rs +++ b/crates/engine/tests/it/crop_content_aware.rs @@ -157,3 +157,62 @@ fn bad_params_and_unfillable_crops_fail_cleanly() { assert_eq!(s.active().unwrap().history.past_len(), past, "{p}"); } } + +/// `doc` with its left 16 columns red, then cropped to x = 16 keeping the cropped pixels: the red +/// lies hidden past the canvas's left edge. +fn hidden_red(background: &str) -> Session { + let mut s = doc("rgb", 8, background); + s.execute("tools.setColors", json!({"foreground": "#ff0000", "background": "#000000"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 16, "height": 48})).unwrap(); + s.execute("edit.fill", json!({"contents": "foreground"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("image.crop", json!({"x": 16, "y": 0, "width": 48, "height": 48, "deleteCroppedPixels": false})).unwrap(); + assert_eq!(size(&s), (48, 48)); + s +} + +const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0]; + +/// A straight frame pulled back out over pixels a Delete Cropped Pixels off crop hid fills over +/// them from the canvas, as Photoshop does with Delete Cropped Pixels off or on (#8060). +#[test] +fn extending_crop_fills_over_the_hidden_pixels() { + for background in ["transparent", "white"] { + for delete in [false, true] { + let mut s = hidden_red(background); + let green = layer(&s).rgba(24, 24); + crop(&mut s, json!({"x": -24, "y": 0, "width": 72, "height": 48, "deleteCroppedPixels": delete}), true).unwrap(); + assert_eq!(size(&s), (72, 48)); + let surf = layer(&s); + for y in 0..48 { + for x in 0..24 { + let px = surf.rgba(x, y); + assert!(close(px, green, 0.06), "{background} delete {delete}: ({x}, {y}) is filled from the canvas, not the hidden red: {px:?}"); + } + assert!(close(surf.rgba(40, y), green, 1e-6), "{background} delete {delete}: the canvas is unchanged"); + } + } + } +} + +/// A turned frame that reaches over the Background's hidden pixels shows them, not a fill +/// from the canvas (#8060). +#[test] +fn rotated_crop_keeps_the_hidden_pixels() { + let mut s = hidden_red("white"); + // A diamond on the canvas's left edge: its left half lies over the hidden red, all of it inside + // the turned canvas (so nothing takes the background colour). + crop(&mut s, json!({"x": -10, "y": 14, "width": 20, "height": 20, "angle": 45, "deleteCroppedPixels": false}), true).unwrap(); + assert_eq!(size(&s), (20, 20)); + let (w, h) = size(&s); + let surf = layer(&s); + let mut red = 0; + for y in 0..h as i32 { + for x in 0..w as i32 { + let px = surf.rgba(x, y); + assert!(px[0] > 0.5 && px[3] > 0.99, "no background colour or transparency at ({x}, {y}): {px:?}"); + red += usize::from(close(px, RED, 0.06)); + } + } + assert!(red >= 100, "the hidden red shows in the turned frame: {red} red pixels"); +} diff --git a/crates/engine/tests/current_affinity_artboards.rs b/crates/engine/tests/it/current_affinity_artboards.rs similarity index 97% rename from crates/engine/tests/current_affinity_artboards.rs rename to crates/engine/tests/it/current_affinity_artboards.rs index e3b27e856b..8348b4065f 100644 --- a/crates/engine/tests/current_affinity_artboards.rs +++ b/crates/engine/tests/it/current_affinity_artboards.rs @@ -1,7 +1,7 @@ //! Artboards recognized from current .af properties move with their children and undo together. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] -#[path = "../../io/tests/support/current_affinity.rs"] +#[path = "../../../io/tests/support/current_affinity.rs"] mod fixture; use fixture::{Marker, document}; diff --git a/crates/engine/tests/duplicate_layer_comps.rs b/crates/engine/tests/it/duplicate_layer_comps.rs similarity index 100% rename from crates/engine/tests/duplicate_layer_comps.rs rename to crates/engine/tests/it/duplicate_layer_comps.rs diff --git a/crates/engine/tests/it/duplicate_slices.rs b/crates/engine/tests/it/duplicate_slices.rs new file mode 100644 index 0000000000..5f5d21c621 --- /dev/null +++ b/crates/engine/tests/it/duplicate_slices.rs @@ -0,0 +1,35 @@ +use photocraft_doc::SliceOrigin; +use photocraft_engine::Session; +use serde_json::{Value, json}; + +fn sliced() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({"name": "logo"})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s.execute("layer.newLayerBasedSlice", json!({"url": "https://example.com", "alt": "logo"})).unwrap(); + s +} + +fn layer_slice(s: &mut Session) -> Option { + let list = s.execute("slice.list", json!({})).unwrap(); + list["slices"].as_array().unwrap().iter().find(|sl| sl["origin"] == "layer").cloned() +} + +#[test] +fn duplicating_a_document_keeps_its_layer_based_slices() { + for cmd in ["image.duplicate", "history.newDocument"] { + let mut s = sliced(); + s.execute(cmd, json!({})).unwrap(); + let st = s.active().unwrap(); + let logo = st.doc.walk().into_iter().find(|(_, _, l)| l.name == "logo").unwrap().2.id; + let slices: Vec<_> = st.doc.slices.list.iter().filter(|sl| sl.origin == SliceOrigin::Layer).collect(); + assert_eq!(slices.len(), 1, "{cmd}"); + assert_eq!(slices[0].layer, Some(logo), "{cmd}"); + assert_eq!((slices[0].url.as_str(), slices[0].alt.as_str()), ("https://example.com", "logo"), "{cmd}"); + let before = layer_slice(&mut s).unwrap()["rect"].clone(); + s.execute("layer.translate", json!({"dx": 5, "dy": 3})).unwrap(); + let after = layer_slice(&mut s).unwrap_or_else(|| panic!("{cmd} lost the slice after a move"))["rect"].clone(); + assert_ne!(before, after, "{cmd}: the slice no longer follows its layer"); + } +} diff --git a/crates/engine/tests/duplicate_variables.rs b/crates/engine/tests/it/duplicate_variables.rs similarity index 100% rename from crates/engine/tests/duplicate_variables.rs rename to crates/engine/tests/it/duplicate_variables.rs diff --git a/crates/engine/tests/empty_selection_combine.rs b/crates/engine/tests/it/empty_selection_combine.rs similarity index 59% rename from crates/engine/tests/empty_selection_combine.rs rename to crates/engine/tests/it/empty_selection_combine.rs index 867acaaf21..12731d7088 100644 --- a/crates/engine/tests/empty_selection_combine.rs +++ b/crates/engine/tests/it/empty_selection_combine.rs @@ -91,3 +91,42 @@ fn subtracting_from_an_existing_selection_preserves_other_pixels_and_history() { assert_eq!(coverage(&session, 12, 12), removed); } } + +#[test] +fn marquee_add_subtract_match_dense_combination_across_tiles() { + use photocraft_algo::selection as sel; + use photocraft_color::PixelFormat; + use photocraft_geom::Rect; + use photocraft_raster::Surface; + + // A partial edge tile, a shape crossing a tile boundary, and an untouched distant tile. + let area = Rect::from_xywh(0, 0, 777, 263); + let r = Rect::from_xywh(247, -5, 24, 32); + for depth in [8, 16, 32] { + for ellipse in [false, true] { + for mode in ["add", "subtract"] { + let mut session = Session::new(); + session.execute("file.new", json!({"width": 777, "height": 263, "depth": depth})).unwrap(); + let mut old = Surface::new(PixelFormat::GRAY8); + old.fill_rect(Rect::from_xywh(240, 0, 40, 40), &[0.6]); + old.fill_rect(Rect::from_xywh(768, 256, 9, 7), &[0.4]); + std::sync::Arc::make_mut(&mut session.active_mut().unwrap().doc).selection = Some(old.clone()); + let shape = if ellipse { + sel::ellipse_surface(r, area, true) + } else { + let mut shape = Surface::new(PixelFormat::GRAY8); + shape.fill_rect(r.intersect(&area), &[1.0]); + shape + }; + let expected = sel::combine_surfaces(&old, &shape, area, |a, b| if mode == "add" { a.max(b) } else { a * (1.0 - b) }); + session.execute("select.rect", json!({"x": 247, "y": -5, "width": 24, "height": 32, "ellipse": ellipse, "mode": mode})).unwrap(); + let pixels = |session: &Session| session.active().unwrap().doc.selection.as_ref().unwrap().read_region(area); + assert_eq!(pixels(&session), expected.read_region(area), "depth={depth}, ellipse={ellipse}, mode={mode}"); + assert!(session.undo()); + assert_eq!(pixels(&session), old.read_region(area)); + assert!(session.redo()); + assert_eq!(pixels(&session), expected.read_region(area)); + } + } + } +} diff --git a/crates/engine/tests/eraser_locked.rs b/crates/engine/tests/it/eraser_locked.rs similarity index 100% rename from crates/engine/tests/eraser_locked.rs rename to crates/engine/tests/it/eraser_locked.rs diff --git a/crates/engine/tests/explicit_layer_target.rs b/crates/engine/tests/it/explicit_layer_target.rs similarity index 100% rename from crates/engine/tests/explicit_layer_target.rs rename to crates/engine/tests/it/explicit_layer_target.rs diff --git a/crates/engine/tests/it/export_shape_strokes.rs b/crates/engine/tests/it/export_shape_strokes.rs new file mode 100644 index 0000000000..0875f4bfc0 --- /dev/null +++ b/crates/engine/tests/it/export_shape_strokes.rs @@ -0,0 +1,105 @@ +//! Regression coverage for #8341: canvas resizing must scale shape strokes too. +use photocraft_doc::{LayerContent, LayerId}; +use photocraft_engine::{ + Session, + web_cmds::{WebSettings, web_document}, +}; +use serde_json::json; + +fn shape_session(depth: u32) -> (Session, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 1024, "height": 1024, "depth": depth, "background": "transparent"})).unwrap(); + let id = s.execute("shape.create", json!({"rect": [256, 256, 512, 512], "fill": null, "stroke": {"width": 32, "color": "#000000"}})).unwrap()["layer"] + .as_u64() + .unwrap(); + (s, LayerId(id)) +} + +#[test] +fn web_downscale_keeps_stroke_only_shape_proportions_at_every_depth() { + for depth in [8, 16, 32] { + let (s, id) = shape_session(depth); + let original = &s.active().unwrap().doc; + for params in [json!({"percent": 25}), json!({"width": 256, "height": 256})] { + let (scaled, sx, sy) = web_document(original, ¶ms, &WebSettings::default()).unwrap(); + assert_eq!((scaled.size.width, scaled.size.height), (256, 256)); + assert_eq!((sx, sy), (0.25, 0.25)); + let LayerContent::Shape(sh) = &scaled.layer(id).unwrap().content else { + panic!("shape lost"); + }; + assert_eq!(sh.stroke.as_ref().unwrap().width, 8.0, "{depth}-bit stroke"); + let pixels = photocraft_compose::flatten(&scaled); + let alpha = |x: usize| pixels.px[128 * 256 + x][3]; + assert!(alpha(62) > 0.99, "scaled stroke is visible"); + assert!(alpha(52) < 0.01, "stroke must not retain its original width"); + assert!(alpha(128) < 0.01, "no fill is introduced"); + } + let LayerContent::Shape(sh) = &original.layer(id).unwrap().content else { + panic!("shape lost"); + }; + assert_eq!(sh.stroke.as_ref().unwrap().width, 32.0, "export leaves the source untouched"); + } +} + +#[test] +fn image_size_scales_strokes_once_and_undo_restores_them() { + let (mut s, id) = shape_session(8); + s.execute("shape.edit", json!({"layer": id.0, "stroke": {"dashes": [2, 1], "dashOffset": 0.5}})).unwrap(); + let before = s.active().unwrap().doc.layer(id).unwrap().clone(); + s.execute("image.imageSize", json!({"width": 512, "height": 512})).unwrap(); + let LayerContent::Shape(sh) = &s.active().unwrap().doc.layer(id).unwrap().content else { + panic!("shape lost"); + }; + assert_eq!(sh.stroke.as_ref().unwrap().width, 16.0); + assert_eq!(sh.stroke.as_ref().unwrap().dashes, vec![2.0, 1.0], "dash lengths stay in stroke-width units"); + assert_eq!(sh.stroke.as_ref().unwrap().dash_offset, 0.5); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc.layer(id).unwrap(), &before); + s.execute("edit.redo", json!({})).unwrap(); + let LayerContent::Shape(sh) = &s.active().unwrap().doc.layer(id).unwrap().content else { + panic!("shape lost"); + }; + assert_eq!(sh.stroke.as_ref().unwrap().width, 16.0); +} + +#[test] +fn grouped_strokes_scale_up_and_unchanged_size_is_a_noop() { + let (mut s, id) = shape_session(8); + s.edit("Group", |doc, _| { + let children = std::mem::take(&mut doc.layers); + doc.layers.push(photocraft_doc::Layer::group("Outer", vec![photocraft_doc::Layer::group("Inner", children)])); + Ok(()) + }) + .unwrap(); + let original = &s.active().unwrap().doc; + for (width, expected) in [(1024, 32.0), (1280, 40.0), (256, 8.0)] { + let (scaled, _, _) = web_document(original, &json!({"width": width}), &WebSettings::default()).unwrap(); + let LayerContent::Shape(sh) = &scaled.layer(id).unwrap().content else { + panic!("shape lost"); + }; + assert_eq!(sh.stroke.as_ref().unwrap().width, expected); + } +} + +#[test] +fn png_export_contains_the_scaled_stroke_and_keeps_the_source_document() { + let (mut s, _) = shape_session(8); + let before = s.active().unwrap().doc.clone(); + let mut bytes = Vec::new(); + photocraft_engine::web_cmds::save_for_web_with_writer( + &mut s, + &json!({"format": "png24", "path": "stroke.png", "width": 256, "height": 256}), + &mut |_, encoded| { + bytes = encoded.to_vec(); + Ok(()) + }, + ) + .unwrap(); + let image = photocraft_codecs::decode(&bytes).unwrap().convert(photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8); + assert_eq!(image.dimensions(), (256, 256)); + let alpha = |x: usize| image.data()[(128 * 256 + x) * 4 + 3]; + assert_eq!(alpha(62), 255); + assert_eq!(alpha(52), 0); + assert_eq!(alpha(128), 0); + assert_eq!(s.active().unwrap().doc, before); +} diff --git a/crates/engine/tests/fill_lock_transparency.rs b/crates/engine/tests/it/fill_lock_transparency.rs similarity index 100% rename from crates/engine/tests/fill_lock_transparency.rs rename to crates/engine/tests/it/fill_lock_transparency.rs diff --git a/crates/engine/tests/filter_locked.rs b/crates/engine/tests/it/filter_locked.rs similarity index 100% rename from crates/engine/tests/filter_locked.rs rename to crates/engine/tests/it/filter_locked.rs diff --git a/crates/engine/tests/it/finite_command_params.rs b/crates/engine/tests/it/finite_command_params.rs new file mode 100644 index 0000000000..0350d74493 --- /dev/null +++ b/crates/engine/tests/it/finite_command_params.rs @@ -0,0 +1,154 @@ +//! #6424: rejecting an overflowing f32 parameter must leave a saveable document and history. +use photocraft_engine::{EngineError, Session}; +use serde_json::{Value, json}; + +fn fresh(mode: &str, depth: u32) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 16, "height": 12, "mode": mode, "depth": depth})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s +} + +fn saved(s: &Session) -> Vec { + photocraft_format::save_to_bytes(&s.active().unwrap().doc, &Default::default()).unwrap() +} + +fn rejected(cases: Vec<(&str, Value)>) { + let mut failures = Vec::new(); + for (id, p) in cases { + let mut s = fresh("rgb", 8); + let before = saved(&s); + let undo = s.active().unwrap().history.undo_label().map(str::to_owned); + let result = s.execute(id, p.clone()); + let save = photocraft_format::save_to_bytes(&s.active().unwrap().doc, &Default::default()); + if !matches!(result, Err(EngineError::BadParams { .. })) { + failures.push(format!("{id} {p}: result={result:?}, save error={:?}", save.as_ref().err())); + continue; + } + assert_eq!(save.unwrap(), before, "rejected {id} must not change the document"); + assert_eq!(s.active().unwrap().history.undo_label(), undo.as_deref(), "rejected {id} must not add history"); + } + assert!(failures.is_empty(), "{}", failures.join("\n")); +} + +#[test] +fn overflowing_layer_styles_are_rejected_before_mutation() { + let mut cases = Vec::new(); + for value in [1e39, -1e39] { + for (kind, key) in [ + ("dropShadow", "angle"), + ("innerShadow", "angle"), + ("outerGlow", "size"), + ("innerGlow", "size"), + ("stroke", "size"), + ("satin", "angle"), + ("bevelEmboss", "depth"), + ("gradientOverlay", "angle"), + ("patternOverlay", "phaseX"), + ] { + let id = match kind { + "dropShadow" => "layer.layerStyle.dropShadow", + "innerShadow" => "layer.layerStyle.innerShadow", + "outerGlow" => "layer.layerStyle.outerGlow", + "innerGlow" => "layer.layerStyle.innerGlow", + "stroke" => "layer.layerStyle.stroke", + "satin" => "layer.layerStyle.satin", + "bevelEmboss" => "layer.layerStyle.bevelEmboss", + "gradientOverlay" => "layer.layerStyle.gradientOverlay", + _ => "layer.layerStyle.patternOverlay", + }; + let params = json!({key: value}); + cases.push((id, params.clone())); + cases.push(("layer.layerStyle.replace", json!({"effects": [{"kind": kind, "params": params}]}))); + } + cases.push(("layer.layerStyle.colorOverlay", json!({"color": [value, 0, 0]}))); + } + rejected(cases); +} + +#[test] +fn overflowing_document_colors_are_rejected_before_mutation() { + let mut cases = Vec::new(); + for id in ["layer.newFillLayer.solidColor", "channel.new", "channel.newSpot", "layer.new.artboard", "count.newGroup"] { + for value in [1e39, -1e39] { + for component in 0..4 { + let mut color = [0.5; 4]; + color[component] = value; + cases.push((id, json!({"color": color}))); + } + } + } + rejected(cases); +} + +#[test] +fn overflowing_type_positions_and_colors_are_rejected_before_mutation() { + rejected(vec![ + ("type.create", json!({"text": "hello", "x": 1e39})), + ("type.create", json!({"text": "hello", "y": -1e39})), + ("type.create", json!({"text": "hello", "color": [1e39, 0, 0]})), + ]); +} + +#[test] +fn valid_color_and_style_edits_still_save_and_undo_at_each_depth() { + for mode in ["rgb", "gray", "cmyk", "lab"] { + for depth in [8, 16, 32] { + let mut s = fresh(mode, depth); + let before = saved(&s); + s.execute("layer.layerStyle.dropShadow", json!({"angle": -45, "color": [0.1, 0.3, 0.8]})).unwrap(); + let edited = saved(&s); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(saved(&s), before); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(saved(&s), edited); + s.execute("layer.newFillLayer.solidColor", json!({"color": "#4080c0"})).unwrap(); + let bytes = saved(&s); + photocraft_format::load_from_bytes(&bytes).unwrap(); + } + } +} + +#[test] +fn effect_snapshots_cannot_bypass_numeric_validation() { + let mut fx = serde_json::to_value(photocraft_engine::layer_style::effect_from_params("dropShadow", &json!({})).unwrap()).unwrap(); + fx["DropShadow"]["angle"] = json!(1e39); + rejected(vec![( + "layer.layerStyle.replace", + json!({"effects": [ + {"kind": "colorOverlay", "params": {"color": "#00ff00"}}, + {"kind": "dropShadow", "fx": fx, "params": {}} + ]}), + )]); +} + +#[test] +fn direct_effect_helpers_do_not_introduce_infinity() { + let original = photocraft_engine::layer_style::effect_from_params("dropShadow", &json!({})).unwrap(); + for p in [json!({"angle": 1e39}), json!({"color": [0, -1e39, 0]})] { + assert!(photocraft_engine::layer_style::effect_from_params("dropShadow", &p).is_none()); + let mut edited = original.clone(); + photocraft_engine::layer_style::overlay_effect(&mut edited, &p); + assert_eq!(edited, original); + } +} + +#[test] +fn bad_background_color_does_not_partially_change_tool_colors() { + let mut s = fresh("rgb", 32); + let before = (s.tools.foreground, s.tools.background); + let result = s.execute("tools.setColors", json!({"foreground": "#40a0c0", "background": [0, 1e39, 0]})); + assert!(matches!(result, Err(EngineError::BadParams { .. }))); + assert_eq!((s.tools.foreground, s.tools.background), before); +} + +#[test] +fn finite_hdr_colors_are_preserved_without_unit_clamping() { + let mut s = fresh("rgb", 32); + let id = s.execute("layer.newFillLayer.solidColor", json!({"color": [4.0, -0.5, 0.25]})).unwrap()["layer"].as_u64().unwrap(); + let bytes = saved(&s); + let reopened = photocraft_format::load_from_bytes(&bytes).unwrap(); + let layer = reopened.layer(photocraft_doc::LayerId(id)).unwrap(); + let photocraft_doc::LayerContent::Fill(photocraft_doc::Fill::Solid(c)) = &layer.content else { panic!("expected a solid fill") }; + assert_eq!(c.to_rgb(), [4.0, -0.5, 0.25]); +} diff --git a/crates/engine/tests/flatten_background.rs b/crates/engine/tests/it/flatten_background.rs similarity index 100% rename from crates/engine/tests/flatten_background.rs rename to crates/engine/tests/it/flatten_background.rs diff --git a/crates/engine/tests/gallery_colours.rs b/crates/engine/tests/it/gallery_colours.rs similarity index 100% rename from crates/engine/tests/gallery_colours.rs rename to crates/engine/tests/it/gallery_colours.rs diff --git a/crates/engine/tests/gauss_small_sigma.rs b/crates/engine/tests/it/gauss_small_sigma.rs similarity index 100% rename from crates/engine/tests/gauss_small_sigma.rs rename to crates/engine/tests/it/gauss_small_sigma.rs diff --git a/crates/engine/tests/it/gradient_overlay_strokes.rs b/crates/engine/tests/it/gradient_overlay_strokes.rs new file mode 100644 index 0000000000..e233f8984d --- /dev/null +++ b/crates/engine/tests/it/gradient_overlay_strokes.rs @@ -0,0 +1,89 @@ +//! A Gradient Overlay spans one gradient over the layer's current bounds, so a stroke that grows +//! the layer re-lays the gradient out over all of it (#4053). The damage a stroke reports must +//! cover that, or a canvas that redraws only the damage keeps the old gradient around the earlier +//! strokes: one gradient per stroke instead of one over the whole layer. + +use photocraft_engine::Session; +use serde_json::json; + +/// What a canvas that redraws only each edit's damage shows: the cold render before the edit, +/// with the edit's damage (grown by the effects' reach, as the canvas grows it) rendered again. +fn redraw_damage(shown: &mut photocraft_compose::Buffer, s: &Session) { + let st = s.active().unwrap(); + let reach = st.doc.layers.iter().filter(|l| photocraft_compose::effects::has_effects(l)).map(photocraft_compose::effects::margin).max().unwrap_or(0); + let damage = st.last_damage.expect("a stroke reports bounded damage").inflate(reach).intersect(&st.doc.bounds()); + let (w, h) = (st.doc.size.width, st.doc.size.height); + let patch = photocraft_compose::render_reduced_damage(&st.doc, w, h, damage); + for y in patch.rect.y0..patch.rect.y1 { + for x in patch.rect.x0..patch.rect.x1 { + let i = (y * w as i32 + x) as usize; + shown.px[i] = patch.get(x, y); + } + } +} + +fn session(effect: &str, params: serde_json::Value) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 160, "height": 80, "background": "white"})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute(effect, params).unwrap(); + s +} + +const GREY: &str = "#808080"; + +/// Each stroke redrawn over the last cold render equals a cold render after it. +fn strokes_redraw_like_a_cold_render(mut s: Session, strokes: &[serde_json::Value]) { + s.execute("paint.stroke", json!({"points": [[20, 20], [40, 30]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap(); + for (i, p) in strokes.iter().enumerate() { + let st = s.active().unwrap(); + let mut shown = photocraft_compose::render(&st.doc, st.doc.bounds()); + s.execute("paint.stroke", p.clone()).unwrap(); + redraw_damage(&mut shown, &s); + let st = s.active().unwrap(); + let cold = photocraft_compose::render(&st.doc, st.doc.bounds()); + let worst = shown.px.iter().zip(&cold.px).map(|(a, b)| (0..4).map(|c| (a[c] - b[c]).abs()).fold(0.0f32, f32::max)).fold(0.0f32, f32::max); + assert!(worst < 1e-6, "after stroke {i} the redrawn canvas differs from a cold render by {worst}"); + } +} + +#[test] +fn a_gradient_overlay_is_laid_out_again_after_each_stroke() { + let s = session("layer.layerStyle.gradientOverlay", json!({"from": "#ff0000", "to": "#0000ff", "angle": 0})); + strokes_redraw_like_a_cold_render( + s, + &[ + // Far from the first: the layer grows to the right and down. + json!({"points": [[120, 50], [140, 64]], "size": 12, "hardness": 1.0, "color": GREY}), + // Up and to the left of both. + json!({"points": [[6, 6], [10, 8]], "size": 6, "hardness": 1.0, "color": GREY}), + // Erasing the far stroke shrinks the layer again. + json!({"points": [[110, 40], [150, 74]], "size": 40, "hardness": 1.0, "erase": true}), + ], + ); +} + +#[test] +fn a_stroke_inside_the_layer_keeps_its_own_damage() { + let mut s = session("layer.layerStyle.gradientOverlay", json!({"from": "#ff0000", "to": "#0000ff", "angle": 0})); + s.execute("paint.stroke", json!({"points": [[10, 10], [150, 70]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap(); + // Within the layer's bounds: the gradient's frame stays, so only the stroke repaints. + let r = s.execute("paint.stroke", json!({"points": [[60, 40], [70, 40]], "size": 6, "hardness": 1.0, "color": GREY})).unwrap(); + let d = &r["damage"]; + let reported = photocraft_geom::Rect::new( + d[0].as_i64().unwrap() as i32, + d[1].as_i64().unwrap() as i32, + (d[0].as_i64().unwrap() + d[2].as_i64().unwrap()) as i32, + (d[1].as_i64().unwrap() + d[3].as_i64().unwrap()) as i32, + ); + assert_eq!(s.active().unwrap().last_damage, Some(reported)); +} + +#[test] +fn effects_not_laid_out_over_the_layer_keep_the_stroke_damage() { + let mut s = session("layer.layerStyle.colorOverlay", json!({"color": "#00ff00"})); + s.execute("paint.stroke", json!({"points": [[20, 20], [40, 30]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap(); + s.execute("paint.stroke", json!({"points": [[120, 50], [140, 64]], "size": 12, "hardness": 1.0, "color": GREY})).unwrap(); + let d = s.active().unwrap().last_damage.unwrap(); + assert!(d.x0 >= 100, "a Color Overlay repaints only the stroke: {d:?}"); +} diff --git a/crates/engine/tests/guide_layout_limit.rs b/crates/engine/tests/it/guide_layout_limit.rs similarity index 100% rename from crates/engine/tests/guide_layout_limit.rs rename to crates/engine/tests/it/guide_layout_limit.rs diff --git a/crates/engine/tests/guide_layout_preview.rs b/crates/engine/tests/it/guide_layout_preview.rs similarity index 100% rename from crates/engine/tests/guide_layout_preview.rs rename to crates/engine/tests/it/guide_layout_preview.rs diff --git a/crates/engine/tests/hdr_gamma_direction.rs b/crates/engine/tests/it/hdr_gamma_direction.rs similarity index 100% rename from crates/engine/tests/hdr_gamma_direction.rs rename to crates/engine/tests/it/hdr_gamma_direction.rs diff --git a/crates/engine/tests/it/huge_doc_flatten.rs b/crates/engine/tests/it/huge_doc_flatten.rs new file mode 100644 index 0000000000..fd83c1169d --- /dev/null +++ b/crates/engine/tests/it/huge_doc_flatten.rs @@ -0,0 +1,30 @@ +//! Trim and Duplicate › Merged Layers Only flatten the document; on a canvas over the compositor's +//! render cap the flatten comes back empty, and both must report it instead of panicking (#7223). + +use photocraft_engine::Session; +use serde_json::json; + +/// 1.2e9 px: within File › New's per-side limit, over `MAX_RENDER_PIXELS` (2^30). +fn huge() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40000, "height": 30000, "background": "transparent"})).unwrap(); + s +} + +#[test] +fn trim_on_a_document_too_large_to_flatten_is_an_error() { + for based in ["topLeft", "bottomRight", "transparent"] { + let mut s = huge(); + let err = s.execute("image.trim", json!({"basedOn": based})).unwrap_err().to_string(); + assert!(err.contains("too large"), "{based}: {err}"); + assert_eq!(s.active().unwrap().doc.size.width, 40000, "{based}: the document is untouched"); + } +} + +#[test] +fn merged_duplicate_of_a_document_too_large_to_flatten_is_an_error() { + let mut s = huge(); + let err = s.execute("image.duplicate", json!({"mergedOnly": true})).unwrap_err().to_string(); + assert!(err.contains("too large"), "{err}"); + assert_eq!(s.documents().len(), 1, "no half-built copy is added"); +} diff --git a/crates/engine/tests/it/integration_batch.rs b/crates/engine/tests/it/integration_batch.rs new file mode 100644 index 0000000000..14977beece --- /dev/null +++ b/crates/engine/tests/it/integration_batch.rs @@ -0,0 +1,34 @@ +//! Cross-PR acceptance checks for the merged parity batch: each test pins one fix through the +//! public command surface, so a bad merge between the PRs shows up here. +use photocraft_engine::Session; +use serde_json::json; + +fn session(width: u32, height: u32) -> Session { + let mut session = Session::new(); + session.execute("file.new", json!({"width": width, "height": height})).unwrap(); + session +} + +#[test] +fn a_new_fill_layer_consumes_the_selection_it_took_as_mask() { + let mut s = session(32, 24); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 10, "height": 8})).unwrap(); + s.execute("layer.newFillLayer.solidColor", json!({"color": "#4080c0"})).unwrap(); + assert!(s.active().unwrap().doc.selection.is_none()); +} + +#[test] +fn define_pattern_accepts_a_canvas_wider_than_the_old_4000_px_cap() { + let mut s = session(4500, 8); + s.execute("edit.definePattern", json!({"name": "Wide"})).unwrap(); + let mut s = session(8001, 8); + assert!(s.execute("edit.definePattern", json!({"name": "TooWide"})).is_err()); +} + +#[test] +fn gaussian_blur_accepts_radii_below_one_without_failing() { + for radius in [0.1, 0.3, 0.7, 1.0] { + let mut s = session(24, 16); + s.execute("filter.blur.gaussianBlur", json!({"radius": radius})).unwrap(); + } +} diff --git a/crates/engine/tests/jpeg_exif_resolution.rs b/crates/engine/tests/it/jpeg_exif_resolution.rs similarity index 100% rename from crates/engine/tests/jpeg_exif_resolution.rs rename to crates/engine/tests/it/jpeg_exif_resolution.rs diff --git a/crates/engine/tests/keyboard_shortcut_bulk.rs b/crates/engine/tests/it/keyboard_shortcut_bulk.rs similarity index 100% rename from crates/engine/tests/keyboard_shortcut_bulk.rs rename to crates/engine/tests/it/keyboard_shortcut_bulk.rs diff --git a/crates/engine/tests/kra.rs b/crates/engine/tests/it/kra.rs similarity index 96% rename from crates/engine/tests/kra.rs rename to crates/engine/tests/it/kra.rs index 8bdf10531c..783c5fb28f 100644 --- a/crates/engine/tests/kra.rs +++ b/crates/engine/tests/it/kra.rs @@ -1,7 +1,7 @@ //! Krita documents opened through the session: read-only source, 16-bit layers, editable //! layers with history, and saving as OpenRaster or native. The files come from //! `tools/krita-oracles/generate.py` in https://github.com/storytold/photocraft-corpus. Run with -//! `KRITA_TEST_DIR=

cargo test -p photocraft-engine --test kra -- --ignored --nocapture`. +//! `KRITA_TEST_DIR= cargo test -p photocraft-engine --test it -- kra:: --ignored --nocapture`. use photocraft_engine::Session; use serde_json::json; @@ -24,7 +24,7 @@ fn krita_documents_open_read_only_and_stay_editable() { assert!(session.execute("file.save", json!({})).is_err()); // Krita's merged image is the composite (the corpus files carry no ICC profile, so both - // sides are sRGB); crates/io/tests/krita_corpus.rs compares every file. + // sides are sRGB); crates/io/tests/it/krita_corpus.rs compares every file. let ours = photocraft_compose::flatten(&session.active().unwrap().doc); let zip_bytes = std::fs::read(&path).unwrap(); let zip = photocraft_format::zip::ZipReader::new(&zip_bytes).unwrap(); diff --git a/crates/engine/tests/kys_import.rs b/crates/engine/tests/it/kys_import.rs similarity index 100% rename from crates/engine/tests/kys_import.rs rename to crates/engine/tests/it/kys_import.rs diff --git a/crates/engine/tests/layer_delete_last.rs b/crates/engine/tests/it/layer_delete_last.rs similarity index 100% rename from crates/engine/tests/layer_delete_last.rs rename to crates/engine/tests/it/layer_delete_last.rs diff --git a/crates/engine/tests/layer_mask_depth.rs b/crates/engine/tests/it/layer_mask_depth.rs similarity index 92% rename from crates/engine/tests/layer_mask_depth.rs rename to crates/engine/tests/it/layer_mask_depth.rs index 7093164a5f..cbe4138532 100644 --- a/crates/engine/tests/layer_mask_depth.rs +++ b/crates/engine/tests/it/layer_mask_depth.rs @@ -45,12 +45,10 @@ fn newly_created_layer_masks_follow_document_depth() { assert_eq!(m.value(20, 20), white); if selected { assert_eq!(m.value(0, 0), 0.0); - if command == "layer.layerMask.revealSelection" { - // The selection became the mask: Photoshop drops it, as Hide Selection does. - assert!(d.doc.selection.is_none(), "{command}: the selection is dropped"); - } else { - assert_eq!(d.doc.selection, before.selection, "the selection is unchanged"); - } + // The selection became the mask: Photoshop drops it, as Hide Selection does + // (#5251; new fill and adjustment layers too, #8801). + assert!(d.doc.selection.is_none(), "{command}: the selection is dropped"); + assert!(before.selection.is_some()); } else { assert_eq!(m.surface.tile_count(), 0, "uniform masks stay sparse"); assert_eq!(m.value(-300_000, 300_000), white); @@ -72,7 +70,6 @@ fn soft_mask_edits_match_after_native_and_psd_reopen() { let mut s = session(depth, "rgb"); s.execute("select.rect", json!({"x": 8, "y": 6, "width": 40, "height": 32, "antiAlias": false})).unwrap(); s.execute("layer.newFillLayer.solidColor", json!({"color": "#000000"})).unwrap(); - s.execute("select.deselect", json!({})).unwrap(); let before = s.active().unwrap().doc.clone(); let native = photocraft_format::save_to_bytes(&before, &Default::default()).unwrap(); let psd = photocraft_io::export(&before, "psd", &Default::default()).unwrap(); @@ -138,7 +135,8 @@ fn mask_creation_preserves_selection_coverage_and_existing_masks() { before.selection.as_ref().unwrap().read_region(before.bounds()), "{depth}-bit coverage is unchanged" ); - let kept = if command == "layer.layerMask.revealSelection" { None } else { before.selection.clone() }; + // The selection went into the mask, except for the Gradient tool's fill layer. + let kept = if command == "gradient.fill.create" { before.selection.clone() } else { None }; assert_eq!(after.selection, kept, "{command}"); assert_eq!(after.layers[0].mask, before.layers[0].mask, "creating a mask does not requantize an existing mask"); } diff --git a/crates/engine/tests/layer_props_several.rs b/crates/engine/tests/it/layer_props_several.rs similarity index 100% rename from crates/engine/tests/layer_props_several.rs rename to crates/engine/tests/it/layer_props_several.rs diff --git a/crates/engine/tests/layer_range_panel_order.rs b/crates/engine/tests/it/layer_range_panel_order.rs similarity index 83% rename from crates/engine/tests/layer_range_panel_order.rs rename to crates/engine/tests/it/layer_range_panel_order.rs index 0e6199d539..86b4ad81c8 100644 --- a/crates/engine/tests/layer_range_panel_order.rs +++ b/crates/engine/tests/it/layer_range_panel_order.rs @@ -20,7 +20,10 @@ fn shift_range_follows_visible_rows_on_both_sides_of_an_expanded_group() { select(&mut s, b, "replace"); select(&mut s, c, "add"); let group = s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap(); + // D goes above the closed group (an open one would take it inside, as in Photoshop). let d = s.execute("layer.new.layer", json!({"name": "D"})).unwrap()["layer"].as_u64().unwrap(); + // Group Layers makes a collapsed group, as in Photoshop; open it so B and C are visible rows. + s.execute("layer.setExpanded", json!({"layer": group, "expanded": true})).unwrap(); for (anchor, target, wanted) in [(d, group, vec![d, group]), (group, d, vec![d, group]), (group, b, vec![group, c, b]), (b, group, vec![group, c, b]), (c, b, vec![c, b])] diff --git a/crates/engine/tests/live_line_transform.rs b/crates/engine/tests/it/live_line_transform.rs similarity index 100% rename from crates/engine/tests/live_line_transform.rs rename to crates/engine/tests/it/live_line_transform.rs diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs new file mode 100644 index 0000000000..f6a8360a4f --- /dev/null +++ b/crates/engine/tests/it/main.rs @@ -0,0 +1,84 @@ +//! The crate's integration tests, linked into one test binary. + +mod affinity; +mod artboard_export_names; +mod atn_crystallize_cell_size; +mod atn_ripple_amount; +mod atn_selective_color; +mod atn_stroke_width; +mod background_and_mask_depth; +mod background_layer_consistency; +mod brush_smoothing; +mod camera_raw_exposure_clamp; +mod camera_raw_noise_clamp; +mod clipping_semantics; +mod close_document; +mod command_lookup; +mod convert_keeps_references; +mod copy_shape_placement; +mod corpus_atn; +mod crop_content_aware; +mod current_affinity_artboards; +mod duplicate_layer_comps; +mod duplicate_slices; +mod duplicate_variables; +mod empty_selection_combine; +mod eraser_locked; +mod explicit_layer_target; +mod export_shape_strokes; +mod fill_lock_transparency; +mod filter_locked; +mod finite_command_params; +mod flatten_background; +mod gallery_colours; +mod gauss_small_sigma; +mod gradient_overlay_strokes; +mod guide_layout_limit; +mod guide_layout_preview; +mod hdr_gamma_direction; +mod huge_doc_flatten; +mod integration_batch; +mod jpeg_exif_resolution; +mod keyboard_shortcut_bulk; +mod kra; +mod kys_import; +mod layer_delete_last; +mod layer_mask_depth; +mod layer_props_several; +mod layer_range_panel_order; +mod live_line_transform; +mod mask_lock_all; +mod merge_transparent_bounds; +mod missing_layer_error; +mod nonfinite_style; +mod open_identity; +mod path_blur_speed_clamp; +mod path_transform_target; +mod pdn; +mod photoshop_oracles; +mod pixel_lock_edits; +mod precheck; +mod prefs_usage; +mod rasterize_psd_reopen; +mod rasterize_selected_layers; +mod rendering_preferences; +mod save_keeps_encoding; +mod selection_layer_commands; +mod shortcut_sets; +mod shortcuts_headless; +mod smart_clipboard_sources; +mod smart_duplicates; +mod smart_filter_canvas_geometry; +mod smart_object_tones; +mod smart_psd; +mod style_and_align; +mod svg_smart_object; +mod transform_budget_unclamped; +mod type_format_targets; +mod type_postscript_font; +mod undo_keeps_active_layer; +mod unicode_colors; +mod unsupported_smart_filters; +mod wand_transparent_copy; +mod working_rgb_profile; +mod xmp_charref; diff --git a/crates/engine/tests/it/mask_lock_all.rs b/crates/engine/tests/it/mask_lock_all.rs new file mode 100644 index 0000000000..74ea71ff62 --- /dev/null +++ b/crates/engine/tests/it/mask_lock_all.rs @@ -0,0 +1,92 @@ +use photocraft_doc::LayerMask; +use photocraft_engine::Session; +use serde_json::json; + +const COMMANDS: [&str; 7] = [ + "layer.layerMask.revealAll", + "layer.layerMask.hideAll", + "layer.layerMask.revealSelection", + "layer.layerMask.hideSelection", + "layer.layerMask.delete", + "layer.maskAllObjects", + "image.adjustments.invert", +]; + +fn masked(all: bool) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("paint.stroke", json!({"points": [[25, 20], [35, 20]], "size": 6, "hardness": 1.0, "color": "#000000", "target": "mask"})).unwrap(); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 20, "height": 15})).unwrap(); + s.execute("layer.setProps", json!({"locks": {"all": all}})).unwrap(); + s +} + +fn mask(s: &Session) -> Option { + let st = s.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().mask.clone() +} + +fn undo_depth(s: &Session) -> usize { + s.active().unwrap().history.past_len() +} + +#[test] +fn lock_all_refuses_every_mask_command() { + for cmd in COMMANDS { + let mut s = masked(true); + let before = mask(&s); + let undo = undo_depth(&s); + let err = s.execute(cmd, json!({"target": "mask"})).unwrap_err(); + assert_eq!(err.to_string(), "Could not complete your request because the layer \"Layer 1\" is locked", "{cmd}"); + assert_eq!(mask(&s), before, "{cmd} left the mask unchanged"); + assert_eq!(undo_depth(&s), undo, "{cmd} added no history step"); + } +} + +#[test] +fn unlocked_layers_still_take_every_mask_command() { + for cmd in COMMANDS { + let mut s = masked(false); + let before = mask(&s); + s.execute(cmd, json!({"target": "mask"})).unwrap_or_else(|e| panic!("{cmd}: {e}")); + assert_ne!(mask(&s), before, "{cmd} changed the mask"); + } +} + +#[test] +fn a_locked_layer_among_the_selected_layers_refuses_the_whole_mask() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let first = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.setProps", json!({"locks": {"all": true}})).unwrap(); + s.execute("layer.select", json!({"layer": first.0, "mode": "add"})).unwrap(); + assert_eq!(s.active().unwrap().selected_layers.len(), 2); + assert!(s.execute("layer.layerMask.hideAll", json!({})).is_err()); + assert!(s.active().unwrap().doc.walk().iter().all(|(_, _, l)| l.mask.is_none()), "no layer got a mask"); +} + +#[test] +fn paste_into_a_lock_all_group_refuses_before_pasting() { + for cmd in ["edit.pasteSpecial.pasteInto", "edit.pasteSpecial.pasteOutside"] { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + let child = s.execute("layer.new.layer", json!({"name": "Child"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [20, 10]], "size": 8, "hardness": 1.0, "color": "#ff0000"})).unwrap(); + s.execute("select.all", json!({})).unwrap(); + s.execute("edit.copy", json!({})).unwrap(); + let group = s.execute("layer.groupLayers", json!({})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.setProps", json!({"layer": group, "locks": {"all": true}})).unwrap(); + s.execute("layer.select", json!({"layer": child, "mode": "replace"})).unwrap(); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 20, "height": 15})).unwrap(); + let layers = s.active().unwrap().doc.walk().len(); + let undo = undo_depth(&s); + assert!(s.execute(cmd, json!({})).is_err(), "{cmd}"); + assert_eq!(s.active().unwrap().doc.walk().len(), layers, "{cmd} pasted no layer"); + assert_eq!(undo_depth(&s), undo, "{cmd} added no history step"); + } +} diff --git a/crates/engine/tests/merge_transparent_bounds.rs b/crates/engine/tests/it/merge_transparent_bounds.rs similarity index 99% rename from crates/engine/tests/merge_transparent_bounds.rs rename to crates/engine/tests/it/merge_transparent_bounds.rs index c796007233..03e9473fb2 100644 --- a/crates/engine/tests/merge_transparent_bounds.rs +++ b/crates/engine/tests/it/merge_transparent_bounds.rs @@ -101,7 +101,6 @@ fn merging_a_masked_fill_layer_keeps_content_bounds_in_every_mode() { let (mut s, a, _) = scene(mode, depth); s.execute("select.rect", json!({"x": B[0], "y": B[1], "width": B[2], "height": B[3]})).unwrap(); let fill = id_of(&s.execute("layer.newFillLayer.solidColor", json!({"color": "#3060c0"})).unwrap()); - s.execute("select.deselect", json!({})).unwrap(); (s, a, fill) }; for &(mode, depth) in CASES { diff --git a/crates/engine/tests/it/missing_layer_error.rs b/crates/engine/tests/it/missing_layer_error.rs new file mode 100644 index 0000000000..5764f5d85c --- /dev/null +++ b/crates/engine/tests/it/missing_layer_error.rs @@ -0,0 +1,68 @@ +//! A `"layer"` param that names no layer says which layers there are (#5058): ids are not +//! positions (a freshly opened PNG's Background is layer 2), and an agent should be able to +//! recover from the error alone. A name matches regardless of case as a fallback. + +use photocraft_engine::{EngineError, Session}; +use serde_json::json; + +fn session() -> (Session, u64) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 6})).unwrap(); + let bg = s.active().unwrap().active_layer.unwrap().0; + (s, bg) +} + +fn unused_id(s: &Session) -> u64 { + let doc = &s.active().unwrap().doc; + (1..).find(|id| doc.layer(photocraft_doc::LayerId(*id)).is_none()).unwrap() +} + +#[test] +fn an_unknown_layer_id_lists_the_documents_layers() { + let (mut s, bg) = session(); + let layer = s.execute("layer.new.layer", json!({})).unwrap()["layer"].as_u64().unwrap(); + let missing = unused_id(&s); + let err = s.execute("layer.setProps", json!({"layer": missing, "visible": false})).unwrap_err(); + assert!(matches!(err, EngineError::UnknownLayer { id, .. } if id.0 == missing), "{err}"); + let err = err.to_string(); + assert!(err.contains(&format!("no such layer {missing};")), "{err}"); + assert!(err.contains(&format!("{layer} \"Layer 1\", {bg} \"Background\"")), "top-down ids and names: {err}"); +} + +#[test] +fn an_unknown_layer_name_lists_the_documents_layers() { + let (mut s, bg) = session(); + let err = s.execute("layer.setProps", json!({"layer": "Nope", "visible": false})).unwrap_err().to_string(); + assert!(err.contains("no layer named \"Nope\""), "{err}"); + assert!(err.contains(&format!("{bg} \"Background\"")), "{err}"); +} + +#[test] +fn a_layer_name_matches_regardless_of_case() { + let (mut s, bg) = session(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.setProps", json!({"layer": "background", "name": "Base"})).unwrap(); + assert_eq!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(bg)).unwrap().name, "Base"); +} + +#[test] +fn an_exact_name_wins_over_a_case_insensitive_one() { + let (mut s, _) = session(); + let lower = s.execute("layer.new.layer", json!({"name": "shadow"})).unwrap()["layer"].as_u64().unwrap(); + let upper = s.execute("layer.new.layer", json!({"name": "Shadow"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.setProps", json!({"layer": "shadow", "visible": false})).unwrap(); + let doc = &s.active().unwrap().doc; + assert!(!doc.layer(photocraft_doc::LayerId(lower)).unwrap().visible); + assert!(doc.layer(photocraft_doc::LayerId(upper)).unwrap().visible); +} + +#[test] +fn a_long_layer_name_is_shortened_in_the_listing() { + let (mut s, _) = session(); + let long = "é".repeat(500); + let layer = s.execute("layer.new.layer", json!({"name": long})).unwrap()["layer"].as_u64().unwrap(); + let err = s.execute("layer.setProps", json!({"layer": "Nope", "visible": false})).unwrap_err().to_string(); + let shown = format!("{layer} \"{}…\"", "é".repeat(64)); + assert!(err.contains(&shown), "the name is cut at 64 characters: {err}"); + assert!(err.chars().count() < 300, "{err}"); +} diff --git a/crates/engine/tests/it/nonfinite_style.rs b/crates/engine/tests/it/nonfinite_style.rs new file mode 100644 index 0000000000..994f3dbe6e --- /dev/null +++ b/crates/engine/tests/it/nonfinite_style.rs @@ -0,0 +1,56 @@ +//! Character style numbers that overflow `f32` are refused, so the document stays saveable. + +use photocraft_engine::{EngineError, Session}; +use serde_json::json; + +fn fixture() -> (Session, u64) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); + let id = s.execute("type.create", json!({"text": "Hi", "size": 24})).unwrap()["layer"].as_u64().unwrap(); + (s, id) +} + +#[test] +fn out_of_range_style_numbers_are_refused() { + for key in ["leading", "baselineShift", "horizontalScale", "verticalScale"] { + let (mut s, id) = fixture(); + let past = s.active().unwrap().history.past_len(); + for value in [1e39, -1e39, 1e12] { + let r = s.execute("type.setStyle", json!({"layer": id, key: value})); + assert!(matches!(r, Err(EngineError::BadParams { .. })), "{key}={value}: {r:?}"); + } + assert_eq!(s.active().unwrap().history.past_len(), past, "{key}: no history step"); + let r = s.execute("type.create", json!({"text": "x", key: 1e39})); + assert!(matches!(r, Err(EngineError::BadParams { .. })), "create {key}: {r:?}"); + let dir = std::env::temp_dir().join(format!("pc_nonfinite_style_{key}_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("t.pcraft"); + let saved = s.execute("file.saveACopy", json!({"path": path.to_string_lossy()})); + std::fs::remove_dir_all(&dir).ok(); + saved.unwrap_or_else(|e| panic!("{key}: save failed: {e:?}")); + } +} + +#[test] +fn in_range_style_numbers_still_apply_and_save() { + let (mut s, id) = fixture(); + s.execute("type.setStyle", json!({"layer": id, "leading": 30, "baselineShift": -4, "horizontalScale": 120, "verticalScale": 80})).unwrap(); + s.execute("type.setStyle", json!({"layer": id, "leading": "auto"})).unwrap(); + let dir = std::env::temp_dir().join(format!("pc_nonfinite_style_ok_{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("t.pcraft"); + let saved = s.execute("file.saveACopy", json!({"path": path.to_string_lossy()})); + std::fs::remove_dir_all(&dir).ok(); + saved.unwrap_or_else(|e| panic!("save failed: {e:?}")); +} + +#[test] +fn non_numeric_leading_is_refused() { + let (mut s, id) = fixture(); + let past = s.active().unwrap().history.past_len(); + for value in [json!("18pt"), json!("Auto "), json!(true), json!([12])] { + let r = s.execute("type.setStyle", json!({"layer": id, "leading": value})); + assert!(matches!(r, Err(EngineError::BadParams { .. })), "leading={value}: {r:?}"); + } + assert_eq!(s.active().unwrap().history.past_len(), past, "no history step"); +} diff --git a/crates/engine/tests/it/open_identity.rs b/crates/engine/tests/it/open_identity.rs new file mode 100644 index 0000000000..2fa4ae17f6 --- /dev/null +++ b/crates/engine/tests/it/open_identity.rs @@ -0,0 +1,51 @@ +//! Background imports report the admitted document's identity, not just its temporary tab index. + +use std::sync::Arc; + +use photocraft_engine::Session; +use photocraft_engine::jobs::{OpenSource, Started}; +use serde_json::{Value, json}; + +fn source() -> Session { + let mut session = Session::new(); + session.execute("file.new", json!({"width": 8, "height": 6})).unwrap(); + session.execute("layer.new.layer", json!({})).unwrap(); + session.execute("edit.fill", json!({"color": "#336699"})).unwrap(); + session +} + +fn open(session: &mut Session, name: &str, bytes: Vec) -> Value { + match session.start_open(name, OpenSource::Bytes(Arc::new(bytes))).unwrap() { + Started::Done(result) => result, + Started::Job(job) => session.wait_job(job).unwrap(), + } +} + +#[test] +fn open_identity_reports_the_live_document_after_tabs_move() { + let input = source(); + let bytes = photocraft_io::export(&input.active().unwrap().doc, "synthetic.png", &Default::default()).unwrap().bytes; + let mut session = source(); + let result = open(&mut session, "synthetic.png", bytes); + let index = result["document"].as_u64().unwrap() as usize; + let imported = session.documents()[index].doc.id; + assert_eq!(result["docId"], imported.0, "the engine result must carry the actual admitted identity: {result}"); + session.move_document(index, 0).unwrap(); + assert_eq!(session.documents()[0].doc.id.0, result["docId"]); + assert_ne!(session.documents()[index].doc.id, imported, "the old index now identifies another document"); +} + +#[test] +fn open_identity_reports_the_remapped_id_when_a_native_document_is_opened_twice() { + let mut session = source(); + let original = session.active().unwrap().doc.clone(); + // A wholly synthetic in-memory native file retains its DocId. PNG and DNG allocate fresh + // IDs during decode, so they cannot deterministically exercise this collision. + let bytes = photocraft_format::PcraftWriter::new().save_zip(&original, &Default::default()).unwrap().0; + let result = open(&mut session, "synthetic.pcraft", bytes); + let index = result["document"].as_u64().unwrap() as usize; + let admitted = session.documents()[index].doc.id; + assert_ne!(admitted, original.id, "the original document still owns its persisted ID"); + assert_eq!(result["docId"], admitted.0, "report the remapped identity, not the ID decoded from the file"); + assert!(Arc::ptr_eq(&session.documents()[0].doc, &original)); +} diff --git a/crates/engine/tests/path_blur_speed_clamp.rs b/crates/engine/tests/it/path_blur_speed_clamp.rs similarity index 100% rename from crates/engine/tests/path_blur_speed_clamp.rs rename to crates/engine/tests/it/path_blur_speed_clamp.rs diff --git a/crates/engine/tests/path_transform_target.rs b/crates/engine/tests/it/path_transform_target.rs similarity index 100% rename from crates/engine/tests/path_transform_target.rs rename to crates/engine/tests/it/path_transform_target.rs diff --git a/crates/engine/tests/pdn.rs b/crates/engine/tests/it/pdn.rs similarity index 100% rename from crates/engine/tests/pdn.rs rename to crates/engine/tests/it/pdn.rs diff --git a/crates/engine/tests/photoshop_oracles.rs b/crates/engine/tests/it/photoshop_oracles.rs similarity index 81% rename from crates/engine/tests/photoshop_oracles.rs rename to crates/engine/tests/it/photoshop_oracles.rs index 2edd13b2ba..88e63b67fc 100644 --- a/crates/engine/tests/photoshop_oracles.rs +++ b/crates/engine/tests/it/photoshop_oracles.rs @@ -3,7 +3,7 @@ //! `cargo xtask corpus --all`. Feature `corpus` (run with `cargo xtask test-corpus`); a missing //! corpus fails. //! -//! The io corpus test (`crates/io/tests/corpus.rs`) flattens imported documents, which composite +//! The io corpus test (`crates/io/tests/it/corpus.rs`) flattens imported documents, which composite //! Photoshop's own cached pixels for smart objects and type layers. This test throws those caches //! away: every smart object is re-rendered from its embedded source through our smart-filter //! stack, and every type layer is re-laid-out and re-rasterised by our text engine; the flatten is @@ -80,6 +80,20 @@ fn rerender(doc: &mut Document) -> Result { Ok(n) } +/// Re-rendering Photoshop's Screen/Motion Blur oracle matches its merged composite, so the result retains the input's coverage. +#[test] +fn screen_motion_blur_retains_input_coverage() { + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop/smart-filters/sf-blend-screen-100.psd"); + let bytes = std::fs::read(&path).expect("Screen/Motion Blur oracle missing: run cargo xtask corpus --all"); + let file = PsdFile::from_bytes(&bytes).expect("parse Screen/Motion Blur oracle"); + let mut doc = photocraft_io::import("sf-blend-screen-100.psd", &bytes).expect("import Screen/Motion Blur oracle").document; + assert!(rerender(&mut doc).expect("re-render Screen/Motion Blur oracle") > 0); + let merged = photocraft_io::merged_composite(&file).expect("Photoshop merged composite"); + let (error, bad) = max_diff(&photocraft_compose::flatten(&doc).px, &merged); + assert!(error <= PASS_TOL, "Screen/Motion Blur error {error}, {bad}% pixels exceed 2/255"); +} + +/// Re-render every Photoshop oracle in the corpus with PhotoCraft's filters and compare it with Photoshop's own merged composite. #[test] fn photoshop_oracles_rerendered() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop"); diff --git a/crates/engine/tests/pixel_lock_edits.rs b/crates/engine/tests/it/pixel_lock_edits.rs similarity index 100% rename from crates/engine/tests/pixel_lock_edits.rs rename to crates/engine/tests/it/pixel_lock_edits.rs diff --git a/crates/engine/tests/precheck.rs b/crates/engine/tests/it/precheck.rs similarity index 54% rename from crates/engine/tests/precheck.rs rename to crates/engine/tests/it/precheck.rs index f549fbb8c8..b737ddea5a 100644 --- a/crates/engine/tests/precheck.rs +++ b/crates/engine/tests/it/precheck.rs @@ -7,28 +7,32 @@ use serde_json::{Value, json}; /// Documents in different states: a Background only, a pixel layer active, a group active, a /// selection, no document. -fn states() -> Vec<(&'static str, Session)> { - let mut out = Vec::new(); - out.push(("no document", Session::new())); +const STATES: [&str; 5] = ["no document", "background only", "pixel layer", "group, no background", "selection"]; + +/// A fresh session in `state`. +fn state(state: &str) -> Session { let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); - out.push(("background only", s)); - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - out.push(("pixel layer", s)); - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30, "background": "transparent"})).unwrap(); - s.execute("layer.new.group", json!({})).unwrap(); - out.push(("group, no background", s)); - let mut s = Session::new(); - s.execute("file.new", json!({"width": 40, "height": 30, "background": "white"})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); - s.execute("select.rect", json!({"x": 2, "y": 2, "width": 10, "height": 10})).unwrap(); - out.push(("selection", s)); - out + if state == "no document" { + return s; + } + let background = if state == "group, no background" { "transparent" } else { "white" }; + s.execute("file.new", json!({"width": 40, "height": 30, "background": background})).unwrap(); + match state { + "pixel layer" => { + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + } + "group, no background" => { + s.execute("layer.new.group", json!({})).unwrap(); + } + "selection" => { + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#00ff00"})).unwrap(); + s.execute("select.rect", json!({"x": 2, "y": 2, "width": 10, "height": 10})).unwrap(); + } + _ => {} + } + s } fn params() -> Vec { @@ -63,13 +67,13 @@ fn precheck_agrees_with_execute() { let mut disagreements = Vec::new(); for id in PRECHECKED { for p in params() { - for (state, _) in states() { - // A fresh copy of the state for each call: execute changes it. - let mut s = states().into_iter().find(|(n, _)| *n == state).map(|(_, s)| s).unwrap(); + for name in STATES { + // A fresh session for each call: execute changes it. + let mut s = state(name); let pre = s.precheck(id, &p).is_ok(); let ran = s.execute(id, p.clone()).is_ok(); if pre != ran { - disagreements.push(format!("{id} {p} on {state}: precheck {pre}, execute {ran}")); + disagreements.push(format!("{id} {p} on {name}: precheck {pre}, execute {ran}")); } } } diff --git a/crates/engine/tests/prefs_usage.rs b/crates/engine/tests/it/prefs_usage.rs similarity index 77% rename from crates/engine/tests/prefs_usage.rs rename to crates/engine/tests/it/prefs_usage.rs index 7b9adebdd1..eabad21ed8 100644 --- a/crates/engine/tests/prefs_usage.rs +++ b/crates/engine/tests/it/prefs_usage.rs @@ -44,15 +44,36 @@ fn has_ident(text: &str, prefix: &str, ident: &str) -> bool { }) } -/// Production source of a file: everything before its unit-test module. -fn production(text: &str) -> &str { +/// Production source of a file: the file without its inline unit-test modules +/// (`#[cfg(test)] mod tests { … }` at the top level, closed by `}` at column 0). A declaration +/// (`#[cfg(test)] mod tests;`) can come before the rest of the code, so it doesn't end the scan. +fn production(text: &str) -> String { // Windows checkouts may use CRLF; test-only reads must stay excluded. - let cut = ["#[cfg(test)]\nmod ", "#[cfg(test)]\r\nmod ", "#[cfg(test)]\npub mod ", "#[cfg(test)]\r\npub mod "] - .into_iter() - .filter_map(|marker| text.find(marker)) - .min() - .unwrap_or(text.len()); - &text[..cut] + let marker = "#[cfg(test)]"; + let mut out = String::with_capacity(text.len()); + let mut rest = text; + while let Some(at) = rest.match_indices(marker).map(|(i, _)| i).find(|&i| i == 0 || rest[..i].ends_with('\n')) { + let after = &rest[at + marker.len()..]; + let item = after.trim_start(); + let item = item.strip_prefix("pub ").unwrap_or(item); + let body = item.strip_prefix("mod ").and_then(|m| { + let name_end = m.find(|c: char| !c.is_alphanumeric() && c != '_')?; + m[name_end..].trim_start().strip_prefix('{') + }); + let Some(body) = body else { + out.push_str(&rest[..at + marker.len()]); + rest = after; + continue; + }; + out.push_str(&rest[..at]); + let Some(end) = body.match_indices("\n}").map(|(i, _)| i + 2).find(|&e| matches!(body[e..].chars().next(), None | Some('\r' | '\n'))) else { + return out; + }; + let tail = &body[end..]; + rest = tail.strip_prefix("\r\n").or_else(|| tail.strip_prefix('\n')).unwrap_or(tail); + } + out.push_str(rest); + out } fn rust_sources(dir: &Path, out: &mut Vec) { @@ -96,12 +117,22 @@ fn every_preference_is_read_or_hidden() { let mut files = Vec::new(); rust_sources(&root.join("crates"), &mut files); rust_sources(&root.join("apps"), &mut files); - let sources: Vec = files - .iter() - .filter(|p| !p.ends_with("engine/src/prefs.rs")) - .filter_map(|p| std::fs::read_to_string(p).ok()) - .map(|t| production(&t).to_string()) + let prefs_dir = root.join("crates/engine/src/prefs"); + let files: Vec<(PathBuf, String)> = files + .into_iter() + .filter(|p| !p.ends_with("engine/src/prefs.rs") && !p.starts_with(&prefs_dir)) + .filter_map(|p| std::fs::read_to_string(&p).ok().map(|t| (p, production(&t)))) .collect(); + let mut sources: Vec = files.iter().map(|(_, t)| t.clone()).collect(); + // A module split into child files (`foo.rs` + `foo/*.rs`) counts as one file too, so a section + // named in the parent and a field read in a child still count as a read. + for (path, text) in &files { + let dir = path.with_extension(""); + let children: Vec<&str> = files.iter().filter(|(p, _)| p.starts_with(&dir)).map(|(_, t)| t.as_str()).collect(); + if !children.is_empty() { + sources.push([text.as_str()].into_iter().chain(children).collect::>().join("\n")); + } + } assert!(sources.len() > 100, "found only {} source files under {}", sources.len(), root.display()); // Section key → struct name, from `pub : ,` in `struct Preferences`. diff --git a/crates/engine/tests/rasterize_psd_reopen.rs b/crates/engine/tests/it/rasterize_psd_reopen.rs similarity index 100% rename from crates/engine/tests/rasterize_psd_reopen.rs rename to crates/engine/tests/it/rasterize_psd_reopen.rs diff --git a/crates/engine/tests/rasterize_selected_layers.rs b/crates/engine/tests/it/rasterize_selected_layers.rs similarity index 100% rename from crates/engine/tests/rasterize_selected_layers.rs rename to crates/engine/tests/it/rasterize_selected_layers.rs diff --git a/crates/engine/tests/rendering_preferences.rs b/crates/engine/tests/it/rendering_preferences.rs similarity index 100% rename from crates/engine/tests/rendering_preferences.rs rename to crates/engine/tests/it/rendering_preferences.rs diff --git a/crates/engine/tests/save_keeps_encoding.rs b/crates/engine/tests/it/save_keeps_encoding.rs similarity index 100% rename from crates/engine/tests/save_keeps_encoding.rs rename to crates/engine/tests/it/save_keeps_encoding.rs diff --git a/crates/engine/tests/selection_layer_commands.rs b/crates/engine/tests/it/selection_layer_commands.rs similarity index 100% rename from crates/engine/tests/selection_layer_commands.rs rename to crates/engine/tests/it/selection_layer_commands.rs diff --git a/crates/engine/tests/shortcut_sets.rs b/crates/engine/tests/it/shortcut_sets.rs similarity index 100% rename from crates/engine/tests/shortcut_sets.rs rename to crates/engine/tests/it/shortcut_sets.rs diff --git a/crates/engine/tests/shortcuts_headless.rs b/crates/engine/tests/it/shortcuts_headless.rs similarity index 100% rename from crates/engine/tests/shortcuts_headless.rs rename to crates/engine/tests/it/shortcuts_headless.rs diff --git a/crates/engine/tests/smart_clipboard_sources.rs b/crates/engine/tests/it/smart_clipboard_sources.rs similarity index 100% rename from crates/engine/tests/smart_clipboard_sources.rs rename to crates/engine/tests/it/smart_clipboard_sources.rs diff --git a/crates/engine/tests/smart_duplicates.rs b/crates/engine/tests/it/smart_duplicates.rs similarity index 100% rename from crates/engine/tests/smart_duplicates.rs rename to crates/engine/tests/it/smart_duplicates.rs diff --git a/crates/engine/tests/smart_filter_canvas_geometry.rs b/crates/engine/tests/it/smart_filter_canvas_geometry.rs similarity index 100% rename from crates/engine/tests/smart_filter_canvas_geometry.rs rename to crates/engine/tests/it/smart_filter_canvas_geometry.rs diff --git a/crates/engine/tests/it/smart_object_tones.rs b/crates/engine/tests/it/smart_object_tones.rs new file mode 100644 index 0000000000..3c57526bad --- /dev/null +++ b/crates/engine/tests/it/smart_object_tones.rs @@ -0,0 +1,54 @@ +use photocraft_doc::{LayerContent, LayerId}; +use photocraft_engine::{Session, smart_cmds}; +use photocraft_raster::Surface; +use serde_json::json; + +fn mean_difference(a: &Surface, b: &Surface) -> f64 { + let r = a.content_bounds().intersect(&b.content_bounds()); + let (mut sum, mut n) = (0f64, 0u32); + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let (p, q) = (a.rgba(x, y), b.rgba(x, y)); + for c in 0..3 { + sum += f64::from((p[c] - q[c]).abs()); + n += 1; + } + } + } + sum / f64::from(n.max(1)) * 255.0 +} + +fn converted(cmd: &str) -> (Session, LayerId, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 10], [60, 40]], "size": 20, "hardness": 0.3, "color": "#e04a20"})).unwrap(); + s.execute("paint.stroke", json!({"points": [[5, 40], [60, 5]], "size": 14, "hardness": 0.6, "color": "#2a7fd0"})).unwrap(); + let pixel = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.duplicate", json!({})).unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let smart = s.active().unwrap().active_layer.unwrap(); + s.execute(cmd, json!({})).unwrap(); + (s, pixel, smart) +} + +#[test] +fn smart_objects_re_render_with_the_tones_of_the_converted_document() { + for cmd in ["image.mode.bits32", "image.mode.grayscale"] { + let (s, pixel, smart) = converted(cmd); + let st = s.active().unwrap(); + let layer = st.doc.layer(smart).unwrap().clone(); + let mut rerendered = layer.clone(); + smart_cmds::refresh_layer(&st.doc, &mut rerendered).unwrap(); + let (LayerContent::Smart(stored), LayerContent::Smart(fresh)) = (&layer.content, &rerendered.content) else { panic!("not a smart object") }; + let (stored, fresh) = (stored.cache.as_ref().unwrap(), fresh.cache.as_ref().unwrap()); + let reference = st.doc.layer(pixel).unwrap().surface().unwrap(); + let drift = mean_difference(stored, fresh); + assert!(drift < 1.0, "{cmd}: a re-render drifts {drift:.2}/255 from the converted rendering"); + let (off, baseline) = (mean_difference(fresh, reference), mean_difference(stored, reference)); + assert!( + off <= baseline + 0.5, + "{cmd}: the re-render is {off:.2}/255 from a pixel layer converted the same way, the converted rendering {baseline:.2}/255" + ); + } +} diff --git a/crates/engine/tests/smart_psd.rs b/crates/engine/tests/it/smart_psd.rs similarity index 78% rename from crates/engine/tests/smart_psd.rs rename to crates/engine/tests/it/smart_psd.rs index 2586f24cc9..542220f34b 100644 --- a/crates/engine/tests/smart_psd.rs +++ b/crates/engine/tests/it/smart_psd.rs @@ -331,7 +331,7 @@ fn photoshop_samples() { } /// PSD export of a 24 MP smart object versus the same layer as pixels (release: -/// `cargo test --release -p photocraft-engine --test smart_psd -- --ignored --nocapture perf`). +/// `cargo test --release -p photocraft-engine --test it -- --ignored --nocapture smart_psd::perf`). #[test] #[ignore = "benchmark"] fn perf_24mp_smart_object_export() { @@ -469,6 +469,135 @@ fn displace_psd_filter_renders_with_its_embedded_map() { } } +/// #9162: a Photoshop Displace smart filter that names its map file (`DspF`, a `Pth `) without +/// embedding it was skipped as "not supported" on Replace Contents. Opened from PSD, it now reads +/// that file and the replaced contents come out displaced. +#[test] +fn displace_psd_filter_with_only_a_map_path_renders_after_replace_contents() { + use photocraft_psd::descriptor::{Descriptor, Id, UnicodeString, Value}; + let dir = std::env::temp_dir().join(format!("pcraft-9162-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let png = |name: &str, size: (u32, u32), c: [f32; 4]| { + let d = Document::with_background( + "m", + photocraft_geom::Size::new(size.0, size.1), + photocraft_color::ColorMode::Rgb, + photocraft_color::SampleType::U8, + photocraft_color::Color::rgba(c[0], c[1], c[2], c[3]), + ); + let path = dir.join(name); + std::fs::write(&path, photocraft_io::export(&d, "png", &Default::default()).unwrap().bytes).unwrap(); + path.to_str().unwrap().to_string() + }; + // Red is the horizontal map (full right), green the vertical one (about neutral), as in the + // issue's map. + let map = png("map.png", (12, 8), [1.0, 0.5, 0.5, 1.0]); + let design = png("design.png", (72, 52), [1.0, 0.0, 0.0, 1.0]); + // `DspF` as Photoshop writes a path: `txtu`, sizes (LE), UTF-16LE with a trailing NUL. + let units: Vec = map.encode_utf16().chain([0]).collect(); + let mut pth = b"txtu".to_vec(); + pth.extend((12 + 2 * units.len() as u32).to_le_bytes()); + pth.extend((units.len() as u32).to_le_bytes()); + pth.extend(units.iter().flat_map(|u| u.to_le_bytes())); + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let fltr = Descriptor::new("Dspl") + .with("HrzS", Value::Integer(10)) + .with("VrtS", Value::Integer(10)) + .with("DspM", e("DspM", "StrF")) + .with("UndA", e("UndA", "RptE")) + .with("DspF", Value::Path(pth)); + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Displace..."))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(1148416108)); + for depth in [8, 16, 32] { + let mut s = session(depth); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let id = photocraft_doc::LayerId(s.active().unwrap().active_layer.unwrap().0); + s.edit("displace", |doc, _| { + let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("smart") }; + sm.smart_filters.push(photocraft_io::smart_map::filter_from_item(&item)); + Ok(()) + }) + .unwrap(); + let psd = photocraft_io::export(&s.active().unwrap().doc, "psd", &Default::default()).unwrap().bytes; + // The issue's flow: open the PSD, Replace Contents of the smart object. + let mut s2 = Session::new(); + s2.add_document(photocraft_io::import("pc-displace-repro.psd", &psd).unwrap().document, None); + let (sid, _) = smart(&s2.active().unwrap().doc); + let out = s2.execute("layer.smartObjects.replaceContents", json!({"layer": sid, "path": design})).unwrap(); + assert_eq!(out["warnings"], json!([]), "depth {depth}"); + let cache = smart(&s2.active().unwrap().doc).1.cache.unwrap(); + // 10 % of 128 px at full map contrast: the design (x 8..80) moves 12.8 px left. + let alpha = |x: i32| cache.pixel(x, 30)[3]; + assert!(alpha(2) > 0.99 && alpha(75) < 0.01, "depth {depth}: {} {}", alpha(2), alpha(75)); + } + std::fs::remove_dir_all(&dir).ok(); +} + +/// A Perspective Warp smart filter as Photoshop writes it (`perspectiveWarpTransform`: vertices, +/// warped vertices and quads of vertex indices, document pixels) opens as +/// `layer.smartObjects.perspectiveWarp` and re-renders warped instead of flat (#6268), and keeps +/// its planes through PSD export, `.pcraft` and a PSD re-import. +#[test] +fn perspective_warp_psd_filter_renders_warped() { + use photocraft_psd::descriptor::{Descriptor, UnicodeString, Value}; + let pnts = |p: &[[f64; 2]]| { + let px = |v: f64| Value::UnitFloat { unit: *b"#Pxl", value: v }; + Value::List(p.iter().map(|q| Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", px(q[0])).with("Vrtc", px(q[1])))).collect()) + }; + let quad = Descriptor::new("perspectiveWarpQuad").with("indices", Value::List((0..4).map(Value::Integer).collect())); + let fltr = Descriptor::new("perspectiveWarpTransform") + .with("vertices", pnts(&[[8.0, 6.0], [80.0, 6.0], [80.0, 58.0], [8.0, 58.0]])) + // The right edge moves in and shrinks: the content no longer reaches x = 70. + .with("warpedVertices", pnts(&[[8.0, 6.0], [50.0, 20.0], [50.0, 44.0], [8.0, 58.0]])) + .with("quads", Value::List(vec![Value::Descriptor(quad)])); + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Perspective Warp..."))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(708)); + for depth in [8, 16, 32] { + let mut s = session(depth); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let id = photocraft_doc::LayerId(s.active().unwrap().active_layer.unwrap().0); + let filter = photocraft_io::smart_map::filter_from_item(&item); + assert_eq!(filter.command, "layer.smartObjects.perspectiveWarp"); + s.edit("perspective warp", |doc, _| { + let mut l = doc.layer(id).unwrap().clone(); + let LayerContent::Smart(sm) = &mut l.content else { panic!("smart") }; + sm.smart_filters.push(filter.clone()); + assert!(refresh_layer(doc, &mut l).unwrap()); + *doc.layer_mut(id).unwrap() = l; + Ok(()) + }) + .unwrap(); + let doc = s.active().unwrap().doc.clone(); + let alpha = |doc: &Document, x: i32, y: i32| smart(doc).1.cache.unwrap().pixel(x, y)[3]; + assert!(alpha(&doc, 20, 30) > 0.99 && alpha(&doc, 70, 30) < 0.01 && alpha(&doc, 45, 10) < 0.01, "depth {depth}: warped"); + + let out = photocraft_io::export(&doc, "psd", &Default::default()).unwrap(); + assert!(out.warnings.is_empty(), "{:?}", out.warnings); + assert_eq!(structure_errors(&out.bytes), Vec::::new()); + keep(&format!("perspective-warp-{depth}.psd"), &out.bytes); + let back = photocraft_io::import("perspective.psd", &out.bytes).unwrap().document; + let native = photocraft_format::load_from_bytes(&photocraft_format::save_to_bytes(&back, &Default::default()).unwrap()).unwrap(); + for doc in [&back, &native] { + let (bid, after) = smart(doc); + assert_eq!(after.smart_filters, vec![filter.clone()], "depth {depth}"); + let mut l = doc.layer(photocraft_doc::LayerId(bid)).unwrap().clone(); + assert!(refresh_layer(doc, &mut l).unwrap()); + let LayerContent::Smart(sm) = &l.content else { panic!("smart") }; + let (a, b) = (sm.cache.as_ref().unwrap(), smart(&s.active().unwrap().doc).1.cache.unwrap()); + assert_eq!(a.pixel(20, 30), b.pixel(20, 30), "depth {depth}: re-render"); + assert_eq!(a.pixel(70, 30), b.pixel(70, 30), "depth {depth}: re-render"); + } + } +} + /// A version-2 `liFD` item, as in Photoshop CS2's `lnkD` block: the fields every version has, and /// none of the version 5–7 fields after the file data. fn lifd_v2(f: &photocraft_io::linked::LinkedFile) -> Vec { @@ -670,3 +799,46 @@ fn a_soft_filter_mask_is_stored_the_way_photoshop_stores_it() { let d = max_diff(&photocraft_compose::flatten(&back).px, &photocraft_compose::flatten(&doc).px); assert!(d <= 2.0 / 255.0, "re-opened differs by {d}"); } + +/// Image › Adjustments › Hue/Saturation on a smart object is a smart filter that Photoshop saves +/// as an `HStr` filter (#6267): it is written in that layout, opens as the same command and +/// params, and re-renders adjusted instead of passing the source through. +#[test] +fn hue_saturation_smart_filter_survives_psd_and_re_renders() { + for depth in [8, 16, 32] { + let mut s = session(depth); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let plain = smart(&s.active().unwrap().doc).1.cache.unwrap(); + let params = json!({"hue": 40, "saturation": -30, "lightness": 10, "colorize": false, + "greens": {"hue": -20, "saturation": 15, "lightness": -5, "range": [75, 105, 135, 165]}}); + s.execute("image.adjustments.hueSaturation", params.clone()).unwrap(); + let doc = s.active().unwrap().doc.clone(); + let (_, before) = smart(&doc); + assert_eq!(before.smart_filters.len(), 1); + assert_eq!(before.smart_filters[0].command, "image.adjustments.hueSaturation"); + let adjusted = before.cache.clone().unwrap(); + assert!(plain.pixel(20, 20) != adjusted.pixel(20, 20), "depth {depth}: the filter changes pixels"); + + let out = photocraft_io::export(&doc, "psd", &Default::default()).unwrap(); + assert!(out.warnings.is_empty(), "{:?}", out.warnings); + assert_eq!(structure_errors(&out.bytes), Vec::::new()); + keep(&format!("hue-sat-{depth}.psd"), &out.bytes); + let back = photocraft_io::import("hue-sat.psd", &out.bytes).unwrap().document; + let (bid, after) = smart(&back); + assert_eq!(after.smart_filters.len(), 1); + let f = &after.smart_filters[0]; + assert_eq!(f.command, "image.adjustments.hueSaturation", "depth {depth}: {}", f.params); + for (k, v) in params.as_object().unwrap() { + assert_eq!(&f.params[k], v, "depth {depth}: {k}"); + } + // Re-rendered from the source (as replaceContents / relink do), the filter still applies. + let mut l = back.layer(photocraft_doc::LayerId(bid)).unwrap().clone(); + assert!(refresh_layer(&back, &mut l).unwrap()); + let LayerContent::Smart(sm) = &l.content else { panic!("smart") }; + let cache = sm.cache.as_ref().unwrap(); + for (x, y) in [(20, 20), (40, 30), (70, 50)] { + let (a, b) = (cache.pixel(x, y), adjusted.pixel(x, y)); + assert!((0..4).all(|c| (a[c] - b[c]).abs() <= 1.0 / 255.0 + 1e-6), "depth {depth} at {x},{y}: {a:?} vs {b:?}"); + } + } +} diff --git a/crates/engine/tests/style_and_align.rs b/crates/engine/tests/it/style_and_align.rs similarity index 100% rename from crates/engine/tests/style_and_align.rs rename to crates/engine/tests/it/style_and_align.rs diff --git a/crates/engine/tests/svg_smart_object.rs b/crates/engine/tests/it/svg_smart_object.rs similarity index 100% rename from crates/engine/tests/svg_smart_object.rs rename to crates/engine/tests/it/svg_smart_object.rs diff --git a/crates/engine/tests/it/transform_budget_unclamped.rs b/crates/engine/tests/it/transform_budget_unclamped.rs new file mode 100644 index 0000000000..267667f33b --- /dev/null +++ b/crates/engine/tests/it/transform_budget_unclamped.rs @@ -0,0 +1,74 @@ +//! #6430: integration tests use the production budget (Memory Usage), not the unit tests' reduced +//! one. +use photocraft_engine::Session; +use photocraft_geom::Rect; +use serde_json::{Value, json}; + +fn painted() -> Session { + let mut session = Session::new(); + session.execute("file.new", json!({"width": 24, "height": 16})).unwrap(); + session.execute("layer.new.layer", json!({})).unwrap(); + session + .edit("paint", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 2, 11, 10), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + session +} + +/// `command` with `params` is refused as too large, and the document, its revision and History +/// are as they were. +fn assert_refused_without_changes(session: &mut Session, command: &str, params: Value) { + let state = session.active().unwrap(); + let (document, revision, history) = (state.doc.clone(), state.revision, state.history.past_len()); + let error = session.execute(command, params).unwrap_err().to_string(); + assert!(error.starts_with("too large to transform"), "{error}"); + let state = session.active().unwrap(); + assert!(state.doc == document, "refusal changed the document"); + assert_eq!((state.revision, state.history.past_len()), (revision, history)); +} + +#[test] +fn extreme_layer_scales_are_refused_before_a_clipped_strip_is_warped() { + let mut session = painted(); + session.execute("filter.render.clouds", json!({})).unwrap(); + for scale in [1e9, 1e30, 1e308, -1e308] { + for axis in ["scaleX", "scaleY"] { + assert_refused_without_changes(&mut session, "edit.transform", json!({(axis): scale})); + } + } +} + +/// Transform Selection refuses the same extreme scales and keeps the selection. +#[test] +fn extreme_selection_scales_are_refused_without_losing_the_selection() { + let mut session = painted(); + session.execute("select.rect", json!({"x": 10, "y": 2, "width": 1, "height": 8})).unwrap(); + for scale in [1e30, 1e308] { + for axis in ["scaleX", "scaleY"] { + assert_refused_without_changes(&mut session, "select.transformSelection", json!({(axis): scale})); + } + } +} + +/// #7807: what one transform's result may take is Memory Usage (Preferences › Performance), so +/// raising it lets a refused transform through. +#[test] +fn memory_usage_is_what_a_transform_may_take() { + let mut session = painted(); + // The 1 x 8 px column stretched 40000 times about its centre: 40003 x 10 px of 8-bit RGBA, 1.6 MB. + let stretch = json!({"scaleX": 4e6, "interpolation": "nearest"}); + session.edit_prefs(|p| p.performance.memory_usage_mb = 1); + let state = session.active().unwrap(); + let (document, history) = (state.doc.clone(), state.history.past_len()); + let error = session.execute("edit.transform", stretch.clone()).unwrap_err().to_string(); + assert!(error.contains("would be 40003 x 10 px (2 MB), more than the 1 MB Memory Usage allows (Preferences › Performance)"), "{error}"); + let state = session.active().unwrap(); + assert!(state.doc == document && state.history.past_len() == history, "refusal changed the document"); + session.edit_prefs(|p| p.performance.memory_usage_mb = 2); + session.execute("edit.transform", stretch).unwrap(); + let state = session.active().unwrap(); + let bounds = state.doc.layer(state.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds(); + assert_eq!((bounds.width(), bounds.height()), (40_000, 8), "stretched, nothing clipped"); +} diff --git a/crates/engine/tests/type_format_targets.rs b/crates/engine/tests/it/type_format_targets.rs similarity index 97% rename from crates/engine/tests/type_format_targets.rs rename to crates/engine/tests/it/type_format_targets.rs index 54e7d3f8af..e5f13ffd48 100644 --- a/crates/engine/tests/type_format_targets.rs +++ b/crates/engine/tests/it/type_format_targets.rs @@ -87,8 +87,10 @@ fn selected_type_properties_preserve_each_layers_other_attributes_and_one_histor if indices.contains(&i) { assert_ne!(actual.psd_raw, before[i].psd_raw, "editable PSD data refreshed"); let damage = s.active().unwrap().last_damage.unwrap(); + // The pixels the layer draws, before and after (the cache's tiles, narrowed to the + // layer's extents by the edit's own damage, #9329). for t in [&before[i], &actual] { - let bounds = t.cache.as_ref().unwrap().tile_bounds(); + let bounds = t.cache.as_ref().unwrap().content_bounds(); assert_eq!(damage.intersect(&bounds), bounds); } } diff --git a/crates/engine/tests/it/type_postscript_font.rs b/crates/engine/tests/it/type_postscript_font.rs new file mode 100644 index 0000000000..06171d7d84 --- /dev/null +++ b/crates/engine/tests/it/type_postscript_font.rs @@ -0,0 +1,94 @@ +//! `type.create` accepts an exact PostScript face name in its font field. + +use photocraft_doc::{LayerContent, LayerId}; +use photocraft_engine::Session; +use serde_json::json; + +fn font_with_postscript_name(source: &[u8], postscript: &str) -> Vec { + let mut bytes = source.to_vec(); + let count = u16::from_be_bytes(bytes[4..6].try_into().unwrap()) as usize; + let name_record = (0..count).map(|i| 12 + i * 16).find(|r| &bytes[*r..r + 4] == b"name").unwrap(); + for i in 0..count { + let r = 12 + i * 16; + let offset = u32::from_be_bytes(bytes[r + 8..r + 12].try_into().unwrap()) as usize; + if &bytes[r..r + 4] == b"OS/2" { + bytes[offset + 4..offset + 6].copy_from_slice(&900u16.to_be_bytes()); + } + } + let names = [(1u16, "PhotoCraft Black Test"), (2, "Black"), (6, postscript), (16, "PhotoCraft Black Test"), (17, "Black")]; + let mut table = Vec::new(); + let mut strings = Vec::new(); + for value in [0u16, names.len() as u16, (6 + names.len() * 12) as u16] { + table.extend(value.to_be_bytes()); + } + for (id, value) in names { + let encoded: Vec<_> = value.encode_utf16().flat_map(u16::to_be_bytes).collect(); + for n in [3u16, 1, 0x409, id, encoded.len() as u16, strings.len() as u16] { + table.extend(n.to_be_bytes()); + } + strings.extend(encoded); + } + table.extend(strings); + let offset = bytes.len() as u32; + bytes[name_record + 8..name_record + 12].copy_from_slice(&offset.to_be_bytes()); + bytes[name_record + 12..name_record + 16].copy_from_slice(&(table.len() as u32).to_be_bytes()); + bytes.extend(table); + bytes +} + +#[test] +fn type_create_resolves_exact_postscript_font_names() { + let ps = "PhotoCraft-Black-Test"; + photocraft_text::shared() + .lock() + .unwrap_or_else(|e| e.into_inner()) + .fonts + .register_font_data(font_with_postscript_name(photocraft_text::fonts::INTER_REGULAR.as_slice(), ps)); + + let mut session = Session::new(); + session.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap(); + let response = session.execute("type.create", json!({"text": "Test", "font": ps})).unwrap(); + assert!(response["warnings"].as_array().is_none_or(Vec::is_empty), "{response}"); + let id = LayerId(response["layer"].as_u64().unwrap()); + let LayerContent::Text(text) = &session.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a text layer") }; + let style = &text.char_runs()[0].style; + assert_eq!(style.font_family, "PhotoCraft Black Test"); + assert_eq!(style.postscript_name.as_deref(), Some(ps)); + assert_eq!(style.weight, 900); + + let regular = session.execute("type.create", json!({"text": "Test", "font": "Inter"})).unwrap(); + assert!(regular["warnings"].as_array().is_none_or(Vec::is_empty), "{regular}"); + let missing = session.execute("type.create", json!({"text": "Test", "font": "No Such Family 123"})).unwrap(); + assert_eq!(missing["warnings"], json!(["font 'No Such Family 123' is not installed; text may render using a fallback face"])); +} + +#[test] +fn exact_postscript_face_clears_retained_weight_variation() { + let ps = "PhotoCraft-Black-Test-Var"; + photocraft_text::shared() + .lock() + .unwrap_or_else(|e| e.into_inner()) + .fonts + .register_font_data(font_with_postscript_name(photocraft_text::fonts::INTER_REGULAR.as_slice(), ps)); + + let mut session = Session::new(); + session.execute("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap(); + let response = session.execute("type.create", json!({"text": "Test", "font": "Inter", "variations": {"wght": 300}})).unwrap(); + let id = LayerId(response["layer"].as_u64().unwrap()); + + // Setting an exact PostScript face should clear the previously retained wght variation + session.execute("type.setStyle", json!({"layer": id.0, "font": ps})).unwrap(); + let LayerContent::Text(text) = &session.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a text layer") }; + let style = &text.char_runs()[0].style; + assert_eq!(style.font_family, "PhotoCraft Black Test"); + assert_eq!(style.weight, 900); + assert!(!style.variations.iter().any(|v| v.axis == "wght")); + + // Supplying a variations.wght in the same call should apply after clearing + session.execute("type.setStyle", json!({"layer": id.0, "font": ps, "variations": {"wght": 700}})).unwrap(); + let LayerContent::Text(text2) = &session.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a text layer") }; + let style2 = &text2.char_runs()[0].style; + assert_eq!(style2.font_family, "PhotoCraft Black Test"); + assert_eq!(style2.weight, 900); + assert_eq!(style2.variations.iter().find(|v| v.axis == "wght").map(|v| v.value), Some(700.0)); +} diff --git a/crates/engine/tests/undo_keeps_active_layer.rs b/crates/engine/tests/it/undo_keeps_active_layer.rs similarity index 100% rename from crates/engine/tests/undo_keeps_active_layer.rs rename to crates/engine/tests/it/undo_keeps_active_layer.rs diff --git a/crates/engine/tests/unicode_colors.rs b/crates/engine/tests/it/unicode_colors.rs similarity index 100% rename from crates/engine/tests/unicode_colors.rs rename to crates/engine/tests/it/unicode_colors.rs diff --git a/crates/engine/tests/it/unsupported_smart_filters.rs b/crates/engine/tests/it/unsupported_smart_filters.rs new file mode 100644 index 0000000000..8861fea4d0 --- /dev/null +++ b/crates/engine/tests/it/unsupported_smart_filters.rs @@ -0,0 +1,85 @@ +use photocraft_doc::{LayerContent, LayerId, SmartFilter}; +use photocraft_engine::{Session, smart_cmds}; +use serde_json::json; + +const STORED: [f32; 4] = [0.0, 0.0, 1.0, 1.0]; + +fn mockup() -> (Session, LayerId) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + s.edit("imported filter", |doc, _| { + let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("not a smart object") }; + sm.smart_filters.push(SmartFilter { + command: "psd.unsupportedFilter".into(), + params: json!({"name": "Sharpen", "filterID": 1399353968, "psd": ""}), + blend: photocraft_color::BlendMode::Normal, + opacity: 1.0, + visible: true, + }); + let c = sm.cache.as_mut().unwrap(); + let r = c.content_bounds(); + c.fill_rect(r, &STORED); + Ok(()) + }) + .unwrap(); + (s, id) +} + +fn cache(s: &Session, id: LayerId) -> photocraft_raster::Surface { + let LayerContent::Smart(sm) = &s.active().unwrap().doc.layer(id).unwrap().content else { panic!("not a smart object") }; + sm.cache.clone().unwrap() +} + +fn png() -> Vec { + let image = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgb, vec![0; 8 * 8 * 3]).unwrap(); + photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap() +} + +#[test] +fn geometry_edits_keep_the_stored_render_of_an_unsupported_filter() { + for (cmd, params) in [("edit.transform", json!({"scaleX": 50, "scaleY": 50})), ("image.imageSize", json!({"width": 20, "height": 15}))] { + let (mut s, id) = mockup(); + let before = cache(&s, id).content_bounds(); + s.execute(cmd, params).unwrap(); + let after = cache(&s, id); + let r = after.content_bounds(); + assert!(r.width() < before.width(), "{cmd} resized the stored render"); + assert_eq!(after.rgba((r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2), STORED, "{cmd} kept the stored render"); + } +} + +#[test] +fn replacing_contents_reports_the_filters_it_cannot_apply() { + let (mut s, id) = mockup(); + let r = smart_cmds::replace_contents_bytes(&mut s, id, "new.png", png()).unwrap(); + let warnings = r["warnings"].as_array().unwrap(); + assert_eq!(warnings.len(), 1, "{r}"); + assert!(warnings[0].as_str().unwrap().contains("Sharpen"), "{r}"); +} + +#[test] +fn supported_filters_still_re_render_after_a_transform() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 40, "height": 30})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); + let id = s.active().unwrap().active_layer.unwrap(); + s.edit("stale cache", |doc, _| { + let LayerContent::Smart(sm) = &mut doc.layer_mut(id).unwrap().content else { panic!("not a smart object") }; + let c = sm.cache.as_mut().unwrap(); + let r = c.content_bounds(); + c.fill_rect(r, &STORED); + Ok(()) + }) + .unwrap(); + s.execute("edit.transform", json!({"scaleX": 50, "scaleY": 50})).unwrap(); + let c = cache(&s, id); + let r = c.content_bounds(); + assert_ne!(c.rgba((r.x0 + r.x1) / 2, (r.y0 + r.y1) / 2), STORED, "a supported filter re-renders from the source"); +} diff --git a/crates/engine/tests/wand_transparent_copy.rs b/crates/engine/tests/it/wand_transparent_copy.rs similarity index 100% rename from crates/engine/tests/wand_transparent_copy.rs rename to crates/engine/tests/it/wand_transparent_copy.rs diff --git a/crates/engine/tests/working_rgb_profile.rs b/crates/engine/tests/it/working_rgb_profile.rs similarity index 100% rename from crates/engine/tests/working_rgb_profile.rs rename to crates/engine/tests/it/working_rgb_profile.rs diff --git a/crates/engine/tests/it/xmp_charref.rs b/crates/engine/tests/it/xmp_charref.rs new file mode 100644 index 0000000000..a4b98da4e5 --- /dev/null +++ b/crates/engine/tests/it/xmp_charref.rs @@ -0,0 +1,24 @@ +use photocraft_engine::file_cmds::read_file_info; + +#[test] +fn numeric_character_references_are_decoded() { + let xmp = "\ + Line one Line two © & co A &#38;\ + "; + let info = read_file_info(Some(xmp)); + assert_eq!(info["description"], "Line one\nLine two © & co A &"); +} + +/// Many `&` with no `;` near them stay literal, and are scanned a bounded distance each (a +/// multi-megabyte XMP once made this quadratic). +#[test] +fn a_long_run_of_ampersands_without_semicolons_is_left_unchanged() { + let run = "&".repeat(200_000); + let xmp = format!( + "\ + {run}\ + " + ); + let info = read_file_info(Some(&xmp)); + assert_eq!(info["description"], run.as_str()); +} diff --git a/crates/engine/tests/live_dab_matches_replay.rs b/crates/engine/tests/live_dab_matches_replay.rs new file mode 100644 index 0000000000..aae7e32b53 --- /dev/null +++ b/crates/engine/tests/live_dab_matches_replay.rs @@ -0,0 +1,216 @@ +use photocraft_engine::{Session, retouch_cmds::LiveDab}; +use photocraft_paint::StrokePoint; +use serde_json::json; + +fn setup(depth: u32, mode: &str) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width":200,"height":120,"depth":depth,"mode":mode})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.edit("texture", |doc, active| { + let bounds = doc.bounds(); + let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap(); + let fmt = surf.format(); + let mut px = Vec::new(); + for y in 0..120 { + for x in 0..200 { + px.extend(photocraft_raster::from_rgba( + &fmt, + [((x * 7 + y * 3) % 31) as f32 / 31.0, x as f32 / 200.0, y as f32 / 120.0, 0.4 + ((x + y) % 7) as f32 / 12.0], + )); + } + } + surf.write_region(bounds, &px); + Ok(()) + }) + .unwrap(); + s +} + +fn pixels(s: &Session) -> Vec { + let st = s.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().read_region(st.doc.bounds()) +} + +#[test] +fn pressure_shape_and_smoothing_match_replay() { + let mut failures = Vec::new(); + for cmd in ["paint.smudge", "paint.blur", "paint.sharpen", "paint.dodge", "paint.burn", "paint.sponge"] { + for case in ["pressure", "locked", "ellipse", "smoothing", "scattering"] { + for depth in [8, 16, 32] { + let mut s = setup(depth, "rgb"); + match case { + "locked" => { + s.edit("lock alpha", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().locks.transparency = true; + Ok(()) + }) + .unwrap(); + } + "ellipse" => { + s.tools.brush.roundness = 0.2; + s.tools.brush.angle = 37.0; + } + "smoothing" => { + s.tools.brush.smoothing.amount = 0.6; + } + "scattering" => { + s.tools.brush.scattering.enabled = true; + s.tools.brush.scattering.scatter.jitter = 1.0; + } + _ => {} + } + let points = [[40.0, 40.0, 0.1], [65.0, 50.0, 0.4], [100.0, 60.0, 1.0], [140.0, 70.0, 0.7], [60.0, 40.0, 0.2]]; + let params = json!({"points":points,"size":40,"hardness":70,"strength":80,"opacity":55,"spacing":25}); + s.execute(cmd, params.clone()).unwrap(); + let want = pixels(&s); + s.execute("edit.undo", json!({})).unwrap(); + let mut start = params.clone(); + start["points"] = json!([points[0]]); + let mut live = LiveDab::begin(&s, cmd, &start).unwrap(); + for q in &points[1..] { + live.push(&[StrokePoint::new(q[0], q[1], q[2] as f32)]).unwrap(); + } + s.offer_live_dab(live).unwrap(); + let result = s.execute(cmd, params).unwrap(); + assert_eq!(result["fromLive"], true); + let got = pixels(&s); + let count = got.iter().zip(&want).filter(|(a, b)| a != b).count(); + let max = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + if count > 0 { + failures.push(format!("{cmd} {case} depth={depth}: {count} channels differ, max={max}")); + } + } + } + } + assert!(failures.is_empty(), "{}", failures.join("\n")); +} + +#[test] +fn native_modes_selection_and_lock_match_replay() { + for mode in ["rgb", "gray", "cmyk", "lab"] { + for depth in [8, 16, 32] { + let mut s = setup(depth, mode); + s.edit("lock alpha", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().locks.transparency = true; + Ok(()) + }) + .unwrap(); + s.execute("select.rect", json!({"x":30,"y":20,"width":110,"height":60})).unwrap(); + let points = [[-10.0, 30.0], [80.0, 50.0], [160.0, 80.0], [60.0, 60.0]]; + for cmd in ["paint.blur", "paint.sharpen", "paint.dodge", "paint.burn", "paint.sponge", "paint.smudge"] { + let params = json!({"points":points,"size":30,"hardness":40,"strength":70,"opacity":37}); + let expected_damage = s.execute(cmd, params.clone()).unwrap()["damage"].clone(); + let want = pixels(&s); + s.execute("edit.undo", json!({})).unwrap(); + let mut start = params.clone(); + start["points"] = json!([points[0]]); + let mut live = LiveDab::begin(&s, cmd, &start).unwrap(); + for q in &points[1..] { + live.push(&[StrokePoint::new(q[0], q[1], 1.0)]).unwrap(); + } + s.offer_live_dab(live).unwrap(); + let result = s.execute(cmd, params).unwrap(); + assert_eq!(result["fromLive"], true); + assert_eq!(result["damage"], expected_damage); + assert_eq!(pixels(&s), want, "{mode} {depth} {cmd}"); + s.execute("edit.undo", json!({})).unwrap(); + } + } + } +} + +#[test] +fn changed_document_refuses_live_pixels() { + let mut s = setup(8, "rgb"); + let p = json!({"points":[[40,40],[80,60]],"size":30}); + let live = LiveDab::begin(&s, "paint.blur", &p).unwrap(); + s.execute("paint.stroke", json!({"points":[[40,40],[80,60]],"color":"#ffffff","size":20})).unwrap(); + let mut replay = Session::new(); + replay.add_document((*s.active().unwrap().doc).clone(), None); + replay.tools.brush = s.tools.brush.clone(); + s.offer_live_dab(live).unwrap(); + assert!(s.execute("paint.blur", p.clone()).unwrap().get("fromLive").is_none()); + replay.execute("paint.blur", p).unwrap(); + assert_eq!(pixels(&s), pixels(&replay)); +} + +/// An independent paint-level oracle gives Smudge a generously padded working region. Pixels +/// picked up by the initial small dab must not depend on the final stroke's bounding rectangle. +#[test] +fn smudge_pickup_matches_full_neighborhood_when_pressure_expands() { + use photocraft_paint::{ + Stroke, + retouch::{Smudge, apply_dab_stroke}, + }; + + let points = [[40.0, 40.0, 0.1], [65.0, 50.0, 0.4], [100.0, 60.0, 1.0], [140.0, 70.0, 0.7], [60.0, 40.0, 0.2]]; + for depth in [8, 16, 32] { + let mut s = setup(depth, "rgb"); + let params = json!({"points":points,"size":40,"hardness":70,"strength":80,"opacity":55,"spacing":25}); + let (mut expected, lock) = { + let st = s.active().unwrap(); + let id = st.active_layer.unwrap(); + (st.doc.layer(id).unwrap().surface().unwrap().clone(), st.doc.effective_locks(id).transparency) + }; + let stroke = Stroke { + brush: photocraft_engine::retouch_cmds::stroke_brush(&s.tools.brush, ¶ms), + points: points.iter().map(|q| StrokePoint::new(q[0], q[1], q[2] as f32)).collect(), + }; + let mut effect = Smudge::new(0.8, None, 40.0).with_alpha(Some(3)); + apply_dab_stroke(&mut expected, &stroke, None, lock, 40, |work, fp| effect.dab(work, fp)); + let bounds = s.active().unwrap().doc.bounds(); + let want = expected.read_region(bounds); + s.execute("paint.smudge", params.clone()).unwrap(); + assert!(pixels(&s) == want, "command must read the whole pickup square at depth {depth}"); + s.execute("edit.undo", json!({})).unwrap(); + let live = LiveDab::begin(&s, "paint.smudge", ¶ms).unwrap(); + s.offer_live_dab(live).unwrap(); + assert_eq!(s.execute("paint.smudge", params).unwrap()["fromLive"], true); + assert!(pixels(&s) == want, "a single batch uses the same pickup square at depth {depth}"); + } +} + +#[test] +fn pressure_sized_live_dabs_commit_only_the_requested_mask_or_channel() { + use photocraft_raster::Surface; + + let fill = |surface: &mut Surface| { + let values: Vec = (0..120).flat_map(|y| (0..200).map(move |x| ((x * 7 + y * 3) % 31) as f32 / 31.0)).collect(); + surface.write_region(photocraft_geom::Rect::new(0, 0, 200, 120), &values); + }; + for depth in [8, 16, 32] { + let mut s = setup(depth, "rgb"); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("channel.new", json!({"fill":"white"})).unwrap(); + s.execute("select.editInQuickMaskMode", json!({"on":true})).unwrap(); + s.edit("mask texture", |doc, active| { + fill(&mut doc.layer_mut(active.unwrap()).unwrap().mask.as_mut().unwrap().surface); + fill(&mut doc.channels[0].surface); + fill(&mut doc.quick_mask.as_mut().unwrap().surface); + Ok(()) + }) + .unwrap(); + let before = s.active().unwrap().doc.clone(); + for target in [json!("mask"), json!({"channel":0}), json!("quickMask")] { + for cmd in ["paint.smudge", "paint.blur", "paint.sharpen", "paint.dodge", "paint.burn", "paint.sponge"] { + let points = [[40.0, 40.0, 0.1], [65.0, 50.0, 0.4], [100.0, 60.0, 1.0], [140.0, 70.0, 0.7], [60.0, 40.0, 0.2]]; + let params = json!({"target":target,"points":points,"size":40,"hardness":70,"strength":80,"opacity":55,"spacing":25}); + s.execute(cmd, params.clone()).unwrap(); + let want = s.active().unwrap().doc.clone(); + s.execute("edit.undo", json!({})).unwrap(); + let mut start = params.clone(); + start["points"] = json!([points[0]]); + let mut live = LiveDab::begin(&s, cmd, &start).unwrap(); + for q in &points[1..] { + live.push(&[StrokePoint::new(q[0], q[1], q[2] as f32)]).unwrap(); + } + s.offer_live_dab(live).unwrap(); + assert_eq!(s.execute(cmd, params).unwrap()["fromLive"], true); + assert_eq!(s.active().unwrap().doc, want, "depth={depth} {cmd} target={target}"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.active().unwrap().doc, before); + } + } + } +} diff --git a/crates/engine/tests/panic_hunt.rs b/crates/engine/tests/panic_hunt.rs index 893ba5c23d..c4d8359e18 100644 --- a/crates/engine/tests/panic_hunt.rs +++ b/crates/engine/tests/panic_hunt.rs @@ -13,6 +13,8 @@ fn fresh() -> Session { // Belt and braces with the EXTERNAL skip below: nothing in this process may reach a printer. photocraft_engine::print_cmds::disable_printing(); let mut s = Session::new(); + // `file.printers` and `file.print.printers` list no printers and do not ask the spooler. + s.printer_source = Some(std::sync::Arc::new(Vec::new)); s.execute("file.new", json!({"width": 24, "height": 16})).unwrap(); s.execute("layer.new.layer", json!({})).unwrap(); s.execute("select.rect", json!({"x": 1, "y": 1, "width": 6, "height": 5})).unwrap(); @@ -30,9 +32,14 @@ static PANICS: AtomicUsize = AtomicUsize::new(0); /// One run at a time, so a panic is charged to the run that caused it. static RUN: Mutex<()> = Mutex::new(()); -/// Run `f` on a worker thread. `None` if it timed out, else whether it finished without -/// panicking, contained or not (returning Ok or a graceful Err both count). -fn run_watched(f: impl FnOnce() + Send + 'static) -> Option { +/// Budget for the self-test below, which checks how panics are counted, not hangs. The panic hook +/// prints a backtrace under `RUST_BACKTRACE=1` (as in CI), and on a loaded Windows runner the +/// first symbol lookup alone can take 15–20 s. +const SELF_TEST_DEADLINE: Duration = Duration::from_secs(120); + +/// Run `f` on a worker thread. `None` if it took longer than `deadline`, else whether it +/// finished without panicking, contained or not (returning Ok or a graceful Err both count). +fn run_watched(deadline: Duration, f: impl FnOnce() + Send + 'static) -> Option { static HOOK: Once = Once::new(); HOOK.call_once(|| { let default = std::panic::take_hook(); @@ -48,13 +55,13 @@ fn run_watched(f: impl FnOnce() + Send + 'static) -> Option { let r = catch_unwind(AssertUnwindSafe(f)); let _ = tx.send(r.is_ok()); }); - rx.recv_timeout(DEADLINE).ok().map(|ok| ok && PANICS.load(Ordering::SeqCst) == before) + rx.recv_timeout(deadline).ok().map(|ok| ok && PANICS.load(Ordering::SeqCst) == before) } /// Run `id` with `p` on a fresh session; see [`run_watched`]. fn run_once(id: &str, p: &Value) -> Option { let (id, p) = (id.to_string(), p.clone()); - run_watched(move || { + run_watched(DEADLINE, move || { let mut s = fresh(); let _ = s.execute(&id, p); }) @@ -107,11 +114,11 @@ fn no_command_panics_or_hangs_on_adversarial_params() { /// #4982: a panic the code under test contains still fails the run; a graceful `Err` does not. #[test] fn contained_panics_count_and_graceful_errors_do_not() { - let contained = run_watched(|| { + let contained = run_watched(SELF_TEST_DEADLINE, || { let _ = catch_unwind(|| panic!("contained, as Session::execute does")); }); assert_eq!(contained, Some(false)); - let graceful = run_watched(|| { + let graceful = run_watched(SELF_TEST_DEADLINE, || { let mut s = fresh(); assert!(s.execute("layer.delete", json!({"layer": 999999})).is_err()); }); diff --git a/crates/engine/tests/pen_path_append.rs b/crates/engine/tests/pen_path_append.rs new file mode 100644 index 0000000000..41306ea6a2 --- /dev/null +++ b/crates/engine/tests/pen_path_append.rs @@ -0,0 +1,80 @@ +use photocraft_engine::Session; +use serde_json::json; + +fn session() -> Session { + let mut session = Session::new(); + session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + session +} + +#[test] +fn appended_work_and_saved_components_preserve_existing_geometry_and_undo() { + for name in ["work", "Saved drawing"] { + let mut session = session(); + let first = json!({"subpaths": [{"closed": false, "knots": [[20, 20], [80, 20]]}]}); + let second = json!({"subpaths": [{"closed": false, "knots": [[30, 60], [90, 60]]}]}); + session.execute("path.set", json!({"name": name, "path": first})).unwrap(); + let before = session.execute("path.info", json!({"name": name})).unwrap(); + session.execute("path.set", json!({"name": name, "path": second, "op": "combine"})).unwrap(); + let after = session.execute("path.info", json!({"name": name})).unwrap(); + assert_eq!(after["path"]["subpaths"].as_array().unwrap().len(), 2); + assert_eq!(after["path"]["subpaths"][0], before["path"]["subpaths"][0]); + session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(session.execute("path.info", json!({"name": name})).unwrap(), before); + session.execute("edit.redo", json!({})).unwrap(); + assert_eq!(session.execute("path.info", json!({"name": name})).unwrap(), after); + } +} + +#[test] +fn appending_valid_components_cannot_exceed_total_path_budget() { + let mut session = session(); + let subpath = json!({"closed": false, "knots": []}); + session.execute("path.set", json!({"path": {"subpaths": vec![subpath.clone(); 2_049]}})).unwrap(); + let before = session.active().unwrap().doc.work_path.clone(); + let history = session.active().unwrap().history.entries().len(); + let error = session.execute("path.set", json!({"path": {"subpaths": vec![subpath; 2_048]}, "op": "combine"})).unwrap_err(); + assert!(error.to_string().contains("4096")); + assert_eq!(session.active().unwrap().doc.work_path, before); + assert_eq!(session.active().unwrap().history.entries().len(), history); +} + +#[test] +fn invalid_path_coordinates_leave_work_and_saved_paths_unchanged() { + for name in ["work", "Saved drawing"] { + let mut session = session(); + session.execute("path.set", json!({"name": name, "path": {"subpaths": [{"knots": [[10, 10], [20, 20]]}]}})).unwrap(); + let before = session.execute("path.info", json!({"name": name})).unwrap(); + let history = session.active().unwrap().history.entries().len(); + assert!( + session.execute("path.set", json!({"name": name, "op": "combine", "path": {"subpaths": [{"knots": [[-1.0e308, 10], [1.0e308, 20]]}]}})).is_err() + ); + assert_eq!(session.execute("path.info", json!({"name": name})).unwrap(), before); + assert_eq!(session.active().unwrap().history.entries().len(), history); + } +} + +#[test] +fn large_straight_work_and_saved_paths_append_and_undo_without_shape_coordinate_limits() { + for name in ["work", "Saved drawing"] { + let mut session = session(); + let large = json!({"subpaths": [{"closed": true, "knots": [[0.0, 0.0], [3.0e9, 0.0], [3.0e9, 1.0e9]]}]}); + let before = session.active().unwrap().doc.clone(); + let history = session.active().unwrap().history.entries().len(); + session.execute("path.set", json!({"name":name,"path":large})).unwrap(); + let first = session.execute("path.info", json!({"name":name})).unwrap(); + assert_eq!(first["path"]["subpaths"][0]["knots"][1]["anchor"], json!([3.0e9, 0.0])); + assert_eq!(session.active().unwrap().history.entries().len(), history + 1); + session.execute("path.set", json!({"name":name,"op":"combine","path":{"subpaths":[{"knots":[[10,20],[80,20]]}]}})).unwrap(); + let appended = session.execute("path.info", json!({"name":name})).unwrap(); + assert_eq!(appended["path"]["subpaths"].as_array().unwrap().len(), 2); + assert_eq!(appended["path"]["subpaths"][0], first["path"]["subpaths"][0]); + session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(session.execute("path.info", json!({"name":name})).unwrap(), first); + session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(session.active().unwrap().doc.work_path, before.work_path); + assert_eq!(session.active().unwrap().doc.paths, before.paths); + session.execute("edit.redo", json!({})).unwrap(); + assert_eq!(session.execute("path.info", json!({"name":name})).unwrap(), first); + } +} diff --git a/crates/engine/tests/smart_rerender_fallback.rs b/crates/engine/tests/smart_rerender_fallback.rs new file mode 100644 index 0000000000..1359ec0aeb --- /dev/null +++ b/crates/engine/tests/smart_rerender_fallback.rs @@ -0,0 +1,77 @@ +use std::sync::Arc; + +use photocraft_doc::{LayerContent, SmartObject, SmartSource}; +use photocraft_engine::{EngineError, Session}; +use serde_json::{Value, json}; + +fn deny_blur(id: &str, _: &Value) -> photocraft_engine::Result<()> { + if id == "filter.blur.gaussianBlur" { Err(EngineError::Other("denied".into())) } else { Ok(()) } +} + +fn edits() -> [(&'static str, Value); 2] { + [("edit.transform", json!({"scaleX": 50, "scaleY": 100})), ("image.imageSize", json!({"width": 100, "height": 50}))] +} + +fn blurred_smart_object() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 200, "height": 100})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 50})).unwrap(); + s.execute("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap(); + s.execute("select.deselect", json!({})).unwrap(); + s.execute("layer.smartObjects.convertToSmartObject", json!({})).unwrap(); + s.execute("filter.blur.gaussianBlur", json!({"radius": 2})).unwrap(); + s +} + +fn cache_width(s: &Session) -> i32 { + let st = s.active().unwrap(); + let LayerContent::Smart(sm) = &st.doc.layer(st.active_layer.unwrap()).unwrap().content else { panic!("not a smart object") }; + sm.cache.as_ref().unwrap().content_bounds().width() as i32 +} + +fn edit_smart_object(s: &mut Session, edit: impl FnOnce(&mut SmartObject)) { + let st = s.active_mut().unwrap(); + let id = st.active_layer.unwrap(); + let LayerContent::Smart(sm) = &mut Arc::make_mut(&mut st.doc).layer_mut(id).unwrap().content else { panic!("not a smart object") }; + edit(sm); +} + +#[test] +fn a_smart_object_whose_filters_are_denied_keeps_the_resampled_fallback() { + for (cmd, params) in edits() { + let mut s = blurred_smart_object(); + let before = cache_width(&s); + s.authorize = Some(deny_blur); + s.execute(cmd, params).unwrap(); + let after = cache_width(&s); + assert!(after < before * 3 / 4, "{cmd}: the fallback stayed at {after} px wide (was {before})"); + } +} + +#[test] +fn a_smart_object_that_fails_to_re_render_keeps_the_resampled_fallback() { + let unrenderable = + [SmartSource::Linked { path: "missing.psd".into() }, SmartSource::Embedded { file_name: "broken.png".into(), bytes: Arc::new(vec![0; 16]) }]; + for (cmd, params) in edits() { + for source in &unrenderable { + let mut s = blurred_smart_object(); + let before = cache_width(&s); + edit_smart_object(&mut s, |sm| sm.source = source.clone()); + s.execute(cmd, params.clone()).unwrap(); + let after = cache_width(&s); + assert!(after < before * 3 / 4, "{cmd} with {source:?}: the fallback stayed at {after} px wide (was {before})"); + } + } +} + +#[test] +fn an_allowed_smart_object_still_re_renders_at_the_new_size() { + for (cmd, params) in edits() { + let mut s = blurred_smart_object(); + let before = cache_width(&s); + s.execute(cmd, params).unwrap(); + let after = cache_width(&s); + assert!(after < before * 3 / 4, "{cmd}: {after} px wide (was {before})"); + } +} diff --git a/crates/engine/tests/smart_source_cache.rs b/crates/engine/tests/smart_source_cache.rs new file mode 100644 index 0000000000..ec28b21292 --- /dev/null +++ b/crates/engine/tests/smart_source_cache.rs @@ -0,0 +1,92 @@ +//! #10030: the decoded-source cache counts the bytes it holds, and Purge All and closing a +//! document with smart objects release them. One integration-test process keeps the global +//! cache isolated from parallel unit tests; the tests here take turns on it. +use std::sync::Mutex; + +use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, SampleType}; +use photocraft_engine::{Session, file_cmds, smart_cmds}; +use serde_json::json; + +static TURN: Mutex<()> = Mutex::new(()); + +const SIDE: u32 = 256; + +fn png() -> Vec { + let px = (0..SIDE * SIDE).flat_map(|i| [(i % SIDE) as u8, (i / SIDE) as u8, ((i * 7) % 251) as u8, 255]); + let img = Image::from_raw(SIDE, SIDE, ChannelLayout::Rgba, SampleType::U8, px.collect()).unwrap(); + photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap() +} + +/// Every cache `edit.purge.all` clears, emptied. +fn empty_caches() { + smart_cmds::purge_source_cache(); + smart_cmds::purge_stack_cache(); + photocraft_compose::purge_effect_cache(); + photocraft_compose::masks::purge_cache(); + photocraft_compose::shape_split::purge_cache(); +} + +fn session_with_smart_object(bytes: &[u8]) -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": SIDE, "height": SIDE})).unwrap(); + file_cmds::place_bytes(&mut s, "art.png", bytes.to_vec(), None, &json!({})).unwrap(); + s +} + +fn decode(s: &Session, bytes: &[u8]) { + smart_cmds::source_image("art.png", bytes, s.active().unwrap().doc.pixel_format()).unwrap(); +} + +#[test] +fn the_cache_counts_real_tile_bytes() { + let _turn = TURN.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + empty_caches(); + let bytes = png(); + let s = session_with_smart_object(&bytes); + decode(&s, &bytes); + let held = smart_cmds::source_cache_bytes(); + let px = (SIDE * SIDE) as usize; + assert!(held >= 4 * px && held < 5 * px, "{held} bytes for {px} 8-bit RGBA pixels"); + assert_eq!(smart_cmds::purge_source_cache(), held); + assert_eq!(smart_cmds::source_cache_bytes(), 0); +} + +#[test] +fn purge_all_reports_and_empties_the_source_cache() { + let _turn = TURN.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + empty_caches(); + let bytes = png(); + let mut s = session_with_smart_object(&bytes); + // Nothing else holds anything to purge: only the source cache enables Purge All. + s.execute("edit.purge.histories", json!({})).unwrap(); + s.clipboard = None; + assert!(!s.is_enabled("edit.purge.all")); + decode(&s, &bytes); + assert!(smart_cmds::source_cache_bytes() > 0); + assert!(s.is_enabled("edit.purge.all")); + let purged = s.execute("edit.purge.all", json!({})).unwrap(); + assert!(purged["purged"].as_array().unwrap().contains(&json!("smart object source cache")), "{purged}"); + assert_eq!(smart_cmds::source_cache_bytes(), 0); + assert!(!s.is_enabled("edit.purge.all")); +} + +#[test] +fn closing_a_document_with_a_smart_object_empties_the_source_cache() { + let _turn = TURN.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + empty_caches(); + let bytes = png(); + let mut s = session_with_smart_object(&bytes); + decode(&s, &bytes); + assert!(smart_cmds::source_cache_bytes() > 0); + s.execute("file.close", json!({})).unwrap(); + assert_eq!(smart_cmds::source_cache_bytes(), 0); + + // A document without smart objects leaves it alone. + let mut plain = Session::new(); + plain.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let s = session_with_smart_object(&bytes); + decode(&s, &bytes); + plain.execute("file.close", json!({})).unwrap(); + assert!(smart_cmds::source_cache_bytes() > 0); + empty_caches(); +} diff --git a/crates/engine/tests/smart_source_peak.rs b/crates/engine/tests/smart_source_peak.rs new file mode 100644 index 0000000000..ea74b88e0f --- /dev/null +++ b/crates/engine/tests/smart_source_peak.rs @@ -0,0 +1,50 @@ +//! #10030: decoding a smart object's source (what its first re-render does) composites it in +//! tile-aligned blocks, so the peak memory growth stays a small multiple of the source's pixels, not the 32 +//! bytes a pixel the full-size RGBA buffer and its f32 copy took on top of the decoded document. +//! +//! The one test here runs in its own process: it reads the process's peak RSS (`VmHWM`, Linux +//! only) and nothing else may allocate meanwhile. + +#![cfg(target_os = "linux")] + +use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, SampleType}; +use photocraft_engine::{Session, smart_cmds}; +use serde_json::json; + +fn peak_rss_bytes() -> Option { + let status = std::fs::read_to_string("/proc/self/status").ok()?; + let line = status.lines().find(|l| l.starts_with("VmHWM:"))?; + let kib: u64 = line.split_whitespace().nth(1)?.parse().ok()?; + Some(kib * 1024) +} + +const SIDE: u32 = 4096; + +fn png() -> Vec { + let px = (0..SIDE * SIDE).flat_map(|i| { + let (x, y) = (i % SIDE, i / SIDE); + [(x * 5 + y) as u8, ((y * 7) ^ x) as u8, ((x * y) % 251) as u8, 255 - ((x / 64 + y / 64) % 4 * 40) as u8] + }); + let img = Image::from_raw(SIDE, SIDE, ChannelLayout::Rgba, SampleType::U8, px.collect()).unwrap(); + photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap() +} + +#[test] +fn decoding_a_smart_object_source_stays_within_16_bytes_a_source_pixel() { + let bytes = png(); + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let fmt = s.active().unwrap().doc.pixel_format(); + // Writing 5 resets the peak RSS to the current RSS; skip where the kernel doesn't allow it. + if std::fs::write("/proc/self/clear_refs", "5").is_err() { + return; + } + let Some(before) = peak_rss_bytes() else { return }; + let img = smart_cmds::source_image("big.png", &bytes, fmt).unwrap(); + let grown = peak_rss_bytes().unwrap().saturating_sub(before); + assert_eq!(img.bounds.width(), SIDE); + + let limit = 16 * u64::from(SIDE) * u64::from(SIDE); + eprintln!("peak RSS growth {} MiB for {} px ({:.1} B/px)", grown >> 20, SIDE * SIDE, grown as f64 / f64::from(SIDE * SIDE)); + assert!(grown < limit, "peak RSS grew by {grown} bytes, over {limit} (16 B/px)"); +} diff --git a/crates/format/Cargo.toml b/crates/format/Cargo.toml index 113d9ba026..4267746633 100644 --- a/crates/format/Cargo.toml +++ b/crates/format/Cargo.toml @@ -16,7 +16,7 @@ photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-codecs = { workspace = true } serde = { workspace = true } -serde_json = { workspace = true, features = ["unbounded_depth"] } +serde_json = { workspace = true, features = ["unbounded_depth", "float_roundtrip"] } thiserror = { workspace = true } flate2 = { workspace = true } # Same IEEE CRC-32 the ZIP container needs; already in Cargo.lock through flate2. @@ -34,5 +34,16 @@ rustix = { version = "1.1", default-features = false, features = ["std", "fs"] } [target.'cfg(target_vendor = "apple")'.dependencies] libc = "0.2" +# Replacing a file keeps its ACL (#7073). `fcopyfile(COPYFILE_ACL)` is an `unsafe fn` and +# `fclonefileat(CLONE_ACL)` is APFS-only; exacl is a safe wrapper over acl_get_file/acl_set_file. +[target.'cfg(target_os = "macos")'.dependencies] +exacl = "0.13" + +# `ReplaceFileW` is how Windows replaces a file without dropping its DACL, named streams +# (Zone.Identifier, the Mark of the Web) and creation time (#7094, #7096). winsafe's `kernel` +# feature is a dependency-free safe wrapper; the workspace forbids writing the FFI call here. +[target.'cfg(windows)'.dependencies] +winsafe = { version = "0.0.29", features = ["kernel"] } + [dev-dependencies] proptest = { workspace = true } diff --git a/crates/format/src/atomic.rs b/crates/format/src/atomic.rs index bf39fe2761..8cd349efff 100644 --- a/crates/format/src/atomic.rs +++ b/crates/format/src/atomic.rs @@ -2,8 +2,9 @@ //! //! [`atomic_write`] never truncates the destination. It writes a temporary file in the **same //! directory**, flushes it to stable storage (`sync_all`), renames it over the destination and, -//! on Unix, fsyncs the directory so the rename itself survives a power loss. If anything fails, -//! the destination keeps its previous bytes and the temporary file is removed. +//! flushes the directory so the rename itself survives a power loss. Failures before replacement +//! keep the destination's previous bytes. On Windows a failed publication flush reports that the +//! new file is in place but its durability could not be confirmed (#7858). //! //! - **Windows:** antivirus scanners and the search indexer briefly hold files open, which makes //! the replacing rename fail with `PermissionDenied`. The rename is retried with exponential @@ -13,6 +14,10 @@ //! if it does anyway, the save fails with a clear error. It never falls back to writing the //! destination in place. //! - **Unwritable folder:** the save fails with a clear error instead of writing in place. +//! - **Read-only file:** the save is refused before anything is written (#6600). A rename only +//! needs the folder to be writable, so without this check Linux and macOS would silently replace +//! a `chmod 444` file while Windows refuses; now every platform refuses, as Photoshop does, and +//! the user saves a copy with Save As. //! - **Network shares (macOS):** `sync_all` asks for a full flush to the disk's platters //! (`F_FULLFSYNC`), which SMB shares and some virtual file systems refuse while a plain `fsync` //! works. [`sync_file`] falls back to the plain one there (#1336). @@ -21,8 +26,15 @@ //! this module is never reached there; on `wasm32` every call returns the `std` "unsupported" //! error. //! +//! Replacing a file also carries its metadata across — permission bits, extended attributes, +//! the "downloaded from the internet" mark and the access-control list — so that saving a +//! document does not quietly undo a security decision someone made about the file. That part is +//! [`meta`], and it is best effort: it never fails a save and never touches the bytes. +//! //! [`atomic_write_with`] takes an [`AtomicFs`] so tests can inject failures at each step. +pub mod meta; + use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; @@ -30,6 +42,12 @@ use std::sync::atomic::{AtomicU64, Ordering}; /// The file-system steps [`atomic_write_with`] performs. [`RealFs`] is the real thing; tests /// wrap it to inject failures. pub trait AtomicFs { + /// Whether the existing file at `path` is marked read-only (a missing file is not). A + /// read-only destination is never replaced (#6600). By default nothing is read-only. + fn is_readonly(&self, path: &Path) -> bool { + let _ = path; + false + } /// Create `tmp` (it must not exist), write all of `bytes` and flush it ([`sync_file`]). fn write_new(&self, tmp: &Path, bytes: &[u8]) -> io::Result<()>; /// [`write_new`](Self::write_new), reporting the fraction of `bytes` written so far to @@ -42,11 +60,18 @@ pub trait AtomicFs { fn rename(&self, from: &Path, to: &Path) -> io::Result<()>; /// Remove a (temporary) file. fn remove(&self, path: &Path) -> io::Result<()>; - /// Flush a directory entry change (Unix); a no-op elsewhere. + /// Flush a directory entry change on Unix and Windows; a no-op elsewhere. fn sync_dir(&self, dir: &Path) -> io::Result<()>; - /// Copy the destination's permissions onto the temporary file, so replacing a file keeps its - /// mode. Best effort. - fn copy_permissions(&self, from: &Path, to: &Path) -> io::Result<()>; + /// Copy the metadata a replacement should keep — mode, extended attributes, ACL — from the + /// file about to be replaced onto the temporary file. Best effort; see [`meta`]. + fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()>; + /// Replace the **existing** file `to` with `from`. Everywhere but Windows this is + /// [`rename`](Self::rename); on Windows it keeps the destination's DACL, named streams and + /// creation time ([`meta::replace_existing`]). The default is `rename`, which is what the + /// failure-injecting test file systems want. + fn replace_existing(&self, from: &Path, to: &Path) -> io::Result<()> { + self.rename(from, to) + } } /// The real file system. @@ -76,20 +101,40 @@ impl AtomicFs for RealFs { fn remove(&self, path: &Path) -> io::Result<()> { std::fs::remove_file(path) } + /// Flush directory publication using a directory handle on Unix and Windows. + /// Windows requests only `FILE_ADD_FILE`; opening or flushing the handle can fail the save. fn sync_dir(&self, dir: &Path) -> io::Result<()> { #[cfg(unix)] { - sync_file(&std::fs::File::open(dir)?) + open_dir_for_sync(dir)?.map_or(Ok(()), |d| sync_file(&d)) } - #[cfg(not(unix))] + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt; + + // FILE_ADD_FILE is the directory equivalent of FILE_WRITE_DATA. Requesting only + // this right permits FlushFileBuffers without requiring GENERIC_WRITE's additional + // directory permissions. BACKUP_SEMANTICS opens a directory, not a volume: no + // administrative privilege or volume-wide flush is needed. + const FILE_ADD_FILE: u32 = 0x0002; + const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000; + let directory = std::fs::OpenOptions::new().access_mode(FILE_ADD_FILE).custom_flags(FILE_FLAG_BACKUP_SEMANTICS).open(dir)?; + directory.sync_all() + } + #[cfg(not(any(unix, windows)))] { let _ = dir; Ok(()) } } - fn copy_permissions(&self, from: &Path, to: &Path) -> io::Result<()> { - let perms = std::fs::metadata(from)?.permissions(); - std::fs::set_permissions(to, perms) + fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> { + meta::copy_metadata(from, to) + } + fn replace_existing(&self, from: &Path, to: &Path) -> io::Result<()> { + meta::replace_existing(from, to) + } + fn is_readonly(&self, path: &Path) -> bool { + std::fs::metadata(path).is_ok_and(|m| m.is_file() && m.permissions().readonly()) } } @@ -143,8 +188,8 @@ impl RenameRetry { /// Atomically replace (or create) `path` with `bytes`. See the module docs. /// -/// Errors keep their [`io::ErrorKind`] and say what happened and that the original file is -/// unchanged. +/// Errors keep their [`io::ErrorKind`]. Before replacement they say the original is unchanged; +/// a Windows publication-flush error says the new file may not survive a power loss. pub fn atomic_write(path: &Path, bytes: &[u8]) -> io::Result<()> { atomic_write_with(&RealFs, RenameRetry::platform(), path, bytes) } @@ -162,13 +207,16 @@ pub fn atomic_write_with(fs: &dyn AtomicFs, retry: RenameRetry, path: &Path, byt /// [`atomic_write_with`] reporting write progress (see [`atomic_write_progress`]). pub fn atomic_write_with_progress(fs: &dyn AtomicFs, retry: RenameRetry, path: &Path, bytes: &[u8], progress: &dyn Fn(f32)) -> io::Result<()> { - let target = resolve_symlink(path); + let target = resolve_symlink(path).map_err(|e| context(e, format!("cannot save {}", path.display())))?; let name = target.file_name().ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, format!("{}: not a file path", path.display())))?; let dir = match target.parent() { Some(d) if !d.as_os_str().is_empty() => d.to_path_buf(), _ => PathBuf::from("."), }; std::fs::create_dir_all(&dir).map_err(|e| context(e, format!("cannot create the folder {}", dir.display())))?; + if fs.is_readonly(&target) { + return Err(io::Error::new(io::ErrorKind::PermissionDenied, format!("cannot save {}: the file is read-only; {UNCHANGED}", path.display()))); + } // Create a fresh temporary file next to the target (same directory, so the rename never // crosses a device). The name is unique per process and call; retry a few on collisions. @@ -190,21 +238,95 @@ pub fn atomic_write_with_progress(fs: &dyn AtomicFs, retry: RenameRetry, path: & } let tmp = tmp.ok_or_else(|| io::Error::new(io::ErrorKind::AlreadyExists, format!("{}: could not create a temporary file", dir.display())))?; - // Keep the existing file's mode (e.g. 0600) instead of the process default. - if target.exists() { - let _ = fs.copy_permissions(&target, &tmp); + // Keep what the file being replaced carried: its mode (e.g. 0600) instead of the process + // default, its extended attributes, and its ACL. Best effort, and before the swap, so the + // file the user ends up with never passes through a more permissive state. + let replacing = target.exists(); + if replacing { + let _ = fs.copy_metadata(&target, &tmp); } - if let Err(e) = retry_rename(retry, || fs.rename(&tmp, &target)) { + // Replacing an existing file goes through `replace_existing`, which on Windows is the one + // call that can keep the destination's DACL and named streams; creating a new one is a + // plain rename, with nothing to preserve. + let swap = || if replacing { fs.replace_existing(&tmp, &target) } else { fs.rename(&tmp, &target) }; + if let Err(e) = retry_rename(retry, swap) { let _ = fs.remove(&tmp); return Err(rename_error(e, path)); } - // The new bytes are in place; a failed directory fsync can't be undone and must not report - // the save as failed, so it is best effort. - let _ = fs.sync_dir(&dir); + // The new bytes are in place, but until the directory is flushed the rename may not survive + // a power loss (#4968). Windows fails the save on any failed flush (#7858). Elsewhere a file + // system that can't flush directories at all counts as done; any other failure is reported, + // so the caller keeps the document unsaved and its recovery data instead of discarding them + // on a save that may not last. + #[cfg(windows)] + fs.sync_dir(&dir).map_err(|e| publication_error(e, path))?; + #[cfg(not(windows))] + dir_synced(fs.sync_dir(&dir)).map_err(|e| unconfirmed_error(e, path))?; Ok(()) } +/// `dir` opened to flush it, or `None` when it can't be opened for that because it has no read +/// permission (a write-and-search-only folder): no flush can be attempted there, so none failed. +/// Any other error is returned. +#[cfg(unix)] +fn open_dir_for_sync(dir: &Path) -> io::Result> { + match std::fs::File::open(dir) { + Ok(d) => Ok(Some(d)), + Err(e) if e.kind() == io::ErrorKind::PermissionDenied => Ok(None), + Err(e) => Err(e), + } +} + +/// `result` of flushing a directory, with "this file system can't flush directories" counted as +/// success: `EINVAL` (Linux file systems without directory fsync), `ENOTSUP`/`EOPNOTSUPP` and, +/// on Apple platforms, `ENOTTY`, or [`io::ErrorKind::Unsupported`]. Waiting or retrying won't +/// change those, and failing every save there would make saving impossible. Any other error is +/// returned: the flush was attempted and failed. +#[cfg(not(windows))] +pub(crate) fn dir_synced(result: io::Result<()>) -> io::Result<()> { + match result { + Err(e) if dir_sync_unsupported(&e) => Ok(()), + other => other, + } +} + +#[cfg(not(windows))] +fn dir_sync_unsupported(e: &io::Error) -> bool { + if e.kind() == io::ErrorKind::Unsupported { + return true; + } + // errno values: EINVAL is 22 everywhere; ENOTSUP and EOPNOTSUPP are 95 on Linux and + // Android, 45 and 102 on Apple platforms, 45 on the BSDs; ENOTTY is 25. Elsewhere 45 means + // something else (illumos: EDEADLK), so only EINVAL and `Unsupported` count there. + const EINVAL: i32 = 22; + let unsupported: &[i32] = if cfg!(any(target_os = "linux", target_os = "android")) { + &[EINVAL, 95] + } else if cfg!(target_vendor = "apple") { + &[EINVAL, 45, 102, 25] + } else if cfg!(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "dragonfly")) { + &[EINVAL, 45] + } else if cfg!(unix) { + &[EINVAL] + } else { + &[] + }; + e.raw_os_error().is_some_and(|code| unsupported.contains(&code)) +} + +/// The error for a file that was published but whose directory flush failed: same kind, saying +/// the file is in place but may not survive a power loss. (Windows reports `publication_error`.) +#[cfg(not(windows))] +fn unconfirmed_error(e: io::Error, path: &Path) -> io::Error { + context( + e, + format!( + "{} was written, but the folder could not be flushed to disk, so the save may not survive a power loss or crash; save again or to another folder", + path.display() + ), + ) +} + /// Run `rename`, retrying with backoff while it fails with `PermissionDenied` (see /// [`RenameRetry`]). For callers that rename through their own handles (capability directories). pub fn retry_rename(retry: RenameRetry, mut rename: impl FnMut() -> io::Result<()>) -> io::Result<()> { @@ -229,12 +351,37 @@ fn sleep_ms(ms: u64) { #[cfg(target_arch = "wasm32")] fn sleep_ms(_ms: u64) {} +/// How deep a symlink chain we follow before giving up, like the OS limit. +const MAX_LINK_DEPTH: u32 = 40; + /// Write through a symlink to the file it points at, instead of replacing the link. -fn resolve_symlink(path: &Path) -> PathBuf { - match std::fs::symlink_metadata(path) { - Ok(m) if m.file_type().is_symlink() => std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf()), - _ => path.to_path_buf(), +/// +/// A dangling link (its target, or a folder on the way to it, doesn't exist yet) resolves to its +/// target too, so saving through one creates the target instead of silently turning the link +/// into a regular file (#6605). A chain that never ends is an error, never a link replacement. +fn resolve_symlink(path: &Path) -> io::Result { + if !std::fs::symlink_metadata(path).is_ok_and(|m| m.file_type().is_symlink()) { + return Ok(path.to_path_buf()); } + // A resolvable chain: canonicalize follows every link and relative target in one go. + if let Ok(real) = std::fs::canonicalize(path) { + return Ok(real); + } + // Dangling chain: walk the links lexically; the final target may not exist yet. + let mut current = path.to_path_buf(); + for _ in 0..MAX_LINK_DEPTH { + let Ok(link) = std::fs::read_link(¤t) else { + return Ok(current); // not (or no longer) a link: the chain ends here + }; + let base = current.parent().unwrap_or(Path::new(".")).to_path_buf(); + // `..` stays in the path for the OS to resolve: folding it by text is wrong when a folder + // on the way is itself a symlink. + current = if link.is_absolute() { link } else { base.join(link) }; + if !std::fs::symlink_metadata(¤t).is_ok_and(|m| m.file_type().is_symlink()) { + return Ok(current); + } + } + Err(io::Error::new(io::ErrorKind::InvalidInput, format!("{}: too many levels of symbolic links", path.display()))) } /// A unique hidden temporary file name to write beside `file` before renaming over it. @@ -252,6 +399,15 @@ fn context(e: io::Error, msg: String) -> io::Error { const UNCHANGED: &str = "the original file was not changed"; +/// Replacement already happened: never claim that the original file is unchanged or undo it. +#[cfg(windows)] +pub(crate) fn publication_error(error: io::Error, path: &Path) -> io::Error { + io::Error::new( + error.kind(), + format!("wrote {}, but could not confirm the save is durable; it may not survive a power loss or crash: {error}", path.display()), + ) +} + fn write_error(e: io::Error, path: &Path, dir: &Path) -> io::Error { let msg = match e.kind() { io::ErrorKind::PermissionDenied | io::ErrorKind::ReadOnlyFilesystem => { diff --git a/crates/format/src/atomic/meta.rs b/crates/format/src/atomic/meta.rs new file mode 100644 index 0000000000..6e1aaa15fe --- /dev/null +++ b/crates/format/src/atomic/meta.rs @@ -0,0 +1,315 @@ +//! What a replacement keeps from the file it replaces. +//! +//! Saving writes a new file and renames it over the old one, so the user gets a file with the +//! right bytes and none of the old file's history: everything the operating system and other +//! programs had attached to the file that was just unlinked is gone. Some of it is a security +//! decision — *this came from the internet*, *only these people may read it* — so a save that +//! drops it quietly weakens the user's protections (#6603, #7073, #7094, #7095, #7096). +//! +//! | | macOS | Linux | Windows | +//! |---|---|---|---| +//! | permission bits | [`copy_metadata`] | [`copy_metadata`] | [`copy_metadata`] (read-only flag) | +//! | extended attributes (Finder tags, `user.*`) | `copy_xattrs` | `copy_xattrs` | — | +//! | "downloaded from the internet" | `com.apple.quarantine`, an xattr | — | `Zone.Identifier`, a named stream kept by [`replace_existing`] | +//! | access-control list | `copy_acl` (`exacl`) | not carried over (see below) | DACL, kept by [`replace_existing`] | +//! | creation time | `copy_created` (#9388) | — | kept by [`replace_existing`] | +//! +//! Everything here except [`replace_existing`] is **best effort and never touches the file's +//! bytes**: a save that cannot carry a piece of metadata across is still a save, because a +//! document the user can open matters more than a Finder tag. [`replace_existing`] *is* the +//! replacement step, so it holds the atomicity guarantee instead: it never leaves the +//! destination missing, and it falls back to a plain rename rather than fail a save. +//! +//! Not carried over, and why: Linux POSIX ACLs live in `system.posix_acl_access`, which +//! `listxattr` does not report, so copying the listed attributes misses them; they would need +//! their own step. + +use std::io; +use std::path::Path; + +#[cfg(unix)] +use std::os::fd::BorrowedFd; + +/// Largest attribute-name list [`copy_xattrs`] will read. Names are short; a file with a +/// megabyte of them is not something a save should spend memory on. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +const MAX_XATTR_NAMES: usize = 1 << 20; + +/// Largest single attribute value [`copy_xattrs`] will carry over. Finder tags and `user.*` +/// attributes are tens of bytes; a resource fork can be large, and past this it is not worth +/// holding a copy of it in memory during a save. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +const MAX_XATTR_VALUE: usize = 16 << 20; + +/// First read size for an attribute list or value; grown on `ERANGE`. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +const XATTR_CHUNK: usize = 4 << 10; + +/// Copy, best effort, everything [`crate::atomic::atomic_write`] should carry from the file it +/// is about to replace (`from`) onto the temporary file that will replace it (`to`). +/// +/// Errors are the caller's to ignore: each piece is attempted on its own, and the first failure +/// is returned only so a caller that wants to log one can. +pub fn copy_metadata(from: &Path, to: &Path) -> io::Result<()> { + // The permission bits go on last. Writing an attribute needs the destination open for + // writing, and replacing a read-only (0444) file would copy that mode onto the temporary + // file and then be unable to touch it again. + #[cfg(unix)] + let attributes = copy_xattrs_at(from, to); + #[cfg(not(unix))] + let attributes = Ok(()); + + // The creation time before the ACL: it needs the destination open for writing, which a + // copied ACL denying its owner write access would refuse. + #[cfg(target_os = "macos")] + let created = copy_created(from, to); + #[cfg(not(target_os = "macos"))] + let created = Ok(()); + + #[cfg(target_os = "macos")] + let acl = copy_acl(from, to); + #[cfg(not(target_os = "macos"))] + let acl = Ok(()); + + // A save over a 0600 file must not widen it to whatever the process default is. + let permissions = std::fs::metadata(from).and_then(|m| std::fs::set_permissions(to, m.permissions())); + + attributes.and(acl).and(created).and(permissions) +} + +/// The creation time ("Created" in Finder) of the file being replaced, onto the file replacing +/// it: the rename leaves a new file born at the time of the save, where Photoshop keeps the +/// original's (#9388). The rename keeps the time set here. +#[cfg(target_os = "macos")] +fn copy_created(from: &Path, to: &Path) -> io::Result<()> { + let old = std::fs::File::open(from)?; + let new = std::fs::OpenOptions::new().write(true).open(to)?; + copy_created_file(&old, &new) +} + +/// [`copy_created`] through two open files rather than their paths, for the automation write +/// path's capability-checked handles (see [`copy_xattrs`]). `to` must be open for writing. +/// Nothing to do outside macOS: Windows keeps the time in [`replace_existing`], and Linux has no +/// settable creation time. +pub fn copy_created_file(from: &std::fs::File, to: &std::fs::File) -> io::Result<()> { + #[cfg(target_os = "macos")] + { + use std::os::macos::fs::FileTimesExt; + let created = from.metadata()?.created()?; + to.set_times(std::fs::FileTimes::new().set_created(created)) + } + #[cfg(not(target_os = "macos"))] + { + let _ = (from, to); + Ok(()) + } +} + +/// [`copy_metadata`]'s extended-attribute step, by path: open both files and hand the +/// descriptors to [`copy_xattrs`]. +#[cfg(unix)] +fn copy_xattrs_at(from: &Path, to: &Path) -> io::Result<()> { + use std::os::fd::AsFd; + let old = std::fs::File::open(from)?; + // Writing an attribute needs write access to the file, not only to its directory. + let new = std::fs::OpenOptions::new().write(true).open(to)?; + copy_xattrs(old.as_fd(), new.as_fd()) +} + +/// Copy every extended attribute of `from` onto `to`, best effort, through the two open files +/// rather than their paths. +/// +/// Descriptors, not paths, because the automation write path holds capability-checked handles +/// and must never reach back out to an ambient path to finish a write (#7095). The same reason +/// makes this the right shape for [`copy_metadata`] too. +/// +/// An attribute that cannot be read or cannot be set is skipped: `com.apple.provenance` and +/// `com.apple.rootless` on macOS and `security.*` on Linux belong to the system and refuse to +/// be written by an ordinary process, and failing a document save over one would be absurd. +/// `Err` means the attribute *list* could not be read, so nothing was copied. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +pub fn copy_xattrs(from: BorrowedFd<'_>, to: BorrowedFd<'_>) -> io::Result<()> { + use rustix::fs::{XattrFlags, fsetxattr}; + + for name in list_xattrs(from)?.split(|b| *b == 0) { + if name.is_empty() || skip_xattr(name) { + continue; + } + let Some(value) = read_xattr(from, name) else { continue }; + let _ = fsetxattr(to, name, &value, XattrFlags::empty()); + } + Ok(()) +} + +/// [`copy_xattrs`] where rustix offers no extended-attribute calls (FreeBSD and the other BSDs): +/// nothing is copied, as on Windows. +#[cfg(all(unix, not(any(target_os = "linux", target_os = "android", target_vendor = "apple"))))] +pub fn copy_xattrs(_from: BorrowedFd<'_>, _to: BorrowedFd<'_>) -> io::Result<()> { + Ok(()) +} + +/// Attributes that describe how the *old* file's bytes are stored rather than anything about +/// the document, and so must never be pinned onto a file holding different bytes. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +fn skip_xattr(name: &[u8]) -> bool { + // `com.apple.decmpfs` marks a file whose data lives compressed inside the attribute itself. + // The kernel hides it from a plain `listxattr` and refuses to set it, but a file claiming + // compressed storage it does not have would read as garbage, so never try. + name == b"com.apple.decmpfs" +} + +/// The NUL-separated attribute names of `fd`. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +fn list_xattrs(fd: BorrowedFd<'_>) -> io::Result> { + let mut buf = vec![0u8; XATTR_CHUNK]; + loop { + match rustix::fs::flistxattr(fd, buf.as_mut_slice()) { + Ok(len) => { + buf.truncate(len.min(buf.len())); + return Ok(buf); + } + Err(e) if e == rustix::io::Errno::RANGE && buf.len() < MAX_XATTR_NAMES => { + buf.resize(buf.len().saturating_mul(4).min(MAX_XATTR_NAMES), 0); + } + Err(e) => return Err(e.into()), + } + } +} + +/// The value of one attribute, or `None` if it cannot be read or is larger than +/// [`MAX_XATTR_VALUE`]. A missing value is normal: another process may remove an attribute +/// between the listing and the read. +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +fn read_xattr(fd: BorrowedFd<'_>, name: &[u8]) -> Option> { + let mut buf = vec![0u8; XATTR_CHUNK]; + loop { + match rustix::fs::fgetxattr(fd, name, buf.as_mut_slice()) { + Ok(len) => { + buf.truncate(len.min(buf.len())); + return Some(buf); + } + Err(e) if e == rustix::io::Errno::RANGE && buf.len() < MAX_XATTR_VALUE => { + buf.resize(buf.len().saturating_mul(4).min(MAX_XATTR_VALUE), 0); + } + Err(_) => return None, + } + } +} + +/// Copy an explicit macOS access-control list (`chmod +a`) from `from` onto `to` (#7073). +/// +/// `fcopyfile(COPYFILE_ACL)` is the obvious call and is an `unsafe fn`, which this workspace +/// forbids; `fclonefileat` with `CLONE_ACL` is safe but only works on APFS and would mean +/// writing every save as a clone-then-truncate. `exacl` is a small, safe wrapper over +/// `acl_get_file`/`acl_set_file` that works on every file system that has ACLs at all. +/// +/// An old file with no ACL leaves `to` alone: the new file may have picked up the folder's +/// inherited entries, and taking access away is a worse surprise than leaving it as the folder +/// says it should be. +#[cfg(target_os = "macos")] +fn copy_acl(from: &Path, to: &Path) -> io::Result<()> { + let entries = exacl::getfacl(from, None)?; + if entries.is_empty() { + return Ok(()); + } + exacl::setfacl(&[to], &entries, None) +} + +/// Replace the **existing** file `to` with `from`, keeping what the destination owns rather than +/// its bytes. +/// +/// Everywhere but Windows this is exactly [`std::fs::rename`], and the metadata is carried by +/// [`copy_metadata`] onto `from` beforehand. On Windows it is `ReplaceFileW`, which preserves +/// the replaced file's DACL (#7096), its named streams — `Zone.Identifier`, the Mark of the Web +/// (#7094) — its creation time, object identifier, compression and encryption, in the one call +/// Microsoft documents for the save-through-a-temporary-file pattern. None of those have a safe +/// path-based API to copy by hand. +pub fn replace_existing(from: &Path, to: &Path) -> io::Result<()> { + #[cfg(windows)] + { + windows_replace(from, to) + } + #[cfg(not(windows))] + { + std::fs::rename(from, to) + } +} + +/// `ReplaceFileW`, with the plain rename as the fallback. See [`replace_existing`]. +/// +/// **A backup name is not optional.** `ReplaceFile` without one deletes the replaced file before +/// the final rename, and documents `ERROR_UNABLE_TO_MOVE_REPLACEMENT`: "the replaced file no +/// longer exists and the replacement file exists under its original name" — the user's document +/// gone, its replacement left under a hidden temporary name. With a backup name the same failure +/// leaves both files where they were. So this always passes one, and deletes it on the way out. +/// +/// The failures and what each leaves behind (from the `ReplaceFileW` reference): +/// +/// - `ERROR_UNABLE_TO_MOVE_REPLACEMENT_2` (1177) is the only one that moves anything: the +/// replacement keeps its name but has already inherited the destination's streams and +/// attributes, and the destination's old content is at the backup name. Finishing the rename +/// by hand completes the save *with* its metadata; if even that fails, the backup goes back to +/// the destination so the user's file is where they left it. +/// - Every other error leaves the replaced and replacement files under their own names, so a +/// plain rename still saves the document — without the metadata, which is where this started. +#[cfg(windows)] +fn windows_replace(from: &Path, to: &Path) -> io::Result<()> { + use winsafe::co::REPLACEFILE; + + /// `ERROR_UNABLE_TO_MOVE_REPLACEMENT_2`: the destination now lives under the backup name. + const MOVE_REPLACEMENT_2: i32 = 1177; + + let backup = backup_path(to); + let paths = (to.to_str(), from.to_str(), backup.as_deref().and_then(Path::to_str)); + // `ReplaceFileW` takes `&str`; a path that is not valid Unicode, or one past MAX_PATH, is + // the plain rename's job. + let (Some(replaced), Some(replacement), Some(backup_str)) = paths else { + return std::fs::rename(from, to); + }; + + let flags = REPLACEFILE::IGNORE_MERGE_ERRORS | REPLACEFILE::IGNORE_ACL_ERRORS; + match winsafe::ReplaceFile(replaced, replacement, Some(backup_str), flags) { + Ok(()) => { + remove_backup(backup.as_deref()); + Ok(()) + } + Err(e) if e.raw() as i32 == MOVE_REPLACEMENT_2 => match std::fs::rename(from, to) { + Ok(()) => { + remove_backup(backup.as_deref()); + Ok(()) + } + Err(rename_failed) => { + if let Some(backup) = backup.as_deref() { + let _ = std::fs::rename(backup, to); + } + Err(rename_failed) + } + }, + Err(_) => { + remove_backup(backup.as_deref()); + std::fs::rename(from, to) + } + } +} + +/// A unique name beside `to` for `ReplaceFile` to park the replaced file under. It shares the +/// temporary-file suffix, so the save's own cleanup and the tests' leftover check cover it. +#[cfg(windows)] +fn backup_path(to: &Path) -> Option { + let name = to.file_name()?.to_string_lossy().into_owned(); + let dir = match to.parent() { + Some(d) if !d.as_os_str().is_empty() => d, + _ => Path::new("."), + }; + Some(dir.join(super::temp_name(&name))) +} + +#[cfg(windows)] +fn remove_backup(backup: Option<&Path>) { + if let Some(backup) = backup { + let _ = std::fs::remove_file(backup); + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/format/src/atomic/meta/tests.rs b/crates/format/src/atomic/meta/tests.rs new file mode 100644 index 0000000000..834e87e66e --- /dev/null +++ b/crates/format/src/atomic/meta/tests.rs @@ -0,0 +1,310 @@ +//! What a save keeps from the file it replaces. +//! +//! The Unix tests run for real. The Windows ones exercise `ReplaceFileW` through +//! [`crate::atomic::atomic_write`] and can only run on Windows, so they are `#[cfg(windows)]` +//! and are covered by the Windows CI job, not by a developer on a Mac. + +use crate::atomic::atomic_write; +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; + +struct TempDir(PathBuf); +impl TempDir { + fn new(tag: &str) -> Self { + static N: AtomicU64 = AtomicU64::new(0); + let d = std::env::temp_dir().join(format!("pc-meta-{tag}-{}-{}", std::process::id(), N.fetch_add(1, Ordering::Relaxed))); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + Self(d) + } + fn join(&self, name: &str) -> PathBuf { + self.0.join(name) + } +} +impl Drop for TempDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// No temporary or backup file is left beside the saved document. +fn assert_no_temp(dir: &Path) { + let leftovers: Vec<_> = + std::fs::read_dir(dir).unwrap().flatten().map(|e| e.file_name().to_string_lossy().into_owned()).filter(|n| n.ends_with(".tmp")).collect(); + assert!(leftovers.is_empty(), "temporary files left behind: {leftovers:?}"); +} + +// ---------------------------------------------------------------- extended attributes (Unix) + +#[cfg(any(target_os = "linux", target_os = "android", target_vendor = "apple"))] +mod xattrs { + use super::*; + + /// macOS wants a reverse-DNS name; Linux only lets an unprivileged process write `user.*`. + #[cfg(target_os = "macos")] + const TAG: &str = "com.apple.metadata:_kMDItemUserTags"; + #[cfg(not(target_os = "macos"))] + const TAG: &str = "user.photocraft.test"; + + fn set(path: &Path, name: &str, value: &[u8]) -> bool { + rustix::fs::setxattr(path, name, value, rustix::fs::XattrFlags::empty()).is_ok() + } + + fn get(path: &Path, name: &str) -> Option> { + let mut buf = vec![0u8; 1 << 20]; + let len = rustix::fs::getxattr(path, name, buf.as_mut_slice()).ok()?; + buf.truncate(len); + Some(buf) + } + + /// #6603: the attributes of the replaced file — a Finder tag, a `user.*` attribute — are on + /// the file the user is left with. + #[test] + fn replacing_a_file_keeps_its_extended_attributes() { + let d = TempDir::new("xattr"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + if !set(&p, TAG, b"tagged") { + // tmpfs and some CI file systems have no xattr support; nothing to assert. + return; + } + + atomic_write(&p, b"second, longer").unwrap(); + + assert_eq!(std::fs::read(&p).unwrap(), b"second, longer"); + assert_eq!(get(&p, TAG).as_deref(), Some(&b"tagged"[..]), "the replaced file's attribute was dropped"); + assert_no_temp(&d.0); + } + + /// A large value still comes across (and is not silently truncated). + #[test] + fn a_large_attribute_value_survives() { + let d = TempDir::new("xattr-big"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + // Comfortably past the first read buffer, so the grow-on-ERANGE path runs. + let value = vec![0xABu8; 64 << 10]; + if !set(&p, TAG, &value) { + return; + } + + atomic_write(&p, b"second").unwrap(); + + assert_eq!(get(&p, TAG), Some(value)); + } + + /// A read-only file is refused outright (#6600), so a save never reaches the metadata copy and + /// the file keeps everything it had. The writable cases above cover the copy itself. + #[test] + fn a_read_only_file_keeps_its_attributes_and_its_mode() { + use std::os::unix::fs::PermissionsExt; + let d = TempDir::new("xattr-ro"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + if !set(&p, TAG, b"tagged") { + return; + } + std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o444)).unwrap(); + + // #6600: a read-only file is refused before anything is written, so the question this test + // asks — does a replace keep the attributes and the mode — is answered by the save not + // happening. The writable case above covers attributes surviving a replace. + let e = atomic_write(&p, b"second, longer").expect_err("a read-only file is refused"); + assert_eq!(e.kind(), std::io::ErrorKind::PermissionDenied); + + assert_eq!(std::fs::read(&p).unwrap(), b"first", "the original was left alone"); + assert_eq!(get(&p, TAG).as_deref(), Some(&b"tagged"[..]), "a read-only file lost its attribute"); + assert_eq!(std::fs::metadata(&p).unwrap().permissions().mode() & 0o777, 0o444); + } + + /// Creating a file that was not there sets no attributes and does not fail. + #[test] + fn creating_a_new_file_needs_no_source() { + let d = TempDir::new("xattr-new"); + let p = d.join("fresh.psd"); + atomic_write(&p, b"bytes").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"bytes"); + assert!(get(&p, TAG).is_none()); + } + + /// The descriptor-based copy is what the automation write path uses (#7095): it works on two + /// open files and never looks at a path. + #[test] + fn copy_xattrs_works_on_descriptors() { + use std::os::fd::AsFd; + let d = TempDir::new("xattr-fd"); + let (from, to) = (d.join("from"), d.join("to")); + std::fs::write(&from, b"a").unwrap(); + std::fs::write(&to, b"b").unwrap(); + if !set(&from, TAG, b"carried") { + return; + } + + let old = std::fs::File::open(&from).unwrap(); + let new = std::fs::OpenOptions::new().write(true).open(&to).unwrap(); + super::super::copy_xattrs(old.as_fd(), new.as_fd()).unwrap(); + + assert_eq!(get(&to, TAG).as_deref(), Some(&b"carried"[..])); + assert_eq!(std::fs::read(&to).unwrap(), b"b", "copying attributes must not touch the bytes"); + } +} + +// --------------------------------------------------------------------------- mode (Unix) + +/// The mode was already kept before any of this; it still is. +#[cfg(unix)] +#[test] +fn replacing_a_file_keeps_its_mode() { + use std::os::unix::fs::PermissionsExt; + let d = TempDir::new("mode"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o600)).unwrap(); + + atomic_write(&p, b"second").unwrap(); + + assert_eq!(std::fs::metadata(&p).unwrap().permissions().mode() & 0o777, 0o600); + assert_no_temp(&d.0); +} + +// ----------------------------------------------------------------------------- ACL (macOS) + +#[cfg(target_os = "macos")] +mod acl { + use super::*; + + /// #7073: an ACL entry added with `chmod +a` is still there after a save. + #[test] + fn replacing_a_file_keeps_its_acl() { + let d = TempDir::new("acl"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + + // The same thing `chmod +a` does: one explicit entry for the user running the test, so + // no second account has to exist. + let Ok(user) = std::env::var("USER") else { return }; + let entries = [exacl::AclEntry::allow_user(&user, exacl::Perm::READ | exacl::Perm::WRITE, None)]; + if exacl::setfacl(&[&p], &entries, None).is_err() { + // A file system without ACL support (a RAM disk, some CI volumes). + return; + } + let before = exacl::getfacl(&p, None).unwrap(); + assert!(!before.is_empty(), "the test could not set an ACL to begin with"); + + atomic_write(&p, b"second, longer").unwrap(); + + assert_eq!(std::fs::read(&p).unwrap(), b"second, longer"); + assert_eq!(exacl::getfacl(&p, None).unwrap(), before, "the replaced file's ACL was dropped"); + assert_no_temp(&d.0); + } + + /// A file with no ACL keeps having no ACL, rather than gaining an empty one. + #[test] + fn a_file_without_an_acl_is_left_alone() { + let d = TempDir::new("acl-none"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + let before = exacl::getfacl(&p, None).unwrap(); + + atomic_write(&p, b"second").unwrap(); + + assert_eq!(exacl::getfacl(&p, None).unwrap(), before); + } +} + +// --------------------------------------------------------------- named streams and ACL (Windows) + +/// Unverified on a Mac: these run only on Windows (CI's Windows job). +#[cfg(windows)] +mod windows { + use super::*; + + /// Open a named stream of `path`, e.g. `Zone.Identifier`. + fn stream(path: &Path, name: &str) -> PathBuf { + let mut s = path.as_os_str().to_os_string(); + s.push(":"); + s.push(name); + PathBuf::from(s) + } + + /// #7094: a file marked as downloaded from the internet is still marked after a save, so the + /// shell and other applications keep warning about it. + #[test] + fn replacing_a_file_keeps_its_mark_of_the_web() { + let d = TempDir::new("motw"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + let motw = b"[ZoneTransfer]\r\nZoneId=3\r\n"; + std::fs::write(stream(&p, "Zone.Identifier"), motw).unwrap(); + + atomic_write(&p, b"second, longer").unwrap(); + + assert_eq!(std::fs::read(&p).unwrap(), b"second, longer"); + assert_eq!(std::fs::read(stream(&p, "Zone.Identifier")).unwrap(), motw, "the Mark of the Web was dropped"); + assert_no_temp(&d.0); + } + + /// Any other named stream rides along too, because `ReplaceFileW` keeps the lot. + #[test] + fn replacing_a_file_keeps_other_named_streams() { + let d = TempDir::new("ads"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + std::fs::write(stream(&p, "photocraft.test"), b"side data").unwrap(); + + atomic_write(&p, b"second").unwrap(); + + assert_eq!(std::fs::read(stream(&p, "photocraft.test")).unwrap(), b"side data"); + } + + /// #7096: an explicit ACL entry survives, instead of the file falling back to whatever the + /// folder hands down. Compared as the string `icacls` prints, which is what a user checks. + #[test] + fn replacing_a_file_keeps_its_explicit_acl() { + let d = TempDir::new("dacl"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + + // Grant the current user an entry that inheritance would not have produced. + let user = std::env::var("USERNAME").unwrap_or_default(); + if user.is_empty() { + return; + } + let granted = std::process::Command::new("icacls").arg(&p).arg("/grant").arg(format!("{user}:(RX)")).output(); + if !granted.map(|o| o.status.success()).unwrap_or(false) { + return; + } + let before = std::process::Command::new("icacls").arg(&p).output().unwrap().stdout; + + atomic_write(&p, b"second, longer").unwrap(); + + let after = std::process::Command::new("icacls").arg(&p).output().unwrap().stdout; + assert_eq!(String::from_utf8_lossy(&after), String::from_utf8_lossy(&before), "the explicit ACL was dropped"); + assert_no_temp(&d.0); + } + + /// `ReplaceFileW` also keeps the creation time, which the rename used to reset (#6603). + #[test] + fn replacing_a_file_keeps_its_creation_time() { + let d = TempDir::new("born"); + let p = d.join("doc.psd"); + atomic_write(&p, b"first").unwrap(); + let before = std::fs::metadata(&p).unwrap().created().unwrap(); + + std::thread::sleep(std::time::Duration::from_millis(50)); + atomic_write(&p, b"second").unwrap(); + + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), before); + } + + /// Creating a file where there was none still works: there is nothing to replace, so the + /// write never reaches `ReplaceFileW`. + #[test] + fn creating_a_new_file_still_works() { + let d = TempDir::new("new"); + let p = d.join("fresh.psd"); + atomic_write(&p, b"bytes").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"bytes"); + assert_no_temp(&d.0); + } +} diff --git a/crates/format/src/atomic/tests.rs b/crates/format/src/atomic/tests.rs index 8e71bc7ce3..c768044e06 100644 --- a/crates/format/src/atomic/tests.rs +++ b/crates/format/src/atomic/tests.rs @@ -41,6 +41,10 @@ struct Faulty { /// Fail the first N renames with PermissionDenied (a transient lock), then succeed. locked_renames: Cell, renames: Cell, + /// Fail the directory flush (the durability barrier after publication, #4968, #7858) + /// with this raw OS error, or with this kind. + fail_sync_dir: Option>, + synced: Cell, } impl AtomicFs for Faulty { @@ -66,16 +70,55 @@ impl AtomicFs for Faulty { fn remove(&self, path: &Path) -> io::Result<()> { RealFs.remove(path) } + /// Count publication barriers and optionally fail before the real directory flush. fn sync_dir(&self, dir: &Path) -> io::Result<()> { - RealFs.sync_dir(dir) + self.synced.set(self.synced.get() + 1); + match self.fail_sync_dir { + Some(Ok(code)) => Err(io::Error::from_raw_os_error(code)), + Some(Err(kind)) => Err(io::Error::new(kind, "injected directory flush failure")), + None => RealFs.sync_dir(dir), + } } - fn copy_permissions(&self, from: &Path, to: &Path) -> io::Result<()> { - RealFs.copy_permissions(from, to) + fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> { + RealFs.copy_metadata(from, to) } } const NO_RETRY: RenameRetry = RenameRetry { attempts: 0, first_delay_ms: 0 }; +/// A successful rename does not establish power-loss durability. A failed Windows publication +/// barrier must leave the new bytes in place, report uncertainty, and allow a subsequent retry. +#[cfg(windows)] +#[test] +fn a_failed_publication_flush_is_reported_and_can_be_retried() { + let d = TempDir::new("publication-flush"); + let p = d.0.join("document.pcraft"); + std::fs::write(&p, ORIGINAL).unwrap(); + let fs = Faulty { fail_sync_dir: Some(Err(io::ErrorKind::Other)), ..Default::default() }; + + let error = atomic_write_with(&fs, NO_RETRY, &p, b"new document").unwrap_err(); + assert_eq!(fs.synced.get(), 1, "{error}"); + assert_eq!(error.kind(), io::ErrorKind::Other); + assert!(error.to_string().contains("document.pcraft"), "{error}"); + assert!(error.to_string().contains("may not survive"), "{error}"); + assert!(!error.to_string().contains(UNCHANGED), "the replacement already happened: {error}"); + assert_eq!(std::fs::read(&p).unwrap(), b"new document"); + assert_no_temp(&d.0); + + atomic_write(&p, b"retried document").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"retried document"); + assert_no_temp(&d.0); +} + +/// A real Windows directory handle must flush successfully and preserve directory-open errors. +#[cfg(windows)] +#[test] +fn windows_directory_flush_opens_a_directory_and_reports_open_errors() { + let d = TempDir::new("sync-directory"); + RealFs.sync_dir(&d.0).unwrap(); + assert_eq!(RealFs.sync_dir(&d.0.join("missing")).unwrap_err().kind(), io::ErrorKind::NotFound); +} + #[test] fn replaces_and_creates() { let d = TempDir::new("ok"); @@ -113,6 +156,7 @@ fn creates_missing_parent_folders() { assert_eq!(std::fs::read(&p).unwrap(), b"x"); } +/// A partial temporary write must preserve the original and never reach publication or its flush. #[test] fn write_failure_halfway_keeps_original() { let d = TempDir::new("write"); @@ -124,9 +168,11 @@ fn write_failure_halfway_keeps_original() { assert!(e.to_string().contains("not changed"), "{e}"); assert_eq!(std::fs::read(&p).unwrap(), ORIGINAL); assert_eq!(fs.renames.get(), 0, "never renames after a failed write"); + assert_eq!(fs.synced.get(), 0, "nothing was published"); assert_no_temp(&d.0); } +/// A failed replacement must preserve the original and skip the publication barrier. #[test] fn rename_failure_keeps_original() { let d = TempDir::new("rename"); @@ -134,6 +180,7 @@ fn rename_failure_keeps_original() { std::fs::write(&p, ORIGINAL).unwrap(); let fs = Faulty { fail_rename: Some(io::ErrorKind::Other), ..Default::default() }; assert!(atomic_write_with(&fs, NO_RETRY, &p, b"new").is_err()); + assert_eq!(fs.synced.get(), 0, "a failed replacement never reaches the publication barrier"); assert_eq!(std::fs::read(&p).unwrap(), ORIGINAL); assert_no_temp(&d.0); } @@ -176,6 +223,81 @@ fn lock_that_outlasts_the_retries_keeps_original() { assert_no_temp(&d.0); } +/// A file system that reports the target as read-only and records every write and rename. +#[derive(Default)] +struct ReadOnlyTarget { + writes: Cell, + renames: Cell, +} + +impl AtomicFs for ReadOnlyTarget { + fn is_readonly(&self, _path: &Path) -> bool { + true + } + fn write_new(&self, tmp: &Path, bytes: &[u8]) -> io::Result<()> { + self.writes.set(self.writes.get() + 1); + RealFs.write_new(tmp, bytes) + } + fn rename(&self, from: &Path, to: &Path) -> io::Result<()> { + self.renames.set(self.renames.get() + 1); + RealFs.rename(from, to) + } + fn remove(&self, path: &Path) -> io::Result<()> { + RealFs.remove(path) + } + fn sync_dir(&self, dir: &Path) -> io::Result<()> { + RealFs.sync_dir(dir) + } + fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> { + RealFs.copy_metadata(from, to) + } +} + +/// #6600: a read-only destination is refused before anything is written, with an error that says +/// so, instead of being replaced by the rename (which only needs the folder to be writable). +#[test] +fn read_only_target_is_refused_before_anything_is_written() { + let d = TempDir::new("rofake"); + let p = d.0.join("a.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + let fs = ReadOnlyTarget::default(); + let e = atomic_write_with(&fs, NO_RETRY, &p, b"new").unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::PermissionDenied); + assert!(e.to_string().contains("a.psd") && e.to_string().contains("read-only") && e.to_string().contains("not changed"), "{e}"); + assert_eq!((fs.writes.get(), fs.renames.get()), (0, 0), "nothing is written or renamed"); + assert_eq!(std::fs::read(&p).unwrap(), ORIGINAL); + assert_no_temp(&d.0); +} + +/// #6600 on the real file system: a file marked read-only (`chmod 444`, or the Windows read-only +/// attribute) keeps its bytes and the save says why; a writable one still saves. +#[test] +fn read_only_file_on_disk_is_never_replaced() { + let d = TempDir::new("rofile"); + let p = d.0.join("a.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + let mut perms = std::fs::metadata(&p).unwrap().permissions(); + perms.set_readonly(true); + std::fs::set_permissions(&p, perms.clone()).unwrap(); + let r = atomic_write(&p, b"new"); + let unchanged = std::fs::read(&p).unwrap(); + let still_readonly = std::fs::metadata(&p).unwrap().permissions().readonly(); + // Make it writable again so the folder can be removed (Windows refuses to delete it otherwise). + #[allow(clippy::permissions_set_readonly_false)] + perms.set_readonly(false); + std::fs::set_permissions(&p, perms).unwrap(); + let e = r.unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::PermissionDenied); + assert!(e.to_string().contains("the file is read-only;"), "a clear reason, not a failed rename: {e}"); + assert_eq!(unchanged, ORIGINAL); + assert!(still_readonly); + assert_no_temp(&d.0); + // New files and writable files are unaffected. + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + atomic_write(&d.0.join("fresh.psd"), b"x").unwrap(); +} + #[cfg(unix)] #[test] fn read_only_folder_errors_clearly_and_never_writes_in_place() { @@ -208,6 +330,19 @@ fn keeps_the_original_file_mode() { assert_eq!(std::fs::metadata(&p).unwrap().permissions().mode() & 0o777, 0o600); } +#[cfg(unix)] +#[test] +fn new_file_follows_the_process_umask() { + use std::os::unix::fs::PermissionsExt; + let d = TempDir::new("umask"); + let probe = d.0.join("probe.psd"); + std::fs::File::create(&probe).unwrap(); + let expected = std::fs::metadata(&probe).unwrap().permissions().mode() & 0o777; + let p = d.0.join("fresh.psd"); + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::metadata(&p).unwrap().permissions().mode() & 0o777, expected); +} + #[cfg(unix)] #[test] fn writes_through_a_symlink() { @@ -221,6 +356,52 @@ fn writes_through_a_symlink() { assert_eq!(std::fs::read(&real).unwrap(), b"new"); } +/// #6605: saving onto a dangling symlink writes through to its target instead of replacing the +/// link with a regular file, even when a folder on the way to the target doesn't exist yet. +#[cfg(unix)] +#[test] +fn writes_through_a_dangling_symlink() { + let d = TempDir::new("dangling"); + let real = d.0.join("missing/dir/a.psd"); + let link = d.0.join("link.psd"); + std::os::unix::fs::symlink(&real, &link).unwrap(); + atomic_write(&link, b"new").unwrap(); + assert!(std::fs::symlink_metadata(&link).unwrap().file_type().is_symlink(), "the link survives"); + assert_eq!(std::fs::read(&real).unwrap(), b"new", "the bytes land at the target"); + assert_no_temp(real.parent().unwrap_or(Path::new("."))); +} + +/// A dangling link with a relative target resolves against the link's own folder. +#[cfg(unix)] +#[test] +fn writes_through_a_dangling_relative_symlink() { + let d = TempDir::new("dangling-rel"); + let here = d.0.join("here"); + std::fs::create_dir(&here).unwrap(); + let link = here.join("link.psd"); + std::os::unix::fs::symlink("../elsewhere/real.psd", &link).unwrap(); + atomic_write(&link, b"new").unwrap(); + assert!(std::fs::symlink_metadata(&link).unwrap().file_type().is_symlink(), "the link survives"); + assert_eq!(std::fs::read(d.0.join("elsewhere/real.psd")).unwrap(), b"new"); + assert_no_temp(&d.0.join("elsewhere")); +} + +/// A symlink loop is an error; it never replaces either link. +#[cfg(unix)] +#[test] +fn symlink_loop_errors_without_replacing_the_links() { + let d = TempDir::new("loop"); + let a = d.0.join("a.psd"); + let b = d.0.join("b.psd"); + std::os::unix::fs::symlink(&b, &a).unwrap(); + std::os::unix::fs::symlink(&a, &b).unwrap(); + let e = atomic_write(&a, b"new").unwrap_err(); + assert!(e.to_string().contains("symbolic links"), "{e}"); + assert!(std::fs::symlink_metadata(&a).unwrap().file_type().is_symlink()); + assert!(std::fs::symlink_metadata(&b).unwrap().file_type().is_symlink()); + assert_no_temp(&d.0); +} + #[test] fn directory_target_or_bare_root_is_an_error_not_a_panic() { let d = TempDir::new("dir"); @@ -265,3 +446,80 @@ fn sync_file_flushes_a_local_file() { let file = std::fs::OpenOptions::new().write(true).create_new(true).open(&path).unwrap(); sync_file(&file).unwrap(); } + +/// #4968: the rename succeeded but the directory flush failed. The new bytes are in place, and +/// the save is reported as unconfirmed (same kind, saying so) instead of as a success, so the +/// caller keeps the document unsaved and its recovery data. +#[test] +fn a_failed_directory_flush_is_reported_not_swallowed() { + let d = TempDir::new("syncdir"); + let p = d.0.join("doc.pcraft"); + std::fs::write(&p, ORIGINAL).unwrap(); + let fs = Faulty { fail_sync_dir: Some(Err(io::ErrorKind::Other)), ..Default::default() }; + let e = atomic_write_with(&fs, NO_RETRY, &p, b"new").unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::Other); + let msg = e.to_string(); + assert!(msg.contains("doc.pcraft") && msg.contains("may not survive"), "{msg}"); + assert_eq!(std::fs::read(&p).unwrap(), b"new", "published: the new bytes are in place"); + assert_no_temp(&d.0); +} + +/// A file system that can't flush directories at all (Linux's EINVAL, ENOTSUP, or an +/// `Unsupported` error) still saves: failing every save there would make saving impossible. +/// Windows fails the save on any failed flush (#7858), so this is the Unix rule. +#[cfg(not(windows))] +#[test] +fn a_directory_flush_the_file_system_cannot_do_still_saves() { + let d = TempDir::new("syncdir-unsupported"); + let p = d.0.join("doc.pcraft"); + let fs = Faulty { fail_sync_dir: Some(Err(io::ErrorKind::Unsupported)), ..Default::default() }; + atomic_write_with(&fs, NO_RETRY, &p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + // EINVAL (22) means "can't flush a directory" only on Unix; EIO (5) is a real failure. + assert_eq!(dir_synced(Err(io::Error::from_raw_os_error(22))).is_ok(), cfg!(unix)); + assert!(dir_synced(Err(io::Error::from_raw_os_error(5))).is_err()); + let fs = Faulty { fail_sync_dir: Some(Ok(5)), ..Default::default() }; + assert!(atomic_write_with(&fs, NO_RETRY, &p, b"newer").is_err()); +} + +/// #9388: on macOS a save keeps the replaced file's creation time ("Created" in Finder), as +/// Photoshop's does; the rename would otherwise leave a file born at the time of the save. +#[cfg(target_os = "macos")] +#[test] +fn keeps_the_original_creation_time() { + use std::os::macos::fs::FileTimesExt; + let d = TempDir::new("created"); + let p = d.0.join("doc.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + // A creation time well in the past, so a reset to "now" can't pass for it. + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&p).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born); + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born, "the creation time survives the save"); + assert_no_temp(&d.0); +} + +/// #9401 review: a replaced file whose ACL denies its owner writing still keeps its creation +/// time. The ACL lands on the temporary file too, so the time has to be set before it is. +#[cfg(target_os = "macos")] +#[test] +fn keeps_the_creation_time_under_a_deny_write_acl() { + use std::os::macos::fs::FileTimesExt; + let d = TempDir::new("created-acl"); + let p = d.0.join("doc.psd"); + std::fs::write(&p, ORIGINAL).unwrap(); + let born = std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_600_000_000); + std::fs::OpenOptions::new().write(true).open(&p).unwrap().set_times(std::fs::FileTimes::new().set_created(born)).unwrap(); + let Ok(user) = std::env::var("USER") else { return }; + let deny = exacl::AclEntry::deny_user(&user, exacl::Perm::WRITE, None); + if exacl::setfacl(&[&p], &[deny], None).is_err() { + return; // a file system without ACLs: nothing to assert + } + atomic_write(&p, b"new").unwrap(); + assert_eq!(std::fs::read(&p).unwrap(), b"new"); + assert_eq!(std::fs::metadata(&p).unwrap().created().unwrap(), born, "the creation time survives a deny-write ACL"); + let _ = exacl::setfacl(&[&p], &[], None); + assert_no_temp(&d.0); +} diff --git a/crates/format/src/autosave.rs b/crates/format/src/autosave.rs index 06150e60ed..7b379451dd 100644 --- a/crates/format/src/autosave.rs +++ b/crates/format/src/autosave.rs @@ -9,6 +9,7 @@ //! [`RecoveryStore`] is the app-facing lifecycle: an entry is only deleted once //! the document is saved or closed, never just because it was recovered. +use std::collections::BTreeSet; #[cfg(not(target_arch = "wasm32"))] use std::collections::HashMap; use std::path::{Path, PathBuf}; @@ -20,7 +21,6 @@ use std::time::{SystemTime, UNIX_EPOCH}; use photocraft_doc::Document; use serde::{Deserialize, Serialize}; -use crate::store::write_atomic; use crate::{FormatError, PcraftWriter, Result, SaveOptions, SaveStats}; /// Sidecar describing an autosave. @@ -168,7 +168,7 @@ fn worker(rx: Receiver, bundle: PathBuf, sidecar: PathBuf, last: Arc, bundle: PathBuf, sidecar: PathBuf, last: Arc Vec { - let mut out = Vec::new(); - let Ok(rd) = std::fs::read_dir(recovery_dir) else { - return out; - }; - for e in rd.flatten() { - let p = e.path(); - if p.extension().is_some_and(|x| x == "json") - && let Ok(bytes) = std::fs::read(&p) - && let Ok(info) = serde_json::from_slice::(&bytes) - && sanitize(&info.key) == info.key - && p.file_stem().is_some_and(|stem| *stem == *info.key) - { - let bundle = recovery_dir.join(format!("{}.pcraft", info.key)); - if bundle.join(crate::store::MANIFEST).is_file() { - out.push(RecoveryEntry { info, bundle }); +/// Publish a recovery sidecar atomically: a flushed temporary file renamed over the old one. +/// +/// The replacing step is a plain rename on every platform, never `ReplaceFileW` (what +/// [`crate::atomic::RealFs`] uses on Windows to keep a user file's ACL and named streams). That +/// call first moves the old file to a backup name and only then moves the new one in, so for a +/// moment `.json` does not exist: a listing in between took the entry for a sidecar-less +/// bundle and listed it twice, or not at all (#9034). A rename swaps the name in one step, and +/// recovery data is private to the app, so there is no metadata to keep. +fn write_sidecar(path: &Path, bytes: &[u8]) -> Result<()> { + struct SidecarFs; + impl crate::atomic::AtomicFs for SidecarFs { + // As before: Windows refuses a read-only sidecar up front, Unix replaces it. + #[cfg(not(unix))] + fn is_readonly(&self, path: &Path) -> bool { + crate::atomic::RealFs.is_readonly(path) + } + fn write_new(&self, tmp: &Path, bytes: &[u8]) -> std::io::Result<()> { + #[cfg(unix)] + { + use std::io::Write; + use std::os::unix::fs::OpenOptionsExt; + let mut f = std::fs::OpenOptions::new().write(true).create_new(true).mode(0o600).open(tmp)?; + f.write_all(bytes)?; + crate::atomic::sync_file(&f) } - } else if e.file_type().is_ok_and(|ft| ft.is_dir()) - && let Some(entry) = sidecarless_entry(recovery_dir, &p) - { - out.push(entry); + #[cfg(not(unix))] + { + crate::atomic::RealFs.write_new(tmp, bytes) + } + } + fn rename(&self, from: &Path, to: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.rename(from, to) + } + // `replace_existing` keeps the trait's default: this same one-step rename. + fn remove(&self, path: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.remove(path) + } + fn sync_dir(&self, dir: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.sync_dir(dir) + } + fn copy_metadata(&self, from: &Path, to: &Path) -> std::io::Result<()> { + crate::atomic::RealFs.copy_metadata(from, to) } } + crate::atomic::atomic_write_with(&SidecarFs, crate::atomic::RenameRetry::platform(), path, bytes)?; + Ok(()) +} + +fn read_sidecar_bounded(path: &Path, max: usize) -> Option> { + use std::io::Read; + #[cfg(unix)] + let mut file = { + use rustix::fs::{Mode, OFlags, open}; + let fd = open(path, OFlags::RDONLY | OFlags::NOFOLLOW | OFlags::NONBLOCK | OFlags::CLOEXEC, Mode::empty()).ok()?; + let file = std::fs::File::from(fd); + if !file.metadata().ok()?.is_file() { + return None; + } + let _ = rustix::fs::fcntl_setfl(&file, OFlags::empty()); + file + }; + #[cfg(not(unix))] + let mut file = { + if !std::fs::symlink_metadata(path).ok()?.file_type().is_file() { + return None; + } + let file = std::fs::File::open(path).ok()?; + if !file.metadata().ok()?.is_file() { + return None; + } + file + }; + let mut bytes = Vec::new(); + (&mut file).take((max as u64).saturating_add(1)).read_to_end(&mut bytes).ok()?; + if bytes.len() > max { + return None; + } + Some(bytes) +} + +/// Recoverable documents in `recovery_dir`, newest first. A sidecar must be named after its own +/// (sanitized) key, so a stray or hand-edited one can never point outside the directory. +/// +/// Each key is looked at once, after the directory has been read: an autosave may publish a +/// document's first sidecar while the listing runs, and judging the bundle and the sidecar as +/// each name came up listed that bundle twice, or not at all. +pub fn list_recovery(recovery_dir: &Path) -> Vec { + let Ok(rd) = std::fs::read_dir(recovery_dir) else { + return Vec::new(); + }; + let keys: BTreeSet = rd + .flatten() + .filter_map(|e| { + let name = e.file_name().into_string().ok()?; + name.strip_suffix(".json").or_else(|| name.strip_suffix(".pcraft")).map(str::to_string) + }) + .collect(); + let mut out: Vec = keys.iter().filter_map(|key| recovery_entry(recovery_dir, key)).collect(); out.sort_by(|a, b| b.info.saved_at.cmp(&a.info.saved_at).then(a.info.key.cmp(&b.info.key))); out } +/// `key`'s entry. The sidecar is tried again last: it can appear after the first look, once the +/// bundle it describes is committed, and is never removed by a save. +fn recovery_entry(recovery_dir: &Path, key: &str) -> Option { + let bundle = recovery_dir.join(format!("{key}.pcraft")); + sidecar_entry(recovery_dir, key, &bundle).or_else(|| sidecarless_entry(recovery_dir, &bundle)).or_else(|| sidecar_entry(recovery_dir, key, &bundle)) +} + +fn sidecar_entry(recovery_dir: &Path, key: &str, bundle: &Path) -> Option { + let bytes = read_sidecar_bounded(&recovery_dir.join(format!("{key}.json")), 1 << 20)?; + let info = serde_json::from_slice::(&bytes).ok()?; + (sanitize(&info.key) == info.key && info.key == key && bundle.join(crate::store::MANIFEST).is_file()) + .then(|| RecoveryEntry { info, bundle: bundle.to_path_buf() }) +} + /// A committed `.pcraft` bundle (its manifest is written last) whose sidecar was never /// published, e.g. a crash during a document's first autosave. The metadata is rebuilt from the /// manifest (read like a load reads it) and its modification time. Incomplete bundles (no /// readable manifest) and bundles with anything at the sidecar's path are not entries. fn sidecarless_entry(recovery_dir: &Path, bundle: &Path) -> Option { let key = bundle.file_name()?.to_str()?.strip_suffix(".pcraft")?; - if key.is_empty() || sanitize(key) != key || std::fs::symlink_metadata(recovery_dir.join(format!("{key}.json"))).is_ok() { + if !std::fs::symlink_metadata(bundle).is_ok_and(|m| m.is_dir()) + || key.is_empty() + || sanitize(key) != key + || std::fs::symlink_metadata(recovery_dir.join(format!("{key}.json"))).is_ok() + { return None; } let meta = std::fs::symlink_metadata(bundle.join(crate::store::MANIFEST)).ok().filter(|m| m.is_file())?; @@ -297,6 +388,24 @@ impl RecoveryStore { self.savers.iter().flat_map(|(&id, saver)| saver.take_completed().into_iter().map(move |(revision, result)| (id, revision, result))).collect() } + /// Finish every queued autosave and wait for it, then return the outcomes as + /// [`take_completed`](Self::take_completed) does. For a session that ends without asking, + /// e.g. because the window's graphics device was lost (#3438). The workers stop; a later + /// autosave of the same document starts a new one on the same entry, so a failed write can be + /// tried again. + pub fn flush(&mut self) -> Vec<(u64, u64, std::result::Result<(), String>)> { + for saver in self.savers.values_mut() { + saver.shutdown(); + } + let done = self.take_completed(); + // Keep each entry's key, so a later autosave writes the same entry and a discard (the + // document was saved or closed meanwhile) still removes it. + for (id, saver) in self.savers.drain() { + self.adopted.entry(id).or_insert(saver.key.clone()); + } + done + } + /// Document `doc_id` was saved or closed: remove its recovery data, both its own autosaves /// and the entry it adopted. pub fn discard(&mut self, doc_id: u64) -> Result<()> { @@ -384,6 +493,9 @@ fn untouched_for(path: &Path, min_age: std::time::Duration, missing_ok: bool) -> } fn remove_entry(dir: &Path, key: &str) -> Result<()> { + if sanitize(key) != key { + return Err(FormatError::corrupt("recovery entry key contains path components")); + } let bundle = dir.join(format!("{key}.pcraft")); if bundle.exists() { std::fs::remove_dir_all(bundle)?; diff --git a/crates/format/src/convert.rs b/crates/format/src/convert.rs index 95d45fafb9..a093bd10e0 100644 --- a/crates/format/src/convert.rs +++ b/crates/format/src/convert.rs @@ -11,7 +11,10 @@ use photocraft_doc::{ use photocraft_geom::{TILE_SIZE, TileCoord}; use photocraft_raster::{Surface, Tile, decode_pixel, encode_pixel}; -use crate::manifest::*; +use crate::manifest::{ + ChannelM, CompAppearanceM, CompStateM, ContentM, DocM, EffectsM, FillCacheM, Hash, LayerCompM, LayerM, MaskM, MetadataM, NamedPathM, PatternM, + SmartSourceM, SurfaceM, TileRef, +}; use crate::{FormatError, Result}; /// Deepest group nesting a bundle may hold — the one nesting limit every consumer of the layer @@ -125,9 +128,12 @@ fn video_m(v: &photocraft_doc::VideoData, sink: &mut dyn Sink) -> crate::manifes fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM { let content = match &l.content { LayerContent::Raster(s) => ContentM::Raster { surface: surface_m(s, sink) }, - LayerContent::Group(g) => { - ContentM::Group { children: g.children.iter().map(|c| layer_m(c, sink)).collect(), expanded: g.expanded, artboard: g.artboard.clone() } - } + LayerContent::Group(g) => ContentM::Group { + children: g.children.iter().map(|c| layer_m(c, sink)).collect(), + expanded: g.expanded, + artboard: g.artboard.clone(), + frame_stroke: g.frame_stroke.clone(), + }, LayerContent::Adjustment(a) => ContentM::Adjustment { adjustment: a.clone() }, LayerContent::Fill(f) => ContentM::Fill { fill: f.clone() }, LayerContent::Text(t) => ContentM::Text { @@ -152,6 +158,8 @@ fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM { path: s.path.clone(), stroke: s.stroke.clone(), live: s.live.clone(), + density: s.density, + feather: s.feather, }, LayerContent::Smart(s) => ContentM::Smart { contents_id: Some(s.contents_id.0), @@ -209,6 +217,7 @@ fn layer_m(l: &Layer, sink: &mut dyn Sink) -> LayerM { blend_if: l.blend_if.clone(), advanced: l.advanced, video: l.video.as_ref().map(|v| video_m(v, sink)), + xmp: l.xmp.as_deref().map(str::to_string), } } @@ -233,7 +242,7 @@ pub(crate) fn doc_m(d: &Document, sink: &mut dyn Sink) -> DocM { ruler_origin: d.ruler_origin, selection: d.selection.as_ref().map(|s| surface_m(s, sink)), metadata: MetadataM { - xmp: d.metadata.xmp.clone(), + xmp: d.metadata.xmp.as_deref().map(str::to_owned), exif: opt_blob(&d.metadata.exif, sink), text: d.metadata.text.clone(), psd_resources: d.metadata.psd_resources.iter().map(|(id, n, b)| (*id, n.clone(), sink.blob(b))).collect(), @@ -446,7 +455,9 @@ impl Loader<'_> { fn layer_with(&mut self, m: &LayerM, children: Vec) -> Result { let content = match &m.content { ContentM::Raster { surface } => LayerContent::Raster(self.surface(surface)?), - ContentM::Group { expanded, artboard, .. } => LayerContent::Group(Group { artboard: artboard.clone(), children, expanded: *expanded }), + ContentM::Group { expanded, artboard, frame_stroke, .. } => { + LayerContent::Group(Group { artboard: artboard.clone(), children, expanded: *expanded, frame_stroke: frame_stroke.clone() }) + } ContentM::Adjustment { adjustment } => LayerContent::Adjustment(adjustment.clone()), ContentM::Fill { fill } => LayerContent::Fill(fill.clone()), ContentM::Text { text, font_family, size_pt, color, transform, cache, psd_raw, runs, paragraphs, shape, orientation, antialias, warp } => { @@ -466,13 +477,15 @@ impl Loader<'_> { warp: warp.clone(), }) } - ContentM::Shape { fill, cache, psd_raw, path, stroke, live } => LayerContent::Shape(ShapeLayer { + ContentM::Shape { fill, cache, psd_raw, path, stroke, live, density, feather } => LayerContent::Shape(ShapeLayer { path: path.clone(), fill: fill.clone(), stroke: stroke.clone(), live: live.clone(), cache: self.opt_surface(cache)?, psd_raw: self.opt_blob(psd_raw)?, + density: *density, + feather: *feather, }), ContentM::Smart { contents_id, source, transform, smart_filters, cache, psd_raw, filters_enabled, filter_mask, warp, stack_mode, perspective } => { LayerContent::Smart(SmartObject { @@ -543,6 +556,7 @@ impl Loader<'_> { blend_if: m.blend_if.clone(), advanced: m.advanced, video: m.video.as_ref().map(|v| self.video(v)).transpose()?, + xmp: m.xmp.as_deref().map(Arc::from), }) } @@ -575,7 +589,7 @@ impl Loader<'_> { } let quick_mask = m.quick_mask.as_ref().map(|c| self.channel(c)).transpose()?; let mut md = Metadata { - xmp: m.metadata.xmp.clone(), + xmp: m.metadata.xmp.as_deref().map(std::sync::Arc::from), exif: self.opt_blob(&m.metadata.exif)?, text: m.metadata.text.clone(), psd_resources: Vec::new(), diff --git a/crates/format/src/lib.rs b/crates/format/src/lib.rs index 0c87720212..992e925ae9 100644 --- a/crates/format/src/lib.rs +++ b/crates/format/src/lib.rs @@ -8,6 +8,7 @@ //! blobs/.zst content-addressed binary data (ICC, EXIF, PSD blocks, smart objects) //! thumb.png optional thumbnail (caller-rendered) //! composite/preview.png optional flattened preview (caller-rendered) +//! save.lock directory bundles only: held while a save runs (empty) //! ``` //! //! A bundle is either a ZIP archive (STORE entries) or a directory with the @@ -15,7 +16,9 @@ //! already compressed/wrote, so only new tiles are encoded. Directory saves //! verify existing content-addressed objects once per writer and folder, //! rechecking files whose size or modification time changes; they replace -//! damaged or missing objects and garbage-collect unreferenced ones. +//! damaged or missing objects and garbage-collect unreferenced ones. A directory +//! save holds an OS file lock on `save.lock` throughout, so two writers (or two +//! processes) saving one bundle take turns instead of removing each other's objects. //! //! This crate sits at L3 next to the compositor, so it does not render. //! Callers pass previews in [`SaveOptions`]; `photocraft-io` does that. diff --git a/crates/format/src/manifest.rs b/crates/format/src/manifest.rs index 82480e05c0..0b1e08b706 100644 --- a/crates/format/src/manifest.rs +++ b/crates/format/src/manifest.rs @@ -242,6 +242,9 @@ pub struct LayerM { pub advanced: photocraft_doc::AdvancedBlending, #[serde(default)] pub video: Option, + /// The layer's own XMP packet (missing in older files = none). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub xmp: Option, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] @@ -256,6 +259,9 @@ pub enum ContentM { /// Artboard (Layer › New › Artboard). #[serde(default)] artboard: Option, + /// A Photoshop Frame's stroke, drawn along the group's vector mask. + #[serde(default, skip_serializing_if = "Option::is_none")] + frame_stroke: Option, }, Adjustment { adjustment: Adjustment, @@ -294,6 +300,12 @@ pub enum ContentM { stroke: Option, #[serde(default)] live: Option, + /// The path's density as the fill's mask; older files have none (100 %). + #[serde(default = "full_density")] + density: f32, + /// The path's feather in px; older files have none. + #[serde(default)] + feather: f32, }, Smart { /// Shared source identity; older bundles did not record it. @@ -318,6 +330,10 @@ pub enum ContentM { }, } +fn full_density() -> f32 { + 1.0 +} + fn yes() -> bool { true } @@ -385,3 +401,16 @@ mod channel_tests { assert_eq!(c.indicates, photocraft_doc::ColorIndicates::MaskedAreas); } } + +#[cfg(test)] +mod shape_tests { + use super::*; + + /// Shapes saved before Density and Feather load at 100 % and no feather. + #[test] + fn older_shapes_load_at_full_density_without_feather() { + let old = serde_json::json!({ "kind": "shape", "fill": null, "cache": null, "psd_raw": null }); + let ContentM::Shape { density, feather, .. } = serde_json::from_value(old).unwrap() else { panic!("a shape") }; + assert_eq!((density, feather), (1.0, 0.0)); + } +} diff --git a/crates/format/src/read.rs b/crates/format/src/read.rs index 4ebfcbbfa6..b71e3c8030 100644 --- a/crates/format/src/read.rs +++ b/crates/format/src/read.rs @@ -6,7 +6,10 @@ //! to 4 GiB in one `ReadFile`, and some file systems (exFAT, network redirectors) reject a read //! that large with `ERROR_INVALID_PARAMETER` ("The parameter is incorrect", #375). //! - **A clear out-of-memory error.** The buffer is reserved up front with `try_reserve_exact`, -//! and a file larger than the memory available says how much it needed. +//! and a file larger than the memory available says how much it needed. A file that grows past +//! its stat length while being read grows the buffer with `try_reserve` too. +//! - **A total limit.** No read returns more than [`MAX_OPEN_BYTES`], so a file that keeps growing +//! while it is read cannot keep an open or a save from finishing. //! //! On `wasm32` there is no file system and every call returns the `std` "unsupported" error. @@ -16,40 +19,113 @@ use std::path::Path; /// Largest single read: big enough that a 5 GB file takes under a hundred reads. pub const READ_CHUNK: usize = 64 << 20; -/// Read all of `path`, in reads of at most [`READ_CHUNK`] bytes. +/// Most bytes [`read_file`] and [`read_all`] return. Far above any document memory can hold (a +/// PSB can run to tens of gigabytes), it only bounds a file that never stops growing; a file over it +/// is a `FileTooLarge` error, never a truncated read. +pub const MAX_OPEN_BYTES: u64 = 1 << 38; + +/// Read all of `path`, in reads of at most [`READ_CHUNK`] bytes and at most [`MAX_OPEN_BYTES`] in +/// all. Anything but a regular file is an `InvalidInput` error. pub fn read_file(path: &Path) -> io::Result> { read_file_with(path, READ_CHUNK) } /// [`read_file`] with a chosen read size (tests use small ones to exercise the loop). pub fn read_file_with(path: &Path, chunk: usize) -> io::Result> { - let mut f = std::fs::File::open(path)?; - let len = f.metadata().map(|m| m.len()).unwrap_or(0); - read_all_with(&mut f, len, chunk) + read_path(path, chunk, MAX_OPEN_BYTES) +} + +/// [`read_file_with`] with a chosen total limit (tests use small ones). +fn read_path(path: &Path, chunk: usize, max: u64) -> io::Result> { + #[cfg(unix)] + let (mut f, metadata) = { + use rustix::fs::{Mode, OFlags, open}; + let fd = open(path, OFlags::RDONLY | OFlags::NONBLOCK | OFlags::CLOEXEC, Mode::empty()).map_err(io::Error::from)?; + let f = std::fs::File::from(fd); + let metadata = f.metadata()?; + if !metadata.is_file() { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "path is not a regular file")); + } + rustix::fs::fcntl_setfl(&f, OFlags::empty()).map_err(io::Error::from)?; + (f, metadata) + }; + #[cfg(not(unix))] + let (mut f, metadata) = { + let f = std::fs::File::open(path)?; + let metadata = f.metadata()?; + if !metadata.is_file() { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "path is not a regular file")); + } + (f, metadata) + }; + let len = metadata.len(); + read_all_with(&mut f, len, chunk, max) } /// Read `reader` to its end in reads of at most [`READ_CHUNK`] bytes, for an already open file /// (a capability-scoped handle, say). `size_hint` (the file's length) sizes the buffer up front. +/// More than [`MAX_OPEN_BYTES`] in all is a `FileTooLarge` error. pub fn read_all(reader: &mut impl Read, size_hint: u64) -> io::Result> { - read_all_with(reader, size_hint, READ_CHUNK) + read_all_with(reader, size_hint, READ_CHUNK, MAX_OPEN_BYTES) } -fn read_all_with(reader: &mut impl Read, size_hint: u64, chunk: usize) -> io::Result> { - // The hint only sizes the buffer: a file that grows or shrinks while being read is still read - // to its end. +/// Read `reader` to its end in reads of at most `chunk` bytes, failing once it has given more +/// than `max` bytes. The hint only sizes the buffer: a file that grows or shrinks while being +/// read is still read to its end, within `max`. Past the hint the buffer grows only after a probe +/// read has found more bytes, and fallibly, so a file read exactly to its stat length is never +/// over-allocated and one that outgrows memory is an `OutOfMemory` error, not an abort. +fn read_all_with(reader: &mut impl Read, size_hint: u64, chunk: usize, max: u64) -> io::Result> { + if size_hint > max { + return Err(too_large(max)); + } let mut buf = buffer_for(size_hint)?; - let chunk = u64::try_from(chunk.max(1)).unwrap_or(u64::MAX); - while reader.take(chunk).read_to_end(&mut buf)? > 0 {} + let chunk = chunk.max(1); + // One byte over `max` is enough to tell a file over the limit from one exactly at it. + let mut reader = reader.take(max.saturating_add(1)); + loop { + if buf.len() == buf.capacity() { + let mut probe = [0; 4096]; + let n = loop { + match reader.read(&mut probe) { + Err(e) if e.kind() == io::ErrorKind::Interrupted => {} + r => break r?, + } + }; + if n == 0 { + break; + } + let needed = buf.len() as u64 + n as u64; + buf.try_reserve(n.max(chunk)).map_err(|_| out_of_memory(needed))?; + buf.extend_from_slice(&probe[..n]); + } + let room = u64::try_from((buf.capacity() - buf.len()).min(chunk)).unwrap_or(u64::MAX); + if (&mut reader).take(room).read_to_end(&mut buf)? == 0 && buf.len() < buf.capacity() { + break; + } + } + if buf.len() as u64 > max { + return Err(too_large(max)); + } Ok(buf) } +/// The error for a file over the `max` bytes a read may return. +fn too_large(max: u64) -> io::Error { + io::Error::new(io::ErrorKind::FileTooLarge, format!("the file is larger than {} and was not read", size_text(max))) +} + +/// The `OutOfMemory` error for a read that needed `len` bytes. +fn out_of_memory(len: u64) -> io::Error { + io::Error::new(io::ErrorKind::OutOfMemory, format!("not enough memory to read the whole file ({})", size_text(len))) +} + /// An empty buffer with room for `len` bytes, or an `OutOfMemory` error saying how much was needed. pub fn buffer_for(len: u64) -> io::Result> { let mut buf = Vec::new(); let reserved = usize::try_from(len).ok().and_then(|n| buf.try_reserve_exact(n).ok()); match reserved { Some(()) => Ok(buf), - None => Err(io::Error::new(io::ErrorKind::OutOfMemory, format!("not enough memory to read the whole file ({})", size_text(len)))), + None => Err(out_of_memory(len)), } } @@ -95,6 +171,36 @@ mod tests { } } + /// Nothing over the total limit is returned (#8601): a file larger than it, a reader that + /// gives more than it whatever its hint says, and one that never ends are `FileTooLarge` + /// errors, while a file exactly at the limit is read whole. + #[test] + fn reads_stop_at_the_total_limit() { + let data: Vec = (0..10_000u32).map(|i| (i % 251) as u8).collect(); + let p = temp("limit", &data); + assert_eq!(read_path(&p, 7, data.len() as u64).unwrap(), data, "exactly at the limit"); + assert_eq!(read_path(&p, 7, data.len() as u64 - 1).unwrap_err().kind(), io::ErrorKind::FileTooLarge); + for hint in [0, 100, data.len() as u64] { + let e = read_all_with(&mut std::io::Cursor::new(&data), hint, 64, 5_000).unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::FileTooLarge, "hint {hint}"); + } + let e = read_all_with(&mut std::io::Cursor::new(&data), 1 << 40, 64, 5_000).unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::FileTooLarge, "a hint over the limit fails before reading"); + let e = read_all_with(&mut std::io::repeat(1), 0, 1 << 20, 3 << 20).unwrap_err(); + assert_eq!(e.kind(), io::ErrorKind::FileTooLarge, "a reader that never ends"); + assert!(e.to_string().contains("3.15 MB"), "{e}"); + } + + /// A file read exactly to its stat length keeps the buffer it reserved: the end is found by a + /// probe read, without growing the buffer first. + #[test] + fn an_exact_hint_is_not_over_allocated() { + let data = vec![7u8; 100_000]; + let buf = read_all_with(&mut std::io::Cursor::new(&data), data.len() as u64, 4096, MAX_OPEN_BYTES).unwrap(); + assert_eq!(buf, data); + assert_eq!(buf.capacity(), data.len()); + } + #[test] fn empty_and_missing_files() { assert!(read_file(&temp("empty", b"")).unwrap().is_empty()); diff --git a/crates/format/src/store.rs b/crates/format/src/store.rs index d047668cb0..97e285a573 100644 --- a/crates/format/src/store.rs +++ b/crates/format/src/store.rs @@ -18,6 +18,10 @@ use crate::{FormatError, LoadOptions, Result, SaveOptions}; pub(crate) const MANIFEST: &str = "manifest.json"; pub(crate) const THUMB: &str = "thumb.png"; pub(crate) const COMPOSITE: &str = "composite/preview.png"; +/// The file a directory save holds an exclusive lock on for its whole transaction (#4957). +pub(crate) const SAVE_LOCK: &str = "save.lock"; +/// How long a directory save waits for another save of the same bundle to finish. +const SAVE_LOCK_WAIT: std::time::Duration = std::time::Duration::from_secs(10); fn tile_path(h: &str) -> String { format!("tiles/{h}.zst") @@ -60,6 +64,34 @@ fn png(img: &Rgba8Image) -> Result> { photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).map_err(|e| FormatError::Unsupported(format!("preview: {e}"))) } +fn previews(opts: &SaveOptions) -> Result)>> { + let (composite, thumbnail) = beside( + opts.thumbnail.is_some() && opts.composite.is_some(), + || opts.composite.as_ref().map(png).transpose(), + || opts.thumbnail.as_ref().map(png).transpose(), + ); + let mut out = Vec::new(); + if let Some(t) = thumbnail? { + out.push((THUMB, t)); + } + if let Some(t) = composite? { + out.push((COMPOSITE, t)); + } + Ok(out) +} + +fn beside(parallel: bool, a: impl FnOnce() -> Result + Send, b: impl FnOnce() -> B) -> (Result, B) { + if !parallel || cfg!(target_arch = "wasm32") { + let a = a(); + return (a, b()); + } + std::thread::scope(|sc| { + let h = sc.spawn(a); + let b = b(); + (h.join().unwrap_or_else(|_| Err(FormatError::Io(std::io::Error::other("preview worker panicked")))), b) + }) +} + // --------------------------------------------------------------------------- // Sources // --------------------------------------------------------------------------- @@ -122,10 +154,17 @@ pub fn read_entry(file: &mut impl std::io::Read, len: u64, max: usize, path: &st /// The saver checks the same bound, so every bundle it writes loads again. const MAX_MANIFEST_DEPTH: usize = 3 * (convert::MAX_GROUP_DEPTH + 1) + 64; -/// Rejects JSON nested deeper than [`MAX_MANIFEST_DEPTH`] with a linear scan (no recursion), so -/// parsing with serde_json's recursion limit disabled can't exhaust the stack on hostile input. +/// Most JSON items (arrays and objects opened, plus elements and members after a comma) a +/// manifest may hold. Parsing builds a `serde_json::Value` tree of tens of bytes per item, so +/// `[0,0,…]` costs about 16× its size in memory; a typical manifest spends 20 bytes or more per +/// item, so this still admits manifests of about 80 MiB. +const MAX_MANIFEST_ITEMS: usize = 4 << 20; + +/// Rejects JSON nested deeper than [`MAX_MANIFEST_DEPTH`] or holding more than +/// [`MAX_MANIFEST_ITEMS`] items with a linear scan (no recursion), so parsing with serde_json's +/// recursion limit disabled can't exhaust the stack, nor tiny values the memory, on hostile input. fn check_manifest_depth(json: &[u8]) -> Result<()> { - let (mut depth, mut in_str, mut escaped) = (0usize, false, false); + let (mut depth, mut items, mut in_str, mut escaped) = (0usize, 0usize, false, false); for &b in json { if in_str { match b { @@ -147,6 +186,12 @@ fn check_manifest_depth(json: &[u8]) -> Result<()> { b']' | b'}' => depth = depth.saturating_sub(1), _ => {} } + if matches!(b, b'[' | b'{' | b',') { + items += 1; + if items > MAX_MANIFEST_ITEMS { + return Err(FormatError::LimitExceeded(format!("manifest JSON holds more than {MAX_MANIFEST_ITEMS} values"))); + } + } } Ok(()) } @@ -163,6 +208,10 @@ pub(crate) fn read_manifest(src: &dyn ByteSource, opts: &LoadOptions) -> Result< let layers = take_array(&mut v, "/document/layers"); let mut m: Manifest = serde_json::from_value(v)?; m.document.layers = layers_from_json(layers)?; + // A number past f32's range decodes as infinity, which no save could write back (#6862). + if let Err(crate::finite::Problem::NonFinite { path }) = crate::finite::check(&m) { + return Err(FormatError::corrupt(format!("`{path}` is out of range"))); + } Ok(m) } @@ -300,19 +349,24 @@ pub struct SaveStats { pub manifest_bytes: usize, } +type BlobHashCache = HashMap>, Hash)>; +type TileHashCache = HashMap<(usize, SampleType), (Weak, Hash)>; + #[derive(Default)] struct Collect<'c> { tiles: BTreeMap, SampleType)>, blobs: BTreeMap>>, - hash_cache: Option<&'c mut HashMap, Hash)>>, + hash_cache: Option<&'c mut TileHashCache>, + blob_hash_cache: Option<&'c mut BlobHashCache>, } impl Sink for Collect<'_> { + /// Reuses hashes by allocation and sample byte order without upgrading weak references. fn tile(&mut self, format: PixelFormat, tile: &Arc) -> Hash { - let key = Arc::as_ptr(tile) as usize; + let key = (Arc::as_ptr(tile) as usize, format.sample); if let Some(cache) = self.hash_cache.as_deref_mut() && let Some((w, h)) = cache.get(&key) - && w.upgrade().is_some_and(|t| Arc::ptr_eq(&t, tile)) + && std::ptr::eq(w.as_ptr(), Arc::as_ptr(tile)) { let h = h.clone(); self.tiles.entry(h.clone()).or_insert_with(|| (tile.clone(), format.sample)); @@ -326,8 +380,21 @@ impl Sink for Collect<'_> { h } + /// Reuses hashes for the same allocation without upgrading weak references. fn blob(&mut self, data: &Arc>) -> Hash { + let key = Arc::as_ptr(data) as usize; + if let Some(cache) = self.blob_hash_cache.as_deref_mut() + && let Some((w, h)) = cache.get(&key) + && std::ptr::eq(w.as_ptr(), Arc::as_ptr(data)) + { + let h = h.clone(); + self.blobs.entry(h.clone()).or_insert_with(|| data.clone()); + return h; + } let h = hash_bytes(data); + if let Some(cache) = self.blob_hash_cache.as_deref_mut() { + cache.insert(key, (Arc::downgrade(data), h.clone())); + } self.blobs.entry(h.clone()).or_insert_with(|| data.clone()); h } @@ -361,16 +428,56 @@ pub const DEFAULT_REVERIFY_BUDGET: u64 = 16 * 1024 * 1024; /// [`Self::set_reverify_budget`]). Over enough saves every object is read again, and a damaged /// one is rewritten. pub struct PcraftWriter { - hash_cache: HashMap, Hash)>, + hash_cache: TileHashCache, + blob_hash_cache: BlobHashCache, /// Compressed objects by bundle path (ZIP mode). compressed: HashMap>>, + compressed_budget: usize, verified_directory: Option, reverify_budget: u64, + /// How long [`Self::save_dir`] waits for another save of the bundle ([`SAVE_LOCK_WAIT`]). + lock_wait: std::time::Duration, } impl Default for PcraftWriter { fn default() -> Self { - Self { hash_cache: HashMap::new(), compressed: HashMap::new(), verified_directory: None, reverify_budget: DEFAULT_REVERIFY_BUDGET } + Self { + hash_cache: HashMap::new(), + blob_hash_cache: HashMap::new(), + compressed: HashMap::new(), + compressed_budget: usize::MAX, + verified_directory: None, + reverify_budget: DEFAULT_REVERIFY_BUDGET, + lock_wait: SAVE_LOCK_WAIT, + } + } +} + +/// Hold `dir`'s [`SAVE_LOCK`] for a directory save, so two writers (two stores, or two +/// processes) can't interleave: each lists the existing objects, writes its own, publishes its +/// manifest and removes what that manifest doesn't reference. Interleaved, one writer's cleanup +/// could remove an object the other's winning manifest needs (#4957). The lock is the OS's +/// advisory file lock, released when the returned file is dropped or the process ends, so a +/// crash leaves no stale lock. Another save holding it is waited for up to `wait`; after that the +/// save fails without writing anything. A file system without file locks (some network shares) +/// saves unlocked, as before. +fn lock_bundle(dir: &Path, wait: std::time::Duration) -> Result> { + let path = dir.join(SAVE_LOCK); + validate_bundle_file(dir, &path)?; + let file = std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).open(&path)?; + let start = std::time::Instant::now(); + loop { + match file.try_lock() { + Ok(()) => return Ok(Some(file)), + Err(std::fs::TryLockError::WouldBlock) if start.elapsed() < wait => std::thread::sleep(std::time::Duration::from_millis(20)), + Err(std::fs::TryLockError::WouldBlock) => { + return Err(FormatError::Io(std::io::Error::new( + std::io::ErrorKind::WouldBlock, + format!("another save of {} is in progress; nothing was written", dir.display()), + ))); + } + Err(std::fs::TryLockError::Error(_)) => return Ok(None), + } } } @@ -402,7 +509,6 @@ struct FileSignature { struct Prepared { manifest: Vec, objects: BTreeMap, - previews: Vec<(&'static str, Vec)>, stats: SaveStats, } @@ -521,14 +627,69 @@ fn validate_bundle_file(root: &Path, path: &Path) -> Result<()> { plain_file_or_missing(path) } -fn write_bundle_file(root: &Path, path: &Path, data: &[u8]) -> Result<()> { +/// Flush `dir`, the folder `written` was just renamed into, as `atomic_write` does (with the +/// full-flush fallback of `sync_file`, #1336). Windows flushes through the injected `AtomicFs` +/// and fails on any error (#7858). +#[cfg(not(windows))] +fn flush_bundle_dir(dir: &Path, written: &Path) -> std::io::Result<()> { + use crate::atomic::AtomicFs; + #[cfg(test)] + { + FLUSHED.with(|f| f.borrow_mut().push(dir.to_path_buf())); + let named = |p: Option<&Path>, name: &str| p.and_then(Path::file_name).is_some_and(|n| n == name); + if FAIL_DIR_FLUSH.with(|f| { + let hit = f.get().is_some_and(|name| named(Some(written), name) || named(written.parent(), name)); + if hit { + f.set(None); + } + hit + }) { + return Err(std::io::Error::other("injected folder flush failure")); + } + } + let _ = written; + crate::atomic::RealFs.sync_dir(dir) +} + +/// [`flush_bundle_dir`], as the save's error when the flush was attempted and failed: `written` +/// is in place, but may not survive a power loss or crash (#4968). +#[cfg(not(windows))] +fn confirm_flushed(dir: &Path, written: &Path) -> Result<()> { + crate::atomic::dir_synced(flush_bundle_dir(dir, written)).map_err(|e| { + FormatError::Io(std::io::Error::new( + e.kind(), + format!("{} was written, but its folder could not be flushed to disk, so the save may not survive a power loss or crash: {e}", written.display()), + )) + }) +} + +#[cfg(all(test, not(windows)))] +thread_local! { + /// Tests: the bundle file, or folder, whose next folder flush fails, once, on this thread. + static FAIL_DIR_FLUSH: std::cell::Cell> = const { std::cell::Cell::new(None) }; + /// Tests: the folders flushed on this thread, in order. + static FLUSHED: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; +} + +/// Publish a validated bundle entry through a flushed temporary file and replacing rename. +/// On Windows, propagate a failed directory barrier even though the new entry is already visible. +fn write_bundle_file(root: &Path, path: &Path, data: &[u8], fs: &dyn crate::atomic::AtomicFs) -> Result<()> { + #[cfg(not(windows))] + let _ = fs; validate_bundle_file(root, path)?; let parent = path.parent().ok_or_else(|| unsafe_bundle_entry(path))?; let name = path.file_name().ok_or_else(|| unsafe_bundle_entry(path))?; let mut temporary = None; for _ in 0..8 { let candidate = parent.join(crate::atomic::temp_name(&name.to_string_lossy())); - match std::fs::OpenOptions::new().write(true).create_new(true).open(&candidate) { + let mut opts = std::fs::OpenOptions::new(); + opts.write(true).create_new(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt; + opts.mode(0o600); + } + match opts.open(&candidate) { Ok(file) => { temporary = Some((candidate, file)); break; @@ -558,10 +719,14 @@ fn write_bundle_file(root: &Path, path: &Path, data: &[u8]) -> Result<()> { // itself, never its referent, even if one appears after this check. validate_bundle_file(root, path)?; crate::atomic::retry_rename(crate::atomic::RenameRetry::platform(), || std::fs::rename(&tmp, path))?; - #[cfg(unix)] + #[cfg(windows)] { - let _ = std::fs::File::open(parent).and_then(|d| d.sync_all()); + fs.sync_dir(parent).map_err(|e| crate::atomic::publication_error(e, path))?; } + // Elsewhere a failed flush is reported too (#4968), but a file system that can't flush a + // directory at all still saves, as in `atomic_write`. + #[cfg(not(windows))] + confirm_flushed(parent, path)?; Ok(()) })(); if result.is_err() && validate_bundle_file(root, &tmp).is_ok() { @@ -725,18 +890,25 @@ impl PcraftWriter { self.reverify_budget = bytes; } + /// Cap compressed payload capacity retained between ZIP saves. Clears the previous payload + /// cache; tile hashes remain reusable. The default is unbounded; `0` retains no payloads. + pub fn set_compressed_cache_budget(&mut self, bytes: usize) { + self.compressed_budget = bytes; + self.compressed.clear(); + } + + /// How long a directory save waits for another save of the same bundle to finish before + /// failing without writing anything (10 s by default; see [`Self::save_dir`]). + pub fn set_lock_wait(&mut self, wait: std::time::Duration) { + self.lock_wait = wait; + } + fn prepare(&mut self, doc: &Document, opts: &SaveOptions) -> Result { self.hash_cache.retain(|_, (w, _)| w.strong_count() > 0); + self.blob_hash_cache.retain(|_, (w, _)| w.strong_count() > 0); convert::check_nesting(&doc.layers)?; - let mut c = Collect { hash_cache: Some(&mut self.hash_cache), ..Default::default() }; + let mut c = Collect { hash_cache: Some(&mut self.hash_cache), blob_hash_cache: Some(&mut self.blob_hash_cache), ..Default::default() }; let document = convert::doc_m(doc, &mut c); - let mut previews = Vec::new(); - if let Some(t) = &opts.thumbnail { - previews.push((THUMB, png(t)?)); - } - if let Some(t) = &opts.composite { - previews.push((COMPOSITE, png(t)?)); - } let manifest = Manifest { format_version: FORMAT_VERSION, generator: format!("photocraft-format {}", env!("CARGO_PKG_VERSION")), @@ -760,40 +932,61 @@ impl PcraftWriter { for (h, b) in c.blobs { objects.insert(blob_path(&h), Object::Blob(b)); } - Ok(Prepared { manifest, objects, previews, stats }) + Ok(Prepared { manifest, objects, stats }) } /// Save as a ZIP bundle in memory. pub fn save_zip(&mut self, doc: &Document, opts: &SaveOptions) -> Result<(Vec, SaveStats)> { - let p = self.prepare(doc, opts)?; + // Another thread encodes the previews while the document is collected and its new + // objects are compressed on all cores (zstd dominates a full save). + let (previews, prepared) = beside( + opts.thumbnail.is_some() || opts.composite.is_some(), + || previews(opts), + || -> Result<_> { + let p = self.prepare(doc, opts)?; + let todo: Vec<(&String, &Object)> = p.objects.iter().filter(|(path, _)| !self.compressed.contains_key(*path)).collect(); + let fresh: HashMap>> = par_compress(&todo)?.into_iter().map(|(path, data)| (path.clone(), data)).collect(); + Ok((p, fresh)) + }, + ); + let (p, mut fresh) = prepared?; + let previews = previews?; let mut stats = p.stats; - let mut z = ZipWriter::new(); + let mut next = HashMap::with_capacity(p.objects.len()); + let mut cached_bytes = 0usize; + // The archive is sized for every entry up front (#7689): previews, then reused and fresh + // objects at their compressed sizes. + let entries = + std::iter::once((MANIFEST, p.manifest.len())).chain(previews.iter().map(|(name, data)| (*name, data.len()))).chain(p.objects.keys().map(|path| { + let size = self.compressed.get(path).or_else(|| fresh.get(path)).map_or(0, |data| data.len()); + (path.as_str(), size) + })); + let mut z = ZipWriter::with_entries(entries)?; z.add(MANIFEST, &p.manifest)?; - for (name, data) in &p.previews { + for (name, data) in &previews { z.add(name, data)?; } - let mut next = HashMap::with_capacity(p.objects.len()); - // New objects are compressed on all cores first (zstd dominates a full save). - let todo: Vec<(&String, &Object)> = p.objects.iter().filter(|(path, _)| !self.compressed.contains_key(*path)).collect(); - let mut fresh: HashMap<&String, Arc>> = par_compress(&todo)?.into_iter().collect(); for (path, obj) in &p.objects { - let data = match self.compressed.get(path) { - Some(d) => { + let (key, data) = match self.compressed.remove_entry(path) { + Some((key, data)) => { if matches!(obj, Object::Tile(..)) { stats.tiles_reused += 1; } - d.clone() + (Some(key), data) } None => { match obj { Object::Tile(..) => stats.tiles_written += 1, Object::Blob(_) => stats.blobs_written += 1, } - fresh.remove(path).unwrap_or_else(|| Arc::new(obj.compressed())) + (None, fresh.remove(path).unwrap_or_else(|| Arc::new(obj.compressed()))) } }; z.add(path, &data)?; - next.insert(path.clone(), data); + if data.capacity() <= self.compressed_budget.saturating_sub(cached_bytes) { + cached_bytes += data.capacity(); + next.insert(key.unwrap_or_else(|| path.clone()), data); + } } self.compressed = next; Ok((z.finish()?, stats)) @@ -801,10 +994,18 @@ impl PcraftWriter { /// Save into a directory bundle: reuses only valid existing objects, /// rewrites missing or damaged objects, then writes the manifest - /// atomically and removes unreferenced objects. + /// atomically and removes unreferenced objects. The whole save holds the bundle's + /// `save.lock`, so saves of one bundle from other writers or processes wait their turn. pub fn save_dir(&mut self, doc: &Document, dir: &Path, opts: &SaveOptions) -> Result { + self.save_dir_with(doc, dir, opts, &crate::atomic::RealFs) + } + + /// The same save path with an injectable publication barrier, for failure/GC ordering tests. + fn save_dir_with(&mut self, doc: &Document, dir: &Path, opts: &SaveOptions, fs: &dyn crate::atomic::AtomicFs) -> Result { let canonical_dir = bundle_root(dir)?; let dir = canonical_dir.as_path(); + // Held until the end of the save: listing, writing, publishing and cleanup (#4957). + let _lock = lock_bundle(dir, self.lock_wait)?; let existing = list_objects(dir)?; // Reject a hostile existing entry before writing any object or manifest. This also // covers entries that this save would otherwise leave untouched or garbage-collect. @@ -812,6 +1013,7 @@ impl PcraftWriter { validate_bundle_file(dir, &dir.join(path))?; } let p = self.prepare(doc, opts)?; + let previews = previews(opts)?; let mut stats = p.stats; if self.verified_directory.as_ref().is_none_or(|cache| cache.directory != canonical_dir) { self.verified_directory = Some(DirectoryVerificationCache { directory: canonical_dir.clone(), ..Default::default() }); @@ -858,7 +1060,7 @@ impl PcraftWriter { Object::Tile(..) => stats.tiles_written += 1, Object::Blob(_) => stats.blobs_written += 1, } - write_bundle_file(dir, &object_path, &obj.compressed())?; + write_bundle_file(dir, &object_path, &obj.compressed(), fs)?; let written = path_signature(&object_path)?; if let Some(cache) = &mut self.verified_directory { match written { @@ -871,23 +1073,37 @@ impl PcraftWriter { } } } - for (name, data) in &p.previews { - write_bundle_file(dir, &dir.join(name), data)?; + for (name, data) in &previews { + write_bundle_file(dir, &dir.join(name), data, fs)?; } // Pin all GC directories before removing stale previews or objects. // Unix cleanup then stays within these directories even if a pathname is swapped. let gc = BundleGc::new(dir)?; + #[cfg(windows)] + { + // A previous attempt may have published an object and then failed its directory + // flush. The retry can reuse those valid bytes (including cached ones), so flush + // both object directories again before publishing a manifest that references them. + // This also persists the fixed subdirectories of a newly created bundle. + for sub in ["tiles", "blobs"] { + fs.sync_dir(&dir.join(sub)).map_err(|e| crate::atomic::publication_error(e, &dir.join(sub)))?; + } + } + #[cfg(not(windows))] + for sub in ["tiles", "blobs"] { + let folder = dir.join(sub); + if folder.is_dir() { + confirm_flushed(&folder, &folder)?; + } + } + write_bundle_file(dir, &dir.join(MANIFEST), &p.manifest, fs)?; + // Omitted previews belong to the previous manifest until the new one commits. + // After commit, a locked leftover is only housekeeping, just like unused objects. for stale in [THUMB, COMPOSITE] { - if !p.previews.iter().any(|(n, _)| *n == stale) { - let path = dir.join(stale); - validate_bundle_file(dir, &path)?; - match remove_bundle_file(dir, &path, &gc) { - Err(FormatError::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {} - other => other?, - } + if !previews.iter().any(|(n, _)| *n == stale) { + let _ = remove_bundle_file(dir, &dir.join(stale), &gc); } } - write_bundle_file(dir, &dir.join(MANIFEST), &p.manifest)?; // The manifest is committed: the save has succeeded. Removing objects it no longer // references is best-effort housekeeping (a vanished or locked file must not turn a // complete save into a reported failure); a leftover is retried on the next save. @@ -941,6 +1157,124 @@ impl PcraftWriter { } } +#[cfg(test)] +#[test] +fn tile_hash_cache_keeps_sample_byte_order_in_its_identity() { + let mut cache = HashMap::new(); + let mut surface = photocraft_raster::Surface::new(PixelFormat::RGBA8); + surface.write_pixel(0, 0, &[0.1, 0.2, 0.3, 1.0]); + let tile = surface.tile(photocraft_geom::TileCoord::new(0, 0)).unwrap(); + let mut collect = Collect { hash_cache: Some(&mut cache), ..Default::default() }; + for sample in [SampleType::U8, SampleType::U16] { + let format = if sample == SampleType::U8 { PixelFormat::RGBA8 } else { PixelFormat::new(photocraft_color::ColorMode::Grayscale, sample, true) }; + assert_eq!(collect.tile(format, tile), hash_bytes(&tile_le(tile, sample))); + } + drop(collect); + assert_eq!(cache.len(), 2); +} + +#[cfg(test)] +#[test] +fn blob_hash_cache_tracks_arc_identity_without_retaining_payloads() { + let mut cache = HashMap::new(); + let original = Arc::new(vec![1, 2, 3]); + let weak = Arc::downgrade(&original); + let mut collect = Collect { blob_hash_cache: Some(&mut cache), ..Default::default() }; + let hash = collect.blob(&original); + assert_eq!(collect.blob(&original), hash); + let mut edited = original.clone(); + Arc::make_mut(&mut edited)[0] = 7; + assert_eq!(collect.blob(&edited), hash_bytes(&[7, 2, 3])); + assert_ne!(collect.blob(&edited), hash); + drop(collect); + assert_eq!(cache.len(), 2); + drop(original); + assert!(weak.upgrade().is_none()); +} + +#[cfg(test)] +#[test] +fn compressed_cache_budget_limits_retained_capacity_without_changing_bytes() { + use photocraft_doc::{Layer, LayerContent}; + use photocraft_geom::Size; + use photocraft_raster::Surface; + + let mut doc = Document::new("cache", Size::new(257, 1), photocraft_color::ColorMode::Rgb, SampleType::U8); + let mut surface = Surface::new(doc.pixel_format()); + surface.write_pixel(0, 0, &[0.2, 0.4, 0.6, 1.0]); + surface.write_pixel(256, 0, &[0.6, 0.4, 0.2, 1.0]); + doc.layers.push(Layer::new("pixels", LayerContent::Raster(surface))); + let mut writer = PcraftWriter::new(); + let (expected, _) = writer.save_zip(&doc, &SaveOptions::default()).unwrap(); + let key = writer.compressed.keys().next().unwrap().clone(); + let key_allocation = writer.compressed.get_key_value(&key).unwrap().0.as_ptr(); + assert_eq!(writer.save_zip(&doc, &SaveOptions::default()).unwrap().0, expected); + assert_eq!(writer.compressed.get_key_value(&key).unwrap().0.as_ptr(), key_allocation); + let budget = writer.compressed.values().map(|v| v.capacity()).min().unwrap(); + writer.set_compressed_cache_budget(budget); + assert!(writer.compressed.is_empty()); + let (bytes, _) = writer.save_zip(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(bytes, expected); + assert!(writer.compressed.values().map(|v| v.capacity()).sum::() <= budget); + assert!(!writer.compressed.is_empty()); + let retained = writer.compressed.len(); + let (bytes, stats) = writer.save_zip(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(bytes, expected); + assert_eq!(stats.tiles_reused, retained); + writer.set_compressed_cache_budget(0); + let (bytes, stats) = writer.save_zip(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(bytes, expected); + assert_eq!(stats.tiles_reused, 0); + assert!(writer.compressed.is_empty()); +} + +#[cfg(test)] +#[test] +fn previews_follow_the_manifest_with_the_bytes_of_a_serial_encode() { + use photocraft_doc::{Layer, LayerContent}; + use photocraft_geom::Size; + use photocraft_raster::Surface; + + let mut doc = Document::new("previews", Size::new(600, 3), photocraft_color::ColorMode::Rgb, SampleType::U8); + let mut surface = Surface::new(doc.pixel_format()); + for x in 0..600 { + surface.write_pixel(x, 1, &[x as f32 / 600.0, 0.5, 0.25, 1.0]); + } + doc.layers.push(Layer::new("pixels", LayerContent::Raster(surface))); + let image = |w: u32, h: u32, k: u8| { + let mut i = Rgba8Image::new(w, h); + i.pixels.iter_mut().enumerate().for_each(|(j, p)| *p = (j as u8).wrapping_mul(k)); + i + }; + let (thumb, composite) = (image(4, 3, 7), image(9, 5, 13)); + for (t, c) in [(true, true), (true, false), (false, true), (false, false)] { + let opts = SaveOptions { thumbnail: t.then(|| thumb.clone()), composite: c.then(|| composite.clone()) }; + let (bytes, _) = PcraftWriter::new().save_zip(&doc, &opts).unwrap(); + let zip = ZipReader::new(&bytes).unwrap(); + let names: Vec<&str> = zip.entries.iter().map(|e| e.name.as_str()).collect(); + let mut want = vec![MANIFEST]; + want.extend(t.then_some(THUMB)); + want.extend(c.then_some(COMPOSITE)); + assert_eq!(names[..want.len()], want[..]); + assert!(names[want.len()..].iter().all(|n| n.starts_with("tiles/") || n.starts_with("blobs/")), "{names:?}"); + for (name, img) in [(THUMB, opts.thumbnail.as_ref()), (COMPOSITE, opts.composite.as_ref())] { + assert_eq!(zip.read_by_name(name, usize::MAX).ok(), img.map(|i| png(i).unwrap()), "{name}"); + } + } + let bad = SaveOptions { thumbnail: Some(Rgba8Image { width: 4, height: 3, pixels: vec![0; 5] }), composite: Some(composite.clone()) }; + assert!(matches!(PcraftWriter::new().save_zip(&doc, &bad), Err(FormatError::Unsupported(m)) if m.starts_with("preview"))); + let bad = SaveOptions { thumbnail: Some(thumb.clone()), composite: Some(Rgba8Image { width: 9, height: 5, pixels: vec![0; 5] }) }; + assert!(matches!(PcraftWriter::new().save_zip(&doc, &bad), Err(FormatError::Unsupported(m)) if m.starts_with("preview"))); + + let dir = std::env::temp_dir().join(format!("pcraft-previews-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let opts = SaveOptions { thumbnail: Some(thumb.clone()), composite: Some(composite.clone()) }; + PcraftWriter::new().save_dir(&doc, &dir, &opts).unwrap(); + assert_eq!(std::fs::read(dir.join(THUMB)).unwrap(), png(&thumb).unwrap()); + assert_eq!(std::fs::read(dir.join(COMPOSITE)).unwrap(), png(&composite).unwrap()); + std::fs::remove_dir_all(&dir).unwrap(); +} + #[cfg(all(test, unix))] #[test] fn gc_pinned_directory_handles_resist_subdirectory_swap() { @@ -971,6 +1305,72 @@ fn gc_pinned_directory_handles_resist_subdirectory_swap() { std::fs::remove_dir_all(root).unwrap(); } +/// #4968: a manifest whose folder can't be flushed fails the save, and the objects the previous +/// manifest references stay in place; the next save that flushes removes what is unreferenced. +#[cfg(all(test, not(windows)))] +#[test] +fn a_failed_manifest_flush_keeps_the_previous_objects() { + use std::sync::atomic::{AtomicU64, Ordering}; + + static NEXT: AtomicU64 = AtomicU64::new(0); + let dir = std::env::temp_dir().join(format!("pcraft-flush-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))); + std::fs::create_dir_all(&dir).unwrap(); + let dir = std::fs::canonicalize(dir).unwrap(); + let mut doc = Document::new("flush", photocraft_doc::Size::new(64, 64), photocraft_color::ColorMode::Rgb, SampleType::U8); + let id = doc.insert_above(None, photocraft_doc::Layer::raster("paint", doc.pixel_format())); + let paint = |doc: &mut Document, red: f32| { + doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 64, 64), &[red, 0.5, 0.25, 1.0]); + }; + paint(&mut doc, 1.0); + let mut w = PcraftWriter::new(); + w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + let old = list_objects(&dir).unwrap(); + paint(&mut doc, 0.0); + FAIL_DIR_FLUSH.with(|f| f.set(Some(MANIFEST))); + let e = w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap_err(); + assert!(e.to_string().contains("could not be flushed"), "{e}"); + let now = list_objects(&dir).unwrap(); + assert!(old.is_subset(&now), "the previous manifest's objects are kept"); + assert!(now.len() > old.len(), "the new objects were written"); + let stats = w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert!(stats.objects_removed > 0, "the next save removes the unreferenced objects"); + assert_eq!(crate::load_path(&dir).unwrap(), doc); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// An object whose folder flush failed is reused by the retried save, which writes no new object +/// there: that save still flushes the object folder before committing a manifest that names it. +#[cfg(all(test, not(windows)))] +#[test] +fn a_retried_save_flushes_the_folder_of_a_reused_object() { + use std::sync::atomic::{AtomicU64, Ordering}; + + static NEXT: AtomicU64 = AtomicU64::new(0); + let dir = std::env::temp_dir().join(format!("pcraft-retry-{}-{}", std::process::id(), NEXT.fetch_add(1, Ordering::Relaxed))); + std::fs::create_dir_all(&dir).unwrap(); + let dir = std::fs::canonicalize(dir).unwrap(); + // One layer of one tile: the retry finds its only object already written. + let mut doc = Document::new("retry", photocraft_doc::Size::new(16, 16), photocraft_color::ColorMode::Rgb, SampleType::U8); + let id = doc.insert_above(None, photocraft_doc::Layer::raster("paint", doc.pixel_format())); + doc.layer_mut(id).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 16, 16), &[1.0, 0.5, 0.25, 1.0]); + let mut w = PcraftWriter::new(); + FAIL_DIR_FLUSH.with(|f| f.set(Some("tiles"))); + let e = w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap_err(); + assert!(e.to_string().contains("could not be flushed"), "{e}"); + assert!(!dir.join(MANIFEST).exists(), "nothing committed"); + let written = list_objects(&dir).unwrap(); + assert_eq!(written.iter().filter(|o| o.starts_with("tiles/")).count(), 1, "{written:?}"); + FLUSHED.with(|f| f.borrow_mut().clear()); + let stats = w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert_eq!(stats.tiles_written, 0, "the tile is reused"); + let flushed = FLUSHED.with(|f| f.borrow().clone()); + let tiles = flushed.iter().position(|d| *d == dir.join("tiles")); + let manifest = flushed.iter().rposition(|d| *d == dir); + assert!(tiles.is_some() && tiles < manifest, "tiles flushed before the manifest: {flushed:?}"); + assert_eq!(crate::load_path(&dir).unwrap(), doc); + std::fs::remove_dir_all(dir).unwrap(); +} + fn list_objects(dir: &Path) -> Result> { let mut out = HashSet::new(); for sub in ["tiles", "blobs"] { @@ -1007,3 +1407,6 @@ pub(crate) fn write_atomic(path: &Path, data: &[u8]) -> Result<()> { crate::atomic::atomic_write(path, data)?; Ok(()) } + +#[cfg(all(test, windows))] +mod durability_tests; diff --git a/crates/format/src/store/durability_tests.rs b/crates/format/src/store/durability_tests.rs new file mode 100644 index 0000000000..477c476a99 --- /dev/null +++ b/crates/format/src/store/durability_tests.rs @@ -0,0 +1,117 @@ +//! Exercise the real directory save, including cached-object reuse and garbage collection. +use std::cell::{Cell, RefCell}; +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, Size}; + +use super::{MANIFEST, PcraftWriter, SaveOptions, list_objects}; +use crate::atomic::{AtomicFs, RealFs}; + +/// The object or manifest directory barrier at which to inject one publication failure. +#[derive(Clone, Copy, Debug)] +enum FailAt { + Tiles, + Blobs, + Manifest, +} + +/// Real filesystem operations with recorded barriers and a single failure at the chosen stage. +struct PublicationFault { + root: PathBuf, + old_manifest: Vec, + stage: FailAt, + armed: Cell, + synced: RefCell>, +} + +impl AtomicFs for PublicationFault { + /// Keep temporary-file writes and file flushes real; only directory barriers are injected. + fn write_new(&self, path: &Path, bytes: &[u8]) -> io::Result<()> { + RealFs.write_new(path, bytes) + } + /// Publish through the real rename so a barrier error occurs after replacement. + fn rename(&self, from: &Path, to: &Path) -> io::Result<()> { + RealFs.rename(from, to) + } + /// Use real cleanup so the test can detect premature removal of old objects. + fn remove(&self, path: &Path) -> io::Result<()> { + RealFs.remove(path) + } + /// Preserve normal replacement metadata behavior during failure injection. + fn copy_metadata(&self, from: &Path, to: &Path) -> io::Result<()> { + RealFs.copy_metadata(from, to) + } + /// Record every barrier and fail the selected stage once, allowing a same-writer retry. + fn sync_dir(&self, dir: &Path) -> io::Result<()> { + self.synced.borrow_mut().push(dir.to_path_buf()); + let fail = match self.stage { + FailAt::Tiles => dir == self.root.join("tiles"), + FailAt::Blobs => dir == self.root.join("blobs"), + FailAt::Manifest => dir == self.root && std::fs::read(dir.join(MANIFEST))? != self.old_manifest, + }; + if fail && self.armed.replace(false) { + return Err(io::Error::other("injected publication flush failure")); + } + RealFs.sync_dir(dir) + } +} + +/// Failed object or manifest publication retains old objects; a retry flushes reused objects +/// before the manifest and resumes garbage collection only after durable publication. +#[test] +fn failed_bundle_publication_retains_old_objects_and_same_writer_retry_is_durable() { + static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + for stage in [FailAt::Tiles, FailAt::Blobs, FailAt::Manifest] { + let id = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("pcraft-durability-{}-{id}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let dir = std::fs::canonicalize(dir).unwrap(); + let mut old = Document::new("old", Size::new(1, 1), ColorMode::Rgb, SampleType::U8); + let mut layer = Layer::raster("pixels", old.pixel_format()); + layer.surface_mut().unwrap().write_pixel(0, 0, &[1.0, 0.0, 0.0, 1.0]); + old.layers.push(layer); + old.metadata.exif = Some(Arc::new(vec![1; 100])); + let mut new = old.clone(); + new.name = "new".into(); + new.layers[0].surface_mut().unwrap().write_pixel(0, 0, &[0.0, 0.0, 1.0, 1.0]); + new.metadata.exif = Some(Arc::new(vec![2; 100])); + + let mut writer = PcraftWriter::new(); + writer.save_dir(&old, &dir, &SaveOptions::default()).unwrap(); + let old_objects: Vec<_> = list_objects(&dir).unwrap().into_iter().map(|p| (p.clone(), std::fs::read(dir.join(p)).unwrap())).collect(); + assert!(old_objects.len() >= 2); + let fs = PublicationFault { + old_manifest: std::fs::read(dir.join(MANIFEST)).unwrap(), + root: dir.clone(), + stage, + armed: Cell::new(true), + synced: RefCell::new(Vec::new()), + }; + + let error = writer.save_dir_with(&new, &dir, &SaveOptions::default(), &fs).unwrap_err(); + assert!(!fs.armed.get(), "the {stage:?} failure fired"); + assert!(error.to_string().contains("may not survive"), "{error}"); + let expected = if matches!(stage, FailAt::Manifest) { &new } else { &old }; + assert_eq!(&crate::load_path(&dir).unwrap(), expected); + for (path, bytes) in &old_objects { + assert_eq!(&std::fs::read(dir.join(path)).unwrap(), bytes, "old object removed before durable publication: {path}"); + } + + fs.synced.borrow_mut().clear(); + writer.save_dir_with(&new, &dir, &SaveOptions::default(), &fs).unwrap(); + assert_eq!(crate::load_path(&dir).unwrap(), new); + let synced = fs.synced.borrow(); + let publication = synced.iter().rposition(|p| p == &dir).unwrap(); + for sub in ["tiles", "blobs"] { + assert!( + synced.iter().position(|p| p == &dir.join(sub)).is_some_and(|position| position < publication), + "retry must flush reused {sub} before its manifest" + ); + } + assert!(old_objects.iter().all(|(p, _)| !dir.join(p).exists()), "cleanup resumes after the durable retry"); + std::fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/crates/format/src/zip.rs b/crates/format/src/zip.rs index 9ec92c2fba..2452f87903 100644 --- a/crates/format/src/zip.rs +++ b/crates/format/src/zip.rs @@ -3,6 +3,7 @@ //! unzipped and re-zipped by another tool still loads). No ZIP64, no //! encryption, no multi-disk archives. +use std::borrow::Cow; use std::collections::HashMap; use std::io::Read; @@ -31,18 +32,41 @@ impl Default for ZipWriter { } impl ZipWriter { + /// Creates an empty archive of STORE entries. pub fn new() -> Self { ZipWriter { out: Vec::new(), central: Vec::new(), count: 0 } } + /// Reserves the complete archive from its entry names and payload sizes. + pub(crate) fn with_entries<'a>(entries: impl IntoIterator) -> Result { + let (size, directory) = entries.into_iter().try_fold((22usize, 0usize), |(size, directory), (name, bytes)| { + let directory = directory.checked_add(46 + name.len()); + let size = size.checked_add(76).and_then(|n| n.checked_add(name.len())).and_then(|n| n.checked_add(name.len())).and_then(|n| n.checked_add(bytes)); + size.zip(directory).ok_or_else(|| FormatError::Unsupported("zip size overflow".into())) + })?; + let mut writer = Self::new(); + writer.out.try_reserve_exact(size).map_err(|_| FormatError::LimitExceeded("not enough memory for zip".into()))?; + writer.central.try_reserve_exact(directory).map_err(|_| FormatError::LimitExceeded("not enough memory for zip directory".into()))?; + Ok(writer) + } + + /// Adds a STORE entry, rejecting names or data that exceed ZIP field limits. + /// The entry path has a 16-bit byte length, independent of filesystem limits. pub fn add(&mut self, name: &str, data: &[u8]) -> Result<(), FormatError> { if self.count == u16::MAX { return Err(FormatError::Unsupported("more than 65535 zip entries (ZIP64 not supported)".into())); } let size = u32::try_from(data.len()).map_err(|_| FormatError::Unsupported("zip entry over 4 GiB".into()))?; let offset = u32::try_from(self.out.len()).map_err(|_| FormatError::Unsupported("zip over 4 GiB".into()))?; - let crc = crc32(data); let name_b = name.as_bytes(); + let name_len = u16::try_from(name_b.len()).map_err(|_| FormatError::Unsupported("zip entry path exceeds the 65535-byte format limit".into()))?; + let local_len = 30usize + .checked_add(name_b.len()) + .and_then(|n| n.checked_add(data.len())) + .ok_or_else(|| FormatError::Unsupported("zip entry size overflow".into()))?; + self.out.try_reserve(local_len).map_err(|_| FormatError::LimitExceeded("not enough memory for zip entry".into()))?; + self.central.try_reserve(46 + name_b.len()).map_err(|_| FormatError::LimitExceeded("not enough memory for zip directory".into()))?; + let crc = crc32(data); // Local file header. let o = &mut self.out; o.extend_from_slice(&LOCAL_SIG.to_le_bytes()); @@ -54,7 +78,7 @@ impl ZipWriter { o.extend_from_slice(&crc.to_le_bytes()); o.extend_from_slice(&size.to_le_bytes()); o.extend_from_slice(&size.to_le_bytes()); - o.extend_from_slice(&(name_b.len() as u16).to_le_bytes()); + o.extend_from_slice(&name_len.to_le_bytes()); o.extend_from_slice(&0u16.to_le_bytes()); o.extend_from_slice(name_b); o.extend_from_slice(data); @@ -70,7 +94,7 @@ impl ZipWriter { c.extend_from_slice(&crc.to_le_bytes()); c.extend_from_slice(&size.to_le_bytes()); c.extend_from_slice(&size.to_le_bytes()); - c.extend_from_slice(&(name_b.len() as u16).to_le_bytes()); + c.extend_from_slice(&name_len.to_le_bytes()); c.extend_from_slice(&[0; 8]); // extra len, comment len, disk, internal attrs c.extend_from_slice(&0u32.to_le_bytes()); // external attrs c.extend_from_slice(&offset.to_le_bytes()); @@ -79,9 +103,11 @@ impl ZipWriter { Ok(()) } + /// Appends the central directory and returns the complete archive. pub fn finish(mut self) -> Result, FormatError> { let cd_offset = u32::try_from(self.out.len()).map_err(|_| FormatError::Unsupported("zip over 4 GiB".into()))?; - let cd_size = self.central.len() as u32; + let cd_size = u32::try_from(self.central.len()).map_err(|_| FormatError::Unsupported("zip directory over 4 GiB".into()))?; + self.out.try_reserve(self.central.len().saturating_add(22)).map_err(|_| FormatError::LimitExceeded("not enough memory to finish zip".into()))?; self.out.extend_from_slice(&self.central); self.out.extend_from_slice(&EOCD_SIG.to_le_bytes()); self.out.extend_from_slice(&[0; 4]); // disk numbers @@ -112,7 +138,7 @@ pub struct ZipReader<'a> { pub entries: Vec, /// Name -> index of the first entry with that name, so `find` is O(1) /// instead of a scan per lookup (a bundle reads one entry per tile). - by_name: HashMap, + by_name: HashMap, usize>, } fn u16_at(b: &[u8], o: usize) -> Result { @@ -123,21 +149,36 @@ fn u32_at(b: &[u8], o: usize) -> Result { } impl<'a> ZipReader<'a> { + /// Checks ZIP record bounds and indexes the first entry for each name. pub fn new(bytes: &'a [u8]) -> Result { if bytes.len() < 22 { return Err(FormatError::corrupt("not a zip archive (too short)")); } - // End of central directory: scan back over a possible comment. + // A comment may itself contain the signature. Only accept a record whose + // declared comment reaches the end of the archive (APPNOTE 4.3.16). let min = bytes.len().saturating_sub(22 + 65535); let eocd = (min..=bytes.len() - 22) .rev() - .find(|&i| u32_at(bytes, i).ok() == Some(EOCD_SIG)) + .find(|&i| { + if u32_at(bytes, i).ok() != Some(EOCD_SIG) { + return false; + } + let comment_end = u16_at(bytes, i + 20).ok().and_then(|length| i.checked_add(22)?.checked_add(usize::from(length))); + let directory_end = u32_at(bytes, i + 16) + .ok() + .zip(u32_at(bytes, i + 12).ok()) + .and_then(|(offset, size)| usize::try_from(offset).ok()?.checked_add(usize::try_from(size).ok()?)); + // In the supported non-ZIP64 layout, the central directory immediately + // precedes this record. A zeroed signature in a comment is not an empty ZIP. + comment_end == Some(bytes.len()) && directory_end == Some(i) + }) .ok_or_else(|| FormatError::corrupt("zip end-of-central-directory not found"))?; let count = u16_at(bytes, eocd + 10)? as usize; let cd_size = u32_at(bytes, eocd + 12)? as usize; let cd_off = u32_at(bytes, eocd + 16)? as usize; let cd_end = cd_off.checked_add(cd_size).filter(|end| *end <= eocd).ok_or_else(|| FormatError::corrupt("zip central directory out of range"))?; - let mut entries = Vec::with_capacity(count.min(bytes.len() / 46)); + let mut entries = Vec::with_capacity(count.min(cd_size / 46)); + let mut by_name = HashMap::with_capacity(entries.capacity()); let mut p = cd_off; for _ in 0..count { let header_end = p.checked_add(46).filter(|end| *end <= cd_end).ok_or_else(|| FormatError::corrupt("truncated zip central directory entry"))?; @@ -155,7 +196,7 @@ impl<'a> ZipReader<'a> { let local = u32_at(bytes, p + 42)? as usize; let name_end = header_end.checked_add(nlen).filter(|end| *end <= cd_end).ok_or_else(|| FormatError::corrupt("zip entry name out of range"))?; let name = bytes.get(header_end..name_end).ok_or_else(|| FormatError::corrupt("truncated zip name"))?; - let name = String::from_utf8_lossy(name).into_owned(); + let name = String::from_utf8_lossy(name); if flags & 1 != 0 { return Err(FormatError::Unsupported("encrypted zip entries".into())); } @@ -181,14 +222,11 @@ impl<'a> ZipReader<'a> { if data_offset.checked_add(compressed).is_none_or(|end| end > bytes.len()) { return Err(FormatError::corrupt(format!("zip entry `{name}` out of range"))); } - entries.push(Entry { name, method, crc, compressed, uncompressed, data_offset }); + // The index borrows valid UTF-8 names; duplicate names keep the first entry. + by_name.entry(name.clone()).or_insert(entries.len()); + entries.push(Entry { name: name.into_owned(), method, crc, compressed, uncompressed, data_offset }); p = entry_end; } - let mut by_name = HashMap::with_capacity(entries.len()); - for (i, e) in entries.iter().enumerate() { - // Duplicate names: the first entry wins, as with a front-to-back scan. - by_name.entry(e.name.clone()).or_insert(i); - } Ok(ZipReader { bytes, entries, by_name }) } @@ -214,8 +252,9 @@ impl<'a> ZipReader<'a> { // small forged archive must not reserve gigabytes up front, so the // preallocation is capped (DEFLATE expands at most ~1032x). let mut out = Vec::with_capacity(e.uncompressed.min(raw.len().saturating_mul(1032)).min(64 << 20)); - flate2::read::DeflateDecoder::new(raw) - .take((max as u64).saturating_add(1)) + // One surplus byte proves a false size without expanding to the caller's larger budget. + flate2::bufread::DeflateDecoder::new(raw) + .take((e.uncompressed as u64).saturating_add(1)) .read_to_end(&mut out) .map_err(|err| FormatError::corrupt(format!("zip entry `{}`: {err}", e.name)))?; out @@ -248,6 +287,20 @@ mod tests { assert_eq!(crc32(b"123456789"), 0xCBF4_3926); } + /// ZIP entry paths have a format limit even when each component is short. + #[test] + fn entry_path_length_is_checked_before_writing() { + let mut writer = ZipWriter::new(); + for name in ["dir/".repeat(16384), "é/".repeat(21846)] { + assert!(matches!(writer.add(&name, b"invalid"), Err(FormatError::Unsupported(_)))); + assert!(writer.out.is_empty() && writer.central.is_empty()); + } + let name = "dir/".repeat(16383) + "end"; + writer.add(&name, b"valid").unwrap(); + let bytes = writer.finish().unwrap(); + assert_eq!(ZipReader::new(&bytes).unwrap().read_by_name(&name, 5).unwrap(), b"valid"); + } + #[test] fn roundtrip() { let mut w = ZipWriter::new(); @@ -261,6 +314,57 @@ mod tests { assert_eq!(r.read_by_name("dir/b.bin", 100).unwrap(), [0, 1, 2, 3]); assert!(r.read_by_name("empty", 0).unwrap().is_empty()); assert!(matches!(r.read_by_name("a.txt", 3), Err(FormatError::LimitExceeded(_)))); + assert!(r.by_name.keys().all(|name| matches!(name, Cow::Borrowed(_)))); + } + + fn archive_with_comment(comment: &[u8]) -> Vec { + let mut writer = ZipWriter::new(); + writer.add("manifest.json", b"project content").unwrap(); + let mut bytes = writer.finish().unwrap(); + let length_offset = bytes.len() - 2; + bytes[length_offset..].copy_from_slice(&u16::try_from(comment.len()).unwrap().to_le_bytes()); + bytes.extend_from_slice(comment); + bytes + } + + #[test] + fn directory_signature_inside_comment_does_not_hide_entries() { + for length in [22, 32, usize::from(u16::MAX)] { + let mut comment = vec![0; length]; + comment[..4].copy_from_slice(&EOCD_SIG.to_le_bytes()); + let bytes = archive_with_comment(&comment); + let reader = ZipReader::new(&bytes).unwrap(); + assert_eq!(reader.entries.len(), 1, "comment length {length}"); + assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content"); + } + } + + #[test] + fn archive_comments_support_empty_short_and_maximum_lengths() { + for length in [0, 1, 21, 22, 100, usize::from(u16::MAX)] { + let bytes = archive_with_comment(&vec![b'x'; length]); + let reader = ZipReader::new(&bytes).unwrap(); + assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content", "comment length {length}"); + } + } + + #[test] + fn central_directory_digital_signature_remains_supported() { + let mut bytes = archive_with_comment(b"signed archive"); + let eocd = bytes.len() - 22 - b"signed archive".len(); + let signature = [0x50, 0x4b, 0x05, 0x05, 3, 0, 1, 2, 3]; + let size = u32_at(&bytes, eocd + 12).unwrap() + signature.len() as u32; + bytes[eocd + 12..eocd + 16].copy_from_slice(&size.to_le_bytes()); + bytes.splice(eocd..eocd, signature); + let reader = ZipReader::new(&bytes).unwrap(); + assert_eq!(reader.read_by_name("manifest.json", 100).unwrap(), b"project content"); + } + + #[test] + fn truncated_archive_comment_is_rejected() { + let mut bytes = archive_with_comment(b"comment"); + bytes.pop(); + assert!(ZipReader::new(&bytes).is_err()); } #[test] @@ -316,6 +420,46 @@ mod tests { assert_eq!(r.read_by_name("b", 100).unwrap(), b"other"); } + /// Reservation changes neither entry bytes nor final output, and avoids growth. + #[test] + fn whole_archive_reservation_preserves_output() { + let entries = [("层/é.png", &b"image"[..]), ("empty", &b""[..]), ("empty", &b"duplicate"[..])]; + for entries in [&entries[..0], &entries[..]] { + let mut previous = ZipWriter::new(); + let mut reserved = ZipWriter::with_entries(entries.iter().map(|(name, data)| (*name, data.len()))).unwrap(); + let (capacity, directory) = (reserved.out.capacity(), reserved.central.capacity()); + for (name, data) in entries { + previous.add(name, data).unwrap(); + reserved.add(name, data).unwrap(); + } + assert_eq!(reserved.central.capacity(), directory); + let bytes = reserved.finish().unwrap(); + assert_eq!(bytes.capacity(), capacity); + assert_eq!(bytes, previous.finish().unwrap()); + } + assert!(matches!(ZipWriter::with_entries([("x", usize::MAX)]), Err(FormatError::Unsupported(_)))); + assert!(matches!(ZipWriter::with_entries([("x", isize::MAX as usize)]), Err(FormatError::LimitExceeded(_)))); + } + + /// Invalid UTF-8 names retain their lossy spelling and first-entry lookup. + #[test] + fn name_index_borrows_unicode_and_owns_lossy_names() { + let mut writer = ZipWriter::new(); + for name in ["层/é.png", "a", "b"] { + writer.add(name, name.as_bytes()).unwrap(); + } + let mut bytes = writer.finish().unwrap(); + let mut p = u32_at(&bytes, bytes.len() - 6).unwrap() as usize; + p += 46 + "层/é.png".len(); + bytes[p + 46] = 0xff; + bytes[p + 47 + 46] = 0xfe; + let reader = ZipReader::new(&bytes).unwrap(); + assert_eq!(reader.read_by_name("层/é.png", 100).unwrap(), "层/é.png".as_bytes()); + assert!(matches!(reader.by_name.get_key_value("层/é.png").unwrap().0, Cow::Borrowed(_))); + assert!(matches!(reader.by_name.get_key_value("�").unwrap().0, Cow::Owned(_))); + assert_eq!(reader.read_by_name("�", 1).unwrap(), b"a"); + } + #[test] fn crc_mismatch_detected() { let mut w = ZipWriter::new(); @@ -327,9 +471,63 @@ mod tests { assert!(r.read_by_name("a", 100).is_err()); } - /// A valid DEFLATE-compressed bundle may be read with an effectively - /// unlimited caller budget (e.g. corruption/round-trip checks). On 64-bit - /// targets `usize::MAX as u64 + 1` overflows, so the decoder must saturate. + #[test] + fn slice_deflate_matches_buffered_reader_and_preserves_checks() { + use std::io::Write as _; + + let content = b"PhotoCraft ZIP payload".repeat(5000); + let mut encoder = flate2::write::DeflateEncoder::new(Vec::new(), flate2::Compression::none()); + encoder.write_all(&content).unwrap(); + let compressed = encoder.finish().unwrap(); + assert!(compressed.len() > 32 * 1024); + let mut previous = Vec::new(); + flate2::read::DeflateDecoder::new(compressed.as_slice()).read_to_end(&mut previous).unwrap(); + let mut writer = ZipWriter::new(); + writer.add("payload", &compressed).unwrap(); + let bytes = writer.finish().unwrap(); + let reader = ZipReader::new(&bytes).unwrap(); + let mut entry = reader.entries[0].clone(); + entry.method = 8; + entry.uncompressed = content.len(); + entry.crc = crc32(&content); + assert_eq!(reader.read(&entry, content.len()).unwrap(), previous); + assert!(matches!(reader.read(&entry, content.len() - 1), Err(FormatError::LimitExceeded(_)))); + entry.crc ^= 1; + assert!(matches!(reader.read(&entry, content.len()), Err(FormatError::Corrupt(_)))); + entry.crc ^= 1; + entry.compressed /= 2; + assert!(matches!(reader.read(&entry, content.len()), Err(FormatError::Corrupt(_)))); + } + + /// A false small size must stop expansion before decoding the rest of the stream. + #[test] + fn deflate_surplus_stops_before_a_corrupt_suffix() { + // Exceed miniz's internal 32 KiB output buffer before the corrupt block. + let raw = [&[0, 0xff, 0xff, 0, 0][..], &[b'x'; 65535], &[7]].concat(); + let reader = ZipReader { bytes: &raw, entries: Vec::new(), by_name: HashMap::new() }; + for declared in [0, 7] { + let entry = Entry { name: "surplus".into(), method: 8, crc: 0, compressed: raw.len(), uncompressed: declared, data_offset: 0 }; + let err = reader.read(&entry, usize::MAX).unwrap_err(); + assert!(err.to_string().contains("wrong size"), "{err}"); + } + } + + /// Correct output length and CRC do not replace the final DEFLATE block. + #[test] + fn deflate_requires_a_complete_stream_including_empty_entries() { + for (raw, content) in [(&[0, 1, 0, 0xfe, 0xff, b'x', 1, 0, 0, 0xff, 0xff][..], &b"x"[..]), (&[3, 0][..], &b""[..])] { + let reader = ZipReader { bytes: raw, entries: Vec::new(), by_name: HashMap::new() }; + let mut entry = + Entry { name: "complete".into(), method: 8, crc: crc32(content), compressed: raw.len(), uncompressed: content.len(), data_offset: 0 }; + assert_eq!(reader.read(&entry, content.len()).unwrap(), content); + for cut in 0..raw.len() { + entry.compressed = cut; + assert!(matches!(reader.read(&entry, content.len()), Err(FormatError::Corrupt(_))), "cut at {cut}"); + } + } + } + + /// An unlimited caller budget still checks a DEFLATE entry's declared size. #[test] fn deflated_entry_with_unbounded_limit_round_trips() { use std::io::Write as _; diff --git a/crates/format/tests/analysis.rs b/crates/format/tests/it/analysis.rs similarity index 100% rename from crates/format/tests/analysis.rs rename to crates/format/tests/it/analysis.rs diff --git a/crates/format/tests/autosave.rs b/crates/format/tests/it/autosave.rs similarity index 63% rename from crates/format/tests/autosave.rs rename to crates/format/tests/it/autosave.rs index 29905d7ac5..6eeb48d66f 100644 --- a/crates/format/tests/autosave.rs +++ b/crates/format/tests/it/autosave.rs @@ -1,6 +1,6 @@ //! Background autosave and crash recovery. -mod common; +use crate::common; use std::sync::Arc; use common::*; @@ -34,19 +34,26 @@ fn background_save_outcomes_report_failure_then_recovery() { let root = temp_dir("retry-failed-write"); let blocked = root.join("recovery"); std::fs::write(&blocked, b"not a directory").unwrap(); - let saver = Autosaver::new(&blocked, "doc"); + // Not dropped on a failure: dropping joins the worker, which waits for a write in progress, + // so a stuck write would hold the test past its own bound until nextest kills it. + let saver = std::mem::ManuallyDrop::new(Autosaver::new(&blocked, "doc")); let doc = Arc::new(rich_doc(ColorMode::Rgb, SampleType::U8)); saver.request_checked(doc.clone(), 7, None, SaveOptions::default()).unwrap(); + // The bound only turns a worker that never reports into a failure instead of a hang. A real + // write flushes every tile to disk and took over 30 s on a busy Windows CI runner (normally + // 2-3 s), so it is generous, and still under the 10 minutes after which nextest's `ci` + // profile kills a test. + const PATIENCE: std::time::Duration = std::time::Duration::from_secs(5 * 60); let receive = |saver: &Autosaver| { - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30); + let deadline = std::time::Instant::now() + PATIENCE; while std::time::Instant::now() < deadline { if let Some(outcome) = saver.take_completed().into_iter().next() { return outcome; } std::thread::sleep(std::time::Duration::from_millis(10)); } - panic!("autosave worker did not report its completed write within 30 seconds"); + panic!("autosave worker did not report its completed write within {PATIENCE:?}"); }; let (revision, result) = receive(&saver); assert_eq!(revision, 7); @@ -60,7 +67,7 @@ fn background_save_outcomes_report_failure_then_recovery() { result.unwrap(); assert_eq!(list_recovery(&blocked).len(), 1); assert_eq!(recover(&list_recovery(&blocked)[0]).unwrap(), *doc); - drop(saver); + drop(std::mem::ManuallyDrop::into_inner(saver)); std::fs::remove_dir_all(root).unwrap(); } @@ -188,6 +195,56 @@ fn recovered_entries_survive_a_second_crash_until_saved_or_closed() { std::fs::remove_dir_all(dir).unwrap(); } +/// #3438: a session about to end without asking (its window's graphics device was lost) writes +/// everything queued before it returns, so the next launch recovers it. +#[test] +fn flush_writes_every_queued_autosave_before_returning() { + let dir = temp_dir("flush"); + let (a, b) = (version(1, 3, ColorMode::Rgb), version(2, 5, ColorMode::Grayscale)); + let mut store = RecoveryStore::new(&dir); + store.autosave_checked(&Arc::new(a.clone()), 4, None).unwrap(); + store.autosave_checked(&Arc::new(b.clone()), 6, Some("/work/b.pcraft".into())).unwrap(); + let mut done = store.flush(); + done.sort_by_key(|(id, ..)| *id); + assert_eq!(done, vec![(1, 4, Ok(())), (2, 6, Ok(()))]); + // On disk while the store is still alive, not only once it is dropped. + let docs = recovered(&RecoveryStore::new(&dir)); + assert_eq!(docs.len(), 2); + assert!(docs.values().any(|d| *d == same(a.clone())) && docs.values().any(|d| *d == same(b.clone()))); + // A later autosave (trying a failed write again) starts a new worker on the same entry. + let newer = version(1, 4, ColorMode::Rgb); + store.autosave_checked(&Arc::new(newer.clone()), 5, None).unwrap(); + assert_eq!(store.flush(), vec![(1, 5, Ok(()))]); + let docs = recovered(&RecoveryStore::new(&dir)); + assert_eq!(docs.len(), 2, "the same entry, not a new one"); + assert!(docs.values().any(|d| *d == same(newer.clone())) && !docs.values().any(|d| *d == same(a.clone()))); + drop(store); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// #6464 review: a document saved or closed after a flush (the app stayed open because a recovery +/// write failed) still has its entry removed, so it doesn't come back at the next launch. +#[test] +fn discard_after_flush_removes_the_entry() { + let dir = temp_dir("discard-after-flush"); + let mut store = RecoveryStore::new(&dir); + store.autosave_checked(&Arc::new(version(1, 3, ColorMode::Rgb)), 4, None).unwrap(); + assert_eq!(store.flush(), vec![(1, 4, Ok(()))]); + assert_eq!(list_recovery(&dir).len(), 1); + store.discard(1).unwrap(); + assert!(list_recovery(&dir).is_empty(), "the flushed document's entry is gone"); + // And after a retry wrote the entry again through a new worker. + store.autosave_checked(&Arc::new(version(2, 5, ColorMode::Rgb)), 6, None).unwrap(); + store.flush(); + store.autosave_checked(&Arc::new(version(2, 6, ColorMode::Rgb)), 7, None).unwrap(); + store.flush(); + assert_eq!(list_recovery(&dir).len(), 1, "one entry per document"); + store.discard(2).unwrap(); + assert!(list_recovery(&dir).is_empty()); + drop(store); + std::fs::remove_dir_all(dir).unwrap(); +} + #[test] fn new_documents_never_overwrite_surviving_entries() { let dir = temp_dir("collision"); @@ -254,3 +311,54 @@ fn failed_cleanup_after_commit_does_not_fail_the_save() { assert_eq!(load_path(&dir).unwrap(), doc); std::fs::remove_dir_all(dir.parent().unwrap()).unwrap(); } + +/// #9034: while a document's autosave replaces its entry, a listing (another launch's recovery +/// prompt, or the launch's own check after closing a second document) sees that entry exactly +/// once. On Windows the sidecar used to be replaced through `ReplaceFileW`, which moves the old +/// sidecar to a backup name before moving the new one in: a listing in between found the bundle +/// without a sidecar and listed it a second time (or, the other way round, not at all). +#[test] +fn a_listing_during_an_autosave_sees_the_entry_exactly_once() { + let dir = temp_dir("listing-during-autosave"); + let doc = Document::new("Busy", photocraft_doc::Size { width: 4, height: 4 }, ColorMode::Rgb, SampleType::U8); + save_once(&dir, "busy", doc.clone()); + let done = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let writer = { + let (dir, done) = (dir.clone(), done.clone()); + std::thread::spawn(move || { + // Sets `done` however the thread ends, so a failing writer stops the listing loop + // and `join` reports its panic instead of the test hanging. + struct SetOnDrop(Arc); + impl Drop for SetOnDrop { + fn drop(&mut self) { + self.0.store(true, std::sync::atomic::Ordering::SeqCst); + } + } + let _done = SetOnDrop(done); + let saver = Autosaver::new(&dir, "busy"); + for revision in 2..=200 { + saver.request(Arc::new(doc.clone()), revision, None, SaveOptions::default()); + // Wait for this write, so every revision replaces the sidecar once. + let completed = loop { + let completed = saver.take_completed(); + if !completed.is_empty() { + break completed; + } + std::thread::yield_now(); + }; + assert!(completed.iter().all(|(_, result)| result.is_ok()), "revision {revision}: {completed:?}"); + } + saver.flush() + }) + }; + let mut listings = 0u32; + while !done.load(std::sync::atomic::Ordering::SeqCst) { + let entries = list_recovery(&dir); + let keys: Vec<&str> = entries.iter().map(|e| e.info.key.as_str()).collect(); + assert_eq!(keys, ["busy"], "listing {listings} during an autosave: {entries:?}"); + listings += 1; + } + assert!(writer.join().unwrap().is_some_and(|r| r.is_ok())); + assert!(listings > 0); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/format/tests/common/mod.rs b/crates/format/tests/it/common/mod.rs similarity index 97% rename from crates/format/tests/common/mod.rs rename to crates/format/tests/it/common/mod.rs index 8d5641d9cd..c58b542750 100644 --- a/crates/format/tests/common/mod.rs +++ b/crates/format/tests/it/common/mod.rs @@ -66,7 +66,12 @@ pub fn rich_doc(mode: ColorMode, depth: SampleType) -> Document { mask.linked = false; paint.mask = Some(mask); paint.effects.items.push(Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 0.8), color: Color::rgb(1.0, 0.2, 0.1) }); - paint.effects.items.push(Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 0.8), gradient: Gradient::default(), dither: true }); + paint.effects.items.push(Effect::GradientOverlay { + common: FxCommon::new(BlendMode::Normal, 0.8), + gradient: Gradient::default(), + dither: true, + interpolation: None, + }); paint.effects.psd_raw = Some(blob(3, 64)); paint.psd_blocks.push((*b"vmsk", blob(4, 40))); paint.psd_blocks.push((*b"lnsr", blob(5, 4))); @@ -154,6 +159,8 @@ pub fn rich_doc(mode: ColorMode, depth: SampleType) -> Document { live: Some(LiveShape::Rect { rect: [1.0, 2.0, 30.0, 40.0], radii: [4.0, 0.0, 4.0, 0.0] }), cache: Some(cache.clone()), psd_raw: Some(blob(10, 30)), + density: 0.6, + feather: 3.5, }), ); shape.label = LabelColor::Green; @@ -226,7 +233,7 @@ pub fn rich_doc(mode: ColorMode, depth: SampleType) -> Document { d.color_table = Some(photocraft_doc::ColorTable { colors: vec![[0, 0, 0], [255, 128, 0]], transparent: Some(1) }); d.duotone = Some(photocraft_doc::Duotone { inks: vec![photocraft_doc::DuotoneInk::new("Black", [0.0; 3]), photocraft_doc::DuotoneInk::new("PANTONE 151 C", [1.0, 0.5, 0.0])], - psd_raw: Some(vec![1, 2, 3]), + psd_raw: Some(vec![1, 2, 3].into()), }); d.guides = Guides { horizontal: vec![10.0, 20.5], vertical: vec![100.25], colors: vec![GuideColor { vertical: true, position: 100.25, rgb: [0x33, 0x66, 0xff] }] }; diff --git a/crates/format/tests/corruption.rs b/crates/format/tests/it/corruption.rs similarity index 93% rename from crates/format/tests/corruption.rs rename to crates/format/tests/it/corruption.rs index d293abed1e..1a69a60055 100644 --- a/crates/format/tests/corruption.rs +++ b/crates/format/tests/it/corruption.rs @@ -1,6 +1,6 @@ //! Damaged or hostile bundles must fail cleanly, never panic. -mod common; +use crate::common; use common::*; use photocraft_color::{ColorMode, SampleType}; use photocraft_format::*; @@ -94,6 +94,24 @@ fn bad_manifest_json() { assert!(matches!(load_from_bytes(&b), Err(FormatError::Json(_)))); } +/// A number past f32's range would load as infinity, and every later save would then fail +/// (#6862): it is refused when the file is opened, naming the value. +#[test] +fn out_of_range_number_refused_on_open() { + let b = rebuild(&sample(), |n, d| { + Some(if n == "manifest.json" { + let mut v: serde_json::Value = serde_json::from_slice(&d).unwrap(); + v["document"]["layers"][0]["opacity"] = serde_json::json!(1e39); + serde_json::to_vec(&v).unwrap() + } else { + d + }) + }); + let Err(e) = load_from_bytes(&b) else { panic!("a manifest with opacity 1e39 opened") }; + assert!(matches!(e, FormatError::Corrupt(_)), "{e}"); + assert!(e.to_string().contains("document.layers[0].opacity"), "{e}"); +} + /// Hostile nesting fails with an error instead of overflowing the stack; nesting as deep as the /// deepest groups a bundle may hold (three JSON levels each) parses, and is then rejected for /// lacking a format version. @@ -110,6 +128,24 @@ fn over_deep_manifest_fails_cleanly() { assert!(matches!(e, FormatError::Corrupt(_)), "{e}"); } +/// A manifest of millions of tiny values (`[0,0,…]` deflates about 1000:1) is refused before +/// it is parsed into a JSON tree, which would cost about 16 bytes of memory per input byte. +#[test] +fn manifest_with_too_many_values_is_refused() { + let n = (4 << 20) + 16; + let mut m = Vec::with_capacity(2 * n + 1); + m.push(b'['); + for i in 0..n { + m.extend_from_slice(if i == 0 { b"0" } else { b",0" }); + } + m.push(b']'); + let b = rebuild(&sample(), |name, d| Some(if name == "manifest.json" { m.clone() } else { d })); + let e = load_from_bytes(&b).unwrap_err(); + assert!(matches!(e, FormatError::LimitExceeded(_)), "{e}"); + let e = read_manifest(&b).unwrap_err(); + assert!(matches!(e, FormatError::LimitExceeded(_)), "{e}"); +} + #[test] fn too_new_version_rejected() { let b = rebuild(&sample(), |n, d| { diff --git a/crates/format/tests/directory_symlinks.rs b/crates/format/tests/it/directory_symlinks.rs similarity index 93% rename from crates/format/tests/directory_symlinks.rs rename to crates/format/tests/it/directory_symlinks.rs index 0302593f49..0a8d7d2462 100644 --- a/crates/format/tests/directory_symlinks.rs +++ b/crates/format/tests/it/directory_symlinks.rs @@ -1,6 +1,6 @@ //! Directory cleanup only owns content-addressed objects; unrelated files survive. -mod common; +use crate::common; use common::temp_dir; use photocraft_color::{ColorMode, SampleType}; @@ -121,7 +121,7 @@ mod permissions { } #[test] - fn denied_stale_preview_cleanup_preserves_the_previous_manifest() { + fn denied_stale_preview_cleanup_is_retried_after_a_successful_save() { let root = temp_dir("denied-preview-cleanup"); let bundle = root.join("project.pcraft"); let mut writer = PcraftWriter::new(); @@ -139,12 +139,14 @@ mod permissions { return; } let mut changed = empty_doc(); - changed.name = "must not commit".into(); + changed.name = "committed despite housekeeping failure".into(); let result = writer.save_dir(&changed, &bundle, &SaveOptions::default()); drop(restricted); - assert!(result.is_err(), "a required pre-commit cleanup failure is reported"); - assert_eq!(std::fs::read(preview).unwrap(), b"stale composite"); - assert_eq!(load_path(&bundle).unwrap(), original); + result.expect("a stale preview must not turn a committed save into an error"); + assert_eq!(std::fs::read(&preview).unwrap(), b"stale composite"); + assert_eq!(load_path(&bundle).unwrap(), changed); + writer.save_dir(&changed, &bundle, &SaveOptions::default()).unwrap(); + assert!(!preview.exists(), "a later save retries stale-preview cleanup"); std::fs::remove_dir_all(root).unwrap(); } diff --git a/crates/format/tests/it/float_roundtrip.rs b/crates/format/tests/it/float_roundtrip.rs new file mode 100644 index 0000000000..da6e7626d1 --- /dev/null +++ b/crates/format/tests/it/float_roundtrip.rs @@ -0,0 +1,27 @@ +//! `.pcraft` is lossless for f64: every coordinate reloads bit-for-bit. + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent, Size}; + +#[test] +fn f64_coordinates_reload_bit_exact() { + let x = 11.183221215612905_f64; + let y = 0.1 + 0.2; + let mut doc = Document::new("Floats", Size::new(2, 2), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::new( + "Shape", + LayerContent::Shape(photocraft_doc::ShapeLayer { + path: photocraft_doc::Path::new(vec![photocraft_doc::Subpath::polygon(&[(x, y), (40.0, 10.0), (25.0, 40.0)])]), + ..Default::default() + }), + )); + doc.guides.horizontal.push(x); + doc.guides.vertical.push(y); + doc.ruler_origin = [x, y]; + let bytes = photocraft_format::save_to_bytes(&doc, &Default::default()).unwrap(); + let back = photocraft_format::load_from_bytes(&bytes).unwrap(); + assert_eq!(back.guides.horizontal[0].to_bits(), x.to_bits()); + assert_eq!(back.guides.vertical[0].to_bits(), y.to_bits()); + assert_eq!(back.ruler_origin.map(f64::to_bits), [x, y].map(f64::to_bits)); + assert_eq!(back, doc); +} diff --git a/crates/format/tests/it/frame_stroke.rs b/crates/format/tests/it/frame_stroke.rs new file mode 100644 index 0000000000..89567ee7f0 --- /dev/null +++ b/crates/format/tests/it/frame_stroke.rs @@ -0,0 +1,37 @@ +//! A Photoshop Frame's stroke (`Group::frame_stroke`) survives a `.pcraft` round trip; older +//! manifests, written before it existed, load without one (#4381). + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, ShapeStroke, Size, StrokeAlign}; +use photocraft_format::*; + +fn stroke_of(d: &Document) -> Option { + match &d.layers.first()?.content { + LayerContent::Group(g) => g.frame_stroke.clone(), + _ => None, + } +} + +#[test] +fn frame_stroke_roundtrips_and_defaults_when_absent() { + let mut doc = Document::new("a", Size::new(40, 30), ColorMode::Rgb, SampleType::U8); + let stroke = + ShapeStroke { width: 8.0, align: StrokeAlign::Inside, paint: Fill::Solid(Color::rgb(1.0, 0.0, 0.0)), dashes: vec![2.0, 1.0], ..ShapeStroke::default() }; + let mut g = Layer::group("Frame 1", vec![]); + if let LayerContent::Group(gr) = &mut g.content { + gr.frame_stroke = Some(stroke.clone()); + } + doc.layers.push(g); + let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(stroke_of(&load_from_bytes(&bytes).unwrap()), Some(stroke)); + + // A manifest without the key (older files) loads with no stroke. + let m = read_manifest(&bytes).unwrap(); + let mut v = serde_json::to_value(&m.document).unwrap(); + let content = &mut v["layers"][0]["content"]; + assert!(content.get("frame_stroke").is_some()); + content.as_object_mut().unwrap().remove("frame_stroke"); + let old: photocraft_format::manifest::DocM = serde_json::from_value(v).unwrap(); + let photocraft_format::manifest::ContentM::Group { frame_stroke, .. } = &old.layers[0].content else { panic!("not a group") }; + assert!(frame_stroke.is_none()); +} diff --git a/crates/format/tests/layer_labels.rs b/crates/format/tests/it/layer_labels.rs similarity index 100% rename from crates/format/tests/layer_labels.rs rename to crates/format/tests/it/layer_labels.rs diff --git a/crates/format/tests/it/main.rs b/crates/format/tests/it/main.rs new file mode 100644 index 0000000000..71eae66ddd --- /dev/null +++ b/crates/format/tests/it/main.rs @@ -0,0 +1,12 @@ +//! The crate's integration tests, linked into one test binary. + +mod analysis; +mod autosave; +mod common; +mod corruption; +mod directory_symlinks; +mod float_roundtrip; +mod frame_stroke; +mod layer_labels; +mod roundtrip; +mod smart_contents; diff --git a/crates/format/tests/roundtrip.rs b/crates/format/tests/it/roundtrip.rs similarity index 74% rename from crates/format/tests/roundtrip.rs rename to crates/format/tests/it/roundtrip.rs index f78e93947e..32ef503139 100644 --- a/crates/format/tests/roundtrip.rs +++ b/crates/format/tests/it/roundtrip.rs @@ -1,6 +1,6 @@ //! doc → .pcraft → doc equality, incremental saves, ids, previews. -mod common; +use crate::common; use common::*; use photocraft_color::{ColorMode, SampleType}; use photocraft_doc::text::{CharStyle, ParagraphStyle, TextRun}; @@ -11,6 +11,46 @@ use photocraft_raster::Rgba8Image; const MODES: [ColorMode; 4] = [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab]; +#[test] +fn gradient_overlay_interpolation_roundtrips_in_native_documents() { + use photocraft_doc::effects::GradientInterpolation; + use photocraft_doc::{Color, Effect, FxCommon, Gradient}; + + for mode in MODES { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut doc = rich_doc(mode, depth); + let source = GradientInterpolation { + method: "smooth".into(), + smoothness: 73.0, + stops: vec![(0.0, Color::rgb(0.123456, 0.4, 0.9)), (0.4, Color::WHITE), (1.0, Color::BLACK)], + }; + let effect = Effect::GradientOverlay { + common: FxCommon::new(photocraft_doc::BlendMode::Normal, 1.0), + gradient: Gradient::default(), + dither: true, + interpolation: Some(source), + } + .in_mode(mode); + doc.layers.first_mut().unwrap().effects.items.push(effect); + let bytes = save_to_bytes(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(load_from_bytes(&bytes).unwrap(), doc); + } + } +} + +#[test] +fn old_effect_snapshots_need_no_interpolation_record() { + let effect = photocraft_doc::Effect::GradientOverlay { + common: photocraft_doc::FxCommon::new(photocraft_doc::BlendMode::Normal, 1.0), + gradient: photocraft_doc::Gradient::default(), + dither: false, + interpolation: None, + }; + let json = serde_json::to_value(&effect).unwrap(); + assert!(json["GradientOverlay"].get("interpolation").is_none()); + assert_eq!(serde_json::from_value::(json).unwrap(), effect); +} + #[test] fn custom_shape_stroke_roundtrip_keeps_every_style_field() { for mode in MODES { @@ -213,14 +253,38 @@ fn exhausted_document_ids_and_maximal_text_runs_roundtrip() { assert_eq!(text.paragraph_runs().iter().map(|r| r.len).sum::(), text.text.len()); } +#[test] +fn text_run_baseline_roundtrips() { + use photocraft_doc::text::FontBaseline; + let mut doc = Document::new("scripts", photocraft_doc::Size::new(2, 1), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::new( + "Text", + LayerContent::Text(TextLayer { + text: "H2O2".into(), + runs: vec![ + TextRun { len: 1, style: CharStyle::default() }, + TextRun { len: 1, style: CharStyle { baseline: FontBaseline::Subscript, ..Default::default() } }, + TextRun { len: 1, style: CharStyle::default() }, + TextRun { len: 1, style: CharStyle { baseline: FontBaseline::Superscript, ..Default::default() } }, + ], + ..Default::default() + }), + )); + let loaded = load_from_bytes(&save_to_bytes(&doc, &SaveOptions::default()).unwrap()).unwrap(); + let LayerContent::Text(text) = &loaded.layers.last().unwrap().content else { panic!("text layer must roundtrip") }; + let baselines: Vec = text.char_runs().iter().map(|r| r.style.baseline).collect(); + assert_eq!(baselines, [FontBaseline::Normal, FontBaseline::Subscript, FontBaseline::Normal, FontBaseline::Superscript]); +} + #[test] fn zip_incremental_only_new_tiles() { let mut doc = rich_doc(ColorMode::Rgb, SampleType::U16); let mut w = PcraftWriter::new(); - let (_, s1) = w.save_zip(&doc, &SaveOptions::default()).unwrap(); + let (first, s1) = w.save_zip(&doc, &SaveOptions::default()).unwrap(); assert!(s1.tiles_total > 5); assert_eq!(s1.tiles_written, s1.tiles_total); - let (_, s2) = w.save_zip(&doc, &SaveOptions::default()).unwrap(); + let (cached, s2) = w.save_zip(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(first, cached); assert_eq!(s2.tiles_written, 0); assert_eq!(s2.tiles_reused, s2.tiles_total); assert_eq!(s2.blobs_written, 0); @@ -230,6 +294,7 @@ fn zip_incremental_only_new_tiles() { let (bytes, s3) = w.save_zip(&doc, &SaveOptions::default()).unwrap(); assert_eq!(s3.tiles_written, 1); assert_eq!(load_from_bytes(&bytes).unwrap(), doc); + assert_eq!(bytes, PcraftWriter::new().save_zip(&doc, &SaveOptions::default()).unwrap().0); } #[test] @@ -488,6 +553,11 @@ fn vector_fields_roundtrip_and_default_when_absent() { for l in layers.as_array_mut().unwrap() { l.as_object_mut().unwrap().remove("vector_mask"); let c = l["content"].as_object_mut().unwrap(); + // A shape's density and feather (#5846) came later still. + if c.get("kind").and_then(|k| k.as_str()) == Some("shape") { + c.remove("density"); + c.remove("feather"); + } for k in ["path", "stroke", "live"] { c.remove(k); } @@ -500,6 +570,15 @@ fn vector_fields_roundtrip_and_default_when_absent() { let old: photocraft_format::manifest::DocM = serde_json::from_value(v).unwrap(); assert!(old.paths.is_empty() && old.work_path.is_none()); assert!(old.layers.iter().all(|l| l.vector_mask.is_none())); + let shapes: Vec<_> = old + .layers + .iter() + .filter_map(|l| match &l.content { + photocraft_format::manifest::ContentM::Shape { density, feather, .. } => Some((*density, *feather)), + _ => None, + }) + .collect(); + assert!(!shapes.is_empty() && shapes.iter().all(|s| *s == (1.0, 0.0)), "{shapes:?}"); } /// Type kerning (#206): the auto mode and manual kerning survive .pcraft; manifests written @@ -562,6 +641,32 @@ fn path_text_shape_roundtrips() { assert_eq!(shape, Some(TextShape::Path { path, start: 12.5, flip: true })); } +/// Area type (#218): the whole path, ops and fill rule included, survives .pcraft. +#[test] +fn area_text_shape_roundtrips() { + use photocraft_doc::text::TextShape; + use photocraft_doc::vector::{FillRule, Path, PathOp, Subpath}; + let mut doc = rich_doc(ColorMode::Rgb, SampleType::U8); + let mut path = Path::new(vec![ + Subpath::polygon(&[(0.0, 0.0), (200.0, 0.0), (200.0, 120.0), (0.0, 120.0)]), + Subpath::polygon(&[(80.0, 40.0), (120.0, 40.0), (120.0, 80.0)]).with_op(PathOp::Subtract), + ]); + path.fill_rule = FillRule::EvenOdd; + let ids: Vec = doc.walk().into_iter().filter(|(_, _, l)| matches!(l.content, LayerContent::Text(_))).map(|(_, _, l)| l.id).collect(); + assert!(!ids.is_empty()); + for id in ids { + if let Some(LayerContent::Text(t)) = doc.layer_mut(id).map(|l| &mut l.content) { + t.shape = TextShape::Area { path: path.clone() }; + } + } + let back = load_from_bytes(&save_to_bytes(&doc, &SaveOptions::default()).unwrap()).unwrap(); + let shape = back.walk().into_iter().find_map(|(_, _, l)| match &l.content { + LayerContent::Text(t) => Some(t.shape.clone()), + _ => None, + }); + assert_eq!(shape, Some(TextShape::Area { path })); +} + #[test] fn channel_restrictions_and_bevel_elements_roundtrip() { use photocraft_doc::{Bevel, BevelContour, BevelTexture, Contour, Effect}; @@ -579,6 +684,7 @@ fn channel_restrictions_and_bevel_elements_roundtrip() { altitude: 40.0, use_global_light: false, gloss_contour: Contour::Linear, + gloss_anti_alias: false, highlight: photocraft_doc::FxCommon::new(photocraft_color::BlendMode::Screen, 0.7), highlight_color: photocraft_color::Color::WHITE, shadow: photocraft_doc::FxCommon::new(photocraft_color::BlendMode::Multiply, 0.6), @@ -730,6 +836,34 @@ fn a_non_finite_float_is_refused_at_save_rather_than_breaking_every_later_load() assert_eq!(back, doc); } +/// #4957: a directory save holds the bundle's lock for its whole transaction. While another +/// writer holds it, a save waits, then fails without writing anything; once it is released the +/// save goes ahead. +#[test] +fn directory_save_waits_for_the_bundle_lock() { + let doc = rich_doc(ColorMode::Rgb, SampleType::U8); + let dir = temp_dir("lock"); + let other = std::fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).open(dir.join("save.lock")).unwrap(); + // A file system without file locks saves unlocked (`lock_bundle`), so there's nothing to test. + match other.try_lock() { + Ok(()) => {} + Err(std::fs::TryLockError::Error(e)) if e.kind() == std::io::ErrorKind::Unsupported => return, + Err(std::fs::TryLockError::Error(e)) => panic!("locking save.lock failed: {e}"), + Err(std::fs::TryLockError::WouldBlock) => panic!("save.lock is already locked"), + } + let mut w = PcraftWriter::new(); + w.set_lock_wait(std::time::Duration::from_millis(100)); + let e = w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap_err(); + assert!(e.to_string().contains("in progress"), "{e}"); + assert!(!dir.join("manifest.json").exists(), "nothing written while locked"); + assert!(!dir.join("tiles").exists() || std::fs::read_dir(dir.join("tiles")).unwrap().count() == 0); + other.unlock().unwrap(); + w.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert_eq!(load_path(&dir).unwrap(), doc); + drop(other); + std::fs::remove_dir_all(dir).unwrap(); +} + #[test] fn variables_follow_layer_id_remapping() { let mut doc = rich_doc(ColorMode::Rgb, SampleType::U8); @@ -751,3 +885,93 @@ fn variables_follow_layer_id_remapping() { let gone = defs.iter().find(|d| d.name == "gone").unwrap().layer; assert!(back.walk().into_iter().all(|(_, _, l)| l.id != gone), "dangling target must not address a live layer"); } + +#[test] +fn color_lookup_lut_file_round_trips_and_old_files_load() { + use photocraft_doc::Adjustment; + use photocraft_doc::adjust::{LutFormat, LutSource}; + let lut = Some(std::sync::Arc::new(vec![0.5f32; 24])); + let source = Some(LutSource { format: LutFormat::ThreeDl, bytes: std::sync::Arc::new(b"0 1023\n0 0 0\n".to_vec()) }); + let adj = Adjustment::ColorLookup { name: "Film.3dl".into(), lut, size: 2, tetrahedral: false, dither: false, source }; + let mut doc = Document::new("d", photocraft_doc::Size::new(4, 4), ColorMode::Rgb, SampleType::U8); + doc.insert_above(None, Layer::new("lookup", LayerContent::Adjustment(adj.clone()))); + let back = load_from_bytes(&save_to_bytes(&doc, &SaveOptions::default()).unwrap()).unwrap(); + assert_eq!(back, doc, "the LUT file survives a .pcraft round trip (#6544)"); + // A .pcraft written before the field existed has no `source`: it loads as None. + let mut v = serde_json::to_value(&adj).unwrap(); + let removed = v.as_object_mut().and_then(|o| o.values_mut().next()).and_then(|f| f.as_object_mut()).and_then(|f| f.remove("source")); + assert!(removed.is_some(), "{v}"); + let Adjustment::ColorLookup { source: None, name, .. } = serde_json::from_value(v).unwrap() else { panic!("old lookup must load") }; + assert_eq!(name, "Film.3dl"); +} + +/// Removing previews is post-commit housekeeping, not part of publishing the new document. +#[test] +fn directory_save_without_previews_removes_stale_files_after_commit() { + let mut doc = Document::new("before", photocraft_doc::Size::new(4, 3), ColorMode::Rgb, SampleType::U8); + let dir = temp_dir("preview-cleanup"); + let mut writer = PcraftWriter::new(); + let opts = SaveOptions { thumbnail: Some(Rgba8Image::new(2, 2)), composite: Some(Rgba8Image::new(4, 3)) }; + writer.save_dir(&doc, &dir, &opts).unwrap(); + doc.name = "after".into(); + writer.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert_eq!(load_path(&dir).unwrap(), doc); + assert!(!dir.join("thumb.png").exists()); + assert!(!dir.join("composite/preview.png").exists()); + std::fs::remove_dir_all(dir).unwrap(); +} + +/// A real Windows handle denying delete-sharing blocks manifest replacement after preparation. +#[cfg(windows)] +#[test] +fn failed_manifest_commit_preserves_omitted_previews() { + use std::os::windows::fs::OpenOptionsExt; + + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut doc = Document::with_background("before", photocraft_doc::Size::new(4, 3), ColorMode::Rgb, depth, photocraft_color::Color::WHITE); + let dir = temp_dir("locked-manifest-previews"); + let mut writer = PcraftWriter::new(); + let opts = SaveOptions { thumbnail: Some(Rgba8Image::new(2, 2)), composite: Some(Rgba8Image::new(4, 3)) }; + writer.save_dir(&doc, &dir, &opts).unwrap(); + let old_doc = doc.clone(); + let paths = ["manifest.json", "thumb.png", "composite/preview.png"]; + let before: Vec<_> = paths.iter().map(|p| std::fs::read(dir.join(p)).unwrap()).collect(); + // FILE_SHARE_READ | FILE_SHARE_WRITE; deliberately omit FILE_SHARE_DELETE. + let held = std::fs::OpenOptions::new().read(true).share_mode(3).open(dir.join("manifest.json")).unwrap(); + doc.name = "after".into(); + let error = writer.save_dir(&doc, &dir, &SaveOptions::default()).unwrap_err(); + assert!(matches!(error, FormatError::Io(_)), "expected a real commit I/O failure: {error}"); + for (path, bytes) in paths.iter().zip(&before) { + assert_eq!(std::fs::read(dir.join(path)).ok().as_ref(), Some(bytes), "saved file lost after failed commit: {path}"); + } + assert_eq!(load_path(&dir).unwrap(), old_doc); + drop(held); + writer.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert_eq!(load_path(&dir).unwrap(), doc); + assert!(!dir.join("thumb.png").exists()); + assert!(!dir.join("composite/preview.png").exists()); + std::fs::remove_dir_all(dir).unwrap(); + } +} + +#[cfg(windows)] +#[test] +fn locked_stale_preview_does_not_fail_committed_save() { + use std::os::windows::fs::OpenOptionsExt; + + let mut doc = Document::new("before", photocraft_doc::Size::new(4, 3), ColorMode::Rgb, SampleType::U8); + let dir = temp_dir("locked-stale-preview"); + let mut writer = PcraftWriter::new(); + let opts = SaveOptions { thumbnail: Some(Rgba8Image::new(2, 2)), composite: Some(Rgba8Image::new(4, 3)) }; + writer.save_dir(&doc, &dir, &opts).unwrap(); + let held = std::fs::OpenOptions::new().read(true).share_mode(3).open(dir.join("thumb.png")).unwrap(); + doc.name = "after".into(); + writer.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert_eq!(load_path(&dir).unwrap(), doc); + assert!(dir.join("thumb.png").exists(), "locked leftover remains until a later cleanup"); + assert!(!dir.join("composite/preview.png").exists()); + drop(held); + writer.save_dir(&doc, &dir, &SaveOptions::default()).unwrap(); + assert!(!dir.join("thumb.png").exists()); + std::fs::remove_dir_all(dir).unwrap(); +} diff --git a/crates/format/tests/smart_contents.rs b/crates/format/tests/it/smart_contents.rs similarity index 100% rename from crates/format/tests/smart_contents.rs rename to crates/format/tests/it/smart_contents.rs diff --git a/crates/geom/src/lib.rs b/crates/geom/src/lib.rs index d95e4b847f..44a078dd36 100644 --- a/crates/geom/src/lib.rs +++ b/crates/geom/src/lib.rs @@ -96,6 +96,14 @@ impl Rect { pub fn inflate(&self, d: i32) -> Rect { Rect::new(self.x0.saturating_sub(d), self.y0.saturating_sub(d), self.x1.saturating_add(d), self.y1.saturating_add(d)) } + /// How many tiles overlap this rect, without visiting them (a rect can span billions). + pub fn tile_count(&self) -> u64 { + if self.is_empty() { + return 0; + } + let (a, b) = (TileCoord::containing(self.x0, self.y0), TileCoord::containing(self.x1 - 1, self.y1 - 1)); + ((i64::from(b.tx) - i64::from(a.tx) + 1) as u64) * ((i64::from(b.ty) - i64::from(a.ty) + 1) as u64) + } /// All tiles overlapping this rect. pub fn tiles(&self) -> impl Iterator + use<> { let (a, b) = if self.is_empty() { @@ -181,14 +189,16 @@ impl Affine { pub fn determinant(&self) -> f64 { self.m[0] * self.m[3] - self.m[1] * self.m[2] } + /// Returns `None` for a near-singular matrix or non-finite determinant/inverse coefficients. pub fn inverse(&self) -> Option { let det = self.determinant(); - if det.abs() < 1e-12 { + if !det.is_finite() || det.abs() < 1e-12 { return None; } let [a, b, c, d, e, f] = self.m; let inv = 1.0 / det; - Some(Affine { m: [d * inv, -b * inv, -c * inv, a * inv, (c * f - d * e) * inv, (b * e - a * f) * inv] }) + let inverse = Affine { m: [d * inv, -b * inv, -c * inv, a * inv, (c * f - d * e) * inv, (b * e - a * f) * inv] }; + inverse.m.iter().all(|v| v.is_finite()).then_some(inverse) } } @@ -245,6 +255,34 @@ mod tests { assert!(Affine::scale(0.0).inverse().is_none()); } + #[test] + fn affine_inverse_rejects_non_finite_coefficients() { + for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + for index in 0..6 { + let mut transform = Affine::IDENTITY; + transform.m[index] = value; + assert!(transform.inverse().is_none(), "coefficient {index}: {value}"); + } + } + } + + #[test] + fn affine_inverse_rejects_determinant_overflow() { + assert!(Affine::scale(f64::MAX).inverse().is_none()); + } + + #[test] + fn affine_inverse_rejects_translation_overflow() { + let transform = Affine { m: [1.0, 0.0, 1.0, 1.0, -f64::MAX, f64::MAX] }; + assert_eq!(transform.determinant(), 1.0); + assert!(transform.inverse().is_none()); + } + + #[test] + fn affine_inverse_keeps_large_finite_translations() { + assert_eq!(Affine::translate(f64::MAX, -f64::MAX).inverse(), Some(Affine::translate(-f64::MAX, f64::MAX))); + } + #[test] fn affine_then_order() { let t = Affine::translate(10.0, 0.0).then(&Affine::scale(2.0)); diff --git a/crates/geom/src/warp.rs b/crates/geom/src/warp.rs index 9c8f138747..8c1320cf6a 100644 --- a/crates/geom/src/warp.rs +++ b/crates/geom/src/warp.rs @@ -9,6 +9,9 @@ //! - **Custom meshes** ([`BezierMesh`]): a grid of bicubic Bezier patches. The default is one //! patch (a 4×4 control grid); split warps add patch rows/columns by exact de Casteljau //! subdivision, so splitting never changes the shape. +//! - **Cylinder** ([`WarpStyle::Cylinder`], Transform Warp only): seven `warpValues` that +//! Photoshop turns into a 4 × 1 patch mesh wrapping the contents round a cylinder; see +//! [`cylinder_mesh`]. //! //! Pure geometry: resampling lives in `photocraft-algo::warp`. @@ -38,6 +41,10 @@ pub enum WarpStyle { Inflate, Squeeze, Twist, + /// Edit › Transform › Warp's Cylinder (`warpCylinder`): not one of the fifteen bend presets + /// (Warp Text doesn't have it), it takes seven values instead of a bend; see + /// [`Warp::cylinder`]. + Cylinder, } impl WarpStyle { @@ -60,7 +67,8 @@ impl WarpStyle { WarpStyle::Twist, ]; - /// Every style (none, custom, presets). + /// The styles the Warp menus list: none, custom and the presets. [`WarpStyle::Cylinder`] is + /// read from PSDs and taken by commands, but has no editor yet. pub fn all() -> impl Iterator { [WarpStyle::None, WarpStyle::Custom].into_iter().chain(Self::PRESETS) } @@ -85,6 +93,7 @@ impl WarpStyle { WarpStyle::Inflate => "warpInflate", WarpStyle::Squeeze => "warpSqueeze", WarpStyle::Twist => "warpTwist", + WarpStyle::Cylinder => "warpCylinder", } } @@ -109,6 +118,7 @@ impl WarpStyle { "Fisheye" => "fisheye", "Inflate" => "inflate", "Squeeze" => "squeeze", + "Cylinder" => "cylinder", _ => "twist", } } @@ -133,17 +143,19 @@ impl WarpStyle { WarpStyle::Inflate => "Inflate", WarpStyle::Squeeze => "Squeeze", WarpStyle::Twist => "Twist", + WarpStyle::Cylinder => "Cylinder", } } /// Parses a short id, a PSD name or a label (case-insensitive, spaces ignored). pub fn parse(s: &str) -> Option { let k: String = s.chars().filter(|c| !c.is_whitespace()).collect::().to_ascii_lowercase(); - Self::all().find(|w| w.id().to_ascii_lowercase() == k || w.psd_name().to_ascii_lowercase() == k) + Self::all().chain([WarpStyle::Cylinder]).find(|w| w.id().to_ascii_lowercase() == k || w.psd_name().to_ascii_lowercase() == k) } + /// One of the fifteen bend presets ([`Self::PRESETS`]). pub fn is_preset(self) -> bool { - !matches!(self, WarpStyle::None | WarpStyle::Custom) + Self::PRESETS.contains(&self) } } @@ -357,7 +369,7 @@ fn preset_grid(style: WarpStyle, b: f64, hd: f64, vd: f64, vertical: bool, aspec g[2][2][1] -= a; } } - WarpStyle::None | WarpStyle::Custom => {} + WarpStyle::None | WarpStyle::Custom | WarpStyle::Cylinder => {} } // Vertical distortion: the top and bottom rows scale about their end points' midpoint (by // 1 ∓ vd) and the middle rows follow by the blend of those two moves. @@ -412,6 +424,101 @@ fn split_cubic(p: [[f64; 2]; 4], t: f64) -> [[f64; 2]; 7] { [p[0], a, d, f, e, c, p[3]] } +/// Photoshop's Cylinder warp as a Bezier mesh in the `warp` descriptor's value space (the space +/// of its `bounds`), from the seven `warpValues` `v` of a horizontal cylinder with no +/// perspective. Like a custom warp's mesh, its control-point box is what the placement quad +/// frames. `None` unless there are seven finite values with the right silhouette right of the +/// left one. +/// +/// The model was measured against Photoshop 2026's renders of a UV grid (issue #4112: within +/// 0.3 px vertically and 1.2 px horizontally at the 99th percentile), not taken from Adobe's code: +/// - `v[0]`, `v[2]`: the left and right silhouettes. Half axis `a = (v[2] − v[0]) / 2`. +/// - `v[3]`, `v[1]`: the top and bottom edges at the silhouettes (the centre lines of the upper +/// and lower ellipses); `v[4]`, `v[5]`: those ellipses' depth in units of `a`. +/// - `v[6]` (0..1): how far the contents wrap round. The outer `uL` of the contents on each side +/// is on the back of the cylinder and collapses onto the silhouette: 1/6 at 0, 0.006 at 1. +/// +/// Four patches across (`us = [0, uL, 0.5, 1 − uL, 1]`: back, front left, front right, back) and +/// one down; control rows are linear between the top and bottom edge. +pub fn cylinder_mesh(v: &[f64]) -> Option { + let v: [f64; 7] = v.try_into().ok()?; + if !v.iter().all(|x| x.is_finite()) || v[2] <= v[0] { + return None; + } + let a = (v[2] - v[0]) / 2.0; + let cx = (v[0] + v[2]) / 2.0; + let [ul, kx, ky, my] = cylinder_shape(v[6]); + let s = [-1.0, -1.0, -1.0, -1.0, -1.0, -kx, 0.0, kx, 1.0, 1.0, 1.0, 1.0, 1.0]; + let e = [0.0, 0.0, 0.0, 0.0, ky, my, 1.0, my, ky, 0.0, 0.0, 0.0, 0.0]; + let mut points = Vec::with_capacity(13 * 4); + for r in 0..4 { + let t = f64::from(r) / 3.0; + for (sk, ek) in s.iter().zip(e) { + let (top, bottom) = (v[3] + v[4] * a * ek, v[1] + v[5] * a * ek); + points.push([cx + a * sk, top + (bottom - top) * t]); + } + } + let mesh = BezierMesh { us: vec![0.0, ul, 0.5, 1.0 - ul, 1.0], vs: vec![0.0, 1.0], points }; + mesh.is_valid().then_some(mesh) +} + +/// `[uL, kx, ky, my]` for a cylinder's wrap `g` (`warpValues[6]`, clamped to 0..1): the share of +/// the contents on the back on each side, then the front patches' inner x control point (in +/// units of the half axis) and their ellipse factors. Linear between the measured rows. +fn cylinder_shape(g: f64) -> [f64; 4] { + let n = CYLINDER_SHAPE.len() - 1; + let x = if g.is_finite() { g.clamp(0.0, 1.0) * n as f64 } else { 0.0 }; + let i = (x.floor() as usize).min(n - 1); + let (a, b, t) = (CYLINDER_SHAPE[i], CYLINDER_SHAPE[i + 1], x - i as f64); + std::array::from_fn(|k| a[k] + (b[k] - a[k]) * t) +} + +/// [`cylinder_shape`]'s rows for `g` = 0, 0.025 … 1, measured from Photoshop's renders (#4112): +/// no closed form was found. +const CYLINDER_SHAPE: [[f64; 4]; 41] = [ + [0.166169, 0.655404, 0.382374, 1.003104], + [0.157304, 0.650228, 0.388326, 1.002685], + [0.148438, 0.645374, 0.394211, 1.007918], + [0.140060, 0.639652, 0.401415, 1.006938], + [0.131885, 0.634064, 0.408465, 1.006069], + [0.123875, 0.628845, 0.414586, 1.005743], + [0.116162, 0.623565, 0.421033, 1.005233], + [0.108674, 0.618563, 0.426905, 1.004990], + [0.101198, 0.614439, 0.431943, 1.010787], + [0.094322, 0.609611, 0.437749, 1.010560], + [0.087757, 0.604929, 0.443406, 1.010361], + [0.081514, 0.600390, 0.448907, 1.010190], + [0.075599, 0.596012, 0.454288, 1.010019], + [0.069994, 0.591836, 0.459420, 1.009888], + [0.064709, 0.587844, 0.464361, 1.009768], + [0.059740, 0.584026, 0.469109, 1.009669], + [0.055072, 0.580427, 0.473609, 1.009588], + [0.050712, 0.576990, 0.477904, 1.009528], + [0.046635, 0.573760, 0.481968, 1.009477], + [0.042824, 0.570758, 0.485764, 1.009440], + [0.039295, 0.567910, 0.489377, 1.009411], + [0.036023, 0.565229, 0.492800, 1.009382], + [0.032681, 0.563478, 0.495519, 1.015285], + [0.029877, 0.561152, 0.498513, 1.015270], + [0.027282, 0.559012, 0.501313, 1.015245], + [0.024894, 0.557004, 0.503913, 1.015241], + [0.022693, 0.555163, 0.506331, 1.015225], + [0.020668, 0.553463, 0.508575, 1.015213], + [0.018816, 0.551877, 0.510651, 1.015217], + [0.017106, 0.550434, 0.512556, 1.015214], + [0.015542, 0.549100, 0.514319, 1.015213], + [0.014115, 0.547868, 0.515936, 1.015223], + [0.012806, 0.546743, 0.517432, 1.015223], + [0.011600, 0.545730, 0.518775, 1.015228], + [0.010506, 0.544788, 0.520034, 1.015230], + [0.009510, 0.543920, 0.521179, 1.015238], + [0.008600, 0.543134, 0.522224, 1.015244], + [0.007763, 0.542420, 0.523173, 1.015251], + [0.007006, 0.541757, 0.524065, 1.015252], + [0.006315, 0.541164, 0.524867, 1.015252], + [0.005686, 0.540608, 0.525602, 1.015261], +]; + /// Inverse of the Bernstein interpolation matrix at t = 0, 1/3, 2/3, 1 (rows: control point, /// columns: sample). Exact rational values. const BERN_INV: [[f64; 4]; 4] = [[1.0, 0.0, 0.0, 0.0], [-5.0 / 6.0, 3.0, -1.5, 1.0 / 3.0], [1.0 / 3.0, -1.5, 3.0, -5.0 / 6.0], [0.0, 0.0, 0.0, 1.0]]; @@ -474,38 +581,50 @@ impl BezierMesh { /// Control points per row. pub fn nx(&self) -> usize { - 3 * (self.us.len() - 1) + 1 + 3usize.saturating_mul(self.us.len().saturating_sub(1)).saturating_add(1) } /// Control point rows. pub fn ny(&self) -> usize { - 3 * (self.vs.len() - 1) + 1 + 3usize.saturating_mul(self.vs.len().saturating_sub(1)).saturating_add(1) } pub fn point(&self, i: usize, j: usize) -> [f64; 2] { - self.points[j * self.nx() + i] + self.points.get(j.saturating_mul(self.nx()).saturating_add(i)).copied().unwrap_or([0.0; 2]) } /// Checks the invariants (knots and point count); malformed data from files is rejected. pub fn is_valid(&self) -> bool { - let knots_ok = |k: &[f64]| k.len() >= 2 && k[0] == 0.0 && k[k.len() - 1] == 1.0 && k.windows(2).all(|w| w[1] > w[0]); - knots_ok(&self.us) - && knots_ok(&self.vs) - && self.points.len() == self.nx() * self.ny() - && self.points.iter().all(|p| p[0].is_finite() && p[1].is_finite()) + let knots_ok = |k: &[f64]| (2..=65).contains(&k.len()) && k[0] == 0.0 && k[k.len() - 1] == 1.0 && k.windows(2).all(|w| w[1] > w[0]); + if !knots_ok(&self.us) || !knots_ok(&self.vs) { + return false; + } + let Some(nx) = (self.us.len() - 1).checked_mul(3).and_then(|v| v.checked_add(1)) else { + return false; + }; + let Some(ny) = (self.vs.len() - 1).checked_mul(3).and_then(|v| v.checked_add(1)) else { + return false; + }; + let Some(expected) = nx.checked_mul(ny) else { + return false; + }; + self.points.len() == expected && self.points.iter().all(|p| p[0].is_finite() && p[1].is_finite()) } fn locate(knots: &[f64], s: f64) -> (usize, f64) { - let n = knots.len() - 1; - let s = s.clamp(0.0, 1.0); - let mut i = 0; - while i + 1 < n && s > knots[i + 1] { - i += 1; + if knots.len() < 2 { + return (0, 0.0); } + let n = knots.len() - 1; + let s = if s.is_finite() { s.clamp(0.0, 1.0) } else { 0.0 }; + let i = knots[1..n].partition_point(|&k| k < s); let w = knots[i + 1] - knots[i]; (i, if w > 0.0 { (s - knots[i]) / w } else { 0.0 }) } /// Evaluates the surface at normalised `(s, t)`. pub fn eval(&self, s: f64, t: f64) -> [f64; 2] { + if self.us.len() < 2 || self.vs.len() < 2 { + return [0.0; 2]; + } let (pi, a) = Self::locate(&self.us, s); let (pj, b) = Self::locate(&self.vs, t); let (ba, bb) = (bernstein(a), bernstein(b)); @@ -513,7 +632,10 @@ impl BezierMesh { let mut out = [0.0; 2]; for (j, wb) in bb.iter().enumerate() { for (i, wa) in ba.iter().enumerate() { - let p = self.points[(3 * pj + j) * nx + 3 * pi + i]; + let idx = (3 * pj + j).saturating_mul(nx).saturating_add(3 * pi + i); + let Some(&p) = self.points.get(idx) else { + return [0.0; 2]; + }; let k = wa * wb; out[0] += p[0] * k; out[1] += p[1] * k; @@ -555,7 +677,7 @@ impl BezierMesh { /// Exact when the patches came from an unedited split; otherwise the outer control points are /// kept and the inner handles extrapolated. pub fn remove_split_u(&mut self, k: usize) -> bool { - if k == 0 || k + 1 >= self.us.len() { + if k == 0 || k >= self.us.len().saturating_sub(1) { return false; } let a = (self.us[k] - self.us[k - 1]) / (self.us[k + 1] - self.us[k - 1]); @@ -714,15 +836,8 @@ impl BezierMesh { *f = f.max((1.0 - interior) * w); } // Every control point moves with the anchor it is closest to. - for j in 0..self.ny() { - for i in 0..nx { - let (ai, aj) = (((i + 1) / 3).min(cols), ((j + 1) / 3).min(rows)); - let k = share[aj * (cols + 1) + ai]; - let p = &mut self.points[j * nx + i]; - p[0] += d[0] * k; - p[1] += d[1] * k; - } - } + let ny = self.ny(); + move_control_points(&mut self.points, nx, ny, cols, rows, &share, d); // Close the gap exactly, with three groups of control points that share the work: // - the four inner points of the grabbed patch (all of it in the middle of the patch, // little near a grid line, where they barely reach the surface); @@ -900,6 +1015,18 @@ impl BezierMesh { } } +fn move_control_points(points: &mut [[f64; 2]], nx: usize, ny: usize, cols: usize, rows: usize, shares: &[f64], delta: [f64; 2]) { + for j in 0..ny { + for i in 0..nx { + let (ai, aj) = (((i + 1) / 3).min(cols), ((j + 1) / 3).min(rows)); + let share = shares[aj * (cols + 1) + ai]; + let point = &mut points[j * nx + i]; + point[0] += delta[0] * share; + point[1] += delta[1] * share; + } + } +} + /// A complete warp: what Photoshop's `warp` descriptor stores, plus the custom mesh. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -917,15 +1044,24 @@ pub struct Warp { pub vertical: bool, /// The source box `[x0, y0, x1, y1]` the warp is defined over. pub bounds: [f64; 4], - /// Control mesh for [`WarpStyle::Custom`] (output coordinates). + /// Control mesh for [`WarpStyle::Custom`] and [`WarpStyle::Cylinder`] (output coordinates). #[serde(default)] pub mesh: Option, + /// A cylinder's seven `warpValues` (see [`Warp::cylinder`]); empty for other styles. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub values: Vec, +} + +/// Relative to the magnitude so the fit's rounding at large origins isn't rejected. +fn point_fits(expected: [f64; 2], actual: [f64; 2]) -> bool { + let tol = |e: f64| (1e-9 * e.abs().max(1.0)).min(1e-6); + !((actual[0] - expected[0]).abs() >= tol(expected[0]) || (actual[1] - expected[1]).abs() >= tol(expected[1])) } impl Warp { /// No warp over `bounds`. pub fn none(bounds: [f64; 4]) -> Warp { - Warp { style: WarpStyle::None, bend: 0.0, h_distort: 0.0, v_distort: 0.0, vertical: false, bounds, mesh: None } + Warp { style: WarpStyle::None, bend: 0.0, h_distort: 0.0, v_distort: 0.0, vertical: false, bounds, mesh: None, values: Vec::new() } } /// A preset style. @@ -938,6 +1074,13 @@ impl Warp { Warp { style: WarpStyle::Custom, mesh: Some(mesh), ..Warp::none(bounds) } } + /// A horizontal Cylinder warp over `bounds` from its seven `warpValues` (see + /// [`cylinder_mesh`]). `None` when they don't describe one. + pub fn cylinder(values: &[f64], bounds: [f64; 4]) -> Option { + let mesh = cylinder_mesh(values)?; + Some(Warp { style: WarpStyle::Cylinder, mesh: Some(mesh), values: values.to_vec(), ..Warp::none(bounds) }) + } + fn style_warp(&self) -> Option { StyleWarp::new(self.style, self.bend, self.h_distort, self.v_distort, self.vertical, self.bounds) } @@ -946,17 +1089,55 @@ impl Warp { pub fn is_identity(&self) -> bool { match self.style { WarpStyle::None => true, - WarpStyle::Custom => match &self.mesh { + WarpStyle::Custom | WarpStyle::Cylinder => match &self.mesh { None => true, - Some(m) => { - let id = BezierMesh::fit(&|s, t| self.box_point(s, t), m.us.clone(), m.vs.clone()); - m.points.iter().zip(&id.points).all(|(a, b)| (a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9) - } + Some(m) => self.mesh_is_box_fit(m), }, _ => self.style_warp().is_none(), } } + /// True when every control point of `m` sits where the box's own grid puts it. + fn mesh_is_box_fit(&self, m: &BezierMesh) -> bool { + if !m.is_valid() { + return false; + } + let (pc, pr) = (m.us.len() - 1, m.vs.len() - 1); + for pj in 0..pr { + for j in 0..4 { + if !self.mesh_row_is_box_fit(m, pj, j, pc) { + return false; + } + } + } + true + } + + /// One sample row (`j` within patch row `pj`) of [`Self::mesh_is_box_fit`]. + fn mesh_row_is_box_fit(&self, m: &BezierMesh, pj: usize, j: usize, pc: usize) -> bool { + let nx = m.nx(); + let (Some(&v0), Some(&v1)) = (m.vs.get(pj), m.vs.get(pj + 1)) else { + return false; + }; + let v = v0 + (v1 - v0) * j as f64 / 3.0; + for pi in 0..pc { + let (Some(&u0), Some(&u1)) = (m.us.get(pi), m.us.get(pi + 1)) else { + return false; + }; + for i in 0..4 { + let u = u0 + (u1 - u0) * i as f64 / 3.0; + let expected = self.box_point(u, v); + let Some(&actual) = m.points.get((3 * pj + j) * nx + 3 * pi + i) else { + return false; + }; + if !point_fits(expected, actual) { + return false; + } + } + } + true + } + fn box_point(&self, s: f64, t: f64) -> [f64; 2] { let b = self.bounds; [b[0] + s * (b[2] - b[0]), b[1] + t * (b[3] - b[1])] @@ -966,7 +1147,7 @@ impl Warp { pub fn map(&self, x: f64, y: f64) -> (f64, f64) { match self.style { WarpStyle::None => (x, y), - WarpStyle::Custom => match &self.mesh { + WarpStyle::Custom | WarpStyle::Cylinder => match &self.mesh { Some(m) => { let b = self.bounds; let (w, h) = (b[2] - b[0], b[3] - b[1]); @@ -986,9 +1167,9 @@ impl Warp { } /// The warp as a control mesh (a preset is its own Bezier patch split into `cols × rows`; - /// custom warps return their own mesh). + /// custom and cylinder warps return their own mesh). pub fn to_mesh(&self, cols: usize, rows: usize) -> BezierMesh { - if let (WarpStyle::Custom, Some(m)) = (self.style, &self.mesh) { + if let (WarpStyle::Custom | WarpStyle::Cylinder, Some(m)) = (self.style, &self.mesh) { return m.clone(); } let (cols, rows) = (cols.clamp(1, 64), rows.clamp(1, 64)); @@ -1039,329 +1220,4 @@ impl Warp { } #[cfg(test)] -mod tests { - use super::*; - - const B: [f64; 4] = [10.0, 20.0, 110.0, 70.0]; - - /// The largest mismatch between the tangents on the two sides of any interior grid line. - fn tangent_defect(m: &BezierMesh) -> f64 { - let nx = m.nx(); - let mut worst: f64 = 0.0; - for a in 1..m.us.len() - 1 { - let (hl, hr) = (m.us[a] - m.us[a - 1], m.us[a + 1] - m.us[a]); - for j in 0..m.ny() { - let (l, c, r) = (m.points[j * nx + 3 * a - 1], m.points[j * nx + 3 * a], m.points[j * nx + 3 * a + 1]); - for k in 0..2 { - worst = worst.max(((c[k] - l[k]) / hl - (r[k] - c[k]) / hr).abs()); - } - } - } - for b in 1..m.vs.len() - 1 { - let (hl, hr) = (m.vs[b] - m.vs[b - 1], m.vs[b + 1] - m.vs[b]); - for i in 0..nx { - let (l, c, r) = (m.points[(3 * b - 1) * nx + i], m.points[3 * b * nx + i], m.points[(3 * b + 1) * nx + i]); - for k in 0..2 { - worst = worst.max(((c[k] - l[k]) / hl - (r[k] - c[k]) / hr).abs()); - } - } - } - worst - } - - #[test] - fn dragging_an_anchor_or_handle_keeps_the_surface_smooth_across_the_lines() { - let mut base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 1, 1); - for s in [0.3, 0.55, 0.9] { - assert!(base.split_u(s)); - assert!(base.split_v(s)); - } - let nx = base.nx(); - // Every kind of point on a grid line, interior and border: anchors, handles each way. - for (i, j) in [(6, 6), (7, 6), (5, 6), (6, 7), (6, 5), (3, 9), (9, 3), (0, 6), (6, 0), (4, 3), (3, 4), (12, 12)] { - let mut m = base.clone(); - m.drag_point(&base.points, j * nx + i, [23.0, -31.0]); - assert_eq!(m.points[j * nx + i], [base.points[j * nx + i][0] + 23.0, base.points[j * nx + i][1] - 31.0], "({i}, {j}) lands on the pointer"); - assert!(tangent_defect(&m) < 1e-6, "({i}, {j}): {}", tangent_defect(&m)); - } - } - - /// Fraction of the surface where the map folds over itself (its Jacobian is not positive). - fn fold_fraction(m: &BezierMesh) -> f64 { - let (n, h) = (80, 1e-4); - let mut bad = 0; - for j in 0..n { - for i in 0..n { - let (s, t) = ((i as f64 + 0.5) / n as f64, (j as f64 + 0.5) / n as f64); - let q = m.eval(s, t); - let (qs, qt) = (m.eval(s + h, t), m.eval(s, t + h)); - if (qs[0] - q[0]) * (qt[1] - q[1]) - (qt[0] - q[0]) * (qs[1] - q[1]) <= 0.0 { - bad += 1; - } - } - } - bad as f64 / (n * n) as f64 - } - - #[test] - fn a_pull_within_a_quarter_cell_never_folds_the_surface() { - // 75 px cells: dragging a quarter of a cell, any direction, anywhere in the grid must not tear the picture. - let base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 4, 4); - for (s, t) in [(0.42, 0.42), (0.75, 0.2), (0.2, 0.2), (0.5, 0.5), (0.85, 0.7), (0.3, 0.7)] { - for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0), (0.7, 0.7), (-0.7, 0.7)] { - let mut m = base.clone(); - m.pull(&base.points, s, t, [dx * 19.0, dy * 19.0]); - assert_eq!(fold_fraction(&m), 0.0, "({s}, {t}) by ({dx}, {dy}) quarter cells"); - } - } - } - - #[test] - fn a_pull_in_the_middle_leaves_the_border_where_it_is() { - let mut base = BezierMesh::identity([0.0, 0.0, 600.0, 800.0], 1, 1); - for s in [0.33, 0.66] { - assert!(base.split_u(s)); - } - assert!(base.split_v(0.5)); - let (nx, ny) = (base.nx(), base.ny()); - let on_border = |i: usize, j: usize| i == 0 || j == 0 || i == nx - 1 || j == ny - 1; - let mut m = base.clone(); - m.pull(&base.points, 0.5, 0.5, [-180.0, 40.0]); - for j in 0..ny { - for i in 0..nx { - if on_border(i, j) { - let (a, b) = (m.points[j * nx + i], base.points[j * nx + i]); - assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "border point ({i}, {j}) moved"); - } - } - } - // The pull itself still lands exactly. - let (before, after) = (base.eval(0.5, 0.5), m.eval(0.5, 0.5)); - assert!((after[0] - before[0] + 180.0).abs() < 1e-4 && (after[1] - before[1] - 40.0).abs() < 1e-4); - } - - #[test] - fn a_pull_anywhere_away_from_the_border_never_moves_the_border() { - // The default one-patch grid and a cut one; grabs all over, none within 12% of an edge. - let one = BezierMesh::identity([0.0, 0.0, 600.0, 800.0], 1, 1); - let mut cut = one.clone(); - assert!(cut.split_u(0.4) && cut.split_u(0.7) && cut.split_v(0.35) && cut.split_v(0.8)); - for base in [one, cut] { - let (nx, ny) = (base.nx(), base.ny()); - for (s, t) in [(0.5, 0.5), (0.2, 0.5), (0.8, 0.3), (0.3, 0.85), (0.14, 0.14), (0.86, 0.5)] { - for d in [[-400.0, 0.0], [250.0, -90.0], [0.0, 300.0]] { - let mut m = base.clone(); - m.pull(&base.points, s, t, d); - for j in 0..ny { - for i in 0..nx { - if i == 0 || j == 0 || i == nx - 1 || j == ny - 1 { - let (a, b) = (m.points[j * nx + i], base.points[j * nx + i]); - assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "({s}, {t}) by {d:?} moved border point ({i}, {j})"); - } - } - } - let (before, after) = (base.eval(s, t), m.eval(s, t)); - assert!((after[0] - before[0] - d[0]).abs() < 1e-3 && (after[1] - before[1] - d[1]).abs() < 1e-3, "({s}, {t}) by {d:?}: {after:?}"); - } - } - } - } - - #[test] - fn pulling_on_the_border_bows_it_and_leaves_the_corners_put() { - // A one-patch grid, grabbed on the middle of its right edge and pulled 170 px out. - let base = BezierMesh::identity([0.0, 0.0, 800.0, 1200.0], 1, 1); - let mut m = base.clone(); - m.pull(&base.points, 1.0, 0.5, [170.0, 0.0]); - let at = m.eval(1.0, 0.5); - assert!((at[0] - 970.0).abs() < 1e-3 && (at[1] - 600.0).abs() < 1e-3, "{at:?}"); - for (s, t) in [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)] { - let (a, b) = (m.eval(s, t), base.eval(s, t)); - assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "corner ({s}, {t}) moved"); - } - // The other three edges did not move. - for k in 0..=10 { - let u = k as f64 / 10.0; - for (s, t) in [(0.0, u), (u, 0.0), (u, 1.0)] { - let (a, b) = (m.eval(s, t), base.eval(s, t)); - assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "edge point ({s}, {t}) moved"); - } - } - // Only the right edge's two handles carry the pull. - let nx = base.nx(); - let moved: Vec = (0..m.points.len()).filter(|k| m.points[*k] != base.points[*k]).collect(); - assert_eq!(moved, vec![nx + 3, 2 * nx + 3], "{moved:?}"); - assert_eq!(fold_fraction(&m), 0.0); - } - - #[test] - fn pull_keeps_the_surface_smooth_across_the_grid_lines() { - // A grid cut unevenly both ways, then pulled hard inside one cell and then another. - let mut base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 1, 1); - for s in [0.3, 0.55, 0.9] { - assert!(base.split_u(s)); - assert!(base.split_v(s)); - } - assert!(tangent_defect(&base) < 1e-9, "cuts are exact"); - for (s, t, d) in [(0.42, 0.42, [60.0, -35.0]), (0.75, 0.2, [-40.0, 50.0]), (0.1, 0.95, [30.0, 30.0])] { - let mut m = base.clone(); - m.pull(&base.points, s, t, d); - assert!(tangent_defect(&m) < 1e-6, "({s}, {t}): {}", tangent_defect(&m)); - let before = base.eval(s, t); - let after = m.eval(s, t); - assert!((after[0] - before[0] - d[0]).abs() < 1e-4 && (after[1] - before[1] - d[1]).abs() < 1e-4, "{after:?}"); - } - } - - #[test] - fn pull_moves_the_grabbed_surface_point_exactly_and_stays_local() { - let base = BezierMesh::identity(B, 3, 2); - for (s, t) in [(0.5, 0.5), (0.1, 0.9), (0.4, 0.25), (0.0, 0.5), (1.0, 0.3)] { - let mut m = base.clone(); - let before = base.eval(s, t); - m.pull(&base.points, s, t, [7.0, -4.5]); - let after = m.eval(s, t); - assert!((after[0] - before[0] - 7.0).abs() < 1e-4 && (after[1] - before[1] + 4.5).abs() < 1e-4, "({s}, {t}): {after:?}"); - } - // The pull stays near the grabbed patch: the far end of the grid does not move. - let mut m = base.clone(); - m.pull(&base.points, 0.1, 0.25, [5.0, 5.0]); - let nx = base.nx(); - let moved = |k: usize| (m.points[k][0] - base.points[k][0]).abs() > 1e-6 || (m.points[k][1] - base.points[k][1]).abs() > 1e-6; - assert!((0..base.ny()).all(|j| !moved(j * nx + nx - 1)), "last column stays put"); - assert!((0..nx).any(moved), "something moved"); - // Pulling again from the same base replaces the pull (a drag is not cumulative). - m.pull(&base.points, 0.5, 0.25, [0.0, 0.0]); - assert!(m.points.iter().zip(&base.points).all(|(a, b)| (a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9)); - } - - fn close(a: (f64, f64), b: (f64, f64), eps: f64) -> bool { - (a.0 - b.0).abs() < eps && (a.1 - b.1).abs() < eps - } - - #[test] - fn styles_parse_and_round_trip_names() { - for s in WarpStyle::all() { - assert_eq!(WarpStyle::parse(s.id()), Some(s)); - assert_eq!(WarpStyle::parse(s.psd_name()), Some(s)); - assert_eq!(WarpStyle::parse(s.label()), Some(s), "{s:?}"); - } - assert_eq!(WarpStyle::parse("spiral"), None); - } - - #[test] - fn presets_at_bend_zero_are_identity() { - for s in WarpStyle::PRESETS { - let w = Warp::preset(s, 0.0, B); - assert!(w.is_identity()); - assert_eq!(w.map(33.0, 44.0), (33.0, 44.0)); - } - } - - #[test] - fn identity_mesh_is_exact_and_fit_reproduces_affine() { - let m = BezierMesh::identity(B, 1, 1); - assert_eq!((m.nx(), m.ny()), (4, 4)); - let w = Warp::custom(m, B); - assert!(w.is_identity()); - assert!(close(w.map(37.5, 61.25), (37.5, 61.25), 1e-9)); - // Affine maps fit exactly. - let f = |s: f64, t: f64| [3.0 + 2.0 * s - t, 1.0 + 0.5 * s + 4.0 * t]; - let m = BezierMesh::fit(&f, vec![0.0, 1.0], vec![0.0, 0.4, 1.0]); - for (s, t) in [(0.1, 0.9), (0.5, 0.2), (0.77, 0.41)] { - let p = m.eval(s, t); - let e = f(s, t); - assert!((p[0] - e[0]).abs() < 1e-9 && (p[1] - e[1]).abs() < 1e-9); - } - } - - #[test] - fn splits_are_exact_and_removable() { - let mut m = BezierMesh::identity(B, 1, 1); - // Bend it: move an inner control point. - m.points[5] = [50.0, 10.0]; - let before: Vec<[f64; 2]> = (0..=10).flat_map(|j| (0..=10).map(move |i| (i, j))).map(|(i, j)| m.eval(i as f64 / 10.0, j as f64 / 10.0)).collect(); - let orig = m.clone(); - assert!(m.split_u(0.3)); - assert!(m.split_v(0.6)); - assert!(!m.split_u(0.3), "existing boundary"); - assert_eq!((m.nx(), m.ny()), (7, 7)); - assert!(m.is_valid()); - let after: Vec<[f64; 2]> = (0..=10).flat_map(|j| (0..=10).map(move |i| (i, j))).map(|(i, j)| m.eval(i as f64 / 10.0, j as f64 / 10.0)).collect(); - for (a, b) in before.iter().zip(&after) { - assert!((a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9); - } - let (ls, lt) = m.param_at(m.eval(0.42, 0.77)); - assert!((ls - 0.42).abs() < 1e-6 && (lt - 0.77).abs() < 1e-6, "{ls},{lt}"); - assert!(m.remove_split_u(1)); - assert!(m.remove_split_v(1)); - assert_eq!(m.us, orig.us); - for (a, b) in m.points.iter().zip(&orig.points) { - assert!((a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9); - } - } - - const SQUARE: [f64; 4] = [128.0, 128.0, 384.0, 384.0]; - const WIDE: [f64; 4] = [128.0, 208.0, 384.0, 304.0]; - const TALL: [f64; 4] = [208.0, 128.0, 304.0, 384.0]; - - /// Photoshop 27.11, measured: a 256 px grid image whose colours encode each pixel's own - /// position, warped in a 512 px document over the box (128, 128)–(384, 384). Each pair is a - /// source point and where Photoshop put it. - #[test] - fn presets_match_photoshop() { - type Case = (WarpStyle, f64, f64, f64, bool, [([f64; 2], [f64; 2]); 3]); - let cases: [Case; 9] = [ - (WarpStyle::Arc, 50.0, 0.0, 0.0, false, [([272.5, 155.5], [296.5, 104.5]), ([331.5, 182.5], [424.5, 168.5]), ([259.5, 157.5], [264.5, 104.5])]), - (WarpStyle::Arc, -100.0, 0.0, 0.0, false, [([277.5, 354.5], [344.5, 472.5]), ([140.5, 218.5], [40.5, 168.5]), ([172.5, 237.5], [56.5, 264.5])]), - (WarpStyle::Flag, 50.0, 0.0, 0.0, false, [([136.5, 143.5], [136.5, 120.5]), ([136.5, 159.5], [136.5, 136.5]), ([136.5, 175.5], [136.5, 152.5])]), - (WarpStyle::Twist, -50.0, 0.0, 0.0, false, [([148.5, 205.5], [152.5, 216.5]), ([197.5, 212.5], [200.5, 232.5]), ([134.5, 212.5], [136.5, 216.5])]), - ( - WarpStyle::ShellLower, - 50.0, - 0.0, - 0.0, - false, - [([248.5, 136.5], [248.5, 136.5]), ([264.5, 136.5], [264.5, 136.5]), ([232.5, 136.5], [232.5, 136.5])], - ), - (WarpStyle::Fish, -100.0, 0.0, 0.0, false, [([296.5, 355.5], [296.5, 440.5]), ([344.5, 298.5], [344.5, 344.5]), ([328.5, 356.5], [328.5, 472.5])]), - (WarpStyle::Bulge, 50.0, 50.0, 0.0, false, [([293.5, 147.5], [296.5, 88.5]), ([293.5, 375.5], [296.5, 440.5]), ([340.5, 281.5], [344.5, 296.5])]), - (WarpStyle::Arc, 50.0, 0.0, -50.0, false, [([181.5, 350.5], [184.5, 328.5]), ([291.5, 372.5], [280.5, 344.5]), ([232.5, 349.5], [232.5, 312.5])]), - (WarpStyle::Arc, 50.0, 0.0, 0.0, true, [([155.5, 272.5], [104.5, 296.5]), ([182.5, 331.5], [168.5, 424.5]), ([157.5, 259.5], [104.5, 264.5])]), - ]; - // Wide (256 × 96 at 128, 208) and tall (96 × 256 at 208, 128) boxes, and a vertical warp - // with distortion. - type Boxed = (WarpStyle, f64, f64, f64, bool, [f64; 4], [([f64; 2], [f64; 2]); 2]); - let boxed: [Boxed; 6] = [ - (WarpStyle::Arc, 50.0, 0.0, 0.0, false, WIDE, [([144.5, 269.6], [120.5, 264.5]), ([271.5, 220.42], [280.5, 168.5])]), - (WarpStyle::Bulge, 50.0, 0.0, 0.0, false, TALL, [([249.11, 374.5], [248.5, 392.5]), ([249.11, 360.5], [248.5, 376.5])]), - (WarpStyle::Flag, 50.0, 0.0, 0.0, true, WIDE, [([137.5, 296.42], [184.5, 296.5]), ([153.5, 296.42], [200.5, 296.5])]), - (WarpStyle::Rise, 50.0, 0.0, 0.0, true, TALL, [([250.97, 152.5], [344.5, 152.5]), ([301.26, 264.5], [344.5, 264.5])]), - (WarpStyle::Bulge, 60.0, 20.0, -10.0, false, WIDE, [([208.5, 224.15], [200.5, 184.5]), ([219.5, 277.42], [216.5, 296.5])]), - (WarpStyle::Arc, 50.0, 30.0, 0.0, true, SQUARE, [([331.5, 327.5], [296.5, 360.5]), ([318.5, 240.5], [264.5, 232.5])]), - ]; - let all = - cases.iter().map(|c| (c.0, c.1, c.2, c.3, c.4, SQUARE, c.5.to_vec())).chain(boxed.iter().map(|c| (c.0, c.1, c.2, c.3, c.4, c.5, c.6.to_vec()))); - for (style, bend, hd, vd, vertical, bounds, pairs) in all { - let w = Warp { h_distort: hd, v_distort: vd, vertical, ..Warp::preset(style, bend, bounds) }; - for (src, want) in pairs { - let got = w.map(src[0], src[1]); - let d = (got.0 - want[0]).hypot(got.1 - want[1]); - assert!(d < 1.5, "{style:?} {bend} h{hd} v{vd} vertical {vertical} {bounds:?}: {src:?} -> {got:?}, Photoshop {want:?}"); - } - } - } - - #[test] - fn preset_fits_closely_as_mesh_and_affine_composes() { - let w = Warp::preset(WarpStyle::Bulge, 40.0, B); - let m = Warp::custom(w.to_mesh(4, 4), B); - for (x, y) in [(20.0, 30.0), (60.0, 45.0), (100.0, 65.0)] { - assert!(close(w.map(x, y), m.map(x, y), 0.5), "{:?} vs {:?}", w.map(x, y), m.map(x, y)); - } - let t = Warp::custom(BezierMesh::identity(B, 1, 1), B).then_affine([1.0, 0.0, 0.0, 1.0, 5.0, -2.0]); - assert!(close(t.map(50.0, 50.0), (55.0, 48.0), 1e-9)); - let ob = Warp::none(B).output_bounds(); - assert!((ob[0] - 10.0).abs() < 1e-9 && (ob[3] - 70.0).abs() < 1e-9); - } -} +mod tests; diff --git a/crates/geom/src/warp/tests.rs b/crates/geom/src/warp/tests.rs new file mode 100644 index 0000000000..1fc9612316 --- /dev/null +++ b/crates/geom/src/warp/tests.rs @@ -0,0 +1,459 @@ +use super::*; + +const B: [f64; 4] = [10.0, 20.0, 110.0, 70.0]; + +/// The largest mismatch between the tangents on the two sides of any interior grid line. +fn tangent_defect(m: &BezierMesh) -> f64 { + let nx = m.nx(); + let mut worst: f64 = 0.0; + for a in 1..m.us.len() - 1 { + let (hl, hr) = (m.us[a] - m.us[a - 1], m.us[a + 1] - m.us[a]); + for j in 0..m.ny() { + let (l, c, r) = (m.points[j * nx + 3 * a - 1], m.points[j * nx + 3 * a], m.points[j * nx + 3 * a + 1]); + for k in 0..2 { + worst = worst.max(((c[k] - l[k]) / hl - (r[k] - c[k]) / hr).abs()); + } + } + } + for b in 1..m.vs.len() - 1 { + let (hl, hr) = (m.vs[b] - m.vs[b - 1], m.vs[b + 1] - m.vs[b]); + for i in 0..nx { + let (l, c, r) = (m.points[(3 * b - 1) * nx + i], m.points[3 * b * nx + i], m.points[(3 * b + 1) * nx + i]); + for k in 0..2 { + worst = worst.max(((c[k] - l[k]) / hl - (r[k] - c[k]) / hr).abs()); + } + } + } + worst +} + +#[test] +fn dragging_an_anchor_or_handle_keeps_the_surface_smooth_across_the_lines() { + let mut base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 1, 1); + for s in [0.3, 0.55, 0.9] { + assert!(base.split_u(s)); + assert!(base.split_v(s)); + } + let nx = base.nx(); + // Every kind of point on a grid line, interior and border: anchors, handles each way. + for (i, j) in [(6, 6), (7, 6), (5, 6), (6, 7), (6, 5), (3, 9), (9, 3), (0, 6), (6, 0), (4, 3), (3, 4), (12, 12)] { + let mut m = base.clone(); + m.drag_point(&base.points, j * nx + i, [23.0, -31.0]); + assert_eq!(m.points[j * nx + i], [base.points[j * nx + i][0] + 23.0, base.points[j * nx + i][1] - 31.0], "({i}, {j}) lands on the pointer"); + assert!(tangent_defect(&m) < 1e-6, "({i}, {j}): {}", tangent_defect(&m)); + } +} + +/// Fraction of the surface where the map folds over itself (its Jacobian is not positive). +fn fold_fraction(m: &BezierMesh) -> f64 { + let (n, h) = (80, 1e-4); + let mut bad = 0; + for j in 0..n { + for i in 0..n { + let (s, t) = ((i as f64 + 0.5) / n as f64, (j as f64 + 0.5) / n as f64); + let q = m.eval(s, t); + let (qs, qt) = (m.eval(s + h, t), m.eval(s, t + h)); + if (qs[0] - q[0]) * (qt[1] - q[1]) - (qt[0] - q[0]) * (qs[1] - q[1]) <= 0.0 { + bad += 1; + } + } + } + bad as f64 / (n * n) as f64 +} + +#[test] +fn a_pull_within_a_quarter_cell_never_folds_the_surface() { + // 75 px cells: dragging a quarter of a cell, any direction, anywhere in the grid must not tear the picture. + let base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 4, 4); + for (s, t) in [(0.42, 0.42), (0.75, 0.2), (0.2, 0.2), (0.5, 0.5), (0.85, 0.7), (0.3, 0.7)] { + for (dx, dy) in [(-1.0, 0.0), (1.0, 0.0), (0.0, -1.0), (0.0, 1.0), (0.7, 0.7), (-0.7, 0.7)] { + let mut m = base.clone(); + m.pull(&base.points, s, t, [dx * 19.0, dy * 19.0]); + assert_eq!(fold_fraction(&m), 0.0, "({s}, {t}) by ({dx}, {dy}) quarter cells"); + } + } +} + +#[test] +fn a_pull_in_the_middle_leaves_the_border_where_it_is() { + let mut base = BezierMesh::identity([0.0, 0.0, 600.0, 800.0], 1, 1); + for s in [0.33, 0.66] { + assert!(base.split_u(s)); + } + assert!(base.split_v(0.5)); + let (nx, ny) = (base.nx(), base.ny()); + let on_border = |i: usize, j: usize| i == 0 || j == 0 || i == nx - 1 || j == ny - 1; + let mut m = base.clone(); + m.pull(&base.points, 0.5, 0.5, [-180.0, 40.0]); + for j in 0..ny { + for i in 0..nx { + if on_border(i, j) { + let (a, b) = (m.points[j * nx + i], base.points[j * nx + i]); + assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "border point ({i}, {j}) moved"); + } + } + } + // The pull itself still lands exactly. + let (before, after) = (base.eval(0.5, 0.5), m.eval(0.5, 0.5)); + assert!((after[0] - before[0] + 180.0).abs() < 1e-4 && (after[1] - before[1] - 40.0).abs() < 1e-4); +} + +#[test] +fn a_pull_anywhere_away_from_the_border_never_moves_the_border() { + // The default one-patch grid and a cut one; grabs all over, none within 12% of an edge. + let one = BezierMesh::identity([0.0, 0.0, 600.0, 800.0], 1, 1); + let mut cut = one.clone(); + assert!(cut.split_u(0.4) && cut.split_u(0.7) && cut.split_v(0.35) && cut.split_v(0.8)); + for base in [one, cut] { + let (nx, ny) = (base.nx(), base.ny()); + for (s, t) in [(0.5, 0.5), (0.2, 0.5), (0.8, 0.3), (0.3, 0.85), (0.14, 0.14), (0.86, 0.5)] { + for d in [[-400.0, 0.0], [250.0, -90.0], [0.0, 300.0]] { + let mut m = base.clone(); + m.pull(&base.points, s, t, d); + for j in 0..ny { + for i in 0..nx { + if i == 0 || j == 0 || i == nx - 1 || j == ny - 1 { + let (a, b) = (m.points[j * nx + i], base.points[j * nx + i]); + assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "({s}, {t}) by {d:?} moved border point ({i}, {j})"); + } + } + } + let (before, after) = (base.eval(s, t), m.eval(s, t)); + assert!((after[0] - before[0] - d[0]).abs() < 1e-3 && (after[1] - before[1] - d[1]).abs() < 1e-3, "({s}, {t}) by {d:?}: {after:?}"); + } + } + } +} + +#[test] +fn pulling_on_the_border_bows_it_and_leaves_the_corners_put() { + // A one-patch grid, grabbed on the middle of its right edge and pulled 170 px out. + let base = BezierMesh::identity([0.0, 0.0, 800.0, 1200.0], 1, 1); + let mut m = base.clone(); + m.pull(&base.points, 1.0, 0.5, [170.0, 0.0]); + let at = m.eval(1.0, 0.5); + assert!((at[0] - 970.0).abs() < 1e-3 && (at[1] - 600.0).abs() < 1e-3, "{at:?}"); + for (s, t) in [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)] { + let (a, b) = (m.eval(s, t), base.eval(s, t)); + assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "corner ({s}, {t}) moved"); + } + // The other three edges did not move. + for k in 0..=10 { + let u = k as f64 / 10.0; + for (s, t) in [(0.0, u), (u, 0.0), (u, 1.0)] { + let (a, b) = (m.eval(s, t), base.eval(s, t)); + assert!((a[0] - b[0]).abs() < 1e-6 && (a[1] - b[1]).abs() < 1e-6, "edge point ({s}, {t}) moved"); + } + } + // Only the right edge's two handles carry the pull. + let nx = base.nx(); + let moved: Vec = (0..m.points.len()).filter(|k| m.points[*k] != base.points[*k]).collect(); + assert_eq!(moved, vec![nx + 3, 2 * nx + 3], "{moved:?}"); + assert_eq!(fold_fraction(&m), 0.0); +} + +#[test] +fn pull_keeps_the_surface_smooth_across_the_grid_lines() { + // A grid cut unevenly both ways, then pulled hard inside one cell and then another. + let mut base = BezierMesh::identity([0.0, 0.0, 300.0, 300.0], 1, 1); + for s in [0.3, 0.55, 0.9] { + assert!(base.split_u(s)); + assert!(base.split_v(s)); + } + assert!(tangent_defect(&base) < 1e-9, "cuts are exact"); + for (s, t, d) in [(0.42, 0.42, [60.0, -35.0]), (0.75, 0.2, [-40.0, 50.0]), (0.1, 0.95, [30.0, 30.0])] { + let mut m = base.clone(); + m.pull(&base.points, s, t, d); + assert!(tangent_defect(&m) < 1e-6, "({s}, {t}): {}", tangent_defect(&m)); + let before = base.eval(s, t); + let after = m.eval(s, t); + assert!((after[0] - before[0] - d[0]).abs() < 1e-4 && (after[1] - before[1] - d[1]).abs() < 1e-4, "{after:?}"); + } +} + +#[test] +fn pull_moves_the_grabbed_surface_point_exactly_and_stays_local() { + let base = BezierMesh::identity(B, 3, 2); + for (s, t) in [(0.5, 0.5), (0.1, 0.9), (0.4, 0.25), (0.0, 0.5), (1.0, 0.3)] { + let mut m = base.clone(); + let before = base.eval(s, t); + m.pull(&base.points, s, t, [7.0, -4.5]); + let after = m.eval(s, t); + assert!((after[0] - before[0] - 7.0).abs() < 1e-4 && (after[1] - before[1] + 4.5).abs() < 1e-4, "({s}, {t}): {after:?}"); + } + // The pull stays near the grabbed patch: the far end of the grid does not move. + let mut m = base.clone(); + m.pull(&base.points, 0.1, 0.25, [5.0, 5.0]); + let nx = base.nx(); + let moved = |k: usize| (m.points[k][0] - base.points[k][0]).abs() > 1e-6 || (m.points[k][1] - base.points[k][1]).abs() > 1e-6; + assert!((0..base.ny()).all(|j| !moved(j * nx + nx - 1)), "last column stays put"); + assert!((0..nx).any(moved), "something moved"); + // Pulling again from the same base replaces the pull (a drag is not cumulative). + m.pull(&base.points, 0.5, 0.25, [0.0, 0.0]); + assert!(m.points.iter().zip(&base.points).all(|(a, b)| (a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9)); +} + +fn close(a: (f64, f64), b: (f64, f64), eps: f64) -> bool { + (a.0 - b.0).abs() < eps && (a.1 - b.1).abs() < eps +} + +#[test] +fn styles_parse_and_round_trip_names() { + for s in WarpStyle::all() { + assert_eq!(WarpStyle::parse(s.id()), Some(s)); + assert_eq!(WarpStyle::parse(s.psd_name()), Some(s)); + assert_eq!(WarpStyle::parse(s.label()), Some(s), "{s:?}"); + } + assert_eq!(WarpStyle::parse("spiral"), None); +} + +#[test] +fn presets_at_bend_zero_are_identity() { + for s in WarpStyle::PRESETS { + let w = Warp::preset(s, 0.0, B); + assert!(w.is_identity()); + assert_eq!(w.map(33.0, 44.0), (33.0, 44.0)); + } +} + +#[test] +fn identity_mesh_is_exact_and_fit_reproduces_affine() { + let m = BezierMesh::identity(B, 1, 1); + assert_eq!((m.nx(), m.ny()), (4, 4)); + let w = Warp::custom(m, B); + assert!(w.is_identity()); + assert!(close(w.map(37.5, 61.25), (37.5, 61.25), 1e-9)); + // Affine maps fit exactly. + let f = |s: f64, t: f64| [3.0 + 2.0 * s - t, 1.0 + 0.5 * s + 4.0 * t]; + let m = BezierMesh::fit(&f, vec![0.0, 1.0], vec![0.0, 0.4, 1.0]); + for (s, t) in [(0.1, 0.9), (0.5, 0.2), (0.77, 0.41)] { + let p = m.eval(s, t); + let e = f(s, t); + assert!((p[0] - e[0]).abs() < 1e-9 && (p[1] - e[1]).abs() < 1e-9); + } +} + +#[test] +fn remove_split_u_rejects_out_of_range_indices() { + let original = BezierMesh::identity(B, 2, 3); + for k in [0, original.us.len() - 1, original.us.len(), usize::MAX - 1, usize::MAX] { + let mut mesh = original.clone(); + assert!(!mesh.remove_split_u(k), "index {k}"); + assert_eq!(mesh, original, "index {k} must leave the mesh unchanged"); + } +} + +#[test] +fn remove_split_v_rejects_out_of_range_indices() { + let original = BezierMesh::identity(B, 2, 3); + for k in [0, original.vs.len() - 1, original.vs.len(), usize::MAX - 1, usize::MAX] { + let mut mesh = original.clone(); + assert!(!mesh.remove_split_v(k), "index {k}"); + assert_eq!(mesh, original, "index {k} must leave the mesh unchanged"); + } +} + +#[test] +fn splits_are_exact_and_removable() { + let mut m = BezierMesh::identity(B, 1, 1); + // Bend it: move an inner control point. + m.points[5] = [50.0, 10.0]; + let before: Vec<[f64; 2]> = (0..=10).flat_map(|j| (0..=10).map(move |i| (i, j))).map(|(i, j)| m.eval(i as f64 / 10.0, j as f64 / 10.0)).collect(); + let orig = m.clone(); + assert!(m.split_u(0.3)); + assert!(m.split_v(0.6)); + assert!(!m.split_u(0.3), "existing boundary"); + assert_eq!((m.nx(), m.ny()), (7, 7)); + assert!(m.is_valid()); + let after: Vec<[f64; 2]> = (0..=10).flat_map(|j| (0..=10).map(move |i| (i, j))).map(|(i, j)| m.eval(i as f64 / 10.0, j as f64 / 10.0)).collect(); + for (a, b) in before.iter().zip(&after) { + assert!((a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9); + } + let (ls, lt) = m.param_at(m.eval(0.42, 0.77)); + assert!((ls - 0.42).abs() < 1e-6 && (lt - 0.77).abs() < 1e-6, "{ls},{lt}"); + assert!(m.remove_split_u(1)); + assert!(m.remove_split_v(1)); + assert_eq!(m.us, orig.us); + for (a, b) in m.points.iter().zip(&orig.points) { + assert!((a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9); + } +} + +const SQUARE: [f64; 4] = [128.0, 128.0, 384.0, 384.0]; +const WIDE: [f64; 4] = [128.0, 208.0, 384.0, 304.0]; +const TALL: [f64; 4] = [208.0, 128.0, 304.0, 384.0]; + +/// Photoshop 27.11, measured: a 256 px grid image whose colours encode each pixel's own +/// position, warped in a 512 px document over the box (128, 128)–(384, 384). Each pair is a +/// source point and where Photoshop put it. +#[test] +fn presets_match_photoshop() { + type Case = (WarpStyle, f64, f64, f64, bool, [([f64; 2], [f64; 2]); 3]); + let cases: [Case; 9] = [ + (WarpStyle::Arc, 50.0, 0.0, 0.0, false, [([272.5, 155.5], [296.5, 104.5]), ([331.5, 182.5], [424.5, 168.5]), ([259.5, 157.5], [264.5, 104.5])]), + (WarpStyle::Arc, -100.0, 0.0, 0.0, false, [([277.5, 354.5], [344.5, 472.5]), ([140.5, 218.5], [40.5, 168.5]), ([172.5, 237.5], [56.5, 264.5])]), + (WarpStyle::Flag, 50.0, 0.0, 0.0, false, [([136.5, 143.5], [136.5, 120.5]), ([136.5, 159.5], [136.5, 136.5]), ([136.5, 175.5], [136.5, 152.5])]), + (WarpStyle::Twist, -50.0, 0.0, 0.0, false, [([148.5, 205.5], [152.5, 216.5]), ([197.5, 212.5], [200.5, 232.5]), ([134.5, 212.5], [136.5, 216.5])]), + (WarpStyle::ShellLower, 50.0, 0.0, 0.0, false, [([248.5, 136.5], [248.5, 136.5]), ([264.5, 136.5], [264.5, 136.5]), ([232.5, 136.5], [232.5, 136.5])]), + (WarpStyle::Fish, -100.0, 0.0, 0.0, false, [([296.5, 355.5], [296.5, 440.5]), ([344.5, 298.5], [344.5, 344.5]), ([328.5, 356.5], [328.5, 472.5])]), + (WarpStyle::Bulge, 50.0, 50.0, 0.0, false, [([293.5, 147.5], [296.5, 88.5]), ([293.5, 375.5], [296.5, 440.5]), ([340.5, 281.5], [344.5, 296.5])]), + (WarpStyle::Arc, 50.0, 0.0, -50.0, false, [([181.5, 350.5], [184.5, 328.5]), ([291.5, 372.5], [280.5, 344.5]), ([232.5, 349.5], [232.5, 312.5])]), + (WarpStyle::Arc, 50.0, 0.0, 0.0, true, [([155.5, 272.5], [104.5, 296.5]), ([182.5, 331.5], [168.5, 424.5]), ([157.5, 259.5], [104.5, 264.5])]), + ]; + // Wide (256 × 96 at 128, 208) and tall (96 × 256 at 208, 128) boxes, and a vertical warp + // with distortion. + type Boxed = (WarpStyle, f64, f64, f64, bool, [f64; 4], [([f64; 2], [f64; 2]); 2]); + let boxed: [Boxed; 6] = [ + (WarpStyle::Arc, 50.0, 0.0, 0.0, false, WIDE, [([144.5, 269.6], [120.5, 264.5]), ([271.5, 220.42], [280.5, 168.5])]), + (WarpStyle::Bulge, 50.0, 0.0, 0.0, false, TALL, [([249.11, 374.5], [248.5, 392.5]), ([249.11, 360.5], [248.5, 376.5])]), + (WarpStyle::Flag, 50.0, 0.0, 0.0, true, WIDE, [([137.5, 296.42], [184.5, 296.5]), ([153.5, 296.42], [200.5, 296.5])]), + (WarpStyle::Rise, 50.0, 0.0, 0.0, true, TALL, [([250.97, 152.5], [344.5, 152.5]), ([301.26, 264.5], [344.5, 264.5])]), + (WarpStyle::Bulge, 60.0, 20.0, -10.0, false, WIDE, [([208.5, 224.15], [200.5, 184.5]), ([219.5, 277.42], [216.5, 296.5])]), + (WarpStyle::Arc, 50.0, 30.0, 0.0, true, SQUARE, [([331.5, 327.5], [296.5, 360.5]), ([318.5, 240.5], [264.5, 232.5])]), + ]; + let all = cases.iter().map(|c| (c.0, c.1, c.2, c.3, c.4, SQUARE, c.5.to_vec())).chain(boxed.iter().map(|c| (c.0, c.1, c.2, c.3, c.4, c.5, c.6.to_vec()))); + for (style, bend, hd, vd, vertical, bounds, pairs) in all { + let w = Warp { h_distort: hd, v_distort: vd, vertical, ..Warp::preset(style, bend, bounds) }; + for (src, want) in pairs { + let got = w.map(src[0], src[1]); + let d = (got.0 - want[0]).hypot(got.1 - want[1]); + assert!(d < 1.5, "{style:?} {bend} h{hd} v{vd} vertical {vertical} {bounds:?}: {src:?} -> {got:?}, Photoshop {want:?}"); + } + } +} + +#[test] +fn preset_fits_closely_as_mesh_and_affine_composes() { + let w = Warp::preset(WarpStyle::Bulge, 40.0, B); + let m = Warp::custom(w.to_mesh(4, 4), B); + for (x, y) in [(20.0, 30.0), (60.0, 45.0), (100.0, 65.0)] { + assert!(close(w.map(x, y), m.map(x, y), 0.5), "{:?} vs {:?}", w.map(x, y), m.map(x, y)); + } + let t = Warp::custom(BezierMesh::identity(B, 1, 1), B).then_affine([1.0, 0.0, 0.0, 1.0, 5.0, -2.0]); + assert!(close(t.map(50.0, 50.0), (55.0, 48.0), 1e-9)); + let ob = Warp::none(B).output_bounds(); + assert!((ob[0] - 10.0).abs() < 1e-9 && (ob[3] - 70.0).abs() < 1e-9); +} + +/// A cylinder over a 1000 × 1000 source like #4112's: silhouettes at x 100 and 900, edges at +/// y 200 (top) and 800 (bottom), ellipses 0.3 deep, half wrapped round. +const CYL: [f64; 7] = [100.0, 800.0, 900.0, 200.0, 0.3, 0.3, 0.5]; + +#[test] +fn cylinder_style_is_read_and_named_but_not_a_bend_preset() { + for s in ["cylinder", "warpCylinder", "Cylinder"] { + assert_eq!(WarpStyle::parse(s), Some(WarpStyle::Cylinder), "{s}"); + } + assert_eq!(WarpStyle::Cylinder.psd_name(), "warpCylinder"); + assert_eq!(WarpStyle::Cylinder.id(), "cylinder"); + assert!(!WarpStyle::Cylinder.is_preset()); + assert!(StyleWarp::new(WarpStyle::Cylinder, 50.0, 0.0, 0.0, false, B).is_none()); + // The Warp menus list what they can edit. + assert!(WarpStyle::all().all(|s| s != WarpStyle::Cylinder)); + assert_eq!(WarpStyle::all().filter(|s| s.is_preset()).count(), 15); +} + +#[test] +fn cylinder_values_that_describe_no_cylinder_are_refused() { + assert!(cylinder_mesh(&CYL[..6]).is_none(), "six values"); + assert!(cylinder_mesh(&[CYL.as_slice(), &[0.0]].concat()).is_none(), "eight values"); + for i in 0..7 { + for bad in [f64::NAN, f64::INFINITY] { + let mut v = CYL; + v[i] = bad; + assert!(cylinder_mesh(&v).is_none(), "value {i} = {bad}"); + } + } + let mut v = CYL; + v[2] = v[0]; + assert!(Warp::cylinder(&v, [0.0, 0.0, 1000.0, 1000.0]).is_none(), "no width"); + v[2] = 50.0; + assert!(Warp::cylinder(&v, [0.0, 0.0, 1000.0, 1000.0]).is_none(), "right silhouette left of the left one"); +} + +#[test] +fn cylinder_wraps_the_contents_between_its_silhouettes() { + let w = Warp::cylinder(&CYL, [0.0, 0.0, 1000.0, 1000.0]).unwrap(); + assert_eq!(w.style, WarpStyle::Cylinder); + assert_eq!(w.values, CYL); + assert!(!w.is_identity()); + let m = w.mesh.as_ref().unwrap(); + assert_eq!((m.nx(), m.ny()), (13, 4)); + let [ul, ..] = cylinder_shape(0.5); + assert!((m.us[1] - ul).abs() < 1e-12 && (m.us[3] - (1.0 - ul)).abs() < 1e-12); + // The middle of the contents is at the front: centred, on the ellipses' near side. + assert!(close(w.map(500.0, 0.0), (500.0, 200.0 + 0.3 * 400.0), 1e-9)); + assert!(close(w.map(500.0, 1000.0), (500.0, 800.0 + 0.3 * 400.0), 1e-9)); + // What wraps round the back lies on the silhouettes, at the edges' ends. + for x in [0.0, ul * 500.0, ul * 1000.0] { + assert!(close(w.map(x, 0.0), (100.0, 200.0), 1e-9), "{x}"); + assert!(close(w.map(1000.0 - x, 1000.0), (900.0, 800.0), 1e-9), "{x}"); + } + // Across the front x only grows, and the two halves mirror each other. + let mut last = f64::MIN; + for i in 0..=200 { + let x = 1000.0 * (ul + (1.0 - 2.0 * ul) * f64::from(i) / 200.0); + let (px, py) = w.map(x, 400.0); + assert!(px >= last - 1e-9, "x {x}"); + last = px; + let (qx, qy) = w.map(1000.0 - x, 400.0); + assert!(close((px - 500.0, py), (500.0 - qx, qy), 1e-9), "x {x}"); + } + // Every point stays inside the control-point box the placement frames. + let cb = m.control_bounds(); + let ob = w.output_bounds(); + assert!(ob[0] >= cb[0] - 1e-9 && ob[1] >= cb[1] - 1e-9 && ob[2] <= cb[2] + 1e-9 && ob[3] <= cb[3] + 1e-9); + assert!((cb[0] - 100.0).abs() < 1e-9 && (cb[2] - 900.0).abs() < 1e-9); + // The box's bottom is the lower ellipse's deepest control point (`my` > 1). + assert!((cb[3] - (800.0 + 0.3 * 400.0 * cylinder_shape(0.5)[3])).abs() < 1e-9); + assert_eq!(w.to_mesh(4, 4), *m, "a cylinder is its own mesh"); +} + +#[test] +fn cylinder_shape_follows_the_measured_rows() { + assert_eq!(cylinder_shape(0.0), CYLINDER_SHAPE[0]); + assert_eq!(cylinder_shape(1.0), CYLINDER_SHAPE[40]); + assert_eq!(cylinder_shape(-3.0), CYLINDER_SHAPE[0]); + assert_eq!(cylinder_shape(7.0), CYLINDER_SHAPE[40]); + assert_eq!(cylinder_shape(f64::NAN), CYLINDER_SHAPE[0]); + let mid = cylinder_shape(0.0125); + for k in 0..4 { + assert!((mid[k] - (CYLINDER_SHAPE[0][k] + CYLINDER_SHAPE[1][k]) / 2.0).abs() < 1e-12); + } + // The more it wraps, the less of the contents is on the back. + assert!(CYLINDER_SHAPE.windows(2).all(|r| r[1][0] < r[0][0])); +} + +#[test] +fn is_identity_rejects_malformed_custom_mesh_without_allocating() { + let us: Vec = (0..=1000).map(|i| i as f64 / 1000.0).collect(); + let vs: Vec = (0..=1000).map(|i| i as f64 / 1000.0).collect(); + let malformed = BezierMesh { us, vs, points: Vec::new() }; + assert!(!malformed.is_valid()); + let w = Warp::custom(malformed.clone(), B); + assert!(!w.is_identity()); + assert_eq!(malformed.eval(0.5, 0.5), [0.0, 0.0]); +} + +#[test] +fn locate_binary_search_matches_linear_scan() { + let knots = [0.0, 0.1, 0.25, 0.5, 0.85, 1.0]; + let linear = |s: f64| { + let n = knots.len() - 1; + let s = if s.is_finite() { s.clamp(0.0, 1.0) } else { 0.0 }; + let mut i = 0; + while i + 1 < n && s > knots[i + 1] { + i += 1; + } + let w = knots[i + 1] - knots[i]; + (i, if w > 0.0 { (s - knots[i]) / w } else { 0.0 }) + }; + for k in -10..=110 { + let s = k as f64 / 100.0; + assert_eq!(BezierMesh::locate(&knots, s), linear(s), "s={s}"); + } + for &s in &knots { + assert_eq!(BezierMesh::locate(&knots, s), linear(s), "knot={s}"); + } + assert_eq!(BezierMesh::locate(&knots, f64::NAN), linear(f64::NAN)); +} diff --git a/crates/gpu/Cargo.toml b/crates/gpu/Cargo.toml index 6a0b649cd7..e33ec9c01a 100644 --- a/crates/gpu/Cargo.toml +++ b/crates/gpu/Cargo.toml @@ -14,6 +14,9 @@ photocraft-color = { workspace = true } photocraft-raster = { workspace = true } photocraft-doc = { workspace = true } photocraft-compose = { workspace = true } +# Camera Raw's per-pixel stages (`camera_raw.rs`) take their constants from the CPU develop. +photocraft-algo = { workspace = true } +bytemuck = { workspace = true } # Same major version as eframe's wgpu so the app's device can be shared. wgpu = { version = "30", default-features = false, features = ["std", "wgsl"] } log = { workspace = true } @@ -27,3 +30,5 @@ wgpu = { version = "30" } pollster = "1" # Shape layers in the effect parity tests. photocraft-vector = { workspace = true } +# A document-tagged CMYK profile in the tile conversion test. +photocraft-cms = { workspace = true } diff --git a/crates/gpu/examples/bench_camera_raw.rs b/crates/gpu/examples/bench_camera_raw.rs new file mode 100644 index 0000000000..be206fe382 --- /dev/null +++ b/crates/gpu/examples/bench_camera_raw.rs @@ -0,0 +1,116 @@ +//! Camera Raw's preview develop on the 858×572 proxy of a 6000×4000 image: the CPU develop +//! against the GPU pass (upload, render, readback), for per-pixel settings. +//! `cargo run --release -p photocraft-gpu --example bench_camera_raw [-- --quick]` +use std::time::Instant; + +use photocraft_algo::camera_raw::{CameraRaw, Wheel, develop, per_pixel, white_black_points}; +use photocraft_gpu::CameraRawDevelop; + +fn median(mut v: Vec) -> f64 { + v.sort_by(f64::total_cmp); + v[v.len() / 2] +} + +fn time(runs: usize, mut f: impl FnMut()) -> f64 { + f(); + median( + (0..runs) + .map(|_| { + let t = Instant::now(); + f(); + t.elapsed().as_secs_f64() * 1e3 + }) + .collect(), + ) +} + +/// [`time`] for a call that can fail: its first error ends the benchmark instead of being timed. +fn try_time(runs: usize, mut f: impl FnMut() -> Result<(), E>) -> Result { + let mut failed = None; + let ms = time(runs, || { + if failed.is_none() { + failed = f().err(); + } + }); + failed.map_or(Ok(ms), Err) +} + +fn main() -> Result<(), Box> { + let runs = if std::env::args().any(|a| a == "--quick") { 5 } else { 40 }; + let (w, h) = (858usize, 572usize); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); + let options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }; + let adapter = pollster::block_on(instance.request_adapter(&options))?; + let (device, queue) = pollster::block_on(adapter.request_device(&Default::default()))?; + let info = adapter.get_info(); + println!("adapter: {} ({:?}), proxy {w}x{h}, median of {runs}", info.name, info.backend); + let built = Instant::now(); + let mut pass = CameraRawDevelop::new(&device)?; + println!("pipeline build: {:.1} ms", built.elapsed().as_secs_f64() * 1e3); + let src: Vec<[f32; 4]> = (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as f32 / w as f32, (i / w) as f32 / h as f32); + [0.1 + 0.8 * x, 0.2 + 0.6 * y, 0.6 - 0.5 * x * y, 1.0] + }) + .collect(); + let scale = w as f32 / 6000.0; + let mut hsl = [0.0; 8]; + hsl[1] = 30.0; + hsl[5] = -40.0; + let sets = [ + ( + "Basic (WB, Exposure, Contrast, Whites, Blacks)", + CameraRaw { temperature: 20.0, exposure: 0.4, contrast: 25.0, whites: 30.0, blacks: -20.0, pixel_scale: scale, ..Default::default() }, + ), + ( + "every per-pixel stage", + CameraRaw { + temperature: 20.0, + tint: 5.0, + exposure: 0.4, + contrast: 25.0, + whites: 30.0, + blacks: -20.0, + vibrance: 30.0, + saturation: 10.0, + curve_lights: 20.0, + point_curve: vec![[0.0, 8.0], [128.0, 140.0], [255.0, 250.0]], + hsl_hue: hsl, + hsl_sat: hsl, + hsl_lum: hsl, + grade_shadows: Wheel { hue: 210.0, sat: 30.0, lum: 0.0 }, + grade_highlights: Wheel { hue: 40.0, sat: 20.0, lum: 5.0 }, + vignette_amount: -30.0, + grain_amount: 25.0, + pixel_scale: scale, + ..Default::default() + }, + ), + ]; + for (name, p) in &sets { + let cpu = time(runs, || { + let mut px = src.clone(); + develop(&mut px, w, h, p, false); + }); + let points_ms = time(runs, || { + std::hint::black_box(white_black_points(&src, w, h, p)); + }); + let points = white_black_points(&src, w, h, p); + let stages = per_pixel(p, w, h, false, points).ok_or("not per pixel")?; + let size = [w as u32, h as u32]; + let upload = try_time(runs, || pass.develop(&device, &queue, None, &src, None, size, &stages).map(drop))?; + let cached = try_time(runs, || pass.develop(&device, &queue, None, &src, Some(1), size, &stages).map(drop))?; + println!("{name}:"); + println!(" CPU develop {cpu:7.2} ms"); + println!(" GPU, source uploaded every time {upload:7.2} ms"); + println!(" GPU, source cached (slider drag) {cached:7.2} ms"); + println!(" Whites/Blacks points on the CPU {points_ms:7.2} ms (cached per WB/Exposure/Contrast in the dialog)"); + } + let shadows = CameraRaw { shadows: 50.0, pixel_scale: scale, ..Default::default() }; + let cpu = time(runs.min(10), || { + let mut px = src.clone(); + develop(&mut px, w, h, &shadows, false); + }); + println!("for reference, Shadows +50 (CPU only): {cpu:.2} ms"); + Ok(()) +} diff --git a/crates/gpu/examples/bench_shape_drag.rs b/crates/gpu/examples/bench_shape_drag.rs new file mode 100644 index 0000000000..820c1e187f --- /dev/null +++ b/crates/gpu/examples/bench_shape_drag.rs @@ -0,0 +1,82 @@ +//! Styled shape group translation: synchronized frame time and effect-map rebuild counters. +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, Path, ShapeLayer, Subpath}; +use photocraft_geom::{Affine, Size}; +use photocraft_gpu::Compositor; +use std::time::Instant; + +/// Translate the fixture's paths and raster caches together, as a group drag preview does. +fn moved(base: &Document, offset: i32) -> Document { + let mut doc = base.clone(); + if let Some(group) = doc.layers.last_mut().and_then(Layer::children_mut) { + for layer in group { + if let LayerContent::Shape(shape) = &mut layer.content { + shape.path = shape.path.transform(&Affine::translate(f64::from(offset), f64::from(offset))); + if let Some(cache) = &mut shape.cache { + *cache = cache.translated(offset, offset, photocraft_compose::bounds::content_bounds(cache)); + } + } + } + } + doc +} +/// Measure synchronized drag frames; quick mode also verifies fresh-render parity. +fn main() -> Result<(), Box> { + let quick = std::env::args().any(|a| a == "--quick"); + let size = if quick { Size::new(1600, 1200) } else { Size::new(6000, 4000) }; + let mut doc = Document::with_background("styled shapes", size, ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut children = Vec::new(); + for i in 0..12 { + let x = 100.1 + f64::from(i % 4) * f64::from(size.width) / 5.0; + let y = 100.2 + f64::from(i / 4) * f64::from(size.height) / 4.0; + let w = f64::from(size.width) / 7.0; + let h = f64::from(size.height) / 6.0; + let mut shape = ShapeLayer { + path: Path::new(vec![Subpath::polygon(&[(x, y), (x + w, y), (x + w, y + h), (x, y + h)])]), + fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.8))), + ..Default::default() + }; + shape.cache = Some(photocraft_vector::render_shape(&shape, doc.pixel_format(), doc.bounds())); + let mut layer = Layer::new(format!("shape {i}"), LayerContent::Shape(shape)); + layer.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); + children.push(layer); + } + doc.layers.push(Layer::group("group", children)); + let adapter = pollster::block_on( + wgpu::Instance::default() + .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }), + )?; + let (device, queue) = pollster::block_on(adapter.request_device(&Default::default()))?; + println!("adapter={:?} document={}x{}", adapter.get_info().name, size.width, size.height); + let mut comp = Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter))?; + let mut samples = Vec::new(); + for frame in 0..8 { + let start = Instant::now(); + let current = moved(&doc, frame * 3); + let preview_ms = start.elapsed().as_secs_f64() * 1000.0; + let stats = comp.render(&device, &queue, ¤t, current.bounds(), |_, _| {})?; + device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None })?; + let ms = start.elapsed().as_secs_f64() * 1000.0; + println!( + "frame={frame} ms={ms:.3} preview_ms={preview_ms:.3} fx_shapes={} fx_programs={} fx_pixels={}", + stats.fx_shapes, stats.fx_programs, stats.fx_pixels + ); + if frame > 1 { + samples.push(ms); + } + } + samples.sort_by(f64::total_cmp); + println!("warm_median_ms={:.3}", samples.get(samples.len() / 2).copied().unwrap_or_default()); + if quick { + let current = moved(&doc, 21); + let warm = photocraft_gpu::render_to_vec(&mut comp, &device, &queue, ¤t, current.bounds())?; + let mut fresh = Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter))?; + let expected = photocraft_gpu::render_to_vec(&mut fresh, &device, &queue, ¤t, current.bounds())?; + let difference = warm.iter().zip(&expected).flat_map(|(a, b)| a.iter().zip(b).map(|(a, b)| (a - b).abs())).fold(0.0f32, f32::max); + println!("fresh_render_max_difference={difference}"); + if difference >= 1.0 / 255.0 { + return Err("reused effect maps differ from fresh rendering".into()); + } + } + Ok(()) +} diff --git a/crates/gpu/examples/bench_stack_reuse.rs b/crates/gpu/examples/bench_stack_reuse.rs index 30745c3794..2014cee902 100644 --- a/crates/gpu/examples/bench_stack_reuse.rs +++ b/crates/gpu/examples/bench_stack_reuse.rs @@ -17,7 +17,8 @@ fn render(comp: &mut Compositor, device: &wgpu::Device, queue: &wgpu::Queue, doc fn main() -> Result<(), Box> { let quick = std::env::args().any(|a| a == "--quick"); let size = if quick { Size::new(1024, 768) } else { Size::new(6000, 4000) }; - let adapter = pollster::block_on(wgpu::Instance::default().request_adapter(&Default::default()))?; + let adapter = + pollster::block_on(wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()).request_adapter(&Default::default()))?; let (device, queue) = pollster::block_on(adapter.request_device(&Default::default()))?; let format = Compositor::preferred_acc_format(&adapter); println!("adapter={:?}, accumulation={format:?}, size={}x{}", adapter.get_info(), size.width, size.height); diff --git a/crates/gpu/examples/zero_block_probe.rs b/crates/gpu/examples/zero_block_probe.rs new file mode 100644 index 0000000000..ef9a5b44a2 --- /dev/null +++ b/crates/gpu/examples/zero_block_probe.rs @@ -0,0 +1,195 @@ +//! Probe: do textures uploaded like egui-wgpu's (TEXTURE_BINDING | COPY_DST, one full +//! `write_texture`) read back exactly what was written? Uploads mostly-transparent images with a +//! few opaque texels, samples every texel in a render pass, reads the target back and counts +//! texels that differ. Between uploads it frees render targets cleared to red, so stale memory is +//! red if the driver hands it back without honouring the copy. +//! +//! cargo run --release -p photocraft-gpu --example zero_block_probe -- [dx12|vulkan|gl] [rounds] [guard] [adapter=] +//! +//! `adapter=` probes the first adapter whose name contains `` (case-insensitive), e.g. +//! `adapter=iris` on a machine that also has a discrete GPU; without it wgpu picks its default adapter. + +use wgpu::util::DeviceExt; + +const SHADER: &str = r" +@group(0) @binding(0) var t: texture_2d; +@vertex fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4 { + let p = array(vec2(-1.0, -3.0), vec2(-1.0, 1.0), vec2(3.0, 1.0)); + return vec4(p[i], 0.0, 1.0); +} +@fragment fn fs(@builtin(position) p: vec4) -> @location(0) vec4 { + return textureLoad(t, vec2(p.xy), 0); +} +"; + +fn main() { + let args: Vec = std::env::args().collect(); + let backends = match args.get(1).map(String::as_str) { + Some("vulkan") => wgpu::Backends::VULKAN, + Some("gl") => wgpu::Backends::GL, + _ => wgpu::Backends::DX12, + }; + let rounds: usize = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(300); + // `guard`: transparent texels are uploaded as (0, 0, 0, 1), as `photocraft_ui_egui::texel_guard` does. + let guard = args.get(3).is_some_and(|a| a == "guard"); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends, ..wgpu::InstanceDescriptor::new_without_display_handle() }); + let adapter = match args.iter().find_map(|a| a.strip_prefix("adapter=")).map(str::to_lowercase) { + Some(want) => { + let adapters = pollster::block_on(instance.enumerate_adapters(backends)); + let names: Vec = adapters.iter().map(|a| a.get_info().name).collect(); + adapters + .into_iter() + .find(|a| a.get_info().name.to_lowercase().contains(&want)) + .unwrap_or_else(|| panic!("no adapter matches {want:?}; found {names:?}")) + } + None => pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).expect("adapter"), + }; + let info = adapter.get_info(); + println!("adapter: {} ({:?}) driver {} {}", info.name, info.backend, info.driver, info.driver_info); + let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).expect("device"); + + let fmt = wgpu::TextureFormat::Rgba8Unorm; + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: None, source: wgpu::ShaderSource::Wgsl(SHADER.into()) }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: None, + layout: None, + vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), compilation_options: Default::default(), buffers: &[] }, + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { format: fmt, blend: None, write_mask: wgpu::ColorWrites::ALL })], + }), + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + multiview_mask: None, + cache: None, + }); + + let mut bad_textures = 0; + let mut bad_texels = 0usize; + for round in 0..rounds { + // Vary the size like the brush previews and icons do. + let (w, h) = (16 + (round as u32 * 37) % 300, 16 + (round as u32 * 13) % 60); + // Free some red render targets first: their memory is what a not-zeroed heap hands back. + for _ in 0..2 { + let rt = device.create_texture(&wgpu::TextureDescriptor { + label: Some("red-rt"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: fmt, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + view_formats: &[], + }); + let view = rt.create_view(&Default::default()); + let mut enc = device.create_command_encoder(&Default::default()); + enc.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &view, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color { r: 1.0, g: 0.03, b: 0.03, a: 1.0 }), store: wgpu::StoreOp::Store }, + })], + ..Default::default() + }); + queue.submit([enc.finish()]); + } + // The upload: transparent except a diagonal band of opaque grey, like a stroke strip. + let mut px = if guard { [0u8, 0, 0, 1].repeat((w * h) as usize) } else { vec![0u8; (w * h * 4) as usize] }; + for y in 0..h { + for x in 0..w { + if (x as i64 * h as i64 / w as i64 - y as i64).abs() < 3 { + let i = ((y * w + x) * 4) as usize; + px[i..i + 4].copy_from_slice(&[200, 200, 200, 255]); + } + } + } + let tex = device.create_texture_with_data( + &queue, + &wgpu::TextureDescriptor { + label: Some("egui-like"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: fmt, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }, + wgpu::util::TextureDataOrder::LayerMajor, + &px, + ); + let target = device.create_texture(&wgpu::TextureDescriptor { + label: Some("target"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: fmt, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + let bind = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: None, + layout: &pipeline.get_bind_group_layout(0), + entries: &[wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&tex.create_view(&Default::default())) }], + }); + let tview = target.create_view(&Default::default()); + let row = (w * 4).div_ceil(256) * 256; + let buf = device.create_buffer(&wgpu::BufferDescriptor { + label: None, + size: u64::from(row * h), + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + let mut enc = device.create_command_encoder(&Default::default()); + { + let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &tview, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::BLUE), store: wgpu::StoreOp::Store }, + })], + ..Default::default() + }); + pass.set_pipeline(&pipeline); + pass.set_bind_group(0, &bind, &[]); + pass.draw(0..3, 0..1); + } + enc.copy_texture_to_buffer( + wgpu::TexelCopyTextureInfo { texture: &target, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyBufferInfo { buffer: &buf, layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: None } }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + queue.submit([enc.finish()]); + buf.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + device.poll(wgpu::PollType::wait_indefinitely()).expect("poll"); + let data = buf.slice(..).get_mapped_range().expect("mapped"); + let mut bad = 0; + let mut first = None; + for y in 0..h { + for x in 0..w { + let got = &data[(y * row + x * 4) as usize..(y * row + x * 4 + 4) as usize]; + let want = &px[((y * w + x) * 4) as usize..((y * w + x) * 4 + 4) as usize]; + if got != want { + bad += 1; + first.get_or_insert((x, y, got.to_vec())); + } + } + } + if bad > 0 { + bad_textures += 1; + bad_texels += bad; + if bad_textures <= 10 { + println!("round {round}: {w}x{h} texture has {bad} wrong texels, first {first:?}"); + } + } + } + println!("{bad_textures}/{rounds} textures read back wrong ({bad_texels} texels)"); +} diff --git a/crates/gpu/src/batch.rs b/crates/gpu/src/batch.rs new file mode 100644 index 0000000000..700ed8361f --- /dev/null +++ b/crates/gpu/src/batch.rs @@ -0,0 +1,215 @@ +use super::convert::{W, words}; +use super::textures::Resident; +use super::{DeviceHealth, F, I, Kernel, Plan, Rect, TILE_SIZE, TileCoord, U, plan, timing}; + +/// The uniform record of a pass. +pub(crate) fn op_words(p: &plan::Pass<'_>, tex: Option<[i32; 4]>, mask: Option<[i32; 4]>, map: Option) -> Vec { + words(&op_values(p, tex, mask, map)) +} + +/// The uniform record of the `fs_layerbatch` pass that draws `p` (whose texture and mask cover +/// `tex` and `mask`) and then `extras`, each an unmasked plain layer's `LayerBlend` pass with the +/// area its texture covers. Layout: see `batch_layer` in compose.wgsl. +pub(crate) fn batch_op_words(p: &plan::Pass<'_>, tex: Option<[i32; 4]>, mask: Option<[i32; 4]>, extras: &[(&plan::Pass<'_>, Option<[i32; 4]>)]) -> Vec { + let mut v = op_values(p, tex, mask, None); + // Word offsets of the record's vec4s holding the extra layers, three each: p0, p1, p2 | p3, + // q0, q1 | q2, q3, q4. + const AT: [usize; 9] = [20, 24, 28, 32, 44, 48, 52, 56, 60]; + if let Some(kind) = v.get_mut(1) { + *kind = I(extras.len().min(AT.len() / 3) as i32); + } + for (e, (x, area)) in extras.iter().take(AT.len() / 3).enumerate() { + let r = area.unwrap_or([0; 4]); + let geo = [r[0] as f32, r[1] as f32, r[2] as f32, r[3] as f32]; + let prm = [x.opacity, x.fill, plan::mode_index(x.mode) as f32, if area.is_some() { 1.0 } else { 0.0 }]; + for (k, vals) in [geo, x.color, prm].into_iter().enumerate() { + let at = AT[e * 3 + k]; + for (c, f) in vals.into_iter().enumerate() { + if let Some(w) = v.get_mut(at + c) { + *w = F(f); + } + } + } + } + words(&v) +} + +/// The words of a pass's uniform record (`Op` in compose.wgsl). +fn op_values(p: &plan::Pass<'_>, tex: Option<[i32; 4]>, mask: Option<[i32; 4]>, map: Option) -> Vec { + let mut flags = p.flags; + let (mut to, mut ts, mut mo, mut ms) = ([0; 2], [0; 2], [0; 2], [0; 2]); + if let Some(r) = tex { + flags |= 4; + to = [r[0], r[1]]; + ts = [r[2], r[3]]; + } + let (mut density, mut mdefault) = (0.0, 1.0); + if let Some(m) = &p.mask { + flags |= 1; + density = m.density; + mdefault = m.default; + if let Some(r) = mask { + flags |= 2; + mo = [r[0], r[1]]; + ms = [r[2], r[3]]; + } + } + if p.gradient { + flags |= 8; + } + let (mpo, mps) = map.map_or(([0, 0], [0, 0]), |r| ([r.x0, r.y0], [r.width() as i32, r.height() as i32])); + let mut v = vec![ + I(plan::mode_index(p.mode)), + I(p.adjust_kind), + U(flags), + U(0), + F(p.opacity), + F(density), + F(mdefault), + F(p.fill), + I(to[0]), + I(to[1]), + I(ts[0]), + I(ts[1]), + I(mo[0]), + I(mo[1]), + I(ms[0]), + I(ms[1]), + ]; + v.extend(p.color.iter().map(|f| F(*f))); + for row in &p.params { + v.extend(row.iter().map(|f| F(*f))); + } + v.extend([I(mpo[0]), I(mpo[1]), I(mps[0]), I(mps[1])]); + v.extend(p.extra.iter().map(|f| F(*f))); + // q0..q4: only `fs_layerbatch` reads them (`batch_op_words`). + v.extend((0..20).map(|_| F(0.0))); + v +} + +/// Most layers one `fs_layerbatch` pass blends: its own and the three more its record holds. +pub(crate) const BATCH: usize = 4; + +/// For each pass, how many passes from it on the executor draws as one `fs_layerbatch` pass +/// (1: the pass alone). A run starts at a `LayerBlend` pass over the whole chunk (no clip) that +/// isn't a gradient (whose parameters use the words the extra layers go in), and continues while +/// the next pass is a `LayerBlend` of an unmasked plain layer (no text gamma, no Dissolve) onto +/// exactly what the previous pass wrote, a slot nothing else reads before it is written again. +/// A run never ends in the slot it reads from. +/// +/// The runs depend on the plan alone, never on the prefix cache: a compiler may give a layer +/// other bits batched than drawn alone (FXC on D3D12 does), and reusing a prefix must stay exact +/// (#5286). The cache captures and resumes only at `run_starts` instead. +pub(crate) fn batch_runs(plan: &Plan<'_>) -> Vec { + let passes = &plan.passes; + let n = passes.len(); + let reads = |p: &plan::Pass<'_>, s: plan::Slot| [p.a, p.b, p.c, p.d].contains(&Some(s)); + // feeds_next[i]: what pass i writes is read by pass i + 1 alone. + let feeds_next: Vec = (0..n) + .map(|i| { + let (Some(this), Some(next)) = (passes.get(i), passes.get(i + 1)) else { return false }; + let dst = this.dst; + if next.a != Some(dst) || [next.b, next.c, next.d].contains(&Some(dst)) { + return false; + } + for later in passes.iter().skip(i + 2) { + // A copy into a slot keeps what lies outside its rect: that is a read too. + if reads(later, dst) || (later.dst == dst && later.kernel == Kernel::CopyRect) { + return false; + } + if later.dst == dst { + return true; + } + } + dst != plan.root + }) + .collect(); + let lead = |p: &plan::Pass<'_>| p.kernel == Kernel::LayerBlend && p.clip.is_none() && !p.gradient; + let extra = |p: &plan::Pass<'_>| lead(p) && p.mask.is_none() && p.flags & plan::F_TEXT_GAMMA == 0 && p.mode != photocraft_color::BlendMode::Dissolve; + (0..n) + .map(|i| { + let Some(first) = passes.get(i).filter(|p| lead(p)) else { return 1 }; + let (mut k, mut best) = (1, 1); + while k < BATCH && feeds_next.get(i + k - 1).copied().unwrap_or(false) && passes.get(i + k).is_some_and(extra) { + k += 1; + if passes.get(i + k - 1).is_some_and(|p| Some(p.dst) != first.a) { + best = k; + } + } + best + }) + .collect() +} + +/// For each pass and the end of the plan, whether the executor draws `runs` (`batch_runs`) so +/// that a pass starts there rather than inside a run. +pub(crate) fn run_starts(runs: &[usize]) -> Vec { + let mut starts = vec![false; runs.len() + 1]; + let mut i = 0; + while let Some(s) = starts.get_mut(i) { + *s = true; + i += runs.get(i).copied().unwrap_or(1).max(1); + } + starts +} + +/// The pipeline that draws `run` (passes `batch_runs` joined): `fs_layerbatch_normal` when every +/// layer blends Normal without text gamma outside Lab documents, else `fs_layerbatch`. +pub(crate) fn batch_kernel(run: &[plan::Pass<'_>]) -> Kernel { + let normal = |p: &plan::Pass<'_>| p.mode == photocraft_color::BlendMode::Normal && p.flags & (plan::F_TEXT_GAMMA | plan::F_LAB) == 0; + if run.iter().all(normal) { Kernel::LayerBatchNormal } else { Kernel::LayerBatch } +} + +/// Canvas rounded out to whole tiles. +pub(crate) fn tile_grid(canvas: Rect) -> Rect { + let t = TILE_SIZE; + Rect::new(canvas.x0.div_euclid(t) * t, canvas.y0.div_euclid(t) * t, (canvas.x1 + t - 1).div_euclid(t) * t, (canvas.y1 + t - 1).div_euclid(t) * t) +} + +pub(crate) fn write_tile(queue: &wgpu::Queue, r: &Resident, c: TileCoord, bytes: &[u8]) { + let tr = c.rect(); + let bpp = r.kind.bytes_per_pixel() as u32; + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &r.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: (tr.x0 - r.region.x0) as u32, y: (tr.y0 - r.region.y0) as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(TILE_SIZE as u32 * bpp), rows_per_image: Some(TILE_SIZE as u32) }, + wgpu::Extent3d { width: TILE_SIZE as u32, height: TILE_SIZE as u32, depth_or_array_layers: 1 }, + ); +} + +/// Submit what `encoder` recorded and continue in a fresh one, then wait for the previous +/// intermediate submit (`last`), so at most two batches of uploads and evicted pages are in +/// flight. The web has no blocking wait and relies on the browser's own scheduling. +pub(crate) fn submit_batch( + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + health: Option<&DeviceHealth>, + timer: Option<&mut timing::GpuTimer>, + last: &mut Option, +) { + let mut done = std::mem::replace(encoder, device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose") })); + if let Some(t) = timer { + // One refresh, several command buffers: the measurement spans all of them. + t.split(&mut done, encoder); + } + let prev = last.replace(queue.submit([done.finish()])); + #[cfg(not(target_arch = "wasm32"))] + if let Some(prev) = prev { + match health { + Some(h) => { + h.wait(device, Some(prev)); + } + None => { + let _ = device.poll(wgpu::PollType::Wait { submission_index: Some(prev), timeout: None }); + } + } + } + #[cfg(target_arch = "wasm32")] + let _ = (prev, health); +} diff --git a/crates/gpu/src/camera_raw.rs b/crates/gpu/src/camera_raw.rs new file mode 100644 index 0000000000..3f1833a54b --- /dev/null +++ b/crates/gpu/src/camera_raw.rs @@ -0,0 +1,418 @@ +//! Camera Raw's per-pixel develop on the GPU, for the dialog's interactive preview (#6916). +//! +//! One fragment pass (`camera_raw.wgsl`) runs the stages that read only their own pixel: white +//! balance and exposure, Contrast without Texture or Clarity, Whites and Blacks, Vibrance and +//! Saturation, the tone curves, the Color Mixer, Color Grading, the vignette and grain. Their +//! constants and tables come from [`photocraft_algo::camera_raw::per_pixel`], which the CPU +//! develop builds its own from, so both evaluate the same maths; the curves and the Whites and +//! Blacks rows are rows of one `R32Float` texture. Settings with a spatial stage (Dehaze, Shadows +//! and Highlights, Texture, Clarity, Detail) have no [`PerPixel`] and stay on the CPU. +//! +//! The pipeline is built on demand ([`CameraRawDevelop::new`]), not with the compositor (#6419). +//! It renders into `Rgba32Float`; [`CameraRawDevelop::supported`] says whether the adapter can. + +use photocraft_algo::camera_raw::{CURVE_LUT, PerPixel, VignetteStyle, WB_STEPS}; + +use crate::Unsupported; + +/// The pass's WGSL source. +pub const CAMERA_RAW_SHADER: &str = include_str!("camera_raw.wgsl"); + +const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba32Float; +/// `Params` in the shader: 19 rows of 16 bytes. +const PARAMS_SIZE: u64 = 19 * 16; +/// Rows of the table texture: parametric, master, red, green, blue curves; Whites, Blacks. +const LUT_ROWS: u32 = 7; +const LUT_WIDTH: u32 = CURVE_LUT as u32; +const BYTES_PER_PIXEL: u32 = 16; + +struct Image { + key: Option, + size: [u32; 2], + texture: wgpu::Texture, + view: wgpu::TextureView, +} + +impl Image { + fn new(device: &wgpu::Device, label: &str, size: [u32; 2], usage: wgpu::TextureUsages) -> Self { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { width: size[0], height: size[1], depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: FORMAT, + usage, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + Image { key: None, size, texture, view } + } +} + +/// The Camera Raw preview pass and its textures (reused while the size stays). +pub struct CameraRawDevelop { + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + params: wgpu::Buffer, + luts: wgpu::Texture, + luts_view: wgpu::TextureView, + source: Option, + target: Option, + #[cfg(not(target_arch = "wasm32"))] + readback: Option, +} + +impl CameraRawDevelop { + /// Whether `adapter` can render the pass's `Rgba32Float` output. + pub fn supported(adapter: &wgpu::Adapter) -> bool { + adapter.get_texture_format_features(FORMAT).allowed_usages.contains(wgpu::TextureUsages::RENDER_ATTACHMENT) + } + + /// Builds the pipeline. A shader or pipeline the driver can't build is an `Err`, so the + /// caller keeps the CPU develop (where errors only arrive asynchronously, on WebGPU, creation + /// is assumed to have worked). + pub fn new(device: &wgpu::Device) -> Result { + match crate::first_error(device, || Self::build(device)) { + (dev, None) => Ok(dev), + (_, Some(e)) => Err(Unsupported(format!("couldn't build the Camera Raw pipeline: {e}"))), + } + } + + fn build(device: &wgpu::Device) -> Self { + let module = device + .create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_camera_raw"), source: wgpu::ShaderSource::Wgsl(CAMERA_RAW_SHADER.into()) }); + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_camera_raw"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { + ty: wgpu::BufferBindingType::Uniform, + has_dynamic_offset: false, + min_binding_size: wgpu::BufferSize::new(PARAMS_SIZE), + }, + count: None, + }, + crate::tex_entry(1), + crate::tex_entry(2), + ], + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("pc_camera_raw"), + bind_group_layouts: &[Some(&layout)], + immediate_size: 0, + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("pc_camera_raw"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: None, + multisample: wgpu::MultisampleState::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some("fs"), + targets: &[Some(wgpu::ColorTargetState { format: FORMAT, blend: None, write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }); + let params = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_camera_raw_params"), + size: PARAMS_SIZE, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let luts = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_camera_raw_luts"), + size: wgpu::Extent3d { width: LUT_WIDTH, height: LUT_ROWS, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::R32Float, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let luts_view = luts.create_view(&Default::default()); + CameraRawDevelop { + pipeline, + layout, + params, + luts, + luts_view, + source: None, + target: None, + #[cfg(not(target_arch = "wasm32"))] + readback: None, + } + } + + /// Records the develop of `source` (`w × h` straight RGBA, display-encoded) with `stages` + /// into `encoder` and returns the `Rgba32Float` texture it renders. The source is uploaded + /// unless the last one had the same `key` and size (`None` always uploads). + #[allow(clippy::too_many_arguments)] + pub fn encode( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + source: &[[f32; 4]], + key: Option, + size: [u32; 2], + stages: &PerPixel, + ) -> Result<&wgpu::Texture, Unsupported> { + check_size(device, source, size)?; + let [w, h] = size; + let usage = wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST; + let src = match self.source.take() { + Some(s) if s.size == size => s, + _ => Image::new(device, "pc_camera_raw_source", size, usage), + }; + let src = self.source.insert(src); + if key.is_none() || src.key != key { + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &src.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytemuck::cast_slice(source), + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * BYTES_PER_PIXEL), rows_per_image: Some(h) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + src.key = key; + } + queue.write_buffer(&self.params, 0, ¶ms(size, stages)); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &self.luts, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytemuck::cast_slice(&tables(stages)), + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(LUT_WIDTH * 4), rows_per_image: Some(LUT_ROWS) }, + wgpu::Extent3d { width: LUT_WIDTH, height: LUT_ROWS, depth_or_array_layers: 1 }, + ); + let target = match self.target.take() { + Some(t) if t.size == size => t, + _ => Image::new(device, "pc_camera_raw_target", size, wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC), + }; + let target = self.target.insert(target); + let group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_camera_raw"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: self.params.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(&src.view) }, + wgpu::BindGroupEntry { binding: 2, resource: wgpu::BindingResource::TextureView(&self.luts_view) }, + ], + }); + { + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("pc_camera_raw"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &target.view, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT), store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, &group, &[]); + pass.draw(0..3, 0..1); + } + Ok(&target.texture) + } + + /// Develops `source` and reads the result back (blocking; native only, the web has no + /// blocking wait). GPU errors and a lost device are `Err`; `health`, when the device is + /// watched, records a loss. + #[cfg(not(target_arch = "wasm32"))] + #[allow(clippy::too_many_arguments)] + pub fn develop( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + health: Option<&crate::DeviceHealth>, + source: &[[f32; 4]], + key: Option, + size: [u32; 2], + stages: &PerPixel, + ) -> Result, Unsupported> { + if let Some(f) = health.and_then(crate::DeviceHealth::fault) { + return Err(Unsupported(f.to_string())); + } + let (result, error) = crate::first_error(device, || self.develop_inner(device, queue, health, source, key, size, stages)); + match error { + Some(e) => { + // A half-done upload must not be taken for the cached source next time. + self.source = None; + Err(Unsupported(format!("GPU Camera Raw develop failed: {e}"))) + } + None => result, + } + } + + #[cfg(not(target_arch = "wasm32"))] + #[allow(clippy::too_many_arguments)] + fn develop_inner( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + health: Option<&crate::DeviceHealth>, + source: &[[f32; 4]], + key: Option, + size: [u32; 2], + stages: &PerPixel, + ) -> Result, Unsupported> { + check_size(device, source, size)?; + let [w, h] = size; + let row = (w * BYTES_PER_PIXEL).div_ceil(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT) * wgpu::COPY_BYTES_PER_ROW_ALIGNMENT; + let bytes = u64::from(row) * u64::from(h); + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_camera_raw") }); + self.encode(device, queue, &mut encoder, source, key, size, stages)?; + let Some(target) = self.target.as_ref().map(|t| &t.texture) else { return Err(Unsupported("no Camera Raw target".into())) }; + let buffer = match self.readback.take() { + Some(b) if b.size() == bytes => b, + _ => device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_camera_raw_readback"), + size: bytes, + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }), + }; + encoder.copy_texture_to_buffer( + wgpu::TexelCopyTextureInfo { texture: target, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyBufferInfo { buffer: &buffer, layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: Some(h) } }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + let index = queue.submit([encoder.finish()]); + buffer.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + match health { + Some(hl) => { + if !hl.wait(device, Some(index)) { + return Err(Unsupported(hl.fault().map_or_else(|| "GPU device lost".into(), |f| f.to_string()))); + } + } + None => { + device.poll(wgpu::PollType::Wait { submission_index: Some(index), timeout: None }).map_err(|e| Unsupported(format!("GPU wait failed: {e}")))?; + } + } + let mut out = Vec::with_capacity(source.len()); + { + let data = buffer.slice(..).get_mapped_range().map_err(|e| Unsupported(format!("GPU readback failed: {e:?}")))?; + let width = w as usize * BYTES_PER_PIXEL as usize; + for y in 0..h as usize { + let start = y * row as usize; + let line = data.get(start..start + width).ok_or_else(|| Unsupported("short GPU readback".into()))?; + out.extend(bytemuck::pod_collect_to_vec::(line)); + } + } + buffer.unmap(); + self.readback = Some(buffer); + Ok(out) + } +} + +fn check_size(device: &wgpu::Device, source: &[[f32; 4]], [w, h]: [u32; 2]) -> Result<(), Unsupported> { + let max = device.limits().max_texture_dimension_2d; + if w == 0 || h == 0 || w > max || h > max { + return Err(Unsupported(format!("a {w}×{h} Camera Raw preview doesn't fit the GPU's {max} px textures"))); + } + if source.len() as u64 != u64::from(w) * u64::from(h) { + return Err(Unsupported(format!("{} Camera Raw preview pixels for {w}×{h}", source.len()))); + } + Ok(()) +} + +/// Flags of `Params.flags`, in the shader's bit order. +fn flags(s: &PerPixel) -> u32 { + let wb = s.whites_blacks.as_ref(); + let on = [ + s.gains.is_some(), + s.contrast.is_some(), + wb.is_some_and(|w| w.whites.is_some()), + wb.is_some_and(|w| w.blacks.is_some()), + wb.is_some_and(|w| w.per_channel), + s.presence.is_some(), + s.parametric.is_some(), + s.point[0].is_some(), + s.point[1].is_some(), + s.point[2].is_some(), + s.point[3].is_some(), + s.hsl.is_some(), + s.grade.is_some(), + s.vignette.is_some(), + s.grain.is_some(), + s.clamp, + ]; + on.iter().enumerate().fold(0, |f, (bit, on)| if *on { f | 1 << bit } else { f }) +} + +/// The shader's `Params` for a `size` image. +fn params(size: [u32; 2], s: &PerPixel) -> Vec { + let g = s.gains.unwrap_or([1.0; 3]); + let presence = s.presence.unwrap_or_default(); + let hsl = s.hsl.unwrap_or_default(); + let grade = s.grade.as_ref(); + let vig = s.vignette.as_ref(); + let grain = s.grain.as_ref(); + let mut out: Vec = Vec::with_capacity(PARAMS_SIZE as usize); + out.extend([size[0], size[1], flags(s)].iter().flat_map(|v| v.to_le_bytes()).chain(s.encoding.to_le_bytes())); + let mut f = |v: [f32; 4]| out.extend(v.iter().flat_map(|x| x.to_le_bytes())); + f([g[0], g[1], g[2], s.contrast.unwrap_or(0.0)]); + f([presence[0], presence[1], 0.0, 0.0]); + for row in hsl { + f([row[0], row[1], row[2], row[3]]); + f([row[4], row[5], row[6], row[7]]); + } + for k in 0..4 { + let (t, l) = grade.map_or(([0.0; 3], 0.0), |gr| (gr.tints[k], gr.lums[k])); + f([t[0], t[1], t[2], l]); + } + f(grade.map_or([0.0; 4], |gr| [gr.blend, gr.balance, 0.0, 0.0])); + let style = |v: VignetteStyle| match v { + VignetteStyle::HighlightPriority => 0.0, + VignetteStyle::ColorPriority => 1.0, + VignetteStyle::PaintOverlay => 2.0, + }; + f(vig.map_or([0.0, 0.0, 0.0, 2.0], |v| [v.amount, v.highlights, style(v.style), v.power])); + f(vig.map_or([0.0, 0.0, 1.0, 1.0], |v| [v.centre[0], v.centre[1], v.axes[0], v.axes[1]])); + f(vig.map_or([0.0; 4], |v| [v.edges[0], v.edges[1], 0.0, 0.0])); + f(grain.map_or([1.0, 0.0, 0.0, 1.0], |gr| [gr.cell, gr.roughness, gr.amount, gr.scale])); + let seed = grain.map_or(0, |gr| gr.seed); + let fine = seed.wrapping_add(7); + out.extend([seed as u32, (seed >> 32) as u32, fine as u32, (fine >> 32) as u32].iter().flat_map(|v| v.to_le_bytes())); + out +} + +/// The table texture's rows (see [`LUT_ROWS`]); rows of stages that are off stay zero. +fn tables(s: &PerPixel) -> Vec { + let mut t = vec![0.0f32; (LUT_WIDTH * LUT_ROWS) as usize]; + let mut put = |row: usize, v: &[f32]| { + let start = row * LUT_WIDTH as usize; + let n = v.len().min(LUT_WIDTH as usize); + if let (Some(dst), Some(src)) = (t.get_mut(start..start + n), v.get(..n)) { + dst.copy_from_slice(src); + } + }; + if let Some(l) = &s.parametric { + put(0, l); + } + for (k, l) in s.point.iter().enumerate() { + if let Some(l) = l { + put(1 + k, l); + } + } + if let Some(wb) = &s.whites_blacks { + if let Some(r) = &wb.whites { + put(5, r); + } + if let Some(r) = &wb.blacks { + put(6, r); + } + } + t +} + +// The Whites and Blacks rows fit a table row. +const _: () = assert!(WB_STEPS <= CURVE_LUT); diff --git a/crates/gpu/src/camera_raw.wgsl b/crates/gpu/src/camera_raw.wgsl new file mode 100644 index 0000000000..abad7cbbc1 --- /dev/null +++ b/crates/gpu/src/camera_raw.wgsl @@ -0,0 +1,416 @@ +// Camera Raw's per-pixel develop stages (`photocraft_algo::camera_raw::develop`, constants from +// `camera_raw::per_pixel`), one fragment per pixel. Each function mirrors the CPU code it is +// named after, in the same order of operations; `camera_raw.rs` holds the bindings. + +struct Params { + size: vec2, + flags: u32, + // The document's transfer curve: 0 for sRGB's, else a power-law gamma. + encoding: f32, + // White balance gains (rgb), Contrast's strength (w). + gains: vec4, + // Vibrance, Saturation. + presence: vec4, + // Color Mixer bands 0-3 and 4-7: hue, saturation, luminance. + hsl: array, 6>, + // Color Grading wheels: tint (rgb), luminance (w). + tints: array, 4>, + // Blending, Balance. + grade: vec4, + // [amount, highlights, style, power], [centre, axes], [edges]. + vignette: array, 3>, + // Cell, roughness, amount, full-resolution pixels per pixel. + grain: vec4, + // The two octaves' seeds as (low, high) words. + seeds: vec4, +} + +@group(0) @binding(0) var P: Params; +@group(0) @binding(1) var src: texture_2d; +// R32Float rows of `LUT_WIDTH`: parametric, master, red, green, blue curves; Whites, Blacks rows. +@group(0) @binding(2) var luts: texture_2d; + +const GAINS: u32 = 0u; +const CONTRAST: u32 = 1u; +const WHITES: u32 = 2u; +const BLACKS: u32 = 3u; +const PER_CHANNEL: u32 = 4u; +const PRESENCE: u32 = 5u; +const PARAMETRIC: u32 = 6u; +const MASTER: u32 = 7u; +const RED: u32 = 8u; +const HSL: u32 = 11u; +const GRADE: u32 = 12u; +const VIGNETTE: u32 = 13u; +const GRAIN: u32 = 14u; +const CLAMP: u32 = 15u; + +const CURVE_LUT: i32 = 1024; +const WB_STEPS: i32 = 33; +const TAU: f32 = 6.2831855; + +struct VOut { + @builtin(position) pos: vec4, +} + +@vertex +fn vs(@builtin(vertex_index) vi: u32) -> VOut { + let uv = vec2(f32((vi << 1u) & 2u), f32(vi & 2u)); + return VOut(vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0)); +} + +fn on(flag: u32) -> bool { + return (P.flags & (1u << flag)) != 0u; +} + +fn luma(c: vec3) -> f32 { + return 0.2126 * c.x + 0.7152 * c.y + 0.0722 * c.z; +} + +fn smoothstep_(e0: f32, e1: f32, x: f32) -> f32 { + let t = clamp((x - e0) / (e1 - e0), 0.0, 1.0); + return t * t * (3.0 - 2.0 * t); +} + +fn rem_euclid(x: f32, m: f32) -> f32 { + let r = x % m; + return select(r, r + m, r < 0.0); +} + +fn srgb_to_linear(v: f32) -> f32 { + if v <= 0.04045 { + return v / 12.92; + } + return pow((v + 0.055) / 1.055, 2.4); +} + +fn linear_to_srgb(v: f32) -> f32 { + if v <= 0.0031308 { + return v * 12.92; + } + return 1.055 * pow(v, 1.0 / 2.4) - 0.055; +} + +fn decode(v: f32) -> f32 { + if P.encoding > 0.0 { + return pow(max(v, 0.0), P.encoding); + } + return srgb_to_linear(v); +} + +fn encode(v: f32) -> f32 { + if P.encoding > 0.0 { + return pow(max(v, 0.0), 1.0 / P.encoding); + } + return linear_to_srgb(v); +} + +fn lut_at(row: i32, i: i32) -> f32 { + return textureLoad(luts, vec2(i, row), 0).r; +} + +// `lut_eval`: linear inside 0..1, extended linearly outside (float documents). +fn lut_eval(row: i32, v: f32) -> f32 { + if v < 0.0 { + return v + lut_at(row, 0); + } + if v > 1.0 { + return lut_at(row, CURVE_LUT - 1) + (v - 1.0); + } + let x = v * f32(CURVE_LUT - 1); + let i = min(i32(x), CURVE_LUT - 2); + let t = x - f32(i); + let a = lut_at(row, i); + return a + (lut_at(row, i + 1) - a) * t; +} + +// `whites_blacks::curve` on one row. +fn wb_curve(row: i32, v: f32) -> f32 { + if v >= 1.0 { + return 1.0; + } + let p = max(v, 0.0) * f32(WB_STEPS - 1); + let j = min(i32(p), WB_STEPS - 2); + let s = p - f32(j); + let a = lut_at(row, j); + return a + (lut_at(row, j + 1) - a) * s; +} + +fn grey(v: f32) -> f32 { + var g = v; + if on(WHITES) { + g = wb_curve(5, g); + } + if on(BLACKS) { + g = wb_curve(6, g); + } + return g; +} + +// `whites_blacks::apply`. +fn whites_blacks(q: vec3) -> vec3 { + let lin = vec3(decode(q.x), decode(q.y), decode(q.z)); + var out = q; + if on(PER_CHANNEL) { + for (var c = 0; c < 3; c++) { + let over = max(lin[c] - 1.0, 0.0); + out[c] = encode(srgb_to_linear(grey(linear_to_srgb(min(lin[c], 1.0)))) + over); + } + return out; + } + let y = max(luma(lin), 0.0); + if y <= 1e-7 { + return q; + } + let y2 = srgb_to_linear(grey(linear_to_srgb(min(y, 1.0)))) + max(y - 1.0, 0.0); + let k = y2 / y; + for (var c = 0; c < 3; c++) { + out[c] = encode(lin[c] * k); + } + return out; +} + +fn rgb_to_hsv(c: vec3) -> vec3 { + let mx = max(max(c.x, c.y), c.z); + let mn = min(min(c.x, c.y), c.z); + let d = mx - mn; + var h = 0.0; + if d <= 1e-9 { + h = 0.0; + } else if mx == c.x { + h = 60.0 * rem_euclid((c.y - c.z) / d, 6.0); + } else if mx == c.y { + h = 60.0 * ((c.z - c.x) / d + 2.0); + } else { + h = 60.0 * ((c.x - c.y) / d + 4.0); + } + return vec3(h, select(0.0, d / mx, mx > 1e-9), mx); +} + +fn hsv_to_rgb(hue: f32, s: f32, v: f32) -> vec3 { + let h = rem_euclid(hue, 360.0) / 60.0; + let c = v * s; + let x = c * (1.0 - abs((h % 2.0) - 1.0)); + var rgb: vec3; + switch u32(h) { + case 0u: { rgb = vec3(c, x, 0.0); } + case 1u: { rgb = vec3(x, c, 0.0); } + case 2u: { rgb = vec3(0.0, c, x); } + case 3u: { rgb = vec3(0.0, x, c); } + case 4u: { rgb = vec3(x, 0.0, c); } + default: { rgb = vec3(c, 0.0, x); } + } + let m = v - c; + return rgb + vec3(m); +} + +const HSL_BANDS = array(0.0, 30.0, 60.0, 120.0, 180.0, 240.0, 270.0, 300.0); + +fn band(k: i32, row: i32) -> f32 { + return P.hsl[row * 2 + k / 4][k % 4]; +} + +// `band_weights` folded into the three sums. +fn hsl_shift(h: f32) -> vec3 { + var bands = HSL_BANDS; + var d = vec3(0.0); + for (var k = 0; k < 8; k++) { + let c = bands[k]; + let prev = bands[(k + 7) % 8]; + let next = bands[(k + 1) % 8]; + let diff = ((h - c + 540.0) % 360.0) - 180.0; + let span = select(rem_euclid(c - prev, 360.0), rem_euclid(next - c, 360.0), diff >= 0.0); + let w = max(1.0 - abs(diff) / max(span, 1e-3), 0.0); + d += w * vec3(band(k, 0), band(k, 1), band(k, 2)); + } + return d; +} + +// `photo_util::hash01`: 64-bit words as (low, high). +fn mul_wide(a: u32, b: u32) -> vec2 { + let a0 = a & 0xffffu; + let a1 = a >> 16u; + let b0 = b & 0xffffu; + let b1 = b >> 16u; + let p00 = a0 * b0; + let p01 = a0 * b1; + let p10 = a1 * b0; + let p11 = a1 * b1; + let mid = (p00 >> 16u) + (p01 & 0xffffu) + (p10 & 0xffffu); + return vec2((p00 & 0xffffu) | (mid << 16u), p11 + (p01 >> 16u) + (p10 >> 16u) + (mid >> 16u)); +} + +fn mul64(a: vec2, b: vec2) -> vec2 { + let w = mul_wide(a.x, b.x); + return vec2(w.x, w.y + a.x * b.y + a.y * b.x); +} + +fn shr64(z: vec2, n: u32) -> vec2 { + return vec2((z.x >> n) | (z.y << (32u - n)), z.y >> n); +} + +fn i64_(v: i32) -> vec2 { + return vec2(bitcast(v), select(0u, 0xffffffffu, v < 0)); +} + +fn hash01(x: i32, y: i32, seed: vec2) -> f32 { + var z = mul64(i64_(x), vec2(0x7f4a7c15u, 0x9e3779b9u)) ^ mul64(i64_(y), vec2(0x27d4eb4fu, 0xc2b2ae3du)) ^ mul64(seed, vec2(0x9e3779f9u, 0x165667b1u)); + z = mul64(z ^ shr64(z, 30u), vec2(0x1ce4e5b9u, 0xbf58476du)); + z = mul64(z ^ shr64(z, 27u), vec2(0x133111ebu, 0x94d049bbu)); + z = z ^ shr64(z, 31u); + return f32(z.y >> 8u) / 16777216.0; +} + +fn value_noise(x: f32, y: f32, seed: vec2) -> f32 { + let xi = floor(x); + let yi = floor(y); + let tx = x - xi; + let ty = y - yi; + let sx = tx * tx * (3.0 - 2.0 * tx); + let sy = ty * ty * (3.0 - 2.0 * ty); + let ix = i32(xi); + let iy = i32(yi); + let h00 = hash01(ix, iy, seed); + let h10 = hash01(ix + 1, iy, seed); + let h01 = hash01(ix, iy + 1, seed); + let h11 = hash01(ix + 1, iy + 1, seed); + let top = h00 + (h10 - h00) * sx; + let bot = h01 + (h11 - h01) * sx; + return top + (bot - top) * sy; +} + +@fragment +fn fs(@builtin(position) pos: vec4) -> @location(0) vec4 { + let p = vec2(floor(pos.xy)); + let q = textureLoad(src, p, 0); + var c = q.rgb; + // 1. White balance and exposure. + if on(GAINS) { + for (var i = 0; i < 3; i++) { + c[i] = linear_to_srgb(max(srgb_to_linear(c[i]) * P.gains[i], 0.0)); + } + } + // 3. Contrast (without Texture and Clarity). + if on(CONTRAST) { + let l0 = max(luma(c), 0.0); + let lc = min(l0, 1.0); + var lv = lc; + let xx = clamp(lv, 0.0, 1.0); + lv += -P.gains.w * sin(TAU * xx) / TAU; + lv = max(lv, 0.0) + (l0 - lc); + if l0 > 1e-5 { + c = c * (lv / l0); + } else { + c = c + vec3(lv - l0); + } + } + // 3b. Whites and Blacks. + if on(WHITES) || on(BLACKS) { + c = whites_blacks(c); + } + // 4. Vibrance and Saturation. + if on(PRESENCE) { + let l = luma(c); + let mx = max(max(c.x, c.y), c.z); + let mn = min(min(c.x, c.y), c.z); + let sat = select(0.0, (mx - mn) / mx, mx > 1e-6); + let hue = rgb_to_hsv(c).x; + let skin = clamp(1.0 - (abs(hue - 25.0) / 25.0), 0.0, 1.0) * 0.5; + let k = 1.0 + P.presence.y + P.presence.x * (1.0 - sat) * (1.0 - skin); + c = vec3(l) + (c - vec3(l)) * max(k, 0.0); + } + // 5. Tone curves. + if on(PARAMETRIC) { + c = vec3(lut_eval(0, c.x), lut_eval(0, c.y), lut_eval(0, c.z)); + } + if on(MASTER) { + c = vec3(lut_eval(1, c.x), lut_eval(1, c.y), lut_eval(1, c.z)); + } + for (var i = 0; i < 3; i++) { + if on(RED + u32(i)) { + c[i] = lut_eval(2 + i, c[i]); + } + } + // 6. Color Mixer. + if on(HSL) { + let hsv = rgb_to_hsv(max(c, vec3(0.0))); + let s = hsv.y; + if s > 1e-4 { + let d = hsl_shift(hsv.x); + let nh = hsv.x + d.x / 100.0 * 30.0; + let ns = clamp(s * (1.0 + d.y / 100.0), 0.0, 1.0); + var out = hsv_to_rgb(nh, ns, hsv.z); + let l0 = luma(c); + let l1 = luma(out); + if l1 > 1e-6 { + out = out * l0 / l1; + } + let k = exp2(d.z / 100.0 * 1.0 * s); + c = out * k; + } + } + // 7. Color Grading. + if on(GRADE) { + let l = clamp(luma(c), 0.0, 1.0); + let blend = P.grade.x; + let bal = P.grade.y; + let sh = 1.0 - smoothstep_(0.0, 0.5 + bal + blend, l); + let hi = smoothstep_(0.5 + bal - blend, 1.0, l); + let mid = max(1.0 - sh - hi, 0.0); + let masks = vec4(sh, mid, hi, 1.0); + for (var k = 0; k < 4; k++) { + c += masks[k] * P.tints[k].xyz; + if P.tints[k].w != 0.0 { + c = c * exp2(masks[k] * P.tints[k].w / 100.0 * 0.6); + } + } + } + // 9. Vignette. + if on(VIGNETTE) { + let amt = P.vignette[0].x; + let hl = P.vignette[0].y; + let style = u32(P.vignette[0].z); + let pw = P.vignette[0].w; + let centre = P.vignette[1].xy; + let axes = P.vignette[1].zw; + let dx = abs((f32(p.x) + 0.5 - centre.x) / axes.x); + let dy = abs((f32(p.y) + 0.5 - centre.y) / axes.y); + let r = pow(pow(dx, pw) + pow(dy, pw), 1.0 / pw); + let m = smoothstep_(P.vignette[2].x, P.vignette[2].y, r); + if m > 0.0 { + let l = clamp(luma(c), 0.0, 1.0); + if style == 2u { + let goal = select(1.0, 0.0, amt < 0.0); + let k = m * abs(amt); + c += (vec3(goal) - c) * k; + } else { + var k = exp2(amt * 2.0 * m); + if amt < 0.0 && hl > 0.0 { + let protect = smoothstep_(0.6, 1.0, l) * hl; + k += (1.0 - k) * protect; + } + if style == 1u { + let nl = l * k; + c += vec3(nl - l); + } else { + c = c * k; + } + } + } + } + // 9. Grain, in full-resolution coordinates. + if on(GRAIN) { + let cell = P.grain.x; + let rough = P.grain.y; + let fx = f32(p.x) * P.grain.w / cell; + let fy = f32(p.y) * P.grain.w / cell; + let n = value_noise(fx, fy, P.seeds.xy) * (1.0 - rough) + (value_noise(fx * 2.3, fy * 2.3, P.seeds.zw) - 0.0) * rough; + let l = clamp(luma(c), 0.0, 1.0); + let g = (n - 0.5) * 2.0 * P.grain.z * (0.4 + 2.4 * l * (1.0 - l)); + c += vec3(g); + } + if on(CLAMP) { + c = clamp(c, vec3(0.0), vec3(1.0)); + } + return vec4(c, q.a); +} diff --git a/crates/gpu/src/cells.rs b/crates/gpu/src/cells.rs new file mode 100644 index 0000000000..ab3c451bed --- /dev/null +++ b/crates/gpu/src/cells.rs @@ -0,0 +1,523 @@ +use super::batch::{batch_kernel, batch_op_words, op_words, submit_batch}; +use super::consts::{FLUSH_OPS, MAP32, STRIDE}; +use super::convert::{convert_tile, convert_tiles, keep_overlap, put_tiles, words}; +use super::kit::{Bound, FrameRes, texture_group, uniform_group}; +use super::paging::cell_rect; +use super::textures::{CachedPattern, Cell, ChunkOut, ResKey, Resident, ResidentRef, Stats, Tex, TexKind}; +use super::{Arc, Compositor, DocId, HashMap, I, Kernel, Pattern, Plan, Rect, Role, Surface, TILE_SIZE, TileCoord, plan}; + +impl Compositor { + /// Bring the pages of `cell` that `plan` samples up to date and resolve its effect maps. + #[allow(clippy::too_many_arguments)] + fn bind_cell( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: DocId, + plan: &Plan<'_>, + paged: &[bool], + cell: Cell, + fr: &FrameRes, + first_pass: usize, + stats: &mut Stats, + ) -> Bound { + let cr = cell_rect(cell, self.page); + let mut views: Vec<(Option, Option)> = Vec::with_capacity(plan.passes.len()); + let mut keys: Vec = Vec::new(); + let mut maps = Vec::with_capacity(plan.passes.len()); + for (i, p) in plan.passes.iter().enumerate() { + if i < first_pass { + views.push((None, None)); + maps.push(None); + continue; + } + let (ta, ma) = fr.areas.get(i).copied().unwrap_or((Rect::EMPTY, Rect::EMPTY)); + let tex = + p.tex.as_ref().and_then(|t| self.sync(device, queue, encoder, doc, (t.layer, t.role, cell), t.surface.get(), ta.intersect(&cr), stats)).map( + |(k, r)| { + keys.push(k); + (keys.len() - 1, r) + }, + ); + let mask = p + .mask + .as_ref() + .and_then(|m| self.sync(device, queue, encoder, doc, (m.layer, Role::Mask, cell), m.surface.get(), ma.intersect(&cr), stats)) + .map(|(k, r)| { + keys.push(k); + (keys.len() - 1, r) + }); + views.push((tex, mask)); + maps.push(p.map.and_then(|m| { + let f = plan.fx.get(m.fx)?; + let key = (f.layer.id, paged.get(m.fx).copied().unwrap_or(false).then_some(cell)); + let e = self.fx.get(&key)?; + if m.item == plan::SHAPE_MAP { + return Some((e.shape.view.clone(), e.region)); + } + let t = e.progs.get(m.item)?.maps.get(m.map)?.as_ref()?; + Some((t.view.clone(), e.region)) + })); + } + Bound { views, keys, maps } + } + + /// Run `plan` (drawn as `runs`, its `batch_runs`) over the `chunks` of one page cell + /// (document rects, at most a chunk square), handing each finished chunk to `sink`. + #[allow(clippy::too_many_arguments)] + pub(crate) fn run_cell( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: DocId, + plan: &Plan<'_>, + runs: &[usize], + paged: &[bool], + cell: Cell, + fr: &FrameRes, + chunks: &[Rect], + checkpoint: Option, + stats: &mut Stats, + sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), + // The previous intermediate submit, when this refresh may submit in between. + batch: &mut Option<&mut Option>, + ) { + if chunks.is_empty() { + return; + } + // Chunks may hold different checkpoints: bind from the lowest one any of them resumes at. + let first_pass = checkpoint + .and_then(|_| chunks.iter().map(|c| self.prefix.restore_point(*c).map(|r| r.pass_end)).try_fold(usize::MAX, |m, p| Some(m.min(p?)))) + .unwrap_or(0); + let bound = self.bind_cell(device, queue, encoder, doc, plan, paged, cell, fr, first_pass, stats); + // A page couldn't be created: draw nothing with the pages missing (the caller ends the refresh). + if self.layer_alloc.is_some() { + return; + } + + // Chunk pool. + while self.pool.len() < plan.slots as usize { + let t = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_compose_chunk"), + size: wgpu::Extent3d { width: self.chunk, height: self.chunk, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: self.acc_format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let v = t.create_view(&Default::default()); + self.pool.push((t, v)); + } + + // Uniforms: chunk records, then one record per pass. encode_scoped preflight checked + // the length, device capacity and dynamic offsets before any GPU work for this frame. + let op_base = chunks.len() as u64 * STRIDE; + let mut data = vec![0u8; (op_base + plan.passes.len() as u64 * STRIDE) as usize]; + for (i, c) in chunks.iter().enumerate() { + let w = words(&[I(c.x0), I(c.y0), I(c.width() as i32), I(c.height() as i32)]); + data[i * STRIDE as usize..][..w.len()].copy_from_slice(&w); + } + for (i, p) in plan.passes.iter().enumerate() { + if i < first_pass { + continue; + } + let (tex, mask) = bound.views[i]; + let map = bound.maps[i].as_ref().map(|(_, r)| *r); + let run = runs.get(i).copied().unwrap_or(1); + let w = if run > 1 { + let extras: Vec<_> = (i + 1..i + run).filter_map(|j| Some((plan.passes.get(j)?, bound.views.get(j).and_then(|v| v.0).map(|t| t.1)))).collect(); + batch_op_words(p, tex.map(|t| t.1), mask.map(|m| m.1), &extras) + } else { + op_words(p, tex.map(|t| t.1), mask.map(|m| m.1), map) + }; + let off = (op_base + i as u64 * STRIDE) as usize; + data[off..][..w.len()].copy_from_slice(&w); + } + let ubuf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_compose_uniforms"), + size: data.len() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + queue.write_buffer(&ubuf, 0, &data); + let bg0 = uniform_group(device, &self.kit.bgl0, &ubuf); + + // Per-pass texture bind groups (chunk-independent: pool slots are reused per chunk). + let dummy = &self.kit.dummy; + let resident_views: Vec<&wgpu::TextureView> = bound.keys.iter().map(|k| self.residents.get(k).map_or(dummy, |r| &r.view)).collect(); + let mut bg1 = Vec::with_capacity(plan.passes.len()); + for (i, p) in plan.passes.iter().enumerate() { + if i < first_pass || p.kernel.entry().is_none() { + bg1.push(None); + continue; + } + let slot = |s: Option| s.map_or(dummy, |s| &self.pool[s as usize].1); + let (tex, mask) = &bound.views[i]; + let tv = tex.map_or(slot(p.d), |(k, _)| resident_views[k]); + let mv = mask.map_or(dummy, |(k, _)| resident_views[k]); + let lv = fr.luts.get(i).and_then(Option::as_ref).map_or(dummy, |(_, v)| v); + let map = bound.maps[i].as_ref().map_or(dummy, |(v, _)| v); + let pat = fr.patterns.get(i).and_then(Option::as_ref).unwrap_or(dummy); + let run = runs.get(i).copied().unwrap_or(1); + if run > 1 { + // The extra layers' pixels in tex_b, tex_c and map_tex (`batch_layer`). + let extra = |e: usize| bound.views.get(i + e).and_then(|v| v.0).map_or(dummy, |(k, _)| resident_views[k]); + bg1.push(Some(texture_group(device, &self.kit.bgl1, &[slot(p.a), extra(1), tv, mv, lv, extra(2), extra(3), pat]))); + continue; + } + bg1.push(Some(texture_group(device, &self.kit.bgl1, &[slot(p.a), slot(p.b), tv, mv, lv, slot(p.c), map, pat]))); + } + + for (ci, c) in chunks.iter().enumerate() { + let mut resume = 0; + if let Some(cp) = checkpoint.and_then(|_| self.prefix.restore_point(*c)) + && let Some((dst, _)) = self.pool.get(cp.slot as usize) + && self.prefix.copy_out(encoder, *c, dst) + { + resume = cp.pass_end; + stats.prefix_hits = stats.prefix_hits.saturating_add(1); + stats.prefix_passes_skipped = stats.prefix_passes_skipped.saturating_add(resume); + } + // Passes drawn inside the previous `fs_layerbatch` pass. + let mut batched_to = 0; + for (i, p) in plan.passes.iter().enumerate() { + if i < resume || i < batched_to { + continue; + } + // Chunk textures, uniforms and bind groups outlive the submit, and the queue + // keeps the order, so a chunk may continue in the next command buffer. + if self.recorded >= FLUSH_OPS + && let Some(last) = batch.as_deref_mut() + { + submit_batch(device, queue, encoder, self.health.as_ref(), self.timer.as_mut(), last); + self.staged = 0; + self.recorded = 0; + stats.flushes += 1; + if self.fault().is_some() { + return; + } + } + // Also after resuming from a lower checkpoint: the chunk moves up to this one. + if resume < i + && let Some(cp) = checkpoint.filter(|cp| cp.pass_end == i) + && let Some((src, _)) = self.pool.get(cp.slot as usize) + { + self.prefix.capture(device, encoder, *c, src, cp); + } + let scissor = match p.clip { + Some(clip) => { + let r = clip.intersect(c); + if r.is_empty() { + continue; + } + Rect::new(r.x0 - c.x0, r.y0 - c.y0, r.x1 - c.x0, r.y1 - c.y0) + } + None => Rect::new(0, 0, c.width() as i32, c.height() as i32), + }; + match p.kernel { + Kernel::CopyRect | Kernel::CopyFull => { + // The planner always gives copies a source. + let Some(a) = p.a else { continue }; + let src = &self.pool[a as usize].0; + let dst = &self.pool[p.dst as usize].0; + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { + texture: src, + mip_level: 0, + origin: wgpu::Origin3d { x: scissor.x0 as u32, y: scissor.y0 as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::TexelCopyTextureInfo { + texture: dst, + mip_level: 0, + origin: wgpu::Origin3d { x: scissor.x0 as u32, y: scissor.y0 as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::Extent3d { width: scissor.width(), height: scissor.height(), depth_or_array_layers: 1 }, + ); + self.recorded += 1; + continue; + } + _ => {} + } + self.recorded += 1; + // A run draws as one pass into the slot its last layer writes. + let run = runs.get(i).copied().unwrap_or(1); + let (kernel, dst) = match plan.passes.get(i + run - 1) { + Some(last) if run > 1 => { + batched_to = i + run; + stats.layers_batched = stats.layers_batched.saturating_add(run - 1); + let kernel = plan.passes.get(i..i + run).map_or(Kernel::LayerBatch, batch_kernel); + if kernel == Kernel::LayerBatchNormal { + stats.normal_batches = stats.normal_batches.saturating_add(1); + } + (kernel, last.dst) + } + _ => (p.kernel, p.dst), + }; + let target = &self.pool[dst as usize].1; + let clear = p.kernel == Kernel::Clear; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("pc_compose_pass"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { + load: if clear { wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT) } else { wgpu::LoadOp::Load }, + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + if clear { + continue; + } + pass.set_pipeline(&self.kit.pipelines[&(kernel, self.acc_format)]); + pass.set_scissor_rect(scissor.x0 as u32, scissor.y0 as u32, scissor.width(), scissor.height()); + pass.set_bind_group(0, &bg0, &[(ci as u64 * STRIDE) as u32, (op_base + i as u64 * STRIDE) as u32]); + pass.set_bind_group(1, bg1[i].as_ref(), &[]); + pass.draw(0..3, 0..1); + } + if resume < plan.passes.len() + && let Some(cp) = checkpoint.filter(|cp| cp.pass_end == plan.passes.len()) + && let Some((src, _)) = self.pool.get(cp.slot as usize) + { + self.prefix.capture(device, encoder, *c, src, cp); + } + let (t, v) = &self.pool[plan.root as usize]; + sink(encoder, ChunkOut { rect: *c, texture: t, view: v }); + } + } + + /// Upload changed tiles of `surface` within `region` (its tile area inside one page cell); + /// returns the resident key and its region (x, y, w, h). + #[allow(clippy::too_many_arguments)] + fn sync( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: DocId, + key: ResKey, + surface: &Surface, + region: Rect, + stats: &mut Stats, + ) -> Option<(ResKey, [i32; 4])> { + // An earlier page of this cell didn't fit: the refresh ends after the cell anyway. + if region.is_empty() || self.layer_alloc.is_some() { + return None; + } + let format = surface.format(); + let kind = TexKind::for_surface(key.1, format); + let cmyk = if format.mode == photocraft_color::ColorMode::Cmyk { self.cmyk } else { 0 }; + let default_pixel = surface.default_pixel(); + let default_bits: Vec = default_pixel.iter().map(|v| v.to_bits()).collect(); + let stale = self + .residents + .get(&key) + .is_none_or(|r| r.region != region || r.kind != kind || r.format != format || r.cmyk != cmyk || r.default_bits != default_bits); + if stale { + let desc = wgpu::TextureDescriptor { + label: Some("pc_compose_layer"), + size: wgpu::Extent3d { width: region.width(), height: region.height(), depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: kind.format(), + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }; + let bytes = region.width() as u64 * region.height() as u64 * kind.bytes_per_pixel() as u64; + let texture = self.create_layer_texture(device, &desc, key, bytes, stats)?; + let view = texture.create_view(&Default::default()); + let default_nonzero = default_pixel.iter().any(|v| *v != 0.0); + let mut r = Resident { + texture, + view, + region, + kind, + format, + tiles: HashMap::new(), + default_nonzero, + default_bits: default_bits.clone(), + doc, + last_used: 0, + stamp: 0, + cmyk, + ordered: false, + }; + // Only the region moved (painting past a layer's tiles grows it): keep the texels the + // old texture already holds instead of converting and uploading every tile again. + let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(device, queue, &old, &mut r)); + // A new layer whose tiles another resident already holds (a duplicate shares its + // source's tiles) starts as a GPU copy of that texture. Duplicating a 20 MP photo + // converted and uploaded every tile again. + let shares = + |o: &Resident| region.tiles().filter(|c| surface.tile(*c).is_some_and(|t| o.tiles.get(c).is_some_and(|ot| Arc::ptr_eq(ot, t)))).count(); + let donor = self + .residents + .iter() + .filter(|(k, o)| k.1 == key.1 && o.region == region && o.kind == kind && o.format == format && o.cmyk == cmyk && o.default_bits == default_bits) + .map(|(k, o)| (shares(o), *k)) + .filter(|(n, _)| *n > 0) + .max_by_key(|(n, _)| *n); + if kept.is_empty() + && let Some((n, o)) = donor.and_then(|(n, k)| Some((n, self.residents.get_mut(&k)?))) + { + // Recorded in the frame's encoder, after the writes and copies that brought the + // donor up to date ([`keep_overlap`]'s own submission would run before those). + o.ordered = true; + encoder.copy_texture_to_texture( + o.texture.as_image_copy(), + r.texture.as_image_copy(), + wgpu::Extent3d { width: region.width(), height: region.height(), depth_or_array_layers: 1 }, + ); + r.tiles = o.tiles.clone(); + r.ordered = true; + stats.tiles_shared += n; + } else { + // A new texture starts zeroed: write the tiles that are present, and the default + // pixel where they are missing if it isn't zero. Coordinates sharing a tile (a + // solid fill, the default) take one upload and GPU copies (#1774). Kept tiles are + // brought up to date below, like those of a resident that didn't move. + let mut present = Vec::new(); + let mut missing = Vec::new(); + for c in region.tiles().filter(|c| !kept.contains_rect(&c.rect())) { + match surface.tile(c) { + Some(t) => present.push((c, t.clone())), + None if default_nonzero => missing.push(c), + None => {} + } + } + if !missing.is_empty() { + let bytes = convert_tile(surface, None, kind, TileCoord::new(0, 0)); + self.staged += put_tiles(device, queue, encoder, &r, &missing, &bytes); + } + let staged = &mut self.staged; + convert_tiles(surface, present, kind, |t, cs, bytes| { + *staged += put_tiles(device, queue, encoder, &r, cs, bytes); + for &c in cs { + stats.tiles_uploaded += 1; + stats.bytes_uploaded += bytes.len(); + r.tiles.insert(c, t.clone()); + } + }); + } + // Tiles the donor held that this surface doesn't, or holds differently, are fixed up + // by the pass over the region below. + self.residents.insert(key, r); + } + // Present: inserted above when it was missing or stale. + let r = self.residents.get_mut(&key)?; + r.last_used = self.frame; + r.stamp = self.stamp; + r.doc = doc; + let mut changed = Vec::new(); + let mut gone = Vec::new(); + for c in region.tiles() { + match surface.tile(c) { + Some(t) => { + if !r.tiles.get(&c).is_some_and(|old| Arc::ptr_eq(old, t)) { + changed.push((c, t.clone())); + } + } + None => { + if r.tiles.remove(&c).is_some() { + gone.push(c); + } + } + } + } + if !gone.is_empty() { + // A tile that went away reads as the default pixel again. + let bytes = if r.default_nonzero { + convert_tile(surface, None, kind, TileCoord::new(0, 0)) + } else { + vec![0u8; (TILE_SIZE * TILE_SIZE) as usize * kind.bytes_per_pixel()] + }; + self.staged += put_tiles(device, queue, encoder, r, &gone, &bytes); + } + let staged = &mut self.staged; + convert_tiles(surface, changed, kind, |t, cs, bytes| { + *staged += put_tiles(device, queue, encoder, r, cs, bytes); + for &c in cs { + stats.tiles_uploaded += 1; + stats.bytes_uploaded += bytes.len(); + r.tiles.insert(c, t.clone()); + } + }); + Some((key, [region.x0, region.y0, region.width() as i32, region.height() as i32])) + } + + /// Premultiplied RGBA32F texture of a pattern as it paints in the document's mode (cached by + /// pixel identity and mode). + pub(crate) fn pattern_view(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, pat: &Pattern) -> wgpu::TextureView { + let fp = pat.surface.tiles().fold((pat.width as u64) << 32 | pat.height as u64, |acc, (c, t)| { + acc.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ (Arc::as_ptr(t) as usize as u64) ^ ((c.tx as u64) << 20) ^ c.ty as u64 + }); + let mode = self.pattern_mode; + let key = format!("{}\u{0}{}\u{0}{mode:?}", pat.id, pat.name); + let format = pat.surface.format(); + // Unstored tiles read the default pixel, interpreted in the source format. + let default_bits: Vec = pat.surface.default_pixel().iter().map(|v| v.to_bits()).collect(); + let (doc, last_used) = (self.pattern_doc, self.frame); + if let Some(cached) = self.patterns.get_mut(&key) + && cached.fingerprint == fp + && cached.format == format + && cached.default_bits == default_bits + { + cached.doc = doc; + cached.last_used = last_used; + return cached.texture.view.clone(); + } + let (w, h) = (pat.width, pat.height); + let px = photocraft_compose::pattern::pattern_rgba(pat, mode); + let bytes: Vec = px.iter().flat_map(|q| [q[0] * q[3], q[1] * q[3], q[2] * q[3], q[3]]).flat_map(f32::to_le_bytes).collect(); + let t = Tex::new(device, "pc_pattern", w, h, wgpu::TextureFormat::Rgba32Float, wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + &bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * 16), rows_per_image: Some(h) }, + wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + ); + let v = t.view.clone(); + let bytes = u64::from(w.max(1)) * u64::from(h.max(1)) * 16; + self.patterns.insert(key, CachedPattern { fingerprint: fp, format, default_bits, texture: t, doc, last_used, bytes }); + v + } + + /// An R32F temporary at least `region`-sized (the smallest that fits) not in `taken`. + /// Stages only read what they wrote, inside the region, so a larger one is as good; page + /// windows of different sizes share them. + pub(crate) fn temp(&mut self, device: &wgpu::Device, region: Rect, taken: &[wgpu::TextureView]) -> wgpu::TextureView { + let (w, h) = (region.width(), region.height()); + let frame = self.frame; + let fit = self + .temps + .iter_mut() + .filter(|(t, _)| { + let s = t.texture.size(); + s.width >= w && s.height >= h && !taken.contains(&t.view) + }) + .min_by_key(|(t, _)| t.texture.size().width as u64 * t.texture.size().height as u64); + if let Some((t, used)) = fit { + *used = frame; + return t.view.clone(); + } + let t = Tex::map(device, "pc_fx_temp", region, MAP32); + let v = t.view.clone(); + self.temps.push((t, frame)); + v + } +} diff --git a/crates/gpu/src/compose.wgsl b/crates/gpu/src/compose.wgsl index 6457b1192c..8b7701e3c1 100644 --- a/crates/gpu/src/compose.wgsl +++ b/crates/gpu/src/compose.wgsl @@ -31,6 +31,13 @@ struct Op { map_origin: vec2, map_size: vec2, p4: vec4, + // fs_layerbatch's third extra layer and the rest of its second (see `batch_layer`); zero + // elsewhere. With them the record fills its 256-byte stride. + q0: vec4, + q1: vec4, + q2: vec4, + q3: vec4, + q4: vec4, }; const F_MASK: u32 = 1u; // layer has an enabled mask @@ -53,6 +60,7 @@ const F_QUANT: u32 = 32768u; // lerp: A rounded to p0.x steps (adjustment res const F_ADD_DIFF: u32 = 16384u; // lerp: A + (B - C) premultiplied (clips on pass-through groups) const F_BLEND_IF: u32 = 524288u; // lerp: B back towards A by the Blend If ranges in p0..p3 const F_TEXT_GAMMA: u32 = 8192u; // blend / atop / fx merge: type layer, mix coverage at gamma p4.w +const F_LAYER_SHAPE: u32 = 1048576u; // fx merge: B is the shape; layer = its colour at fill p0.x inside it × its alpha @group(0) @binding(0) var chunk: Chunk; @group(0) @binding(1) var op: Op; @@ -131,9 +139,13 @@ fn hard_light(cb: f32, cs: f32) -> f32 { let s = 2.0 * cs - 1.0; return cb + s - cb * s; } +// The W3C curve, as Photoshop: D(cb) is the cubic below a quarter, sqrt above it (#8804). fn soft_light_ps(cb: f32, cs: f32) -> f32 { if (cs <= 0.5) { return 2.0 * cb * cs + cb * cb * (1.0 - 2.0 * cs); } - return 2.0 * cb * (1.0 - cs) + sqrt(max(cb, 0.0)) * (2.0 * cs - 1.0); + let b = max(cb, 0.0); + var d = sqrt(b); + if (b <= 0.25) { d = ((16.0 * b - 12.0) * b + 4.0) * b; } + return 2.0 * cb * (1.0 - cs) + d * (2.0 * cs - 1.0); } fn vivid_light_ps(cb: f32, cs: f32) -> f32 { if (cs <= 0.5) { @@ -312,6 +324,9 @@ fn fill_is_special(mode: i32) -> bool { // colour (Hard Mix: (cb - f(1 - cs)) / (1 - f)) at `opacity`, Fill staying coverage over // transparency. Lab documents mix Normal in CIELAB; type layers mix in the text gamma space. fn composite_fill(mode: i32, b: vec4, s: vec4, opacity: f32, fill: f32, gamma_on: bool) -> vec4 { + // The same code as fs_layerbatch_normal, so a layer gives the same bits whichever pass + // draws it (a render resumed from the prefix cache splits runs differently). + if (mode == M_NORMAL && !gamma_on && (op.flags & F_LAB) == 0u) { return composite_normal_fill(b, s, opacity, fill); } let f = clamp(fill, 0.0, 1.0); let special = f < 1.0 && fill_is_special(mode); let ab = b.a; @@ -340,6 +355,21 @@ fn composite_fill(mode: i32, b: vec4, s: vec4, opacity: f32, fill: f32 return vec4(text_dec(wb * text_enc(b.rgb, g) + wbs * text_enc(bl, g) + ws * text_enc(s.rgb, g), g), ao); } +// composite_fill for Normal outside Lab documents and without text gamma: Fill is plain coverage +// and the blend is the source. Kept apart so fs_layerbatch_normal doesn't carry every other +// mode's code, which costs registers and so pixels in flight. +fn composite_normal_fill(b: vec4, s: vec4, opacity: f32, fill: f32) -> vec4 { + let ab = b.a; + let over = s.a * opacity * clamp(fill, 0.0, 1.0); + if (over <= 0.0) { return b; } + let ao = ab + (1.0 - ab) * over; + if (ao <= 0.0) { return vec4(0.0); } + let wb = ab * (1.0 - over) / ao; + let wbs = ab * over / ao; + let ws = (1.0 - ab) * over / ao; + return vec4(wb * b.rgb + wbs * s.rgb + ws * s.rgb, ao); +} + // photocraft_color::convert::{srgb_to_lab, lab_to_srgb} (D50, Bradford to sRGB). const D50: vec3 = vec3(0.96422, 1.0, 0.82521); fn lab_f(t: f32) -> f32 { @@ -586,8 +616,22 @@ fn levels_float(record: i32, v: f32) -> f32 { return lut_at(base + 3) + mapped * (lut_at(base + 4) - lut_at(base + 3)); } -fn tone_channel(row: i32, v: f32, identity: f32) -> f32 { +fn tone_channel(row: i32, v: f32, identity: f32, extrapolate: f32) -> f32 { if (identity > 0.5) { return v; } + // Mirrors compose::adjust::lut_extended: past 0..1 along the end segments' slopes, measured + // over TONE_SLOPE_SPAN (16) table steps. + if (extrapolate > 0.5 && abs(v) <= 3.0e38) { + if (v < 0.0) { + let a = textureLoad(lut_tex, vec2(0, row), 0).r; + let b = textureLoad(lut_tex, vec2(16, row), 0).r; + return a + v * (b - a) * (4095.0 / 16.0); + } + if (v > 1.0) { + let a = textureLoad(lut_tex, vec2(4079, row), 0).r; + let b = textureLoad(lut_tex, vec2(4095, row), 0).r; + return b + (v - 1.0) * (b - a) * (4095.0 / 16.0); + } + } return lut(row, v); } @@ -621,7 +665,7 @@ fn adjust(c: vec3) -> vec3 { return clamp(v, vec3(0.0), vec3(1.0)); } case 6: { return vec3(exposure(c.r, p0), exposure(c.g, p0), exposure(c.b, p0)); } // Exposure - case 7: { return vec3(tone_channel(0, c.r, p0.x), tone_channel(1, c.g, p0.y), tone_channel(2, c.b, p0.z)); } // Levels / Curves / modern Brightness/Contrast (compose::adjust::modern_lut) + case 7: { return vec3(tone_channel(0, c.r, p0.x, p0.w), tone_channel(1, c.g, p0.y, p0.w), tone_channel(2, c.b, p0.z, p0.w)); } // Levels / Curves / modern Brightness/Contrast (compose::adjust::modern_lut) case 17: { return vec3(levels_float(0, levels_float(1, c.r)), levels_float(0, levels_float(2, c.g)), levels_float(0, levels_float(3, c.b))); } case 8: { // Hue/Saturation let hsl = rgb_to_hsl(c); @@ -649,13 +693,30 @@ fn adjust(c: vec3) -> vec3 { if (light > 0.0) { lc = c + (1.0 - c) * light; } let lh = rgb_to_hsl(lc); hh = rem_euclid(lh.x + (p0.x + dh) / 360.0, 1.0); - // Raising saturation divides by 1 - s; lowering it multiplies by 1 + s. - if (sat > 0.0) { - ss = clamp(lh.y / max(1.0 - sat, 1.0 / 512.0), 0.0, 1.0); + var rgb: vec3; + if (hdr) { + // 32-bit: the chroma scaled around the lightness, unclipped, by Photoshop's factor + // in whole numbers (compose::adjust::hue_saturation_in, hdr_saturation_factor). + var k: f32; + if (sat <= 0.0) { + k = 1.0 - floor(-sat * 256.0 + 1e-4) / 256.0; + } else { + let steps = min(u32(floor(sat * 254.0 + 1e-4)), 254u); + k = f32(min(65280u / (255u - steps), 32767u)) / 256.0; + } + let d = (max(max(lc.r, lc.g), lc.b) - min(min(lc.r, lc.g), lc.b)) * k; + let lo = lh.z - d / 2.0; + let hi = lh.z + d / 2.0; + rgb = vec3(hue_ch(lo, hi, hh + 1.0 / 3.0), hue_ch(lo, hi, hh), hue_ch(lo, hi, hh - 1.0 / 3.0)); } else { - ss = clamp(lh.y * (1.0 + sat), 0.0, 1.0); + // Raising saturation divides by 1 - s; lowering it multiplies by 1 + s. + if (sat > 0.0) { + ss = clamp(lh.y / max(1.0 - sat, 1.0 / 512.0), 0.0, 1.0); + } else { + ss = clamp(lh.y * (1.0 + sat), 0.0, 1.0); + } + rgb = hsl_to_rgb(hh, ss, lh.z); } - var rgb = hsl_to_rgb(hh, ss, lh.z); // compose::adjust::range_lightness: towards the brightest channel when raised, the darkest when lowered. let rl = clamp(dl, -1.0, 1.0); if (rl > 0.0) { @@ -704,7 +765,8 @@ fn adjust(c: vec3) -> vec3 { if (c.r >= c.g && c.r >= c.b) { primary = 0; } else if (c.g >= c.b) { primary = 2; } var secondary = 5; if (c.b <= c.r && c.b <= c.g) { secondary = 1; } else if (c.r <= c.g) { secondary = 3; } - let g = clamp(mn + (mid - mn) * w[secondary] / 100.0 + (mx - mid) * w[primary] / 100.0, 0.0, 1.0); + // 32-bit: superwhites stay, up to Photoshop's 16 + let g = clamp(mn + (mid - mn) * w[secondary] / 100.0 + (mx - mid) * w[primary] / 100.0, 0.0, select(1.0, 16.0, hdr)); if (p1.z > 0.5) { if (hdr) { return set_lum(p2.rgb, g, F32_MAX); } // 32-bit: nothing clips at 1 return clamp(set_lum(p2.rgb, g, 1.0), vec3(0.0), vec3(1.0)); // Color blend of the tint @@ -812,12 +874,9 @@ fn fs_mask(in: VOut) -> @location(0) vec4 { return c; } -// blend_into(backdrop = A, src = B, mode, opacity), including Dissolve. -@fragment -fn fs_blend(in: VOut) -> @location(0) vec4 { - let p = local(in.pos); - let b = textureLoad(tex_a, p, 0); - var s = textureLoad(tex_b, p, 0); +// blend_into(backdrop = b, src = s, mode, opacity) at chunk pixel p, including Dissolve. +fn blend_px(p: vec2, b: vec4, src: vec4) -> vec4 { + var s = src; if (s.a <= 0.0) { return b; } if (op.mode == M_DISSOLVE) { s.a = select(0.0, 1.0, dissolve_noise(doc_px(p)) < s.a * op.opacity * op.fill); @@ -826,6 +885,85 @@ fn fs_blend(in: VOut) -> @location(0) vec4 { return composite_fill(op.mode, b, s, op.opacity, op.fill, (op.flags & F_TEXT_GAMMA) != 0u); } +// blend_into(backdrop = A, src = B, mode, opacity), including Dissolve. +@fragment +fn fs_blend(in: VOut) -> @location(0) vec4 { + let p = local(in.pos); + return blend_px(p, textureLoad(tex_a, p, 0), textureLoad(tex_b, p, 0)); +} + +// fs_content then fs_blend in one pass: the layer's own pixels (times its mask) blended straight +// onto A, without writing them to a chunk texture and reading them back. +@fragment +fn fs_layerblend(in: VOut) -> @location(0) vec4 { + let p = local(in.pos); + let d = doc_px(p); + var s = layer_texel(d); + s.a = s.a * mask_value(d); + return blend_px(p, textureLoad(tex_a, p, 0), s); +} + +// The pixel at document pixel d of an extra layer of fs_layerbatch: an unmasked plain layer whose +// pixels are in texture `t` (0: tex_b, 1: tex_c, 2: map_tex). Three vec4 of the record describe +// it: geo = (texture origin x, y, size w, h), color = its colour outside the texture, +// prm = (opacity, fill, mode, 1 when it has a texture), all whole numbers stored as floats. +fn batch_texel(t: i32, d: vec2, geo: vec4, color: vec4, prm: vec4) -> vec4 { + let q = d - vec2(geo.xy); + if (prm.w > 0.5 && inside(q, vec2(geo.zw))) { + if (t == 0) { + return textureLoad(tex_b, q, 0); + } else if (t == 1) { + return textureLoad(tex_c, q, 0); + } + return textureLoad(map_tex, q, 0); + } + return color; +} + +// One extra layer of fs_layerbatch (see batch_texel) blended onto b at document pixel d. +fn batch_layer(t: i32, d: vec2, b: vec4, geo: vec4, color: vec4, prm: vec4) -> vec4 { + let s = batch_texel(t, d, geo, color, prm); + if (s.a <= 0.0) { return b; } + return composite_fill(i32(prm.z), b, s, prm.x, prm.y, false); +} + +// batch_layer of a Normal layer outside Lab documents. +fn batch_layer_normal(t: i32, d: vec2, b: vec4, geo: vec4, color: vec4, prm: vec4) -> vec4 { + let s = batch_texel(t, d, geo, color, prm); + if (s.a <= 0.0) { return b; } + return composite_normal_fill(b, s, prm.x, prm.y); +} + +// fs_layerblend of this pass's layer, then `op.kind` (at most 3) more plain layers: consecutive +// LayerBlend passes the executor draws as one, reading and writing the chunk once for all of them. +@fragment +fn fs_layerbatch(in: VOut) -> @location(0) vec4 { + let p = local(in.pos); + let d = doc_px(p); + var s = layer_texel(d); + s.a = s.a * mask_value(d); + var b = blend_px(p, textureLoad(tex_a, p, 0), s); + if (op.kind >= 1) { b = batch_layer(0, d, b, op.p0, op.p1, op.p2); } + if (op.kind >= 2) { b = batch_layer(1, d, b, op.p3, op.q0, op.q1); } + if (op.kind >= 3) { b = batch_layer(2, d, b, op.q2, op.q3, op.q4); } + return b; +} + +// fs_layerbatch for a run of Normal layers without text gamma, outside Lab documents. +@fragment +fn fs_layerbatch_normal(in: VOut) -> @location(0) vec4 { + let p = local(in.pos); + let d = doc_px(p); + var s = layer_texel(d); + s.a = s.a * mask_value(d); + var b = textureLoad(tex_a, p, 0); + if (s.a > 0.0) { b = composite_normal_fill(b, s, op.opacity, op.fill); } + if (op.kind >= 1) { b = batch_layer_normal(0, d, b, op.p0, op.p1, op.p2); } + if (op.kind >= 2) { b = batch_layer_normal(1, d, b, op.p3, op.q0, op.q1); } + if (op.kind >= 3) { b = batch_layer_normal(2, d, b, op.q2, op.q3, op.q4); } + return b; +} + // composite_atop(base = A, src = B): blend as if the base were opaque, keep its alpha. @fragment fn fs_atop(in: VOut) -> @location(0) vec4 { @@ -833,6 +971,9 @@ fn fs_atop(in: VOut) -> @location(0) vec4 { let base = textureLoad(tex_a, p, 0); if (base.a <= 0.0) { return base; } let s = textureLoad(tex_b, p, 0); + if (op.mode == M_DISSOLVE) { + return select(base, vec4(s.rgb, base.a), dissolve_noise(doc_px(p)) < s.a * op.opacity); + } let r = composite_g(op.mode, vec4(base.rgb, 1.0), s, op.opacity, (op.flags & F_TEXT_GAMMA) != 0u); return vec4(r.rgb, base.a); } @@ -848,6 +989,7 @@ fn fs_adjust(in: VOut) -> @location(0) vec4 { } // Mix an adjusted result B back over the original A with blend mode, opacity and mask. +// Both ordinary and clipped adjustments use this pass, retaining the original alpha. @fragment fn fs_adjmix(in: VOut) -> @location(0) vec4 { let p = local(in.pos); @@ -855,8 +997,24 @@ fn fs_adjmix(in: VOut) -> @location(0) vec4 { let k = op.opacity * mask_value(doc_px(p)); if (k <= 0.0) { return b; } let a = textureLoad(tex_b, p, 0); - let bl = blend_rgb(op.mode, b.rgb, a.rgb); - return vec4(b.rgb + (bl - b.rgb) * k, b.a); + if (op.mode == M_DISSOLVE) { + return select(b, vec4(a.rgb, b.a), dissolve_noise(doc_px(p)) < k); + } + // Fill below 100% in one of the special eight (compose::blend_back, psblend::blend_rgb_fill): + // the result pulled toward the mode's neutral colour; Hard Mix (cb - f(1 - cs)) / (1 - f). + let f = clamp(op.fill, 0.0, 1.0); + let special = f < 1.0 && fill_is_special(op.mode); + var cs = a.rgb; + if (special) { + var neutral = 0.0; + if (op.mode == 5 || op.mode == 6) { neutral = 1.0; } + if (op.mode == 16 || op.mode == 17) { neutral = 0.5; } + cs = vec3(neutral) + (a.rgb - vec3(neutral)) * f; + } + var bl = blend_rgb(op.mode, b.rgb, cs); + if (special && op.mode == 19) { bl = clamp((b.rgb - f * (vec3(1.0) - a.rgb)) / (1.0 - f), vec3(0.0), vec3(1.0)); } + // Keep bounded adjustment results exact at full coverage over HDR backdrops. + return vec4(b.rgb * (1.0 - k) + bl * k, b.a); } // Pass-through group coverage mixes premultiplied colour, then returns straight alpha. @@ -880,8 +1038,8 @@ fn fs_lerp(in: VOut) -> @location(0) vec4 { return vec4(clamp(pm.rgb / al, vec3(0.0), vec3(1.0)), al); } if ((op.flags & F_BLEND_IF) != 0u) { - // compose::apply_blend_if: "This Layer" is C (the layer's own content with its masks), - // or B itself for an adjustment layer; "Underlying Layer" is A. + // compose::apply_blend_if: "This Layer" is C (the layer's own content with its masks, or an + // adjustment's result before Opacity, #9542), B when p4.y is set; "Underlying Layer" is A. var this_px = b; if (op.p4.y < 0.5) { this_px = textureLoad(tex_c, p, 0); @@ -935,15 +1093,19 @@ fn blend_if_weight(this_px: vec4, under: vec4) -> f32 { if (px.a <= 0.0) { continue; } - // The CPU gets 8- and 16-bit levels exactly (k / 255 × 255 = k), but GPU arithmetic can - // land a few millionths below, which flips a hard threshold at that level: values that - // close to a level are that level. Real 8/16-bit values off a level are ≥ 1/257 away. - let raw = clamp(px.rgb, vec3(0.0), vec3(1.0)) * 255.0; - let v = select(raw, round(raw), abs(raw - round(raw)) < vec3(1e-4)); + // compose::blend_if_level: a value on a level's 16-bit step (Photoshop's 0..32768, + // round(c × 32768 / 255)) is that level, as Photoshop compares Blend If (#9521); every + // 8-bit level is on its own step, a few millionths of GPU drift included. + let x = clamp(px.rgb, vec3(0.0), vec3(1.0)); + let raw = x * 255.0; + let level = round(raw); + let v = select(raw, level, round(x * 32768.0) == round(level * 32768.0 / 255.0)); if (op.p4.x > 0.5) { k = k * bi_weight(0u, s, v.r) * bi_weight(1u, s, v.r); } else { - let gray = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b; + // compose::blend_if_gray: (30 R + 59 G + 11 B) / 100, rounded half down on whole levels. + let sum = 30.0 * v.r + 59.0 * v.g + 11.0 * v.b; + let gray = select(sum / 100.0, floor((sum + 49.0) / 100.0), all(v == round(v))); k = k * bi_weight(0u, s, gray) * bi_weight(1u, s, v.r) * bi_weight(2u, s, v.g) * bi_weight(3u, s, v.b); } if (k <= 0.0) { @@ -971,15 +1133,10 @@ fn wrap(i: i32, n: i32) -> i32 { return ((i % n) + n) % n; } -// compose::pattern::Tile::sample through Placement::map. p3 = (origin, cos, sin), -// p4 = (1 / scale, tile w, tile h); `pat_tex` holds premultiplied RGBA. -fn pattern_sample(d: vec2) -> vec4 { - let dx = f32(d.x) + 0.5 - op.p3.x; - let dy = f32(d.y) + 0.5 - op.p3.y; - let c = op.p3.z; - let s = op.p3.w; - let u = (dx * c - dy * s) * op.p4.x - 0.5; - let v = (dx * s + dy * c) * op.p4.x - 0.5; +// One bilinear, wrapping, premultiplied tap at tile coordinates (u, v) (Tile::sample_premul). +fn pattern_tap(u0: f32, v0: f32) -> vec4 { + let u = u0 - 0.5; + let v = v0 - 0.5; let x0 = floor(u); let y0 = floor(v); let fx = u - x0; @@ -998,6 +1155,28 @@ fn pattern_sample(d: vec2) -> vec4 { acc += textureLoad(pat_tex, vec2(wrap(ix + i, tw), wrap(iy + j, th)), 0) * (wx[i] * wy[j]); } } + return acc; +} + +// compose::pattern::Tile::sample_area through Placement::map. p3 = (origin, cos, sin), +// p4 = (1 / scale, tile w, tile h); `pat_tex` holds premultiplied RGBA. Below 100 % a pixel +// averages n x n taps over its footprint (n = ceil(1 / scale), at most 8), like the CPU. +fn pattern_sample(d: vec2) -> vec4 { + var n = 1; + if (op.p4.x > 1.0) { n = i32(min(ceil(op.p4.x), 8.0)); } + let c = op.p3.z; + let s = op.p3.w; + var acc = vec4(0.0); + for (var j = 0; j < n; j++) { + for (var i = 0; i < n; i++) { + let ox = (f32(i) + 0.5) / f32(n) - 0.5; + let oy = (f32(j) + 0.5) / f32(n) - 0.5; + let dx = f32(d.x) + 0.5 + ox - op.p3.x; + let dy = f32(d.y) + 0.5 + oy - op.p3.y; + acc += pattern_tap((dx * c - dy * s) * op.p4.x, (dx * s + dy * c) * op.p4.x); + } + } + acc = acc / f32(n * n); if (acc.a <= 0.0) { return vec4(0.0); } return vec4(acc.rgb / acc.a, min(acc.a, 1.0)); } @@ -1016,6 +1195,16 @@ fn fx_color(d: vec2) -> vec4 { return op.color; } +// effects::paint: blend an effect's colour `s` (alpha = colour alpha × coverage × opacity) into +// `dst` at document pixel `d`. Dissolve takes the effect fully or not at all (effects::dissolves_in). +fn fx_composite(d: vec2, dst: vec4, s: vec4) -> vec4 { + if (op.mode == M_DISSOLVE) { + if (dissolve_noise(d) < s.a) { return composite(M_NORMAL, dst, vec4(s.rgb, 1.0), 1.0); } + return dst; + } + return composite(op.mode, dst, s, 1.0); +} + // Effect chain steps (`kind`): 0 A at `opacity` × alpha; 1 an opaque copy of A (a clipping base // for clipped layers' effects); 2 A's colour with alpha `opacity` inside B's shape (the layer // before its interior effects); 3 A with its alpha × B's alpha (the shape's own alpha); 4 the @@ -1034,7 +1223,7 @@ fn fs_fxinit(in: VOut) -> @location(0) vec4 { let a = textureLoad(tex_b, p, 0).a; let s = textureLoad(tex_c, p, 0); if (s.a <= 0.0 || a <= INSIDE_EPS) { return c; } - return composite(op.mode, c, vec4(s.rgb, op.opacity * min(s.a / a, 1.0)), 1.0); + return fx_composite(doc_px(p), c, vec4(s.rgb, op.opacity * min(s.a / a, 1.0))); } case 5: { let a = textureLoad(tex_b, p, 0).a; @@ -1051,6 +1240,7 @@ fn fs_fxinit(in: VOut) -> @location(0) vec4 { // beneath a layer of alpha `l` (disjoint areas). fn outline_share(cov: f32, l: f32) -> f32 { let outside = max(1.0 - clamp(cov, 0.0, 1.0), 0.0); + if (outside <= 1e-6) { return 0.0; } let rest = 1.0 - clamp(l, 0.0, 1.0); if (rest <= 1e-6) { return 1.0; } return min(outside / rest, 1.0); @@ -1083,12 +1273,12 @@ fn fs_fxpaint(in: VOut) -> @location(0) vec4 { let k = min(m * op.p0.x, 1.0) * op.opacity; if (k <= 0.0) { return dst; } let t = 1.0 - clamp(m, 0.0, 1.0); - return composite(op.mode, dst, vec4(lut(0, t), lut(1, t), lut(2, t), lut(3, t) * k), 1.0); + return fx_composite(d, dst, vec4(lut(0, t), lut(1, t), lut(2, t), lut(3, t) * k)); } let k = m * op.opacity; if (k <= 0.0) { return dst; } let c = fx_color(d); - return composite(op.mode, dst, vec4(c.rgb, c.a * k), 1.0); + return fx_composite(d, dst, vec4(c.rgb, c.a * k)); } // Outside strokes (A = the exterior result before them, B = the layer, C = coverage so far in @@ -1116,16 +1306,19 @@ fn fs_fxstroke(in: VOut) -> @location(0) vec4 { k = k * outline_share(a, lay_a); } else if (a > INSIDE_EPS) { // Pixel layers: the stroke shows beneath only through the part the shape leaves - // (`effects.rs`): (1 - a) / (1 - layer alpha), 1 under an opaque layer. + // (`effects.rs`): (1 - a) / (1 - layer alpha), none beneath a fully covered pixel. if (vector) { k = 0.0; - } else if (lay_a >= 1.0) { - k = 1.0; } else { - k = clamp((1.0 - clamp(a, 0.0, 1.0)) / (1.0 - lay_a), 0.0, 1.0); + k = k * outline_share(a, lay_a); } } - let share = k * op.opacity * (1.0 - cover); + var cov = k * op.opacity; + if (op.mode == M_DISSOLVE) { + // effects.rs: a Dissolve stroke covers each pixel fully or not at all. + cov = select(0.0, 1.0, cov > 0.0 && dissolve_noise(d) < cov); + } + let share = cov * (1.0 - cover); if (op.kind == 1) { return vec4(cover + share, lay_a, 0.0, 0.0); } var acc = vec4(0.0); if (!first) { acc = textureLoad(layer_tex, p, 0); } @@ -1168,13 +1361,24 @@ fn mix_premul(a: vec4, b: vec4, k: f32) -> vec4 { fn fs_fxmerge(in: VOut) -> @location(0) vec4 { let p = local(in.pos); var w = textureLoad(tex_a, p, 0); - let l = textureLoad(tex_b, p, 0); + var l = textureLoad(tex_b, p, 0); let c = textureLoad(tex_c, p, 0); + if ((op.flags & F_LAYER_SHAPE) != 0u) { + // fs_fxinit INIT_INSIDE then INIT_ALPHA, on the shape in B. + l = vec4(l.rgb, select(0.0, op.p0.x, l.a > INSIDE_EPS) * min(l.a, 1.0)); + } if (l.a > 0.0 && (op.flags & F_NO_LAYER) == 0u) { w = composite_g(op.mode, w, l, 1.0, (op.flags & F_TEXT_GAMMA) != 0u); } let atop = (op.flags & F_ATOP) != 0u; var before = c; if (atop) { before = vec4(c.rgb, 1.0); } - let o = mix_premul(before, w, op.opacity); + var o: vec4; + if (op.mode == M_DISSOLVE) { + // Match the CPU effects compositor: opacity gates the completed stack, not + // its individual colours or alpha. Use document coordinates across chunks. + o = select(before, w, dissolve_noise(doc_px(p)) < op.opacity); + } else { + o = mix_premul(before, w, op.opacity); + } if (atop) { if (c.a > 0.0) { return vec4(o.rgb, c.a); } return c; @@ -1245,6 +1449,18 @@ fn fs_mblur(in: VOut) -> @location(0) vec4 { @fragment fn fs_mglow(in: VOut) -> @location(0) vec4 { let d = textureLoad(tex_a, local(in.pos), 0).r + 0.5; + if (op.p0.w > 0.5) { + let t = (d - op.p0.x) / op.p0.y; + var tail = 0.0; + if (t >= 0.0 && t <= 1.0) { + tail = op.p0.z * t * t * (3.0 - 2.0 * t); + } else if (t > 1.0 && t <= 2.0) { + let u = t - 1.0; + let remaining = 1.0 - u; + tail = op.p0.z * remaining * remaining * (1.0 + 2.0 * u); + } + return mout(tail); + } var m = 0.5 * clamp(1.0 - (d - op.p0.x) / op.p0.y, 0.0, 1.0); if (d <= op.p0.x) { m = 1.0; } return mout(m); @@ -1256,7 +1472,11 @@ fn fs_mglow(in: VOut) -> @location(0) vec4 { fn fs_mfinish(in: VOut) -> @location(0) vec4 { let p = local(in.pos); var v = textureLoad(tex_a, p, 0).r; - if (op.p0.x > 0.5) { v = abs(v - textureLoad(tex_b, p, 0).r); } + if (op.p0.x > 1.5) { + v = max(v, textureLoad(tex_b, p, 0).r); + } else if (op.p0.x > 0.5) { + v = abs(v - textureLoad(tex_b, p, 0).r); + } if (op.p0.y > 0.5 && op.p0.y < 1.5) { v = 1.0 - v; } if (op.p0.z > 0.5) { v = lut(0, v); } if (op.p0.y > 1.5) { v = 1.0 - v; } @@ -1329,7 +1549,7 @@ fn fs_mbevelshade(in: VOut) -> @location(0) vec4 { } // One bevel shading value (`bevel_maps`' shade_into): highlight (p1.z = 0) or shadow amount for -// height-gradient scale `depth`, times `region`, through the gloss contour. +// height-gradient scale `depth`, times `region`, with the shading through the gloss contour. fn bevel_part(p: vec2, depth: f32, region: f32) -> f32 { let gx = (ra(p + vec2(1, 0)) - ra(p - vec2(1, 0))) * 0.5 * depth; let gy = (ra(p + vec2(0, 1)) - ra(p - vec2(0, 1))) * 0.5 * depth; @@ -1337,14 +1557,15 @@ fn bevel_part(p: vec2, depth: f32, region: f32) -> f32 { let len = sqrt(n.x * n.x + n.y * n.y + 1.0); let shade = (n.x * op.p0.x + n.y * op.p0.y + n.z * op.p0.z) / len; let se = op.p0.w; - let k = shade - se; + var k = shade - se; + // effects::bevel_maps: the gloss contour maps the shading, a flat face's as the neutral (#7360). + if (op.p1.w > 0.5) { k = lut(0, shade) - lut(0, se); } var v = 0.0; if (op.p1.z > 0.5) { if (k <= 0.0) { v = clamp(-k / max(se, 1e-3), 0.0, 1.0) * region; } } else if (k > 0.0) { v = clamp(k / max(1.0 - se, 1e-3), 0.0, 1.0) * region; } - if (op.p1.w > 0.5) { v = lut(0, v); } return v; } diff --git a/crates/gpu/src/consts.rs b/crates/gpu/src/consts.rs new file mode 100644 index 0000000000..536aea8a49 --- /dev/null +++ b/crates/gpu/src/consts.rs @@ -0,0 +1,57 @@ +/// Accumulator format for intermediate buffers. Full float: discontinuous operations +/// (Posterize, Threshold, Hard Mix, Dissolve) must land on the same side of their thresholds as +/// the CPU reference, and Color Burn/Dodge amplify input error. +pub const ACC_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba32Float; + +// default accumulation format; `preferred_acc_format` downgrades to Rgba16Float where 32-bit float isn't renderable +/// Effect intermediates (distances, blurs) and layer shapes. +pub(crate) const MAP32: wgpu::TextureFormat = wgpu::TextureFormat::R32Float; + +/// Final effect coverage maps (0..1; half precision is ~1/4000). +pub(crate) const MAP16: wgpu::TextureFormat = wgpu::TextureFormat::R16Float; + +/// Side of the square chunks the canvas is processed in. +pub const CHUNK: u32 = 1024; + +pub(crate) const STRIDE: u64 = 256; + +pub(crate) const CHUNK_UNIFORM: u64 = 16; + +pub(crate) const OP_UNIFORM: u64 = 256; + +/// GPU memory the effect-map cache may hold before evicting layers not drawn this frame. +pub const FX_BUDGET: usize = 1536 << 20; + +/// Largest side of the square pages layer surfaces are stored in on the GPU (smaller when the +/// device's texture limit is). A chunk only binds the pages of the cell it lies in, so layers of +/// any size composite on the GPU. +pub const PAGE: u32 = 2048; + +/// GPU memory resident layer pages may hold before the least recently used are evicted. +pub const RESIDENT_BUDGET: u64 = 4 << 30; + +/// GPU memory pattern textures may hold before the least recently painted are dropped (those the +/// current frame paints with always stay). +pub const PATTERN_BUDGET: u64 = 256 << 20; +/// The most a single pattern's RGBA32F texture may take. A pattern over it paints on the CPU, which +/// holds a large 8-bit pattern at 4 bytes a pixel instead of 16 (`Pattern::working_bytes`). +pub(crate) const MAX_PATTERN_TEXTURE_BYTES: u64 = 256 << 20; + +/// A pattern texture no frame has painted with for this many frames is dropped. +pub const PATTERN_IDLE_FRAMES: u64 = 240; + +/// Uploads (bytes) [`Compositor::render`] stages before submitting the work recorded so far, so +/// a refresh of a huge document never holds all its uploads in staging memory at once. +pub(crate) const FLUSH_BYTES: u64 = 512 << 20; + +/// Pass pixels (chunk pixels × passes) [`Compositor::render`] records before submitting: one +/// command buffer holding a whole huge refresh can run for seconds, long enough for the OS to +/// reset the GPU. +pub(crate) const FLUSH_WORK: u64 = 1 << 30; + +/// Copies and render passes [`Compositor::render`] records before submitting. wgpu opens up to +/// two backend command buffers for each (a render pass and its barrier pre-pass), and Metal +/// treats more than 4096 outstanding ones as a lost device: a refresh of a document with a few +/// hundred layers reached that inside a single page cell (P12, "pc_compose_uniforms is +/// invalid"), where the work and byte limits above never trigger. +pub(crate) const FLUSH_OPS: u32 = 1000; diff --git a/crates/gpu/src/convert.rs b/crates/gpu/src/convert.rs new file mode 100644 index 0000000000..9dbfc81814 --- /dev/null +++ b/crates/gpu/src/convert.rs @@ -0,0 +1,371 @@ +use super::batch::write_tile; +use super::textures::{Resident, TexKind}; +use super::{Arc, F, HashMap, I, PixelFormat, Rect, SampleType, Surface, TILE_SIZE, Tile, TileCoord, U, fx}; + +/// Distinct tiles converted per parallel batch: bounds the converted bytes held at once (64 tiles +/// of a 16-bit page are 32 MiB). +pub(crate) const CONVERT_BATCH: usize = 64; + +/// Write the same tile `bytes` at every coordinate in `cs` of resident `r`; returns the bytes +/// staged. Several coordinates (a solid fill or a cleared area shares one tile) get one upload to +/// a scratch tile and GPU copies from it: writing each one from the CPU sent 387 MB per refresh +/// of a filled 50 MP 16-bit page (#1774). +pub(crate) fn put_tiles(device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, r: &Resident, cs: &[TileCoord], bytes: &[u8]) -> u64 { + if let [c] = cs + && !r.ordered + { + write_tile(queue, r, *c, bytes); + return bytes.len() as u64; + } + if cs.is_empty() { + return 0; + } + let extent = wgpu::Extent3d { width: TILE_SIZE as u32, height: TILE_SIZE as u32, depth_or_array_layers: 1 }; + // Its own texture per call: queued writes land before the encoder runs, so a shared scratch + // would hold only the last tile by then. + let scratch = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_tile_scratch"), + size: extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: r.kind.format(), + usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &scratch, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytes, + wgpu::TexelCopyBufferLayout { + offset: 0, + bytes_per_row: Some(TILE_SIZE as u32 * r.kind.bytes_per_pixel() as u32), + rows_per_image: Some(TILE_SIZE as u32), + }, + extent, + ); + for c in cs { + let tr = c.rect(); + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { texture: &scratch, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyTextureInfo { + texture: &r.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: (tr.x0 - r.region.x0) as u32, y: (tr.y0 - r.region.y0) as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + extent, + ); + } + bytes.len() as u64 +} + +/// Copy the texels `old` holds inside the region of its replacement `new` (same texel kind, +/// format and default pixel; only the region moved) and carry over the tiles they show, so only +/// tiles outside the overlap or changed since need uploading. Returns the kept (tile-aligned) +/// rect; empty when nothing can be kept. +pub(crate) fn keep_overlap(device: &wgpu::Device, queue: &wgpu::Queue, old: &Resident, new: &mut Resident) -> Rect { + let same = old.kind == new.kind && old.format == new.format && old.cmyk == new.cmyk && old.default_bits == new.default_bits; + let k = old.region.intersect(&new.region); + if !same || k.is_empty() { + return Rect::EMPTY; + } + // Its own submission: tile writes go through the queue and land at the start of the next + // submit, before anything in the frame's encoder, so a copy recorded there would overwrite + // the tiles written after it with the old texels. + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose_keep") }); + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { + texture: &old.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: (k.x0 - old.region.x0) as u32, y: (k.y0 - old.region.y0) as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::TexelCopyTextureInfo { + texture: &new.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: (k.x0 - new.region.x0) as u32, y: (k.y0 - new.region.y0) as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + wgpu::Extent3d { width: k.width(), height: k.height(), depth_or_array_layers: 1 }, + ); + queue.submit([encoder.finish()]); + new.tiles = old.tiles.iter().filter(|(c, _)| k.contains_rect(&c.rect())).map(|(c, t)| (*c, t.clone())).collect(); + k +} + +/// Convert `tiles` of `surface` to `kind` texels and hand each distinct tile, the coordinates +/// sharing it and its bytes to `put`. Each distinct tile is converted once (a solid fill shares +/// one `Arc` across every coordinate it covers), in parallel batches. Converting every tile one +/// after another on the UI thread froze a 50 MP 16-bit document for about 1.4 s after a +/// full-canvas fill (#1774). Returns how many tiles were converted. +pub(crate) fn convert_tiles( + surface: &Surface, + tiles: Vec<(TileCoord, Arc)>, + kind: TexKind, + mut put: impl FnMut(&Arc, &[TileCoord], &[u8]), +) -> usize { + let mut groups: Vec<(Arc, Vec)> = Vec::new(); + let mut index: HashMap<*const Tile, usize> = HashMap::new(); + for (c, t) in tiles { + match index.get(&Arc::as_ptr(&t)).and_then(|&i| groups.get_mut(i)) { + Some(g) => g.1.push(c), + None => { + index.insert(Arc::as_ptr(&t), groups.len()); + groups.push((t, vec![c])); + } + } + } + // CMYK and Lab tiles convert pixel by pixel through a colour transform (a CMYK tile took + // ~5 ms on one thread, the whole of a brush dab's refresh on an A4 CMYK page, #5108): those + // go in bands of rows, so even the one tile a dab touches spreads over the threads. Workers + // don't inherit this thread's CMYK profile scope, so each band enters it. + let bands = if colour_managed(&surface.format(), kind) { TILE_SIZE / MANAGED_BAND_ROWS } else { 1 }; + let space = photocraft_color::convert::active_cmyk_space(); + for batch in groups.chunks(CONVERT_BATCH) { + // A tile's content is the same at every coordinate sharing it: convert it at the first. + let jobs: Vec<_> = batch.iter().filter_map(|(t, cs)| cs.first().map(|c| (t, *c))).flat_map(|(t, c)| (0..bands).map(move |b| (t, c, b))).collect(); + let parts = fx::par_map(jobs, |(t, c, b)| { + photocraft_color::convert::with_cmyk_space(space.as_ref(), || { + if bands == 1 { + return convert_tile(surface, Some(t), kind, c); + } + let r = c.rect(); + let y0 = r.y0 + b * MANAGED_BAND_ROWS; + convert_rows(surface, Some(t), kind, Rect::new(r.x0, y0, r.x1, y0 + MANAGED_BAND_ROWS)) + }) + }); + for ((t, cs), bytes) in batch.iter().zip(parts.chunks(bands as usize)) { + // One part (every format but CMYK and Lab) goes through as is: no copy per tile. + match bytes { + [one] => put(t, cs, one), + _ => put(t, cs, &bytes.concat()), + } + } + } + groups.len() +} + +/// Rows per parallel band of a colour-managed (CMYK, Lab) tile conversion. +const MANAGED_BAND_ROWS: i32 = 16; + +/// Whether `fmt` pixels reach `kind` texels through a per-pixel colour transform (CMYK, Lab). +pub(crate) fn colour_managed(fmt: &PixelFormat, kind: TexKind) -> bool { + use photocraft_color::ColorMode; + matches!(kind, TexKind::Rgba8 | TexKind::Rgba16F) && matches!(fmt.mode, ColorMode::Cmyk | ColorMode::Lab) +} + +/// Tile pixels in the texture's format. `tile = None` gives the default pixel everywhere. +pub(crate) fn convert_tile(surface: &Surface, tile: Option<&Arc>, kind: TexKind, c: TileCoord) -> Vec { + if let (Some(t), TexKind::Rgba8Direct | TexKind::R8Direct) = (tile, kind) { + return t.bytes().to_vec(); + } + let fmt = surface.format(); + if let (Some(t), TexKind::Rgba16F) = (tile, kind) + && let Some(out) = rgb_gray16_to_f16(&fmt, t.bytes()).or_else(|| rgb_gray32_to_f16(&fmt, t.bytes())) + { + return out; + } + convert_rows(surface, tile, kind, c.rect()) +} + +/// [`convert_tile`]'s general path for `rect` (rows of one tile): each pixel through +/// `photocraft_raster::to_rgba`. A run of identical pixels (the transparent or flat areas around a +/// brush dab) is converted once: the same samples give the same texels. +fn convert_rows(surface: &Surface, tile: Option<&Arc>, kind: TexKind, rect: Rect) -> Vec { + let n = rect.width() as usize * rect.height() as usize; + let fmt = surface.format(); + let ch = fmt.channels(); + let raw: Vec = match tile { + Some(_) => surface.read_region(rect), + None => surface.default_pixel().repeat(n), + }; + let mut out = Vec::with_capacity(n * kind.bytes_per_pixel()); + // The previous pixel and its colour. + let mut last: Option<(usize, [f32; 4])> = None; + for (i, px) in raw.chunks_exact(ch).enumerate() { + let mut rgba = || -> [f32; 4] { + if let Some((j, v)) = last + && raw.get(j * ch..j * ch + ch).is_some_and(|p| p.iter().zip(px).all(|(a, b)| a.to_bits() == b.to_bits())) + { + return v; + } + let v = photocraft_raster::to_rgba(&fmt, px); + last = Some((i, v)); + v + }; + match kind { + TexKind::Rgba8Direct | TexKind::Rgba8 => { + let v = rgba(); + out.extend(v.map(|x| (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8)); + } + TexKind::Rgba16F => { + let v = rgba(); + for x in v { + out.extend(f32_to_f16(x).to_le_bytes()); + } + } + TexKind::R8Direct => out.push((px[0].clamp(0.0, 1.0) * 255.0 + 0.5) as u8), + TexKind::R16F => out.extend(f32_to_f16(px[0]).to_le_bytes()), + TexKind::R32F => out.extend(px[0].to_le_bytes()), + } + } + out +} + +/// RGBA16F texels of a 16-bit RGB or grayscale tile, through a table of every 16-bit sample's +/// half: the same bytes as the general path at a fraction of its cost (a 50 MP 16-bit refresh +/// converts dozens of tiles). `None` for other formats. +pub(crate) fn rgb_gray16_to_f16(fmt: &PixelFormat, bytes: &[u8]) -> Option> { + use photocraft_color::ColorMode; + static HALF: std::sync::OnceLock> = std::sync::OnceLock::new(); + if fmt.sample != SampleType::U16 { + return None; + } + let gray = match fmt.mode { + ColorMode::Rgb => false, + ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => true, + _ => return None, + }; + let half = HALF.get_or_init(|| (0..=u16::MAX).map(|v| f32_to_f16(v as f32 / 65535.0)).collect()); + let one = f32_to_f16(1.0).to_le_bytes(); + let ch = fmt.channels(); + let mut out = Vec::with_capacity((TILE_SIZE * TILE_SIZE) as usize * 8); + for px in bytes.chunks_exact(ch * 2) { + let h = |i: usize| -> [u8; 2] { + let v = px.get(i * 2..i * 2 + 2).map_or(0, |b| u16::from_ne_bytes([b[0], b[1]])); + half.get(v as usize).copied().unwrap_or(0).to_le_bytes() + }; + let (r, g, b) = if gray { (h(0), h(0), h(0)) } else { (h(0), h(1), h(2)) }; + let a = if fmt.alpha { h(ch - 1) } else { one }; + out.extend_from_slice(&r); + out.extend_from_slice(&g); + out.extend_from_slice(&b); + out.extend_from_slice(&a); + } + Some(out) +} + +/// RGBA16F texels of a 32-bit float RGB or grayscale tile, straight from its samples: the same +/// bytes as the general path without decoding the tile into a float buffer and converting it +/// pixel by pixel (a brush dab on a 32-bit layer re-uploads the tiles it touched). `None` for +/// other formats. +pub(crate) fn rgb_gray32_to_f16(fmt: &PixelFormat, bytes: &[u8]) -> Option> { + use photocraft_color::ColorMode; + if fmt.sample != SampleType::F32 { + return None; + } + let gray = match fmt.mode { + ColorMode::Rgb => false, + ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => true, + _ => return None, + }; + let one = f32_to_f16(1.0).to_le_bytes(); + let ch = fmt.channels(); + let mut out = Vec::with_capacity((TILE_SIZE * TILE_SIZE) as usize * 8); + for px in bytes.chunks_exact(ch * 4) { + let h = |i: usize| -> [u8; 2] { + let v = px.get(i * 4..i * 4 + 4).map_or(0.0, |b| f32::from_ne_bytes([b[0], b[1], b[2], b[3]])); + f32_to_f16(v).to_le_bytes() + }; + let (r, g, b) = if gray { + let v = h(0); + (v, v, v) + } else { + (h(0), h(1), h(2)) + }; + let a = if fmt.alpha { h(ch - 1) } else { one }; + out.extend_from_slice(&r); + out.extend_from_slice(&g); + out.extend_from_slice(&b); + out.extend_from_slice(&a); + } + Some(out) +} + +pub(crate) fn lut_texture(device: &wgpu::Device, queue: &wgpu::Queue, rows: &[[f32; 4096]]) -> (wgpu::Texture, wgpu::TextureView) { + let t = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_compose_lut"), + size: wgpu::Extent3d { width: 4096, height: rows.len() as u32, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::R32Float, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + let bytes: Vec = rows.iter().flat_map(|r| r.iter().flat_map(|v| v.to_le_bytes())).collect(); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &t, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + &bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(4096 * 4), rows_per_image: Some(rows.len() as u32) }, + wgpu::Extent3d { width: 4096, height: rows.len() as u32, depth_or_array_layers: 1 }, + ); + let v = t.create_view(&Default::default()); + (t, v) +} + +#[derive(Clone, Copy)] +pub(crate) enum W { + I(i32), + U(u32), + F(f32), +} + +pub(crate) fn words(v: &[W]) -> Vec { + v.iter() + .flat_map(|w| match w { + I(i) => i.to_le_bytes(), + U(u) => u.to_le_bytes(), + F(f) => f.to_le_bytes(), + }) + .collect() +} + +/// IEEE half from f32 (round to nearest even; overflow → inf). +pub fn f32_to_f16(x: f32) -> u16 { + let b = x.to_bits(); + let sign = ((b >> 16) & 0x8000) as u16; + let exp = ((b >> 23) & 0xff) as i32; + let man = b & 0x7f_ffff; + if exp == 0xff { + return sign | 0x7c00 | if man != 0 { 0x200 } else { 0 }; + } + let e = exp - 127 + 15; + if e >= 0x1f { + return sign | 0x7c00; + } + if e <= 0 { + if e < -10 { + return sign; + } + let m = man | 0x80_0000; + let shift = (14 - e) as u32; + let half = 1u32 << (shift - 1); + let rounded = (m + half - 1 + ((m >> shift) & 1)) >> shift; + return sign | rounded as u16; + } + let mut h = ((e as u32) << 10) | (man >> 13); + let rest = man & 0x1fff; + if rest > 0x1000 || (rest == 0x1000 && (h & 1) == 1) { + h += 1; + } + sign | h as u16 +} + +/// f32 from IEEE half. +pub fn f16_to_f32(h: u16) -> f32 { + let sign = if h & 0x8000 != 0 { -1.0 } else { 1.0 }; + let exp = ((h >> 10) & 0x1f) as i32; + let man = (h & 0x3ff) as f32; + match exp { + 0 => sign * man * 2f32.powi(-24), + 0x1f => { + if man == 0.0 { + sign * f32::INFINITY + } else { + f32::NAN + } + } + e => sign * (1.0 + man / 1024.0) * 2f32.powi(e - 15), + } +} diff --git a/crates/gpu/src/encode.rs b/crates/gpu/src/encode.rs new file mode 100644 index 0000000000..cfa4bbd723 --- /dev/null +++ b/crates/gpu/src/encode.rs @@ -0,0 +1,423 @@ +use super::batch::{batch_runs, run_starts, submit_batch, tile_grid}; +use super::consts::{FLUSH_BYTES, FLUSH_OPS, FLUSH_WORK, PATTERN_IDLE_FRAMES}; +use super::convert::lut_texture; +use super::kit::FrameRes; +use super::paging::{OVER_BUDGET, cell_rect, first_error, grid_rect_count, grid_rects, lru_victims, scoped_work, uniform_buffer_len}; +use super::textures::{ChunkOut, FxKey, ResKey, Resident, Stats}; +use super::{Compositor, Document, LayerId, Plan, Rect, Surface, Unsupported, plan, timing}; + +impl Compositor { + /// Composite `region` of `doc`. Each finished chunk is passed to `sink` while its encoder is + /// still open; the chunk texture is reused afterwards, so the sink must record any copies + /// or passes that read it into the given encoder. Submits the work before returning; a + /// refresh of a huge document also submits in between (the sink may be handed a new + /// encoder), so its uploads and evicted pages never pile up in memory. + pub fn render( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + doc: &Document, + region: Rect, + mut sink: impl FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), + ) -> Result { + let result = scoped_work(device, self.health.clone(), || { + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose") }); + if self.timer.as_ref().is_some_and(timing::GpuTimer::busy) { + // The readback map completes on a poll. The app polls every frame, but a + // non-blocking poll here keeps the measurements coming whatever the caller does. + match &self.health { + Some(h) => h.poll(device), + None => { + let _ = device.poll(wgpu::PollType::Poll); + } + } + } + // Whatever came back since the last render travels out with this one's stats, so + // nothing has to reach back into the compositor for it afterwards. + let landed = self.timer.as_mut().and_then(timing::GpuTimer::take); + if let Some(t) = &mut self.timer { + let r = region.intersect(&doc.bounds()); + t.start(&mut encoder, r.width() as u64 * r.height() as u64); + } + let mut stats = self.encode_inner(device, queue, &mut encoder, doc, region, &mut sink, true); + if let Ok(s) = &mut stats { + s.gpu_time = landed; + } + if let Some(t) = &mut self.timer { + // A refresh that failed part way measures nothing: its spans describe work the + // caller is about to redo on the CPU. + match stats { + Ok(_) => t.finish(&mut encoder), + Err(_) => { + t.cancel(); + // The refresh is lost, but a measurement that came back with it is not: + // hand it to the next render rather than drop it on the fallback path. + if let Some(landed) = landed { + t.restore(landed); + } + } + } + } + let stats = stats?; + queue.submit([encoder.finish()]); + if let Some(t) = &mut self.timer { + t.submitted(); + } + Ok(stats) + }); + if result.is_err() { + self.discard_failed_work(); + } + result + } + + /// Like [`Self::render`] but records into `encoder` without submitting. + pub fn encode( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: &Document, + region: Rect, + sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), + ) -> Result { + let result = scoped_work(device, self.health.clone(), || self.encode_inner(device, queue, encoder, doc, region, sink, false)); + if result.is_err() { + self.discard_failed_work(); + } + result + } + + fn discard_failed_work(&mut self) { + // Cache metadata advances while recording. A failed encoder may never execute its + // texture uploads or effect maps, so none of those cached pixels can be trusted. + self.prefix.clear(); + self.residents.clear(); + self.fx.clear(); + self.patterns.clear(); + self.temps.clear(); + self.pool.clear(); + } + + #[allow(clippy::too_many_arguments)] + fn encode_inner( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: &Document, + region: Rect, + sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), + flush: bool, + ) -> Result { + if let Some(f) = self.fault() { + return Err(Unsupported(f.to_string())); + } + // CMYK layers convert through the document's own CMYK profile (uploads and plan colours). + let space = photocraft_compose::cmyk_space(doc); + self.cmyk = space.as_ref().map_or(0, |s| s.id); + photocraft_color::convert::with_cmyk_space(space.as_ref(), || self.encode_scoped(device, queue, encoder, doc, region, sink, flush)) + } + + #[allow(clippy::too_many_arguments)] + fn encode_scoped( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: &Document, + region: Rect, + sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), + flush: bool, + ) -> Result { + let canvas = doc.bounds(); + let region = region.intersect(&canvas); + let plan = plan(doc)?; + self.check_fx(doc, &plan)?; + self.pattern_mode = doc.pixel_format().mode; + self.pattern_doc = doc.id; + let mut stats = Stats { passes: plan.passes.len(), slots: plan.slots, ..Default::default() }; + if region.is_empty() { + return Ok(stats); + } + let page = self.page; + let mut cells = grid_rects(region, page); + // Check every actual page cell before uploads, submissions or sink output. A later + // cell can need more chunk records than the first; a failure must leave the device healthy. + let buffer_limit = device.limits().max_buffer_size; + for part in &cells { + uniform_buffer_len(grid_rect_count(*part, self.chunk)?, plan.passes.len(), buffer_limit)?; + } + if self.frame == 0 { + // The first submission after device creation is dropped on some drivers (RADV), + // which would render the first frame black; land it on an empty buffer instead. + queue.submit(std::iter::empty::()); + } + self.frame += 1; + self.stamp += 1; + self.staged = 0; + self.recorded = 0; + self.layer_alloc = None; + self.admit_freed = 0; + + // Only render owns submission. A caller may discard encode's command buffer, so it + // cannot establish (or consume) a checkpoint whose GPU copy might never execute. + // Runs of plain layers drawn as one pass each (`fs_layerbatch`). The grouping is the same + // for every render of this plan, so a layer's bits don't depend on the cache: renders + // capture and resume only where a run starts. + let runs = batch_runs(&plan); + let checkpoint = if flush { + let starts = run_starts(&runs); + let resumable: Vec<_> = plan.checkpoints.iter().filter(|c| starts.get(c.pass_end).copied().unwrap_or(false)).copied().collect(); + self.prefix.prepare(doc, &resumable) + } else { + None + }; + + // Effect maps of whole regions first (they may render group shapes through sub-plans); + // regions larger than the limit are built per cell below. + let paged: Vec = plan.fx.iter().map(|f| self.fx_paged(f.region)).collect(); + let aprons: Vec = plan.fx.iter().zip(&paged).map(|(f, &pg)| if pg { Self::fx_apron(doc, f) } else { 0 }).collect(); + for (f, &pg) in plan.fx.iter().zip(&paged) { + if !pg { + self.sync_fx(device, queue, encoder, doc, f, (f.layer.id, None), f.region, &mut stats)?; + } + } + let fr = self.frame_res(device, queue, canvas, &plan); + + // Page cells; every other frame in reverse, so a repeated full refresh of a document + // larger than the budget starts with the pages still resident. + if self.frame.is_multiple_of(2) { + cells.reverse(); + } + if let Some(f) = self.focus { + let p = page as i32; + let focused = |r: &Rect| !cell_rect((r.x0.div_euclid(p), r.y0.div_euclid(p)), page).intersect(&f).is_empty(); + cells.sort_by_key(focused); + } + let flush_bytes = FLUSH_BYTES.min(self.memory_budget() / 8).max(16 << 20); + stats.cells = cells.len(); + // The previous intermediate submit (see `submit_batch`). + let mut last_submit: Option = None; + // Work recorded and bytes evicted since the last submit. + let (mut work, mut freed) = (0u64, 0u64); + for part in cells { + self.stamp += 1; + let cell = (part.x0.div_euclid(page as i32), part.y0.div_euclid(page as i32)); + let cr = cell_rect(cell, page); + for (i, f) in plan.fx.iter().enumerate() { + let pf = cr.intersect(&f.region); + if !paged.get(i).copied().unwrap_or(false) || pf.is_empty() { + continue; + } + let window = pf.inflate(aprons.get(i).copied().unwrap_or(0)).intersect(&f.region); + self.sync_fx(device, queue, encoder, doc, f, (f.layer.id, Some(cell)), window, &mut stats)?; + } + let chunks = grid_rects(part, self.chunk); + stats.chunks += chunks.len(); + work += part.width() as u64 * part.height() as u64 * plan.passes.len() as u64; + let mut batch = flush.then_some(&mut last_submit); + self.run_cell(device, queue, encoder, doc.id, &plan, &runs, &paged, cell, &fr, &chunks, checkpoint, &mut stats, sink, &mut batch); + // The device was lost in a submit inside the cell: issue no more work. + if let Some(f) = self.fault() { + return Err(Unsupported(f.to_string())); + } + // Out of memory although every page this cell doesn't use was dropped: the work recorded + // since the last submit can still hold those pages. Submit it, wait until the GPU is done + // with it so their memory is released, and run the cell once more. `encode` can't + // submit, so there the refresh goes to the CPU straight away. + if flush && self.layer_alloc.as_deref() == Some(OVER_BUDGET) { + self.layer_alloc = None; + submit_batch(device, queue, encoder, self.health.as_ref(), self.timer.as_mut(), &mut last_submit); + #[cfg(not(target_arch = "wasm32"))] + { + let idle = match self.health.as_ref() { + Some(h) => h.wait_idle(device, last_submit.clone()), + None => device.poll(wgpu::PollType::Wait { submission_index: last_submit.clone(), timeout: None }).is_ok(), + }; + // The GPU has run every timestamp submitted so far, so a retry that fails too + // can drop this measurement without switching timing off for good. + if idle && let Some(t) = self.timer.as_mut() { + t.drained(); + } + } + self.staged = 0; + self.recorded = 0; + work = 0; + freed = 0; + self.admit_freed = 0; + stats.flushes += 1; + if let Some(f) = self.fault() { + return Err(Unsupported(f.to_string())); + } + let mut batch = Some(&mut last_submit); + self.run_cell(device, queue, encoder, doc.id, &plan, &runs, &paged, cell, &fr, &chunks, checkpoint, &mut stats, sink, &mut batch); + if let Some(f) = self.fault() { + return Err(Unsupported(f.to_string())); + } + } + // A page didn't fit even after evicting: this refresh goes to the CPU, the next one + // tries the GPU again (#6563). + if let Some(e) = self.layer_alloc.take() { + return Err(Unsupported(e)); + } + let (evicted, bytes) = self.evict(); + stats.evicted += evicted; + freed = freed.saturating_add(bytes.saturating_add(std::mem::take(&mut self.admit_freed))); + if flush && (self.staged >= flush_bytes || work >= FLUSH_WORK || freed >= flush_bytes || self.recorded >= FLUSH_OPS) { + submit_batch(device, queue, encoder, self.health.as_ref(), self.timer.as_mut(), &mut last_submit); + self.staged = 0; + self.recorded = 0; + work = 0; + freed = 0; + stats.flushes += 1; + // The device was lost mid-refresh: issue no more work. + if let Some(f) = self.fault() { + return Err(Unsupported(f.to_string())); + } + } + } + + // Evict this document's textures whose layers are gone (hidden layers stay resident so + // toggling visibility costs no upload). + let frame = self.frame; + let live: std::collections::HashSet = doc.walk().into_iter().map(|(_, _, l)| l.id).collect(); + self.residents.retain(|(id, _, _), r| r.doc != doc.id || r.last_used == frame || live.contains(id)); + self.fx.retain(|(id, _), e| e.doc != doc.id || e.last_used == frame || live.contains(id)); + stats.evicted += self.evict().0; + self.temps.retain(|(_, used)| frame.saturating_sub(*used) < 240); + self.evict_patterns(); + Ok(stats) + } + + /// Drop the pattern textures no frame has painted with for [`PATTERN_IDLE_FRAMES`] frames, + /// then the least recently painted ones over the pattern budget. Never one this frame painted + /// with: it would only be uploaded again on the next frame. + fn evict_patterns(&mut self) { + let frame = self.frame; + self.patterns.retain(|_, p| frame.saturating_sub(p.last_used) < PATTERN_IDLE_FRAMES); + let mut total = self.pattern_cache_bytes(); + if total > self.pattern_budget { + let mut lru: Vec<(u64, String, u64)> = + self.patterns.iter().filter(|(_, p)| p.last_used != frame).map(|(k, p)| (p.last_used, k.clone(), p.bytes)).collect(); + lru.sort_unstable(); + for (_, key, bytes) in lru { + if total <= self.pattern_budget { + break; + } + self.patterns.remove(&key); + total = total.saturating_sub(bytes); + } + } + } + + /// Keep resident pages within the resident budget and the effect cache within + /// its budget, least recently used first. Never drops what the current cell uses, nor + /// whole-region effect maps drawn this frame. Returns how many textures were dropped and + /// their bytes. + fn evict(&mut self) -> (usize, u64) { + let (mut n, mut freed) = self.evict_residents(self.resident_budget, 0, None); + let mut total = self.fx_cache_bytes(); + let fx_budget = usize::try_from(self.fx_budget).unwrap_or(usize::MAX); + if total > fx_budget { + let (frame, stamp) = (self.frame, self.stamp); + let mut lru: Vec<((u64, u64), FxKey)> = self + .fx + .iter() + .filter(|(k, e)| e.last_used != frame || (k.1.is_some() && e.stamp != stamp)) + .map(|(k, e)| ((e.last_used, e.stamp), *k)) + .collect(); + lru.sort_unstable_by_key(|c| c.0); + for (_, k) in lru { + if total <= fx_budget { + break; + } + if let Some(e) = self.fx.remove(&k) { + total = total.saturating_sub(e.bytes()); + n += 1; + freed += e.bytes() as u64; + } + } + } + (n, freed) + } + + /// Drop least recently used resident pages until `incoming` more bytes fit in `budget`. + /// Never drops what the current cell uses, nor `keep`. Returns how many pages were dropped + /// and their bytes. + fn evict_residents(&mut self, budget: u64, incoming: u64, keep: Option) -> (usize, u64) { + let stamp = self.stamp; + let pages: Vec<(u64, ResKey, u64)> = + self.residents.iter().filter(|(k, r)| r.stamp != stamp && Some(**k) != keep).map(|(k, r)| (r.stamp, *k, r.bytes())).collect(); + let (mut n, mut freed) = (0, 0u64); + for k in lru_victims(pages, self.resident_bytes(), incoming, budget) { + if let Some(r) = self.residents.remove(&k) { + n += 1; + freed += r.bytes(); + } + } + (n, freed) + } + + /// Create the page texture for `key`, `bytes` large. Least recently used pages make room in + /// the budget first; when the device is still out of memory, every page the current cell + /// doesn't use is dropped and creation is tried once more (#6563). `None` (with + /// `layer_alloc` set) when it still fails: `render` submits the recorded work, which can still + /// hold the dropped pages, and runs the cell once more; after that the CPU draws the refresh. + pub(crate) fn create_layer_texture( + &mut self, + device: &wgpu::Device, + desc: &wgpu::TextureDescriptor<'_>, + key: ResKey, + bytes: u64, + stats: &mut Stats, + ) -> Option { + let replaced = self.residents.get(&key).map_or(0, Resident::bytes); + let (n, dropped) = self.evict_residents(self.resident_budget, bytes.saturating_sub(replaced), Some(key)); + stats.evicted += n; + self.admit_freed = self.admit_freed.saturating_add(dropped); + // WebGPU reports errors asynchronously: its failures reach the health callbacks. + if cfg!(target_arch = "wasm32") { + return Some(device.create_texture(desc)); + } + for retry in [false, true] { + let (texture, mut error) = first_error(device, || device.create_texture(desc)); + if error.is_none() && self.fail_allocs > 0 { + self.fail_allocs -= 1; + error = Some(wgpu::Error::OutOfMemory { source: "out of memory (test hook)".into() }); + } + match error { + None => return Some(texture), + Some(wgpu::Error::OutOfMemory { .. }) if !retry => { + let (n, dropped) = self.evict_residents(0, 0, Some(key)); + stats.evicted += n; + self.admit_freed = self.admit_freed.saturating_add(dropped); + } + Some(wgpu::Error::OutOfMemory { .. }) => { + self.layer_alloc = Some(OVER_BUDGET.into()); + return None; + } + Some(e) => { + self.layer_alloc = Some(format!("GPU layer texture failed: {e}")); + return None; + } + } + } + self.layer_alloc = Some(OVER_BUDGET.into()); + None + } + + /// Per-frame resources of `plan`: the tile area of each pass's surfaces, its LUT and pattern. + fn frame_res(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, canvas: Rect, plan: &Plan<'_>) -> FrameRes { + let grid = tile_grid(canvas); + let area = |s: &Surface| s.tile_bounds().intersect(&grid); + let areas = plan + .passes + .iter() + .map(|p| (p.tex.as_ref().map_or(Rect::EMPTY, |t| area(t.surface.get())), p.mask.as_ref().map_or(Rect::EMPTY, |m| area(m.surface.get())))) + .collect(); + let luts = plan.passes.iter().map(|p| p.lut.as_ref().map(|rows| lut_texture(device, queue, rows))).collect(); + let patterns = plan.passes.iter().map(|p| p.pattern.map(|pat| self.pattern_view(device, queue, pat))).collect(); + FrameRes { areas, luts, patterns } + } +} diff --git a/crates/gpu/src/fx.rs b/crates/gpu/src/fx.rs index 01e147d51a..4e23b7d332 100644 --- a/crates/gpu/src/fx.rs +++ b/crates/gpu/src/fx.rs @@ -25,13 +25,14 @@ use std::collections::HashMap; use std::hash::{Hash, Hasher}; use std::sync::Arc; -use photocraft_compose::effects::BevelPaint; use photocraft_compose::effects::FieldKind; -use photocraft_doc::{BevelTechnique, Contour, Effect, GlobalLight, GlowSource, GlowTechnique, Layer, LayerContent, Pattern}; +use photocraft_doc::{Contour, Effect, GlobalLight, Layer, LayerContent, Pattern}; use photocraft_geom::{Rect, TileCoord}; use photocraft_raster::{Surface, Tile}; -use crate::plan::{Kernel, stroke_widths}; +use crate::plan::Kernel; + +mod builders; /// Input of a map stage. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -203,6 +204,11 @@ impl B { self.fields.push((kind, reach_for(width))); In::Field(kind) } + /// A chiselled bevel's inside and outside fields, always asked for together ([`bevel_fields`] + /// computes them as one). + fn bevel_fields(&mut self, width: f32) -> [In; 2] { + [FieldKind::BevelInside, FieldKind::BevelOutside].map(|k| self.field(k, width)) + } /// `Map::shifted` by whole pixels (`invert`: of `1 - a`); identity shifts of the plain map /// are skipped. fn shift(&mut self, src: In, dx: f32, dy: f32, outside: f32, invert: bool) -> In { @@ -220,6 +226,24 @@ impl B { let Some(k) = blur_kernel(size) else { return src }; self.conv(src, k) } + fn max(&mut self, a: In, b: In) -> In { + self.push(stage(Kernel::MFinish, Some(a), Some(b), [2.0, 0.0, 0.0, 0.0], 0)) + } + fn softer_glow_tail(&mut self, blur: In, size: f32, range: f32) -> In { + let width = photocraft_compose::effects::softer_glow_tail_width(size); + if width == 0.0 { + return blur; + } + let distance = self.field(FieldKind::Outside, size + width); + let tail = self.push(stage( + Kernel::MGlow, + Some(distance), + None, + [size - width, width, photocraft_compose::effects::softer_glow_tail_amplitude(range), 1.0], + 0, + )); + self.max(blur, tail) + } /// Separable convolution with a symmetric kernel `(radius, weights)` (two `MBlur` passes). fn conv(&mut self, src: In, (r, w): (i32, Vec)) -> In { if r <= 0 { @@ -301,182 +325,14 @@ pub(crate) fn program(e: &Effect, light: &GlobalLight, vector_shape: bool) -> Ma pub(crate) fn program_with(e: &Effect, light: &GlobalLight, vector_shape: bool, patterns: &[Pattern], anchor: (f64, f64)) -> MapProgram { let mut b = B { stages: Vec::new(), fields: Vec::new() }; let maps = match e { - Effect::DropShadow(s) | Effect::InnerShadow(s) => { - // shadow_map - let inner = matches!(e, Effect::InnerShadow(_)); - let angle = if s.use_global_light { light.angle } else { s.angle }; - let (dx, dy) = offset(angle, s.distance); - let src = b.shift(In::Shape, dx, dy, if inner { 1.0 } else { 0.0 }, inner); - let (r, bw) = photocraft_compose::effects::spread_split(s.size, s.spread); - let mut m = src; - if r > 0.0 { - // dist_outside of the shifted map is the shifted field (integer offsets; shifted-in - // pixels are far away for a drop shadow, inside for an inner shadow). - let f = b.field(if inner { FieldKind::ChokeInside } else { FieldKind::StrokeOutside }, r); - let d = if dx == 0.0 && dy == 0.0 { - f - } else { - b.push(stage(Kernel::MShift, Some(f), None, [dx, dy, if inner { -0.5 } else { 1e10 }, 0.0], dx.abs().max(dy.abs()) as i32)) - }; - m = b.dilate(src, d, r); - } - let m = b.blur(m, bw); - b.finish(m, None, false, &s.contour, inner, 0); - 1 - } - Effect::OuterGlow(g) | Effect::InnerGlow(g) => { - // glow_map - let inner = matches!(e, Effect::InnerGlow(_)); - let center = inner && g.source == GlowSource::Center; - match g.technique { - GlowTechnique::Precise => { - let d = b.field(if inner { FieldKind::Inside } else { FieldKind::Outside }, g.size); - let solid = g.size * g.spread; - let soft = (g.size + 1.0 - solid).max(1e-3); - let m = b.push(stage(Kernel::MGlow, Some(d), None, [solid, soft, 0.0, 0.0], 0)); - b.finish_glow(m, photocraft_compose::effects::glow_lut(g), inner, center); - } - GlowTechnique::Softer => { - // Inner glows (edge, and centre as 1 - the edge result) spread 1 - alpha. - let src = if inner { b.shift(In::Shape, 0.0, 0.0, 0.0, true) } else { In::Shape }; - let (r, bw) = photocraft_compose::effects::spread_split(g.size, g.spread); - let mut m = src; - if r > 0.0 { - let d = b.field(if inner { FieldKind::ChokeInside } else { FieldKind::StrokeOutside }, r); - m = b.dilate(src, d, r); - } - let m = b.blur(m, bw); - b.finish_glow(m, photocraft_compose::effects::glow_lut(g), inner, center); - } - } - 1 - } - Effect::Satin(s) => { - // satin_map - let (dx, dy) = offset(s.angle, photocraft_compose::effects::satin_distance(s)); - let a = b.shift(In::Shape, dx, dy, 0.0, false); - let a = b.conv(a, photocraft_compose::effects::tent_kernel(s.size)); - let c = b.shift(In::Shape, -dx, -dy, 0.0, false); - let c = b.conv(c, photocraft_compose::effects::tent_kernel(s.size)); - b.finish_lut(a, Some(c), s.invert, photocraft_compose::effects::satin_lut(&s.contour), true, 0); - 1 - } - Effect::BevelEmboss(bv) => { - // bevel_maps: height map (tent blur of the shape, or chiselled distance ramps), then - // highlight / shadow shading inside and / or outside the shape. - let g = photocraft_compose::effects::bevel_geom(bv); - let size = bv.size.max(1.0); - let paint = |p: BevelPaint| match p { - BevelPaint::Outer => 0.0, - BevelPaint::Both => 1.0, - BevelPaint::Inner => 2.0, - }; - let mut h = if bv.technique == BevelTechnique::Smooth { - b.conv(In::Shape, photocraft_compose::effects::tent_kernel(g.width)) - } else { - let din = b.field(FieldKind::BevelInside, size); - let dout = b.field(FieldKind::BevelOutside, size); - let soft = g.chisel_soft > 0.0; - let h = b.push(stage(Kernel::MBevelH, Some(din), Some(dout), [paint(g.paint), size, f32::from(u8::from(soft)), 0.0], 0)); - // As `effects::bevel_height`: only emboss styles blur a Chisel Soft ramp. - if soft && g.paint == BevelPaint::Both { b.conv(h, photocraft_compose::effects::tent_kernel(g.chisel_soft)) } else { h } - }; - if let Some(c) = &bv.contour { - // Contour element: the height through the contour (`bevel_contour_lut`). - let lut = photocraft_compose::effects::bevel_contour_lut(c); - let mut st = stage(Kernel::MFinish, Some(h), None, [0.0, 0.0, 1.0, 0.0], 0); - st.lut = Some(Arc::new(lut)); - h = b.push(st); - } - if let Some(t) = &bv.texture - && let Some(pat) = photocraft_doc::pattern::find(patterns, &t.id, &t.name).filter(|p| p.is_paintable()) - { - // Texture element (`effects::bevel_height`): luminance × depth / unit added. - let unit = if bv.depth.abs() > 1e-6 { (g.depth / bv.depth).abs().max(1e-3) } else { g.width.max(1.0) }; - let pl = photocraft_compose::pattern::Placement::anchored(anchor, t.link, t.phase, t.scale, 0.0); - let (origin, cs, inv) = pl.parts(); - let mut st = stage(Kernel::MBevelTex, Some(h), None, [t.depth / unit, f32::from(u8::from(t.invert)), 0.0, 0.0], 0); - st.p3 = [origin.0 as f32, origin.1 as f32, cs.0 as f32, cs.1 as f32]; - st.p4 = [inv as f32, pat.width as f32, pat.height as f32, 0.0]; - st.pattern = Some(pat.clone()); - h = b.push(st); - } - if bv.soften >= 1.0 { - h = b.conv(h, photocraft_compose::effects::tent_kernel(bv.soften)); - } - let (angle, altitude) = if bv.use_global_light { (light.angle, light.altitude) } else { (bv.angle, bv.altitude) }; - let (sa, ca) = angle.to_radians().sin_cos(); - let (se, ce) = altitude.to_radians().sin_cos(); - let lut = contour_lut(&bv.gloss_contour).map(Arc::new); - // Region (`fs_mbevelshade`): 0 inside, 1 under the edge and outside, 2 / 3 emboss / - // pillow emboss (both halves in one map). - let passes: Vec<(f32, f32)> = match g.paint { - BevelPaint::Inner => vec![(g.depth, 0.0)], - BevelPaint::Outer => vec![(g.depth, 1.0)], - BevelPaint::Both => vec![(g.depth, if g.pillow { 3.0 } else { 2.0 })], - }; - let mut out = 0; - for (depth, region) in &passes { - for which in [0.0, 1.0] { - let mut s = stage(Kernel::MBevelShade, Some(h), None, [ca * ce, -sa * ce, se, se], 1); - s.p1 = [*depth, *region, which, f32::from(u8::from(lut.is_some()))]; - s.p3 = [f32::from(u8::from(g.paint == BevelPaint::Outer && bv.technique != BevelTechnique::Smooth)), 0.0, 0.0, 0.0]; - s.lut = lut.clone(); - s.s = Some(In::Shape); - s.out = Some(out); - out += 1; - b.stages.push(s); - } - } - out - } - Effect::Stroke(st) => { - // composite_with_effects' stroke bands (maps 0 outside, 1 inside) - let (in_w, out_w) = stroke_widths(st); - if out_w > 0.0 { - // Narrow strokes keep the fitted chamfer, wide ones the exact transform (#3646); - // the CPU picks the same field by the same width (`stroke_outside_exact`). - let kind = match (vector_shape, photocraft_compose::effects::stroke_outside_exact(out_w)) { - (true, true) => FieldKind::StrokeOutsideVector, - (true, false) => FieldKind::StrokeOutsideVectorChamfer, - (false, true) => FieldKind::StrokeOutsideExact, - (false, false) => FieldKind::StrokeOutside, - }; - let d = b.field(kind, out_w); - let mut s = stage(Kernel::MStroke, Some(d), None, [out_w, 0.0, 0.0, 0.0], 0); - s.out = Some(0); - b.stages.push(s); - } - if in_w > 0.0 { - let d = b.field(FieldKind::StrokeInside, in_w); - let mut s = stage(Kernel::MStroke, Some(d), None, [in_w, 0.0, 0.0, 0.0], 0); - s.out = Some(1); - b.stages.push(s); - } - 2 - } + Effect::DropShadow(s) | Effect::InnerShadow(s) => b.shadow(s, light, matches!(e, Effect::InnerShadow(_))), + Effect::OuterGlow(g) | Effect::InnerGlow(g) => b.glow(g, matches!(e, Effect::InnerGlow(_))), + Effect::Satin(s) => b.satin(s), + Effect::BevelEmboss(bv) => b.bevel(bv, light, patterns, anchor), + Effect::Stroke(st) => b.stroke(st, vector_shape), Effect::ColorOverlay { .. } | Effect::GradientOverlay { .. } | Effect::PatternOverlay { .. } => return MapProgram::empty(), }; - let mut h = std::collections::hash_map::DefaultHasher::new(); - for s in &b.stages { - (format!("{:?}", s.kernel), s.a, s.b, s.s, s.radius, s.out).hash(&mut h); - for v in s.p0.iter().chain(&s.p1).chain(&s.p3).chain(&s.p4) { - v.to_bits().hash(&mut h); - } - if let Some(p) = &s.pattern { - (&p.id, &p.name, p.width, p.height).hash(&mut h); - for (c, t) in p.surface.tiles() { - (c.tx, c.ty, Arc::as_ptr(t) as usize).hash(&mut h); - } - } - if let Some(l) = &s.lut { - for v in l.iter() { - v.to_bits().hash(&mut h); - } - } - } - (maps, &b.fields).hash(&mut h); - MapProgram { key: h.finish() | 1, maps, stages: b.stages, fields: b.fields } + MapProgram { key: b.key(maps), maps, stages: b.stages, fields: b.fields } } // --------------------------------------------------------------------------------------------- @@ -542,20 +398,51 @@ fn threaded_inside(kind: FieldKind) -> bool { /// fields run on all threads inside `distance_field`, from one window. That is also the window the CPU /// compositor computes them from, so a rebuilt field is exactly the one it gives. fn field_banded(kind: FieldKind, reach: i32, shape: &[f32], region: Rect, out: Rect, halo: i32) -> Vec { - if threaded_inside(kind) { - let win = out.inflate(halo).intersect(®ion); - if win.is_empty() { - return Vec::new(); - } - let f = photocraft_compose::effects::distance_field(kind, crop(shape, region, win), win.width() as usize, win.height() as usize, reach as f32); - return if win == out { f } else { crop(&f, win, out) }; - } - let parts = par_map(bands(out, BAND.max(halo)), |band| { - let win = band.inflate(halo).intersect(®ion); - let f = photocraft_compose::effects::distance_field(kind, crop(shape, region, win), win.width() as usize, win.height() as usize, reach as f32); - crop(&f, win, band) + let [f] = fields_in_windows(threaded_inside(kind), shape, region, out, halo, |s, w, h| { + [photocraft_compose::effects::distance_field(kind, s, w, h, reach as f32)] }); - parts.concat() + f +} + +/// [`field`] of both bevel fields over the same `out`, `[BevelInside, BevelOutside]`, with the +/// sharp edges of each window found once (the two kinds have the same radius and threading). +pub(crate) fn bevel_fields(reach: i32, shape: &[f32], region: Rect, out: Rect) -> [Vec; 2] { + let kind = FieldKind::BevelInside; + fields_in_windows(threaded_inside(kind), shape, region, out, field_radius(kind, reach) + 1, |s, w, h| { + photocraft_compose::effects::bevel_distance_fields(s, w, h, reach as f32) + }) +} + +/// The fields `compute` gives for a `w × h` window of the region-sized `shape`, over `out` (inside +/// `region`), each window grown by `halo`: `out` as one window when `whole` (the field spreads over +/// threads itself), else bands of it in parallel. +fn fields_in_windows( + whole: bool, + shape: &[f32], + region: Rect, + out: Rect, + halo: i32, + compute: impl Fn(Vec, usize, usize) -> [Vec; N] + Sync + Send, +) -> [Vec; N] { + let part = |r: Rect| -> [Vec; N] { + let win = r.inflate(halo).intersect(®ion); + if win.is_empty() { + return std::array::from_fn(|_| Vec::new()); + } + let f = compute(crop(shape, region, win), win.width() as usize, win.height() as usize); + if win == r { f } else { f.map(|f| crop(&f, win, r)) } + }; + if whole { + return part(out); + } + let parts = par_map(bands(out, BAND.max(halo)), part); + std::array::from_fn(|k| { + let mut f = Vec::with_capacity(parts.iter().map(|p| p[k].len()).sum()); + for p in &parts { + f.extend_from_slice(&p[k]); + } + f + }) } /// The layer's shape (`compose::layer_shape`) over `r`, in parallel bands. @@ -629,7 +516,23 @@ pub(crate) fn shape_key(layer: &Layer, canvas: Rect) -> u64 { LayerContent::Shape(sh) => { std::mem::discriminant(&layer.content).hash(&mut h); if photocraft_compose::effect_outline(layer).is_some() { - format!("{:?}", sh.path).hash(&mut h); + // The cached maps live in region-local coordinates. Preserve their identity + // under translation so sync_fx can verify coverage and reuse them after a move. + let mut outline = + sh.path.control_bounds().map_or_else(|| sh.path.clone(), |(x, y, _, _)| sh.path.transform(&photocraft_geom::Affine::translate(-x, -y))); + // Integer translation can perturb fractional f64 coordinates in the low bits. + // Quantize only the fingerprint (1/65536 px); rendering keeps the original path, + // and sync_fx still requires exact coverage equality before reusing moved maps. + for subpath in &mut outline.subpaths { + for knot in &mut subpath.knots { + for point in [&mut knot.anchor, &mut knot.in_ctrl, &mut knot.out_ctrl] { + // Adding zero also canonicalizes negative zero in Debug output. + point.x = (point.x * 65536.0).round() / 65536.0 + 0.0; + point.y = (point.y * 65536.0).round() / 65536.0 + 0.0; + } + } + } + format!("{outline:?}").hash(&mut h); } } other => std::mem::discriminant(other).hash(&mut h), @@ -682,6 +585,8 @@ pub(crate) fn group_key(layer: &Layer, light: &GlobalLight, pin: &mut dyn FnMut( surface_fp(&m.surface, h, pin); } format!("{:?}{:?}", l.vector_mask, l.effects).hash(h); + l.blend_if.hash(h); + l.advanced.hash(h); h.write_u8(0xfe); } let mut h = std::collections::hash_map::DefaultHasher::new(); @@ -695,6 +600,7 @@ pub(crate) fn group_key(layer: &Layer, light: &GlobalLight, pin: &mut dyn FnMut( (m.enabled, m.density.to_bits(), m.feather.to_bits()).hash(&mut h); surface_fp(&m.surface, &mut h, pin); } + format!("{:?}", layer.vector_mask).hash(&mut h); } _ => walk(layer, &mut h, pin), } @@ -703,215 +609,4 @@ pub(crate) fn group_key(layer: &Layer, light: &GlobalLight, pin: &mut dyn FnMut( #[cfg(test)] #[allow(clippy::unreachable)] // clippy.toml exempts unwrap/expect/panic in tests, not unreachable! -mod tests { - use super::*; - use photocraft_color::BlendMode; - use photocraft_doc::FxCommon; - - #[test] - fn programs_key_on_geometry_not_colour() { - let light = GlobalLight::default(); - let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; - let a = program(&Effect::DropShadow(s.clone()), &light, false); - s.color = photocraft_color::Color::rgb(1.0, 0.0, 0.0); - s.common = FxCommon::new(BlendMode::Screen, 0.3); - let b = program(&Effect::DropShadow(s.clone()), &light, false); - assert_eq!(a.key, b.key); - s.size += 1.0; - assert_ne!(a.key, program(&Effect::DropShadow(s), &light, false).key); - } - - #[test] - fn windows_grow_by_reach_and_temps_are_reused() { - let light = GlobalLight::default(); - let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; - s.spread = 0.5; - let p = program(&Effect::DropShadow(s), &light, false); - let region = Rect::new(-100, -100, 1000, 1000); - let d = Rect::new(0, 0, 10, 10); - let fr = |_| 5; - let w = p.windows(d, region, &fr); - let last = *w.last().unwrap(); - assert!(last.contains_rect(&d.inflate(7)), "{last:?}"); - // Earlier stages need at least as much as later ones read. - assert!(w[0].width() >= last.width()); - let (t, n) = p.temps(); - assert!(n <= 3, "{n} temporaries for {} stages", p.stages.len()); - assert!(t.last().unwrap().is_none()); - } - - /// A distance field computed in bands from grown windows equals the whole-region field - /// wherever it is within its reach (the property incremental updates rely on). - #[test] - fn banded_fields_match_whole_region_within_reach() { - let region = Rect::new(-20, -10, 300, 290); - let (w, h) = (region.width() as usize, region.height() as usize); - let mut shape = vec![0.0f32; w * h]; - for y in 0..h { - for x in 0..w { - let (fx, fy) = (x as f32 - 150.0, y as f32 - 140.0); - let r = (fx * fx + fy * fy).sqrt() + 8.0 * (fy * 0.07).sin() * (fx * 0.05).cos(); - shape[y * w + x] = (90.0 - r).clamp(0.0, 1.0) * if (x / 13 + y / 17) % 5 == 0 { 0.6 } else { 1.0 }; - } - } - for kind in [ - FieldKind::Outside, - FieldKind::Inside, - FieldKind::BevelOutside, - FieldKind::BevelInside, - FieldKind::ChokeInside, - FieldKind::StrokeOutside, - FieldKind::StrokeInside, - FieldKind::StrokeOutsideExact, - FieldKind::StrokeOutsideVector, - FieldKind::StrokeOutsideVectorChamfer, - ] { - let reach = 9; - let whole = photocraft_compose::effects::distance_field(kind, shape.clone(), w, h, reach as f32); - let banded = field(kind, reach, &shape, region, region); - let part = Rect::new(40, 30, 170, 120); - let partial = field(kind, reach, &shape, region, part); - for (i, (a, b)) in whole.iter().zip(&banded).enumerate() { - if a.min(*b) < reach as f32 { - assert!((a - b).abs() < 1e-5, "{kind:?} at {i}: {a} vs {b}"); - } - } - for (i, b) in partial.iter().enumerate() { - let (x, y) = (part.x0 + (i % part.width() as usize) as i32, part.y0 + (i / part.width() as usize) as i32); - let a = whole[(y - region.y0) as usize * w + (x - region.x0) as usize]; - if a.min(*b) < reach as f32 { - assert!((a - b).abs() < 1e-5, "{kind:?} at ({x},{y}): {a} vs {b}"); - } - } - } - } - #[test] - fn windowed_bevel_fields_match_whole_region_on_anti_aliased_edges() { - // The bevel fields read the edge pixels around the foot of the perpendicular and take the - // direction of each from its neighbours, so the halo of a part of the region has to cover all of - // that. A wide bar with clean anti-aliased edges (almost along the 128 row bands the other - // fields use, across them and at an angle) puts sharp edge pixels where bands would meet and - // inside the partial window. The whole region is one window, so it is exactly the CPU's field. - let region = Rect::new(-20, -10, 300, 290); - let (w, h) = (region.width() as usize, region.height() as usize); - let part = Rect::new(40, 30, 170, 120); - let mut too_small_noticed = false; - for deg in [1.5f32, 5.0, 30.0, 88.5] { - let (sn, cs) = deg.to_radians().sin_cos(); - let inside = |x: f32, y: f32| { - let (u, v) = ((x - 150.0) * cs + (y - 128.3) * sn, -(x - 150.0) * sn + (y - 128.3) * cs); - u.abs() <= 140.0 && v.abs() <= 60.0 - }; - let mut shape = vec![0.0f32; w * h]; - for y in 0..h { - for x in 0..w { - let hits = (0..64).filter(|k| inside(x as f32 + ((k % 8) as f32 + 0.5) / 8.0, y as f32 + ((k / 8) as f32 + 0.5) / 8.0)).count(); - shape[y * w + x] = hits as f32 / 64.0; - } - } - for reach in [9, 24] { - for kind in [FieldKind::BevelInside, FieldKind::BevelOutside] { - let whole = photocraft_compose::effects::distance_field(kind, shape.clone(), w, h, reach as f32); - let differs = |part_field: &[f32], out: Rect| { - let ow = out.width() as usize; - part_field - .iter() - .enumerate() - .filter(|&(i, b)| { - let (x, y) = (out.x0 + (i % ow) as i32, out.y0 + (i / ow) as i32); - let a = whole[(y - region.y0) as usize * w + (x - region.x0) as usize]; - a.min(*b) < reach as f32 && (a - b).abs() > 1e-5 - }) - .count() - }; - let all = field(kind, reach, &shape, region, region); - assert!( - all.iter().zip(&whole).all(|(a, b)| a.to_bits() == b.to_bits()), - "{kind:?}, {deg} deg, reach {reach}: the whole region is not the CPU's field" - ); - assert_eq!( - differs(&field(kind, reach, &shape, region, part), part), - 0, - "{kind:?}, {deg} deg, reach {reach}: partial window differs from whole" - ); - // The test can see a halo that is too small: two rows past the part is not enough. - too_small_noticed |= differs(&field_banded(kind, reach, &shape, region, part, 2), part) > 0; - } - } - } - assert!(too_small_noticed, "a 2 row halo went unnoticed, so this test guards nothing"); - } - - /// #3646: a stroke's outer edge is the fitted 5 × 5 chamfer while it is narrow and the exact - /// Euclidean transform once it is wide — the chamfer's octagon facets a wide stroke. The GPU - /// builds it from the field its width picks (`stroke_outside_exact`), while the spread of drop - /// shadows and softer outer glows keeps the chamfer field (fitted to Photoshop, #224) — so - /// routing a wide stroke through the chamfer, or a shadow through the exact field, fails here. - #[test] - fn a_stroke_uses_the_chamfer_narrow_and_the_exact_field_wide() { - use photocraft_color::{BlendMode, Color}; - use photocraft_doc::{FxCommon, FxPaint, StrokeFx, StrokePosition}; - let light = GlobalLight::default(); - let stroke = - |size, position| Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, 1.0), size, position, paint: FxPaint::Color(Color::BLACK) }); - // Narrow (< STROKE_CHAMFER_MAX): the chamfer, exactly as before #3646. - let narrow = program(&stroke(6.0, StrokePosition::Outside), &light, false); - assert!(narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "a narrow raster outside stroke keeps the chamfer: {:?}", narrow.fields); - assert!(!narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideExact), "…not the exact field: {:?}", narrow.fields); - // Wide: the exact transform. - let wide = program(&stroke(40.0, StrokePosition::Outside), &light, false); - assert!( - wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideExact), - "a wide raster outside stroke needs the exact outer field: {:?}", - wide.fields - ); - assert!(!wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "…not the chamfer field: {:?}", wide.fields); - // A shape layer's outline stroke follows the same width rule; the inside edge keeps the chamfer. - let vector_narrow = program(&stroke(6.0, StrokePosition::Outside), &light, true); - assert!(vector_narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideVectorChamfer), "{:?}", vector_narrow.fields); - let vector_wide = program(&stroke(40.0, StrokePosition::Outside), &light, true); - assert!(vector_wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideVector), "{:?}", vector_wide.fields); - let inner = program(&stroke(40.0, StrokePosition::Inside), &light, false); - assert!(inner.fields.iter().any(|(k, _)| *k == FieldKind::StrokeInside), "{:?}", inner.fields); - // The shadow / softer-glow spread is unchanged: still the chamfer. - let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; - s.spread = 0.5; - let shadow = program(&Effect::DropShadow(s), &light, false); - assert!(shadow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "the shadow spread keeps the chamfer: {:?}", shadow.fields); - } - - /// Cached shapes and group keys reserve tile addresses without keeping pixels (#4530): - /// damage still tells a replaced tile from an unchanged one, and a dropped tile's pixels - /// are freed although the entry remembers it. - #[test] - fn shape_tiles_and_group_pins_hold_addresses_not_pixels() { - use photocraft_color::{ColorMode, PixelFormat, SampleType}; - let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::U8, true); - let mut s = Surface::new(fmt); - // A ramp, so no two tiles share their pixels. - for x in 0..600 { - let v = x as f32 / 600.0; - s.fill_rect(Rect::new(x, 0, x + 1, 300), &[v, 0.2, 0.9, 1.0]); - } - let first = s.tiles().find(|(c, _)| c.tx == 0 && c.ty == 0).map(|(_, t)| t.clone()).unwrap(); - assert_eq!(Arc::strong_count(&first), 2); - let old = snapshot(&s); - assert_eq!(Arc::strong_count(&first), 2, "the snapshot adds no strong reference"); - assert!(damage(&old, &s).is_empty()); - s.fill_rect(Rect::new(0, 0, 4, 4), &[0.0, 0.0, 0.0, 1.0]); - let origin = s.tiles().map(|(c, _)| *c).find(|c| c.tx == 0 && c.ty == 0).unwrap(); - assert_eq!(damage(&old, &s), origin.rect(), "only the edited tile"); - drop(first); - assert!(old.values().any(|w| w.strong_count() == 0), "the replaced tile's pixels are freed"); - - let child = Layer::new("c", LayerContent::Raster(s)); - let n = child.surface().map_or(0, |s| s.tile_count()); - let group = Layer::group("g", vec![child]); - let mut pins = Vec::new(); - let key = group_key(&group, &GlobalLight::default(), &mut |t| pins.push(Arc::downgrade(t))); - assert_eq!(key, group_key(&group, &GlobalLight::default(), &mut |_| {}), "pinning doesn't change the key"); - assert_eq!(pins.len(), n); - drop(group); - assert!(pins.iter().all(|w| w.strong_count() == 0), "a group's pins keep none of its pixels"); - } -} +mod tests; diff --git a/crates/gpu/src/fx/builders.rs b/crates/gpu/src/fx/builders.rs new file mode 100644 index 0000000000..253ebe65ad --- /dev/null +++ b/crates/gpu/src/fx/builders.rs @@ -0,0 +1,209 @@ +use std::hash::{Hash, Hasher}; +use std::sync::Arc; + +use photocraft_compose::effects::{self as fx, BevelGeom, BevelPaint, FieldKind}; +use photocraft_doc::{Bevel, BevelTechnique, BevelTexture, GlobalLight, Glow, GlowSource, GlowTechnique, Pattern, Satin, Shadow, StrokeFx}; + +use super::{B, In, contour_lut, offset, stage}; +use crate::plan::{Kernel, stroke_widths}; + +/// A flag as the shaders read it: 1.0 or 0.0. +fn flag(v: bool) -> f32 { + f32::from(u8::from(v)) +} + +impl B { + /// Drop or Inner Shadow stages. Returns the number of maps written. + pub(super) fn shadow(&mut self, s: &Shadow, light: &GlobalLight, inner: bool) -> usize { + // shadow_map + let angle = if s.use_global_light { light.angle } else { s.angle }; + let (dx, dy) = offset(angle, s.distance); + let src = self.shift(In::Shape, dx, dy, if inner { 1.0 } else { 0.0 }, inner); + let m = self.spread_blur(src, (s.size, s.spread), inner, dx, dy); + self.finish(m, None, false, &s.contour, inner, 0); + 1 + } + + /// `src` spread or choked by `spread` of `size`, then blurred by the rest. + fn spread_blur(&mut self, src: In, (size, spread): (f32, f32), inner: bool, dx: f32, dy: f32) -> In { + let (r, bw) = fx::spread_split(size, spread); + if r <= 0.0 { + return self.blur(src, bw); + } + // The shifted map's distance field is the field shifted by the same whole pixels. + let f = self.field(if inner { FieldKind::ChokeInside } else { FieldKind::StrokeOutside }, r); + let d = if dx == 0.0 && dy == 0.0 { + f + } else { + self.push(stage(Kernel::MShift, Some(f), None, [dx, dy, if inner { -0.5 } else { 1e10 }, 0.0], dx.abs().max(dy.abs()) as i32)) + }; + let m = self.dilate(src, d, r); + self.blur(m, bw) + } + + /// Outer or Inner Glow stages, Precise or Softer. Returns the number of maps written. + pub(super) fn glow(&mut self, g: &Glow, inner: bool) -> usize { + // glow_map + let center = inner && g.source == GlowSource::Center; + let m = match g.technique { + GlowTechnique::Precise => { + let d = self.field(if inner { FieldKind::Inside } else { FieldKind::Outside }, g.size); + let solid = g.size * g.spread; + let soft = (g.size + 1.0 - solid).max(1e-3); + self.push(stage(Kernel::MGlow, Some(d), None, [solid, soft, 0.0, 0.0], 0)) + } + GlowTechnique::Softer => { + // Inner glows spread 1 - alpha. + let src = if inner { self.shift(In::Shape, 0.0, 0.0, 0.0, true) } else { In::Shape }; + let m = self.spread_blur(src, (g.size, g.spread), inner, 0.0, 0.0); + if inner { m } else { self.softer_glow_tail(m, g.size, g.range) } + } + }; + self.finish_glow(m, fx::glow_lut(g), inner, center); + 1 + } + + /// Satin stages. Returns the number of maps written. + pub(super) fn satin(&mut self, s: &Satin) -> usize { + // satin_map + let (dx, dy) = offset(s.angle, fx::satin_distance(s)); + let a = self.shift(In::Shape, dx, dy, 0.0, false); + let a = self.conv(a, fx::tent_kernel(s.size)); + let c = self.shift(In::Shape, -dx, -dy, 0.0, false); + let c = self.conv(c, fx::tent_kernel(s.size)); + self.finish_lut(a, Some(c), s.invert, fx::satin_lut(&s.contour), true, 0); + 1 + } + + /// Bevel and Emboss stages: the height map with its contour, texture and soften, then the shading. Returns the number of maps written. + pub(super) fn bevel(&mut self, bv: &Bevel, light: &GlobalLight, patterns: &[Pattern], anchor: (f64, f64)) -> usize { + // bevel_maps: a height map, then highlight and shadow shading. + let g = fx::bevel_geom(bv); + let mut h = self.bevel_height(bv, &g); + if let Some(c) = &bv.contour { + // Contour element: the height through `bevel_contour_lut`. + let mut st = stage(Kernel::MFinish, Some(h), None, [0.0, 0.0, 1.0, 0.0], 0); + st.lut = Some(Arc::new(fx::bevel_contour_lut(c))); + h = self.push(st); + } + if let Some(t) = &bv.texture + && let Some(pat) = photocraft_doc::pattern::find(patterns, &t.id, &t.name).filter(|p| p.is_paintable()) + { + let unit = if bv.depth.abs() > 1e-6 { (g.depth / bv.depth).abs().max(1e-3) } else { g.width.max(1.0) }; + h = self.bevel_texture(h, t, pat, unit, anchor); + } + if bv.soften >= 1.0 { + h = self.conv(h, fx::tent_kernel(bv.soften)); + } + self.bevel_shading(h, bv, &g, light) + } + + /// The bevel's height map: a tent blur of the shape for Smooth, distance ramps for the chisel techniques. + fn bevel_height(&mut self, bv: &Bevel, g: &BevelGeom) -> In { + if bv.technique == BevelTechnique::Smooth { + return self.conv(In::Shape, fx::tent_kernel(g.width)); + } + let paint = match g.paint { + BevelPaint::Outer => 0.0, + BevelPaint::Both => 1.0, + BevelPaint::Inner => 2.0, + }; + let size = bv.size.max(1.0); + let [din, dout] = self.bevel_fields(size); + let soft = g.chisel_soft > 0.0; + let h = self.push(stage(Kernel::MBevelH, Some(din), Some(dout), [paint, size, flag(soft), 0.0], 0)); + // Only emboss styles blur a Chisel Soft ramp, as in `effects::bevel_height`. + if soft && g.paint == BevelPaint::Both { self.conv(h, fx::tent_kernel(g.chisel_soft)) } else { h } + } + + /// `h` with the Texture element's pattern luminance added. + fn bevel_texture(&mut self, h: In, t: &BevelTexture, pat: &Pattern, unit: f32, anchor: (f64, f64)) -> In { + // Texture element: adds luminance × depth / unit to the height. + let pl = photocraft_compose::pattern::Placement::anchored(anchor, t.link, t.phase, t.scale, 0.0); + let (origin, cs, inv) = pl.parts(); + let mut st = stage(Kernel::MBevelTex, Some(h), None, [t.depth / unit, flag(t.invert), 0.0, 0.0], 0); + st.p3 = [origin.0 as f32, origin.1 as f32, cs.0 as f32, cs.1 as f32]; + st.p4 = [inv as f32, pat.width as f32, pat.height as f32, 0.0]; + st.pattern = Some(pat.clone()); + self.push(st) + } + + /// Highlight and shadow shading of height map `h` into maps 0 and 1. Returns 2. + fn bevel_shading(&mut self, h: In, bv: &Bevel, g: &BevelGeom, light: &GlobalLight) -> usize { + let (angle, altitude) = if bv.use_global_light { (light.angle, light.altitude) } else { (bv.angle, bv.altitude) }; + let (sa, ca) = angle.to_radians().sin_cos(); + let (se, ce) = altitude.to_radians().sin_cos(); + let lut = contour_lut(&bv.gloss_contour).map(Arc::new); + // Region for `fs_mbevelshade`: 0 inside, 1 outside, 2 emboss, 3 pillow emboss. + let region = match g.paint { + BevelPaint::Inner => 0.0, + BevelPaint::Outer => 1.0, + BevelPaint::Both if g.pillow => 3.0, + BevelPaint::Both => 2.0, + }; + let outer_chisel = g.paint == BevelPaint::Outer && bv.technique != BevelTechnique::Smooth; + for (out, which) in [0.0, 1.0].into_iter().enumerate() { + let mut s = stage(Kernel::MBevelShade, Some(h), None, [ca * ce, -sa * ce, se, se], 1); + s.p1 = [g.depth, region, which, flag(lut.is_some())]; + s.p3 = [flag(outer_chisel), 0.0, 0.0, 0.0]; + s.lut = lut.clone(); + s.s = Some(In::Shape); + s.out = Some(out); + self.stages.push(s); + } + 2 + } + + /// Stroke stages, the outside band into map 0 and the inside band into map 1. Returns 2. + pub(super) fn stroke(&mut self, st: &StrokeFx, vector_shape: bool) -> usize { + // Stroke bands: map 0 outside, map 1 inside. + let (in_w, out_w) = stroke_widths(st); + if out_w > 0.0 { + // Narrow strokes keep the fitted chamfer, wide ones the exact transform (#3646); + // the CPU picks the same field by the same width (`stroke_outside_exact`). + let kind = match (vector_shape, fx::stroke_outside_exact(out_w)) { + (true, true) => FieldKind::StrokeOutsideVector, + (true, false) => FieldKind::StrokeOutsideVectorChamfer, + (false, true) => FieldKind::StrokeOutsideExact, + (false, false) => FieldKind::StrokeOutside, + }; + self.stroke_band(kind, out_w, 0); + } + if in_w > 0.0 { + self.stroke_band(FieldKind::StrokeInside, in_w, 1); + } + 2 + } + + /// One stroke band of `width` from distance field `kind` into map `out`. + fn stroke_band(&mut self, kind: FieldKind, width: f32, out: usize) { + let d = self.field(kind, width); + let mut s = stage(Kernel::MStroke, Some(d), None, [width, 0.0, 0.0, 0.0], 0); + s.out = Some(out); + self.stages.push(s); + } + + /// A hash of the built stages and fields that keys the compiled program (never 0). + pub(super) fn key(&self, maps: usize) -> u64 { + let mut h = std::collections::hash_map::DefaultHasher::new(); + for s in &self.stages { + (format!("{:?}", s.kernel), s.a, s.b, s.s, s.radius, s.out).hash(&mut h); + for v in s.p0.iter().chain(&s.p1).chain(&s.p3).chain(&s.p4) { + v.to_bits().hash(&mut h); + } + if let Some(p) = &s.pattern { + (&p.id, &p.name, p.width, p.height).hash(&mut h); + for (c, t) in p.surface.tiles() { + (c.tx, c.ty, Arc::as_ptr(t) as usize).hash(&mut h); + } + } + if let Some(l) = &s.lut { + for v in l.iter() { + v.to_bits().hash(&mut h); + } + } + } + (maps, &self.fields).hash(&mut h); + h.finish() | 1 + } +} diff --git a/crates/gpu/src/fx/tests.rs b/crates/gpu/src/fx/tests.rs new file mode 100644 index 0000000000..32bdbae8cc --- /dev/null +++ b/crates/gpu/src/fx/tests.rs @@ -0,0 +1,340 @@ +use super::*; +use photocraft_color::BlendMode; +use photocraft_doc::{FxCommon, GlowSource, GlowTechnique}; + +/// Filled-shape identity survives integer moves, including fractional anchors and handles. +#[test] +fn filled_shape_key_preserves_translation_but_detects_deformation() { + use photocraft_doc::{Fill, Path, ShapeLayer, Subpath}; + let mut layer = Layer::new( + "shape", + LayerContent::Shape(ShapeLayer { + path: Path::new(vec![Subpath::polygon(&[(20.0, 20.0), (70.0, 20.0), (70.0, 60.0), (20.0, 60.0)])]), + fill: Some(Fill::Solid(photocraft_color::Color::WHITE)), + ..Default::default() + }), + ); + let canvas = Rect::new(0, 0, 200, 200); + let before = shape_key(&layer, canvas); + let LayerContent::Shape(shape) = &mut layer.content else { unreachable!() }; + shape.path = shape.path.transform(&photocraft_geom::Affine::translate(13.0, 7.0)); + assert_eq!(before, shape_key(&layer, canvas)); + let LayerContent::Shape(shape) = &mut layer.content else { unreachable!() }; + shape.path.subpaths[0].knots[1].anchor.x += 3.0; + assert_ne!(before, shape_key(&layer, canvas)); + + let LayerContent::Shape(shape) = &mut layer.content else { unreachable!() }; + shape.path = Path::new(vec![Subpath::polygon(&[(0.1, 0.2), (0.3, 0.2), (0.3, 1.7), (0.1, 1.7)])]); + shape.path.subpaths[0].knots[1].out_ctrl = photocraft_geom::Point::new(0.45, 0.65); + let original = shape.path.clone(); + let before = shape_key(&layer, canvas); + for (dx, dy) in [(1.0, 1.0), (-13.0, 7.0), (100_000.0, -100_000.0)] { + let LayerContent::Shape(shape) = &mut layer.content else { unreachable!() }; + shape.path = original.transform(&photocraft_geom::Affine::translate(dx, dy)); + assert_eq!(before, shape_key(&layer, canvas), "fractional path translated by {dx}, {dy}"); + } + let LayerContent::Shape(shape) = &mut layer.content else { unreachable!() }; + shape.path.subpaths[0].knots[1].out_ctrl.x += 0.01; + assert_ne!(before, shape_key(&layer, canvas), "a handle deformation invalidates the key"); +} + +#[test] +fn programs_key_on_geometry_not_colour() { + let light = GlobalLight::default(); + let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; + let a = program(&Effect::DropShadow(s.clone()), &light, false); + s.color = photocraft_color::Color::rgb(1.0, 0.0, 0.0); + s.common = FxCommon::new(BlendMode::Screen, 0.3); + let b = program(&Effect::DropShadow(s.clone()), &light, false); + assert_eq!(a.key, b.key); + s.size += 1.0; + assert_ne!(a.key, program(&Effect::DropShadow(s), &light, false).key); +} + +#[test] +fn softer_outer_glow_program_keeps_the_tent_and_adds_a_distance_tail() { + let effect = Effect::OuterGlow(photocraft_doc::Glow { + common: FxCommon::new(BlendMode::Screen, 1.0), + paint: photocraft_doc::FxPaint::Color(photocraft_color::Color::WHITE), + technique: GlowTechnique::Softer, + spread: 0.0, + size: 60.0, + contour: Contour::Linear, + anti_alias: false, + range: 0.5, + jitter: 0.0, + noise: 0.0, + source: GlowSource::Edge, + }); + let kernel = blur_kernel(60.0).unwrap(); + let p = program(&effect, &GlobalLight::default(), false); + let blur: Vec<_> = p.stages.iter().filter(|s| s.kernel == Kernel::MBlur).collect(); + assert_eq!(blur.len(), 2); + for stage in blur { + assert_eq!(stage.radius, kernel.0); + assert_eq!(stage.lut.as_deref().unwrap(), kernel.1.as_slice()); + } + let tail = p.stages.iter().find(|s| s.kernel == Kernel::MGlow).unwrap(); + assert_eq!(tail.a, Some(In::Field(FieldKind::Outside))); + assert_eq!(tail.p0, [54.0, 6.0, 0.0025, 1.0]); + assert!(p.stages.iter().any(|s| s.kernel == Kernel::MFinish && s.p0[0] == 2.0)); + assert!(p.fields.contains(&(FieldKind::Outside, reach_for(66.0)))); +} + +/// A styled group's effect shape follows its own vector mask and its children's Blend If and +/// Advanced Blending, so the cached shape must be rebuilt when any of them changes. +#[test] +fn group_key_follows_vector_mask_blend_if_and_advanced() { + use photocraft_doc::{BlendRange, Knockout, Path, PixelFormat, Subpath, VectorMask}; + let light = GlobalLight::default(); + let group = Layer::group("g", vec![Layer::raster("child", PixelFormat::RGBA8)]); + let base = group_key(&group, &light, &mut |_| {}); + + let mask = |x: f64| VectorMask::new(Path::new(vec![Subpath::polygon(&[(x, 0.0), (x + 10.0, 0.0), (x + 10.0, 10.0)])])); + let mut masked = group.clone(); + masked.vector_mask = Some(mask(0.0)); + let with_mask = group_key(&masked, &light, &mut |_| {}); + assert_ne!(base, with_mask, "adding the group's vector mask"); + masked.vector_mask = Some(mask(5.0)); + assert_ne!(with_mask, group_key(&masked, &light, &mut |_| {}), "moving the group's vector mask"); + + fn child_mut(g: &mut Layer) -> &mut Layer { + let LayerContent::Group(gr) = &mut g.content else { unreachable!() }; + &mut gr.children[0] + } + let mut blend_if = group.clone(); + child_mut(&mut blend_if).blend_if.ranges = vec![[BlendRange { black: [128, 128], white: [255, 255] }, BlendRange::FULL]]; + assert_ne!(base, group_key(&blend_if, &light, &mut |_| {}), "a child's Blend If"); + + let mut advanced = group.clone(); + child_mut(&mut advanced).advanced.knockout = Knockout::Deep; + assert_ne!(base, group_key(&advanced, &light, &mut |_| {}), "a child's Advanced Blending"); +} + +#[test] +fn windows_grow_by_reach_and_temps_are_reused() { + let light = GlobalLight::default(); + let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; + s.spread = 0.5; + let p = program(&Effect::DropShadow(s), &light, false); + let region = Rect::new(-100, -100, 1000, 1000); + let d = Rect::new(0, 0, 10, 10); + let fr = |_| 5; + let w = p.windows(d, region, &fr); + let last = *w.last().unwrap(); + assert!(last.contains_rect(&d.inflate(7)), "{last:?}"); + // Earlier stages need at least as much as later ones read. + assert!(w[0].width() >= last.width()); + let (t, n) = p.temps(); + assert!(n <= 3, "{n} temporaries for {} stages", p.stages.len()); + assert!(t.last().unwrap().is_none()); +} + +/// A distance field computed in bands from grown windows equals the whole-region field +/// wherever it is within its reach (the property incremental updates rely on). +#[test] +fn banded_fields_match_whole_region_within_reach() { + let region = Rect::new(-20, -10, 300, 290); + let (w, h) = (region.width() as usize, region.height() as usize); + let mut shape = vec![0.0f32; w * h]; + for y in 0..h { + for x in 0..w { + let (fx, fy) = (x as f32 - 150.0, y as f32 - 140.0); + let r = (fx * fx + fy * fy).sqrt() + 8.0 * (fy * 0.07).sin() * (fx * 0.05).cos(); + shape[y * w + x] = (90.0 - r).clamp(0.0, 1.0) * if (x / 13 + y / 17) % 5 == 0 { 0.6 } else { 1.0 }; + } + } + for kind in [ + FieldKind::Outside, + FieldKind::Inside, + FieldKind::BevelOutside, + FieldKind::BevelInside, + FieldKind::ChokeInside, + FieldKind::StrokeOutside, + FieldKind::StrokeInside, + FieldKind::StrokeOutsideExact, + FieldKind::StrokeOutsideVector, + FieldKind::StrokeOutsideVectorChamfer, + ] { + let reach = 9; + let whole = photocraft_compose::effects::distance_field(kind, shape.clone(), w, h, reach as f32); + let banded = field(kind, reach, &shape, region, region); + let part = Rect::new(40, 30, 170, 120); + let partial = field(kind, reach, &shape, region, part); + for (i, (a, b)) in whole.iter().zip(&banded).enumerate() { + if a.min(*b) < reach as f32 { + assert!((a - b).abs() < 1e-5, "{kind:?} at {i}: {a} vs {b}"); + } + } + for (i, b) in partial.iter().enumerate() { + let (x, y) = (part.x0 + (i % part.width() as usize) as i32, part.y0 + (i / part.width() as usize) as i32); + let a = whole[(y - region.y0) as usize * w + (x - region.x0) as usize]; + if a.min(*b) < reach as f32 { + assert!((a - b).abs() < 1e-5, "{kind:?} at ({x},{y}): {a} vs {b}"); + } + } + } +} +#[test] +fn windowed_bevel_fields_match_whole_region_on_anti_aliased_edges() { + // The bevel fields read the edge pixels around the foot of the perpendicular and take the + // direction of each from its neighbours, so the halo of a part of the region has to cover all of + // that. A wide bar with clean anti-aliased edges (almost along the 128 row bands the other + // fields use, across them and at an angle) puts sharp edge pixels where bands would meet and + // inside the partial window. The whole region is one window, so it is exactly the CPU's field. + let region = Rect::new(-20, -10, 300, 290); + let (w, h) = (region.width() as usize, region.height() as usize); + let part = Rect::new(40, 30, 170, 120); + let mut too_small_noticed = false; + for deg in [1.5f32, 5.0, 30.0, 88.5] { + let (sn, cs) = deg.to_radians().sin_cos(); + let inside = |x: f32, y: f32| { + let (u, v) = ((x - 150.0) * cs + (y - 128.3) * sn, -(x - 150.0) * sn + (y - 128.3) * cs); + u.abs() <= 140.0 && v.abs() <= 60.0 + }; + let mut shape = vec![0.0f32; w * h]; + for y in 0..h { + for x in 0..w { + let hits = (0..64).filter(|k| inside(x as f32 + ((k % 8) as f32 + 0.5) / 8.0, y as f32 + ((k / 8) as f32 + 0.5) / 8.0)).count(); + shape[y * w + x] = hits as f32 / 64.0; + } + } + for reach in [9, 24] { + for kind in [FieldKind::BevelInside, FieldKind::BevelOutside] { + let whole = photocraft_compose::effects::distance_field(kind, shape.clone(), w, h, reach as f32); + let differs = |part_field: &[f32], out: Rect| { + let ow = out.width() as usize; + part_field + .iter() + .enumerate() + .filter(|&(i, b)| { + let (x, y) = (out.x0 + (i % ow) as i32, out.y0 + (i / ow) as i32); + let a = whole[(y - region.y0) as usize * w + (x - region.x0) as usize]; + a.min(*b) < reach as f32 && (a - b).abs() > 1e-5 + }) + .count() + }; + let all = field(kind, reach, &shape, region, region); + assert!( + all.iter().zip(&whole).all(|(a, b)| a.to_bits() == b.to_bits()), + "{kind:?}, {deg} deg, reach {reach}: the whole region is not the CPU's field" + ); + assert_eq!( + differs(&field(kind, reach, &shape, region, part), part), + 0, + "{kind:?}, {deg} deg, reach {reach}: partial window differs from whole" + ); + // The test can see a halo that is too small: two rows past the part is not enough. + too_small_noticed |= differs(&field_banded(kind, reach, &shape, region, part, 2), part) > 0; + } + } + } + assert!(too_small_noticed, "a 2 row halo went unnoticed, so this test guards nothing"); +} + +/// Both bevel fields from one window (sharp edges found once) are bit for bit the fields +/// computed one at a time, over the whole region and over a part of it. +#[test] +fn paired_bevel_fields_equal_the_fields_one_at_a_time() { + let region = Rect::new(-20, -10, 300, 290); + let (w, h) = (region.width() as usize, region.height() as usize); + let mut shape = vec![0.0f32; w * h]; + for y in 0..h { + for x in 0..w { + let (fx, fy) = (x as f32 - 150.0, y as f32 - 140.0); + let r = (fx * fx + fy * fy).sqrt() + 8.0 * (fy * 0.07).sin() * (fx * 0.05).cos(); + shape[y * w + x] = (90.0 - r).clamp(0.0, 1.0) * if (x / 13 + y / 17) % 5 == 0 { 0.6 } else { 1.0 }; + } + } + let bits = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::>(); + for reach in [3, 9, 40] { + for out in [region, Rect::new(40, 30, 170, 120), Rect::new(-20, 200, 300, 290)] { + let [inside, outside] = bevel_fields(reach, &shape, region, out); + assert_eq!(bits(&inside), bits(&field(FieldKind::BevelInside, reach, &shape, region, out)), "inside, reach {reach}, {out:?}"); + assert_eq!(bits(&outside), bits(&field(FieldKind::BevelOutside, reach, &shape, region, out)), "outside, reach {reach}, {out:?}"); + // The banded path (wasm) pairs them per band the same way. + let halo = field_radius(FieldKind::BevelInside, reach) + 1; + let banded = + fields_in_windows(false, &shape, region, out, halo, |s, w, h| photocraft_compose::effects::bevel_distance_fields(s, w, h, reach as f32)); + for (k, kind) in [FieldKind::BevelInside, FieldKind::BevelOutside].into_iter().enumerate() { + let [one] = + fields_in_windows(false, &shape, region, out, halo, |s, w, h| [photocraft_compose::effects::distance_field(kind, s, w, h, reach as f32)]); + assert_eq!(bits(&banded[k]), bits(&one), "banded {kind:?}, reach {reach}, {out:?}"); + } + } + } +} + +/// #3646: a stroke's outer edge is the fitted 5 × 5 chamfer while it is narrow and the exact +/// Euclidean transform once it is wide — the chamfer's octagon facets a wide stroke. The GPU +/// builds it from the field its width picks (`stroke_outside_exact`), while the spread of drop +/// shadows and softer outer glows keeps the chamfer field (fitted to Photoshop, #224) — so +/// routing a wide stroke through the chamfer, or a shadow through the exact field, fails here. +#[test] +fn a_stroke_uses_the_chamfer_narrow_and_the_exact_field_wide() { + use photocraft_color::{BlendMode, Color}; + use photocraft_doc::{FxCommon, FxPaint, StrokeFx, StrokePosition}; + let light = GlobalLight::default(); + let stroke = |size, position| { + Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, 1.0), size, position, paint: FxPaint::Color(Color::BLACK), overprint: false }) + }; + // Narrow (< STROKE_CHAMFER_MAX): the chamfer, exactly as before #3646. + let narrow = program(&stroke(6.0, StrokePosition::Outside), &light, false); + assert!(narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "a narrow raster outside stroke keeps the chamfer: {:?}", narrow.fields); + assert!(!narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideExact), "…not the exact field: {:?}", narrow.fields); + // Wide: the exact transform. + let wide = program(&stroke(40.0, StrokePosition::Outside), &light, false); + assert!( + wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideExact), + "a wide raster outside stroke needs the exact outer field: {:?}", + wide.fields + ); + assert!(!wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "…not the chamfer field: {:?}", wide.fields); + // A shape layer's outline stroke follows the same width rule; the inside edge keeps the chamfer. + let vector_narrow = program(&stroke(6.0, StrokePosition::Outside), &light, true); + assert!(vector_narrow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideVectorChamfer), "{:?}", vector_narrow.fields); + let vector_wide = program(&stroke(40.0, StrokePosition::Outside), &light, true); + assert!(vector_wide.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutsideVector), "{:?}", vector_wide.fields); + let inner = program(&stroke(40.0, StrokePosition::Inside), &light, false); + assert!(inner.fields.iter().any(|(k, _)| *k == FieldKind::StrokeInside), "{:?}", inner.fields); + // The shadow / softer-glow spread is unchanged: still the chamfer. + let Effect::DropShadow(mut s) = Effect::default_drop_shadow() else { unreachable!() }; + s.spread = 0.5; + let shadow = program(&Effect::DropShadow(s), &light, false); + assert!(shadow.fields.iter().any(|(k, _)| *k == FieldKind::StrokeOutside), "the shadow spread keeps the chamfer: {:?}", shadow.fields); +} + +/// Cached shapes and group keys reserve tile addresses without keeping pixels (#4530): +/// damage still tells a replaced tile from an unchanged one, and a dropped tile's pixels +/// are freed although the entry remembers it. +#[test] +fn shape_tiles_and_group_pins_hold_addresses_not_pixels() { + use photocraft_color::{ColorMode, PixelFormat, SampleType}; + let fmt = PixelFormat::new(ColorMode::Rgb, SampleType::U8, true); + let mut s = Surface::new(fmt); + // A ramp, so no two tiles share their pixels. + for x in 0..600 { + let v = x as f32 / 600.0; + s.fill_rect(Rect::new(x, 0, x + 1, 300), &[v, 0.2, 0.9, 1.0]); + } + let first = s.tiles().find(|(c, _)| c.tx == 0 && c.ty == 0).map(|(_, t)| t.clone()).unwrap(); + assert_eq!(Arc::strong_count(&first), 2); + let old = snapshot(&s); + assert_eq!(Arc::strong_count(&first), 2, "the snapshot adds no strong reference"); + assert!(damage(&old, &s).is_empty()); + s.fill_rect(Rect::new(0, 0, 4, 4), &[0.0, 0.0, 0.0, 1.0]); + let origin = s.tiles().map(|(c, _)| *c).find(|c| c.tx == 0 && c.ty == 0).unwrap(); + assert_eq!(damage(&old, &s), origin.rect(), "only the edited tile"); + drop(first); + assert!(old.values().any(|w| w.strong_count() == 0), "the replaced tile's pixels are freed"); + + let child = Layer::new("c", LayerContent::Raster(s)); + let n = child.surface().map_or(0, |s| s.tile_count()); + let group = Layer::group("g", vec![child]); + let mut pins = Vec::new(); + let key = group_key(&group, &GlobalLight::default(), &mut |t| pins.push(Arc::downgrade(t))); + assert_eq!(key, group_key(&group, &GlobalLight::default(), &mut |_| {}), "pinning doesn't change the key"); + assert_eq!(pins.len(), n); + drop(group); + assert!(pins.iter().all(|w| w.strong_count() == 0), "a group's pins keep none of its pixels"); +} diff --git a/crates/gpu/src/fx_passes.rs b/crates/gpu/src/fx_passes.rs new file mode 100644 index 0000000000..f3f5ce6da3 --- /dev/null +++ b/crates/gpu/src/fx_passes.rs @@ -0,0 +1,268 @@ +use super::batch::op_words; +use super::consts::{MAP16, MAP32}; +use super::kit::MapDraw; +use super::paging::field_reaches; +use super::textures::{FxEntry, FxKey, ProgState, Stats, Tex}; +use super::{Arc, Compositor, Document, FieldKind, HashMap, LayerContent, Rect, Unsupported, fx, plan}; + +impl Compositor { + /// Build or update the effect maps of one layer over `region`: its whole effect region, or + /// the window of one page cell (`key.1`) grown by the effects' reach. + #[allow(clippy::too_many_arguments)] + pub(crate) fn sync_fx( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + doc: &Document, + f: &plan::FxLayer<'_>, + key: FxKey, + region: Rect, + stats: &mut Stats, + ) -> Result<(), Unsupported> { + let layer = f.layer; + let canvas = doc.bounds(); + let is_group = matches!(layer.content, LayerContent::Group(_)); + let shape_key = if is_group { fx::group_key(layer, &doc.global_light, &mut |_| {}) } else { fx::shape_key(layer, canvas) }; + let (content_src, mask_src) = if is_group { (None, None) } else { fx::shape_sources(layer) }; + let frame = self.frame; + let (rw, rh) = (region.width(), region.height()); + + // What changed: everything (new settings / size), the position only, some tiles, or + // nothing. + let mut damage = Rect::EMPTY; + let prev = self.fx.get(&key).filter(|e| e.shape_key == shape_key).map(|e| e.region); + let mut rebuild = true; + if prev == Some(region) + && let Some(e) = self.fx.get(&key) + { + rebuild = false; + for (old, cur) in e.tiles.iter().zip([content_src, mask_src]) { + let d = match (old, cur) { + (Some(o), Some(s)) => fx::damage(o, s), + (None, None) => Rect::EMPTY, + // A source appeared or vanished (the key changes with it too). + _ => region, + }; + damage = if damage.is_empty() { d } else { damage.union(&d) }; + } + damage = damage.intersect(®ion); + } else if let Some(old) = prev + && (old.width(), old.height()) == (rw, rh) + && !region.is_empty() + { + // Moved by whole pixels: maps are computed relative to the region, so if the shape + // moved unchanged with it, every map is still exact. + let v = fx::shape(doc, layer, region); + if let Some(e) = self.fx.get_mut(&key) + && v == e.shape_cpu + { + e.region = region; + rebuild = false; + } + } + if rebuild { + let shape = Tex::map(device, "pc_fx_shape", region, MAP32); + let n = rw as usize * rh as usize; + self.fx.insert( + key, + FxEntry { + doc: doc.id, + region, + shape_key, + tiles: [None, None], + _pin: if is_group { + let mut pin = Vec::new(); + fx::group_key(layer, &doc.global_light, &mut |t| pin.push(Arc::downgrade(t))); + pin + } else { + Vec::new() + }, + shape_cpu: vec![0.0; n], + shape, + fields: HashMap::new(), + progs: Vec::new(), + last_used: frame, + stamp: 0, + }, + ); + damage = region; + } + // Present: kept or (re)inserted above. + let stamp = self.stamp; + let Some(e) = self.fx.get_mut(&key) else { return Ok(()) }; + e.last_used = frame; + e.stamp = stamp; + e.doc = doc.id; + e.tiles = [content_src.map(fx::snapshot), mask_src.map(fx::snapshot)]; + if region.is_empty() { + return Ok(()); + } + + let t_trace = web_time_now(); + let mut staged = 0u64; + // Shape over the damage (compose's own alpha). + if !damage.is_empty() { + stats.fx_shapes += 1; + let v = fx::shape(doc, layer, damage); + fx::paste(&mut e.shape_cpu, region, damage, &v); + e.shape.write_r32(queue, region, damage, &v); + staged += v.len() as u64 * 4; + } + + // Programs, and the distance fields they read (max reach per field). + // Filled shapes stroke their outline (the effect shape); others estimate it. + let vector_shape = matches!(layer.content, LayerContent::Shape(_)) && photocraft_compose::effect_outline(layer).is_none(); + let anchor = layer.effects.reference.unwrap_or((f64::from(f.bounds.x0), f64::from(f.bounds.y0))); + let progs: Vec = + layer.effects.items.iter().filter(|e| e.enabled()).map(|e| fx::program_with(e, &doc.global_light, vector_shape, &doc.patterns, anchor)).collect(); + let want = field_reaches(&progs); + e.fields.retain(|k, _| want.contains_key(k)); + // Each field to recompute: over which rectangle, exact up to which reach. + let jobs: Vec<(FieldKind, Rect, i32)> = want + .iter() + .filter_map(|(&kind, &reach)| match e.fields.get(&kind).map(|(r, _)| *r) { + Some(r) if r >= reach => (!damage.is_empty()).then(|| (kind, damage.inflate(fx::field_radius(kind, r)).intersect(®ion), r)), + _ => Some((kind, region, reach)), + }) + .collect(); + // A chiselled bevel's two fields are asked for and kept together (`fx::bevel_fields`), so they + // are recomputed together, over one rectangle at one reach. + let job = |k: FieldKind| jobs.iter().find(|j| j.0 == k).map(|j| (j.1, j.2)); + let (bevel_in, bevel_out) = (job(FieldKind::BevelInside), job(FieldKind::BevelOutside)); + if bevel_in != bevel_out { + return Err(Unsupported(format!("bevel fields of `{}` out of step: inside {bevel_in:?}, outside {bevel_out:?}", layer.name))); + } + for (kind, out, reach) in jobs { + let fields = match kind { + FieldKind::BevelInside => { + let [inside, outside] = fx::bevel_fields(reach, &e.shape_cpu, region, out); + vec![(FieldKind::BevelInside, inside), (FieldKind::BevelOutside, outside)] + } + // Computed with BevelInside, over the same rectangle at the same reach (checked above). + FieldKind::BevelOutside => continue, + _ => vec![(kind, fx::field(kind, reach, &e.shape_cpu, region, out))], + }; + for (kind, v) in fields { + if e.fields.get(&kind).is_none_or(|(r, _)| *r < reach) { + e.fields.insert(kind, (reach, Tex::map(device, "pc_fx_field", region, MAP32))); + } + if let Some((_, t)) = e.fields.get(&kind) { + t.write_r32(queue, region, out, &v); + } + staged += v.len() as u64 * 4; + stats.fx_pixels += out.width() as u64 * out.height() as u64; + } + } + + if let Some(t) = t_trace.filter(|_| !damage.is_empty()) { + eprintln!("effect maps of `{}`: shape + distance fields over {damage:?} in {:.2} ms (CPU)", layer.name, t.elapsed().as_secs_f64() * 1000.0); + } + e.progs.truncate(progs.len()); + while e.progs.len() < progs.len() { + e.progs.push(ProgState { key: 0, maps: Vec::new() }); + } + self.staged += staged; + for (i, prog) in progs.iter().enumerate() { + if prog.maps == 0 { + continue; + } + let Some(e) = self.fx.get_mut(&key) else { return Ok(()) }; + let same = e.progs[i].key == prog.key && e.progs[i].maps.len() == prog.maps; + let d = if same { damage } else { region }; + if d.is_empty() { + continue; + } + if !same { + e.progs[i] = ProgState { + key: prog.key, + maps: (0..prog.maps).map(|k| prog.stages.iter().any(|s| s.out == Some(k)).then(|| Tex::map(device, "pc_fx_map", region, MAP16))).collect(), + }; + } + stats.fx_programs += 1; + self.run_program(device, queue, encoder, key, i, prog, d, stats); + } + Ok(()) + } + + /// Record the passes of `prog` (maps of item `item` of `layer`) recomputing damage `d`. + #[allow(clippy::too_many_arguments)] + fn run_program( + &mut self, + device: &wgpu::Device, + queue: &wgpu::Queue, + encoder: &mut wgpu::CommandEncoder, + key: FxKey, + item: usize, + prog: &fx::MapProgram, + d: Rect, + stats: &mut Stats, + ) { + let pattern_views: Vec> = + prog.stages.iter().map(|s| s.pattern.as_ref().map(|p| self.pattern_view(device, queue, p))).collect(); + let Some(e) = self.fx.get(&key) else { return }; + let region = e.region; + let shape = e.shape.view.clone(); + let finals: Vec> = e.progs[item].maps.iter().map(|m| m.as_ref().map(|t| t.view.clone())).collect(); + let fields: HashMap = e.fields.iter().map(|(k, (r, t))| (*k, (*r, t.view.clone()))).collect(); + let windows = prog.windows(d, region, &|k| fields.get(&k).map_or(0, |f| f.0)); + let (assign, count) = prog.temps(); + let mut temps: Vec = Vec::with_capacity(count); + for _ in 0..count { + let v = self.temp(device, region, &temps); + temps.push(v); + } + // `None` only if a program's map or temporary wasn't allocated, which `sync_fx` and + // `MapProgram::temps` rule out. + let target_of = |i: usize| -> Option<(wgpu::TextureView, wgpu::TextureFormat)> { + match prog.stages[i].out { + Some(k) => Some((finals.get(k)?.clone()?, MAP16)), + None => Some((temps.get((*assign.get(i)?)?)?.clone(), MAP32)), + } + }; + let view_of = |inp: Option| -> Option { + match inp? { + fx::In::Shape => Some(shape.clone()), + fx::In::Field(k) => fields.get(&k).map(|f| f.1.clone()), + fx::In::Val(k) => target_of(k).map(|t| t.0), + } + }; + let mut draws = Vec::with_capacity(prog.stages.len()); + for (i, s) in prog.stages.iter().enumerate() { + let w = windows[i]; + if w.is_empty() { + continue; + } + stats.fx_pixels += w.width() as u64 * w.height() as u64; + let Some((target, format)) = target_of(i) else { continue }; + let mut p = plan::Pass::new(s.kernel, 0); + p.params[0] = s.p0; + p.params[1] = s.p1; + p.params[3] = s.p3; + p.extra = s.p4; + draws.push(MapDraw { + kernel: s.kernel, + format, + target, + scissor: [(w.x0 - region.x0) as u32, (w.y0 - region.y0) as u32, w.width(), w.height()], + space: [region.x0, region.y0, region.width() as i32, region.height() as i32], + op: op_words(&p, None, None, None), + views: [view_of(s.a), view_of(s.b), view_of(s.s), None, None, None, None, pattern_views[i].clone()], + lut: s.lut.clone(), + }); + } + self.kit.run(device, queue, encoder, &draws); + } +} + +/// A start time when `PHOTOCRAFT_FX_TRACE=1` (it prints the CPU time of effect shapes and +/// distance fields); never on wasm. +fn web_time_now() -> Option { + #[cfg(not(target_arch = "wasm32"))] + { + static T: std::sync::OnceLock = std::sync::OnceLock::new(); + T.get_or_init(|| std::env::var_os("PHOTOCRAFT_FX_TRACE").is_some()).then(std::time::Instant::now) + } + #[cfg(target_arch = "wasm32")] + None +} diff --git a/crates/gpu/src/health.rs b/crates/gpu/src/health.rs index cb1a116c4b..5c07fbc180 100644 --- a/crates/gpu/src/health.rs +++ b/crates/gpu/src/health.rs @@ -30,6 +30,14 @@ impl Fault { matches!(self, Fault::Lost(_)) } + /// An out-of-memory error on a device that still works: freeing memory and trying again can + /// succeed, unlike after a loss or a validation error (#6564). Only wgpu's own out-of-memory + /// error is recorded with exactly [`OUT_OF_MEMORY`]; a validation error that merely mentions + /// memory is not one. + pub fn is_out_of_memory(&self) -> bool { + matches!(self, Fault::Error(s) if s == OUT_OF_MEMORY) + } + /// The driver's or wgpu's message. pub fn detail(&self) -> &str { match self { @@ -48,11 +56,16 @@ impl fmt::Display for Fault { } } +/// The message an out-of-memory [`Fault::Error`] carries (wgpu's `Error::OutOfMemory`). +pub const OUT_OF_MEMORY: &str = "Out of Memory"; + #[derive(Default)] struct Inner { faulted: AtomicBool, fault: Mutex>, errors: AtomicU64, + /// wgpu itself reported the device lost (see [`DeviceHealth::device_lost`]). + device_lost: AtomicBool, } /// Shared health flag of one wgpu device. Cheap to clone; every clone sees the same state. @@ -84,6 +97,7 @@ impl DeviceHealth { (_, false) => message, }; log::error!("wgpu device lost ({reason:?}): {detail}"); + lost.0.device_lost.store(true, Ordering::Release); lost.mark(Fault::Lost(detail)); }); let errors = health.clone(); @@ -96,19 +110,50 @@ impl DeviceHealth { self.0.errors.fetch_add(1, Ordering::Relaxed); let text = e.to_string(); log::error!("uncaptured wgpu error: {text}"); - // Errors about invalid or lost objects follow a device loss whose callback may not have - // run yet (it fires on the next poll). - let lower = text.to_ascii_lowercase(); - let fault = if lower.contains("lost") || lower.contains("destroyed") { Fault::Lost(first_line(&text)) } else { Fault::Error(first_line(&text)) }; - self.mark(fault); + self.mark(fault_of(e)); } - /// Mark the device unusable. The first fault is kept; a later device loss upgrades an error. + /// Record an error an error scope caught. + /// + /// [`DeviceHealth::watch`]'s uncaptured-error handler never sees a scoped error, so without + /// this a watched device would keep being handed to a GPU that has already failed. Polls + /// first: a lost device is a [`Fault::Lost`], and its callback may not have run yet (it fires + /// on a poll, and `Device::poll` panics on a lost device). + /// + /// The poll is a single non-blocking check, never a fence wait. This runs on the UI thread + /// immediately after a failed frame, so `PollType::Wait` with no timeout would stall the app + /// until every in-flight submission drained (or forever, if the device is wedged). A + /// non-blocking poll loses nothing here: wgpu still reads the fence and fires the + /// device-lost callback for a lost device, it just never blocks waiting for one. + /// + /// A device that only recorded an error is still polled and marked, so a loss or an + /// out-of-memory error can upgrade that fault as [`DeviceHealth::mark`] allows (#6564). + pub fn caught(&self, device: &wgpu::Device, error: &wgpu::Error) { + if self.lost() { + return; + } + #[cfg(not(target_arch = "wasm32"))] + self.poll(device); + #[cfg(target_arch = "wasm32")] + let _ = device; + if self.lost() { + return; + } + self.mark(fault_of(error)); + } + + fn lost(&self) -> bool { + self.fault().is_some_and(|f| f.is_lost()) + } + + /// Mark the device unusable. The first fault is kept; a later device loss upgrades an error, + /// and a later out-of-memory error upgrades another one, so the canvas can still recover by + /// itself when memory ran out after a validation error in the same frame (#6564). pub fn mark(&self, fault: Fault) { let mut slot = self.0.fault.lock().unwrap_or_else(PoisonError::into_inner); let replace = match slot.as_ref() { None => true, - Some(old) => !old.is_lost() && fault.is_lost(), + Some(old) => !old.is_lost() && (fault.is_lost() || (fault.is_out_of_memory() && !old.is_out_of_memory())), }; if replace { *slot = Some(fault); @@ -134,22 +179,61 @@ impl DeviceHealth { self.0.errors.load(Ordering::Relaxed) } + /// Whether wgpu itself reported the device lost: its device-lost callback ran, or polling it + /// panicked. Unlike a fault recorded with [`mark`](Self::mark) (a simulated loss, or an + /// uncaptured error that names one), nothing drawn with the device reaches the screen any + /// more, the window's own frames included (#3438). + pub fn device_lost(&self) -> bool { + self.0.device_lost.load(Ordering::Acquire) + } + + /// Non-blocking poll that runs even after a fault was recorded, so a device that is lost + /// later (a driver reset while the app sat idle or the PC slept) is noticed before the next + /// frame is drawn with it. Skipped once [`device_lost`](Self::device_lost). + pub fn probe(&self, device: &wgpu::Device) { + if self.device_lost() { + return; + } + if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| device.poll(wgpu::PollType::Poll))).is_err() { + self.lost_by_poll(); + } + } + + /// `Device::poll` panicked: wgpu found the device lost. + fn lost_by_poll(&self) { + self.0.device_lost.store(true, Ordering::Release); + self.mark(Fault::Lost("the device stopped responding".into())); + } + /// Block until `index` (or all submitted work) finished, unless the device is unusable. /// Returns whether the device is still healthy afterwards. `Device::poll` panics on a lost /// device, so a panic is caught and recorded as a loss (native only; the web has no blocking /// wait). #[cfg(not(target_arch = "wasm32"))] pub fn wait(&self, device: &wgpu::Device, index: Option) -> bool { + self.wait_idle(device, index); + self.is_ok() + } + + /// Like [`Self::wait`], but returns whether the wait completed: `false` also when the poll + /// itself failed, so the work may still be running. + #[cfg(not(target_arch = "wasm32"))] + pub fn wait_idle(&self, device: &wgpu::Device, index: Option) -> bool { if !self.is_ok() { return false; } let poll = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| device.poll(wgpu::PollType::Wait { submission_index: index, timeout: None }))); match poll { - Ok(Ok(_)) => {} - Ok(Err(e)) => log::warn!("wgpu poll: {e}"), - Err(_) => self.mark(Fault::Lost("the device stopped responding".into())), + Ok(Ok(_)) => self.is_ok(), + Ok(Err(e)) => { + log::warn!("wgpu poll: {e}"); + false + } + Err(_) => { + self.lost_by_poll(); + false + } } - self.is_ok() } /// Non-blocking poll (reclaims staging memory), skipped once the device is unusable. @@ -158,11 +242,26 @@ impl DeviceHealth { return; } if std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| device.poll(wgpu::PollType::Poll))).is_err() { - self.mark(Fault::Lost("the device stopped responding".into())); + self.lost_by_poll(); } } } +/// The fault a wgpu error stands for. Errors about invalid or lost objects follow a device loss +/// whose callback may not have run yet (it fires on the next poll), so they count as a loss +/// rather than as an error. Only wgpu's own out-of-memory error is [`OUT_OF_MEMORY`]. +fn fault_of(e: &wgpu::Error) -> Fault { + let text = e.to_string(); + let lower = text.to_ascii_lowercase(); + if matches!(e, wgpu::Error::OutOfMemory { .. }) { + Fault::Error(OUT_OF_MEMORY.into()) + } else if lower.contains("lost") || lower.contains("destroyed") { + Fault::Lost(first_line(&text)) + } else { + Fault::Error(first_line(&text)) + } +} + fn first_line(s: &str) -> String { let line = s.lines().map(str::trim).find(|l| !l.is_empty()).unwrap_or(""); let mut out: String = line.chars().take(200).collect(); @@ -190,6 +289,8 @@ mod tests { assert_eq!(h.fault(), Some(Fault::Lost("reset".into()))); c.mark(Fault::Lost("later".into())); assert_eq!(h.fault().map(|f| f.to_string()).as_deref(), Some("GPU device lost: reset")); + // A recorded loss is not wgpu reporting the device lost (#3438). + assert!(!h.device_lost()); } #[test] @@ -198,5 +299,17 @@ mod tests { assert_eq!(Fault::Error("x".into()).to_string(), "GPU error: x"); assert_eq!(first_line("\n Buffer 'pc' is invalid\nmore"), "Buffer 'pc' is invalid"); assert_eq!(first_line(&"a".repeat(300)).chars().count(), 201); + assert!(Fault::Error("Out of Memory".into()).is_out_of_memory()); + assert!(!Fault::Error("Validation Error".into()).is_out_of_memory()); + assert!(!Fault::Error("Validation Error: buffer allocation would run out of memory".into()).is_out_of_memory()); + // A later out-of-memory error replaces another error, but never a loss. + let h = DeviceHealth::new(); + h.mark(Fault::Error("Validation Error".into())); + h.mark(Fault::Error(OUT_OF_MEMORY.into())); + assert!(h.fault().is_some_and(|f| f.is_out_of_memory())); + h.mark(Fault::Lost("reset".into())); + h.mark(Fault::Error(OUT_OF_MEMORY.into())); + assert!(h.fault().is_some_and(|f| f.is_lost())); + assert!(!Fault::Lost("out of memory".into()).is_out_of_memory()); } } diff --git a/crates/gpu/src/kit.rs b/crates/gpu/src/kit.rs new file mode 100644 index 0000000000..6ac1b76dc3 --- /dev/null +++ b/crates/gpu/src/kit.rs @@ -0,0 +1,144 @@ +use super::consts::{CHUNK_UNIFORM, OP_UNIFORM, STRIDE}; +use super::convert::{lut_texture, words}; +use super::textures::{ResKey, ResidentRef}; +use super::{Arc, HashMap, I, Kernel, NonZeroU64, Rect}; + +/// Pipelines and layouts, shared by cheap clones (wgpu handles are reference counted). +#[derive(Clone)] +pub(crate) struct Kit { + pub(crate) pipelines: HashMap<(Kernel, wgpu::TextureFormat), wgpu::RenderPipeline>, + pub(crate) bgl0: wgpu::BindGroupLayout, + pub(crate) bgl1: wgpu::BindGroupLayout, + pub(crate) dummy: wgpu::TextureView, +} + +/// One draw of an effect-map kernel (outside the chunk loop). +pub(crate) struct MapDraw { + pub(crate) kernel: Kernel, + pub(crate) format: wgpu::TextureFormat, + pub(crate) target: wgpu::TextureView, + /// Target-local scissor (x, y, w, h). + pub(crate) scissor: [u32; 4], + /// Chunk record: origin and size of the target's space. + pub(crate) space: [i32; 4], + pub(crate) op: Vec, + pub(crate) views: [Option; 8], + pub(crate) lut: Option>>, +} + +impl Kit { + /// Record `draws` (each its own render pass) into `encoder`. + pub(crate) fn run(&self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, draws: &[MapDraw]) { + if draws.is_empty() { + return; + } + let mut data = vec![0u8; draws.len() * 2 * STRIDE as usize]; + for (i, d) in draws.iter().enumerate() { + let w = words(&[I(d.space[0]), I(d.space[1]), I(d.space[2]), I(d.space[3])]); + data[2 * i * STRIDE as usize..][..w.len()].copy_from_slice(&w); + data[(2 * i + 1) * STRIDE as usize..][..d.op.len()].copy_from_slice(&d.op); + } + let ubuf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_fx_uniforms"), + size: data.len() as u64, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + queue.write_buffer(&ubuf, 0, &data); + let bg0 = uniform_group(device, &self.bgl0, &ubuf); + for (i, d) in draws.iter().enumerate() { + let lut = d.lut.as_ref().map(|l| { + let mut row = [0.0f32; 4096]; + row[..l.len().min(4096)].copy_from_slice(&l[..l.len().min(4096)]); + lut_texture(device, queue, &[row]) + }); + let mut views: Vec<&wgpu::TextureView> = d.views.iter().map(|v| v.as_ref().unwrap_or(&self.dummy)).collect(); + if let Some((_, lv)) = &lut { + views[4] = lv; + } + let bg1 = texture_group(device, &self.bgl1, &views); + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("pc_fx_map"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &d.target, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(&self.pipelines[&(d.kernel, d.format)]); + pass.set_scissor_rect(d.scissor[0], d.scissor[1], d.scissor[2], d.scissor[3]); + pass.set_bind_group(0, &bg0, &[(2 * i as u64 * STRIDE) as u32, ((2 * i + 1) as u64 * STRIDE) as u32]); + pass.set_bind_group(1, &bg1, &[]); + pass.draw(0..3, 0..1); + } + } +} + +pub(crate) fn uniform_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, ubuf: &wgpu::Buffer) -> wgpu::BindGroup { + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_compose_uniforms"), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(CHUNK_UNIFORM) }), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(OP_UNIFORM) }), + }, + ], + }) +} + +pub(crate) fn texture_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, views: &[&wgpu::TextureView]) -> wgpu::BindGroup { + let e: Vec = + views.iter().enumerate().map(|(b, v)| wgpu::BindGroupEntry { binding: b as u32, resource: wgpu::BindingResource::TextureView(v) }).collect(); + device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("pc_compose_pass"), layout, entries: &e }) +} + +pub(crate) fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: false }, + view_dimension: wgpu::TextureViewDimension::D2, + }, + count: None, + } +} + +pub(crate) fn uniform_entry(binding: u32, size: u64) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: true, min_binding_size: NonZeroU64::new(size) }, + count: None, + } +} + +/// The compositor's WGSL source (exposed for validation in tests). +pub const SHADER: &str = include_str!("compose.wgsl"); + +/// Pass-level resources resolved for one page cell: resident pages (texture, mask) and effect +/// maps (view and region) per pass. +pub(crate) struct Bound { + pub(crate) views: Vec<(Option, Option)>, + pub(crate) keys: Vec, + pub(crate) maps: Vec>, +} + +/// Pass-level resources shared by every cell of a frame. +pub(crate) struct FrameRes { + /// Tile area of each pass's (texture, mask) surface within the canvas tile grid. + pub(crate) areas: Vec<(Rect, Rect)>, + pub(crate) luts: Vec>, + pub(crate) patterns: Vec>, +} diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 690d144d68..6fd8c13439 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -34,14 +34,19 @@ //! (Multichannel documents, patterns larger than the texture limit, uncached gradients with //! coincident colour or opacity stops) returns [`Unsupported`]; //! callers fall back to the CPU compositor. +//! +//! [`CameraRawDevelop`] (`camera_raw.rs`) is a separate pass, not part of the compositor: Camera +//! Raw's per-pixel develop stages for the dialog's preview, built the first time the dialog opens. #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] pub mod bounds; +mod camera_raw; mod fx; pub mod health; pub mod plan; mod prefix; +pub mod timing; use std::collections::HashMap; use std::num::NonZeroU64; @@ -53,375 +58,10 @@ use photocraft_doc::{DocId, Document, LayerContent, LayerId, Pattern}; use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; use photocraft_raster::{Surface, Tile}; +pub use camera_raw::{CAMERA_RAW_SHADER, CameraRawDevelop}; pub use health::{DeviceHealth, Fault}; pub use plan::{Kernel, Plan, Role, Unsupported, plan}; - -/// Accumulator format for intermediate buffers. Full float: discontinuous operations -/// (Posterize, Threshold, Hard Mix, Dissolve) must land on the same side of their thresholds as -/// the CPU reference, and Color Burn/Dodge amplify input error. -pub const ACC_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba32Float; // default accumulation format; `preferred_acc_format` downgrades to Rgba16Float where 32-bit float isn't renderable -/// Effect intermediates (distances, blurs) and layer shapes. -const MAP32: wgpu::TextureFormat = wgpu::TextureFormat::R32Float; -/// Final effect coverage maps (0..1; half precision is ~1/4000). -const MAP16: wgpu::TextureFormat = wgpu::TextureFormat::R16Float; -/// Side of the square chunks the canvas is processed in. -pub const CHUNK: u32 = 1024; -const STRIDE: u64 = 256; -const CHUNK_UNIFORM: u64 = 16; -const OP_UNIFORM: u64 = 176; -/// GPU memory the effect-map cache may hold before evicting layers not drawn this frame. -pub const FX_BUDGET: usize = 1536 << 20; -/// Largest side of the square pages layer surfaces are stored in on the GPU (smaller when the -/// device's texture limit is). A chunk only binds the pages of the cell it lies in, so layers of -/// any size composite on the GPU. -pub const PAGE: u32 = 2048; -/// GPU memory resident layer pages may hold before the least recently used are evicted. -pub const RESIDENT_BUDGET: u64 = 4 << 30; -/// GPU memory pattern textures may hold before the least recently painted are dropped (those the -/// current frame paints with always stay). -pub const PATTERN_BUDGET: u64 = 256 << 20; -/// A pattern texture no frame has painted with for this many frames is dropped. -pub const PATTERN_IDLE_FRAMES: u64 = 240; -/// Uploads (bytes) [`Compositor::render`] stages before submitting the work recorded so far, so -/// a refresh of a huge document never holds all its uploads in staging memory at once. -const FLUSH_BYTES: u64 = 512 << 20; -/// Pass pixels (chunk pixels × passes) [`Compositor::render`] records before submitting: one -/// command buffer holding a whole huge refresh can run for seconds, long enough for the OS to -/// reset the GPU. -const FLUSH_WORK: u64 = 1 << 30; -/// Copies and render passes [`Compositor::render`] records before submitting. wgpu opens up to -/// two backend command buffers for each (a render pass and its barrier pre-pass), and Metal -/// treats more than 4096 outstanding ones as a lost device: a refresh of a document with a few -/// hundred layers reached that inside a single page cell (P12, "pc_compose_uniforms is -/// invalid"), where the work and byte limits above never trigger. -const FLUSH_OPS: u32 = 1000; - -/// A finished chunk: straight-alpha RGBA32F pixels of `rect` (document coordinates) at the -/// texture's origin. -pub struct ChunkOut<'a> { - pub rect: Rect, - pub texture: &'a wgpu::Texture, - pub view: &'a wgpu::TextureView, -} - -/// What a render did (for profiling and tests). -#[derive(Clone, Copy, Debug, Default)] -pub struct Stats { - pub passes: usize, - pub chunks: usize, - pub slots: u32, - pub tiles_uploaded: usize, - pub bytes_uploaded: usize, - /// Tiles a new layer texture took from another layer's texture by a GPU copy (a duplicate - /// shares its source's tiles) instead of uploading them. - pub tiles_shared: usize, - /// Effect layers whose shape was (re)rendered, fully or partially. - pub fx_shapes: usize, - /// Effect map programs run (one per effect whose maps were rebuilt). - pub fx_programs: usize, - /// Effect-map pixels written (all stages). - pub fx_pixels: u64, - /// Page cells the region spans (each binds only the layer pages it overlaps). - pub cells: usize, - /// Layer pages and paged effect maps evicted to stay within the memory budgets. - pub evicted: usize, - /// Intermediate submits of a huge refresh (to bound staging and evicted memory). - pub flushes: usize, - /// Chunks restored from an unchanged lower-stack composite. - pub prefix_hits: usize, - /// Planned prefix passes skipped, summed over the reused chunks. - pub prefix_passes_skipped: usize, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum TexKind { - /// RGBA8 tiles copied verbatim. - Rgba8Direct, - /// Other 8-bit formats converted to RGBA8. - Rgba8, - /// 16/32-bit formats converted to RGBA16F. - Rgba16F, - /// GRAY8 masks copied verbatim. - R8Direct, - /// Other masks as R32F. - R32F, -} - -impl TexKind { - fn for_surface(role: Role, f: PixelFormat) -> Self { - match role { - Role::Content | Role::Stroke if f == PixelFormat::RGBA8 => TexKind::Rgba8Direct, - Role::Content | Role::Stroke if f.sample == SampleType::U8 => TexKind::Rgba8, - Role::Content | Role::Stroke => TexKind::Rgba16F, - Role::Mask if f == PixelFormat::GRAY8 => TexKind::R8Direct, - Role::Mask => TexKind::R32F, - } - } - fn format(self) -> wgpu::TextureFormat { - match self { - TexKind::Rgba8Direct | TexKind::Rgba8 => wgpu::TextureFormat::Rgba8Unorm, - TexKind::Rgba16F => wgpu::TextureFormat::Rgba16Float, - TexKind::R8Direct => wgpu::TextureFormat::R8Unorm, - TexKind::R32F => wgpu::TextureFormat::R32Float, - } - } - fn bytes_per_pixel(self) -> usize { - match self { - TexKind::Rgba8Direct | TexKind::Rgba8 => 4, - TexKind::Rgba16F => 8, - TexKind::R8Direct => 1, - TexKind::R32F => 4, - } - } -} - -/// Index into the render's resident key list, and the texture's region (x, y, w, h). -type ResidentRef = (usize, [i32; 4]); -/// A page cell: the square `[x·page, (x+1)·page) × [y·page, (y+1)·page)` of document pixels. -type Cell = (i32, i32); -/// A resident layer page. -type ResKey = (LayerId, Role, Cell); -/// Effect maps of a layer: over its whole effect region (`None`), or one page cell of a region -/// larger than the texture limit. -type FxKey = (LayerId, Option); - -struct CachedPattern { - fingerprint: u64, - format: PixelFormat, - default_bits: Vec, - texture: Tex, - /// The document that last painted with it: closing that document frees the texture (#4971). - doc: DocId, - /// [`Compositor::frame`] when it last painted. - last_used: u64, - /// GPU bytes of the texture (RGBA32F). - bytes: u64, -} - -struct Resident { - texture: wgpu::Texture, - view: wgpu::TextureView, - /// Document-pixel rect covered (tile aligned, inside one page cell). - region: Rect, - kind: TexKind, - format: PixelFormat, - tiles: HashMap>, - default_nonzero: bool, - /// Bit pattern of the surface's default pixel when the texture was filled. Tiles the surface - /// doesn't store hold it, so a changed default (Invert, Fill) makes the texture stale. - default_bits: Vec, - doc: DocId, - last_used: u64, - /// Cell visit that last used it (least recently used pages are evicted first). - stamp: u64, - /// CMYK profile the texels were converted with (`CmykSpace::id`, 0 = built-in / not CMYK). - cmyk: u64, - /// Its texels were copied to or from another layer texture by the encoder, so every write - /// to it goes through the encoder too: a queued write lands before the encoder runs, and - /// would be overwritten by the copy, or leak into it. - ordered: bool, -} - -impl Resident { - fn bytes(&self) -> u64 { - self.region.width() as u64 * self.region.height() as u64 * self.kind.bytes_per_pixel() as u64 - } -} - -/// A texture and its default view. -#[derive(Clone)] -struct Tex { - texture: wgpu::Texture, - view: wgpu::TextureView, -} - -impl Tex { - fn new(device: &wgpu::Device, label: &str, w: u32, h: u32, format: wgpu::TextureFormat, usage: wgpu::TextureUsages) -> Self { - let texture = device.create_texture(&wgpu::TextureDescriptor { - label: Some(label), - size: wgpu::Extent3d { width: w.max(1), height: h.max(1), depth_or_array_layers: 1 }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format, - usage, - view_formats: &[], - }); - let view = texture.create_view(&Default::default()); - Tex { texture, view } - } - fn map(device: &wgpu::Device, label: &str, r: Rect, format: wgpu::TextureFormat) -> Self { - Tex::new( - device, - label, - r.width(), - r.height(), - format, - wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - ) - } - /// Upload R32F values (row-major over `r`) into this region-space texture at `r`. - fn write_r32(&self, queue: &wgpu::Queue, region: Rect, r: Rect, v: &[f32]) { - if r.is_empty() { - return; - } - let bytes: Vec = v.iter().flat_map(|x| x.to_le_bytes()).collect(); - queue.write_texture( - wgpu::TexelCopyTextureInfo { - texture: &self.texture, - mip_level: 0, - origin: wgpu::Origin3d { x: (r.x0 - region.x0) as u32, y: (r.y0 - region.y0) as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - &bytes, - wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(r.width() * 4), rows_per_image: Some(r.height()) }, - wgpu::Extent3d { width: r.width(), height: r.height(), depth_or_array_layers: 1 }, - ); - } - fn bytes(&self) -> usize { - let s = self.texture.size(); - let bpp = match self.texture.format() { - wgpu::TextureFormat::R16Float => 2, - wgpu::TextureFormat::Rgba32Float => 16, - wgpu::TextureFormat::Rgba16Float => 8, - _ => 4, - }; - s.width as usize * s.height as usize * bpp - } -} - -/// Cached maps of one effect (one enabled item of a layer). -struct ProgState { - key: u64, - maps: Vec>, -} - -/// Cached effect state of one layer. -struct FxEntry { - doc: DocId, - region: Rect, - shape_key: u64, - /// Tiles the shape was rendered from (content, mask): damage is their difference. - tiles: [Option; 2], - /// Groups: the tiles `shape_key` hashes, by address only (see [`fx::Tiles`]). - _pin: Vec>, - /// The shape on the CPU (distance fields are computed from it) and on the GPU. - shape_cpu: Vec, - shape: Tex, - /// Distance fields: the reach each is exact to, and its texture. - fields: HashMap, - progs: Vec, - last_used: u64, - stamp: u64, -} - -impl FxEntry { - fn bytes(&self) -> usize { - self.shape.bytes() - + self.shape_cpu.len() * 4 - + self.fields.values().map(|(_, t)| t.bytes()).sum::() - + self.progs.iter().flat_map(|p| p.maps.iter().flatten()).map(Tex::bytes).sum::() - } -} - -/// Pipelines and layouts, shared by cheap clones (wgpu handles are reference counted). -#[derive(Clone)] -struct Kit { - pipelines: HashMap<(Kernel, wgpu::TextureFormat), wgpu::RenderPipeline>, - bgl0: wgpu::BindGroupLayout, - bgl1: wgpu::BindGroupLayout, - dummy: wgpu::TextureView, -} - -/// One draw of an effect-map kernel (outside the chunk loop). -struct MapDraw { - kernel: Kernel, - format: wgpu::TextureFormat, - target: wgpu::TextureView, - /// Target-local scissor (x, y, w, h). - scissor: [u32; 4], - /// Chunk record: origin and size of the target's space. - space: [i32; 4], - op: Vec, - views: [Option; 8], - lut: Option>>, -} - -impl Kit { - /// Record `draws` (each its own render pass) into `encoder`. - fn run(&self, device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, draws: &[MapDraw]) { - if draws.is_empty() { - return; - } - let mut data = vec![0u8; draws.len() * 2 * STRIDE as usize]; - for (i, d) in draws.iter().enumerate() { - let w = words(&[I(d.space[0]), I(d.space[1]), I(d.space[2]), I(d.space[3])]); - data[2 * i * STRIDE as usize..][..w.len()].copy_from_slice(&w); - data[(2 * i + 1) * STRIDE as usize..][..d.op.len()].copy_from_slice(&d.op); - } - let ubuf = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("pc_fx_uniforms"), - size: data.len() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); - queue.write_buffer(&ubuf, 0, &data); - let bg0 = uniform_group(device, &self.bgl0, &ubuf); - for (i, d) in draws.iter().enumerate() { - let lut = d.lut.as_ref().map(|l| { - let mut row = [0.0f32; 4096]; - row[..l.len().min(4096)].copy_from_slice(&l[..l.len().min(4096)]); - lut_texture(device, queue, &[row]) - }); - let mut views: Vec<&wgpu::TextureView> = d.views.iter().map(|v| v.as_ref().unwrap_or(&self.dummy)).collect(); - if let Some((_, lv)) = &lut { - views[4] = lv; - } - let bg1 = texture_group(device, &self.bgl1, &views); - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("pc_fx_map"), - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &d.target, - resolve_target: None, - depth_slice: None, - ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - multiview_mask: None, - }); - pass.set_pipeline(&self.pipelines[&(d.kernel, d.format)]); - pass.set_scissor_rect(d.scissor[0], d.scissor[1], d.scissor[2], d.scissor[3]); - pass.set_bind_group(0, &bg0, &[(2 * i as u64 * STRIDE) as u32, ((2 * i + 1) as u64 * STRIDE) as u32]); - pass.set_bind_group(1, &bg1, &[]); - pass.draw(0..3, 0..1); - } - } -} - -fn uniform_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, ubuf: &wgpu::Buffer) -> wgpu::BindGroup { - device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("pc_compose_uniforms"), - layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(CHUNK_UNIFORM) }), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::Buffer(wgpu::BufferBinding { buffer: ubuf, offset: 0, size: NonZeroU64::new(OP_UNIFORM) }), - }, - ], - }) -} - -fn texture_group(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, views: &[&wgpu::TextureView]) -> wgpu::BindGroup { - let e: Vec = - views.iter().enumerate().map(|(b, v)| wgpu::BindGroupEntry { binding: b as u32, resource: wgpu::BindingResource::TextureView(v) }).collect(); - device.create_bind_group(&wgpu::BindGroupDescriptor { label: Some("pc_compose_pass"), layout, entries: &e }) -} +pub use timing::{GpuTimer, GpuTiming}; /// GPU compositor. Create once per device; call [`Compositor::render`] per refresh. pub struct Compositor { @@ -451,6 +91,12 @@ pub struct Compositor { resident_budget: u64, /// GPU memory the effect-map cache may hold ([`FX_BUDGET`] or less). fx_budget: u64, + /// Layer-page bytes the last full fit check asked for ([`Compositor::fit_layers`]). + layer_need: u64, + /// Bytes of `fx_budget` lent to `resident_budget` so those pages fit. + lent: u64, + /// `resident_budget` was set exactly ([`Compositor::set_resident_budget`]): nothing is lent. + exact_resident: bool, /// What the view shows: a full refresh draws these cells last, so their pages are the /// ones still resident for the edits that follow. focus: Option, @@ -461,6 +107,13 @@ pub struct Compositor { staged: u64, /// Copies and render passes recorded since the last submit. recorded: u32, + /// Why a layer page texture couldn't be created this refresh (it then ends on the CPU). + layer_alloc: Option, + /// Bytes of pages dropped to admit new ones, not yet counted towards an intermediate submit. + admit_freed: u64, + /// Test hook ([`Compositor::fail_layer_allocs`]): how many more layer page textures fail as + /// out of memory. + fail_allocs: u32, acc_format: wgpu::TextureFormat, /// Whether the effect-map pipelines could be built (else documents with layer effects are /// [`Unsupported`] and use the CPU compositor). @@ -469,2159 +122,46 @@ pub struct Compositor { cmyk: u64, /// The device's health (see [`Compositor::set_health`]); `None` = assumed healthy. health: Option, -} - -fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { - wgpu::BindGroupLayoutEntry { - binding, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - multisampled: false, - sample_type: wgpu::TextureSampleType::Float { filterable: false }, - view_dimension: wgpu::TextureViewDimension::D2, - }, - count: None, - } -} - -fn uniform_entry(binding: u32, size: u64) -> wgpu::BindGroupLayoutEntry { - wgpu::BindGroupLayoutEntry { - binding, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: true, min_binding_size: NonZeroU64::new(size) }, - count: None, - } -} - -/// The compositor's WGSL source (exposed for validation in tests). -pub const SHADER: &str = include_str!("compose.wgsl"); - -/// Pass-level resources resolved for one page cell: resident pages (texture, mask) and effect -/// maps (view and region) per pass. -struct Bound { - views: Vec<(Option, Option)>, - keys: Vec, - maps: Vec>, -} - -/// Pass-level resources shared by every cell of a frame. -struct FrameRes { - /// Tile area of each pass's (texture, mask) surface within the canvas tile grid. - areas: Vec<(Rect, Rect)>, - luts: Vec>, - patterns: Vec>, -} - -/// Document pixels of page cell `c`. -fn cell_rect(c: Cell, page: u32) -> Rect { - let p = page as i32; - Rect::new(c.0.saturating_mul(p), c.1.saturating_mul(p), c.0.saturating_add(1).saturating_mul(p), c.1.saturating_add(1).saturating_mul(p)) -} - -/// `r` cut along the grid of `step`-sized squares (row-major). -fn grid_rects(r: Rect, step: u32) -> Vec { - let s = step.max(1) as i32; - let mut out = Vec::new(); - let mut y = r.y0.div_euclid(s) * s; - while y < r.y1 { - let mut x = r.x0.div_euclid(s) * s; - while x < r.x1 { - out.push(Rect::new(x, y, x.saturating_add(s), y.saturating_add(s)).intersect(&r)); - x = x.saturating_add(s); - } - y = y.saturating_add(s); - } - out -} - -/// Number of rectangles `grid_rects` would produce, without allocating them during preflight. -fn grid_rect_count(r: Rect, step: u32) -> Result { - if r.is_empty() { - return Ok(0); - } - let s = i64::from(step.max(1)); - let axis = |lo: i32, hi: i32| usize::try_from((i64::from(hi) - 1).div_euclid(s) - i64::from(lo).div_euclid(s) + 1).ok(); - axis(r.x0, r.x1).and_then(|x| axis(r.y0, r.y1).and_then(|y| x.checked_mul(y))).ok_or_else(|| Unsupported("GPU compositor chunk count is too large".into())) -} - -/// Host length and GPU capacity for the chunk/pass uniform records, including their u32 -/// dynamic binding offsets. Decline unsupported stacks before an invalid buffer faults the device. -fn uniform_buffer_len(chunks: usize, passes: usize, limit: u64) -> Result { - let too_large = || Unsupported("GPU compositor uniform buffer is too large".into()); - let records = chunks.checked_add(passes).and_then(|n| u64::try_from(n).ok()).ok_or_else(too_large)?; - let bytes = records.checked_mul(STRIDE).ok_or_else(too_large)?; - if bytes > limit { - return Err(Unsupported(format!("GPU compositor uniform buffer requires {bytes} bytes; device limit is {limit}"))); - } - if bytes.saturating_sub(STRIDE) > u64::from(u32::MAX) { - return Err(Unsupported("GPU compositor uniform offsets exceed u32".into())); - } - usize::try_from(bytes).map_err(|_| too_large()) -} - -/// The reach up to which each distance field must be exact (the largest any program asks for). -fn field_reaches(progs: &[fx::MapProgram]) -> HashMap { - let mut want: HashMap = HashMap::new(); - for (k, r) in progs.iter().flat_map(|p| p.fields.iter()) { - let w = want.entry(*k).or_insert(0); - *w = (*w).max(*r); - } - want -} - -/// Runs `f` inside GPU error scopes and returns the first error raised. -/// Native wgpu reports errors synchronously; where they only arrive asynchronously (WebGPU) none -/// is reported. -fn first_error(device: &wgpu::Device, f: impl FnOnce() -> T) -> (T, Option) { - let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); - let internal = device.push_error_scope(wgpu::ErrorFilter::Internal); - let memory = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); - let out = f(); - let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); - let mut error = None; - // Scopes pop in reverse order. - for pop in [memory.pop(), internal.pop(), validation.pop()] { - if let std::task::Poll::Ready(Some(e)) = std::pin::pin!(pop).poll(&mut cx) { - error.get_or_insert(e); - } - } - (out, error) -} - -/// Unwatched native callers need GPU errors to become `Unsupported` instead of reaching wgpu's -/// default panic handler. Watched callers keep their fault callbacks, which stop work promptly -/// and update the health flag shared with the canvas. -fn scoped_work(device: &wgpu::Device, watched: bool, f: impl FnOnce() -> Result) -> Result { - // WebGPU errors arrive asynchronously; its callers still use the health callbacks. - if watched || cfg!(target_arch = "wasm32") { - return f(); - } - let (result, error) = first_error(device, f); - match error { - Some(error) => Err(Unsupported(format!("GPU work failed: {error}"))), - None => result, - } -} - -impl Compositor { - /// Create a compositor with the default accumulation format ([`ACC_FORMAT`]). - pub fn new(device: &wgpu::Device) -> Self { - Self::new_with_format(device, ACC_FORMAT) - } - - /// The accumulation format to use on `adapter`: `Rgba32Float` when it can be a render target, - /// else `Rgba16Float`. Some drivers (e.g. Intel Vulkan) don't support rendering to `Rgba32Float` - /// and would otherwise panic at render-pipeline creation. - pub fn preferred_acc_format(adapter: &wgpu::Adapter) -> wgpu::TextureFormat { - let feats = adapter.get_texture_format_features(wgpu::TextureFormat::Rgba32Float); - if feats.allowed_usages.contains(wgpu::TextureUsages::RENDER_ATTACHMENT) { wgpu::TextureFormat::Rgba32Float } else { wgpu::TextureFormat::Rgba16Float } - } - - /// [`Compositor::new_with_format`], but a shader or pipeline the driver can't build (e.g. a - /// D3D12 shader-compiler failure) is an `Err` instead of a panic, so the caller can fall back - /// to the CPU compositor. Where errors only arrive asynchronously (WebGPU), creation is assumed - /// to have worked. - pub fn try_new_with_format(device: &wgpu::Device, acc_format: wgpu::TextureFormat) -> Result { - match first_error(device, || Self::new_with_format(device, acc_format)) { - (comp, None) => Ok(comp), - (_, Some(e)) => Err(Unsupported(format!("couldn't build the compositor's pipelines: {e}"))), - } - } - - /// Create a compositor whose accumulation/render-target format is `acc_format`. Pass - /// `Rgba16Float` on adapters that can't render to `Rgba32Float` (e.g. some Intel Vulkan drivers), - /// which otherwise panics at render-pipeline creation. See `Compositor::preferred_acc_format`. - pub fn new_with_format(device: &wgpu::Device, acc_format: wgpu::TextureFormat) -> Self { - let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_compose"), source: wgpu::ShaderSource::Wgsl(SHADER.into()) }); - let bgl0 = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("pc_compose_uniforms"), - entries: &[uniform_entry(0, CHUNK_UNIFORM), uniform_entry(1, OP_UNIFORM)], - }); - let bgl1 = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - label: Some("pc_compose_textures"), - entries: &(0..8).map(tex_entry).collect::>(), - }); - let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { - label: Some("pc_compose"), - bind_group_layouts: &[Some(&bgl0), Some(&bgl1)], - immediate_size: 0, - }); - // `None` only for kernels without a fragment entry point, which are never drawn. - let pipeline = |k: Kernel, format: wgpu::TextureFormat| { - let entry = k.entry()?; - Some(device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some(entry), - layout: Some(&layout), - vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, - primitive: wgpu::PrimitiveState::default(), - depth_stencil: None, - multisample: wgpu::MultisampleState::default(), - fragment: Some(wgpu::FragmentState { - module: &module, - entry_point: Some(entry), - targets: &[Some(wgpu::ColorTargetState { format, blend: None, write_mask: wgpu::ColorWrites::ALL })], - compilation_options: Default::default(), - }), - multiview_mask: None, - cache: None, - })) - }; - let mut pipelines: HashMap<_, _> = - Kernel::DRAWN.iter().filter(|k| !k.is_map()).filter_map(|&k| Some(((k, acc_format), pipeline(k, acc_format)?))).collect(); - // Effect maps render to R32Float / R16Float, which some adapters (e.g. GL without float - // render targets) can't: then layer effects use the CPU compositor and the rest stays here. - let maps = Kernel::DRAWN.iter().filter(|k| k.is_map()).flat_map(|&k| [(k, MAP32), (k, MAP16)]); - let (maps, map_error) = first_error(device, || maps.filter_map(|key| Some((key, pipeline(key.0, key.1)?))).collect::>()); - if let Some(e) = &map_error { - log::info!("GPU compositor: no effect-map pipelines ({e}); layer effects use the CPU"); - } else { - pipelines.extend(maps); - } - let dummy = Tex::new(device, "pc_compose_dummy", 1, 1, wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureUsages::TEXTURE_BINDING).view; - Self { - kit: Kit { pipelines, bgl0, bgl1, dummy }, - pool: Vec::new(), - residents: HashMap::new(), - fx: HashMap::new(), - prefix: prefix::Prefix::default(), - temps: Vec::new(), - patterns: HashMap::new(), - pattern_mode: photocraft_color::ColorMode::Rgb, - pattern_doc: DocId(0), - pattern_budget: PATTERN_BUDGET, - max_dim: 0, - device_max: device.limits().max_texture_dimension_2d, - page: PAGE, - chunk: CHUNK, - resident_budget: RESIDENT_BUDGET, - fx_budget: (FX_BUDGET as u64).saturating_sub(prefix::MAX_BYTES), - focus: None, - frame: 0, - stamp: 0, - staged: 0, - recorded: 0, - acc_format, - effect_maps: map_error.is_none(), - cmyk: 0, - health: None, - } - .with_texture_limit(device.limits().max_texture_dimension_2d) - } - - fn with_texture_limit(mut self, max: u32) -> Self { - self.set_texture_limit(max); - self - } - - /// Plan paging for a texture side limit of `max` (clamped to the device's own limit and at - /// least one tile). Tests use a small limit to exercise documents larger than it on any - /// adapter. Drops every cached texture. - pub fn set_texture_limit(&mut self, max: u32) { - let tile = TILE_SIZE as u32; - self.max_dim = max.min(self.device_max).max(tile); - let mut page = tile; - while page * 2 <= self.max_dim.min(PAGE) { - page *= 2; - } - self.page = page; - self.chunk = CHUNK.min(page); - self.prefix.set_step(self.chunk); - self.pool.clear(); - self.residents.clear(); - self.fx.clear(); - self.temps.clear(); - self.prefix.clear(); - } - - /// The page side layer surfaces are stored in. - pub fn page_size(&self) -> u32 { - self.page - } - - /// Set the GPU memory resident layer pages may hold (default [`RESIDENT_BUDGET`]). - pub fn set_resident_budget(&mut self, bytes: u64) { - self.resident_budget = bytes; - } - - /// Set the GPU memory the compositor may hold for layer pages and caches together - /// (a quarter of it, at most [`FX_BUDGET`], for effect maps and lower-stack chunks). Uploads and evicted pages in - /// flight during a refresh stay within about a quarter more. - pub fn set_memory_budget(&mut self, bytes: u64) { - let caches = (FX_BUDGET as u64).min(bytes / 4); - self.prefix.set_budget((caches / 2).min(prefix::MAX_BYTES)); - self.fx_budget = caches.saturating_sub(self.prefix.budget()); - self.resident_budget = bytes.saturating_sub(caches); - } - - /// The budget of layer pages, effect maps and lower-stack chunks (bytes). - pub fn memory_budget(&self) -> u64 { - self.resident_budget.saturating_add(self.fx_budget).saturating_add(self.prefix.budget()) - } - - /// Bytes held by reusable lower-stack chunks, within the overall cache allowance. - pub fn composite_cache_bytes(&self) -> u64 { - self.prefix.bytes() - } - - /// Cap lower-stack reuse (default at most 512 MiB); zero disables it. The released allowance - /// goes back to effect maps, preserving the total budget and layer-page allowance. - pub fn set_composite_cache_budget(&mut self, bytes: u64) { - let caches = self.fx_budget.saturating_add(self.prefix.budget()); - self.prefix.set_budget(bytes.min(caches)); - self.fx_budget = caches.saturating_sub(self.prefix.budget()); - } - - /// Bytes of the layer pages a render of `region` needs resident (each layer and mask - /// surface's tiles within the region's page cells, in their texture format). - pub fn working_set(&self, doc: &Document, region: Rect) -> u64 { - let Ok(plan) = plan(doc) else { return 0 }; - let grid = tile_grid(doc.bounds()); - let cells = grid_rects(region.intersect(&doc.bounds()), self.page) - .into_iter() - .map(|r| cell_rect((r.x0.div_euclid(self.page as i32), r.y0.div_euclid(self.page as i32)), self.page)) - .fold(Rect::EMPTY, |a, c| if a.is_empty() { c } else { a.union(&c) }); - let mut seen = std::collections::HashSet::new(); - let mut total = 0u64; - for p in &plan.passes { - let sources = [p.tex.as_ref().map(|t| (t.layer, t.role, t.surface.get())), p.mask.as_ref().map(|m| (m.layer, Role::Mask, m.surface.get()))]; - for (layer, role, s) in sources.into_iter().flatten() { - if seen.insert((layer, role)) { - let a = s.tile_bounds().intersect(&grid).intersect(&cells); - total += a.width() as u64 * a.height() as u64 * TexKind::for_surface(role, s.format()).bytes_per_pixel() as u64; - } - } - } - total - } - - /// The GPU memory resident layer pages may hold (bytes; see [`Compositor::set_memory_budget`]). - pub fn resident_budget(&self) -> u64 { - self.resident_budget - } - - /// Whether a render of `region` keeps every layer page it needs within the budget; when it - /// doesn't, each render re-uploads what was evicted. - pub fn fits_budget(&self, doc: &Document, region: Rect) -> bool { - self.working_set(doc, region) <= self.resident_budget - } - - /// The document area the view shows (`None`: unknown). Full refreshes draw its cells last. - pub fn set_focus(&mut self, focus: Option) { - self.focus = focus; - } - - /// GPU memory held by resident layer pages (bytes). - pub fn resident_bytes(&self) -> u64 { - self.residents.values().map(Resident::bytes).sum() - } - - /// GPU memory held by cached pattern textures (bytes). - pub fn pattern_cache_bytes(&self) -> u64 { - self.patterns.values().map(|p| p.bytes).sum() - } - - /// Set the GPU memory pattern textures may hold (default [`PATTERN_BUDGET`]). It takes effect - /// at the end of the next refresh. - pub fn set_pattern_budget(&mut self, bytes: u64) { - self.pattern_budget = bytes; - } - - /// Whether `doc` can be composited on the GPU. Documents and layers larger than the texture - /// limit are fine: surfaces are stored in page-sized textures and effect maps of regions - /// larger than the limit are built per page cell. - pub fn supports(&self, doc: &Document) -> Result<(), Unsupported> { - let p = plan(doc)?; - self.check_fx(doc, &p) - } - - /// Whether the maps of an effect region are built per page cell (it exceeds the limit). - fn fx_paged(&self, region: Rect) -> bool { - region.width() > self.max_dim || region.height() > self.max_dim - } - - /// How far beyond a page cell the effect maps of `f` must be computed to be exact in it. - fn fx_apron(doc: &Document, f: &plan::FxLayer<'_>) -> i32 { - let vector_shape = matches!(f.layer.content, LayerContent::Shape(_)) && photocraft_compose::effect_outline(f.layer).is_none(); - let progs: Vec = f - .layer - .effects - .items - .iter() - .filter(|e| e.enabled()) - .map(|e| fx::program_with(e, &doc.global_light, vector_shape, &doc.patterns, (0.0, 0.0))) - .collect(); - let want = field_reaches(&progs); - // Distance fields are exact one pixel inside the window they are computed from. - progs.iter().map(|p| p.reach(&|k| want.get(&k).copied().unwrap_or(0))).max().unwrap_or(0).saturating_add(2) - } - - /// Effect maps must fit in textures (per page cell when the region exceeds the limit), and - /// patterns in one texture. - fn check_fx(&self, doc: &Document, p: &Plan<'_>) -> Result<(), Unsupported> { - if !self.effect_maps && !p.fx.is_empty() { - return Err(Unsupported("layer effects need float effect-map render targets, which this GPU lacks".into())); - } - for f in &p.fx { - if self.fx_paged(f.region) { - let side = i64::from(self.page) + 2 * i64::from(Self::fx_apron(doc, f)); - if side > i64::from(self.device_max) { - return Err(Unsupported(format!("effects of `{}` reach too far to build per page", f.layer.name))); - } - } - for e in f.layer.effects.items.iter().filter(|e| e.enabled()) { - // Effect noise is speckled in the CPU maps; keep CPU and GPU identical by - // rendering noise-carrying effects on the CPU path. - let noisy = match e { - photocraft_doc::Effect::DropShadow(s) | photocraft_doc::Effect::InnerShadow(s) => s.noise > 0.0, - photocraft_doc::Effect::OuterGlow(g) | photocraft_doc::Effect::InnerGlow(g) => g.noise > 0.0, - _ => false, - }; - if noisy { - return Err(Unsupported(format!("effect noise on `{}` uses the CPU path", f.layer.name))); - } - for s in fx::program_with(e, &doc.global_light, false, &doc.patterns, (0.0, 0.0)).stages { - if s.lut.as_ref().is_some_and(|l| l.len() > 4096) { - return Err(Unsupported(format!("effect blur on `{}` too wide for the GPU path", f.layer.name))); - } - } - } - } - for pass in &p.passes { - if let Some(pat) = pass.pattern - && (pat.width > self.device_max || pat.height > self.device_max) - { - return Err(Unsupported(format!("pattern `{}` larger than the GPU texture limit", pat.name))); - } - } - Ok(()) - } - - /// Drop GPU textures of a closed document. - pub fn forget_doc(&mut self, doc: DocId) { - self.residents.retain(|_, r| r.doc != doc); - self.fx.retain(|_, e| e.doc != doc); - self.patterns.retain(|_, p| p.doc != doc); - self.prefix.forget(doc); - } - - /// [`Self::forget_doc`] for every document `keep` rejects: what the compositor holds for - /// documents that were closed, however their canvas entry went (#4529). - pub fn retain_docs(&mut self, keep: impl Fn(DocId) -> bool) { - let mut gone: Vec = self - .residents - .values() - .map(|r| r.doc) - .chain(self.fx.values().map(|e| e.doc)) - .chain(self.patterns.values().map(|p| p.doc)) - .chain(self.prefix.doc()) - .filter(|d| !keep(*d)) - .collect(); - gone.sort_by_key(|d| d.0); - gone.dedup(); - for d in gone { - self.forget_doc(d); - } - } - - /// Documents the compositor holds pages, effect maps, pattern textures or a lower-stack - /// composite for. - pub fn held_docs(&self) -> Vec { - let mut docs: Vec = self - .residents - .values() - .map(|r| r.doc) - .chain(self.fx.values().map(|e| e.doc)) - .chain(self.patterns.values().map(|p| p.doc)) - .chain(self.prefix.doc()) - .collect(); - docs.sort_by_key(|d| d.0); - docs.dedup(); - docs - } - - /// GPU memory held by cached effect maps (bytes). - pub fn fx_cache_bytes(&self) -> usize { - self.fx.values().map(FxEntry::bytes).sum() - } - - /// Watch `health` (see [`DeviceHealth::watch`]): once the device is lost or reported an - /// error, every render returns [`Unsupported`] without touching the GPU, so callers use the - /// CPU compositor instead. - pub fn set_health(&mut self, health: DeviceHealth) { - self.health = Some(health); - } - - /// Why the device can't be used (`None` while healthy or unwatched). - pub fn fault(&self) -> Option { - self.health.as_ref().and_then(DeviceHealth::fault) - } - - /// Composite `region` of `doc`. Each finished chunk is passed to `sink` while its encoder is - /// still open; the chunk texture is reused afterwards, so the sink must record any copies - /// or passes that read it into the given encoder. Submits the work before returning; a - /// refresh of a huge document also submits in between (the sink may be handed a new - /// encoder), so its uploads and evicted pages never pile up in memory. - pub fn render( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - doc: &Document, - region: Rect, - mut sink: impl FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), - ) -> Result { - let result = scoped_work(device, self.health.is_some(), || { - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose") }); - let stats = self.encode_inner(device, queue, &mut encoder, doc, region, &mut sink, true)?; - queue.submit([encoder.finish()]); - Ok(stats) - }); - if result.is_err() { - self.discard_failed_work(); - } - result - } - - /// Like [`Self::render`] but records into `encoder` without submitting. - pub fn encode( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: &Document, - region: Rect, - sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), - ) -> Result { - let result = scoped_work(device, self.health.is_some(), || self.encode_inner(device, queue, encoder, doc, region, sink, false)); - if result.is_err() { - self.discard_failed_work(); - } - result - } - - fn discard_failed_work(&mut self) { - // Cache metadata advances while recording. A failed encoder may never execute its - // texture uploads or effect maps, so none of those cached pixels can be trusted. - self.prefix.clear(); - self.residents.clear(); - self.fx.clear(); - self.patterns.clear(); - self.temps.clear(); - self.pool.clear(); - } - - #[allow(clippy::too_many_arguments)] - fn encode_inner( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: &Document, - region: Rect, - sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), - flush: bool, - ) -> Result { - if let Some(f) = self.fault() { - return Err(Unsupported(f.to_string())); - } - // CMYK layers convert through the document's own CMYK profile (uploads and plan colours). - let space = photocraft_compose::cmyk_space(doc); - self.cmyk = space.as_ref().map_or(0, |s| s.id); - photocraft_color::convert::with_cmyk_space(space.as_ref(), || self.encode_scoped(device, queue, encoder, doc, region, sink, flush)) - } - - #[allow(clippy::too_many_arguments)] - fn encode_scoped( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: &Document, - region: Rect, - sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), - flush: bool, - ) -> Result { - let canvas = doc.bounds(); - let region = region.intersect(&canvas); - let plan = plan(doc)?; - self.check_fx(doc, &plan)?; - self.pattern_mode = doc.pixel_format().mode; - self.pattern_doc = doc.id; - let mut stats = Stats { passes: plan.passes.len(), slots: plan.slots, ..Default::default() }; - if region.is_empty() { - return Ok(stats); - } - let page = self.page; - let mut cells = grid_rects(region, page); - // Check every actual page cell before uploads, submissions or sink output. A later - // cell can need more chunk records than the first; a failure must leave the device healthy. - let buffer_limit = device.limits().max_buffer_size; - for part in &cells { - uniform_buffer_len(grid_rect_count(*part, self.chunk)?, plan.passes.len(), buffer_limit)?; - } - if self.frame == 0 { - // The first submission after device creation is dropped on some drivers (RADV), - // which would render the first frame black; land it on an empty buffer instead. - queue.submit(std::iter::empty::()); - } - self.frame += 1; - self.stamp += 1; - self.staged = 0; - self.recorded = 0; - - // Only render owns submission. A caller may discard encode's command buffer, so it - // cannot establish (or consume) a checkpoint whose GPU copy might never execute. - let checkpoint = if flush { self.prefix.prepare(doc, &plan) } else { None }; - - // Effect maps of whole regions first (they may render group shapes through sub-plans); - // regions larger than the limit are built per cell below. - let paged: Vec = plan.fx.iter().map(|f| self.fx_paged(f.region)).collect(); - let aprons: Vec = plan.fx.iter().zip(&paged).map(|(f, &pg)| if pg { Self::fx_apron(doc, f) } else { 0 }).collect(); - for (f, &pg) in plan.fx.iter().zip(&paged) { - if !pg { - self.sync_fx(device, queue, encoder, doc, f, (f.layer.id, None), f.region, &mut stats); - } - } - let fr = self.frame_res(device, queue, canvas, &plan); - - // Page cells; every other frame in reverse, so a repeated full refresh of a document - // larger than the budget starts with the pages still resident. - if self.frame.is_multiple_of(2) { - cells.reverse(); - } - if let Some(f) = self.focus { - let p = page as i32; - let focused = |r: &Rect| !cell_rect((r.x0.div_euclid(p), r.y0.div_euclid(p)), page).intersect(&f).is_empty(); - cells.sort_by_key(focused); - } - let flush_bytes = FLUSH_BYTES.min(self.memory_budget() / 8).max(16 << 20); - stats.cells = cells.len(); - // The previous intermediate submit (see `submit_batch`). - let mut last_submit: Option = None; - // Work recorded and bytes evicted since the last submit. - let (mut work, mut freed) = (0u64, 0u64); - for part in cells { - self.stamp += 1; - let cell = (part.x0.div_euclid(page as i32), part.y0.div_euclid(page as i32)); - let cr = cell_rect(cell, page); - for (i, f) in plan.fx.iter().enumerate() { - let pf = cr.intersect(&f.region); - if !paged.get(i).copied().unwrap_or(false) || pf.is_empty() { - continue; - } - let window = pf.inflate(aprons.get(i).copied().unwrap_or(0)).intersect(&f.region); - self.sync_fx(device, queue, encoder, doc, f, (f.layer.id, Some(cell)), window, &mut stats); - } - let chunks = grid_rects(part, self.chunk); - stats.chunks += chunks.len(); - work += part.width() as u64 * part.height() as u64 * plan.passes.len() as u64; - let mut batch = flush.then_some(&mut last_submit); - self.run_cell(device, queue, encoder, doc.id, &plan, &paged, cell, &fr, &chunks, checkpoint, &mut stats, sink, &mut batch); - // The device was lost in a submit inside the cell: issue no more work. - if let Some(f) = self.fault() { - return Err(Unsupported(f.to_string())); - } - let (evicted, bytes) = self.evict(); - stats.evicted += evicted; - freed += bytes; - if flush && (self.staged >= flush_bytes || work >= FLUSH_WORK || freed >= flush_bytes || self.recorded >= FLUSH_OPS) { - submit_batch(device, queue, encoder, self.health.as_ref(), &mut last_submit); - self.staged = 0; - self.recorded = 0; - work = 0; - freed = 0; - stats.flushes += 1; - // The device was lost mid-refresh: issue no more work. - if let Some(f) = self.fault() { - return Err(Unsupported(f.to_string())); - } - } - } - - // Evict this document's textures whose layers are gone (hidden layers stay resident so - // toggling visibility costs no upload). - let frame = self.frame; - let live: std::collections::HashSet = doc.walk().into_iter().map(|(_, _, l)| l.id).collect(); - self.residents.retain(|(id, _, _), r| r.doc != doc.id || r.last_used == frame || live.contains(id)); - self.fx.retain(|(id, _), e| e.doc != doc.id || e.last_used == frame || live.contains(id)); - stats.evicted += self.evict().0; - self.temps.retain(|(_, used)| frame.saturating_sub(*used) < 240); - self.evict_patterns(); - Ok(stats) - } - - /// Drop the pattern textures no frame has painted with for [`PATTERN_IDLE_FRAMES`] frames, - /// then the least recently painted ones over the pattern budget. Never one this frame painted - /// with: it would only be uploaded again on the next frame. - fn evict_patterns(&mut self) { - let frame = self.frame; - self.patterns.retain(|_, p| frame.saturating_sub(p.last_used) < PATTERN_IDLE_FRAMES); - let mut total = self.pattern_cache_bytes(); - if total > self.pattern_budget { - let mut lru: Vec<(u64, String, u64)> = - self.patterns.iter().filter(|(_, p)| p.last_used != frame).map(|(k, p)| (p.last_used, k.clone(), p.bytes)).collect(); - lru.sort_unstable(); - for (_, key, bytes) in lru { - if total <= self.pattern_budget { - break; - } - self.patterns.remove(&key); - total = total.saturating_sub(bytes); - } - } - } - - /// Keep resident pages within the resident budget and the effect cache within - /// its budget, least recently used first. Never drops what the current cell uses, nor - /// whole-region effect maps drawn this frame. Returns how many textures were dropped and - /// their bytes. - fn evict(&mut self) -> (usize, u64) { - let (mut n, mut freed) = (0, 0u64); - let mut total = self.resident_bytes(); - if total > self.resident_budget { - let mut lru: Vec<(u64, ResKey, u64)> = - self.residents.iter().filter(|(_, r)| r.stamp != self.stamp).map(|(k, r)| (r.stamp, *k, r.bytes())).collect(); - lru.sort_unstable_by_key(|c| c.0); - for (_, k, bytes) in lru { - if total <= self.resident_budget { - break; - } - self.residents.remove(&k); - total = total.saturating_sub(bytes); - n += 1; - freed += bytes; - } - } - let mut total = self.fx_cache_bytes(); - let fx_budget = usize::try_from(self.fx_budget).unwrap_or(usize::MAX); - if total > fx_budget { - let (frame, stamp) = (self.frame, self.stamp); - let mut lru: Vec<((u64, u64), FxKey)> = self - .fx - .iter() - .filter(|(k, e)| e.last_used != frame || (k.1.is_some() && e.stamp != stamp)) - .map(|(k, e)| ((e.last_used, e.stamp), *k)) - .collect(); - lru.sort_unstable_by_key(|c| c.0); - for (_, k) in lru { - if total <= fx_budget { - break; - } - if let Some(e) = self.fx.remove(&k) { - total = total.saturating_sub(e.bytes()); - n += 1; - freed += e.bytes() as u64; - } - } - } - (n, freed) - } - - /// Per-frame resources of `plan`: the tile area of each pass's surfaces, its LUT and pattern. - fn frame_res(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, canvas: Rect, plan: &Plan<'_>) -> FrameRes { - let grid = tile_grid(canvas); - let area = |s: &Surface| s.tile_bounds().intersect(&grid); - let areas = plan - .passes - .iter() - .map(|p| (p.tex.as_ref().map_or(Rect::EMPTY, |t| area(t.surface.get())), p.mask.as_ref().map_or(Rect::EMPTY, |m| area(m.surface.get())))) - .collect(); - let luts = plan.passes.iter().map(|p| p.lut.as_ref().map(|rows| lut_texture(device, queue, rows))).collect(); - let patterns = plan.passes.iter().map(|p| p.pattern.map(|pat| self.pattern_view(device, queue, pat))).collect(); - FrameRes { areas, luts, patterns } - } - - /// Bring the pages of `cell` that `plan` samples up to date and resolve its effect maps. - #[allow(clippy::too_many_arguments)] - fn bind_cell( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: DocId, - plan: &Plan<'_>, - paged: &[bool], - cell: Cell, - fr: &FrameRes, - first_pass: usize, - stats: &mut Stats, - ) -> Bound { - let cr = cell_rect(cell, self.page); - let mut views: Vec<(Option, Option)> = Vec::with_capacity(plan.passes.len()); - let mut keys: Vec = Vec::new(); - let mut maps = Vec::with_capacity(plan.passes.len()); - for (i, p) in plan.passes.iter().enumerate() { - if i < first_pass { - views.push((None, None)); - maps.push(None); - continue; - } - let (ta, ma) = fr.areas.get(i).copied().unwrap_or((Rect::EMPTY, Rect::EMPTY)); - let tex = - p.tex.as_ref().and_then(|t| self.sync(device, queue, encoder, doc, (t.layer, t.role, cell), t.surface.get(), ta.intersect(&cr), stats)).map( - |(k, r)| { - keys.push(k); - (keys.len() - 1, r) - }, - ); - let mask = p - .mask - .as_ref() - .and_then(|m| self.sync(device, queue, encoder, doc, (m.layer, Role::Mask, cell), m.surface.get(), ma.intersect(&cr), stats)) - .map(|(k, r)| { - keys.push(k); - (keys.len() - 1, r) - }); - views.push((tex, mask)); - maps.push(p.map.and_then(|m| { - let f = plan.fx.get(m.fx)?; - let key = (f.layer.id, paged.get(m.fx).copied().unwrap_or(false).then_some(cell)); - let e = self.fx.get(&key)?; - if m.item == plan::SHAPE_MAP { - return Some((e.shape.view.clone(), e.region)); - } - let t = e.progs.get(m.item)?.maps.get(m.map)?.as_ref()?; - Some((t.view.clone(), e.region)) - })); - } - Bound { views, keys, maps } - } - - /// Run `plan` over the `chunks` of one page cell (document rects, at most a chunk square), - /// handing each finished chunk to `sink`. - #[allow(clippy::too_many_arguments)] - fn run_cell( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: DocId, - plan: &Plan<'_>, - paged: &[bool], - cell: Cell, - fr: &FrameRes, - chunks: &[Rect], - checkpoint: Option, - stats: &mut Stats, - sink: &mut dyn FnMut(&mut wgpu::CommandEncoder, ChunkOut<'_>), - // The previous intermediate submit, when this refresh may submit in between. - batch: &mut Option<&mut Option>, - ) { - if chunks.is_empty() { - return; - } - // Chunks may hold different checkpoints: bind from the lowest one any of them resumes at. - let first_pass = checkpoint - .and_then(|_| chunks.iter().map(|c| self.prefix.restore_point(*c).map(|r| r.pass_end)).try_fold(usize::MAX, |m, p| Some(m.min(p?)))) - .unwrap_or(0); - let bound = self.bind_cell(device, queue, encoder, doc, plan, paged, cell, fr, first_pass, stats); - - // Chunk pool. - while self.pool.len() < plan.slots as usize { - let t = device.create_texture(&wgpu::TextureDescriptor { - label: Some("pc_compose_chunk"), - size: wgpu::Extent3d { width: self.chunk, height: self.chunk, depth_or_array_layers: 1 }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: self.acc_format, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING - | wgpu::TextureUsages::COPY_SRC - | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - let v = t.create_view(&Default::default()); - self.pool.push((t, v)); - } - - // Uniforms: chunk records, then one record per pass. encode_scoped preflight checked - // the length, device capacity and dynamic offsets before any GPU work for this frame. - let op_base = chunks.len() as u64 * STRIDE; - let mut data = vec![0u8; (op_base + plan.passes.len() as u64 * STRIDE) as usize]; - for (i, c) in chunks.iter().enumerate() { - let w = words(&[I(c.x0), I(c.y0), I(c.width() as i32), I(c.height() as i32)]); - data[i * STRIDE as usize..][..w.len()].copy_from_slice(&w); - } - for (i, p) in plan.passes.iter().enumerate() { - if i < first_pass { - continue; - } - let (tex, mask) = bound.views[i]; - let map = bound.maps[i].as_ref().map(|(_, r)| *r); - let w = op_words(p, tex.map(|t| t.1), mask.map(|m| m.1), map); - let off = (op_base + i as u64 * STRIDE) as usize; - data[off..][..w.len()].copy_from_slice(&w); - } - let ubuf = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("pc_compose_uniforms"), - size: data.len() as u64, - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, - mapped_at_creation: false, - }); - queue.write_buffer(&ubuf, 0, &data); - let bg0 = uniform_group(device, &self.kit.bgl0, &ubuf); - - // Per-pass texture bind groups (chunk-independent: pool slots are reused per chunk). - let dummy = &self.kit.dummy; - let resident_views: Vec<&wgpu::TextureView> = bound.keys.iter().map(|k| self.residents.get(k).map_or(dummy, |r| &r.view)).collect(); - let mut bg1 = Vec::with_capacity(plan.passes.len()); - for (i, p) in plan.passes.iter().enumerate() { - if i < first_pass || p.kernel.entry().is_none() { - bg1.push(None); - continue; - } - let slot = |s: Option| s.map_or(dummy, |s| &self.pool[s as usize].1); - let (tex, mask) = &bound.views[i]; - let tv = tex.map_or(slot(p.d), |(k, _)| resident_views[k]); - let mv = mask.map_or(dummy, |(k, _)| resident_views[k]); - let lv = fr.luts.get(i).and_then(Option::as_ref).map_or(dummy, |(_, v)| v); - let map = bound.maps[i].as_ref().map_or(dummy, |(v, _)| v); - let pat = fr.patterns.get(i).and_then(Option::as_ref).unwrap_or(dummy); - bg1.push(Some(texture_group(device, &self.kit.bgl1, &[slot(p.a), slot(p.b), tv, mv, lv, slot(p.c), map, pat]))); - } - - for (ci, c) in chunks.iter().enumerate() { - let mut resume = 0; - if let Some(cp) = checkpoint.and_then(|_| self.prefix.restore_point(*c)) - && let Some((dst, _)) = self.pool.get(cp.slot as usize) - && self.prefix.copy_out(encoder, *c, dst) - { - resume = cp.pass_end; - stats.prefix_hits = stats.prefix_hits.saturating_add(1); - stats.prefix_passes_skipped = stats.prefix_passes_skipped.saturating_add(resume); - } - for (i, p) in plan.passes.iter().enumerate() { - if i < resume { - continue; - } - // Chunk textures, uniforms and bind groups outlive the submit, and the queue - // keeps the order, so a chunk may continue in the next command buffer. - if self.recorded >= FLUSH_OPS - && let Some(last) = batch.as_deref_mut() - { - submit_batch(device, queue, encoder, self.health.as_ref(), last); - self.staged = 0; - self.recorded = 0; - stats.flushes += 1; - if self.fault().is_some() { - return; - } - } - // Also after resuming from a lower checkpoint: the chunk moves up to this one. - if resume < i - && let Some(cp) = checkpoint.filter(|cp| cp.pass_end == i) - && let Some((src, _)) = self.pool.get(cp.slot as usize) - { - self.prefix.capture(device, encoder, *c, src, cp); - } - let scissor = match p.clip { - Some(clip) => { - let r = clip.intersect(c); - if r.is_empty() { - continue; - } - Rect::new(r.x0 - c.x0, r.y0 - c.y0, r.x1 - c.x0, r.y1 - c.y0) - } - None => Rect::new(0, 0, c.width() as i32, c.height() as i32), - }; - match p.kernel { - Kernel::CopyRect | Kernel::CopyFull => { - // The planner always gives copies a source. - let Some(a) = p.a else { continue }; - let src = &self.pool[a as usize].0; - let dst = &self.pool[p.dst as usize].0; - encoder.copy_texture_to_texture( - wgpu::TexelCopyTextureInfo { - texture: src, - mip_level: 0, - origin: wgpu::Origin3d { x: scissor.x0 as u32, y: scissor.y0 as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - wgpu::TexelCopyTextureInfo { - texture: dst, - mip_level: 0, - origin: wgpu::Origin3d { x: scissor.x0 as u32, y: scissor.y0 as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - wgpu::Extent3d { width: scissor.width(), height: scissor.height(), depth_or_array_layers: 1 }, - ); - self.recorded += 1; - continue; - } - _ => {} - } - self.recorded += 1; - let target = &self.pool[p.dst as usize].1; - let clear = p.kernel == Kernel::Clear; - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("pc_compose_pass"), - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: target, - resolve_target: None, - depth_slice: None, - ops: wgpu::Operations { - load: if clear { wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT) } else { wgpu::LoadOp::Load }, - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - multiview_mask: None, - }); - if clear { - continue; - } - pass.set_pipeline(&self.kit.pipelines[&(p.kernel, self.acc_format)]); - pass.set_scissor_rect(scissor.x0 as u32, scissor.y0 as u32, scissor.width(), scissor.height()); - pass.set_bind_group(0, &bg0, &[(ci as u64 * STRIDE) as u32, (op_base + i as u64 * STRIDE) as u32]); - pass.set_bind_group(1, bg1[i].as_ref(), &[]); - pass.draw(0..3, 0..1); - } - if resume < plan.passes.len() - && let Some(cp) = checkpoint.filter(|cp| cp.pass_end == plan.passes.len()) - && let Some((src, _)) = self.pool.get(cp.slot as usize) - { - self.prefix.capture(device, encoder, *c, src, cp); - } - let (t, v) = &self.pool[plan.root as usize]; - sink(encoder, ChunkOut { rect: *c, texture: t, view: v }); - } - } - - /// Upload changed tiles of `surface` within `region` (its tile area inside one page cell); - /// returns the resident key and its region (x, y, w, h). - #[allow(clippy::too_many_arguments)] - fn sync( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: DocId, - key: ResKey, - surface: &Surface, - region: Rect, - stats: &mut Stats, - ) -> Option<(ResKey, [i32; 4])> { - if region.is_empty() { - return None; - } - let format = surface.format(); - let kind = TexKind::for_surface(key.1, format); - let cmyk = if format.mode == photocraft_color::ColorMode::Cmyk { self.cmyk } else { 0 }; - let default_pixel = surface.default_pixel(); - let default_bits: Vec = default_pixel.iter().map(|v| v.to_bits()).collect(); - let stale = self - .residents - .get(&key) - .is_none_or(|r| r.region != region || r.kind != kind || r.format != format || r.cmyk != cmyk || r.default_bits != default_bits); - if stale { - let texture = device.create_texture(&wgpu::TextureDescriptor { - label: Some("pc_compose_layer"), - size: wgpu::Extent3d { width: region.width(), height: region.height(), depth_or_array_layers: 1 }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: kind.format(), - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC, - view_formats: &[], - }); - let view = texture.create_view(&Default::default()); - let default_nonzero = default_pixel.iter().any(|v| *v != 0.0); - let mut r = Resident { - texture, - view, - region, - kind, - format, - tiles: HashMap::new(), - default_nonzero, - default_bits: default_bits.clone(), - doc, - last_used: 0, - stamp: 0, - cmyk, - ordered: false, - }; - // Only the region moved (painting past a layer's tiles grows it): keep the texels the - // old texture already holds instead of converting and uploading every tile again. - let kept = self.residents.remove(&key).map_or(Rect::EMPTY, |old| keep_overlap(device, queue, &old, &mut r)); - // A new layer whose tiles another resident already holds (a duplicate shares its - // source's tiles) starts as a GPU copy of that texture. Duplicating a 20 MP photo - // converted and uploaded every tile again. - let shares = - |o: &Resident| region.tiles().filter(|c| surface.tile(*c).is_some_and(|t| o.tiles.get(c).is_some_and(|ot| Arc::ptr_eq(ot, t)))).count(); - let donor = self - .residents - .iter() - .filter(|(k, o)| k.1 == key.1 && o.region == region && o.kind == kind && o.format == format && o.cmyk == cmyk && o.default_bits == default_bits) - .map(|(k, o)| (shares(o), *k)) - .filter(|(n, _)| *n > 0) - .max_by_key(|(n, _)| *n); - if kept.is_empty() - && let Some((n, o)) = donor.and_then(|(n, k)| Some((n, self.residents.get_mut(&k)?))) - { - // Recorded in the frame's encoder, after the writes and copies that brought the - // donor up to date ([`keep_overlap`]'s own submission would run before those). - o.ordered = true; - encoder.copy_texture_to_texture( - o.texture.as_image_copy(), - r.texture.as_image_copy(), - wgpu::Extent3d { width: region.width(), height: region.height(), depth_or_array_layers: 1 }, - ); - r.tiles = o.tiles.clone(); - r.ordered = true; - stats.tiles_shared += n; - } else { - // A new texture starts zeroed: write the tiles that are present, and the default - // pixel where they are missing if it isn't zero. Coordinates sharing a tile (a - // solid fill, the default) take one upload and GPU copies (#1774). Kept tiles are - // brought up to date below, like those of a resident that didn't move. - let mut present = Vec::new(); - let mut missing = Vec::new(); - for c in region.tiles().filter(|c| !kept.contains_rect(&c.rect())) { - match surface.tile(c) { - Some(t) => present.push((c, t.clone())), - None if default_nonzero => missing.push(c), - None => {} - } - } - if !missing.is_empty() { - let bytes = convert_tile(surface, None, kind, TileCoord::new(0, 0)); - self.staged += put_tiles(device, queue, encoder, &r, &missing, &bytes); - } - let staged = &mut self.staged; - convert_tiles(surface, present, kind, |t, cs, bytes| { - *staged += put_tiles(device, queue, encoder, &r, cs, bytes); - for &c in cs { - stats.tiles_uploaded += 1; - stats.bytes_uploaded += bytes.len(); - r.tiles.insert(c, t.clone()); - } - }); - } - // Tiles the donor held that this surface doesn't, or holds differently, are fixed up - // by the pass over the region below. - self.residents.insert(key, r); - } - // Present: inserted above when it was missing or stale. - let r = self.residents.get_mut(&key)?; - r.last_used = self.frame; - r.stamp = self.stamp; - r.doc = doc; - let mut changed = Vec::new(); - let mut gone = Vec::new(); - for c in region.tiles() { - match surface.tile(c) { - Some(t) => { - if !r.tiles.get(&c).is_some_and(|old| Arc::ptr_eq(old, t)) { - changed.push((c, t.clone())); - } - } - None => { - if r.tiles.remove(&c).is_some() { - gone.push(c); - } - } - } - } - if !gone.is_empty() { - // A tile that went away reads as the default pixel again. - let bytes = if r.default_nonzero { - convert_tile(surface, None, kind, TileCoord::new(0, 0)) - } else { - vec![0u8; (TILE_SIZE * TILE_SIZE) as usize * kind.bytes_per_pixel()] - }; - self.staged += put_tiles(device, queue, encoder, r, &gone, &bytes); - } - let staged = &mut self.staged; - convert_tiles(surface, changed, kind, |t, cs, bytes| { - *staged += put_tiles(device, queue, encoder, r, cs, bytes); - for &c in cs { - stats.tiles_uploaded += 1; - stats.bytes_uploaded += bytes.len(); - r.tiles.insert(c, t.clone()); - } - }); - Some((key, [region.x0, region.y0, region.width() as i32, region.height() as i32])) - } - - /// Premultiplied RGBA32F texture of a pattern as it paints in the document's mode (cached by - /// pixel identity and mode). - fn pattern_view(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, pat: &Pattern) -> wgpu::TextureView { - let fp = pat.surface.tiles().fold((pat.width as u64) << 32 | pat.height as u64, |acc, (c, t)| { - acc.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ (Arc::as_ptr(t) as usize as u64) ^ ((c.tx as u64) << 20) ^ c.ty as u64 - }); - let mode = self.pattern_mode; - let key = format!("{}\u{0}{}\u{0}{mode:?}", pat.id, pat.name); - let format = pat.surface.format(); - // Unstored tiles read the default pixel, interpreted in the source format. - let default_bits: Vec = pat.surface.default_pixel().iter().map(|v| v.to_bits()).collect(); - let (doc, last_used) = (self.pattern_doc, self.frame); - if let Some(cached) = self.patterns.get_mut(&key) - && cached.fingerprint == fp - && cached.format == format - && cached.default_bits == default_bits - { - cached.doc = doc; - cached.last_used = last_used; - return cached.texture.view.clone(); - } - let (w, h) = (pat.width, pat.height); - let px = photocraft_compose::pattern::pattern_rgba(pat, mode); - let bytes: Vec = px.iter().flat_map(|q| [q[0] * q[3], q[1] * q[3], q[2] * q[3], q[3]]).flat_map(f32::to_le_bytes).collect(); - let t = Tex::new(device, "pc_pattern", w, h, wgpu::TextureFormat::Rgba32Float, wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST); - queue.write_texture( - wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, - &bytes, - wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(w * 16), rows_per_image: Some(h) }, - wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, - ); - let v = t.view.clone(); - let bytes = u64::from(w.max(1)) * u64::from(h.max(1)) * 16; - self.patterns.insert(key, CachedPattern { fingerprint: fp, format, default_bits, texture: t, doc, last_used, bytes }); - v - } - - /// An R32F temporary at least `region`-sized (the smallest that fits) not in `taken`. - /// Stages only read what they wrote, inside the region, so a larger one is as good; page - /// windows of different sizes share them. - fn temp(&mut self, device: &wgpu::Device, region: Rect, taken: &[wgpu::TextureView]) -> wgpu::TextureView { - let (w, h) = (region.width(), region.height()); - let frame = self.frame; - let fit = self - .temps - .iter_mut() - .filter(|(t, _)| { - let s = t.texture.size(); - s.width >= w && s.height >= h && !taken.contains(&t.view) - }) - .min_by_key(|(t, _)| t.texture.size().width as u64 * t.texture.size().height as u64); - if let Some((t, used)) = fit { - *used = frame; - return t.view.clone(); - } - let t = Tex::map(device, "pc_fx_temp", region, MAP32); - let v = t.view.clone(); - self.temps.push((t, frame)); - v - } - - /// Build or update the effect maps of one layer over `region`: its whole effect region, or - /// the window of one page cell (`key.1`) grown by the effects' reach. - #[allow(clippy::too_many_arguments)] - fn sync_fx( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - doc: &Document, - f: &plan::FxLayer<'_>, - key: FxKey, - region: Rect, - stats: &mut Stats, - ) { - let layer = f.layer; - let canvas = doc.bounds(); - let is_group = matches!(layer.content, LayerContent::Group(_)); - let shape_key = if is_group { fx::group_key(layer, &doc.global_light, &mut |_| {}) } else { fx::shape_key(layer, canvas) }; - let (content_src, mask_src) = if is_group { (None, None) } else { fx::shape_sources(layer) }; - let frame = self.frame; - let (rw, rh) = (region.width(), region.height()); - - // What changed: everything (new settings / size), the position only, some tiles, or - // nothing. - let mut damage = Rect::EMPTY; - let prev = self.fx.get(&key).filter(|e| e.shape_key == shape_key).map(|e| e.region); - let mut rebuild = true; - if prev == Some(region) - && let Some(e) = self.fx.get(&key) - { - rebuild = false; - for (old, cur) in e.tiles.iter().zip([content_src, mask_src]) { - let d = match (old, cur) { - (Some(o), Some(s)) => fx::damage(o, s), - (None, None) => Rect::EMPTY, - // A source appeared or vanished (the key changes with it too). - _ => region, - }; - damage = if damage.is_empty() { d } else { damage.union(&d) }; - } - damage = damage.intersect(®ion); - } else if let Some(old) = prev - && (old.width(), old.height()) == (rw, rh) - && !region.is_empty() - { - // Moved by whole pixels: maps are computed relative to the region, so if the shape - // moved unchanged with it, every map is still exact. - let v = fx::shape(doc, layer, region); - if let Some(e) = self.fx.get_mut(&key) - && v == e.shape_cpu - { - e.region = region; - rebuild = false; - } - } - if rebuild { - let shape = Tex::map(device, "pc_fx_shape", region, MAP32); - let n = rw as usize * rh as usize; - self.fx.insert( - key, - FxEntry { - doc: doc.id, - region, - shape_key, - tiles: [None, None], - _pin: if is_group { - let mut pin = Vec::new(); - fx::group_key(layer, &doc.global_light, &mut |t| pin.push(Arc::downgrade(t))); - pin - } else { - Vec::new() - }, - shape_cpu: vec![0.0; n], - shape, - fields: HashMap::new(), - progs: Vec::new(), - last_used: frame, - stamp: 0, - }, - ); - damage = region; - } - // Present: kept or (re)inserted above. - let stamp = self.stamp; - let Some(e) = self.fx.get_mut(&key) else { return }; - e.last_used = frame; - e.stamp = stamp; - e.doc = doc.id; - e.tiles = [content_src.map(fx::snapshot), mask_src.map(fx::snapshot)]; - if region.is_empty() { - return; - } - - let t_trace = web_time_now(); - let mut staged = 0u64; - // Shape over the damage (compose's own alpha). - if !damage.is_empty() { - stats.fx_shapes += 1; - let v = fx::shape(doc, layer, damage); - fx::paste(&mut e.shape_cpu, region, damage, &v); - e.shape.write_r32(queue, region, damage, &v); - staged += v.len() as u64 * 4; - } - - // Programs, and the distance fields they read (max reach per field). - // Filled shapes stroke their outline (the effect shape); others estimate it. - let vector_shape = matches!(layer.content, LayerContent::Shape(_)) && photocraft_compose::effect_outline(layer).is_none(); - let anchor = layer.effects.reference.unwrap_or((f64::from(f.bounds.x0), f64::from(f.bounds.y0))); - let progs: Vec = - layer.effects.items.iter().filter(|e| e.enabled()).map(|e| fx::program_with(e, &doc.global_light, vector_shape, &doc.patterns, anchor)).collect(); - let want = field_reaches(&progs); - e.fields.retain(|k, _| want.contains_key(k)); - for (&kind, &reach) in &want { - let have = e.fields.get(&kind).map(|(r, _)| *r); - let out = match have { - Some(r) if r >= reach => { - if damage.is_empty() { - continue; - } - damage.inflate(fx::field_radius(kind, r)).intersect(®ion) - } - _ => region, - }; - let reach = have.filter(|r| *r >= reach).unwrap_or(reach); - let v = fx::field(kind, reach, &e.shape_cpu, region, out); - if have.is_none_or(|r| r < reach) { - e.fields.insert(kind, (reach, Tex::map(device, "pc_fx_field", region, MAP32))); - } - if let Some((_, t)) = e.fields.get(&kind) { - t.write_r32(queue, region, out, &v); - } - staged += v.len() as u64 * 4; - stats.fx_pixels += out.width() as u64 * out.height() as u64; - } - - if let Some(t) = t_trace.filter(|_| !damage.is_empty()) { - eprintln!("effect maps of `{}`: shape + distance fields over {damage:?} in {:.2} ms (CPU)", layer.name, t.elapsed().as_secs_f64() * 1000.0); - } - e.progs.truncate(progs.len()); - while e.progs.len() < progs.len() { - e.progs.push(ProgState { key: 0, maps: Vec::new() }); - } - self.staged += staged; - for (i, prog) in progs.iter().enumerate() { - if prog.maps == 0 { - continue; - } - let Some(e) = self.fx.get_mut(&key) else { return }; - let same = e.progs[i].key == prog.key && e.progs[i].maps.len() == prog.maps; - let d = if same { damage } else { region }; - if d.is_empty() { - continue; - } - if !same { - e.progs[i] = ProgState { - key: prog.key, - maps: (0..prog.maps).map(|k| prog.stages.iter().any(|s| s.out == Some(k)).then(|| Tex::map(device, "pc_fx_map", region, MAP16))).collect(), - }; - } - stats.fx_programs += 1; - self.run_program(device, queue, encoder, key, i, prog, d, stats); - } - } - - /// Record the passes of `prog` (maps of item `item` of `layer`) recomputing damage `d`. - #[allow(clippy::too_many_arguments)] - fn run_program( - &mut self, - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - key: FxKey, - item: usize, - prog: &fx::MapProgram, - d: Rect, - stats: &mut Stats, - ) { - let pattern_views: Vec> = - prog.stages.iter().map(|s| s.pattern.as_ref().map(|p| self.pattern_view(device, queue, p))).collect(); - let Some(e) = self.fx.get(&key) else { return }; - let region = e.region; - let shape = e.shape.view.clone(); - let finals: Vec> = e.progs[item].maps.iter().map(|m| m.as_ref().map(|t| t.view.clone())).collect(); - let fields: HashMap = e.fields.iter().map(|(k, (r, t))| (*k, (*r, t.view.clone()))).collect(); - let windows = prog.windows(d, region, &|k| fields.get(&k).map_or(0, |f| f.0)); - let (assign, count) = prog.temps(); - let mut temps: Vec = Vec::with_capacity(count); - for _ in 0..count { - let v = self.temp(device, region, &temps); - temps.push(v); - } - // `None` only if a program's map or temporary wasn't allocated, which `sync_fx` and - // `MapProgram::temps` rule out. - let target_of = |i: usize| -> Option<(wgpu::TextureView, wgpu::TextureFormat)> { - match prog.stages[i].out { - Some(k) => Some((finals.get(k)?.clone()?, MAP16)), - None => Some((temps.get((*assign.get(i)?)?)?.clone(), MAP32)), - } - }; - let view_of = |inp: Option| -> Option { - match inp? { - fx::In::Shape => Some(shape.clone()), - fx::In::Field(k) => fields.get(&k).map(|f| f.1.clone()), - fx::In::Val(k) => target_of(k).map(|t| t.0), - } - }; - let mut draws = Vec::with_capacity(prog.stages.len()); - for (i, s) in prog.stages.iter().enumerate() { - let w = windows[i]; - if w.is_empty() { - continue; - } - stats.fx_pixels += w.width() as u64 * w.height() as u64; - let Some((target, format)) = target_of(i) else { continue }; - let mut p = plan::Pass::new(s.kernel, 0); - p.params[0] = s.p0; - p.params[1] = s.p1; - p.params[3] = s.p3; - p.extra = s.p4; - draws.push(MapDraw { - kernel: s.kernel, - format, - target, - scissor: [(w.x0 - region.x0) as u32, (w.y0 - region.y0) as u32, w.width(), w.height()], - space: [region.x0, region.y0, region.width() as i32, region.height() as i32], - op: op_words(&p, None, None, None), - views: [view_of(s.a), view_of(s.b), view_of(s.s), None, None, None, None, pattern_views[i].clone()], - lut: s.lut.clone(), - }); - } - self.kit.run(device, queue, encoder, &draws); - } -} - -/// A start time when `PHOTOCRAFT_FX_TRACE=1` (it prints the CPU time of effect shapes and -/// distance fields); never on wasm. -fn web_time_now() -> Option { - #[cfg(not(target_arch = "wasm32"))] - { - static T: std::sync::OnceLock = std::sync::OnceLock::new(); - T.get_or_init(|| std::env::var_os("PHOTOCRAFT_FX_TRACE").is_some()).then(std::time::Instant::now) - } - #[cfg(target_arch = "wasm32")] - None -} - -/// The uniform record of a pass. -fn op_words(p: &plan::Pass<'_>, tex: Option<[i32; 4]>, mask: Option<[i32; 4]>, map: Option) -> Vec { - let mut flags = p.flags; - let (mut to, mut ts, mut mo, mut ms) = ([0; 2], [0; 2], [0; 2], [0; 2]); - if let Some(r) = tex { - flags |= 4; - to = [r[0], r[1]]; - ts = [r[2], r[3]]; - } - let (mut density, mut mdefault) = (0.0, 1.0); - if let Some(m) = &p.mask { - flags |= 1; - density = m.density; - mdefault = m.default; - if let Some(r) = mask { - flags |= 2; - mo = [r[0], r[1]]; - ms = [r[2], r[3]]; - } - } - if p.gradient { - flags |= 8; - } - let (mpo, mps) = map.map_or(([0, 0], [0, 0]), |r| ([r.x0, r.y0], [r.width() as i32, r.height() as i32])); - let mut v = vec![ - I(plan::mode_index(p.mode)), - I(p.adjust_kind), - U(flags), - U(0), - F(p.opacity), - F(density), - F(mdefault), - F(p.fill), - I(to[0]), - I(to[1]), - I(ts[0]), - I(ts[1]), - I(mo[0]), - I(mo[1]), - I(ms[0]), - I(ms[1]), - ]; - v.extend(p.color.iter().map(|f| F(*f))); - for row in &p.params { - v.extend(row.iter().map(|f| F(*f))); - } - v.extend([I(mpo[0]), I(mpo[1]), I(mps[0]), I(mps[1])]); - v.extend(p.extra.iter().map(|f| F(*f))); - words(&v) -} - -/// Canvas rounded out to whole tiles. -fn tile_grid(canvas: Rect) -> Rect { - let t = TILE_SIZE; - Rect::new(canvas.x0.div_euclid(t) * t, canvas.y0.div_euclid(t) * t, (canvas.x1 + t - 1).div_euclid(t) * t, (canvas.y1 + t - 1).div_euclid(t) * t) -} - -fn write_tile(queue: &wgpu::Queue, r: &Resident, c: TileCoord, bytes: &[u8]) { - let tr = c.rect(); - let bpp = r.kind.bytes_per_pixel() as u32; - queue.write_texture( - wgpu::TexelCopyTextureInfo { - texture: &r.texture, - mip_level: 0, - origin: wgpu::Origin3d { x: (tr.x0 - r.region.x0) as u32, y: (tr.y0 - r.region.y0) as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - bytes, - wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(TILE_SIZE as u32 * bpp), rows_per_image: Some(TILE_SIZE as u32) }, - wgpu::Extent3d { width: TILE_SIZE as u32, height: TILE_SIZE as u32, depth_or_array_layers: 1 }, - ); -} - -/// Submit what `encoder` recorded and continue in a fresh one, then wait for the previous -/// intermediate submit (`last`), so at most two batches of uploads and evicted pages are in -/// flight. The web has no blocking wait and relies on the browser's own scheduling. -fn submit_batch( - device: &wgpu::Device, - queue: &wgpu::Queue, - encoder: &mut wgpu::CommandEncoder, - health: Option<&DeviceHealth>, - last: &mut Option, -) { - let done = std::mem::replace(encoder, device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose") })); - let prev = last.replace(queue.submit([done.finish()])); - #[cfg(not(target_arch = "wasm32"))] - if let Some(prev) = prev { - match health { - Some(h) => { - h.wait(device, Some(prev)); - } - None => { - let _ = device.poll(wgpu::PollType::Wait { submission_index: Some(prev), timeout: None }); - } - } - } - #[cfg(target_arch = "wasm32")] - let _ = (prev, health); -} - -/// Distinct tiles converted per parallel batch: bounds the converted bytes held at once (64 tiles -/// of a 16-bit page are 32 MiB). -const CONVERT_BATCH: usize = 64; - -/// Write the same tile `bytes` at every coordinate in `cs` of resident `r`; returns the bytes -/// staged. Several coordinates (a solid fill or a cleared area shares one tile) get one upload to -/// a scratch tile and GPU copies from it: writing each one from the CPU sent 387 MB per refresh -/// of a filled 50 MP 16-bit page (#1774). -fn put_tiles(device: &wgpu::Device, queue: &wgpu::Queue, encoder: &mut wgpu::CommandEncoder, r: &Resident, cs: &[TileCoord], bytes: &[u8]) -> u64 { - if let [c] = cs - && !r.ordered - { - write_tile(queue, r, *c, bytes); - return bytes.len() as u64; - } - if cs.is_empty() { - return 0; - } - let extent = wgpu::Extent3d { width: TILE_SIZE as u32, height: TILE_SIZE as u32, depth_or_array_layers: 1 }; - // Its own texture per call: queued writes land before the encoder runs, so a shared scratch - // would hold only the last tile by then. - let scratch = device.create_texture(&wgpu::TextureDescriptor { - label: Some("pc_tile_scratch"), - size: extent, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: r.kind.format(), - usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - queue.write_texture( - wgpu::TexelCopyTextureInfo { texture: &scratch, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, - bytes, - wgpu::TexelCopyBufferLayout { - offset: 0, - bytes_per_row: Some(TILE_SIZE as u32 * r.kind.bytes_per_pixel() as u32), - rows_per_image: Some(TILE_SIZE as u32), - }, - extent, - ); - for c in cs { - let tr = c.rect(); - encoder.copy_texture_to_texture( - wgpu::TexelCopyTextureInfo { texture: &scratch, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, - wgpu::TexelCopyTextureInfo { - texture: &r.texture, - mip_level: 0, - origin: wgpu::Origin3d { x: (tr.x0 - r.region.x0) as u32, y: (tr.y0 - r.region.y0) as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - extent, - ); - } - bytes.len() as u64 -} - -/// Copy the texels `old` holds inside the region of its replacement `new` (same texel kind, -/// format and default pixel; only the region moved) and carry over the tiles they show, so only -/// tiles outside the overlap or changed since need uploading. Returns the kept (tile-aligned) -/// rect; empty when nothing can be kept. -fn keep_overlap(device: &wgpu::Device, queue: &wgpu::Queue, old: &Resident, new: &mut Resident) -> Rect { - let same = old.kind == new.kind && old.format == new.format && old.cmyk == new.cmyk && old.default_bits == new.default_bits; - let k = old.region.intersect(&new.region); - if !same || k.is_empty() { - return Rect::EMPTY; - } - // Its own submission: tile writes go through the queue and land at the start of the next - // submit, before anything in the frame's encoder, so a copy recorded there would overwrite - // the tiles written after it with the old texels. - let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_compose_keep") }); - encoder.copy_texture_to_texture( - wgpu::TexelCopyTextureInfo { - texture: &old.texture, - mip_level: 0, - origin: wgpu::Origin3d { x: (k.x0 - old.region.x0) as u32, y: (k.y0 - old.region.y0) as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - wgpu::TexelCopyTextureInfo { - texture: &new.texture, - mip_level: 0, - origin: wgpu::Origin3d { x: (k.x0 - new.region.x0) as u32, y: (k.y0 - new.region.y0) as u32, z: 0 }, - aspect: wgpu::TextureAspect::All, - }, - wgpu::Extent3d { width: k.width(), height: k.height(), depth_or_array_layers: 1 }, - ); - queue.submit([encoder.finish()]); - new.tiles = old.tiles.iter().filter(|(c, _)| k.contains_rect(&c.rect())).map(|(c, t)| (*c, t.clone())).collect(); - k -} - -/// Convert `tiles` of `surface` to `kind` texels and hand each distinct tile, the coordinates -/// sharing it and its bytes to `put`. Each distinct tile is converted once (a solid fill shares -/// one `Arc` across every coordinate it covers), in parallel batches. Converting every tile one -/// after another on the UI thread froze a 50 MP 16-bit document for about 1.4 s after a -/// full-canvas fill (#1774). Returns how many tiles were converted. -fn convert_tiles(surface: &Surface, tiles: Vec<(TileCoord, Arc)>, kind: TexKind, mut put: impl FnMut(&Arc, &[TileCoord], &[u8])) -> usize { - let mut groups: Vec<(Arc, Vec)> = Vec::new(); - let mut index: HashMap<*const Tile, usize> = HashMap::new(); - for (c, t) in tiles { - match index.get(&Arc::as_ptr(&t)).and_then(|&i| groups.get_mut(i)) { - Some(g) => g.1.push(c), - None => { - index.insert(Arc::as_ptr(&t), groups.len()); - groups.push((t, vec![c])); - } - } - } - for batch in groups.chunks(CONVERT_BATCH) { - // A tile's content is the same at every coordinate sharing it: convert it at the first. - let jobs: Vec<_> = batch.iter().filter_map(|(t, cs)| cs.first().map(|c| (t, *c))).collect(); - let converted = fx::par_map(jobs, |(t, c)| convert_tile(surface, Some(t), kind, c)); - for ((t, cs), bytes) in batch.iter().zip(&converted) { - put(t, cs, bytes); - } - } - groups.len() -} - -/// Tile pixels in the texture's format. `tile = None` gives the default pixel everywhere. -fn convert_tile(surface: &Surface, tile: Option<&Arc>, kind: TexKind, c: TileCoord) -> Vec { - if let (Some(t), TexKind::Rgba8Direct | TexKind::R8Direct) = (tile, kind) { - return t.bytes().to_vec(); - } - let n = (TILE_SIZE * TILE_SIZE) as usize; - let fmt = surface.format(); - let ch = fmt.channels(); - if let (Some(t), TexKind::Rgba16F) = (tile, kind) - && let Some(out) = rgb_gray16_to_f16(&fmt, t.bytes()).or_else(|| rgb_gray32_to_f16(&fmt, t.bytes())) - { - return out; - } - let raw: Vec = match tile { - Some(_) => surface.read_region(c.rect()), - None => surface.default_pixel().repeat(n), - }; - let mut out = Vec::with_capacity(n * kind.bytes_per_pixel()); - for px in raw.chunks_exact(ch) { - match kind { - TexKind::Rgba8Direct | TexKind::Rgba8 => { - let v = photocraft_raster::to_rgba(&fmt, px); - out.extend(v.map(|x| (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8)); - } - TexKind::Rgba16F => { - let v = photocraft_raster::to_rgba(&fmt, px); - for x in v { - out.extend(f32_to_f16(x).to_le_bytes()); - } - } - TexKind::R8Direct => out.push((px[0].clamp(0.0, 1.0) * 255.0 + 0.5) as u8), - TexKind::R32F => out.extend(px[0].to_le_bytes()), - } - } - out -} - -/// RGBA16F texels of a 16-bit RGB or grayscale tile, through a table of every 16-bit sample's -/// half: the same bytes as the general path at a fraction of its cost (a 50 MP 16-bit refresh -/// converts dozens of tiles). `None` for other formats. -fn rgb_gray16_to_f16(fmt: &PixelFormat, bytes: &[u8]) -> Option> { - use photocraft_color::ColorMode; - static HALF: std::sync::OnceLock> = std::sync::OnceLock::new(); - if fmt.sample != SampleType::U16 { - return None; - } - let gray = match fmt.mode { - ColorMode::Rgb => false, - ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => true, - _ => return None, - }; - let half = HALF.get_or_init(|| (0..=u16::MAX).map(|v| f32_to_f16(v as f32 / 65535.0)).collect()); - let one = f32_to_f16(1.0).to_le_bytes(); - let ch = fmt.channels(); - let mut out = Vec::with_capacity((TILE_SIZE * TILE_SIZE) as usize * 8); - for px in bytes.chunks_exact(ch * 2) { - let h = |i: usize| -> [u8; 2] { - let v = px.get(i * 2..i * 2 + 2).map_or(0, |b| u16::from_ne_bytes([b[0], b[1]])); - half.get(v as usize).copied().unwrap_or(0).to_le_bytes() - }; - let (r, g, b) = if gray { (h(0), h(0), h(0)) } else { (h(0), h(1), h(2)) }; - let a = if fmt.alpha { h(ch - 1) } else { one }; - out.extend_from_slice(&r); - out.extend_from_slice(&g); - out.extend_from_slice(&b); - out.extend_from_slice(&a); - } - Some(out) -} - -/// RGBA16F texels of a 32-bit float RGB or grayscale tile, straight from its samples: the same -/// bytes as the general path without decoding the tile into a float buffer and converting it -/// pixel by pixel (a brush dab on a 32-bit layer re-uploads the tiles it touched). `None` for -/// other formats. -fn rgb_gray32_to_f16(fmt: &PixelFormat, bytes: &[u8]) -> Option> { - use photocraft_color::ColorMode; - if fmt.sample != SampleType::F32 { - return None; - } - let gray = match fmt.mode { - ColorMode::Rgb => false, - ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => true, - _ => return None, - }; - let one = f32_to_f16(1.0).to_le_bytes(); - let ch = fmt.channels(); - let mut out = Vec::with_capacity((TILE_SIZE * TILE_SIZE) as usize * 8); - for px in bytes.chunks_exact(ch * 4) { - let h = |i: usize| -> [u8; 2] { - let v = px.get(i * 4..i * 4 + 4).map_or(0.0, |b| f32::from_ne_bytes([b[0], b[1], b[2], b[3]])); - f32_to_f16(v).to_le_bytes() - }; - let (r, g, b) = if gray { - let v = h(0); - (v, v, v) - } else { - (h(0), h(1), h(2)) - }; - let a = if fmt.alpha { h(ch - 1) } else { one }; - out.extend_from_slice(&r); - out.extend_from_slice(&g); - out.extend_from_slice(&b); - out.extend_from_slice(&a); - } - Some(out) -} - -fn lut_texture(device: &wgpu::Device, queue: &wgpu::Queue, rows: &[[f32; 4096]]) -> (wgpu::Texture, wgpu::TextureView) { - let t = device.create_texture(&wgpu::TextureDescriptor { - label: Some("pc_compose_lut"), - size: wgpu::Extent3d { width: 4096, height: rows.len() as u32, depth_or_array_layers: 1 }, - mip_level_count: 1, - sample_count: 1, - dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::R32Float, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, - view_formats: &[], - }); - let bytes: Vec = rows.iter().flat_map(|r| r.iter().flat_map(|v| v.to_le_bytes())).collect(); - queue.write_texture( - wgpu::TexelCopyTextureInfo { texture: &t, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, - &bytes, - wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(4096 * 4), rows_per_image: Some(rows.len() as u32) }, - wgpu::Extent3d { width: 4096, height: rows.len() as u32, depth_or_array_layers: 1 }, - ); - let v = t.create_view(&Default::default()); - (t, v) -} - -#[derive(Clone, Copy)] -enum W { - I(i32), - U(u32), - F(f32), + /// Timestamp queries measuring how long the GPU spends on a refresh (see + /// [`Compositor::measure_gpu_time`]); `None` = not measured. + timer: Option, } use W::{F, I, U}; -fn words(v: &[W]) -> Vec { - v.iter() - .flat_map(|w| match w { - I(i) => i.to_le_bytes(), - U(u) => u.to_le_bytes(), - F(f) => f.to_le_bytes(), - }) - .collect() -} - -/// IEEE half from f32 (round to nearest even; overflow → inf). -pub fn f32_to_f16(x: f32) -> u16 { - let b = x.to_bits(); - let sign = ((b >> 16) & 0x8000) as u16; - let exp = ((b >> 23) & 0xff) as i32; - let man = b & 0x7f_ffff; - if exp == 0xff { - return sign | 0x7c00 | if man != 0 { 0x200 } else { 0 }; - } - let e = exp - 127 + 15; - if e >= 0x1f { - return sign | 0x7c00; - } - if e <= 0 { - if e < -10 { - return sign; - } - let m = man | 0x80_0000; - let shift = (14 - e) as u32; - let half = 1u32 << (shift - 1); - let rounded = (m + half - 1 + ((m >> shift) & 1)) >> shift; - return sign | rounded as u16; - } - let mut h = ((e as u32) << 10) | (man >> 13); - let rest = man & 0x1fff; - if rest > 0x1000 || (rest == 0x1000 && (h & 1) == 1) { - h += 1; - } - sign | h as u16 -} - -/// f32 from IEEE half. -pub fn f16_to_f32(h: u16) -> f32 { - let sign = if h & 0x8000 != 0 { -1.0 } else { 1.0 }; - let exp = ((h >> 10) & 0x1f) as i32; - let man = (h & 0x3ff) as f32; - match exp { - 0 => sign * man * 2f32.powi(-24), - 0x1f => { - if man == 0.0 { - sign * f32::INFINITY - } else { - f32::NAN - } - } - e => sign * (1.0 + man / 1024.0) * 2f32.powi(e - 15), - } -} - -/// Blocking readback of a composite (native tests and tools). +mod batch; +mod cells; +mod consts; +mod convert; +mod encode; +mod fx_passes; +mod kit; +mod paging; #[cfg(not(target_arch = "wasm32"))] -pub fn render_to_vec(comp: &mut Compositor, device: &wgpu::Device, queue: &wgpu::Queue, doc: &Document, rect: Rect) -> Result, Unsupported> { - render_to_vec_stats(comp, device, queue, doc, rect).map(|(v, _)| v) -} - -/// [`render_to_vec`] plus the render's [`Stats`]. -#[cfg(not(target_arch = "wasm32"))] -pub fn render_to_vec_stats( - comp: &mut Compositor, - device: &wgpu::Device, - queue: &wgpu::Queue, - doc: &Document, - rect: Rect, -) -> Result<(Vec<[f32; 4]>, Stats), Unsupported> { - let rect = rect.intersect(&doc.bounds()); - let mut staging: Vec<(Rect, wgpu::Buffer, u32)> = Vec::new(); - let mut bpp = 16u32; - let stats = comp.render(device, queue, doc, rect, |enc, out| { - // Bytes per pixel of the accumulation format (Rgba32Float = 16, Rgba16Float = 8). - bpp = if out.texture.format() == wgpu::TextureFormat::Rgba32Float { 16 } else { 8 }; - let row = (out.rect.width() * bpp).div_ceil(256) * 256; - let buf = device.create_buffer(&wgpu::BufferDescriptor { - label: Some("pc_readback"), - size: (row * out.rect.height()) as u64, - usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, - mapped_at_creation: false, - }); - enc.copy_texture_to_buffer( - wgpu::TexelCopyTextureInfo { texture: out.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, - wgpu::TexelCopyBufferInfo { - buffer: &buf, - layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: Some(out.rect.height()) }, - }, - wgpu::Extent3d { width: out.rect.width(), height: out.rect.height(), depth_or_array_layers: 1 }, - ); - staging.push((out.rect, buf, row)); - })?; - for (_, b, _) in &staging { - b.slice(..).map_async(wgpu::MapMode::Read, |_| {}); - } - match &comp.health { - Some(h) if !h.wait(device, None) => return Err(Unsupported(h.fault().map_or_else(|| "GPU device lost".into(), |f| f.to_string()))), - Some(_) => {} - None => { - let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); - } - } - let w = rect.width() as usize; - let mut out = vec![[0.0f32; 4]; w * rect.height() as usize]; - for (r, b, row) in &staging { - let data = b.slice(..).get_mapped_range().map_err(|e| Unsupported(format!("GPU readback failed: {e:?}")))?; - for y in 0..r.height() as usize { - for x in 0..r.width() as usize { - let o = y * *row as usize + x * bpp as usize; - let px: [f32; 4] = if bpp == 16 { - std::array::from_fn(|i| f32::from_le_bytes([data[o + i * 4], data[o + i * 4 + 1], data[o + i * 4 + 2], data[o + i * 4 + 3]])) - } else { - std::array::from_fn(|i| half::f16::from_le_bytes([data[o + i * 2], data[o + i * 2 + 1]]).to_f32()) - }; - let (dx, dy) = ((r.x0 - rect.x0) as usize + x, (r.y0 - rect.y0) as usize + y); - out[dy * w + dx] = px; - } - } - } - Ok((out, stats)) -} +mod readback; +mod setup; +mod textures; #[cfg(test)] -mod tests { - use super::*; +use batch::{BATCH, batch_kernel, batch_op_words, batch_runs, op_words, run_starts, tile_grid}; +#[cfg(test)] +use consts::OP_UNIFORM; +pub use consts::{ACC_FORMAT, CHUNK, FX_BUDGET, PAGE, PATTERN_BUDGET, PATTERN_IDLE_FRAMES, RESIDENT_BUDGET}; +use convert::W; +#[cfg(test)] +use convert::{CONVERT_BATCH, colour_managed, convert_tile, convert_tiles, rgb_gray16_to_f16, rgb_gray32_to_f16}; +pub use convert::{f16_to_f32, f32_to_f16}; +pub use kit::SHADER; +use kit::{Kit, tex_entry}; +pub use paging::OVER_BUDGET; +use paging::first_error; +#[cfg(test)] +use paging::{lru_victims, uniform_buffer_len}; +#[cfg(not(target_arch = "wasm32"))] +pub use readback::{render_to_vec, render_to_vec_stats}; +#[cfg(test)] +use textures::TexKind; +use textures::{CachedPattern, FxEntry, FxKey, ResKey, Resident, Tex}; +pub use textures::{ChunkOut, Stats}; - #[test] - fn uniform_buffer_sizes_are_checked_before_allocation() { - assert_eq!(uniform_buffer_len(1, 255, 65_536).unwrap(), 65_536); - assert!(uniform_buffer_len(1, 255, 65_535).is_err()); - assert!(uniform_buffer_len(usize::MAX, 1, u64::MAX).is_err()); - assert!(uniform_buffer_len(usize::MAX, 0, u64::MAX).is_err()); - assert!(uniform_buffer_len(1, 16_777_216, u64::MAX).is_err(), "the last record offset cannot be represented by u32"); - #[cfg(target_pointer_width = "64")] - assert_eq!(uniform_buffer_len(1, 16_777_215, u64::MAX).unwrap(), 4_294_967_296, "the last valid u32 dynamic offset is accepted"); - } - - #[test] - fn half_roundtrip() { - for v in [0.0f32, 1.0, 0.5, 0.25, 1.0 / 255.0, 0.333, 65504.0, -2.5, 1e-6] { - let r = f16_to_f32(f32_to_f16(v)); - assert!((r - v).abs() <= v.abs() / 1024.0 + 1e-7, "{v} -> {r}"); - } - } - - #[test] - fn shader_validates() { - let module = wgpu::naga::front::wgsl::parse_str(SHADER).unwrap_or_else(|e| panic!("{}", e.emit_to_string(SHADER))); - wgpu::naga::valid::Validator::new(wgpu::naga::valid::ValidationFlags::all(), wgpu::naga::valid::Capabilities::empty()) - .validate(&module) - .unwrap_or_else(|e| panic!("{e:?}")); - for k in Kernel::DRAWN { - let e = k.entry().unwrap(); - assert!(module.entry_points.iter().any(|ep| ep.name == e), "missing entry point {e}"); - } - } - - /// The decimal float literals in `src` (comments skipped; hex literals are exact, so skipped), - /// with their line numbers. - fn decimal_float_literals(src: &str) -> Vec<(usize, String)> { - let mut out = Vec::new(); - for (n, line) in src.lines().enumerate() { - let code = line.split("//").next().unwrap_or("").as_bytes(); - let mut i = 0; - while i < code.len() { - let digit_or_dot = code[i].is_ascii_digit() || (code[i] == b'.' && code.get(i + 1).is_some_and(u8::is_ascii_digit)); - let word_before = i > 0 && (code[i - 1].is_ascii_alphanumeric() || code[i - 1] == b'_' || code[i - 1] == b'.'); - if !digit_or_dot || word_before { - i += 1; - continue; - } - if code[i] == b'0' && matches!(code.get(i + 1), Some(b'x' | b'X')) { - i += 2; - while i < code.len() && (code[i].is_ascii_hexdigit() || matches!(code[i], b'.' | b'p' | b'P' | b'+' | b'-')) { - i += 1; - } - continue; - } - let start = i; - while i < code.len() && (code[i].is_ascii_digit() || code[i] == b'.') { - i += 1; - } - if i < code.len() && matches!(code[i], b'e' | b'E') { - let mut j = i + 1; - if j < code.len() && matches!(code[j], b'+' | b'-') { - j += 1; - } - if j < code.len() && code[j].is_ascii_digit() { - i = j; - while i < code.len() && code[i].is_ascii_digit() { - i += 1; - } - } - } - out.push((n + 1, String::from_utf8_lossy(&code[start..i]).into_owned())); - } - } - out - } - - /// Browsers' WebGPU shader compiler (Tint) rejects a float literal beyond f32's finite range - /// even when it would round to `f32::MAX` (naga, used natively, rounds it), and then no - /// compositor pipeline builds and the web app falls back to the CPU renderer. `3.40282347e38` - /// was such a literal. - #[test] - fn shader_float_literals_fit_f32() { - let lits = decimal_float_literals(SHADER); - assert!(lits.iter().any(|(_, l)| l == "1.0"), "the scanner finds literals: {lits:?}"); - let bad: Vec<_> = lits.iter().filter(|(_, l)| l.parse::().map_or(true, |v| !v.is_finite() || v.abs() > f64::from(f32::MAX))).collect(); - assert!(bad.is_empty(), "float literals outside f32's finite range (line, literal): {bad:?}"); - } - - #[test] - fn op_record_fits_the_uniform() { - let p = plan::Pass::new(Kernel::FxPaint, 0); - assert_eq!(op_words(&p, None, None, None).len() as u64, OP_UNIFORM); - } - - /// #1774: a solid fill shares one tile across every coordinate; it is converted once, and - /// every coordinate (shared or not) still gets exactly the bytes `convert_tile` gives it, at - /// every depth and texture kind. - #[test] - fn convert_tiles_converts_each_distinct_tile_once() { - let area = Rect::new(0, 0, TILE_SIZE * 9, TILE_SIZE * 9); - for (fmt, kind) in [ - (PixelFormat::RGBA8, TexKind::Rgba8Direct), - (PixelFormat { alpha: false, ..PixelFormat::RGBA8 }, TexKind::Rgba8), - (PixelFormat::RGBA16, TexKind::Rgba16F), - (PixelFormat::RGBA32F, TexKind::Rgba16F), - (PixelFormat::GRAY8, TexKind::R8Direct), - (PixelFormat { sample: SampleType::U16, ..PixelFormat::GRAY8 }, TexKind::R32F), - ] { - let mut s = Surface::new(fmt); - let px = [0.2, 0.4, 0.6, 1.0]; - let n = fmt.channels(); - // Fully covered tiles share one `Arc`; then two tiles get their own pixels. - s.fill_rect(area, &px[..n]); - s.fill_rect(Rect::new(3, 5, 40, 60), &[0.9, 0.1, 0.3, 0.5][..n]); - s.fill_rect(Rect::new(TILE_SIZE * 4 + 7, TILE_SIZE * 2, TILE_SIZE * 4 + 9, TILE_SIZE * 2 + 3), &[0.0; 4][..n]); - let tiles: Vec<_> = area.tiles().filter_map(|c| s.tile(c).map(|t| (c, t.clone()))).collect(); - assert_eq!(tiles.len(), 81, "{fmt:?}"); - let mut seen = Vec::new(); - let converted = convert_tiles(&s, tiles, kind, |t, cs, bytes| { - for &c in cs { - assert!(s.tile(c).is_some_and(|own| Arc::ptr_eq(own, t)), "{fmt:?} {c:?}"); - assert_eq!(bytes, convert_tile(&s, Some(t), kind, c).as_slice(), "{fmt:?} {c:?}"); - seen.push(c); - } - }); - assert_eq!(converted, 3, "{fmt:?}: the shared tile once, plus the two edited ones"); - seen.sort_by_key(|c| (c.ty, c.tx)); - let mut want: Vec<_> = area.tiles().collect(); - want.sort_by_key(|c| (c.ty, c.tx)); - assert_eq!(seen, want, "{fmt:?}: every coordinate once"); - } - // More distinct tiles than one batch: all converted, each coordinate once. - let mut s = Surface::new(PixelFormat::RGBA16); - let wide = Rect::new(0, 0, TILE_SIZE * (CONVERT_BATCH as i32 + 5), TILE_SIZE); - for c in wide.tiles() { - s.fill_rect(Rect::new(c.rect().x0, 0, c.rect().x0 + 1, 1), &[c.tx as f32 / 100.0, 0.0, 0.0, 1.0]); - } - let tiles: Vec<_> = wide.tiles().filter_map(|c| s.tile(c).map(|t| (c, t.clone()))).collect(); - let mut n = 0; - let converted = convert_tiles(&s, tiles, TexKind::Rgba16F, |t, cs, bytes| { - for &c in cs { - assert_eq!(bytes, convert_tile(&s, Some(t), TexKind::Rgba16F, c).as_slice()); - n += 1; - } - }); - assert_eq!((converted, n), (CONVERT_BATCH + 5, CONVERT_BATCH + 5)); - } - - /// The 16-bit and 32-bit float RGB/gray fast paths give exactly the general path's half - /// floats (32-bit samples above 1 and below 0 included). - #[test] - fn sixteen_and_thirty_two_bit_fast_paths_match_the_general_one() { - for fmt in [ - PixelFormat::RGBA16, - PixelFormat { alpha: false, ..PixelFormat::RGBA16 }, - PixelFormat { sample: SampleType::U16, ..PixelFormat::GRAY8 }, - PixelFormat { sample: SampleType::U16, alpha: true, ..PixelFormat::GRAY8 }, - PixelFormat::RGBA32F, - PixelFormat { alpha: false, ..PixelFormat::RGBA32F }, - PixelFormat { sample: SampleType::F32, ..PixelFormat::GRAY8 }, - PixelFormat { sample: SampleType::F32, alpha: true, ..PixelFormat::GRAY8 }, - ] { - let mut s = Surface::new(fmt); - let r = Rect::new(0, 0, TILE_SIZE, TILE_SIZE); - let n = (TILE_SIZE * TILE_SIZE) as usize * fmt.channels(); - let hdr = if fmt.sample == SampleType::F32 { 4.0 } else { 1.0 }; - let data: Vec = (0..n).map(|i| (i as u32).wrapping_mul(2_654_435_761) as f32 / u32::MAX as f32 * hdr - (hdr - 1.0) / 4.0).collect(); - s.write_region(r, &data); - let t = s.tile(TileCoord::new(0, 0)).expect("written").clone(); - let fast = rgb_gray16_to_f16(&fmt, t.bytes()).or_else(|| rgb_gray32_to_f16(&fmt, t.bytes())).expect("fast path"); - let raw = s.read_region(r); - let general: Vec = - raw.chunks_exact(fmt.channels()).flat_map(|px| photocraft_raster::to_rgba(&fmt, px).map(|x| f32_to_f16(x).to_le_bytes())).flatten().collect(); - assert_eq!(fast, general, "{fmt:?}"); - assert_eq!(convert_tile(&s, Some(&t), TexKind::Rgba16F, TileCoord::new(0, 0)), general, "{fmt:?}"); - } - assert!(rgb_gray16_to_f16(&PixelFormat::RGBA8, &[]).is_none()); - assert!(rgb_gray16_to_f16(&PixelFormat { sample: SampleType::U16, ..PixelFormat::CMYKA8 }, &[]).is_none()); - assert!(rgb_gray32_to_f16(&PixelFormat::RGBA16, &[]).is_none()); - assert!(rgb_gray32_to_f16(&PixelFormat { sample: SampleType::F32, ..PixelFormat::CMYKA8 }, &[]).is_none()); - } - - #[test] - fn grid_rounds_out() { - assert_eq!(tile_grid(Rect::new(0, 0, 300, 256)), Rect::new(0, 0, 512, 256)); - } -} +#[cfg(test)] +mod tests; diff --git a/crates/gpu/src/paging.rs b/crates/gpu/src/paging.rs new file mode 100644 index 0000000000..b07f8e8db4 --- /dev/null +++ b/crates/gpu/src/paging.rs @@ -0,0 +1,126 @@ +use super::consts::STRIDE; +use super::textures::Cell; +use super::{DeviceHealth, FieldKind, HashMap, Rect, Unsupported, fx}; + +/// Document pixels of page cell `c`. +pub(crate) fn cell_rect(c: Cell, page: u32) -> Rect { + let p = page as i32; + Rect::new(c.0.saturating_mul(p), c.1.saturating_mul(p), c.0.saturating_add(1).saturating_mul(p), c.1.saturating_add(1).saturating_mul(p)) +} + +/// `r` cut along the grid of `step`-sized squares (row-major). +pub(crate) fn grid_rects(r: Rect, step: u32) -> Vec { + let s = step.max(1) as i32; + let mut out = Vec::new(); + let mut y = r.y0.div_euclid(s) * s; + while y < r.y1 { + let mut x = r.x0.div_euclid(s) * s; + while x < r.x1 { + out.push(Rect::new(x, y, x.saturating_add(s), y.saturating_add(s)).intersect(&r)); + x = x.saturating_add(s); + } + y = y.saturating_add(s); + } + out +} + +/// Number of rectangles `grid_rects` would produce, without allocating them during preflight. +pub(crate) fn grid_rect_count(r: Rect, step: u32) -> Result { + if r.is_empty() { + return Ok(0); + } + let s = i64::from(step.max(1)); + let axis = |lo: i32, hi: i32| usize::try_from((i64::from(hi) - 1).div_euclid(s) - i64::from(lo).div_euclid(s) + 1).ok(); + axis(r.x0, r.x1).and_then(|x| axis(r.y0, r.y1).and_then(|y| x.checked_mul(y))).ok_or_else(|| Unsupported("GPU compositor chunk count is too large".into())) +} + +/// Host length and GPU capacity for the chunk/pass uniform records, including their u32 +/// dynamic binding offsets. Decline unsupported stacks before an invalid buffer faults the device. +pub(crate) fn uniform_buffer_len(chunks: usize, passes: usize, limit: u64) -> Result { + let too_large = || Unsupported("GPU compositor uniform buffer is too large".into()); + let records = chunks.checked_add(passes).and_then(|n| u64::try_from(n).ok()).ok_or_else(too_large)?; + let bytes = records.checked_mul(STRIDE).ok_or_else(too_large)?; + if bytes > limit { + return Err(Unsupported(format!("GPU compositor uniform buffer requires {bytes} bytes; device limit is {limit}"))); + } + if bytes.saturating_sub(STRIDE) > u64::from(u32::MAX) { + return Err(Unsupported("GPU compositor uniform offsets exceed u32".into())); + } + usize::try_from(bytes).map_err(|_| too_large()) +} + +/// The reach up to which each distance field must be exact (the largest any program asks for). +pub(crate) fn field_reaches(progs: &[fx::MapProgram]) -> HashMap { + let mut want: HashMap = HashMap::new(); + for (k, r) in progs.iter().flat_map(|p| p.fields.iter()) { + let w = want.entry(*k).or_insert(0); + *w = (*w).max(*r); + } + want +} + +/// Why a refresh whose layer pages don't fit GPU memory is drawn on the CPU. +pub const OVER_BUDGET: &str = "layers exceed the GPU memory budget; full refresh on the CPU"; + +/// Which of `pages` (LRU clock, key, bytes) to drop, least recently used first, so `incoming` +/// more bytes fit `budget` on top of the `total` held now. Callers leave out pages that must stay. +pub(crate) fn lru_victims(mut pages: Vec<(u64, K, u64)>, total: u64, incoming: u64, budget: u64) -> Vec { + pages.sort_unstable_by_key(|p| p.0); + let mut total = total; + let mut out = Vec::new(); + for (_, k, bytes) in pages { + if total.saturating_add(incoming) <= budget { + break; + } + total = total.saturating_sub(bytes); + out.push(k); + } + out +} + +/// Runs `f` inside GPU error scopes and returns the first error raised. +/// Native wgpu reports validation/internal/OOM errors synchronously; where they only arrive +/// asynchronously (WebGPU) none is reported. +pub(crate) fn first_error(device: &wgpu::Device, f: impl FnOnce() -> T) -> (T, Option) { + let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); + let internal = device.push_error_scope(wgpu::ErrorFilter::Internal); + let memory = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let out = f(); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + let mut error = None; + // Scopes pop in reverse order. + for pop in [memory.pop(), internal.pop(), validation.pop()] { + if let std::task::Poll::Ready(Some(e)) = std::pin::pin!(pop).poll(&mut cx) { + error.get_or_insert(e); + } + } + (out, error) +} + +/// Runs `f` inside the error scopes of [`first_error`], turning a reported error into +/// [`Unsupported`] instead of letting it escape to wgpu's panic handler. +/// +/// Every native caller needs this, watched or not: a validation error (writing into a destroyed +/// or too-small buffer, say) is raised synchronously and never reaches the health callbacks, so a +/// watched compositor still panicked on it. WebGPU reports its errors asynchronously, long after +/// the scope has popped, so wasm relies on the health callbacks alone. +/// +/// A scope also swallows the error that [`DeviceHealth`]'s uncaptured-error handler would have +/// recorded, so a reported error is handed back to `health`: a watched device must stop being +/// used and let the caller fall back to the CPU compositor. +pub(crate) fn scoped_work(device: &wgpu::Device, health: Option, f: impl FnOnce() -> Result) -> Result { + // WebGPU errors arrive asynchronously; its callers still use the health callbacks. + if cfg!(target_arch = "wasm32") { + return f(); + } + let (result, error) = first_error(device, f); + match error { + Some(error) => { + if let Some(health) = health.as_ref() { + health.caught(device, &error); + } + Err(Unsupported(format!("GPU work failed: {error}"))) + } + None => result, + } +} diff --git a/crates/gpu/src/plan.rs b/crates/gpu/src/plan.rs index 58ce6e4299..166e977800 100644 --- a/crates/gpu/src/plan.rs +++ b/crates/gpu/src/plan.rs @@ -23,258 +23,25 @@ use photocraft_raster::Surface; use crate::bounds; +mod lower; +mod planner; +mod types; + +#[cfg(test)] +use lower::check_gradient_stops; +pub use lower::{adjustment_on_gpu, adjustment_program, mode_index, stroke_widths}; +use planner::Planner; +#[cfg(test)] +use planner::blend_if_params; +pub use planner::{ + F_ADD_DIFF, F_ATOP, F_BLEND_IF, F_CHANNELS, F_FIRST, F_GATE, F_HDR, F_KNOCKOUT, F_LAB, F_LAYER_SHAPE, F_NO_LAYER, F_OUTLINE, F_QUANT, F_REL, F_STROKE_OUT, + F_TEXT_GAMMA, F_VECTOR, INIT_ALPHA, INIT_COVER, INIT_INSIDE, INIT_OPAQUE, INIT_OUTLINE, INIT_RELATIVE, INIT_SCALE, INIT_VSTROKE, SHAPE_MAP, +}; +pub use types::{Checkpoint, DocCtx, FxLayer, Kernel, MapRef, MaskUse, Pass, Role, SurfaceRef, TexUse, Unsupported}; + /// Index of a chunk-sized accumulator texture. pub type Slot = u32; -/// Pipeline (fragment entry point) of a pass. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum Kernel { - /// Transparent fill (no draw). - Clear, - Content, - Mask, - Blend, - Atop, - Adjust, - AdjMix, - Lerp, - /// Effect chain start: the layer at fill opacity, or an opaque copy of a clipping base. - FxInit, - /// Paint an effect through a coverage map (or the layer's alpha) into A. - FxPaint, - /// Layer + interior effects over the exterior result, then layer opacity against the backdrop. - FxMerge, - /// Outside strokes: accumulate one stroke's share (`kind` 0, premultiplied colour) or its - /// coverage (`kind` 1). - FxStroke, - /// Outside strokes: resolve the accumulated strokes over the exterior result. - FxStrokeEnd, - /// Copy slot A into `dst` (an existing slot) over the pass clip (no draw). - CopyRect, - /// Copy all of slot A into the fresh slot `dst` (no draw). - CopyFull, - // Effect-map kernels (single-channel targets over an effect region, see `fx`). - MShift, - MDilate, - MBlur, - MGlow, - MFinish, - MBevelH, - MBevelShade, - MStroke, - MBevelTex, -} - -impl Kernel { - /// Fragment entry point in `compose.wgsl` (`None` for copies and clears). - pub fn entry(self) -> Option<&'static str> { - Some(match self { - Kernel::Clear | Kernel::CopyRect | Kernel::CopyFull => return None, - Kernel::Content => "fs_content", - Kernel::Mask => "fs_mask", - Kernel::Blend => "fs_blend", - Kernel::Atop => "fs_atop", - Kernel::Adjust => "fs_adjust", - Kernel::AdjMix => "fs_adjmix", - Kernel::Lerp => "fs_lerp", - Kernel::FxInit => "fs_fxinit", - Kernel::FxPaint => "fs_fxpaint", - Kernel::FxMerge => "fs_fxmerge", - Kernel::FxStroke => "fs_fxstroke", - Kernel::FxStrokeEnd => "fs_fxstrokeend", - Kernel::MShift => "fs_mshift", - Kernel::MDilate => "fs_mdilate", - Kernel::MBlur => "fs_mblur", - Kernel::MGlow => "fs_mglow", - Kernel::MFinish => "fs_mfinish", - Kernel::MBevelH => "fs_mbevelh", - Kernel::MBevelShade => "fs_mbevelshade", - Kernel::MStroke => "fs_mstroke", - Kernel::MBevelTex => "fs_mbeveltex", - }) - } - - /// Kernels that write effect maps (R32F / R16F targets) rather than RGBA accumulators. - pub fn is_map(self) -> bool { - matches!( - self, - Kernel::MShift - | Kernel::MDilate - | Kernel::MBlur - | Kernel::MGlow - | Kernel::MFinish - | Kernel::MBevelH - | Kernel::MBevelShade - | Kernel::MStroke - | Kernel::MBevelTex - ) - } - - /// Every kernel with a pipeline. - pub const DRAWN: [Kernel; 21] = [ - Kernel::Content, - Kernel::Mask, - Kernel::Blend, - Kernel::Atop, - Kernel::Adjust, - Kernel::AdjMix, - Kernel::Lerp, - Kernel::FxInit, - Kernel::FxPaint, - Kernel::FxMerge, - Kernel::FxStroke, - Kernel::FxStrokeEnd, - Kernel::MShift, - Kernel::MDilate, - Kernel::MBlur, - Kernel::MGlow, - Kernel::MFinish, - Kernel::MBevelH, - Kernel::MBevelShade, - Kernel::MStroke, - Kernel::MBevelTex, - ]; -} - -/// Which resident texture a pass samples. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum Role { - Content, - Mask, - /// A stroked shape's stroke alone (its fill uses `Content`). - Stroke, -} - -/// A surface the pass needs on the GPU. -#[derive(Clone, Debug)] -pub struct TexUse<'a> { - pub layer: LayerId, - pub role: Role, - pub surface: SurfaceRef<'a>, -} - -/// An effect map sampled by a pass: map `map` of enabled effect `item` of `plan.fx[fx]`. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct MapRef { - pub fx: usize, - pub item: usize, - pub map: usize, -} - -#[derive(Clone, Debug)] -pub struct Pass<'a> { - pub kernel: Kernel, - pub dst: Slot, - pub a: Option, - pub b: Option, - pub c: Option, - /// A fourth slot, bound in place of `tex` (passes without layer pixels). - pub d: Option, - pub mode: BlendMode, - pub opacity: f32, - /// The layer's Fill where it isn't folded into `opacity`: the final blend of a plain layer - /// (`Kernel::Blend`), so the special eight can apply it inside the blend. 1 elsewhere. - pub fill: f32, - /// Layer pixels (raster / text / shape / smart cache / fill cache). - pub tex: Option>, - /// Colour outside `tex` (the surface's default pixel), the solid fill colour, or the effect - /// colour. - pub color: [f32; 4], - pub mask: Option>, - /// Adjustment kind (see `adjust` in compose.wgsl) and parameters; the coverage kind of an - /// effect paint. - pub adjust_kind: i32, - pub params: [[f32; 4]; 4], - pub extra: [f32; 4], - /// Extra shader flags (`F_KNOCKOUT`, …). - pub flags: u32, - /// 4096-entry LUT rows (Levels / Curves / Gradient map / gradient stops). - pub lut: Option>, - pub gradient: bool, - /// Effect map sampled by `FxPaint`. - pub map: Option, - /// Pattern painted by `FxPaint`. - pub pattern: Option<&'a Pattern>, - /// Only pixels inside this rect (document coordinates) are computed; the pass is skipped for - /// chunks it misses. Consumers of the slot must be clipped alike. - pub clip: Option, -} - -/// Pixels a pass samples: a surface of the document, or one derived from it on the CPU (a -/// combined pixel × vector mask, a shape's fill or stroke alone). -#[derive(Clone, Debug)] -pub enum SurfaceRef<'a> { - Doc(&'a Surface), - Derived(std::sync::Arc), -} - -impl SurfaceRef<'_> { - pub fn get(&self) -> &Surface { - match self { - SurfaceRef::Doc(s) => s, - SurfaceRef::Derived(s) => s, - } - } -} - -#[derive(Clone, Debug)] -pub struct MaskUse<'a> { - pub layer: LayerId, - pub surface: SurfaceRef<'a>, - pub density: f32, - pub default: f32, -} - -impl<'a> Pass<'a> { - pub(crate) fn new(kernel: Kernel, dst: Slot) -> Self { - Pass { - kernel, - dst, - a: None, - b: None, - c: None, - d: None, - mode: BlendMode::Normal, - opacity: 1.0, - fill: 1.0, - tex: None, - color: [0.0; 4], - mask: None, - adjust_kind: 0, - params: [[0.0; 4]; 4], - extra: [0.0; 4], - flags: 0, - lut: None, - gradient: false, - map: None, - pattern: None, - clip: None, - } - } -} - -/// Why a document can't be composited on the GPU (the caller falls back to the CPU compositor). -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct Unsupported(pub String); - -impl std::fmt::Display for Unsupported { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "GPU compositor: {}", self.0) - } -} - -impl std::error::Error for Unsupported {} - -/// A layer whose effects the plan renders: its maps are built (or reused) before the chunks run. -#[derive(Clone, Copy, Debug)] -pub struct FxLayer<'a> { - pub layer: &'a Layer, - /// Region the maps cover (`compose::effect_maps`). - pub region: Rect, - /// Layer bounds (gradients and linked patterns are laid out in them). - pub bounds: Rect, -} - #[derive(Debug)] pub struct Plan<'a> { pub passes: Vec>, @@ -286,46 +53,6 @@ pub struct Plan<'a> { pub checkpoints: Vec, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct Checkpoint { - /// First root layer included after occlusion pruning. - pub layer_start: usize, - /// Root layers below this boundary (including the complete clipping unit). - pub layer_end: usize, - /// Resume at this pass with `slot` restored. - pub pass_end: usize, - pub slot: Slot, -} - -/// Document-level inputs of a plan. -#[derive(Clone, Copy)] -pub struct DocCtx<'a> { - pub canvas: Rect, - pub transfer: Transfer, - pub light: GlobalLight, - pub patterns: &'a [Pattern], - /// Colour mode (channel restrictions name its channels). - pub mode: photocraft_color::ColorMode, - /// Sample depth (adjustment results are rounded to it). - pub depth: photocraft_color::SampleType, - /// The document (effect geometry the CPU computes, e.g. gradient stroke frames). - pub doc: &'a Document, -} - -impl<'a> DocCtx<'a> { - pub fn of(doc: &'a Document) -> Self { - DocCtx { - canvas: doc.bounds(), - transfer: Transfer::for_document(doc.mode, doc.depth), - light: doc.global_light, - patterns: &doc.patterns, - mode: doc.mode, - depth: doc.depth, - doc, - } - } -} - /// Build the pass list for `doc`. pub fn plan(doc: &Document) -> Result, Unsupported> { if doc.mode == photocraft_color::ColorMode::Multichannel { @@ -340,1763 +67,5 @@ pub fn plan(doc: &Document) -> Result, Unsupported> { Ok(p.finish(root)) } -struct Planner<'a> { - passes: Vec>, - free: Vec, - refs: Vec, - cx: DocCtx<'a>, - fx: Vec>, - checkpoints: Vec, -} - -/// Coverage source of an effect paint (`kind` in `fs_fxpaint`); flags refine it. -#[derive(Clone, Copy)] -enum Cov { - /// An effect map; `outside` beyond its region. - Map(MapRef, f32), - /// Full coverage. - One, -} - -/// Shader flags for effect passes (keep in sync with compose.wgsl). Coverage `m` of a paint, with -/// `a` the layer's alpha and `inside` = `a > 0.5/255`: -/// knockout `m × (1 − a × k)` (k in `p4.w`); gate `inside ? m : 0`; rel `inside ? min(m / a, 1) : 0`; -/// stroke-out `inside ? (vector ? 0 : (1 − a) / (1 − layer alpha), 1 at an opaque layer) : m`. -pub const F_KNOCKOUT: u32 = 16; -/// Effect merge: A already holds the layer; only mix with the backdrop by the layer's opacity. -pub const F_NO_LAYER: u32 = 32; -/// Outside strokes along a filled shape's outline (`effects::outline_share`; the coverage slot -/// carries the layer's alpha in `.g`). -pub const F_OUTLINE: u32 = 131072; -/// Shape layer (outside strokes never show inside it). -pub const F_VECTOR: u32 = 64; -/// Merge onto an opaque clipping base, keeping its alpha. -pub const F_ATOP: u32 = 128; -pub const F_GATE: u32 = 256; -pub const F_REL: u32 = 512; -pub const F_STROKE_OUT: u32 = 1024; -/// First outside stroke (no accumulated share yet). -pub const F_FIRST: u32 = 2048; -/// `Lerp` per channel: weights in `p0` (channel restrictions), no opacity or mask. -pub const F_CHANNELS: u32 = 4096; -/// Lab document: Normal blending mixes in CIELAB (`psblend::LAB_MIX`). -pub const F_LAB: u32 = 65536; -/// 32-bit float document: Add, Divide and Exposure retain values above 1; the non-separable -/// blend modes clip only below 0 (`psblend::HDR`). -pub const F_HDR: u32 = 262144; -/// `Lerp`: A rounded to `p0.x` steps per unit (adjustment results on integer documents). -pub const F_QUANT: u32 = 32768; -/// `Lerp` as A + (B − C) premultiplied (layers clipped to pass-through groups). -pub const F_ADD_DIFF: u32 = 16384; -/// `Lerp`: B mixed back towards A (the backdrop) by the layer's Blend If weight -/// (`compose::apply_blend_if`), from C ("This Layer", or B itself for an adjustment layer, `p4.y`) -/// and A ("Underlying Layer"). The ranges are packed in `p0..p3` (`blend_if_params`); `p4.x` marks -/// a one-channel document. -pub const F_BLEND_IF: u32 = 524288; -/// Blend / Atop / FxMerge of a type layer: coverage mixed at `psblend::TEXT_GAMMA`. -pub const F_TEXT_GAMMA: u32 = 8192; - -/// A layer's Blend If ranges for `F_BLEND_IF`: Gray, then the colour channels, each as -/// [this layer, underlying layer]; every range as two floats, `black low × 256 + black high` and -/// `white low × 256 + white high` (exact in f32), so entry `e`, side `s` starts at float -/// `(e × 2 + s) × 2`. Entries past the fourth never apply (RGB has four, one-channel modes two). -fn blend_if_params(bi: &photocraft_doc::BlendIf) -> [[f32; 4]; 4] { - let mut f = [0.0f32; 16]; - for e in 0..4 { - for (s, r) in bi.get(e).iter().enumerate() { - let i = (e * 2 + s) * 2; - f[i] = f32::from(r.black[0]) * 256.0 + f32::from(r.black[1]); - f[i + 1] = f32::from(r.white[0]) * 256.0 + f32::from(r.white[1]); - } - } - [[f[0], f[1], f[2], f[3]], [f[4], f[5], f[6], f[7]], [f[8], f[9], f[10], f[11]], [f[12], f[13], f[14], f[15]]] -} - -/// `F_TEXT_GAMMA` for type layers while text gamma blending is on; the gamma goes in `p4.w`. -fn gamma_flag(layer: &Layer) -> u32 { - if photocraft_compose::text_gamma(layer) != 1.0 { F_TEXT_GAMMA } else { 0 } -} - -/// `FxInit` kinds: A at `opacity` × alpha; an opaque copy of A; A's colour with alpha `opacity` -/// inside B's shape; A with alpha × B's alpha. -pub const INIT_SCALE: i32 = 0; -pub const INIT_OPAQUE: i32 = 1; -pub const INIT_INSIDE: i32 = 2; -pub const INIT_ALPHA: i32 = 3; -/// The vector stroke C over A relative to the shape B, at `opacity`. -pub const INIT_VSTROKE: i32 = 4; -/// A's colour with alpha `opacity` × A's alpha relative to B's. -pub const INIT_RELATIVE: i32 = 5; -/// Outside-stroke coverage seed: (0, A's alpha). -pub const INIT_COVER: i32 = 6; -/// A with its alpha joined with the effect shape (the map). -pub const INIT_OUTLINE: i32 = 7; - -/// `MapRef::item` of the layer's effect shape (`compose::layer_shape`) instead of an effect map. -pub const SHAPE_MAP: usize = usize::MAX; - -impl<'a> Planner<'a> { - fn new(cx: DocCtx<'a>) -> Self { - Planner { passes: Vec::new(), free: Vec::new(), refs: Vec::new(), cx, fx: Vec::new(), checkpoints: Vec::new() } - } - - fn finish(self, root: Slot) -> Plan<'a> { - Plan { passes: self.passes, slots: self.refs.len() as u32, root, fx: self.fx, checkpoints: self.checkpoints } - } - - fn alloc(&mut self) -> Slot { - if let Some(s) = self.free.pop() { - self.refs[s as usize] = 1; - return s; - } - self.refs.push(1); - (self.refs.len() - 1) as Slot - } - fn retain(&mut self, s: Slot) -> Slot { - self.refs[s as usize] += 1; - s - } - fn release(&mut self, s: Slot) { - let r = &mut self.refs[s as usize]; - *r -= 1; - if *r == 0 { - self.free.push(s); - } - } - /// Emit `pass` into a fresh slot, releasing its inputs. - fn emit(&mut self, mut pass: Pass<'a>) -> Slot { - let dst = self.alloc(); - pass.dst = dst; - if self.cx.mode == photocraft_color::ColorMode::Lab { - pass.flags |= F_LAB; - } - if self.cx.depth == photocraft_color::SampleType::F32 { - pass.flags |= F_HDR; - } - let (a, b, c, d) = (pass.a, pass.b, pass.c, pass.d); - self.passes.push(pass); - for s in [a, b, c, d].into_iter().flatten() { - self.release(s); - } - dst - } - fn clear(&mut self) -> Slot { - self.emit(Pass::new(Kernel::Clear, 0)) - } - - /// composite_stack: returns the new backdrop (consumes `backdrop`). - fn stack(&mut self, layers: &'a [Layer], backdrop: Slot) -> Result { - self.stack_inner(layers, backdrop, None) - } - - fn stack_inner(&mut self, layers: &'a [Layer], mut backdrop: Slot, root_start: Option) -> Result { - let mut i = 0; - while i < layers.len() { - let base = &layers[i]; - let mut j = i + 1; - while j < layers.len() && layers[j].clipped && !base.clipped { - j += 1; - } - let clipped = &layers[i + 1..j]; - if base.visible { - backdrop = self.layer(base, clipped, backdrop)?; - if let (LayerContent::Adjustment(_), Some(q)) = (&base.content, photocraft_compose::adjustment_quantum(self.cx.depth)) { - // compose: adjustment results are rounded to the document's depth. - let mut p = Pass::new(Kernel::Lerp, 0); - p.a = Some(backdrop); - p.flags = F_QUANT; - p.params[0] = [q, 0.0, 0.0, 0.0]; - backdrop = self.emit(p); - } - } - i = j.max(i + 1); - if let Some(start) = root_start - && self.refs.iter().enumerate().all(|(slot, &refs)| refs == u32::from(slot == backdrop as usize)) - { - self.checkpoints.push(Checkpoint { layer_start: start, layer_end: start.saturating_add(i), pass_end: self.passes.len(), slot: backdrop }); - } - } - Ok(backdrop) - } - - fn mask_use(&self, layer: &'a Layer) -> Option> { - if let Some(c) = photocraft_compose::masks::combined_mask(layer, self.cx.canvas) { - let default = c.default_pixel().first().copied().unwrap_or(1.0); - return Some(MaskUse { layer: layer.id, surface: SurfaceRef::Derived(std::sync::Arc::new(c)), density: 1.0, default }); - } - let m = layer.mask.as_ref()?; - if !m.enabled { - return None; - } - Some(MaskUse { - layer: layer.id, - surface: SurfaceRef::Doc(&m.surface), - density: m.density, - default: m.surface.default_pixel().first().copied().unwrap_or(1.0), - }) - } - - /// `compose::apply_blend_if`: `after` (the backdrop with `layer` drawn) mixed back towards - /// `before` (the backdrop without it) where the layer's Blend If ranges hide it. "This Layer" - /// is the layer's own content with its masks; an adjustment layer is judged by its result. - /// Consumes both slots. - fn blend_if(&mut self, layer: &'a Layer, before: Slot, after: Slot) -> Result { - let own = match layer.content { - LayerContent::Adjustment(_) => None, - _ => Some(self.content(layer)?), - }; - let mut p = Pass::new(Kernel::Lerp, 0); - p.a = Some(before); - p.b = Some(after); - p.c = own; - p.flags = F_BLEND_IF; - p.params = blend_if_params(&layer.blend_if); - let one_channel = self.cx.mode != photocraft_color::ColorMode::Rgb; - p.extra = [f32::from(u8::from(one_channel)), f32::from(u8::from(own.is_none())), 0.0, 0.0]; - Ok(self.emit(p)) - } - - /// Channel restrictions and Blend If around `draw`, which composites `layer` onto `backdrop` - /// (`compose::composite_layer` / `composite_atop`: both are relative to the same backdrop). - fn restricted(&mut self, layer: &'a Layer, backdrop: Slot, draw: impl FnOnce(&mut Self, Slot) -> Result) -> Result { - let w = photocraft_compose::channel_weights(layer, self.cx.mode); - let blend_if = photocraft_compose::blend_if_active(layer, self.cx.mode); - if w.is_none() && !blend_if { - return draw(self, backdrop); - } - let before = self.retain(backdrop); - let mut after = draw(self, backdrop)?; - if let Some(w) = w { - let b = if blend_if { self.retain(before) } else { before }; - after = self.restore_channels(b, after, w); - } - if blend_if { - after = self.blend_if(layer, before, after)?; - } - Ok(after) - } - - /// Blending Options › Advanced Blending (knockout, clipped layers blended individually, blend - /// interior effects as group, transparency shapes off, masks hiding effects) has no GPU pass: - /// documents using it are composited on the CPU (`photocraft_compose::advanced`). Layers at - /// Photoshop's defaults pass this check for free. - fn check_advanced(&self, layer: &Layer, clipped: &[Layer]) -> Result<(), Unsupported> { - if photocraft_compose::advanced_active(layer, clipped) { - return Err(Unsupported(format!("Advanced Blending on `{}` (composited on the CPU)", layer.name))); - } - Ok(()) - } - - /// composite_layer, honouring the layer's channel restrictions. - fn layer(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { - self.check_advanced(layer, clipped)?; - self.restricted(layer, backdrop, |s, b| s.layer_any(layer, clipped, b)) - } - - /// `compose::restore_channels`: `after` with the channels a layer leaves out taken from - /// `before` (consumes both). - fn restore_channels(&mut self, before: Slot, after: Slot, w: [f32; 3]) -> Slot { - let mut p = Pass::new(Kernel::Lerp, 0); - p.a = Some(before); - p.b = Some(after); - p.flags = F_CHANNELS; - p.params[0] = [w[0], w[1], w[2], 1.0]; - self.emit(p) - } - - fn layer_any(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { - match layer.artboard() { - Some(ab) => self.artboard(layer, ab, clipped, backdrop), - None => self.layer_plain(layer, clipped, backdrop), - } - } - - /// compose::composite_artboard: the board's background and the group composited over the - /// backdrop, kept only inside the board (contents and effects outside it are clipped away). - fn artboard(&mut self, layer: &'a Layer, ab: &photocraft_doc::Artboard, clipped: &'a [Layer], backdrop: Slot) -> Result { - let board = ab.rect.intersect(&self.cx.canvas); - if board.is_empty() { - return Ok(backdrop); - } - let before = self.retain(backdrop); - let mut sub = backdrop; - if let Some(bg) = ab.background.rgba() { - let mut c = Pass::new(Kernel::Content, 0); - c.color = bg; - let c = self.emit(c); - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(sub); - p.b = Some(c); - sub = self.emit(p); - } - let sub = self.layer_plain(layer, clipped, sub)?; - let dst = if self.refs[before as usize] == 1 { - before - } else { - let n = self.alloc(); - let mut p = Pass::new(Kernel::CopyFull, n); - p.a = Some(before); - self.passes.push(p); - self.release(before); - n - }; - let mut p = Pass::new(Kernel::CopyRect, dst); - p.a = Some(sub); - p.clip = Some(board); - self.passes.push(p); - self.release(sub); - Ok(dst) - } - - fn layer_plain(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { - let opacity = layer.opacity * layer.fill_opacity; - let visible_clipped: Vec<&'a Layer> = clipped.iter().filter(|c| c.visible).collect(); - - // Below 100% fill a pass-through group renders isolated (as the CPU compositor does), and - // so does one that is the base of a clipping group and holds an adjustment layer. - let clip_base_adjusts = !visible_clipped.is_empty() - && matches!(&layer.content, LayerContent::Group(g) if g.children.iter().any(|c| c.visible && matches!(c.content, LayerContent::Adjustment(_)))); - if let LayerContent::Group(g) = &layer.content - && layer.blend == BlendMode::PassThrough - && layer.fill_opacity >= 1.0 - && !has_effects(layer) - && !clip_base_adjusts - { - let before = self.retain(backdrop); - let mut after = self.stack(&g.children, backdrop)?; - if opacity < 1.0 || layer.mask.is_some() || layer.vector_mask.is_some() { - let mut p = Pass::new(Kernel::Lerp, 0); - p.a = Some(self.retain(before)); - p.b = Some(after); - p.opacity = opacity; - p.mask = self.mask_use(layer); - after = self.emit(p); - } - if visible_clipped.is_empty() { - self.release(before); - return Ok(after); - } - // Layers clipped to a pass-through group (compose: their effect on the group's - // isolated rendering, each placed over the original backdrop, is added to the - // pass-through result, premultiplied). - let iso = self.content(layer)?; - let iso_keep = self.retain(iso); - let mut clipped_iso = iso; - for c in visible_clipped { - clipped_iso = self.atop(c, clipped_iso)?; - } - let over = |s: &mut Self, src: Slot, before: Slot| { - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(before); - p.b = Some(src); - p.opacity = opacity; - s.emit(p) - }; - let b2 = self.retain(before); - let without = over(self, iso_keep, b2); - let with = over(self, clipped_iso, before); - let mut p = Pass::new(Kernel::Lerp, 0); - p.a = Some(after); - p.b = Some(with); - p.c = Some(without); - p.flags = F_ADD_DIFF; - return Ok(self.emit(p)); - } - - if let LayerContent::Adjustment(adj) = &layer.content { - let before = self.retain(backdrop); - let mut adjusted = self.adjust(adj, backdrop)?; - for c in visible_clipped { - adjusted = self.atop(c, adjusted)?; - } - let mut p = Pass::new(Kernel::AdjMix, 0); - p.a = Some(before); - p.b = Some(adjusted); - p.mode = layer.blend; - p.opacity = opacity; - p.mask = self.mask_use(layer); - return Ok(self.emit(p)); - } - - if has_effects(layer) { - return self.effects(layer, &visible_clipped, backdrop, false); - } - - if let LayerContent::Shape(sh) = &layer.content - && !visible_clipped.is_empty() - && let Some((fill, stroke)) = photocraft_compose::shape_split::split(sh, self.cx.canvas, self.cx.depth) - { - // The vector stroke goes above the clipped layers: the fill is the clipping base, - // the stroke is laid over the clipped result, then the masks apply to both - // (compose::shape_parts). - let mut content = self.shape_part(layer, Role::Content, fill); - for c in visible_clipped { - content = self.atop(c, content)?; - } - let stroke = self.shape_part(layer, Role::Stroke, stroke); - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(content); - p.b = Some(stroke); - let mut content = self.emit(p); - if let Some(m) = self.mask_use(layer) { - let mut p = Pass::new(Kernel::Mask, 0); - p.a = Some(content); - p.mask = Some(m); - content = self.emit(p); - } - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(backdrop); - p.b = Some(content); - p.mode = layer.blend; - p.opacity = layer.opacity; - p.fill = layer.fill_opacity; - return Ok(self.emit(p)); - } - - // A layer that covers little of the canvas composites only over its bounds and is - // copied back into the backdrop (#125): a layout with hundreds of small text and shape - // layers otherwise shades every layer over every chunk. - let clip = self.local_clip(layer); - if clip.is_some_and(|c| c.is_empty()) { - // Nothing to draw (an empty layer, or a group of them). - for c in &visible_clipped { - self.check_advanced(c, &[])?; - } - return Ok(backdrop); - } - let start = self.passes.len(); - let mut content = self.content(layer)?; - for c in visible_clipped { - content = self.atop(c, content)?; - } - if let Some(clip) = clip { - // Everything that built the layer's content only matters inside its bounds. - for p in &mut self.passes[start..] { - if p.clip.is_none() && !matches!(p.kernel, Kernel::Clear | Kernel::CopyFull) { - p.clip = Some(clip); - } - } - } - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(if clip.is_some() { self.retain(backdrop) } else { backdrop }); - p.b = Some(content); - p.mode = layer.blend; - p.opacity = layer.opacity; - p.fill = layer.fill_opacity; - p.flags = gamma_flag(layer); - p.extra[3] = photocraft_compose::text_gamma(layer); - p.clip = clip; - let merged = self.emit(p); - match clip { - Some(clip) => Ok(self.write_back(backdrop, merged, clip)), - None => Ok(merged), - } - } - - /// The rect a plain layer composites over: its composite bounds when they cover at most half - /// the canvas (beyond that, compositing everywhere costs less than the copy back). - fn local_clip(&self, layer: &Layer) -> Option { - let canvas = self.cx.canvas; - let b = bounds::composite_bounds(layer, canvas)?; - (b.width() as u64 * b.height() as u64 * 2 <= canvas.width() as u64 * canvas.height() as u64).then_some(b) - } - - /// Copy `merged` into `backdrop` over `clip` (in place when nothing else holds the backdrop); - /// returns the new backdrop. Consumes both. - fn write_back(&mut self, backdrop: Slot, merged: Slot, clip: Rect) -> Slot { - let dst = if self.refs[backdrop as usize] == 1 { - backdrop - } else { - let n = self.alloc(); - let mut p = Pass::new(Kernel::CopyFull, n); - p.a = Some(backdrop); - self.passes.push(p); - self.release(backdrop); - n - }; - let mut p = Pass::new(Kernel::CopyRect, dst); - p.a = Some(merged); - p.clip = Some(clip); - self.passes.push(p); - self.release(merged); - dst - } - - /// A stroked shape's fill or vector stroke alone (`compose::shape_split`), unmasked. - fn shape_part(&mut self, layer: &'a Layer, role: Role, surface: Surface) -> Slot { - let mut p = Pass::new(Kernel::Content, 0); - p.color = photocraft_raster::to_rgba(&surface.format(), &surface.default_pixel()); - if surface.tile_count() > 0 { - p.tex = Some(TexUse { layer: layer.id, role, surface: SurfaceRef::Derived(std::sync::Arc::new(surface)) }); - } - self.emit(p) - } - - /// render_content for non-adjustment layers. - fn content(&mut self, layer: &'a Layer) -> Result { - match &layer.content { - LayerContent::Group(g) => { - let empty = self.clear(); - let s = self.stack(&g.children, empty)?; - if layer.mask.is_some() || layer.vector_mask.is_some() { - let mut p = Pass::new(Kernel::Mask, 0); - p.a = Some(s); - p.mask = self.mask_use(layer); - return Ok(self.emit(p)); - } - Ok(s) - } - // Adjustments are handled by the caller and never get here. - LayerContent::Adjustment(_) => Err(Unsupported("adjustment layer reached the content planner".into())), - LayerContent::Fill(f @ Fill::Pattern { name, scale, id, angle, link, phase }) if layer.fill_cache.as_ref().is_none_or(|c| c.fill != *f) => { - // compose::render_fill: the pattern tiled from the layer's frame (transparent - // when missing), then the layer's masks. - let empty = self.clear(); - let Some(pat) = photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) else { return Ok(empty) }; - let frame = photocraft_compose::fill_frame(layer, self.cx.canvas); - let anchor = if frame.is_empty() { (0.0, 0.0) } else { (f64::from(frame.x0), f64::from(frame.y0)) }; - let paint = Paint::Pattern(pat, placement(anchor, *link, *phase, *scale, *angle)); - let canvas = self.cx.canvas; - let s = self.paint(empty, empty, Cov::One, &paint, BlendMode::Normal, 1.0, 0, canvas, canvas); - if layer.mask.is_some() || layer.vector_mask.is_some() { - let mut p = Pass::new(Kernel::Mask, 0); - p.a = Some(s); - p.mask = self.mask_use(layer); - return Ok(self.emit(p)); - } - Ok(s) - } - _ => { - let p = self.content_pass(layer)?; - Ok(self.emit(p)) - } - } - } - - /// The Content pass of a raster / text / shape / smart / fill layer. - fn content_pass(&self, layer: &'a Layer) -> Result, Unsupported> { - let mut p = Pass::new(Kernel::Content, 0); - p.mask = self.mask_use(layer); - match &layer.content { - LayerContent::Fill(f) => match &layer.fill_cache { - Some(c) if c.fill == *f => self.surface_tex(&mut p, layer.id, &c.surface), - _ => self.fill(&mut p, f, photocraft_compose::fill_frame(layer, self.cx.canvas))?, - }, - _ => { - if let Some(s) = layer.surface() { - self.surface_tex(&mut p, layer.id, s); - } - } - } - Ok(p) - } - - fn surface_tex(&self, p: &mut Pass<'a>, id: LayerId, s: &'a Surface) { - let dp = s.default_pixel(); - p.color = photocraft_raster::to_rgba(&s.format(), &dp); - if s.tile_count() > 0 { - p.tex = Some(TexUse { layer: id, role: Role::Content, surface: SurfaceRef::Doc(s) }); - } - } - - fn fill(&self, p: &mut Pass<'a>, f: &Fill, frame: photocraft_geom::Rect) -> Result<(), Unsupported> { - // The RGB shader cannot interpolate or dither native ink channels. - if self.cx.mode == photocraft_color::ColorMode::Cmyk && !matches!(f, Fill::Pattern { .. }) { - return Err(Unsupported("native CMYK fill (composited on the CPU)".into())); - } - match f { - Fill::Solid(c) => { - let rgb = c.to_rgb(); - p.color = [rgb[0], rgb[1], rgb[2], c.alpha]; - } - Fill::Gradient { stops, opacity_stops, angle, scale, style, reverse, offset, dither, .. } => { - check_gradient_stops(stops)?; - check_gradient_stops(opacity_stops)?; - p.gradient = true; - // compose::render_fill: whole-pixel end points (fill_layout). - let (angle, scale, offset) = photocraft_compose::fill_layout::gradient_layout(*style, *angle, *scale, *offset, frame); - p.params[0] = [angle, scale, if *reverse { 1.0 } else { 0.0 }, style_index(*style)]; - let c = frame; - p.params[1] = [c.x0 as f32, c.y0 as f32, c.width() as f32, c.height() as f32]; - // p2.xy: centre offset; p2.z: the depth's levels a dithered pixel is rounded to - // (0: float, compose::gradient_fill::render_quantized); p2.w: dither (the shared - // position hash, see the shader). - let quantum = photocraft_compose::adjustment_quantum(self.cx.depth).unwrap_or(0.0); - p.params[2] = [offset.0, offset.1, quantum, if *dither { 1.0 } else { 0.0 }]; - let ramp = photocraft_compose::gradient_fill::Ramp::new(f); - let mut rows = vec![[0.0f32; 4096]; 4]; - for k in 0..4096 { - let v = ramp.as_ref().map_or([0.0; 4], |r| r.sample(k as f32 / 4095.0)); - for (ch, row) in rows.iter_mut().enumerate() { - row[k] = v[ch]; - } - } - p.lut = Some(rows); - } - // Pattern fills fall back to the CPU compositor (see `check`). - Fill::Pattern { .. } => {} - } - Ok(()) - } - - /// composite_atop: `layer` onto `base`, restricted to the base's alpha, honouring the - /// layer's channel restrictions. - fn atop(&mut self, layer: &'a Layer, base: Slot) -> Result { - self.check_advanced(layer, &[])?; - self.restricted(layer, base, |s, b| s.atop_any(layer, b)) - } - - fn atop_any(&mut self, layer: &'a Layer, base: Slot) -> Result { - let opacity = layer.opacity * layer.fill_opacity; - if let LayerContent::Adjustment(adj) = &layer.content { - let before = self.retain(base); - let adjusted = self.adjust(adj, base)?; - let mut p = Pass::new(Kernel::AdjMix, 0); - p.a = Some(before); - p.b = Some(adjusted); - p.mode = layer.blend; - p.opacity = opacity; - p.mask = self.mask_use(layer); - return Ok(self.emit(p)); - } - if has_effects(layer) { - return self.effects(layer, &[], base, true); - } - let content = self.content(layer)?; - let mut p = Pass::new(Kernel::Atop, 0); - p.a = Some(base); - p.b = Some(content); - p.mode = layer.blend; - p.opacity = opacity; - p.flags = gamma_flag(layer); - p.extra[3] = photocraft_compose::text_gamma(layer); - Ok(self.emit(p)) - } - - /// adjust::apply_with on a slot (consumes it). - fn adjust(&mut self, adj: &Adjustment, src: Slot) -> Result { - if !adjustment_on_gpu(adj, self.cx.mode) { - return Err(Unsupported(format!("{} on CMYK/Lab channels (evaluated on the CPU)", adj.label()))); - } - if let Adjustment::GradientMap { stops, .. } = adj { - check_gradient_stops(stops)?; - } - let mut p = Pass::new(Kernel::Adjust, 0); - p.a = Some(src); - let (kind, params, lut) = adjustment_program(adj, self.cx.transfer, self.cx.depth); - p.adjust_kind = kind; - p.params = params; - p.lut = lut; - Ok(self.emit(p)) - } - - /// `composite_with_effects` (atop = false) or the clipped-layer variant of `composite_atop` - /// (atop = true: effects over the base treated as opaque, keeping the base's alpha). - /// Consumes `backdrop`. - fn effects(&mut self, layer: &'a Layer, clipped: &[&'a Layer], backdrop: Slot, atop: bool) -> Result { - let canvas = self.cx.canvas; - let region = bounds::effect_region(layer, canvas); - let sb = photocraft_compose::paint_bounds(layer).unwrap_or_else(|| bounds::layer_bounds(layer, canvas)); - let clip = if bounds::transparent_outside(layer) { region } else { canvas }; - // A stroked shape's vector stroke goes above its clipped layers and interior effects - // (compose::split_parts): the fill and the stroke unmasked, the masks applied after. - let split = match &layer.content { - LayerContent::Shape(sh) if sh.stroke.is_some() => photocraft_compose::shape_split::split(sh, canvas, self.cx.depth), - _ => None, - }; - let (mut content, vstroke) = match split { - Some((fill, stroke)) => { - let f = self.shape_part(layer, Role::Content, fill); - let s = self.shape_part(layer, Role::Stroke, stroke); - (f, Some(s)) - } - None => (self.content(layer)?, None), - }; - for p in self.passes.iter_mut().rev().take(if vstroke.is_some() { 2 } else { 1 }) { - if !matches!(layer.content, LayerContent::Group(_)) { - p.clip = Some(clip); - } - } - for c in clipped { - content = self.atop(c, content)?; - } - let fx = self.fx.len(); - self.fx.push(FxLayer { layer, region, bounds: sb }); - let outline = photocraft_compose::effect_outline(layer).is_some(); - let relative = outline || vstroke.is_some(); - // `content` becomes the effect shape (B of every effect pass); `lay_src` the layer's colour. - let (lay_src, vstroke) = match vstroke { - Some(s) => { - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(self.retain(content)); - p.b = Some(self.retain(s)); - p.clip = Some(clip); - let union = self.emit(p); - let m = self.mask_use(layer); - let masked = |me: &mut Self, slot: Slot| match &m { - Some(m) => { - let mut p = Pass::new(Kernel::Mask, 0); - p.a = Some(slot); - p.mask = Some(m.clone()); - p.clip = Some(clip); - me.emit(p) - } - None => slot, - }; - let fill = masked(self, content); - content = masked(self, union); - (fill, Some(masked(self, s))) - } - None => (self.retain(content), None), - }; - if outline { - let mut p = Pass::new(Kernel::FxInit, 0); - p.a = Some(content); - p.adjust_kind = INIT_OUTLINE; - p.map = Some(MapRef { fx, item: SHAPE_MAP, map: 0 }); - p.clip = Some(clip); - content = self.emit(p); - } - - let items: Vec<&'a Effect> = layer.effects.items.iter().filter(|e| e.enabled()).collect(); - for e in &items { - let gradient = match e { - Effect::GradientOverlay { gradient, .. } => Some(gradient), - Effect::Stroke(s) => match &s.paint { - FxPaint::Gradient(g) => Some(g), - _ => None, - }, - Effect::OuterGlow(g) | Effect::InnerGlow(g) => match &g.paint { - FxPaint::Gradient(g) => Some(g), - _ => None, - }, - _ => None, - }; - if let Some(g) = gradient { - check_gradient_stops(&g.stops)?; - check_gradient_stops(&g.opacity_stops)?; - } - } - // Linked patterns tile from the effects reference point (else the layer's top-left). - let anchor = layer.effects.reference.unwrap_or((f64::from(sb.x0), f64::from(sb.y0))); - let vector_shape = matches!(layer.content, LayerContent::Shape(_)) && !outline; - let map = |item: usize, map: usize| MapRef { fx, item, map }; - let rev: Vec<(usize, &'a Effect)> = items.iter().copied().enumerate().rev().collect(); - let init = |s: &mut Self, src: Slot, b: Option, kind: i32, opacity: f32| { - let mut p = Pass::new(Kernel::FxInit, 0); - p.a = Some(src); - p.b = b; - p.adjust_kind = kind; - p.opacity = opacity; - p.clip = Some(clip); - s.emit(p) - }; - - // Exterior effects over the backdrop (an opaque copy of it for clipped layers). - let mut w = if atop { - let b = self.retain(backdrop); - init(self, b, None, INIT_OPAQUE, 1.0) - } else { - self.retain(backdrop) - }; - for &(i, e) in &rev { - if let Effect::DropShadow(s) = e { - // The layer hides the shadow beneath it where its fill is see-through, and wholly - // in a mode that blends with it (as the CPU renderer: `shadow_see_through`). - let flags = if s.knocks_out { F_KNOCKOUT } else { 0 }; - let see_through = photocraft_compose::effects::shadow_see_through(layer); - w = self.paint_k( - w, - content, - Cov::Map(map(i, 0), 0.0), - &Paint::Color(s.color.to_rgb()), - s.common.blend, - s.common.opacity, - flags, - clip, - sb, - see_through, - ); - } - } - for &(i, e) in &rev { - if let Effect::OuterGlow(g) = e { - let paint = self.glow_paint(g, anchor); - // Match CPU exterior knockout: a see-through fill must not reveal the - // glow through its own shape. The shader applies m × (1 − alpha × k). - let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0); - w = self.paint_k(w, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_KNOCKOUT, clip, sb, see_through); - } - } - - // The layer: its colour at fill opacity inside its shape; interior effects are painted - // relative to the shape (coverage within it), then the shape's alpha applies. - let mut l = if relative { - let c = self.retain(content); - init(self, lay_src, Some(c), INIT_RELATIVE, layer.fill_opacity) - } else { - init(self, lay_src, None, INIT_INSIDE, layer.fill_opacity) - }; - // Blend Interior Effects as Group off: the layer merges in its mode first and the - // interior effects paint over the result (`effects::premerges_layer`). - let premerge = photocraft_compose::effects::premerges_layer(layer); - if premerge { - let mut p = Pass::new(Kernel::Blend, 0); - p.a = Some(self.retain(w)); - p.b = Some(l); - p.mode = layer.blend; - p.flags = gamma_flag(layer); - p.extra[3] = photocraft_compose::text_gamma(layer); - p.clip = Some(clip); - l = self.emit(p); - } - for &(_, e) in &rev { - if let Effect::PatternOverlay { common, name, id, scale, angle, link, phase } = e - && let Some(pat) = photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) - { - let paint = Paint::Pattern(pat, placement(anchor, *link, *phase, *scale, *angle)); - l = self.paint(l, content, Cov::One, &paint, common.blend, common.opacity, F_GATE, clip, sb); - } - } - for &(_, e) in &rev { - if let Effect::GradientOverlay { common, gradient, .. } = e { - l = self.paint(l, content, Cov::One, &Paint::Gradient(gradient), common.blend, common.opacity, F_GATE, clip, sb); - } - } - for &(_, e) in &rev { - if let Effect::ColorOverlay { common, color } = e { - l = self.paint(l, content, Cov::One, &Paint::Color(color.to_rgb()), common.blend, common.opacity, F_GATE, clip, sb); - } - } - for &(i, e) in &rev { - if let Effect::Satin(s) = e { - l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &Paint::Color(s.color.to_rgb()), s.common.blend, s.common.opacity, F_REL, clip, sb); - } - } - for &(i, e) in &rev { - if let Effect::InnerGlow(g) = e { - let paint = self.glow_paint(g, anchor); - l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_REL, clip, sb); - } - } - for &(i, e) in &rev { - if let Effect::InnerShadow(s) = e { - l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &Paint::Color(s.color.to_rgb()), s.common.blend, s.common.opacity, F_REL, clip, sb); - } - } - if let Some(s) = vstroke { - let mut p = Pass::new(Kernel::FxInit, 0); - p.a = Some(l); - p.b = Some(self.retain(content)); - p.c = Some(s); - p.adjust_kind = INIT_VSTROKE; - p.opacity = layer.fill_opacity; - p.clip = Some(clip); - l = self.emit(p); - } - // Inside stroke parts are painted over the layer (bottom instance first). - for &(i, e) in &rev { - if let Effect::Stroke(st) = e { - let (in_w, _) = stroke_widths(st); - if in_w > 0.0 { - let paint = self.fx_paint(&st.paint, anchor); - l = self.paint( - l, - content, - Cov::Map(map(i, 1), (in_w + 0.5).clamp(0.0, 1.0)), - &paint, - st.common.blend, - st.common.opacity, - F_GATE, - clip, - photocraft_compose::stroke_frame(self.cx.doc, layer, st).unwrap_or(sb), - ); - } - } - } - let bevel_paint = |b: &photocraft_doc::Bevel| photocraft_compose::effects::bevel_geom(b).paint; - for &(i, e) in &rev { - if let Effect::BevelEmboss(b) = e - && bevel_paint(b) == photocraft_compose::effects::BevelPaint::Inner - { - l = self.paint( - l, - content, - Cov::Map(map(i, 0), 0.0), - &Paint::Color(b.highlight_color.to_rgb()), - b.highlight.blend, - b.highlight.opacity, - F_REL, - clip, - sb, - ); - l = self.paint(l, content, Cov::Map(map(i, 1), 0.0), &Paint::Color(b.shadow_color.to_rgb()), b.shadow.blend, b.shadow.opacity, F_REL, clip, sb); - } - } - // The shape's own alpha. - let c = self.retain(content); - l = init(self, l, Some(c), INIT_ALPHA, 1.0); - // Outside stroke parts lie beneath the layer and blend onto the exterior result with their - // own mode; a higher stroke covers the ones below it (each takes its coverage × opacity - // not yet taken above it, blended over the result as it was before the strokes). - let outs: Vec<(usize, &photocraft_doc::StrokeFx)> = items - .iter() - .copied() - .enumerate() - .filter_map(|(i, e)| if let Effect::Stroke(st) = e { Some((i, st)) } else { None }) - .filter(|(_, st)| stroke_widths(st).1 > 0.0) - .collect(); - if !outs.is_empty() { - let (mut acc, mut cover): (Option, Option) = (None, None); - if outline || !vector_shape { - // The layer's alpha rides along the coverage: `effects::outline_share`, and for - // pixel layers the stroke's share beneath a partly covering layer (fill < 100 %). - let ll = self.retain(l); - cover = Some(init(self, ll, None, INIT_COVER, 1.0)); - } - for (n, &(i, st)) in outs.iter().enumerate() { - let paint = self.fx_paint(&st.paint, anchor); - let flags = F_STROKE_OUT | if vector_shape { F_VECTOR } else { 0 } | if outline { F_OUTLINE } else { 0 } | if n == 0 { F_FIRST } else { 0 }; - // Share × blended colour, then coverage (both read the coverage so far). - let mut p = self.fx_pass_kernel( - Kernel::FxStroke, - w, - content, - &paint, - st.common.blend, - st.common.opacity, - flags, - clip, - photocraft_compose::stroke_frame(self.cx.doc, layer, st).unwrap_or(sb), - ); - if matches!(paint, Paint::None) { - // A missing pattern paints nothing: the stroke's share keeps the result as is. - p.params[2][2] = 3.0; - } - p.adjust_kind = 0; - p.map = Some(map(i, 0)); - p.c = cover.map(|c| self.retain(c)); - p.d = acc; - let a = self.emit(p); - let mut p = self.fx_pass_kernel(Kernel::FxStroke, w, content, &Paint::Color([0.0; 3]), BlendMode::Normal, st.common.opacity, flags, clip, sb); - p.adjust_kind = 1; - p.map = Some(map(i, 0)); - p.c = cover; - cover = Some(self.emit(p)); - acc = Some(a); - } - let mut p = Pass::new(Kernel::FxStrokeEnd, 0); - p.a = Some(w); - p.b = acc; - p.c = cover; - p.clip = Some(clip); - w = self.emit(p); - } - for &(i, e) in &rev { - if let Effect::BevelEmboss(b) = e - && bevel_paint(b) == photocraft_compose::effects::BevelPaint::Outer - { - let k = 0; - w = self.paint( - w, - content, - Cov::Map(map(i, k), 0.0), - &Paint::Color(b.highlight_color.to_rgb()), - b.highlight.blend, - b.highlight.opacity, - 0, - clip, - sb, - ); - w = self.paint(w, content, Cov::Map(map(i, k + 1), 0.0), &Paint::Color(b.shadow_color.to_rgb()), b.shadow.blend, b.shadow.opacity, 0, clip, sb); - } - } - - let mode = if premerge || layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend }; - let late: Vec<(usize, &'a photocraft_doc::Bevel)> = rev - .iter() - .filter_map(|&(i, e)| if let Effect::BevelEmboss(b) = e { Some((i, b)) } else { None }) - .filter(|(_, b)| bevel_paint(b) == photocraft_compose::effects::BevelPaint::Both) - .collect(); - let merged = if late.is_empty() { - let mut p = Pass::new(Kernel::FxMerge, 0); - p.a = Some(w); - p.b = Some(l); - p.c = Some(self.retain(backdrop)); - p.mode = mode; - p.opacity = layer.opacity; - p.flags = if atop { F_ATOP } else { 0 } | gamma_flag(layer); - p.extra[3] = photocraft_compose::text_gamma(layer); - p.clip = Some(clip); - self.emit(p) - } else { - // Emboss styles shade the composited layer (`composite_with_effects`): merge at full - // opacity, paint their maps (inside and outside halves), then mix. - let mut p = Pass::new(Kernel::FxMerge, 0); - p.a = Some(w); - p.b = Some(l); - p.c = Some(self.retain(w)); - p.mode = mode; - p.opacity = 1.0; - p.flags = gamma_flag(layer); - p.extra[3] = photocraft_compose::text_gamma(layer); - p.clip = Some(clip); - let mut m = self.emit(p); - for (i, b) in late { - for (k, color, fxc) in [(0, &b.highlight_color, &b.highlight), (1, &b.shadow_color, &b.shadow)] { - m = self.paint(m, content, Cov::Map(map(i, k), 0.0), &Paint::Color(color.to_rgb()), fxc.blend, fxc.opacity, 0, clip, sb); - } - } - let mut p = Pass::new(Kernel::FxMerge, 0); - p.b = Some(self.retain(m)); - p.a = Some(m); - p.c = Some(self.retain(backdrop)); - p.mode = mode; - p.opacity = layer.opacity; - p.flags = if atop { F_ATOP } else { 0 } | F_NO_LAYER; - p.clip = Some(clip); - self.emit(p) - }; - self.release(content); - - // Write the clipped result back into the backdrop (in place when nothing else holds it). - Ok(self.write_back(backdrop, merged, clip)) - } - - fn fx_paint(&self, p: &'a FxPaint, anchor: (f64, f64)) -> Paint<'a> { - match p { - FxPaint::Color(c) => Paint::Color(c.to_rgb()), - FxPaint::Gradient(g) => Paint::Gradient(g), - FxPaint::Pattern { name, id, scale } => match photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) { - Some(pat) => Paint::Pattern(pat, placement(anchor, true, (0.0, 0.0), *scale, 0.0)), - None => Paint::None, - }, - } - } - - fn glow_paint(&self, g: &'a Glow, anchor: (f64, f64)) -> Paint<'a> { - match &g.paint { - FxPaint::Gradient(gradient) => Paint::GlowGradient(gradient, photocraft_compose::effects::glow_gradient_gain(g.range)), - p => self.fx_paint(p, anchor), - } - } - - /// An effect pass reading `dst` (A, retained) and the layer `content` (B, retained) with its - /// paint set up. - #[allow(clippy::too_many_arguments)] - fn fx_pass(&mut self, dst: Slot, content: Slot, paint: &Paint<'a>, blend: BlendMode, opacity: f32, flags: u32, clip: Rect, sb: Rect) -> Pass<'a> { - self.fx_pass_kernel(Kernel::FxPaint, dst, content, paint, blend, opacity, flags, clip, sb) - } - - #[allow(clippy::too_many_arguments)] - fn fx_pass_kernel( - &mut self, - kernel: Kernel, - dst: Slot, - content: Slot, - paint: &Paint<'a>, - blend: BlendMode, - opacity: f32, - flags: u32, - clip: Rect, - sb: Rect, - ) -> Pass<'a> { - let mut p = Pass::new(kernel, 0); - p.a = Some(self.retain(dst)); - p.b = Some(self.retain(content)); - p.mode = blend; - p.opacity = opacity; - p.flags = flags; - p.clip = Some(clip); - match paint { - Paint::None => {} - Paint::Color(c) => p.color = [c[0], c[1], c[2], 1.0], - Paint::Gradient(g) => { - // effects::paint_fx: whole-pixel end points (fill_layout::gradient_layout). - let (angle, scale, offset) = photocraft_compose::fill_layout::gradient_layout(g.style, g.angle, g.scale, g.offset, sb); - p.params[0] = [angle, scale, if g.reverse { 1.0 } else { 0.0 }, style_index(g.style)]; - p.params[1] = [sb.x0 as f32, sb.y0 as f32, sb.width() as f32, sb.height() as f32]; - p.params[2][0] = offset.0; - p.params[2][1] = offset.1; - p.params[2][2] = 1.0; - p.lut = Some(gradient_rows(g)); - } - Paint::GlowGradient(g, gain) => { - // effects::paint_glow: the gradient at 1 - strength, opaque from 1 / gain. - p.params[0][0] = *gain; - p.params[2][2] = 4.0; - p.lut = Some(gradient_rows(g)); - } - Paint::Pattern(pat, pl) => { - p.params[2][2] = 2.0; - p.params[3] = [pl.origin.0 as f32, pl.origin.1 as f32, pl.cs.0 as f32, pl.cs.1 as f32]; - p.extra = [pl.inv_scale as f32, pat.width as f32, pat.height as f32, 0.0]; - p.pattern = Some(pat); - } - } - p - } - - /// One effect paint into `dst` (consumed); reads the layer's alpha from `content` (kept). - #[allow(clippy::too_many_arguments)] - fn paint(&mut self, dst: Slot, content: Slot, cov: Cov, paint: &Paint<'a>, blend: BlendMode, opacity: f32, flags: u32, clip: Rect, sb: Rect) -> Slot { - self.paint_k(dst, content, cov, paint, blend, opacity, flags, clip, sb, 1.0) - } - - /// [`Self::paint`] with the knockout strength (`F_KNOCKOUT`: coverage × (1 − alpha × k)). - #[allow(clippy::too_many_arguments)] - fn paint_k( - &mut self, - dst: Slot, - content: Slot, - cov: Cov, - paint: &Paint<'a>, - blend: BlendMode, - opacity: f32, - flags: u32, - clip: Rect, - sb: Rect, - knockout: f32, - ) -> Slot { - if matches!(paint, Paint::None) { - // Missing pattern: compose paints nothing. - return dst; - } - let mut p = self.fx_pass(dst, content, paint, blend, opacity, flags, clip, sb); - self.release(dst); - match cov { - Cov::Map(m, outside) => { - p.adjust_kind = 0; - p.map = Some(m); - p.params[2][3] = outside; - } - Cov::One => p.adjust_kind = 1, - } - p.extra[3] = knockout; - self.emit(p) - } -} - -/// Paint source of an effect. -enum Paint<'a> { - None, - Color([f32; 3]), - Gradient(&'a Gradient), - /// A glow's gradient with its opacity gain (`effects::paint_glow`). - GlowGradient(&'a Gradient, f32), - Pattern(&'a Pattern, Placement), -} - -/// `compose::pattern::Placement` (origin, inverse rotation, inverse scale). -#[derive(Clone, Copy, Debug)] -struct Placement { - origin: (f64, f64), - cs: (f64, f64), - inv_scale: f64, -} - -/// `Placement::anchored`: linked patterns tile from `anchor` (the effects reference point). -fn placement(anchor: (f64, f64), link: bool, phase: (f32, f32), scale: f32, angle: f32) -> Placement { - let base = if link { anchor } else { (0.0, 0.0) }; - let origin = (base.0 + f64::from(phase.0), base.1 + f64::from(phase.1)); - let s = f64::from(scale); - let inv_scale = if s.is_finite() && s > 1e-3 { 1.0 / s } else { 1.0 }; - let a = f64::from(angle).to_radians(); - Placement { origin, cs: (a.cos(), a.sin()), inv_scale } -} - -/// (inside width, outside width) of a stroke. -pub fn stroke_widths(st: &photocraft_doc::StrokeFx) -> (f32, f32) { - match st.position { - StrokePosition::Outside => (0.0, st.size), - StrokePosition::Inside => (st.size, 0.0), - StrokePosition::Center => (st.size / 2.0, st.size / 2.0), - } -} - -fn style_index(s: photocraft_doc::GradientStyle) -> f32 { - match s { - photocraft_doc::GradientStyle::Linear => 0.0, - photocraft_doc::GradientStyle::Radial => 1.0, - photocraft_doc::GradientStyle::Angle => 2.0, - photocraft_doc::GradientStyle::Reflected => 3.0, - photocraft_doc::GradientStyle::Diamond => 4.0, - } -} - -fn sample_stops4(stops: &[(f32, [f32; 4])], t: f32) -> [f32; 4] { - match stops { - [] => [0.0; 4], - [only] => only.1, - _ => { - if t <= stops[0].0 { - return stops[0].1; - } - for w in stops.windows(2) { - let (a, b) = (&w[0], &w[1]); - if t <= b.0 { - let k = if b.0 > a.0 { (t - a.0) / (b.0 - a.0) } else { 0.0 }; - return std::array::from_fn(|i| a.1[i] + (b.1[i] - a.1[i]) * k); - } - } - stops[stops.len() - 1].1 - } - } -} - -fn lut_rows(f: impl Fn(usize, f32) -> f32, rows: usize) -> Vec<[f32; 4096]> { - (0..rows) - .map(|r| { - let mut row = [0.0f32; 4096]; - for (k, v) in row.iter_mut().enumerate() { - *v = f(r, k as f32 / 4095.0); - } - row - }) - .collect() -} - -type Program = (i32, [[f32; 4]; 4], Option>); - -/// A CPU LUT (4096 entries) as one texture row. -fn to_row(t: &[f32]) -> [f32; 4096] { - let mut row = [0.0f32; 4096]; - for (o, v) in row.iter_mut().zip(t) { - *o = *v; - } - row -} - -/// Whether the adjustment kernel can evaluate `adj` in a document of colour `mode`: Levels and -/// Curves on CMYK ink or Lab channels convert through ICC profiles per pixel, and so do the -/// adjustments a Lab document evaluates on L, a and b (`photocraft_compose::adjust::lab_channel_adjustment`) -/// and a CMYK document on its inks (`cmyk_channel_adjustment`), which only the CPU does. -pub fn adjustment_on_gpu(adj: &Adjustment, mode: photocraft_color::ColorMode) -> bool { - use photocraft_compose::adjust::{cmyk_channel_adjustment, lab_channel_adjustment}; - let tone_in_space = - matches!(adj, Adjustment::Levels { space: ToneSpace::Cmyk | ToneSpace::Lab, .. } | Adjustment::Curves { space: ToneSpace::Cmyk | ToneSpace::Lab, .. }); - let own_channels = match mode { - photocraft_color::ColorMode::Lab => lab_channel_adjustment(adj), - photocraft_color::ColorMode::Cmyk => cmyk_channel_adjustment(adj), - _ => false, - }; - !tone_in_space && !own_channels -} - -/// A tone transfer as the shader's `t_decode`/`t_encode` exponent (0: the sRGB curve). -fn transfer_exponent(t: Transfer) -> f32 { - match t { - Transfer::Srgb => 0.0, - Transfer::Gamma(g) => g, - } -} - -/// Adjustment → (kernel kind, parameters, LUT rows). Kinds are the `switch` in `adjust()`. -pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraft_color::SampleType) -> Program { - let mut p = [[0.0f32; 4]; 4]; - match adj { - Adjustment::Invert => (1, p, None), - Adjustment::Threshold { level } => { - // The level, the depth's split (compose::adjust::threshold_white) and the level in 16 bits. - let t = (level * 255.0).round(); - p[0] = match depth { - photocraft_color::SampleType::U8 => [t, 1.0, 0.0, 0.0], - photocraft_color::SampleType::U16 => [t, 2.0, (t * 32768.0 / 255.0).round(), 0.0], - photocraft_color::SampleType::F32 => [t, 0.0, 0.0, 0.0], - }; - (2, p, None) - } - Adjustment::Posterize { levels } => { - // The levels, and the 16-bit rule (compose::adjust::posterize_in). - p[0][0] = (*levels).clamp(2, 255) as f32; - p[0][1] = if depth == photocraft_color::SampleType::U16 { 1.0 } else { 0.0 }; - (3, p, None) - } - Adjustment::BrightnessContrast { brightness, contrast, legacy: true } => { - let c = contrast.clamp(-100.0, 99.0); - let k = if c >= 0.0 { 1.0 / (1.0 - c / 100.0) } else { 1.0 + c / 100.0 }; - // Brightness before contrast when it is raised, after it when lowered (compose::adjust). - let (pre, post) = if c >= 0.0 { (brightness / 255.0, 0.0) } else { (0.0, brightness / 255.0) }; - // 32-bit documents pivot at 127 of 255 and don't clip (the shader checks F_HDR). - let pivot = if depth == photocraft_color::SampleType::F32 { 127.0 / 255.0 } else { photocraft_compose::adjust::LEGACY_PIVOT }; - p[0] = [pre, k, pivot, post]; - (4, p, None) - } - Adjustment::BrightnessContrast { brightness, contrast, .. } => { - // Modern B/C: the CPU's lookup table (compose::adjust::modern_lut) on every channel, - // through the Levels / Curves kernel. - let row = to_row(&adjust::modern_lut(*brightness, *contrast)); - (7, p, Some(vec![row; 3])) - } - Adjustment::Exposure { exposure, offset, gamma } => { - p[0] = [2f32.powf(*exposure), *offset, gamma.max(0.01), transfer_exponent(transfer.for_exposure())]; - (6, p, None) - } - Adjustment::Levels { master, per_channel, space: ToneSpace::Rgb, .. } if depth == photocraft_color::SampleType::F32 => { - // Twenty coefficients exceed the sixteen uniform parameters. Store four records - // in LUT row 0, read texel-exact by the analytic float Levels kernel. - let mut row = [0.0f32; 4096]; - for (i, ch) in std::iter::once(master).chain(per_channel).enumerate() { - let coefficients = [ch.in_black, ch.in_white, ch.gamma.max(0.01), ch.out_black, ch.out_white]; - row[i * 5..i * 5 + 5].copy_from_slice(&coefficients); - } - (17, p, Some(vec![row])) - } - // RGB space only (see `adjustment_on_gpu`); the rows are the CPU's channel∘master LUTs. - Adjustment::Levels { .. } | Adjustment::Curves { .. } => { - for (v, identity) in p[0].iter_mut().zip(adjust::float_tone_identity(adj, Some(depth))) { - *v = if identity { 1.0 } else { 0.0 }; - } - (7, p, Some(adjust::tone_luts_depth(adj, Some(depth)).iter().take(3).map(|t| to_row(t)).collect())) - } - Adjustment::HueSaturation { hue, saturation, lightness, colorize, ranges } => { - p[0] = [*hue, saturation / 100.0, lightness / 100.0, if *colorize { 1.0 } else { 0.0 }]; - if !*colorize && ranges.iter().any(|r| !r.is_neutral()) { - // Range edits: hue-indexed rows (shift, saturation, lightness), as on the CPU. - p[1][0] = 1.0; - (8, p, Some(adjust::hue_range_tables(ranges).iter().map(|t| to_row(t)).collect())) - } else { - (8, p, None) - } - } - Adjustment::Vibrance { vibrance, saturation } => { - // compose::adjust::vibrance_px; 32-bit samples aren't clipped at 1. - let hi = if depth == photocraft_color::SampleType::F32 { f32::MAX } else { 1.0 }; - p[0] = [vibrance / 100.0, saturation / 100.0, transfer_exponent(transfer), hi]; - let b = adjust::VIBRANCE_BOOST; - p[1] = [b[0], b[1], b[2], b[3]]; - p[2] = [b[4], b[5], b[6], 0.0]; - (9, p, None) - } - Adjustment::ChannelMixer { matrix, monochrome } => { - // compose::adjust: 32-bit documents cut each percentage to 1/1024, count 255/512 of - // the constant and don't clip (the shader checks F_HDR). - let hdr = depth == photocraft_color::SampleType::F32; - let cut = |v: f32| if hdr { (v * 1024.0).trunc() / 1024.0 } else { v }; - let rows = matrix.map(|r| [cut(r[0]), cut(r[1]), cut(r[2]), if hdr { r[3] * 255.0 / 512.0 } else { r[3] }]); - p[0] = rows[0]; - p[1] = rows[1]; - p[2] = rows[2]; - p[3][0] = if *monochrome { 1.0 } else { 0.0 }; - (10, p, None) - } - Adjustment::PhotoFilter { color, density, preserve_luminosity } => { - // compose::adjust: the linear-light matrix, then SetLum on the encoded values (8/16-bit). - let linear_doc = depth == photocraft_color::SampleType::F32; - let m = adjust::photo_filter_matrix(*color, *density, *preserve_luminosity && linear_doc); - for (row, m) in p.iter_mut().zip(m) { - *row = [m[0], m[1], m[2], 0.0]; - } - p[3] = [if *preserve_luminosity && !linear_doc { 1.0 } else { 0.0 }, transfer_exponent(transfer), 0.0, 0.0]; - (11, p, None) - } - Adjustment::BlackWhite { weights, tint } => { - p[0] = [weights[0], weights[1], weights[2], weights[3]]; - p[1] = [weights[4], weights[5], if tint.is_some() { 1.0 } else { 0.0 }, 0.0]; - if let Some(t) = tint { - p[2] = [t[0], t[1], t[2], 0.0]; - } - (12, p, None) - } - Adjustment::GradientMap { stops, reverse, dither } => { - p[0][0] = if *reverse { 1.0 } else { 0.0 }; - p[0][1] = if *dither { 1.0 } else { 0.0 }; - let s4: Vec<(f32, [f32; 4])> = stops.iter().map(|(t, c)| (*t, [c[0], c[1], c[2], 1.0])).collect(); - let rows = lut_rows(|r, t| if s4.is_empty() { t } else { sample_stops4(&s4, t)[r] }, 3); - (13, p, Some(rows)) - } - Adjustment::ColorBalance { shadows, midtones, highlights, preserve_luminosity } => { - // Per-channel curves (compose::adjust::color_balance_channel), through the Levels kernel. - let tones = [*shadows, *midtones, *highlights]; - (7, p, Some(lut_rows(|ch, v| adjust::color_balance_channel(tones, *preserve_luminosity, ch, v), 3))) - } - Adjustment::SelectiveColor { relative, adjustments } => { - // 9 ranges × CMYK don't fit the 16 params: they go in LUT row 0 (read texel-exact). - p[0][0] = if *relative { 1.0 } else { 0.0 }; - let mut row = [0.0f32; 4096]; - for (i, v) in adjustments.iter().flatten().enumerate() { - row[i] = *v; - } - (15, p, Some(vec![row])) - } - Adjustment::ColorLookup { lut: Some(table), size, tetrahedral, dither, .. } if lut3d_len(*size).is_some_and(|len| table.len() >= len) => { - // The flattened table (n³ RGB triplets) spans as many 4096-wide rows as it needs. - let n = *size as usize; - let rows = lut3d_len(*size) - .and_then(|len| table.get(..len)) - .unwrap_or_default() - .chunks(4096) - .map(|c| { - let mut row = [0.0f32; 4096]; - row[..c.len()].copy_from_slice(c); - row - }) - .collect(); - p[0] = [n as f32, if *tetrahedral { 1.0 } else { 0.0 }, if *dither { 1.0 } else { 0.0 }, 0.0]; - (16, p, Some(rows)) - } - // Identity on the CPU too (not evaluated there). - Adjustment::ColorLookup { .. } | Adjustment::Unsupported { .. } => (0, p, None), - } -} - -/// A gradient's colour and alpha as four 4096-entry LUT rows (`lut_tex`). -fn gradient_rows(g: &Gradient) -> Vec<[f32; 4096]> { - let mut rows = vec![[0.0f32; 4096]; 4]; - for k in 0..4096 { - let v = photocraft_compose::effects::sample_gradient(g, k as f32 / 4095.0); - for (ch, row) in rows.iter_mut().enumerate() { - row[k] = v[ch]; - } - } - rows -} - -/// Linear interpolation between LUT entries cannot preserve a discontinuity. Let the canvas -/// use the CPU oracle for coincident stops, including opacity stops and unsorted fill ramps -/// (which `gradient_fill::Ramp` sorts). Check only ramps we will sample: cached fills and -/// disabled effects can still use the GPU. -fn check_gradient_stops(stops: &[(f32, T)]) -> Result<(), Unsupported> { - let mut positions: Vec = stops.iter().map(|s| s.0).collect(); - positions.sort_by(f32::total_cmp); - // A 4096-entry LUT cannot keep two stops that fall in the same entry apart (#3053). - if positions.windows(2).any(|w| (w[1] - w[0]).abs() < 1.0 / 4095.0) { - return Err(Unsupported("coincident gradient stops (composited on the CPU)".into())); - } - Ok(()) -} - -/// Mode index used by the shader (declaration order of [`BlendMode`]). -pub fn mode_index(m: BlendMode) -> i32 { - match m { - BlendMode::PassThrough => 0, - BlendMode::Normal => 1, - BlendMode::Dissolve => 2, - BlendMode::Darken => 3, - BlendMode::Multiply => 4, - BlendMode::ColorBurn => 5, - BlendMode::LinearBurn => 6, - BlendMode::DarkerColor => 7, - BlendMode::Lighten => 8, - BlendMode::Screen => 9, - BlendMode::ColorDodge => 10, - BlendMode::LinearDodge => 11, - BlendMode::LighterColor => 12, - BlendMode::Overlay => 13, - BlendMode::SoftLight => 14, - BlendMode::HardLight => 15, - BlendMode::VividLight => 16, - BlendMode::LinearLight => 17, - BlendMode::PinLight => 18, - BlendMode::HardMix => 19, - BlendMode::Difference => 20, - BlendMode::Exclusion => 21, - BlendMode::Subtract => 22, - BlendMode::Divide => 23, - BlendMode::Hue => 24, - BlendMode::Saturation => 25, - BlendMode::Color => 26, - BlendMode::Luminosity => 27, - BlendMode::Reflect => 28, - BlendMode::Glow => 29, - BlendMode::Negation => 30, - BlendMode::Xor => 31, - BlendMode::PaintNetColorBurn => 32, - BlendMode::PaintNetColorDodge => 33, - } -} - #[cfg(test)] -mod tests { - use super::*; - use photocraft_color::{Color, ColorMode, SampleType}; - use photocraft_geom::Size; - - #[test] - fn stops_closer_than_one_lut_step_stay_on_the_cpu() { - assert!(check_gradient_stops(&[(0.4999_f32, ()), (0.5, ())]).is_err()); - assert!(check_gradient_stops(&[(0.0_f32, ()), (1.0, ())]).is_ok()); - } - - #[test] - fn lab_channel_adjustments_defer_to_the_cpu_in_lab_documents() { - let lab_rules = [ - Adjustment::Invert, - Adjustment::Posterize { levels: 4 }, - Adjustment::Threshold { level: 0.5 }, - Adjustment::BrightnessContrast { brightness: 10.0, contrast: 5.0, legacy: false }, - Adjustment::GradientMap { stops: vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])], reverse: false, dither: false }, - ]; - for adj in &lab_rules { - assert!(!adjustment_on_gpu(adj, ColorMode::Lab), "{adj:?}"); - for mode in [ColorMode::Rgb, ColorMode::Grayscale] { - assert!(adjustment_on_gpu(adj, mode), "{adj:?} in {mode:?}"); - } - // CMYK documents invert, posterize, threshold and brighten their inks, on the CPU. - let inks = !matches!(adj, Adjustment::GradientMap { .. }); - assert_eq!(adjustment_on_gpu(adj, ColorMode::Cmyk), !inks, "{adj:?} in CMYK"); - } - // CMYK Hue/Saturation works on the inks (on the CPU); Colorize stays on the screen colour. - let ranges = photocraft_doc::adjust::HueRange::defaults(); - let hue = |colorize| Adjustment::HueSaturation { hue: 30.0, saturation: 10.0, lightness: 0.0, colorize, ranges: ranges.clone() }; - assert!(!adjustment_on_gpu(&hue(false), ColorMode::Cmyk) && adjustment_on_gpu(&hue(true), ColorMode::Cmyk)); - assert!(adjustment_on_gpu(&hue(false), ColorMode::Rgb)); - // Screen-colour adjustments stay on the GPU in Lab documents. - assert!(adjustment_on_gpu(&Adjustment::Exposure { exposure: 1.0, offset: 0.0, gamma: 1.0 }, ColorMode::Lab)); - } - - #[test] - fn native_cmyk_fills_fall_back_only_without_a_valid_cache() { - use photocraft_doc::{FillCache, GradientStyle}; - for depth in photocraft_color::SampleType::ALL { - for fill in [Fill::Solid(Color::WHITE), Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false)] { - let mut doc = Document::new("CMYK", photocraft_geom::Size::new(3, 2), photocraft_color::ColorMode::Cmyk, depth); - doc.layers.push(Layer::new("Fill", LayerContent::Fill(fill.clone()))); - assert!(plan(&doc).unwrap_err().0.contains("native CMYK fill")); - doc.layers[0].visible = false; - assert!(plan(&doc).is_ok()); - doc.layers[0].visible = true; - doc.layers[0].fill_cache = Some(FillCache { fill, surface: photocraft_raster::Surface::new(doc.pixel_format()) }); - assert!(plan(&doc).is_ok()); - doc.layers[0].fill_cache.as_mut().unwrap().fill = Fill::Solid(Color::TRANSPARENT); - assert!(plan(&doc).is_err()); - doc.mode = photocraft_color::ColorMode::Rgb; - assert!(plan(&doc).is_ok()); - } - } - } - #[test] - fn outer_glow_plan_knocks_out_see_through_fill() { - use photocraft_doc::{Contour, FxCommon, GlowSource, GlowTechnique}; - - for fill in [0.0, 0.4, 1.0] { - let mut d = Document::with_background("glow", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut layer = Layer::raster("frame", d.pixel_format()); - layer.surface_mut().unwrap().fill_rect(Rect::new(16, 16, 48, 48), &[1.0, 0.0, 0.0, 1.0]); - layer.fill_opacity = fill; - layer.effects.items.push(Effect::OuterGlow(Glow { - common: FxCommon::new(BlendMode::Normal, 1.0), - paint: FxPaint::Color(Color::rgb(1.0, 1.0, 0.0)), - technique: GlowTechnique::Softer, - spread: 0.3, - size: 8.0, - contour: Contour::Linear, - anti_alias: false, - range: 0.5, - jitter: 0.0, - noise: 0.0, - source: GlowSource::Edge, - })); - d.layers.push(layer); - let planned = plan(&d).unwrap(); - let glow = planned - .passes - .iter() - .find(|pass| { - pass.kernel == Kernel::FxPaint && pass.flags & F_KNOCKOUT != 0 && pass.color[0] > 0.9 && pass.color[1] > 0.9 && pass.color[2] < 0.1 - }) - .expect("outer glow paint pass"); - assert!((glow.extra[3] - (1.0 - fill)).abs() < 1e-6, "fill {fill}"); - } - } - - #[test] - fn color_lookup_with_an_overflowing_stored_size_uploads_no_table() { - for size in [1 << 22, u32::MAX] { - let a = Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false }; - let (kind, _, rows) = adjustment_program(&a, Transfer::Srgb, SampleType::U8); - assert_eq!((kind, rows.is_none()), (0, true), "size {size}"); - } - } - - #[test] - fn root_checkpoints_have_only_one_live_input_and_keep_clips_whole() { - for mode in [BlendMode::Normal, BlendMode::PassThrough] { - let mut d = Document::with_background("t", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut clip = Layer::new("clip", LayerContent::Fill(Fill::Solid(Color::rgba(0.2, 0.3, 0.7, 0.4)))); - clip.clipped = true; - let mut group = Layer::group( - "group", - vec![ - Layer::new("fill", LayerContent::Fill(Fill::Solid(Color::rgba(0.9, 0.1, 0.3, 0.6)))), - Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)), - ], - ); - group.blend = mode; - d.layers.extend([group, clip, Layer::new("top", LayerContent::Adjustment(Adjustment::Invert))]); - let plan = plan(&d).unwrap(); - assert_eq!(plan.checkpoints.iter().map(|c| c.layer_end).collect::>(), vec![1, 3, 4]); - for checkpoint in &plan.checkpoints { - let mut written = std::collections::HashSet::from([checkpoint.slot]); - for pass in &plan.passes[checkpoint.pass_end..] { - for input in [pass.a, pass.b, pass.c, pass.d].into_iter().flatten() { - assert!(written.contains(&input), "a suffix must not need another saved slot: {checkpoint:?}"); - } - written.insert(pass.dst); - } - assert!(written.contains(&plan.root)); - } - } - } - - #[test] - fn slots_are_recycled() { - let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); - for i in 0..10 { - d.layers.push(Layer::raster(format!("l{i}"), d.pixel_format())); - } - let p = plan(&d).unwrap(); - // Empty layers draw nothing. - assert_eq!(p.passes.len(), 1 + 2); - for (i, l) in d.layers.iter_mut().skip(1).enumerate() { - l.surface_mut().unwrap().fill_rect(Rect::new(i as i32 % 4, 0, 4, 8), &[0.0, 0.0, 1.0, 1.0]); - } - let p = plan(&d).unwrap(); - assert!(p.slots <= 3, "{} slots", p.slots); - // The background, then each small layer over its bounds and copied back. - assert_eq!(p.passes.len(), 1 + 2 + 10 * 3); - } - - #[test] - fn small_layers_composite_only_over_their_bounds() { - let mut d = Document::with_background("t", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut l = Layer::raster("small", d.pixel_format()); - l.surface_mut().unwrap().fill_rect(Rect::new(10, 12, 20, 30), &[1.0, 0.0, 0.0, 1.0]); - let mut group = Layer::group("g", vec![l]); - group.blend = BlendMode::Normal; - d.layers.push(group); - let p = plan(&d).unwrap(); - let r = Rect::new(10, 12, 20, 30); - // The group's content, its blend and the copy back are all limited to the layer. - assert!(p.passes.iter().filter(|p| matches!(p.kernel, Kernel::Content | Kernel::Blend)).skip(2).all(|p| p.clip == Some(r)), "{:?}", p.passes); - let last = p.passes.last().unwrap(); - assert_eq!((last.kernel, last.dst, last.clip), (Kernel::CopyRect, p.root, Some(r))); - // A layer that covers most of the canvas composites everywhere. - d.layers[1].children_mut().unwrap()[0].surface_mut().unwrap().fill_rect(Rect::new(0, 0, 60, 60), &[1.0, 0.0, 0.0, 1.0]); - let p = plan(&d).unwrap(); - assert!(p.passes.iter().all(|p| p.clip.is_none())); - } - - #[test] - fn effects_are_planned_in_place() { - let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut l = Layer::raster("fx", d.pixel_format()); - l.surface_mut().unwrap().fill_rect(Rect::new(2, 2, 5, 5), &[1.0, 0.0, 0.0, 1.0]); - l.effects.items.push(photocraft_doc::Effect::ColorOverlay { common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), color: Color::WHITE }); - l.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); - d.layers.push(l); - let p = plan(&d).unwrap(); - assert_eq!(p.fx.len(), 1); - let m = photocraft_compose::effects::margin(&d.layers[1]); - assert_eq!(p.fx[0].region, Rect::new(2, 2, 5, 5).inflate(m).intersect(&d.bounds().inflate(m))); - // The effect result lands back in the backdrop slot. - let last = p.passes.last().unwrap(); - assert_eq!(last.kernel, Kernel::CopyRect); - assert_eq!(last.dst, p.root); - assert!(p.slots <= 6, "{} slots", p.slots); - } - - #[test] - fn opaque_fill_layers_skip_what_they_cover() { - let opaque = || Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 30.0, 1.0, photocraft_doc::GradientStyle::Linear, false); - let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); - for i in 0..4 { - // Small painted layers (empty ones plan no passes at all). - let mut l = Layer::raster(format!("l{i}"), d.pixel_format()); - l.surface_mut().unwrap().fill_rect(Rect::new(i, i, i + 2, i + 2), &[0.0, 0.0, 1.0, 1.0]); - d.layers.push(l); - } - d.layers.push(Layer::new("g", LayerContent::Fill(opaque()))); - let mut top = Layer::raster("top", d.pixel_format()); - top.clipped = true; - d.layers.push(top); - // Clear, then the fill and its clipped layer: the background and four rasters are skipped. - let skipped = plan(&d).unwrap().passes.len(); - let mut alone = d.clone(); - alone.layers.drain(..5); - assert_eq!(skipped, plan(&alone).unwrap().passes.len()); - // Anything that lets the layers below show through keeps them. - let see_through: [fn(&mut Layer); 6] = [ - |l| l.opacity = 0.99, - |l| l.fill_opacity = 0.5, - |l| l.blend = BlendMode::Multiply, - |l| l.visible = false, - |l| l.excluded_channels = 1, - |l| { - if let LayerContent::Fill(Fill::Gradient { opacity_stops, .. }) = &mut l.content { - opacity_stops.push((1.0, 0.5)); - } - }, - ]; - for (k, f) in see_through.iter().enumerate() { - let mut d2 = d.clone(); - f(&mut d2.layers[5]); - assert!(plan(&d2).unwrap().passes.len() > skipped, "case {k}: the layers below are planned"); - assert!(!photocraft_compose::occludes_below(&d2.layers[5], ColorMode::Rgb), "case {k}"); - } - let mut translucent = d.clone(); - translucent.layers[5].content = LayerContent::Fill(Fill::Solid(Color::rgba(1.0, 0.0, 0.0, 0.5))); - assert!(!photocraft_compose::occludes_below(&translucent.layers[5], ColorMode::Rgb)); - assert!(photocraft_compose::occludes_below(&d.layers[5], ColorMode::Rgb)); - } - - #[test] - fn blend_if_plans_on_the_gpu() { - let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut l = Layer::raster("bi", d.pixel_format()); - l.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[1.0, 0.0, 0.0, 1.0]); - l.blend_if.set(0, [photocraft_doc::BlendRange { black: [40, 40], white: [255, 255] }, photocraft_doc::BlendRange::FULL]); - d.layers.push(l.clone()); - let blend_if = |d: &Document| plan(d).unwrap().passes.iter().filter(|p| p.flags & F_BLEND_IF != 0).count(); - assert_eq!(blend_if(&d), 1); - // Clipped layers too. - l.clipped = true; - d.layers[1].blend_if = Default::default(); - d.layers.push(l); - assert_eq!(blend_if(&d), 1); - // Outside RGB, Grayscale and Duotone the setting round-trips without applying. - d.mode = ColorMode::Lab; - assert_eq!(blend_if(&d), 0); - } - - #[test] - fn blend_if_params_pack_every_range_exactly() { - let mut bi = photocraft_doc::BlendIf::default(); - bi.set(3, [photocraft_doc::BlendRange { black: [255, 255], white: [0, 1] }, photocraft_doc::BlendRange { black: [12, 200], white: [201, 254] }]); - let f: Vec = blend_if_params(&bi).iter().flatten().copied().collect(); - // Entry 3 (B), this layer then underlying; unset entries are full ranges. - assert_eq!(&f[12..16], &[255.0 * 256.0 + 255.0, 1.0, 12.0 * 256.0 + 200.0, 201.0 * 256.0 + 254.0]); - assert_eq!(&f[0..2], &[0.0, 255.0 * 256.0 + 255.0]); - } - - #[test] - fn advanced_blending_falls_back_to_the_cpu() { - use photocraft_doc::Knockout; - let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); - let mut l = Layer::raster("ko", d.pixel_format()); - l.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[1.0, 0.0, 0.0, 1.0]); - d.layers.push(l.clone()); - assert!(plan(&d).is_ok(), "defaults stay on the GPU"); - // Switches with nothing to act on (no effects, no clipped layers) stay on the GPU too. - d.layers[1].advanced.blend_interior = true; - d.layers[1].advanced.blend_clipped = false; - assert!(plan(&d).is_ok()); - d.layers[1].advanced.knockout = Knockout::Deep; - assert!(plan(&d).unwrap_err().0.contains("Advanced Blending")); - // Inside groups and clipping groups. - d.layers[1].advanced.knockout = Knockout::None; - let mut c = l.clone(); - c.clipped = true; - d.layers.push(c.clone()); - assert!(plan(&d).is_err(), "clipped layers blended individually"); - d.layers[1].advanced.blend_clipped = true; - assert!(plan(&d).is_ok()); - d.layers[2].advanced.knockout = Knockout::Shallow; - assert!(plan(&d).is_err(), "clipped knockout"); - d.layers.truncate(1); - let mut inner = l; - inner.advanced.knockout = Knockout::Shallow; - d.layers.push(Layer::group("g", vec![inner])); - assert!(plan(&d).is_err(), "knockout inside a group"); - } -} +mod tests; diff --git a/crates/gpu/src/plan/lower.rs b/crates/gpu/src/plan/lower.rs new file mode 100644 index 0000000000..a38cdde867 --- /dev/null +++ b/crates/gpu/src/plan/lower.rs @@ -0,0 +1,344 @@ +use super::types::Unsupported; +use super::{Adjustment, BlendMode, Fill, Gradient, Layer, LayerContent, Pattern, StrokePosition, ToneSpace, Transfer, adjust, lut3d_len}; + +/// Paint source of an effect. +pub(super) enum Paint<'a> { + None, + Color([f32; 3]), + Gradient(&'a Gradient), + /// A glow's gradient with its opacity gain (`effects::paint_glow`). + GlowGradient(&'a Gradient, f32), + Pattern(&'a Pattern, Placement), +} + +/// `compose::pattern::Placement` (origin, inverse rotation, inverse scale). +#[derive(Clone, Copy, Debug)] +pub(super) struct Placement { + pub(super) origin: (f64, f64), + pub(super) cs: (f64, f64), + pub(super) inv_scale: f64, +} + +/// `Placement::anchored`: linked patterns tile from `anchor` (the effects reference point). +pub(super) fn placement(anchor: (f64, f64), link: bool, phase: (f32, f32), scale: f32, angle: f32) -> Placement { + let base = if link { anchor } else { (0.0, 0.0) }; + let origin = (base.0 + f64::from(phase.0), base.1 + f64::from(phase.1)); + let s = f64::from(scale); + let inv_scale = if s.is_finite() && s > 1e-3 { 1.0 / s } else { 1.0 }; + let a = f64::from(angle).to_radians(); + Placement { origin, cs: (a.cos(), a.sin()), inv_scale } +} + +/// (inside width, outside width) of a stroke. +pub fn stroke_widths(st: &photocraft_doc::StrokeFx) -> (f32, f32) { + match st.position { + StrokePosition::Outside => (0.0, st.size), + StrokePosition::Inside => (st.size, 0.0), + StrokePosition::Center => (st.size / 2.0, st.size / 2.0), + } +} + +pub(super) fn style_index(s: photocraft_doc::GradientStyle) -> f32 { + match s { + photocraft_doc::GradientStyle::Linear => 0.0, + photocraft_doc::GradientStyle::Radial => 1.0, + photocraft_doc::GradientStyle::Angle => 2.0, + photocraft_doc::GradientStyle::Reflected => 3.0, + photocraft_doc::GradientStyle::Diamond => 4.0, + } +} + +fn sample_stops4(stops: &[(f32, [f32; 4])], t: f32) -> [f32; 4] { + match stops { + [] => [0.0; 4], + [only] => only.1, + _ => { + if t <= stops[0].0 { + return stops[0].1; + } + for w in stops.windows(2) { + let (a, b) = (&w[0], &w[1]); + if t <= b.0 { + let k = if b.0 > a.0 { (t - a.0) / (b.0 - a.0) } else { 0.0 }; + return std::array::from_fn(|i| a.1[i] + (b.1[i] - a.1[i]) * k); + } + } + stops[stops.len() - 1].1 + } + } +} + +fn lut_rows(f: impl Fn(usize, f32) -> f32, rows: usize) -> Vec<[f32; 4096]> { + (0..rows) + .map(|r| { + let mut row = [0.0f32; 4096]; + for (k, v) in row.iter_mut().enumerate() { + *v = f(r, k as f32 / 4095.0); + } + row + }) + .collect() +} + +type Program = (i32, [[f32; 4]; 4], Option>); + +/// A CPU LUT (4096 entries) as one texture row. +fn to_row(t: &[f32]) -> [f32; 4096] { + let mut row = [0.0f32; 4096]; + for (o, v) in row.iter_mut().zip(t) { + *o = *v; + } + row +} + +/// Whether the adjustment kernel can evaluate `adj` in a document of colour `mode`: Levels and +/// Curves on CMYK ink or Lab channels convert through ICC profiles per pixel, and so do the +/// adjustments a Lab document evaluates on L, a and b (`photocraft_compose::adjust::lab_channel_adjustment`) +/// and a CMYK document on its inks (`cmyk_channel_adjustment`), which only the CPU does. +pub fn adjustment_on_gpu(adj: &Adjustment, mode: photocraft_color::ColorMode) -> bool { + use photocraft_compose::adjust::{cmyk_channel_adjustment, lab_channel_adjustment}; + let tone_in_space = + matches!(adj, Adjustment::Levels { space: ToneSpace::Cmyk | ToneSpace::Lab, .. } | Adjustment::Curves { space: ToneSpace::Cmyk | ToneSpace::Lab, .. }); + let own_channels = match mode { + photocraft_color::ColorMode::Lab => lab_channel_adjustment(adj), + photocraft_color::ColorMode::Cmyk => cmyk_channel_adjustment(adj), + _ => false, + }; + !tone_in_space && !own_channels +} + +/// A tone transfer as the shader's `t_decode`/`t_encode` exponent (0: the sRGB curve). +fn transfer_exponent(t: Transfer) -> f32 { + match t { + Transfer::Srgb => 0.0, + Transfer::Gamma(g) => g, + } +} + +/// Adjustment → (kernel kind, parameters, LUT rows). Kinds are the `switch` in `adjust()`. +pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraft_color::SampleType, mode: photocraft_color::ColorMode) -> Program { + let mut p = [[0.0f32; 4]; 4]; + match adj { + Adjustment::Invert => (1, p, None), + Adjustment::Threshold { level } => { + // The level, the depth's split (compose::adjust::threshold_white) and the level in 16 bits. + let t = (level * 255.0).round(); + p[0] = match depth { + photocraft_color::SampleType::U8 => [t, 1.0, 0.0, 0.0], + photocraft_color::SampleType::U16 => [t, 2.0, (t * 32768.0 / 255.0).round(), 0.0], + photocraft_color::SampleType::F32 => [t, 0.0, 0.0, 0.0], + }; + (2, p, None) + } + Adjustment::Posterize { levels } => { + // The levels, and the 16-bit rule (compose::adjust::posterize_in). + p[0][0] = (*levels).clamp(2, 255) as f32; + p[0][1] = if depth == photocraft_color::SampleType::U16 { 1.0 } else { 0.0 }; + (3, p, None) + } + Adjustment::BrightnessContrast { brightness, contrast, legacy: true } => { + let c = contrast.clamp(-100.0, 99.0); + let k = if c >= 0.0 { 1.0 / (1.0 - c / 100.0) } else { 1.0 + c / 100.0 }; + // Brightness before contrast when it is raised, after it when lowered (compose::adjust). + let (pre, post) = if c >= 0.0 { (brightness / 255.0, 0.0) } else { (0.0, brightness / 255.0) }; + // 32-bit documents pivot at 127 of 255 and don't clip (the shader checks F_HDR). + let pivot = if depth == photocraft_color::SampleType::F32 { 127.0 / 255.0 } else { photocraft_compose::adjust::LEGACY_PIVOT }; + p[0] = [pre, k, pivot, post]; + (4, p, None) + } + Adjustment::BrightnessContrast { brightness, contrast, .. } => { + // Modern B/C: the CPU's lookup table (compose::adjust::modern_lut) on every channel, + // through the Levels / Curves kernel. + let row = to_row(&adjust::modern_lut(*brightness, *contrast)); + (7, p, Some(vec![row; 3])) + } + Adjustment::Exposure { exposure, offset, gamma } => { + p[0] = [2f32.powf(*exposure), *offset, gamma.max(0.01), transfer_exponent(transfer.for_exposure())]; + (6, p, None) + } + Adjustment::Levels { master, per_channel, space: ToneSpace::Rgb, .. } if depth == photocraft_color::SampleType::F32 => { + // Twenty coefficients exceed the sixteen uniform parameters. Store four records + // in LUT row 0, read texel-exact by the analytic float Levels kernel. + let mut row = [0.0f32; 4096]; + for (i, ch) in std::iter::once(master).chain(per_channel).enumerate() { + let coefficients = [ch.in_black, ch.in_white, ch.gamma.max(0.01), ch.out_black, ch.out_white]; + row[i * 5..i * 5 + 5].copy_from_slice(&coefficients); + } + (17, p, Some(vec![row])) + } + // RGB space only (see `adjustment_on_gpu`); the rows are the CPU's channel∘master LUTs. + Adjustment::Levels { .. } | Adjustment::Curves { .. } => { + for (v, identity) in p[0].iter_mut().zip(adjust::float_tone_identity(adj, Some(depth))) { + *v = if identity { 1.0 } else { 0.0 }; + } + // p0.w: continue the curve past 0..1 (32-bit RGB Curves, #7157). + p[0][3] = if adjust::float_tone_extrapolates(adj, Some(depth), mode) { 1.0 } else { 0.0 }; + (7, p, Some(adjust::tone_luts_depth(adj, Some(depth)).iter().take(3).map(|t| to_row(t)).collect())) + } + Adjustment::HueSaturation { hue, saturation, lightness, colorize, ranges } => { + let sat = adjust::colorize_saturation(saturation / 100.0, lightness / 100.0, *colorize, Some(depth)); + p[0] = [*hue, sat, lightness / 100.0, if *colorize { 1.0 } else { 0.0 }]; + if !*colorize && ranges.iter().any(|r| !r.is_neutral()) { + // Range edits: hue-indexed rows (shift, saturation, lightness), as on the CPU. + p[1][0] = 1.0; + (8, p, Some(adjust::hue_range_tables(ranges).iter().map(|t| to_row(t)).collect())) + } else { + (8, p, None) + } + } + Adjustment::Vibrance { vibrance, saturation } => { + // compose::adjust::vibrance_px; 32-bit samples aren't clipped at 1. + let hi = if depth == photocraft_color::SampleType::F32 { f32::MAX } else { 1.0 }; + p[0] = [vibrance / 100.0, saturation / 100.0, transfer_exponent(transfer), hi]; + let b = adjust::VIBRANCE_BOOST; + p[1] = [b[0], b[1], b[2], b[3]]; + p[2] = [b[4], b[5], b[6], 0.0]; + (9, p, None) + } + Adjustment::ChannelMixer { matrix, monochrome } => { + // compose::adjust: 32-bit documents cut each percentage to 1/1024, count 255/512 of + // the constant and don't clip (the shader checks F_HDR). + let hdr = depth == photocraft_color::SampleType::F32; + let cut = |v: f32| if hdr { (v * 1024.0).trunc() / 1024.0 } else { v }; + let rows = matrix.map(|r| [cut(r[0]), cut(r[1]), cut(r[2]), if hdr { r[3] * 255.0 / 512.0 } else { r[3] }]); + p[0] = rows[0]; + p[1] = rows[1]; + p[2] = rows[2]; + p[3][0] = if *monochrome { 1.0 } else { 0.0 }; + (10, p, None) + } + Adjustment::PhotoFilter { color, density, preserve_luminosity } => { + // compose::adjust: the linear-light matrix, then SetLum on the encoded values (8/16-bit). + let linear_doc = depth == photocraft_color::SampleType::F32; + let m = adjust::photo_filter_matrix(*color, *density, *preserve_luminosity && linear_doc); + for (row, m) in p.iter_mut().zip(m) { + *row = [m[0], m[1], m[2], 0.0]; + } + p[3] = [if *preserve_luminosity && !linear_doc { 1.0 } else { 0.0 }, transfer_exponent(transfer), 0.0, 0.0]; + (11, p, None) + } + Adjustment::BlackWhite { weights, tint } => { + p[0] = [weights[0], weights[1], weights[2], weights[3]]; + p[1] = [weights[4], weights[5], if tint.is_some() { 1.0 } else { 0.0 }, 0.0]; + if let Some(t) = tint { + p[2] = [t[0], t[1], t[2], 0.0]; + } + (12, p, None) + } + Adjustment::GradientMap { stops, reverse, dither } => { + p[0][0] = if *reverse { 1.0 } else { 0.0 }; + p[0][1] = if *dither { 1.0 } else { 0.0 }; + let s4: Vec<(f32, [f32; 4])> = stops.iter().map(|(t, c)| (*t, [c[0], c[1], c[2], 1.0])).collect(); + let rows = lut_rows(|r, t| if s4.is_empty() { t } else { sample_stops4(&s4, t)[r] }, 3); + (13, p, Some(rows)) + } + Adjustment::ColorBalance { shadows, midtones, highlights, preserve_luminosity } => { + // Per-channel curves (compose::adjust::BalanceCurve), through the Levels kernel. + let curves = adjust::BalanceCurve::rgb([*shadows, *midtones, *highlights], *preserve_luminosity); + (7, p, Some(lut_rows(|ch, v| curves[ch].eval(v), 3))) + } + Adjustment::SelectiveColor { relative, adjustments } => { + // 9 ranges × CMYK don't fit the 16 params: they go in LUT row 0 (read texel-exact). + p[0][0] = if *relative { 1.0 } else { 0.0 }; + let mut row = [0.0f32; 4096]; + for (i, v) in adjustments.iter().flatten().enumerate() { + row[i] = *v; + } + (15, p, Some(vec![row])) + } + Adjustment::ColorLookup { lut: Some(table), size, tetrahedral, dither, .. } if lut3d_len(*size).is_some_and(|len| table.len() >= len) => { + // The flattened table (n³ RGB triplets) spans as many 4096-wide rows as it needs. + let n = *size as usize; + let rows = lut3d_len(*size) + .and_then(|len| table.get(..len)) + .unwrap_or_default() + .chunks(4096) + .map(|c| { + let mut row = [0.0f32; 4096]; + row[..c.len()].copy_from_slice(c); + row + }) + .collect(); + p[0] = [n as f32, if *tetrahedral { 1.0 } else { 0.0 }, if *dither { 1.0 } else { 0.0 }, 0.0]; + (16, p, Some(rows)) + } + // Identity on the CPU too (not evaluated there). + Adjustment::ColorLookup { .. } | Adjustment::Unsupported { .. } => (0, p, None), + } +} + +/// A gradient's colour and alpha as four 4096-entry LUT rows (`lut_tex`). +pub(super) fn gradient_rows(g: &Gradient) -> Vec<[f32; 4096]> { + let mut rows = vec![[0.0f32; 4096]; 4]; + for k in 0..4096 { + let v = photocraft_compose::effects::sample_gradient(g, k as f32 / 4095.0); + for (ch, row) in rows.iter_mut().enumerate() { + row[k] = v[ch]; + } + } + rows +} + +/// Linear interpolation between LUT entries cannot preserve a discontinuity. Let the canvas +/// use the CPU oracle for coincident stops, including opacity stops and unsorted fill ramps +/// (which `gradient_fill::Ramp` sorts). Check only ramps we will sample: cached fills and +/// disabled effects can still use the GPU. +pub(super) fn check_gradient_stops(stops: &[(f32, T)]) -> Result<(), Unsupported> { + let mut positions: Vec = stops.iter().map(|s| s.0).collect(); + positions.sort_by(f32::total_cmp); + // A 4096-entry LUT cannot keep two stops that fall in the same entry apart (#3053). + if positions.windows(2).any(|w| (w[1] - w[0]).abs() < 1.0 / 4095.0) { + return Err(Unsupported("coincident gradient stops (composited on the CPU)".into())); + } + Ok(()) +} + +/// Mode index used by the shader (declaration order of [`BlendMode`]). +pub fn mode_index(m: BlendMode) -> i32 { + match m { + BlendMode::PassThrough => 0, + BlendMode::Normal => 1, + BlendMode::Dissolve => 2, + BlendMode::Darken => 3, + BlendMode::Multiply => 4, + BlendMode::ColorBurn => 5, + BlendMode::LinearBurn => 6, + BlendMode::DarkerColor => 7, + BlendMode::Lighten => 8, + BlendMode::Screen => 9, + BlendMode::ColorDodge => 10, + BlendMode::LinearDodge => 11, + BlendMode::LighterColor => 12, + BlendMode::Overlay => 13, + BlendMode::SoftLight => 14, + BlendMode::HardLight => 15, + BlendMode::VividLight => 16, + BlendMode::LinearLight => 17, + BlendMode::PinLight => 18, + BlendMode::HardMix => 19, + BlendMode::Difference => 20, + BlendMode::Exclusion => 21, + BlendMode::Subtract => 22, + BlendMode::Divide => 23, + BlendMode::Hue => 24, + BlendMode::Saturation => 25, + BlendMode::Color => 26, + BlendMode::Luminosity => 27, + BlendMode::Reflect => 28, + BlendMode::Glow => 29, + BlendMode::Negation => 30, + BlendMode::Xor => 31, + BlendMode::PaintNetColorBurn => 32, + BlendMode::PaintNetColorDodge => 33, + } +} + +/// Whether `Planner::content` draws `layer` as one `Planner::content_pass`: not a group's stack, +/// an adjustment, or a pattern fill painted in passes. +pub(super) fn single_content_pass(layer: &Layer) -> bool { + match &layer.content { + LayerContent::Group(_) | LayerContent::Adjustment(_) => false, + LayerContent::Fill(f @ Fill::Pattern { .. }) => layer.fill_cache.as_ref().is_some_and(|c| c.fill == *f), + _ => true, + } +} diff --git a/crates/gpu/src/plan/planner.rs b/crates/gpu/src/plan/planner.rs new file mode 100644 index 0000000000..ef0924640a --- /dev/null +++ b/crates/gpu/src/plan/planner.rs @@ -0,0 +1,1426 @@ +use super::lower::{ + Paint, adjustment_on_gpu, adjustment_program, check_gradient_stops, gradient_rows, placement, single_content_pass, stroke_widths, style_index, +}; +use super::types::{Checkpoint, DocCtx, FxLayer, Kernel, MapRef, MaskUse, Pass, Role, SurfaceRef, TexUse, Unsupported}; +use super::{Adjustment, BlendMode, Effect, Fill, FxPaint, Glow, Layer, LayerContent, LayerId, Plan, Rect, Slot, Surface, bounds, has_effects}; + +pub(super) struct Planner<'a> { + passes: Vec>, + free: Vec, + refs: Vec, + cx: DocCtx<'a>, + fx: Vec>, + checkpoints: Vec, + /// Layers planned so far, counting a layer each time it is planned (a clipped pass-through + /// group is planned twice). + visits: usize, +} + +/// Most layer plannings one document may take: a pass-through group with clipped layers is +/// planned twice, so groups nested in one another double the work at every level. +const MAX_PLAN_VISITS: usize = 100_000; + +/// Coverage source of an effect paint (`kind` in `fs_fxpaint`); flags refine it. +#[derive(Clone, Copy)] +enum Cov { + /// An effect map; `outside` beyond its region. + Map(MapRef, f32), + /// Full coverage. + One, +} + +struct EffectState<'a> { + layer: &'a Layer, + items: Vec<&'a Effect>, + rev: Vec<(usize, &'a Effect)>, + fx: usize, + content: Slot, + lay_src: Slot, + vstroke: Option, + backdrop: Slot, + w: Slot, + l: Slot, + clip: Rect, + sb: Rect, + anchor: (f64, f64), + outline: bool, + relative: bool, + vector_shape: bool, + atop: bool, + premerge: bool, +} + +/// Shader flags for effect passes (keep in sync with compose.wgsl). Coverage `m` of a paint, with +/// `a` the layer's alpha and `inside` = `a > 0.5/255`: +/// knockout `m × (1 − a × k)` (k in `p4.w`); gate `inside ? m : 0`; rel `inside ? min(m / a, 1) : 0`; +/// stroke-out `inside ? (vector ? 0 : (1 − a) / (1 − layer alpha), 1 at an opaque layer) : m`. +pub const F_KNOCKOUT: u32 = 16; + +/// Effect merge: A already holds the layer; only mix with the backdrop by the layer's opacity. +pub const F_NO_LAYER: u32 = 32; + +/// Outside strokes along a filled shape's outline (`effects::outline_share`; the coverage slot +/// carries the layer's alpha in `.g`). +pub const F_OUTLINE: u32 = 131072; + +/// Shape layer (outside strokes never show inside it). +pub const F_VECTOR: u32 = 64; + +/// Merge onto an opaque clipping base, keeping its alpha. +pub const F_ATOP: u32 = 128; + +pub const F_GATE: u32 = 256; + +pub const F_REL: u32 = 512; + +pub const F_STROKE_OUT: u32 = 1024; + +/// First outside stroke (no accumulated share yet). +pub const F_FIRST: u32 = 2048; + +/// `Lerp` per channel: weights in `p0` (channel restrictions), no opacity or mask. +pub const F_CHANNELS: u32 = 4096; + +/// Lab document: Normal blending mixes in CIELAB (`psblend::LAB_MIX`). +pub const F_LAB: u32 = 65536; + +/// 32-bit float document: Add, Divide and Exposure retain values above 1; the non-separable +/// blend modes clip only below 0 (`psblend::HDR`). +pub const F_HDR: u32 = 262144; + +/// `Lerp`: A rounded to `p0.x` steps per unit (adjustment results on integer documents). +pub const F_QUANT: u32 = 32768; + +/// `Lerp` as A + (B − C) premultiplied (layers clipped to pass-through groups). +pub const F_ADD_DIFF: u32 = 16384; + +/// `Lerp`: B mixed back towards A (the backdrop) by the layer's Blend If weight +/// (`compose::apply_blend_if`), from C ("This Layer": an adjustment layer's unmixed result; B when +/// `p4.y` is set) +/// and A ("Underlying Layer"). The ranges are packed in `p0..p3` (`blend_if_params`); `p4.x` marks +/// a one-channel document. +pub const F_BLEND_IF: u32 = 524288; + +/// Blend / Atop / FxMerge of a type layer: coverage mixed at `psblend::TEXT_GAMMA`. +pub const F_TEXT_GAMMA: u32 = 8192; + +/// `FxMerge`: B is the layer's shape (its content); the layer is its colour at fill opacity +/// (`p0.x`) inside the shape, times the shape's alpha: `FxInit` `INIT_INSIDE` then +/// `INIT_ALPHA` in the merge, for a layer with no interior effects between them. +pub const F_LAYER_SHAPE: u32 = 1_048_576; + +/// A layer's Blend If ranges for `F_BLEND_IF`: Gray, then the colour channels, each as +/// [this layer, underlying layer]; every range as two floats, `black low × 256 + black high` and +/// `white low × 256 + white high` (exact in f32), so entry `e`, side `s` starts at float +/// `(e × 2 + s) × 2`. Entries past the fourth never apply (RGB has four, one-channel modes two). +pub(super) fn blend_if_params(bi: &photocraft_doc::BlendIf) -> [[f32; 4]; 4] { + let mut f = [0.0f32; 16]; + for e in 0..4 { + for (s, r) in bi.get(e).iter().enumerate() { + let i = (e * 2 + s) * 2; + f[i] = f32::from(r.black[0]) * 256.0 + f32::from(r.black[1]); + f[i + 1] = f32::from(r.white[0]) * 256.0 + f32::from(r.white[1]); + } + } + [[f[0], f[1], f[2], f[3]], [f[4], f[5], f[6], f[7]], [f[8], f[9], f[10], f[11]], [f[12], f[13], f[14], f[15]]] +} + +/// `F_TEXT_GAMMA` for type layers while text gamma blending is on; the gamma goes in `p4.w`. +fn gamma_flag(layer: &Layer) -> u32 { + if photocraft_compose::text_gamma(layer) != 1.0 { F_TEXT_GAMMA } else { 0 } +} + +/// `FxInit` kinds: A at `opacity` × alpha; an opaque copy of A; A's colour with alpha `opacity` +/// inside B's shape; A with alpha × B's alpha. +pub const INIT_SCALE: i32 = 0; + +pub const INIT_OPAQUE: i32 = 1; + +pub const INIT_INSIDE: i32 = 2; + +pub const INIT_ALPHA: i32 = 3; + +/// The vector stroke C over A relative to the shape B, at `opacity`. +pub const INIT_VSTROKE: i32 = 4; + +/// A's colour with alpha `opacity` × A's alpha relative to B's. +pub const INIT_RELATIVE: i32 = 5; + +/// Outside-stroke coverage seed: (0, A's alpha). +pub const INIT_COVER: i32 = 6; + +/// A with its alpha joined with the effect shape (the map). +pub const INIT_OUTLINE: i32 = 7; + +/// `MapRef::item` of the layer's effect shape (`compose::layer_shape`) instead of an effect map. +pub const SHAPE_MAP: usize = usize::MAX; + +fn check_effect_gradients(items: &[&Effect]) -> Result<(), Unsupported> { + for effect in items { + let gradient = match effect { + Effect::GradientOverlay { gradient, .. } => Some(gradient), + Effect::Stroke(stroke) => match &stroke.paint { + FxPaint::Gradient(g) => Some(g), + _ => None, + }, + Effect::OuterGlow(glow) | Effect::InnerGlow(glow) => match &glow.paint { + FxPaint::Gradient(g) => Some(g), + _ => None, + }, + _ => None, + }; + if let Some(gradient) = gradient { + check_gradient_stops(&gradient.stops)?; + check_gradient_stops(&gradient.opacity_stops)?; + } + } + Ok(()) +} + +impl<'a> Planner<'a> { + pub(super) fn new(cx: DocCtx<'a>) -> Self { + Planner { passes: Vec::new(), free: Vec::new(), refs: Vec::new(), cx, fx: Vec::new(), checkpoints: Vec::new(), visits: 0 } + } + + pub(super) fn finish(self, root: Slot) -> Plan<'a> { + Plan { passes: self.passes, slots: self.refs.len() as u32, root, fx: self.fx, checkpoints: self.checkpoints } + } + + fn alloc(&mut self) -> Slot { + if let Some(s) = self.free.pop() { + self.refs[s as usize] = 1; + return s; + } + self.refs.push(1); + (self.refs.len() - 1) as Slot + } + fn retain(&mut self, s: Slot) -> Slot { + self.refs[s as usize] += 1; + s + } + fn release(&mut self, s: Slot) { + let r = &mut self.refs[s as usize]; + *r -= 1; + if *r == 0 { + self.free.push(s); + } + } + /// Emit `pass` into a fresh slot, releasing its inputs. + fn emit(&mut self, mut pass: Pass<'a>) -> Slot { + let dst = self.alloc(); + pass.dst = dst; + if self.cx.mode == photocraft_color::ColorMode::Lab { + pass.flags |= F_LAB; + } + if self.cx.depth == photocraft_color::SampleType::F32 { + pass.flags |= F_HDR; + } + let (a, b, c, d) = (pass.a, pass.b, pass.c, pass.d); + self.passes.push(pass); + for s in [a, b, c, d].into_iter().flatten() { + self.release(s); + } + dst + } + pub(super) fn clear(&mut self) -> Slot { + self.emit(Pass::new(Kernel::Clear, 0)) + } + + /// composite_stack: returns the new backdrop (consumes `backdrop`). + fn stack(&mut self, layers: &'a [Layer], backdrop: Slot) -> Result { + self.stack_inner(layers, backdrop, None) + } + + pub(super) fn stack_inner(&mut self, layers: &'a [Layer], mut backdrop: Slot, root_start: Option) -> Result { + let mut i = 0; + while i < layers.len() { + let base = &layers[i]; + let mut j = i + 1; + while j < layers.len() && layers[j].clipped && !base.clipped { + j += 1; + } + let clipped = &layers[i + 1..j]; + if base.visible { + backdrop = self.layer(base, clipped, backdrop)?; + if let (LayerContent::Adjustment(_), Some(q)) = (&base.content, photocraft_compose::adjustment_quantum(self.cx.depth)) { + // compose: adjustment results are rounded to the document's depth. + let mut p = Pass::new(Kernel::Lerp, 0); + p.a = Some(backdrop); + p.flags = F_QUANT; + p.params[0] = [q, 0.0, 0.0, 0.0]; + backdrop = self.emit(p); + } + } + i = j.max(i + 1); + if let Some(start) = root_start + && self.refs.iter().enumerate().all(|(slot, &refs)| refs == u32::from(slot == backdrop as usize)) + { + self.checkpoints.push(Checkpoint { layer_start: start, layer_end: start.saturating_add(i), pass_end: self.passes.len(), slot: backdrop }); + } + } + Ok(backdrop) + } + + fn mask_use(&self, layer: &'a Layer) -> Option> { + if let Some(c) = photocraft_compose::masks::combined_mask(layer, self.cx.canvas) { + let default = c.default_pixel().first().copied().unwrap_or(1.0); + return Some(MaskUse { layer: layer.id, surface: SurfaceRef::Derived(std::sync::Arc::new(c)), density: 1.0, default }); + } + let m = layer.mask.as_ref()?; + if !m.enabled { + return None; + } + Some(MaskUse { + layer: layer.id, + surface: SurfaceRef::Doc(&m.surface), + density: m.density, + default: m.surface.default_pixel().first().copied().unwrap_or(1.0), + }) + } + + /// `compose::apply_blend_if`: `after` (the backdrop with `layer` drawn) mixed back towards + /// `before` (the backdrop without it) where the layer's Blend If ranges hide it. "This Layer" + /// is the layer's own content with its masks; an adjustment layer is judged by its result at + /// full strength, before Opacity and its mask (#9542). Consumes both slots. + fn blend_if(&mut self, layer: &'a Layer, before: Slot, after: Slot) -> Result { + let own = match &layer.content { + LayerContent::Adjustment(adj) => { + let b = self.retain(before); + self.adjust(adj, b)? + } + _ => self.content(layer)?, + }; + let own = Some(own); + let mut p = Pass::new(Kernel::Lerp, 0); + p.a = Some(before); + p.b = Some(after); + p.c = own; + p.flags = F_BLEND_IF; + p.params = blend_if_params(&layer.blend_if); + let one_channel = self.cx.mode != photocraft_color::ColorMode::Rgb; + p.extra = [f32::from(u8::from(one_channel)), f32::from(u8::from(own.is_none())), 0.0, 0.0]; + Ok(self.emit(p)) + } + + /// Channel restrictions and Blend If around `draw`, which composites `layer` onto `backdrop` + /// (`compose::composite_layer` / `composite_atop`: both are relative to the same backdrop). + fn restricted(&mut self, layer: &'a Layer, backdrop: Slot, draw: impl FnOnce(&mut Self, Slot) -> Result) -> Result { + let w = photocraft_compose::channel_weights(layer, self.cx.mode); + let blend_if = photocraft_compose::blend_if_active(layer, self.cx.mode); + if w.is_none() && !blend_if { + return draw(self, backdrop); + } + let before = self.retain(backdrop); + let mut after = draw(self, backdrop)?; + if let Some(w) = w { + let b = if blend_if { self.retain(before) } else { before }; + after = self.restore_channels(b, after, w); + } + if blend_if { + after = self.blend_if(layer, before, after)?; + } + Ok(after) + } + + /// Blending Options › Advanced Blending (knockout, clipped layers blended individually, blend + /// interior effects as group, transparency shapes off, masks hiding effects) has no GPU pass: + /// documents using it are composited on the CPU (`photocraft_compose::advanced`). Layers at + /// Photoshop's defaults pass this check for free. + fn check_advanced(&self, layer: &Layer, clipped: &[Layer]) -> Result<(), Unsupported> { + if photocraft_compose::advanced_active(layer, clipped) { + return Err(Unsupported(format!("Advanced Blending on `{}` (composited on the CPU)", layer.name))); + } + Ok(()) + } + + /// composite_layer, honouring the layer's channel restrictions. + fn layer(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { + self.visits += 1; + if self.visits > MAX_PLAN_VISITS { + return Err(Unsupported("too many nested layers to plan for the GPU (composited on the CPU)".into())); + } + self.check_advanced(layer, clipped)?; + self.restricted(layer, backdrop, |s, b| s.layer_any(layer, clipped, b)) + } + + /// `compose::restore_channels`: `after` with the channels a layer leaves out taken from + /// `before` (consumes both). + fn restore_channels(&mut self, before: Slot, after: Slot, w: [f32; 3]) -> Slot { + let mut p = Pass::new(Kernel::Lerp, 0); + p.a = Some(before); + p.b = Some(after); + p.flags = F_CHANNELS; + p.params[0] = [w[0], w[1], w[2], 1.0]; + self.emit(p) + } + + fn layer_any(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { + match layer.artboard() { + Some(ab) => self.artboard(layer, ab, clipped, backdrop), + None => self.layer_plain(layer, clipped, backdrop), + } + } + + /// compose::composite_artboard: the board's background and the group composited over the + /// backdrop, kept only inside the board (contents and effects outside it are clipped away). + fn artboard(&mut self, layer: &'a Layer, ab: &photocraft_doc::Artboard, clipped: &'a [Layer], backdrop: Slot) -> Result { + let board = ab.rect.intersect(&self.cx.canvas); + if board.is_empty() { + return Ok(backdrop); + } + let before = self.retain(backdrop); + let mut sub = backdrop; + if let Some(bg) = ab.background.rgba() { + let mut c = Pass::new(Kernel::Content, 0); + c.color = bg; + let c = self.emit(c); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(sub); + p.b = Some(c); + sub = self.emit(p); + } + let sub = self.layer_plain(layer, clipped, sub)?; + let dst = if self.refs[before as usize] == 1 { + before + } else { + let n = self.alloc(); + let mut p = Pass::new(Kernel::CopyFull, n); + p.a = Some(before); + self.passes.push(p); + self.release(before); + n + }; + let mut p = Pass::new(Kernel::CopyRect, dst); + p.a = Some(sub); + p.clip = Some(board); + self.passes.push(p); + self.release(sub); + Ok(dst) + } + + fn layer_plain(&mut self, layer: &'a Layer, clipped: &'a [Layer], backdrop: Slot) -> Result { + let opacity = layer.opacity * layer.fill_opacity; + let visible_clipped: Vec<&'a Layer> = clipped.iter().filter(|c| c.visible).collect(); + + // Below 100% fill a pass-through group renders isolated (as the CPU compositor does), and + // so does one that is the base of a clipping group and holds an adjustment layer, or one + // with Blend If (#9554). + let clip_base_adjusts = !visible_clipped.is_empty() + && matches!(&layer.content, LayerContent::Group(g) if g.children.iter().any(|c| c.visible && matches!(c.content, LayerContent::Adjustment(_)))); + if let LayerContent::Group(g) = &layer.content + && layer.blend == BlendMode::PassThrough + && layer.fill_opacity >= 1.0 + && !has_effects(layer) + && !clip_base_adjusts + && !photocraft_compose::blend_if_active(layer, self.cx.mode) + { + let before = self.retain(backdrop); + let mut after = self.stack(&g.children, backdrop)?; + if opacity < 1.0 || layer.mask.is_some() || layer.vector_mask.is_some() { + let mut p = Pass::new(Kernel::Lerp, 0); + p.a = Some(self.retain(before)); + p.b = Some(after); + p.opacity = opacity; + p.mask = self.mask_use(layer); + after = self.emit(p); + } + // A Frame's stroke over the frame's content, at the group's opacity (compose). + if let Some(stroke) = photocraft_compose::shape_split::frame_stroke(layer, self.cx.canvas, self.cx.depth) { + let stroke = self.shape_part(layer, Role::Stroke, stroke); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(after); + p.b = Some(stroke); + p.opacity = opacity; + after = self.emit(p); + } + if visible_clipped.is_empty() { + self.release(before); + return Ok(after); + } + // Layers clipped to a pass-through group (compose: their effect on the group's + // isolated rendering, each placed over the original backdrop, is added to the + // pass-through result, premultiplied). + let iso = self.content(layer)?; + let iso_keep = self.retain(iso); + let mut clipped_iso = iso; + for c in visible_clipped { + clipped_iso = self.atop(c, clipped_iso)?; + } + let over = |s: &mut Self, src: Slot, before: Slot| { + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(before); + p.b = Some(src); + p.opacity = opacity; + s.emit(p) + }; + let b2 = self.retain(before); + let without = over(self, iso_keep, b2); + let with = over(self, clipped_iso, before); + let mut p = Pass::new(Kernel::Lerp, 0); + p.a = Some(after); + p.b = Some(with); + p.c = Some(without); + p.flags = F_ADD_DIFF; + return Ok(self.emit(p)); + } + + if let LayerContent::Adjustment(adj) = &layer.content { + let before = self.retain(backdrop); + let mut adjusted = self.adjust(adj, backdrop)?; + for c in visible_clipped { + adjusted = self.atop(c, adjusted)?; + } + let mut p = Pass::new(Kernel::AdjMix, 0); + p.a = Some(before); + p.b = Some(adjusted); + p.mode = layer.blend; + (p.opacity, p.fill) = adjustment_fill(layer, opacity); + p.mask = self.mask_use(layer); + return Ok(self.emit(p)); + } + + if has_effects(layer) { + return self.effects(layer, &visible_clipped, backdrop, false); + } + + if let LayerContent::Shape(sh) = &layer.content + && !visible_clipped.is_empty() + && let Some((fill, stroke)) = photocraft_compose::shape_split::split(sh, self.cx.canvas, self.cx.depth) + { + // The vector stroke goes above the clipped layers: the fill is the clipping base, + // the stroke is laid over the clipped result, then the masks apply to both + // (compose::shape_parts). + let mut content = self.shape_part(layer, Role::Content, fill); + for c in visible_clipped { + content = self.atop(c, content)?; + } + let stroke = self.shape_part(layer, Role::Stroke, stroke); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(content); + p.b = Some(stroke); + let mut content = self.emit(p); + if let Some(m) = self.mask_use(layer) { + let mut p = Pass::new(Kernel::Mask, 0); + p.a = Some(content); + p.mask = Some(m); + content = self.emit(p); + } + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(backdrop); + p.b = Some(content); + p.mode = layer.blend; + p.opacity = layer.opacity; + p.fill = layer.fill_opacity; + return Ok(self.emit(p)); + } + + // A layer that covers little of the canvas composites only over its bounds and is + // copied back into the backdrop (#125): a layout with hundreds of small text and shape + // layers otherwise shades every layer over every chunk. + let clip = self.local_clip(layer); + if clip.is_some_and(|c| c.is_empty()) { + // Nothing to draw (an empty layer, or a group of them). + for c in &visible_clipped { + self.check_advanced(c, &[])?; + } + return Ok(backdrop); + } + // A layer with nothing clipped to it whose content is one Content pass is blended + // straight from its pixels (`Kernel::LayerBlend`): one pass instead of two, and no + // scratch chunk written and read back. + let mut p = if visible_clipped.is_empty() && single_content_pass(layer) { + let mut p = self.content_pass(layer)?; + p.kernel = Kernel::LayerBlend; + p + } else { + let start = self.passes.len(); + let mut content = self.content(layer)?; + for c in visible_clipped { + content = self.atop(c, content)?; + } + if let Some(clip) = clip { + // Everything that built the layer's content only matters inside its bounds. + for p in &mut self.passes[start..] { + if p.clip.is_none() && !matches!(p.kernel, Kernel::Clear | Kernel::CopyFull) { + p.clip = Some(clip); + } + } + } + let mut p = Pass::new(Kernel::Blend, 0); + p.b = Some(content); + p + }; + p.a = Some(if clip.is_some() { self.retain(backdrop) } else { backdrop }); + p.mode = layer.blend; + p.opacity = layer.opacity; + p.fill = layer.fill_opacity; + p.flags |= gamma_flag(layer); + p.extra[3] = photocraft_compose::text_gamma(layer); + p.clip = clip; + let merged = self.emit(p); + match clip { + Some(clip) => Ok(self.write_back(backdrop, merged, clip)), + None => Ok(merged), + } + } + + /// The rect a plain layer composites over: its composite bounds when they cover at most half + /// the canvas (beyond that, compositing everywhere costs less than the copy back). + fn local_clip(&self, layer: &Layer) -> Option { + let canvas = self.cx.canvas; + let b = bounds::composite_bounds(layer, canvas)?; + (b.width() as u64 * b.height() as u64 * 2 <= canvas.width() as u64 * canvas.height() as u64).then_some(b) + } + + /// Copy `merged` into `backdrop` over `clip` (in place when nothing else holds the backdrop); + /// returns the new backdrop. Consumes both. + fn write_back(&mut self, backdrop: Slot, merged: Slot, clip: Rect) -> Slot { + // A clip over the whole canvas left no pixel of `merged` uncomputed: it is the backdrop. + if clip.contains_rect(&self.cx.canvas) { + self.release(backdrop); + return merged; + } + let dst = if self.refs[backdrop as usize] == 1 { + backdrop + } else { + let n = self.alloc(); + let mut p = Pass::new(Kernel::CopyFull, n); + p.a = Some(backdrop); + self.passes.push(p); + self.release(backdrop); + n + }; + let mut p = Pass::new(Kernel::CopyRect, dst); + p.a = Some(merged); + p.clip = Some(clip); + self.passes.push(p); + self.release(merged); + dst + } + + /// A stroked shape's fill or vector stroke alone (`compose::shape_split`), unmasked. + fn shape_part(&mut self, layer: &'a Layer, role: Role, surface: Surface) -> Slot { + let mut p = Pass::new(Kernel::Content, 0); + p.color = photocraft_raster::to_rgba(&surface.format(), &surface.default_pixel()); + if surface.tile_count() > 0 { + p.tex = Some(TexUse { layer: layer.id, role, surface: SurfaceRef::Derived(std::sync::Arc::new(surface)) }); + } + self.emit(p) + } + + /// render_content for non-adjustment layers. + fn content(&mut self, layer: &'a Layer) -> Result { + match &layer.content { + LayerContent::Group(g) => { + let empty = self.clear(); + let mut s = self.stack(&g.children, empty)?; + if layer.mask.is_some() || layer.vector_mask.is_some() { + let mut p = Pass::new(Kernel::Mask, 0); + p.a = Some(s); + p.mask = self.mask_use(layer); + s = self.emit(p); + } + // A Frame's stroke over its masked content (compose::render_content). + if let Some(stroke) = photocraft_compose::shape_split::frame_stroke(layer, self.cx.canvas, self.cx.depth) { + let stroke = self.shape_part(layer, Role::Stroke, stroke); + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(s); + p.b = Some(stroke); + s = self.emit(p); + } + Ok(s) + } + // Adjustments are handled by the caller and never get here. + LayerContent::Adjustment(_) => Err(Unsupported("adjustment layer reached the content planner".into())), + LayerContent::Fill(f @ Fill::Pattern { name, scale, id, angle, link, phase }) if layer.fill_cache.as_ref().is_none_or(|c| c.fill != *f) => { + // compose::render_fill: the pattern tiled from the layer's frame (transparent + // when missing), then the layer's masks. + let empty = self.clear(); + let Some(pat) = photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) else { return Ok(empty) }; + let frame = photocraft_compose::fill_frame(layer, self.cx.canvas); + let anchor = if frame.is_empty() { (0.0, 0.0) } else { (f64::from(frame.x0), f64::from(frame.y0)) }; + let paint = Paint::Pattern(pat, placement(anchor, *link, *phase, *scale, *angle)); + let canvas = self.cx.canvas; + let s = self.paint(empty, empty, Cov::One, &paint, BlendMode::Normal, 1.0, 0, canvas, canvas); + if layer.mask.is_some() || layer.vector_mask.is_some() { + let mut p = Pass::new(Kernel::Mask, 0); + p.a = Some(s); + p.mask = self.mask_use(layer); + return Ok(self.emit(p)); + } + Ok(s) + } + _ => { + let p = self.content_pass(layer)?; + Ok(self.emit(p)) + } + } + } + + /// The Content pass of a raster / text / shape / smart / fill layer. + fn content_pass(&self, layer: &'a Layer) -> Result, Unsupported> { + let mut p = Pass::new(Kernel::Content, 0); + p.mask = self.mask_use(layer); + match &layer.content { + LayerContent::Fill(f) => match &layer.fill_cache { + Some(c) if c.fill == *f => self.surface_tex(&mut p, layer.id, &c.surface), + _ => self.fill(&mut p, f, photocraft_compose::fill_frame(layer, self.cx.canvas))?, + }, + _ => { + if let Some(s) = layer.surface() { + self.surface_tex(&mut p, layer.id, s); + } + } + } + Ok(p) + } + + fn surface_tex(&self, p: &mut Pass<'a>, id: LayerId, s: &'a Surface) { + let dp = s.default_pixel(); + p.color = photocraft_raster::to_rgba(&s.format(), &dp); + if s.tile_count() > 0 { + p.tex = Some(TexUse { layer: id, role: Role::Content, surface: SurfaceRef::Doc(s) }); + } + } + + fn fill(&self, p: &mut Pass<'a>, f: &Fill, frame: photocraft_geom::Rect) -> Result<(), Unsupported> { + // The RGB shader cannot interpolate or dither native ink channels. + if self.cx.mode == photocraft_color::ColorMode::Cmyk && !matches!(f, Fill::Pattern { .. }) { + return Err(Unsupported("native CMYK fill (composited on the CPU)".into())); + } + match f { + Fill::Solid(c) => { + let rgb = c.to_rgb(); + p.color = [rgb[0], rgb[1], rgb[2], c.alpha]; + } + Fill::Gradient { stops, opacity_stops, angle, scale, style, reverse, offset, dither, .. } => { + check_gradient_stops(stops)?; + check_gradient_stops(opacity_stops)?; + p.gradient = true; + // compose::render_fill: whole-pixel end points (fill_layout). + let (angle, scale, offset) = photocraft_compose::fill_layout::gradient_layout(*style, *angle, *scale, *offset, frame); + p.params[0] = [angle, scale, if *reverse { 1.0 } else { 0.0 }, style_index(*style)]; + let c = frame; + p.params[1] = [c.x0 as f32, c.y0 as f32, c.width() as f32, c.height() as f32]; + // p2.xy: centre offset; p2.z: the depth's levels a dithered pixel is rounded to + // (0: float, compose::gradient_fill::render_quantized); p2.w: dither (the shared + // position hash, see the shader). + let quantum = photocraft_compose::adjustment_quantum(self.cx.depth).unwrap_or(0.0); + p.params[2] = [offset.0, offset.1, quantum, if *dither { 1.0 } else { 0.0 }]; + let ramp = photocraft_compose::gradient_fill::Ramp::new(f); + let mut rows = vec![[0.0f32; 4096]; 4]; + for k in 0..4096 { + let v = ramp.as_ref().map_or([0.0; 4], |r| r.sample(k as f32 / 4095.0)); + for (ch, row) in rows.iter_mut().enumerate() { + row[k] = v[ch]; + } + } + p.lut = Some(rows); + } + // Pattern fills fall back to the CPU compositor (see `check`). + Fill::Pattern { .. } => {} + } + Ok(()) + } + + /// composite_atop: `layer` onto `base`, restricted to the base's alpha, honouring the + /// layer's channel restrictions. + fn atop(&mut self, layer: &'a Layer, base: Slot) -> Result { + self.check_advanced(layer, &[])?; + self.restricted(layer, base, |s, b| s.atop_any(layer, b)) + } + + fn atop_any(&mut self, layer: &'a Layer, base: Slot) -> Result { + let opacity = layer.opacity * layer.fill_opacity; + if let LayerContent::Adjustment(adj) = &layer.content { + let before = self.retain(base); + let adjusted = self.adjust(adj, base)?; + let mut p = Pass::new(Kernel::AdjMix, 0); + p.a = Some(before); + p.b = Some(adjusted); + p.mode = layer.blend; + // A clipped adjustment takes Fill as coverage in every mode. + (p.opacity, p.fill) = (opacity, 1.0); + p.mask = self.mask_use(layer); + return Ok(self.emit(p)); + } + if has_effects(layer) { + return self.effects(layer, &[], base, true); + } + let content = self.content(layer)?; + let mut p = Pass::new(Kernel::Atop, 0); + p.a = Some(base); + p.b = Some(content); + p.mode = layer.blend; + p.opacity = opacity; + p.flags = gamma_flag(layer); + p.extra[3] = photocraft_compose::text_gamma(layer); + Ok(self.emit(p)) + } + + /// adjust::apply_with on a slot (consumes it). + fn adjust(&mut self, adj: &Adjustment, src: Slot) -> Result { + if !adjustment_on_gpu(adj, self.cx.mode) { + return Err(Unsupported(format!("{} on CMYK/Lab channels (evaluated on the CPU)", adj.label()))); + } + if let Adjustment::GradientMap { stops, .. } = adj { + check_gradient_stops(stops)?; + } + let mut p = Pass::new(Kernel::Adjust, 0); + p.a = Some(src); + let (kind, params, lut) = adjustment_program(adj, self.cx.transfer, self.cx.depth, self.cx.mode); + p.adjust_kind = kind; + p.params = params; + p.lut = lut; + Ok(self.emit(p)) + } + + /// Plans enabled layer effects in the same phase order as the CPU compositor. + /// Consumes `backdrop`. + fn effects(&mut self, layer: &'a Layer, clipped: &[&'a Layer], backdrop: Slot, atop: bool) -> Result { + let items: Vec<&'a Effect> = layer.effects.items.iter().filter(|e| e.enabled()).collect(); + check_effect_gradients(&items)?; + let canvas = self.cx.canvas; + let region = bounds::effect_region(layer, canvas); + let sb = photocraft_compose::paint_bounds(layer).unwrap_or_else(|| bounds::layer_bounds(layer, canvas)); + let clip = if bounds::transparent_outside(layer) { region } else { canvas }; + // A stroked shape's vector stroke goes above its clipped layers and interior effects + // (compose::split_parts): the fill and the stroke unmasked, the masks applied after. + let split = match &layer.content { + LayerContent::Shape(sh) if sh.stroke.is_some() => photocraft_compose::shape_split::split(sh, canvas, self.cx.depth), + _ => None, + }; + let (mut content, vstroke) = match split { + Some((fill, stroke)) => { + let f = self.shape_part(layer, Role::Content, fill); + let s = self.shape_part(layer, Role::Stroke, stroke); + (f, Some(s)) + } + None => (self.content(layer)?, None), + }; + for p in self.passes.iter_mut().rev().take(if vstroke.is_some() { 2 } else { 1 }) { + if !matches!(layer.content, LayerContent::Group(_)) { + p.clip = Some(clip); + } + } + for c in clipped { + content = self.atop(c, content)?; + } + let fx = self.fx.len(); + self.fx.push(FxLayer { layer, region, bounds: sb }); + let outline = photocraft_compose::effect_outline(layer).is_some(); + let relative = outline || vstroke.is_some(); + // `content` becomes the effect shape (B of every effect pass); `lay_src` the layer's colour. + let (lay_src, vstroke) = match vstroke { + Some(s) => { + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(self.retain(content)); + p.b = Some(self.retain(s)); + p.clip = Some(clip); + let union = self.emit(p); + let m = self.mask_use(layer); + let masked = |me: &mut Self, slot: Slot| match &m { + Some(m) => { + let mut p = Pass::new(Kernel::Mask, 0); + p.a = Some(slot); + p.mask = Some(m.clone()); + p.clip = Some(clip); + me.emit(p) + } + None => slot, + }; + let fill = masked(self, content); + content = masked(self, union); + (fill, Some(masked(self, s))) + } + None => (self.retain(content), None), + }; + if outline { + let mut p = Pass::new(Kernel::FxInit, 0); + p.a = Some(content); + p.adjust_kind = INIT_OUTLINE; + p.map = Some(MapRef { fx, item: SHAPE_MAP, map: 0 }); + p.clip = Some(clip); + content = self.emit(p); + } + + // Linked patterns tile from the effects reference point (else the layer's top-left). + let premerge = photocraft_compose::effects::premerges_layer(layer); + let anchor = layer.effects.reference.unwrap_or((f64::from(sb.x0), f64::from(sb.y0))); + let vector_shape = matches!(layer.content, LayerContent::Shape(_)) && !outline; + let rev = items.iter().copied().enumerate().rev().collect(); + let mut work = EffectState { + layer, + items, + rev, + fx, + content, + lay_src, + vstroke, + backdrop, + w: backdrop, + l: lay_src, + clip, + sb, + anchor, + outline, + relative, + vector_shape, + atop, + premerge, + }; + if let Some(merged) = self.effect_exterior(&mut work) { + self.release(work.content); + return Ok(merged); + } + self.effect_layer_base(&mut work); + self.effect_paints(&mut work); + self.effect_vector_stroke(&mut work); + self.effect_inside_strokes(&mut work); + self.effect_inner_bevel(&mut work); + self.effect_alpha(&mut work); + self.effect_outside_strokes(&mut work); + self.effect_outer_bevel(&mut work); + let merged = self.effect_merge(&mut work); + self.release(work.content); + Ok(self.write_back(work.backdrop, merged, work.clip)) + } + + fn effect_exterior(&mut self, work: &mut EffectState<'a>) -> Option { + let layer = work.layer; + let items = work.items.clone(); + let rev = work.rev.clone(); + let fx = work.fx; + let content = work.content; + let lay_src = work.lay_src; + let backdrop = work.backdrop; + let clip = work.clip; + let sb = work.sb; + let atop = work.atop; + let relative = work.relative; + let premerge = work.premerge; + let anchor = work.anchor; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + // Exterior effects over the backdrop (an opaque copy of it for clipped layers). + let mut w = if atop { + let b = self.retain(backdrop); + self.fx_init(clip, b, None, INIT_OPAQUE, 1.0) + } else { + self.retain(backdrop) + }; + for &(i, e) in &rev { + if let Effect::DropShadow(s) = e { + // The layer hides the shadow beneath it where its fill is see-through, and wholly + // in a mode that blends with it (as the CPU renderer: `shadow_see_through`). + let flags = if s.knocks_out { F_KNOCKOUT } else { 0 }; + let see_through = photocraft_compose::effects::shadow_see_through(layer); + w = self.paint_k( + w, + content, + Cov::Map(map(i, 0), 0.0), + &Paint::Color(s.color.to_rgb()), + s.common.blend, + s.common.opacity, + flags, + clip, + sb, + see_through, + ); + } + } + for &(i, e) in &rev { + if let Effect::OuterGlow(g) = e { + let paint = self.glow_paint(g, anchor); + // Match CPU exterior knockout: a see-through fill must not reveal the + // glow through its own shape. The shader applies m × (1 − alpha × k). + let see_through = 1.0 - layer.fill_opacity.clamp(0.0, 1.0); + w = self.paint_k(w, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_KNOCKOUT, clip, sb, see_through); + } + } + + // Only exterior effects (drop shadows, outer glows): nothing is painted between the + // layer's colour inside its shape and the shape's alpha, so the merge derives both from + // the shape (`F_LAYER_SHAPE`) instead of two passes building the layer. + if !relative && !premerge && items.iter().all(|e| matches!(e, Effect::DropShadow(_) | Effect::OuterGlow(_))) { + let mut p = Pass::new(Kernel::FxMerge, 0); + p.a = Some(w); + p.b = Some(lay_src); + p.c = Some(self.retain(backdrop)); + p.mode = if layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend }; + p.opacity = layer.opacity; + p.flags = if atop { F_ATOP } else { 0 } | gamma_flag(layer) | F_LAYER_SHAPE; + p.params[0][0] = layer.fill_opacity; + p.extra[3] = photocraft_compose::text_gamma(layer); + p.clip = Some(clip); + let merged = self.emit(p); + let merged = self.write_back(backdrop, merged, clip); + return Some(merged); + } + work.w = w; + None + } + + fn effect_layer_base(&mut self, work: &mut EffectState<'a>) { + let layer = work.layer; + let content = work.content; + let lay_src = work.lay_src; + let w = work.w; + let clip = work.clip; + let relative = work.relative; + let premerge = work.premerge; + // The layer: its colour at fill opacity inside its shape; interior effects are painted + // relative to the shape (coverage within it), then the shape's alpha applies. + let mut l = if relative { + let c = self.retain(content); + self.fx_init(clip, lay_src, Some(c), INIT_RELATIVE, layer.fill_opacity) + } else { + self.fx_init(clip, lay_src, None, INIT_INSIDE, layer.fill_opacity) + }; + // Blend Interior Effects as Group off: the layer merges in its mode first and the + // interior effects paint over the result (`effects::premerges_layer`). + if premerge { + let mut p = Pass::new(Kernel::Blend, 0); + p.a = Some(self.retain(w)); + p.b = Some(l); + p.mode = layer.blend; + p.flags = gamma_flag(layer); + p.extra[3] = photocraft_compose::text_gamma(layer); + p.clip = Some(clip); + l = self.emit(p); + } + work.l = l; + } + + fn effect_paints(&mut self, work: &mut EffectState<'a>) { + let rev = work.rev.clone(); + let fx = work.fx; + let content = work.content; + let mut l = work.l; + let clip = work.clip; + let sb = work.sb; + let anchor = work.anchor; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + for &(_, e) in &rev { + if let Effect::PatternOverlay { common, name, id, scale, angle, link, phase } = e + && let Some(pat) = photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) + { + let paint = Paint::Pattern(pat, placement(anchor, *link, *phase, *scale, *angle)); + l = self.paint(l, content, Cov::One, &paint, common.blend, common.opacity, F_GATE, clip, sb); + } + } + for &(_, e) in &rev { + if let Effect::GradientOverlay { common, gradient, .. } = e { + l = self.paint(l, content, Cov::One, &Paint::Gradient(gradient), common.blend, common.opacity, F_GATE, clip, sb); + } + } + for &(_, e) in &rev { + if let Effect::ColorOverlay { common, color } = e { + l = self.paint(l, content, Cov::One, &Paint::Color(color.to_rgb()), common.blend, common.opacity, F_GATE, clip, sb); + } + } + for &(i, e) in &rev { + if let Effect::Satin(s) = e { + l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &Paint::Color(s.color.to_rgb()), s.common.blend, s.common.opacity, F_REL, clip, sb); + } + } + for &(i, e) in &rev { + if let Effect::InnerGlow(g) = e { + let paint = self.glow_paint(g, anchor); + l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &paint, g.common.blend, g.common.opacity, F_REL, clip, sb); + } + } + for &(i, e) in &rev { + if let Effect::InnerShadow(s) = e { + l = self.paint(l, content, Cov::Map(map(i, 0), 0.0), &Paint::Color(s.color.to_rgb()), s.common.blend, s.common.opacity, F_REL, clip, sb); + } + } + work.l = l; + } + + fn effect_vector_stroke(&mut self, work: &mut EffectState<'a>) { + let layer = work.layer; + let content = work.content; + let mut l = work.l; + let vstroke = work.vstroke; + let clip = work.clip; + if let Some(s) = vstroke { + let mut p = Pass::new(Kernel::FxInit, 0); + p.a = Some(l); + p.b = Some(self.retain(content)); + p.c = Some(s); + p.adjust_kind = INIT_VSTROKE; + p.opacity = layer.fill_opacity; + p.clip = Some(clip); + l = self.emit(p); + } + work.l = l; + } + + fn effect_inside_strokes(&mut self, work: &mut EffectState<'a>) { + let layer = work.layer; + let rev = work.rev.clone(); + let fx = work.fx; + let content = work.content; + let mut l = work.l; + let clip = work.clip; + let sb = work.sb; + let anchor = work.anchor; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + // Inside stroke parts are painted over the layer (bottom instance first). + for &(i, e) in &rev { + if let Effect::Stroke(st) = e { + let (in_w, _) = stroke_widths(st); + if in_w > 0.0 { + let paint = self.fx_paint(&st.paint, anchor); + l = self.paint( + l, + content, + Cov::Map(map(i, 1), (in_w + 0.5).clamp(0.0, 1.0)), + &paint, + st.common.blend, + st.common.opacity, + F_GATE, + clip, + photocraft_compose::stroke_frame(self.cx.doc, layer, st).unwrap_or(sb), + ); + } + } + } + work.l = l; + } + + fn effect_inner_bevel(&mut self, work: &mut EffectState<'a>) { + let rev = work.rev.clone(); + let fx = work.fx; + let content = work.content; + let mut l = work.l; + let clip = work.clip; + let sb = work.sb; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + let bevel_paint = |b: &photocraft_doc::Bevel| photocraft_compose::effects::bevel_geom(b).paint; + for &(i, e) in &rev { + if let Effect::BevelEmboss(b) = e + && bevel_paint(b) == photocraft_compose::effects::BevelPaint::Inner + { + l = self.paint( + l, + content, + Cov::Map(map(i, 0), 0.0), + &Paint::Color(b.highlight_color.to_rgb()), + b.highlight.blend, + b.highlight.opacity, + F_REL, + clip, + sb, + ); + l = self.paint(l, content, Cov::Map(map(i, 1), 0.0), &Paint::Color(b.shadow_color.to_rgb()), b.shadow.blend, b.shadow.opacity, F_REL, clip, sb); + } + } + work.l = l; + } + + fn effect_alpha(&mut self, work: &mut EffectState<'a>) { + let content = work.content; + let mut l = work.l; + let clip = work.clip; + // The shape's own alpha. + let c = self.retain(content); + l = self.fx_init(clip, l, Some(c), INIT_ALPHA, 1.0); + work.l = l; + } + + fn effect_outside_strokes(&mut self, work: &mut EffectState<'a>) { + let layer = work.layer; + let items = work.items.clone(); + let fx = work.fx; + let content = work.content; + let mut w = work.w; + let l = work.l; + let clip = work.clip; + let sb = work.sb; + let anchor = work.anchor; + let outline = work.outline; + let vector_shape = work.vector_shape; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + // Outside stroke parts lie beneath the layer and blend onto the exterior result with their + // own mode; a higher stroke covers the ones below it (each takes its coverage × opacity + // not yet taken above it, blended over the result as it was before the strokes). + let outs: Vec<(usize, &photocraft_doc::StrokeFx)> = items + .iter() + .copied() + .enumerate() + .filter_map(|(i, e)| if let Effect::Stroke(st) = e { Some((i, st)) } else { None }) + .filter(|(_, st)| stroke_widths(st).1 > 0.0) + .collect(); + if !outs.is_empty() { + let (mut acc, mut cover): (Option, Option) = (None, None); + if outline || !vector_shape { + // The layer's alpha rides along the coverage: `effects::outline_share`, and for + // pixel layers the stroke's share beneath a partly covering layer (fill < 100 %). + let ll = self.retain(l); + cover = Some(self.fx_init(clip, ll, None, INIT_COVER, 1.0)); + } + for (n, &(i, st)) in outs.iter().enumerate() { + let paint = self.fx_paint(&st.paint, anchor); + let flags = F_STROKE_OUT | if vector_shape { F_VECTOR } else { 0 } | if outline { F_OUTLINE } else { 0 } | if n == 0 { F_FIRST } else { 0 }; + // Share × blended colour, then coverage (both read the coverage so far). + let mut p = self.fx_pass_kernel( + Kernel::FxStroke, + w, + content, + &paint, + st.common.blend, + st.common.opacity, + flags, + clip, + photocraft_compose::stroke_frame(self.cx.doc, layer, st).unwrap_or(sb), + ); + if matches!(paint, Paint::None) { + // A missing pattern paints nothing: the stroke's share keeps the result as is. + p.params[2][2] = 3.0; + } + p.adjust_kind = 0; + p.map = Some(map(i, 0)); + p.c = cover.map(|c| self.retain(c)); + p.d = acc; + let a = self.emit(p); + // The stroke's mode rides along only for Dissolve, whose coverage is all or + // nothing in this pass as in the colour pass (#6995). + let mut p = self.fx_pass_kernel(Kernel::FxStroke, w, content, &Paint::Color([0.0; 3]), st.common.blend, st.common.opacity, flags, clip, sb); + p.adjust_kind = 1; + p.map = Some(map(i, 0)); + p.c = cover; + cover = Some(self.emit(p)); + acc = Some(a); + } + let mut p = Pass::new(Kernel::FxStrokeEnd, 0); + p.a = Some(w); + p.b = acc; + p.c = cover; + p.clip = Some(clip); + w = self.emit(p); + } + work.w = w; + } + + fn effect_outer_bevel(&mut self, work: &mut EffectState<'a>) { + let rev = work.rev.clone(); + let bevel_paint = |b: &photocraft_doc::Bevel| photocraft_compose::effects::bevel_geom(b).paint; + let fx = work.fx; + let content = work.content; + let mut w = work.w; + let clip = work.clip; + let sb = work.sb; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + for &(i, e) in &rev { + if let Effect::BevelEmboss(b) = e + && bevel_paint(b) == photocraft_compose::effects::BevelPaint::Outer + { + let k = 0; + w = self.paint( + w, + content, + Cov::Map(map(i, k), 0.0), + &Paint::Color(b.highlight_color.to_rgb()), + b.highlight.blend, + b.highlight.opacity, + 0, + clip, + sb, + ); + w = self.paint(w, content, Cov::Map(map(i, k + 1), 0.0), &Paint::Color(b.shadow_color.to_rgb()), b.shadow.blend, b.shadow.opacity, 0, clip, sb); + } + } + + work.w = w; + } + + fn effect_merge(&mut self, work: &mut EffectState<'a>) -> Slot { + let layer = work.layer; + let rev = work.rev.clone(); + let fx = work.fx; + let content = work.content; + let w = work.w; + let l = work.l; + let backdrop = work.backdrop; + let clip = work.clip; + let sb = work.sb; + let atop = work.atop; + let premerge = work.premerge; + let bevel_paint = |b: &photocraft_doc::Bevel| photocraft_compose::effects::bevel_geom(b).paint; + let map = |item: usize, map: usize| MapRef { fx, item, map }; + let mode = if premerge || layer.blend == BlendMode::PassThrough { BlendMode::Normal } else { layer.blend }; + let late: Vec<(usize, &'a photocraft_doc::Bevel)> = rev + .iter() + .filter_map(|&(i, e)| if let Effect::BevelEmboss(b) = e { Some((i, b)) } else { None }) + .filter(|(_, b)| bevel_paint(b) == photocraft_compose::effects::BevelPaint::Both) + .collect(); + if late.is_empty() { + let mut p = Pass::new(Kernel::FxMerge, 0); + p.a = Some(w); + p.b = Some(l); + p.c = Some(self.retain(backdrop)); + p.mode = mode; + p.opacity = layer.opacity; + p.flags = if atop { F_ATOP } else { 0 } | gamma_flag(layer); + p.extra[3] = photocraft_compose::text_gamma(layer); + p.clip = Some(clip); + self.emit(p) + } else { + // Emboss styles shade the composited layer (`composite_with_effects`): merge at full + // opacity, paint their maps (inside and outside halves), then mix. + let mut p = Pass::new(Kernel::FxMerge, 0); + p.a = Some(w); + p.b = Some(l); + p.c = Some(self.retain(w)); + p.mode = mode; + p.opacity = 1.0; + p.flags = gamma_flag(layer); + p.extra[3] = photocraft_compose::text_gamma(layer); + p.clip = Some(clip); + let mut m = self.emit(p); + for (i, b) in late { + for (k, color, fxc) in [(0, &b.highlight_color, &b.highlight), (1, &b.shadow_color, &b.shadow)] { + m = self.paint(m, content, Cov::Map(map(i, k), 0.0), &Paint::Color(color.to_rgb()), fxc.blend, fxc.opacity, 0, clip, sb); + } + } + let mut p = Pass::new(Kernel::FxMerge, 0); + p.b = Some(self.retain(m)); + p.a = Some(m); + p.c = Some(self.retain(backdrop)); + p.mode = mode; + p.opacity = layer.opacity; + p.flags = if atop { F_ATOP } else { 0 } | F_NO_LAYER; + p.clip = Some(clip); + self.emit(p) + } + } + + fn fx_init(&mut self, clip: Rect, src: Slot, b: Option, kind: i32, opacity: f32) -> Slot { + let mut pass = Pass::new(Kernel::FxInit, 0); + pass.a = Some(src); + pass.b = b; + pass.adjust_kind = kind; + pass.opacity = opacity; + pass.clip = Some(clip); + self.emit(pass) + } + + fn fx_paint(&self, p: &'a FxPaint, anchor: (f64, f64)) -> Paint<'a> { + match p { + FxPaint::Color(c) => Paint::Color(c.to_rgb()), + FxPaint::Gradient(g) => Paint::Gradient(g), + FxPaint::Pattern { name, id, scale } => match photocraft_doc::pattern::find(self.cx.patterns, id, name).filter(|p| p.is_paintable()) { + Some(pat) => Paint::Pattern(pat, placement(anchor, true, (0.0, 0.0), *scale, 0.0)), + None => Paint::None, + }, + } + } + + fn glow_paint(&self, g: &'a Glow, anchor: (f64, f64)) -> Paint<'a> { + match &g.paint { + FxPaint::Gradient(gradient) => Paint::GlowGradient(gradient, photocraft_compose::effects::glow_gradient_gain(g.range)), + p => self.fx_paint(p, anchor), + } + } + + /// An effect pass reading `dst` (A, retained) and the layer `content` (B, retained) with its + /// paint set up. + #[allow(clippy::too_many_arguments)] + fn fx_pass(&mut self, dst: Slot, content: Slot, paint: &Paint<'a>, blend: BlendMode, opacity: f32, flags: u32, clip: Rect, sb: Rect) -> Pass<'a> { + self.fx_pass_kernel(Kernel::FxPaint, dst, content, paint, blend, opacity, flags, clip, sb) + } + + #[allow(clippy::too_many_arguments)] + fn fx_pass_kernel( + &mut self, + kernel: Kernel, + dst: Slot, + content: Slot, + paint: &Paint<'a>, + blend: BlendMode, + opacity: f32, + flags: u32, + clip: Rect, + sb: Rect, + ) -> Pass<'a> { + let mut p = Pass::new(kernel, 0); + p.a = Some(self.retain(dst)); + p.b = Some(self.retain(content)); + p.mode = blend; + p.opacity = opacity; + p.flags = flags; + p.clip = Some(clip); + match paint { + Paint::None => {} + Paint::Color(c) => p.color = [c[0], c[1], c[2], 1.0], + Paint::Gradient(g) => { + // effects::paint_fx: whole-pixel end points (fill_layout::gradient_layout). + let (angle, scale, offset) = photocraft_compose::fill_layout::gradient_layout(g.style, g.angle, g.scale, g.offset, sb); + p.params[0] = [angle, scale, if g.reverse { 1.0 } else { 0.0 }, style_index(g.style)]; + p.params[1] = [sb.x0 as f32, sb.y0 as f32, sb.width() as f32, sb.height() as f32]; + p.params[2][0] = offset.0; + p.params[2][1] = offset.1; + p.params[2][2] = 1.0; + p.lut = Some(gradient_rows(g)); + } + Paint::GlowGradient(g, gain) => { + // effects::paint_glow: the gradient at 1 - strength, opaque from 1 / gain. + p.params[0][0] = *gain; + p.params[2][2] = 4.0; + p.lut = Some(gradient_rows(g)); + } + Paint::Pattern(pat, pl) => { + p.params[2][2] = 2.0; + p.params[3] = [pl.origin.0 as f32, pl.origin.1 as f32, pl.cs.0 as f32, pl.cs.1 as f32]; + p.extra = [pl.inv_scale as f32, pat.width as f32, pat.height as f32, 0.0]; + p.pattern = Some(pat); + } + } + p + } + + /// One effect paint into `dst` (consumed); reads the layer's alpha from `content` (kept). + #[allow(clippy::too_many_arguments)] + fn paint(&mut self, dst: Slot, content: Slot, cov: Cov, paint: &Paint<'a>, blend: BlendMode, opacity: f32, flags: u32, clip: Rect, sb: Rect) -> Slot { + self.paint_k(dst, content, cov, paint, blend, opacity, flags, clip, sb, 1.0) + } + + /// [`Self::paint`] with the knockout strength (`F_KNOCKOUT`: coverage × (1 − alpha × k)). + #[allow(clippy::too_many_arguments)] + fn paint_k( + &mut self, + dst: Slot, + content: Slot, + cov: Cov, + paint: &Paint<'a>, + blend: BlendMode, + opacity: f32, + flags: u32, + clip: Rect, + sb: Rect, + knockout: f32, + ) -> Slot { + if matches!(paint, Paint::None) { + // Missing pattern: compose paints nothing. + return dst; + } + let mut p = self.fx_pass(dst, content, paint, blend, opacity, flags, clip, sb); + self.release(dst); + match cov { + Cov::Map(m, outside) => { + p.adjust_kind = 0; + p.map = Some(m); + p.params[2][3] = outside; + } + Cov::One => p.adjust_kind = 1, + } + p.extra[3] = knockout; + self.emit(p) + } +} + +/// Adjustment Fill belongs inside the special blend modes; other modes use it as coverage. +fn adjustment_fill(layer: &Layer, opacity: f32) -> (f32, f32) { + if layer.fill_opacity < 1.0 && photocraft_compose::psblend::fill_is_special(layer.blend) { (layer.opacity, layer.fill_opacity) } else { (opacity, 1.0) } +} diff --git a/crates/gpu/src/plan/tests.rs b/crates/gpu/src/plan/tests.rs new file mode 100644 index 0000000000..83f286bf2b --- /dev/null +++ b/crates/gpu/src/plan/tests.rs @@ -0,0 +1,337 @@ +use super::*; +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_geom::Size; + +#[test] +fn nested_pass_through_groups_with_clipped_layers_are_not_planned_exponentially() { + let mut layers = vec![Layer::raster("leaf", photocraft_color::PixelFormat::RGBA8)]; + for _ in 0..30 { + let group = Layer::group("g", layers); + let mut clipped = Layer::raster("c", photocraft_color::PixelFormat::RGBA8); + clipped.clipped = true; + layers = vec![group, clipped]; + } + let mut doc = Document::new("nest", Size::new(8, 8), ColorMode::Rgb, SampleType::U8); + doc.layers = layers; + let started = std::time::Instant::now(); + let Err(error) = plan(&doc) else { panic!("2^30 plannings must be refused") }; + assert!(error.0.contains("too many nested layers"), "{}", error.0); + assert!(started.elapsed().as_secs() < 10, "{:?}", started.elapsed()); +} + +#[test] +fn stops_closer_than_one_lut_step_stay_on_the_cpu() { + assert!(check_gradient_stops(&[(0.4999_f32, ()), (0.5, ())]).is_err()); + assert!(check_gradient_stops(&[(0.0_f32, ()), (1.0, ())]).is_ok()); +} + +#[test] +fn lab_channel_adjustments_defer_to_the_cpu_in_lab_documents() { + let lab_rules = [ + Adjustment::Invert, + Adjustment::Posterize { levels: 4 }, + Adjustment::Threshold { level: 0.5 }, + Adjustment::BrightnessContrast { brightness: 10.0, contrast: 5.0, legacy: false }, + Adjustment::GradientMap { stops: vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])], reverse: false, dither: false }, + ]; + for adj in &lab_rules { + assert!(!adjustment_on_gpu(adj, ColorMode::Lab), "{adj:?}"); + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + assert!(adjustment_on_gpu(adj, mode), "{adj:?} in {mode:?}"); + } + // CMYK documents invert, posterize, threshold and brighten their inks, on the CPU. + let inks = !matches!(adj, Adjustment::GradientMap { .. }); + assert_eq!(adjustment_on_gpu(adj, ColorMode::Cmyk), !inks, "{adj:?} in CMYK"); + } + // CMYK Hue/Saturation works on the inks (on the CPU); Colorize stays on the screen colour. + let ranges = photocraft_doc::adjust::HueRange::defaults(); + let hue = |colorize| Adjustment::HueSaturation { hue: 30.0, saturation: 10.0, lightness: 0.0, colorize, ranges: ranges.clone() }; + assert!(!adjustment_on_gpu(&hue(false), ColorMode::Cmyk) && adjustment_on_gpu(&hue(true), ColorMode::Cmyk)); + assert!(adjustment_on_gpu(&hue(false), ColorMode::Rgb)); + // Screen-colour adjustments stay on the GPU in Lab documents. + assert!(adjustment_on_gpu(&Adjustment::Exposure { exposure: 1.0, offset: 0.0, gamma: 1.0 }, ColorMode::Lab)); +} + +#[test] +fn native_cmyk_fills_fall_back_only_without_a_valid_cache() { + use photocraft_doc::{FillCache, GradientStyle}; + for depth in photocraft_color::SampleType::ALL { + for fill in [Fill::Solid(Color::WHITE), Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false)] { + let mut doc = Document::new("CMYK", photocraft_geom::Size::new(3, 2), photocraft_color::ColorMode::Cmyk, depth); + doc.layers.push(Layer::new("Fill", LayerContent::Fill(fill.clone()))); + assert!(plan(&doc).unwrap_err().0.contains("native CMYK fill")); + doc.layers[0].visible = false; + assert!(plan(&doc).is_ok()); + doc.layers[0].visible = true; + doc.layers[0].fill_cache = Some(FillCache { fill, surface: photocraft_raster::Surface::new(doc.pixel_format()) }); + assert!(plan(&doc).is_ok()); + doc.layers[0].fill_cache.as_mut().unwrap().fill = Fill::Solid(Color::TRANSPARENT); + assert!(plan(&doc).is_err()); + doc.mode = photocraft_color::ColorMode::Rgb; + assert!(plan(&doc).is_ok()); + } + } +} +#[test] +fn outer_glow_plan_knocks_out_see_through_fill() { + use photocraft_doc::{Contour, FxCommon, GlowSource, GlowTechnique}; + + for fill in [0.0, 0.4, 1.0] { + let mut d = Document::with_background("glow", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut layer = Layer::raster("frame", d.pixel_format()); + layer.surface_mut().unwrap().fill_rect(Rect::new(16, 16, 48, 48), &[1.0, 0.0, 0.0, 1.0]); + layer.fill_opacity = fill; + layer.effects.items.push(Effect::OuterGlow(Glow { + common: FxCommon::new(BlendMode::Normal, 1.0), + paint: FxPaint::Color(Color::rgb(1.0, 1.0, 0.0)), + technique: GlowTechnique::Softer, + spread: 0.3, + size: 8.0, + contour: Contour::Linear, + anti_alias: false, + range: 0.5, + jitter: 0.0, + noise: 0.0, + source: GlowSource::Edge, + })); + d.layers.push(layer); + let planned = plan(&d).unwrap(); + let glow = planned + .passes + .iter() + .find(|pass| pass.kernel == Kernel::FxPaint && pass.flags & F_KNOCKOUT != 0 && pass.color[0] > 0.9 && pass.color[1] > 0.9 && pass.color[2] < 0.1) + .expect("outer glow paint pass"); + assert!((glow.extra[3] - (1.0 - fill)).abs() < 1e-6, "fill {fill}"); + } +} + +#[test] +fn color_lookup_with_an_overflowing_stored_size_uploads_no_table() { + for size in [1 << 22, u32::MAX] { + let a = + Adjustment::ColorLookup { name: "x".into(), lut: Some(std::sync::Arc::new(vec![0.5; 24])), size, tetrahedral: false, dither: false, source: None }; + let (kind, _, rows) = adjustment_program(&a, Transfer::Srgb, SampleType::U8, photocraft_color::ColorMode::Rgb); + assert_eq!((kind, rows.is_none()), (0, true), "size {size}"); + } +} + +#[test] +fn root_checkpoints_have_only_one_live_input_and_keep_clips_whole() { + for mode in [BlendMode::Normal, BlendMode::PassThrough] { + let mut d = Document::with_background("t", Size::new(32, 24), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut clip = Layer::new("clip", LayerContent::Fill(Fill::Solid(Color::rgba(0.2, 0.3, 0.7, 0.4)))); + clip.clipped = true; + let mut group = Layer::group( + "group", + vec![ + Layer::new("fill", LayerContent::Fill(Fill::Solid(Color::rgba(0.9, 0.1, 0.3, 0.6)))), + Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)), + ], + ); + group.blend = mode; + d.layers.extend([group, clip, Layer::new("top", LayerContent::Adjustment(Adjustment::Invert))]); + let plan = plan(&d).unwrap(); + assert_eq!(plan.checkpoints.iter().map(|c| c.layer_end).collect::>(), vec![1, 3, 4]); + for checkpoint in &plan.checkpoints { + let mut written = std::collections::HashSet::from([checkpoint.slot]); + for pass in &plan.passes[checkpoint.pass_end..] { + for input in [pass.a, pass.b, pass.c, pass.d].into_iter().flatten() { + assert!(written.contains(&input), "a suffix must not need another saved slot: {checkpoint:?}"); + } + written.insert(pass.dst); + } + assert!(written.contains(&plan.root)); + } + } +} + +#[test] +fn slots_are_recycled() { + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + for i in 0..10 { + d.layers.push(Layer::raster(format!("l{i}"), d.pixel_format())); + } + let p = plan(&d).unwrap(); + // Empty layers draw nothing: the clear and the background's one fused pass. + assert_eq!(p.passes.len(), 1 + 1); + for (i, l) in d.layers.iter_mut().skip(1).enumerate() { + l.surface_mut().unwrap().fill_rect(Rect::new(i as i32 % 4, 0, 4, 8), &[0.0, 0.0, 1.0, 1.0]); + } + let p = plan(&d).unwrap(); + assert!(p.slots <= 3, "{} slots", p.slots); + // The background, then each small layer blended over its bounds and copied back. + assert_eq!(p.passes.len(), 1 + 1 + 10 * 2); +} + +#[test] +fn small_layers_composite_only_over_their_bounds() { + let mut d = Document::with_background("t", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut l = Layer::raster("small", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(10, 12, 20, 30), &[1.0, 0.0, 0.0, 1.0]); + let mut group = Layer::group("g", vec![l]); + group.blend = BlendMode::Normal; + d.layers.push(group); + let p = plan(&d).unwrap(); + let r = Rect::new(10, 12, 20, 30); + // The group's content, its blend and the copy back are all limited to the layer. + assert!(p.passes.iter().filter(|p| matches!(p.kernel, Kernel::Content | Kernel::Blend)).skip(2).all(|p| p.clip == Some(r)), "{:?}", p.passes); + let last = p.passes.last().unwrap(); + assert_eq!((last.kernel, last.dst, last.clip), (Kernel::CopyRect, p.root, Some(r))); + // A layer that covers most of the canvas composites everywhere. + d.layers[1].children_mut().unwrap()[0].surface_mut().unwrap().fill_rect(Rect::new(0, 0, 60, 60), &[1.0, 0.0, 0.0, 1.0]); + let p = plan(&d).unwrap(); + assert!(p.passes.iter().all(|p| p.clip.is_none())); +} + +#[test] +fn effects_are_planned_in_place() { + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut l = Layer::raster("fx", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(2, 2, 5, 5), &[1.0, 0.0, 0.0, 1.0]); + l.effects.items.push(photocraft_doc::Effect::ColorOverlay { common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), color: Color::WHITE }); + l.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); + d.layers.push(l); + let p = plan(&d).unwrap(); + assert_eq!(p.fx.len(), 1); + let m = photocraft_compose::effects::margin(&d.layers[1]); + assert_eq!(p.fx[0].region, Rect::new(2, 2, 5, 5).inflate(m).intersect(&d.bounds().inflate(m))); + // The effect region covers the whole canvas: the merged result is the backdrop, no copy. + assert!(p.fx[0].region.contains_rect(&d.bounds())); + let last = p.passes.last().unwrap(); + assert_eq!(last.kernel, Kernel::FxMerge); + assert_eq!(last.dst, p.root); + assert!(p.slots <= 6, "{} slots", p.slots); + // On a larger canvas the result lands back in the backdrop slot over the region only. + let mut big = Document::with_background("t", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE); + big.layers.push(d.layers[1].clone()); + let p = plan(&big).unwrap(); + assert!(!p.fx[0].region.contains_rect(&big.bounds())); + let last = p.passes.last().unwrap(); + assert_eq!((last.kernel, last.dst, last.clip), (Kernel::CopyRect, p.root, Some(p.fx[0].region))); +} + +#[test] +fn exterior_effects_merge_from_the_shape() { + // A drop shadow alone: the layer's pixels, the shadow, the merge. With an interior effect + // the layer is built in passes of its own (colour inside the shape, the overlay, alpha). + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut l = Layer::raster("fx", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 8, 8), &[1.0, 0.0, 0.0, 0.5]); + l.fill_opacity = 0.7; + l.effects.items.push(photocraft_doc::Effect::default_drop_shadow()); + d.layers.push(l); + let p = plan(&d).unwrap(); + let fx: Vec = p.passes.iter().skip(2).map(|p| p.kernel).collect(); + assert_eq!(fx, [Kernel::Content, Kernel::FxPaint, Kernel::FxMerge], "{:?}", p.passes); + let merge = p.passes.last().unwrap(); + assert!(merge.flags & F_LAYER_SHAPE != 0); + assert_eq!(merge.params[0][0], 0.7); + d.layers[1].effects.items.push(photocraft_doc::Effect::ColorOverlay { common: photocraft_doc::FxCommon::new(BlendMode::Normal, 1.0), color: Color::WHITE }); + let p = plan(&d).unwrap(); + assert!(p.passes.iter().all(|p| p.flags & F_LAYER_SHAPE == 0)); + assert!(p.passes.iter().any(|p| p.kernel == Kernel::FxInit)); +} + +#[test] +fn opaque_fill_layers_skip_what_they_cover() { + let opaque = || Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 30.0, 1.0, photocraft_doc::GradientStyle::Linear, false); + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + for i in 0..4 { + // Small painted layers (empty ones plan no passes at all). + let mut l = Layer::raster(format!("l{i}"), d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(i, i, i + 2, i + 2), &[0.0, 0.0, 1.0, 1.0]); + d.layers.push(l); + } + d.layers.push(Layer::new("g", LayerContent::Fill(opaque()))); + let mut top = Layer::raster("top", d.pixel_format()); + top.clipped = true; + d.layers.push(top); + // Clear, then the fill and its clipped layer: the background and four rasters are skipped. + let skipped = plan(&d).unwrap().passes.len(); + let mut alone = d.clone(); + alone.layers.drain(..5); + assert_eq!(skipped, plan(&alone).unwrap().passes.len()); + // Anything that lets the layers below show through keeps them. + let see_through: [fn(&mut Layer); 6] = [ + |l| l.opacity = 0.99, + |l| l.fill_opacity = 0.5, + |l| l.blend = BlendMode::Multiply, + |l| l.visible = false, + |l| l.excluded_channels = 1, + |l| { + if let LayerContent::Fill(Fill::Gradient { opacity_stops, .. }) = &mut l.content { + opacity_stops.push((1.0, 0.5)); + } + }, + ]; + for (k, f) in see_through.iter().enumerate() { + let mut d2 = d.clone(); + f(&mut d2.layers[5]); + assert!(plan(&d2).unwrap().passes.len() > skipped, "case {k}: the layers below are planned"); + assert!(!photocraft_compose::occludes_below(&d2.layers[5], ColorMode::Rgb), "case {k}"); + } + let mut translucent = d.clone(); + translucent.layers[5].content = LayerContent::Fill(Fill::Solid(Color::rgba(1.0, 0.0, 0.0, 0.5))); + assert!(!photocraft_compose::occludes_below(&translucent.layers[5], ColorMode::Rgb)); + assert!(photocraft_compose::occludes_below(&d.layers[5], ColorMode::Rgb)); +} + +#[test] +fn blend_if_plans_on_the_gpu() { + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut l = Layer::raster("bi", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[1.0, 0.0, 0.0, 1.0]); + l.blend_if.set(0, [photocraft_doc::BlendRange { black: [40, 40], white: [255, 255] }, photocraft_doc::BlendRange::FULL]); + d.layers.push(l.clone()); + let blend_if = |d: &Document| plan(d).unwrap().passes.iter().filter(|p| p.flags & F_BLEND_IF != 0).count(); + assert_eq!(blend_if(&d), 1); + // Clipped layers too. + l.clipped = true; + d.layers[1].blend_if = Default::default(); + d.layers.push(l); + assert_eq!(blend_if(&d), 1); + // Outside RGB, Grayscale and Duotone the setting round-trips without applying. + d.mode = ColorMode::Lab; + assert_eq!(blend_if(&d), 0); +} + +#[test] +fn blend_if_params_pack_every_range_exactly() { + let mut bi = photocraft_doc::BlendIf::default(); + bi.set(3, [photocraft_doc::BlendRange { black: [255, 255], white: [0, 1] }, photocraft_doc::BlendRange { black: [12, 200], white: [201, 254] }]); + let f: Vec = blend_if_params(&bi).iter().flatten().copied().collect(); + // Entry 3 (B), this layer then underlying; unset entries are full ranges. + assert_eq!(&f[12..16], &[255.0 * 256.0 + 255.0, 1.0, 12.0 * 256.0 + 200.0, 201.0 * 256.0 + 254.0]); + assert_eq!(&f[0..2], &[0.0, 255.0 * 256.0 + 255.0]); +} + +#[test] +fn advanced_blending_falls_back_to_the_cpu() { + use photocraft_doc::Knockout; + let mut d = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let mut l = Layer::raster("ko", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(0, 0, 4, 4), &[1.0, 0.0, 0.0, 1.0]); + d.layers.push(l.clone()); + assert!(plan(&d).is_ok(), "defaults stay on the GPU"); + // Switches with nothing to act on (no effects, no clipped layers) stay on the GPU too. + d.layers[1].advanced.blend_interior = true; + d.layers[1].advanced.blend_clipped = false; + assert!(plan(&d).is_ok()); + d.layers[1].advanced.knockout = Knockout::Deep; + assert!(plan(&d).unwrap_err().0.contains("Advanced Blending")); + // Inside groups and clipping groups. + d.layers[1].advanced.knockout = Knockout::None; + let mut c = l.clone(); + c.clipped = true; + d.layers.push(c.clone()); + assert!(plan(&d).is_err(), "clipped layers blended individually"); + d.layers[1].advanced.blend_clipped = true; + assert!(plan(&d).is_ok()); + d.layers[2].advanced.knockout = Knockout::Shallow; + assert!(plan(&d).is_err(), "clipped knockout"); + d.layers.truncate(1); + let mut inner = l; + inner.advanced.knockout = Knockout::Shallow; + d.layers.push(Layer::group("g", vec![inner])); + assert!(plan(&d).is_err(), "knockout inside a group"); +} diff --git a/crates/gpu/src/plan/types.rs b/crates/gpu/src/plan/types.rs new file mode 100644 index 0000000000..13005dbba2 --- /dev/null +++ b/crates/gpu/src/plan/types.rs @@ -0,0 +1,304 @@ +use super::{BlendMode, Document, GlobalLight, Layer, LayerId, Pattern, Rect, Slot, Surface, Transfer}; + +/// Pipeline (fragment entry point) of a pass. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Kernel { + /// Transparent fill (no draw). + Clear, + Content, + Mask, + Blend, + /// `Content` then `Blend` in one pass: a plain layer's pixels blended straight onto A. + LayerBlend, + /// A run of consecutive `LayerBlend` passes drawn as one (never planned: the executor draws + /// it in their place, see `batch_runs`). + LayerBatch, + /// `LayerBatch` of a run whose layers all blend Normal without text gamma, outside Lab + /// documents (never planned: the executor picks it, see `batch_kernel`). + LayerBatchNormal, + Atop, + Adjust, + AdjMix, + Lerp, + /// Effect chain start: the layer at fill opacity, or an opaque copy of a clipping base. + FxInit, + /// Paint an effect through a coverage map (or the layer's alpha) into A. + FxPaint, + /// Layer + interior effects over the exterior result, then layer opacity against the backdrop. + FxMerge, + /// Outside strokes: accumulate one stroke's share (`kind` 0, premultiplied colour) or its + /// coverage (`kind` 1). + FxStroke, + /// Outside strokes: resolve the accumulated strokes over the exterior result. + FxStrokeEnd, + /// Copy slot A into `dst` (an existing slot) over the pass clip (no draw). + CopyRect, + /// Copy all of slot A into the fresh slot `dst` (no draw). + CopyFull, + // Effect-map kernels (single-channel targets over an effect region, see `fx`). + MShift, + MDilate, + MBlur, + MGlow, + MFinish, + MBevelH, + MBevelShade, + MStroke, + MBevelTex, +} + +impl Kernel { + /// Fragment entry point in `compose.wgsl` (`None` for copies and clears). + pub fn entry(self) -> Option<&'static str> { + Some(match self { + Kernel::Clear | Kernel::CopyRect | Kernel::CopyFull => return None, + Kernel::Content => "fs_content", + Kernel::Mask => "fs_mask", + Kernel::Blend => "fs_blend", + Kernel::LayerBlend => "fs_layerblend", + Kernel::LayerBatch => "fs_layerbatch", + Kernel::LayerBatchNormal => "fs_layerbatch_normal", + Kernel::Atop => "fs_atop", + Kernel::Adjust => "fs_adjust", + Kernel::AdjMix => "fs_adjmix", + Kernel::Lerp => "fs_lerp", + Kernel::FxInit => "fs_fxinit", + Kernel::FxPaint => "fs_fxpaint", + Kernel::FxMerge => "fs_fxmerge", + Kernel::FxStroke => "fs_fxstroke", + Kernel::FxStrokeEnd => "fs_fxstrokeend", + Kernel::MShift => "fs_mshift", + Kernel::MDilate => "fs_mdilate", + Kernel::MBlur => "fs_mblur", + Kernel::MGlow => "fs_mglow", + Kernel::MFinish => "fs_mfinish", + Kernel::MBevelH => "fs_mbevelh", + Kernel::MBevelShade => "fs_mbevelshade", + Kernel::MStroke => "fs_mstroke", + Kernel::MBevelTex => "fs_mbeveltex", + }) + } + + /// Kernels that write effect maps (R32F / R16F targets) rather than RGBA accumulators. + pub fn is_map(self) -> bool { + matches!( + self, + Kernel::MShift + | Kernel::MDilate + | Kernel::MBlur + | Kernel::MGlow + | Kernel::MFinish + | Kernel::MBevelH + | Kernel::MBevelShade + | Kernel::MStroke + | Kernel::MBevelTex + ) + } + + /// Every kernel with a pipeline. + pub const DRAWN: [Kernel; 24] = [ + Kernel::Content, + Kernel::Mask, + Kernel::Blend, + Kernel::LayerBlend, + Kernel::LayerBatch, + Kernel::LayerBatchNormal, + Kernel::Atop, + Kernel::Adjust, + Kernel::AdjMix, + Kernel::Lerp, + Kernel::FxInit, + Kernel::FxPaint, + Kernel::FxMerge, + Kernel::FxStroke, + Kernel::FxStrokeEnd, + Kernel::MShift, + Kernel::MDilate, + Kernel::MBlur, + Kernel::MGlow, + Kernel::MFinish, + Kernel::MBevelH, + Kernel::MBevelShade, + Kernel::MStroke, + Kernel::MBevelTex, + ]; +} + +/// Which resident texture a pass samples. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Role { + Content, + Mask, + /// A stroked shape's stroke alone (its fill uses `Content`). + Stroke, +} + +/// A surface the pass needs on the GPU. +#[derive(Clone, Debug)] +pub struct TexUse<'a> { + pub layer: LayerId, + pub role: Role, + pub surface: SurfaceRef<'a>, +} + +/// An effect map sampled by a pass: map `map` of enabled effect `item` of `plan.fx[fx]`. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct MapRef { + pub fx: usize, + pub item: usize, + pub map: usize, +} + +#[derive(Clone, Debug)] +pub struct Pass<'a> { + pub kernel: Kernel, + pub dst: Slot, + pub a: Option, + pub b: Option, + pub c: Option, + /// A fourth slot, bound in place of `tex` (passes without layer pixels). + pub d: Option, + pub mode: BlendMode, + pub opacity: f32, + /// The layer's Fill where it isn't folded into `opacity`: the final blend of a plain layer + /// (`Kernel::Blend`), so the special eight can apply it inside the blend. 1 elsewhere. + pub fill: f32, + /// Layer pixels (raster / text / shape / smart cache / fill cache). + pub tex: Option>, + /// Colour outside `tex` (the surface's default pixel), the solid fill colour, or the effect + /// colour. + pub color: [f32; 4], + pub mask: Option>, + /// Adjustment kind (see `adjust` in compose.wgsl) and parameters; the coverage kind of an + /// effect paint. + pub adjust_kind: i32, + pub params: [[f32; 4]; 4], + pub extra: [f32; 4], + /// Extra shader flags (`F_KNOCKOUT`, …). + pub flags: u32, + /// 4096-entry LUT rows (Levels / Curves / Gradient map / gradient stops). + pub lut: Option>, + pub gradient: bool, + /// Effect map sampled by `FxPaint`. + pub map: Option, + /// Pattern painted by `FxPaint`. + pub pattern: Option<&'a Pattern>, + /// Only pixels inside this rect (document coordinates) are computed; the pass is skipped for + /// chunks it misses. Consumers of the slot must be clipped alike. + pub clip: Option, +} + +/// Pixels a pass samples: a surface of the document, or one derived from it on the CPU (a +/// combined pixel × vector mask, a shape's fill or stroke alone). +#[derive(Clone, Debug)] +pub enum SurfaceRef<'a> { + Doc(&'a Surface), + Derived(std::sync::Arc), +} + +impl SurfaceRef<'_> { + pub fn get(&self) -> &Surface { + match self { + SurfaceRef::Doc(s) => s, + SurfaceRef::Derived(s) => s, + } + } +} + +#[derive(Clone, Debug)] +pub struct MaskUse<'a> { + pub layer: LayerId, + pub surface: SurfaceRef<'a>, + pub density: f32, + pub default: f32, +} + +impl<'a> Pass<'a> { + pub(crate) fn new(kernel: Kernel, dst: Slot) -> Self { + Pass { + kernel, + dst, + a: None, + b: None, + c: None, + d: None, + mode: BlendMode::Normal, + opacity: 1.0, + fill: 1.0, + tex: None, + color: [0.0; 4], + mask: None, + adjust_kind: 0, + params: [[0.0; 4]; 4], + extra: [0.0; 4], + flags: 0, + lut: None, + gradient: false, + map: None, + pattern: None, + clip: None, + } + } +} + +/// Why a document can't be composited on the GPU (the caller falls back to the CPU compositor). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Unsupported(pub String); + +impl std::fmt::Display for Unsupported { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "GPU compositor: {}", self.0) + } +} + +impl std::error::Error for Unsupported {} + +/// A layer whose effects the plan renders: its maps are built (or reused) before the chunks run. +#[derive(Clone, Copy, Debug)] +pub struct FxLayer<'a> { + pub layer: &'a Layer, + /// Region the maps cover (`compose::effect_maps`). + pub region: Rect, + /// Layer bounds (gradients and linked patterns are laid out in them). + pub bounds: Rect, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Checkpoint { + /// First root layer included after occlusion pruning. + pub layer_start: usize, + /// Root layers below this boundary (including the complete clipping unit). + pub layer_end: usize, + /// Resume at this pass with `slot` restored. + pub pass_end: usize, + pub slot: Slot, +} + +/// Document-level inputs of a plan. +#[derive(Clone, Copy)] +pub struct DocCtx<'a> { + pub canvas: Rect, + pub transfer: Transfer, + pub light: GlobalLight, + pub patterns: &'a [Pattern], + /// Colour mode (channel restrictions name its channels). + pub mode: photocraft_color::ColorMode, + /// Sample depth (adjustment results are rounded to it). + pub depth: photocraft_color::SampleType, + /// The document (effect geometry the CPU computes, e.g. gradient stroke frames). + pub doc: &'a Document, +} + +impl<'a> DocCtx<'a> { + pub fn of(doc: &'a Document) -> Self { + DocCtx { + canvas: doc.bounds(), + transfer: Transfer::for_document(doc.mode, doc.depth), + light: doc.global_light, + patterns: &doc.patterns, + mode: doc.mode, + depth: doc.depth, + doc, + } + } +} diff --git a/crates/gpu/src/prefix.rs b/crates/gpu/src/prefix.rs index 34c70916e0..31406c6449 100644 --- a/crates/gpu/src/prefix.rs +++ b/crates/gpu/src/prefix.rs @@ -18,15 +18,16 @@ use std::collections::HashMap; use photocraft_doc::{Document, Layer}; use photocraft_geom::Rect; -use crate::Tex; -use crate::plan::{Checkpoint, Plan}; +use crate::plan::Checkpoint; pub(crate) const MAX_BYTES: u64 = 512 << 20; /// One whole grid chunk, with the document rectangles that hold valid pixels: a whole-chunk /// capture covers the cell in one entry, partial captures accumulate alongside each other. +/// The texture is only copied to and from, so it has no view: Vulkan rejects a view on an image +/// without a sampled/storage/attachment usage (VUID-VkImageViewCreateInfo-image-04441). pub(crate) struct Chunk { - tex: Tex, + tex: wgpu::Texture, covered: Vec, /// The checkpoint the covered pixels hold. checkpoint: Checkpoint, @@ -103,8 +104,10 @@ impl Prefix { self.context.as_ref().map(|d| d.id) } - /// Drops the chunks `doc` invalidated and returns the checkpoint this render captures at. - pub(crate) fn prepare(&mut self, doc: &Document, plan: &Plan<'_>) -> Option { + /// Drops the chunks `doc` invalidated and returns the checkpoint this render captures at, one + /// of `checkpoints` (the plan's that a batch run starts at). A chunk held at any other point + /// is dropped too: resuming there would group the layers above it differently. + pub(crate) fn prepare(&mut self, doc: &Document, checkpoints: &[Checkpoint]) -> Option { if self.budget == 0 { return None; } @@ -124,7 +127,7 @@ impl Prefix { if same_context { let mut freed = 0u64; self.chunks.retain(|_, c| { - let keep = changed.is_none_or(|ch| c.checkpoint.layer_end <= ch) && plan.checkpoints.contains(&c.checkpoint); + let keep = changed.is_none_or(|ch| c.checkpoint.layer_end <= ch) && checkpoints.contains(&c.checkpoint); if !keep { freed = freed.saturating_add(c.bytes); } @@ -134,12 +137,12 @@ impl Prefix { } else { self.clear(); } - let kept = self.checkpoint.filter(|c| same_context && changed.is_none() && plan.checkpoints.contains(c)); + let kept = self.checkpoint.filter(|c| same_context && changed.is_none() && checkpoints.contains(c)); let checkpoint = kept.or_else(|| match changed { // Just below the edit; nothing when it is at the bottom. - Some(changed) if same_context => plan.checkpoints.iter().rev().find(|c| c.layer_end <= changed && c.pass_end > 1).copied(), + Some(changed) if same_context => checkpoints.iter().rev().find(|c| c.layer_end <= changed && c.pass_end > 1).copied(), _ => { - let mut useful = plan.checkpoints.iter().rev().filter(|c| c.pass_end > 1); + let mut useful = checkpoints.iter().rev().filter(|c| c.pass_end > 1); let last = useful.next().copied(); useful.next().copied().or(last) } @@ -172,7 +175,7 @@ impl Prefix { let Some((chunk, (ox, oy))) = self.covered_by(rect) else { return false }; encoder.copy_texture_to_texture( wgpu::TexelCopyTextureInfo { - texture: &chunk.tex.texture, + texture: &chunk.tex, mip_level: 0, origin: wgpu::Origin3d { x: ox.unsigned_abs(), y: oy.unsigned_abs(), z: 0 }, aspect: wgpu::TextureAspect::All, @@ -198,8 +201,16 @@ impl Prefix { if cell_bytes == 0 || cell_bytes > self.budget.saturating_sub(self.bytes) { return; } - let tex = - Tex::new(device, "pc_compose_prefix", cell.width(), cell.height(), src.format(), wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST); + let tex = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_compose_prefix"), + size: wgpu::Extent3d { width: cell.width().max(1), height: cell.height().max(1), depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: src.format(), + usage: wgpu::TextureUsages::COPY_SRC | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); self.bytes = self.bytes.saturating_add(cell_bytes); self.chunks.insert(cell, Chunk { tex, covered: Vec::new(), checkpoint, bytes: cell_bytes }); } @@ -221,7 +232,7 @@ impl Prefix { encoder.copy_texture_to_texture( wgpu::TexelCopyTextureInfo { texture: src, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, wgpu::TexelCopyTextureInfo { - texture: &chunk.tex.texture, + texture: &chunk.tex, mip_level: 0, origin: wgpu::Origin3d { x: ox.unsigned_abs(), y: oy.unsigned_abs(), z: 0 }, aspect: wgpu::TextureAspect::All, diff --git a/crates/gpu/src/readback.rs b/crates/gpu/src/readback.rs new file mode 100644 index 0000000000..9abf5f071e --- /dev/null +++ b/crates/gpu/src/readback.rs @@ -0,0 +1,70 @@ +use super::textures::Stats; +use super::{Compositor, Document, Rect, Unsupported}; + +/// Blocking readback of a composite (native tests and tools). +#[cfg(not(target_arch = "wasm32"))] +pub fn render_to_vec(comp: &mut Compositor, device: &wgpu::Device, queue: &wgpu::Queue, doc: &Document, rect: Rect) -> Result, Unsupported> { + render_to_vec_stats(comp, device, queue, doc, rect).map(|(v, _)| v) +} + +/// [`render_to_vec`] plus the render's [`Stats`]. +#[cfg(not(target_arch = "wasm32"))] +pub fn render_to_vec_stats( + comp: &mut Compositor, + device: &wgpu::Device, + queue: &wgpu::Queue, + doc: &Document, + rect: Rect, +) -> Result<(Vec<[f32; 4]>, Stats), Unsupported> { + let rect = rect.intersect(&doc.bounds()); + let mut staging: Vec<(Rect, wgpu::Buffer, u32)> = Vec::new(); + let mut bpp = 16u32; + let stats = comp.render(device, queue, doc, rect, |enc, out| { + // Bytes per pixel of the accumulation format (Rgba32Float = 16, Rgba16Float = 8). + bpp = if out.texture.format() == wgpu::TextureFormat::Rgba32Float { 16 } else { 8 }; + let row = (out.rect.width() * bpp).div_ceil(256) * 256; + let buf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_readback"), + size: (row * out.rect.height()) as u64, + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + enc.copy_texture_to_buffer( + wgpu::TexelCopyTextureInfo { texture: out.texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyBufferInfo { + buffer: &buf, + layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row), rows_per_image: Some(out.rect.height()) }, + }, + wgpu::Extent3d { width: out.rect.width(), height: out.rect.height(), depth_or_array_layers: 1 }, + ); + staging.push((out.rect, buf, row)); + })?; + for (_, b, _) in &staging { + b.slice(..).map_async(wgpu::MapMode::Read, |_| {}); + } + match &comp.health { + Some(h) if !h.wait(device, None) => return Err(Unsupported(h.fault().map_or_else(|| "GPU device lost".into(), |f| f.to_string()))), + Some(_) => {} + None => { + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + } + } + let w = rect.width() as usize; + let mut out = vec![[0.0f32; 4]; w * rect.height() as usize]; + for (r, b, row) in &staging { + let data = b.slice(..).get_mapped_range().map_err(|e| Unsupported(format!("GPU readback failed: {e:?}")))?; + for y in 0..r.height() as usize { + for x in 0..r.width() as usize { + let o = y * *row as usize + x * bpp as usize; + let px: [f32; 4] = if bpp == 16 { + std::array::from_fn(|i| f32::from_le_bytes([data[o + i * 4], data[o + i * 4 + 1], data[o + i * 4 + 2], data[o + i * 4 + 3]])) + } else { + std::array::from_fn(|i| half::f16::from_le_bytes([data[o + i * 2], data[o + i * 2 + 1]]).to_f32()) + }; + let (dx, dy) = ((r.x0 - rect.x0) as usize + x, (r.y0 - rect.y0) as usize + y); + out[dy * w + dx] = px; + } + } + } + Ok((out, stats)) +} diff --git a/crates/gpu/src/setup.rs b/crates/gpu/src/setup.rs new file mode 100644 index 0000000000..15217ed236 --- /dev/null +++ b/crates/gpu/src/setup.rs @@ -0,0 +1,509 @@ +use super::batch::tile_grid; +use super::consts::{ACC_FORMAT, CHUNK, CHUNK_UNIFORM, FX_BUDGET, MAP16, MAP32, MAX_PATTERN_TEXTURE_BYTES, OP_UNIFORM, PAGE, PATTERN_BUDGET, RESIDENT_BUDGET}; +use super::kit::{Kit, SHADER, tex_entry, uniform_entry}; +use super::paging::{cell_rect, field_reaches, first_error, grid_rects}; +use super::textures::{FxEntry, Resident, Tex, TexKind}; +use super::{ + Compositor, DeviceHealth, DocId, Document, Fault, HashMap, Kernel, LayerContent, Plan, Rect, Role, TILE_SIZE, Unsupported, fx, plan, prefix, timing, +}; + +impl Compositor { + /// Create a compositor with the default accumulation format ([`ACC_FORMAT`]). + pub fn new(device: &wgpu::Device) -> Self { + Self::new_with_format(device, ACC_FORMAT) + } + + /// The accumulation format to use on `adapter`: `Rgba32Float` when it can be a render target, + /// else `Rgba16Float`. Some drivers (e.g. Intel Vulkan) don't support rendering to `Rgba32Float` + /// and would otherwise panic at render-pipeline creation. + pub fn preferred_acc_format(adapter: &wgpu::Adapter) -> wgpu::TextureFormat { + let feats = adapter.get_texture_format_features(wgpu::TextureFormat::Rgba32Float); + if feats.allowed_usages.contains(wgpu::TextureUsages::RENDER_ATTACHMENT) { wgpu::TextureFormat::Rgba32Float } else { wgpu::TextureFormat::Rgba16Float } + } + + /// [`Compositor::new_with_format`], but a shader or pipeline the driver can't build (e.g. a + /// D3D12 shader-compiler failure) is an `Err` instead of a panic, so the caller can fall back + /// to the CPU compositor. Where errors only arrive asynchronously (WebGPU), creation is assumed + /// to have worked. + pub fn try_new_with_format(device: &wgpu::Device, acc_format: wgpu::TextureFormat) -> Result { + match first_error(device, || Self::build(device, acc_format)) { + ((comp, None), None) => Ok(comp), + ((_, Some(e)), _) | (_, Some(e)) => Err(Unsupported(format!("couldn't build the compositor's pipelines: {e}"))), + } + } + + /// Create a compositor whose accumulation/render-target format is `acc_format`. Pass + /// `Rgba16Float` on adapters that can't render to `Rgba32Float` (e.g. some Intel Vulkan drivers), + /// which otherwise panics at render-pipeline creation. See `Compositor::preferred_acc_format`. + pub fn new_with_format(device: &wgpu::Device, acc_format: wgpu::TextureFormat) -> Self { + let (comp, error) = Self::build(device, acc_format); + if let Some(e) = error { + log::error!("GPU compositor: couldn't build its pipelines: {e}"); + } + comp + } + + /// The compositor and the first error raised building its (non-map) pipelines. They are + /// built in parallel, each inside its own error scope on the thread that builds it (wgpu's + /// scopes are per thread): on DX12, FXC compiles one shader at a time and the ~30 pipelines + /// took about 8 s on one core (#6419). + fn build(device: &wgpu::Device, acc_format: wgpu::TextureFormat) -> (Self, Option) { + let module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_compose"), source: wgpu::ShaderSource::Wgsl(SHADER.into()) }); + let bgl0 = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_compose_uniforms"), + entries: &[uniform_entry(0, CHUNK_UNIFORM), uniform_entry(1, OP_UNIFORM)], + }); + let bgl1 = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_compose_textures"), + entries: &(0..8).map(tex_entry).collect::>(), + }); + let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("pc_compose"), + bind_group_layouts: &[Some(&bgl0), Some(&bgl1)], + immediate_size: 0, + }); + // `None` only for kernels without a fragment entry point, which are never drawn. + let pipeline = |k: Kernel, format: wgpu::TextureFormat| { + let entry = k.entry()?; + Some(device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(entry), + layout: Some(&layout), + vertex: wgpu::VertexState { module: &module, entry_point: Some("vs"), buffers: &[], compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState::default(), + depth_stencil: None, + multisample: wgpu::MultisampleState::default(), + fragment: Some(wgpu::FragmentState { + module: &module, + entry_point: Some(entry), + targets: &[Some(wgpu::ColorTargetState { format, blend: None, write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + })) + }; + // Effect maps render to R32Float / R16Float, which some adapters (e.g. GL without float + // render targets) can't: then layer effects use the CPU compositor and the rest stays here. + let keys = Kernel::DRAWN.iter().filter(|k| !k.is_map()).map(|&k| (k, acc_format)); + let keys: Vec<_> = keys.chain(Kernel::DRAWN.iter().filter(|k| k.is_map()).flat_map(|&k| [(k, MAP32), (k, MAP16)])).collect(); + let built = fx::par_map(keys, |key| { + let (built, error) = first_error(device, || pipeline(key.0, key.1)); + (key, built, error) + }); + let (mut pipelines, mut maps) = (HashMap::new(), Vec::new()); + let (mut error, mut map_error) = (None, None); + for (key, built, e) in built { + let map = key.0.is_map(); + // A failed map pipeline turns all effect maps off below. A failed non-map pipeline + // keeps its (invalid) handle: draws look pipelines up by key, so dropping it would + // turn a validation error into a missing-key panic on the first draw. + if let Some(e) = e { + if map { + map_error.get_or_insert(e); + continue; + } + error.get_or_insert(e); + } + let Some(p) = built else { continue }; + if map { + maps.push((key, p)); + } else { + pipelines.insert(key, p); + } + } + if let Some(e) = &map_error { + log::info!("GPU compositor: no effect-map pipelines ({e}); layer effects use the CPU"); + } else { + pipelines.extend(maps); + } + let dummy = Tex::new(device, "pc_compose_dummy", 1, 1, wgpu::TextureFormat::Rgba8Unorm, wgpu::TextureUsages::TEXTURE_BINDING).view; + let kit = Kit { pipelines, bgl0, bgl1, dummy }; + let comp = Self::from_kit(kit, device.limits().max_texture_dimension_2d, acc_format, map_error.is_none()); + (comp, error) + } + + /// Create an independent compositor on the same device, reusing compiled pipelines. + /// Texture caches, budgets, focus and timing start at their defaults; no document state is + /// shared. Render with the same device and queue as this compositor. + pub fn sibling(&self) -> Self { + let mut sibling = Self::from_kit(self.kit.clone(), self.device_max, self.acc_format, self.effect_maps); + sibling.health = self.health.clone(); + sibling + } + + fn from_kit(kit: Kit, device_max: u32, acc_format: wgpu::TextureFormat, effect_maps: bool) -> Self { + Self { + kit, + pool: Vec::new(), + residents: HashMap::new(), + fx: HashMap::new(), + prefix: prefix::Prefix::default(), + temps: Vec::new(), + patterns: HashMap::new(), + pattern_mode: photocraft_color::ColorMode::Rgb, + pattern_doc: DocId(0), + pattern_budget: PATTERN_BUDGET, + max_dim: 0, + device_max, + page: PAGE, + chunk: CHUNK, + resident_budget: RESIDENT_BUDGET, + fx_budget: (FX_BUDGET as u64).saturating_sub(prefix::MAX_BYTES), + layer_need: 0, + lent: 0, + exact_resident: false, + focus: None, + frame: 0, + stamp: 0, + staged: 0, + recorded: 0, + layer_alloc: None, + admit_freed: 0, + fail_allocs: 0, + acc_format, + effect_maps, + cmyk: 0, + health: None, + timer: None, + } + .with_texture_limit(device_max) + } + + fn with_texture_limit(mut self, max: u32) -> Self { + self.set_texture_limit(max); + self + } + + /// Plan paging for a texture side limit of `max` (clamped to the device's own limit and at + /// least one tile). Tests use a small limit to exercise documents larger than it on any + /// adapter. Drops every cached texture. + pub fn set_texture_limit(&mut self, max: u32) { + let tile = TILE_SIZE as u32; + self.max_dim = max.min(self.device_max).max(tile); + let mut page = tile; + while page * 2 <= self.max_dim.min(PAGE) { + page *= 2; + } + self.page = page; + self.chunk = CHUNK.min(page); + self.prefix.set_step(self.chunk); + self.pool.clear(); + self.residents.clear(); + self.fx.clear(); + self.temps.clear(); + self.prefix.clear(); + } + + /// The page side layer surfaces are stored in. + pub fn page_size(&self) -> u32 { + self.page + } + + /// Test hook: the next `n` layer page textures fail as if the device were out of memory + /// (a real one can't be forced), so the fallback paths of #6563 can be tested. + #[doc(hidden)] + pub fn fail_layer_allocs(&mut self, n: u32) { + self.fail_allocs = n; + } + + /// Set the GPU memory resident layer pages may hold (default [`RESIDENT_BUDGET`]), exactly: + /// nothing is lent to it ([`Self::fit_layers`]) until the next [`Self::set_memory_budget`]. + pub fn set_resident_budget(&mut self, bytes: u64) { + // The loan goes back to the effect maps, so the total budget stays what it was. + self.fx_budget = self.fx_budget.saturating_add(self.lent); + self.lent = 0; + self.resident_budget = bytes; + self.exact_resident = true; + } + + /// Set the GPU memory the compositor may hold for layer pages and caches together + /// (a quarter of it, at most [`FX_BUDGET`], for effect maps and lower-stack chunks). Uploads and evicted pages in + /// flight during a refresh stay within about a quarter more. + pub fn set_memory_budget(&mut self, bytes: u64) { + let caches = (FX_BUDGET as u64).min(bytes / 4); + self.prefix.set_budget((caches / 2).min(prefix::MAX_BYTES)); + self.fx_budget = caches.saturating_sub(self.prefix.budget()); + self.resident_budget = bytes.saturating_sub(caches); + self.lent = 0; + self.exact_resident = false; + self.lend(); + } + + /// Record that a render needs `needed` bytes of layer pages and return the resident budget + /// they now have. A render of the `whole` document sets the need; a part of it only raises + /// it, so a large partial refresh never shrinks what the next full one keeps resident. + /// + /// Pages that miss their share by less than the effect-map share borrow from it: an evicted + /// effect map is rebuilt by the next refresh that draws it, but one page too many sends the + /// whole refresh to the CPU compositor, 10× slower (#5973, P60: 150 layers of 6 MP on an + /// integrated GPU missed by 5 % and took 680 ms instead of 62). + /// The lower-stack share is never lent: it is what keeps a mid-stack edit at half the work. + /// The loan holds across [`Self::set_memory_budget`] until a later call needs less or more. + pub fn fit_layers(&mut self, needed: u64, whole: bool) -> u64 { + self.layer_need = if whole { needed } else { self.layer_need.max(needed) }; + self.lend(); + self.resident_budget + } + + /// Return the loan to the effect maps, then lend what [`Self::layer_need`] still lacks, if + /// the effect-map share covers it (lending part of it would fit nothing and only evict maps). + fn lend(&mut self) { + self.fx_budget = self.fx_budget.saturating_add(self.lent); + self.resident_budget = self.resident_budget.saturating_sub(self.lent); + let short = self.layer_need.saturating_sub(self.resident_budget); + self.lent = if short <= self.fx_budget && !self.exact_resident { short } else { 0 }; + self.fx_budget = self.fx_budget.saturating_sub(self.lent); + self.resident_budget = self.resident_budget.saturating_add(self.lent); + } + + /// The budget of layer pages, effect maps and lower-stack chunks (bytes). + pub fn memory_budget(&self) -> u64 { + self.resident_budget.saturating_add(self.fx_budget).saturating_add(self.prefix.budget()) + } + + /// Bytes held by reusable lower-stack chunks, within the overall cache allowance. + pub fn composite_cache_bytes(&self) -> u64 { + self.prefix.bytes() + } + + /// Cap lower-stack reuse (default at most 512 MiB); zero disables it. The released allowance + /// goes back to effect maps, preserving the total budget and layer-page allowance. + pub fn set_composite_cache_budget(&mut self, bytes: u64) { + let caches = self.fx_budget.saturating_add(self.prefix.budget()); + self.prefix.set_budget(bytes.min(caches)); + self.fx_budget = caches.saturating_sub(self.prefix.budget()); + } + + /// Bytes of the layer pages a render of `region` needs resident (each layer and mask + /// surface's tiles within the region's page cells, in their texture format). + pub fn working_set(&self, doc: &Document, region: Rect) -> u64 { + let Ok(plan) = plan(doc) else { return 0 }; + let grid = tile_grid(doc.bounds()); + let cells = grid_rects(region.intersect(&doc.bounds()), self.page) + .into_iter() + .map(|r| cell_rect((r.x0.div_euclid(self.page as i32), r.y0.div_euclid(self.page as i32)), self.page)) + .fold(Rect::EMPTY, |a, c| if a.is_empty() { c } else { a.union(&c) }); + let mut seen = std::collections::HashSet::new(); + let mut total = 0u64; + for p in &plan.passes { + let sources = [p.tex.as_ref().map(|t| (t.layer, t.role, t.surface.get())), p.mask.as_ref().map(|m| (m.layer, Role::Mask, m.surface.get()))]; + for (layer, role, s) in sources.into_iter().flatten() { + if seen.insert((layer, role)) { + let a = s.tile_bounds().intersect(&grid).intersect(&cells); + total += a.width() as u64 * a.height() as u64 * TexKind::for_surface(role, s.format()).bytes_per_pixel() as u64; + } + } + } + total + } + + /// The GPU memory resident layer pages may hold (bytes; see [`Compositor::set_memory_budget`]). + pub fn resident_budget(&self) -> u64 { + self.resident_budget + } + + /// Whether a render of `region` keeps every layer page it needs within the budget; when it + /// doesn't, each render re-uploads what was evicted. + pub fn fits_budget(&self, doc: &Document, region: Rect) -> bool { + self.working_set(doc, region) <= self.resident_budget + } + + /// The document area the view shows (`None`: unknown). Full refreshes draw its cells last. + pub fn set_focus(&mut self, focus: Option) { + self.focus = focus; + } + + /// GPU memory held by resident layer pages (bytes). + pub fn resident_bytes(&self) -> u64 { + self.residents.values().map(Resident::bytes).sum() + } + + /// GPU memory held by cached pattern textures (bytes). + pub fn pattern_cache_bytes(&self) -> u64 { + self.patterns.values().map(|p| p.bytes).sum() + } + + /// Set the GPU memory pattern textures may hold (default [`PATTERN_BUDGET`]). It takes effect + /// at the end of the next refresh. + pub fn set_pattern_budget(&mut self, bytes: u64) { + self.pattern_budget = bytes; + } + + /// Whether `doc` can be composited on the GPU. Documents and layers larger than the texture + /// limit are fine: surfaces are stored in page-sized textures and effect maps of regions + /// larger than the limit are built per page cell. + pub fn supports(&self, doc: &Document) -> Result<(), Unsupported> { + let p = plan(doc)?; + self.check_fx(doc, &p) + } + + /// Whether the maps of an effect region are built per page cell (it exceeds the limit). + pub(crate) fn fx_paged(&self, region: Rect) -> bool { + region.width() > self.max_dim || region.height() > self.max_dim + } + + /// How far beyond a page cell the effect maps of `f` must be computed to be exact in it. + pub(crate) fn fx_apron(doc: &Document, f: &plan::FxLayer<'_>) -> i32 { + let vector_shape = matches!(f.layer.content, LayerContent::Shape(_)) && photocraft_compose::effect_outline(f.layer).is_none(); + let progs: Vec = f + .layer + .effects + .items + .iter() + .filter(|e| e.enabled()) + .map(|e| fx::program_with(e, &doc.global_light, vector_shape, &doc.patterns, (0.0, 0.0))) + .collect(); + let want = field_reaches(&progs); + // Distance fields are exact one pixel inside the window they are computed from. + progs.iter().map(|p| p.reach(&|k| want.get(&k).copied().unwrap_or(0))).max().unwrap_or(0).saturating_add(2) + } + + /// Effect maps must fit in textures (per page cell when the region exceeds the limit), and + /// patterns in one texture. + pub(crate) fn check_fx(&self, doc: &Document, p: &Plan<'_>) -> Result<(), Unsupported> { + if !self.effect_maps && !p.fx.is_empty() { + return Err(Unsupported("layer effects need float effect-map render targets, which this GPU lacks".into())); + } + for f in &p.fx { + if self.fx_paged(f.region) { + let side = i64::from(self.page) + 2 * i64::from(Self::fx_apron(doc, f)); + if side > i64::from(self.device_max) { + return Err(Unsupported(format!("effects of `{}` reach too far to build per page", f.layer.name))); + } + } + for e in f.layer.effects.items.iter().filter(|e| e.enabled()) { + // Effect noise is speckled in the CPU maps; keep CPU and GPU identical by + // rendering noise-carrying effects on the CPU path. + let noisy = match e { + photocraft_doc::Effect::DropShadow(s) | photocraft_doc::Effect::InnerShadow(s) => s.noise > 0.0, + photocraft_doc::Effect::OuterGlow(g) | photocraft_doc::Effect::InnerGlow(g) => g.noise > 0.0, + _ => false, + }; + if noisy { + return Err(Unsupported(format!("effect noise on `{}` uses the CPU path", f.layer.name))); + } + for s in fx::program_with(e, &doc.global_light, false, &doc.patterns, (0.0, 0.0)).stages { + if s.lut.as_ref().is_some_and(|l| l.len() > 4096) { + return Err(Unsupported(format!("effect blur on `{}` too wide for the GPU path", f.layer.name))); + } + if let Some(pat) = &s.pattern { + self.check_pattern(pat)?; + } + } + } + } + for pass in &p.passes { + if let Some(pat) = pass.pattern { + self.check_pattern(pat)?; + } + } + Ok(()) + } + + /// A pattern's texture must fit the device and the per-pattern cap; a larger one renders on + /// the CPU compositor. + fn check_pattern(&self, pat: &photocraft_doc::Pattern) -> Result<(), Unsupported> { + if pat.width > self.device_max || pat.height > self.device_max { + return Err(Unsupported(format!("pattern `{}` larger than the GPU texture limit", pat.name))); + } + if u64::from(pat.width) * u64::from(pat.height) * 16 > MAX_PATTERN_TEXTURE_BYTES { + return Err(Unsupported(format!("pattern `{}` too large for a GPU texture; the CPU path paints it", pat.name))); + } + Ok(()) + } + + /// Drop GPU textures of a closed document. + pub fn forget_doc(&mut self, doc: DocId) { + self.residents.retain(|_, r| r.doc != doc); + self.fx.retain(|_, e| e.doc != doc); + self.patterns.retain(|_, p| p.doc != doc); + self.prefix.forget(doc); + } + + /// [`Self::forget_doc`] for every document `keep` rejects: what the compositor holds for + /// documents that were closed, however their canvas entry went (#4529). + pub fn retain_docs(&mut self, keep: impl Fn(DocId) -> bool) { + let mut gone: Vec = self + .residents + .values() + .map(|r| r.doc) + .chain(self.fx.values().map(|e| e.doc)) + .chain(self.patterns.values().map(|p| p.doc)) + .chain(self.prefix.doc()) + .filter(|d| !keep(*d)) + .collect(); + gone.sort_by_key(|d| d.0); + gone.dedup(); + for d in gone { + self.forget_doc(d); + } + } + + /// Documents the compositor holds pages, effect maps, pattern textures or a lower-stack + /// composite for. + pub fn held_docs(&self) -> Vec { + let mut docs: Vec = self + .residents + .values() + .map(|r| r.doc) + .chain(self.fx.values().map(|e| e.doc)) + .chain(self.patterns.values().map(|p| p.doc)) + .chain(self.prefix.doc()) + .collect(); + docs.sort_by_key(|d| d.0); + docs.dedup(); + docs + } + + /// GPU memory held by cached effect maps (bytes). + pub fn fx_cache_bytes(&self) -> usize { + self.fx.values().map(FxEntry::bytes).sum() + } + + /// Watch `health` (see [`DeviceHealth::watch`]): once the device is lost or reported an + /// error, every render returns [`Unsupported`] without touching the GPU, so callers use the + /// CPU compositor instead. + pub fn set_health(&mut self, health: DeviceHealth) { + self.health = Some(health); + } + + /// Why the device can't be used (`None` while healthy or unwatched). + pub fn fault(&self) -> Option { + self.health.as_ref().and_then(DeviceHealth::fault) + } + + /// Measure how long the GPU spends executing each [`Compositor::render`], with timestamp + /// queries read back asynchronously (nothing waits for the GPU). Returns whether the device + /// supports it: without [`timing::FEATURES`] there is no measurement at all, and callers + /// report no GPU time rather than the encode-and-submit cost `composite_ms` already is. + /// + /// A measurement arrives a frame or two after the refresh it belongs to, so it carries that + /// refresh's own pixel count; take it with [`Compositor::take_gpu_timing`]. + pub fn measure_gpu_time(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) -> bool { + // `PHOTOCRAFT_NO_GPU_TIMING` turns it off: a kill switch for a driver whose timestamp + // queries misbehave, and the control arm for measuring what the queries themselves + // cost — the device is identical either way, so only the instrumentation varies. + if std::env::var_os("PHOTOCRAFT_NO_GPU_TIMING").is_some() { + return false; + } + if self.timer.is_none() { + self.timer = timing::GpuTimer::new(device, queue); + } + self.timer.is_some() + } + + /// Whether [`Compositor::measure_gpu_time`] is on for this compositor. + pub fn measures_gpu_time(&self) -> bool { + self.timer.is_some() + } + + /// The most recent finished GPU measurement, leaving none behind (a stale number is never + /// reported twice as new). `None` while unmeasured, unsupported, or still in flight. + /// + /// [`Compositor::render`] already drains this into [`Stats::gpu_time`], so the app never + /// calls it; it is how a bench or a test collects a measurement without issuing another + /// render to carry it. + pub fn take_gpu_timing(&mut self) -> Option { + self.timer.as_mut().and_then(timing::GpuTimer::take) + } +} diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs new file mode 100644 index 0000000000..281c67421e --- /dev/null +++ b/crates/gpu/src/tests.rs @@ -0,0 +1,390 @@ +use super::*; + +/// A run joins `LayerBlend` passes that only feed each other, at most `BATCH` of them, and +/// never ends in the slot it reads from. +fn check_runs(p: &Plan<'_>) -> Vec { + let runs = batch_runs(p); + assert_eq!(runs.len(), p.passes.len()); + for (i, &k) in runs.iter().enumerate() { + assert!((1..=BATCH).contains(&k), "pass {i}: run {k}"); + if k > 1 { + assert_ne!(Some(p.passes[i + k - 1].dst), p.passes[i].a, "run at {i} renders into its input"); + for j in i + 1..i + k { + assert_eq!(p.passes[j].kernel, Kernel::LayerBlend); + assert_eq!(p.passes[j].a, Some(p.passes[j - 1].dst), "run at {i}: pass {j} doesn't read the one before"); + assert!(p.passes[j].mask.is_none() && p.passes[j].mode != photocraft_color::BlendMode::Dissolve, "run at {i}: pass {j}"); + } + } + } + runs +} + +#[test] +fn plain_layers_batch_and_runs_stop_where_a_pass_must_stand_alone() { + use photocraft_color::{BlendMode, Color, ColorMode, SampleType}; + use photocraft_doc::{Document, Layer, LayerMask}; + let mut d = Document::with_background("t", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::WHITE); + for i in 0..12 { + let mut l = Layer::raster(format!("l{i}"), d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(0, 0, 8, 8), &[0.2, 0.4, 0.6, 0.5]); + d.layers.push(l); + } + let p = plan::plan(&d).unwrap(); + let runs = check_runs(&p); + assert!(runs.iter().any(|k| *k >= 3), "{runs:?}"); + // The cache resumes only where a run starts, so a cached render draws the passes above + // its checkpoint in the same runs as an uncached one; checkpoints inside a run are + // skipped, but a stack of plain layers keeps some to resume at. + let starts = run_starts(&runs); + assert_eq!(starts.len(), runs.len() + 1); + assert!(starts[0] && starts[runs.len()]); + let (mut at, mut i) = (Vec::new(), 0); + while i < runs.len() { + at.push(i); + i += runs[i]; + } + assert_eq!(starts.iter().enumerate().filter(|(_, s)| **s).map(|(i, _)| i).collect::>(), [at, vec![runs.len()]].concat()); + let resumable = p.checkpoints.iter().filter(|c| c.pass_end > 1 && starts[c.pass_end]).count(); + assert!(resumable >= 2 && resumable < p.checkpoints.len(), "{resumable} of {:?}", p.checkpoints); + // A masked or Dissolve layer, or a clipped one, never joins a run as an extra layer. + d.layers[3].mask = Some(LayerMask::hide_all()); + d.layers[6].blend = BlendMode::Dissolve; + d.layers[9].clipped = true; + check_runs(&plan::plan(&d).unwrap()); + // Nothing to batch: a lone layer. + d.layers.truncate(1); + assert_eq!(check_runs(&plan::plan(&d).unwrap()), vec![1; plan::plan(&d).unwrap().passes.len()]); +} + +#[test] +fn batch_records_hold_three_extra_layers_in_the_spare_words() { + let lead = plan::Pass::new(Kernel::LayerBlend, 0); + let mut x = plan::Pass::new(Kernel::LayerBlend, 1); + x.opacity = 0.25; + x.fill = 0.5; + x.color = [0.1, 0.2, 0.3, 0.4]; + let w = batch_op_words(&lead, None, None, &[(&x, Some([1, 2, 3, 4])), (&x, None), (&x, None), (&x, None)]); + assert_eq!(w.len() as u64, OP_UNIFORM); + let word = |i: usize| <[u8; 4]>::try_from(&w[i * 4..i * 4 + 4]).unwrap(); + let f = |i: usize| f32::from_le_bytes(word(i)); + // At most three extras; the first's geometry, colour and parameters in p0, p1, p2. + assert_eq!(i32::from_le_bytes(word(1)), 3); + assert_eq!([f(20), f(21), f(22), f(23)], [1.0, 2.0, 3.0, 4.0]); + assert_eq!([f(24), f(25), f(26), f(27)], [0.1, 0.2, 0.3, 0.4]); + assert_eq!([f(28), f(29), f(31)], [0.25, 0.5, 1.0]); + // The third's parameters in q4; one without pixels says so. + assert_eq!([f(60), f(61), f(63)], [0.25, 0.5, 0.0]); + // Other passes leave the batch words zero. + let plain = op_words(&x, None, None, None); + assert!(plain[44 * 4..].iter().all(|b| *b == 0)); +} + +#[test] +fn runs_of_normal_layers_draw_with_normal_code_alone() { + use photocraft_color::BlendMode; + let pass = |mode: BlendMode, flags: u32| { + let mut p = plan::Pass::new(Kernel::LayerBlend, 0); + p.mode = mode; + p.flags = flags; + p + }; + let normal = pass(BlendMode::Normal, plan::F_HDR); + assert_eq!(batch_kernel(&[normal.clone(), pass(BlendMode::Normal, 0)]), Kernel::LayerBatchNormal); + // Any other mode, text gamma or a Lab document anywhere in the run takes the full shader. + for other in [pass(BlendMode::Multiply, 0), pass(BlendMode::Dissolve, 0), pass(BlendMode::Normal, plan::F_TEXT_GAMMA), pass(BlendMode::Normal, plan::F_LAB)] + { + assert_eq!(batch_kernel(&[normal.clone(), other.clone()]), Kernel::LayerBatch); + assert_eq!(batch_kernel(&[other, normal.clone()]), Kernel::LayerBatch); + } +} + +#[test] +fn layer_pages_are_admitted_by_evicting_least_recently_used_first() { + let pages = || vec![(5, 'c', 40), (1, 'a', 30), (3, 'b', 20)]; + // Fits already: nothing dropped. + assert!(lru_victims(pages(), 90, 10, 100).is_empty()); + // 90 held + 40 incoming over 100: the oldest page goes first, then the next. + assert_eq!(lru_victims(pages(), 90, 40, 100), vec!['a']); + assert_eq!(lru_victims(pages(), 90, 60, 100), vec!['a', 'b']); + // Out of memory: a zero budget drops every candidate, and an impossible request stops + // when none are left instead of looping or overflowing. + assert_eq!(lru_victims(pages(), 90, 0, 0), vec!['a', 'b', 'c']); + assert_eq!(lru_victims(pages(), u64::MAX, u64::MAX, 1), vec!['a', 'b', 'c']); + assert!(lru_victims(Vec::<(u64, char, u64)>::new(), 90, 40, 100).is_empty()); +} + +#[test] +fn uniform_buffer_sizes_are_checked_before_allocation() { + assert_eq!(uniform_buffer_len(1, 255, 65_536).unwrap(), 65_536); + assert!(uniform_buffer_len(1, 255, 65_535).is_err()); + assert!(uniform_buffer_len(usize::MAX, 1, u64::MAX).is_err()); + assert!(uniform_buffer_len(usize::MAX, 0, u64::MAX).is_err()); + assert!(uniform_buffer_len(1, 16_777_216, u64::MAX).is_err(), "the last record offset cannot be represented by u32"); + #[cfg(target_pointer_width = "64")] + assert_eq!(uniform_buffer_len(1, 16_777_215, u64::MAX).unwrap(), 4_294_967_296, "the last valid u32 dynamic offset is accepted"); +} + +#[test] +fn half_roundtrip() { + for v in [0.0f32, 1.0, 0.5, 0.25, 1.0 / 255.0, 0.333, 65504.0, -2.5, 1e-6] { + let r = f16_to_f32(f32_to_f16(v)); + assert!((r - v).abs() <= v.abs() / 1024.0 + 1e-7, "{v} -> {r}"); + } +} + +#[test] +fn shader_validates() { + let module = wgpu::naga::front::wgsl::parse_str(SHADER).unwrap_or_else(|e| panic!("{}", e.emit_to_string(SHADER))); + wgpu::naga::valid::Validator::new(wgpu::naga::valid::ValidationFlags::all(), wgpu::naga::valid::Capabilities::empty()) + .validate(&module) + .unwrap_or_else(|e| panic!("{e:?}")); + for k in Kernel::DRAWN { + let e = k.entry().unwrap(); + assert!(module.entry_points.iter().any(|ep| ep.name == e), "missing entry point {e}"); + } +} + +/// The decimal float literals in `src` (comments skipped; hex literals are exact, so skipped), +/// with their line numbers. +fn decimal_float_literals(src: &str) -> Vec<(usize, String)> { + let mut out = Vec::new(); + for (n, line) in src.lines().enumerate() { + let code = line.split("//").next().unwrap_or("").as_bytes(); + let mut i = 0; + while i < code.len() { + let digit_or_dot = code[i].is_ascii_digit() || (code[i] == b'.' && code.get(i + 1).is_some_and(u8::is_ascii_digit)); + let word_before = i > 0 && (code[i - 1].is_ascii_alphanumeric() || code[i - 1] == b'_' || code[i - 1] == b'.'); + if !digit_or_dot || word_before { + i += 1; + continue; + } + if code[i] == b'0' && matches!(code.get(i + 1), Some(b'x' | b'X')) { + i += 2; + while i < code.len() && (code[i].is_ascii_hexdigit() || matches!(code[i], b'.' | b'p' | b'P' | b'+' | b'-')) { + i += 1; + } + continue; + } + let start = i; + while i < code.len() && (code[i].is_ascii_digit() || code[i] == b'.') { + i += 1; + } + if i < code.len() && matches!(code[i], b'e' | b'E') { + let mut j = i + 1; + if j < code.len() && matches!(code[j], b'+' | b'-') { + j += 1; + } + if j < code.len() && code[j].is_ascii_digit() { + i = j; + while i < code.len() && code[i].is_ascii_digit() { + i += 1; + } + } + } + out.push((n + 1, String::from_utf8_lossy(&code[start..i]).into_owned())); + } + } + out +} + +/// Browsers' WebGPU shader compiler (Tint) rejects a float literal beyond f32's finite range +/// even when it would round to `f32::MAX` (naga, used natively, rounds it), and then no +/// compositor pipeline builds and the web app falls back to the CPU renderer. `3.40282347e38` +/// was such a literal. +#[test] +fn shader_float_literals_fit_f32() { + let lits = decimal_float_literals(SHADER); + assert!(lits.iter().any(|(_, l)| l == "1.0"), "the scanner finds literals: {lits:?}"); + let bad: Vec<_> = lits.iter().filter(|(_, l)| l.parse::().map_or(true, |v| !v.is_finite() || v.abs() > f64::from(f32::MAX))).collect(); + assert!(bad.is_empty(), "float literals outside f32's finite range (line, literal): {bad:?}"); +} + +#[test] +fn op_record_fits_the_uniform() { + let p = plan::Pass::new(Kernel::FxPaint, 0); + assert_eq!(op_words(&p, None, None, None).len() as u64, OP_UNIFORM); +} + +/// #6534: a 16-bit mask (16-bit documents, feathered or vector combined masks) costs two +/// bytes per pixel on the GPU, twice an 8-bit mask, not four; 32-bit masks stay R32Float. +#[test] +fn sixteen_bit_masks_upload_at_two_bytes_per_pixel() { + let gray = |sample| PixelFormat { sample, ..PixelFormat::GRAY8 }; + assert_eq!(TexKind::for_surface(Role::Mask, PixelFormat::GRAY8).bytes_per_pixel(), 1); + let k16 = TexKind::for_surface(Role::Mask, gray(SampleType::U16)); + assert_eq!(k16.bytes_per_pixel(), 2, "{k16:?}"); + assert_eq!(k16.format().block_copy_size(None), Some(2), "{k16:?}"); + let k32 = TexKind::for_surface(Role::Mask, gray(SampleType::F32)); + assert_eq!(k32.format(), wgpu::TextureFormat::R32Float); + // The texels hold the mask values (a half each), the default pixel included. + let mut s = Surface::with_default(gray(SampleType::U16), &[1.0]); + s.fill_rect(Rect::new(0, 0, 4, 1), &[0.25]); + let c = TileCoord::new(0, 0); + let bytes = convert_tile(&s, s.tile(c), k16, c); + assert_eq!(bytes.len(), (TILE_SIZE * TILE_SIZE) as usize * 2); + assert_eq!(bytes.get(..2), Some(f32_to_f16(0.25).to_le_bytes().as_slice())); + assert_eq!(bytes.get(8..10), Some(f32_to_f16(1.0).to_le_bytes().as_slice())); + assert_eq!(convert_tile(&s, None, k16, c).get(..2), Some(f32_to_f16(1.0).to_le_bytes().as_slice())); +} + +/// #1774: a solid fill shares one tile across every coordinate; it is converted once, and +/// every coordinate (shared or not) still gets exactly the bytes `convert_tile` gives it, at +/// every depth and texture kind. +#[test] +fn convert_tiles_converts_each_distinct_tile_once() { + let area = Rect::new(0, 0, TILE_SIZE * 9, TILE_SIZE * 9); + for (fmt, kind) in [ + (PixelFormat::RGBA8, TexKind::Rgba8Direct), + (PixelFormat { alpha: false, ..PixelFormat::RGBA8 }, TexKind::Rgba8), + (PixelFormat::RGBA16, TexKind::Rgba16F), + (PixelFormat::RGBA32F, TexKind::Rgba16F), + (PixelFormat::GRAY8, TexKind::R8Direct), + (PixelFormat { sample: SampleType::U16, ..PixelFormat::GRAY8 }, TexKind::R16F), + (PixelFormat { sample: SampleType::F32, ..PixelFormat::GRAY8 }, TexKind::R32F), + ] { + let mut s = Surface::new(fmt); + let px = [0.2, 0.4, 0.6, 1.0]; + let n = fmt.channels(); + // Fully covered tiles share one `Arc`; then two tiles get their own pixels. + s.fill_rect(area, &px[..n]); + s.fill_rect(Rect::new(3, 5, 40, 60), &[0.9, 0.1, 0.3, 0.5][..n]); + s.fill_rect(Rect::new(TILE_SIZE * 4 + 7, TILE_SIZE * 2, TILE_SIZE * 4 + 9, TILE_SIZE * 2 + 3), &[0.0; 4][..n]); + let tiles: Vec<_> = area.tiles().filter_map(|c| s.tile(c).map(|t| (c, t.clone()))).collect(); + assert_eq!(tiles.len(), 81, "{fmt:?}"); + let mut seen = Vec::new(); + let converted = convert_tiles(&s, tiles, kind, |t, cs, bytes| { + for &c in cs { + assert!(s.tile(c).is_some_and(|own| Arc::ptr_eq(own, t)), "{fmt:?} {c:?}"); + assert_eq!(bytes, convert_tile(&s, Some(t), kind, c).as_slice(), "{fmt:?} {c:?}"); + seen.push(c); + } + }); + assert_eq!(converted, 3, "{fmt:?}: the shared tile once, plus the two edited ones"); + seen.sort_by_key(|c| (c.ty, c.tx)); + let mut want: Vec<_> = area.tiles().collect(); + want.sort_by_key(|c| (c.ty, c.tx)); + assert_eq!(seen, want, "{fmt:?}: every coordinate once"); + } + // More distinct tiles than one batch: all converted, each coordinate once. + let mut s = Surface::new(PixelFormat::RGBA16); + let wide = Rect::new(0, 0, TILE_SIZE * (CONVERT_BATCH as i32 + 5), TILE_SIZE); + for c in wide.tiles() { + s.fill_rect(Rect::new(c.rect().x0, 0, c.rect().x0 + 1, 1), &[c.tx as f32 / 100.0, 0.0, 0.0, 1.0]); + } + let tiles: Vec<_> = wide.tiles().filter_map(|c| s.tile(c).map(|t| (c, t.clone()))).collect(); + let mut n = 0; + let converted = convert_tiles(&s, tiles, TexKind::Rgba16F, |t, cs, bytes| { + for &c in cs { + assert_eq!(bytes, convert_tile(&s, Some(t), TexKind::Rgba16F, c).as_slice()); + n += 1; + } + }); + assert_eq!((converted, n), (CONVERT_BATCH + 5, CONVERT_BATCH + 5)); +} + +/// The 16-bit and 32-bit float RGB/gray fast paths give exactly the general path's half +/// floats (32-bit samples above 1 and below 0 included). +#[test] +fn sixteen_and_thirty_two_bit_fast_paths_match_the_general_one() { + for fmt in [ + PixelFormat::RGBA16, + PixelFormat { alpha: false, ..PixelFormat::RGBA16 }, + PixelFormat { sample: SampleType::U16, ..PixelFormat::GRAY8 }, + PixelFormat { sample: SampleType::U16, alpha: true, ..PixelFormat::GRAY8 }, + PixelFormat::RGBA32F, + PixelFormat { alpha: false, ..PixelFormat::RGBA32F }, + PixelFormat { sample: SampleType::F32, ..PixelFormat::GRAY8 }, + PixelFormat { sample: SampleType::F32, alpha: true, ..PixelFormat::GRAY8 }, + ] { + let mut s = Surface::new(fmt); + let r = Rect::new(0, 0, TILE_SIZE, TILE_SIZE); + let n = (TILE_SIZE * TILE_SIZE) as usize * fmt.channels(); + let hdr = if fmt.sample == SampleType::F32 { 4.0 } else { 1.0 }; + let data: Vec = (0..n).map(|i| (i as u32).wrapping_mul(2_654_435_761) as f32 / u32::MAX as f32 * hdr - (hdr - 1.0) / 4.0).collect(); + s.write_region(r, &data); + let t = s.tile(TileCoord::new(0, 0)).expect("written").clone(); + let fast = rgb_gray16_to_f16(&fmt, t.bytes()).or_else(|| rgb_gray32_to_f16(&fmt, t.bytes())).expect("fast path"); + let raw = s.read_region(r); + let general: Vec = + raw.chunks_exact(fmt.channels()).flat_map(|px| photocraft_raster::to_rgba(&fmt, px).map(|x| f32_to_f16(x).to_le_bytes())).flatten().collect(); + assert_eq!(fast, general, "{fmt:?}"); + assert_eq!(convert_tile(&s, Some(&t), TexKind::Rgba16F, TileCoord::new(0, 0)), general, "{fmt:?}"); + } + assert!(rgb_gray16_to_f16(&PixelFormat::RGBA8, &[]).is_none()); + assert!(rgb_gray16_to_f16(&PixelFormat { sample: SampleType::U16, ..PixelFormat::CMYKA8 }, &[]).is_none()); + assert!(rgb_gray32_to_f16(&PixelFormat::RGBA16, &[]).is_none()); + assert!(rgb_gray32_to_f16(&PixelFormat { sample: SampleType::F32, ..PixelFormat::CMYKA8 }, &[]).is_none()); +} + +/// CMYK and Lab tiles convert in parallel bands of rows, each run of equal pixels once +/// (#5108): the texels are still exactly the per-pixel conversion's, at 8 and 16 bits, with +/// the built-in CMYK profile and with a document's own (which the worker threads enter). +#[test] +fn colour_managed_tiles_match_the_per_pixel_conversion() { + use photocraft_cms::synth::{CmykParams, cmyk_profile}; + use photocraft_color::ColorMode; + use photocraft_color::convert::{CmykSpace, with_cmyk_space}; + let icc = Arc::new( + cmyk_profile(&CmykParams { description: "GPU test CMYK".into(), tvi: [0.26, 0.26, 0.26, 0.3], grid_a2b: 5, grid_b2a: 9, ..Default::default() }) + .to_bytes(), + ); + let own = CmykSpace::for_profile(Some(&icc)); + assert!(own.is_some(), "the synthetic profile is not the built-in one"); + let area = Rect::new(0, 0, TILE_SIZE * 3, TILE_SIZE); + for (mode, sample, space) in [ + (ColorMode::Cmyk, SampleType::U8, None), + (ColorMode::Cmyk, SampleType::U16, None), + (ColorMode::Cmyk, SampleType::U8, own.clone()), + (ColorMode::Cmyk, SampleType::U16, own.clone()), + (ColorMode::Lab, SampleType::U8, None), + (ColorMode::Lab, SampleType::U16, None), + ] { + let fmt = PixelFormat { mode, sample, alpha: true }; + let kind = TexKind::for_surface(Role::Content, fmt); + assert!(colour_managed(&fmt, kind), "{fmt:?}"); + let ch = fmt.channels(); + let mut s = Surface::new(fmt); + // Tile 0: noise. Tile 1: transparent with a soft dab (runs of equal pixels). Tile 2: + // a flat colour with a few lines of noise. + let n = (TILE_SIZE * TILE_SIZE) as usize * ch; + let noise: Vec = (0..n).map(|i| (i as u32).wrapping_mul(2_654_435_761) as f32 / u32::MAX as f32).collect(); + s.write_region(Rect::new(0, 0, TILE_SIZE, TILE_SIZE), &noise); + let mut dab = [0.8, 0.6, 0.1, 0.05, 0.0]; + for r in (1..30).rev() { + dab[ch - 1] = 1.0 - r as f32 / 30.0; + s.fill_rect(Rect::new(TILE_SIZE + 100 - r, 90 - r, TILE_SIZE + 100 + r, 90 + r), &dab[..ch]); + } + s.fill_rect(Rect::new(TILE_SIZE * 2, 0, TILE_SIZE * 3, TILE_SIZE), &[0.3, 0.2, 0.7, 0.1, 1.0][..ch]); + s.write_region(Rect::new(TILE_SIZE * 2, 40, TILE_SIZE * 3, 43), &noise[..TILE_SIZE as usize * 3 * ch]); + let tiles: Vec<_> = area.tiles().filter_map(|c| s.tile(c).map(|t| (c, t.clone()))).collect(); + assert_eq!(tiles.len(), 3, "{fmt:?}"); + let mut seen = 0; + with_cmyk_space(space.as_ref(), || { + convert_tiles(&s, tiles, kind, |_, cs, bytes| { + for &c in cs { + // The old path: every pixel through `to_rgba` on this thread, in this scope. + let want: Vec = s + .read_region(c.rect()) + .chunks_exact(ch) + .flat_map(|px| { + let v = photocraft_raster::to_rgba(&fmt, px); + match kind { + TexKind::Rgba16F => v.iter().flat_map(|x| f32_to_f16(*x).to_le_bytes()).collect::>(), + _ => v.map(|x| (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8).to_vec(), + } + }) + .collect(); + assert!(bytes == want.as_slice(), "{fmt:?} {c:?} with {}", if space.is_some() { "the document's profile" } else { "the built-in profile" }); + assert_eq!(convert_tile(&s, s.tile(c), kind, c), want, "{fmt:?} {c:?}"); + seen += 1; + } + }); + }); + assert_eq!(seen, 3, "{fmt:?}"); + } +} + +#[test] +fn grid_rounds_out() { + assert_eq!(tile_grid(Rect::new(0, 0, 300, 256)), Rect::new(0, 0, 512, 256)); +} diff --git a/crates/gpu/src/textures.rs b/crates/gpu/src/textures.rs new file mode 100644 index 0000000000..5b52dfefaa --- /dev/null +++ b/crates/gpu/src/textures.rs @@ -0,0 +1,247 @@ +use super::{Arc, DocId, FieldKind, HashMap, LayerId, PixelFormat, Rect, Role, SampleType, Tile, TileCoord, fx, timing}; + +/// A finished chunk: straight-alpha RGBA32F pixels of `rect` (document coordinates) at the +/// texture's origin. +pub struct ChunkOut<'a> { + pub rect: Rect, + pub texture: &'a wgpu::Texture, + pub view: &'a wgpu::TextureView, +} + +/// What a render did (for profiling and tests). +#[derive(Clone, Copy, Debug, Default)] +pub struct Stats { + /// How long the GPU spent executing a composite, once its timestamps came back. Because the + /// readback is asynchronous this belongs to an *earlier* render than the one returning it + /// (it carries that render's own pixel count, so the two are never confused), and it is + /// `None` unless [`Compositor::measure_gpu_time`] is on and a measurement has landed. + /// Always `None` from [`Compositor::encode`], which submits nothing of its own. + pub gpu_time: Option, + pub passes: usize, + pub chunks: usize, + pub slots: u32, + pub tiles_uploaded: usize, + pub bytes_uploaded: usize, + /// Tiles a new layer texture took from another layer's texture by a GPU copy (a duplicate + /// shares its source's tiles) instead of uploading them. + pub tiles_shared: usize, + /// Effect layers whose shape was (re)rendered, fully or partially. + pub fx_shapes: usize, + /// Effect map programs run (one per effect whose maps were rebuilt). + pub fx_programs: usize, + /// Effect-map pixels written (all stages). + pub fx_pixels: u64, + /// Page cells the region spans (each binds only the layer pages it overlaps). + pub cells: usize, + /// Layer pages and paged effect maps evicted to stay within the memory budgets. + pub evicted: usize, + /// Intermediate submits of a huge refresh (to bound staging and evicted memory). + pub flushes: usize, + /// Chunks restored from an unchanged lower-stack composite. + pub prefix_hits: usize, + /// Planned prefix passes skipped, summed over the reused chunks. + pub prefix_passes_skipped: usize, + /// Plain layers blended inside another layer's pass (`fs_layerbatch`), summed over chunks. + pub layers_batched: usize, + /// Batched passes drawn with Normal's code alone (`fs_layerbatch_normal`), summed over chunks. + pub normal_batches: usize, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum TexKind { + /// RGBA8 tiles copied verbatim. + Rgba8Direct, + /// Other 8-bit formats converted to RGBA8. + Rgba8, + /// 16/32-bit formats converted to RGBA16F. + Rgba16F, + /// GRAY8 masks copied verbatim. + R8Direct, + /// 16-bit masks as R16F (the precision 16-bit pixels get as RGBA16F). + R16F, + /// 32-bit masks as R32F. + R32F, +} + +impl TexKind { + pub(crate) fn for_surface(role: Role, f: PixelFormat) -> Self { + match role { + Role::Content | Role::Stroke if f == PixelFormat::RGBA8 => TexKind::Rgba8Direct, + Role::Content | Role::Stroke if f.sample == SampleType::U8 => TexKind::Rgba8, + Role::Content | Role::Stroke => TexKind::Rgba16F, + Role::Mask if f == PixelFormat::GRAY8 => TexKind::R8Direct, + Role::Mask if f.sample == SampleType::F32 => TexKind::R32F, + Role::Mask => TexKind::R16F, + } + } + pub(crate) fn format(self) -> wgpu::TextureFormat { + match self { + TexKind::Rgba8Direct | TexKind::Rgba8 => wgpu::TextureFormat::Rgba8Unorm, + TexKind::Rgba16F => wgpu::TextureFormat::Rgba16Float, + TexKind::R8Direct => wgpu::TextureFormat::R8Unorm, + TexKind::R16F => wgpu::TextureFormat::R16Float, + TexKind::R32F => wgpu::TextureFormat::R32Float, + } + } + pub(crate) fn bytes_per_pixel(self) -> usize { + match self { + TexKind::Rgba8Direct | TexKind::Rgba8 => 4, + TexKind::Rgba16F => 8, + TexKind::R8Direct => 1, + TexKind::R16F => 2, + TexKind::R32F => 4, + } + } +} + +/// Index into the render's resident key list, and the texture's region (x, y, w, h). +pub(crate) type ResidentRef = (usize, [i32; 4]); + +/// A page cell: the square `[x·page, (x+1)·page) × [y·page, (y+1)·page)` of document pixels. +pub(crate) type Cell = (i32, i32); + +/// A resident layer page. +pub(crate) type ResKey = (LayerId, Role, Cell); + +/// Effect maps of a layer: over its whole effect region (`None`), or one page cell of a region +/// larger than the texture limit. +pub(crate) type FxKey = (LayerId, Option); + +pub(crate) struct CachedPattern { + pub(crate) fingerprint: u64, + pub(crate) format: PixelFormat, + pub(crate) default_bits: Vec, + pub(crate) texture: Tex, + /// The document that last painted with it: closing that document frees the texture (#4971). + pub(crate) doc: DocId, + /// [`Compositor::frame`] when it last painted. + pub(crate) last_used: u64, + /// GPU bytes of the texture (RGBA32F). + pub(crate) bytes: u64, +} + +pub(crate) struct Resident { + pub(crate) texture: wgpu::Texture, + pub(crate) view: wgpu::TextureView, + /// Document-pixel rect covered (tile aligned, inside one page cell). + pub(crate) region: Rect, + pub(crate) kind: TexKind, + pub(crate) format: PixelFormat, + pub(crate) tiles: HashMap>, + pub(crate) default_nonzero: bool, + /// Bit pattern of the surface's default pixel when the texture was filled. Tiles the surface + /// doesn't store hold it, so a changed default (Invert, Fill) makes the texture stale. + pub(crate) default_bits: Vec, + pub(crate) doc: DocId, + pub(crate) last_used: u64, + /// Cell visit that last used it (least recently used pages are evicted first). + pub(crate) stamp: u64, + /// CMYK profile the texels were converted with (`CmykSpace::id`, 0 = built-in / not CMYK). + pub(crate) cmyk: u64, + /// Its texels were copied to or from another layer texture by the encoder, so every write + /// to it goes through the encoder too: a queued write lands before the encoder runs, and + /// would be overwritten by the copy, or leak into it. + pub(crate) ordered: bool, +} + +impl Resident { + pub(crate) fn bytes(&self) -> u64 { + self.region.width() as u64 * self.region.height() as u64 * self.kind.bytes_per_pixel() as u64 + } +} + +/// A texture and its default view. +#[derive(Clone)] +pub(crate) struct Tex { + pub(crate) texture: wgpu::Texture, + pub(crate) view: wgpu::TextureView, +} + +impl Tex { + pub(crate) fn new(device: &wgpu::Device, label: &str, w: u32, h: u32, format: wgpu::TextureFormat, usage: wgpu::TextureUsages) -> Self { + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some(label), + size: wgpu::Extent3d { width: w.max(1), height: h.max(1), depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage, + view_formats: &[], + }); + let view = texture.create_view(&Default::default()); + Tex { texture, view } + } + pub(crate) fn map(device: &wgpu::Device, label: &str, r: Rect, format: wgpu::TextureFormat) -> Self { + Tex::new( + device, + label, + r.width(), + r.height(), + format, + wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + ) + } + /// Upload R32F values (row-major over `r`) into this region-space texture at `r`. + pub(crate) fn write_r32(&self, queue: &wgpu::Queue, region: Rect, r: Rect, v: &[f32]) { + if r.is_empty() { + return; + } + let bytes: Vec = v.iter().flat_map(|x| x.to_le_bytes()).collect(); + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &self.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: (r.x0 - region.x0) as u32, y: (r.y0 - region.y0) as u32, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + &bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(r.width() * 4), rows_per_image: Some(r.height()) }, + wgpu::Extent3d { width: r.width(), height: r.height(), depth_or_array_layers: 1 }, + ); + } + fn bytes(&self) -> usize { + let s = self.texture.size(); + let bpp = match self.texture.format() { + wgpu::TextureFormat::R16Float => 2, + wgpu::TextureFormat::Rgba32Float => 16, + wgpu::TextureFormat::Rgba16Float => 8, + _ => 4, + }; + s.width as usize * s.height as usize * bpp + } +} + +/// Cached maps of one effect (one enabled item of a layer). +pub(crate) struct ProgState { + pub(crate) key: u64, + pub(crate) maps: Vec>, +} + +/// Cached effect state of one layer. +pub(crate) struct FxEntry { + pub(crate) doc: DocId, + pub(crate) region: Rect, + pub(crate) shape_key: u64, + /// Tiles the shape was rendered from (content, mask): damage is their difference. + pub(crate) tiles: [Option; 2], + /// Groups: the tiles `shape_key` hashes, by address only (see [`fx::Tiles`]). + pub(crate) _pin: Vec>, + /// The shape on the CPU (distance fields are computed from it) and on the GPU. + pub(crate) shape_cpu: Vec, + pub(crate) shape: Tex, + /// Distance fields: the reach each is exact to, and its texture. + pub(crate) fields: HashMap, + pub(crate) progs: Vec, + pub(crate) last_used: u64, + pub(crate) stamp: u64, +} + +impl FxEntry { + pub(crate) fn bytes(&self) -> usize { + self.shape.bytes() + + self.shape_cpu.len() * 4 + + self.fields.values().map(|(_, t)| t.bytes()).sum::() + + self.progs.iter().flat_map(|p| p.maps.iter().flatten()).map(Tex::bytes).sum::() + } +} diff --git a/crates/gpu/src/timing.rs b/crates/gpu/src/timing.rs new file mode 100644 index 0000000000..0f072bfc3e --- /dev/null +++ b/crates/gpu/src/timing.rs @@ -0,0 +1,315 @@ +//! GPU execution time of a composite (#7186), measured with wgpu timestamp queries. +//! +//! `GpuCanvas::refresh` reports `composite_ms`, the CPU cost of *encoding and submitting* the +//! composite. That is not the cost of running it: at 24 MP the encode costs ~7.7 ms and the GPU +//! then spends ~171 ms finishing the work, so every consumer of `composite_ms` under-reports a +//! GPU-bound canvas by more than an order of magnitude and cannot see a regression that moves +//! work onto the GPU. +//! +//! Waiting for the GPU to report the true number would serialise the two processors and make the +//! app slower, so this measures instead of waiting: a timestamp is written on the GPU timeline at +//! the start and end of every command encoder a refresh submits, and the values are read back +//! asynchronously. The sum of the spans is the time the GPU spent executing that refresh, with +//! the CPU encoding gaps between submissions excluded. Nothing blocks; the numbers simply arrive +//! a frame or two later, so each measurement carries the pixel count of the refresh it belongs to +//! and is never attributed to whichever refresh happens to be in flight when it lands. +//! +//! **What it does not count.** The spans run between bottom-of-pipe timestamps inside the +//! refresh's own command buffers. Two kinds of GPU work fall outside them: queue-level uploads +//! (`Queue::write_texture`), which wgpu executes in its own command buffer before ours, and +//! whatever the GPU still does after the final timestamp retires — the end-of-batch writeback of +//! the render targets. Measured against a waited-for wall clock on Intel UHD P630 / Mesa, with +//! no uploads outstanding, the uncounted remainder is **~5 % of the reported time**, and it is +//! proportional to the composite rather than a fixed submit latency (1.0 ms at 6 MP, 4.5 ms at +//! 24 MP, 7.6 ms at 42.6 MP, against 21 / 83 / 158 ms reported). So the number is a slight +//! under-report of the GPU's total contribution, by a few per cent rather than the ~22x of the +//! encode-and-submit time it replaces. +//! +//! **What it costs.** Nothing measurable. The same refresh on the same device with +//! `PHOTOCRAFT_NO_GPU_TIMING` set waits 88.1 ms against 87.9 ms measured — a difference of +//! −0.2 ms on an ~88 ms refresh, i.e. inside the noise of a shared host. Per refresh the +//! queries add two timestamp writes (two more per flush), one resolve and one copy of at most +//! 1 KiB, a `map_async` and an `Arc` allocation; the query set and both buffers are allocated +//! once per compositor and are size-independent, so a resize reallocates nothing. +//! +//! The measurement is *optional by construction*. Without `TIMESTAMP_QUERY` and +//! `TIMESTAMP_QUERY_INSIDE_ENCODERS` on the device (older drivers, most browsers, the Noop +//! backend), [`GpuTimer::new`] returns `None` and callers report no GPU time at all rather than a +//! plausible wrong number — reporting a plausible wrong number is the failure this fixes. + +use std::sync::Arc; +use std::sync::atomic::{AtomicU8, Ordering}; + +/// The GPU execution time of one composite, with the refresh it measured. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct GpuTiming { + /// Time the GPU spent executing the refresh's command buffers (ms). Excludes queue-level + /// uploads, which run in wgpu's own command buffer ahead of ours, and the end-of-batch work + /// after the last timestamp: see the module doc for the size of that (~5 % here, more on a + /// refresh that uploads). + pub ms: f64, + /// Pixels that refresh composited (so a late measurement stays interpretable). + pub px: u64, + /// Command buffers the refresh submitted (1 unless it flushed). + pub spans: u32, +} + +/// Device features [`GpuTimer`] needs. +pub const FEATURES: wgpu::Features = wgpu::Features::TIMESTAMP_QUERY.union(wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS); + +/// Spans one measurement may hold. A refresh submits once per flush; a 200 MP document flushes a +/// handful of times, so this is far above anything real. A measurement that would exceed it is +/// dropped rather than truncated (a truncated sum is exactly the under-report being fixed). +const SPANS: u32 = 64; +/// Any span total above this is a wrapped or garbage timestamp, not a composite. +const SANE_MS: f64 = 600_000.0; + +/// Readback state of the buffer holding the resolved timestamps. +const UNMAPPED: u8 = 0; +const MAPPED: u8 = 1; +const FAILED: u8 = 2; + +/// Timestamp queries for one compositor, reused for every refresh. +/// +/// At most one measurement is in flight: while the last one is still being read back a refresh +/// goes unmeasured, which costs a sample rather than correctness. +pub struct GpuTimer { + set: wgpu::QuerySet, + /// Resolved timestamps (`QUERY_RESOLVE | COPY_SRC`). + resolve: wgpu::Buffer, + /// Host-visible copy of `resolve` (`MAP_READ | COPY_DST`). + read: wgpu::Buffer, + /// Nanoseconds per timestamp tick, as the queue reports it. + period_ns: f32, + /// Timestamps written into the current recording. + written: u32, + /// Whether a span is open (a start timestamp waiting for its end). + open: bool, + /// Pixels of the recording in progress. + px: u64, + /// The recording overflowed [`SPANS`]: finish without resolving. + overflowed: bool, + /// The recording in progress has already had command buffers submitted (it flushed), so its + /// query slots may still be in use by the GPU. + flushed: bool, + /// A recording was abandoned after it had already submitted timestamps. Writing a query + /// again resets that slot (wgpu-core resets per write), and resetting one an in-flight + /// submission is still writing is undefined, with no way to learn when it has drained. + /// That only happens when a refresh fails after a flush that nothing waited for (a refresh + /// whose page retry waited for the GPU calls [`Self::drained`] first), which is rare: usually + /// the device faulted and the canvas is about to drop to the CPU compositor anyway. So + /// measuring simply stops: no number, which is the contract everywhere else here. + stopped: bool, + /// Timestamps and pixels of the measurement being read back, and its readback flag. + in_flight: Option<(u32, u64, Arc)>, + /// [`Self::finish`] recorded a readback that [`Self::submitted`] has yet to map. Without + /// this, a refresh that starts before the last measurement lands would map the readback + /// buffer a second time while the first map is still pending, which wgpu rejects. + unmapped: bool, + /// The most recent completed measurement, until a caller takes it. + ready: Option, +} + +impl std::fmt::Debug for GpuTimer { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("GpuTimer").field("recording", &self.open).field("in_flight", &self.in_flight.is_some()).field("ready", &self.ready).finish() + } +} + +impl GpuTimer { + /// A timer for `device`, or `None` when it has no timestamp queries (the caller then reports + /// no GPU time, never an estimate). + pub fn new(device: &wgpu::Device, queue: &wgpu::Queue) -> Option { + if !device.features().contains(FEATURES) { + return None; + } + let period_ns = queue.get_timestamp_period(); + if !period_ns.is_finite() || period_ns <= 0.0 { + return None; + } + let bytes = u64::from(SPANS) * 2 * 8; + Some(Self { + set: device.create_query_set(&wgpu::QuerySetDescriptor { label: Some("pc_compose_time"), ty: wgpu::QueryType::Timestamp, count: SPANS * 2 }), + resolve: device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_compose_time_resolve"), + size: bytes, + usage: wgpu::BufferUsages::QUERY_RESOLVE | wgpu::BufferUsages::COPY_SRC, + mapped_at_creation: false, + }), + read: device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_compose_time_read"), + size: bytes, + usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }), + period_ns, + written: 0, + open: false, + px: 0, + overflowed: false, + flushed: false, + stopped: false, + in_flight: None, + unmapped: false, + ready: None, + }) + } + + /// Start measuring a refresh of `px` pixels, opening its first span in `encoder`. Does + /// nothing while a previous measurement is still being read back (that refresh goes + /// unmeasured) — call [`Self::collect`] first to give it the chance to land. + pub fn start(&mut self, encoder: &mut wgpu::CommandEncoder, px: u64) { + if self.stopped || self.in_flight.is_some() || self.open { + return; + } + self.written = 0; + self.overflowed = false; + self.flushed = false; + self.px = px; + self.open = true; + self.stamp(encoder); + } + + /// Close the span in `done` and open the next in `next`: one refresh that submits several + /// command buffers is one measurement, summed over them. + pub fn split(&mut self, done: &mut wgpu::CommandEncoder, next: &mut wgpu::CommandEncoder) { + if !self.open { + return; + } + self.stamp(done); + self.stamp(next); + self.flushed = true; + } + + /// Close the last span and record the resolve and readback copy into `encoder`, which the + /// caller is about to submit. Call [`Self::submitted`] once it has. + pub fn finish(&mut self, encoder: &mut wgpu::CommandEncoder) { + if !self.open { + return; + } + self.stamp(encoder); + self.open = false; + let count = self.written; + if self.overflowed || count < 2 || !count.is_multiple_of(2) { + self.abandon(); + return; + } + let bytes = u64::from(count) * 8; + encoder.resolve_query_set(&self.set, 0..count, &self.resolve, 0); + encoder.copy_buffer_to_buffer(&self.resolve, 0, &self.read, 0, bytes); + self.in_flight = Some((count, self.px, Arc::new(AtomicU8::new(UNMAPPED)))); + self.unmapped = true; + self.written = 0; + // This recording is over: its slots are protected by `in_flight` until the readback + // lands, not by `flushed`. Leaving `flushed` set would let a later refresh that + // recorded nothing (because `start` found the readback still in flight) stop measuring + // for good when it failed, on the strength of this recording's flush. + self.flushed = false; + } + + /// Map the readback buffer after the caller submitted the encoder [`Self::finish`] wrote + /// into. The map completes on a later device poll; nothing blocks here. + pub fn submitted(&mut self) { + if !self.unmapped { + return; + } + self.unmapped = false; + let Some((count, _, flag)) = &self.in_flight else { return }; + let (bytes, flag) = (u64::from(*count) * 8, flag.clone()); + self.read.slice(0..bytes).map_async(wgpu::MapMode::Read, move |r| { + flag.store(if r.is_ok() { MAPPED } else { FAILED }, Ordering::Release); + }); + } + + /// Abandon the recording in progress (the refresh failed part way, so its spans mean + /// nothing). + pub fn cancel(&mut self) { + self.open = false; + self.abandon(); + } + + /// Drop the recording in progress. Once its timestamps have reached the GPU its slots can't + /// be reused safely, so measuring stops for good rather than resetting a live query. + fn abandon(&mut self) { + self.stopped |= self.flushed; + self.written = 0; + self.overflowed = false; + self.flushed = false; + } + + /// The caller waited until the GPU finished everything submitted so far: no timestamp of + /// this recording is still in flight, so [`Self::cancel`] can reuse its slots safely. + pub fn drained(&mut self) { + self.flushed = false; + } + + /// Whether a measurement is being read back (so this refresh won't be measured). + pub fn busy(&self) -> bool { + self.in_flight.is_some() + } + + /// Read back a finished measurement if the map completed, and return whatever is ready. + /// Cheap and non-blocking; callers poll it per refresh or per frame. + pub fn collect(&mut self) -> Option { + if let Some((count, px, flag)) = &self.in_flight { + match flag.load(Ordering::Acquire) { + MAPPED => { + let (count, px) = (*count, *px); + self.ready = self.value(count, px); + self.read.unmap(); + self.in_flight = None; + } + FAILED => { + self.in_flight = None; + } + _ => {} + } + } + self.ready + } + + /// Put a measurement back after a caller took it but could not use it (its refresh failed), + /// so the sample is reported by the next one instead of being lost on the fallback path. + /// A measurement that has landed since wins: it is the newer of the two. + pub fn restore(&mut self, t: GpuTiming) { + self.ready.get_or_insert(t); + } + + /// Take the most recent completed measurement, leaving none behind, so a stale number is + /// never reported twice as if it were new. + pub fn take(&mut self) -> Option { + self.collect(); + self.ready.take() + } + + /// Sum the resolved spans of a mapped readback. + fn value(&self, count: u32, px: u64) -> Option { + let bytes = u64::from(count) * 8; + let view = self.read.slice(0..bytes).get_mapped_range().ok()?; + let mut ticks = 0u64; + for pair in view.as_chunks::<16>().0 { + let (a, b) = pair.split_at(8); + let start = u64::from_le_bytes(a.try_into().ok()?); + let end = u64::from_le_bytes(b.try_into().ok()?); + // A driver that reset or wrapped its timer gives an end before its start: the whole + // measurement is then unusable, and no number is better than a wrong one. + if end < start { + return None; + } + ticks += end - start; + } + drop(view); + let ms = ticks as f64 * f64::from(self.period_ns) / 1.0e6; + (ms.is_finite() && ms < SANE_MS).then_some(GpuTiming { ms, px, spans: count / 2 }) + } + + /// Write one timestamp, noting an overflow instead of wrapping round the query set. + fn stamp(&mut self, encoder: &mut wgpu::CommandEncoder) { + if self.written >= SPANS * 2 { + self.overflowed = true; + return; + } + encoder.write_timestamp(&self.set, self.written); + self.written += 1; + } +} diff --git a/crates/gpu/tests/it/camera_raw.rs b/crates/gpu/tests/it/camera_raw.rs new file mode 100644 index 0000000000..ff841c825a --- /dev/null +++ b/crates/gpu/tests/it/camera_raw.rs @@ -0,0 +1,370 @@ +//! Camera Raw's GPU preview pass against the CPU develop (the oracle), stage by stage, at 8, 16 +//! and 32 bits. Skips, with a message, when there is no adapter or device or it can't render +//! `Rgba32Float`; with `WGPU_BACKEND` set those fail instead, as in `parity.rs`. A pipeline that +//! doesn't build on an adapter that can run it fails. The shader itself is validated without a +//! GPU ([`the_shader_validates_without_a_gpu`]), so a WGSL error fails on every runner. + +use photocraft_algo::camera_raw::{CameraRaw, Wheel, develop, per_pixel, white_black_points}; +use photocraft_gpu::{CameraRawDevelop, DeviceHealth, Fault}; + +/// Largest difference from the CPU develop, per channel. The pass evaluates the same +/// expressions in f32, so what remains is the GPU's own `pow`, `exp2` and `sin` (a few ulps) and +/// how they move a value across a table entry; measured at most about 1e-5 on NVIDIA (Vulkan, +/// DX12) and WARP. 1/4096 keeps a margin and stays well under one 8-bit step (the compositor's +/// tolerance is 1/255). +const TOL: f32 = 1.0 / 4096.0; + +struct Gpu { + device: wgpu::Device, + queue: wgpu::Queue, + dev: CameraRawDevelop, + _lock: std::sync::MutexGuard<'static, ()>, +} + +fn gpu() -> Option { + let lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let required = std::env::var_os("WGPU_BACKEND").is_some(); + // `WGPU_BACKEND` picks the backend; `PHOTOCRAFT_TEST_FALLBACK_ADAPTER=1` the software one (WARP). + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); + let fallback = std::env::var_os("PHOTOCRAFT_TEST_FALLBACK_ADAPTER").is_some(); + let options = wgpu::RequestAdapterOptions { force_fallback_adapter: fallback, ..Default::default() }; + let adapter = match pollster::block_on(instance.request_adapter(&options)) { + Ok(a) => a, + Err(e) => { + assert!(!required, "WGPU_BACKEND is set but has no adapter: {e}"); + eprintln!("skipping Camera Raw GPU tests: no adapter ({e})"); + return None; + } + }; + let info = adapter.get_info(); + if !CameraRawDevelop::supported(&adapter) { + assert!(!required, "WGPU_BACKEND is set but {} can't render Rgba32Float", info.name); + eprintln!("skipping Camera Raw GPU tests: adapter can't render Rgba32Float"); + return None; + } + let (device, queue) = match pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())) { + Ok(d) => d, + Err(e) => { + assert!(!required, "WGPU_BACKEND is set but {} has no device: {e}", info.name); + eprintln!("skipping Camera Raw GPU tests: no device ({e})"); + return None; + } + }; + eprintln!("Camera Raw GPU tests on {:?} ({})", info.backend, info.name); + // The adapter can run the pass: a pipeline that doesn't build is a bug in it, not a skip. + let dev = CameraRawDevelop::new(&device).unwrap_or_else(|e| panic!("the Camera Raw pipeline didn't build on {}: {}", info.name, e.0)); + Some(Gpu { device, queue, dev, _lock: lock }) +} + +/// The Camera Raw shader parses and validates (naga, as wgpu does before any driver sees it), +/// with no adapter: the GPU tests below skip on runners without one. +#[test] +fn the_shader_validates_without_a_gpu() { + let src = photocraft_gpu::CAMERA_RAW_SHADER; + let module = wgpu::naga::front::wgsl::parse_str(src).unwrap_or_else(|e| panic!("{}", e.emit_to_string(src))); + wgpu::naga::valid::Validator::new(wgpu::naga::valid::ValidationFlags::all(), wgpu::naga::valid::Capabilities::empty()) + .validate(&module) + .unwrap_or_else(|e| panic!("{}", e.emit_to_string(src))); + assert!(!module.entry_points.is_empty()); +} + +#[derive(Clone, Copy, Debug)] +enum Depth { + Eight, + Sixteen, + Float, +} + +const DEPTHS: [Depth; 3] = [Depth::Eight, Depth::Sixteen, Depth::Float]; + +fn hash(i: usize, c: u64) -> f32 { + let mut z = (i as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15) ^ c.wrapping_mul(0xC2B2_AE3D_27D4_EB4F); + z = (z ^ (z >> 29)).wrapping_mul(0xBF58_476D_1CE4_E5B9); + (z >> 40) as f32 / (1u64 << 24) as f32 +} + +/// Gradients, noise and saturated patches (every Color Mixer band), quantized to `depth`. The +/// 32-bit image also has values above 1 and a few below 0. +fn image(w: usize, h: usize, depth: Depth) -> Vec<[f32; 4]> { + (0..w * h) + .map(|i| { + let (x, y) = ((i % w) as f32 / w as f32, (i / w) as f32 / h as f32); + let mut c = if (i / w).is_multiple_of(4) { + // A hue sweep at full and half saturation. + let hue = x * 6.0; + let f = |n: f32| (((hue + n) % 6.0 - 3.0).abs() - 1.0).clamp(0.0, 1.0); + let s = if (i / w).is_multiple_of(8) { 1.0 } else { 0.5 }; + [f(0.0), f(4.0), f(2.0)].map(|v| (1.0 - s) * 0.6 + s * v) + } else { + [0.1 + 0.8 * x, 0.2 + 0.6 * y, 0.5 - 0.4 * x * y].map(|v| v + (hash(i, 1) - 0.5) * 0.15) + }; + if let Depth::Float = depth { + c = c.map(|v| v * (1.0 + 1.5 * y) - 0.02 * hash(i, 2)); + } + let q = |v: f32| match depth { + Depth::Eight => (v.clamp(0.0, 1.0) * 255.0).round() / 255.0, + Depth::Sixteen => (v.clamp(0.0, 1.0) * 32768.0).round() / 32768.0, + Depth::Float => v, + }; + [q(c[0]), q(c[1]), q(c[2]), 1.0] + }) + .collect() +} + +/// Develops `p` both ways and returns the largest channel difference. +fn compare(g: &mut Gpu, p: &CameraRaw, depth: Depth) -> f32 { + let (w, h) = (171, 113); + let float = matches!(depth, Depth::Float); + let src = image(w, h, depth); + let mut cpu = src.clone(); + develop(&mut cpu, w, h, p, float); + let points = white_black_points(&src, w, h, p); + let stages = per_pixel(p, w, h, float, points).unwrap_or_else(|| panic!("{p:?} has a spatial stage")); + let out = g.dev.develop(&g.device, &g.queue, None, &src, None, [w as u32, h as u32], &stages).unwrap(); + assert_eq!(out.len(), cpu.len()); + let mut worst = (0.0f32, 0usize); + for (i, (a, b)) in out.iter().zip(&cpu).enumerate() { + assert!(a.iter().all(|v| v.is_finite()), "{p:?} {depth:?}: non-finite GPU pixel {i}: {a:?}"); + assert_eq!(a[3], b[3], "alpha is kept"); + for c in 0..3 { + let d = (a[c] - b[c]).abs(); + if d > worst.0 { + worst = (d, i); + } + } + } + let (d, i) = worst; + assert!(d <= TOL, "{p:?} {depth:?}: GPU {:?} vs CPU {:?} at pixel {i} (source {:?}), off by {d}", out[i], cpu[i], src[i]); + if !float { + assert!(out.iter().all(|q| q[..3].iter().all(|v| (0.0..=1.0).contains(v))), "{p:?} {depth:?}: clipped like the CPU"); + } + d +} + +fn check(name: &str, sets: &[CameraRaw]) { + let Some(mut g) = gpu() else { return }; + for p in sets { + for depth in DEPTHS { + let d = compare(&mut g, p, depth); + eprintln!("{name} {depth:?}: max |GPU - CPU| = {d:.2e}"); + } + } +} + +#[test] +fn white_balance_and_exposure() { + check( + "white balance", + &[ + CameraRaw { temperature: 40.0, tint: -25.0, ..Default::default() }, + CameraRaw { temperature: -70.0, tint: 60.0, exposure: -1.2, ..Default::default() }, + CameraRaw { exposure: 1.5, ..Default::default() }, + ], + ); +} + +#[test] +fn contrast_without_texture_or_clarity() { + check("contrast", &[CameraRaw { contrast: 60.0, ..Default::default() }, CameraRaw { contrast: -80.0, exposure: 0.4, ..Default::default() }]); +} + +#[test] +fn whites_and_blacks() { + check( + "whites/blacks", + &[ + CameraRaw { whites: 70.0, blacks: -45.0, ..Default::default() }, + CameraRaw { whites: -60.0, blacks: 50.0, temperature: 20.0, ..Default::default() }, + CameraRaw { whites: 30.0, ..Default::default() }, + CameraRaw { blacks: -100.0, exposure: -0.5, ..Default::default() }, + // ProPhoto's power-law encoding. + CameraRaw { whites: 50.0, blacks: -30.0, encoding_gamma: 1.8, ..Default::default() }, + ], + ); +} + +#[test] +fn tone_curves() { + check( + "tone curves", + &[ + CameraRaw { + curve_highlights: -60.0, + curve_lights: 30.0, + curve_darks: 40.0, + curve_shadows: -20.0, + curve_splits: [20.0, 45.0, 80.0], + ..Default::default() + }, + CameraRaw { point_curve: vec![[0.0, 20.0], [64.0, 50.0], [192.0, 220.0], [255.0, 245.0]], ..Default::default() }, + CameraRaw { + point_curve_red: vec![[0.0, 0.0], [128.0, 160.0], [255.0, 255.0]], + point_curve_green: vec![[0.0, 10.0], [255.0, 230.0]], + point_curve_blue: vec![[0.0, 0.0], [100.0, 70.0], [255.0, 255.0]], + ..Default::default() + }, + ], + ); +} + +#[test] +fn vibrance_and_saturation() { + check( + "presence", + &[ + CameraRaw { vibrance: 70.0, ..Default::default() }, + CameraRaw { saturation: -60.0, ..Default::default() }, + CameraRaw { vibrance: -40.0, saturation: 50.0, ..Default::default() }, + ], + ); +} + +#[test] +fn color_mixer() { + let ramp = |k: f32| std::array::from_fn(|i| ((i as f32 * 37.0 + k) % 200.0) - 100.0); + check( + "color mixer", + &[ + CameraRaw { hsl_hue: ramp(0.0), ..Default::default() }, + CameraRaw { hsl_sat: ramp(50.0), ..Default::default() }, + CameraRaw { hsl_lum: ramp(120.0), ..Default::default() }, + CameraRaw { hsl_hue: ramp(10.0), hsl_sat: ramp(80.0), hsl_lum: ramp(150.0), ..Default::default() }, + ], + ); +} + +#[test] +fn color_grading() { + check( + "color grading", + &[ + CameraRaw { grade_shadows: Wheel { hue: 220.0, sat: 70.0, lum: 20.0 }, ..Default::default() }, + CameraRaw { + grade_midtones: Wheel { hue: 120.0, sat: 30.0, lum: -15.0 }, + grade_highlights: Wheel { hue: 40.0, sat: 50.0, lum: 10.0 }, + grade_global: Wheel { hue: 300.0, sat: 15.0, lum: 5.0 }, + grade_blending: 80.0, + grade_balance: -40.0, + ..Default::default() + }, + ], + ); +} + +#[test] +fn vignette() { + check( + "vignette", + &[ + CameraRaw { vignette_amount: -70.0, ..Default::default() }, + CameraRaw { vignette_amount: -50.0, vignette_roundness: -60.0, vignette_feather: 20.0, vignette_highlights: 70.0, ..Default::default() }, + CameraRaw { + vignette_amount: 60.0, + vignette_roundness: 80.0, + vignette_midpoint: 20.0, + vignette_style: "colorPriority".into(), + ..Default::default() + }, + CameraRaw { vignette_amount: -40.0, vignette_style: "paintOverlay".into(), ..Default::default() }, + CameraRaw { vignette_amount: 40.0, vignette_style: "paintOverlay".into(), ..Default::default() }, + ], + ); +} + +#[test] +fn grain_in_full_resolution_coordinates() { + check( + "grain", + &[ + CameraRaw { grain_amount: 60.0, seed: 3, ..Default::default() }, + // A preview proxy: the noise lattice is sampled at full-resolution positions. + CameraRaw { grain_amount: 80.0, grain_size: 60.0, grain_roughness: 90.0, seed: u32::MAX, pixel_scale: 0.143, ..Default::default() }, + CameraRaw { grain_amount: 25.0, grain_size: 0.0, grain_roughness: 0.0, seed: 77, pixel_scale: 0.5, ..Default::default() }, + ], + ); +} + +#[test] +fn every_per_pixel_stage_at_once() { + let mut hsl = [0.0; 8]; + hsl[1] = 35.0; + hsl[5] = -50.0; + check( + "all stages", + &[CameraRaw { + temperature: 25.0, + tint: 10.0, + exposure: 0.3, + contrast: 25.0, + whites: 20.0, + blacks: -15.0, + vibrance: 30.0, + saturation: 10.0, + curve_darks: 20.0, + point_curve: vec![[0.0, 8.0], [255.0, 250.0]], + hsl_hue: hsl, + hsl_sat: hsl, + hsl_lum: hsl, + grade_shadows: Wheel { hue: 210.0, sat: 25.0, lum: 0.0 }, + vignette_amount: -30.0, + grain_amount: 20.0, + pixel_scale: 0.2, + ..Default::default() + }], + ); +} + +#[test] +fn identity_leaves_pixels_alone_and_spatial_settings_have_no_pass() { + let Some(mut g) = gpu() else { return }; + let p = CameraRaw::default(); + let src = image(64, 32, Depth::Float); + let stages = per_pixel(&p, 64, 32, false, None).unwrap(); + let out = g.dev.develop(&g.device, &g.queue, None, &src, Some(1), [64, 32], &stages).unwrap(); + assert_eq!(out, src, "the CPU develop returns early, unclipped"); + for spatial in [ + CameraRaw { dehaze: 10.0, ..Default::default() }, + CameraRaw { shadows: 10.0, ..Default::default() }, + CameraRaw { highlights: -10.0, ..Default::default() }, + CameraRaw { texture: 10.0, ..Default::default() }, + CameraRaw { clarity: 10.0, ..Default::default() }, + CameraRaw { sharpen_amount: 10.0, ..Default::default() }, + CameraRaw { noise_luminance: 10.0, ..Default::default() }, + CameraRaw { noise_color: 10.0, ..Default::default() }, + ] { + assert!(spatial.spatial() && per_pixel(&spatial, 64, 32, false, Some((0.0, 1.0))).is_none(), "{spatial:?}"); + } + // Whites and Blacks need the image's points. + assert!(per_pixel(&CameraRaw { whites: 10.0, ..Default::default() }, 64, 32, false, None).is_none()); +} + +#[test] +fn a_cached_source_is_reused_and_a_new_key_uploads() { + let Some(mut g) = gpu() else { return }; + let p = CameraRaw { exposure: 0.5, ..Default::default() }; + let stages = per_pixel(&p, 64, 32, false, None).unwrap(); + let a = image(64, 32, Depth::Eight); + let b: Vec<[f32; 4]> = a.iter().map(|q| [q[2], q[0], q[1], 1.0]).collect(); + let first = g.dev.develop(&g.device, &g.queue, None, &a, Some(7), [64, 32], &stages).unwrap(); + // Same key: the uploaded source stays (callers key by the proxy's identity). + assert_eq!(g.dev.develop(&g.device, &g.queue, None, &b, Some(7), [64, 32], &stages).unwrap(), first); + let mut cpu = b.clone(); + develop(&mut cpu, 64, 32, &p, false); + let other = g.dev.develop(&g.device, &g.queue, None, &b, Some(8), [64, 32], &stages).unwrap(); + assert!(other.iter().zip(&cpu).all(|(x, y)| (0..3).all(|c| (x[c] - y[c]).abs() <= TOL))); +} + +#[test] +fn bad_sizes_and_faulted_devices_are_errors() { + let Some(mut g) = gpu() else { return }; + let stages = per_pixel(&CameraRaw { exposure: 0.5, ..Default::default() }, 4, 4, false, None).unwrap(); + let px = vec![[0.5f32; 4]; 16]; + for size in [[0, 4], [4, 0], [5, 4], [3, 3], [u32::MAX, 1]] { + assert!(g.dev.develop(&g.device, &g.queue, None, &px, None, size, &stages).is_err(), "{size:?}"); + } + let health = DeviceHealth::new(); + health.mark(Fault::Lost("test".into())); + assert!(g.dev.develop(&g.device, &g.queue, Some(&health), &px, None, [4, 4], &stages).is_err()); + // And the pass still works afterwards. + assert!(g.dev.develop(&g.device, &g.queue, Some(&DeviceHealth::new()), &px, None, [4, 4], &stages).is_ok()); +} diff --git a/crates/gpu/tests/device_loss.rs b/crates/gpu/tests/it/device_loss.rs similarity index 81% rename from crates/gpu/tests/device_loss.rs rename to crates/gpu/tests/it/device_loss.rs index 689a787280..2e8839de12 100644 --- a/crates/gpu/tests/device_loss.rs +++ b/crates/gpu/tests/it/device_loss.rs @@ -1,6 +1,7 @@ //! The compositor on a lost device (#243): renders return `Unsupported` (so callers use the CPU -//! compositor) instead of panicking with "Buffer 'pc_compose_uniforms' is invalid". Skips when no -//! GPU adapter exists. +//! compositor) instead of panicking with "Buffer 'pc_compose_uniforms' is invalid". Also covers a +//! caught validation error on a watched device, which must fault the health flag so callers stop +//! issuing GPU work. Skips when no GPU adapter exists. use photocraft_color::{Color, ColorMode, SampleType}; use photocraft_doc::{Document, Layer, LayerContent}; @@ -8,11 +9,10 @@ use photocraft_geom::{Rect, Size}; use photocraft_gpu::{Compositor, DeviceHealth, Fault, render_to_vec}; use photocraft_raster::Surface; -/// Concurrent wgpu instances in one process segfault on some drivers (see `parity.rs`). -static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); +use crate::GPU_LOCK; fn device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> { - let instance = wgpu::Instance::default(); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) { Ok(a) => a, Err(e) => { @@ -82,6 +82,28 @@ fn health_wait_on_a_destroyed_device_is_safe() { assert!(!health.wait(&device, None)); } +/// #3438: only wgpu reporting the device lost counts as a real loss. A fault recorded by hand (a +/// simulated loss, or an uncaptured error that names one) leaves the device usable, and +/// [`DeviceHealth::probe`] keeps polling it so a later real loss is still noticed. +#[test] +fn only_wgpu_reports_a_real_device_loss() { + let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((_adapter, device, _queue)) = device() else { return }; + let health = DeviceHealth::watch(&device); + health.probe(&device); + assert!(health.is_ok() && !health.device_lost()); + health.mark(Fault::Lost("simulated".into())); + health.probe(&device); + assert!(!health.is_ok()); + assert!(!health.device_lost(), "a recorded fault is not a real loss"); + device.destroy(); + health.probe(&device); + assert!(health.device_lost(), "a destroyed device is lost: {:?}", health.fault()); + // Once lost, probing is a no-op rather than another caught panic. + health.probe(&device); + assert!(health.device_lost()); +} + fn assert_unwatched_destroyed_device(encode_only: bool) { let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let Some((adapter, device, queue)) = device() else { return }; @@ -163,6 +185,34 @@ fn a_captured_render_error_preserves_the_device_for_a_later_render() { } } +#[test] +fn a_watched_validation_error_faults_health_instead_of_panicking() { + let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((adapter, device, queue)) = device() else { return }; + let Ok(mut comp) = Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter)) else { return }; + let health = DeviceHealth::watch(&device); + comp.set_health(health.clone()); + let d = doc(); + assert!(health.is_ok(), "a fresh watched device starts healthy"); + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + comp.render(&device, &queue, &d, d.bounds(), |_, _| { + // A buffer past the device's valid limit is rejected synchronously, so the error + // reaches the compositor's error scope and never the uncaptured-error callback a + // watched device would otherwise record it with. + let _ = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("synthetic_invalid_buffer"), + size: device.limits().max_buffer_size + 1, + usage: wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + }) + })); + assert!(result.is_ok(), "a caught validation error must return Err instead of reaching wgpu's panic handler"); + assert!(result.unwrap().is_err(), "the invalid buffer must fail this render"); + assert!(!health.is_ok(), "the caught error must fault the device's health so callers stop issuing GPU work"); + assert!(health.fault().is_some(), "the fault must be reportable"); +} + fn assert_recovery_after_invalid_encoder(encode_only: bool) { let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let Some((adapter, device, queue)) = device() else { return }; diff --git a/crates/gpu/tests/it/main.rs b/crates/gpu/tests/it/main.rs new file mode 100644 index 0000000000..027db9d995 --- /dev/null +++ b/crates/gpu/tests/it/main.rs @@ -0,0 +1,13 @@ +//! The crate's integration tests, linked into one test binary. + +/// Concurrent wgpu instances in one process segfault on some drivers (RADV), so every test that +/// creates a device takes this lock and holds it until the device is dropped. +static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +mod camera_raw; +mod device_loss; +mod parity; +mod pipeline_errors; +mod timing; +mod uniform_limits; +mod vector_mask; diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/it/parity.rs similarity index 73% rename from crates/gpu/tests/parity.rs rename to crates/gpu/tests/it/parity.rs index 84d3d0e937..d22a4515d1 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/it/parity.rs @@ -4,7 +4,7 @@ use photocraft_color::{BlendMode, Color, ColorMode, PixelFormat, SampleType}; use photocraft_doc::adjust::{CurvePoint, HueRange, LevelsChannel}; use photocraft_doc::{Adjustment, Document, Fill, GradientStyle, Layer, LayerContent, LayerMask}; -use photocraft_geom::{Rect, Size}; +use photocraft_geom::{Rect, Size, TILE_SIZE}; use photocraft_gpu::{Compositor, render_to_vec}; /// One 8-bit step, plus a few f32 ulps: two results exactly one step apart can differ by a hair @@ -12,9 +12,7 @@ use photocraft_gpu::{Compositor, render_to_vec}; /// lands on that side at the Hue/Saturation pixel in `adjustment_layers`). const TOL: f32 = 1.0 / 255.0 + 4.0 * f32::EPSILON; -/// Concurrent wgpu instances in one process segfault on some drivers (RADV), so the GPU tests -/// take this lock and hold it until their device is dropped. -static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); +use crate::GPU_LOCK; struct Gpu { device: wgpu::Device, @@ -59,6 +57,14 @@ fn hdr_adjustments_preserve_range_and_cpu_parity() { black: Vec::new(), pencil: false, }, + // #7157: an edited curve continues past 0..1 along its end segments (Photoshop's `[[0,0],[255,128]]`). + Adjustment::Curves { + master: vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 1.0, output: 128.0 / 255.0 }], + per_channel: std::array::from_fn(|_| identity.clone()), + space: Default::default(), + black: Vec::new(), + pencil: false, + }, // 32-bit Photoshop rules: unclipped, legacy pivot 127, the mixer's 1/1024 steps and halved constant. Adjustment::BrightnessContrast { brightness: 25.0, contrast: 30.0, legacy: true }, Adjustment::BrightnessContrast { brightness: -20.0, contrast: -30.0, legacy: true }, @@ -67,6 +73,7 @@ fn hdr_adjustments_preserve_range_and_cpu_parity() { Adjustment::PhotoFilter { color: [0.97, 0.76, 0.0], density: 0.4, preserve_luminosity: true }, Adjustment::PhotoFilter { color: [0.2, 0.6, 0.9], density: 0.3, preserve_luminosity: false }, Adjustment::BlackWhite { weights: [70.0, 20.0, 50.0, 10.0, 90.0, 30.0], tint: Some([0.9, 0.7, 0.5]) }, + Adjustment::BlackWhite { weights: [40.0, 60.0, 40.0, 60.0, 20.0, 80.0], tint: None }, ] { let mut d = Document::new("HDR adjustments", Size::new(3, 1), ColorMode::Rgb, SampleType::F32); let mut layer = Layer::raster("pixels", PixelFormat::RGBA32F); @@ -78,32 +85,58 @@ fn hdr_adjustments_preserve_range_and_cpu_parity() { } } +/// Phase timings for diagnosing expensive parity checks; nextest shows them only for the +/// selected slow Windows tests, without changing their timeout or pass criteria. +fn timed(what: &str, run: impl FnOnce() -> T) -> T { + let start = std::time::Instant::now(); + let result = run(); + eprintln!("parity phase {what}: {:?}", start.elapsed()); + result +} + +/// The default adapter, or the one wgpu's `WGPU_BACKEND` and `WGPU_DX12_COMPILER` choose: e.g. +/// `WGPU_BACKEND=dx12 WGPU_DX12_COMPILER=fxc` tests D3D12 with FXC, the app's default on Windows, +/// where the default adapter may be Vulkan. A backend asked for that can't run the tests fails +/// them rather than skipping. fn gpu() -> Option { let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let instance = wgpu::Instance::default(); + let required = std::env::var_os("WGPU_BACKEND").is_some(); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) { Ok(a) => a, Err(e) => { + assert!(!required, "WGPU_BACKEND is set but has no adapter: {e}"); eprintln!("skipping GPU parity tests: no adapter ({e})"); return None; } }; - let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?; - eprintln!("GPU parity backend: {:?}", adapter.get_info().backend); - // Exact parity needs 32-bit float targets. Adapters without them (e.g. GL software - // rasterizers) use the CPU compositor in the app, so there is nothing to compare there. - if Compositor::preferred_acc_format(&adapter) != wgpu::TextureFormat::Rgba32Float { - eprintln!("skipping GPU parity tests: adapter can't render Rgba32Float"); - return None; - } - let comp = match Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgba32Float) { - Ok(c) => c, + let info = adapter.get_info(); + let (device, queue) = match pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())) { + Ok(d) => d, Err(e) => { - eprintln!("skipping GPU parity tests: {e}"); + assert!(!required, "WGPU_BACKEND is set but {} has no device: {e}", info.name); + eprintln!("skipping GPU parity tests: no device ({e})"); return None; } }; - let mut paged = Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgba32Float).ok()?; + eprintln!("GPU parity backend: {:?} ({}, {:?})", info.backend, info.name, info.device_type); + // Exact parity needs 32-bit float targets. Adapters without them (e.g. GL software + // rasterizers) use the CPU compositor in the app, so there is nothing to compare there. + if Compositor::preferred_acc_format(&adapter) != wgpu::TextureFormat::Rgba32Float { + assert!(!required, "WGPU_BACKEND is set but {} can't render Rgba32Float", info.name); + eprintln!("skipping GPU parity tests: adapter can't render Rgba32Float"); + return None; + } + let compositor = || match Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgba32Float) { + Ok(c) => Some(c), + Err(e) => { + assert!(!required, "WGPU_BACKEND is set but {} can't build the compositor: {e}", info.name); + eprintln!("skipping GPU parity tests: {e}"); + None + } + }; + let comp = timed("pipeline creation", compositor)?; + let mut paged = comp.sibling(); paged.set_texture_limit(PAGED_LIMIT); Some(Gpu { device, queue, comp, paged, _lock: lock }) } @@ -112,7 +145,10 @@ fn gpu() -> Option { /// The returned guard holds [`GPU_LOCK`] until the device is dropped. fn any_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue, std::sync::MutexGuard<'static, ()>)> { let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); - let adapter = pollster::block_on(wgpu::Instance::default().request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?; + let adapter = pollster::block_on( + wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()).request_adapter(&wgpu::RequestAdapterOptions::default()), + ) + .ok()?; let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?; Some((adapter, device, queue, lock)) } @@ -151,6 +187,14 @@ fn mask(rect: Rect, seed: u32, default: f32) -> LayerMask { m } +/// `l` with a mask that reveals everything, so the executor draws it in a pass of its own rather +/// than batched with the layer below: prefix tests resume at any layer (only where a batch run +/// starts, `batch_runs`). +fn alone(mut l: Layer) -> Layer { + l.mask = Some(LayerMask::reveal_all()); + l +} + fn base_doc(w: u32, h: u32) -> Document { let mut d = Document::new("t", Size::new(w, h), ColorMode::Rgb, SampleType::U8); d.layers.push(noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, w, h), 1, 0.3)); @@ -176,10 +220,11 @@ fn worst_diff(cpu: &[[f32; 4]], gpu: &[[f32; 4]]) -> (f32, usize) { /// GPU (whole textures, then pages) vs CPU over `rect`: Err with the worst pixel if over the /// tolerance, else the stats of the unpaged render. fn diff_rect(g: &mut Gpu, doc: &Document, rect: Rect, what: &str) -> Result { - let cpu = photocraft_compose::render(doc, rect); + let cpu = timed("CPU reference", || photocraft_compose::render(doc, rect)); let mut first = None; for (label, comp) in [("", &mut g.comp), (" (paged)", &mut g.paged)] { - let (out, stats) = photocraft_gpu::render_to_vec_stats(comp, &g.device, &g.queue, doc, rect).map_err(|e| format!("{what}{label}: {e}"))?; + let (out, stats) = timed(&format!("{what}{label} GPU render/readback"), || photocraft_gpu::render_to_vec_stats(comp, &g.device, &g.queue, doc, rect)) + .map_err(|e| format!("{what}{label}: {e}"))?; let worst = worst_diff(&cpu.px, &out); let w = rect.width() as usize; let (x, y) = (rect.x0 + (worst.1 % w) as i32, rect.y0 + (worst.1 / w) as i32); @@ -195,6 +240,29 @@ fn check(g: &mut Gpu, doc: &Document, what: &str) { diff_rect(g, doc, doc.bounds(), what).unwrap_or_else(|e| panic!("{e}")); } +/// Growing a layer's tile footprint keeps the old texels in the overlap (`keep_overlap`) +/// while the same update rewrites tiles inside that overlap (#6243): whatever submission +/// shape the copy takes, the kept overlap must never overwrite tiles the same update +/// rewrote — the fresh texels win. Every step grows by one tile and rewrites everything +/// with per-step noise, on the whole-texture and the paged compositor alike, and each +/// render matches the CPU oracle. +#[test] +fn tile_growth_rewrite_keeps_fresh_texels_in_the_overlap() { + let Some(mut g) = gpu() else { return }; + let tile = TILE_SIZE as u32; + let steps = 4; + let mut doc = Document::new("tile growth", Size::new(steps * tile, tile), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::raster("grow", PixelFormat::RGBA8)); + for step in 1..=steps { + // Wider by one tile than at the last render, so the resident's region grows and every + // tile inside the kept overlap changes: stale texels cannot masquerade as correct ones. + let w = step * tile; + let data: Vec = (0..w * tile * 4).map(|i| rnd(1000 + step, i)).collect(); + doc.layers[0].surface_mut().unwrap().write_region(Rect::from_xywh(0, 0, w, tile), &data); + check(&mut g, &doc, &format!("tile growth step {step}/{steps}")); + } +} + /// Closing a document frees what the compositor holds for it, however its canvas entry went /// (#4529): `retain_docs` drops the resident pages, effect maps and lower-stack composite of /// documents that aren't open, and keeps the open ones. @@ -232,7 +300,7 @@ fn stack_prefix_reuse_matches_uncached_gpu_and_cpu_at_every_depth() { for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { let mut doc = Document::new("reuse", Size::new(300, 70), ColorMode::Rgb, depth); for seed in 1..=4 { - doc.layers.push(noise_layer("layer", doc.pixel_format(), doc.bounds(), seed, 0.2)); + doc.layers.push(alone(noise_layer("layer", doc.pixel_format(), doc.bounds(), seed, 0.2))); } check(&mut g, &doc, "populate prefix"); for (opacity, fill, blend, visible) in [ @@ -270,22 +338,31 @@ fn stack_prefix_reuse_matches_uncached_gpu_and_cpu_at_every_depth() { /// Duplicating a layer high in the stack, undoing and redoing it (#5286): the render resumes /// from the lower cached prefix and moves the cache up to just below the copy, so the undo and /// the redo skip more passes. Every render matches an uncached compositor bit for bit, at every -/// depth. +/// depth, also when plain layers are drawn several to a pass and the cache can only resume where +/// a batch starts. #[test] fn prefix_follows_an_edit_up_the_stack_and_stays_exact() { let Some(mut g) = gpu() else { return }; let mut uncached = Compositor::new(&g.device); uncached.set_composite_cache_budget(0); - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for (depth, batched) in [SampleType::U8, SampleType::U16, SampleType::F32].into_iter().flat_map(|d| [(d, false), (d, true)]) { let mut doc = Document::new("follow", Size::new(300, 70), ColorMode::Rgb, depth); for seed in 1..=6 { - doc.layers.push(noise_layer("layer", doc.pixel_format(), doc.bounds(), seed, 0.2)); + let l = noise_layer("layer", doc.pixel_format(), doc.bounds(), seed, 0.2); + doc.layers.push(if batched { l } else { alone(l) }); } let mut exact = |g: &mut Gpu, doc: &Document, what: &str| -> photocraft_gpu::Stats { let stats = diff_rect(g, doc, doc.bounds(), what).unwrap_or_else(|e| panic!("{e}")); let cached = render_to_vec(&mut g.comp, &g.device, &g.queue, doc, doc.bounds()).unwrap(); - let cold = render_to_vec(&mut uncached, &g.device, &g.queue, doc, doc.bounds()).unwrap(); - assert_eq!(cached, cold, "{depth:?} {what}: reuse preserves the GPU accumulator exactly"); + let (cold, cold_stats) = photocraft_gpu::render_to_vec_stats(&mut uncached, &g.device, &g.queue, doc, doc.bounds()).unwrap(); + assert_eq!(cached, cold, "{depth:?} batched {batched} {what}: reuse preserves the GPU accumulator exactly"); + // Batched and single-layer draws needn't give the same bits on every backend (FXC + // on D3D12): a render that captures the prefix groups the layers as an uncached one + // does, wherever its checkpoint is. + if stats.prefix_hits == 0 { + assert_eq!(stats.layers_batched, cold_stats.layers_batched, "{depth:?} {what}: capturing changed the batches"); + } + assert_eq!(cold_stats.layers_batched > 0, batched, "{depth:?} {what}"); stats }; exact(&mut g, &doc, "populate"); @@ -296,15 +373,17 @@ fn prefix_follows_an_edit_up_the_stack_and_stays_exact() { let copy = doc.layers[4].duplicate(); doc.layers.insert(5, copy); let first = exact(&mut g, &doc, "duplicate"); - assert!(first.prefix_hits > 0, "{depth:?} the duplicate resumes from the low prefix"); let undo = exact(&mut g, &low, "undo"); - assert!(undo.prefix_hits > 0, "{depth:?}"); - assert!(undo.prefix_passes_skipped > first.prefix_passes_skipped, "{depth:?} the cache moved up: {first:?} {undo:?}"); + assert!(undo.prefix_hits > 0, "{depth:?} batched {batched}"); + if !batched { + assert!(first.prefix_hits > 0, "{depth:?} the duplicate resumes from the low prefix"); + assert!(undo.prefix_passes_skipped > first.prefix_passes_skipped, "{depth:?} the cache moved up: {first:?} {undo:?}"); + } let redo = exact(&mut g, &doc, "redo"); - assert_eq!(redo.prefix_passes_skipped, undo.prefix_passes_skipped, "{depth:?}"); + assert_eq!(redo.prefix_passes_skipped, undo.prefix_passes_skipped, "{depth:?} batched {batched}"); // An edit below the moved cache still invalidates it. doc.layers[2].opacity = 0.4; - assert_eq!(exact(&mut g, &doc, "lower edit").prefix_hits, 0, "{depth:?}"); + assert_eq!(exact(&mut g, &doc, "lower edit").prefix_hits, 0, "{depth:?} batched {batched}"); } } @@ -314,12 +393,12 @@ fn prefix_follows_an_edit_up_the_stack_and_stays_exact() { #[test] fn chunks_keep_their_own_checkpoint_after_a_partial_refresh() { let Some(mut g) = gpu() else { return }; - let mut uncached = Compositor::try_new_with_format(&g.device, wgpu::TextureFormat::Rgba32Float).unwrap(); + let mut uncached = g.comp.sibling(); uncached.set_texture_limit(PAGED_LIMIT); uncached.set_composite_cache_budget(0); let mut doc = base_doc(600, 300); for seed in 2..=5 { - doc.layers.push(noise_layer("layer", PixelFormat::RGBA8, doc.bounds(), seed, 0.2)); + doc.layers.push(alone(noise_layer("layer", PixelFormat::RGBA8, doc.bounds(), seed, 0.2))); } let mut render = |g: &mut Gpu, doc: &Document, rect: Rect, what: &str| -> photocraft_gpu::Stats { let (cached, stats) = photocraft_gpu::render_to_vec_stats(&mut g.paged, &g.device, &g.queue, doc, rect).unwrap(); @@ -435,8 +514,8 @@ fn disjoint_partial_damages_admit_whole_chunks_not_damage_rects() { fn prefix_partial_damage_context_occlusion_and_budget_are_conservative() { let Some(mut g) = gpu() else { return }; let mut doc = base_doc(600, 300); - doc.layers.push(noise_layer("middle", PixelFormat::RGBA8, doc.bounds(), 2, 0.2)); - doc.layers.push(noise_layer("top", PixelFormat::RGBA8, doc.bounds(), 3, 0.2)); + doc.layers.push(alone(noise_layer("middle", PixelFormat::RGBA8, doc.bounds(), 2, 0.2))); + doc.layers.push(alone(noise_layer("top", PixelFormat::RGBA8, doc.bounds(), 3, 0.2))); check(&mut g, &doc, "full prefix"); // A partial rectangle inside an already-covered whole chunk is served as a sub-rect copy — // after a full render every in-bounds rectangle is covered, which is the point of whole @@ -560,6 +639,47 @@ fn special_eight_fill_matches_cpu() { } } +/// The same for adjustment layers (#9501): the GPU's adjustment mix takes Fill inside the special +/// eight's blend, as compose's adjustment branch does, with a mask and Opacity on top. +#[test] +fn special_eight_fill_on_adjustment_layers_matches_cpu() { + let Some(mut g) = gpu() else { return }; + let special = [ + BlendMode::ColorBurn, + BlendMode::LinearBurn, + BlendMode::ColorDodge, + BlendMode::LinearDodge, + BlendMode::VividLight, + BlendMode::LinearLight, + BlendMode::HardMix, + BlendMode::Difference, + ]; + for mode in special { + for fill in [0.3, 0.6] { + let mut d = base_doc(64, 48); + d.layers[0] = noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 5, 1.0); + let mut l = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + l.blend = mode; + l.opacity = 0.7; + l.fill_opacity = fill; + d.layers.push(l); + check(&mut g, &d, &format!("adjustment {mode:?} fill {fill}")); + // CodeRabbit on #9512: clipped to a pixel layer, the adjustment takes Fill as + // coverage on both paths (compose's clipped branch, the planner's `atop`). + let mut d = base_doc(64, 48); + d.layers[0] = noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 5, 1.0); + d.layers.push(noise_layer("base", PixelFormat::RGBA8, Rect::from_xywh(8, 6, 40, 30), 9, 1.0)); + let mut l = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + l.blend = mode; + l.opacity = 0.7; + l.fill_opacity = fill; + l.clipped = true; + d.layers.push(l); + check(&mut g, &d, &format!("clipped adjustment {mode:?} fill {fill}")); + } + } +} + #[test] fn xor_rounding_on_half_float_targets() { let Some((_adapter, device, queue, _lock)) = any_device() else { return }; @@ -649,7 +769,30 @@ fn lookup(n: usize, tetrahedral: bool, dither: bool) -> Adjustment { } } } - Adjustment::ColorLookup { name: "test".into(), lut: Some(std::sync::Arc::new(t)), size: n as u32, tetrahedral, dither } + Adjustment::ColorLookup { name: "test".into(), lut: Some(std::sync::Arc::new(t)), size: n as u32, tetrahedral, dither, source: None } +} + +/// 32-bit Hue/Saturation on colours at and above HSL lightness 1 and with a negative channel, +/// where the HSL saturation divides by zero: the GPU scales the chroma as the CPU does +/// (compose::adjust::hue_saturation_in), finite and unclipped. +#[test] +fn hdr_hue_saturation_bright_colours() { + let Some(mut g) = gpu() else { return }; + let mut d = Document::new("t", Size::new(4, 1), ColorMode::Rgb, SampleType::F32); + let mut bg = Layer::raster("bg", d.pixel_format()); + let px = [2.0, 0.0, 0.0, 1.0, 0.25, -0.25, 0.0, 1.0, 1.1, 0.9, 1.0, 1.0, 0.55, 0.45, 0.5, 1.0]; + bg.surface_mut().unwrap().write_region(Rect::from_xywh(0, 0, 4, 1), &px); + d.layers.push(bg); + for (hue, saturation) in [(40.0, 0.0), (0.0, 25.0), (0.0, -25.0), (40.0, 75.0)] { + let hs = Adjustment::HueSaturation { hue, saturation, lightness: 0.0, colorize: false, ranges: HueRange::defaults() }; + d.layers.push(Layer::new("hs", LayerContent::Adjustment(hs))); + check(&mut g, &d, &format!("hue {hue} saturation {saturation} on bright colours")); + d.layers.pop(); + } + // A PSD can store a Colorize saturation beyond 100: clamped before the 32-bit scaling on both. + let hs = Adjustment::HueSaturation { hue: 200.0, saturation: 150.0, lightness: 50.0, colorize: true, ranges: HueRange::defaults() }; + d.layers.push(Layer::new("hs", LayerContent::Adjustment(hs))); + check(&mut g, &d, "colorize saturation 150"); } #[test] @@ -671,6 +814,14 @@ fn adjustment_layers() { let pf = Adjustment::PhotoFilter { color: [0.93, 0.54, 0.0], density: 0.6, preserve_luminosity: true }; d.layers.push(Layer::new("pf", LayerContent::Adjustment(pf))); check(&mut g, &d, &format!("photo filter {depth:?}")); + // Colorize: the saturation scaled by 1 - |lightness| at 32 bits (compose::adjust::colorize_saturation). + let hs = Adjustment::HueSaturation { hue: -160.0, saturation: 40.0, lightness: 5.0, colorize: true, ranges: HueRange::defaults() }; + d.layers.push(Layer::new("hs", LayerContent::Adjustment(hs))); + check(&mut g, &d, &format!("colorize {depth:?}")); + // Hue/Saturation: at 32 bits the raised saturation isn't clipped. + let hs = Adjustment::HueSaturation { hue: 40.0, saturation: 25.0, lightness: -10.0, colorize: false, ranges: HueRange::defaults() }; + d.layers.push(Layer::new("hs2", LayerContent::Adjustment(hs))); + check(&mut g, &d, &format!("hue/saturation {depth:?}")); // Threshold: whole 16-bit samples against the level in 16 bits, or the float luminosity. d.layers.push(Layer::new("th", LayerContent::Adjustment(Adjustment::Threshold { level: 0.5 }))); check(&mut g, &d, &format!("threshold {depth:?}")); @@ -735,6 +886,31 @@ fn groups_masks_and_clipping() { d.layers.extend([base, c1, c2]); check(&mut g, &d, &format!("clipping hidden={hidden}")); } + // #3052/#4921: a clipped Dissolve layer retains opacity/fill binary coverage + // across the partially covered clipping base; the GPU must match the CPU. + for (opacity, fill) in [(0.4, 1.0), (0.8, 0.5)] { + let mut d = base_doc(56, 44); + let base = noise_layer("base", PixelFormat::RGBA8, Rect::new(5, 6, 48, 40), 45, 0.0); + let mut clip = noise_layer("dissolve clip", PixelFormat::RGBA8, d.bounds(), 46, 0.0); + clip.clipped = true; + clip.blend = BlendMode::Dissolve; + clip.opacity = opacity; + clip.fill_opacity = fill; + d.layers.extend([base, clip]); + check(&mut g, &d, "clipped Dissolve opacity/fill CPU/GPU parity"); + } + // #4921: a clipped Dissolve layer keeps binary coverage at partial opacity/fill. + for (opacity, fill) in [(0.4, 1.0), (0.8, 0.5)] { + let mut d = base_doc(56, 44); + let base = noise_layer("base", PixelFormat::RGBA8, Rect::new(5, 6, 48, 40), 45, 0.0); + let mut clip = noise_layer("dissolve clip", PixelFormat::RGBA8, d.bounds(), 46, 0.0); + clip.clipped = true; + clip.blend = BlendMode::Dissolve; + clip.opacity = opacity; + clip.fill_opacity = fill; + d.layers.extend([base, clip]); + check(&mut g, &d, "clipped Dissolve CPU/GPU parity"); + } // Clipped group base (isolated) with a clipped layer. let mut d = base_doc(56, 44); let mut gb = Layer::group("gb", vec![child(18, BlendMode::Normal)]); @@ -854,8 +1030,14 @@ fn hard_stop_effects_and_gradient_maps_request_cpu() { } let paint = FxPaint::Gradient(gradient.clone()); let effects = [ - Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient, dither: false }, - Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, 1.0), size: 1.0, position: StrokePosition::Inside, paint: paint.clone() }), + Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient, dither: false, interpolation: None }, + Effect::Stroke(StrokeFx { + common: FxCommon::new(BlendMode::Normal, 1.0), + size: 1.0, + position: StrokePosition::Inside, + paint: paint.clone(), + overprint: false, + }), Effect::InnerGlow(glow(paint.clone(), GlowTechnique::Softer, 1.0, 0.0, GlowSource::Edge)), Effect::OuterGlow(glow(paint, GlowTechnique::Softer, 1.0, 0.0, GlowSource::Edge)), ]; @@ -1143,7 +1325,7 @@ fn growing_layer_keeps_its_resident_tiles() { let what = format!("{fmt:?} dab {i}"); let s = diff_rect(&mut g, &d, d.bounds(), &what).unwrap_or_else(|e| panic!("{e}")); assert!(s.tiles_uploaded <= 2, "{what}: only the dab's tiles upload: {s:?}"); - let mut fresh = Compositor::try_new_with_format(&g.device, wgpu::TextureFormat::Rgba32Float).unwrap(); + let mut fresh = g.comp.sibling(); let want = render_to_vec(&mut fresh, &g.device, &g.queue, &d, d.bounds()).unwrap(); assert!(render_to_vec(&mut g.comp, &g.device, &g.queue, &d, d.bounds()).unwrap() == want, "{what}: same pixels as a fresh upload"); } @@ -1216,25 +1398,121 @@ fn small_layers_composite_over_their_bounds() { check(&mut g, &big, "small layers across chunks"); } -#[test] -fn many_layers_submit_inside_one_chunk() { +fn many_layers_submit_inside_one_chunk_mode(paged: bool) { // P12: a refresh recorded every pass of a page cell into one command buffer, and with a few // hundred layers Metal ran out of command buffers (device lost, "pc_compose_uniforms is - // invalid"). 700 layers need well over 1000 passes in the document's only chunk. + // invalid"). 1200 layers need well over 1000 passes in the document's only chunk: each is + // masked, so it is drawn in a pass of its own rather than batched with its neighbours. let Some(mut g) = gpu() else { return }; - let mut d = base_doc(64, 48); + // Submission count, not image area, triggers the command-buffer exhaustion. + // Keep all 1200 masked passes and blend modes, with a smaller pixel oracle. + let mut d = base_doc(16, 12); let modes: Vec = BlendMode::layer_modes().collect(); - for (i, mode) in modes.iter().copied().cycle().take(700).enumerate() { - let mut l = noise_layer("l", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 500 + i as u32, 0.0); + for (i, mode) in modes.iter().copied().cycle().take(1200).enumerate() { + let mut l = noise_layer("l", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 16, 12), 500 + i as u32, 0.0); l.blend = mode; l.opacity = 0.9; + l.mask = Some(mask(Rect::from_xywh(0, 0, 16, 12), 3000 + i as u32, 1.0)); d.layers.push(l); } - let stats = diff_rect(&mut g, &d, d.bounds(), "700 layers").unwrap_or_else(|e| panic!("{e}")); + let cpu = photocraft_compose::render(&d, d.bounds()); + let comp = if paged { &mut g.paged } else { &mut g.comp }; + let (out, stats) = photocraft_gpu::render_to_vec_stats(comp, &g.device, &g.queue, &d, d.bounds()).unwrap(); + assert!(worst_diff(&cpu.px, &out).0 <= TOL, "1200 layers, paged={paged}"); + assert_eq!(stats.layers_batched, 0, "{stats:?}"); assert_eq!(stats.chunks, 1); assert!(stats.flushes > 0, "{stats:?}"); } +#[test] +fn many_layers_submit_inside_one_chunk_whole() { + many_layers_submit_inside_one_chunk_mode(false); +} + +#[test] +fn many_layers_submit_inside_one_chunk_paged() { + many_layers_submit_inside_one_chunk_mode(true); +} + +#[test] +fn runs_of_plain_layers_blend_in_one_pass() { + // Consecutive plain layers are drawn several to a pass (`fs_layerbatch`). Every layer mode at + // varied opacity and fill (the fill-special modes included), solid fills, layers that end + // inside the canvas, and the layers that end a run: masked, Dissolve, small (clipped to their + // bounds) and clipping groups. Then an edit in the middle, so the prefix cache resumes + // part-way up the stack and runs start at its checkpoint. + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(96, 64); + let modes: Vec = BlendMode::layer_modes().collect(); + for (i, mode) in modes.iter().copied().cycle().take(90).enumerate() { + let i32_ = i as i32; + let rect = if i % 5 == 2 { Rect::from_xywh(-8, 10, 70, 40) } else { Rect::from_xywh(0, 0, 96, 64) }; + let mut l = noise_layer("l", PixelFormat::RGBA8, rect, 900 + i as u32, 0.0); + l.blend = mode; + l.opacity = 0.35 + 0.05 * (i % 13) as f32; + l.fill_opacity = if i % 4 == 1 { 0.6 } else { 1.0 }; + if i % 11 == 7 { + l.mask = Some(mask(Rect::from_xywh(i32_ % 30, 0, 50, 64), 70 + i as u32, 0.0)); + } + if i % 23 == 9 { + l.clipped = true; + } + d.layers.push(l); + if i % 9 == 4 { + let mut f = Layer::new("fill", LayerContent::Fill(Fill::Solid(Color::rgba(0.3, 0.6, 0.2, 0.4)))); + f.blend = modes[i % modes.len()]; + d.layers.push(f); + } + if i % 17 == 8 { + d.layers.push(noise_layer("small", PixelFormat::RGBA8, Rect::from_xywh(20, 20, 6, 5), 40 + i as u32, 0.5)); + } + } + let stats = diff_rect(&mut g, &d, d.bounds(), "plain layer runs").unwrap_or_else(|e| panic!("{e}")); + assert!(stats.layers_batched > 0, "no layers were batched: {stats:?}"); + let mid = d.layers.len() / 2; + d.layers[mid].opacity = 0.5; + let stats = diff_rect(&mut g, &d, d.bounds(), "plain layer runs, edited in the middle").unwrap_or_else(|e| panic!("{e}")); + assert!(stats.layers_batched > 0, "{stats:?}"); +} + +#[test] +fn runs_of_normal_layers_blend_with_normal_code_alone() { + // Runs whose layers all blend Normal draw with `fs_layerbatch_normal`: varied opacity and fill + // over a partly transparent backdrop, masked layers leading runs, solid fills and layers that + // end inside the canvas, at every depth. Lab documents mix Normal in CIELAB, so their runs + // keep the full shader. + let Some(mut g) = gpu() else { return }; + for (mode, depth) in + [(ColorMode::Rgb, SampleType::U8), (ColorMode::Rgb, SampleType::U16), (ColorMode::Rgb, SampleType::F32), (ColorMode::Lab, SampleType::U8)] + { + let mut d = Document::new("normal", Size::new(96, 64), mode, depth); + let fmt = d.pixel_format(); + d.layers.push(noise_layer("bg", fmt, Rect::from_xywh(0, 0, 96, 64), 5, 0.0)); + for i in 0..40u32 { + let rect = if i % 5 == 3 { Rect::from_xywh(-6, 9, 70, 44) } else { Rect::from_xywh(0, 0, 96, 64) }; + let mut l = noise_layer("n", fmt, rect, 600 + i, if i % 3 == 0 { 0.0 } else { 0.4 }); + l.opacity = 0.2 + 0.07 * (i % 11) as f32; + l.fill_opacity = [1.0, 0.55, 0.0, 0.8][i as usize % 4]; + if i % 7 == 2 { + l.mask = Some(mask(Rect::from_xywh(i as i32 % 25, 0, 60, 64), 50 + i, 0.0)); + } + d.layers.push(l); + if i % 6 == 4 { + let mut f = Layer::new("fill", LayerContent::Fill(Fill::Solid(Color::rgba(0.7, 0.2, 0.5, 0.6)))); + f.opacity = 0.45; + d.layers.push(f); + } + } + let what = format!("normal runs {mode:?} {depth:?}"); + let stats = diff_rect(&mut g, &d, d.bounds(), &what).unwrap_or_else(|e| panic!("{e}")); + if mode == ColorMode::Lab { + assert!(stats.layers_batched > 0 && stats.normal_batches == 0, "{what}: {stats:?}"); + } else { + assert!(stats.normal_batches > 0, "{what}: {stats:?}"); + } + } +} + fn blob(name: &str, fmt: PixelFormat, cx: f32, cy: f32, r: f32, color: [f32; 3]) -> Layer { let mut l = Layer::raster(name, fmt); let rect = Rect::new((cx - r - 12.0) as i32, (cy - r - 4.0) as i32, (cx + r + 14.0) as i32, (cy + r + 4.0) as i32); @@ -1330,6 +1608,7 @@ fn bevel(style: BevelStyle, up: bool, size: f32, soften: f32) -> Bevel { altitude: 35.0, use_global_light: true, gloss_contour: Contour::Linear, + gloss_anti_alias: false, highlight: FxCommon::new(BlendMode::Screen, 0.8), highlight_color: Color::rgb(1.0, 1.0, 0.9), shadow: FxCommon::new(BlendMode::Multiply, 0.7), @@ -1340,7 +1619,7 @@ fn bevel(style: BevelStyle, up: bool, size: f32, soften: f32) -> Bevel { } fn stroke(size: f32, position: StrokePosition, paint: FxPaint) -> StrokeFx { - StrokeFx { common: FxCommon::new(BlendMode::Normal, 0.9), size, position, paint } + StrokeFx { common: FxCommon::new(BlendMode::Normal, 0.9), size, position, paint, overprint: false } } fn checker_pattern() -> Pattern { @@ -1386,6 +1665,14 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { let mut bevel_contour = bevel(BevelStyle::InnerBevel, true, 8.0, 3.0); bevel_contour.gloss_contour = contour(); bevel_contour.use_global_light = false; + // #7360: a gloss contour that starts at 100 % (Rolling Slope - Descending), as on the type in + // the report; its flat face and slopes take the contour-mapped shading on both renderers. + let mut bevel_gloss_high = bevel(BevelStyle::InnerBevel, true, 8.0, 2.0); + bevel_gloss_high.gloss_contour = Contour::Custom { + name: "descending".into(), + points: vec![Cp { input: 0.0, output: 1.0 }, Cp { input: 0.4, output: 0.4 }, Cp { input: 1.0, output: 0.0 }], + corners: vec![], + }; vec![ ("drop shadow", vec![Effect::DropShadow(shadow(BlendMode::Multiply, 0.75, 120.0, 6.0, 8.0, 0.0))]), ("drop shadow spread contour", vec![Effect::DropShadow(ds_contour)]), @@ -1411,6 +1698,29 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { vec![Effect::InnerGlow(glow(FxPaint::Color(Color::rgb(0.2, 1.0, 0.8)), GlowTechnique::Precise, 6.0, 0.3, GlowSource::Edge))], ), ("inner glow precise center", vec![Effect::InnerGlow(glow(pat_paint.clone(), GlowTechnique::Precise, 5.0, 0.0, GlowSource::Center))]), + // Dissolve effects (#6995): the same speckle as the CPU, pixel for pixel. + ( + "outer glow dissolve", + vec![Effect::OuterGlow(Glow { + common: FxCommon::new(BlendMode::Dissolve, 0.8), + ..glow(FxPaint::Color(Color::rgb(1.0, 0.9, 0.2)), GlowTechnique::Softer, 10.0, 0.3, GlowSource::Edge) + })], + ), + ( + "inner glow dissolve gradient", + vec![Effect::InnerGlow(Glow { + common: FxCommon::new(BlendMode::Dissolve, 1.0), + ..glow(FxPaint::Gradient(gradient()), GlowTechnique::Softer, 7.0, 0.1, GlowSource::Edge) + })], + ), + ("drop shadow dissolve", vec![Effect::DropShadow(shadow(BlendMode::Dissolve, 0.75, 120.0, 6.0, 8.0, 0.0))]), + ( + "stroke outside dissolve", + vec![Effect::Stroke(StrokeFx { + common: FxCommon::new(BlendMode::Dissolve, 0.6), + ..stroke(4.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.1, 0.2, 0.9))) + })], + ), ("bevel inner", vec![Effect::BevelEmboss(bevel(BevelStyle::InnerBevel, true, 7.0, 2.0))]), ("bevel outer", vec![Effect::BevelEmboss(bevel(BevelStyle::OuterBevel, true, 6.0, 0.0))]), ("bevel emboss down", vec![Effect::BevelEmboss(bevel(BevelStyle::Emboss, false, 8.0, 1.0))]), @@ -1452,6 +1762,7 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { })], ), ("bevel contour own light", vec![Effect::BevelEmboss(bevel_contour)]), + ("bevel gloss starting high", vec![Effect::BevelEmboss(bevel_gloss_high)]), ("satin", vec![Effect::Satin(satin)]), ("satin inverted contour", vec![Effect::Satin(satin_inv)]), ("stroke outside colour", vec![Effect::Stroke(stroke(4.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.95, 0.85, 0.1))))]), @@ -1464,6 +1775,7 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { common: FxCommon::new(BlendMode::Normal, 0.8), gradient: Gradient { style: GradientStyle::Radial, ..gradient() }, dither: false, + interpolation: None, }], ), ( @@ -1535,7 +1847,7 @@ fn effect_cases() -> Vec<(&'static str, Vec)> { invert: false, }), Effect::ColorOverlay { common: FxCommon::new(BlendMode::SoftLight, 0.5), color: Color::rgb(0.9, 0.3, 0.2) }, - Effect::GradientOverlay { common: FxCommon::new(BlendMode::Overlay, 0.4), gradient: gradient(), dither: false }, + Effect::GradientOverlay { common: FxCommon::new(BlendMode::Overlay, 0.4), gradient: gradient(), dither: false, interpolation: None }, Effect::Stroke(stroke(3.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.0, 0.0, 0.0)))), ], ), @@ -1570,6 +1882,60 @@ fn layer_effects_every_kind() { assert!(failures.is_empty(), "{} failures:\n{}", failures.len(), failures.join("\n")); } +#[test] +fn exterior_effects_merge_from_the_shape_at_any_fill() { + // Drop shadows and outer glows alone: the merge builds the layer from its shape (fill inside + // it, times its alpha) instead of passes of its own. Fill from 0 to 1, opacity, modes + // (including one that blends with the knocked-out shadow), both depths, a layer covering the + // canvas (no copy back), and the layer clipped onto a base (merged atop it). + let Some(mut g) = gpu() else { return }; + let mut ds = shadow(BlendMode::Multiply, 0.75, 120.0, 6.0, 8.0, 0.0); + let mut ds_through = shadow(BlendMode::Normal, 0.9, 45.0, 4.0, 5.0, 0.2); + ds_through.knocks_out = false; + let og = glow(FxPaint::Color(Color::rgb(1.0, 0.9, 0.2)), GlowTechnique::Softer, 10.0, 0.3, GlowSource::Edge); + ds.use_global_light = true; + let stacks = [ + ("drop shadow", vec![Effect::DropShadow(ds.clone())]), + ("drop shadow, not knocked out", vec![Effect::DropShadow(ds_through)]), + ("outer glow", vec![Effect::OuterGlow(og.clone())]), + ("shadow and glow", vec![Effect::OuterGlow(og), Effect::DropShadow(ds)]), + ]; + for depth in [SampleType::U8, SampleType::U16] { + for (name, fx) in &stacks { + for (opacity, fill, blend) in [ + (1.0, 1.0, BlendMode::Normal), + (0.8, 0.6, BlendMode::Normal), + (1.0, 0.0, BlendMode::Normal), + (0.7, 0.35, BlendMode::Multiply), + (1.0, 0.5, BlendMode::Screen), + ] { + let what = format!("{name} {depth:?} opacity {opacity} fill {fill} {blend:?}"); + let mut d = fx_doc(96, 80, depth); + let mut l = blob("fx", d.pixel_format(), 46.0, 40.0, 22.0, [0.2, 0.6, 0.9]); + l.effects.items = fx.clone(); + (l.opacity, l.fill_opacity, l.blend) = (opacity, fill, blend); + d.layers.push(l.clone()); + fx_check(&mut g, &d, &what); + // The same layer clipped onto a base. + let mut base = blob("base", d.pixel_format(), 40.0, 44.0, 30.0, [0.9, 0.8, 0.3]); + base.effects.items.clear(); + d.layers.pop(); + d.layers.push(base); + l.clipped = true; + d.layers.push(l.clone()); + fx_check(&mut g, &d, &format!("{what}, clipped")); + // A partly transparent layer over the whole canvas. + let mut d = fx_doc(64, 48, depth); + let mut full = noise_layer("full", d.pixel_format(), Rect::from_xywh(0, 0, 64, 48), 7, 0.2); + full.effects.items = fx.clone(); + (full.opacity, full.fill_opacity, full.blend) = (opacity, fill, blend); + d.layers.push(full); + fx_check(&mut g, &d, &format!("{what}, full canvas")); + } + } + } +} + #[test] fn layer_effects_opacity_fill_blend_and_off_canvas() { let Some(mut g) = gpu() else { return }; @@ -1601,6 +1967,24 @@ fn layer_effects_opacity_fill_blend_and_off_canvas() { d.layers.push(l); fx_check(&mut g, &d, &format!("outside stroke at fill {fill}")); } + // An Outside stroke has no share beneath fully opaque pixels (#5861): non-Normal layers with + // an Outside stroke on a soft blob and a hard square. (Blend Interior Effects as Group, where + // that share shows through the layer's mode, is composited on the CPU.) + for blend in [BlendMode::Multiply, BlendMode::Screen] { + let mut d = fx_doc(80, 80, SampleType::U8); + let mut l = blob("ring", d.pixel_format(), 40.0, 40.0, 20.0, [0.9, 0.1, 0.1]); + l.effects.items = vec![Effect::Stroke(stroke(4.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.1, 0.2, 0.9))))]; + l.blend = blend; + d.layers.push(l); + fx_check(&mut g, &d, &format!("outside stroke under a {blend:?} blob")); + let mut d = fx_doc(40, 40, SampleType::U8); + let mut l = Layer::raster("square", d.pixel_format()); + l.surface_mut().unwrap().fill_rect(Rect::new(15, 15, 25, 25), &[1.0, 0.0, 0.0, 1.0]); + l.effects.items = vec![Effect::Stroke(stroke(3.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(0.0, 0.0, 1.0))))]; + l.blend = blend; + d.layers.push(l); + fx_check(&mut g, &d, &format!("outside stroke under a {blend:?} square")); + } // Shapes partly off the canvas (their effects reach back in) and a masked effect layer. let mut d = fx_doc(90, 70, SampleType::U8); let mut l = blob("edge", d.pixel_format(), 4.0, 66.0, 18.0, [0.9, 0.9, 0.2]); @@ -1758,7 +2142,7 @@ fn layer_effects_on_shape_layers() { let path = Path::new(vec![Subpath::polygon(&[(14.3, 12.6), (70.2, 18.1), (60.7, 58.4), (24.9, 50.2)])]); let mut fill = Color::rgb(0.3, 0.6, 0.9); fill.alpha = alpha; - let mut sh = ShapeLayer { path, fill: Some(Fill::Solid(fill)), stroke: None, live: None, cache: None, psd_raw: None }; + let mut sh = ShapeLayer { path, fill: Some(Fill::Solid(fill)), stroke: None, live: None, cache: None, psd_raw: None, ..ShapeLayer::default() }; sh.cache = Some(photocraft_vector::render_shape(&sh, d.pixel_format(), d.bounds())); let mut l = Layer::new("shape", LayerContent::Shape(sh)); l.effects.items = vec![ @@ -1796,7 +2180,7 @@ fn stroke_effects_on_filled_and_stroked_shapes() { paint: Fill::Solid(Color::rgb(0.1, 0.8, 0.2)), ..ShapeStroke::default() }); - let mut sh = ShapeLayer { path, fill: Some(fill), stroke: stroke_v, live: None, cache: None, psd_raw: None }; + let mut sh = ShapeLayer { path, fill: Some(fill), stroke: stroke_v, live: None, cache: None, psd_raw: None, ..ShapeLayer::default() }; sh.cache = Some(photocraft_vector::render_shape(&sh, d.pixel_format(), d.bounds())); let mut l = Layer::new("shape", LayerContent::Shape(sh)); if masked { @@ -1871,6 +2255,8 @@ fn blend_if() { // An adjustment judged by its result, and a clipped layer. let mut adj = Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert)); adj.blend_if.set(e, pair); + // Judged by its result before Opacity (#9542). + adj.opacity = 0.4; d.layers[1] = l.clone(); d.layers[1].blend_if = Default::default(); d.layers.push(adj); @@ -1890,6 +2276,25 @@ fn blend_if() { check(&mut g, &d, "gray blend if"); } +/// A Pass Through group with Blend If renders isolated on the GPU as on the CPU (#9554): an +/// adjustment inside it leaves the layers beneath alone. +#[test] +fn pass_through_group_with_blend_if_is_isolated() { + use photocraft_doc::BlendRange; + let Some(mut g) = gpu() else { return }; + for gated in [true, false] { + let mut d = base_doc(64, 48); + d.layers[0] = noise_layer("bg", PixelFormat::RGBA8, Rect::from_xywh(0, 0, 64, 48), 5, 1.0); + let mut grp = Layer::group("group", vec![Layer::new("inv", LayerContent::Adjustment(Adjustment::Invert))]); + grp.blend = BlendMode::PassThrough; + if gated { + grp.blend_if.set(1, [BlendRange::FULL, BlendRange { black: [0, 0], white: [110, 110] }]); + } + d.layers.push(grp); + check(&mut g, &d, &format!("pass-through group, Blend If {gated}")); + } +} + #[test] fn channel_restrictions() { let Some(mut g) = gpu() else { return }; @@ -2151,7 +2556,15 @@ fn stroked_shapes_with_clipped_layers() { d.layers.push(noise_layer("bg", d.pixel_format(), Rect::from_xywh(0, 0, 80, 64), 1, 0.3)); let path = Path::new(vec![Subpath::polygon(&[(10.3, 8.6), (66.2, 12.1), (58.7, 54.4), (16.9, 48.2)])]); let stroke = ShapeStroke { width: 5.0, paint: Fill::Solid(Color::rgb(0.9, 0.9, 0.1)), ..Default::default() }; - let mut sh = ShapeLayer { path, fill: Some(Fill::Solid(Color::rgb(0.2, 0.3, 0.8))), stroke: Some(stroke), live: None, cache: None, psd_raw: None }; + let mut sh = ShapeLayer { + path, + fill: Some(Fill::Solid(Color::rgb(0.2, 0.3, 0.8))), + stroke: Some(stroke), + live: None, + cache: None, + psd_raw: None, + ..ShapeLayer::default() + }; sh.cache = Some(photocraft_vector::render_shape(&sh, d.pixel_format(), d.bounds())); let mut l = Layer::new("shape", LayerContent::Shape(sh)); l.blend = blend; @@ -2170,6 +2583,36 @@ fn stroked_shapes_with_clipped_layers() { } } +/// A Photoshop Frame's stroke (#4381): along the group's vector mask, above its masked content, +/// for an empty frame and one holding a layer, pass-through and isolated. +#[test] +fn frame_strokes() { + let Some(mut g) = gpu() else { return }; + use photocraft_doc::vector::{Path, ShapeStroke, StrokeAlign, Subpath, VectorMask}; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for (blend, align, content) in [ + (BlendMode::PassThrough, StrokeAlign::Inside, false), + (BlendMode::PassThrough, StrokeAlign::Outside, true), + (BlendMode::Multiply, StrokeAlign::Center, true), + ] { + let mut d = Document::new("t", Size::new(80, 64), ColorMode::Rgb, depth); + d.layers.push(noise_layer("bg", d.pixel_format(), Rect::from_xywh(0, 0, 80, 64), 1, 0.3)); + let kids = if content { vec![noise_layer("c", d.pixel_format(), Rect::from_xywh(0, 0, 80, 64), 7, 0.4)] } else { vec![] }; + let mut l = Layer::group("Frame 1", kids); + l.blend = blend; + l.opacity = 0.8; + l.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(&[(10.3, 8.6), (66.2, 12.1), (58.7, 54.4), (16.9, 48.2)])]))); + if let LayerContent::Group(gr) = &mut l.content { + gr.frame_stroke = Some(ShapeStroke { width: 5.0, align, paint: Fill::Solid(Color::rgb(0.9, 0.1, 0.1)), ..Default::default() }); + } + d.layers.push(l); + let st = photocraft_gpu::render_to_vec_stats(&mut g.comp, &g.device, &g.queue, &d, d.bounds()); + assert!(st.is_ok(), "planned on the GPU"); + check(&mut g, &d, &format!("{depth:?} frame stroke {blend:?} {align:?} content {content}")); + } + } +} + #[test] fn artboards() { let Some(mut g) = gpu() else { return }; @@ -2267,6 +2710,27 @@ fn closed_documents_release_their_pattern_textures() { check(&mut g, &a, "the pattern is uploaded again after being forgotten"); } +/// A pattern whose RGBA32F texture would pass the per-pattern cap (Define Pattern accepts 8-bit +/// ones up to 8000 x 8000) is declined by the GPU path and painted by the CPU compositor, which +/// holds a large 8-bit pattern at a quarter of the texture's size. +#[test] +fn a_pattern_too_large_for_a_gpu_texture_falls_back_to_the_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = Document::new("big pattern", Size::new(8, 6), ColorMode::Rgb, SampleType::U8); + // 100 MB as RGBA8, 400 MB as an RGBA32F texture. + let surface = photocraft_raster::Surface::with_default(PixelFormat::RGBA8, &[1.0, 0.0, 0.0, 1.0]); + let pat = Pattern { id: "big".into(), name: "big".into(), width: 5000, height: 5000, surface }; + assert!(pat.is_paintable()); + d.layers.push(Layer::new( + "pattern", + LayerContent::Fill(Fill::Pattern { name: pat.name.clone(), id: pat.id.clone(), scale: 1.0, angle: 0.0, link: true, phase: (0.0, 0.0) }), + )); + d.patterns.push(pat); + assert!(render_to_vec(&mut g.comp, &g.device, &g.queue, &d, d.bounds()).is_err(), "the GPU path declines it"); + assert_eq!(g.comp.pattern_cache_bytes(), 0, "nothing was uploaded"); + assert!(photocraft_compose::flatten(&d).px.iter().all(|p| *p == [1.0, 0.0, 0.0, 1.0]), "the CPU compositor paints it"); +} + /// #4971: pattern textures are bounded while their documents stay open, by a byte budget and /// by age. What the current frame paints with is never dropped. #[test] @@ -2346,7 +2810,7 @@ fn pattern_cache_tracks_source_format_and_cow_tiles() { fn pattern_fill_layers() { let Some(mut g) = gpu() else { return }; let pat = checker_pattern(); - for (link, scale, angle, masked) in [(true, 1.0, 0.0, false), (false, 0.7, 25.0, true), (true, 1.6, -40.0, false)] { + for (link, scale, angle, masked) in [(true, 1.0, 0.0, false), (false, 0.7, 25.0, true), (true, 1.6, -40.0, false), (true, 0.3, 10.0, false)] { let mut d = base_doc(70, 50); d.patterns.push(pat.clone()); let fill = Fill::Pattern { name: pat.name.clone(), id: pat.id.clone(), scale, angle, link, phase: (3.0, -2.0) }; @@ -2558,8 +3022,9 @@ fn pages_are_evicted_under_the_budget() { let page = u64::from(g.paged.page_size()).pow(2) * 4; g.paged.set_resident_budget(page); for round in 0..3 { - let cpu = photocraft_compose::flatten(&d); - let (out, s) = photocraft_gpu::render_to_vec_stats(&mut g.paged, &g.device, &g.queue, &d, d.bounds()).unwrap(); + let cpu = timed("eviction CPU reference", || photocraft_compose::flatten(&d)); + let (out, s) = + timed("eviction GPU render/readback", || photocraft_gpu::render_to_vec_stats(&mut g.paged, &g.device, &g.queue, &d, d.bounds())).unwrap(); let worst = worst_diff(&cpu.px, &out); assert!(worst.0 <= TOL, "round {round}: max diff {:.2}/255 at {}", worst.0 * 255.0, worst.1); assert!(s.evicted > 0, "round {round}: {s:?}"); @@ -2571,12 +3036,97 @@ fn pages_are_evicted_under_the_budget() { check(&mut g, &d, "after eviction"); } +/// #6563: a page texture that doesn't fit the budget is admitted by first dropping the least +/// recently used pages of other cells (never the current cell's). With a zero budget every page +/// created in a cell evicts the previous cell's pages, and the refresh still stays on the GPU +/// (budget pressure alone never ends it on the CPU) and matches the CPU compositor. +#[test] +fn layer_pages_are_admitted_under_a_zero_budget_and_stay_on_the_gpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(600, 500); + d.layers.push(noise_layer("mid", PixelFormat::RGBA8, Rect::new(-20, 10, 590, 480), 61, 0.0)); + let mut top = noise_layer("top", PixelFormat::RGBA8, Rect::new(30, -15, 620, 510), 62, 0.2); + top.blend = BlendMode::Multiply; + d.layers.push(top); + // Exercise page residency itself: lower-stack checkpoints could skip binding the pages. + g.paged.set_composite_cache_budget(0); + check(&mut g, &d, "warm"); + let all = g.paged.resident_bytes(); + g.paged.set_resident_budget(0); + for round in 0..2 { + let cpu = photocraft_compose::flatten(&d); + let (out, s) = photocraft_gpu::render_to_vec_stats(&mut g.paged, &g.device, &g.queue, &d, d.bounds()) + .unwrap_or_else(|e| panic!("round {round}: a zero budget must not end the refresh on the CPU: {e}")); + let worst = worst_diff(&cpu.px, &out); + assert!(worst.0 <= TOL, "round {round}: max diff {:.2}/255 at {}", worst.0 * 255.0, worst.1); + assert!(s.cells > 1 && s.evicted > 0, "round {round}: {s:?}"); + // Evicted pages were created again (uploaded), and only the last cell's are left. + assert!(s.tiles_uploaded > 0, "round {round}: {s:?}"); + assert!(g.paged.resident_bytes() < all, "round {round}: {} of {all}", g.paged.resident_bytes()); + d.layers[1].surface_mut().unwrap().fill_rect(Rect::new(40 + round * 250, 60, 90 + round * 250, 120), &[0.2, 0.8, 0.3, 1.0]); + } + check(&mut g, &d, "after zero-budget renders"); +} + +/// A layer page texture the device has no memory for (#6563): the compositor drops the pages +/// this cell doesn't use and tries again, then submits, waits and runs the cell once more, and +/// only then ends this refresh on the CPU. The device stays healthy and the next refresh is back +/// on the GPU. The test hook makes the next `n` page textures fail as out of memory. +#[test] +fn a_layer_page_out_of_memory_retries_then_ends_only_that_refresh_on_the_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(600, 500); + d.layers.push(noise_layer("mid", PixelFormat::RGBA8, Rect::new(-20, 10, 590, 480), 71, 0.0)); + let mut top = noise_layer("top", PixelFormat::RGBA8, Rect::new(30, -15, 620, 510), 72, 0.2); + top.blend = BlendMode::Multiply; + d.layers.push(top); + g.paged.set_composite_cache_budget(0); + let cpu = photocraft_compose::flatten(&d); + let encode = |g: &mut Gpu, d: &Document| { + let mut encoder = g.device.create_command_encoder(&wgpu::CommandEncoderDescriptor::default()); + g.paged.encode(&g.device, &g.queue, &mut encoder, d, d.bounds(), &mut |_, _| {}) + }; + let render = |g: &mut Gpu, d: &Document| photocraft_gpu::render_to_vec_stats(&mut g.paged, &g.device, &g.queue, d, d.bounds()); + let matches = |out: &[[f32; 4]], what: &str| { + let worst = worst_diff(&cpu.px, out); + assert!(worst.0 <= TOL, "{what}: max diff {:.2}/255 at {}", worst.0 * 255.0, worst.1); + }; + // Every case starts with no pages, so the first page texture is the one that fails. + let fresh = |g: &mut Gpu, d: &Document, n: u32| { + g.paged.forget_doc(d.id); + g.paged.fail_layer_allocs(n); + }; + + // One failure: the second try right away succeeds (`encode` can't submit, so this is the + // only retry it has). + fresh(&mut g, &d, 1); + encode(&mut g, &d).unwrap_or_else(|e| panic!("one failed page texture is retried at once: {}", e.0)); + // Two: both tries fail, and `encode` gives the refresh to the CPU. + fresh(&mut g, &d, 2); + let e = encode(&mut g, &d).expect_err("encode can't submit to free memory"); + assert_eq!(e.0, photocraft_gpu::OVER_BUDGET); + assert!(g.paged.fault().is_none(), "out of memory for a page doesn't lose the device"); + // `render` submits, waits and runs the cell once more, which then succeeds. + fresh(&mut g, &d, 2); + let (out, s) = render(&mut g, &d).unwrap_or_else(|e| panic!("render retries after submitting: {}", e.0)); + assert!(s.flushes > 0, "{s:?}"); + matches(&out, "after the submit-and-retry"); + // Four: the retry after the submit fails as well, so this refresh goes to the CPU... + fresh(&mut g, &d, 4); + let e = render(&mut g, &d).expect_err("every try failed"); + assert_eq!(e.0, photocraft_gpu::OVER_BUDGET); + assert!(g.paged.fault().is_none(), "out of memory for a page doesn't lose the device"); + // ...and the next one is back on the GPU. + let (out, _) = render(&mut g, &d).unwrap_or_else(|e| panic!("the refresh after an out-of-memory one is on the GPU: {}", e.0)); + matches(&out, "the refresh after the CPU one"); +} + #[test] fn the_focused_view_stays_resident_after_a_full_refresh() { let _gamma = TEXT_GAMMA.lock().unwrap_or_else(|e| e.into_inner()); let Some(g) = gpu() else { return }; let d = big_doc(); - let mut comp = Compositor::try_new_with_format(&g.device, wgpu::TextureFormat::Rgba32Float).unwrap(); + let mut comp = g.comp.sibling(); // Exercise input-page residency itself: prefix hits can legitimately skip those pages. comp.set_composite_cache_budget(0); comp.set_texture_limit(PAGED_LIMIT); @@ -2595,11 +3145,50 @@ fn the_focused_view_stays_resident_after_a_full_refresh() { assert!(s.evicted > 0, "{s:?}"); let (out, s) = photocraft_gpu::render_to_vec_stats(&mut comp, &g.device, &g.queue, &d, view).unwrap(); assert_eq!(s.tiles_uploaded, 0, "{s:?}"); - let cpu = photocraft_compose::render(&d, view); + let cpu = timed("focused-view CPU reference", || photocraft_compose::render(&d, view)); assert!(worst_diff(&cpu.px, &out).0 <= TOL); } } +#[test] +fn layer_pages_that_just_miss_borrow_the_effect_map_share() { + let _gamma = TEXT_GAMMA.lock().unwrap_or_else(|e| e.into_inner()); + let Some(g) = gpu() else { return }; + let d = big_doc(); + let mut comp = g.comp.sibling(); + comp.set_texture_limit(PAGED_LIMIT); + let need = comp.working_set(&d, d.bounds()); + // Pages get three quarters of the budget and effect maps an eighth: at 5/4 of the need the + // pages' own share misses by about 6 %, which the effect-map share covers (#5973, P60). + let budget = need / 4 * 5; + comp.set_memory_budget(budget); + let own = comp.resident_budget(); + assert!(own < need && !comp.fits_budget(&d, d.bounds())); + assert_eq!(comp.fit_layers(need, true), need, "lends exactly the shortfall"); + assert_eq!(comp.memory_budget(), budget, "the total stays"); + comp.set_memory_budget(budget); + assert_eq!(comp.resident_budget(), need, "the loan holds across the budget set before every refresh"); + assert_eq!(comp.fit_layers(need / 2, false), need, "a partial refresh doesn't shrink it"); + photocraft_gpu::render_to_vec(&mut comp, &g.device, &g.queue, &d, d.bounds()).unwrap(); + let (out, s) = photocraft_gpu::render_to_vec_stats(&mut comp, &g.device, &g.queue, &d, d.bounds()).unwrap(); + // Every page stays resident; effect maps may be evicted and rebuilt, that's the loan. + assert_eq!(s.tiles_uploaded, 0, "{s:?}"); + assert!(worst_diff(&photocraft_compose::render(&d, d.bounds()).px, &out).0 <= TOL); + // A need the effect-map share can't cover lends nothing (it would only evict maps), and a + // smaller need gives the loan back. + assert_eq!(comp.fit_layers(budget, true), own); + comp.fit_layers(need, true); + assert_eq!(comp.fit_layers(own, true), own); + assert_eq!(comp.memory_budget(), budget); + // An exact resident budget returns the loan and lends nothing until the next memory budget. + comp.fit_layers(need, true); + comp.set_resident_budget(own); + assert_eq!(comp.memory_budget(), budget, "the loan went back to the effect maps"); + assert_eq!(comp.fit_layers(need, true), own, "an exact budget stays exact"); + comp.set_memory_budget(budget); + assert_eq!(comp.resident_budget(), need, "the next memory budget lends again"); +} + // A pass-through group holding an adjustment renders isolated when layers clip to it, on the GPU // as on the CPU (psd-tools passthrough_clipping_mask_adjustment). #[test] @@ -2618,3 +3207,212 @@ fn pass_through_clipping_base_with_an_adjustment() { d.layers.push(clip); check(&mut g, &d, "pass-through clipping base with an adjustment"); } + +/// Dissolve's adjustment/effect decisions use document coordinates, even in offset regions +/// and in the paged compositor. The clipped path comes from #4992. +#[test] +fn dissolve_adjustments_and_effects_match_cpu_in_offset_regions() { + let Some(mut g) = gpu() else { return }; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for effects in [false, true] { + let mut d = Document::new("Dissolve", Size::new(300, 280), ColorMode::Rgb, depth); + d.layers.push(noise_layer("base", d.pixel_format(), Rect::new(0, 0, 300, 280), 19, 0.4)); + let mut layer = if effects { + let mut l = noise_layer("effect", d.pixel_format(), Rect::new(-8, 13, 290, 270), 27, 0.6); + l.effects.items = vec![Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 0.7), color: Color::rgb(0.2, 0.8, 0.4) }]; + l + } else { + Layer::new("adjustment", LayerContent::Adjustment(Adjustment::Invert)) + }; + layer.blend = BlendMode::Dissolve; + layer.opacity = 0.37; + layer.mask = Some(mask(Rect::new(11, 7, 287, 265), 43, 0.0)); + d.layers.push(layer); + let label = format!("Dissolve effects={effects} {depth:?}"); + fx_check(&mut g, &d, &label); + diff_rect(&mut g, &d, Rect::new(17, 23, 281, 269), &label).unwrap(); + } + } +} + +/// Transparency Shapes Layer off changes the special eight's blend (the layer's alpha blends as +/// Fill, #5692); the GPU has no pass for it and leaves such documents to the CPU. Other modes look +/// the same either way and stay on the GPU. +#[test] +fn transparency_shapes_off_special_blend_falls_back_to_the_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(64, 48); + let mut l = noise_layer("top", PixelFormat::RGBA8, Rect::new(4, 3, 60, 45), 2, 0.0); + l.blend = BlendMode::ColorDodge; + l.opacity = 0.53; + l.advanced.transparency_shapes = false; + d.layers.push(l); + assert!(render_to_vec(&mut g.comp, &g.device, &g.queue, &d, d.bounds()).is_err(), "Color Dodge with Transparency Shapes Layer off"); + if let Some(l) = d.layers.last_mut() { + l.blend = BlendMode::Multiply; + } + check(&mut g, &d, "Multiply with Transparency Shapes Layer off"); +} + +/// #5846: a shape's Density and Feather soften its fill (its own rendered pixels), with and +/// without a vector stroke over clipped layers and layer effects: the GPU matches the CPU. +#[test] +fn soft_shapes_match_the_cpu() { + let Some(mut g) = gpu() else { return }; + use photocraft_doc::vector::{Path, ShapeLayer, ShapeStroke, Subpath}; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for (density, feather, stroked, fx) in [(1.0, 6.0, false, false), (0.4, 0.0, true, false), (0.6, 4.0, true, true), (1.0, 3.0, false, true)] { + let mut d = fx_doc(90, 70, depth); + let path = Path::new(vec![Subpath::polygon(&[(14.3, 12.6), (70.2, 18.1), (60.7, 58.4), (24.9, 50.2)])]); + let stroke = stroked.then(|| ShapeStroke { width: 3.0, paint: Fill::Solid(Color::rgb(0.1, 0.8, 0.2)), ..ShapeStroke::default() }); + let mut sh = ShapeLayer { path, fill: Some(Fill::Solid(Color::rgb(0.3, 0.6, 0.9))), stroke, density, feather, ..ShapeLayer::default() }; + sh.cache = Some(photocraft_vector::render_shape(&sh, d.pixel_format(), d.bounds())); + let mut l = Layer::new("shape", LayerContent::Shape(sh)); + if fx { + l.effects.items = vec![Effect::DropShadow(shadow(BlendMode::Multiply, 0.7, 120.0, 4.0, 5.0, 0.0))]; + } + d.layers.push(l); + if stroked { + let mut c = noise_layer("clipped", d.pixel_format(), Rect::from_xywh(0, 0, 90, 70), 11, 0.9); + c.clipped = true; + d.layers.push(c); + } + let what = format!("{depth:?} density {density} feather {feather} stroked {stroked} fx {fx}"); + if fx { + fx_check(&mut g, &d, &what); + } else { + check(&mut g, &d, &what); + } + } + } +} + +/// An adjustment has no standalone source alpha: its opacity and mask gate binary coverage. +#[test] +fn dissolve_adjustment_matches_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(64, 48); + let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + adjustment.blend = BlendMode::Dissolve; + adjustment.opacity = 0.5; + adjustment.mask = Some(mask(Rect::new(0, 0, 32, 48), 31, 0.0)); + d.layers.push(adjustment); + check(&mut g, &d, "dissolve adjustment + mask"); +} + +/// Clipped pixels still use document-positioned noise, without changing the base alpha. +#[test] +fn dissolve_clipped_layer_matches_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(64, 48); + let mut clipped = Layer::new("clipped", LayerContent::Fill(Fill::Solid(Color::BLACK))); + clipped.clipped = true; + clipped.blend = BlendMode::Dissolve; + clipped.opacity = 0.5; + d.layers.push(clipped); + check(&mut g, &d, "dissolve clipped"); +} + +/// Layer opacity gates the completed effects stack, instead of fading its pixels. +#[test] +fn dissolve_effects_stack_matches_cpu() { + let Some(mut g) = gpu() else { return }; + let mut d = base_doc(64, 48); + let mut styled = Layer::new("styled", LayerContent::Fill(Fill::Solid(Color::BLACK))); + styled.blend = BlendMode::Dissolve; + styled.opacity = 0.5; + styled.effects.items.push(Effect::ColorOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), color: Color::BLACK }); + d.layers.push(styled); + check(&mut g, &d, "dissolve effects"); +} + +#[test] +fn dissolve_clipped_adjustment_matches_cpu() { + let Some(mut g) = gpu() else { return }; + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let mut d = Document::new("clipped adjustment", Size::new(272, 24), ColorMode::Rgb, fmt.sample); + let base = noise_layer("base", fmt, Rect::new(-3, -2, 268, 22), 47, 0.0); + let mut adjustment = Layer::new("invert", LayerContent::Adjustment(Adjustment::Invert)); + adjustment.clipped = true; + adjustment.blend = BlendMode::Dissolve; + adjustment.opacity = 0.6; + adjustment.fill_opacity = 0.5; + adjustment.mask = Some(mask(Rect::new(0, 0, 260, 24), 48, 0.0)); + d.layers.extend([base, adjustment]); + check(&mut g, &d, &format!("clipped Dissolve adjustment {fmt:?}")); + diff_rect(&mut g, &d, Rect::new(250, 1, 270, 20), "clipped adjustment offset region").unwrap_or_else(|e| panic!("{e}")); + } +} + +/// Integration of #9509 and #9512: coverage endpoints, Fill, masks and clipping share the CPU oracle. +#[test] +fn integration_adjustment_fill_opacity_masks_and_hdr_match_cpu() { + let Some(mut g) = gpu() else { return }; + for clipped in [false, true] { + for mode in [BlendMode::Normal, BlendMode::ColorDodge, BlendMode::LinearBurn] { + for fill in [0.0, 0.5, 1.0] { + for opacity in [0.5, 1.0] { + for coverage in [0.5, 1.0] { + let mut d = Document::new("combined adjustment", Size::new(1, 1), ColorMode::Rgb, SampleType::F32); + let mut base = Layer::raster("base", PixelFormat::RGBA32F); + base.surface_mut().unwrap().write_pixel(0, 0, &[0.25, 0.5, 0.75, 1.0]); + let look = Adjustment::ColorLookup { + name: "half".into(), + lut: Some(std::sync::Arc::new(vec![0.5; 24])), + size: 2, + tetrahedral: false, + dither: false, + source: None, + }; + let mut layer = Layer::new("adjust", LayerContent::Adjustment(look)); + layer.blend = mode; + layer.clipped = clipped; + layer.fill_opacity = fill; + layer.opacity = opacity; + let mut mask = LayerMask::reveal_all(); + mask.surface = photocraft_raster::Surface::with_default( + PixelFormat { mode: ColorMode::Grayscale, sample: SampleType::F32, alpha: false }, + &[coverage], + ); + layer.mask = Some(mask); + d.layers = vec![base, layer]; + check(&mut g, &d, &format!("combined {mode:?} fill={fill} opacity={opacity} mask={coverage} clipped={clipped}")); + } + } + } + } + } + // The existing GPU harness requires float32 accumulation. The CPU regression separately + // covers 1e8 inputs, beyond the range of half-float source textures on some adapters. + let mut d = Document::new("HDR full coverage", Size::new(1, 1), ColorMode::Rgb, SampleType::F32); + let mut base = Layer::raster("HDR", PixelFormat::RGBA32F); + base.surface_mut().unwrap().write_pixel(0, 0, &[1.0e4, 1.0e4, 1.0e4, 1.0]); + let look = Adjustment::ColorLookup { + name: "half".into(), + lut: Some(std::sync::Arc::new(vec![0.5; 24])), + size: 2, + tetrahedral: false, + dither: false, + source: None, + }; + d.layers = vec![base, Layer::new("lookup", LayerContent::Adjustment(look))]; + check(&mut g, &d, "HDR full coverage with bounded result"); +} + +/// Siblings share only immutable GPU objects, not resident pages or document caches. +#[test] +fn sibling_compositors_have_independent_residency_and_configuration() { + let Some(mut g) = gpu() else { return }; + let d = base_doc(300, 300); + let mut sibling = g.comp.sibling(); + assert_eq!(sibling.resident_bytes(), 0); + sibling.set_texture_limit(PAGED_LIMIT); + sibling.set_resident_budget(0); + let (out, _) = photocraft_gpu::render_to_vec_stats(&mut sibling, &g.device, &g.queue, &d, d.bounds()).unwrap(); + assert!(worst_diff(&photocraft_compose::render(&d, d.bounds()).px, &out).0 <= TOL); + assert_eq!(g.comp.resident_bytes(), 0, "the original has not rendered"); + let (out, _) = photocraft_gpu::render_to_vec_stats(&mut g.comp, &g.device, &g.queue, &d, d.bounds()).unwrap(); + assert!(worst_diff(&photocraft_compose::render(&d, d.bounds()).px, &out).0 <= TOL); + assert!(g.comp.resident_bytes() > 0); + assert_ne!(g.comp.resident_budget(), sibling.resident_budget()); +} diff --git a/crates/gpu/tests/it/pipeline_errors.rs b/crates/gpu/tests/it/pipeline_errors.rs new file mode 100644 index 0000000000..28d7a56791 --- /dev/null +++ b/crates/gpu/tests/it/pipeline_errors.rs @@ -0,0 +1,50 @@ +//! #6419: the compositor's pipelines are built on several threads. A pipeline the device can't +//! build is still an `Err` from `try_new_with_format`, not a panic from wgpu's default error +//! handler (error scopes are per thread). Skips when no GPU adapter exists. + +use std::sync::Arc; + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::Document; +use photocraft_geom::Size; +use photocraft_gpu::{Compositor, render_to_vec}; + +use crate::GPU_LOCK; + +fn device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> { + let instance = wgpu::Instance::default(); + let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else { + eprintln!("skipping: no adapter"); + return None; + }; + let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?; + Some((adapter, device, queue)) +} + +#[test] +fn a_pipeline_built_on_another_thread_fails_as_an_error() { + let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((adapter, device, _queue)) = device() else { return }; + // Never a render target, so every accumulation pipeline fails validation. + let Err(e) = Compositor::try_new_with_format(&device, wgpu::TextureFormat::Rgb9e5Ufloat) else { + panic!("a compositor accumulating into Rgb9e5Ufloat"); + }; + assert!(e.0.contains("couldn't build the compositor's pipelines"), "{e:?}"); + // The device is still usable for a compositor it can build. + assert!(Compositor::try_new_with_format(&device, Compositor::preferred_acc_format(&adapter)).is_ok()); +} + +/// `new_with_format` on a device whose errors go to the caller's handler: a pipeline that failed +/// to build stays in the compositor as an invalid handle, so a draw reports GPU errors instead of +/// panicking on a missing pipeline. +#[test] +fn a_compositor_with_failed_pipelines_draws_without_panicking() { + let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((_adapter, device, queue)) = device() else { return }; + // As the app does (`DeviceHealth::watch`): GPU errors go to a handler, not wgpu's default panic. + device.on_uncaptured_error(Arc::new(|_: wgpu::Error| {})); + let mut comp = Compositor::new_with_format(&device, wgpu::TextureFormat::Rgb9e5Ufloat); + let doc = Document::with_background("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8, Color::rgba(0.5, 0.5, 0.5, 1.0)); + // Drawing into an unrenderable format can't succeed, but must not panic. + let _ = render_to_vec(&mut comp, &device, &queue, &doc, doc.bounds()); +} diff --git a/crates/gpu/tests/it/timing.rs b/crates/gpu/tests/it/timing.rs new file mode 100644 index 0000000000..68ebc9d0be --- /dev/null +++ b/crates/gpu/tests/it/timing.rs @@ -0,0 +1,311 @@ +//! The canvas timing must measure GPU *execution*, not encode-and-submit (#7186). +//! +//! `composite_ms` reports the CPU cost of handing the composite to the driver, which on a +//! GPU-bound refresh is a small fraction of what the refresh costs — at 24 MP it read 7.7 ms for +//! work that took ~171 ms. `Compositor::measure_gpu_time` adds timestamp queries whose spans are +//! summed into a [`GpuTiming`]. This test pins that down from both sides: the measurement must +//! track the waited-for wall clock and must be several times the submit time, so an accidental +//! return to timing the submit fails it. +//! +//! **Hardware only.** A software rasteriser composites on the CPU, where submit and execution +//! are the same work, so the whole premise disappears and the test would pass on a bug. It +//! therefore refuses to run on a CPU adapter and says so, as every GPU timing measurement in +//! this project must (egui_kittest's `default_wgpu_setup` prefers CPU adapters on purpose). + +use std::time::Instant; + +use photocraft_color::{BlendMode, Color, ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, Size}; +use photocraft_geom::Rect; +use photocraft_gpu::{Compositor, DeviceHealth, GpuTiming, timing}; + +/// Pixels in `r`, as the compositor counts them. +fn area(r: Rect) -> u64 { + r.width() as u64 * r.height() as u64 +} + +fn block_on(f: F) -> F::Output { + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + let mut f = std::pin::pin!(f); + loop { + if let std::task::Poll::Ready(v) = f.as_mut().poll(&mut cx) { + return v; + } + std::thread::yield_now(); + } +} + +/// A real GPU with timestamp queries, or `None` with the reason printed. +fn gpu() -> Option<(wgpu::Device, wgpu::Queue, Compositor)> { + let options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }; + let Ok(adapter) = block_on(wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()).request_adapter(&options)) else { + eprintln!("SKIP gpu timing: no adapter"); + return None; + }; + let info = adapter.get_info(); + if info.device_type == wgpu::DeviceType::Cpu || info.backend == wgpu::Backend::Noop { + // Not a skip we can be quiet about: a GPU timing test on llvmpipe measures nothing. + eprintln!("SKIP gpu timing: {} is a software adapter, which cannot show a GPU/CPU split", info.name); + return None; + } + if !adapter.features().contains(timing::FEATURES) { + eprintln!("SKIP gpu timing: {} has no timestamp queries", info.name); + return None; + } + let descriptor = wgpu::DeviceDescriptor { required_features: timing::FEATURES, ..Default::default() }; + let Ok((device, queue)) = block_on(adapter.request_device(&descriptor)) else { + eprintln!("SKIP gpu timing: no device with timestamp queries"); + return None; + }; + let mut comp = Compositor::new(&device); + assert!(comp.measure_gpu_time(&device, &queue), "the device has the features, so the timer must build"); + eprintln!("gpu timing on {} ({:?}, {:?})", info.name, info.device_type, info.backend); + Some((device, queue, comp)) +} + +fn rnd(seed: u32, i: u32) -> f32 { + let mut h = seed.wrapping_mul(0x9e37_79b9) ^ i.wrapping_mul(0x85eb_ca6b); + h ^= h >> 13; + h = h.wrapping_mul(0xc2b2_ae35); + h ^= h >> 16; + (h & 0xffff) as f32 / 65535.0 +} + +/// A document whose composite is clearly GPU-bound: several full-canvas blended photo layers. +fn document(size: Size) -> Document { + let mut doc = Document::with_background("timing", size, ColorMode::Rgb, SampleType::U8, Color::rgba(0.5, 0.5, 0.5, 1.0)); + let fmt = doc.pixel_format(); + let rect = Rect::from_xywh(0, 0, size.width, size.height); + for (i, blend) in [BlendMode::Normal, BlendMode::Multiply, BlendMode::Overlay, BlendMode::SoftLight].into_iter().enumerate() { + let mut layer = Layer::raster(format!("l{i}"), fmt); + let channels = fmt.channels(); + let px: Vec = (0..rect.width() * rect.height()).flat_map(|p| (0..channels).map(move |c| rnd(7 + i as u32 * 131 + c as u32 * 7919, p))).collect(); + if let Some(s) = layer.surface_mut() { + s.write_region(rect, &px); + } + layer.blend = blend; + layer.opacity = 0.8; + doc.layers.push(layer); + } + doc +} + +/// Render `region` and return (submit ms, waited-for wall ms, the measurement). +fn measure(device: &wgpu::Device, queue: &wgpu::Queue, comp: &mut Compositor, doc: &Document, region: Rect) -> (f64, f64, Option) { + let t0 = Instant::now(); + comp.render(device, queue, doc, region, |_, _| {}).expect("the document composites on the GPU"); + let submit_ms = t0.elapsed().as_secs_f64() * 1000.0; + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + let wall_ms = t0.elapsed().as_secs_f64() * 1000.0; + // The readback map completes on a poll, and its callback runs on the next one. + let mut timing = None; + for _ in 0..4 { + timing = comp.take_gpu_timing(); + if timing.is_some() { + break; + } + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + } + (submit_ms, wall_ms, timing) +} + +#[test] +fn gpu_time_tracks_execution_not_submission() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, mut comp)) = gpu() else { return }; + let size = Size::new(3000, 2000); + let doc = document(size); + let full = Rect::from_xywh(0, 0, size.width, size.height); + + // Warm the residency and pipelines: the first refresh uploads every layer. + let _ = measure(&device, &queue, &mut comp, &doc, full); + + // Best of three, by the smallest encode. Everything below is a ratio against `submit_ms`, + // and CPU contention on a shared machine only ever inflates it — one run that happened to + // encode behind a busy scheduler (8.2 ms against a typical 1.0 ms) failed the comparison + // while the measurement itself was right. The GPU side is unaffected by that contention, + // so taking the least-disturbed encode tests the same property without the false failure. + let mut best: Option<(f64, f64, Option)> = None; + for _ in 0..3 { + let run = measure(&device, &queue, &mut comp, &doc, full); + if run.2.is_some() && best.as_ref().is_none_or(|b| run.0 < b.0) { + best = Some(run); + } + } + let (submit_ms, wall_ms, timing) = best.expect("at least one of three refreshes reports a GPU time"); + let t = timing.expect("a hardware device with timestamp queries must report a GPU time"); + eprintln!("full {}x{}: submit {submit_ms:.1} ms, waited wall {wall_ms:.1} ms, gpu {:.1} ms", size.width, size.height, t.ms); + + assert_eq!(t.px, area(full), "the measurement carries the pixels of the refresh it measured"); + assert!(t.spans >= 1, "at least one command buffer was timed"); + + // The premise: on this adapter the refresh really is dominated by GPU execution. `wall_ms` + // includes `submit_ms`, so the GPU execution window is `wall_ms - submit_ms`; a machine fast + // enough that this window isn't well above `3.0 * submit_ms` cannot tell the two timings + // apart, so say so instead of failing. + if wall_ms - submit_ms < 3.0 * submit_ms { + eprintln!("SKIP the comparison: this GPU finishes a 6 MP composite in {wall_ms:.1} ms against {submit_ms:.1} ms of encoding"); + return; + } + + // Timing the submit again (the bug) would report roughly `submit_ms` here. + assert!(t.ms > 3.0 * submit_ms, "gpu time {:.1} ms looks like the submit time {submit_ms:.1} ms, not the execution", t.ms); + // …and it must be the real execution, not merely a bigger number: the waited-for wall clock + // is encode + execute, so the GPU span sits below it and accounts for most of it. + assert!(t.ms <= wall_ms * 1.25, "gpu time {:.1} ms exceeds the whole waited-for refresh {wall_ms:.1} ms", t.ms); + assert!(t.ms >= (wall_ms - submit_ms) * 0.5, "gpu time {:.1} ms is far below the {:.1} ms the refresh waited", t.ms, wall_ms - submit_ms); + + // A quarter of the canvas is a quarter of the per-pixel work: a constant or a stale value + // would not follow it. + let quarter = Rect::from_xywh(0, 0, size.width / 2, size.height / 2); + let (_, _, small) = measure(&device, &queue, &mut comp, &doc, quarter); + let s = small.expect("the smaller refresh is measured too"); + eprintln!("quarter: gpu {:.1} ms for {} px against {:.1} ms for {} px", s.ms, s.px, t.ms, t.px); + assert_eq!(s.px, area(quarter)); + assert!(s.ms < t.ms * 0.6, "a quarter-canvas refresh took {:.1} ms against the full canvas's {:.1} ms", s.ms, t.ms); +} + +#[test] +fn an_unmeasured_compositor_reports_nothing() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, _)) = gpu() else { return }; + let mut comp = Compositor::new(&device); + assert!(!comp.measures_gpu_time(), "timing is off until it is asked for"); + let doc = document(Size::new(256, 256)); + comp.render(&device, &queue, &doc, Rect::from_xywh(0, 0, 256, 256), |_, _| {}).expect("composites"); + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + assert_eq!(comp.take_gpu_timing(), None, "no measurement is reported rather than a wrong one"); +} + +#[test] +fn a_refresh_that_submits_several_times_is_one_measurement() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, mut comp)) = gpu() else { return }; + // A budget far below the document's layer pages makes the refresh evict as it goes, which is + // what makes it submit in batches: the measurement must span all of them, not just the last. + comp.set_memory_budget(24 << 20); + let size = Size::new(3000, 2000); + let doc = document(size); + let full = Rect::from_xywh(0, 0, size.width, size.height); + + let _ = measure(&device, &queue, &mut comp, &doc, full); + let (submit_ms, wall_ms, timing) = measure(&device, &queue, &mut comp, &doc, full); + let t = timing.expect("a batched refresh is measured like any other"); + eprintln!("batched: {} command buffers, gpu {:.1} ms, submit {submit_ms:.1} ms, waited wall {wall_ms:.1} ms", t.spans, t.ms); + assert_eq!(t.px, area(full)); + if t.spans < 2 { + eprintln!("SKIP the sum: this refresh still fit one command buffer"); + return; + } + assert!(t.ms <= wall_ms * 1.25, "summed gpu time {:.1} ms exceeds the whole waited-for refresh {wall_ms:.1} ms", t.ms); + // What this bound excludes is summing only the last command buffer. A correct sum measures + // ~85 % of the waited-for GPU time here; the flush thresholds split on accumulated work, so + // no single buffer of the several carries much over half of it, and timing only the last + // would land near 40 %. 0.6 sits between the two with room on both sides for a machine + // whose split is less even than this one's. + assert!( + t.ms >= (wall_ms - submit_ms) * 0.6, + "summed gpu time {:.1} ms is far below the {:.1} ms the refresh waited: is only the last command buffer timed?", + t.ms, + wall_ms - submit_ms + ); +} + +#[test] +fn refreshes_faster_than_the_readback_stay_valid() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, mut comp)) = gpu() else { return }; + // Refreshes arriving before the last measurement has been read back must simply go + // unmeasured. Mapping the readback buffer again while its first map is still pending is a + // validation error, and an unusable device drops the whole canvas to the CPU compositor. + let health = DeviceHealth::watch(&device); + comp.set_health(health.clone()); + let size = Size::new(600, 400); + let doc = document(size); + let full = Rect::from_xywh(0, 0, size.width, size.height); + for i in 0..16 { + comp.render(&device, &queue, &doc, full, |_, _| {}).unwrap_or_else(|e| panic!("refresh {i} failed: {e}")); + } + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + assert_eq!(comp.fault(), None, "hurried refreshes must not fault the device"); + assert_eq!(health.errors(), 0, "no uncaptured wgpu errors"); +} + +/// A refresh that recorded nothing must not stop the timer for good. `finish` leaves the +/// measurement's slots protected by the pending readback, not by `flushed`; if `flushed` +/// survived a successful finish, the next refresh to fail while that readback was still in +/// flight would set `stopped` on the strength of the *earlier* refresh's flush — even though it +/// wrote no timestamps itself — and the compositor would silently never measure again. +#[test] +fn a_failed_refresh_that_recorded_nothing_keeps_the_timer_alive() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, _)) = gpu() else { return }; + let mut timer = timing::GpuTimer::new(&device, &queue).expect("the device has timestamp queries"); + + // A refresh that flushes once, then succeeds. + let mut first = device.create_command_encoder(&Default::default()); + let mut second = device.create_command_encoder(&Default::default()); + timer.start(&mut first, 1024); + timer.split(&mut first, &mut second); + queue.submit([first.finish()]); + timer.finish(&mut second); + queue.submit([second.finish()]); + timer.submitted(); + + // A later refresh fails before recording anything, because the readback is still in flight. + let mut third = device.create_command_encoder(&Default::default()); + timer.start(&mut third, 2048); + timer.cancel(); + drop(third); + + // The first measurement still lands... + let mut first_timing = None; + for _ in 0..8 { + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + first_timing = timer.take(); + if first_timing.is_some() { + break; + } + } + let landed = first_timing.expect("the flushed measurement completes"); + assert_eq!(landed.spans, 2, "both command buffers are one measurement"); + + // ...and the timer still measures afterwards, which is what the bug destroyed. + let mut next = device.create_command_encoder(&Default::default()); + timer.start(&mut next, 4096); + timer.finish(&mut next); + queue.submit([next.finish()]); + timer.submitted(); + let mut after = None; + for _ in 0..8 { + let _ = device.poll(wgpu::PollType::Wait { submission_index: None, timeout: None }); + after = timer.take(); + if after.is_some() { + break; + } + } + let after = after.expect("the timer still measures after a refresh that recorded nothing"); + assert_eq!(after.px, 4096, "the measurement belongs to the refresh that produced it"); +} + +/// A refresh whose layer pages ran out of GPU memory even after the submit-and-retry ends on the +/// CPU (#6563), but measuring goes on: the retry waited for the GPU, so none of the dropped +/// measurement's timestamps were still in flight. +#[test] +fn an_out_of_memory_refresh_does_not_stop_gpu_timing() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + let Some((device, queue, mut comp)) = gpu() else { return }; + let doc = document(Size::new(640, 480)); + let full = doc.bounds(); + // The first page texture fails twice, and twice more in the retry after the submit. + comp.fail_layer_allocs(4); + assert!(comp.render(&device, &queue, &doc, full, |_, _| {}).is_err(), "every try failed"); + let mut timing = None; + for _ in 0..4 { + timing = measure(&device, &queue, &mut comp, &doc, full).2; + if timing.is_some() { + break; + } + } + assert!(timing.is_some(), "GPU timing stopped for good after one out-of-memory refresh"); +} diff --git a/crates/gpu/tests/uniform_limits.rs b/crates/gpu/tests/it/uniform_limits.rs similarity index 95% rename from crates/gpu/tests/uniform_limits.rs rename to crates/gpu/tests/it/uniform_limits.rs index aea8fb6b12..e562a8ca9d 100644 --- a/crates/gpu/tests/uniform_limits.rs +++ b/crates/gpu/tests/it/uniform_limits.rs @@ -5,7 +5,7 @@ use photocraft_doc::{Document, Fill, Layer, LayerContent}; use photocraft_geom::Size; use photocraft_gpu::{Compositor, DeviceHealth, plan, render_to_vec}; -static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); +use crate::GPU_LOCK; fn fills(count: usize) -> Document { fills_size(count, Size::new(1, 1)) @@ -20,7 +20,7 @@ fn fills_size(count: usize, size: Size) -> Document { } fn constrained_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> { - let instance = wgpu::Instance::default(); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) { Ok(adapter) => adapter, Err(error) => { @@ -59,7 +59,7 @@ fn a_deep_stack_above_the_device_buffer_limit_returns_an_error_without_panicking let Some((adapter, device, queue)) = constrained_device() else { return }; let mut compositor = compositor(&adapter, &device); healthy_control(&mut compositor, &device, &queue); - let deep = fills(150); + let deep = fills(300); eprintln!( "uniform-limit reproduction: backend={:?}, buffer limit={}, page={}, passes={}", adapter.get_info().backend, @@ -80,7 +80,7 @@ fn an_oversized_stack_falls_back_without_faulting_the_watched_device() { let mut compositor = compositor(&adapter, &device); compositor.set_health(health.clone()); healthy_control(&mut compositor, &device, &queue); - let deep = fills(150); + let deep = fills(300); let fallback = match render_to_vec(&mut compositor, &device, &queue, &deep, deep.bounds()) { Ok(_) => panic!("the oversized stack must return Unsupported for CPU fallback"), Err(_) => photocraft_compose::flatten(&deep), @@ -104,7 +104,8 @@ fn the_exact_buffer_limit_still_renders_with_cpu_parity() { let health = DeviceHealth::watch(&device); let mut compositor = compositor(&adapter, &device); compositor.set_health(health.clone()); - let boundary = fills(127); + // Every fill layer is one pass (`Kernel::LayerBlend`) after the clear. + let boundary = fills(254); assert_eq!(plan(&boundary).unwrap().passes.len(), 255, "one chunk plus255passes uses exactly65536bytes"); let expected = photocraft_compose::flatten(&boundary); let actual = render_to_vec(&mut compositor, &device, &queue, &boundary, boundary.bounds()).expect("the exact supported buffer boundary is accepted"); @@ -121,7 +122,7 @@ fn later_oversized_page_cells_are_rejected_before_render_or_encode_calls_the_sin let _lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let Some((adapter, device, queue)) = constrained_device() else { return }; let health = DeviceHealth::watch(&device); - let doc = fills_size(126, Size::new(4096, 2048)); + let doc = fills_size(252, Size::new(4096, 2048)); let region = photocraft_geom::Rect::new(2047, 0, 4096, 2048); assert_eq!(plan(&doc).unwrap().passes.len(), 253); // Default pages are2048, chunks1024: the narrow first page has2chunks and fits; diff --git a/crates/gpu/tests/vector_mask.rs b/crates/gpu/tests/it/vector_mask.rs similarity index 96% rename from crates/gpu/tests/vector_mask.rs rename to crates/gpu/tests/it/vector_mask.rs index 43d3d081e3..feb8597b8e 100644 --- a/crates/gpu/tests/vector_mask.rs +++ b/crates/gpu/tests/it/vector_mask.rs @@ -6,7 +6,7 @@ use photocraft_doc::{Adjustment, Document, Effect, Layer, LayerContent, LayerMas use photocraft_geom::Rect; fn gpu() -> Option<(wgpu::Device, wgpu::Queue, photocraft_gpu::Compositor)> { - let instance = wgpu::Instance::default(); + let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()); let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?; let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?; // Exact comparison needs 32-bit float targets (see parity.rs); skip on adapters without them. @@ -47,6 +47,7 @@ fn painted(name: &str, fmt: PixelFormat, rgba: [f32; 4]) -> Layer { #[test] fn vector_masks_match_the_cpu() { + let _lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner); let Some(mut g) = gpu() else { return }; for depth in [SampleType::U8, SampleType::U16] { for (inverted, density) in [(false, 1.0), (true, 1.0), (false, 0.6)] { diff --git a/crates/heif/README.md b/crates/heif/README.md index 8915129443..9d859e8401 100644 --- a/crates/heif/README.md +++ b/crates/heif/README.md @@ -30,5 +30,5 @@ inside it (fuzzing found two in 0.1.1) becomes `Error::Malformed`. The crate depends on no other PhotoCraft crate and knows nothing of `photocraft-codecs`' types; `codecs` → `heif` is the one allowed dependency between standalone crates (`cargo xtask layers`). -Tests on real files live in `photocraft-codecs` (`tests/heif.rs`, features `corpus,heif`, files +Tests on real files live in `photocraft-codecs` (`tests/it/heif.rs`, features `corpus,heif`, files fetched by `cargo xtask corpus --heif`). diff --git a/crates/heif/src/lib.rs b/crates/heif/src/lib.rs index 7c88fc1b9c..218ccde5eb 100644 --- a/crates/heif/src/lib.rs +++ b/crates/heif/src/lib.rs @@ -64,6 +64,10 @@ pub struct Decoded { pub data: Vec, /// The primary image's ICC profile. pub icc: Option>, + /// The primary image's `nclx` colour description (inherited from its tiles like [`Self::icc`]). + /// The decoder has already applied its matrix and range; its primaries and transfer + /// characteristics describe the RGB samples returned. + pub nclx: Option, /// EXIF as a TIFF structure (without HEIF's offset header). Its Orientation tag only mirrors /// what the container says; it is returned verbatim. pub exif: Option>, @@ -71,6 +75,19 @@ pub struct Decoded { pub xmp: Option, } +/// A `colr` box of type `nclx`: ITU-T H.273 code points (CICP), as written. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Nclx { + /// Colour primaries: 1 BT.709, 9 BT.2020, 12 P3-D65… + pub primaries: u16, + /// Transfer characteristics: 13 sRGB, 1/6/14/15 BT.709, 16 PQ, 18 HLG… + pub transfer: u16, + /// Matrix coefficients (already applied by the decoder). + pub matrix: u16, + /// Full-range samples (already applied by the decoder). + pub full_range: bool, +} + /// Why a file could not be decoded. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Error { @@ -155,24 +172,37 @@ fn decode_unguarded(bytes: &[u8], options: &Options) -> Result { .with_transforms(options.apply_transforms) .with_alpha(info.has_alpha); let decoded = heic_rs::decode(bytes, &raw).map_err(err)?; - let (icc, exif, xmp) = metadata(bytes); - Ok(Decoded { width: decoded.width, height: decoded.height, has_alpha: info.has_alpha, sixteen_bit: info.sixteen_bit, data: decoded.data, icc, exif, xmp }) + let (icc, nclx, exif, xmp) = metadata(bytes); + Ok(Decoded { + width: decoded.width, + height: decoded.height, + has_alpha: info.has_alpha, + sixteen_bit: info.sixteen_bit, + data: decoded.data, + icc, + nclx, + exif, + xmp, + }) } -/// The primary image's ICC profile, EXIF (TIFF structure) and XMP packet. Metadata is a +type Metadata = (Option>, Option, Option>, Option); + +/// The primary image's ICC profile, `nclx`, EXIF (TIFF structure) and XMP packet. Metadata is a /// courtesy: a broken metadata item never fails a decode whose pixels came out fine. -fn metadata(bytes: &[u8]) -> (Option>, Option>, Option) { +fn metadata(bytes: &[u8]) -> Metadata { let Ok(ctx) = heic_rs::context::Context::open(bytes) else { - return (None, None, None); + return (None, None, None, None); }; let id = ctx.meta.primary; let icc = icc_for_item(&ctx, id); + let nclx = nclx_for_item(&ctx, id); let exif = ctx.exif(id).ok().flatten().map(<[u8]>::to_vec); let xmp = ctx.xmp_item(id).and_then(|x| ctx.item_data(x).ok()).and_then(|d| match d { Cow::Borrowed(b) => String::from_utf8(b.to_vec()).ok(), Cow::Owned(b) => String::from_utf8(b).ok(), }); - (icc, exif, xmp.map(|x| x.trim_end_matches('\0').to_string())) + (icc, nclx, exif, xmp.map(|x| x.trim_end_matches('\0').to_string())) } /// The ICC profile describing `item`, inherited from the coded tiles when the item itself carries @@ -188,13 +218,26 @@ fn metadata(bytes: &[u8]) -> (Option>, Option>, Option) /// (tbraun96/heic-rs#6). The sample range cannot be recovered here as well: `DecodeOptions` has no /// override for it, so that half has to be fixed upstream. fn icc_for_item(ctx: &heic_rs::context::Context<'_>, item: u32) -> Option> { - let own = ctx.props(item).ok().and_then(|p| ctx.icc(&p).map(<[u8]>::to_vec)); + inherited(ctx, item, |p| ctx.icc(p).map(<[u8]>::to_vec)) +} + +/// The `nclx` colour description of `item`, inherited from its tiles as [`icc_for_item`] does. +/// A 10-bit HDR HEIC declares its BT.2020 primaries and HLG or PQ transfer only here (#6752). +fn nclx_for_item(ctx: &heic_rs::context::Context<'_>, item: u32) -> Option { + inherited(ctx, item, |p| { + p.nclx.map(|n| Nclx { primaries: n.primaries, transfer: n.transfer, matrix: n.matrix_code, full_range: n.range == heic_rs::Range::Full }) + }) +} + +/// `get` of `item`'s properties, or of the first of its grid tiles that has it. +fn inherited(ctx: &heic_rs::context::Context<'_>, item: u32, get: impl Fn(&heic_rs::props::ItemProps<'_>) -> Option) -> Option { + let own = ctx.props(item).ok().and_then(|p| get(&p)); if own.is_some() { return own; } // `Ok(None)` is a coded picture rather than a derivation: no tiles to inherit from. let (_, tiles) = ctx.grid(item).ok()??; - tiles.iter().find_map(|&t| ctx.props(t).ok().and_then(|p| ctx.icc(&p).map(<[u8]>::to_vec))) + tiles.iter().find_map(|&t| ctx.props(t).ok().and_then(|p| get(&p))) } #[cfg(test)] @@ -297,6 +340,17 @@ mod tests { bx(b"colr", &p) } + /// A `colr` box of type `nclx`, laid out as ISO/IEC 23008-12 says: three big-endian `u16` + /// code points, then the full-range flag in the top bit of a byte. + fn colr_nclx(primaries: u16, transfer: u16, matrix: u16, full_range: bool) -> Vec { + let mut p = b"nclx".to_vec(); + for v in [primaries, transfer, matrix] { + p.extend_from_slice(&v.to_be_bytes()); + } + p.push(if full_range { 0x80 } else { 0 }); + bx(b"colr", &p) + } + fn ispe(w: u32, h: u32) -> Vec { let mut p = w.to_be_bytes().to_vec(); p.extend_from_slice(&h.to_be_bytes()); @@ -340,16 +394,21 @@ mod tests { /// `ipco` children are numbered from 1 in file order; an association byte's top bit marks the /// property essential, as macOS writes it. fn grid_file(grid_icc: Option<&[u8]>, tile_icc: Option<&[u8]>) -> Vec { + grid_file_colr(grid_icc.map(colr_prof), tile_icc.map(colr_prof)) + } + + /// [`grid_file`] with whole `colr` boxes. + fn grid_file_colr(grid_colr: Option>, tile_colr: Option>) -> Vec { let mut ipco = ispe(64, 64); let (mut grid_assoc, mut tile_assoc, mut index) = (vec![0x81], vec![0x81], 1u8); - if let Some(icc) = grid_icc { + if let Some(colr) = grid_colr { index += 1; - ipco.extend_from_slice(&colr_prof(icc)); + ipco.extend_from_slice(&colr); grid_assoc.push(0x80 | index); } - if let Some(icc) = tile_icc { + if let Some(colr) = tile_colr { index += 1; - ipco.extend_from_slice(&colr_prof(icc)); + ipco.extend_from_slice(&colr); tile_assoc.push(0x80 | index); } let mut iprp = bx(b"ipco", &ipco); @@ -374,10 +433,15 @@ mod tests { /// One coded picture rather than a derivation, with `icc` on the item itself. fn picture_file(icc: Option<&[u8]>) -> Vec { + picture_file_colr(icc.map(colr_prof)) + } + + /// [`picture_file`] with a whole `colr` box. + fn picture_file_colr(colr: Option>) -> Vec { let mut ipco = ispe(64, 64); let mut assoc = vec![0x81]; - if let Some(icc) = icc { - ipco.extend_from_slice(&colr_prof(icc)); + if let Some(colr) = colr { + ipco.extend_from_slice(&colr); assoc.push(0x82); } let mut iprp = bx(b"ipco", &ipco); @@ -436,4 +500,30 @@ mod tests { let ctx = ctx_of(&bare); assert_eq!(icc_for_item(&ctx, ctx.meta.primary), None); } + /// An iPhone 10-bit HDR HEIC's description (#6752): BT.2020 primaries, ARIB STD-B67 (HLG) + /// transfer, BT.2020 non-constant-luminance matrix, full range. + const HLG: Nclx = Nclx { primaries: 9, transfer: 18, matrix: 9, full_range: true }; + + #[test] + fn a_coded_pictures_nclx_is_read() { + let bytes = picture_file_colr(Some(colr_nclx(9, 18, 9, true))); + let ctx = ctx_of(&bytes); + assert_eq!(nclx_for_item(&ctx, ctx.meta.primary), Some(HLG)); + + let sdr = picture_file_colr(Some(colr_nclx(1, 13, 6, false))); + let ctx = ctx_of(&sdr); + assert_eq!(nclx_for_item(&ctx, ctx.meta.primary), Some(Nclx { primaries: 1, transfer: 13, matrix: 6, full_range: false })); + + let bare = picture_file(Some(TILE_ICC)); + let ctx = ctx_of(&bare); + assert_eq!(nclx_for_item(&ctx, ctx.meta.primary), None, "an ICC profile is not an nclx"); + } + + #[test] + fn a_grid_inherits_its_tiles_nclx() { + let bytes = grid_file_colr(None, Some(colr_nclx(9, 18, 9, true))); + let ctx = ctx_of(&bytes); + assert!(ctx.props(ctx.meta.primary).unwrap().nclx.is_none(), "the grid item carries no colr of its own"); + assert_eq!(nclx_for_item(&ctx, ctx.meta.primary), Some(HLG)); + } } diff --git a/crates/io/Cargo.toml b/crates/io/Cargo.toml index 9bd4c559c5..4eb91fb728 100644 --- a/crates/io/Cargo.toml +++ b/crates/io/Cargo.toml @@ -31,6 +31,7 @@ photocraft-text = { workspace = true } photocraft-paint = { workspace = true } photocraft-vector = { workspace = true } photocraft-format = { path = "../format" } +craft-docpath = { workspace = true } thiserror = { workspace = true } serde_json = { workspace = true } blake3 = { version = "1.8", default-features = false, features = ["pure"] } @@ -38,7 +39,7 @@ blake3 = { version = "1.8", default-features = false, features = ["pure"] } # pdn.rs inflates Paint.NET's gzip pixel chunks. flate2 = { workspace = true } # ora.rs reads OpenRaster's stack.xml (already in the tree through usvg). -roxmltree = "0.20" +roxmltree = "0.21" # SVG import (svg.rs): usvg parses, resvg rasterises what the shape model can't hold. Both are # already in the tree through the UI's icon loader; text shaping needs the fonts feature set. [target.'cfg(not(target_arch = "wasm32"))'.dependencies] @@ -48,9 +49,15 @@ resvg = { version = "0.45", default-features = false, features = ["text", "syste resvg = { version = "0.45", default-features = false, features = ["text", "raster-images"] } [dev-dependencies] +miniz_oxide = "0.9" crc32fast = "1.5" photocraft-affinity = { workspace = true, features = ["synth"] } flate2 = { workspace = true } photocraft-psd = { workspace = true, features = ["testgen"] } photocraft-raw = { workspace = true, features = ["testgen"] } +photocraft-testkit = { workspace = true } proptest = { workspace = true } +# Lossless JPEG XL encoder for the JPEG XL DNG preview test (tests only), as in photocraft-raw. +zune-jpegxl = { version = "=0.5.2", default-features = false, features = ["std"] } +zune-core = { workspace = true } +tiff = { version = "0.11", default-features = false, features = ["deflate", "lzw"] } diff --git a/crates/io/examples/bench_abr.rs b/crates/io/examples/bench_abr.rs index 0811cb0ae8..17d4740962 100644 --- a/crates/io/examples/bench_abr.rs +++ b/crates/io/examples/bench_abr.rs @@ -62,7 +62,7 @@ fn main() { // `--write `: also save a small showcase file (procedural tips) for UI snapshots. let args: Vec = std::env::args().collect(); if let Some(path) = args.iter().position(|a| a == "--write").and_then(|i| args.get(i + 1)) { - use photocraft_paint::procedural::*; + use photocraft_paint::procedural::{charcoal_tip, leaf_tip, sponge_tip, star_tip}; let tips = [("Leafy", leaf_tip(96)), ("Charcoal Stick", charcoal_tip(96, 3)), ("Sea Sponge", sponge_tip(96, 5)), ("Star Burst", star_tip(96))]; let samples: Vec = tips .iter() diff --git a/crates/io/examples/bench_export.rs b/crates/io/examples/bench_export.rs new file mode 100644 index 0000000000..bd5685ecc7 --- /dev/null +++ b/crates/io/examples/bench_export.rs @@ -0,0 +1,93 @@ +//! Export timings on a realistic photo document (default 7360×4912 = 36 MP): the opened photo, +//! a painted layer covering part of the canvas, and a soft half-transparent layer. +//! +//! ```sh +//! cargo run --release -p photocraft-io --example bench_export -- [--size 7360x4912] [--reps 7] [--only pcraft] [--out DIR] +//! ``` +//! +//! Each row is the median (and min) of `--reps` runs. `--out DIR` writes each format's bytes to +//! `DIR/out.` so two builds' output can be compared. +use std::time::Instant; + +use photocraft_doc::Layer; +use photocraft_geom::Rect; + +fn arg(args: &[String], name: &str) -> Option { + args.iter().position(|a| a == name).and_then(|i| args.get(i + 1).cloned()) +} + +fn photo(w: u32, h: u32) -> photocraft_codecs::Image { + let mut px = vec![0u8; w as usize * h as usize * 3]; + for (y, row) in px.chunks_mut(w as usize * 3).enumerate() { + for x in 0..w as usize { + let (fx, fy) = (x as f32 / w as f32, y as f32 / h as f32); + let n = ((x.wrapping_mul(73_856_093) ^ y.wrapping_mul(19_349_663)) % 997) as f32 / 997.0 - 0.5; + let blob = (-(((fx - 0.4) * 3.0).powi(2) + ((fy - 0.55) * 4.0).powi(2))).exp(); + let r = 0.55 + 0.35 * (fx * 6.0).sin() * 0.5 + 0.3 * blob + n * 0.06; + let g = 0.45 + 0.25 * (fy * 9.0 + fx * 2.0).cos() * 0.5 + 0.2 * blob + n * 0.06; + let b = 0.35 + 0.4 * fy - 0.2 * blob + n * 0.06; + for (c, v) in [r, g, b].into_iter().enumerate() { + row[x * 3 + c] = (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + } + } + photocraft_codecs::Image::from_raw(w, h, photocraft_codecs::ChannelLayout::Rgb, photocraft_codecs::SampleType::U8, px).expect("image") +} + +fn main() { + let args: Vec = std::env::args().collect(); + let (w, h) = arg(&args, "--size").and_then(|s| s.split_once('x').and_then(|(a, b)| Some((a.parse().ok()?, b.parse().ok()?)))).unwrap_or((7360u32, 4912u32)); + let reps: usize = arg(&args, "--reps").and_then(|s| s.parse().ok()).unwrap_or(7); + let only = arg(&args, "--only"); + let out = arg(&args, "--out"); + + let png = photocraft_codecs::encode(&photo(w, h), photocraft_codecs::Format::Png, &Default::default()).expect("png"); + let mut doc = photocraft_io::import("photo.png", &png).expect("import").document; + let fmt = doc.pixel_format(); + let mut paint = Layer::raster("paint", fmt); + if let Some(s) = paint.surface_mut() { + let (pw, ph) = (w / 2, h / 3); + let data: Vec = (0..pw * ph) + .flat_map(|i| { + let (x, y) = (i % pw, i / pw); + let a = if (x / 64 + y / 64) % 3 == 0 { 0.0 } else { 0.85 }; + [0.75, (x % 256) as f32 / 255.0, (y % 256) as f32 / 255.0, a] + }) + .collect(); + s.write_region(Rect::from_xywh(w as i32 / 8, h as i32 / 4, pw, ph), &data); + } + doc.layers.push(paint); + let mut soft = Layer::raster("soft", fmt); + soft.opacity = 0.6; + if let Some(s) = soft.surface_mut() { + s.fill_rect(Rect::from_xywh(w as i32 / 2, 0, w / 2, h), &[0.1, 0.2, 0.9, 0.5]); + } + doc.layers.push(soft); + + println!("document {w}×{h}, {} layers, {reps} reps (median / min ms)", doc.layers.len()); + // "pcraft+cache" is the app's Save: one export cache per document, so the first save + // compresses every tile and later ones reuse what fits the cache (timed after a warm-up save). + for ext in ["psd", "pcraft", "pcraft+cache", "tif", "png", "jpg"] { + if only.as_deref().is_some_and(|o| o != ext) { + continue; + } + let cached = ext == "pcraft+cache"; + let opts = photocraft_io::ExportOptions { pcraft_cache: cached.then(photocraft_io::PcraftExportCache::default), ..Default::default() }; + let name = format!("x.{}", ext.trim_end_matches("+cache")); + if cached { + photocraft_io::export(&doc, &name, &opts).expect("warm-up"); + } + let mut times = Vec::with_capacity(reps); + let mut bytes = Vec::new(); + for _ in 0..reps { + let t = Instant::now(); + bytes = photocraft_io::export(&doc, &name, &opts).expect("export").bytes; + times.push(t.elapsed().as_secs_f64() * 1000.0); + } + times.sort_by(f64::total_cmp); + println!("{ext:>7}: {:9.1} / {:9.1} ({} bytes)", times[times.len() / 2], times[0], bytes.len()); + if let Some(dir) = &out { + std::fs::write(format!("{dir}/out.{}", ext.replace('+', "-")), &bytes).expect("write"); + } + } +} diff --git a/crates/io/examples/big_psb_check.rs b/crates/io/examples/big_psb_check.rs index 9b1eb80d47..3b1c21034b 100644 --- a/crates/io/examples/big_psb_check.rs +++ b/crates/io/examples/big_psb_check.rs @@ -1,14 +1,10 @@ //! Opens the files written by `photocraft-psd`'s `big_psb` example and checks their pixels. //! //! `cargo run --release -p photocraft-io --example big_psb_check -- ...` +//! +//! The opt-in tests in `tests/it/large_files.rs` generate and check the same files. -fn sample(x: u32, y: u32, c: u32, rle: bool) -> u8 { - if c == 3 { - return 255; - } - let x = if rle { 0 } else { x }; - (x.wrapping_mul(7).wrapping_add(y.wrapping_mul(13)).wrapping_add(c.wrapping_mul(101)) & 0xff) as u8 -} +use photocraft_psd::fixtures::sample; fn main() { for path in std::env::args().skip(1) { diff --git a/crates/io/examples/gen_abr_library.rs b/crates/io/examples/gen_abr_library.rs index 2736e49d06..c19e4a85d8 100644 --- a/crates/io/examples/gen_abr_library.rs +++ b/crates/io/examples/gen_abr_library.rs @@ -10,10 +10,10 @@ //! //! Options (defaults in brackets): `--brushes N` [600], `--edge PX` largest tip edge [2000], //! `--min-edge PX` smallest tip edge [800], `--depth 8|16` [16], `--per-file N` presets per file [100], -//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's 512 MB decoded cap. +//! `--raw` store tips uncompressed (bigger files). Each file stays under the reader's decoded cap (1 GiB; 512 MiB on 32-bit). #![allow(clippy::unwrap_used, clippy::expect_used)] -use photocraft_paint::procedural::*; +use photocraft_paint::procedural::{bristle_tip, chalk_tip, charcoal_tip, grass_tip, leaf_tip, rake_tip, spatter_tip, sponge_tip, star_tip}; use photocraft_paint::tile::GrayTile; use photocraft_psd::abr::{AbrSample, MAX_TOTAL_BYTES, write_v6}; use photocraft_psd::descriptor::{Descriptor, UnicodeString, Value}; diff --git a/crates/io/examples/oracle_diff.rs b/crates/io/examples/oracle_diff.rs index a7f295d463..cc4b5aa0de 100644 --- a/crates/io/examples/oracle_diff.rs +++ b/crates/io/examples/oracle_diff.rs @@ -22,9 +22,244 @@ fn main() { corpus_summary(std::path::Path::new(&path)); return; } - let bytes = std::fs::read(&path).unwrap(); + run_file(&path); +} + +fn run_file(path: &str) { + let bytes = std::fs::read(path).unwrap(); let file = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); - let mut imp = photocraft_io::import(&path, &bytes).unwrap(); + let mut imp = photocraft_io::import(path, &bytes).unwrap(); + configure_visibility(&mut imp.document.layers); + let doc = &imp.document; + println!("mode {:?} depth {:?} layers {} warnings {:?} light {:?}", doc.mode, doc.depth, doc.layer_count(), imp.warnings, doc.global_light); + print_layer_details(doc); + dump_filter_effects(&file); + let ours = photocraft_compose::flatten(doc).px; + let merged = photocraft_io::merged_composite(&file).unwrap(); + compare_modes(doc, &ours, &merged); +} + +fn compare_modes(doc: &photocraft_doc::Document, ours: &[[f32; 4]], merged: &[[f32; 4]]) { + if std::env::args().nth(3).as_deref() == Some("png") { + write_comparison_png(doc, ours, merged); + return; + } + if std::env::args().nth(3).as_deref() == Some("dump") { + // dump prefix: raw little-endian f32 RGBA (straight) planes for offline fitting: + // prefix_ours.f32, prefix_ps.f32, prefix_L.f32 (each top-level layer's own pixels, + // masks not applied) and prefix.txt (width, height, layer names). + let out = std::env::args().nth(4).expect("prefix"); + let wr = |name: String, px: &[[f32; 4]]| { + let bytes: Vec = px.iter().flat_map(|p| p.iter().flat_map(|v| v.to_le_bytes())).collect(); + std::fs::write(name, bytes).unwrap(); + }; + wr(format!("{out}_ours.f32"), ours); + wr(format!("{out}_ps.f32"), merged); + let mut meta = format!("{} {}\n", doc.size.width, doc.size.height); + for (i, l) in doc.layers.iter().enumerate() { + let b = photocraft_compose::surface_to_buffer( + l.surface().unwrap_or(&photocraft_raster::Surface::new(photocraft_color::PixelFormat::RGBA8)), + doc.bounds(), + ); + wr(format!("{out}_L{i}.f32"), &b.px); + meta += &format!("{i} {}\n", l.name); + } + std::fs::write(format!("{out}.txt"), meta).unwrap(); + return; + } + if std::env::args().nth(3).as_deref() == Some("layerpx") { + // layerpx x y: each layer's own pixel (0-255). + let x: i32 = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(0); + let y: i32 = std::env::args().nth(5).and_then(|s| s.parse().ok()).unwrap_or(0); + for l in doc.walk() { + if let Some(s) = l.2.surface() { + println!("{:?}: {:?}", l.2.name, s.pixel(x, y).iter().map(|v| (v * 255.0 * 100.0).round() / 100.0).collect::>()); + } + } + return; + } + if std::env::args().nth(3).as_deref() == Some("grid") { + // grid x0 y0 x1 y1 [channel]: ours / photoshop values (0-255, premultiplied colour). + let w = doc.size.width as usize; + let a: Vec = (4..8).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); + let ch: usize = std::env::args().nth(8).and_then(|s| s.parse().ok()).unwrap_or(3); + for (label, img) in [("ours", &ours), ("ps", &merged)] { + println!("{label} (channel {ch}):"); + for y in a[1]..a[3] { + let row: Vec = (a[0]..a[2]) + .map(|x| { + let p = img[y * w + x]; + let v = if ch == 3 { p[3] } else { p[ch] * p[3] }; + format!("{:4}", (v * 255.0).round()) + }) + .collect(); + println!(" y={y:4} {}", row.join("")); + } + } + return; + } + if std::env::args().nth(3).as_deref() == Some("bylayer") { + // Max error and bad-pixel count inside each layer's bounds grown by its effects' reach + // (regions overlap; a quick way to tell which layer or effect is off). + let (w, h) = (doc.size.width as i32, doc.size.height as i32); + for (_, _, l) in doc.walk() { + let Some(sf) = l.surface() else { continue }; + let cb = sf.content_bounds(); + let mut b = photocraft_geom::Rect::new(i32::MAX, i32::MAX, i32::MIN, i32::MIN); + for y in cb.y0..cb.y1 { + for x in cb.x0..cb.x1 { + let p = sf.pixel(x, y); + if p[p.len() - 1] > 0.0 { + b = photocraft_geom::Rect::new(b.x0.min(x), b.y0.min(y), b.x1.max(x + 1), b.y1.max(y + 1)); + } + } + } + if b.x0 > b.x1 { + continue; + } + let m = photocraft_compose::effects::margin(l); + let r = photocraft_geom::Rect::new((b.x0 - m).max(0), (b.y0 - m).max(0), (b.x1 + m).min(w), (b.y1 + m).min(h)); + let (mut worst, mut at, mut bad) = (0.0f32, (0, 0), 0usize); + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let i = (y * w + x) as usize; + let (a, b) = (ours[i], merged[i]); + let d = (0..3).map(|c| (a[c] * a[3] - b[c] * b[3]).abs()).fold((a[3] - b[3]).abs(), f32::max); + if d > 2.0 / 255.0 { + bad += 1; + } + if d > worst { + (worst, at) = (d, (x, y)); + } + } + } + let fx: Vec<&str> = l.effects.items.iter().filter(|e| e.enabled()).map(|e| e.label()).collect(); + println!("{:<32} {:>6.1} at {:?} bad {:>6} {:?} {:?}", l.name, worst * 255.0, at, bad, l.blend, fx); + } + return; + } + if std::env::args().nth(3).as_deref() == Some("worst") { + // The N worst pixels (premultiplied max channel error), with coordinates. + let w = doc.size.width as usize; + let n: usize = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(20); + let mut v: Vec<(f32, usize)> = ours + .iter() + .zip(merged) + .enumerate() + .map(|(i, (a, b))| ((0..4).map(|c| (a[c] * a[3] - b[c] * b[3]).abs()).fold((a[3] - b[3]).abs(), f32::max), i)) + .collect(); + v.sort_by(|a, b| b.0.total_cmp(&a.0)); + for (d, i) in v.into_iter().take(n) { + let (a, b) = (ours[i], merged[i]); + println!("({:4},{:4}) d={:5.1} ours {:?} ps {:?}", i % w, i / w, d * 255.0, a.map(|v| (v * 255.0).round()), b.map(|v| (v * 255.0).round())); + } + return; + } + if std::env::args().nth(3).as_deref() == Some("row") { + let w = doc.size.width as usize; + let y: usize = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(0); + let (x0, x1): (usize, usize) = + (std::env::args().nth(5).and_then(|s| s.parse().ok()).unwrap_or(0), std::env::args().nth(6).and_then(|s| s.parse().ok()).unwrap_or(w)); + for x in (x0..x1).step_by(((x1 - x0) / 20).max(1)) { + let (a, b) = (ours[y * w + x], merged[y * w + x]); + println!("x={x:5} ours {:?} ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); + } + return; + } + if std::env::args().nth(3).as_deref() == Some("solve") { + // solve layer-name x0 y0 x1 y1: per pixel the layer's content alpha `l`, its shape's path + // coverage `cov`, Photoshop's alpha `A` and the alpha `s` an effect beneath the layer + // would need (A = s + l (1 - s)) over a transparent backdrop. + let name = std::env::args().nth(4).expect("layer name"); + let a: Vec = (5..9).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); + let w = doc.size.width as usize; + let l = doc.walk().into_iter().map(|t| t.2).find(|l| l.name == name).expect("layer"); + let r = photocraft_geom::Rect::new(a[0], a[1], a[2], a[3]); + let cov = match &l.content { + photocraft_doc::LayerContent::Shape(sh) => photocraft_vector::path_coverage(&sh.path, r), + _ => vec![0.0; r.width() as usize * r.height() as usize], + }; + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let la = l.surface().map_or(0.0, |s| s.pixel(x, y)[3]); + let c = cov[((y - r.y0) * r.width() as i32 + x - r.x0) as usize]; + let p = merged[y as usize * w + x as usize]; + let o = ours[y as usize * w + x as usize]; + let s = if la < 1.0 { (p[3] - la) / (1.0 - la) } else { f32::NAN }; + println!("({x:3},{y:3}) l {la:.3} cov {c:.3} A {:.3} ours {:.3} s {s:.3} ps {:?}", p[3], o[3], p.map(|v| (v * 255.0).round())); + } + } + return; + } + if std::env::args().nth(3).as_deref() == Some("line") { + // line x0 y0 x1 y1: every pixel on the segment, straight RGBA 0-255, ours | ps. + let w = doc.size.width as usize; + let a: Vec = (4..8).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); + let n = (a[2] - a[0]).abs().max((a[3] - a[1]).abs()).max(1); + for k in 0..=n { + let (x, y) = ((a[0] + (a[2] - a[0]) * k / n) as usize, (a[1] + (a[3] - a[1]) * k / n) as usize); + let (o, p) = (ours[y * w + x], merged[y * w + x]); + println!("({x:4},{y:4}) ours {:?} ps {:?}", o.map(|v| (v * 255.0).round()), p.map(|v| (v * 255.0).round())); + } + return; + } + if std::env::args().nth(3).as_deref() == Some("col") { + let (w, h) = (doc.size.width as usize, doc.size.height as usize); + let x = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(w / 2); + for k in 0..=16 { + let y = ((h - 1) * k / 16).min(h - 1); + let (a, b) = (ours[y * w + x], merged[y * w + x]); + println!("y={y:5} ours {:?} ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); + } + return; + } + let n = std::env::args().nth(2).and_then(|s| s.parse().ok()).unwrap_or(8usize); + for (i, (a, b)) in ours.iter().zip(merged).enumerate().take(n) { + println!("{i:4}: ours {:?}\n ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); + } +} + +fn write_comparison_png(doc: &photocraft_doc::Document, ours: &[[f32; 4]], merged: &[[f32; 4]]) { + // ours | photoshop | diff heatmap, side by side (over white). + let out = std::env::args().nth(4).expect("out.png"); + let (w, h) = (doc.size.width as usize, doc.size.height as usize); + let mut img = vec![0u8; w * 3 * h * 4]; + let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + for y in 0..h { + for x in 0..w { + let (a, b) = (ours[y * w + x], merged[y * w + x]); + let over = |p: [f32; 4]| [p[0] * p[3] + 1.0 - p[3], p[1] * p[3] + 1.0 - p[3], p[2] * p[3] + 1.0 - p[3]]; + let (oa, ob) = (over(a), over(b)); + let d = (0..3).map(|c| (oa[c] - ob[c]).abs()).fold(0.0f32, f32::max); + let heat = [q((d * 4.0).min(1.0)), q((1.0 - d * 4.0).max(0.0) * 0.3), 0]; + for (k, px) in [[q(oa[0]), q(oa[1]), q(oa[2])], [q(ob[0]), q(ob[1]), q(ob[2])], heat].iter().enumerate() { + let o = (y * w * 3 + k * w + x) * 4; + img[o..o + 3].copy_from_slice(px); + img[o + 3] = 255; + } + } + } + // SCALE=n: nearest-neighbour upscale (small files). + let k: usize = std::env::var("SCALE").ok().and_then(|s| s.parse().ok()).unwrap_or(1).clamp(1, 16); + let (img, w, h) = if k > 1 { + let mut big = vec![0u8; w * 3 * k * h * k * 4]; + for y in 0..h * k { + for x in 0..w * 3 * k { + let (s, d) = (((y / k) * w * 3 + x / k) * 4, (y * w * 3 * k + x) * 4); + big[d..d + 4].copy_from_slice(&img[s..s + 4]); + } + } + (big, w * k, h * k) + } else { + (img, w, h) + }; + let image = + photocraft_codecs::Image::from_raw((w * 3) as u32, h as u32, photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8, img).unwrap(); + std::fs::write(&out, photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); + println!("wrote {out}"); +} + +fn configure_visibility(layers: &mut [photocraft_doc::Layer]) { if std::env::var_os("HIDE_ADJ").is_some() { // Composite without adjustment layers (their input, for fitting transfer curves). fn hide(ls: &mut [photocraft_doc::Layer]) { @@ -37,18 +272,19 @@ fn main() { } } } - hide(&mut imp.document.layers); + hide(layers); } if let Some(keep) = std::env::var_os("ONLY") { // Hide every top-level layer but the bottom one and those named in ONLY (comma-separated). let keep = keep.to_string_lossy().to_string(); let names: Vec<&str> = keep.split(',').collect(); - for l in imp.document.layers.iter_mut().skip(1) { + for l in layers.iter_mut().skip(1) { l.visible = names.contains(&l.name.as_str()); } } - let doc = &imp.document; - println!("mode {:?} depth {:?} layers {} warnings {:?} light {:?}", doc.mode, doc.depth, doc.layer_count(), imp.warnings, doc.global_light); +} + +fn print_layer_details(doc: &photocraft_doc::Document) { for l in doc.walk() { let l = l.2; println!( @@ -184,6 +420,9 @@ fn main() { } } } +} + +fn dump_filter_effects(file: &photocraft_psd::PsdFile) { if std::env::var_os("DUMP_FX").is_some() { fn walk(d: &photocraft_psd::descriptor::Descriptor, depth: usize) { for (k, v) in &d.items { @@ -219,225 +458,9 @@ fn main() { } } } - let ours = photocraft_compose::flatten(doc).px; - let merged = photocraft_io::merged_composite(&file).unwrap(); - if std::env::args().nth(3).as_deref() == Some("png") { - // ours | photoshop | diff heatmap, side by side (over white). - let out = std::env::args().nth(4).expect("out.png"); - let (w, h) = (doc.size.width as usize, doc.size.height as usize); - let mut img = vec![0u8; w * 3 * h * 4]; - let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - for y in 0..h { - for x in 0..w { - let (a, b) = (ours[y * w + x], merged[y * w + x]); - let over = |p: [f32; 4]| [p[0] * p[3] + 1.0 - p[3], p[1] * p[3] + 1.0 - p[3], p[2] * p[3] + 1.0 - p[3]]; - let (oa, ob) = (over(a), over(b)); - let d = (0..3).map(|c| (oa[c] - ob[c]).abs()).fold(0.0f32, f32::max); - let heat = [q((d * 4.0).min(1.0)), q((1.0 - d * 4.0).max(0.0) * 0.3), 0]; - for (k, px) in [[q(oa[0]), q(oa[1]), q(oa[2])], [q(ob[0]), q(ob[1]), q(ob[2])], heat].iter().enumerate() { - let o = (y * w * 3 + k * w + x) * 4; - img[o..o + 3].copy_from_slice(px); - img[o + 3] = 255; - } - } - } - // SCALE=n: nearest-neighbour upscale (small files). - let k: usize = std::env::var("SCALE").ok().and_then(|s| s.parse().ok()).unwrap_or(1).clamp(1, 16); - let (img, w, h) = if k > 1 { - let mut big = vec![0u8; w * 3 * k * h * k * 4]; - for y in 0..h * k { - for x in 0..w * 3 * k { - let (s, d) = (((y / k) * w * 3 + x / k) * 4, (y * w * 3 * k + x) * 4); - big[d..d + 4].copy_from_slice(&img[s..s + 4]); - } - } - (big, w * k, h * k) - } else { - (img, w, h) - }; - let image = - photocraft_codecs::Image::from_raw((w * 3) as u32, h as u32, photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8, img) - .unwrap(); - std::fs::write(&out, photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap()).unwrap(); - println!("wrote {out}"); - return; - } - if std::env::args().nth(3).as_deref() == Some("dump") { - // dump prefix: raw little-endian f32 RGBA (straight) planes for offline fitting: - // prefix_ours.f32, prefix_ps.f32, prefix_L.f32 (each top-level layer's own pixels, - // masks not applied) and prefix.txt (width, height, layer names). - let out = std::env::args().nth(4).expect("prefix"); - let wr = |name: String, px: &[[f32; 4]]| { - let bytes: Vec = px.iter().flat_map(|p| p.iter().flat_map(|v| v.to_le_bytes())).collect(); - std::fs::write(name, bytes).unwrap(); - }; - wr(format!("{out}_ours.f32"), &ours); - wr(format!("{out}_ps.f32"), &merged); - let mut meta = format!("{} {}\n", doc.size.width, doc.size.height); - for (i, l) in doc.layers.iter().enumerate() { - let b = photocraft_compose::surface_to_buffer( - l.surface().unwrap_or(&photocraft_raster::Surface::new(photocraft_color::PixelFormat::RGBA8)), - doc.bounds(), - ); - wr(format!("{out}_L{i}.f32"), &b.px); - meta += &format!("{i} {}\n", l.name); - } - std::fs::write(format!("{out}.txt"), meta).unwrap(); - return; - } - if std::env::args().nth(3).as_deref() == Some("layerpx") { - // layerpx x y: each layer's own pixel (0-255). - let x: i32 = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(0); - let y: i32 = std::env::args().nth(5).and_then(|s| s.parse().ok()).unwrap_or(0); - for l in doc.walk() { - if let Some(s) = l.2.surface() { - println!("{:?}: {:?}", l.2.name, s.pixel(x, y).iter().map(|v| (v * 255.0 * 100.0).round() / 100.0).collect::>()); - } - } - return; - } - if std::env::args().nth(3).as_deref() == Some("grid") { - // grid x0 y0 x1 y1 [channel]: ours / photoshop values (0-255, premultiplied colour). - let w = doc.size.width as usize; - let a: Vec = (4..8).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); - let ch: usize = std::env::args().nth(8).and_then(|s| s.parse().ok()).unwrap_or(3); - for (label, img) in [("ours", &ours), ("ps", &merged)] { - println!("{label} (channel {ch}):"); - for y in a[1]..a[3] { - let row: Vec = (a[0]..a[2]) - .map(|x| { - let p = img[y * w + x]; - let v = if ch == 3 { p[3] } else { p[ch] * p[3] }; - format!("{:4}", (v * 255.0).round()) - }) - .collect(); - println!(" y={y:4} {}", row.join("")); - } - } - return; - } - if std::env::args().nth(3).as_deref() == Some("bylayer") { - // Max error and bad-pixel count inside each layer's bounds grown by its effects' reach - // (regions overlap; a quick way to tell which layer or effect is off). - let (w, h) = (doc.size.width as i32, doc.size.height as i32); - for (_, _, l) in doc.walk() { - let Some(sf) = l.surface() else { continue }; - let cb = sf.content_bounds(); - let mut b = photocraft_geom::Rect::new(i32::MAX, i32::MAX, i32::MIN, i32::MIN); - for y in cb.y0..cb.y1 { - for x in cb.x0..cb.x1 { - let p = sf.pixel(x, y); - if p[p.len() - 1] > 0.0 { - b = photocraft_geom::Rect::new(b.x0.min(x), b.y0.min(y), b.x1.max(x + 1), b.y1.max(y + 1)); - } - } - } - if b.x0 > b.x1 { - continue; - } - let m = photocraft_compose::effects::margin(l); - let r = photocraft_geom::Rect::new((b.x0 - m).max(0), (b.y0 - m).max(0), (b.x1 + m).min(w), (b.y1 + m).min(h)); - let (mut worst, mut at, mut bad) = (0.0f32, (0, 0), 0usize); - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let i = (y * w + x) as usize; - let (a, b) = (ours[i], merged[i]); - let d = (0..3).map(|c| (a[c] * a[3] - b[c] * b[3]).abs()).fold((a[3] - b[3]).abs(), f32::max); - if d > 2.0 / 255.0 { - bad += 1; - } - if d > worst { - (worst, at) = (d, (x, y)); - } - } - } - let fx: Vec<&str> = l.effects.items.iter().filter(|e| e.enabled()).map(|e| e.label()).collect(); - println!("{:<32} {:>6.1} at {:?} bad {:>6} {:?} {:?}", l.name, worst * 255.0, at, bad, l.blend, fx); - } - return; - } - if std::env::args().nth(3).as_deref() == Some("worst") { - // The N worst pixels (premultiplied max channel error), with coordinates. - let w = doc.size.width as usize; - let n: usize = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(20); - let mut v: Vec<(f32, usize)> = ours - .iter() - .zip(&merged) - .enumerate() - .map(|(i, (a, b))| ((0..4).map(|c| (a[c] * a[3] - b[c] * b[3]).abs()).fold((a[3] - b[3]).abs(), f32::max), i)) - .collect(); - v.sort_by(|a, b| b.0.total_cmp(&a.0)); - for (d, i) in v.into_iter().take(n) { - let (a, b) = (ours[i], merged[i]); - println!("({:4},{:4}) d={:5.1} ours {:?} ps {:?}", i % w, i / w, d * 255.0, a.map(|v| (v * 255.0).round()), b.map(|v| (v * 255.0).round())); - } - return; - } - if std::env::args().nth(3).as_deref() == Some("row") { - let w = doc.size.width as usize; - let y: usize = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(0); - let (x0, x1): (usize, usize) = - (std::env::args().nth(5).and_then(|s| s.parse().ok()).unwrap_or(0), std::env::args().nth(6).and_then(|s| s.parse().ok()).unwrap_or(w)); - for x in (x0..x1).step_by(((x1 - x0) / 20).max(1)) { - let (a, b) = (ours[y * w + x], merged[y * w + x]); - println!("x={x:5} ours {:?} ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); - } - return; - } - if std::env::args().nth(3).as_deref() == Some("solve") { - // solve layer-name x0 y0 x1 y1: per pixel the layer's content alpha `l`, its shape's path - // coverage `cov`, Photoshop's alpha `A` and the alpha `s` an effect beneath the layer - // would need (A = s + l (1 - s)) over a transparent backdrop. - let name = std::env::args().nth(4).expect("layer name"); - let a: Vec = (5..9).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); - let w = doc.size.width as usize; - let l = doc.walk().into_iter().map(|t| t.2).find(|l| l.name == name).expect("layer"); - let r = photocraft_geom::Rect::new(a[0], a[1], a[2], a[3]); - let cov = match &l.content { - photocraft_doc::LayerContent::Shape(sh) => photocraft_vector::path_coverage(&sh.path, r), - _ => vec![0.0; r.width() as usize * r.height() as usize], - }; - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let la = l.surface().map_or(0.0, |s| s.pixel(x, y)[3]); - let c = cov[((y - r.y0) * r.width() as i32 + x - r.x0) as usize]; - let p = merged[y as usize * w + x as usize]; - let o = ours[y as usize * w + x as usize]; - let s = if la < 1.0 { (p[3] - la) / (1.0 - la) } else { f32::NAN }; - println!("({x:3},{y:3}) l {la:.3} cov {c:.3} A {:.3} ours {:.3} s {s:.3} ps {:?}", p[3], o[3], p.map(|v| (v * 255.0).round())); - } - } - return; - } - if std::env::args().nth(3).as_deref() == Some("line") { - // line x0 y0 x1 y1: every pixel on the segment, straight RGBA 0-255, ours | ps. - let w = doc.size.width as usize; - let a: Vec = (4..8).map(|i| std::env::args().nth(i).and_then(|s| s.parse().ok()).unwrap_or(0)).collect(); - let n = (a[2] - a[0]).abs().max((a[3] - a[1]).abs()).max(1); - for k in 0..=n { - let (x, y) = ((a[0] + (a[2] - a[0]) * k / n) as usize, (a[1] + (a[3] - a[1]) * k / n) as usize); - let (o, p) = (ours[y * w + x], merged[y * w + x]); - println!("({x:4},{y:4}) ours {:?} ps {:?}", o.map(|v| (v * 255.0).round()), p.map(|v| (v * 255.0).round())); - } - return; - } - if std::env::args().nth(3).as_deref() == Some("col") { - let (w, h) = (doc.size.width as usize, doc.size.height as usize); - let x = std::env::args().nth(4).and_then(|s| s.parse().ok()).unwrap_or(w / 2); - for k in 0..=16 { - let y = ((h - 1) * k / 16).min(h - 1); - let (a, b) = (ours[y * w + x], merged[y * w + x]); - println!("y={y:5} ours {:?} ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); - } - return; - } - let n = std::env::args().nth(2).and_then(|s| s.parse().ok()).unwrap_or(8usize); - for (i, (a, b)) in ours.iter().zip(&merged).enumerate().take(n) { - println!("{i:4}: ours {:?}\n ps {:?}", a.map(|v| (v * 1000.0).round() / 1000.0), b.map(|v| (v * 1000.0).round() / 1000.0)); - } } -/// The corpus oracle table (as `tests/corpus.rs`), files in parallel, without a test build. +/// The corpus oracle table (as `tests/it/corpus.rs`), files in parallel, without a test build. fn corpus_summary(root: &std::path::Path) { fn collect(dir: &std::path::Path, out: &mut Vec) { let Ok(rd) = std::fs::read_dir(dir) else { return }; @@ -467,7 +490,7 @@ fn corpus_summary(root: &std::path::Path) { let ours = photocraft_compose::flatten(&imp.document).px; let mut m = 0.0f32; let mut bad = 0usize; - for (a, b) in ours.iter().zip(&merged) { + for (a, b) in ours.iter().zip(merged) { let d = (0..4).map(|c| (a[c] * a[3] - b[c] * b[3]).abs()).fold((a[3] - b[3]).abs(), f32::max); m = m.max(d); if (0..4).any(|c| (a[c] * a[3] - b[c] * b[3]).abs() > TOL) { diff --git a/crates/io/examples/psd_open_mem.rs b/crates/io/examples/psd_open_mem.rs new file mode 100644 index 0000000000..903eb35af9 --- /dev/null +++ b/crates/io/examples/psd_open_mem.rs @@ -0,0 +1,56 @@ +//! Opens a file the way the app's background open does and reports the time. Run it under +//! `/usr/bin/time -l` for the peak memory. `--read` reads the whole file first and imports the +//! bytes, as opens did before `import_file` (to compare). +//! +//! `cargo run --release -p photocraft-io --example psd_open_mem -- [--read]` + +fn main() { + let Some(path) = std::env::args().nth(1).filter(|a| a != "--read") else { + eprintln!("usage: psd_open_mem "); + std::process::exit(2); + }; + if std::env::args().any(|a| a == "--read") { + read_then_import(&path); + return; + } + let t = std::time::Instant::now(); + let res = + match photocraft_io::import_file(&path, std::path::Path::new(&path), &photocraft_io::ImportOptions::default(), &photocraft_raster::Interrupt::NONE) { + Ok(r) => r, + Err(e) => { + eprintln!("{path}: open failed: {e}"); + std::process::exit(1); + } + }; + report(&path, &format!("open {:.2}s", t.elapsed().as_secs_f64()), &res); +} + +fn read_then_import(path: &str) { + let t = std::time::Instant::now(); + let bytes = match photocraft_format::read_file(std::path::Path::new(path)) { + Ok(b) => b, + Err(e) => { + eprintln!("{path}: read failed: {e}"); + std::process::exit(1); + } + }; + let read_s = t.elapsed().as_secs_f64(); + let t = std::time::Instant::now(); + let res = match photocraft_io::import_with(path, &bytes, &photocraft_raster::Interrupt::NONE) { + Ok(r) => r, + Err(e) => { + eprintln!("{path}: open failed: {e}"); + std::process::exit(1); + } + }; + drop(bytes); + report(path, &format!("read {read_s:.2}s, import {:.2}s", t.elapsed().as_secs_f64()), &res); +} + +fn report(path: &str, times: &str, res: &photocraft_io::ImportResult) { + let doc = &res.document; + println!("{path}: {times}, {} layers ({} top level)", doc.walk().len(), doc.layers.len()); + for w in &res.warnings { + println!(" warning: {w}"); + } +} diff --git a/crates/io/src/abr_map.rs b/crates/io/src/abr_map.rs index 549ac40970..a41ba3ac01 100644 --- a/crates/io/src/abr_map.rs +++ b/crates/io/src/abr_map.rs @@ -57,7 +57,7 @@ pub fn map_file(f: &AbrFile, group: &str) -> AbrImport { /// [`map_file`] that checks `ctl` before each brush. pub fn map_file_with(f: &AbrFile, group: &str, ctl: &photocraft_raster::Interrupt) -> Result { let mut out = AbrImport { version: f.version, warnings: f.warnings.clone(), ..Default::default() }; - let mut m = Mapper { file: f, warnings: BTreeSet::new(), unknown: BTreeSet::new() }; + let mut m = Mapper { file: f, warnings: BTreeSet::new(), unknown: BTreeSet::new(), tips: Default::default() }; let total = f.legacy.len().max(f.samples.len()).max(f.presets.len()).max(1) as f32; let step = |i: usize| -> Result<(), photocraft_raster::Cancelled> { ctl.check()?; @@ -255,18 +255,26 @@ struct Mapper<'a> { file: &'a AbrFile, warnings: BTreeSet, unknown: BTreeSet, + /// Tips already converted, by sample: presets can name one sample thousands of times, and a + /// tile clones by sharing its data. + tips: std::collections::HashMap<*const AbrSample, (photocraft_paint::tile::GrayTile, f32)>, } impl Mapper<'_> { /// A sampled tip (downsampled to [`MAX_TIP_EDGE`]) and its size (larger side). fn tip(&mut self, s: &AbrSample) -> (TipShape, f32) { + if let Some((tile, size)) = self.tips.get(&std::ptr::from_ref(s)) { + return (TipShape::Sampled(tile.clone()), *size); + } let v = s.to_unit(); let (w, h, v) = fit(s.width, s.height, v, MAX_TIP_EDGE); if (w, h) != (s.width, s.height) { self.warnings.insert(format!("tips larger than {MAX_TIP_EDGE} px were downsampled")); } let data = v.iter().map(|x| (x.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16).collect(); - (TipShape::Sampled(GrayTile { width: w, height: h, data }), s.width.max(s.height) as f32) + let (tile, size) = (GrayTile { width: w, height: h, data }, s.width.max(s.height) as f32); + self.tips.insert(std::ptr::from_ref(s), (tile.clone(), size)); + (TipShape::Sampled(tile), size) } fn dynamic(&mut self, d: Option<&Descriptor>) -> Dynamic { @@ -473,7 +481,8 @@ impl Mapper<'_> { tilt_y: (num(d, "brushPoseTiltY").unwrap_or(0.0) * 0.9).clamp(-90.0, 90.0) as f32, rotation: num(d, "brushPoseAngle").unwrap_or(0.0).rem_euclid(360.0) as f32, pressure: pct(d, "brushPosePressure").unwrap_or(1.0).clamp(0.0, 1.0), - override_tilt: boolean(d, "overridePoseTiltX").unwrap_or(false) || boolean(d, "overridePoseTiltY").unwrap_or(false), + override_tilt_x: boolean(d, "overridePoseTiltX").unwrap_or(false), + override_tilt_y: boolean(d, "overridePoseTiltY").unwrap_or(false), override_rotation: boolean(d, "overridePoseAngle").unwrap_or(false), override_pressure: boolean(d, "overridePosePressure").unwrap_or(false), }; diff --git a/crates/io/src/abr_map/tests.rs b/crates/io/src/abr_map/tests.rs index 501e331af9..26f68d7aa0 100644 --- a/crates/io/src/abr_map/tests.rs +++ b/crates/io/src/abr_map/tests.rs @@ -128,6 +128,43 @@ fn grit_pattern() -> PsdPattern { } } +#[test] +fn pose_axes_abr_import_preserves_independent_stylus_tilt() { + let input = photocraft_paint::StrokePoint { tilt_x: 13.0, tilt_y: -27.0, pressure: 0.71, rotation: 67.0, wheel: 0.24, time: 73.0, ..Default::default() }; + for subversion in [1, 2] { + for (x, y) in [(false, false), (true, false), (false, true), (true, true)] { + let preset = Descriptor::new("brushPreset") + .with("Brsh", Value::Descriptor(Descriptor::new("computedBrush").with("Dmtr", u(b"#Pxl", 47.0)))) + .with("useBrushPose", Value::Boolean(true)) + .with("brushPoseTiltX", prc(50.0)) + .with("brushPoseTiltY", prc(-20.0)) + .with("overridePoseTiltX", Value::Boolean(x)) + .with("overridePoseTiltY", Value::Boolean(y)); + let bytes = write_v6(subversion, &[], &[], &[preset], false).unwrap(); + let imported = read_abr(&bytes, "Pose axes").unwrap(); + assert_eq!(imported.presets.len(), 1); + let b = &imported.presets[0].brush; + assert!(b.pose.enabled); + assert_eq!((b.pose.tilt_x, b.pose.tilt_y), (45.0, -18.0), "ABR percentages retain the W3C degree conversion"); + let mut expected = input; + if x { + expected.tilt_x = 45.0; + } + if y { + expected.tilt_y = -18.0; + } + assert_eq!( + photocraft_paint::dynamics::apply_pose(b, input), + expected, + "v6.{subversion}, X={x}, Y={y}: one axis must not override the other's tablet data" + ); + let stored = serde_json::to_vec(&imported.presets[0]).unwrap(); + let restored: photocraft_paint::BrushPreset = serde_json::from_slice(&stored).unwrap(); + assert_eq!(photocraft_paint::dynamics::apply_pose(&restored.brush, input), expected, "preset persistence retains the independent flags"); + } + } +} + #[test] fn v6_settings_map_onto_brush_sections() { for sub in [1, 2] { @@ -166,7 +203,7 @@ fn v6_settings_map_onto_brush_sections() { assert!(b.transfer.enabled); assert_eq!((b.transfer.opacity.control, b.transfer.opacity.minimum), (Control::PenPressure, 0.1)); assert_eq!((b.transfer.flow.control, b.transfer.flow.jitter), (Control::Fade, 0.15)); - assert!(b.pose.enabled && b.pose.override_rotation && !b.pose.override_tilt); + assert!(b.pose.enabled && b.pose.override_rotation && !b.pose.override_tilt_x && !b.pose.override_tilt_y); assert_eq!((b.pose.tilt_x, b.pose.rotation), (45.0, 90.0)); assert!(b.wet_edges && b.noise && b.build_up); assert_eq!((b.opacity, b.flow, b.smoothing.amount, b.pressure_size), (0.8, 0.6, 0.4, true)); diff --git a/crates/io/src/adjust_map.rs b/crates/io/src/adjust_map.rs index d4bc312690..463c882efb 100644 --- a/crates/io/src/adjust_map.rs +++ b/crates/io/src/adjust_map.rs @@ -11,9 +11,11 @@ //! stay [`Adjustment::Unsupported`] and are written back verbatim. use photocraft_doc::Adjustment; -use photocraft_doc::adjust::{CURVE_MAP_LEN, CurvePoint, HueRange, LevelsChannel, ToneSpace, curve_map, curve_map_outputs}; +use photocraft_doc::adjust::{CURVE_MAP_LEN, CurvePoint, HueRange, LevelsChannel, LutFormat, LutSource, ToneSpace, curve_map}; use photocraft_psd::descriptor::{Descriptor, Value, VersionedDescriptor}; +mod encode; + /// All PSD adjustment keys recognized as adjustment layers. pub const ADJUSTMENT_KEYS: [&[u8; 4]; 16] = [b"levl", b"curv", b"hue2", b"brit", b"nvrt", b"thrs", b"post", b"expA", b"vibA", b"blnc", b"mixr", b"grdm", b"phfl", b"selc", b"blwh", b"clrL"]; @@ -29,7 +31,7 @@ fn bef32(d: &[u8], at: usize) -> Option { } fn unsupported(key: &[u8; 4], data: &[u8]) -> Adjustment { - Adjustment::Unsupported { psd_key: String::from_utf8_lossy(key).into_owned(), raw: data.to_vec() } + Adjustment::Unsupported { psd_key: String::from_utf8_lossy(key).into_owned(), raw: data.into() } } fn levels_rec(d: &[u8], at: usize) -> Option { @@ -139,6 +141,19 @@ pub fn label(key: &[u8; 4]) -> &'static str { } } +/// Keys of a Brightness/Contrast `CgEd` descriptor: the ones [`write`] emits plus Photoshop's +/// preset bookkeeping. +const BRIT_CGED_KEYS: [&str; 9] = ["Vrsn", "Brgh", "Cntr", "means", "Lab ", "useLegacy", "Auto", "presetKind", "presetFileName"]; + +/// Whether a `brit` layer's `CgEd` descriptor holds settings Brightness/Contrast has no place +/// for. Photoshop 2026's Light adjustment (Exposure, Contrast, Highlights, Shadows, Whites, +/// Blacks) is stored under the `brit` key; read as Brightness/Contrast it kept only Contrast and +/// lost the rest without a word (#6542), so such a layer stays [`Adjustment::Unsupported`]. +pub fn is_extended_brit(cged: Option<&[u8]>) -> bool { + cged.and_then(|c| VersionedDescriptor::parse_prefix(c).ok()) + .is_some_and(|(v, _)| v.descriptor.items.iter().any(|(k, _)| !BRIT_CGED_KEYS.iter().any(|n| k.is(n)))) +} + /// Why [`parse`] keeps a block as [`Adjustment::Unsupported`], when there is more to say than /// "it could not be read" (so far: Color Lookup). pub fn unreadable_reason(key: &[u8; 4], data: &[u8]) -> Option { @@ -204,6 +219,8 @@ fn parse_any(key: &[u8; 4], data: &[u8], cged: Option<&[u8]>) -> Adjustment { b"nvrt" => Some(Adjustment::Invert), b"thrs" => be16(data, 0).map(|v| Adjustment::Threshold { level: f32::from(v) / 255.0 }), b"post" => be16(data, 0).map(|v| Adjustment::Posterize { levels: u32::from(v) }), + // Kept raw (with its `CgEd`, which the PSD writer keeps while the layer is unchanged). + b"brit" if is_extended_brit(cged) => None, b"brit" => { let modern = cged.and_then(|c| VersionedDescriptor::parse_prefix(c).ok()).and_then(|(v, _)| { let d = v.descriptor; @@ -228,8 +245,10 @@ fn parse_any(key: &[u8; 4], data: &[u8], cged: Option<&[u8]>) -> Adjustment { *r = HueRange { hue: h, saturation: s, lightness: l, bounds: HueRange::canonical_bounds([a, b, c, d]) }; } } + let hue = f32::from(bei16(data, base)?); Some(Adjustment::HueSaturation { - hue: f32::from(bei16(data, base)?), + // Photoshop stores the colorize hue signed (200 as -160); the model keeps 0..360. + hue: if colorize { hue.rem_euclid(360.0) } else { hue }, saturation: f32::from(bei16(data, base + 2)?), lightness: f32::from(bei16(data, base + 4)?), colorize, @@ -328,6 +347,22 @@ pub(crate) fn retarget_gradient_map(data: &mut [u8], target: &Adjustment, channe } } +/// `true` for a `grdm` block exactly as older PhotoCraft builds wrote it: [`write_gradient_map`]'s +/// output without the two padding bytes, which makes Photoshop refuse the file (#8070). Blocks +/// from other writers, whatever their length, are not matched. +pub(crate) fn is_unpadded_photocraft_gradient_map(data: &[u8]) -> bool { + if data.len().is_multiple_of(4) { + return false; + } + match parse_gradient_map(data) { + Some(Adjustment::GradientMap { stops, reverse, dither }) => { + let padded = write_gradient_map(&stops, reverse, dither); + padded.len() == data.len() + 2 && padded.starts_with(data) + } + _ => false, + } +} + /// `grdm` version 1 for [`Adjustment::GradientMap`] (Classic interpolation, Smoothness 0, so /// the stops are reproduced exactly). fn write_gradient_map(stops: &[(f32, [f32; 3])], reverse: bool, dither: bool) -> Vec { @@ -370,6 +405,8 @@ fn write_gradient_map(stops: &[(f32, [f32; 3])], reverse: bool, dither: bool) -> put16(&mut v, x); } put16(&mut v, 0); + // Photoshop includes the two-byte pad in the `grdm` length when the block is 2 mod 4 (#8070). + v.resize(v.len().next_multiple_of(4), 0); v } @@ -424,12 +461,18 @@ fn read_lookup(d: &[u8]) -> Result { Some(_) => return Err("its LUT data is empty".into()), None => return Err("it uses a colour profile (abstract or device link), not a LUT file, which isn't supported yet".into()), }; - let (ext, format) = match desc_enum(&desc, "LUTFormat") { - Some(b"LUTFormat3DL") => ("x.3dl", "3DL"), - Some(b"LUTFormatLOOK") => ("x.look", "LOOK"), - _ => ("x.cube", "CUBE"), + let format = match desc_enum(&desc, "LUTFormat") { + Some(b"LUTFormat3DL") => LutFormat::ThreeDl, + Some(b"LUTFormatLOOK") => LutFormat::Look, + _ => LutFormat::Cube, }; - let lut = photocraft_cms::lutfile::parse(ext, bytes).map_err(|e| format!("its {format} LUT can't be read ({e})"))?; + let lut = parse_lut_source(format, bytes).map_err(|e| format!("its {} LUT can't be read ({e})", psd_lut_format(format).trim_start_matches("LUTFormat")))?; + if !lut.domain_is_default() { + return Err(format!( + "its LUT has a non-default input domain (DOMAIN_MIN {:?}, DOMAIN_MAX {:?}), which is not supported yet", + lut.domain_min, lut.domain_max + )); + } let name = desc_text(&desc, "LUT3DFileName").or_else(|| desc_text(&desc, "NM ")).unwrap_or_default(); Ok(Adjustment::ColorLookup { name, @@ -437,9 +480,47 @@ fn read_lookup(d: &[u8]) -> Result { lut: Some(std::sync::Arc::new(lut.data)), tetrahedral: false, dither: desc_bool(&desc, "Dthr").unwrap_or(false), + // Kept so a save embeds the file as loaded (Photoshop never converts it). + source: Some(LutSource { format, bytes: std::sync::Arc::new(bytes.clone()) }), }) } +/// A LUT file's table, read the way a PSD read does (an XML file tagged `.cube` is sniffed as `.look`). +fn parse_lut_source(format: LutFormat, bytes: &[u8]) -> Result { + let format = match format { + LutFormat::Cube => photocraft_cms::lutfile::Format::Cube, + LutFormat::ThreeDl => photocraft_cms::lutfile::Format::ThreeDl, + LutFormat::Look => photocraft_cms::lutfile::Format::Look, + }; + photocraft_cms::lutfile::parse(&format!("x.{}", format.extension()), bytes) +} + +/// Photoshop's `LUTFormatType` value for a format. +fn psd_lut_format(format: LutFormat) -> &'static str { + match format { + LutFormat::Cube => "LUTFormatCUBE", + LutFormat::ThreeDl => "LUTFormat3DL", + LutFormat::Look => "LUTFormatLOOK", + } +} + +/// The source file of a Color Lookup to embed as is: only while it still parses to the +/// layer's table, so an edited table is never saved with stale bytes. +fn lut_source_for<'a>(source: Option<&'a LutSource>, table: &[f32], size: usize) -> Option<&'a LutSource> { + let source = source?; + let file = parse_lut_source(source.format, &source.bytes).ok()?; + (file.size == size && file.data == table).then_some(source) +} + +/// `name` with a `.3dl` / `.look` extension swapped for `.cube`, for a table saved as `.cube`. +fn cube_file_name(name: &str) -> String { + let lower = name.to_ascii_lowercase(); + match [".3dl", ".look"].iter().find(|ext| lower.ends_with(*ext)) { + Some(ext) => format!("{}.cube", name.get(..name.len() - ext.len()).unwrap_or(name)), + None => name.to_string(), + } +} + /// The descriptor of a `vibA` / `blwh` block: a 4-byte descriptor version (16), then the descriptor. fn versioned(d: &[u8]) -> Option { VersionedDescriptor::parse_prefix(d).ok().map(|(v, _)| v.descriptor) @@ -589,205 +670,42 @@ fn levels_write(v: &mut Vec, c: &LevelsChannel) { /// (Brightness/Contrast also writes a `CgEd` descriptor). Every evaluated kind has an /// encoding; the result is empty only for an [`Adjustment::Unsupported`] with a malformed key. pub fn write(adj: &Adjustment) -> Vec<([u8; 4], Vec)> { - let mut v = Vec::new(); + let block = |key: &[u8; 4], data: Vec| vec![(*key, data)]; match adj { - Adjustment::Invert => return vec![(*b"nvrt", Vec::new())], - Adjustment::Threshold { level } => { - put16(&mut v, q255(*level).max(1)); - put16(&mut v, 0); - return vec![(*b"thrs", v)]; - } - Adjustment::Posterize { levels } => { - put16(&mut v, (*levels).clamp(2, 255) as u16); - put16(&mut v, 0); - return vec![(*b"post", v)]; - } - Adjustment::BrightnessContrast { brightness, contrast, legacy } => { - v.extend_from_slice(&clamp_i16(*brightness).to_be_bytes()); - v.extend_from_slice(&clamp_i16(*contrast).to_be_bytes()); - put16(&mut v, 127); - v.push(0); - let d = Descriptor::new("null") - .with("Vrsn", Value::Integer(1)) - .with("Brgh", Value::Integer(brightness.round() as i32)) - .with("Cntr", Value::Integer(contrast.round() as i32)) - .with("means", Value::Integer(127)) - .with("Lab ", Value::Boolean(false)) - .with("useLegacy", Value::Boolean(*legacy)) - .with("Auto", Value::Boolean(false)); - return vec![(*b"brit", v), (*b"CgEd", VersionedDescriptor::new(d).to_bytes())]; - } + Adjustment::Invert => return block(b"nvrt", Vec::new()), + Adjustment::Threshold { level } => return block(b"thrs", encode::words(&[q255(*level).max(1), 0])), + Adjustment::Posterize { levels } => return block(b"post", encode::words(&[(*levels).clamp(2, 255) as u16, 0])), + Adjustment::BrightnessContrast { brightness, contrast, legacy } => return encode::brightness_contrast(*brightness, *contrast, *legacy), Adjustment::HueSaturation { hue, saturation, lightness, colorize, ranges } => { - put16(&mut v, 2); - v.push(u8::from(*colorize)); - v.push(0); - for _ in 0..2 { - for x in [hue, saturation, lightness] { - v.extend_from_slice(&clamp_i16(*x).to_be_bytes()); - } - } - // Six hue ranges (reds, yellows, greens, cyans, blues, magentas): range values in - // 0..360, then the range's hue, saturation and lightness. - for r in ranges { - for x in r.bounds { - put16(&mut v, x.rem_euclid(360.0).round() as u16 % 360); - } - for x in [r.hue, r.saturation, r.lightness] { - v.extend_from_slice(&clamp_i16(x).to_be_bytes()); - } - } - return vec![(*b"hue2", v)]; - } - Adjustment::Exposure { exposure, offset, gamma } => { - put16(&mut v, 1); - for x in [exposure, offset, gamma] { - v.extend_from_slice(&x.to_be_bytes()); - } - v.push(1); // color space flag (spec: "1 byte") - return vec![(*b"expA", v)]; - } - Adjustment::Levels { master, per_channel, space, black } => { - put16(&mut v, 2); - levels_write(&mut v, master); - for c in per_channel { - levels_write(&mut v, c); - } - let ident = LevelsChannel::default(); - levels_write(&mut v, if *space == ToneSpace::Cmyk { black } else { &ident }); - for _ in 5..29 { - levels_write(&mut v, &LevelsChannel::default()); - } - return vec![(*b"levl", v)]; + return block(b"hue2", encode::hue_saturation([*hue, *saturation, *lightness], *colorize, ranges)); } + Adjustment::Exposure { exposure, offset, gamma } => return block(b"expA", encode::exposure(*exposure, *offset, *gamma)), + Adjustment::Levels { master, per_channel, space, black } => return block(b"levl", encode::levels(master, per_channel, *space, black)), Adjustment::Curves { master, per_channel, space, black, pencil } => { - v.push(u8::from(*pencil)); - put16(&mut v, 1); - let ink = *space == ToneSpace::Cmyk && black.len() >= 2; - v.extend_from_slice(&(if ink { 0b11111u32 } else { 0b1111 }).to_be_bytes()); - for c in std::iter::once(master).chain(per_channel.iter()).chain(ink.then_some(black)) { - if *pencil { - v.extend(curve_map_outputs(c).iter().map(|&o| u8::try_from(q255(o)).unwrap_or(u8::MAX))); - continue; - } - put16(&mut v, c.len().min(19) as u16); - for p in c.iter().take(19) { - put16(&mut v, q255(p.output)); - put16(&mut v, q255(p.input)); - } - } - return vec![(*b"curv", v)]; + return block(b"curv", encode::curves(master, per_channel, *space, black, *pencil)); } - Adjustment::SelectiveColor { relative, adjustments } => { - put16(&mut v, 1); - put16(&mut v, u16::from(!*relative)); - v.extend_from_slice(&[0; 8]); - for rec in adjustments { - for x in rec { - v.extend_from_slice(&clamp_i16(x.clamp(-100.0, 100.0)).to_be_bytes()); - } - } - return vec![(*b"selc", v)]; - } - Adjustment::ColorLookup { name, lut, size, dither, .. } => { - // A lookup without a usable table (none chosen yet, or a bad size) renders as the - // identity, so it is written as a 2³ identity cube rather than dropped. - let n = *size as usize; - let cube = lut.as_deref().zip(photocraft_doc::adjust::lut3d_len(*size)).and_then(|(table, len)| table.get(..len)).filter(|_| n <= 256); - let (file, dither) = match cube { - Some(cube) => ( - photocraft_cms::lutfile::LutFile { title: String::new(), size: n, data: cube.to_vec(), domain_min: [0.0; 3], domain_max: [1.0; 3] }, - *dither, - ), - _ => (photocraft_cms::lutfile::LutFile { title: String::new(), ..photocraft_cms::lutfile::LutFile::identity(2) }, false), - }; - let en = - |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; - let text = |t: &str| Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul(t)); - let d = Descriptor::new("null") - .with("lookupType", en("colorLookupType", "3DLUT")) - .with("NM ", text(name)) - .with("Dthr", Value::Boolean(dither)) - .with("profile", Value::RawData(Vec::new())) - .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) - .with("dataOrder", en("colorLookupOrder", "rgbOrder")) - .with("tableOrder", en("colorLookupOrder", "bgrOrder")) - .with("LUT3DFileData", Value::RawData(photocraft_cms::lutfile::write_cube(&file).into_bytes())) - .with("LUT3DFileName", text(name)); - put16(&mut v, 1); - v.extend_from_slice(&VersionedDescriptor::new(d).to_bytes()); - return vec![(*b"clrL", v)]; + Adjustment::SelectiveColor { relative, adjustments } => return block(b"selc", encode::selective_color(*relative, adjustments)), + Adjustment::ColorLookup { name, lut, size, dither, source, .. } => { + return block(b"clrL", encode::color_lookup(name, lut.as_deref().map(Vec::as_slice), *size, *dither, source.as_ref())); } Adjustment::GradientMap { stops, reverse, dither } => { - // Photoshop needs two stops; fewer are written as the gradient they render as - // (none: black to white, i.e. the gray value itself; one: that colour throughout). - let stops = match stops.as_slice() { - [] => vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])], - [(_, c)] => vec![(0.0, *c), (1.0, *c)], - s => s.to_vec(), - }; - return vec![(*b"grdm", write_gradient_map(&stops, *reverse, *dither))]; - } - Adjustment::Vibrance { vibrance, saturation } => { - let d = Descriptor::new("null") - .with("vibrance", Value::Integer(i32::from(clamp_i16(vibrance.clamp(-100.0, 100.0))))) - .with("Strt", Value::Integer(i32::from(clamp_i16(saturation.clamp(-100.0, 100.0))))); - return vec![(*b"vibA", VersionedDescriptor::new(d).to_bytes())]; + return block(b"grdm", write_gradient_map(&encode::gradient_stops(stops), *reverse, *dither)); } + Adjustment::Vibrance { vibrance, saturation } => return block(b"vibA", encode::vibrance(*vibrance, *saturation)), Adjustment::ColorBalance { shadows, midtones, highlights, preserve_luminosity } => { - for x in shadows.iter().chain(midtones).chain(highlights) { - v.extend_from_slice(&clamp_i16(x.clamp(-100.0, 100.0)).to_be_bytes()); - } - v.push(u8::from(*preserve_luminosity)); - v.push(0); - return vec![(*b"blnc", v)]; - } - Adjustment::BlackWhite { weights, tint } => { - let mut d = Descriptor::new("null"); - for (w, k) in weights.iter().zip(BW_KEYS) { - d = d.with(k, Value::Integer(i32::from(clamp_i16(w.clamp(-200.0, 300.0))))); - } - let d = d - .with("useTint", Value::Boolean(tint.is_some())) - .with("tintColor", Value::Descriptor(rgb_desc(tint.unwrap_or(BW_TINT)))) - .with("bwPresetKind", Value::Integer(1)) - .with("blackAndWhitePresetFileName", Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul(""))); - return vec![(*b"blwh", VersionedDescriptor::new(d).to_bytes())]; + return block(b"blnc", encode::color_balance([shadows, midtones, highlights], *preserve_luminosity)); } + Adjustment::BlackWhite { weights, tint } => return block(b"blwh", encode::black_white(weights, *tint)), Adjustment::PhotoFilter { color, density, preserve_luminosity } => { - // Version 2: an RGB colour structure keeps the colour at 16 bits. - put16(&mut v, 2); - put16(&mut v, 0); - for x in color { - put16(&mut v, (x.clamp(0.0, 1.0) * 65535.0).round() as u16); - } - put16(&mut v, 0); - v.extend_from_slice(&((density.clamp(0.0, 1.0) * 100.0).round() as u32).to_be_bytes()); - v.push(u8::from(*preserve_luminosity)); - // Photoshop requires the 17-byte Photo Filter payload after the version to be - // padded to a 4-byte boundary INSIDE the tagged block's declared length. - // A single pad byte made an 18-byte `phfl` block that Photoshop could not open - // even though PhotoCraft and other tolerant readers accepted it (#1455). - v.extend_from_slice(&[0, 0, 0]); - return vec![(*b"phfl", v)]; + return block(b"phfl", encode::photo_filter(*color, *density, *preserve_luminosity)); } Adjustment::ChannelMixer { matrix, monochrome } => { - put16(&mut v, 1); - put16(&mut v, u16::from(*monochrome)); - // Red, green, blue, then the gray record (the first row, which monochrome uses). - for row in matrix.iter().chain(std::iter::once(&matrix[0])) { - let pct = |x: f32| clamp_i16((x * 100.0).clamp(-200.0, 200.0)).to_be_bytes(); - for x in &row[..3] { - v.extend_from_slice(&pct(*x)); - } - v.extend_from_slice(&[0, 0]); - v.extend_from_slice(&pct(row[3])); - } - return vec![(*b"mixr", v)]; + return block(b"mixr", encode::channel_mixer(matrix, *monochrome)); } Adjustment::Unsupported { psd_key, raw } => { let k = psd_key.as_bytes(); if k.len() == 4 { - return vec![([k[0], k[1], k[2], k[3]], raw.clone())]; + return vec![([k[0], k[1], k[2], k[3]], raw.to_vec())]; } } } @@ -796,552 +714,4 @@ pub fn write(adj: &Adjustment) -> Vec<([u8; 4], Vec)> { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn gradient_map_v3_methods_and_smoothness() { - let classic = vec![(0.0, [0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 0.0])]; - let v1 = write_gradient_map(&classic, false, false); - // Version 3 inserts the interpolation method after reverse/dither. - let v3 = |m: &[u8; 4]| { - let mut v = v1.clone(); - v[1] = 3; - v.splice(4..4, m.iter().copied()); - v - }; - let at_t = |data: &[u8], t: f32| match parse(b"grdm", data, None, Channels::Rgb) { - Adjustment::GradientMap { stops, .. } => { - let i = stops.windows(2).position(|w| w[0].0 <= t && t <= w[1].0).unwrap(); - let (x, y) = (stops[i], stops[i + 1]); - let u = (t - x.0) / (y.0 - x.0).max(1e-6); - std::array::from_fn::(|k| x.1[k] + (y.1[k] - x.1[k]) * u) - } - other => panic!("{other:?}"), - }; - let mid = |data: &[u8]| at_t(data, 0.5); - let c = mid(&v3(b"Gcls")); - assert!((c[0] - 0.5).abs() < 1e-3 && (c[2] - 0.5).abs() < 1e-3, "classic = sRGB lerp {c:?}"); - let p = mid(&v3(b"Perc")); - assert!((p[0] - c[0]).abs() > 0.02, "perceptual differs from classic {p:?}"); - // Smoothness 100 % (4096) bends a three-stop ramp. - let three = vec![(0.0, [0.0; 3]), (0.25, [1.0, 0.0, 0.0]), (0.75, [0.0, 0.0, 1.0]), (1.0, [1.0; 3])]; - let mut smooth = write_gradient_map(&three, false, false); - let at = smooth.len() - 2 - 16 - 2 - 4 - 2 - 2 - 4 - 2 - 2 - 2; - smooth[at..at + 2].copy_from_slice(&4096u16.to_be_bytes()); - let (a, b) = (at_t(&write_gradient_map(&three, false, false), 0.4), at_t(&smooth, 0.4)); - assert!((0..3).any(|k| (a[k] - b[k]).abs() > 1e-3), "{a:?} {b:?}"); - // Noise gradients (no colour stops) stay unsupported. - assert!(matches!(parse(b"grdm", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - } - - #[test] - fn pencil_curves_read_and_write_the_map_form() { - // Photoshop's pencil-drawn curves: map flag 1, then 256 output bytes per channel. - let mut block = vec![1, 0, 1, 0, 0, 0, 0b1111]; - let maps: [Vec; 4] = [ - (0..=255u8).rev().collect(), - (0..=255u8).map(|k| if k < 128 { 0 } else { 255 }).collect(), - (0..=255u8).collect(), - (0..=255u8).map(|k| k / 2).collect(), - ]; - for m in &maps { - block.extend_from_slice(m); - } - let a = parse(b"curv", &block, None, Channels::Rgb); - let Adjustment::Curves { master, per_channel, pencil, .. } = &a else { panic!("{a:?}") }; - assert!(*pencil); - assert_eq!(master.len(), CURVE_MAP_LEN); - assert_eq!((master[0].output, master[255].output), (1.0, 0.0)); - assert_eq!((per_channel[0][127].output, per_channel[0][128].output), (0.0, 1.0)); - assert_eq!(write(&a)[0].1, block); - } - - /// A two-stop `grdm` (version 1) whose stops are the colour structures `a` and `b`. - fn grdm_with(a: (u16, [u16; 4]), b: (u16, [u16; 4])) -> Vec { - let mut v = write_gradient_map(&[(0.0, [0.0; 3]), (1.0, [1.0; 3])], false, false); - // Header 4 + name length 4 + "Custom\0" 14 + stop count 2; 20 bytes per stop. - for (i, (space, c)) in [a, b].into_iter().enumerate() { - let at = 24 + 20 * i + 8; - v[at..at + 2].copy_from_slice(&space.to_be_bytes()); - for (k, x) in c.iter().enumerate() { - v[at + 2 + 2 * k..at + 4 + 2 * k].copy_from_slice(&x.to_be_bytes()); - } - } - v - } - - fn ends(a: &Adjustment) -> ([f32; 3], [f32; 3]) { - match a { - Adjustment::GradientMap { stops, .. } => (stops[0].1, stops[stops.len() - 1].1), - other => panic!("{other:?}"), - } - } - - #[test] - fn gradient_map_stops_in_every_colour_space() { - use photocraft_color::{Color, ColorMode}; - let white_rgb = (0, [65535, 65535, 65535, 0]); - // CMYK, stored inverted: 65535 = no ink. Full K only. - let k_only = grdm_with((2, [65535, 65535, 65535, 0]), white_rgb); - let expected = Color { mode: ColorMode::Cmyk, c: [0.0, 0.0, 0.0, 1.0], alpha: 1.0 }.to_rgb(); - for channels in [Channels::Cmyk, Channels::Rgb, Channels::Lab] { - let a = parse(b"grdm", &k_only, None, channels); - let (first, last) = ends(&a); - assert!(first.iter().zip(expected).all(|(x, y)| (x - y).abs() < 1e-5), "{first:?} vs {expected:?}"); - assert!(first[0] < 0.4 && (first[0] - first[2]).abs() < 0.05, "K-only black is a dark neutral: {first:?}"); - assert_eq!(last, [1.0; 3]); - } - // Lab: L 50, a 0, b 0 is a neutral mid grey; L 100 white. - let lab = parse(b"grdm", &grdm_with((7, [5000, 0, 0, 0]), (7, [10000, 0, 0, 0])), None, Channels::Lab); - let (first, last) = ends(&lab); - assert!(first.iter().all(|v| (v - 0.466).abs() < 0.01), "{first:?}"); - assert!(last.iter().all(|v| *v > 0.99), "{last:?}"); - // Lab with chroma: a = +60 is reddish (a and b are signed hundredths). - let red = parse(b"grdm", &grdm_with((7, [5000, 6000, 0, 0]), white_rgb), None, Channels::Lab); - let (first, _) = ends(&red); - assert!(first[0] > first[1] + 0.2, "{first:?}"); - // Grayscale: 0..10000 is ink coverage (10000 = black). - let gray = parse(b"grdm", &grdm_with((8, [10000, 0, 0, 0]), (8, [0, 0, 0, 0])), None, Channels::Gray); - assert_eq!(ends(&gray), ([0.0; 3], [1.0; 3])); - // HSB: hue 240 degrees (2/3 of 65535), full saturation and brightness = blue. - let hsb = parse(b"grdm", &grdm_with((1, [43690, 65535, 65535, 0]), white_rgb), None, Channels::Rgb); - let (first, _) = ends(&hsb); - assert!(first[0] < 1e-3 && first[1] < 1e-3 && (first[2] - 1.0).abs() < 1e-3, "{first:?}"); - // Book colours and unknown ids are not guessed: the block stays raw. - for space in [3u16, 9, 0xFFFF] { - let d = grdm_with((space, [1, 2, 3, 4]), white_rgb); - assert!(matches!(parse(b"grdm", &d, None, Channels::Rgb), Adjustment::Unsupported { .. }), "space {space}"); - } - // Truncated stops never panic. - for n in 0..k_only.len() { - let _ = parse(b"grdm", &k_only[..n], None, Channels::Cmyk); - } - } - - #[test] - fn gradient_map_reverse_and_dither_keep_the_stops_block() { - let original = grdm_with((2, [65535, 65535, 65535, 0]), (7, [10000, 0, 0, 0])); - let Adjustment::GradientMap { stops, .. } = parse(b"grdm", &original, None, Channels::Cmyk) else { panic!() }; - let mut data = original.clone(); - let target = Adjustment::GradientMap { stops: stops.clone(), reverse: true, dither: true }; - assert!(retarget_gradient_map(&mut data, &target, Channels::Cmyk)); - assert_eq!(&data[2..4], &[1, 1]); - assert_eq!(data[4..], original[4..], "the stops keep their colour spaces"); - assert_eq!(parse(b"grdm", &data, None, Channels::Cmyk), target); - // Edited stops are not patched in. - let mut edited = stops; - edited[0].1 = [0.5; 3]; - let mut data2 = original.clone(); - let target = Adjustment::GradientMap { stops: edited, reverse: false, dither: false }; - assert!(!retarget_gradient_map(&mut data2, &target, Channels::Cmyk)); - assert_eq!(data2, original); - assert!(!retarget_gradient_map(&mut [], &target, Channels::Cmyk)); - assert!(!retarget_gradient_map(&mut data2, &Adjustment::Invert, Channels::Cmyk)); - } - - fn rt(a: Adjustment) { - let blocks = write(&a); - assert!(!blocks.is_empty(), "{a:?}"); - let cged = blocks.iter().find(|b| &b.0 == b"CgEd").map(|b| &b.1[..]); - let back = parse(&blocks[0].0, &blocks[0].1, cged, Channels::Rgb); - assert_eq!(back, a); - } - - #[test] - fn roundtrips() { - rt(Adjustment::Invert); - rt(Adjustment::Threshold { level: 128.0 / 255.0 }); - rt(Adjustment::Posterize { levels: 4 }); - rt(Adjustment::BrightnessContrast { brightness: 20.0, contrast: -10.0, legacy: false }); - rt(Adjustment::BrightnessContrast { brightness: -150.0, contrast: 100.0, legacy: true }); - rt(Adjustment::HueSaturation { hue: 30.0, saturation: -20.0, lightness: 5.0, colorize: false, ranges: HueRange::defaults() }); - rt(Adjustment::HueSaturation { hue: 200.0, saturation: 50.0, lightness: 0.0, colorize: true, ranges: HueRange::defaults() }); - let mut ranges = HueRange::defaults(); - ranges[0] = HueRange { hue: 15.0, saturation: -40.0, lightness: 10.0, bounds: [-60.0, -20.0, 10.0, 30.0] }; - ranges[4].saturation = 25.0; - rt(Adjustment::HueSaturation { hue: -10.0, saturation: 0.0, lightness: 0.0, colorize: false, ranges }); - rt(Adjustment::Exposure { exposure: 1.5, offset: -0.01, gamma: 0.9 }); - let lc = |a: u16, b: u16| LevelsChannel { in_black: f32::from(a) / 255.0, in_white: f32::from(b) / 255.0, gamma: 1.2, out_black: 0.0, out_white: 1.0 }; - rt(Adjustment::Levels { - master: lc(10, 240), - per_channel: [lc(0, 255), lc(5, 250), lc(20, 200)], - space: ToneSpace::Rgb, - black: LevelsChannel::default(), - }); - let pts = |v: &[(u8, u8)]| v.iter().map(|&(i, o)| CurvePoint { input: f32::from(i) / 255.0, output: f32::from(o) / 255.0 }).collect::>(); - rt(Adjustment::Curves { - master: pts(&[(0, 0), (128, 150), (255, 255)]), - per_channel: [pts(&[(0, 10), (255, 255)]), pts(&[(0, 0), (255, 245)]), pts(&[(0, 0), (64, 32), (255, 255)])], - space: ToneSpace::Rgb, - black: Vec::new(), - pencil: false, - }); - // CMYK ink curves and levels keep their black record. - let ink = Adjustment::Curves { - master: pts(&[(0, 0), (255, 255)]), - per_channel: [pts(&[(0, 0), (255, 255)]), pts(&[(0, 30), (255, 255)]), pts(&[(0, 0), (255, 255)])], - space: ToneSpace::Cmyk, - black: pts(&[(0, 0), (128, 100), (255, 255)]), - pencil: false, - }; - let b = write(&ink); - assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Cmyk), ink); - let inkl = Adjustment::Levels { master: lc(0, 255), per_channel: [lc(0, 255), lc(5, 250), lc(0, 255)], space: ToneSpace::Cmyk, black: lc(30, 255) }; - let b = write(&inkl); - assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Cmyk), inkl); - rt(Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![1, 2, 3] }); - rt(Adjustment::GradientMap { stops: vec![(0.0, [0.0, 0.0, 0.0]), (0.5, [1.0, 0.0, 0.0]), (1.0, [1.0, 1.0, 1.0])], reverse: true, dither: false }); - rt(Adjustment::GradientMap { stops: vec![(0.0, [0.0, 0.0, 0.0]), (1.0, [1.0, 1.0, 1.0])], reverse: false, dither: true }); - rt(Adjustment::SelectiveColor { relative: true, adjustments: std::array::from_fn(|r| [r as f32 * 10.0 - 40.0, 5.0, -100.0, 100.0]) }); - rt(Adjustment::SelectiveColor { relative: false, adjustments: [[0.0; 4]; 9] }); - rt(Adjustment::Vibrance { vibrance: 35.0, saturation: -12.0 }); - rt(Adjustment::Vibrance { vibrance: 0.0, saturation: 0.0 }); - rt(Adjustment::ColorBalance { shadows: [20.0, -10.0, 5.0], midtones: [-15.0, 10.0, 30.0], highlights: [0.0, 5.0, -100.0], preserve_luminosity: true }); - rt(Adjustment::ColorBalance { shadows: [0.0; 3], midtones: [100.0, 0.0, 0.0], highlights: [0.0; 3], preserve_luminosity: false }); - rt(Adjustment::BlackWhite { weights: [40.0, 60.0, 40.0, 60.0, 20.0, 80.0], tint: None }); - rt(Adjustment::BlackWhite { weights: [-200.0, 300.0, 0.0, 10.0, 90.0, 30.0], tint: Some([0.9, 0.7, 0.5]) }); - let q = |v: u16| f32::from(v) / 65535.0; - rt(Adjustment::PhotoFilter { color: [q(60000), q(30000), q(0)], density: 0.25, preserve_luminosity: true }); - rt(Adjustment::PhotoFilter { color: [q(0), q(40000), q(65535)], density: 1.0, preserve_luminosity: false }); - rt(Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false }); - rt(Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.1], [0.0, 1.0, 0.0, 0.0], [-2.0, 0.0, 2.0, 0.0]], monochrome: true }); - let id = photocraft_cms::lutfile::LutFile::identity(5); - rt(Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(std::sync::Arc::new(id.data)), size: 5, tetrahedral: false, dither: true }); - } - - #[test] - fn selective_color_synthetic_block() { - // Absolute; reds = (+10, -20, +30, -40), blacks = (0, 0, 0, 25). - let mut d = vec![0, 1, 0, 1]; - d.extend([0u8; 8]); - for r in 0..9 { - let rec: [i16; 4] = match r { - 0 => [10, -20, 30, -40], - 8 => [0, 0, 0, 25], - _ => [0; 4], - }; - for x in rec { - d.extend(x.to_be_bytes()); - } - } - let a = parse(b"selc", &d, None, Channels::Rgb); - let Adjustment::SelectiveColor { relative, adjustments } = &a else { panic!("{a:?}") }; - assert!(!relative); - assert_eq!(adjustments[0], [10.0, -20.0, 30.0, -40.0]); - assert_eq!(adjustments[8], [0.0, 0.0, 0.0, 25.0]); - // Written back byte-exact. - assert_eq!(write(&a)[0].1, d); - } - - #[test] - fn color_lookup_synthetic_block() { - let cube = b"TITLE \"t\"\nLUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; - let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; - let desc = Descriptor::new("null") - .with("lookupType", en("colorLookupType", "3DLUT")) - .with("NM ", Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul("Id"))) - .with("Dthr", Value::Boolean(true)) - .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) - .with("LUT3DFileData", Value::RawData(cube.to_vec())); - let mut d = vec![0, 1]; - d.extend(VersionedDescriptor::new(desc).to_bytes()); - let a = parse(b"clrL", &d, None, Channels::Rgb); - let Adjustment::ColorLookup { name, lut: Some(t), size: 2, dither: true, .. } = &a else { panic!("{a:?}") }; - assert_eq!(name, "Id"); - assert_eq!(&t[..6], &[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]); - // A profile-based lookup (no table) is preserved raw. - let mut p = vec![0, 1]; - p.extend(VersionedDescriptor::new(Descriptor::new("null").with("lookupType", en("colorLookupType", "abstractProfile"))).to_bytes()); - assert!(matches!(parse(b"clrL", &p, None, Channels::Rgb), Adjustment::Unsupported { .. })); - // Each way a lookup can't be read has a reason for the import warning (#1763). - assert_eq!(unreadable_reason(b"clrL", &d), None); - assert!(unreadable_reason(b"clrL", &p).unwrap().contains("colour profile")); - assert!(unreadable_reason(b"clrL", &[0]).unwrap().contains("truncated")); - assert!(unreadable_reason(b"clrL", &[0, 2, 0, 0]).unwrap().contains("version 2")); - assert!(unreadable_reason(b"clrL", &[0, 1, 9]).unwrap().contains("unreadable settings")); - let bad = Descriptor::new("null").with("LUTFormat", en("LUTFormatType", "LUTFormat3DL")).with("LUT3DFileData", Value::RawData(b"not a lut".to_vec())); - let mut b = vec![0, 1]; - b.extend(VersionedDescriptor::new(bad).to_bytes()); - assert!(matches!(parse(b"clrL", &b, None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(unreadable_reason(b"clrL", &b).unwrap().contains("3DL LUT can't be read"), "{:?}", unreadable_reason(b"clrL", &b)); - assert_eq!(label(b"clrL"), "Color Lookup"); - assert!(matches!(parse(b"clrL", &[0, 1, 9], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"selc", &[0, 1, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - } - - #[test] - fn malformed_falls_back_to_unsupported() { - assert!(matches!(parse(b"levl", &[0, 2, 1], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"curv", &[0, 0, 9], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"thrs", &[], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"blnc", &[0; 18], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"vibA", &[0, 0, 0, 15, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"blwh", &[], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"mixr", &[0, 2, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - // A CMYK mixer (black source used) stays raw in CMYK documents; our own RGB layout maps. - let mut cmyk = i16s(&[1, 0]); - for r in 0..4 { - cmyk.extend(i16s(&std::array::from_fn::(|k| if k == r { 100 } else { 0 }))); - } - assert!(matches!(parse(b"mixr", &cmyk, None, Channels::Cmyk), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"mixr", &cmyk, None, Channels::Other), Adjustment::Unsupported { .. })); - let ours = write(&Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: false }); - assert!(matches!(parse(b"mixr", &ours[0].1, None, Channels::Cmyk), Adjustment::ChannelMixer { .. })); - assert!(matches!(parse(b"mixr", &ours[0].1, None, Channels::Other), Adjustment::ChannelMixer { .. })); - assert!(matches!(parse(b"mixr", &[0, 1, 0, 0, 0, 100], None, Channels::Rgb), Adjustment::Unsupported { .. })); - // Photo Filter: unknown version, HSB colour space, out-of-range version-3 XYZ, truncated. - assert!(matches!(parse(b"phfl", &[0, 4, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - assert!(matches!(parse(b"phfl", &[0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 1], None, Channels::Rgb), Adjustment::Unsupported { .. })); - for bad in [[200_000u32, 0, 0], [0, u32::MAX, 0]] { - let mut xyz = vec![0, 3]; - for x in bad { - xyz.extend(x.to_be_bytes()); - } - xyz.extend([0, 0, 0, 25, 1]); - assert!(matches!(parse(b"phfl", &xyz, None, Channels::Rgb), Adjustment::Unsupported { .. })); - } - assert!(matches!(parse(b"phfl", &[0, 2, 0, 0, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); - // Arbitrary bytes never panic, whatever the key. - for key in ADJUSTMENT_KEYS { - for len in 0..64usize { - let d: Vec = (0..len).map(|i| (i * 37 + len) as u8).collect(); - let _ = parse(key, &d, Some(&d), Channels::Rgb); - let _ = parse(key, &d, None, Channels::Other); - } - } - } - - fn i16s(v: &[i16]) -> Vec { - v.iter().flat_map(|x| x.to_be_bytes()).collect() - } - - /// Hand-built blocks laid out per the Adobe spec (no file written by Photoshop involved). - #[test] - fn color_balance_golden_block() { - let mut d = i16s(&[-30, 20, 0, 10, -5, 100, 0, 0, -100]); - d.extend([1, 0]); - let a = parse(b"blnc", &d, None, Channels::Rgb); - assert_eq!( - a, - Adjustment::ColorBalance { shadows: [-30.0, 20.0, 0.0], midtones: [10.0, -5.0, 100.0], highlights: [0.0, 0.0, -100.0], preserve_luminosity: true } - ); - assert_eq!(write(&a), vec![(*b"blnc", d.clone())]); - // The 19-byte form (no trailing pad) parses the same. - assert_eq!(parse(b"blnc", &d[..19], None, Channels::Rgb), a); - } - - #[test] - fn photo_filter_golden_blocks() { - // Version 2, RGB colour structure (65535, 32768, 0), density 40 %, preserve luminosity. - let mut v2 = i16s(&[2, 0]); - v2.extend([0xff, 0xff, 0x80, 0x00, 0, 0, 0, 0]); - v2.extend(40u32.to_be_bytes()); - v2.extend([1, 0, 0, 0]); - assert_eq!(v2.len(), 20, "Photo Filter must occupy 20 bytes"); - assert_eq!(&v2[17..], &[0, 0, 0], "padding is included inside the block length"); - let a = parse(b"phfl", &v2, None, Channels::Rgb); - let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = a else { panic!("{a:?}") }; - assert_eq!(color, [1.0, 32768.0 / 65535.0, 0.0]); - assert_eq!(density, 0.4); - assert_eq!(write(&a)[0].1, v2, "version 2 RGB is written back byte-exact"); - // Older PhotoCraft files used only one padding byte (18 bytes); keep importing them, - // but never produce that Photoshop-incompatible length again. - assert_eq!(parse(b"phfl", &v2[..18], None, Channels::Rgb), a); - for preserve_luminosity in [false, true] { - for density in [0.0, 0.14, 1.0] { - let adjustment = Adjustment::PhotoFilter { color: [0.8, 0.4, 0.2], density, preserve_luminosity }; - let blocks = write(&adjustment); - assert_eq!(blocks.len(), 1); - assert_eq!(blocks[0].0, *b"phfl"); - assert_eq!(blocks[0].1.len(), 20); - assert_eq!(&blocks[0].1[17..], &[0, 0, 0]); - let Adjustment::PhotoFilter { color, density: read_density, preserve_luminosity: read_preserve } = - parse(b"phfl", &blocks[0].1, None, Channels::Rgb) - else { - panic!("written phfl must parse") - }; - assert!(color.iter().zip([0.8, 0.4, 0.2]).all(|(got, expected)| (got - expected).abs() <= 1.0 / 65535.0)); - assert!((read_density - density).abs() < 0.00001); - assert_eq!(read_preserve, preserve_luminosity); - } - } - // Version 2, Lab colour structure: L 50, a 0, b 0 is mid gray. - let mut lab = i16s(&[2, 7, 5000, 0, 0, 0]); - lab.extend(25u32.to_be_bytes()); - lab.push(0); - let Adjustment::PhotoFilter { color, density, preserve_luminosity: false } = parse(b"phfl", &lab, None, Channels::Rgb) else { panic!() }; - assert!(color.iter().all(|c| (c - 0.466).abs() < 0.01), "{color:?}"); - assert_eq!(density, 0.25); - // Version 3: XYZ relative to the D50 white, 1.0 = 32768. White is (32768, 32768, 32768). - let v3 = phfl_v3([32768, 32768, 32768], 100, true); - let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = parse(b"phfl", &v3, None, Channels::Rgb) else { panic!() }; - assert!(color.iter().all(|c| (c - 1.0).abs() < 0.005), "{color:?}"); - assert_eq!(density, 1.0); - // Photoshop's Warming Filter (85) preset, sRGB #EC8A00: the CS2 file of #4940 stores it - // relative to the D50 white as (15548, 12029, 0). - let Adjustment::PhotoFilter { color, density, preserve_luminosity: false } = - parse(b"phfl", &phfl_v3([15548, 12029, 0], 25, false), None, Channels::Rgb) - else { - panic!() - }; - let expected = [0xecu8, 0x8a, 0x00].map(|v| f32::from(v) / 255.0); - assert!(color.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{color:?}"); - assert_eq!(density, 0.25); - // The block from #3777 (Photoshop CS2): the Yellow filter preset, #F9E31C, density 63 %. - let issue = [0, 3, 0, 0, 0x5e, 0x7f, 0, 0, 0x61, 0xaa, 0, 0, 0x0e, 0xf3, 0, 0, 0, 0x3f, 1, 0]; - let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = parse(b"phfl", &issue, None, Channels::Rgb) else { panic!() }; - let expected = [0xf9u8, 0xe3, 0x1c].map(|v| f32::from(v) / 255.0); - assert!(color.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{color:?}"); - assert_eq!(density, 0.63); - // Cooling Filter (82) from #4940: a deep blue outside sRGB; its Z stays under the 2.0 - // garbage guard and the colour clamps to (0, 181, 255). - let Adjustment::PhotoFilter { color: cooling, density, preserve_luminosity: true } = - parse(b"phfl", &phfl_v3([5327, 9584, 65357], 72, true), None, Channels::Rgb) - else { - panic!("Cooling Filter (82) must parse") - }; - let expected = [0u8, 181, 255].map(|v| f32::from(v) / 255.0); - assert!(cooling.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{cooling:?}"); - assert_eq!(density, 0.72); - // The 19-byte form (no trailing pad) parses the same. - assert_eq!(parse(b"phfl", &issue[..19], None, Channels::Rgb), parse(b"phfl", &issue, None, Channels::Rgb)); - // Version 3 is read, never written: an edited filter is saved as version 2 and keeps its colour. - let a = parse(b"phfl", &issue, None, Channels::Rgb); - let w = write(&a); - assert_eq!(&w[0].1[..2], &[0, 2]); - let Adjustment::PhotoFilter { color: back, .. } = parse(b"phfl", &w[0].1, None, Channels::Rgb) else { panic!() }; - assert!(back.iter().zip(color).all(|(b, c)| (b - c).abs() <= 1.0 / 65535.0)); - } - - /// A version 3 Photo Filter block: version, three u32 XYZ components, density, flag, pad. - fn phfl_v3(xyz: [u32; 3], density: u32, preserve_luminosity: bool) -> Vec { - let mut d = vec![0, 3]; - for x in xyz { - d.extend(x.to_be_bytes()); - } - d.extend(density.to_be_bytes()); - d.extend([u8::from(preserve_luminosity), 0]); - d - } - - #[test] - fn channel_mixer_golden_blocks() { - // RGB: red = 100 % R + 20 % const; green = 50 % R + 50 % G; blue = -50 % G + 150 % B - 10 % const. - let mut d = i16s(&[1, 0]); - d.extend(i16s(&[100, 0, 0, 0, 20, 50, 50, 0, 0, 0, 0, -50, 150, 0, -10])); - let a = parse(b"mixr", &d, None, Channels::Rgb); - assert_eq!(a, Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.2], [0.5, 0.5, 0.0, 0.0], [0.0, -0.5, 1.5, -0.1]], monochrome: false }); - // Written with the trailing gray record (a copy of the first row). - let w = write(&a); - assert_eq!(w[0].1[..d.len()], d[..]); - assert_eq!(w[0].1[d.len()..], i16s(&[100, 0, 0, 0, 20])[..]); - // Monochrome with only the gray record: 30/59/11. - let mut m = i16s(&[1, 1]); - m.extend(i16s(&[30, 59, 11, 0, 0])); - let Adjustment::ChannelMixer { matrix, monochrome: true } = parse(b"mixr", &m, None, Channels::Rgb) else { panic!() }; - assert_eq!(matrix[0], [0.3, 0.59, 0.11, 0.0]); - } - - fn vdesc(d: Descriptor) -> Vec { - VersionedDescriptor::new(d).to_bytes() - } - - #[test] - fn vibrance_golden_block() { - let d = vdesc(Descriptor::new("null").with("vibrance", Value::Integer(-40)).with("Strt", Value::Integer(25))); - assert_eq!(&d[..4], &[0, 0, 0, 16], "descriptor version 16"); - assert_eq!(parse(b"vibA", &d, None, Channels::Rgb), Adjustment::Vibrance { vibrance: -40.0, saturation: 25.0 }); - // Photoshop omits zero values. - let only = vdesc(Descriptor::new("null").with("Strt", Value::Integer(10))); - assert_eq!(parse(b"vibA", &only, None, Channels::Rgb), Adjustment::Vibrance { vibrance: 0.0, saturation: 10.0 }); - // Other descriptor versions are rejected (kept raw). - let mut v = d.clone(); - v[3] = 17; - assert!(matches!(parse(b"vibA", &v, None, Channels::Rgb), Adjustment::Unsupported { .. })); - } - - #[test] - fn black_white_golden_block() { - let tint = Descriptor::new("RGBC").with("Rd ", Value::Double(255.0)).with("Grn ", Value::Double(127.5)).with("Bl ", Value::Double(0.0)); - let mut desc = Descriptor::new("null"); - for (k, w) in BW_KEYS.iter().zip([10, 20, 30, 40, 50, 60]) { - desc = desc.with(k, Value::Integer(w)); - } - let d = vdesc( - desc.clone().with("useTint", Value::Boolean(true)).with("tintColor", Value::Descriptor(tint.clone())).with("bwPresetKind", Value::Integer(1)), - ); - let a = parse(b"blwh", &d, None, Channels::Rgb); - assert_eq!(a, Adjustment::BlackWhite { weights: [10.0, 20.0, 30.0, 40.0, 50.0, 60.0], tint: Some([1.0, 0.5, 0.0]) }); - // useTint off: no tint, even with a stored colour. - let off = vdesc(desc.with("useTint", Value::Boolean(false)).with("tintColor", Value::Descriptor(tint))); - assert!(matches!(parse(b"blwh", &off, None, Channels::Rgb), Adjustment::BlackWhite { tint: None, .. })); - // Missing weights take Photoshop's defaults. - let empty = vdesc(Descriptor::new("null")); - assert_eq!(parse(b"blwh", &empty, None, Channels::Rgb), Adjustment::BlackWhite { weights: BW_DEFAULT, tint: None }); - } - - #[test] - fn degenerate_adjustments_still_get_an_encoding() { - // Gradient maps with fewer than two stops render as gray (none) or one colour. - for (stops, want) in - [(vec![], vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])]), (vec![(0.3, [0.2, 0.4, 0.6])], vec![(0.0, [0.2, 0.4, 0.6]), (1.0, [0.2, 0.4, 0.6])])] - { - let b = write(&Adjustment::GradientMap { stops, reverse: true, dither: false }); - let back = parse(&b[0].0, &b[0].1, None, Channels::Rgb); - let Adjustment::GradientMap { stops, reverse: true, .. } = back else { panic!("{back:?}") }; - assert_eq!(stops.len(), want.len()); - for (s, w) in stops.iter().zip(&want) { - assert!((s.0 - w.0).abs() < 1e-6 && (0..3).all(|k| (s.1[k] - w.1[k]).abs() < 1e-4), "{s:?} vs {w:?}"); - } - } - // A colour lookup without a (usable) table becomes an identity cube, without dither. - for lut in [None, Some(std::sync::Arc::new(vec![0.5; 7]))] { - let b = write(&Adjustment::ColorLookup { name: "L".into(), lut, size: 3, tetrahedral: false, dither: true }); - let back = parse(&b[0].0, &b[0].1, None, Channels::Rgb); - let Adjustment::ColorLookup { lut: Some(t), size: 2, dither: false, name, .. } = back else { panic!("{back:?}") }; - assert_eq!(name, "L"); - assert_eq!(*t, photocraft_cms::lutfile::LutFile::identity(2).data); - } - // Every kind except a malformed Unsupported key has an encoding. - assert!(write(&Adjustment::Unsupported { psd_key: "abc".into(), raw: vec![] }).is_empty()); - } - - #[test] - fn non_rgb_ignores_channel_records() { - let lc = LevelsChannel { in_black: 0.1, ..Default::default() }; - let a = Adjustment::Levels { - master: LevelsChannel::default(), - per_channel: [lc.clone(), lc.clone(), lc], - space: ToneSpace::Rgb, - black: LevelsChannel::default(), - }; - let b = write(&a); - match parse(&b[0].0, &b[0].1, None, Channels::Other) { - Adjustment::Levels { per_channel, .. } => assert_eq!(per_channel, <[LevelsChannel; 3]>::default()), - other => panic!("{other:?}"), - } - } - - #[test] - fn gray_uses_first_channel_record_for_all() { - let lc = LevelsChannel { in_black: 44.0 / 255.0, ..Default::default() }; - let a = Adjustment::Levels { - master: LevelsChannel::default(), - per_channel: [lc.clone(), Default::default(), Default::default()], - space: ToneSpace::Rgb, - black: LevelsChannel::default(), - }; - let b = write(&a); - match parse(&b[0].0, &b[0].1, None, Channels::Gray) { - Adjustment::Levels { per_channel, .. } => assert_eq!(per_channel, [lc.clone(), lc.clone(), lc]), - other => panic!("{other:?}"), - } - } - - #[test] - fn legacy_brit_without_cged() { - let a = parse(b"brit", &[0, 10, 0xff, 0xf6, 0, 127, 0], None, Channels::Rgb); - assert_eq!(a, Adjustment::BrightnessContrast { brightness: 10.0, contrast: -10.0, legacy: true }); - } -} +mod tests; diff --git a/crates/io/src/adjust_map/encode.rs b/crates/io/src/adjust_map/encode.rs new file mode 100644 index 0000000000..08d3075b57 --- /dev/null +++ b/crates/io/src/adjust_map/encode.rs @@ -0,0 +1,236 @@ +use photocraft_doc::adjust::{CurvePoint, HueRange, LevelsChannel, LutFormat, LutSource, ToneSpace, curve_map_outputs}; +use photocraft_psd::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; + +use super::{BW_KEYS, BW_TINT, clamp_i16, cube_file_name, levels_write, lut_source_for, psd_lut_format, put16, q255, rgb_desc}; + +/// The values as big-endian 16-bit words. +pub(super) fn words(xs: &[u16]) -> Vec { + let mut v = Vec::new(); + for &x in xs { + put16(&mut v, x); + } + v +} + +/// Appends `x` as a clamped big-endian 16-bit integer. +fn push_i16(v: &mut Vec, x: f32) { + v.extend_from_slice(&clamp_i16(x).to_be_bytes()); +} + +/// The legacy Brightness/Contrast block and its descriptor block. +pub(super) fn brightness_contrast(brightness: f32, contrast: f32, legacy: bool) -> Vec<([u8; 4], Vec)> { + let mut v = Vec::new(); + push_i16(&mut v, brightness); + push_i16(&mut v, contrast); + put16(&mut v, 127); + v.push(0); + let d = Descriptor::new("null") + .with("Vrsn", Value::Integer(1)) + .with("Brgh", Value::Integer(brightness.round() as i32)) + .with("Cntr", Value::Integer(contrast.round() as i32)) + .with("means", Value::Integer(127)) + .with("Lab ", Value::Boolean(false)) + .with("useLegacy", Value::Boolean(legacy)) + .with("Auto", Value::Boolean(false)); + vec![(*b"brit", v), (*b"CgEd", VersionedDescriptor::new(d).to_bytes())] +} + +/// Hue/Saturation payload: the master values, then the six hue ranges. +pub(super) fn hue_saturation(hsl: [f32; 3], colorize: bool, ranges: &[HueRange; 6]) -> Vec { + let mut v = words(&[2]); + v.push(u8::from(colorize)); + v.push(0); + // The colorize triplet, then the master triplet. The inactive one gets Photoshop's + // defaults; the colorize hue (0..360 in the model) is stored signed, -180..180. + let [hue, saturation, lightness] = hsl; + let (colorize_hsl, master) = if colorize { + let h = hue.rem_euclid(360.0); + ([if h > 180.0 { h - 360.0 } else { h }, saturation, lightness], [0.0; 3]) + } else { + ([30.0, 25.0, 0.0], hsl) + }; + for x in colorize_hsl.into_iter().chain(master) { + push_i16(&mut v, x); + } + // Six hue ranges: four bounds in 0..360, then hue, saturation and lightness. + for r in ranges { + for x in r.bounds { + put16(&mut v, x.rem_euclid(360.0).round() as u16 % 360); + } + for x in [r.hue, r.saturation, r.lightness] { + push_i16(&mut v, x); + } + } + v +} + +/// Exposure payload. +pub(super) fn exposure(exposure: f32, offset: f32, gamma: f32) -> Vec { + let mut v = words(&[1]); + for x in [exposure, offset, gamma] { + v.extend_from_slice(&x.to_be_bytes()); + } + v.push(1); // color space flag (spec: "1 byte") + v +} + +/// Levels payload: master, the three colour channels, black (CMYK only), then identity records. +pub(super) fn levels(master: &LevelsChannel, per_channel: &[LevelsChannel; 3], space: ToneSpace, black: &LevelsChannel) -> Vec { + let mut v = words(&[2]); + levels_write(&mut v, master); + for c in per_channel { + levels_write(&mut v, c); + } + let ident = LevelsChannel::default(); + levels_write(&mut v, if space == ToneSpace::Cmyk { black } else { &ident }); + for _ in 5..29 { + levels_write(&mut v, &LevelsChannel::default()); + } + v +} + +/// Curves payload: each channel's points, or its 256-entry map in pencil mode. +pub(super) fn curves(master: &[CurvePoint], per_channel: &[Vec; 3], space: ToneSpace, black: &[CurvePoint], pencil: bool) -> Vec { + let mut v = vec![u8::from(pencil)]; + put16(&mut v, 1); + let ink = space == ToneSpace::Cmyk && black.len() >= 2; + v.extend_from_slice(&(if ink { 0b11111u32 } else { 0b1111 }).to_be_bytes()); + for c in std::iter::once(master).chain(per_channel.iter().map(Vec::as_slice)).chain(ink.then_some(black)) { + if pencil { + v.extend(curve_map_outputs(c).iter().map(|&o| u8::try_from(q255(o)).unwrap_or(u8::MAX))); + continue; + } + put16(&mut v, c.len().min(19) as u16); + for p in c.iter().take(19) { + put16(&mut v, q255(p.output)); + put16(&mut v, q255(p.input)); + } + } + v +} + +/// Selective Color payload, with each value clamped to ±100%. +pub(super) fn selective_color(relative: bool, adjustments: &[[f32; 4]; 9]) -> Vec { + let mut v = words(&[1, u16::from(!relative)]); + v.extend_from_slice(&[0; 8]); + for rec in adjustments { + for x in rec { + push_i16(&mut v, x.clamp(-100.0, 100.0)); + } + } + v +} + +/// Color Lookup payload, written as an identity cube when there is no usable table. +pub(super) fn color_lookup(name: &str, lut: Option<&[f32]>, size: u32, dither: bool, source: Option<&LutSource>) -> Vec { + // A lookup without a usable table (none chosen yet, or a bad size) renders as the + // identity, so it is written as a 2³ identity cube rather than dropped. + let n = size as usize; + let cube = lut.zip(photocraft_doc::adjust::lut3d_len(size)).and_then(|(table, len)| table.get(..len)).filter(|_| n <= 256); + // Like Photoshop, embed the LUT file as loaded, in its own format; a table made or + // changed in PhotoCraft is written as `.cube`, named to match. + let (format, data, name, dither) = match cube { + Some(cube) => match lut_source_for(source, cube, n) { + Some(src) => (src.format, src.bytes.to_vec(), name.to_string(), dither), + None => { + let file = photocraft_cms::lutfile::LutFile { title: String::new(), size: n, data: cube.to_vec(), domain_min: [0.0; 3], domain_max: [1.0; 3] }; + (LutFormat::Cube, photocraft_cms::lutfile::write_cube(&file).into_bytes(), cube_file_name(name), dither) + } + }, + _ => { + let file = photocraft_cms::lutfile::LutFile { title: String::new(), ..photocraft_cms::lutfile::LutFile::identity(2) }; + (LutFormat::Cube, photocraft_cms::lutfile::write_cube(&file).into_bytes(), cube_file_name(name), false) + } + }; + let en = |t: &str, val: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(val) }; + let text = |t: &str| Value::Text(UnicodeString::new_nul(t)); + let d = Descriptor::new("null") + .with("lookupType", en("colorLookupType", "3DLUT")) + .with("NM ", text(&name)) + .with("Dthr", Value::Boolean(dither)) + .with("profile", Value::RawData(Vec::new())) + .with("LUTFormat", en("LUTFormatType", psd_lut_format(format))) + .with("dataOrder", en("colorLookupOrder", "rgbOrder")) + .with("tableOrder", en("colorLookupOrder", "bgrOrder")) + .with("LUT3DFileData", Value::RawData(data)) + .with("LUT3DFileName", text(&name)); + let mut v = words(&[1]); + v.extend_from_slice(&VersionedDescriptor::new(d).to_bytes()); + v +} + +/// At least the two stops Photoshop needs, matching how fewer stops render. +pub(super) fn gradient_stops(stops: &[(f32, [f32; 3])]) -> Vec<(f32, [f32; 3])> { + // Photoshop needs two stops: none becomes black to white, one becomes a flat colour. + match stops { + [] => vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])], + [(_, c)] => vec![(0.0, *c), (1.0, *c)], + s => s.to_vec(), + } +} + +/// A descriptor integer of `x` clamped to `lo..=hi`. +fn percent(x: f32, lo: f32, hi: f32) -> Value { + Value::Integer(i32::from(clamp_i16(x.clamp(lo, hi)))) +} + +/// Vibrance payload. +pub(super) fn vibrance(vibrance: f32, saturation: f32) -> Vec { + let d = Descriptor::new("null").with("vibrance", percent(vibrance, -100.0, 100.0)).with("Strt", percent(saturation, -100.0, 100.0)); + VersionedDescriptor::new(d).to_bytes() +} + +/// Color Balance payload: shadows, midtones and highlights, then Preserve Luminosity. +pub(super) fn color_balance(tones: [&[f32; 3]; 3], preserve_luminosity: bool) -> Vec { + let mut v = Vec::new(); + for x in tones.into_iter().flatten() { + push_i16(&mut v, x.clamp(-100.0, 100.0)); + } + v.push(u8::from(preserve_luminosity)); + v.push(0); + v +} + +/// Black & White payload. +pub(super) fn black_white(weights: &[f32; 6], tint: Option<[f32; 3]>) -> Vec { + let mut d = Descriptor::new("null"); + for (w, k) in weights.iter().zip(BW_KEYS) { + d = d.with(k, percent(*w, -200.0, 300.0)); + } + let d = d + .with("useTint", Value::Boolean(tint.is_some())) + .with("tintColor", Value::Descriptor(rgb_desc(tint.unwrap_or(BW_TINT)))) + .with("bwPresetKind", Value::Integer(1)) + .with("blackAndWhitePresetFileName", Value::Text(UnicodeString::new_nul(""))); + VersionedDescriptor::new(d).to_bytes() +} + +/// Photo Filter payload, version 2 with a 16-bit colour. +pub(super) fn photo_filter(color: [f32; 3], density: f32, preserve_luminosity: bool) -> Vec { + // Version 2 keeps the colour at 16 bits. + let mut v = words(&[2, 0]); + for x in color { + put16(&mut v, (x.clamp(0.0, 1.0) * 65535.0).round() as u16); + } + put16(&mut v, 0); + v.extend_from_slice(&((density.clamp(0.0, 1.0) * 100.0).round() as u32).to_be_bytes()); + v.push(u8::from(preserve_luminosity)); + // Photoshop only opens the payload padded to 4 bytes inside the block (#1455). + v.extend_from_slice(&[0, 0, 0]); + v +} + +/// Channel Mixer payload: the red, green and blue records, then the gray record. +pub(super) fn channel_mixer(matrix: &[[f32; 4]; 3], monochrome: bool) -> Vec { + let mut v = words(&[1, u16::from(monochrome)]); + // Red, green and blue rows, then the gray record that monochrome uses. + for row in matrix.iter().chain(std::iter::once(&matrix[0])) { + let pct = |x: f32| clamp_i16((x * 100.0).clamp(-200.0, 200.0)).to_be_bytes(); + for x in &row[..3] { + v.extend_from_slice(&pct(*x)); + } + v.extend_from_slice(&[0, 0]); + v.extend_from_slice(&pct(row[3])); + } + v +} diff --git a/crates/io/src/adjust_map/tests.rs b/crates/io/src/adjust_map/tests.rs new file mode 100644 index 0000000000..c369327d86 --- /dev/null +++ b/crates/io/src/adjust_map/tests.rs @@ -0,0 +1,717 @@ +use super::*; + +#[test] +fn gradient_map_v3_methods_and_smoothness() { + let classic = vec![(0.0, [0.0, 0.0, 1.0]), (1.0, [1.0, 1.0, 0.0])]; + let v1 = write_gradient_map(&classic, false, false); + // Version 3 inserts the interpolation method after reverse/dither. + let v3 = |m: &[u8; 4]| { + let mut v = v1.clone(); + v[1] = 3; + v.splice(4..4, m.iter().copied()); + v + }; + let at_t = |data: &[u8], t: f32| match parse(b"grdm", data, None, Channels::Rgb) { + Adjustment::GradientMap { stops, .. } => { + let i = stops.windows(2).position(|w| w[0].0 <= t && t <= w[1].0).unwrap(); + let (x, y) = (stops[i], stops[i + 1]); + let u = (t - x.0) / (y.0 - x.0).max(1e-6); + std::array::from_fn::(|k| x.1[k] + (y.1[k] - x.1[k]) * u) + } + other => panic!("{other:?}"), + }; + let mid = |data: &[u8]| at_t(data, 0.5); + let c = mid(&v3(b"Gcls")); + assert!((c[0] - 0.5).abs() < 1e-3 && (c[2] - 0.5).abs() < 1e-3, "classic = sRGB lerp {c:?}"); + let p = mid(&v3(b"Perc")); + assert!((p[0] - c[0]).abs() > 0.02, "perceptual differs from classic {p:?}"); + // Smoothness 100 % (4096) bends a three-stop ramp. + let three = vec![(0.0, [0.0; 3]), (0.25, [1.0, 0.0, 0.0]), (0.75, [0.0, 0.0, 1.0]), (1.0, [1.0; 3])]; + let mut smooth = write_gradient_map(&three, false, false); + // v1 header/name/count + colour stops + transparency stops + expansion count. + let at = 48 + three.len() * 20; + smooth[at..at + 2].copy_from_slice(&4096u16.to_be_bytes()); + let (a, b) = (at_t(&write_gradient_map(&three, false, false), 0.4), at_t(&smooth, 0.4)); + assert!((0..3).any(|k| (a[k] - b[k]).abs() > 1e-3), "{a:?} {b:?}"); + // Noise gradients (no colour stops) stay unsupported. + assert!(matches!(parse(b"grdm", &[0, 1, 0, 0, 0, 0, 0, 0, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); +} + +#[test] +fn pencil_curves_read_and_write_the_map_form() { + // Photoshop's pencil-drawn curves: map flag 1, then 256 output bytes per channel. + let mut block = vec![1, 0, 1, 0, 0, 0, 0b1111]; + let maps: [Vec; 4] = [ + (0..=255u8).rev().collect(), + (0..=255u8).map(|k| if k < 128 { 0 } else { 255 }).collect(), + (0..=255u8).collect(), + (0..=255u8).map(|k| k / 2).collect(), + ]; + for m in &maps { + block.extend_from_slice(m); + } + let a = parse(b"curv", &block, None, Channels::Rgb); + let Adjustment::Curves { master, per_channel, pencil, .. } = &a else { panic!("{a:?}") }; + assert!(*pencil); + assert_eq!(master.len(), CURVE_MAP_LEN); + assert_eq!((master[0].output, master[255].output), (1.0, 0.0)); + assert_eq!((per_channel[0][127].output, per_channel[0][128].output), (0.0, 1.0)); + assert_eq!(write(&a)[0].1, block); +} + +/// A two-stop `grdm` (version 1) whose stops are the colour structures `a` and `b`. +fn grdm_with(a: (u16, [u16; 4]), b: (u16, [u16; 4])) -> Vec { + let mut v = write_gradient_map(&[(0.0, [0.0; 3]), (1.0, [1.0; 3])], false, false); + // Header 4 + name length 4 + "Custom\0" 14 + stop count 2; 20 bytes per stop. + for (i, (space, c)) in [a, b].into_iter().enumerate() { + let at = 24 + 20 * i + 8; + v[at..at + 2].copy_from_slice(&space.to_be_bytes()); + for (k, x) in c.iter().enumerate() { + v[at + 2 + 2 * k..at + 4 + 2 * k].copy_from_slice(&x.to_be_bytes()); + } + } + v +} + +fn ends(a: &Adjustment) -> ([f32; 3], [f32; 3]) { + match a { + Adjustment::GradientMap { stops, .. } => (stops[0].1, stops[stops.len() - 1].1), + other => panic!("{other:?}"), + } +} + +#[test] +fn gradient_map_stops_in_every_colour_space() { + use photocraft_color::{Color, ColorMode}; + let white_rgb = (0, [65535, 65535, 65535, 0]); + // CMYK, stored inverted: 65535 = no ink. Full K only. + let k_only = grdm_with((2, [65535, 65535, 65535, 0]), white_rgb); + let expected = Color { mode: ColorMode::Cmyk, c: [0.0, 0.0, 0.0, 1.0], alpha: 1.0 }.to_rgb(); + for channels in [Channels::Cmyk, Channels::Rgb, Channels::Lab] { + let a = parse(b"grdm", &k_only, None, channels); + let (first, last) = ends(&a); + assert!(first.iter().zip(expected).all(|(x, y)| (x - y).abs() < 1e-5), "{first:?} vs {expected:?}"); + assert!(first[0] < 0.4 && (first[0] - first[2]).abs() < 0.05, "K-only black is a dark neutral: {first:?}"); + assert_eq!(last, [1.0; 3]); + } + // Lab: L 50, a 0, b 0 is a neutral mid grey; L 100 white. + let lab = parse(b"grdm", &grdm_with((7, [5000, 0, 0, 0]), (7, [10000, 0, 0, 0])), None, Channels::Lab); + let (first, last) = ends(&lab); + assert!(first.iter().all(|v| (v - 0.466).abs() < 0.01), "{first:?}"); + assert!(last.iter().all(|v| *v > 0.99), "{last:?}"); + // Lab with chroma: a = +60 is reddish (a and b are signed hundredths). + let red = parse(b"grdm", &grdm_with((7, [5000, 6000, 0, 0]), white_rgb), None, Channels::Lab); + let (first, _) = ends(&red); + assert!(first[0] > first[1] + 0.2, "{first:?}"); + // Grayscale: 0..10000 is ink coverage (10000 = black). + let gray = parse(b"grdm", &grdm_with((8, [10000, 0, 0, 0]), (8, [0, 0, 0, 0])), None, Channels::Gray); + assert_eq!(ends(&gray), ([0.0; 3], [1.0; 3])); + // HSB: hue 240 degrees (2/3 of 65535), full saturation and brightness = blue. + let hsb = parse(b"grdm", &grdm_with((1, [43690, 65535, 65535, 0]), white_rgb), None, Channels::Rgb); + let (first, _) = ends(&hsb); + assert!(first[0] < 1e-3 && first[1] < 1e-3 && (first[2] - 1.0).abs() < 1e-3, "{first:?}"); + // Book colours and unknown ids are not guessed: the block stays raw. + for space in [3u16, 9, 0xFFFF] { + let d = grdm_with((space, [1, 2, 3, 4]), white_rgb); + assert!(matches!(parse(b"grdm", &d, None, Channels::Rgb), Adjustment::Unsupported { .. }), "space {space}"); + } + // Truncated stops never panic. + for n in 0..k_only.len() { + let _ = parse(b"grdm", &k_only[..n], None, Channels::Cmyk); + } +} + +#[test] +fn gradient_map_reverse_and_dither_keep_the_stops_block() { + let original = grdm_with((2, [65535, 65535, 65535, 0]), (7, [10000, 0, 0, 0])); + let Adjustment::GradientMap { stops, .. } = parse(b"grdm", &original, None, Channels::Cmyk) else { panic!() }; + let mut data = original.clone(); + let target = Adjustment::GradientMap { stops: stops.clone(), reverse: true, dither: true }; + assert!(retarget_gradient_map(&mut data, &target, Channels::Cmyk)); + assert_eq!(&data[2..4], &[1, 1]); + assert_eq!(data[4..], original[4..], "the stops keep their colour spaces"); + assert_eq!(parse(b"grdm", &data, None, Channels::Cmyk), target); + // Edited stops are not patched in. + let mut edited = stops; + edited[0].1 = [0.5; 3]; + let mut data2 = original.clone(); + let target = Adjustment::GradientMap { stops: edited, reverse: false, dither: false }; + assert!(!retarget_gradient_map(&mut data2, &target, Channels::Cmyk)); + assert_eq!(data2, original); + assert!(!retarget_gradient_map(&mut [], &target, Channels::Cmyk)); + assert!(!retarget_gradient_map(&mut data2, &Adjustment::Invert, Channels::Cmyk)); +} + +fn rt(a: Adjustment) { + let blocks = write(&a); + assert!(!blocks.is_empty(), "{a:?}"); + let cged = blocks.iter().find(|b| &b.0 == b"CgEd").map(|b| &b.1[..]); + let back = parse(&blocks[0].0, &blocks[0].1, cged, Channels::Rgb); + assert_eq!(back, a); +} + +/// Photoshop keeps the colorize triplet at byte 4 and the master triplet at byte 10, and fills +/// the inactive one with its defaults: colorize (30, 25, 0), master (0, 0, 0). The colorize hue +/// is stored signed (200 -> -160). +#[test] +fn hue2_writes_each_triplet_in_its_own_slot() { + let triplets = |a: &Adjustment| -> Vec { + let blocks = write(a); + (0..6).map(|i| bei16(&blocks[0].1, 4 + 2 * i).unwrap_or(i16::MIN)).collect() + }; + let on = Adjustment::HueSaturation { hue: 200.0, saturation: 40.0, lightness: 10.0, colorize: true, ranges: HueRange::defaults() }; + assert_eq!(triplets(&on), [-160, 40, 10, 0, 0, 0]); + let off = Adjustment::HueSaturation { hue: -30.0, saturation: 20.0, lightness: 5.0, colorize: false, ranges: HueRange::defaults() }; + assert_eq!(triplets(&off), [30, 25, 0, -30, 20, 5]); +} + +#[test] +fn roundtrips() { + rt(Adjustment::Invert); + rt(Adjustment::Threshold { level: 128.0 / 255.0 }); + rt(Adjustment::Posterize { levels: 4 }); + rt(Adjustment::BrightnessContrast { brightness: 20.0, contrast: -10.0, legacy: false }); + rt(Adjustment::BrightnessContrast { brightness: -150.0, contrast: 100.0, legacy: true }); + rt(Adjustment::HueSaturation { hue: 30.0, saturation: -20.0, lightness: 5.0, colorize: false, ranges: HueRange::defaults() }); + rt(Adjustment::HueSaturation { hue: 200.0, saturation: 50.0, lightness: 0.0, colorize: true, ranges: HueRange::defaults() }); + let mut ranges = HueRange::defaults(); + ranges[0] = HueRange { hue: 15.0, saturation: -40.0, lightness: 10.0, bounds: [-60.0, -20.0, 10.0, 30.0] }; + ranges[4].saturation = 25.0; + rt(Adjustment::HueSaturation { hue: -10.0, saturation: 0.0, lightness: 0.0, colorize: false, ranges }); + rt(Adjustment::Exposure { exposure: 1.5, offset: -0.01, gamma: 0.9 }); + let lc = |a: u16, b: u16| LevelsChannel { in_black: f32::from(a) / 255.0, in_white: f32::from(b) / 255.0, gamma: 1.2, out_black: 0.0, out_white: 1.0 }; + rt(Adjustment::Levels { master: lc(10, 240), per_channel: [lc(0, 255), lc(5, 250), lc(20, 200)], space: ToneSpace::Rgb, black: LevelsChannel::default() }); + let pts = |v: &[(u8, u8)]| v.iter().map(|&(i, o)| CurvePoint { input: f32::from(i) / 255.0, output: f32::from(o) / 255.0 }).collect::>(); + rt(Adjustment::Curves { + master: pts(&[(0, 0), (128, 150), (255, 255)]), + per_channel: [pts(&[(0, 10), (255, 255)]), pts(&[(0, 0), (255, 245)]), pts(&[(0, 0), (64, 32), (255, 255)])], + space: ToneSpace::Rgb, + black: Vec::new(), + pencil: false, + }); + // CMYK ink curves and levels keep their black record. + let ink = Adjustment::Curves { + master: pts(&[(0, 0), (255, 255)]), + per_channel: [pts(&[(0, 0), (255, 255)]), pts(&[(0, 30), (255, 255)]), pts(&[(0, 0), (255, 255)])], + space: ToneSpace::Cmyk, + black: pts(&[(0, 0), (128, 100), (255, 255)]), + pencil: false, + }; + let b = write(&ink); + assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Cmyk), ink); + let inkl = Adjustment::Levels { master: lc(0, 255), per_channel: [lc(0, 255), lc(5, 250), lc(0, 255)], space: ToneSpace::Cmyk, black: lc(30, 255) }; + let b = write(&inkl); + assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Cmyk), inkl); + rt(Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![1, 2, 3].into() }); + rt(Adjustment::GradientMap { stops: vec![(0.0, [0.0, 0.0, 0.0]), (0.5, [1.0, 0.0, 0.0]), (1.0, [1.0, 1.0, 1.0])], reverse: true, dither: false }); + rt(Adjustment::GradientMap { stops: vec![(0.0, [0.0, 0.0, 0.0]), (1.0, [1.0, 1.0, 1.0])], reverse: false, dither: true }); + rt(Adjustment::SelectiveColor { relative: true, adjustments: std::array::from_fn(|r| [r as f32 * 10.0 - 40.0, 5.0, -100.0, 100.0]) }); + rt(Adjustment::SelectiveColor { relative: false, adjustments: [[0.0; 4]; 9] }); + rt(Adjustment::Vibrance { vibrance: 35.0, saturation: -12.0 }); + rt(Adjustment::Vibrance { vibrance: 0.0, saturation: 0.0 }); + rt(Adjustment::ColorBalance { shadows: [20.0, -10.0, 5.0], midtones: [-15.0, 10.0, 30.0], highlights: [0.0, 5.0, -100.0], preserve_luminosity: true }); + rt(Adjustment::ColorBalance { shadows: [0.0; 3], midtones: [100.0, 0.0, 0.0], highlights: [0.0; 3], preserve_luminosity: false }); + rt(Adjustment::BlackWhite { weights: [40.0, 60.0, 40.0, 60.0, 20.0, 80.0], tint: None }); + rt(Adjustment::BlackWhite { weights: [-200.0, 300.0, 0.0, 10.0, 90.0, 30.0], tint: Some([0.9, 0.7, 0.5]) }); + let q = |v: u16| f32::from(v) / 65535.0; + rt(Adjustment::PhotoFilter { color: [q(60000), q(30000), q(0)], density: 0.25, preserve_luminosity: true }); + rt(Adjustment::PhotoFilter { color: [q(0), q(40000), q(65535)], density: 1.0, preserve_luminosity: false }); + rt(Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false }); + rt(Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.1], [0.0, 1.0, 0.0, 0.0], [-2.0, 0.0, 2.0, 0.0]], monochrome: true }); + let id = photocraft_cms::lutfile::LutFile::identity(5); + let cube = photocraft_cms::lutfile::write_cube(&id).into_bytes(); + let source = Some(LutSource { format: LutFormat::Cube, bytes: std::sync::Arc::new(cube) }); + rt(Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(std::sync::Arc::new(id.data)), size: 5, tetrahedral: false, dither: true, source }); +} + +#[test] +fn selective_color_synthetic_block() { + // Absolute; reds = (+10, -20, +30, -40), blacks = (0, 0, 0, 25). + let mut d = vec![0, 1, 0, 1]; + d.extend([0u8; 8]); + for r in 0..9 { + let rec: [i16; 4] = match r { + 0 => [10, -20, 30, -40], + 8 => [0, 0, 0, 25], + _ => [0; 4], + }; + for x in rec { + d.extend(x.to_be_bytes()); + } + } + let a = parse(b"selc", &d, None, Channels::Rgb); + let Adjustment::SelectiveColor { relative, adjustments } = &a else { panic!("{a:?}") }; + assert!(!relative); + assert_eq!(adjustments[0], [10.0, -20.0, 30.0, -40.0]); + assert_eq!(adjustments[8], [0.0, 0.0, 0.0, 25.0]); + // Written back byte-exact. + assert_eq!(write(&a)[0].1, d); +} + +/// A 2³ Lustre `.3dl` (12-bit, blue fastest) that inverts every colour. +fn inverting_3dl() -> Vec { + let mut text = String::from("# made by hand\n0 1023\n"); + for r in 0..2 { + for g in 0..2 { + for b in 0..2 { + text.push_str(&format!("{} {} {}\n", (1 - r) * 4095, (1 - g) * 4095, (1 - b) * 4095)); + } + } + } + text.into_bytes() +} + +/// A 2³ SpeedGrade `.look` holding the identity. +fn identity_look() -> Vec { + let hex: String = photocraft_cms::lutfile::LutFile::identity(2).data.iter().flat_map(|v| v.to_le_bytes()).map(|b| format!("{b:02X}")).collect(); + format!("\n\"2\"\"{hex}\"\n").into_bytes() +} + +/// A `clrL` block as Photoshop writes one for a loaded LUT file. +fn clrl_block(format: &str, bytes: &[u8], name: &str) -> Vec { + let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; + let text = |t: &str| Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul(t)); + let desc = Descriptor::new("null") + .with("lookupType", en("colorLookupType", "3DLUT")) + .with("NM ", text(name)) + .with("LUTFormat", en("LUTFormatType", format)) + .with("LUT3DFileData", Value::RawData(bytes.to_vec())) + .with("LUT3DFileName", text(name)); + let mut d = vec![0, 1]; + d.extend(VersionedDescriptor::new(desc).to_bytes()); + d +} + +/// The descriptor of the `clrL` block `a` is written as. +fn written_clrl(a: &Adjustment) -> Descriptor { + let b = write(a); + assert_eq!(&b[0].0, b"clrL"); + VersionedDescriptor::parse_prefix(&b[0].1[2..]).unwrap().0.descriptor +} + +fn raw<'a>(d: &'a Descriptor, key: &str) -> &'a [u8] { + let Some(Value::RawData(b)) = d.get(key) else { panic!("{key} missing") }; + b +} + +#[test] +fn color_lookup_keeps_a_3dl_or_look_file_as_loaded() { + // #6544: Photoshop embeds the LUT file as loaded and tags its format; it never converts it. + for (format, kind, bytes, name) in + [("LUTFormat3DL", LutFormat::ThreeDl, inverting_3dl(), "Invert.3DL"), ("LUTFormatLOOK", LutFormat::Look, identity_look(), "Grade.look")] + { + let a = parse(b"clrL", &clrl_block(format, &bytes, name), None, Channels::Rgb); + let Adjustment::ColorLookup { name: n, lut: Some(_), size: 2, source: Some(src), .. } = &a else { panic!("{a:?}") }; + assert_eq!((n.as_str(), src.format, src.bytes.as_slice()), (name, kind, bytes.as_slice())); + let d = written_clrl(&a); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(format.as_bytes()), "{name}"); + assert_eq!(raw(&d, "LUT3DFileData"), bytes.as_slice(), "{name}: the file's bytes, unchanged"); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some(name)); + assert_eq!(desc_text(&d, "NM ").as_deref(), Some(name)); + // And it reads back as the same layer. + let b = write(&a); + assert_eq!(parse(&b[0].0, &b[0].1, None, Channels::Rgb), a); + } +} + +#[test] +fn color_lookup_table_without_matching_file_is_saved_as_cube_named_to_match() { + let a = parse(b"clrL", &clrl_block("LUTFormat3DL", &inverting_3dl(), "Invert.3DL"), None, Channels::Rgb); + let Adjustment::ColorLookup { name, size, tetrahedral, dither, source, .. } = a else { panic!("{a:?}") }; + // The table no longer matches the stored file (changed in PhotoCraft): the bytes are stale. + let id = photocraft_cms::lutfile::LutFile::identity(2).data; + let edited = Adjustment::ColorLookup { name, lut: Some(std::sync::Arc::new(id.clone())), size, tetrahedral, dither, source }; + let d = written_clrl(&edited); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(&b"LUTFormatCUBE"[..])); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some("Invert.cube")); + assert_eq!(photocraft_cms::lutfile::parse("x.cube", raw(&d, "LUT3DFileData")).unwrap().data, id); + // A table made in PhotoCraft (no file) named like another format is also renamed. + let made = + Adjustment::ColorLookup { name: "Grade.look".into(), lut: Some(std::sync::Arc::new(id)), size: 2, tetrahedral: false, dither: false, source: None }; + let d = written_clrl(&made); + assert_eq!(desc_enum(&d, "LUTFormat"), Some(&b"LUTFormatCUBE"[..])); + assert_eq!(desc_text(&d, "LUT3DFileName").as_deref(), Some("Grade.cube")); + assert_eq!(desc_text(&d, "NM ").as_deref(), Some("Grade.cube")); + // An unreadable stored file falls back the same way instead of failing the save. + let junk = Adjustment::ColorLookup { + name: "Junk.3dl".into(), + lut: Some(std::sync::Arc::new(photocraft_cms::lutfile::LutFile::identity(2).data)), + size: 2, + tetrahedral: false, + dither: false, + source: Some(LutSource { format: LutFormat::ThreeDl, bytes: std::sync::Arc::new(b"\xFF not a lut".to_vec()) }), + }; + assert_eq!(desc_text(&written_clrl(&junk), "LUT3DFileName").as_deref(), Some("Junk.cube")); + assert_eq!(cube_file_name("a.b.3dl"), "a.b.cube"); + assert_eq!(cube_file_name("Day for Night"), "Day for Night"); + assert_eq!(cube_file_name("x.cube"), "x.cube"); +} + +#[test] +fn color_lookup_synthetic_block() { + let cube = b"TITLE \"t\"\nLUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; + let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; + let desc = Descriptor::new("null") + .with("lookupType", en("colorLookupType", "3DLUT")) + .with("NM ", Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul("Id"))) + .with("Dthr", Value::Boolean(true)) + .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) + .with("LUT3DFileData", Value::RawData(cube.to_vec())); + let mut d = vec![0, 1]; + d.extend(VersionedDescriptor::new(desc).to_bytes()); + let a = parse(b"clrL", &d, None, Channels::Rgb); + let Adjustment::ColorLookup { name, lut: Some(t), size: 2, dither: true, .. } = &a else { panic!("{a:?}") }; + assert_eq!(name, "Id"); + assert_eq!(&t[..6], &[0.0, 0.0, 0.0, 1.0, 0.0, 0.0]); + // A profile-based lookup (no table) is preserved raw. + let mut p = vec![0, 1]; + p.extend(VersionedDescriptor::new(Descriptor::new("null").with("lookupType", en("colorLookupType", "abstractProfile"))).to_bytes()); + assert!(matches!(parse(b"clrL", &p, None, Channels::Rgb), Adjustment::Unsupported { .. })); + // Each way a lookup can't be read has a reason for the import warning (#1763). + assert_eq!(unreadable_reason(b"clrL", &d), None); + assert!(unreadable_reason(b"clrL", &p).unwrap().contains("colour profile")); + assert!(unreadable_reason(b"clrL", &[0]).unwrap().contains("truncated")); + assert!(unreadable_reason(b"clrL", &[0, 2, 0, 0]).unwrap().contains("version 2")); + assert!(unreadable_reason(b"clrL", &[0, 1, 9]).unwrap().contains("unreadable settings")); + let bad = Descriptor::new("null").with("LUTFormat", en("LUTFormatType", "LUTFormat3DL")).with("LUT3DFileData", Value::RawData(b"not a lut".to_vec())); + let mut b = vec![0, 1]; + b.extend(VersionedDescriptor::new(bad).to_bytes()); + assert!(matches!(parse(b"clrL", &b, None, Channels::Rgb), Adjustment::Unsupported { .. })); + assert!(unreadable_reason(b"clrL", &b).unwrap().contains("3DL LUT can't be read"), "{:?}", unreadable_reason(b"clrL", &b)); + assert_eq!(label(b"clrL"), "Color Lookup"); + assert!(matches!(parse(b"clrL", &[0, 1, 9], None, Channels::Rgb), Adjustment::Unsupported { .. })); + assert!(matches!(parse(b"selc", &[0, 1, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); +} + +fn cube_lookup_block(cube: &[u8]) -> Vec { + let en = |t: &str, val: &str| Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new(t), value: photocraft_psd::descriptor::Id::new(val) }; + let desc = Descriptor::new("null") + .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) + .with("LUT3DFileName", Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul("domain.cube"))) + .with("LUT3DFileData", Value::RawData(cube.to_vec())); + let mut block = vec![0, 1]; + block.extend(VersionedDescriptor::new(desc).to_bytes()); + block +} + +fn identity_cube(domain: &str) -> Vec { + format!("{domain}LUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n").into_bytes() +} + +#[test] +fn color_lookup_custom_domains_are_kept_unsupported() { + let domains = ["DOMAIN_MIN 0 0 0\nDOMAIN_MAX 0.5 0.5 0.5\n", "DOMAIN_MIN 0 0.25 0\nDOMAIN_MAX 1 0.75 1\n", "LUT_3D_INPUT_RANGE 0 0.5\n"]; + for domain in domains { + let raw = cube_lookup_block(&identity_cube(domain)); + let Adjustment::Unsupported { psd_key, raw: kept } = parse(b"clrL", &raw, None, Channels::Rgb) else { + panic!("custom LUT domain was applied instead of kept unsupported: {domain}") + }; + assert_eq!(psd_key, "clrL"); + assert_eq!(*kept, *raw); + let reason = unreadable_reason(b"clrL", &raw).expect("custom domain has an explanation"); + assert!(reason.contains("DOMAIN_MIN") && reason.contains("DOMAIN_MAX"), "{reason}"); + assert!(reason.to_ascii_lowercase().contains("not supported"), "{reason}"); + } +} + +#[test] +fn color_lookup_default_domains_stay_supported() { + for domain in ["", "DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\n"] { + let raw = cube_lookup_block(&identity_cube(domain)); + assert!(matches!(parse(b"clrL", &raw, None, Channels::Rgb), Adjustment::ColorLookup { size: 2, .. })); + assert_eq!(unreadable_reason(b"clrL", &raw), None); + } +} + +#[test] +fn malformed_falls_back_to_unsupported() { + malformed_adjustments_are_unsupported(); + malformed_channel_mixers_are_unsupported(); + malformed_photo_filters_are_unsupported(); + arbitrary_adjustment_bytes_do_not_panic(); +} + +fn malformed_adjustments_are_unsupported() { + for (key, bytes) in [ + (&b"levl"[..], &[0, 2, 1][..]), + (&b"curv"[..], &[0, 0, 9][..]), + (&b"thrs"[..], &[][..]), + (&b"blnc"[..], &[0; 18][..]), + (&b"vibA"[..], &[0, 0, 0, 15, 0][..]), + (&b"blwh"[..], &[][..]), + (&b"mixr"[..], &[0, 2, 0, 0][..]), + ] { + let key: &[u8; 4] = key.try_into().unwrap_or(b"????"); + assert!(matches!(parse(key, bytes, None, Channels::Rgb), Adjustment::Unsupported { .. }), "{key:?}"); + } +} + +fn malformed_channel_mixers_are_unsupported() { + let mut cmyk = i16s(&[1, 0]); + for r in 0..4 { + cmyk.extend(i16s(&std::array::from_fn::(|k| if k == r { 100 } else { 0 }))); + } + for channels in [Channels::Cmyk, Channels::Other] { + assert!(matches!(parse(b"mixr", &cmyk, None, channels), Adjustment::Unsupported { .. })); + } + let ours = write(&Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: false }); + for channels in [Channels::Cmyk, Channels::Other] { + assert!(matches!(parse(b"mixr", &ours[0].1, None, channels), Adjustment::ChannelMixer { .. })); + } + assert!(matches!(parse(b"mixr", &[0, 1, 0, 0, 0, 100], None, Channels::Rgb), Adjustment::Unsupported { .. })); +} + +fn malformed_photo_filters_are_unsupported() { + assert!(matches!(parse(b"phfl", &[0, 4, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); + assert!(matches!(parse(b"phfl", &[0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 1], None, Channels::Rgb), Adjustment::Unsupported { .. })); + for bad in [[200_000u32, 0, 0], [0, u32::MAX, 0]] { + let mut xyz = vec![0, 3]; + for x in bad { + xyz.extend(x.to_be_bytes()); + } + xyz.extend([0, 0, 0, 25, 1]); + assert!(matches!(parse(b"phfl", &xyz, None, Channels::Rgb), Adjustment::Unsupported { .. })); + } + assert!(matches!(parse(b"phfl", &[0, 2, 0, 0, 0, 0], None, Channels::Rgb), Adjustment::Unsupported { .. })); +} + +fn arbitrary_adjustment_bytes_do_not_panic() { + for key in ADJUSTMENT_KEYS { + for len in 0..64usize { + let data: Vec = (0..len).map(|i| (i * 37 + len) as u8).collect(); + let _ = parse(key, &data, Some(&data), Channels::Rgb); + let _ = parse(key, &data, None, Channels::Other); + } + } +} + +fn i16s(v: &[i16]) -> Vec { + v.iter().flat_map(|x| x.to_be_bytes()).collect() +} + +/// Hand-built blocks laid out per the Adobe spec (no file written by Photoshop involved). +#[test] +fn color_balance_golden_block() { + let mut d = i16s(&[-30, 20, 0, 10, -5, 100, 0, 0, -100]); + d.extend([1, 0]); + let a = parse(b"blnc", &d, None, Channels::Rgb); + assert_eq!( + a, + Adjustment::ColorBalance { shadows: [-30.0, 20.0, 0.0], midtones: [10.0, -5.0, 100.0], highlights: [0.0, 0.0, -100.0], preserve_luminosity: true } + ); + assert_eq!(write(&a), vec![(*b"blnc", d.clone())]); + // The 19-byte form (no trailing pad) parses the same. + assert_eq!(parse(b"blnc", &d[..19], None, Channels::Rgb), a); +} + +#[test] +fn photo_filter_golden_blocks() { + // Version 2, RGB colour structure (65535, 32768, 0), density 40 %, preserve luminosity. + let mut v2 = i16s(&[2, 0]); + v2.extend([0xff, 0xff, 0x80, 0x00, 0, 0, 0, 0]); + v2.extend(40u32.to_be_bytes()); + v2.extend([1, 0, 0, 0]); + assert_eq!(v2.len(), 20, "Photo Filter must occupy 20 bytes"); + assert_eq!(&v2[17..], &[0, 0, 0], "padding is included inside the block length"); + let a = parse(b"phfl", &v2, None, Channels::Rgb); + let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = a else { panic!("{a:?}") }; + assert_eq!(color, [1.0, 32768.0 / 65535.0, 0.0]); + assert_eq!(density, 0.4); + assert_eq!(write(&a)[0].1, v2, "version 2 RGB is written back byte-exact"); + // Older PhotoCraft files used only one padding byte (18 bytes); keep importing them, + // but never produce that Photoshop-incompatible length again. + assert_eq!(parse(b"phfl", &v2[..18], None, Channels::Rgb), a); + for preserve_luminosity in [false, true] { + for density in [0.0, 0.14, 1.0] { + let adjustment = Adjustment::PhotoFilter { color: [0.8, 0.4, 0.2], density, preserve_luminosity }; + let blocks = write(&adjustment); + assert_eq!(blocks.len(), 1); + assert_eq!(blocks[0].0, *b"phfl"); + assert_eq!(blocks[0].1.len(), 20); + assert_eq!(&blocks[0].1[17..], &[0, 0, 0]); + let Adjustment::PhotoFilter { color, density: read_density, preserve_luminosity: read_preserve } = + parse(b"phfl", &blocks[0].1, None, Channels::Rgb) + else { + panic!("written phfl must parse") + }; + assert!(color.iter().zip([0.8, 0.4, 0.2]).all(|(got, expected)| (got - expected).abs() <= 1.0 / 65535.0)); + assert!((read_density - density).abs() < 0.00001); + assert_eq!(read_preserve, preserve_luminosity); + } + } + // Version 2, Lab colour structure: L 50, a 0, b 0 is mid gray. + let mut lab = i16s(&[2, 7, 5000, 0, 0, 0]); + lab.extend(25u32.to_be_bytes()); + lab.push(0); + let Adjustment::PhotoFilter { color, density, preserve_luminosity: false } = parse(b"phfl", &lab, None, Channels::Rgb) else { panic!() }; + assert!(color.iter().all(|c| (c - 0.466).abs() < 0.01), "{color:?}"); + assert_eq!(density, 0.25); + // Version 3: XYZ relative to the D50 white, 1.0 = 32768. White is (32768, 32768, 32768). + let v3 = phfl_v3([32768, 32768, 32768], 100, true); + let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = parse(b"phfl", &v3, None, Channels::Rgb) else { panic!() }; + assert!(color.iter().all(|c| (c - 1.0).abs() < 0.005), "{color:?}"); + assert_eq!(density, 1.0); + // Photoshop's Warming Filter (85) preset, sRGB #EC8A00: the CS2 file of #4940 stores it + // relative to the D50 white as (15548, 12029, 0). + let Adjustment::PhotoFilter { color, density, preserve_luminosity: false } = parse(b"phfl", &phfl_v3([15548, 12029, 0], 25, false), None, Channels::Rgb) + else { + panic!() + }; + let expected = [0xecu8, 0x8a, 0x00].map(|v| f32::from(v) / 255.0); + assert!(color.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{color:?}"); + assert_eq!(density, 0.25); + // The block from #3777 (Photoshop CS2): the Yellow filter preset, #F9E31C, density 63 %. + let issue = [0, 3, 0, 0, 0x5e, 0x7f, 0, 0, 0x61, 0xaa, 0, 0, 0x0e, 0xf3, 0, 0, 0, 0x3f, 1, 0]; + let Adjustment::PhotoFilter { color, density, preserve_luminosity: true } = parse(b"phfl", &issue, None, Channels::Rgb) else { panic!() }; + let expected = [0xf9u8, 0xe3, 0x1c].map(|v| f32::from(v) / 255.0); + assert!(color.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{color:?}"); + assert_eq!(density, 0.63); + // Cooling Filter (82) from #4940: a deep blue outside sRGB; its Z stays under the 2.0 + // garbage guard and the colour clamps to (0, 181, 255). + let Adjustment::PhotoFilter { color: cooling, density, preserve_luminosity: true } = + parse(b"phfl", &phfl_v3([5327, 9584, 65357], 72, true), None, Channels::Rgb) + else { + panic!("Cooling Filter (82) must parse") + }; + let expected = [0u8, 181, 255].map(|v| f32::from(v) / 255.0); + assert!(cooling.iter().zip(expected).all(|(got, want)| (got - want).abs() < 1.5 / 255.0), "{cooling:?}"); + assert_eq!(density, 0.72); + // The 19-byte form (no trailing pad) parses the same. + assert_eq!(parse(b"phfl", &issue[..19], None, Channels::Rgb), parse(b"phfl", &issue, None, Channels::Rgb)); + // Version 3 is read, never written: an edited filter is saved as version 2 and keeps its colour. + let a = parse(b"phfl", &issue, None, Channels::Rgb); + let w = write(&a); + assert_eq!(&w[0].1[..2], &[0, 2]); + let Adjustment::PhotoFilter { color: back, .. } = parse(b"phfl", &w[0].1, None, Channels::Rgb) else { panic!() }; + assert!(back.iter().zip(color).all(|(b, c)| (b - c).abs() <= 1.0 / 65535.0)); +} + +/// A version 3 Photo Filter block: version, three u32 XYZ components, density, flag, pad. +fn phfl_v3(xyz: [u32; 3], density: u32, preserve_luminosity: bool) -> Vec { + let mut d = vec![0, 3]; + for x in xyz { + d.extend(x.to_be_bytes()); + } + d.extend(density.to_be_bytes()); + d.extend([u8::from(preserve_luminosity), 0]); + d +} + +#[test] +fn channel_mixer_golden_blocks() { + // RGB: red = 100 % R + 20 % const; green = 50 % R + 50 % G; blue = -50 % G + 150 % B - 10 % const. + let mut d = i16s(&[1, 0]); + d.extend(i16s(&[100, 0, 0, 0, 20, 50, 50, 0, 0, 0, 0, -50, 150, 0, -10])); + let a = parse(b"mixr", &d, None, Channels::Rgb); + assert_eq!(a, Adjustment::ChannelMixer { matrix: [[1.0, 0.0, 0.0, 0.2], [0.5, 0.5, 0.0, 0.0], [0.0, -0.5, 1.5, -0.1]], monochrome: false }); + // Written with the trailing gray record (a copy of the first row). + let w = write(&a); + assert_eq!(w[0].1[..d.len()], d[..]); + assert_eq!(w[0].1[d.len()..], i16s(&[100, 0, 0, 0, 20])[..]); + // Monochrome with only the gray record: 30/59/11. + let mut m = i16s(&[1, 1]); + m.extend(i16s(&[30, 59, 11, 0, 0])); + let Adjustment::ChannelMixer { matrix, monochrome: true } = parse(b"mixr", &m, None, Channels::Rgb) else { panic!() }; + assert_eq!(matrix[0], [0.3, 0.59, 0.11, 0.0]); +} + +fn vdesc(d: Descriptor) -> Vec { + VersionedDescriptor::new(d).to_bytes() +} + +#[test] +fn vibrance_golden_block() { + let d = vdesc(Descriptor::new("null").with("vibrance", Value::Integer(-40)).with("Strt", Value::Integer(25))); + assert_eq!(&d[..4], &[0, 0, 0, 16], "descriptor version 16"); + assert_eq!(parse(b"vibA", &d, None, Channels::Rgb), Adjustment::Vibrance { vibrance: -40.0, saturation: 25.0 }); + // Photoshop omits zero values. + let only = vdesc(Descriptor::new("null").with("Strt", Value::Integer(10))); + assert_eq!(parse(b"vibA", &only, None, Channels::Rgb), Adjustment::Vibrance { vibrance: 0.0, saturation: 10.0 }); + // Other descriptor versions are rejected (kept raw). + let mut v = d.clone(); + v[3] = 17; + assert!(matches!(parse(b"vibA", &v, None, Channels::Rgb), Adjustment::Unsupported { .. })); +} + +#[test] +fn black_white_golden_block() { + let tint = Descriptor::new("RGBC").with("Rd ", Value::Double(255.0)).with("Grn ", Value::Double(127.5)).with("Bl ", Value::Double(0.0)); + let mut desc = Descriptor::new("null"); + for (k, w) in BW_KEYS.iter().zip([10, 20, 30, 40, 50, 60]) { + desc = desc.with(k, Value::Integer(w)); + } + let d = + vdesc(desc.clone().with("useTint", Value::Boolean(true)).with("tintColor", Value::Descriptor(tint.clone())).with("bwPresetKind", Value::Integer(1))); + let a = parse(b"blwh", &d, None, Channels::Rgb); + assert_eq!(a, Adjustment::BlackWhite { weights: [10.0, 20.0, 30.0, 40.0, 50.0, 60.0], tint: Some([1.0, 0.5, 0.0]) }); + // useTint off: no tint, even with a stored colour. + let off = vdesc(desc.with("useTint", Value::Boolean(false)).with("tintColor", Value::Descriptor(tint))); + assert!(matches!(parse(b"blwh", &off, None, Channels::Rgb), Adjustment::BlackWhite { tint: None, .. })); + // Missing weights take Photoshop's defaults. + let empty = vdesc(Descriptor::new("null")); + assert_eq!(parse(b"blwh", &empty, None, Channels::Rgb), Adjustment::BlackWhite { weights: BW_DEFAULT, tint: None }); +} + +#[test] +fn degenerate_adjustments_still_get_an_encoding() { + // Gradient maps with fewer than two stops render as gray (none) or one colour. + for (stops, want) in + [(vec![], vec![(0.0, [0.0; 3]), (1.0, [1.0; 3])]), (vec![(0.3, [0.2, 0.4, 0.6])], vec![(0.0, [0.2, 0.4, 0.6]), (1.0, [0.2, 0.4, 0.6])])] + { + let b = write(&Adjustment::GradientMap { stops, reverse: true, dither: false }); + let back = parse(&b[0].0, &b[0].1, None, Channels::Rgb); + let Adjustment::GradientMap { stops, reverse: true, .. } = back else { panic!("{back:?}") }; + assert_eq!(stops.len(), want.len()); + for (s, w) in stops.iter().zip(&want) { + assert!((s.0 - w.0).abs() < 1e-6 && (0..3).all(|k| (s.1[k] - w.1[k]).abs() < 1e-4), "{s:?} vs {w:?}"); + } + } + // A colour lookup without a (usable) table becomes an identity cube, without dither. + for lut in [None, Some(std::sync::Arc::new(vec![0.5; 7]))] { + let b = write(&Adjustment::ColorLookup { name: "L".into(), lut, size: 3, tetrahedral: false, dither: true, source: None }); + let back = parse(&b[0].0, &b[0].1, None, Channels::Rgb); + let Adjustment::ColorLookup { lut: Some(t), size: 2, dither: false, name, .. } = back else { panic!("{back:?}") }; + assert_eq!(name, "L"); + assert_eq!(*t, photocraft_cms::lutfile::LutFile::identity(2).data); + } + // Every kind except a malformed Unsupported key has an encoding. + assert!(write(&Adjustment::Unsupported { psd_key: "abc".into(), raw: Vec::new().into() }).is_empty()); +} + +#[test] +fn non_rgb_ignores_channel_records() { + let lc = LevelsChannel { in_black: 0.1, ..Default::default() }; + let a = Adjustment::Levels { + master: LevelsChannel::default(), + per_channel: [lc.clone(), lc.clone(), lc], + space: ToneSpace::Rgb, + black: LevelsChannel::default(), + }; + let b = write(&a); + match parse(&b[0].0, &b[0].1, None, Channels::Other) { + Adjustment::Levels { per_channel, .. } => assert_eq!(per_channel, <[LevelsChannel; 3]>::default()), + other => panic!("{other:?}"), + } +} + +#[test] +fn gray_uses_first_channel_record_for_all() { + let lc = LevelsChannel { in_black: 44.0 / 255.0, ..Default::default() }; + let a = Adjustment::Levels { + master: LevelsChannel::default(), + per_channel: [lc.clone(), Default::default(), Default::default()], + space: ToneSpace::Rgb, + black: LevelsChannel::default(), + }; + let b = write(&a); + match parse(&b[0].0, &b[0].1, None, Channels::Gray) { + Adjustment::Levels { per_channel, .. } => assert_eq!(per_channel, [lc.clone(), lc.clone(), lc]), + other => panic!("{other:?}"), + } +} + +#[test] +fn legacy_brit_without_cged() { + let a = parse(b"brit", &[0, 10, 0xff, 0xf6, 0, 127, 0], None, Channels::Rgb); + assert_eq!(a, Adjustment::BrightnessContrast { brightness: 10.0, contrast: -10.0, legacy: true }); +} diff --git a/crates/io/src/affinity.rs b/crates/io/src/affinity.rs index efba868be0..4e5ed90f86 100644 --- a/crates/io/src/affinity.rs +++ b/crates/io/src/affinity.rs @@ -28,6 +28,10 @@ pub const EXTENSIONS: &[&str] = &["af", "afphoto", "afdesign", "afpub"]; const MAX_PIXELS: u64 = 64 << 20; /// Canvas side in pixels. const MAX_SIDE: f64 = 300_000.0; +/// Decoded pixel bytes allowed across the whole import: the caps above are per call, so a small +/// container of embedded images could still decode (and keep) a surface each until the process +/// dies. ORA and KRA budget their decoded pixels the same way. +const MAX_PIXEL_BYTES: u64 = 1 << 30; pub fn has_extension(name: &str) -> bool { name.rsplit(['/', '\\']).next().and_then(|n| n.rsplit_once('.')).is_some_and(|(_, ext)| EXTENSIONS.iter().any(|e| ext.eq_ignore_ascii_case(e))) @@ -43,7 +47,7 @@ pub(crate) fn import(name: &str, bytes: &[u8]) -> Result // Type layers draw from their model, within the import's raster bound: the sizes and // frames come from an untrusted file. let mut document = b.doc; - crate::text_import::prepare(&mut document); + crate::text_import::prepare(&mut document, None); // Save never writes back over the Affinity file. Ok(ImportResult { document, warnings, source_read_only: true, preview_only: false }) } @@ -82,6 +86,20 @@ struct Builder { /// Document pixels (in the canvas) per Affinity document pixel: always 1; spreads are offset. format: PixelFormat, canvas: Rect, + /// Decoded pixel bytes left for embedded images and their resampled surfaces. + pixel_budget: u64, +} + +/// Deduct `cost` bytes from a decoded-pixel budget; `false` (leaving the budget alone) when it +/// cannot cover them. +fn charge(budget: &mut u64, cost: u64) -> bool { + match budget.checked_sub(cost) { + Some(rest) => { + *budget = rest; + true + } + None => false, + } } fn affine(a: af::Affine) -> Affine { @@ -108,7 +126,7 @@ impl Builder { let mut doc = Document::new(name, size, ColorMode::Rgb, SampleType::U8); doc.resolution_dpi = d.dpi as f32; let canvas = Rect::new(0, 0, size.width as i32, size.height as i32); - Ok(Self { doc, warnings: Vec::new(), format: PixelFormat::RGBA8, canvas }) + Ok(Self { doc, warnings: Vec::new(), format: PixelFormat::RGBA8, canvas, pixel_budget: MAX_PIXEL_BYTES }) } fn warn(&mut self, w: &str) { @@ -131,7 +149,7 @@ impl Builder { let r = self.bbox(to_doc, page); let mut g = Layer::new( format!("Page {}", i + 1), - LayerContent::Group(Group { children: std::mem::take(&mut children), expanded: true, artboard: None }), + LayerContent::Group(Group { children: std::mem::take(&mut children), expanded: true, artboard: None, frame_stroke: None }), ); if let LayerContent::Group(gr) = &mut g.content { gr.artboard = Some(Artboard { rect: r, background: ArtboardBackground::White, preset: String::new() }); @@ -226,7 +244,7 @@ impl Builder { fn group(&mut self, n: &af::Node, children: Vec) -> Layer { // Pass-through and Normal (isolated) groups map one to one. - self.base(n, LayerContent::Group(Group { children, expanded: false, artboard: None })) + self.base(n, LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None })) } /// One Affinity node and its subtree; `to_doc` maps Affinity document pixels to the canvas. @@ -424,8 +442,18 @@ impl Builder { dash_offset: s.dash.as_ref().map_or(0.0, |(_, o)| (o / s.width.max(1e-9)) as f32), }) }); - let mut sh = ShapeLayer { path, fill, stroke, live: None, cache: None, psd_raw: None }; - sh.cache = Some(photocraft_vector::render_shape(&sh, self.format, self.canvas)); + let mut sh = ShapeLayer { path, fill, stroke, ..ShapeLayer::default() }; + // The pixel cache is optional (the shape renders from its path without it), so charge the + // area it would cover to the import's pixel budget and go without it when that is spent: + // geometry across a 300000 px canvas would otherwise be rasterized for every shape. + let reach = sh.stroke.as_ref().map_or(0, |s| (s.width.ceil() as i32).saturating_add(1)); + let area = frame.inflate(reach).intersect(&self.canvas); + let cost = u64::from(area.width()).saturating_mul(u64::from(area.height())).saturating_mul(self.format.bytes_per_pixel() as u64); + if charge(&mut self.pixel_budget, cost) { + sh.cache = Some(photocraft_vector::render_shape(&sh, self.format, self.canvas)); + } else { + self.warn("shapes covering more pixels than the import's memory allows were left without a pixel cache"); + } sh } @@ -494,6 +522,10 @@ impl Builder { match photocraft_codecs::decode_with(bytes, &opts) { Ok(d) => { let (w, h) = d.dimensions(); + if !charge(&mut self.pixel_budget, u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4)) { + self.warn("the document's embedded images need more pixel memory than the import allows; the rest were left out"); + return None; + } Some((w, h, d.to_rgba8())) } Err(_) => { @@ -548,6 +580,10 @@ impl Builder { let exact = a == 1.0 && d == 1.0 && b == 0.0 && c == 0.0 && e.fract() == 0.0 && f.fract() == 0.0; let inv = m.inverse()?; let (rw, rh) = (r.width() as usize, r.height() as usize); + if !charge(&mut self.pixel_budget, (rw as u64).saturating_mul(rh as u64).saturating_mul(channels as u64)) { + self.warn("the document's images need more pixel memory than the import allows; the rest were left out"); + return None; + } let mut buf = vec![0u8; rw * rh * channels]; let at = |x: i64, y: i64, ch: usize| -> f64 { if x < 0 || y < 0 || x >= i64::from(w) || y >= i64::from(h) { @@ -619,3 +655,75 @@ fn color(c: ap::Color) -> Color { } } } + +#[cfg(test)] +mod tests { + use super::*; + + /// A transparent 64×64 page holding one image node per entry; the identity transform maps + /// each image onto the page's top-left corner. + fn affinity_document(images: Vec<(u32, u32, Pixels)>) -> af::Document { + let nodes = images + .into_iter() + .enumerate() + .map(|(i, (width, height, pixels))| af::Node { + class: photocraft_affinity::synth::tag(b"ImgN"), + name: format!("Image {i}"), + visible: true, + locked: false, + opacity: 1.0, + blend: Blend::Normal, + kind: Kind::Image(af::Image { width, height, pixels, transform: af::Affine::IDENTITY }), + mask: None, + pixel_mask: None, + children: Vec::new(), + }) + .collect(); + af::Document { + dpi: 72.0, + spreads: vec![af::Spread { bounds: af::Rect { x0: 0.0, y0: 0.0, x1: 64.0, y1: 64.0 }, pages: Vec::new(), transparent: true, nodes }], + saved_by: None, + warnings: Vec::new(), + } + } + + #[test] + fn the_pixel_budget_refuses_what_does_not_fit() { + let mut budget = 100u64; + assert!(charge(&mut budget, 60)); + assert!(!charge(&mut budget, 60), "a later image cannot overdraw the import's budget"); + assert_eq!(budget, 40, "a refused charge leaves the budget alone"); + } + + #[test] + fn images_past_the_pixel_budget_are_left_out() { + // Two 64×64 RGBA images cost 16 KiB each when resampled; a 16 KiB budget admits one and + // the second is dropped with a warning instead of being staged anyway. + let d = affinity_document(vec![(64, 64, Pixels::Rgba8(vec![0; 64 * 64 * 4])), (64, 64, Pixels::Rgba8(vec![0; 64 * 64 * 4]))]); + let mut b = Builder::new("budget", &d).unwrap(); + b.pixel_budget = 64 * 64 * 4; + b.document(&d).unwrap(); + assert_eq!(b.doc.layers.len(), 1, "the image past the budget must not import"); + assert!(b.warnings.iter().any(|w| w.contains("pixel memory")), "{:?}", b.warnings); + } + + #[test] + fn embedded_images_past_the_pixel_budget_are_not_decoded_into_it() { + // An 8×8 embedded PNG costs 256 bytes decoded and 256 more resampled. + let png = { + let img = photocraft_codecs::Image::from_u8(8, 8, photocraft_codecs::ChannelLayout::Rgba, vec![0; 8 * 8 * 4]).unwrap(); + photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &photocraft_codecs::EncodeOptions::default()).unwrap() + }; + let d = affinity_document(vec![(8, 8, Pixels::Encoded(png))]); + let mut b = Builder::new("budget", &d).unwrap(); + b.pixel_budget = 8 * 8 * 4 - 1; + b.document(&d).unwrap(); + assert!(b.doc.layers.is_empty(), "the embedded image must not import past the budget"); + assert!(b.warnings.iter().any(|w| w.contains("pixel memory")), "{:?}", b.warnings); + // The same image imports when the budget covers the decode and the resample. + let mut ok = Builder::new("room", &d).unwrap(); + ok.pixel_budget = 8 * 8 * 4 * 2; + ok.document(&d).unwrap(); + assert_eq!(ok.doc.layers.len(), 1, "within the budget the image imports as a smart object"); + } +} diff --git a/crates/io/src/asl_map.rs b/crates/io/src/asl_map.rs new file mode 100644 index 0000000000..76e9e11757 --- /dev/null +++ b/crates/io/src/asl_map.rs @@ -0,0 +1,410 @@ +//! Layer style libraries (`.asl`) ⇄ PhotoCraft styles. +//! +//! The container is parsed by [`photocraft_psd::asl`]. Each style's `Lefx` is the same effects +//! tree as a layer's `lfx2` block ([`crate::effects_map`]); `blendOptions` ("Include Layer +//! Blending Options") carries the blend mode, opacity and fill opacity; the file's patterns feed +//! the styles' Pattern Overlays, pattern strokes and bevel textures. +//! +//! What PhotoCraft doesn't model (an effect kind it doesn't know, advanced blending keys, an +//! effects scale other than 100 %) is left out with a warning naming the style, and the rest of +//! the file still imports. + +use std::collections::{BTreeSet, HashMap, HashSet}; + +use photocraft_color::BlendMode; +use photocraft_doc::{Effect, FxPaint, Pattern}; +use photocraft_psd::asl::{self, AslFile}; +use photocraft_psd::descriptor::{Descriptor, Id, Value}; + +use crate::blocks::{enum_of, get_desc, num}; +use crate::effects_map::{blend_from_id, blend_value, effects_descriptor, effects_from_descriptor, is_known_effects_key}; +use crate::pattern_map::{from_psd, to_psd}; + +/// One style as PhotoCraft keeps it. +#[derive(Clone, Debug, PartialEq)] +pub struct AslStyle { + /// Unique within the file (empty names get "Style N", repeats get "Name 2"). + pub name: String, + /// The effects (switched off when the style's effects were). + pub effects: Vec, + /// Blending options, when the style was saved with them. + pub blend: Option, + /// 0..1. + pub opacity: Option, + /// 0..1. + pub fill_opacity: Option, +} + +/// The result of reading a library. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct AslImport { + pub styles: Vec, + /// Patterns embedded in the file. + pub patterns: Vec, + /// What was left out, for the user. + pub warnings: Vec, +} + +/// Patterns an effect list refers to: `(name, id)` in first-use order, without repeats. +pub fn referenced_patterns(effects: &[Effect]) -> Vec<(String, String)> { + let mut out: Vec<(String, String)> = Vec::new(); + let mut push = |name: &str, id: &str| { + if !out.iter().any(|(n, i)| if id.is_empty() { n == name } else { i == id }) { + out.push((name.to_string(), id.to_string())); + } + }; + for e in effects { + match e { + Effect::PatternOverlay { name, id, .. } => push(name, id), + Effect::Stroke(s) => { + if let FxPaint::Pattern { name, id, .. } = &s.paint { + push(name, id); + } + } + Effect::BevelEmboss(b) => { + if let Some(t) = &b.texture { + push(&t.name, &t.id); + } + } + _ => {} + } + } + out +} + +fn pct(d: &Descriptor, key: &str) -> Option { + num(d.get(key)).filter(|v| v.is_finite()).map(|v| (v as f32 / 100.0).clamp(0.0, 1.0)) +} + +/// Style names in use within one file (ASCII-lowercased), with the next number to try for each +/// repeated name, so a file of identical names stays linear. +#[derive(Default)] +struct Taken { + names: HashSet, + next: HashMap, +} + +impl Taken { + /// `name`, else `name 2`, `name 3`… (case-insensitive); in use from then on. + fn unique(&mut self, name: &str) -> String { + let key = name.to_ascii_lowercase(); + if self.names.insert(key.clone()) { + return name.to_string(); + } + let from = self.next.get(&key).copied().unwrap_or(2); + let i = (from..).find(|i| !self.names.contains(&format!("{key} {i}"))).unwrap_or(from); + self.names.insert(format!("{key} {i}")); + self.next.insert(key, i + 1); + format!("{name} {i}") + } +} + +/// Reads an `.asl` style library. +pub fn read_asl(bytes: &[u8]) -> Result { + let file = asl::parse(bytes).map_err(|e| format!("not a readable style library (.asl): {e}"))?; + let mut out = AslImport { patterns: file.patterns.iter().filter_map(from_psd).collect(), ..Default::default() }; + if let Some(e) = &file.pattern_error { + out.warnings.push(format!("the file's patterns could not be read and were skipped ({e})")); + } + let skipped_patterns = file.patterns.len().saturating_sub(out.patterns.len()); + if skipped_patterns > 0 { + out.warnings.push(format!("{skipped_patterns} empty or unreadable patterns were skipped")); + } + let mut taken = Taken::default(); + let mut missing: BTreeSet = BTreeSet::new(); + for (i, s) in file.styles.iter().enumerate() { + let base = if s.name.trim().is_empty() { format!("Style {}", i + 1) } else { s.name.trim().to_string() }; + let name = taken.unique(&base); + let mut style = AslStyle { name: name.clone(), effects: Vec::new(), blend: None, opacity: None, fill_opacity: None }; + let mut left_out: Vec = Vec::new(); + if let Some(fx) = get_desc(&s.style, "Lefx") { + let (master, mut effects) = effects_from_descriptor(fx); + if !master { + effects.iter_mut().for_each(|e| e.set_enabled(false)); + } + for (k, _) in &fx.items { + if !is_known_effects_key(k.as_bytes()) { + left_out.push(String::from_utf8_lossy(k.as_bytes()).trim().to_string()); + } + } + if let Some(scale) = num(fx.get("Scl ")).filter(|v| v.is_finite() && (v - 100.0).abs() > 0.01) { + left_out.push(format!("effects scale {scale:.0} %")); + } + style.effects = effects; + } + if let Some(b) = get_desc(&s.style, "blendOptions") { + style.blend = enum_of(b, "Md ").and_then(blend_from_id); + style.opacity = pct(b, "Opct"); + style.fill_opacity = pct(b, "fillOpacity"); + for (k, _) in &b.items { + if !matches!(k.as_bytes(), b"Md " | b"Opct" | b"fillOpacity") { + left_out.push(format!("blending option {}", String::from_utf8_lossy(k.as_bytes()).trim())); + } + } + } + for (pname, pid) in referenced_patterns(&style.effects) { + if !out.patterns.iter().any(|p| if pid.is_empty() { p.name == pname } else { p.id == pid }) { + missing.insert(if pname.is_empty() { pid } else { pname }); + } + } + if !left_out.is_empty() { + out.warnings.push(format!("\"{name}\": left out {}", left_out.join(", "))); + } + out.styles.push(style); + } + if !missing.is_empty() { + let list: Vec = missing.into_iter().collect(); + out.warnings.push(format!("patterns not in the file (they must be in the pattern library to render): {}", list.join(", "))); + } + Ok(out) +} + +/// A stable UUID-shaped id for a style (Photoshop gives every style one). +fn style_id(index: usize, name: &str) -> String { + // FNV-1a over the name, mixed with the index: deterministic output for deterministic tests. + let mut h: u128 = 0x6c62_272e_07bb_0142_62b8_2175_6295_c58d; + for b in name.bytes().chain(index.to_le_bytes()) { + h ^= u128::from(b); + h = h.wrapping_mul(0x0000_0000_0100_0000_0000_0000_0000_013b); + } + let x = format!("{h:032x}"); + let p = |a: usize, b: usize| x.get(a..b).unwrap_or("0"); + format!("{}-{}-{}-{}-{}", p(0, 8), p(8, 12), p(12, 16), p(16, 20), p(20, 32)) +} + +/// Writes styles (and the patterns their effects use) as an `.asl` style library. +pub fn write_asl(styles: &[AslStyle], patterns: &[Pattern]) -> Result, String> { + let pct = |v: f32| Value::UnitFloat { unit: *b"#Prc", value: f64::from(v.clamp(0.0, 1.0)) * 100.0 }; + let file = AslFile { + patterns: patterns.iter().map(to_psd).collect(), + pattern_error: None, + styles: styles + .iter() + .enumerate() + .map(|(i, s)| { + // A style library's effects object has class `Lefx` (a layer's `lfx2` uses `null`). + let mut lefx = effects_descriptor(true, &s.effects); + lefx.class_id = Id::new("Lefx"); + let mut d = + Descriptor::new("Styl").with("documentMode", Value::Descriptor(Descriptor::new("documentMode"))).with("Lefx", Value::Descriptor(lefx)); + if s.blend.is_some() || s.opacity.is_some() || s.fill_opacity.is_some() { + d = d.with( + "blendOptions", + Value::Descriptor( + Descriptor::new("blendOptions") + .with("Md ", blend_value(s.blend.unwrap_or(BlendMode::Normal))) + .with("Opct", pct(s.opacity.unwrap_or(1.0))) + .with("fillOpacity", pct(s.fill_opacity.unwrap_or(1.0))), + ), + ); + } + asl::AslStyle { name: s.name.clone(), id: style_id(i, &s.name), style: d } + }) + .collect(), + }; + asl::write(&file).map_err(|e| e.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + use photocraft_color::{Color, ColorMode, PixelFormat, SampleType}; + use photocraft_doc::{BevelTexture, FxCommon, StrokePosition}; + use photocraft_psd::descriptor::UnicodeString; + use photocraft_raster::Surface; + + fn pattern(id: &str, s: SampleType) -> Pattern { + let fmt = PixelFormat::new(ColorMode::Rgb, s, true); + let mut surf = Surface::new(fmt); + for y in 0..3 { + for x in 0..4 { + surf.write_pixel(x, y, &[x as f32 / 4.0, y as f32 / 3.0, 0.5, 1.0]); + } + } + let mut p = Pattern::new("Grid", surf, 4, 3); + p.id = id.into(); + p + } + + /// Every effect kind with its descriptor defaults (the reader fills in what's absent). + fn defaults() -> Vec { + let d = |k: &str| Value::Descriptor(Descriptor::new(k)); + let lefx = Descriptor::new("null") + .with("DrSh", d("DrSh")) + .with("IrSh", d("IrSh")) + .with("OrGl", d("OrGl")) + .with("IrGl", d("IrGl")) + .with("ebbl", d("ebbl")) + .with("ChFX", d("ChFX")) + .with("SoFi", d("SoFi")) + .with("GrFl", d("GrFl")) + .with("FrFX", d("FrFX")); + effects_from_descriptor(&lefx).1 + } + + fn styles() -> Vec { + let mut fx = defaults(); + assert_eq!(fx.len(), 9); + for e in &mut fx { + match e { + Effect::DropShadow(s) => (s.distance, s.size, s.color) = (7.0, 9.0, Color::rgb(0.2, 0.1, 0.0)), + Effect::Stroke(s) => (s.size, s.position, s.paint) = (4.0, StrokePosition::Inside, FxPaint::Color(Color::rgb(0.0, 0.0, 1.0))), + Effect::ColorOverlay { common, color } => (*common, *color) = (FxCommon::new(BlendMode::Multiply, 0.5), Color::rgb(1.0, 0.5, 0.0)), + _ => {} + } + } + let mut bevel = defaults().into_iter().find(|e| matches!(e, Effect::BevelEmboss(_))).unwrap(); + if let Effect::BevelEmboss(b) = &mut bevel { + b.size = 6.0; + b.texture = Some(BevelTexture { name: "Grid".into(), id: "pat-2".into(), scale: 0.5, depth: 1.0, invert: false, link: true, phase: (0.0, 0.0) }); + } + vec![ + AslStyle { name: "Gold".into(), effects: fx, blend: Some(BlendMode::Screen), opacity: Some(0.8), fill_opacity: Some(0.25) }, + AslStyle { + name: "Textured".into(), + effects: vec![ + bevel, + Effect::PatternOverlay { + common: FxCommon { enabled: true, blend: BlendMode::Normal, opacity: 1.0 }, + name: "Grid".into(), + id: "pat-1".into(), + scale: 2.0, + angle: 0.0, + link: true, + phase: (0.0, 0.0), + }, + ], + blend: None, + opacity: None, + fill_opacity: None, + }, + ] + } + + #[test] + fn round_trip_keeps_effects_blending_and_patterns() { + let pats = vec![pattern("pat-1", SampleType::U8), pattern("pat-2", SampleType::U16)]; + let bytes = write_asl(&styles(), &pats).unwrap(); + let back = read_asl(&bytes).unwrap(); + assert!(back.warnings.is_empty(), "{:?}", back.warnings); + assert_eq!(back.patterns, pats); + assert_eq!(back.styles.len(), 2); + let (a, b) = (&back.styles[0], &styles()[0]); + assert_eq!((a.blend, a.opacity, a.fill_opacity), (b.blend, b.opacity, b.fill_opacity)); + // Effects go through the same descriptor writer and reader as PSD `lfx2`. + let via_lfx2 = |fx: &[Effect]| effects_from_descriptor(&effects_descriptor(true, fx)).1; + for (x, y) in back.styles.iter().zip(styles()) { + assert_eq!(x.name, y.name); + assert_eq!(x.effects, via_lfx2(&y.effects)); + assert_eq!(x.effects.len(), y.effects.len()); + } + assert_eq!(referenced_patterns(&back.styles[1].effects), vec![("Grid".to_string(), "pat-2".to_string()), ("Grid".to_string(), "pat-1".to_string())]); + } + + /// A library as Photoshop lays it out, built by hand from descriptors. + fn synthetic(lefx: Descriptor, blend: Option, names: &[&str]) -> Vec { + let mut style = Descriptor::new("Styl").with("documentMode", Value::Descriptor(Descriptor::new("documentMode"))).with("Lefx", Value::Descriptor(lefx)); + if let Some(b) = blend { + style = style.with("blendOptions", Value::Descriptor(b)); + } + let file = AslFile { + patterns: vec![], + pattern_error: None, + styles: names.iter().map(|n| asl::AslStyle { name: (*n).into(), id: "x".into(), style: style.clone() }).collect(), + }; + asl::write(&file).unwrap() + } + + #[test] + fn blend_options_names_and_warnings() { + let en = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let pc = |v: f64| Value::UnitFloat { unit: *b"#Prc", value: v }; + let lefx = Descriptor::new("null") + .with("Scl ", pc(100.0)) + .with("masterFXSwitch", Value::Boolean(false)) + .with( + "SoFi", + Value::Descriptor(Descriptor::new("SoFi").with("enab", Value::Boolean(true)).with("Md ", en("BlnM", "Nrml")).with("Opct", pc(100.0)).with( + "Clr ", + Value::Descriptor( + Descriptor::new("RGBC").with("Rd ", Value::Double(255.0)).with("Grn ", Value::Double(0.0)).with("Bl ", Value::Double(0.0)), + ), + )), + ) + .with("futureFX", Value::Descriptor(Descriptor::new("futureFX"))); + let blend = Descriptor::new("blendOptions") + .with("Md ", en("BlnM", "multiply")) + .with("Opct", pc(40.0)) + .with("fillOpacity", pc(0.0)) + .with("knockout", Value::Integer(1)); + let bytes = synthetic(lefx, Some(blend), &["Red", "", "red", " "]); + let imp = read_asl(&bytes).unwrap(); + let names: Vec<&str> = imp.styles.iter().map(|s| s.name.as_str()).collect(); + assert_eq!(names, ["Red", "Style 2", "red 2", "Style 4"]); + let s = &imp.styles[0]; + assert_eq!((s.blend, s.opacity, s.fill_opacity), (Some(BlendMode::Multiply), Some(0.4), Some(0.0))); + assert_eq!(s.effects.len(), 1); + // The style's effects were switched off. + assert!(!s.effects[0].enabled()); + assert!(imp.warnings.iter().any(|w| w.contains("futureFX") && w.contains("knockout")), "{:?}", imp.warnings); + } + + #[test] + fn missing_patterns_are_reported() { + let po = Descriptor::new("patternFill").with("enab", Value::Boolean(true)).with( + "Ptrn", + Value::Descriptor( + Descriptor::new("Ptrn").with("Nm ", Value::Text(UnicodeString::new_nul("Bubbles"))).with("Idnt", Value::Text(UnicodeString::new_nul("b-1"))), + ), + ); + let bytes = synthetic(Descriptor::new("null").with("patternFill", Value::Descriptor(po)), None, &["P"]); + let imp = read_asl(&bytes).unwrap(); + assert_eq!(imp.styles[0].effects.len(), 1); + assert_eq!(imp.styles[0].blend, None); + assert!(imp.warnings.iter().any(|w| w.contains("Bubbles")), "{:?}", imp.warnings); + } + + #[test] + fn hostile_bytes_are_errors() { + let bytes = write_asl(&styles(), &[pattern("pat-1", SampleType::U8)]).unwrap(); + for cut in 0..bytes.len() { + assert!(read_asl(&bytes[..cut]).is_err(), "cut {cut}"); + } + for i in 0..bytes.len() { + let mut c = bytes.clone(); + c[i] ^= 0xa5; + let _ = read_asl(&c); + } + assert!(read_asl(b"").is_err()); + assert!(read_asl(&photocraft_psd::grd::write(&[])).is_err()); + } + + #[test] + fn exported_effects_object_has_class_lefx() { + let bytes = write_asl(&styles(), &[]).unwrap(); + let file = asl::parse(&bytes).unwrap(); + for s in &file.styles { + let lefx = get_desc(&s.style, "Lefx").unwrap(); + assert_eq!(lefx.class_id, Id::new("Lefx"), "{}", s.name); + } + } + + #[test] + fn many_identical_names_import_quickly() { + let names = vec!["A"; asl::MAX_STYLES]; + let bytes = synthetic(Descriptor::new("Lefx"), None, &names); + let start = std::time::Instant::now(); + let imp = read_asl(&bytes).unwrap(); + assert!(start.elapsed() < std::time::Duration::from_secs(10), "{:?}", start.elapsed()); + assert_eq!(imp.styles.len(), asl::MAX_STYLES); + assert_eq!(imp.styles[1].name, "A 2"); + assert_eq!(imp.styles.last().unwrap().name, format!("A {}", asl::MAX_STYLES)); + } + + #[test] + fn style_ids_are_uuid_shaped() { + let id = style_id(3, "Gold"); + assert_eq!(id.len(), 36); + assert_ne!(id, style_id(4, "Gold")); + } +} diff --git a/crates/io/src/blocks.rs b/crates/io/src/blocks.rs index 87fda102fe..c280747611 100644 --- a/crates/io/src/blocks.rs +++ b/crates/io/src/blocks.rs @@ -170,6 +170,11 @@ pub fn color_from_desc(d: &Descriptor) -> Option { b"Grsc" => Color::gray(1.0 - g("Gry ")? / 100.0), // CIE L*a*b*: L 0..100, a / b −128..127 (stored like Lab pixels: offset by 128). b"LbCl" => Color { mode: ColorMode::Lab, c: [g("Lmnc")? / 100.0, (g("A ")? + 128.0) / 255.0, (g("B ")? + 128.0) / 255.0, 0.0], alpha: 1.0 }, + // HSB: hue an angle in degrees, saturation / brightness 0..100. + b"HSBC" => { + let [r, gr, b] = crate::channel_map::hsb_to_rgb(g("H ")?, g("Strt")? / 100.0, g("Brgh")? / 100.0); + Color::rgb(r, gr, b) + } // Newer Photoshop writes 0..1 floats. _ if d.get("redFloat").is_some() => Color::rgb(g("redFloat")?, g("greenFloat")?, g("blueFloat")?), _ => { @@ -177,6 +182,17 @@ pub fn color_from_desc(d: &Descriptor) -> Option { Color::rgb(g("Rd ")? / 255.0, g("Grn ")? / 255.0, g("Bl ")? / 255.0) } }) + .map(|c| with_pc_alpha(c, d)) +} + +/// PSD colour descriptors have no alpha. PhotoCraft writes `pcAl` (0..1) so shape fill +/// opacity round-trips (#6180). Photoshop ignores the key. +fn with_pc_alpha(mut c: Color, d: &Descriptor) -> Color { + // A hostile file's NaN or infinity would survive the clamp: keep the colour opaque instead. + if let Some(a) = num(d.get("pcAl")).filter(|a| a.is_finite()) { + c.alpha = (a as f32).clamp(0.0, 1.0); + } + c } /// [`Color`] → descriptor in its own model. @@ -199,6 +215,13 @@ pub fn color_to_desc(c: &Color) -> Descriptor { } } +/// Solid shape fills have no other alpha slot. Write `pcAl` only here so gradient stops +/// keep using `Trns` alone (#6180). +fn color_to_desc_with_pc_alpha(c: &Color) -> Descriptor { + let desc = color_to_desc(c); + if c.alpha.is_finite() && (c.alpha - 1.0).abs() > 1e-6 { desc.with("pcAl", Value::Double(f64::from(c.alpha.clamp(0.0, 1.0)))) } else { desc } +} + pub(crate) fn get_desc<'a>(d: &'a Descriptor, key: &str) -> Option<&'a Descriptor> { match d.get(key)? { Value::Descriptor(x) | Value::GlobalObject(x) => Some(x), @@ -252,7 +275,8 @@ pub(crate) fn gradient_style_value(s: GradientStyle) -> Value { pub(crate) type Stops = (Vec<(f32, Color)>, Vec<(f32, f32)>); /// Stops of a `Grdn` descriptor, baked to Photoshop's interpolation (smoothness `Intr`, stop -/// midpoints, and the parent's interpolation method when given, see [`gradient_method`]). +/// midpoints, and the parent's interpolation method when given, see [`gradient_method`]); the +/// opacity stops as [`baked_opacity_stops`]. pub(crate) fn gradient_stops_with(grad: &Descriptor, method: Option<&[u8]>) -> Stops { let raw = gradient_stops_raw(grad); let stops = if raw.stops.len() >= 2 { @@ -260,7 +284,7 @@ pub(crate) fn gradient_stops_with(grad: &Descriptor, method: Option<&[u8]>) -> S } else { raw.stops }; - (stops, raw.opacity) + (stops, baked_opacity_stops(grad, method)) } /// A `Grdn` descriptor's stops as stored: colour stops (sorted), the midpoint of each segment @@ -294,6 +318,29 @@ pub(crate) fn gradient_stops_editable(grad: &Descriptor, method: Option<&[u8]>) raw } +/// The exact, constant-RGB subset of Photoshop noise gradients. If every RGB component has the +/// same minimum and maximum, the generated field is constant regardless of seed or roughness. +/// Other models, transparency, and varying ranges are not approximated. +fn constant_rgb_noise_color(grad: &Descriptor) -> Option { + if enum_of(grad, "GrdF")? != b"ClNs" || enum_of(grad, "ClrS")? != b"RGBC" || bool_of(grad, "ShTr") { + return None; + } + let components = |key: &str| -> Option<[f32; 4]> { + let Some(Value::List(values)) = grad.get(key) else { return None }; + if values.len() != 4 { + return None; + } + let converted: Vec = values.iter().map(|value| num(Some(value)).map(|v| v as f32)).collect::>()?; + converted.try_into().ok() + }; + let min = components("Mnm ")?; + let max = components("Mxm ")?; + if min.iter().zip(max).take(3).any(|(a, b)| !a.is_finite() || *a < 0.0 || *a > 100.0 || *a != b) { + return None; + } + Some(Color::rgb(min[0] / 100.0, min[1] / 100.0, min[2] / 100.0)) +} + fn gradient_stops_raw(grad: &Descriptor) -> RawStops { let mut stops = Vec::new(); let mut mids = Vec::new(); @@ -376,26 +423,27 @@ pub(crate) fn gradient_desc_with(stops: &[(f32, Color)], opacity: &[(f32, f32)], .with("Trns", Value::List(trns)) } -/// Opacity stops of a `Grdn` descriptor with Photoshop's smoothness and midpoints applied (the -/// same curve as the colour stops, Classic interpolation), densely sampled. -fn smoothed_opacity_stops(grad: &Descriptor) -> Vec<(f32, f32)> { +/// Opacity stops of a `Grdn` descriptor with Photoshop's smoothness, opacity midpoints and +/// interpolation method applied (densely sampled; as stored when the interpolation is plain). +/// Gradient fill layers and the gradient layer effects share it: Photoshop renders their +/// transparency identically. +fn baked_opacity_stops(grad: &Descriptor, method: Option<&[u8]>) -> Vec<(f32, f32)> { let mut stops = Vec::new(); let mut mids = Vec::new(); if let Some(Value::List(items)) = grad.get("Trns") { for it in items { if let Value::Descriptor(s) = it { - let a = num(s.get("Opct")).unwrap_or(100.0) as f32 / 100.0; - stops.push((num(s.get("Lctn")).unwrap_or(0.0) as f32 / 4096.0, Color::rgba(a, a, a, 1.0))); + stops.push((num(s.get("Lctn")).unwrap_or(0.0) as f32 / 4096.0, num(s.get("Opct")).unwrap_or(100.0) as f32 / 100.0)); mids.push(num(s.get("Mdpn")).unwrap_or(50.0) as f32 / 100.0); } } } + // Midpoint k applies to the segment after stop k (stored on the stop that ends it). let mut order: Vec = (0..stops.len()).collect(); order.sort_by(|a, b| stops[*a].0.total_cmp(&stops[*b].0)); let mids: Vec = order.iter().skip(1).filter_map(|i| mids.get(*i).copied()).collect(); let smooth = num(grad.get("Intr")).map_or(0.0, |v| (v / 4096.0) as f32); - let baked = if stops.len() >= 2 { crate::gradient_bake::bake(stops, &mids, smooth, crate::gradient_bake::Method::Classic) } else { stops }; - baked.into_iter().map(|(t, c)| (t, c.to_rgb()[0])).collect() + crate::gradient_bake::bake_opacity(stops, &mids, smooth, crate::gradient_bake::Method::from_code(method)) } /// Parses a fill block (`SoCo`, `GdFl`, `PtFl`). @@ -413,16 +461,27 @@ pub fn fill_from_desc(key: &[u8; 4], d: &Descriptor) -> Option { b"GdFl" => { let angle = num(d.get("Angl")).unwrap_or(90.0) as f32; let scale = num(d.get("Scl ")).map_or(1.0, |v| v as f32 / 100.0); - let raw = get_desc(d, "Grad").map(|g| gradient_stops_editable(g, gradient_method(d))); + let gradient = get_desc(d, "Grad"); + let raw = gradient.map(|g| gradient_stops_editable(g, gradient_method(d))); let (mut stops, midpoints) = raw.map(|r| (r.stops, r.mids)).unwrap_or_default(); - // Opacity stops with Photoshop's smoothness and opacity midpoints applied (raw when - // the interpolation is plain); none when fully opaque. - let mut opacity_stops = get_desc(d, "Grad").map(smoothed_opacity_stops).unwrap_or_default(); + // Opacity stops with Photoshop's smoothness, opacity midpoints and method applied + // (raw when the interpolation is plain); none when fully opaque. + let mut opacity_stops = get_desc(d, "Grad").map(|g| baked_opacity_stops(g, gradient_method(d))).unwrap_or_default(); if opacity_stops.iter().all(|o| o.1 >= 1.0 - 1e-4) { opacity_stops.clear(); } if stops.is_empty() { - stops = vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]; + // A provably constant-RGB noise gradient renders one exact colour. Any other + // noise gradient (variable ranges, transparency, other models) has no stops to + // read: keep the placeholder ramp rather than failing the whole fill — the + // layer stays a fill layer, Photoshop's own pixels ride along as `fill_cache`, + // and the raw `GdFl` block survives the round trip byte for byte. Failing + // here would import the layer as pixels and drop `GdFl` on save (#6272). + stops = gradient + .filter(|g| enum_of(g, "GrdF") == Some(b"ClNs")) + .and_then(constant_rgb_noise_color) + .map(|color| vec![(0.0, color), (1.0, color)]) + .unwrap_or_else(|| vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]); } let pct = |o: &Descriptor, k: &str| num(o.get(k)).map_or(0.0, |v| v as f32 / 100.0); let offset = get_desc(d, "Ofst").map_or((0.0, 0.0), |o| (pct(o, "Hrzn"), pct(o, "Vrtc"))); @@ -458,7 +517,7 @@ pub fn write_fill(f: &Fill) -> ([u8; 4], Vec) { /// A fill as (block key, descriptor with class `null`). pub fn fill_to_desc(f: &Fill) -> ([u8; 4], Descriptor) { match f { - Fill::Solid(c) => (*b"SoCo", Descriptor::new("null").with("Clr ", Value::Descriptor(color_to_desc(c)))), + Fill::Solid(c) => (*b"SoCo", Descriptor::new("null").with("Clr ", Value::Descriptor(color_to_desc_with_pc_alpha(c)))), Fill::Gradient { stops, angle, scale, style, reverse, opacity_stops, midpoints, offset, dither, align } => { let prc = |v: f32| Value::UnitFloat { unit: *b"#Prc", value: f64::from(v * 100.0) }; let mut d = Descriptor::new("null") @@ -525,22 +584,27 @@ pub(crate) fn with_pattern_placement(mut d: Descriptor, angle: f32, link: bool, ) } -/// The warp of a placed layer (`SoLd`/`SoLE` `warp` descriptor): style, bend, distortions, -/// axis, bounds and, for `warpCustom`, the 4 × 4 `customEnvelopeWarp` mesh. Read-only: the raw -/// block is what gets written back. `None` when absent or `warpNone` with no mesh. A `PlLd` -/// record keeps its warp elsewhere: see [`parse_plld_warp`]. +/// The warp of a placed layer (`SoLd`/`SoLE` `quiltWarp` or `warp` descriptor): style, bend, +/// distortions, axis, bounds and, for `warpCustom`, the `customEnvelopeWarp` mesh: one 4 × 4 +/// patch, or the multi-patch grid Photoshop's split warp writes as `quiltWarp`; for +/// `warpCylinder` its seven `warpValues`. Read-only: the raw block is what gets written back. +/// `None` when absent or `warpNone` with no mesh. A `PlLd` record keeps its warp elsewhere: see +/// [`parse_plld_warp`]. pub fn parse_placed_warp(key: &[u8; 4], data: &[u8]) -> Option { if key != b"SoLd" && key != b"SoLE" { return None; } let d = data.get(8..).and_then(parse_prefix_versioned)?; - warp_of(get_desc(&d, "warp")?).flatten() + // A split (quilt) warp is stored under `quiltWarp`; `warp` then holds no usable mesh. + warp_of(get_desc(&d, "quiltWarp").or_else(|| get_desc(&d, "warp"))?).flatten() } -/// A `warp` descriptor (the `SoLd` key, or the whole descriptor after a `PlLd` record's corners) -/// as a warp: `Some(None)` for no warp (`warpNone`, or a warp that moves nothing), `None` when it -/// can't be read (an unknown style, missing bounds, a mesh that isn't 4 × 4). -fn warp_of(w: &Descriptor) -> Option> { +/// A `warp` or `quiltWarp` descriptor (the `SoLd` key, or the whole descriptor after a `PlLd` +/// record's corners) as a warp: `Some(None)` for no warp (`warpNone`, or a warp that moves +/// nothing), `None` when it can't be read (an unknown style, missing bounds, a mesh that is +/// neither one 4 × 4 patch nor a consistent quilt grid, a cylinder that isn't a horizontal one +/// without perspective, the only kind measured). +pub(crate) fn warp_of(w: &Descriptor) -> Option> { use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; let style = enum_of(w, "warpStyle").and_then(|v| WarpStyle::parse(&String::from_utf8_lossy(v)))?; if style == WarpStyle::None { @@ -553,6 +617,17 @@ fn warp_of(w: &Descriptor) -> Option> { warp.h_distort = num(w.get("warpPerspective")).unwrap_or(0.0); warp.v_distort = num(w.get("warpPerspectiveOther")).unwrap_or(0.0); warp.vertical = enum_of(w, "warpRotate") == Some(b"Vrtc"); + if style == WarpStyle::Cylinder { + if warp.vertical || warp.h_distort != 0.0 || warp.v_distort != 0.0 { + return None; + } + let values: Vec = match w.get("warpValues")? { + Value::List(l) => l.iter().map(|v| num(Some(v))).collect::>()?, + Value::UnitFloats { values, .. } => values.clone(), + _ => return None, + }; + return Some(Some(Warp::cylinder(&values, bounds)?)); + } if style == WarpStyle::Custom { let env = get_desc(w, "customEnvelopeWarp")?; let Some(Value::ObjectArray(arr)) = env.get("meshPoints") else { return None }; @@ -561,16 +636,81 @@ fn warp_of(w: &Descriptor) -> Option> { _ => None, }; let (xs, ys) = (vals("Hrzn")?, vals("Vrtc")?); - if xs.len() != 16 || ys.len() != 16 { + if xs.len() != ys.len() { return None; } - let mesh = BezierMesh { us: vec![0.0, 1.0], vs: vec![0.0, 1.0], points: xs.iter().zip(&ys).map(|(x, y)| [*x, *y]).collect() }; + let points: Vec<[f64; 2]> = xs.iter().zip(&ys).map(|(x, y)| [*x, *y]).collect(); + let mesh = if points.len() == 16 && env.get("quiltSliceX").is_none() { + BezierMesh { us: vec![0.0, 1.0], vs: vec![0.0, 1.0], points } + } else { + quilt_mesh(w, env, points)? + }; warp.mesh = mesh.is_valid().then_some(mesh); warp.mesh.as_ref()?; } Some((!warp.is_identity()).then_some(warp)) } +/// The most patches a `quiltWarp` is read with along each side. +pub(crate) const QUILT_MAX_PATCHES: usize = 64; + +/// The control-point grid of a split ("quilt") custom warp. `deformNumCols` × `deformNumRows` +/// control points, three per patch side plus one, in row-major order; `quiltSliceX` and +/// `quiltSliceY` are the patch boundaries in the mesh's own units, first and last at its edges, +/// which become the knots on the unit square. `None` for anything inconsistent. +fn quilt_mesh(w: &Descriptor, env: &Descriptor, points: Vec<[f64; 2]>) -> Option { + // Photoshop's split grid tops out far below this; it only bounds what a file can claim. + const MAX_SIDE: usize = 3 * QUILT_MAX_PATCHES + 1; + let count = |k: &str| -> Option { + let n = num(w.get(k))?; + (n.is_finite() && n >= 4.0 && n <= MAX_SIDE as f64 && n.fract() == 0.0).then_some(n as usize) + }; + let (cols, rows) = (count("deformNumCols")?, count("deformNumRows")?); + if (cols - 1) % 3 != 0 || (rows - 1) % 3 != 0 || points.len() != cols.checked_mul(rows)? { + return None; + } + let us = quilt_knots(env, "quiltSliceX", (cols - 1) / 3)?; + let vs = quilt_knots(env, "quiltSliceY", (rows - 1) / 3)?; + Some(photocraft_geom::warp::BezierMesh { us, vs, points }) +} + +/// The knots of a quilt's patch boundaries `key` (`quiltSliceX` or `quiltSliceY` in its +/// `customEnvelopeWarp`): `patches + 1` strictly increasing slices, mapped onto `0..=1` with the +/// first and last at the ends. `None` for anything else. +pub(crate) fn quilt_knots(env: &Descriptor, key: &str, patches: usize) -> Option> { + // Stored as an object array holding one unit-float list under the same key; a plain list + // of numbers is taken too. + let s: Vec = match env.get(key)? { + Value::ObjectArray(arr) => match arr.body.get(key)? { + Value::UnitFloats { values, .. } => values.clone(), + Value::List(l) => l.iter().map(|v| num(Some(v))).collect::>()?, + _ => return None, + }, + Value::List(l) => l.iter().map(|v| num(Some(v))).collect::>()?, + _ => return None, + }; + let (first, last) = (*s.first()?, *s.last()?); + let span = last - first; + if s.len() != patches + 1 || !(span.is_finite() && span > 0.0) || !s.windows(2).all(|p| p[1] > p[0]) { + return None; + } + let n = s.len() - 1; + Some( + s.iter() + .enumerate() + .map(|(i, v)| { + if i == 0 { + 0.0 + } else if i == n { + 1.0 + } else { + (v - first) / span + } + }) + .collect(), + ) +} + /// Text content and transform from a `TySh` block (best effort). pub fn parse_tysh(data: &[u8]) -> Option<(String, Affine)> { let f = |at: usize| data.get(at..at + 8).map(|b| f64::from_be_bytes(b.try_into().unwrap_or([0; 8]))); @@ -593,13 +733,14 @@ pub(crate) fn parse_prefix_versioned(data: &[u8]) -> Option { } /// The source-space box Photoshop maps onto a placed layer's `Trnf` quad: the bounding box of a -/// single-patch custom warp's 16 control points, which may spill past the source, else the source -/// rect `0..w × 0..h`. Mapping the source rect instead counts the overhang twice and stretches the -/// placement. A degenerate or non-finite box falls back to the source rect. +/// custom warp's control points (one patch or a split grid) or of a cylinder's mesh (#4112), +/// which may spill past the source, else the source rect `0..w × 0..h`. Mapping the source rect +/// instead counts the overhang twice and stretches the placement. A degenerate or non-finite box +/// falls back to the source rect. pub(crate) fn placed_frame(warp: Option<&photocraft_geom::warp::Warp>, w: f64, h: f64) -> [f64; 4] { + use photocraft_geom::warp::WarpStyle; let rect = [0.0, 0.0, w, h]; - let Some(m) = warp.filter(|wp| wp.style == photocraft_geom::warp::WarpStyle::Custom).and_then(|wp| wp.mesh.as_ref()).filter(|m| m.points.len() == 16) - else { + let Some(m) = warp.filter(|wp| matches!(wp.style, WarpStyle::Custom | WarpStyle::Cylinder)).and_then(|wp| wp.mesh.as_ref()).filter(|m| m.is_valid()) else { return rect; }; let inf = f64::INFINITY; @@ -608,6 +749,16 @@ pub(crate) fn placed_frame(warp: Option<&photocraft_geom::warp::Warp>, w: f64, h if ok { b } else { rect } } +/// The source-space box a projective (Distort / Perspective) placement maps onto its corners: a +/// cylinder's control-point box, as for an affine placement (#4112), else the source rect. A +/// custom warp keeps the source rect here: there is no Photoshop reference for that combination. +pub(crate) fn perspective_frame(warp: Option<&photocraft_geom::warp::Warp>, w: f64, h: f64) -> [f64; 4] { + match warp { + Some(wp) if wp.style == photocraft_geom::warp::WarpStyle::Cylinder => placed_frame(warp, w, h), + _ => [0.0, 0.0, w, h], + } +} + /// Smart object identifier and transform from `SoLd`/`SoLE` (best effort). A `PlLd` block /// stores no source size, so its transform needs the embedded file: see [`parse_plld`]. pub fn parse_smart(key: &[u8; 4], data: &[u8]) -> (String, Affine) { @@ -645,17 +796,17 @@ pub fn corners_affine(p: &[f64; 8], frame: [f64; 4]) -> Option { /// The transform and (Distort/Perspective) projective placement of a `PlLd` record's corners `p` /// for a source of `size` warped by `warp`: the inverse of [`crate::smart_map::plld_bytes`]'s -/// quad. As for `SoLd`'s `Trnf` (#3677), affine corners frame a single-patch custom warp's +/// quad. As for `SoLd`'s `Trnf` (#3677), affine corners frame a custom warp's or a cylinder's /// control-point box (which may spill past the source), else the source rect; projective corners -/// frame the source rect, as `parse_smart_perspective` reads them. `PlLd` stores no source size: -/// without the source file (`None`) the layer is taken as placed at 100 %, the edge lengths being -/// its size (a custom warp's box needs no size); position and rotation stay right, only Replace -/// Contents and re-renders depend on the scale. Import and export (which keeps an unedited record -/// verbatim) both place a `PlLd` layer with this. +/// frame [`perspective_frame`], as `parse_smart_perspective` reads them. `PlLd` stores no source +/// size: without the source file (`None`) the layer is taken as placed at 100 %, the edge lengths +/// being its size (a custom warp's box needs no size); position and rotation stay right, only +/// Replace Contents and re-renders depend on the scale. Import and export (which keeps an unedited +/// record verbatim) both place a `PlLd` layer with this. pub fn plld_transform(p: &[f64; 8], size: Option<(f64, f64)>, warp: Option<&photocraft_geom::warp::Warp>) -> (Affine, Option<[f64; 9]>) { let (w, h) = size.unwrap_or_else(|| ((p[2] - p[0]).hypot(p[3] - p[1]), (p[6] - p[0]).hypot(p[7] - p[1]))); - let perspective = corners_perspective([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], (w, h)); - let frame = if perspective.is_some() { [0.0, 0.0, w, h] } else { placed_frame(warp, w, h) }; + let perspective = corners_perspective_in([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], perspective_frame(warp, w, h)); + let frame = if perspective.is_some() { perspective_frame(warp, w, h) } else { placed_frame(warp, w, h) }; (corners_affine(p, frame).unwrap_or(Affine::IDENTITY), perspective) } @@ -730,18 +881,27 @@ pub fn parse_smart_perspective(key: &[u8; 4], data: &[u8]) -> Option<[f64; 9]> { (p.len() == 8 && p.iter().all(|v| v.is_finite())).then(|| [[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]]) }; let q = quad("nonAffineTransform").or_else(|| quad("Trnf"))?; - corners_perspective(q, (w, h)) + // The warp `parse_placed_warp` reads: a split warp's `quiltWarp` comes first. + let warp = get_desc(&d, "quiltWarp").or_else(|| get_desc(&d, "warp")).and_then(warp_of).flatten(); + corners_perspective_in(q, perspective_frame(warp.as_ref(), w, h)) } /// The projective placement of a `w` × `h` source onto corners `q` that aren't a parallelogram; /// `None` for affine corners (see [`corners_affine`]) or an empty size. pub fn corners_perspective(q: [[f64; 2]; 4], (w, h): (f64, f64)) -> Option<[f64; 9]> { + corners_perspective_in(q, [0.0, 0.0, w, h]) +} + +/// [`corners_perspective`] for the source-space box `frame` (left, top, right, bottom; see +/// [`perspective_frame`]). +pub(crate) fn corners_perspective_in(q: [[f64; 2]; 4], frame: [f64; 4]) -> Option<[f64; 9]> { // A parallelogram (top-left + bottom-right = top-right + bottom-left) is affine. let tol = 1e-6 * (1.0 + q.iter().flatten().fold(0.0f64, |m, v| m.max(v.abs()))); - if (0..2).all(|i| (q[0][i] + q[2][i] - q[1][i] - q[3][i]).abs() <= tol) || w <= 0.0 || h <= 0.0 { + let ok = frame.iter().all(|v| v.is_finite()) && frame[2] > frame[0] && frame[3] > frame[1]; + if (0..2).all(|i| (q[0][i] + q[2][i] - q[1][i] - q[3][i]).abs() <= tol) || !ok { return None; } - photocraft_algo::transform::Homography::rect_to_quad([0.0, 0.0, w, h], q).map(|m| m.0) + photocraft_algo::transform::Homography::rect_to_quad(frame, q).map(|m| m.0) } /// `masterFXSwitch` from an `lfx2` block (defaults to `true`). @@ -756,499 +916,7 @@ pub fn effects_enabled(lfx2: &[u8]) -> bool { } #[cfg(test)] -mod tests { - /// psd-tools layers-minimal/gradient-fill.psd ("Color to Transparent", no stored pixels): a - /// gradient fill's opacity stops reach the model (as its opacity stops; written from stop - /// alpha) and survive a write; the ramp's alpha follows them. - #[test] - fn gradient_fill_opacity_stops_become_alpha() { - use photocraft_color::Color; - use photocraft_doc::{Fill, GradientStyle}; - let red = Color::rgba(1.0, 0.0, 0.0, 1.0); - let f = Fill::gradient(vec![(0.0, red), (1.0, Color { alpha: 0.0, ..red })], 90.0, 1.0, GradientStyle::Linear, false); - let (k, data) = super::write_fill(&f); - let back = super::parse_fill(&k, &data).unwrap(); - let Fill::Gradient { stops, opacity_stops, .. } = &back else { panic!("gradient") }; - assert_eq!(opacity_stops, &vec![(0.0, 1.0), (1.0, 0.0)]); - assert!(stops.iter().all(|s| s.1.c[0] == 1.0 && s.1.c[1] == 0.0)); - let ramp = photocraft_compose::gradient_fill::Ramp::new(&back).unwrap(); - assert_eq!(ramp.sample(0.0)[3], 1.0); - assert!(ramp.sample(1.0)[3] < 0.01); - // Opaque gradients come back without opacity stops. - let (k, data) = super::write_fill(&Fill::gradient(vec![(0.0, red), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false)); - let Some(Fill::Gradient { opacity_stops, .. }) = super::parse_fill(&k, &data) else { panic!("gradient") }; - assert!(opacity_stops.is_empty()); - } - - /// Photoshop 27.11 stores a gradient's method as the enum `gradientsInterpolationMethod` - /// (seen in gradient fill layers saved by scripted Photoshop): a black→white Linear gradient - /// is at 188 of 255 halfway (measured), where Classic gives 128. The key used to be read as - /// `gs99`, which Photoshop doesn't write, so every gradient opened as Classic. - #[test] - fn gradient_fill_reads_the_interpolation_method() { - use photocraft_color::Color; - use photocraft_doc::{Fill, GradientStyle}; - use photocraft_psd::descriptor::{Id, Value}; - let f = Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false); - let (k, data) = super::write_fill(&f); - let d = super::parse_prefix_versioned(&data).unwrap(); - let mid = |code: &str| { - let d = - d.clone().with("gradientsInterpolationMethod", Value::Enumerated { type_id: Id::new("gradientInterpolationMethodType"), value: Id::new(code) }); - let back = super::fill_from_desc(&k, &d).unwrap(); - let ramp = photocraft_compose::gradient_fill::Ramp::new(&back).unwrap(); - (ramp.sample(0.5)[0] * 255.0).round() - }; - assert_eq!(mid("Gcls"), 128.0); - assert!((mid("Lnr ") - 188.0).abs() <= 1.0, "Linear: {}", mid("Lnr ")); - assert!((mid("Perc") - 99.0).abs() <= 2.0, "Perceptual: {}", mid("Perc")); - assert!((mid("Smoo") - 99.0).abs() <= 2.0, "Smooth: {}", mid("Smoo")); - } - - #[test] - fn lab_descriptor_colours_round_trip() { - use photocraft_color::ColorMode; - use photocraft_psd::descriptor::{Descriptor, Value}; - let d = Descriptor::new("LbCl").with("Lmnc", Value::Double(19.07)).with("A ", Value::Double(52.29)).with("B ", Value::Double(-85.08)); - let c = super::color_from_desc(&d).unwrap(); - assert_eq!(c.mode, ColorMode::Lab); - assert!((c.c[0] - 0.1907).abs() < 1e-5 && (c.c[1] * 255.0 - 128.0 - 52.29).abs() < 1e-3 && (c.c[2] * 255.0 - 128.0 + 85.08).abs() < 1e-3); - let back = super::color_from_desc(&super::color_to_desc(&c)).unwrap(); - assert_eq!(back.mode, ColorMode::Lab); - for i in 0..3 { - assert!((back.c[i] - c.c[i]).abs() < 1e-6); - } - // Missing components are rejected, not defaulted. - assert!(super::color_from_desc(&Descriptor::new("LbCl").with("Lmnc", Value::Double(50.0))).is_none()); - } - - #[test] - fn blending_ranges_map_to_blend_if() { - use photocraft_doc::{BlendIf, BlendRange}; - use photocraft_psd::layer::BlendingRanges; - // Full ranges (what every layer without Blend If carries) → the default setting. - assert_eq!(super::blend_if_from_ranges(&BlendingRanges::full(3)), BlendIf::default()); - assert_eq!(super::blend_if_from_ranges(&BlendingRanges::default()), BlendIf::default()); - // Gray: This Layer black split 10/40; Blue (entry 3): Underlying white at 200. - let mut data = BlendingRanges::full(3).data; - data[..4].copy_from_slice(&[10, 40, 255, 255]); - data[3 * 8 + 4..3 * 8 + 8].copy_from_slice(&[0, 0, 200, 200]); - let b = super::blend_if_from_ranges(&BlendingRanges { data: data.clone() }); - assert_eq!(b.get(0), [BlendRange { black: [10, 40], white: [255, 255] }, BlendRange::FULL]); - assert_eq!(b.get(3), [BlendRange::FULL, BlendRange { black: [0, 0], white: [200, 200] }]); - assert_eq!(b.ranges.len(), 4); - assert_eq!(super::ranges_from_blend_if(&b, 3).data, data); - // The default writes full ranges for gray + each channel. - assert_eq!(super::ranges_from_blend_if(&BlendIf::default(), 4), BlendingRanges::full(4)); - // A trimmed setting is padded to the document's channel count. - let mut short = BlendIf::default(); - short.set(0, [BlendRange { black: [5, 5], white: [255, 255] }, BlendRange::FULL]); - let r = super::ranges_from_blend_if(&short, 3); - assert_eq!(r.data.len(), 4 * 8); - assert_eq!(&r.data[..8], &[5, 5, 255, 255, 0, 0, 255, 255]); - assert_eq!(&r.data[8..], &BlendingRanges::full(2).data[..]); - } - - #[test] - fn advanced_blending_blocks_round_trip() { - use photocraft_doc::{AdvancedBlending, Knockout}; - let a = AdvancedBlending { - knockout: Knockout::Deep, - blend_interior: true, - blend_clipped: false, - transparency_shapes: false, - layer_mask_hides_effects: true, - vector_mask_hides_effects: true, - }; - let mut raw = Vec::new(); - super::put_advanced(&a, &mut raw); - assert_eq!(raw.len(), 6); - assert!(raw.contains(&(*b"knko", vec![2, 0, 0, 0]))); - assert!(raw.contains(&(*b"clbl", vec![0, 0, 0, 0]))); - let get = |k: &[u8; 4]| raw.iter().find(|(bk, _)| bk == k).map(|(_, d)| d.as_slice()); - assert_eq!(super::advanced_from_blocks(get), a); - // Defaults: nothing added to a plain layer; existing blocks rewritten in place, in order. - let mut plain = Vec::new(); - super::put_advanced(&AdvancedBlending::default(), &mut plain); - assert!(plain.is_empty()); - let mut kept = vec![(*b"luni", vec![0; 4]), (*b"knko", vec![1, 0, 0, 0]), (*b"clbl", vec![1])]; - super::put_advanced(&AdvancedBlending::default(), &mut kept); - assert_eq!(kept, vec![(*b"luni", vec![0; 4]), (*b"knko", vec![0, 0, 0, 0]), (*b"clbl", vec![1, 0, 0, 0])]); - // Missing and empty blocks read as Photoshop's defaults. - assert!(super::advanced_from_blocks(|_| None).is_default()); - assert!(super::advanced_from_blocks(|_| Some(&[][..])).is_default()); - assert_eq!(super::advanced_from_blocks(|k| (k == b"knko").then_some(&[1u8][..])).knockout, Knockout::Shallow); - } - - #[test] - fn brst_round_trip() { - assert_eq!(super::parse_brst(&[0, 0, 0, 2]), 0b100); - assert_eq!(super::parse_brst(&[0, 0, 0, 0, 0, 0, 0, 1]), 0b11); - assert_eq!(super::brst_data(0b101).unwrap(), vec![0, 0, 0, 0, 0, 0, 0, 2]); - assert_eq!(super::brst_data(0), None); - } - - use super::*; - - #[test] - fn placed_layer_warp_is_read() { - use photocraft_geom::warp::WarpStyle; - use photocraft_psd::descriptor::ObjectArray; - let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; - let bounds = Descriptor::new("Rctn") - .with("Top ", Value::UnitFloat { unit: *b"#Pxl", value: 0.0 }) - .with("Left", Value::UnitFloat { unit: *b"#Pxl", value: 0.0 }) - .with("Btom", Value::UnitFloat { unit: *b"#Pxl", value: 30.0 }) - .with("Rght", Value::UnitFloat { unit: *b"#Pxl", value: 60.0 }); - let sold = |warp: Descriptor| { - let mut v = b"soLD".to_vec(); - v.extend_from_slice(&4u32.to_be_bytes()); - v.extend(VersionedDescriptor::new(Descriptor::new("null").with("warp", Value::Descriptor(warp))).to_bytes()); - v - }; - let arc = Descriptor::new("warp") - .with("warpStyle", e("warpStyle", "warpArc")) - .with("warpValue", Value::Double(40.0)) - .with("warpPerspective", Value::Double(0.0)) - .with("warpPerspectiveOther", Value::Double(0.0)) - .with("warpRotate", e("Ornt", "Hrzn")) - .with("bounds", Value::Descriptor(bounds.clone())); - let w = parse_placed_warp(b"SoLd", &sold(arc)).unwrap(); - assert_eq!((w.style, w.bend, w.bounds), (WarpStyle::Arc, 40.0, [0.0, 0.0, 60.0, 30.0])); - let (mut xs, mut ys) = (Vec::new(), Vec::new()); - for j in 0..4 { - for i in 0..4 { - xs.push(f64::from(i) * 20.0 + if i == 3 && j == 3 { 7.0 } else { 0.0 }); - ys.push(f64::from(j) * 10.0); - } - } - let mesh = Descriptor::new("null") - .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: xs }) - .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: ys }); - let custom = Descriptor::new("warp").with("warpStyle", e("warpStyle", "warpCustom")).with("bounds", Value::Descriptor(bounds)).with( - "customEnvelopeWarp", - Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body: mesh }))), - ); - let w = parse_placed_warp(b"SoLd", &sold(custom)).unwrap(); - assert_eq!(w.style, WarpStyle::Custom); - assert_eq!(w.mesh.as_ref().unwrap().points[15], [67.0, 30.0]); - assert!(parse_placed_warp(b"PlLd", &[0; 20]).is_none()); - } - - #[test] - fn locks_and_labels() { - // Reference values round-trip byte for byte; Background layers carry 0x0D - // (transparency + position + the bit-3 lock reference files write). - for bits in [0u32, 1, 2, 4, 8, 0x0D, 0x8000_0000, 0x8000_000F] { - assert_eq!(lspf_from_locks(&locks_from_lspf(bits)), bits); - } - // Older PhotoCraft files wrote the artboard lock at 0x10; it reads back as artboard - // and is rewritten at the reference value 0x08. - assert!(locks_from_lspf(0x10).artboard); - assert_eq!(lspf_from_locks(&locks_from_lspf(0x10)), 0x08); - for i in 0..8 { - assert_eq!(label_index(label_from_index(i)), i); - } - } - - #[test] - fn fills_roundtrip() { - for f in [ - Fill::Solid(Color::rgb(1.0, 0.5, 0.0)), - Fill::Solid(Color { mode: ColorMode::Cmyk, c: [0.1, 0.2, 0.3, 0.4], alpha: 1.0 }), - Fill::Solid(Color::gray(0.25)), - Fill::gradient(vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], 45.0, 1.0, GradientStyle::Reflected, true), - // A live gradient (Gradient tool): canvas-aligned, offset, midpoints, opacity, dither. - Fill::Gradient { - stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (0.5, Color::rgb(0.0, 1.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], - angle: -33.5, - scale: 2.25, - style: GradientStyle::Radial, - reverse: false, - opacity_stops: vec![(0.0, 1.0), (0.25, 0.5), (1.0, 0.0)], - midpoints: vec![0.25, 0.75], - offset: (0.125, -0.375), - dither: true, - align: false, - }, - Fill::Pattern { name: "Bubbles".into(), scale: 0.5, id: "abc".into(), angle: 30.0, link: false, phase: (3.0, -2.0) }, - ] { - let (k, d) = write_fill(&f); - let back = parse_fill(&k, &d).unwrap(); - match (&f, &back) { - (Fill::Solid(a), Fill::Solid(b)) => { - assert_eq!(a.mode, b.mode); - for i in 0..4 { - assert!((a.c[i] - b.c[i]).abs() < 1e-5); - } - } - _ => assert_eq!(back, f), - } - } - } -} +mod tests; #[cfg(test)] -mod more_tests { - use super::*; - - #[test] - fn float_rgb_colors_parse() { - let d = Descriptor::new("RGBC").with("redFloat", Value::Double(1.0)).with("greenFloat", Value::Double(0.5)).with("blueFloat", Value::Double(0.25)); - let c = color_from_desc(&d).unwrap(); - assert_eq!(c.to_rgb(), [1.0, 0.5, 0.25]); - } - - #[test] - fn smart_transform_from_sold() { - let pts = [10.0, 20.0, 110.0, 20.0, 110.0, 70.0, 10.0, 70.0].map(Value::Double).to_vec(); - let d = Descriptor::new("null") - .with("Idnt", Value::Text(UnicodeString::new_nul("uuid-1"))) - .with("Trnf", Value::List(pts)) - .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(50.0)).with("Hght", Value::Double(25.0)))); - let mut data = b"soLD".to_vec(); - data.extend_from_slice(&4u32.to_be_bytes()); - data.extend(VersionedDescriptor::new(d).to_bytes()); - data.extend_from_slice(&[0, 0]); - let (id, a) = parse_smart(b"SoLd", &data); - assert_eq!(id, "uuid-1"); - assert_eq!(a.m, [2.0, 0.0, 0.0, 2.0, 10.0, 20.0]); - assert_eq!(parse_smart(b"PlLd", &data).0, ""); - assert_eq!(parse_plld(&data), None, "soLD data is not a plcL record"); - } - - /// A `PlLd` record (as Photoshop CS2 writes it, with no `SoLd`, #2465) gives its source id and - /// placed corners; with the source size those are the same transform `SoLd` gives. - #[test] - fn plld_id_and_corners() { - use crate::smart_map::{PlacedSpec, plld_bytes}; - let t = Affine { m: [1.5, 0.25, -0.5, 2.0, 10.0, 20.0] }; - let spec = PlacedSpec { idnt: "uuid-cs2", placed: "", transform: t, perspective: None, size: (40.0, 30.0), dpi: 72.0, warp: None, filter_fx: None }; - let data = plld_bytes(&spec, &mut Vec::new()); - let (id, p) = parse_plld(&data).expect("a plcL record"); - assert_eq!(id, "uuid-cs2"); - assert_eq!(p, [10.0, 20.0, 70.0, 30.0, 55.0, 90.0, -5.0, 80.0]); - let back = corners_affine(&p, [0.0, 0.0, 40.0, 30.0]).unwrap(); - for (a, b) in back.m.iter().zip(t.m) { - assert!((a - b).abs() < 1e-12, "{back:?} vs {t:?}"); - } - assert_eq!(corners_perspective([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], (40.0, 30.0)), None, "affine corners"); - assert_eq!(corners_affine(&p, [0.0, 0.0, 0.0, 30.0]), None); - assert_eq!(corners_affine(&p, [0.0, 0.0, f64::NAN, 30.0]), None); - assert_eq!(corners_affine(&p, [5.0, 0.0, 5.0, 30.0]), None, "an empty frame"); - let mut nan = p; - nan[3] = f64::INFINITY; - assert_eq!(corners_affine(&nan, [0.0, 0.0, 40.0, 30.0]), None); - // The placed-layer record is the one block that can be the principal one: PlLd-only - // layers have no smart transform from `parse_smart`. - assert_eq!(parse_smart(b"PlLd", &data), (String::new(), Affine::IDENTITY)); - } - - /// A `PlLd` record's corners frame the same box as `SoLd`'s `Trnf` (#3677): with a custom - /// warp whose control points spill past the source, the control-point box, not the source - /// rect. The CS2 record then places the layer exactly as the equivalent `SoLd` does, with or - /// without the source size (the box needs none). - #[test] - fn plld_corners_frame_a_spilled_custom_warp_like_sold() { - use crate::smart_map::{PlacedSpec, parse_sold, plld_bytes, sold_bytes}; - use photocraft_geom::warp::{BezierMesh, Warp}; - let bounds = [0.0, 0.0, 50.0, 25.0]; - let mut mesh = BezierMesh::identity(bounds, 1, 1); - mesh.points[0] = [-10.0, -5.0]; - mesh.points[15] = [60.0, 25.0]; - let warp = Warp::custom(mesh, bounds); - let t = Affine { m: [2.0, 0.5, -0.25, 2.0, 120.0, 210.0] }; - let spec = PlacedSpec { idnt: "cs2", placed: "p", transform: t, perspective: None, size: (50.0, 25.0), dpi: 72.0, warp: Some(&warp), filter_fx: None }; - let plld = plld_bytes(&spec, &mut Vec::new()); - let sold = sold_bytes(None, &spec, &mut Vec::new()); - let (_, p) = parse_plld(&plld).expect("a plcL record"); - assert_eq!(parse_plld_warp(&plld), PlldWarp::Warp(warp.clone())); - let from_sold = parse_smart(b"SoLd", &sold).1; - assert_eq!(parse_sold(&sold).map(|s| s.transform), Some(from_sold)); - let close = |a: Affine, b: Affine| a.m.iter().zip(b.m).all(|(x, y)| (x - y).abs() < 1e-9); - assert!(close(from_sold, t), "{from_sold:?} vs {t:?}"); - for size in [Some((50.0, 25.0)), None] { - let (got, perspective) = plld_transform(&p, size, Some(&warp)); - assert!(close(got, from_sold), "size {size:?}: {got:?} vs SoLd {from_sold:?}"); - assert_eq!(perspective, None); - } - // Framing the source rect instead would stretch it (the bug #3677 fixed for SoLd). - let rect = corners_affine(&p, [0.0, 0.0, 50.0, 25.0]).unwrap(); - assert!(!close(rect, t), "the test needs a mesh that spills: {rect:?}"); - // With no warp, or a preset one, the frame is the source rect, as before. - let plain = PlacedSpec { warp: None, ..spec }; - let (_, q) = parse_plld(&plld_bytes(&plain, &mut Vec::new())).unwrap(); - assert!(close(plld_transform(&q, Some((50.0, 25.0)), None).0, t)); - } - - /// Malformed `PlLd` data (wrong magic, truncated anywhere, a Pascal length past the end, - /// non-finite corners) reads as `None`, never a panic. - #[test] - fn plld_malformed_is_none() { - use crate::smart_map::{PlacedSpec, plld_bytes}; - let spec = - PlacedSpec { idnt: "id", placed: "", transform: Affine::IDENTITY, perspective: None, size: (4.0, 4.0), dpi: 72.0, warp: None, filter_fx: None }; - let data = plld_bytes(&spec, &mut Vec::new()); - // Magic + version + Pascal "id" + 4 × u32 + 8 × f64: everything after is the warp. - let end = 4 + 4 + 1 + 2 + 16 + 64; - assert!(parse_plld(&data[..end]).is_some()); - for n in 0..end { - assert_eq!(parse_plld(&data[..n]), None, "truncated to {n} bytes"); - } - let mut bad = data.clone(); - bad[..4].copy_from_slice(b"soLD"); - assert_eq!(parse_plld(&bad), None); - let mut long = data.clone(); - long[8] = 255; // a Pascal id longer than the record - assert_eq!(parse_plld(&long[..200.min(long.len())]), None); - let mut nan = data.clone(); - nan[end - 8..end].copy_from_slice(&f64::NAN.to_be_bytes()); - assert_eq!(parse_plld(&nan), None); - assert_eq!(parse_plld(&[]), None); - } - - /// Photoshop CS2 can warp a smart object; `PlLd` keeps that warp after the corners (a u32 - /// warp version, then the versioned `warp` descriptor). It reads back as written, and - /// `warpNone`, an identity warp or a record that stops at its corners mean no warp. - #[test] - fn plld_warps_round_trip() { - use crate::smart_map::{PlacedSpec, plld_bytes}; - use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; - let bounds = [0.0, 0.0, 60.0, 30.0]; - let mut arc = Warp::preset(WarpStyle::Arc, 40.0, bounds); - arc.vertical = true; - arc.h_distort = 10.0; - let mut mesh = BezierMesh::identity(bounds, 1, 1); - mesh.points[15] = [67.0, 30.0]; - let custom = Warp::custom(mesh, bounds); - let flat = Warp::preset(WarpStyle::Flag, 0.0, bounds); - for (w, back) in - [(Some(&arc), PlldWarp::Warp(arc.clone())), (Some(&custom), PlldWarp::Warp(custom.clone())), (Some(&flat), PlldWarp::None), (None, PlldWarp::None)] - { - let t = Affine { m: [0.5, 0.0, 0.0, 0.5, 3.0, 4.0] }; - let spec = PlacedSpec { idnt: "w", placed: "", transform: t, perspective: None, size: (60.0, 30.0), dpi: 72.0, warp: w, filter_fx: None }; - let data = plld_bytes(&spec, &mut Vec::new()); - assert_eq!(parse_plld_warp(&data), back, "{w:?}"); - // The corners are unchanged by the warp that follows them. - assert_eq!(parse_plld(&data).map(|(_, p)| p[..2].to_vec()), Some(vec![3.0, 4.0])); - } - let spec = - PlacedSpec { idnt: "id", placed: "", transform: Affine::IDENTITY, perspective: None, size: (4.0, 4.0), dpi: 72.0, warp: None, filter_fx: None }; - let end = 4 + 4 + 1 + 2 + 16 + 64; - let data = plld_bytes(&spec, &mut Vec::new()); - assert_eq!(parse_plld_warp(&data[..end]), PlldWarp::None, "no warp stored"); - assert_eq!(parse_plld_warp(&[data[..end].to_vec(), vec![0; 3]].concat()), PlldWarp::None, "padding"); - } - - /// A `PlLd` warp that is stored but can't be read (truncated, an unknown style, a missing - /// bounds rectangle or mesh) is `Unreadable`, so the import keeps the warped pixels. - #[test] - fn plld_unreadable_warps() { - use crate::smart_map::{PlacedSpec, plld_bytes}; - use photocraft_geom::warp::{Warp, WarpStyle}; - let arc = Warp::preset(WarpStyle::Arc, 40.0, [0.0, 0.0, 4.0, 4.0]); - let spec = PlacedSpec { - idnt: "id", - placed: "", - transform: Affine::IDENTITY, - perspective: None, - size: (4.0, 4.0), - dpi: 72.0, - warp: Some(&arc), - filter_fx: None, - }; - let data = plld_bytes(&spec, &mut Vec::new()); - let end = 4 + 4 + 1 + 2 + 16 + 64; - assert!(matches!(parse_plld_warp(&data), PlldWarp::Warp(_))); - for n in end + 1..data.len() - 4 { - if data[end..n].iter().any(|b| *b != 0) { - assert_eq!(parse_plld_warp(&data[..n]), PlldWarp::Unreadable, "truncated to {n} bytes"); - } - } - let with = |d: Descriptor| [data[..end].to_vec(), vec![0; 4], VersionedDescriptor::new(d).to_bytes()].concat(); - let style = |s: &str| Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new(s) }; - assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpSomethingNew")))), PlldWarp::Unreadable); - assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpArc")))), PlldWarp::Unreadable, "no bounds"); - assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpNone")))), PlldWarp::None); - let bounds = Descriptor::new("classFloatRect") - .with("Top ", Value::Double(0.0)) - .with("Left", Value::Double(0.0)) - .with("Btom", Value::Double(4.0)) - .with("Rght", Value::Double(4.0)); - let custom = Descriptor::new("warp").with("warpStyle", style("warpCustom")).with("bounds", Value::Descriptor(bounds)); - assert_eq!(parse_plld_warp(&with(custom)), PlldWarp::Unreadable, "custom with no mesh"); - assert_eq!(parse_plld_warp(&[data[..end].to_vec(), vec![0, 0, 0, 0, 0, 0, 0, 16, 1, 2]].concat()), PlldWarp::Unreadable); - assert_eq!(parse_plld_warp(&data[..end - 1]), PlldWarp::Unreadable, "the record itself"); - assert_eq!(parse_plld_warp(&[]), PlldWarp::Unreadable); - } - - /// `SoLd` data with a 50 × 25 source, the quad `trnf` and, when given, a custom warp whose - /// 16 control points are `pts`. - fn sold_with_warp(trnf: [f64; 8], pts: Option<&[[f64; 2]]>) -> Vec { - use photocraft_psd::descriptor::ObjectArray; - let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; - let mut d = Descriptor::new("null") - .with("Idnt", Value::Text(UnicodeString::new_nul("uuid-1"))) - .with("Trnf", Value::List(trnf.map(Value::Double).to_vec())) - .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(50.0)).with("Hght", Value::Double(25.0)))); - if let Some(pts) = pts { - let bounds = Descriptor::new("Rctn") - .with("Top ", Value::Double(0.0)) - .with("Left", Value::Double(0.0)) - .with("Btom", Value::Double(25.0)) - .with("Rght", Value::Double(50.0)); - let mesh = Descriptor::new("null") - .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[0]).collect() }) - .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[1]).collect() }); - let warp = Descriptor::new("warp").with("warpStyle", e("warpStyle", "warpCustom")).with("bounds", Value::Descriptor(bounds)).with( - "customEnvelopeWarp", - Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body: mesh }))), - ); - d = d.with("warp", Value::Descriptor(warp)); - } - let mut data = b"soLD".to_vec(); - data.extend_from_slice(&4u32.to_be_bytes()); - data.extend(VersionedDescriptor::new(d).to_bytes()); - data - } - - /// Photoshop's `Trnf` is the image of the custom warp's control-point bounding box, not of the - /// source rect: a mesh spilling to x = -10, y = -5 and x = 60 frames a 70 × 30 box. - #[test] - fn smart_transform_maps_the_warp_control_box_onto_the_quad() { - let mut pts = photocraft_geom::warp::BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 1, 1).points; - pts[0] = [-10.0, -5.0]; - pts[15] = [60.0, 25.0]; - let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; - let (_, a) = parse_smart(b"SoLd", &sold_with_warp(quad, Some(&pts))); - assert_eq!(a.m, [2.0, 0.0, 0.0, 2.0, 120.0, 210.0]); - // Without a warp the source rect is the frame, as before. - assert_eq!(parse_smart(b"SoLd", &sold_with_warp(quad, None)).1.m, [2.8, 0.0, 0.0, 2.4, 100.0, 200.0]); - } - - /// A custom mesh with no area (every control point on one spot) can't frame the quad: the - /// source rect does, with no division by zero. - #[test] - fn smart_transform_with_a_degenerate_warp_box_uses_the_source_rect() { - let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; - let (_, a) = parse_smart(b"SoLd", &sold_with_warp(quad, Some(&[[7.0, 7.0]; 16]))); - assert_eq!(a.m, [2.8, 0.0, 0.0, 2.4, 100.0, 200.0]); - let mut warp = photocraft_geom::warp::Warp::preset(photocraft_geom::warp::WarpStyle::Custom, 0.0, [0.0, 0.0, 50.0, 25.0]); - warp.mesh = Some(photocraft_geom::warp::BezierMesh { us: vec![0.0, 1.0], vs: vec![0.0, 1.0], points: vec![[f64::NAN, 1.0]; 16] }); - assert_eq!(placed_frame(Some(&warp), 50.0, 25.0), [0.0, 0.0, 50.0, 25.0]); - assert_eq!(placed_frame(None, 50.0, 25.0), [0.0, 0.0, 50.0, 25.0]); - } - - #[test] - fn effects_switch() { - let mut lfx = vec![0, 0, 0, 0]; - lfx.extend(VersionedDescriptor::new(Descriptor::new("null").with("masterFXSwitch", Value::Boolean(false))).to_bytes()); - assert!(!effects_enabled(&lfx)); - assert!(effects_enabled(&[0, 0, 0, 0])); - } - - #[test] - fn tysh_garbage_is_none() { - assert!(parse_tysh(&[0; 10]).is_none()); - } -} +mod more_tests; diff --git a/crates/io/src/blocks/more_tests.rs b/crates/io/src/blocks/more_tests.rs new file mode 100644 index 0000000000..3c41296134 --- /dev/null +++ b/crates/io/src/blocks/more_tests.rs @@ -0,0 +1,496 @@ +use super::*; + +#[test] +fn float_rgb_colors_parse() { + let d = Descriptor::new("RGBC").with("redFloat", Value::Double(1.0)).with("greenFloat", Value::Double(0.5)).with("blueFloat", Value::Double(0.25)); + let c = color_from_desc(&d).unwrap(); + assert_eq!(c.to_rgb(), [1.0, 0.5, 0.25]); +} + +#[test] +fn smart_transform_from_sold() { + let pts = [10.0, 20.0, 110.0, 20.0, 110.0, 70.0, 10.0, 70.0].map(Value::Double).to_vec(); + let d = Descriptor::new("null") + .with("Idnt", Value::Text(UnicodeString::new_nul("uuid-1"))) + .with("Trnf", Value::List(pts)) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(50.0)).with("Hght", Value::Double(25.0)))); + let mut data = b"soLD".to_vec(); + data.extend_from_slice(&4u32.to_be_bytes()); + data.extend(VersionedDescriptor::new(d).to_bytes()); + data.extend_from_slice(&[0, 0]); + let (id, a) = parse_smart(b"SoLd", &data); + assert_eq!(id, "uuid-1"); + assert_eq!(a.m, [2.0, 0.0, 0.0, 2.0, 10.0, 20.0]); + assert_eq!(parse_smart(b"PlLd", &data).0, ""); + assert_eq!(parse_plld(&data), None, "soLD data is not a plcL record"); +} + +/// A `PlLd` record (as Photoshop CS2 writes it, with no `SoLd`, #2465) gives its source id and +/// placed corners; with the source size those are the same transform `SoLd` gives. +#[test] +fn plld_id_and_corners() { + use crate::smart_map::{PlacedSpec, plld_bytes}; + let t = Affine { m: [1.5, 0.25, -0.5, 2.0, 10.0, 20.0] }; + let spec = PlacedSpec { idnt: "uuid-cs2", placed: "", transform: t, perspective: None, size: (40.0, 30.0), dpi: 72.0, warp: None, filter_fx: None }; + let data = plld_bytes(&spec, &mut Vec::new()); + let (id, p) = parse_plld(&data).expect("a plcL record"); + assert_eq!(id, "uuid-cs2"); + assert_eq!(p, [10.0, 20.0, 70.0, 30.0, 55.0, 90.0, -5.0, 80.0]); + let back = corners_affine(&p, [0.0, 0.0, 40.0, 30.0]).unwrap(); + for (a, b) in back.m.iter().zip(t.m) { + assert!((a - b).abs() < 1e-12, "{back:?} vs {t:?}"); + } + assert_eq!(corners_perspective([[p[0], p[1]], [p[2], p[3]], [p[4], p[5]], [p[6], p[7]]], (40.0, 30.0)), None, "affine corners"); + assert_eq!(corners_affine(&p, [0.0, 0.0, 0.0, 30.0]), None); + assert_eq!(corners_affine(&p, [0.0, 0.0, f64::NAN, 30.0]), None); + assert_eq!(corners_affine(&p, [5.0, 0.0, 5.0, 30.0]), None, "an empty frame"); + let mut nan = p; + nan[3] = f64::INFINITY; + assert_eq!(corners_affine(&nan, [0.0, 0.0, 40.0, 30.0]), None); + // The placed-layer record is the one block that can be the principal one: PlLd-only + // layers have no smart transform from `parse_smart`. + assert_eq!(parse_smart(b"PlLd", &data), (String::new(), Affine::IDENTITY)); +} + +/// A `PlLd` record's corners frame the same box as `SoLd`'s `Trnf` (#3677): with a custom +/// warp whose control points spill past the source, the control-point box, not the source +/// rect. The CS2 record then places the layer exactly as the equivalent `SoLd` does, with or +/// without the source size (the box needs none). +#[test] +fn plld_corners_frame_a_spilled_custom_warp_like_sold() { + use crate::smart_map::{PlacedSpec, parse_sold, plld_bytes, sold_bytes}; + use photocraft_geom::warp::{BezierMesh, Warp}; + let bounds = [0.0, 0.0, 50.0, 25.0]; + let mut mesh = BezierMesh::identity(bounds, 1, 1); + mesh.points[0] = [-10.0, -5.0]; + mesh.points[15] = [60.0, 25.0]; + let warp = Warp::custom(mesh, bounds); + let t = Affine { m: [2.0, 0.5, -0.25, 2.0, 120.0, 210.0] }; + let spec = PlacedSpec { idnt: "cs2", placed: "p", transform: t, perspective: None, size: (50.0, 25.0), dpi: 72.0, warp: Some(&warp), filter_fx: None }; + let plld = plld_bytes(&spec, &mut Vec::new()); + let sold = sold_bytes(None, &spec, &mut Vec::new()); + let (_, p) = parse_plld(&plld).expect("a plcL record"); + assert_eq!(parse_plld_warp(&plld), PlldWarp::Warp(warp.clone())); + let from_sold = parse_smart(b"SoLd", &sold).1; + assert_eq!(parse_sold(&sold).map(|s| s.transform), Some(from_sold)); + let close = |a: Affine, b: Affine| a.m.iter().zip(b.m).all(|(x, y)| (x - y).abs() < 1e-9); + assert!(close(from_sold, t), "{from_sold:?} vs {t:?}"); + for size in [Some((50.0, 25.0)), None] { + let (got, perspective) = plld_transform(&p, size, Some(&warp)); + assert!(close(got, from_sold), "size {size:?}: {got:?} vs SoLd {from_sold:?}"); + assert_eq!(perspective, None); + } + // Framing the source rect instead would stretch it (the bug #3677 fixed for SoLd). + let rect = corners_affine(&p, [0.0, 0.0, 50.0, 25.0]).unwrap(); + assert!(!close(rect, t), "the test needs a mesh that spills: {rect:?}"); + // With no warp, or a preset one, the frame is the source rect, as before. + let plain = PlacedSpec { warp: None, ..spec }; + let (_, q) = parse_plld(&plld_bytes(&plain, &mut Vec::new())).unwrap(); + assert!(close(plld_transform(&q, Some((50.0, 25.0)), None).0, t)); +} + +/// Malformed `PlLd` data (wrong magic, truncated anywhere, a Pascal length past the end, +/// non-finite corners) reads as `None`, never a panic. +#[test] +fn plld_malformed_is_none() { + use crate::smart_map::{PlacedSpec, plld_bytes}; + let spec = PlacedSpec { idnt: "id", placed: "", transform: Affine::IDENTITY, perspective: None, size: (4.0, 4.0), dpi: 72.0, warp: None, filter_fx: None }; + let data = plld_bytes(&spec, &mut Vec::new()); + // Magic + version + Pascal "id" + 4 × u32 + 8 × f64: everything after is the warp. + let end = 4 + 4 + 1 + 2 + 16 + 64; + assert!(parse_plld(&data[..end]).is_some()); + for n in 0..end { + assert_eq!(parse_plld(&data[..n]), None, "truncated to {n} bytes"); + } + let mut bad = data.clone(); + bad[..4].copy_from_slice(b"soLD"); + assert_eq!(parse_plld(&bad), None); + let mut long = data.clone(); + long[8] = 255; // a Pascal id longer than the record + assert_eq!(parse_plld(&long[..200.min(long.len())]), None); + let mut nan = data.clone(); + nan[end - 8..end].copy_from_slice(&f64::NAN.to_be_bytes()); + assert_eq!(parse_plld(&nan), None); + assert_eq!(parse_plld(&[]), None); +} + +/// Photoshop CS2 can warp a smart object; `PlLd` keeps that warp after the corners (a u32 +/// warp version, then the versioned `warp` descriptor). It reads back as written, and +/// `warpNone`, an identity warp or a record that stops at its corners mean no warp. +#[test] +fn plld_warps_round_trip() { + use crate::smart_map::{PlacedSpec, plld_bytes}; + use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; + let bounds = [0.0, 0.0, 60.0, 30.0]; + let mut arc = Warp::preset(WarpStyle::Arc, 40.0, bounds); + arc.vertical = true; + arc.h_distort = 10.0; + let mut mesh = BezierMesh::identity(bounds, 1, 1); + mesh.points[15] = [67.0, 30.0]; + let custom = Warp::custom(mesh, bounds); + let flat = Warp::preset(WarpStyle::Flag, 0.0, bounds); + for (w, back) in + [(Some(&arc), PlldWarp::Warp(arc.clone())), (Some(&custom), PlldWarp::Warp(custom.clone())), (Some(&flat), PlldWarp::None), (None, PlldWarp::None)] + { + let t = Affine { m: [0.5, 0.0, 0.0, 0.5, 3.0, 4.0] }; + let spec = PlacedSpec { idnt: "w", placed: "", transform: t, perspective: None, size: (60.0, 30.0), dpi: 72.0, warp: w, filter_fx: None }; + let data = plld_bytes(&spec, &mut Vec::new()); + assert_eq!(parse_plld_warp(&data), back, "{w:?}"); + // The corners are unchanged by the warp that follows them. + assert_eq!(parse_plld(&data).map(|(_, p)| p[..2].to_vec()), Some(vec![3.0, 4.0])); + } + let spec = PlacedSpec { idnt: "id", placed: "", transform: Affine::IDENTITY, perspective: None, size: (4.0, 4.0), dpi: 72.0, warp: None, filter_fx: None }; + let end = 4 + 4 + 1 + 2 + 16 + 64; + let data = plld_bytes(&spec, &mut Vec::new()); + assert_eq!(parse_plld_warp(&data[..end]), PlldWarp::None, "no warp stored"); + assert_eq!(parse_plld_warp(&[data[..end].to_vec(), vec![0; 3]].concat()), PlldWarp::None, "padding"); +} + +/// A `PlLd` warp that is stored but can't be read (truncated, an unknown style, a missing +/// bounds rectangle or mesh) is `Unreadable`, so the import keeps the warped pixels. +#[test] +fn plld_unreadable_warps() { + use crate::smart_map::{PlacedSpec, plld_bytes}; + use photocraft_geom::warp::{Warp, WarpStyle}; + let arc = Warp::preset(WarpStyle::Arc, 40.0, [0.0, 0.0, 4.0, 4.0]); + let spec = + PlacedSpec { idnt: "id", placed: "", transform: Affine::IDENTITY, perspective: None, size: (4.0, 4.0), dpi: 72.0, warp: Some(&arc), filter_fx: None }; + let data = plld_bytes(&spec, &mut Vec::new()); + let end = 4 + 4 + 1 + 2 + 16 + 64; + assert!(matches!(parse_plld_warp(&data), PlldWarp::Warp(_))); + for n in end + 1..data.len() - 4 { + if data[end..n].iter().any(|b| *b != 0) { + assert_eq!(parse_plld_warp(&data[..n]), PlldWarp::Unreadable, "truncated to {n} bytes"); + } + } + let with = |d: Descriptor| [data[..end].to_vec(), vec![0; 4], VersionedDescriptor::new(d).to_bytes()].concat(); + let style = |s: &str| Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new(s) }; + assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpSomethingNew")))), PlldWarp::Unreadable); + assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpArc")))), PlldWarp::Unreadable, "no bounds"); + assert_eq!(parse_plld_warp(&with(Descriptor::new("warp").with("warpStyle", style("warpNone")))), PlldWarp::None); + let bounds = Descriptor::new("classFloatRect") + .with("Top ", Value::Double(0.0)) + .with("Left", Value::Double(0.0)) + .with("Btom", Value::Double(4.0)) + .with("Rght", Value::Double(4.0)); + let custom = Descriptor::new("warp").with("warpStyle", style("warpCustom")).with("bounds", Value::Descriptor(bounds)); + assert_eq!(parse_plld_warp(&with(custom)), PlldWarp::Unreadable, "custom with no mesh"); + assert_eq!(parse_plld_warp(&[data[..end].to_vec(), vec![0, 0, 0, 0, 0, 0, 0, 16, 1, 2]].concat()), PlldWarp::Unreadable); + assert_eq!(parse_plld_warp(&data[..end - 1]), PlldWarp::Unreadable, "the record itself"); + assert_eq!(parse_plld_warp(&[]), PlldWarp::Unreadable); +} + +/// `SoLd` data with a 50 × 25 source, the quad `trnf` and, when given, a custom warp whose +/// 16 control points are `pts`. +fn sold_with_warp(trnf: [f64; 8], pts: Option<&[[f64; 2]]>) -> Vec { + use photocraft_psd::descriptor::ObjectArray; + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let mut d = Descriptor::new("null") + .with("Idnt", Value::Text(UnicodeString::new_nul("uuid-1"))) + .with("Trnf", Value::List(trnf.map(Value::Double).to_vec())) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(50.0)).with("Hght", Value::Double(25.0)))); + if let Some(pts) = pts { + let bounds = Descriptor::new("Rctn") + .with("Top ", Value::Double(0.0)) + .with("Left", Value::Double(0.0)) + .with("Btom", Value::Double(25.0)) + .with("Rght", Value::Double(50.0)); + let mesh = Descriptor::new("null") + .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[0]).collect() }) + .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[1]).collect() }); + let warp = Descriptor::new("warp").with("warpStyle", e("warpStyle", "warpCustom")).with("bounds", Value::Descriptor(bounds)).with( + "customEnvelopeWarp", + Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body: mesh }))), + ); + d = d.with("warp", Value::Descriptor(warp)); + } + let mut data = b"soLD".to_vec(); + data.extend_from_slice(&4u32.to_be_bytes()); + data.extend(VersionedDescriptor::new(d).to_bytes()); + data +} + +/// `SoLd` data with a split (quilt) custom warp, as Photoshop writes it: a `quiltWarp` +/// descriptor with `deformNumCols` × `deformNumRows` control points and the patch boundaries +/// in `quiltSliceX` / `quiltSliceY`. With `plain_warp`, a `warpNone` `warp` key sits next to it. +fn sold_with_quilt(trnf: [f64; 8], cols: f64, rows: f64, sx: &[f64], sy: &[f64], pts: &[[f64; 2]], plain_warp: bool) -> Vec { + use photocraft_psd::descriptor::ObjectArray; + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let bounds = || { + Descriptor::new("Rctn") + .with("Top ", Value::Double(0.0)) + .with("Left", Value::Double(0.0)) + .with("Btom", Value::Double(25.0)) + .with("Rght", Value::Double(50.0)) + }; + let slices = |k: &str, v: &[f64]| { + let body = Descriptor::new("null").with(k, Value::UnitFloats { unit: *b"#Pxl", values: v.to_vec() }); + Value::ObjectArray(ObjectArray { prefix: v.len() as u32, body }) + }; + let mesh = Descriptor::new("null") + .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[0]).collect() }) + .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: pts.iter().map(|p| p[1]).collect() }); + let envelope = Descriptor::new("customEnvelopeWarp") + .with("quiltSliceX", slices("quiltSliceX", sx)) + .with("quiltSliceY", slices("quiltSliceY", sy)) + .with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body: mesh })); + let quilt = Descriptor::new("quiltWarp") + .with("warpStyle", e("warpStyle", "warpCustom")) + .with("bounds", Value::Descriptor(bounds())) + .with("deformNumRows", Value::Integer(rows as i32)) + .with("deformNumCols", Value::Double(cols)) + .with("customEnvelopeWarp", Value::Descriptor(envelope)); + let mut d = Descriptor::new("null") + .with("Idnt", Value::Text(UnicodeString::new_nul("uuid-1"))) + .with("Trnf", Value::List(trnf.map(Value::Double).to_vec())) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(50.0)).with("Hght", Value::Double(25.0)))); + if plain_warp { + d = d + .with("warp", Value::Descriptor(Descriptor::new("warp").with("warpStyle", e("warpStyle", "warpNone")).with("bounds", Value::Descriptor(bounds())))); + } + d = d.with("quiltWarp", Value::Descriptor(quilt)); + let mut data = b"soLD".to_vec(); + data.extend_from_slice(&4u32.to_be_bytes()); + data.extend(VersionedDescriptor::new(d).to_bytes()); + data +} + +/// A 3 × 2 patch identity grid over the 50 × 25 source with one interior control point pulled. +fn quilt_points() -> Vec<[f64; 2]> { + let mut pts = photocraft_geom::warp::BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 3, 2).points; + if let Some(p) = pts.get_mut(10 * 3 + 4) { + p[1] += 3.0; + } + pts +} + +/// Photoshop's split warp (`quiltWarp`, mockups' usual warp) reads as a multi-patch mesh: the +/// patch boundaries become the knots, the control points stay in order, and the quilt wins over +/// the `warpNone` `warp` key written next to it. Before, any mesh but 4 × 4 was dropped and the +/// layer re-rendered flat. +#[test] +fn quilt_warp_is_read_as_a_multi_patch_mesh() { + let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; + let pts = quilt_points(); + // Photoshop pads the outer boundaries a little past the grid's edges. + let data = sold_with_quilt(quad, 10.0, 7.0, &[-0.5, 12.5, 37.5, 50.5], &[-0.5, 12.5, 25.5], &pts, true); + let warp = parse_placed_warp(b"SoLd", &data).expect("a quilt warp"); + let mesh = warp.mesh.as_ref().expect("its mesh"); + assert_eq!(mesh.points, pts); + assert_eq!(mesh.us.len(), 4); + assert_eq!(mesh.vs.len(), 3); + assert_eq!((mesh.us[0], mesh.us[3], mesh.vs[0], mesh.vs[2]), (0.0, 1.0, 0.0, 1.0)); + assert!((mesh.us[1] - 13.0 / 51.0).abs() < 1e-12 && (mesh.vs[1] - 13.0 / 26.0).abs() < 1e-12); + assert!(mesh.is_valid()); +} + +/// A quilt's control points frame the placed quad as a single patch's do (#3677): a grid +/// spilling to x = -10, y = -5 and x = 60 frames a 70 × 30 box. +#[test] +fn quilt_warp_control_box_frames_the_quad() { + let mut pts = quilt_points(); + pts[0] = [-10.0, -5.0]; + if let Some(last) = pts.last_mut() { + *last = [60.0, 25.0]; + } + let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; + let data = sold_with_quilt(quad, 10.0, 7.0, &[0.0, 12.5, 37.5, 50.0], &[0.0, 12.5, 25.0], &pts, false); + assert_eq!(parse_smart(b"SoLd", &data).1.m, [2.0, 0.0, 0.0, 2.0, 120.0, 210.0]); +} + +/// An inconsistent quilt reads as no warp rather than a wrong one, and never panics. +#[test] +fn inconsistent_quilt_warps_are_not_read() { + let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; + let pts = quilt_points(); + let (sx, sy) = ([0.0, 12.5, 37.5, 50.0], [0.0, 12.5, 25.0]); + let read = + |cols: f64, rows: f64, sx: &[f64], sy: &[f64], pts: &[[f64; 2]]| parse_placed_warp(b"SoLd", &sold_with_quilt(quad, cols, rows, sx, sy, pts, false)); + assert!(read(10.0, 7.0, &sx, &sy, &pts).is_some()); + assert!(read(9.0, 7.0, &sx, &sy, &pts).is_none(), "not three points per patch side plus one"); + assert!(read(13.0, 7.0, &sx, &sy, &pts).is_none(), "more points claimed than stored"); + assert!(read(10.0, 7.0, &[0.0, 12.5, 50.0], &sy, &pts).is_none(), "one slice per patch boundary"); + assert!(read(10.0, 7.0, &[0.0, 37.5, 12.5, 50.0], &sy, &pts).is_none(), "slices must increase"); + assert!(read(10.0, 7.0, &[0.0, f64::NAN, 37.5, 50.0], &sy, &pts).is_none()); + assert!(read(1e9, 7.0, &sx, &sy, &pts).is_none(), "an absurd grid is refused before any arithmetic"); + assert!(read(10.0, 7.0, &sx, &sy, &pts[..pts.len() - 1]).is_none()); + let mut nan = pts.clone(); + nan[3] = [f64::NAN, 0.0]; + assert!(read(10.0, 7.0, &sx, &sy, &nan).is_none()); +} + +/// Photoshop's `Trnf` is the image of the custom warp's control-point bounding box, not of the +/// source rect: a mesh spilling to x = -10, y = -5 and x = 60 frames a 70 × 30 box. +#[test] +fn smart_transform_maps_the_warp_control_box_onto_the_quad() { + let mut pts = photocraft_geom::warp::BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 1, 1).points; + pts[0] = [-10.0, -5.0]; + pts[15] = [60.0, 25.0]; + let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; + let (_, a) = parse_smart(b"SoLd", &sold_with_warp(quad, Some(&pts))); + assert_eq!(a.m, [2.0, 0.0, 0.0, 2.0, 120.0, 210.0]); + // Without a warp the source rect is the frame, as before. + assert_eq!(parse_smart(b"SoLd", &sold_with_warp(quad, None)).1.m, [2.8, 0.0, 0.0, 2.4, 100.0, 200.0]); +} + +/// A custom mesh with no area (every control point on one spot) can't frame the quad: the +/// source rect does, with no division by zero. +#[test] +fn smart_transform_with_a_degenerate_warp_box_uses_the_source_rect() { + let quad = [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]; + let (_, a) = parse_smart(b"SoLd", &sold_with_warp(quad, Some(&[[7.0, 7.0]; 16]))); + assert_eq!(a.m, [2.8, 0.0, 0.0, 2.4, 100.0, 200.0]); + let mut warp = photocraft_geom::warp::Warp::preset(photocraft_geom::warp::WarpStyle::Custom, 0.0, [0.0, 0.0, 50.0, 25.0]); + warp.mesh = Some(photocraft_geom::warp::BezierMesh { us: vec![0.0, 1.0], vs: vec![0.0, 1.0], points: vec![[f64::NAN, 1.0]; 16] }); + assert_eq!(placed_frame(Some(&warp), 50.0, 25.0), [0.0, 0.0, 50.0, 25.0]); + assert_eq!(placed_frame(None, 50.0, 25.0), [0.0, 0.0, 50.0, 25.0]); +} + +/// A `SoLd` placed on `quad` (`Trnf` and `nonAffineTransform`) for a 1000 × 1000 source, +/// warped by `warp`. +fn sold_placed(quad: [f64; 8], warp: Descriptor) -> Vec { + let list = Value::List(quad.iter().map(|v| Value::Double(*v)).collect()); + let d = Descriptor::new("null") + .with("Trnf", list.clone()) + .with("nonAffineTransform", list) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(1000.0)).with("Hght", Value::Double(1000.0)))) + .with("warp", Value::Descriptor(warp)); + let mut data = b"soLD".to_vec(); + data.extend_from_slice(&4u32.to_be_bytes()); + data.extend(VersionedDescriptor::new(d).to_bytes()); + data +} + +/// A `warpCylinder` descriptor as #4112 describes it: horizontal, no perspective, seven +/// `warpValues` in the space of `bounds`. +fn cylinder_desc(values: Value) -> Descriptor { + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let bounds = Descriptor::new("classFloatRect") + .with("Top ", Value::Double(0.0)) + .with("Left", Value::Double(0.0)) + .with("Btom", Value::Double(1000.0)) + .with("Rght", Value::Double(1000.0)); + Descriptor::new("warp") + .with("warpStyle", e("warpStyle", "warpCylinder")) + .with("warpValue", Value::Double(0.0)) + .with("warpValues", values) + .with("warpPerspective", Value::Double(0.0)) + .with("warpPerspectiveOther", Value::Double(0.0)) + .with("warpRotate", e("Ornt", "Hrzn")) + .with("bounds", Value::Descriptor(bounds)) +} + +const CYL: [f64; 7] = [100.0, 800.0, 900.0, 200.0, 0.3, 0.3, 0.5]; + +fn doubles(v: &[f64]) -> Value { + Value::List(v.iter().map(|x| Value::Double(*x)).collect()) +} + +/// A projective placement is framed by the warp `parse_placed_warp` imports: an unreadable +/// `quiltWarp` next to a cylinder under `warp` imports no warp, so the source rect is framed. +#[test] +fn perspective_frame_follows_the_warp_parse_placed_warp_reads() { + let quad = [10.0, 20.0, 410.0, 60.0, 400.0, 480.0, 30.0, 520.0]; + let list = Value::List(quad.iter().map(|v| Value::Double(*v)).collect()); + let unreadable = Descriptor::new("quiltWarp").with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpSomethingNew") }); + let d = Descriptor::new("null") + .with("Trnf", list.clone()) + .with("nonAffineTransform", list) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(1000.0)).with("Hght", Value::Double(1000.0)))) + .with("quiltWarp", Value::Descriptor(unreadable)) + .with("warp", Value::Descriptor(cylinder_desc(doubles(&CYL)))); + let mut data = b"soLD".to_vec(); + data.extend_from_slice(&4u32.to_be_bytes()); + data.extend(VersionedDescriptor::new(d).to_bytes()); + assert_eq!(parse_placed_warp(b"SoLd", &data), None); + let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &data).expect("projective")); + let (px, py) = h.apply(1000.0, 1000.0); + assert!((px - 400.0).abs() < 1e-6 && (py - 480.0).abs() < 1e-6, "{px},{py}"); +} +/// #4112: a `warpCylinder` placed layer comes in as the cylinder its seven values describe, +/// instead of no warp (which re-rendered the contents flat). +#[test] +fn placed_cylinder_warp_is_read() { + use photocraft_geom::warp::{Warp, WarpStyle}; + let quad = [0.0, 0.0, 800.0, 0.0, 800.0, 800.0, 0.0, 800.0]; + let want = Warp::cylinder(&CYL, [0.0, 0.0, 1000.0, 1000.0]).unwrap(); + let w = parse_placed_warp(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).expect("a cylinder"); + assert_eq!(w, want); + assert_eq!(w.style, WarpStyle::Cylinder); + // Values written as one unit-float list read the same. + let floats = Value::UnitFloats { unit: *b"#Pxl", values: CYL.to_vec() }; + assert_eq!(parse_placed_warp(b"SoLE", &sold_placed(quad, cylinder_desc(floats))), Some(want)); +} + +/// Cylinders nobody has measured (vertical, with perspective) and broken values stay +/// unreadable, as every cylinder was before: the layer keeps the pixels Photoshop stored. +#[test] +fn placed_cylinder_warps_that_cant_be_read() { + let quad = [0.0, 0.0, 800.0, 0.0, 800.0, 800.0, 0.0, 800.0]; + let read = |d: Descriptor| parse_placed_warp(b"SoLd", &sold_placed(quad, d)); + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let with = |key: &str, v: Value| { + let mut d = cylinder_desc(doubles(&CYL)); + d.items.iter_mut().filter(|(k, _)| k.is(key)).for_each(|(_, old)| *old = v.clone()); + d + }; + assert!(read(with("warpRotate", e("Ornt", "Vrtc"))).is_none(), "vertical"); + assert!(read(with("warpPerspective", Value::Double(20.0))).is_none(), "perspective"); + assert!(read(with("warpPerspectiveOther", Value::Double(-5.0))).is_none(), "the other perspective"); + assert!(read(cylinder_desc(doubles(&CYL[..6]))).is_none(), "six values"); + assert!(read(cylinder_desc(Value::List(vec![Value::Text(UnicodeString::new_nul("x")); 7]))).is_none(), "not numbers"); + assert!(read(cylinder_desc(Value::Double(1.0))).is_none(), "one number"); + let mut flipped = CYL; + flipped.swap(0, 2); + assert!(read(cylinder_desc(doubles(&flipped))).is_none(), "silhouettes swapped"); + let mut no_values = cylinder_desc(doubles(&CYL)); + no_values.items.retain(|(k, _)| !k.is("warpValues")); + assert!(read(no_values).is_none(), "no warpValues"); +} + +/// Like a custom warp's (#3677), a cylinder's control-point box is what the placement quad +/// frames, for affine corners and for projective ones (`nonAffineTransform`). +#[test] +fn placed_cylinder_quad_frames_its_control_box() { + use photocraft_geom::warp::Warp; + let cb = Warp::cylinder(&CYL, [0.0, 0.0, 1000.0, 1000.0]).unwrap().mesh.unwrap().control_bounds(); + // Affine: the box lands on the quad. + let quad = [10.0, 20.0, 410.0, 20.0, 410.0, 520.0, 10.0, 520.0]; + let (_, a) = parse_smart(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))); + let [m0, m1, m2, m3, m4, m5] = a.m; + let map = |x: f64, y: f64| (m0 * x + m2 * y + m4, m1 * x + m3 * y + m5); + for ((x, y), (qx, qy)) in + [(cb[0], cb[1]), (cb[2], cb[1]), (cb[2], cb[3]), (cb[0], cb[3])].into_iter().zip(quad.as_chunks::<2>().0.iter().map(|p| (p[0], p[1]))) + { + let (px, py) = map(x, y); + assert!((px - qx).abs() < 1e-9 && (py - qy).abs() < 1e-9, "{x},{y} → {px},{py}, not {qx},{qy}"); + } + assert!(parse_smart_perspective(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).is_none(), "affine"); + // Projective: the same box lands on the four corners. + let quad = [10.0, 20.0, 410.0, 60.0, 400.0, 480.0, 30.0, 520.0]; + let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &sold_placed(quad, cylinder_desc(doubles(&CYL)))).expect("projective")); + for ((x, y), (qx, qy)) in + [(cb[0], cb[1]), (cb[2], cb[1]), (cb[2], cb[3]), (cb[0], cb[3])].into_iter().zip(quad.as_chunks::<2>().0.iter().map(|p| (p[0], p[1]))) + { + let (px, py) = h.apply(x, y); + assert!((px - qx).abs() < 1e-6 && (py - qy).abs() < 1e-6, "{x},{y} → {px},{py}, not {qx},{qy}"); + } + // Without a warp a projective quad still frames the source rect. + let none = Descriptor::new("warp").with("warpStyle", Value::Enumerated { type_id: Id::new("warpStyle"), value: Id::new("warpNone") }); + let h = photocraft_algo::transform::Homography(parse_smart_perspective(b"SoLd", &sold_placed(quad, none)).expect("projective")); + let (px, py) = h.apply(1000.0, 1000.0); + assert!((px - 400.0).abs() < 1e-6 && (py - 480.0).abs() < 1e-6); +} + +#[test] +fn effects_switch() { + let mut lfx = vec![0, 0, 0, 0]; + lfx.extend(VersionedDescriptor::new(Descriptor::new("null").with("masterFXSwitch", Value::Boolean(false))).to_bytes()); + assert!(!effects_enabled(&lfx)); + assert!(effects_enabled(&[0, 0, 0, 0])); +} + +#[test] +fn tysh_garbage_is_none() { + assert!(parse_tysh(&[0; 10]).is_none()); +} diff --git a/crates/io/src/blocks/tests.rs b/crates/io/src/blocks/tests.rs new file mode 100644 index 0000000000..59193a9344 --- /dev/null +++ b/crates/io/src/blocks/tests.rs @@ -0,0 +1,335 @@ +use super::*; + +fn noise_gradient_fill(min: [i32; 4], max: [i32; 4], add_transparency: bool) -> Descriptor { + let values = |v: [i32; 4]| Value::List(v.into_iter().map(Value::Integer).collect()); + let gradient = Descriptor::new("Grdn") + .with("GrdF", Value::Enumerated { type_id: Id::new("GrdF"), value: Id::new("ClNs") }) + .with("ClrS", Value::Enumerated { type_id: Id::new("ClrS"), value: Id::new("RGBC") }) + .with("ShTr", Value::Boolean(add_transparency)) + .with("VctC", Value::Boolean(true)) + .with("RndS", Value::Integer(9052369)) + .with("Smth", Value::Integer(2048)) + .with("Mnm ", values(min)) + .with("Mxm ", values(max)); + Descriptor::new("null").with("Grad", Value::Descriptor(gradient)) +} + +#[test] +fn constant_rgb_noise_fill_uses_its_exact_colour() { + use photocraft_doc::Fill; + let fill = fill_from_desc(b"GdFl", &noise_gradient_fill([50, 25, 0, 0], [50, 25, 0, 100], false)); + let Some(Fill::Gradient { stops, .. }) = fill else { panic!("constant noise gradient") }; + assert_eq!(stops, vec![(0.0, Color::rgb(0.5, 0.25, 0.0)), (1.0, Color::rgb(0.5, 0.25, 0.0))]); +} + +#[test] +fn variable_or_transparent_noise_stays_a_fill_with_the_placeholder_ramp() { + use photocraft_doc::Fill; + // Photoshop's default Noise gradient (min 0, max 100) and a transparent variant: no + // exact colour is provable, but the fill must parse — the layer keeps its type, its + // stored pixels (`fill_cache`) and its raw `GdFl` block. Only the uncached preview + // shows the placeholder ramp. + for descriptor in [noise_gradient_fill([0, 0, 0, 0], [100, 100, 100, 100], false), noise_gradient_fill([50, 25, 0, 0], [50, 25, 0, 100], true)] { + let Some(Fill::Gradient { stops, .. }) = fill_from_desc(b"GdFl", &descriptor) else { panic!("variable noise gradient must stay a fill layer") }; + assert_eq!(stops, vec![(0.0, Color::BLACK), (1.0, Color::WHITE)]); + } +} + +/// psd-tools layers-minimal/gradient-fill.psd ("Color to Transparent", no stored pixels): a +/// gradient fill's opacity stops reach the model (as its opacity stops; written from stop +/// alpha) and survive a write; the ramp's alpha follows them. +#[test] +fn gradient_fill_opacity_stops_become_alpha() { + use photocraft_color::Color; + use photocraft_doc::{Fill, GradientStyle}; + let red = Color::rgba(1.0, 0.0, 0.0, 1.0); + let f = Fill::gradient(vec![(0.0, red), (1.0, Color { alpha: 0.0, ..red })], 90.0, 1.0, GradientStyle::Linear, false); + let (k, data) = super::write_fill(&f); + let back = super::parse_fill(&k, &data).unwrap(); + let Fill::Gradient { stops, opacity_stops, .. } = &back else { panic!("gradient") }; + assert_eq!(opacity_stops, &vec![(0.0, 1.0), (1.0, 0.0)]); + assert!(stops.iter().all(|s| s.1.c[0] == 1.0 && s.1.c[1] == 0.0)); + let ramp = photocraft_compose::gradient_fill::Ramp::new(&back).unwrap(); + assert_eq!(ramp.sample(0.0)[3], 1.0); + assert!(ramp.sample(1.0)[3] < 0.01); + // Opaque gradients come back without opacity stops. + let (k, data) = super::write_fill(&Fill::gradient(vec![(0.0, red), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false)); + let Some(Fill::Gradient { opacity_stops, .. }) = super::parse_fill(&k, &data) else { panic!("gradient") }; + assert!(opacity_stops.is_empty()); +} + +/// Photoshop 27.11 stores a gradient's method as the enum `gradientsInterpolationMethod` +/// (seen in gradient fill layers saved by scripted Photoshop): a black→white Linear gradient +/// is at 188 of 255 halfway (measured), where Classic gives 128. The key used to be read as +/// `gs99`, which Photoshop doesn't write, so every gradient opened as Classic. +#[test] +fn gradient_fill_reads_the_interpolation_method() { + use photocraft_color::Color; + use photocraft_doc::{Fill, GradientStyle}; + use photocraft_psd::descriptor::{Id, Value}; + let f = Fill::gradient(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], 0.0, 1.0, GradientStyle::Linear, false); + let (k, data) = super::write_fill(&f); + let d = super::parse_prefix_versioned(&data).unwrap(); + let mid = |code: &str| { + let d = d.clone().with("gradientsInterpolationMethod", Value::Enumerated { type_id: Id::new("gradientInterpolationMethodType"), value: Id::new(code) }); + let back = super::fill_from_desc(&k, &d).unwrap(); + let ramp = photocraft_compose::gradient_fill::Ramp::new(&back).unwrap(); + (ramp.sample(0.5)[0] * 255.0).round() + }; + assert_eq!(mid("Gcls"), 128.0); + assert!((mid("Lnr ") - 188.0).abs() <= 1.0, "Linear: {}", mid("Lnr ")); + assert!((mid("Perc") - 99.0).abs() <= 2.0, "Perceptual: {}", mid("Perc")); + assert!((mid("Smoo") - 99.0).abs() <= 2.0, "Smooth: {}", mid("Smoo")); +} + +#[test] +fn lab_descriptor_colours_round_trip() { + use photocraft_color::ColorMode; + use photocraft_psd::descriptor::{Descriptor, Value}; + let d = Descriptor::new("LbCl").with("Lmnc", Value::Double(19.07)).with("A ", Value::Double(52.29)).with("B ", Value::Double(-85.08)); + let c = super::color_from_desc(&d).unwrap(); + assert_eq!(c.mode, ColorMode::Lab); + assert!((c.c[0] - 0.1907).abs() < 1e-5 && (c.c[1] * 255.0 - 128.0 - 52.29).abs() < 1e-3 && (c.c[2] * 255.0 - 128.0 + 85.08).abs() < 1e-3); + let back = super::color_from_desc(&super::color_to_desc(&c)).unwrap(); + assert_eq!(back.mode, ColorMode::Lab); + for i in 0..3 { + assert!((back.c[i] - c.c[i]).abs() < 1e-6); + } + // Missing components are rejected, not defaulted. + assert!(super::color_from_desc(&Descriptor::new("LbCl").with("Lmnc", Value::Double(50.0))).is_none()); +} + +/// Colours stored as HSB (`HSBC`: `H ` an angle in degrees, `Strt`/`Brgh` 0..100), as in +/// psd-tools `stroke-color-descriptors-hsb-with-rgb-mode.psd`, used to be dropped (#6774). +#[test] +fn hsb_descriptor_colours_parse() { + use photocraft_doc::Fill; + use photocraft_psd::descriptor::{Descriptor, Value}; + let hsb = |h: f64, s: f64, b: f64| { + Descriptor::new("HSBC").with("H ", Value::UnitFloat { unit: *b"#Ang", value: h }).with("Strt", Value::Double(s)).with("Brgh", Value::Double(b)) + }; + let close = |c: [f32; 3], e: [f32; 3]| c.iter().zip(e).all(|(a, b)| (a - b).abs() < 1e-5); + let green = super::color_from_desc(&hsb(120.0, 100.0, 100.0)).unwrap().to_rgb(); + assert!(close(green, [0.0, 1.0, 0.0]), "{green:?}"); + let white = super::color_from_desc(&hsb(0.0, 0.0, 100.0)).unwrap().to_rgb(); + assert!(close(white, [1.0, 1.0, 1.0]), "{white:?}"); + let half = super::color_from_desc(&hsb(240.0, 50.0, 50.0)).unwrap().to_rgb(); + assert!(close(half, [0.25, 0.25, 0.5]), "{half:?}"); + // A solid fill (`SoCo`) with an HSB colour is read, not lost. + let soco = Descriptor::new("null").with("Clr ", Value::Descriptor(hsb(0.0, 100.0, 100.0))); + let Some(Fill::Solid(c)) = super::fill_from_desc(b"SoCo", &soco) else { panic!("solid fill") }; + assert!(close(c.to_rgb(), [1.0, 0.0, 0.0]), "{:?}", c.to_rgb()); + // Missing components are rejected, not defaulted. + assert!(super::color_from_desc(&Descriptor::new("HSBC").with("Strt", Value::Double(50.0))).is_none()); +} + +#[test] +fn color_descriptor_keeps_photocraft_alpha() { + use photocraft_color::Color; + use photocraft_doc::Fill; + let c = Color::rgba(0.2, 0.4, 0.6, 0.25); + let (k, d) = super::fill_to_desc(&Fill::Solid(c)); + let Fill::Solid(back) = super::fill_from_desc(&k, &d).unwrap() else { panic!("solid") }; + assert!((back.alpha - 0.25).abs() < 1e-5); + assert!((back.to_rgb()[0] - 0.2).abs() < 1e-3); + let (k, d) = super::fill_to_desc(&Fill::Solid(Color::rgb(1.0, 0.0, 0.0))); + let Fill::Solid(opaque) = super::fill_from_desc(&k, &d).unwrap() else { panic!("solid") }; + assert!((opaque.alpha - 1.0).abs() < 1e-6); + assert!(super::color_to_desc(&c).get("pcAl").is_none(), "generic colour descriptors have no pcAl"); +} + +#[test] +fn non_finite_photocraft_alpha_is_ignored() { + use photocraft_color::Color; + for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + let d = super::color_to_desc(&Color::rgb(0.2, 0.4, 0.6)).with("pcAl", Value::Double(bad)); + let c = super::color_from_desc(&d).unwrap(); + assert_eq!(c.alpha, 1.0, "pcAl {bad}"); + } + let d = super::color_to_desc(&Color::rgb(0.2, 0.4, 0.6)).with("pcAl", Value::Double(0.5)); + assert!((super::color_from_desc(&d).unwrap().alpha - 0.5).abs() < 1e-6); +} + +#[test] +fn gradient_stop_alpha_round_trips_once() { + use photocraft_color::Color; + use photocraft_doc::Fill; + let fill = Fill::Gradient { + stops: vec![(0.0, Color::rgba(1.0, 0.0, 0.0, 0.5)), (1.0, Color::WHITE)], + angle: 90.0, + scale: 1.0, + style: photocraft_doc::GradientStyle::Linear, + reverse: false, + opacity_stops: vec![], + midpoints: vec![], + offset: (0.0, 0.0), + dither: false, + align: true, + }; + let (k, d) = super::fill_to_desc(&fill); + let Fill::Gradient { stops, opacity_stops, .. } = super::fill_from_desc(&k, &d).unwrap() else { panic!("grad") }; + assert!((stops[0].1.alpha - 1.0).abs() < 1e-4, "colour alpha lives in Trns, not the stop colour"); + assert!(opacity_stops.iter().any(|(_, a)| (*a - 0.5).abs() < 1e-3), "{opacity_stops:?}"); +} + +#[test] +fn blending_ranges_map_to_blend_if() { + use photocraft_doc::{BlendIf, BlendRange}; + use photocraft_psd::layer::BlendingRanges; + // Full ranges (what every layer without Blend If carries) → the default setting. + assert_eq!(super::blend_if_from_ranges(&BlendingRanges::full(3)), BlendIf::default()); + assert_eq!(super::blend_if_from_ranges(&BlendingRanges::default()), BlendIf::default()); + // Gray: This Layer black split 10/40; Blue (entry 3): Underlying white at 200. + let mut data = BlendingRanges::full(3).data; + data[..4].copy_from_slice(&[10, 40, 255, 255]); + data[3 * 8 + 4..3 * 8 + 8].copy_from_slice(&[0, 0, 200, 200]); + let b = super::blend_if_from_ranges(&BlendingRanges { data: data.clone() }); + assert_eq!(b.get(0), [BlendRange { black: [10, 40], white: [255, 255] }, BlendRange::FULL]); + assert_eq!(b.get(3), [BlendRange::FULL, BlendRange { black: [0, 0], white: [200, 200] }]); + assert_eq!(b.ranges.len(), 4); + assert_eq!(super::ranges_from_blend_if(&b, 3).data, data); + // The default writes full ranges for gray + each channel. + assert_eq!(super::ranges_from_blend_if(&BlendIf::default(), 4), BlendingRanges::full(4)); + // A trimmed setting is padded to the document's channel count. + let mut short = BlendIf::default(); + short.set(0, [BlendRange { black: [5, 5], white: [255, 255] }, BlendRange::FULL]); + let r = super::ranges_from_blend_if(&short, 3); + assert_eq!(r.data.len(), 4 * 8); + assert_eq!(&r.data[..8], &[5, 5, 255, 255, 0, 0, 255, 255]); + assert_eq!(&r.data[8..], &BlendingRanges::full(2).data[..]); +} + +#[test] +fn advanced_blending_blocks_round_trip() { + use photocraft_doc::{AdvancedBlending, Knockout}; + let a = AdvancedBlending { + knockout: Knockout::Deep, + blend_interior: true, + blend_clipped: false, + transparency_shapes: false, + layer_mask_hides_effects: true, + vector_mask_hides_effects: true, + }; + let mut raw = Vec::new(); + super::put_advanced(&a, &mut raw); + assert_eq!(raw.len(), 6); + assert!(raw.contains(&(*b"knko", vec![2, 0, 0, 0]))); + assert!(raw.contains(&(*b"clbl", vec![0, 0, 0, 0]))); + let get = |k: &[u8; 4]| raw.iter().find(|(bk, _)| bk == k).map(|(_, d)| d.as_slice()); + assert_eq!(super::advanced_from_blocks(get), a); + // Defaults: nothing added to a plain layer; existing blocks rewritten in place, in order. + let mut plain = Vec::new(); + super::put_advanced(&AdvancedBlending::default(), &mut plain); + assert!(plain.is_empty()); + let mut kept = vec![(*b"luni", vec![0; 4]), (*b"knko", vec![1, 0, 0, 0]), (*b"clbl", vec![1])]; + super::put_advanced(&AdvancedBlending::default(), &mut kept); + assert_eq!(kept, vec![(*b"luni", vec![0; 4]), (*b"knko", vec![0, 0, 0, 0]), (*b"clbl", vec![1, 0, 0, 0])]); + // Missing and empty blocks read as Photoshop's defaults. + assert!(super::advanced_from_blocks(|_| None).is_default()); + assert!(super::advanced_from_blocks(|_| Some(&[][..])).is_default()); + assert_eq!(super::advanced_from_blocks(|k| (k == b"knko").then_some(&[1u8][..])).knockout, Knockout::Shallow); +} + +#[test] +fn brst_round_trip() { + assert_eq!(super::parse_brst(&[0, 0, 0, 2]), 0b100); + assert_eq!(super::parse_brst(&[0, 0, 0, 0, 0, 0, 0, 1]), 0b11); + assert_eq!(super::brst_data(0b101).unwrap(), vec![0, 0, 0, 0, 0, 0, 0, 2]); + assert_eq!(super::brst_data(0), None); +} + +#[test] +fn placed_layer_warp_is_read() { + use photocraft_geom::warp::WarpStyle; + use photocraft_psd::descriptor::ObjectArray; + let e = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + let bounds = Descriptor::new("Rctn") + .with("Top ", Value::UnitFloat { unit: *b"#Pxl", value: 0.0 }) + .with("Left", Value::UnitFloat { unit: *b"#Pxl", value: 0.0 }) + .with("Btom", Value::UnitFloat { unit: *b"#Pxl", value: 30.0 }) + .with("Rght", Value::UnitFloat { unit: *b"#Pxl", value: 60.0 }); + let sold = |warp: Descriptor| { + let mut v = b"soLD".to_vec(); + v.extend_from_slice(&4u32.to_be_bytes()); + v.extend(VersionedDescriptor::new(Descriptor::new("null").with("warp", Value::Descriptor(warp))).to_bytes()); + v + }; + let arc = Descriptor::new("warp") + .with("warpStyle", e("warpStyle", "warpArc")) + .with("warpValue", Value::Double(40.0)) + .with("warpPerspective", Value::Double(0.0)) + .with("warpPerspectiveOther", Value::Double(0.0)) + .with("warpRotate", e("Ornt", "Hrzn")) + .with("bounds", Value::Descriptor(bounds.clone())); + let w = parse_placed_warp(b"SoLd", &sold(arc)).unwrap(); + assert_eq!((w.style, w.bend, w.bounds), (WarpStyle::Arc, 40.0, [0.0, 0.0, 60.0, 30.0])); + let (mut xs, mut ys) = (Vec::new(), Vec::new()); + for j in 0..4 { + for i in 0..4 { + xs.push(f64::from(i) * 20.0 + if i == 3 && j == 3 { 7.0 } else { 0.0 }); + ys.push(f64::from(j) * 10.0); + } + } + let mesh = + Descriptor::new("null").with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: xs }).with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: ys }); + let custom = Descriptor::new("warp").with("warpStyle", e("warpStyle", "warpCustom")).with("bounds", Value::Descriptor(bounds)).with( + "customEnvelopeWarp", + Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body: mesh }))), + ); + let w = parse_placed_warp(b"SoLd", &sold(custom)).unwrap(); + assert_eq!(w.style, WarpStyle::Custom); + assert_eq!(w.mesh.as_ref().unwrap().points[15], [67.0, 30.0]); + assert!(parse_placed_warp(b"PlLd", &[0; 20]).is_none()); +} + +#[test] +fn locks_and_labels() { + // Reference values round-trip byte for byte; Background layers carry 0x0D + // (transparency + position + the bit-3 lock reference files write). + for bits in [0u32, 1, 2, 4, 8, 0x0D, 0x8000_0000, 0x8000_000F] { + assert_eq!(lspf_from_locks(&locks_from_lspf(bits)), bits); + } + // Older PhotoCraft files wrote the artboard lock at 0x10; it reads back as artboard + // and is rewritten at the reference value 0x08. + assert!(locks_from_lspf(0x10).artboard); + assert_eq!(lspf_from_locks(&locks_from_lspf(0x10)), 0x08); + for i in 0..8 { + assert_eq!(label_index(label_from_index(i)), i); + } +} + +#[test] +fn fills_roundtrip() { + for f in [ + Fill::Solid(Color::rgb(1.0, 0.5, 0.0)), + Fill::Solid(Color { mode: ColorMode::Cmyk, c: [0.1, 0.2, 0.3, 0.4], alpha: 1.0 }), + Fill::Solid(Color::gray(0.25)), + Fill::gradient(vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], 45.0, 1.0, GradientStyle::Reflected, true), + // A live gradient (Gradient tool): canvas-aligned, offset, midpoints, opacity, dither. + Fill::Gradient { + stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (0.5, Color::rgb(0.0, 1.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], + angle: -33.5, + scale: 2.25, + style: GradientStyle::Radial, + reverse: false, + opacity_stops: vec![(0.0, 1.0), (0.25, 0.5), (1.0, 0.0)], + midpoints: vec![0.25, 0.75], + offset: (0.125, -0.375), + dither: true, + align: false, + }, + Fill::Pattern { name: "Bubbles".into(), scale: 0.5, id: "abc".into(), angle: 30.0, link: false, phase: (3.0, -2.0) }, + ] { + let (k, d) = write_fill(&f); + let back = parse_fill(&k, &d).unwrap(); + match (&f, &back) { + (Fill::Solid(a), Fill::Solid(b)) => { + assert_eq!(a.mode, b.mode); + for i in 0..4 { + assert!((a.c[i] - b.c[i]).abs() < 1e-5); + } + } + _ => assert_eq!(back, f), + } + } +} diff --git a/crates/io/src/camera_raw_map.rs b/crates/io/src/camera_raw_map.rs index cfe3ad6be2..a53d6d40c6 100644 --- a/crates/io/src/camera_raw_map.rs +++ b/crates/io/src/camera_raw_map.rs @@ -343,329 +343,5 @@ pub(super) fn export_item(params: &J) -> Result { } #[cfg(test)] -mod tests { - use super::*; - use crate::smart_map::{UNSUPPORTED_FILTER, filter_from_item, item_for_filter}; - use photocraft_color::BlendMode; - - // Parameter values observed in the supplied 18.4 PSD. No source pixels or private - // document metadata are included in this synthetic regression fixture. - fn source_item() -> Descriptor { - let mut d = Descriptor::new(CLASS) - .with("CrVe", Value::Text(UnicodeString::new_nul("18.4"))) - .with("PrVN", Value::Integer(6)) - .with("PrVe", Value::Integer(251920384)) - .with("Ex12", Value::Double(1.15)); - for (key, value) in [ - ("Cr12", -38), - ("Hi12", 35), - ("Sh12", -50), - ("Wh12", 49), - ("Bk12", -34), - ("PC_S", 0), - ("PC_D", -21), - ("PC_L", 38), - ("PC_H", 0), - ("PC_1", 25), - ("PC_2", 50), - ("PC_3", 75), - ] { - d = d.with(key, Value::Integer(value)); - } - for (_, keys) in ARRAYS.iter().skip(1) { - for key in *keys { - let value = match *key { - "HA_R" => -50, - "HA_Y" => 29, - "HA_G" => 1, - _ => 0, - }; - d = d.with(key, Value::Integer(value)); - } - } - let f = SmartFilter { command: COMMAND.into(), params: J::Null, blend: BlendMode::Normal, opacity: 1.0, visible: true }; - Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul("Camera Raw Filter"))) - .with("blendOptions", blend_options(&f)) - .with("enab", Value::Boolean(true)) - .with("hasoptions", Value::Boolean(true)) - .with("FrgC", rgbc(0.0)) - .with("BckC", rgbc(255.0)) - .with("Fltr", Value::Descriptor(d)) - .with("filterID", Value::Integer(FILTER_ID)) - } - - fn all_controls_item() -> Descriptor { - let mut item = source_item(); - let mut d = get_desc(&item, "Fltr").unwrap().clone(); - set(&mut d, "WBal", Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new("WBal"), value: photocraft_psd::descriptor::Id::new("Cst ") }); - set(&mut d, "ShpR", Value::Double(1.2)); - for (key, value) in [ - ("Temp", -26), - ("Tint", -25), - ("CrTx", -31), - ("Cl12", 30), - ("Dhze", -34), - ("Vibr", 33), - ("Strt", 33), - ("Shrp", 43), - ("ShpD", 36), - ("ShpM", 30), - ("LNR ", 47), - ("LNRD", 50), - ("CNR ", 25), - ("CNRD", 50), - ("STSH", 117), - ("STSS", 52), - ("STHH", 316), - ("STHS", 70), - ("STB ", 35), - ("CgMH", 51), - ("CgMS", 51), - ("CgSL", -24), - ("CgML", 41), - ("CgHL", 28), - ("CgBl", 36), - ("CgGH", 20), - ("CgGS", 61), - ("CgGL", 45), - ("GRNA", 45), - ("GRNS", 25), - ("GRNF", 50), - ("PCVA", 31), - ("PCVM", 50), - ("PCVF", 50), - ("PCVR", 0), - ] { - set(&mut d, key, Value::Integer(value)); - } - for (key, neutral) in NEUTRAL_ONLY { - set(&mut d, key, Value::Integer(*neutral)); - } - for (key, x, y) in [("Crv ", 146, 102), ("CrvR", 90, 174), ("CrvG", 162, 122), ("CrvB", 129, 202)] { - set(&mut d, key, Value::List([0, 0, x, y, 255, 255].into_iter().map(Value::Integer).collect())); - } - set(&mut item, "Fltr", Value::Descriptor(d)); - item - } - - #[test] - fn all_observed_develop_controls_roundtrip_and_patch_only_the_edited_field() { - let source = all_controls_item(); - let f = filter_from_item(&source); - assert_eq!(f.command, COMMAND); - assert_eq!(f.params["temperature"], -26.0); - assert_eq!(f.params["texture"], -31.0); - assert!((f.params["sharpenRadius"].as_f64().unwrap() - 1.2).abs() < 1e-6); - assert_eq!(f.params["gradeHighlights"], json!({"hue": 316.0, "sat": 70.0, "lum": 28.0})); - assert_eq!(f.params["pointCurveBlue"], json!([[0.0, 0.0], [129.0, 202.0], [255.0, 255.0]])); - assert_eq!(item_for_filter(&f).unwrap(), source); - let defaults = default_params().unwrap(); - for (param, key) in SCALARS { - let mut edited = f.clone(); - let mut number = get_param(&defaults, param).unwrap().as_f64().unwrap(); - if get_param(&f.params, param).unwrap().as_f64() == Some(number) { - number += 1.0; - } - *get_param_mut(&mut edited.params, param).unwrap() = json!(number); - let mut expected = get_desc(&source, "Fltr").unwrap().clone(); - set(&mut expected, key, if matches!(*key, "Ex12" | "ShpR") { Value::Double(number) } else { Value::Integer(number as i32) }); - assert_eq!(get_desc(&item_for_filter(&edited).unwrap(), "Fltr"), Some(&expected), "{param}"); - } - for (param, key) in CURVES { - let mut edited = f.clone(); - edited.params[*param] = json!([]); - let item = item_for_filter(&edited).unwrap(); - let mut expected = get_desc(&source, "Fltr").unwrap().clone(); - set(&mut expected, key, Value::List([0, 0, 255, 255].into_iter().map(Value::Integer).collect())); - assert_eq!(get_desc(&item, "Fltr"), Some(&expected), "{param}"); - assert_eq!(filter_from_item(&item).params[*param], json!([]), "{param}"); - } - } - - #[test] - fn active_unimplemented_controls_and_malformed_curves_stay_opaque() { - for (key, neutral) in NEUTRAL_ONLY { - let mut item = all_controls_item(); - let mut d = get_desc(&item, "Fltr").unwrap().clone(); - set(&mut d, key, Value::Integer(neutral + 1)); - set(&mut item, "Fltr", Value::Descriptor(d)); - let f = filter_from_item(&item); - assert_eq!(f.command, UNSUPPORTED_FILTER, "{key}"); - assert_eq!(item_for_filter(&f).unwrap(), item, "{key}"); - } - for value in [ - Value::List(vec![Value::Integer(0)]), - Value::List([0, 0, 0, 255].into_iter().map(Value::Integer).collect()), - Value::List([0, 0, 255, 256].into_iter().map(Value::Integer).collect()), - Value::List(vec![Value::Double(0.0); 4]), - Value::List(vec![Value::Integer(0); 34]), - ] { - let mut item = all_controls_item(); - let mut d = get_desc(&item, "Fltr").unwrap().clone(); - set(&mut d, "Crv ", value); - set(&mut item, "Fltr", Value::Descriptor(d)); - let f = filter_from_item(&item); - assert_eq!(f.command, UNSUPPORTED_FILTER); - assert_eq!(item_for_filter(&f).unwrap(), item); - } - } - - #[test] - #[ignore = "requires a user-supplied Camera Raw PSD"] - fn all_sliders_fixture() { - let path = std::env::var("PHOTOCRAFT_CAMERA_RAW_PSD").expect("set PHOTOCRAFT_CAMERA_RAW_PSD"); - let doc = crate::import("fixture.psd", &std::fs::read(path).unwrap()).unwrap().document; - let original = doc - .walk() - .into_iter() - .find_map(|(_, _, layer)| { - let photocraft_doc::LayerContent::Smart(sm) = &layer.content else { return None }; - sm.smart_filters.iter().find_map(|f| { - let item = item_for_filter(f).ok()?; - get_desc(&item, "Fltr").is_some_and(|d| d.class_id.is(CLASS)).then_some(item) - }) - }) - .expect("a Camera Raw descriptor"); - assert_eq!(item_for_filter(&filter_from_item(&original)).unwrap(), original); - let mut projected = original.clone(); - let mut d = get_desc(&original, "Fltr").unwrap().clone(); - // This is a codec projection, not an editable replacement for the source filter: - // active Optics settings have no rendering counterpart, and PCVS is unverified. - d.items.retain(|(key, _)| !["MDis", "VigA", "PCVS"].iter().any(|k| key.is(k))); - set(&mut projected, "Fltr", Value::Descriptor(d)); - let mut f = filter_from_item(&projected); - assert_eq!(f.command, COMMAND); - assert_eq!(f.params["temperature"], -26.0); - assert_eq!(f.params["sharpenAmount"], 43.0); - assert_eq!(f.params["gradeGlobal"], json!({"hue": 20.0, "sat": 61.0, "lum": 45.0})); - assert_eq!(f.params["pointCurveRed"], json!([[0.0, 0.0], [90.0, 174.0], [255.0, 255.0]])); - assert_eq!(item_for_filter(&f).unwrap(), projected); - f.params["exposure"] = json!(0.5); - let mut expected = get_desc(&projected, "Fltr").unwrap().clone(); - set(&mut expected, "Ex12", Value::Double(0.5)); - assert_eq!(get_desc(&item_for_filter(&f).unwrap(), "Fltr"), Some(&expected)); - } - - #[test] - fn light_curve_and_hsl_are_editable_without_rewriting_unchanged_settings() { - let source = source_item().with("futureOuterData", Value::RawData(vec![1, 2, 3, 4])); - let mut f = filter_from_item(&source); - assert_eq!(f.command, COMMAND); - assert!((f.params["exposure"].as_f64().unwrap() - 1.15).abs() < 1e-6); - assert_eq!(f.params["contrast"], -38.0); - assert_eq!(f.params["curveDarks"], -21.0); - assert_eq!(f.params["curveLights"], 38.0); - assert_eq!(f.params["curveSplits"], json!([25.0, 50.0, 75.0])); - assert_eq!(f.params["hslHue"], json!([-50.0, 0.0, 29.0, 1.0, 0.0, 0.0, 0.0, 0.0])); - assert_eq!(item_for_filter(&f).unwrap(), source); - f.params["exposure"] = json!(0.0); - f.params["hslHue"][2] = json!(-20.0); - f.visible = false; - f.opacity = 0.4; - f.blend = BlendMode::Multiply; - let edited = item_for_filter(&f).unwrap(); - let mut expected = get_desc(&source, "Fltr").unwrap().clone(); - set(&mut expected, "Ex12", Value::Double(0.0)); - set(&mut expected, "HA_Y", Value::Integer(-20)); - assert_eq!(get_desc(&edited, "Fltr"), Some(&expected)); - assert_eq!(edited.get("futureOuterData"), source.get("futureOuterData")); - let back = filter_from_item(&edited); - assert_eq!((back.visible, back.blend, back.opacity), (false, BlendMode::Multiply, 0.4)); - assert_eq!(back.params["exposure"], 0.0); - assert_eq!(back.params["hslHue"][2], -20.0); - } - - #[test] - fn new_filters_write_every_verified_control_and_accept_explicit_defaults() { - let mut params = default_params().unwrap(); - params["seed"] = json!(0); - for (param, key) in SCALARS { - let number = match *key { - "Ex12" => 0.75, - "ShpR" => 1.2, - _ => 12.0, - }; - *get_param_mut(&mut params, param).unwrap() = json!(number); - } - for (param, keys) in ARRAYS { - params[*param] = if *param == "curveSplits" { json!([20, 45, 80]) } else { json!((0..keys.len()).map(|i| i as f64 + 1.0).collect::>()) }; - } - let f = SmartFilter { command: COMMAND.into(), params: params.clone(), blend: BlendMode::Screen, opacity: 0.5, visible: false }; - let item = item_for_filter(&f).unwrap(); - let back = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); - assert_eq!((&back.command, back.visible, back.blend, back.opacity), (&f.command, false, BlendMode::Screen, 0.5)); - // Compare slider numbers after both sides have been normalized through CameraRaw. - let typed: CameraRaw = serde_json::from_value(params).unwrap(); - let expected = serde_json::to_value(typed).unwrap(); - for param in SCALARS.iter().map(|(p, _)| *p).chain(ARRAYS.iter().map(|(p, _)| *p)) { - assert_eq!(get_param(&back.params, param), get_param(&expected, param)); - } - } - - #[test] - fn unsupported_processing_settings_remain_opaque_and_lossless() { - for (key, value) in [ - ("LCs ", Value::Text(UnicodeString::new("unknown masks"))), - ("PrVN", Value::Integer(7)), - ("PrVe", Value::Integer(123)), - ("Ex12", Value::Double(f64::INFINITY)), - ("Cr12", Value::Text(UnicodeString::new("bad type"))), - ] { - let mut item = source_item(); - let mut d = get_desc(&item, "Fltr").unwrap().clone(); - set(&mut d, key, value); - set(&mut item, "Fltr", Value::Descriptor(d)); - let f = filter_from_item(&item); - assert_eq!(f.command, UNSUPPORTED_FILTER, "{key}"); - assert_eq!(item_for_filter(&f).unwrap(), item, "{key}"); - } - } - - #[test] - fn fractional_slider_levels_round_instead_of_dropping_the_filter() { - let mut f = filter_from_item(&source_item()); - f.params["contrast"] = json!(-12.4); - f.params["hslHue"][0] = json!(10.5); - f.params["gradeShadows"]["hue"] = json!(359.7); - f.params["pointCurve"] = json!([[0, 0], [100.2, 120.6], [200.6, 180.4], [255, 255]]); - let item = item_for_filter(&f).unwrap(); - let d = get_desc(&item, "Fltr").unwrap(); - assert_eq!(d.get("Cr12"), Some(&Value::Integer(-12))); - assert_eq!(d.get("HA_R"), Some(&Value::Integer(11))); - assert_eq!(d.get("STSH"), Some(&Value::Integer(0))); - assert_eq!(d.get("Crv "), Some(&Value::List([0, 0, 100, 121, 201, 180, 255, 255].into_iter().map(Value::Integer).collect()))); - let back = filter_from_item(&item); - assert_eq!(back.command, COMMAND); - assert_eq!(back.params["contrast"], -12.0); - } - - #[test] - fn unsupported_or_malformed_exports_report_errors() { - for params in [ - J::Null, - json!({"exposure": "bright"}), - json!({"exposure": 1e40}), - json!({"exposure": 6}), - json!({"contrast": 101}), - json!({"hslHue": [1, 2]}), - json!({"curveSplits": [80, 50, 20]}), - json!({"curveSplits": [25, 25, 75]}), - json!({"pointCurve": [[0, 0], [0, 255]]}), - json!({"sharpenRadius": 4}), - json!({"gradeShadows": {"hue": 361}}), - json!({"gradeShadows": {"unknown": 1}}), - json!({"seed": 1}), - json!({"vignetteHighlights": 10}), - json!({"futureSetting": 1}), - json!({TEMPLATE_KEY: "zz"}), - json!({TEMPLATE_KEY: "00"}), - json!({TEMPLATE_KEY: 12}), - json!({TEMPLATE_KEY: "0".repeat(MAX_TEMPLATE_BYTES * 2 + 1)}), - ] { - assert!(export_item(¶ms).is_err()); - } - let wrong = Descriptor::new("filterFX").with("filterID", Value::Integer(1)); - assert!(export_item(&json!({TEMPLATE_KEY: to_hex(&wrong.to_bytes())})).is_err()); - } -} +#[path = "camera_raw_map/tests.rs"] +mod tests; diff --git a/crates/io/src/camera_raw_map/tests.rs b/crates/io/src/camera_raw_map/tests.rs new file mode 100644 index 0000000000..ad40acb25d --- /dev/null +++ b/crates/io/src/camera_raw_map/tests.rs @@ -0,0 +1,324 @@ +use super::*; +use crate::smart_map::{UNSUPPORTED_FILTER, filter_from_item, item_for_filter}; +use photocraft_color::BlendMode; + +// Parameter values observed in the supplied 18.4 PSD. No source pixels or private +// document metadata are included in this synthetic regression fixture. +fn source_item() -> Descriptor { + let mut d = Descriptor::new(CLASS) + .with("CrVe", Value::Text(UnicodeString::new_nul("18.4"))) + .with("PrVN", Value::Integer(6)) + .with("PrVe", Value::Integer(251920384)) + .with("Ex12", Value::Double(1.15)); + for (key, value) in [ + ("Cr12", -38), + ("Hi12", 35), + ("Sh12", -50), + ("Wh12", 49), + ("Bk12", -34), + ("PC_S", 0), + ("PC_D", -21), + ("PC_L", 38), + ("PC_H", 0), + ("PC_1", 25), + ("PC_2", 50), + ("PC_3", 75), + ] { + d = d.with(key, Value::Integer(value)); + } + for (_, keys) in ARRAYS.iter().skip(1) { + for key in *keys { + let value = match *key { + "HA_R" => -50, + "HA_Y" => 29, + "HA_G" => 1, + _ => 0, + }; + d = d.with(key, Value::Integer(value)); + } + } + let f = SmartFilter { command: COMMAND.into(), params: J::Null, blend: BlendMode::Normal, opacity: 1.0, visible: true }; + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Camera Raw Filter"))) + .with("blendOptions", blend_options(&f)) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(d)) + .with("filterID", Value::Integer(FILTER_ID)) +} + +fn all_controls_item() -> Descriptor { + let mut item = source_item(); + let mut d = get_desc(&item, "Fltr").unwrap().clone(); + set(&mut d, "WBal", Value::Enumerated { type_id: photocraft_psd::descriptor::Id::new("WBal"), value: photocraft_psd::descriptor::Id::new("Cst ") }); + set(&mut d, "ShpR", Value::Double(1.2)); + for (key, value) in [ + ("Temp", -26), + ("Tint", -25), + ("CrTx", -31), + ("Cl12", 30), + ("Dhze", -34), + ("Vibr", 33), + ("Strt", 33), + ("Shrp", 43), + ("ShpD", 36), + ("ShpM", 30), + ("LNR ", 47), + ("LNRD", 50), + ("CNR ", 25), + ("CNRD", 50), + ("STSH", 117), + ("STSS", 52), + ("STHH", 316), + ("STHS", 70), + ("STB ", 35), + ("CgMH", 51), + ("CgMS", 51), + ("CgSL", -24), + ("CgML", 41), + ("CgHL", 28), + ("CgBl", 36), + ("CgGH", 20), + ("CgGS", 61), + ("CgGL", 45), + ("GRNA", 45), + ("GRNS", 25), + ("GRNF", 50), + ("PCVA", 31), + ("PCVM", 50), + ("PCVF", 50), + ("PCVR", 0), + ] { + set(&mut d, key, Value::Integer(value)); + } + for (key, neutral) in NEUTRAL_ONLY { + set(&mut d, key, Value::Integer(*neutral)); + } + for (key, x, y) in [("Crv ", 146, 102), ("CrvR", 90, 174), ("CrvG", 162, 122), ("CrvB", 129, 202)] { + set(&mut d, key, Value::List([0, 0, x, y, 255, 255].into_iter().map(Value::Integer).collect())); + } + set(&mut item, "Fltr", Value::Descriptor(d)); + item +} + +#[test] +fn all_observed_develop_controls_roundtrip_and_patch_only_the_edited_field() { + let source = all_controls_item(); + let f = filter_from_item(&source); + assert_eq!(f.command, COMMAND); + assert_eq!(f.params["temperature"], -26.0); + assert_eq!(f.params["texture"], -31.0); + assert!((f.params["sharpenRadius"].as_f64().unwrap() - 1.2).abs() < 1e-6); + assert_eq!(f.params["gradeHighlights"], json!({"hue": 316.0, "sat": 70.0, "lum": 28.0})); + assert_eq!(f.params["pointCurveBlue"], json!([[0.0, 0.0], [129.0, 202.0], [255.0, 255.0]])); + assert_eq!(item_for_filter(&f).unwrap(), source); + let defaults = default_params().unwrap(); + for (param, key) in SCALARS { + let mut edited = f.clone(); + let mut number = get_param(&defaults, param).unwrap().as_f64().unwrap(); + if get_param(&f.params, param).unwrap().as_f64() == Some(number) { + number += 1.0; + } + *get_param_mut(&mut edited.params, param).unwrap() = json!(number); + let mut expected = get_desc(&source, "Fltr").unwrap().clone(); + set(&mut expected, key, if matches!(*key, "Ex12" | "ShpR") { Value::Double(number) } else { Value::Integer(number as i32) }); + assert_eq!(get_desc(&item_for_filter(&edited).unwrap(), "Fltr"), Some(&expected), "{param}"); + } + for (param, key) in CURVES { + let mut edited = f.clone(); + edited.params[*param] = json!([]); + let item = item_for_filter(&edited).unwrap(); + let mut expected = get_desc(&source, "Fltr").unwrap().clone(); + set(&mut expected, key, Value::List([0, 0, 255, 255].into_iter().map(Value::Integer).collect())); + assert_eq!(get_desc(&item, "Fltr"), Some(&expected), "{param}"); + assert_eq!(filter_from_item(&item).params[*param], json!([]), "{param}"); + } +} + +#[test] +fn active_unimplemented_controls_and_malformed_curves_stay_opaque() { + for (key, neutral) in NEUTRAL_ONLY { + let mut item = all_controls_item(); + let mut d = get_desc(&item, "Fltr").unwrap().clone(); + set(&mut d, key, Value::Integer(neutral + 1)); + set(&mut item, "Fltr", Value::Descriptor(d)); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{key}"); + assert_eq!(item_for_filter(&f).unwrap(), item, "{key}"); + } + for value in [ + Value::List(vec![Value::Integer(0)]), + Value::List([0, 0, 0, 255].into_iter().map(Value::Integer).collect()), + Value::List([0, 0, 255, 256].into_iter().map(Value::Integer).collect()), + Value::List(vec![Value::Double(0.0); 4]), + Value::List(vec![Value::Integer(0); 34]), + ] { + let mut item = all_controls_item(); + let mut d = get_desc(&item, "Fltr").unwrap().clone(); + set(&mut d, "Crv ", value); + set(&mut item, "Fltr", Value::Descriptor(d)); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +#[test] +#[ignore = "requires a user-supplied Camera Raw PSD"] +fn all_sliders_fixture() { + let path = std::env::var("PHOTOCRAFT_CAMERA_RAW_PSD").expect("set PHOTOCRAFT_CAMERA_RAW_PSD"); + let doc = crate::import("fixture.psd", &std::fs::read(path).unwrap()).unwrap().document; + let original = doc + .walk() + .into_iter() + .find_map(|(_, _, layer)| { + let photocraft_doc::LayerContent::Smart(sm) = &layer.content else { return None }; + sm.smart_filters.iter().find_map(|f| { + let item = item_for_filter(f).ok()?; + get_desc(&item, "Fltr").is_some_and(|d| d.class_id.is(CLASS)).then_some(item) + }) + }) + .expect("a Camera Raw descriptor"); + assert_eq!(item_for_filter(&filter_from_item(&original)).unwrap(), original); + let mut projected = original.clone(); + let mut d = get_desc(&original, "Fltr").unwrap().clone(); + // This is a codec projection, not an editable replacement for the source filter: + // active Optics settings have no rendering counterpart, and PCVS is unverified. + d.items.retain(|(key, _)| !["MDis", "VigA", "PCVS"].iter().any(|k| key.is(k))); + set(&mut projected, "Fltr", Value::Descriptor(d)); + let mut f = filter_from_item(&projected); + assert_eq!(f.command, COMMAND); + assert_eq!(f.params["temperature"], -26.0); + assert_eq!(f.params["sharpenAmount"], 43.0); + assert_eq!(f.params["gradeGlobal"], json!({"hue": 20.0, "sat": 61.0, "lum": 45.0})); + assert_eq!(f.params["pointCurveRed"], json!([[0.0, 0.0], [90.0, 174.0], [255.0, 255.0]])); + assert_eq!(item_for_filter(&f).unwrap(), projected); + f.params["exposure"] = json!(0.5); + let mut expected = get_desc(&projected, "Fltr").unwrap().clone(); + set(&mut expected, "Ex12", Value::Double(0.5)); + assert_eq!(get_desc(&item_for_filter(&f).unwrap(), "Fltr"), Some(&expected)); +} + +#[test] +fn light_curve_and_hsl_are_editable_without_rewriting_unchanged_settings() { + let source = source_item().with("futureOuterData", Value::RawData(vec![1, 2, 3, 4])); + let mut f = filter_from_item(&source); + assert_eq!(f.command, COMMAND); + assert!((f.params["exposure"].as_f64().unwrap() - 1.15).abs() < 1e-6); + assert_eq!(f.params["contrast"], -38.0); + assert_eq!(f.params["curveDarks"], -21.0); + assert_eq!(f.params["curveLights"], 38.0); + assert_eq!(f.params["curveSplits"], json!([25.0, 50.0, 75.0])); + assert_eq!(f.params["hslHue"], json!([-50.0, 0.0, 29.0, 1.0, 0.0, 0.0, 0.0, 0.0])); + assert_eq!(item_for_filter(&f).unwrap(), source); + f.params["exposure"] = json!(0.0); + f.params["hslHue"][2] = json!(-20.0); + f.visible = false; + f.opacity = 0.4; + f.blend = BlendMode::Multiply; + let edited = item_for_filter(&f).unwrap(); + let mut expected = get_desc(&source, "Fltr").unwrap().clone(); + set(&mut expected, "Ex12", Value::Double(0.0)); + set(&mut expected, "HA_Y", Value::Integer(-20)); + assert_eq!(get_desc(&edited, "Fltr"), Some(&expected)); + assert_eq!(edited.get("futureOuterData"), source.get("futureOuterData")); + let back = filter_from_item(&edited); + assert_eq!((back.visible, back.blend, back.opacity), (false, BlendMode::Multiply, 0.4)); + assert_eq!(back.params["exposure"], 0.0); + assert_eq!(back.params["hslHue"][2], -20.0); +} + +#[test] +fn new_filters_write_every_verified_control_and_accept_explicit_defaults() { + let mut params = default_params().unwrap(); + params["seed"] = json!(0); + for (param, key) in SCALARS { + let number = match *key { + "Ex12" => 0.75, + "ShpR" => 1.2, + _ => 12.0, + }; + *get_param_mut(&mut params, param).unwrap() = json!(number); + } + for (param, keys) in ARRAYS { + params[*param] = if *param == "curveSplits" { json!([20, 45, 80]) } else { json!((0..keys.len()).map(|i| i as f64 + 1.0).collect::>()) }; + } + let f = SmartFilter { command: COMMAND.into(), params: params.clone(), blend: BlendMode::Screen, opacity: 0.5, visible: false }; + let item = item_for_filter(&f).unwrap(); + let back = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!((&back.command, back.visible, back.blend, back.opacity), (&f.command, false, BlendMode::Screen, 0.5)); + // Compare slider numbers after both sides have been normalized through CameraRaw. + let typed: CameraRaw = serde_json::from_value(params).unwrap(); + let expected = serde_json::to_value(typed).unwrap(); + for param in SCALARS.iter().map(|(p, _)| *p).chain(ARRAYS.iter().map(|(p, _)| *p)) { + assert_eq!(get_param(&back.params, param), get_param(&expected, param)); + } +} + +#[test] +fn unsupported_processing_settings_remain_opaque_and_lossless() { + for (key, value) in [ + ("LCs ", Value::Text(UnicodeString::new("unknown masks"))), + ("PrVN", Value::Integer(7)), + ("PrVe", Value::Integer(123)), + ("Ex12", Value::Double(f64::INFINITY)), + ("Cr12", Value::Text(UnicodeString::new("bad type"))), + ] { + let mut item = source_item(); + let mut d = get_desc(&item, "Fltr").unwrap().clone(); + set(&mut d, key, value); + set(&mut item, "Fltr", Value::Descriptor(d)); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{key}"); + assert_eq!(item_for_filter(&f).unwrap(), item, "{key}"); + } +} + +#[test] +fn fractional_slider_levels_round_instead_of_dropping_the_filter() { + let mut f = filter_from_item(&source_item()); + f.params["contrast"] = json!(-12.4); + f.params["hslHue"][0] = json!(10.5); + f.params["gradeShadows"]["hue"] = json!(359.7); + f.params["pointCurve"] = json!([[0, 0], [100.2, 120.6], [200.6, 180.4], [255, 255]]); + let item = item_for_filter(&f).unwrap(); + let d = get_desc(&item, "Fltr").unwrap(); + assert_eq!(d.get("Cr12"), Some(&Value::Integer(-12))); + assert_eq!(d.get("HA_R"), Some(&Value::Integer(11))); + assert_eq!(d.get("STSH"), Some(&Value::Integer(0))); + assert_eq!(d.get("Crv "), Some(&Value::List([0, 0, 100, 121, 201, 180, 255, 255].into_iter().map(Value::Integer).collect()))); + let back = filter_from_item(&item); + assert_eq!(back.command, COMMAND); + assert_eq!(back.params["contrast"], -12.0); +} + +#[test] +fn unsupported_or_malformed_exports_report_errors() { + for params in [ + J::Null, + json!({"exposure": "bright"}), + json!({"exposure": 1e40}), + json!({"exposure": 6}), + json!({"contrast": 101}), + json!({"hslHue": [1, 2]}), + json!({"curveSplits": [80, 50, 20]}), + json!({"curveSplits": [25, 25, 75]}), + json!({"pointCurve": [[0, 0], [0, 255]]}), + json!({"sharpenRadius": 4}), + json!({"gradeShadows": {"hue": 361}}), + json!({"gradeShadows": {"unknown": 1}}), + json!({"seed": 1}), + json!({"vignetteHighlights": 10}), + json!({"futureSetting": 1}), + json!({TEMPLATE_KEY: "zz"}), + json!({TEMPLATE_KEY: "00"}), + json!({TEMPLATE_KEY: 12}), + json!({TEMPLATE_KEY: "0".repeat(MAX_TEMPLATE_BYTES * 2 + 1)}), + ] { + assert!(export_item(¶ms).is_err()); + } + let wrong = Descriptor::new("filterFX").with("filterID", Value::Integer(1)); + assert!(export_item(&json!({TEMPLATE_KEY: to_hex(&wrong.to_bytes())})).is_err()); +} diff --git a/crates/io/src/camera_raw_xmp.rs b/crates/io/src/camera_raw_xmp.rs index 159e298a06..65df97e898 100644 --- a/crates/io/src/camera_raw_xmp.rs +++ b/crates/io/src/camera_raw_xmp.rs @@ -3,8 +3,9 @@ //! `x:xmpmeta/rdf:RDF/rdf:Description`, as attributes or as child elements. //! //! Loading applies only the settings the file holds, over the current ones, as Camera Raw's -//! Load Settings does; settings PhotoCraft has no control for (lens corrections, calibration, -//! geometry, defringe, …) are ignored and named in the report. Saving writes every supported +//! Load Settings does; settings PhotoCraft has no control for (lens corrections, geometry, +//! defringe, …) are ignored and named in the report. The same reader fills the dialog from a +//! raw file's sidecar (`IMG_0001.xmp` beside `IMG_0001.CR2`), and the writer keeps it. Saving writes every supported //! control, so loading the file in Photoshop reproduces the dialog. //! //! White balance: a Camera Raw Filter on a rendered image uses the relative @@ -100,6 +101,14 @@ const SCALARS: &[(&str, Field, f32, f32)] = &[ ("PostCropVignetteRoundness", |p| &mut p.vignette_roundness, -100.0, 100.0), ("PostCropVignetteFeather", |p| &mut p.vignette_feather, 0.0, 100.0), ("PostCropVignetteHighlightContrast", |p| &mut p.vignette_highlights, 0.0, 100.0), + // Calibration + ("ShadowTint", |p| &mut p.shadow_tint, -100.0, 100.0), + ("RedHue", |p| &mut p.calibration_hue[0], -100.0, 100.0), + ("RedSaturation", |p| &mut p.calibration_sat[0], -100.0, 100.0), + ("GreenHue", |p| &mut p.calibration_hue[1], -100.0, 100.0), + ("GreenSaturation", |p| &mut p.calibration_sat[1], -100.0, 100.0), + ("BlueHue", |p| &mut p.calibration_hue[2], -100.0, 100.0), + ("BlueSaturation", |p| &mut p.calibration_sat[2], -100.0, 100.0), ]; type Curve = fn(&mut CameraRaw) -> &mut Vec<[f32; 2]>; @@ -219,7 +228,7 @@ pub fn load(params: &mut CameraRaw, bytes: &[u8]) -> Result let text = std::str::from_utf8(bytes).map_err(|_| "not a Camera Raw settings file (not UTF-8 text)".to_string())?; // Some writers put a byte order mark or an `` wrapper first; roxmltree takes both. let text = text.trim_start_matches('\u{feff}'); - let doc = roxmltree::Document::parse(text).map_err(|e| format!("not a Camera Raw settings file ({e})"))?; + let doc = crate::xml::parse(text, roxmltree::ParsingOptions::default()).map_err(|e| format!("not a Camera Raw settings file ({e})"))?; let mut next = params.clone(); let mut report = LoadReport::default(); let mut found = false; @@ -265,6 +274,25 @@ fn escape(s: &str) -> String { s.replace('&', "&").replace('<', "<").replace('>', ">").replace('"', """) } +/// The XMP toolkit [`save`] names on `x:xmpmeta`. +const TOOLKIT: &str = "PhotoCraft"; +const META_NS: &str = "adobe:ns:meta/"; + +/// Did [`save`] write this file? True when its `x:xmpmeta` names PhotoCraft as the toolkit +/// (`x:xmptk`). Lightroom, Bridge and Camera Raw put their own toolkit there when they write a +/// file, so a sidecar another application touched since reads false. Never panics. +pub fn is_photocraft_xmp(bytes: &[u8]) -> bool { + if bytes.len() > MAX_XMP_BYTES { + return false; + } + let Ok(text) = std::str::from_utf8(bytes) else { return false }; + let Ok(doc) = roxmltree::Document::parse(text.trim_start_matches('\u{feff}')) else { return false }; + doc.descendants() + .find(|n| n.is_element() && n.tag_name().namespace() == Some(META_NS) && n.tag_name().name() == "xmpmeta") + .and_then(|n| n.attribute((META_NS, "xmptk"))) + == Some(TOOLKIT) +} + /// Write `params` as a Camera Raw settings file Photoshop's Load Settings… reads. pub fn save(params: &CameraRaw) -> String { let mut p = params.clone(); @@ -357,7 +385,18 @@ mod tests { assert_eq!(p.point_curve_red, vec![[0.0, 0.0], [128.0, 140.0], [255.0, 255.0]]); assert_eq!(p.dehaze, 12.0); // Lens vignetting, calibration and geometry have no counterpart. - assert_eq!(r.ignored, vec!["VignetteAmount".to_string(), "RedHue".to_string()]); + assert_eq!(r.ignored, vec!["VignetteAmount".to_string()], "the Calibration panel's keys load now"); + assert_eq!(p.calibration_hue[0], -5.0); + } + + /// A settings file nested 20 000 deep overflowed the XML parser's recursion (an abort nothing + /// can catch); it is an error now. The small stack makes a regression abort instead of passing. + #[test] + fn deeply_nested_settings_are_an_error_not_a_stack_overflow() { + let xml = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let e = + std::thread::Builder::new().stack_size(512 * 1024).spawn(move || load(&mut CameraRaw::default(), xml.as_bytes()).err()).unwrap().join().unwrap(); + assert!(e.as_deref().is_some_and(|m| m.contains("nested")), "{e:?}"); } #[test] @@ -420,4 +459,24 @@ mod tests { assert_eq!(p, before); assert_eq!(r.ignored, vec!["Contrast2012".to_string(), "ToneCurvePV2012".to_string()]); } + + #[test] + fn is_photocraft_xmp_reads_the_toolkit_marker() { + let own = save(&CameraRaw { contrast: 15.0, ..CameraRaw::default() }); + assert!(is_photocraft_xmp(own.as_bytes())); + let wrapped = format!("\u{feff}\n{own}"); + assert!(is_photocraft_xmp(wrapped.as_bytes()), "an xpacket wrapper and a byte order mark"); + assert!(!is_photocraft_xmp(FIXTURE), "Camera Raw's own toolkit"); + for other in [ + &b""[..], + b"", + b"", + b"", + b"", + b"\xff\xfe", + ] { + assert!(!is_photocraft_xmp(other), "{}", String::from_utf8_lossy(other)); + } + } } diff --git a/crates/io/src/channel_map.rs b/crates/io/src/channel_map.rs index bafde26f30..ef3b389bee 100644 --- a/crates/io/src/channel_map.rs +++ b/crates/io/src/channel_map.rs @@ -4,15 +4,18 @@ //! one 13-byte record per extra channel: colour space (u16), four u16 colour components, opacity //! (u16, 0..100) and kind (u8: 0 = colour indicates selected areas, 1 = masked ("protected") //! areas, 2 = spot channel). Resource 1007 has the same records without the version and the spot -//! kind, each followed by a padding byte (14 bytes). +//! kind, each followed by a padding byte (14 bytes). Photoshop 27's TIFFs use the same 13-byte +//! 1077 records. -use photocraft_color::{Color, ColorMode}; +use photocraft_color::{Color, ColorMode, SampleType}; use photocraft_doc::{AlphaChannel, ColorIndicates}; pub(crate) const DISPLAY_INFO: u16 = 1077; pub(crate) const DISPLAY_INFO_OLD: u16 = 1007; /// Quick Mask information: channel id (u16) and an "initially empty" flag (u8). pub(crate) const QUICK_MASK_INFO: u16 = 1022; +/// Alpha channel identifiers (Photoshop 6): one big-endian u32 per extra channel, no count prefix. +pub(crate) const ALPHA_IDENTIFIERS: u16 = 1053; const SPACE_RGB: u16 = 0; const SPACE_HSB: u16 = 1; @@ -20,6 +23,33 @@ const SPACE_CMYK: u16 = 2; const SPACE_LAB: u16 = 7; const SPACE_GRAY: u16 = 8; +/// Converts one spot channel's plane (native-endian samples) between the document's ink density +/// (1 = solid ink) and the way Photoshop stores a spot channel (0 = solid ink, the maximum = +/// bare paper; every PSD and TIFF Photoshop 27.11 saves stores it so). The conversion is its own +/// inverse, so it serves both open and save. Every reader and writer of Photoshop-polarity spot +/// planes goes through here: a change to the in-memory convention changes only this function. +/// Alpha channels are stored straight (the maximum = selected) and never come here. +pub(crate) fn photoshop_spot_plane(plane: &mut [u8], sample: SampleType) { + crate::pixels::invert_plane(plane, sample); +} + +/// Resource 1053 for `count` channels: one big-endian u32 identifier each, with no length prefix +/// (Photoshop, ag-psd and psd-tools read u32s until the resource ends). Photoshop 27.11 numbers a +/// new document's channels 3, 4, 5, … in every colour mode (RGB, CMYK, Gray and Lab, in its PSDs +/// and TIFFs alike), so these do too. `transparency`: the merged image's transparency channel +/// comes first, with identifier 0 (Photoshop's transparent PSD lists `0, 3, 4, 5`). +pub(crate) fn alpha_identifiers(transparency: bool, count: usize) -> Vec { + let mut out = Vec::with_capacity(count.saturating_add(1).saturating_mul(4)); + if transparency { + out.extend_from_slice(&0u32.to_be_bytes()); + } + for i in 0..count { + let id = 3u32.saturating_add(u32::try_from(i).unwrap_or(u32::MAX)); + out.extend_from_slice(&id.to_be_bytes()); + } + out +} + /// Resource 1077 for `channels` (in file order): a version, then 13 bytes per channel (colour /// space, four components, opacity, kind). Every corpus file, Photoshop's included, uses 13; /// 14-byte records (the older 1007 layout) shift every channel after the first. @@ -46,12 +76,33 @@ pub(crate) fn display_info(channels: &[&AlphaChannel], transparency: bool) -> Ve out } +/// The records of a DisplayInfo resource (`versioned` = 1077: 13-byte records after a version; +/// else the older 1007 with 14-byte records). A trailing partial record is left out. +fn display_records(data: &[u8], versioned: bool) -> std::slice::ChunksExact<'_, u8> { + let (body, size) = if versioned { (data.get(4..).unwrap_or(&[]), 13) } else { (data, 14) }; + body.chunks_exact(size) +} + +/// Which of the extra channels are spot channels (kind 2), after skipping `skip` records (the +/// merged transparency channel's), in the same records [`apply_display_info`] reads. A channel +/// without a record is not a spot. +pub(crate) fn spot_flags(data: &[u8], versioned: bool, skip: usize) -> Vec { + display_records(data, versioned).skip(skip).map(|rec| rec.get(12) == Some(&2)).collect() +} + +/// Whether a DisplayInfo resource 1077 marks any channel as a spot (kind 2), in 13-byte records +/// (Photoshop's, and PhotoCraft's since 0.8.2) or in the 14-byte padded records PhotoCraft wrote +/// up to 0.7.0. +pub(crate) fn display_info_marks_spot(data: &[u8]) -> bool { + let body = data.get(4..).unwrap_or_default(); + [13, 14].into_iter().any(|size| body.chunks_exact(size).any(|rec| rec.get(12) == Some(&2))) +} + /// Applies a DisplayInfo resource (`versioned` = 1077: 13-byte records after a version; else the /// older 1007 with 14-byte records) to the imported channels in order, after skipping `skip` /// records (the merged transparency channel's). pub(crate) fn apply_display_info(data: &[u8], versioned: bool, skip: usize, channels: &mut [AlphaChannel]) { - let (body, size) = if versioned { (data.get(4..).unwrap_or(&[]), 13) } else { (data, 14) }; - for (c, rec) in channels.iter_mut().zip(body.chunks_exact(size).skip(skip)) { + for (c, rec) in channels.iter_mut().zip(display_records(data, versioned).skip(skip)) { let u = |i: usize| u16::from_be_bytes([rec[i], rec[i + 1]]); let comps = [u(2), u(4), u(6), u(8)]; let color = decode_color(u(0), comps); @@ -111,7 +162,7 @@ pub(crate) fn decode_known_color(space: u16, c: [u16; 4]) -> Option { } /// HSB (hue in degrees, saturation and brightness 0..1) → RGB. -fn hsb_to_rgb(h: f32, s: f32, v: f32) -> [f32; 3] { +pub(crate) fn hsb_to_rgb(h: f32, s: f32, v: f32) -> [f32; 3] { let h = if h.is_finite() { h.rem_euclid(360.0) / 60.0 } else { 0.0 }; let (s, v) = (s.clamp(0.0, 1.0), v.clamp(0.0, 1.0)); let f = h - h.floor(); @@ -149,6 +200,15 @@ mod tests { assert!((back[0].opacity - 0.3).abs() < 1e-6); assert_eq!(back[0].indicates, ColorIndicates::SelectedAreas); assert_eq!(back[1].spot, s.spot); + assert_eq!(spot_flags(&bytes, true, 0), [false, true]); + assert!(display_info_marks_spot(&bytes)); + assert!(!display_info_marks_spot(&display_info(&[&a], true)), "alpha and transparency records only"); + assert!(!display_info_marks_spot(&bytes[..10])); + // Photoshop 27 writes a 13-byte record (no pad): red spot, solidity 60. + let ps = [0, 0, 0, 1, 0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0x3c, 2]; + let mut spot = vec![AlphaChannel::new("S", Surface::new(PixelFormat::GRAY8))]; + apply_display_info(&ps, true, 0, &mut spot); + assert_eq!(spot[0].spot.map(|(c, s)| (c.to_rgb(), (s * 100.0).round() as u16)), Some(([1.0, 0.0, 0.0], 60))); // With the merged transparency's record first, the channels still get their own. let with_t = display_info(&[&a, &s], true); assert_eq!(with_t.len(), 4 + 3 * 13); @@ -156,10 +216,79 @@ mod tests { apply_display_info(&with_t, true, 1, &mut back); assert_eq!(back[0].indicates, ColorIndicates::SelectedAreas); assert_eq!(back[1].spot, s.spot); + assert_eq!(spot_flags(&with_t, true, 1), [false, true]); // Truncated data is ignored rather than misread. let mut c = vec![AlphaChannel::new("x", Surface::new(PixelFormat::GRAY8))]; apply_display_info(&bytes[..10], true, 0, &mut c); assert_eq!(c[0].opacity, 0.5); + assert!(spot_flags(&bytes[..10], true, 0).is_empty()); + assert!(spot_flags(&bytes, true, 5).is_empty()); + + // 14 unpadded records are a multiple of 14 as well (14×13 = 13×14). Resource 1077 + // still reads them as 13-byte records; the last spot's solidity must survive. + let mut many = Vec::new(); + for i in 0..14 { + let mut ch = AlphaChannel::new(format!("{i}"), Surface::new(PixelFormat::GRAY8)); + ch.spot = Some((Color::rgb(1.0, 0.0, 0.0), (i as f32) / 14.0)); + many.push(ch); + } + let refs: Vec<&AlphaChannel> = many.iter().collect(); + let packed = display_info(&refs, false); + assert_eq!(packed.len(), 4 + 14 * 13); + let mut got: Vec = (0..14).map(|i| AlphaChannel::new(format!("{i}"), Surface::new(PixelFormat::GRAY8))).collect(); + apply_display_info(&packed, true, 0, &mut got); + assert_eq!(got[13].spot.map(|(_, s)| (s * 100.0).round() as u16), many[13].spot.map(|(_, s)| (s * 100.0).round() as u16)); + } + + /// Resource 1053 as Photoshop 27.11 numbers a new document's channels: 3, 4, 5 in every colour + /// mode, with the merged transparency first as 0 (its transparent PSD lists `0, 3, 4, 5`). + #[test] + fn alpha_identifiers_are_numbered_as_photoshop_does() { + let ids = |transparency, count| alpha_identifiers(transparency, count).as_chunks::<4>().0.iter().map(|c| u32::from_be_bytes(*c)).collect::>(); + assert_eq!(ids(false, 3), [3, 4, 5]); + assert_eq!(ids(true, 3), [0, 3, 4, 5]); + assert_eq!(ids(false, 1), [3]); + assert!(ids(false, 0).is_empty()); + assert_eq!(ids(true, 0), [0]); + assert_eq!(alpha_identifiers(false, 2), [0, 0, 0, 3, 0, 0, 0, 4]); + } + + /// PhotoCraft 0.1.0 to 0.7.0 padded each 1077 record to 14 bytes, so a spot after the first + /// record sits where a 13-byte reader finds no kind 2; it is still recognised. + #[test] + fn a_spot_is_found_in_padded_records() { + let alpha = |kind: u8| [0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 50, kind, 0]; + let mut old = 1u32.to_be_bytes().to_vec(); + old.extend_from_slice(&alpha(1)); + old.extend_from_slice(&alpha(0)); + old.extend_from_slice(&alpha(2)); + assert_eq!(spot_flags(&old, true, 0), [false, false, false], "13-byte records misread the padded ones"); + assert!(display_info_marks_spot(&old)); + old.truncate(old.len() - 14); + assert!(!display_info_marks_spot(&old), "two alpha records"); + } + + /// Photoshop's spot ink (0 = solid) and the document's density (1 = solid) at every depth, + /// both ways: the 25 % / 75 % columns of the Photoshop oracles store 191 / 64. + #[test] + fn photoshop_spot_plane_converts_both_ways() { + let mut p8 = vec![255u8, 64, 191, 0]; + photoshop_spot_plane(&mut p8, SampleType::U8); + assert_eq!(p8, [0, 191, 64, 255]); + photoshop_spot_plane(&mut p8, SampleType::U8); + assert_eq!(p8, [255, 64, 191, 0]); + let mut p16: Vec = [65535u16, 16448, 0].iter().flat_map(|v| v.to_ne_bytes()).collect(); + photoshop_spot_plane(&mut p16, SampleType::U16); + let back: Vec = p16.as_chunks::<2>().0.iter().map(|c| u16::from_ne_bytes(*c)).collect(); + assert_eq!(back, [0, 49087, 65535]); + let mut p32: Vec = [1.0f32, 0.25].iter().flat_map(|v| v.to_ne_bytes()).collect(); + photoshop_spot_plane(&mut p32, SampleType::F32); + let back: Vec = p32.as_chunks::<4>().0.iter().map(|c| f32::from_ne_bytes(*c)).collect(); + assert_eq!(back, [0.0, 0.75]); + // A trailing partial sample is left alone rather than misread. + let mut odd = vec![0u8, 0, 7]; + photoshop_spot_plane(&mut odd, SampleType::U16); + assert_eq!(odd, [0xff, 0xff, 7]); } #[test] diff --git a/crates/io/src/comps_map.rs b/crates/io/src/comps_map.rs index 8d0c78f3b0..3348061b34 100644 --- a/crates/io/src/comps_map.rs +++ b/crates/io/src/comps_map.rs @@ -12,6 +12,8 @@ //! Both are re-emitted verbatim while they still decode to the document's state, and are //! regenerated otherwise (comp appearance is not written to PSD; `.pcraft` keeps it). +use std::collections::HashMap; + use photocraft_doc::comps::layer_position; use photocraft_doc::{Artboard, ArtboardBackground, CompLayerState, Document, Guides, Layer, LayerComp}; use photocraft_geom::Rect; @@ -203,6 +205,14 @@ pub(crate) fn decode_comps(resource: Option<&[u8]>, doc: &Document) -> (Vec = HashMap::with_capacity(comps.len()); + for (i, c) in comps.iter().enumerate() { + id_to_index.entry(c.id).or_insert(i); + } let mut last = LayerComp { id: 0, name: "Last Document State".into(), @@ -215,33 +225,37 @@ pub(crate) fn decode_comps(resource: Option<&[u8]>, doc: &Document) -> (Vec { - let p = offset_position(h, o); - if p.is_none() { - warnings.push(format!( - "layer \"{}\": layer comp position offset ({}, {}) is out of range; that position was not imported", - l.name, o.0, o.1 - )); - } - p + let position = match (here, s.offset) { + (Some(h), Some(o)) => { + let p = offset_position(h, o); + if p.is_none() { + warnings.push(format!( + "layer \"{}\": layer comp position offset ({}, {}) is out of range; that position was not imported", + l.name, o.0, o.1 + )); } - _ => None, - }, - appearance: None, + p + } + _ => None, }; for id in &s.comps { - let target = if *id == 0 { Some(&mut last) } else { comps.iter_mut().find(|c| c.id == *id) }; - if let Some(c) = target { - c.states.push(state.clone()); + let target = if *id == 0 { Some(&mut last) } else { id_to_index.get(id).map(|&i| &mut comps[i]) }; + let Some(c) = target else { continue }; + let visible = if *id != 0 && c.apply_visibility { Some(resolved) } else { s.visible }; + if visible.is_none() && s.offset.is_none() { + continue; } + c.states.push(CompLayerState { layer: l.id, visible, position, appearance: None }); } } } @@ -406,6 +420,72 @@ mod tests { assert!(only.is_empty()); } + /// #8093: Photoshop writes `enab` only where a layer's visibility changes between comps. An + /// entry without it has the last `enab` before it, or visible when none came before: a layer + /// added after comp 1 and shown in it on Recapture is visible there, and comp 3, saved in the + /// same state as comp 2, hides the layer comp 2 hides. + #[test] + fn a_comp_entry_without_enab_inherits_the_previous_visibility() { + let mut d = Document::with_background("t", Size::new(32, 32), ColorMode::Rgb, SampleType::U8, Color::WHITE); + let comp = |id: u32, name: &str| LayerComp { + id, + name: name.into(), + comment: String::new(), + apply_visibility: true, + apply_position: true, + apply_appearance: false, + states: Vec::new(), + }; + // c4 records positions only: it never takes a visibility. + let mut c4 = comp(4, "c4"); + c4.apply_visibility = false; + d.layer_comps = vec![comp(1, "c1"), comp(2, "c2"), comp(3, "c3"), c4]; + let entry = |ids: &[u32], enab: Option| { + let mut e = obj("null"); + if let Some(v) = enab { + e = e.with("enab", Value::Boolean(v)); + } + Value::Descriptor(e.with("compList", Value::List(ids.iter().map(|i| Value::Integer(*i as i32)).collect()))) + }; + let attach = |l: &mut Layer, settings: Vec| { + let cmls = VersionedDescriptor::new(Descriptor::new("null").with("layerSettings", Value::List(settings))).to_bytes(); + let mut raw = vec![(*b"shmd", write_shmd(&[]))]; + set_cmls(&mut raw, Some(cmls)); + l.psd_blocks = raw.iter().map(|(k, v)| (*k, std::sync::Arc::new(v.clone()))).collect(); + }; + // Layer A: [c1 {}], [c2 {enab false}], [c3 {}], [0 {}] (repro_comp_inherit.psd). + let mut a = Layer::raster("A", PixelFormat::RGBA8); + attach(&mut a, vec![entry(&[1], None), entry(&[2], Some(false)), entry(&[3], None), entry(&[0], None)]); + // Layer C: [c1 {}], [0 {enab false}] (repro_comp_new_layer.psd). + let mut c = Layer::raster("C", PixelFormat::RGBA8); + attach(&mut c, vec![entry(&[1], None), entry(&[0], Some(false))]); + // Layer E: [c1 {enab false}], [c4 {Ofst}], [c3 + 0 {}]: c4 keeps no visibility, c3 inherits + // hidden, and the Last Document State sharing c3's entry records nothing. + let mut e = Layer::raster("E", PixelFormat::RGBA8); + e.surface_mut().unwrap().fill_rect(Rect::new(4, 4, 8, 8), &[1.0, 0.0, 0.0, 1.0]); + let moved = obj("null") + .with("compList", Value::List(vec![Value::Integer(4)])) + .with("Ofst", Value::Descriptor(obj("null").with("Hrzn", Value::Integer(2)).with("Vrtc", Value::Integer(3)))); + attach(&mut e, vec![entry(&[1], Some(false)), Value::Descriptor(moved), entry(&[3, 0], None)]); + let (a_id, c_id, e_id) = (a.id, c.id, e.id); + d.layers.push(a); + d.layers.push(c); + d.layers.push(e); + let res = write_comps_resource(&d).unwrap(); + let (comps, _, last) = comps_from_psd(Some(&res), &d); + let visible = |id: u32, layer| comps.iter().find(|c| c.id == id).and_then(|c| c.state(layer)).and_then(|s| s.visible); + assert_eq!([visible(1, a_id), visible(2, a_id), visible(3, a_id)], [Some(true), Some(false), Some(false)]); + assert_eq!(visible(1, c_id), Some(true), "no enab before it: visible"); + // The Last Document State entry keeps exactly what it records. + let last = last.unwrap(); + assert_eq!(last.state(c_id).and_then(|s| s.visible), Some(false)); + assert_eq!(last.state(a_id), None); + let c4 = comps.iter().find(|c| c.id == 4).and_then(|c| c.state(e_id)).unwrap(); + assert_eq!((c4.visible, c4.position), (None, Some((6, 7))), "a comp without visibility takes none"); + assert_eq!(visible(3, e_id), Some(false), "inherited from c1, past c4"); + assert_eq!(last.state(e_id), None, "the shared entry gives the Last Document State nothing"); + } + /// A layer at origin (4, 4) whose `shmd` carries a `cmls` item with one `layerSettings` /// entry for comp 1: visible, offset by `ofst`. fn doc_with_cmls_offset(ofst: (Value, Value)) -> (Document, photocraft_doc::LayerId, Vec) { diff --git a/crates/io/src/displace_map.rs b/crates/io/src/displace_map.rs index 8993663cc0..792c8e72df 100644 --- a/crates/io/src/displace_map.rs +++ b/crates/io/src/displace_map.rs @@ -10,10 +10,14 @@ //! width * height bytes (horizontal map, vertical map), then padding //! ``` //! +//! Photoshop does not always embed the map: a Displace can carry only `DspF`, a `Pth ` to the map +//! file (ag-psd models the filter with no `DspD` at all), and Photoshop reads that file again. Such +//! a filter imports with the path as its `mapPath`, read on re-render like any stored path (#9162). +//! //! The whole `filterFX` item (with the map) is kept verbatim in the params as a template //! ([`TEMPLATE_KEY`]), so the filter re-renders, survives `.pcraft` and is written back to PSD -//! with only the editable settings replaced. Anything else (another mode, a missing or truncated -//! map, unknown enum values) stays an opaque [`super::UNSUPPORTED_FILTER`]. +//! with only the editable settings replaced. Anything else (another mode, a truncated map, no map +//! and no readable path, unknown enum values) stays an opaque [`super::UNSUPPORTED_FILTER`]. use std::sync::Arc; @@ -38,6 +42,10 @@ const HEADER: usize = 10; const MODE_TWO_PLANES: u16 = 3; /// 8K × 8K: far beyond the observed document-sized maps, and bounds the f32 map we build. const MAX_MAP_PIXELS: usize = 1 << 26; +/// 4K × 4K: the largest map PhotoCraft embeds itself. Params hold it as hex, about 4 bytes per +/// pixel, copied with every history state and written into `.pcraft`'s manifest, so a few of the +/// largest readable maps would make a file the reader refuses. +const MAX_EMBED_PIXELS: usize = 1 << 24; /// The template is the item plus the map; a little room for the rest of the descriptor. const MAX_TEMPLATE_BYTES: usize = 2 * MAX_MAP_PIXELS + (1 << 20); @@ -76,6 +84,35 @@ fn fltr_map(item: &Descriptor) -> Option> { } } +/// The map file `DspF` names, when it is a `Pth `: `txtu`, u32 LE total size (`12 + 2 * units`), +/// u32 LE count of UTF-16 units, then the UTF-16LE path ending in one NUL (ag-psd's layout). A +/// record whose size disagrees with its units, or that lacks the NUL, is not trusted as a path. A +/// Mac alias (`alis`) is not decoded. +fn fltr_path(item: &Descriptor) -> Option { + let Value::Path(b) = get_desc(item, "Fltr")?.get("DspF")? else { return None }; + let u32_at = |i: usize| usize::try_from(u32::from_le_bytes(b.get(i..i.checked_add(4)?)?.try_into().ok()?)).ok(); + if b.get(..4)? != b"txtu" { + return None; + } + let (size, units) = (u32_at(4)?, u32_at(8)?); + if size != 12usize.checked_add(units.checked_mul(2)?)? || b.len() != size { + return None; + } + let text: Vec = b.get(12..)?.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect(); + let (&0, path) = text.split_last()? else { return None }; + let path = String::from_utf16(path).ok()?; + (!path.is_empty() && !path.contains('\0')).then_some(path) +} + +/// The `DspF` path a Displace reads its map from, only when no `DspD` is embedded at all: a +/// damaged embedded map keeps the filter opaque rather than falling back to the path. +fn path_only(item: &Descriptor) -> Option { + if get_desc(item, "Fltr")?.get(MAP_KEY).is_some() { + return None; + } + fltr_path(item) +} + fn lookup<'a>(table: &[(&[u8], &'a str)], v: &[u8]) -> Option<&'a str> { table.iter().find(|(k, _)| *k == v).map(|(_, s)| *s) } @@ -91,14 +128,17 @@ fn settings(f: &Descriptor) -> Option { })) } -/// `filter.distort.displace` params for a Displace item with an embedded map. +/// `filter.distort.displace` params for a Displace item with an embedded map, or else a map file +/// path (then `mapPath`). pub(super) fn import_params(item: &Descriptor) -> Option { let f = get_desc(item, "Fltr")?; if !f.class_id.is(CLASS) { return None; } let mut params = settings(f)?; - fltr_map(item)?; + if fltr_map(item).is_none() { + params.as_object_mut()?.insert("mapPath".into(), json!(path_only(item)?)); + } // Neutral blending in the template: the model's own fields replace it on export. let mut template = item.clone(); let neutral = SmartFilter { command: String::new(), params: J::Null, blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; @@ -108,7 +148,7 @@ pub(super) fn import_params(item: &Descriptor) -> Option { Some(params) } -/// The template item of `params`, checked to be a Displace filter with a readable map. +/// The template item of `params`, checked to be a Displace filter with a readable map or map path. fn template(params: &J) -> Result { let raw = params.get(TEMPLATE_KEY).ok_or("a Displace map from a file or document is not written to PSD")?; let raw = raw.as_str().ok_or("the Displace PSD data must be hex text")?; @@ -117,8 +157,8 @@ fn template(params: &J) -> Result { } let bytes = from_hex(raw).ok_or("the Displace PSD data is not valid hex")?; let item = Descriptor::from_bytes(&bytes).map_err(|e| format!("the Displace PSD data is unreadable ({e})"))?; - if !item.class_id.is("filterFX") || int(&item, "filterID") != Some(FILTER_ID) || fltr_map(&item).is_none() { - return Err("the Displace PSD data is not a Displace filter with an embedded map".into()); + if !item.class_id.is("filterFX") || int(&item, "filterID") != Some(FILTER_ID) || (fltr_map(&item).is_none() && path_only(&item).is_none()) { + return Err("the Displace PSD data is not a Displace filter with an embedded map or map file".into()); } Ok(item) } @@ -126,10 +166,18 @@ fn template(params: &J) -> Result { /// The `filterFXList` item: the template with the current scales, fit and undefined areas. pub(super) fn export_item(params: &J) -> Result { let object = params.as_object().ok_or("Displace parameters must be an object")?; - if let Some(k) = object.keys().find(|k| !super::is_meta(k) && !["horizontal", "vertical", "fit", "undefinedAreas"].contains(&k.as_str())) { + let mut item = template(params)?; + // The `mapPath` imported from the template's own `DspF` is already in it; another one isn't. + // A path-only template also needs that `mapPath`: without it the filter renders no map while + // the PSD would still name one. + let template_path = path_only(&item); + if template_path.is_some() && params.get("mapPath").and_then(J::as_str) != template_path.as_deref() { + return Err("a Displace that reads its map from a file needs that file as `mapPath` to be written to PSD".into()); + } + let own_path = |k: &str| k == "mapPath" && template_path.is_some(); + if let Some(k) = object.keys().find(|k| !super::is_meta(k) && !own_path(k) && !["horizontal", "vertical", "fit", "undefinedAreas"].contains(&k.as_str())) { return Err(format!("Displace setting `{k}` has no PSD equivalent next to its embedded map")); } - let mut item = template(params)?; let mut f = get_desc(&item, "Fltr").ok_or("the Displace PSD data has no filter")?.clone(); let scale = |k: &str, d: f64| -> Result { let v = params.get(k).map_or(Some(d), J::as_f64).filter(|v| v.is_finite()).ok_or(format!("Displace `{k}` must be a number"))?; @@ -156,6 +204,69 @@ pub(super) fn export_item(params: &J) -> Result { Ok(item) } +/// `filter.distort.displace` params with `map` embedded the way Photoshop embeds a Displace +/// smart filter's map: a `filterFX` item ([`TEMPLATE_KEY`]) holding it as two 8-bit planes. A +/// smart filter re-renders from its params alone, so a map read from a file or an open document +/// must travel with them (#7423); embedded, it also saves to `.pcraft` and PSD like an imported +/// one. `map`'s first channel is the horizontal map and its second the vertical one (the first +/// again for one or two channels), as the filter reads them. `mapPath`, `mapDocument` and +/// `mapLayer` are dropped: `map` was already read from them (from that layer, when one was +/// named), and the embedded map replaces them. `None` for params that are not an +/// object, or an empty, short or oversized map. +pub fn embed_map(params: &J, map: &Image) -> Option { + let (w, h) = (map.rect.width(), map.rect.height()); + let n = usize::try_from(w).ok()?.checked_mul(usize::try_from(h).ok()?)?; + if n == 0 || n > MAX_EMBED_PIXELS || map.ch == 0 || map.data.len() < n.checked_mul(map.ch)? { + return None; + } + let vertical = if map.ch >= 3 { 1 } else { 0 }; + let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + let pixels = || map.data.chunks_exact(map.ch).take(n); + let mut planes = Vec::with_capacity(HEADER + 2 * n); + planes.extend(h.to_le_bytes()); + planes.extend(w.to_le_bytes()); + planes.extend(MODE_TWO_PLANES.to_le_bytes()); + planes.extend(pixels().map(|px| px.first().map_or(0, |v| byte(*v)))); + planes.extend(pixels().map(|px| px.get(vertical).map_or(0, |v| byte(*v)))); + let enumerated = |t: &str, v: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(v) }; + // The settings here are placeholders: the params' own replace them when the filter renders + // and when it is written to PSD (`export_item`). + let neutral = SmartFilter { command: String::new(), params: J::Null, blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(photocraft_psd::descriptor::UnicodeString::new_nul("Displace..."))) + .with("blendOptions", blend_options(&neutral)) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with( + "Fltr", + Value::Descriptor( + Descriptor::new(CLASS) + .with("HrzS", Value::Integer(10)) + .with("VrtS", Value::Integer(10)) + .with("DspM", enumerated("DspM", "StrF")) + .with("UndA", enumerated("UndA", "RptE")) + .with("EmbF", Value::Boolean(true)) + .with(MAP_KEY, Value::RawData(planes)), + ), + ) + .with("filterID", Value::Integer(FILTER_ID)); + let mut out = params.as_object()?.clone(); + for key in ["mapPath", "mapDocument", "mapLayer"] { + out.remove(key); + } + out.insert(TEMPLATE_KEY.into(), json!(to_hex(&item.to_bytes()))); + Some(J::Object(out)) +} + +/// `params` without the embedded map, for a Displace that names another one to read instead. +pub fn without_map(params: &J) -> J { + let mut out = params.clone(); + if let J::Object(m) = &mut out { + m.remove(TEMPLATE_KEY); + } + out +} + /// The displacement map embedded in Displace params imported from PSD (`None` when there is /// none or it is unreadable). The two planes become the first two channels; identical planes /// (every file seen so far) become one channel, which Displace reads for both directions. @@ -318,6 +429,69 @@ mod tests { } } + /// A `Pth ` value as ag-psd reads it: `txtu`, sizes (LE), UTF-16LE path with a trailing NUL. + fn pth(path: &str) -> Value { + let units: Vec = path.encode_utf16().chain([0]).collect(); + let mut b = b"txtu".to_vec(); + b.extend((12 + 2 * units.len() as u32).to_le_bytes()); + b.extend((units.len() as u32).to_le_bytes()); + b.extend(units.iter().flat_map(|u| u.to_le_bytes())); + Value::Path(b) + } + + /// #9162: Photoshop can save a Displace with only `DspF`, the map file's path, and no + /// embedded map. It imports as Displace reading that file, and writes back unchanged. + #[test] + fn a_map_named_only_by_its_path_imports_as_map_path() { + let with_file = |file: Value| { + let mut i = item(Vec::new()); + let mut f = get_desc(&i, "Fltr").unwrap().clone(); + f.items.retain(|(k, _)| !k.is(MAP_KEY)); + set(&mut f, "DspF", file); + set(&mut i, "Fltr", Value::Descriptor(f)); + i + }; + let src = with_file(pth("C:\\maps\\repro-map.psd")); + let f = filter_from_item(&src); + assert_eq!(f.command, COMMAND); + assert_eq!(f.params["mapPath"], json!("C:\\maps\\repro-map.psd")); + assert!(embedded_map(&f.params).is_none()); + assert_eq!(item_for_filter(&f).unwrap(), src, "written back as it was"); + let mut moved = f.clone(); + moved.params["mapPath"] = json!("/elsewhere/map.png"); + assert!(item_for_filter(&moved).is_err(), "another map path has no place in the template"); + let mut no_path = f.clone(); + no_path.params.as_object_mut().unwrap().remove("mapPath"); + assert!(item_for_filter(&no_path).is_err(), "a path-only template needs its mapPath, or the PSD names a map the render never read"); + // A record whose size field disagrees with its units, or without the final NUL. + let mut wrong_size = pth("map.psd"); + if let Value::Path(b) = &mut wrong_size { + b[4] = b[4].wrapping_add(2); + } + let mut no_nul = b"txtu".to_vec(); + no_nul.extend(16u32.to_le_bytes()); + no_nul.extend(2u32.to_le_bytes()); + no_nul.extend([b'a', 0, b'b', 0]); + // Not a path we can read: kept opaque, as before. + for bad in [ + Value::Path(b"txtu".to_vec()), + Value::Path(b"abcd\0\0\0\0\x02\0\0\0a\0\0\0".to_vec()), + Value::Path(b"txtu\0\0\0\0\x09\0\0\0a\0".to_vec()), + pth(""), + wrong_size, + Value::Path(no_nul), + ] { + let src = with_file(bad); + assert_eq!(filter_from_item(&src).command, UNSUPPORTED_FILTER); + } + // A damaged embedded map is never replaced by a readable path. + let mut damaged = item(vec![0; 4]); + let mut fltr = get_desc(&damaged, "Fltr").unwrap().clone(); + set(&mut fltr, "DspF", pth("C:\\maps\\repro-map.psd")); + set(&mut damaged, "Fltr", Value::Descriptor(fltr)); + assert_eq!(filter_from_item(&damaged).command, UNSUPPORTED_FILTER); + } + #[test] fn bad_exports_report_errors() { let ok = filter_from_item(&item(tdta(2, 2, |_| 0, |_| 0, 0))).params; @@ -340,4 +514,38 @@ mod tests { } assert!(embedded_map(&json!({TEMPLATE_KEY: "0".repeat(MAX_TEMPLATE_BYTES * 2 + 2)})).is_none()); } + + /// #7423: a map from a file or an open document, embedded for a smart filter, is read back as + /// the filter's map and written to PSD like an imported one. + #[test] + fn an_embedded_map_reads_back_and_exports() { + let rect = Rect::new(0, 0, 3, 2); + // RGBA: red is the horizontal map, green the vertical one. + let rgba = Image { rect, ch: 4, data: (0..6).flat_map(|i| [i as f32 / 5.0, 1.0 - i as f32 / 5.0, 0.0, 1.0]).collect() }; + let params = json!({"horizontal": 20, "vertical": -5, "fit": "tile", "undefinedAreas": "wrap", "mapDocument": 0, "mapLayer": 3}); + let embedded = embed_map(¶ms, &rgba).unwrap(); + for key in ["mapPath", "mapDocument", "mapLayer"] { + assert!(embedded.get(key).is_none(), "{key}"); + } + assert_eq!(embedded["horizontal"], json!(20)); + let map = embedded_map(&embedded).unwrap(); + assert_eq!((map.rect, map.ch), (rect, 3)); + let h: Vec = map.data.chunks(3).map(|p| p[0]).collect(); + let v: Vec = map.data.chunks(3).map(|p| p[1]).collect(); + assert_eq!(h, [0.0, 51.0 / 255.0, 102.0 / 255.0, 153.0 / 255.0, 204.0 / 255.0, 1.0]); + assert_eq!(v, [1.0, 204.0 / 255.0, 153.0 / 255.0, 102.0 / 255.0, 51.0 / 255.0, 0.0]); + // Written to PSD with the params' own settings. + let f = export_item(&embedded).unwrap(); + let fltr = get_desc(&f, "Fltr").unwrap(); + assert_eq!(num(fltr.get("HrzS")), Some(20.0)); + assert_eq!(enum_of(fltr, "DspM"), Some(&b"Tile"[..])); + // A grey map displaces the same both ways. + let grey = Image { rect, ch: 1, data: vec![0.25; 6] }; + let map = embedded_map(&embed_map(&json!({}), &grey).unwrap()).unwrap(); + assert_eq!(map.ch, 1); + // Nothing to embed. + assert!(embed_map(&json!({}), &Image { rect: Rect::new(0, 0, 0, 0), ch: 4, data: vec![] }).is_none()); + assert!(embed_map(&json!({}), &Image { rect, ch: 4, data: vec![0.0; 5] }).is_none(), "short data"); + assert!(embed_map(&json!(3), &grey).is_none(), "params not an object"); + } } diff --git a/crates/io/src/doc_path.rs b/crates/io/src/doc_path.rs new file mode 100644 index 0000000000..8c3c36c93d --- /dev/null +++ b/crates/io/src/doc_path.rs @@ -0,0 +1,83 @@ +//! Files named by paths stored in a document: linked smart objects, the Displace map and Color +//! Lookup file of a smart filter, data-set images, video footage. +//! +//! Such a path is followed only through [`resolve`]: a relative path is taken from the document's +//! folder, and only local files, or files on the network share the document itself is on, are +//! opened (see `craft_docpath`). A refused path is treated like a missing file. + +use std::fmt; +use std::path::{Path, PathBuf}; + +pub use craft_docpath::Refused; + +/// Why a stored path is not followed. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum NotFollowed { + /// The path is not a local file, or is not on the document's own share. + Refused(Refused), + /// The path is relative and the document has no file to take it from. + NoDocumentFolder, +} + +impl fmt::Display for NotFollowed { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Refused(r) => r.fmt(f), + Self::NoDocumentFolder => f.write_str("the path is relative and the document has not been saved"), + } + } +} + +impl std::error::Error for NotFollowed {} + +/// The file to open for `stored`, a path or `file:` URL stored in a document, or why it is not +/// followed. `document` is the path of the document it is stored in, if the document has a file; +/// a relative `document` is taken from the working directory, where it was opened from. +pub fn resolve(stored: &str, document: Option<&Path>) -> Result { + let document = document.map(|d| if d.is_relative() { std::path::absolute(d).unwrap_or_else(|_| d.to_path_buf()) } else { d.to_path_buf() }); + craft_docpath::resolve_absolute(stored, document.as_deref()).map_err(|r| match r { + // Without a document, a relative path resolves but stays relative. + Refused::Invalid if document.is_none() && craft_docpath::resolve(stored, None).is_ok_and(|p| cfg!(windows) || !is_windows_form(&p)) => { + NotFollowed::NoDocumentFolder + } + r => NotFollowed::Refused(r), + }) +} + +/// Whether `path`, as `craft_docpath` writes it, has a Windows drive or share root (`C:x`, `C:\x`, +/// `\\server\share\x`). Outside Windows no document folder makes such a path absolute. +fn is_windows_form(path: &Path) -> bool { + let s = path.as_os_str().as_encoded_bytes(); + s.starts_with(b"\\") || matches!(s, [l, b':', ..] if l.is_ascii_alphabetic()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn relative_paths_are_taken_from_the_document_folder() { + let doc = std::env::temp_dir().join("art").join("poster.pcraft"); + assert_eq!(resolve("logo.png", Some(&doc)), Ok(std::env::temp_dir().join("art").join("logo.png"))); + assert_eq!(resolve("logo.png", None), Err(NotFollowed::NoDocumentFolder)); + assert_eq!(resolve(r"\\192.0.2.1\share\x.png", Some(&doc)), Err(NotFollowed::Refused(Refused::Network))); + assert_eq!(resolve("//192.0.2.1/share/x.png", None), Err(NotFollowed::Refused(Refused::Network))); + assert_eq!(resolve("jar:logo.png", None), Err(NotFollowed::Refused(Refused::Url))); + // Colons in names: an alternate data stream or a URL on Windows, a plain character elsewhere. + if cfg!(windows) { + assert_eq!(resolve("img/fig:1.png", Some(&doc)), Err(NotFollowed::Refused(Refused::Invalid))); + assert_eq!(resolve("chapter1:intro.png", None), Err(NotFollowed::Refused(Refused::Url))); + } else { + assert_eq!(resolve("img/fig:1.png", Some(&doc)), Ok(std::env::temp_dir().join("art").join("img").join("fig:1.png"))); + assert_eq!(resolve("chapter1:intro.png", None), Err(NotFollowed::NoDocumentFolder)); + } + // Outside Windows a Windows path names no file: it is refused, not reported as relative. + if !cfg!(windows) { + for stored in [r"C:\art\logo.png", "C:logo.png"] { + assert_eq!(resolve(stored, Some(&doc)), Err(NotFollowed::Refused(Refused::Invalid)), "{stored}"); + assert_eq!(resolve(stored, None), Err(NotFollowed::Refused(Refused::Invalid)), "{stored}"); + } + assert_eq!(resolve("../logo.png", None), Err(NotFollowed::NoDocumentFolder)); + } + } +} diff --git a/crates/io/src/duotone_map.rs b/crates/io/src/duotone_map.rs new file mode 100644 index 0000000000..693264adec --- /dev/null +++ b/crates/io/src/duotone_map.rs @@ -0,0 +1,293 @@ +//! Duotone inks ⇄ a PSD's Color Mode Data in Duotone mode (colour mode 8). +//! +//! The PSD specification leaves this block undocumented. Photoshop writes 524 bytes (observed on +//! files it made with 1–4 inks): +//! +//! | offset | size | content | +//! |---|---|---| +//! | 0 | 2 | version, 1 | +//! | 2 | 2 | number of inks, 1–4 | +//! | 4 | 4 × 10 | ink colours: colour space + 4 × u16, as in swatch files; space 0xFFFF = no ink | +//! | 44 | 4 × 64 | ink names, Pascal strings | +//! | 300 | 4 × 28 | ink curves: 13 × i16 then an i16 (0), the PSD's transfer-function layout | +//! | 412 | 2 | dot gain, 20 | +//! | 414 | 11 × 10 | overprint colours, space 0xFFFF = computed | +//! +//! A curve's 13 values are the ink's density in tenths of a percent at gray densities of 0, 5, +//! 10, 20, …, 90, 95 and 100 %; −1 leaves a point out (Photoshop's empty dialog fields), except +//! at 0 and 100 %: Photoshop refuses to open a file with −1 there. +//! +//! A colour-book ink (PANTONE, …) is stored by its book key, not a colour. Photoshop also writes +//! every ink's Lab equivalent in image resource 1066 (Alternate Duotone Colors): version 1, a +//! count, then one 10-byte colour per ink in the same order. + +use photocraft_doc::adjust::CurvePoint; +use photocraft_doc::{Duotone, DuotoneInk}; +use photocraft_psd::aco::AcoColor; + +/// Length of the block Photoshop writes. +pub(crate) const BLOCK_LEN: usize = 524; +/// Gray densities (%) of a curve's 13 points. +const CURVE_AT: [u16; 13] = [0, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 100]; +const NO_COLOR: [u8; 10] = [0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0]; +const NAMES_AT: usize = 44; +const CURVES_AT: usize = 300; +const DOT_GAIN_AT: usize = 412; +/// Image resource: the inks' Lab equivalents. +pub(crate) const ALTERNATE_DUOTONE_COLORS: u16 = 1066; + +fn u16_at(b: &[u8], at: usize) -> Option { + b.get(at..at + 2).map(|s| u16::from_be_bytes([s[0], s[1]])) +} + +/// The sRGB display colour of the 10-byte colour structure at `at`; `None` for a colour book or +/// another space we can't show. +fn display(b: &[u8], at: usize) -> Option<[f32; 3]> { + let space = u16_at(b, at)?; + let values = [u16_at(b, at + 2)?, u16_at(b, at + 4)?, u16_at(b, at + 6)?, u16_at(b, at + 8)?]; + let c = crate::channel_map::decode_known_color(space, values)?; + Some(c.to_rgb().map(|v| v.clamp(0.0, 1.0))) +} + +/// The inks of a Duotone block, or `None` when it is not one this reads (the caller keeps it raw). +/// `alternate` is resource 1066, which gives a colour-book ink its colour. `unknown` is set when +/// an ink's colour can't be shown even so; it is then black here, and the caller shows the +/// grey plate instead. +pub(crate) fn parse(b: &[u8], alternate: Option<&[u8]>, unknown: &mut bool) -> Option> { + if b.len() < DOT_GAIN_AT || u16_at(b, 0)? != 1 { + return None; + } + let n = usize::from(u16_at(b, 2)?); + if !(1..=4).contains(&n) { + return None; + } + (0..n) + .map(|i| { + let alt = alternate.filter(|a| u16_at(a, 0) == Some(1) && u16_at(a, 2).is_some_and(|k| usize::from(k) > i)); + let color = display(b, 4 + 10 * i).or_else(|| display(alt?, 4 + 10 * i)).unwrap_or_else(|| { + *unknown = true; + [0.0; 3] + }); + let name_at = NAMES_AT + 64 * i; + let len = usize::from(*b.get(name_at)?).min(63); + let name = String::from_utf8_lossy(b.get(name_at + 1..name_at + 1 + len)?).into_owned(); + let curve_at = CURVES_AT + 28 * i; + let mut curve: Vec = Vec::new(); + for (k, x) in CURVE_AT.iter().enumerate() { + let v = u16_at(b, curve_at + 2 * k)? as i16; + if v >= 0 { + curve.push(CurvePoint { input: f32::from(*x) / 100.0, output: (f32::from(v) / 1000.0).clamp(0.0, 1.0) }); + } + } + if curve.len() < 2 { + curve = DuotoneInk::new("", [0.0; 3]).curve; + } + Some(DuotoneInk { name, color, curve }) + }) + .collect() +} + +/// The Duotone block for `d`'s first four inks: RGB ink colours, their names and their curves +/// sampled at the 13 gray densities (the points on that grid as they are, the others left out), +/// Photoshop's default dot gain and computed overprint colours. +pub(crate) fn write(d: &Duotone) -> Vec { + let mut b = Vec::with_capacity(BLOCK_LEN); + let inks: Vec<&DuotoneInk> = d.inks.iter().take(4).collect(); + b.extend_from_slice(&1u16.to_be_bytes()); + b.extend_from_slice(&(inks.len().max(1) as u16).to_be_bytes()); + for i in 0..4 { + match inks.get(i) { + Some(ink) => { + let q = |v: f32| (v.clamp(0.0, 1.0) * 65535.0).round() as u16; + let (space, v) = AcoColor::Rgb(ink.color.map(q)).to_record(); + b.extend_from_slice(&space.to_be_bytes()); + v.iter().for_each(|x| b.extend_from_slice(&x.to_be_bytes())); + } + None => b.extend_from_slice(&NO_COLOR), + } + } + for i in 0..4 { + let mut name = [0u8; 64]; + if let Some(ink) = inks.get(i) { + let bytes: Vec = ink.name.chars().map(|c| if c.is_ascii() { c as u8 } else { b'?' }).take(63).collect(); + name[0] = bytes.len() as u8; + name[1..=bytes.len()].copy_from_slice(&bytes); + } + b.extend_from_slice(&name); + } + for i in 0..4 { + let values = curve_values(inks.get(i).map(|ink| ink.curve.as_slice())); + values.iter().for_each(|v| b.extend_from_slice(&v.to_be_bytes())); + b.extend_from_slice(&0i16.to_be_bytes()); + } + b.extend_from_slice(&20u16.to_be_bytes()); + for _ in 0..11 { + b.extend_from_slice(&NO_COLOR); + } + b +} + +/// A curve's 13 values: an identity curve (or none) is 0 and 100 % only, as Photoshop writes it; +/// a curve whose points all lie on the 13 gray densities keeps exactly those points; any other +/// curve is sampled at all 13. +fn curve_values(curve: Option<&[CurvePoint]>) -> [i16; 13] { + let mut out = [-1i16; 13]; + let pts = curve.filter(|c| c.len() >= 2); + let identity = pts.is_none_or(|c| c.iter().all(|p| (p.input - p.output).abs() < 1e-4)); + let per_mille = |v: f32| (v.clamp(0.0, 1.0) * 1000.0).round() as i16; + if identity { + out[0] = 0; + out[12] = 1000; + return out; + } + let pts = pts.unwrap_or_default(); + let slot = |x: f32| CURVE_AT.iter().position(|a| (f32::from(*a) / 100.0 - x).abs() < 1e-4); + let ink = DuotoneInk { name: String::new(), color: [0.0; 3], curve: pts.to_vec() }; + if pts.iter().all(|p| slot(p.input).is_some()) { + for p in pts { + if let Some(k) = slot(p.input) { + out[k] = per_mille(p.output); + } + } + // Photoshop needs both ends. + for k in [0, 12] { + if out[k] < 0 { + out[k] = per_mille(ink.density(f32::from(CURVE_AT[k]) / 100.0)); + } + } + return out; + } + for (k, x) in CURVE_AT.iter().enumerate() { + out[k] = per_mille(ink.density(f32::from(*x) / 100.0)); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + fn ink(name: &str, color: [f32; 3], curve: &[(f32, f32)]) -> DuotoneInk { + let curve = + if curve.is_empty() { DuotoneInk::new("", [0.0; 3]).curve } else { curve.iter().map(|&(input, output)| CurvePoint { input, output }).collect() }; + DuotoneInk { name: name.into(), color, curve } + } + + #[test] + fn writes_the_block_as_photoshop_does() { + // Black and orange (255, 128, 0) with plain curves: the block Photoshop 2025 wrote for the + // same inks, field by field. + let d = Duotone { inks: vec![ink("Black", [0.0; 3], &[]), ink("Orange", [1.0, 32895.0 / 65535.0, 0.0], &[])], psd_raw: None }; + let b = write(&d); + assert_eq!(b.len(), BLOCK_LEN); + assert_eq!(&b[..4], &[0, 1, 0, 2]); + assert_eq!(&b[4..14], &[0; 10], "black, RGB"); + assert_eq!(&b[14..24], &[0, 0, 0xFF, 0xFF, 0x80, 0x7F, 0, 0, 0, 0], "orange, RGB"); + assert_eq!(&b[24..44], &[NO_COLOR, NO_COLOR].concat(), "no third and fourth ink"); + assert_eq!(&b[44..50], b"\x05Black"); + assert_eq!(&b[108..115], b"\x06Orange"); + let identity: Vec = [0i16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1000, 0].iter().flat_map(|v| v.to_be_bytes()).collect(); + for i in 0..4 { + assert_eq!(&b[300 + 28 * i..328 + 28 * i], &identity[..], "curve {i}"); + } + assert_eq!(&b[412..414], &[0, 20], "dot gain"); + assert!(b[414..].chunks(10).all(|c| c == NO_COLOR), "overprint colours computed"); + } + + #[test] + fn inks_round_trip() { + let d = Duotone { + inks: vec![ + ink("Black", [0.0; 3], &[(0.0, 0.0), (0.6, 0.0), (1.0, 1.0)]), + ink("Teal", [0.0, 0.5, 0.5], &[(0.0, 0.1), (0.05, 0.2), (0.95, 0.9), (1.0, 0.95)]), + ink("Tan", [0.8, 0.7, 0.5], &[]), + ], + psd_raw: None, + }; + let mut unknown = false; + let back = parse(&write(&d), None, &mut unknown).unwrap(); + assert!(!unknown); + assert_eq!(back.len(), 3); + for (a, b) in d.inks.iter().zip(&back) { + assert_eq!(a.name, b.name); + assert!(a.color.iter().zip(b.color).all(|(x, y)| (x - y).abs() < 1e-4), "{a:?} {b:?}"); + assert_eq!(a.curve.len(), b.curve.len(), "{}", a.name); + for (p, q) in a.curve.iter().zip(&b.curve) { + assert!((p.input - q.input).abs() < 1e-6 && (p.output - q.output).abs() < 1e-3, "{}: {p:?} {q:?}", a.name); + } + } + } + + #[test] + fn a_curve_off_the_grid_is_sampled_at_all_13_points() { + let curve = [CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 0.33, output: 0.66 }, CurvePoint { input: 1.0, output: 1.0 }]; + let v = curve_values(Some(&curve)); + assert!(v.iter().all(|&x| x >= 0), "{v:?}"); + assert_eq!((v[0], v[6], v[12]), (0, 746, 1000), "{v:?}"); + } + + /// Black (CMYK) and PANTONE 327 CVC as Photoshop writes them (psd-tools' + /// `colormodes/4x4_8bit_duotone.psd`): the book ink is stored by its key, " 327C". + fn black_and_pantone() -> Vec { + let mut b = write(&Duotone { inks: vec![ink("Black", [0.0; 3], &[]), ink("PANTONE 327 CVC", [0.0; 3], &[])], psd_raw: None }); + b[4..14].copy_from_slice(&[0, 2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0]); + b[14..24].copy_from_slice(&[0, 3, 0x20, 0x20, 0x33, 0x32, 0x37, 0x43, 0, 0]); + b + } + + #[test] + fn a_colour_book_ink_without_its_lab_equivalent_is_unknown() { + let mut unknown = false; + let inks = parse(&black_and_pantone(), None, &mut unknown).unwrap(); + assert!(unknown); + assert_eq!((inks[1].name.as_str(), inks[1].color), ("PANTONE 327 CVC", [0.0; 3])); + assert!(inks[0].color.iter().all(|&c| c < 0.2), "CMYK black: {:?}", inks[0].color); + } + + #[test] + fn a_colour_book_ink_takes_its_colour_from_resource_1066() { + // Photoshop's resource 1066 of the same file: Lab (9.53, 4.98, 4.25) and (45.49, -66, -5). + let alternate = [0, 1, 0, 2, 0, 7, 0x03, 0xB9, 0x01, 0xF2, 0x01, 0xA9, 0, 0, 0, 7, 0x11, 0xC5, 0xE6, 0x38, 0xFE, 0x0C, 0, 0, 1, 0]; + let mut unknown = false; + let inks = parse(&black_and_pantone(), Some(&alternate), &mut unknown).unwrap(); + assert!(!unknown); + let [r, g, b] = inks[1].color; + assert!(r < 0.1 && g > 0.4 && b > 0.35 && g > b, "PANTONE 327 is teal: {:?}", inks[1].color); + // A short or foreign resource gives nothing. + let mut unknown = false; + parse(&black_and_pantone(), Some(&alternate[..14]), &mut unknown).unwrap(); + assert!(unknown); + } + + #[test] + fn an_hsb_ink_is_shown() { + let mut b = write(&Duotone { inks: vec![ink("Green", [0.0; 3], &[])], psd_raw: None }); + // Hue 120°, full saturation and brightness. + b[4..14].copy_from_slice(&[0, 1, 0x55, 0x55, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0]); + let mut unknown = false; + let inks = parse(&b, None, &mut unknown).unwrap(); + assert!(!unknown); + assert!(inks[0].color[1] > 0.99 && inks[0].color[0] < 0.01 && inks[0].color[2] < 0.01, "{:?}", inks[0].color); + } + + #[test] + fn a_curve_always_has_both_ends() { + // On the grid, but without a 0 % or a 100 % point: Photoshop won't open −1 there. + let curve = [CurvePoint { input: 0.2, output: 0.1 }, CurvePoint { input: 0.5, output: 0.7 }]; + let v = curve_values(Some(&curve)); + assert_eq!(v, [100, -1, -1, 100, -1, -1, 700, -1, -1, -1, -1, -1, 700]); + } + + #[test] + fn malformed_blocks_are_not_read() { + let mut unknown = false; + let good = write(&Duotone { inks: vec![ink("Black", [0.0; 3], &[])], psd_raw: None }); + assert!(parse(&good[..100], None, &mut unknown).is_none()); + let mut v = good.clone(); + v[1] = 2; + assert!(parse(&v, None, &mut unknown).is_none(), "version"); + let mut n = good; + n[3] = 5; + assert!(parse(&n, None, &mut unknown).is_none(), "ink count"); + } +} diff --git a/crates/io/src/effects_map.rs b/crates/io/src/effects_map.rs index 00815e2c83..0c7f60154f 100644 --- a/crates/io/src/effects_map.rs +++ b/crates/io/src/effects_map.rs @@ -69,9 +69,15 @@ const BLEND_LONG_NAMES: [(BlendMode, &str); 17] = [ (BlendMode::Luminosity, "luminosity"), ]; -/// A blend-mode enum from either its four-character code or its long string ID. +/// A blend-mode enum from its four-character code, its long string ID, or the layer record's key: +/// Photoshop 2026 writes `vLit`, `lddg`, `lbrn`… as an effect's `Md ` for some modes (#8567). pub(crate) fn blend_from_id(v: &[u8]) -> Option { - BLEND_NAMES.iter().chain(&BLEND_LONG_NAMES).find(|(_, n)| n.as_bytes() == v).map(|(m, _)| *m) + BLEND_NAMES + .iter() + .chain(&BLEND_LONG_NAMES) + .find(|(_, n)| n.as_bytes() == v) + .map(|(m, _)| *m) + .or_else(|| <[u8; 4]>::try_from(v).ok().and_then(BlendMode::from_psd_key)) } fn blend_of(d: &Descriptor, key: &str, default: BlendMode) -> BlendMode { @@ -85,7 +91,7 @@ pub(crate) fn blend_long_value(m: BlendMode) -> Value { Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new(n) } } -fn blend_value(m: BlendMode) -> Value { +pub(crate) fn blend_value(m: BlendMode) -> Value { let n = BLEND_NAMES.iter().find(|(b, _)| *b == m).map_or("Nrml", |(_, n)| n); Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new(n) } } @@ -122,14 +128,48 @@ fn contour(d: &Descriptor, key: &str) -> Contour { let linear = points.len() == 2 && points[0].input == 0.0 && points[0].output == 0.0 && points[1].input == 1.0 && points[1].output == 1.0; if points.is_empty() || (linear && (name.is_empty() || name == "Linear")) { Contour::Linear } else { Contour::Custom { name, points, corners } } } + +/// Indices of the contour points a PSD keeps, in order: all of them up to [`PSD_CONTOUR_POINTS`], +/// else both ends and then, one at a time, the point farthest (in output) from the line between +/// the kept points around it, so peaks and valleys (Ring - Double's two rings) survive. +fn psd_contour_points(points: &[CurvePoint]) -> Vec { + if points.len() <= PSD_CONTOUR_POINTS { + return (0..points.len()).collect(); + } + let mut kept = vec![0, points.len() - 1]; + while kept.len() < PSD_CONTOUR_POINTS { + let mut farthest: Option<(f32, usize)> = None; + for pair in kept.windows(2) { + let &[a, b] = pair else { continue }; + let (Some(pa), Some(pb)) = (points.get(a), points.get(b)) else { continue }; + let span = pb.input - pa.input; + for (i, p) in points.iter().enumerate().take(b).skip(a + 1) { + // Only equal inputs (a vertical step) have no span: inputs a few ulps apart still do. + let t = if span != 0.0 && span.is_finite() { (p.input - pa.input) / span } else { 0.0 }; + let off = (p.output - (pa.output + t * (pb.output - pa.output))).abs(); + if off.is_finite() && farthest.is_none_or(|(f, _)| off > f) { + farthest = Some((off, i)); + } + } + } + let Some((_, i)) = farthest else { break }; + kept.insert(kept.partition_point(|&k| k < i), i); + } + kept +} + +/// The most points Photoshop reads in an effect's contour (`ShpC` › `Crv `): a layer whose effects +/// carry a longer one fails to load ("A problem was encountered reading layer … program error"), +/// and Photoshop then drops all of its effects; 16 loads (#9556). +const PSD_CONTOUR_POINTS: usize = 16; fn contour_value(c: &Contour) -> Value { let (name, points) = match c { Contour::Linear => ("Linear".to_string(), vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 1.0, output: 1.0 }]), Contour::Custom { name, points, .. } => (name.clone(), points.clone()), }; - let pts = points - .iter() - .enumerate() + let pts = psd_contour_points(&points) + .into_iter() + .filter_map(|i| points.get(i).map(|p| (i, p))) .map(|(i, p)| { let d = Descriptor::new("CrPt").with("Hrzn", Value::Double(f64::from(p.input * 255.0))).with("Vrtc", Value::Double(f64::from(p.output * 255.0))); // Photoshop writes no `Cnty` (continuity) on smooth points; a corner carries `Cnty` false. @@ -213,6 +253,7 @@ fn parse_stroke(d: &Descriptor) -> StrokeFx { _ => StrokePosition::Outside, }, paint, + overprint: bool_of(d, "overprint"), } } @@ -239,6 +280,7 @@ fn parse_bevel(d: &Descriptor) -> Bevel { altitude: f(d, "Lald", 30.0), use_global_light: !matches!(d.get("uglg"), Some(Value::Boolean(false))), gloss_contour: contour(d, "TrnS"), + gloss_anti_alias: bool_of(d, "antialiasGloss"), highlight: FxCommon { enabled: true, blend: blend_of(d, "hglM", BlendMode::Screen), opacity: pct(d, "hglO", 0.75) }, highlight_color: get_desc(d, "hglC").and_then(color_from_desc).unwrap_or(Color::WHITE), shadow: FxCommon { enabled: true, blend: blend_of(d, "sdwM", BlendMode::Multiply), opacity: pct(d, "sdwO", 0.75) }, @@ -263,7 +305,9 @@ fn parse_one(key: &str, d: &Descriptor) -> Option { "IrGl" => Effect::InnerGlow(parse_glow(d, true)), "FrFX" => Effect::Stroke(parse_stroke(d)), "SoFi" => Effect::ColorOverlay { common: common(d, BlendMode::Normal, 1.0), color: color(d, "Clr ") }, - "GrFl" => Effect::GradientOverlay { common: common(d, BlendMode::Normal, 1.0), gradient: gradient_from(d), dither: bool_of(d, "Dthr") }, + "GrFl" => { + Effect::GradientOverlay { common: common(d, BlendMode::Normal, 1.0), gradient: gradient_from(d), dither: bool_of(d, "Dthr"), interpolation: None } + } "patternFill" => { let (name, id) = pattern_ref(d); let (angle, link, phase) = pattern_placement(d); @@ -301,7 +345,19 @@ const KINDS: [(&str, &str); 10] = [ /// Parses `lfx2` block data: `(master switch, effects)`. pub fn parse_lfx2(data: &[u8]) -> Option<(bool, Vec)> { let (vd, _) = VersionedDescriptor::parse_prefix(data.get(4..)?).ok()?; - let d = vd.descriptor; + Some(effects_from_descriptor(&vd.descriptor)) +} + +/// Keys of an effects descriptor (`lfx2`, a style library's `Lefx`) that +/// [`effects_from_descriptor`] reads, or that carry no effect of their own. +pub fn is_known_effects_key(key: &[u8]) -> bool { + KINDS.iter().any(|(single, multi)| single.as_bytes() == key || (!multi.is_empty() && multi.as_bytes() == key)) + || matches!(key, b"masterFXSwitch" | b"Scl " | b"numModifyingFX") +} + +/// The effects of an effects descriptor (the body of `lfx2`, a style library's `Lefx`): +/// `(master switch, effects)`. +pub fn effects_from_descriptor(d: &Descriptor) -> (bool, Vec) { let master = !matches!(d.get("masterFXSwitch"), Some(Value::Boolean(false))); let mut out = Vec::new(); for (single, multi) in KINDS { @@ -317,7 +373,7 @@ pub fn parse_lfx2(data: &[u8]) -> Option<(bool, Vec)> { } } _ => { - if let Some(x) = get_desc(&d, single) + if let Some(x) = get_desc(d, single) && let Some(e) = parse_one(single, x) { out.push(e); @@ -325,7 +381,7 @@ pub fn parse_lfx2(data: &[u8]) -> Option<(bool, Vec)> { } } } - Some((master, out)) + (master, out) } fn unit(u: &[u8; 4], v: f32) -> Value { @@ -404,7 +460,8 @@ fn write_one(e: &Effect) -> (&'static str, Descriptor) { }; let mut d = with_common(Descriptor::new("FrFX"), &s.common) .with("Styl", Value::Enumerated { type_id: Id::new("FStl"), value: Id::new(styl) }) - .with("Sz ", unit(b"#Pxl", s.size)); + .with("Sz ", unit(b"#Pxl", s.size)) + .with("overprint", Value::Boolean(s.overprint)); d = match &s.paint { FxPaint::Color(c) => { d.with("PntT", Value::Enumerated { type_id: Id::new("FrFl"), value: Id::new("SClr") }).with("Clr ", Value::Descriptor(color_to_desc(c))) @@ -425,7 +482,7 @@ fn write_one(e: &Effect) -> (&'static str, Descriptor) { ("FrFX", d) } Effect::ColorOverlay { common: c, color } => ("SoFi", with_common(Descriptor::new("SoFi"), c).with("Clr ", Value::Descriptor(color_to_desc(color)))), - Effect::GradientOverlay { common: c, gradient, dither } => { + Effect::GradientOverlay { common: c, gradient, dither, .. } => { ("GrFl", gradient_keys(with_common(Descriptor::new("GrFl"), c), gradient).with("Dthr", Value::Boolean(*dither))) } Effect::PatternOverlay { common: c, name, id, scale, angle, link, phase } => ( @@ -482,6 +539,7 @@ fn write_one(e: &Effect) -> (&'static str, Descriptor) { .with("blur", unit(b"#Pxl", b.size)) .with("bvlD", Value::Enumerated { type_id: Id::new("BESs"), value: Id::new(if b.up { "In " } else { "Out " }) }) .with("TrnS", contour_value(&b.gloss_contour)) + .with("antialiasGloss", Value::Boolean(b.gloss_anti_alias)) .with("Sftn", unit(b"#Pxl", b.soften)); let d = match &b.contour { Some(c) => d @@ -511,6 +569,13 @@ fn write_one(e: &Effect) -> (&'static str, Descriptor) { /// Serializes effects as `lfx2` block data. pub fn write_lfx2(master: bool, items: &[Effect]) -> Vec { + let mut out = 0u32.to_be_bytes().to_vec(); + out.extend(VersionedDescriptor::new(effects_descriptor(master, items)).to_bytes()); + out +} + +/// Effects as an effects descriptor (the body of `lfx2`, a style library's `Lefx`). +pub fn effects_descriptor(master: bool, items: &[Effect]) -> Descriptor { let mut d = Descriptor::new("null").with("Scl ", unit(b"#Prc", 100.0)).with("masterFXSwitch", Value::Boolean(master)); for (single, multi) in KINDS { let of_kind: Vec = items.iter().map(write_one).filter(|(k, _)| *k == single).map(|(_, d)| d).collect(); @@ -522,9 +587,7 @@ pub fn write_lfx2(master: bool, items: &[Effect]) -> Vec { _ => d = d.with(single, Value::Descriptor(of_kind.into_iter().next().unwrap_or_default())), } } - let mut out = 0u32.to_be_bytes().to_vec(); - out.extend(VersionedDescriptor::new(d).to_bytes()); - out + d } fn fixed(v: u32) -> f32 { @@ -618,232 +681,4 @@ pub fn parse_lrfx(data: &[u8]) -> Option<(bool, Vec)> { #[cfg(test)] #[allow(clippy::unreachable)] // clippy.toml exempts unwrap/expect/panic in tests, not unreachable! -mod tests { - use super::*; - - fn sample_effects() -> Vec { - let g = Gradient { - stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], - opacity_stops: vec![(0.0, 1.0), (1.0, 0.5)], - ..Gradient::default() - }; - vec![ - Effect::default_drop_shadow(), - Effect::DropShadow(Shadow { - distance: 9.0, - spread: 0.25, - contour: Contour::Custom { - name: "Cone".into(), - points: vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 0.5, output: 1.0 }, CurvePoint { input: 1.0, output: 0.0 }], - corners: vec![false, true, false], - }, - ..match Effect::default_drop_shadow() { - Effect::DropShadow(s) => s, - _ => unreachable!(), - } - }), - Effect::InnerShadow(Shadow { - knocks_out: false, - ..match Effect::default_drop_shadow() { - Effect::DropShadow(s) => s, - _ => unreachable!(), - } - }), - Effect::OuterGlow(Glow { - common: FxCommon::new(BlendMode::Screen, 0.5), - paint: FxPaint::Color(Color::rgb(1.0, 1.0, 0.0)), - technique: GlowTechnique::Precise, - spread: 0.1, - size: 8.0, - contour: Contour::Linear, - anti_alias: true, - range: 0.5, - jitter: 0.0, - noise: 0.0, - source: GlowSource::Edge, - }), - Effect::InnerGlow(Glow { - common: FxCommon::new(BlendMode::Screen, 0.75), - paint: FxPaint::Gradient(g.clone()), - technique: GlowTechnique::Softer, - spread: 0.0, - size: 5.0, - contour: Contour::Linear, - anti_alias: false, - range: 0.5, - jitter: 0.0, - noise: 0.0, - source: GlowSource::Center, - }), - Effect::Stroke(StrokeFx { - common: FxCommon::new(BlendMode::Normal, 1.0), - size: 3.0, - position: StrokePosition::Inside, - paint: FxPaint::Color(Color::rgb(0.0, 1.0, 0.0)), - }), - Effect::Stroke(StrokeFx { - common: FxCommon::new(BlendMode::Multiply, 0.5), - size: 6.0, - position: StrokePosition::Center, - paint: FxPaint::Gradient(g.clone()), - }), - Effect::ColorOverlay { common: FxCommon::new(BlendMode::Overlay, 0.5), color: Color::rgb(0.0, 0.0, 1.0) }, - Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: g, dither: true }, - Effect::PatternOverlay { - common: FxCommon::new(BlendMode::Normal, 1.0), - name: "Bubbles".into(), - id: "abc".into(), - scale: 1.0, - angle: 15.0, - link: false, - phase: (4.0, 2.0), - }, - Effect::Satin(Satin { - common: FxCommon::new(BlendMode::Multiply, 0.5), - color: Color::BLACK, - angle: 19.0, - distance: 11.0, - size: 14.0, - contour: Contour::Linear, - anti_alias: true, - invert: true, - }), - Effect::BevelEmboss(Bevel { - enabled: true, - style: BevelStyle::Emboss, - technique: BevelTechnique::ChiselSoft, - depth: 1.5, - up: false, - size: 7.0, - soften: 2.0, - angle: 45.0, - altitude: 40.0, - use_global_light: false, - gloss_contour: Contour::Linear, - highlight: FxCommon::new(BlendMode::Screen, 0.75), - highlight_color: Color::WHITE, - shadow: FxCommon::new(BlendMode::Multiply, 0.6), - shadow_color: Color::BLACK, - contour: Some(BevelContour { contour: Contour::Linear, range: 0.7, anti_alias: true }), - texture: Some(BevelTexture { name: "Bubbles".into(), id: "abc".into(), scale: 0.5, depth: -2.0, invert: true, link: false, phase: (3.0, 1.0) }), - }), - ] - } - - fn approx(a: &[Effect], b: &[Effect]) { - // Colours go through 0..255 doubles; compare via re-serialization. - assert_eq!(a.len(), b.len()); - assert_eq!(write_lfx2(true, a), write_lfx2(true, b)); - } - - #[test] - fn lfx2_roundtrip_all_kinds() { - let fx = sample_effects(); - let data = write_lfx2(false, &fx); - let (master, back) = parse_lfx2(&data).unwrap(); - assert!(!master); - approx(&fx, &back); - // Kind order is canonical: shadows first ... strokes last. - assert!(matches!(back[0], Effect::DropShadow(_))); - assert!(matches!(back.last().unwrap(), Effect::Stroke(_))); - } - - #[test] - fn lfx2_ignores_absent_effects_but_keeps_disabled_configured_effects() { - let d = Descriptor::new("null") - .with("DrSh", Value::Descriptor(Descriptor::new("DrSh").with("present", Value::Boolean(false)).with("enab", Value::Boolean(false)))) - .with("FrFX", Value::Descriptor(Descriptor::new("FrFX").with("present", Value::Boolean(true)).with("enab", Value::Boolean(false)))) - .with("SoFi", Value::Descriptor(Descriptor::new("SoFi").with("enab", Value::Boolean(false)))); - let mut data = 0u32.to_be_bytes().to_vec(); - data.extend(VersionedDescriptor::new(d).to_bytes()); - - let (_, effects) = parse_lfx2(&data).unwrap(); - - assert_eq!(effects.len(), 2); - assert!(effects.iter().all(|effect| !effect.enabled())); - assert!(effects.iter().any(|effect| effect.label() == "Stroke")); - assert!(effects.iter().any(|effect| effect.label() == "Color Overlay")); - } - - #[test] - fn multi_instances_use_multi_keys() { - let fx = sample_effects(); - let data = write_lfx2(true, &fx); - let (vd, _) = VersionedDescriptor::parse_prefix(&data[4..]).unwrap(); - assert!(vd.descriptor.get("dropShadowMulti").is_some()); - assert!(vd.descriptor.get("frameFXMulti").is_some()); - assert!(vd.descriptor.get("SoFi").is_some()); - } - - #[test] - fn blend_names_are_unique() { - let mut s = std::collections::HashSet::new(); - for (_, n) in BLEND_NAMES { - assert!(s.insert(n)); - } - } - - #[test] - fn long_string_blend_ids_parse() { - // Some writers store effect modes as long string IDs (`BlnM` `multiply`, `colorDodge`) - // instead of the four-character codes (psd-tools effects/blend-modes.psd). - let long = |v: &str| Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new(v) }; - let d = Descriptor::new("null") - .with("masterFXSwitch", Value::Boolean(true)) - .with("DrSh", Value::Descriptor(Descriptor::new("DrSh").with("enab", Value::Boolean(true)).with("Md ", long("linearBurn")))) - .with("ebbl", Value::Descriptor(Descriptor::new("ebbl").with("hglM", long("colorDodge")).with("sdwM", long("colorBurn")))) - .with("SoFi", Value::Descriptor(Descriptor::new("SoFi").with("Md ", long("hue")))); - let mut data = 0u32.to_be_bytes().to_vec(); - data.extend(VersionedDescriptor { version: 16, descriptor: d }.to_bytes()); - let (_, fx) = parse_lfx2(&data).unwrap(); - let modes: Vec = fx - .iter() - .flat_map(|e| match e { - Effect::DropShadow(s) => vec![s.common.blend], - Effect::BevelEmboss(b) => vec![b.highlight.blend, b.shadow.blend], - Effect::ColorOverlay { common, .. } => vec![common.blend], - _ => vec![], - }) - .collect(); - assert_eq!(modes, vec![BlendMode::LinearBurn, BlendMode::ColorDodge, BlendMode::ColorBurn, BlendMode::Hue]); - for (m, n) in BLEND_LONG_NAMES { - assert_eq!(blend_from_id(n.as_bytes()), Some(m)); - } - } - - #[test] - fn garbage_is_none() { - assert!(parse_lfx2(&[0, 0]).is_none()); - assert!(parse_lrfx(&[0, 0, 0, 1, b'X']).is_none()); - } - - #[test] - fn lrfx_drop_shadow() { - // version 0, count 1, 'dsdw' v2 record. - let mut d = vec![0, 0, 0, 1]; - d.extend_from_slice(b"8BIMdsdw"); - let mut rec = Vec::new(); - rec.extend_from_slice(&2u32.to_be_bytes()); // version - rec.extend_from_slice(&(7u32 << 16).to_be_bytes()); // blur (16.16) - rec.extend_from_slice(&0u32.to_be_bytes()); // intensity - rec.extend_from_slice(&(90u32 << 16).to_be_bytes()); // angle - rec.extend_from_slice(&(4u32 << 16).to_be_bytes()); // distance - rec.extend_from_slice(&[0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0]); // color: red - rec.extend_from_slice(b"8BIMmul "); - rec.extend_from_slice(&[1, 1, 128]); - rec.extend_from_slice(&[0; 10]); - d.extend_from_slice(&(rec.len() as u32).to_be_bytes()); - d.extend(rec); - let (_, fx) = parse_lrfx(&d).unwrap(); - match &fx[0] { - Effect::DropShadow(s) => { - assert_eq!(s.size, 7.0); - assert_eq!(s.distance, 4.0); - assert_eq!(s.common.blend, BlendMode::Multiply); - assert!((s.common.opacity - 128.0 / 255.0).abs() < 1e-6); - assert_eq!(s.color.c[0], 1.0); - } - other => panic!("{other:?}"), - } - } -} +mod tests; diff --git a/crates/io/src/effects_map/tests.rs b/crates/io/src/effects_map/tests.rs new file mode 100644 index 0000000000..5a43cd194e --- /dev/null +++ b/crates/io/src/effects_map/tests.rs @@ -0,0 +1,430 @@ +use super::*; + +fn sample_effects() -> Vec { + let g = Gradient { + stops: vec![(0.0, Color::rgb(1.0, 0.0, 0.0)), (1.0, Color::rgb(0.0, 0.0, 1.0))], + opacity_stops: vec![(0.0, 1.0), (1.0, 0.5)], + ..Gradient::default() + }; + vec![ + Effect::default_drop_shadow(), + Effect::DropShadow(Shadow { + distance: 9.0, + spread: 0.25, + contour: Contour::Custom { + name: "Cone".into(), + points: vec![CurvePoint { input: 0.0, output: 0.0 }, CurvePoint { input: 0.5, output: 1.0 }, CurvePoint { input: 1.0, output: 0.0 }], + corners: vec![false, true, false], + }, + ..match Effect::default_drop_shadow() { + Effect::DropShadow(s) => s, + _ => unreachable!(), + } + }), + Effect::InnerShadow(Shadow { + knocks_out: false, + ..match Effect::default_drop_shadow() { + Effect::DropShadow(s) => s, + _ => unreachable!(), + } + }), + Effect::OuterGlow(Glow { + common: FxCommon::new(BlendMode::Screen, 0.5), + paint: FxPaint::Color(Color::rgb(1.0, 1.0, 0.0)), + technique: GlowTechnique::Precise, + spread: 0.1, + size: 8.0, + contour: Contour::Linear, + anti_alias: true, + range: 0.5, + jitter: 0.0, + noise: 0.0, + source: GlowSource::Edge, + }), + Effect::InnerGlow(Glow { + common: FxCommon::new(BlendMode::Screen, 0.75), + paint: FxPaint::Gradient(g.clone()), + technique: GlowTechnique::Softer, + spread: 0.0, + size: 5.0, + contour: Contour::Linear, + anti_alias: false, + range: 0.5, + jitter: 0.0, + noise: 0.0, + source: GlowSource::Center, + }), + Effect::Stroke(StrokeFx { + common: FxCommon::new(BlendMode::Normal, 1.0), + size: 3.0, + position: StrokePosition::Inside, + paint: FxPaint::Color(Color::rgb(0.0, 1.0, 0.0)), + overprint: false, + }), + Effect::Stroke(StrokeFx { + common: FxCommon::new(BlendMode::Multiply, 0.5), + size: 6.0, + position: StrokePosition::Center, + paint: FxPaint::Gradient(g.clone()), + overprint: true, + }), + Effect::ColorOverlay { common: FxCommon::new(BlendMode::Overlay, 0.5), color: Color::rgb(0.0, 0.0, 1.0) }, + Effect::GradientOverlay { common: FxCommon::new(BlendMode::Normal, 1.0), gradient: g, dither: true, interpolation: None }, + Effect::PatternOverlay { + common: FxCommon::new(BlendMode::Normal, 1.0), + name: "Bubbles".into(), + id: "abc".into(), + scale: 1.0, + angle: 15.0, + link: false, + phase: (4.0, 2.0), + }, + Effect::Satin(Satin { + common: FxCommon::new(BlendMode::Multiply, 0.5), + color: Color::BLACK, + angle: 19.0, + distance: 11.0, + size: 14.0, + contour: Contour::Linear, + anti_alias: true, + invert: true, + }), + Effect::BevelEmboss(Bevel { + enabled: true, + style: BevelStyle::Emboss, + technique: BevelTechnique::ChiselSoft, + depth: 1.5, + up: false, + size: 7.0, + soften: 2.0, + angle: 45.0, + altitude: 40.0, + use_global_light: false, + gloss_contour: Contour::Linear, + gloss_anti_alias: true, + highlight: FxCommon::new(BlendMode::Screen, 0.75), + highlight_color: Color::WHITE, + shadow: FxCommon::new(BlendMode::Multiply, 0.6), + shadow_color: Color::BLACK, + contour: Some(BevelContour { contour: Contour::Linear, range: 0.7, anti_alias: true }), + texture: Some(BevelTexture { name: "Bubbles".into(), id: "abc".into(), scale: 0.5, depth: -2.0, invert: true, link: false, phase: (3.0, 1.0) }), + }), + ] +} + +/// #9556: Photoshop drops every effect of a layer whose contour has more than 16 points. A +/// 29-point contour with two rings is written as 16 points that keep both ends and both peaks; +/// up to 16 points are written as they are. +#[test] +fn psd_contours_keep_at_most_16_points_and_their_peaks() { + let points: Vec = (0..29) + .map(|i| { + let x = i as f32 / 28.0; + // Rings at x = 0.25 and 0.75, small wiggles between. + let y = (1.0 - ((x - 0.25).abs() * 8.0)).max(0.0).max(1.0 - (x - 0.75).abs() * 8.0) + if i % 2 == 0 { 0.0 } else { 0.01 }; + CurvePoint { input: x, output: y.clamp(0.0, 1.0) } + }) + .collect(); + let c = Contour::Custom { name: "Two rings".into(), points: points.clone(), corners: vec![false; 29] }; + let out = crv(&contour_value(&c)); + assert_eq!(out.len(), 16); + assert_eq!(out.first().copied(), Some((0.0, f64::from(points[0].output) * 255.0))); + assert_eq!(out.last().map(|p| p.0), Some(255.0)); + let peak = |x: f32| out.iter().any(|p| ((p.0 / 255.0) as f32 - x).abs() < 0.03 && p.1 > 240.0); + assert!(peak(0.25) && peak(0.75), "both rings kept: {out:?}"); + assert!(out.windows(2).all(|w| w[0].0 < w[1].0), "in order: {out:?}"); + + let short = Contour::Custom { name: "Short".into(), points: points[..16].to_vec(), corners: vec![false; 16] }; + assert_eq!(crv(&contour_value(&short)).len(), 16); +} + +fn crv(v: &Value) -> Vec<(f64, f64)> { + let Value::Descriptor(d) = v else { panic!("ShpC") }; + let Some(Value::List(pts)) = d.get("Crv ") else { panic!("Crv ") }; + pts.iter() + .map(|p| { + let Value::Descriptor(p) = p else { panic!("CrPt") }; + let n = |k: &str| match p.get(k) { + Some(Value::Double(x)) => *x, + _ => panic!("{k}"), + }; + (n("Hrzn"), n("Vrtc")) + }) + .collect() +} + +fn approx(a: &[Effect], b: &[Effect]) { + // Colours go through 0..255 doubles; compare via re-serialization. + assert_eq!(a.len(), b.len()); + assert_eq!(write_lfx2(true, a), write_lfx2(true, b)); +} + +/// `approx` compares re-serialized bytes, so a field the writer drops looks equal on both +/// sides: read the Gloss Contour's Anti-aliased back directly. +#[test] +fn bevel_gloss_anti_alias_is_written() { + let fx: Vec = sample_effects().into_iter().filter(|e| matches!(e, Effect::BevelEmboss(_))).collect(); + assert!(matches!(fx.as_slice(), [Effect::BevelEmboss(b)] if b.gloss_anti_alias)); + let (_, back) = parse_lfx2(&write_lfx2(false, &fx)).unwrap(); + assert!(matches!(back.as_slice(), [Effect::BevelEmboss(b)] if b.gloss_anti_alias), "{back:?}"); +} + +#[test] +fn lfx2_roundtrip_all_kinds() { + let fx = sample_effects(); + let data = write_lfx2(false, &fx); + let (master, back) = parse_lfx2(&data).unwrap(); + assert!(!master); + approx(&fx, &back); + // Kind order is canonical: shadows first ... strokes last. + assert!(matches!(back[0], Effect::DropShadow(_))); + assert!(matches!(back.last().unwrap(), Effect::Stroke(_))); +} + +#[test] +fn stroke_overprint_is_read_and_written() { + for overprint in [false, true] { + let fx = [Effect::Stroke(StrokeFx { + common: FxCommon::new(BlendMode::Normal, 1.0), + size: 3.0, + position: StrokePosition::Outside, + paint: FxPaint::Color(Color::BLACK), + overprint, + })]; + let (_, back) = parse_lfx2(&write_lfx2(false, &fx)).unwrap(); + let [Effect::Stroke(s)] = back.as_slice() else { panic!("{back:?}") }; + assert_eq!(s.overprint, overprint); + } + // A stroke without the key (other writers) has Overprint off. + let d = Descriptor::new("null").with("FrFX", Value::Descriptor(Descriptor::new("FrFX").with("enab", Value::Boolean(true)))); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor::new(d).to_bytes()); + let (_, back) = parse_lfx2(&data).unwrap(); + let [Effect::Stroke(s)] = back.as_slice() else { panic!("{back:?}") }; + assert!(!s.overprint); +} + +#[test] +fn lfx2_ignores_absent_effects_but_keeps_disabled_configured_effects() { + let d = Descriptor::new("null") + .with("DrSh", Value::Descriptor(Descriptor::new("DrSh").with("present", Value::Boolean(false)).with("enab", Value::Boolean(false)))) + .with("FrFX", Value::Descriptor(Descriptor::new("FrFX").with("present", Value::Boolean(true)).with("enab", Value::Boolean(false)))) + .with("SoFi", Value::Descriptor(Descriptor::new("SoFi").with("enab", Value::Boolean(false)))); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor::new(d).to_bytes()); + + let (_, effects) = parse_lfx2(&data).unwrap(); + + assert_eq!(effects.len(), 2); + assert!(effects.iter().all(|effect| !effect.enabled())); + assert!(effects.iter().any(|effect| effect.label() == "Stroke")); + assert!(effects.iter().any(|effect| effect.label() == "Color Overlay")); +} + +#[test] +fn multi_instances_use_multi_keys() { + let fx = sample_effects(); + let data = write_lfx2(true, &fx); + let (vd, _) = VersionedDescriptor::parse_prefix(&data[4..]).unwrap(); + assert!(vd.descriptor.get("dropShadowMulti").is_some()); + assert!(vd.descriptor.get("frameFXMulti").is_some()); + assert!(vd.descriptor.get("SoFi").is_some()); +} + +#[test] +fn blend_names_are_unique() { + let mut s = std::collections::HashSet::new(); + for (_, n) in BLEND_NAMES { + assert!(s.insert(n)); + } +} + +#[test] +fn layer_record_blend_keys_parse() { + for (key, mode) in [ + (b"vLit", BlendMode::VividLight), + (b"lLit", BlendMode::LinearLight), + (b"pLit", BlendMode::PinLight), + (b"hMix", BlendMode::HardMix), + (b"lbrn", BlendMode::LinearBurn), + (b"lddg", BlendMode::LinearDodge), + (b"dkCl", BlendMode::DarkerColor), + (b"lgCl", BlendMode::LighterColor), + (b"fsub", BlendMode::Subtract), + (b"fdiv", BlendMode::Divide), + ] { + assert_eq!(blend_from_id(key), Some(mode), "{}", String::from_utf8_lossy(key)); + } + assert_eq!(blend_from_id(b"zzzz"), None); + // The issue's Color Overlay: Vivid Light stored as `vLit`, not left as Normal. + let key = |v: &str| Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new(v) }; + let d = Descriptor::new("null") + .with("masterFXSwitch", Value::Boolean(true)) + .with("SoFi", Value::Descriptor(Descriptor::new("SoFi").with("enab", Value::Boolean(true)).with("Md ", key("vLit")))); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor { version: 16, descriptor: d }.to_bytes()); + let (_, fx) = parse_lfx2(&data).unwrap(); + let modes: Vec = fx.iter().filter_map(|e| if let Effect::ColorOverlay { common, .. } = e { Some(common.blend) } else { None }).collect(); + assert_eq!(modes, vec![BlendMode::VividLight]); +} + +#[test] +fn long_string_blend_ids_parse() { + // Some writers store effect modes as long string IDs (`BlnM` `multiply`, `colorDodge`) + // instead of the four-character codes (psd-tools effects/blend-modes.psd). + let long = |v: &str| Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new(v) }; + let d = Descriptor::new("null") + .with("masterFXSwitch", Value::Boolean(true)) + .with("DrSh", Value::Descriptor(Descriptor::new("DrSh").with("enab", Value::Boolean(true)).with("Md ", long("linearBurn")))) + .with("ebbl", Value::Descriptor(Descriptor::new("ebbl").with("hglM", long("colorDodge")).with("sdwM", long("colorBurn")))) + .with("SoFi", Value::Descriptor(Descriptor::new("SoFi").with("Md ", long("hue")))); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor { version: 16, descriptor: d }.to_bytes()); + let (_, fx) = parse_lfx2(&data).unwrap(); + let modes: Vec = fx + .iter() + .flat_map(|e| match e { + Effect::DropShadow(s) => vec![s.common.blend], + Effect::BevelEmboss(b) => vec![b.highlight.blend, b.shadow.blend], + Effect::ColorOverlay { common, .. } => vec![common.blend], + _ => vec![], + }) + .collect(); + assert_eq!(modes, vec![BlendMode::LinearBurn, BlendMode::ColorDodge, BlendMode::ColorBurn, BlendMode::Hue]); + for (m, n) in BLEND_LONG_NAMES { + assert_eq!(blend_from_id(n.as_bytes()), Some(m)); + } +} + +/// A Gradient Overlay `lfx2` laid out as Photoshop writes it: one black colour, opacity +/// 100 → 0 % with the opacity midpoint `mid` (%), Smoothness `intr` (0..4096) and the method +/// enum `gs99` (Photoshop's key in layer effects; codes `Gcls`, `Lnr `, `Perc`). +fn overlay_lfx2(mid: i32, intr: f64, method: &str) -> Vec { + let black = || Descriptor::new("RGBC").with("Rd ", Value::Double(0.0)).with("Grn ", Value::Double(0.0)).with("Bl ", Value::Double(0.0)); + let clr = |loc: i32| { + Value::Descriptor( + Descriptor::new("Clrt") + .with("Clr ", Value::Descriptor(black())) + .with("Type", Value::Enumerated { type_id: Id::new("Clry"), value: Id::new("UsrS") }) + .with("Lctn", Value::Integer(loc)) + .with("Mdpn", Value::Integer(50)), + ) + }; + let trn = |loc: i32, op: f32, mid: i32| { + Value::Descriptor(Descriptor::new("TrnS").with("Opct", unit(b"#Prc", op)).with("Lctn", Value::Integer(loc)).with("Mdpn", Value::Integer(mid))) + }; + let grad = Descriptor::new("Grdn") + .with("GrdF", Value::Enumerated { type_id: Id::new("GrdF"), value: Id::new("CstS") }) + .with("Intr", Value::Double(intr)) + .with("Clrs", Value::List(vec![clr(0), clr(4096)])) + .with("Trns", Value::List(vec![trn(0, 100.0, 50), trn(4096, 0.0, mid)])); + let fx = Descriptor::new("GrFl") + .with("enab", Value::Boolean(true)) + .with("Grad", Value::Descriptor(grad)) + .with("Angl", unit(b"#Ang", 0.0)) + .with("gs99", Value::Enumerated { type_id: Id::new("gradientInterpolationMethodType"), value: Id::new(method) }); + let d = Descriptor::new("null").with("masterFXSwitch", Value::Boolean(true)).with("GrFl", Value::Descriptor(fx)); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor { version: 16, descriptor: d }.to_bytes()); + data +} + +fn overlay_opacity(data: &[u8]) -> Vec<(f32, f32)> { + let (_, fx) = parse_lfx2(data).unwrap(); + let Some(Effect::GradientOverlay { gradient, .. }) = fx.first() else { panic!("overlay: {fx:?}") }; + gradient.opacity_stops.clone() +} + +fn opacity_at(o: &[(f32, f32)], t: f32) -> f32 { + let i = o.windows(2).position(|w| t >= w[0].0 && t <= w[1].0).unwrap(); + o[i].1 + (o[i + 1].1 - o[i].1) * (t - o[i].0) / (o[i + 1].0 - o[i].0) +} + +/// #1954: the overlay's transparency follows Smoothness, the opacity midpoints and the method +/// like a gradient fill layer's (Photoshop renders both identically; the values are its +/// composites at t = 0.25 and 0.5). +#[test] +fn gradient_overlay_opacity_follows_smoothness_midpoint_and_method() { + let at = |mid, intr, method: &str, t| opacity_at(&overlay_opacity(&overlay_lfx2(mid, intr, method)), t); + // Plain Classic stays the linear fade it is stored as. + assert_eq!(overlay_opacity(&overlay_lfx2(50, 0.0, "Gcls")), vec![(0.0, 1.0), (1.0, 0.0)]); + // Midpoint 25 %: half transparent a quarter of the way. + assert!((at(25, 0.0, "Gcls", 0.25) - 0.5).abs() < 0.01); + // Smoothness 100 % eases the ends (Photoshop: 0.796 at a quarter). + assert!((at(50, 4096.0, "Gcls", 0.25) - 0.796).abs() < 0.015); + // The method bends the transparency ramp too, not only the colours (Photoshop at the + // middle: 0.631 Linear, 0.255 Perceptual). + assert!((at(50, 0.0, "Lnr ", 0.5) - 0.631).abs() < 0.015); + assert!((at(50, 0.0, "Perc", 0.5) - 0.255).abs() < 0.015); + assert!((at(25, 4096.0, "Perc", 0.25) - 0.255).abs() < 0.015); +} + +/// A regenerated `lfx2` carries the baked ramp as plain (Classic, Smoothness 0) stops, so it +/// reads back unchanged. +#[test] +fn baked_overlay_opacity_round_trips() { + let data = overlay_lfx2(25, 4096.0, "Perc"); + let (master, fx) = parse_lfx2(&data).unwrap(); + let a = overlay_opacity(&data); + let b = overlay_opacity(&write_lfx2(master, &fx)); + assert!(a.len() > 2); + assert_eq!(a.len(), b.len()); + for (x, y) in a.iter().zip(&b) { + assert!((x.0 - y.0).abs() < 1e-3 && (x.1 - y.1).abs() < 1e-4, "{x:?} vs {y:?}"); + } +} + +#[test] +fn garbage_is_none() { + assert!(parse_lfx2(&[0, 0]).is_none()); + assert!(parse_lrfx(&[0, 0, 0, 1, b'X']).is_none()); +} + +#[test] +fn lrfx_drop_shadow() { + // version 0, count 1, 'dsdw' v2 record. + let mut d = vec![0, 0, 0, 1]; + d.extend_from_slice(b"8BIMdsdw"); + let mut rec = Vec::new(); + rec.extend_from_slice(&2u32.to_be_bytes()); // version + rec.extend_from_slice(&(7u32 << 16).to_be_bytes()); // blur (16.16) + rec.extend_from_slice(&0u32.to_be_bytes()); // intensity + rec.extend_from_slice(&(90u32 << 16).to_be_bytes()); // angle + rec.extend_from_slice(&(4u32 << 16).to_be_bytes()); // distance + rec.extend_from_slice(&[0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0]); // color: red + rec.extend_from_slice(b"8BIMmul "); + rec.extend_from_slice(&[1, 1, 128]); + rec.extend_from_slice(&[0; 10]); + d.extend_from_slice(&(rec.len() as u32).to_be_bytes()); + d.extend(rec); + let (_, fx) = parse_lrfx(&d).unwrap(); + match &fx[0] { + Effect::DropShadow(s) => { + assert_eq!(s.size, 7.0); + assert_eq!(s.distance, 4.0); + assert_eq!(s.common.blend, BlendMode::Multiply); + assert!((s.common.opacity - 128.0 / 255.0).abs() < 1e-6); + assert_eq!(s.color.c[0], 1.0); + } + other => panic!("{other:?}"), + } +} + +/// CodeRabbit on #9590: inputs closer than `f32::EPSILON` but distinct are still a span. A point +/// on the straight line between two such inputs isn't mistaken for a bend and kept in place of a +/// genuine one. +#[test] +fn a_point_on_a_tiny_straight_span_is_not_a_bend() { + let p = |input: f32, output: f32| CurvePoint { input, output }; + // Three distinct inputs a few ulps apart near 0.5, on a straight rise from 0 to 1. + let (a, b) = (0.5_f32, f32::from_bits(0.5_f32.to_bits() + 2)); + let mid = f32::from_bits(0.5_f32.to_bits() + 1); + let mut points = vec![p(0.0, 0.0), p(0.2, 0.0)]; + points.extend([p(a, 0.0), p(mid, 0.5), p(b, 1.0)]); + // Fourteen gentle bends after it, each one a real (if small) peak. + points.extend((0..14).map(|k| p(0.55 + k as f32 * 0.03, if k % 2 == 0 { 1.0 } else { 0.9 }))); + points.push(p(1.0, 1.0)); + let kept = psd_contour_points(&points); + assert_eq!(kept.len(), 16); + assert!(!kept.contains(&3), "the point on the tiny straight span was kept over a real bend: {kept:?}"); +} diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs index a18367467a..975a988e95 100644 --- a/crates/io/src/flat.rs +++ b/crates/io/src/flat.rs @@ -22,11 +22,12 @@ pub fn import_flat(name: &str, bytes: &[u8]) -> Result { return import_tiff_page(name, bytes, None); } let img = codecs::decode_as(format.ok_or(codecs::CodecError::UnknownFormat)?, bytes)?; - // A palette BMP or PNG opens in Indexed Color mode with its colour table, as in Photoshop - // (#3802, #4804). + // A palette BMP, PNG or GIF opens in Indexed Color mode with its colour table, as in + // Photoshop (#3802, #4804, #7886). A GIF is always palette-based: the first frame's table. let palette = match format { Some(Format::Bmp) => codecs::bmp_palette(bytes).ok().flatten().map(|c| (c, None)), Some(Format::Png) => codecs::png_palette(bytes).ok().flatten().map(|p| (p.colors, p.transparent)), + Some(Format::Gif) => codecs::gif_palette(bytes), _ => None, }; let mut r = image_to_document(name, &img)?; @@ -51,14 +52,57 @@ pub fn import_flat(name: &str, bytes: &[u8]) -> Result { _ => None, }; } + if d.icc_profile.is_none() && d.mode == ColorMode::Rgb { + d.icc_profile = cicp_profile(&img); + if d.icc_profile.is_none() + && let Some((profile, warning)) = hdr_fallback(&img) + { + d.icc_profile = Some(profile); + r.warnings.push(warning); + } + } Ok(r) } +/// A PQ or HLG image, which the decoder turned into linear light, on primaries +/// [`cicp_profile`] has no profile for: left untagged it would be read as sRGB-encoded, so it is +/// tagged linear sRGB (its tones right, its primaries assumed) and the warning says so. +fn hdr_fallback(img: &Image) -> Option<(Arc>, String)> { + let c = img.cicp.filter(|c| c.is_hdr() && img.sample_type().is_float())?; + let warning = format!("the HDR colour primaries (H.273 code {}) aren't supported; the image is read as linear sRGB", c.primaries); + Some((photocraft_cms::Builtin::LinearSrgb.profile().to_bytes(), warning)) +} + +/// The profile a HEIF's `nclx` colour description stands for, when the file has no ICC profile +/// (#6752): a BT.2020 or P3 image is tagged so, and a PQ or HLG one, which the decoder turned into +/// linear light, gets a linear profile on its primaries. BT.709 SDR stays untagged, read as sRGB +/// as before; BT.709 with a linear transfer (8) is tagged linear sRGB. +fn cicp_profile(img: &Image) -> Option>> { + let c = img.cicp?; + let linear = img.sample_type().is_float(); + if c.primaries == 1 && !linear && matches!(c.transfer, 1 | 2 | 6 | 13 | 14 | 15) { + return None; + } + // Unspecified primaries on an HDR image: BT.2100's, which PQ and HLG are defined with. + let primaries = if c.primaries == 2 && c.is_hdr() { 9 } else { c.primaries }; + photocraft_cms::builtin::cicp_rgb(primaries, c.transfer, linear).map(|p| p.to_bytes()) +} + /// Every frame of a GIF as a single-layer document, handed to `each` in order with its delay in /// milliseconds (a still GIF has one). One frame at a time becomes a document, so the caller /// can flatten and drop it. Returns the number of frames; stops at the first error `each` gives. pub fn import_gif_frames(name: &str, bytes: &[u8], each: &mut dyn FnMut(Document, u32) -> Result<(), IoError>) -> Result { - let frames = codecs::decode_gif_frames(bytes, &codecs::DecodeOptions::default())?; + import_frames(name, codecs::decode_gif_frames(bytes, &codecs::DecodeOptions::default())?, each) +} + +/// Every frame of a WebP as a single-layer document, as [`import_gif_frames`] does for a GIF: an +/// animation's frames composited onto its canvas, with their durations in milliseconds; a still +/// WebP is one frame. +pub fn import_webp_frames(name: &str, bytes: &[u8], each: &mut dyn FnMut(Document, u32) -> Result<(), IoError>) -> Result { + import_frames(name, codecs::decode_webp_frames(bytes, &codecs::DecodeOptions::default())?, each) +} + +fn import_frames(name: &str, frames: Vec, each: &mut dyn FnMut(Document, u32) -> Result<(), IoError>) -> Result { let n = frames.len(); for f in frames { each(image_to_document(name, &f.image)?.document, f.delay_ms)?; @@ -67,8 +111,9 @@ pub fn import_gif_frames(name: &str, bytes: &[u8], each: &mut dyn FnMut(Document } /// The lossy settings `bytes` were encoded with, for File › Save to keep: a JPEG's quality -/// (estimated from its quantisation tables) and a WebP's lossless or lossy encoding with its -/// quality. A lossy WebP whose quality can't be read gets [`DEFAULT_LOSSY_WEBP_QUALITY`]. +/// (estimated from its quantisation tables), a WebP's lossless or lossy encoding with its +/// quality, and a PNG's `sTER` stereo pairing. A lossy WebP whose quality can't be read gets +/// [`DEFAULT_LOSSY_WEBP_QUALITY`]. pub fn file_encoding(bytes: &[u8]) -> photocraft_doc::FileEncoding { file_encoding_as(codecs::detect(bytes), bytes) } @@ -78,6 +123,7 @@ fn file_encoding_as(format: Option, bytes: &[u8]) -> photocraft_doc::Fil let mut e = photocraft_doc::FileEncoding::default(); match format { Some(Format::Jpeg) => e.jpeg_quality = codecs::jpeg_quality(bytes), + Some(Format::Png) => e.png_stereo = codecs::png_stereo(bytes).map(codecs::PngStereo::mode), Some(Format::WebP) => match codecs::webp_encoding(bytes) { Some(codecs::WebpEncoding::Lossless) => e.webp_lossless = Some(true), Some(codecs::WebpEncoding::Lossy { quality }) => { @@ -95,8 +141,8 @@ fn file_encoding_as(format: Option, bytes: &[u8]) -> photocraft_doc::Fil pub const DEFAULT_LOSSY_WEBP_QUALITY: u8 = 75; /// Applies the settings `doc`'s file was encoded with (see [`file_encoding`]) to `opts`: what a -/// save that chose no quality writes, so a JPEG keeps its quality and a WebP stays lossy or -/// lossless. +/// save that chose no quality writes, so a JPEG keeps its quality, a WebP stays lossy or lossless, +/// and a stereo PNG keeps its `sTER` pairing (#6063). pub fn apply_file_encoding(doc: &Document, opts: &mut codecs::EncodeOptions) { let e = &doc.file_encoding; if let Some(q) = e.jpeg_quality { @@ -108,6 +154,7 @@ pub fn apply_file_encoding(doc: &Document, opts: &mut codecs::EncodeOptions) { if let Some(q) = e.webp_quality { opts.webp_quality = q.min(100); } + opts.png_stereo = e.png_stereo.and_then(codecs::PngStereo::from_mode); } /// Photoshop opens a 32-bit Targa as an opaque Background plus an "Alpha 1" channel holding the @@ -233,14 +280,20 @@ pub(crate) fn image_to_document(name: &str, img: &Image) -> Result Option<&Surface> { let [l] = &doc.layers[..] else { return None }; - let ok = l.visible + match &l.content { + LayerContent::Raster(s) if plain_layer(l, doc.mode) && s.format() == doc.pixel_format() => Some(s), + _ => None, + } +} + +/// A visible, unmasked, normal, fully opaque layer with no effects or blending options: on its +/// own, its pixels are the composite. +fn plain_layer(l: &Layer, mode: ColorMode) -> bool { + l.visible && l.opacity >= 1.0 && l.fill_opacity >= 1.0 && l.mask.is_none() @@ -289,12 +351,8 @@ pub(crate) fn single_layer(doc: &Document) -> Option<&Surface> { && l.effects.items.is_empty() && l.effects.psd_raw.is_none() && matches!(l.blend, BlendMode::Normal | BlendMode::PassThrough) - && photocraft_compose::channel_weights(l, doc.mode).is_none() - && !photocraft_compose::blend_if_active(l, doc.mode); - match (&l.content, ok) { - (LayerContent::Raster(s), true) if s.format() == doc.pixel_format() => Some(s), - _ => None, - } + && photocraft_compose::channel_weights(l, mode).is_none() + && !photocraft_compose::blend_if_active(l, mode) } pub(crate) fn layout_for(mode: ColorMode, alpha: bool) -> ChannelLayout { @@ -325,48 +383,68 @@ pub fn document_to_image(doc: &Document, warnings: &mut Vec) -> Result, cmyk_target: bool, lab_target: bool) -> Result { + let src = FlatSource::new(doc, warnings, cmyk_target, lab_target)?; let (w, h) = (doc.size.width, doc.size.height); - let canvas = doc.bounds(); - let fmt = doc.pixel_format(); - let n = (w as usize) * (h as usize); - // A Lab document keeps its pixels only for a format that stores Lab (TIFF); for the others it - // goes through the sRGB composite, which flattens nothing when there is a single plain layer. - let single = single_layer(doc); - let native = single.filter(|_| fmt.mode != ColorMode::Lab || lab_target); - let mut icc = doc.icc_profile.as_ref().map(|i| i.to_vec()); - let img = if let Some(s) = native { - // Native path: keep model and depth. The surface's encoded samples are the codec's raw - // native-endian samples, copied a band of rows at a time (alpha dropped when opaque). - let ch = fmt.channels(); - let opaque = opaque_surface(s, canvas); - let layout = layout_for(fmt.mode, !opaque); - let bps = fmt.sample.bytes(); - let keep = if opaque { (ch - 1) * bps } else { ch * bps }; - let mut data = try_buffer(n, keep)?; - let rows = (BAND_BYTES / (w as usize * ch * bps).max(1)).max(1) as i32; - let mut y = 0; - while y < h as i32 { - let y1 = y.saturating_add(rows).min(h as i32); - let band = s.to_interleaved(Rect::new(0, y, w as i32, y1)); - if opaque { - for px in band.chunks_exact(ch * bps) { - data.extend_from_slice(&px[..keep]); - } - } else { - data.extend_from_slice(&band); - } - y = y1; + // The single layer's alpha is dropped up front when it is opaque. A composite is rendered + // with alpha, dropped afterwards, in place, when every pixel turned out opaque. + let alpha = src.native.is_none_or(|s| !opaque_surface(s, doc.bounds())); + let bytes = src.sample.bytes(); + let mut data = try_buffer((w as usize) * (h as usize), src.layout(alpha).channels() * bytes)?; + let mut opaque = true; + src.bands(alpha, |_, band, translucent| { + opaque &= !translucent; + data.extend_from_slice(&band); + Ok(()) + })?; + let mut layout = src.layout(alpha); + if src.native.is_none() && opaque { + drop_alpha(&mut data, layout.color_channels() * bytes, bytes); + layout = src.layout(false); + } + Ok(Image::from_raw(w, h, layout, src.sample, data)?.with_icc(src.icc.clone()).with_meta(src.meta())) +} + +/// How a document becomes flat codec pixels, a band of rows at a time: [`document_to_image`] +/// gathers the bands into one image, a streamed PNG export ([`export_png_streamed`]) encodes each +/// band as it is rendered. +struct FlatSource<'a> { + doc: &'a Document, + /// The single plain layer, written in its own model and depth ([`single_layer`]). + native: Option<&'a Surface>, + /// The model of the pixels: the document's for the single layer, else CMYK, gray or RGB. + mode: ColorMode, + sample: CSample, + /// A CMYK composite mixed on ink values by the native CMYK compositor, so opaque pixels keep + /// their exact inks ([`photocraft_compose::cmyk::renders_inks`]). + inks: bool, + /// The CMYK profile any other CMYK composite is converted back through, for a CMYK target. + space: Option>, + icc: Option>, + /// Rows per band; 0 picks them from the size (tests ask for small bands). + rows: i32, +} + +impl<'a> FlatSource<'a> { + /// `cmyk_target` / `lab_target`: the format stores CMYK / Lab pixels. + fn new(doc: &'a Document, warnings: &mut Vec, cmyk_target: bool, lab_target: bool) -> Result { + let fmt = doc.pixel_format(); + let sample = csample(fmt.sample); + // A Lab document keeps its pixels only for a format that stores Lab (TIFF); for the others it + // goes through the sRGB composite, which flattens nothing when there is a single plain layer. + let single = single_layer(doc); + let native = single.filter(|_| fmt.mode != ColorMode::Lab || lab_target); + let mut icc = doc.icc_profile.as_ref().map(|i| i.to_vec()); + if native.is_some() { + return Ok(Self { doc, native, mode: fmt.mode, sample, inks: false, space: None, icc, rows: 0 }); } - Image::from_raw(w, h, layout, csample(fmt.sample), data)? - } else { if single.is_none() { let count = doc.layer_count(); warnings.push(format!("{count} layer(s) flattened; layers, masks and blend modes are not kept")); } - let cmyk = fmt.mode == ColorMode::Cmyk && cmyk_target; - // Only a CMYK target needs the document's profile; RGB-only targets keep the compositor's - // own handling (built-in coated CMYK when the embedded profile is unusable). - let space = if cmyk { + if fmt.mode == ColorMode::Cmyk && cmyk_target { + // A CMYK target is written as CMYK with the document's own profile (#2220), which it + // therefore needs; RGB-only targets keep the compositor's own handling (built-in + // coated CMYK when the embedded profile is unusable). use photocraft_cms::{Builtin, ColorSpace, Intent, Profile, Transform}; let profile = match doc.icc_profile.as_ref() { Some(bytes) => Profile::parse(bytes).map_err(|e| IoError::Unsupported(format!("invalid CMYK composite profile: {e}")))?, @@ -381,89 +459,141 @@ fn document_to_image_for(doc: &Document, warnings: &mut Vec, cmyk_target if icc.is_none() { icc = Some(profile.to_bytes().to_vec()); } - photocraft_compose::cmyk_space(doc) - } else { - None - }; - let gray = fmt.mode == ColorMode::Grayscale; - if (fmt.mode == ColorMode::Cmyk && !cmyk) || fmt.mode == ColorMode::Lab { - // RGB-only targets use the sRGB composite directly, without reseparating CMYK. + let inks = photocraft_compose::cmyk::renders_inks(doc); + let space = if inks { + None + } else { + // Through RGB and back: the colour model is kept, exact ink values are not. + warnings.push("CMYK composite converted back through the document's CMYK profile; ink values can differ from the layers'".to_string()); + photocraft_compose::cmyk_space(doc) + }; + return Ok(Self { doc, native: None, mode: ColorMode::Cmyk, sample, inks, space, icc, rows: 0 }); + } + if matches!(fmt.mode, ColorMode::Cmyk | ColorMode::Lab) { + // RGB-only targets use the sRGB composite directly, without separating CMYK; the + // compositor renders Lab documents in sRGB. Either way the file is tagged sRGB. warnings.push(format!("{:?} composite written as sRGB RGB (colour-managed conversion)", fmt.mode)); icc = Some(photocraft_cms::Builtin::Srgb.profile().to_bytes().to_vec()); } - let cs = csample(fmt.sample); - let colors = if cmyk { - 4 - } else if gray { - 1 - } else { - 3 + let mode = if fmt.mode == ColorMode::Grayscale { ColorMode::Grayscale } else { ColorMode::Rgb }; + Ok(Self { doc, native: None, mode, sample, inks: false, space: None, icc, rows: 0 }) + } + + fn layout(&self, alpha: bool) -> ChannelLayout { + layout_for(self.mode, alpha) + } + + /// The document's EXIF, XMP, text and resolution. + fn meta(&self) -> codecs::Metadata { + let doc = self.doc; + codecs::Metadata { + exif: doc.metadata.exif.as_ref().map(|e| e.to_vec()), + xmp: doc.metadata.xmp.as_deref().map(str::to_owned), + text: doc.metadata.text.clone(), + dpi: Some((doc.resolution_dpi, doc.resolution_dpi)), + ..Default::default() + } + } + + /// Every row of the document, top to bottom, as bands of interleaved native-endian samples + /// in [`Self::layout`]`(alpha)`: `sink(rows, samples, translucent)`, where `translucent` says + /// whether a composite band held a pixel that isn't fully opaque (its alpha is dropped all + /// the same when `alpha` is false). A single layer's band is never translucent without + /// `alpha`: drop it only when [`opaque_surface`] says so. + fn bands(&self, alpha: bool, mut sink: impl FnMut(u32, Vec, bool) -> Result<(), IoError>) -> Result<(), IoError> { + let doc = self.doc; + let (w, h) = (doc.size.width, doc.size.height); + let fmt = doc.pixel_format(); + if let Some(s) = self.native { + // Native path: keep model and depth. The surface's encoded samples are the codec's raw + // native-endian samples, copied a band of rows at a time (alpha dropped when opaque). + let ch = fmt.channels(); + let bps = fmt.sample.bytes(); + let keep = (ch - 1) * bps; + let rows = if self.rows > 0 { self.rows } else { (BAND_BYTES / (w as usize * ch * bps).max(1)).max(1) as i32 }; + let mut y = 0; + while y < h as i32 { + let y1 = y.saturating_add(rows).min(h as i32); + let mut band = s.to_interleaved(Rect::new(0, y, w as i32, y1)); + if !alpha { + compact_alpha(&mut band, keep, bps); + } + sink((y1 - y) as u32, band, alpha)?; + y = y1; + } + return Ok(()); + } + let cs = self.sample; + let put = move |out: &mut Vec, v: f32| { + let v = if v.is_nan() { 0.0 } else { v }; + match cs { + CSample::U8 => out.push((v.clamp(0.0, 1.0) * 255.0).round() as u8), + CSample::U16 => out.extend_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_ne_bytes()), + CSample::F16 | CSample::F32 => out.extend_from_slice(&v.to_ne_bytes()), + } }; - // Rendered and quantised in bands (no full-size float composite), with alpha; the alpha - // is dropped afterwards, in place, when every pixel turned out opaque. - let mut data = try_buffer(n, (colors + 1) * cs.bytes())?; - let mut opaque = true; - let _ = photocraft_compose::render_bands(doc, canvas, 0, |band| -> Result<(), ()> { - opaque &= band.px.iter().all(|p| p[3] >= 1.0); + let colors = self.layout(false).channels(); + // Bands of quantised samples with alpha; the alpha is dropped here when not wanted. + let mut emit = |rows: u32, parts: Vec>, translucent: bool| -> Result<(), IoError> { + let mut data = Vec::with_capacity(parts.iter().map(Vec::len).sum()); + for part in parts { + data.extend_from_slice(&part); + } + if !alpha { + compact_alpha(&mut data, colors * cs.bytes(), cs.bytes()); + } + sink(rows, data, translucent) + }; + if self.inks { + // Mixed on ink values, rendered and quantised in bands. + let rendered = photocraft_compose::cmyk::render_cmyk_bands(doc, doc.bounds(), self.rows, |band| -> Result<(), IoError> { + let translucent = band.px.iter().any(|p| p[4] < 1.0 || p[4].is_nan()); + let parts = crate::pixels::par_map(crate::pixels::bands(band.px.len()), |range| { + let mut out = Vec::with_capacity(range.len() * 5 * cs.bytes()); + for p in band.px.get(range).unwrap_or_default() { + for v in p { + put(&mut out, *v); + } + } + out + }); + emit(band.rect.height(), parts, translucent) + }); + return rendered.unwrap_or_else(|| Err(IoError::Unsupported("the CMYK compositor can't mix this document on its inks".into()))); + } + let cmyk = self.mode == ColorMode::Cmyk; + let gray = self.mode == ColorMode::Grayscale; + let space = &self.space; + // Rendered and quantised in bands (no full-size float composite), with alpha. + photocraft_compose::render_bands(doc, doc.bounds(), self.rows, |band| -> Result<(), IoError> { + let translucent = band.px.iter().any(|p| p[3] < 1.0 || p[3].is_nan()); let parts = crate::pixels::par_map(crate::pixels::bands(band.px.len()), |range| { + // A CMYK composite came through the document's CMYK profile: convert back through it. photocraft_color::convert::with_cmyk_space(space.as_ref(), || { let mut out = Vec::with_capacity(range.len() * (colors + 1) * cs.bytes()); - let mut put = |v: f32| { - let v = if v.is_nan() { 0.0 } else { v }; - match cs { - CSample::U8 => out.push((v.clamp(0.0, 1.0) * 255.0).round() as u8), - CSample::U16 => out.extend_from_slice(&((v.clamp(0.0, 1.0) * 65535.0).round() as u16).to_ne_bytes()), - CSample::F16 | CSample::F32 => out.extend_from_slice(&v.to_ne_bytes()), - } - }; let mut converted = [0.0; 5]; - for p in &band.px[range] { + for p in band.px.get(range).unwrap_or_default() { if cmyk { let channels = photocraft_raster::from_rgba_into(&fmt, *p, &mut converted); for value in converted.iter().take(channels) { - put(*value); + put(&mut out, *value); } continue; } else if gray { - put(photocraft_color::convert::rgb_to_gray([p[0], p[1], p[2]])); + put(&mut out, photocraft_color::convert::rgb_to_gray([p[0], p[1], p[2]])); } else { - put(p[0]); - put(p[1]); - put(p[2]); + put(&mut out, p[0]); + put(&mut out, p[1]); + put(&mut out, p[2]); } - put(p[3]); + put(&mut out, p[3]); } out }) }); - for part in parts { - data.extend_from_slice(&part); - } - Ok(()) - }); - if opaque { - drop_alpha(&mut data, colors * cs.bytes(), cs.bytes()); - } - let layout = layout_for( - if cmyk { - ColorMode::Cmyk - } else if gray { - ColorMode::Grayscale - } else { - ColorMode::Rgb - }, - !opaque, - ); - Image::from_raw(w, h, layout, cs, data)? - }; - let meta = codecs::Metadata { - exif: doc.metadata.exif.as_ref().map(|e| e.to_vec()), - xmp: doc.metadata.xmp.clone(), - text: doc.metadata.text.clone(), - dpi: Some((doc.resolution_dpi, doc.resolution_dpi)), - ..Default::default() - }; - Ok(img.with_icc(icc).with_meta(meta)) + emit(band.rect.height(), parts, translucent) + }) + } } /// An empty buffer with room for `pixels × bytes_per_pixel` bytes, or an error (not an abort) @@ -475,19 +605,25 @@ pub(crate) fn try_buffer(pixels: usize, bytes_per_pixel: usize) -> Result, color: usize, alpha: usize) { + compact_alpha(data, color, alpha); + data.shrink_to_fit(); +} + +/// [`drop_alpha`] keeping the allocation (a band about to be handed on). +fn compact_alpha(data: &mut Vec, color: usize, alpha: usize) { let stride = color + alpha; let n = data.len() / stride; for i in 0..n { data.copy_within(i * stride..i * stride + color, i * color); } data.truncate(n * color); - data.shrink_to_fit(); } /// Whether every pixel of `s` over `r` is fully opaque (read from the tiles, no copy). -fn opaque_surface(s: &Surface, r: Rect) -> bool { +pub(crate) fn opaque_surface(s: &Surface, r: Rect) -> bool { let fmt = s.format(); let (ch, bps) = (fmt.channels(), fmt.sample.bytes()); let a = ch - 1; @@ -525,23 +661,47 @@ pub fn export_flat(doc: &Document, format: Format, opts: &ExportOptions) -> Resu /// [`export_flat`] reporting a coarse step through `ctl` once the composite is rendered; the /// encoder does the rest (#3389). pub(crate) fn export_flat_with(doc: &Document, format: Format, opts: &ExportOptions, ctl: &photocraft_raster::Interrupt) -> Result { - use photocraft_cms::{Builtin, Intent}; if let Some(r) = export_mode_specific(doc, format, opts)? { return Ok(r); } + if format == Format::Png && streams_png(doc) { + return export_png_streamed(doc, opts, ctl, 0); + } let mut warnings = Vec::new(); let cmyk_target = format.caps().layouts.iter().any(|layout| layout.is_cmyk()); let lab_target = format.caps().layouts.iter().any(|layout| layout.is_lab()); let mut img = document_to_image_for(doc, &mut warnings, cmyk_target, lab_target)?; ctl.check().map_err(|_| IoError::Cancelled)?; ctl.progress(composite_share(format)); - if opts.xmp == XmpEmbed::None { - // Export As's Metadata: None: the packet lists the text of every type layer and one id - // per placed document (#647). - img.meta.xmp = None; - // Free-form descriptions can contain the same sensitive text as an XMP packet. - img.meta.text.clear(); + strip_xmp(&mut img.meta, opts); + let mut img = prepare_flat(img, format, &mut warnings)?; + if format == Format::Tga { + img = tga_image(doc, img, opts.tga_bits, &mut warnings)?; } + if format == Format::Dds { + img = dds_image(doc, img, &mut warnings)?; + } + if format == Format::Tiff { + warnings.extend(crate::tiff_layers::stamp_flat_channels(doc, &mut img)); + } + encode_image(&img, format, opts, warnings) +} + +/// Export As's Metadata: None drops the XMP packet, which lists the text of every type layer and +/// one id per placed document (#647), and the free-form descriptions, which can hold the same +/// sensitive text. +fn strip_xmp(meta: &mut codecs::Metadata, opts: &ExportOptions) { + if opts.xmp == XmpEmbed::None { + meta.xmp = None; + meta.text.clear(); + } +} + +/// `img` made storable as `format`: matted over white without alpha support, CMYK converted for a +/// format without CMYK, colours converted to what an untagged file means. Pixel by pixel, so a +/// band of rows gets what the whole image would (and the same warnings). +fn prepare_flat(mut img: Image, format: Format, warnings: &mut Vec) -> Result { + use photocraft_cms::{Builtin, Intent}; if img.layout().has_alpha() && !format.caps().alpha { // Flattened over white, as saving a transparent document without transparency does. img = matte_over_white(&img)?; @@ -570,11 +730,132 @@ pub(crate) fn export_flat_with(doc: &Document, format: Format, opts: &ExportOpti img = gray; warnings.push(format!("colours converted to sGray; {format:?} can't embed the document's colour profile")); } + } else if img.icc.is_none() { + // An untagged document is in the default working space for its mode, which is how the + // canvas shows it and what Photoshop embeds for it (#9325): sRGB, and for gray the Dot + // Gain 20 %-compatible working gray (not sGray, which would show the same samples + // darker). A JPEG or PNG with Embed Color Profile then says so instead of leaving + // viewers to guess. The codec still leaves it out when embedding is off. + if img.layout().is_rgb() { + img.icc = Some(Builtin::Srgb.profile().to_bytes().to_vec()); + } else if img.layout().is_gray() { + img.icc = Some(Builtin::DotGain20Compat.profile().to_bytes().to_vec()); + } } - if format == Format::Tga { - img = tga_image(doc, img, opts.tga_bits, &mut warnings)?; + Ok(img) +} + +/// Decoded pixel bytes past which a PNG export is written a band of rows at a time +/// ([`export_png_streamed`]) instead of from one image buffer (about 90 MP of 8-bit RGB). +const STREAM_PNG_BYTES: u64 = 256 << 20; + +/// Whether a PNG export of `doc` streams: its pixels would take more than [`STREAM_PNG_BYTES`]. +fn streams_png(doc: &Document) -> bool { + let fmt = doc.pixel_format(); + doc.size.area().saturating_mul((fmt.channels() * fmt.sample.bytes()) as u64) > STREAM_PNG_BYTES +} + +/// A PNG export rendered, converted and encoded a band of rows at a time, so no full-size image +/// is held: the only way a multi-gigapixel print (a 117504 × 22464 px banner is 7.9 GB of 8-bit +/// RGB) can be written as PNG (#8456). The pixels are the ones [`export_flat`] writes for a +/// smaller document, with these differences: Interlaced is turned off (Adam7 needs the whole +/// image), and a composite whose bottom layer isn't an opaque pixel layer keeps its alpha even +/// when every pixel turns out opaque (that is only known once the last row is written). +/// +/// `rows`: rows per band, 0 to size them to the document. +fn export_png_streamed(doc: &Document, opts: &ExportOptions, ctl: &photocraft_raster::Interrupt, rows: i32) -> Result { + // A wrong guess (a knockout or a blend mode the guess doesn't foresee) shows in a band and + // the export starts again with alpha. + let opaque = match single_layer(doc) { + Some(s) => opaque_surface(s, doc.bounds()), + None => opaque_bottom(doc), + }; + // Progress never goes back: a second pass reports nothing new until it passes the first. + let mut progress = 0.0; + match stream_png(doc, opts, ctl, !opaque, rows, &mut progress)? { + Some(r) => Ok(r), + None => stream_png(doc, opts, ctl, true, rows, &mut progress)?.ok_or_else(|| IoError::Unsupported("PNG export lost its alpha channel".into())), } - encode_image(&img, format, opts, warnings) +} + +/// Whether the bottom layer is a plain pixel layer opaque over the whole canvas (a Background +/// layer): the layers above it add coverage, so the composite is (most likely) opaque too. +fn opaque_bottom(doc: &Document) -> bool { + doc.layers.first().is_some_and(|l| plain_layer(l, doc.mode) && matches!(&l.content, LayerContent::Raster(s) if opaque_surface(s, doc.bounds()))) +} + +/// [`export_png_streamed`] with or without `alpha`, in bands of `rows` rows (0: sized to the +/// document). `None` when a translucent pixel shows up without `alpha`. `progress` is the most +/// reported so far, by this pass or an earlier one. +fn stream_png( + doc: &Document, + opts: &ExportOptions, + ctl: &photocraft_raster::Interrupt, + alpha: bool, + rows: i32, + progress: &mut f32, +) -> Result, IoError> { + let format = Format::Png; + let (w, h) = (doc.size.width, doc.size.height); + let mut warnings = Vec::new(); + let cmyk_target = format.caps().layouts.iter().any(|layout| layout.is_cmyk()); + let lab_target = format.caps().layouts.iter().any(|layout| layout.is_lab()); + let src = FlatSource { rows, ..FlatSource::new(doc, &mut warnings, cmyk_target, lab_target)? }; + let mut encode = opts.encode.clone(); + let mut late = Vec::new(); + if encode.png_interlaced { + encode.png_interlaced = false; + late.push(format!("written without interlacing: a {w} × {h} px PNG is written a band of rows at a time")); + } + // One blank row through the same steps as the bands gives the file's layout, profile and + // metadata (and their warnings) before anything is rendered. + let mut meta = src.meta(); + strip_xmp(&mut meta, opts); + let row = try_buffer(w as usize, src.layout(alpha).channels() * src.sample.bytes()).map(|mut v| { + v.resize(v.capacity(), 0); + v + })?; + let first = prepare_flat(Image::from_raw(w, 1, src.layout(alpha), src.sample, row)?.with_icc(src.icc.clone()).with_meta(meta), format, &mut warnings)?; + let mut fidelity: Vec = Vec::new(); + let mut note = |img: &Image| -> Result<(), IoError> { + for f in codecs::fidelity_warnings_with(img, format, &encode) { + if f.is_fatal() { + return Err(IoError::Unsupported(f.to_string())); + } + let f = f.to_string(); + if !fidelity.contains(&f) { + fidelity.push(f); + } + } + Ok(()) + }; + note(&first)?; + let mut bytes = Vec::new(); + let mut png = codecs::PngRows::new(&mut bytes, &first, h, &encode)?; + let (mut done, mut translucent) = (0u64, false); + let streamed = src.bands(alpha, |rows, data, see_through| { + if see_through && !alpha { + translucent = true; + return Err(IoError::Cancelled); + } + ctl.check().map_err(|_| IoError::Cancelled)?; + let mut band_warnings = Vec::new(); + let band = prepare_flat(Image::from_raw(w, rows, src.layout(alpha), src.sample, data)?.with_icc(src.icc.clone()), format, &mut band_warnings)?; + note(&band)?; + png.push(&band)?; + done += u64::from(rows); + *progress = progress.max(done as f32 / h as f32); + ctl.progress(*progress); + Ok(()) + }); + if translucent { + return Ok(None); + } + streamed?; + png.finish()?; + warnings.extend(fidelity); + warnings.extend(late); + Ok(Some(ExportResult { bytes, warnings })) } /// What Targa Options offers first, as Photoshop does: 32 bits per pixel when the document has an @@ -612,7 +893,7 @@ fn tga_image(doc: &Document, img: Image, bits: Option, warnings: &mut V warnings.push("transparency composited over white; a 24-bit Targa has no alpha".into()); matte_over_white(&img) } - (TgaBits::Bits32, [channel]) => tga_with_alpha_channel(doc, img, channel, warnings), + (TgaBits::Bits32, [channel]) => with_alpha_channel(doc, img, channel, "Targa", warnings), (TgaBits::Bits32, alphas) => { if !alphas.is_empty() { warnings.push("Targa holds one alpha channel; with several, none was written (as in Photoshop)".into()); @@ -625,11 +906,32 @@ fn tga_image(doc: &Document, img: Image, bits: Option, warnings: &mut V } } -/// A 32-bit Targa's image: the colours (composited over white where the layers are transparent) -/// and `channel`'s values as the alpha. -fn tga_with_alpha_channel(doc: &Document, img: Image, channel: &AlphaChannel, warnings: &mut Vec) -> Result { +/// The image a DDS texture holds (#8563): as with a 32-bit Targa, the document's alpha channel +/// becomes the texture's alpha when it has exactly one (spot channels don't count), the colours +/// composited over white where the layers are transparent; BC1 keeps it as on/off, BC2 and BC3 +/// in full. With none, the layers' transparency is the alpha as before; with several, none of +/// them is written. Grayscale textures keep their transparency. +fn dds_image(doc: &Document, img: Image, warnings: &mut Vec) -> Result { + let alphas: Vec<&AlphaChannel> = doc.channels.iter().filter(|c| c.spot.is_none()).collect(); + match alphas.as_slice() { + [] => Ok(img), + [channel] if img.layout().is_rgb() => with_alpha_channel(doc, img, channel, "texture", warnings), + [_] => { + warnings.push("a grayscale DDS keeps the layers' transparency; the alpha channel was not saved".into()); + Ok(img) + } + _ => { + warnings.push("a DDS texture holds one alpha channel; with several, none was written".into()); + Ok(img) + } + } +} + +/// A 32-bit Targa's or a texture's image: the colours (composited over white where the layers are +/// transparent) and `channel`'s values as the alpha. `what` names the file in the warning. +fn with_alpha_channel(doc: &Document, img: Image, channel: &AlphaChannel, what: &str, warnings: &mut Vec) -> Result { let img = if img.layout().has_alpha() { - warnings.push("transparency composited over white; the alpha channel is the Targa's alpha".into()); + warnings.push(format!("transparency composited over white; the alpha channel is the {what}'s alpha")); matte_over_white(&img)? } else { img @@ -805,22 +1107,41 @@ fn cmyk_image_to_srgb(img: &Image) -> Result { Ok(Image::from_normalized(w, h, layout, sample, &out)?.with_icc(Some(dst.to_bytes().to_vec())).with_meta(img.meta.clone())) } -/// Indexed Color → PNG-8 with its colour table; Duotone → the inks rendered as RGB. +/// Indexed Color → PNG-8 or an 8-bit BMP with its colour table; Duotone → the inks rendered as RGB. fn export_mode_specific(doc: &Document, format: Format, opts: &ExportOptions) -> Result, IoError> { match doc.mode { - ColorMode::Indexed if format == Format::Png => { + ColorMode::Indexed if matches!(format, Format::Png | Format::Bmp) => { let Some(table) = doc.color_table.as_ref().filter(|t| !t.colors.is_empty() && t.colors.len() <= 256) else { return Ok(None) }; + // PNG transparency belongs to a palette index, not its RGB value. Keep the + // transparent entry out of opaque lookup, even when it duplicates an opaque colour. + let png_transparent_index = table.transparent.map(usize::from).filter(|_| format == Format::Png); + let (opaque_indices, colors): (Vec, Vec<_>) = + table.colors.iter().copied().enumerate().filter(|(i, _)| Some(*i) != png_transparent_index).map(|(i, c)| (i as u8, c)).unzip(); + let opaque = photocraft_doc::ColorTable { colors, transparent: None }; let mut idx = try_buffer(doc.size.area() as usize, 1)?; - let _ = photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> { - idx.extend(band.px.iter().map(|p| match table.transparent { - Some(t) if p[3] < 0.5 => t, - _ => table.nearest([p[0], p[1], p[2]]) as u8, - })); + photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), IoError> { + for p in &band.px { + let i = match table.transparent { + Some(t) if p[3] < 0.5 => t, + _ => *opaque_indices + .get(opaque.nearest([p[0], p[1], p[2]])) + .ok_or_else(|| IoError::Unsupported("Indexed palette has no opaque entry; add an opaque colour or convert to RGB".into()))?, + }; + idx.push(i); + } Ok(()) - }); + })?; + if format == Format::Bmp { + let bytes = codecs::encode_bmp_indexed(doc.size.width, doc.size.height, &idx, &table.colors)?; + let mut warnings = vec![format!("written as an 8-bit palette BMP ({} colours)", table.colors.len())]; + if let Some(t) = table.transparent { + warnings.push(format!("BMP has no transparency; transparent pixels are written in the colour of palette entry {t}")); + } + return Ok(Some(ExportResult { bytes, warnings })); + } // Same rule as the ordinary PNG encoder: the resolution goes into `pHYs` with the metadata. let dpi = opts.encode.embed_metadata.then_some((doc.resolution_dpi, doc.resolution_dpi)); - let bytes = codecs::encode_png_indexed(doc.size.width, doc.size.height, &idx, &table.colors, table.transparent, dpi)?; + let bytes = codecs::encode_png_indexed(doc.size.width, doc.size.height, &idx, &table.colors, table.transparent, dpi, opts.encode.png_stereo)?; let mut warnings = vec![format!("written as an 8-bit palette PNG ({} colours)", table.colors.len())]; if opts.encode.embed_metadata && opts.xmp == XmpEmbed::All && !doc.metadata.text.is_empty() { warnings.push("text metadata is not supported by the palette PNG exporter; it will be dropped".into()); @@ -843,6 +1164,59 @@ fn export_mode_specific(doc: &Document, format: Format, opts: &ExportOptions) -> } } +#[cfg(test)] +mod cicp_tests { + use super::*; + use photocraft_cms::{Builtin, Profile}; + + fn tagged(sample: CSample, cicp: codecs::Cicp) -> Image { + let mut img = Image::new(1, 1, ChannelLayout::Rgb, sample).unwrap(); + img.cicp = Some(cicp); + img + } + + fn description(p: Option>>) -> Option { + p.map(|b| Profile::parse(&b).unwrap().description) + } + + #[test] + fn hlg_bt2020_is_tagged_linear_bt2020() { + let hlg = codecs::Cicp { primaries: 9, transfer: codecs::Cicp::TRANSFER_HLG, matrix: 9, full_range: true }; + let d = description(cicp_profile(&tagged(CSample::F32, hlg))).unwrap(); + assert!(d.contains("BT.2020") && d.contains("linear"), "{d}"); + // Unspecified primaries on HLG are BT.2100's. + let d = description(cicp_profile(&tagged(CSample::F32, codecs::Cicp { primaries: 2, ..hlg }))).unwrap(); + assert!(d.contains("BT.2020"), "{d}"); + } + + #[test] + fn sdr_nclx_is_unchanged_or_tagged_with_its_builtin() { + // BT.709 / sRGB: untagged, as before. + for transfer in [1, 2, 13] { + assert_eq!(cicp_profile(&tagged(CSample::U16, codecs::Cicp { primaries: 1, transfer, matrix: 6, full_range: false })), None); + } + // BT.709 with a linear transfer: linear sRGB, not read as sRGB. + let linear = codecs::Cicp { primaries: 1, transfer: 8, matrix: 6, full_range: true }; + assert_eq!(description(cicp_profile(&tagged(CSample::U16, linear))).as_deref(), Some(Builtin::LinearSrgb.description())); + let p3 = codecs::Cicp { primaries: 12, transfer: 13, matrix: 6, full_range: true }; + assert_eq!(description(cicp_profile(&tagged(CSample::U8, p3))).as_deref(), Some(Builtin::DisplayP3.description())); + assert_eq!(cicp_profile(&Image::new(1, 1, ChannelLayout::Rgb, CSample::U8).unwrap()), None, "no nclx, no profile"); + } + + #[test] + fn hdr_on_unsupported_primaries_is_tagged_linear_srgb_with_a_warning() { + // DCI-P3 (11) primaries: no profile, but the linear pixels must not be read as sRGB. + let hlg = codecs::Cicp { primaries: 11, transfer: codecs::Cicp::TRANSFER_HLG, matrix: 9, full_range: true }; + let img = tagged(CSample::F32, hlg); + assert_eq!(cicp_profile(&img), None); + let (profile, warning) = hdr_fallback(&img).unwrap(); + assert_eq!(description(Some(profile)).as_deref(), Some(Builtin::LinearSrgb.description())); + assert!(warning.contains("11"), "{warning}"); + // SDR on the same primaries keeps its samples as decoded: no fallback. + assert!(hdr_fallback(&tagged(CSample::U16, codecs::Cicp { transfer: 13, ..hlg })).is_none()); + } +} + #[cfg(test)] mod cmyk_band_tests { use super::*; @@ -871,3 +1245,217 @@ mod cmyk_band_tests { assert_eq!(first[3], 255); } } + +#[cfg(test)] +mod assumed_profile_tests { + use super::*; + use photocraft_cms::{Builtin, Profile}; + use photocraft_color::Color; + + fn doc(mode: ColorMode) -> Document { + Document::with_background("t", Size::new(16, 12), mode, SampleType::U8, Color::rgba(0.78, 0.39, 0.2, 1.0)) + } + + fn embedded(r: &ExportResult) -> Option { + codecs::decode(&r.bytes).unwrap().icc.map(|b| Profile::parse(&b).unwrap().description) + } + + #[test] + fn streamed_png_keeps_the_same_assumed_profile_and_pixels() { + for mode in [ColorMode::Rgb, ColorMode::Grayscale] { + for embed in [false, true] { + let d = doc(mode); + let mut opts = ExportOptions::default(); + opts.encode.embed_icc = embed; + let whole = export_flat(&d, Format::Png, &opts).unwrap(); + let streamed = export_png_streamed(&d, &opts, &photocraft_raster::Interrupt::NONE, 3).unwrap(); + assert_eq!(embedded(&streamed), embedded(&whole)); + let expected = if mode == ColorMode::Rgb { Builtin::Srgb } else { Builtin::DotGain20Compat }; + assert_eq!(embedded(&streamed), embed.then(|| expected.description().to_string())); + let a = codecs::decode(&whole.bytes).unwrap(); + let b = codecs::decode(&streamed.bytes).unwrap(); + assert_eq!((a.dimensions(), a.layout(), a.sample_type()), (b.dimensions(), b.layout(), b.sample_type())); + assert_eq!(a.data(), b.data()); + } + } + } + + /// #9325: an untagged document is in the profile assumed for its mode, and Photoshop embeds + /// that (sRGB IEC61966-2.1 for a new or untagged RGB document) when the profile is embedded. + #[test] + fn untagged_documents_embed_the_profile_they_are_assumed_to_be_in() { + let opts = ExportOptions::default(); + assert!(opts.encode.embed_icc); + for ext in ["jpg", "png", "tif", "webp"] { + let r = crate::export(&doc(ColorMode::Rgb), ext, &opts).unwrap(); + assert_eq!(embedded(&r).as_deref(), Some(Builtin::Srgb.description()), "{ext}"); + } + // #9342 review: an untagged gray document shows through the default working gray (Dot + // Gain 20 %-compatible), so that is the profile its samples are tagged with, not sGray + // (which would show them darker than the canvas). + let r = crate::export(&doc(ColorMode::Grayscale), "png", &opts).unwrap(); + assert_eq!(embedded(&r).as_deref(), Some(Builtin::DotGain20Compat.description())); + // Embed Color Profile off: still none. + let mut off = ExportOptions::default(); + off.encode.embed_icc = false; + assert_eq!(embedded(&crate::export(&doc(ColorMode::Rgb), "jpg", &off).unwrap()), None); + // A format without profiles gets no warning about one. + let r = crate::export(&doc(ColorMode::Rgb), "bmp", &opts).unwrap(); + assert!(!r.warnings.iter().any(|w| w.contains("profile")), "{:?}", r.warnings); + // A tagged document keeps its own profile. + let mut p3 = doc(ColorMode::Rgb); + p3.icc_profile = Some(Arc::new(Builtin::DisplayP3.profile().to_bytes().to_vec())); + assert_eq!(embedded(&crate::export(&p3, "jpg", &opts).unwrap()).as_deref(), Some(Builtin::DisplayP3.description())); + } +} + +#[cfg(test)] +mod streamed_png_tests { + use super::*; + use photocraft_raster::Interrupt; + + fn layer(doc: &Document, fills: &[(Rect, &[f32])]) -> Layer { + let mut s = Surface::new(doc.pixel_format()); + for (r, px) in fills { + s.fill_rect(*r, px); + } + Layer::new("Layer", LayerContent::Raster(s)) + } + + fn decoded(bytes: &[u8]) -> Image { + codecs::decode(bytes).unwrap() + } + + /// The bytes ahead of the first IDAT: signature, header, profile and metadata chunks. + fn head(png: &[u8]) -> &[u8] { + &png[..png.windows(4).position(|w| w == b"IDAT").unwrap() - 4] + } + + /// Streamed in bands of `rows`, `doc` gives the pixels, warnings and header chunks of the + /// whole-image export. + fn same_as_whole(doc: &Document, rows: i32, opts: &ExportOptions) -> ExportResult { + let whole = export_flat(doc, Format::Png, opts).unwrap(); + let streamed = export_png_streamed(doc, opts, &Interrupt::NONE, rows).unwrap(); + let (a, b) = (decoded(&whole.bytes), decoded(&streamed.bytes)); + assert_eq!((b.dimensions(), b.layout(), b.sample_type()), (a.dimensions(), a.layout(), a.sample_type())); + assert!(a.data() == b.data(), "{}: pixels differ", doc.name); + assert_eq!(streamed.warnings, whole.warnings); + assert!(head(&streamed.bytes) == head(&whole.bytes), "{}: header chunks differ", doc.name); + streamed + } + + #[test] + fn a_composite_over_an_opaque_bottom_layer_streams_as_rgb() { + let mut doc = Document::new("composite", Size::new(300, 700), ColorMode::Rgb, SampleType::U8); + doc.resolution_dpi = 300.0; + doc.metadata.xmp = Some("".into()); + doc.metadata.text = vec![("Title".into(), "Tent".into())]; + let bottom = layer(&doc, &[(doc.bounds(), &[0.9, 0.5, 0.1, 1.0]), (Rect::new(0, 300, 300, 420), &[0.1, 0.2, 0.8, 1.0])]); + let mut top = layer(&doc, &[(Rect::new(40, 100, 260, 650), &[0.2, 0.7, 0.3, 0.5])]); + top.opacity = 0.7; + top.blend = BlendMode::Multiply; + doc.layers = vec![bottom, top]; + assert!(opaque_bottom(&doc)); + for xmp in [XmpEmbed::All, XmpEmbed::None] { + let r = same_as_whole(&doc, 256, &ExportOptions { xmp, ..Default::default() }); + assert_eq!(decoded(&r.bytes).layout(), ChannelLayout::Rgb); + } + } + + #[test] + fn single_layers_stream_in_their_own_model_and_depth() { + // Transparent RGBA, opaque CMYK (written as sRGB through its profile), 16-bit gray and + // float RGB out of range (written with 16 bits, clipped, with the warning). + let mut rgba = Document::new("rgba", Size::new(130, 333), ColorMode::Rgb, SampleType::U8); + rgba.layers = vec![layer(&rgba, &[(Rect::new(10, 20, 120, 300), &[0.3, 0.6, 0.9, 0.4])])]; + let mut cmyk = Document::new("cmyk", Size::new(97, 211), ColorMode::Cmyk, SampleType::U8); + cmyk.icc_profile = Some(photocraft_cms::Builtin::CoatedCmyk.profile().to_bytes()); + cmyk.layers = vec![layer(&cmyk, &[(cmyk.bounds(), &[0.3, 0.6, 0.1, 0.2, 1.0]), (Rect::new(0, 50, 97, 99), &[0.0, 0.9, 0.9, 0.0, 1.0])])]; + let mut gray = Document::new("gray", Size::new(64, 190), ColorMode::Grayscale, SampleType::U16); + gray.layers = vec![layer(&gray, &[(gray.bounds(), &[0.25, 1.0]), (Rect::new(0, 0, 64, 77), &[0.6123, 1.0])])]; + let mut hdr = Document::new("hdr", Size::new(50, 120), ColorMode::Rgb, SampleType::F32); + hdr.layers = vec![layer(&hdr, &[(hdr.bounds(), &[1.5, 0.25, -0.1, 1.0])])]; + for doc in [&rgba, &cmyk, &gray, &hdr] { + assert!(single_layer(doc).is_some(), "{}", doc.name); + same_as_whole(doc, 37, &ExportOptions::default()); + } + let r = same_as_whole(&hdr, 37, &ExportOptions::default()); + assert!(r.warnings.iter().any(|w| w.contains("clip")), "{:?}", r.warnings); + } + + /// Without an opaque bottom layer the composite keeps its alpha, even when every pixel is + /// opaque (the whole-image export finds that out and drops it). + #[test] + fn a_composite_without_an_opaque_bottom_layer_keeps_its_alpha() { + let mut doc = Document::new("masked", Size::new(80, 300), ColorMode::Rgb, SampleType::U8); + let mut bottom = layer(&doc, &[(doc.bounds(), &[0.2, 0.4, 0.6, 1.0])]); + bottom.mask = Some(photocraft_doc::LayerMask { + surface: Surface::with_default(PixelFormat::GRAY8, &[1.0]), + enabled: true, + linked: true, + density: 1.0, + feather: 0.0, + }); + doc.layers = vec![bottom, layer(&doc, &[(Rect::new(0, 0, 40, 40), &[1.0, 0.0, 0.0, 1.0])])]; + assert!(!opaque_bottom(&doc)); + let whole = decoded(&export_flat(&doc, Format::Png, &ExportOptions::default()).unwrap().bytes); + let streamed = decoded(&export_png_streamed(&doc, &ExportOptions::default(), &Interrupt::NONE, 256).unwrap().bytes); + assert_eq!((whole.layout(), streamed.layout()), (ChannelLayout::Rgb, ChannelLayout::Rgba)); + assert!(whole.convert(ChannelLayout::Rgba, CSample::U8).data() == streamed.data()); + } + + /// A knockout through an opaque bottom layer makes the guess wrong: the export starts again + /// with alpha and keeps the hole, and its progress never goes back. + #[test] + fn a_wrong_opacity_guess_starts_again_with_alpha() { + // The hole is in the fourth band: three are written before the guess turns out wrong. + let mut doc = Document::new("knockout", Size::new(64, 1000), ColorMode::Rgb, SampleType::U8); + let bottom = layer(&doc, &[(doc.bounds(), &[0.2, 0.4, 0.6, 1.0])]); + let mut hole = layer(&doc, &[(Rect::new(8, 800, 56, 960), &[1.0, 1.0, 1.0, 1.0])]); + hole.fill_opacity = 0.0; + hole.advanced.knockout = photocraft_doc::Knockout::Deep; + doc.layers = vec![bottom, hole]; + assert!(opaque_bottom(&doc)); + assert!(stream_png(&doc, &ExportOptions::default(), &Interrupt::NONE, false, 256, &mut 0.0).unwrap().is_none()); + let r = same_as_whole(&doc, 256, &ExportOptions::default()); + assert_eq!(decoded(&r.bytes).layout(), ChannelLayout::Rgba); + + let seen = std::sync::Mutex::new(Vec::new()); + let report = |f: f32| seen.lock().unwrap().push(f); + let never = || false; + export_png_streamed(&doc, &ExportOptions::default(), &Interrupt::new(&never, &report), 256).unwrap(); + let seen = seen.into_inner().unwrap(); + assert!(seen.windows(2).all(|w| w[0] <= w[1]), "progress went back: {seen:?}"); + assert_eq!(seen.last(), Some(&1.0)); + } + + #[test] + fn interlacing_is_turned_off_with_a_warning() { + let mut doc = Document::new("interlaced", Size::new(40, 30), ColorMode::Rgb, SampleType::U8); + doc.layers = vec![layer(&doc, &[(doc.bounds(), &[0.5, 0.5, 0.5, 1.0])])]; + let mut opts = ExportOptions::default(); + opts.encode.png_interlaced = true; + let r = export_png_streamed(&doc, &opts, &Interrupt::NONE, 7).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("without interlacing")), "{:?}", r.warnings); + let whole = decoded(&export_flat(&doc, Format::Png, &opts).unwrap().bytes); + assert!(decoded(&r.bytes).data() == whole.data()); + } + + #[test] + fn a_cancelled_stream_stops_with_cancelled() { + let mut doc = Document::new("cancel", Size::new(40, 30), ColorMode::Rgb, SampleType::U8); + doc.layers = vec![layer(&doc, &[(doc.bounds(), &[0.5, 0.5, 0.5, 1.0])])]; + let yes = || true; + let r = export_png_streamed(&doc, &ExportOptions::default(), &Interrupt::cancel_only(&yes), 7); + assert!(matches!(r, Err(IoError::Cancelled)), "{:?}", r.map(|_| ())); + } + + #[test] + fn only_large_documents_stream() { + let doc = |w, h, depth| Document::new("size", Size::new(w, h), ColorMode::Rgb, depth); + assert!(!streams_png(&doc(6000, 4000, SampleType::U8))); + assert!(streams_png(&doc(10_000, 8000, SampleType::U8))); + assert!(streams_png(&doc(8000, 6000, SampleType::U16))); + assert!(streams_png(&doc(117_504, 22_464, SampleType::U8))); + } +} diff --git a/crates/io/src/gradient_bake.rs b/crates/io/src/gradient_bake.rs index 0d34816a79..6c7aaf32f4 100644 --- a/crates/io/src/gradient_bake.rs +++ b/crates/io/src/gradient_bake.rs @@ -185,10 +185,116 @@ pub fn bake(stops: Vec<(f32, Color)>, midpoints: &[f32], smoothness: f32, method out } +/// Exponent of Photoshop's opacity response for Perceptual and Linear gradients: the opacity +/// stops enter the method's space as if they were sRGB grey levels (Linear: linear light; +/// Perceptual: Oklab L), and the interpolated linear-light value `y` comes out as opacity +/// `y^0.65`. Fitted on Photoshop (scripted Gradient Overlays and gradient fill layers, which +/// render identical pixels: 100 → 0 % and 100 → 20 → 90 % ramps with Smoothness 0 and 100 and +/// midpoints 25 and 50 %, within 0.015 of every 8-bit sample; a flat 60 % renders as 47 %). +const OPACITY_RESPONSE: f32 = 0.65; + +/// Dense linear opacity stops reproducing Photoshop's interpolation of a gradient's transparency +/// (Smoothness, opacity midpoints and the method, see [`OPACITY_RESPONSE`]). Returns the input +/// unchanged when it already interpolates linearly. Smooth keeps Classic's opacity curve: it was +/// not measured (the Photoshop build it was fitted on has no Smooth method). +pub fn bake_opacity(stops: Vec<(f32, f32)>, midpoints: &[f32], smoothness: f32, method: Method) -> Vec<(f32, f32)> { + let method = if method == Method::Smooth { Method::Classic } else { method }; + let plain_mids = midpoints.iter().all(|m| (m - 0.5).abs() < 1e-3); + let smoothness = if smoothness.is_finite() { smoothness.clamp(0.0, 1.0) } else { 0.0 }; + if stops.len() < 2 || (smoothness <= 0.0 && method == Method::Classic && plain_mids) { + return stops; + } + let into = |o: f32| -> f32 { + let o = if o.is_finite() { o.clamp(0.0, 1.0) } else { 1.0 }; + match method { + Method::Linear => lin(o), + Method::Perceptual => lin(o).cbrt(), + Method::Classic | Method::Smooth => o, + } + }; + let out = |v: f32| -> f32 { + let v = if v.is_finite() { v } else { 1.0 }; + let y = match method { + Method::Linear => v, + Method::Perceptual => v.max(0.0).powi(3), + Method::Classic | Method::Smooth => return v.clamp(0.0, 1.0), + }; + y.clamp(0.0, 1.0).powf(OPACITY_RESPONSE) + }; + let mut s = stops; + s.sort_by(|a, b| a.0.total_cmp(&b.0)); + let pts: Vec<(f32, [f32; 3])> = s.iter().map(|(t, o)| (*t, [into(*o); 3])).collect(); + const N: usize = 96; + let mut baked: Vec<(f32, f32)> = (0..=N) + .map(|k| { + let t = k as f32 / N as f32; + // Smoothness and midpoints shape the curve as for Classic colours, in the method's space. + (t, out(eval(&pts, midpoints, smoothness, t, Method::Classic)[0])) + }) + .collect(); + baked.dedup_by(|a, b| (a.0 - b.0).abs() < 1e-6); + baked +} + #[cfg(test)] mod tests { use super::*; + fn opacity_at(stops: &[(f32, f32)], t: f32) -> f32 { + let i = stops.windows(2).position(|w| t >= w[0].0 && t <= w[1].0).unwrap(); + let (a, b) = (stops[i], stops[i + 1]); + a.1 + (b.1 - a.1) * (t - a.0) / (b.0 - a.0) + } + + /// Photoshop Gradient Overlay samples (black, Normal, over white; a 256 px wide linear + /// gradient, pixel `x` at t = (x + 0.5) / 256): opacity stops, midpoints, Smoothness, method, + /// and (x, opacity) pairs read from the composite. + #[test] + fn opacity_matches_photoshop_samples() { + type Case = (&'static [(f32, f32)], &'static [f32], f32, Method, &'static [(f32, f32)]); + let ramp: &[(f32, f32)] = &[(0.0, 1.0), (1.0, 0.0)]; + let three: &[(f32, f32)] = &[(0.0, 1.0), (0.25, 0.2), (1.0, 0.9)]; + let cases: [Case; 9] = [ + (ramp, &[0.5], 0.0, Method::Linear, &[(64.0, 0.827), (128.0, 0.631), (192.0, 0.400)]), + (ramp, &[0.5], 0.0, Method::Perceptual, &[(64.0, 0.565), (128.0, 0.255), (192.0, 0.063)]), + (ramp, &[0.25], 1.0, Method::Classic, &[(32.0, 0.796), (64.0, 0.502), (128.0, 0.298), (192.0, 0.122)]), + (ramp, &[0.25], 1.0, Method::Linear, &[(32.0, 0.859), (64.0, 0.631), (128.0, 0.447), (192.0, 0.247)]), + (ramp, &[0.25], 1.0, Method::Perceptual, &[(32.0, 0.639), (64.0, 0.255), (128.0, 0.090), (192.0, 0.016)]), + (ramp, &[0.5], 1.0, Method::Classic, &[(64.0, 0.796), (128.0, 0.502), (192.0, 0.204)]), + (three, &[0.5, 0.5], 1.0, Method::Classic, &[(32.0, 0.557), (64.0, 0.200), (160.0, 0.502)]), + (three, &[0.5, 0.5], 1.0, Method::Perceptual, &[(32.0, 0.392), (64.0, 0.106), (160.0, 0.341)]), + (three, &[0.5, 0.5], 0.0, Method::Linear, &[(32.0, 0.647), (64.0, 0.106), (160.0, 0.557)]), + ]; + for (stops, mids, smooth, m, samples) in cases { + let baked = bake_opacity(stops.to_vec(), mids, smooth, m); + for (x, want) in samples { + let got = opacity_at(&baked, (x + 0.5) / 256.0); + assert!((got - want).abs() < 0.015, "{m:?} smooth {smooth} mids {mids:?} x {x}: {got} vs Photoshop {want}"); + } + } + // A flat 60 % renders as 47 % in Linear and Perceptual, 60 % in Classic. + for (m, want) in [(Method::Linear, 0.471), (Method::Perceptual, 0.471)] { + let baked = bake_opacity(vec![(0.0, 0.6), (1.0, 0.6)], &[0.5], 0.0, m); + assert!(baked.iter().all(|o| (o.1 - want).abs() < 0.01), "{m:?}: {baked:?}"); + } + } + + #[test] + fn plain_classic_and_smooth_opacity_stay_as_stored() { + let stops = vec![(1.0, 0.0), (0.0, 1.0)]; + assert_eq!(bake_opacity(stops.clone(), &[0.5], 0.0, Method::Classic), stops); + assert_eq!(bake_opacity(stops.clone(), &[0.5], 0.0, Method::Smooth), stops); + assert_eq!(bake_opacity(vec![(0.5, 0.3)], &[], 1.0, Method::Linear), vec![(0.5, 0.3)]); + } + + #[test] + fn opacity_bake_survives_hostile_values() { + for m in [Method::Classic, Method::Perceptual, Method::Linear, Method::Smooth] { + let baked = bake_opacity(vec![(f32::NAN, f32::INFINITY), (1.0, f32::NAN), (0.5, -3.0)], &[f32::NAN, 2.0], f32::NAN, m); + assert!(baked.iter().all(|o| (0.0..=1.0).contains(&o.1)), "{m:?}: {baked:?}"); + } + } + fn at(stops: &[(f32, Color)], t: f32) -> [f32; 3] { let i = stops.windows(2).position(|w| t >= w[0].0 && t <= w[1].0).unwrap(); let (a, b) = (&stops[i], &stops[i + 1]); diff --git a/crates/io/src/guides_map.rs b/crates/io/src/guides_map.rs new file mode 100644 index 0000000000..6137011e2c --- /dev/null +++ b/crates/io/src/guides_map.rs @@ -0,0 +1,157 @@ +//! Guides ⇄ PSD beyond image resource 1032. +//! +//! Photoshop writes two undocumented resources next to 1032 (Grid and Guides), layout +//! inferred from the psd-tools, ag-psd and Photoshop-authored corpora (#5965): +//! +//! * 1092, Grid and Guides version 2: `u32` version 2, `u32` horizontal and vertical grid +//! cycle (576 = 18 px in 1/32 px, as in 1032), `u32` count, then per guide 9 bytes: `i32` +//! position in 1/32 px, `u8` direction (0 vertical, 1 horizontal), one `u8` that is always 0 +//! in the corpora, and the guide's colour as `u8` R, G, B. In every corpus file its guides +//! equal 1032's, in the same order. +//! * 1097: `u32` count (1092's), then per guide a length-prefixed ASCII GUID, a `u32` 0 and a +//! second length-prefixed GUID, always all zeros. +//! +//! Import takes the guides' own colours from 1092 (a guide in Photoshop's default colour keeps +//! following the preference colour). Export keeps both resources only while 1092 still +//! describes the document's guides and colours; otherwise it leaves both out, so the file +//! carries 1032 alone, as Photoshop files before version 2 did (#5966). + +use photocraft_doc::{Document, Guides}; + +/// Grid and Guides, version 2 (positions, directions and colours). +pub const GUIDES_V2: u16 = 1092; +/// One GUID per guide, in 1092's order. +pub const GUIDE_IDS: u16 = 1097; +/// Photoshop's default guide colour (Cyan, `#4affff`), which 1092 also stores for guides +/// that use the preference colour. +pub const DEFAULT_GUIDE_RGB: [u8; 3] = [0x4a, 0xff, 0xff]; + +/// One guide of a 1092 resource. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct GuideV2 { + /// Position in 1/32 px. + pub location: i32, + pub vertical: bool, + pub rgb: [u8; 3], +} + +fn be_u32(data: &[u8], at: usize) -> Option { + let b = data.get(at..at.checked_add(4)?)?; + Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) +} + +/// The guides of a 1092 resource, or `None` when it is not version 2 or is cut short. +pub fn parse_v2(data: &[u8]) -> Option> { + if be_u32(data, 0)? != 2 { + return None; + } + let count = usize::try_from(be_u32(data, 12)?).ok()?; + // Each guide takes 9 bytes: a count the data can't hold is not a 1092 we understand. + if count > data.len().saturating_sub(16) / 9 { + return None; + } + (0..count) + .map(|i| { + let at = 16 + i * 9; + let g = data.get(at..at + 9)?; + Some(GuideV2 { location: i32::from_be_bytes([g[0], g[1], g[2], g[3]]), vertical: g[4] != 1, rgb: [g[6], g[7], g[8]] }) + }) + .collect() +} + +/// The number of entries a 1097 resource declares. +pub fn ids_count(data: &[u8]) -> Option { + be_u32(data, 0) +} + +/// The document's guides as 1092 would store them, sorted. +fn expected(guides: &Guides) -> Vec { + let mut v: Vec = [(&guides.vertical, true), (&guides.horizontal, false)] + .into_iter() + .flat_map(|(list, vertical)| { + list.iter().map(move |&p| GuideV2 { location: (p * 32.0).round() as i32, vertical, rgb: guides.color(vertical, p).unwrap_or(DEFAULT_GUIDE_RGB) }) + }) + .collect(); + v.sort(); + v +} + +/// Whether an imported 1092 still describes the document's guides: the same positions, +/// directions and colours, in any order. +pub fn describes(doc: &Document, data: &[u8]) -> bool { + let Some(mut stored) = parse_v2(data) else { return false }; + stored.sort(); + stored == expected(&doc.guides) +} + +/// Whether to write back an imported 1092 or 1097: only while 1092 describes the guides +/// (and, for 1097, holds as many entries as 1092). +pub fn keep_resource(doc: &Document, id: u16, data: &[u8]) -> bool { + match id { + GUIDES_V2 => describes(doc, data), + GUIDE_IDS => { + let v2 = doc.metadata.psd_resources.iter().find(|(rid, _, _)| *rid == GUIDES_V2); + v2.is_some_and(|(_, _, d)| describes(doc, d) && parse_v2(d).map(|g| g.len()).and_then(|n| u32::try_from(n).ok()) == ids_count(data)) + } + _ => true, + } +} + +/// Gives the document's guides the colours an imported 1092 stores. Only guides that 1032 +/// also has are coloured, and Photoshop's default colour is left to the preference. +pub fn apply_colors(doc: &mut Document) { + let Some(stored) = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == GUIDES_V2).and_then(|(_, _, d)| parse_v2(d)) else { + return; + }; + for g in stored { + if g.rgb == DEFAULT_GUIDE_RGB { + continue; + } + let pos = f64::from(g.location) / 32.0; + let list = if g.vertical { &doc.guides.vertical } else { &doc.guides.horizontal }; + if list.contains(&pos) { + doc.guides.set_color(g.vertical, pos, Some(g.rgb)); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn v2(guides: &[(i32, u8, [u8; 3])]) -> Vec { + let mut v = Vec::new(); + for n in [2u32, 576, 576, guides.len() as u32] { + v.extend_from_slice(&n.to_be_bytes()); + } + for (loc, dir, rgb) in guides { + v.extend_from_slice(&loc.to_be_bytes()); + v.extend_from_slice(&[*dir, 0]); + v.extend_from_slice(rgb); + } + v + } + + #[test] + fn parses_corpus_layout() { + // clipping-mask4.psd (psd-tools): one vertical and one horizontal guide at 16 px. + let d = v2(&[(512, 0, DEFAULT_GUIDE_RGB), (512, 1, DEFAULT_GUIDE_RGB)]); + let g = parse_v2(&d).unwrap(); + assert_eq!(g.len(), 2); + assert!(g[0].vertical && !g[1].vertical); + assert_eq!(g[1].location, 512); + } + + #[test] + fn rejects_other_versions_and_short_or_hostile_data() { + let mut d = v2(&[(512, 0, DEFAULT_GUIDE_RGB)]); + assert!(parse_v2(&d[..d.len() - 1]).is_none()); + d[3] = 1; + assert!(parse_v2(&d).is_none()); + let mut huge = v2(&[]); + huge[12..16].copy_from_slice(&u32::MAX.to_be_bytes()); + assert!(parse_v2(&huge).is_none()); + assert!(parse_v2(&[]).is_none()); + assert!(ids_count(&[0, 0]).is_none()); + } +} diff --git a/crates/io/src/hue_sat_filter_map.rs b/crates/io/src/hue_sat_filter_map.rs new file mode 100644 index 0000000000..05af178c27 --- /dev/null +++ b/crates/io/src/hue_sat_filter_map.rs @@ -0,0 +1,384 @@ +//! Hue/Saturation smart filters (`Fltr` class `HStr`, filterID `HStr` as a long), as Photoshop +//! records Image › Adjustments › Hue/Saturation applied to a smart object: +//! +//! ```text +//! Fltr = HStr { presetKind = presetKindType.presetKindCustom, Clrz = bool, +//! Adjs = [ Hst2 { H , Strt, Lght } (master, first) +//! Hst2 { LclR (1 reds … 6 magentas), BgnR, BgnS, EndS, EndR, (one per edited range) +//! H , Strt, Lght } ] } +//! ``` +//! +//! Every number is a long: hue in degrees (a shift of -180..180, or 0..360 with Colorize), the +//! rest in percent; the four range sliders are hue degrees (reds: 315, 345, 15, 45). The filter +//! maps to `image.adjustments.hueSaturation` with the command's own params, so it re-renders +//! when the smart object does. Anything else (an unknown key, a duplicated or unknown range, a +//! value Photoshop's dialog can't hold) stays an opaque [`super::UNSUPPORTED_FILTER`]. + +use photocraft_doc::SmartFilter; +use photocraft_doc::adjust::HueRange; +use photocraft_psd::descriptor::{Descriptor, UnicodeString, Value}; +use serde_json::{Map, Value as J, json}; + +use super::{blend_options, enumv, is_meta, rgbc}; +use crate::blocks::num; + +pub(super) const CLASS: &str = "HStr"; +pub(super) const COMMAND: &str = "image.adjustments.hueSaturation"; +const FILTER_ID: i32 = super::code(b"HStr"); +/// `Nm ` (Photoshop writes the localized menu item). +const NAME: &str = "Hue/Saturation..."; +/// The `Fltr` descriptor's own name. +const FLTR_NAME: &str = "Hue/Saturation"; + +/// The colour ranges in `LclR` order (1 = reds), as the command's param keys. +const RANGES: [&str; 6] = ["reds", "yellows", "greens", "cyans", "blues", "magentas"]; +const BOUNDS: [&str; 4] = ["BgnR", "BgnS", "EndS", "EndR"]; +const SETTINGS: [(&str, &str); 3] = [("H ", "hue"), ("Strt", "saturation"), ("Lght", "lightness")]; + +/// A whole, finite number within `lo..=hi`, as JSON. +fn whole(v: Option<&Value>, lo: f64, hi: f64) -> Option { + let v = num(v).filter(|v| v.is_finite() && v.fract() == 0.0 && (lo..=hi).contains(v))?; + Some(json!(v as i64)) +} + +/// `image.adjustments.hueSaturation` params for an `HStr` filter; `None` when any part of it is +/// not the observed layout. +pub(super) fn import_params(f: &Descriptor) -> Option { + if !f.class_id.is(CLASS) || f.items.iter().any(|(k, _)| !["presetKind", "Clrz", "Adjs"].iter().any(|n| k.is(n))) { + return None; + } + let colorize = match f.get("Clrz") { + Some(Value::Boolean(b)) => *b, + None => false, + Some(_) => return None, + }; + let Some(Value::List(entries)) = f.get("Adjs") else { return None }; + // A master and six ranges at most. + if entries.len() > 1 + RANGES.len() { + return None; + } + let mut out = Map::new(); + out.insert("colorize".into(), J::Bool(colorize)); + let mut seen_master = false; + for e in entries { + let Value::Descriptor(e) = e else { return None }; + if e.items.iter().any(|(k, _)| !k.is("LclR") && !BOUNDS.iter().any(|n| k.is(n)) && !SETTINGS.iter().any(|(n, _)| k.is(n))) { + return None; + } + let mut o = Map::new(); + match e.get("LclR") { + None => { + if std::mem::replace(&mut seen_master, true) { + return None; + } + let (hue, sat) = if colorize { ((0.0, 360.0), (0.0, 100.0)) } else { ((-180.0, 180.0), (-100.0, 100.0)) }; + o.insert("hue".into(), whole(e.get("H "), hue.0, hue.1)?); + o.insert("saturation".into(), whole(e.get("Strt"), sat.0, sat.1)?); + o.insert("lightness".into(), whole(e.get("Lght"), -100.0, 100.0)?); + out.extend(o); + } + r => { + let i = whole(r, 1.0, RANGES.len() as f64)?.as_u64()?; + let key = *RANGES.get(usize::try_from(i).ok()?.checked_sub(1)?)?; + if out.contains_key(key) { + return None; + } + o.insert("hue".into(), whole(e.get("H "), -180.0, 180.0)?); + o.insert("saturation".into(), whole(e.get("Strt"), -100.0, 100.0)?); + o.insert("lightness".into(), whole(e.get("Lght"), -100.0, 100.0)?); + let bounds: Option> = BOUNDS.iter().map(|k| whole(e.get(k), -720.0, 720.0)).collect(); + o.insert("range".into(), J::Array(bounds?)); + out.insert(key.into(), J::Object(o)); + } + } + } + Some(J::Object(out)) +} + +/// A number param of `o` (`default` when absent), finite. +fn number(o: &J, key: &str, default: f64) -> Result { + match o.get(key) { + None => Ok(default), + Some(v) => v.as_f64().filter(|v| v.is_finite()).ok_or(format!("Hue/Saturation `{key}` must be a number")), + } +} + +/// A number param rounded and clamped like the dialog's field, as a long. +fn setting(o: &J, key: &str, default: f64, lo: f64, hi: f64) -> Result { + Ok(long(number(o, key, default)?, lo, hi)) +} + +fn long(v: f64, lo: f64, hi: f64) -> Value { + Value::Integer(v.round().clamp(lo, hi) as i32) +} + +/// The `Adjs` entry of colour range `i` (0 = reds), or `None` when it is neutral with its default +/// extent (Photoshop lists only the ranges that were edited). +fn range_entry(i: usize, o: &J) -> Result, String> { + let obj = o.as_object().ok_or(format!("Hue/Saturation `{}` must be an object", RANGES.get(i).unwrap_or(&"")))?; + if let Some(k) = obj.keys().find(|k| !["hue", "saturation", "lightness", "range"].contains(&k.as_str())) { + return Err(format!("Hue/Saturation range setting `{k}` has no PSD equivalent")); + } + let neutral = HueRange::neutral(i); + let bounds = match o.get("range") { + None => neutral.bounds, + Some(r) => { + let a = r.as_array().filter(|a| a.len() == 4).ok_or("a Hue/Saturation range must be four hue degrees")?; + let mut b = [0f32; 4]; + for (slot, v) in b.iter_mut().zip(a) { + *slot = v.as_f64().filter(|v| v.is_finite()).ok_or("a Hue/Saturation range must be four hue degrees")? as f32; + } + HueRange::canonical_bounds(b) + } + }; + let hue = setting(o, "hue", 0.0, -180.0, 180.0)?; + let sat = setting(o, "saturation", 0.0, -100.0, 100.0)?; + let light = setting(o, "lightness", 0.0, -100.0, 100.0)?; + let zero = Value::Integer(0); + if [&hue, &sat, &light].iter().all(|v| **v == zero) && bounds == neutral.bounds { + return Ok(None); + } + let lclr = i32::try_from(i + 1).map_err(|e| e.to_string())?; + let mut d = Descriptor::new("Hst2").with("LclR", Value::Integer(lclr)); + for (k, v) in BOUNDS.iter().zip(bounds) { + // Photoshop writes the sliders within one turn (reds: 315, 345, 15, 45). + d = d.with(k, Value::Integer(f64::from(v).round().rem_euclid(360.0) as i32)); + } + Ok(Some(Value::Descriptor(d.with("H ", hue).with("Strt", sat).with("Lght", light)))) +} + +/// The `filterFXList` item for an `image.adjustments.hueSaturation` smart filter. +pub(super) fn export_item(f: &SmartFilter) -> Result { + let p = &f.params; + let obj = match p { + J::Object(o) => o, + J::Null => &Map::new(), + _ => return Err("Hue/Saturation parameters must be an object".into()), + }; + let known = |k: &str| ["hue", "saturation", "lightness", "colorize"].contains(&k) || RANGES.contains(&k); + if let Some(k) = obj.keys().find(|k| !is_meta(k) && !known(k)) { + return Err(format!("Hue/Saturation setting `{k}` has no PSD equivalent")); + } + let colorize = match p.get("colorize") { + None => false, + Some(v) => v.as_bool().ok_or("Hue/Saturation `colorize` must be true or false")?, + }; + // As the command: Colorize hue is 0..360 and starts at 25 % saturation unless one is given. + let master = if colorize { + (long(number(p, "hue", 0.0)?.rem_euclid(360.0), 0.0, 360.0), setting(p, "saturation", 25.0, 0.0, 100.0)?) + } else { + (setting(p, "hue", 0.0, -180.0, 180.0)?, setting(p, "saturation", 0.0, -100.0, 100.0)?) + }; + let mut adjs = vec![Value::Descriptor( + Descriptor::new("Hst2").with("H ", master.0).with("Strt", master.1).with("Lght", setting(p, "lightness", 0.0, -100.0, 100.0)?), + )]; + for (i, key) in RANGES.iter().enumerate() { + // A `null` range is an untouched one, like a missing key. + if let Some(o) = p.get(*key).filter(|o| !o.is_null()) { + adjs.extend(range_entry(i, o)?); + } + } + let mut fltr = Descriptor::new(CLASS) + .with("presetKind", enumv("presetKindType", "presetKindCustom")) + .with("Clrz", Value::Boolean(colorize)) + .with("Adjs", Value::List(adjs)); + fltr.name = UnicodeString::new_nul(FLTR_NAME); + Ok(Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(NAME))) + .with("blendOptions", blend_options(f)) + .with("enab", Value::Boolean(f.visible)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(FILTER_ID))) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::smart_map::{UNSUPPORTED_FILTER, filter_from_item, item_for_filter}; + use photocraft_color::BlendMode; + use photocraft_psd::descriptor::Id; + + fn hst2(range: Option<(i32, [i32; 4])>, h: i32, s: i32, l: i32) -> Value { + let mut d = Descriptor::new("Hst2"); + if let Some((r, b)) = range { + d = d.with("LclR", Value::Integer(r)); + for (k, v) in BOUNDS.iter().zip(b) { + d = d.with(k, Value::Integer(v)); + } + } + Value::Descriptor(d.with("H ", Value::Integer(h)).with("Strt", Value::Integer(s)).with("Lght", Value::Integer(l))) + } + + fn entry(items: &[(&str, Value)]) -> Value { + Value::Descriptor(items.iter().fold(Descriptor::new("Hst2"), |d, (k, v)| d.with(k, v.clone()))) + } + + fn fltr(colorize: bool, adjs: Vec) -> Descriptor { + let mut f = Descriptor::new(CLASS) + .with("presetKind", enumv("presetKindType", "presetKindCustom")) + .with("Clrz", Value::Boolean(colorize)) + .with("Adjs", Value::List(adjs)); + f.name = UnicodeString::new_nul("Farbton/Sättigung"); + f + } + + /// A `filterFXList` item exactly as Photoshop saved it (German UI: localized names, the + /// foreground colour of the moment in `FrgC`). + fn item(f: Descriptor) -> Descriptor { + let c = |r: f64, g: f64, b: f64| { + Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(r)).with("Grn ", Value::Double(g)).with("Bl ", Value::Double(b))) + }; + let neutral = SmartFilter { command: String::new(), params: J::Null, blend: BlendMode::Normal, opacity: 1.0, visible: true }; + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Farbton/Sättigung..."))) + .with("blendOptions", blend_options(&neutral)) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", c(167.0, 157.0, 147.0)) + .with("BckC", c(255.0, 255.0, 255.0)) + .with("Fltr", Value::Descriptor(f)) + .with("filterID", Value::Integer(FILTER_ID)) + } + + /// Master 12 / -20 / 5 and Yellows -16 / 10 / -5 over 15, 45, 75, 105, Colorize off. + fn observed() -> Descriptor { + item(fltr(false, vec![hst2(None, 12, -20, 5), hst2(Some((2, [15, 45, 75, 105])), -16, 10, -5)])) + } + + fn fltr_of(d: &Descriptor) -> &Descriptor { + match d.get("Fltr") { + Some(Value::Descriptor(f)) => f, + _ => panic!("no Fltr"), + } + } + + fn filter(params: J) -> SmartFilter { + SmartFilter { command: COMMAND.into(), params, blend: BlendMode::Normal, opacity: 1.0, visible: true } + } + + #[test] + fn filter_id_is_hstr() { + assert_eq!(FILTER_ID, 1213428850); + } + + #[test] + fn photoshop_items_map_to_the_command() { + let f = filter_from_item(&observed()); + assert_eq!(f.command, COMMAND); + assert_eq!( + f.params, + json!({"colorize": false, "hue": 12, "saturation": -20, "lightness": 5, + "yellows": {"hue": -16, "saturation": 10, "lightness": -5, "range": [15, 45, 75, 105]}}) + ); + let c = filter_from_item(&item(fltr(true, vec![hst2(None, 200, 40, -10)]))); + assert_eq!(c.command, COMMAND); + assert_eq!(c.params, json!({"colorize": true, "hue": 200, "saturation": 40, "lightness": -10})); + } + + #[test] + fn export_writes_photoshops_layout_and_round_trips() { + let reds = item(fltr(false, vec![hst2(None, 0, 0, 0), hst2(Some((1, [315, 345, 15, 45])), 30, 0, 0)])); + for src in [observed(), item(fltr(true, vec![hst2(None, 200, 40, -10)])), reds] { + let f = filter_from_item(&src); + let out = item_for_filter(&f).unwrap(); + let keys = |d: &Descriptor| d.items.iter().map(|(k, _)| k.clone()).collect::>(); + assert_eq!(keys(&out), keys(&src), "item key order"); + let (a, b) = (fltr_of(&out), fltr_of(&src)); + assert_eq!((&a.class_id, &a.items), (&b.class_id, &b.items), "Fltr as Photoshop writes it"); + assert_eq!(out.get("filterID"), Some(&Value::Integer(FILTER_ID))); + assert_eq!(filter_from_item(&out), f); + } + // Blending and visibility are the model's own. + let mut f = filter_from_item(&observed()); + (f.opacity, f.blend, f.visible) = (0.5, BlendMode::Screen, false); + assert_eq!(filter_from_item(&item_for_filter(&f).unwrap()), f); + } + + #[test] + fn command_params_are_written_like_the_dialog() { + let export = |p: J| filter_from_item(&item_for_filter(&filter(p)).unwrap()).params; + // Partial params: the command's defaults, numbers rounded and clamped. + assert_eq!(export(json!({"hue": 12.4})), json!({"colorize": false, "hue": 12, "saturation": 0, "lightness": 0})); + assert_eq!(export(json!({"hue": -400, "lightness": 101, "layer": 3})), json!({"colorize": false, "hue": -180, "saturation": 0, "lightness": 100})); + assert_eq!(export(J::Null), json!({"colorize": false, "hue": 0, "saturation": 0, "lightness": 0})); + assert_eq!(export(json!({"reds": null})), json!({"colorize": false, "hue": 0, "saturation": 0, "lightness": 0})); + // Colorize starts at 25 % saturation and wraps the hue into 0..360. + assert_eq!(export(json!({"colorize": true, "hue": -30})), json!({"colorize": true, "hue": 330, "saturation": 25, "lightness": 0})); + // The full set (`to_params`): neutral ranges at their default extent are left out, reds + // are written within one turn. + let mut full = json!({"hue": 0, "saturation": 0, "lightness": 0, "colorize": false}); + for (i, k) in RANGES.iter().enumerate() { + let r = HueRange::neutral(i); + full[*k] = json!({"hue": r.hue, "saturation": r.saturation, "lightness": r.lightness, "range": r.bounds}); + } + full["reds"]["saturation"] = json!(-50.0); + full["blues"]["range"] = json!([190, 220, 250, 280]); + let back = export(full); + assert_eq!(back["reds"], json!({"hue": 0, "saturation": -50, "lightness": 0, "range": [315, 345, 15, 45]})); + assert_eq!(back["blues"], json!({"hue": 0, "saturation": 0, "lightness": 0, "range": [190, 220, 250, 280]})); + assert_eq!(RANGES.iter().filter(|k| back.get(**k).is_some()).count(), 2, "{back}"); + } + + #[test] + fn unreadable_items_stay_opaque_and_are_written_back() { + let zero = Value::Integer(0); + let b4 = [0, 30, 60, 90]; + let cases = vec![ + fltr(false, vec![hst2(None, 1, 2, 3)]).with("Lab ", Value::Boolean(true)), + fltr(false, vec![hst2(None, 1, 2, 3), hst2(None, 1, 2, 3)]), + fltr(false, vec![hst2(None, 0, 0, 0), hst2(Some((2, b4)), 1, 0, 0), hst2(Some((2, b4)), 2, 0, 0)]), + fltr(false, vec![hst2(Some((0, b4)), 1, 0, 0)]), + fltr(false, vec![hst2(Some((7, b4)), 1, 0, 0)]), + fltr(false, vec![hst2(Some((-1, b4)), 1, 0, 0)]), + fltr(false, vec![hst2(None, 181, 0, 0)]), + fltr(false, vec![hst2(None, 0, -101, 0)]), + fltr(true, vec![hst2(None, -1, 0, 0)]), + fltr(true, vec![hst2(None, 10, -5, 0)]), + fltr(false, vec![hst2(Some((3, [0, 1, 2, 9999])), 1, 0, 0)]), + fltr(false, vec![Value::Integer(4)]), + fltr(false, vec![entry(&[("H ", zero.clone()), ("Strt", zero.clone()), ("Lght", zero.clone()), ("Clr ", zero.clone())])]), + fltr(false, vec![entry(&[("H ", Value::Double(f64::NAN)), ("Strt", zero.clone()), ("Lght", zero.clone())])]), + fltr(false, vec![entry(&[("H ", Value::Double(1.5)), ("Strt", zero.clone()), ("Lght", zero.clone())])]), + fltr(false, vec![entry(&[("H ", Value::Boolean(true)), ("Strt", zero.clone()), ("Lght", zero.clone())])]), + fltr(false, vec![entry(&[("Strt", zero.clone()), ("Lght", zero.clone())])]), + fltr(false, vec![entry(&[("LclR", Value::Integer(1)), ("H ", zero.clone()), ("Strt", zero.clone()), ("Lght", zero.clone())])]), + fltr(false, (0..8).map(|i| hst2(Some((i % 6 + 1, b4)), 0, 0, 0)).collect()), + Descriptor::new(CLASS).with("Clrz", Value::Integer(1)).with("Adjs", Value::List(vec![hst2(None, 1, 2, 3)])), + Descriptor::new(CLASS).with("Clrz", Value::Boolean(false)), + Descriptor::new(CLASS).with("Adjs", Value::Integer(0)), + ]; + for (n, f) in cases.into_iter().enumerate() { + let src = item(f); + let sf = filter_from_item(&src); + assert_eq!(sf.command, UNSUPPORTED_FILTER, "case {n}"); + assert_eq!(item_for_filter(&sf).unwrap().to_bytes(), src.to_bytes(), "case {n}"); + } + // No `Fltr` at all. + let mut bare = observed(); + bare.items.retain(|(k, _)| !k.is("Fltr")); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); + } + + #[test] + fn bad_exports_report_errors() { + for p in [ + json!(3), + json!({"hue": "red"}), + json!({"colorize": "yes"}), + json!({"preset": "sepia"}), + json!({"reds": 4}), + json!({"reds": {"hue": "x"}}), + json!({"reds": {"range": [1, 2, 3]}}), + json!({"reds": {"range": [1, 2, 3, "x"]}}), + json!({"reds": {"width": 2}}), + ] { + assert!(item_for_filter(&filter(p.clone())).is_err(), "{p}"); + } + // Huge numbers clamp rather than overflow. + let out = filter_from_item(&item_for_filter(&filter(json!({"hue": f64::MAX, "colorize": true, "saturation": -1e300}))).unwrap()); + assert_eq!(out.command, COMMAND); + } +} diff --git a/crates/io/src/kra.rs b/crates/io/src/kra.rs index 64e4facf61..d8b3f9a36d 100644 --- a/crates/io/src/kra.rs +++ b/crates/io/src/kra.rs @@ -112,7 +112,7 @@ impl Reader<'_, '_> { if file.pixel_size != space.pixel_size() { return Err(invalid(format!("{name}: pixel size {} does not match its colour space", file.pixel_size))); } - let cost = file.decoded_bytes(fmt.bytes_per_pixel())?; + let cost = file.decoded_bytes(fmt.bytes_per_pixel(), dx, dy)?; self.budget = self.budget.checked_sub(cost).ok_or_else(|| invalid("decoded layers exceed the 1 GiB limit"))?; let mut surface = match self.default_pixel(name, space)? { Some(px) if px.iter().any(|v| *v != 0.0) => Surface::with_default(fmt, &space.to_format(&px, fmt)), @@ -283,10 +283,10 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result Result { let v = number(image, a, 0.0)?; diff --git a/crates/io/src/kra/tests.rs b/crates/io/src/kra/tests.rs index 4316a74c10..214ccdc9c6 100644 --- a/crates/io/src/kra/tests.rs +++ b/crates/io/src/kra/tests.rs @@ -28,7 +28,8 @@ fn tile_file(pixel_size: usize, tiles: &[(i32, i32, Vec)]) -> Vec { planar[k * n + p] = px[p * pixel_size + k]; } } - let body: Vec = if i % 2 == 1 { [vec![1], lzf_literal(&planar)].concat() } else { [vec![0], planar].concat() }; + // Krita splits only LZF tiles into byte planes; raw tiles stay interleaved. + let body: Vec = if i % 2 == 1 { [vec![1], lzf_literal(&planar)].concat() } else { [&[0][..], px].concat() }; out.extend(format!("{x},{y},LZF,{}\n", body.len()).into_bytes()); out.extend(body); } @@ -160,6 +161,22 @@ fn a_nonzero_default_pixel_fills_untouched_areas() { assert_eq!(s.to_interleaved(Rect::from_xywh(100, 100, 1, 1)), [0, 0, 255, 255]); } +#[test] +fn raw_tiles_hold_interleaved_pixels() { + // Krita writes a tile uncompressed (flag 0) when LZF can't shrink it, and then the pixels + // stay interleaved: only LZF tiles are split into byte planes. + let mut body = vec![0u8]; + body.extend((0..T * T).flat_map(|_| [10u8, 20, 30, 255])); + let mut file = format!("VERSION 2\nTILEWIDTH {T}\nTILEHEIGHT {T}\nPIXELSIZE 4\nDATA 1\n0,0,LZF,{}\n", body.len()).into_bytes(); + file.extend(body); + let doc = maindoc("RGBA", r#""#); + let r = crate::import("a.kra", &archive(&doc, &[("img/layers/l", file)])).unwrap(); + let s = layer(&r.document, "L").surface().unwrap(); + for (x, y) in [(0, 0), (1, 0), (3, 3)] { + assert_eq!(s.to_interleaved(Rect::from_xywh(x, y, 1, 1)), [30, 20, 10, 255], "pixel {x},{y}"); + } +} + fn merged_png() -> Vec { let img = Image::from_raw(12, 8, ChannelLayout::Rgba, CSample::U8, vec![90; 12 * 8 * 4]).unwrap(); photocraft_codecs::encode(&img, Format::Png, &Default::default()).unwrap() @@ -183,6 +200,23 @@ fn export_is_refused() { } } +/// maindoc.xml nested 20 000 deep, directly or through an internal-subset entity the depth check +/// can't see, overflowed the XML parser's recursion (an abort nothing can catch). Both are errors +/// now; the small stack makes a regression abort instead of passing. +#[test] +fn deeply_nested_maindoc_is_an_error_not_a_stack_overflow() { + let (open, close) = ("".repeat(20_000), "".repeat(20_000)); + let direct = maindoc("RGBA", &format!("{open}{close}")); + let entity = format!("]>&e;"); + // A quoted `?>` in the XML declaration must not hide the DOCTYPE behind it. + let declared = format!("\"?>{entity}"); + for (xml, why) in [(direct, "nested"), (entity, "internal DTD"), (declared, "internal DTD")] { + let bytes = archive(&xml, &[]); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || crate::import("deep.kra", &bytes).err()).unwrap().join().unwrap(); + assert!(matches!(&e, Some(IoError::Kra(m)) if m.contains(why)), "{why}: {e:?}"); + } +} + #[test] fn lzf_decodes_literals_and_overlapping_references() { // "abc" then a 6-byte back-reference 3 back: abcabcabc. diff --git a/crates/io/src/kra/tiles.rs b/crates/io/src/kra/tiles.rs index 8af6638aa8..d7330868b7 100644 --- a/crates/io/src/kra/tiles.rs +++ b/crates/io/src/kra/tiles.rs @@ -216,9 +216,27 @@ impl<'a> TileFile<'a> { Ok(TileFile { width, height, pixel_size, tiles }) } - /// Bytes the decoded tiles take at `bpp` bytes per pixel. - pub(crate) fn decoded_bytes(&self, bpp: usize) -> Result { - self.tiles.len().checked_mul(self.width * self.height).and_then(|n| n.checked_mul(bpp)).ok_or_else(|| invalid("tile size overflow")) + /// Bytes the decoded tiles take at `bpp` bytes per pixel: the larger of their own pixels and + /// the whole 256×256 backing tiles they land in (a surface stores no less than a tile, so + /// sparse tiles at distinct positions retain far more than they hold). + pub(crate) fn decoded_bytes(&self, bpp: usize, dx: i32, dy: i32) -> Result { + let overflow = || invalid("tile size overflow"); + let logical = self.tiles.len().checked_mul(self.width * self.height).and_then(|n| n.checked_mul(bpp)).ok_or_else(overflow)?; + let side = photocraft_geom::TILE_SIZE; + let (w, h) = (i32::try_from(self.width.max(1)).map_err(|_| overflow())?, i32::try_from(self.height.max(1)).map_err(|_| overflow())?); + let mut backing = std::collections::HashSet::new(); + for &(x, y, _) in &self.tiles { + // The tile lands at its offset position and covers every backing tile its box overlaps. + let (x, y) = (x.checked_add(dx).ok_or_else(overflow)?, y.checked_add(dy).ok_or_else(overflow)?); + let (x1, y1) = (x.checked_add(w - 1).ok_or_else(overflow)?, y.checked_add(h - 1).ok_or_else(overflow)?); + for c in x.div_euclid(side)..=x1.div_euclid(side) { + for r in y.div_euclid(side)..=y1.div_euclid(side) { + backing.insert((c, r)); + } + } + } + let tile_bytes = (side as usize).checked_mul(side as usize).and_then(|n| n.checked_mul(bpp)).ok_or_else(overflow)?; + Ok(logical.max(backing.len().checked_mul(tile_bytes).ok_or_else(overflow)?)) } /// Writes every tile into `surface` (format `surface.format()`), offset by (`dx`, `dy`). @@ -231,19 +249,22 @@ impl<'a> TileFile<'a> { for &(x, y, data) in &self.tiles { ctl.check().map_err(|_| IoError::Cancelled)?; let (flag, payload) = data.split_first().ok_or_else(|| invalid("empty tile"))?; - let planar: std::borrow::Cow<'_, [u8]> = match flag { - 1 => lzf_decompress(payload, expected)?.into(), - 0 if payload.len() == expected => payload.into(), - 0 => return Err(invalid("raw tile has the wrong size")), - f => return Err(invalid(format!("unknown tile compression flag {f}"))), - }; - // Byte planes: byte k of pixel i is at k * n + i. - for (k, plane) in planar.chunks_exact(n).enumerate() { - for (i, b) in plane.iter().enumerate() { - if let Some(slot) = interleaved.get_mut(i * self.pixel_size + k) { - *slot = *b; + match flag { + 1 => { + // LZF tiles hold byte planes: byte k of pixel i is at k * n + i. + let planar = lzf_decompress(payload, expected)?; + for (k, plane) in planar.chunks_exact(n).enumerate() { + for (i, b) in plane.iter().enumerate() { + if let Some(slot) = interleaved.get_mut(i * self.pixel_size + k) { + *slot = *b; + } + } } } + // Raw tiles (written when LZF doesn't shrink them) are already interleaved. + 0 if payload.len() == expected => interleaved.copy_from_slice(payload), + 0 => return Err(invalid("raw tile has the wrong size")), + f => return Err(invalid(format!("unknown tile compression flag {f}"))), } space.convert(&interleaved, fmt, &mut out); let (x0, y0) = @@ -257,3 +278,31 @@ impl<'a> TileFile<'a> { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn file(positions: &[(i32, i32)]) -> TileFile<'static> { + TileFile { width: 64, height: 64, pixel_size: 4, tiles: positions.iter().map(|&(x, y)| (x, y, &[][..])).collect() } + } + + #[test] + fn sparse_tiles_are_charged_for_the_backing_tiles_they_land_in() { + // Sixteen 64×64 tiles that fill one 256×256 backing tile cost what they hold. + let dense: Vec<_> = (0..4).flat_map(|y| (0..4).map(move |x| (x * 64, y * 64))).collect(); + assert_eq!(file(&dense).decoded_bytes(4, 0, 0).unwrap(), 16 * 64 * 64 * 4); + // The same number far apart each take a whole backing tile: 16 × 256 × 256 × 4. + let sparse: Vec<_> = (0..16).map(|i| (i * 10_240, 0)).collect(); + assert_eq!(file(&sparse).decoded_bytes(4, 0, 0).unwrap(), 16 * 256 * 256 * 4); + } + + #[test] + fn the_placement_offset_and_tile_extent_decide_the_backing_tiles() { + // One 64×64 tile straddling a backing-tile corner once offset touches four. + assert_eq!(file(&[(0, 0)]).decoded_bytes(4, 224, 224).unwrap(), 4 * 256 * 256 * 4); + // A tile wider than a backing tile reaches the ones between its corners. + let wide = TileFile { width: 1024, height: 1, pixel_size: 4, tiles: vec![(0, 0, &[][..])] }; + assert_eq!(wide.decoded_bytes(4, 0, 0).unwrap(), 4 * 256 * 256 * 4); + } +} diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index 24b8b3f948..0a184bda79 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -20,6 +20,8 @@ //! opacity and visibility; see `ora`. //! * Krita (`.kra`, `.krz`) documents open with their paint layers, groups and transparency //! masks (import only); see `kra`. +//! * GIMP XCF (`.xcf`, `.xcf.gz`) documents open with their layers, groups, masks, blend modes, +//! channels and selection, read only; see `xcf`. //! * Affinity documents (`.af`, `.afdesign`, `.afphoto`, `.afpub`) open natively //! with no source save path, what isn't imported listed in the warnings; a file //! whose native data can't be read opens as its embedded preview; see `affinity`. @@ -36,23 +38,30 @@ pub mod abr_map; mod adjust_map; pub mod affinity; pub mod annotations_map; +pub mod asl_map; pub mod blocks; pub mod camera_raw_xmp; mod channel_map; pub mod comps_map; +pub mod doc_path; pub mod effects_map; pub mod exr_parts; mod flat; mod gradient_bake; +pub mod guides_map; pub use gradient_bake::{Method as GradientMethod, bake as bake_gradient}; +mod duotone_map; mod kra; pub mod linked; mod multichannel_map; mod ora; +mod palette_map; pub use ora::losses as ora_losses; pub mod pattern_map; +mod pcraft_cache; pub mod pdf; mod pdn; +pub use pcraft_cache::PcraftExportCache; pub mod pixels; mod psd_export; mod psd_import; @@ -65,16 +74,18 @@ mod text_import; pub mod text_styles_map; pub mod tiff_layers; pub mod vector_map; +mod xcf; +pub mod xml; -pub use photocraft_codecs::DpxBits; use photocraft_codecs::{CodecError, EncodeOptions}; -use photocraft_doc::Document; +pub use photocraft_codecs::{DdsCompression, DdsOptions, DpxBits}; +use photocraft_doc::{Adjustment, Document, Layer, LayerContent}; use photocraft_psd::{PsdError, PsdFile}; pub use adjust_map::ADJUSTMENT_KEYS; pub use flat::{ - DEFAULT_LOSSY_WEBP_QUALITY, apply_file_encoding, document_to_image, file_encoding, import_gif_frames, import_tiff_page, tga_alpha_channel_to_transparency, - tga_default_bits, + DEFAULT_LOSSY_WEBP_QUALITY, apply_file_encoding, document_to_image, file_encoding, import_gif_frames, import_tiff_page, import_webp_frames, + tga_alpha_channel_to_transparency, tga_default_bits, }; pub use psd_export::{PsdExportOptions, document_to_psd, document_to_psd_with}; pub use psd_import::{psd_to_document, psd_to_document_with}; @@ -91,6 +102,9 @@ pub enum IoError { /// OpenRaster read failure. #[error("OpenRaster: {0}")] Ora(String), + /// A GIMP XCF file that could not be read. + #[error("XCF: {0}")] + Xcf(String), /// PSD parse/write failure. #[error("PSD: {0}")] Psd(#[from] PsdError), @@ -112,6 +126,9 @@ pub enum IoError { /// An SVG that does not parse (or is too large to rasterise). #[error("SVG: {0}")] Svg(String), + /// The file could not be read. + #[error("{0}")] + Read(String), /// A background import or save was cancelled ([`import_with`], [`export_with`]). #[error("cancelled")] Cancelled, @@ -139,6 +156,34 @@ pub(crate) fn unequal_resolution_warning(x: f64, y: f64) -> Option { .then(|| format!("the vertical resolution ({y:.2} ppi) differs from the horizontal ({x:.2} ppi); the document keeps {x:.2} ppi for both")) } +/// Warnings about a loaded document's layers that render without their stored effect, for +/// sources that carry no import warnings of their own (a crafted `.pcraft`): a Color Lookup +/// whose table can't be applied silently renders as the identity instead (#1763). +pub fn open_warnings(doc: &Document) -> Vec { + let mut out = Vec::new(); + // Iterative, so no document's nesting can make this recurse. + let mut stack: Vec<&Layer> = doc.layers.iter().rev().collect(); + while let Some(l) = stack.pop() { + if let LayerContent::Adjustment(Adjustment::ColorLookup { name: _, size, lut, .. }) = &l.content { + match lut.as_deref().zip(photocraft_doc::adjust::lut3d_len(*size)) { + Some((table, len)) if table.len() >= len => {} + Some((table, len)) => out.push(format!( + "layer \"{}\": its Color Lookup table has {} values, but a {size}³ lookup needs {len}; the layer renders without the lookup", + l.name, + table.len() + )), + None => { + out.push(format!("layer \"{}\": its Color Lookup has no usable table for a {size}³ lookup; the layer renders without the lookup", l.name)) + } + } + } + if let Some(children) = l.children() { + stack.extend(children.iter().rev()); + } + } + out +} + /// Result of [`export`]. #[derive(Debug, Clone)] pub struct ExportResult { @@ -187,6 +232,14 @@ pub struct ExportOptions { /// File Compatibility (see [`PsdExportOptions::maximize_compatibility`]). On by default, as /// Photoshop's Always. pub psd_maximize_compatibility: bool, + /// The path of the document's own file, which linked smart objects that PSD/PSB and layered + /// TIFF embed are resolved against ([`doc_path::resolve`]). + pub document_path: Option, + /// Reuse one cache per open document for native saves; other formats ignore it. + pub pcraft_cache: Option, + /// PSD/PSB and layered TIFF: read and embed the files linked smart objects name (see + /// [`PsdExportOptions::read_linked_files`]). On by default; automation turns it off. + pub read_linked_files: bool, } impl Default for ExportOptions { @@ -199,6 +252,9 @@ impl Default for ExportOptions { tga_bits: None, image_previews: true, psd_maximize_compatibility: true, + document_path: None, + pcraft_cache: None, + read_linked_files: true, } } } @@ -231,11 +287,21 @@ impl TgaBits { } } +/// Photoshop document extensions File › Open routes to the PSD reader. +const PSD_EXTS: &[&str] = &["psd", "psb", "psdt"]; + /// `true` if `bytes` start with the PSD/PSB signature. pub fn is_psd(bytes: &[u8]) -> bool { bytes.starts_with(b"8BPS") } +/// PSD/PSB/PSDT by magic, or by a declared Photoshop extension when no other raster +/// signature is present. A PNG/JPEG/TIFF downloaded as `.psd` still opens from its bytes +/// (#5258 review). +fn is_psd_input(name: &str, bytes: &[u8]) -> bool { + is_psd(bytes) || (PSD_EXTS.iter().any(|e| has_extension(name, e)) && photocraft_codecs::detect(bytes).is_none()) +} + /// Imports a file. PSD/PSB, Affinity containers and camera raws are detected by magic; everything /// else is decoded with `photocraft-codecs`. pub fn import(name: &str, bytes: &[u8]) -> Result { @@ -261,34 +327,144 @@ pub fn import_with_svg_group_depth_and_interrupt( max_group_depth: usize, ctl: &photocraft_raster::Interrupt, ) -> Result { - ctl.check().map_err(|_| IoError::Cancelled)?; - let r = import_stages(name, bytes, ctl, max_group_depth)?; - ctl.check().map_err(|_| IoError::Cancelled)?; - ctl.progress(1.0); - Ok(r) + import_with_options_and_interrupt(name, bytes, &ImportOptions { max_group_depth, ..Default::default() }, ctl) } -fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, max_group_depth: usize) -> Result { +/// The user's settings that shape an import. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ImportOptions { + /// SVG groups nested deeper than this are rasterised. + pub max_group_depth: usize, + /// The demosaic method camera raws are developed with. + pub demosaic: raw::Demosaic, +} + +impl Default for ImportOptions { + fn default() -> Self { + ImportOptions { max_group_depth: photocraft_doc::MAX_GROUP_DEPTH, demosaic: raw::Demosaic::default() } + } +} + +/// [`import`] with the user's import settings. +pub fn import_with_options(name: &str, bytes: &[u8], opts: &ImportOptions) -> Result { + import_with_options_and_interrupt(name, bytes, opts, &photocraft_raster::Interrupt::NONE) +} + +/// [`import_with`] with the user's import settings. +pub fn import_with_options_and_interrupt(name: &str, bytes: &[u8], opts: &ImportOptions, ctl: &photocraft_raster::Interrupt) -> Result { + import_for_open_with(name, bytes, opts, ctl).map(|(result, _)| result) +} + +/// The ordinary import result plus whether PSD/PSB decoding encountered damaged data. +/// Interactive callers can ask whether to continue before adding the document. This is +/// information, not a save policy: accepting it opens an ordinary, writable document. +/// Non-interactive callers keep using [`import`], which still returns readable content +/// and warnings without requiring confirmation. +/// +/// The flag is independent of the wording of `warnings`. An ordinary compatibility +/// warning does not set it. No file is decoded a second time to obtain this information. +pub fn import_for_open(name: &str, bytes: &[u8], opts: &ImportOptions) -> Result<(ImportResult, bool), IoError> { + import_for_open_with(name, bytes, opts, &photocraft_raster::Interrupt::NONE) +} + +/// [`import_for_open`] with cancellation and progress, shared with [`import_with_options_and_interrupt`]. +pub fn import_for_open_with(name: &str, bytes: &[u8], opts: &ImportOptions, ctl: &photocraft_raster::Interrupt) -> Result<(ImportResult, bool), IoError> { + ctl.check().map_err(|_| IoError::Cancelled)?; + let mut damaged = false; + let result = import_stages(name, bytes, ctl, opts, &mut damaged)?; + ctl.check().map_err(|_| IoError::Cancelled)?; + ctl.progress(1.0); + Ok((result, damaged)) +} + +/// The PSD/PSB part of an import, from a parse that takes [`PsdFile::from_bytes_and_merged_recovering`]'s +/// `want` and `recover`: of the file's bytes, or of the file itself ([`import_file_for_open_with`]). +fn import_psd( + name: &str, + ctl: &photocraft_raster::Interrupt, + damaged: &mut bool, + parse: impl FnOnce(&dyn Fn(&PsdFile) -> bool, &dyn Fn(&PsdFile) -> bool) -> photocraft_psd::Result<(PsdFile, Option>>)>, +) -> Result { + // A merged image the import reads is decoded with the parse: a ZIP one is inflated once. + // A failed read of the file (one parsed from its source) is a read error, as reading the + // whole file first would report it. + let (file, merged) = + parse(&|f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f), &psd_import::can_recover_layers).map_err(|e| match e { + photocraft_psd::PsdError::Read(msg) => IoError::Read(msg), + e => e.into(), + })?; + // Nesting past the document model's cap could never be saved; reject the file with the + // actionable limit. + psd_import::check_group_depth(&file)?; + ctl.check().map_err(|_| IoError::Cancelled)?; + ctl.progress(0.05); + // Read before the conversion, which moves the global blocks into the document. + let txt2 = file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data)); + let (mut document, warnings, psd_damaged, read_error) = psd_import::psd_to_document_owned(file, ctl, merged).ok_or(IoError::Cancelled)?; + // The file became unreadable while its layers were read from it (`import_file_for_open_with`): + // the open fails, rather than opening with those layers empty. + if let Some(e) = read_error { + return Err(IoError::Read(e.to_string())); + } + *damaged = psd_damaged; + document.name = name.to_string(); + text_import::prepare(&mut document, txt2.as_ref()); + Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false }) +} + +/// [`import_for_open_with`] for the file at `path`. A PSD/PSB is parsed from the open file +/// ([`PsdFile::from_source`]) rather than read whole: its channel data is read layer by layer as +/// it is decoded, so opening it needs memory for the document, not for the document plus the +/// file. A read that fails meanwhile (the file is truncated or rewritten, its drive goes away) +/// fails the open. Other formats are read whole. +#[cfg(any(unix, windows))] +pub fn import_file_for_open_with( + name: &str, + path: &std::path::Path, + opts: &ImportOptions, + ctl: &photocraft_raster::Interrupt, +) -> Result<(ImportResult, bool), IoError> { + let read_failed = |e: std::io::Error| IoError::Read(format!("{}: {e}", path.display())); + ctl.check().map_err(|_| IoError::Cancelled)?; + let source = photocraft_psd::FileSource::open(path).map_err(read_failed)?; + let mut magic = [0u8; 4]; + let psd = !has_extension(name, photocraft_format::EXTENSION) && photocraft_psd::ReadAt::read_at(&source, 0, &mut magic).is_ok() && is_psd(&magic); + if !psd { + drop(source); + let bytes = photocraft_format::read_file(path).map_err(read_failed)?; + return import_for_open_with(name, &bytes, opts, ctl); + } + import_source_for_open_with(name, photocraft_psd::Source::new(source), ctl) +} + +/// [`import_file_for_open_with`] for a PSD/PSB in any [`photocraft_psd::ReadAt`] source. +pub fn import_source_for_open_with(name: &str, source: photocraft_psd::Source, ctl: &photocraft_raster::Interrupt) -> Result<(ImportResult, bool), IoError> { + ctl.check().map_err(|_| IoError::Cancelled)?; + let mut damaged = false; + let r = import_psd(name, ctl, &mut damaged, |want, recover| PsdFile::from_source_and_merged_recovering(source, want, recover))?; + ctl.check().map_err(|_| IoError::Cancelled)?; + ctl.progress(1.0); + Ok((r, damaged)) +} + +/// [`import_file_for_open_with`] without the damage flag. +#[cfg(any(unix, windows))] +pub fn import_file(name: &str, path: &std::path::Path, opts: &ImportOptions, ctl: &photocraft_raster::Interrupt) -> Result { + import_file_for_open_with(name, path, opts, ctl).map(|(result, _)| result) +} + +/// Dispatches format-specific importers and propagates a structured PSD damage +/// flag alongside the ordinary best-effort import result. +fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, opts: &ImportOptions, damaged: &mut bool) -> Result { // A declared native extension must reach its loader so malformed bundles retain format errors. if has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) { - return Ok(ImportResult { document: photocraft_format::load_from_bytes(bytes)?, warnings: Vec::new(), source_read_only: false, preview_only: false }); + let document = photocraft_format::load_from_bytes(bytes)?; + return Ok(ImportResult { warnings: open_warnings(&document), document, source_read_only: false, preview_only: false }); } + // Magic first: an Affinity file renamed .psd is still Affinity (#5258 corpus + // preview_never_saves_back_to_its_native_or_renamed_source). if is_psd(bytes) { - // A merged image the import reads is decoded with the parse: a ZIP one is inflated once. - let (file, merged) = PsdFile::from_bytes_and_merged_recovering( - bytes, - |f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f), - psd_import::can_recover_layers, - )?; - // Nesting past the document model's cap could never be saved; reject the file with the - // actionable limit. - psd_import::check_group_depth(&file)?; - ctl.check().map_err(|_| IoError::Cancelled)?; - ctl.progress(0.05); - let (mut document, warnings) = psd_import::psd_to_document_decoded(&file, ctl, merged).ok_or(IoError::Cancelled)?; - document.name = name.to_string(); - text_import::prepare(&mut document); - return Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false }); + return import_psd(name, ctl, damaged, |want, recover| PsdFile::from_bytes_and_merged_recovering(bytes, want, recover)); } if affinity::is_affinity(bytes) || affinity::has_extension(name) { return affinity::import(name, bytes); @@ -301,14 +477,27 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m if kra::is_kra(bytes) || (kra::EXTENSIONS.iter().any(|e| has_extension(name, e)) && photocraft_codecs::detect(bytes).is_none()) { return kra::import(name, bytes, ctl); } + // GIMP files are gzip or raw; as above, a recognized raster signature still wins over a + // misleading .xcf suffix. + if xcf::is_xcf(bytes) || (has_extension(name, "xcf") && photocraft_codecs::detect(bytes).is_none()) { + return xcf::import(name, bytes, ctl); + } + if xcf::is_gzipped_xcf(name, bytes) { + return xcf::import(name, &xcf::gunzip(bytes)?, ctl); + } if has_extension(name, "pdn") || bytes.starts_with(b"PDN3") { return pdn::import(name, bytes, ctl); } if raw::is_raw(bytes) { - return raw::import_raw(name, bytes); + return raw::import_raw_demosaic(name, bytes, opts.demosaic); } if is_svg_input(name, bytes) { - return svg::import_svg_with_group_depth(name, bytes, max_group_depth); + return svg::import_svg_with_group_depth(name, bytes, opts.max_group_depth); + } + // A `.psd`/`.psb`/`.psdt` with no raster signature keeps the PSD error instead of + // `codec: unrecognized image format` (#5258). Magic already returned above. + if is_psd_input(name, bytes) { + return import_psd(name, ctl, damaged, |want, recover| PsdFile::from_bytes_and_merged_recovering(bytes, want, recover)); } flat::import_flat(name, bytes) } @@ -393,6 +582,11 @@ fn export_stages(doc: &Document, name_or_ext: &str, opts: &ExportOptions, ctl: & if kra::EXTENSIONS.contains(&ext.as_str()) { return Err(IoError::Unsupported("Krita files are import-only; save as .ora to open the layers in Krita, or .pcraft to keep everything".into())); } + if ext == "xcf" { + return Err(IoError::Unsupported( + "XCF is GIMP's own format and PhotoCraft only opens it; save as .pcraft to keep the layers, or export a flat image".into(), + )); + } if ext == "ora" { return ora::export(doc); } @@ -408,10 +602,20 @@ fn export_stages(doc: &Document, name_or_ext: &str, opts: &ExportOptions, ctl: & ctl.check().map_err(|_| IoError::Cancelled)?; ctl.progress(PCRAFT_PREVIEWS_DONE); let previews = photocraft_format::SaveOptions { thumbnail, composite }; - return Ok(ExportResult { bytes: photocraft_format::save_to_bytes(doc.as_ref(), &previews)?, warnings: Vec::new() }); + let bytes = match &opts.pcraft_cache { + Some(cache) => cache.encode(doc.as_ref(), &previews)?, + None => photocraft_format::save_to_bytes(doc.as_ref(), &previews)?, + }; + return Ok(ExportResult { bytes, warnings: Vec::new() }); } if ext == "psd" || ext == "psb" { - let o = PsdExportOptions { force_psb: opts.force_psb || ext == "psb", maximize_compatibility: opts.psd_maximize_compatibility, ..Default::default() }; + let o = PsdExportOptions { + force_psb: opts.force_psb || ext == "psb", + maximize_compatibility: opts.psd_maximize_compatibility, + document_path: opts.document_path.clone(), + read_linked_files: opts.read_linked_files, + ..Default::default() + }; let (mut file, mut warnings) = with_span(ctl, 0.0, PSD_MODEL_DONE, |ctl| psd_export::document_to_psd_interruptible(doc, &o, ctl)); // A cancelled conversion stops early with an incomplete model: never write it. ctl.check().map_err(|_| IoError::Cancelled)?; @@ -525,7 +729,44 @@ pub fn merged_composite(file: &PsdFile) -> Result, IoError> { #[cfg(test)] mod tests { - use super::is_svg_input; + use super::{ImportOptions, import, import_for_open, is_psd_input, is_svg_input}; + use photocraft_psd::{Compression, Version, testgen}; + + /// Verifies a valid PSD remains editable after import. + #[test] + fn valid_psd_can_still_be_edited() { + let bytes = testgen::small(Version::Psd, Compression::Raw).to_bytes().unwrap(); + let imported = import("valid.psd", &bytes).unwrap(); + assert!(!imported.source_read_only); + } + + /// Verifies corrupt PSD channels produce warnings and an interactive damage flag + /// without rejecting otherwise readable headless imports. + #[test] + fn unreadable_psd_channel_keeps_best_effort_import_and_reports_damage() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + let channel = file.layers_mut()[0].channels.iter_mut().find(|c| c.id == 0).unwrap(); + channel.data.clear(); + let bytes = file.to_bytes().unwrap(); + let imported = import("damaged.psd", &bytes).unwrap(); + assert!(!imported.source_read_only); + assert!(imported.warnings.iter().any(|w| w.contains("could not be decoded"))); + let (opened, damaged) = import_for_open("damaged.psd", &bytes, &ImportOptions::default()).unwrap(); + assert!(damaged); + assert!(!opened.source_read_only); + assert!(!opened.preview_only); + assert_eq!(opened.warnings, imported.warnings); + assert_eq!(opened.document.layers.len(), imported.document.layers.len()); + } + + /// Verifies healthy PSD data never requires damage confirmation. + #[test] + fn valid_psd_does_not_request_damage_confirmation() { + let bytes = testgen::small(Version::Psd, Compression::Raw).to_bytes().unwrap(); + let (result, damaged) = import_for_open("valid.psd", &bytes, &ImportOptions::default()).unwrap(); + assert!(!damaged); + assert!(!result.source_read_only); + } #[test] fn svg_extension_does_not_override_another_format_signature() { @@ -535,4 +776,37 @@ mod tests { assert!(is_svg_input("drawing.svgz", &[0x1f, 0x8b, 0, 0])); assert!(!is_svg_input("drawing.svgz", webp_header)); } + + #[test] + fn photoshop_extension_reaches_the_psd_loader() { + // A declared .psd/.PSB/.psdt must not fall through to codecs (#5258). + for name in ["photo.psd", "dir/Photo.PSD", r"C:\w\a.PSB", "card.psdt"] { + assert!(is_psd_input(name, b"not a signature"), "{name}"); + } + assert!(!is_psd_input("photo.png", b"not a signature")); + assert!(is_psd_input("renamed.png", b"8BPS")); + let err = import("photo.psd", b"not a photoshop file").expect_err("named .psd is a PSD parse, not a codec miss"); + let msg = err.to_string(); + assert!(msg.starts_with("PSD:"), "{msg}"); + assert!(!msg.contains("unrecognized image format"), "{msg}"); + // A renamed Affinity file keeps the Affinity loader (magic before the .psd suffix). + assert!(!super::is_psd(b"\0\xffKA")); + } + + /// A common mislabeled download: PNG/JPEG bytes named `.psd` open from the signature, + /// as on main and in Photoshop. + #[test] + fn raster_bytes_named_psd_still_open() { + use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image}; + let n = 8 * 8 * 3; + let img = Image::from_u8(8, 8, ChannelLayout::Rgb, vec![180; n]).unwrap(); + let png = photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let jpeg = photocraft_codecs::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + for (name, bytes) in [("download.psd", png.as_slice()), ("scan.PSB", jpeg.as_slice())] { + assert!(!is_psd_input(name, bytes), "{name}"); + let d = import(name, bytes).unwrap_or_else(|e| panic!("{name}: {e}")).document; + assert_eq!(d.bounds().width(), 8, "{name}"); + assert_eq!(d.bounds().height(), 8, "{name}"); + } + } } diff --git a/crates/io/src/linked.rs b/crates/io/src/linked.rs index e200b875ea..dcf7ecd98d 100644 --- a/crates/io/src/linked.rs +++ b/crates/io/src/linked.rs @@ -118,7 +118,30 @@ pub fn find_in_blocks<'a>(blocks: impl IntoIterator(blocks: impl IntoIterator, uuid: &str) -> Option<(String, String, &'a [u8])> { + if uuid.is_empty() { + return None; + } + blocks.into_iter().filter(|(k, _)| matches!(*k, b"lnk2" | b"lnk3" | b"lnkD")).find_map(|(_, d)| { + let mut r = Rd { d, p: 0 }; + while let Some(len) = r.u64() { + let Ok(len) = usize::try_from(len) else { return None }; + let item = r.take(len)?; + let found = item_header(item).filter(|h| h.0 == uuid).and_then(|(id, name, start, n)| Some((id, name, item.get(start..start.checked_add(n)?)?))); + if found.is_some() { + return found; + } + r.p = r.p.next_multiple_of(4); + } + None + }) } /// The size of embedded file `uuid` among PSD global blocks given as (key, data), when it is there @@ -128,11 +151,11 @@ pub(crate) fn source_size_in_blocks<'a>( uuid: &str, ctl: &photocraft_raster::Interrupt, ) -> Option<(f64, f64)> { - let f = find_in_blocks(blocks, uuid)?; + let (_, _, bytes) = find_slice_in_blocks(blocks, uuid)?; if ctl.cancelled() { return None; } - source_size_probe(&f.bytes).filter(|(w, h)| *w > 0.0 && *h > 0.0) + source_size_probe(bytes).filter(|(w, h)| *w > 0.0 && *h > 0.0) } /// Pixel size of an embedded source file from its header: PSD/PSB, TIFF (the default page's diff --git a/crates/io/src/multichannel_map.rs b/crates/io/src/multichannel_map.rs index db31e743ef..2b6f4b9b54 100644 --- a/crates/io/src/multichannel_map.rs +++ b/crates/io/src/multichannel_map.rs @@ -3,8 +3,9 @@ //! A Multichannel document has no layers: its image is a set of ink (spot) channels, stored in //! [`Document::channels`] with ink density as the value (1 = solid ink, like every spot channel). //! A PSD stores the same planes the way Photoshop shows them in the Channels panel: 0 (black) = -//! solid ink, max = paper. Channel names come from resources 1006 / 1045 and ink colours from -//! DisplayInfo (1077), exactly as for spot channels in the other modes. +//! solid ink, max = paper. Alpha-type channels (DisplayInfo kind 0 / 1) are stored as-is, like the +//! alpha channels of every other mode (max = selected). Channel names come from resources 1006 / +//! 1045 and ink colours from DisplayInfo (1077), exactly as for spot channels in the other modes. use photocraft_color::{Color, ColorMode, PixelFormat, SampleType}; use photocraft_doc::{AlphaChannel, Document}; @@ -61,10 +62,20 @@ pub(crate) fn import(file: &PsdFile, all: &[u8], doc: &mut Document, names: &[St } else if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO_OLD) { crate::channel_map::apply_display_info(&r.data, false, 0, &mut doc.channels); } + // Alpha-type planes are stored as-is, not as ink: undo the inversion for them. + for ch in doc.channels.iter_mut().filter(|c| c.spot.is_none()) { + let vals: Vec = ch.surface.read_region(canvas).chunks_exact(ch.surface.channels()).map(|v| 1.0 - v[0]).collect(); + let mut s = Surface::new(fmt); + s.write_region(canvas, &vals); + s.prune(); + ch.surface = s; + } } /// Writes a Multichannel document as a mode-7 PSD (no layer section; every channel a plane). -pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec) { +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> (PsdFile, Vec) { let mut warnings = Vec::new(); // Photoshop's Multichannel mode is 8 or 16 bits per channel. let sample = match doc.depth { @@ -90,22 +101,21 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< } } for ch in &channels { + // Inks are stored as tone (0 = solid ink), alpha-type channels as-is. + let ink = ch.spot.is_some(); for v in ch.surface.read_region(canvas).chunks_exact(ch.surface.channels()) { - encode_be(1.0 - v[0], sample, &mut planes); + encode_be(if ink { 1.0 - v[0] } else { v[0] }, sample, &mut planes); } } let big = doc.size.width > 30_000 || doc.size.height > 30_000; - let size_exceeds_limit = !force_psb && !big && crate::psd_export::psd_size_exceeds_limit(doc); - let version = crate::psd_export::psd_version(doc, force_psb, size_exceeds_limit); + let version = crate::psd_export::psd_version(doc, force_psb); if big && !force_psb { warnings.push("document exceeds 30000 px; written as PSB".into()); - } else if size_exceeds_limit && !force_psb { - warnings.push("estimated encoded size exceeds 2 GB; written as PSB".into()); } let header = Header::new(version, doc.size.width, doc.size.height, channels.len().max(1) as u16, psd_depth(sample), PsdMode::Multichannel); let image_data = ImageData::encode(Compression::Rle, &planes, &header) .or_else(|_| ImageData::encode(Compression::Raw, &planes, &header)) - .unwrap_or(ImageData { compression: Compression::Raw, data: planes }); + .unwrap_or(ImageData::new(Compression::Raw, planes)); let mut resources = vec![ ImageResource::new(ids::RESOLUTION_INFO, ResolutionInfo::from_dpi(f64::from(doc.resolution_dpi)).to_bytes()), ImageResource::new(ids::GLOBAL_ANGLE, (doc.global_light.angle.round() as i32).to_be_bytes().to_vec()), @@ -119,7 +129,30 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< resources.push(ImageResource::new(1045, crate::psd_export::unicode_names_resource(&names))); resources.push(ImageResource::new(crate::channel_map::DISPLAY_INFO, crate::channel_map::display_info(&channels, false))); } - resources.extend(crate::psd_export::path_resources(doc, &mut warnings)); + // Alpha identifiers: one u32 per channel, only while they still match the channels. + let keep = |id: u16, data: &[u8]| id != ALPHA_IDENTIFIERS || data.len() == channels.len().saturating_mul(4); + let color_mode_data = photocraft_psd::hdr::color_mode_data_for_depth(header.depth); + let file = layerless_file(doc, header, image_data, color_mode_data, resources, keep, size_limit, &mut warnings); + (file, warnings) +} + +/// A PSD without layers around `image_data`: `resources` (the mode's own) plus the paths, XMP, +/// EXIF and every other imported resource `keep` accepts (except the ones that only describe +/// layers and any already in `resources`), and the document-level tagged blocks. Shared by the +/// modes that have no layers in Photoshop: Multichannel, Indexed Color and Bitmap. A file past +/// `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps the version. +#[allow(clippy::too_many_arguments)] +pub(crate) fn layerless_file( + doc: &Document, + header: Header, + image_data: ImageData, + color_mode_data: Vec, + mut resources: Vec, + keep: impl Fn(u16, &[u8]) -> bool, + size_limit: Option, + warnings: &mut Vec, +) -> PsdFile { + resources.extend(crate::psd_export::path_resources(doc, warnings)); // The XMP and EXIF resolution follow the ResolutionInfo resource, so no copy contradicts it. let ppi = Some((doc.resolution_dpi, doc.resolution_dpi)).filter(|d| d.0 > 0.0); if let Some(x) = &doc.metadata.xmp { @@ -132,11 +165,7 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< // ones that only describe layers and any already written above. let written: Vec = resources.iter().map(|r| r.id).collect(); for (id, name, data) in &doc.metadata.psd_resources { - if LAYER_ONLY_RESOURCES.contains(id) || written.contains(id) || !crate::psd_export::keep_imported_resource(doc, *id, data) { - continue; - } - // Alpha identifiers: one u32 per channel, only while they still match the channels. - if *id == ALPHA_IDENTIFIERS && data.len() != channels.len().saturating_mul(4) { + if LAYER_ONLY_RESOURCES.contains(id) || written.contains(id) || !crate::psd_export::keep_imported_resource(doc, *id, data) || !keep(*id, data) { continue; } let mut r = ImageResource::new(*id, data.to_vec()); @@ -148,14 +177,14 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< } resources.push(version_info_resource(true)); // Document-level tagged blocks (patterns, notes, ...) are valid without a layer section; a - // kept layer block would describe layers the document doesn't have. + // kept layer block would describe layers the document doesn't have. Their signatures follow + // the version the file is written as, known once its size is. let global_blocks: Vec = crate::annotations_map::export_blocks(doc, crate::pattern_map::export_global_blocks(doc)) .into_iter() .filter(|(_, key, _)| !matches!(key, b"Layr" | b"Lr16" | b"Lr32")) .map(|(sig, key, data)| crate::psd_export::global_block(sig, key, &data)) .collect(); - let color_mode_data = photocraft_psd::hdr::color_mode_data_for_depth(header.depth); - let file = PsdFile { + let mut file = PsdFile { header, color_mode_data, resources, @@ -165,6 +194,13 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool) -> (PsdFile, Vec< global_blocks, layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, }; - (file, warnings) + if let Some(limit) = size_limit + && let Some(warning) = crate::psd_export::fit_psd_size(&mut file, limit) + { + warnings.push(warning); + } + crate::psd_export::sign_global_blocks(&mut file); + file } diff --git a/crates/io/src/ora.rs b/crates/io/src/ora.rs index f365154333..fb7968fbcc 100644 --- a/crates/io/src/ora.rs +++ b/crates/io/src/ora.rs @@ -77,9 +77,9 @@ pub(crate) fn krita_blend(id: &str) -> Option<(BlendMode, bool)> { "dodge" => (B::ColorDodge, false), "burn" => (B::ColorBurn, false), "hard_light" => (B::HardLight, false), - "soft_light" => (B::SoftLight, false), - // The W3C soft light curve, close to but not Photoshop's. - "soft_light_svg" => (B::SoftLight, true), + // Krita's own Soft Light keeps sqrt(cb) over dark backdrops; Photoshop's is the W3C curve. + "soft_light" => (B::SoftLight, true), + "soft_light_svg" => (B::SoftLight, false), "diff" => (B::Difference, false), "exclusion" => (B::Exclusion, false), "linear_burn" => (B::LinearBurn, false), @@ -124,7 +124,7 @@ pub(crate) fn krita_id(mode: BlendMode) -> Option<&'static str> { B::LinearDodge => "linear_dodge", B::LighterColor => "lighter color", B::Overlay => "overlay", - B::SoftLight => "soft_light", + B::SoftLight => "soft_light_svg", B::HardLight => "hard_light", B::VividLight => "vivid_light", B::LinearLight => "linear light", @@ -167,9 +167,6 @@ const SVG_OPS: [(&str, BlendMode); 15] = [ /// A composite-op attribute as a blend mode; unknown names fall back to Normal with a warning. fn composite_op(op: &str, layer: &str, warnings: &mut Warnings) -> BlendMode { if let Some((_, mode)) = SVG_OPS.iter().find(|(name, _)| *name == op) { - if op == "svg:soft-light" { - warnings.note("svg:soft-light (the W3C curve) opens as Photoshop's Soft Light, which differs slightly"); - } return *mode; } match op { @@ -417,7 +414,7 @@ pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result root")); @@ -523,6 +520,20 @@ fn png(w: u32, h: u32, px: &[[f32; 4]], sample: CSample) -> Result> { /// `px` (row-major over `rect`) cut down to the pixels that aren't fully transparent, with /// transparent pixels zeroed, so masked-out or empty margins aren't written. +/// Most bytes of straight RGBA floats one layer may take while it is written. A layer is one PNG +/// of its content's bounding box, so a few pixels far apart make a box of mostly nothing; refuse +/// that rather than allocate it. 1.5 × [`MAX_ENTRY`] of floats is at most 8 B/px × a third of that +/// raw, so the PNG stays within what [`Reader::png`] will read back. +const MAX_EXPORT_LAYER_BYTES: u64 = 3 * MAX_ENTRY as u64 / 2; + +fn admit_layer(rect: Rect) -> Result<()> { + let bytes = u64::from(rect.width()).saturating_mul(u64::from(rect.height())).saturating_mul(std::mem::size_of::<[f32; 4]>() as u64); + if bytes > MAX_EXPORT_LAYER_BYTES { + return Err(IoError::Unsupported(format!("a {}×{} layer is too large to save to OpenRaster", rect.width(), rect.height()))); + } + Ok(()) +} + fn crop(rect: Rect, mut px: Vec<[f32; 4]>) -> (Rect, Vec<[f32; 4]>) { let w = rect.width() as usize; if w == 0 { @@ -566,6 +577,15 @@ impl Writer<'_> { if layer.locks.all || layer.locks.pixels { attrs.push_str(" edit-locked=\"true\""); } + // OpenRaster's one edit lock reopens as Lock All; it has no position lock (#7018). + if layer.locks.pixels && !layer.locks.all { + self.warnings.layer(&layer.name, "Lock Image Pixels was saved as OpenRaster's edit lock; it reopens as Lock All"); + } + // The Background's position lock is part of being the Background, not one the user set: + // every new document has it, and OpenRaster has no Background to keep it on. + if layer.locks.position && !layer.locks.all && !self.doc.is_background(layer.id) { + self.warnings.layer(&layer.name, "Lock Position is not saved to OpenRaster"); + } if layer.locks.transparency { attrs.push_str(" alpha-preserve=\"true\""); } @@ -603,11 +623,12 @@ impl Writer<'_> { } /// The layer's own pixels (straight RGBA floats) and their bounds; its pixel mask applied. - fn pixels(&mut self, layer: &Layer) -> (Rect, Vec<[f32; 4]>) { + fn pixels(&mut self, layer: &Layer) -> Result<(Rect, Vec<[f32; 4]>)> { let canvas = self.doc.bounds(); let (rect, mut buf) = match &layer.content { LayerContent::Raster(s) => { let r = s.content_bounds(); + admit_layer(r)?; (r, photocraft_compose::surface_to_buffer(s, r).px) } _ => { @@ -621,6 +642,7 @@ impl Writer<'_> { solo.mask = None; solo.effects = Default::default(); let r = photocraft_compose::composite_bounds(&solo, canvas).unwrap_or(canvas).intersect(&canvas); + admit_layer(r)?; (r, photocraft_compose::render_layer(&solo, r).px) } }; @@ -636,7 +658,7 @@ impl Writer<'_> { p[3] *= layer.fill_opacity.clamp(0.0, 1.0); } } - crop(rect, buf) + Ok(crop(rect, buf)) } fn layers(&mut self, layers: &[Layer], depth: usize) -> Result<()> { @@ -665,7 +687,7 @@ impl Writer<'_> { if self.dry { continue; } - let (rect, px) = self.pixels(layer); + let (rect, px) = self.pixels(layer)?; let (rect, px) = if rect.is_empty() { (Rect::from_xywh(0, 0, 1, 1), vec![[0.0; 4]]) } else { (rect, px) }; let src = format!("data/layer{}.png", self.next); self.next += 1; diff --git a/crates/io/src/ora/tests.rs b/crates/io/src/ora/tests.rs index 4f0b1c4c47..e4797fc52d 100644 --- a/crates/io/src/ora/tests.rs +++ b/crates/io/src/ora/tests.rs @@ -195,6 +195,16 @@ fn archive(stack: &str, images: &[(&str, Vec)]) -> Vec { z.finish().unwrap() } +/// A stack.xml nested 20 000 deep overflowed the XML parser's recursion (an abort nothing can +/// catch); it is an error now. The small stack makes a regression abort instead of passing. +#[test] +fn deeply_nested_stack_xml_is_an_error_not_a_stack_overflow() { + let stack = format!("{}{}", "".repeat(20_000), "".repeat(20_000)); + let bytes = archive(&stack, &[]); + let e = std::thread::Builder::new().stack_size(512 * 1024).spawn(move || crate::import("deep.ora", &bytes).err()).unwrap().join().unwrap(); + assert!(matches!(&e, Some(IoError::Ora(m)) if m.contains("nested")), "{e:?}"); +} + fn solid_png(w: u32, h: u32, rgba: [u8; 4]) -> Vec { let data = rgba.iter().copied().cycle().take((w * h * 4) as usize).collect(); codecs::encode(&Image::from_raw(w, h, ChannelLayout::Rgba, CSample::U8, data).unwrap(), Format::Png, &Default::default()).unwrap() @@ -327,9 +337,45 @@ fn reads_other_applications_stacks() { assert_eq!(get("Strange").blend, BlendMode::Normal); assert!(get("Lost").surface().unwrap().content_bounds().is_empty()); let all = r.warnings.join("\n"); - for needle in ["svg:dst-in", "", "missing from the archive", "krita:hard mix", "svg:soft-light"] { + for needle in ["svg:dst-in", "", "missing from the archive", "krita:hard mix"] { assert!(all.contains(needle), "{needle} not in {all}"); } + // svg:soft-light is the W3C curve, which is Photoshop's Soft Light (#8804): no warning. + assert!(!all.contains("soft-light"), "{all}"); +} + +#[test] +fn krita_soft_light_ids_map_to_photoshop_soft_light() { + // Krita's own soft_light keeps sqrt(cb) over dark backdrops, so it is the approximation; + // soft_light_svg is the W3C curve Photoshop uses (#8804). + assert_eq!(super::krita_blend("soft_light"), Some((BlendMode::SoftLight, true))); + assert_eq!(super::krita_blend("soft_light_svg"), Some((BlendMode::SoftLight, false))); + assert_eq!(super::krita_id(BlendMode::SoftLight), Some("soft_light_svg")); + let xml = r#" + + + "#; + let r = crate::import("soft.ora", &archive(xml, &[("a.png", solid_png(2, 2, [40, 40, 40, 255]))])).unwrap(); + assert!(r.document.layers.iter().all(|l| l.blend == BlendMode::SoftLight)); + let all = r.warnings.join("\n"); + assert!(all.contains("krita:soft_light blend mode is approximated"), "{all}"); + assert!(!all.contains("soft_light_svg"), "{all}"); +} + +#[test] +fn soft_light_exports_as_svg_soft_light() { + // The W3C id, not krita:soft_light_svg (which import also accepts): other applications read it. + let mut d = doc(SampleType::U8); + let mut soft = block("Soft", PixelFormat::RGBA8, 0, 0, 10, 10, 5); + soft.blend = BlendMode::SoftLight; + d.layers.push(soft); + let out = export(&d).unwrap(); + let xml = String::from_utf8(ZipReader::new(&out.bytes).unwrap().read_by_name("stack.xml", 1 << 20).unwrap()).unwrap(); + assert!(xml.contains("name=\"Soft\"") && xml.contains("composite-op=\"svg:soft-light\""), "{xml}"); + assert!(!xml.contains("soft_light"), "{xml}"); + let (back, exported, imported) = round_trip(&d); + assert!(back.layers.iter().any(|l| l.name == "Soft" && l.blend == BlendMode::SoftLight)); + assert!(!exported.iter().chain(&imported).any(|w| w.to_lowercase().contains("soft")), "{exported:?} {imported:?}"); } #[test] @@ -494,6 +540,9 @@ fn lossy_documents() -> Vec<(&'static str, Document)> { }), ("clipped", with(&|d| d.layers[1].clipped = true)), ("fill opacity", with(&|d| d.layers[0].fill_opacity = 0.5)), + ("pixel lock", with(&|d| d.layers[0].locks.pixels = true)), + // Not the bottom layer: with its transparency lock, `Base` would become the Background. + ("position lock", with(&|d| d.layers[1].locks.position = true)), ("float", doc(SampleType::F32)), ("profile", with(&|d| d.icc_profile = Some(photocraft_cms::Builtin::DisplayP3.profile().to_bytes()))), ("metadata", with(&|d| d.metadata.xmp = Some("".into()))), @@ -520,6 +569,15 @@ fn plain_documents_lose_nothing() { d.icc_profile = Some(photocraft_cms::Builtin::Srgb.profile().to_bytes()); assert_eq!(losses(&d), Vec::::new(), "{depth:?} tagged sRGB"); assert_eq!(export(&d).unwrap().warnings, Vec::::new()); + // OpenRaster's edit-locked and alpha-preserve hold Lock All and Lock Transparent Pixels. + d.layers[0].locks.all = true; + d.layers[1].locks.transparency = true; + assert_eq!(losses(&d), Vec::::new(), "{depth:?} locked"); + // A Background (every new document's bottom layer) loses nothing for its position lock. + d.layers[0].locks.all = false; + d.layers[0].locks.position = true; + assert!(d.background().is_some()); + assert_eq!(losses(&d), Vec::::new(), "{depth:?} Background"); } let mut cmyk = doc(SampleType::U8); cmyk.mode = ColorMode::Cmyk; diff --git a/crates/io/src/palette_map.rs b/crates/io/src/palette_map.rs new file mode 100644 index 0000000000..e1729dcce0 --- /dev/null +++ b/crates/io/src/palette_map.rs @@ -0,0 +1,289 @@ +//! Indexed Color and Bitmap documents -> PSD colour modes 2 and 0. +//! +//! Neither mode has layers in Photoshop, so the file holds only the merged image (the document +//! flattened): for Indexed Color one plane of palette indices, with the Color Table as Color Mode +//! Data (768 bytes: 256 reds, 256 greens, 256 blues), its length in resource 1046 and the +//! transparent entry in resource 1047; for Bitmap one plane of 1 bit per pixel, 1 = black. + +use photocraft_color::ColorMode; +use photocraft_doc::{ColorTable, Document}; +use photocraft_psd::resources::{ImageResource, ResolutionInfo, ids}; +use photocraft_psd::{ColorMode as PsdMode, Compression, Header, ImageData, PsdFile}; + +/// Indexed Color Table Count: the number of colours in the table (u16). +pub(crate) const COLOR_TABLE_COUNT: u16 = 1046; +/// Transparency Index: the colour table entry shown as transparent (u16). +pub(crate) const TRANSPARENCY_INDEX: u16 = 1047; +/// Channel resources (names, display info, alpha identifiers): these modes have no extra channels. +const CHANNEL_RESOURCES: [u16; 4] = [1006, 1045, 1053, crate::channel_map::DISPLAY_INFO]; + +/// The PSD for an Indexed Color document with a colour table of at most 256 entries, one of them +/// opaque, or a Bitmap document; None for any other document (it is written like the others). A +/// document with alpha channels or a Quick Mask is written like the others too: the importer does +/// not read the extra channels of these modes back, so they would be lost. +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> Option<(PsdFile, Vec)> { + if !doc.channels.is_empty() || doc.quick_mask.is_some() { + return None; + } + let table = doc.color_table.as_ref().filter(|t| t.colors.len() <= 256 && (0..t.colors.len()).any(|i| Some(i) != t.transparent.map(usize::from))); + let (mode, plane, color_mode_data) = match doc.mode { + ColorMode::Indexed => { + let table = table?; + (PsdMode::Indexed, indices(doc, table), palette(table)) + } + ColorMode::Bitmap => (PsdMode::Bitmap, bits(doc), Vec::new()), + _ => return None, + }; + let mut warnings = Vec::new(); + if !flattens_losslessly(doc) { + warnings.push(format!("{:?} documents have no layers in Photoshop: written flattened", doc.mode)); + } + let version = crate::psd_export::psd_version(doc, force_psb); + let depth = if mode == PsdMode::Bitmap { 1 } else { 8 }; + let header = Header::new(version, doc.size.width, doc.size.height, 1, depth, mode); + let image_data = ImageData::encode(Compression::Rle, &plane, &header) + .or_else(|_| ImageData::encode(Compression::Raw, &plane, &header)) + .unwrap_or(ImageData::new(Compression::Raw, plane)); + let mut resources = vec![ + ImageResource::new(ids::RESOLUTION_INFO, ResolutionInfo::from_dpi(f64::from(doc.resolution_dpi)).to_bytes()), + ImageResource::new(ids::GLOBAL_ANGLE, (doc.global_light.angle.round() as i32).to_be_bytes().to_vec()), + ImageResource::new(ids::GLOBAL_ALTITUDE, (doc.global_light.altitude.round() as i32).to_be_bytes().to_vec()), + ImageResource::new(1032, crate::psd_export::guides_resource(doc)), + ]; + if let (PsdMode::Indexed, Some(t)) = (mode, table) { + resources.push(ImageResource::new(COLOR_TABLE_COUNT, (t.colors.len() as u16).to_be_bytes().to_vec())); + if let Some(i) = t.transparent { + resources.push(ImageResource::new(TRANSPARENCY_INDEX, u16::from(i).to_be_bytes().to_vec())); + } + // Photoshop keeps the RGB profile of an Indexed document (Bitmap has none). A profile of + // another colour space (kept from a Grayscale or CMYK source) does not describe the table. + if let Some(icc) = doc.icc_profile.as_ref().filter(|icc| icc.get(16..20) == Some(b"RGB ".as_slice())) { + resources.push(ImageResource::new(ids::ICC_PROFILE, icc.to_vec())); + } + } + // An imported transparency index that no longer applies is not copied either. + let keep = |id: u16, _: &[u8]| !CHANNEL_RESOURCES.contains(&id) && id != TRANSPARENCY_INDEX && id != COLOR_TABLE_COUNT; + let file = crate::multichannel_map::layerless_file(doc, header, image_data, color_mode_data, resources, keep, size_limit, &mut warnings); + Some((file, warnings)) +} + +/// Whether the merged image holds everything the layers do: no layer, or a single visible pixel +/// layer at full opacity, Normal blend, unclipped, without masks or layer effects. Anything else +/// (more layers, a type, shape or smart object layer, a mask, a hidden layer) is lost when the +/// document is written flattened. +fn flattens_losslessly(doc: &Document) -> bool { + match doc.layers.as_slice() { + [] => true, + [l] => { + l.visible + && matches!(l.content, photocraft_doc::LayerContent::Raster(_)) + && l.mask.is_none() + && l.vector_mask.is_none() + && !l.clipped + && l.opacity >= 1.0 + && l.fill_opacity >= 1.0 + && l.blend == photocraft_color::BlendMode::Normal + && !(l.effects.enabled && l.effects.items.iter().any(photocraft_doc::Effect::enabled)) + } + _ => false, + } +} + +/// The Color Table as Color Mode Data: 256 reds, 256 greens, 256 blues (unused entries black). +fn palette(t: &ColorTable) -> Vec { + let mut m = vec![0u8; 768]; + for (i, c) in t.colors.iter().take(256).enumerate() { + m[i] = c[0]; + m[256 + i] = c[1]; + m[512 + i] = c[2]; + } + m +} + +/// The flattened document as palette indices: the transparent entry where the document is less +/// than half covered, else the nearest other entry (the transparent one may duplicate an opaque +/// colour, as in many PNG-8 files). Without a transparent entry the document is flattened over +/// white, as Photoshop does. +fn indices(doc: &Document, t: &ColorTable) -> Vec { + let (opaque_indices, colors): (Vec, Vec<_>) = + t.colors.iter().copied().enumerate().filter(|(i, _)| Some(*i) != t.transparent.map(usize::from)).map(|(i, c)| (i as u8, c)).unzip(); + let opaque = ColorTable { colors, transparent: None }; + let mut idx = Vec::with_capacity(doc.size.area() as usize); + let _ = photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> { + idx.extend(band.px.iter().map(|p| match t.transparent { + Some(i) if p[3] < 0.5 => i, + Some(_) => opaque_indices.get(opaque.nearest([p[0], p[1], p[2]])).copied().unwrap_or(0), + None => { + let over_white = |c: f32| c * p[3] + (1.0 - p[3]); + t.nearest([over_white(p[0]), over_white(p[1]), over_white(p[2])]) as u8 + } + })); + Ok(()) + }); + idx +} + +/// The flattened document over white as 1-bit rows (padded to a byte), 1 = black: pixels darker +/// than middle grey. +fn bits(doc: &Document) -> Vec { + let w = doc.size.width as usize; + let row = w.div_ceil(8); + let mut out = Vec::with_capacity(row * doc.size.height as usize); + let _ = photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> { + for line in band.px.chunks(w.max(1)) { + let mut bytes = vec![0u8; row]; + for (x, p) in line.iter().enumerate() { + let grey = (p[0] + p[1] + p[2]) / 3.0 * p[3] + (1.0 - p[3]); + if grey < 0.5 { + bytes[x / 8] |= 0x80 >> (x % 8); + } + } + out.extend(bytes); + } + Ok(()) + }); + out +} + +#[cfg(test)] +mod tests { + use super::*; + use photocraft_color::{Color, SampleType}; + use photocraft_doc::Size; + + fn document(width: u32, height: u32, depth: SampleType, mode: ColorMode, pixels: &[f32]) -> Document { + let mut doc = Document::with_background("palette test", Size::new(width, height), ColorMode::Rgb, depth, Color::rgba(0.0, 0.0, 0.0, 1.0)); + let bounds = doc.bounds(); + doc.layers[0].surface_mut().unwrap().write_region(bounds, pixels); + doc.mode = mode; + doc + } + + fn round_trip(doc: &Document) -> (PsdFile, Document) { + let (file, _) = document_to_psd(doc, false, None).unwrap(); + let bytes = file.to_bytes().unwrap(); + let file = PsdFile::from_bytes(&bytes).unwrap(); + let back = crate::import("test.psd", &bytes).unwrap().document; + (file, back) + } + + #[test] + fn full_palette_endpoints_round_trip_with_either_transparent_endpoint() { + let colors: Vec<_> = (0..=255).map(|v| [v, 255 - v, v / 2]).collect(); + for transparent in [0, 255] { + let opaque = 255 - transparent; + let rgb = colors[usize::from(opaque)].map(|v| f32::from(v) / 255.0); + let mut doc = document(2, 1, SampleType::U8, ColorMode::Indexed, &[rgb[0], rgb[1], rgb[2], 1.0, 0.0, 0.0, 0.0, 0.0]); + doc.color_table = Some(ColorTable { colors: colors.clone(), transparent: Some(transparent) }); + let (file, back) = round_trip(&doc); + assert_eq!((file.header.color_mode, file.header.depth, file.header.channels), (PsdMode::Indexed, 8, 1)); + assert_eq!(file.decode_merged().unwrap(), [opaque, transparent]); + assert_eq!(file.resource(COLOR_TABLE_COUNT).unwrap().data, [1, 0]); + assert_eq!(file.color_mode_data.len(), 768); + for (i, c) in colors.iter().enumerate() { + assert_eq!([file.color_mode_data[i], file.color_mode_data[256 + i], file.color_mode_data[512 + i]], *c); + } + assert_eq!(back.color_table, doc.color_table); + let surface = back.layers[0].surface().unwrap(); + assert_eq!(surface.pixel(0, 0), vec![rgb[0], rgb[1], rgb[2], 1.0]); + assert_eq!(surface.pixel(1, 0)[3], 0.0); + } + } + + #[test] + fn indexed_alpha_threshold_uses_stored_samples_at_all_depths() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut doc = document(3, 1, depth, ColorMode::Indexed, &[0.0, 0.0, 0.0, 0.49, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.51]); + doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: Some(1) }); + let (file, back) = round_trip(&doc); + assert_eq!(file.decode_merged().unwrap(), [1, 0, 0], "{depth:?}"); + let surface = back.layers[0].surface().unwrap(); + assert_eq!((0..3).map(|x| surface.pixel(x, 0)[3]).collect::>(), [0.0, 1.0, 1.0]); + } + } + + #[test] + fn small_palette_zero_pads_unused_entries_and_rejects_invalid_lengths() { + let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[1.0, 0.0, 0.0, 1.0]); + doc.color_table = Some(ColorTable { colors: vec![[255, 0, 0]], transparent: None }); + let (file, back) = round_trip(&doc); + assert_eq!(file.color_mode_data[0], 255); + assert!(file.color_mode_data[1..].iter().all(|&v| v == 0)); + assert_eq!(back.color_table, doc.color_table); + for count in [0, 257] { + doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0]; count], transparent: None }); + assert!(document_to_psd(&doc, false, None).is_none(), "{count}"); + } + doc.color_table = None; + assert!(document_to_psd(&doc, false, None).is_none()); + } + + #[test] + fn indexed_import_bounds_malformed_count_and_transparency_resources() { + let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[0.0, 0.0, 0.0, 1.0]); + doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: None }); + let (original, _) = document_to_psd(&doc, false, None).unwrap(); + for (count, expected) in [(vec![0, 0], 1), (vec![1, 1], 256), (vec![255, 255], 256), (vec![0], 256)] { + let mut file = original.clone(); + file.resources.retain(|r| r.id != COLOR_TABLE_COUNT); + file.resources.push(ImageResource::new(COLOR_TABLE_COUNT, count)); + file.resources.push(ImageResource::new(TRANSPARENCY_INDEX, vec![255, 255])); + let bytes = file.to_bytes().unwrap(); + let back = crate::import("malformed.psd", &bytes).unwrap().document; + let table = back.color_table.unwrap(); + assert_eq!(table.colors.len(), expected); + assert_eq!(table.transparent, None); + assert_eq!(back.layers[0].surface().unwrap().pixel(0, 0)[3], 1.0); + } + } + + #[test] + fn bitmap_rows_are_msb_first_and_independently_byte_padded() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for width in [1u32, 7, 8, 9, 15, 17] { + // Opposite rows expose padding carried into the next row or reversed bit order. + let pixels: Vec<_> = (0..2) + .flat_map(|y| { + (0..width).flat_map(move |x| { + let v = if (x + y) % 2 == 0 { 0.0 } else { 1.0 }; + [v, v, v, 1.0] + }) + }) + .collect(); + let doc = document(width, 2, depth, ColorMode::Bitmap, &pixels); + let (file, back) = round_trip(&doc); + assert_eq!((file.header.color_mode, file.header.depth, file.header.channels), (PsdMode::Bitmap, 1, 1)); + let mut expected = Vec::new(); + for y in 0..2 { + for byte in 0..width.div_ceil(8) { + let mut packed = 0; + for bit in 0..8 { + let x = byte * 8 + bit; + if x < width && (x + y) % 2 == 0 { + packed |= 0x80 >> bit; + } + } + expected.push(packed); + } + } + assert_eq!(file.decode_merged().unwrap(), expected, "{depth:?}, width={width}"); + assert!(file.color_mode_data.is_empty()); + assert_eq!(back.mode, ColorMode::Bitmap); + let gray_alpha: Vec<_> = pixels.as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[3]]).collect(); + assert_eq!(back.layers[0].surface().unwrap().read_region(back.bounds()), gray_alpha); + } + } + } + + #[test] + fn bitmap_alpha_is_flattened_over_white_before_thresholding() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let doc = document(3, 1, depth, ColorMode::Bitmap, &[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.49, 0.0, 0.0, 0.0, 0.51]); + let (file, back) = round_trip(&doc); + assert_eq!(file.decode_merged().unwrap(), [0x20], "{depth:?}"); + assert_eq!(back.layers[0].surface().unwrap().read_region(back.bounds()), [1.0, 1.0, 1.0, 1.0, 0.0, 1.0]); + } + } +} diff --git a/crates/io/src/pattern_map.rs b/crates/io/src/pattern_map.rs index bd84d9aeb1..4f0b5fe54d 100644 --- a/crates/io/src/pattern_map.rs +++ b/crates/io/src/pattern_map.rs @@ -9,7 +9,7 @@ use std::sync::Arc; use photocraft_color::{ColorMode, PixelFormat, SampleType}; use photocraft_doc::{Document, Pattern}; use photocraft_geom::Rect; -use photocraft_psd::patterns::{PatternBudget, PsdPattern, block_key, mode_channels, parse_pattern_block_with, write_pattern_block}; +use photocraft_psd::patterns::{PatternBudget, PsdPattern, block_has_user_mask, block_key, mode_channels, parse_pattern_block_with, write_pattern_block}; use photocraft_raster::Surface; use crate::pixels::{deinterleave, interleave, max_sample, psd_depth, sample_for_depth, zero_sample}; @@ -110,6 +110,12 @@ pub fn to_psd(p: &Pattern) -> PsdPattern { } } +/// A warning for each of `patterns` too large to paint: such a pattern fills with transparency, +/// so the file says so on opening. +pub fn unpaintable_warnings(patterns: &[Pattern]) -> Vec { + patterns.iter().filter_map(|p| Some(format!("pattern \"{}\" would paint nothing: {}", p.display_name(), p.paint_problem()?))).collect() +} + /// Patterns decoded from the document's raw pattern blocks (malformed blocks are skipped). All /// blocks share one memory budget, so repeating a block can't multiply it. pub fn from_global_blocks(doc: &Document) -> Vec { @@ -124,12 +130,13 @@ pub fn from_global_blocks(doc: &Document) -> Vec { .collect() } -/// Global blocks to write: the raw ones while they still match [`Document::patterns`], else the -/// raw pattern blocks replaced by one written from the document (at the first one's position). +/// Global blocks to write: the raw ones while they still match [`Document::patterns`] (see +/// [`repair_legacy_blocks`]), else the raw pattern blocks replaced by one written from the +/// document (at the first one's position). pub fn export_global_blocks(doc: &Document) -> Vec { let raw = &doc.metadata.psd_global_blocks; if from_global_blocks(doc) == doc.patterns { - return raw.clone(); + return repair_legacy_blocks(raw); } let key = block_key(psd_depth(doc.depth)); let fresh = write_pattern_block(&doc.patterns.iter().map(to_psd).collect::>()).unwrap_or_default(); @@ -151,6 +158,31 @@ pub fn export_global_blocks(doc: &Document) -> Vec Vec { + if !raw.iter().any(|(_, k, d)| PATTERN_KEYS.contains(k) && block_has_user_mask(d)) { + return raw.to_vec(); + } + let mut budget = PatternBudget::default(); + raw.iter() + .map(|b @ (sig, key, data)| { + if !PATTERN_KEYS.contains(key) { + return b.clone(); + } + match parse_pattern_block_with(data, &mut budget) { + Ok(ps) if block_has_user_mask(data) => match write_pattern_block(&ps) { + Ok(fresh) => (*sig, *key, Arc::new(fresh)), + Err(_) => b.clone(), + }, + _ => b.clone(), + } + }) + .collect() +} + /// Reads a `.pat` pattern file. pub fn read_pat(bytes: &[u8]) -> Result, String> { let ps = photocraft_psd::patterns::parse_pat_file(bytes).map_err(|e| format!("not a readable .pat file: {e}"))?; @@ -217,4 +249,61 @@ mod tests { assert_eq!(from_global_blocks(&d2), d.patterns); assert_eq!(read_pat(&write_pat(&d.patterns).unwrap()).unwrap(), d.patterns); } + + #[test] + fn transparent_pattern_blocks_from_older_builds_are_rewritten_on_save() { + // #8054: Define Pattern over transparency, then a Pattern Fill layer; Photoshop refused + // the PSD because the pattern's transparency sat in the user mask array. + let mut d = Document::new("x", photocraft_geom::Size { width: 4, height: 4 }, ColorMode::Rgb, SampleType::U16); + d.patterns.push(pattern(ColorMode::Rgb, SampleType::U16, true)); + let out = export_global_blocks(&d); + assert_eq!(out.len(), 1); + assert!(!block_has_user_mask(&out[0].2)); + // Move the transparency into the user mask array, as older builds wrote it: a 16-bit + // pattern's array is raw, so it is the last 8 + 23 + 5 × 3 × 2 bytes of the pattern. + let mut legacy = out[0].2.to_vec(); + let end = 4 + u32::from_be_bytes([legacy[0], legacy[1], legacy[2], legacy[3]]) as usize; + legacy[end - (8 + 23 + 30) - 4..end].rotate_left(4); + assert!(block_has_user_mask(&legacy)); + // A pattern block that doesn't decode (pattern version 2) sits beside it. + let unreadable = (*b"8BIM", *b"Patt", Arc::new(vec![0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 0])); + let other = (*b"8BIM", *b"Txt2", Arc::new(vec![1])); + d.metadata.psd_global_blocks = vec![other.clone(), (*b"8BIM", *b"Pat2", Arc::new(legacy)), unreadable.clone()]; + assert_eq!(from_global_blocks(&d), d.patterns, "older files still open"); + // The patterns are unchanged: only the legacy block is rewritten, in place; the other + // blocks are kept verbatim. + let out = export_global_blocks(&d); + assert_eq!(out.len(), 3); + assert_eq!((&out[0], &out[2]), (&other, &unreadable)); + assert_eq!(out[1].1, *b"Pat2"); + assert!(!block_has_user_mask(&out[1].2)); + let mut back = d.clone(); + back.metadata.psd_global_blocks = out; + assert_eq!(from_global_blocks(&back), d.patterns); + // Saving again keeps every block as it is. + assert_eq!(export_global_blocks(&back), back.metadata.psd_global_blocks); + } +} + +#[cfg(test)] +mod unpaintable_tests { + use super::*; + use photocraft_doc::PixelFormat; + use photocraft_raster::Surface; + + #[test] + fn a_pattern_too_large_to_paint_is_named_on_opening() { + let sparse = |name: &str, format: PixelFormat, side: u32| Pattern { + id: name.into(), + name: name.into(), + width: side, + height: side, + surface: Surface::new(format), + }; + let patterns = + [sparse("Small", PixelFormat::RGBA8, 64), sparse("Huge float", PixelFormat::RGBA32F, 5000), sparse("Largest 8-bit", PixelFormat::RGBA8, 8000)]; + let warnings = unpaintable_warnings(&patterns); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert!(warnings[0].contains("Huge float") && warnings[0].contains("would paint nothing") && warnings[0].contains("32 bits"), "{warnings:?}"); + } } diff --git a/crates/io/src/pcraft_cache.rs b/crates/io/src/pcraft_cache.rs new file mode 100644 index 0000000000..690169c605 --- /dev/null +++ b/crates/io/src/pcraft_cache.rs @@ -0,0 +1,74 @@ +//! Per-document native export cache; file writes and save identity stay with the caller. + +use std::sync::{Arc, Mutex}; + +use photocraft_doc::Document; +use photocraft_format::{PcraftWriter, SaveOptions}; + +/// Shared native export cache. Clones reuse tile hashes and up to 64 MiB of compressed payloads; +/// dropping the last clone releases it, including when a document closes after a save worker. +#[derive(Clone)] +pub struct PcraftExportCache(Arc>); + +impl Default for PcraftExportCache { + fn default() -> Self { + let mut writer = PcraftWriter::new(); + writer.set_compressed_cache_budget(64 << 20); + Self(Arc::new(Mutex::new(writer))) + } +} + +impl std::fmt::Debug for PcraftExportCache { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("PcraftExportCache") + } +} + +impl PartialEq for PcraftExportCache { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl PcraftExportCache { + pub(crate) fn encode(&self, doc: &Document, opts: &SaveOptions) -> photocraft_format::Result> { + self.0.lock().unwrap_or_else(std::sync::PoisonError::into_inner).save_zip(doc, opts).map(|(bytes, _)| bytes) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{ExportOptions, export}; + use photocraft_color::{ColorMode, SampleType}; + use photocraft_doc::{Layer, LayerContent}; + use photocraft_geom::Size; + use photocraft_raster::Surface; + + #[test] + fn cached_exports_reuse_tiles_and_preserve_edits_and_save_as() { + for depth in SampleType::ALL { + let mut doc = Document::new("first.pcraft", Size::new(2, 1), ColorMode::Rgb, depth); + let mut surface = Surface::new(doc.pixel_format()); + surface.write_pixel(0, 0, &[0.2, 0.4, 0.6, 1.0]); + doc.layers.push(Layer::new("photo", LayerContent::Raster(surface))); + let cache = PcraftExportCache::default(); + let options = ExportOptions { pcraft_cache: Some(cache.clone()), ..Default::default() }; + let first = export(&doc, "first.pcraft", &options).unwrap(); + assert_eq!(photocraft_format::load_from_bytes(&first.bytes).unwrap(), doc); + let (_, stats) = cache.0.lock().unwrap().save_zip(&doc, &SaveOptions::default()).unwrap(); + assert_eq!(stats.tiles_reused, stats.tiles_total); + assert_eq!(stats.tiles_written, 0); + doc.layers[0].surface_mut().unwrap().try_write_region(photocraft_geom::Rect::new(0, 0, 1, 1), &[0.8, 0.3, 0.1, 1.0]).unwrap(); + let next = export(&doc, "second.pcraft", &options).unwrap(); + let mut expected = doc.clone(); + expected.name = "second.pcraft".into(); + assert_eq!(photocraft_format::load_from_bytes(&next.bytes).unwrap(), expected); + assert_eq!(next.bytes, export(&doc, "second.pcraft", &ExportOptions::default()).unwrap().bytes); + let weak = Arc::downgrade(&cache.0); + drop(options); + drop(cache); + assert!(weak.upgrade().is_none()); + } + } +} diff --git a/crates/io/src/pdf.rs b/crates/io/src/pdf.rs index 4f992bc8fd..92a5a05850 100644 --- a/crates/io/src/pdf.rs +++ b/crates/io/src/pdf.rs @@ -59,8 +59,10 @@ fn pdf_string(s: &str) -> String { } fn deflate(data: &[u8]) -> Vec { - // Fast: print PDFs are transient and large; level 1 is ~4× quicker than the default. - let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + // Fast: print PDFs are transient and large. Level 3, not 1: zlib-rs level 1 writes static + // Huffman blocks only (13-30% bigger on photos); level 3 is the lowest that is never bigger + // than miniz's level 1 was, at about twice its time. + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::new(3)); let _ = e.write_all(data); e.finish().unwrap_or_default() } diff --git a/crates/io/src/pdn.rs b/crates/io/src/pdn.rs index 08e294b7bc..1e2d99f7af 100644 --- a/crates/io/src/pdn.rs +++ b/crates/io/src/pdn.rs @@ -154,6 +154,12 @@ pub(super) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result) -> Result 255 { return Err(invalid("opacity exceeds 255")); } - let mut l = Layer::raster(graph.string(graph.field(props, "name")?)?, PixelFormat::RGBA8); + let mut l = Layer::raster( + { + let name = graph.string(graph.field(props, "name")?)?; + if name.len() > MAX_NAME_BYTES { + return Err(invalid("layer name is too long")); + } + name_bytes = name_bytes.saturating_add(name.len()); + if name_bytes > MAX_TOTAL_NAME_BYTES { + return Err(invalid("layer names exceed the total metadata budget")); + } + name.to_string() + }, + PixelFormat::RGBA8, + ); l.visible = graph.boolean(graph.field(props, "visible")?)?; l.opacity = opacity as f32 / 255.0; l.blend = blend(&graph, layer, props)?; diff --git a/crates/io/src/perspective_warp_map.rs b/crates/io/src/perspective_warp_map.rs new file mode 100644 index 0000000000..654de23b32 --- /dev/null +++ b/crates/io/src/perspective_warp_map.rs @@ -0,0 +1,455 @@ +//! Perspective Warp smart filters (`Fltr` class `perspectiveWarpTransform`). +//! +//! Layout as Photoshop writes it (checked by applying Edit › Perspective Warp to a smart object +//! through the Action Manager and saving the PSD): +//! +//! ```text +//! filterFX: Nm, blendOptions, enab, hasoptions (true), FrgC, BckC, filterID (1023 observed; +//! other versions write other ids), Fltr = perspectiveWarpTransform { +//! null = reference to the target layer, +//! referenceRect = Rctn { Top, Left, Btom, Rght } (UntF #Pxl, the layer bounds), +//! vertices = [ Pnt { Hrzn, Vrtc } (UntF #Pxl, document pixels) ], (Layout mode) +//! warpedVertices = [ Pnt { Hrzn, Vrtc } ] (same count), (Warp mode) +//! quads = [ perspectiveWarpQuad { indices = [4 × long] } ], +//! PuX0..PuX3, PuY0..PuY3 (doubles: the placement corners when the filter was applied) } +//! ``` +//! +//! Each quad is one plane of `layer.smartObjects.perspectiveWarp`: `src` = the quad's +//! `vertices`, `dst` = its `warpedVertices`, in the stored corner order. Quads that share a vertex +//! share the corner, which the engine links. The item is kept as a template ([`TEMPLATE_KEY`]) so +//! the keys PhotoCraft does not model go back unchanged, with only the vertices and quads +//! rewritten from the edited planes. Anything inconsistent (an index out of range, mismatched +//! vertex counts, a non-finite or degenerate quad) stays an opaque [`super::UNSUPPORTED_FILTER`]. + +use photocraft_algo::perspective::{PerspectiveMap, Plane, Straighten, straighten}; +use photocraft_doc::SmartFilter; +use photocraft_psd::descriptor::{Class, Descriptor, Id, ReferenceItem, UnicodeString, Value}; +use serde_json::{Value as J, json}; + +use super::{blend_options, from_hex, get_desc, rgbc, set, to_hex, unit}; + +pub(super) const CLASS: &str = "perspectiveWarpTransform"; +/// The command this filter maps to (`photocraft_engine::distort_cmds::PERSPECTIVE_SMART`). +pub(super) const COMMAND: &str = "layer.smartObjects.perspectiveWarp"; +/// Edit › Perspective Warp on a smart object stores its own id; it renders the same. +const EDIT_COMMAND: &str = "edit.perspectiveWarp"; +/// The `filterID` Photoshop wrote for a new Perspective Warp smart filter. +const FILTER_ID: i32 = 1023; +const NAME: &str = "Perspective Warp..."; +pub(super) const TEMPLATE_KEY: &str = "__perspectiveWarpPsd"; +/// Far more planes than anyone lays out by hand; bounds the corner linking (quadratic). +const MAX_QUADS: usize = 256; +const MAX_VERTICES: usize = 4 * MAX_QUADS; +const MAX_TEMPLATE_BYTES: usize = 1 << 20; + +/// Whether `command` is a Perspective Warp smart filter this module writes. +pub(super) fn handles(command: &str) -> bool { + command == COMMAND || command == EDIT_COMMAND +} + +fn point(v: &Value) -> Option<[f64; 2]> { + let Value::Descriptor(p) = v else { return None }; + let coord = |k: &str| match p.get(k)? { + Value::Double(d) => Some(*d), + Value::UnitFloat { unit: [b'#', b'P', b'x', b'l'], value } => Some(*value), + _ => None, + }; + Some([coord("Hrzn")?, coord("Vrtc")?]).filter(|q| q.iter().all(|v| v.is_finite())) +} + +fn points(f: &Descriptor, key: &str) -> Option> { + let Some(Value::List(list)) = f.get(key) else { return None }; + if list.is_empty() || list.len() > MAX_VERTICES { + return None; + } + list.iter().map(point).collect() +} + +fn quads(f: &Descriptor) -> Option> { + let Some(Value::List(list)) = f.get("quads") else { return None }; + if list.is_empty() || list.len() > MAX_QUADS { + return None; + } + list.iter() + .map(|q| { + let Value::Descriptor(q) = q else { return None }; + let Some(Value::List(ix)) = q.get("indices") else { return None }; + let ix: Vec = ix + .iter() + .map(|i| match i { + Value::Integer(i) => usize::try_from(*i).ok(), + _ => None, + }) + .collect::>()?; + ix.try_into().ok() + }) + .collect() +} + +/// The planes a `perspectiveWarpTransform` descriptor describes; `None` when it is inconsistent. +fn planes_of(f: &Descriptor) -> Option> { + let (src, dst) = (points(f, "vertices")?, points(f, "warpedVertices")?); + if src.len() != dst.len() { + return None; + } + let corners = + |list: &[[f64; 2]], q: &[usize; 4]| -> Option<[[f64; 2]; 4]> { Some([*list.get(q[0])?, *list.get(q[1])?, *list.get(q[2])?, *list.get(q[3])?]) }; + let planes: Vec = quads(f)?.iter().map(|q| Some(Plane { src: corners(&src, q)?, dst: corners(&dst, q)? })).collect::>()?; + PerspectiveMap::new(&planes)?; + Some(planes) +} + +/// `layer.smartObjects.perspectiveWarp` params for a Perspective Warp item. +pub(super) fn import_params(item: &Descriptor) -> Option { + let f = get_desc(item, "Fltr")?; + if !f.class_id.is(CLASS) { + return None; + } + let planes = planes_of(f)?; + // Neutral blending in the template: the model's own fields replace it on export. + let mut template = item.clone(); + let neutral = SmartFilter { command: String::new(), params: J::Null, blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; + set(&mut template, "blendOptions", blend_options(&neutral)); + set(&mut template, "enab", Value::Boolean(true)); + // Export refuses a larger template; such an item stays opaque, so it is still written back. + let bytes = template.to_bytes(); + if bytes.len() > MAX_TEMPLATE_BYTES { + return None; + } + Some(json!({"planes": planes, TEMPLATE_KEY: to_hex(&bytes)})) +} + +/// The template item of `params` (`None` when there is none), checked to be a Perspective Warp. +fn template(params: &J) -> Result, String> { + let Some(raw) = params.get(TEMPLATE_KEY) else { return Ok(None) }; + let raw = raw.as_str().ok_or("the Perspective Warp PSD data must be hex text")?; + if raw.len() > MAX_TEMPLATE_BYTES * 2 { + return Err("the Perspective Warp PSD data is too large".into()); + } + let bytes = from_hex(raw).ok_or("the Perspective Warp PSD data is not valid hex")?; + let item = Descriptor::from_bytes(&bytes).map_err(|e| format!("the Perspective Warp PSD data is unreadable ({e})"))?; + if !item.class_id.is("filterFX") || !get_desc(&item, "Fltr").is_some_and(|f| f.class_id.is(CLASS)) { + return Err("the Perspective Warp PSD data is not a Perspective Warp filter".into()); + } + Ok(Some(item)) +} + +/// The planes to write: as rendered, so a `straighten` is baked in. +fn export_planes(params: &J) -> Result, String> { + let object = params.as_object().ok_or("Perspective Warp parameters must be an object")?; + if let Some(k) = object.keys().find(|k| !super::is_meta(k) && !["planes", "straighten", "interpolation"].contains(&k.as_str())) { + return Err(format!("Perspective Warp setting `{k}` has no PSD equivalent")); + } + let mut planes: Vec = + serde_json::from_value(params.get("planes").cloned().ok_or("it has no planes")?).map_err(|e| format!("its planes are unreadable ({e})"))?; + if planes.is_empty() || planes.len() > MAX_QUADS { + return Err(format!("it must have 1 to {MAX_QUADS} planes")); + } + if planes.iter().any(|q| q.src.iter().chain(&q.dst).flatten().any(|v| !v.is_finite())) { + return Err("a plane corner is not finite".into()); + } + if let Some(s) = params.get("straighten").and_then(J::as_str) { + straighten(&mut planes, Straighten::parse(s).ok_or_else(|| format!("unknown straighten `{s}`"))?); + } + PerspectiveMap::new(&planes).ok_or("a plane is degenerate")?; + Ok(planes) +} + +fn pnt(p: [f64; 2]) -> Value { + Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", unit(b"#Pxl", p[0])).with("Vrtc", unit(b"#Pxl", p[1]))) +} + +/// `vertices`, `warpedVertices` and `quads` for `planes`; a corner shared by several planes (same +/// layout and warped position) is written once. +fn geometry(planes: &[Plane]) -> (Value, Value, Value) { + let mut corners: Vec<([f64; 2], [f64; 2])> = Vec::new(); + let mut quads = Vec::with_capacity(planes.len()); + for p in planes { + let mut ix = Vec::with_capacity(4); + for (s, d) in p.src.iter().zip(&p.dst) { + let i = corners.iter().position(|c| c == &(*s, *d)).unwrap_or_else(|| { + corners.push((*s, *d)); + corners.len() - 1 + }); + ix.push(Value::Integer(i32::try_from(i).unwrap_or(i32::MAX))); + } + quads.push(Value::Descriptor(Descriptor::new("perspectiveWarpQuad").with("indices", Value::List(ix)))); + } + let src = corners.iter().map(|c| pnt(c.0)).collect(); + let dst = corners.iter().map(|c| pnt(c.1)).collect(); + (Value::List(src), Value::List(dst), Value::List(quads)) +} + +/// A new `perspectiveWarpTransform` descriptor (no Photoshop template to start from). +fn fresh_fltr(planes: &[Plane]) -> Descriptor { + let (mut x0, mut y0, mut x1, mut y1) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY); + for c in planes.iter().flat_map(|p| p.src) { + (x0, y0, x1, y1) = (x0.min(c[0]), y0.min(c[1]), x1.max(c[0]), y1.max(c[1])); + } + let target = Value::Reference(vec![ReferenceItem::Enumerated { + class: Class { name: UnicodeString::new_nul(""), class_id: Id::new("Lyr ") }, + type_id: Id::new("Ordn"), + value: Id::new("Trgt"), + }]); + let rect = + Descriptor::new("Rctn").with("Top ", unit(b"#Pxl", y0)).with("Left", unit(b"#Pxl", x0)).with("Btom", unit(b"#Pxl", y1)).with("Rght", unit(b"#Pxl", x1)); + Descriptor::new(CLASS).with("null", target).with("referenceRect", Value::Descriptor(rect)) +} + +/// The `filterFXList` item for a Perspective Warp smart filter. +pub(super) fn export_item(sf: &SmartFilter) -> Result { + let params = &sf.params; + let planes = export_planes(params)?; + let template = template(params)?; + let unchanged = template.as_ref().and_then(|item| get_desc(item, "Fltr")).and_then(planes_of).is_some_and(|p| p == planes); + let mut item = match template { + // Unchanged planes: the original item, vertex order and sharing included. + Some(item) if unchanged => item, + Some(mut item) => { + let mut f = get_desc(&item, "Fltr").cloned().ok_or("the Perspective Warp PSD data has no filter")?; + write_geometry(&mut f, &planes); + set(&mut item, "Fltr", Value::Descriptor(f)); + item + } + None => { + let mut f = fresh_fltr(&planes); + write_geometry(&mut f, &planes); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(NAME))) + .with("blendOptions", blend_options(sf)) + .with("enab", Value::Boolean(sf.visible)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(f)) + .with("filterID", Value::Integer(FILTER_ID)) + } + }; + set(&mut item, "blendOptions", blend_options(sf)); + set(&mut item, "enab", Value::Boolean(sf.visible)); + Ok(item) +} + +fn write_geometry(f: &mut Descriptor, planes: &[Plane]) { + let (src, dst, quads) = geometry(planes); + set(f, "vertices", src); + set(f, "warpedVertices", dst); + set(f, "quads", quads); +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::smart_map::{UNSUPPORTED_FILTER, filter_from_item, int, item_for_filter}; + + fn pts(list: &[[f64; 2]]) -> Value { + Value::List(list.iter().map(|&p| pnt(p)).collect()) + } + + fn quad_list>(q: &[Q]) -> Value { + Value::List( + q.iter() + .map(|q| { + Value::Descriptor( + Descriptor::new("perspectiveWarpQuad").with("indices", Value::List(q.as_ref().iter().map(|&i| Value::Integer(i)).collect())), + ) + }) + .collect(), + ) + } + + /// A `filterFXList` item as Photoshop wrote it for a one-plane Perspective Warp of a smart + /// object placed at (40, 30)–(160, 120). + fn item_with>(vertices: &[[f64; 2]], warped: &[[f64; 2]], q: &[Q], filter_id: i32) -> Descriptor { + let mut f = fresh_fltr(&[Plane::identity([[40.0, 30.0], [160.0, 30.0], [160.0, 120.0], [40.0, 120.0]])]) + .with("vertices", pts(vertices)) + .with("warpedVertices", pts(warped)) + .with("quads", quad_list(q)); + for (i, (x, y)) in [(40.0, 30.0), (160.0, 30.0), (160.0, 120.0), (40.0, 120.0)].into_iter().enumerate() { + f = f.with(&format!("PuX{i}"), Value::Double(x)); + f = f.with(&format!("PuY{i}"), Value::Double(y)); + } + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Perspektivische Verformung..."))) + .with( + "blendOptions", + blend_options(&SmartFilter { + command: String::new(), + params: J::Null, + blend: photocraft_color::BlendMode::Normal, + opacity: 1.0, + visible: true, + }), + ) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(f)) + .with("filterID", Value::Integer(filter_id)) + } + + const V: [[f64; 2]; 4] = [[40.0, 30.0], [160.0, 30.0], [160.0, 120.0], [40.0, 120.0]]; + const W: [[f64; 2]; 4] = [[60.0, 50.0], [140.0, 40.0], [180.0, 130.0], [20.0, 110.0]]; + + fn one_plane() -> Descriptor { + item_with(&V, &W, &[[0, 1, 2, 3]], FILTER_ID) + } + + /// Two planes sharing an edge (vertices 1 and 4). + fn two_planes(filter_id: i32) -> Descriptor { + let v = [[40.0, 30.0], [100.0, 30.0], [160.0, 30.0], [160.0, 120.0], [100.0, 120.0], [40.0, 120.0]]; + let w = [[40.0, 40.0], [100.0, 30.0], [170.0, 45.0], [150.0, 115.0], [100.0, 125.0], [30.0, 110.0]]; + item_with(&v, &w, &[[0, 1, 4, 5], [1, 2, 3, 4]], filter_id) + } + + #[test] + fn a_perspective_warp_opens_as_the_command_with_its_planes() { + let f = filter_from_item(&one_plane()); + assert_eq!(f.command, COMMAND); + let planes: Vec = serde_json::from_value(f.params["planes"].clone()).unwrap(); + assert_eq!(planes, vec![Plane { src: V, dst: W }]); + + // Several quads become several planes; any filterID (708 in other versions) is read. + let f = filter_from_item(&two_planes(708)); + assert_eq!(f.command, COMMAND); + let planes: Vec = serde_json::from_value(f.params["planes"].clone()).unwrap(); + assert_eq!(planes.len(), 2); + assert_eq!(planes[0].src[1], planes[1].src[0], "the shared vertex is the same corner in both planes"); + assert_eq!(planes[1].dst, [[100.0, 30.0], [170.0, 45.0], [150.0, 115.0], [100.0, 125.0]]); + } + + #[test] + fn unchanged_filters_write_the_original_item_back() { + for src in [one_plane(), two_planes(708)] { + let f = filter_from_item(&src); + assert_eq!(item_for_filter(&f).unwrap(), src); + } + } + + #[test] + fn edited_planes_round_trip_and_keep_the_rest_of_the_template() { + let src = two_planes(708); + let mut f = filter_from_item(&src); + f.params["planes"][1]["dst"][2] = json!([155.5, 118.0]); + f.opacity = 0.5; + f.visible = false; + let item = item_for_filter(&f).unwrap(); + assert_eq!(int(&item, "filterID"), Some(708), "the original filter id stays"); + let fltr = get_desc(&item, "Fltr").unwrap(); + assert_eq!(fltr.get("PuX1"), Some(&Value::Double(160.0)), "unmodelled keys stay"); + let Some(Value::List(v)) = fltr.get("vertices") else { panic!() }; + assert_eq!(v.len(), 6, "shared corners stay shared"); + let back = filter_from_item(&item); + assert_eq!(back.command, COMMAND); + assert_eq!(back.params["planes"], f.params["planes"]); + assert_eq!((back.opacity, back.visible), (0.5, false)); + } + + #[test] + fn a_new_perspective_warp_writes_photoshops_layout() { + let planes = vec![Plane { src: V, dst: W }]; + let f = SmartFilter { + command: COMMAND.into(), + params: json!({"planes": planes}), + blend: photocraft_color::BlendMode::Multiply, + opacity: 0.25, + visible: true, + }; + let item = item_for_filter(&f).unwrap(); + assert_eq!(int(&item, "filterID"), Some(FILTER_ID)); + assert_eq!(item.get("hasoptions"), Some(&Value::Boolean(true))); + let fltr = get_desc(&item, "Fltr").unwrap(); + assert!(fltr.class_id.is(CLASS)); + assert_eq!(fltr.get("vertices"), Some(&pts(&V))); + assert_eq!(fltr.get("warpedVertices"), Some(&pts(&W))); + assert_eq!(fltr.get("quads"), Some(&quad_list(&[[0, 1, 2, 3]]))); + let rect = get_desc(fltr, "referenceRect").unwrap(); + assert_eq!(rect.get("Rght"), Some(&unit(b"#Pxl", 160.0))); + let back = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!((back.command.as_str(), &back.params["planes"]), (COMMAND, &json!(planes))); + assert_eq!((back.blend, back.opacity), (photocraft_color::BlendMode::Multiply, 0.25)); + + // Edit › Perspective Warp on a smart object stores its own id; it is written too, with a + // straighten baked in. + let f = SmartFilter { command: EDIT_COMMAND.into(), params: json!({"planes": planes, "straighten": "auto", "interpolation": "bilinear"}), ..f }; + assert!(item_for_filter(&f).is_ok()); + } + + #[test] + fn inconsistent_perspective_warps_stay_opaque() { + let nan = [[f64::NAN, 30.0], V[1], V[2], V[3]]; + let flat = [[0.0, 0.0]; 4]; + let without = |key: &str| { + let mut i = one_plane(); + let mut f = get_desc(&i, "Fltr").unwrap().clone(); + f.items.retain(|(k, _)| !k.is(key)); + set(&mut i, "Fltr", Value::Descriptor(f)); + i + }; + let mut bad_unit = one_plane(); + let mut f = get_desc(&bad_unit, "Fltr").unwrap().clone(); + let Some(Value::List(l)) = f.get("vertices").cloned() else { panic!() }; + let mut l = l; + l[0] = Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", unit(b"#Prc", 40.0)).with("Vrtc", unit(b"#Prc", 30.0))); + set(&mut f, "vertices", Value::List(l)); + set(&mut bad_unit, "Fltr", Value::Descriptor(f)); + let too_many: Vec<[i32; 4]> = vec![[0, 1, 2, 3]; MAX_QUADS + 1]; + // Valid planes, but a template export would refuse as too large. + let mut huge = one_plane(); + huge.items.push((Id::new("bigData"), Value::RawData(vec![0; MAX_TEMPLATE_BYTES]))); + for (i, src) in [ + item_with(&V, &W, &[[0, 1, 2, 4]], FILTER_ID), + item_with(&V, &W, &[[0, 1, 2, -1]], FILTER_ID), + item_with(&V, &W, &[[0, 1, 2]], FILTER_ID), + item_with(&V, &W, &[[0, 1, 2, 3, 0]], FILTER_ID), + item_with(&V, &W, &[] as &[[i32; 4]], FILTER_ID), + item_with(&V, &W, &too_many, FILTER_ID), + item_with(&V, &W[..3], &[[0, 1, 2, 3]], FILTER_ID), + item_with(&nan, &W, &[[0, 1, 2, 3]], FILTER_ID), + item_with(&flat, &W, &[[0, 1, 2, 3]], FILTER_ID), + item_with(&[], &[], &[[0, 1, 2, 3]], FILTER_ID), + without("vertices"), + without("warpedVertices"), + without("quads"), + bad_unit, + huge, + ] + .into_iter() + .enumerate() + { + let f = filter_from_item(&src); + assert_eq!(f.command, UNSUPPORTED_FILTER, "case {i}"); + // Bytes, as NaN is not equal to itself. + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), src.to_bytes()); + } + } + + #[test] + fn bad_exports_report_errors() { + let ok = filter_from_item(&one_plane()).params; + let with = |k: &str, v: J| { + let mut p = ok.clone(); + p[k] = v; + p + }; + let plane = json!([{"src": V, "dst": W}]); + for p in [ + J::Null, + json!({}), + json!({"planes": []}), + json!({"planes": "x"}), + json!({"planes": [{"src": [[0, 0], [0, 0], [0, 0], [0, 0]], "dst": W}]}), + json!({"planes": plane, "straighten": "sideways"}), + json!({"planes": plane, "pins": []}), + json!({"planes": vec![json!({"src": V, "dst": W}); MAX_QUADS + 1]}), + with(TEMPLATE_KEY, json!("zz")), + with(TEMPLATE_KEY, json!(7)), + with(TEMPLATE_KEY, json!(to_hex(&Descriptor::new("filterFX").to_bytes()))), + with(TEMPLATE_KEY, json!("0".repeat(MAX_TEMPLATE_BYTES * 2 + 2))), + ] { + let f = SmartFilter { command: COMMAND.into(), params: p.clone(), blend: photocraft_color::BlendMode::Normal, opacity: 1.0, visible: true }; + assert!(export_item(&f).is_err(), "{p}"); + } + } +} diff --git a/crates/io/src/pixels.rs b/crates/io/src/pixels.rs index 0167eb3b3f..9dff11fcce 100644 --- a/crates/io/src/pixels.rs +++ b/crates/io/src/pixels.rs @@ -21,6 +21,14 @@ pub fn sample_for_depth(depth: u16) -> SampleType { } } +/// Complements every native-endian sample of one plane (`max − v`, `1 − v` for float). A +/// trailing partial sample is left unchanged. +pub(crate) fn invert_plane(bytes: &mut [u8], s: SampleType) { + for c in bytes.chunks_exact_mut(s.bytes().max(1)) { + invert_sample(c, s); + } +} + fn invert_sample(bytes: &mut [u8], s: SampleType) { match s { SampleType::U8 => bytes[0] = 255 - bytes[0], @@ -43,50 +51,72 @@ pub fn interleave(planes: &[Option<&[u8]>], fill: &[Vec], n: usize, s: Sampl let ch = planes.len(); let row = ch * bps; let mut out = vec![0u8; n * row]; - // One band of rows per core, written straight into its slice of the shared output: the - // per-pixel channel loop stays, but a band holds ~1/cores of a 20 MP image's iterations - // instead of all of them. + if out.is_empty() { + return out; + } let bs = bands(n); - let parts: Vec> = par_map(bs.clone(), |band| { - let mut part = vec![0u8; band.len() * row]; - for (i, p) in band.clone().enumerate() { - for (c, plane) in planes.iter().enumerate() { - let dst = &mut part[(i * ch + c) * bps..(i * ch + c + 1) * bps]; - match plane { - Some(pl) if pl.len() >= (p + 1) * bps => { - let src = &pl[p * bps..(p + 1) * bps]; - for k in 0..bps { - dst[k] = src[bps - 1 - k]; - } - if cfg!(target_endian = "big") { - dst.copy_from_slice(src); - } - if invert[c] { - invert_sample(dst, s); - } - } - _ => dst.copy_from_slice(&fill[c]), - } + let step = bs.first().map_or(n, ExactSizeIterator::len) * row; + par_map(bs.into_iter().zip(out.chunks_mut(step)).collect(), |(band, part)| { + for (c, plane) in planes.iter().enumerate() { + let src = plane.and_then(|p| p.get(band.start * bps..)).unwrap_or_default(); + let inv = src.len() >= bps && invert[c]; + let fill = fill.get(c).map(Vec::as_slice).unwrap_or_default(); + match s { + SampleType::U8 => write_channel(part, ch, c, src, fill, |[v]| [if inv { 255 - v } else { v }]), + SampleType::U16 => write_channel(part, ch, c, src, fill, |v| { + let v = u16::from_be_bytes(v); + (if inv { 65535 - v } else { v }).to_ne_bytes() + }), + SampleType::F32 => write_channel(part, ch, c, src, fill, |v| { + // Preserve float bits when no inversion is requested. + if inv { (1.0 - f32::from_be_bytes(v)).to_ne_bytes() } else { u32::from_be_bytes(v).to_ne_bytes() } + }), } } - part }); - for (band, part) in bs.into_iter().zip(parts) { - out[band.start * row..band.end * row].copy_from_slice(&part); - } out } +fn write_channel(out: &mut [u8], ch: usize, c: usize, src: &[u8], fill: &[u8], decode: impl Fn([u8; N]) -> [u8; N]) { + let mut samples = src.as_chunks::().0.iter(); + for pixel in out.chunks_exact_mut(ch * N) { + let dst = &mut pixel[c * N..(c + 1) * N]; + match samples.next() { + Some(sample) => dst.copy_from_slice(&decode(*sample)), + None => dst.copy_from_slice(fill), + } + } +} + /// Splits interleaved native-endian bytes into big-endian planes. pub fn deinterleave(bytes: &[u8], ch: usize, s: SampleType, invert: &[bool]) -> Vec> { let bps = s.bytes(); let n = bytes.len() / (ch * bps).max(1); if s == SampleType::U8 { - // Fast path: one plane per thread, no per-sample copies. - return par_map((0..ch).collect(), |c| { - let it = bytes.iter().skip(c).step_by(ch).take(n); - if invert[c] { it.map(|v| 255 - v).collect() } else { it.copied().collect() } + // Fast path: one band of pixels per core, written straight into every plane. + let mut planes = vec![vec![0u8; n]; ch]; + let bs = bands(n); + let mut slots: Vec> = bs.iter().map(|_| Vec::with_capacity(ch)).collect(); + for plane in &mut planes { + let mut rest = plane.as_mut_slice(); + for (slot, band) in slots.iter_mut().zip(&bs) { + let Some((part, tail)) = rest.split_at_mut_checked(band.len()) else { break }; + slot.push(part); + rest = tail; + } + } + par_map(bs.into_iter().zip(slots).collect(), |(band, mut outs)| { + let src = bytes.get(band.start * ch..band.end * ch).unwrap_or(&[]); + for (c, out) in outs.iter_mut().enumerate() { + let px = src.chunks_exact(ch).filter_map(|p| p.get(c).copied()); + if invert.get(c).copied().unwrap_or(false) { + out.iter_mut().zip(px).for_each(|(o, v)| *o = 255 - v); + } else { + out.iter_mut().zip(px).for_each(|(o, v)| *o = v); + } + } }); + return planes; } // One band of rows per core, appended in band order into pre-grown planes. let row = ch * bps; @@ -217,21 +247,29 @@ pub fn bands(n: usize) -> Vec> { mod tests { use super::*; - #[test] - fn banded_shuffles_match_a_sequential_reference() { - fn seq_interleave(planes: &[Option<&[u8]>], fill: &[Vec], n: usize, s: SampleType, invert: &[bool]) -> Vec { - let bps = s.bytes(); - let ch = planes.len(); - let mut out = vec![0u8; n * ch * bps]; - for (c, plane) in planes.iter().enumerate() { - for i in 0..n { - let dst = &mut out[(i * ch + c) * bps..(i * ch + c + 1) * bps]; + pub(super) fn previous_interleave(planes: &[Option<&[u8]>], fill: &[Vec], n: usize, s: SampleType, invert: &[bool]) -> Vec { + let bps = s.bytes(); + let ch = planes.len(); + let row = ch * bps; + let mut out = vec![0u8; n * row]; + // One band of rows per core, written straight into its slice of the shared output: the + // per-pixel channel loop stays, but a band holds ~1/cores of a 20 MP image's iterations + // instead of all of them. + let bs = bands(n); + let parts: Vec> = par_map(bs.clone(), |band| { + let mut part = vec![0u8; band.len() * row]; + for (i, p) in band.clone().enumerate() { + for (c, plane) in planes.iter().enumerate() { + let dst = &mut part[(i * ch + c) * bps..(i * ch + c + 1) * bps]; match plane { - Some(p) if p.len() >= (i + 1) * bps => { - let src = &p[i * bps..(i + 1) * bps]; + Some(pl) if pl.len() >= (p + 1) * bps => { + let src = &pl[p * bps..(p + 1) * bps]; for k in 0..bps { dst[k] = src[bps - 1 - k]; } + if cfg!(target_endian = "big") { + dst.copy_from_slice(src); + } if invert[c] { invert_sample(dst, s); } @@ -240,8 +278,16 @@ mod tests { } } } - out + part + }); + for (band, part) in bs.into_iter().zip(parts) { + out[band.start * row..band.end * row].copy_from_slice(&part); } + out + } + + #[test] + fn banded_shuffles_match_a_sequential_reference() { fn seq_deinterleave(bytes: &[u8], ch: usize, s: SampleType, invert: &[bool]) -> Vec> { let bps = s.bytes(); let n = bytes.len() / (ch * bps).max(1); @@ -260,16 +306,17 @@ mod tests { planes } // Odd pixel counts cross band boundaries; a missing plane exercises the fill path. - for n in [1usize, 7, 64, 1000] { + for n in [0usize, 1, 7, 64, 1000] { for s in [SampleType::U8, SampleType::U16, SampleType::F32] { let bps = s.bytes(); let planes: Vec> = (0..4).map(|c| (0..n * bps).map(|i| (i * 13 + c * 5) as u8).collect()).collect(); let mut refs: Vec> = planes.iter().map(|p| Some(&p[..])).collect(); refs[2] = None; + refs[3] = Some(&planes[3][..(n * bps).saturating_sub(1)]); let fill: Vec> = (0..4).map(|c| vec![c as u8 * 40; bps]).collect(); let invert = [false, true, false, true]; let got = interleave(&refs, &fill, n, s, &invert); - let want = seq_interleave(&refs, &fill, n, s, &invert); + let want = previous_interleave(&refs, &fill, n, s, &invert); assert_eq!(got, want, "interleave n={n}"); let planes_back = deinterleave(&got, 4, s, &invert); let want_back = seq_deinterleave(&got, 4, s, &invert); @@ -287,6 +334,7 @@ mod tests { let fill = vec![zero_sample(s); 3]; let inv = [false, true, false]; let il = interleave(&refs, &fill, 4, s, &inv); + assert_eq!(interleave(&refs, &[], 4, s, &inv), il); let back = deinterleave(&il, 3, s, &inv); if s == SampleType::F32 { // 1 - (1 - v) may not be exact for floats; only check non-inverted planes. @@ -302,6 +350,12 @@ mod tests { fn missing_planes_use_fill() { let il = interleave(&[None, Some(&[5, 6])], &[vec![9], vec![0]], 2, SampleType::U8, &[false, false]); assert_eq!(il, vec![9, 5, 9, 6]); + for s in [SampleType::U8, SampleType::U16, SampleType::F32] { + for n in [1, 1000] { + let fill = vec![vec![9; s.bytes()]; 2]; + assert_eq!(interleave(&[None, Some(&[1, 2, 3][..s.bytes() - 1])], &fill, n, s, &[]), vec![9; n * 2 * s.bytes()]); + } + } } #[test] @@ -343,3 +397,35 @@ mod tests { assert_eq!(decode_be(&f, 0, SampleType::F32), 2.5); } } + +#[cfg(test)] +mod measurement { + use super::*; + #[test] + #[ignore = "manual 24 MP release benchmark"] + fn benchmark_interleave() { + use std::{hint::black_box, time::Instant}; + const N: usize = 24_000_000; + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let bps = sample.bytes(); + let planes: Vec> = (0..4).map(|c| (0..N * bps).map(|i| (i * 13 + c * 5) as u8).collect()).collect(); + let refs: Vec<_> = planes.iter().map(|p| Some(p.as_slice())).collect(); + let fill = vec![vec![0; bps]; 4]; + let invert = [false, true, false, true]; + assert_eq!(tests::previous_interleave(&refs, &fill, N, sample, &invert), interleave(&refs, &fill, N, sample, &invert)); + for trial in 0..6 { + let mut paths = [("main", tests::previous_interleave as fn(_, _, _, _, _) -> _), ("split", interleave)]; + if trial % 2 == 1 { + paths.reverse(); + } + for (name, path) in paths { + let t = Instant::now(); + let out = black_box(path)(&refs, &fill, N, sample, &invert); + let elapsed = t.elapsed().as_secs_f64() * 1000.0; + black_box(&out); + println!("interleave,{sample:?},{trial},{name},{elapsed:.6}"); + } + } + } + } +} diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 8ba30d83c4..23319de097 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -15,20 +15,46 @@ use crate::blocks; use crate::pixels::{deinterleave, encode_be, psd_depth}; use crate::psd_import::REGENERATED; +mod document; +mod estimate; +mod merged; +mod resources; +mod smart; + +pub(crate) use document::document_to_psd_interruptible; +#[cfg(test)] +use document::document_to_psd_sized; +pub use document::{document_to_psd, document_to_psd_with}; +use document::{document_to_psd_nested, mask_parameters}; +pub(crate) use estimate::{fit_psd_size, psd_version, sign_global_blocks}; +pub(crate) use resources::{global_block, guides_resource, keep_imported_resource, legacy_name, pascal_names_resource, path_resources, unicode_names_resource}; +use smart::{LINK_KEYS, SmartOut, Source, contents_uuid, effects_unchanged, masked, size_from_layer, smart_keys, upsert}; + /// Blocks that define a layer's content as something other than pixels: fill (`SoCo`, `GdFl`, /// `PtFl`), placed / smart object (`SoLd`, `PlLd`, `SoLE`), type (`TySh`) and shape fill (`vscg`). /// A raster layer never writes them, nor adjustment blocks. const RASTER_STALE_KEYS: [&[u8; 4]; 8] = [b"SoCo", b"GdFl", b"PtFl", b"SoLd", b"PlLd", b"SoLE", b"TySh", b"vscg"]; +/// The largest file written as PSD: past it, the document is saved as PSB (Large Document Format), +/// as Photoshop does. const PSD_MAX_FILE_SIZE: u64 = 2 * 1024 * 1024 * 1024; -const PSD_ESTIMATE_FIXED_OVERHEAD: u64 = 1024 * 1024; -const PSD_ESTIMATE_LAYER_OVERHEAD: u64 = 64 * 1024; +/// Room kept under [`PSD_MAX_FILE_SIZE`] for what the save adds after the conversion (the image +/// preview resource, a few KB). +const PSD_SAVE_HEADROOM: u64 = 1024 * 1024; +/// Most bytes of one layer's interleaved samples the exporter gathers. The interleaved bytes and +/// the channel planes made from them (or the band parts and the joined buffer) are alive together, +/// so the peak is about twice this; it is also what fits in a file of at most [`PSD_MAX_FILE_SIZE`]. +const MAX_LAYER_GATHER_BYTES: u64 = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 256 << 20 }; /// Options for [`document_to_psd_with`]. #[derive(Debug, Clone)] pub struct PsdExportOptions { /// Write PSB even when the document fits PSD limits. pub force_psb: bool, + /// Switch to PSB when the encoded file exceeds PSD's 2 GB limit (`fit_psd_size`). A writer + /// that embeds the layer data elsewhere turns this off: the layered TIFF writes PSD-style + /// layer data and falls back to PSB only when a length really overflows 32 bits. + pub size_limit_psb: bool, /// Matte the merged image against white where it is translucent, as Photoshop does /// (`false` keeps straight colour under the alpha: for containers that store the composite /// with its own alpha, such as a layered TIFF). @@ -41,11 +67,26 @@ pub struct PsdExportOptions { /// readers that only show the merged image see white. A document without layer records always /// gets the real image, since its pixels live nowhere else. pub maximize_compatibility: bool, + /// The path of the document's own file: linked smart objects are resolved against it + /// ([`crate::doc_path::resolve`]) before they are read and embedded. + pub document_path: Option, + /// Read and embed the files linked smart objects name. On by default; automation turns it + /// off, since a stored path is document data rather than a granted file: such a layer is + /// then written as pixels, with a warning. + pub read_linked_files: bool, } impl Default for PsdExportOptions { fn default() -> Self { - PsdExportOptions { force_psb: false, merged_matte: true, merged_compression: Compression::Rle, maximize_compatibility: true } + PsdExportOptions { + force_psb: false, + size_limit_psb: true, + merged_matte: true, + merged_compression: Compression::Rle, + maximize_compatibility: true, + document_path: None, + read_linked_files: true, + } } } @@ -53,6 +94,10 @@ struct Ex { fmt: PixelFormat, /// Document resolution (for generated type-layer data). dpi: f32, + /// The document's own file, which linked smart objects are resolved against. + document: Option, + /// Whether linked files may be read and embedded ([`PsdExportOptions::read_linked_files`]). + read_linked_files: bool, /// Character/paragraph styles written into every type layer's engine data. text_styles: photocraft_doc::TextStyles, mask_fmt: PixelFormat, @@ -102,210 +147,13 @@ fn psd_background(doc: &Document) -> Option { if canvas.is_empty() || s.content_bounds() != canvas { return None; } - let n = s.channels(); - let opaque = s.read_region(canvas).chunks_exact(n).all(|p| p.last().is_some_and(|a| *a >= 1.0)); - opaque.then_some(l.id) + crate::flat::opaque_surface(s, canvas).then_some(l.id) } /// How deep embedded documents are exported inside each other before a smart object is written /// as pixels instead. const MAX_NESTING: u32 = 8; -/// An embedded smart-object source as written to `lnk2`. -#[derive(Clone, Debug)] -struct Source { - uuid: String, - /// Pixel size of the source document. - size: (f64, f64), - dpi: f64, -} - -fn contents_uuid(contents_id: SmartContentsId, bytes: &[u8]) -> String { - let mut identity = blake3::Hasher::new(); - identity.update(&contents_id.0.to_le_bytes()); - identity.update(bytes); - crate::smart_map::uuid_from(identity.finalize().as_bytes()) -} - -/// Smart-object state collected while emitting layers, written into the global blocks at the end. -#[derive(Default)] -struct SmartOut { - /// Nesting depth of this export (embedded documents are exported recursively). - depth: u32, - /// The document's preserved global blocks (embedded files and filter caches from PSD import). - globals: Vec, - /// uuids of the files in the preserved linked-layer blocks. - known: std::collections::HashSet, - /// uuids the layers reference. - used: std::collections::HashSet, - /// Unreferenced preserved files may be dropped (every smart layer's file id is known). - prune_ok: bool, - /// Files to embed. - add: Vec, - /// Sources converted so far, by contents identity and bytes (`Err` = can't be embedded). - converted: std::collections::HashMap<(SmartContentsId, [u8; 32]), Result>, - /// Each placed layer has its own id, even when it duplicates an imported layer. - placed_used: std::collections::HashSet, - /// Original instances reserve their imported placement regardless of layer order. - placed_owners: std::collections::HashMap, - /// Legacy records without a placed id stay unchanged for the original instance. - unplaced_owners: std::collections::HashMap, - /// The imported identity that keeps each original linked UUID. - linked_contents: std::collections::HashMap, - /// Independent copies of preserved linked items, including unavailable external sources. - linked_copies: std::collections::BTreeMap<(String, SmartContentsId), photocraft_doc::PsdGlobalBlock>, - /// Embedded documents decoded while converting them, by uuid (for the filter caches). - docs: std::collections::HashMap, - /// Source composites (document pixel format, source space) by uuid; `None` = unreadable. - composites: std::collections::HashMap, photocraft_geom::Rect)>>, - /// Preserved `FEid` items (by placed id). - old_fx: Vec, - /// Placed ids whose preserved `FEid` item stays. - keep_fx: std::collections::HashSet, - /// Regenerated `FEid` items. - new_fx: Vec, - /// The unfiltered pixels of each smart object whose filter mask shows: its layer record holds - /// the filtered pixels without the mask, as Photoshop's do (`compose::filter_mask`). - unfiltered: std::collections::HashMap, -} - -/// Whether a smart object's filter mask shows: enabled over at least one visible, enabled filter. -fn masked(sm: &photocraft_doc::SmartObject) -> bool { - sm.filter_mask.as_ref().is_some_and(|m| m.enabled) && sm.filters_enabled && sm.smart_filters.iter().any(|f| f.visible) -} - -/// `FEid` data padded to 4 bytes inside the block length: Photoshop counts the padding in the -/// length and can't open a file whose `FEid` is padded outside it. -fn padded_fx(fx: &photocraft_psd::filter_effects::FilterEffects) -> Vec { - let mut d = fx.to_bytes(); - d.resize(d.len().next_multiple_of(4), 0); - d -} - -const LINK_KEYS: [&[u8; 4]; 4] = [b"lnk2", b"lnk3", b"lnkD", b"lnkE"]; -const FX_KEYS: [&[u8; 4]; 2] = [b"FEid", b"FXid"]; - -impl SmartOut { - fn new(doc: &Document, depth: u32) -> Self { - let globals = doc.metadata.psd_global_blocks.clone(); - let known = globals.iter().filter(|(_, k, _)| LINK_KEYS.contains(&k)).flat_map(|(_, _, d)| crate::linked::block_uuids(d)).collect(); - let old_fx = globals - .iter() - .filter(|(_, k, _)| FX_KEYS.contains(&k)) - .filter_map(|(_, _, d)| photocraft_psd::filter_effects::FilterEffects::parse(d).ok()) - .flat_map(|fx| fx.items) - .collect(); - let mut linked_contents = std::collections::HashMap::new(); - let mut placed_owners = std::collections::HashMap::new(); - let mut unplaced_owners = std::collections::HashMap::new(); - let mut layers = doc.walk(); - // Duplicating a layer clears psd_id and gives it a newer runtime id. - layers.sort_by_key(|(_, _, layer)| (layer.psd_id.is_none(), layer.id)); - for (_, _, layer) in layers { - if let LayerContent::Smart(sm) = &layer.content - && let photocraft_doc::SmartSource::Linked { path } = &sm.source - && let Some(placed) = sm.psd_raw.as_ref().and_then(|d| crate::smart_map::parse_sold(d)) - && placed.idnt == *path - { - linked_contents.entry(path.clone()).or_insert(sm.contents_id); - if placed.placed.is_empty() { - unplaced_owners.entry(sm.contents_id).or_insert(layer.id); - } else { - placed_owners.entry(placed.placed).or_insert(layer.id); - } - } - } - SmartOut { depth, globals, known, prune_ok: true, old_fx, linked_contents, placed_owners, unplaced_owners, ..Default::default() } - } - - /// The document's global blocks with the linked-layer files and filter caches brought up to - /// date: unreferenced files dropped (when every reference is known), new files appended, - /// filter caches kept or regenerated per smart object. Unchanged blocks stay byte-identical. - fn finish(&self, blocks: Vec) -> Vec { - use std::sync::Arc; - let keep = |u: &str| !self.prune_ok || self.used.contains(u); - let mut out = Vec::with_capacity(blocks.len() + 2); - let mut added = false; - let mut fx_placed = false; - for (sig, key, data) in blocks { - if LINK_KEYS.contains(&&key) { - let add: &[crate::linked::LinkedFile] = if !added && &key == b"lnk2" { &self.add } else { &[] }; - added |= &key == b"lnk2"; - match crate::linked::rebuild_block(&data, &keep, add) { - None => out.push((sig, key, data)), - Some(d) if d.is_empty() => {} - Some(d) => out.push((sig, key, Arc::new(d))), - } - } else if FX_KEYS.contains(&&key) { - let Ok(mut fx) = photocraft_psd::filter_effects::FilterEffects::parse(&data) else { - out.push((sig, key, data)); - continue; - }; - let before = fx.items.len(); - fx.items.retain(|i| self.keep_fx.contains(&i.id)); - let fresh = !fx_placed && !self.new_fx.is_empty(); - if fresh { - fx.items.extend(self.new_fx.iter().cloned()); - fx_placed = true; - } - if fx.items.len() == before && !fresh { - out.push((sig, key, data)); - } else if !fx.items.is_empty() { - out.push((sig, key, Arc::new(padded_fx(&fx)))); - } - } else { - out.push((sig, key, data)); - } - } - if !added && !self.add.is_empty() { - let data: Vec = self.add.iter().flat_map(crate::linked::encode_linked_file).collect(); - out.push((*b"8BIM", *b"lnk2", Arc::new(data))); - } - for (sig, key, data) in self.linked_copies.values() { - if let Some((_, _, block)) = out.iter_mut().find(|(s, k, _)| s == sig && k == key) { - Arc::make_mut(block).extend_from_slice(data); - } else { - out.push((*sig, *key, data.clone())); - } - } - if !fx_placed && !self.new_fx.is_empty() { - let fx = photocraft_psd::filter_effects::FilterEffects { version: 3, items: self.new_fx.clone() }; - out.push((*b"8BIM", *b"FEid", Arc::new(padded_fx(&fx)))); - if !out.iter().any(|(_, k, _)| k == b"FMsk") { - // Filter mask overlay: RGB red at 50 %, Photoshop's default. - out.push((*b"8BIM", *b"FMsk", Arc::new(vec![0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 50]))); - } - } - out - } -} - -/// The source size of a smart object whose file we can't decode: its rendered bounds taken back -/// through the inverse transform (the source's top-left is the origin). -fn size_from_layer(sm: &photocraft_doc::SmartObject) -> (f64, f64) { - let r = sm.cache.as_ref().map(Surface::content_bounds).filter(|r| !r.is_empty()); - let (Some(r), Some(inv)) = (r, sm.transform.inverse()) else { return (1.0, 1.0) }; - let [a, b, c, d, e, f] = inv.m; - let (mut w, mut h) = (1.0f64, 1.0f64); - for (x, y) in [(r.x0, r.y0), (r.x1, r.y0), (r.x1, r.y1), (r.x0, r.y1)] { - let (x, y) = (f64::from(x), f64::from(y)); - w = w.max((a * x + c * y + e).round()); - h = h.max((b * x + d * y + f).round()); - } - (w, h) -} - -fn upsert(raw: &mut Vec<([u8; 4], Vec)>, key: &[u8; 4], data: Vec, before: Option<&[u8; 4]>) { - if let Some(e) = raw.iter_mut().find(|(k, _)| k == key) { - e.1 = data; - return; - } - match before.and_then(|b| raw.iter().position(|(k, _)| k == b)) { - Some(i) => raw.insert(i, (*key, data)), - None => raw.push((*key, data)), - } -} - fn h_of(r: photocraft_geom::Rect) -> usize { r.height() as usize } @@ -340,7 +188,7 @@ impl Ex { let compression = if w == 0 || h == 0 { Compression::Raw } else { self.compression() }; ChannelData::encode(id, compression, plane, w, h, depth, self.version) .or_else(|_| ChannelData::encode(id, Compression::Raw, plane, w, h, depth, self.version)) - .unwrap_or(ChannelData { id, compression: Some(Compression::Raw), data: plane.to_vec() }) + .unwrap_or(ChannelData::new(id, Some(Compression::Raw), plane.to_vec())) } fn empty_channels(&self) -> Vec { @@ -359,6 +207,13 @@ impl Ex { if r.is_empty() { return (PsdRect::default(), self.empty_channels(), warnings); } + // A layer record is one dense rectangle, so a few pixels far apart make a box of mostly + // nothing, gathered and then copied again. Past the cap the layer is saved empty, said so. + let dense = u64::from(r.width()).saturating_mul(u64::from(r.height())).saturating_mul(s.format().bytes_per_pixel() as u64); + if dense > MAX_LAYER_GATHER_BYTES { + warnings.push(format!("layer \"{name}\" spans {}×{} px, too large to write: saved without its pixels", r.width(), r.height())); + return (PsdRect::default(), self.empty_channels(), warnings); + } // The interleaved gather is the sequential pass for full-canvas layers, so it runs // in row bands on all cores and the band results concatenate in order. let row_len = r.width() as usize; @@ -439,7 +294,11 @@ impl Ex { self.warnings.extend(warnings); let mut mask = MaskData::None; // Vector mask density/feather live in the mask parameters, with or without a user mask. - let vector = l.vector_mask.as_ref().map(|vm| (vm.density, vm.feather)); + // A shape layer's path is its vector mask, with the shape's own density and feather. + let vector = match &l.content { + LayerContent::Shape(sh) if l.vector_mask.is_none() => Some((sh.density, sh.feather)), + _ => l.vector_mask.as_ref().map(|vm| (vm.density, vm.feather)), + }; if let Some(m) = &l.mask { let (md, ch) = self.mask(m, vector); mask = md; @@ -591,16 +450,29 @@ impl Ex { // It parses here, but Photoshop requires four-byte alignment, so repair // that legacy record on re-save rather than preserving the broken bytes. let legacy_phfl = k == b"phfl" && d.starts_with(&[0, 2]) && d.len() % 4 != 0; - !legacy_phfl && adjust_map::ADJUSTMENT_KEYS.contains(&k) && adjust_map::parse(k, d, cged.as_deref(), channels) == *a + // Older PhotoCraft builds wrote `grdm` 2 bytes short of four-byte alignment, + // which makes Photoshop refuse the file (#8070): regenerate it too. + let legacy_grdm = k == b"grdm" && adjust_map::is_unpadded_photocraft_gradient_map(d); + !legacy_phfl && !legacy_grdm && adjust_map::ADJUSTMENT_KEYS.contains(&k) && adjust_map::parse(k, d, cged.as_deref(), channels) == *a }); // A Gradient Map whose stops are unchanged keeps the file's own block (stops in // the document's CMYK/Lab/Gray space, smoothness, method) with Reverse and Dither // updated in place. let keep = keep || (matches!(a, photocraft_doc::Adjustment::GradientMap { .. }) - && raw.iter_mut().any(|(k, d)| k == b"grdm" && adjust_map::retarget_gradient_map(d, a, channels))); + && raw.iter_mut().any(|(k, d)| { + k == b"grdm" && !adjust_map::is_unpadded_photocraft_gradient_map(d) && adjust_map::retarget_gradient_map(d, a, channels) + })); + // An adjustment type no mapper knows (#6575) is its block: kept verbatim, and + // never written a second time beside a stale copy. + let own = match a { + photocraft_doc::Adjustment::Unsupported { psd_key, .. } => Some(psd_key.as_bytes()), + _ => None, + }; + let keep = keep + || matches!(a, photocraft_doc::Adjustment::Unsupported { raw: r, .. } if raw.iter().any(|(k, d)| own == Some(&k[..]) && d[..] == r[..])); if !keep { - raw.retain(|(k, _)| !adjust_map::ADJUSTMENT_KEYS.contains(&k) && k != b"CgEd"); + raw.retain(|(k, _)| !adjust_map::ADJUSTMENT_KEYS.contains(&k) && k != b"CgEd" && own != Some(&k[..])); let w = adjust_map::write(a); if w.is_empty() { self.warnings.push(format!("layer \"{}\": {} adjustment is not yet written to PSD", l.name, a.label())); @@ -625,6 +497,10 @@ impl Ex { crate::vector_map::shape_blocks(sh, &mut raw, w, h, self.dpi); } LayerContent::Text(t) => { + if matches!(t.shape, photocraft_doc::text::TextShape::Area { .. }) { + self.warnings + .push(format!("layer \"{}\": area type is saved as paragraph text over its path's bounds (PSD area type is not written yet)", l.name)); + } // Text layers without PSD data (created here) get a generated TySh. let generated = t.psd_raw.is_none().then(|| std::sync::Arc::new(photocraft_text::psd::build_tysh(t, self.dpi, None))); let src = t.psd_raw.as_ref().or(generated.as_ref()); @@ -666,7 +542,23 @@ impl Ex { let masked = l.vector_mask.is_some(); raw.retain(|(k, _)| !(RASTER_STALE_KEYS.contains(&k) || adjust_map::ADJUSTMENT_KEYS.contains(&k) || k == b"CgEd" || (k == b"vstk" && masked))); } - LayerContent::Group(_) => {} + // A Frame's stroke (`vstk`), kept byte-identical while it still decodes to the model. + LayerContent::Group(g) => { + let pos = raw.iter().position(|(k, _)| k == b"vstk"); + let old = pos.and_then(|i| raw.get(i)).and_then(|(_, d)| crate::vector_map::parse_vstk(d, self.dpi)); + // One we can't decode stays as it was unless the model has a stroke to write. + let stale = match &old { + Some(v) => v.stroke != g.frame_stroke, + None => g.frame_stroke.is_some(), + }; + if stale { + let data = crate::vector_map::vstk_bytes(g.frame_stroke.as_ref(), old.is_none_or(|v| v.fill_enabled), self.dpi); + match pos.and_then(|i| raw.get_mut(i)) { + Some(e) => e.1 = data, + None => raw.push((*b"vstk", data)), + } + } + } } // Effects reference point: written from the field (in place, keeping block order). match l.effects.reference { @@ -698,9 +590,24 @@ impl Ex { let id = self.layer_ids.get(&l.id).copied().unwrap_or(0); crate::comps_map::set_cmls(&mut raw, crate::comps_map::write_cmls(comps, last.as_ref(), l, id)); } + self.layer_xmp_block(l, &mut raw); regenerated.into_iter().chain(raw).map(|(k, d)| TaggedBlock::new(k, d)).collect() } + /// Writes the layer's XMP packet into its `shmd` block (created when it has none). Import + /// takes the packet out of `shmd`, so a layer without one keeps the block as it was. + fn layer_xmp_block(&mut self, l: &Layer, raw: &mut Vec<([u8; 4], Vec)>) { + let Some(xmp) = l.xmp.as_deref() else { return }; + let pos = raw.iter().position(|(k, _)| k == b"shmd"); + match photocraft_psd::metadata::put_layer_xmp(pos.map(|i| raw[i].1.as_slice()), xmp) { + Ok(data) => match pos { + Some(i) => raw[i].1 = data, + None => raw.push((*b"shmd", data)), + }, + Err(e) => self.warnings.push(format!("layer \"{}\": its XMP metadata was not saved (its metadata block can't be read: {e})", l.name)), + } + } + /// Keeps, regenerates or removes the `vmsk`/`vsms` block of a non-shape layer. fn vector_mask_block(&mut self, l: &Layer, raw: &mut Vec<([u8; 4], Vec)>) { let (w, h) = (self.canvas.width(), self.canvas.height()); @@ -929,15 +836,22 @@ impl Ex { SmartSource::Linked { path } => { #[cfg(not(target_arch = "wasm32"))] { - let bytes = std::fs::read(path).map_err(|e| format!("the linked file {path} can't be read: {e}"))?; + if !self.read_linked_files { + return Err(format!("the linked file {path} is not read for this export")); + } + let file = crate::doc_path::resolve(path, self.document.as_deref()).map_err(|e| format!("the linked file {path} is not opened: {e}"))?; + // Regular files only, in bounded reads: a link to a FIFO would block the save + // and one to a device such as `/dev/zero` would never end (#8601). + let bytes = photocraft_format::read_file(&file).map_err(|e| format!("the linked file {path} can't be read: {e}"))?; let name = path.rsplit(['/', '\\']).next().unwrap_or(path).to_string(); + let source = self.embed(sm.contents_id, &name, &bytes, true)?; self.warnings.push(format!("the linked smart object {name} was embedded (PSD export keeps no external links)")); - self.embed(sm.contents_id, &name, &bytes) + Ok(source) } #[cfg(target_arch = "wasm32")] Err(format!("the linked file {path} can't be read here")) } - SmartSource::Embedded { file_name, bytes } => self.embed(sm.contents_id, file_name, bytes), + SmartSource::Embedded { file_name, bytes } => self.embed(sm.contents_id, file_name, bytes, false), } } @@ -982,33 +896,43 @@ impl Ex { /// Adds `bytes` as an embedded file (converted to PSB when it is a `.pcraft` bundle), once /// per contents identity and bytes. Equal bytes in independent objects stay independent. - fn embed(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8]) -> Result { - let key = (contents_id, *blake3::hash(bytes).as_bytes()); + /// `linked`: the bytes were read from a path the document names, so only a file PhotoCraft + /// can open is embedded (a link to a text or settings file is written as pixels instead). + fn embed(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8], linked: bool) -> Result { + let key = (contents_id, *blake3::hash(bytes).as_bytes(), linked); if let Some(r) = self.smart.converted.get(&key) { return r.clone(); } - let r = self.convert_source(contents_id, file_name, bytes); + let r = self.convert_source(contents_id, file_name, bytes, linked); self.smart.converted.insert(key, r.clone()); r } - fn convert_source(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8]) -> Result { + fn convert_source(&mut self, contents_id: SmartContentsId, file_name: &str, bytes: &[u8], linked: bool) -> Result { let (name, data, size, dpi, nested) = if photocraft_format::is_pcraft(bytes) { if self.smart.depth >= MAX_NESTING { return Err(format!("smart objects nest more than {MAX_NESTING} deep")); } let doc = photocraft_format::load_from_bytes(bytes).map_err(|e| format!("its contents can't be read: {e}"))?; - let (file, warnings) = - document_to_psd_nested(&doc, &PsdExportOptions { force_psb: true, ..Default::default() }, self.smart.depth + 1, &Interrupt::NONE); + let (file, warnings) = document_to_psd_nested( + &doc, + &PsdExportOptions { force_psb: true, document_path: self.document.clone(), read_linked_files: self.read_linked_files, ..Default::default() }, + self.smart.depth + 1, + &Interrupt::NONE, + ); let data = file.to_bytes().map_err(|e| format!("its contents can't be written: {e}"))?; let stem = file_name.rsplit_once('.').map_or(file_name, |(a, _)| a); self.warnings.extend(warnings.into_iter().map(|w| format!("smart object {stem}: {w}"))); let size = (f64::from(doc.size.width), f64::from(doc.size.height)); (format!("{stem}.psb"), data, size, f64::from(doc.resolution_dpi), Some(doc)) } else { - // Formats we can't decode (vector PDF/AI…) are still embedded; their size comes from the layer. - let (size, dpi) = - crate::linked::source_geometry(file_name, bytes, &photocraft_raster::Interrupt::NONE).unwrap_or(((0.0, 0.0), f64::from(self.dpi))); + // Embedded formats we can't decode (vector PDF/AI…) are kept; their size comes from the + // layer. A linked file we can't decode is not embedded. + let (size, dpi) = match crate::linked::source_geometry(file_name, bytes, &photocraft_raster::Interrupt::NONE) { + Ok(geometry) => geometry, + Err(e) if linked => return Err(format!("the linked file {file_name} is not an image PhotoCraft can open: {e}")), + Err(_) => ((0.0, 0.0), f64::from(self.dpi)), + }; (file_name.to_string(), bytes.to_vec(), size, dpi, None) }; let uuid = contents_uuid(contents_id, &data); @@ -1109,976 +1033,5 @@ impl Ex { } } -/// Resource 1026 (Layer › Link Layers): one u16 group id per layer record, in the order -/// [`Ex::emit`] writes records (a group's bounding divider, its children, then the group). -/// Imported ids that fit in a u16 are kept, so unchanged files write the same bytes; otherwise -/// groups are renumbered by first appearance. `None` when no layer is linked. -fn link_group_resource(layers: &[Layer]) -> Option> { - fn walk(layers: &[Layer], out: &mut Vec>) { - for l in layers { - if let LayerContent::Group(g) = &l.content { - out.push(None); - walk(&g.children, out); - } - out.push(l.link_group); - } - } - let mut per = Vec::new(); - walk(layers, &mut per); - if per.iter().all(Option::is_none) { - return None; - } - let fits = per.iter().flatten().all(|&g| (1..=u64::from(u16::MAX)).contains(&g)); - let mut seen: Vec = Vec::new(); - let ids = per.iter().map(|g| match *g { - None => 0u16, - Some(g) if fits => g as u16, - Some(g) => { - let i = seen.iter().position(|&s| s == g).unwrap_or_else(|| { - seen.push(g); - seen.len() - 1 - }); - u16::try_from(i + 1).unwrap_or(u16::MAX) - } - }); - Some(ids.flat_map(u16::to_be_bytes).collect()) -} - -/// Whether the layer's effects still equal what its preserved `lmfx`/`lfx2` -/// decodes to. `None` when there is nothing preserved or it can't be decoded. -/// Block keys a smart object's `psd_raw` may be written under, in priority order. The data says -/// which it is: `PlLd` holds a `plcL` structure, `SoLd`/`SoLE` a `soLD` one (Adobe PSD spec, -/// "Placed Layer" and "Placed Layer Data"). Writing one under the other's key makes the block -/// unreadable. -fn smart_keys(data: Option<&[u8]>) -> &'static [&'static [u8; 4]] { - match data.and_then(|d| d.get(..4)) { - Some(b"plcL") => &[b"PlLd"], - Some(b"soLD") => &[b"SoLd", b"SoLE"], - _ => &[b"SoLd", b"PlLd", b"SoLE"], - } -} - -fn effects_unchanged(l: &Layer) -> Option { - let src = l.psd_blocks.iter().find(|(k, _)| k == b"lmfx").map(|(_, d)| d.clone()).or_else(|| l.effects.psd_raw.clone())?; - let (m, items) = crate::effects_map::parse_lfx2(&src)?; - Some(m == l.effects.enabled && items == l.effects.items) -} - -pub(crate) fn legacy_name(s: &str) -> Vec { - s.chars().map(|c| if c.is_ascii() && !c.is_ascii_control() { c as u8 } else { b'?' }).take(255).collect() -} - -pub(crate) fn unicode_names_resource(names: &[&str]) -> Vec { - let mut v = Vec::new(); - for n in names { - let units: Vec = n.encode_utf16().chain(std::iter::once(0)).collect(); - v.extend_from_slice(&(units.len() as u32).to_be_bytes()); - for u in units { - v.extend_from_slice(&u.to_be_bytes()); - } - } - v -} - -pub(crate) fn pascal_names_resource(names: &[&str]) -> Vec { - let mut v = Vec::new(); - for n in names { - let b = legacy_name(n); - v.push(b.len() as u8); - v.extend_from_slice(&b); - } - v -} - -pub(crate) fn guides_resource(doc: &Document) -> Vec { - let mut v = Vec::new(); - v.extend_from_slice(&1u32.to_be_bytes()); - v.extend_from_slice(&576u32.to_be_bytes()); - v.extend_from_slice(&576u32.to_be_bytes()); - let n = doc.guides.horizontal.len() + doc.guides.vertical.len(); - v.extend_from_slice(&(n as u32).to_be_bytes()); - for (list, dir) in [(&doc.guides.vertical, 0u8), (&doc.guides.horizontal, 1u8)] { - for p in list { - v.extend_from_slice(&((p * 32.0).round() as i32).to_be_bytes()); - v.push(dir); - } - } - v -} - -/// Path resources: saved paths (2000+), the work path (1025) and the clipping path name (2999), -/// the last only when the imported one doesn't already name the document's clipping path. -pub(crate) fn path_resources(doc: &Document, warnings: &mut Vec) -> Vec { - use crate::vector_map::{CLIPPING_PATH, WORK_PATH, path_from_resource, path_to_records}; - let mut out = Vec::new(); - let (w, h) = (doc.size.width, doc.size.height); - let max = (*crate::vector_map::SAVED_PATHS.end() - *crate::vector_map::SAVED_PATHS.start() + 1) as usize; - if doc.paths.len() > max { - warnings.push(format!("only {max} saved paths fit in PSD; the rest were dropped")); - } - for (i, p) in doc.paths.iter().take(max).enumerate() { - let data = match &p.psd_raw { - Some(r) if path_from_resource(r, w, h).as_ref() == Some(&p.path) => r.to_vec(), - _ => path_to_records(&p.path, w, h).to_bytes(), - }; - let mut r = ImageResource::new(crate::vector_map::SAVED_PATHS.start() + i as u16, data); - r.name = legacy_name(&p.name); - out.push(r); - } - if let Some(wp) = &doc.work_path { - out.push(ImageResource::new(WORK_PATH, path_to_records(wp, w, h).to_bytes())); - } - let raw_clip = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == CLIPPING_PATH); - let raw_name = raw_clip.map(|(_, _, d)| { - let n = usize::from(d.first().copied().unwrap_or(0)); - d.get(1..1 + n).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default() - }); - if let Some(c) = &doc.clipping_path - && raw_name.as_deref() != Some(c.name.as_str()) - { - // Pascal name, then flatness as 16.16 fixed (best effort; see vector_map docs). - let mut data = vec![0u8]; - let name = legacy_name(&c.name); - data[0] = name.len() as u8; - data.extend_from_slice(&name); - data.extend_from_slice(&((c.flatness.max(0.0) * 65536.0) as u32).to_be_bytes()); - out.push(ImageResource::new(CLIPPING_PATH, data)); - } - out -} - -/// Whether an imported (unmodelled) resource still describes the document and is written back. -pub(crate) fn keep_imported_resource(doc: &Document, id: u16, data: &[u8]) -> bool { - // Measurement scale / count resources: only while they still describe the document. - if !crate::annotations_map::keep_resource(doc, id) { - return false; - } - // A preserved clipping-path resource is only valid while it names the current one. - if id == crate::vector_map::CLIPPING_PATH { - let n = usize::from(data.first().copied().unwrap_or(0)); - let raw_name = data.get(1..1 + n).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default(); - return doc.clipping_path.as_ref().is_some_and(|c| c.name == raw_name); - } - true -} - -/// A document-level (global) tagged block. Photoshop pads these to a multiple of 4, and readers -/// such as psd-tools step to the next block that way: an even pad after `CAI ` (77 bytes) -/// misaligned every block after it (#200). -pub(crate) fn global_block(signature: [u8; 4], key: [u8; 4], data: &[u8]) -> TaggedBlock { - let mut tb = TaggedBlock::new(key, data.to_vec()); - tb.signature = signature; - tb.padding = Some(vec![0; (4 - data.len() % 4) % 4]); - tb -} - -fn cmyk_of(fmt: &PixelFormat) -> bool { - fmt.mode == ColorMode::Cmyk -} - -/// The merged image's planes (colour channels, then alpha), big-endian, matted against white -/// when `matte`; with whether any pixel needs the alpha channel and whether any is below 1. -/// `ctl` gets the fraction of rows done after each band; once it is cancelled the rest of the -/// image is left blank (the caller discards it). -fn merged_planes(doc: &Document, fmt: &PixelFormat, cmyk: bool, matte: bool, ctl: &Interrupt) -> (Vec, bool, bool) { - let sample = fmt.sample; - let cc = fmt.mode.color_channels(); - let bps = sample.bytes(); - let n = doc.size.area() as usize; - let plane = n * bps; - let mut planes = vec![0u8; plane * (cc + 1)]; - let (mut has_alpha, mut translucent) = (false, false); - let white = photocraft_raster::from_rgba(fmt, [1.0, 1.0, 1.0, 1.0]); - let canvas = doc.bounds(); - let w = canvas.width() as usize; - let space = photocraft_compose::cmyk_space(doc); - let lab16 = fmt.mode == ColorMode::Lab && sample == SampleType::U16; - let rows = canvas.height().max(1) as f32; - let _ = photocraft_compose::render_bands(doc, canvas, 0, |band| -> Result<(), ()> { - if ctl.cancelled() { - return Err(()); - } - // Retain alpha whenever it differs from opaque at the stored precision. - has_alpha |= band.px.iter().any(|p| match sample { - SampleType::U8 => q255(p[3]) < 255, - SampleType::U16 => (p[3].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 != u16::MAX, - SampleType::F32 => p[3] != 1.0, - }); - translucent |= band.px.iter().any(|p| p[3] < 1.0); - let start = (band.rect.y0 - canvas.y0) as usize * w; - // Converted and encoded on all cores, then copied into each plane. - let parts = crate::pixels::par_map(crate::pixels::bands(band.px.len()), |range| { - // The composite came through the document's CMYK profile: convert back through it too. - photocraft_color::convert::with_cmyk_space(space.as_ref(), || { - let mut out: Vec> = vec![Vec::with_capacity(range.len() * bps); cc + 1]; - let mut v = [0.0f32; 5]; - for p in &band.px[range.clone()] { - photocraft_raster::from_rgba_into(fmt, *p, &mut v); - for c in 0..=cc { - // Matte against white like Photoshop (see `pixels::matte`). - let m = if c < cc && matte { crate::pixels::matte(v[c], v[cc], white[c]) } else { v[c] }; - let x = if cmyk && c < cc { 1.0 - m } else { m }; - // 16-bit Lab a*/b* use Photoshop's 0..65280 scale (`pixels::lab16_chroma`). - let x = if lab16 && (c == 1 || c == 2) { x * (crate::pixels::LAB16_CHROMA_MAX / 65535.0) } else { x }; - encode_be(x, sample, &mut out[c]); - } - } - (range.start, out) - }) - }); - for (at, part) in parts { - for (c, src) in part.iter().enumerate() { - let o = c * plane + (start + at) * bps; - planes[o..o + src.len()].copy_from_slice(src); - } - } - ctl.progress((band.rect.y1 - canvas.y0) as f32 / rows); - Ok(()) - }); - (planes, has_alpha, translucent) -} - -/// The merged image of a file saved without Maximize Compatibility: Photoshop's white placeholder -/// in the document's colour channels, with no alpha. Encoded like [`merged_planes`], so CMYK white -/// is no ink and Lab white is L 100 with neutral a*/b*. -fn placeholder_planes(doc: &Document, fmt: &PixelFormat) -> Vec { - let cc = fmt.mode.color_channels(); - let cmyk = cmyk_of(fmt); - let lab16 = fmt.mode == ColorMode::Lab && fmt.sample == SampleType::U16; - let space = photocraft_compose::cmyk_space(doc); - let white = photocraft_color::convert::with_cmyk_space(space.as_ref(), || photocraft_raster::from_rgba(fmt, [1.0, 1.0, 1.0, 1.0])); - let n = doc.size.area() as usize; - let mut planes = Vec::with_capacity(n * fmt.sample.bytes() * cc); - for c in 0..cc { - let v = white.get(c).copied().unwrap_or(1.0); - let x = if cmyk { 1.0 - v } else { v }; - let x = if lab16 && (c == 1 || c == 2) { x * (crate::pixels::LAB16_CHROMA_MAX / 65535.0) } else { x }; - let mut sample = Vec::with_capacity(4); - encode_be(x, fmt.sample, &mut sample); - planes.extend(sample.iter().copied().cycle().take(n * sample.len())); - } - planes -} - -/// Converts a document to a PSD file model (see [`document_to_psd_with`]). -pub fn document_to_psd(doc: &Document) -> PsdFile { - document_to_psd_with(doc, &PsdExportOptions::default()).0 -} - -/// Converts a document to a PSD/PSB file model, returning warnings about -/// anything that could not be represented. The merged composite is rendered -/// with `photocraft_compose::flatten`. -pub fn document_to_psd_with(doc: &Document, opts: &PsdExportOptions) -> (PsdFile, Vec) { - document_to_psd_nested(doc, opts, 0, &Interrupt::NONE) -} - -/// [`document_to_psd_with`] for a background save (#3389): reports progress through `ctl` after -/// each layer record and each band of the merged composite. Once `ctl` is cancelled it stops -/// early and returns an **incomplete** model: callers check `ctl` and discard it. -pub(crate) fn document_to_psd_interruptible(doc: &Document, opts: &PsdExportOptions, ctl: &Interrupt) -> (PsdFile, Vec) { - document_to_psd_nested(doc, opts, 0, ctl) -} - -/// How much of a PSD conversion the merged composite weighs, in layer records: rendering it -/// (all layers composited) and encoding its planes cost about two full-canvas layers. -const MERGED_WEIGHT: f32 = 2.0; -/// The part of the merged composite's share filled once its bands are rendered and converted; the -/// rest is compressing its planes. -const MERGED_RENDERED: f32 = 0.8; - -/// Adds headroom for compressed channel streams and their per-row length tables. -fn estimate_encoded_size(raw_bytes: u64, rows: u64, structural_bytes: u64) -> Option { - raw_bytes.checked_add(raw_bytes.checked_add(63)?.checked_div(64)?)?.checked_add(rows.checked_mul(8)?)?.checked_add(structural_bytes) -} - -fn add_plane_estimate(raw_bytes: &mut u64, rows: &mut u64, width: u64, height: u64, channels: u64, bytes_per_sample: u64) -> Option<()> { - let plane_bytes = width.checked_mul(height)?.checked_mul(channels)?.checked_mul(bytes_per_sample)?; - let plane_rows = height.checked_mul(channels)?; - *raw_bytes = raw_bytes.checked_add(plane_bytes)?; - *rows = rows.checked_add(plane_rows)?; - Some(()) -} - -fn exceeds_psd_size_limit(estimated_size: Option) -> bool { - estimated_size.is_none_or(|size| size > PSD_MAX_FILE_SIZE) -} - -fn surface_dimensions(surface: &Surface) -> Option<(u64, u64)> { - let bounds = surface.content_bounds(); - Some((u64::from(bounds.width()), u64::from(bounds.height()))) -} - -pub(crate) fn estimate_psd_size(doc: &Document) -> Option { - let mut raw_bytes = 0; - let mut rows = 0; - let mut structural_bytes = PSD_ESTIMATE_FIXED_OVERHEAD; - - let width = u64::from(doc.size.width); - let height = u64::from(doc.size.height); - - if doc.mode == ColorMode::Multichannel { - let channels = u64::try_from(doc.channels.len().clamp(1, 56)).ok()?; - let sample_bytes = u64::try_from(if doc.depth == SampleType::F32 { SampleType::U16.bytes() } else { doc.depth.bytes() }).ok()?; - add_plane_estimate(&mut raw_bytes, &mut rows, width, height, channels, sample_bytes)?; - } else { - let format = doc.pixel_format(); - let color_channels = u64::try_from(format.mode.color_channels()).ok()?; - let bytes_per_sample = u64::try_from(format.sample.bytes()).ok()?; - let layer_channels = color_channels.checked_add(1)?; - - for (_, _, layer) in doc.walk() { - structural_bytes = structural_bytes.checked_add(PSD_ESTIMATE_LAYER_OVERHEAD)?.checked_add(u64::try_from(layer.name.len()).ok()?)?; - for (_, data) in &layer.psd_blocks { - structural_bytes = structural_bytes.checked_add(u64::try_from(data.len()).ok()?)?; - } - - let surface = match &layer.content { - LayerContent::Fill(fill) => { - let mut fill_width = width; - let mut fill_height = height; - if let Some(cache) = &layer.fill_cache - && cache.fill == *fill - { - let (cache_width, cache_height) = surface_dimensions(&cache.surface)?; - fill_width = fill_width.max(cache_width); - fill_height = fill_height.max(cache_height); - } - add_plane_estimate(&mut raw_bytes, &mut rows, fill_width, fill_height, layer_channels, bytes_per_sample)?; - None - } - _ => layer.surface(), - }; - if let Some(surface) = surface { - let (surface_width, surface_height) = surface_dimensions(surface)?; - add_plane_estimate(&mut raw_bytes, &mut rows, surface_width, surface_height, layer_channels, bytes_per_sample)?; - } - if let Some(mask) = &layer.mask { - let (mask_width, mask_height) = surface_dimensions(&mask.surface)?; - add_plane_estimate(&mut raw_bytes, &mut rows, mask_width, mask_height, 1, bytes_per_sample)?; - } - if let LayerContent::Smart(smart) = &layer.content - && let photocraft_doc::SmartSource::Embedded { bytes, .. } = &smart.source - { - structural_bytes = structural_bytes.checked_add(u64::try_from(bytes.len()).ok()?)?; - } - } - - let max_extra_channels = 56usize.saturating_sub(format.mode.color_channels() + 1); - let mut extra_channels = doc.channels.len().min(max_extra_channels); - if doc.quick_mask.is_some() && extra_channels < max_extra_channels { - extra_channels += 1; - } - let merged_channels = color_channels.checked_add(1)?.checked_add(u64::try_from(extra_channels).ok()?)?; - add_plane_estimate(&mut raw_bytes, &mut rows, width, height, merged_channels, bytes_per_sample)?; - for channel in doc.channels.iter().take(extra_channels) { - structural_bytes = structural_bytes.checked_add(u64::try_from(channel.name.len()).ok()?)?; - } - } - - structural_bytes = structural_bytes - .checked_add(u64::try_from(doc.icc_profile.as_ref().map_or(0, |profile| profile.len())).ok()?)? - .checked_add(u64::try_from(doc.metadata.xmp.as_ref().map_or(0, String::len)).ok()?)? - .checked_add(u64::try_from(doc.metadata.exif.as_ref().map_or(0, |data| data.len())).ok()?)?; - for (_, name, data) in &doc.metadata.psd_resources { - structural_bytes = structural_bytes.checked_add(u64::try_from(name.len()).ok()?)?.checked_add(u64::try_from(data.len()).ok()?)?; - } - for (_, _, data) in &doc.metadata.psd_global_blocks { - structural_bytes = structural_bytes.checked_add(u64::try_from(data.len()).ok()?)?; - } - - estimate_encoded_size(raw_bytes, rows, structural_bytes) -} - -/// The signature a kept tagged block is written with. `8B64` exists only in PSB files: a PSD -/// with an `8B64` block (kept from a PSB source, e.g. its global `FMsk` or `cinf`) is refused by -/// Photoshop as "not compatible with this version" (measured in Photoshop 27.11). -fn block_signature(sig: [u8; 4], version: Version) -> [u8; 4] { - if version == Version::Psd && sig == *b"8B64" { *b"8BIM" } else { sig } -} - -pub(crate) fn psd_version(doc: &Document, force_psb: bool, size_exceeds_limit: bool) -> Version { - let exceeds_dimensions = doc.size.width > 30_000 || doc.size.height > 30_000; - if force_psb || exceeds_dimensions || size_exceeds_limit { Version::Psb } else { Version::Psd } -} - -pub(crate) fn psd_size_exceeds_limit(doc: &Document) -> bool { - exceeds_psd_size_limit(estimate_psd_size(doc)) -} - -/// [`document_to_psd_with`] for a document embedded `depth` smart objects deep. -fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt) -> (PsdFile, Vec) { - if doc.mode == ColorMode::Multichannel { - return crate::multichannel_map::document_to_psd(doc, opts.force_psb); - } - let fmt = doc.pixel_format(); - let sample = fmt.sample; - let cc = fmt.mode.color_channels(); - let big = doc.size.width > 30_000 || doc.size.height > 30_000; - let size_exceeds_limit = !opts.force_psb && !big && psd_size_exceeds_limit(doc); - let version = psd_version(doc, opts.force_psb, size_exceeds_limit); - let mut ex = Ex { - fmt, - dpi: doc.resolution_dpi, - text_styles: doc.text_styles.clone(), - text_index: Default::default(), - mask_fmt: PixelFormat::new(ColorMode::Grayscale, sample, false), - cc, - cmyk: fmt.mode == ColorMode::Cmyk, - version, - next_id: 0, - used_ids: Default::default(), - layer_ids: Default::default(), - canvas: doc.bounds(), - records: Vec::new(), - warnings: Vec::new(), - guides: doc.guides.clone(), - comps: (!crate::comps_map::comps_unchanged(doc)).then(|| (doc.layer_comps.clone(), doc.last_document_state.clone())), - smart: SmartOut::new(doc, depth), - patterns: doc.patterns.clone(), - background: psd_background(doc), - }; - if big && !opts.force_psb { - ex.warnings.push("document exceeds 30000 px; written as PSB".into()); - } else if size_exceeds_limit && !opts.force_psb { - ex.warnings.push("estimated encoded size exceeds 2 GB; written as PSB".into()); - } - if fmt.mode != doc.mode { - ex.warnings.push(format!("{:?} document written as {:?}", doc.mode, fmt.mode)); - } - // Assign layer ids up front (stable across round trips): keep unique - // `psd_id`s, number the rest after them; dividers come after all. - { - let walk = doc.walk(); - let mut used = std::collections::HashSet::new(); - for (_, _, l) in &walk { - if let Some(id) = l.psd_id - && used.insert(id) - { - ex.layer_ids.insert(l.id, id); - } - } - let mut next = 1u32; - for (_, _, l) in &walk { - if let std::collections::hash_map::Entry::Vacant(e) = ex.layer_ids.entry(l.id) { - while used.contains(&next) { - next += 1; - } - used.insert(next); - e.insert(next); - } - } - ex.next_id = used.iter().copied().max().unwrap_or(0); - ex.used_ids = used; - // Text objects' number in the generated `Txt2` (#1348): a preserved `TySh` keeps the - // number it already names — its `Txt2` object must sit at that slot for `apply_txt2` — - // and new layers get the next free number. - let mut used_txt = std::collections::HashSet::new(); - let mut next_txt = 0i32; - ex.text_index = Default::default(); - for (_, _, l) in &walk { - let LayerContent::Text(t) = &l.content else { - continue; - }; - let kept = t.psd_raw.as_deref().and_then(|d| photocraft_text::psd::text_index(d)); - let index = match kept { - Some(i) if used_txt.insert(i) => i, - _ => { - while used_txt.contains(&next_txt) { - next_txt += 1; - } - used_txt.insert(next_txt); - next_txt - } - }; - ex.text_index.insert(l.id, index); - } - } - // Progress: one unit per layer record, then the merged composite (#3389). - let records = doc.walk().len() as f32; - let units = records + MERGED_WEIGHT; - let layers_share = records / units; - let mut done = 0.0f32; - let mut tick = || { - done += 1.0; - ctl.progress(done / units); - !ctl.cancelled() - }; - let space = photocraft_compose::cmyk_space(doc); - photocraft_color::convert::with_cmyk_space(space.as_ref(), || ex.emit(&doc.layers, &mut tick)); - - // Without Maximize Compatibility the merged image is a white placeholder and the composite - // isn't rendered at all. A document without layer records keeps the real image: its pixels - // live nowhere else. - let real_merged = opts.maximize_compatibility || ex.records.is_empty(); - let (mut planes, has_alpha) = if real_merged { - // Merged composite, rendered and encoded in bands (no full-size float composite). Matting - // against white only changes pixels with alpha < 1; if some are slightly translucent but - // all round to opaque (so no alpha channel is written), encode once more without the matte. - let merged_end = layers_share + (1.0 - layers_share) * MERGED_RENDERED; - let (mut planes, has_alpha, translucent) = - crate::with_span(ctl, layers_share, merged_end, |ctl| merged_planes(doc, &fmt, cmyk_of(&fmt), opts.merged_matte, ctl)); - if !has_alpha && translucent && opts.merged_matte { - planes = merged_planes(doc, &fmt, cmyk_of(&fmt), false, &Interrupt::cancel_only(&|| ctl.cancelled())).0; - } - (planes, has_alpha) - } else { - (placeholder_planes(doc, &fmt), false) - }; - let n = doc.size.area() as usize; - if !has_alpha { - planes.truncate(cc * n * sample.bytes()); - } - let canvas = doc.bounds(); - let max_extra = 56 - cc - usize::from(has_alpha); - if doc.channels.len() > max_extra { - ex.warnings.push(format!("only {max_extra} alpha channels fit in PSD; the rest were dropped")); - } - let mut extra: Vec<_> = doc.channels.iter().take(max_extra).collect(); - // Saved in Quick Mask mode: the mask is written as the last extra channel and flagged by - // resource 1022 (quick mask info: channel id, initially-empty flag), as Photoshop does. - let quick_mask_id = match &doc.quick_mask { - Some(q) if extra.len() < max_extra => { - extra.push(q); - Some((cc + usize::from(has_alpha) + extra.len() - 1) as u16) - } - Some(_) => { - ex.warnings.push("Quick Mask was dropped because the PSD channel limit was reached".into()); - None - } - None => None, - }; - for a in &extra { - let s = if a.surface.format() != ex.mask_fmt { a.surface.convert(ex.mask_fmt) } else { a.surface.clone() }; - let bytes = s.to_interleaved(canvas); - planes.extend(deinterleave(&bytes, 1, sample, &[false]).remove(0)); - } - let channels = (cc + usize::from(has_alpha) + extra.len()) as u16; - let header = Header::new(version, doc.size.width, doc.size.height, channels, psd_depth(sample), psd_mode(fmt.mode)); - let mcomp = if sample == SampleType::F32 { Compression::Raw } else { opts.merged_compression }; - let image_data = ImageData::encode(mcomp, &planes, &header) - .or_else(|_| ImageData::encode(Compression::Raw, &planes, &header)) - .unwrap_or(ImageData { compression: Compression::Raw, data: planes }); - ctl.progress(1.0); - - // Resources. - let mut resources = vec![ - // hRes/vRes are always pixels per inch. The display unit written here is inches. - ImageResource::new(ids::RESOLUTION_INFO, ResolutionInfo::from_dpi(f64::from(doc.resolution_dpi)).to_bytes()), - ImageResource::new(ids::GLOBAL_ANGLE, (doc.global_light.angle.round() as i32).to_be_bytes().to_vec()), - ImageResource::new(ids::GLOBAL_ALTITUDE, (doc.global_light.altitude.round() as i32).to_be_bytes().to_vec()), - ]; - if let Some(icc) = &doc.icc_profile { - resources.push(ImageResource::new(ids::ICC_PROFILE, icc.to_vec())); - } - if !doc.guides.horizontal.is_empty() || !doc.guides.vertical.is_empty() { - resources.push(ImageResource::new(1032, guides_resource(doc))); - } - if !extra.is_empty() { - // One entry per extra channel, the merged transparency included (as in every file - // Photoshop opens; one short, Photoshop refuses the file with "unexpected end-of-file"). - let names: Vec<&str> = has_alpha.then_some("Transparency").into_iter().chain(extra.iter().map(|a| a.name.as_str())).collect(); - resources.push(ImageResource::new(1006, pascal_names_resource(&names))); - resources.push(ImageResource::new(1045, unicode_names_resource(&names))); - resources.push(ImageResource::new(crate::channel_map::DISPLAY_INFO, crate::channel_map::display_info(&extra, has_alpha))); - } - if let Some(id) = quick_mask_id { - let mut data = id.to_be_bytes().to_vec(); - data.push(0); - resources.push(ImageResource::new(crate::channel_map::QUICK_MASK_INFO, data)); - } - resources.extend(path_resources(doc, &mut ex.warnings)); - if let Some(groups) = link_group_resource(&doc.layers) { - resources.push(ImageResource::new(ids::LAYER_GROUP_INFO, groups)); - } - // The pixels are saved as they are shown: never let a reader rotate them again. The XMP and - // EXIF resolution follow the ResolutionInfo resource, so no copy contradicts it (#1691). - let ppi = Some((doc.resolution_dpi, doc.resolution_dpi)).filter(|d| d.0 > 0.0); - if let Some(x) = &doc.metadata.xmp { - resources.push(ImageResource::new(ids::XMP, photocraft_codecs::export_xmp(x, ppi).as_bytes().to_vec())); - } - if let Some(e) = &doc.metadata.exif { - resources.push(ImageResource::new(ids::EXIF, photocraft_codecs::export_exif(e, ppi).into_owned())); - } - let mut global_blocks = Vec::new(); - let preserved = ex.smart.finish(crate::annotations_map::export_blocks(doc, crate::pattern_map::export_global_blocks(doc))); - // The type layers' `Txt2` is regenerated (the auto-kern modes live there, and a preserved - // block goes stale as text is edited): one object per `TextIndex`, at the slot each layer's - // `TySh` names (unused slots stay empty; see `photocraft_text::psd::build_txt2`). The block - // it replaces is handed over so a kept object keeps everything the file held beyond its text - // and style runs (Photoshop's `/21 /1` glyph pen positions, …). - let text_layers: Vec<(i32, &photocraft_doc::TextLayer)> = doc - .walk() - .into_iter() - .filter_map(|(_, _, l)| match &l.content { - LayerContent::Text(t) => Some((ex.text_index.get(&l.id).copied().unwrap_or(0), t)), - _ => None, - }) - .collect(); - let previous = (!text_layers.is_empty()).then(|| preserved.iter().find(|(_, key, _)| key == b"Txt2").map(|(_, _, data)| data.to_vec())).flatten(); - // A fresh document of ordinary (Metrics) text has nothing to put in `Txt2`, and Photoshop never - // writes its empty `<< /0 << >> >>` stub — readers such as Photopea reject it (#3514). Keep the - // block when the file had one (round-trip its pen positions/extras) or a layer is Optical (the - // one mode EngineData can't carry; #1348). - let txt2 = - (previous.is_some() || photocraft_text::psd::needs_txt2(&text_layers)).then(|| photocraft_text::psd::build_txt2(&text_layers, previous.as_deref())); - for (sig, key, data) in preserved { - if txt2.is_some() && key == *b"Txt2" { - continue; - } - // A kept layer block (one the import could not read, #2700) describes layers the - // document doesn't have. The document's own layers are written below, and a file holds - // one layer list: a second `Lr16`/`Lr32` would contradict it. - if !ex.records.is_empty() && matches!(&key, b"Lr16" | b"Lr32" | b"Layr") { - continue; - } - global_blocks.push(global_block(block_signature(sig, version), key, &data)); - } - if let Some(txt2) = txt2 { - let mut tb = TaggedBlock::new(*b"Txt2", txt2.clone()); - tb.signature = *b"8BIM"; - tb.padding = Some(vec![0; (4 - txt2.len() % 4) % 4]); - global_blocks.push(tb); - } - let comps_resource = ex.comps.is_some().then(|| crate::comps_map::write_comps_resource(doc)); - let (slices_resource, slices_warning) = crate::slices_map::export_resource(doc, &ex.layer_ids); - if let Some(warning) = slices_warning { - ex.warnings.push(warning); - } - for (id, name, data) in &doc.metadata.psd_resources { - // Layer comps: the preserved list while unchanged, else regenerated (or dropped) below. - if *id == crate::comps_map::LAYER_COMPS && comps_resource.is_some() { - continue; - } - // Slices: the preserved resource while unchanged, else regenerated (or dropped) below. - if *id == crate::slices_map::SLICES && slices_resource.is_some() { - continue; - } - if !keep_imported_resource(doc, *id, data) { - continue; - } - let mut r = ImageResource::new(*id, data.to_vec()); - r.name = legacy_name(name); - resources.push(r); - } - if let Some(data) = crate::annotations_map::fresh_scale_resource(doc) { - resources.push(ImageResource::new(crate::annotations_map::MEASUREMENT_SCALE, data)); - } - if let Some(Some(data)) = comps_resource { - resources.push(ImageResource::new(crate::comps_map::LAYER_COMPS, data)); - } - if let Some(Some(data)) = slices_resource { - resources.push(ImageResource::new(crate::slices_map::SLICES, data)); - } - resources.push(version_info_resource(real_merged)); - - let has_layers = !ex.records.is_empty(); - // Photoshop writes a merged-transparency marker (`Mtrn`, `Mt16`, `Mt32`) before the layer - // block, and won't open a file whose `Lr32` comes first (#5068): the layer block goes right - // after the last kept marker, else first. - let index = global_blocks.iter().rposition(|b| matches!(&b.key, b"Mtrn" | b"Mt16" | b"Mt32")).map_or(0, |i| i + 1); - let placement = match (has_layers, sample) { - (true, SampleType::U16) => LayerInfoPlacement::GlobalBlock { index, signature: *b"8BIM", key: *b"Lr16", padding: None }, - (true, SampleType::F32) => LayerInfoPlacement::GlobalBlock { index, signature: *b"8BIM", key: *b"Lr32", padding: None }, - _ => LayerInfoPlacement::Section, - }; - let records = std::mem::take(&mut ex.records); - let mut layer_info = has_layers.then_some(LayerInfo { merged_alpha: has_alpha, layers: records, padding: None }); - if let Some(info) = &mut layer_info { - // Photoshop pads the layer info to 4 bytes (an even pad misaligns readers after a global - // `Lr16`/`Lr32` block, #200). A length that cannot be computed keeps the even default. - // A global block's length is the exact data length and its padding to 4 follows the block: - // readers that check that length exactly (Unity's PSD Importer) reject it otherwise (#4654). - if matches!(placement, LayerInfoPlacement::GlobalBlock { .. }) { - info.padding = Some(Vec::new()); - } else if info.pad_to(version, 4).is_err() { - info.padding = None; - } - } - // 32-bit files carry Photoshop's HDR toning records as Color Mode Data; Photoshop will not - // open a 32-bit file without them (#291). - let color_mode_data = photocraft_psd::hdr::color_mode_data_for_depth(header.depth); - let file = PsdFile { - header, - color_mode_data, - resources, - layer_info, - layer_info_placement: placement, - global_layer_mask: (has_layers || !global_blocks.is_empty()).then(GlobalLayerMask::default), - global_blocks, - layer_mask_trailing: Vec::new(), - image_data, - }; - (file, ex.warnings) -} - -/// PSD mask parameters for a user mask's and a vector mask's (density, feather); `None` when -/// every value is the default (density 1, feather 0). -fn mask_parameters(user: Option<(f32, f32)>, vector: Option<(f32, f32)>) -> Option { - let density = |v: Option<(f32, f32)>| v.and_then(|(d, _)| (d < 1.0).then(|| q255(d))); - let feather = |v: Option<(f32, f32)>| v.and_then(|(_, f)| (f != 0.0).then_some(f64::from(f))); - let p = - MaskParameters { flags: 0, user_density: density(user), user_feather: feather(user), vector_density: density(vector), vector_feather: feather(vector) }; - let flags = u8::from(p.user_density.is_some()) - | u8::from(p.user_feather.is_some()) << 1 - | u8::from(p.vector_density.is_some()) << 2 - | u8::from(p.vector_feather.is_some()) << 3; - (flags != 0).then_some(MaskParameters { flags, ..p }) -} - #[cfg(test)] -mod tests { - use super::*; - - fn document(width: u32, height: u32) -> Document { - Document::new("size estimate", photocraft_geom::Size::new(width, height), ColorMode::Rgb, SampleType::U8) - } - - /// Photoshop opens a PSD's bottom layer as its Background only when that layer has no - /// transparency channel (measured on Photoshop 27.11: with the channel written, re-exported - /// files reopened with the Background as an ordinary layer). - #[test] - fn a_background_is_written_without_a_transparency_channel() { - use photocraft_color::Color; - let size = photocraft_geom::Size::new(8, 6); - let mut doc = Document::with_background("bg", size, ColorMode::Rgb, SampleType::U8, Color::rgb(0.2, 0.4, 0.6)); - doc.layers.push(Layer::raster("Layer 1", doc.pixel_format())); - let file = document_to_psd(&doc); - let layers = &file.layer_info.as_ref().unwrap().layers; - let ids = |i: usize| layers[i].channels.iter().map(|c| c.id).collect::>(); - assert_eq!(ids(0), vec![0, 1, 2], "the Background has no -1 channel"); - assert_eq!(layers[0].flags.0, LayerFlags::BIT4_USEFUL | LayerFlags::TRANSPARENCY_PROTECTED, "as Photoshop writes it"); - assert!(ids(1).contains(&-1), "other layers keep their transparency"); - // It comes back as the same locked, opaque Background. - let back = crate::import("bg.psd", &file.to_bytes().unwrap()).unwrap().document; - let bg = &back.layers[0]; - assert!(bg.locks.transparency && bg.locks.position, "{:?}", bg.locks); - assert_eq!(bg.surface().unwrap().read_region(back.bounds()), doc.layers[0].surface().unwrap().read_region(doc.bounds())); - // A bottom layer with transparency (or unlocked) is not a Background. - let mut holey = doc.clone(); - holey.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[0.0, 0.0, 0.0, 0.0]); - assert!(document_to_psd(&holey).layer_info.unwrap().layers[0].channels.iter().any(|c| c.id == -1)); - let mut unlocked = doc.clone(); - unlocked.layers[0].locks.position = false; - assert!(document_to_psd(&unlocked).layer_info.unwrap().layers[0].channels.iter().any(|c| c.id == -1)); - } - - /// A flat 1-bit Bitmap file (no layers) opens in Photoshop as its Background; so does it in - /// PhotoCraft, and it is written back as one (no transparency channel). - #[test] - fn a_flat_bitmap_file_opens_and_saves_as_a_background() { - let file = photocraft_psd::testgen::merged_only(Version::Psd, PsdMode::Bitmap, 1, Compression::Raw, 8, 4); - let doc = crate::import("flat.psd", &file.to_bytes().unwrap()).unwrap().document; - let bg = &doc.layers[0]; - assert!(bg.name == "Background" && bg.locks.transparency && bg.locks.position, "{:?}", bg.locks); - let out = document_to_psd(&doc); - assert!(out.layer_info.unwrap().layers[0].channels.iter().all(|c| c.id >= 0)); - } - - #[test] - fn group_divider_id_wraps_without_colliding_with_imported_layer_ids() { - let mut doc = document(8, 8); - let mut group = Layer::group("Imported group", vec![Layer::raster("Child", doc.pixel_format())]); - group.psd_id = Some(u32::MAX); - doc.layers.push(group); - - let file = document_to_psd(&doc); - let ids: Vec = file.layers().iter().filter_map(|layer| layer.layer_id()).collect(); - let unique: std::collections::HashSet = ids.iter().copied().collect(); - assert_eq!(ids.len(), 3, "group, child and closing divider"); - assert_eq!(unique.len(), ids.len(), "no generated ID may collide"); - assert!(unique.contains(&u32::MAX), "preserve the imported group ID"); - assert!(unique.contains(&1), "reuse the first unused positive ID on wrap"); - - let encoded = file.to_bytes().unwrap(); - let parsed = PsdFile::from_bytes(&encoded).unwrap(); - let reloaded_ids: std::collections::HashSet = parsed.layers().iter().filter_map(|layer| layer.layer_id()).collect(); - assert_eq!(unique, reloaded_ids); - } - - /// #5068: Photoshop won't open a file whose `Lr32` comes before the `Mt32` marker it keeps for - /// a 32-bit document's merged transparency (verified in Photoshop 2026 on psd-tools' - /// `transparentbg.psd` re-saved both ways). The layer block follows a kept marker. - #[test] - fn layer_block_follows_a_kept_merged_transparency_marker() { - for (depth, key, marker) in [(SampleType::F32, *b"Lr32", *b"Mt32"), (SampleType::U16, *b"Lr16", *b"Mt16"), (SampleType::F32, *b"Lr32", *b"Mtrn")] { - let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, depth); - let mut s = Surface::new(doc.pixel_format()); - s.fill_rect(photocraft_geom::Rect::new(1, 1, 5, 4), &[0.5, 0.2, 0.9, 1.0]); - doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(s))); - doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), (*b"8BIM", marker, std::sync::Arc::new(Vec::new()))]; - let saved = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); - let LayerInfoPlacement::GlobalBlock { index, key: written, .. } = saved.layer_info_placement else { panic!("{depth:?}: a global layer block") }; - assert_eq!(written, key); - let at = saved.global_blocks.iter().position(|b| b.key == marker).expect("the marker is kept"); - assert!(at < index, "{depth:?}: {marker:?} at {at}, the layer block at {index}"); - } - // Without a marker the layer block stays first. - let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, SampleType::F32); - doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(Surface::new(doc.pixel_format())))); - doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new()))]; - let saved = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); - assert!(matches!(saved.layer_info_placement, LayerInfoPlacement::GlobalBlock { index: 0, .. })); - } - - #[test] - fn unreadable_lr16_warns_on_import_and_is_not_saved_twice() { - // #2700: a 16-bit file whose Lr16 block claims five layers but holds none. - let mut file = photocraft_psd::testgen::merged_only(Version::Psd, PsdMode::Rgb, 16, Compression::Raw, 8, 6); - file.global_layer_mask = Some(GlobalLayerMask::default()); - file.global_blocks.push(TaggedBlock::new(*b"Lr16", vec![0, 5, 0, 0])); - let imported = crate::import("broken.psd", &file.to_bytes().unwrap()).unwrap(); - assert!(imported.warnings.iter().any(|w| w.contains("Lr16") && w.contains("could not be read")), "{:?}", imported.warnings); - - let saved = PsdFile::from_bytes(&document_to_psd(&imported.document).to_bytes().unwrap()).unwrap(); - let ours = matches!(saved.layer_info_placement, LayerInfoPlacement::GlobalBlock { key, .. } if key == *b"Lr16"); - let kept = saved.global_blocks.iter().filter(|b| b.key == *b"Lr16").count(); - assert_eq!((ours, kept), (true, 0), "exactly one Lr16 block: the document's own layers"); - assert_eq!(saved.layers().len(), 1); - } - - #[test] - fn photo_filter_psd_is_four_byte_aligned_and_round_trips() { - let color = [60000u16, 30000, 0].map(|x| x as f32 / 65535.0); - let adjustment = photocraft_doc::Adjustment::PhotoFilter { color, density: 0.14, preserve_luminosity: true }; - let mut doc = - Document::with_background("Photo Filter PSD", photocraft_geom::Size::new(32, 24), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE); - let mut layer = Layer::new("Warming Filter", LayerContent::Adjustment(adjustment.clone())); - doc.layers.push(layer.clone()); - - let assert_phfl = |file: &PsdFile| { - let records = &file.layer_info.as_ref().unwrap().layers; - let data = - records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *b"phfl").map(|b| b.data.as_slice()).expect("Photo Filter adjustment data"); - assert_eq!(data.len(), 20, "PSD layer block length includes four-byte padding"); - assert_eq!(&data[0..2], &[0, 2], "version 2 RGB Photo Filter"); - assert_eq!(&data[17..], &[0, 0, 0], "three padding bytes after the 17-byte payload"); - }; - - let exported = document_to_psd(&doc); - assert_phfl(&exported); - let encoded = exported.to_bytes().unwrap(); - let parsed = PsdFile::from_bytes(&encoded).unwrap(); - assert_phfl(&parsed); - let (restored, _) = crate::psd_import::psd_to_document(&parsed); - assert!(restored.layers.iter().any(|l| l.content == LayerContent::Adjustment(adjustment.clone()))); - - // Importing a legacy 18-byte block must still work; saving it again should repair it - // even when the adjustment values have not changed (normally raw blocks are reused). - let mut legacy = crate::adjust_map::write(&adjustment)[0].1.clone(); - legacy.truncate(18); - layer.psd_blocks = vec![(*b"phfl", std::sync::Arc::new(legacy))]; - doc.layers.pop(); - doc.layers.push(layer); - let repaired = document_to_psd(&doc); - assert_phfl(&repaired); - let repaired_bytes = repaired.to_bytes().unwrap(); - assert_phfl(&PsdFile::from_bytes(&repaired_bytes).unwrap()); - } - - #[test] - fn photo_filter_version_3_is_kept_until_edited() { - // The #3777 block (Photoshop CS2, XYZ colour): saved back byte-exact while unchanged. - let v3 = vec![0, 3, 0, 0, 0x5e, 0x7f, 0, 0, 0x61, 0xaa, 0, 0, 0x0e, 0xf3, 0, 0, 0, 0x3f, 1, 0]; - let adjustment = crate::adjust_map::parse(b"phfl", &v3, None, crate::adjust_map::Channels::Rgb); - let photocraft_doc::Adjustment::PhotoFilter { color, .. } = adjustment else { panic!("{adjustment:?}") }; - let mut doc = - Document::with_background("Photo Filter v3", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE); - let mut layer = Layer::new("Photo Filter 1", LayerContent::Adjustment(adjustment.clone())); - layer.psd_blocks = vec![(*b"phfl", std::sync::Arc::new(v3.clone()))]; - doc.layers.push(layer); - let phfl = |doc: &Document| { - let file = PsdFile::from_bytes(&document_to_psd(doc).to_bytes().unwrap()).unwrap(); - let records = &file.layer_info.as_ref().unwrap().layers; - records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *b"phfl").map(|b| b.data.clone()).expect("Photo Filter adjustment data") - }; - assert_eq!(phfl(&doc), v3); - // Once edited it is written as version 2 (RGB), and the colour survives. - let edited = photocraft_doc::Adjustment::PhotoFilter { color, density: 0.5, preserve_luminosity: true }; - if let Some(l) = doc.layers.last_mut() { - l.content = LayerContent::Adjustment(edited.clone()); - } - let saved = phfl(&doc); - assert_eq!(&saved[..2], &[0, 2]); - let reread = crate::adjust_map::parse(b"phfl", &saved, None, crate::adjust_map::Channels::Rgb); - let photocraft_doc::Adjustment::PhotoFilter { color: back, density, .. } = reread else { panic!("{reread:?}") }; - assert!(back.iter().zip(color).all(|(b, c)| (b - c).abs() <= 1.0 / 65535.0), "{back:?} vs {color:?}"); - assert_eq!(density, 0.5); - } - - #[test] - fn size_limit_boundary_keeps_limit_and_switches_above_it() { - assert!(!exceeds_psd_size_limit(Some(PSD_MAX_FILE_SIZE))); - assert!(exceeds_psd_size_limit(Some(PSD_MAX_FILE_SIZE + 1))); - assert!(exceeds_psd_size_limit(None)); - } - - #[test] - fn encoded_size_estimator_rejects_arithmetic_overflow() { - assert_eq!(estimate_encoded_size(u64::MAX, 0, 0), None); - assert_eq!(estimate_encoded_size(0, u64::MAX, 0), None); - assert_eq!(estimate_encoded_size(0, 0, u64::MAX), Some(u64::MAX)); - assert!(exceeds_psd_size_limit(estimate_psd_size(&document(u32::MAX, u32::MAX)))); - } - - #[test] - fn large_encoded_size_selects_psb_without_changing_explicit_choice() { - let large = document(30_000, 30_000); - assert!(estimate_psd_size(&large).is_some_and(|size| size > PSD_MAX_FILE_SIZE)); - let large_size_exceeds_limit = psd_size_exceeds_limit(&large); - assert_eq!(psd_version(&large, false, large_size_exceeds_limit), Version::Psb); - - let small = document(1, 1); - let small_size_exceeds_limit = psd_size_exceeds_limit(&small); - assert_eq!(psd_version(&small, false, small_size_exceeds_limit), Version::Psd); - assert_eq!(psd_version(&small, true, small_size_exceeds_limit), Version::Psb); - } - #[test] - fn kept_psb_blocks_are_written_as_8bim_in_a_psd() { - // Photoshop 27.11 refuses a PSD holding `8B64` blocks ("not compatible with this - // version"); they came from re-saving a PSB as PSD with its global FMsk and cinf kept. - let mut doc = document(4, 4); - doc.metadata.psd_global_blocks = vec![ - (*b"8B64", *b"FMsk", std::sync::Arc::new(vec![0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50])), - (*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), - ]; - let signatures = |force_psb| { - let opts = PsdExportOptions { force_psb, ..PsdExportOptions::default() }; - let bytes = document_to_psd_with(&doc, &opts).0.to_bytes().unwrap(); - let file = PsdFile::from_bytes(&bytes).unwrap(); - file.global_blocks.iter().map(|b| (b.signature, b.key)).collect::>() - }; - assert_eq!(signatures(false), vec![(*b"8BIM", *b"FMsk"), (*b"8BIM", *b"Patt")]); - assert_eq!(signatures(true), vec![(*b"8B64", *b"FMsk"), (*b"8BIM", *b"Patt")], "a PSB keeps 8B64"); - } - - /// A layered document with transparency in the merged image and an alpha channel: the name - /// and DisplayInfo resources hold one entry per extra channel, the merged transparency first, - /// as in every file Photoshop opens (one short, Photoshop 27.11 refused the file with "Could - /// not import the clipboard because an unexpected end-of-file was encountered"). The alpha - /// channel keeps its own name and options on the way back. - #[test] - fn alpha_names_count_the_merged_transparency() { - let mut doc = document(8, 6); - let mut layer = Layer::raster("Layer 1", doc.pixel_format()); - layer.surface_mut().unwrap().write_pixel(1, 1, &[1.0, 0.0, 0.0, 1.0]); - doc.layers.push(layer); - let mut a = photocraft_doc::AlphaChannel::new("Alpha 1", photocraft_raster::Surface::new(PixelFormat::GRAY8)); - a.opacity = 0.5; - doc.channels.push(a); - let file = document_to_psd(&doc); - let header_extra = usize::from(file.header.channels) - 3; - let names = |id| file.resources.iter().find(|r| r.id == id).map(|r| r.data.clone()).unwrap(); - let pascal = names(1006); - let (mut count, mut i) = (0, 0); - while i < pascal.len() { - count += 1; - i += 1 + usize::from(pascal[i]); - } - assert_eq!((header_extra, count), (2, 2), "transparency + Alpha 1"); - assert_eq!(names(crate::channel_map::DISPLAY_INFO).len(), 4 + 2 * 13); - let back = crate::import("a.psd", &file.to_bytes().unwrap()).unwrap().document; - assert_eq!(back.channels.len(), 1); - assert_eq!(back.channels[0].name, "Alpha 1"); - assert!((back.channels[0].opacity - 0.5).abs() < 1e-6); - } -} +mod tests; diff --git a/crates/io/src/psd_export/document.rs b/crates/io/src/psd_export/document.rs new file mode 100644 index 0000000000..f6c36c2d42 --- /dev/null +++ b/crates/io/src/psd_export/document.rs @@ -0,0 +1,463 @@ +use super::PSD_MAX_FILE_SIZE; +use super::estimate::{MERGED_RENDERED, MERGED_WEIGHT, fit_psd_size, psd_version, sign_global_blocks}; +use super::merged::{cmyk_of, merged_planes, placeholder_planes}; +use super::resources::{global_block, guides_resource, keep_imported_resource, legacy_name, pascal_names_resource, path_resources, unicode_names_resource}; +use super::smart::{SmartOut, link_group_resource}; +use super::{ + ColorMode, Compression, Document, Ex, GlobalLayerMask, Header, ImageData, ImageResource, Interrupt, LayerContent, LayerInfo, LayerInfoPlacement, + MaskParameters, PixelFormat, PsdExportOptions, PsdFile, PsdMode, ResolutionInfo, SampleType, TaggedBlock, deinterleave, ids, psd_background, psd_depth, + psd_mode, q255, version_info_resource, +}; + +/// Converts a document to a PSD file model (see [`document_to_psd_with`]). +pub fn document_to_psd(doc: &Document) -> PsdFile { + document_to_psd_with(doc, &PsdExportOptions::default()).0 +} + +/// Converts a document to a PSD/PSB file model, returning warnings about +/// anything that could not be represented. The merged composite is rendered +/// with `photocraft_compose::flatten`. +pub fn document_to_psd_with(doc: &Document, opts: &PsdExportOptions) -> (PsdFile, Vec) { + document_to_psd_sized(doc, opts, 0, &Interrupt::NONE, PSD_MAX_FILE_SIZE) +} + +/// [`document_to_psd_with`] for a background save (#3389): reports progress through `ctl` after +/// each layer record and each band of the merged composite. Once `ctl` is cancelled it stops +/// early and returns an **incomplete** model: callers check `ctl` and discard it. +pub(crate) fn document_to_psd_interruptible(doc: &Document, opts: &PsdExportOptions, ctl: &Interrupt) -> (PsdFile, Vec) { + document_to_psd_sized(doc, opts, 0, ctl, PSD_MAX_FILE_SIZE) +} + +/// [`document_to_psd_with`] for a document embedded `depth` smart objects deep. +pub(super) fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt) -> (PsdFile, Vec) { + document_to_psd_sized(doc, opts, depth, ctl, PSD_MAX_FILE_SIZE) +} + +/// Export with a caller-selected PSD size limit; the test suite lowers it to exercise promotion. +pub(super) fn document_to_psd_sized(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt, size_limit: u64) -> (PsdFile, Vec) { + let layerless_limit = opts.size_limit_psb.then_some(size_limit); + if doc.mode == ColorMode::Multichannel { + return crate::multichannel_map::document_to_psd(doc, opts.force_psb, layerless_limit); + } + if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb, layerless_limit) { + return file; + } + let fmt = doc.pixel_format(); + export_supported_document(doc, opts, depth, ctl, fmt, size_limit) +} + +fn export_supported_document( + doc: &Document, + opts: &PsdExportOptions, + depth: u32, + ctl: &Interrupt, + fmt: PixelFormat, + size_limit: u64, +) -> (PsdFile, Vec) { + // A Duotone is written over its grey plate with its inks: the block it was imported with, + // verbatim, while the inks are unedited, else one made from them (`duotone_map`). Photoshop + // has only 8-bit Duotone: it refuses to open a 16-bit one. + let duotone_inks: Option> = doc + .duotone + .as_ref() + .filter(|d| !d.inks.is_empty() && doc.mode == ColorMode::Duotone && fmt.mode == ColorMode::Grayscale && fmt.sample == SampleType::U8) + .map(|d| d.psd_raw.as_deref().map_or_else(|| crate::duotone_map::write(d), <[u8]>::to_vec)); + let sample = fmt.sample; + let cc = fmt.mode.color_channels(); + let big = doc.size.width > 30_000 || doc.size.height > 30_000; + let version = psd_version(doc, opts.force_psb); + let mut ex = Ex { + fmt, + dpi: doc.resolution_dpi, + document: opts.document_path.clone(), + read_linked_files: opts.read_linked_files, + text_styles: doc.text_styles.clone(), + text_index: Default::default(), + mask_fmt: PixelFormat::new(ColorMode::Grayscale, sample, false), + cc, + cmyk: fmt.mode == ColorMode::Cmyk, + version, + next_id: 0, + used_ids: Default::default(), + layer_ids: Default::default(), + canvas: doc.bounds(), + records: Vec::new(), + warnings: Vec::new(), + guides: doc.guides.clone(), + comps: (!crate::comps_map::comps_unchanged(doc)).then(|| (doc.layer_comps.clone(), doc.last_document_state.clone())), + smart: SmartOut::new(doc, depth), + patterns: doc.patterns.clone(), + background: psd_background(doc), + }; + add_format_warnings(doc, opts, fmt, big, duotone_inks.is_some(), &mut ex.warnings); + index_layer_ids_and_text(doc, &mut ex); + // Progress: one unit per layer record, then the merged composite (#3389). + let records = doc.walk().len() as f32; + let units = records + MERGED_WEIGHT; + let layers_share = records / units; + let mut done = 0.0f32; + let mut tick = || { + done += 1.0; + ctl.progress(done / units); + !ctl.cancelled() + }; + let space = photocraft_compose::cmyk_space(doc); + photocraft_color::convert::with_cmyk_space(space.as_ref(), || ex.emit(&doc.layers, &mut tick)); + drop(tick); + ctl.progress(layers_share); + + let merged = prepare_merged_planes(doc, &mut ex, opts, ctl, fmt, duotone_inks.is_some(), layers_share); + let MergedExport { header, image_data, extra, has_alpha, quick_mask_id, real_merged } = merged; + + // Resources. + let mut resources = vec![ + // hRes/vRes are always pixels per inch. The display unit written here is inches. + ImageResource::new(ids::RESOLUTION_INFO, ResolutionInfo::from_dpi(f64::from(doc.resolution_dpi)).to_bytes()), + ImageResource::new(ids::GLOBAL_ANGLE, (doc.global_light.angle.round() as i32).to_be_bytes().to_vec()), + ImageResource::new(ids::GLOBAL_ALTITUDE, (doc.global_light.altitude.round() as i32).to_be_bytes().to_vec()), + ]; + if let Some(icc) = &doc.icc_profile { + resources.push(ImageResource::new(ids::ICC_PROFILE, icc.to_vec())); + } + if !doc.guides.horizontal.is_empty() || !doc.guides.vertical.is_empty() { + resources.push(ImageResource::new(1032, guides_resource(doc))); + } + if !extra.is_empty() { + // One entry per extra channel, the merged transparency included (as in every file + // Photoshop opens; one short, Photoshop refuses the file with "unexpected end-of-file"). + let names: Vec<&str> = has_alpha.then_some("Transparency").into_iter().chain(extra.iter().map(|a| a.name.as_str())).collect(); + resources.push(ImageResource::new(1006, pascal_names_resource(&names))); + resources.push(ImageResource::new(1045, unicode_names_resource(&names))); + resources.push(ImageResource::new(crate::channel_map::DISPLAY_INFO, crate::channel_map::display_info(&extra, has_alpha))); + // Photoshop writes 1053 in every file with an extra channel. Its absence is how a file from + // an earlier PhotoCraft is told apart (`psd_import::earlier_photocraft_spot_planes`). + resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, crate::channel_map::alpha_identifiers(has_alpha, extra.len()))); + } + if let Some(id) = quick_mask_id { + let mut data = id.to_be_bytes().to_vec(); + data.push(0); + resources.push(ImageResource::new(crate::channel_map::QUICK_MASK_INFO, data)); + } + resources.extend(path_resources(doc, &mut ex.warnings)); + if let Some(groups) = link_group_resource(&doc.layers) { + resources.push(ImageResource::new(ids::LAYER_GROUP_INFO, groups)); + } + // The pixels are saved as they are shown: never let a reader rotate them again. The XMP and + // EXIF resolution follow the ResolutionInfo resource, so no copy contradicts it (#1691). + let ppi = Some((doc.resolution_dpi, doc.resolution_dpi)).filter(|d| d.0 > 0.0); + if let Some(x) = &doc.metadata.xmp { + resources.push(ImageResource::new(ids::XMP, photocraft_codecs::export_xmp(x, ppi).as_bytes().to_vec())); + } + if let Some(e) = &doc.metadata.exif { + resources.push(ImageResource::new(ids::EXIF, photocraft_codecs::export_exif(e, ppi).into_owned())); + } + let mut global_blocks = Vec::new(); + let preserved = ex.smart.finish(crate::annotations_map::export_blocks(doc, crate::pattern_map::export_global_blocks(doc))); + // The type layers' `Txt2` is regenerated (the auto-kern modes live there, and a preserved + // block goes stale as text is edited): one object per `TextIndex`, at the slot each layer's + // `TySh` names (unused slots stay empty; see `photocraft_text::psd::build_txt2`). The block + // it replaces is handed over so a kept object keeps everything the file held beyond its text + // and style runs (Photoshop's `/21 /1` glyph pen positions, …). + let text_layers: Vec<(i32, &photocraft_doc::TextLayer)> = doc + .walk() + .into_iter() + .filter_map(|(_, _, l)| match &l.content { + LayerContent::Text(t) => Some((ex.text_index.get(&l.id).copied().unwrap_or(0), t)), + _ => None, + }) + .collect(); + let previous = (!text_layers.is_empty()).then(|| preserved.iter().find(|(_, key, _)| key == b"Txt2").map(|(_, _, data)| data.to_vec())).flatten(); + // A fresh document of ordinary (Metrics) text has nothing to put in `Txt2`, and Photoshop never + // writes its empty `<< /0 << >> >>` stub — readers such as Photopea reject it (#3514). Keep the + // block when the file had one (round-trip its pen positions/extras) or a layer is Optical (the + // one mode EngineData can't carry; #1348). + let txt2 = (previous.is_some() || photocraft_text::psd::needs_txt2(&text_layers)) + .then(|| photocraft_text::psd::build_txt2_at(&text_layers, previous.as_deref(), ex.dpi)); + for (sig, key, data) in preserved { + // With no type layers left the opened file's `Txt2` describes text that is gone (fonts and + // layout caches, often most of the file): Photoshop drops it, and so does the save (#9563). + if key == *b"Txt2" && (txt2.is_some() || text_layers.is_empty()) { + continue; + } + // A kept layer block (one the import could not read, #2700) describes layers the + // document doesn't have. The document's own layers are written below, and a file holds + // one layer list: a second `Lr16`/`Lr32` would contradict it. + if !ex.records.is_empty() && matches!(&key, b"Lr16" | b"Lr32" | b"Layr") { + continue; + } + let block = global_block(sig, key, &data); + // A kept merged-transparency marker describes the opened file's composite. Over an opaque + // one it would make the first alpha channel read back as the transparency, and vanish. + if !has_alpha && block.is_merged_transparency_marker() { + continue; + } + global_blocks.push(block); + } + if let Some(txt2) = txt2 { + let mut tb = TaggedBlock::new(*b"Txt2", txt2.clone()); + tb.signature = *b"8BIM"; + tb.padding = Some(vec![0; (4 - txt2.len() % 4) % 4]); + global_blocks.push(tb); + } + let comps_resource = ex.comps.is_some().then(|| crate::comps_map::write_comps_resource(doc)); + let (slices_resource, slices_warning) = crate::slices_map::export_resource(doc, &ex.layer_ids); + if let Some(warning) = slices_warning { + ex.warnings.push(warning); + } + for (id, name, data) in &doc.metadata.psd_resources { + // Layer comps: the preserved list while unchanged, else regenerated (or dropped) below. + if *id == crate::comps_map::LAYER_COMPS && comps_resource.is_some() { + continue; + } + // Slices: the preserved resource while unchanged, else regenerated (or dropped) below. + if *id == crate::slices_map::SLICES && slices_resource.is_some() { + continue; + } + if !keep_imported_resource(doc, *id, data) { + continue; + } + // 1053 is written afresh with the extra channels. A kept copy (from a Multichannel file, or + // a document saved by an earlier version) would be a second one, or list channels that + // are gone. + if *id == crate::channel_map::ALPHA_IDENTIFIERS { + continue; + } + let mut r = ImageResource::new(*id, data.to_vec()); + r.name = legacy_name(name); + resources.push(r); + } + if let Some(data) = crate::annotations_map::fresh_scale_resource(doc) { + resources.push(ImageResource::new(crate::annotations_map::MEASUREMENT_SCALE, data)); + } + if let Some(Some(data)) = comps_resource { + resources.push(ImageResource::new(crate::comps_map::LAYER_COMPS, data)); + } + if let Some(Some(data)) = slices_resource { + resources.push(ImageResource::new(crate::slices_map::SLICES, data)); + } + resources.push(version_info_resource(real_merged)); + + let has_layers = !ex.records.is_empty(); + // Photoshop writes a merged-transparency marker (`Mtrn`, `Mt16`, `Mt32`; `MTrn` in a TIFF) + // before the layer block, and won't open a file whose `Lr32` comes first (#5068): the layer + // block goes right after the last kept marker, else first. + let index = global_blocks.iter().rposition(TaggedBlock::is_merged_transparency_marker).map_or(0, |i| i + 1); + let placement = match (has_layers, sample) { + (true, SampleType::U16) => LayerInfoPlacement::GlobalBlock { index, signature: *b"8BIM", key: *b"Lr16", padding: None }, + (true, SampleType::F32) => LayerInfoPlacement::GlobalBlock { index, signature: *b"8BIM", key: *b"Lr32", padding: None }, + _ => LayerInfoPlacement::Section, + }; + let records = std::mem::take(&mut ex.records); + let mut layer_info = has_layers.then_some(LayerInfo { merged_alpha: has_alpha, layers: records, padding: None }); + if let Some(info) = &mut layer_info { + // Photoshop pads the layer info to 4 bytes (an even pad misaligns readers after a global + // `Lr16`/`Lr32` block, #200). A length that cannot be computed keeps the even default. + // A global block's length is the exact data length and its padding to 4 follows the block: + // readers that check that length exactly (Unity's PSD Importer) reject it otherwise (#4654). + if matches!(placement, LayerInfoPlacement::GlobalBlock { .. }) { + info.padding = Some(Vec::new()); + } else if info.pad_to(version, 4).is_err() { + info.padding = None; + } + } + // 32-bit files carry Photoshop's HDR toning records as Color Mode Data; Photoshop will not + // open a 32-bit file without them (#291). + let color_mode_data = match duotone_inks { + Some(raw) => raw, + None => photocraft_psd::hdr::color_mode_data_for_depth(header.depth), + }; + let mut file = PsdFile { + header, + color_mode_data, + resources, + layer_info, + layer_info_placement: placement, + global_layer_mask: (has_layers || !global_blocks.is_empty()).then(GlobalLayerMask::default), + global_blocks, + layer_mask_trailing: Vec::new(), + image_data, + write_as_psb: false, + }; + if opts.size_limit_psb + && let Some(warning) = fit_psd_size(&mut file, size_limit) + { + ex.warnings.push(warning); + } + sign_global_blocks(&mut file); + (file, ex.warnings) +} + +fn add_format_warnings(doc: &Document, opts: &PsdExportOptions, fmt: PixelFormat, big: bool, has_duotone: bool, warnings: &mut Vec) { + if big && !opts.force_psb { + warnings.push("document exceeds 30000 px; written as PSB".into()); + } + if fmt.mode != doc.mode && !has_duotone { + warnings.push(format!("{:?} document written as {:?}", doc.mode, fmt.mode)); + } +} + +struct MergedExport<'a> { + header: Header, + image_data: ImageData, + extra: Vec<&'a photocraft_doc::AlphaChannel>, + has_alpha: bool, + quick_mask_id: Option, + real_merged: bool, +} + +fn prepare_merged_planes<'a>( + doc: &'a Document, + ex: &mut Ex, + opts: &PsdExportOptions, + ctl: &Interrupt, + fmt: PixelFormat, + duotone: bool, + layers_share: f32, +) -> MergedExport<'a> { + let sample = fmt.sample; + let cc = fmt.mode.color_channels(); + let real_merged = opts.maximize_compatibility || ex.records.is_empty() || duotone; + let (mut planes, has_alpha) = merged_image(doc, opts, ctl, &fmt, real_merged, layers_share); + if !has_alpha { + planes.truncate(cc * doc.size.area() as usize * sample.bytes()); + } + let (extra, quick_mask_id) = append_extra_channels(doc, ex, &mut planes, has_alpha, sample); + let channels = (cc + usize::from(has_alpha) + extra.len()) as u16; + let mode = if duotone { PsdMode::Duotone } else { psd_mode(fmt.mode) }; + let header = Header::new(ex.version, doc.size.width, doc.size.height, channels, psd_depth(sample), mode); + let compression = if sample == SampleType::F32 { Compression::Raw } else { opts.merged_compression }; + let image_data = ImageData::encode(compression, &planes, &header).or_else(|_| ImageData::encode(Compression::Raw, &planes, &header)).unwrap_or(ImageData { + compression: Compression::Raw, + data: planes, + stored: None, + }); + ctl.progress(1.0); + MergedExport { header, image_data, extra, has_alpha, quick_mask_id, real_merged } +} + +fn append_extra_channels<'a>( + doc: &'a Document, + ex: &mut Ex, + planes: &mut Vec, + has_alpha: bool, + sample: SampleType, +) -> (Vec<&'a photocraft_doc::AlphaChannel>, Option) { + let cc = ex.cc; + let max_extra = 56 - cc - usize::from(has_alpha); + if doc.channels.len() > max_extra { + ex.warnings.push(format!("only {max_extra} alpha channels fit in PSD; the rest were dropped")); + } + let mut extra: Vec<_> = doc.channels.iter().take(max_extra).collect(); + let quick_mask_id = match &doc.quick_mask { + Some(q) if extra.len() < max_extra => { + extra.push(q); + Some((cc + usize::from(has_alpha) + extra.len() - 1) as u16) + } + Some(_) => { + ex.warnings.push("Quick Mask was dropped because the PSD channel limit was reached".into()); + None + } + None => None, + }; + let canvas = doc.bounds(); + for channel in &extra { + let surface = if channel.surface.format() != ex.mask_fmt { channel.surface.convert(ex.mask_fmt) } else { channel.surface.clone() }; + let mut bytes = surface.to_interleaved(canvas); + if channel.spot.is_some() { + crate::channel_map::photoshop_spot_plane(&mut bytes, sample); + } + planes.extend(deinterleave(&bytes, 1, sample, &[false]).remove(0)); + } + (extra, quick_mask_id) +} + +fn merged_image(doc: &Document, opts: &PsdExportOptions, ctl: &Interrupt, fmt: &PixelFormat, real_merged: bool, layers_share: f32) -> (Vec, bool) { + if !real_merged { + return (placeholder_planes(doc, fmt), false); + } + // Merged composite, rendered and encoded in bands (no full-size float composite). Matting + // against white only changes pixels with alpha < 1; if some are slightly translucent but + // all round to opaque (so no alpha channel is written), encode once more without the matte. + let merged_end = layers_share + (1.0 - layers_share) * MERGED_RENDERED; + let (mut planes, has_alpha, translucent) = + crate::with_span(ctl, layers_share, merged_end, |ctl| merged_planes(doc, fmt, cmyk_of(fmt), opts.merged_matte, ctl)); + if !has_alpha && translucent && opts.merged_matte { + planes = merged_planes(doc, fmt, cmyk_of(fmt), false, &Interrupt::cancel_only(&|| ctl.cancelled())).0; + } + (planes, has_alpha) +} + +fn index_layer_ids_and_text(doc: &Document, ex: &mut Ex) { + // Assign layer ids up front (stable across round trips): keep unique + // `psd_id`s, number the rest after them; dividers come after all. + { + let walk = doc.walk(); + let mut used = std::collections::HashSet::new(); + for (_, _, l) in &walk { + if let Some(id) = l.psd_id + && used.insert(id) + { + ex.layer_ids.insert(l.id, id); + } + } + let mut next = 1u32; + for (_, _, l) in &walk { + if let std::collections::hash_map::Entry::Vacant(e) = ex.layer_ids.entry(l.id) { + while used.contains(&next) { + next += 1; + } + used.insert(next); + e.insert(next); + } + } + ex.next_id = used.iter().copied().max().unwrap_or(0); + ex.used_ids = used; + // Text objects' number in the generated `Txt2` (#1348): a preserved `TySh` keeps the + // number it already names — its `Txt2` object must sit at that slot for `apply_txt2` — + // and new layers get numbers past every kept one and every object in the file's `Txt2`. + // Kept numbers are claimed first: a layer added below others must not take a number a + // layer above it keeps, or each of those would be renumbered onto its neighbour's object + // and take its style (#7773); nor may it land on a deleted layer's object. + let mut used_txt = std::collections::HashSet::new(); + ex.text_index = Default::default(); + let mut fresh = Vec::new(); + for (_, _, l) in &walk { + let LayerContent::Text(t) = &l.content else { + continue; + }; + match t.psd_raw.as_deref().and_then(|d| photocraft_text::psd::text_index(d)) { + Some(i) if used_txt.insert(i) => { + ex.text_index.insert(l.id, i); + } + _ => fresh.push(l.id), + } + } + let objects = + doc.metadata.psd_global_blocks.iter().find(|(_, key, _)| key == b"Txt2").map_or(0, |(_, _, d)| photocraft_text::psd::txt2_object_count(d)); + let mut next_txt = used_txt.iter().max().map_or(0, |m| m + 1).max(i32::try_from(objects).unwrap_or(i32::MAX)); + if next_txt.saturating_add(fresh.len() as i32) > photocraft_text::psd::MAX_TEXT_INDEX + 1 { + next_txt = 0; + } + for id in fresh { + while used_txt.contains(&next_txt) { + next_txt += 1; + } + used_txt.insert(next_txt); + ex.text_index.insert(id, next_txt); + } + } +} + +/// PSD mask parameters for a user mask's and a vector mask's (density, feather); `None` when +/// every value is the default (density 1, feather 0). +pub(super) fn mask_parameters(user: Option<(f32, f32)>, vector: Option<(f32, f32)>) -> Option { + let density = |v: Option<(f32, f32)>| v.and_then(|(d, _)| (d < 1.0).then(|| q255(d))); + let feather = |v: Option<(f32, f32)>| v.and_then(|(_, f)| (f.is_finite() && f > 0.0).then_some(f64::from(f))); + let p = + MaskParameters { flags: 0, user_density: density(user), user_feather: feather(user), vector_density: density(vector), vector_feather: feather(vector) }; + let flags = u8::from(p.user_density.is_some()) + | u8::from(p.user_feather.is_some()) << 1 + | u8::from(p.vector_density.is_some()) << 2 + | u8::from(p.vector_feather.is_some()) << 3; + (flags != 0).then_some(MaskParameters { flags, ..p }) +} diff --git a/crates/io/src/psd_export/estimate.rs b/crates/io/src/psd_export/estimate.rs new file mode 100644 index 0000000000..bb2d9b1c59 --- /dev/null +++ b/crates/io/src/psd_export/estimate.rs @@ -0,0 +1,63 @@ +use super::{Document, PSD_SAVE_HEADROOM}; +use photocraft_psd::{PsdFile, Version, file::LayerInfoPlacement}; + +/// The signature a kept tagged block is written with. `8B64` exists only in PSB files: a PSD +/// with an `8B64` block (kept from a PSB source, e.g. its global `FMsk` or `cinf`) is refused by +/// Photoshop as "not compatible with this version" (measured in Photoshop 27.11). The other way +/// round, a PSB block with an 8-byte length is `8B64`, as Photoshop writes it: an `8BIM` `lnk2`, +/// `pths` or `cinf` (kept from a PSD source, whose lengths the PSB widens) makes Photoshop read +/// the length as 4 bytes and refuse the whole file (#7937). +pub(crate) fn block_signature(sig: [u8; 4], key: [u8; 4], version: Version) -> [u8; 4] { + match version { + Version::Psd if sig == *b"8B64" => *b"8BIM", + Version::Psb if sig == *b"8BIM" && photocraft_psd::tagged::uses_long_length(version, &key) => *b"8B64", + _ => sig, + } +} + +/// How much of a PSD conversion the merged composite weighs, in layer records. +pub(super) const MERGED_WEIGHT: f32 = 2.0; + +/// The part of the merged composite's share filled once its bands are rendered and converted. +pub(super) const MERGED_RENDERED: f32 = 0.8; + +/// The version a document is encoded as: PSB when asked for or past PSD's 30000 px; otherwise +/// PSD, which [`fit_psd_size`] turns into PSB once the encoded file proves too large. +pub(crate) fn psd_version(doc: &Document, force_psb: bool) -> Version { + let exceeds_dimensions = doc.size.width > 30_000 || doc.size.height > 30_000; + if force_psb || exceeds_dimensions { Version::Psb } else { Version::Psd } +} + +/// Promote files that exceed the PSD limit, accounting for the preview appended by the save +/// (#212, #5285). The decision is made on the exact length of the file as it will be written, +/// not on the raw pixel size: RLE usually packs the channels far below it. +pub(crate) fn fit_psd_size(file: &mut PsdFile, limit: u64) -> Option { + if file.written_version() == Version::Psb { + return None; + } + if file.encoded_len().is_ok_and(|len| len.saturating_add(PSD_SAVE_HEADROOM) <= limit) { + return None; + } + match file.promote_to_psb() { + Ok(()) => { + let padded = file.layer_info.as_ref().is_some_and(|info| info.padding.is_some()); + if matches!(file.layer_info_placement, LayerInfoPlacement::Section) + && padded + && file.pad_layer_info_to(4).is_err() + && let Some(info) = &mut file.layer_info + { + info.padding = None; + } + Some("encoded size exceeds 2 GB; written as PSB".into()) + } + Err(error) => Some(format!("encoded size exceeds 2 GB, but the file could not be written as PSB: {error}")), + } +} + +/// Signs every global block for the version the file is written as. +pub(crate) fn sign_global_blocks(file: &mut PsdFile) { + let version = file.written_version(); + for block in &mut file.global_blocks { + block.signature = block_signature(block.signature, block.key, version); + } +} diff --git a/crates/io/src/psd_export/merged.rs b/crates/io/src/psd_export/merged.rs new file mode 100644 index 0000000000..94b7bc4014 --- /dev/null +++ b/crates/io/src/psd_export/merged.rs @@ -0,0 +1,273 @@ +use super::{ColorMode, Document, Interrupt, PixelFormat, SampleType, encode_be, q255}; + +pub(super) fn cmyk_of(fmt: &PixelFormat) -> bool { + fmt.mode == ColorMode::Cmyk +} + +/// The merged image's planes (colour channels, then alpha), big-endian, matted against white +/// when `matte`; with whether any pixel needs the alpha channel and whether any is below 1. +/// `ctl` gets the fraction of rows done after each band; once it is cancelled the rest of the +/// image is left blank (the caller discards it). +pub(super) fn merged_planes(doc: &Document, fmt: &PixelFormat, cmyk: bool, matte: bool, ctl: &Interrupt) -> (Vec, bool, bool) { + if cmyk && let Some(planes) = merged_ink_planes(doc, fmt.sample, matte, ctl) { + return planes; + } + let sample = fmt.sample; + let cc = fmt.mode.color_channels(); + let bps = sample.bytes(); + let n = doc.size.area() as usize; + let plane = n * bps; + let mut planes = vec![0u8; plane * (cc + 1)]; + let (mut has_alpha, mut translucent) = (false, false); + let white = photocraft_raster::from_rgba(fmt, [1.0, 1.0, 1.0, 1.0]); + let canvas = doc.bounds(); + let w = canvas.width() as usize; + let space = photocraft_compose::cmyk_space(doc); + let lab16 = fmt.mode == ColorMode::Lab && sample == SampleType::U16; + let rows = canvas.height().max(1) as f32; + let _ = photocraft_compose::render_bands(doc, canvas, 0, |band| -> Result<(), ()> { + if ctl.cancelled() { + return Err(()); + } + let start = (band.rect.y0 - canvas.y0) as usize * w; + let ranges = crate::pixels::bands(band.px.len()); + let mut slots: Vec> = ranges.iter().map(|_| Vec::with_capacity(cc + 1)).collect(); + for chunk in planes.chunks_mut(plane.max(1)).take(cc + 1) { + let Some(mut rest) = chunk.get_mut(start * bps..(start + band.px.len()) * bps) else { continue }; + for (slot, range) in slots.iter_mut().zip(&ranges) { + let Some((part, tail)) = rest.split_at_mut_checked(range.len() * bps) else { break }; + slot.push(part); + rest = tail; + } + } + // Converted and encoded on all cores. The composite came through the document's CMYK + // profile: convert back through it too. + let flags = crate::pixels::par_map(ranges.into_iter().zip(slots).collect(), |(range, mut out)| { + let px = band.px.get(range).unwrap_or(&[]); + let k = Encode { fmt, cc, matte, cmyk, lab16, white: &white }; + photocraft_color::convert::with_cmyk_space(space.as_ref(), || match sample { + SampleType::U8 => k.run( + px, + &mut out, + |a| q255(a) < 255, + |x, d, i| { + if let Some(b) = d.get_mut(i) { + *b = (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + }, + ), + SampleType::U16 => k.run( + px, + &mut out, + |a| (a.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 != u16::MAX, + |x, d, i| { + if let Some(b) = d.get_mut(i * 2..i * 2 + 2) { + b.copy_from_slice(&((x.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16).to_be_bytes()); + } + }, + ), + SampleType::F32 => k.run( + px, + &mut out, + |a| a != 1.0, + |x, d, i| { + if let Some(b) = d.get_mut(i * 4..i * 4 + 4) { + b.copy_from_slice(&x.to_be_bytes()); + } + }, + ), + }) + }); + for (a, t) in flags { + has_alpha |= a; + translucent |= t; + } + ctl.progress((band.rect.y1 - canvas.y0) as f32 / rows); + Ok(()) + }); + (planes, has_alpha, translucent) +} + +struct Encode<'a> { + fmt: &'a PixelFormat, + cc: usize, + matte: bool, + cmyk: bool, + lab16: bool, + white: &'a [f32], +} + +impl Encode<'_> { + fn run(&self, px: &[[f32; 4]], out: &mut [&mut [u8]], stored_translucent: impl Fn(f32) -> bool, put: impl Fn(f32, &mut [u8], usize)) -> (bool, bool) { + let Self { fmt, cc, matte, cmyk, lab16, white } = *self; + let (mut has_alpha, mut translucent) = (false, false); + let mut v = [0.0f32; 5]; + for (i, p) in px.iter().enumerate() { + // Retain alpha whenever it differs from opaque at the stored precision. + has_alpha |= stored_translucent(p[3]); + translucent |= p[3] < 1.0; + photocraft_raster::from_rgba_into(fmt, *p, &mut v); + for (c, dst) in out.iter_mut().enumerate() { + // Matte against white like Photoshop (see `pixels::matte`). + let m = if c < cc && matte { crate::pixels::matte(v[c], v[cc], white.get(c).copied().unwrap_or(1.0)) } else { v[c] }; + let x = if cmyk && c < cc { 1.0 - m } else { m }; + // 16-bit Lab a*/b* use Photoshop's 0..65280 scale (`pixels::lab16_chroma`). + let x = if lab16 && (c == 1 || c == 2) { x * (crate::pixels::LAB16_CHROMA_MAX / 65535.0) } else { x }; + put(x, dst, i); + } + } + (has_alpha, translucent) + } +} + +/// [`merged_planes`] of a CMYK document whose layers are mixed on their ink values +/// (`photocraft_compose::cmyk`), so opaque pixels keep their exact inks; `None` for documents +/// that composite in RGB. Reports progress and stops on cancel like [`merged_planes`]. +fn merged_ink_planes(doc: &Document, sample: SampleType, matte: bool, ctl: &Interrupt) -> Option<(Vec, bool, bool)> { + let bps = sample.bytes(); + let n = doc.size.area() as usize; + let plane = n * bps; + let mut planes = vec![0u8; plane * 5]; + let (mut has_alpha, mut translucent) = (false, false); + let canvas = doc.bounds(); + let w = canvas.width() as usize; + let rows = canvas.height().max(1) as f32; + // A cancelled export leaves the rest blank (the caller discards it), so the result stands either way. + let _ = photocraft_compose::cmyk::render_cmyk_bands(doc, canvas, 0, |band| -> Result<(), ()> { + if ctl.cancelled() { + return Err(()); + } + // Retain alpha whenever it differs from opaque at the stored precision. + has_alpha |= band.px.iter().any(|p| match sample { + SampleType::U8 => q255(p[4]) < 255, + SampleType::U16 => (p[4].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 != u16::MAX, + SampleType::F32 => p[4] != 1.0, + }); + translucent |= band.px.iter().any(|p| p[4] < 1.0); + let start = (band.rect.y0 - canvas.y0) as usize * w; + let parts = crate::pixels::par_map(crate::pixels::bands(band.px.len()), |range| { + let mut out: Vec> = (0..5).map(|_| Vec::with_capacity(range.len() * bps)).collect(); + for p in band.px.get(range.clone()).unwrap_or(&[]) { + for (c, o) in out.iter_mut().enumerate() { + // Inks matted against white paper (no ink) like Photoshop (see + // `pixels::matte`), and stored inverted as PSD's CMYK is. + let v = if c < 4 { 1.0 - if matte { crate::pixels::matte(p[c], p[4], 0.0) } else { p[c] } } else { p[4] }; + encode_be(v, sample, o); + } + } + (range.start, out) + }); + for (at, part) in parts { + for (c, src) in part.iter().enumerate() { + let o = c * plane + (start + at) * bps; + if let Some(dst) = planes.get_mut(o..o + src.len()) { + dst.copy_from_slice(src); + } + } + } + ctl.progress((band.rect.y1 - canvas.y0) as f32 / rows); + Ok(()) + })?; + Some((planes, has_alpha, translucent)) +} + +/// The merged image of a file saved without Maximize Compatibility: Photoshop's white placeholder +/// in the document's colour channels, with no alpha. Encoded like [`merged_planes`], so CMYK white +/// is no ink and Lab white is L 100 with neutral a*/b*. +pub(super) fn placeholder_planes(doc: &Document, fmt: &PixelFormat) -> Vec { + let cc = fmt.mode.color_channels(); + let cmyk = cmyk_of(fmt); + let lab16 = fmt.mode == ColorMode::Lab && fmt.sample == SampleType::U16; + let space = photocraft_compose::cmyk_space(doc); + let white = photocraft_color::convert::with_cmyk_space(space.as_ref(), || photocraft_raster::from_rgba(fmt, [1.0, 1.0, 1.0, 1.0])); + let n = doc.size.area() as usize; + let mut planes = Vec::with_capacity(n * fmt.sample.bytes() * cc); + for c in 0..cc { + let v = white.get(c).copied().unwrap_or(1.0); + let x = if cmyk { 1.0 - v } else { v }; + let x = if lab16 && (c == 1 || c == 2) { x * (crate::pixels::LAB16_CHROMA_MAX / 65535.0) } else { x }; + let mut sample = Vec::with_capacity(4); + encode_be(x, fmt.sample, &mut sample); + planes.extend(sample.iter().copied().cycle().take(n * sample.len())); + } + planes +} + +#[cfg(test)] +mod tests { + use super::*; + use photocraft_doc::{Layer, LayerContent}; + use photocraft_raster::Surface; + + fn previous_merged_planes(doc: &Document, fmt: &PixelFormat, cmyk: bool, matte: bool) -> (Vec, bool, bool) { + if cmyk && let Some(planes) = merged_ink_planes(doc, fmt.sample, matte, &Interrupt::NONE) { + return planes; + } + let sample = fmt.sample; + let cc = fmt.mode.color_channels(); + let bps = sample.bytes(); + let plane = doc.size.area() as usize * bps; + let mut planes = vec![0u8; plane * (cc + 1)]; + let (mut has_alpha, mut translucent) = (false, false); + let white = photocraft_raster::from_rgba(fmt, [1.0, 1.0, 1.0, 1.0]); + let canvas = doc.bounds(); + let w = canvas.width() as usize; + let space = photocraft_compose::cmyk_space(doc); + let lab16 = fmt.mode == ColorMode::Lab && sample == SampleType::U16; + let _ = photocraft_compose::render_bands(doc, canvas, 0, |band| -> Result<(), ()> { + has_alpha |= band.px.iter().any(|p| match sample { + SampleType::U8 => q255(p[3]) < 255, + SampleType::U16 => (p[3].clamp(0.0, 1.0) * 65535.0 + 0.5) as u16 != u16::MAX, + SampleType::F32 => p[3] != 1.0, + }); + translucent |= band.px.iter().any(|p| p[3] < 1.0); + let start = (band.rect.y0 - canvas.y0) as usize * w; + photocraft_color::convert::with_cmyk_space(space.as_ref(), || { + let mut v = [0.0f32; 5]; + for (i, p) in band.px.iter().enumerate() { + photocraft_raster::from_rgba_into(fmt, *p, &mut v); + for c in 0..=cc { + let m = if c < cc && matte { crate::pixels::matte(v[c], v[cc], white[c]) } else { v[c] }; + let x = if cmyk && c < cc { 1.0 - m } else { m }; + let x = if lab16 && (c == 1 || c == 2) { x * (crate::pixels::LAB16_CHROMA_MAX / 65535.0) } else { x }; + let mut b = Vec::new(); + encode_be(x, sample, &mut b); + let o = c * plane + (start + i) * bps; + planes[o..o + bps].copy_from_slice(&b); + } + } + }); + Ok(()) + }); + (planes, has_alpha, translucent) + } + + #[test] + fn merged_planes_match_the_per_band_reference() { + let size = photocraft_geom::Size::new(37, 23); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Lab, ColorMode::Cmyk] { + for sample in SampleType::ALL { + for alpha_floor in [0.0f32, 0.999_9, 1.0] { + let mut doc = Document::new("merged", size, mode, sample); + let fmt = doc.pixel_format(); + let mut s = Surface::new(fmt); + for y in 0..23 { + for x in 0..37 { + let a = (alpha_floor + (1.0 - alpha_floor) * ((x * 7 + y * 3) % 11) as f32 / 10.0).min(1.0); + let rgba = [x as f32 / 36.0, y as f32 / 22.0, ((x + y) % 5) as f32 / 4.0, a]; + let mut v = [0.0f32; 5]; + let n = photocraft_raster::from_rgba_into(&fmt, rgba, &mut v); + s.write_pixel(x, y, &v[..n]); + } + } + doc.layers.push(Layer::new("pixels", LayerContent::Raster(s))); + for matte in [false, true] { + let got = merged_planes(&doc, &fmt, cmyk_of(&fmt), matte, &Interrupt::NONE); + let want = previous_merged_planes(&doc, &fmt, cmyk_of(&fmt), matte); + assert_eq!(got, want, "{mode:?} {sample:?} alpha >= {alpha_floor} matte {matte}"); + } + } + } + } + } +} diff --git a/crates/io/src/psd_export/resources.rs b/crates/io/src/psd_export/resources.rs new file mode 100644 index 0000000000..09d9e67d22 --- /dev/null +++ b/crates/io/src/psd_export/resources.rs @@ -0,0 +1,116 @@ +use super::{Document, ImageResource, TaggedBlock}; + +pub(crate) fn legacy_name(s: &str) -> Vec { + s.chars().map(|c| if c.is_ascii() && !c.is_ascii_control() { c as u8 } else { b'?' }).take(255).collect() +} + +pub(crate) fn unicode_names_resource(names: &[&str]) -> Vec { + let mut v = Vec::new(); + for n in names { + let units: Vec = n.encode_utf16().chain(std::iter::once(0)).collect(); + v.extend_from_slice(&(units.len() as u32).to_be_bytes()); + for u in units { + v.extend_from_slice(&u.to_be_bytes()); + } + } + v +} + +pub(crate) fn pascal_names_resource(names: &[&str]) -> Vec { + let mut v = Vec::new(); + for n in names { + let b = legacy_name(n); + let n = b.len().min(255); + v.push(n as u8); + v.extend_from_slice(&b[..n]); + } + // No per-string pad: Photoshop's resource 1006 for "Spot Color 1" is 13 bytes + // (length byte + 12 characters). The 8BIM container pads an odd resource separately. + v +} + +pub(crate) fn guides_resource(doc: &Document) -> Vec { + let mut v = Vec::new(); + v.extend_from_slice(&1u32.to_be_bytes()); + v.extend_from_slice(&576u32.to_be_bytes()); + v.extend_from_slice(&576u32.to_be_bytes()); + let n = doc.guides.horizontal.len() + doc.guides.vertical.len(); + v.extend_from_slice(&(n as u32).to_be_bytes()); + for (list, dir) in [(&doc.guides.vertical, 0u8), (&doc.guides.horizontal, 1u8)] { + for p in list { + v.extend_from_slice(&((p * 32.0).round() as i32).to_be_bytes()); + v.push(dir); + } + } + v +} + +/// Path resources: saved paths (2000+), the work path (1025) and the clipping path name (2999), +/// the last only when the imported one doesn't already name the document's clipping path. +pub(crate) fn path_resources(doc: &Document, warnings: &mut Vec) -> Vec { + use crate::vector_map::{CLIPPING_PATH, WORK_PATH, path_from_resource, path_to_records}; + let mut out = Vec::new(); + let (w, h) = (doc.size.width, doc.size.height); + let max = (*crate::vector_map::SAVED_PATHS.end() - *crate::vector_map::SAVED_PATHS.start() + 1) as usize; + if doc.paths.len() > max { + warnings.push(format!("only {max} saved paths fit in PSD; the rest were dropped")); + } + for (i, p) in doc.paths.iter().take(max).enumerate() { + let data = match &p.psd_raw { + Some(r) if path_from_resource(r, w, h).as_ref() == Some(&p.path) => r.to_vec(), + _ => path_to_records(&p.path, w, h).to_bytes(), + }; + let mut r = ImageResource::new(crate::vector_map::SAVED_PATHS.start() + i as u16, data); + r.name = legacy_name(&p.name); + out.push(r); + } + if let Some(wp) = &doc.work_path { + out.push(ImageResource::new(WORK_PATH, path_to_records(wp, w, h).to_bytes())); + } + let raw_clip = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == CLIPPING_PATH); + let raw_name = raw_clip.map(|(_, _, d)| { + let n = usize::from(d.first().copied().unwrap_or(0)); + d.get(1..1 + n).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default() + }); + if let Some(c) = &doc.clipping_path + && raw_name.as_deref() != Some(c.name.as_str()) + { + // Pascal name, then flatness as 16.16 fixed (best effort; see vector_map docs). + let mut data = vec![0u8]; + let name = legacy_name(&c.name); + data[0] = name.len() as u8; + data.extend_from_slice(&name); + data.extend_from_slice(&((c.flatness.max(0.0) * 65536.0) as u32).to_be_bytes()); + out.push(ImageResource::new(CLIPPING_PATH, data)); + } + out +} + +/// Whether an imported (unmodelled) resource still describes the document and is written back. +pub(crate) fn keep_imported_resource(doc: &Document, id: u16, data: &[u8]) -> bool { + // Measurement scale / count resources: only while they still describe the document. + if !crate::annotations_map::keep_resource(doc, id) { + return false; + } + // Guides v2 and guide GUIDs: only while they still describe the document's guides. + if !crate::guides_map::keep_resource(doc, id, data) { + return false; + } + // A preserved clipping-path resource is only valid while it names the current one. + if id == crate::vector_map::CLIPPING_PATH { + let n = usize::from(data.first().copied().unwrap_or(0)); + let raw_name = data.get(1..1 + n).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default(); + return doc.clipping_path.as_ref().is_some_and(|c| c.name == raw_name); + } + true +} + +/// A document-level (global) tagged block. Photoshop pads these to a multiple of 4, and readers +/// such as psd-tools step to the next block that way: an even pad after `CAI ` (77 bytes) +/// misaligned every block after it (#200). +pub(crate) fn global_block(signature: [u8; 4], key: [u8; 4], data: &[u8]) -> TaggedBlock { + let mut tb = TaggedBlock::new(key, data.to_vec()); + tb.signature = signature; + tb.padding = Some(vec![0; (4 - data.len() % 4) % 4]); + tb +} diff --git a/crates/io/src/psd_export/smart.rs b/crates/io/src/psd_export/smart.rs new file mode 100644 index 0000000000..10833c7033 --- /dev/null +++ b/crates/io/src/psd_export/smart.rs @@ -0,0 +1,253 @@ +use super::{Document, Layer, LayerContent, SmartContentsId, Surface}; + +/// An embedded smart-object source as written to `lnk2`. +#[derive(Clone, Debug)] +pub(super) struct Source { + pub(super) uuid: String, + /// Pixel size of the source document. + pub(super) size: (f64, f64), + pub(super) dpi: f64, +} + +pub(super) fn contents_uuid(contents_id: SmartContentsId, bytes: &[u8]) -> String { + let mut identity = blake3::Hasher::new(); + identity.update(&contents_id.0.to_le_bytes()); + identity.update(bytes); + crate::smart_map::uuid_from(identity.finalize().as_bytes()) +} + +/// Smart-object state collected while emitting layers, written into the global blocks at the end. +#[derive(Default)] +pub(super) struct SmartOut { + /// Nesting depth of this export (embedded documents are exported recursively). + pub(super) depth: u32, + /// The document's preserved global blocks (embedded files and filter caches from PSD import). + pub(super) globals: Vec, + /// uuids of the files in the preserved linked-layer blocks. + pub(super) known: std::collections::HashSet, + /// uuids the layers reference. + pub(super) used: std::collections::HashSet, + /// Unreferenced preserved files may be dropped (every smart layer's file id is known). + pub(super) prune_ok: bool, + /// Files to embed. + pub(super) add: Vec, + /// Sources converted so far, by contents identity, bytes and whether they were read from a + /// link (`Err` = can't be embedded). + pub(super) converted: std::collections::HashMap<(SmartContentsId, [u8; 32], bool), Result>, + /// Each placed layer has its own id, even when it duplicates an imported layer. + pub(super) placed_used: std::collections::HashSet, + /// Original instances reserve their imported placement regardless of layer order. + pub(super) placed_owners: std::collections::HashMap, + /// Legacy records without a placed id stay unchanged for the original instance. + pub(super) unplaced_owners: std::collections::HashMap, + /// The imported identity that keeps each original linked UUID. + pub(super) linked_contents: std::collections::HashMap, + /// Independent copies of preserved linked items, including unavailable external sources. + pub(super) linked_copies: std::collections::BTreeMap<(String, SmartContentsId), photocraft_doc::PsdGlobalBlock>, + /// Embedded documents decoded while converting them, by uuid (for the filter caches). + pub(super) docs: std::collections::HashMap, + /// Source composites (document pixel format, source space) by uuid; `None` = unreadable. + pub(super) composites: std::collections::HashMap, photocraft_geom::Rect)>>, + /// Preserved `FEid` items (by placed id). + pub(super) old_fx: Vec, + /// Placed ids whose preserved `FEid` item stays. + pub(super) keep_fx: std::collections::HashSet, + /// Regenerated `FEid` items. + pub(super) new_fx: Vec, + /// The unfiltered pixels of each smart object whose filter mask shows: its layer record holds + /// the filtered pixels without the mask, as Photoshop's do (`compose::filter_mask`). + pub(super) unfiltered: std::collections::HashMap, +} + +/// Whether a smart object's filter mask shows: enabled over at least one visible, enabled filter. +pub(super) fn masked(sm: &photocraft_doc::SmartObject) -> bool { + sm.filter_mask.as_ref().is_some_and(|m| m.enabled) && sm.filters_enabled && sm.smart_filters.iter().any(|f| f.visible) +} + +/// `FEid` data padded to 4 bytes inside the block length: Photoshop counts the padding in the +/// length and can't open a file whose `FEid` is padded outside it. +fn padded_fx(fx: &photocraft_psd::filter_effects::FilterEffects) -> Vec { + let mut d = fx.to_bytes(); + d.resize(d.len().next_multiple_of(4), 0); + d +} + +pub(super) const LINK_KEYS: [&[u8; 4]; 4] = [b"lnk2", b"lnk3", b"lnkD", b"lnkE"]; + +const FX_KEYS: [&[u8; 4]; 2] = [b"FEid", b"FXid"]; + +impl SmartOut { + pub(super) fn new(doc: &Document, depth: u32) -> Self { + let globals = doc.metadata.psd_global_blocks.clone(); + let known = globals.iter().filter(|(_, k, _)| LINK_KEYS.contains(&k)).flat_map(|(_, _, d)| crate::linked::block_uuids(d)).collect(); + let old_fx = globals + .iter() + .filter(|(_, k, _)| FX_KEYS.contains(&k)) + .filter_map(|(_, _, d)| photocraft_psd::filter_effects::FilterEffects::parse(d).ok()) + .flat_map(|fx| fx.items) + .collect(); + let mut linked_contents = std::collections::HashMap::new(); + let mut placed_owners = std::collections::HashMap::new(); + let mut unplaced_owners = std::collections::HashMap::new(); + let mut layers = doc.walk(); + // Duplicating a layer clears psd_id and gives it a newer runtime id. + layers.sort_by_key(|(_, _, layer)| (layer.psd_id.is_none(), layer.id)); + for (_, _, layer) in layers { + if let LayerContent::Smart(sm) = &layer.content + && let photocraft_doc::SmartSource::Linked { path } = &sm.source + && let Some(placed) = sm.psd_raw.as_ref().and_then(|d| crate::smart_map::parse_sold(d)) + && placed.idnt == *path + { + linked_contents.entry(path.clone()).or_insert(sm.contents_id); + if placed.placed.is_empty() { + unplaced_owners.entry(sm.contents_id).or_insert(layer.id); + } else { + placed_owners.entry(placed.placed).or_insert(layer.id); + } + } + } + SmartOut { depth, globals, known, prune_ok: true, old_fx, linked_contents, placed_owners, unplaced_owners, ..Default::default() } + } + + /// The document's global blocks with the linked-layer files and filter caches brought up to + /// date: unreferenced files dropped (when every reference is known), new files appended, + /// filter caches kept or regenerated per smart object. Unchanged blocks stay byte-identical. + pub(super) fn finish(&self, blocks: Vec) -> Vec { + use std::sync::Arc; + let keep = |u: &str| !self.prune_ok || self.used.contains(u); + let mut out = Vec::with_capacity(blocks.len() + 2); + let mut added = false; + let mut fx_placed = false; + for (sig, key, data) in blocks { + if LINK_KEYS.contains(&&key) { + let add: &[crate::linked::LinkedFile] = if !added && &key == b"lnk2" { &self.add } else { &[] }; + added |= &key == b"lnk2"; + match crate::linked::rebuild_block(&data, &keep, add) { + None => out.push((sig, key, data)), + Some(d) if d.is_empty() => {} + Some(d) => out.push((sig, key, Arc::new(d))), + } + } else if FX_KEYS.contains(&&key) { + let Ok(mut fx) = photocraft_psd::filter_effects::FilterEffects::parse(&data) else { + out.push((sig, key, data)); + continue; + }; + let before = fx.items.len(); + fx.items.retain(|i| self.keep_fx.contains(&i.id)); + let fresh = !fx_placed && !self.new_fx.is_empty(); + if fresh { + fx.items.extend(self.new_fx.iter().cloned()); + fx_placed = true; + } + if fx.items.len() == before && !fresh { + out.push((sig, key, data)); + } else if !fx.items.is_empty() { + out.push((sig, key, Arc::new(padded_fx(&fx)))); + } + } else { + out.push((sig, key, data)); + } + } + if !added && !self.add.is_empty() { + let data: Vec = self.add.iter().flat_map(crate::linked::encode_linked_file).collect(); + out.push((*b"8BIM", *b"lnk2", Arc::new(data))); + } + for (sig, key, data) in self.linked_copies.values() { + if let Some((_, _, block)) = out.iter_mut().find(|(s, k, _)| s == sig && k == key) { + Arc::make_mut(block).extend_from_slice(data); + } else { + out.push((*sig, *key, data.clone())); + } + } + if !fx_placed && !self.new_fx.is_empty() { + let fx = photocraft_psd::filter_effects::FilterEffects { version: 3, items: self.new_fx.clone() }; + out.push((*b"8BIM", *b"FEid", Arc::new(padded_fx(&fx)))); + if !out.iter().any(|(_, k, _)| k == b"FMsk") { + // Filter mask overlay: RGB red at 50 %, Photoshop's default. + out.push((*b"8BIM", *b"FMsk", Arc::new(vec![0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 50]))); + } + } + out + } +} + +/// The source size of a smart object whose file we can't decode: its rendered bounds taken back +/// through the inverse transform (the source's top-left is the origin). +pub(super) fn size_from_layer(sm: &photocraft_doc::SmartObject) -> (f64, f64) { + let r = sm.cache.as_ref().map(Surface::content_bounds).filter(|r| !r.is_empty()); + let (Some(r), Some(inv)) = (r, sm.transform.inverse()) else { return (1.0, 1.0) }; + let [a, b, c, d, e, f] = inv.m; + let (mut w, mut h) = (1.0f64, 1.0f64); + for (x, y) in [(r.x0, r.y0), (r.x1, r.y0), (r.x1, r.y1), (r.x0, r.y1)] { + let (x, y) = (f64::from(x), f64::from(y)); + w = w.max((a * x + c * y + e).round()); + h = h.max((b * x + d * y + f).round()); + } + (w, h) +} + +pub(super) fn upsert(raw: &mut Vec<([u8; 4], Vec)>, key: &[u8; 4], data: Vec, before: Option<&[u8; 4]>) { + if let Some(e) = raw.iter_mut().find(|(k, _)| k == key) { + e.1 = data; + return; + } + match before.and_then(|b| raw.iter().position(|(k, _)| k == b)) { + Some(i) => raw.insert(i, (*key, data)), + None => raw.push((*key, data)), + } +} + +/// Resource 1026 (Layer › Link Layers): one u16 group id per layer record, in the order +/// [`Ex::emit`] writes records (a group's bounding divider, its children, then the group). +/// Imported ids that fit in a u16 are kept, so unchanged files write the same bytes; otherwise +/// groups are renumbered by first appearance. `None` when no layer is linked. +pub(super) fn link_group_resource(layers: &[Layer]) -> Option> { + fn walk(layers: &[Layer], out: &mut Vec>) { + for l in layers { + if let LayerContent::Group(g) = &l.content { + out.push(None); + walk(&g.children, out); + } + out.push(l.link_group); + } + } + let mut per = Vec::new(); + walk(layers, &mut per); + if per.iter().all(Option::is_none) { + return None; + } + let fits = per.iter().flatten().all(|&g| (1..=u64::from(u16::MAX)).contains(&g)); + let mut seen: Vec = Vec::new(); + let ids = per.iter().map(|g| match *g { + None => 0u16, + Some(g) if fits => g as u16, + Some(g) => { + let i = seen.iter().position(|&s| s == g).unwrap_or_else(|| { + seen.push(g); + seen.len() - 1 + }); + u16::try_from(i + 1).unwrap_or(u16::MAX) + } + }); + Some(ids.flat_map(u16::to_be_bytes).collect()) +} + +/// Whether the layer's effects still equal what its preserved `lmfx`/`lfx2` +/// decodes to. `None` when there is nothing preserved or it can't be decoded. +/// Block keys a smart object's `psd_raw` may be written under, in priority order. The data says +/// which it is: `PlLd` holds a `plcL` structure, `SoLd`/`SoLE` a `soLD` one (Adobe PSD spec, +/// "Placed Layer" and "Placed Layer Data"). Writing one under the other's key makes the block +/// unreadable. +pub(super) fn smart_keys(data: Option<&[u8]>) -> &'static [&'static [u8; 4]] { + match data.and_then(|d| d.get(..4)) { + Some(b"plcL") => &[b"PlLd"], + Some(b"soLD") => &[b"SoLd", b"SoLE"], + _ => &[b"SoLd", b"PlLd", b"SoLE"], + } +} + +pub(super) fn effects_unchanged(l: &Layer) -> Option { + let src = l.psd_blocks.iter().find(|(k, _)| k == b"lmfx").map(|(_, d)| d.clone()).or_else(|| l.effects.psd_raw.clone())?; + let (m, items) = crate::effects_map::parse_lfx2(&src)?; + Some(m == l.effects.enabled && items == l.effects.items) +} diff --git a/crates/io/src/psd_export/tests.rs b/crates/io/src/psd_export/tests.rs new file mode 100644 index 0000000000..effa34a10a --- /dev/null +++ b/crates/io/src/psd_export/tests.rs @@ -0,0 +1,620 @@ +use super::*; + +fn document(width: u32, height: u32) -> Document { + Document::new("size estimate", photocraft_geom::Size::new(width, height), ColorMode::Rgb, SampleType::U8) +} + +/// Photoshop opens a PSD's bottom layer as its Background only when that layer has no +/// transparency channel (measured on Photoshop 27.11: with the channel written, re-exported +/// files reopened with the Background as an ordinary layer). +#[test] +fn a_background_is_written_without_a_transparency_channel() { + use photocraft_color::Color; + let size = photocraft_geom::Size::new(8, 6); + let mut doc = Document::with_background("bg", size, ColorMode::Rgb, SampleType::U8, Color::rgb(0.2, 0.4, 0.6)); + doc.layers.push(Layer::raster("Layer 1", doc.pixel_format())); + let file = document_to_psd(&doc); + let layers = &file.layer_info.as_ref().unwrap().layers; + let ids = |i: usize| layers[i].channels.iter().map(|c| c.id).collect::>(); + assert_eq!(ids(0), vec![0, 1, 2], "the Background has no -1 channel"); + assert_eq!(layers[0].flags.0, LayerFlags::BIT4_USEFUL | LayerFlags::TRANSPARENCY_PROTECTED, "as Photoshop writes it"); + assert!(ids(1).contains(&-1), "other layers keep their transparency"); + // It comes back as the same locked, opaque Background. + let back = crate::import("bg.psd", &file.to_bytes().unwrap()).unwrap().document; + let bg = &back.layers[0]; + assert!(bg.locks.transparency && bg.locks.position, "{:?}", bg.locks); + assert_eq!(bg.surface().unwrap().read_region(back.bounds()), doc.layers[0].surface().unwrap().read_region(doc.bounds())); + // A bottom layer with transparency (or unlocked) is not a Background. + let mut holey = doc.clone(); + holey.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[0.0, 0.0, 0.0, 0.0]); + assert!(document_to_psd(&holey).layer_info.unwrap().layers[0].channels.iter().any(|c| c.id == -1)); + let mut unlocked = doc.clone(); + unlocked.layers[0].locks.position = false; + assert!(document_to_psd(&unlocked).layer_info.unwrap().layers[0].channels.iter().any(|c| c.id == -1)); +} + +/// A flat 1-bit Bitmap file (no layers) opens in Photoshop as its Background; so does it in +/// PhotoCraft, and it is written back as one (no transparency channel). +#[test] +fn a_flat_bitmap_file_opens_and_saves_as_a_background() { + let file = photocraft_psd::testgen::merged_only(Version::Psd, PsdMode::Bitmap, 1, Compression::Raw, 8, 4); + let doc = crate::import("flat.psd", &file.to_bytes().unwrap()).unwrap().document; + let bg = &doc.layers[0]; + assert!(bg.name == "Background" && bg.locks.transparency && bg.locks.position, "{:?}", bg.locks); + // Bitmap has no layers in Photoshop (#4814): the file is saved flat in Bitmap mode, without + // a layer section, and opens as a Background again. + let out = document_to_psd(&doc); + assert_eq!(out.header.color_mode, PsdMode::Bitmap); + assert!(out.layer_info.as_ref().is_none_or(|li| li.layers.is_empty())); + let back = crate::import("flat.psd", &out.to_bytes().unwrap()).unwrap().document; + let bg = &back.layers[0]; + assert!(back.layers.len() == 1 && bg.name == "Background" && bg.locks.transparency && bg.locks.position, "{:?}", bg.locks); +} + +/// A layered document with a spot and an alpha channel. Each channel holds 100 %, 25 %, 75 % +/// and 0 % in four columns, the layout of the Photoshop spot oracles. +fn spot_and_alpha_document() -> Document { + let mut doc = document(4, 1); + doc.layers.push(Layer::group("Group", vec![Layer::raster("Layer 1", doc.pixel_format())])); + let channel = |name: &str| { + let mut s = Surface::new(PixelFormat::GRAY8); + s.write_interleaved(doc.bounds(), &[255, 64, 191, 0]); + photocraft_doc::AlphaChannel::new(name, s) + }; + let mut spot = channel("Spot Color 1"); + spot.spot = Some((photocraft_doc::Color::rgb(0.0, 0.6, 1.0), 0.0)); + let alpha = channel("Alpha 1"); + doc.channels.extend([spot, alpha]); + doc +} + +/// The extra planes after the colour channels (and the merged transparency) of `file`. +fn extra_planes(file: &PsdFile, count: usize) -> Vec { + let planes = file.decode_merged().unwrap(); + let plane = (file.header.width * file.header.height) as usize * usize::from(file.header.depth / 8); + planes[planes.len() - count * plane..].to_vec() +} + +/// Resource 1053's identifiers. +fn alpha_ids(file: &PsdFile) -> Option> { + file.resource(crate::channel_map::ALPHA_IDENTIFIERS).map(|r| r.data.as_chunks::<4>().0.iter().map(|c| u32::from_be_bytes(*c)).collect()) +} + +/// A PSD stores spot ink as Photoshop's own PSDs do (0 = solid ink: the 100, 25, 75 and 0 % +/// columns are 0, 191, 64, 255) and an alpha channel straight (255 = selected). The global +/// layer mask stays empty and flag bit 3 stays on the layers only, not on the group's closing +/// divider: Photoshop opens those as they are, and 509 of the corpus's 721 Photoshop PSDs have +/// an empty mask too. Opening the file gives the same ink back. +#[test] +fn psd_stores_spot_ink_as_photoshop_does() { + let doc = spot_and_alpha_document(); + let file = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); + assert_eq!(extra_planes(&file, 2), [0, 191, 64, 255, 255, 64, 191, 0], "spot, then alpha"); + assert_eq!(file.global_layer_mask.as_ref().map(|m| m.data.len()), Some(0)); + let flags: Vec = file.layers().iter().map(|l| l.flags.0 & LayerFlags::BIT4_USEFUL).collect(); + assert_eq!(flags, [0, LayerFlags::BIT4_USEFUL, LayerFlags::BIT4_USEFUL], "closing divider, layer, group"); + let back = crate::import("spot.psd", &file.to_bytes().unwrap()).unwrap().document; + assert!(back.channels[0].spot.is_some() && back.channels[1].spot.is_none()); + for c in &back.channels { + assert_eq!(c.surface.to_interleaved(back.bounds()), [255, 64, 191, 0], "{}", c.name); + } +} + +/// Resource 1053 numbers the extra channels as Photoshop 27.11 does in a new document: 3, 4, 5 +/// in every colour mode, after 0 for the merged transparency when there is one (its +/// transparent PSD lists `0, 3, 4, 5`). A Quick Mask is an extra channel too. Without extra +/// channels there is none. +#[test] +fn psd_alpha_identifiers_number_the_channels_as_photoshop_does() { + use photocraft_color::Color; + let size = photocraft_geom::Size::new(4, 1); + for mode in [ColorMode::Rgb, ColorMode::Cmyk, ColorMode::Grayscale, ColorMode::Lab] { + let mut doc = Document::with_background("bg", size, mode, SampleType::U8, Color::rgb(1.0, 1.0, 1.0)); + assert_eq!(alpha_ids(&document_to_psd(&doc)), None, "{mode:?} without channels"); + doc.channels = spot_and_alpha_document().channels; + let file = document_to_psd(&doc); + assert!(!file.merged_has_alpha(), "{mode:?}"); + assert_eq!(alpha_ids(&file), Some(vec![3, 4]), "{mode:?}"); + doc.quick_mask = Some(photocraft_doc::AlphaChannel::new("Quick Mask", Surface::new(PixelFormat::GRAY8))); + assert_eq!(alpha_ids(&document_to_psd(&doc)), Some(vec![3, 4, 5]), "{mode:?} with a Quick Mask"); + } + // The empty layer leaves the composite transparent. + let file = document_to_psd(&spot_and_alpha_document()); + assert!(file.merged_has_alpha()); + assert_eq!(alpha_ids(&file), Some(vec![0, 3, 4])); +} + +/// A save writes one fresh 1053 for the channels the document has. A Photoshop file's own is +/// not kept on open (it would be new metadata after every round trip), and one a document +/// carries anyway (kept by an earlier version) is never written: next to the fresh one it +/// would be a second, and without channels it would list channels that are gone. The +/// identifiers here, 3 and 2, are those of psd-tools' `cmyk-alpha-spot.psd` (Photoshop 24.7). +#[test] +fn a_kept_alpha_identifiers_resource_is_not_written() { + let kept = || (crate::channel_map::ALPHA_IDENTIFIERS, String::new(), std::sync::Arc::new([0, 0, 0, 3, 0, 0, 0, 2].to_vec())); + let mut file = document_to_psd(&spot_and_alpha_document()); + file.resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS); + file.resources.push(ImageResource::new(kept().0, kept().2.to_vec())); + let mut opened = crate::import("ps.psd", &file.to_bytes().unwrap()).unwrap().document; + assert!(!opened.metadata.psd_resources.iter().any(|(id, ..)| *id == crate::channel_map::ALPHA_IDENTIFIERS)); + opened.metadata.psd_resources.push(kept()); + let again = PsdFile::from_bytes(&crate::export(&opened, "x.psd", &crate::ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(again.resources.iter().filter(|r| r.id == crate::channel_map::ALPHA_IDENTIFIERS).count(), 1); + assert_eq!(alpha_ids(&again), Some(vec![0, 3, 4])); + opened.channels.clear(); + assert_eq!(alpha_ids(&document_to_psd(&opened)), None, "no channels left"); +} + +#[test] +fn group_divider_id_wraps_without_colliding_with_imported_layer_ids() { + let mut doc = document(8, 8); + let mut group = Layer::group("Imported group", vec![Layer::raster("Child", doc.pixel_format())]); + group.psd_id = Some(u32::MAX); + doc.layers.push(group); + + let file = document_to_psd(&doc); + let ids: Vec = file.layers().iter().filter_map(|layer| layer.layer_id()).collect(); + let unique: std::collections::HashSet = ids.iter().copied().collect(); + assert_eq!(ids.len(), 3, "group, child and closing divider"); + assert_eq!(unique.len(), ids.len(), "no generated ID may collide"); + assert!(unique.contains(&u32::MAX), "preserve the imported group ID"); + assert!(unique.contains(&1), "reuse the first unused positive ID on wrap"); + + let encoded = file.to_bytes().unwrap(); + let parsed = PsdFile::from_bytes(&encoded).unwrap(); + let reloaded_ids: std::collections::HashSet = parsed.layers().iter().filter_map(|layer| layer.layer_id()).collect(); + assert_eq!(unique, reloaded_ids); +} + +/// #5068: Photoshop won't open a file whose `Lr32` comes before the `Mt32` marker it keeps for +/// a 32-bit document's merged transparency (verified in Photoshop 2026 on psd-tools' +/// `transparentbg.psd` re-saved both ways). The layer block follows a kept marker. +#[test] +fn layer_block_follows_a_kept_merged_transparency_marker() { + for (depth, key, marker) in [ + (SampleType::F32, *b"Lr32", *b"Mt32"), + (SampleType::U16, *b"Lr16", *b"Mt16"), + (SampleType::F32, *b"Lr32", *b"Mtrn"), + (SampleType::F32, *b"Lr32", *b"MTrn"), + ] { + let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, depth); + let mut s = Surface::new(doc.pixel_format()); + s.fill_rect(photocraft_geom::Rect::new(1, 1, 5, 4), &[0.5, 0.2, 0.9, 1.0]); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(s))); + doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), (*b"8BIM", marker, std::sync::Arc::new(Vec::new()))]; + let saved = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); + let LayerInfoPlacement::GlobalBlock { index, key: written, .. } = saved.layer_info_placement else { panic!("{depth:?}: a global layer block") }; + assert_eq!(written, key); + let at = saved.global_blocks.iter().position(|b| b.key == marker).expect("the marker is kept"); + assert!(at < index, "{depth:?}: {marker:?} at {at}, the layer block at {index}"); + } + // Without a marker the layer block stays first. + let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, SampleType::F32); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(Surface::new(doc.pixel_format())))); + doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new()))]; + let saved = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); + assert!(matches!(saved.layer_info_placement, LayerInfoPlacement::GlobalBlock { index: 0, .. })); +} + +/// #9563: a `Txt2` kept from the opened file is dropped once no type layer is left (Photoshop +/// drops it too); with a type layer the block is still written. +#[test] +fn a_kept_txt2_goes_with_the_last_type_layer() { + let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(Surface::new(doc.pixel_format())))); + doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Txt2", std::sync::Arc::new(b"<< /0 << /1 (stale) >> >>".to_vec()))]; + let file = document_to_psd(&doc); + assert!(!file.global_blocks.iter().any(|b| b.key == *b"Txt2"), "no type layers: no Txt2"); + + doc.layers.push(Layer::new("T", LayerContent::Text(photocraft_doc::TextLayer::default()))); + let file = document_to_psd(&doc); + assert!(file.global_blocks.iter().any(|b| b.key == *b"Txt2"), "a type layer keeps its Txt2"); +} + +/// A merged-transparency marker kept from the opened file describes that file's composite. +/// Saved with an opaque composite, it would make the first alpha channel read back as the +/// merged transparency, and the channel would be gone: the save drops it. +#[test] +fn an_opaque_save_drops_a_kept_merged_transparency_marker() { + for (depth, marker) in [(SampleType::U8, *b"MTrn"), (SampleType::U8, *b"Mtrn"), (SampleType::U16, *b"Mt16"), (SampleType::F32, *b"Mt32")] { + let case = format!("{depth:?} {}", String::from_utf8_lossy(&marker)); + let mut doc = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, depth); + let mut s = Surface::new(doc.pixel_format()); + s.fill_rect(photocraft_geom::Rect::new(0, 0, 8, 6), &[0.5, 0.2, 0.9, 1.0]); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(s))); + doc.channels.push(photocraft_doc::AlphaChannel::new("Alpha 1", Surface::new(PixelFormat::GRAY8))); + doc.metadata.psd_global_blocks = vec![(*b"8BIM", marker, std::sync::Arc::new(Vec::new()))]; + let file = document_to_psd(&doc); + assert!(!file.global_blocks.iter().any(TaggedBlock::is_merged_transparency_marker), "{case}"); + assert!(!file.merged_has_alpha(), "{case}"); + let back = crate::import("a.psd", &file.to_bytes().unwrap()).unwrap().document; + let names: Vec<&str> = back.channels.iter().map(|c| c.name.as_str()).collect(); + assert_eq!(names, ["Alpha 1"], "{case}"); + } +} + +#[test] +fn unreadable_lr16_warns_on_import_and_is_not_saved_twice() { + // #2700: a 16-bit file whose Lr16 block claims five layers but holds none. + let mut file = photocraft_psd::testgen::merged_only(Version::Psd, PsdMode::Rgb, 16, Compression::Raw, 8, 6); + file.global_layer_mask = Some(GlobalLayerMask::default()); + file.global_blocks.push(TaggedBlock::new(*b"Lr16", vec![0, 5, 0, 0])); + let imported = crate::import("broken.psd", &file.to_bytes().unwrap()).unwrap(); + assert!(imported.warnings.iter().any(|w| w.contains("Lr16") && w.contains("could not be read")), "{:?}", imported.warnings); + + let saved = PsdFile::from_bytes(&document_to_psd(&imported.document).to_bytes().unwrap()).unwrap(); + let ours = matches!(saved.layer_info_placement, LayerInfoPlacement::GlobalBlock { key, .. } if key == *b"Lr16"); + let kept = saved.global_blocks.iter().filter(|b| b.key == *b"Lr16").count(); + assert_eq!((ours, kept), (true, 0), "exactly one Lr16 block: the document's own layers"); + assert_eq!(saved.layers().len(), 1); +} + +#[test] +fn photo_filter_psd_is_four_byte_aligned_and_round_trips() { + let color = [60000u16, 30000, 0].map(|x| x as f32 / 65535.0); + let adjustment = photocraft_doc::Adjustment::PhotoFilter { color, density: 0.14, preserve_luminosity: true }; + let mut doc = + Document::with_background("Photo Filter PSD", photocraft_geom::Size::new(32, 24), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE); + let mut layer = Layer::new("Warming Filter", LayerContent::Adjustment(adjustment.clone())); + doc.layers.push(layer.clone()); + + let assert_phfl = |file: &PsdFile| { + let records = &file.layer_info.as_ref().unwrap().layers; + let data = records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *b"phfl").map(|b| b.data.as_slice()).expect("Photo Filter adjustment data"); + assert_eq!(data.len(), 20, "PSD layer block length includes four-byte padding"); + assert_eq!(&data[0..2], &[0, 2], "version 2 RGB Photo Filter"); + assert_eq!(&data[17..], &[0, 0, 0], "three padding bytes after the 17-byte payload"); + }; + + let exported = document_to_psd(&doc); + assert_phfl(&exported); + let encoded = exported.to_bytes().unwrap(); + let parsed = PsdFile::from_bytes(&encoded).unwrap(); + assert_phfl(&parsed); + let (restored, _) = crate::psd_import::psd_to_document(&parsed); + assert!(restored.layers.iter().any(|l| l.content == LayerContent::Adjustment(adjustment.clone()))); + + // Importing a legacy 18-byte block must still work; saving it again should repair it + // even when the adjustment values have not changed (normally raw blocks are reused). + let mut legacy = crate::adjust_map::write(&adjustment)[0].1.clone(); + legacy.truncate(18); + layer.psd_blocks = vec![(*b"phfl", std::sync::Arc::new(legacy))]; + doc.layers.pop(); + doc.layers.push(layer); + let repaired = document_to_psd(&doc); + assert_phfl(&repaired); + let repaired_bytes = repaired.to_bytes().unwrap(); + assert_phfl(&PsdFile::from_bytes(&repaired_bytes).unwrap()); +} + +#[test] +fn photo_filter_version_3_is_kept_until_edited() { + // The #3777 block (Photoshop CS2, XYZ colour): saved back byte-exact while unchanged. + let v3 = vec![0, 3, 0, 0, 0x5e, 0x7f, 0, 0, 0x61, 0xaa, 0, 0, 0x0e, 0xf3, 0, 0, 0, 0x3f, 1, 0]; + let adjustment = crate::adjust_map::parse(b"phfl", &v3, None, crate::adjust_map::Channels::Rgb); + let photocraft_doc::Adjustment::PhotoFilter { color, .. } = adjustment else { panic!("{adjustment:?}") }; + let mut doc = Document::with_background("Photo Filter v3", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE); + let mut layer = Layer::new("Photo Filter 1", LayerContent::Adjustment(adjustment.clone())); + layer.psd_blocks = vec![(*b"phfl", std::sync::Arc::new(v3.clone()))]; + doc.layers.push(layer); + let phfl = |doc: &Document| { + let file = PsdFile::from_bytes(&document_to_psd(doc).to_bytes().unwrap()).unwrap(); + let records = &file.layer_info.as_ref().unwrap().layers; + records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *b"phfl").map(|b| b.data.clone()).expect("Photo Filter adjustment data") + }; + assert_eq!(phfl(&doc), v3); + // Once edited it is written as version 2 (RGB), and the colour survives. + let edited = photocraft_doc::Adjustment::PhotoFilter { color, density: 0.5, preserve_luminosity: true }; + if let Some(l) = doc.layers.last_mut() { + l.content = LayerContent::Adjustment(edited.clone()); + } + let saved = phfl(&doc); + assert_eq!(&saved[..2], &[0, 2]); + let reread = crate::adjust_map::parse(b"phfl", &saved, None, crate::adjust_map::Channels::Rgb); + let photocraft_doc::Adjustment::PhotoFilter { color: back, density, .. } = reread else { panic!("{reread:?}") }; + assert!(back.iter().zip(color).all(|(b, c)| (b - c).abs() <= 1.0 / 65535.0), "{back:?} vs {color:?}"); + assert_eq!(density, 0.5); +} + +#[test] +fn light_adjustment_is_kept_verbatim_and_warned() { + // #6542: Photoshop 2026's Light layer is a `brit` block whose `CgEd` descriptor carries + // more than Brightness/Contrast. Synthetic block: the extra key names are placeholders. + use photocraft_psd::descriptor::{Descriptor, Value, VersionedDescriptor}; + let brit = vec![0, 0, 0xff, 0xfe, 0, 127, 0, 0]; + let d = Descriptor::new("null") + .with("Vrsn", Value::Integer(1)) + .with("Brgh", Value::Integer(0)) + .with("Cntr", Value::Integer(-2)) + .with("Exps", Value::Double(-2.3)) + .with("Hghl", Value::Integer(-64)) + .with("useLegacy", Value::Boolean(false)); + let cged = VersionedDescriptor::new(d).to_bytes(); + let adjustment = crate::adjust_map::parse(b"brit", &brit, Some(&cged), crate::adjust_map::Channels::Rgb); + assert_eq!(adjustment, photocraft_doc::Adjustment::Unsupported { psd_key: "brit".into(), raw: brit.clone().into() }); + let mut doc = Document::with_background("Light", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8, photocraft_doc::Color::WHITE); + let mut layer = Layer::new("Light 1", LayerContent::Adjustment(adjustment)); + layer.psd_blocks = vec![(*b"brit", std::sync::Arc::new(brit.clone())), (*b"CgEd", std::sync::Arc::new(cged.clone()))]; + doc.layers.push(layer); + let block = |file: &PsdFile, key: &[u8; 4]| { + let records = &file.layer_info.as_ref().unwrap().layers; + records.iter().flat_map(|r| r.blocks.iter()).find(|b| b.key == *key).map(|b| b.data.clone()) + }; + let file = PsdFile::from_bytes(&document_to_psd(&doc).to_bytes().unwrap()).unwrap(); + let (restored, warnings) = crate::psd_import::psd_to_document(&file); + assert!(warnings.iter().any(|w| w.contains("\"Light 1\"") && w.contains("Light settings")), "{warnings:?}"); + // Saved again unchanged, both blocks are written back byte for byte. + let again = PsdFile::from_bytes(&document_to_psd(&restored).to_bytes().unwrap()).unwrap(); + assert_eq!(block(&again, b"brit").as_deref(), Some(&brit[..])); + assert_eq!(block(&again, b"CgEd").as_deref(), Some(&cged[..])); + // A plain Brightness/Contrast descriptor still reads as Brightness/Contrast. + let plain = crate::adjust_map::write(&photocraft_doc::Adjustment::BrightnessContrast { brightness: 5.0, contrast: -2.0, legacy: false }); + assert!(!crate::adjust_map::is_extended_brit(Some(&plain[1].1))); +} + +/// A 1024² RGB document with one full-canvas layer: flat colour (packs to a few KB) or noise +/// (RLE can't pack it). Its raw planes (layer RGBA + merged RGB) are 7 MiB either way. +fn sized_document(noise: bool, depth: SampleType) -> Document { + const SIDE: u32 = 1024; + let mut doc = Document::new("size limit", photocraft_geom::Size::new(SIDE, SIDE), ColorMode::Rgb, depth); + let mut layer = Layer::raster("Layer 1", doc.pixel_format()); + let rect = photocraft_geom::Rect::new(0, 0, SIDE as i32, SIDE as i32); + let surface = layer.surface_mut().unwrap(); + if noise { + let mut state = 0x2545_f491_u32; + let data: Vec = (0..SIDE * SIDE * 4) + .map(|i| { + state ^= state << 13; + state ^= state >> 17; + state ^= state << 5; + if i % 4 == 3 { 1.0 } else { (state >> 24) as f32 / 255.0 } + }) + .collect(); + surface.write_region(rect, &data); + } else { + surface.fill_rect(rect, &[0.2, 0.4, 0.6, 1.0]); + } + doc.layers.push(layer); + doc +} + +const SIZED_RAW_BYTES: u64 = 1024 * 1024 * (4 + 3); +/// Under the raw size, so only the encoded size can keep a document PSD. +const SIZED_LIMIT: u64 = 4 * 1024 * 1024; +const _: () = assert!(SIZED_RAW_BYTES > SIZED_LIMIT); + +fn export_sized(doc: &Document, limit: u64) -> (PsdFile, Vec) { + document_to_psd_sized(doc, &PsdExportOptions::default(), 0, &Interrupt::NONE, limit) +} + +/// #5285: a document whose raw pixels are over the PSD limit but whose encoded file fits is +/// saved as PSD (the raw-size estimate used to make it a PSB). +#[test] +fn a_file_that_fits_after_compression_stays_psd() { + let doc = sized_document(false, SampleType::U8); + let (file, warnings) = export_sized(&doc, SIZED_LIMIT); + assert_eq!(file.written_version(), Version::Psd); + assert!(warnings.iter().all(|w| !w.contains("PSB")), "{warnings:?}"); + let bytes = file.to_bytes().unwrap(); + assert!(bytes.len() as u64 + PSD_SAVE_HEADROOM <= SIZED_LIMIT, "{} bytes", bytes.len()); + let (back, _) = crate::psd_to_document(&PsdFile::from_bytes(&bytes).unwrap()); + assert_eq!(back.layers[0].surface().unwrap().pixel(700, 300), doc.layers[0].surface().unwrap().pixel(700, 300)); +} + +/// #5285: a document whose encoded file is over the limit becomes a PSB, byte for byte the +/// one a forced PSB save writes, and reads back. 8 bits keep the layers in the layer section, +/// 16 bits in a global `Lr16` block. +#[test] +fn a_file_over_the_limit_after_compression_is_psb() { + for depth in [SampleType::U8, SampleType::U16] { + let doc = sized_document(true, depth); + let (file, warnings) = export_sized(&doc, SIZED_LIMIT); + assert_eq!(file.written_version(), Version::Psb, "{depth:?}"); + assert!(warnings.iter().any(|w| w == "encoded size exceeds 2 GB; written as PSB"), "{warnings:?}"); + let bytes = file.to_bytes().unwrap(); + assert!(bytes.len() as u64 > SIZED_LIMIT); + let forced = document_to_psd_with(&doc, &PsdExportOptions { force_psb: true, ..Default::default() }).0; + let forced = forced.to_bytes().unwrap(); + assert!(bytes == forced, "{depth:?}: the promoted PSB differs from a PSB written directly"); + assert_eq!(PsdFile::from_bytes(&bytes).unwrap(), PsdFile::from_bytes(&forced).unwrap(), "{depth:?}"); + // The promoted model itself stays a consistent PSD for every reader. + assert_eq!(file.header.version, Version::Psd); + assert_eq!(crate::psd_to_document(&file).0.layers[0].surface().unwrap().pixel(511, 777), doc.layers[0].surface().unwrap().pixel(511, 777)); + let (back, _) = crate::psd_to_document(&PsdFile::from_bytes(&bytes).unwrap()); + for (x, y) in [(0, 0), (1023, 0), (511, 777), (1023, 1023)] { + assert_eq!(back.layers[0].surface().unwrap().pixel(x, y), doc.layers[0].surface().unwrap().pixel(x, y), "{depth:?} ({x}, {y})"); + } + } +} + +/// The limit is the exact written length plus the save's headroom: one byte less is a PSB. +#[test] +fn the_size_limit_is_the_exact_encoded_length() { + let doc = sized_document(false, SampleType::U8); + let len = export_sized(&doc, u64::MAX).0.to_bytes().unwrap().len() as u64; + assert_eq!(export_sized(&doc, len + PSD_SAVE_HEADROOM).0.written_version(), Version::Psd); + assert_eq!(export_sized(&doc, len + PSD_SAVE_HEADROOM - 1).0.written_version(), Version::Psb); + // The real limit: a small document stays PSD, a forced one is PSB. + let small = document(1, 1); + assert_eq!(document_to_psd(&small).written_version(), Version::Psd); + assert_eq!(document_to_psd_with(&small, &PsdExportOptions { force_psb: true, ..Default::default() }).0.written_version(), Version::Psb); + assert_eq!(psd_version(&document(30_001, 1), false), Version::Psb); +} + +/// A Multichannel document follows the same rule. +#[test] +fn a_multichannel_file_over_the_limit_is_psb() { + let mut doc = document(64, 64); + doc.mode = ColorMode::Multichannel; + doc.channels.push(photocraft_doc::AlphaChannel::new("Cyan", photocraft_raster::Surface::new(PixelFormat::GRAY8))); + let (file, _) = export_sized(&doc, u64::MAX); + assert_eq!(file.written_version(), Version::Psd); + let len = file.to_bytes().unwrap().len() as u64; + let (file, warnings) = export_sized(&doc, len + PSD_SAVE_HEADROOM - 1); + assert_eq!(file.written_version(), Version::Psb); + assert!(warnings.iter().any(|w| w.contains("written as PSB")), "{warnings:?}"); + let back = PsdFile::from_bytes(&file.to_bytes().unwrap()).unwrap(); + assert_eq!(back.header.version, Version::Psb); + assert_eq!(back.decode_merged().unwrap(), PsdFile::from_bytes(&export_sized(&doc, u64::MAX).0.to_bytes().unwrap()).unwrap().decode_merged().unwrap()); +} + +/// Indexed Color and Bitmap (written without layers, #4816), Multichannel and RGB follow the +/// encoded-size rule, and a file turned into PSB by its size writes its global blocks with +/// 8-byte lengths as `8B64` (#7937), whatever signature they were kept with; a file that fits +/// stays a PSD with `8BIM` throughout. +#[test] +fn every_mode_promoted_by_size_signs_long_blocks_8b64() { + let white = photocraft_color::Color::rgba(1.0, 1.0, 1.0, 1.0); + let mut docs = Vec::new(); + for (mode, psd_mode) in [(ColorMode::Indexed, PsdMode::Indexed), (ColorMode::Bitmap, PsdMode::Bitmap), (ColorMode::Rgb, PsdMode::Rgb)] { + let mut doc = Document::with_background("modes", photocraft_geom::Size::new(64, 64), ColorMode::Rgb, SampleType::U8, white); + doc.mode = mode; + doc.color_table = Some(photocraft_doc::ColorTable { colors: vec![[255, 255, 255], [0, 0, 0]], transparent: None }); + docs.push((doc, psd_mode)); + } + let mut multichannel = document(64, 64); + multichannel.mode = ColorMode::Multichannel; + multichannel.channels.push(photocraft_doc::AlphaChannel::new("Cyan", photocraft_raster::Surface::new(PixelFormat::GRAY8))); + docs.push((multichannel, PsdMode::Multichannel)); + for (mut doc, psd_mode) in docs { + doc.metadata.psd_global_blocks = vec![ + (*b"8BIM", *b"cinf", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8BIM", *b"pths", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8B64", *b"FMsk", std::sync::Arc::new(vec![0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50])), + (*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), + ]; + let written = |limit| { + let (file, warnings) = export_sized(&doc, limit); + let bytes = file.to_bytes().unwrap(); + let back = PsdFile::from_bytes(&bytes).unwrap(); + let blocks = back.global_blocks.iter().map(|b| (b.signature, b.key)).collect::>(); + (bytes.len() as u64, back, blocks, warnings) + }; + let (len, small, blocks, _) = written(u64::MAX); + assert_eq!((small.header.version, small.header.color_mode), (Version::Psd, psd_mode), "{psd_mode:?}"); + assert_eq!(blocks, vec![(*b"8BIM", *b"cinf"), (*b"8BIM", *b"pths"), (*b"8BIM", *b"FMsk"), (*b"8BIM", *b"Patt")], "{psd_mode:?}: a PSD keeps 8BIM"); + // One byte short of the encoded length: the same document is written as PSB. + let (_, big, blocks, warnings) = written(len + PSD_SAVE_HEADROOM - 1); + assert_eq!((big.header.version, big.header.color_mode), (Version::Psb, psd_mode), "{psd_mode:?}: {warnings:?}"); + assert!(warnings.iter().any(|w| w == "encoded size exceeds 2 GB; written as PSB"), "{psd_mode:?}: {warnings:?}"); + assert_eq!(blocks, vec![(*b"8B64", *b"cinf"), (*b"8B64", *b"pths"), (*b"8B64", *b"FMsk"), (*b"8BIM", *b"Patt")], "{psd_mode:?}: a PSB signs 8B64"); + assert_eq!(big.decode_merged().unwrap(), small.decode_merged().unwrap(), "{psd_mode:?}"); + // Without `size_limit_psb` no mode is promoted by its size, the layerless ones included. + let opts = PsdExportOptions { size_limit_psb: false, ..PsdExportOptions::default() }; + let (kept, warnings) = document_to_psd_sized(&doc, &opts, 0, &Interrupt::NONE, len + PSD_SAVE_HEADROOM - 1); + assert_eq!((kept.written_version(), kept.header.color_mode), (Version::Psd, psd_mode), "{psd_mode:?}: {warnings:?}"); + assert!(!warnings.iter().any(|w| w.contains("written as PSB")), "{psd_mode:?}: {warnings:?}"); + } +} + +#[test] +fn kept_psb_blocks_are_written_as_8bim_in_a_psd() { + // Photoshop 27.11 refuses a PSD holding `8B64` blocks ("not compatible with this + // version"); they came from re-saving a PSB as PSD with its global FMsk and cinf kept. + let mut doc = document(4, 4); + doc.metadata.psd_global_blocks = + vec![(*b"8B64", *b"FMsk", std::sync::Arc::new(vec![0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50])), (*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new()))]; + let signatures = |force_psb| { + let opts = PsdExportOptions { force_psb, ..PsdExportOptions::default() }; + let bytes = document_to_psd_with(&doc, &opts).0.to_bytes().unwrap(); + let file = PsdFile::from_bytes(&bytes).unwrap(); + file.global_blocks.iter().map(|b| (b.signature, b.key)).collect::>() + }; + assert_eq!(signatures(false), vec![(*b"8BIM", *b"FMsk"), (*b"8BIM", *b"Patt")]); + assert_eq!(signatures(true), vec![(*b"8B64", *b"FMsk"), (*b"8BIM", *b"Patt")], "a PSB keeps 8B64"); +} + +/// A PSB writes every global block with an 8-byte length as `8B64`, the way Photoshop does: +/// kept from a PSD source, `lnk2`, `pths` and `cinf` arrive as `8BIM`, and with that signature +/// and the PSB's 8-byte length Photoshop 27.11 refused the whole file (#7937). Blocks with a +/// 4-byte length keep `8BIM`, and a PSD keeps `8BIM` throughout. +#[test] +fn psb_writes_long_length_blocks_as_8b64() { + let mut doc = document(4, 4); + doc.metadata.psd_global_blocks = vec![ + (*b"8BIM", *b"cinf", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8BIM", *b"pths", std::sync::Arc::new(vec![0, 0, 0, 16, 0, 0, 0, 0])), + (*b"8BIM", *b"Patt", std::sync::Arc::new(Vec::new())), + ]; + let written = |force_psb| { + let opts = PsdExportOptions { force_psb, ..PsdExportOptions::default() }; + let bytes = document_to_psd_with(&doc, &opts).0.to_bytes().unwrap(); + let file = PsdFile::from_bytes(&bytes).unwrap(); + let blocks = file.global_blocks.iter().map(|b| (b.signature, b.key, b.data.len())).collect::>(); + (bytes, blocks) + }; + let (bytes, blocks) = written(true); + assert_eq!(blocks, vec![(*b"8B64", *b"cinf", 8), (*b"8B64", *b"pths", 8), (*b"8BIM", *b"Patt", 0)]); + // On disk: the signature, the key, then the 8-byte length. + let at = bytes.windows(8).position(|w| w == b"8B64cinf").expect("8B64cinf in the file"); + assert_eq!(bytes.get(at + 8..at + 16), Some(&[0u8, 0, 0, 0, 0, 0, 0, 8][..])); + assert!(!bytes.windows(8).any(|w| w == b"8BIMcinf" || w == b"8BIMpths")); + let (_, blocks) = written(false); + assert_eq!(blocks, vec![(*b"8BIM", *b"cinf", 8), (*b"8BIM", *b"pths", 8), (*b"8BIM", *b"Patt", 0)], "a PSD keeps 8BIM"); +} + +/// A layered document with transparency in the merged image and an alpha channel: the name +/// and DisplayInfo resources hold one entry per extra channel, the merged transparency first, +/// as in every file Photoshop opens (one short, Photoshop 27.11 refused the file with "Could +/// not import the clipboard because an unexpected end-of-file was encountered"). The alpha +/// channel keeps its own name and options on the way back. +#[test] +fn alpha_names_count_the_merged_transparency() { + let mut doc = document(8, 6); + let mut layer = Layer::raster("Layer 1", doc.pixel_format()); + layer.surface_mut().unwrap().write_pixel(1, 1, &[1.0, 0.0, 0.0, 1.0]); + doc.layers.push(layer); + let mut a = photocraft_doc::AlphaChannel::new("Alpha 1", photocraft_raster::Surface::new(PixelFormat::GRAY8)); + a.opacity = 0.5; + doc.channels.push(a); + let file = document_to_psd(&doc); + let header_extra = usize::from(file.header.channels) - 3; + let names = |id| file.resources.iter().find(|r| r.id == id).map(|r| r.data.clone()).unwrap(); + let pascal = names(1006); + let (mut count, mut i) = (0, 0); + while i < pascal.len() { + count += 1; + i += 1 + usize::from(pascal[i]); + } + assert_eq!((header_extra, count), (2, 2), "transparency + Alpha 1"); + assert_eq!(names(crate::channel_map::DISPLAY_INFO).len(), 4 + 2 * 13); + let back = crate::import("a.psd", &file.to_bytes().unwrap()).unwrap().document; + assert_eq!(back.channels.len(), 1); + assert_eq!(back.channels[0].name, "Alpha 1"); + assert!((back.channels[0].opacity - 0.5).abs() < 1e-6); +} + +/// A linked smart object whose path names a FIFO is not read (#8601): opening a FIFO for +/// reading blocks until a writer appears, and a device such as `/dev/zero` never ends, so the +/// export would hang or run out of memory. The export finishes and writes the layer as pixels, +/// saying why. The export runs on its own thread so that a regression fails here instead of +/// hanging the test run. +#[cfg(unix)] +#[test] +fn a_linked_fifo_is_not_read() { + let dir = std::env::temp_dir().join(format!("photocraft-linked-fifo-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let fifo = dir.join("link.png"); + let _ = std::fs::remove_file(&fifo); + assert!(std::process::Command::new("mkfifo").arg(&fifo).status().unwrap().success(), "mkfifo failed"); + let mut doc = document(8, 6); + let smart = photocraft_doc::SmartObject { + contents_id: SmartContentsId::fresh(), + source: photocraft_doc::SmartSource::Linked { path: fifo.to_string_lossy().into_owned() }, + transform: photocraft_geom::Affine::IDENTITY, + smart_filters: vec![], + cache: Some(Surface::new(doc.pixel_format())), + psd_raw: None, + filters_enabled: true, + filter_mask: None, + warp: None, + stack_mode: None, + perspective: None, + }; + doc.layers.push(Layer::new("Linked", LayerContent::Smart(smart))); + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || tx.send(document_to_psd_with(&doc, &PsdExportOptions::default()).1)); + let warnings = rx.recv_timeout(std::time::Duration::from_secs(30)).expect("the export blocked reading the FIFO"); + let _ = std::fs::remove_dir_all(&dir); + assert!(warnings.iter().any(|w| w.contains("smart object written as pixels") && w.contains("not a regular file")), "{warnings:?}"); +} diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 12307f32ba..19cc08b3f8 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -4,7 +4,8 @@ use std::sync::Arc; use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; use photocraft_doc::{ - AlphaChannel, Document, Effects, FillCache, Group, Layer, LayerContent, LayerMask, ShapeLayer, SmartContentsId, SmartObject, SmartSource, TextLayer, + AlphaChannel, Document, Effects, FillCache, Group, Layer, LayerContent, LayerMask, PsdGlobalBlock, ShapeLayer, SmartContentsId, SmartObject, SmartSource, + TextLayer, }; use photocraft_geom::{Rect, Size, TILE_SIZE}; use photocraft_psd::layer::{CHANNEL_REAL_USER_MASK, CHANNEL_TRANSPARENCY, CHANNEL_USER_MASK}; @@ -46,17 +47,30 @@ pub(crate) const REGENERATED: [&[u8; 4]; 9] = [b"luni", b"lyid", b"lsct", b"lsdk pub(crate) struct Ctx<'a> { pub file: &'a PsdFile, + /// Global blocks shared with the document. + pub global: Vec, pub fmt: PixelFormat, pub mask_fmt: PixelFormat, pub cc: usize, pub cmyk: bool, pub warnings: Vec, + /// Decoding encountered damaged PSD data; interactive opens ask whether to continue. + pub damaged: bool, + /// Failure to read source channel data, which must fail the open. + pub read_error: Option, /// Document resolution (type sizes are converted to points with it). pub dpi: f32, /// The parsed `Txt2` block (type settings EngineData lacks, e.g. optical kerning). pub txt2: Option, /// Smart-filter caches from the global `FEid`/`FXid` blocks (filter masks, by placed id). pub filter_effects: Vec, + /// Filter masks already decoded, by placed id: every layer of a source reads the same item, and + /// a surface clones by sharing its tiles. + pub filter_masks: std::collections::HashMap>, + /// The same for the unfiltered pixels the mask mixes back in. + pub unfiltered: std::collections::HashMap>, + /// Pixel bytes retained in [`Self::unfiltered`], held under [`MAX_UNFILTERED_BYTES`]. + pub unfiltered_bytes: u64, /// Placed layers naming the same source share editable contents, not their placement. pub smart_contents: std::collections::HashMap, /// Source sizes of `PlLd`-only placed layers, by source id (read once per embedded file). @@ -65,9 +79,43 @@ pub(crate) struct Ctx<'a> { pub ctl: photocraft_raster::Interrupt<'a>, /// Layer records decoded so far, out of `total` (progress). pub done: usize, + /// Uniform tiles shared across imported layers. + pub uniform: photocraft_raster::TileInterner, pub total: usize, } +/// Bytes of interleaved pixels a band decoded into tiles at once aims at: whole layers up to this +/// size decode in one band, larger ones in bands of whole tile rows. +const BAND_BYTES: usize = 32 << 20; + +/// Row ranges (`0..h`, relative to a layer whose first row is at document row `top`) to decode a +/// layer in: each ends on a tile row boundary, so no tile is written by two bands, and holds +/// about [`BAND_BYTES`] of `row_bytes`-byte rows. +fn band_rows(top: i32, h: usize, row_bytes: usize) -> Vec<(usize, usize)> { + let tile = TILE_SIZE as usize; + let rows = (BAND_BYTES / row_bytes.max(1)).max(tile) / tile * tile; + // The first band ends at the first tile boundary at least `rows` rows in. + let into_tile = usize::try_from(top.rem_euclid(TILE_SIZE)).unwrap_or(0); + let mut out = Vec::new(); + let mut r0 = 0; + let mut r1 = rows - into_tile; + while r0 < h { + let end = r1.min(h); + out.push((r0, end)); + r0 = end; + r1 = end + rows; + } + out +} + +fn import_format(file: &PsdFile) -> (ColorMode, bool, bool, SampleType) { + let mode = doc_mode(file.header.color_mode).unwrap_or(ColorMode::Rgb); + let layered = matches!(mode, ColorMode::Grayscale | ColorMode::Rgb | ColorMode::Cmyk | ColorMode::Lab) && file.header.depth != 1; + let multichannel = mode == ColorMode::Multichannel && file.header.depth != 1; + let depth = if layered || multichannel { sample_for_depth(file.header.depth) } else { SampleType::U8 }; + (mode, layered, multichannel, depth) +} + fn doc_mode(m: PsdMode) -> Option { Some(match m { PsdMode::Bitmap => ColorMode::Bitmap, @@ -88,9 +136,19 @@ fn selected_real_mask(rec: &LayerRecord) -> Option { rec.layer_mask()?.real } +/// The (density, feather) a shape layer's mask parameters give its path, which is its vector +/// mask: (1, 0) without them. +fn shape_mask_params(rec: &LayerRecord) -> (f32, f32) { + let p = rec.layer_mask().and_then(|m| m.parameters); + let density = p.and_then(|p| p.vector_density).map_or(1.0, |d| f32::from(d) / 255.0); + let feather = p.and_then(|p| p.vector_feather).filter(|f| f.is_finite()).map_or(0.0, |f| (f as f32).clamp(0.0, 1000.0)); + (density, feather) +} + /// The fill of a plain shape layer (fill block + vector path, no stroke) to import as a fill layer -/// with a vector mask: when its mask parameters give the vector mask a density below 100 % or a -/// feather (`.1` true), or when it fills with a pattern. +/// with a vector mask: when it fills with a pattern, which the shape renderer can't draw (`.1` +/// true when its vector mask is also soft: a density below 100 % or a feather). Other soft +/// shapes stay shapes with that density and feather. fn soft_shape_fill(rec: &LayerRecord, has_vector: bool, fill_key: Option<&[u8; 4]>) -> Option<(photocraft_doc::Fill, bool)> { if !has_vector || rec.block(b"vstk").is_some() || rec.block(b"vscg").is_some() { return None; @@ -104,7 +162,19 @@ fn soft_shape_fill(rec: &LayerRecord, has_vector: bool, fill_key: Option<&[u8; 4 // (our own export writes fill layers so, #1907): it is a pattern fill layer seen through // its vector mask. let pattern = matches!(fill, photocraft_doc::Fill::Pattern { .. }); - (soft || pattern).then_some((fill, soft)) + pattern.then_some((fill, soft)) +} + +#[derive(Clone, Copy)] +struct DecodedImport<'a> { + file: &'a PsdFile, + merged: &'a Option>>, + sizes: Option<(usize, usize)>, + canvas: Rect, + layered: bool, + multichannel: bool, + depth: SampleType, + fmt: PixelFormat, } /// Where a placed layer's source goes: read from `SoLd`/`SoLE`, or from a CS2 `PlLd` record. @@ -122,6 +192,24 @@ impl Ctx<'_> { self.warnings.push(s.into()); } + /// Records a PSD decoding failure and marks the import as potentially incomplete. + /// Unlike compatibility warnings, this requires interactive confirmation. + fn warn_unreadable_data(&mut self, s: impl Into) { + self.damaged = true; + self.warn(s); + } + + /// A channel that failed to decode: a failed read of the file is kept to fail the open + /// (`true`); damaged data is warned about, its layer left empty (`false`). + fn channel_failed(&mut self, name: &str, id: i16, e: &photocraft_psd::PsdError) -> bool { + if let photocraft_psd::PsdError::Read(msg) = e { + self.read_error.get_or_insert_with(|| format!("layer \"{name}\": {msg}")); + return true; + } + self.warn_unreadable_data(format!("layer \"{name}\": channel {id} could not be decoded ({e}); treated as empty")); + false + } + /// Source id, transform, (Distort/Perspective) projective placement and warp of a placed /// layer that has only a `PlLd` block, as Photoshop CS2 and older write it (#2465). `PlLd` /// stores the placed corners but not the source size, which comes from the embedded file. @@ -142,85 +230,101 @@ impl Ctx<'_> { return Placement::default(); } }; - let (file, ctl) = (self.file, self.ctl); + let (global, ctl) = (&self.global, self.ctl); let size = *self .plld_sizes .entry(id.clone()) - .or_insert_with(|| crate::linked::source_size_in_blocks(file.global_blocks.iter().map(|b| (&b.key, b.data.as_slice())), &id, &ctl)); + .or_insert_with(|| crate::linked::source_size_in_blocks(global.iter().map(|(_, k, d)| (k, d.as_slice())), &id, &ctl)); // Without the source (missing or unreadable), it is taken as placed at 100 %. let (transform, perspective) = blocks::plld_transform(&p, size, warp.as_ref()); Placement { id, transform, perspective, warp } } + /// Decodes a PSD layer channel, marking the document damaged if its data is unreadable. fn channel_plane(&mut self, rec: &LayerRecord, id: i16, name: &str) -> Option> { rec.channel(id)?; + if self.read_error.is_some() { + return None; + } match rec.decode_channel(id, self.file.header.depth, self.file.header.version) { Ok(p) => Some(p), Err(e) => { - self.warn(format!("layer \"{name}\": channel {id} could not be decoded ({e}); treated as empty")); + self.channel_failed(name, id, &e); None } } } - /// [`Self::channel_plane`] without the warnings sink, so a layer's channels decode - /// concurrently (their RLE rows are the bulk of the open time on full-canvas layers). - fn channel_plane_decoded(&self, rec: &LayerRecord, id: i16) -> Option> { - rec.channel(id)?; - rec.decode_channel(id, self.file.header.depth, self.file.header.version).ok() - } - + /// Builds a raster surface from the layer channels, leaving the surface empty if + /// any present channel fails to decode rather than inventing replacement pixels. fn record_surface(&mut self, rec: &LayerRecord, name: &str) -> Surface { let r = rec.rect; - if r.is_empty() || r.size().is_err() { + // Once the file couldn't be read the open fails: nothing more is decoded. + if r.is_empty() || r.size().is_err() || self.read_error.is_some() { return Surface::new(self.fmt); } let (w, h) = r.size().unwrap_or((0, 0)); let s = self.fmt.sample; + let (depth, version) = (self.file.header.depth, self.file.header.version); let ids: Vec = (0..self.cc as i16).chain([CHANNEL_TRANSPARENCY]).collect(); - let planes: Vec>> = if cfg!(target_arch = "wasm32") { - ids.iter().map(|&id| self.channel_plane(rec, id, name)).collect() - } else { - // The channels decode independently; their RLE rows are the bulk of the open - // time for full-canvas layers, so they run side by side and the warnings land - // in the same order afterwards. - // par_map reuses the crate's worker pool (one OS thread per channel per layer - // would spawn thousands on a file with many layers). - let decoded: Vec>> = crate::pixels::par_map(ids.to_vec(), |id| self.channel_plane_decoded(rec, id)); - let mut planes: Vec>> = Vec::with_capacity(decoded.len()); - for (&id, p) in ids.iter().zip(decoded) { - if p.is_none() && rec.channel(id).is_some() { - self.warn(format!("layer \"{name}\": channel {id} could not be decoded; treated as empty")); - } - planes.push(p); + // A decoder per channel present (a ZIP channel is inflated here, the channels side by + // side); the rows are then decoded and interleaved a band at a time straight into the + // tiles, so no buffer of the whole layer (planes or interleaved) is ever held. + // par_map reuses the crate's worker pool (one OS thread per channel per layer would + // spawn thousands on a file with many layers). + let decoders: Vec>>> = + crate::pixels::par_map(ids.to_vec(), |id| rec.channel(id).map(|ch| ch.row_decoder(w, h, depth, version))); + let mut failed = false; + for (&id, d) in ids.iter().zip(&decoders) { + if let Some(Err(e)) = d { + self.channel_failed(name, id, e); + failed = true; } - planes - }; + } // A channel that is in the file but could not be decoded leaves the layer empty, as the // warning says. Filling in for it would paint an opaque black layer over the document. - if ids.iter().zip(planes.iter()).any(|(&id, p)| p.is_none() && rec.channel(id).is_some()) { + if failed { return Surface::new(self.fmt); } // Only decoded channel data justifies a buffer: the decoders bound their output by the // bytes in the file, but a bare rectangle can declare 300000² pixels in a few bytes (#755). - if planes.iter().all(Option::is_none) { - self.warn(format!("layer \"{name}\": no channel data for its {w}x{h} bounds; treated as empty")); + if decoders.iter().all(Option::is_none) { + self.warn_unreadable_data(format!("layer \"{name}\": no channel data for its {w}x{h} bounds; treated as empty")); return Surface::new(self.fmt); } - let refs: Vec> = planes.iter().map(|p| p.as_deref()).collect(); let mut fill: Vec> = vec![zero_sample(s); self.cc]; fill.push(max_sample(s)); let mut invert = vec![self.cmyk; self.cc]; invert.push(false); - let mut bytes = interleave(&refs, &fill, w * h, s, &invert); - if self.fmt.mode == ColorMode::Lab && s == SampleType::U16 { - crate::pixels::lab16_chroma(&mut bytes, self.cc + 1, true); + let mut surf = Surface::new(self.fmt); + for (b0, b1) in band_rows(r.top, h, w * (self.cc + 1) * s.bytes()) { + let planes: Vec>>> = + crate::pixels::par_map(decoders.iter().collect(), |d| d.as_ref().and_then(|d| d.as_ref().ok()).map(|d| d.rows(b0, b1))); + let mut refs: Vec> = Vec::with_capacity(planes.len()); + for (&id, p) in ids.iter().zip(&planes) { + match p { + Some(Err(e)) => { + self.channel_failed(name, id, e); + return Surface::new(self.fmt); + } + Some(Ok(p)) => refs.push(Some(p)), + None => refs.push(None), + } + } + let mut bytes = interleave(&refs, &fill, w * (b1 - b0), s, &invert); + if self.fmt.mode == ColorMode::Lab && s == SampleType::U16 { + crate::pixels::lab16_chroma(&mut bytes, self.cc + 1, true); + } + surf.write_interleaved(Rect::new(r.left, r.top + b0 as i32, r.right, r.top + b1 as i32), &bytes); } - let mut surf = Surface::from_interleaved(self.fmt, Rect::new(r.left, r.top, r.right, r.bottom), &bytes); surf.prune(); + // A white background, a solid fill's cache: one tile per value, shared across layers. + surf.share_uniform_tiles(&mut self.uniform); surf } + /// Restores the selected user mask from its channel and metadata, recording an + /// integrity failure if the mask channel cannot be decoded. fn record_mask(&mut self, rec: &LayerRecord, name: &str) -> Option { let m = rec.layer_mask()?; let (id, rect, default, flags) = match selected_real_mask(rec) { @@ -239,6 +343,7 @@ impl Ctx<'_> { let bytes = interleave(&[Some(&plane)], &[zero_sample(s)], w * h, s, &[false]); surface.write_interleaved(Rect::new(rect.left, rect.top, rect.right, rect.bottom), &bytes); surface.prune(); + surface.share_uniform_tiles(&mut self.uniform); } let params = m.parameters; Some(LayerMask { @@ -254,14 +359,19 @@ impl Ctx<'_> { /// document coordinates, white beyond its bounds (`filterMaskExtendWithWhite`). An all-white /// mask is no mask. fn filter_mask(&mut self, placed: &str, stack: &crate::smart_map::FilterStack, name: &str) -> Option { + if let Some(done) = self.filter_masks.get(placed) { + return done.clone(); + } let item = self.filter_effects.iter().find(|i| i.id == placed)?; - match crate::smart_map::mask_from_item(item, self.mask_fmt.sample, stack) { + let mask = match crate::smart_map::mask_from_item(item, self.mask_fmt.sample, stack) { Ok(m) => m, Err(e) => { self.warn(format!("layer \"{name}\": {e}; the filter mask was ignored")); None } - } + }; + self.filter_masks.insert(placed.to_string(), mask.clone()); + mask } fn apply_common(&mut self, l: &mut Layer, rec: &LayerRecord, blend_override: Option) { @@ -320,6 +430,19 @@ impl Ctx<'_> { // Blend If lives in the layer record's blending ranges. l.blend_if = crate::blocks::blend_if_from_ranges(&rec.blending_ranges); l.psd_id = rec.layer_id(); + // The layer's XMP packet (`shmd` › `cust` › `layerXMP`) becomes the field; the rest of + // `shmd` stays in `psd_blocks`, where export puts the packet back. + if let Some(i) = l.psd_blocks.iter().position(|(k, _)| k == b"shmd") + && let Some((xmp, rest)) = photocraft_psd::metadata::take_layer_xmp(&l.psd_blocks[i].1) + { + l.xmp = Some(Arc::from(xmp)); + match rest { + Some(data) => l.psd_blocks[i].1 = Arc::new(data), + None => { + l.psd_blocks.remove(i); + } + } + } let name = l.name.clone(); l.mask = self.record_mask(rec, &name); } @@ -354,87 +477,61 @@ impl Ctx<'_> { // Kept verbatim for saving, but it renders as nothing: say so rather than open the // layer silently without its effect (#1763). if matches!(adj, photocraft_doc::Adjustment::Unsupported { .. }) { - let why = adjust_map::unreadable_reason(k, &data).map(|r| format!(" ({r})")).unwrap_or_default(); + // Photoshop 2026's Light adjustment shares the Brightness/Contrast key (#6542). + let light = k == b"brit" && adjust_map::is_extended_brit(cged); + let why = if light { + " (Light adjustments, with Exposure, Highlights, Shadows, Whites and Blacks, aren't supported yet)".to_string() + } else { + adjust_map::unreadable_reason(k, &data).map(|r| format!(" ({r})")).unwrap_or_default() + }; self.warn(format!( "layer \"{name}\": its {} settings could not be read{why}; the layer is kept as saved but has no effect", - adjust_map::label(k) + if light { "Light" } else { adjust_map::label(k) } )); } LayerContent::Adjustment(adj) - } else if rec.block(b"TySh").is_some() { - // Typed model from TySh/EngineData (photocraft-text); Photoshop's pixels stay the cache. - let data = rec.block(b"TySh").map(|b| b.data.clone()).unwrap_or_default(); - let mut t = photocraft_text::psd::text_layer_from_tysh(&data, self.dpi).unwrap_or_else(|| { - let (text, transform) = blocks::parse_tysh(&data).unwrap_or_default(); - TextLayer { text, transform, ..Default::default() } - }); - if let Some(txt2) = &self.txt2 { - photocraft_text::psd::apply_txt2(&mut t, &data, txt2); - photocraft_text::psd::apply_txt2_frame(&mut t, &data, txt2); + } else if let Some(block) = rec.block(b"TySh") { + // Only canonical type data with a safe transform can become a text layer. A retained + // foreign-order block must never be handed to the big-endian reader (#4758). Canonical + // data the typed reader can't decode stays a type layer as before (usually without + // text), so its TySh is saved back verbatim instead of being dropped with the raster. + let parsed = (!block.is_foreign_order()) + .then(|| { + photocraft_text::psd::text_layer_from_tysh(&block.data, self.dpi).or_else(|| { + let (text, transform) = blocks::parse_tysh(&block.data)?; + photocraft_text::psd::transform_is_safe(&transform).then(|| TextLayer { text, transform, ..Default::default() }) + }) + }) + .flatten(); + if let Some(mut t) = parsed { + let mut path_error = None; + if let Some(txt2) = &self.txt2 { + photocraft_text::psd::apply_txt2(&mut t, &block.data, txt2); + // Type on a path keeps its path only in its `Txt2` frame (#7962). + path_error = photocraft_text::psd::apply_txt2_path(&mut t, &block.data, txt2).err(); + photocraft_text::psd::apply_txt2_frame(&mut t, &block.data, txt2); + } + if let Some(e) = path_error { + let shape = if matches!(t.shape, photocraft_doc::text::TextShape::Box { .. }) { "paragraph" } else { "point" }; + self.warn(format!("layer \"{name}\": its type path could not be read ({e}); it opens as {shape} text")); + } + // Photoshop's pixels stay until edited. Writers without type pixels are + // rendered by text_import::prepare instead of retaining an empty cache. + let cache = self.record_surface(rec, &name); + let drawn = !cache.content_bounds().is_empty() || photocraft_text::psd::engine_text_is_blank(&block.data); + t.cache = drawn.then_some(cache); + t.psd_raw = principal(b"TySh"); + LayerContent::Text(t) + } else { + self.warn(format!("layer \"{name}\": type data in another byte order or with unsafe type geometry; kept as pixels")); + LayerContent::Raster(self.record_surface(rec, &name)) } - // Photoshop's pixels are the cache only when the file has some: ag-psd, GIMP and - // other writers leave type layers without image data (Photoshop re-renders them on - // open), and an empty cache would show nothing until the layer is edited; the import - // renders such a layer from its model instead (`text_import::prepare`). When the - // engine text itself is blank, Photoshop draws nothing either: keep the empty pixels - // (corpus: text/path-wave-open.psd). - let cache = self.record_surface(rec, &name); - let drawn = !cache.content_bounds().is_empty() || photocraft_text::psd::engine_text_is_blank(&data); - t.cache = drawn.then_some(cache); - t.psd_raw = principal(b"TySh"); - LayerContent::Text(t) } else if let Some(k) = smart_key { - let Placement { id, transform, perspective, warp } = match rec.block(k) { - Some(b) if k == b"PlLd" => self.plld_placement(&b.data, &name), - Some(b) => { - let (id, transform) = blocks::parse_smart(k, &b.data); - // Distort / Perspective: the fourth corner (the affine `transform` drops it). - let perspective = blocks::parse_smart_perspective(k, &b.data); - Placement { id, transform, perspective, warp: blocks::parse_placed_warp(k, &b.data) } - } - None => Placement::default(), - }; - let contents_id = - if id.is_empty() { SmartContentsId::fresh() } else { *self.smart_contents.entry(id.clone()).or_insert_with(SmartContentsId::fresh) }; - // Smart filters (`filterFX` in the placed-layer data) and their mask (`FEid`). - let placed = rec.block(b"SoLd").or_else(|| rec.block(b"SoLE")).and_then(|b| crate::smart_map::parse_sold(&b.data)); - let stack = placed.as_ref().and_then(|p| p.stack.clone()).unwrap_or_default(); - let filter_mask = match &placed { - Some(p) if !stack.filters.is_empty() => self.filter_mask(&p.placed, &stack, &name), - _ => None, - }; - // Photoshop stores the filtered pixels without the filter mask and mixes the unfiltered - // ones (`FEid`) back in through it; our smart objects hold the mixed result. - let stored = self.record_surface(rec, &name); - let shown = stack.enabled && stack.filters.iter().any(|f| f.visible); - let cache = match (&placed, filter_mask.as_ref().filter(|m| m.enabled && shown)) { - (Some(p), Some(m)) => { - let unfiltered = self.filter_effects.iter().find(|i| i.id == p.placed).and_then(|i| crate::smart_map::unfiltered_from_item(i, self.fmt)); - match unfiltered { - Some(u) => photocraft_compose::filter_mask::mix(&u, &stored, m), - None => stored, - } - } - _ => stored, - }; - LayerContent::Smart(SmartObject { - contents_id, - source: SmartSource::Linked { path: id }, - transform, - smart_filters: stack.filters, - cache: Some(cache), - psd_raw: principal(k), - filters_enabled: stack.enabled, - filter_mask, - warp, - stack_mode: None, - perspective, - }) + self.smart_content(rec, &name, k, &principal) } else if let Some((f, soft)) = soft_shape_fill(rec, vector_key.is_some(), fill_key) { - // A shape whose vector mask has a density or feather is a fill layer seen through a - // soft vector mask: the fill shows beyond the path, which the stored pixels (the - // shape alone) lack. Import it as exactly that. A pattern fill keeps the stored - // pixels as its rendering (a soft mask's pixels lack the fill beyond the path). + // A pattern-filled shape is a pattern fill layer seen through its vector mask (soft + // or not). Unless that mask is soft, the stored pixels stay its rendering (a soft + // mask's pixels lack the fill beyond the path). if !soft && matches!(f, photocraft_doc::Fill::Pattern { .. }) && !rec.rect.is_empty() { fill_cache = Some(FillCache { fill: f.clone(), surface: self.record_surface(rec, &name) }); } @@ -444,9 +541,13 @@ impl Ctx<'_> { let mut sh = ShapeLayer { fill, cache: Some(self.record_surface(rec, &name)), psd_raw: vector_key.and_then(principal), ..Default::default() }; let lookup = |k: &[u8; 4]| rec.block(k).map(|b| b.data.clone()); crate::vector_map::shape_from_blocks(&mut sh, &lookup, self.file.header.width, self.file.header.height, self.dpi); + (sh.density, sh.feather) = shape_mask_params(rec); // No stored pixels (32-bit documents, files saved without layer pixels): render the - // shape from its path and fill, as Photoshop does when it opens the file. - if (rec.rect.is_empty() || rec.rect.size().is_err()) && (!sh.path.subpaths.is_empty() || sh.path.inverted) { + // shape from its path and fill, as Photoshop does when it opens the file. A soft + // shape's stored pixels hold the shape alone, without the fill its density and feather + // show beyond the path: render it too. + let soft = sh.has_soft_mask() && sh.fill.is_some(); + if soft || ((rec.rect.is_empty() || rec.rect.size().is_err()) && (!sh.path.subpaths.is_empty() || sh.path.inverted)) { let canvas = Rect::new(0, 0, self.file.header.width as i32, self.file.header.height as i32); sh.cache = Some(photocraft_vector::render_shape(&sh, self.fmt, canvas)); } @@ -465,6 +566,15 @@ impl Ctx<'_> { LayerContent::Raster(self.record_surface(rec, &name)) } } + } else if let Some(k) = unknown_adjustment_key(rec) { + // An adjustment type no mapper knows: kept verbatim like an unreadable one, not + // opened as an empty pixel layer without a word (#6575). + let key = String::from_utf8_lossy(&k).into_owned(); + self.warn(format!( + "layer \"{name}\": its adjustment settings could not be read (unknown adjustment type \"{key}\"); the layer is kept as saved but has no effect" + )); + let raw = rec.block(&k).map(|b| b.data.as_slice()).unwrap_or_default().into(); + LayerContent::Adjustment(photocraft_doc::Adjustment::Unsupported { psd_key: key, raw }) } else { LayerContent::Raster(self.record_surface(rec, &name)) }; @@ -485,6 +595,71 @@ impl Ctx<'_> { l } + fn smart_content(&mut self, rec: &LayerRecord, name: &str, k: &[u8; 4], principal: &impl Fn(&[u8; 4]) -> Option>>) -> LayerContent { + let Placement { id, transform, perspective, warp } = match rec.block(k) { + Some(b) if k == b"PlLd" => self.plld_placement(&b.data, name), + Some(b) => { + let (id, transform) = blocks::parse_smart(k, &b.data); + // Distort / Perspective: the fourth corner (the affine `transform` drops it). + let perspective = blocks::parse_smart_perspective(k, &b.data); + Placement { id, transform, perspective, warp: blocks::parse_placed_warp(k, &b.data) } + } + None => Placement::default(), + }; + let contents_id = if id.is_empty() { SmartContentsId::fresh() } else { *self.smart_contents.entry(id.clone()).or_insert_with(SmartContentsId::fresh) }; + // Smart filters (`filterFX` in the placed-layer data) and their mask (`FEid`). + let placed = rec.block(b"SoLd").or_else(|| rec.block(b"SoLE")).and_then(|b| crate::smart_map::parse_sold(&b.data)); + let stack = placed.as_ref().and_then(|p| p.stack.clone()).unwrap_or_default(); + let filter_mask = match &placed { + Some(p) if !stack.filters.is_empty() => self.filter_mask(&p.placed, &stack, name), + _ => None, + }; + // Photoshop stores the filtered pixels without the filter mask and mixes the unfiltered + // ones (`FEid`) back in through it; our smart objects hold the mixed result. + let stored = self.record_surface(rec, name); + let shown = stack.enabled && stack.filters.iter().any(|f| f.visible); + let cache = match (&placed, filter_mask.as_ref().filter(|m| m.enabled && shown)) { + (Some(p), Some(m)) => { + let unfiltered = match self.unfiltered.get(&p.placed) { + Some(done) => done.clone(), + None => { + // Every distinct source is kept until the import ends, so what they add up to is + // bounded: past the limit the layer keeps its stored pixels, unmixed. + let item = self.filter_effects.iter().find(|i| i.id == p.placed); + let cost = item + .and_then(|i| i.rect.size().ok()) + .map_or(0, |(w, h)| (w as u64).saturating_mul(h as u64).saturating_mul(self.fmt.bytes_per_pixel() as u64)); + let fits = self.unfiltered_bytes.saturating_add(cost) <= MAX_UNFILTERED_BYTES; + let made = item.filter(|_| fits).and_then(|i| crate::smart_map::unfiltered_from_item(i, self.fmt)); + if made.is_some() { + self.unfiltered_bytes += cost; + } + self.unfiltered.insert(p.placed.clone(), made.clone()); + made + } + }; + match unfiltered { + Some(u) => photocraft_compose::filter_mask::mix(&u, &stored, m), + None => stored, + } + } + _ => stored, + }; + LayerContent::Smart(SmartObject { + contents_id, + source: SmartSource::Linked { path: id }, + transform, + smart_filters: stack.filters, + cache: Some(cache), + psd_raw: principal(k), + filters_enabled: stack.enabled, + filter_mask, + warp, + stack_mode: None, + perspective, + }) + } + /// Typed vector mask from `vsms`/`vmsk` (non-shape layers). A user mask that Photoshop /// rendered from the vector data (flag bit 3, no separate real mask) is dropped: the /// compositor rasterizes the vector mask itself. @@ -535,7 +710,9 @@ impl Ctx<'_> { let sd = rec.section_divider(); let expanded = sd.is_none_or(|s| s.kind != photocraft_psd::SectionType::ClosedFolder); let artboard = crate::comps_map::ARTBOARD_KEYS.iter().find_map(|k| rec.block(k)).and_then(|b| crate::comps_map::parse_artboard(&b.data)); - let mut l = Layer::new(rec.name(), LayerContent::Group(Group { children, expanded, artboard })); + // A Photoshop Frame's stroke: `vstk` on the frame group, in a shape's layout. + let frame_stroke = rec.block(b"vstk").and_then(|b| crate::vector_map::parse_vstk(&b.data, self.dpi)).and_then(|v| v.stroke); + let mut l = Layer::new(rec.name(), LayerContent::Group(Group { children, expanded, artboard, frame_stroke })); l.psd_blocks = preserved_blocks(rec); self.apply_common(&mut l, rec, sd.and_then(|s| s.blend_mode)); self.apply_vector_mask(&mut l, rec); @@ -595,6 +772,36 @@ pub(crate) fn group_depth(file: &PsdFile) -> usize { depth } +/// Additional-layer-information keys (Adobe PSD spec, plus `cust`, `vowv` and `SoLE` seen in +/// Photoshop's files) that are not an adjustment of their own: names, ids, locks, effects, +/// masks, metadata, and the fill, type, placed and shape content read before. +/// Most pixel bytes the smart-filter `FEid` sources may keep decoded across one import. +const MAX_UNFILTERED_BYTES: u64 = 1 << 30; + +const LAYER_INFO_KEYS: [&[u8; 4]; 50] = [ + b"luni", b"lyid", b"lnsr", b"lsct", b"lsdk", b"lspf", b"lclr", b"iOpa", b"clbl", b"infx", b"knko", b"tsly", b"lmgm", b"vmgm", b"brst", b"shmd", b"fxrp", + b"lfx2", b"lfxs", b"lmfx", b"lrFX", b"lyvr", b"sn2P", b"cust", b"CgEd", b"vmsk", b"vsms", b"vogk", b"vowv", b"vstk", b"vscg", b"artb", b"artd", b"abdd", + b"FMsk", b"PxSc", b"PxSD", b"cinf", b"ffxi", b"shpa", b"anFX", b"SoCo", b"GdFl", b"PtFl", b"TySh", b"tySh", b"SoLd", b"SoLE", b"PlLd", b"plLd", +]; + +/// The key of an adjustment layer whose type no mapper knows (#6575), e.g. Photoshop 4's +/// `hue ` or a type newer than PhotoCraft. Photoshop writes every adjustment layer without +/// pixels and with "pixel data irrelevant" set (every one in the corpus; pixel, type and smart +/// layers never have it). Such a record that is not a group and carries exactly one block that +/// is neither a known layer-information key nor a known adjustment is that adjustment. More than +/// one candidate is a guess, so the layer stays an (empty) pixel layer, as before; either way no +/// pixels can be lost and the block is saved back verbatim. +fn unknown_adjustment_key(rec: &LayerRecord) -> Option<[u8; 4]> { + let marked = rec.flags.bit4_useful() && rec.flags.pixel_data_irrelevant(); + if !marked || !rec.rect.is_empty() || rec.block(b"lsct").is_some() || rec.block(b"lsdk").is_some() { + return None; + } + let mut unknown = rec.blocks.iter().filter(|b| !LAYER_INFO_KEYS.contains(&&b.key) && !ADJUSTMENT_KEYS.contains(&&b.key)); + let only = unknown.next()?; + // A key that isn't four printable ASCII bytes names no adjustment (nor saves back as one). + (unknown.next().is_none() && only.key.iter().all(|c| c.is_ascii_graphic() || *c == b' ')).then_some(only.key) +} + fn preserved_blocks(rec: &LayerRecord) -> Vec<([u8; 4], Arc>)> { rec.blocks.iter().filter(|b| !REGENERATED.contains(&&b.key)).map(|b| (b.key, Arc::new(b.data.clone()))).collect() } @@ -603,7 +810,7 @@ fn be_to_ne_plane(plane: &[u8], s: SampleType, n: usize) -> Vec { interleave(&[Some(plane)], &[zero_sample(s)], n, s, &[false]) } -fn unicode_names(data: &[u8]) -> Vec { +pub(crate) fn unicode_names(data: &[u8]) -> Vec { let mut out = Vec::new(); let mut at = 0; while at + 4 <= data.len() { @@ -686,7 +893,7 @@ fn write_rgba8(s: &mut Surface, canvas: Rect, data: &[u8]) { } } -fn parse_guides(data: &[u8], doc: &mut Document) { +pub(crate) fn parse_guides(data: &[u8], doc: &mut Document) { let rd = |at: usize| data.get(at..at + 4).map(|b| u32::from_be_bytes([b[0], b[1], b[2], b[3]])); let Some(count) = rd(12) else { return }; for i in 0..count as usize { @@ -742,6 +949,7 @@ fn mark_background(file: &PsdFile, layers: &mut [Layer]) { } } +/// Converts a parsed PSD into a document, preserving best-effort recovery warnings. pub fn psd_to_document(file: &PsdFile) -> (Document, Vec) { // Never cancelled, so always `Some`; the fallback is unreachable. psd_to_document_with(file, &photocraft_raster::Interrupt::NONE) @@ -754,6 +962,91 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt) psd_to_document_decoded(file, ctl, None) } +/// [`psd_to_document_decoded_integrity`] taking the file: its global blocks move into the +/// document rather than being copied (an embedded smart object's `lnk2` block can be most of a +/// file). The file's channel data may still be in its source ([`PsdFile::from_source`]): the +/// fourth value is a failure to read it, which fails the open (a layer is never left empty +/// because its file became unreadable while it opened). +pub(crate) fn psd_to_document_owned( + mut file: PsdFile, + ctl: &photocraft_raster::Interrupt, + decoded: Option>>, +) -> Option<(Document, Vec, bool, Option)> { + let global = file + .global_blocks + .iter_mut() + .map(|b| { + // A layer block is still read by `unreadable_layer_block` (to say why it failed). + let data = if matches!(&b.key, b"Lr16" | b"Lr32" | b"Layr") { b.data.clone() } else { std::mem::take(&mut b.data) }; + (b.signature, b.key, Arc::new(data)) + }) + .collect(); + psd_to_document_global(&file, ctl, decoded, global) +} + +/// Whether `file` stores spot planes the way earlier PhotoCraft versions (releases 0.1.0 to +/// 0.16.0) wrote them: straight (the maximum = solid ink), the opposite of Photoshop, which +/// stores 0 = solid ink in every PSD and TIFF it saves. Their files are recognised by three +/// things together: +/// +/// - DisplayInfo (1077) marks at least one spot channel; +/// - there is no Alpha Identifiers resource (1053), or one that lists fewer channels than the +/// file has ([`photoshop_identified_channels`]). Photoshop has written it, one identifier per +/// extra channel, in every file with an extra channel since version 6, and PhotoCraft writes it +/// now. No earlier PhotoCraft wrote one of its own, but each kept the 1053 of a Photoshop file +/// it opened (a transparent document's `[0]`, for one) and wrote it back unchanged, after the +/// channels added in it, which it stored straight; +/// - the version info (1057) is byte for byte what those versions wrote: writer "Adobe Photoshop" +/// with reader "Adobe Photoshop CS6", both NUL-terminated (0.1.0 to 0.7.0), or reader "Adobe +/// Photoshop 2026" without a NUL (0.8.2 on), with either merged-data flag. +/// +/// Any other file is read in Photoshop's polarity. psd-tools' `cmyk-spot.psd`, for one, also has +/// spots and no 1053, but names psd-tools in 1057 and stores Photoshop's polarity. A Photoshop +/// file that an earlier PhotoCraft opened and saved again kept Photoshop's 1053 and its planes as +/// Photoshop stored them, so it is read in Photoshop's polarity too; when channels were added +/// there, only the planes after the ones the kept 1053 lists are read straight. +/// +/// Keeping these files' ink is the only reason this exists: deleting it (and its one caller) +/// reads every file in Photoshop's polarity, at the cost of inverting the spot ink of PSDs saved +/// by those versions. +pub(crate) fn earlier_photocraft_spot_planes(file: &PsdFile) -> bool { + use photocraft_psd::descriptor::UnicodeString; + use photocraft_psd::resources::VersionInfo; + + let marks_spot = file.resource(crate::channel_map::DISPLAY_INFO).is_some_and(|r| crate::channel_map::display_info_marks_spot(&r.data)); + let identifies_every_channel = match file.header.color_mode.color_channels() { + Some(color) => { + let channels = file.header.channels.saturating_sub(color).saturating_sub(u16::from(file.merged_has_alpha())); + photoshop_identified_channels(file) >= usize::from(channels) + } + // Multichannel has no fixed number of colour channels to count from. + None => file.resource(crate::channel_map::ALPHA_IDENTIFIERS).is_some(), + }; + if !marks_spot || identifies_every_channel { + return false; + } + let Some(r) = file.resource(ids::VERSION_INFO) else { return false }; + // Byte for byte: a resource with anything after the fields is someone else's. + let Ok(v) = VersionInfo::from_bytes(&r.data) else { return false }; + if v.to_bytes() != r.data { + return false; + } + let writers = [ + (UnicodeString::new_nul("Adobe Photoshop"), UnicodeString::new_nul("Adobe Photoshop CS6")), + (UnicodeString::new("Adobe Photoshop"), UnicodeString::new("Adobe Photoshop 2026")), + ]; + v.version == 1 && v.file_version == 1 && writers.iter().any(|(writer, reader)| v.writer == *writer && v.reader == *reader) +} + +/// How many extra channels, after the merged transparency, the Alpha Identifiers resource (1053) +/// lists: its identifiers other than 0, which Photoshop gives the merged transparency (first, and +/// in some files with no transparency plane at all). 0 without a 1053. In a file from an earlier +/// PhotoCraft ([`earlier_photocraft_spot_planes`]) these are the channels of the Photoshop file +/// it opened, stored as Photoshop stored them; the channels after them were added there. +pub(crate) fn photoshop_identified_channels(file: &PsdFile) -> usize { + file.resource(crate::channel_map::ALPHA_IDENTIFIERS).map_or(0, |r| r.data.as_chunks::<4>().0.iter().filter(|id| **id != [0; 4]).count()) +} + /// Whether the import reads the merged composite: for a flattened or multichannel file (it *is* /// the image), a file without layers, or extra alpha/spot channels behind the colour ones. pub(crate) fn merged_needed(file: &PsdFile) -> bool { @@ -770,6 +1063,77 @@ pub(crate) fn can_recover_layers(file: &PsdFile) -> bool { matches!(file.header.color_mode, PsdMode::Grayscale | PsdMode::Rgb | PsdMode::Cmyk | PsdMode::Lab) && file.header.depth != 1 && !file.layers().is_empty() } +fn read_initial_metadata(file: &PsdFile, global: &[PsdGlobalBlock], doc: &mut Document, layered: bool, multichannel: bool, warnings: &mut Vec) -> bool { + let header = &file.header; + for resource in &file.resources { + match resource.id { + ids::RESOLUTION_INFO => { + if let Ok(info) = photocraft_psd::ResolutionInfo::from_bytes(&resource.data) { + let (x, y) = (info.h_res(), info.v_res()); + doc.resolution_dpi = x as f32; + warnings.extend(crate::unequal_resolution_warning(x, y)); + } + } + ids::ICC_PROFILE => doc.icc_profile = Some(Arc::new(resource.data.clone())), + ids::XMP => doc.metadata.xmp = Some(String::from_utf8_lossy(&resource.data).into()), + ids::EXIF => doc.metadata.exif = Some(Arc::new(resource.data.clone())), + 1032 => parse_guides(&resource.data, doc), + ids::GLOBAL_ANGLE | ids::GLOBAL_ALTITUDE => { + if let Some(bytes) = resource.data.get(..4) { + let value = i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) as f32; + if resource.id == ids::GLOBAL_ANGLE { + doc.global_light.angle = value; + } else { + doc.global_light.altitude = value; + } + } + } + id if crate::vector_map::SAVED_PATHS.contains(&id) => match crate::vector_map::path_from_resource(&resource.data, header.width, header.height) { + Some(path) => doc.paths.push(photocraft_doc::NamedPath { + name: String::from_utf8_lossy(&resource.name).into_owned(), + path, + psd_raw: Some(Arc::new(resource.data.clone())), + }), + None => doc.metadata.psd_resources.push((id, String::from_utf8_lossy(&resource.name).into_owned(), Arc::new(resource.data.clone()))), + }, + crate::vector_map::WORK_PATH => doc.work_path = crate::vector_map::path_from_resource(&resource.data, header.width, header.height), + crate::vector_map::CLIPPING_PATH => { + let len = usize::from(resource.data.first().copied().unwrap_or(0)); + let name = resource.data.get(1..1 + len).map(|bytes| String::from_utf8_lossy(bytes).into_owned()).unwrap_or_default(); + doc.clipping_path = Some(photocraft_doc::ClippingPath { name, flatness: 0.0 }); + doc.metadata.psd_resources.push((resource.id, String::from_utf8_lossy(&resource.name).into_owned(), Arc::new(resource.data.clone()))); + } + crate::channel_map::ALPHA_IDENTIFIERS if !multichannel => {} + id if MAPPED_RESOURCES.contains(&id) => {} + id => doc.metadata.psd_resources.push((id, String::from_utf8_lossy(&resource.name).into_owned(), Arc::new(resource.data.clone()))), + } + } + crate::guides_map::apply_colors(doc); + doc.metadata.psd_global_blocks.extend(global.iter().cloned()); + // A 16/32-bit file whose layer block can't be parsed would otherwise open as a flattened + // file without a word (#2700). The raw block is kept, but a PSD save writes the document's + // own layers in its place (see `psd_export`). + let mut damaged = false; + if let Some((block, error)) = file.unreadable_layer_block() { + damaged = true; + warnings.push(format!( + "the layers ({} block) could not be read ({error}); only the merged image was opened. Saving as PSD replaces the unreadable layer data", + block.key_str() + )); + } + doc.patterns = crate::pattern_map::from_global_blocks(doc); + warnings.extend(crate::pattern_map::unpaintable_warnings(&doc.patterns)); + doc.notes = crate::annotations_map::notes_from_blocks(doc); + if crate::annotations_map::anno_unreadable(doc) { + warnings.push("the notes (Anno block) could not be read; they are kept unchanged on PSD save unless a note is added".into()); + } + if let Some(scale) = crate::annotations_map::raw_scale(doc) { + doc.measurement.scale = scale; + } + let _ = layered; + damaged +} + /// [`psd_to_document_with`] with the merged image already decoded (by /// [`PsdFile::from_bytes_and_merged`] with [`merged_needed`]), so it is not decoded again. pub(crate) fn psd_to_document_decoded( @@ -777,113 +1141,69 @@ pub(crate) fn psd_to_document_decoded( ctl: &photocraft_raster::Interrupt, decoded: Option>>, ) -> Option<(Document, Vec)> { + psd_to_document_decoded_integrity(file, ctl, decoded).map(|(document, warnings, _)| (document, warnings)) +} + +/// Also reports whether unrecoverable channel, layer or merged-image data was discarded. +/// Interactive opens use this to ask whether to continue. Both ordinary imports and +/// the low-level conversion API retain their existing best-effort behaviour. +pub(crate) fn psd_to_document_decoded_integrity( + file: &PsdFile, + ctl: &photocraft_raster::Interrupt, + decoded: Option>>, +) -> Option<(Document, Vec, bool)> { + let global = file.global_blocks.iter().map(|b| (b.signature, b.key, Arc::new(b.data.clone()))).collect(); + psd_to_document_global(file, ctl, decoded, global).map(|(doc, warnings, damaged, _)| (doc, warnings, damaged)) +} + +fn psd_to_document_global( + file: &PsdFile, + ctl: &photocraft_raster::Interrupt, + decoded: Option>>, + global: Vec, +) -> Option<(Document, Vec, bool, Option)> { + let (mode, layered, multichannel, depth) = import_format(file); let h = &file.header; let mut warnings = Vec::new(); - let mode = doc_mode(h.color_mode).unwrap_or_else(|| { + if doc_mode(h.color_mode).is_none() { warnings.push(format!("unknown color mode {}; importing as RGB", h.color_mode.as_u16())); - ColorMode::Rgb - }); - let layered = matches!(mode, ColorMode::Grayscale | ColorMode::Rgb | ColorMode::Cmyk | ColorMode::Lab) && h.depth != 1; - let multichannel = mode == ColorMode::Multichannel && h.depth != 1; - let depth = if layered || multichannel { sample_for_depth(h.depth) } else { SampleType::U8 }; + } let mut doc = Document::new("Untitled", Size::new(h.width, h.height), mode, depth); - - // Resources. - for r in &file.resources { - match r.id { - ids::RESOLUTION_INFO => { - if let Ok(ri) = photocraft_psd::ResolutionInfo::from_bytes(&r.data) { - // Fixed 16.16 hRes/vRes are pixels per inch. The unit shorts only - // say how to show that number (1 = px/in, 2 = px/cm). - let (x, y) = (ri.h_res(), ri.v_res()); - doc.resolution_dpi = x as f32; - warnings.extend(crate::unequal_resolution_warning(x, y)); - } - } - ids::ICC_PROFILE => doc.icc_profile = Some(Arc::new(r.data.clone())), - ids::XMP => doc.metadata.xmp = Some(String::from_utf8_lossy(&r.data).into_owned()), - ids::EXIF => doc.metadata.exif = Some(Arc::new(r.data.clone())), - 1032 => parse_guides(&r.data, &mut doc), - ids::GLOBAL_ANGLE | ids::GLOBAL_ALTITUDE => { - if let Some(b) = r.data.get(..4) { - let v = i32::from_be_bytes([b[0], b[1], b[2], b[3]]) as f32; - if r.id == ids::GLOBAL_ANGLE { - doc.global_light.angle = v; - } else { - doc.global_light.altitude = v; - } - } - } - id if crate::vector_map::SAVED_PATHS.contains(&id) => match crate::vector_map::path_from_resource(&r.data, h.width, h.height) { - Some(path) => doc.paths.push(photocraft_doc::NamedPath { - name: String::from_utf8_lossy(&r.name).into_owned(), - path, - psd_raw: Some(Arc::new(r.data.clone())), - }), - None => doc.metadata.psd_resources.push((id, String::from_utf8_lossy(&r.name).into_owned(), Arc::new(r.data.clone()))), - }, - crate::vector_map::WORK_PATH => { - doc.work_path = crate::vector_map::path_from_resource(&r.data, h.width, h.height); - } - crate::vector_map::CLIPPING_PATH => { - // Kept raw (layout beyond the name is not modelled); the name is exposed. - let n = usize::from(r.data.first().copied().unwrap_or(0)); - let name = r.data.get(1..1 + n).map(|b| String::from_utf8_lossy(b).into_owned()).unwrap_or_default(); - doc.clipping_path = Some(photocraft_doc::ClippingPath { name, flatness: 0.0 }); - doc.metadata.psd_resources.push((r.id, String::from_utf8_lossy(&r.name).into_owned(), Arc::new(r.data.clone()))); - } - id if MAPPED_RESOURCES.contains(&id) => {} - id => doc.metadata.psd_resources.push((id, String::from_utf8_lossy(&r.name).into_owned(), Arc::new(r.data.clone()))), - } - } - for b in &file.global_blocks { - doc.metadata.psd_global_blocks.push((b.signature, b.key, Arc::new(b.data.clone()))); - } - // A 16/32-bit file whose layer block can't be parsed would otherwise open as a flattened - // file without a word (#2700). The raw block is kept, but a PSD save writes the document's - // own layers in its place (see `psd_export`). - if let Some((b, e)) = file.unreadable_layer_block() { - warnings.push(format!( - "the layers ({} block) could not be read ({e}); only the merged image was opened. Saving as PSD replaces the unreadable layer data", - b.key_str() - )); - } - doc.patterns = crate::pattern_map::from_global_blocks(&doc); - // Notes (`Anno`) and the measurement scale (resource 1074); raw data stays for verbatim export. - doc.notes = crate::annotations_map::notes_from_blocks(&doc); - if crate::annotations_map::anno_unreadable(&doc) { - warnings.push("the notes (Anno block) could not be read; they are kept unchanged on PSD save unless a note is added".into()); - } - if let Some(scale) = crate::annotations_map::raw_scale(&doc) { - doc.measurement.scale = scale; - } + let damaged = read_initial_metadata(file, &global, &mut doc, layered, multichannel, &mut warnings); let fmt = doc.pixel_format(); let cc = fmt.mode.color_channels(); let mut cx = Ctx { file, + global: global.clone(), fmt, mask_fmt: PixelFormat::new(ColorMode::Grayscale, depth, false), cc, cmyk: fmt.mode == ColorMode::Cmyk, warnings, + damaged, + read_error: None, dpi: doc.resolution_dpi, - txt2: file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data)), + txt2: global.iter().find(|(_, k, _)| k == b"Txt2").and_then(|(_, _, d)| photocraft_text::psd::parse_txt2(d)), filter_effects: Vec::new(), + filter_masks: Default::default(), + unfiltered: Default::default(), + unfiltered_bytes: 0, smart_contents: Default::default(), plld_sizes: Default::default(), ctl: *ctl, done: 0, + uniform: Default::default(), total: file.layers().len(), }; - for b in file.global_blocks.iter().filter(|b| matches!(&b.key, b"FEid" | b"FXid")) { + for (_, key, data) in cx.global.clone().iter().filter(|(_, key, _)| matches!(key, b"FEid" | b"FXid")) { // Smart-filter caches can be large: a cancelled open stops between blocks. if ctl.cancelled() { return None; } - match photocraft_psd::filter_effects::FilterEffects::parse(&b.data) { + match photocraft_psd::filter_effects::FilterEffects::parse(data) { Ok(fx) => cx.filter_effects.extend(fx.items), - Err(e) => cx.warn(format!("smart filter masks ({}) could not be read: {e}", b.key_str())), + Err(e) => cx.warn(format!("smart filter masks ({}) could not be read: {e}", String::from_utf8_lossy(key))), } } if ctl.cancelled() { @@ -895,234 +1215,240 @@ pub(crate) fn psd_to_document_decoded( // budget while `w * hh` does not fit a 32-bit `usize` (wasm32). let sizes = w.checked_mul(hh).zip(h.row_bytes().checked_mul(hh)); let canvas = Rect::new(0, 0, h.width as i32, h.height as i32); - // The merged composite is decoded only when something consumes it: a flattened or - // multichannel file (it *is* the image), or extra alpha/spot channels behind the colour - // ones. A normal layered Photoshop save carries a merged composite nobody reads - roughly - // half the file's bytes, decoded and thrown away on every open before this. - let merged = if merged_needed(file) { - let m = decoded.unwrap_or_else(|| file.decode_merged()); - if ctl.cancelled() { - return None; - } - ctl.progress(0.1); - if let Err(e) = &m { - cx.warn(format!("merged image could not be decoded: {e}")); - if can_recover_layers(file) { - cx.warn("extra alpha/spot channels in the merged image could not be recovered"); - } - } - Some(m) - } else { - if let Some(Err(e)) = decoded { - cx.warn(format!("merged image could not be decoded; opened the available layers instead: {e}")); - } - None - }; + let merged = decode_merged(file, decoded, ctl, &mut cx)?; + import_decoded_content(DecodedImport { file, merged: &merged, sizes, canvas, layered, multichannel, depth, fmt }, &mut doc, &mut cx); + finish_import(doc, cx, ctl) +} - if multichannel { - // Ink channels only (no layers); see `multichannel_map`. - if let Some(Ok(all)) = &merged { - let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default(); - crate::multichannel_map::import(file, all, &mut doc, &names, &mut cx.warnings); - } - if !file.layers().is_empty() { - cx.warn(format!("Multichannel documents have no layers: {} layer records were not imported", file.layers().len())); - } - } else if layered { - // Capped like `build` (#2940): a group past the document's nesting cap keeps no children, - // so no file can make this tree, or anything walking or dropping it, recurse deeper. - let tree = file.layer_tree_max_depth(photocraft_doc::MAX_GROUP_DEPTH); - // Colours stored in the document's CMYK space (Gradient Map stops…) convert through - // its own profile, as the compositor and the PSD writer read them. - let space = photocraft_compose::cmyk_space(&doc); - doc.layers = photocraft_color::convert::with_cmyk_space(space.as_ref(), || cx.build(&tree, 0)); - // Photoshop's Background: the bottom record stored without a transparency channel (-1), - // locked whatever its (localized) name and whether or not the file carries an `lspf` - // block. Its name is kept. - mark_background(file, &mut doc.layers); - // Layer › Link Layers: resource 1026 holds one group id per layer record (0 = unlinked). - if let Some(Ok(photocraft_psd::resources::ResourceData::LayerGroupInfo(groups))) = - file.resources.iter().find(|r| r.id == ids::LAYER_GROUP_INFO).and_then(photocraft_psd::resources::ImageResource::parsed) - { - apply_link_groups(&tree, &mut doc.layers, &groups); - } - if doc.layers.is_empty() - && let Some(Ok(all)) = &merged - // Always `Some` here: a decoded 8/16/32-bit plane holds at least `w * hh` bytes. - && let Some((n, plane)) = sizes - { - // Flattened file: the merged image becomes the background layer. - let alpha_idx = if file.merged_has_alpha() { Some(cc) } else { None }; - let mut planes: Vec> = (0..cc).map(|c| merged_plane(all, c, plane)).collect(); - planes.push(alpha_idx.and_then(|a| merged_plane(all, a, plane))); - let mut fill = vec![zero_sample(depth); cc]; - fill.push(max_sample(depth)); - let mut inv = vec![cx.cmyk; cc]; - inv.push(false); - let mut bytes = interleave(&planes, &fill, n, depth, &inv); - if fmt.mode == ColorMode::Lab && depth == SampleType::U16 { - crate::pixels::lab16_chroma(&mut bytes, cc + 1, true); - } - let mut s = Surface::from_interleaved(fmt, canvas, &bytes); - if alpha_idx.is_some() { - // Undo Photoshop's white matting of the merged image. - // A band of tile rows at a time (no full-size float copy of the image). - let white = photocraft_raster::from_rgba(&fmt, [1.0, 1.0, 1.0, 1.0]); - let mut vals = Vec::new(); - let mut y = canvas.y0; - while y < canvas.y1 { - let band = Rect::new(canvas.x0, y, canvas.x1, y.saturating_add(TILE_SIZE).min(canvas.y1)); - s.read_region_into(band, &mut vals); - for px in vals.chunks_exact_mut(cc + 1) { - let a = px[cc]; - for c in 0..cc { - px[c] = if a <= 0.0 { 0.0 } else { crate::pixels::unmatte(px[c], a, white[c]) }; - } - } - s.write_region(band, &vals); - y = band.y1; - } - } - s.prune(); - let mut bg = Layer::new("Background", LayerContent::Raster(s)); - if alpha_idx.is_none() { - bg.locks.transparency = true; - bg.locks.position = true; - } - doc.layers.push(bg); - } - } else { - if !file.layers().is_empty() { - cx.warn(format!("{:?} documents are imported flattened: {} layer records were not imported", h.color_mode, file.layers().len())); - } else { - cx.warn(format!("{:?} {}-bit document converted to {:?} 8-bit for editing", h.color_mode, h.depth, fmt.mode)); - } - if h.color_mode == PsdMode::Indexed && file.color_mode_data.len() >= 768 { - // Planar palette: 256 reds, 256 greens, 256 blues (the Color Table). - let m = &file.color_mode_data; - let colors = (0..256).map(|i| [m[i], m[256 + i], m[512 + i]]).collect(); - doc.color_table = Some(photocraft_doc::ColorTable { colors, transparent: None }); - } else if h.color_mode == PsdMode::Duotone && !file.color_mode_data.is_empty() { - // The duotone ink block is undocumented: keep it raw; the image shows as its gray plate. - doc.duotone = - Some(photocraft_doc::Duotone { inks: vec![photocraft_doc::DuotoneInk::new("Black", [0.0; 3])], psd_raw: Some(file.color_mode_data.clone()) }); - cx.warn("duotone inks are not interpreted (shown as grayscale)"); - } else if !file.color_mode_data.is_empty() { - cx.warn("color mode data is not preserved"); - } - match flat_composite(file, merged.as_ref(), photocraft_psd::compression::MAX_DECODED_BYTES) { - Ok(img) => { - let mut s = Surface::new(fmt); - write_rgba8(&mut s, canvas, &img.data); - drop(img); - s.prune(); - doc.layers.push(flat_background(s)); - } - // A merged image that failed to decode was reported above. - Err(_) if matches!(merged, Some(Err(_))) => {} - Err(e) => cx.warn(format!("the image could not be read: {e}")), - } - } - - // Extra (alpha / spot) channels of the merged image. - if layered - && let Some(Ok(all)) = &merged - && let Some((n, plane)) = sizes - { - let first = cc + usize::from(file.merged_has_alpha()); - let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default(); - // Photoshop lists every extra channel, the merged transparency first when there is one. - let skip = usize::from(file.merged_has_alpha() && names.len() == usize::from(h.channels).saturating_sub(cc)); - let names = names.get(skip..).unwrap_or_default(); - for (k, idx) in (first..usize::from(h.channels)).enumerate() { - let Some(p) = merged_plane(all, idx, plane) else { break }; - let mut s = Surface::new(cx.mask_fmt); - s.write_interleaved(canvas, &be_to_ne_plane(p, depth, n)); - s.prune(); - let name = names.get(k).cloned().unwrap_or_else(|| format!("Alpha {}", k + 1)); - doc.channels.push(AlphaChannel::new(name, s)); - } - // Channel options (overlay colour, opacity, colour indicates) and spot inks. - if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO) { - crate::channel_map::apply_display_info(&r.data, true, skip, &mut doc.channels); - } else if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO_OLD) { - crate::channel_map::apply_display_info(&r.data, false, skip, &mut doc.channels); - } - // Saved in Quick Mask mode: that channel becomes the document's Quick Mask again. - if let Some(r) = file.resource(crate::channel_map::QUICK_MASK_INFO) - && r.data.len() >= 2 - && let Some(k) = usize::from(u16::from_be_bytes([r.data[0], r.data[1]])).checked_sub(first) - && k < doc.channels.len() - { - doc.quick_mask = Some(doc.channels.remove(k)); - } - } - - // Layer comps (resource 1065 + per-layer `cmls`); the raw data stays for verbatim export. - let raw_comps = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == crate::comps_map::LAYER_COMPS).map(|(_, _, d)| d.clone()); - let comp_warnings; - (doc.layer_comps, doc.last_applied_comp, doc.last_document_state, comp_warnings) = - crate::comps_map::decode_comps(raw_comps.as_deref().map(Vec::as_slice), &doc); - cx.warnings.extend(comp_warnings); - // Slices (resource 1050), after layer ids are known; the raw data stays for verbatim export. +fn finish_import(mut doc: Document, mut cx: Ctx<'_>, ctl: &photocraft_raster::Interrupt) -> Option<(Document, Vec, bool, Option)> { + let raw_comps = doc.metadata.psd_resources.iter().find(|(id, _, _)| *id == crate::comps_map::LAYER_COMPS).map(|(_, _, data)| data.clone()); + let (comps, applied, state, warnings) = crate::comps_map::decode_comps(raw_comps.as_deref().map(Vec::as_slice), &doc); + (doc.layer_comps, doc.last_applied_comp, doc.last_document_state) = (comps, applied, state); + cx.warnings.extend(warnings); crate::slices_map::import(&mut doc); - // Character and paragraph styles from the type layers' engine data. cx.warnings.extend(crate::text_styles_map::import(&mut doc)); - if ctl.cancelled() { return None; } ctl.progress(1.0); - Some((doc, cx.warnings)) + Some((doc, cx.warnings, cx.damaged, cx.read_error)) +} + +fn decode_merged( + file: &PsdFile, + decoded: Option>>, + ctl: &photocraft_raster::Interrupt, + cx: &mut Ctx<'_>, +) -> Option>>> { + if merged_needed(file) { + let merged = decoded.unwrap_or_else(|| file.decode_merged()); + if ctl.cancelled() { + return None; + } + ctl.progress(0.1); + if let Err(error) = &merged { + cx.warn_unreadable_data(format!("merged image could not be decoded: {error}")); + if can_recover_layers(file) { + cx.warn_unreadable_data("extra alpha/spot channels in the merged image could not be recovered"); + } + } + Some(Some(merged)) + } else { + if let Some(Err(error)) = decoded { + cx.warn_unreadable_data(format!("merged image could not be decoded; opened the available layers instead: {error}")); + } + Some(None) + } +} + +fn import_decoded_content(import: DecodedImport<'_>, doc: &mut Document, cx: &mut Ctx<'_>) { + if import.multichannel { + import_multichannel(import.file, import.merged, doc, cx); + } else if import.layered { + import_layered(import, doc, cx); + } else { + import_flattened(import.file, import.merged, import.sizes, import.canvas, import.fmt, doc, cx); + } + import_extra_channels(import, doc, cx); +} + +fn import_multichannel(file: &PsdFile, merged: &Option>>, doc: &mut Document, cx: &mut Ctx<'_>) { + if let Some(Ok(all)) = merged { + let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default(); + crate::multichannel_map::import(file, all, doc, &names, &mut cx.warnings); + } + if !file.layers().is_empty() { + cx.warn(format!("Multichannel documents have no layers: {} layer records were not imported", file.layers().len())); + } +} + +fn import_layered(import: DecodedImport<'_>, doc: &mut Document, cx: &mut Ctx<'_>) { + let DecodedImport { file, merged, sizes, canvas, depth, fmt, .. } = import; + let cc = fmt.mode.color_channels(); + let tree = file.layer_tree_max_depth(photocraft_doc::MAX_GROUP_DEPTH); + let space = photocraft_compose::cmyk_space(doc); + doc.layers = photocraft_color::convert::with_cmyk_space(space.as_ref(), || cx.build(&tree, 0)); + mark_background(file, &mut doc.layers); + if let Some(Ok(photocraft_psd::resources::ResourceData::LayerGroupInfo(groups))) = + file.resources.iter().find(|r| r.id == ids::LAYER_GROUP_INFO).and_then(photocraft_psd::resources::ImageResource::parsed) + { + apply_link_groups(&tree, &mut doc.layers, &groups); + } + let Some((n, plane)) = sizes else { return }; + let Some(Ok(all)) = merged else { return }; + if !doc.layers.is_empty() { + return; + } + let alpha_idx = file.merged_has_alpha().then_some(cc); + let mut planes: Vec> = (0..cc).map(|c| merged_plane(all, c, plane)).collect(); + planes.push(alpha_idx.and_then(|a| merged_plane(all, a, plane))); + let mut fill = vec![zero_sample(depth); cc]; + fill.push(max_sample(depth)); + let mut inv = vec![cx.cmyk; cc]; + inv.push(false); + let mut bytes = interleave(&planes, &fill, n, depth, &inv); + if fmt.mode == ColorMode::Lab && depth == SampleType::U16 { + crate::pixels::lab16_chroma(&mut bytes, cc + 1, true); + } + let mut surface = Surface::from_interleaved(fmt, canvas, &bytes); + if alpha_idx.is_some() { + let white = photocraft_raster::from_rgba(&fmt, [1.0, 1.0, 1.0, 1.0]); + let mut vals = Vec::new(); + let mut y = canvas.y0; + while y < canvas.y1 { + let band = Rect::new(canvas.x0, y, canvas.x1, y.saturating_add(TILE_SIZE).min(canvas.y1)); + surface.read_region_into(band, &mut vals); + for px in vals.chunks_exact_mut(cc + 1) { + let a = px[cc]; + for c in 0..cc { + px[c] = if a <= 0.0 { 0.0 } else { crate::pixels::unmatte(px[c], a, white[c]) }; + } + } + surface.write_region(band, &vals); + y = band.y1; + } + } + surface.prune(); + let mut bg = Layer::new("Background", LayerContent::Raster(surface)); + if alpha_idx.is_none() { + bg.locks.transparency = true; + bg.locks.position = true; + } + doc.layers.push(bg); +} + +fn import_flattened( + file: &PsdFile, + merged: &Option>>, + sizes: Option<(usize, usize)>, + canvas: Rect, + fmt: PixelFormat, + doc: &mut Document, + cx: &mut Ctx<'_>, +) { + let h = &file.header; + if !file.layers().is_empty() { + cx.warn(format!("{:?} documents are imported flattened: {} layer records were not imported", h.color_mode, file.layers().len())); + } else { + cx.warn(format!("{:?} {}-bit document converted to {:?} 8-bit for editing", h.color_mode, h.depth, fmt.mode)); + } + if h.color_mode == PsdMode::Indexed && file.color_mode_data.len() >= 768 { + let m = &file.color_mode_data; + let u16_of = |id: u16| file.resource(id).and_then(|r| r.data.get(..2)).map(|b| u16::from_be_bytes([b[0], b[1]])); + let count = u16_of(crate::palette_map::COLOR_TABLE_COUNT).map_or(256, usize::from).clamp(1, 256); + let colors = (0..count).map(|i| [m[i], m[256 + i], m[512 + i]]).collect(); + let transparent = u16_of(crate::palette_map::TRANSPARENCY_INDEX).filter(|&i| usize::from(i) < count).map(|i| i as u8); + doc.color_table = Some(photocraft_doc::ColorTable { colors, transparent }); + } else if h.color_mode == PsdMode::Duotone && !file.color_mode_data.is_empty() { + let mut unknown = false; + let alternate = file.resource(crate::duotone_map::ALTERNATE_DUOTONE_COLORS).map(|r| r.data.as_slice()); + let inks = crate::duotone_map::parse(&file.color_mode_data, alternate, &mut unknown).filter(|_| !unknown); + if inks.is_none() { + cx.warn("duotone inks are not interpreted (shown as grayscale)"); + } + let inks = inks.unwrap_or_else(|| vec![photocraft_doc::DuotoneInk::new("Black", [0.0; 3])]); + doc.duotone = Some(photocraft_doc::Duotone { inks, psd_raw: Some(file.color_mode_data.as_slice().into()) }); + } else if !file.color_mode_data.is_empty() { + cx.warn("color mode data is not preserved"); + } + match flat_composite(file, merged.as_ref(), photocraft_psd::compression::MAX_DECODED_BYTES) { + Ok(mut img) => { + if h.color_mode == PsdMode::Indexed + && let Some(t) = doc.color_table.as_ref().and_then(|t| t.transparent) + && let Some(Ok(all)) = merged + && let Some(indices) = sizes.and_then(|(_, plane)| merged_plane(all, 0, plane)) + { + for (px, &i) in img.data.as_chunks_mut::<4>().0.iter_mut().zip(indices) { + if i == t { + px[3] = 0; + } + } + } + let mut surface = Surface::new(fmt); + write_rgba8(&mut surface, canvas, &img.data); + drop(img); + surface.prune(); + doc.layers.push(flat_background(surface)); + } + Err(_) if matches!(merged, Some(Err(_))) => {} + Err(error) => cx.warn_unreadable_data(format!("the image could not be read: {error}")), + } +} + +fn import_extra_channels(import: DecodedImport<'_>, doc: &mut Document, cx: &mut Ctx<'_>) { + // Extra (alpha / spot) channels of the merged image. + if !import.layered { + return; + } + let DecodedImport { file, merged, sizes, canvas, depth, .. } = import; + let Some(Ok(all)) = merged else { return }; + let Some((n, plane)) = sizes else { return }; + let cc = cx.cc; + let first = cc + usize::from(file.merged_has_alpha()); + let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default(); + // Photoshop lists every extra channel, the merged transparency first when there is one. + let skip = usize::from(file.merged_has_alpha() && names.len() == usize::from(file.header.channels).saturating_sub(cc)); + let names = names.get(skip..).unwrap_or_default(); + // Which planes hold spot ink in Photoshop's polarity (0 = solid): the DisplayInfo records + // the channel options are read from below, the transparency's first when `skip` is 1. + // A file from an earlier PhotoCraft stores them straight as it was written, + // past the channels of the Photoshop file it opened, if any. + let straight_from = if earlier_photocraft_spot_planes(file) { photoshop_identified_channels(file) } else { usize::MAX }; + let photoshop_spots = if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO) { + crate::channel_map::spot_flags(&r.data, true, skip) + } else if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO_OLD) { + crate::channel_map::spot_flags(&r.data, false, skip) + } else { + Vec::new() + }; + for (k, idx) in (first..usize::from(file.header.channels)).enumerate() { + let Some(p) = merged_plane(all, idx, plane) else { + cx.warn_unreadable_data(format!("merged extra channel {idx} is incomplete and was not imported")); + break; + }; + let mut raw = be_to_ne_plane(p, depth, n); + if k < straight_from && photoshop_spots.get(k).copied().unwrap_or(false) { + crate::channel_map::photoshop_spot_plane(&mut raw, depth); + } + let mut s = Surface::new(cx.mask_fmt); + s.write_interleaved(canvas, &raw); + s.prune(); + let name = names.get(k).cloned().unwrap_or_else(|| format!("Alpha {}", k + 1)); + doc.channels.push(AlphaChannel::new(name, s)); + } + if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO) { + crate::channel_map::apply_display_info(&r.data, true, skip, &mut doc.channels); + } else if let Some(r) = file.resource(crate::channel_map::DISPLAY_INFO_OLD) { + crate::channel_map::apply_display_info(&r.data, false, skip, &mut doc.channels); + } + if let Some(r) = file.resource(crate::channel_map::QUICK_MASK_INFO) + && r.data.len() >= 2 + && let Some(k) = usize::from(u16::from_be_bytes([r.data[0], r.data[1]])).checked_sub(first) + && k < doc.channels.len() + { + doc.quick_mask = Some(doc.channels.remove(k)); + } } #[cfg(test)] -mod tests { - use super::*; - use photocraft_psd::{Compression, PsdError, Version, testgen}; - - /// #5200: the Multichannel fallback checks its RGBA output against the budget before - /// allocating it (here a 2x2 image, 16 bytes, against caps of 15 and 16 bytes). - #[test] - fn flat_fallback_refuses_an_output_over_the_cap() { - let file = testgen::merged_only(Version::Psd, PsdMode::Multichannel, 8, Compression::Raw, 2, 2); - assert!(file.composite_rgba8().is_err(), "Multichannel goes through the fallback"); - let merged = file.decode_merged(); - assert!(matches!(flat_composite(&file, Some(&merged), 15), Err(PsdError::LimitExceeded(_)))); - let img = flat_composite(&file, Some(&merged), 16).unwrap(); - assert_eq!(img.data.len(), 16); - } - - /// #5200: a composite over the budget is not retried by the fallback (which allocated the - /// refused buffer: 14.4 GB for this 60000x60000 Bitmap header). - #[test] - fn flat_fallback_does_not_retry_an_over_budget_composite() { - let mut file = testgen::small(Version::Psb, Compression::Raw); - file.header.color_mode = PsdMode::Bitmap; - file.header.depth = 1; - file.header.channels = 1; - file.header.width = 60_000; - file.header.height = 60_000; - let merged = Ok(vec![0u8; 16]); - let cap = photocraft_psd::compression::MAX_DECODED_BYTES; - assert!(matches!(flat_composite(&file, Some(&merged), cap), Err(PsdError::LimitExceeded(_)))); - } - - /// #5202: a hostile name length ends the list instead of overflowing. - #[test] - fn unicode_names_stop_at_a_hostile_length() { - let mut data = 1u32.to_be_bytes().to_vec(); - data.extend([0, b'A']); - data.extend(u32::MAX.to_be_bytes()); - data.extend([0, b'B']); - assert_eq!(unicode_names(&data), ["A"]); - } - - #[test] - fn merged_plane_checks_offsets() { - let all = [1u8, 2, 3, 4]; - assert_eq!(merged_plane(&all, 1, 2), Some(&[3u8, 4][..])); - assert_eq!(merged_plane(&all, 2, 2), None); - assert_eq!(merged_plane(&all, usize::MAX, 2), None); - } -} +mod tests; diff --git a/crates/io/src/psd_import/tests.rs b/crates/io/src/psd_import/tests.rs new file mode 100644 index 0000000000..3bb19e3ef8 --- /dev/null +++ b/crates/io/src/psd_import/tests.rs @@ -0,0 +1,390 @@ +use super::*; +use photocraft_psd::descriptor::UnicodeString; +use photocraft_psd::resources::VersionInfo; +use photocraft_psd::{Compression, ImageData, ImageResource, PsdError, Version, testgen}; + +/// #5200: the Multichannel fallback checks its RGBA output against the budget before +/// allocating it (here a 2x2 image, 16 bytes, against caps of 15 and 16 bytes). +#[test] +fn flat_fallback_refuses_an_output_over_the_cap() { + let file = testgen::merged_only(Version::Psd, PsdMode::Multichannel, 8, Compression::Raw, 2, 2); + assert!(file.composite_rgba8().is_err(), "Multichannel goes through the fallback"); + let merged = file.decode_merged(); + assert!(matches!(flat_composite(&file, Some(&merged), 15), Err(PsdError::LimitExceeded(_)))); + let img = flat_composite(&file, Some(&merged), 16).unwrap(); + assert_eq!(img.data.len(), 16); +} + +/// #5200: a composite over the budget is not retried by the fallback (which allocated the +/// refused buffer: 14.4 GB for this 60000x60000 Bitmap header). +#[test] +fn flat_fallback_does_not_retry_an_over_budget_composite() { + let mut file = testgen::small(Version::Psb, Compression::Raw); + file.header.color_mode = PsdMode::Bitmap; + file.header.depth = 1; + file.header.channels = 1; + file.header.width = 60_000; + file.header.height = 60_000; + let merged = Ok(vec![0u8; 16]); + let cap = photocraft_psd::compression::MAX_DECODED_BYTES; + assert!(matches!(flat_composite(&file, Some(&merged), cap), Err(PsdError::LimitExceeded(_)))); +} + +/// #5202: a hostile name length ends the list instead of overflowing. +#[test] +fn unicode_names_stop_at_a_hostile_length() { + let mut data = 1u32.to_be_bytes().to_vec(); + data.extend([0, b'A']); + data.extend(u32::MAX.to_be_bytes()); + data.extend([0, b'B']); + assert_eq!(unicode_names(&data), ["A"]); +} + +/// Verifies an unreadable layer channel sets the structured damage indicator. +#[test] +fn unreadable_layer_channel_marks_conversion_damaged() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + let channel = file.layers_mut()[0].channels.iter_mut().find(|c| c.id == 0).unwrap(); + channel.data.clear(); + assert!(file.layers()[0].decode_channel(0, file.header.depth, file.header.version).is_err()); + + let (_, warnings, damaged) = psd_to_document_decoded_integrity(&file, &photocraft_raster::Interrupt::NONE, None).unwrap(); + assert!(damaged); + assert!(warnings.iter().any(|w| w.contains("could not be decoded"))); +} + +/// Verifies an unreadable user-mask channel sets the structured damage indicator. +#[test] +fn unreadable_mask_channel_marks_conversion_damaged() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + let channel = file.layers_mut()[0].channels.iter_mut().find(|c| c.id == CHANNEL_USER_MASK).unwrap(); + channel.data.clear(); + + let (_, warnings, damaged) = psd_to_document_decoded_integrity(&file, &photocraft_raster::Interrupt::NONE, None).unwrap(); + assert!(damaged); + assert!(warnings.iter().any(|w| w.contains("could not be decoded"))); +} + +/// A layer whose bounds hold pixels but whose colour and transparency channels are missing +/// lost its pixel data: the open asks before continuing. +#[test] +fn layer_bounds_without_channel_data_mark_conversion_damaged() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + file.layers_mut()[0].channels.retain(|c| c.id == CHANNEL_USER_MASK); + + let (_, warnings, damaged) = psd_to_document_decoded_integrity(&file, &photocraft_raster::Interrupt::NONE, None).unwrap(); + assert!(damaged); + assert!(warnings.iter().any(|w| w.contains("no channel data"))); +} + +/// A layer without pixels (an empty layer, or a text or shape layer written without a +/// pixel cache) has empty bounds: nothing is missing, so the file is not damaged even when +/// it lists no colour or transparency channel. +#[test] +fn empty_layer_without_channel_data_is_not_damaged() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + file.layers_mut()[0].channels.retain(|c| c.id == CHANNEL_USER_MASK); + file.layers_mut()[0].rect = Default::default(); + + let (_, warnings, damaged) = psd_to_document_decoded_integrity(&file, &photocraft_raster::Interrupt::NONE, None).unwrap(); + assert!(!damaged, "an empty layer marked the PSD damaged: {warnings:?}"); +} + +/// Verifies healthy layer channels do not mark the PSD as damaged. +#[test] +fn valid_layer_channels_keep_conversion_editable() { + let file = testgen::small(Version::Psd, Compression::Raw); + let (_, warnings, damaged) = psd_to_document_decoded_integrity(&file, &photocraft_raster::Interrupt::NONE, None).unwrap(); + assert!(!damaged, "unexpectedly marked good PSD damaged: {warnings:?}"); +} + +#[test] +fn merged_plane_checks_offsets() { + let all = [1u8, 2, 3, 4]; + assert_eq!(merged_plane(&all, 1, 2), Some(&[3u8, 4][..])); + assert_eq!(merged_plane(&all, 2, 2), None); + assert_eq!(merged_plane(&all, usize::MAX, 2), None); +} + +/// Resource 1057's data. +fn version_info(writer: UnicodeString, reader: UnicodeString, has_real_merged_data: bool) -> Vec { + VersionInfo { version: 1, has_real_merged_data, writer, reader, file_version: 1 }.to_bytes() +} + +/// The 1057 PhotoCraft 0.1.0 to 0.7.0 wrote: Photoshop CS6's names, NUL-terminated. +fn version_info_cs6(has_real_merged_data: bool) -> Vec { + version_info(UnicodeString::new_nul("Adobe Photoshop"), UnicodeString::new_nul("Adobe Photoshop CS6"), has_real_merged_data) +} + +/// The 1057 PhotoCraft writes since 0.8.2: Photoshop 2026's bytes, no NUL. +fn version_info_2026(has_real_merged_data: bool) -> Vec { + photocraft_psd::resources::version_info_resource(has_real_merged_data).data +} + +/// An opaque 4 × 1 RGB document with the Photoshop spot oracles' channels in their order: +/// "Spot A", "Alpha 1" and "Spot B", each holding 100, 25, 75 and 0 % in four columns. +fn channels_document(depth: SampleType) -> Document { + let mut doc = Document::with_background("old", Size::new(4, 1), ColorMode::Rgb, depth, photocraft_color::Color::rgb(1.0, 1.0, 1.0)); + let gray = PixelFormat::new(ColorMode::Grayscale, depth, false); + let canvas = doc.bounds(); + let channel = |name: &str, spot: Option<(photocraft_color::Color, f32)>| { + let mut s = Surface::new(gray); + s.write_region(canvas, &[1.0, 0.25, 0.75, 0.0]); + AlphaChannel { spot, ..AlphaChannel::new(name, s) } + }; + doc.channels.push(channel("Spot A", Some((photocraft_color::Color::rgb(0.0, 0.627, 1.0), 0.0)))); + doc.channels.push(channel("Alpha 1", None)); + doc.channels.push(channel("Spot B", Some((photocraft_color::Color::rgb(1.0, 0.0, 0.627), 0.5)))); + doc +} + +/// Each channel's values on an 8-bit scale. +fn ink(doc: &Document) -> Vec> { + doc.channels.iter().map(|c| c.surface.read_region(doc.bounds()).iter().map(|v| (v * 255.0).round() as u8).collect()).collect() +} + +fn spots(doc: &Document) -> Vec { + doc.channels.iter().map(|c| c.spot.is_some()).collect() +} + +/// `doc` (opaque, 8 or 16-bit) saved the way PhotoCraft 0.1.0 to 0.16.0 saved it: spot planes +/// straight (the maximum = solid ink), no 1053, and `version_info` as resource 1057. `padded` +/// lays DisplayInfo out in the 14-byte records of 0.1.0 to 0.7.0. +fn as_earlier_photocraft_saved(doc: &Document, version_info: Vec, padded: bool) -> Vec { + let mut file = crate::psd_export::document_to_psd(doc); + assert!(!file.merged_has_alpha()); + file.resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS); + for r in &mut file.resources { + match r.id { + ids::VERSION_INFO => r.data = version_info.clone(), + crate::channel_map::DISPLAY_INFO if padded => { + let (version, records) = r.data.split_at(4); + r.data = version.iter().copied().chain(records.chunks(13).flat_map(|rec| rec.iter().copied().chain([0]))).collect(); + } + _ => {} + } + } + let mut planes = file.decode_merged().unwrap(); + let plane = planes.len() / usize::from(file.header.channels); + for (k, c) in doc.channels.iter().enumerate() { + if c.spot.is_some() { + // At 8 and 16 bits the complement of a sample is that of each of its bytes. + planes[(3 + k) * plane..(4 + k) * plane].iter_mut().for_each(|b| *b = !*b); + } + } + file.image_data = ImageData::encode(Compression::Raw, &planes, &file.header).unwrap(); + file.to_bytes().unwrap() +} + +/// PSDs that PhotoCraft 0.1.0 to 0.16.0 saved with spot channels store them straight. They +/// open with the ink they had in those versions (which read every plane straight), whichever +/// 1057 they carry. Saved again, they get Photoshop's polarity and a 1053, and reopen with the +/// same ink. (A 0.1.0 to 0.7.0 file's padded records misread every channel's options after +/// the first, here Spot B's kind, as those versions' successors always have; its ink is kept.) +#[test] +fn spot_planes_from_earlier_photocraft_keep_their_ink() { + for depth in [SampleType::U8, SampleType::U16] { + let doc = channels_document(depth); + let cases = [ + ("0.1.0 to 0.7.0", version_info_cs6(true), true), + ("0.8.2 on", version_info_2026(true), false), + ("0.8.2 on, Maximize Compatibility off", version_info_2026(false), false), + ("0.1.0 to 0.7.0, 13-byte records", version_info_cs6(true), false), + ]; + for (what, version_info, padded) in cases { + let bytes = as_earlier_photocraft_saved(&doc, version_info, padded); + assert!(earlier_photocraft_spot_planes(&PsdFile::from_bytes(&bytes).unwrap()), "{what}"); + let opened = crate::import("old.psd", &bytes).unwrap().document; + assert_eq!(ink(&opened), ink(&doc), "{depth:?} {what}: the ink earlier versions opened"); + assert!(spots(&opened)[0], "{depth:?} {what}"); + if !padded { + assert_eq!(spots(&opened), spots(&doc), "{depth:?} {what}"); + } + let saved = crate::export(&opened, "old.psd", &crate::ExportOptions::default()).unwrap(); + let file = PsdFile::from_bytes(&saved.bytes).unwrap(); + assert!(file.resource(crate::channel_map::ALPHA_IDENTIFIERS).is_some() && !earlier_photocraft_spot_planes(&file), "{what}"); + let again = crate::import("again.psd", &saved.bytes).unwrap().document; + assert_eq!(ink(&again), ink(&doc), "{depth:?} {what}: saved again"); + assert_eq!(spots(&again), spots(&opened), "{depth:?} {what}: saved again"); + } + } +} + +/// A 0.1.0 to 0.7.0 file whose first extra channel is an alpha channel, spots after it. In its +/// 14-byte DisplayInfo records the spots' kind bytes sit where a 13-byte reader (main's, and +/// this one's) doesn't look, so only the padded reading finds a spot for the rule. Every plane +/// is then read straight, as those versions read it, and keeps its ink. The 13-byte reader +/// opens each channel's options from shifted bytes, as on main: here every channel opens as +/// an alpha channel. A spot of 2 % solidity puts a 2 where that reader looks for the kind, so +/// that channel opens as a spot: without the rule its plane would be inverted. Saved again and +/// reopened, every channel keeps its ink and kind. +#[test] +fn an_early_file_with_an_alpha_channel_first_keeps_its_ink() { + for solidity in [0.0, 0.02] { + let mut doc = channels_document(SampleType::U8); + doc.channels.swap(0, 1); + doc.channels[1].spot = Some((photocraft_color::Color::rgb(0.0, 0.627, 1.0), solidity)); + let names: Vec<&str> = doc.channels.iter().map(|c| c.name.as_str()).collect(); + assert_eq!(names, ["Alpha 1", "Spot A", "Spot B"]); + let bytes = as_earlier_photocraft_saved(&doc, version_info_cs6(true), true); + let file = PsdFile::from_bytes(&bytes).unwrap(); + let info = &file.resource(crate::channel_map::DISPLAY_INFO).unwrap().data; + let misread = solidity > 0.0; + assert_eq!(crate::channel_map::spot_flags(info, true, 0), [false, misread, false], "{solidity}: 13-byte records"); + assert!(earlier_photocraft_spot_planes(&file), "{solidity}: found in the 14-byte records"); + let opened = crate::import("old.psd", &bytes).unwrap().document; + assert_eq!(ink(&opened), ink(&doc), "{solidity}: the ink 0.7.0 opened"); + assert_eq!(spots(&opened), [false, misread, false], "{solidity}: kinds as main reads them"); + let saved = crate::export(&opened, "old.psd", &crate::ExportOptions::default()).unwrap(); + let again = crate::import("again.psd", &saved.bytes).unwrap().document; + assert_eq!(ink(&again), ink(&doc), "{solidity}: saved again"); + assert_eq!(spots(&again), spots(&opened), "{solidity}: saved again"); + } +} + +/// A file with spots and no 1053 that no earlier PhotoCraft wrote is read in Photoshop's +/// polarity (0 = solid ink). psd-tools' `cmyk-spot.psd` is one: its 1057 names psd-tools as +/// writer and reader. So is a file with no 1057, with Photoshop CS6's names without the NUL, +/// or with bytes after PhotoCraft's. +#[test] +fn other_files_without_alpha_identifiers_use_photoshop_polarity() { + let doc = channels_document(SampleType::U8); + let mut trailing = version_info_2026(true); + trailing.extend([0, 0]); + let cases = [ + ("psd-tools", Some(version_info(UnicodeString::new("psd-tools 1.10.0"), UnicodeString::new("psd-tools 1.10.0"), true))), + ("no 1057", None), + ("CS6 without a NUL", Some(version_info(UnicodeString::new("Adobe Photoshop"), UnicodeString::new("Adobe Photoshop CS6"), true))), + ("bytes after PhotoCraft's 1057", Some(trailing)), + ]; + for (what, info) in cases { + // Photoshop's polarity, as this version writes it, without 1053. + let mut file = crate::psd_export::document_to_psd(&doc); + file.resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS && r.id != ids::VERSION_INFO); + file.resources.extend(info.map(|d| ImageResource::new(ids::VERSION_INFO, d))); + let bytes = file.to_bytes().unwrap(); + assert!(!earlier_photocraft_spot_planes(&PsdFile::from_bytes(&bytes).unwrap()), "{what}"); + let opened = crate::import("x.psd", &bytes).unwrap().document; + assert_eq!(ink(&opened), ink(&doc), "{what}"); + assert_eq!(spots(&opened), spots(&doc), "{what}"); + } +} + +/// The rule needs a spot in DisplayInfo and no 1053, besides PhotoCraft's 1057. +#[test] +fn the_earlier_photocraft_rule_needs_a_spot_and_no_alpha_identifiers() { + let mut doc = channels_document(SampleType::U8); + let old = PsdFile::from_bytes(&as_earlier_photocraft_saved(&doc, version_info_2026(true), false)).unwrap(); + assert!(earlier_photocraft_spot_planes(&old)); + let mut with_ids = old.clone(); + with_ids.resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, crate::channel_map::alpha_identifiers(false, 3))); + assert!(!earlier_photocraft_spot_planes(&with_ids), "a 1053"); + let mut no_info = old.clone(); + no_info.resources.retain(|r| r.id != crate::channel_map::DISPLAY_INFO); + assert!(!earlier_photocraft_spot_planes(&no_info), "no DisplayInfo"); + doc.channels.retain(|c| c.spot.is_none()); + let alpha_only = PsdFile::from_bytes(&as_earlier_photocraft_saved(&doc, version_info_2026(true), false)).unwrap(); + assert!(!earlier_photocraft_spot_planes(&alpha_only), "alpha channels only"); +} + +/// `doc` as an earlier PhotoCraft saved a Photoshop file it had opened with the first `kept` +/// of `doc`'s channels: it wrote back the Photoshop file's 1053, `stale_ids`, unchanged and +/// those channels' planes as Photoshop stored them (it had read and written them straight), +/// and stored the channels added after them straight. +fn as_earlier_photocraft_resaved(doc: &Document, stale_ids: &[u32], kept: usize) -> Vec { + let mut file = crate::psd_export::document_to_psd(doc); + file.resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS && r.id != ids::VERSION_INFO); + file.resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, stale_ids.iter().flat_map(|id| id.to_be_bytes()).collect())); + file.resources.push(ImageResource::new(ids::VERSION_INFO, version_info_2026(true))); + let mut planes = file.decode_merged().unwrap(); + let plane = planes.len() / usize::from(file.header.channels); + let first = usize::from(file.header.channels) - doc.channels.len(); + for (k, c) in doc.channels.iter().enumerate().skip(kept) { + if c.spot.is_some() { + planes[(first + k) * plane..(first + k + 1) * plane].iter_mut().for_each(|b| *b = !*b); + } + } + file.image_data = ImageData::encode(Compression::Raw, &planes, &file.header).unwrap(); + file.to_bytes().unwrap() +} + +/// An earlier PhotoCraft kept the 1053 of a Photoshop file it opened and wrote it back +/// unchanged, so a spot channel added there sits behind a 1053 that doesn't list it: a +/// transparent Photoshop document's `[0]` (the white plate of a DTF print, added before +/// saving), the `[0, 3]` of one with a spot channel already, or a `[0]` kept through a +/// flatten that removed the transparency. The added spot is stored straight and keeps its +/// ink; the Photoshop file's own spot keeps Photoshop's polarity. Saved again, both reopen +/// the same. +#[test] +fn a_spot_added_behind_a_kept_alpha_identifiers_resource_keeps_its_ink() { + let transparent = |names: &[&str]| { + let mut doc = channels_document(SampleType::U8); + doc.layers = vec![Layer::raster("Layer 1", doc.pixel_format())]; + doc.channels.retain(|c| names.contains(&c.name.as_str())); + doc + }; + let mut flattened = channels_document(SampleType::U8); + flattened.channels.retain(|c| c.name == "Spot A"); + let cases = [ + ("transparent, one spot added", transparent(&["Spot A"]), vec![0], 0), + ("transparent, a Photoshop spot and one added", transparent(&["Spot A", "Spot B"]), vec![0, 3], 1), + ("flattened, one spot added", flattened, vec![0], 0), + ]; + for (what, doc, stale_ids, kept) in cases { + let bytes = as_earlier_photocraft_resaved(&doc, &stale_ids, kept); + let file = PsdFile::from_bytes(&bytes).unwrap(); + assert_eq!(file.merged_has_alpha(), what.starts_with("transparent"), "{what}"); + assert!(earlier_photocraft_spot_planes(&file), "{what}"); + let opened = crate::import("old.psd", &bytes).unwrap().document; + assert_eq!(ink(&opened), ink(&doc), "{what}: the ink earlier versions opened"); + assert_eq!(spots(&opened), spots(&doc), "{what}"); + let saved = crate::export(&opened, "old.psd", &crate::ExportOptions::default()).unwrap(); + let again = crate::import("again.psd", &saved.bytes).unwrap().document; + assert_eq!(ink(&again), ink(&doc), "{what}: saved again"); + } +} + +/// A 1053 that lists every extra channel, as Photoshop and this version write it, keeps the +/// file in Photoshop's polarity, with PhotoCraft's (Photoshop 2026's) 1057 too: a transparent +/// file with a spot, and Photoshop's own `[0]` in a file without a transparency plane. +#[test] +fn a_complete_alpha_identifiers_resource_keeps_photoshop_polarity() { + let mut doc = channels_document(SampleType::U8); + doc.layers = vec![Layer::raster("Layer 1", doc.pixel_format())]; + let bytes = as_earlier_photocraft_resaved(&doc, &[0, 3, 4, 5], 3); + let file = PsdFile::from_bytes(&bytes).unwrap(); + assert!(file.merged_has_alpha()); + assert!(!earlier_photocraft_spot_planes(&file)); + assert_eq!(ink(&crate::import("ps.psd", &bytes).unwrap().document), ink(&doc)); + let mut opaque = channels_document(SampleType::U8); + opaque.channels.clear(); + let mut file = PsdFile::from_bytes(&as_earlier_photocraft_resaved(&opaque, &[0], 0)).unwrap(); + file.resources.retain(|r| r.id != crate::channel_map::DISPLAY_INFO); + file.resources.push(ImageResource::new( + crate::channel_map::DISPLAY_INFO, + crate::channel_map::display_info(&channels_document(SampleType::U8).channels.iter().collect::>(), false), + )); + assert_eq!(photoshop_identified_channels(&file), 0, "0 is the transparency's"); + assert!(!earlier_photocraft_spot_planes(&file), "no channel left unlisted"); +} + +#[test] +fn layer_bands_end_on_tile_rows_and_cover_the_layer() { + let t = TILE_SIZE as usize; + for (top, h, row_bytes) in [(0, 2000, 8000), (-37, 15000, 80_000), (100, 5, 4), (255, 1, 4), (-256, 40_000, 1 << 20), (7, 0, 4)] { + let bands = band_rows(top, h, row_bytes); + let mut next = 0; + for &(a, b) in &bands { + assert_eq!(a, next); + assert!(b > a); + if b < h { + assert_eq!((top + b as i32).rem_euclid(TILE_SIZE), 0, "{top} {h}: band ends inside a tile row"); + assert!((b - a) * row_bytes <= BAND_BYTES + t * row_bytes || b - a <= t); + } + next = b; + } + assert_eq!(next, h); + } + // A small layer is one band. + assert_eq!(band_rows(0, 2000, 8000), vec![(0, 2000)]); +} diff --git a/crates/io/src/raw.rs b/crates/io/src/raw.rs index 008173839b..a1ed4aaa01 100644 --- a/crates/io/src/raw.rs +++ b/crates/io/src/raw.rs @@ -14,8 +14,9 @@ use std::sync::Arc; use photocraft_cms::{Builtin, Intent, Profile, Transform}; use photocraft_codecs::{self as codecs, ChannelLayout, Format, Image, SampleType}; +pub use photocraft_raw::Demosaic; use photocraft_raw::{CameraLook, DevelopOptions, Limits, PreviewColorSpace, RawError, Reference, Sensor, WhiteBalance}; -pub use photocraft_raw::{Tone, retone}; +pub use photocraft_raw::{EXTENSIONS, Tone, retone}; use crate::flat::image_to_document; use crate::{ImportResult, IoError}; @@ -34,9 +35,14 @@ fn limits() -> Limits { /// A raw file's embedded JPEG preview, turned upright. Its own EXIF orientation wins when it /// has one; otherwise the raw's IFD0 orientation applies (TIFF-based raws record it there, and /// their previews are usually stored as the sensor reads out). Developed raws are oriented by -/// `photocraft-raw` and carry no EXIF, so nothing is turned twice. -fn upright_preview(raw: &[u8], jpeg: &[u8]) -> Result { - let img = codecs::decode_as_with(Format::Jpeg, jpeg, &codecs::DecodeOptions { keep_orientation: true, ..Default::default() })?; +/// `photocraft-raw` and their EXIF carries no orientation, so nothing is turned twice. +fn upright_preview(raw: &[u8], jpeg: &[u8], limits: &photocraft_raw::Limits) -> Result { + // Decode the embedded preview under the caller's limits, not the codec defaults: an + // embedded JPEG can declare far more decoded pixels than the raw development the caller + // authorized, so the preview fallback must honor the same policy. + let codec_limits = + codecs::Limits { max_width: limits.max_width, max_height: limits.max_height, max_pixels: limits.max_pixels, max_alloc: limits.max_alloc }; + let img = codecs::decode_as_with(Format::Jpeg, jpeg, &codecs::DecodeOptions { keep_orientation: true, limits: codec_limits })?; let own = img.meta.exif.as_deref().map_or(1, codecs::exif_orientation); let o = if own != 1 { own } else { photocraft_raw::orientation(raw).unwrap_or_else(|| codecs::exif_orientation(raw)) }; Ok(img.oriented(o)?) @@ -44,7 +50,7 @@ fn upright_preview(raw: &[u8], jpeg: &[u8]) -> Result { /// Camera Raw's Temperature, Tint, Exposure, Shadows, Highlights, Clarity, Texture and Dehaze, /// applied while developing the sensor data (the open-time Camera Raw dialog) instead of to the -/// developed RGB pixels. +/// developed RGB pixels, and the demosaic method. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub struct RawTuning { /// −100…100, as `photocraft_algo::camera_raw::CameraRaw::temperature`. @@ -53,6 +59,7 @@ pub struct RawTuning { pub tint: f32, /// EV, as `CameraRaw::exposure`. pub exposure: f32, + pub demosaic: Demosaic, /// −100…100, as `CameraRaw::shadows`: in linear light before the tone curve, as Camera Raw /// does for raw files ([`photocraft_raw::Tone`]). pub shadows: f32, @@ -90,7 +97,7 @@ pub fn import_raw_tuned(name: &str, bytes: &[u8], tuning: &RawTuning) -> Result< // The colour rendering the default open used (a look fitted to the camera's JPEG for files // without colour calibration), so re-developing changes only what the sliders change. let camera_look = default_look(bytes, &sensor); - let mut opts = DevelopOptions { limits: limits(), exposure, tone, camera_look, ..Default::default() }; + let mut opts = DevelopOptions { limits: limits(), exposure, demosaic: tuning.demosaic, tone, camera_look, ..Default::default() }; let wb_applied = match sensor.camera_wb { Some(m) => { let g = wb_gains(tuning.temperature, tuning.tint); @@ -110,8 +117,8 @@ pub fn import_raw_tuned(name: &str, bytes: &[u8], tuning: &RawTuning) -> Result< .filter(|(_, v)| *v != 0.0) .map(|(k, v)| format!(", {k} {v:+.0}")) .collect::(); - let summary = format!("developed in Camera Raw (exposure {:+.2} EV{tone}, {wb})", tuning.exposure); - Ok((developed_document(name, format, dev, &summary)?, wb_applied)) + let summary = format!("developed in Camera Raw (exposure {:+.2} EV{tone}, {wb}, {} demosaic)", tuning.exposure, tuning.demosaic.id()); + Ok((developed_document(name, bytes, format, dev, &summary)?, wb_applied)) } /// The tone curve `bytes` develop with by default: the fitted look's for a file without colour @@ -164,8 +171,11 @@ fn wb_gains(temperature: f32, tint: f32) -> [f64; 3] { } /// A developed raw as a 16-bit ProPhoto document, with a note saying how it was developed. -fn developed_document(name: &str, format: &str, dev: photocraft_raw::Developed, summary: &str) -> Result { - let img = Image::from_u16(dev.width, dev.height, ChannelLayout::Rgb, &dev.rgb)?; +fn developed_document(name: &str, bytes: &[u8], format: &str, dev: photocraft_raw::Developed, summary: &str) -> Result { + let mut img = Image::from_u16(dev.width, dev.height, ChannelLayout::Rgb, &dev.rgb)?; + // The raw's own EXIF (camera, lens, exposure, date), for File Info's Camera Data. It carries + // no orientation: the developed image is already upright. + img.meta.exif = codecs::raw_exif(bytes); let mut r = image_to_document(name, &img)?; r.document.icc_profile = Some(Arc::new(photocraft_cms::Builtin::ProPhotoCompat.profile().to_bytes().to_vec())); let camera = camera_name(dev.info.make.as_deref(), dev.info.model.as_deref()); @@ -180,7 +190,12 @@ fn developed_document(name: &str, format: &str, dev: photocraft_raw::Developed, /// Develops a raw file with the default settings. pub fn import_raw(name: &str, bytes: &[u8]) -> Result { - import_raw_with(name, bytes, &DevelopOptions { limits: limits(), ..Default::default() }) + import_raw_demosaic(name, bytes, Demosaic::default()) +} + +/// Develops a raw file with the default settings and the `demosaic` method. +pub fn import_raw_demosaic(name: &str, bytes: &[u8], demosaic: Demosaic) -> Result { + import_raw_with(name, bytes, &DevelopOptions { limits: limits(), demosaic, ..Default::default() }) } /// Smallest preview long edge preferred for the colour fit (a smaller preview decodes faster; @@ -213,7 +228,7 @@ pub fn fit_preview_look(raw: &[u8], sensor: &Sensor, opts: &DevelopOptions) -> R .min_by_key(|p| long(p)) .or_else(|| previews.iter().max_by_key(|p| long(p))) .ok_or("no embedded JPEG preview")?; - let img = upright_preview(raw, pick.jpeg).map_err(|e| format!("the embedded preview does not decode: {e}"))?; + let img = upright_preview(raw, pick.jpeg, &opts.limits).map_err(|e| format!("the embedded preview does not decode: {e}"))?; if img.layout() != ChannelLayout::Rgb || img.sample_type() != SampleType::U8 { return Err("the embedded preview is not 8-bit RGB".into()); } @@ -246,6 +261,16 @@ fn develop_fitted(bytes: &[u8], opts: &DevelopOptions) -> Result Option { + let note = warnings.iter().find(|w| w.contains(DEVELOPED_NOTE))?; + let after = ¬e[note.find(DEVELOPED_NOTE)? + DEVELOPED_NOTE.len()..]; + let id = after.trim_start().strip_prefix('(')?.split(" demosaic").next()?; + Demosaic::from_id(id) +} + /// Develops a raw file with explicit settings. Without `opts.camera_look`, a file without /// colour calibration gets one fitted to its embedded JPEG when that clearly helps. pub fn import_raw_with(name: &str, bytes: &[u8], opts: &DevelopOptions) -> Result { @@ -253,31 +278,66 @@ pub fn import_raw_with(name: &str, bytes: &[u8], opts: &DevelopOptions) -> Resul match develop_fitted(bytes, opts) { Ok(dev) => { let summary = format!("{DEVELOPED_NOTE} ({} demosaic, as-shot white balance)", opts.demosaic.id()); - developed_document(name, format, dev, &summary) + developed_document(name, bytes, format, dev, &summary) + } + Err(RawError::Unsupported(reason)) => { + let jpeg = photocraft_raw::embedded_preview(bytes); + // A DNG 1.7 converter keeps only its small thumbnail as JPEG and writes the larger + // previews as JPEG XL, like the raw data (#8125): open the largest of either kind. + let jpeg_area = jpeg.map_or(0, |p| u64::from(p.width) * u64::from(p.height)); + let jxl = photocraft_raw::embedded_jxl_preview(bytes, &opts.limits).filter(|p| u64::from(p.width) * u64::from(p.height) > jpeg_area); + let (img, kind, (w, h)) = match (jxl, jpeg) { + (Some(p), _) => { + let size = (p.width, p.height); + (jxl_preview_image(bytes, p)?, "JPEG XL", size) + } + (None, Some(p)) => (upright_preview(bytes, p.jpeg, &opts.limits)?, "JPEG", (p.width, p.height)), + (None, None) => return Err(IoError::Raw(RawError::Unsupported(reason))), + }; + let mut r = image_to_document(name, &img)?; + let depth = if img.sample_type() == SampleType::U8 { "8-bit" } else { "16-bit" }; + r.warnings.insert( + 0, + format!( + "{format}: {reason} is not supported yet; opened the camera's embedded {w}x{h} {kind} preview instead ({depth}, not the raw sensor data)" + ), + ); + Ok(r) } - Err(RawError::Unsupported(reason)) => match photocraft_raw::embedded_preview(bytes) { - Some(p) => { - let img = upright_preview(bytes, p.jpeg)?; - let mut r = image_to_document(name, &img)?; - r.warnings.insert( - 0, - format!( - "{format}: {reason} is not supported yet; opened the camera's embedded {}x{} JPEG preview instead (8-bit, not the raw sensor data)", - p.width, p.height - ), - ); - Ok(r) - } - None => Err(IoError::Raw(RawError::Unsupported(reason))), - }, Err(e) => Err(IoError::Raw(e)), } } +/// A decoded JPEG XL preview as an image: in its stated colour space (sRGB when it states +/// none), turned upright by the raw's orientation (DNG previews are stored as the raw is). +fn jxl_preview_image(raw: &[u8], p: photocraft_raw::DecodedPreview) -> Result { + let img = if p.bits == 8 { + Image::from_u8(p.width, p.height, ChannelLayout::Rgb, p.rgb.iter().map(|&v| v.min(255) as u8).collect())? + } else { + let max = f64::from((1u32 << p.bits.clamp(1, 16)) - 1); + let rgb: Vec = p.rgb.iter().map(|&v| (f64::from(v).min(max) * 65535.0 / max).round() as u16).collect(); + Image::from_u16(p.width, p.height, ChannelLayout::Rgb, &rgb)? + }; + let icc = match p.color_space { + Some(PreviewColorSpace::AdobeRgb) => Some(Builtin::AdobeRgbCompat.profile().to_bytes().to_vec()), + Some(PreviewColorSpace::ProPhoto) => Some(Builtin::ProPhotoCompat.profile().to_bytes().to_vec()), + Some(PreviewColorSpace::Srgb) | None => None, + }; + Ok(img.with_icc(icc).oriented(photocraft_raw::orientation(raw).unwrap_or(1))?) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn the_developed_note_says_which_demosaic_method_was_used() { + let note = format!("NEF: {DEVELOPED_NOTE} (mhc demosaic, as-shot white balance) into 16-bit ProPhoto RGB"); + assert_eq!(developed_demosaic(&["unrelated".to_string(), note]), Some(Demosaic::Mhc)); + assert_eq!(developed_demosaic(&[format!("{DEVELOPED_NOTE} (amaze demosaic)")]), None); + assert_eq!(developed_demosaic(&["opened the embedded preview".to_string()]), None); + } + #[test] fn camera_names_drop_a_repeated_make() { assert_eq!(camera_name(Some("Canon"), Some("Canon EOS 80D")), "Canon EOS 80D"); diff --git a/crates/io/src/smart_map.rs b/crates/io/src/smart_map.rs index 3de336e16c..72adbe1291 100644 --- a/crates/io/src/smart_map.rs +++ b/crates/io/src/smart_map.rs @@ -20,11 +20,13 @@ //! listed, can be hidden, reordered or deleted, and is written back unchanged. //! //! Camera Raw and Displace keep their whole item as a template next to the editable settings -//! (Displace's carries the embedded displacement map, see `displace_map.rs`). +//! (Displace's carries the embedded displacement map, see `displace_map.rs`); so does Perspective +//! Warp (`perspective_warp_map.rs`), whose quads become the planes of its command. Hue/Saturation +//! (`HStr`) maps to its adjustment command in `hue_sat_filter_map.rs`. use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; use photocraft_doc::{LayerMask, SmartFilter}; -use photocraft_geom::warp::{Warp, WarpStyle}; +use photocraft_geom::warp::{BezierMesh, Warp, WarpStyle}; use photocraft_geom::{Affine, Rect as GeomRect}; use photocraft_psd::Compression; use photocraft_psd::Rect as PsdRect; @@ -35,12 +37,35 @@ use serde_json::{Map, Value as J, json}; use crate::blocks::{enum_of, get_desc, num}; +#[cfg(test)] +#[path = "smart_map/halftone_tests.rs"] +mod halftone_tests; + #[path = "camera_raw_map.rs"] mod camera_raw; #[path = "displace_map.rs"] mod displace; +#[path = "hue_sat_filter_map.rs"] +mod hue_sat; +#[path = "perspective_warp_map.rs"] +mod perspective_warp; -pub use displace::embedded_map as displace_embedded_map; +pub use displace::{embed_map as displace_embed_map, embedded_map as displace_embedded_map, without_map as displace_without_map}; + +mod export; +mod import; +mod placed; + +use export::{blend_options, is_meta, rgbc, set, unit}; +pub use export::{filter_fx, item_for_filter}; +pub use import::filter_from_item; +#[cfg(test)] +use import::{GALLERY_EFFECTS, LENSES, MEZZOTINT_TYPES, adjustment_list, known_params}; +use import::{code, from_hex, int, to_hex}; +use placed::enumv; +pub use placed::{PlacedSpec, plld_bytes, sold_bytes, stored_size}; +#[cfg(test)] +use placed::{quad_moved, warp_desc}; /// Command id of a Photoshop smart filter PhotoCraft does not implement. Its params hold the /// filter's name, Photoshop filter id and descriptor (`psd`, hex); it renders as a pass-through. @@ -129,737 +154,6 @@ pub fn filter_stack(fx: &Descriptor) -> FilterStack { } } -// ---------- the filters we model ---------- - -/// Photoshop filter → PhotoCraft command, keyed by the `Fltr` class (and `filterID`). -struct Known { - class: &'static str, - filter_id: i32, - /// `Nm ` as Photoshop writes it (the menu item, with its ellipsis and mnemonic). - name: &'static str, - command: &'static str, -} - -const fn code(c: &[u8; 4]) -> i32 { - i32::from_be_bytes(*c) -} - -const KNOWN: [Known; 14] = [ - Known { class: "GsnB", filter_id: code(b"GsnB"), name: "Gaussian Blur...", command: "filter.blur.gaussianBlur" }, - Known { class: "boxblur", filter_id: 840, name: "Box Blur...", command: "filter.blur.boxBlur" }, - Known { class: "MtnB", filter_id: code(b"MtnB"), name: "Motion Blur...", command: "filter.blur.motionBlur" }, - Known { class: "UnsM", filter_id: code(b"UnsM"), name: "Unsharp Mask...", command: "filter.sharpen.unsharpMask" }, - Known { class: "AdNs", filter_id: code(b"AdNs"), name: "Add Noise...", command: "filter.noise.addNoise" }, - Known { class: "Mdn ", filter_id: code(b"Mdn "), name: "Median...", command: "filter.noise.median" }, - Known { class: "HghP", filter_id: code(b"HghP"), name: "High Pass...", command: "filter.other.highPass" }, - Known { class: "Mxm ", filter_id: code(b"Mxm "), name: "Maximum...", command: "filter.other.maximum" }, - Known { class: "Mnm ", filter_id: code(b"Mnm "), name: "Minimum...", command: "filter.other.minimum" }, - Known { class: "Msc ", filter_id: code(b"Msc "), name: "Mosaic...", command: "filter.pixelate.mosaic" }, - Known { class: "Embs", filter_id: code(b"Embs"), name: "Emboss...", command: "filter.stylize.emboss" }, - Known { class: "Lvls", filter_id: code(b"Lvls"), name: "Levels...", command: "image.adjustments.levels" }, - Known { class: "Crvs", filter_id: code(b"Crvs"), name: "Curves...", command: "image.adjustments.curves" }, - // Shadows/Highlights at its defaults stores no `Fltr` (`hasoptions` false). - Known { class: "adaptCorrect", filter_id: 781, name: "Shado&ws/Highlights...", command: "image.adjustments.shadowsHighlights" }, -]; - -fn known_by_command(command: &str) -> Option<&'static Known> { - KNOWN.iter().find(|k| k.command == command) -} - -/// RGB channel references in Levels/Curves (`Chnl` enum) and their parameter keys ("" = the -/// composite: top-level keys). -const CHANNELS: [(&[u8; 4], &str); 4] = [(b"Cmps", ""), (b"Rd ", "red"), (b"Grn ", "green"), (b"Bl ", "blue")]; - -fn px(d: &Descriptor, key: &str) -> Option { - num(d.get(key)).filter(|v| v.is_finite()) -} - -fn int(d: &Descriptor, key: &str) -> Option { - match d.get(key)? { - Value::Integer(i) => Some(*i), - _ => None, - } -} - -fn channel_key(d: &Descriptor) -> Option<&'static str> { - let Some(Value::Reference(items)) = d.get("Chnl") else { return None }; - let Some(ReferenceItem::Enumerated { value, .. }) = items.first() else { return None }; - CHANNELS.iter().find(|(c, _)| value.as_bytes() == &c[..]).map(|(_, k)| *k) -} - -fn adjustment_list(d: &Descriptor) -> Option> { - let Some(Value::List(items)) = d.get("Adjs") else { return None }; - items - .iter() - .map(|v| match v { - Value::Descriptor(x) => Some(x), - _ => None, - }) - .collect() -} - -fn levels_params(f: &Descriptor) -> Option { - let mut out = Map::new(); - for a in adjustment_list(f)? { - let key = channel_key(a)?; - let mut o = Map::new(); - let pair = |k: &str| match a.get(k) { - Some(Value::List(v)) if v.len() == 2 => Some((int_value(&v[0])?, int_value(&v[1])?)), - _ => None, - }; - if let Some((lo, hi)) = pair("Inpt") { - o.insert("inBlack".into(), json!(lo)); - o.insert("inWhite".into(), json!(hi)); - } - if let Some(g) = px(a, "Gmm ") { - o.insert("gamma".into(), json!(g)); - } - if let Some((lo, hi)) = pair("Otpt") { - o.insert("outBlack".into(), json!(lo)); - o.insert("outWhite".into(), json!(hi)); - } - if key.is_empty() { - out.extend(o); - } else { - out.insert(key.into(), J::Object(o)); - } - } - Some(J::Object(out)) -} - -fn int_value(v: &Value) -> Option { - match v { - Value::Integer(i) => Some(*i), - _ => None, - } -} - -fn curves_params(f: &Descriptor) -> Option { - let mut out = Map::new(); - for a in adjustment_list(f)? { - let key = channel_key(a)?; - // A pencil-drawn curve is a mapping of 256 output levels (`Mpng`) instead of points. - if let Some(Value::List(map)) = a.get("Mpng") { - let map: Option> = map.iter().map(|v| int_value(v).map(|o| json!(o.clamp(0, 255)))).collect(); - out.insert(if key.is_empty() { "points".into() } else { key.into() }, J::Array(map.filter(|m| m.len() == 256)?)); - out.insert("pencil".into(), J::Bool(true)); - continue; - } - let Some(Value::List(pts)) = a.get("Crv ") else { return None }; - let pts: Option> = pts - .iter() - .map(|p| match p { - Value::Descriptor(p) => Some(json!([px(p, "Hrzn")?, px(p, "Vrtc")?])), - _ => None, - }) - .collect(); - out.insert(if key.is_empty() { "points".into() } else { key.into() }, J::Array(pts?)); - } - Some(J::Object(out)) -} - -/// Our command and parameters for a known Photoshop filter; `None` when the filter (or one of its -/// settings) is not modelled. -fn known_params(class: &str, f: Option<&Descriptor>) -> Option<(&'static str, J)> { - let k = KNOWN.iter().find(|k| k.class == class)?; - let params = match (class, f) { - ("adaptCorrect", None) => json!({}), - (_, None) => return None, - ("GsnB" | "boxblur" | "Mdn " | "HghP" | "Mxm " | "Mnm ", Some(f)) => json!({"radius": px(f, "Rds ")?}), - ("MtnB", Some(f)) => json!({"angle": int(f, "Angl")?, "distance": px(f, "Dstn")?}), - ("UnsM", Some(f)) => json!({"amount": px(f, "Amnt")?, "radius": px(f, "Rds ")?, "threshold": int(f, "Thsh")?}), - ("AdNs", Some(f)) => { - let dist = match enum_of(f, "Dstr")? { - b"Unfr" => "uniform", - b"Gsn " => "gaussian", - _ => return None, - }; - let mono = match f.get("Mnch") { - Some(Value::Boolean(b)) => *b, - _ => return None, - }; - json!({"amount": px(f, "Nose")?, "distribution": dist, "monochromatic": mono, "seed": int(f, "FlRs").unwrap_or(0)}) - } - ("Msc ", Some(f)) => json!({"cellSize": px(f, "ClSz")?}), - ("Embs", Some(f)) => json!({"angle": int(f, "Angl")?, "height": int(f, "Hght")?, "amount": int(f, "Amnt")?}), - ("Lvls", Some(f)) => levels_params(f)?, - ("Crvs", Some(f)) => curves_params(f)?, - _ => return None, - }; - Some((k.command, params)) -} - -// ---------- Filter Gallery ---------- -// -// Filter › Filter Gallery stores its effect stack in a `GEfc` filter (filterID `GEfc`). Photoshop -// writes a single effect inline; Photopea writes a list (also for one effect): -// -// ```text -// Fltr = GEfc { GEfk = GEft enum (the effect), } (Photoshop) -// Fltr = GEfc { GEfs = [ GEfc { , GEfk = GEft enum, GELv = visible } ] } (Photopea) -// ``` -// -// The list is in application order, like `filter.filterGallery`'s `effects`. The item's -// `FrgC`/`BckC` are the colours the Sketch effects read. Only effects whose settings we know are -// mapped; a stack with any other effect (or setting) stays an [`UNSUPPORTED_FILTER`]. - -const GALLERY_CLASS: &str = "GEfc"; -const GALLERY_COMMAND: &str = "filter.filterGallery"; -const GALLERY_NAME: &str = "Filter Gallery..."; - -/// One gallery effect: our filter key, Photoshop's `GEft` code and its settings (PSD key, our key). -struct GalleryCode { - key: &'static str, - code: &'static str, - settings: &'static [(&'static str, &'static str)], -} - -const GALLERY_EFFECTS: [GalleryCode; 1] = [GalleryCode { key: "chrome", code: "Chrm", settings: &[("Dtl ", "detail"), ("Smth", "smoothness")] }]; - -/// An `RGBC` descriptor (0–255 channels) as straight `[r, g, b, a]`, 0–1. -fn rgbc_colour(d: &Descriptor) -> Option { - let c = |k: &str| px(d, k).map(|v| (v / 255.0).clamp(0.0, 1.0)); - Some(json!([c("Rd ")?, c("Grn ")?, c("Bl ")?, 1.0])) -} - -/// `filter.filterGallery` params for a `GEfc` filter item; `None` when an effect or setting is -/// not modelled. -fn gallery_params(item: &Descriptor, f: &Descriptor) -> Option { - let list: Vec<&Descriptor> = match f.get("GEfs") { - Some(Value::List(list)) => list - .iter() - .map(|v| match v { - Value::Descriptor(e) => Some(e), - _ => None, - }) - .collect::>()?, - Some(_) => return None, - None if f.get("GEfk").is_some() => vec![f], - None => return None, - }; - let mut effects = Vec::with_capacity(list.len()); - for e in list { - let code = enum_of(e, "GEfk")?; - let g = GALLERY_EFFECTS.iter().find(|g| g.code.as_bytes() == code)?; - let mut params = Map::new(); - for (k, v) in &e.items { - if k.is("GEfk") || k.is("GELv") { - continue; - } - let (_, ours) = g.settings.iter().find(|(psd, _)| k.is(psd))?; - params.insert((*ours).into(), json!(int_value(v)?)); - } - effects.push(json!({"filter": g.key, "params": params, "visible": bool_or(e, "GELv", true)})); - } - if effects.is_empty() { - return None; - } - let mut out = Map::new(); - out.insert("effects".into(), J::Array(effects)); - for (psd, ours) in [("FrgC", "foreground"), ("BckC", "background")] { - if let Some(c) = get_desc(item, psd).and_then(rgbc_colour) { - out.insert(ours.into(), c); - } - } - Some(J::Object(out)) -} - -fn colour_rgb(v: &J) -> Option<[f64; 3]> { - match v { - J::Array(a) => Some([a.first()?.as_f64()?, a.get(1)?.as_f64()?, a.get(2)?.as_f64()?]), - J::String(h) if h.len() == 7 && h.starts_with('#') => { - let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map(|v| f64::from(v) / 255.0); - Some([ch(1)?, ch(3)?, ch(5)?]) - } - _ => None, - } -} - -/// A `[r, g, b(, a)]` (0–1) or `#rrggbb` colour param as an `RGBC` descriptor (0–255). -fn colour_rgbc(v: Option<&J>, default: f64) -> Result { - let Some(v) = v else { return Ok(rgbc(default)) }; - let rgb = colour_rgb(v).filter(|c| c.iter().all(|v| v.is_finite())).ok_or("a colour is not [r, g, b] or #rrggbb")?; - let c = |v: f64| Value::Double(v.clamp(0.0, 1.0) * 255.0); - Ok(Value::Descriptor(Descriptor::new("RGBC").with("Rd ", c(rgb[0])).with("Grn ", c(rgb[1])).with("Bl ", c(rgb[2])))) -} - -/// The `filterFXList` item for a `filter.filterGallery` smart filter. -fn gallery_item(f: &SmartFilter) -> Result { - let p = &f.params; - if let Some(k) = p.as_object().and_then(|o| o.keys().find(|k| !is_meta(k) && !["effects", "foreground", "background"].contains(&k.as_str()))) { - return Err(format!("setting `{k}` has no PSD equivalent")); - } - let list = p.get("effects").and_then(J::as_array).ok_or("it has no effects")?; - let mut out = Vec::with_capacity(list.len()); - for e in list { - let key = e.get("filter").and_then(J::as_str).ok_or("an effect has no `filter`")?; - let g = GALLERY_EFFECTS.iter().find(|g| g.key == key).ok_or_else(|| format!("gallery effect `{key}` has no PSD equivalent"))?; - let params = e.get("params").filter(|v| v.is_object()).unwrap_or(e); - if let Some(k) = params.as_object().and_then(|o| { - o.keys().find(|k| !is_meta(k) && !["filter", "params", "visible"].contains(&k.as_str()) && !g.settings.iter().any(|(_, ours)| ours == k)) - }) { - return Err(format!("gallery setting `{key}.{k}` has no PSD equivalent")); - } - let defaults = photocraft_algo::GalleryFilter::from_key(key).map(photocraft_algo::GalleryEffect::new); - let settings: Vec<(&str, Value)> = - g.settings.iter().map(|(psd, ours)| (*psd, pi(params, ours, defaults.as_ref().map_or(0.0, |x| f64::from(x.get(ours)))))).collect(); - out.push((g.code, settings, e.get("visible").and_then(J::as_bool).unwrap_or(true))); - } - let gallery = || { - let mut d = Descriptor::new(GALLERY_CLASS); - d.name = UnicodeString::new_nul("Filter Gallery"); - d - }; - // One visible effect: inline, as Photoshop writes it. Otherwise the effect list. - let fltr = match out.as_slice() { - [] => return Err("it has no effects".into()), - [(code, settings, true)] => settings.iter().fold(gallery().with("GEfk", enumv("GEft", code)), |d, (k, v)| d.with(k, v.clone())), - _ => { - let list = out - .into_iter() - .map(|(code, settings, visible)| { - let d = settings.into_iter().fold(gallery(), |d, (k, v)| d.with(k, v)); - Value::Descriptor(d.with("GEfk", enumv("GEft", code)).with("GELv", Value::Boolean(visible))) - }) - .collect(); - gallery().with("GEfs", Value::List(list)) - } - }; - Ok(Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul(GALLERY_NAME))) - .with("blendOptions", blend_options(f)) - .with("enab", Value::Boolean(f.visible)) - .with("hasoptions", Value::Boolean(true)) - .with("FrgC", colour_rgbc(p.get("foreground"), 0.0)?) - .with("BckC", colour_rgbc(p.get("background"), 255.0)?) - .with("Fltr", Value::Descriptor(fltr)) - .with("filterID", Value::Integer(code(b"GEfc")))) -} - -/// Lowercase hex of `bytes`. -fn to_hex(bytes: &[u8]) -> String { - const H: &[u8; 16] = b"0123456789abcdef"; - let mut s = String::with_capacity(bytes.len() * 2); - for b in bytes { - s.push(char::from(H[usize::from(b >> 4)])); - s.push(char::from(H[usize::from(b & 15)])); - } - s -} - -fn from_hex(s: &str) -> Option> { - let d = |c: u8| (c as char).to_digit(16).map(|v| v as u8); - let b = s.as_bytes(); - if !b.len().is_multiple_of(2) { - return None; - } - b.as_chunks::<2>().0.iter().map(|c| Some(d(c[0])? << 4 | d(c[1])?)).collect() -} - -/// One `filterFXList` item → [`SmartFilter`] (an [`UNSUPPORTED_FILTER`] when not modelled). -pub fn filter_from_item(item: &Descriptor) -> SmartFilter { - let opts = get_desc(item, "blendOptions"); - let opacity = opts.and_then(|o| px(o, "Opct")).map_or(1.0, |v| (v / 100.0).clamp(0.0, 1.0) as f32); - let blend = opts.and_then(|o| enum_of(o, "Md ")).and_then(crate::effects_map::blend_from_id).unwrap_or(BlendMode::Normal); - let visible = bool_or(item, "enab", true); - let fltr = get_desc(item, "Fltr"); - let class = fltr.map(|f| String::from_utf8_lossy(f.class_id.as_bytes()).into_owned()); - let filter_id = int(item, "filterID").unwrap_or(0); - let class = class.or_else(|| KNOWN.iter().find(|k| k.filter_id == filter_id).map(|k| k.class.to_string())); - let known = match class.as_deref() { - Some(camera_raw::CLASS) => camera_raw::import_params(item).map(|p| (camera_raw::COMMAND, p)), - Some(displace::CLASS) => displace::import_params(item).map(|p| (displace::COMMAND, p)), - Some(GALLERY_CLASS) => fltr.and_then(|f| gallery_params(item, f)).map(|p| (GALLERY_COMMAND, p)), - c => c.and_then(|c| known_params(c, fltr)), - }; - let (command, params) = match known { - Some((c, p)) => (c.to_string(), p), - None => { - let name = text_of(item, "Nm ").unwrap_or_default(); - // The stored descriptor carries neutral blending (the model's own fields replace - // them on export), so hiding or re-blending the filter leaves its params alone. - let mut neutral = item.clone(); - for (k, v) in &mut neutral.items { - if k.is("blendOptions") { - *v = blend_options(&SmartFilter { command: String::new(), params: J::Null, blend: BlendMode::Normal, opacity: 1.0, visible: true }); - } else if k.is("enab") { - *v = Value::Boolean(true); - } - } - (UNSUPPORTED_FILTER.to_string(), json!({"name": name.replace('&', ""), "filterID": filter_id, "psd": to_hex(&neutral.to_bytes())})) - } - }; - SmartFilter { command, params, blend, opacity, visible } -} - -// ---------- writing ---------- - -fn unit(u: &[u8; 4], v: f64) -> Value { - Value::UnitFloat { unit: *u, value: v } -} - -fn rgbc(v: f64) -> Value { - Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))) -} - -fn blend_options(f: &SmartFilter) -> Value { - let opacity = if f.opacity.is_finite() { f64::from(f.opacity.clamp(0.0, 1.0)) * 100.0 } else { 100.0 }; - Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", opacity)).with("Md ", crate::effects_map::blend_long_value(f.blend))) -} - -/// Parameter keys that are bookkeeping, not filter settings. -fn is_meta(k: &str) -> bool { - k == "layer" || k.starts_with("__") -} - -fn pf(p: &J, key: &str, default: f64) -> f64 { - p.get(key).and_then(J::as_f64).filter(|v| v.is_finite()).unwrap_or(default) -} - -fn pi(p: &J, key: &str, default: f64) -> Value { - Value::Integer(pf(p, key, default).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32) -} - -fn channel_ref(code: &[u8; 4]) -> Value { - Value::Reference(vec![ReferenceItem::Enumerated { - class: Class { name: UnicodeString::new_nul(""), class_id: Id::new("Chnl") }, - type_id: Id::new("Chnl"), - value: Id::Code(*code), - }]) -} - -/// The `Adjs` entries of a Levels or Curves filter: the composite first, then red, green, blue. -fn adjustments(p: &J, curves: bool) -> Result { - let obj = p.as_object().ok_or("parameters are not an object")?; - let allowed: &[&str] = - if curves { &["points", "red", "green", "blue", "pencil"] } else { &["inBlack", "inWhite", "gamma", "outBlack", "outWhite", "red", "green", "blue"] }; - if let Some(k) = obj.keys().find(|k| !is_meta(k) && !allowed.contains(&k.as_str())) { - return Err(format!("setting `{k}` has no PSD equivalent")); - } - let mut list = Vec::new(); - for (code, key) in CHANNELS { - let o = if key.is_empty() { Some(p) } else { p.get(key) }; - let Some(o) = o else { continue }; - let mut a = Descriptor::new(if curves { "CrvA" } else { "LvlA" }).with("Chnl", channel_ref(code)); - if curves { - let src = if key.is_empty() { p.get("points") } else { Some(o) }; - let Some(pts) = src.and_then(J::as_array) else { continue }; - if p.get("pencil").and_then(J::as_bool) == Some(true) { - let map: Option> = - pts.iter().map(|o| o.as_f64().filter(|o| o.is_finite()).map(|o| Value::Integer(o.round().clamp(0.0, 255.0) as i32))).collect(); - a = a.with("Mpng", Value::List(map.filter(|m| m.len() == 256).ok_or("a pencil-drawn curve is not 256 output levels")?)); - list.push(Value::Descriptor(a)); - continue; - } - let pts: Option> = pts - .iter() - .map(|q| { - let x = q.get(0)?.as_f64().filter(|v| v.is_finite())?; - let y = q.get(1)?.as_f64().filter(|v| v.is_finite())?; - Some(Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", Value::Double(x)).with("Vrtc", Value::Double(y)))) - }) - .collect(); - a = a.with("Crv ", Value::List(pts.ok_or("a curve point is not [input, output]")?)); - } else { - if key.is_empty() && !["inBlack", "inWhite", "gamma", "outBlack", "outWhite"].iter().any(|k| o.get(k).is_some()) { - continue; - } - a = a.with("Inpt", Value::List(vec![pi(o, "inBlack", 0.0), pi(o, "inWhite", 255.0)])).with("Gmm ", Value::Double(pf(o, "gamma", 1.0))); - if o.get("outBlack").is_some() || o.get("outWhite").is_some() { - a = a.with("Otpt", Value::List(vec![pi(o, "outBlack", 0.0), pi(o, "outWhite", 255.0)])); - } - } - list.push(Value::Descriptor(a)); - } - Ok(Value::List(list)) -} - -/// The `Fltr` descriptor of a modelled filter (`None` = written without options). -fn fltr_for(k: &Known, p: &J) -> Result, String> { - let d = Descriptor::new(k.class); - Ok(Some(match k.class { - "GsnB" | "boxblur" | "Mdn " | "HghP" | "Mxm " | "Mnm " => { - if p.get("preserve").and_then(J::as_str).is_some_and(|s| s == "roundness") { - return Err("“preserve roundness” has no PSD equivalent".into()); - } - d.with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0))) - } - "MtnB" => d.with("Angl", pi(p, "angle", 0.0)).with("Dstn", unit(b"#Pxl", pf(p, "distance", 10.0))), - "UnsM" => d.with("Amnt", unit(b"#Prc", pf(p, "amount", 50.0))).with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0))).with("Thsh", pi(p, "threshold", 0.0)), - "AdNs" => { - let gauss = p.get("distribution").and_then(J::as_str) == Some("gaussian"); - d.with("Dstr", Value::Enumerated { type_id: Id::new("Dstr"), value: Id::new(if gauss { "Gsn " } else { "Unfr" }) }) - .with("Nose", unit(b"#Prc", pf(p, "amount", 12.5))) - .with("Mnch", Value::Boolean(p.get("monochromatic").and_then(J::as_bool).unwrap_or(false))) - .with("FlRs", pi(p, "seed", 0.0)) - } - "Msc " => d.with("ClSz", unit(b"#Pxl", pf(p, "cellSize", 10.0))), - "Embs" => d.with("Angl", pi(p, "angle", 135.0)).with("Hght", pi(p, "height", 3.0)).with("Amnt", pi(p, "amount", 100.0)), - "Lvls" => d.with("Adjs", adjustments(p, false)?), - "Crvs" => d.with("Adjs", adjustments(p, true)?), - "adaptCorrect" => { - if p.as_object().is_some_and(|o| o.keys().any(|k| !is_meta(k))) { - return Err("only default Shadows/Highlights settings are written to PSD".into()); - } - return Ok(None); - } - _ => return Err("not a recognized filter".into()), - })) -} - -/// The `filterFXList` item for one smart filter, or why it can't be written. -pub fn item_for_filter(f: &SmartFilter) -> Result { - if f.command == camera_raw::COMMAND { - let mut d = camera_raw::export_item(&f.params)?; - set(&mut d, "blendOptions", blend_options(f)); - set(&mut d, "enab", Value::Boolean(f.visible)); - return Ok(d); - } - if f.command == displace::COMMAND { - let mut d = displace::export_item(&f.params)?; - set(&mut d, "blendOptions", blend_options(f)); - set(&mut d, "enab", Value::Boolean(f.visible)); - return Ok(d); - } - if f.command == GALLERY_COMMAND { - return gallery_item(f); - } - if f.command == UNSUPPORTED_FILTER { - let raw = f.params.get("psd").and_then(J::as_str).and_then(from_hex).ok_or("its filter data is missing")?; - let mut d = Descriptor::from_bytes(&raw).map_err(|e| format!("its filter data is unreadable ({e})"))?; - set(&mut d, "blendOptions", blend_options(f)); - set(&mut d, "enab", Value::Boolean(f.visible)); - return Ok(d); - } - let k = known_by_command(&f.command).ok_or("this filter has no PSD equivalent")?; - let fltr = fltr_for(k, &f.params)?; - let mut d = Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul(k.name))) - .with("blendOptions", blend_options(f)) - .with("enab", Value::Boolean(f.visible)) - .with("hasoptions", Value::Boolean(fltr.is_some())) - .with("FrgC", rgbc(0.0)) - .with("BckC", rgbc(255.0)); - if let Some(x) = fltr { - d = d.with("Fltr", Value::Descriptor(x)); - } - Ok(d.with("filterID", Value::Integer(k.filter_id))) -} - -/// Builds a `filterFX` object; filters that can't be written are left out, with a message each. -pub fn filter_fx(stack: &FilterStack, layer: &str, warnings: &mut Vec) -> Descriptor { - let mut list = Vec::new(); - for f in &stack.filters { - match item_for_filter(f) { - Ok(d) => list.push(Value::Descriptor(d)), - Err(e) => warnings.push(format!("layer \"{layer}\": smart filter {} was not written to PSD: {e}", f.command)), - } - } - Descriptor::new("filterFXStyle") - .with("enab", Value::Boolean(stack.enabled)) - .with("validAtPosition", Value::Boolean(true)) - .with("filterMaskEnable", Value::Boolean(stack.mask_enabled)) - .with("filterMaskLinked", Value::Boolean(stack.mask_linked)) - .with("filterMaskExtendWithWhite", Value::Boolean(true)) - .with("filterFXList", Value::List(list)) -} - -/// Replaces item `key` in place, or appends it. -fn set(d: &mut Descriptor, key: &str, v: Value) { - match d.items.iter_mut().find(|(k, _)| k.is(key)) { - Some(e) => e.1 = v, - None => d.items.push((Id::new(key), v)), - } -} - -/// Everything needed to write the placed-layer blocks of a smart object. -#[derive(Clone, Debug)] -pub struct PlacedSpec<'a> { - pub idnt: &'a str, - pub placed: &'a str, - /// Source pixels → document pixels. - pub transform: Affine, - /// Distort / Perspective: the full projective map (row-major 3×3), overriding `transform`. - pub perspective: Option<[f64; 9]>, - /// Source size in pixels. - pub size: (f64, f64), - /// Source resolution. - pub dpi: f64, - pub warp: Option<&'a Warp>, - /// `filterFX`, when the smart object has filters. - pub filter_fx: Option, -} - -/// Where the frame corners land (top-left, top-right, bottom-right, bottom-left), as x, y pairs. -/// The frame is [`crate::blocks::placed_frame`] for an affine placement (the inverse of -/// `parse_smart`), the source rect for a projective one (as `parse_smart_perspective` reads it). -fn quad_points(s: &PlacedSpec<'_>) -> [f64; 8] { - let (w, h) = s.size; - let [a, b, c, d, e, f] = s.transform.m; - let map = |x: f64, y: f64| match &s.perspective { - Some(p) => photocraft_algo::transform::Homography(*p).apply(x, y), - None => (a * x + c * y + e, b * x + d * y + f), - }; - let [x0, y0, x1, y1] = if s.perspective.is_some() { [0.0, 0.0, w, h] } else { crate::blocks::placed_frame(s.warp, w, h) }; - let mut out = [0.0; 8]; - for (i, (x, y)) in [(x0, y0), (x1, y0), (x1, y1), (x0, y1)].into_iter().enumerate() { - (out[2 * i], out[2 * i + 1]) = map(x, y); - } - out -} - -/// The transform quad: the source corners (top-left, top-right, bottom-right, bottom-left). -fn quad(s: &PlacedSpec<'_>) -> Value { - Value::List(quad_points(s).into_iter().map(Value::Double).collect()) -} - -/// Has the placement moved from the quad a template descriptor stores (`Trnf`)? -fn quad_moved(template: &Descriptor, s: &PlacedSpec<'_>) -> bool { - let Some(Value::List(pts)) = template.get("Trnf") else { return true }; - let old: Vec = pts.iter().filter_map(|v| crate::blocks::num(Some(v))).collect(); - let new = quad_points(s); - old.len() != 8 || old.iter().zip(new).any(|(a, b)| (a - b).abs() > 1e-6 * (1.0 + a.abs())) -} - -fn enumv(t: &str, v: &str) -> Value { - Value::Enumerated { type_id: Id::new(t), value: Id::new(v) } -} - -/// The `warp` descriptor (also the `PlLd` warp). Multi-patch meshes have no 1 × 1 envelope and -/// are written as no warp, with a message. -fn warp_desc(w: Option<&Warp>, size: (f64, f64), warnings: &mut Vec) -> Value { - let none = Warp::none([0.0, 0.0, size.0, size.1]); - let mut w = w.unwrap_or(&none).clone(); - let mesh = match (&w.style, &w.mesh) { - (WarpStyle::Custom, Some(m)) if m.points.len() == 16 => Some(m.clone()), - (WarpStyle::Custom, _) => { - warnings.push("a custom warp with more than one patch was written to PSD without its warp".into()); - w = none.clone(); - None - } - _ => None, - }; - let [x0, y0, x1, y1] = w.bounds; - let bounds = Descriptor::new("classFloatRect") - .with("Top ", Value::Double(y0)) - .with("Left", Value::Double(x0)) - .with("Btom", Value::Double(y1)) - .with("Rght", Value::Double(x1)); - let mut d = Descriptor::new("warp") - .with("warpStyle", enumv("warpStyle", w.style.psd_name())) - .with("warpValue", Value::Double(w.bend)) - .with("warpPerspective", Value::Double(w.h_distort)) - .with("warpPerspectiveOther", Value::Double(w.v_distort)) - .with("warpRotate", enumv("Ornt", if w.vertical { "Vrtc" } else { "Hrzn" })) - .with("bounds", Value::Descriptor(bounds)) - .with("uOrder", Value::Integer(4)) - .with("vOrder", Value::Integer(4)); - if let Some(m) = mesh { - let body = Descriptor::new("rationalPoint") - .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[0]).collect() }) - .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[1]).collect() }); - d = d.with( - "customEnvelopeWarp", - Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", Value::ObjectArray(ObjectArray { prefix: 16, body }))), - ); - } - Value::Descriptor(d) -} - -fn ratio(n: i32, d: i32) -> Value { - Value::Descriptor(Descriptor::new("null").with("numerator", Value::Integer(n)).with("denominator", Value::Integer(d))) -} - -fn pad4(mut v: Vec) -> Vec { - while !v.len().is_multiple_of(4) { - v.push(0); - } - v -} - -/// `SoLd` block data. With `template` (the imported descriptor) every key it has is kept and only -/// the placed id (for a duplicate), transform (when it moved), warp and filter stack are rewritten; -/// otherwise a fresh descriptor in Photoshop's key order. -pub fn sold_bytes(template: Option<&Descriptor>, s: &PlacedSpec<'_>, warnings: &mut Vec) -> Vec { - let warp = warp_desc(s.warp, s.size, warnings); - let d = match template { - Some(t) => { - let mut d = t.clone(); - if text_of(t, "placed").as_deref() != Some(s.placed) { - set(&mut d, "placed", Value::Text(UnicodeString::new_nul(s.placed))); - } - if quad_moved(t, s) { - set(&mut d, "Trnf", quad(s)); - set(&mut d, "nonAffineTransform", quad(s)); - } - set(&mut d, "warp", warp); - match &s.filter_fx { - Some(fx) => set(&mut d, "filterFX", Value::Descriptor(fx.clone())), - None => d.items.retain(|(k, _)| !k.is("filterFX")), - } - d - } - None => { - let mut d = Descriptor::new("null") - .with("Idnt", Value::Text(UnicodeString::new_nul(s.idnt))) - .with("placed", Value::Text(UnicodeString::new_nul(s.placed))) - .with("PgNm", Value::Integer(1)) - .with("totalPages", Value::Integer(1)) - .with("Crop", Value::Integer(1)) - .with("frameStep", ratio(0, 600)) - .with("duration", ratio(0, 600)) - .with("frameCount", Value::Integer(1)) - .with("Annt", Value::Integer(16)) - .with("Type", Value::Integer(2)) - .with("Trnf", quad(s)) - .with("nonAffineTransform", quad(s)) - .with("warp", warp) - .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(s.size.0)).with("Hght", Value::Double(s.size.1)))) - .with("Rslt", unit(b"#Rsl", s.dpi)); - if let Some(fx) = &s.filter_fx { - d = d.with("filterFX", Value::Descriptor(fx.clone())); - } - d.with("comp", Value::Integer(-1)) - .with("compInfo", Value::Descriptor(Descriptor::new("null").with("compID", Value::Integer(-1)).with("originalCompID", Value::Integer(-1)))) - } - }; - let mut out = b"soLD".to_vec(); - out.extend_from_slice(&4u32.to_be_bytes()); - out.extend(VersionedDescriptor::new(d).to_bytes()); - pad4(out) -} - -/// `PlLd` block data (`plcL` version 3): the legacy placed-layer record Photoshop writes next to -/// `SoLd`. -pub fn plld_bytes(s: &PlacedSpec<'_>, warnings: &mut Vec) -> Vec { - let mut out = b"plcL".to_vec(); - out.extend_from_slice(&3u32.to_be_bytes()); - let id = s.idnt.as_bytes(); - let n = id.len().min(255); - out.push(n as u8); - out.extend_from_slice(&id[..n]); - for v in [1i32, 1, 16, 2] { - out.extend_from_slice(&v.to_be_bytes()); - } - if let Value::List(q) = quad(s) { - for v in q { - if let Value::Double(x) = v { - out.extend_from_slice(&x.to_be_bytes()); - } - } - } - out.extend_from_slice(&0u32.to_be_bytes()); - let Value::Descriptor(w) = warp_desc(s.warp, s.size, warnings) else { return pad4(out) }; - out.extend(VersionedDescriptor::new(w).to_bytes()); - pad4(out) -} - -/// The source size a `soLD` descriptor stores (`Sz `). -pub fn stored_size(d: &Descriptor) -> Option<(f64, f64)> { - let sz = get_desc(d, "Sz ")?; - Some((num(sz.get("Wdth"))?, num(sz.get("Hght"))?)).filter(|(w, h)| *w > 0.0 && *h > 0.0) -} - // ---------- the filter mask (`FEid`) ---------- /// The filter mask stored in an `FEid` item, in document coordinates and white beyond its bounds @@ -1020,457 +314,4 @@ pub fn uuid_from(seed: &[u8]) -> String { } #[cfg(test)] -mod tests { - use super::*; - - fn count(hay: &[u8], needle: &[u8]) -> usize { - hay.windows(needle.len()).filter(|w| *w == needle).count() - } - - /// A freshly built placed layer writes `warp` (key and class) as a length-4 string, as - /// Photoshop does; the code form is a different key to Photoshop. - #[test] - fn fresh_placed_warp_is_a_string_id() { - let spec = - PlacedSpec { idnt: "id", placed: "id", transform: Affine::IDENTITY, perspective: None, size: (10.0, 10.0), dpi: 72.0, warp: None, filter_fx: None }; - let mut warnings = Vec::new(); - let sold = sold_bytes(None, &spec, &mut warnings); - let plld = plld_bytes(&spec, &mut warnings); - // `SoLd`: the `warp` key and the warp descriptor's class; `PlLd`: the descriptor's class. - for (bytes, n) in [(&sold, 2), (&plld, 1)] { - assert_eq!(count(bytes, b"\0\0\0\x04warp"), n); - assert_eq!(count(bytes, b"\0\0\0\0warp"), 0); - } - let d = sold_descriptor(&sold).unwrap(); - assert!(matches!(d.get("warp"), Some(Value::Descriptor(w)) if w.class_id == Id::Str(b"warp".to_vec()))); - } - - fn sf(command: &str, params: J) -> SmartFilter { - SmartFilter { command: command.into(), params, blend: BlendMode::Normal, opacity: 1.0, visible: true } - } - - #[test] - fn modelled_filters_round_trip() { - let filters = vec![ - sf("filter.blur.gaussianBlur", json!({"radius": 4.0})), - sf("filter.blur.boxBlur", json!({"radius": 5.0})), - sf("filter.blur.motionBlur", json!({"angle": -20, "distance": 10.0})), - sf("filter.sharpen.unsharpMask", json!({"amount": 150.0, "radius": 2.0, "threshold": 0})), - sf("filter.noise.addNoise", json!({"amount": 12.0, "distribution": "uniform", "monochromatic": false, "seed": 1234})), - sf("filter.noise.median", json!({"radius": 3.0})), - sf("filter.other.highPass", json!({"radius": 3.0})), - sf("filter.other.maximum", json!({"radius": 2.0})), - sf("filter.other.minimum", json!({"radius": 2.0})), - sf("filter.pixelate.mosaic", json!({"cellSize": 8.0})), - sf("filter.stylize.emboss", json!({"angle": 135, "height": 3, "amount": 100})), - sf("image.adjustments.levels", json!({"inBlack": 20, "inWhite": 220, "gamma": 1.4, "red": {"inBlack": 0, "inWhite": 255, "gamma": 0.8}})), - sf("image.adjustments.curves", json!({"points": [[0.0, 0.0], [64.0, 40.0], [255.0, 255.0]], "blue": [[0.0, 20.0], [255.0, 235.0]]})), - sf("image.adjustments.shadowsHighlights", json!({})), - // A pencil-drawn curve: 256 output levels (`Mpng`). - sf("image.adjustments.curves", json!({"pencil": true, "points": (0..256).rev().collect::>()})), - ]; - for mut f in filters { - f.blend = BlendMode::Overlay; - f.opacity = 0.75; - f.visible = false; - let d = item_for_filter(&f).unwrap(); - let bytes = d.to_bytes(); - let back = filter_from_item(&Descriptor::from_bytes(&bytes).unwrap()); - assert_eq!(back, f, "{}", f.command); - } - } - - #[test] - fn unknown_filters_keep_their_descriptor() { - let item = Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul("Twirl..."))) - .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) - .with("enab", Value::Boolean(true)) - .with("Fltr", Value::Descriptor(Descriptor::new("Twrl").with("Angl", Value::Integer(50)))) - .with("filterID", Value::Integer(code(b"Twrl"))); - let f = filter_from_item(&item); - assert_eq!(f.command, UNSUPPORTED_FILTER); - assert_eq!(f.params["name"], "Twirl..."); - assert_eq!(item_for_filter(&f).unwrap(), item); - // Hidden and re-blended: only those keys change. - let mut g = f.clone(); - g.visible = false; - g.blend = BlendMode::Screen; - let back = filter_from_item(&item_for_filter(&g).unwrap()); - assert_eq!((back.visible, back.blend, &back.params), (false, BlendMode::Screen, &f.params)); - // A known filter with a setting we don't model is kept verbatim too. - let odd = Descriptor::new("filterFX").with("Fltr", Value::Descriptor(Descriptor::new("GsnB"))).with("filterID", Value::Integer(code(b"GsnB"))); - assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); - } - - /// A Filter Gallery item as Photopea writes it (Chrome, Detail 12, Smoothness 4). - fn gallery_item_with(effects: Vec) -> Descriptor { - let rgb = - |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); - Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul("Filtergalerie"))) - .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) - .with("enab", Value::Boolean(true)) - .with("hasoptions", Value::Boolean(true)) - .with("FrgC", rgb(0.0)) - .with("BckC", rgb(255.0)) - .with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::List(effects.into_iter().map(Value::Descriptor).collect())))) - .with("filterID", Value::Integer(code(b"GEfc"))) - } - - fn chrome(detail: i32, smoothness: i32, visible: bool) -> Descriptor { - Descriptor::new("GEfc") - .with("Dtl ", Value::Integer(detail)) - .with("Smth", Value::Integer(smoothness)) - .with("GEfk", enumv("GEft", "Chrm")) - .with("GELv", Value::Boolean(visible)) - } - - #[test] - fn filter_gallery_chrome_imports_as_editable_gallery() { - let f = filter_from_item(&gallery_item_with(vec![chrome(12, 4, true), chrome(3, 9, false)])); - assert_eq!(f.command, "filter.filterGallery"); - assert_eq!( - f.params, - json!({ - "effects": [ - {"filter": "chrome", "params": {"detail": 12, "smoothness": 4}, "visible": true}, - {"filter": "chrome", "params": {"detail": 3, "smoothness": 9}, "visible": false}, - ], - "foreground": [0.0, 0.0, 0.0, 1.0], - "background": [1.0, 1.0, 1.0, 1.0], - }) - ); - // Written back and read again: unchanged, also when re-blended and hidden. - let mut g = f.clone(); - g.blend = BlendMode::Multiply; - g.opacity = 0.5; - g.visible = false; - let d = item_for_filter(&g).unwrap(); - assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), g); - let fltr = get_desc(&d, "Fltr").unwrap(); - assert!(fltr.class_id.is("GEfc")); - assert_eq!(int(&d, "filterID"), Some(code(b"GEfc"))); - } - - /// A single effect as Photoshop 27 writes it: inline in `Fltr`, no `GEfs` list or `GELv`. - fn photoshop_chrome(detail: i32, smoothness: i32) -> Descriptor { - let rgb = - |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); - let mut fltr = - Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Dtl ", Value::Integer(detail)).with("Smth", Value::Integer(smoothness)); - fltr.name = UnicodeString::new_nul("Filter Gallery"); - Descriptor::new("filterFX") - .with("Nm ", Value::Text(UnicodeString::new_nul("Filter Gallery..."))) - .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "Nrml")))) - .with("enab", Value::Boolean(true)) - .with("hasoptions", Value::Boolean(true)) - .with("FrgC", rgb(255.0)) - .with("BckC", rgb(0.0)) - .with("Fltr", Value::Descriptor(fltr)) - .with("filterID", Value::Integer(code(b"GEfc"))) - } - - #[test] - fn filter_gallery_photoshop_inline_effect_imports_and_writes_back_inline() { - let item = photoshop_chrome(10, 4); - let f = filter_from_item(&item); - assert_eq!(f.command, "filter.filterGallery"); - assert_eq!( - f.params, - json!({ - "effects": [{"filter": "chrome", "params": {"detail": 10, "smoothness": 4}, "visible": true}], - "foreground": [1.0, 1.0, 1.0, 1.0], - "background": [0.0, 0.0, 0.0, 1.0], - }) - ); - // Written back in Photoshop's own layout. - let d = item_for_filter(&f).unwrap(); - assert_eq!(get_desc(&d, "Fltr"), get_desc(&item, "Fltr")); - assert_eq!((get_desc(&d, "FrgC"), get_desc(&d, "BckC")), (get_desc(&item, "FrgC"), get_desc(&item, "BckC"))); - assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); - // A hidden single effect needs the list (only it has a visibility flag). - let mut hidden = f.clone(); - hidden.params["effects"][0]["visible"] = json!(false); - let d = item_for_filter(&hidden).unwrap(); - assert!(get_desc(&d, "Fltr").unwrap().get("GEfs").is_some()); - assert_eq!(filter_from_item(&d), hidden); - // Inline effects we don't model stay verbatim. - let mut odd = photoshop_chrome(10, 4); - set(&mut odd, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("Dtl ", Value::Integer(1)))); - assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); - let mut extra = photoshop_chrome(10, 4); - set(&mut extra, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Xtra", Value::Integer(1)))); - assert_eq!(filter_from_item(&extra).command, UNSUPPORTED_FILTER); - } - - #[test] - fn gallery_table_matches_the_gallery_filters() { - for g in &GALLERY_EFFECTS { - let f = photocraft_algo::GalleryFilter::from_key(g.key).unwrap(); - let keys: Vec<&str> = f.params().iter().map(|p| p.key).collect(); - for (_, ours) in g.settings { - assert!(keys.contains(ours), "{}.{ours}", g.key); - } - } - } - - #[test] - fn filter_gallery_written_with_defaults_and_colours() { - let f = sf("filter.filterGallery", json!({"effects": [{"filter": "chrome"}], "foreground": "#ff0000", "background": [0.0, 0.0, 1.0, 1.0]})); - let back = filter_from_item(&item_for_filter(&f).unwrap()); - assert_eq!(back.params["effects"][0]["params"], json!({"detail": 4, "smoothness": 7})); - assert_eq!(back.params["foreground"], json!([1.0, 0.0, 0.0, 1.0])); - assert_eq!(back.params["background"], json!([0.0, 0.0, 1.0, 1.0])); - } - - #[test] - fn filter_gallery_we_dont_model_is_kept_verbatim() { - // Another effect, an extra setting, a wrong-typed setting, no effects, a non-list. - let other = Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("GELv", Value::Boolean(true)); - let extra = chrome(4, 7, true).with("Xtra", Value::Integer(1)); - let typed = Descriptor::new("GEfc").with("Dtl ", Value::Double(4.0)).with("GEfk", enumv("GEft", "Chrm")); - for effects in [vec![chrome(4, 7, true), other], vec![extra], vec![typed], vec![]] { - let item = gallery_item_with(effects); - let f = filter_from_item(&item); - assert_eq!(f.command, UNSUPPORTED_FILTER); - assert_eq!(item_for_filter(&f).unwrap(), item); - } - let odd = Descriptor::new("filterFX").with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::Integer(3)))); - assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); - let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"GEfc"))); - assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); - } - - #[test] - fn filter_gallery_without_psd_equivalent_is_reported() { - for p in [ - json!({"effects": [{"filter": "cutout"}]}), - json!({"effects": [{"filter": "chrome", "params": {"sparkle": 1}}]}), - json!({"effects": [{"params": {}}]}), - json!({"effects": []}), - json!({}), - json!({"effects": [{"filter": "chrome"}], "list": true}), - json!({"effects": [{"filter": "chrome"}], "foreground": "red"}), - json!(null), - ] { - assert!(item_for_filter(&sf("filter.filterGallery", p.clone())).is_err(), "{p}"); - } - } - - #[test] - fn unwritable_filters_are_reported_not_fatal() { - let stack = FilterStack { - filters: vec![ - sf("filter.distort.twirl", json!({"angle": 50})), - sf("filter.blur.gaussianBlur", json!({"radius": 2.0})), - sf(UNSUPPORTED_FILTER, json!({"psd": "zz"})), - sf("image.adjustments.levels", json!({"cyan": {"gamma": 2.0}})), - sf("image.adjustments.shadowsHighlights", json!({"shadowAmount": 50})), - ], - ..Default::default() - }; - let mut w = Vec::new(); - let d = filter_fx(&stack, "L", &mut w); - assert_eq!(w.len(), 4, "{w:?}"); - let back = filter_stack(&d); - assert_eq!(back.filters, vec![stack.filters[1].clone()]); - } - - #[test] - fn placed_blocks_parse_back() { - let t = Affine { m: [0.5, 0.1, -0.1, 0.5, 10.0, 20.0] }; - let stack = - FilterStack { enabled: false, filters: vec![sf("filter.blur.gaussianBlur", json!({"radius": 3.0}))], mask_enabled: false, mask_linked: true }; - let mut w = Vec::new(); - let spec = PlacedSpec { - idnt: "id-1", - placed: "pl-2", - transform: t, - perspective: None, - size: (64.0, 32.0), - dpi: 72.0, - warp: None, - filter_fx: Some(filter_fx(&stack, "L", &mut w)), - }; - let sold = sold_bytes(None, &spec, &mut w); - assert!(w.is_empty()); - assert_eq!(sold.len() % 4, 0); - let p = parse_sold(&sold).unwrap(); - assert_eq!((p.idnt.as_str(), p.placed.as_str()), ("id-1", "pl-2")); - for (a, b) in p.transform.m.iter().zip(t.m) { - assert!((a - b).abs() < 1e-9); - } - assert_eq!(p.stack, Some(stack)); - let tb = photocraft_psd::TaggedBlock::new(*b"SoLd", sold.clone()); - tb.check_structure().unwrap(); - let pl = photocraft_psd::TaggedBlock::new(*b"PlLd", plld_bytes(&spec, &mut w)); - pl.check_structure().unwrap(); - // A template keeps its other keys; an unmoved transform keeps its exact quad. - let mut tmpl = sold_descriptor(&sold).unwrap(); - tmpl.items.push((Id::new("extraKey"), Value::Integer(7))); - let again = sold_bytes(Some(&tmpl), &PlacedSpec { filter_fx: None, ..spec }, &mut w); - let d = sold_descriptor(&again).unwrap(); - assert_eq!(d.get("extraKey"), Some(&Value::Integer(7))); - assert!(d.get("filterFX").is_none()); - assert_eq!(d.get("Trnf"), tmpl.get("Trnf")); - } - - /// A Distort / Perspective placement goes out as its four corners (`Trnf`, `nonAffineTransform`) - /// and comes back as the same projective map; an affine one reads back as no perspective. - #[test] - fn perspective_placement_round_trips_through_sold() { - let h = photocraft_algo::transform::Homography::rect_to_quad([0.0, 0.0, 64.0, 32.0], [[10.0, 5.0], [80.0, 8.0], [70.0, 50.0], [12.0, 40.0]]).unwrap(); - let mut w = Vec::new(); - let mut spec = PlacedSpec { - idnt: "id", - placed: "pl", - transform: Affine::IDENTITY, - perspective: Some(h.0), - size: (64.0, 32.0), - dpi: 72.0, - warp: None, - filter_fx: None, - }; - let sold = sold_bytes(None, &spec, &mut w); - let back = crate::blocks::parse_smart_perspective(b"SoLd", &sold).expect("a projective placement"); - let n = |m: [f64; 9]| m.map(|v| v / m[8]); - for (a, b) in n(back).iter().zip(n(h.0)) { - assert!((a - b).abs() < 1e-9 * (1.0 + b.abs()), "{back:?} vs {:?}", h.0); - } - // Affine placements carry no perspective. - spec.perspective = None; - spec.transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 5.0, 7.0] }; - let sold = sold_bytes(None, &spec, &mut w); - assert_eq!(crate::blocks::parse_smart_perspective(b"SoLd", &sold), None); - assert_eq!(crate::blocks::parse_smart_perspective(b"PlLd", &sold), None); - } - - /// `quad_moved` compares the corners a template stores with the placement's: unchanged keeps - /// the template's quad, a moved corner (affine or projective) or a missing quad rewrites it. - #[test] - fn quad_moved_detects_corner_changes() { - let t = Affine { m: [1.0, 0.0, 0.0, 1.0, 3.0, 4.0] }; - let mut w = Vec::new(); - let mut spec = PlacedSpec { idnt: "id", placed: "pl", transform: t, perspective: None, size: (20.0, 10.0), dpi: 72.0, warp: None, filter_fx: None }; - let template = parse_sold(&sold_bytes(None, &spec, &mut w)).expect("parses").descriptor; - assert!(!quad_moved(&template, &spec), "same placement"); - spec.transform = Affine { m: [1.0, 0.0, 0.0, 1.0, 3.5, 4.0] }; - assert!(quad_moved(&template, &spec), "moved half a pixel"); - spec.transform = t; - spec.perspective = - photocraft_algo::transform::Homography::rect_to_quad([0.0, 0.0, 20.0, 10.0], [[3.0, 4.0], [23.0, 4.0], [20.0, 12.0], [3.0, 14.0]]).map(|h| h.0); - assert!(quad_moved(&template, &spec), "one corner moved (Distort)"); - assert!(quad_moved(&Descriptor::new("null"), &spec), "no stored quad"); - } - - /// Corrupted placed-layer data and filter descriptors never panic: they parse to something - /// (unknown filters stay verbatim) or to nothing. - #[test] - fn mutated_placed_data_never_panics() { - let stack = FilterStack { - filters: vec![ - sf("filter.blur.gaussianBlur", json!({"radius": 3.0})), - sf("image.adjustments.curves", json!({"points": [[0.0, 0.0], [255.0, 255.0]]})), - sf("image.adjustments.levels", json!({"inBlack": 3, "inWhite": 250, "gamma": 1.1})), - ], - ..Default::default() - }; - let mut w = Vec::new(); - let spec = PlacedSpec { - idnt: "id", - placed: "pl", - transform: Affine::IDENTITY, - perspective: None, - size: (8.0, 8.0), - dpi: 72.0, - warp: None, - filter_fx: Some(filter_fx(&stack, "L", &mut w)), - }; - let sold = sold_bytes(None, &spec, &mut w); - let mut state = 0x2545_F491_4F6C_DD1Du64; - let mut next = || { - state ^= state << 13; - state ^= state >> 7; - state ^= state << 17; - state - }; - for _ in 0..3000 { - let mut b = sold.clone(); - for _ in 0..1 + next() % 3 { - let i = (next() as usize) % b.len(); - b[i] = next() as u8; - } - if let Some(p) = parse_sold(&b) { - for f in p.stack.iter().flat_map(|s| &s.filters) { - let _ = item_for_filter(f); - } - let _ = sold_bytes(Some(&p.descriptor), &spec, &mut w); - } - } - } - - #[test] - fn warps_are_written_and_read_back() { - use photocraft_geom::warp::BezierMesh; - let bounds = [0.0, 0.0, 60.0, 30.0]; - let mut custom = BezierMesh::identity(bounds, 1, 1); - custom.points[15] = [67.0, 30.0]; - let mut multi = Warp::preset(WarpStyle::Custom, 0.0, bounds); - multi.mesh = Some(BezierMesh::identity(bounds, 2, 2)); - let mut arc = Warp::preset(WarpStyle::Arc, 40.0, bounds); - arc.vertical = true; - arc.h_distort = 10.0; - let mut c = Warp::preset(WarpStyle::Custom, 0.0, bounds); - c.mesh = Some(custom); - for (w, back) in [(Some(arc.clone()), Some(arc)), (Some(c.clone()), Some(c)), (Some(multi), None), (None, None)] { - let mut warnings = Vec::new(); - let spec = PlacedSpec { - idnt: "i", - placed: "p", - transform: Affine::IDENTITY, - perspective: None, - size: (60.0, 30.0), - dpi: 72.0, - warp: w.as_ref(), - filter_fx: None, - }; - let sold = sold_bytes(None, &spec, &mut warnings); - assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), back); - assert_eq!(warnings.len(), usize::from(w.is_some() && back.is_none())); - } - } - - /// Export writes `Trnf` as the image of the custom warp's control-point box, the inverse of - /// import: a spilled mesh comes back with the same transform and the quad Photoshop expects. - #[test] - fn warp_control_box_quad_round_trips() { - use photocraft_geom::warp::BezierMesh; - let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 1, 1); - mesh.points[0] = [-10.0, -5.0]; - mesh.points[15] = [60.0, 25.0]; - let mut warp = Warp::preset(WarpStyle::Custom, 0.0, [0.0, 0.0, 50.0, 25.0]); - warp.mesh = Some(mesh); - let transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 120.0, 210.0] }; - let spec = PlacedSpec { idnt: "i", placed: "p", transform, perspective: None, size: (50.0, 25.0), dpi: 72.0, warp: Some(&warp), filter_fx: None }; - let sold = sold_bytes(None, &spec, &mut Vec::new()); - let p = parse_sold(&sold).expect("parses"); - let Some(Value::List(q)) = p.descriptor.get("Trnf") else { panic!("no Trnf") }; - let q: Vec = q.iter().filter_map(|v| num(Some(v))).collect(); - assert_eq!(q, [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]); - assert_eq!(p.transform, transform); - // The imported quad reads as unmoved, so the template's `Trnf` is kept as stored. - assert!(!quad_moved(&p.descriptor, &spec)); - } - - #[test] - fn hex_and_uuid() { - assert_eq!(from_hex(&to_hex(&[0, 1, 0xab, 0xff])), Some(vec![0, 1, 0xab, 0xff])); - assert_eq!(from_hex("abc"), None); - assert_eq!(from_hex("zz"), None); - let u = uuid_from(b"x"); - assert_eq!(u.len(), 36); - assert_eq!(u, uuid_from(b"x")); - assert_ne!(u, uuid_from(b"y")); - } -} +mod tests; diff --git a/crates/io/src/smart_map/export.rs b/crates/io/src/smart_map/export.rs new file mode 100644 index 0000000000..e057a7032f --- /dev/null +++ b/crates/io/src/smart_map/export.rs @@ -0,0 +1,391 @@ +use super::import::{ + CHANNELS, CUSTOM_CELL, CUSTOM_OFFSET, CUSTOM_SCALE, DIFFUSE_MODES, EXTRUDE_DEPTH, EXTRUDE_DEPTHS, EXTRUDE_SIZE, EXTRUDE_TYPES, GALLERY_COMMAND, Known, + LENSES, MEZZOTINT_TYPES, NO_OPTIONS, OFFSET_FILL, POLAR_MODE, RADIAL_METHOD, RADIAL_QUALITY, RIPPLE_SIZE, SMART_BLUR_MODE, SMART_BLUR_QUALITY, + SMART_SHARPEN, SPHERIZE_MODE, TILE_COUNT, TILE_FILLS, TILE_OFFSET, TRACE_EDGES, UNDEFINED_AREAS, WAVE_TYPE, WIND_DIRECTIONS, WIND_METHODS, ZIGZAG_STYLE, + colour_rgbc, enum_param, from_hex, gallery_item, known_by_command, pf_range, pi_range, seed_param, +}; +use super::placed::enumv; +use super::{ + Class, Descriptor, FilterStack, Id, J, ReferenceItem, SmartFilter, UNSUPPORTED_FILTER, UnicodeString, Value, camera_raw, displace, hue_sat, + perspective_warp, +}; + +// ---------- writing ---------- + +pub(crate) fn unit(u: &[u8; 4], v: f64) -> Value { + Value::UnitFloat { unit: *u, value: v } +} + +pub(crate) fn rgbc(v: f64) -> Value { + Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))) +} + +pub(crate) fn blend_options(f: &SmartFilter) -> Value { + let opacity = if f.opacity.is_finite() { f64::from(f.opacity.clamp(0.0, 1.0)) * 100.0 } else { 100.0 }; + Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", opacity)).with("Md ", crate::effects_map::blend_long_value(f.blend))) +} + +/// Parameter keys that are bookkeeping, not filter settings. +pub(crate) fn is_meta(k: &str) -> bool { + k == "layer" || k.starts_with("__") +} + +fn pf(p: &J, key: &str, default: f64) -> f64 { + p.get(key).and_then(J::as_f64).filter(|v| v.is_finite()).unwrap_or(default) +} + +pub(super) fn pi(p: &J, key: &str, default: f64) -> Value { + Value::Integer(pf(p, key, default).round().clamp(f64::from(i32::MIN), f64::from(i32::MAX)) as i32) +} + +/// A whole-number setting as a long, clamped to `range` like the engine clamps it; a fraction +/// has no PSD equivalent (Photoshop stores these as integers). +pub(super) fn whole(v: f64, (lo, hi): (i32, i32), what: &str) -> Result { + if v.fract() != 0.0 { + return Err(format!("{what} ({v}) is not a whole number")); + } + Ok(Value::Integer(v.clamp(f64::from(lo), f64::from(hi)) as i32)) +} + +fn channel_ref(code: &[u8; 4]) -> Value { + Value::Reference(vec![ReferenceItem::Enumerated { + class: Class { name: UnicodeString::new_nul(""), class_id: Id::new("Chnl") }, + type_id: Id::new("Chnl"), + value: Id::Code(*code), + }]) +} + +/// The `Adjs` entries of a Levels or Curves filter: the composite first, then red, green, blue. +fn adjustments(p: &J, curves: bool) -> Result { + let obj = p.as_object().ok_or("parameters are not an object")?; + let allowed: &[&str] = + if curves { &["points", "red", "green", "blue", "pencil"] } else { &["inBlack", "inWhite", "gamma", "outBlack", "outWhite", "red", "green", "blue"] }; + if let Some(k) = obj.keys().find(|k| !is_meta(k) && !allowed.contains(&k.as_str())) { + return Err(format!("setting `{k}` has no PSD equivalent")); + } + let mut list = Vec::new(); + for (code, key) in CHANNELS { + let o = if key.is_empty() { Some(p) } else { p.get(key) }; + let Some(o) = o else { continue }; + let mut a = Descriptor::new(if curves { "CrvA" } else { "LvlA" }).with("Chnl", channel_ref(code)); + if curves { + let src = if key.is_empty() { p.get("points") } else { Some(o) }; + let Some(pts) = src.and_then(J::as_array) else { continue }; + if p.get("pencil").and_then(J::as_bool) == Some(true) { + let map: Option> = + pts.iter().map(|o| o.as_f64().filter(|o| o.is_finite()).map(|o| Value::Integer(o.round().clamp(0.0, 255.0) as i32))).collect(); + a = a.with("Mpng", Value::List(map.filter(|m| m.len() == 256).ok_or("a pencil-drawn curve is not 256 output levels")?)); + list.push(Value::Descriptor(a)); + continue; + } + let pts: Option> = pts + .iter() + .map(|q| { + let x = q.get(0)?.as_f64().filter(|v| v.is_finite())?; + let y = q.get(1)?.as_f64().filter(|v| v.is_finite())?; + Some(Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", Value::Double(x)).with("Vrtc", Value::Double(y)))) + }) + .collect(); + a = a.with("Crv ", Value::List(pts.ok_or("a curve point is not [input, output]")?)); + } else { + if key.is_empty() && !["inBlack", "inWhite", "gamma", "outBlack", "outWhite"].iter().any(|k| o.get(k).is_some()) { + continue; + } + a = a.with("Inpt", Value::List(vec![pi(o, "inBlack", 0.0), pi(o, "inWhite", 255.0)])).with("Gmm ", Value::Double(pf(o, "gamma", 1.0))); + if o.get("outBlack").is_some() || o.get("outWhite").is_some() { + a = a.with("Otpt", Value::List(vec![pi(o, "outBlack", 0.0), pi(o, "outWhite", 255.0)])); + } + } + list.push(Value::Descriptor(a)); + } + Ok(Value::List(list)) +} + +/// Crystallize's and Pointillize's cell size range (`ClSz`, a `long`). +pub(super) const CELL_SIZE: (i32, i32) = (3, 300); + +/// Our `key` (a string) as Photoshop's enum value through `table` (Photoshop's value, ours). +fn enum_out(p: &J, key: &str, default: &str, type_id: &str, table: &[(&str, &str)]) -> Result { + let ours = match p.get(key) { + None => default, + Some(v) => v.as_str().ok_or_else(|| format!("`{key}` is not a string"))?, + }; + let (psd, _) = table.iter().find(|(_, o)| *o == ours).ok_or_else(|| format!("{key} `{ours}` has no PSD equivalent"))?; + Ok(enumv(type_id, psd)) +} + +/// `key` rounded to a `long` in `lo..=hi` (clamped, and a non-number is the default, as the +/// engine reads Extrude, Tiles, Trace Contour, Crystallize and Pointillize). +fn pi_in(p: &J, key: &str, default: f64, lo: i32, hi: i32) -> Value { + Value::Integer(pf(p, key, default).round().clamp(f64::from(lo), f64::from(hi)) as i32) +} + +/// Param `key` as a `bool` (false when absent or not a bool, as the engine reads it). +fn bool_param(p: &J, key: &str) -> Value { + Value::Boolean(p.get(key).and_then(J::as_bool).unwrap_or(false)) +} + +/// The `Fltr` descriptor of a modelled filter (`None` = written without options). +fn fltr_for(k: &Known, p: &J) -> Result, String> { + let d = Descriptor::new(k.class); + Ok(Some(match k.class { + "GsnB" | "boxblur" | "Mdn " | "HghP" | "Mxm " | "Mnm " => { + if p.get("preserve").and_then(J::as_str).is_some_and(|s| s == "roundness") { + return Err("“preserve roundness” has no PSD equivalent".into()); + } + d.with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0))) + } + "MtnB" => d.with("Angl", pi(p, "angle", 0.0)).with("Dstn", unit(b"#Pxl", pf(p, "distance", 10.0))), + "UnsM" => d.with("Amnt", unit(b"#Prc", pf(p, "amount", 50.0))).with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0))).with("Thsh", pi(p, "threshold", 0.0)), + "AdNs" => { + let gauss = p.get("distribution").and_then(J::as_str) == Some("gaussian"); + d.with("Dstr", Value::Enumerated { type_id: Id::new("Dstr"), value: Id::new(if gauss { "Gsn " } else { "Unfr" }) }) + .with("Nose", unit(b"#Prc", pf(p, "amount", 12.5))) + .with("Mnch", Value::Boolean(p.get("monochromatic").and_then(J::as_bool).unwrap_or(false))) + .with("FlRs", pi(p, "seed", 0.0)) + } + "surfaceBlur" => d.with("Rds ", unit(b"#Pxl", pf_range(p, "radius", 5.0, 1.0, 100.0)?)).with("Thsh", pi_range(p, "threshold", 15.0, 2, 255)?), + "SmrB" => d + .with("Rds ", Value::Double(pf_range(p, "radius", 3.0, 0.1, 100.0)?)) + .with("Thsh", Value::Double(pf_range(p, "threshold", 25.0, 0.1, 100.0)?)) + .with("SmBQ", enum_param(p, "quality", "high", "SmBQ", &SMART_BLUR_QUALITY)?) + .with("SmBM", enum_param(p, "mode", "normal", "SmBM", &SMART_BLUR_MODE)?), + "RdlB" => { + // Photoshop wrote no centre key for a centred blur; an off-centre one is unmeasured. + for k in ["centerX", "centerY"] { + if (pf_range(p, k, 0.5, 0.0, 1.0)? - 0.5).abs() > 1e-9 { + return Err("an off-centre Radial Blur centre is not written to PSD".into()); + } + } + d.with("Amnt", pi_range(p, "amount", 10.0, 1, 100)?) + .with("BlrM", enum_param(p, "method", "spin", "BlrM", &RADIAL_METHOD)?) + .with("BlrQ", enum_param(p, "quality", "good", "BlrQ", &RADIAL_QUALITY)?) + } + "Msc " => d.with("ClSz", unit(b"#Pxl", pf(p, "cellSize", 10.0))), + // Photoshop's Mezzotint has no seed (it is random on every render), so `seed` is not saved. + "Mztn" => { + let t = p.get("type").and_then(J::as_str).unwrap_or("fineDots"); + let code = MEZZOTINT_TYPES.iter().find(|(_, k)| *k == t).map_or(b"FnDt", |(c, _)| *c); + d.with("MztT", Value::Enumerated { type_id: Id::new("MztT"), value: Id::Code(*code) }) + } + "ClrH" => { + // Longs within the dialog's ranges (what the engine accepts: radius 4..127, angles ±360). + let long = |key: &str, default: f64, lo: f64, hi: f64| Value::Integer(pf(p, key, default).round().clamp(lo, hi) as i32); + d.with("Rds ", long("maxRadius", 8.0, 4.0, 127.0)) + .with("Ang1", long("channel1", 108.0, -360.0, 360.0)) + .with("Ang2", long("channel2", 162.0, -360.0, 360.0)) + .with("Ang3", long("channel3", 90.0, -360.0, 360.0)) + .with("Ang4", long("channel4", 45.0, -360.0, 360.0)) + } + "Embs" => d.with("Angl", pi(p, "angle", 135.0)).with("Hght", pi(p, "height", 3.0)).with("Amnt", pi(p, "amount", 100.0)), + SMART_SHARPEN => { + // The engine's defaults and ranges (it clamps the same way when rendering). + d.with("useLegacy", Value::Boolean(false)) + .with("Amnt", unit(b"#Prc", pf(p, "amount", 100.0).clamp(1.0, 500.0))) + .with("Rds ", unit(b"#Pxl", pf(p, "radius", 1.0).clamp(0.1, 64.0))) + .with("noiseReduction", unit(b"#Prc", pf(p, "reduceNoise", 10.0).clamp(0.0, 100.0))) + .with("blur", enumv("blurType", "GsnB")) + } + // Photoshop stores whole pixels and levels. + "DstS" => { + let whole = |key: &str, default: f64, lo: f64, hi: f64| Value::Integer(pf(p, key, default).round().clamp(lo, hi) as i32); + d.with("Rds ", whole("radius", 1.0, 1.0, 500.0)).with("Thsh", whole("threshold", 0.0, 0.0, 255.0)) + } + // Photoshop stores no random seed for these: `seed` is ours only and is not written. + "Dfs " => d.with("Md ", enum_out(p, "mode", "normal", "DfsM", &DIFFUSE_MODES)?), + "TrcC" => d.with("Lvl ", pi_in(p, "level", 128.0, 0, 255)).with("Edg ", enum_out(p, "edge", "lower", "CntE", &TRACE_EDGES)?), + "Crst" => d.with("ClSz", pi_in(p, "cellSize", 10.0, CELL_SIZE.0, CELL_SIZE.1)), + "Pntl" => d.with("ClSz", pi_in(p, "cellSize", 5.0, CELL_SIZE.0, CELL_SIZE.1)), + // Photoshop stores no random seed for these: our `seed` is not written. + "Wnd " => { + d.with("WndM", enum_out(p, "method", "wind", "WndM", &WIND_METHODS)?).with("Drct", enum_out(p, "direction", "fromRight", "Drct", &WIND_DIRECTIONS)?) + } + "Extr" => d + .with("ExtT", enum_out(p, "type", "blocks", "ExtT", &EXTRUDE_TYPES)?) + .with("ExtS", pi_in(p, "size", 30.0, EXTRUDE_SIZE.0, EXTRUDE_SIZE.1)) + .with("ExtD", pi_in(p, "depth", 30.0, EXTRUDE_DEPTH.0, EXTRUDE_DEPTH.1)) + .with("ExtR", enum_out(p, "depthMode", "random", "ExtR", &EXTRUDE_DEPTHS)?) + .with("ExtF", bool_param(p, "solidFrontFaces")) + .with("ExtM", bool_param(p, "maskIncompleteBlocks")), + "Tls " => d + .with("TlNm", pi_in(p, "count", 10.0, TILE_COUNT.0, TILE_COUNT.1)) + .with("TlOf", pi_in(p, "maxOffset", 10.0, TILE_OFFSET.0, TILE_OFFSET.1)) + .with("FlCl", enum_out(p, "fill", "background", "FlCl", &TILE_FILLS)?), + "Ofst" => { + // As the engine reads it: anything but repeat / transparent wraps. + let ours = p.get("undefinedAreas").and_then(J::as_str).unwrap_or("wrap"); + let fill = OFFSET_FILL.iter().find(|(_, o)| *o == ours).map_or("Wrp ", |(psd, _)| *psd); + d.with("Hrzn", pi(p, "horizontal", 0.0)).with("Vrtc", pi(p, "vertical", 0.0)).with("Fl ", enumv("FlMd", fill)) + } + "Cstm" => { + // The engine's identity kernel when none is given; missing cells are 0. + let kernel = p.get("kernel").and_then(J::as_array); + let cell = |i: usize| match kernel { + Some(a) => a.get(i).and_then(J::as_f64).filter(|v| v.is_finite()).unwrap_or(0.0), + None => f64::from(u8::from(i == 12)), + }; + let m = (0..25).map(|i| whole(cell(i), CUSTOM_CELL, "a kernel value")).collect::, _>>()?; + d.with("Scl ", whole(pf(p, "scale", 1.0), CUSTOM_SCALE, "Scale")?) + .with("Ofst", whole(pf(p, "offset", 0.0), CUSTOM_OFFSET, "Offset")?) + .with("Mtrx", Value::List(m)) + } + "LnsF" => { + let lens = p.get("lens").and_then(J::as_str).unwrap_or("zoom"); + let code = LENSES.iter().find(|(_, l)| *l == lens).ok_or_else(|| format!("lens `{lens}` has no PSD equivalent"))?.0; + let mut centre = Descriptor::new("Pnt ").with("Hrzn", Value::Double(pf(p, "centerX", 0.5))).with("Vrtc", Value::Double(pf(p, "centerY", 0.5))); + centre.name = UnicodeString::new_nul(""); + d.with("Brgh", pi(p, "brightness", 100.0)) + .with("FlrC", Value::Descriptor(centre)) + .with("Lns ", Value::Enumerated { type_id: Id::new("Lns "), value: Id::Code(*code) }) + } + "Fbrs" => d.with("Vrnc", pi(p, "variance", 16.0)).with("Strg", pi(p, "strength", 4.0)).with("RndS", seed_param(p)), + "Clds" | "DfrC" => d.with("FlRs", seed_param(p)), + "Lvls" => d.with("Adjs", adjustments(p, false)?), + "Crvs" => d.with("Adjs", adjustments(p, true)?), + "adaptCorrect" => { + if p.as_object().is_some_and(|o| o.keys().any(|k| !is_meta(k))) { + return Err("only default Shadows/Highlights settings are written to PSD".into()); + } + return Ok(None); + } + // No settings to write; any parameter keys an old record carries have no effect on the + // filter either, so nothing is lost. + c if NO_OPTIONS.contains(&c) => return Ok(None), + // Keys and order as Photoshop writes them. + "Pnch" => d.with("Amnt", pi(p, "amount", 50.0)), + "Twrl" => d.with("Angl", pi(p, "angle", 50.0)), + "Rple" => d.with("Amnt", pi(p, "amount", 100.0)).with("RplS", enum_param(p, "size", "medium", "RplS", &RIPPLE_SIZE)?), + "Sphr" => d.with("Amnt", pi(p, "amount", 100.0)).with("SphM", enum_param(p, "mode", "normal", "SphM", &SPHERIZE_MODE)?), + "Plr " => d.with("Cnvr", enum_param(p, "mode", "rectangularToPolar", "Cnvr", &POLAR_MODE)?), + "ZgZg" => { + d.with("Amnt", pi(p, "amount", 10.0)).with("NmbR", pi(p, "ridges", 5.0)).with("ZZTy", enum_param(p, "style", "pondRipples", "ZZTy", &ZIGZAG_STYLE)?) + } + "Wave" => { + // The seed the renderer draws with (engine `filters::seed`): a u32, else a legacy signed + // or fractional seed, rounded and saturated, as its bit pattern. + let seed = match p.get("seed").and_then(J::as_u64).and_then(|s| u32::try_from(s).ok()) { + Some(s) => Value::Integer(s as i32), + None => pi(p, "seed", 0.0), + }; + // The renderer draws at most MAX_WAVE_GENERATORS: write what PhotoCraft rendered, so + // Photoshop doesn't draw more generators than the user saw. + let generators = pf(p, "generators", 5.0).round().clamp(1.0, f64::from(photocraft_algo::MAX_WAVE_GENERATORS)) as i32; + d.with("Wvtp", enum_param(p, "type", "sine", "Wvtp", &WAVE_TYPE)?) + .with("NmbG", Value::Integer(generators)) + .with("WLMn", pi(p, "wavelengthMin", 10.0)) + .with("WLMx", pi(p, "wavelengthMax", 120.0)) + .with("AmMn", pi(p, "amplitudeMin", 5.0)) + .with("AmMx", pi(p, "amplitudeMax", 35.0)) + .with("SclH", Value::Integer(100)) + .with("SclV", Value::Integer(100)) + .with("UndA", enum_param(p, "undefinedAreas", "wrap", "UndA", &UNDEFINED_AREAS)?) + .with("RndS", seed) + } + "Invr" => return Ok(None), + _ => return Err("not a recognized filter".into()), + })) +} + +/// The `filterFXList` item for one smart filter, or why it can't be written. +pub fn item_for_filter(f: &SmartFilter) -> Result { + if f.command == camera_raw::COMMAND { + let mut d = camera_raw::export_item(&f.params)?; + set(&mut d, "blendOptions", blend_options(f)); + set(&mut d, "enab", Value::Boolean(f.visible)); + return Ok(d); + } + if f.command == displace::COMMAND { + let mut d = displace::export_item(&f.params)?; + set(&mut d, "blendOptions", blend_options(f)); + set(&mut d, "enab", Value::Boolean(f.visible)); + return Ok(d); + } + if f.command == GALLERY_COMMAND { + return gallery_item(f); + } + if f.command == hue_sat::COMMAND { + return hue_sat::export_item(f); + } + if perspective_warp::handles(&f.command) { + return perspective_warp::export_item(f); + } + if f.command == UNSUPPORTED_FILTER { + let raw = f.params.get("psd").and_then(J::as_str).and_then(from_hex).ok_or("its filter data is missing")?; + let mut d = Descriptor::from_bytes(&raw).map_err(|e| format!("its filter data is unreadable ({e})"))?; + set(&mut d, "blendOptions", blend_options(f)); + set(&mut d, "enab", Value::Boolean(f.visible)); + return Ok(d); + } + let k = known_by_command(&f.command).ok_or("this filter has no PSD equivalent")?; + let fltr = fltr_for(k, &f.params)?; + // Pointillize's background, Tiles' colours and the render filters' colours are the entry's; + // the others store Photoshop's defaults. + let (fg, bg) = match k.class { + "Pntl" => (rgbc(0.0), colour_rgbc(f.params.get("background"), 255.0)?), + "Tls " | "Fbrs" | "Clds" | "DfrC" => (colour_rgbc(f.params.get("foreground"), 0.0)?, colour_rgbc(f.params.get("background"), 255.0)?), + _ => (rgbc(0.0), rgbc(255.0)), + }; + // The render filters are written with the descriptor names Photoshop gives them: the filter's + // own name on `Fltr` and empty ones on the colours. + let render = matches!(k.class, "Fbrs" | "Clds" | "DfrC" | "LnsF"); + let (fltr, fg, bg) = if render { (fltr.map(|f| named(f, k.name.trim_end_matches("..."))), named_value(fg), named_value(bg)) } else { (fltr, fg, bg) }; + let mut d = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(k.name))) + .with("blendOptions", blend_options(f)) + .with("enab", Value::Boolean(f.visible)) + .with("hasoptions", Value::Boolean(fltr.is_some())) + .with("FrgC", fg) + .with("BckC", bg); + if let Some(x) = fltr { + d = d.with("Fltr", Value::Descriptor(x)); + } + Ok(d.with("filterID", Value::Integer(k.filter_id))) +} + +fn named(mut d: Descriptor, name: &str) -> Descriptor { + d.name = UnicodeString::new_nul(name); + d +} + +fn named_value(v: Value) -> Value { + match v { + Value::Descriptor(d) => Value::Descriptor(named(d, "")), + other => other, + } +} + +/// Builds a `filterFX` object; filters that can't be written are left out, with a message each. +pub fn filter_fx(stack: &FilterStack, layer: &str, warnings: &mut Vec) -> Descriptor { + let mut list = Vec::new(); + for f in &stack.filters { + match item_for_filter(f) { + Ok(d) => list.push(Value::Descriptor(d)), + Err(e) => warnings.push(format!("layer \"{layer}\": smart filter {} was not written to PSD: {e}", f.command)), + } + } + Descriptor::new("filterFXStyle") + .with("enab", Value::Boolean(stack.enabled)) + .with("validAtPosition", Value::Boolean(true)) + .with("filterMaskEnable", Value::Boolean(stack.mask_enabled)) + .with("filterMaskLinked", Value::Boolean(stack.mask_linked)) + .with("filterMaskExtendWithWhite", Value::Boolean(true)) + .with("filterFXList", Value::List(list)) +} + +/// Replaces item `key` in place, or appends it. +pub(crate) fn set(d: &mut Descriptor, key: &str, v: Value) { + match d.items.iter_mut().find(|(k, _)| k.is(key)) { + Some(e) => e.1 = v, + None => d.items.push((Id::new(key), v)), + } +} + +/// Replaces item `key` in place, or inserts it before item `before` (appends it without one). +pub(super) fn set_before(d: &mut Descriptor, key: &str, v: Value, before: &str) { + if let Some(e) = d.items.iter_mut().find(|(k, _)| k.is(key)) { + e.1 = v; + return; + } + let at = d.items.iter().position(|(k, _)| k.is(before)).unwrap_or(d.items.len()); + d.items.insert(at, (Id::new(key), v)); +} diff --git a/crates/io/src/smart_map/halftone_tests.rs b/crates/io/src/smart_map/halftone_tests.rs new file mode 100644 index 0000000000..d4f67dbfec --- /dev/null +++ b/crates/io/src/smart_map/halftone_tests.rs @@ -0,0 +1,220 @@ +//! Halftone Pattern descriptors with the keys from the reference application's Filter Gallery plug-in terminology (#8940): +//! `HlSz` size, `Cntr` contrast and a `ScrT` enum (`ScrD` dot, `ScrC` circle, `ScrL` line). +use super::import::{GALLERY_CLASS, GALLERY_COMMAND, colour_rgbc}; +use super::*; + +/// Builds an editable Halftone Pattern filter with explicit foreground/background colours. +fn filter(pattern: &str, size: i32, contrast: i32) -> SmartFilter { + SmartFilter { + command: GALLERY_COMMAND.into(), + params: json!({ + "effects": [{"filter": "halftonePattern", "params": {"size": size, "contrast": contrast, "patternType": pattern}, "visible": true}], + "foreground": [0.2, 0.4, 0.6, 1.0], "background": [0.8, 0.6, 0.4, 1.0], + }), + blend: BlendMode::Normal, + opacity: 1.0, + visible: true, + } +} + +/// Builds the inline Halftone descriptor as the reference application writes it. +fn native_effect(pattern: &str, size: i32, contrast: i32) -> Descriptor { + let mut d = Descriptor::new("GEfc") + .with("GEfk", enumv("GEft", "HlfS")) + .with("HlSz", Value::Integer(size)) + .with("Cntr", Value::Integer(contrast)) + .with("ScrT", enumv("ScrT", pattern)); + d.name = UnicodeString::new_nul("Filter Gallery"); + d +} + +/// Wraps a Gallery descriptor in a Photoshop smart-filter item. +fn native_item(effect: Descriptor) -> Descriptor { + let f = filter("dot", 2, 5); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filter Gallery..."))) + .with("blendOptions", blend_options(&f)) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", colour_rgbc(f.params.get("foreground"), 0.0).unwrap()) + .with("BckC", colour_rgbc(f.params.get("background"), 1.0).unwrap()) + .with("Fltr", Value::Descriptor(effect)) + .with("filterID", Value::Integer(code(b"GEfc"))) +} + +/// Checks the pattern enums and editable settings in both mapping directions. +#[test] +fn halftone_pattern_reads_and_writes_photoshop_inline_descriptors() { + for (pattern, native) in [("dot", "ScrD"), ("circle", "ScrC"), ("line", "ScrL")] { + for (size, contrast) in [(1, 0), (2, 5), (7, 31), (12, 50)] { + let expected = filter(pattern, size, contrast); + let native = native_item(native_effect(native, size, contrast)); + assert_eq!(filter_from_item(&native), expected); + let written = item_for_filter(&expected).unwrap(); + assert_eq!(get_desc(&written, "Fltr"), get_desc(&native, "Fltr")); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&written.to_bytes()).unwrap()), expected); + } + } +} + +/// Keeps defaults, hidden effects, stack order, and blending intact on round trip. +#[test] +fn halftone_pattern_defaults_and_mixed_hidden_stack_round_trip() { + let defaults = SmartFilter { params: json!({"effects": [{"filter": "halftonePattern"}]}), ..filter("dot", 1, 5) }; + assert_eq!(filter_from_item(&item_for_filter(&defaults).unwrap()).params["effects"][0]["params"], json!({"size": 1, "contrast": 5, "patternType": "dot"})); + let mut f = filter("circle", 8, 22); + f.params["effects"][0]["visible"] = json!(false); + f.params["effects"].as_array_mut().unwrap().push(json!({"filter": "chrome", "params": {"detail": 4, "smoothness": 7}, "visible": true})); + f.opacity = 0.5; + f.blend = BlendMode::Multiply; + f.visible = false; + let item = item_for_filter(&f).unwrap(); + assert!(get_desc(&item, "Fltr").unwrap().get("GEfs").is_some()); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()), f); +} + +/// A default Halftone Pattern as the reference application saves it (size 1, contrast 5, Dot) opens editable. +#[test] +fn halftone_pattern_photoshop_default_imports_editable() { + let f = filter_from_item(&native_item(native_effect("ScrD", 1, 5))); + assert_eq!(f.command, GALLERY_COMMAND); + assert_eq!(f.params["effects"][0]["filter"], json!("halftonePattern")); + assert_eq!(f.params["effects"][0]["params"], json!({"size": 1, "contrast": 5, "patternType": "dot"})); +} + +/// Keys the reference application's Filter Gallery doesn't use (`Sz `, `Ptrn` of type `HlfT`) are not taken for +/// Halftone settings: the item stays opaque and is written back as it was read. +#[test] +fn halftone_pattern_foreign_keys_stay_verbatim() { + for (pattern, shape) in [("Dt ", "dot"), ("Crc ", "circle"), ("Ln ", "line")] { + let mut d = Descriptor::new("GEfc") + .with("GEfk", enumv("GEft", "HlfS")) + .with("Sz ", Value::Integer(2)) + .with("Cntr", Value::Integer(5)) + .with("Ptrn", enumv("HlfT", pattern)); + d.name = UnicodeString::new_nul("Filter Gallery"); + let item = native_item(d); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{shape}"); + assert_eq!(item_for_filter(&f).unwrap(), item, "{shape}"); + } +} + +/// Preserves unfamiliar and malformed settings instead of substituting editable defaults. +#[test] +fn halftone_pattern_unknown_or_malformed_descriptors_stay_verbatim() { + let valid = native_effect("ScrD", 2, 5); + let mut invalid = vec![ + valid.clone().with("futureSetting", Value::Integer(1)), + valid.clone().with("HlSz", Value::Integer(3)), // repeated key + ]; + for (key, value) in [ + ("GEfk", enumv("wrong", "HlfS")), + ("ScrT", enumv("ScrT", "????")), + ("ScrT", enumv("wrong", "ScrD")), + ("ScrT", Value::Integer(0)), + ("HlSz", Value::Integer(0)), + ("HlSz", Value::Integer(13)), + ("HlSz", Value::Double(2.0)), + ("Cntr", Value::Integer(-1)), + ("Cntr", Value::Integer(51)), + ("GELv", Value::Integer(1)), + ] { + let mut effect = valid.clone(); + effect.items.retain(|(id, _)| !id.is(key)); + effect.items.push((Id::new(key), value)); + invalid.push(effect); + } + for missing in ["HlSz", "Cntr", "ScrT"] { + let mut effect = valid.clone(); + effect.items.retain(|(id, _)| !id.is(missing)); + invalid.push(effect); + } + for effect in invalid { + let item = native_item(effect); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +/// Keeps a whole Gallery stack opaque when a Halftone entry has an unfamiliar class ID. +#[test] +fn halftone_pattern_unknown_nested_class_stays_verbatim() { + let valid = native_effect("ScrD", 2, 5); + let mut unknown = valid.clone(); + unknown.class_id = Id::new("Xxxx"); + for effects in [vec![unknown.clone()], vec![valid.clone(), unknown.clone()], vec![unknown, valid]] { + let gallery = Descriptor::new(GALLERY_CLASS).with("GEfs", Value::List(effects.into_iter().map(Value::Descriptor).collect())); + let item = native_item(gallery); + let parsed = Descriptor::from_bytes(&item.to_bytes()).unwrap(); + let filter = filter_from_item(&parsed); + assert_eq!(filter.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&filter).unwrap().to_bytes(), item.to_bytes()); + } +} + +/// Keeps a Gallery stack opaque when its own descriptor holds a setting beside the effect list, +/// which a save would otherwise drop. +#[test] +fn halftone_pattern_unknown_outer_setting_stays_verbatim() { + let valid = native_effect("ScrD", 2, 5); + let gallery = Descriptor::new(GALLERY_CLASS).with("GEfs", Value::List(vec![Value::Descriptor(valid)])).with("futureSetting", Value::Integer(7)); + let item = native_item(gallery); + let parsed = Descriptor::from_bytes(&item.to_bytes()).unwrap(); + let filter = filter_from_item(&parsed); + assert_eq!(filter.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&filter).unwrap().to_bytes(), item.to_bytes()); +} + +/// Rejects unsupported export values instead of silently changing their meaning. +#[test] +fn halftone_pattern_invalid_export_settings_are_reported() { + for (key, value) in [ + ("patternType", json!("triangle")), + ("patternType", json!(2)), + ("size", json!(0)), + ("size", json!(13)), + ("size", json!("2")), + ("size", json!(2.5)), + ("contrast", json!(-1)), + ("contrast", json!(51)), + ("contrast", json!(1e100)), + ("contrast", json!(5.5)), + ("futureSetting", json!(1)), + ] { + let mut f = filter("dot", 2, 5); + f.params["effects"][0]["params"][key] = value; + assert!(item_for_filter(&f).is_err(), "{key}: {}", f.params); + } +} + +/// Checks complete PSD/PSB round trips for every pattern at 8-, 16-, and 32-bit depth. +#[test] +fn halftone_pattern_survives_complete_psd_and_psb_export() { + use photocraft_doc::{Document, Layer, LayerContent, SmartObject, SmartSource}; + use photocraft_geom::Size; + use std::sync::Arc; + + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + for pattern in ["dot", "circle", "line"] { + let mut source = Document::new("synthetic contents", Size::new(2, 2), ColorMode::Rgb, sample); + let mut pixels = Surface::new(source.pixel_format()); + pixels.fill_rect(source.bounds(), &[0.5, 0.5, 0.5, 1.0]); + source.layers.push(Layer::new("pixels", LayerContent::Raster(pixels.clone()))); + let bytes = photocraft_format::save_to_bytes(&source, &Default::default()).unwrap(); + let mut smart = + SmartObject::new(SmartSource::Embedded { file_name: "contents.pcraft".into(), bytes: Arc::new(bytes) }, Affine::IDENTITY, Some(pixels)); + smart.smart_filters.push(filter(pattern, 2, 5)); + let mut doc = Document::new("halftone", Size::new(2, 2), ColorMode::Rgb, sample); + doc.layers.push(Layer::new("halftone", LayerContent::Smart(smart))); + for ext in ["psd", "psb"] { + let out = crate::export(&doc, ext, &crate::ExportOptions::default()).unwrap(); + assert!(!out.warnings.iter().any(|w| w.contains("smart filter")), "{ext}: {:?}", out.warnings); + let back = crate::import(ext, &out.bytes).unwrap().document; + let LayerContent::Smart(smart) = &back.layers[0].content else { panic!("smart object lost") }; + assert_eq!(smart.smart_filters, vec![filter(pattern, 2, 5)], "{ext} {sample:?} {pattern}"); + } + } + } +} diff --git a/crates/io/src/smart_map/import.rs b/crates/io/src/smart_map/import.rs new file mode 100644 index 0000000000..f8f3f50e3f --- /dev/null +++ b/crates/io/src/smart_map/import.rs @@ -0,0 +1,895 @@ +use super::export::{CELL_SIZE, blend_options, is_meta, pi, rgbc, whole}; +use super::placed::enumv; +use super::{ + BlendMode, Descriptor, Id, J, Map, ReferenceItem, SmartFilter, UNSUPPORTED_FILTER, UnicodeString, Value, bool_or, camera_raw, displace, enum_of, get_desc, + hue_sat, json, num, perspective_warp, text_of, +}; + +// ---------- the filters we model ---------- + +/// Photoshop filter → PhotoCraft command, keyed by the `Fltr` class (and `filterID`). +pub(super) struct Known { + pub(super) class: &'static str, + pub(super) filter_id: i32, + /// `Nm ` as Photoshop writes it (the menu item, with its ellipsis and mnemonic). + pub(super) name: &'static str, + command: &'static str, +} + +/// A four-character PSD code as the big-endian integer Photoshop stores it as. +pub(crate) const fn code(c: &[u8; 4]) -> i32 { + i32::from_be_bytes(*c) +} + +const KNOWN: [Known; 53] = [ + Known { class: "GsnB", filter_id: code(b"GsnB"), name: "Gaussian Blur...", command: "filter.blur.gaussianBlur" }, + Known { class: "boxblur", filter_id: 840, name: "Box Blur...", command: "filter.blur.boxBlur" }, + Known { class: "MtnB", filter_id: code(b"MtnB"), name: "Motion Blur...", command: "filter.blur.motionBlur" }, + // Surface Blur, Smart Blur and Radial Blur: keys, value types and enums measured in Photoshop. + Known { class: "surfaceBlur", filter_id: 1135, name: "Surface Blur...", command: "filter.blur.surfaceBlur" }, + Known { class: "SmrB", filter_id: code(b"SmrB"), name: "Smart Blur...", command: "filter.blur.smartBlur" }, + // Radial Blur at its defaults stores no `Fltr` (`hasoptions` false); that entry is kept verbatim. + Known { class: "RdlB", filter_id: code(b"RdlB"), name: "Radial Blur...", command: "filter.blur.radialBlur" }, + Known { class: "UnsM", filter_id: code(b"UnsM"), name: "Unsharp Mask...", command: "filter.sharpen.unsharpMask" }, + Known { class: "AdNs", filter_id: code(b"AdNs"), name: "Add Noise...", command: "filter.noise.addNoise" }, + Known { class: "Mdn ", filter_id: code(b"Mdn "), name: "Median...", command: "filter.noise.median" }, + Known { class: "HghP", filter_id: code(b"HghP"), name: "High Pass...", command: "filter.other.highPass" }, + Known { class: "Mxm ", filter_id: code(b"Mxm "), name: "Maximum...", command: "filter.other.maximum" }, + Known { class: "Mnm ", filter_id: code(b"Mnm "), name: "Minimum...", command: "filter.other.minimum" }, + Known { class: "Ofst", filter_id: code(b"Ofst"), name: "Offset...", command: "filter.other.offset" }, + Known { class: "Cstm", filter_id: code(b"Cstm"), name: "Custom...", command: "filter.other.custom" }, + Known { class: "Msc ", filter_id: code(b"Msc "), name: "Mosaic...", command: "filter.pixelate.mosaic" }, + Known { class: "Mztn", filter_id: code(b"Mztn"), name: "Mezzotint...", command: "filter.pixelate.mezzotint" }, + Known { class: "ClrH", filter_id: code(b"ClrH"), name: "Color Halftone...", command: "filter.pixelate.colorHalftone" }, + Known { class: "Embs", filter_id: code(b"Embs"), name: "Emboss...", command: "filter.stylize.emboss" }, + Known { class: "Dfs ", filter_id: code(b"Dfs "), name: "Diffuse...", command: "filter.stylize.diffuse" }, + Known { class: "TrcC", filter_id: code(b"TrcC"), name: "Trace Contour...", command: "filter.stylize.traceContour" }, + Known { class: "Crst", filter_id: code(b"Crst"), name: "Crystallize...", command: "filter.pixelate.crystallize" }, + Known { class: "Pntl", filter_id: code(b"Pntl"), name: "Pointillize...", command: "filter.pixelate.pointillize" }, + Known { class: "Wnd ", filter_id: code(b"Wnd "), name: "Wind...", command: "filter.stylize.wind" }, + Known { class: "Extr", filter_id: code(b"Extr"), name: "Extrude...", command: "filter.stylize.extrude" }, + Known { class: "Tls ", filter_id: code(b"Tls "), name: "Tiles...", command: "filter.stylize.tiles" }, + Known { class: "Lvls", filter_id: code(b"Lvls"), name: "Levels...", command: "image.adjustments.levels" }, + Known { class: "Crvs", filter_id: code(b"Crvs"), name: "Curves...", command: "image.adjustments.curves" }, + // Shadows/Highlights at its defaults stores no `Fltr` (`hasoptions` false). + Known { class: "adaptCorrect", filter_id: 781, name: "Shado&ws/Highlights...", command: "image.adjustments.shadowsHighlights" }, + // Filters without options (measured in Photoshop): `hasoptions` false, no `Fltr`, only the + // four-char `filterID`. The class is that code; see `NO_OPTIONS`. + Known { class: "Blr ", filter_id: code(b"Blr "), name: "Blur", command: "filter.blur.blur" }, + Known { class: "BlrM", filter_id: code(b"BlrM"), name: "Blur More", command: "filter.blur.blurMore" }, + Known { class: "Avrg", filter_id: code(b"Avrg"), name: "Average", command: "filter.blur.average" }, + Known { class: "Dspc", filter_id: code(b"Dspc"), name: "Despeckle", command: "filter.noise.despeckle" }, + Known { class: "FndE", filter_id: code(b"FndE"), name: "Find Edges", command: "filter.stylize.findEdges" }, + Known { class: "Fct ", filter_id: code(b"Fct "), name: "Facet", command: "filter.pixelate.facet" }, + Known { class: "Frgm", filter_id: code(b"Frgm"), name: "Fragment", command: "filter.pixelate.fragment" }, + Known { class: "Shrp", filter_id: code(b"Shrp"), name: "Sharpen", command: "filter.sharpen.sharpen" }, + Known { class: "ShrM", filter_id: code(b"ShrM"), name: "Sharpen More", command: "filter.sharpen.sharpenMore" }, + Known { class: "ShrE", filter_id: code(b"ShrE"), name: "Sharpen Edges", command: "filter.sharpen.sharpenEdges" }, + Known { class: "Slrz", filter_id: code(b"Slrz"), name: "Solarize", command: "filter.stylize.solarize" }, + // Smart Sharpen has a numeric filterID and a stringID class, like Box Blur. + Known { class: SMART_SHARPEN, filter_id: 1131, name: "Smart Sharpen...", command: "filter.sharpen.smartSharpen" }, + Known { class: "DstS", filter_id: code(b"DstS"), name: "Dust & Scratches...", command: "filter.noise.dustAndScratches" }, + // Distort (Photoshop 27, `corpus`-style check: one smart object per filter saved from Photoshop + // and read back). Shear isn't mapped yet: its curve lives on Photoshop's own grid. + Known { class: "Pnch", filter_id: code(b"Pnch"), name: "Pinch...", command: "filter.distort.pinch" }, + Known { class: "Plr ", filter_id: code(b"Plr "), name: "Polar Coordinates...", command: "filter.distort.polarCoordinates" }, + Known { class: "Rple", filter_id: code(b"Rple"), name: "Ripple...", command: "filter.distort.ripple" }, + Known { class: "Sphr", filter_id: code(b"Sphr"), name: "Spherize...", command: "filter.distort.spherize" }, + Known { class: "Twrl", filter_id: code(b"Twrl"), name: "Twirl...", command: "filter.distort.twirl" }, + Known { class: "Wave", filter_id: code(b"Wave"), name: "Wave...", command: "filter.distort.wave" }, + Known { class: "ZgZg", filter_id: code(b"ZgZg"), name: "ZigZag...", command: "filter.distort.zigZag" }, + // Image › Adjustments › Invert on a smart object: no settings, so no `Fltr` (`hasoptions` false). + Known { class: "Invr", filter_id: code(b"Invr"), name: "Invert", command: "image.adjustments.invert" }, + Known { class: "LnsF", filter_id: code(b"LnsF"), name: "Lens Flare...", command: "filter.render.lensFlare" }, + // These three also read the item's foreground and background colours (`coloured_params`). + Known { class: "Fbrs", filter_id: code(b"Fbrs"), name: "Fibers...", command: "filter.render.fibers" }, + Known { class: "Clds", filter_id: code(b"Clds"), name: "Clouds", command: "filter.render.clouds" }, + Known { class: "DfrC", filter_id: code(b"DfrC"), name: "Difference Clouds", command: "filter.render.differenceClouds" }, +]; + +/// Lens Flare's `Lns ` enum and our `lens` values. Photoshop's codes keep the old lens names: +/// Nikon 35 mm and 105 mm primes, a Panavision movie prime. +pub(super) const LENSES: [(&[u8; 4], &str); 4] = [(b"Zm ", "zoom"), (b"Nkn ", "prime35"), (b"Nkn1", "prime105"), (b"PnVs", "moviePrime")]; + +/// Classes of the filters that have no options: Photoshop stores them without a `Fltr`. +pub(super) const NO_OPTIONS: [&str; 11] = ["Blr ", "BlrM", "Avrg", "Dspc", "FndE", "Fct ", "Frgm", "Shrp", "ShrM", "ShrE", "Slrz"]; + +// ---------- Smart Sharpen and Dust & Scratches ---------- +// +// Measured in Photoshop (applied through Action Manager to a smart object, saved, `filterFX` read +// back): +// +// ```text +// Smart Sharpen (filterID 1131): smartSharpen { presetKind = presetKindType enum (only on the +// default entry), useLegacy bool, Amnt UntF #Prc, Rds UntF #Pxl, noiseReduction UntF #Prc, +// blur = blurType enum (GsnB | lensBlur | MtnB), Angl long (Motion Blur only) } +// Dust & Scratches (filterID DstS): DstS { Rds long, Thsh long } +// ``` +// +// PhotoCraft's Smart Sharpen removes a Gaussian blur, non-legacy: Lens Blur, Motion Blur, legacy +// mode, Shadow/Highlight settings or any other key keep the whole entry verbatim. `presetKind` +// only names the dialog's preset: it is accepted on open but not kept (the engine takes no such +// parameter) and not written (Photoshop leaves it out of entries with explicit values). + +pub(super) const SMART_SHARPEN: &str = "smartSharpen"; + +const SMART_SHARPEN_KEYS: [&str; 6] = ["presetKind", "useLegacy", "Amnt", "Rds ", "noiseReduction", "blur"]; + +/// A `UntF` value in `unit`, finite and within `lo..=hi`. +fn unit_in(d: &Descriptor, key: &str, u: &[u8; 4], lo: f64, hi: f64) -> Option { + match d.get(key)? { + Value::UnitFloat { unit, value } if unit == u && value.is_finite() && (lo..=hi).contains(value) => Some(*value), + _ => None, + } +} + +/// `filter.sharpen.smartSharpen` params; `None` for a setting PhotoCraft doesn't render. +fn smart_sharpen_params(f: &Descriptor) -> Option { + if f.items.iter().any(|(k, _)| !SMART_SHARPEN_KEYS.iter().any(|x| k.is(x))) { + return None; + } + let Some(Value::Boolean(false)) = f.get("useLegacy") else { return None }; + let Some(Value::Enumerated { type_id, value }) = f.get("blur") else { return None }; + if !type_id.is("blurType") || !value.is("GsnB") { + return None; + } + let mut out = Map::new(); + out.insert("amount".into(), json!(unit_in(f, "Amnt", b"#Prc", 1.0, 500.0)?)); + out.insert("radius".into(), json!(unit_in(f, "Rds ", b"#Pxl", 0.1, 64.0)?)); + out.insert("reduceNoise".into(), json!(unit_in(f, "noiseReduction", b"#Prc", 0.0, 100.0)?)); + match f.get("presetKind") { + None => {} + Some(Value::Enumerated { type_id, .. }) if type_id.is("presetKindType") => {} + Some(_) => return None, + } + Some(J::Object(out)) +} + +/// Photoshop enum codes ↔ PhotoCraft parameter values, per filter setting. +pub(super) const RIPPLE_SIZE: [(&[u8; 4], &str); 3] = [(b"Sml ", "small"), (b"Mdm ", "medium"), (b"Lrg ", "large")]; + +pub(super) const SPHERIZE_MODE: [(&[u8; 4], &str); 3] = [(b"Nrml", "normal"), (b"HrzO", "horizontalOnly"), (b"VrtO", "verticalOnly")]; + +pub(super) const POLAR_MODE: [(&[u8; 4], &str); 2] = [(b"RctP", "rectangularToPolar"), (b"PlrR", "polarToRectangular")]; + +pub(super) const ZIGZAG_STYLE: [(&[u8; 4], &str); 3] = [(b"PndR", "pondRipples"), (b"OtFr", "outFromCenter"), (b"ArnC", "aroundCenter")]; + +pub(super) const WAVE_TYPE: [(&[u8; 4], &str); 3] = [(b"WvSn", "sine"), (b"WvTr", "triangle"), (b"WvSq", "square")]; + +pub(super) const UNDEFINED_AREAS: [(&[u8; 4], &str); 2] = [(b"WrpA", "wrap"), (b"RptE", "repeat")]; + +/// The PSD filter record that PhotoCraft's `command` is written as, if it has one. +pub(super) fn known_by_command(command: &str) -> Option<&'static Known> { + KNOWN.iter().find(|k| k.command == command) +} + +/// RGB channel references in Levels/Curves (`Chnl` enum) and their parameter keys ("" = the +/// composite: top-level keys). +pub(super) const CHANNELS: [(&[u8; 4], &str); 4] = [(b"Cmps", ""), (b"Rd ", "red"), (b"Grn ", "green"), (b"Bl ", "blue")]; + +/// Mezzotint's `MztT` enum codes and our `type` values (measured in Photoshop by rendering each). +pub(super) const MEZZOTINT_TYPES: [(&[u8; 4], &str); 10] = [ + (b"FnDt", "fineDots"), + (b"MdmD", "mediumDots"), + (b"GrnD", "grainyDots"), + (b"CrsD", "coarseDots"), + (b"ShrL", "shortLines"), + (b"MdmL", "mediumLines"), + (b"LngL", "longLines"), + (b"ShSt", "shortStrokes"), + (b"MdmS", "mediumStrokes"), + (b"LngS", "longStrokes"), +]; + +/// Color Halftone's `Rds ` (max radius) and `Ang1`..`Ang4` (screen angles) longs, read within +/// the dialog's ranges; anything else is kept verbatim. +fn color_halftone_params(f: &Descriptor) -> Option { + if !only_keys(f, &["Rds ", "Ang1", "Ang2", "Ang3", "Ang4"]) { + return None; + } + let radius = int(f, "Rds ").filter(|r| (4..=127).contains(r))?; + let angle = |k: &str| int(f, k).filter(|a| (-360..=360).contains(a)); + Some(json!({"maxRadius": radius, "channel1": angle("Ang1")?, "channel2": angle("Ang2")?, "channel3": angle("Ang3")?, "channel4": angle("Ang4")?})) +} + +/// A numeric descriptor item as a finite number; `None` when it is absent, not a number, or not finite. +fn px(d: &Descriptor, key: &str) -> Option { + num(d.get(key)).filter(|v| v.is_finite()) +} + +/// An integer descriptor item; `None` when it is absent or of another type. +pub(crate) fn int(d: &Descriptor, key: &str) -> Option { + match d.get(key)? { + Value::Integer(i) => Some(*i), + _ => None, + } +} + +/// `v` when it lies in `lo..=hi` (the filter dialog's range); out-of-range settings are not +/// opened as editable filters, so a re-save keeps Photoshop's value instead of a clamped one. +fn in_range(v: f64, lo: f64, hi: f64) -> Option { + (lo..=hi).contains(&v).then_some(v) +} + +/// Integer `key` of `f` when it lies in `lo..=hi`. +fn int_in(f: &Descriptor, key: &str, lo: i32, hi: i32) -> Option { + int(f, key).filter(|v| (lo..=hi).contains(v)) +} + +/// True when `f` holds only the given keys, each at most once. A filter with a setting we don't +/// know about (Radial Blur's off-centre centre, for one) or a repeated one is kept verbatim rather +/// than losing that setting. +fn only_keys(f: &Descriptor, keys: &[&str]) -> bool { + f.items.iter().all(|(k, _)| keys.iter().any(|x| k.is(x))) && keys.iter().all(|key| f.items.iter().filter(|(k, _)| k.is(key)).count() <= 1) +} + +/// Our name for enum `key`'s value in `f` (of enum type `type_id`), from `(PSD value, our name)` +/// pairs. An enum of another type is not one we know, so the filter stays verbatim. +fn enum_name(f: &Descriptor, key: &str, type_id: &str, table: &[(&[u8; 4], &'static str)]) -> Option<&'static str> { + let Some(Value::Enumerated { type_id: t, value }) = f.get(key) else { return None }; + if t.as_bytes() != type_id.as_bytes() { + return None; + } + table.iter().find(|(c, _)| value.as_bytes() == &c[..]).map(|(_, n)| *n) +} + +/// True when `f` holds each of the given keys once and nothing else. +fn exact_keys(f: &Descriptor, keys: &[&str]) -> bool { + only_keys(f, keys) && keys.iter().all(|x| f.items.iter().any(|(k, _)| k.is(x))) +} + +/// Finite `key` of `f` stored as a plain double (the type Photoshop writes and we write back). +fn double(f: &Descriptor, key: &str) -> Option { + match f.get(key)? { + Value::Double(v) if v.is_finite() => Some(*v), + _ => None, + } +} + +/// `p[key]` (`default` when absent) when it is a number in `lo..=hi`, the range the filter opens +/// with; anything else is an error, so a saved filter always opens as the same editable filter. +pub(super) fn pf_range(p: &J, key: &str, default: f64, lo: f64, hi: f64) -> Result { + match p.get(key) { + None | Some(J::Null) => Ok(default), + Some(v) => v.as_f64().filter(|x| (lo..=hi).contains(x)).ok_or_else(|| format!("setting `{key}` ({v}) is not a number in {lo}..={hi}")), + } +} + +/// Integer `p[key]` (rounded; `default` when absent) in `lo..=hi`, as a PSD long. +pub(super) fn pi_range(p: &J, key: &str, default: f64, lo: i32, hi: i32) -> Result { + let v = pf_range(p, key, default, f64::from(i32::MIN), f64::from(i32::MAX))?.round(); + let range = f64::from(lo)..=f64::from(hi); + if !range.contains(&v) { + return Err(format!("setting `{key}` ({v}) is not a number in {lo}..={hi}")); + } + Ok(Value::Integer(v as i32)) +} + +/// Enum `type_id` holding the PSD value of our name `p[key]` (`default` when absent). +pub(super) fn enum_param(p: &J, key: &str, default: &str, type_id: &str, table: &[(&[u8; 4], &'static str)]) -> Result { + let name = match p.get(key) { + None | Some(J::Null) => default, + Some(v) => v.as_str().ok_or_else(|| format!("setting `{key}` is not text"))?, + }; + let code = table.iter().find(|(_, n)| *n == name).map(|(c, _)| **c).ok_or_else(|| format!("{key} `{name}` has no PSD equivalent"))?; + Ok(Value::Enumerated { type_id: Id::new(type_id), value: Id::Code(code) }) +} + +/// Smart Blur `SmBQ` / `SmBM` and Radial Blur `BlrM` / `BlrQ` values (measured in Photoshop). +pub(super) const SMART_BLUR_QUALITY: [(&[u8; 4], &str); 3] = [(b"SBQL", "low"), (b"SBQM", "medium"), (b"SBQH", "high")]; + +pub(super) const SMART_BLUR_MODE: [(&[u8; 4], &str); 3] = [(b"SBMN", "normal"), (b"SBME", "edgeOnly"), (b"SBMO", "overlayEdge")]; + +pub(super) const RADIAL_METHOD: [(&[u8; 4], &str); 2] = [(b"Spn ", "spin"), (b"Zm ", "zoom")]; + +pub(super) const RADIAL_QUALITY: [(&[u8; 4], &str); 3] = [(b"Drft", "draft"), (b"Gd ", "good"), (b"Bst ", "best")]; + +fn channel_key(d: &Descriptor) -> Option<&'static str> { + let Some(Value::Reference(items)) = d.get("Chnl") else { return None }; + let Some(ReferenceItem::Enumerated { value, .. }) = items.first() else { return None }; + CHANNELS.iter().find(|(c, _)| value.as_bytes() == &c[..]).map(|(_, k)| *k) +} + +pub(super) fn adjustment_list(d: &Descriptor) -> Option> { + let Some(Value::List(items)) = d.get("Adjs") else { return None }; + items + .iter() + .map(|v| match v { + Value::Descriptor(x) => Some(x), + _ => None, + }) + .collect() +} + +fn levels_params(f: &Descriptor) -> Option { + let mut out = Map::new(); + for a in adjustment_list(f)? { + let key = channel_key(a)?; + let mut o = Map::new(); + let pair = |k: &str| match a.get(k) { + Some(Value::List(v)) if v.len() == 2 => Some((int_value(&v[0])?, int_value(&v[1])?)), + _ => None, + }; + if let Some((lo, hi)) = pair("Inpt") { + o.insert("inBlack".into(), json!(lo)); + o.insert("inWhite".into(), json!(hi)); + } + if let Some(g) = px(a, "Gmm ") { + o.insert("gamma".into(), json!(g)); + } + if let Some((lo, hi)) = pair("Otpt") { + o.insert("outBlack".into(), json!(lo)); + o.insert("outWhite".into(), json!(hi)); + } + if key.is_empty() { + out.extend(o); + } else { + out.insert(key.into(), J::Object(o)); + } + } + Some(J::Object(out)) +} + +fn int_value(v: &Value) -> Option { + match v { + Value::Integer(i) => Some(*i), + _ => None, + } +} + +fn curves_params(f: &Descriptor) -> Option { + let mut out = Map::new(); + for a in adjustment_list(f)? { + let key = channel_key(a)?; + // A pencil-drawn curve is a mapping of 256 output levels (`Mpng`) instead of points. + if let Some(Value::List(map)) = a.get("Mpng") { + let map: Option> = map.iter().map(|v| int_value(v).map(|o| json!(o.clamp(0, 255)))).collect(); + out.insert(if key.is_empty() { "points".into() } else { key.into() }, J::Array(map.filter(|m| m.len() == 256)?)); + out.insert("pencil".into(), J::Bool(true)); + continue; + } + let Some(Value::List(pts)) = a.get("Crv ") else { return None }; + let pts: Option> = pts + .iter() + .map(|p| match p { + Value::Descriptor(p) => Some(json!([px(p, "Hrzn")?, px(p, "Vrtc")?])), + _ => None, + }) + .collect(); + out.insert(if key.is_empty() { "points".into() } else { key.into() }, J::Array(pts?)); + } + Some(J::Object(out)) +} + +/// Diffuse's `Md ` (`DfsM` enum, measured in Photoshop) ↔ our `mode`. +pub(super) const DIFFUSE_MODES: [(&str, &str); 4] = [("Nrml", "normal"), ("DrkO", "darkenOnly"), ("LghO", "lightenOnly"), ("anisotropic", "anisotropic")]; + +/// Trace Contour's `Edg ` (`CntE` enum, measured in Photoshop) ↔ our `edge`. +pub(super) const TRACE_EDGES: [(&str, &str); 2] = [("Upr ", "upper"), ("Lwr ", "lower")]; + +/// Our name for the enum value of `key` (of type `type_id`) through `table` (Photoshop's value, ours). +fn enum_from(d: &Descriptor, key: &str, type_id: &str, table: &[(&str, &'static str)]) -> Option<&'static str> { + let Some(Value::Enumerated { type_id: t, value }) = d.get(key) else { return None }; + if t.as_bytes() != type_id.as_bytes() { + return None; + } + table.iter().find(|(psd, _)| value.as_bytes() == psd.as_bytes()).map(|(_, ours)| *ours) +} + +// Wind, Extrude and Tiles, measured in Photoshop (each code changes the rendered result). + +/// Wind's `WndM` (enum `WndM`) ↔ our `method`. +pub(super) const WIND_METHODS: [(&str, &str); 3] = [("Wnd ", "wind"), ("Blst", "blast"), ("Stgr", "stagger")]; + +/// Wind's `Drct` (enum `Drct`) ↔ our `direction`. Photoshop's `Left` streaks extend to the left +/// of an edge, which is our wind from the right. +pub(super) const WIND_DIRECTIONS: [(&str, &str); 2] = [("Left", "fromRight"), ("Rght", "fromLeft")]; + +/// Extrude's `ExtT` (enum `ExtT`) ↔ our `type`. +pub(super) const EXTRUDE_TYPES: [(&str, &str); 2] = [("Blks", "blocks"), ("Pyrm", "pyramids")]; + +/// Extrude's `ExtR` (enum `ExtR`) ↔ our `depthMode`. +pub(super) const EXTRUDE_DEPTHS: [(&str, &str); 2] = [("Rndm", "random"), ("LvlB", "levelBased")]; + +/// Tiles' `FlCl` (enum `FlCl`) ↔ our `fill`. +pub(super) const TILE_FILLS: [(&str, &str); 4] = [("FlBc", "background"), ("FlFr", "foreground"), ("FlIn", "inverse"), ("FlSm", "unaltered")]; + +/// Extrude's size and depth (`ExtS`, `ExtD`) and Tiles' count and offset (`TlNm`, `TlOf`), all +/// `long`s, in the engine's (and Photoshop's dialog) ranges. +pub(super) const EXTRUDE_SIZE: (i32, i32) = (2, 255); + +pub(super) const EXTRUDE_DEPTH: (i32, i32) = (1, 255); + +pub(super) const TILE_COUNT: (i32, i32) = (1, 99); + +pub(super) const TILE_OFFSET: (i32, i32) = (1, 99); + +/// The `bool` at `key`. +fn bool_at(d: &Descriptor, key: &str) -> Option { + match d.get(key)? { + Value::Boolean(b) => Some(*b), + _ => None, + } +} + +/// Tiles' params; background and foreground fills use the colours the entry carries (`FrgC`, +/// `BckC`). `None` when a setting or a colour that is there can't be read. +fn tiles_params(f: &Descriptor, item: &Descriptor) -> Option { + let mut out = Map::new(); + out.insert("count".into(), json!(int_in(f, "TlNm", TILE_COUNT.0, TILE_COUNT.1)?)); + out.insert("maxOffset".into(), json!(int_in(f, "TlOf", TILE_OFFSET.0, TILE_OFFSET.1)?)); + out.insert("fill".into(), json!(enum_from(f, "FlCl", "FlCl", &TILE_FILLS)?)); + for (psd, ours) in [("FrgC", "foreground"), ("BckC", "background")] { + if item.get(psd).is_some() { + out.insert(ours.into(), get_desc(item, psd).and_then(rgbc_colour)?); + } + } + Some(J::Object(out)) +} + +/// Offset › Undefined Areas: the `FlMd` enum (`Fl `) and our `undefinedAreas`. On a layer +/// Photoshop labels `Bckg` "Set to Transparent"; a smart object is always a layer. +pub(super) const OFFSET_FILL: [(&str, &str); 3] = [("Wrp ", "wrap"), ("Rpt ", "repeat"), ("Bckg", "transparent")]; + +/// Custom's ranges in Photoshop's dialog: matrix cells, Scale, Offset. +pub(super) const CUSTOM_CELL: (i32, i32) = (-999, 999); + +pub(super) const CUSTOM_SCALE: (i32, i32) = (1, 9999); + +pub(super) const CUSTOM_OFFSET: (i32, i32) = (-9999, 9999); + +/// `v` when it lies in `lo..=hi`. +fn long_in_range(v: Option, (lo, hi): (i32, i32)) -> Option { + v.filter(|v| (lo..=hi).contains(v)) +} + +/// `filter.other.custom` params for a `Cstm` filter: `Mtrx` is 25 longs, row-major 5 × 5. Values +/// outside Photoshop's ranges are not modelled (the filter stays verbatim). +fn custom_params(f: &Descriptor) -> Option { + if !only_keys(f, &["Mtrx", "Scl ", "Ofst"]) { + return None; + } + let Some(Value::List(m)) = f.get("Mtrx") else { return None }; + if m.len() != 25 { + return None; + } + let kernel: Option> = m.iter().map(|v| long_in_range(int_value(v), CUSTOM_CELL).map(|v| json!(v))).collect(); + let scale = long_in_range(int(f, "Scl "), CUSTOM_SCALE)?; + let offset = long_in_range(int(f, "Ofst"), CUSTOM_OFFSET)?; + Some(json!({"kernel": kernel?, "scale": scale, "offset": offset})) +} + +/// The settings of Photoshop's Wave, as it writes them (and we write them back). +const WAVE_KEYS: [&str; 10] = ["Wvtp", "NmbG", "WLMn", "WLMx", "AmMn", "AmMx", "SclH", "SclV", "UndA", "RndS"]; + +/// Our command and parameters for a known Photoshop filter; `None` when the filter (or one of its +/// settings) is not modelled. `item` is the whole `filterFX` entry (Pointillize reads its `BckC`, +/// Tiles its colours). +pub(super) fn known_params(class: &str, f: Option<&Descriptor>, item: &Descriptor) -> Option<(&'static str, J)> { + let k = KNOWN.iter().find(|k| k.class == class)?; + let params = match f { + None if class == "adaptCorrect" || class == "Invr" || NO_OPTIONS.contains(&class) => json!({}), + None => return None, + Some(f) => distort_filter_params(class, f) + .or_else(|| blur_filter_params(class, f)) + .or_else(|| pixel_filter_params(class, f, item)) + .or_else(|| stylize_filter_params(class, f, item)) + .or_else(|| other_filter_params(class, f))?, + }; + Some((k.command, params)) +} + +fn distort_filter_params(class: &str, f: &Descriptor) -> Option { + Some(match (class, Some(f)) { + ("Pnch", Some(f)) if exact_keys(f, &["Amnt"]) => json!({"amount": int(f, "Amnt")?}), + ("Twrl", Some(f)) if exact_keys(f, &["Angl"]) => json!({"angle": int(f, "Angl")?}), + ("Rple", Some(f)) if exact_keys(f, &["Amnt", "RplS"]) => json!({"amount": int(f, "Amnt")?, "size": enum_name(f, "RplS", "RplS", &RIPPLE_SIZE)?}), + ("Sphr", Some(f)) if exact_keys(f, &["Amnt", "SphM"]) => json!({"amount": int(f, "Amnt")?, "mode": enum_name(f, "SphM", "SphM", &SPHERIZE_MODE)?}), + ("Plr ", Some(f)) if exact_keys(f, &["Cnvr"]) => json!({"mode": enum_name(f, "Cnvr", "Cnvr", &POLAR_MODE)?}), + ("ZgZg", Some(f)) if exact_keys(f, &["Amnt", "NmbR", "ZZTy"]) => { + json!({"amount": int(f, "Amnt")?, "ridges": int(f, "NmbR")?, "style": enum_name(f, "ZZTy", "ZZTy", &ZIGZAG_STYLE)?}) + } + ("Wave", Some(f)) if exact_keys(f, &WAVE_KEYS) => { + // PhotoCraft's Wave has no Scale: a scaled wave stays verbatim rather than lose it. The + // same for more generators than the renderer models, which would draw fewer than + // Photoshop did (and than the file asks for, once its cached pixels are re-rendered). + let generators = int(f, "NmbG")?; + let modelled = u32::try_from(generators).is_ok_and(|g| (1..=photocraft_algo::MAX_WAVE_GENERATORS).contains(&g)); + if int(f, "SclH")? != 100 || int(f, "SclV")? != 100 || !modelled { + return None; + } + json!({ + "type": enum_name(f, "Wvtp", "Wvtp", &WAVE_TYPE)?, + "generators": generators, + "wavelengthMin": int(f, "WLMn")?, + "wavelengthMax": int(f, "WLMx")?, + "amplitudeMin": int(f, "AmMn")?, + "amplitudeMax": int(f, "AmMx")?, + "undefinedAreas": enum_name(f, "UndA", "UndA", &UNDEFINED_AREAS)?, + // `RndS` is a signed long; the seed is its bit pattern. + "seed": (int(f, "RndS")? as u32), + }) + } + _ => return None, + }) +} + +fn blur_filter_params(class: &str, f: &Descriptor) -> Option { + Some(match (class, Some(f)) { + ("GsnB" | "boxblur" | "Mdn " | "HghP" | "Mxm " | "Mnm ", Some(f)) => json!({"radius": px(f, "Rds ")?}), + ("MtnB", Some(f)) => json!({"angle": int(f, "Angl")?, "distance": px(f, "Dstn")?}), + ("UnsM", Some(f)) => json!({"amount": px(f, "Amnt")?, "radius": px(f, "Rds ")?, "threshold": int(f, "Thsh")?}), + ("AdNs", Some(f)) => { + let dist = match enum_of(f, "Dstr")? { + b"Unfr" => "uniform", + b"Gsn " => "gaussian", + _ => return None, + }; + let mono = match f.get("Mnch") { + Some(Value::Boolean(b)) => *b, + _ => return None, + }; + json!({"amount": px(f, "Nose")?, "distribution": dist, "monochromatic": mono, "seed": int(f, "FlRs").unwrap_or(0)}) + } + ("surfaceBlur", Some(f)) if only_keys(f, &["Rds ", "Thsh"]) => json!({ + "radius": unit_in(f, "Rds ", b"#Pxl", 1.0, 100.0)?, + "threshold": int_in(f, "Thsh", 2, 255)?, + }), + ("SmrB", Some(f)) if only_keys(f, &["Rds ", "Thsh", "SmBQ", "SmBM"]) => json!({ + "radius": in_range(double(f, "Rds ")?, 0.1, 100.0)?, + "threshold": in_range(double(f, "Thsh")?, 0.1, 100.0)?, + "quality": enum_name(f, "SmBQ", "SmBQ", &SMART_BLUR_QUALITY)?, + "mode": enum_name(f, "SmBM", "SmBM", &SMART_BLUR_MODE)?, + }), + ("RdlB", Some(f)) if only_keys(f, &["Amnt", "BlrM", "BlrQ"]) => json!({ + "amount": int_in(f, "Amnt", 1, 100)?, + "method": enum_name(f, "BlrM", "BlrM", &RADIAL_METHOD)?, + "quality": enum_name(f, "BlrQ", "BlrQ", &RADIAL_QUALITY)?, + }), + _ => return None, + }) +} + +fn pixel_filter_params(class: &str, f: &Descriptor, item: &Descriptor) -> Option { + Some(match (class, Some(f)) { + ("Msc ", Some(f)) => json!({"cellSize": px(f, "ClSz")?}), + ("Mztn", Some(f)) if only_keys(f, &["MztT"]) => json!({"type": enum_name(f, "MztT", "MztT", &MEZZOTINT_TYPES)?}), + ("ClrH", Some(f)) => color_halftone_params(f)?, + ("Embs", Some(f)) => json!({"angle": int(f, "Angl")?, "height": int(f, "Hght")?, "amount": int(f, "Amnt")?}), + (SMART_SHARPEN, Some(f)) => smart_sharpen_params(f)?, + ("DstS", Some(f)) => { + if f.items.iter().any(|(k, _)| !k.is("Rds ") && !k.is("Thsh")) { + return None; + } + json!({"radius": int_in(f, "Rds ", 1, 500)?, "threshold": int_in(f, "Thsh", 0, 255)?}) + } + ("Dfs ", Some(f)) => json!({"mode": enum_from(f, "Md ", "DfsM", &DIFFUSE_MODES)?}), + ("TrcC", Some(f)) => json!({"level": int_in(f, "Lvl ", 0, 255)?, "edge": enum_from(f, "Edg ", "CntE", &TRACE_EDGES)?}), + ("Crst", Some(f)) => json!({"cellSize": int_in(f, "ClSz", CELL_SIZE.0, CELL_SIZE.1)?}), + ("Pntl", Some(f)) => { + let mut p = json!({"cellSize": int_in(f, "ClSz", CELL_SIZE.0, CELL_SIZE.1)?}); + // Pointillize paints the gaps in the background colour the entry carries. + if item.get("BckC").is_some() { + p["background"] = get_desc(item, "BckC").and_then(rgbc_colour)?; + } + p + } + _ => return None, + }) +} + +fn stylize_filter_params(class: &str, f: &Descriptor, item: &Descriptor) -> Option { + Some(match (class, Some(f)) { + ("Wnd ", Some(f)) => json!({ + "method": enum_from(f, "WndM", "WndM", &WIND_METHODS)?, + "direction": enum_from(f, "Drct", "Drct", &WIND_DIRECTIONS)?, + }), + ("Extr", Some(f)) => json!({ + "type": enum_from(f, "ExtT", "ExtT", &EXTRUDE_TYPES)?, + "size": int_in(f, "ExtS", EXTRUDE_SIZE.0, EXTRUDE_SIZE.1)?, + "depth": int_in(f, "ExtD", EXTRUDE_DEPTH.0, EXTRUDE_DEPTH.1)?, + "depthMode": enum_from(f, "ExtR", "ExtR", &EXTRUDE_DEPTHS)?, + "solidFrontFaces": bool_at(f, "ExtF")?, + "maskIncompleteBlocks": bool_at(f, "ExtM")?, + }), + ("Tls ", Some(f)) => tiles_params(f, item)?, + _ => return None, + }) +} + +fn other_filter_params(class: &str, f: &Descriptor) -> Option { + Some(match (class, Some(f)) { + ("Ofst", Some(f)) if only_keys(f, &["Hrzn", "Vrtc", "Fl "]) => { + json!({"horizontal": int(f, "Hrzn")?, "vertical": int(f, "Vrtc")?, "undefinedAreas": enum_from(f, "Fl ", "FlMd", &OFFSET_FILL)?}) + } + ("Cstm", Some(f)) => custom_params(f)?, + // Only the settings and value types Photoshop writes (and we write back); anything else + // keeps the filter verbatim. + ("LnsF", Some(f)) if exact_keys(f, &["Brgh", "FlrC", "Lns "]) => { + let centre = get_desc(f, "FlrC").filter(|c| c.class_id.is("Pnt ") && exact_keys(c, &["Hrzn", "Vrtc"]))?; + let lens = enum_name(f, "Lns ", "Lns ", &LENSES)?; + json!({"brightness": int(f, "Brgh")?, "centerX": double(centre, "Hrzn")?, "centerY": double(centre, "Vrtc")?, "lens": lens}) + } + ("Lvls", Some(f)) => levels_params(f)?, + ("Crvs", Some(f)) => curves_params(f)?, + _ => return None, + }) +} +// ---------- Filter Gallery ---------- +// +// Filter › Filter Gallery stores its effect stack in a `GEfc` filter (filterID `GEfc`). Photoshop +// writes a single effect inline; Photopea writes a list (also for one effect): +// +// ```text +// Fltr = GEfc { GEfk = GEft enum (the effect), } (Photoshop) +// Fltr = GEfc { GEfs = [ GEfc { , GEfk = GEft enum, GELv = visible } ] } (Photopea) +// ``` +// +// The list is in application order, like `filter.filterGallery`'s `effects`. The item's +// `FrgC`/`BckC` are the colours the Sketch effects read. Only effects whose settings we know are +// mapped; a stack with any other effect (or setting) stays an [`UNSUPPORTED_FILTER`]. + +pub(super) const GALLERY_CLASS: &str = "GEfc"; + +pub(super) const GALLERY_COMMAND: &str = "filter.filterGallery"; + +const GALLERY_NAME: &str = "Filter Gallery..."; + +/// One gallery effect: our filter key, Photoshop's `GEft` code and its settings (PSD key, our key). +pub(super) struct GalleryCode { + pub(super) key: &'static str, + code: &'static str, + pub(super) settings: &'static [(&'static str, &'static str)], +} + +pub(super) const GALLERY_EFFECTS: [GalleryCode; 2] = [ + GalleryCode { key: "chrome", code: "Chrm", settings: &[("Dtl ", "detail"), ("Smth", "smoothness")] }, + GalleryCode { key: "halftonePattern", code: "HlfS", settings: &[("HlSz", "size"), ("Cntr", "contrast")] }, +]; + +// Halftone Pattern's shape is a `ScrT` enum (Filter Gallery terminology: Dot, Circle, Line). +const HALFTONE_PATTERNS: [(&[u8; 4], &str); 3] = [(b"ScrD", "dot"), (b"ScrC", "circle"), (b"ScrL", "line")]; + +/// An `RGBC` descriptor (0–255 channels) as straight `[r, g, b, a]`, 0–1. +fn rgbc_colour(d: &Descriptor) -> Option { + let c = |k: &str| px(d, k).map(|v| (v / 255.0).clamp(0.0, 1.0)); + Some(json!([c("Rd ")?, c("Grn ")?, c("Bl ")?, 1.0])) +} + +/// Fibers, Clouds and Difference Clouds draw with the item's foreground and background colours: +/// their params are the `Fltr` settings plus those colours. +fn coloured_params(class: &str, item: &Descriptor, f: &Descriptor) -> Option<(&'static str, J)> { + let k = KNOWN.iter().find(|k| k.class == class)?; + // An `RGBC` of three `doub`s in 0–255, as Photoshop writes it (and we write it back); any + // other colour keeps the filter verbatim. + let colour = |key: &str| { + get_desc(item, key) + .filter(|c| c.class_id.is("RGBC") && exact_keys(c, &["Rd ", "Grn ", "Bl "])) + .filter(|c| ["Rd ", "Grn ", "Bl "].iter().all(|k| double(c, k).is_some_and(|v| (0.0..=255.0).contains(&v)))) + .and_then(rgbc_colour) + }; + // Only the settings Photoshop writes; an extra or repeated one keeps the filter verbatim. + let keys: &[&str] = if class == "Fbrs" { &["Vrnc", "Strg", "RndS"] } else { &["FlRs"] }; + if !exact_keys(f, keys) { + return None; + } + // The seeds (`RndS`, `FlRs`) are signed longs; ours is their bit pattern. + let mut p = match class { + "Fbrs" => json!({"variance": int(f, "Vrnc")?, "strength": int(f, "Strg")?, "seed": int(f, "RndS")? as u32}), + _ => json!({"seed": int(f, "FlRs")? as u32}), + }; + let o = p.as_object_mut()?; + o.insert("foreground".into(), colour("FrgC")?); + o.insert("background".into(), colour("BckC")?); + Some((k.command, p)) +} + +pub(super) fn seed_param(p: &J) -> Value { + Value::Integer(p.get("seed").and_then(J::as_u64).and_then(|n| u32::try_from(n).ok()).unwrap_or(0) as i32) +} + +/// `filter.filterGallery` params for a `GEfc` filter item; `None` when an effect or setting is +/// not modelled. +fn gallery_params(item: &Descriptor, f: &Descriptor) -> Option { + let list: Vec<&Descriptor> = match f.get("GEfs") { + Some(Value::List(list)) => list + .iter() + .map(|v| match v { + Value::Descriptor(e) => Some(e), + _ => None, + }) + .collect::>()?, + Some(_) => return None, + None if f.get("GEfk").is_some() => vec![f], + None => return None, + }; + let mut effects = Vec::with_capacity(list.len()); + for e in list { + let code = enum_of(e, "GEfk")?; + let g = GALLERY_EFFECTS.iter().find(|g| g.code.as_bytes() == code)?; + let mut params = Map::new(); + if g.key == "halftonePattern" { + // Do not turn an unfamiliar shape or malformed setting into the engine's defaults: + // keeping the whole item opaque preserves it through a subsequent save. That covers + // the stack's own descriptor too: a key beside `GEfs` would not be written back. + if f.get("GEfs").is_some() && !(f.class_id.is(GALLERY_CLASS) && only_keys(f, &["GEfs"])) { + return None; + } + if !e.class_id.is(GALLERY_CLASS) + || !only_keys(e, &["GEfk", "HlSz", "Cntr", "ScrT", "GELv"]) + || !matches!(e.get("GEfk"), Some(Value::Enumerated { type_id, .. }) if type_id.is("GEft")) + || e.get("GELv").is_some_and(|v| !matches!(v, Value::Boolean(_))) + { + return None; + } + int_in(e, "HlSz", 1, 12)?; + int_in(e, "Cntr", 0, 50)?; + params.insert("patternType".into(), json!(enum_name(e, "ScrT", "ScrT", &HALFTONE_PATTERNS)?)); + } + for (k, v) in &e.items { + if k.is("GEfk") || k.is("GELv") || g.key == "halftonePattern" && k.is("ScrT") { + continue; + } + let (_, ours) = g.settings.iter().find(|(psd, _)| k.is(psd))?; + params.insert((*ours).into(), json!(int_value(v)?)); + } + effects.push(json!({"filter": g.key, "params": params, "visible": bool_or(e, "GELv", true)})); + } + if effects.is_empty() { + return None; + } + let mut out = Map::new(); + out.insert("effects".into(), J::Array(effects)); + for (psd, ours) in [("FrgC", "foreground"), ("BckC", "background")] { + if let Some(c) = get_desc(item, psd).and_then(rgbc_colour) { + out.insert(ours.into(), c); + } + } + Some(J::Object(out)) +} + +fn colour_rgb(v: &J) -> Option<[f64; 3]> { + match v { + J::Array(a) => Some([a.first()?.as_f64()?, a.get(1)?.as_f64()?, a.get(2)?.as_f64()?]), + J::String(h) if h.len() == 7 && h.starts_with('#') => { + let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map(|v| f64::from(v) / 255.0); + Some([ch(1)?, ch(3)?, ch(5)?]) + } + _ => None, + } +} + +/// A `[r, g, b(, a)]` (0–1) or `#rrggbb` colour param as an `RGBC` descriptor (0–255). +pub(super) fn colour_rgbc(v: Option<&J>, default: f64) -> Result { + let Some(v) = v else { return Ok(rgbc(default)) }; + let rgb = colour_rgb(v).filter(|c| c.iter().all(|v| v.is_finite())).ok_or("a colour is not [r, g, b] or #rrggbb")?; + let c = |v: f64| Value::Double(v.clamp(0.0, 1.0) * 255.0); + Ok(Value::Descriptor(Descriptor::new("RGBC").with("Rd ", c(rgb[0])).with("Grn ", c(rgb[1])).with("Bl ", c(rgb[2])))) +} + +/// The `filterFXList` item for a `filter.filterGallery` smart filter. +pub(super) fn gallery_item(f: &SmartFilter) -> Result { + let p = &f.params; + if let Some(k) = p.as_object().and_then(|o| o.keys().find(|k| !is_meta(k) && !["effects", "foreground", "background"].contains(&k.as_str()))) { + return Err(format!("setting `{k}` has no PSD equivalent")); + } + let list = p.get("effects").and_then(J::as_array).ok_or("it has no effects")?; + let mut out = Vec::with_capacity(list.len()); + for e in list { + let key = e.get("filter").and_then(J::as_str).ok_or("an effect has no `filter`")?; + let g = GALLERY_EFFECTS.iter().find(|g| g.key == key).ok_or_else(|| format!("gallery effect `{key}` has no PSD equivalent"))?; + let params = e.get("params").filter(|v| v.is_object()).unwrap_or(e); + if let Some(k) = params.as_object().and_then(|o| { + o.keys().find(|k| { + !is_meta(k) + && !["filter", "params", "visible"].contains(&k.as_str()) + && !g.settings.iter().any(|(_, ours)| ours == k) + && !(key == "halftonePattern" && *k == "patternType") + }) + }) { + return Err(format!("gallery setting `{key}.{k}` has no PSD equivalent")); + } + let defaults = photocraft_algo::GalleryFilter::from_key(key).map(photocraft_algo::GalleryEffect::new); + let settings: Vec<(&str, Value)> = if key == "halftonePattern" { + vec![ + ("HlSz", whole(pf_range(params, "size", 1.0, 1.0, 12.0)?, (1, 12), "size")?), + ("Cntr", whole(pf_range(params, "contrast", 5.0, 0.0, 50.0)?, (0, 50), "contrast")?), + ("ScrT", enum_param(params, "patternType", "dot", "ScrT", &HALFTONE_PATTERNS)?), + ] + } else { + g.settings.iter().map(|(psd, ours)| (*psd, pi(params, ours, defaults.as_ref().map_or(0.0, |x| f64::from(x.get(ours)))))).collect() + }; + out.push((g.code, settings, e.get("visible").and_then(J::as_bool).unwrap_or(true))); + } + let gallery = || { + let mut d = Descriptor::new(GALLERY_CLASS); + d.name = UnicodeString::new_nul("Filter Gallery"); + d + }; + // One visible effect: inline, as Photoshop writes it. Otherwise the effect list. + let fltr = match out.as_slice() { + [] => return Err("it has no effects".into()), + [(code, settings, true)] => settings.iter().fold(gallery().with("GEfk", enumv("GEft", code)), |d, (k, v)| d.with(k, v.clone())), + _ => { + let list = out + .into_iter() + .map(|(code, settings, visible)| { + let d = settings.into_iter().fold(gallery(), |d, (k, v)| d.with(k, v)); + Value::Descriptor(d.with("GEfk", enumv("GEft", code)).with("GELv", Value::Boolean(visible))) + }) + .collect(); + gallery().with("GEfs", Value::List(list)) + } + }; + Ok(Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(GALLERY_NAME))) + .with("blendOptions", blend_options(f)) + .with("enab", Value::Boolean(f.visible)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", colour_rgbc(p.get("foreground"), 0.0)?) + .with("BckC", colour_rgbc(p.get("background"), 255.0)?) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(b"GEfc")))) +} + +/// Lowercase hex of `bytes`. +pub(crate) fn to_hex(bytes: &[u8]) -> String { + const H: &[u8; 16] = b"0123456789abcdef"; + let mut s = String::with_capacity(bytes.len() * 2); + for b in bytes { + s.push(char::from(H[usize::from(b >> 4)])); + s.push(char::from(H[usize::from(b & 15)])); + } + s +} + +pub(crate) fn from_hex(s: &str) -> Option> { + let d = |c: u8| (c as char).to_digit(16).map(|v| v as u8); + let b = s.as_bytes(); + if !b.len().is_multiple_of(2) { + return None; + } + b.as_chunks::<2>().0.iter().map(|c| Some(d(c[0])? << 4 | d(c[1])?)).collect() +} + +/// One `filterFXList` item → [`SmartFilter`] (an [`UNSUPPORTED_FILTER`] when not modelled). +pub fn filter_from_item(item: &Descriptor) -> SmartFilter { + let opts = get_desc(item, "blendOptions"); + let opacity = opts.and_then(|o| px(o, "Opct")).map_or(1.0, |v| (v / 100.0).clamp(0.0, 1.0) as f32); + let blend = opts.and_then(|o| enum_of(o, "Md ")).and_then(crate::effects_map::blend_from_id).unwrap_or(BlendMode::Normal); + let visible = bool_or(item, "enab", true); + let fltr = get_desc(item, "Fltr"); + let class = fltr.map(|f| String::from_utf8_lossy(f.class_id.as_bytes()).into_owned()); + let filter_id = int(item, "filterID").unwrap_or(0); + let class = class.or_else(|| KNOWN.iter().find(|k| k.filter_id == filter_id).map(|k| k.class.to_string())); + let known = match class.as_deref() { + Some(camera_raw::CLASS) => camera_raw::import_params(item).map(|p| (camera_raw::COMMAND, p)), + Some(displace::CLASS) => displace::import_params(item).map(|p| (displace::COMMAND, p)), + Some(hue_sat::CLASS) => fltr.and_then(hue_sat::import_params).map(|p| (hue_sat::COMMAND, p)), + Some(perspective_warp::CLASS) => perspective_warp::import_params(item).map(|p| (perspective_warp::COMMAND, p)), + Some(GALLERY_CLASS) => fltr.and_then(|f| gallery_params(item, f)).map(|p| (GALLERY_COMMAND, p)), + Some(c @ ("Fbrs" | "Clds" | "DfrC")) => fltr.and_then(|f| coloured_params(c, item, f)), + // A `Fltr` that is there but not a descriptor is settings we can't read: kept verbatim + // rather than taken for a filter stored without options (which would drop it on save). + _ if fltr.is_none() && item.get("Fltr").is_some() => None, + c => c.and_then(|c| known_params(c, fltr, item)), + }; + let (command, params) = match known { + Some((c, p)) => (c.to_string(), p), + None => { + let name = text_of(item, "Nm ").unwrap_or_default(); + // The stored descriptor carries neutral blending (the model's own fields replace + // them on export), so hiding or re-blending the filter leaves its params alone. + let mut neutral = item.clone(); + for (k, v) in &mut neutral.items { + if k.is("blendOptions") { + *v = blend_options(&SmartFilter { command: String::new(), params: J::Null, blend: BlendMode::Normal, opacity: 1.0, visible: true }); + } else if k.is("enab") { + *v = Value::Boolean(true); + } + } + (UNSUPPORTED_FILTER.to_string(), json!({"name": name.replace('&', ""), "filterID": filter_id, "psd": to_hex(&neutral.to_bytes())})) + } + }; + SmartFilter { command, params, blend, opacity, visible } +} diff --git a/crates/io/src/smart_map/placed.rs b/crates/io/src/smart_map/placed.rs new file mode 100644 index 0000000000..20079f9840 --- /dev/null +++ b/crates/io/src/smart_map/placed.rs @@ -0,0 +1,274 @@ +use super::export::{set, set_before, unit}; +use super::{Affine, BezierMesh, Descriptor, Id, ObjectArray, UnicodeString, Value, VersionedDescriptor, Warp, WarpStyle, get_desc, num, text_of}; + +/// Everything needed to write the placed-layer blocks of a smart object. +#[derive(Clone, Debug)] +pub struct PlacedSpec<'a> { + pub idnt: &'a str, + pub placed: &'a str, + /// Source pixels → document pixels. + pub transform: Affine, + /// Distort / Perspective: the full projective map (row-major 3×3), overriding `transform`. + pub perspective: Option<[f64; 9]>, + /// Source size in pixels. + pub size: (f64, f64), + /// Source resolution. + pub dpi: f64, + pub warp: Option<&'a Warp>, + /// `filterFX`, when the smart object has filters. + pub filter_fx: Option, +} + +/// Where the frame corners land (top-left, top-right, bottom-right, bottom-left), as x, y pairs. +/// The frame is [`crate::blocks::placed_frame`] of `warp`, the warp the block stores, for an +/// affine placement (the inverse of `parse_smart`), [`crate::blocks::perspective_frame`] of it +/// for a projective one (as `parse_smart_perspective` reads it). +fn quad_points(s: &PlacedSpec<'_>, warp: Option<&Warp>) -> [f64; 8] { + let (w, h) = s.size; + let [a, b, c, d, e, f] = s.transform.m; + let map = |x: f64, y: f64| match &s.perspective { + Some(p) => photocraft_algo::transform::Homography(*p).apply(x, y), + None => (a * x + c * y + e, b * x + d * y + f), + }; + let [x0, y0, x1, y1] = if s.perspective.is_some() { crate::blocks::perspective_frame(warp, w, h) } else { crate::blocks::placed_frame(warp, w, h) }; + let mut out = [0.0; 8]; + for (i, (x, y)) in [(x0, y0), (x1, y0), (x1, y1), (x0, y1)].into_iter().enumerate() { + (out[2 * i], out[2 * i + 1]) = map(x, y); + } + out +} + +/// The transform quad: the source corners (top-left, top-right, bottom-right, bottom-left). +fn quad(s: &PlacedSpec<'_>, warp: Option<&Warp>) -> Value { + Value::List(quad_points(s, warp).into_iter().map(Value::Double).collect()) +} + +/// Has the placement moved from the quad a template descriptor stores (`Trnf`)? +pub(super) fn quad_moved(template: &Descriptor, s: &PlacedSpec<'_>, warp: Option<&Warp>) -> bool { + let Some(Value::List(pts)) = template.get("Trnf") else { return true }; + let old: Vec = pts.iter().filter_map(|v| crate::blocks::num(Some(v))).collect(); + let new = quad_points(s, warp); + old.len() != 8 || old.iter().zip(new).any(|(a, b)| (a - b).abs() > 1e-6 * (1.0 + a.abs())) +} + +pub(crate) fn enumv(t: &str, v: &str) -> Value { + Value::Enumerated { type_id: Id::new(t), value: Id::new(v) } +} + +/// The mesh of a split warp: a custom warp of more than one patch, which `SoLd` stores as +/// `quiltWarp` and `PlLd` can't store. +fn split_mesh(w: Option<&Warp>) -> Option<&BezierMesh> { + w.filter(|w| w.style == WarpStyle::Custom).and_then(|w| w.mesh.as_ref()).filter(|m| m.points.len() != 16 && m.is_valid()) +} + +/// The warp `SoLd` stores: none, with a message, for a split warp of more patches along a side +/// than a `quiltWarp` is read with, so its corners are framed by the source rect as on reopen. +fn sold_warp<'a>(w: Option<&'a Warp>, warnings: &mut Vec) -> Option<&'a Warp> { + let too_big = split_mesh(w).is_some_and(|m| m.us.len().max(m.vs.len()) > crate::blocks::QUILT_MAX_PATCHES + 1); + if too_big { + warnings.push(format!("a split warp of more than {} patches along a side was written to PSD without its warp", crate::blocks::QUILT_MAX_PATCHES)); + return None; + } + w +} + +/// Style, bend, distortions, axis, bounds and order: the keys a `warp` and a `quiltWarp` +/// descriptor share, in Photoshop's order. +fn warp_head(class: &str, w: &Warp) -> Descriptor { + let [x0, y0, x1, y1] = w.bounds; + let bounds = Descriptor::new("classFloatRect") + .with("Top ", Value::Double(y0)) + .with("Left", Value::Double(x0)) + .with("Btom", Value::Double(y1)) + .with("Rght", Value::Double(x1)); + let d = Descriptor::new(class).with("warpStyle", enumv("warpStyle", w.style.psd_name())).with("warpValue", Value::Double(w.bend)); + // A cylinder is stored as its seven `warpValues` (#4112). + let d = if w.style == WarpStyle::Cylinder { d.with("warpValues", Value::List(w.values.iter().copied().map(Value::Double).collect())) } else { d }; + d.with("warpPerspective", Value::Double(w.h_distort)) + .with("warpPerspectiveOther", Value::Double(w.v_distort)) + .with("warpRotate", enumv("Ornt", if w.vertical { "Vrtc" } else { "Hrzn" })) + .with("bounds", Value::Descriptor(bounds)) + .with("uOrder", Value::Integer(4)) + .with("vOrder", Value::Integer(4)) +} + +/// The control points of a custom warp: an object array of their x and y. +fn mesh_points(m: &BezierMesh) -> Value { + let body = Descriptor::new("rationalPoint") + .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[0]).collect() }) + .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: m.points.iter().map(|p| p[1]).collect() }); + Value::ObjectArray(ObjectArray { prefix: m.points.len() as u32, body }) +} + +/// The `warp` descriptor (also the `PlLd` warp). A split warp is written as no warp: `SoLd` +/// keeps it in `quiltWarp` ([`quilt_desc`]), next to this `warpNone`, as Photoshop does. A +/// custom warp without a usable mesh is written as no warp, with a message. A cylinder is +/// written as its seven `warpValues`. +pub(super) fn warp_desc(w: Option<&Warp>, size: (f64, f64), warnings: &mut Vec) -> Value { + let none = Warp::none([0.0, 0.0, size.0, size.1]); + let mut w = w.unwrap_or(&none).clone(); + let mesh = match (&w.style, &w.mesh) { + (WarpStyle::Custom, Some(m)) if m.points.len() == 16 => Some(m.clone()), + (WarpStyle::Custom, Some(m)) if m.is_valid() => { + w = none.clone(); + None + } + (WarpStyle::Custom, _) => { + warnings.push("a custom warp without a valid mesh was written to PSD without its warp".into()); + w = none.clone(); + None + } + _ => None, + }; + let mut d = warp_head("warp", &w); + if let Some(m) = mesh { + d = d.with("customEnvelopeWarp", Value::Descriptor(Descriptor::new("customEnvelopeWarp").with("meshPoints", mesh_points(&m)))); + } + Value::Descriptor(d) +} + +/// The `quiltWarp` descriptor of a split warp, in Photoshop's layout: `deformNumCols` × +/// `deformNumRows` control points, and the patch boundaries (`quiltSliceX` / `quiltSliceY`) at +/// the knots across the warp's box. The imported `quiltWarp` (`old`) gives its own slices where +/// they still bound the mesh's patches, so their units and padding stay Photoshop's. +fn quilt_desc(w: &Warp, mesh: &BezierMesh, old: Option<&Descriptor>) -> Value { + let old_env = old.and_then(|q| get_desc(q, "customEnvelopeWarp")); + let [x0, y0, x1, y1] = w.bounds; + let slices = |key: &str, knots: &[f64], lo: f64, hi: f64| { + if let Some(env) = old_env + && crate::blocks::quilt_knots(env, key, knots.len() - 1).as_deref() == Some(knots) + && let Some(v) = env.get(key) + { + return v.clone(); + } + let (lo, hi) = if (hi - lo).is_finite() && hi > lo { (lo, hi) } else { (0.0, 1.0) }; + let values: Vec = knots.iter().map(|u| lo + u * (hi - lo)).collect(); + let prefix = values.len() as u32; + Value::ObjectArray(ObjectArray { prefix, body: Descriptor::new("UntF").with(key, Value::UnitFloats { unit: *b"#Pxl", values }) }) + }; + let envelope = Descriptor::new("customEnvelopeWarp") + .with("quiltSliceX", slices("quiltSliceX", &mesh.us, x0, x1)) + .with("quiltSliceY", slices("quiltSliceY", &mesh.vs, y0, y1)) + .with("meshPoints", mesh_points(mesh)); + Value::Descriptor( + warp_head("quiltWarp", w) + .with("deformNumRows", Value::Integer(mesh.ny() as i32)) + .with("deformNumCols", Value::Integer(mesh.nx() as i32)) + .with("customEnvelopeWarp", Value::Descriptor(envelope)), + ) +} + +fn ratio(n: i32, d: i32) -> Value { + Value::Descriptor(Descriptor::new("null").with("numerator", Value::Integer(n)).with("denominator", Value::Integer(d))) +} + +fn pad4(mut v: Vec) -> Vec { + while !v.len().is_multiple_of(4) { + v.push(0); + } + v +} + +/// `SoLd` block data. With `template` (the imported descriptor) every key it has is kept and only +/// the placed id (for a duplicate), transform (when it moved), warp and filter stack are rewritten; +/// otherwise a fresh descriptor in Photoshop's key order. A split warp goes in `quiltWarp`. +pub fn sold_bytes(template: Option<&Descriptor>, s: &PlacedSpec<'_>, warnings: &mut Vec) -> Vec { + let stored = sold_warp(s.warp, warnings); + let warp = warp_desc(stored, s.size, warnings); + let split = stored.zip(split_mesh(stored)); + let d = match template { + Some(t) => { + let mut d = t.clone(); + if text_of(t, "placed").as_deref() != Some(s.placed) { + set(&mut d, "placed", Value::Text(UnicodeString::new_nul(s.placed))); + } + if quad_moved(t, s, stored) { + set(&mut d, "Trnf", quad(s, stored)); + set(&mut d, "nonAffineTransform", quad(s, stored)); + } + set(&mut d, "warp", warp); + // `quiltWarp` is read before `warp`, so the imported one stays only while it still + // says what the layer's warp is (unchanged, or unreadable while the layer has none); + // otherwise it is rewritten, or dropped for a warp that isn't split. + let old = get_desc(t, "quiltWarp"); + let kept = old.is_some_and(|q| match crate::blocks::warp_of(q) { + Some(w) => w.as_ref() == stored, + None => s.warp.is_none(), + }); + if !kept { + match split { + Some((w, m)) => set_before(&mut d, "quiltWarp", quilt_desc(w, m, old), "warp"), + None => d.items.retain(|(k, _)| !k.is("quiltWarp")), + } + } + match &s.filter_fx { + Some(fx) => set(&mut d, "filterFX", Value::Descriptor(fx.clone())), + None => d.items.retain(|(k, _)| !k.is("filterFX")), + } + d + } + None => { + let mut d = Descriptor::new("null") + .with("Idnt", Value::Text(UnicodeString::new_nul(s.idnt))) + .with("placed", Value::Text(UnicodeString::new_nul(s.placed))) + .with("PgNm", Value::Integer(1)) + .with("totalPages", Value::Integer(1)) + .with("Crop", Value::Integer(1)) + .with("frameStep", ratio(0, 600)) + .with("duration", ratio(0, 600)) + .with("frameCount", Value::Integer(1)) + .with("Annt", Value::Integer(16)) + .with("Type", Value::Integer(2)) + .with("Trnf", quad(s, stored)) + .with("nonAffineTransform", quad(s, stored)); + if let Some((w, m)) = split { + d = d.with("quiltWarp", quilt_desc(w, m, None)); + } + let mut d = d + .with("warp", warp) + .with("Sz ", Value::Descriptor(Descriptor::new("Pnt ").with("Wdth", Value::Double(s.size.0)).with("Hght", Value::Double(s.size.1)))) + .with("Rslt", unit(b"#Rsl", s.dpi)); + if let Some(fx) = &s.filter_fx { + d = d.with("filterFX", Value::Descriptor(fx.clone())); + } + d.with("comp", Value::Integer(-1)) + .with("compInfo", Value::Descriptor(Descriptor::new("null").with("compID", Value::Integer(-1)).with("originalCompID", Value::Integer(-1)))) + } + }; + let mut out = b"soLD".to_vec(); + out.extend_from_slice(&4u32.to_be_bytes()); + out.extend(VersionedDescriptor::new(d).to_bytes()); + pad4(out) +} + +/// `PlLd` block data (`plcL` version 3): the legacy placed-layer record Photoshop writes next to +/// `SoLd`. It has no split warp: it stores none, its corners framed by the source rect. +pub fn plld_bytes(s: &PlacedSpec<'_>, warnings: &mut Vec) -> Vec { + let warp = s.warp.filter(|_| split_mesh(s.warp).is_none()); + let mut out = b"plcL".to_vec(); + out.extend_from_slice(&3u32.to_be_bytes()); + let id = s.idnt.as_bytes(); + let n = id.len().min(255); + out.push(n as u8); + out.extend_from_slice(&id[..n]); + for v in [1i32, 1, 16, 2] { + out.extend_from_slice(&v.to_be_bytes()); + } + if let Value::List(q) = quad(s, warp) { + for v in q { + if let Value::Double(x) = v { + out.extend_from_slice(&x.to_be_bytes()); + } + } + } + out.extend_from_slice(&0u32.to_be_bytes()); + let Value::Descriptor(w) = warp_desc(warp, s.size, warnings) else { return pad4(out) }; + out.extend(VersionedDescriptor::new(w).to_bytes()); + pad4(out) +} + +/// The source size a `soLD` descriptor stores (`Sz `). +pub fn stored_size(d: &Descriptor) -> Option<(f64, f64)> { + let sz = get_desc(d, "Sz ")?; + Some((num(sz.get("Wdth"))?, num(sz.get("Hght"))?)).filter(|(w, h)| *w > 0.0 && *h > 0.0) +} diff --git a/crates/io/src/smart_map/tests.rs b/crates/io/src/smart_map/tests.rs new file mode 100644 index 0000000000..22808b08a2 --- /dev/null +++ b/crates/io/src/smart_map/tests.rs @@ -0,0 +1,2315 @@ +use super::*; + +fn count(hay: &[u8], needle: &[u8]) -> usize { + hay.windows(needle.len()).filter(|w| *w == needle).count() +} + +/// A freshly built placed layer writes `warp` (key and class) as a length-4 string, as +/// Photoshop does; the code form is a different key to Photoshop. +#[test] +fn fresh_placed_warp_is_a_string_id() { + let spec = + PlacedSpec { idnt: "id", placed: "id", transform: Affine::IDENTITY, perspective: None, size: (10.0, 10.0), dpi: 72.0, warp: None, filter_fx: None }; + let mut warnings = Vec::new(); + let sold = sold_bytes(None, &spec, &mut warnings); + let plld = plld_bytes(&spec, &mut warnings); + // `SoLd`: the `warp` key and the warp descriptor's class; `PlLd`: the descriptor's class. + for (bytes, n) in [(&sold, 2), (&plld, 1)] { + assert_eq!(count(bytes, b"\0\0\0\x04warp"), n); + assert_eq!(count(bytes, b"\0\0\0\0warp"), 0); + } + let d = sold_descriptor(&sold).unwrap(); + assert!(matches!(d.get("warp"), Some(Value::Descriptor(w)) if w.class_id == Id::Str(b"warp".to_vec()))); +} + +fn sf(command: &str, params: J) -> SmartFilter { + SmartFilter { command: command.into(), params, blend: BlendMode::Normal, opacity: 1.0, visible: true } +} + +#[test] +fn modelled_filters_round_trip() { + let filters = vec![ + sf("filter.blur.gaussianBlur", json!({"radius": 4.0})), + sf("filter.blur.boxBlur", json!({"radius": 5.0})), + sf("filter.blur.motionBlur", json!({"angle": -20, "distance": 10.0})), + sf("filter.blur.surfaceBlur", json!({"radius": 12.0, "threshold": 40})), + sf("filter.blur.smartBlur", json!({"radius": 5.0, "threshold": 25.0, "quality": "high", "mode": "normal"})), + sf("filter.blur.smartBlur", json!({"radius": 3.0, "threshold": 30.0, "quality": "medium", "mode": "overlayEdge"})), + sf("filter.blur.smartBlur", json!({"radius": 0.5, "threshold": 0.1, "quality": "low", "mode": "edgeOnly"})), + sf("filter.blur.radialBlur", json!({"amount": 10, "method": "spin", "quality": "draft"})), + sf("filter.blur.radialBlur", json!({"amount": 20, "method": "zoom", "quality": "good"})), + sf("filter.blur.radialBlur", json!({"amount": 100, "method": "spin", "quality": "best"})), + sf("filter.sharpen.unsharpMask", json!({"amount": 150.0, "radius": 2.0, "threshold": 0})), + sf("filter.noise.addNoise", json!({"amount": 12.0, "distribution": "uniform", "monochromatic": false, "seed": 1234})), + sf("filter.noise.median", json!({"radius": 3.0})), + sf("filter.other.highPass", json!({"radius": 3.0})), + sf("filter.other.maximum", json!({"radius": 2.0})), + sf("filter.other.minimum", json!({"radius": 2.0})), + sf("filter.pixelate.mosaic", json!({"cellSize": 8.0})), + sf("filter.pixelate.mezzotint", json!({"type": "mediumStrokes"})), + sf("filter.pixelate.colorHalftone", json!({"maxRadius": 6, "channel1": 10, "channel2": 20, "channel3": 30, "channel4": 40})), + sf("filter.stylize.emboss", json!({"angle": 135, "height": 3, "amount": 100})), + sf("filter.sharpen.smartSharpen", json!({"amount": 120.0, "radius": 2.5, "reduceNoise": 5.0})), + sf("filter.noise.dustAndScratches", json!({"radius": 4, "threshold": 20})), + sf("filter.stylize.diffuse", json!({"mode": "normal"})), + sf("filter.stylize.diffuse", json!({"mode": "darkenOnly"})), + sf("filter.stylize.diffuse", json!({"mode": "lightenOnly"})), + sf("filter.stylize.diffuse", json!({"mode": "anisotropic"})), + sf("filter.stylize.traceContour", json!({"level": 60, "edge": "lower"})), + sf("filter.stylize.traceContour", json!({"level": 128, "edge": "upper"})), + sf("filter.stylize.traceContour", json!({"level": 0, "edge": "upper"})), + sf("filter.stylize.traceContour", json!({"level": 255, "edge": "lower"})), + sf("filter.pixelate.crystallize", json!({"cellSize": 10})), + sf("filter.pixelate.crystallize", json!({"cellSize": 3})), + sf("filter.pixelate.crystallize", json!({"cellSize": 300})), + sf("filter.pixelate.pointillize", json!({"cellSize": 7, "background": [1.0, 1.0, 1.0, 1.0]})), + sf("filter.pixelate.pointillize", json!({"cellSize": 300, "background": [0.0, 0.5, 1.0, 1.0]})), + sf("filter.other.offset", json!({"horizontal": 12, "vertical": -7, "undefinedAreas": "transparent"})), + sf("filter.other.custom", json!({"kernel": SHARPEN, "scale": 1, "offset": 0})), + sf("image.adjustments.levels", json!({"inBlack": 20, "inWhite": 220, "gamma": 1.4, "red": {"inBlack": 0, "inWhite": 255, "gamma": 0.8}})), + sf("image.adjustments.curves", json!({"points": [[0.0, 0.0], [64.0, 40.0], [255.0, 255.0]], "blue": [[0.0, 20.0], [255.0, 235.0]]})), + sf("image.adjustments.shadowsHighlights", json!({})), + sf("filter.sharpen.sharpen", json!({})), + // Photoshop stores Dust & Scratches' radius in whole pixels (a `long`). + sf("filter.noise.dustAndScratches", json!({"radius": 4, "threshold": 12})), + sf( + "image.adjustments.hueSaturation", + json!({"colorize": false, "hue": 10, "saturation": -20, "lightness": 5, "reds": {"hue": 5, "saturation": 0, "lightness": -3, "range": [315, 345, 15, 45]}}), + ), + // A pencil-drawn curve: 256 output levels (`Mpng`). + sf("image.adjustments.curves", json!({"pencil": true, "points": (0..256).rev().collect::>()})), + sf("filter.distort.pinch", json!({"amount": -28})), + sf("filter.distort.polarCoordinates", json!({"mode": "rectangularToPolar"})), + sf("filter.distort.ripple", json!({"amount": 250, "size": "small"})), + sf("filter.distort.spherize", json!({"amount": 60, "mode": "verticalOnly"})), + sf("filter.distort.twirl", json!({"angle": -355})), + sf( + "filter.distort.wave", + json!({"type": "square", "generators": 3, "wavelengthMin": 20, "wavelengthMax": 90, "amplitudeMin": 4, "amplitudeMax": 30, + "undefinedAreas": "repeat", "seed": 4_000_000_000u32}), + ), + sf("filter.distort.zigZag", json!({"amount": 30, "ridges": 7, "style": "aroundCenter"})), + sf("image.adjustments.invert", json!({})), + ]; + for mut f in filters.into_iter().chain(wind_extrude_tiles_settings()) { + f.blend = BlendMode::Overlay; + f.opacity = 0.75; + f.visible = false; + let d = item_for_filter(&f).unwrap(); + let bytes = d.to_bytes(); + let back = filter_from_item(&Descriptor::from_bytes(&bytes).unwrap()); + assert_eq!(back, f, "{}", f.command); + } +} + +/// The filters without options, their measured `filterID`s (as Photoshop wrote them). +const MEASURED_NO_OPTIONS: [(&str, i32); 11] = [ + ("filter.blur.blur", 1114403360), + ("filter.blur.blurMore", 1114403405), + ("filter.blur.average", 1098281575), + ("filter.noise.despeckle", 1148416099), + ("filter.stylize.findEdges", 1181639749), + ("filter.pixelate.facet", 1180922912), + ("filter.pixelate.fragment", 1181902701), + ("filter.sharpen.sharpen", 1399353968), + ("filter.sharpen.sharpenMore", 1399353933), + ("filter.sharpen.sharpenEdges", 1399353925), + ("filter.stylize.solarize", 1399616122), +]; + +/// A filter without options as Photoshop saves it: `hasoptions` false, no `Fltr`, a localized +/// name and the current colours. +fn photoshop_no_options_item(name: &str, filter_id: Value) -> Descriptor { + let rgb = |r: f64, g: f64, b: f64| { + Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(r)).with("Grn ", Value::Double(g)).with("Bl ", Value::Double(b))) + }; + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(name))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 60.0)).with("Md ", enumv("BlnM", "Mltp")))) + .with("enab", Value::Boolean(false)) + .with("hasoptions", Value::Boolean(false)) + .with("FrgC", rgb(167.0, 157.0, 147.0)) + .with("BckC", rgb(255.0, 255.0, 255.0)) + .with("filterID", filter_id) +} + +#[test] +fn filters_without_options_round_trip_in_the_measured_shape() { + let keys = ["Nm ", "blendOptions", "enab", "hasoptions", "FrgC", "BckC", "filterID"]; + for (command, id) in MEASURED_NO_OPTIONS { + let mut f = sf(command, json!({})); + f.blend = BlendMode::Overlay; + f.opacity = 0.75; + f.visible = false; + let d = item_for_filter(&f).unwrap(); + let got: Vec = d.items.iter().map(|(k, _)| String::from_utf8_lossy(k.as_bytes()).into_owned()).collect(); + assert_eq!(got, keys, "{command}"); + assert_eq!(d.get("hasoptions"), Some(&Value::Boolean(false)), "{command}"); + assert_eq!(d.get("filterID"), Some(&Value::Integer(id)), "{command}"); + let back = filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()); + assert_eq!(back, f, "{command}"); + // Keys an old record may carry have no effect on these filters: still written. + let extra = item_for_filter(&sf(command, json!({"radius": 3.0, "layer": "L"}))).unwrap(); + assert_eq!(extra.get("filterID"), Some(&Value::Integer(id)), "{command}"); + for p in [json!(null), json!("x"), json!([1, 2])] { + assert!(item_for_filter(&sf(command, p)).is_ok(), "{command}"); + } + } +} + +#[test] +fn photoshop_filters_without_options_open_editable() { + for (command, id) in MEASURED_NO_OPTIONS { + let item = photoshop_no_options_item("Weichzeichnen", Value::Integer(id)); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, command); + assert_eq!(f.params, json!({})); + assert_eq!((f.visible, f.blend), (false, BlendMode::Multiply)); + assert!((f.opacity - 0.6).abs() < 1e-6); + } +} + +#[test] +fn malformed_filters_without_options_are_kept_verbatim() { + let blur = code(b"Blr "); + let cases = [ + // An id no filter has, a missing id, ids of the wrong type. + photoshop_no_options_item("X", Value::Integer(code(b"Zzzz"))), + Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)), + photoshop_no_options_item("X", Value::Double(f64::from(blur))), + photoshop_no_options_item("X", Value::Double(f64::NAN)), + photoshop_no_options_item("X", Value::Text(UnicodeString::new_nul("Blr "))), + photoshop_no_options_item("X", Value::Integer(-1)), + // Settings we don't know of on a filter that has none. + photoshop_no_options_item("X", Value::Integer(blur)).with("Fltr", Value::Descriptor(Descriptor::new("Blr ").with("Amnt", Value::Integer(5)))), + // Settings that aren't a descriptor, on a filter that has none. + photoshop_no_options_item("X", Value::Integer(blur)).with("Fltr", Value::Integer(5)), + photoshop_no_options_item("X", Value::Integer(blur)).with("Fltr", Value::Text(UnicodeString::new_nul("Blr "))), + photoshop_no_options_item("X", Value::Integer(781)).with("Fltr", Value::Integer(5)), + ]; + for item in cases { + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}"); + // Written back (a bare item gains blending and visibility) and still not editable. + let written = item_for_filter(&f).unwrap(); + assert_eq!(written.get("Fltr"), item.get("Fltr"), "{item:?}"); + let back = filter_from_item(&written); + assert_eq!((back.command.as_str(), &back.params["filterID"]), (UNSUPPORTED_FILTER, &f.params["filterID"])); + } +} + +/// Smart Sharpen's `Fltr` in the measured shape (no `presetKind`, no `Angl`). +fn smart_sharpen(amount: f64, radius: f64, noise: f64, blur: &str) -> Descriptor { + Descriptor::new("smartSharpen") + .with("useLegacy", Value::Boolean(false)) + .with("Amnt", unit(b"#Prc", amount)) + .with("Rds ", unit(b"#Pxl", radius)) + .with("noiseReduction", unit(b"#Prc", noise)) + .with("blur", enumv("blurType", blur)) +} + +fn dust(radius: i32, threshold: i32) -> Descriptor { + Descriptor::new("DstS").with("Rds ", Value::Integer(radius)).with("Thsh", Value::Integer(threshold)) +} + +/// The `Fltr` descriptor PhotoCraft writes for `f`, after a byte round trip. +fn written_fltr(f: &SmartFilter) -> Descriptor { + let d = Descriptor::from_bytes(&item_for_filter(f).unwrap().to_bytes()).unwrap(); + get_desc(&d, "Fltr").unwrap().clone() +} + +/// Measured in Photoshop: Smart Sharpen 120 %, 2.5 px, noise 5 %, Gaussian Blur +/// (filterID 1131); the default entry adds `presetKind` first, which is not kept. +#[test] +fn smart_sharpen_photoshop_entry_opens_and_writes_back() { + let fltr = smart_sharpen(120.0, 2.5, 5.0, "GsnB"); + let item = ps_item("Smart Sharpen...", fltr.clone(), 1131); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, "filter.sharpen.smartSharpen"); + assert_eq!(f.params, json!({"amount": 120.0, "radius": 2.5, "reduceNoise": 5.0})); + assert_eq!(written_fltr(&f), fltr); + let d = item_for_filter(&f).unwrap(); + assert_eq!(int(&d, "filterID"), Some(1131)); + assert_eq!(get_desc(&d, "Fltr").unwrap().class_id, Id::Str(b"smartSharpen".to_vec())); + // The four-character keys and enum values are codes, as Photoshop writes them. + let bytes = d.to_bytes(); + assert_eq!(count(&bytes, b"\0\0\0\0blurenum\0\0\0\x08blurType\0\0\0\0GsnB"), 1); + assert_eq!(count(&bytes, b"\0\0\0\0Rds UntF#Pxl"), 1); + + // With `presetKind` (any value): opens, but the params hold only the engine's own keys + // (it rejects unknown ones), and a re-save leaves it out as Photoshop does for explicit + // values. + for kind in ["presetKindDefault", "presetKindCustom", "Xxxx"] { + let mut with_preset = Descriptor::new("smartSharpen").with("presetKind", enumv("presetKindType", kind)); + with_preset.items.extend(smart_sharpen(200.0, 1.0, 10.0, "GsnB").items); + let f = filter_from_item(&Descriptor::from_bytes(&ps_item("Smart Sharpen...", with_preset, 1131).to_bytes()).unwrap()); + assert_eq!(f.command, "filter.sharpen.smartSharpen"); + let keys: Vec<&str> = f.params.as_object().unwrap().keys().map(String::as_str).collect(); + assert_eq!(keys.len(), 3, "{keys:?}"); + assert!(keys.iter().all(|k| ["amount", "radius", "reduceNoise"].contains(k)), "{keys:?}"); + assert_eq!(f.params, json!({"amount": 200.0, "radius": 1.0, "reduceNoise": 10.0})); + assert_eq!(written_fltr(&f), smart_sharpen(200.0, 1.0, 10.0, "GsnB")); + } + + // Without options PhotoCraft writes the engine's defaults, Gaussian Blur. + assert_eq!(written_fltr(&sf("filter.sharpen.smartSharpen", json!({}))), smart_sharpen(100.0, 1.0, 10.0, "GsnB")); +} + +/// Lens Blur (Photoshop's default), Motion Blur and legacy mode render differently from +/// PhotoCraft's Smart Sharpen, so those entries stay verbatim. +#[test] +fn smart_sharpen_settings_we_dont_render_stay_verbatim() { + let mut default_entry = Descriptor::new("smartSharpen").with("presetKind", enumv("presetKindType", "presetKindDefault")); + default_entry.items.extend(smart_sharpen(200.0, 1.0, 10.0, "lensBlur").items); + let motion = smart_sharpen(80.0, 1.5, 0.0, "MtnB").with("Angl", Value::Integer(30)); + let mut legacy = smart_sharpen(100.0, 1.0, 10.0, "GsnB"); + set(&mut legacy, "useLegacy", Value::Boolean(true)); + for fltr in [default_entry, motion, legacy] { + let item = ps_item("Smart Sharpen...", fltr, 1131); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(f.params["filterID"], 1131); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +#[test] +fn malformed_smart_sharpen_is_kept_verbatim() { + let g = || smart_sharpen(100.0, 1.0, 10.0, "GsnB"); + let without = |key: &str| { + let mut d = g(); + d.items.retain(|(k, _)| !k.is(key)); + d + }; + let with = |key: &str, v: Value| { + let mut d = g(); + set(&mut d, key, v); + d + }; + let cases = vec![ + without("useLegacy"), + without("Amnt"), + without("Rds "), + without("noiseReduction"), + without("blur"), + with("Amnt", Value::Double(100.0)), + with("Amnt", unit(b"#Pxl", 100.0)), + with("Rds ", Value::Integer(1)), + with("useLegacy", Value::Integer(0)), + with("Amnt", unit(b"#Prc", 0.0)), + with("Amnt", unit(b"#Prc", 501.0)), + with("Rds ", unit(b"#Pxl", 0.05)), + with("Rds ", unit(b"#Pxl", 65.0)), + with("Rds ", unit(b"#Pxl", f64::NAN)), + with("noiseReduction", unit(b"#Prc", -1.0)), + with("noiseReduction", unit(b"#Prc", f64::INFINITY)), + with("blur", enumv("blurType", "Xxxx")), + with("blur", enumv("BlrM", "GsnB")), + with("blur", Value::Integer(0)), + with("presetKind", Value::Integer(1)), + g().with("Angl", Value::Integer(30)), + g().with("shadowMode", Value::Descriptor(Descriptor::new("adaptCorrectTones"))), + ]; + for fltr in cases { + let item = ps_item("Smart Sharpen...", fltr, 1131); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{:?}", get_desc(&item, "Fltr")); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes()); + } + // No `Fltr` at all (hasoptions false). + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(1131)); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); +} + +/// Measured in Photoshop: Dust & Scratches at its defaults (1, 0) and at 4, 20 (filterID +/// `DstS`, 1148417107; both settings `long`). +#[test] +fn dust_and_scratches_photoshop_entry_opens_and_writes_back() { + for (radius, threshold) in [(1, 0), (4, 20), (500, 255)] { + let fltr = dust(radius, threshold); + let item = ps_item("Dust & Scratches...", fltr.clone(), 1_148_417_107); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, "filter.noise.dustAndScratches"); + assert_eq!(f.params, json!({"radius": radius, "threshold": threshold})); + assert_eq!(written_fltr(&f), fltr); + assert_eq!(int(&item_for_filter(&f).unwrap(), "filterID"), Some(1_148_417_107)); + } + // No `Fltr` (hasoptions false): kept verbatim. + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"DstS"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); +} + +#[test] +fn malformed_dust_and_scratches_is_kept_verbatim() { + let cases = vec![ + Descriptor::new("DstS").with("Thsh", Value::Integer(0)), + Descriptor::new("DstS").with("Rds ", Value::Integer(1)), + Descriptor::new("DstS").with("Rds ", Value::Double(1.0)).with("Thsh", Value::Integer(0)), + Descriptor::new("DstS").with("Rds ", unit(b"#Pxl", 1.0)).with("Thsh", Value::Integer(0)), + Descriptor::new("DstS").with("Rds ", Value::Integer(1)).with("Thsh", Value::Boolean(false)), + dust(0, 0), + dust(501, 0), + dust(1, -1), + dust(1, 256), + dust(i32::MIN, i32::MAX), + dust(1, 0).with("Xtra", Value::Integer(1)), + ]; + for fltr in cases { + let item = ps_item("Dust & Scratches...", fltr, code(b"DstS")); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{:?}", get_desc(&item, "Fltr")); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +/// Every option's range ends round trip; hostile params are written in range (the engine +/// clamps the same way), never a panic. +#[test] +fn smart_sharpen_and_dust_option_values_round_trip() { + let mut cases = Vec::new(); + for amount in [1.0, 100.0, 500.0] { + for radius in [0.1, 1.0, 64.0] { + for noise in [0.0, 10.0, 100.0] { + cases.push(sf("filter.sharpen.smartSharpen", json!({"amount": amount, "radius": radius, "reduceNoise": noise}))); + } + } + } + for radius in [1, 2, 100, 500] { + for threshold in [0, 1, 128, 255] { + cases.push(sf("filter.noise.dustAndScratches", json!({"radius": radius, "threshold": threshold}))); + } + } + for f in cases { + let back = filter_from_item(&Descriptor::from_bytes(&item_for_filter(&f).unwrap().to_bytes()).unwrap()); + assert_eq!(back, f); + } + let defaults = json!({"amount": 100.0, "radius": 1.0, "reduceNoise": 10.0}); + for (command, p, want) in [ + ( + "filter.sharpen.smartSharpen", + json!({"amount": 1e9, "radius": -5.0, "reduceNoise": "x"}), + json!({"amount": 500.0, "radius": 0.1, "reduceNoise": 10.0}), + ), + ("filter.sharpen.smartSharpen", json!({"presetKind": "presetKindDefault", "amount": null}), defaults.clone()), + ("filter.sharpen.smartSharpen", json!(null), defaults.clone()), + ("filter.noise.dustAndScratches", json!({"radius": 2.4, "threshold": 1e12}), json!({"radius": 2, "threshold": 255})), + ("filter.noise.dustAndScratches", json!({"radius": -3, "threshold": -3}), json!({"radius": 1, "threshold": 0})), + ("filter.noise.dustAndScratches", json!([]), json!({"radius": 1, "threshold": 0})), + ] { + let back = filter_from_item(&item_for_filter(&sf(command, p.clone())).unwrap()); + assert_eq!((back.command.as_str(), &back.params), (command, &want), "{p}"); + } +} + +/// Every option value of Wind, Extrude and Tiles (and the ends of their ranges). +fn wind_extrude_tiles_settings() -> Vec { + let mut v = Vec::new(); + for method in ["wind", "blast", "stagger"] { + for direction in ["fromRight", "fromLeft"] { + v.push(sf("filter.stylize.wind", json!({"method": method, "direction": direction}))); + } + } + for t in ["blocks", "pyramids"] { + for mode in ["random", "levelBased"] { + for (solid, mask) in [(false, false), (true, false), (false, true), (true, true)] { + v.push(sf( + "filter.stylize.extrude", + json!({"type": t, "size": 7, "depth": 9, "depthMode": mode, "solidFrontFaces": solid, "maskIncompleteBlocks": mask}), + )); + } + } + } + v.push(sf( + "filter.stylize.extrude", + json!({"type": "blocks", "size": 2, "depth": 1, "depthMode": "random", "solidFrontFaces": false, "maskIncompleteBlocks": false}), + )); + v.push(sf( + "filter.stylize.extrude", + json!({"type": "pyramids", "size": 255, "depth": 255, "depthMode": "levelBased", "solidFrontFaces": true, "maskIncompleteBlocks": true}), + )); + for fill in ["background", "foreground", "inverse", "unaltered"] { + v.push(sf( + "filter.stylize.tiles", + json!({"count": 6, "maxOffset": 20, "fill": fill, "foreground": [1.0, 0.0, 0.5, 1.0], "background": [0.0, 0.5, 1.0, 1.0]}), + )); + } + for (count, offset) in [(1, 1), (99, 99)] { + v.push(sf( + "filter.stylize.tiles", + json!({"count": count, "maxOffset": offset, "fill": "background", "foreground": [0.0, 0.0, 0.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]}), + )); + } + v +} + +/// A `filterFX` entry shaped as Photoshop saves Wind, Extrude and Tiles (measured in Photoshop: +/// applied through Action Manager to a smart object, saved, `filterFX` read back): the item's +/// `FrgC`/`BckC` are the document colours, `Fltr` holds the settings, `filterID` the class code. +fn measured_item(fltr: Descriptor, fg: f64, bg: f64) -> Descriptor { + let id = code(fltr.class_id.as_bytes().try_into().unwrap()); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filter..."))) + .with("blendOptions", blend_options(&sf("", J::Null))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(fg)) + .with("BckC", rgbc(bg)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(id)) +} + +/// The `Fltr` descriptors Photoshop 27 wrote for these filters (one smart object each, saved as +/// PSD and read back), with the settings PhotoCraft reads from them. +const PHOTOSHOP_FLTR: [(&str, &str, &str); 6] = [ + ("filter.distort.pinch", r#"{"amount":-28}"#, "0000000600500069006e00630068000000000000506e63680000000100000000416d6e746c6f6e67ffffffe4"), + ("filter.distort.twirl", r#"{"angle":-355}"#, "000000060054007700690072006c0000000000005477726c0000000100000000416e676c6c6f6e67fffffe9d"), + ( + "filter.distort.ripple", + r#"{"amount":-273,"size":"large"}"#, + "000000070052006900700070006c006500000000000052706c650000000200000000416d6e746c6f6e67fffffeef0000000052706c53656e756d0000000052706c53000000004c726720", + ), + ( + "filter.distort.spherize", + r#"{"amount":-1,"mode":"horizontalOnly"}"#, + "00000009005300700068006500720069007a0065000000000000537068720000000200000000416d6e746c6f6e67ffffffff000000005370684d656e756d000000005370684d0000000048727a4f", + ), + ( + "filter.distort.polarCoordinates", + r#"{"mode":"polarToRectangular"}"#, + "000000120050006f006c0061007200200043006f006f007200640069006e0061007400650073000000000000506c72200000000100000000436e7672656e756d00000000436e767200000000506c7252", + ), + ( + "filter.distort.zigZag", + r#"{"amount":-42,"ridges":11,"style":"pondRipples"}"#, + "00000007005a00690067005a006100670000000000005a675a670000000300000000416d6e746c6f6e67ffffffd6000000004e6d62526c6f6e670000000b000000005a5a5479656e756d000000005a5a547900000000506e6452", + ), +]; + +/// Photoshop's Wave with Scale 100% / 28%: PhotoCraft's Wave has no Scale. +const PHOTOSHOP_WAVE: &str = "000000050057006100760065000000000000576176650000000a0000000057767470656e756d00000000577674700000000057765472000000004e6d62476c6f6e670000014c00000000574c4d6e6c6f6e67000000d500000000574c4d786c6f6e67000000d600000000416d4d6e6c6f6e67000000f000000000416d4d786c6f6e67000000f10000000053636c486c6f6e67000000640000000053636c566c6f6e670000001c00000000556e6441656e756d00000000556e6441000000005772704100000000526e64536c6f6e670013624a"; + +/// A `filterFX` list item as Photoshop writes it (enabled, Normal at 100%, with options), wrapping the `Fltr` descriptor `fltr`. +fn photoshop_fltr_item(fltr: Descriptor) -> Descriptor { + let id = code(fltr.class_id.as_bytes().try_into().unwrap()); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("from Photoshop"))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(id)) +} + +fn wind(method: &str, direction: &str) -> Descriptor { + Descriptor::new("Wnd ").with("WndM", enumv("WndM", method)).with("Drct", enumv("Drct", direction)) +} + +fn extrude(t: &str, size: Value, depth: Value, mode: &str, solid: Value, mask: Value) -> Descriptor { + Descriptor::new("Extr") + .with("ExtT", enumv("ExtT", t)) + .with("ExtS", size) + .with("ExtD", depth) + .with("ExtR", enumv("ExtR", mode)) + .with("ExtF", solid) + .with("ExtM", mask) +} + +fn tiles(count: Value, offset: Value, fill: &str) -> Descriptor { + Descriptor::new("Tls ").with("TlNm", count).with("TlOf", offset).with("FlCl", enumv("FlCl", fill)) +} + +#[test] +fn wind_extrude_tiles_open_from_photoshop_shapes() { + let (long, yes, no) = (Value::Integer, Value::Boolean(true), Value::Boolean(false)); + let cases = [ + // The measured files. + (wind("Blst", "Left"), "filter.stylize.wind", json!({"method": "blast", "direction": "fromRight"})), + (wind("Stgr", "Rght"), "filter.stylize.wind", json!({"method": "stagger", "direction": "fromLeft"})), + (wind("Wnd ", "Rght"), "filter.stylize.wind", json!({"method": "wind", "direction": "fromLeft"})), + ( + extrude("Blks", long(7), long(9), "Rndm", yes.clone(), no.clone()), + "filter.stylize.extrude", + json!({"type": "blocks", "size": 7, "depth": 9, "depthMode": "random", "solidFrontFaces": true, "maskIncompleteBlocks": false}), + ), + ( + extrude("Pyrm", long(12), long(20), "LvlB", no, yes), + "filter.stylize.extrude", + json!({"type": "pyramids", "size": 12, "depth": 20, "depthMode": "levelBased", "solidFrontFaces": false, "maskIncompleteBlocks": true}), + ), + ( + tiles(long(6), long(20), "FlFr"), + "filter.stylize.tiles", + json!({"count": 6, "maxOffset": 20, "fill": "foreground", "foreground": [0.2, 0.2, 0.2, 1.0], "background": [1.0, 1.0, 1.0, 1.0]}), + ), + ( + tiles(long(4), long(5), "FlIn"), + "filter.stylize.tiles", + json!({"count": 4, "maxOffset": 5, "fill": "inverse", "foreground": [0.2, 0.2, 0.2, 1.0], "background": [1.0, 1.0, 1.0, 1.0]}), + ), + ( + tiles(long(10), long(10), "FlBc"), + "filter.stylize.tiles", + json!({"count": 10, "maxOffset": 10, "fill": "background", "foreground": [0.2, 0.2, 0.2, 1.0], "background": [1.0, 1.0, 1.0, 1.0]}), + ), + ( + tiles(long(99), long(1), "FlSm"), + "filter.stylize.tiles", + json!({"count": 99, "maxOffset": 1, "fill": "unaltered", "foreground": [0.2, 0.2, 0.2, 1.0], "background": [1.0, 1.0, 1.0, 1.0]}), + ), + ]; + for (fltr, command, params) in cases { + let item = measured_item(fltr, 51.0, 255.0); + // Through bytes, as read from a file. + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!((f.command.as_str(), &f.params), (command, ¶ms)); + // Written back in the measured shape: same class, keys and their order, value types + // (long, bool, enum with its type), filterID; Tiles also its colours. + let back = item_for_filter(&f).unwrap(); + assert_eq!(get_desc(&back, "Fltr").map(Descriptor::to_bytes), get_desc(&item, "Fltr").map(Descriptor::to_bytes), "{command}"); + assert_eq!(back.get("filterID"), item.get("filterID"), "{command}"); + if command == "filter.stylize.tiles" { + assert_eq!((back.get("FrgC"), back.get("BckC")), (item.get("FrgC"), item.get("BckC"))); + } + } + // The class codes: numeric filterIDs of the four-char classes. + for (c, id) in [("filter.stylize.wind", 1466852384), ("filter.stylize.extrude", 1165522034), ("filter.stylize.tiles", 1416393504)] { + assert_eq!(item_for_filter(&sf(c, json!({}))).unwrap().get("filterID"), Some(&Value::Integer(id))); + } + // A filter found by its filterID alone (no `Fltr` class to go by) still needs its settings. + let bare = Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)).with("filterID", Value::Integer(1466852384)); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); + // Only Tiles reads the entry's colours; Wind and Extrude write the defaults. + let f = filter_from_item(&measured_item(wind("Wnd ", "Left"), 51.0, 0.0)); + assert_eq!(f.params, json!({"method": "wind", "direction": "fromRight"})); + let back = item_for_filter(&f).unwrap(); + assert_eq!((back.get("FrgC"), back.get("BckC")), (Some(&rgbc(0.0)), Some(&rgbc(255.0)))); + // Tiles without colours in its params (or the entry) writes the engine's black and white. + let f = filter_from_item(&Descriptor::new("filterFX").with("Fltr", Value::Descriptor(tiles(long(4), long(5), "FlBc")))); + assert_eq!(f.params, json!({"count": 4, "maxOffset": 5, "fill": "background"})); + let back = item_for_filter(&f).unwrap(); + assert_eq!((back.get("FrgC"), back.get("BckC")), (Some(&rgbc(0.0)), Some(&rgbc(255.0)))); +} + +#[test] +fn wind_extrude_tiles_write_defaults_and_photoshop_ranges() { + let fltr = |c: &str, p: J| match item_for_filter(&sf(c, p)).unwrap().get("Fltr") { + Some(Value::Descriptor(d)) => d.clone(), + other => panic!("no Fltr: {other:?}"), + }; + let long = Value::Integer; + let (yes, no) = (Value::Boolean(true), Value::Boolean(false)); + // Defaults are the engine's; our random seed has no PSD key and is not written. + assert_eq!(fltr("filter.stylize.wind", json!({"seed": 9})), wind("Wnd ", "Left")); + assert_eq!(fltr("filter.stylize.extrude", json!({"seed": 9})), extrude("Blks", long(30), long(30), "Rndm", no.clone(), no.clone())); + assert_eq!(fltr("filter.stylize.tiles", json!({"seed": 9})), tiles(long(10), long(10), "FlBc")); + // Out of range, fractional, non-numeric and non-bool values as the engine reads them. + assert_eq!( + fltr("filter.stylize.extrude", json!({"size": 1e12, "depth": -5, "solidFrontFaces": "yes", "maskIncompleteBlocks": true})), + extrude("Blks", long(255), long(1), "Rndm", no.clone(), yes) + ); + assert_eq!(fltr("filter.stylize.tiles", json!({"count": 0, "maxOffset": 12.6})), tiles(long(1), long(13), "FlBc")); + assert_eq!(fltr("filter.stylize.tiles", json!({"count": "many", "maxOffset": 1000})), tiles(long(10), long(99), "FlBc")); + assert_eq!(fltr("filter.stylize.extrude", json!({"size": f64::NAN})), extrude("Blks", long(30), long(30), "Rndm", no.clone(), no)); +} + +#[test] +fn malformed_wind_extrude_tiles_are_kept_verbatim() { + let long = Value::Integer; + let (yes, no) = (Value::Boolean(true), Value::Boolean(false)); + let cases = [ + // Missing keys. + Descriptor::new("Wnd "), + Descriptor::new("Wnd ").with("WndM", enumv("WndM", "Blst")), + Descriptor::new("Wnd ").with("Drct", enumv("Drct", "Left")), + Descriptor::new("Extr").with("ExtT", enumv("ExtT", "Blks")).with("ExtS", long(7)).with("ExtD", long(9)).with("ExtR", enumv("ExtR", "Rndm")), + Descriptor::new("Tls ").with("TlNm", long(6)).with("TlOf", long(20)), + Descriptor::new("Tls ").with("TlNm", long(6)).with("FlCl", enumv("FlCl", "FlFr")), + // Unknown enum values and enum types (Photoshop falls back to a default for these; + // we keep them as they are rather than guess). + wind("Wild", "Left"), + wind("Blst", "Up "), + Descriptor::new("Wnd ").with("WndM", enumv("Drct", "Blst")).with("Drct", enumv("Drct", "Left")), + extrude("Cube", long(7), long(9), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(7), long(9), "Sine", yes.clone(), no.clone()), + tiles(long(6), long(20), "FlXx"), + Descriptor::new("Tls ").with("TlNm", long(6)).with("TlOf", long(20)).with("FlCl", enumv("BlnM", "FlFr")), + // Wrong types (also NaN). + Descriptor::new("Wnd ").with("WndM", long(1)).with("Drct", enumv("Drct", "Left")), + extrude("Blks", Value::Double(7.0), long(9), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(7), unit(b"#Pxl", 9.0), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(7), long(9), "Rndm", long(1), no.clone()), + extrude("Blks", long(7), long(9), "Rndm", yes.clone(), Value::Double(f64::NAN)), + tiles(Value::Double(f64::NAN), long(20), "FlFr"), + tiles(long(6), Value::Text(UnicodeString::new_nul("20")), "FlFr"), + // Out of range. + extrude("Blks", long(1), long(9), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(256), long(9), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(7), long(0), "Rndm", yes.clone(), no.clone()), + extrude("Blks", long(7), long(i32::MAX), "Rndm", yes, no), + tiles(long(0), long(20), "FlFr"), + tiles(long(100), long(20), "FlFr"), + tiles(long(6), long(0), "FlFr"), + tiles(long(6), long(i32::MIN), "FlFr"), + ]; + for fltr in cases { + let item = measured_item(fltr, 0.0, 255.0); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}"); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes()); + } + // A Tiles entry whose colour is unreadable is kept verbatim too. + for (key, bad) in [ + ("FrgC", Value::Integer(3)), + ("BckC", Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(0.0)))), + ( + "BckC", + Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(f64::NAN)).with("Grn ", Value::Double(0.0)).with("Bl ", Value::Double(0.0))), + ), + ] { + let mut item = measured_item(tiles(long(6), long(20), "FlFr"), 0.0, 255.0); + set(&mut item, key, bad); + assert_eq!(filter_from_item(&item).command, UNSUPPORTED_FILTER); + } + // Values with no PSD equivalent are reported, not guessed. + for (c, p) in [ + ("filter.stylize.wind", json!({"method": "gale"})), + ("filter.stylize.wind", json!({"direction": "fromTop"})), + ("filter.stylize.wind", json!({"direction": 1})), + ("filter.stylize.extrude", json!({"type": "cubes"})), + ("filter.stylize.extrude", json!({"depthMode": null})), + ("filter.stylize.tiles", json!({"fill": "transparent"})), + ("filter.stylize.tiles", json!({"foreground": "teal"})), + ("filter.stylize.tiles", json!({"background": [1.0]})), + ("filter.stylize.tiles", json!({"background": [f64::NAN, 0.0, 0.0]})), + ] { + assert!(item_for_filter(&sf(c, p.clone())).is_err(), "{c} {p}"); + } +} + +/// Photoshop's default Custom kernel: a 3 × 3 sharpen centred in the 5 × 5. +const SHARPEN: [i32; 25] = [0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 5, -1, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0]; + +/// A `filterFXList` item as measured in Photoshop (applied through Action Manager to a smart +/// object, saved, `filterFX` read back). +fn measured_named_item(id: &[u8; 4], name: &str, fltr: Descriptor) -> Descriptor { + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(name))) + .with("blendOptions", blend_options(&sf("", J::Null))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(id))) +} + +fn offset_fltr(h: Value, v: Value, fill: Value) -> Descriptor { + Descriptor::new("Ofst").with("Hrzn", h).with("Vrtc", v).with("Fl ", fill) +} + +fn custom_fltr(scale: Value, offset: Value, m: Vec) -> Descriptor { + Descriptor::new("Cstm").with("Scl ", scale).with("Ofst", offset).with("Mtrx", Value::List(m)) +} + +fn longs(m: &[i32]) -> Vec { + m.iter().map(|v| Value::Integer(*v)).collect() +} + +#[test] +fn offset_round_trips_every_fill_mode() { + for (fill, ours) in [("Wrp ", "wrap"), ("Rpt ", "repeat"), ("Bckg", "transparent")] { + for (h, v) in [(0, 0), (12, -7), (5, 3), (i32::MIN, i32::MAX)] { + let f = sf("filter.other.offset", json!({"horizontal": h, "vertical": v, "undefinedAreas": ours})); + let d = item_for_filter(&f).unwrap(); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert_eq!(*fltr, offset_fltr(Value::Integer(h), Value::Integer(v), enumv("FlMd", fill))); + assert_eq!(int(&d, "filterID"), Some(1_332_114_292)); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); + } + } +} + +/// The measured descriptors (Wrap 0/0, Set to Background 12/−7, Repeat 5/3) open as Offset. +#[test] +fn offset_measured_descriptors_open_as_offset() { + for (h, v, fill, ours) in [(0, 0, "Wrp ", "wrap"), (12, -7, "Bckg", "transparent"), (5, 3, "Rpt ", "repeat")] { + let item = measured_named_item(b"Ofst", "Offset...", offset_fltr(Value::Integer(h), Value::Integer(v), enumv("FlMd", fill))); + let f = filter_from_item(&item); + assert_eq!(f.command, "filter.other.offset"); + assert_eq!(f.params, json!({"horizontal": h, "vertical": v, "undefinedAreas": ours})); + // Written back exactly as measured. + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +#[test] +fn offset_written_with_engine_defaults() { + // No params: 0, 0, Wrap Around (the engine's defaults); fractional pixels round. + let d = item_for_filter(&sf("filter.other.offset", json!({}))).unwrap(); + assert_eq!(*get_desc(&d, "Fltr").unwrap(), offset_fltr(Value::Integer(0), Value::Integer(0), enumv("FlMd", "Wrp "))); + let d = item_for_filter(&sf("filter.other.offset", json!({"horizontal": 2.6, "vertical": 3e12, "undefinedAreas": "bogus"}))).unwrap(); + assert_eq!(*get_desc(&d, "Fltr").unwrap(), offset_fltr(Value::Integer(3), Value::Integer(i32::MAX), enumv("FlMd", "Wrp "))); +} + +#[test] +fn malformed_offset_is_kept_verbatim() { + let i = Value::Integer; + for fltr in [ + offset_fltr(i(1), i(2), enumv("FlMd", "Xxxx")), + offset_fltr(i(1), i(2), Value::Integer(1)), + offset_fltr(Value::Double(1.5), i(2), enumv("FlMd", "Wrp ")), + offset_fltr(i(1), unit(b"#Pxl", f64::NAN), enumv("FlMd", "Wrp ")), + Descriptor::new("Ofst").with("Hrzn", i(1)).with("Fl ", enumv("FlMd", "Wrp ")), + Descriptor::new("Ofst"), + // A mistyped enum, a setting we don't write back and a repeated one. + offset_fltr(i(1), i(2), enumv("Xxxx", "Wrp ")), + offset_fltr(i(1), i(2), enumv("FlMd", "Wrp ")).with("Xtra", i(1)), + offset_fltr(i(1), i(2), enumv("FlMd", "Wrp ")).with("Hrzn", i(3)), + ] { + let item = measured_named_item(b"Ofst", "Offset...", fltr); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}"); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "kept byte for byte"); + } + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"Ofst"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); +} + +#[test] +fn custom_round_trips() { + let edge: Vec = (0..25) + .map(|i| { + if i == 12 { + 8 + } else if (6..=18).contains(&i) && i % 5 != 0 && i % 5 != 4 { + -1 + } else { + 0 + } + }) + .collect(); + let full: Vec = (0..25).map(|i| i * 83 - 999).collect(); + for (kernel, scale, offset) in [(SHARPEN.to_vec(), 1, 0), (edge, 2, 10), (full, 9999, -9999), (vec![999; 25], 1, 9999)] { + let f = sf("filter.other.custom", json!({"kernel": kernel, "scale": scale, "offset": offset})); + let d = item_for_filter(&f).unwrap(); + assert_eq!(*get_desc(&d, "Fltr").unwrap(), custom_fltr(Value::Integer(scale), Value::Integer(offset), longs(&kernel))); + assert_eq!(int(&d, "filterID"), Some(1_131_639_917)); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); + } +} + +/// The measured descriptors (the default sharpen; a blur at Scale 2, Offset 10) open as Custom. +#[test] +fn custom_measured_descriptors_open_as_custom() { + let blur = [0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 4, 2, 0, 0, 1, 2, 1, 0, 0, 0, 0, 0, 0]; + for (kernel, scale, offset) in [(SHARPEN, 1, 0), (blur, 2, 10)] { + let item = measured_named_item(b"Cstm", "Custom...", custom_fltr(Value::Integer(scale), Value::Integer(offset), longs(&kernel))); + let f = filter_from_item(&item); + assert_eq!(f.command, "filter.other.custom"); + assert_eq!(f.params, json!({"kernel": kernel, "scale": scale, "offset": offset})); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +#[test] +fn custom_written_like_the_engine_reads_it() { + let fltr = |p: J| get_desc(&item_for_filter(&sf("filter.other.custom", p)).unwrap(), "Fltr").cloned().unwrap(); + // No kernel: the engine's identity; short kernels pad with 0; out-of-range values clamp. + let mut id = [0; 25]; + id[12] = 1; + assert_eq!(fltr(json!({})), custom_fltr(Value::Integer(1), Value::Integer(0), longs(&id))); + let mut short = [0; 25]; + short[0] = 3; + short[1] = -999; + assert_eq!(fltr(json!({"kernel": [3, -5000], "scale": 0, "offset": 20000})), custom_fltr(Value::Integer(1), Value::Integer(9999), longs(&short))); + // Fractions have no PSD equivalent. + for p in [json!({"kernel": [0.5]}), json!({"scale": 1.5}), json!({"offset": -0.25})] { + assert!(item_for_filter(&sf("filter.other.custom", p.clone())).is_err(), "{p}"); + } +} + +#[test] +fn malformed_custom_is_kept_verbatim() { + let i = Value::Integer; + let mut wrong_type = longs(&SHARPEN); + wrong_type[3] = Value::Double(1.0); + for fltr in [ + custom_fltr(i(1), i(0), longs(&SHARPEN[..24])), + custom_fltr(i(1), i(0), longs(&[0; 26])), + custom_fltr(i(1), i(0), wrong_type), + custom_fltr(i(1), i(0), longs(&[1000; 25])), + custom_fltr(i(0), i(0), longs(&SHARPEN)), + custom_fltr(i(10_000), i(0), longs(&SHARPEN)), + custom_fltr(i(1), i(-10_000), longs(&SHARPEN)), + custom_fltr(Value::Double(f64::NAN), i(0), longs(&SHARPEN)), + custom_fltr(i(1), unit(b"#Pxl", 1.0), longs(&SHARPEN)), + Descriptor::new("Cstm").with("Scl ", i(1)).with("Ofst", i(0)).with("Mtrx", i(5)), + Descriptor::new("Cstm").with("Scl ", i(1)).with("Mtrx", Value::List(longs(&SHARPEN))), + Descriptor::new("Cstm"), + // A setting we don't write back. + custom_fltr(i(1), i(0), longs(&SHARPEN)).with("Xtra", i(1)), + ] { + let item = measured_named_item(b"Cstm", "Custom...", fltr); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}"); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "kept byte for byte"); + } +} + +/// A `filterFX` item holding `fltr`, as Photoshop writes a filter with options. +fn pixelate_item(name: &str, fltr: Descriptor, filter_id: i32) -> Descriptor { + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(name))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "Nrml")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(filter_id)) +} + +#[test] +fn mezzotint_round_trips_every_type_and_reads_photoshops_shape() { + for (code, ty) in MEZZOTINT_TYPES { + let f = sf("filter.pixelate.mezzotint", json!({"type": ty})); + let d = item_for_filter(&f).unwrap(); + assert_eq!(int(&d, "filterID"), Some(1_299_870_830)); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert!(fltr.class_id.is("Mztn")); + assert_eq!(enum_of(fltr, "MztT"), Some(&code[..])); + assert!(matches!(fltr.get("MztT"), Some(Value::Enumerated { type_id, .. }) if type_id.is("MztT"))); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f, "{ty}"); + } + // Measured in Photoshop: `{'_class': 'Mztn', 'MztT': 'MztT.CrsD'}` and `LngS`, filterID 1299870830. + for (code, ty) in [("CrsD", "coarseDots"), ("LngS", "longStrokes")] { + let item = pixelate_item("Mezzotint...", Descriptor::new("Mztn").with("MztT", enumv("MztT", code)), 1_299_870_830); + let f = filter_from_item(&item); + assert_eq!((f.command.as_str(), &f.params), ("filter.pixelate.mezzotint", &json!({"type": ty}))); + } + // No type or an unknown type is Fine Dots, like the engine; the seed + // (PhotoCraft only) is not saved. + for p in [json!({}), json!({"type": "sparkles"}), json!({"type": "fineDots", "seed": 77})] { + let d = item_for_filter(&sf("filter.pixelate.mezzotint", p)).unwrap(); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert_eq!(enum_of(fltr, "MztT"), Some(&b"FnDt"[..])); + assert_eq!(fltr.items.len(), 1); + } +} + +#[test] +fn color_halftone_round_trips_and_reads_photoshops_shape() { + for p in [ + json!({"maxRadius": 8, "channel1": 108, "channel2": 162, "channel3": 90, "channel4": 45}), + json!({"maxRadius": 4, "channel1": -360, "channel2": 0, "channel3": 360, "channel4": -45}), + json!({"maxRadius": 127, "channel1": 1, "channel2": 2, "channel3": 3, "channel4": 4}), + ] { + let f = sf("filter.pixelate.colorHalftone", p); + let d = item_for_filter(&f).unwrap(); + assert_eq!(int(&d, "filterID"), Some(1_131_180_616)); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert!(fltr.class_id.is("ClrH")); + for k in ["Rds ", "Ang1", "Ang2", "Ang3", "Ang4"] { + assert!(matches!(fltr.get(k), Some(Value::Integer(_))), "{k} is a long"); + } + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); + } + // Measured in Photoshop: `{'_class': 'ClrH', 'Rds ': 6, 'Ang1': 10, 'Ang2': 20, 'Ang3': 30, 'Ang4': 40}`. + let measured = Descriptor::new("ClrH") + .with("Rds ", Value::Integer(6)) + .with("Ang1", Value::Integer(10)) + .with("Ang2", Value::Integer(20)) + .with("Ang3", Value::Integer(30)) + .with("Ang4", Value::Integer(40)); + let item = pixelate_item("Color Halftone...", measured.clone(), 1_131_180_616); + let f = filter_from_item(&item); + assert_eq!(f.command, "filter.pixelate.colorHalftone"); + assert_eq!(f.params, json!({"maxRadius": 6, "channel1": 10, "channel2": 20, "channel3": 30, "channel4": 40})); + assert_eq!(get_desc(&item_for_filter(&f).unwrap(), "Fltr"), Some(&measured)); + // Defaults and hostile values write longs within the dialog's ranges. + let d = + item_for_filter(&sf("filter.pixelate.colorHalftone", json!({"maxRadius": f64::NAN, "channel1": 1e12, "channel2": -1e12, "channel3": "x"}))).unwrap(); + let fltr = get_desc(&d, "Fltr").unwrap(); + let got: Vec<_> = ["Rds ", "Ang1", "Ang2", "Ang3", "Ang4"].iter().map(|k| int(fltr, k)).collect(); + assert_eq!(got, [Some(8), Some(360), Some(-360), Some(90), Some(45)]); +} + +#[test] +fn malformed_pixelate_filters_stay_verbatim() { + let mz = |v: Value| Descriptor::new("Mztn").with("MztT", v); + let ch = |rds: Value, ang1: Value| { + Descriptor::new("ClrH") + .with("Rds ", rds) + .with("Ang1", ang1) + .with("Ang2", Value::Integer(20)) + .with("Ang3", Value::Integer(30)) + .with("Ang4", Value::Integer(40)) + }; + let cases = [ + pixelate_item("Mezzotint...", Descriptor::new("Mztn"), code(b"Mztn")), + pixelate_item("Mezzotint...", mz(enumv("MztT", "Sprk")), code(b"Mztn")), + pixelate_item("Mezzotint...", mz(Value::Integer(1)), code(b"Mztn")), + pixelate_item("Color Halftone...", Descriptor::new("ClrH"), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Double(6.0), Value::Integer(10)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(unit(b"#Pxl", 6.0), Value::Integer(10)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(3), Value::Integer(10)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(128), Value::Integer(10)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(6), Value::Integer(361)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(6), Value::Integer(i32::MIN)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(6), Value::Double(f64::NAN)), code(b"ClrH")), + // A mistyped enum and settings we don't write back. + pixelate_item("Mezzotint...", mz(enumv("Xxxx", "FnDt")), code(b"Mztn")), + pixelate_item("Mezzotint...", mz(enumv("MztT", "FnDt")).with("Xtra", Value::Integer(1)), code(b"Mztn")), + pixelate_item("Color Halftone...", ch(Value::Integer(6), Value::Integer(10)).with("Xtra", Value::Integer(1)), code(b"ClrH")), + pixelate_item("Color Halftone...", ch(Value::Integer(6), Value::Integer(10)).with("Ang1", Value::Integer(11)), code(b"ClrH")), + ]; + for item in cases { + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{:?}", get_desc(&item, "Fltr")); + // Written back with its own settings (blending is rewritten from the model). + let back = item_for_filter(&f).unwrap(); + // Compared as bytes: a NaN setting is not equal to itself. + let fltr = |d: &Descriptor| get_desc(d, "Fltr").map(Descriptor::to_bytes); + assert_eq!((fltr(&back), int(&back, "filterID")), (fltr(&item), int(&item, "filterID"))); + } + // Without a `Fltr` the filter has no settings to read: kept verbatim too. + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"ClrH"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); +} + +/// Distort smart filters from a Photoshop file open as their PhotoCraft filters, and the ones +/// PhotoCraft writes carry the same settings, keys and order as Photoshop's. +#[test] +fn photoshop_distort_filters_read_and_write_like_photoshop() { + for (command, params, hex) in PHOTOSHOP_FLTR { + let ps = Descriptor::from_bytes(&from_hex(hex).unwrap()).unwrap(); + let f = filter_from_item(&photoshop_fltr_item(ps.clone())); + let want: J = serde_json::from_str(params).unwrap(); + assert_eq!((f.command.as_str(), &f.params), (command, &want), "{command}"); + let ours = match item_for_filter(&f).unwrap().get("Fltr") { + Some(Value::Descriptor(d)) => d.clone(), + other => panic!("{command}: no Fltr ({other:?})"), + }; + assert_eq!((&ours.class_id, &ours.items), (&ps.class_id, &ps.items), "{command}: same keys, values and order"); + } + // A scaled wave can't be modelled, so it stays Photoshop's, unchanged. + let wave = Descriptor::from_bytes(&from_hex(PHOTOSHOP_WAVE).unwrap()).unwrap(); + let item = photoshop_fltr_item(wave); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), item.get("Fltr")); + // Unscaled, but with 332 generators (the renderer draws 64 at most), it stays Photoshop's too. + let mut unscaled = match item.get("Fltr") { + Some(Value::Descriptor(d)) => d.clone(), + other => panic!("wave: no Fltr ({other:?})"), + }; + set(&mut unscaled, "SclV", Value::Integer(100)); + let item = photoshop_fltr_item(unscaled); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), item.get("Fltr")); + + // An extra or repeated setting PhotoCraft doesn't model keeps the filter Photoshop's, unchanged. + let mut fltrs: Vec = PHOTOSHOP_FLTR.iter().map(|(_, _, hex)| Descriptor::from_bytes(&from_hex(hex).unwrap()).unwrap()).collect(); + let mut wave = unscaled_wave(); + set(&mut wave, "NmbG", Value::Integer(5)); + assert_eq!(filter_from_item(&photoshop_fltr_item(wave.clone())).command, "filter.distort.wave", "the plain wave maps"); + fltrs.push(wave); + for ps in fltrs { + let name = String::from_utf8_lossy(ps.class_id.as_bytes()).into_owned(); + let mut repeated = ps.clone(); + repeated.items.push(ps.items[0].clone()); + for odd in [ps.clone().with("Xtra", Value::Integer(7)), repeated] { + let item = photoshop_fltr_item(odd); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{name}"); + assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), item.get("Fltr"), "{name}: written back unchanged"); + } + } +} + +/// Photoshop's Wave ([`PHOTOSHOP_WAVE`]) at 100% scale both ways. +fn unscaled_wave() -> Descriptor { + let mut wave = Descriptor::from_bytes(&from_hex(PHOTOSHOP_WAVE).unwrap()).unwrap(); + set(&mut wave, "SclV", Value::Integer(100)); + wave +} + +/// An unscaled Wave opens as PhotoCraft's Wave (Triangle, the other settings as stored) up to +/// the generators the renderer models; one more and it stays Photoshop's, unchanged. +#[test] +fn wave_maps_up_to_the_generators_the_renderer_models() { + let max = i32::try_from(photocraft_algo::MAX_WAVE_GENERATORS).unwrap(); + assert_eq!(max, 64); + let mut wave = Descriptor::from_bytes(&from_hex(PHOTOSHOP_WAVE).unwrap()).unwrap(); + set(&mut wave, "SclV", Value::Integer(100)); + for (generators, mapped) in [(1, true), (max, true), (max + 1, false), (999, false), (0, false), (-1, false)] { + set(&mut wave, "NmbG", Value::Integer(generators)); + let item = photoshop_fltr_item(wave.clone()); + let f = filter_from_item(&item); + if mapped { + assert_eq!( + (f.command.as_str(), &f.params), + ( + "filter.distort.wave", + &json!({"type": "triangle", "generators": generators, "wavelengthMin": 213, "wavelengthMax": 214, "amplitudeMin": 240, + "amplitudeMax": 241, "undefinedAreas": "wrap", "seed": 1270346}) + ), + "{generators} generators" + ); + match item_for_filter(&f).unwrap().get("Fltr") { + Some(Value::Descriptor(ours)) => assert_eq!((&ours.class_id, &ours.items), (&wave.class_id, &wave.items), "{generators} generators"), + other => panic!("wave: no Fltr ({other:?})"), + } + } else { + assert_eq!(f.command, UNSUPPORTED_FILTER, "{generators} generators"); + assert_eq!(item_for_filter(&f).unwrap().get("Fltr"), item.get("Fltr"), "{generators} generators"); + } + } +} + +/// PhotoCraft draws at most `MAX_WAVE_GENERATORS` generators, so a Wave edited past that exports +/// the 64 it rendered, and a count below 1 exports 1, instead of Photoshop drawing more. +#[test] +fn wave_export_writes_only_the_generators_the_renderer_draws() { + let max = i32::try_from(photocraft_algo::MAX_WAVE_GENERATORS).unwrap(); + for (stored, written) in [(100, max), (999, max), (max + 1, max), (max, max), (5, 5), (1, 1), (0, 1), (-3, 1)] { + let wave = sf( + "filter.distort.wave", + json!({"type": "sine", "generators": stored, "wavelengthMin": 20, "wavelengthMax": 90, "amplitudeMin": 4, "amplitudeMax": 30}), + ); + let fltr = match item_for_filter(&wave).unwrap().get("Fltr") { + Some(Value::Descriptor(d)) => d.clone(), + other => panic!("wave: no Fltr ({other:?})"), + }; + assert_eq!(int(&fltr, "NmbG"), Some(written), "{stored} generators"); + } +} + +/// `RndS` is the seed the renderer draws with, also for a legacy signed or fractional seed +/// (stored as is by `layer.smartFilter.setParams`), not 0. +#[test] +fn wave_export_writes_the_seed_the_renderer_draws() { + for (stored, written) in [ + (json!(77), 77), + (json!(4_000_000_000u32), -294_967_296), + (json!(-1), -1), + (json!(7.4), 7), + (json!(-2.6), -3), + (json!(1e12), i32::MAX), + (json!("x"), 0), + ] { + let wave = sf("filter.distort.wave", json!({"generators": 3, "seed": stored})); + let fltr = match item_for_filter(&wave).unwrap().get("Fltr") { + Some(Value::Descriptor(d)) => d.clone(), + other => panic!("wave: no Fltr ({other:?})"), + }; + assert_eq!(int(&fltr, "RndS"), Some(written), "seed {stored}"); + // And it reads back as that bit pattern. + assert_eq!(known_params("Wave", Some(&fltr), &Descriptor::new("filterFX")).unwrap().1["seed"], json!(written as u32), "seed {stored}"); + } +} + +/// Invert on a smart object: Photoshop stores no `Fltr`, `hasoptions` false and its filter id. +#[test] +fn invert_smart_filter_reads_and_writes_like_photoshop() { + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Invert"))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(false)) + .with("FrgC", rgbc(255.0)) + .with("BckC", rgbc(0.0)) + .with("filterID", Value::Integer(code(b"Invr"))); + let f = filter_from_item(&item); + assert_eq!((f.command.as_str(), &f.params), ("image.adjustments.invert", &json!({}))); + let back = item_for_filter(&f).unwrap(); + assert_eq!(back.get("Fltr"), None); + assert_eq!(back.get("hasoptions"), Some(&Value::Boolean(false))); + assert_eq!(back.get("filterID"), Some(&Value::Integer(code(b"Invr")))); + assert_eq!(back.get("Nm "), Some(&Value::Text(UnicodeString::new_nul("Invert")))); +} + +/// A filter PhotoCraft doesn't model, or a known one with settings it doesn't, is kept verbatim; only its visibility and blend change. +#[test] +fn photoshop_sharpen_without_fltr_imports_as_sharpen() { + // #6267: Photoshop's Sharpen item holds only its name, blending and `filterID`. + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Sharpen"))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(false)) + .with("filterID", Value::Integer(1_399_353_968)); + let f = filter_from_item(&item); + assert_eq!((f.command.as_str(), &f.params), ("filter.sharpen.sharpen", &json!({}))); + // Hue/Saturation with an unnumbered range and Dust & Scratches with a missing setting fail + // cleanly instead of panicking. + let bad = SmartFilter { + command: "image.adjustments.hueSaturation".into(), + params: json!({"reds": {"range": [1, "x", 3, 4]}}), + blend: BlendMode::Normal, + opacity: 1.0, + visible: true, + }; + assert!(item_for_filter(&bad).is_err()); + let dust = Descriptor::new("filterFX") + .with("Fltr", Value::Descriptor(Descriptor::new("DstS").with("Rds ", Value::Integer(3)))) + .with("filterID", Value::Integer(code(b"DstS"))); + assert_eq!(filter_from_item(&dust).command, UNSUPPORTED_FILTER); +} + +#[test] +fn hue_saturation_ranges_are_not_lost() { + let hue_sat = |params: J| item_for_filter(&sf("image.adjustments.hueSaturation", params)); + // A range that is not an object is an error, not a silently dropped setting. + assert!(hue_sat(json!({"reds": 5})).is_err()); + assert!(hue_sat(json!({"reds": null})).is_ok()); + // Colorize's 25% default is the master saturation's only: a range's stays 0. + let d = hue_sat(json!({"colorize": true, "reds": {"hue": 5}})).unwrap(); + let adjs = adjustment_list(get_desc(&d, "Fltr").unwrap()).unwrap(); + let saturations: Vec<_> = adjs.iter().map(|a| int(a, "Strt")).collect(); + assert_eq!(saturations, [Some(25), Some(0)]); + // A repeated range (or master) would overwrite the first: the filter is kept verbatim. + let entry = |range: Option| { + let e = Descriptor::new("Hst2").with("H ", Value::Integer(0)).with("Strt", Value::Integer(0)).with("Lght", Value::Integer(0)); + let Some(r) = range else { return Value::Descriptor(e) }; + let e = ["BgnR", "BgnS", "EndS", "EndR"].iter().fold(e.with("LclR", Value::Integer(r)), |e, k| e.with(k, Value::Integer(10))); + Value::Descriptor(e) + }; + for adjs in [vec![entry(None), entry(Some(1)), entry(Some(1))], vec![entry(None), entry(None)]] { + let fltr = Descriptor::new("HStr").with("Clrz", Value::Boolean(false)).with("Adjs", Value::List(adjs)); + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Hue/Saturation..."))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(b"HStr"))); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap(), item); + } +} + +#[test] +fn unknown_filters_keep_their_descriptor() { + let item = Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Shear..."))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("Fltr", Value::Descriptor(Descriptor::new("Shr ").with("UndA", enumv("UndA", "WrpA")))) + .with("filterID", Value::Integer(code(b"Shr "))); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(f.params["name"], "Shear..."); + assert_eq!(item_for_filter(&f).unwrap(), item); + // Hidden and re-blended: only those keys change. + let mut g = f.clone(); + g.visible = false; + g.blend = BlendMode::Screen; + let back = filter_from_item(&item_for_filter(&g).unwrap()); + assert_eq!((back.visible, back.blend, &back.params), (false, BlendMode::Screen, &f.params)); + // A known filter with a setting we don't model is kept verbatim too. + let odd = Descriptor::new("filterFX").with("Fltr", Value::Descriptor(Descriptor::new("GsnB"))).with("filterID", Value::Integer(code(b"GsnB"))); + assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); +} + +/// A `filterFXList` item with filter `fltr` and `filterID`, as Photoshop saves it. +fn ps_item(name: &str, fltr: Descriptor, filter_id: i32) -> Descriptor { + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul(name))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(filter_id)) +} + +fn surface_blur(rds: f64, thsh: i32) -> Descriptor { + Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", rds)).with("Thsh", Value::Integer(thsh)) +} + +fn smart_blur(rds: f64, thsh: f64, q: &str, m: &str) -> Descriptor { + Descriptor::new("SmrB").with("Rds ", Value::Double(rds)).with("Thsh", Value::Double(thsh)).with("SmBQ", enumv("SmBQ", q)).with("SmBM", enumv("SmBM", m)) +} + +fn radial_blur(amnt: i32, m: &str, q: &str) -> Descriptor { + Descriptor::new("RdlB").with("Amnt", Value::Integer(amnt)).with("BlrM", enumv("BlrM", m)).with("BlrQ", enumv("BlrQ", q)) +} + +/// The `Fltr` shapes measured in Photoshop (each filter applied to a smart object through +/// Action Manager, saved, and `filterFX` read back) open as our filters and are written back +/// with the same keys, value types, units, enums and filter ids. +#[test] +fn blur_family_matches_photoshop_shapes() { + let smart = code(b"SmrB"); + let radial = code(b"RdlB"); + assert_eq!((smart, radial), (1_399_681_602, 1_382_313_026)); + let cases = [ + (surface_blur(5.0, 15), 1135, "filter.blur.surfaceBlur", json!({"radius": 5.0, "threshold": 15})), + (surface_blur(12.0, 40), 1135, "filter.blur.surfaceBlur", json!({"radius": 12.0, "threshold": 40})), + (smart_blur(5.0, 25.0, "SBQH", "SBMN"), smart, "filter.blur.smartBlur", json!({"radius": 5.0, "threshold": 25.0, "quality": "high", "mode": "normal"})), + ( + smart_blur(3.0, 30.0, "SBQL", "SBME"), + smart, + "filter.blur.smartBlur", + json!({"radius": 3.0, "threshold": 30.0, "quality": "low", "mode": "edgeOnly"}), + ), + ( + smart_blur(3.0, 30.0, "SBQM", "SBMO"), + smart, + "filter.blur.smartBlur", + json!({"radius": 3.0, "threshold": 30.0, "quality": "medium", "mode": "overlayEdge"}), + ), + (radial_blur(10, "Spn ", "Drft"), radial, "filter.blur.radialBlur", json!({"amount": 10, "method": "spin", "quality": "draft"})), + (radial_blur(20, "Spn ", "Bst "), radial, "filter.blur.radialBlur", json!({"amount": 20, "method": "spin", "quality": "best"})), + (radial_blur(20, "Zm ", "Gd "), radial, "filter.blur.radialBlur", json!({"amount": 20, "method": "zoom", "quality": "good"})), + ]; + for (fltr, id, command, params) in cases { + let item = ps_item("x", fltr.clone(), id); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!((f.command.as_str(), &f.params), (command, ¶ms), "{fltr:?}"); + let out = item_for_filter(&f).unwrap(); + assert_eq!(out.get("filterID"), Some(&Value::Integer(id))); + assert_eq!(out.get("hasoptions"), Some(&Value::Boolean(true))); + // Byte-identical filter descriptor: class id, four-char keys, types, units and enums. + let Some(Value::Descriptor(w)) = out.get("Fltr") else { panic!("no Fltr for {command}") }; + assert_eq!(w.to_bytes(), fltr.to_bytes(), "{command}"); + } + // Surface Blur's class is a string id; Smart Blur's and Radial Blur's are four-char codes. + assert_eq!(surface_blur(1.0, 2).class_id, Id::Str(b"surfaceBlur".to_vec())); + assert_eq!(radial_blur(1, "Spn ", "Gd ").class_id, Id::Code(*b"RdlB")); + // Unset options are written with the dialog defaults. + let fltr = |c: &str, p: J| item_for_filter(&sf(c, p)).unwrap().get("Fltr").cloned(); + assert_eq!(fltr("filter.blur.surfaceBlur", json!({})), Some(Value::Descriptor(surface_blur(5.0, 15)))); + assert_eq!(fltr("filter.blur.smartBlur", json!({})), Some(Value::Descriptor(smart_blur(3.0, 25.0, "SBQH", "SBMN")))); + assert_eq!(fltr("filter.blur.radialBlur", json!({"centerX": 0.5, "centerY": 0.5})), Some(Value::Descriptor(radial_blur(10, "Spn ", "Gd ")))); +} + +/// `d` with `key` set to `v` (replacing the measured value). +fn replaced(mut d: Descriptor, key: &str, v: Value) -> Descriptor { + set(&mut d, key, v); + d +} + +/// Malformed or unmodelled blur descriptors stay verbatim (and are written back unchanged); +/// settings PSD can't hold are reported, never a panic. +#[test] +fn blur_family_malformed_is_kept_verbatim() { + let smart = code(b"SmrB"); + let radial = code(b"RdlB"); + let text = |s: &str| Value::Text(UnicodeString::new_nul(s)); + let bad = [ + // Missing keys. + (Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", 5.0)), 1135), + (Descriptor::new("SmrB").with("Rds ", Value::Double(3.0)).with("Thsh", Value::Double(25.0)), smart), + (Descriptor::new("RdlB").with("Amnt", Value::Integer(10)).with("BlrM", enumv("BlrM", "Spn ")), radial), + // Wrong types. + (Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Pxl", 5.0)).with("Thsh", Value::Double(15.0)), 1135), + ( + Descriptor::new("SmrB") + .with("Rds ", text("3")) + .with("Thsh", Value::Double(25.0)) + .with("SmBQ", enumv("SmBQ", "SBQH")) + .with("SmBM", enumv("SmBM", "SBMN")), + smart, + ), + (Descriptor::new("RdlB").with("Amnt", text("10")).with("BlrM", enumv("BlrM", "Spn ")).with("BlrQ", enumv("BlrQ", "Gd ")), radial), + (Descriptor::new("RdlB").with("Amnt", Value::Integer(10)).with("BlrM", Value::Integer(1)).with("BlrQ", enumv("BlrQ", "Gd ")), radial), + // Out of range, NaN and infinity. + (surface_blur(0.5, 15), 1135), + (surface_blur(5.0, 300), 1135), + (surface_blur(f64::NAN, 15), 1135), + (smart_blur(f64::INFINITY, 25.0, "SBQH", "SBMN"), smart), + (smart_blur(3.0, 0.0, "SBQH", "SBMN"), smart), + (radial_blur(0, "Spn ", "Gd "), radial), + (radial_blur(101, "Zm ", "Gd "), radial), + // Unknown enum values. + (smart_blur(3.0, 25.0, "SBQX", "SBMN"), smart), + (smart_blur(3.0, 25.0, "Hgh ", "SBMN"), smart), + (radial_blur(10, "Twrl", "Gd "), radial), + (radial_blur(10, "Spn ", "Hgh "), radial), + // A key we don't model (an off-centre Radial Blur would carry one). + (radial_blur(10, "Zm ", "Gd ").with("Cntr", Value::Descriptor(Descriptor::new("Pnt ").with("Hrzn", Value::Double(10.0)))), radial), + (surface_blur(5.0, 15).with("Edg ", Value::Integer(1)), 1135), + // Enums of another type, units and value types we would not write back. + (replaced(smart_blur(3.0, 25.0, "SBQH", "SBMN"), "SmBQ", enumv("Othr", "SBQH")), smart), + (replaced(smart_blur(3.0, 25.0, "SBQH", "SBMN"), "SmBM", enumv("SmBQ", "SBMN")), smart), + (replaced(radial_blur(10, "Spn ", "Gd "), "BlrM", enumv("Othr", "Spn ")), radial), + (replaced(radial_blur(10, "Spn ", "Gd "), "BlrQ", enumv("BlrM", "Gd ")), radial), + (Descriptor::new("surfaceBlur").with("Rds ", unit(b"#Prc", 5.0)).with("Thsh", Value::Integer(15)), 1135), + (Descriptor::new("surfaceBlur").with("Rds ", Value::Double(5.0)).with("Thsh", Value::Integer(15)), 1135), + (replaced(smart_blur(3.0, 25.0, "SBQH", "SBMN"), "Rds ", unit(b"#Pxl", 3.0)), smart), + (replaced(smart_blur(3.0, 25.0, "SBQH", "SBMN"), "Thsh", unit(b"#Prc", 25.0)), smart), + (replaced(smart_blur(3.0, 25.0, "SBQH", "SBMN"), "Thsh", Value::Integer(25)), smart), + ]; + for (fltr, id) in bad { + let item = ps_item("x", fltr.clone(), id); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{fltr:?}"); + // Compared as bytes: a NaN setting is not equal to itself. + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "{fltr:?}"); + } + // Radial Blur at its defaults has no `Fltr` (measured): its values are not stored, so the + // entry stays verbatim instead of guessing them. + let no_opts = Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)).with("filterID", Value::Integer(radial)); + assert_eq!(filter_from_item(&no_opts).command, UNSUPPORTED_FILTER); + // Settings with no PSD equivalent are errors when writing. + for (command, p) in [ + ("filter.blur.smartBlur", json!({"quality": "ultra"})), + ("filter.blur.smartBlur", json!({"mode": 3})), + ("filter.blur.radialBlur", json!({"method": "twirl"})), + ("filter.blur.radialBlur", json!({"quality": ["best"]})), + ("filter.blur.radialBlur", json!({"centerX": 0.25})), + ("filter.blur.radialBlur", json!({"centerY": 1.0})), + // A centre, or a value, that is there but can't be written as Photoshop would open it. + ("filter.blur.radialBlur", json!({"centerX": "middle"})), + ("filter.blur.radialBlur", json!({"centerY": true})), + ("filter.blur.radialBlur", json!({"amount": 0})), + ("filter.blur.radialBlur", json!({"amount": 101})), + ("filter.blur.radialBlur", json!({"amount": "10"})), + ("filter.blur.surfaceBlur", json!({"radius": 0.5})), + ("filter.blur.surfaceBlur", json!({"radius": 100.5})), + ("filter.blur.surfaceBlur", json!({"threshold": 1})), + ("filter.blur.surfaceBlur", json!({"threshold": 256})), + ("filter.blur.surfaceBlur", json!({"radius": "5"})), + ("filter.blur.smartBlur", json!({"radius": 0.05})), + ("filter.blur.smartBlur", json!({"threshold": 100.5})), + ("filter.blur.smartBlur", json!({"threshold": [25]})), + ] { + assert!(item_for_filter(&sf(command, p.clone())).is_err(), "{command} {p}"); + } + // Every value that is written opens again as the same editable filter (range ends included). + for (command, p) in [ + ("filter.blur.surfaceBlur", json!({"radius": 1.0, "threshold": 2})), + ("filter.blur.surfaceBlur", json!({"radius": 100.0, "threshold": 255})), + ("filter.blur.smartBlur", json!({"radius": 0.1, "threshold": 100.0, "quality": "low", "mode": "edgeOnly"})), + ("filter.blur.radialBlur", json!({"amount": 1, "method": "zoom", "quality": "best", "centerX": 0.5, "centerY": null})), + ("filter.blur.radialBlur", json!({"amount": 100, "method": "spin", "quality": "draft"})), + ] { + let back = filter_from_item(&item_for_filter(&sf(command, p.clone())).unwrap()); + assert_eq!(back.command, command, "{p}"); + } + // Hostile numbers write without panicking. + for p in [json!({"radius": f64::MAX, "threshold": -1e300, "amount": 1e300}), json!({"radius": "x", "threshold": null}), json!(null), json!([1, 2])] { + for command in ["filter.blur.surfaceBlur", "filter.blur.smartBlur", "filter.blur.radialBlur"] { + let _ = item_for_filter(&sf(command, p.clone())); + } + } +} + +/// A `filterFX` entry shaped as Photoshop saves it (measured in Photoshop: the filter applied +/// through Action Manager to a smart object, the PSD saved, `filterFX` read back). +fn photoshop_item(filter_id: &[u8; 4], fltr: Descriptor) -> Descriptor { + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filter..."))) + .with("blendOptions", blend_options(&sf("", J::Null))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgbc(0.0)) + .with("BckC", rgbc(255.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(filter_id))) +} + +#[test] +fn stylize_and_pixelate_filters_open_from_photoshop_shapes() { + let cases = [ + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Nrml")), "filter.stylize.diffuse", json!({"mode": "normal"})), + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "DrkO")), "filter.stylize.diffuse", json!({"mode": "darkenOnly"})), + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "LghO")), "filter.stylize.diffuse", json!({"mode": "lightenOnly"})), + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "anisotropic")), "filter.stylize.diffuse", json!({"mode": "anisotropic"})), + ( + b"TrcC", + Descriptor::new("TrcC").with("Lvl ", Value::Integer(128)).with("Edg ", enumv("CntE", "Upr ")), + "filter.stylize.traceContour", + json!({"level": 128, "edge": "upper"}), + ), + ( + b"TrcC", + Descriptor::new("TrcC").with("Lvl ", Value::Integer(60)).with("Edg ", enumv("CntE", "Lwr ")), + "filter.stylize.traceContour", + json!({"level": 60, "edge": "lower"}), + ), + (b"Crst", Descriptor::new("Crst").with("ClSz", Value::Integer(10)), "filter.pixelate.crystallize", json!({"cellSize": 10})), + ( + b"Pntl", + Descriptor::new("Pntl").with("ClSz", Value::Integer(7)), + "filter.pixelate.pointillize", + json!({"cellSize": 7, "background": [1.0, 1.0, 1.0, 1.0]}), + ), + ]; + for (id, fltr, command, params) in cases { + let item = photoshop_item(id, fltr); + // Through bytes, as read from a file. + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!((f.command.as_str(), &f.params), (command, ¶ms)); + // Written back in the measured shape: same class, keys, value types and filterID. + let back = item_for_filter(&f).unwrap(); + for key in ["Fltr", "filterID", "BckC"] { + assert_eq!(back.get(key), item.get(key), "{command} {key}"); + } + } + // The `anisotropic` mode is a long id, as Photoshop writes it. + let d = item_for_filter(&sf("filter.stylize.diffuse", json!({"mode": "anisotropic"}))).unwrap(); + let Some(Value::Descriptor(fltr)) = d.get("Fltr") else { panic!("no Fltr") }; + assert!(matches!(fltr.get("Md "), Some(Value::Enumerated { value: Id::Str(v), .. }) if v == b"anisotropic")); + // Pointillize takes the entry's background colour; the others ignore it. + let mut item = photoshop_item(b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Integer(7))); + set(&mut item, "BckC", rgbc(51.0)); + let f = filter_from_item(&item); + assert_eq!(f.params["background"], json!([0.2, 0.2, 0.2, 1.0])); + assert_eq!(item_for_filter(&f).unwrap().get("BckC"), Some(&rgbc(51.0))); + let mut item = photoshop_item(b"Crst", Descriptor::new("Crst").with("ClSz", Value::Integer(7))); + set(&mut item, "BckC", rgbc(51.0)); + assert_eq!(filter_from_item(&item).params, json!({"cellSize": 7})); + // Our random seed has no PSD key: it is not written. + let fltr = |c: &str, p: J| item_for_filter(&sf(c, p)).unwrap().get("Fltr").cloned(); + assert_eq!( + fltr("filter.stylize.diffuse", json!({"mode": "normal", "seed": 7})), + Some(Value::Descriptor(Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Nrml")))) + ); + // Defaults and out-of-range values are written as the engine renders them: clamped to + // Photoshop's ranges, and a non-number as the default. + assert_eq!( + fltr("filter.stylize.traceContour", json!({})), + Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(128)).with("Edg ", enumv("CntE", "Lwr ")))) + ); + assert_eq!(fltr("filter.pixelate.crystallize", json!({})), Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(10))))); + assert_eq!(fltr("filter.pixelate.pointillize", json!({})), Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(5))))); + assert_eq!( + fltr("filter.pixelate.crystallize", json!({"cellSize": 300})), + Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(300)))) + ); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": 2.6})), Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(3))))); + assert_eq!( + fltr("filter.stylize.traceContour", json!({"level": 0, "edge": "upper"})), + Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(0)).with("Edg ", enumv("CntE", "Upr ")))) + ); + assert_eq!( + fltr("filter.stylize.traceContour", json!({"level": 255.4})), + Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(255)).with("Edg ", enumv("CntE", "Lwr ")))) + ); + let crst = |v: i32| Some(Value::Descriptor(Descriptor::new("Crst").with("ClSz", Value::Integer(v)))); + let pntl = |v: i32| Some(Value::Descriptor(Descriptor::new("Pntl").with("ClSz", Value::Integer(v)))); + let trcc = |v: i32, e: &str| Some(Value::Descriptor(Descriptor::new("TrcC").with("Lvl ", Value::Integer(v)).with("Edg ", enumv("CntE", e)))); + assert_eq!(fltr("filter.pixelate.crystallize", json!({"cellSize": 1e9})), crst(300)); + assert_eq!(fltr("filter.pixelate.crystallize", json!({"cellSize": 2.4})), crst(3)); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": "big"})), pntl(5)); + assert_eq!(fltr("filter.pixelate.pointillize", json!({"cellSize": null})), pntl(5)); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": -40, "edge": "upper"})), trcc(0, "Upr ")); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": 255.5})), trcc(255, "Lwr ")); + assert_eq!(fltr("filter.stylize.traceContour", json!({"level": [128]})), trcc(128, "Lwr ")); +} + +#[test] +fn malformed_stylize_and_pixelate_filters_are_kept_verbatim() { + let long = Value::Integer; + let cases = [ + // Missing keys. + (b"Dfs ", Descriptor::new("Dfs ")), + (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(60))), + (b"TrcC", Descriptor::new("TrcC").with("Edg ", enumv("CntE", "Upr "))), + (b"Crst", Descriptor::new("Crst")), + (b"Pntl", Descriptor::new("Pntl")), + // Unknown enum values or enum types. + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("DfsM", "Wild"))), + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", enumv("BlnM", "Nrml"))), + (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(60)).with("Edg ", enumv("CntE", "Mddl"))), + // Wrong types (also NaN). + (b"Dfs ", Descriptor::new("Dfs ").with("Md ", long(1))), + (b"TrcC", Descriptor::new("TrcC").with("Lvl ", Value::Double(60.0)).with("Edg ", enumv("CntE", "Upr "))), + (b"Crst", Descriptor::new("Crst").with("ClSz", unit(b"#Pxl", 10.0))), + (b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Double(f64::NAN))), + // Out of range. + (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(256)).with("Edg ", enumv("CntE", "Upr "))), + (b"TrcC", Descriptor::new("TrcC").with("Lvl ", long(-1)).with("Edg ", enumv("CntE", "Upr "))), + (b"Crst", Descriptor::new("Crst").with("ClSz", long(2))), + (b"Pntl", Descriptor::new("Pntl").with("ClSz", long(i32::MAX))), + ]; + for (id, fltr) in cases { + let item = photoshop_item(id, fltr); + let f = filter_from_item(&Descriptor::from_bytes(&item.to_bytes()).unwrap()); + assert_eq!(f.command, UNSUPPORTED_FILTER, "{item:?}"); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes()); + } + // Saved without options (`hasoptions` false, no `Fltr`): kept verbatim. + let bare = Descriptor::new("filterFX").with("hasoptions", Value::Boolean(false)).with("filterID", Value::Integer(code(b"Crst"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); + // A Pointillize entry whose background colour is unreadable is kept verbatim too. + for bad in [Value::Integer(3), Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(0.0)))] { + let mut item = photoshop_item(b"Pntl", Descriptor::new("Pntl").with("ClSz", Value::Integer(7))); + set(&mut item, "BckC", bad); + assert_eq!(filter_from_item(&item).command, UNSUPPORTED_FILTER); + } + // Values with no PSD equivalent are reported, not guessed. + for (c, p) in [ + ("filter.stylize.diffuse", json!({"mode": "sideways"})), + ("filter.stylize.diffuse", json!({"mode": 3})), + ("filter.stylize.traceContour", json!({"edge": "middle"})), + ("filter.pixelate.pointillize", json!({"background": "teal"})), + ("filter.pixelate.pointillize", json!({"background": [1.0]})), + ] { + assert!(item_for_filter(&sf(c, p.clone())).is_err(), "{c} {p}"); + } +} + +/// A Filter Gallery item as Photopea writes it (Chrome, Detail 12, Smoothness 4). +fn gallery_item_with(effects: Vec) -> Descriptor { + let rgb = |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filtergalerie"))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "normal")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgb(0.0)) + .with("BckC", rgb(255.0)) + .with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::List(effects.into_iter().map(Value::Descriptor).collect())))) + .with("filterID", Value::Integer(code(b"GEfc"))) +} + +fn chrome(detail: i32, smoothness: i32, visible: bool) -> Descriptor { + Descriptor::new("GEfc") + .with("Dtl ", Value::Integer(detail)) + .with("Smth", Value::Integer(smoothness)) + .with("GEfk", enumv("GEft", "Chrm")) + .with("GELv", Value::Boolean(visible)) +} + +#[test] +fn filter_gallery_chrome_imports_as_editable_gallery() { + let f = filter_from_item(&gallery_item_with(vec![chrome(12, 4, true), chrome(3, 9, false)])); + assert_eq!(f.command, "filter.filterGallery"); + assert_eq!( + f.params, + json!({ + "effects": [ + {"filter": "chrome", "params": {"detail": 12, "smoothness": 4}, "visible": true}, + {"filter": "chrome", "params": {"detail": 3, "smoothness": 9}, "visible": false}, + ], + "foreground": [0.0, 0.0, 0.0, 1.0], + "background": [1.0, 1.0, 1.0, 1.0], + }) + ); + // Written back and read again: unchanged, also when re-blended and hidden. + let mut g = f.clone(); + g.blend = BlendMode::Multiply; + g.opacity = 0.5; + g.visible = false; + let d = item_for_filter(&g).unwrap(); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), g); + let fltr = get_desc(&d, "Fltr").unwrap(); + assert!(fltr.class_id.is("GEfc")); + assert_eq!(int(&d, "filterID"), Some(code(b"GEfc"))); +} + +/// A single effect as Photoshop 27 writes it: inline in `Fltr`, no `GEfs` list or `GELv`. +fn photoshop_chrome(detail: i32, smoothness: i32) -> Descriptor { + let rgb = |v: f64| Value::Descriptor(Descriptor::new("RGBC").with("Rd ", Value::Double(v)).with("Grn ", Value::Double(v)).with("Bl ", Value::Double(v))); + let mut fltr = Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Dtl ", Value::Integer(detail)).with("Smth", Value::Integer(smoothness)); + fltr.name = UnicodeString::new_nul("Filter Gallery"); + Descriptor::new("filterFX") + .with("Nm ", Value::Text(UnicodeString::new_nul("Filter Gallery..."))) + .with("blendOptions", Value::Descriptor(Descriptor::new("blendOptions").with("Opct", unit(b"#Prc", 100.0)).with("Md ", enumv("BlnM", "Nrml")))) + .with("enab", Value::Boolean(true)) + .with("hasoptions", Value::Boolean(true)) + .with("FrgC", rgb(255.0)) + .with("BckC", rgb(0.0)) + .with("Fltr", Value::Descriptor(fltr)) + .with("filterID", Value::Integer(code(b"GEfc"))) +} + +#[test] +fn filter_gallery_photoshop_inline_effect_imports_and_writes_back_inline() { + let item = photoshop_chrome(10, 4); + let f = filter_from_item(&item); + assert_eq!(f.command, "filter.filterGallery"); + assert_eq!( + f.params, + json!({ + "effects": [{"filter": "chrome", "params": {"detail": 10, "smoothness": 4}, "visible": true}], + "foreground": [1.0, 1.0, 1.0, 1.0], + "background": [0.0, 0.0, 0.0, 1.0], + }) + ); + // Written back in Photoshop's own layout. + let d = item_for_filter(&f).unwrap(); + assert_eq!(get_desc(&d, "Fltr"), get_desc(&item, "Fltr")); + assert_eq!((get_desc(&d, "FrgC"), get_desc(&d, "BckC")), (get_desc(&item, "FrgC"), get_desc(&item, "BckC"))); + assert_eq!(filter_from_item(&Descriptor::from_bytes(&d.to_bytes()).unwrap()), f); + // A hidden single effect needs the list (only it has a visibility flag). + let mut hidden = f.clone(); + hidden.params["effects"][0]["visible"] = json!(false); + let d = item_for_filter(&hidden).unwrap(); + assert!(get_desc(&d, "Fltr").unwrap().get("GEfs").is_some()); + assert_eq!(filter_from_item(&d), hidden); + // Inline effects we don't model stay verbatim. + let mut odd = photoshop_chrome(10, 4); + set(&mut odd, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("Dtl ", Value::Integer(1)))); + assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); + let mut extra = photoshop_chrome(10, 4); + set(&mut extra, "Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Chrm")).with("Xtra", Value::Integer(1)))); + assert_eq!(filter_from_item(&extra).command, UNSUPPORTED_FILTER); +} + +#[test] +fn gallery_table_matches_the_gallery_filters() { + for g in &GALLERY_EFFECTS { + let f = photocraft_algo::GalleryFilter::from_key(g.key).unwrap(); + let keys: Vec<&str> = f.params().iter().map(|p| p.key).collect(); + for (_, ours) in g.settings { + assert!(keys.contains(ours), "{}.{ours}", g.key); + } + } +} + +#[test] +fn filter_gallery_written_with_defaults_and_colours() { + let f = sf("filter.filterGallery", json!({"effects": [{"filter": "chrome"}], "foreground": "#ff0000", "background": [0.0, 0.0, 1.0, 1.0]})); + let back = filter_from_item(&item_for_filter(&f).unwrap()); + assert_eq!(back.params["effects"][0]["params"], json!({"detail": 4, "smoothness": 7})); + assert_eq!(back.params["foreground"], json!([1.0, 0.0, 0.0, 1.0])); + assert_eq!(back.params["background"], json!([0.0, 0.0, 1.0, 1.0])); +} + +#[test] +fn filter_gallery_we_dont_model_is_kept_verbatim() { + // Another effect, an extra setting, a wrong-typed setting, no effects, a non-list. + let other = Descriptor::new("GEfc").with("GEfk", enumv("GEft", "Ctot")).with("GELv", Value::Boolean(true)); + let extra = chrome(4, 7, true).with("Xtra", Value::Integer(1)); + let typed = Descriptor::new("GEfc").with("Dtl ", Value::Double(4.0)).with("GEfk", enumv("GEft", "Chrm")); + for effects in [vec![chrome(4, 7, true), other], vec![extra], vec![typed], vec![]] { + let item = gallery_item_with(effects); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER); + assert_eq!(item_for_filter(&f).unwrap(), item); + } + let odd = Descriptor::new("filterFX").with("Fltr", Value::Descriptor(Descriptor::new("GEfc").with("GEfs", Value::Integer(3)))); + assert_eq!(filter_from_item(&odd).command, UNSUPPORTED_FILTER); + let bare = Descriptor::new("filterFX").with("filterID", Value::Integer(code(b"GEfc"))); + assert_eq!(filter_from_item(&bare).command, UNSUPPORTED_FILTER); +} + +#[test] +fn filter_gallery_without_psd_equivalent_is_reported() { + for p in [ + json!({"effects": [{"filter": "cutout"}]}), + json!({"effects": [{"filter": "chrome", "params": {"sparkle": 1}}]}), + json!({"effects": [{"params": {}}]}), + json!({"effects": []}), + json!({}), + json!({"effects": [{"filter": "chrome"}], "list": true}), + json!({"effects": [{"filter": "chrome"}], "foreground": "red"}), + json!(null), + ] { + assert!(item_for_filter(&sf("filter.filterGallery", p.clone())).is_err(), "{p}"); + } +} + +/// Filters with no PSD equivalent are skipped with a warning each, and the writable ones around them still export. +#[test] +fn unwritable_filters_are_reported_not_fatal() { + let stack = FilterStack { + filters: vec![ + // Shear's curve isn't mapped to Photoshop's grid yet. + sf("filter.distort.shear", json!({"amount": 40})), + sf("filter.blur.gaussianBlur", json!({"radius": 2.0})), + sf(UNSUPPORTED_FILTER, json!({"psd": "zz"})), + sf("image.adjustments.levels", json!({"cyan": {"gamma": 2.0}})), + sf("image.adjustments.shadowsHighlights", json!({"shadowAmount": 50})), + ], + ..Default::default() + }; + let mut w = Vec::new(); + let d = filter_fx(&stack, "L", &mut w); + assert_eq!(w.len(), 4, "{w:?}"); + let back = filter_stack(&d); + assert_eq!(back.filters, vec![stack.filters[1].clone()]); +} + +#[test] +fn placed_blocks_parse_back() { + let t = Affine { m: [0.5, 0.1, -0.1, 0.5, 10.0, 20.0] }; + let stack = FilterStack { enabled: false, filters: vec![sf("filter.blur.gaussianBlur", json!({"radius": 3.0}))], mask_enabled: false, mask_linked: true }; + let mut w = Vec::new(); + let spec = PlacedSpec { + idnt: "id-1", + placed: "pl-2", + transform: t, + perspective: None, + size: (64.0, 32.0), + dpi: 72.0, + warp: None, + filter_fx: Some(filter_fx(&stack, "L", &mut w)), + }; + let sold = sold_bytes(None, &spec, &mut w); + assert!(w.is_empty()); + assert_eq!(sold.len() % 4, 0); + let p = parse_sold(&sold).unwrap(); + assert_eq!((p.idnt.as_str(), p.placed.as_str()), ("id-1", "pl-2")); + for (a, b) in p.transform.m.iter().zip(t.m) { + assert!((a - b).abs() < 1e-9); + } + assert_eq!(p.stack, Some(stack)); + let tb = photocraft_psd::TaggedBlock::new(*b"SoLd", sold.clone()); + tb.check_structure().unwrap(); + let pl = photocraft_psd::TaggedBlock::new(*b"PlLd", plld_bytes(&spec, &mut w)); + pl.check_structure().unwrap(); + // A template keeps its other keys; an unmoved transform keeps its exact quad. + let mut tmpl = sold_descriptor(&sold).unwrap(); + tmpl.items.push((Id::new("extraKey"), Value::Integer(7))); + let again = sold_bytes(Some(&tmpl), &PlacedSpec { filter_fx: None, ..spec }, &mut w); + let d = sold_descriptor(&again).unwrap(); + assert_eq!(d.get("extraKey"), Some(&Value::Integer(7))); + assert!(d.get("filterFX").is_none()); + assert_eq!(d.get("Trnf"), tmpl.get("Trnf")); +} + +/// A Distort / Perspective placement goes out as its four corners (`Trnf`, `nonAffineTransform`) +/// and comes back as the same projective map; an affine one reads back as no perspective. +#[test] +fn perspective_placement_round_trips_through_sold() { + let h = photocraft_algo::transform::Homography::rect_to_quad([0.0, 0.0, 64.0, 32.0], [[10.0, 5.0], [80.0, 8.0], [70.0, 50.0], [12.0, 40.0]]).unwrap(); + let mut w = Vec::new(); + let mut spec = PlacedSpec { + idnt: "id", + placed: "pl", + transform: Affine::IDENTITY, + perspective: Some(h.0), + size: (64.0, 32.0), + dpi: 72.0, + warp: None, + filter_fx: None, + }; + let sold = sold_bytes(None, &spec, &mut w); + let back = crate::blocks::parse_smart_perspective(b"SoLd", &sold).expect("a projective placement"); + let n = |m: [f64; 9]| m.map(|v| v / m[8]); + for (a, b) in n(back).iter().zip(n(h.0)) { + assert!((a - b).abs() < 1e-9 * (1.0 + b.abs()), "{back:?} vs {:?}", h.0); + } + // Affine placements carry no perspective. + spec.perspective = None; + spec.transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 5.0, 7.0] }; + let sold = sold_bytes(None, &spec, &mut w); + assert_eq!(crate::blocks::parse_smart_perspective(b"SoLd", &sold), None); + assert_eq!(crate::blocks::parse_smart_perspective(b"PlLd", &sold), None); +} + +/// `quad_moved` compares the corners a template stores with the placement's: unchanged keeps +/// the template's quad, a moved corner (affine or projective) or a missing quad rewrites it. +#[test] +fn quad_moved_detects_corner_changes() { + let t = Affine { m: [1.0, 0.0, 0.0, 1.0, 3.0, 4.0] }; + let mut w = Vec::new(); + let mut spec = PlacedSpec { idnt: "id", placed: "pl", transform: t, perspective: None, size: (20.0, 10.0), dpi: 72.0, warp: None, filter_fx: None }; + let template = parse_sold(&sold_bytes(None, &spec, &mut w)).expect("parses").descriptor; + assert!(!quad_moved(&template, &spec, spec.warp), "same placement"); + spec.transform = Affine { m: [1.0, 0.0, 0.0, 1.0, 3.5, 4.0] }; + assert!(quad_moved(&template, &spec, spec.warp), "moved half a pixel"); + spec.transform = t; + spec.perspective = + photocraft_algo::transform::Homography::rect_to_quad([0.0, 0.0, 20.0, 10.0], [[3.0, 4.0], [23.0, 4.0], [20.0, 12.0], [3.0, 14.0]]).map(|h| h.0); + assert!(quad_moved(&template, &spec, spec.warp), "one corner moved (Distort)"); + assert!(quad_moved(&Descriptor::new("null"), &spec, spec.warp), "no stored quad"); +} + +/// Corrupted placed-layer data and filter descriptors never panic: they parse to something +/// (unknown filters stay verbatim) or to nothing. +#[test] +fn mutated_placed_data_never_panics() { + let stack = FilterStack { + filters: vec![ + sf("filter.blur.gaussianBlur", json!({"radius": 3.0})), + sf("image.adjustments.curves", json!({"points": [[0.0, 0.0], [255.0, 255.0]]})), + sf("image.adjustments.levels", json!({"inBlack": 3, "inWhite": 250, "gamma": 1.1})), + ], + ..Default::default() + }; + let mut w = Vec::new(); + let spec = PlacedSpec { + idnt: "id", + placed: "pl", + transform: Affine::IDENTITY, + perspective: None, + size: (8.0, 8.0), + dpi: 72.0, + warp: None, + filter_fx: Some(filter_fx(&stack, "L", &mut w)), + }; + let sold = sold_bytes(None, &spec, &mut w); + let mut state = 0x2545_F491_4F6C_DD1Du64; + let mut next = || { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + state + }; + for _ in 0..3000 { + let mut b = sold.clone(); + for _ in 0..1 + next() % 3 { + let i = (next() as usize) % b.len(); + b[i] = next() as u8; + } + if let Some(p) = parse_sold(&b) { + for f in p.stack.iter().flat_map(|s| &s.filters) { + let _ = item_for_filter(f); + } + let _ = sold_bytes(Some(&p.descriptor), &spec, &mut w); + } + } +} + +#[test] +fn warps_are_written_and_read_back() { + use photocraft_geom::warp::BezierMesh; + let bounds = [0.0, 0.0, 60.0, 30.0]; + let mut custom = BezierMesh::identity(bounds, 1, 1); + custom.points[15] = [67.0, 30.0]; + let mut multi = Warp::preset(WarpStyle::Custom, 0.0, bounds); + let mut split = BezierMesh::identity(bounds, 2, 2); + split.points[8][1] += 4.0; + multi.mesh = Some(split); + let mut arc = Warp::preset(WarpStyle::Arc, 40.0, bounds); + arc.vertical = true; + arc.h_distort = 10.0; + let mut c = Warp::preset(WarpStyle::Custom, 0.0, bounds); + c.mesh = Some(custom); + for (w, back) in [(Some(arc.clone()), Some(arc)), (Some(c.clone()), Some(c)), (Some(multi.clone()), Some(multi)), (None, None)] { + let mut warnings = Vec::new(); + let spec = PlacedSpec { + idnt: "i", + placed: "p", + transform: Affine::IDENTITY, + perspective: None, + size: (60.0, 30.0), + dpi: 72.0, + warp: w.as_ref(), + filter_fx: None, + }; + let sold = sold_bytes(None, &spec, &mut warnings); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), back); + assert!(warnings.is_empty(), "{warnings:?}"); + } +} + +/// A placed layer over a 50 × 25 source, at `transform`, with `warp`. +fn placed<'a>(transform: Affine, warp: Option<&'a Warp>) -> PlacedSpec<'a> { + PlacedSpec { idnt: "i", placed: "p", transform, perspective: None, size: (50.0, 25.0), dpi: 72.0, warp, filter_fx: None } +} + +/// A 3 × 2 patch split warp over the 50 × 25 source with one interior control point pulled. +fn split_warp() -> Warp { + use photocraft_geom::warp::BezierMesh; + let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 3, 2); + mesh.points[10 * 3 + 4][1] += 3.0; + Warp::custom(mesh, [0.0, 0.0, 50.0, 25.0]) +} + +/// An imported `soLD` descriptor holding `warp` as Photoshop writes a split warp: a +/// `quiltWarp` whose outer slices sit 0.6 px past the source, before a `warpNone` `warp`. +fn photoshop_template(warp: &Warp) -> Descriptor { + let mesh = warp.mesh.as_ref().expect("a mesh"); + let slices = |k: &str, knots: &[f64], len: f64| { + let n = knots.len() - 1; + let values = knots + .iter() + .enumerate() + .map(|(i, u)| { + if i == 0 { + -0.6 + } else if i == n { + len + 0.6 + } else { + u * len + } + }) + .collect::>(); + let prefix = values.len() as u32; + Value::ObjectArray(ObjectArray { prefix, body: Descriptor::new("UntF").with(k, Value::UnitFloats { unit: *b"#Pxl", values }) }) + }; + let Value::Descriptor(plain) = warp_desc(None, (50.0, 25.0), &mut Vec::new()) else { panic!("a descriptor") }; + let mut quilt = plain.clone(); + quilt.class_id = Id::new("quiltWarp"); + set(&mut quilt, "warpStyle", enumv("warpStyle", "warpCustom")); + let points = Descriptor::new("rationalPoint") + .with("Hrzn", Value::UnitFloats { unit: *b"#Pxl", values: mesh.points.iter().map(|p| p[0]).collect() }) + .with("Vrtc", Value::UnitFloats { unit: *b"#Pxl", values: mesh.points.iter().map(|p| p[1]).collect() }); + let envelope = Descriptor::new("customEnvelopeWarp") + .with("quiltSliceX", slices("quiltSliceX", &mesh.us, 50.0)) + .with("quiltSliceY", slices("quiltSliceY", &mesh.vs, 25.0)) + .with("meshPoints", Value::ObjectArray(ObjectArray { prefix: mesh.points.len() as u32, body: points })); + quilt = quilt + .with("deformNumRows", Value::Integer(mesh.ny() as i32)) + .with("deformNumCols", Value::Integer(mesh.nx() as i32)) + .with("customEnvelopeWarp", Value::Descriptor(envelope)); + let mut d = sold_descriptor(&sold_bytes(None, &placed(Affine::IDENTITY, None), &mut Vec::new())).expect("a descriptor"); + let at = d.items.iter().position(|(k, _)| k.is("warp")).expect("a warp key"); + d.items.insert(at, (Id::new("quiltWarp"), Value::Descriptor(quilt))); + d +} + +fn sold_of(d: Descriptor) -> Vec { + let mut out = b"soLD".to_vec(); + out.extend_from_slice(&4u32.to_be_bytes()); + out.extend(VersionedDescriptor::new(d).to_bytes()); + out +} + +fn quilt_of(d: &Descriptor) -> Option<&Descriptor> { + get_desc(d, "quiltWarp") +} + +/// A split warp of more patches along a side than a `quiltWarp` is read with (Warp Grid 64, +/// then one more split) is written as no warp, with a message, framed by the source rect, so +/// the layer reopens where it was. Before, its `quiltWarp` was refused on reopen while `Trnf` +/// still framed its control box: the layer came back stretched and shifted. +#[test] +fn a_split_warp_past_the_quilt_limit_keeps_its_placement() { + use photocraft_geom::warp::BezierMesh; + let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], crate::blocks::QUILT_MAX_PATCHES, 1); + assert!(mesh.split_u(0.005)); + mesh.points[0] = [-10.0, -5.0]; + let warp = Warp::custom(mesh, [0.0, 0.0, 50.0, 25.0]); + let transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 120.0, 210.0] }; + let mut warnings = Vec::new(); + let sold = sold_bytes(None, &placed(transform, Some(&warp)), &mut warnings); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert_eq!(crate::blocks::parse_smart(b"SoLd", &sold).1, transform); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), None); + assert!(quilt_of(&sold_descriptor(&sold).expect("a descriptor")).is_none()); + // Over an imported split warp, the stale `quiltWarp` goes and the placement holds. + let template = photoshop_template(&split_warp()); + let mut warnings = Vec::new(); + let sold = sold_bytes(Some(&template), &placed(transform, Some(&warp)), &mut warnings); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert_eq!(crate::blocks::parse_smart(b"SoLd", &sold).1, transform); + assert!(quilt_of(&sold_descriptor(&sold).expect("a descriptor")).is_none()); + // So does one this reader can't read, which Photoshop would still apply. + let mut template = photoshop_template(&split_warp()); + let Some((_, Value::Descriptor(q))) = template.items.iter_mut().find(|(k, _)| k.is("quiltWarp")) else { panic!("a quiltWarp") }; + set(q, "deformNumRows", Value::Integer(0)); + let sold = sold_bytes(Some(&template), &placed(transform, Some(&warp)), &mut Vec::new()); + assert!(quilt_of(&sold_descriptor(&sold).expect("a descriptor")).is_none()); + // At the limit the split warp is still written. + let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], crate::blocks::QUILT_MAX_PATCHES, 1); + mesh.points[0] = [-10.0, -5.0]; + let warp = Warp::custom(mesh, [0.0, 0.0, 50.0, 25.0]); + let mut warnings = Vec::new(); + let sold = sold_bytes(None, &placed(transform, Some(&warp)), &mut warnings); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(crate::blocks::parse_smart(b"SoLd", &sold).1, transform); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), Some(warp)); +} + +/// A split (multi-patch) custom warp is written as Photoshop writes it, a `quiltWarp` before a +/// `warpNone` `warp`, and reads back with its mesh and its transform. Before, the warp was +/// written as none while `Trnf` was still the image of its control-point box, so the layer +/// came back stretched and shifted. +#[test] +fn split_warp_round_trips_through_sold() { + use photocraft_geom::warp::BezierMesh; + let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 2, 1); + mesh.points[0] = [-10.0, -5.0]; + let warp = Warp::custom(mesh, [0.0, 0.0, 50.0, 25.0]); + let transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 120.0, 210.0] }; + let mut warnings = Vec::new(); + let sold = sold_bytes(None, &placed(transform, Some(&warp)), &mut warnings); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(crate::blocks::parse_smart(b"SoLd", &sold).1, transform); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), Some(warp)); + let d = sold_descriptor(&sold).expect("a descriptor"); + let at = |key: &str| d.items.iter().position(|(k, _)| k.is(key)); + assert!(at("quiltWarp").expect("a quiltWarp") < at("warp").expect("a warp")); + let plain = get_desc(&d, "warp").expect("a warp"); + assert_eq!(enum_of(plain, "warpStyle"), Some(&b"warpNone"[..])); + assert!(plain.get("customEnvelopeWarp").is_none()); + let quilt = quilt_of(&d).expect("a quiltWarp"); + assert!(quilt.class_id.is("quiltWarp")); + assert_eq!((quilt.get("deformNumCols"), quilt.get("deformNumRows")), (Some(&Value::Integer(7)), Some(&Value::Integer(4)))); + let env = get_desc(quilt, "customEnvelopeWarp").expect("an envelope"); + let prefix = |k: &str| match env.get(k) { + Some(Value::ObjectArray(a)) => Some(a.prefix), + _ => None, + }; + assert_eq!((prefix("quiltSliceX"), prefix("quiltSliceY"), prefix("meshPoints")), (Some(3), Some(2), Some(28))); +} + +/// A template's split warp is replaced along with its `warp`: a warp changed to Arc, to none, +/// to one patch or to another mesh reads back as changed, not as the imported split warp. +#[test] +fn a_replaced_split_warp_does_not_come_back() { + use photocraft_geom::warp::BezierMesh; + let bounds = [0.0, 0.0, 50.0, 25.0]; + let split = split_warp(); + let template = photoshop_template(&split); + let arc = Warp::preset(WarpStyle::Arc, 30.0, bounds); + let mut one = BezierMesh::identity(bounds, 1, 1); + one.points[5][0] += 2.0; + let one = Warp::custom(one, bounds); + let mut resplit = BezierMesh::identity(bounds, 2, 2); + resplit.points[8][1] += 4.0; + let resplit = Warp::custom(resplit, bounds); + for w in [Some(arc), None, Some(one), Some(resplit)] { + let mut warnings = Vec::new(); + let sold = sold_bytes(Some(&template), &placed(Affine::IDENTITY, w.as_ref()), &mut warnings); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), w); + let d = sold_descriptor(&sold).expect("a descriptor"); + assert_eq!(quilt_of(&d).is_some(), w.as_ref().is_some_and(|w| w.mesh.as_ref().is_some_and(|m| m.points.len() > 16)), "{w:?}"); + assert!(warnings.is_empty(), "{warnings:?}"); + } +} + +/// An imported split warp that nothing changed keeps Photoshop's `quiltWarp` as stored when +/// the layer is rewritten for something else (here a move); a mesh edited on the same grid +/// keeps Photoshop's slices. +#[test] +fn an_imported_split_warp_keeps_photoshop_s_quilt() { + let template = photoshop_template(&split_warp()); + let read = crate::blocks::parse_placed_warp(b"SoLd", &sold_of(template.clone())).expect("the split warp"); + let moved = Affine { m: [1.0, 0.0, 0.0, 1.0, 4.0, 0.0] }; + let sold = sold_bytes(Some(&template), &placed(moved, Some(&read)), &mut Vec::new()); + let d = sold_descriptor(&sold).expect("a descriptor"); + assert_eq!(d.get("quiltWarp"), template.get("quiltWarp")); + assert_eq!(crate::blocks::parse_smart(b"SoLd", &sold).1, moved); + let mut edited = read.clone(); + if let Some(m) = edited.mesh.as_mut() { + m.points[12][0] += 1.5; + } + let sold = sold_bytes(Some(&template), &placed(Affine::IDENTITY, Some(&edited)), &mut Vec::new()); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), Some(edited)); + let slices = |d: &Descriptor| { + let env = get_desc(quilt_of(d)?, "customEnvelopeWarp")?; + Some((env.get("quiltSliceX").cloned(), env.get("quiltSliceY").cloned())) + }; + assert_eq!(slices(&sold_descriptor(&sold).expect("a descriptor")), slices(&template)); +} + +/// The legacy `PlLd` record has no split warp: it stores none, with its corners framed by the +/// source rect, so a reader of `PlLd` alone places the layer where its transform puts it. +#[test] +fn plld_of_a_split_warp_is_framed_by_the_source() { + let warp = split_warp(); + let transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 120.0, 210.0] }; + let mut warnings = Vec::new(); + let plld = plld_bytes(&placed(transform, Some(&warp)), &mut warnings); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(crate::blocks::parse_plld_warp(&plld), crate::blocks::PlldWarp::None); + let (_, corners) = crate::blocks::parse_plld(&plld).expect("a record"); + assert_eq!(crate::blocks::plld_transform(&corners, Some((50.0, 25.0)), None).0, transform); +} + +/// Export writes `Trnf` as the image of the custom warp's control-point box, the inverse of +/// import: a spilled mesh comes back with the same transform and the quad Photoshop expects. +#[test] +fn warp_control_box_quad_round_trips() { + use photocraft_geom::warp::BezierMesh; + let mut mesh = BezierMesh::identity([0.0, 0.0, 50.0, 25.0], 1, 1); + mesh.points[0] = [-10.0, -5.0]; + mesh.points[15] = [60.0, 25.0]; + let mut warp = Warp::preset(WarpStyle::Custom, 0.0, [0.0, 0.0, 50.0, 25.0]); + warp.mesh = Some(mesh); + let transform = Affine { m: [2.0, 0.0, 0.0, 2.0, 120.0, 210.0] }; + let spec = PlacedSpec { idnt: "i", placed: "p", transform, perspective: None, size: (50.0, 25.0), dpi: 72.0, warp: Some(&warp), filter_fx: None }; + let sold = sold_bytes(None, &spec, &mut Vec::new()); + let p = parse_sold(&sold).expect("parses"); + let Some(Value::List(q)) = p.descriptor.get("Trnf") else { panic!("no Trnf") }; + let q: Vec = q.iter().filter_map(|v| num(Some(v))).collect(); + assert_eq!(q, [100.0, 200.0, 240.0, 200.0, 240.0, 260.0, 100.0, 260.0]); + assert_eq!(p.transform, transform); + // The imported quad reads as unmoved, so the template's `Trnf` is kept as stored. + assert!(!quad_moved(&p.descriptor, &spec, spec.warp)); +} + +/// #4112: a regenerated `SoLd` (after Replace Contents, a move…) writes a cylinder back as +/// `warpCylinder` and its seven values, not as no warp, and frames its control-point box with +/// the quad, affine or projective, so import places it where it was. +#[test] +fn cylinder_warp_round_trips_through_sold() { + let v = [100.0, 800.0, 900.0, 200.0, 0.3, 0.3, 0.5]; + let warp = Warp::cylinder(&v, [0.0, 0.0, 1000.0, 1000.0]).unwrap(); + let transform = Affine { m: [0.5, 0.0, 0.0, 0.5, 40.0, 30.0] }; + let mut spec = PlacedSpec { idnt: "i", placed: "p", transform, perspective: None, size: (1000.0, 1000.0), dpi: 72.0, warp: Some(&warp), filter_fx: None }; + let mut warnings = Vec::new(); + let sold = sold_bytes(None, &spec, &mut warnings); + assert!(warnings.is_empty(), "{warnings:?}"); + let p = parse_sold(&sold).expect("parses"); + let Some(Value::Descriptor(w)) = p.descriptor.get("warp") else { panic!("no warp") }; + assert_eq!(crate::blocks::enum_of(w, "warpStyle"), Some(b"warpCylinder".as_slice())); + let Some(Value::List(values)) = w.get("warpValues") else { panic!("no warpValues") }; + assert_eq!(values.iter().filter_map(|x| num(Some(x))).collect::>(), v); + assert!(w.get("customEnvelopeWarp").is_none()); + assert_eq!(crate::blocks::parse_placed_warp(b"SoLd", &sold), Some(warp.clone())); + for (a, b) in p.transform.m.iter().zip(transform.m) { + assert!((a - b).abs() < 1e-9, "{:?}", p.transform); + } + // The quad is the image of the control-point box, not of the source rect. + let cb = warp.mesh.as_ref().unwrap().control_bounds(); + let Some(Value::List(q)) = p.descriptor.get("Trnf") else { panic!("no Trnf") }; + let q: Vec = q.iter().filter_map(|x| num(Some(x))).collect(); + let want = [cb[0], cb[1], cb[2], cb[1], cb[2], cb[3], cb[0], cb[3]].map(|c| 0.5 * c); + for (i, (a, b)) in q.iter().zip(want).enumerate() { + let shift = if i % 2 == 0 { 40.0 } else { 30.0 }; + assert!((a - (b + shift)).abs() < 1e-9, "corner {i}: {q:?}"); + } + assert!(!quad_moved(&p.descriptor, &spec, spec.warp)); + // A projective placement comes back as the same map. + let h = photocraft_algo::transform::Homography::rect_to_quad(cb, [[10.0, 20.0], [410.0, 60.0], [400.0, 480.0], [30.0, 520.0]]).unwrap(); + spec.perspective = Some(h.0); + let sold = sold_bytes(None, &spec, &mut warnings); + let back = crate::blocks::parse_smart_perspective(b"SoLd", &sold).expect("projective"); + let n = |m: [f64; 9]| m.map(|x| x / m[8]); + for (a, b) in n(back).iter().zip(n(h.0)) { + assert!((a - b).abs() < 1e-9 * (1.0 + b.abs()), "{back:?} vs {:?}", h.0); + } + let plld = plld_bytes(&spec, &mut warnings); + assert_eq!(crate::blocks::parse_plld_warp(&plld), crate::blocks::PlldWarp::Warp(warp)); + assert!(warnings.is_empty(), "{warnings:?}"); +} + +#[test] +fn hex_and_uuid() { + assert_eq!(from_hex(&to_hex(&[0, 1, 0xab, 0xff])), Some(vec![0, 1, 0xab, 0xff])); + assert_eq!(from_hex("abc"), None); + assert_eq!(from_hex("zz"), None); + let u = uuid_from(b"x"); + assert_eq!(u.len(), 36); + assert_eq!(u, uuid_from(b"x")); + assert_ne!(u, uuid_from(b"y")); +} + +// Render filters as Photoshop 27 saves them on a smart object (`filterFXList` items, with +// neutral blending): Clouds and Difference Clouds in white on black, Fibers in black on white, +// Lens Flare with the 50-300mm Zoom at 159%. +const PHOTOSHOP_CLOUDS: &str = "0000000100000000000866696c746572465800000008000000004e6d202054455854000000070043006c006f00750064007300000000000c626c656e644f7074696f6e734f626a63000000000000000c626c656e644f7074696f6e7300000002000000004f706374556e7446235072634059000000000000000000004d642020656e756d00000000426c6e4d000000066e6f726d616c00000000656e6162626f6f6c010000000a6861736f7074696f6e73626f6f6c0100000000467267434f626a630000000100000000000052474243000000030000000052642020646f7562406fe000000000000000000047726e20646f7562406fe0000000000000000000426c2020646f7562406fe000000000000000000042636b434f626a630000000100000000000052474243000000030000000052642020646f756200000000000000000000000047726e20646f7562000000000000000000000000426c2020646f7562000000000000000000000000466c74724f626a63000000070043006c006f007500640073000000000000436c64730000000100000000466c52736c6f6e67000049e40000000866696c74657249446c6f6e67436c6473"; +const PHOTOSHOP_DIFFERENCE_CLOUDS: &str = "0000000100000000000866696c746572465800000008000000004e6d202054455854000000120044006900660066006500720065006e0063006500200043006c006f00750064007300000000000c626c656e644f7074696f6e734f626a63000000000000000c626c656e644f7074696f6e7300000002000000004f706374556e7446235072634059000000000000000000004d642020656e756d00000000426c6e4d000000066e6f726d616c00000000656e6162626f6f6c010000000a6861736f7074696f6e73626f6f6c0100000000467267434f626a630000000100000000000052474243000000030000000052642020646f7562406fe000000000000000000047726e20646f7562406fe0000000000000000000426c2020646f7562406fe000000000000000000042636b434f626a630000000100000000000052474243000000030000000052642020646f756200000000000000000000000047726e20646f7562000000000000000000000000426c2020646f7562000000000000000000000000466c74724f626a63000000120044006900660066006500720065006e0063006500200043006c006f007500640073000000000000446672430000000100000000466c52736c6f6e67000057d20000000866696c74657249446c6f6e6744667243"; +const PHOTOSHOP_FIBERS: &str = "0000000100000000000866696c746572465800000008000000004e6d2020544558540000000a004600690062006500720073002e002e002e00000000000c626c656e644f7074696f6e734f626a63000000000000000c626c656e644f7074696f6e7300000002000000004f706374556e7446235072634059000000000000000000004d642020656e756d00000000426c6e4d000000066e6f726d616c00000000656e6162626f6f6c010000000a6861736f7074696f6e73626f6f6c0100000000467267434f626a630000000100000000000052474243000000030000000052642020646f756200000000000000000000000047726e20646f7562000000000000000000000000426c2020646f756200000000000000000000000042636b434f626a630000000100000000000052474243000000030000000052642020646f7562406fe000000000000000000047726e20646f7562406fe0000000000000000000426c2020646f7562406fe0000000000000000000466c74724f626a630000000700460069006200650072007300000000000046627273000000030000000056726e636c6f6e670000001000000000537472676c6f6e670000000400000000526e64536c6f6e67000062f10000000866696c74657249446c6f6e6746627273"; +const PHOTOSHOP_LENS_FLARE: &str = "0000000100000000000866696c746572465800000008000000004e6d2020544558540000000e004c0065006e007300200046006c006100720065002e002e002e00000000000c626c656e644f7074696f6e734f626a63000000000000000c626c656e644f7074696f6e7300000002000000004f706374556e7446235072634059000000000000000000004d642020656e756d00000000426c6e4d000000066e6f726d616c00000000656e6162626f6f6c010000000a6861736f7074696f6e73626f6f6c0100000000467267434f626a630000000100000000000052474243000000030000000052642020646f756200000000000000000000000047726e20646f7562000000000000000000000000426c2020646f756200000000000000000000000042636b434f626a630000000100000000000052474243000000030000000052642020646f7562406fe000000000000000000047726e20646f7562406fe0000000000000000000426c2020646f7562406fe0000000000000000000466c74724f626a630000000b004c0065006e007300200046006c0061007200650000000000004c6e73460000000300000000427267686c6f6e670000009f00000000466c72434f626a6300000001000000000000506e7420000000020000000048727a6e646f75623fe86666600000000000000056727463646f75623fe60b60c0000000000000004c6e7320656e756d000000004c6e7320000000005a6d20200000000866696c74657249446c6f6e674c6e7346"; + +fn photoshop(hex: &str) -> Descriptor { + Descriptor::from_bytes(&from_hex(hex).unwrap()).unwrap() +} + +fn set_lens(item: &mut Descriptor, code: [u8; 4]) { + let Some(Value::Descriptor(fltr)) = item.items.iter_mut().find(|(k, _)| k.is("Fltr")).map(|(_, v)| v) else { panic!("no Fltr") }; + set(fltr, "Lns ", Value::Enumerated { type_id: Id::new("Lns "), value: Id::Code(code) }); +} + +#[test] +fn photoshop_render_filters_read_and_write_like_photoshop() { + let colours = |fg: f64, bg: f64| (json!([fg, fg, fg, 1.0]), json!([bg, bg, bg, 1.0])); + let (white, black) = colours(1.0, 0.0); + let cases = [ + (PHOTOSHOP_CLOUDS, "filter.render.clouds", json!({"seed": 18916, "foreground": white, "background": black})), + (PHOTOSHOP_DIFFERENCE_CLOUDS, "filter.render.differenceClouds", json!({"seed": 22482, "foreground": white, "background": black})), + (PHOTOSHOP_FIBERS, "filter.render.fibers", json!({"variance": 16, "strength": 4, "seed": 25329, "foreground": black, "background": white})), + ( + PHOTOSHOP_LENS_FLARE, + "filter.render.lensFlare", + json!({"brightness": 159, "centerX": 0.762499988079071, "centerY": 0.6888889074325562, "lens": "zoom"}), + ), + ]; + for (hex, command, params) in cases { + let item = photoshop(hex); + let f = filter_from_item(&item); + assert_eq!((f.command.as_str(), &f.params), (command, ¶ms)); + // Written back, it is Photoshop's item again: name, colours, settings and key order. + assert_eq!(item_for_filter(&f).unwrap().items, item.items, "{command}"); + } +} + +#[test] +fn every_lens_flare_lens_round_trips_with_photoshops_code() { + for (code, lens) in LENSES { + let mut item = photoshop(PHOTOSHOP_LENS_FLARE); + set_lens(&mut item, *code); + let f = filter_from_item(&item); + assert_eq!(f.params["lens"], lens); + assert_eq!(item_for_filter(&f).unwrap().items, item.items, "{lens}"); + } + // A lens we don't know keeps the filter verbatim. + let mut item = photoshop(PHOTOSHOP_LENS_FLARE); + set_lens(&mut item, *b"Xxxx"); + assert_eq!(filter_from_item(&item).command, UNSUPPORTED_FILTER); +} + +/// The descriptor at `path` (nested keys) inside `item`, for editing. +fn desc_at<'a>(item: &'a mut Descriptor, path: &[&str]) -> &'a mut Descriptor { + path.iter().fold(item, |d, key| { + let Some(Value::Descriptor(x)) = d.items.iter_mut().find(|(k, _)| k.is(key)).map(|(_, v)| v) else { panic!("no {key}") }; + x + }) +} + +#[test] +fn render_filters_with_settings_we_dont_model_stay_verbatim() { + // Each case changes one thing in a Photoshop item: an extra setting, a repeated one, or a + // value of another type than Photoshop writes. Opened as editable, a re-save would drop the + // extra setting or rewrite the value; kept verbatim, the item is written back byte for byte. + type Edit = Box; + /// A descriptor path in the item (nested keys) and the edit made there. + type Change = (&'static [&'static str], Edit); + let extra: fn(&mut Descriptor) = |d| d.items.push((Id::new("Xtra"), Value::Integer(1))); + let as_double = |key: &'static str| -> Edit { Box::new(move |d| set(d, key, Value::Double(1.0))) }; + let as_long = |key: &'static str| -> Edit { Box::new(move |d| set(d, key, Value::Integer(1))) }; + let repeat = |key: &'static str| -> Edit { + Box::new(move |d| { + let v = d.get(key).cloned().unwrap(); + d.items.push((Id::new(key), v)); + }) + }; + let colour_edits = || -> Vec { + vec![ + (&["FrgC"], Box::new(|d: &mut Descriptor| d.items.push((Id::new("redFloat"), Value::Double(0.5))))), + (&["BckC"], as_long("Grn ")), + (&["FrgC"], Box::new(|d: &mut Descriptor| set(d, "Rd ", Value::Double(300.0)))), + (&["BckC"], Box::new(|d: &mut Descriptor| d.class_id = Id::new("HSBC"))), + ] + }; + let mut cases: Vec<(&str, &'static [&'static str], Edit)> = Vec::new(); + for hex in [PHOTOSHOP_CLOUDS, PHOTOSHOP_DIFFERENCE_CLOUDS] { + cases.push((hex, &["Fltr"], Box::new(extra))); + cases.push((hex, &["Fltr"], repeat("FlRs"))); + cases.push((hex, &["Fltr"], as_double("FlRs"))); + cases.extend(colour_edits().into_iter().map(|(p, e)| (hex, p, e))); + } + cases.push((PHOTOSHOP_FIBERS, &["Fltr"], Box::new(extra))); + cases.push((PHOTOSHOP_FIBERS, &["Fltr"], repeat("Strg"))); + for key in ["Vrnc", "Strg", "RndS"] { + cases.push((PHOTOSHOP_FIBERS, &["Fltr"], as_double(key))); + } + cases.extend(colour_edits().into_iter().map(|(p, e)| (PHOTOSHOP_FIBERS, p, e))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr"], Box::new(extra))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr"], repeat("Brgh"))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr"], as_double("Brgh"))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr"], Box::new(|d| set(d, "Lns ", Value::Enumerated { type_id: Id::new("Xxxx"), value: Id::Code(*b"Zm ") })))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr", "FlrC"], Box::new(extra))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr", "FlrC"], as_long("Hrzn"))); + cases.push((PHOTOSHOP_LENS_FLARE, &["Fltr", "FlrC"], Box::new(|d| set(d, "Vrtc", Value::UnitFloat { unit: *b"#Pxl", value: 0.5 })))); + for (i, (hex, path, edit)) in cases.iter().enumerate() { + let mut item = photoshop(hex); + edit(desc_at(&mut item, path)); + let f = filter_from_item(&item); + assert_eq!(f.command, UNSUPPORTED_FILTER, "case {i} ({path:?}): {:?}", f.params); + assert_eq!(item_for_filter(&f).unwrap().to_bytes(), item.to_bytes(), "case {i} ({path:?})"); + } +} + +#[test] +fn render_filters_made_in_photocraft_are_written() { + // Without colours (an older document or a script), Photoshop's default black and white. + let back = filter_from_item(&item_for_filter(&sf("filter.render.clouds", json!({"seed": 7}))).unwrap()); + assert_eq!(back.params, json!({"seed": 7, "foreground": [0.0, 0.0, 0.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]})); + // A seed above i32::MAX keeps its bit pattern, and colours may be hex. + let p = json!({"variance": 30, "strength": 10, "seed": 4_000_000_000u32, "foreground": "#ff0000", "background": [0.0, 0.0, 1.0, 1.0]}); + let back = filter_from_item(&item_for_filter(&sf("filter.render.fibers", p)).unwrap()); + assert_eq!( + back.params, + json!({"variance": 30, "strength": 10, "seed": 4_000_000_000u32, "foreground": [1.0, 0.0, 0.0, 1.0], "background": [0.0, 0.0, 1.0, 1.0]}) + ); + let flare = sf("filter.render.lensFlare", json!({"brightness": 120, "centerX": 0.25, "centerY": 0.75, "lens": "moviePrime"})); + assert_eq!(filter_from_item(&item_for_filter(&flare).unwrap()).params, flare.params); + // Lenses and colours we can't write are reported, not guessed. + assert!(item_for_filter(&sf("filter.render.lensFlare", json!({"lens": "fisheye"}))).unwrap_err().contains("fisheye")); + assert!(item_for_filter(&sf("filter.render.clouds", json!({"foreground": "red"}))).is_err()); +} diff --git a/crates/io/src/svg.rs b/crates/io/src/svg.rs index 04fc4bee3a..27b536d996 100644 --- a/crates/io/src/svg.rs +++ b/crates/io/src/svg.rs @@ -28,6 +28,79 @@ use crate::{ImportResult, IoError}; pub const MAX_SIDE: u32 = 16_384; /// A rasterised fallback layer or a smart-object render never exceeds this many pixels. const MAX_RASTER_PIXELS: u64 = 24_000_000; +/// Largest decoded byte size (RGBA8) accepted for an embedded raster image before resvg decodes +/// it. resvg/tiny-skia allocate the full intrinsic image before any destination-size scaling, +/// so a highly compressed PNG or WebP could otherwise trigger gigabyte-scale allocation and +/// decompression that the destination pixmap's size check never sees. +const MAX_EMBEDDED_IMAGE_BYTES: u64 = MAX_RASTER_PIXELS * 4; + +/// The IHDR width/height of a PNG, read directly from its header (no decoding): the signature +/// (8 bytes), the IHDR chunk's length and type (8 bytes), then big-endian width and height. +fn png_dimensions(data: &[u8]) -> Option<(u32, u32)> { + let sig = data.get(0..8)?; + if sig != [0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A] { + return None; + } + if data.get(12..16)? != b"IHDR" { + return None; + } + let w = u32::from_be_bytes(data.get(16..20)?.try_into().ok()?); + let h = u32::from_be_bytes(data.get(20..24)?.try_into().ok()?); + Some((w, h)) +} + +/// Intrinsic dimensions of a raster image resvg can embed, from its header alone, for formats +/// where decoding allocates the full image before any destination scaling (PNG, WebP). +/// Returns `Some(Some((w, h)))` when the format is PNG/WebP and its header parses cleanly, +/// `Some(None)` when the format is PNG/WebP (by MIME or magic bytes under `"text/plain"`) +/// and its header is malformed, or `None` for other formats. +/// A JPEG's size from its frame header (the first SOF marker), without decoding anything. +fn jpeg_dimensions(data: &[u8]) -> Option<(u32, u32)> { + let mut i = 2; + loop { + if *data.get(i)? != 0xFF { + return None; + } + let marker = *data.get(i + 1)?; + match marker { + 0xFF => i += 1, + // SOF0..SOF15, but for DHT (C4), JPG (C8) and DAC (CC). + 0xC0..=0xCF if !matches!(marker, 0xC4 | 0xC8 | 0xCC) => { + let h = u16::from_be_bytes([*data.get(i + 5)?, *data.get(i + 6)?]); + let w = u16::from_be_bytes([*data.get(i + 7)?, *data.get(i + 8)?]); + return Some((u32::from(w), u32::from(h))); + } + // Start of scan or end of image before any frame header. + 0xD8..=0xDA => return None, + 0x01 | 0xD0..=0xD7 => i += 2, + _ => i += 2 + usize::from(u16::from_be_bytes([*data.get(i + 2)?, *data.get(i + 3)?])), + } + } +} + +fn embedded_image_dimensions(mime: &str, data: &[u8]) -> Option> { + match mime { + "image/png" => Some(png_dimensions(data)), + "image/webp" | "image/gif" => Some(photocraft_codecs::probe_size(data).ok()), + "image/jpeg" | "image/jpg" => Some(jpeg_dimensions(data)), + "text/plain" => { + if data.starts_with(&[0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A]) { + Some(png_dimensions(data)) + } else if (data.get(0..4) == Some(b"RIFF") && data.get(8..12) == Some(b"WEBP")) || data.starts_with(b"GIF8") { + Some(photocraft_codecs::probe_size(data).ok()) + } else if data.starts_with(&[0xFF, 0xD8, 0xFF]) { + Some(jpeg_dimensions(data)) + } else { + None + } + } + _ => None, + } +} +/// Aggregate memory for retained fallback surfaces in the document's pixel format. +const MAX_PIXEL_BYTES: u64 = 1 << 30; +/// Estimated peak during raster conversion (retained surfaces + RGBA8 pixmap + f32 staging + tiles). +const MAX_PEAK_PIXEL_BYTES: u64 = 3 << 29; /// Past this many drawable elements the whole drawing opens as one pixel layer. const MAX_NODES: usize = 2_000; /// Nesting past which a subtree is rasterised instead of walked (bounded recursion). @@ -61,20 +134,200 @@ fn fonts() -> Arc { } #[cfg(not(target_arch = "wasm32"))] db.load_system_fonts(); + resolve_generic_families(&mut db, system_generic_family); Arc::new(db) }) .clone() } +/// Installed families that stand in for each generic one, best first: the metric-compatible +/// Liberation and Croscore fonts, then common Linux defaults, then the bundled craft fonts. +const SANS_SERIF: &[&str] = &["Liberation Sans", "Arimo", "Helvetica", "DejaVu Sans", "Noto Sans", photocraft_text::fonts::DEFAULT_FAMILY]; +const SERIF: &[&str] = &["Liberation Serif", "Tinos", "Times", "DejaVu Serif", "Noto Serif"]; +const MONOSPACE: &[&str] = &["Liberation Mono", "Cousine", "Courier", "DejaVu Sans Mono", "Noto Sans Mono", photocraft_text::fonts::MONO_FAMILY]; + +/// Points the generic families (`sans-serif`, `serif`, `monospace`, `cursive`, `fantasy`) at +/// fonts the database holds. fontdb names Windows fonts for them (Arial, Times New Roman, +/// Courier New…): on a system without those, every family of a usual +/// `font-family="Arial, Helvetica, sans-serif"` misses, and usvg drops the text although +/// hundreds of fonts are installed, where a browser sets it in a fallback font. A generic whose +/// font is installed is left alone. Otherwise the system's choice (`system`, Fontconfig on Linux) +/// wins when the database has it, then the first installed stand-in, then the sans-serif choice, +/// then any font at all. +fn resolve_generic_families(db: &mut usvg::fontdb::Database, system: impl Fn(&str) -> Option) { + use usvg::fontdb::Family; + let has = |db: &usvg::fontdb::Database, name: &str| db.faces().any(|f| f.families.iter().any(|(n, _)| n.eq_ignore_ascii_case(name))); + let Some(any) = db.faces().find_map(|f| f.families.first()).map(|(n, _)| n.clone()) else { return }; + let mut sans = None; + for (generic, css, stand_ins) in [ + (Family::SansSerif, "sans-serif", SANS_SERIF), + (Family::Serif, "serif", SERIF), + (Family::Monospace, "monospace", MONOSPACE), + (Family::Cursive, "cursive", &[][..]), + (Family::Fantasy, "fantasy", &[][..]), + ] { + let current = db.family_name(&generic).to_string(); + let pick = if has(db, ¤t) { + current + } else { + system(css) + .filter(|n| has(db, n)) + .or_else(|| stand_ins.iter().find(|n| has(db, n)).map(|n| n.to_string())) + .or_else(|| sans.clone()) + .unwrap_or_else(|| any.clone()) + }; + match generic { + Family::SansSerif => db.set_sans_serif_family(pick.clone()), + Family::Serif => db.set_serif_family(pick.clone()), + Family::Monospace => db.set_monospace_family(pick.clone()), + Family::Cursive => db.set_cursive_family(pick.clone()), + _ => db.set_fantasy_family(pick.clone()), + } + if generic == Family::SansSerif { + sans = Some(pick); + } + } +} + +/// The family Fontconfig sets a generic family in (`fc-match sans-serif`), as the rest of the +/// desktop does; `None` without Fontconfig. +#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))] +fn system_generic_family(generic: &str) -> Option { + let out = std::process::Command::new("fc-match").args(["--format=%{family[0]}", generic]).output().ok()?; + let name = String::from_utf8(out.stdout).ok()?; + Some(name.trim().to_string()).filter(|n| out.status.success() && !n.is_empty()) +} + +#[cfg(not(all(target_os = "linux", not(target_arch = "wasm32"))))] +fn system_generic_family(_generic: &str) -> Option { + None +} + +/// Most font file bytes [`in_memory`] keeps; past it a face stays on disk (slow, still right). +#[cfg(not(target_arch = "wasm32"))] +const MAX_CACHED_FONT_BYTES: usize = 256 << 20; + +/// `id`, or the same face loaded from memory when the database names a file for it. fontdb +/// re-reads a system font's whole file for every glyph usvg outlines (about a millisecond a +/// character with Times New Roman), so a short entity-expanded text could keep an import busy +/// for minutes. Each file is read once per process; the parse's own database copy then holds +/// every face of that file from memory. +#[cfg(not(target_arch = "wasm32"))] +fn in_memory(id: usvg::fontdb::ID, db: &mut Arc) -> usvg::fontdb::ID { + use std::collections::HashMap; + use std::path::PathBuf; + use std::sync::{Mutex, PoisonError}; + use usvg::fontdb::Source; + type Files = (HashMap>>, usize); + static FILES: OnceLock> = OnceLock::new(); + static LOADING: Mutex<()> = Mutex::new(()); + let Some((Source::File(path), index)) = db.face_source(id) else { return id }; + let files = || FILES.get_or_init(Mutex::default).lock().unwrap_or_else(PoisonError::into_inner); + let fits = |files: &Files, len: usize| files.1.saturating_add(len) <= MAX_CACHED_FONT_BYTES; + let cached = files().0.get(&path).cloned(); + // One miss at a time, so imports wanting the same file read it once: a second one waits + // here, then takes the bytes the first one kept. + let loading = cached.is_none().then(|| LOADING.lock().unwrap_or_else(PoisonError::into_inner)); + let cached = cached.or_else(|| files().0.get(&path).cloned()); + let bytes = match cached { + Some(b) => b, + None => { + // The size first: a file past the budget isn't read at all, on this lookup or the next. + let Ok(len) = std::fs::metadata(&path).map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)) else { return id }; + if !fits(&files(), len) { + return id; + } + let Ok(b) = std::fs::read(&path) else { return id }; + let b = Arc::new(b); + // The face has to load from the bytes before they're kept or the file's faces leave + // the database: a file replaced or cut short since fontdb indexed it stays on disk. + let mut probe = usvg::fontdb::Database::new(); + let ids = probe.load_font_source(Source::Binary(b.clone())); + if !ids.iter().any(|&n| probe.face_source(n).is_some_and(|(_, i)| i == index)) { + return id; + } + let mut files = files(); + if !fits(&files, b.len()) { + return id; + } + files.1 = files.1.saturating_add(b.len()); + files.0.insert(path.clone(), b.clone()); + b + } + }; + drop(loading); + let db = Arc::make_mut(db); + let same_file: Vec<_> = db.faces().filter(|f| matches!(&f.source, Source::File(p) if *p == path)).map(|f| f.id).collect(); + // Last first: fontdb's slots are reused last-freed-first, so the file's faces come back in + // their old places and order, and the fallback search (first face with the character) + // still finds the face it finds on disk. + for f in same_file.into_iter().rev() { + db.remove_face(f); + } + let loaded = db.load_font_source(Source::Binary(bytes)); + loaded.into_iter().find(|&n| db.face_source(n).is_some_and(|(_, i)| i == index)).unwrap_or(id) +} + +/// usvg's font choice, with the chosen faces read from memory ([`in_memory`]). +fn font_resolver() -> usvg::FontResolver<'static> { + #[cfg(not(target_arch = "wasm32"))] + { + let select = usvg::FontResolver::default_font_selector(); + let fallback = usvg::FontResolver::default_fallback_selector(); + usvg::FontResolver { + select_font: Box::new(move |font, db| select(font, db).map(|id| in_memory(id, db))), + select_fallback: Box::new(move |c, used, db| fallback(c, used, db).map(|id| in_memory(id, db))), + } + } + #[cfg(target_arch = "wasm32")] + usvg::FontResolver::default() +} + /// Parse options for an untrusted drawing. `` resolves only `data:` URLs: a file path /// or relative href is never read, so opening an SVG cannot pull local files (`/etc/...`, /// `~/.ssh/...`) into the document. Embedded SVG images get no `` resolution at all -/// (`usvg` already blanks it for sub-SVGs). -fn options(fontdb: Arc) -> usvg::Options<'static> { +/// (`usvg` already blanks it for sub-SVGs). An embedded SVG may nest `depth_left` levels: it is +/// parsed on top of the drawing's own recursion, so the two depths share [`MAX_XML_DEPTH`]. +fn options(fontdb: Arc, depth_left: usize) -> usvg::Options<'static> { + let default_resolver = usvg::ImageHrefResolver::default_data_resolver(); + let resolve_data: usvg::ImageHrefDataResolverFn<'static> = Box::new(move |mime, data, opts| { + // Reject embedded images whose decoded RGBA storage would exceed the budget before + // resvg (which allocates the full intrinsic image before any destination scaling) ever + // sees them. Formats without a cheap header probe fall through unchanged; their decode + // path is bounded elsewhere or accepted as-is, matching the previous behavior. + let probed = embedded_image_dimensions(mime, &data); + if let Some(probed) = probed { + let (w, h) = probed?; + let decoded = u64::from(w).saturating_mul(u64::from(h)).saturating_mul(4); + if decoded > MAX_EMBEDDED_IMAGE_BYTES { + return None; + } + } + // An embedded SVG (`image/svg+xml`, or `text/plain` that isn't one of the raster + // formats probed above) is parsed by the default resolver with none of `parse_with`'s + // checks, so they run here. It can itself be gzip-compressed (SVGZ-in-data-URL), and the + // default resolver's sub-SVG loader would decompress again with no byte limit of its + // own: hand it our bounded inflate's output instead, which no longer starts with the + // gzip magic. Nesting deeper than `depth_left` drops the image rather than overflowing + // the parser's stack. + if mime == "image/svg+xml" || (mime == "text/plain" && probed.is_none()) { + let (mime, xml) = match inflate(&data) { + Ok(std::borrow::Cow::Borrowed(_)) => (mime, Arc::clone(&data)), + Ok(std::borrow::Cow::Owned(xml)) if !xml.starts_with(&[0x1f, 0x8b]) => ("image/svg+xml", Arc::new(xml)), + _ => return None, + }; + if nesting_depth(&xml, depth_left) > depth_left { + return None; + } + return default_resolver(mime, xml, opts); + } + default_resolver(mime, data, opts) + }); usvg::Options { resources_dir: None, - image_href_resolver: usvg::ImageHrefResolver { resolve_data: usvg::ImageHrefResolver::default_data_resolver(), resolve_string: Box::new(|_, _| None) }, + image_href_resolver: usvg::ImageHrefResolver { resolve_data, resolve_string: Box::new(|_, _| None) }, fontdb, + font_resolver: font_resolver(), ..usvg::Options::default() } } @@ -99,14 +352,19 @@ fn parse(bytes: &[u8]) -> Result { /// [`parse`] with the font database `fonts` supplies (the tests hand in an empty one). fn parse_with(bytes: &[u8], fonts: fn() -> Arc) -> Result { let xml = inflate(bytes)?; - if nesting_depth(&xml, MAX_XML_DEPTH) > MAX_XML_DEPTH { + let depth = nesting_depth(&xml, MAX_XML_DEPTH); + if depth > MAX_XML_DEPTH { return Err(IoError::Svg(format!("elements are nested more than {MAX_XML_DEPTH} levels deep"))); } let has_text = xml.windows(4).any(|w| w == b"text"); let fontdb = if has_text { fonts() } else { Arc::new(usvg::fontdb::Database::new()) }; let fontless_text = has_text && fontdb.faces().next().is_none(); - let opt = options(fontdb); - let tree = usvg::Tree::from_data(&xml, &opt).map_err(|e| IoError::Svg(e.to_string()))?; + let opt = options(fontdb, MAX_XML_DEPTH.saturating_sub(depth)); + // Parse the already-bounded-and-decoded text with from_str, not from_data: from_data + // re-checks the gzip magic and would decompress a second time without our size limit if + // the inflated content happened to start with a nested gzip stream. + let text = std::str::from_utf8(&xml).map_err(|_| IoError::Svg("the SVG is not valid UTF-8".into()))?; + let tree = usvg::Tree::from_str(text, &opt).map_err(|e| IoError::Svg(e.to_string()))?; Ok(Parsed { tree, fontless_text }) } @@ -118,7 +376,7 @@ fn inflate(bytes: &[u8]) -> Result, IoError> { return Ok(std::borrow::Cow::Borrowed(bytes)); } let mut out = Vec::new(); - let mut reader = flate2::read::MultiGzDecoder::new(bytes).take(MAX_INFLATED_BYTES.saturating_add(1)); + let mut reader = flate2::bufread::MultiGzDecoder::new(bytes).take(MAX_INFLATED_BYTES.saturating_add(1)); reader.read_to_end(&mut out).map_err(|e| IoError::Svg(format!("the compressed SVG couldn't be inflated: {e}")))?; if u64::try_from(out.len()).unwrap_or(u64::MAX) > MAX_INFLATED_BYTES { return Err(IoError::Svg(format!("the compressed SVG inflates past {} MB", MAX_INFLATED_BYTES >> 20))); @@ -130,7 +388,7 @@ fn inflate(bytes: &[u8]) -> Result, IoError> { /// then `limit + 1`). Comments, CDATA sections, processing instructions and the DOCTYPE add /// nothing; quoted attribute values may hold `>` or `/>`. Malformed markup is only counted /// loosely: the parser rejects it afterwards. -fn nesting_depth(xml: &[u8], limit: usize) -> usize { +pub(crate) fn nesting_depth(xml: &[u8], limit: usize) -> usize { let after = |from: usize, pat: &[u8]| -> usize { xml.get(from..).and_then(|s| s.windows(pat.len()).position(|w| w == pat)).map_or(xml.len(), |p| from.saturating_add(p).saturating_add(pat.len())) }; @@ -212,6 +470,10 @@ pub fn import_svg_with_group_depth(name: &str, bytes: &[u8], max_group_depth: us } fn import_parsed(name: &str, parsed: Parsed, max_group_depth: usize) -> Result { + import_parsed_with_budget(name, parsed, max_group_depth, MAX_PIXEL_BYTES) +} + +fn import_parsed_with_budget(name: &str, parsed: Parsed, max_group_depth: usize, pixel_budget: u64) -> Result { let Parsed { tree, fontless_text } = parsed; let mut warnings = Vec::new(); if fontless_text { @@ -222,7 +484,8 @@ fn import_parsed(name: &str, parsed: Parsed, max_group_depth: usize) -> Result MAX_NODES { conv.warnings.push(format!("SVG: more than {MAX_NODES} elements; opened as one pixel layer instead of shapes")); let (w, h) = (cv.size.width, cv.size.height); @@ -244,14 +507,47 @@ fn import_parsed(name: &str, parsed: Parsed, max_group_depth: usize) -> Result Result { let tree = parse(bytes)?.tree; let cv = canvas(&tree, &mut Vec::new()); - let k = cv.scale * scale.clamp(1.0 / 64.0, 64.0); + let pixmap = render(&tree, cv.scale * scale.clamp(1.0 / 64.0, 64.0))?; + let rect = Rect::new(0, 0, pixmap.width() as i32, pixmap.height() as i32); + let px = pixmap.pixels().iter().map(|p| rgba(p.demultiply())).collect(); + Ok(photocraft_compose::Buffer { rect, px }) +} + +/// The whole drawing at one pixel per SVG unit, scaled down to fit [`MAX_SIDE`] a side and +/// [`MAX_RASTER_PIXELS`] in all: its width, height and straight-alpha RGBA8 pixels. Paste uses it +/// for a drawing copied as SVG markup. +pub fn rasterize_rgba8(bytes: &[u8]) -> Result<(u32, u32, Vec), IoError> { + let tree = parse(bytes)?.tree; + let cv = canvas(&tree, &mut Vec::new()); + let (w, h) = (f64::from(tree.size().width() * cv.scale), f64::from(tree.size().height() * cv.scale)); + // The largest k ≤ 1 with (w·k + 2)(h·k + 2) ≤ the limit: `render` rounds each side up, and + // f32 rounding can add one more pixel. + let max = MAX_RASTER_PIXELS as f64; + let fit = if (w + 2.0) * (h + 2.0) <= max { + 1.0 + } else { + let (a, b) = (w * h, 2.0 * (w + h)); + (-b + (b * b - 4.0 * a * (4.0 - max)).sqrt()) / (2.0 * a) + }; + let pixmap = render(&tree, cv.scale * fit as f32)?; + let px = pixmap + .pixels() + .iter() + .flat_map(|p| { + let c = p.demultiply(); + [c.red(), c.green(), c.blue(), c.alpha()] + }) + .collect(); + Ok((pixmap.width(), pixmap.height(), px)) +} + +/// `tree` drawn at `k` pixels per SVG unit on a transparent pixmap whose sides round up. +fn render(tree: &usvg::Tree, k: f32) -> Result { let side = |v: f32| ((v * k).ceil() as u32).max(1); let (w, h) = (side(tree.size().width()), side(tree.size().height())); let mut pixmap = pixmap(w, h).ok_or_else(|| IoError::Svg(format!("{w} × {h} px is too large to rasterise")))?; - resvg::render(&tree, tiny_skia::Transform::from_scale(k, k), &mut pixmap.as_mut()); - let rect = Rect::new(0, 0, w as i32, h as i32); - let px = pixmap.pixels().iter().map(|p| rgba(p.demultiply())).collect(); - Ok(photocraft_compose::Buffer { rect, px }) + resvg::render(tree, tiny_skia::Transform::from_scale(k, k), &mut pixmap.as_mut()); + Ok(pixmap) } /// A transparent pixmap, or `None` past [`MAX_RASTER_PIXELS`]. @@ -311,6 +607,10 @@ struct Conv { noted: Vec<&'static str>, /// Unnamed shapes, groups, images and texts so far, for "Shape 3"-style names. counts: [usize; 4], + /// Remaining budget for retained rasterised surfaces, in actual sample bytes. + pixel_budget: u64, + /// Successfully retained raster surface memory; rejected nodes are never counted. + retained_pixel_bytes: u64, } impl Conv { @@ -321,6 +621,23 @@ impl Conv { } } + /// Check both retained and transient memory before rendering; charge only retained output. + fn can_retain_pixels(&mut self, pixels: u64) -> bool { + let bytes = pixels.saturating_mul(self.fmt.bytes_per_pixel() as u64); + if bytes > self.pixel_budget { + self.note("pixel-budget", "SVG: some rasterised elements were omitted because they exceeded the remaining pixel memory budget".into()); + return false; + } + // surface() converts the RGBA8 pixmap into f32 scratch space before writing + // sample-format tiles. All three buffers briefly coexist with retained layers. + let temporary = pixels.saturating_mul(4 + self.fmt.channels() as u64 * 4 + self.fmt.bytes_per_pixel() as u64); + if self.retained_pixel_bytes.saturating_add(temporary) > MAX_PEAK_PIXEL_BYTES { + self.note("pixel-peak", "SVG: some rasterised elements were omitted to bound temporary conversion memory".into()); + return false; + } + true + } + /// The element's `id`, or a numbered name by kind (0 shape, 1 group, 2 image, 3 text). fn name(&mut self, id: &str, kind: usize) -> String { if !id.is_empty() { @@ -337,6 +654,11 @@ impl Conv { /// `nodes` in paint order (first = bottom), as layers in the same order. `parent` is the /// absolute transform of the group holding them (what resvg needs to render one of them /// alone, since a node's own data is in its parent's space). + /// + /// Transforms are composed from `parent` down rather than read from `abs_transform()`: the + /// glyph outlines usvg builds for a text element (`Text::flattened`) keep identity absolute + /// transforms, so text placed by its own or an ancestor's `transform` (Illustrator writes + /// `matrix(1 0 0 1 x y)` on every text element) would lose it (#4806). Elsewhere the two agree. fn convert_children(&mut self, nodes: &[usvg::Node], depth: usize, rec: usize, parent: tiny_skia::Transform) -> Vec { let mut out = Vec::new(); for node in nodes { @@ -350,7 +672,7 @@ impl Conv { usvg::Node::Group(g) => { // A clip-path of one plain path becomes the group's vector mask (#4266); others // are rasterised with the group. - let clip = g.clip_path().and_then(|cp| self.clip_mask(g, cp)); + let clip = g.clip_path().and_then(|cp| self.clip_mask(parent.pre_concat(g.transform()), cp)); let dropped: [(bool, &'static str); 3] = [(!g.filters().is_empty(), "filter"), (g.clip_path().is_some() && clip.is_none(), "clip-path"), (g.mask().is_some(), "mask")]; let too_deep = self.max_group_depth.is_some_and(|limit| depth >= limit) || depth >= MAX_GROUP_DEPTH || rec >= MAX_RECURSION; @@ -370,7 +692,8 @@ impl Conv { // A group that only carries a transform (baked into its paths by usvg) or that // usvg inserted for its own bookkeeping adds nothing to the layer tree. let plain = clip.is_none() && g.id().is_empty() && g.opacity().get() >= 1.0 && g.blend_mode() == usvg::BlendMode::Normal; - let children = self.convert_children(g.children(), if plain { depth } else { depth + 1 }, rec + 1, g.abs_transform()); + let abs = parent.pre_concat(g.transform()); + let children = self.convert_children(g.children(), if plain { depth } else { depth + 1 }, rec + 1, abs); if children.is_empty() { return; } @@ -391,7 +714,7 @@ impl Conv { } return; } - let mut l = Layer::new(self.name(g.id(), 1), LayerContent::Group(Group { children, expanded: false, artboard: None })); + let mut l = Layer::new(self.name(g.id(), 1), LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None })); l.opacity = g.opacity().get(); l.blend = blend(g.blend_mode()); l.vector_mask = clip; @@ -404,7 +727,9 @@ impl Conv { out.extend(self.raster_layer(node, name, parent)); } usvg::Node::Text(t) => { - let children = self.convert_children(t.flattened().children(), depth + 1, rec + 1, t.flattened().abs_transform()); + // The outlines are in the text element's own space. + let abs = t.abs_transform().pre_concat(t.flattened().transform()); + let children = self.convert_children(t.flattened().children(), depth + 1, rec + 1, abs); if children.is_empty() { self.note("text", "SVG: text needs a font the drawing can use; none matched, so it was dropped".to_string()); return; @@ -416,15 +741,15 @@ impl Conv { } else { t.id().to_string() }; - out.push(Layer::new(name, LayerContent::Group(Group { children, expanded: false, artboard: None }))); + out.push(Layer::new(name, LayerContent::Group(Group { children, expanded: false, artboard: None, frame_stroke: None }))); } } } - /// The vector mask for group `g` clipped by `cp`, when the clip is one visible path with no - /// clip of its own: what resvg draws for the clip (the group's transform, then the clip's, - /// then the path), in canvas pixels, with the clip rule as its fill rule. - fn clip_mask(&self, g: &usvg::Group, cp: &usvg::ClipPath) -> Option { + /// The vector mask for a group clipped by `cp`, when the clip is one visible path with no + /// clip of its own: what resvg draws for the clip (the group's absolute transform `abs`, then + /// the clip's, then the path), in canvas pixels, with the clip rule as its fill rule. + fn clip_mask(&self, abs: tiny_skia::Transform, cp: &usvg::ClipPath) -> Option { if cp.clip_path().is_some() { return None; } @@ -432,7 +757,7 @@ impl Conv { if !p.is_visible() { return None; } - let ts = g.abs_transform().pre_concat(cp.transform()).post_concat(self.global); + let ts = abs.pre_concat(cp.transform()).post_concat(self.global); let subpaths = knots(&(*p.data()).clone().transform(ts)?); if subpaths.is_empty() { return None; @@ -455,7 +780,8 @@ impl Conv { let name = self.name(p.id(), 0); return self.raster_layer(node, name, parent); } - let ts = p.abs_transform().post_concat(self.global); + // A path has no transform of its own: its data is in its parent's space. + let ts = parent.post_concat(self.global); let data = (*p.data()).clone().transform(ts)?; let subpaths = knots(&data); if subpaths.is_empty() { @@ -496,7 +822,25 @@ impl Conv { _ => FillRule::NonZero, }; let mut sh = ShapeLayer { path: Path { subpaths, fill_rule, inverted: false }, fill, stroke, ..Default::default() }; - sh.cache = Some(photocraft_vector::render_shape(&sh, self.fmt, self.canvas)); + // The pixel cache is optional, so it is charged like any other retained raster and left + // out (the shape renders from its path) once the budget is spent: compact geometry can + // cover the whole canvas in every one of thousands of shapes. + let b = data.bounds(); + let reach = sh.stroke.as_ref().map_or(0.0, |s| s.width); + let area = Rect::new( + (b.left() - reach).floor() as i32, + (b.top() - reach).floor() as i32, + (b.right() + reach).ceil() as i32, + (b.bottom() + reach).ceil() as i32, + ) + .intersect(&self.canvas); + let pixels = u64::from(area.width()) * u64::from(area.height()); + if self.can_retain_pixels(pixels) { + sh.cache = Some(photocraft_vector::render_shape(&sh, self.fmt, self.canvas)); + let bytes = pixels * self.fmt.bytes_per_pixel() as u64; + self.pixel_budget -= bytes; + self.retained_pixel_bytes += bytes; + } Some(Layer::new(self.name(p.id(), 0), LayerContent::Shape(sh))) } @@ -542,7 +886,14 @@ impl Conv { /// The node rendered by resvg into a pixel layer over its bounding box, clipped to the canvas. /// `parent` is the absolute transform of the node's parent group. fn raster_layer(&mut self, node: &usvg::Node, name: String, parent: tiny_skia::Transform) -> Option { - let bb = node.abs_layer_bounding_box()?; + // Where the node lands, from its box in its parent's space (as usvg computes its absolute + // box, but through `parent`, which is right inside text too). + let bb = match node { + usvg::Node::Group(g) => g.layer_bounding_box().transform(parent.pre_concat(g.transform()))?, + _ => node.bounding_box().to_non_zero_rect()?.transform(parent)?, + }; + // What resvg shifts the node by before drawing it. + let shift = node.abs_layer_bounding_box()?; let k = self.global.sx; let x0 = ((bb.x() * k).floor() as i32).max(self.canvas.x0); let y0 = ((bb.y() * k).floor() as i32).max(self.canvas.y0); @@ -551,17 +902,31 @@ impl Conv { if x1 <= x0 || y1 <= y0 { return None; } - let Some(mut pixmap) = pixmap((x1 - x0) as u32, (y1 - y0) as u32) else { + let (w, h) = ((x1 - x0) as u32, (y1 - y0) as u32); + let pixels = u64::from(w) * u64::from(h); + // Preflight the pixmap limit first, without charging the shared allowance. + if pixels > MAX_RASTER_PIXELS { self.note("raster-size", "SVG: an element too large to rasterise was dropped".to_string()); return None; + } + if !self.can_retain_pixels(pixels) { + return None; + } + let Some(mut pixmap) = pixmap(w, h) else { + self.note("raster-alloc", "SVG: a rasterised element could not be allocated".to_string()); + return None; }; // resvg renders the node in its parent's space after shifting its absolute bounding box // to the origin: undo that shift, apply the parent's transform, then scale to the canvas // and shift so the pixmap starts at (x0, y0). let canvas = tiny_skia::Transform::from_row(k, 0.0, 0.0, k, -(x0 as f32), -(y0 as f32)); - let ts = canvas.pre_concat(parent).pre_translate(bb.x(), bb.y()); + let ts = canvas.pre_concat(parent).pre_translate(shift.x(), shift.y()); resvg::render_node(node, ts, &mut pixmap.as_mut())?; - Some(Layer::new(name, LayerContent::Raster(surface(&pixmap, Rect::new(x0, y0, x1, y1), self.fmt)))) + let layer = Layer::new(name, LayerContent::Raster(surface(&pixmap, Rect::new(x0, y0, x1, y1), self.fmt))); + let bytes = pixels * self.fmt.bytes_per_pixel() as u64; + self.pixel_budget -= bytes; + self.retained_pixel_bytes += bytes; + Some(layer) } } @@ -664,116 +1029,4 @@ fn knots(path: &tiny_skia::Path) -> Vec { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn sniffing_accepts_svg_and_rejects_other_xml() { - assert!(is_svg(b"")); - assert!(is_svg("\u{feff}\n\n".as_bytes())); - assert!(is_svg(b"")); - assert!(!is_svg(b"")); - assert!(!is_svg(b"\x89PNG\r\n")); - assert!(!is_svg(b"")); - } - - #[test] - fn quadratics_become_cubics_and_closing_merges_the_start_knot() { - let mut pb = tiny_skia::PathBuilder::new(); - pb.move_to(0.0, 0.0); - pb.quad_to(5.0, 10.0, 10.0, 0.0); - pb.line_to(0.0, 0.0); - pb.close(); - pb.move_to(20.0, 20.0); - pb.line_to(30.0, 20.0); - let path = pb.finish().unwrap(); - let sp = knots(&path); - assert_eq!(sp.len(), 2); - assert!(sp[0].closed && !sp[1].closed); - // The explicit line back to the start was merged into the first knot. - assert_eq!(sp[0].knots.len(), 2); - let (a, b) = (&sp[0].knots[0], &sp[0].knots[1]); - assert!((a.out_ctrl.x - 10.0 / 3.0).abs() < 1e-9 && (a.out_ctrl.y - 20.0 / 3.0).abs() < 1e-9); - assert!((b.in_ctrl.x - 20.0 / 3.0).abs() < 1e-9 && (b.in_ctrl.y - 20.0 / 3.0).abs() < 1e-9); - assert_eq!(b.anchor, Point::new(10.0, 0.0)); - assert_eq!(sp[1].knots.len(), 2); - } - - #[test] - fn text_without_any_font_is_reported_not_silently_dropped() { - // No font at all (a bare CI image): usvg drops the while parsing, so nothing - // reaches the conversion; the reader still has to say why the text is missing. - fn no_fonts() -> Arc { - Arc::new(usvg::fontdb::Database::new()) - } - let svg = b"II"; - let parsed = parse_with(svg, no_fonts).unwrap(); - assert!(parsed.fontless_text); - let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); - assert!(r.document.layers.is_empty()); - assert!(r.warnings.iter().any(|w| w.contains("font")), "{:?}", r.warnings); - // A drawing without text never loads or misses fonts. - let parsed = - parse_with(b"", no_fonts).unwrap(); - assert!(!parsed.fontless_text); - let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); - assert!(r.warnings.iter().all(|w| !w.contains("font")), "{:?}", r.warnings); - } - - /// #4266: a group clipped by one path (the PhotoCraft icon's rounded square) stays editable - /// shapes with the clip as its vector mask, and looks as resvg draws the drawing; a clip of - /// two shapes is still rasterised, with the warning. - #[test] - fn a_single_path_clip_becomes_the_groups_vector_mask() { - let svg = br##" - - - - - "##; - let r = import_svg("t", svg).unwrap(); - assert!(r.warnings.iter().all(|w| !w.contains("clip-path")), "{:?}", r.warnings); - let [group] = r.document.layers.as_slice() else { panic!("one group: {:?}", r.document.layers.len()) }; - let LayerContent::Group(g) = &group.content else { panic!("a group") }; - assert_eq!(g.children.len(), 2); - assert!(g.children.iter().all(|c| matches!(c.content, LayerContent::Shape(_))), "editable shapes"); - let mask = group.vector_mask.as_ref().expect("the clip as a vector mask"); - let xs: Vec = mask.path.subpaths.iter().flat_map(|s| s.knots.iter().map(|k| k.anchor.x)).collect(); - let (lo, hi) = (xs.iter().copied().fold(f64::MAX, f64::min), xs.iter().copied().fold(f64::MIN, f64::max)); - assert!((lo - 12.0).abs() < 1e-3 && (hi - 52.0).abs() < 1e-3, "in the group's space: {lo}..{hi}"); - // Pixel for pixel what resvg draws, but for anti-aliasing along the edges. - let ours = photocraft_compose::flatten(&r.document).to_rgba8().pixels; - let tree = parse(svg).unwrap().tree; - let mut pm = tiny_skia::Pixmap::new(64, 64).unwrap(); - resvg::render(&tree, tiny_skia::Transform::default(), &mut pm.as_mut()); - let theirs: Vec = pm - .pixels() - .iter() - .flat_map(|p| { - let c = p.demultiply(); - [c.red(), c.green(), c.blue(), c.alpha()] - }) - .collect(); - let off = ours.chunks(4).zip(theirs.chunks(4)).filter(|(a, b)| a.iter().zip(*b).any(|(x, y)| x.abs_diff(*y) > 48)).count(); - assert!(off < 64, "{off} pixels differ"); - // The report's file: PhotoCraft's own icon opens without the clip-path warning. - let icon = import_svg("photocraft.svg", include_bytes!("../../../assets/app-icon/photocraft.svg")).unwrap(); - assert!(icon.warnings.iter().all(|w| !w.contains("clip-path")), "{:?}", icon.warnings); - assert!(icon.document.layers.iter().any(|l| l.vector_mask.is_some()), "its rounded square is a vector mask"); - // Two shapes in the clip: rasterised, and said so. - let two = br##" - - "##; - let r = import_svg("t", two).unwrap(); - assert!(r.warnings.iter().any(|w| w.contains("clip-path")), "{:?}", r.warnings); - } - - #[test] - fn huge_drawings_scale_to_the_side_limit() { - let tree = parse(b"").unwrap().tree; - let mut w = Vec::new(); - let cv = canvas(&tree, &mut w); - assert_eq!((cv.size.width, cv.size.height), (MAX_SIDE, MAX_SIDE / 2)); - assert_eq!(w.len(), 1); - } -} +mod tests; diff --git a/crates/io/src/svg/tests.rs b/crates/io/src/svg/tests.rs new file mode 100644 index 0000000000..426962fa9c --- /dev/null +++ b/crates/io/src/svg/tests.rs @@ -0,0 +1,499 @@ +use super::*; + +#[test] +fn sniffing_accepts_svg_and_rejects_other_xml() { + assert!(is_svg(b"")); + assert!(is_svg("\u{feff}\n\n".as_bytes())); + assert!(is_svg(b"")); + assert!(!is_svg(b"")); + assert!(!is_svg(b"\x89PNG\r\n")); + assert!(!is_svg(b"")); +} + +#[test] +fn quadratics_become_cubics_and_closing_merges_the_start_knot() { + let mut pb = tiny_skia::PathBuilder::new(); + pb.move_to(0.0, 0.0); + pb.quad_to(5.0, 10.0, 10.0, 0.0); + pb.line_to(0.0, 0.0); + pb.close(); + pb.move_to(20.0, 20.0); + pb.line_to(30.0, 20.0); + let path = pb.finish().unwrap(); + let sp = knots(&path); + assert_eq!(sp.len(), 2); + assert!(sp[0].closed && !sp[1].closed); + // The explicit line back to the start was merged into the first knot. + assert_eq!(sp[0].knots.len(), 2); + let (a, b) = (&sp[0].knots[0], &sp[0].knots[1]); + assert!((a.out_ctrl.x - 10.0 / 3.0).abs() < 1e-9 && (a.out_ctrl.y - 20.0 / 3.0).abs() < 1e-9); + assert!((b.in_ctrl.x - 20.0 / 3.0).abs() < 1e-9 && (b.in_ctrl.y - 20.0 / 3.0).abs() < 1e-9); + assert_eq!(b.anchor, Point::new(10.0, 0.0)); + assert_eq!(sp[1].knots.len(), 2); +} + +#[test] +fn text_without_any_font_is_reported_not_silently_dropped() { + // No font at all (a bare CI image): usvg drops the while parsing, so nothing + // reaches the conversion; the reader still has to say why the text is missing. + fn no_fonts() -> Arc { + Arc::new(usvg::fontdb::Database::new()) + } + let svg = b"II"; + let parsed = parse_with(svg, no_fonts).unwrap(); + assert!(parsed.fontless_text); + let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); + assert!(r.document.layers.is_empty()); + assert!(r.warnings.iter().any(|w| w.contains("font")), "{:?}", r.warnings); + // A drawing without text never loads or misses fonts. + let parsed = + parse_with(b"", no_fonts).unwrap(); + assert!(!parsed.fontless_text); + let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); + assert!(r.warnings.iter().all(|w| !w.contains("font")), "{:?}", r.warnings); +} + +/// #4806: text placed by a `transform` (Illustrator writes `matrix(1 0 0 1 x y)` on every text +/// element), on the text or on a group around it, lands where resvg draws it. usvg leaves the +/// glyph outlines' absolute transforms at identity, so reading them dropped the transform: the +/// text went off the canvas or kept its untransformed place and size. +/// +/// Shaped with the bundled Inter alone, never a system font: on a machine without fonts (a +/// bare CI image or VM) the test would otherwise have nothing to check, and the drawing and +/// its reference come from the same parse, so both use that face. +#[test] +fn transformed_text_lands_where_resvg_draws_it() { + fn inter() -> Arc { + let mut db = usvg::fontdb::Database::new(); + db.load_font_data(photocraft_text::fonts::INTER_REGULAR.to_vec()); + Arc::new(db) + } + assert!(inter().faces().next().is_some(), "the bundled Inter loads"); + let font = r#"font-size="24" font-family="Inter" fill="black""#; + let cases = [ + format!(r#"IHI"#), + format!(r#"IHI"#), + format!(r#"IHI"#), + format!(r#"IHI"#), + format!(r#"IHI"#), + format!(r#"IHI"#), + ]; + for c in cases { + let svg = format!(r#"{c}"#); + let parsed = parse_with(svg.as_bytes(), inter).unwrap(); + assert!(!parsed.fontless_text, "{c}"); + let mut reference = tiny_skia::Pixmap::new(120, 60).unwrap(); + resvg::render(&parsed.tree, tiny_skia::Transform::default(), &mut reference.as_mut()); + let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); + let [layer] = r.document.layers.as_slice() else { panic!("one text layer, got {}: {c}", r.document.layers.len()) }; + assert!(matches!(layer.content, LayerContent::Group(_)), "text stays outline shapes: {c}"); + let imported = photocraft_compose::flatten(&r.document); + let (mut both, mut either) = (0usize, 0usize); + let (mut a_box, mut b_box) = ([i32::MAX, i32::MAX, i32::MIN, i32::MIN], [i32::MAX, i32::MAX, i32::MIN, i32::MIN]); + let grow = |bx: &mut [i32; 4], x: i32, y: i32| *bx = [bx[0].min(x), bx[1].min(y), bx[2].max(x), bx[3].max(y)]; + for (i, px) in reference.pixels().iter().enumerate() { + let (x, y) = ((i % 120) as i32, (i / 120) as i32); + let (a, b) = (imported.get(x, y)[3] > 0.5, px.alpha() > 127); + if a { + grow(&mut a_box, x, y); + } + if b { + grow(&mut b_box, x, y); + } + both += usize::from(a && b); + either += usize::from(a || b); + } + assert!(either > 50, "resvg draws the text on the canvas: {c}"); + // Same place and size to the pixel; the outlines are filled by a different rasteriser, + // so the edges of thin stems may differ by half a pixel. + assert!(a_box.iter().zip(b_box).all(|(a, b)| (a - b).abs() <= 1), "imported {a_box:?}, resvg {b_box:?}: {c}"); + assert!(both * 10 >= either * 7, "imported glyphs cover what resvg draws ({both} of {either} px): {c}"); + } +} + +/// #4266: a group clipped by one path (the PhotoCraft icon's rounded square) stays editable +/// shapes with the clip as its vector mask, and looks as resvg draws the drawing; a clip of +/// two shapes is still rasterised, with the warning. +#[test] +fn a_single_path_clip_becomes_the_groups_vector_mask() { + let svg = br##" + + + + + "##; + let r = import_svg("t", svg).unwrap(); + assert!(r.warnings.iter().all(|w| !w.contains("clip-path")), "{:?}", r.warnings); + let [group] = r.document.layers.as_slice() else { panic!("one group: {:?}", r.document.layers.len()) }; + let LayerContent::Group(g) = &group.content else { panic!("a group") }; + assert_eq!(g.children.len(), 2); + assert!(g.children.iter().all(|c| matches!(c.content, LayerContent::Shape(_))), "editable shapes"); + let mask = group.vector_mask.as_ref().expect("the clip as a vector mask"); + let xs: Vec = mask.path.subpaths.iter().flat_map(|s| s.knots.iter().map(|k| k.anchor.x)).collect(); + let (lo, hi) = (xs.iter().copied().fold(f64::MAX, f64::min), xs.iter().copied().fold(f64::MIN, f64::max)); + assert!((lo - 12.0).abs() < 1e-3 && (hi - 52.0).abs() < 1e-3, "in the group's space: {lo}..{hi}"); + // Pixel for pixel what resvg draws, but for anti-aliasing along the edges. + let ours = photocraft_compose::flatten(&r.document).to_rgba8().pixels; + let tree = parse(svg).unwrap().tree; + let mut pm = tiny_skia::Pixmap::new(64, 64).unwrap(); + resvg::render(&tree, tiny_skia::Transform::default(), &mut pm.as_mut()); + let theirs: Vec = pm + .pixels() + .iter() + .flat_map(|p| { + let c = p.demultiply(); + [c.red(), c.green(), c.blue(), c.alpha()] + }) + .collect(); + let off = ours.chunks(4).zip(theirs.chunks(4)).filter(|(a, b)| a.iter().zip(*b).any(|(x, y)| x.abs_diff(*y) > 48)).count(); + assert!(off < 64, "{off} pixels differ"); + // The report's file: PhotoCraft's own icon opens without the clip-path warning. + let icon = import_svg("photocraft.svg", include_bytes!("../../../../assets/app-icon/photocraft.svg")).unwrap(); + assert!(icon.warnings.iter().all(|w| !w.contains("clip-path")), "{:?}", icon.warnings); + assert!(icon.document.layers.iter().any(|l| l.vector_mask.is_some()), "its rounded square is a vector mask"); + // Two shapes in the clip: rasterised, and said so. + let two = br##" + + "##; + let r = import_svg("t", two).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("clip-path")), "{:?}", r.warnings); +} + +/// A database with the bundled Inter alone, generics resolved without Fontconfig: none of +/// fontdb's Windows defaults (Arial, Times New Roman, Courier New) is in it. +fn inter_only() -> Arc { + let mut db = usvg::fontdb::Database::new(); + db.load_font_data(include_bytes!("../../../../assets/fonts/Inter-Regular.ttf").to_vec()); + resolve_generic_families(&mut db, |_| None); + Arc::new(db) +} + +/// Text asking for fonts that aren't installed (`Arial, Helvetica, sans-serif` on a Linux +/// without them) is set in an installed font, as a browser does, instead of being dropped. +#[test] +fn text_in_missing_fonts_falls_back_to_an_installed_one() { + for family in ["Arial, Helvetica, sans-serif", "Times New Roman", "Courier New, monospace", "cursive", "fantasy"] { + let svg = format!( + "II" + ); + let parsed = parse_with(svg.as_bytes(), inter_only).unwrap(); + assert!(!parsed.fontless_text); + let r = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); + assert_eq!(r.document.layers.first().map(|l| l.name.as_str()), Some("II"), "{family}: {:?}", r.warnings); + } +} + +/// #9406: a font installed on disk (how the system fonts load) is read once, not once per +/// glyph: fontdb re-reads a file source's whole font on every glyph it outlines, about a +/// millisecond a character. The face text is set in comes from memory, and the outlines +/// match the same font loaded from memory in the first place. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn text_in_a_font_on_disk_is_set_from_memory() { + fn inter_on_disk() -> Arc { + let mut db = usvg::fontdb::Database::new(); + let _ = db.load_font_file(concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts/Inter-Regular.ttf")); + Arc::new(db) + } + assert!(inter_on_disk().faces().all(|f| matches!(f.source, usvg::fontdb::Source::File(_)))); + let svg = + b"IHI"; + let parsed = parse_with(svg, inter_on_disk).unwrap(); + let faces: Vec<_> = parsed.tree.fontdb().faces().map(|f| matches!(f.source, usvg::fontdb::Source::Binary(_))).collect(); + assert_eq!(faces, [true], "the face text was set in is held in memory"); + let on_disk = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap(); + let in_memory = import_parsed("t", parse_with(svg, inter_only).unwrap(), MAX_GROUP_DEPTH).unwrap(); + assert_eq!(on_disk.document.layers.first().map(|l| l.name.as_str()), Some("IHI")); + let outlines = |r: &ImportResult| { + let groups = r.document.layers.iter().filter_map(|l| if let LayerContent::Group(g) = &l.content { Some(g) } else { None }); + groups.flat_map(|g| &g.children).filter_map(|l| if let LayerContent::Shape(s) = &l.content { Some(s.path.clone()) } else { None }).collect::>() + }; + assert!(outlines(&on_disk).iter().any(|p| !p.subpaths.is_empty()), "the text has outlines"); + assert_eq!(outlines(&on_disk), outlines(&in_memory)); +} + +/// A font file that no longer loads (replaced or cut short since fontdb indexed it) keeps its +/// faces in the database and isn't remembered: once the file is whole again, text is set +/// from it. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_font_file_gone_bad_since_indexing_is_not_cached() { + use std::sync::atomic::{AtomicBool, Ordering}; + static BAD: AtomicBool = AtomicBool::new(true); + fn indexed_then_overwritten() -> Arc { + let path = std::env::temp_dir().join(format!("photocraft-svg-test-bad-{}.ttf", std::process::id())); + std::fs::write(&path, include_bytes!("../../../../assets/fonts/Inter-Regular.ttf")).unwrap(); + let mut db = usvg::fontdb::Database::new(); + db.load_font_file(&path).unwrap(); + if BAD.load(Ordering::SeqCst) { + std::fs::write(&path, b"not a font any more").unwrap(); + } + Arc::new(db) + } + let svg = b"IHI"; + let parsed = parse_with(svg, indexed_then_overwritten).unwrap(); + let sources: Vec<_> = parsed.tree.fontdb().faces().map(|f| matches!(f.source, usvg::fontdb::Source::File(_))).collect(); + assert_eq!(sources, [true], "the face stays in the database, on disk"); + BAD.store(false, Ordering::SeqCst); + let parsed = parse_with(svg, indexed_then_overwritten).unwrap(); + let sources: Vec<_> = parsed.tree.fontdb().faces().map(|f| matches!(f.source, usvg::fontdb::Source::Binary(_))).collect(); + assert_eq!(sources, [true], "the whole file is read into memory now"); + let layers = import_parsed("t", parsed, MAX_GROUP_DEPTH).unwrap().document.layers; + assert_eq!(layers.first().map(|l| l.name.as_str()), Some("IHI")); + assert!( + layers.iter().any(|l| matches!(&l.content, LayerContent::Group(g) if g.children.iter().any(|c| matches!(&c.content, LayerContent::Shape(s) if !s.path.subpaths.is_empty())))), + "the text has outlines" + ); +} + +/// Imports that miss the cache for the same file at once read it once: every one sets its +/// text from the same bytes, not a copy of its own next to the kept one. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn imports_missing_the_same_font_at_once_share_one_read() { + use std::sync::atomic::{AtomicUsize, Ordering}; + static ROUND: AtomicUsize = AtomicUsize::new(0); + fn fresh_file() -> Arc { + let round = ROUND.load(Ordering::SeqCst); + let path = std::env::temp_dir().join(format!("photocraft-svg-test-shared-{}-{round}.ttf", std::process::id())); + let mut db = usvg::fontdb::Database::new(); + db.load_font_file(&path).unwrap(); + Arc::new(db) + } + let svg = b"IHI"; + for round in 0..5 { + ROUND.store(round, Ordering::SeqCst); + let path = std::env::temp_dir().join(format!("photocraft-svg-test-shared-{}-{round}.ttf", std::process::id())); + std::fs::write(&path, include_bytes!("../../../../assets/fonts/Inter-Regular.ttf")).unwrap(); + let start = Arc::new(std::sync::Barrier::new(8)); + let threads: Vec<_> = (0..8) + .map(|_| { + let start = start.clone(); + std::thread::spawn(move || { + start.wait(); + let parsed = parse_with(svg, fresh_file).unwrap(); + let bytes: Vec<_> = parsed + .tree + .fontdb() + .faces() + .filter_map(|f| if let usvg::fontdb::Source::Binary(b) = &f.source { Some(Arc::as_ptr(b).cast::() as usize) } else { None }) + .collect(); + bytes + }) + }) + .collect(); + let bytes: Vec<_> = threads.into_iter().flat_map(|t| t.join().unwrap()).collect(); + assert_eq!(bytes.len(), 8, "every import set its text from memory"); + assert!(bytes.iter().all(|&b| b == bytes[0]), "round {round}: {bytes:x?}"); + let _ = std::fs::remove_file(&path); + } +} + +/// #9406: moving a font collection into memory keeps its faces in their order, so the +/// fallback search for a later text still finds the face it found on disk first. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_collection_in_memory_keeps_its_face_order() { + fn collection_on_disk() -> Arc { + // A two-face .ttc of the bundled Inter Regular and SemiBold: the `ttcf` header, then + // each font with its table offsets moved by where it starts. + let fonts: [&[u8]; 2] = [include_bytes!("../../../../assets/fonts/Inter-Regular.ttf"), include_bytes!("../../../../assets/fonts/Inter-SemiBold.ttf")]; + let mut ttc = b"ttcf������".to_vec(); + let mut start = 20; + let mut body = Vec::new(); + for f in fonts { + ttc.extend_from_slice(&u32::try_from(start).unwrap().to_be_bytes()); + let mut font = f.to_vec(); + for t in 0..usize::from(u16::from_be_bytes([font[4], font[5]])) { + let at = 12 + t * 16 + 8; + let off = u32::from_be_bytes(font[at..at + 4].try_into().unwrap()) + u32::try_from(start).unwrap(); + font[at..at + 4].copy_from_slice(&off.to_be_bytes()); + } + font.resize(font.len().next_multiple_of(4), 0); + start += font.len(); + body.extend(font); + } + ttc.extend(body); + let path = std::env::temp_dir().join(format!("photocraft-svg-test-{}.ttc", std::process::id())); + std::fs::write(&path, ttc).unwrap(); + let mut db = usvg::fontdb::Database::new(); + db.load_font_file(&path).unwrap(); + Arc::new(db) + } + let order = |db: &usvg::fontdb::Database| db.faces().map(|f| (f.index, f.weight.0)).collect::>(); + assert_eq!(order(&collection_on_disk()), [(0, 400), (1, 600)]); + let svg = b"IHI"; + let parsed = parse_with(svg, collection_on_disk).unwrap(); + let db = parsed.tree.fontdb(); + assert!(db.faces().all(|f| matches!(f.source, usvg::fontdb::Source::Binary(_))), "both faces of the file are in memory"); + assert_eq!(order(db), [(0, 400), (1, 600)]); +} + +#[test] +fn generic_families_point_at_installed_fonts() { + use usvg::fontdb::Family; + let db = inter_only(); + for g in [Family::SansSerif, Family::Serif, Family::Monospace, Family::Cursive, Family::Fantasy] { + assert_eq!(db.family_name(&g), "Inter", "{g:?}"); + } + // The system's choice wins when installed; a generic whose font is installed stays. + let mut db = usvg::fontdb::Database::new(); + db.load_font_data(include_bytes!("../../../../assets/fonts/Inter-Regular.ttf").to_vec()); + db.load_font_data(include_bytes!("../../../../assets/fonts/JetBrainsMono-Regular.ttf").to_vec()); + db.set_cursive_family("Inter"); + resolve_generic_families(&mut db, |g| Some(if g == "serif" { "JetBrains Mono" } else { "Not Installed" }.to_string())); + assert_eq!(db.family_name(&Family::Serif), "JetBrains Mono"); + assert_eq!(db.family_name(&Family::Monospace), "JetBrains Mono", "the bundled mono stand-in"); + assert_eq!(db.family_name(&Family::SansSerif), "Inter"); + assert_eq!(db.family_name(&Family::Cursive), "Inter"); + // An empty database is left alone. + let mut empty = usvg::fontdb::Database::new(); + resolve_generic_families(&mut empty, |_| Some("Inter".into())); + assert_eq!(empty.family_name(&Family::SansSerif), "Arial"); +} + +#[test] +fn rasterized_groups_stop_at_the_aggregate_pixel_budget() { + let mut svg = String::from(""); + svg.push_str(""); + for i in 0..8 { + svg.push_str(&format!("")); + } + svg.push_str(""); + // Enough for one retained 256×256 RGBA8 surface, not eight of them. + let budget = 256u64 * 256 * 4; + let result = import_parsed_with_budget("budget", parse(svg.as_bytes()).unwrap(), MAX_GROUP_DEPTH, budget).unwrap(); + assert_eq!(result.document.layers.len(), 1, "one U8 surface fills the retained-byte allowance"); + assert_eq!(result.document.layers[0].name, "g0"); + assert!(result.warnings.iter().any(|warning| warning.contains("pixel memory")), "{:?}", result.warnings); +} + +#[test] +fn oversized_raster_does_not_consume_budget_for_later_layers() { + let svg = br##" + + + + + + + + + + + "##; + let result = import_parsed_with_budget("budget", parse(svg).unwrap(), MAX_GROUP_DEPTH, 64 * 64 * 4).unwrap(); + assert_eq!(result.document.layers.len(), 1, "the oversized group must not debit the budget"); + assert_eq!(result.document.layers[0].name, "small"); + assert!(result.warnings.iter().any(|w| w.contains("too large to rasterise")), "{:?}", result.warnings); + assert!(!result.warnings.iter().any(|w| w.contains("pixel memory budget")), "{:?}", result.warnings); +} + +#[test] +fn skipped_expensive_raster_does_not_hide_a_smaller_one() { + let svg = br##" + + + + + + + "##; + let result = import_parsed_with_budget("budget", parse(svg).unwrap(), MAX_GROUP_DEPTH, 32 * 32 * 4).unwrap(); + assert_eq!(result.document.layers.len(), 1); + assert_eq!(result.document.layers[0].name, "small"); + assert!(result.warnings.iter().any(|w| w.contains("some rasterised elements were omitted")), "{:?}", result.warnings); +} + +#[test] +fn huge_drawings_scale_to_the_side_limit() { + let tree = parse(b"").unwrap().tree; + let mut w = Vec::new(); + let cv = canvas(&tree, &mut w); + assert_eq!((cv.size.width, cv.size.height), (MAX_SIDE, MAX_SIDE / 2)); + assert_eq!(w.len(), 1); +} + +/// A pasted drawing is one pixel per SVG unit, and a drawing past the pixel limit is scaled +/// down to fit it, keeping its proportions, instead of failing. +#[test] +fn rgba8_renders_at_its_own_size_within_the_pixel_limit() { + let svg = br##""##; + let (w, h, px) = rasterize_rgba8(svg).unwrap(); + assert_eq!((w, h, px.len()), (40, 20, 40 * 20 * 4)); + assert_eq!(px[..4], [255, 0, 0, 255], "the rectangle"); + assert_eq!(px[(10 * 40 + 30) * 4..][..4], [0, 0, 0, 0], "transparent beside it"); + for (side_w, side_h) in [(10_000u32, 5_000u32), (20_000, 20_000), (20_000, 2_000)] { + let svg = format!(""); + let (w, h, px) = rasterize_rgba8(svg.as_bytes()).unwrap(); + let pixels = u64::from(w) * u64::from(h); + assert!(pixels <= MAX_RASTER_PIXELS && pixels > MAX_RASTER_PIXELS / 2, "{side_w} × {side_h} became {w} × {h}"); + assert!(w <= MAX_SIDE && h <= MAX_SIDE, "{w} × {h}"); + let (aspect, want) = (f64::from(w) / f64::from(h), f64::from(side_w) / f64::from(side_h)); + assert!((aspect / want - 1.0).abs() < 0.01, "{w} × {h} keeps {side_w} × {side_h}"); + assert_eq!(px.len() as u64, pixels * 4); + } + assert!(rasterize_rgba8(b" Vec { + let mut e = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast()); + e.write_all(bytes).unwrap(); + e.finish().unwrap() + } + let opt = options(Arc::new(usvg::fontdb::Database::new()), MAX_XML_DEPTH); + let resolve = &opt.image_href_resolver.resolve_data; + let svg = b""; + let once = gz(svg); + assert!(resolve("image/svg+xml", Arc::new(once.clone()), &opt).is_some()); + assert!(resolve("text/plain", Arc::new(once.clone()), &opt).is_some()); + + // Doubly gzip-compressed payload must be rejected rather than decompressed a second time by usvg. + let twice = gz(&once); + assert!(resolve("image/svg+xml", Arc::new(twice.clone()), &opt).is_none()); + assert!(resolve("text/plain", Arc::new(twice), &opt).is_none()); +} diff --git a/crates/io/src/text_import.rs b/crates/io/src/text_import.rs index 9272f0b5b7..0522f39e90 100644 --- a/crates/io/src/text_import.rs +++ b/crates/io/src/text_import.rs @@ -28,8 +28,14 @@ fn render_bounded(eng: &mut photocraft_text::TextEngine, t: &mut TextLayer, dpi: /// - a type layer without pixels (ag-psd, GIMP and other writers leave the image data out and /// let Photoshop re-render on open) is rendered from its model, instead of staying empty /// until it is edited. -pub(crate) fn prepare(doc: &mut Document) { - fn visit(layers: &mut [Layer], eng: &mut photocraft_text::TextEngine, dpi: f32, format: photocraft_color::PixelFormat) { +pub(crate) fn prepare(doc: &mut Document, txt2: Option<&photocraft_text::engine_data::Value>) { + fn visit( + layers: &mut [Layer], + eng: &mut photocraft_text::TextEngine, + dpi: f32, + format: photocraft_color::PixelFormat, + txt2: Option<&photocraft_text::engine_data::Value>, + ) { for l in layers { match &mut l.content { LayerContent::Text(t) => { @@ -56,11 +62,12 @@ pub(crate) fn prepare(doc: &mut Document) { if changed { t.sync_summary(); } + photocraft_text::psd::refine_legacy_point_origin(t, dpi, eng, txt2); if t.cache.is_none() { render_bounded(eng, t, dpi, format); } } - LayerContent::Group(g) => visit(&mut g.children, eng, dpi, format), + LayerContent::Group(g) => visit(&mut g.children, eng, dpi, format, txt2), _ => {} } } @@ -68,5 +75,5 @@ pub(crate) fn prepare(doc: &mut Document) { let dpi = doc.resolution_dpi; let format = doc.pixel_format(); let mut eng = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner()); - visit(&mut doc.layers, &mut eng, dpi, format); + visit(&mut doc.layers, &mut eng, dpi, format, txt2); } diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs index 3cd8de5b4c..ba35e53fed 100644 --- a/crates/io/src/tiff_layers.rs +++ b/crates/io/src/tiff_layers.rs @@ -11,11 +11,13 @@ use std::sync::Arc; use photocraft_codecs::{self as codecs, ChannelLayout, Format, Image, SampleType as CSample}; -use photocraft_color::ColorMode; -use photocraft_doc::Document; +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{AlphaChannel, Document}; +use photocraft_geom::Rect; use photocraft_psd::resources::ids; use photocraft_psd::tiff::{ByteOrder, ImageSourceData, resources_from_bytes, resources_to_bytes}; use photocraft_psd::{ColorMode as PsdMode, Compression, Header, ImageData, ImageResource, ResolutionInfo}; +use photocraft_raster::Surface; use crate::flat::{csample, encode_image, image_to_document, layout_for, single_layer, try_buffer}; use crate::psd_export::PsdExportOptions; @@ -101,13 +103,36 @@ pub(crate) fn import_layered(name: &str, img: &Image, layers: &[u8]) -> Result 32, }; let (w, h) = img.dimensions(); - let channels = u16::try_from(img.layout().channels()).map_err(|_| IoError::Unsupported("too many channels".into()))?; + let mut data = planar_big_endian(&img, color_channels, mode == PsdMode::Cmyk)?; + let bps = img.sample_type().bytes(); + let plane_len = (w as usize).saturating_mul(h as usize).saturating_mul(bps); + let mut added = 0usize; + for e in img.meta.extra_samples.iter().take(codecs::MAX_EXTRA_SAMPLES) { + match native_plane_to_be(&e.data, bps) { + Ok(be) if be.len() == plane_len => { + data.extend(be); + added += 1; + } + _ => warnings.push("an extra TIFF sample did not match the image and was dropped".into()), + } + } + let channels = u16::try_from(img.layout().channels().saturating_add(added)).map_err(|_| IoError::Unsupported("too many channels".into()))?; let header = Header::new(isd.version, w, h, channels, depth, mode); - let data = planar_big_endian(&img, color_channels, mode == PsdMode::Cmyk)?; - let image_data = ImageData { compression: Compression::Raw, data }; - let file = isd.into_psd(header, resources, image_data); + let image_data = ImageData::new(Compression::Raw, data); + let mut file = isd.into_psd(header, resources, image_data); + // Empty `Patt` and the default filter mask are written so Photoshop's block list matches + // a file it saved. They carry nothing the document stores, so they are not kept: a + // round trip would otherwise grow `metadata.psd_global_blocks` on every open. + file.global_blocks.retain(|b| { + let empty_pattern = matches!(&b.key, b"Patt" | b"Pat2" | b"Pat3") && b.data.is_empty(); + let default_filter_mask = &b.key == b"FMsk" && b.data.as_slice() == FILTER_MASK_DEFAULT; + !empty_pattern && !default_filter_mask + }); // The same nesting limit as opening a PSD (#2940). crate::psd_import::check_group_depth(&file)?; + // Spot planes are read in Photoshop's polarity (0 = solid ink), as from a PSD. The importer's + // rule for PSDs from earlier PhotoCraft versions never applies: those versions stored no + // extra samples in a TIFF, and a TIFF with them carries 1053. let (mut doc, w) = psd_to_document(&file); warnings.extend(w); doc.name = name.to_string(); @@ -200,40 +225,116 @@ pub(crate) fn export_layered(doc: &Document, opts: &ExportOptions, ctl: &photocr } let mut warnings = Vec::new(); let big = doc.size.width > 30_000 || doc.size.height > 30_000; - // The PSD exporter renders the composite we need for the TIFF itself: unmatted (the TIFF - // keeps straight alpha) and uncompressed (its planes are interleaved below). - let psd_opts = - PsdExportOptions { force_psb: opts.force_psb || big, merged_matte: false, merged_compression: Compression::Raw, maximize_compatibility: true }; - let (file, w) = crate::with_span(ctl, 0.0, TIFF_MODEL_DONE, |ctl| crate::psd_export::document_to_psd_interruptible(doc, &psd_opts, ctl)); + // The PSD exporter renders the composite we need for the TIFF itself, uncompressed (its + // planes are interleaved below). A spot (or any user channel) is saved the way Photoshop's + // TIFF dialog does with "Save Transparency" off: the composite is colour plus those + // channels, matted on white, and each layer keeps its own transparency. With no user + // channel the composite stays unmatted, straight RGBA, which is what a plain transparent + // TIFF already round-trips as. + // Layer data is written PSD-style ("Adobe Photoshop Document Data Block", 4-byte lengths), as + // Photoshop writes it: Photoshop does not read the PSB-style header ("... Data V0002") in a + // TIFF and opens only the composite, as one locked Background. The exporter's switch to PSB + // on the encoded file size is not used here: PSB-style data is written only past 30000 px, + // when asked for, or when a length really overflows 32 bits. + let user_channels = !doc.channels.is_empty() || doc.quick_mask.is_some(); + let force_psb = opts.force_psb || big; + let psd_opts = PsdExportOptions { + force_psb, + size_limit_psb: false, + merged_matte: user_channels, + merged_compression: Compression::Raw, + maximize_compatibility: true, + document_path: opts.document_path.clone(), + read_linked_files: opts.read_linked_files, + }; + // TIFF has no palette, bitmap or duotone layout: the PSD model of an Indexed Color, Bitmap + // or Duotone document is made in its pixel format (RGB or Grayscale), which the PSD exporter + // otherwise writes in the document's own mode. + let plain; + let doc = if matches!(doc.mode, ColorMode::Indexed | ColorMode::Bitmap | ColorMode::Duotone) { + plain = Document { mode: doc.pixel_format().mode, ..doc.clone() }; + warnings.push(format!("{:?} document written as {:?}", doc.mode, plain.mode)); + &plain + } else { + doc + }; + let (mut file, w) = crate::with_span(ctl, 0.0, TIFF_MODEL_DONE, |ctl| crate::psd_export::document_to_psd_interruptible(doc, &psd_opts, ctl)); // A cancelled conversion stops early with an incomplete model: never encode it. ctl.check().map_err(|_| IoError::Cancelled)?; warnings.extend(w); let order = if codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; - let (layers, w) = ImageSourceData::from_psd(&file).to_bytes(order)?; + let (layers, w) = match tiff_image_source(doc, &file).to_bytes(order) { + Err(photocraft_psd::PsdError::LimitExceeded(_)) if !force_psb => { + let psb_opts = PsdExportOptions { force_psb: true, ..psd_opts }; + let (psb, w) = crate::with_span(ctl, 0.0, TIFF_MODEL_DONE, |ctl| crate::psd_export::document_to_psd_interruptible(doc, &psb_opts, ctl)); + ctl.check().map_err(|_| IoError::Cancelled)?; + warnings.extend(w); + warnings.push("layer data exceeds 4 GB; written with PSB-sized lengths, which some readers do not open".into()); + file = psb; + tiff_image_source(doc, &file).to_bytes(order)? + } + r => r?, + }; ctl.check().map_err(|_| IoError::Cancelled)?; ctl.progress(TIFF_LAYERS_DONE); warnings.extend(w.into_iter().map(|w| format!("layer data: {w}"))); // The profile and XMP are written as TIFF tags, not as resources. EXIF stays a resource // (1058), which the PSD importer reads back unchanged; a block rebuilt from the TIFF's own // Exif directory keeps only the Exif, GPS and descriptive tags. - let resources: Vec = file.resources.iter().filter(|r| !matches!(r.id, ids::ICC_PROFILE | ids::XMP)).cloned().collect(); - let resources = resources_to_bytes(&resources)?; + let mut resources: Vec = file.resources.iter().filter(|r| !matches!(r.id, ids::ICC_PROFILE | ids::XMP)).cloned().collect(); let fmt = doc.pixel_format(); let cc = fmt.mode.color_channels(); let has_alpha = file.layer_info.as_ref().is_some_and(|l| l.merged_alpha); let channels = cc + usize::from(has_alpha); - let extra = usize::from(file.header.channels).saturating_sub(channels); - if extra > 0 { - warnings.push(format!("{extra} alpha channel(s) are not written to TIFF (one transparency channel only)")); + let extra_n = usize::from(file.header.channels).saturating_sub(channels); + let kept = extra_n.min(codecs::MAX_EXTRA_SAMPLES); + if extra_n > kept { + warnings.push(format!("{extra_n} extra channels; TIFF keeps the first {kept}")); } + // With spot or alpha channels, the IFD image is colour + those channels. The merged + // transparency plane stays out of the IFD (see above); a file with no extra channels keeps + // it, so a plain transparent TIFF still opens with alpha. + let write_alpha = has_alpha && kept == 0; + // One identifier per extra sample actually stored. The PSD model's 1053 lists the merged + // transparency too, which the IFD leaves out next to extra samples, so it is not copied. + resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS); + if kept > 0 { + resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, crate::channel_map::alpha_identifiers(false, kept))); + } + // Resource 1022 names a channel of the PSD merged image, which counts the transparency plane. + // The IFD below does not, so the id moves back by one. + if has_alpha + && kept > 0 + && let Some(r) = resources.iter_mut().find(|r| r.id == crate::channel_map::QUICK_MASK_INFO) + && r.data.len() >= 2 + { + let id = u16::from_be_bytes([r.data[0], r.data[1]]).saturating_sub(1); + let be = id.to_be_bytes(); + r.data[0] = be[0]; + r.data[1] = be[1]; + } + // The name and DisplayInfo resources list the merged transparency plane first. The IFD + // leaves that plane out, so the resources must too: Photoshop pairs each name with the + // next sample past the colour channels. + if has_alpha && !write_alpha { + drop_merged_transparency(&mut resources); + } + // Past MAX_EXTRA_SAMPLES the TIFF stores only the first `kept` channels: the name and + // DisplayInfo resources describe just those, and a Quick Mask among the rest is dropped. + if extra_n > kept { + keep_first_channels(&mut resources, kept, cc); + } + let resources = resources_to_bytes(&resources)?; + let ifd_channels = cc + usize::from(write_alpha); let sample = csample(fmt.sample); let (w, h) = (doc.size.width, doc.size.height); let cmyk = fmt.mode == ColorMode::Cmyk; - let data = interleave(&file.image_data.data, w, h, channels, cc, sample, cmyk)?; + let data = interleave(&file.image_data.data, w, h, ifd_channels, cc, sample, cmyk)?; + let extras = native_extra_planes(&file.image_data.data, w, h, channels, kept, sample)?; ctl.check().map_err(|_| IoError::Cancelled)?; ctl.progress(TIFF_COMPOSITE_DONE); - let layout = layout_for(fmt.mode, has_alpha); + let layout = layout_for(fmt.mode, write_alpha); let mut img = Image::from_raw(w, h, layout, sample, data)?; img.icc = doc.icc_profile.as_ref().map(|i| i.to_vec()); img.meta = codecs::Metadata { @@ -241,16 +342,185 @@ pub(crate) fn export_layered(doc: &Document, opts: &ExportOptions, ctl: &photocr // importer reads it back from there unchanged. A second copy in `meta.exif` (the codec's // Exif directory) would be read back in its place, rebuilt without the thumbnail and IFD0 // orientation (#1545). - xmp: doc.metadata.xmp.clone().filter(|_| opts.xmp == crate::XmpEmbed::All), + xmp: doc.metadata.xmp.as_deref().map(str::to_owned).filter(|_| opts.xmp == crate::XmpEmbed::All), text: if opts.xmp == crate::XmpEmbed::All { doc.metadata.text.clone() } else { Vec::new() }, dpi: Some((doc.resolution_dpi, doc.resolution_dpi)), photoshop_resources: Some(resources), photoshop_layers: Some(layers), + extra_samples: extras, ..Default::default() }; encode_image(&img, Format::Tiff, opts, warnings) } +/// The layer data (`ImageSourceData`) of a layered TIFF saved from `file`, adjusted the way +/// Photoshop writes it. +fn tiff_image_source(doc: &Document, file: &photocraft_psd::PsdFile) -> ImageSourceData { + // Photoshop pairs alpha-name resources with samples past the colour channels and ignores + // the ExtraSamples kind. The merged transparency must not occupy that first slot when a + // spot is also stored. Layer transparency stays in each layer's channel −1. + let mut isd = ImageSourceData::from_psd(file); + let spot_extras = file.header.channels as usize > { + let cc = doc.pixel_format().mode.color_channels(); + let alpha = file.layer_info.as_ref().is_some_and(|l| l.merged_alpha); + cc + usize::from(alpha) + }; + // Positive layer count: the merged image's first extra sample is not transparency + // (Photoshop's own spot TIFF does this; a negative count would claim the spot plane is alpha). + if spot_extras && let Some(info) = &mut isd.layer_info { + info.merged_alpha = false; + } + // A merged-transparency marker kept from an opened file (Photoshop's `MTrn`) would make the + // first spot read back as transparency. The PSD export already drops it over an opaque + // composite; a transparent one keeps it there, but the TIFF leaves that sample out here. + if spot_extras { + isd.global_blocks.retain(|b| !b.is_merged_transparency_marker()); + } + if let Some(info) = &mut isd.layer_info { + for layer in &mut info.layers { + // Photoshop sets flag bit 3 on every record it writes, group dividers included (299 of + // the 305 dividers in the corpus's Photoshop files). PhotoCraft's PSDs set it on + // layers only, unchanged here. + layer.flags.0 |= photocraft_psd::layer::LayerFlags::BIT4_USEFUL; + photoshop_layer_blocks(&mut layer.blocks); + } + } + // Photoshop reads a fixed 14-byte user-mask record from `LMsk` and an empty `Patt` + // before the filter mask. A zero-length `LMsk` is what makes it report that the + // layer data could not be read. + if let Some(mask) = &mut isd.global_layer_mask + && mask.data.len() < 14 + { + *mask = photocraft_psd::GlobalLayerMask::photoshop_default(); + } + if !isd.global_blocks.iter().any(|b| matches!(&b.key, b"Patt" | b"Pat2" | b"Pat3")) { + isd.global_blocks.insert(0, photocraft_psd::TaggedBlock::new(*b"Patt", Vec::new())); + } + if !isd.global_blocks.iter().any(|b| &b.key == b"FMsk") { + isd.global_blocks.push(photocraft_psd::TaggedBlock::new(*b"FMsk", FILTER_MASK_DEFAULT.to_vec())); + } + isd +} + +/// Removes the leading transparency entry from the channel-name resources (1006, 1045) and +/// from DisplayInfo (1077). Each is left unchanged when it is too short to hold that entry. +fn drop_merged_transparency(resources: &mut [ImageResource]) { + for r in resources { + r.data = match r.id { + 1006 => drop_first_pascal(&r.data), + 1045 => drop_first_unicode(&r.data), + id if id == crate::channel_map::DISPLAY_INFO => drop_first_display_record(&r.data), + _ => continue, + }; + } +} + +fn drop_first_pascal(data: &[u8]) -> Vec { + let Some(&n) = data.first() else { return data.to_vec() }; + let end = 1usize.saturating_add(usize::from(n)); + data.get(end..).unwrap_or(data).to_vec() +} + +fn drop_first_unicode(data: &[u8]) -> Vec { + let Some(nbe) = data.get(..4) else { return data.to_vec() }; + let n = u32::from_be_bytes([nbe[0], nbe[1], nbe[2], nbe[3]]) as usize; + let end = 4usize.saturating_add(n.saturating_mul(2)); + data.get(end..).unwrap_or(data).to_vec() +} + +/// Trims the channel-name resources (1006, 1045) and DisplayInfo (1077) to their first `kept` +/// entries, and drops Quick Mask info (1022) when its channel is not one of the `kept` extra +/// samples, which start at sample `first_extra`. A resource too short for `kept` entries is +/// left as it is. +fn keep_first_channels(resources: &mut Vec, kept: usize, first_extra: usize) { + for r in resources.iter_mut() { + r.data = match r.id { + 1006 => first_pascal_names(&r.data, kept), + 1045 => first_unicode_names(&r.data, kept), + id if id == crate::channel_map::DISPLAY_INFO => first_display_records(&r.data, kept), + _ => continue, + }; + } + let end = first_extra.saturating_add(kept); + resources.retain(|r| r.id != crate::channel_map::QUICK_MASK_INFO || r.data.get(..2).is_none_or(|b| usize::from(u16::from_be_bytes([b[0], b[1]])) < end)); +} + +fn first_pascal_names(data: &[u8], n: usize) -> Vec { + let mut at = 0usize; + for _ in 0..n { + let Some(&len) = data.get(at) else { return data.to_vec() }; + at = at.saturating_add(1).saturating_add(usize::from(len)); + } + data.get(..at).unwrap_or(data).to_vec() +} + +fn first_unicode_names(data: &[u8], n: usize) -> Vec { + let mut at = 0usize; + for _ in 0..n { + let Some(b) = data.get(at..at.saturating_add(4)) else { return data.to_vec() }; + let len = u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + at = at.saturating_add(4).saturating_add(len.saturating_mul(2)); + } + data.get(..at).unwrap_or(data).to_vec() +} + +/// Resource 1077: a 4-byte version, then 13-byte records. Keeps the first `n` records. +fn first_display_records(data: &[u8], n: usize) -> Vec { + let end = 4usize.saturating_add(n.saturating_mul(13)); + data.get(..end).unwrap_or(data).to_vec() +} + +/// Resource 1077: a 4-byte version, then 13-byte records. Drops the first record. +fn drop_first_display_record(data: &[u8]) -> Vec { + const RECORD: usize = 13; + if data.len() < 4 + RECORD { + return data.to_vec(); + } + let mut out = Vec::with_capacity(data.len() - RECORD); + out.extend_from_slice(&data[..4]); + out.extend_from_slice(&data[4 + RECORD..]); + out +} + +/// Filter-mask block (`FMsk`) Photoshop writes next to a layered TIFF: the same red 50% +/// overlay as [`photocraft_psd::GlobalLayerMask::photoshop_default`], without the kind byte. +const FILTER_MASK_DEFAULT: &[u8] = &[0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 50]; + +/// Blocks Photoshop writes on every layer of a layered TIFF, when the layer does not +/// already carry them. Defaults match a plain pixel layer (clipped-as-group on, nothing +/// locked, no label). +fn photoshop_layer_blocks(blocks: &mut Vec) { + let has = |k: &[u8; 4]| blocks.iter().any(|b| &b.key == k); + let mut add = Vec::new(); + if !has(b"lnsr") { + add.push(photocraft_psd::TaggedBlock::name_source(*b"layr")); + } + if !has(b"clbl") { + add.push(photocraft_psd::TaggedBlock::blend_clipped_as_group(true)); + } + if !has(b"infx") { + add.push(photocraft_psd::TaggedBlock::blend_interior_elements(false)); + } + if !has(b"knko") { + add.push(photocraft_psd::TaggedBlock::knockout(0)); + } + if !has(b"lspf") { + add.push(photocraft_psd::TaggedBlock::protection(0)); + } + if !has(b"lclr") { + add.push(photocraft_psd::TaggedBlock::sheet_color(0)); + } + let at = blocks + .iter() + .position(|b| &b.key == b"lyid") + .map(|i| i + 1) + .or_else(|| blocks.iter().position(|b| &b.key == b"luni").map(|i| i + 1)) + .unwrap_or(blocks.len()); + for (i, block) in add.into_iter().enumerate() { + blocks.insert(at.saturating_add(i), block); + } +} + /// Removes the blocks a document kept verbatim from a little-endian TIFF because their layout /// is unknown ([`photocraft_psd::TaggedBlock::is_foreign_order`]) before `file` is written as a /// PSD: their bytes are in the wrong order for a PSD and cannot be converted. Returns a warning @@ -279,3 +549,465 @@ pub fn strip_foreign_order_blocks(file: &mut photocraft_psd::PsdFile) -> Vec Result, IoError> { + if bps == 1 { + return Ok(src.to_vec()); + } + if !matches!(bps, 2 | 4) || !src.len().is_multiple_of(bps) { + return Err(IoError::Unsupported("an extra channel plane is incomplete".into())); + } + let mut out = Vec::with_capacity(src.len()); + for c in src.chunks_exact(bps) { + match (bps, to_be, c) { + (2, true, [a, b]) => { + let v = u16::from_ne_bytes([*a, *b]); + out.extend_from_slice(&v.to_be_bytes()); + } + (2, false, [a, b]) => { + let v = u16::from_be_bytes([*a, *b]); + out.extend_from_slice(&v.to_ne_bytes()); + } + (4, true, [a, b, c, d]) => { + let v = f32::from_ne_bytes([*a, *b, *c, *d]); + out.extend_from_slice(&v.to_be_bytes()); + } + (4, false, [a, b, c, d]) => { + let v = f32::from_be_bytes([*a, *b, *c, *d]); + out.extend_from_slice(&v.to_ne_bytes()); + } + _ => return Err(IoError::Unsupported("an extra channel plane is incomplete".into())), + } + } + Ok(out) +} + +/// Native-endian extra-sample plane → big-endian, for the PSD merged image a layered TIFF rebuilds. +fn native_plane_to_be(src: &[u8], bps: usize) -> Result, IoError> { + swap_plane(src, bps, true) +} + +/// Extra planes of a PSD merged image (`skip` colour-and-alpha planes already consumed), as +/// native-endian TIFF extra samples. A spot plane is already Photoshop ink (0 = solid, max = +/// paper); an alpha plane is already straight. Neither is inverted again here. +fn native_extra_planes(planes: &[u8], w: u32, h: u32, skip: usize, count: usize, sample: CSample) -> Result, IoError> { + let bps = sample.bytes(); + let n = (w as usize).checked_mul(h as usize).ok_or_else(|| IoError::Unsupported("image size overflows".into()))?; + let plane = n.checked_mul(bps).ok_or_else(|| IoError::Unsupported("image size overflows".into()))?; + let mut out = Vec::with_capacity(count); + for i in 0..count { + let at = skip.checked_add(i).and_then(|k| k.checked_mul(plane)).ok_or_else(|| IoError::Unsupported("image size overflows".into()))?; + let Some(src) = planes.get(at..at.saturating_add(plane)) else { + return Err(IoError::Unsupported("the merged image is missing an extra channel".into())); + }; + if src.len() != plane { + return Err(IoError::Unsupported("the merged image is missing an extra channel".into())); + } + out.push(codecs::ExtraSample { kind: 0, data: swap_plane(src, bps, false)? }); + } + Ok(out) +} + +/// One grayscale channel as native-endian samples in `img`'s sample type. +fn channel_plane(channel: &AlphaChannel, canvas: Rect, sample: CSample) -> Result, String> { + let depth = match sample { + CSample::U8 => SampleType::U8, + CSample::U16 => SampleType::U16, + CSample::F16 | CSample::F32 => SampleType::F32, + }; + let fmt = PixelFormat::new(ColorMode::Grayscale, depth, false); + let owned; + let surface = if channel.surface.format() == fmt { + &channel.surface + } else { + owned = channel.surface.convert(fmt); + &owned + }; + let mut bytes = surface.to_interleaved(canvas); + let n = (canvas.width() as usize).saturating_mul(canvas.height() as usize); + let expect = n.saturating_mul(depth.bytes()); + if bytes.len() != expect { + return Err(format!("channel \"{}\" does not cover the canvas", channel.name)); + } + if channel.spot.is_some() { + crate::channel_map::photoshop_spot_plane(&mut bytes, depth); + } + if sample == CSample::F16 { + let mut out = Vec::with_capacity(n.saturating_mul(2)); + for arr in bytes.as_chunks::<4>().0 { + let v = f32::from_ne_bytes(*arr); + out.extend_from_slice(&codecs::f16::from_f32(v).to_ne_bytes()); + } + return Ok(out); + } + Ok(bytes) +} + +/// Composites straight alpha over white and drops it. Photoshop's TIFF save with +/// "Save Transparency" off stores that matte in the colour samples and keeps transparency +/// only inside each layer. Fully opaque pixels are copied unchanged, so they stay identical +/// to a composite that was quantized before the matte. `false` when there is no alpha or +/// the buffer is not one sample per channel. +fn matte_and_drop_alpha(img: &mut Image) -> bool { + if !img.layout().has_alpha() { + return false; + } + let (w, h) = img.dimensions(); + let sample = img.sample_type(); + let dst = img.layout().without_alpha(); + let bps = sample.bytes(); + let keep = dst.channels().saturating_mul(bps); + let stride = img.layout().channels().saturating_mul(bps); + if stride == 0 || keep == 0 || bps == 0 || stride <= keep || !img.data().len().is_multiple_of(stride) { + return false; + } + let cmyk = img.layout().is_cmyk(); + let white_byte: u8 = if cmyk { 0 } else { 255 }; + let white_u16: u16 = if cmyk { 0 } else { u16::MAX }; + let white_f: f32 = if cmyk { 0.0 } else { 1.0 }; + // Lab's white is L* 100 with a* and b* 0, which PhotoCraft holds mid-range (`(0 + 128) / 255`). + let lab = img.layout().is_lab(); + let mut out = Vec::with_capacity(img.data().len() / stride * keep); + for px in img.data().chunks_exact(stride) { + let Some(color) = px.get(..keep) else { return false }; + let Some(alpha) = px.get(keep..keep.saturating_add(bps)) else { return false }; + let ok = if lab { + let (Some(l), Some(ab)) = (color.get(..bps), color.get(bps..)) else { return false }; + matte_pixel(sample, l, alpha, white_byte, white_u16, white_f, &mut out) && matte_pixel(sample, ab, alpha, 128, 128 * 257, 128.0 / 255.0, &mut out) + } else { + matte_pixel(sample, color, alpha, white_byte, white_u16, white_f, &mut out) + }; + if !ok { + return false; + } + } + let icc = img.icc.clone(); + let meta = img.meta.clone(); + let warnings = img.warnings.clone(); + let Ok(mut next) = Image::from_raw(w, h, dst, sample, out) else { return false }; + next.icc = icc; + next.meta = meta; + next.warnings = warnings; + *img = next; + true +} + +/// One pixel of [`matte_and_drop_alpha`]. Opaque samples are copied; a clear pixel becomes +/// `white`; anything in between is `c·a + white·(1−a)` in normalized space. +fn matte_pixel(sample: CSample, color: &[u8], alpha: &[u8], white_byte: u8, white_u16: u16, white_f: f32, out: &mut Vec) -> bool { + match sample { + CSample::U8 => { + let Some(&a) = alpha.first() else { return false }; + if a == 255 { + out.extend_from_slice(color); + } else if a == 0 { + out.resize(out.len().saturating_add(color.len()), white_byte); + } else { + let af = f32::from(a) / 255.0; + for &c in color { + let m = crate::pixels::matte(f32::from(c) / 255.0, af, white_f); + out.push((m.clamp(0.0, 1.0) * 255.0).round() as u8); + } + } + } + CSample::U16 => { + let Some(a) = ne_u16(alpha) else { return false }; + if a == u16::MAX { + out.extend_from_slice(color); + } else if a == 0 { + for _ in 0..color.len() / 2 { + out.extend_from_slice(&white_u16.to_ne_bytes()); + } + } else { + let af = f32::from(a) / 65535.0; + for pair in color.as_chunks::<2>().0 { + let c = u16::from_ne_bytes(*pair); + let m = crate::pixels::matte(f32::from(c) / 65535.0, af, white_f); + out.extend_from_slice(&((m.clamp(0.0, 1.0) * 65535.0).round() as u16).to_ne_bytes()); + } + } + } + CSample::F32 => { + let Some(a) = ne_f32(alpha) else { return false }; + if a == 1.0 { + out.extend_from_slice(color); + } else if a == 0.0 || !a.is_finite() { + for _ in 0..color.len() / 4 { + out.extend_from_slice(&white_f.to_ne_bytes()); + } + } else { + let af = a.clamp(0.0, 1.0); + for quad in color.as_chunks::<4>().0 { + let m = crate::pixels::matte(f32::from_ne_bytes(*quad), af, white_f); + out.extend_from_slice(&m.to_ne_bytes()); + } + } + } + CSample::F16 => { + let Some(bits) = ne_u16(alpha) else { return false }; + let a = codecs::f16::from_bits(bits).to_f32(); + if a == 1.0 { + out.extend_from_slice(color); + } else if a == 0.0 || !a.is_finite() { + let w = codecs::f16::from_f32(white_f).to_bits().to_ne_bytes(); + for _ in 0..color.len() / 2 { + out.extend_from_slice(&w); + } + } else { + let af = a.clamp(0.0, 1.0); + for pair in color.as_chunks::<2>().0 { + let c = codecs::f16::from_bits(u16::from_ne_bytes(*pair)).to_f32(); + let m = codecs::f16::from_f32(crate::pixels::matte(c, af, white_f)).to_bits(); + out.extend_from_slice(&m.to_ne_bytes()); + } + } + } + } + true +} + +fn ne_u16(b: &[u8]) -> Option { + let arr: [u8; 2] = b.get(..2)?.try_into().ok()?; + Some(u16::from_ne_bytes(arr)) +} + +fn ne_f32(b: &[u8]) -> Option { + let arr: [u8; 4] = b.get(..4)?.try_into().ok()?; + Some(f32::from_ne_bytes(arr)) +} + +/// Writes alpha and spot channels (and a Quick Mask) into a flat TIFF: extra samples in the +/// IFD, plus Photoshop resources for the name, ink colour and solidity. At most +/// [`codecs::MAX_EXTRA_SAMPLES`] are kept. A TIFF with no extra channels is left unchanged. +/// When any are written, the composite's own transparency is left out of the IFD: readers pair +/// the channel resources with every sample past the colour channels, in order. +pub(crate) fn stamp_flat_channels(doc: &Document, img: &mut Image) -> Vec { + let total = doc.channels.len().saturating_add(usize::from(doc.quick_mask.is_some())); + if total == 0 { + return Vec::new(); + } + let mut warnings = Vec::new(); + let (w, h) = img.dimensions(); + if w != doc.size.width || h != doc.size.height { + warnings.push("extra channels were not written: the image size differs from the document".into()); + return warnings; + } + let max = codecs::MAX_EXTRA_SAMPLES; + let mut picked: Vec<&AlphaChannel> = doc.channels.iter().take(max).collect(); + if let Some(q) = &doc.quick_mask + && picked.len() < max + { + picked.push(q); + } + if total > picked.len() { + warnings.push(format!("{total} extra channels; TIFF keeps the first {}", picked.len())); + } + let canvas = Rect::new(0, 0, w as i32, h as i32); + let mut planes = Vec::with_capacity(picked.len()); + for ch in &picked { + match channel_plane(ch, canvas, img.sample_type()) { + Ok(data) => planes.push(data), + Err(msg) => { + warnings.push(msg); + break; + } + } + } + picked.truncate(planes.len()); + if picked.is_empty() { + return warnings; + } + let names: Vec<&str> = picked.iter().map(|c| c.name.as_str()).collect(); + let quick_last = matches!((picked.last(), doc.quick_mask.as_ref()), (Some(p), Some(q)) if std::ptr::eq(*p, q)); + let color_channels = img.layout().color_channels(); + // Save Transparency off: the colour samples are the composite over white, and the + // alpha plane is not an extra sample (it would be named as the first spot). The + // composite transparency is gone after this; say so, because the user did not pick + // that option themselves. + if img.layout().has_alpha() { + if !matte_and_drop_alpha(img) { + warnings.push("extra channels were not written: the composite transparency could not be separated from them".into()); + return warnings; + } + warnings.push( + "transparency composited over white; a TIFF with alpha or spot channels stores them as extra samples and drops image transparency (Save Transparency off)" + .into(), + ); + } + let quick_id = if quick_last { u16::try_from(color_channels.saturating_add(picked.len()).saturating_sub(1)).ok() } else { None }; + match channel_resources(img.meta.photoshop_resources.as_deref(), &names, &picked, quick_id) { + Ok(bytes) => img.meta.photoshop_resources = Some(bytes), + Err(e) => warnings.push(format!("spot channel options were not written to the TIFF: {e}")), + } + img.meta.extra_samples = planes.into_iter().map(|data| codecs::ExtraSample { kind: 0, data }).collect(); + warnings +} + +/// Existing Photoshop resources with the channel-name, display-info and quick-mask blocks replaced. +fn channel_resources(existing: Option<&[u8]>, names: &[&str], channels: &[&AlphaChannel], quick_id: Option) -> Result, IoError> { + let mut resources = match existing.filter(|b| !b.is_empty()) { + Some(b) => resources_from_bytes(b)?.0, + None => Vec::new(), + }; + resources.retain(|r| { + !matches!(r.id, 1006 | 1045 | crate::channel_map::DISPLAY_INFO | crate::channel_map::QUICK_MASK_INFO | crate::channel_map::ALPHA_IDENTIFIERS) + }); + resources.push(ImageResource::new(1006, crate::psd_export::pascal_names_resource(names))); + resources.push(ImageResource::new(1045, crate::psd_export::unicode_names_resource(names))); + resources.push(ImageResource::new(crate::channel_map::DISPLAY_INFO, crate::channel_map::display_info(channels, false))); + resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, crate::channel_map::alpha_identifiers(false, names.len()))); + if let Some(id) = quick_id { + let mut data = id.to_be_bytes().to_vec(); + data.push(0); + resources.push(ImageResource::new(crate::channel_map::QUICK_MASK_INFO, data)); + } + Ok(resources_to_bytes(&resources)?) +} + +/// Extra samples of a flat TIFF become alpha / spot channels. Layered TIFFs rebuild those +/// through the PSD importer instead, so this is only for a file with no layer tag. +pub(crate) fn channels_from_extras(doc: &mut Document, img: &Image) -> Vec { + if img.meta.extra_samples.is_empty() { + return Vec::new(); + } + let mut warnings = Vec::new(); + let (w, h) = img.dimensions(); + if w != doc.size.width || h != doc.size.height { + warnings.push("extra TIFF samples were dropped: their size differs from the image".into()); + return warnings; + } + let parsed = img.meta.photoshop_resources.as_deref().filter(|b| !b.is_empty()).and_then(|b| resources_from_bytes(b).ok()); + let none = Vec::new(); + let resources = parsed.as_ref().map(|(r, _)| r.as_slice()).unwrap_or(&none); + let all_names = resources.iter().find(|r| r.id == 1045).map(|r| crate::psd_import::unicode_names(&r.data)).unwrap_or_default(); + // As in the PSD importer: Photoshop lists the image transparency first ("Transparency", with + // its own display record), ahead of the channels the extra samples hold. + let skip = usize::from(img.layout().has_alpha() && all_names.len() == img.meta.extra_samples.len().saturating_add(1)); + let names = all_names.get(skip..).unwrap_or_default(); + let spots = if let Some(r) = resources.iter().find(|r| r.id == crate::channel_map::DISPLAY_INFO) { + crate::channel_map::spot_flags(&r.data, true, skip) + } else if let Some(r) = resources.iter().find(|r| r.id == crate::channel_map::DISPLAY_INFO_OLD) { + crate::channel_map::spot_flags(&r.data, false, skip) + } else { + Vec::new() + }; + let start = doc.channels.len(); + let n_px = (w as usize).saturating_mul(h as usize); + for (k, e) in img.meta.extra_samples.iter().take(codecs::MAX_EXTRA_SAMPLES).enumerate() { + let spot = spots.get(k).copied().unwrap_or(false); + match mask_from_plane(&e.data, w, h, n_px, img.sample_type(), doc.depth, spot) { + Ok(surface) => { + let name = names.get(k).cloned().filter(|s| !s.is_empty()).unwrap_or_else(|| format!("Alpha {}", k + 1)); + doc.channels.push(AlphaChannel::new(name, surface)); + } + Err(msg) => warnings.push(msg), + } + } + if doc.channels.len() == start { + return warnings; + } + { + let slice = &mut doc.channels[start..]; + if let Some(r) = resources.iter().find(|r| r.id == crate::channel_map::DISPLAY_INFO) { + crate::channel_map::apply_display_info(&r.data, true, skip, slice); + } else if let Some(r) = resources.iter().find(|r| r.id == crate::channel_map::DISPLAY_INFO_OLD) { + crate::channel_map::apply_display_info(&r.data, false, skip, slice); + } + } + let first = img.layout().color_channels().saturating_add(usize::from(img.layout().has_alpha())); + if let Some(r) = resources.iter().find(|r| r.id == crate::channel_map::QUICK_MASK_INFO) + && r.data.len() >= 2 + && let Some(k) = usize::from(u16::from_be_bytes([r.data[0], r.data[1]])).checked_sub(first) + && start.saturating_add(k) < doc.channels.len() + { + doc.quick_mask = Some(doc.channels.remove(start + k)); + } + warnings +} + +/// Native-endian extra-sample bytes → a grayscale surface at the document depth. +/// `photoshop_spot`: the sample is a spot channel in Photoshop's polarity (0 = solid ink). +fn mask_from_plane(data: &[u8], w: u32, h: u32, n: usize, from: CSample, depth: SampleType, photoshop_spot: bool) -> Result { + let rect = Rect::new(0, 0, w as i32, h as i32); + let mut bytes = match (from, depth) { + (CSample::U8, SampleType::U8) | (CSample::U16, SampleType::U16) | (CSample::F32, SampleType::F32) => { + if data.len() != n.saturating_mul(from.bytes()) { + return Err("an extra TIFF sample did not match the image and was dropped".into()); + } + data.to_vec() + } + (CSample::F16, SampleType::F32) => { + if data.len() != n.saturating_mul(2) { + return Err("an extra TIFF sample did not match the image and was dropped".into()); + } + let mut out = Vec::with_capacity(n.saturating_mul(4)); + for arr in data.as_chunks::<2>().0 { + let v = codecs::f16::from_bits(u16::from_ne_bytes(*arr)).to_f32(); + out.extend_from_slice(&v.to_ne_bytes()); + } + out + } + _ => return Err(format!("an extra TIFF sample ({from:?}) does not match the document depth and was dropped")), + }; + if photoshop_spot { + crate::channel_map::photoshop_spot_plane(&mut bytes, depth); + } + if bytes.len() != n.saturating_mul(depth.bytes()) { + return Err("an extra TIFF sample did not match the image and was dropped".into()); + } + let mut s = Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false)); + s.write_interleaved(rect, &bytes); + s.prune(); + Ok(s) +} + +#[cfg(test)] +mod tests { + use photocraft_doc::{Color, Document, Fill, FillCache, Layer, LayerContent}; + use photocraft_raster::Surface; + + use crate::{ExportOptions, export}; + + /// Hidden solid fills on a 2048² canvas: counted as canvas-sized planes (16 MiB each at 8 + /// bits), 140 of them pass 2 GB, while their real pixels (the cached rendering, pruned to + /// nothing) take almost no space. A layered TIFF of such a document must still carry + /// PSD-style layer data: Photoshop does not read the PSB-style header in a TIFF and opens only + /// the composite, as one locked Background. + fn many_cheap_fills() -> Document { + let mut doc = Document::new("many fills", photocraft_geom::Size::new(2048, 2048), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + for i in 0..140 { + let fill = Fill::Solid(Color::rgb(0.5, 0.25, 0.75)); + let mut l = Layer::new(format!("Fill {i}"), LayerContent::Fill(fill.clone())); + l.visible = false; + l.fill_cache = Some(FillCache { fill, surface: Surface::new(doc.pixel_format()) }); + doc.layers.push(l); + } + doc + } + + #[test] + fn layered_tiff_ignores_the_uncompressed_size_estimate() { + let doc = many_cheap_fills(); + + let r = export(&doc, "x.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap(); + let (_, layers) = photocraft_codecs::tiff_photoshop_tags(&r.bytes); + let layers = layers.unwrap(); + assert!(layers.starts_with(photocraft_psd::tiff::SIGNATURE), "{:?}", String::from_utf8_lossy(&layers[..36.min(layers.len())])); + assert!(!r.warnings.iter().any(|w| w.contains("PSB")), "{:?}", r.warnings); + let back = crate::import("x.tif", &r.bytes).unwrap().document; + assert_eq!(back.layers.len(), doc.layers.len()); + + // A .psd save switches to PSB on the encoded size, which is small here: it stays PSD. + let psd = export(&doc, "x.psd", &ExportOptions::default()).unwrap(); + assert_eq!(psd.bytes.get(4..6), Some(&[0u8, 1][..]), "PSD saves switch on the encoded size"); + } + + #[test] + fn forcing_psb_still_writes_psb_style_layer_data() { + let doc = many_cheap_fills(); + let r = export(&doc, "x.tif", &ExportOptions { tiff_layers: true, force_psb: true, ..Default::default() }).unwrap(); + let (_, layers) = photocraft_codecs::tiff_photoshop_tags(&r.bytes); + assert!(layers.unwrap().starts_with(photocraft_psd::tiff::SIGNATURE_PSB)); + } +} diff --git a/crates/io/src/vector_map.rs b/crates/io/src/vector_map.rs index 3bd127445d..621edf47b8 100644 --- a/crates/io/src/vector_map.rs +++ b/crates/io/src/vector_map.rs @@ -258,10 +258,20 @@ pub fn parse_vstk(data: &[u8], dpi: f32) -> Option { _ => Vec::new(), }; let paint = get_desc(&d, "strokeStyleContent").and_then(content_fill).unwrap_or(Fill::Solid(photocraft_color::Color::BLACK)); + let mut opacity = num(d.get("strokeStyleOpacity")).map_or(1.0, |v| v as f32 / 100.0); + // Write folds colour alpha into strokeStyleOpacity for Photoshop. Restore both Appearance + // fields when pcAl is present so PhotoCraft does not show a maxed colour alpha (#6180). + if let Fill::Solid(c) = &paint + && c.alpha.is_finite() + && c.alpha > 1e-6 + && (c.alpha - 1.0).abs() > 1e-6 + { + opacity = (opacity / c.alpha).clamp(0.0, 1.0); + } let stroke = ShapeStroke { width: unit_px(d.get("strokeStyleLineWidth"), dpi).unwrap_or(1.0) as f32, paint, - opacity: num(d.get("strokeStyleOpacity")).map_or(1.0, |v| v as f32 / 100.0), + opacity, align, cap, join, @@ -276,10 +286,28 @@ fn en(ty: &str, v: &str) -> Value { Value::Enumerated { type_id: Id::new(ty), value: Id::new(v) } } +/// Photoshop's stroke colour has no alpha. Fold the colour's alpha into `strokeStyleOpacity` +/// so Appearance still matches after a PSD round-trip (#6180). +fn fold_stroke_color_alpha(s: &ShapeStroke) -> ShapeStroke { + let mut out = s.clone(); + if let Fill::Solid(c) = &mut out.paint + && c.alpha.is_finite() + && (c.alpha - 1.0).abs() > 1e-6 + { + out.opacity = (out.opacity * c.alpha).clamp(0.0, 1.0); + c.alpha = 1.0; + } + out +} + pub fn vstk_bytes(stroke: Option<&ShapeStroke>, fill_enabled: bool, dpi: f32) -> Vec { let default = ShapeStroke::default(); - let s = stroke.unwrap_or(&default); - let (_, content) = fill_to_desc(&s.paint); + let folded = stroke.map(fold_stroke_color_alpha); + let s = folded.as_ref().unwrap_or(&default); + // Keep pcAl on the original colour so Appearance can restore fill/stroke alphas separately + // from the folded Photoshop opacity (#6180). + let paint = stroke.map(|st| &st.paint).unwrap_or(&s.paint); + let (_, content) = fill_to_desc(paint); let class = match s.paint { Fill::Solid(_) => "solidColorLayer", Fill::Gradient { .. } => "gradientLayer", @@ -660,6 +688,20 @@ mod tests { assert!(off.stroke.is_none() && off.fill_enabled); } + #[test] + fn vstk_folds_color_alpha_into_stroke_opacity() { + let s = ShapeStroke { width: 2.0, paint: Fill::Solid(Color::rgba(0.0, 0.0, 1.0, 0.4)), opacity: 0.5, ..ShapeStroke::default() }; + let bytes = vstk_bytes(Some(&s), true, 72.0); + let raw = crate::blocks::parse_prefix_versioned(&bytes).unwrap(); + let file_opacity = crate::blocks::num(raw.get("strokeStyleOpacity")).unwrap() as f32 / 100.0; + assert!((file_opacity - 0.2).abs() < 1e-4, "Photoshop sees the product in strokeStyleOpacity"); + let got = parse_vstk(&bytes, 72.0).unwrap().stroke.unwrap(); + assert!((got.opacity - 0.5).abs() < 1e-4); + let Fill::Solid(c) = got.paint else { panic!("solid") }; + assert!((c.alpha - 0.4).abs() < 1e-5); + assert!((c.to_rgb()[2] - 1.0).abs() < 1e-3); + } + #[test] fn vogk_roundtrip() { for s in [ @@ -671,7 +713,7 @@ mod tests { let b = vogk_bytes(&s, 72.0).unwrap(); assert_eq!(live_from_vogk(&b), Some(s)); } - assert!(vogk_bytes(&LiveShape::Polygon { rect: [0.0; 4], sides: 5, star_ratio: 1.0 }, 72.0).is_none()); + assert!(vogk_bytes(&LiveShape::Polygon { rect: [0.0; 4], sides: 5, star_ratio: 1.0, corner_radius: 0.0 }, 72.0).is_none()); } #[test] @@ -699,7 +741,7 @@ mod tests { // Edit: path changes → vmsk regenerated, live dropped when polygon. let mut edited = sh.clone(); edited.path.subpaths.pop(); - edited.live = Some(LiveShape::Polygon { rect: [0.0; 4], sides: 3, star_ratio: 1.0 }); + edited.live = Some(LiveShape::Polygon { rect: [0.0; 4], sides: 3, star_ratio: 1.0, corner_radius: 0.0 }); shape_blocks(&edited, &mut raw, 64, 64, 72.0); assert!(!raw.iter().any(|(k, _)| k == b"vogk")); let (p, _) = path_from_vmsk(&raw[0].1, 64, 64).unwrap(); diff --git a/crates/io/src/xcf.rs b/crates/io/src/xcf.rs new file mode 100644 index 0000000000..e9a6b92a21 --- /dev/null +++ b/crates/io/src/xcf.rs @@ -0,0 +1,1087 @@ +//! GIMP XCF (`.xcf`, `.xcf.gz`) read. Layout from the public specification +//! (https://developer.gimp.org/core/standards/xcf/, versions 0 to 26): a header, image +//! properties, then pointers to layer, channel and path structures; each drawable's pixels sit in +//! 64×64 tiles, uncompressed, RLE-coded or zlib-coded. +//! +//! Read: pixel layers at any position and size, groups (nested, pass-through), names, opacity, +//! visibility, offsets, layer masks (applied or disabled), locks, colour tags, blend modes (the +//! legacy and the 2.10 sets; those without a Photoshop equivalent open as Normal with a warning), +//! 8/16/32-bit integer and 16/32/64-bit float precisions (linear precisions get a linear-light +//! profile), the embedded ICC profile, resolution, guides, the comment, extra channels and the +//! saved selection; indexed images open as RGB. Text, vector and link layers open as their pixels; +//! non-destructive layer effects, paths and floating selections are reported, not applied. +//! Write: not supported (GIMP's own format; `.pcraft` keeps everything PhotoCraft holds). +//! +//! The reader is written from the specification and from files GIMP 3.2 writes; no GIMP code. + +#[cfg(test)] +mod tests; + +use std::collections::HashMap; +use std::io::Read; +use std::sync::Arc; + +use photocraft_codecs::f16; +use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{AlphaChannel, Document, LabelColor, Layer, LayerContent, LayerMask}; +use photocraft_geom::{Rect, Size}; +use photocraft_raster::{Interrupt, Surface}; + +use crate::ora::Warnings; +use crate::{ImportResult, IoError}; + +type Result = std::result::Result; + +const MAGIC: &[u8] = b"gimp xcf "; +/// Canvas side limit (Photoshop's PSB limit). +pub(crate) const MAX_SIDE: u32 = 300_000; +/// Decoded pixels across every drawable are capped, as for OpenRaster and PDN. +pub(crate) const MAX_PIXEL_BYTES: usize = 1 << 30; +/// Layers and groups in one document. +pub(crate) const MAX_LAYERS: usize = 8192; +/// Extra channels in one document. +const MAX_CHANNELS: usize = 1024; +/// Group nesting. +const MAX_DEPTH: usize = 64; +/// Total copied layer metadata (names and item paths). A single reused record with a large +/// name is bounded by MAX_STRING, but repeated layer offsets referencing it must not multiply +/// into unbounded retained metadata. +const MAX_METADATA_BYTES: usize = 32 << 20; +/// A string field (layer names, parasite names). +const MAX_STRING: usize = 1 << 20; +/// A property payload or a parasite that is read into memory. +const MAX_PAYLOAD: usize = 64 << 20; +/// The largest file a gzipped XCF may inflate to. +const MAX_INFLATED: usize = 1 << 30; +/// Highest version this reader was written against; newer files still open (unknown properties +/// are skipped by their length), with a warning. +const KNOWN_VERSION: u32 = 26; +const TILE: u32 = 64; + +fn invalid(message: impl Into) -> IoError { + IoError::Xcf(message.into()) +} + +/// `true` for an XCF file (by its signature). +pub(crate) fn is_xcf(bytes: &[u8]) -> bool { + bytes.starts_with(MAGIC) +} + +/// `true` for a gzip stream named `.xcf.gz` / `.xcfgz` (GIMP writes and reads compressed XCF). +pub(crate) fn is_gzipped_xcf(name: &str, bytes: &[u8]) -> bool { + let lower = name.rsplit(['/', '\\']).next().unwrap_or(name).to_ascii_lowercase(); + bytes.starts_with(&[0x1f, 0x8b]) && (lower.ends_with(".xcf.gz") || lower.ends_with(".xcfgz")) +} + +/// Inflates a gzipped XCF, bounded. +pub(crate) fn gunzip(bytes: &[u8]) -> Result> { + let mut out = Vec::new(); + let mut dec = flate2::read::GzDecoder::new(bytes).take(MAX_INFLATED as u64 + 1); + dec.read_to_end(&mut out).map_err(|e| invalid(format!("gzip: {e}")))?; + if out.len() > MAX_INFLATED { + return Err(invalid("the compressed file inflates past the 1 GiB limit")); + } + Ok(out) +} + +/// How samples are stored: bytes per sample, whether they are floats, and whether the values are +/// linear light (else gamma-encoded, i.e. the profile's own curve). +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct Precision { + bytes: usize, + float: bool, + linear: bool, +} + +impl Precision { + const GAMMA8: Precision = Precision { bytes: 1, float: false, linear: false }; + + fn from_header(version: u32, code: u32) -> Result { + let p = |bytes, float, linear| Ok(Precision { bytes, float, linear }); + match version { + 0..=3 => Ok(Self::GAMMA8), + 4 => match code { + 0 => p(1, false, false), + 1 => p(2, false, false), + 2 => p(4, false, true), + 3 => p(2, true, true), + 4 => p(4, true, true), + c => Err(invalid(format!("unknown precision {c} in an XCF version 4 file"))), + }, + 5 | 6 => match code { + 100 => p(1, false, true), + 150 => p(1, false, false), + 200 => p(2, false, true), + 250 => p(2, false, false), + 300 => p(4, false, true), + 350 => p(4, false, false), + 400 => p(2, true, true), + 450 => p(2, true, false), + 500 => p(4, true, true), + 550 => p(4, true, false), + c => Err(invalid(format!("unknown precision {c} in an XCF version {version} file"))), + }, + _ => match code { + 100 => p(1, false, true), + 150 => p(1, false, false), + 200 => p(2, false, true), + 250 => p(2, false, false), + 300 => p(4, false, true), + 350 => p(4, false, false), + 500 => p(2, true, true), + 550 => p(2, true, false), + 600 => p(4, true, true), + 650 => p(4, true, false), + 700 => p(8, true, true), + 750 => p(8, true, false), + c => Err(invalid(format!("unknown precision {c}"))), + }, + } + } + + /// The document depth the samples open at. + fn depth(self) -> SampleType { + match (self.float, self.bytes) { + (false, 1) => SampleType::U8, + (false, _) => SampleType::U16, + (true, _) => SampleType::F32, + } + } + + /// One sample, big-endian, as a normalised float. + fn sample(self, b: &[u8]) -> f32 { + match (self.float, self.bytes, b) { + (false, 1, [v]) => f32::from(*v) / 255.0, + (false, 2, [a, b]) => f32::from(u16::from_be_bytes([*a, *b])) / 65535.0, + (false, 4, [a, b, c, d]) => (u32::from_be_bytes([*a, *b, *c, *d]) as f64 / f64::from(u32::MAX)) as f32, + (true, 2, [a, b]) => f16::from_bits(u16::from_be_bytes([*a, *b])).to_f32(), + (true, 4, [a, b, c, d]) => f32::from_be_bytes([*a, *b, *c, *d]), + (true, 8, [a, b, c, d, e, f, g, h]) => f64::from_be_bytes([*a, *b, *c, *d, *e, *f, *g, *h]) as f32, + _ => 0.0, + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Compression { + None, + Rle, + Zlib, +} + +/// Big-endian reads with bounds checks; every failure is an error, never a panic. +struct Cursor<'a> { + b: &'a [u8], + pos: usize, + /// XCF 11 and later store pointers as 64-bit. + wide: bool, +} + +impl<'a> Cursor<'a> { + fn at(&self, pos: usize) -> Cursor<'a> { + Cursor { b: self.b, pos, wide: self.wide } + } + + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.pos.checked_add(n).ok_or_else(|| invalid("offset overflow"))?; + let s = self.b.get(self.pos..end).ok_or_else(|| invalid(format!("the file ends early (wanted {n} bytes at {})", self.pos)))?; + self.pos = end; + Ok(s) + } + + fn u32(&mut self) -> Result { + let s = self.take(4)?; + Ok(u32::from_be_bytes([s[0], s[1], s[2], s[3]])) + } + + fn ptr(&mut self) -> Result { + if self.wide { + let s = self.take(8)?; + usize::try_from(u64::from_be_bytes([s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]])).map_err(|_| invalid("pointer does not fit in memory")) + } else { + self.u32().map(|v| v as usize) + } + } + + /// A STRING: `n+1` then `n` bytes and a NUL (0 for the empty string). + fn string(&mut self) -> Result { + Ok(String::from_utf8_lossy(self.string_bytes()?).into_owned()) + } + + /// The same bounded STRING read without allocating or decoding an unused field. + fn string_bytes(&mut self) -> Result<&'a [u8]> { + let n = self.u32()? as usize; + if n > MAX_STRING { + return Err(invalid(format!("a string of {n} bytes exceeds the limit"))); + } + let s = self.take(n)?; + Ok(s.strip_suffix(&[0]).unwrap_or(s)) + } + + fn skip(&mut self, n: usize) -> Result<()> { + self.take(n).map(|_| ()) + } +} + +/// One property record: its type and payload. +struct Prop<'a> { + kind: u32, + data: &'a [u8], +} + +/// Reads a property list, measuring known payloads whose length words GIMP writes incorrectly. +/// Ends at PROP_END (type 0). +fn props<'a>(c: &mut Cursor<'a>) -> Result>> { + let mut out = Vec::new(); + loop { + let kind = c.u32()?; + let len = c.u32()? as usize; + if kind == 0 { + return Ok(out); + } + // PROP_COLORMAP's length word was wrong in some releases (n+4 for 3n+4 bytes); its real + // size follows from the count. PROP_USER_UNIT's length is wrong in every release. + // PROP_LINK_LAYER's length can be four bytes short (#8857); read all its fields. + let len = match kind { + 1 => { + let n = c.at(c.pos).u32()? as usize; + 4usize.checked_add(n.checked_mul(3).ok_or_else(|| invalid("colormap overflow"))?).ok_or_else(|| invalid("colormap overflow"))? + } + 24 => user_unit_len(c)?, + 48 => link_layer_len(c)?, + _ => len, + }; + if len > MAX_PAYLOAD { + return Err(invalid(format!("property {kind} declares {len} bytes"))); + } + let data = c.take(len)?; + out.push(Prop { kind, data }); + if out.len() > 4096 { + return Err(invalid("too many properties")); + } + } +} + +/// A PROP_LINK_LAYER payload: flags, a path STRING, then the source width and height. +/// The dimensions are present for raster links too, where their values are unused. +fn link_layer_len(c: &Cursor<'_>) -> Result { + let mut p = c.at(c.pos); + p.skip(4)?; + p.string_bytes()?; + p.skip(8)?; + Ok(p.pos - c.pos) +} + +/// The real size of a PROP_USER_UNIT payload: a float, a count and three (XCF 21+) or five +/// strings. Measured without consuming. +fn user_unit_len(c: &Cursor<'_>) -> Result { + let mut p = c.at(c.pos); + p.skip(8)?; + for _ in 0..3 { + p.string()?; + } + // The two name forms of older files, when present: another string pair before the next + // property. Decide by whether a plausible property header follows. + let three = p.pos - c.pos; + let mut q = p.at(p.pos); + let looks_like_prop = |c: &mut Cursor<'_>| c.u32().map(|k| k <= 64).unwrap_or(false); + if looks_like_prop(&mut q) { + return Ok(three); + } + p.string()?; + p.string()?; + Ok(p.pos - c.pos) +} + +fn be_u32(b: &[u8]) -> Option { + b.get(..4).map(|s| u32::from_be_bytes([s[0], s[1], s[2], s[3]])) +} + +fn be_i32(b: &[u8]) -> Option { + be_u32(b).map(|v| v as i32) +} + +fn be_f32(b: &[u8]) -> Option { + be_u32(b).map(f32::from_bits) +} + +/// The parasites of a PROP_PARASITES payload as (name, data); a later duplicate wins. +fn parasites(data: &[u8]) -> Vec<(String, Vec)> { + let mut c = Cursor { b: data, pos: 0, wide: false }; + let mut out: Vec<(String, Vec)> = Vec::new(); + // Map each name to its output slot so duplicate replacement is O(1). The parasite + // records are file-controlled, so scanning `out` with iter_mut().find() per record is + // O(n^2) and lets a crafted payload with many distinct names stall import. The map + // preserves insertion order and last-value-wins behavior. + let mut index: HashMap = HashMap::new(); + while c.pos < data.len() { + let Ok(name) = c.string() else { break }; + let (Ok(_flags), Ok(len)) = (c.u32(), c.u32()) else { break }; + let Ok(payload) = c.take(len as usize) else { break }; + match index.get(&name) { + Some(&i) => out[i].1 = payload.to_vec(), + None => { + index.insert(name.clone(), out.len()); + out.push((name, payload.to_vec())); + } + } + } + out +} + +/// What one tile's bytes decode to, for a drawable of `bpp` bytes per pixel. +fn decode_tile(c: &Cursor<'_>, at: usize, end: usize, compression: Compression, bpp: usize, pixels: usize) -> Result> { + let expected = pixels.checked_mul(bpp).ok_or_else(|| invalid("tile size overflow"))?; + let input = c.b.get(at..end.min(c.b.len())).ok_or_else(|| invalid("a tile pointer is outside the file"))?; + match compression { + Compression::None => input.get(..expected).map(<[u8]>::to_vec).ok_or_else(|| invalid("an uncompressed tile is cut short")), + Compression::Zlib => { + let mut out = Vec::with_capacity(expected); + flate2::read::ZlibDecoder::new(input).take(expected as u64).read_to_end(&mut out).map_err(|e| invalid(format!("zlib tile: {e}")))?; + if out.len() != expected { + return Err(invalid("a zlib tile is cut short")); + } + Ok(out) + } + Compression::Rle => rle_decode(input, bpp, pixels), + } +} + +/// RLE tile data: one run-length stream per byte position of the pixel, each of `pixels` bytes. +fn rle_decode(input: &[u8], bpp: usize, pixels: usize) -> Result> { + let mut out = vec![0u8; pixels * bpp]; + let mut pos = 0usize; + let mut plane = Vec::with_capacity(pixels); + for b in 0..bpp { + plane.clear(); + while plane.len() < pixels { + let op = *input.get(pos).ok_or_else(|| invalid("an RLE tile is cut short"))?; + pos += 1; + let (run, verbatim) = match op { + 0..=126 => (usize::from(op) + 1, false), + 127 => { + let (p, q) = (input.get(pos).copied(), input.get(pos + 1).copied()); + pos += 2; + ( + usize::from(p.ok_or_else(|| invalid("an RLE tile is cut short"))?) * 256 + + usize::from(q.ok_or_else(|| invalid("an RLE tile is cut short"))?), + false, + ) + } + 128 => { + let (p, q) = (input.get(pos).copied(), input.get(pos + 1).copied()); + pos += 2; + ( + usize::from(p.ok_or_else(|| invalid("an RLE tile is cut short"))?) * 256 + + usize::from(q.ok_or_else(|| invalid("an RLE tile is cut short"))?), + true, + ) + } + _ => (256 - usize::from(op), true), + }; + if plane.len() + run > pixels { + return Err(invalid("an RLE run crosses the end of its tile")); + } + if verbatim { + let data = input.get(pos..pos + run).ok_or_else(|| invalid("an RLE tile is cut short"))?; + pos += run; + plane.extend_from_slice(data); + } else { + let v = *input.get(pos).ok_or_else(|| invalid("an RLE tile is cut short"))?; + pos += 1; + plane.resize(plane.len() + run, v); + } + } + for (i, v) in plane.iter().enumerate() { + out[i * bpp + b] = *v; + } + } + Ok(out) +} + +/// Image-wide facts the drawables need. +struct Image<'a> { + c: Cursor<'a>, + version: u32, + precision: Precision, + compression: Compression, + base_type: u32, + colormap: Vec<[u8; 3]>, + budget: usize, + /// Remaining allowance for copied layer metadata (names, item paths). Repeated layer + /// offsets referencing one record with a large name would otherwise allocate an owned + /// copy per reference, multiplying a small file into gigabytes of retained metadata. + meta_budget: usize, + warnings: Warnings, + /// Layers GIMP composites with "Clip to backdrop" (its default for every mode but Normal). + clipped_to_backdrop: usize, +} + +/// Pixel data of a drawable: `channels` normalised samples per pixel, row-major. +struct Pixels { + width: u32, + height: u32, + channels: usize, + data: Vec, +} + +impl<'a> Image<'a> { + /// Reads a hierarchy (`hptr`): the first level's tiles into `channels` floats per pixel, + /// `source_channels` as stored (indexed images expand to RGB here). + fn hierarchy(&mut self, hptr: usize, source_channels: usize, ctl: &Interrupt<'_>) -> Result { + let mut c = self.c.at(hptr); + let (w, h, bpp) = (c.u32()?, c.u32()?, c.u32()? as usize); + if w == 0 || h == 0 || w > MAX_SIDE || h > MAX_SIDE { + return Err(invalid(format!("drawable size {w}x{h} is out of range"))); + } + let expected_bpp = source_channels.checked_mul(self.precision.bytes).ok_or_else(|| invalid("bpp overflow"))?; + if bpp != expected_bpp { + return Err(invalid(format!("{bpp} bytes per pixel where {expected_bpp} were expected"))); + } + let indexed = self.base_type == 2; + let channels = if indexed { source_channels + 2 } else { source_channels }; + let count = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(channels)).ok_or_else(|| invalid("drawable overflow"))?; + let bytes = count.checked_mul(4).ok_or_else(|| invalid("drawable overflow"))?; + self.budget = self.budget.checked_sub(bytes).ok_or_else(|| invalid("the layers exceed the 1 GiB pixel limit"))?; + let lptr = c.ptr()?; + let mut l = self.c.at(lptr); + let (lw, lh) = (l.u32()?, l.u32()?); + if (lw, lh) != (w, h) { + return Err(invalid("the level does not match its hierarchy")); + } + let across = w.div_ceil(TILE) as usize; + let down = h.div_ceil(TILE) as usize; + let mut tiles = Vec::with_capacity(across * down); + for _ in 0..across * down { + let p = l.ptr()?; + if p == 0 { + return Err(invalid("the tile list ends before every tile")); + } + tiles.push(p); + } + let mut data = vec![0.0f32; count]; + let stride = w as usize * channels; + for (i, &at) in tiles.iter().enumerate() { + if i % 64 == 0 { + ctl.check().map_err(|_| IoError::Cancelled)?; + } + let (tx, ty) = ((i % across) as u32, (i / across) as u32); + let (tw, th) = ((w - tx * TILE).min(TILE), (h - ty * TILE).min(TILE)); + let end = tiles.get(i + 1).copied().filter(|&n| n > at).unwrap_or(self.c.b.len()); + let raw = decode_tile(&self.c, at, end, self.compression, bpp, (tw * th) as usize)?; + for py in 0..th as usize { + for px in 0..tw as usize { + let src = (py * tw as usize + px) * bpp; + let dst = (ty as usize * TILE as usize + py) * stride + (tx as usize * TILE as usize + px) * channels; + let pixel = &raw[src..src + bpp]; + if indexed { + let idx = usize::from(pixel[0]); + let [r, g, b] = self.colormap.get(idx).copied().unwrap_or([pixel[0]; 3]); + data[dst] = f32::from(r) / 255.0; + data[dst + 1] = f32::from(g) / 255.0; + data[dst + 2] = f32::from(b) / 255.0; + if source_channels == 2 { + data[dst + 3] = f32::from(pixel[1]) / 255.0; + } + } else { + for ch in 0..channels { + data[dst + ch] = self.precision.sample(&pixel[ch * self.precision.bytes..(ch + 1) * self.precision.bytes]); + } + } + } + } + } + Ok(Pixels { width: w, height: h, channels, data }) + } + + /// A channel structure (a layer mask, the selection or an extra channel) as one-channel + /// pixels, with its properties. + fn channel(&mut self, ptr: usize, ctl: &Interrupt<'_>) -> Result<(String, Vec>, Pixels)> { + let mut c = self.c.at(ptr); + let (_w, _h) = (c.u32()?, c.u32()?); + let name = c.string()?; + let props = props(&mut c)?; + let hptr = c.ptr()?; + let saved = (self.base_type, self.precision); + // Channels are always one plain sample per pixel, even in indexed images. + self.base_type = 0; + let px = self.hierarchy(hptr, 1, ctl); + (self.base_type, self.precision) = saved; + Ok((name, props, px?)) + } +} + +/// A layer as read, before the tree is built. +struct Node { + layer: Layer, + path: Vec, + group: bool, + children: Vec, +} + +/// GIMP's layer mode as Photoshop's, with a note when the two differ. +fn blend_mode(mode: u32) -> (BlendMode, Option<&'static str>) { + use BlendMode as B; + match mode { + 0 | 28 => (B::Normal, None), + 1 => (B::Dissolve, None), + 2 | 29 => (B::Normal, Some("Behind")), + 3 | 30 => (B::Multiply, None), + 4 | 31 => (B::Screen, None), + 5 | 19 => (B::SoftLight, Some("GIMP's legacy Soft light")), + 45 => (B::SoftLight, Some("GIMP's Soft light")), + 23 => (B::Overlay, None), + 6 | 32 => (B::Difference, None), + 7 | 33 => (B::LinearDodge, None), + 8 | 34 => (B::Subtract, None), + 9 | 35 => (B::Darken, None), + 10 | 36 => (B::Lighten, None), + 11 | 37 => (B::Hue, Some("HSV Hue")), + 12 | 38 => (B::Saturation, Some("HSV Saturation")), + 13 | 39 => (B::Color, Some("HSL Color")), + 14 | 40 => (B::Luminosity, Some("HSV Value")), + 15 | 41 => (B::Divide, None), + 16 | 42 => (B::ColorDodge, None), + 17 | 43 => (B::ColorBurn, None), + 18 | 44 => (B::HardLight, None), + 20 | 46 => (B::Normal, Some("Grain extract")), + 21 | 47 => (B::Normal, Some("Grain merge")), + 22 | 57 => (B::Normal, Some("Color erase")), + 24 => (B::Hue, Some("LCH Hue")), + 25 => (B::Saturation, Some("LCH Chroma")), + 26 => (B::Color, Some("LCH Color")), + 27 => (B::Luminosity, Some("LCH Lightness")), + 48 => (B::VividLight, None), + 49 => (B::PinLight, None), + 50 => (B::LinearLight, None), + 51 => (B::HardMix, None), + 52 => (B::Exclusion, None), + 53 => (B::LinearBurn, None), + 54 => (B::DarkerColor, Some("Luma darken only")), + 55 => (B::LighterColor, Some("Luma lighten only")), + 56 => (B::Luminosity, Some("Luminance")), + 58 => (B::Normal, Some("Erase")), + 59 => (B::Normal, Some("Merge")), + 60 => (B::Normal, Some("Split")), + 61 => (B::PassThrough, None), + _ => (B::Normal, Some("an unknown mode")), + } +} + +fn label(tag: u32) -> LabelColor { + match tag { + 1 => LabelColor::Blue, + 2 => LabelColor::Green, + 3 => LabelColor::Yellow, + 4 => LabelColor::Orange, + 6 => LabelColor::Red, + 7 => LabelColor::Violet, + 8 => LabelColor::Gray, + _ => LabelColor::None, + } +} + +/// Writes `px` (its channels matching `surface`'s) at `x, y` of the document. +fn write_pixels(surface: &mut Surface, px: &Pixels, x: i32, y: i32) -> Result<()> { + let (w, h) = (px.width as i32, px.height as i32); + let rect = Rect::new(x, y, x.checked_add(w).ok_or_else(|| invalid("offset overflow"))?, y.checked_add(h).ok_or_else(|| invalid("offset overflow"))?); + if surface.channels() != px.channels || px.data.len() != (px.width as usize) * (px.height as usize) * px.channels { + return Err(invalid("pixel data does not match the drawable")); + } + surface.write_region(rect, &px.data); + surface.prune(); + Ok(()) +} + +struct ImageMetadata { + compression: Compression, + colormap: Vec<[u8; 3]>, + resolution: Option<(f32, f32)>, + guides: Vec<(i32, u8)>, + icc: Option>, + comment: Option, + path_count: usize, +} + +fn read_image_metadata(c: &mut Cursor<'_>) -> Result { + // Image properties. + let image_props = props(c)?; + let mut compression = Compression::None; + let mut colormap = Vec::new(); + let mut resolution = None; + let mut guides: Vec<(i32, u8)> = Vec::new(); + let mut icc = None; + let mut comment = None; + let mut path_count = 0usize; + for p in &image_props { + match p.kind { + 1 => { + let n = be_u32(p.data).unwrap_or(0) as usize; + colormap = p.data.get(4..).unwrap_or(&[]).as_chunks::<3>().0.iter().take(n.min(256)).copied().collect(); + } + 17 => { + compression = match p.data.first() { + Some(0) => Compression::None, + Some(1) => Compression::Rle, + Some(2) => Compression::Zlib, + Some(k) => return Err(invalid(format!("unknown compression {k}"))), + None => return Err(invalid("empty compression property")), + } + } + 18 => { + for g in p.data.as_chunks::<5>().0 { + guides.push((i32::from_be_bytes([g[0], g[1], g[2], g[3]]), g[4])); + } + } + 19 => { + if let (Some(x), Some(y)) = (be_f32(p.data), p.data.get(4..).and_then(be_f32)) { + resolution = Some((x, y)); + } + } + 21 => { + for (name, data) in parasites(p.data) { + match name.as_str() { + "icc-profile" if !data.is_empty() => icc = Some(data), + "gimp-comment" => comment = Some(String::from_utf8_lossy(&data).trim_end_matches('\0').to_string()), + _ => {} + } + } + } + 23 => path_count += be_u32(p.data.get(4..).unwrap_or(&[])).unwrap_or(0) as usize, + 25 => path_count += be_u32(p.data.get(8..).unwrap_or(&[])).unwrap_or(0) as usize, + _ => {} + } + } + + Ok(ImageMetadata { compression, colormap, resolution, guides, icc, comment, path_count }) +} + +/// Imports an XCF file (already inflated when it was gzipped). +pub(crate) fn import(name: &str, bytes: &[u8], ctl: &Interrupt<'_>) -> Result { + if !is_xcf(bytes) { + return Err(invalid("not an XCF file")); + } + let mut c = Cursor { b: bytes, pos: MAGIC.len(), wide: false }; + let tag = c.take(5)?; + let version = match tag { + b"file\0" => 0, + [b'v', a, b, d, 0] if a.is_ascii_digit() && b.is_ascii_digit() && d.is_ascii_digit() => { + u32::from(a - b'0') * 100 + u32::from(b - b'0') * 10 + u32::from(d - b'0') + } + _ => return Err(invalid("unknown XCF version tag")), + }; + c.wide = version >= 11; + let mut warnings = Warnings::default(); + if version > KNOWN_VERSION { + warnings.note(&format!("XCF version {version} is newer than this reader (version {KNOWN_VERSION}); unknown parts were skipped")); + } + let (w, h, base_type) = (c.u32()?, c.u32()?, c.u32()?); + if w == 0 || h == 0 || w > MAX_SIDE || h > MAX_SIDE { + return Err(invalid(format!("canvas {w}x{h} must be 1 to {MAX_SIDE} pixels a side"))); + } + if (w as usize).saturating_mul(h as usize).saturating_mul(16) > MAX_PIXEL_BYTES { + return Err(invalid("the canvas exceeds the 1 GiB pixel limit")); + } + let precision = if version >= 4 { Precision::from_header(version, c.u32()?)? } else { Precision::GAMMA8 }; + let mode = match base_type { + 0 => ColorMode::Rgb, + 1 => ColorMode::Grayscale, + 2 => ColorMode::Rgb, + t => return Err(invalid(format!("unknown image type {t}"))), + }; + let depth = precision.depth(); + if base_type == 2 { + warnings.note("the indexed image opens as RGB"); + } + if !precision.float && precision.bytes == 4 { + warnings.note("32-bit integer samples open as 16-bit"); + } + + let ImageMetadata { compression, colormap, resolution, guides, icc, comment, mut path_count } = read_image_metadata(&mut c)?; + + let mut layer_ptrs = Vec::new(); + loop { + let p = c.ptr()?; + if p == 0 { + break; + } + layer_ptrs.push(p); + if layer_ptrs.len() > MAX_LAYERS { + return Err(invalid(format!("more than {MAX_LAYERS} layers"))); + } + } + let mut channel_ptrs = Vec::new(); + loop { + let p = c.ptr()?; + if p == 0 { + break; + } + channel_ptrs.push(p); + if channel_ptrs.len() > MAX_CHANNELS { + return Err(invalid(format!("more than {MAX_CHANNELS} channels"))); + } + } + if version >= 18 { + let mut n = 0; + while c.ptr().ok().is_some_and(|p| p != 0) { + n += 1; + if n > MAX_CHANNELS { + break; + } + } + path_count += n; + } + + let mut image = Image { + c: c.at(0), + version, + precision, + compression, + base_type, + colormap, + budget: MAX_PIXEL_BYTES, + meta_budget: MAX_METADATA_BYTES, + warnings, + clipped_to_backdrop: 0, + }; + let fmt = PixelFormat::new(mode, depth, true); + let mask_fmt = PixelFormat::new(ColorMode::Grayscale, depth, false); + let total = layer_ptrs.len().max(1) as f32; + + // Layers, topmost first in the file. + let mut nodes: Vec = Vec::new(); + let mut linear_compositing = false; + for (i, &ptr) in layer_ptrs.iter().enumerate() { + ctl.check().map_err(|_| IoError::Cancelled)?; + ctl.progress(0.05 + 0.85 * i as f32 / total); + if let Some((node, linear)) = image.layer(ptr, fmt, mask_fmt, ctl)? { + linear_compositing |= linear; + nodes.push(node); + } + } + let mut roots: Vec = Vec::new(); + for (i, mut node) in nodes.into_iter().enumerate() { + // Default path: the item's position at the top level, as a file without groups has it. + if node.path.is_empty() { + node.path = vec![i as u32]; + } + if node.path.len() > MAX_DEPTH { + image.warnings.layer(&node.layer.name, "is nested deeper than the reader allows and was moved to the top level"); + node.path.truncate(1); + } + place(&mut roots, node, &mut image.warnings); + } + let layers = finish(roots); + + // Channels: the selection and the extras. + let mut channels = Vec::new(); + let mut selection = None; + for &ptr in &channel_ptrs { + ctl.check().map_err(|_| IoError::Cancelled)?; + let (name, cprops, px) = image.channel(ptr, ctl)?; + let mut surface = Surface::new(mask_fmt); + write_pixels(&mut surface, &px, 0, 0)?; + if cprops.iter().any(|p| p.kind == 4) { + selection = Some(surface); + } else { + channels.push(AlphaChannel::new(name, surface)); + } + } + + let mut document = Document::new(name, Size::new(w, h), mode, depth); + document.layers = layers; + document.channels = channels; + document.selection = selection; + if let Some((x, y)) = resolution.filter(|(x, _)| *x > 0.0 && *x < 1.0e6) { + document.resolution_dpi = x; + if let Some(w) = crate::unequal_resolution_warning(f64::from(x), f64::from(y)) { + image.warnings.note(&w); + } + } + for (coord, orientation) in guides { + if coord < 0 && version < 15 { + continue; + } + match orientation { + 1 => document.guides.horizontal.push(f64::from(coord)), + 2 => document.guides.vertical.push(f64::from(coord)), + _ => {} + } + } + document.icc_profile = match (precision.linear, icc, mode) { + (true, icc, ColorMode::Grayscale) => { + if icc.is_some() { + image.warnings.note("the linear-light image's own profile was replaced by linear gray"); + } + Some(photocraft_cms::builtin::linear_gray().to_bytes()) + } + (true, icc, _) => { + if icc.is_some() { + image.warnings.note("the linear-light image's own profile was replaced by linear sRGB"); + } + Some(photocraft_cms::Builtin::LinearSrgb.profile().to_bytes()) + } + (false, Some(icc), _) => Some(Arc::new(icc)), + (false, None, _) => None, + }; + if let Some(comment) = comment.filter(|c| !c.is_empty()) { + document.metadata.text.push(("Comment".into(), comment)); + } + if path_count > 0 { + image.warnings.note(&format!("{path_count} path(s) were not imported")); + } + add_document_warnings(&mut image, &document, precision, linear_compositing); + ctl.progress(1.0); + Ok(ImportResult { document, warnings: image.warnings.finish(), source_read_only: true, preview_only: false }) +} + +fn add_document_warnings(image: &mut Image<'_>, document: &Document, precision: Precision, linear_compositing: bool) { + // GIMP clips a layer in any mode but Normal to the layers beneath it, so it shows nothing where + // they are transparent; PhotoCraft (like Photoshop) shows it there. Only tell when there is + // transparency to show through: a bottom layer that is not opaque. + if image.clipped_to_backdrop > 0 && document.layers.first().is_some_and(|l| !l.locks.transparency || l.is_group()) { + image.warnings.note(&format!( + "{} layer(s) use a mode GIMP clips to the layers beneath; where those are transparent GIMP shows nothing and PhotoCraft shows the layer", + image.clipped_to_backdrop + )); + } + // GIMP blends and composites in linear light by default (since 2.10); PhotoCraft, like + // Photoshop, in the document's own encoding, so semi-transparent edges and most blend modes + // render a little differently. Layers set to a perceptual space in GIMP match. + if !precision.linear && linear_compositing { + image.warnings.note("GIMP composites this image in linear light; PhotoCraft composites in gamma-encoded RGB, so blends and soft edges differ slightly"); + } + if document.layers.is_empty() { + image.warnings.note("the XCF file has no layers"); + } +} + +/// Puts `node` where its item path says, under the groups already placed; a path that leads +/// nowhere (at any depth) puts it at the top level, so later siblings keep their indices. +fn place(roots: &mut Vec, node: Node, warnings: &mut Warnings) { + let depth = node.path.len().saturating_sub(1); + let mut probe: &Vec = roots; + let reachable = node.path.iter().take(depth).all(|&index| match probe.get(index as usize) { + Some(n) if n.group => { + probe = &n.children; + true + } + _ => false, + }); + if !reachable { + warnings.layer(&node.layer.name, "has an item path that leads to no group; opened at the top level"); + let mut node = node; + node.path = vec![]; + roots.push(node); + return; + } + let mut level = roots; + for &index in node.path.iter().take(depth) { + level = &mut level[index as usize].children; + } + level.push(node); +} + +/// Nodes top-first into layers bottom-first, groups recursively. +fn finish(nodes: Vec) -> Vec { + let mut out = Vec::with_capacity(nodes.len()); + for mut node in nodes.into_iter().rev() { + if node.group { + let children = finish(std::mem::take(&mut node.children)); + if let Some(c) = node.layer.children_mut() { + *c = children; + } + } + out.push(node.layer); + } + out +} + +impl<'a> Image<'a> { + /// Reads a layer structure, and whether GIMP composites it in linear light. `None` for a + /// floating selection, which is reported instead. + fn layer(&mut self, ptr: usize, fmt: PixelFormat, mask_fmt: PixelFormat, ctl: &Interrupt<'_>) -> Result> { + let mut c = self.c.at(ptr); + let (lw, lh, kind) = (c.u32()?, c.u32()?, c.u32()?); + let name = c.string()?; + // Charge the owned name against the document-wide metadata budget: repeated layer + // offsets referencing one large name must not retain unbounded copies. + self.meta_budget = self.meta_budget.checked_sub(name.len()).ok_or_else(|| invalid("layer metadata exceeds the budget"))?; + let lprops = props(&mut c)?; + let hptr = c.ptr()?; + let mptr = c.ptr()?; + let mut effects = 0usize; + if self.version >= 20 { + while c.ptr()? != 0 { + effects += 1; + if effects > 1024 { + return Err(invalid("too many layer effects")); + } + } + } + let (type_rgb, alpha) = match kind { + 0 => (true, false), + 1 => (true, true), + 2 | 4 => (false, false), + 3 | 5 => (false, true), + k => return Err(invalid(format!("unknown layer type {k} on '{name}'"))), + }; + if type_rgb != (self.base_type != 1) && self.base_type != 2 { + return Err(invalid(format!("layer '{name}' does not match the image's colour model"))); + } + + let mut layer_mode = 0u32; + let mut opacity = 1.0f32; + // PROP_FLOAT_OPACITY wins over PROP_OPACITY whatever their order in the file. + let mut float_opacity = None; + let mut visible = true; + let (mut x, mut y) = (0i32, 0i32); + let mut apply_mask = true; + let mut group = false; + let mut expanded = true; + let mut path = Vec::new(); + let mut text = false; + let mut vector = false; + let mut link = false; + let mut floating = false; + let mut clipped = false; + let mut composite_mode = 0i32; + let mut composite_space = 0i32; + let mut blend_space = 0i32; + let mut locks = photocraft_doc::Locks::default(); + let mut tag = LabelColor::None; + for p in &lprops { + let v = be_u32(p.data).unwrap_or(0); + match p.kind { + 5 => floating = true, + 6 => opacity = (v.min(255) as f32) / 255.0, + 33 => float_opacity = be_f32(p.data).filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0)).or(float_opacity), + 7 => layer_mode = v, + 8 => visible = v != 0, + 10 => locks.transparency = v != 0, + 11 => apply_mask = v != 0, + 15 => { + x = be_i32(p.data).unwrap_or(0); + y = p.data.get(4..).and_then(be_i32).unwrap_or(0); + } + 26 => text = true, + 28 => locks.pixels = v != 0, + 29 => group = true, + 30 => { + path = p.data.as_chunks::<4>().0.iter().map(|s| u32::from_be_bytes(*s)).collect(); + self.meta_budget = + self.meta_budget.checked_sub(path.len().saturating_mul(4)).ok_or_else(|| invalid("layer metadata exceeds the budget"))?; + } + 31 => expanded = v & 1 != 0, + 32 => locks.position = v != 0, + 34 => tag = label(v), + 35 => composite_mode = be_i32(p.data).unwrap_or(0), + 36 => composite_space = be_i32(p.data).unwrap_or(0), + 37 => blend_space = be_i32(p.data).unwrap_or(0), + 47 => vector = true, + 48 => link = true, + 21 => { + for (pname, _) in parasites(p.data) { + match pname.as_str() { + "gimphoto-clipped" => clipped = true, + "gimp-text-layer" => text = true, + _ => {} + } + } + } + _ => {} + } + } + if floating { + self.warnings.layer(&name, "is a floating selection and was not imported (anchor it in GIMP first)"); + return Ok(None); + } + + let (blend, note) = blend_mode(layer_mode); + if let Some(note) = note { + self.warnings.layer( + &name, + &format!( + "uses GIMP's {note} mode, which opens as {}", + if blend == BlendMode::Normal { "Normal".to_string() } else { format!("Photoshop's {blend:?}") } + ), + ); + } + match composite_mode.unsigned_abs() { + 0 | 1 => {} + 2 => self.clipped_to_backdrop += 1, + 3 => self.warnings.layer(&name, "composites with Clip to layer, which is not reproduced"), + 4 => self.warnings.layer(&name, "composites with Intersection, which is not reproduced"), + _ => {} + } + if text { + self.warnings.layer(&name, "is a text layer and opens as pixels"); + } + if vector { + self.warnings.layer(&name, "is a vector layer and opens as pixels"); + } + if link { + self.warnings.layer(&name, "is a link layer and opens as pixels"); + } + if effects > 0 { + self.warnings.layer(&name, &format!("has {effects} layer effect(s) (non-destructive filters) that are not applied")); + } + + let mut layer = if group { + let mut g = Layer::group(name.clone(), Vec::new()); + if let LayerContent::Group(gr) = &mut g.content { + gr.expanded = expanded; + } + g.blend = if blend == BlendMode::PassThrough { BlendMode::PassThrough } else { blend }; + g + } else { + let mut l = Layer::raster(name.clone(), fmt); + l.blend = if blend == BlendMode::PassThrough { BlendMode::Normal } else { blend }; + l + }; + layer.opacity = float_opacity.unwrap_or(opacity); + layer.visible = visible; + layer.locks = locks; + layer.label = tag; + layer.clipped = clipped; + + if !group && hptr != 0 { + let source_channels = match (self.base_type, alpha) { + (2, false) => 1, + (2, true) => 2, + (1, false) => 1, + (1, true) => 2, + (_, false) => 3, + (_, true) => 4, + }; + let mut px = self.hierarchy(hptr, source_channels, ctl)?; + if (px.width, px.height) != (lw, lh) { + return Err(invalid(format!("layer '{name}' pixels are {}x{} where {lw}x{lh} were declared", px.width, px.height))); + } + if !alpha { + // An opaque layer: add the alpha channel the document's pixel format carries. + let n = px.channels; + let mut data = Vec::with_capacity(px.data.len() / n * (n + 1)); + for p in px.data.chunks_exact(n) { + data.extend_from_slice(p); + data.push(1.0); + } + px = Pixels { width: px.width, height: px.height, channels: n + 1, data }; + } + let mut surface = Surface::new(fmt); + write_pixels(&mut surface, &px, x, y)?; + layer.content = LayerContent::Raster(surface); + if !alpha { + layer.locks.transparency = true; + } + } + if mptr != 0 { + let (_mname, _mprops, px) = self.channel(mptr, ctl)?; + let mut surface = Surface::new(mask_fmt); + write_pixels(&mut surface, &px, x, y)?; + layer.mask = Some(LayerMask { surface, enabled: apply_mask, linked: true, density: 1.0, feather: 0.0 }); + } + let linear = composite_space.unsigned_abs() == 1 || blend_space.unsigned_abs() == 1; + Ok(Some((Node { layer, path, group, children: Vec::new() }, linear))) + } +} diff --git a/crates/io/src/xcf/tests.rs b/crates/io/src/xcf/tests.rs new file mode 100644 index 0000000000..24481f1956 --- /dev/null +++ b/crates/io/src/xcf/tests.rs @@ -0,0 +1,810 @@ +//! Synthetic XCF files (written here, from the specification) for the reader: structure, +//! every compression, precisions, groups, masks, channels, limits and malformed input. + +use super::*; +use photocraft_raster::Interrupt; + +/// A small XCF writer for tests: version 11+ layout (64-bit pointers) by default. +pub(super) struct W { + pub out: Vec, + pub wide: bool, +} + +impl W { + pub fn new(version: u32) -> Self { + let mut w = W { out: Vec::new(), wide: version >= 11 }; + w.out.extend_from_slice(b"gimp xcf "); + if version == 0 { + w.out.extend_from_slice(b"file\0"); + } else { + w.out.extend_from_slice(format!("v{version:03}\0").as_bytes()); + } + w + } + pub fn u32(&mut self, v: u32) { + self.out.extend_from_slice(&v.to_be_bytes()); + } + pub fn ptr(&mut self, v: usize) { + if self.wide { + self.out.extend_from_slice(&(v as u64).to_be_bytes()); + } else { + self.u32(v as u32); + } + } + pub fn string(&mut self, s: &str) { + if s.is_empty() { + self.u32(0); + } else { + self.u32(s.len() as u32 + 1); + self.out.extend_from_slice(s.as_bytes()); + self.out.push(0); + } + } + pub fn prop(&mut self, kind: u32, payload: &[u8]) { + self.u32(kind); + self.u32(payload.len() as u32); + self.out.extend_from_slice(payload); + } + pub fn prop_u32(&mut self, kind: u32, v: u32) { + self.prop(kind, &v.to_be_bytes()); + } + pub fn end(&mut self) { + self.u32(0); + self.u32(0); + } + /// Reserves a pointer slot, returning where to patch it. + pub fn slot(&mut self) -> usize { + let at = self.out.len(); + self.ptr(0); + at + } + pub fn patch(&mut self, slot: usize, target: usize) { + if self.wide { + self.out[slot..slot + 8].copy_from_slice(&(target as u64).to_be_bytes()); + } else { + self.out[slot..slot + 4].copy_from_slice(&(target as u32).to_be_bytes()); + } + } +} + +/// RLE-encodes one tile of `bpp`-byte pixels, plane by plane, with runs and literals. +pub(super) fn rle(pixels: &[u8], bpp: usize) -> Vec { + let n = pixels.len() / bpp; + let mut out = Vec::new(); + for b in 0..bpp { + let plane: Vec = (0..n).map(|i| pixels[i * bpp + b]).collect(); + let mut i = 0; + while i < n { + let v = plane[i]; + let mut run = 1; + while i + run < n && plane[i + run] == v && run < 65535 { + run += 1; + } + if run >= 2 { + if run <= 127 { + out.push((run - 1) as u8); + } else { + out.push(127); + out.push((run / 256) as u8); + out.push((run % 256) as u8); + } + out.push(v); + i += run; + } else { + let start = i; + while i < n && (i + 1 >= n || plane[i + 1] != plane[i]) && i - start < 127 { + i += 1; + } + let len = i - start; + out.push((256 - len) as u8); + out.extend_from_slice(&plane[start..i]); + } + } + } + out +} + +/// Writes a hierarchy for `w`×`h` pixels of `bpp` bytes (`data` row-major), returning its offset. +pub(super) fn hierarchy(w: &mut W, width: u32, height: u32, bpp: usize, data: &[u8], compression: Compression) -> usize { + let at = w.out.len(); + w.u32(width); + w.u32(height); + w.u32(bpp as u32); + let level_slot = w.slot(); + w.ptr(0); + let level = w.out.len(); + w.patch(level_slot, level); + w.u32(width); + w.u32(height); + let across = width.div_ceil(TILE) as usize; + let down = height.div_ceil(TILE) as usize; + let slots: Vec = (0..across * down).map(|_| w.slot()).collect(); + w.ptr(0); + for (i, slot) in slots.into_iter().enumerate() { + let (tx, ty) = ((i % across) as u32, (i / across) as u32); + let (tw, th) = ((width - tx * TILE).min(TILE), (height - ty * TILE).min(TILE)); + let mut tile = Vec::new(); + for py in 0..th { + for px in 0..tw { + let src = ((ty * TILE + py) as usize * width as usize + (tx * TILE + px) as usize) * bpp; + tile.extend_from_slice(&data[src..src + bpp]); + } + } + let encoded = match compression { + Compression::None => tile, + Compression::Rle => rle(&tile, bpp), + Compression::Zlib => { + use std::io::Write; + let mut e = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(&tile).unwrap(); + e.finish().unwrap() + } + }; + let here = w.out.len(); + w.patch(slot, here); + w.out.extend_from_slice(&encoded); + } + at +} + +/// A layer to write. +pub(super) struct TestLayer { + pub name: &'static str, + pub width: u32, + pub height: u32, + pub kind: u32, + pub x: i32, + pub y: i32, + pub opacity: f32, + pub visible: bool, + pub mode: u32, + pub props: Vec<(u32, Vec)>, + pub data: Vec, + pub mask: Option>, + pub apply_mask: bool, +} + +impl TestLayer { + pub fn rgba(name: &'static str, width: u32, height: u32, data: Vec) -> Self { + TestLayer { name, width, height, kind: 1, x: 0, y: 0, opacity: 1.0, visible: true, mode: 28, props: Vec::new(), data, mask: None, apply_mask: true } + } +} + +pub(super) struct TestImage { + pub version: u32, + pub width: u32, + pub height: u32, + pub base_type: u32, + pub precision: u32, + pub compression: Compression, + pub image_props: Vec<(u32, Vec)>, + pub layers: Vec, + pub channels: Vec<(&'static str, Vec, bool)>, +} + +impl TestImage { + pub fn rgb8(width: u32, height: u32) -> Self { + TestImage { + version: 11, + width, + height, + base_type: 0, + precision: 150, + compression: Compression::Rle, + image_props: Vec::new(), + layers: Vec::new(), + channels: Vec::new(), + } + } + + pub fn write(&self) -> Vec { + let mut w = W::new(self.version); + w.u32(self.width); + w.u32(self.height); + w.u32(self.base_type); + if self.version >= 4 { + w.u32(self.precision); + } + let comp = match self.compression { + Compression::None => 0u8, + Compression::Rle => 1, + Compression::Zlib => 2, + }; + w.prop(17, &[comp]); + for (k, v) in &self.image_props { + w.prop(*k, v); + } + w.end(); + let layer_slots: Vec = self.layers.iter().map(|_| w.slot()).collect(); + w.ptr(0); + let channel_slots: Vec = self.channels.iter().map(|_| w.slot()).collect(); + w.ptr(0); + if self.version >= 18 { + w.ptr(0); + } + let bytes = match self.precision { + 100 | 150 => 1, + 200 | 250 | 500 | 550 => 2, + 300 | 350 | 600 | 650 => 4, + _ => 8, + }; + for (layer, slot) in self.layers.iter().zip(layer_slots) { + let at = w.out.len(); + w.patch(slot, at); + w.u32(layer.width); + w.u32(layer.height); + w.u32(layer.kind); + w.string(layer.name); + w.prop_u32(6, (layer.opacity * 255.0).round() as u32); + w.prop(33, &layer.opacity.to_be_bytes()); + w.prop_u32(8, u32::from(layer.visible)); + w.prop_u32(7, layer.mode); + let mut off = Vec::new(); + off.extend_from_slice(&layer.x.to_be_bytes()); + off.extend_from_slice(&layer.y.to_be_bytes()); + w.prop(15, &off); + if layer.mask.is_some() { + w.prop_u32(11, u32::from(layer.apply_mask)); + } + for (k, v) in &layer.props { + w.prop(*k, v); + } + w.end(); + let hslot = w.slot(); + let mslot = w.slot(); + if self.version >= 20 { + w.ptr(0); + } + let channels = match layer.kind { + 0 => 3, + 1 => 4, + 2 | 4 => 1, + _ => 2, + }; + if !layer.data.is_empty() { + let h = hierarchy(&mut w, layer.width, layer.height, channels * bytes, &layer.data, self.compression); + w.patch(hslot, h); + } + if let Some(mask) = &layer.mask { + let at = w.out.len(); + w.patch(mslot, at); + w.u32(layer.width); + w.u32(layer.height); + w.string("mask"); + w.end(); + let hs = w.slot(); + let h = hierarchy(&mut w, layer.width, layer.height, bytes, mask, self.compression); + w.patch(hs, h); + } + } + for ((name, data, selection), slot) in self.channels.iter().zip(channel_slots) { + let at = w.out.len(); + w.patch(slot, at); + w.u32(self.width); + w.u32(self.height); + w.string(name); + if *selection { + w.prop(4, &[]); + } + w.end(); + let hs = w.slot(); + let h = hierarchy(&mut w, self.width, self.height, bytes, data, self.compression); + w.patch(hs, h); + } + w.out + } +} + +fn open(bytes: &[u8]) -> Result { + import("test.xcf", bytes, &Interrupt::NONE) +} + +fn rgba(w: u32, h: u32, f: impl Fn(u32, u32) -> [u8; 4]) -> Vec { + let f = &f; + (0..h).flat_map(|y| (0..w).flat_map(move |x| f(x, y))).collect() +} + +fn pixel(layer: &Layer, x: i32, y: i32) -> [f32; 4] { + let LayerContent::Raster(s) = &layer.content else { panic!("not raster") }; + let px = s.read_region(Rect::new(x, y, x + 1, y + 1)); + [px[0], px[1], px[2], px[3]] +} + +#[test] +fn a_layer_decodes_with_every_compression() { + for compression in [Compression::None, Compression::Rle, Compression::Zlib] { + let mut img = TestImage::rgb8(70, 66); + img.compression = compression; + img.layers.push(TestLayer::rgba("A", 70, 66, rgba(70, 66, |x, y| [x as u8 * 3, y as u8 * 2, (x + y) as u8, 255 - x as u8]))); + let r = open(&img.write()).unwrap(); + let d = &r.document; + assert_eq!((d.size.width, d.size.height), (70, 66)); + assert_eq!(d.layers.len(), 1, "{compression:?}"); + for (x, y) in [(0, 0), (63, 0), (64, 0), (69, 65), (10, 64)] { + let p = pixel(&d.layers[0], x, y); + let want = [x as f32 * 3.0, y as f32 * 2.0, (x + y) as f32, 255.0 - x as f32].map(|v| v / 255.0); + assert!((0..4).all(|c| (p[c] - want[c]).abs() < 1e-6), "{compression:?} at {x},{y}: {p:?} vs {want:?}"); + } + assert!(r.source_read_only); + } +} + +#[test] +fn every_truncation_errors_never_panics() { + let mut img = TestImage::rgb8(20, 20); + img.layers.push(TestLayer::rgba("A", 20, 20, rgba(20, 20, |x, y| [x as u8, y as u8, 0, 255]))); + let bytes = img.write(); + for cut in 0..bytes.len() { + let _ = open(&bytes[..cut]); + } + assert!(open(&bytes).is_ok()); +} + +fn layer_at(layers: &[Layer], name: &str) -> Layer { + layers.iter().find(|l| l.name == name).cloned().unwrap_or_else(|| panic!("no layer {name}")) +} + +#[test] +fn properties_offsets_opacity_mode_visibility_locks_and_tags() { + let mut img = TestImage::rgb8(30, 20); + let mut l = TestLayer::rgba("Top", 10, 8, rgba(10, 8, |x, y| [x as u8 * 20, y as u8 * 30, 0, 200])); + l.x = -3; + l.y = 15; + l.opacity = 0.4; + l.visible = false; + l.mode = 30; // Multiply + l.props = vec![(10, 1u32.to_be_bytes().to_vec()), (28, 1u32.to_be_bytes().to_vec()), (32, 1u32.to_be_bytes().to_vec()), (34, 2u32.to_be_bytes().to_vec())]; + img.layers.push(l); + img.layers.push(TestLayer::rgba("Bottom", 30, 20, rgba(30, 20, |_, _| [9, 9, 9, 255]))); + let r = open(&img.write()).unwrap(); + let d = &r.document; + assert_eq!(d.layers.iter().map(|l| l.name.as_str()).collect::>(), ["Bottom", "Top"], "topmost in the file is last in the stack"); + let top = &d.layers[1]; + assert_eq!((top.blend, top.opacity, top.visible), (BlendMode::Multiply, 0.4, false)); + assert!(top.locks.transparency && top.locks.pixels && top.locks.position); + assert_eq!(top.label, LabelColor::Green); + // Offset (-3, 15): the layer's pixel (5, 2) sits at document (2, 17). + let p = pixel(top, 2, 17); + assert!((p[0] - 100.0 / 255.0).abs() < 1e-6 && (p[1] - 60.0 / 255.0).abs() < 1e-6 && (p[3] - 200.0 / 255.0).abs() < 1e-6, "{p:?}"); + assert_eq!(pixel(top, 2, 14)[3], 0.0, "above the layer"); +} + +#[test] +fn groups_follow_item_paths_and_pass_through() { + // File order: Top, Group (0), child A [0,0], Nested group [0,1], nested child [0,1,0], Bottom. + let mut img = TestImage::rgb8(16, 16); + let px = |v: u8| rgba(4, 4, move |_, _| [v, v, v, 255]); + img.layers.push(TestLayer::rgba("Top", 4, 4, px(1))); + let mut group = TestLayer::rgba("Group", 1, 1, Vec::new()); + group.props = vec![(29, vec![]), (31, 0u32.to_be_bytes().to_vec())]; + group.mode = 28; + img.layers.push(group); + let mut a = TestLayer::rgba("A", 4, 4, px(2)); + a.props = vec![(30, [1u32, 0].iter().flat_map(|v| v.to_be_bytes()).collect())]; + img.layers.push(a); + let mut nested = TestLayer::rgba("Nested", 1, 1, Vec::new()); + nested.props = vec![(29, vec![]), (30, [1u32, 1].iter().flat_map(|v| v.to_be_bytes()).collect())]; + nested.mode = 61; // Pass through + img.layers.push(nested); + let mut b = TestLayer::rgba("B", 4, 4, px(3)); + b.props = vec![(30, [1u32, 1, 0].iter().flat_map(|v| v.to_be_bytes()).collect())]; + img.layers.push(b); + img.layers.push(TestLayer::rgba("Bottom", 16, 16, rgba(16, 16, |_, _| [0, 0, 0, 255]))); + let d = open(&img.write()).unwrap().document; + assert_eq!(d.layers.iter().map(|l| l.name.as_str()).collect::>(), ["Bottom", "Group", "Top"]); + let g = &d.layers[1]; + assert!(g.is_group() && g.blend == BlendMode::Normal); + if let LayerContent::Group(gr) = &g.content { + assert!(!gr.expanded); + } + let children = g.children().unwrap(); + assert_eq!(children.iter().map(|l| l.name.as_str()).collect::>(), ["Nested", "A"], "bottom first inside the group too"); + assert_eq!(children[0].blend, BlendMode::PassThrough); + assert_eq!(children[0].children().unwrap()[0].name, "B"); + assert_eq!(pixel(&layer_at(children, "A"), 0, 0)[0], 2.0 / 255.0); +} + +#[test] +fn a_path_into_nothing_lands_at_the_top_level_with_a_note() { + let mut img = TestImage::rgb8(8, 8); + let mut stray = TestLayer::rgba("Stray", 2, 2, rgba(2, 2, |_, _| [5, 5, 5, 255])); + stray.props = vec![(30, [7u32, 0].iter().flat_map(|v| v.to_be_bytes()).collect())]; + img.layers.push(stray); + img.layers.push(TestLayer::rgba("Bottom", 8, 8, rgba(8, 8, |_, _| [0, 0, 0, 255]))); + let r = open(&img.write()).unwrap(); + assert_eq!(r.document.layers.len(), 2); + assert!(r.warnings.iter().any(|w| w.contains("item path")), "{:?}", r.warnings); +} + +#[test] +fn masks_are_read_at_the_layer_offset_applied_or_not() { + for apply in [true, false] { + let mut img = TestImage::rgb8(20, 20); + let mut l = TestLayer::rgba("Masked", 6, 6, rgba(6, 6, |_, _| [255, 0, 0, 255])); + l.x = 4; + l.y = 5; + l.mask = Some((0..36).map(|i| (i * 7) as u8).collect()); + l.apply_mask = apply; + img.layers.push(l); + let d = open(&img.write()).unwrap().document; + let mask = d.layers[0].mask.as_ref().expect("mask"); + assert_eq!(mask.enabled, apply); + assert_eq!(mask.surface.read_region(Rect::new(4 + 3, 5 + 2, 4 + 4, 5 + 3))[0], (2 * 6 + 3) as f32 * 7.0 / 255.0); + } +} + +#[test] +fn sixteen_bit_and_float_samples_are_big_endian() { + // 16-bit gamma integer (250): one 2x1 gray+alpha layer. + let mut img = TestImage::rgb8(2, 1); + img.base_type = 1; + img.precision = 250; + let mut data = Vec::new(); + for v in [0x1234u16, 0xffff, 0x8000, 0x0001] { + data.extend_from_slice(&v.to_be_bytes()); + } + let mut l = TestLayer::rgba("G", 2, 1, data); + l.kind = 3; // gray with alpha + img.layers.push(l); + let d = open(&img.write()).unwrap().document; + assert_eq!((d.mode, d.depth), (ColorMode::Grayscale, SampleType::U16)); + let p = pixel_n(&d.layers[0], 0, 0, 2); + assert!((p[0] - 0x1234 as f32 / 65535.0).abs() < 1e-6 && (p[1] - 1.0).abs() < 1e-6, "{p:?}"); + let p = pixel_n(&d.layers[0], 1, 0, 2); + assert!((p[0] - 0x8000 as f32 / 65535.0).abs() < 1e-6 && (p[1] - 1.0 / 65535.0).abs() < 1e-6, "{p:?}"); + + // 32-bit float linear (600): RGB without alpha, values above 1 kept, linear profile. + let mut img = TestImage::rgb8(1, 1); + img.precision = 600; + let mut data = Vec::new(); + for v in [0.25f32, 1.5, -0.5] { + data.extend_from_slice(&v.to_be_bytes()); + } + let mut l = TestLayer::rgba("F", 1, 1, data); + l.kind = 0; + img.layers.push(l); + let r = open(&img.write()).unwrap(); + let d = &r.document; + assert_eq!(d.depth, SampleType::F32); + assert_eq!(pixel(&d.layers[0], 0, 0), [0.25, 1.5, -0.5, 1.0]); + assert!(d.icc_profile.is_some(), "linear light gets a linear profile"); + assert!(d.layers[0].locks.transparency, "no alpha channel in the file: the layer is opaque"); + + // 16-bit float (500) and 64-bit float (700) decode too. + for (precision, bytes) in [(500u32, f16::from_f32(0.75).to_bits().to_be_bytes().to_vec()), (700, 0.75f64.to_be_bytes().to_vec())] { + let mut img = TestImage::rgb8(1, 1); + img.base_type = 1; + img.precision = precision; + let mut l = TestLayer::rgba("F", 1, 1, bytes); + l.kind = 2; + img.layers.push(l); + let d = open(&img.write()).unwrap().document; + assert_eq!(pixel_n(&d.layers[0], 0, 0, 2)[0], 0.75, "precision {precision}"); + } +} + +fn pixel_n(layer: &Layer, x: i32, y: i32, n: usize) -> Vec { + let LayerContent::Raster(s) = &layer.content else { panic!("not raster") }; + let px = s.read_region(Rect::new(x, y, x + 1, y + 1)); + assert_eq!(px.len(), n); + px +} + +#[test] +fn indexed_images_use_the_colormap() { + let mut img = TestImage::rgb8(2, 1); + img.base_type = 2; + let mut cmap = 3u32.to_be_bytes().to_vec(); + cmap.extend_from_slice(&[255, 0, 0, 0, 255, 0, 0, 0, 255]); + img.image_props.push((1, cmap)); + let mut l = TestLayer::rgba("I", 2, 1, vec![2, 255, 1, 128]); + l.kind = 5; // indexed with alpha + img.layers.push(l); + let r = open(&img.write()).unwrap(); + assert_eq!(r.document.mode, ColorMode::Rgb); + assert_eq!(pixel(&r.document.layers[0], 0, 0), [0.0, 0.0, 1.0, 1.0]); + assert_eq!(pixel(&r.document.layers[0], 1, 0), [0.0, 1.0, 0.0, 128.0 / 255.0]); + assert!(r.warnings.iter().any(|w| w.contains("indexed"))); +} + +#[test] +fn channels_become_alpha_channels_and_the_selection() { + let mut img = TestImage::rgb8(4, 4); + img.layers.push(TestLayer::rgba("L", 4, 4, rgba(4, 4, |_, _| [0, 0, 0, 255]))); + img.channels.push(("Extra", (0..16).map(|i| i as u8 * 16).collect(), false)); + img.channels.push(("Selection Mask", vec![255; 16], true)); + let d = open(&img.write()).unwrap().document; + assert_eq!(d.channels.len(), 1); + assert_eq!(d.channels[0].name, "Extra"); + assert_eq!(d.channels[0].surface.read_region(Rect::new(3, 1, 4, 2))[0], 7.0 * 16.0 / 255.0); + assert_eq!(d.selection.as_ref().unwrap().read_region(Rect::new(0, 0, 1, 1))[0], 1.0); +} + +#[test] +fn image_parasites_resolution_guides_and_comment() { + let mut img = TestImage::rgb8(10, 10); + let mut res = Vec::new(); + res.extend_from_slice(&300.0f32.to_be_bytes()); + res.extend_from_slice(&300.0f32.to_be_bytes()); + img.image_props.push((19, res)); + let mut guides = Vec::new(); + guides.extend_from_slice(&4i32.to_be_bytes()); + guides.push(1); + guides.extend_from_slice(&7i32.to_be_bytes()); + guides.push(2); + img.image_props.push((18, guides)); + let mut parasites = Vec::new(); + for (name, data) in [("gimp-comment", &b"hello\0"[..]), ("icc-profile", &[1u8, 2, 3][..])] { + parasites.extend_from_slice(&(name.len() as u32 + 1).to_be_bytes()); + parasites.extend_from_slice(name.as_bytes()); + parasites.push(0); + parasites.extend_from_slice(&0u32.to_be_bytes()); + parasites.extend_from_slice(&(data.len() as u32).to_be_bytes()); + parasites.extend_from_slice(data); + } + img.image_props.push((21, parasites)); + // An unknown property is skipped by its length. + img.image_props.push((99, vec![1, 2, 3, 4, 5])); + img.layers.push(TestLayer::rgba("L", 10, 10, rgba(10, 10, |_, _| [0, 0, 0, 255]))); + let d = open(&img.write()).unwrap().document; + assert_eq!(d.resolution_dpi, 300.0); + assert_eq!((d.guides.horizontal.as_slice(), d.guides.vertical.as_slice()), (&[4.0][..], &[7.0][..])); + assert_eq!(d.metadata.text, vec![("Comment".to_string(), "hello".to_string())]); + assert_eq!(d.icc_profile.as_deref().map(|p| p.as_slice()), Some(&[1u8, 2, 3][..])); +} + +#[test] +fn clipping_text_vector_effects_and_floating_selections_are_reported() { + let mut img = TestImage::rgb8(4, 4); + img.version = 20; + let mut clipped = TestLayer::rgba("Clipped", 4, 4, rgba(4, 4, |_, _| [1, 1, 1, 255])); + let mut parasite = Vec::new(); + parasite.extend_from_slice(&("gimphoto-clipped".len() as u32 + 1).to_be_bytes()); + parasite.extend_from_slice(b"gimphoto-clipped\0"); + parasite.extend_from_slice(&0u32.to_be_bytes()); + parasite.extend_from_slice(&1u32.to_be_bytes()); + parasite.push(1); + clipped.props = vec![(21, parasite), (26, 1u32.to_be_bytes().to_vec()), (47, vec![0; 8])]; + img.layers.push(clipped); + let mut floating = TestLayer::rgba("Floating Selection", 2, 2, rgba(2, 2, |_, _| [2, 2, 2, 255])); + floating.props = vec![(5, vec![0; 8])]; + img.layers.push(floating); + img.layers.push(TestLayer::rgba("Bottom", 4, 4, rgba(4, 4, |_, _| [0, 0, 0, 255]))); + let r = open(&img.write()).unwrap(); + assert_eq!(r.document.layers.iter().map(|l| l.name.as_str()).collect::>(), ["Bottom", "Clipped"], "the floating selection is left out"); + assert!(r.document.layers[1].clipped); + let w = r.warnings.join("\n"); + assert!(w.contains("text layer") && w.contains("vector layer") && w.contains("floating selection"), "{w}"); +} + +/// #8857: GIMP's link-layer length omits one u32. Trusting it shifts the hierarchy pointer +/// by four bytes, even though the cached pixels and every offset in the file are valid. +#[test] +fn linked_layers_open_cached_pixels_with_short_or_correct_property_lengths() { + for (precision, depth, data) in [ + (150, SampleType::U8, vec![51, 102, 153, 255]), + (250, SampleType::U16, [13107u16, 26214, 39321, 65535].into_iter().flat_map(u16::to_be_bytes).collect()), + (600, SampleType::F32, [0.2f32, 0.4, 0.6, 1.0].into_iter().flat_map(f32::to_be_bytes).collect()), + ] { + for (path, width, height) in [("assets/étiquette.svg", 2000, 1270), ("assets/label.png", 0, 0), ("", 0, 0)] { + let mut payload = W { out: Vec::new(), wide: true }; + payload.u32(1); + payload.string(path); + payload.u32(width); + payload.u32(height); + let payload = payload.out; + let marker = [48u32.to_be_bytes(), (payload.len() as u32).to_be_bytes()].concat(); + let mut img = TestImage::rgb8(1, 1); + img.version = 25; + img.precision = precision; + let mut linked = TestLayer::rgba("Linked", 1, 1, data.clone()); + // Later properties must still be read, including unknown ones skipped by length. + linked.props = vec![(48, payload.clone()), (99, vec![1, 2, 3]), (33, 0.375f32.to_be_bytes().to_vec()), (8, 0u32.to_be_bytes().to_vec())]; + img.layers.push(linked); + img.layers.push(TestLayer::rgba("Bottom", 1, 1, data.clone())); + for short in [false, true] { + let mut bytes = img.write(); + let at = bytes.windows(marker.len()).position(|w| w == marker).unwrap(); + if short { + bytes[at + 4..at + 8].copy_from_slice(&((payload.len() - 4) as u32).to_be_bytes()); + } + let r = open(&bytes).unwrap_or_else(|e| panic!("precision {precision}, path {path:?}, short {short}: {e}")); + assert!(r.source_read_only); + assert_eq!(r.document.depth, depth); + assert_eq!(r.document.layers.len(), 2); + let linked = layer_at(&r.document.layers, "Linked"); + assert_eq!(linked.opacity, 0.375); + assert!(!linked.visible); + for layer in [linked, layer_at(&r.document.layers, "Bottom")] { + for (got, expected) in pixel(&layer, 0, 0).into_iter().zip([0.2, 0.4, 0.6, 1.0]) { + assert!((got - expected).abs() < 1e-5, "precision {precision}: {got} != {expected}"); + } + } + assert!(r.warnings.iter().any(|w| w.contains("is a link layer and opens as pixels"))); + } + } + } +} + +#[test] +fn truncated_and_oversized_link_layer_fields_are_errors() { + let mut payload = W { out: Vec::new(), wide: true }; + payload.u32(1); + payload.string("assets/label.svg"); + payload.u32(2000); + payload.u32(1270); + let payload = payload.out; + let marker = [48u32.to_be_bytes(), (payload.len() as u32).to_be_bytes()].concat(); + let mut img = TestImage::rgb8(1, 1); + img.version = 25; + let mut linked = TestLayer::rgba("Linked", 1, 1, vec![51, 102, 153, 255]); + linked.props.push((48, payload.clone())); + img.layers.push(linked); + let bytes = img.write(); + let start = bytes.windows(marker.len()).position(|w| w == marker).unwrap() + 8; + // Cut within the flags, string length, path and either dimension. + for end in start..start + payload.len() { + assert!(open(&bytes[..end]).is_err(), "truncated at {end}"); + } + for len in [(MAX_STRING + 1) as u32, u32::MAX] { + let mut oversized = bytes.clone(); + oversized[start + 4..start + 8].copy_from_slice(&len.to_be_bytes()); + assert!(open(&oversized).is_err(), "path length {len}"); + } +} + +#[test] +fn version_3_files_use_32_bit_pointers_and_legacy_modes() { + let mut img = TestImage::rgb8(5, 5); + img.version = 3; + let mut l = TestLayer::rgba("Old", 5, 5, rgba(5, 5, |x, _| [x as u8, 0, 0, 255])); + l.mode = 3; // Multiply (legacy) + img.layers.push(l); + let bytes = img.write(); + assert!(bytes.starts_with(b"gimp xcf v003\0")); + let d = open(&bytes).unwrap().document; + assert_eq!((d.depth, d.layers[0].blend), (SampleType::U8, BlendMode::Multiply)); + assert_eq!(pixel(&d.layers[0], 4, 0)[0], 4.0 / 255.0); + // Version 0 ("file") as well. + let mut img0 = TestImage::rgb8(2, 2); + img0.version = 0; + img0.layers.push(TestLayer::rgba("Oldest", 2, 2, rgba(2, 2, |_, _| [7, 7, 7, 255]))); + assert!(open(&img0.write()).unwrap().document.layers.len() == 1); +} + +#[test] +fn rle_long_runs_and_literals_round_trip() { + // A tile with runs over 127 and long literal stretches, in every byte plane. + let w = 64; + let data = rgba(w, 64, |x, y| if y < 40 { [200, 1, 2, 255] } else { [(x * 7 + y) as u8, (x * 3) as u8, y as u8, 255] }); + let mut img = TestImage::rgb8(w, 64); + img.layers.push(TestLayer::rgba("R", w, 64, data.clone())); + let d = open(&img.write()).unwrap().document; + let got = pixel(&d.layers[0], 63, 63); + assert!((got[0] - (63 * 7 + 63) as u8 as f32 / 255.0).abs() < 1e-6 && (got[1] - 189.0 / 255.0).abs() < 1e-6, "{got:?}"); + assert_eq!(pixel(&d.layers[0], 10, 10), [200.0 / 255.0, 1.0 / 255.0, 2.0 / 255.0, 1.0]); +} + +#[test] +fn limits_and_malformed_structures_are_errors() { + // A canvas past the side limit. + let mut big = TestImage::rgb8(1, 1); + big.width = MAX_SIDE + 1; + assert!(open(&big.write()).is_err()); + // A layer whose declared size has no pixels behind it (hierarchy pointer to garbage). + let mut img = TestImage::rgb8(4, 4); + img.layers.push(TestLayer::rgba("L", 4, 4, rgba(4, 4, |_, _| [0; 4]))); + let mut bytes = img.write(); + let n = bytes.len(); + bytes[n - 10..].fill(0xff); + assert!(open(&bytes).is_err()); + // Wrong signature, unknown version tag, unknown compression. + assert!(matches!(open(b"gimp xcf vXYZ\0"), Err(IoError::Xcf(_)))); + assert!(open(b"not an xcf").is_err()); + let mut img = TestImage::rgb8(2, 2); + img.layers.push(TestLayer::rgba("L", 2, 2, rgba(2, 2, |_, _| [0; 4]))); + let mut bytes = img.write(); + let at = 14 + 12 + 4 + 8; + bytes[at] = 3; // compression byte: reserved + assert!(open(&bytes).is_err()); +} + +#[test] +fn gzipped_files_are_recognised_by_name_and_inflated() { + use std::io::Write; + let mut img = TestImage::rgb8(3, 3); + img.layers.push(TestLayer::rgba("L", 3, 3, rgba(3, 3, |_, _| [4, 5, 6, 255]))); + let plain = img.write(); + let mut e = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + e.write_all(&plain).unwrap(); + let gz = e.finish().unwrap(); + assert!(is_gzipped_xcf("a/b/photo.XCF.GZ", &gz) && is_gzipped_xcf("x.xcfgz", &gz)); + assert!(!is_gzipped_xcf("photo.xcf", &gz) && !is_gzipped_xcf("photo.xcf.gz", &plain)); + let d = crate::import("photo.xcf.gz", &gz).unwrap().document; + assert_eq!(pixel(&d.layers[0], 1, 1), [4.0 / 255.0, 5.0 / 255.0, 6.0 / 255.0, 1.0]); + assert!(gunzip(&gz[..gz.len() / 2]).is_err() || crate::import("photo.xcf.gz", &gz[..gz.len() / 2]).is_err()); +} + +/// A recognized raster signature wins over a misleading `.xcf` suffix, as with `.kra` and `.svg`. +#[test] +fn a_png_with_an_xcf_suffix_opens_by_content() { + let img = + photocraft_codecs::Image::from_raw(12, 8, photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8, vec![90; 12 * 8 * 4]).unwrap(); + let png = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + let r = crate::import("renamed.xcf", &png).unwrap(); + assert_eq!((r.document.size.width, r.document.size.height), (12, 8)); + assert!(crate::import("broken.xcf", b"not an image at all").is_err()); +} + +/// `PROP_FLOAT_OPACITY` wins over `PROP_OPACITY` whichever comes first in the file. +#[test] +fn float_opacity_wins_over_opacity_in_either_order() { + let mut img = TestImage::rgb8(4, 4); + // The writer emits OPACITY (0.4 → 102) then FLOAT_OPACITY 0.4; an OPACITY 255 after them must not win. + let mut byte_last = TestLayer::rgba("Float then byte", 4, 4, rgba(4, 4, |_, _| [1, 2, 3, 255])); + byte_last.opacity = 0.4; + byte_last.props = vec![(6, 255u32.to_be_bytes().to_vec())]; + // A later FLOAT_OPACITY replaces the earlier one. + let mut float_last = TestLayer::rgba("Byte then float", 4, 4, rgba(4, 4, |_, _| [1, 2, 3, 255])); + float_last.opacity = 0.4; + float_last.props = vec![(33, 0.25f32.to_be_bytes().to_vec())]; + img.layers.push(byte_last); + img.layers.push(float_last); + let d = open(&img.write()).unwrap().document; + assert_eq!(layer_at(&d.layers, "Float then byte").opacity, 0.4); + assert_eq!(layer_at(&d.layers, "Byte then float").opacity, 0.25); +} + +/// A path that fails below the top level (here at depth 2) still lands at the top level, so the +/// group it would have been pushed into keeps its indices for the siblings placed after it. +#[test] +fn a_path_failing_below_the_top_level_lands_at_the_top_level() { + let mut img = TestImage::rgb8(8, 8); + let px = |v: u8| rgba(2, 2, move |_, _| [v, v, v, 255]); + let mut group = TestLayer::rgba("Group", 1, 1, Vec::new()); + group.props = vec![(29, vec![]), (31, 0u32.to_be_bytes().to_vec())]; + img.layers.push(group); + let mut stray = TestLayer::rgba("Stray", 2, 2, px(5)); + stray.props = vec![(30, [0u32, 4, 0].iter().flat_map(|v| v.to_be_bytes()).collect())]; + img.layers.push(stray); + let mut a = TestLayer::rgba("A", 2, 2, px(6)); + a.props = vec![(30, [0u32, 0].iter().flat_map(|v| v.to_be_bytes()).collect())]; + img.layers.push(a); + img.layers.push(TestLayer::rgba("Bottom", 8, 8, rgba(8, 8, |_, _| [0, 0, 0, 255]))); + let r = open(&img.write()).unwrap(); + let d = &r.document; + assert_eq!(d.layers.iter().map(|l| l.name.as_str()).collect::>(), ["Bottom", "Stray", "Group"]); + let children = layer_at(&d.layers, "Group").children().unwrap().iter().map(|l| l.name.clone()).collect::>(); + assert_eq!(children, ["A"], "the valid sibling placed after the stray one still lands in the group"); + assert!(r.warnings.iter().any(|w| w.contains("Stray") && w.contains("item path")), "{:?}", r.warnings); +} + +/// A parasite name field: u32 `len+1`, then the bytes and a trailing NUL. +fn parasite_name(out: &mut Vec, name: &str) { + out.extend_from_slice(&((name.len() as u32) + 1).to_be_bytes()); + out.extend_from_slice(name.as_bytes()); + out.push(0); +} + +/// One PROP_PARASITES record: name, u32 flags, u32 len, then the payload. +fn parasite_record(out: &mut Vec, name: &str, payload: &[u8]) { + parasite_name(out, name); + out.extend_from_slice(&0u32.to_be_bytes()); + out.extend_from_slice(&(payload.len() as u32).to_be_bytes()); + out.extend_from_slice(payload); +} + +#[test] +fn duplicate_parasites_keep_order_and_last_value() { + // Duplicate names must resolve to the last payload while keeping first-seen order. + // This is the behavior the name->index map must preserve versus the old linear scan. + let mut data = Vec::new(); + parasite_record(&mut data, "gimp-comment", b"first"); + parasite_record(&mut data, "gimp-image-grid", b"grid"); + parasite_record(&mut data, "gimp-comment", b"second"); + let out = parasites(&data); + assert_eq!(out.len(), 2, "duplicate collapses to one slot"); + assert_eq!(out[0].0, "gimp-comment"); + assert_eq!(out[0].1, b"second", "last value wins"); + assert_eq!(out[1].0, "gimp-image-grid", "insertion order preserved"); +} diff --git a/crates/io/src/xml.rs b/crates/io/src/xml.rs new file mode 100644 index 0000000000..13e02004d1 --- /dev/null +++ b/crates/io/src/xml.rs @@ -0,0 +1,156 @@ +//! XML parsing with a nesting cap. roxmltree recurses once per element level, so a few thousand +//! nested tags (a 30 KB file or command param) overflow a 2 MB thread stack, an abort no guard +//! can catch. Every roxmltree caller that reads a file or a param goes through [`parse`]. + +use roxmltree::{Document, ParsingOptions}; + +/// Deepest element nesting accepted, the same as for SVG drawings: real files stay far below. +pub const MAX_DEPTH: usize = crate::svg::MAX_XML_DEPTH; + +/// Parses `text`, refusing nesting deeper than [`MAX_DEPTH`] before roxmltree sees it. +/// +/// With `opts.allow_dtd` an internal DTD subset is refused too: its entities can expand into +/// markup the depth check never sees. An external DOCTYPE (a public or system id) still parses. +pub fn parse<'i>(text: &'i str, opts: ParsingOptions<'i>) -> Result, String> { + if opts.allow_dtd && internal_subset(text.as_bytes()) { + return Err("internal DTD subsets aren't supported".into()); + } + if crate::svg::nesting_depth(text.as_bytes(), MAX_DEPTH) > MAX_DEPTH { + return Err(format!("elements are nested more than {MAX_DEPTH} levels deep")); + } + Document::parse_with_options(text, opts).map_err(|e| e.to_string()) +} + +/// Does a DOCTYPE have an internal subset (``)? Every `` in the XML +/// declaration must not hide it. One inside a comment may be refused needlessly; that fails safe. +/// +/// Each `` ends a DOCTYPE without one. Scans in +/// the same quote state go on alike, so one pass keeps just three flags (outside quotes, in `"`, +/// in `'`) and stays linear however many ` bool { + const DOCTYPE: &[u8] = b"')) || (double && b == b'"') || (single && b == b'\''), + (double && b != b'"') || (bare && b == b'"'), + (single && b != b'\'') || (bare && b == b'\''), + ); + } + false +} + +#[cfg(test)] +mod tests { + use super::*; + + fn nested(levels: usize) -> String { + format!("{}{}", "".repeat(levels), "".repeat(levels)) + } + + fn dtd<'a>() -> ParsingOptions<'a> { + ParsingOptions { allow_dtd: true, ..Default::default() } + } + + #[test] + fn nesting_past_the_cap_is_an_error_not_a_stack_overflow() { + let deep = nested(20_000); + std::thread::Builder::new() + .stack_size(512 * 1024) + .spawn(move || { + for opts in [ParsingOptions::default(), dtd()] { + let e = parse(&deep, opts).err().unwrap_or_else(|| panic!("parsed")); + assert!(e.contains("nested") && e.contains(&MAX_DEPTH.to_string()), "{e}"); + } + }) + .unwrap() + .join() + .unwrap(); + // The root plus MAX_DEPTH - 1 levels is exactly MAX_DEPTH deep. + assert!(parse(&nested(MAX_DEPTH - 1), ParsingOptions::default()).is_ok()); + assert!(parse(&nested(MAX_DEPTH), ParsingOptions::default()).is_err()); + } + + #[test] + fn markup_in_comments_cdata_pis_and_attributes_adds_no_depth() { + let g = "".repeat(2 * MAX_DEPTH); + let xml = format!("\">"); + assert!(parse(&xml, ParsingOptions::default()).is_ok()); + } + + /// The scan per ` bool { + const DOCTYPE: &[u8] = b" quote = None, + Some(_) => {} + None if b == b'"' || b == b'\'' => quote = Some(b), + None if b == b'[' => return true, + None if b == b'>' => return false, + None => {} + } + } + false + }) + } + + #[test] + fn the_one_pass_doctype_scan_agrees_with_a_scan_per_doctype() { + let parts: [&[u8]; 7] = [b"", b"a", b"", "\n\n"; + assert!(parse(krita, dtd()).is_ok()); + let g = ("".repeat(20_000), "".repeat(20_000)); + for xml in [ + "]>&e;".to_string(), + "\u{feff} ]>".to_string(), + format!("]>&e;", g.0, g.1), + // roxmltree reads the declaration's values as quoted attributes, so they may hold `?>`. + format!("\"?>]>&e;", g.0, g.1), + format!("\"?>]>{}{}", g.0, g.1), + ] { + let e = parse(&xml, dtd()).err().unwrap_or_else(|| panic!("parsed {xml:.60}")); + assert!(e.contains("internal DTD"), "{e}"); + } + // Without allow_dtd roxmltree refuses any DOCTYPE itself. + assert!(parse(krita, ParsingOptions::default()).is_err()); + } +} diff --git a/crates/io/tests/adjust_warnings.rs b/crates/io/tests/adjust_warnings.rs deleted file mode 100644 index 9b91f40bcf..0000000000 --- a/crates/io/tests/adjust_warnings.rs +++ /dev/null @@ -1,42 +0,0 @@ -//! Opening a file whose adjustment layer can't be read warns, naming the layer and the reason, -//! instead of opening it silently without its effect (#1763). - -use photocraft_color::{ColorMode, SampleType}; -use photocraft_doc::{Adjustment, Document, Layer, LayerContent}; -use photocraft_geom::Size; -use photocraft_io::*; - -fn reopen(adj: Adjustment, path: &str, opts: &ExportOptions) -> (Document, Vec) { - let mut d = Document::new("a", Size::new(16, 16), ColorMode::Rgb, SampleType::U8); - d.layers.push(Layer::new("Teal Look", LayerContent::Adjustment(adj))); - let r = export(&d, path, opts).expect("export"); - let imp = import(path, &r.bytes).expect("import"); - (imp.document, imp.warnings) -} - -#[test] -fn unreadable_adjustments_warn_on_open() { - // A Color Lookup that uses a profile, not a LUT file: kept verbatim, not applied. - let profile = Adjustment::Unsupported { psd_key: "clrL".into(), raw: vec![0, 2, 0, 0] }; - for (path, opts) in [("x.psd", ExportOptions::default()), ("x.psb", ExportOptions { force_psb: true, ..Default::default() })] { - let (doc, warnings) = reopen(profile.clone(), path, &opts); - assert!(matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::Unsupported { .. })), "{path}"); - let w: Vec<&String> = warnings.iter().filter(|w| w.contains("Teal Look")).collect(); - assert_eq!(w.len(), 1, "{path}: {warnings:?}"); - assert!(w[0].contains("Color Lookup") && w[0].contains("version 2") && w[0].contains("no effect"), "{path}: {}", w[0]); - } - // Another kind without a specific reason still names the layer and the adjustment. - let (_, warnings) = reopen(Adjustment::Unsupported { psd_key: "levl".into(), raw: vec![0, 9] }, "x.psd", &ExportOptions::default()); - assert!(warnings.iter().any(|w| w.contains("Teal Look") && w.contains("Levels settings could not be read;")), "{warnings:?}"); -} - -#[test] -fn readable_adjustments_open_without_warnings() { - let lut: Vec = (0..8).flat_map(|i| [(i & 1) as f32, ((i >> 1) & 1) as f32, ((i >> 2) & 1) as f32]).collect(); - let lookup = Adjustment::ColorLookup { name: "id.cube".into(), size: 2, lut: Some(std::sync::Arc::new(lut)), tetrahedral: false, dither: false }; - for adj in [lookup, Adjustment::Invert] { - let (doc, warnings) = reopen(adj, "x.psd", &ExportOptions::default()); - assert!(!matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::Unsupported { .. }))); - assert!(!warnings.iter().any(|w| w.contains("Teal Look")), "{warnings:?}"); - } -} diff --git a/crates/io/tests/adjust_blend_roundtrip.rs b/crates/io/tests/it/adjust_blend_roundtrip.rs similarity index 94% rename from crates/io/tests/adjust_blend_roundtrip.rs rename to crates/io/tests/it/adjust_blend_roundtrip.rs index 4e8b727af2..bab904f58c 100644 --- a/crates/io/tests/adjust_blend_roundtrip.rs +++ b/crates/io/tests/it/adjust_blend_roundtrip.rs @@ -4,7 +4,7 @@ //! document flattens to the same composite (within 1/255) in every case, so nothing a user made //! in PhotoCraft silently disappears or changes when Photoshop opens the file. -mod common; +use crate::common; use std::sync::Arc; @@ -80,9 +80,20 @@ fn adjustments() -> Vec<(Adjustment, bool)> { (Adjustment::PhotoFilter { color: [q16(0), q16(39321), q16(65535)], density: 0.3, preserve_luminosity: false }, true), (Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false }, true), (Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], monochrome: true }, true), - (Adjustment::ColorLookup { name: "Look.cube".into(), lut: Some(Arc::new(lut.data)), size: 5, tetrahedral: false, dither: false }, true), + // The LUT file is embedded as loaded (here a .cube), so it reads back with its source. + ( + Adjustment::ColorLookup { + name: "Look.cube".into(), + source: Some(adjust::LutSource { format: adjust::LutFormat::Cube, bytes: Arc::new(photocraft_cms::lutfile::write_cube(&lut).into_bytes()) }), + lut: Some(Arc::new(lut.data)), + size: 5, + tetrahedral: false, + dither: false, + }, + true, + ), // No table chosen: written as an identity cube (renders the same, comes back with a table). - (Adjustment::ColorLookup { name: String::new(), lut: None, size: 0, tetrahedral: false, dither: false }, false), + (Adjustment::ColorLookup { name: String::new(), lut: None, size: 0, tetrahedral: false, dither: false, source: None }, false), (Adjustment::Invert, true), (Adjustment::Posterize { levels: 4 }, true), (Adjustment::Threshold { level: g(140) }, true), @@ -101,7 +112,7 @@ fn adjustments() -> Vec<(Adjustment, bool)> { (Adjustment::SelectiveColor { relative: true, adjustments: std::array::from_fn(|r| [r as f32 * 10.0 - 40.0, 5.0, -30.0, 20.0]) }, true), (Adjustment::SelectiveColor { relative: false, adjustments: std::array::from_fn(|r| [0.0, r as f32 * 5.0, 0.0, -10.0]) }, true), // A block this version cannot read (a truncated Selective Color) is kept verbatim. - (Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![0, 1, 0, 0] }, true), + (Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![0, 1, 0, 0].into() }, true), ]; // Compile-time guard: a new Adjustment kind must be added to the list above. for (a, _) in &all { @@ -207,7 +218,8 @@ fn every_adjustment_kind_round_trips_16bit() { fn color_lookup_tetrahedral_warns_on_psd_save() { let lut = photocraft_cms::lutfile::LutFile::from_fn("t", 2, |c| if c == [1.0; 3] { [1.0; 3] } else { [0.0; 3] }); for tetrahedral in [true, false] { - let adj = Adjustment::ColorLookup { name: "test.cube".into(), lut: Some(Arc::new(lut.data.clone())), size: 2, tetrahedral, dither: false }; + let adj = + Adjustment::ColorLookup { name: "test.cube".into(), lut: Some(Arc::new(lut.data.clone())), size: 2, tetrahedral, dither: false, source: None }; let mut d = Document::new("lut", photocraft_geom::Size::new(8, 8), ColorMode::Rgb, SampleType::U8); let fmt = d.pixel_format(); d.layers.push(raster("Background", fmt, d.bounds(), 1, false)); diff --git a/crates/io/tests/it/adjust_warnings.rs b/crates/io/tests/it/adjust_warnings.rs new file mode 100644 index 0000000000..0e4e822527 --- /dev/null +++ b/crates/io/tests/it/adjust_warnings.rs @@ -0,0 +1,232 @@ +//! Opening a file whose adjustment layer can't be read warns, naming the layer and the reason, +//! instead of opening it silently without its effect (#1763). + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Adjustment, Document, Layer, LayerContent}; +use photocraft_geom::Size; +use photocraft_io::*; +use photocraft_psd::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; +use photocraft_psd::{Compression, LayerFlags, LayerRecord, PsdFile, TaggedBlock, Version, testgen}; +use photocraft_raster::Surface; + +fn reopen(adj: Adjustment, path: &str, opts: &ExportOptions) -> (Document, Vec) { + let mut d = Document::new("a", Size::new(16, 16), ColorMode::Rgb, SampleType::U8); + d.layers.push(Layer::new("Teal Look", LayerContent::Adjustment(adj))); + let r = export(&d, path, opts).expect("export"); + let imp = import(path, &r.bytes).expect("import"); + (imp.document, imp.warnings) +} + +#[test] +fn unreadable_adjustments_warn_on_open() { + // A Color Lookup that uses a profile, not a LUT file: kept verbatim, not applied. + let profile = Adjustment::Unsupported { psd_key: "clrL".into(), raw: vec![0, 2, 0, 0].into() }; + for (path, opts) in [("x.psd", ExportOptions::default()), ("x.psb", ExportOptions { force_psb: true, ..Default::default() })] { + let (doc, warnings) = reopen(profile.clone(), path, &opts); + assert!(matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::Unsupported { .. })), "{path}"); + let w: Vec<&String> = warnings.iter().filter(|w| w.contains("Teal Look")).collect(); + assert_eq!(w.len(), 1, "{path}: {warnings:?}"); + assert!(w[0].contains("Color Lookup") && w[0].contains("version 2") && w[0].contains("no effect"), "{path}: {}", w[0]); + } + // Another kind without a specific reason still names the layer and the adjustment. + let (_, warnings) = reopen(Adjustment::Unsupported { psd_key: "levl".into(), raw: vec![0, 9].into() }, "x.psd", &ExportOptions::default()); + assert!(warnings.iter().any(|w| w.contains("Teal Look") && w.contains("Levels settings could not be read;")), "{warnings:?}"); +} + +#[test] +fn readable_adjustments_open_without_warnings() { + let lut: Vec = (0..8).flat_map(|i| [(i & 1) as f32, ((i >> 1) & 1) as f32, ((i >> 2) & 1) as f32]).collect(); + let lookup = + Adjustment::ColorLookup { name: "id.cube".into(), size: 2, lut: Some(std::sync::Arc::new(lut)), tetrahedral: false, dither: false, source: None }; + for adj in [lookup, Adjustment::Invert] { + let (doc, warnings) = reopen(adj, "x.psd", &ExportOptions::default()); + assert!(!matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::Unsupported { .. }))); + assert!(!warnings.iter().any(|w| w.contains("Teal Look")), "{warnings:?}"); + } +} + +/// The raw bytes of a `clrL` block laid out the way Photoshop writes it (Adobe spec): version 1, +/// then a version 16 descriptor carrying a small `.cube` LUT file (its entry for red maps to +/// cyan, so the table is not the identity). +fn photoshop_clr_l_block() -> Vec { + let cube = b"LUT_3D_SIZE 2\n0 0 0\n0 1 1\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"; + photoshop_clr_l_block_from_cube(cube) +} + +fn photoshop_clr_l_block_from_cube(cube: &[u8]) -> Vec { + let en = |t: &str, val: &str| Value::Enumerated { type_id: Id::new(t), value: Id::new(val) }; + let text = |t: &str| Value::Text(UnicodeString::new_nul(t)); + let desc = Descriptor::new("null") + .with("NM ", text("Frost")) + .with("LUTFormat", en("LUTFormatType", "LUTFormatCUBE")) + .with("LUT3DFileName", text("frost.cube")) + .with("LUT3DFileData", Value::RawData(cube.to_vec())); + let mut raw = vec![0, 1]; + raw.extend(VersionedDescriptor::new(desc).to_bytes()); + assert_eq!(&raw[..6], &[0, 1, 0, 0, 0, 16], "version 1, then the descriptor's version 16"); + raw +} + +fn identity_cube(domain: &str) -> Vec { + format!("{domain}LUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n").into_bytes() +} + +fn synthetic_lookup_psd(version: Version, depth: u16, cube: &[u8]) -> Vec { + const LAYER_NAME: &str = "Custom-domain LUT"; + let mut file = testgen::layered(version, photocraft_psd::ColorMode::Rgb, depth, Compression::Rle); + let mut flags = LayerFlags::default(); + flags.set_pixel_data_irrelevant(true); + // Photoshop stores layer-block alignment inside the block length. Make the source payload + // canonical so the exporter can preserve it exactly rather than adding alignment on save. + let mut clrl = photoshop_clr_l_block_from_cube(cube); + clrl.resize(clrl.len().next_multiple_of(2), 0); + let record = LayerRecord { + name: LAYER_NAME.as_bytes().to_vec(), + flags, + blocks: vec![TaggedBlock::new(*b"clrL", clrl), TaggedBlock::unicode_name(LAYER_NAME), TaggedBlock::layer_id(9876)], + ..Default::default() + }; + file.layers_mut().insert(0, record); + file.to_bytes().expect("serialize synthetic PSD/PSB") +} + +fn unsupported_lookup_raw(doc: &Document) -> &[u8] { + let layer = doc.layers.iter().find(|layer| layer.name == "Custom-domain LUT").expect("lookup layer"); + let LayerContent::Adjustment(Adjustment::Unsupported { psd_key, raw }) = &layer.content else { + panic!("expected preserved unsupported Color Lookup, got {:?}", layer.content) + }; + assert_eq!(psd_key, "clrL"); + raw +} + +fn parsed_lookup_block(bytes: &[u8]) -> (String, Vec) { + let file = PsdFile::from_bytes(bytes).expect("parse PSD/PSB"); + let layer = file.layers().iter().find(|layer| layer.name() == "Custom-domain LUT").expect("lookup layer record"); + let blocks: Vec<_> = layer.blocks.iter().filter(|block| block.key == *b"clrL").collect(); + assert_eq!(blocks.len(), 1, "lookup blocks on layer: {blocks:?}"); + (layer.name(), blocks[0].data.clone()) +} + +#[test] +fn photoshop_layout_color_lookup_round_trips_through_psb() { + // A kept-raw `clrL` block in Photoshop's layout survives a PSB round trip and reopens as a + // working Color Lookup, with no warning naming the layer (#1763). + let raw = photoshop_clr_l_block(); + let opts = ExportOptions { force_psb: true, ..Default::default() }; + let (doc, warnings) = reopen(Adjustment::Unsupported { psd_key: "clrL".into(), raw: raw.as_slice().into() }, "x.psb", &opts); + assert!(warnings.iter().all(|w| !w.contains("Teal Look")), "{warnings:?}"); + let LayerContent::Adjustment(Adjustment::ColorLookup { name, size, lut: Some(table), tetrahedral: false, dither: false, source: Some(source) }) = + &doc.layers[0].content + else { + panic!("{:?}", doc.layers[0].content) + }; + assert_eq!(name, "frost.cube"); + // The loaded `.cube` file is kept as its source, so a later save can embed it unchanged. + assert_eq!(source.format, photocraft_doc::adjust::LutFormat::Cube); + assert_eq!(source.bytes.as_slice(), b"LUT_3D_SIZE 2\n0 0 0\n0 1 1\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1\n"); + assert_eq!(*size, 2); + let want: Vec = [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0].into(); + assert_eq!(table.as_slice(), want.as_slice()); + // The exporter wrote the block verbatim (the importer keeps it for saving; the block writer + // pads to Photoshop's four-byte alignment, which the spec requires). + let kept = doc.layers[0].psd_blocks.iter().find(|(k, _)| k == b"clrL").map(|(_, d)| d.as_slice()); + let Some(kept) = kept else { panic!("{:?}", doc.layers[0].psd_blocks) }; + assert!(kept.starts_with(&raw) && kept[raw.len()..].iter().all(|&b| b == 0), "{kept:?}"); + // The reopened lookup really applies: a red backdrop renders as the table's cyan. + let mut d = doc; + let mut bg = Surface::new(d.pixel_format()); + bg.fill_rect(d.bounds(), &[1.0, 0.0, 0.0, 1.0]); + d.layers.insert(0, Layer::new("bg", LayerContent::Raster(bg))); + let px = photocraft_compose::flatten(&d).px[0]; + assert!(px[0] < 0.05 && (px[1] - 1.0).abs() < 0.05 && (px[2] - 1.0).abs() < 0.05, "{px:?}"); +} + +#[test] +fn short_color_lookup_table_warns_on_open() { + // A stored table shorter than size³×3 renders as the identity; opening must say so, naming + // the layer (#1763). + let short = Adjustment::ColorLookup { + name: "t.cube".into(), + size: 3, + lut: Some(std::sync::Arc::new(vec![0.5; 7])), + tetrahedral: false, + dither: false, + source: None, + }; + let (doc, warnings) = reopen(short, "x.pcraft", &ExportOptions::default()); + assert!(matches!(&doc.layers[0].content, LayerContent::Adjustment(Adjustment::ColorLookup { .. })), "{:?}", doc.layers[0].content); + let w: Vec<&String> = warnings.iter().filter(|w| w.contains("Teal Look")).collect(); + assert_eq!(w.len(), 1, "{warnings:?}"); + assert!(w[0].contains("Color Lookup") && w[0].contains("7 values") && w[0].contains("81"), "{}", w[0]); + // A table long enough for its size opens without the note. + let ok = Adjustment::ColorLookup { + name: "t.cube".into(), + size: 2, + lut: Some(std::sync::Arc::new(vec![0.5; 24])), + tetrahedral: false, + dither: false, + source: None, + }; + let (_, warnings) = reopen(ok, "x.pcraft", &ExportOptions::default()); + assert!(warnings.iter().all(|w| !w.contains("Teal Look")), "{warnings:?}"); + // A missing table renders as the identity too. + let none = Adjustment::ColorLookup { name: "t.cube".into(), size: 2, lut: None, tetrahedral: false, dither: false, source: None }; + let (_, warnings) = reopen(none, "x.pcraft", &ExportOptions::default()); + assert!(warnings.iter().any(|w| w.contains("Teal Look") && w.contains("no usable table")), "{warnings:?}"); +} + +#[test] +fn custom_domain_lookup_warns_and_round_trips_verbatim() { + let domains = ["DOMAIN_MIN 0 0 0\nDOMAIN_MAX 0.5 0.5 0.5\n", "DOMAIN_MIN 0 0.25 0\nDOMAIN_MAX 1 0.75 1\n", "LUT_3D_INPUT_RANGE 0 0.5\n"]; + for domain in domains { + let cube = identity_cube(domain); + for (version, depth, extension) in [ + (Version::Psd, 8, "psd"), + (Version::Psd, 16, "psd"), + (Version::Psd, 32, "psd"), + (Version::Psb, 8, "psb"), + (Version::Psb, 16, "psb"), + (Version::Psb, 32, "psb"), + ] { + let input = synthetic_lookup_psd(version, depth, &cube); + let (_, source_block) = parsed_lookup_block(&input); + let name = format!("input.{extension}"); + let opened = import(&name, &input).expect("open synthetic PSD/PSB"); + assert_eq!(unsupported_lookup_raw(&opened.document), source_block, "{extension}, {depth}-bit, {domain:?}"); + let warnings: Vec<_> = opened.warnings.iter().filter(|warning| warning.contains("Custom-domain LUT")).collect(); + assert_eq!(warnings.len(), 1, "{extension}, {depth}-bit: {:?}", opened.warnings); + assert!( + warnings[0].contains("Color Lookup") + && warnings[0].contains("DOMAIN_MIN") + && warnings[0].contains("DOMAIN_MAX") + && warnings[0].to_ascii_lowercase().contains("not supported"), + "{}", + warnings[0] + ); + + let native = export(&opened.document, "preserved.pcraft", &ExportOptions::default()).expect("save native document"); + let native_open = import("preserved.pcraft", &native.bytes).expect("reopen native document"); + assert_eq!(unsupported_lookup_raw(&native_open.document), source_block, "native preservation, {extension}, {depth}-bit"); + + let output_name = format!("roundtrip.{extension}"); + let options = ExportOptions { force_psb: version.is_psb(), ..Default::default() }; + let saved = export(&native_open.document, &output_name, &options).expect("re-export PSD/PSB"); + let (_, output_block) = parsed_lookup_block(&saved.bytes); + assert_eq!(output_block, source_block, "saved block, {extension}, {depth}-bit, {domain:?}"); + let reopened = import(&output_name, &saved.bytes).expect("reopen re-exported PSD/PSB"); + assert_eq!(unsupported_lookup_raw(&reopened.document), source_block, "reopened block, {extension}, {depth}-bit"); + } + } +} + +#[test] +fn default_domain_lookup_stays_readable() { + for (version, domain, extension) in [(Version::Psd, "", "psd"), (Version::Psb, "DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\n", "psb")] { + let cube = identity_cube(domain); + let input = synthetic_lookup_psd(version, 8, &cube); + let opened = import(&format!("default.{extension}"), &input).expect("open default-domain LUT"); + let layer = opened.document.layers.iter().find(|layer| layer.name == "Custom-domain LUT").expect("lookup layer"); + assert!(matches!(layer.content, LayerContent::Adjustment(Adjustment::ColorLookup { size: 2, .. })), "{:?}", layer.content); + assert!(opened.warnings.iter().all(|warning| !warning.contains("Custom-domain LUT")), "{:?}", opened.warnings); + } +} diff --git a/crates/io/tests/affinity.rs b/crates/io/tests/it/affinity.rs similarity index 100% rename from crates/io/tests/affinity.rs rename to crates/io/tests/it/affinity.rs diff --git a/crates/io/tests/affinity_artboards.rs b/crates/io/tests/it/affinity_artboards.rs similarity index 98% rename from crates/io/tests/affinity_artboards.rs rename to crates/io/tests/it/affinity_artboards.rs index 67e3739043..26a9a3c7e5 100644 --- a/crates/io/tests/affinity_artboards.rs +++ b/crates/io/tests/it/affinity_artboards.rs @@ -1,7 +1,7 @@ //! Current .af artboards stay separate, editable and clipped through native/PSD saves. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] -#[path = "support/current_affinity.rs"] +#[path = "../support/current_affinity.rs"] mod fixture; use fixture::{Marker, document}; diff --git a/crates/io/tests/affinity_corpus.rs b/crates/io/tests/it/affinity_corpus.rs similarity index 100% rename from crates/io/tests/affinity_corpus.rs rename to crates/io/tests/it/affinity_corpus.rs diff --git a/crates/io/tests/annotations.rs b/crates/io/tests/it/annotations.rs similarity index 100% rename from crates/io/tests/annotations.rs rename to crates/io/tests/it/annotations.rs diff --git a/crates/io/tests/artboard_guide_order.rs b/crates/io/tests/it/artboard_guide_order.rs similarity index 100% rename from crates/io/tests/artboard_guide_order.rs rename to crates/io/tests/it/artboard_guide_order.rs diff --git a/crates/io/tests/banded.rs b/crates/io/tests/it/banded.rs similarity index 100% rename from crates/io/tests/banded.rs rename to crates/io/tests/it/banded.rs diff --git a/crates/io/tests/it/blend_modes_corpus.rs b/crates/io/tests/it/blend_modes_corpus.rs new file mode 100644 index 0000000000..296e2e2a50 --- /dev/null +++ b/crates/io/tests/it/blend_modes_corpus.rs @@ -0,0 +1,67 @@ +//! Blend modes against Photoshop's own merged image of psd-tools `blend-modes/rgb-blend-modes.psd`: +//! one 64 × 64 cell per mode, a duck over a rainbow backdrop. +#![cfg(feature = "corpus")] + +use photocraft_color::BlendMode; +use photocraft_geom::Rect; +use photocraft_io::{import, merged_composite}; +use photocraft_psd::PsdFile; + +const CELL: i32 = 64; + +/// The largest and mean difference in 8-bit levels between our flatten and Photoshop's merged image +/// over the cell of `mode`, on the pixels where that cell's duck is fully opaque (its anti-aliased +/// edge mixes in the backdrop, which says nothing about the mode). +fn cell_error(mode: BlendMode) -> (f32, f32) { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/psd-tools/blend-modes/rgb-blend-modes.psd"); + let bytes = std::fs::read(path).expect("run cargo xtask corpus --psd-tools"); + let doc = import("rgb-blend-modes.psd", &bytes).expect("opens").document; + let file = PsdFile::from_bytes(&bytes).expect("parses"); + let merged = merged_composite(&file).expect("merged image"); + let ours = photocraft_compose::flatten(&doc).px; + let width = doc.size.width as i32; + let duck = doc.walk().into_iter().map(|(_, _, l)| l).find(|l| l.name == "rubber_duck_PNG1" && l.blend == mode).expect("the mode's duck"); + let surface = duck.surface().expect("duck pixels"); + let bounds = surface.content_bounds(); + let (x0, y0) = ((bounds.x0 / CELL) * CELL, (bounds.y0 / CELL) * CELL); + let mut alpha = vec![[0.0f32; 4]; (CELL * CELL) as usize]; + surface.read_rgba_into(Rect::from_xywh(x0, y0, CELL as u32, CELL as u32), &mut alpha); + let (mut max, mut sum, mut n) = (0.0f32, 0.0f32, 0u32); + for y in 1..CELL - 1 { + for x in 1..CELL - 1 { + // Opaque, and so are its four neighbours (one pixel in from the edge). + let opaque = |dx: i32, dy: i32| alpha[((y + dy) * CELL + x + dx) as usize][3] >= 1.0; + if !(opaque(0, 0) && opaque(-1, 0) && opaque(1, 0) && opaque(0, -1) && opaque(0, 1)) { + continue; + } + let i = ((y0 + y) * width + x0 + x) as usize; + for c in 0..3 { + let d = ((ours[i][c] * 255.0).round() - (merged[i][c] * 255.0).round()).abs(); + max = max.max(d); + sum += d; + n += 1; + } + } + } + assert!(n > 3000, "{mode:?}: only {n} opaque samples"); + (max, sum / n as f32) +} + +/// #8804: Soft Light over the dark parts of the rainbow. With `sqrt(cb)` below a quarter the cell +/// was up to 20 levels too light; the W3C curve Photoshop uses is within 7. +#[test] +fn soft_light_matches_photoshop_over_dark_backdrops() { + let (max, mean) = cell_error(BlendMode::SoftLight); + eprintln!("Soft Light: max {max} mean {mean:.3}"); + assert!(max <= 7.0 && mean < 0.6, "max {max} mean {mean}"); +} + +/// The modes #8804 checked next to Soft Light stay where they were. +#[test] +fn separable_light_modes_match_photoshop() { + for mode in [BlendMode::Multiply, BlendMode::Screen, BlendMode::Overlay, BlendMode::HardLight] { + let (max, mean) = cell_error(mode); + eprintln!("{mode:?}: max {max} mean {mean:.3}"); + assert!(max <= 3.0, "{mode:?}: max {max} mean {mean}"); + } +} diff --git a/crates/io/tests/cancel.rs b/crates/io/tests/it/cancel.rs similarity index 100% rename from crates/io/tests/cancel.rs rename to crates/io/tests/it/cancel.rs diff --git a/crates/io/tests/channels.rs b/crates/io/tests/it/channels.rs similarity index 100% rename from crates/io/tests/channels.rs rename to crates/io/tests/it/channels.rs diff --git a/crates/io/tests/cmyk_composite_export.rs b/crates/io/tests/it/cmyk_composite_export.rs similarity index 90% rename from crates/io/tests/cmyk_composite_export.rs rename to crates/io/tests/it/cmyk_composite_export.rs index 31c0b6969f..0fea33695d 100644 --- a/crates/io/tests/cmyk_composite_export.rs +++ b/crates/io/tests/it/cmyk_composite_export.rs @@ -1,6 +1,6 @@ use photocraft_cms::{Builtin, Intent, Profile, Transform}; use photocraft_codecs::{ChannelLayout, SampleType as CodecSample, decode}; -use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; use photocraft_doc::{Document, Layer, LayerContent, LayerMask}; use photocraft_geom::{Rect, Size}; use photocraft_io::{ExportOptions, document_to_image, export}; @@ -99,6 +99,30 @@ fn tiff_keeps_true_zero_alpha_when_composited() { #[test] fn flat_and_layered_tiff_agree_for_mixed_cmyk_regions() { + compare_flat_and_layered_mixed_cmyk_regions(); +} + +fn assert_tiff_layout(image: &photocraft_codecs::Image, bytes: &[u8], opaque: bool, sample: CodecSample, context: &str) { + assert_eq!(image.dimensions(), (48, 16), "{context}"); + assert_eq!(image.layout(), if opaque { ChannelLayout::Cmyk } else { ChannelLayout::CmykA }, "{context}"); + assert_eq!(image.sample_type(), sample, "{context}"); + assert_eq!(image.meta.dpi, Some((300.0, 300.0)), "{context}"); + assert_eq!(tag(bytes, 262), Some(5), "{context}"); + assert_eq!(tag(bytes, 277), Some(if opaque { 4 } else { 5 }), "{context}"); + assert_eq!(tag(bytes, 338), if opaque { None } else { Some(2) }, "{context}"); +} + +fn assert_mixed_region_alpha(samples: &[f32], tolerance: f32, context: &str) { + for (i, px) in samples.as_chunks::<5>().0.iter().enumerate() { + match i % 48 { + 0..16 => assert_eq!(px[4], 1.0, "{context}: opaque region"), + 16..32 => assert!((px[4] - 0.5).abs() <= tolerance, "{context}: translucent region: {px:?}"), + _ => assert_eq!(px[4], 0.0, "{context}: empty region"), + } + } +} + +fn compare_flat_and_layered_mixed_cmyk_regions() { let custom = photocraft_cms::synth::cmyk_profile(&photocraft_cms::synth::CmykParams { description: "Mixed regions uncoated".into(), tvi: [0.26, 0.26, 0.26, 0.3], @@ -123,15 +147,8 @@ fn flat_and_layered_tiff_agree_for_mixed_cmyk_regions() { assert_eq!(reference.icc.as_deref(), profile.as_deref().map(Vec::as_slice), "{context}"); assert!(image.meta.photoshop_layers.is_none(), "{context}"); assert!(reference.meta.photoshop_layers.is_some(), "{context}"); - for (decoded, bytes) in [(&image, &flat.bytes), (&reference, &layered.bytes)] { - assert_eq!(decoded.dimensions(), (48, 16), "{context}"); - assert_eq!(decoded.layout(), if opaque { ChannelLayout::Cmyk } else { ChannelLayout::CmykA }, "{context}"); - assert_eq!(decoded.sample_type(), sample, "{context}"); - assert_eq!(decoded.meta.dpi, Some((300.0, 300.0)), "{context}"); - assert_eq!(tag(bytes, 262), Some(5), "{context}"); - assert_eq!(tag(bytes, 277), Some(if opaque { 4 } else { 5 }), "{context}"); - assert_eq!(tag(bytes, 338), if opaque { None } else { Some(2) }, "{context}"); - } + assert_tiff_layout(&image, &flat.bytes, opaque, sample, &context); + assert_tiff_layout(&reference, &layered.bytes, opaque, sample, &context); // The PSD-backed layered path inverts CMYK samples twice, so allow one stored level. let tolerance = match depth { SampleType::U8 => 1.0 / 255.0, @@ -144,13 +161,7 @@ fn flat_and_layered_tiff_agree_for_mixed_cmyk_regions() { let difference = actual.iter().zip(&expected).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); assert!(difference <= tolerance + 1e-6, "{context}: maximum sample difference {difference}"); if !opaque { - for (i, px) in actual.as_chunks::<5>().0.iter().enumerate() { - match i % 48 { - 0..16 => assert_eq!(px[4], 1.0, "{context}: opaque region"), - 16..32 => assert!((px[4] - 0.5).abs() <= tolerance, "{context}: translucent region: {px:?}"), - _ => assert_eq!(px[4], 0.0, "{context}: empty region"), - } - } + assert_mixed_region_alpha(&actual, tolerance, &context); } let back = photocraft_io::import("regions.tiff", &flat.bytes).unwrap().document; assert_eq!((back.mode, back.depth), (ColorMode::Cmyk, depth), "{context}"); @@ -223,6 +234,9 @@ fn composite_uses_distinctive_document_profile_on_every_worker() { let mut doc = document(SampleType::F32, 0.5); doc.size = Size::new(64, 64); doc.icc_profile = Some(profile.to_bytes()); + // Hue mixes channels, so the stack is composited in RGB and converted back through the + // profile (the empty layer leaves the composite as it is): the path under test. + doc.layers[1].blend = BlendMode::Hue; let rgb = photocraft_compose::flatten(&doc); let transform = Transform::new(Builtin::Srgb.profile(), &profile, Intent::RelativeColorimetric, true).unwrap(); let fallback = Transform::new(Builtin::Srgb.profile(), Builtin::CoatedCmyk.profile(), Intent::RelativeColorimetric, true).unwrap(); diff --git a/crates/io/tests/it/cmyk_export.rs b/crates/io/tests/it/cmyk_export.rs new file mode 100644 index 0000000000..043da677f2 --- /dev/null +++ b/crates/io/tests/it/cmyk_export.rs @@ -0,0 +1,237 @@ +//! A CMYK document is written to flat formats as CMYK with its own profile whatever its layers +//! (#2220), mixing layers on their ink values so opaque pixels keep their exact inks. + +use std::sync::Arc; + +use photocraft_cms::Builtin; +use photocraft_codecs::ChannelLayout; +use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{Document, Layer}; +use photocraft_geom::{Rect, Size}; +use photocraft_io::*; + +fn cmyk_doc(depth: SampleType) -> Document { + let mut d = Document::new("c", Size::new(12, 8), ColorMode::Cmyk, depth); + d.icc_profile = Some(Arc::new(Builtin::CoatedCmyk.profile().to_bytes().to_vec())); + d +} + +fn fill(depth: SampleType, alpha: bool, rects: &[(Rect, [f32; 5])]) -> Layer { + let mut l = Layer::raster("l", PixelFormat::new(ColorMode::Cmyk, depth, alpha)); + if let Some(s) = l.surface_mut() { + for (r, c) in rects { + s.fill_rect(*r, &c[..if alpha { 5 } else { 4 }]); + } + } + l +} + +/// Paper (no ink), K-only black "type", and a four-colour patch, as separate layers. +fn layered(depth: SampleType) -> Document { + let mut d = cmyk_doc(depth); + d.layers.push(fill(depth, false, &[(Rect::new(0, 0, 12, 8), [0.0; 5])])); + d.layers.push(fill(depth, true, &[(Rect::new(0, 0, 6, 8), [0.0, 0.0, 0.0, 1.0, 1.0])])); + d.layers.push(fill(depth, true, &[(Rect::new(6, 0, 12, 4), [0.2, 0.4, 0.6, 0.1, 1.0])])); + d +} + +fn decode(bytes: &[u8]) -> photocraft_codecs::Image { + photocraft_codecs::decode(bytes).unwrap_or_else(|e| panic!("decode: {e}")) +} + +/// Normalized C, M, Y, K (and alpha) of pixel (x, y). +fn pixel(img: &photocraft_codecs::Image, x: u32, y: u32) -> Vec { + let n = img.layout().channels(); + let (w, _) = img.dimensions(); + let v = img.to_normalized(); + let i = ((y * w + x) as usize) * n; + v[i..i + n].to_vec() +} + +fn q8(v: f32) -> f32 { + (v * 255.0).round() / 255.0 +} + +#[test] +fn layered_cmyk_tiff_stays_cmyk_with_the_document_profile_and_exact_inks() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let d = layered(depth); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{depth:?}: {e}")); + assert!(!r.warnings.iter().any(|w| w.contains("written as sRGB") || w.contains("converted back")), "{depth:?}: {:?}", r.warnings); + let img = decode(&r.bytes); + assert_eq!(img.layout(), ChannelLayout::Cmyk, "{depth:?}: opaque CMYK, no alpha"); + assert_eq!(img.icc.as_deref(), d.icc_profile.as_deref().map(|v| &v[..]), "{depth:?}: the document's own profile"); + // Exact inks: K-only black stays K-only, the four-colour patch keeps every ink, paper stays blank. + let (k, rich, paper) = (pixel(&img, 2, 3), pixel(&img, 8, 1), pixel(&img, 8, 6)); + let tol = if depth == SampleType::U8 { 0.0 } else { 1e-4 }; + let close = |a: &[f32], b: [f32; 4]| a.iter().zip(b).all(|(x, y)| (x - y).abs() <= tol); + let want_rich = if depth == SampleType::U8 { [q8(0.2), q8(0.4), q8(0.6), q8(0.1)] } else { [0.2, 0.4, 0.6, 0.1] }; + assert!(close(&k, [0.0, 0.0, 0.0, 1.0]), "{depth:?}: {k:?}"); + assert!(close(&rich, want_rich), "{depth:?}: {rich:?}"); + assert!(close(&paper, [0.0; 4]), "{depth:?}: {paper:?}"); + // And it reads back as a CMYK document. + let back = import("a.tif", &r.bytes).unwrap_or_else(|e| panic!("{e}")).document; + assert_eq!(back.mode, ColorMode::Cmyk, "{depth:?}"); + } +} + +#[test] +fn the_reported_case_an_empty_extra_layer() { + // #2220: one layer converted to CMYK plus an empty layer used to export as RGB. + let mut d = cmyk_doc(SampleType::U8); + d.layers.push(fill(SampleType::U8, false, &[(Rect::new(0, 0, 12, 8), [0.3, 0.2, 0.1, 0.0, 1.0])])); + d.layers.push(Layer::raster("empty", PixelFormat::new(ColorMode::Cmyk, SampleType::U8, true))); + for ext in ["tif", "jpg"] { + let r = export(&d, &format!("a.{ext}"), &ExportOptions::default()).unwrap_or_else(|e| panic!("{ext}: {e}")); + assert_eq!(decode(&r.bytes).layout(), ChannelLayout::Cmyk, "{ext}"); + } +} + +#[test] +fn transparency_is_kept_in_tiff_and_composited_over_paper_in_jpeg() { + let mut d = cmyk_doc(SampleType::U8); + d.layers.push(fill(SampleType::U8, true, &[(Rect::new(0, 0, 6, 8), [0.0, 0.0, 0.0, 1.0, 1.0])])); + d.layers.push(fill(SampleType::U8, true, &[(Rect::new(6, 0, 12, 4), [0.0, 1.0, 0.0, 0.0, 0.5])])); + let tif = decode(&export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")).bytes); + assert_eq!(tif.layout(), ChannelLayout::CmykA); + assert_eq!(pixel(&tif, 8, 1), vec![0.0, 1.0, 0.0, 0.0, q8(0.5)], "straight ink and coverage"); + assert_eq!(pixel(&tif, 8, 6)[4], 0.0, "uncovered stays transparent"); + let r = export(&d, "a.jpg", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + let jpg = decode(&r.bytes); + assert_eq!(jpg.layout(), ChannelLayout::Cmyk, "{:?}", r.warnings); + // JPEG is lossy: half-covered magenta over blank paper is about M50; uncovered paper has no ink. + let (half, blank, k) = (pixel(&jpg, 9, 1), pixel(&jpg, 9, 6), pixel(&jpg, 2, 3)); + assert!((half[1] - 0.5).abs() < 0.06 && half[0] < 0.06 && half[3] < 0.06, "{half:?}"); + assert!(blank.iter().all(|v| *v < 0.06), "{blank:?}"); + assert!(k[3] > 0.94 && k[..3].iter().all(|v| *v < 0.06), "{k:?}"); +} + +#[test] +fn multiply_adds_inks_exactly() { + // Multiply darkens: each ink of the result is b + s − b·s, with no ink the layers don't have. + let mut d = layered(SampleType::U16); + let mut m = fill(SampleType::U16, true, &[(Rect::new(0, 0, 12, 8), [0.0, 0.5, 0.0, 0.0, 1.0])]); + m.blend = BlendMode::Multiply; + d.layers.push(m); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + assert!(!r.warnings.iter().any(|w| w.contains("converted back")), "{:?}", r.warnings); + let img = decode(&r.bytes); + assert_eq!(img.layout(), ChannelLayout::Cmyk); + let mul = |b: f32, s: f32| b + s - b * s; + let close = |a: &[f32], b: [f32; 4]| a.iter().zip(b).all(|(x, y)| (x - y).abs() <= 1e-4); + let (k, rich, paper) = (pixel(&img, 2, 3), pixel(&img, 8, 1), pixel(&img, 8, 6)); + assert!(close(&k, [0.0, 0.5, 0.0, 1.0]), "{k:?}"); + assert!(close(&rich, [0.2, mul(0.4, 0.5), 0.6, 0.1]), "{rich:?}"); + assert!(close(&paper, [0.0, 0.5, 0.0, 0.0]), "{paper:?}"); +} + +#[test] +fn other_stacks_stay_cmyk_through_the_profile() { + // Hue mixes channels, so it isn't composited on ink values: the RGB composite is converted back. + let mut d = layered(SampleType::U8); + let mut m = fill(SampleType::U8, true, &[(Rect::new(0, 0, 12, 8), [0.0, 0.5, 0.0, 0.0, 1.0])]); + m.blend = BlendMode::Hue; + d.layers.push(m); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + assert!(r.warnings.iter().any(|w| w.contains("converted back through the document's CMYK profile")), "{:?}", r.warnings); + let img = decode(&r.bytes); + assert_eq!(img.layout(), ChannelLayout::Cmyk); + assert_eq!(img.icc.as_deref(), d.icc_profile.as_deref().map(|v| &v[..])); + // Magenta's hue on the four-colour patch shifts its colour; the page still has ink. + assert!(pixel(&img, 2, 3)[3] > 0.5, "{:?}", pixel(&img, 2, 3)); +} + +#[test] +fn rgb_only_formats_still_get_rgb_through_the_profile() { + let r = export(&layered(SampleType::U8), "a.png", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + let img = decode(&r.bytes); + assert!(matches!(img.layout(), ChannelLayout::Rgb | ChannelLayout::Rgba), "{:?}", img.layout()); + assert!(r.warnings.iter().any(|w| w.contains("Cmyk composite written as sRGB RGB")), "{:?}", r.warnings); +} + +#[test] +fn ink_adjustments_export_exact_inks() { + // Invert in CMYK turns every ink into its opposite: blank paper becomes four-colour black, + // K-only black loses its K and gains full C, M and Y. + let mut d = layered(SampleType::U8); + d.layers.push(Layer::new("inv", photocraft_doc::LayerContent::Adjustment(photocraft_doc::Adjustment::Invert))); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + assert!(!r.warnings.iter().any(|w| w.contains("converted back")), "{:?}", r.warnings); + let img = decode(&r.bytes); + assert_eq!(img.layout(), ChannelLayout::Cmyk); + assert_eq!(pixel(&img, 8, 6), vec![1.0, 1.0, 1.0, 1.0], "paper"); + assert_eq!(pixel(&img, 2, 3), vec![1.0, 1.0, 1.0, 0.0], "K-only black"); + let inv = |v: f32| (255.0 - (v * 255.0).round()) / 255.0; + assert_eq!(pixel(&img, 8, 1), vec![inv(0.2), inv(0.4), inv(0.6), inv(0.1)], "four-colour patch"); +} + +#[test] +fn channel_weighing_adjustments_go_through_the_profile() { + // Threshold weighs the channels together: it isn't an ink-by-ink edit. + let mut d = layered(SampleType::U8); + d.layers.push(Layer::new("t", photocraft_doc::LayerContent::Adjustment(photocraft_doc::Adjustment::Threshold { level: 0.5 }))); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + assert!(r.warnings.iter().any(|w| w.contains("converted back through the document's CMYK profile")), "{:?}", r.warnings); + assert_eq!(decode(&r.bytes).layout(), ChannelLayout::Cmyk); +} + +#[test] +fn layer_effects_export_their_inks() { + // K-only type with a K-only drop shadow on blank paper: the TIFF has black ink only. + use photocraft_doc::effects::{Contour, Effect, FxCommon, Shadow}; + let mut d = cmyk_doc(SampleType::U8); + d.layers.push(fill(SampleType::U8, false, &[(Rect::new(0, 0, 12, 8), [0.0; 5])])); + let mut t = fill(SampleType::U8, true, &[(Rect::new(2, 2, 6, 5), [0.0, 0.0, 0.0, 1.0, 1.0])]); + t.effects.enabled = true; + t.effects.items.push(Effect::DropShadow(Shadow { + common: FxCommon::new(BlendMode::Multiply, 0.75), + color: photocraft_color::Color { mode: ColorMode::Cmyk, c: [0.0, 0.0, 0.0, 1.0], alpha: 1.0 }, + angle: 135.0, + use_global_light: false, + distance: 2.0, + spread: 0.0, + size: 1.0, + contour: Contour::Linear, + anti_alias: false, + noise: 0.0, + knocks_out: true, + })); + d.layers.push(t); + let r = export(&d, "a.tif", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + assert!(!r.warnings.iter().any(|w| w.contains("converted back")), "{:?}", r.warnings); + let img = decode(&r.bytes); + assert_eq!(img.layout(), ChannelLayout::Cmyk); + let mut shadow = 0; + for y in 0..8 { + for x in 0..12 { + let p = pixel(&img, x, y); + assert_eq!(&p[..3], &[0.0; 3], "({x},{y}) {p:?}"); + shadow += usize::from(p[3] > 0.0 && p[3] < 1.0); + } + } + assert!(shadow > 0, "a soft shadow"); + assert_eq!(pixel(&img, 3, 3), vec![0.0, 0.0, 0.0, 1.0], "type"); +} + +#[test] +fn psd_merged_image_keeps_exact_inks() { + // The PSD's merged image (what other apps show and print) is mixed on the inks too: K-only + // black under a Multiply layer stays K-only, and the four-colour patch keeps its inks. + let mut d = layered(SampleType::U8); + let mut m = fill(SampleType::U8, true, &[(Rect::new(0, 0, 12, 8), [0.0, 0.4, 0.0, 0.0, 1.0])]); + m.blend = BlendMode::Multiply; + d.layers.push(m); + let r = export(&d, "a.psd", &ExportOptions::default()).unwrap_or_else(|e| panic!("{e}")); + let f = photocraft_psd::PsdFile::from_bytes(&r.bytes).unwrap_or_else(|e| panic!("{e}")); + let planes = f.decode_merged().unwrap_or_else(|e| panic!("{e}")); + let n = 12 * 8; + // Planar, inverted (255 = no ink). + let ink = |c: usize, x: usize, y: usize| 255 - i32::from(planes.get(c * n + y * 12 + x).copied().unwrap_or(0)); + let q = |v: f32| (v * 255.0).round() as i32; + assert_eq!([ink(0, 2, 3), ink(1, 2, 3), ink(2, 2, 3), ink(3, 2, 3)], [0, q(0.4), 0, 255], "K-only black with magenta multiplied in"); + let mul = |b: f32, s: f32| b + s - b * s; + let qb = |v: f32| ((v * 255.0).round()) / 255.0; + let want = [q(0.2), q(mul(qb(0.4), qb(0.4))), q(0.6), q(0.1)]; + let got = [ink(0, 8, 1), ink(1, 8, 1), ink(2, 8, 1), ink(3, 8, 1)]; + assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= 1), "{got:?} vs {want:?}"); +} diff --git a/crates/io/tests/color.rs b/crates/io/tests/it/color.rs similarity index 85% rename from crates/io/tests/color.rs rename to crates/io/tests/it/color.rs index c5d4479904..15453a2518 100644 --- a/crates/io/tests/color.rs +++ b/crates/io/tests/it/color.rs @@ -1,7 +1,7 @@ //! ICC profiles through import/export: byte-exact PSD round trips with real profiles, PNG/JPEG //! embedding and extraction, colour-managed CMYK → RGB for formats without CMYK. -mod common; +use crate::common; use std::sync::Arc; @@ -180,6 +180,53 @@ fn rgb_fill_colours_separate_through_the_document_cmyk_profile() { } } +#[test] +fn cmyk_merged_composite_reads_through_the_embedded_profile() { + // The merged image of a CMYK file tagged with a non-default profile is turned into RGB through + // that profile, as the layers are, or every comparison against it is off (#3778). + use photocraft_cms::{Intent, Transform}; + use photocraft_psd::{Compression, ImageResource, Version, testgen}; + let profile = photocraft_cms::synth::cmyk_profile(&photocraft_cms::synth::CmykParams { + tvi: [0.26, 0.26, 0.26, 0.3], + grid_a2b: 5, + grid_b2a: 9, + ..Default::default() + }); + let reference = |p: &Profile, ink: &[f32; 4]| { + let t = Transform::new(p, Builtin::Srgb.profile(), Intent::RelativeColorimetric, true).unwrap(); + let mut o = [0.0f32; 4]; + t.eval(ink, &mut o); + [o[0].clamp(0.0, 1.0), o[1].clamp(0.0, 1.0), o[2].clamp(0.0, 1.0)] + }; + for depth in [8u16, 16] { + let mut f = testgen::merged_only(Version::Psd, photocraft_psd::ColorMode::Cmyk, depth, Compression::Raw, 4, 1); + let inks: [[f32; 4]; 4] = [[0.1, 0.6, 0.7, 0.05], [0.8, 0.2, 0.1, 0.0], [0.3, 0.3, 0.3, 0.5], [0.0, 0.0, 0.0, 0.0]]; + let max = if depth == 8 { 255.0 } else { 65535.0 }; + let mut data = Vec::new(); + for c in 0..4 { + for ink in &inks { + // PSD stores CMYK inverted: full scale is no ink. + let v = ((1.0 - ink[c]) * max).round(); + if depth == 8 { data.push(v as u8) } else { data.extend((v as u16).to_be_bytes()) } + } + } + f.image_data = photocraft_psd::ImageData { compression: Compression::Raw, data, stored: None }; + f.resources.push(ImageResource::new(1039, profile.to_bytes().to_vec())); + let merged = merged_composite(&f).unwrap(); + let mut seen_far = false; + for (px, ink) in merged.iter().zip(&inks) { + let ink = ink.map(|v| ((v * max).round()) / max); + let want = reference(&profile, &ink); + let coated = reference(Builtin::CoatedCmyk.profile(), &ink); + seen_far |= (0..3).any(|k| (want[k] - coated[k]).abs() > 4.0 / 255.0); + for k in 0..3 { + assert!((px[k] - want[k]).abs() <= 2.0 / 255.0, "{depth}-bit {ink:?}: {px:?} vs {want:?} (built-in {coated:?})"); + } + } + assert!(seen_far, "the fixture profile differs from the built-in one"); + } +} + /// A two-stop version 1 `grdm` block whose stops are the PSD colour structures `a` and `b`. fn grdm_block(a: (u16, [u16; 4]), b: (u16, [u16; 4])) -> Vec { let mut v = vec![0, 1, 0, 0]; @@ -234,7 +281,7 @@ fn gradient_maps_with_cmyk_lab_gray_stops_import_and_keep_their_space() { } let fmt = doc.pixel_format(); doc.layers.push(raster("Background", fmt, doc.bounds(), 3, false)); - doc.layers.push(Layer::new("Map", LayerContent::Adjustment(Adjustment::Unsupported { psd_key: "grdm".into(), raw: block.clone() }))); + doc.layers.push(Layer::new("Map", LayerContent::Adjustment(Adjustment::Unsupported { psd_key: "grdm".into(), raw: block.as_slice().into() }))); let bytes = export(&doc, "x.psd", &ExportOptions::default()).unwrap().bytes; let mut back = import("x.psd", &bytes).unwrap().document; let LayerContent::Adjustment(Adjustment::GradientMap { stops: got, .. }) = &back.layers[1].content else { diff --git a/crates/io/tests/common/mod.rs b/crates/io/tests/it/common/mod.rs similarity index 95% rename from crates/io/tests/common/mod.rs rename to crates/io/tests/it/common/mod.rs index 28c1a01906..7bb010f1be 100644 --- a/crates/io/tests/common/mod.rs +++ b/crates/io/tests/it/common/mod.rs @@ -172,7 +172,7 @@ pub fn gen_doc(mode: ColorMode, depth: SampleType, f: Features) -> Document { black: Vec::new(), pencil: false, }, - Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![0, 1, 0, 0] }, + Adjustment::Unsupported { psd_key: "selc".into(), raw: vec![0, 1, 0, 0].into() }, ]; // Colour adjustments (Photoshop offers them for colour documents only: they would tint // a grayscale composite). @@ -236,6 +236,12 @@ fn color_close(a: &Color, b: &Color, ctx: &str) { pub fn assert_layers_eq(a: &Layer, b: &Layer, path: &str) { let ctx = format!("{path}/{}", a.name); + assert_layer_properties(a, b, &ctx); + assert_layer_mask_eq(a, b, &ctx); + assert_layer_content_eq(a, b, &ctx); +} + +fn assert_layer_properties(a: &Layer, b: &Layer, ctx: &str) { assert_eq!(a.name, b.name, "{ctx}: name"); assert_eq!(a.visible, b.visible, "{ctx}: visible"); assert_eq!(a.locks, b.locks, "{ctx}: locks"); @@ -259,6 +265,9 @@ pub fn assert_layers_eq(a: &Layer, b: &Layer, path: &str) { let other = b.fill_cache.as_ref().expect("fill cache kept"); surface_eq(&fc.surface, &other.surface, &format!("{ctx}: fill cache")); } +} + +fn assert_layer_mask_eq(a: &Layer, b: &Layer, ctx: &str) { match (&a.mask, &b.mask) { (None, None) => {} (Some(x), Some(y)) => { @@ -270,28 +279,27 @@ pub fn assert_layers_eq(a: &Layer, b: &Layer, path: &str) { } _ => panic!("{ctx}: mask presence differs"), } +} + +fn assert_layer_content_eq(a: &Layer, b: &Layer, ctx: &str) { match (&a.content, &b.content) { - (LayerContent::Raster(x), LayerContent::Raster(y)) => surface_eq(x, y, &ctx), + (LayerContent::Raster(x), LayerContent::Raster(y)) => surface_eq(x, y, ctx), (LayerContent::Group(x), LayerContent::Group(y)) => { assert_eq!(x.expanded, y.expanded, "{ctx}: expanded"); assert_eq!(x.children.len(), y.children.len(), "{ctx}: child count"); for (p, q) in x.children.iter().zip(&y.children) { - assert_layers_eq(p, q, &ctx); + assert_layers_eq(p, q, ctx); } } (LayerContent::Adjustment(x), LayerContent::Adjustment(y)) => assert_eq!(x, y, "{ctx}: adjustment"), - (LayerContent::Fill(Fill::Solid(x)), LayerContent::Fill(Fill::Solid(y))) => color_close(x, y, &ctx), + (LayerContent::Fill(Fill::Solid(x)), LayerContent::Fill(Fill::Solid(y))) => color_close(x, y, ctx), (LayerContent::Fill(x), LayerContent::Fill(y)) => assert_eq!(x, y, "{ctx}: fill"), (LayerContent::Text(x), LayerContent::Text(y)) => { assert_eq!(x.text, y.text, "{ctx}: text"); - surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), &ctx); - } - (LayerContent::Shape(x), LayerContent::Shape(y)) => { - surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), &ctx); - } - (LayerContent::Smart(x), LayerContent::Smart(y)) => { - surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), &ctx); + surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), ctx); } + (LayerContent::Shape(x), LayerContent::Shape(y)) => surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), ctx), + (LayerContent::Smart(x), LayerContent::Smart(y)) => surface_eq(x.cache.as_ref().unwrap(), y.cache.as_ref().unwrap(), ctx), (x, y) => panic!("{ctx}: kind {} vs {}", x.kind_name(), y.kind_name()), } } diff --git a/crates/io/tests/composite.rs b/crates/io/tests/it/composite.rs similarity index 99% rename from crates/io/tests/composite.rs rename to crates/io/tests/it/composite.rs index fbb9e5ab3d..cef80e6a08 100644 --- a/crates/io/tests/composite.rs +++ b/crates/io/tests/it/composite.rs @@ -1,7 +1,7 @@ //! Composite oracle: the merged image stored in a PSD must match our //! compositor's rendering of the imported document. -mod common; +use crate::common; use common::*; use photocraft_color::{BlendMode, ColorMode, SampleType}; diff --git a/crates/io/tests/comps_artboards.rs b/crates/io/tests/it/comps_artboards.rs similarity index 96% rename from crates/io/tests/comps_artboards.rs rename to crates/io/tests/it/comps_artboards.rs index 346d7cc06a..a5b553f5a8 100644 --- a/crates/io/tests/comps_artboards.rs +++ b/crates/io/tests/it/comps_artboards.rs @@ -182,7 +182,15 @@ fn synthetic_psd_roundtrip_all_depths() { v.sort_by(|x, y| x.0.cmp(&y.0)); v }; - assert_eq!(named(&d, a), named(&back, b), "{depth:?} comp {}", a.name); + let mut want = named(&d, a); + if a.name == "One" { + // "One" records nothing for Board 1. A PSD can't say "leaves it alone": its entry + // has no `enab`, which Photoshop reads as the previous comp's visibility (Both: + // shown), and so does the import now (#8093). + want.push(("Board 1".to_string(), Some(true), None)); + want.sort_by(|x, y| x.0.cmp(&y.0)); + } + assert_eq!(want, named(&back, b), "{depth:?} comp {}", a.name); } // Re-exporting the imported document is byte-stable for comps and artboards. let f1 = PsdFile::from_bytes(&to_psd(&back)).unwrap(); diff --git a/crates/io/tests/corpus.rs b/crates/io/tests/it/corpus.rs similarity index 99% rename from crates/io/tests/corpus.rs rename to crates/io/tests/it/corpus.rs index 710b2a8644..182d60095a 100644 --- a/crates/io/tests/corpus.rs +++ b/crates/io/tests/it/corpus.rs @@ -44,7 +44,7 @@ //! asserts import + flatten return (Ok or Err) without panicking. #![cfg(feature = "corpus")] -mod common; +use crate::common; use std::panic::{AssertUnwindSafe, catch_unwind}; use std::path::{Path, PathBuf}; @@ -154,9 +154,9 @@ const MIXED: Source = Source { label: "io corpus", dir: "corpus/psd", group_dept /// 226 vs the merged image + 10 vs the thumbnail (Advanced Blending knockouts: +7). const PSD_TOOLS: Source = Source { label: "psd-tools corpus", dir: "corpus/psd-tools", group_depth: 0, pass_floor: 249, roundtrip_floor: 308 }; -/// Our Photoshop-authored oracles (256 files; https://github.com/storytold/photocraft-corpus), +/// Our Photoshop-authored oracles (258 files; https://github.com/storytold/photocraft-corpus), /// grouped by feature (`smart-filters`, `effects`, `text`, `adjustments/`). -const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 188, roundtrip_floor: 258 }; +const PHOTOSHOP: Source = Source { label: "photoshop oracles", dir: "corpus/photoshop", group_depth: 2, pass_floor: 190, roundtrip_floor: 258 }; /// The corpus directory; a missing corpus fails the test. fn locate(src: &Source) -> PathBuf { diff --git a/crates/io/tests/corpus_regressions.rs b/crates/io/tests/it/corpus_regressions.rs similarity index 94% rename from crates/io/tests/corpus_regressions.rs rename to crates/io/tests/it/corpus_regressions.rs index 0555fc47ca..26266c9f07 100644 --- a/crates/io/tests/corpus_regressions.rs +++ b/crates/io/tests/it/corpus_regressions.rs @@ -1,5 +1,5 @@ //! Synthetic reproductions of failures found by the psd-tools corpus -//! (`cargo xtask corpus --psd-tools`; see `tests/corpus.rs`). The corpus +//! (`cargo xtask corpus --psd-tools`; see `tests/it/corpus.rs`). The corpus //! files themselves are never committed: each test rebuilds the relevant //! structure from our own documents. @@ -116,9 +116,10 @@ fn real_user_mask_with_parameters_is_decoded() { /// psd-tools layer_mask_data.psd, mask-parameters-no-real-channel.psd: a shape layer whose /// vector mask has a density (or feather) shows its fill beyond the path at 1 - density; the -/// stored pixels hold only the shape. It imports as a fill layer with a soft vector mask. +/// stored pixels hold only the shape. It imports as a shape with that density (#5846), rendered +/// from its path rather than from the stored pixels. #[test] -fn shape_with_vector_mask_density_imports_as_fill_with_soft_mask() { +fn shape_with_vector_mask_density_imports_as_soft_shape() { let mut d = doc(SampleType::U8); let mut l = Layer::new("Ellipse", LayerContent::Fill(Fill::Solid(Color::BLACK))); let mut vm = VectorMask::new(square(8.0, 8.0, 24.0, 24.0)); @@ -138,8 +139,9 @@ fn shape_with_vector_mask_density_imports_as_fill_with_soft_mask() { } let imp = reimport(&file); let l = &imp.document.layers[1]; - assert!(matches!(l.content, LayerContent::Fill(_)), "{}", l.content.kind_name()); - assert_eq!(l.vector_mask.as_ref().map(|v| v.density), Some(204.0 / 255.0)); + let LayerContent::Shape(sh) = &l.content else { panic!("{}", l.content.kind_name()) }; + assert_eq!(sh.density, 204.0 / 255.0); + assert!(l.vector_mask.is_none()); let f = photocraft_compose::flatten(&imp.document); assert!(f.get(16, 16)[0] < 0.01, "inside the path: black"); assert!((f.get(2, 2)[0] - 0.8).abs() < 0.01, "outside: 20% of the fill shows: {:?}", f.get(2, 2)); diff --git a/crates/io/tests/it/cylinder_warp.rs b/crates/io/tests/it/cylinder_warp.rs new file mode 100644 index 0000000000..8ed1caa916 --- /dev/null +++ b/crates/io/tests/it/cylinder_warp.rs @@ -0,0 +1,123 @@ +//! Smart objects warped with Photoshop's Cylinder (#4112) through PSD persistence and a re-render +//! from their contents, as Replace Contents, Relink and Edit Contents do. + +use std::sync::Arc; + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent, SmartObject, SmartSource}; +use photocraft_geom::warp::{Warp, WarpStyle}; +use photocraft_geom::{Affine, Rect, Size}; +use photocraft_io::{ExportOptions, export, import, smart_map}; +use photocraft_psd::PsdFile; +use photocraft_raster::Surface; + +/// Silhouettes at x 10 and 90, edges at y 20 and 80, ellipses 0.3 deep, wrapped as little as +/// Photoshop allows (a sixth of the contents on the back on each side). +const VALUES: [f64; 7] = [10.0, 80.0, 90.0, 20.0, 0.3, 0.3, 0.0]; + +/// A 100 × 100 source: a red band at the left and a blue band at the right (both on the back of +/// the cylinder), green in between. +fn contents() -> Surface { + let mut s = Surface::new(photocraft_color::PixelFormat::new(ColorMode::Rgb, SampleType::U8, true)); + s.fill_rect(Rect::new(0, 0, 100, 100), &[0.0, 1.0, 0.0, 1.0]); + s.fill_rect(Rect::new(0, 0, 5, 100), &[1.0, 0.0, 0.0, 1.0]); + s.fill_rect(Rect::new(95, 0, 100, 100), &[0.0, 0.0, 1.0, 1.0]); + s +} + +fn document(transform: Affine, perspective: Option<[f64; 9]>) -> (Document, Warp) { + let pixels = contents(); + let mut source = Document::new("label", Size::new(100, 100), ColorMode::Rgb, SampleType::U8); + source.layers.push(Layer::new("art", LayerContent::Raster(pixels.clone()))); + let bytes = photocraft_format::save_to_bytes(&source, &Default::default()).unwrap(); + let warp = Warp::cylinder(&VALUES, [0.0, 0.0, 100.0, 100.0]).unwrap(); + let mut sm = SmartObject::new(SmartSource::Embedded { file_name: "label.pcraft".into(), bytes: Arc::new(bytes) }, transform, None); + sm.warp = Some(warp.clone()); + sm.perspective = perspective; + sm.cache = Some(render(&sm)); + let mut doc = Document::new("jar", Size::new(160, 160), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::new("label", LayerContent::Smart(sm))); + (doc, warp) +} + +/// The smart object drawn from its contents, as the engine re-renders it. +fn render(sm: &SmartObject) -> Surface { + let src = contents(); + match &sm.perspective { + Some(p) => { + photocraft_algo::warp::place_source_projective(&src, Rect::new(0, 0, 100, 100), &photocraft_algo::transform::Homography(*p), sm.warp.as_ref()) + } + None => photocraft_algo::warp::place_source(&src, Rect::new(0, 0, 100, 100), &sm.transform, sm.warp.as_ref()), + } + .unwrap() +} + +fn smart(doc: &Document) -> &SmartObject { + let LayerContent::Smart(sm) = &doc.layers[0].content else { panic!("not a smart object") }; + sm +} + +fn round_trip(doc: &Document) -> (PsdFile, Document) { + let out = export(doc, "psd", &ExportOptions::default()).unwrap(); + assert!(!out.warnings.iter().any(|w| w.contains("warp") || w.contains("written as pixels")), "{:?}", out.warnings); + (PsdFile::from_bytes(&out.bytes).unwrap(), import("psd", &out.bytes).unwrap().document) +} + +/// Opaque pixels of the render that are mostly `channel` (0 red, 1 green, 2 blue). +fn count(s: &Surface, channel: usize) -> usize { + let r = s.content_bounds(); + s.read_region(r).as_chunks::<4>().0.iter().filter(|p| p[3] > 0.5 && p[channel] > 0.5 && (0..3).all(|c| c == channel || p[c] < 0.5)).count() +} + +/// Before #4112 a cylinder was read as no warp: re-rendering the contents drew them flat, the +/// red and blue bands that wrap round the back included. Now the PSD keeps the cylinder and the +/// re-render hides the back and centres the front between the silhouettes. +#[test] +fn a_cylinder_survives_psd_and_wraps_the_contents_again() { + let transform = Affine { m: [1.0, 0.0, 0.0, 1.0, 30.0, 20.0] }; + let (doc, warp) = document(transform, None); + let (file, back) = round_trip(&doc); + let layer = file.layers().iter().find(|l| l.name() == "label").unwrap(); + let sold = smart_map::parse_sold(&layer.block(b"SoLd").unwrap().data).unwrap(); + let Some(photocraft_psd::descriptor::Value::Descriptor(w)) = sold.descriptor.get("warp") else { panic!("no warp") }; + assert!(matches!(w.get("warpStyle"), Some(photocraft_psd::descriptor::Value::Enumerated { value, .. }) if value.as_bytes() == b"warpCylinder")); + let sm = smart(&back); + assert_eq!(sm.warp.as_ref().map(|w| w.style), Some(WarpStyle::Cylinder)); + assert_eq!(sm.warp.as_ref(), Some(&warp)); + for (a, b) in sm.transform.m.iter().zip(transform.m) { + assert!((a - b).abs() < 1e-9, "{:?}", sm.transform); + } + let out = render(sm); + assert_eq!((count(&out, 0), count(&out, 2)), (0, 0), "the bands on the back are hidden"); + assert!(count(&out, 1) > 3000, "the front is drawn"); + // Between the silhouettes (x 10 and 90, shifted by 30) and no further. + let b = out.content_bounds(); + assert!(b.x0 >= 39 && b.x1 <= 121, "{b:?}"); + // The middle of the contents is at the front, centred. + let mid = out.pixel(80, 60); + assert!(mid[1] > 0.9 && mid[3] > 0.9, "{mid:?}"); + // Saved again unchanged, the block is written as it was read. + let (again, _) = round_trip(&back); + let layer2 = again.layers().iter().find(|l| l.name() == "label").unwrap(); + assert_eq!(layer2.block(b"SoLd").unwrap().data, layer.block(b"SoLd").unwrap().data); +} + +/// Mockups usually place the label in perspective (`nonAffineTransform`): that placement and +/// the cylinder both come back. +#[test] +fn a_cylinder_in_perspective_survives_psd() { + let cb = Warp::cylinder(&VALUES, [0.0, 0.0, 100.0, 100.0]).unwrap().mesh.unwrap().control_bounds(); + let h = photocraft_algo::transform::Homography::rect_to_quad(cb, [[20.0, 20.0], [140.0, 30.0], [135.0, 150.0], [25.0, 140.0]]).unwrap(); + let (doc, warp) = document(Affine::IDENTITY, Some(h.0)); + let (_, back) = round_trip(&doc); + let sm = smart(&back); + assert_eq!(sm.warp.as_ref(), Some(&warp)); + let p = sm.perspective.expect("still projective"); + let n = |m: [f64; 9]| m.map(|v| v / m[8]); + for (a, b) in n(p).iter().zip(n(h.0)) { + assert!((a - b).abs() < 1e-9 * (1.0 + b.abs()), "{p:?} vs {:?}", h.0); + } + let out = render(sm); + assert_eq!((count(&out, 0), count(&out, 2)), (0, 0)); + assert!(count(&out, 1) > 3000); +} diff --git a/crates/io/tests/it/deflate_compatibility.rs b/crates/io/tests/it/deflate_compatibility.rs new file mode 100644 index 0000000000..b6f4331b07 --- /dev/null +++ b/crates/io/tests/it/deflate_compatibility.rs @@ -0,0 +1,145 @@ +//! Backend compatibility, including native-format tile data rather than ZIP assembly alone. +use crate::common; + +use std::io::Read; + +use photocraft_codecs::{ChannelLayout, Format, Image, SampleType as CodecSample}; +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent}; +use photocraft_format::{SaveOptions, load_from_bytes, read_thumbnail, save_to_bytes}; +use photocraft_geom::Size; +use photocraft_io::{PsdExportOptions, document_to_psd_with, import}; +use photocraft_psd::Compression; +use photocraft_raster::Rgba8Image; + +fn document(depth: SampleType, size: Size) -> Document { + let mut doc = Document::new("compatibility", size, ColorMode::Rgb, depth); + doc.layers.push(Layer::new("pixels", LayerContent::Raster(common::pattern(doc.pixel_format(), doc.bounds(), 3, false)))); + doc +} + +/// Compare both decoders and generate a legacy stream without flate2 compression. +fn old_and_new_inflate(bytes: &[u8]) -> (Vec, Vec) { + let old = miniz_oxide::inflate::decompress_to_vec_zlib(bytes) + .unwrap_or_else(|e| panic!("old decoder: {:?}, header {:?}", e.status, &bytes[..bytes.len().min(8)])); + let legacy = miniz_oxide::deflate::compress_to_vec_zlib(&old, 6); + for stream in [bytes, legacy.as_slice()] { + let mut new = Vec::new(); + flate2::read::ZlibDecoder::new(stream).read_to_end(&mut new).unwrap(); + assert_eq!(new, old); + } + (old, legacy) +} + +/// Read new PNGs and PNGs with legacy-miniz IDAT data through the codec. +fn check_png(bytes: &[u8], image: &Image) { + assert_eq!(photocraft_codecs::decode(bytes).unwrap().data(), image.data()); + let mut idat = Vec::new(); + let mut legacy_png = bytes[..8].to_vec(); + let mut idat_offset = None; + let mut offset = 8; + while offset < bytes.len() { + let len = u32::from_be_bytes(bytes[offset..offset + 4].try_into().unwrap()) as usize; + let end = offset + 12 + len; + if &bytes[offset + 4..offset + 8] == b"IDAT" { + idat_offset.get_or_insert(legacy_png.len()); + idat.extend_from_slice(&bytes[offset + 8..offset + 8 + len]); + } else { + legacy_png.extend_from_slice(&bytes[offset..end]); + } + offset = end; + } + let (_, legacy) = old_and_new_inflate(&idat); + let mut chunk = (legacy.len() as u32).to_be_bytes().to_vec(); + chunk.extend_from_slice(b"IDAT"); + chunk.extend_from_slice(&legacy); + chunk.extend_from_slice(&crc32fast::hash(&chunk[4..]).to_be_bytes()); + let at = idat_offset.unwrap(); + legacy_png.splice(at..at, chunk); + assert_eq!(photocraft_codecs::decode(&legacy_png).unwrap().data(), image.data()); +} + +/// New zlib-rs streams and legacy-miniz streams preserve the same document samples. +#[test] +fn png_psd_and_pcraft_decode_identically() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let doc = document(depth, Size::new(257, 131)); + let pixels = doc.layers[0].surface().unwrap().read_region(doc.bounds()); + if depth != SampleType::F32 { + let sample = if depth == SampleType::U8 { CodecSample::U8 } else { CodecSample::U16 }; + let image = Image::from_normalized(257, 131, ChannelLayout::Rgba, sample, &pixels).unwrap(); + check_png(&photocraft_codecs::encode(&image, Format::Png, &Default::default()).unwrap(), &image); + } + let raw_opts = PsdExportOptions { merged_compression: Compression::Raw, merged_matte: false, ..Default::default() }; + let merged = document_to_psd_with(&doc, &raw_opts).0.image_data.data; + for compression in [Compression::Zip, Compression::ZipPrediction] { + let opts = PsdExportOptions { merged_compression: compression, merged_matte: false, ..Default::default() }; + let mut file = document_to_psd_with(&doc, &opts).0; + // The IO exporter normally chooses Raw for float composites; also cover ZIP here. + if depth == SampleType::F32 { + file.image_data = photocraft_psd::ImageData::encode(compression, &merged, &file.header).unwrap(); + } + let bytes = file.to_bytes().unwrap(); + let parsed = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); + assert_eq!(parsed.image_data.compression, compression); + let (inflated, legacy) = old_and_new_inflate(&parsed.image_data.data); + // The PSD streaming helper uses a different flate2 reader from plane decoding. + assert_eq!(photocraft_psd::compression::zip_decompress(&legacy, inflated.len()).unwrap(), inflated); + file.image_data.data = legacy; + for fixture in [bytes, file.to_bytes().unwrap()] { + let parsed = photocraft_psd::PsdFile::from_bytes(&fixture).unwrap(); + assert_eq!(parsed.image_data.decode(&parsed.header).unwrap(), merged); + let back = import("compatibility.psd", &fixture).unwrap().document; + assert_eq!(back.layers[0].surface().unwrap().read_region(doc.bounds()), pixels); + } + } + let mut thumbnail = Rgba8Image::new(7, 3); + thumbnail.pixels = (0..84).map(|i| (i * 37) as u8).collect(); + let opts = SaveOptions { thumbnail: Some(thumbnail.clone()), ..Default::default() }; + let bytes = save_to_bytes(&doc, &opts).unwrap(); + assert!(load_from_bytes(&bytes).unwrap() == doc, "all tile bytes, {depth:?}"); + let image = Image::from_u8(7, 3, ChannelLayout::Rgba, thumbnail.pixels).unwrap(); + check_png(&read_thumbnail(&bytes).unwrap().unwrap(), &image); + } +} + +/// PDF export writes its image at the old backend's size or smaller: zlib-rs level 1 +/// (`Compression::fast()`) writes static Huffman blocks only, which made PDFs of photos +/// 5-30% bigger than miniz's level 1 did. +#[test] +fn pdf_image_stream_is_no_bigger_than_miniz_fast() { + let (w, h) = (512u32, 384u32); + let mut doc = Document::new("photo", Size::new(w, h), ColorMode::Rgb, SampleType::U8); + // A noisy gradient: smooth like a photo, with sensor-like grain. + let mut x32 = 0x9E37_79B9u32; + let mut samples = Vec::with_capacity((w * h * 3) as usize); + let mut vals = Vec::with_capacity((w * h * 4) as usize); + for y in 0..h { + for x in 0..w { + for c in 0..3 { + x32 ^= x32 << 13; + x32 ^= x32 >> 17; + x32 ^= x32 << 5; + let base = (x * 160 / w + y * 80 / h + c * 20) as i32; + let v = (base + (x32 % 9) as i32 - 4).clamp(0, 255) as u8; + samples.push(v); + vals.push(common::g(v)); + } + vals.push(1.0); + } + } + let mut surface = photocraft_raster::Surface::new(doc.pixel_format()); + surface.write_region(doc.bounds(), &vals); + doc.layers.push(Layer::new("photo", LayerContent::Raster(surface))); + let pdf = photocraft_io::export(&doc, "photo.pdf", &Default::default()).unwrap().bytes; + let at = pdf.windows(15).position(|s| s == b"/Subtype /Image").unwrap(); + let dict = &pdf[at..]; + let len_at = dict.windows(8).position(|s| s == b"/Length ").unwrap() + 8; + let digits = dict[len_at..].iter().take_while(|b| b.is_ascii_digit()).count(); + let len: usize = std::str::from_utf8(&dict[len_at..len_at + digits]).unwrap().parse().unwrap(); + let start = dict.windows(7).position(|s| s == b"stream\n").unwrap() + 7; + let stream = &dict[start..start + len]; + assert_eq!(miniz_oxide::inflate::decompress_to_vec_zlib(stream).unwrap(), samples); + let old = miniz_oxide::deflate::compress_to_vec_zlib(&samples, 1).len(); + assert!(len <= old, "PDF image stream {len} bytes, miniz level 1 wrote {old}"); +} diff --git a/crates/io/tests/effects_multi.rs b/crates/io/tests/it/effects_multi.rs similarity index 94% rename from crates/io/tests/effects_multi.rs rename to crates/io/tests/it/effects_multi.rs index 4eda6ad81b..637e926085 100644 --- a/crates/io/tests/effects_multi.rs +++ b/crates/io/tests/it/effects_multi.rs @@ -8,7 +8,7 @@ use photocraft_geom::{Rect, Size}; use photocraft_io::{ExportOptions, export, import}; fn stroke(size: f32, position: StrokePosition, paint: FxPaint, opacity: f32) -> Effect { - Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, opacity), size, position, paint }) + Effect::Stroke(StrokeFx { common: FxCommon::new(BlendMode::Normal, opacity), size, position, paint, overprint: false }) } fn roundtrip(doc: &Document) -> Document { @@ -65,7 +65,8 @@ fn joined_shape_components_roundtrip() { let outer = Subpath::polygon(&[(5.0, 5.0), (35.0, 5.0), (35.0, 35.0), (5.0, 35.0)]); let hole = Subpath::polygon(&[(12.0, 12.0), (12.0, 28.0), (28.0, 28.0), (28.0, 12.0)]).with_op(PathOp::Join); let path = Path::new(vec![outer, hole]); - let mut sh = ShapeLayer { path, fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.9))), stroke: None, live: None, cache: None, psd_raw: None }; + let mut sh = + ShapeLayer { path, fill: Some(Fill::Solid(Color::rgb(0.2, 0.4, 0.9))), stroke: None, live: None, cache: None, psd_raw: None, ..ShapeLayer::default() }; sh.cache = Some(photocraft_vector::render_shape(&sh, PixelFormat::RGBA8, d.bounds())); let mut l = Layer::new("ring", LayerContent::Shape(sh)); l.effects.items = vec![stroke(2.0, StrokePosition::Outside, FxPaint::Color(Color::rgb(1.0, 0.0, 0.0)), 1.0)]; diff --git a/crates/io/tests/exif_resolution.rs b/crates/io/tests/it/exif_resolution.rs similarity index 100% rename from crates/io/tests/exif_resolution.rs rename to crates/io/tests/it/exif_resolution.rs diff --git a/crates/io/tests/export_progress.rs b/crates/io/tests/it/export_progress.rs similarity index 100% rename from crates/io/tests/export_progress.rs rename to crates/io/tests/it/export_progress.rs diff --git a/crates/io/tests/exr_part_profile.rs b/crates/io/tests/it/exr_part_profile.rs similarity index 100% rename from crates/io/tests/exr_part_profile.rs rename to crates/io/tests/it/exr_part_profile.rs diff --git a/crates/io/tests/flat.rs b/crates/io/tests/it/flat.rs similarity index 88% rename from crates/io/tests/flat.rs rename to crates/io/tests/it/flat.rs index 8cdecde369..f6e888eb06 100644 --- a/crates/io/tests/flat.rs +++ b/crates/io/tests/it/flat.rs @@ -1,6 +1,6 @@ //! Flat formats through photocraft-codecs, and export warnings. -mod common; +use crate::common; use common::*; use photocraft_color::{ColorMode, SampleType}; @@ -56,6 +56,10 @@ codec_rt!(fits_rgb32, "fits", ColorMode::Rgb, SampleType::F32, false, 0.0); codec_rt!(dpx_gray8, "dpx", ColorMode::Grayscale, SampleType::U8, false, 0.0); codec_rt!(dpx_rgb16, "dpx", ColorMode::Rgb, SampleType::U16, false, 0.0); codec_rt!(dpx_rgba16, "dpx", ColorMode::Rgb, SampleType::U16, true, 0.0); +// Cineon stores 10-bit code values: within half a 10-bit step. +codec_rt!(cin_gray16, "cin", ColorMode::Grayscale, SampleType::U16, false, 0.5 / 1023.0); +codec_rt!(cin_rgb16, "cin", ColorMode::Rgb, SampleType::U16, false, 0.5 / 1023.0); +codec_rt!(iff_rgb8_opaque, "iff", ColorMode::Rgb, SampleType::U8, false, 0.0); fn indexed_bmp_8() -> Vec { const FILE_HEADER_SIZE: usize = 14; @@ -105,7 +109,7 @@ fn indexed_bmp_opens_with_its_color_table() { #[test] fn indexed_png_opens_with_its_color_table() { let pal = [[10, 20, 30], [200, 100, 50], [7, 8, 9], [0, 255, 0]]; - let png = photocraft_codecs::encode_png_indexed(2, 2, &[1, 0, 2, 1], &pal, Some(2), None).unwrap(); + let png = photocraft_codecs::encode_png_indexed(2, 2, &[1, 0, 2, 1], &pal, Some(2), None, None).unwrap(); let doc = import("indexed.png", &png).unwrap().document; assert_eq!(doc.mode, ColorMode::Indexed); assert_eq!(doc.depth, SampleType::U8); @@ -118,6 +122,60 @@ fn indexed_png_opens_with_its_color_table() { assert_eq!(s.rgba(0, 1)[3], 0.0, "the tRNS entry is transparent"); } +/// A 2 × 2 GIF89a of palette indices `idx` over `pal` (padded to 4 entries), with `transparent` +/// set through a Graphic Control Extension. Its LZW stream clears before every pixel, so every +/// code stays 3 bits: the simplest valid stream any decoder reads. +fn indexed_gif(pal: &[[u8; 3]; 4], idx: [u8; 4], transparent: Option) -> Vec { + let mut b = b"GIF89a".to_vec(); + b.extend([2, 0, 2, 0, 0x81, 0, 0]); // 2 × 2, global table of 4 + pal.iter().for_each(|c| b.extend(c)); + if let Some(t) = transparent { + b.extend([0x21, 0xF9, 4, 0x01, 0, 0, t, 0]); + } + b.extend([0x2C, 0, 0, 0, 0, 2, 0, 2, 0, 0]); + let (clear, end) = (4u32, 5u32); + let codes: Vec = idx.iter().flat_map(|&i| [clear, u32::from(i)]).chain([end]).collect(); + let (mut bits, mut n, mut data) = (0u32, 0u32, Vec::new()); + for c in codes { + bits |= c << n; + n += 3; + while n >= 8 { + data.push((bits & 0xFF) as u8); + bits >>= 8; + n -= 8; + } + } + if n > 0 { + data.push(bits as u8); + } + b.push(2); // LZW minimum code size + b.push(data.len() as u8); + b.extend(&data); + b.extend([0, 0x3B]); + b +} + +/// An indexed GIF opens in Indexed Color mode with its colour table and transparent entry, as a +/// palette PNG does (#7886); it used to open as RGB. +#[test] +fn indexed_gif_opens_with_its_color_table() { + let pal = [[10, 20, 30], [200, 100, 50], [7, 8, 9], [0, 255, 0]]; + let doc = import("indexed.gif", &indexed_gif(&pal, [1, 0, 2, 1], Some(2))).unwrap().document; + assert_eq!(doc.mode, ColorMode::Indexed); + assert_eq!(doc.depth, SampleType::U8); + let table = doc.color_table.as_ref().unwrap(); + assert_eq!(table.colors, pal, "every entry, used or not"); + assert_eq!(table.transparent, Some(2)); + let s = doc.layers[0].surface().unwrap(); + let px = s.rgba(0, 0); + assert!((px[0] - 200.0 / 255.0).abs() < 1e-6 && (px[3] - 1.0).abs() < 1e-6, "{px:?}"); + assert_eq!(s.rgba(0, 1)[3], 0.0, "the transparent entry is transparent"); + // Without a transparent entry, the table alone. + let doc = import("opaque.gif", &indexed_gif(&pal, [3, 3, 0, 1], None)).unwrap().document; + assert_eq!(doc.mode, ColorMode::Indexed); + assert_eq!(doc.color_table.as_ref().unwrap().transparent, None); +} + /// FITS header cards travel as the document's text metadata, so an astro frame keeps its /// OBJECT, EXPTIME and the rest through an edit. #[test] @@ -396,8 +454,8 @@ fn transparent_flat_images_open_as_layer_0() { ("ga.png", enc(ChannelLayout::GrayA, CS::U8, Format::Png), true), ("a.tif", enc(ChannelLayout::Rgba, CS::U8, Format::Tiff), true), ("ga.tif", enc(ChannelLayout::GrayA, CS::U16, Format::Tiff), true), - ("pal.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], Some(0), None).unwrap(), true), - ("pal_opaque.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], None, None).unwrap(), false), + ("pal.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], Some(0), None, None).unwrap(), true), + ("pal_opaque.png", photocraft_codecs::encode_png_indexed(w, h, &[0, 1].repeat(n / 2), &[[255, 0, 0], [0, 0, 255]], None, None, None).unwrap(), false), ("rgb.png", enc(ChannelLayout::Rgb, CS::U8, Format::Png), false), ("g.png", enc(ChannelLayout::Gray, CS::U16, Format::Png), false), ("rgb.tif", enc(ChannelLayout::Rgb, CS::U8, Format::Tiff), false), @@ -690,6 +748,37 @@ fn truncated_jpeg_imports_with_a_warning() { } } +/// Guides stored by Photoshop in a JPEG's APP13 resources (resource 1032) open as document guides. +#[test] +fn jpeg_photoshop_guides_open_as_document_guides() { + let full = export(&smooth(ColorMode::Rgb), "jpg", &ExportOptions::default()).unwrap().bytes; + assert!(import("x.jpg", &full).unwrap().document.guides.vertical.is_empty()); + // Version, grid 576 x 576, two guides in 1/32 px: vertical at 10, horizontal at 5. + let mut guides = Vec::new(); + for v in [1u32, 576, 576, 2] { + guides.extend_from_slice(&v.to_be_bytes()); + } + guides.extend_from_slice(&(10i32 * 32).to_be_bytes()); + guides.push(0); + guides.extend_from_slice(&(5i32 * 32).to_be_bytes()); + guides.push(1); + let mut res = b"8BIM".to_vec(); + res.extend_from_slice(&1032u16.to_be_bytes()); + res.extend_from_slice(&[0, 0]); + res.extend_from_slice(&(guides.len() as u32).to_be_bytes()); + res.extend_from_slice(&guides); + let mut seg = b"Photoshop 3.0\0".to_vec(); + seg.extend_from_slice(&res); + let mut jpeg = full[..2].to_vec(); + jpeg.extend_from_slice(&[0xFF, 0xED]); + jpeg.extend_from_slice(&((seg.len() + 2) as u16).to_be_bytes()); + jpeg.extend_from_slice(&seg); + jpeg.extend_from_slice(&full[2..]); + let g = import("x.jpg", &jpeg).unwrap().document.guides; + assert_eq!(g.vertical, vec![10.0]); + assert_eq!(g.horizontal, vec![5.0]); +} + /// A GIF of `frames` identical 1x1 black frames. fn gif_frames(frames: usize) -> Vec { let mut b = b"GIF89a\x01\0\x01\0\x80\0\0\0\0\0\xFF\xFF\xFF".to_vec(); @@ -1083,3 +1172,58 @@ fn a_lab_document_with_one_layer_is_not_reported_as_flattened() { let r = export(&two, "tif", &ExportOptions::default()).unwrap(); assert!(r.warnings.iter().any(|w| w.contains("2 layer(s) flattened")), "{:?}", r.warnings); } + +#[test] +fn a_stereo_png_keeps_its_stereo_mark_on_save() { + use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, PngStereo, SampleType as CS}; + let img = Image::from_raw(4, 2, ChannelLayout::Rgb, CS::U8, vec![40u8; 24]).unwrap(); + for view in [PngStereo::CrossFuse, PngStereo::DivergingFuse] { + let bytes = photocraft_codecs::encode(&img, Format::Png, &EncodeOptions { png_stereo: Some(view), ..Default::default() }).unwrap(); + let doc = import("stereo.png", &bytes).unwrap().document; + assert_eq!(doc.file_encoding.png_stereo, Some(view.mode())); + // What File > Save does: reuse the settings the file was opened with (#6063). + let mut opts = ExportOptions::default(); + apply_file_encoding(&doc, &mut opts.encode); + let r = export(&doc, "png", &opts).unwrap(); + assert_eq!(photocraft_codecs::png_stereo(&r.bytes), Some(view), "{view:?}"); + } + // An ordinary PNG gains no mark. + let plain = export(&single(ColorMode::Rgb, SampleType::U8, false), "png", &ExportOptions::default()).unwrap(); + assert_eq!(photocraft_codecs::png_stereo(&plain.bytes), None); +} + +/// #8563: a DDS export writes the document's alpha channel as the texture's alpha, as a 32-bit +/// Targa does: exactly in an uncompressed texture, to 4 bits in BC2, interpolated in BC3, and on +/// or off in BC1. With several alpha channels none is written, with a warning. +#[test] +fn dds_writes_the_alpha_channel_as_the_texture_alpha() { + use photocraft_codecs::DdsCompression; + let d = with_alpha_channel(single(ColorMode::Rgb, SampleType::U8, false), "Alpha 1"); + for compression in DdsCompression::ALL { + let mut opts = ExportOptions::default(); + opts.encode.dds.compression = compression; + let r = export(&d, "a.dds", &opts).unwrap(); + let img = photocraft_codecs::decode(&r.bytes).expect("decode dds"); + assert_eq!(img.layout(), photocraft_codecs::ChannelLayout::Rgba, "{compression:?}"); + let (w, _) = img.dimensions(); + let tolerance = match compression { + DdsCompression::None => 1.0, + DdsCompression::Bc2 => 9.0, + _ => 18.0, + }; + for (i, px) in img.data().as_chunks::<4>().0.iter().enumerate() { + let x = i as u32 % w; + let want = alpha_value(x) * 255.0; + if compression == DdsCompression::Bc1 && x == 4 { + continue; // half opacity sits on BC1's on/off threshold + } + assert!((f32::from(px[3]) - want).abs() <= tolerance, "{compression:?} pixel {i}: alpha {} want {want}", px[3]); + } + } + // Two alpha channels: Photoshop's rule for Targa, none of them is written. + let two = with_alpha_channel(d.clone(), "Alpha 2"); + let r = export(&two, "a.dds", &ExportOptions::default()).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("several")), "{:?}", r.warnings); + let img = photocraft_codecs::decode(&r.bytes).expect("decode dds"); + assert!(!img.layout().has_alpha() || img.data().as_chunks::<4>().0.iter().all(|p| p[3] == 255)); +} diff --git a/crates/io/tests/gif_frames.rs b/crates/io/tests/it/gif_frames.rs similarity index 100% rename from crates/io/tests/gif_frames.rs rename to crates/io/tests/it/gif_frames.rs diff --git a/crates/io/tests/it/guides_v2.rs b/crates/io/tests/it/guides_v2.rs new file mode 100644 index 0000000000..73884f00f4 --- /dev/null +++ b/crates/io/tests/it/guides_v2.rs @@ -0,0 +1,128 @@ +//! Image resources 1092 (guides with colours) and 1097 (guide GUIDs) next to 1032 (#5966). + +use photocraft_io::guides_map::{DEFAULT_GUIDE_RGB, GUIDE_IDS, GUIDES_V2}; +use photocraft_io::*; +use photocraft_psd::testgen; +use photocraft_psd::{Compression, ImageResource, PsdFile, Version}; + +const RED: [u8; 3] = [0xff, 0x00, 0x00]; + +type Edit = fn(&mut photocraft_doc::Document); + +fn guides_v1(guides: &[(i32, u8)]) -> Vec { + let mut v = Vec::new(); + for n in [1u32, 576, 576, guides.len() as u32] { + v.extend_from_slice(&n.to_be_bytes()); + } + for (loc, dir) in guides { + v.extend_from_slice(&loc.to_be_bytes()); + v.push(*dir); + } + v +} + +fn guides_v2(guides: &[(i32, u8, [u8; 3])]) -> Vec { + let mut v = Vec::new(); + for n in [2u32, 576, 576, guides.len() as u32] { + v.extend_from_slice(&n.to_be_bytes()); + } + for (loc, dir, rgb) in guides { + v.extend_from_slice(&loc.to_be_bytes()); + v.extend_from_slice(&[*dir, 0]); + v.extend_from_slice(rgb); + } + v +} + +fn guide_ids(n: u32) -> Vec { + let mut v = n.to_be_bytes().to_vec(); + for i in 0..n { + let id = format!("0000000{i}-1111-2222-3333-444444444444"); + let zero = "00000000-0000-0000-0000-000000000000"; + v.extend_from_slice(&36u32.to_be_bytes()); + v.extend_from_slice(id.as_bytes()); + v.extend_from_slice(&0u32.to_be_bytes()); + v.extend_from_slice(&36u32.to_be_bytes()); + v.extend_from_slice(zero.as_bytes()); + } + v +} + +/// A file as Photoshop writes it: a vertical guide at 16 px in the default colour and a red +/// horizontal one at 10 px, in 1032, 1092 and 1097. +fn photoshop_file() -> PsdFile { + let mut f = testgen::small(Version::Psd, Compression::Raw); + f.resources.push(ImageResource::new(1032, guides_v1(&[(512, 0), (320, 1)]))); + f.resources.push(ImageResource::new(GUIDES_V2, guides_v2(&[(512, 0, DEFAULT_GUIDE_RGB), (320, 1, RED)]))); + f.resources.push(ImageResource::new(GUIDE_IDS, guide_ids(2))); + f +} + +fn resource(f: &PsdFile, id: u16) -> Option<&[u8]> { + f.resources.iter().find(|r| r.id == id).map(|r| r.data.as_slice()) +} + +#[test] +fn colours_come_from_guides_v2() { + let (d, _) = psd_to_document(&photoshop_file()); + assert_eq!(d.guides.vertical, [16.0]); + assert_eq!(d.guides.horizontal, [10.0]); + assert_eq!(d.guides.color(false, 10.0), Some(RED)); + // The default colour stays with the preference. + assert_eq!(d.guides.color(true, 16.0), None); +} + +#[test] +fn unchanged_guides_keep_both_resources_byte_exact() { + let f = photoshop_file(); + let (d, _) = psd_to_document(&f); + let out = document_to_psd(&d); + assert_eq!(resource(&out, GUIDES_V2), resource(&f, GUIDES_V2)); + assert_eq!(resource(&out, GUIDE_IDS), resource(&f, GUIDE_IDS)); +} + +#[test] +fn edited_guides_drop_both_resources() { + let (base, _) = psd_to_document(&photoshop_file()); + let edits: [(&str, Edit); 4] = [ + ("clear", |d| d.guides = Default::default()), + ("add", |d| d.guides.horizontal.push(40.0)), + ("move", |d| { + d.guides.vertical[0] = 20.0; + }), + ("recolour", |d| d.guides.set_color(true, 16.0, Some(RED))), + ]; + for (name, edit) in edits { + let mut d = base.clone(); + edit(&mut d); + let out = document_to_psd(&d); + assert!(resource(&out, GUIDES_V2).is_none(), "{name}: stale 1092 written"); + assert!(resource(&out, GUIDE_IDS).is_none(), "{name}: stale 1097 written"); + let (back, _) = psd_to_document(&out); + assert_eq!(back.guides.vertical, d.guides.vertical, "{name}"); + assert_eq!(back.guides.horizontal, d.guides.horizontal, "{name}"); + } +} + +#[test] +fn mismatched_ids_or_malformed_v2_are_not_kept() { + let mut f = photoshop_file(); + f.resources.retain(|r| r.id != GUIDE_IDS); + f.resources.push(ImageResource::new(GUIDE_IDS, guide_ids(3))); + let (d, _) = psd_to_document(&f); + let out = document_to_psd(&d); + assert!(resource(&out, GUIDES_V2).is_some()); + assert!(resource(&out, GUIDE_IDS).is_none()); + + let mut f = photoshop_file(); + for r in &mut f.resources { + if r.id == GUIDES_V2 { + r.data.truncate(20); + } + } + let (d, _) = psd_to_document(&f); + assert_eq!(d.guides.color(false, 10.0), None); + let out = document_to_psd(&d); + assert!(resource(&out, GUIDES_V2).is_none()); + assert!(resource(&out, GUIDE_IDS).is_none()); +} diff --git a/crates/io/tests/heif.rs b/crates/io/tests/it/heif.rs similarity index 100% rename from crates/io/tests/heif.rs rename to crates/io/tests/it/heif.rs diff --git a/crates/io/tests/heif_unsupported.rs b/crates/io/tests/it/heif_unsupported.rs similarity index 100% rename from crates/io/tests/heif_unsupported.rs rename to crates/io/tests/it/heif_unsupported.rs diff --git a/crates/io/tests/krita_corpus.rs b/crates/io/tests/it/krita_corpus.rs similarity index 97% rename from crates/io/tests/krita_corpus.rs rename to crates/io/tests/it/krita_corpus.rs index b0b3a01d63..ab2c56c143 100644 --- a/crates/io/tests/krita_corpus.rs +++ b/crates/io/tests/it/krita_corpus.rs @@ -2,7 +2,7 @@ //! Krita's own rendering of the layer stack (`mergedimage.png`). The files are generated by //! `tools/krita-oracles/generate.py` in https://github.com/storytold/photocraft-corpus and are //! not bundled. Run with -//! `KRITA_TEST_DIR= cargo test -p photocraft-io --test krita_corpus -- --ignored --nocapture`. +//! `KRITA_TEST_DIR= cargo test -p photocraft-io --test it -- krita_corpus:: --ignored --nocapture`. use std::path::{Path, PathBuf}; diff --git a/crates/io/tests/lab16.rs b/crates/io/tests/it/lab16.rs similarity index 100% rename from crates/io/tests/lab16.rs rename to crates/io/tests/it/lab16.rs diff --git a/crates/io/tests/it/large_files.rs b/crates/io/tests/it/large_files.rs new file mode 100644 index 0000000000..b0a2669480 --- /dev/null +++ b/crates/io/tests/it/large_files.rs @@ -0,0 +1,166 @@ +//! Large-file fixtures (#216, #5107), generated at test time by `photocraft_psd::fixtures`: a 20 MP +//! layered PSD, a 150-layer layout, a PSD just under Photoshop's 2 GB cap and a 4.4 GB PSB. Each +//! is opened through `photocraft_io`, checked (size, layers, masks, effects, merged pixels), saved +//! again and re-opened. +//! +//! The two layered documents run in memory; the streamed files are written to +//! `$PHOTOCRAFT_FIXTURE_DIR` (default: the system temp dir), removed afterwards, and are opt-in: +//! `cargo test --release -p photocraft-io --test it -- large_files:: --ignored`. + +use std::path::PathBuf; + +use photocraft_doc::{Document, LayerContent}; +use photocraft_geom::Rect; +use photocraft_psd::Version; +use photocraft_psd::fixtures::{Grid, LAYERED_20MP, LAYOUT_150, PSB_4GB, PSD_2GB, Streamed}; + +/// Raster layers, groups, masked layers, layers with effects and hidden layers in `doc`. +fn census(doc: &Document) -> [usize; 5] { + let mut n = [0; 5]; + for (_, _, l) in doc.walk() { + match l.content { + LayerContent::Raster(_) => n[0] += 1, + LayerContent::Group(_) => n[1] += 1, + _ => {} + } + n[2] += usize::from(l.mask.is_some()); + n[3] += usize::from(!l.effects.items.is_empty()); + n[4] += usize::from(!l.visible); + } + n +} + +fn check_grid(g: &Grid, doc: &Document, what: &str) { + assert_eq!((doc.size.width, doc.size.height), (g.width, g.height), "{what}"); + let layers: Vec<_> = (0..g.layers).filter_map(|i| g.layer(i)).collect(); + let want = [ + g.layers as usize + 1, + g.rows() as usize, + layers.iter().filter(|l| l.masked).count(), + layers.iter().filter(|l| l.shadow).count(), + layers.iter().filter(|l| l.hidden).count(), + ]; + assert_eq!(census(doc), want, "{what}: [raster, groups, masked, with effects, hidden]"); + for (x, y) in g.sample_points() { + let px = photocraft_compose::render(doc, Rect::from_xywh(x as i32, y as i32, 1, 1)).px; + let got = px.first().copied().unwrap_or_default(); + let want = g.expected(x, y); + for c in 0..3 { + let v = (got[c] * 255.0).round() as i32; + assert!((v - i32::from(want[c])).abs() <= 1, "{what}: ({x}, {y}) channel {c}: got {v}, want {}", want[c]); + } + assert!(got[3] > 0.999, "{what}: ({x}, {y}) is not opaque"); + } +} + +/// Builds `g`, opens it, checks it, saves it as PSD and checks the re-opened file. +fn grid_roundtrip(g: Grid, what: &str) { + let t = std::time::Instant::now(); + let bytes = g.build(Version::Psd).and_then(|f| f.to_bytes()).unwrap(); + let gen_s = t.elapsed().as_secs_f64(); + let t = std::time::Instant::now(); + let res = photocraft_io::import("fixture.psd", &bytes).unwrap(); + let open_s = t.elapsed().as_secs_f64(); + check_grid(&g, &res.document, what); + let out = photocraft_io::export(&res.document, "fixture.psd", &Default::default()).unwrap(); + let again = photocraft_io::import("fixture.psd", &out.bytes).unwrap(); + check_grid(&g, &again.document, &format!("{what}, saved and re-opened")); + eprintln!( + "{what}: {:.1} MB, generated in {gen_s:.2} s, opened in {open_s:.2} s, saved as {:.1} MB", + bytes.len() as f64 / 1e6, + out.bytes.len() as f64 / 1e6 + ); +} + +/// 150 layers in 15 groups on A4 at 300 ppi, with masks, hidden layers, closed groups and drop +/// shadows. +#[test] +fn layout_with_150_layers_opens_and_saves() { + grid_roundtrip(LAYOUT_150, "150-layer layout"); +} + +/// A 20 MP layered photo: background, five layers in two groups, a mask and a drop shadow. +#[test] +fn layered_20_mp_psd_opens_and_saves() { + grid_roundtrip(LAYERED_20MP, "20 MP layered"); +} + +fn fixture_dir() -> PathBuf { + let dir = std::env::var_os("PHOTOCRAFT_FIXTURE_DIR").map_or_else(std::env::temp_dir, PathBuf::from).join("photocraft-large-files"); + std::fs::create_dir_all(&dir).unwrap(); + dir +} + +/// Removes the file when the test ends, passed or not. +struct Remove(PathBuf); + +impl Drop for Remove { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + +fn check_streamed(case: &Streamed, doc: &Document, what: &str) { + assert_eq!((doc.size.width, doc.size.height), (case.width, case.height), "{what}"); + assert_eq!(doc.layers.len(), 1, "{what}"); + let s = doc.layers[0].surface().unwrap(); + let (w, h) = (case.width, case.height); + for (x, y) in [(0, 0), (w - 1, 0), (0, h - 1), (w - 1, h - 1), (w / 2, h / 2), (w / 3, h * 2 / 3), (12_345 % w, h - 7)] { + let got = s.pixel(x as i32, y as i32); + assert_eq!(got.len(), 4, "{what}"); + for (c, v) in got.iter().enumerate() { + let want = f32::from(case.sample(x, y, c as u32)) / 255.0; + assert!((v - want).abs() <= 1.0 / 510.0, "{what}: ({x}, {y}) channel {c}: got {v}, want {want}"); + } + } +} + +/// Writes `case`, opens it, checks it, saves it again (the writer picks PSD or PSB) and checks the +/// re-opened file. +fn streamed_roundtrip(case: &Streamed) { + let path = fixture_dir().join(case.file_name()); + let _cleanup = Remove(path.clone()); + let t = std::time::Instant::now(); + let len = case.write_file(&path).unwrap(); + assert_eq!(std::fs::metadata(&path).unwrap().len(), len); + eprintln!("{}: {:.2} GB written in {:.1} s", case.name, len as f64 / 1e9, t.elapsed().as_secs_f64()); + + let t = std::time::Instant::now(); + let bytes = std::fs::read(&path).unwrap(); + let res = photocraft_io::import(&case.file_name(), &bytes).unwrap(); + drop(bytes); + eprintln!("{}: read and opened in {:.1} s, warnings {:?}", case.name, t.elapsed().as_secs_f64(), res.warnings); + check_streamed(case, &res.document, case.name); + + let t = std::time::Instant::now(); + let out = photocraft_io::export(&res.document, &case.file_name(), &Default::default()).unwrap(); + drop(res); + // Version 2 = PSB: a re-save past 2 GB must switch to PSB; a smaller one stays PSD. + let psb = out.bytes.get(4..6) == Some(&[0, 2]); + assert!(psb || (case.version == Version::Psd && (out.bytes.len() as u64) < photocraft_psd::fixtures::PSD_MAX_FILE_BYTES), "{}", case.name); + eprintln!( + "{}: saved {:.2} GB ({}) in {:.1} s, warnings {:?}", + case.name, + out.bytes.len() as f64 / 1e9, + if psb { "PSB" } else { "PSD" }, + t.elapsed().as_secs_f64(), + out.warnings + ); + let again = photocraft_io::import(if psb { "again.psb" } else { "again.psd" }, &out.bytes).unwrap(); + drop(out); + check_streamed(case, &again.document, &format!("{}, saved and re-opened", case.name)); +} + +/// A 17,000² layered PSD of 2.02 GB, just under Photoshop's 2 GB PSD cap. +#[test] +#[ignore = "writes a 2 GB file and needs ~12 GB of memory; run in release"] +fn psd_just_under_2_gb_opens_and_saves() { + streamed_roundtrip(&PSD_2GB); +} + +/// A 25,000² layered PSB of 4.38 GB: layer channels of 625 MB, data past 4 GiB (#375). +#[test] +#[ignore = "writes a 4.4 GB file and needs ~25 GB of memory; run in release"] +fn psb_of_4_gb_opens_and_saves() { + streamed_roundtrip(&PSB_4GB); +} diff --git a/crates/io/tests/large_psb.rs b/crates/io/tests/it/large_psb.rs similarity index 96% rename from crates/io/tests/large_psb.rs rename to crates/io/tests/it/large_psb.rs index bcde955bd0..da9f6ca9a1 100644 --- a/crates/io/tests/large_psb.rs +++ b/crates/io/tests/it/large_psb.rs @@ -25,7 +25,7 @@ fn undecodable_channel_leaves_the_layer_empty() { /// A flattened 29000² RGB PSB: its merged image decodes to 2.52 GB, past the old 2 GiB cap, which /// opened it as a document with no layers. Needs about 12 GB of memory, so it is opt-in: -/// `cargo test --release -p photocraft-io --test large_psb -- --ignored`. +/// `cargo test --release -p photocraft-io --test it -- large_psb:: --ignored`. #[test] #[ignore = "allocates ~12 GB; run in release"] fn flattened_psb_with_a_merged_image_over_2_gib_opens() { diff --git a/crates/io/tests/layer_labels.rs b/crates/io/tests/it/layer_labels.rs similarity index 100% rename from crates/io/tests/layer_labels.rs rename to crates/io/tests/it/layer_labels.rs diff --git a/crates/io/tests/it/layer_xmp.rs b/crates/io/tests/it/layer_xmp.rs new file mode 100644 index 0000000000..87a02b0876 --- /dev/null +++ b/crates/io/tests/it/layer_xmp.rs @@ -0,0 +1,102 @@ +//! A layer's own XMP packet (`Layer::xmp`, PSD `shmd` › `cust` › `layerXMP`) survives every +//! layered format and keeps the rest of a Photoshop `shmd` block as it was. + +use std::sync::Arc; + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, Size}; +use photocraft_io::{ExportOptions, export, import}; +use photocraft_psd::PsdFile; +use photocraft_psd::metadata::{layer_xmp, put_layer_xmp}; + +const A: &str = r#"120"#; +const G: &str = r#"Café 🎨"#; + +/// `shmd` of a Photoshop 2026 layer without XMP: a `cust` item holding only `layerTime`. +const PS_WITHOUT_XMP: &str = + "000000013842494d63757374000000000000003400000010000000010000000000086d6574616461746100000001000000096c6179657254696d65646f756241dab643ffd86bec00"; + +fn unhex(h: &str) -> Vec { + (0..h.len()).step_by(2).map(|i| u8::from_str_radix(&h[i..i + 2], 16).unwrap()).collect() +} + +fn xmps(d: &Document) -> Vec<(String, Option)> { + d.walk().into_iter().map(|(_, _, l)| (l.name.clone(), l.xmp.as_deref().map(str::to_string))).collect() +} + +fn layer_shmd<'a>(f: &'a PsdFile, name: &str) -> Option<&'a [u8]> { + f.layers().iter().find(|r| r.name() == name).and_then(|r| r.block(b"shmd")).map(|b| b.data.as_slice()) +} + +fn sample(depth: SampleType) -> Document { + let mut doc = Document::new("Layer XMP", Size::new(4, 4), ColorMode::Rgb, depth); + let mut a = Layer::raster("A", doc.pixel_format()); + a.xmp = Some(Arc::from(A)); + let plain = Layer::raster("Plain", doc.pixel_format()); + let mut g = Layer::group("G", vec![plain]); + g.xmp = Some(Arc::from(G)); + doc.layers.extend([a, g]); + doc +} + +#[test] +fn layer_xmp_round_trips_through_every_layered_format_and_depth() { + let psb = ExportOptions { force_psb: true, ..Default::default() }; + let tiff = ExportOptions { tiff_layers: true, ..Default::default() }; + for depth in SampleType::ALL { + let doc = sample(depth); + for (path, opts) in [("x.psd", &ExportOptions::default()), ("x.psb", &psb), ("x.tif", &tiff), ("x.pcraft", &ExportOptions::default())] { + let bytes = export(&doc, path, opts).unwrap().bytes; + let back = import(path, &bytes).unwrap().document; + assert_eq!(xmps(&back), xmps(&doc), "{path} {depth:?}"); + } + // Stored as Photoshop stores it; a layer without XMP gets no block. + let bytes = export(&doc, "x.psd", &ExportOptions::default()).unwrap().bytes; + let f = PsdFile::from_bytes(&bytes).unwrap(); + assert_eq!(layer_shmd(&f, "A").and_then(layer_xmp).as_deref(), Some(A)); + assert_eq!(layer_shmd(&f, "G").and_then(layer_xmp).as_deref(), Some(G)); + assert!(layer_shmd(&f, "Plain").is_none()); + } +} + +#[test] +fn photoshop_shmd_keeps_its_other_data() { + let without = unhex(PS_WITHOUT_XMP); + let with = put_layer_xmp(Some(&without), A).unwrap(); + // A layer as an older PhotoCraft kept it: Photoshop's block verbatim, no field. + let mut doc = Document::new("PS", Size::new(2, 2), ColorMode::Rgb, SampleType::U8); + let mut l = Layer::raster("L", doc.pixel_format()); + l.psd_blocks.push((*b"shmd", Arc::new(with.clone()))); + doc.layers.push(l); + let bytes = export(&doc, "x.psd", &ExportOptions::default()).unwrap().bytes; + + // Opening it gives the packet to the field and keeps the rest of the block. + let mut back = import("x.psd", &bytes).unwrap().document; + let l = &back.layers[0]; + assert_eq!(l.xmp.as_deref(), Some(A)); + assert_eq!(l.psd_blocks.iter().find(|(k, _)| k == b"shmd").map(|(_, d)| d.as_slice()), Some(without.as_slice())); + + // Unchanged, the block is written back byte for byte. + let again = export(&back, "x.psd", &ExportOptions::default()).unwrap().bytes; + assert_eq!(layer_shmd(&PsdFile::from_bytes(&again).unwrap(), "L"), Some(with.as_slice())); + + // Edited, `layerTime` stays; removed, the block is Photoshop's block without XMP. + back.layers[0].xmp = Some(Arc::from(G)); + let edited = export(&back, "x.psd", &ExportOptions::default()).unwrap().bytes; + assert_eq!(layer_shmd(&PsdFile::from_bytes(&edited).unwrap(), "L"), Some(put_layer_xmp(Some(&without), G).unwrap().as_slice())); + back.layers[0].xmp = None; + let removed = export(&back, "x.psd", &ExportOptions::default()).unwrap().bytes; + assert_eq!(layer_shmd(&PsdFile::from_bytes(&removed).unwrap(), "L"), Some(without.as_slice())); +} + +#[test] +fn an_unreadable_shmd_is_kept_and_reported() { + let mut doc = Document::new("Bad", Size::new(2, 2), ColorMode::Rgb, SampleType::U8); + let mut l = Layer::raster("L", doc.pixel_format()); + l.psd_blocks.push((*b"shmd", Arc::new(vec![0, 0, 0, 9]))); + l.xmp = Some(Arc::from(A)); + doc.layers.push(l); + let out = export(&doc, "x.psd", &ExportOptions::default()).unwrap(); + assert!(out.warnings.iter().any(|w| w.contains("XMP metadata was not saved")), "{:?}", out.warnings); + assert_eq!(layer_shmd(&PsdFile::from_bytes(&out.bytes).unwrap(), "L"), Some([0, 0, 0, 9].as_slice())); +} diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs new file mode 100644 index 0000000000..40154335d0 --- /dev/null +++ b/crates/io/tests/it/main.rs @@ -0,0 +1,83 @@ +//! The crate's integration tests, linked into one test binary. + +mod adjust_blend_roundtrip; +mod adjust_warnings; +mod affinity; +mod affinity_artboards; +mod affinity_corpus; +mod annotations; +mod artboard_guide_order; +mod banded; +mod blend_modes_corpus; +mod cancel; +mod channels; +mod cmyk_composite_export; +mod cmyk_export; +mod color; +mod common; +mod composite; +mod comps_artboards; +mod corpus; +mod corpus_regressions; +mod cylinder_warp; +mod deflate_compatibility; +mod effects_multi; +mod exif_resolution; +mod export_progress; +mod exr_part_profile; +mod flat; +mod gif_frames; +mod guides_v2; +mod heif; +mod heif_unsupported; +mod krita_corpus; +mod lab16; +mod large_files; +mod large_psb; +mod layer_labels; +mod layer_xmp; +mod modes; +mod multichannel; +mod orientation; +mod pattern_fill_psd; +mod pcraft_manifest_name; +mod pdn_corpus; +mod png_text_metadata; +mod psd16_merged_transparency; +mod psd32_export; +mod psd_background; +mod psd_block_alignment; +mod psd_block_layout; +mod psd_damaged_merged; +mod psd_empty_channels; +mod psd_font_identity; +mod psd_frame_stroke; +mod psd_import_file; +mod psd_kerning; +mod psd_layer_block_length; +mod psd_maximize_compatibility; +mod psd_noise_fill_roundtrip; +mod psd_recovery_generated; +mod psd_roundtrip_bytes; +mod psd_structure; +mod psd_style_sheets; +mod psd_thumbnail; +mod psd_type_bounds; +mod psd_type_photoshop_bytes; +mod psd_unknown_adjustment; +mod psd_vertical_type; +mod raw; +mod resolution_axes; +mod roundtrip; +mod smart_contents_identity; +mod smart_corpus; +mod svg; +mod text_corpus; +mod text_styles; +mod tiff_corpus; +mod tiff_layers; +mod tiff_pages; +mod tiff_type_geometry; +mod vector; +mod xcf; +mod xmp_export; diff --git a/crates/io/tests/it/modes.rs b/crates/io/tests/it/modes.rs new file mode 100644 index 0000000000..b1bfff3d30 --- /dev/null +++ b/crates/io/tests/it/modes.rs @@ -0,0 +1,211 @@ +//! Indexed Color and Duotone documents in flat exports. + +use photocraft_color::{Color, ColorMode, SampleType}; +use photocraft_doc::{ColorTable, Document, Duotone, DuotoneInk, Size}; + +fn doc(mode: ColorMode) -> Document { + let mut d = Document::with_background("m", Size::new(4, 2), ColorMode::Rgb, SampleType::U8, Color::rgba(1.0, 0.0, 0.0, 1.0)); + d.mode = mode; + d +} + +#[test] +fn indexed_png_is_palette_png() { + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0]], transparent: None }); + let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); + // IHDR colour type 3 = indexed; a PLTE chunk follows. + assert_eq!(r.bytes[25], 3); + assert!(r.bytes.windows(4).any(|w| w == b"PLTE")); + let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; + let px = back.layers[0].surface().unwrap().rgba(1, 1); + assert_eq!(&px[..3], &[1.0, 0.0, 0.0]); + // Other formats keep the expanded pixels. + let j = photocraft_io::export(&d, "x.tif", &Default::default()).unwrap(); + assert!(!j.bytes.is_empty()); +} + +#[test] +fn indexed_png_keeps_duplicate_palette_colors_opaque() { + // PLTE entries may have identical RGB but different tRNS alpha. RGB-only lookup used to + // choose the first red entry for both pixels, making the opaque one disappear on save. + let palette = [[255, 0, 0], [0, 255, 0], [255, 0, 0], [0, 0, 255]]; + for transparent in [0, 2] { + let png = photocraft_codecs::encode_png_indexed(4, 1, &[0, 1, 2, 3], &palette, Some(transparent), None, None).unwrap(); + let d = photocraft_io::import("in.png", &png).unwrap().document; + assert_eq!(d.mode, ColorMode::Indexed); + let before = d.color_table.clone(); + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + let table = photocraft_codecs::png_palette(&r.bytes).unwrap().unwrap(); + assert_eq!(table.colors, palette, "palette order must not change"); + assert_eq!(table.transparent, Some(transparent)); + assert_eq!(photocraft_codecs::decode(&r.bytes).unwrap().data(), photocraft_codecs::decode(&png).unwrap().data()); + let back = photocraft_io::import("out.png", &r.bytes).unwrap().document; + assert_eq!(back.color_table, before); + assert_eq!(d.color_table, before, "export must not edit the document's table"); + for x in 0..4 { + assert_eq!(back.layers[0].surface().unwrap().rgba(x, 0)[3], if x == i32::from(transparent) { 0.0 } else { 1.0 }); + } + } +} + +#[test] +fn indexed_png_uses_the_nearest_opaque_color() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for transparent in [0, 1, 2] { + let mut palette = vec![[0, 0, 255], [255, 128, 0]]; + palette.insert(usize::from(transparent), [255, 0, 0]); + let mut d = Document::with_background("m", Size::new(1, 1), ColorMode::Rgb, depth, Color::rgba(1.0, 0.0, 0.0, 1.0)); + d.mode = ColorMode::Indexed; + d.color_table = Some(ColorTable { colors: palette.clone(), transparent: Some(transparent) }); + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + let image = photocraft_codecs::decode(&r.bytes).unwrap(); + assert_eq!(image.data(), &[255, 128, 0, 255], "{depth:?}, transparent = {transparent}"); + let table = photocraft_codecs::png_palette(&r.bytes).unwrap().unwrap(); + assert_eq!(table.colors, palette); + assert_eq!(table.transparent, Some(transparent)); + } + } +} + +#[test] +fn indexed_png_breaks_opaque_color_ties_in_palette_order() { + // Both opaque entries are equally far from magenta; the transparent entry is an exact + // match. Filtering transparency must still choose the earlier original opaque entry. + for opaque in [[[255, 0, 0], [0, 0, 255]], [[0, 0, 255], [255, 0, 0]]] { + let mut d = Document::with_background("m", Size::new(1, 1), ColorMode::Rgb, SampleType::U8, Color::rgba(1.0, 0.0, 1.0, 1.0)); + d.mode = ColorMode::Indexed; + d.color_table = Some(ColorTable { colors: vec![[255, 0, 255], opaque[0], opaque[1]], transparent: Some(0) }); + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + let image = photocraft_codecs::decode(&r.bytes).unwrap(); + assert_eq!(image.data(), &[opaque[0][0], opaque[0][1], opaque[0][2], 255]); + } +} + +#[test] +fn indexed_png_keeps_the_alpha_threshold() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut d = Document::with_background("m", Size::new(3, 1), ColorMode::Rgb, depth, Color::rgba(1.0, 0.0, 0.0, 1.0)); + d.mode = ColorMode::Indexed; + d.color_table = Some(ColorTable { colors: vec![[255, 0, 0], [255, 0, 0]], transparent: Some(0) }); + let surface = d.layers[0].surface_mut().unwrap(); + surface.write_pixel(0, 0, &[1.0, 0.0, 0.0, 0.49]); + surface.write_pixel(1, 0, &[1.0, 0.0, 0.0, 0.5]); + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + assert_eq!(photocraft_codecs::decode(&r.bytes).unwrap().data(), &[255, 0, 0, 0, 255, 0, 0, 255, 255, 0, 0, 255], "{depth:?}"); + } +} + +#[test] +fn indexed_png_keeps_the_last_palette_index() { + let mut palette = vec![[0, 0, 255]; 256]; + palette[0] = [255, 0, 0]; + palette[255] = [255, 0, 0]; + for transparent in [0, 255] { + let png = photocraft_codecs::encode_png_indexed(2, 1, &[0, 255], &palette, Some(transparent), None, None).unwrap(); + let d = photocraft_io::import("in.png", &png).unwrap().document; + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + assert_eq!(photocraft_codecs::png_palette(&r.bytes).unwrap(), photocraft_codecs::png_palette(&png).unwrap()); + assert_eq!(photocraft_codecs::decode(&r.bytes).unwrap().data(), photocraft_codecs::decode(&png).unwrap().data()); + } +} + +#[test] +fn indexed_png_keeps_a_single_transparent_entry() { + let png = photocraft_codecs::encode_png_indexed(2, 1, &[0, 0], &[[255, 0, 0]], Some(0), None, None).unwrap(); + let d = photocraft_io::import("in.png", &png).unwrap().document; + let r = photocraft_io::export(&d, "out.png", &Default::default()).unwrap(); + assert_eq!(photocraft_codecs::png_palette(&r.bytes).unwrap(), photocraft_codecs::png_palette(&png).unwrap()); + assert_eq!(photocraft_codecs::decode(&r.bytes).unwrap().data(), photocraft_codecs::decode(&png).unwrap().data()); +} + +#[test] +fn indexed_png_rejects_opaque_pixels_without_an_opaque_entry() { + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[255, 0, 0]], transparent: Some(0) }); + let err = photocraft_io::export(&d, "out.png", &Default::default()).unwrap_err(); + assert!(matches!(err, photocraft_io::IoError::Unsupported(_)), "{err}"); + assert!(err.to_string().contains("no opaque entry"), "{err}"); +} + +#[test] +fn duotone_exports_the_inks_as_rgb() { + // Mid gray background. + let mut d = Document::with_background("m", Size::new(4, 2), ColorMode::Grayscale, SampleType::U8, Color::rgba(0.5, 0.5, 0.5, 1.0)); + d.mode = ColorMode::Duotone; + d.duotone = Some(Duotone { inks: vec![DuotoneInk::new("Black", [0.0; 3]), DuotoneInk::new("Orange", [1.0, 0.5, 0.0])], psd_raw: None }); + let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("Duotone")), "{:?}", r.warnings); + let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; + let px = back.layers[0].surface().unwrap().rgba(0, 0); + assert!(px[0] > px[2] + 0.05, "warm: {px:?}"); +} + +#[test] +fn indexed_png_keeps_the_document_resolution() { + // #3047: the palette PNG lost its pHYs chunk and reopened at 72 ppi. + let mut d = doc(ColorMode::Indexed); + d.resolution_dpi = 300.0; + d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0]], transparent: None }); + let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); + assert_eq!(r.bytes[25], 3, "still a palette PNG"); + let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; + assert!((back.resolution_dpi - 300.0).abs() < 0.01, "{}", back.resolution_dpi); +} + +#[test] +fn indexed_bmp_is_palette_bmp() { + // The BMP export wrote an Indexed Color document as 24-bit RGB, so reopening it lost the mode (#3869 follow-up). + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0], [0, 255, 0]], transparent: None }); + let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap(); + assert_eq!(u16::from_le_bytes([r.bytes[28], r.bytes[29]]), 8, "8 bits per pixel"); + assert!(r.warnings.iter().any(|w| w.contains("palette BMP")), "{:?}", r.warnings); + let back = photocraft_io::import("x.bmp", &r.bytes).unwrap().document; + assert_eq!(back.mode, ColorMode::Indexed); + assert_eq!(back.color_table.as_ref().map(|t| t.colors.clone()), Some(vec![[0, 0, 0], [255, 0, 0], [0, 255, 0]])); + let px = back.layers[0].surface().unwrap().rgba(1, 1); + assert_eq!(&px[..3], &[1.0, 0.0, 0.0]); +} + +#[test] +fn indexed_bmp_warns_that_it_has_no_transparency() { + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0]], transparent: Some(0) }); + let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("no transparency")), "{:?}", r.warnings); +} + +#[test] +fn indexed_bmp_exports_when_its_only_palette_entry_is_marked_transparent() { + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[255, 0, 0]], transparent: Some(0) }); + + let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap(); + + assert_eq!(u16::from_le_bytes([r.bytes[28], r.bytes[29]]), 8, "8 bits per pixel"); + let back = photocraft_io::import("x.bmp", &r.bytes).unwrap().document; + assert_eq!(&back.layers[0].surface().unwrap().rgba(1, 1)[..3], &[1.0, 0.0, 0.0]); +} + +#[test] +fn indexed_bmp_matches_opaque_pixels_against_the_transparent_palette_entry() { + let mut d = doc(ColorMode::Indexed); + d.color_table = Some(ColorTable { colors: vec![[255, 0, 0], [0, 0, 255]], transparent: Some(0) }); + + let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap(); + + let back = photocraft_io::import("x.bmp", &r.bytes).unwrap().document; + assert_eq!(&back.layers[0].surface().unwrap().rgba(1, 1)[..3], &[1.0, 0.0, 0.0]); +} + +#[test] +fn indexed_bmp_without_a_usable_table_falls_back_to_rgb() { + // No table (or one past 256 entries) keeps the old 24-bit path rather than failing the save. + for table in [None, Some(ColorTable { colors: vec![[0; 3]; 257], transparent: None })] { + let mut d = doc(ColorMode::Indexed); + d.color_table = table; + let r = photocraft_io::export(&d, "x.bmp", &Default::default()).unwrap(); + assert_eq!(u16::from_le_bytes([r.bytes[28], r.bytes[29]]), 24); + } +} diff --git a/crates/io/tests/multichannel.rs b/crates/io/tests/it/multichannel.rs similarity index 61% rename from crates/io/tests/multichannel.rs rename to crates/io/tests/it/multichannel.rs index d6016070d1..fd8f8448c3 100644 --- a/crates/io/tests/multichannel.rs +++ b/crates/io/tests/it/multichannel.rs @@ -46,10 +46,21 @@ fn alpha_type_channels_import_as_alpha_and_round_trip() { assert!((a.opacity - 0.5).abs() < 1e-6); assert_eq!(a.indicates, ColorIndicates::MaskedAreas); - // It still prints as black ink, so the composite is what a black spot channel gives. + // It still prints as black ink, so the composite is what a black spot channel gives. The + // alpha is stored as-is (1 = selected), so that ink is its inverse, as on the PSD plane. let mut as_ink = d.clone(); + let canvas = d.bounds(); + let ink: Vec = d.channels[1].surface.read_region(canvas).iter().map(|v| 1.0 - v).collect(); + let mut s = photocraft_raster::Surface::new(d.channels[1].surface.format()); + s.write_region(canvas, &ink); + as_ink.channels[1].surface = s; as_ink.channels[1].spot = Some((photocraft_color::Color::BLACK, 0.0)); - assert_eq!(photocraft_compose::flatten(&d).px, photocraft_compose::flatten(&as_ink).px); + // Within f32 rounding: the inverted copy is re-quantised to the channel depth. + let (shown, inked) = (photocraft_compose::flatten(&d).px, photocraft_compose::flatten(&as_ink).px); + assert_eq!(shown.len(), inked.len()); + for (a, b) in shown.iter().zip(&inked) { + assert!(a.iter().zip(b).all(|(x, y)| (x - y).abs() < 1e-5), "{a:?} vs {b:?} ({depth}-bit)"); + } // Saved back unchanged: same DisplayInfo, same planes, same channels on reopening. let (back, d2) = roundtrip(&d); @@ -62,6 +73,78 @@ fn alpha_type_channels_import_as_alpha_and_round_trip() { } } +/// Stored samples of one big-endian plane at `depth` (8 or 16), from 0..=1 tone values. +fn plane(vals: &[f32], depth: u16) -> Vec { + let max = if depth == 16 { 65535.0 } else { 255.0 }; + vals.iter() + .flat_map(|v| { + let s = (v * max).round() as u16; + if depth == 16 { s.to_be_bytes().to_vec() } else { vec![s as u8] } + }) + .collect() +} + +/// A 4×1 Multichannel file laid out like Photoshop writes one (measured with a file Photoshop saved +/// after RGB → Multichannel): a cyan spot plane stored as ink-inverted tone (0 = solid ink) and a +/// kind-0 alpha plane stored as-is (max = selected), selected in the left half. +fn ink_and_alpha_file(depth: u16) -> PsdFile { + let mut f = testgen::merged_only(Version::Psd, ColorMode::Multichannel, depth, Compression::Rle, 4, 1); + let mut planes = plane(&[0.0, 1.0, 0.0, 1.0], depth); + planes.extend(plane(&[1.0, 1.0, 0.0, 0.0], depth)); + f.image_data = photocraft_psd::ImageData::encode(Compression::Rle, &planes, &f.header).unwrap(); + let mut info = 1u32.to_be_bytes().to_vec(); + display_record(&mut info, [0, 65535, 65535], 0, 2); + display_record(&mut info, [65535, 0, 0], 50, 0); + f.resources.push(ImageResource::new(1077, info)); + f +} + +fn values(ch: &photocraft_doc::AlphaChannel) -> Vec { + ch.surface.read_region(photocraft_geom::Rect::new(0, 0, 4, 1)).chunks_exact(ch.surface.channels()).map(|v| v[0]).collect() +} + +#[test] +fn alpha_type_channels_are_stored_as_is_not_as_ink() { + for depth in [8, 16] { + let f = ink_and_alpha_file(depth); + let (d, _) = psd_to_document(&f); + // A white (selected) alpha plane imports as 1.0, like an alpha channel of any other mode; + // the spot plane is still read as ink density (stored 0 = solid ink). + assert_eq!(d.channels[1].spot, None); + assert_eq!(values(&d.channels[1]), [1.0, 1.0, 0.0, 0.0], "alpha channel ({depth}-bit)"); + assert_eq!(values(&d.channels[0]), [1.0, 0.0, 1.0, 0.0], "spot channel ({depth}-bit)"); + + // The composite is what Photoshop shows: the alpha prints as black ink where unselected. + let px = photocraft_compose::flatten(&d).px; + assert_eq!(px.iter().map(|p| [p[0], p[1], p[2]]).collect::>(), [[0.0, 1.0, 1.0], [1.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]); + + // Saved back byte for byte. + let (back, d2) = roundtrip(&d); + assert_eq!(back.decode_merged().unwrap(), f.decode_merged().unwrap(), "{depth}-bit"); + assert_eq!(values(&d2.channels[1]), [1.0, 1.0, 0.0, 0.0]); + } +} + +#[test] +fn a_full_alpha_channel_exports_as_a_full_plane() { + use photocraft_color::{Color, ColorMode as Mode, PixelFormat, SampleType}; + for (sample, depth) in [(SampleType::U8, 8u16), (SampleType::U16, 16)] { + let mut doc = photocraft_doc::Document::new("m", photocraft_doc::Size::new(4, 1), Mode::Multichannel, sample); + let fmt = PixelFormat::new(Mode::Grayscale, sample, false); + let surface = |v: f32| { + let mut s = photocraft_raster::Surface::new(fmt); + s.write_region(photocraft_geom::Rect::new(0, 0, 4, 1), &[v; 4]); + s + }; + doc.channels.push(photocraft_doc::AlphaChannel { spot: Some((Color::BLACK, 0.0)), ..photocraft_doc::AlphaChannel::new("Black", surface(1.0)) }); + doc.channels.push(photocraft_doc::AlphaChannel::new("Alpha 1", surface(1.0))); + let (back, _) = roundtrip(&doc); + let mut want = plane(&[0.0; 4], depth); + want.extend(plane(&[1.0; 4], depth)); + assert_eq!(back.decode_merged().unwrap(), want, "solid ink is stored 0, a full alpha max ({depth}-bit)"); + } +} + #[test] fn resources_and_global_blocks_survive_a_save() { let mut f = file(8); diff --git a/crates/io/tests/orientation.rs b/crates/io/tests/it/orientation.rs similarity index 100% rename from crates/io/tests/orientation.rs rename to crates/io/tests/it/orientation.rs diff --git a/crates/io/tests/pattern_fill_psd.rs b/crates/io/tests/it/pattern_fill_psd.rs similarity index 100% rename from crates/io/tests/pattern_fill_psd.rs rename to crates/io/tests/it/pattern_fill_psd.rs diff --git a/crates/io/tests/pcraft_manifest_name.rs b/crates/io/tests/it/pcraft_manifest_name.rs similarity index 98% rename from crates/io/tests/pcraft_manifest_name.rs rename to crates/io/tests/it/pcraft_manifest_name.rs index 285fa02203..fffb88fa8b 100644 --- a/crates/io/tests/pcraft_manifest_name.rs +++ b/crates/io/tests/it/pcraft_manifest_name.rs @@ -1,7 +1,7 @@ //! A `.pcraft` bundle's manifest reports the file it is in (#3312): writing a document to a new //! name must not leave the previous one inside, on every path that produces `.pcraft` bytes. -mod common; +use crate::common; use common::*; use photocraft_color::{ColorMode, SampleType}; diff --git a/crates/io/tests/pdn_corpus.rs b/crates/io/tests/it/pdn_corpus.rs similarity index 98% rename from crates/io/tests/pdn_corpus.rs rename to crates/io/tests/it/pdn_corpus.rs index 1e67b3d22b..c85b87f3ee 100644 --- a/crates/io/tests/pdn_corpus.rs +++ b/crates/io/tests/it/pdn_corpus.rs @@ -1,5 +1,5 @@ //! Local PDN oracles are not bundled. Set PDN_TEST_DIR to a directory containing PDNs and their -//! exported PNGs, then run `cargo test -p photocraft-io --test pdn_corpus -- --ignored --nocapture`. +//! exported PNGs, then run `cargo test -p photocraft-io --test it -- pdn_corpus:: --ignored --nocapture`. use std::path::{Path, PathBuf}; diff --git a/crates/io/tests/png_text_metadata.rs b/crates/io/tests/it/png_text_metadata.rs similarity index 100% rename from crates/io/tests/png_text_metadata.rs rename to crates/io/tests/it/png_text_metadata.rs diff --git a/crates/io/tests/it/psd16_merged_transparency.rs b/crates/io/tests/it/psd16_merged_transparency.rs new file mode 100644 index 0000000000..03030e557c --- /dev/null +++ b/crates/io/tests/it/psd16_merged_transparency.rs @@ -0,0 +1,63 @@ +//! Opening and re-saving a Photoshop 16/32-bit file whose merged transparency is marked only by +//! its name keeps the channel count: the "Transparency" channel is the composite's alpha, not a +//! user alpha channel that the next save writes a second time (#4654). +//! +//! Photoshop's 16-bit PSB in the report has four channels, a positive layer count in `Lr16`, no +//! `Mt16` block, and one alpha channel name, "Transparency". Re-saving it wrote five channels. + +use crate::common; + +use common::raster; +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::Document; +use photocraft_geom::{Rect, Size}; +use photocraft_io::*; +use photocraft_psd::PsdFile; + +/// The unicode alpha channel names (resource 1045), empty without it. +fn names(file: &PsdFile) -> Vec { + let Some(data) = file.resources.iter().find(|r| r.id == 1045).map(|r| &r.data) else { return Vec::new() }; + let mut out = Vec::new(); + let mut at = 0; + while at + 4 <= data.len() { + let n = u32::from_be_bytes(data[at..at + 4].try_into().unwrap()) as usize; + let units: Vec = data[at + 4..at + 4 + n * 2].chunks(2).map(|c| u16::from_be_bytes([c[0], c[1]])).collect(); + out.push(String::from_utf16_lossy(&units).trim_end_matches('\0').to_string()); + at += 4 + n * 2; + } + out +} + +#[test] +fn resave_keeps_a_name_marked_merged_transparency() { + for depth in [SampleType::U16, SampleType::F32] { + for path in ["x.psd", "x.psb"] { + let case = format!("{depth:?} {path}"); + let mut doc = Document::new("t", Size::new(8, 6), ColorMode::Rgb, depth); + let fmt = doc.pixel_format(); + doc.layers.push(raster("shape", fmt, Rect::new(1, 1, 5, 4), 3, true)); + let ours = PsdFile::from_bytes(&export(&doc, path, &ExportOptions::default()).expect("export").bytes).expect("parse"); + assert_eq!(ours.header.channels, 4, "{case}: colour + merged transparency"); + + // Photoshop's form: a positive layer count, no `Mt16`/`Mt32` marker, and the merged + // transparency named in the alpha channel names. + let mut ps = ours.clone(); + ps.layer_info.as_mut().expect("layers").merged_alpha = false; + ps.global_blocks.retain(|b| !b.is_merged_transparency_marker()); + assert!(!ps.merged_has_alpha(), "{case}: nothing marks it yet"); + let name: Vec = [0u8, 0, 0, 13].into_iter().chain("Transparency\0".encode_utf16().flat_map(u16::to_be_bytes)).collect(); + ps.resources.retain(|r| !matches!(r.id, 1006 | 1045)); + ps.resources.push(photocraft_psd::ImageResource::new(1045, name)); + assert!(ps.merged_has_alpha(), "{case}: the name marks the merged transparency"); + + let opened = import(path, &ps.to_bytes().expect("write")).expect("import").document; + let user: Vec<_> = opened.channels.iter().map(|c| c.name.as_str()).collect(); + assert!(user.is_empty(), "{case}: no user alpha channel from the transparency: {user:?}"); + + let resaved = PsdFile::from_bytes(&export(&opened, path, &ExportOptions::default()).expect("re-export").bytes).expect("reparse"); + assert_eq!(resaved.header.channels, 4, "{case}: a save with no edits keeps the channel count"); + let resaved_names = names(&resaved); + assert!(resaved_names.iter().filter(|n| *n == "Transparency").count() <= 1, "{case}: no duplicate Transparency: {resaved_names:?}"); + } + } +} diff --git a/crates/io/tests/psd32_export.rs b/crates/io/tests/it/psd32_export.rs similarity index 99% rename from crates/io/tests/psd32_export.rs rename to crates/io/tests/it/psd32_export.rs index 9ff2b4113b..f9a297a7f6 100644 --- a/crates/io/tests/psd32_export.rs +++ b/crates/io/tests/it/psd32_export.rs @@ -6,7 +6,7 @@ //! resources, layer info in an `Lr32` global block with an empty classic layer info, channel //! compression codes valid for 32-bit data, and a raw big-endian float merged image. -mod common; +use crate::common; use common::Features; use photocraft_color::{ColorMode, SampleType}; @@ -226,11 +226,11 @@ fn mt32_marks_the_merged_transparency() { let plane = |v: [f32; 2]| v.into_iter().flat_map(f32::to_be_bytes).collect::>(); file.image_data.data = [plane([0.0, 1.0]), plane([0.0, 1.0]), plane([0.0, 1.0]), plane([1.0, 0.0])].concat(); let name: Vec = [0u8, 0, 0, 13].into_iter().chain("Transparency\0".encode_utf16().flat_map(u16::to_be_bytes)).collect(); - file.resources.retain(|r| r.id != 1045); - file.resources.push(photocraft_psd::ImageResource::new(1045, name)); + file.resources.retain(|r| !matches!(r.id, 1006 | 1045)); assert!(!file.merged_has_alpha(), "a positive layer count alone"); file.global_blocks.push(photocraft_psd::TaggedBlock::new(*b"Mt32", Vec::new())); assert!(file.merged_has_alpha()); + file.resources.push(photocraft_psd::ImageResource::new(1045, name)); let merged = merged_composite(&file).unwrap(); assert_eq!(merged.iter().map(|p| p[3]).collect::>(), vec![1.0, 0.0]); let opened = import("transparent.psd", &file.to_bytes().unwrap()).unwrap(); diff --git a/crates/io/tests/psd_background.rs b/crates/io/tests/it/psd_background.rs similarity index 100% rename from crates/io/tests/psd_background.rs rename to crates/io/tests/it/psd_background.rs diff --git a/crates/io/tests/it/psd_block_alignment.rs b/crates/io/tests/it/psd_block_alignment.rs new file mode 100644 index 0000000000..279a352cf7 --- /dev/null +++ b/crates/io/tests/it/psd_block_alignment.rs @@ -0,0 +1,96 @@ +//! Every layer-level tagged block of an exported PSD/PSB is a multiple of 4 bytes long. +//! +//! Photoshop pads layer-level blocks to 4 inside the length field; all of its own files do (567 +//! fixtures checked), and a regenerated block that stopped at an even but unaligned size left the +//! layer record's extra data unaligned: a Pattern Fill's `PtFl` came out 2 bytes short of the +//! boundary for half of all pattern names. + +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{Adjustment, Document, Fill, Layer, LayerContent, Pattern}; +use photocraft_geom::{Rect, Size}; +use photocraft_io::*; +use photocraft_psd::PsdFile; +use photocraft_raster::Surface; + +fn pattern(name: &str) -> Pattern { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 8, 8), &[0.8, 0.8, 0.8, 1.0]); + s.fill_rect(Rect::new(0, 0, 4, 4), &[0.2, 0.4, 0.9, 1.0]); + Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: name.into(), width: 8, height: 8, surface: s } +} + +/// One Pattern Fill layer per name; the name's length moves the descriptor's size by two bytes +/// per character, so consecutive lengths cover every residue modulo 4. +fn doc(depth: SampleType, names: &[&str]) -> Document { + let mut d = Document::new("p", Size::new(40, 24), ColorMode::Rgb, depth); + for name in names { + let p = pattern(name); + let fill = Fill::Pattern { name: (*name).into(), scale: 1.0, id: p.id.clone(), angle: 0.0, link: true, phase: (0.0, 0.0) }; + d.patterns.push(p); + d.layers.push(Layer::new(format!("Fill {name}"), LayerContent::Fill(fill))); + } + d +} + +#[test] +fn layer_blocks_are_four_byte_aligned() { + let names = ["A", "Ab", "Abc", "Abcd", "Dots", "Checker", "Wood grain"]; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for path in ["x.psd", "x.psb"] { + let bytes = export(&doc(depth, &names), path, &ExportOptions::default()).expect("export").bytes; + let file = PsdFile::from_bytes(&bytes).expect("reparse"); + let mut fills = 0; + for layer in file.layers() { + for b in &layer.blocks { + let key = String::from_utf8_lossy(&b.key).into_owned(); + assert_eq!(b.data.len() % 4, 0, "{depth:?} {path}: layer {:?} block {key} is {} bytes", layer.name(), b.data.len()); + } + fills += usize::from(layer.block(b"PtFl").is_some()); + } + assert_eq!(fills, names.len(), "{depth:?} {path}: every pattern fill keeps its PtFl block"); + } + } +} + +#[test] +fn generated_gradient_map_blocks_are_four_byte_aligned() { + let cases = [(vec![(0.0, [0.04, 0.1, 0.18]), (1.0, [1.0; 3])], 128), (vec![(0.0, [0.04, 0.1, 0.18]), (0.5, [0.4, 0.2, 0.1]), (1.0, [1.0; 3])], 148)]; + for (stops, expected_length) in cases { + let mut doc = Document::new("gradient", Size::new(16, 16), ColorMode::Rgb, SampleType::U8); + doc.layers.push(Layer::new("Gradient Map", LayerContent::Adjustment(Adjustment::GradientMap { stops, reverse: false, dither: false }))); + let bytes = export(&doc, "gradient.psd", &ExportOptions::default()).expect("export").bytes; + let file = PsdFile::from_bytes(&bytes).expect("reparse"); + let block = file.layers().iter().find_map(|layer| layer.block(b"grdm")).expect("Gradient Map block"); + assert_eq!(block.data.len(), expected_length, "grdm declared length must include padding"); + } +} + +/// A file saved by an older build carries the 2-mod-4 `grdm` Photoshop refuses; saving it again, +/// with the stops unchanged or only Reverse toggled, must write an aligned block (#8070). +#[test] +fn legacy_unaligned_gradient_map_is_repaired_on_resave() { + let stops = vec![(0.0, [0.04, 0.1, 0.18]), (0.5, [0.4, 0.2, 0.1]), (1.0, [1.0; 3])]; + for mode in [ColorMode::Rgb, ColorMode::Cmyk] { + let mut doc = Document::new("gradient", Size::new(16, 16), mode, SampleType::U8); + doc.layers.push(Layer::new("Gradient Map", LayerContent::Adjustment(Adjustment::GradientMap { stops: stops.clone(), reverse: false, dither: false }))); + let mut file = PsdFile::from_bytes(&export(&doc, "gradient.psd", &ExportOptions::default()).expect("export").bytes).expect("reparse"); + let legacy = file.layers_mut().iter_mut().find_map(|layer| layer.block_mut(b"grdm")).expect("Gradient Map block"); + let unpadded = legacy.data.len() - 2; + legacy.data.truncate(unpadded); + let legacy = file.to_bytes().expect("legacy file"); + for reverse in [false, true] { + let mut reopened = import("gradient.psd", &legacy).expect("import").document; + let Some(LayerContent::Adjustment(Adjustment::GradientMap { reverse: r, .. })) = reopened.layers.iter_mut().map(|l| &mut l.content).last() else { + panic!("no Gradient Map layer") + }; + *r = reverse; + let expected = reopened.layers.last().map(|l| l.content.clone()); + let bytes = export(&reopened, "gradient.psd", &ExportOptions::default()).expect("re-save").bytes; + let file = PsdFile::from_bytes(&bytes).expect("reparse"); + let block = file.layers().iter().find_map(|layer| layer.block(b"grdm")).expect("Gradient Map block"); + assert_eq!(block.data.len() % 4, 0, "{mode:?} reverse {reverse}: re-saved grdm is {} bytes", block.data.len()); + let again = import("gradient.psd", &bytes).expect("reopen").document; + assert_eq!(again.layers.last().map(|l| l.content.clone()), expected, "{mode:?} reverse {reverse}: Gradient Map changed on re-save"); + } + } +} diff --git a/crates/io/tests/psd_block_layout.rs b/crates/io/tests/it/psd_block_layout.rs similarity index 99% rename from crates/io/tests/psd_block_layout.rs rename to crates/io/tests/it/psd_block_layout.rs index bb858633b5..d081bfbc43 100644 --- a/crates/io/tests/psd_block_layout.rs +++ b/crates/io/tests/it/psd_block_layout.rs @@ -3,7 +3,7 @@ //! to even, a layer info body not padded to 4 and, in PSB, `cinf`/`lnkE` read and written //! with 4-byte lengths; psd-tools rejected or misaligned all of them. -mod common; +use crate::common; use photocraft_io::*; use photocraft_psd::descriptor::{Descriptor, VersionedDescriptor}; diff --git a/crates/io/tests/psd_damaged_merged.rs b/crates/io/tests/it/psd_damaged_merged.rs similarity index 96% rename from crates/io/tests/psd_damaged_merged.rs rename to crates/io/tests/it/psd_damaged_merged.rs index e3d0970dd9..8e20687bb5 100644 --- a/crates/io/tests/psd_damaged_merged.rs +++ b/crates/io/tests/it/psd_damaged_merged.rs @@ -89,7 +89,7 @@ fn modes_imported_flattened_do_not_recover_from_unusable_layer_records() { for mode in [photocraft_psd::ColorMode::Indexed, photocraft_psd::ColorMode::Multichannel, photocraft_psd::ColorMode::Duotone] { let mut file = layered(SampleType::U8, "x.psd"); file.header.color_mode = mode; - file.image_data = photocraft_psd::ImageData { compression: Compression::Raw, data: Vec::new() }; + file.image_data = photocraft_psd::ImageData::new(Compression::Raw, Vec::new()); assert!(import("flat-mode.psd", &file.to_bytes().unwrap()).is_err(), "{mode:?}"); } } @@ -98,7 +98,7 @@ fn modes_imported_flattened_do_not_recover_from_unusable_layer_records() { fn lost_extra_channels_are_reported_while_layers_remain_usable() { let mut file = layered(SampleType::U8, "x.psd"); file.header.channels = 5; - file.image_data = photocraft_psd::ImageData { compression: Compression::Raw, data: Vec::new() }; + file.image_data = photocraft_psd::ImageData::new(Compression::Raw, Vec::new()); let result = import("extra-channel.psd", &file.to_bytes().unwrap()).unwrap(); assert_eq!(result.document.layer_count(), 1); assert!(result.document.channels.is_empty()); diff --git a/crates/io/tests/psd_empty_channels.rs b/crates/io/tests/it/psd_empty_channels.rs similarity index 99% rename from crates/io/tests/psd_empty_channels.rs rename to crates/io/tests/it/psd_empty_channels.rs index 285f03cfcd..610bb145cc 100644 --- a/crates/io/tests/psd_empty_channels.rs +++ b/crates/io/tests/it/psd_empty_channels.rs @@ -1,4 +1,4 @@ -mod common; +use crate::common; use common::Features; use photocraft_color::{ColorMode, SampleType}; use photocraft_io::{ExportOptions, export}; diff --git a/crates/io/tests/psd_font_identity.rs b/crates/io/tests/it/psd_font_identity.rs similarity index 100% rename from crates/io/tests/psd_font_identity.rs rename to crates/io/tests/it/psd_font_identity.rs diff --git a/crates/io/tests/it/psd_frame_stroke.rs b/crates/io/tests/it/psd_frame_stroke.rs new file mode 100644 index 0000000000..3ae2284543 --- /dev/null +++ b/crates/io/tests/it/psd_frame_stroke.rs @@ -0,0 +1,117 @@ +//! A Photoshop Frame (Frame tool, K) is a layer group whose record carries the frame outline as a +//! vector mask (`vsms`) and its Properties › Stroke as a shape-style `vstk`. The stroke draws +//! along the outline even when nothing is placed in the frame (#4381). + +use photocraft_color::{Color, ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{Document, Fill, Group, Layer, LayerContent, Path, ShapeStroke, Size, StrokeAlign, Subpath, VectorMask}; +use photocraft_io::vector_map::{parse_vstk, vstk_bytes}; +use photocraft_io::*; +use photocraft_psd::{PsdFile, SectionType, TaggedBlock}; + +fn red_inside(width: f32) -> ShapeStroke { + ShapeStroke { width, align: StrokeAlign::Inside, paint: Fill::Solid(Color::rgb(1.0, 0.0, 0.0)), ..ShapeStroke::default() } +} + +/// An 80 × 60 white document with an empty rectangular frame over (20, 10)–(60, 50). +fn frame_doc(depth: SampleType, stroke: Option) -> Document { + let mut d = Document::with_background("f", Size::new(80, 60), ColorMode::Rgb, depth, Color::WHITE); + let mut g = Layer::group("Frame 1", vec![]); + if let LayerContent::Group(Group { frame_stroke, .. }) = &mut g.content { + *frame_stroke = stroke; + } + g.vector_mask = Some(VectorMask::new(Path::new(vec![Subpath::polygon(&[(20.0, 10.0), (60.0, 10.0), (60.0, 50.0), (20.0, 50.0)])]))); + d.layers.push(g); + d +} + +fn frame_stroke(d: &Document) -> Option { + d.layers.iter().find_map(|l| match &l.content { + LayerContent::Group(g) => Some(g.frame_stroke.clone()), + _ => None, + })? +} + +fn px(d: &Document, x: i32, y: i32) -> [f32; 4] { + photocraft_compose::flatten_to_surface(d, PixelFormat::RGBA8.with_sample(SampleType::F32), None).rgba(x, y) +} + +/// The group's own record (its top, `lsct` 1 or 2) in a PSD file. +fn frame_record(f: &mut PsdFile) -> &mut photocraft_psd::LayerRecord { + f.layers_mut() + .iter_mut() + .find(|r| r.section_divider().is_some_and(|s| matches!(s.kind, SectionType::OpenFolder | SectionType::ClosedFolder))) + .expect("the frame's group record") +} + +#[test] +fn a_photoshop_frames_vstk_draws_its_stroke_along_the_outline() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + // A file shaped like Photoshop's: the group record carries `vsms` and a `vstk`. + let mut f = document_to_psd(&frame_doc(depth, None)); + frame_record(&mut f).blocks.push(TaggedBlock::new(*b"vstk", vstk_bytes(Some(&red_inside(4.0)), true, 72.0))); + let (d, _) = psd_to_document(&f); + assert_eq!(frame_stroke(&d), Some(red_inside(4.0)), "{depth:?}"); + // Inside the outline, 4 px wide: red on the frame's edge band, white in the middle and + // outside it. + let close = |a: [f32; 4], b: [f32; 3]| (0..3).all(|i| (a[i] - b[i]).abs() < 0.02); + for (x, y) in [(21, 30), (58, 30), (40, 11), (40, 48)] { + assert!(close(px(&d, x, y), [1.0, 0.0, 0.0]), "{depth:?} ({x}, {y}): {:?}", px(&d, x, y)); + } + for (x, y) in [(40, 30), (18, 30), (62, 30), (40, 8)] { + assert!(close(px(&d, x, y), [1.0, 1.0, 1.0]), "{depth:?} ({x}, {y}): {:?}", px(&d, x, y)); + } + } +} + +#[test] +fn a_frame_stroke_round_trips_through_psd() { + let stroke = ShapeStroke { dashes: vec![2.0, 1.0], opacity: 0.5, align: StrokeAlign::Outside, ..red_inside(3.0) }; + let d = frame_doc(SampleType::U8, Some(stroke.clone())); + let bytes = export(&d, "f.psd", &ExportOptions::default()).unwrap().bytes; + let back = import("f.psd", &bytes).unwrap().document; + assert_eq!(frame_stroke(&back), Some(stroke.clone())); + // Outside the outline at half opacity: pink just outside the edge. + let p = px(&back, 18, 30); + assert!((p[1] - 0.5).abs() < 0.05 && p[0] > 0.98, "{p:?}"); + + // Saved again unchanged, the imported `vstk` stays byte for byte. + let mut f = PsdFile::from_bytes(&bytes).unwrap(); + let first = frame_record(&mut f).block(b"vstk").unwrap().data.clone(); + let again = export(&back, "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f2 = PsdFile::from_bytes(&again).unwrap(); + assert_eq!(frame_record(&mut f2).block(b"vstk").unwrap().data, first); + + // Removing the stroke writes it disabled; no stroke at all writes no `vstk`. + let mut cleared = back.clone(); + if let Some(LayerContent::Group(g)) = cleared.layers.iter_mut().map(|l| &mut l.content).find(|c| matches!(c, LayerContent::Group(_))) { + g.frame_stroke = None; + } + let out = export(&cleared, "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f3 = PsdFile::from_bytes(&out).unwrap(); + assert_eq!(parse_vstk(&frame_record(&mut f3).block(b"vstk").unwrap().data, 72.0).unwrap().stroke, None); + let plain = export(&frame_doc(SampleType::U8, None), "f.psd", &ExportOptions::default()).unwrap().bytes; + let mut f4 = PsdFile::from_bytes(&plain).unwrap(); + assert!(frame_record(&mut f4).block(b"vstk").is_none()); +} + +#[test] +fn a_frame_stroke_without_a_vector_mask_or_width_draws_nothing() { + let mut d = frame_doc(SampleType::U8, Some(red_inside(4.0))); + d.layers[1].vector_mask = None; + assert_eq!(px(&d, 21, 30), [1.0, 1.0, 1.0, 1.0]); + let d = frame_doc(SampleType::U8, Some(ShapeStroke { width: 0.0, ..red_inside(4.0) })); + assert_eq!(px(&d, 21, 30), [1.0, 1.0, 1.0, 1.0]); + // Hostile widths from a file render without panicking. + for width in [f32::NAN, f32::INFINITY, -5.0, 1e9] { + let _ = px(&frame_doc(SampleType::U8, Some(ShapeStroke { width, ..red_inside(4.0) })), 21, 30); + } +} + +#[test] +fn an_isolated_frame_draws_its_outside_stroke_past_the_outline() { + let mut d = frame_doc(SampleType::U8, Some(ShapeStroke { align: StrokeAlign::Outside, ..red_inside(3.0) })); + d.layers[1].blend = photocraft_color::BlendMode::Normal; + let p = px(&d, 18, 30); + assert!(p[0] > 0.98 && p[1] < 0.02, "{p:?}"); + assert!(px(&d, 22, 30)[1] > 0.98, "inside stays white"); +} diff --git a/crates/io/tests/it/psd_import_file.rs b/crates/io/tests/it/psd_import_file.rs new file mode 100644 index 0000000000..cd1b08761a --- /dev/null +++ b/crates/io/tests/it/psd_import_file.rs @@ -0,0 +1,179 @@ +//! `import_file` parses a PSD/PSB from the open file (channel data read as it is decoded) and must +//! open the same document, with the same warnings, as importing the file's bytes. + +use photocraft_io::{ImportResult, import, import_file}; +use photocraft_psd::testgen; +use photocraft_raster::Interrupt; + +struct TempDir(std::path::PathBuf); + +impl TempDir { + fn new(tag: &str) -> Self { + let dir = std::env::temp_dir().join(format!("photocraft-import-file-{tag}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + TempDir(dir) + } + + fn write(&self, name: &str, bytes: &[u8]) -> std::path::PathBuf { + let path = self.0.join(name); + std::fs::write(&path, bytes).unwrap(); + path + } +} + +impl Drop for TempDir { + fn drop(&mut self) { + let _ = std::fs::remove_dir_all(&self.0); + } +} + +/// The document's debug form with every `…Id(n)` number blanked. +fn without_ids(doc: &photocraft_doc::Document) -> String { + let s = format!("{doc:?}"); + let mut out = String::with_capacity(s.len()); + let mut rest = s.as_str(); + while let Some(at) = rest.find("Id(") { + out.push_str(&rest[..at + 3]); + rest = rest[at + 3..].trim_start_matches(|c: char| c.is_ascii_digit()); + } + out.push_str(rest); + out +} + +fn open(name: &str, path: &std::path::Path) -> Result { + import_file(name, path, &photocraft_io::ImportOptions::default(), &Interrupt::NONE) +} + +// Every generated case still runs once, with the same document and warning comparisons. +// Separate nextest processes let the runner schedule the matrix alongside other tests rather +// than leaving one multi-minute test on the critical path. +fn import_file_opens_what_importing_the_bytes_opens_part(part: usize) { + let dir = TempDir::new(&format!("same-{part}")); + let mut checked = 0; + for case in testgen::all_cases().into_iter().skip(part).step_by(4) { + let ext = if case.file.header.version.is_psb() { "psb" } else { "psd" }; + let name = format!("case.{ext}"); + let bytes = case.file.to_bytes().unwrap(); + let path = dir.write(&name, &bytes); + let from_bytes = import(&name, &bytes); + let from_file = open(&name, &path); + match (from_bytes, from_file) { + (Ok(a), Ok(b)) => { + // Ids come from a process-wide counter, so the two opens differ only there. + assert!(without_ids(&b.document) == without_ids(&a.document), "{}", case.name); + assert_eq!(b.warnings, a.warnings, "{}", case.name); + checked += 1; + } + (Err(_), Err(_)) => {} + (a, b) => panic!("{}: bytes {:?}, file {:?}", case.name, a.map(|r| r.warnings), b.map(|r| r.warnings)), + } + } + // Four parts each opening at least 13 cases retain (and slightly strengthen) the former + // aggregate floor of more than 50 successful opens. + assert!(checked >= 13, "part {part}: only {checked} cases opened"); +} + +#[test] +fn import_file_opens_what_importing_the_bytes_opens_part_0() { + import_file_opens_what_importing_the_bytes_opens_part(0); +} + +#[test] +fn import_file_opens_what_importing_the_bytes_opens_part_1() { + import_file_opens_what_importing_the_bytes_opens_part(1); +} + +#[test] +fn import_file_opens_what_importing_the_bytes_opens_part_2() { + import_file_opens_what_importing_the_bytes_opens_part(2); +} + +#[test] +fn import_file_opens_what_importing_the_bytes_opens_part_3() { + import_file_opens_what_importing_the_bytes_opens_part(3); +} + +#[test] +fn import_file_reads_other_formats_whole() { + let dir = TempDir::new("other"); + let doc = photocraft_doc::Document::new("x", photocraft_geom::Size::new(3, 2), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + let png = photocraft_io::export(&doc, "x.png", &Default::default()).unwrap().bytes; + let path = dir.write("x.png", &png); + let r = open("x.png", &path).unwrap(); + assert_eq!(r.document.size, doc.size); +} + +#[test] +fn import_file_fails_cleanly() { + let dir = TempDir::new("fail"); + assert!(open("missing.psd", &dir.0.join("missing.psd")).is_err()); + let bytes = testgen::small(photocraft_psd::Version::Psd, photocraft_psd::Compression::Rle).to_bytes().unwrap(); + // Cut inside the layer records, inside the channel data, and inside the merged image. + for cut in [10, 60, bytes.len() / 2, bytes.len() - 5] { + let name = format!("cut{cut}.psd"); + let path = dir.write(&name, &bytes[..cut]); + let from_bytes = import(&name, &bytes[..cut]); + let from_file = open(&name, &path); + // The merged image is not read when the layers are what opens, so a file cut inside it + // can open from the file where the strict parse of its bytes refused it. + if from_bytes.is_ok() { + assert!(from_file.is_ok(), "cut at {cut}"); + } + } +} + +#[test] +fn a_cancelled_import_file_reports_cancelled() { + let dir = TempDir::new("cancel"); + let bytes = testgen::small(photocraft_psd::Version::Psd, photocraft_psd::Compression::Zip).to_bytes().unwrap(); + let path = dir.write("c.psd", &bytes); + let cancel = || true; + let ctl = Interrupt::cancel_only(&cancel); + assert!(matches!(import_file("c.psd", &path, &photocraft_io::ImportOptions::default(), &ctl), Err(photocraft_io::IoError::Cancelled))); +} + +/// A read that fails while the layers are decoded from the file (it is truncated or rewritten, +/// its drive goes away) fails the open: no layer is left empty for a save to lose. +#[test] +fn a_read_failing_while_layers_decode_fails_the_open() { + use photocraft_psd::ReadAt; + /// The file's bytes, failing every read of channel data (as a dropped network share or a + /// truncated file would) except the 2-byte compression fields the parse reads. + struct Failing { + bytes: Vec, + data: Vec<(u64, u64)>, + } + impl ReadAt for Failing { + fn len(&self) -> u64 { + self.bytes.len() as u64 + } + fn read_at(&self, offset: u64, buf: &mut [u8]) -> photocraft_psd::Result<()> { + let end = offset + buf.len() as u64; + if self.data.iter().any(|&(a, b)| offset < b && end > a) { + return Err(photocraft_psd::PsdError::Read("the drive went away".into())); + } + self.bytes.as_slice().read_at(offset, buf) + } + } + for c in [photocraft_psd::Compression::Rle, photocraft_psd::Compression::Raw, photocraft_psd::Compression::Zip] { + let file = testgen::layered(photocraft_psd::Version::Psd, photocraft_psd::ColorMode::Rgb, 8, c); + let bytes = file.to_bytes().unwrap(); + let lazy = photocraft_psd::PsdFile::from_source(photocraft_psd::Source::new(bytes.clone())).unwrap(); + let data: Vec<(u64, u64)> = lazy + .layers() + .iter() + .flat_map(|l| &l.channels) + .filter_map(|ch| ch.stored.as_ref()) + .filter(|s| s.len > 0) + .map(|s| (s.offset, s.offset + s.len)) + .collect(); + assert!(!data.is_empty()); + // Opening the same file whole succeeds: only the failed reads fail the open. + assert!(import("x.psd", &bytes).is_ok()); + let r = photocraft_io::import_source_for_open_with("x.psd", photocraft_psd::Source::new(Failing { bytes, data }), &Interrupt::NONE); + match r { + Err(photocraft_io::IoError::Read(msg)) => assert!(msg.contains("the drive went away"), "{c:?}: {msg}"), + r => panic!("{c:?}: the open didn't fail: {:?}", r.map(|(r, damaged)| (r.warnings, damaged))), + } + } +} diff --git a/crates/io/tests/psd_kerning.rs b/crates/io/tests/it/psd_kerning.rs similarity index 61% rename from crates/io/tests/psd_kerning.rs rename to crates/io/tests/it/psd_kerning.rs index b75a6542ae..d873d4a3e2 100644 --- a/crates/io/tests/psd_kerning.rs +++ b/crates/io/tests/it/psd_kerning.rs @@ -144,6 +144,167 @@ fn duplicate_preserved_text_indices_get_distinct_txt2_objects() { } } +/// A type layer added below preserved ones takes a number past theirs: claiming the next free +/// number on the way up renumbered every layer above it onto its neighbour's `Txt2` object +/// (#7773, 70 of 74 layers on the #7698 file). +#[test] +fn new_text_layer_below_keeps_the_preserved_text_indices() { + let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + for (i, text) in ["AV", "BC"].into_iter().enumerate() { + let mut t = layer(text, &[(2, Kerning::Optical, 0.0)]); + photocraft_text::TextEngine::new().render_layer(&mut t, doc.resolution_dpi, doc.pixel_format()); + let raw = photocraft_text::psd::build_tysh(&t, doc.resolution_dpi, None); + t.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::set_text_index(&raw, i as i32).unwrap())); + doc.layers.push(Layer::new(text, LayerContent::Text(t))); + } + let mut t = layer("new", &[(3, Kerning::Metrics, 0.0)]); + photocraft_text::TextEngine::new().render_layer(&mut t, doc.resolution_dpi, doc.pixel_format()); + doc.layers.insert(0, Layer::new("new", LayerContent::Text(t))); + let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes; + let back = photocraft_io::import("k.psd", &bytes).unwrap().document; + let indices: Vec<_> = back + .layers + .iter() + .map(|l| { + let LayerContent::Text(t) = &l.content else { panic!("not text") }; + (l.name.as_str(), t.psd_raw.as_deref().and_then(|d| photocraft_text::psd::text_index(d))) + }) + .collect(); + assert_eq!(indices, [("new", Some(2)), ("AV", Some(0)), ("BC", Some(1))]); +} + +/// A Photoshop-shaped `Txt2` block: one object per `(text, style keys)` pair, each with its view +/// naming its own frame. +fn photoshop_txt2(objects: &[(&str, &str)]) -> Vec { + let bom = "\u{fe}\u{ff}"; + let frame = "<< /0 << /2 << /2 [ 1.0 0.0 0.0 1.0 5.0 6.0 ] >> >> >> "; + let mut frames = String::new(); + let mut body = String::new(); + for (i, (text, style)) in objects.iter().enumerate() { + let utf16: String = format!("{text}\r").chars().flat_map(|c| ['\0', c]).collect(); + let len = text.len() + 1; + frames.push_str(frame); + body.push_str(&format!( + "<< /0 << /0 ({bom}{utf16}) /5 << /0 [ << /0 << /0 << /0 ({bom}) /5 << /7 1.2 >> >> >> /1 {len} >> ] >> /6 << /0 [ << /0 << /0 << /0 ({bom}) /6 << /0 0 /1 12.0 /11 1 /53 << /99 /SimplePaint /0 << /0 1 /1 [ 1.0 0.0 0.0 0.0 ] >> >> {style} >> >> >> /1 {len} >> ] >> >> /1 << /0 [ << /0 {i} >> ] /1 << /0 [ << /0 << /1 1 >> /1 {len} >> ] >> /2 [ << /99 /PC /6 [ << /99 /F /10 {i} >> ] >> ] >> >> " + )); + } + let font: String = "MyriadPro-Regular".chars().flat_map(|c| ['\0', c]).collect(); + format!("\n\n/98 << /0 7 >> /0 << /1 << /0 [ << /0 << /99 /CoolTypeFont /0 << /0 ({bom}{font}) /2 1 >> >> >> ] >> /8 << /0 [ {frames}] >> >> /1 << /0 << /0 1 >> /1 [ {body}] /2 << /0 7 >> >>").chars().map(|c| c as u32 as u8).collect() +} + +/// A reopened type layer: its name, `TextIndex` and font features. +type Reopened = (String, Option, Vec); + +/// A Photoshop file whose `Txt2` holds `objects` and whose type layers are `kept` (text and +/// `TextIndex`, bottom first), with a type layer "Hi" made the way the Type tool makes one +/// (`build_tysh`, no index of its own) inserted at `at`, saved and reopened: each layer's +/// index and font features (but `calt`, on by default and written on as Photoshop does), and +/// "Hi"'s style run in the saved `Txt2`. The kept layers are styled as their objects are (font +/// and size too), so their objects are saved unchanged, which this checks. +fn add_type_layer(objects: &[(&str, &str)], kept: &[(&str, i32)], at: usize) -> (Vec, E) { + let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + for (text, index) in kept { + let mut t = layer(text, &[(text.len(), Kerning::Metrics, 0.0)]); + // `photoshop_txt2` writes MyriadPro-Regular at 12 pt (the document is at 72 dpi). + t.runs[0].style.size_pt = 12.0; + t.runs[0].style.postscript_name = Some("MyriadPro-Regular".into()); + t.sync_summary(); + photocraft_text::TextEngine::new().render_layer(&mut t, doc.resolution_dpi, doc.pixel_format()); + let raw = photocraft_text::psd::set_text_index(&photocraft_text::psd::build_tysh(&t, doc.resolution_dpi, None), *index).unwrap(); + // As opening the file does: a kept layer's style holds what its `Txt2` object says (#8415: + // a layer whose style disagrees with its object is saved as a style edit). + photocraft_text::psd::apply_txt2(&mut t, &raw, &photocraft_text::psd::parse_txt2(&photoshop_txt2(objects)).unwrap()); + t.psd_raw = Some(std::sync::Arc::new(raw)); + doc.layers.push(Layer::new(*text, LayerContent::Text(t))); + } + doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Txt2", std::sync::Arc::new(photoshop_txt2(objects)))]; + let mut t = layer("Hi", &[(2, Kerning::Metrics, 0.0)]); + t.runs[0].style.size_pt = 30.0; + photocraft_text::TextEngine::new().render_layer(&mut t, doc.resolution_dpi, doc.pixel_format()); + t.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::build_tysh(&t, doc.resolution_dpi, None))); + doc.layers.insert(at, Layer::new("Hi", LayerContent::Text(t))); + let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes; + let file = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); + let txt2 = file.global_blocks.iter().find(|b| &b.key == b"Txt2").and_then(|b| photocraft_text::psd::parse_txt2(&b.data)).unwrap(); + let back = photocraft_io::import("k.psd", &bytes).unwrap().document; + let layers: Vec<_> = back + .layers + .iter() + .map(|l| { + let LayerContent::Text(t) = &l.content else { panic!("not text") }; + let index = t.psd_raw.as_deref().and_then(|d| photocraft_text::psd::text_index(d)); + (l.name.clone(), index, t.char_runs().iter().flat_map(|r| r.style.features.iter().filter(|f| f.tag != "calt").map(|f| f.tag.clone())).collect()) + }) + .collect(); + let source = photocraft_text::psd::parse_txt2(&photoshop_txt2(objects)).unwrap(); + let object = |txt2: &E, i: i32| txt2.path(&["1", "1"]).and_then(E::as_array).and_then(|a| a.get(usize::try_from(i).ok()?)).cloned(); + for (text, index) in kept { + assert_eq!(object(&txt2, *index), object(&source, *index), "{text}: the kept layer's object is saved unchanged"); + } + let hi = usize::try_from(layers.iter().find(|l| l.0 == "Hi").and_then(|l| l.1).unwrap()).unwrap(); + let run = txt2.path(&["1", "1"]).and_then(E::as_array).unwrap()[hi].path(&["0", "6", "0"]).and_then(E::as_array).unwrap()[0].clone(); + (layers, run) +} + +/// The layer `name` in `add_type_layer`'s result. +fn named<'a>(layers: &'a [Reopened], name: &str) -> &'a Reopened { + layers.iter().find(|l| l.0 == name).unwrap() +} + +/// A new type layer takes the structure of one of the file's objects but not its style: the +/// template's style runs carried its font features (here `salt` and `ss01`) into the new layer. +/// Its style run is the template's complete one (Photoshop quit on a run holding only the +/// auto-kern mode, and reads the character style from it) with the layer's own size. +#[test] +fn new_text_layer_takes_no_style_from_the_txt2_template() { + let (layers, run) = add_type_layer(&[("AV", ""), ("BC", "/28 true /85 1")], &[("AV", 0), ("BC", 1)], 2); + assert_eq!(named(&layers, "Hi").1..=named(&layers, "Hi").1, Some(2)..=Some(2)); + assert!(named(&layers, "Hi").2.is_empty(), "{layers:?}"); + let style = run.path(&["0", "0", "6"]).unwrap(); + assert_eq!(style.get("1").and_then(E::as_f64), Some(30.0), "the layer's own size"); + assert_eq!(style.get("28"), Some(&E::Bool(false))); + assert_eq!(style.path(&["53", "0", "0"]), Some(&E::Int(1)), "the layer's fill colour, RGB"); + assert_eq!(style.get("11"), Some(&E::Int(1))); +} + +/// A new type layer is numbered past the file's `Txt2` objects, not only past the kept indices: +/// a deleted type layer's object ("Gone") is not reused with its style, whether it held the +/// highest index or index 0 (the Type tool's layer used to bring a `TextIndex` 0 of its own). +#[test] +fn new_text_layer_skips_a_deleted_layers_txt2_object() { + let (layers, _) = add_type_layer(&[("AV", ""), ("BC", ""), ("Gone", "/28 true")], &[("AV", 0), ("BC", 1)], 2); + assert_eq!((named(&layers, "Hi").1, named(&layers, "Hi").2.is_empty()), (Some(3), true)); + let (layers, _) = add_type_layer(&[("Gone", "/28 true"), ("AV", ""), ("BC", "")], &[("AV", 1), ("BC", 2)], 2); + assert_eq!((named(&layers, "Hi").1, named(&layers, "Hi").2.is_empty()), (Some(3), true)); +} + +/// A type layer added below the one holding index 0 neither takes that index nor its style. +#[test] +fn new_text_layer_at_the_bottom_keeps_index_0_where_it_was() { + let (layers, _) = add_type_layer(&[("AV", "/28 true"), ("BC", "")], &[("AV", 0), ("BC", 1)], 0); + assert_eq!(named(&layers, "AV").1, Some(0)); + assert_eq!(named(&layers, "AV").2, ["salt"]); + assert_eq!((named(&layers, "Hi").1, named(&layers, "Hi").2.is_empty()), (Some(2), true)); +} + +/// An edited object takes the layer's style, not the old text's: "Hello world" with Stylistic +/// Alternates on "Hello " edited to "world" without them comes back without `salt`. +#[test] +fn edited_text_layer_takes_its_own_style_runs() { + let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); + let mut t = layer("world", &[(5, Kerning::Metrics, 0.0)]); + photocraft_text::TextEngine::new().render_layer(&mut t, doc.resolution_dpi, doc.pixel_format()); + let raw = photocraft_text::psd::build_tysh(&t, doc.resolution_dpi, None); + t.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::set_text_index(&raw, 0).unwrap())); + doc.layers.push(Layer::new("edited", LayerContent::Text(t))); + doc.metadata.psd_global_blocks = vec![(*b"8BIM", *b"Txt2", std::sync::Arc::new(photoshop_txt2(&[("Hello world", "/28 true")])))]; + let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes; + let back = photocraft_io::import("k.psd", &bytes).unwrap().document; + let LayerContent::Text(t) = &back.layers[0].content else { panic!("not text") }; + assert_eq!(t.text, "world"); + assert!(t.char_runs().iter().all(|r| r.style.features.iter().all(|f| f.tag == "calt")), "the old text's salt came back"); +} + /// A file-controlled `TextIndex` must not size the save: `i32::MAX` is ignored (the regenerated /// `Txt2` is sized by real text objects, never by a file's numbers) and the document still saves /// and reopens with its text. @@ -186,6 +347,9 @@ fn txt2_extras_survive_a_resave_of_unchanged_text() { let mut doc = Document::new("k", photocraft_geom::Size::new(64, 32), photocraft_color::ColorMode::Rgb, photocraft_color::SampleType::U8); let mut r = t.clone(); photocraft_text::TextEngine::new().render_layer(&mut r, doc.resolution_dpi, doc.pixel_format()); + // The file's layer: its `TySh` names object 0. + let raw = photocraft_text::psd::build_tysh(&r, doc.resolution_dpi, None); + r.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::set_text_index(&raw, 0).unwrap())); doc.layers.push(Layer::new("kept", LayerContent::Text(r))); doc.metadata.psd_global_blocks.push((*b"8BIM", *b"Txt2", std::sync::Arc::new(prev.clone()))); let bytes = photocraft_io::export(&doc, "k.psd", &Default::default()).unwrap().bytes; diff --git a/crates/io/tests/psd_layer_block_length.rs b/crates/io/tests/it/psd_layer_block_length.rs similarity index 99% rename from crates/io/tests/psd_layer_block_length.rs rename to crates/io/tests/it/psd_layer_block_length.rs index acb4a6b15c..71f8e2552d 100644 --- a/crates/io/tests/psd_layer_block_length.rs +++ b/crates/io/tests/it/psd_layer_block_length.rs @@ -4,7 +4,7 @@ //! Photoshop writes these blocks this way. Readers that check the block length exactly (Unity's //! PSD Importer, "Incorrect length for InfoLayers") reject a block whose length counts the padding. -mod common; +use crate::common; use common::Features; use photocraft_color::{ColorMode, SampleType}; diff --git a/crates/io/tests/psd_maximize_compatibility.rs b/crates/io/tests/it/psd_maximize_compatibility.rs similarity index 100% rename from crates/io/tests/psd_maximize_compatibility.rs rename to crates/io/tests/it/psd_maximize_compatibility.rs diff --git a/crates/io/tests/it/psd_noise_fill_roundtrip.rs b/crates/io/tests/it/psd_noise_fill_roundtrip.rs new file mode 100644 index 0000000000..e8fb38b88d --- /dev/null +++ b/crates/io/tests/it/psd_noise_fill_roundtrip.rs @@ -0,0 +1,54 @@ +//! #6272: a Photoshop noise `GdFl` gradient that is not provably constant-RGB still opens as +//! a fill layer — the placeholder ramp stands in where the model has no stops, Photoshop's +//! own pixels ride along as the fill cache — and its raw `GdFl` block survives a re-save byte +//! for byte. Before the fix, `fill_from_desc` returned `None` for those gradients: the layer +//! imported as pixels and the export dropped the `GdFl` (`RASTER_STALE_KEYS`), so opening and +//! re-saving a file silently turned the fill layer into a plain pixel layer. + +use photocraft_doc::{Fill, LayerContent}; +use photocraft_io::*; +use photocraft_psd::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; +use photocraft_psd::testgen; +use photocraft_psd::{Compression, LayerRecord, PsdFile, TaggedBlock, Version}; + +/// Photoshop's default Noise gradient (RGB, min 0/0/0, max 100/100/100) as `GdFl` block data. +fn default_noise_gdfl() -> Vec { + let values = |v: [i32; 4]| Value::List(v.into_iter().map(Value::Integer).collect()); + let gradient = Descriptor::new("Grdn") + .with("GrdF", Value::Enumerated { type_id: Id::new("GrdF"), value: Id::new("ClNs") }) + .with("ClrS", Value::Enumerated { type_id: Id::new("ClrS"), value: Id::new("RGBC") }) + .with("ShTr", Value::Boolean(false)) + .with("VctC", Value::Boolean(true)) + .with("RndS", Value::Integer(9052369)) + .with("Smth", Value::Integer(2048)) + .with("Mnm ", values([0, 0, 0, 0])) + .with("Mxm ", values([100, 100, 100, 100])); + let d = Descriptor::new("null") + .with("Grad", Value::Descriptor(gradient)) + .with("Angl", Value::UnitFloat { unit: *b"#Ang", value: 0.0 }) + .with("Type", Value::Enumerated { type_id: Id::new("Type"), value: Id::new("CstS") }); + VersionedDescriptor::new(d).to_bytes() +} + +#[test] +fn variable_noise_gradient_stays_a_fill_layer_and_keeps_its_gdfl() { + let _ = UnicodeString::new_nul("x"); + let mut f = testgen::small(Version::Psd, Compression::Raw); + let gdfl = default_noise_gdfl(); + f.layers_mut().push(LayerRecord { + name: b"noise fill".to_vec(), + blocks: vec![TaggedBlock::unicode_name("noise fill"), TaggedBlock::new(*b"GdFl", gdfl.clone())], + ..Default::default() + }); + for b in f.layers().last().unwrap().blocks.iter() { + b.check_structure().unwrap(); + } + let bytes = f.to_bytes().unwrap(); + let imp = import("in.psd", &bytes).unwrap(); + let l = imp.document.layers.iter().find(|l| l.name == "noise fill").unwrap(); + assert!(matches!(l.content, LayerContent::Fill(Fill::Gradient { .. })), "a variable noise GdFl opens as a gradient fill layer, not pixels"); + let out = export(&imp.document, "out.psd", &ExportOptions::default()).unwrap().bytes; + let back = PsdFile::from_bytes(&out).unwrap(); + let rec = back.layers().iter().find(|l| l.block(b"GdFl").is_some()).expect("the re-saved layer carries its GdFl"); + assert_eq!(rec.block(b"GdFl").unwrap().data, gdfl, "the raw GdFl survives the round trip byte for byte"); +} diff --git a/crates/io/tests/it/psd_recovery_generated.rs b/crates/io/tests/it/psd_recovery_generated.rs new file mode 100644 index 0000000000..7b9e4bb2e7 --- /dev/null +++ b/crates/io/tests/it/psd_recovery_generated.rs @@ -0,0 +1,106 @@ +//! PSD/PSB recovery regression tests using in-memory Rust-generated fixtures. +//! +//! No `.psd` file is checked into the repository and no Python generator runs. +//! Fixtures are created at the beginning of each test, so parallel test order +//! does not matter. See the ignored test for GUI-ready temporary files. + +#[path = "../support/recovery_psd.rs"] +mod recovery_psd; + +use photocraft_io::{ImportOptions, import, import_for_open}; +use photocraft_psd::PsdFile; + +/// Verifies a generated, two-layer PSD opens without source restrictions or damage prompts. +#[test] +fn valid_generated_psd_opens_normally() { + let bytes = recovery_psd::make_psd(false); + assert_eq!(bytes.len(), 74_986, "the fixture should retain the reference PSD layout"); + let result = import("valid-two-layers.psd", &bytes).expect("generated healthy PSD must import"); + assert!(!result.source_read_only); + assert!(!result.preview_only); + assert_eq!(result.document.layers.len(), 2); + let (_, damaged) = import_for_open("valid-two-layers.psd", &bytes, &ImportOptions::default()).unwrap(); + assert!(!damaged); +} + +/// Verifies a truncated green PackBits row leaves the PSD container and merged image +/// readable, while normal import reports a warning and interactive open reports damage. +#[test] +fn corrupt_generated_rle_channel_keeps_best_effort_import_and_reports_damage() { + let bytes = recovery_psd::make_psd(true); + assert_eq!(bytes.len(), 74_986, "the fixture should retain the reference PSD layout"); + let (parsed, _) = PsdFile::from_bytes_and_merged(&bytes, |_| false).expect("the PSD container must stay valid"); + let layer = parsed.layers().iter().find(|layer| layer.name() == "BROKEN orange RLE").expect("generated upper layer exists"); + + assert!(layer.decode_channel(0, parsed.header.depth, parsed.header.version).is_ok()); + assert!(layer.decode_channel(1, parsed.header.depth, parsed.header.version).is_err()); + assert!(layer.decode_channel(2, parsed.header.depth, parsed.header.version).is_ok()); + assert!(parsed.decode_merged().is_ok(), "the composite must remain recoverable"); + + let imported = import("corrupt-green-rle.psd", &bytes).expect("headless imports keep readable content"); + assert!(!imported.source_read_only); + assert!(!imported.preview_only); + assert_eq!(imported.document.layers.len(), 2); + assert!(imported.warnings.iter().any(|w| w.contains("could not be decoded"))); + + let (opened, damaged) = import_for_open("corrupt-green-rle.psd", &bytes, &ImportOptions::default()).unwrap(); + assert!(damaged, "interactive opening must ask whether to continue"); + assert!(!opened.source_read_only, "the flag does not introduce a save restriction"); + assert_eq!(opened.warnings, imported.warnings); + assert_eq!(opened.document.layers.len(), imported.document.layers.len()); +} + +/// Verifies a truncated merged preview triggers confirmation without losing intact layers. +#[test] +fn damaged_composite_keeps_layers_and_requests_interactive_confirmation() { + let mut file = PsdFile::from_bytes(&recovery_psd::make_psd(false)).unwrap(); + file.image_data.data.truncate(file.image_data.data.len() / 2); + let bytes = file.to_bytes().unwrap(); + let imported = import("damaged-preview.psd", &bytes).expect("intact layers are still usable"); + assert_eq!(imported.document.layers.len(), 2); + assert!(!imported.source_read_only); + let (opened, damaged) = import_for_open("damaged-preview.psd", &bytes, &ImportOptions::default()).unwrap(); + assert!(damaged); + assert_eq!(opened.warnings, imported.warnings); + assert!(opened.warnings.iter().any(|w| w.contains("merged image"))); +} + +/// Verifies mismatched resolution metadata produces a warning but no damage prompt. +#[test] +fn ordinary_compatibility_warning_does_not_mean_damaged() { + use photocraft_psd::{ImageResource, ResolutionInfo, resources::ids}; + let mut file = PsdFile::from_bytes(&recovery_psd::make_psd(false)).unwrap(); + let resolution = ResolutionInfo { v_res_fixed: ResolutionInfo::from_dpi(150.0).v_res_fixed, ..ResolutionInfo::from_dpi(300.0) }; + file.resources.push(ImageResource::new(ids::RESOLUTION_INFO, resolution.to_bytes())); + let (opened, damaged) = import_for_open("resolution.psd", &file.to_bytes().unwrap(), &ImportOptions::default()).unwrap(); + assert!(!damaged); + assert!(opened.warnings.iter().any(|w| w.contains("vertical resolution"))); +} + +/// Verifies a structurally unparseable PSD still fails instead of offering recovery. +#[test] +fn fatal_parse_errors_are_not_turned_into_confirmation_requests() { + let bytes = b"8BPS\0"; + assert!(import("truncated.psd", bytes).is_err()); + assert!(import_for_open("truncated.psd", bytes, &ImportOptions::default()).is_err()); +} + +/// Generate the same two PSDs on demand for manual testing in the GUI. +/// +/// Run with: +/// cargo test -p photocraft-io --test it \ +/// psd_recovery_generated::write_psds_for_manual_gui_test -- --ignored --nocapture +/// +/// The files go to the system's temp folder, not into the repository. +#[test] +#[ignore = "writes temporary PSD fixtures only when manual GUI testing is requested"] +fn write_psds_for_manual_gui_test() { + let output = std::env::temp_dir().join(format!("photocraft-psd-recovery-{}", std::process::id())); + std::fs::create_dir_all(&output).expect("create temporary fixture directory"); + + for (name, corrupt) in [("valid-two-layers.psd", false), ("corrupt-green-rle.psd", true)] { + let path = output.join(name); + std::fs::write(&path, recovery_psd::make_psd(corrupt)).expect("write generated temporary PSD"); + println!("{}", path.display()); + } +} diff --git a/crates/io/tests/psd_roundtrip_bytes.rs b/crates/io/tests/it/psd_roundtrip_bytes.rs similarity index 100% rename from crates/io/tests/psd_roundtrip_bytes.rs rename to crates/io/tests/it/psd_roundtrip_bytes.rs diff --git a/crates/io/tests/psd_structure.rs b/crates/io/tests/it/psd_structure.rs similarity index 62% rename from crates/io/tests/psd_structure.rs rename to crates/io/tests/it/psd_structure.rs index 5627dca3f4..e263f83a6b 100644 --- a/crates/io/tests/psd_structure.rs +++ b/crates/io/tests/it/psd_structure.rs @@ -177,6 +177,278 @@ fn fallback_modes_import_flattened() { } } +#[test] +fn palette_and_duotone_modes_are_written_in_their_own_mode() { + // Indexed Color and Bitmap are written flat in their own mode (palette / 1 bit); a Duotone + // keeps the ink data it was imported with over its grey plate. Same image, same mode data. + for mode in [ColorMode::Indexed, ColorMode::Bitmap, ColorMode::Duotone] { + let depth = testgen::mode_depths(mode)[0]; + let f = testgen::merged_only(Version::Psd, mode, depth, Compression::Rle, 9, 5); + let (d, _) = psd_to_document(&f); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + assert!(out.warnings.iter().all(|w| !w.contains("written as")), "{mode:?}: {:?}", out.warnings); + let back = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!((back.header.color_mode, back.header.depth, back.header.channels), (mode, f.header.depth, 1), "{mode:?}"); + assert_eq!(back.composite_rgba8().unwrap(), f.composite_rgba8().unwrap(), "{mode:?}"); + if mode != ColorMode::Bitmap { + assert_eq!(back.color_mode_data, f.color_mode_data, "{mode:?}"); + } + } +} + +#[test] +fn a_duotone_made_in_photocraft_is_written_with_its_inks() { + use photocraft_doc::{Duotone, DuotoneInk}; + let mut d = photocraft_doc::Document::with_background( + "d", + photocraft_doc::Size::new(4, 2), + photocraft_color::ColorMode::Grayscale, + photocraft_color::SampleType::U8, + photocraft_color::Color::rgba(0.5, 0.5, 0.5, 1.0), + ); + d.mode = photocraft_color::ColorMode::Duotone; + d.duotone = Some(Duotone { inks: vec![DuotoneInk::new("Black", [0.0; 3]), DuotoneInk::new("Orange", [1.0, 0.5, 0.0])], psd_raw: None }); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + assert!(out.warnings.iter().all(|w| !w.contains("written as")), "{:?}", out.warnings); + let f = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!((f.header.color_mode, f.color_mode_data.len()), (ColorMode::Duotone, 524)); + let (back, w) = psd_to_document(&f); + assert!(w.iter().all(|w| !w.contains("duotone")), "{w:?}"); + let inks = &back.duotone.as_ref().unwrap().inks; + assert_eq!(inks.iter().map(|i| i.name.as_str()).collect::>(), ["Black", "Orange"]); + assert!((inks[1].color[1] - 0.5).abs() < 1e-4, "{:?}", inks[1].color); + // Re-exported unedited, the block is the one read. + let again = PsdFile::from_bytes(&export(&back, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(again.color_mode_data, f.color_mode_data); +} + +fn duotone_doc(sample: photocraft_color::SampleType) -> photocraft_doc::Document { + use photocraft_doc::{Duotone, DuotoneInk}; + let mut d = photocraft_doc::Document::with_background( + "d", + photocraft_doc::Size::new(4, 2), + photocraft_color::ColorMode::Grayscale, + sample, + photocraft_color::Color::rgba(0.5, 0.5, 0.5, 1.0), + ); + d.mode = photocraft_color::ColorMode::Duotone; + d.duotone = Some(Duotone { inks: vec![DuotoneInk::new("Black", [0.0; 3]), DuotoneInk::new("Orange", [1.0, 0.5, 0.0])], psd_raw: None }); + d +} + +#[test] +fn a_16_bit_duotone_is_written_as_grayscale() { + // Photoshop has only 8-bit Duotone and refuses to open a 16-bit Duotone PSD. + let out = export(&duotone_doc(photocraft_color::SampleType::U16), "x.psd", &ExportOptions::default()).unwrap(); + let f = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!((f.header.color_mode, f.header.depth), (ColorMode::Grayscale, 16)); + assert!(out.warnings.iter().any(|w| w == "Duotone document written as Grayscale"), "{:?}", out.warnings); +} + +#[test] +fn a_layered_duotone_keeps_its_image_without_maximize_compatibility() { + // A Duotone opens flattened from its merged image (its layer records are not read), so that + // image is written in full even when Maximize Compatibility is off, never the placeholder. + let mut d = duotone_doc(photocraft_color::SampleType::U8); + let mut top = photocraft_doc::Layer::new("Dark", LayerContent::Raster(photocraft_raster::Surface::new(d.pixel_format()))); + if let LayerContent::Raster(s) = &mut top.content { + s.fill_rect(photocraft_geom::Rect::new(0, 0, 2, 2), &photocraft_raster::from_rgba(&d.pixel_format(), [0.0, 0.0, 0.0, 1.0])); + } + d.layers.push(top); + let opts = ExportOptions { psd_maximize_compatibility: false, ..Default::default() }; + let f = PsdFile::from_bytes(&export(&d, "x.psd", &opts).unwrap().bytes).unwrap(); + assert_eq!(f.header.color_mode, ColorMode::Duotone); + assert!(!f.layers().is_empty(), "the layers are still written"); + // The grey plate: the left half black (the top layer), the right half the 50% grey background. + let plate: Vec = f.decode_merged().unwrap(); + assert_eq!(plate.len(), 8); + assert!(plate[..2].iter().chain(&plate[4..6]).all(|&v| v == 0), "{plate:?}"); + assert!(plate[2..4].iter().chain(&plate[6..]).all(|&v| (127..=128).contains(&v)), "{plate:?}"); + let (back, _) = psd_to_document(&f); + let maxed = PsdFile::from_bytes(&export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(f.decode_merged().unwrap(), maxed.decode_merged().unwrap()); + assert_eq!(back.duotone.as_ref().map(|x| x.inks.len()), Some(2)); +} + +#[test] +fn a_colour_book_ink_shows_its_lab_equivalent_or_the_grey_plate() { + let out = export(&duotone_doc(photocraft_color::SampleType::U8), "x.psd", &ExportOptions::default()).unwrap(); + let mut f = PsdFile::from_bytes(&out.bytes).unwrap(); + // The second ink becomes PANTONE 327 CVC, stored by its book key as Photoshop does. + f.color_mode_data[14..24].copy_from_slice(&[0, 3, 0x20, 0x20, 0x33, 0x32, 0x37, 0x43, 0, 0]); + // Without resource 1066 its colour is unknown: the grey plate, not a black ink. + let (d, w) = psd_to_document(&f); + let inks = &d.duotone.as_ref().unwrap().inks; + assert_eq!((inks.len(), inks[0].color), (1, [0.0; 3]), "{inks:?}"); + assert!(w.iter().any(|w| w.contains("shown as grayscale")), "{w:?}"); + // With Photoshop's Lab equivalents (black, then the PANTONE teal) it shows in colour. + let lab = vec![0, 1, 0, 2, 0, 7, 0x03, 0xB9, 0x01, 0xF2, 0x01, 0xA9, 0, 0, 0, 7, 0x11, 0xC5, 0xE6, 0x38, 0xFE, 0x0C, 0, 0]; + f.resources.push(photocraft_psd::ImageResource::new(1066, lab)); + let (d, w) = psd_to_document(&f); + assert!(w.iter().all(|w| !w.contains("duotone")), "{w:?}"); + let duo = d.duotone.as_ref().unwrap(); + assert_eq!(duo.inks.len(), 2); + let [r, g, b] = duo.inks[1].color; + assert!(r < 0.1 && g > 0.4 && b > 0.35, "{:?}", duo.inks[1].color); + assert_eq!(duo.psd_raw.as_deref(), Some(f.color_mode_data.as_slice()), "the block is kept raw"); +} + +#[test] +fn a_layered_duotone_tiff_says_its_inks_are_dropped() { + // TIFF has no Duotone: a layered one is written in its grey plate, and says so. + let mut d = duotone_doc(photocraft_color::SampleType::U8); + let adj = d.duotone.as_ref().unwrap().display_adjustment(); + d.layers.push(photocraft_doc::Layer::new("Map", LayerContent::Adjustment(adj))); + let out = export(&d, "x.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap(); + assert!(out.warnings.iter().any(|w| w == "Duotone document written as Grayscale"), "{:?}", out.warnings); +} + +#[test] +fn indexed_psd_keeps_the_table_length_and_transparent_entry() { + use photocraft_doc::ColorTable; + let mut d = photocraft_doc::Document::with_background( + "i", + photocraft_doc::Size::new(4, 2), + photocraft_color::ColorMode::Rgb, + photocraft_color::SampleType::U8, + photocraft_color::Color::rgba(1.0, 0.0, 0.0, 1.0), + ); + d.mode = photocraft_color::ColorMode::Indexed; + d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0], [0, 0, 255]], transparent: Some(2) }); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + let f = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!(f.header.color_mode, ColorMode::Indexed); + assert_eq!(f.resource(1046).map(|r| r.data.clone()), Some(vec![0, 3])); + assert_eq!(f.resource(1047).map(|r| r.data.clone()), Some(vec![0, 2])); + assert_eq!(f.decode_merged().unwrap(), vec![1; 8], "every pixel is entry 1 (red)"); + let (back, _) = psd_to_document(&f); + assert_eq!(back.color_table, d.color_table); +} + +/// A 4x2 document filled with `fill` in Indexed Color mode with `colors` and `transparent`. +fn indexed_doc(fill: photocraft_color::Color, colors: Vec<[u8; 3]>, transparent: Option) -> photocraft_doc::Document { + let mut d = photocraft_doc::Document::with_background( + "i", + photocraft_doc::Size::new(4, 2), + photocraft_color::ColorMode::Rgb, + photocraft_color::SampleType::U8, + fill, + ); + d.mode = photocraft_color::ColorMode::Indexed; + d.color_table = Some(photocraft_doc::ColorTable { colors, transparent }); + d +} + +#[test] +fn indexed_psd_never_writes_an_opaque_pixel_as_the_transparent_entry() { + // The transparent entry duplicates opaque black (common in PNG-8 files) and comes first. + let d = indexed_doc(photocraft_color::Color::rgba(0.0, 0.0, 0.0, 1.0), vec![[0, 0, 0], [0, 0, 0], [255, 255, 255]], Some(0)); + let f = PsdFile::from_bytes(&export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(f.decode_merged().unwrap(), vec![1; 8], "opaque black is entry 1, not the transparent entry 0"); +} + +#[test] +fn indexed_psd_transparency_survives_a_round_trip() { + let d = indexed_doc(photocraft_color::Color::rgba(1.0, 0.0, 0.0, 0.0), vec![[255, 0, 0], [255, 255, 255]], Some(1)); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + let f = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!(f.decode_merged().unwrap(), vec![1; 8]); + let back = import("x.psd", &out.bytes).unwrap().document; + let s = back.layers[0].surface().unwrap(); + let px = s.read_region(back.bounds()); + assert!(px.chunks_exact(s.channels()).all(|p| p.last() == Some(&0.0)), "transparent pixels came back opaque"); + assert!(!back.layers[0].locks.transparency); +} + +#[test] +fn indexed_psd_without_a_transparent_entry_flattens_over_white() { + let d = indexed_doc(photocraft_color::Color::rgba(0.0, 0.0, 0.0, 0.0), vec![[0, 0, 0], [255, 255, 255]], None); + let f = PsdFile::from_bytes(&export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(f.decode_merged().unwrap(), vec![1; 8], "empty areas are white, as Photoshop flattens them"); +} + +#[test] +fn indexed_psd_keeps_the_icc_profile() { + // Bytes 16..20 of a profile name its colour space; only an RGB one describes the table. + let profile = |space: &[u8; 4]| { + let mut p = vec![7u8; 132]; + p[16..20].copy_from_slice(space); + p + }; + for (space, kept) in [(b"RGB ", true), (b"GRAY", false)] { + let mut d = indexed_doc(photocraft_color::Color::rgba(1.0, 0.0, 0.0, 1.0), vec![[255, 0, 0]], None); + d.icc_profile = Some(std::sync::Arc::new(profile(space))); + let f = PsdFile::from_bytes(&export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert_eq!(f.header.color_mode, ColorMode::Indexed); + assert_eq!(f.resource(1039).map(|r| r.data.clone()), kept.then(|| profile(space))); + } +} + +#[test] +fn indexed_document_whose_only_entry_is_transparent_is_not_written_as_indexed() { + // Every opaque pixel would have to take the transparent entry and vanish: written as RGB. + let d = indexed_doc(photocraft_color::Color::rgba(1.0, 0.0, 0.0, 1.0), vec![[255, 0, 0]], Some(0)); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + let f = PsdFile::from_bytes(&out.bytes).unwrap(); + assert_eq!(f.header.color_mode, ColorMode::Rgb, "{:?}", out.warnings); + assert_eq!(&f.composite_rgba8().unwrap().data[..4], &[255, 0, 0, 255]); +} + +#[test] +fn indexed_save_warns_whenever_flattening_loses_a_layer() { + // One plain visible pixel layer is the merged image: no warning. A single hidden, masked, + // half-opaque or type layer, or a second layer, is lost in the flat file, so the save says so. + let flat = |d: &photocraft_doc::Document| { + let out = export(d, "x.psd", &ExportOptions::default()).unwrap(); + assert_eq!(PsdFile::from_bytes(&out.bytes).unwrap().header.color_mode, ColorMode::Indexed); + out.warnings.iter().any(|w| w.contains("written flattened")) + }; + let plain = indexed_doc(photocraft_color::Color::rgba(1.0, 1.0, 1.0, 1.0), vec![[255, 255, 255], [0, 0, 0]], None); + assert!(!flat(&plain), "a single plain layer flattens losslessly"); + let changes: [fn(&mut photocraft_doc::Layer); 4] = [ + |l| l.visible = false, + |l| l.opacity = 0.5, + |l| l.mask = Some(photocraft_doc::LayerMask::hide_all()), + |l| l.content = LayerContent::Text(photocraft_doc::TextLayer::default()), + ]; + for (i, change) in changes.iter().enumerate() { + let mut d = plain.clone(); + change(&mut d.layers[0]); + assert!(flat(&d), "change {i} is lost when flattened, so the save warns"); + } + let mut two = plain.clone(); + two.layers.push(two.layers[0].clone()); + assert!(flat(&two)); +} + +#[test] +fn indexed_and_bitmap_documents_with_channels_keep_them() { + // PhotoCraft does not read extra channels of a palette-mode PSD back, so these documents keep + // the RGB / Grayscale file that holds their alpha channels and Quick Mask. + for mode in [photocraft_color::ColorMode::Indexed, photocraft_color::ColorMode::Bitmap] { + let mut d = indexed_doc(photocraft_color::Color::rgba(1.0, 1.0, 1.0, 1.0), vec![[255, 255, 255]], None); + d.mode = mode; + let alpha = || photocraft_doc::AlphaChannel::new("Alpha 1", photocraft_raster::Surface::new(photocraft_color::PixelFormat::GRAY8)); + d.channels.push(alpha()); + d.quick_mask = Some(alpha()); + let out = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + let back = import("x.psd", &out.bytes).unwrap().document; + assert_eq!((back.channels.len(), back.quick_mask.is_some()), (1, true), "{mode:?}: {:?}", out.warnings); + } +} + +#[test] +fn layerless_psd_never_keeps_psb_only_block_signatures() { + for mode in [photocraft_color::ColorMode::Indexed, photocraft_color::ColorMode::Bitmap, photocraft_color::ColorMode::Multichannel] { + let mut d = indexed_doc(photocraft_color::Color::rgba(1.0, 1.0, 1.0, 1.0), vec![[255, 255, 255]], None); + d.mode = mode; + d.metadata.psd_global_blocks.push((*b"8B64", *b"FMsk", std::sync::Arc::new(vec![0; 12]))); + let f = PsdFile::from_bytes(&export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + assert!(f.global_blocks.iter().any(|b| &b.key == b"FMsk"), "{mode:?}"); + assert!(f.global_blocks.iter().all(|b| &b.signature == b"8BIM"), "{mode:?}"); + } +} + #[test] fn multichannel_imports_ink_channels() { for depth in testgen::mode_depths(ColorMode::Multichannel) { @@ -361,9 +633,7 @@ fn adjustment_and_fill_layers_from_psd() { use photocraft_doc::{Adjustment, Fill}; assert!(matches!(d.layers[n - 4].content, LayerContent::Adjustment(Adjustment::Invert))); assert!(matches!(d.layers[n - 3].content, LayerContent::Adjustment(Adjustment::Posterize { levels: 4 }))); - assert!( - matches!(&d.layers[n - 2].content, LayerContent::Adjustment(Adjustment::Unsupported { psd_key, raw }) if psd_key == "selc" && raw == &vec![1, 2, 3]) - ); + assert!(matches!(&d.layers[n - 2].content, LayerContent::Adjustment(Adjustment::Unsupported { psd_key, raw }) if psd_key == "selc" && **raw == [1, 2, 3])); assert!(matches!(&d.layers[n - 1].content, LayerContent::Fill(Fill::Solid(c)) if c.c[0] == 1.0)); } @@ -388,7 +658,7 @@ fn fill_layer_keeps_photoshop_pixels() { VersionedDescriptor::new(Descriptor::new("null").with("Clr ", Value::Descriptor(c))).to_bytes() }; // Photoshop-rendered pixels deliberately differ from the parametric fill. - let plane = |v: u8| ChannelData { id: 0, compression: Some(Compression::Raw), data: vec![v; 4] }; + let plane = |v: u8| ChannelData::new(0, Some(Compression::Raw), vec![v; 4]); let mut chans = vec![plane(255), plane(10), plane(20), plane(30)]; for (i, c) in chans.iter_mut().enumerate() { c.id = i as i16 - 1; @@ -422,9 +692,11 @@ fn text_and_smart_detected() { use photocraft_psd::{LayerRecord, TaggedBlock}; let mut f = testgen::small(Version::Psd, Compression::Raw); for (name, key) in [("t", b"TySh"), ("s", b"PlLd"), ("sh", b"vscg")] { + // Type identification needs readable type data, not an eight-byte malformed stub. + let data = if key == b"TySh" { photocraft_text::psd::build_tysh(&photocraft_doc::TextLayer::default(), 72.0, None) } else { vec![0; 8] }; f.layers_mut().push(LayerRecord { name: name.as_bytes().to_vec(), - blocks: vec![TaggedBlock::unicode_name(name), TaggedBlock::new(*key, vec![0; 8])], + blocks: vec![TaggedBlock::unicode_name(name), TaggedBlock::new(*key, data)], ..Default::default() }); } @@ -490,7 +762,7 @@ fn merged_composite_unmattes_photoshop_white() { let mut f = testgen::merged_only(Version::Psd, ColorMode::Rgb, 8, Compression::Raw, 1, 1); f.header.channels = 4; // straight red at alpha 0.5, matted: r = 1, g = b = 0.5 - f.image_data = photocraft_psd::ImageData { compression: Compression::Raw, data: vec![255, 128, 128, 128] }; + f.image_data = photocraft_psd::ImageData::new(Compression::Raw, vec![255, 128, 128, 128]); let m = merged_composite(&f).unwrap(); assert!((m[0][0] - 1.0).abs() < 0.01 && m[0][1] < 0.01 && m[0][2] < 0.01, "{:?}", m[0]); let (d, _) = psd_to_document(&f); @@ -500,8 +772,7 @@ fn merged_composite_unmattes_photoshop_white() { // HDR and negative straight samples must survive the actual F32 PSD import boundary. let mut hdr = testgen::merged_only(Version::Psd, ColorMode::Rgb, 32, Compression::Raw, 1, 1); hdr.header.channels = 4; - hdr.image_data = - photocraft_psd::ImageData { compression: Compression::Raw, data: [1.5_f32, 0.25, 0.5, 0.5].into_iter().flat_map(f32::to_be_bytes).collect() }; + hdr.image_data = photocraft_psd::ImageData::new(Compression::Raw, [1.5_f32, 0.25, 0.5, 0.5].into_iter().flat_map(f32::to_be_bytes).collect()); assert_eq!(merged_composite(&hdr).unwrap(), vec![[2.0, -0.5, 0.0, 0.5]]); let mut deep = testgen::merged_only(Version::Psd, ColorMode::Rgb, 16, Compression::Raw, 1, 1); deep.header.channels = 4; @@ -521,7 +792,7 @@ fn vector_rendered_mask_not_doubled_on_shapes() { f.layers_mut().push(LayerRecord { name: b"shape".to_vec(), mask: photocraft_psd::MaskData::Mask(mask), - channels: vec![photocraft_psd::ChannelData { id: -2, compression: Some(Compression::Raw), data: vec![] }], + channels: vec![photocraft_psd::ChannelData::new(-2, Some(Compression::Raw), vec![])], blocks: vec![TaggedBlock::unicode_name("shape"), TaggedBlock::new(*b"SoCo", vec![]), TaggedBlock::new(*b"vmsk", vec![0; 8])], ..Default::default() }); @@ -533,7 +804,7 @@ fn vector_rendered_mask_not_doubled_on_shapes() { let record = f.layers_mut().last_mut().unwrap(); let photocraft_psd::MaskData::Mask(mask) = &mut record.mask else { panic!("fixture has a mask") }; mask.real = Some(photocraft_psd::RealMask { flags: 0, background: 255, rect: photocraft_psd::Rect { top: 0, left: 0, bottom: 1, right: 1 } }); - record.channels.push(photocraft_psd::ChannelData { id: -3, compression: Some(Compression::Raw), data: vec![64] }); + record.channels.push(photocraft_psd::ChannelData::new(-3, Some(Compression::Raw), vec![64])); let (imported, _) = psd_to_document(&f); assert_eq!(imported.layers.last().unwrap().content.kind_name(), "Shape"); let mask = imported.layers.last().unwrap().mask.as_ref().expect("selected real mask must survive synthetic cleanup"); diff --git a/crates/io/tests/psd_style_sheets.rs b/crates/io/tests/it/psd_style_sheets.rs similarity index 100% rename from crates/io/tests/psd_style_sheets.rs rename to crates/io/tests/it/psd_style_sheets.rs diff --git a/crates/io/tests/psd_thumbnail.rs b/crates/io/tests/it/psd_thumbnail.rs similarity index 100% rename from crates/io/tests/psd_thumbnail.rs rename to crates/io/tests/it/psd_thumbnail.rs diff --git a/crates/io/tests/psd_type_bounds.rs b/crates/io/tests/it/psd_type_bounds.rs similarity index 95% rename from crates/io/tests/psd_type_bounds.rs rename to crates/io/tests/it/psd_type_bounds.rs index 8fabe8be24..b87d4f4a3a 100644 --- a/crates/io/tests/psd_type_bounds.rs +++ b/crates/io/tests/it/psd_type_bounds.rs @@ -10,7 +10,7 @@ //! //! Fonts: the bundled Inter (deterministic), so our render is the "Photoshop" render here. -mod common; +use crate::common; use photocraft_color::{ColorMode, SampleType}; use photocraft_doc::text::TextShape; @@ -367,7 +367,21 @@ fn hostile_type_sizes_without_pixels_import_without_a_huge_render() { { let runs = vec![Run { len: 1, size, leading: None }]; let data = tysh("M", &runs, None, transform, [10.0, 10.0, 20.0, 20.0]); - let mut t = photocraft_text::psd::text_layer_from_tysh(&data, 72.0).unwrap(); + let Some(mut t) = photocraft_text::psd::text_layer_from_tysh(&data, 72.0) else { + // Unsafe affine data is now rejected at the boundary (#4758). Exercise the + // actual file import too: with no saved pixels it must remain empty and safe. + let mut file = photocraft_psd::testgen::small(photocraft_psd::Version::Psd, photocraft_psd::Compression::Raw); + file.layers_mut().push(photocraft_psd::LayerRecord { + name: b"hostile".to_vec(), + blocks: vec![photocraft_psd::TaggedBlock::new(*b"TySh", data)], + ..Default::default() + }); + let back = photocraft_io::import("t.psd", &file.to_bytes().unwrap()).unwrap(); + assert!(back.warnings.iter().any(|w| w.contains("unsafe type geometry"))); + let LayerContent::Raster(pixels) = &back.document.layers.last().unwrap().content else { panic!("unsafe type must stay pixels") }; + assert!(pixels.content_bounds().is_empty()); + continue; + }; if big { // Precondition: the renderer's own guard (256 Mpx) would have drawn this one. let mut engine = photocraft_text::TextEngine::new(); diff --git a/crates/io/tests/psd_type_photoshop_bytes.rs b/crates/io/tests/it/psd_type_photoshop_bytes.rs similarity index 100% rename from crates/io/tests/psd_type_photoshop_bytes.rs rename to crates/io/tests/it/psd_type_photoshop_bytes.rs diff --git a/crates/io/tests/it/psd_unknown_adjustment.rs b/crates/io/tests/it/psd_unknown_adjustment.rs new file mode 100644 index 0000000000..58bc909c57 --- /dev/null +++ b/crates/io/tests/it/psd_unknown_adjustment.rs @@ -0,0 +1,106 @@ +//! An adjustment layer whose key PhotoCraft doesn't know (an older or newer adjustment type) +//! opens as an unsupported adjustment with a warning and saves its block back unchanged, instead +//! of opening silently as an empty pixel layer (#6575). Pixel layers that merely carry an unknown +//! block stay pixel layers. + +use photocraft_doc::{Adjustment, Document, LayerContent}; +use photocraft_io::*; +use photocraft_psd::testgen; +use photocraft_psd::{ChannelData, Compression, LayerFlags, LayerRecord, PsdFile, Rect, TaggedBlock, Version}; + +/// A layer record as Photoshop writes an adjustment layer: no pixels, "pixel data irrelevant" +/// set, the adjustment block first, then the usual metadata. +fn adjustment_record(name: &str, blocks: Vec) -> LayerRecord { + let mut flags = LayerFlags::default(); + flags.set_pixel_data_irrelevant(true); + let mut all = blocks; + all.extend([TaggedBlock::unicode_name(name), TaggedBlock::layer_id(77)]); + LayerRecord { name: name.as_bytes().to_vec(), flags, blocks: all, ..Default::default() } +} + +fn open(f: &PsdFile) -> (Document, Vec) { + let bytes = f.to_bytes().expect("write psd"); + let imp = import("x.psd", &bytes).expect("import"); + (imp.document, imp.warnings) +} + +fn save(d: &Document) -> PsdFile { + let r = export(d, "x.psd", &ExportOptions::default()).expect("export"); + PsdFile::from_bytes(&r.bytes).expect("reparse") +} + +fn content<'a>(d: &'a Document, name: &str) -> &'a LayerContent { + &d.layers.iter().find(|l| l.name == name).unwrap_or_else(|| panic!("layer {name}")).content +} + +fn record<'a>(f: &'a PsdFile, name: &str) -> &'a LayerRecord { + f.layers().iter().find(|r| r.name() == name).unwrap_or_else(|| panic!("record {name}")) +} + +#[test] +fn unknown_adjustment_key_opens_as_unsupported_and_saves_verbatim() { + // `hue ` is the Photoshop 4 Hue/Saturation block (in the spec, never mapped); `zNew` stands + // for an adjustment type newer than PhotoCraft. + for key in [*b"hue ", *b"zNew"] { + let data = vec![0, 2, 1, 2, 3, 4, 5, 6]; + let mut f = testgen::small(Version::Psd, Compression::Raw); + f.layers_mut().push(adjustment_record("Mystery", vec![TaggedBlock::new(key, data.clone())])); + let (d, warnings) = open(&f); + let k = String::from_utf8_lossy(&key).into_owned(); + assert!( + matches!(content(&d, "Mystery"), LayerContent::Adjustment(Adjustment::Unsupported { psd_key, raw }) if *psd_key == k && **raw == data[..]), + "{k}: {:?}", + content(&d, "Mystery") + ); + let w: Vec<&String> = warnings.iter().filter(|w| w.contains("Mystery")).collect(); + assert_eq!(w.len(), 1, "{k}: {warnings:?}"); + assert!(w[0].contains(&format!("\"{k}\"")) && w[0].contains("no effect"), "{k}: {}", w[0]); + + // Saved back: the block exactly once, byte-identical, still an adjustment record. + let g = save(&d); + let r = record(&g, "Mystery"); + let same: Vec<&TaggedBlock> = r.blocks.iter().filter(|b| b.key == key).collect(); + assert_eq!(same.len(), 1, "{k}: {:?}", r.blocks.iter().map(TaggedBlock::key_str).collect::>()); + assert_eq!(same[0].data, data, "{k}"); + assert!(r.flags.bit4_useful() && r.flags.pixel_data_irrelevant(), "{k}"); + assert!(r.rect.is_empty(), "{k}"); + // And it reopens the same way. + let (d2, _) = open(&g); + assert_eq!(content(&d2, "Mystery"), content(&d, "Mystery"), "{k}"); + } +} + +#[test] +fn pixel_layers_with_unknown_blocks_stay_pixel_layers() { + let extra = || TaggedBlock::new(*b"zNew", vec![9, 8, 7, 6]); + let mut f = testgen::small(Version::Psd, Compression::Raw); + // A normal pixel layer (pixels, flag clear) carrying an unknown block. + f.layers_mut()[0].blocks.push(extra()); + // Flagged but with pixels: never dropped for an adjustment. + let plane = |id: i16| ChannelData::new(id, Some(Compression::Raw), vec![200; 4]); + let mut painted = adjustment_record("painted", vec![extra()]); + painted.rect = Rect::from_xywh(0, 0, 2, 2); + painted.channels = (-1..3).map(plane).collect(); + f.layers_mut().push(painted); + // No pixels and an unknown block, but not marked "pixel data irrelevant": an empty pixel layer. + let mut unflagged = adjustment_record("unflagged", vec![extra()]); + unflagged.flags = LayerFlags::default(); + f.layers_mut().push(unflagged); + // Two unknown blocks: which one is the adjustment is a guess, so nothing is guessed. + f.layers_mut().push(adjustment_record("ambiguous", vec![extra(), TaggedBlock::new(*b"zOth", vec![1, 1, 1, 1])])); + let (d, warnings) = open(&f); + for name in ["a", "painted", "unflagged", "ambiguous"] { + assert!(matches!(content(&d, name), LayerContent::Raster(_)), "{name}: {:?}", content(&d, name)); + assert!(!warnings.iter().any(|w| w.contains(&format!("\"{name}\""))), "{name}: {warnings:?}"); + } + let LayerContent::Raster(s) = content(&d, "painted") else { unreachable!() }; + assert!(!s.content_bounds().is_empty(), "painted keeps its pixels"); + // The unknown block is still kept verbatim on save, once. + let g = save(&d); + for name in ["a", "painted", "unflagged", "ambiguous"] { + let r = record(&g, name); + let kept: Vec<&TaggedBlock> = r.blocks.iter().filter(|b| &b.key == b"zNew").collect(); + assert_eq!(kept.len(), 1, "{name}"); + assert_eq!(kept[0].data, vec![9, 8, 7, 6], "{name}"); + } +} diff --git a/crates/io/tests/psd_vertical_type.rs b/crates/io/tests/it/psd_vertical_type.rs similarity index 100% rename from crates/io/tests/psd_vertical_type.rs rename to crates/io/tests/it/psd_vertical_type.rs diff --git a/crates/io/tests/raw.rs b/crates/io/tests/it/raw.rs similarity index 58% rename from crates/io/tests/raw.rs rename to crates/io/tests/it/raw.rs index d9e31a87c1..52250c24d7 100644 --- a/crates/io/tests/raw.rs +++ b/crates/io/tests/it/raw.rs @@ -22,6 +22,28 @@ fn dng_opens_as_16_bit_prophoto() { assert!(r.warnings.iter().any(|w| w.contains("DNG") && w.contains("ProPhoto")), "{:?}", r.warnings); } +/// #8125: a lossy DNG's floating-point LinearRaw data, JPEG XL-compressed, is developed (not +/// replaced by a preview). The test codestream is an integer one the reader takes as floats. +#[test] +fn floating_point_jpeg_xl_dng_is_developed() { + use photocraft_raw::testgen::DngStorage; + use zune_core::{bit_depth::BitDepth, colorspace::ColorSpace, options::EncoderOptions}; + let (w, h) = (24, 16); + let data: Vec = scene(w, h).iter().flat_map(|p| p.map(|v| (v * 20000.0) as u16)).collect(); + let bytes: Vec = data.iter().flat_map(|v| v.to_ne_bytes()).collect(); + let mut segment = Vec::new(); + zune_jpegxl::JxlSimpleEncoder::new(&bytes, EncoderOptions::new(w, h, ColorSpace::RGB, BitDepth::Sixteen)).encode(&mut segment).unwrap(); + let mut spec = DngSpec::cfa(w, h, data); + spec.samples = 3; + spec.white = 0; + spec.storage = DngStorage::JxlStrips { rows: h }; + spec.jxl_segments = vec![segment]; + spec.raw_extra = vec![(339, Val::Short(vec![3; 3]))]; + let r = import("lossy.dng", &spec.build()).unwrap(); + assert_eq!((r.document.size.width, r.document.size.height, r.document.depth), (24, 16, SampleType::U16)); + assert!(r.warnings.iter().all(|w| !w.contains("preview")), "{:?}", r.warnings); +} + /// #3497: a monochrome (one-sample LinearRaw) DNG, as VueScan writes film scans, opens as a /// 16-bit RGB document (neutral grey) instead of failing as unsupported. #[test] @@ -46,6 +68,118 @@ fn cr2_opens() { assert_eq!(r.document.depth, SampleType::U16); } +/// #6957: a developed raw keeps the raw's EXIF (camera, exposure), so File Info shows its Camera +/// Data. The raw's Orientation is left out: the developed image is already upright. +#[test] +fn a_developed_raw_keeps_its_exif() { + let (w, h) = (48, 16); + let data = mosaic(&scene(w, h), w, [0, 1, 1, 2], 256, 12000); + let spec = + Cr2Spec { width: w, height: h, data, precision: 14, components: 2, slices: vec![24, 24], borders: None, wb_rggb: None, orientation: 6, model_id: None }; + let r = import("IMG_0001.CR2", &spec.build()).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains(photocraft_io::raw::DEVELOPED_NOTE)), "developed, not a preview: {:?}", r.warnings); + let exif = r.document.metadata.exif.as_deref().expect("the raw's EXIF"); + let camera = photocraft_algo::exif::read(exif); + assert_eq!(camera.make.as_deref(), Some("Canon")); + assert_eq!(camera.model.as_deref(), Some("Canon EOS Synthetic")); + assert_eq!(camera.exposure_time, Some(0.01)); + assert_eq!(photocraft_codecs::exif_orientation(exif), 1, "no orientation to apply a second time"); + assert_eq!((r.document.size.width, r.document.size.height), (16, 48)); +} + +/// The tags of the Exif directory of an EXIF block (a little-endian TIFF stream). +fn exif_ifd_tags(b: &[u8]) -> Vec { + assert_eq!(&b[..2], b"II"); + let u16_at = |i: usize| u16::from_le_bytes([b[i], b[i + 1]]); + let u32_at = |i: usize| u32::from_le_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) as usize; + let entries = |at: usize| (0..usize::from(u16_at(at))).map(move |k| at + 2 + 12 * k); + let ifd0 = u32_at(4); + let exif = entries(ifd0).find(|&e| u16_at(e) == 34665).map(|e| u32_at(e + 8)).expect("an Exif directory"); + entries(exif).map(u16_at).collect() +} + +/// A Panasonic RW2 (its TIFF header has 0x55 for 42) with an Exif directory: the developed +/// document keeps it, without the Exif pixel dimensions (the sensor's, not the image's). +#[test] +fn a_developed_rw2_keeps_its_exif() { + let (w, h, bits) = (20usize, 12usize, 12u32); + let data: Vec = (0..w * h).map(|i| (i * 13 % 4000) as u16 + 200).collect(); + let mut t = TiffBuilder::default(); + let raw = t.blob(photocraft_raw::testgen::rw2_pack(&data, w, bits)); + let exif = t.ifd(vec![(33434, Val::Rational(vec![(1, 60)])), (40962, Val::Long(vec![w as u32])), (40963, Val::Long(vec![h as u32]))]); + let white = ((1u32 << bits) - 1) as u16; + let ifd0 = t.ifd(vec![ + (0x0002, Val::Short(vec![w as u16])), + (0x0003, Val::Short(vec![h as u16])), + (0x0004, Val::Short(vec![2])), + (0x0005, Val::Short(vec![2])), + (0x0006, Val::Short(vec![h as u16 - 2])), + (0x0007, Val::Short(vec![w as u16 - 2])), + (0x0009, Val::Short(vec![1])), + (0x000A, Val::Short(vec![bits as u16])), + (0x000E, Val::Short(vec![white])), + (0x000F, Val::Short(vec![white])), + (0x0010, Val::Short(vec![white])), + (0x002D, Val::Short(vec![5])), + (0x010F, Val::Ascii("Panasonic".into())), + (0x0110, Val::Ascii("DC-Synthetic".into())), + (0x0112, Val::Short(vec![1])), + (0x0118, Val::Blobs(vec![raw])), + (34665, Val::Ifds(vec![exif])), + ]); + t.chain = vec![ifd0]; + let mut bytes = t.build(); + bytes[2..4].copy_from_slice(b"U\0"); + let r = import("P1000001.RW2", &bytes).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains(photocraft_io::raw::DEVELOPED_NOTE)), "developed, not a preview: {:?}", r.warnings); + let exif = r.document.metadata.exif.as_deref().expect("the raw's EXIF"); + let camera = photocraft_algo::exif::read(exif); + assert_eq!((camera.make.as_deref(), camera.model.as_deref()), (Some("Panasonic"), Some("DC-Synthetic"))); + assert!(camera.exposure_time.is_some_and(|t| (t - 1.0 / 60.0).abs() < 1e-9)); + let tags = exif_ifd_tags(exif); + assert!(tags.contains(&33434), "{tags:?}"); + assert!(!tags.contains(&40962) && !tags.contains(&40963), "no Exif pixel dimensions: {tags:?}"); +} + +/// A raw whose Exif directory holds only the pixel dimensions, which a developed image drops: +/// IFD0's camera make and model are kept on their own rather than lost with the empty directory. +#[test] +fn a_developed_raw_keeps_its_camera_when_the_exif_directory_empties() { + let (w, h, bits) = (20usize, 12usize, 12u32); + let data: Vec = (0..w * h).map(|i| (i * 13 % 4000) as u16 + 200).collect(); + let mut t = TiffBuilder::default(); + let raw = t.blob(photocraft_raw::testgen::rw2_pack(&data, w, bits)); + let exif = t.ifd(vec![(40962, Val::Long(vec![w as u32])), (40963, Val::Long(vec![h as u32]))]); + let white = ((1u32 << bits) - 1) as u16; + let ifd0 = t.ifd(vec![ + (0x0002, Val::Short(vec![w as u16])), + (0x0003, Val::Short(vec![h as u16])), + (0x0004, Val::Short(vec![2])), + (0x0005, Val::Short(vec![2])), + (0x0006, Val::Short(vec![h as u16 - 2])), + (0x0007, Val::Short(vec![w as u16 - 2])), + (0x0009, Val::Short(vec![1])), + (0x000A, Val::Short(vec![bits as u16])), + (0x000E, Val::Short(vec![white])), + (0x000F, Val::Short(vec![white])), + (0x0010, Val::Short(vec![white])), + (0x002D, Val::Short(vec![5])), + (0x010F, Val::Ascii("Panasonic".into())), + (0x0110, Val::Ascii("DC-Synthetic".into())), + (0x0112, Val::Short(vec![1])), + (0x0118, Val::Blobs(vec![raw])), + (34665, Val::Ifds(vec![exif])), + ]); + t.chain = vec![ifd0]; + let mut bytes = t.build(); + bytes[2..4].copy_from_slice(b"U\0"); + let r = import("P1000001.RW2", &bytes).unwrap(); + let exif = r.document.metadata.exif.as_deref().expect("the raw's camera, without an Exif directory"); + let camera = photocraft_algo::exif::read(exif); + assert_eq!((camera.make.as_deref(), camera.model.as_deref()), (Some("Panasonic"), Some("DC-Synthetic"))); + assert_eq!(camera.exposure_time, None); +} + /// A NEF-like file with Nikon's (undocumented) compression and a full-size /// baseline JPEG preview in IFD0. fn nef_with_preview() -> Vec { @@ -82,6 +216,79 @@ fn unsupported_raw_falls_back_to_the_embedded_preview() { assert!(r.warnings.first().is_some_and(|w| w.contains("Nikon compressed NEF") && w.contains("embedded")), "{:?}", r.warnings); } +/// A DNG 1.7 laid out as converters write one with JPEG XL raw data: an 8×6 JPEG thumbnail in +/// IFD0, raw data this reader refuses (here floating-point LinearRaw), and a 40×30 preview +/// compressed with JPEG XL, stored as the raw is (IFD0 Orientation 6). +fn dng_with_jxl_preview() -> Vec { + use zune_core::{bit_depth::BitDepth, colorspace::ColorSpace, options::EncoderOptions}; + let thumb = Image::from_u8(8, 6, ChannelLayout::Rgb, vec![90; 8 * 6 * 3]).unwrap(); + let thumb = photocraft_codecs::encode(&thumb, Format::Jpeg, &EncodeOptions::default()).unwrap(); + let (w, h) = (40usize, 30usize); + let rgb: Vec = (0..w * h).flat_map(|i| [(i % w * 6) as u8, (i / w * 8) as u8, 120]).collect(); + let mut preview = Vec::new(); + zune_jpegxl::JxlSimpleEncoder::new(&rgb, EncoderOptions::new(w, h, ColorSpace::RGB, BitDepth::Eight)).encode(&mut preview).unwrap(); + let mut t = TiffBuilder::default(); + let raw_strip = t.blob(vec![0; 4 * 4 * 3 * 2]); + let raw = t.ifd(vec![ + (254, Val::Long(vec![0])), + (256, Val::Long(vec![4])), + (257, Val::Long(vec![4])), + (258, Val::Short(vec![16, 16, 16])), + (259, Val::Short(vec![1])), + (262, Val::Short(vec![34892])), + (273, Val::Blobs(vec![raw_strip])), + (277, Val::Short(vec![3])), + (278, Val::Long(vec![4])), + (279, Val::Long(vec![4 * 4 * 3 * 2])), + (339, Val::Short(vec![3, 3, 3])), + ]); + let preview_len = preview.len() as u32; + let preview_strip = t.blob(preview); + let preview = t.ifd(vec![ + (254, Val::Long(vec![1])), + (256, Val::Long(vec![w as u32])), + (257, Val::Long(vec![h as u32])), + (258, Val::Short(vec![8, 8, 8])), + (259, Val::Short(vec![52546])), + (262, Val::Short(vec![2])), + (273, Val::Blobs(vec![preview_strip])), + (277, Val::Short(vec![3])), + (278, Val::Long(vec![h as u32])), + (279, Val::Long(vec![preview_len])), + (50970, Val::Long(vec![2])), + ]); + let thumb_len = thumb.len() as u32; + let thumb_strip = t.blob(thumb); + let ifd0 = t.ifd(vec![ + (254, Val::Long(vec![1])), + (256, Val::Long(vec![8])), + (257, Val::Long(vec![6])), + (258, Val::Short(vec![8, 8, 8])), + (259, Val::Short(vec![7])), + (262, Val::Short(vec![6])), + (271, Val::Ascii("Synthetic".into())), + (273, Val::Blobs(vec![thumb_strip])), + (274, Val::Short(vec![6])), + (277, Val::Short(vec![3])), + (278, Val::Long(vec![6])), + (279, Val::Long(vec![thumb_len])), + (330, Val::Ifds(vec![raw, preview])), + (50706, Val::Byte(vec![1, 7, 0, 0])), + ]); + t.chain = vec![ifd0]; + t.build() +} + +/// #8125: a DNG whose raw data isn't supported opens its full JPEG XL preview, upright, not +/// the small JPEG thumbnail. +#[test] +fn unsupported_dng_falls_back_to_its_jpeg_xl_preview() { + let r = import("photo.dng", &dng_with_jxl_preview()).unwrap(); + assert_eq!((r.document.size.width, r.document.size.height), (30, 40), "the 40×30 preview, turned upright: {:?}", r.warnings); + let w = r.warnings.first().map_or("", String::as_str); + assert!(w.contains("floating-point") && w.contains("40x30 JPEG XL preview"), "{:?}", r.warnings); +} + #[test] fn unsupported_raw_without_preview_is_a_clear_error() { let mut cr3 = vec![0, 0, 0, 24]; diff --git a/crates/io/tests/resolution_axes.rs b/crates/io/tests/it/resolution_axes.rs similarity index 99% rename from crates/io/tests/resolution_axes.rs rename to crates/io/tests/it/resolution_axes.rs index f7f8cbf006..fb2bd209e2 100644 --- a/crates/io/tests/resolution_axes.rs +++ b/crates/io/tests/it/resolution_axes.rs @@ -1,7 +1,7 @@ //! A file whose horizontal and vertical resolutions differ opens at the horizontal one, and the //! import says the vertical one is not kept (#1018). Equal axes, the common case, open silently. -mod common; +use crate::common; use common::*; use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image, Metadata, SampleType as CSample}; diff --git a/crates/io/tests/roundtrip.rs b/crates/io/tests/it/roundtrip.rs similarity index 99% rename from crates/io/tests/roundtrip.rs rename to crates/io/tests/it/roundtrip.rs index 6502079f20..1031b5504a 100644 --- a/crates/io/tests/roundtrip.rs +++ b/crates/io/tests/it/roundtrip.rs @@ -1,6 +1,6 @@ //! Document → PSD → Document round trips. -mod common; +use crate::common; use common::*; use photocraft_color::{ColorMode, SampleType}; @@ -340,12 +340,14 @@ fn effects_roundtrip_typed_and_raw() { size: 3.0, position: StrokePosition::Center, paint: FxPaint::Color(photocraft_color::Color::rgb(0.0, 1.0, 0.0)), + overprint: false, }), Effect::Stroke(StrokeFx { common: FxCommon::new(photocraft_color::BlendMode::Normal, 0.5), size: 6.0, position: StrokePosition::Outside, paint: FxPaint::Color(photocraft_color::Color::rgb(0.0, 0.0, 1.0)), + overprint: false, }), ]; let back = roundtrip(&d); diff --git a/crates/io/tests/smart_contents_identity.rs b/crates/io/tests/it/smart_contents_identity.rs similarity index 100% rename from crates/io/tests/smart_contents_identity.rs rename to crates/io/tests/it/smart_contents_identity.rs diff --git a/crates/io/tests/smart_corpus.rs b/crates/io/tests/it/smart_corpus.rs similarity index 99% rename from crates/io/tests/smart_corpus.rs rename to crates/io/tests/it/smart_corpus.rs index 494a43071c..f258c01103 100644 --- a/crates/io/tests/smart_corpus.rs +++ b/crates/io/tests/it/smart_corpus.rs @@ -10,7 +10,7 @@ //! ones dropped). #![cfg(feature = "corpus")] -mod common; +use crate::common; use std::path::{Path, PathBuf}; use std::sync::Arc; diff --git a/crates/io/tests/svg.rs b/crates/io/tests/it/svg.rs similarity index 71% rename from crates/io/tests/svg.rs rename to crates/io/tests/it/svg.rs index d1baf418fa..5c695d1526 100644 --- a/crates/io/tests/svg.rs +++ b/crates/io/tests/it/svg.rs @@ -190,6 +190,31 @@ fn png_data_url() -> String { out } +/// A small valid PNG whose IHDR claims `w`x`h`, far larger than its actual (tiny) image data. +/// The header alone must be enough to reject it before any decode is attempted. +fn png_data_url_with_claimed_size(w: u32, h: u32) -> String { + let img = photocraft_codecs::Image::from_u8(2, 2, photocraft_codecs::ChannelLayout::Rgb, vec![255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255]).unwrap(); + let mut bytes = photocraft_codecs::encode(&img, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + // IHDR starts right after the 8-byte signature and 8-byte chunk length/type: width at 16, + // height at 20 (big-endian u32 each). + bytes[16..20].copy_from_slice(&w.to_be_bytes()); + bytes[20..24].copy_from_slice(&h.to_be_bytes()); + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = String::from("data:image/png;base64,"); + for chunk in bytes.chunks(3) { + let n = chunk.len(); + let v = (u32::from(chunk[0]) << 16) | (u32::from(*chunk.get(1).unwrap_or(&0)) << 8) | u32::from(*chunk.get(2).unwrap_or(&0)); + for i in 0..4 { + if i <= n { + out.push(T[((v >> (18 - 6 * i)) & 63) as usize] as char); + } else { + out.push('='); + } + } + } + out +} + #[test] fn embedded_raster_images_become_pixel_layers() { let r = open(&format!( @@ -204,6 +229,18 @@ fn embedded_raster_images_become_pixel_layers() { assert!(close(px(d, 16, 16), [1.0, 1.0, 1.0, 1.0]), "{:?}", px(d, 16, 16)); } +#[test] +fn embedded_png_with_a_huge_claimed_size_is_not_decoded() { + // The IHDR claims 40000x40000 (6.4 GB RGBA decoded) but the display size is tiny; the + // header-only check must reject it before resvg ever decodes the actual (2x2) image data. + let r = open(&format!( + "", + png_data_url_with_claimed_size(40_000, 40_000) + )); + // Rejected at the resolver: no image layer is produced (resvg treats it as a missing image). + assert!(r.document.layers.is_empty() || !matches!(r.document.layers[0].content, LayerContent::Raster(_)), "{:?}", r.document.layers); +} + #[test] fn text_becomes_outline_shapes_or_warns_without_a_font() { let r = open(&format!( @@ -245,6 +282,112 @@ fn gzip(bytes: &[u8]) -> Vec { e.finish().unwrap() } +/// A `data:` URL of `bytes` under `mime`, base64-encoded. +fn base64_data_url(mime: &str, bytes: &[u8]) -> String { + const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let mut out = format!("data:{mime};base64,"); + for chunk in bytes.chunks(3) { + let n = chunk.len(); + let v = (u32::from(chunk[0]) << 16) | (u32::from(*chunk.get(1).unwrap_or(&0)) << 8) | u32::from(*chunk.get(2).unwrap_or(&0)); + for i in 0..4 { + if i <= n { + out.push(T[((v >> (18 - 6 * i)) & 63) as usize] as char); + } else { + out.push('='); + } + } + } + out +} + +#[test] +fn gzip_compressed_embedded_svg_decompresses_once_with_a_size_cap() { + // A small gzip-compressed SVG embedded via data URL (SVGZ-in-data-URL) must still import. + let inner = format!(""); + let url = base64_data_url("image/svg+xml", &gzip(inner.as_bytes())); + let r = open(&format!("")); + assert!( + r.document.layers.iter().any(|l| matches!(l.content, LayerContent::Raster(_))), + "valid embedded gzip SVG must decode into a raster layer: {:?}", + r.document.layers + ); + + // A gzip-compressed embedded SVG whose decompressed size exceeds the cap must be rejected + // by our bounded inflate, not silently decompressed again without a limit by usvg. + let mut bomb = format!("").into_bytes(); + bomb.resize(usize::try_from(svg::MAX_INFLATED_BYTES).unwrap() + 1024, b' '); + bomb.extend_from_slice(b""); + let gz = gzip(&bomb); + assert!(gz.len() < 2 << 20, "{} bytes compressed", gz.len()); + let url = base64_data_url("image/svg+xml", &gz); + let start = std::time::Instant::now(); + let r = open(&format!("")); + assert!(start.elapsed() < std::time::Duration::from_secs(5), "took {:?}", start.elapsed()); + // The embedded bomb is rejected; the outer drawing may still open (e.g. with a missing + // image), but must not have decoded the bomb into a raster layer. + assert!(r.document.layers.iter().all(|l| !matches!(l.content, LayerContent::Raster(_))), "{:?}", r.document.layers); +} + +/// usvg parses an SVG embedded as a `data:` image on its own, past the drawing's depth check: +/// 20 000 nested groups in it overflowed a 2 MB thread stack, an abort nothing can catch. The +/// embedded SVG gets the same check now, so the image is dropped and the drawing opens. +#[test] +fn deeply_nested_embedded_svg_is_dropped_not_a_stack_overflow() { + let deep = nested(20_000); + let percent: String = deep.bytes().map(|b| if b.is_ascii_alphanumeric() { char::from(b).to_string() } else { format!("%{b:02X}") }).collect(); + let urls = [ + format!("data:image/svg+xml,{percent}"), + base64_data_url("image/svg+xml", deep.as_bytes()), + base64_data_url("text/plain", deep.as_bytes()), + base64_data_url("image/svg+xml", &gzip(deep.as_bytes())), + base64_data_url("text/plain", &gzip(deep.as_bytes())), + ]; + let embed = |url: &str| format!(""); + std::thread::Builder::new() + .stack_size(2 << 20) + .spawn(move || { + for url in &urls { + let r = open(&embed(url)); + assert!(r.document.layers.iter().all(|l| !matches!(l.content, LayerContent::Raster(_))), "{}: {:?}", &url[..20], r.document.layers); + } + // Just inside the limit (the drawing's own `` takes one level) the embedded + // drawing still renders (its rect is black). + let r = open(&embed(&base64_data_url("image/svg+xml", nested(svg::MAX_XML_DEPTH - 2).as_bytes()))); + assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 4, 4)); + }) + .unwrap() + .join() + .unwrap(); + // A PNG sent as `text/plain` is still an image, not markup to count. + let r = open(&format!( + "", + png_data_url().replace("image/png", "text/plain") + )); + assert!(close(px(&r.document, 1, 1), [1.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 1, 1)); +} + +/// The embedded SVG is parsed on top of the drawing's own recursion, so the two depths share +/// [`svg::MAX_XML_DEPTH`]: 511 + 511 groups, each fine alone, overflowed a 1 MB thread stack. +#[test] +fn an_embedded_svg_gets_only_the_nesting_depth_the_drawing_left() { + let embed = |outer: usize, inner: usize| { + let url = base64_data_url("image/svg+xml", nested(inner).as_bytes()); + format!("{}{}", "".repeat(outer), "".repeat(outer)) + }; + std::thread::Builder::new() + .stack_size(1 << 20) + .spawn(move || { + let r = open(&embed(svg::MAX_XML_DEPTH - 1, svg::MAX_XML_DEPTH - 1)); + assert!(px(&r.document, 4, 4)[3] < 0.01, "{:?}", px(&r.document, 4, 4)); + // Within the shared budget the embedded drawing renders (its rect is black). + let r = open(&embed(250, 250)); + assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 0.0, 1.0]), "{:?}", px(&r.document, 4, 4)); + }) + .unwrap() + .join() + .unwrap(); +} + /// 8 000 nested groups (70 KB) overflowed the XML parser's recursion on a 2 MB thread stack, an /// abort nothing can catch. It is an error now, found before the parser runs; the thread here /// has a small stack so a regression aborts the test instead of passing by luck. @@ -289,6 +432,9 @@ fn svgz_is_inflated_with_a_size_cap() { let r = import("drawing.svgz", &gzip(plain.as_bytes())).unwrap(); assert_eq!(r.document.layers.len(), 1); assert!(close(px(&r.document, 4, 4), [0.0, 0.0, 1.0, 1.0])); + let split = plain.len() / 2; + let members = [gzip(&plain.as_bytes()[..split]), gzip(&plain.as_bytes()[split..])].concat(); + assert!(close(px(&import("members.svgz", &members).unwrap().document, 4, 4), [0.0, 0.0, 1.0, 1.0])); // A bomb: a few hundred KB of gzip holding more than the cap of whitespace. let mut bomb = format!("").into_bytes(); bomb.resize(usize::try_from(svg::MAX_INFLATED_BYTES).unwrap() + 1024, b' '); diff --git a/crates/io/tests/text_corpus.rs b/crates/io/tests/it/text_corpus.rs similarity index 51% rename from crates/io/tests/text_corpus.rs rename to crates/io/tests/it/text_corpus.rs index 8597ac49d8..b0deed7746 100644 --- a/crates/io/tests/text_corpus.rs +++ b/crates/io/tests/it/text_corpus.rs @@ -169,6 +169,107 @@ fn corpus_photoshop_txt2_survives_an_unedited_save() { println!("Txt2 kept byte-for-byte in {checked} Photoshop type files"); } +/// #7962: Photoshop's type on a path (`Txt2` frames, EngineData calls it a text box) opens on its +/// path, at the anchors the generator gave Photoshop; the circle re-renders where Photoshop drew +/// it; and a save after an edit (new text, a moved path) reopens on the layer's path. +#[cfg(feature = "corpus")] +#[test] +fn corpus_photoshop_path_text_opens_on_its_path() { + use photocraft_doc::text::TextShape; + use photocraft_geom::Point; + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/photoshop/text"); + let k = 0.5522847498 * 96.0; + let cases: [(&str, bool, Vec<[f64; 2]>); 2] = [ + // Photoshop sets the type from the closed path's last anchor (the bottom). + ("path-circle.psd", true, vec![[128.0, 224.0], [32.0, 128.0], [128.0, 32.0], [224.0, 128.0]]), + ("path-wave-open.psd", false, vec![[16.0, 100.0], [160.0, 80.0], [304.0, 60.0]]), + ]; + let mut engine = photocraft_text::TextEngine::with_system_fonts(); + for (file, closed, anchors) in cases { + let f = root.join(file); + let bytes = std::fs::read(&f).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --photoshop`", f.display())); + let imp = photocraft_io::import(file, &bytes).unwrap(); + assert!(!imp.warnings.iter().any(|w| w.contains("type path")), "{file}: {:?}", imp.warnings); + let doc = imp.document; + let mut all = Vec::new(); + walk(&doc.layers, &mut all); + let layer = all.iter().find(|l| matches!(l.content, LayerContent::Text(_))).unwrap(); + let LayerContent::Text(t) = &layer.content else { unreachable!() }; + let doc_anchors = |t: &photocraft_doc::TextLayer| -> Vec { + let TextShape::Path { path, start, flip } = &t.shape else { panic!("{file}: not path text: {:?}", t.shape) }; + assert_eq!((path.closed, *start, *flip), (closed, 0.0, false), "{file}"); + path.knots.iter().map(|k| t.transform.apply(k.anchor)).collect() + }; + let got = doc_anchors(t); + assert_eq!(got.len(), anchors.len(), "{file}"); + for (g, [x, y]) in got.iter().zip(&anchors) { + assert!((g.x - x).abs() < 1e-3 && (g.y - y).abs() < 1e-3, "{file}: anchor {g:?}, want ({x}, {y})"); + } + if closed { + // The circle's handles are the generator's (kappa × r), so the path is the circle. + let TextShape::Path { path, .. } = &t.shape else { unreachable!() }; + let h = t.transform.apply(path.knots[0].out_ctrl); + assert!((h.x - (128.0 - k)).abs() < 1e-3 && (h.y - 224.0).abs() < 1e-3, "{file}: handle {h:?}"); + } + + // Photoshop's pixels (the wave's EngineData text is blank: Photoshop drew nothing). + if let Some(ps) = t.cache.as_ref().filter(|c| !c.content_bounds().is_empty()) { + let ps_rect = ps.content_bounds(); + let (_, ours) = engine.render(t, doc.resolution_dpi, doc.pixel_format()); + let our_rect = ours.surface.content_bounds(); + let u = Rect::new(ps_rect.x0.min(our_rect.x0), ps_rect.y0.min(our_rect.y0), ps_rect.x1.max(our_rect.x1), ps_rect.y1.max(our_rect.y1)); + let (a, b) = (alpha(ps, u), alpha(&ours.surface, u)); + let inter: f32 = a.iter().zip(&b).map(|(x, y)| x.min(*y)).sum(); + let union: f32 = a.iter().zip(&b).map(|(x, y)| x.max(*y)).sum(); + let iou = if union > 0.0 { inter / union } else { 1.0 }; + let edges = [ps_rect.x0 - our_rect.x0, ps_rect.y0 - our_rect.y0, ps_rect.x1 - our_rect.x1, ps_rect.y1 - our_rect.y1]; + println!("{file}: ps={ps_rect:?} ours={our_rect:?} iou={iou:.2} edges={edges:?}"); + // Where the type starts on the path and how far it reaches across it hold with any + // font; the far end and the glyph overlap need the font Photoshop used (a wider + // fallback runs further around the circle). + let [x0, y0, _, y1] = edges; + assert!([x0, y0, y1].iter().all(|d| d.abs() <= 8), "{file}: ink bounds {our_rect:?}, Photoshop's {ps_rect:?}"); + let family = t.runs.first().map(|r| r.style.font_family.clone()).unwrap_or_default(); + if engine.fonts.has_family(&family) { + assert!(edges.iter().all(|d| d.abs() <= 8), "{file}: ink bounds {our_rect:?}, Photoshop's {ps_rect:?}"); + assert!(iou > 0.2, "{file}: the glyphs sit where Photoshop drew them (IoU {iou:.2})"); + } else { + eprintln!("{file}: {family:?} not installed, far edge and overlap comparison skipped"); + } + } + + // Edited: new text and the path moved 10 px right; saved and opened again. + let mut edited = doc.clone(); + let id = layer.id; + for l in edited.layers.iter_mut() { + if l.id != id { + continue; + } + let LayerContent::Text(t) = &mut l.content else { continue }; + t.text = format!("{} again", t.text); + if let Some(r) = t.runs.last_mut() { + r.len += " again".len(); + } + t.sync_summary(); + if let TextShape::Path { path, .. } = &mut t.shape { + for k in &mut path.knots { + *k = k.transform(&photocraft_geom::Affine::translate(10.0, 0.0)); + } + } + t.psd_raw = Some(std::sync::Arc::new(photocraft_text::psd::build_tysh(t, edited.resolution_dpi, None))); + } + let out = photocraft_io::export(&edited, file, &Default::default()).unwrap().bytes; + let back = photocraft_io::import(file, &out).unwrap().document; + let mut all = Vec::new(); + walk(&back.layers, &mut all); + let LayerContent::Text(bt) = &all.iter().find(|l| matches!(l.content, LayerContent::Text(_))).unwrap().content else { unreachable!() }; + assert!(bt.text.ends_with(" again"), "{file}: {:?}", bt.text); + for (g, [x, y]) in doc_anchors(bt).iter().zip(&anchors) { + assert!((g.x - (x + 10.0)).abs() < 1e-3 && (g.y - y).abs() < 1e-3, "{file}: saved anchor {g:?}, want ({}, {y})", x + 10.0); + } + } +} + /// A text layer created in PhotoCraft (no PSD data) exports as an editable type layer and /// imports back with the same model. #[test] @@ -214,3 +315,65 @@ fn created_text_layer_roundtrips_through_psd() { assert_eq!(bt.transform, t.transform); assert!(bt.cache.as_ref().is_some_and(|c| c.content_bounds().width() > 10)); } + +/// Review of #8888: the older Photoshop's text objects (`/DocumentObjects /TextObjects`) in the +/// real psd-tools `text.psd`. Saving the type layer unedited leaves them (and the font list they +/// index) exactly as the file has them; editing only the size changes the size of the legacy style +/// run and nothing else in it, and the object's text, paragraph runs and view are untouched. +#[cfg(feature = "corpus")] +#[test] +fn corpus_text_psd_legacy_objects_are_edited_in_place() { + use photocraft_text::engine_data::Value as E; + + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/psd-tools/text.psd"); + assert!(path.is_file(), "{} is missing: run `cargo xtask corpus --all`", path.display()); + let bytes = std::fs::read(&path).unwrap(); + let doc = photocraft_io::import("text.psd", &bytes).unwrap().document; + let txt2 = doc.metadata.psd_global_blocks.iter().find(|b| &b.1 == b"Txt2").map(|b| b.2.clone()).expect("text.psd has a Txt2 block"); + let mut all = Vec::new(); + walk(&doc.layers, &mut all); + let layer = all.iter().find_map(|l| if let LayerContent::Text(t) = &l.content { Some(t.clone()) } else { None }).expect("a type layer"); + + let before = photocraft_text::psd::parse_txt2(&txt2).expect("the file's Txt2 parses"); + let legacy = |block: &E| block.path(&["DocumentObjects", "TextObjects"]).and_then(E::as_array).and_then(<[E]>::first).cloned().expect("the legacy object"); + let features = |entry: &E| { + entry + .path(&["Model", "StyleRun", "RunArray"]) + .and_then(E::as_array) + .and_then(<[E]>::first) + .and_then(|r| r.path(&["RunData", "StyleSheet", "Features"])) + .cloned() + }; + let items = |value: &E| if let E::Dict(items) = value { items.clone() } else { Vec::new() }; + + // Unedited: the legacy object and the font list are as the file has them. + let unedited = photocraft_text::psd::parse_txt2(&photocraft_text::psd::build_txt2_at(&[(0, &layer)], Some(&txt2), doc.resolution_dpi)).unwrap(); + assert!(legacy(&unedited) == legacy(&before), "an unedited layer leaves the legacy text object alone"); + assert!(unedited.path(&["DocumentResources", "FontSet"]) == before.path(&["DocumentResources", "FontSet"]), "and the font list it indexes"); + + // Edited: only the size. + let mut bigger = layer.clone(); + for run in &mut bigger.runs { + run.style.size_pt += 7.0; + } + let edited = photocraft_text::psd::parse_txt2(&photocraft_text::psd::build_txt2_at(&[(0, &bigger)], Some(&txt2), doc.resolution_dpi)).unwrap(); + let (was, now) = (features(&legacy(&before)).expect("features"), features(&legacy(&edited)).expect("features")); + let size = |f: &E| f.get("FontSize").and_then(E::as_f64).expect("FontSize"); + assert!((size(&now) - size(&was) - 7.0).abs() < 1e-6, "the legacy size follows the edit: {} -> {}", size(&was), size(&now)); + let rest = |f: &E| items(f).into_iter().filter(|(k, _)| k != "FontSize").collect::>(); + let changed: Vec = + rest(&was).iter().zip(rest(&now).iter()).filter(|(a, b)| a != b).map(|(a, b)| format!("{}: {:?} -> {:?}", a.0, a.1, b.1)).collect(); + assert!(rest(&was) == rest(&now), "every other style key of the run is as the file has it; changed: {changed:?}"); + let others = |entry: &E, key: &str| entry.path(&["Model", key]).cloned(); + for key in ["Text", "ParagraphRun", "KernRun"] { + assert!(others(&legacy(&edited), key) == others(&legacy(&before), key), "{key} is untouched"); + } + assert!(legacy(&edited).get("View") == legacy(&before).get("View"), "the view is untouched"); + let lengths = |entry: &E| { + entry + .path(&["Model", "StyleRun", "RunArray"]) + .and_then(E::as_array) + .map(|rs| rs.iter().map(|r| r.get("Length").and_then(E::as_i64)).collect::>()) + }; + assert!(lengths(&legacy(&edited)) == lengths(&legacy(&before)), "the runs still cover the text"); +} diff --git a/crates/io/tests/text_styles.rs b/crates/io/tests/it/text_styles.rs similarity index 100% rename from crates/io/tests/text_styles.rs rename to crates/io/tests/it/text_styles.rs diff --git a/crates/io/tests/tiff_corpus.rs b/crates/io/tests/it/tiff_corpus.rs similarity index 93% rename from crates/io/tests/tiff_corpus.rs rename to crates/io/tests/it/tiff_corpus.rs index 143e75bea8..697eab6d32 100644 --- a/crates/io/tests/tiff_corpus.rs +++ b/crates/io/tests/it/tiff_corpus.rs @@ -114,6 +114,14 @@ fn corpus_round_trips_through_layered_tiff() { let expected: Document = { let mut d = oracle.clone(); d.channels.clear(); + // A flat Duotone is written as its inks rendered in RGB (as for any flat format). + if flat + && d.mode == photocraft_color::ColorMode::Duotone + && let Some(duo) = d.duotone.take() + { + d.layers.push(Layer::new("Duotone", photocraft_doc::LayerContent::Adjustment(duo.display_adjustment()))); + d.mode = photocraft_color::ColorMode::Rgb; + } d }; let x = photocraft_compose::flatten(&expected).px; diff --git a/crates/io/tests/it/tiff_layers.rs b/crates/io/tests/it/tiff_layers.rs new file mode 100644 index 0000000000..f2ccddc92e --- /dev/null +++ b/crates/io/tests/it/tiff_layers.rs @@ -0,0 +1,1166 @@ +//! Layered TIFF (Photoshop layer data in tags 37724 / 34377): Document → TIFF → Document round +//! trips in every mode and depth, equivalence with the PSD path, the flat fall-backs, files +//! written by an independent implementation (psdtags, both byte orders), and malformed input. + +use crate::common; + +use common::*; +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, LayerContent}; +use photocraft_io::*; +use photocraft_psd::tiff::{ByteOrder, ImageSourceData}; + +fn tiff(doc: &Document, layers: bool) -> ExportResult { + export(doc, "x.tif", &ExportOptions { tiff_layers: layers, ..Default::default() }).expect("export") +} + +/// The two Photoshop tags of a TIFF: (resources, layers). +fn tags(bytes: &[u8]) -> (Option>, Option>) { + let (r, l) = photocraft_codecs::tiff_photoshop_tags(bytes); + (r.map(<[u8]>::to_vec), l.map(<[u8]>::to_vec)) +} + +fn roundtrip(doc: &Document) -> Document { + let r = tiff(doc, true); + let (resources, layers) = tags(&r.bytes); + let layers = layers.expect("layer data"); + assert!(resources.is_some_and(|r| !r.is_empty()), "image resources"); + // The layer data matches the container's byte order and is byte-stable through the psd crate. + let order = if photocraft_codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; + let (isd, w) = ImageSourceData::from_bytes(&layers).expect("parse layer data"); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(isd.byte_order, order); + assert_eq!(isd.to_bytes(order).unwrap().0, layers); + // The TIFF is a plain image to any other reader: the composite at the document's size. + let flat = photocraft_codecs::decode(&r.bytes).expect("flat decode"); + assert_eq!(flat.dimensions(), (doc.size.width, doc.size.height)); + // Extra alpha channels and a Quick Mask travel as TIFF extra samples (at most MAX_EXTRA_SAMPLES). + let extra = doc.channels.len() + usize::from(doc.quick_mask.is_some()); + assert!(!r.warnings.iter().any(|w| w.contains("not written")), "{:?}", r.warnings); + if extra > 0 && extra <= photocraft_codecs::MAX_EXTRA_SAMPLES { + assert_eq!(flat.meta.extra_samples.len(), extra, "{:?}", r.warnings); + } + import("x.tif", &r.bytes).expect("import").document +} + +/// `doc` as a layered TIFF round trip returns it, including extra channels. +fn expected(doc: &Document) -> Document { + doc.clone() +} + +macro_rules! rt_case { + ($name:ident, $mode:expr, $depth:expr, $f:expr) => { + #[test] + fn $name() { + let d = gen_doc($mode, $depth, $f); + let back = roundtrip(&d); + assert_docs_eq(&expected(&d), &back); + } + }; +} + +rt_case!(rt_rgb8_all, ColorMode::Rgb, SampleType::U8, Features::ALL); +rt_case!(rt_rgb16_all, ColorMode::Rgb, SampleType::U16, Features::ALL); +rt_case!(rt_rgb32_all, ColorMode::Rgb, SampleType::F32, Features::ALL); +rt_case!(rt_gray8_all, ColorMode::Grayscale, SampleType::U8, Features::ALL); +rt_case!(rt_gray16_all, ColorMode::Grayscale, SampleType::U16, Features::ALL); +rt_case!(rt_gray32_all, ColorMode::Grayscale, SampleType::F32, Features::ALL); +rt_case!(rt_cmyk8_all, ColorMode::Cmyk, SampleType::U8, Features::ALL); +rt_case!(rt_cmyk16_all, ColorMode::Cmyk, SampleType::U16, Features::ALL); +rt_case!(rt_rgb8_pixels, ColorMode::Rgb, SampleType::U8, Features::PIXELS); +rt_case!(rt_gray16_pixels, ColorMode::Grayscale, SampleType::U16, Features::PIXELS); +rt_case!(rt_cmyk8_pixels, ColorMode::Cmyk, SampleType::U8, Features::PIXELS); + +#[test] +fn tiff_and_psd_open_as_the_same_document() { + for (mode, depth) in [(ColorMode::Rgb, SampleType::U8), (ColorMode::Cmyk, SampleType::U16), (ColorMode::Grayscale, SampleType::F32)] { + let d = gen_doc(mode, depth, Features::ALL); + let psd = export(&d, "x.psd", &ExportOptions::default()).unwrap(); + let from_psd = import("x.psd", &psd.bytes).unwrap().document; + let from_tif = roundtrip(&d); + assert_docs_eq(&expected(&from_psd), &from_tif); + // The composite the TIFF carries is what the document looks like. + let tif = tiff(&d, true); + let flat = import( + "flat.tif", + &photocraft_codecs::encode(&photocraft_codecs::decode(&tif.bytes).unwrap(), photocraft_codecs::Format::Tiff, &Default::default()).unwrap(), + ) + .unwrap() + .document; + let a = photocraft_compose::flatten(&d).px; + let b = photocraft_compose::flatten(&flat).px; + assert!(max_diff(&a, &b) <= 2.0 / 255.0, "{mode:?} {depth:?}: composite differs by {}", max_diff(&a, &b)); + } +} + +#[test] +fn every_compression_keeps_the_layers() { + use photocraft_codecs::TiffCompression; + let d = gen_doc(ColorMode::Rgb, SampleType::U16, Features::PIXELS); + for c in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { + let mut opts = ExportOptions { tiff_layers: true, ..Default::default() }; + opts.encode.tiff_compression = c; + let r = export(&d, "x.tiff", &opts).unwrap(); + let back = import("x.tiff", &r.bytes).unwrap().document; + assert_docs_eq(&d, &back); + } +} + +/// The Artist tag's text, and the bytes every EXIF assertion looks for. +const ARTIST: &[u8] = b"fixture-artist"; + +/// A minimal little-endian EXIF (TIFF) block with a single Artist tag and no Orientation, so +/// `upright_exif` passes it through untouched. +fn exif() -> Vec { + let mut artist = ARTIST.to_vec(); + artist.push(0); // ASCII NUL terminator + let mut v = b"II*\0\x08\0\0\0".to_vec(); // TIFF header, first IFD at 8 + v.extend_from_slice(&1u16.to_le_bytes()); // one entry + v.extend_from_slice(&0x013bu16.to_le_bytes()); // Artist + v.extend_from_slice(&2u16.to_le_bytes()); // ASCII + v.extend_from_slice(&(artist.len() as u32).to_le_bytes()); + v.extend_from_slice(&26u32.to_le_bytes()); // value offset: 8 + 2 + 12 + 4 + v.extend_from_slice(&0u32.to_le_bytes()); // no next IFD + v.extend_from_slice(&artist); + v +} + +/// #1545: a layered TIFF carries EXIF as Photoshop resource 1058 and reads it back, so it must +/// not report a loss it never makes. A flat TIFF writes it as its own tags and keeps it too. +#[test] +fn layered_tiff_keeps_exif_without_claiming_a_loss() { + let mut d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + d.metadata.exif = Some(std::sync::Arc::new(exif())); + + let r = tiff(&d, true); + assert!(!r.warnings.iter().any(|w| w.contains("EXIF")), "a layered TIFF keeps EXIF, so it must not claim to drop it: {:?}", r.warnings); + // ...and the tags really are in the file, so the missing warning is a true absence of loss. + let back = import("x.tif", &r.bytes).unwrap().document; + let kept = back.metadata.exif.as_deref().expect("EXIF survives the layered export"); + assert!(kept.windows(ARTIST.len()).any(|w| w == ARTIST), "the Artist tag did not survive: {kept:?}"); + + // The flat path writes the EXIF block's tags as the TIFF's own (here IFD0's Artist, which + // reads back as the document's Artist text), so it reports no loss either. + let flat = tiff(&d, false); + assert!(!flat.warnings.iter().any(|w| w.contains("EXIF")), "a flat TIFF keeps EXIF: {:?}", flat.warnings); + let back = import("x.tif", &flat.bytes).unwrap().document; + let artist = std::str::from_utf8(ARTIST).unwrap().trim_end_matches('\0'); + assert!(back.metadata.text.iter().any(|(k, v)| k == "Artist" && v == artist), "{:?}", back.metadata.text); +} + +#[test] +fn discard_layers_writes_a_flat_file() { + let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + let r = tiff(&d, false); + assert_eq!(tags(&r.bytes), (None, None)); + assert!(r.warnings.iter().any(|w| w.contains("flattened")), "{:?}", r.warnings); + let back = import("x.tif", &r.bytes).unwrap().document; + assert_eq!(back.layers.len(), 1); + assert!(!tiff_layers::would_write_layers(&back)); +} + +#[test] +fn a_plain_background_needs_no_layer_data() { + let d = import("x.png", &export(&gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS), "x.png", &Default::default()).unwrap().bytes).unwrap().document; + assert_eq!(d.layers.len(), 1); + assert!(!tiff_layers::would_write_layers(&d)); + let r = tiff(&d, true); + assert_eq!(tags(&r.bytes), (None, None)); + assert!(r.warnings.is_empty(), "{:?}", r.warnings); + let back = import("x.tif", &r.bytes).unwrap().document; + assert_docs_eq(&d, &back); +} + +#[test] +fn lab_documents_are_saved_flat_with_a_warning() { + let d = gen_doc(ColorMode::Lab, SampleType::U8, Features::PIXELS); + let r = tiff(&d, true); + assert_eq!(tags(&r.bytes), (None, None)); + assert!(r.warnings.iter().any(|w| w.contains("Lab") && w.contains("layers are not kept")), "{:?}", r.warnings); + assert!(import("x.tif", &r.bytes).is_ok()); +} + +#[test] +fn transparency_is_straight_in_the_composite() { + // A translucent layer over nothing: the TIFF's composite keeps its colour under the alpha + // instead of being matted against white like a PSD's merged image. + let mut d = Document::new("t", photocraft_geom::Size::new(4, 4), ColorMode::Rgb, SampleType::U8); + let fmt = d.pixel_format(); + let mut l = raster("Red", fmt, photocraft_geom::Rect::new(0, 0, 4, 4), 0, false); + if let LayerContent::Raster(s) = &mut l.content { + let px: Vec = (0..16).flat_map(|_| [1.0, 0.0, 0.0, 0.5]).collect(); + s.write_region(photocraft_geom::Rect::new(0, 0, 4, 4), &px); + } + d.layers.push(l); + let r = tiff(&d, true); + let flat = photocraft_codecs::decode(&r.bytes).unwrap(); + assert!(flat.layout().has_alpha()); + let px = flat.data(); + assert_eq!(&px[..4], &[255, 0, 0, 128]); + let back = import("x.tif", &r.bytes).unwrap().document; + assert_docs_eq(&d, &back); +} + +/// Photoshop (27.11) saves a layered TIFF with Save Transparency as a composite with a +/// transparency sample, a positive layer count and an `MTrn` block first in the layer data +/// (the specification's key is `Mtrn`; `nrTM` in an `II` file). That sample is the merged +/// transparency: it opens as no alpha channel, and a PSD save doesn't write it twice. +#[test] +fn photoshop_mtrn_marks_the_merged_transparency_of_a_layered_tiff() { + use photocraft_psd::{PsdFile, TaggedBlock}; + for depth in [SampleType::U8, SampleType::U16] { + let mut d = Document::new("t", photocraft_geom::Size::new(8, 6), ColorMode::Rgb, depth); + let fmt = d.pixel_format(); + d.layers.push(raster("shape", fmt, photocraft_geom::Rect::new(1, 1, 5, 4), 3, true)); + let r = tiff(&d, true); + let mut img = photocraft_codecs::decode(&r.bytes).expect("decode"); + assert!(img.layout().has_alpha(), "{depth:?}: the composite has a transparency sample"); + let (isd, _) = ImageSourceData::from_bytes(&tags(&r.bytes).1.expect("layer data")).expect("parse"); + for order in [ByteOrder::Little, ByteOrder::Big] { + let case = format!("{depth:?} {order:?}"); + // Photoshop's form of the same layer data. + let mut ps = isd.clone(); + ps.layer_info.as_mut().expect("layers").merged_alpha = false; + ps.global_blocks.insert(0, TaggedBlock::new(*b"MTrn", Vec::new())); + let layers = ps.to_bytes(order).expect("layer data").0; + if order == ByteOrder::Little { + assert!(layers.windows(12).any(|w| w == b"MIB8nrTM\0\0\0\0"), "{case}: stored as Photoshop stores it"); + } + img.meta.photoshop_layers = Some(layers); + let bytes = photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &photocraft_codecs::EncodeOptions::default()).expect("encode"); + + let opened = import("x.tif", &bytes).expect("import").document; + let names: Vec<&str> = opened.channels.iter().map(|c| c.name.as_str()).collect(); + assert!(names.is_empty(), "{case}: no alpha channel from the merged transparency: {names:?}"); + // The marker is kept for the next save. + let mut want = d.clone(); + want.metadata.psd_global_blocks = vec![(*b"8BIM", *b"MTrn", std::sync::Arc::new(Vec::new()))]; + assert_docs_eq(&want, &opened); + let psd = PsdFile::from_bytes(&export(&opened, "x.psd", &ExportOptions::default()).expect("PSD save").bytes).expect("parse"); + assert_eq!(psd.header.channels, 4, "{case}: colour + merged transparency"); + } + } +} + +#[test] +fn big_documents_use_the_psb_signature() { + // Forcing PSB stands in for a > 30000 px document. + let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + let r = export(&d, "x.tif", &ExportOptions { force_psb: true, tiff_layers: true, ..Default::default() }).unwrap(); + let (_, layers) = tags(&r.bytes); + assert!(layers.unwrap().starts_with(photocraft_psd::tiff::SIGNATURE_PSB)); + let back = import("x.tif", &r.bytes).unwrap().document; + assert_docs_eq(&d, &back); +} + +/// Files written by psdtags (`scripts/layered_tiff_fixtures.py`): two RGB layers, "Background" +/// (RLE) and an offset, translucent "Upper é" (ZIP, Multiply, opacity 200), in both byte orders +/// at 8 and 16 bits. Binary files stay out of this repository: run the script to write them +/// to `corpus/layered-tiff/` (gitignored); without them these checks are skipped. +fn fixture(name: &str) -> Option> { + let bytes = std::fs::read(format!("{}/../../corpus/layered-tiff/{name}", env!("CARGO_MANIFEST_DIR"))).ok(); + if bytes.is_none() { + eprintln!("skipped {name}: run `python scripts/layered_tiff_fixtures.py` to write corpus/layered-tiff/"); + } + bytes +} + +#[test] +fn psdtags_fixtures_open_with_their_layers() { + for (name, depth) in [ + ("layered-le-8bit.tif", SampleType::U8), + ("layered-be-8bit.tif", SampleType::U8), + ("layered-le-16bit.tif", SampleType::U16), + ("layered-be-16bit.tif", SampleType::U16), + ] { + let Some(bytes) = fixture(name) else { continue }; + let r = import(name, &bytes).unwrap_or_else(|e| panic!("{name}: {e}")); + let d = &r.document; + assert_eq!(d.size, photocraft_geom::Size::new(6, 4), "{name}"); + assert_eq!(d.mode, ColorMode::Rgb); + assert_eq!(d.depth, depth, "{name}"); + let names: Vec<&str> = d.layers.iter().map(|l| l.name.as_str()).collect(); + assert_eq!(names, ["Background", "Upper \u{e9}"], "{name}"); + let upper = &d.layers[1]; + assert_eq!(upper.blend, photocraft_color::BlendMode::Multiply); + assert!((upper.opacity - 200.0 / 255.0).abs() < 1e-6); + let LayerContent::Raster(s) = &upper.content else { panic!("raster") }; + let bounds = s.content_bounds(); + assert_eq!((bounds.x0, bounds.y0, bounds.width(), bounds.height()), (1, 1, 4, 3), "{name}"); + // Pixel (x=2, y=0) of the upper layer: the generator's ramp, alpha 160/255. + let px = s.pixel(3, 1); + let expect = |v: u32| (v % 256) as f32 / 255.0; + assert!((px[0] - expect(2 * 40 + 7)).abs() < 1e-3, "{name}: {px:?}"); + assert!((px[1] - expect(7 * 3)).abs() < 1e-3, "{name}: {px:?}"); + assert!((px[3] - 160.0 / 255.0).abs() < 1e-3, "{name}: {px:?}"); + let LayerContent::Raster(bg) = &d.layers[0].content else { panic!("raster") }; + let b = bg.pixel(5, 3); + assert!((b[0] - expect(5 * 40 + 1)).abs() < 1e-3, "{name}: {b:?}"); + assert!((b[3] - 1.0).abs() < 1e-6); + assert_eq!(d.resolution_dpi, 300.0); + // tifffile writes a Software tag; nothing else is worth a warning. + assert!(r.warnings.iter().all(|w| w.contains("text metadata")), "{name}: {:?}", r.warnings); + // psdtags wrote the composite's alpha sample without the transparency flag, so by the + // PSD rules it is an alpha channel. A re-save keeps it as a TIFF extra sample. + assert_eq!(d.channels.len(), 1, "{name}"); + let out = tiff(d, true); + let back = import(name, &out.bytes).unwrap().document; + assert_docs_eq(d, &back); + } +} + +/// Advanced Blending (`knko`, `infx`, `clbl`, `tsly`, `lmgm`, `vmgm`), fill opacity (`iOpa`), +/// channel restrictions (`brst`) and Blend If (the records' blending ranges) survive a layered +/// TIFF with its layer data in either byte order: the encoder's own, and the other one made by +/// transcoding the tag (the same length, spliced in place). +#[test] +fn advanced_blending_survives_layered_tiff_in_both_byte_orders() { + use photocraft_doc::{AdvancedBlending, BlendRange, Knockout}; + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut d = gen_doc(ColorMode::Rgb, depth, Features::PIXELS); + let adv = AdvancedBlending { + knockout: Knockout::Deep, + blend_interior: true, + blend_clipped: false, + transparency_shapes: false, + layer_mask_hides_effects: true, + vector_mask_hides_effects: true, + }; + d.layers[1].advanced = adv; + d.layers[1].fill_opacity = 64.0 / 255.0; + d.layers[1].excluded_channels = 0b010; + d.layers[1].blend_if.set(0, [BlendRange { black: [10, 40], white: [200, 250] }, BlendRange { black: [0, 0], white: [128, 128] }]); + d.layers[2].advanced.knockout = Knockout::Shallow; + let r = tiff(&d, true); + let (_, layers) = tags(&r.bytes); + let layers = layers.expect("layer data"); + let (isd, w) = ImageSourceData::from_bytes(&layers).expect("parse"); + assert!(w.is_empty(), "{w:?}"); + let native = isd.byte_order; + let other = if native == ByteOrder::Little { ByteOrder::Big } else { ByteOrder::Little }; + let (swapped, w) = isd.to_bytes(other).expect("transcode"); + assert!(w.is_empty(), "{depth:?}: blocks dropped while transcoding: {w:?}"); + assert_eq!(swapped.len(), layers.len()); + let at = r.bytes.windows(layers.len()).position(|x| x == &layers[..]).expect("tag data in the file"); + let mut flipped = r.bytes.clone(); + flipped[at..at + layers.len()].copy_from_slice(&swapped); + for (order, bytes) in [(native, &r.bytes), (other, &flipped)] { + let back = import("x.tif", bytes).unwrap_or_else(|e| panic!("{depth:?} {order:?}: {e}")).document; + let l = &back.layers[1]; + assert_eq!(l.advanced, adv, "{depth:?} {order:?}"); + assert_eq!(l.fill_opacity, 64.0 / 255.0, "{depth:?} {order:?}"); + assert_eq!(l.excluded_channels, 0b010, "{depth:?} {order:?}"); + assert_eq!(l.blend_if, d.layers[1].blend_if, "{depth:?} {order:?}"); + assert_eq!(back.layers[2].advanced.knockout, Knockout::Shallow, "{depth:?} {order:?}"); + assert_docs_eq(&expected(&d), &back); + } + } +} + +fn damaged_layer_data_opens_flattened_part(part: u64) { + let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + let r = tiff(&d, true); + let (_, layers) = tags(&r.bytes); + let layers = layers.unwrap(); + let at = r.bytes.windows(layers.len()).position(|w| w == &layers[..]).unwrap(); + // Garbage after the signature: no layers, a warning, the composite as one layer. + let mut bad = r.bytes.clone(); + for b in &mut bad[at + 36..at + 36 + 24] { + *b ^= 0xa5; + } + let r2 = import("x.tif", &bad).unwrap(); + assert_eq!(r2.document.layers.len(), 1); + assert!(r2.warnings.iter().any(|w| w.contains("could not be read")), "{:?}", r2.warnings); + // Truncations and bit flips through the whole file: never a panic. A run takes a quarter of + // them, a different quarter each day. + let sweep = photocraft_testkit::Sweep::new(4); + let mut case = 0; + let mut selected = 0; + let mut runs = || { + case += 1; + if !sweep.includes(case) { + return false; + } + let belongs = selected % 4 == part; + selected += 1; + belongs + }; + for cut in (0..r.bytes.len()).step_by(11).filter(|_| runs()) { + let _ = import("x.tif", &r.bytes[..cut]); + } + for i in (0..r.bytes.len()).step_by(29).filter(|_| runs()) { + let mut b = r.bytes.clone(); + b[i] ^= 0x5a; + let _ = import("x.tif", &b); + } + for name in ["layered-le-8bit.tif", "layered-be-16bit.tif"] { + let Some(bytes) = fixture(name) else { continue }; + for cut in (0..bytes.len()).step_by(7).filter(|_| runs()) { + let _ = import(name, &bytes[..cut]); + } + for i in (0..bytes.len()).step_by(5).filter(|_| runs()) { + let mut b = bytes.clone(); + b[i] ^= 0xff; + let _ = import(name, &b); + } + } +} + +/// Every sample of a TIFF, read by the `tiff` crate rather than PhotoCraft's decoder. +/// +/// That decoder drops unspecified extra samples when the photometric interpretation is RGB, so +/// the file is retagged MinIsBlack first. The result is then a multiband image of every sample, +/// which is what libtiff's `TIFFReadScanline` returns and what a RIP walks. +fn independent_samples(bytes: &[u8]) -> (u16, Vec, Vec, u32, Vec) { + let mut tags = tiff::decoder::Decoder::new(std::io::Cursor::new(bytes)).expect("tiff tags"); + let spp: u16 = tags.get_tag_unsigned(tiff::tags::Tag::SamplesPerPixel).expect("spp"); + let bps = tags.get_tag_u16_vec(tiff::tags::Tag::BitsPerSample).expect("bps"); + let extra = tags.get_tag_u16_vec(tiff::tags::Tag::ExtraSamples).unwrap_or_default(); + let rows = tags.find_tag_unsigned::(tiff::tags::Tag::RowsPerStrip).ok().flatten().unwrap_or(0); + let patched = photometric_min_is_black(bytes); + let mut dec = tiff::decoder::Decoder::new(std::io::Cursor::new(patched.as_slice())).expect("tiff decode"); + let tiff::decoder::DecodingResult::U8(samples) = dec.read_image().expect("read image") else { + panic!("expected 8-bit samples"); + }; + (spp, bps, extra, rows, samples) +} + +/// Classic-TIFF photometric tag 262 → 1 (MinIsBlack), value stored inline in the IFD entry. +fn photometric_min_is_black(src: &[u8]) -> Vec { + let mut bytes = src.to_vec(); + let le = bytes.first().copied() == Some(b'I'); + let ru16 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 2)?; + Some(if le { u16::from_le_bytes([s[0], s[1]]) } else { u16::from_be_bytes([s[0], s[1]]) }) + }; + let ru32 = |b: &[u8], i: usize| -> Option { + let s = b.get(i..i + 4)?; + Some(if le { u32::from_le_bytes(s.try_into().ok()?) } else { u32::from_be_bytes(s.try_into().ok()?) }) + }; + let Some(ifd) = ru32(&bytes, 4) else { return bytes }; + let Some(n) = ru16(&bytes, ifd as usize) else { return bytes }; + for e in 0..n as usize { + let at = ifd as usize + 2 + e * 12; + if ru16(&bytes, at) == Some(262) { + let value = if le { 1u16.to_le_bytes() } else { 1u16.to_be_bytes() }; + if let Some(slot) = bytes.get_mut(at + 8..at + 10) { + slot.copy_from_slice(&value); + } + break; + } + } + bytes +} + +fn assert_plane(samples: &[u8], spp: usize, plane: usize, expected: &[u8]) { + assert_eq!(samples.len(), expected.len().saturating_mul(spp)); + let mut bad = 0usize; + let mut first = None; + for (i, want) in expected.iter().enumerate() { + let got = samples.get(i * spp + plane).copied(); + if got != Some(*want) { + bad += 1; + first.get_or_insert((i, got)); + } + } + assert_eq!(bad, 0, "{bad} mismatches, first {first:?}"); +} + +/// A multi-layer transparent document, large enough for several TIFF strips and several +/// raster tiles, with a spot whose every pixel is a known value. +#[test] +fn dtf_like_spot_is_written_in_full() { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let (w, h) = (900u32, 360u32); + let mut doc = Document::new("dtf", Size::new(w, h), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + let px = photocraft_raster::from_rgba(&fmt, [0.2, 0.5, 0.9, 1.0]); + art.fill_rect(Rect::new(30, 20, 870, 340), &px); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let mut ink = Surface::new(fmt); + let px = photocraft_raster::from_rgba(&fmt, [0.9, 0.1, 0.2, 0.5]); + ink.fill_rect(Rect::new(80, 40, 700, 300), &px); + let mut layer = Layer::new("Ink", LayerContent::Raster(ink)); + layer.opacity = 0.8; + doc.layers.push(layer); + + let n = (w as usize) * (h as usize); + let mut spot_bytes = vec![0u8; n]; + for y in 0..h as usize { + for x in 0..w as usize { + spot_bytes[y * w as usize + x] = ((x * 3 + y * 17) % 251) as u8; + } + } + // Outside the artwork the composite is transparent, but the spot is not zero: a reader that + // binds "Spot Color 1" to the alpha plane fails here. + assert_eq!(spot_bytes[10 * w as usize + 10], 200); + let gray = PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false); + let spot_surface = Surface::from_interleaved(gray, Rect::new(0, 0, w as i32, h as i32), &spot_bytes); + let mut channel = AlphaChannel::new("Spot Color 1", spot_surface); + channel.spot = Some((Color::rgb(1.0, 0.0, 0.0), 0.6)); + doc.channels.push(channel); + + verify_dtf_exports(&doc, &spot_bytes, w, h); +} + +fn assert_spot_resources(resources: &[u8], name: &str) { + use photocraft_psd::tiff::resources_from_bytes; + let (blocks, warnings) = resources_from_bytes(resources).unwrap(); + assert!(warnings.is_empty(), "{name} {warnings:?}"); + let block = |id: u16| blocks.iter().find(|b| b.id == id).unwrap_or_else(|| panic!("{name} missing {id}")); + // "Spot Color 1" is 12 characters: Photoshop stores 13 bytes and pads the 8BIM block, not the string. + assert_eq!(block(1006).data, b"\x0cSpot Color 1", "{name}"); + assert_eq!(block(1045).data.len(), 30, "{name}"); + let info = &block(1077).data; + assert_eq!(info.len(), 17, "{name}"); + assert_eq!(u16::from_be_bytes([info[14], info[15]]), 60, "{name} solidity"); + assert_eq!(info[16], 2, "{name} spot kind"); + // Photoshop numbers a new document's channels from 3. + assert_eq!(block(1053).data, 3u32.to_be_bytes(), "{name} channel id"); +} + +fn verify_dtf_samples(bytes: &[u8], stored_spot: &[u8], w: u32, h: u32, name: &str) -> Vec { + let img = photocraft_codecs::decode(bytes).unwrap(); + assert!(!img.layout().has_alpha(), "{name}: {:?}", img.layout()); + assert_eq!(img.layout().color_channels(), 3, "{name}"); + assert_eq!(img.meta.extra_samples.len(), 1, "{name}"); + assert_eq!(img.meta.extra_samples[0].kind, 0, "{name}"); + assert_eq!(img.meta.extra_samples[0].data, stored_spot, "{name} own decoder"); + let (spp, bps, extra, rows, samples) = independent_samples(bytes); + assert_eq!(spp, 4, "{name}"); + assert_eq!(bps, vec![8, 8, 8, 8], "{name}"); + assert_eq!(extra, vec![0], "{name} ExtraSamples"); + assert!(rows > 0 && rows < h, "{name} rows_per_strip {rows}"); + assert_plane(&samples, 4, 3, stored_spot); + let at = (10 * w as usize + 10) * 4; + assert_eq!(&samples[at..at + 3], &[255, 255, 255], "{name} matted rgb"); + samples +} + +fn assert_dtf_channel(back: &Document, spot_bytes: &[u8], w: u32, h: u32, name: &str) { + use photocraft_geom::Rect; + assert_eq!(back.channels.len(), 1, "{name}"); + assert_eq!(back.channels[0].name, "Spot Color 1"); + let (ink, sol) = back.channels[0].spot.expect("spot"); + assert_eq!(ink.to_rgb(), [1.0, 0.0, 0.0], "{name}"); + assert!((sol - 0.6).abs() < 1e-4, "{name}"); + let area = Rect::new(0, 0, w as i32, h as i32); + assert_eq!(back.channels[0].surface.to_interleaved(area), spot_bytes, "{name} reimport"); +} + +fn verify_dtf_layers(bytes: &[u8], layer_tag: Option>, back: &Document, layers: bool, name: &str) { + if layers { + let layer_tag = layer_tag.expect("layer tag"); + let (isd, w) = ImageSourceData::from_bytes(&layer_tag).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let info = isd.layer_info.expect("layers"); + assert!(!info.merged_alpha, "{name}"); + assert!(info.layers.iter().all(|l| l.flags.0 & 0x08 != 0), "{name} photoshop flag"); + assert!(info.layers.iter().all(|l| l.blocks.iter().any(|b| &b.key == b"lnsr")), "{name} lnsr"); + let mask = isd.global_layer_mask.expect("LMsk"); + assert_eq!(mask.data.len(), 14, "{name}"); + assert_eq!(mask.opacity(), Some(50)); + assert_eq!(mask.kind(), Some(128)); + assert_photoshop_layered_tiff(bytes, true); + assert_eq!(back.layers.len(), 2, "{name}"); + let ink = back.layers.iter().find(|l| l.name == "Ink").expect("ink layer"); + let px = ink.surface().expect("ink pixels").pixel(100, 100); + assert!((px[3] - 0.5).abs() < 0.02, "{name} layer alpha {px:?}"); + let outside = ink.surface().expect("ink pixels").pixel(0, 0); + assert!(outside[3] < 0.02, "{name} transparent pixel {outside:?}"); + } else { + assert!(layer_tag.is_none(), "{name}"); + } +} + +fn verify_dtf_exports(doc: &Document, spot_bytes: &[u8], w: u32, h: u32) { + let mut seen = Vec::new(); + for (name, layers) in [("flat", false), ("layered", true)] { + let opts = ExportOptions { tiff_layers: layers, ..ExportOptions::default() }; + let r = export(doc, "dtf.tif", &opts).unwrap(); + assert!(!r.warnings.iter().any(|w| w.contains("not written")), "{name}: {:?}", r.warnings); + // Photoshop stores spot ink inverted: document 255 (solid) is file 0. + let stored_spot: Vec = spot_bytes.iter().map(|b| 255 - b).collect(); + + let samples = verify_dtf_samples(&r.bytes, &stored_spot, w, h, name); + seen.push(samples.clone()); + let (resources, layer_tag) = tags(&r.bytes); + assert_spot_resources(&resources.expect("photoshop resources"), name); + + let back = import("dtf.tif", &r.bytes).unwrap().document; + assert_dtf_channel(&back, spot_bytes, w, h, name); + verify_dtf_layers(&r.bytes, layer_tag, &back, layers, name); + } + assert_eq!(seen[0], seen[1], "flat and layered IFD samples differ"); +} + +/// A transparent document with no user channels still stores image alpha. +#[test] +fn transparent_tiff_without_channels_keeps_image_alpha() { + use photocraft_doc::Layer; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let mut doc = Document::new("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + art.fill_rect(Rect::new(1, 1, 6, 6), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 0.5])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + for layers in [false, true] { + let r = export(&doc, "t.tif", &ExportOptions { tiff_layers: layers, ..ExportOptions::default() }).unwrap(); + let img = photocraft_codecs::decode(&r.bytes).unwrap(); + assert!(img.layout().has_alpha(), "{layers}"); + assert!(img.meta.extra_samples.is_empty(), "{layers}"); + let mut dec = tiff::decoder::Decoder::new(std::io::Cursor::new(r.bytes.as_slice())).unwrap(); + let extra = dec.get_tag_u16_vec(tiff::tags::Tag::ExtraSamples).unwrap(); + assert_eq!(extra, vec![2], "{layers}"); + assert!(!r.warnings.iter().any(|w| w.contains("Save Transparency")), "{layers}: {:?}", r.warnings); + if layers { + assert_photoshop_layered_tiff(&r.bytes, false); + } + } +} + +/// A flat TIFF that also stores an alpha or spot channel mattes the composite on white +/// and drops image transparency. The export says so. +#[test] +fn flat_tiff_warns_when_extra_channels_matte_transparency() { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let mut doc = Document::new("t", Size::new(4, 4), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + // (0, 0) stays transparent. (1, 1) is half-red, so the matte is halfway to white. + art.fill_rect(Rect::new(1, 1, 2, 2), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 0.5])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let mut spot = AlphaChannel::new("Spot Color 1", Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false))); + spot.spot = Some((Color::rgb(0.0, 1.0, 0.0), 1.0)); + doc.channels.push(spot); + + let r = export(&doc, "t.tif", &ExportOptions { tiff_layers: false, ..ExportOptions::default() }).unwrap(); + assert!(r.warnings.iter().any(|w| w.contains("composited over white") && w.contains("Save Transparency")), "{:?}", r.warnings); + let img = photocraft_codecs::decode(&r.bytes).unwrap(); + assert!(!img.layout().has_alpha(), "{:?}", img.layout()); + assert_eq!(img.meta.extra_samples.len(), 1); + let (_spp, _bps, extra, _rows, samples) = independent_samples(&r.bytes); + assert_eq!(extra, vec![0]); + // 4 samples per pixel: RGB + spot. (0, 0) was transparent and is now white. + assert_eq!(&samples[..3], &[255, 255, 255]); + // Half-red over white is about (255, 128, 128). Pixel (1, 1), 4 samples per pixel. + let (x, y, width, spp) = (1usize, 1usize, 4usize, 4usize); + let at = (y * width + x) * spp; + let rgb = &samples[at..at + 3]; + assert!(rgb[0] > 250 && (rgb[1] as i16 - 128).abs() <= 2 && (rgb[2] as i16 - 128).abs() <= 2, "{rgb:?}"); +} + +/// A 4×4 RGB document whose one layer is transparent outside its middle, plus a "White" spot +/// channel with solid ink in its left half: the shape of a UV print's underbase. +fn transparent_art_with_white_spot() -> Document { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let mut doc = Document::new("t", Size::new(4, 4), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + art.fill_rect(Rect::new(1, 1, 3, 3), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + doc.layers.push(Layer::new("Layer 1", LayerContent::Raster(art))); + let gray = PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false); + let mut plate = Surface::new(gray); + plate.fill_rect(Rect::new(0, 0, 2, 4), &photocraft_raster::from_rgba(&gray, [1.0, 1.0, 1.0, 1.0])); + let mut white = AlphaChannel::new("White", plate); + white.spot = Some((Color::rgb(0.8, 0.2, 0.2), 1.0)); + doc.channels.push(white); + doc +} + +/// The first channel's ink at (0, 0), inked, and at (3, 0), bare. +fn ink(d: &Document) -> (f32, f32) { + (d.channels[0].surface.pixel(0, 0)[0], d.channels[0].surface.pixel(3, 0)[0]) +} + +/// `doc` laid out as Photoshop 25.12 saves a TIFF with Save Transparency on: R, G, B, +/// transparency and spot samples, with "Transparency" first in the channel names and display +/// records. With `layers`, the layer data has a positive layer count and an `MTrn` block (the +/// specification's key is `Mtrn`); without, the file is flat. Built from the PSD model of `doc`. +fn photoshop_transparency_and_spot_tiff(doc: &Document, layers: bool) -> Vec { + use photocraft_psd::{Compression, TaggedBlock}; + + let opts = PsdExportOptions { merged_matte: false, merged_compression: Compression::Raw, ..Default::default() }; + let (file, _) = document_to_psd_with(doc, &opts); + let planes = file.decode_merged().unwrap(); + let n = 16; + assert_eq!(planes.len(), 5 * n); + let rgba: Vec = (0..n).flat_map(|i| (0..4).map(move |c| (i, c))).map(|(i, c)| planes[c * n + i]).collect(); + let mut img = photocraft_codecs::Image::from_raw(4, 4, photocraft_codecs::ChannelLayout::Rgba, photocraft_codecs::SampleType::U8, rgba).unwrap(); + img.meta.extra_samples = vec![photocraft_codecs::ExtraSample { kind: 0, data: planes[4 * n..].to_vec() }]; + img.meta.photoshop_resources = Some(photocraft_psd::tiff::resources_to_bytes(&file.resources).unwrap()); + if layers { + let mut isd = ImageSourceData::from_psd(&file); + if let Some(info) = &mut isd.layer_info { + info.merged_alpha = false; + } + isd.global_blocks.insert(0, TaggedBlock::new(*b"MTrn", Vec::new())); + let order = if photocraft_codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; + img.meta.photoshop_layers = Some(isd.to_bytes(order).unwrap().0); + } + photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &photocraft_codecs::EncodeOptions::default()).unwrap() +} + +/// Photoshop's layered TIFF with transparency next to a spot channel opens with the spot as the +/// only channel. A layered re-save leaves the transparency sample out next to the spot, so it +/// must also drop the `MTrn` marker, or the spot would read back as transparency. +#[test] +fn photoshop_mtrn_tiff_opens_without_a_transparency_channel() { + use photocraft_psd::TaggedBlock; + + let bytes = photoshop_transparency_and_spot_tiff(&transparent_art_with_white_spot(), true); + let opened = import("t.tif", &bytes).unwrap().document; + let names: Vec<&str> = opened.channels.iter().map(|c| c.name.as_str()).collect(); + assert_eq!(names, ["White"]); + assert!(opened.channels[0].spot.is_some()); + assert_eq!(ink(&opened), (1.0, 0.0)); + + let r = tiff(&opened, true); + let (isd, _) = ImageSourceData::from_bytes(&tags(&r.bytes).1.unwrap()).unwrap(); + assert!(!isd.global_blocks.iter().any(TaggedBlock::is_merged_transparency_marker)); + let back = import("t.tif", &r.bytes).unwrap().document; + let names: Vec<&str> = back.channels.iter().map(|c| c.name.as_str()).collect(); + assert_eq!(names, ["White"]); + assert_eq!(ink(&back), (1.0, 0.0)); +} + +/// The same Photoshop layout in a flat TIFF (no layer data): the image keeps its transparency, +/// and the spot gets its own name and display record, not the leading "Transparency" ones, so +/// it opens as a spot with its ink the right way round. +#[test] +fn photoshop_flat_tiff_with_transparency_opens_its_spot_as_a_spot() { + let bytes = photoshop_transparency_and_spot_tiff(&transparent_art_with_white_spot(), false); + assert!(tags(&bytes).1.is_none(), "flat: no layer data"); + let opened = import("t.tif", &bytes).unwrap().document; + let names: Vec<&str> = opened.channels.iter().map(|c| c.name.as_str()).collect(); + assert_eq!(names, ["White"]); + assert!(opened.channels[0].spot.is_some(), "a spot, not an alpha channel"); + assert_eq!(ink(&opened), (1.0, 0.0)); + let LayerContent::Raster(s) = &opened.layers[0].content else { panic!("raster") }; + assert_eq!(s.pixel(0, 0)[3], 0.0, "the image keeps its transparency"); +} + +/// A Lab document with a spot channel keeps the spot as a CIELab extra sample, and its +/// transparency is matted on Lab white (L* 100, a* and b* 0), not on the maximum of every sample. +#[test] +fn lab_tiff_keeps_spot_and_mattes_on_lab_white() { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + for sample in [SampleType::U8, SampleType::U16] { + let mut doc = Document::new("t", Size::new(4, 4), ColorMode::Lab, sample); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + // (0, 0) stays transparent. + art.fill_rect(Rect::new(1, 1, 2, 2), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let mut plate = Surface::new(PixelFormat::new(ColorMode::Grayscale, sample, false)); + plate.fill_rect(Rect::new(0, 0, 2, 4), &photocraft_raster::from_rgba(&plate.format(), [1.0, 1.0, 1.0, 1.0])); + let mut spot = AlphaChannel::new("Spot Color 1", plate); + spot.spot = Some((Color::rgb(0.0, 1.0, 0.0), 1.0)); + doc.channels.push(spot); + + // Lab is always saved flat, whether layers were asked for or not. + for layers in [false, true] { + let r = export(&doc, "t.tif", &ExportOptions { tiff_layers: layers, ..ExportOptions::default() }).unwrap(); + let img = photocraft_codecs::decode(&r.bytes).unwrap(); + assert_eq!(img.layout(), photocraft_codecs::ChannelLayout::Lab, "{sample:?} {layers}"); + assert_eq!(img.meta.extra_samples.len(), 1, "{sample:?} {layers}"); + let white = &img.to_normalized()[..3]; + assert!((white[0] - 1.0).abs() < 1e-3 && (white[1] - 128.0 / 255.0).abs() < 1e-3 && (white[2] - 128.0 / 255.0).abs() < 1e-3, "{white:?}"); + let back = import("t.tif", &r.bytes).unwrap().document; + assert_eq!(back.pixel_format().mode, ColorMode::Lab, "{sample:?} {layers}"); + assert_eq!(back.channels.len(), 1, "{sample:?} {layers}"); + assert_eq!(back.channels[0].name, "Spot Color 1"); + assert!(back.channels[0].spot.is_some(), "{sample:?} {layers}"); + let plate = |c: &AlphaChannel| c.surface.to_interleaved(Rect::new(0, 0, 4, 4)); + assert_eq!(plate(&back.channels[0]), plate(&doc.channels[0]), "{sample:?} {layers}"); + } + } +} + +/// Tag 37724 walked as raw bytes (not through [`ImageSourceData`]). +/// +/// Matches a Photoshop 27 layered TIFF: classic 4-byte block lengths, `Layr` then a 14-byte +/// `LMsk`, no `MTrn` (that block is only for a composite that keeps both transparency and a +/// spot). A 4320×4320 RGB spot plate is 74_649_600 bytes and still fits those u32 lengths; +/// this check uses the same layout on a smaller image. +fn assert_photoshop_layered_tiff(bytes: &[u8], with_spot: bool) { + assert!(bytes.starts_with(b"II"), "little-endian TIFF"); + let u16_at = |i: usize| u16::from_le_bytes([bytes[i], bytes[i + 1]]); + let u32_at = |i: usize| u32::from_le_bytes(bytes[i..i + 4].try_into().unwrap()); + let ifd = u32_at(4) as usize; + let n = u16_at(ifd) as usize; + let mut layers = None; + let mut spp = 0u16; + let mut extra = Vec::new(); + for e in 0..n { + let at = ifd + 2 + e * 12; + let tag = u16_at(at); + let typ = u16_at(at + 2); + let count = u32_at(at + 4) as usize; + let val = u32_at(at + 8) as usize; + let unit: usize = match typ { + 3 => 2, + 4 => 4, + _ => 1, + }; + let size = unit.saturating_mul(count); + let inline = at + 8; + let raw = if size <= 4 { &bytes[inline..inline + size] } else { &bytes[val..val + size] }; + match tag { + 277 => spp = u16::from_le_bytes([raw[0], raw[1]]), + 338 => extra = raw.as_chunks::<2>().0.iter().map(|c| u16::from_le_bytes(*c)).collect(), + 37724 => layers = Some(raw), + _ => {} + } + } + let isd = layers.expect("37724"); + let sig = b"Adobe Photoshop Document Data Block\0"; + assert!(isd.starts_with(sig), "PSB-sized lengths"); + let mut pos = sig.len(); + let mut keys = Vec::new(); + let mut layr: Option> = None; + while pos + 12 <= isd.len() { + assert_eq!(&isd[pos..pos + 4], b"MIB8", "block at {pos}"); + let key = [isd[pos + 7], isd[pos + 6], isd[pos + 5], isd[pos + 4]]; + let len = u32::from_le_bytes(isd[pos + 8..pos + 12].try_into().unwrap()) as usize; + let body = &isd[pos + 12..pos + 12 + len]; + if &key == b"Layr" { + layr = Some(body.to_vec()); + } + if &key == b"LMsk" { + assert_eq!(body, &[0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0x32, 0, 0x80, 0]); + } + keys.push(key); + pos += 12 + len; + let rest = &isd[pos..]; + let pad = (0..=3).find(|&k| k == rest.len() || rest.get(k..k + 4) == Some(b"MIB8")).unwrap_or(0); + pos += pad; + } + assert_eq!(pos, isd.len(), "trailing bytes after the last block"); + assert_eq!(keys.first(), Some(b"Layr")); + assert_eq!(keys.get(1), Some(b"LMsk")); + assert!(!keys.iter().any(|k| k == b"MTrn" || k == b"Mtrn")); + let layr = layr.expect("Layr"); + let count = i16::from_le_bytes([layr[0], layr[1]]); + if with_spot { + assert!(count > 0, "layer count {count}"); + assert_eq!((spp, extra.as_slice()), (4, [0u16].as_slice())); + } else { + assert!(count < 0, "layer count {count}"); + assert_eq!(extra, vec![2]); + } + let nch = u16::from_le_bytes([layr[18], layr[19]]) as usize; + let mut ids = Vec::new(); + let mut p = 20; + for _ in 0..nch { + ids.push(i16::from_le_bytes([layr[p], layr[p + 1]])); + p += 6; + } + assert!(ids.contains(&-1), "channel ids {ids:?}"); +} + +/// Photoshop 27's Kato-Karate TIFF stores spot ink inverted: every non-white artwork pixel +/// has spot 0, and the white background is spot 255. An alpha channel is the other way +/// around (255 = selected). Photoshop's PSDs store both the same way, and so does a PSD saved +/// here. Everything round-trips, PSD → TIFF → PSD included. +#[test] +fn spot_ink_is_stored_as_zero_and_alpha_is_not() { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_psd::PsdFile; + use photocraft_raster::Surface; + + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let (w, h) = (8u32, 8u32); + let mut doc = Document::new("pol", Size::new(w, h), ColorMode::Rgb, depth); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + art.fill_rect(Rect::new(2, 2, 6, 6), &photocraft_raster::from_rgba(&fmt, [1.0, 0.2, 0.1, 1.0])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let gray = PixelFormat::new(ColorMode::Grayscale, depth, false); + let n = (w as usize) * (h as usize); + let mut ink = vec![0.0f32; n]; + for y in 2..6 { + for x in 2..6 { + ink[y * w as usize + x] = 1.0; + } + } + let mut spot = AlphaChannel::new("Spot Color 1", Surface::new(gray)); + spot.surface.write_region(Rect::new(0, 0, w as i32, h as i32), &ink); + spot.spot = Some((Color::rgb(1.0, 0.0, 0.0), 0.6)); + doc.channels.push(spot); + let mut alpha = AlphaChannel::new("Alpha 1", Surface::new(gray)); + alpha.surface.write_region(Rect::new(0, 0, w as i32, h as i32), &ink); + doc.channels.push(alpha); + + let psd = export(&doc, "pol.psd", &ExportOptions::default()).unwrap(); + let file = PsdFile::from_bytes(&psd.bytes).unwrap(); + let merged = file.decode_merged().unwrap(); + let bps = depth.bytes(); + let plane = n * bps; + let first = 3 + usize::from(file.merged_has_alpha()); + let spot_plane = &merged[first * plane..(first + 1) * plane]; + let alpha_plane = &merged[(first + 1) * plane..(first + 2) * plane]; + // Background (0,0) and artwork (3,3). Spot: 0 = solid ink, max = paper. + // Alpha: max = selected, 0 = empty. + assert_stored(spot_plane, 0, depth, false, "psd spot background"); + assert_stored(spot_plane, (3 * w as usize + 3) * bps, depth, true, "psd spot art"); + assert_stored(alpha_plane, 0, depth, true, "psd alpha background"); + assert_stored(alpha_plane, (3 * w as usize + 3) * bps, depth, false, "psd alpha art"); + // The merged transparency (0), then the spot and the alpha channel (3, 4). + assert_eq!(file.resource(1053).map(|r| identifiers(&r.data)), Some(vec![0, 3, 4]), "{depth:?}"); + let back = import("pol.psd", &psd.bytes).unwrap().document; + let area = Rect::new(0, 0, w as i32, h as i32); + assert_eq!(back.channels[0].surface.to_interleaved(area), doc.channels[0].surface.to_interleaved(area), "{depth:?} psd spot"); + assert_eq!(back.channels[1].surface.to_interleaved(area), doc.channels[1].surface.to_interleaved(area), "{depth:?} psd alpha"); + assert!(back.channels[0].spot.is_some(), "{depth:?}"); + assert!(back.channels[1].spot.is_none(), "{depth:?}"); + + if depth == SampleType::U8 { + for layers in [false, true] { + // The document as opened from the PSD, so the PSD's own resources come along. + let r = export(&back, "pol.tif", &ExportOptions { tiff_layers: layers, ..ExportOptions::default() }).unwrap(); + let (spp, _bps, extra, _rows, samples) = independent_samples(&r.bytes); + assert_eq!((spp, extra.as_slice()), (5, [0u16, 0].as_slice()), "{layers}"); + // Reference TIFF: artwork spot sample is 0, background spot sample is 255. + assert_eq!(samples[3], 255, "{layers} background spot"); + assert_eq!(samples[(3 * w as usize + 3) * 5 + 3], 0, "{layers} art spot"); + assert_eq!(samples[4], 0, "{layers} background alpha"); + assert_eq!(samples[(3 * w as usize + 3) * 5 + 4], 255, "{layers} art alpha"); + // One identifier per extra sample, numbered from 3 (no transparency sample). + assert_eq!(tiff_alpha_identifiers(&r.bytes), [3, 4], "{layers}"); + let from_tiff = import("pol.tif", &r.bytes).unwrap().document; + assert_eq!(from_tiff.channels[0].surface.to_interleaved(area), doc.channels[0].surface.to_interleaved(area), "{layers}"); + assert_eq!(from_tiff.channels[1].surface.to_interleaved(area), doc.channels[1].surface.to_interleaved(area), "{layers}"); + assert!(from_tiff.channels[0].spot.is_some() && from_tiff.channels[1].spot.is_none(), "{layers}"); + // Saved as PSD again: the first PSD's planes, and one 1053 of its own. The flat TIFF's + // composite was matted on white, so only the layered one brings transparency back. + let psd = PsdFile::from_bytes(&export(&from_tiff, "pol.psd", &ExportOptions::default()).unwrap().bytes).unwrap(); + let again = psd.decode_merged().unwrap(); + assert_eq!(again[again.len() - 2 * plane..], merged[(first * plane)..(first + 2) * plane], "{layers} tiff to psd"); + assert_eq!(psd.merged_has_alpha(), layers, "{layers} tiff to psd"); + let want: Vec = if layers { vec![0, 3, 4] } else { vec![3, 4] }; + assert_eq!(psd.resources.iter().filter(|r| r.id == 1053).map(|r| identifiers(&r.data)).collect::>(), [want], "{layers} tiff to psd"); + let reopened = import("pol.psd", &psd.to_bytes().unwrap()).unwrap().document; + assert_eq!(reopened.channels[0].surface.to_interleaved(area), doc.channels[0].surface.to_interleaved(area), "{layers}"); + assert_eq!(reopened.channels[1].surface.to_interleaved(area), doc.channels[1].surface.to_interleaved(area), "{layers}"); + } + } + } +} + +/// The big-endian u32s of a resource 1053. +fn identifiers(data: &[u8]) -> Vec { + data.as_chunks::<4>().0.iter().map(|c| u32::from_be_bytes(*c)).collect() +} + +/// The identifiers in a TIFF's resource 1053 (none without one). +fn tiff_alpha_identifiers(bytes: &[u8]) -> Vec { + let resources = tags(bytes).0.expect("photoshop resources"); + let (blocks, _) = photocraft_psd::tiff::resources_from_bytes(&resources).unwrap(); + blocks.iter().find(|b| b.id == 1053).map(|b| identifiers(&b.data)).unwrap_or_default() +} + +/// `at` addresses one sample of a big-endian PSD plane. `zero` expects 0; otherwise the +/// maximum sample (255 / 65535 / 1.0). +fn assert_stored(plane: &[u8], at: usize, depth: SampleType, zero: bool, what: &str) { + match depth { + SampleType::U8 => { + let v = plane[at]; + assert_eq!(v, if zero { 0 } else { 255 }, "{what} {v}"); + } + SampleType::U16 => { + let v = u16::from_be_bytes([plane[at], plane[at + 1]]); + assert_eq!(v, if zero { 0 } else { 65535 }, "{what} {v}"); + } + SampleType::F32 => { + let v = f32::from_be_bytes([plane[at], plane[at + 1], plane[at + 2], plane[at + 3]]); + assert_eq!(v, if zero { 0.0 } else { 1.0 }, "{what} {v}"); + } + } +} + +/// Quick Mask stays the last channel after the merged transparency plane is omitted. +#[test] +fn spot_tiff_keeps_quick_mask_when_composite_alpha_is_omitted() { + use photocraft_color::{Color, PixelFormat}; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let mut doc = Document::new("q", Size::new(8, 8), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + art.fill_rect(Rect::new(1, 1, 6, 6), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 0.5])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let gray = PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false); + let area = Rect::new(0, 0, 8, 8); + let mut spot_bytes = vec![9u8; 64]; + spot_bytes[0] = 3; + let mut channel = AlphaChannel::new("Spot Color 1", Surface::from_interleaved(gray, area, &spot_bytes)); + channel.spot = Some((Color::rgb(1.0, 0.0, 0.0), 0.6)); + doc.channels.push(channel); + let mut qbytes = vec![7u8; 64]; + qbytes[1] = 4; + doc.quick_mask = Some(AlphaChannel::new("Quick Mask", Surface::from_interleaved(gray, area, &qbytes))); + + for layers in [false, true] { + let r = export(&doc, "q.tif", &ExportOptions { tiff_layers: layers, ..ExportOptions::default() }).unwrap(); + let (spp, _bps, extra, _rows, samples) = independent_samples(&r.bytes); + assert_eq!((spp, extra.as_slice()), (5, [0, 0].as_slice()), "{layers}"); + let stored_spot: Vec = spot_bytes.iter().map(|b| 255 - b).collect(); + assert_plane(&samples, 5, 3, &stored_spot); + // A Quick Mask is an alpha channel: stored straight, not as spot ink. + assert_plane(&samples, 5, 4, &qbytes); + let back = import("q.tif", &r.bytes).unwrap().document; + assert_eq!(back.channels.len(), 1, "{layers}"); + assert_eq!(back.channels[0].surface.to_interleaved(area), spot_bytes, "{layers}"); + let qm = back.quick_mask.as_ref().unwrap_or_else(|| panic!("{layers}: no quick mask")); + assert_eq!(qm.surface.to_interleaved(area), qbytes, "{layers}"); + } +} + +#[test] +fn default_export_options_write_a_flat_tiff() { + // Scripted and agent saves (CLI, batch, MCP) build on the defaults: layers only on request. + let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + assert!(tiff_layers::would_write_layers(&d)); + let r = export(&d, "x.tif", &ExportOptions::default()).unwrap(); + assert_eq!(tags(&r.bytes), (None, None)); + assert_eq!(import("x.tif", &r.bytes).unwrap().document.layers.len(), 1); +} + +/// #2940: layered TIFF data whose groups nest past the document's cap is rejected with the same +/// limit error as a PSD, instead of being converted. +#[test] +fn layered_tiff_nested_past_the_cap_is_rejected() { + use photocraft_psd::{GroupSpec, LayerSpec, PixelData, PsdBuilder, PsdFile}; + let tiff_nested = |levels: usize| { + let mut b = PsdBuilder::new(4, 4); + for _ in 0..levels { + b.begin_group(GroupSpec::new("g")); + } + b.push_layer(LayerSpec::new("deep", 0, 0, 1, 1, PixelData::Rgba8(vec![1, 2, 3, 4]))); + for _ in 0..levels { + b.end_group().unwrap(); + } + let file = PsdFile::from_bytes(&b.to_bytes().unwrap()).unwrap(); + let order = if photocraft_codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; + let layers = ImageSourceData::from_psd(&file).to_bytes(order).unwrap().0; + let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); + let mut img = photocraft_codecs::decode(&tiff(&d, false).bytes).unwrap(); + img.meta.photoshop_layers = Some(layers); + photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &photocraft_codecs::EncodeOptions::default()).unwrap() + }; + let err = import("deep.tif", &tiff_nested(101)).unwrap_err(); + assert!(err.to_string().contains("nested deeper than 100"), "{err}"); + let r = import("hundred.tif", &tiff_nested(100)).unwrap(); + assert_eq!(r.document.max_group_depth(), 100); +} + +#[test] +fn layered_tiff_writes_photoshops_global_layer_mask() { + // #9095: Photoshop reads only the composite of a layered TIFF whose `LMsk` block is empty + // ("There was a problem reading the layer data"); it writes 14 bytes there itself: overlay + // colour space 0, colour (65535, 0, 0, 0), opacity 50, kind 128, one filler byte. + for depth in [SampleType::U8, SampleType::U16] { + let d = gen_doc(ColorMode::Rgb, depth, Features::PIXELS); + let (_, layers) = tags(&tiff(&d, true).bytes); + let (isd, _) = ImageSourceData::from_bytes(&layers.expect("layer data")).expect("parse layer data"); + let mask = isd.global_layer_mask.expect("LMsk block"); + assert_eq!(mask.data, [0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 50, 128, 0], "{depth:?}"); + } +} + +/// Past MAX_EXTRA_SAMPLES (16), a layered TIFF stores the first 16 extra channels. Its channel +/// resources must describe those 16 and no more: 16 names in 1006 and 1045, 16 DisplayInfo +/// records and 16 identifiers, and no Quick Mask record for a Quick Mask that wasn't stored. +#[test] +fn channels_past_sixteen_leave_no_resources_behind() { + use photocraft_color::PixelFormat; + use photocraft_doc::{AlphaChannel, Layer}; + use photocraft_geom::{Rect, Size}; + use photocraft_raster::Surface; + + let mut doc = Document::new("t", Size::new(8, 8), ColorMode::Rgb, SampleType::U8); + let fmt = doc.pixel_format(); + let mut art = Surface::new(fmt); + art.fill_rect(Rect::new(0, 0, 8, 8), &photocraft_raster::from_rgba(&fmt, [1.0, 0.0, 0.0, 1.0])); + doc.layers.push(Layer::new("Art", LayerContent::Raster(art))); + let gray = PixelFormat::new(ColorMode::Grayscale, SampleType::U8, false); + for i in 1..=18 { + doc.channels.push(AlphaChannel::new(format!("Alpha {i}"), Surface::new(gray))); + } + doc.quick_mask = Some(AlphaChannel::new("Quick Mask", Surface::new(gray))); + + let r = tiff(&doc, true); + assert!(r.warnings.iter().any(|w| w.contains("19 extra channels") && w.contains("first 16")), "{:?}", r.warnings); + let (resources, _) = tags(&r.bytes); + let (resources, _) = photocraft_psd::tiff::resources_from_bytes(&resources.expect("resources")).expect("parse resources"); + let data = |id: u16| resources.iter().find(|r| r.id == id).map(|r| r.data.clone()).unwrap_or_default(); + let pascal = data(1006); + let (mut at, mut names) = (0usize, 0usize); + while let Some(&len) = pascal.get(at) { + at += 1 + usize::from(len); + names += 1; + } + assert_eq!(names, 16, "1006"); + let unicode = data(1045); + let (mut at, mut names) = (0usize, 0usize); + while let Some(b) = unicode.get(at..at + 4) { + at += 4 + 2 * u32::from_be_bytes([b[0], b[1], b[2], b[3]]) as usize; + names += 1; + } + assert_eq!(names, 16, "1045"); + assert_eq!(data(1077).len(), 4 + 16 * 13, "1077"); + assert_eq!(data(1053).len(), 16 * 4, "1053"); + assert!(resources.iter().all(|r| r.id != 1022), "the Quick Mask was not stored, so its record goes"); + + let flat = photocraft_codecs::decode(&r.bytes).expect("decode"); + assert_eq!(flat.meta.extra_samples.len(), 16); + let back = import("x.tif", &r.bytes).expect("import").document; + let names: Vec<&str> = back.channels.iter().map(|c| c.name.as_str()).collect(); + let want: Vec = (1..=16).map(|i| format!("Alpha {i}")).collect(); + assert_eq!(names, want.iter().map(String::as_str).collect::>()); + assert!(back.quick_mask.is_none()); +} + +#[test] +fn damaged_layer_data_opens_flattened_part_0() { + damaged_layer_data_opens_flattened_part(0); +} + +#[test] +fn damaged_layer_data_opens_flattened_part_1() { + damaged_layer_data_opens_flattened_part(1); +} + +#[test] +fn damaged_layer_data_opens_flattened_part_2() { + damaged_layer_data_opens_flattened_part(2); +} + +#[test] +fn damaged_layer_data_opens_flattened_part_3() { + damaged_layer_data_opens_flattened_part(3); +} diff --git a/crates/io/tests/tiff_pages.rs b/crates/io/tests/it/tiff_pages.rs similarity index 100% rename from crates/io/tests/tiff_pages.rs rename to crates/io/tests/it/tiff_pages.rs diff --git a/crates/io/tests/it/tiff_type_geometry.rs b/crates/io/tests/it/tiff_type_geometry.rs new file mode 100644 index 0000000000..326ec35cee --- /dev/null +++ b/crates/io/tests/it/tiff_type_geometry.rs @@ -0,0 +1,134 @@ +//! Photoshop's mixed-order Intel TIFF type blocks and unsafe/unresolved type fallback (#4758). + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::text::{CharStyle, TextRun}; +use photocraft_doc::{Document, Layer, LayerContent, Size, TextLayer}; +use photocraft_geom::Affine; +use photocraft_io::{ExportOptions, PsdExportOptions, document_to_psd_with, export, import, psd_to_document}; +use photocraft_psd::descriptor::{Value, VersionedDescriptor}; + +fn document() -> Document { + document_in(ColorMode::Rgb, SampleType::U8) +} + +fn document_in(mode: ColorMode, depth: SampleType) -> Document { + let mut doc = Document::new("Synthetic type", Size::new(640, 400), mode, depth); + doc.resolution_dpi = 300.0; + let mut t = TextLayer { + text: "Synthetic".into(), + transform: Affine { m: [1.8054202887117468, 0.0, -0.143, 1.8063853899082987, 40.0, 120.0] }, + ..Default::default() + }; + t.runs = vec![TextRun { len: t.text.len(), style: CharStyle { size_pt: 24.0, ..Default::default() } }]; + t.sync_summary(); + t.cache = Some(photocraft_text::TextEngine::default().render(&t, doc.resolution_dpi, doc.pixel_format()).1.surface); + doc.layers.push(Layer::new("Synthetic", LayerContent::Text(t))); + doc +} + +#[test] +fn photoshop_mixed_order_tiff_import_keeps_editable_geometry_and_pixels() { + let doc = document(); + let (psd, _) = document_to_psd_with(&doc, &PsdExportOptions::default()); + let canonical = &psd.layers()[0].block(b"TySh").unwrap().data; + let encoded = export(&doc, "test.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap(); + let mut image = photocraft_codecs::decode(&encoded.bytes).unwrap(); + let blocks = image.meta.photoshop_layers.as_mut().unwrap(); + let at = blocks.windows(8).position(|b| b == b"MIB8hSyT").unwrap() + 12; + // Only the text version/descriptor switch order; warp and bounds remain little-endian. + let (_, used) = photocraft_psd::descriptor::VersionedDescriptor::parse_prefix(&canonical[52..]).unwrap(); + blocks[at + 50..at + 52 + used].copy_from_slice(&canonical[50..52 + used]); + let bytes = photocraft_codecs::encode(&image, photocraft_codecs::Format::Tiff, &Default::default()).unwrap(); + let opened = import("photoshop.tif", &bytes).unwrap(); + assert!(!opened.warnings.iter().any(|w| w.contains("TySh")), "{:?}", opened.warnings); + let LayerContent::Text(before) = &doc.layers[0].content else { panic!("source type") }; + let LayerContent::Text(after) = &opened.document.layers[0].content else { panic!("imported type") }; + assert_eq!(after.transform, before.transform); + assert_eq!(after.size_pt, 24.0); + assert_eq!(after.text, before.text); + assert_eq!(after.cache.as_ref().unwrap().content_bounds(), before.cache.as_ref().unwrap().content_bounds()); + let rerendered = photocraft_text::TextEngine::default().render(after, opened.document.resolution_dpi, opened.document.pixel_format()).1.surface; + assert_eq!(rerendered.content_bounds(), after.cache.as_ref().unwrap().content_bounds()); +} + +#[test] +fn layered_tiff_export_keeps_tysh_text_descriptor_big_endian() { + for (mode, depth) in [ + (ColorMode::Rgb, SampleType::U8), + (ColorMode::Rgb, SampleType::U16), + (ColorMode::Rgb, SampleType::F32), + (ColorMode::Grayscale, SampleType::U8), + (ColorMode::Grayscale, SampleType::U16), + (ColorMode::Grayscale, SampleType::F32), + (ColorMode::Cmyk, SampleType::U8), + (ColorMode::Cmyk, SampleType::U16), + ] { + let doc = document_in(mode, depth); + let (psd, _) = document_to_psd_with(&doc, &PsdExportOptions::default()); + let canonical = &psd.layers()[0].block(b"TySh").unwrap().data; + let encoded = export(&doc, "test.tif", &ExportOptions { tiff_layers: true, ..Default::default() }).unwrap(); + let (_, layers) = photocraft_codecs::tiff_photoshop_tags(&encoded.bytes); + let layers = layers.expect("layered TIFF tag 37724"); + let block_at = layers.windows(8).position(|window| window == b"MIB8hSyT").expect("little-endian TySh block"); + let length_at = block_at + 8; + let length_bytes: [u8; 4] = layers.get(length_at..length_at + 4).expect("TySh length").try_into().expect("four-byte length"); + let length = usize::try_from(u32::from_le_bytes(length_bytes)).expect("test block length fits usize"); + let data_at = length_at + 4; + let payload = layers.get(data_at..data_at + length).expect("TySh payload"); + let (_, descriptor_len) = VersionedDescriptor::parse_prefix(canonical.get(52..).unwrap()).unwrap(); + let text_section_len = 2 + descriptor_len; + assert_eq!(payload.get(50..52), Some(&50u16.to_be_bytes()[..]), "{mode:?} {depth:?}"); + assert_eq!(payload.get(52..56), Some(&16u32.to_be_bytes()[..]), "{mode:?} {depth:?}"); + assert_eq!(payload.get(50..50 + text_section_len), canonical.get(50..50 + text_section_len), "{mode:?} {depth:?}"); + let descriptor = VersionedDescriptor::parse_prefix(payload.get(52..).unwrap()).unwrap().0; + assert!(matches!(descriptor.descriptor.get("Txt "), Some(Value::Text(value)) if value.to_string_lossy() == doc.layers[0].name)); + + let opened = import("photoshop.tif", &encoded.bytes).unwrap(); + assert!(!opened.warnings.iter().any(|warning| warning.contains("TySh")), "{:?}", opened.warnings); + let LayerContent::Text(before) = &doc.layers[0].content else { panic!("source type") }; + let LayerContent::Text(after) = &opened.document.layers[0].content else { panic!("imported type") }; + assert_eq!(after.transform, before.transform, "{mode:?} {depth:?}"); + assert_eq!(after.size_pt, before.size_pt, "{mode:?} {depth:?}"); + assert_eq!(after.text, before.text, "{mode:?} {depth:?}"); + assert_eq!(after.cache.as_ref().unwrap().content_bounds(), before.cache.as_ref().unwrap().content_bounds(), "{mode:?} {depth:?}"); + } +} + +#[test] +fn foreign_or_unsafe_type_keeps_cached_pixels_without_editable_geometry() { + let doc = document(); + for foreign in [true, false] { + let (mut psd, _) = document_to_psd_with(&doc, &PsdExportOptions::default()); + let block = psd.layer_info.as_mut().unwrap().layers[0].blocks.iter_mut().find(|b| b.key == *b"TySh").unwrap(); + if foreign { + // A foreign-order marker must be respected even if the descriptor could parse. + block.signature = *b"MIB8"; + } else { + block.data[2..10].copy_from_slice(&1.315776645269349e62f64.to_be_bytes()); + } + let raw = block.data.clone(); + let (opened, warnings) = psd_to_document(&psd); + let LayerContent::Raster(pixels) = &opened.layers[0].content else { panic!("unsafe type must stay pixels") }; + assert!(!pixels.content_bounds().is_empty()); + assert_eq!(pixels.content_bounds(), doc.layers[0].surface().unwrap().content_bounds()); + assert!(warnings.iter().any(|w| w.contains("unsafe type geometry")), "{warnings:?}"); + assert!(opened.layers[0].psd_blocks.iter().any(|(k, data)| k == b"TySh" && **data == raw)); + } +} + +#[test] +fn canonical_type_the_typed_reader_rejects_stays_type_and_saves_its_block() { + let doc = document(); + let (mut psd, _) = document_to_psd_with(&doc, &PsdExportOptions::default()); + let block = psd.layer_info.as_mut().unwrap().layers[0].blocks.iter_mut().find(|b| b.key == *b"TySh").unwrap(); + // A text descriptor version the typed reader doesn't know; the matrix is ordinary. + block.data[52..56].copy_from_slice(&17u32.to_be_bytes()); + let raw = block.data.clone(); + let (opened, warnings) = psd_to_document(&psd); + assert!(!warnings.iter().any(|w| w.contains("unsafe type geometry")), "{warnings:?}"); + let LayerContent::Text(t) = &opened.layers[0].content else { panic!("canonical type must stay a type layer") }; + let LayerContent::Text(before) = &doc.layers[0].content else { panic!("source type") }; + assert_eq!(t.transform, before.transform); + let (saved, _) = document_to_psd_with(&opened, &PsdExportOptions::default()); + assert_eq!(saved.layers()[0].block(b"TySh").map(|b| &b.data), Some(&raw), "PSD save must keep the type block"); +} diff --git a/crates/io/tests/vector.rs b/crates/io/tests/it/vector.rs similarity index 74% rename from crates/io/tests/vector.rs rename to crates/io/tests/it/vector.rs index 594a1827e8..71bf66823c 100644 --- a/crates/io/tests/vector.rs +++ b/crates/io/tests/it/vector.rs @@ -135,6 +135,37 @@ fn shape_layer_typed_roundtrip() { } } +/// #6180: Appearance fill and stroke alphas survive PSD save/load, and a redraw +/// (move/crop) uses those values instead of snapping to opaque. +#[test] +fn rectangle_appearance_alphas_survive_psd_and_redraw() { + let mut d = doc(); + let sh = ShapeLayer { + path: photocraft_vector::shapes::rect(16.0, 16.0, 128.0, 96.0), + fill: Some(Fill::Solid(Color::rgba(1.0, 0.0, 0.0, 0.5))), + stroke: Some(ShapeStroke { width: 4.0, paint: Fill::Solid(Color::rgba(0.0, 0.0, 1.0, 0.4)), opacity: 0.5, ..ShapeStroke::default() }), + live: Some(LiveShape::Rect { rect: [16.0, 16.0, 128.0, 96.0], radii: [0.0; 4] }), + ..Default::default() + }; + d.layers.push(shape_layer(sh)); + let b = roundtrip(&d); + let LayerContent::Shape(s) = &b.layers[1].content else { panic!("not a shape") }; + let Fill::Solid(fill) = s.fill.as_ref().unwrap() else { panic!("solid fill") }; + assert!((fill.alpha - 0.5).abs() < 1e-4, "fill appearance alpha {}", fill.alpha); + let st = s.stroke.as_ref().unwrap(); + let Fill::Solid(stroke_c) = &st.paint else { panic!("solid stroke") }; + assert!((stroke_c.alpha - 0.4).abs() < 1e-4, "stroke colour alpha {}", stroke_c.alpha); + assert!((st.opacity - 0.5).abs() < 1e-4, "stroke opacity {}", st.opacity); + let mut redrawn = s.clone(); + redrawn.cache = Some(photocraft_vector::render_shape(&redrawn, photocraft_color::PixelFormat::RGBA8, Rect::new(0, 0, 256, 256))); + let Fill::Solid(fill2) = redrawn.fill.as_ref().unwrap() else { panic!("solid fill") }; + assert!((fill2.alpha - 0.5).abs() < 1e-4); + let st2 = redrawn.stroke.as_ref().unwrap(); + let Fill::Solid(stroke2) = &st2.paint else { panic!("solid stroke") }; + assert!((stroke2.alpha - 0.4).abs() < 1e-4); + assert!((st2.opacity - 0.5).abs() < 1e-4); +} + #[test] fn custom_stroke_styles_roundtrip_through_psd_vstk() { for align in [StrokeAlign::Inside, StrokeAlign::Center, StrokeAlign::Outside] { @@ -171,6 +202,51 @@ fn custom_stroke_styles_roundtrip_through_psd_vstk() { } } +/// A shape layer's Density and Feather (#5846) live in the PSD mask parameters, as Photoshop +/// writes a shape's vector-mask density and feather; with and without a vector stroke, at 8 and +/// 16 bits, the shape comes back a shape with both and renders its soft fill again. +#[test] +fn shape_density_and_feather_roundtrip_through_mask_parameters() { + let square = Path::new(vec![Subpath::polygon(&[(64.0, 64.0), (192.0, 64.0), (192.0, 192.0), (64.0, 192.0)])]); + for depth in [SampleType::U8, SampleType::U16] { + for stroke in [None, Some(ShapeStroke { width: 2.0, ..ShapeStroke::default() })] { + let mut d = Document::with_background("v", Size::new(256, 256), ColorMode::Rgb, depth, Color::WHITE); + let mut sh = ShapeLayer { + path: square.clone(), + fill: Some(Fill::Solid(Color::BLACK)), + stroke: stroke.clone(), + density: 0.5, + feather: 6.0, + ..ShapeLayer::default() + }; + sh.cache = Some(photocraft_vector::render_shape(&sh, d.pixel_format(), d.bounds())); + d.layers.push(Layer::new("Shape", LayerContent::Shape(sh))); + let bytes = export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes; + let file = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); + let rec = file.layers().last().unwrap(); + let p = rec.layer_mask().and_then(|m| m.parameters).expect("mask parameters"); + assert_eq!((p.vector_density, p.vector_feather), (Some(128), Some(6.0)), "{depth:?}"); + assert!(rec.channel(-2).is_none(), "no user mask channel"); + let b = import("x.psd", &bytes).unwrap().document; + let LayerContent::Shape(back) = &b.layers[1].content else { panic!("{depth:?} {stroke:?}: {}", b.layers[1].content.kind_name()) }; + assert_eq!((back.density, back.feather), (128.0 / 255.0, 6.0), "{depth:?} {stroke:?}"); + assert_eq!(back.stroke, stroke); + assert!(b.layers[1].mask.is_none() && b.layers[1].vector_mask.is_none()); + let f = photocraft_compose::flatten(&b); + assert!(f.get(128, 128)[0] < 0.01, "inside: black"); + assert!((f.get(4, 4)[0] - 127.0 / 255.0).abs() < 0.01, "{depth:?}: beyond the path at half density: {:?}", f.get(4, 4)); + } + } + // Defaults write no parameters. + let mut d = doc(); + d.layers.push(shape_layer(ShapeLayer { path: square, fill: Some(Fill::Solid(Color::BLACK)), ..ShapeLayer::default() })); + let bytes = export(&d, "x.psd", &ExportOptions::default()).unwrap().bytes; + let file = photocraft_psd::PsdFile::from_bytes(&bytes).unwrap(); + assert!(file.layers().last().unwrap().layer_mask().and_then(|m| m.parameters).is_none()); + let LayerContent::Shape(back) = &import("x.psd", &bytes).unwrap().document.layers[1].content else { panic!("a shape") }; + assert_eq!((back.density, back.feather), (1.0, 0.0)); +} + #[test] fn vector_mask_roundtrip_and_removal() { let mut d = doc(); diff --git a/crates/io/tests/it/xcf.rs b/crates/io/tests/it/xcf.rs new file mode 100644 index 0000000000..5c2399c3ee --- /dev/null +++ b/crates/io/tests/it/xcf.rs @@ -0,0 +1,347 @@ +//! GIMP XCF files written by GIMP 3.2.6 itself (feature `corpus`; `cargo xtask test-corpus -p io`). +//! They come from `corpus/gimp/` (`cargo xtask corpus --gimp`; pin `PHOTOCRAFT_CORPUS_GIMP_COMMIT` in +//! `xtask/src/corpus_pins.rs`, generator `tools/gimp-xcf/xcf_fixtures.py` in photocraft-corpus); a +//! missing corpus fails. What the reader gets out is checked against the procedural images the +//! generator drew (`pattern`, `alpha`), and our composite of each file against GIMP's own merged +//! rendering saved next to it (`.png`). + +#![cfg(feature = "corpus")] + +use photocraft_color::{BlendMode, ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent}; +use photocraft_geom::Rect; +use photocraft_io::{ExportOptions, export, import}; + +fn fixture(name: &str) -> Vec { + let p = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/gimp").join(name); + std::fs::read(&p).unwrap_or_else(|e| panic!("{}: {e}: run `cargo xtask corpus --gimp`", p.display())) +} + +fn open(name: &str) -> photocraft_io::ImportResult { + import(name, &fixture(name)).unwrap_or_else(|e| panic!("{name}: {e}")) +} + +const W: u32 = 40; +const H: u32 = 30; + +fn pattern(x: u32, y: u32, c: u32, maxval: u32) -> u32 { + (x * 31 + y * 17 + c * 101 + 7) * 13 % (maxval + 1) +} + +fn alpha(x: u32, y: u32, w: u32, h: u32, maxval: u32) -> u32 { + ((x + y) * maxval / (w + h - 4).max(1)).min(maxval) +} + +fn raster(layer: &Layer) -> &photocraft_raster::Surface { + match &layer.content { + LayerContent::Raster(s) => s, + _ => panic!("{} is not a pixel layer", layer.name), + } +} + +fn px(layer: &Layer, x: i32, y: i32) -> Vec { + raster(layer).read_region(Rect::new(x, y, x + 1, y + 1)) +} + +fn names(layers: &[Layer]) -> Vec<&str> { + layers.iter().map(|l| l.name.as_str()).collect() +} + +/// Largest premultiplied channel difference between our composite and GIMP's merged PNG, and +/// the share of pixels over `tolerance`. +fn composite_error(doc: &Document, png: &str, tolerance: f32) -> (f32, f32, String) { + let oracle = import(png, &fixture(png)).unwrap().document; + assert_eq!(doc.size, oracle.size, "{png}"); + let ours = photocraft_compose::flatten(doc); + let theirs = photocraft_compose::flatten(&oracle); + let w = doc.size.width as usize; + let mut worst = (0.0f32, String::new()); + let mut over = 0usize; + for (i, (a, b)) in ours.px.iter().zip(&theirs.px).enumerate() { + let e = (0..4).map(|c| if c == 3 { (a[3] - b[3]).abs() } else { (a[c] * a[3] - b[c] * b[3]).abs() }).fold(0.0f32, f32::max); + over += usize::from(e > tolerance); + if e > worst.0 { + let q = |p: &[f32; 4]| p.map(|v| (v * 255.0).round() as i32); + worst = (e, format!("at ({}, {}): ours {:?}, GIMP {:?}", i % w, i / w, q(a), q(b))); + } + } + (worst.0, over as f32 / ours.px.len().max(1) as f32, worst.1) +} + +#[test] +fn layers_8bit_open_with_their_properties_and_pixels() { + let r = open("layers-8bit.xcf"); + let d = &r.document; + assert_eq!((d.size.width, d.size.height, d.mode, d.depth), (W, H, ColorMode::Rgb, SampleType::U8)); + assert_eq!(names(&d.layers), ["Background", "Multiply 60%", "Screen off-canvas", "Hidden"]); + assert!((d.resolution_dpi - 300.0).abs() < 0.01); + assert_eq!((d.guides.horizontal.as_slice(), d.guides.vertical.as_slice()), (&[10.0][..], &[25.0][..])); + assert!(r.source_read_only); + // The background: opaque, every sample as drawn. + let bg = &d.layers[0]; + assert!(bg.locks.transparency, "an opaque XCF layer opens as a locked-transparency layer"); + for (x, y) in [(0, 0), (39, 29), (17, 11)] { + let p = px(bg, x as i32, y as i32); + for c in 0..3 { + assert!((p[c] * 255.0 - pattern(x, y, c as u32, 255) as f32).abs() < 0.51, "background at {x},{y} channel {c}: {p:?}"); + } + assert_eq!(p[3], 1.0); + } + // Multiply at 60%, offset (5, 4), lock alpha, green tag. + let m = &d.layers[1]; + assert_eq!((m.blend, m.opacity, m.visible, m.locks.transparency, m.label), (BlendMode::Multiply, 0.6, true, true, photocraft_doc::LabelColor::Green)); + let p = px(m, 5, 4); + assert!( + (p[0] * 255.0 - pattern(3, 0, 0, 255) as f32).abs() < 0.51 && p[3] == 0.0, + "top-left of the layer is transparent (alpha gradient starts at 0): {p:?}" + ); + let p = px(m, 24, 19); + assert!((p[0] * 255.0 - pattern(19 + 3, 15, 0, 255) as f32).abs() < 0.51 && (p[3] * 255.0 - alpha(19, 15, 20, 16, 255) as f32).abs() < 0.51, "{p:?}"); + assert_eq!(px(m, 4, 4)[3], 0.0, "outside the layer"); + // Screen, partly off-canvas at (-8, 20): its pixels at x < 0 exist. + let s = &d.layers[2]; + assert_eq!(s.blend, BlendMode::Screen); + assert!((px(s, -8, 20)[0] * 255.0 - pattern(7, 0, 0, 255) as f32).abs() < 0.51); + assert!((px(s, 15, 31)[3] * 255.0 - alpha(23, 11, 24, 12, 255) as f32).abs() < 0.51); + // Hidden. + assert!(!d.layers[3].visible); + // The only note: GIMP composites this file in linear light. + assert_eq!(r.warnings.len(), 1, "{:?}", r.warnings); + assert!(r.warnings[0].contains("linear light"), "{:?}", r.warnings); +} + +/// Per mode: our composite of the `legacy-modes-perceptual.xcf` cell against GIMP's. Modes with +/// a Photoshop equivalent must match; the approximated ones are reported. +#[test] +fn each_mode_against_gimp_in_gamma_space() { + let d = open("legacy-modes-perceptual.xcf").document; + let oracle = import("legacy-modes-perceptual.png", &fixture("legacy-modes-perceptual.png")).unwrap().document; + let ours = photocraft_compose::flatten(&d); + let theirs = photocraft_compose::flatten(&oracle); + let w = d.size.width as usize; + // Where the layer pixel is fully opaque the formulas must agree. Where it is translucent, + // GIMP applies the pixel's alpha before clamping the result of a mode that can leave 0..1 + // (Addition, Subtract, Divide, Dodge, Burn, Linear light) and Photoshop clamps first, so + // those pixels are only reported. + let exact = ["Addition", "Subtract", "Divide", "Dodge", "Burn", "Hard light", "Exclusion", "Linear light"]; + let mut report = Vec::new(); + let mut failed = false; + for (i, layer) in d.layers.iter().skip(1).enumerate() { + let (x0, y0) = ((i % 5) * 8, (i / 5) * 10); + let (mut worst_opaque, mut worst_all) = (0.0f32, 0.0f32); + for y in y0..y0 + 8 { + for x in x0..x0 + 8 { + let (a, b) = (ours.px[y * w + x], theirs.px[y * w + x]); + let e = (0..3).map(|c| (a[c] - b[c]).abs()).fold(0.0, f32::max); + worst_all = worst_all.max(e); + let opaque = alpha((x - x0) as u32, (y - y0) as u32, 8, 8, 255) == 255; + if opaque { + worst_opaque = worst_opaque.max(e); + } + } + } + let must = exact.contains(&layer.name.as_str()); + let ok = !must || worst_opaque <= 2.5 / 255.0; + failed |= !ok; + report.push(format!( + "{} {:<14} {:?}: opaque pixels worst {:.1}/255, all {:.1}/255", + if ok { "ok " } else { "FAIL" }, + layer.name, + layer.blend, + worst_opaque * 255.0, + worst_all * 255.0 + )); + } + println!("{}", report.join("\n")); + assert!(!failed, "\n{}", report.join("\n")); +} + +#[test] +fn groups_nest_and_pass_through() { + let r = open("groups.xcf"); + let d = &r.document; + assert_eq!(names(&d.layers), ["Background", "Group", "Top"]); + let g = &d.layers[1]; + assert!(g.is_group()); + assert!((g.opacity - 0.7).abs() < 1e-6); + assert_eq!(g.blend, BlendMode::Normal, "a GIMP group in Normal mode composites as a unit"); + let children = g.children().unwrap(); + assert_eq!(names(children), ["Child Multiply", "Nested pass-through"]); + assert_eq!(children[0].blend, BlendMode::Multiply); + let nested = &children[1]; + assert!(nested.is_group()); + assert_eq!(nested.blend, BlendMode::PassThrough); + assert_eq!(names(nested.children().unwrap()), ["Nested child Screen"]); + assert_eq!(nested.children().unwrap()[0].blend, BlendMode::Screen); + assert!((px(&nested.children().unwrap()[0], 18, 10)[0] * 255.0 - pattern(5, 0, 0, 255) as f32).abs() < 0.51, "nested child at its offset"); +} + +#[test] +fn masks_open_applied_or_disabled() { + let d = open("mask.xcf").document; + assert_eq!(names(&d.layers), ["Background", "Masked", "Mask disabled"]); + let masked = d.layers[1].mask.as_ref().expect("mask"); + assert!(masked.enabled); + // A left-to-right gradient across 30 px at the layer's offset (5, 5). The script drew it as + // gamma-encoded values and GIMP stores mask coverage linearly, so the midpoint is where + // sRGB 15/29 lands in linear light. + let v = |x: i32| masked.surface.read_region(Rect::new(x, 10, x + 1, 11))[0]; + let linear = |s: f32| if s <= 0.04045 { s / 12.92 } else { ((s + 0.055) / 1.055).powf(2.4) }; + assert!(v(5) < 0.01 && (v(5 + 29) - 1.0).abs() < 0.01 && (v(5 + 15) - linear(15.0 / 29.0)).abs() < 0.01, "{} {} {}", v(5), v(34), v(20)); + let disabled = d.layers[2].mask.as_ref().expect("mask"); + assert!(!disabled.enabled); + assert!(disabled.surface.read_region(Rect::new(20, 20, 21, 21))[0] < 0.01, "a black mask, kept but not applied"); +} + +#[test] +fn gray16_keeps_its_depth_and_mode() { + let d = open("gray16.xcf").document; + assert_eq!((d.mode, d.depth), (ColorMode::Grayscale, SampleType::U16)); + assert_eq!(names(&d.layers), ["Background", "Gray alpha"]); + let bg = &d.layers[0]; + assert_eq!(raster(bg).channels(), 2); + for (x, y) in [(0, 0), (39, 29), (13, 7)] { + let p = px(bg, x as i32, y as i32); + assert!((p[0] * 65535.0 - pattern(x, y, 0, 65535) as f32).abs() < 0.6, "gray at {x},{y}: {p:?}"); + } + let top = &d.layers[1]; + let p = px(top, 10 + 19, 8 + 14); + assert!((p[0] * 65535.0 - pattern(19 + 9, 14, 0, 65535) as f32).abs() < 0.6 && (p[1] * 65535.0 - alpha(19, 14, 20, 15, 65535) as f32).abs() < 0.6, "{p:?}"); +} + +#[test] +fn float_linear_opens_as_32_bit_float_with_a_linear_profile() { + let d = open("float-linear.xcf").document; + assert_eq!((d.mode, d.depth), (ColorMode::Rgb, SampleType::F32)); + let icc = d.icc_profile.as_ref().expect("a linear-light profile"); + assert_eq!(**icc, photocraft_cms::Builtin::LinearSrgb.profile().to_bytes().as_ref().clone()); + let bg = &d.layers[0]; + for (x, y) in [(0, 0), (39, 29), (5, 21)] { + let p = px(bg, x as i32, y as i32); + for (c, v) in p.iter().take(3).enumerate() { + assert_eq!(*v, (x * 3 + y * 5 + c as u32 * 7) as f32 / 64.0, "float at {x},{y} channel {c}"); + } + } + assert!(px(bg, 39, 29)[0] > 1.0, "values above 1 are kept"); + let top = &d.layers[1]; + let p = px(top, 12 + 4, 7 + 6); + assert_eq!(p[3], 10.0 / 30.0); +} + +#[test] +fn indexed_opens_as_rgb_with_a_note() { + let r = open("indexed.xcf"); + assert_eq!((r.document.mode, r.document.depth), (ColorMode::Rgb, SampleType::U8)); + assert!(r.warnings.iter().any(|w| w.contains("indexed")), "{:?}", r.warnings); + assert_eq!(names(&r.document.layers), ["Background", "Top"]); +} + +#[test] +fn text_layers_open_as_pixels_with_a_note() { + let r = open("text.xcf"); + assert_eq!(names(&r.document.layers), ["Background", "XCF"]); + assert!(r.warnings.iter().any(|w| w.contains("text layer")), "{:?}", r.warnings); + let text = &r.document.layers[1]; + let s = raster(text); + let region = s.read_region(Rect::new(10, 10, 10 + 60, 10 + 30)); + assert!(region.as_chunks::<4>().0.iter().any(|p| p[3] > 0.5), "the rendered glyphs are there"); +} + +#[test] +fn channels_and_selection_come_through() { + let d = open("channel.xcf").document; + assert_eq!(d.channels.len(), 1); + assert_eq!(d.channels[0].name, "Alpha 1"); + let v = |y: i32| d.channels[0].surface.read_region(Rect::new(3, y, 4, y + 1))[0]; + assert!(v(0) < 0.01 && (v(29) - 1.0).abs() < 0.01, "a top-to-bottom gradient"); + let sel = d.selection.as_ref().expect("the saved selection"); + assert!((sel.read_region(Rect::new(10, 10, 11, 11))[0] - 1.0).abs() < 0.01 && sel.read_region(Rect::new(1, 1, 2, 2))[0] < 0.01); +} + +#[test] +fn modes_map_to_photoshops_and_the_rest_warn() { + let r = open("legacy-modes.xcf"); + let by_name = |n: &str| r.document.layers.iter().find(|l| l.name == n).unwrap_or_else(|| panic!("{n}")); + assert_eq!(by_name("Addition").blend, BlendMode::LinearDodge); + assert_eq!(by_name("Subtract").blend, BlendMode::Subtract); + assert_eq!(by_name("Divide").blend, BlendMode::Divide); + assert_eq!(by_name("Dodge").blend, BlendMode::ColorDodge); + assert_eq!(by_name("Burn").blend, BlendMode::ColorBurn); + assert_eq!(by_name("Hard light").blend, BlendMode::HardLight); + assert_eq!(by_name("Exclusion").blend, BlendMode::Exclusion); + assert_eq!(by_name("Linear light").blend, BlendMode::LinearLight); + assert_eq!(by_name("Grain merge").blend, BlendMode::Normal); + assert_eq!(by_name("HSV Hue").blend, BlendMode::Hue); + assert_eq!(by_name("LCH Color").blend, BlendMode::Color); + let w = r.warnings.join("\n"); + assert!(w.contains("Grain merge") && w.contains("HSV Value") && w.contains("LCH Color"), "{w}"); +} + +#[test] +fn xcf_cannot_be_written_and_says_so() { + let d = open("layers-8bit.xcf").document; + let e = export(&d, "out.xcf", &ExportOptions::default()).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(e.contains("pcraft"), "{e}"); +} + +#[test] +fn broken_files_are_errors_not_crashes() { + let bytes = fixture("groups.xcf"); + assert!(import("x.xcf", b"gimp xcf v011\0").is_err()); + assert!(import("x.xcf", &bytes[..bytes.len() / 2]).is_err()); + for cut in (0..bytes.len()).step_by(7) { + let _ = import("x.xcf", &bytes[..cut]); + } +} + +/// Our composite against GIMP's merged rendering. The `-perceptual` files have every layer set +/// to blend and composite in gamma-encoded RGB, which is how PhotoCraft composes, so they must +/// match tightly. GIMP's defaults compose in linear light, so those files measure the known gap +/// (the tolerance and the allowed share of pixels over it are the numbers seen, not goals). +/// Per file: tolerance (premultiplied, 0..1), the share of pixels allowed over it, and why. +const COMPOSITES: &[(&str, f32, f32, &str)] = &[ + ("layers-8bit-perceptual.xcf", 2.5 / 255.0, 0.0, "Normal, Multiply and Screen in gamma space"), + ( + "groups-perceptual.xcf", + 2.5 / 255.0, + 0.16, + "groups and pass-through in gamma space; the 15% over are where GIMP's Clip to backdrop hides the Multiply and Screen children above the transparent part of the background", + ), + ("mask-perceptual.xcf", 2.5 / 255.0, 0.0, "masks in gamma space"), + ("text-perceptual.xcf", 2.5 / 255.0, 0.0, "antialiased text in gamma space"), + ( + "legacy-modes-perceptual.xcf", + 2.5 / 255.0, + 0.40, + "modes without an equivalent open as Normal and HSV/LCH modes are approximated; see each_mode_against_gimp_in_gamma_space", + ), + ("gray16.xcf", 2.0 / 255.0, 0.0, "16-bit gray, Normal"), + ("indexed.xcf", 2.0 / 255.0, 0.0, "indexed to RGB"), + ("channel.xcf", 2.0 / 255.0, 0.0, "channels are not rendered"), + ("layers-8bit.xcf", 40.0 / 255.0, 0.0, "GIMP's default: Multiply blended in linear light"), + ("groups.xcf", 140.0 / 255.0, 0.0, "GIMP's default: linear light and Clip to backdrop over a transparent background"), + ("mask.xcf", 60.0 / 255.0, 0.0, "GIMP's default: alpha composited in linear light"), + ("text.xcf", 75.0 / 255.0, 0.0, "GIMP's default: antialiased edges composited in linear light"), +]; + +#[test] +fn composites_match_gimps_merged_rendering() { + let mut report = Vec::new(); + let mut failed = false; + for (name, tolerance, allowed, why) in COMPOSITES { + let d = open(name).document; + let png = name.replace(".xcf", ".png"); + let (worst, over, at) = composite_error(&d, &png, *tolerance); + let ok = over <= *allowed; + failed |= !ok; + report.push(format!( + "{} {name}: worst {:.1}/255, {:.1}% over {:.1}/255 ({why}) {at}", + if ok { "ok " } else { "FAIL" }, + worst * 255.0, + over * 100.0, + tolerance * 255.0 + )); + } + println!("{}", report.join("\n")); + assert!(!failed, "\n{}", report.join("\n")); +} diff --git a/crates/io/tests/xmp_export.rs b/crates/io/tests/it/xmp_export.rs similarity index 98% rename from crates/io/tests/xmp_export.rs rename to crates/io/tests/it/xmp_export.rs index 8d22017ff6..08af0006c7 100644 --- a/crates/io/tests/xmp_export.rs +++ b/crates/io/tests/it/xmp_export.rs @@ -17,7 +17,7 @@ fn doc_with_xmp() -> Document { let mut s = Surface::new(d.pixel_format()); s.fill_rect(photocraft_geom::Rect::new(0, 0, 8, 6), &[0.5, 0.4, 0.3, 1.0]); d.layers.push(Layer::new("flat", LayerContent::Raster(s))); - d.metadata.xmp = Some(XMP.to_string()); + d.metadata.xmp = Some(XMP.into()); d } diff --git a/crates/io/tests/large_png_export.rs b/crates/io/tests/large_png_export.rs new file mode 100644 index 0000000000..797efab049 --- /dev/null +++ b/crates/io/tests/large_png_export.rs @@ -0,0 +1,68 @@ +//! PNG export of a multi-gigapixel print (#8456): a 136 ft × 26 ft banner at 72 ppi is +//! 117504 × 22464 px (2.64 GP, 7.9 GB of 8-bit RGB), past the 2^30 px a whole-image export could +//! hold. It is written a band of rows at a time, its zlib stream split over IDAT chunks. + +use std::io::Read; + +use photocraft_color::{ColorMode, SampleType}; +use photocraft_doc::{Document, Layer, LayerContent}; +use photocraft_geom::{Rect, Size}; +use photocraft_io::{ExportOptions, export}; +use photocraft_raster::Surface; + +/// The IHDR fields and the IDAT payloads of `png`. +fn chunks(png: &[u8]) -> ((u32, u32, u8, u8), Vec<&[u8]>) { + let (mut at, mut ihdr, mut idat) = (8, None, Vec::new()); + while at + 12 <= png.len() { + let len = u32::from_be_bytes(png[at..at + 4].try_into().unwrap()) as usize; + let data = &png[at + 8..at + 8 + len]; + match &png[at + 4..at + 8] { + b"IHDR" => ihdr = Some((u32::from_be_bytes(data[0..4].try_into().unwrap()), u32::from_be_bytes(data[4..8].try_into().unwrap()), data[8], data[9])), + b"IDAT" => idat.push(data), + _ => {} + } + at += 12 + len; + } + (ihdr.unwrap(), idat) +} + +/// Run with `cargo test --release -p photocraft-io --test large_png_export -- --ignored`; with +/// `COMPOSITE=1` a translucent layer on top makes the export composite the banner. +/// Measured 2026-10-22 (20 threads): 12.3 s and 0.49 GB peak RSS, composited 29 s and 1.2 GB; the +/// whole-image export took 12.9 s and 8.0 GB, composited 43 s and 10.3 GB. +#[test] +#[ignore = "encodes 7.9 GB of pixels; run in release"] +fn a_banner_over_two_gigapixels_exports_as_png() { + const W: u32 = 117_504; + const H: u32 = 22_464; + let mut doc = Document::new("Tent banner", Size::new(W, H), ColorMode::Rgb, SampleType::U8); + let mut s = Surface::new(doc.pixel_format()); + s.fill_rect(doc.bounds(), &[1.0, 1.0, 1.0, 1.0]); + s.fill_rect(Rect::new(0, 15_000, W as i32, H as i32), &[0.1, 0.45, 0.8, 1.0]); + s.fill_rect(Rect::new(40_000, 2_000, 77_504, 14_000), &[0.55, 0.05, 0.08, 1.0]); + doc.layers.push(Layer::new("Background", LayerContent::Raster(s))); + if std::env::var_os("COMPOSITE").is_some() { + let mut t = Surface::new(doc.pixel_format()); + t.fill_rect(Rect::new(10_000, 5_000, 100_000, 20_000), &[0.9, 0.7, 0.1, 0.6]); + doc.layers.push(Layer::new("Text", LayerContent::Raster(t))); + } + + let started = std::time::Instant::now(); + let r = export(&doc, "banner.png", &ExportOptions::default()).unwrap(); + eprintln!("exported {} MB in {:.1} s; warnings {:?}", r.bytes.len() >> 20, started.elapsed().as_secs_f64(), r.warnings); + + let ((w, h, depth, color), idat) = chunks(&r.bytes); + assert_eq!((w, h, depth, color), (W, H, 8, 2), "8-bit RGB"); + // The zlib stream inflates (Adler-32 checked) to a filter byte and W RGB pixels per row. + let joined = idat.iter().flat_map(|c| c.iter().copied()).collect::>(); + let mut z = flate2::read::ZlibDecoder::new(joined.as_slice()); + let (mut buf, mut total) = (vec![0u8; 1 << 20], 0u64); + loop { + let n = z.read(&mut buf).unwrap(); + if n == 0 { + break; + } + total += n as u64; + } + assert_eq!(total, u64::from(H) * (1 + u64::from(W) * 3)); +} diff --git a/crates/io/tests/modes.rs b/crates/io/tests/modes.rs deleted file mode 100644 index f33fe5927c..0000000000 --- a/crates/io/tests/modes.rs +++ /dev/null @@ -1,51 +0,0 @@ -//! Indexed Color and Duotone documents in flat exports. - -use photocraft_color::{Color, ColorMode, SampleType}; -use photocraft_doc::{ColorTable, Document, Duotone, DuotoneInk, Size}; - -fn doc(mode: ColorMode) -> Document { - let mut d = Document::with_background("m", Size::new(4, 2), ColorMode::Rgb, SampleType::U8, Color::rgba(1.0, 0.0, 0.0, 1.0)); - d.mode = mode; - d -} - -#[test] -fn indexed_png_is_palette_png() { - let mut d = doc(ColorMode::Indexed); - d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0]], transparent: None }); - let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); - // IHDR colour type 3 = indexed; a PLTE chunk follows. - assert_eq!(r.bytes[25], 3); - assert!(r.bytes.windows(4).any(|w| w == b"PLTE")); - let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; - let px = back.layers[0].surface().unwrap().rgba(1, 1); - assert_eq!(&px[..3], &[1.0, 0.0, 0.0]); - // Other formats keep the expanded pixels. - let j = photocraft_io::export(&d, "x.tif", &Default::default()).unwrap(); - assert!(!j.bytes.is_empty()); -} - -#[test] -fn duotone_exports_the_inks_as_rgb() { - // Mid gray background. - let mut d = Document::with_background("m", Size::new(4, 2), ColorMode::Grayscale, SampleType::U8, Color::rgba(0.5, 0.5, 0.5, 1.0)); - d.mode = ColorMode::Duotone; - d.duotone = Some(Duotone { inks: vec![DuotoneInk::new("Black", [0.0; 3]), DuotoneInk::new("Orange", [1.0, 0.5, 0.0])], psd_raw: None }); - let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); - assert!(r.warnings.iter().any(|w| w.contains("Duotone")), "{:?}", r.warnings); - let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; - let px = back.layers[0].surface().unwrap().rgba(0, 0); - assert!(px[0] > px[2] + 0.05, "warm: {px:?}"); -} - -#[test] -fn indexed_png_keeps_the_document_resolution() { - // #3047: the palette PNG lost its pHYs chunk and reopened at 72 ppi. - let mut d = doc(ColorMode::Indexed); - d.resolution_dpi = 300.0; - d.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 0, 0]], transparent: None }); - let r = photocraft_io::export(&d, "x.png", &Default::default()).unwrap(); - assert_eq!(r.bytes[25], 3, "still a palette PNG"); - let back = photocraft_io::import("x.png", &r.bytes).unwrap().document; - assert!((back.resolution_dpi - 300.0).abs() < 0.01, "{}", back.resolution_dpi); -} diff --git a/crates/io/tests/psd_block_alignment.rs b/crates/io/tests/psd_block_alignment.rs deleted file mode 100644 index 534fdc257b..0000000000 --- a/crates/io/tests/psd_block_alignment.rs +++ /dev/null @@ -1,53 +0,0 @@ -//! Every layer-level tagged block of an exported PSD/PSB is a multiple of 4 bytes long. -//! -//! Photoshop pads layer-level blocks to 4 inside the length field; all of its own files do (567 -//! fixtures checked), and a regenerated block that stopped at an even but unaligned size left the -//! layer record's extra data unaligned: a Pattern Fill's `PtFl` came out 2 bytes short of the -//! boundary for half of all pattern names. - -use photocraft_color::{ColorMode, PixelFormat, SampleType}; -use photocraft_doc::{Document, Fill, Layer, LayerContent, Pattern}; -use photocraft_geom::{Rect, Size}; -use photocraft_io::*; -use photocraft_psd::PsdFile; -use photocraft_raster::Surface; - -fn pattern(name: &str) -> Pattern { - let mut s = Surface::new(PixelFormat::RGBA8); - s.fill_rect(Rect::new(0, 0, 8, 8), &[0.8, 0.8, 0.8, 1.0]); - s.fill_rect(Rect::new(0, 0, 4, 4), &[0.2, 0.4, 0.9, 1.0]); - Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: name.into(), width: 8, height: 8, surface: s } -} - -/// One Pattern Fill layer per name; the name's length moves the descriptor's size by two bytes -/// per character, so consecutive lengths cover every residue modulo 4. -fn doc(depth: SampleType, names: &[&str]) -> Document { - let mut d = Document::new("p", Size::new(40, 24), ColorMode::Rgb, depth); - for name in names { - let p = pattern(name); - let fill = Fill::Pattern { name: (*name).into(), scale: 1.0, id: p.id.clone(), angle: 0.0, link: true, phase: (0.0, 0.0) }; - d.patterns.push(p); - d.layers.push(Layer::new(format!("Fill {name}"), LayerContent::Fill(fill))); - } - d -} - -#[test] -fn layer_blocks_are_four_byte_aligned() { - let names = ["A", "Ab", "Abc", "Abcd", "Dots", "Checker", "Wood grain"]; - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - for path in ["x.psd", "x.psb"] { - let bytes = export(&doc(depth, &names), path, &ExportOptions::default()).expect("export").bytes; - let file = PsdFile::from_bytes(&bytes).expect("reparse"); - let mut fills = 0; - for layer in file.layers() { - for b in &layer.blocks { - let key = String::from_utf8_lossy(&b.key).into_owned(); - assert_eq!(b.data.len() % 4, 0, "{depth:?} {path}: layer {:?} block {key} is {} bytes", layer.name(), b.data.len()); - } - fills += usize::from(layer.block(b"PtFl").is_some()); - } - assert_eq!(fills, names.len(), "{depth:?} {path}: every pattern fill keeps its PtFl block"); - } - } -} diff --git a/crates/io/tests/support/recovery_psd.rs b/crates/io/tests/support/recovery_psd.rs new file mode 100644 index 0000000000..4d26bec646 --- /dev/null +++ b/crates/io/tests/support/recovery_psd.rs @@ -0,0 +1,196 @@ +//! Minimal, deterministic PSD fixtures for the PSD recovery tests. +//! +//! Pure Rust / std: no binary fixtures, external assets, Python, or build script. +//! The damaged fixture has valid layer records and a valid merged composite; +//! only the green channel of the orange layer contains a truncated PackBits run. + +const WIDTH: usize = 128; +const HEIGHT: usize = 96; +const PANEL: (usize, usize, usize, usize) = (20, 16, 108, 80); +const PANEL_RGB: [u8; 3] = [244, 156, 28]; +const PANEL_ALPHA: u8 = 220; + +struct Channel { + id: i16, + data: Vec, +} + +struct Layer { + record: Vec, + pixels: Vec, +} + +/// Writes the big-endian 32-bit length of a PSD section to the output buffer. +fn put_len(out: &mut Vec, len: usize) { + out.extend_from_slice(&u32::try_from(len).expect("test PSD section fits u32").to_be_bytes()); +} + +/// Builds the three RGB planes of the deterministic blue checkerboard background. +fn background_rgb() -> [Vec; 3] { + let mut planes: [Vec; 3] = std::array::from_fn(|_| Vec::with_capacity(WIDTH * HEIGHT)); + for y in 0..HEIGHT { + for x in 0..WIDTH { + let checker = u8::try_from((x / 16 + y / 16) % 2).expect("checker value is 0 or 1"); + planes[0].push(34 + checker * 12); + planes[1].push(102 + checker * 15); + planes[2].push(184 + checker * 12); + } + } + planes +} + +/// Encodes a layer channel without compression, including its PSD compression marker. +fn raw_channel(id: i16, pixels: &[u8]) -> Channel { + let mut data = Vec::with_capacity(2 + pixels.len()); + data.extend_from_slice(&0u16.to_be_bytes()); // 0 = uncompressed + data.extend_from_slice(pixels); + Channel { id, data } +} + +/// Encodes constant-color PackBits rows, optionally truncating the middle row. +/// The corrupt variant keeps the row-length table structurally valid. +fn rle_channel(id: i16, sample: u8, width: usize, height: usize, corrupt: bool) -> Channel { + let mut rows: Vec> = Vec::with_capacity(height); + for y in 0..height { + if corrupt && y == height / 2 { + // 0 declares one literal byte, but that byte is missing. + // Its row-length entry remains valid: this tests the decoder. + rows.push(vec![0]); + continue; + } + let mut row = Vec::new(); + let mut remaining = width; + while remaining != 0 { + let count = remaining.min(128); + // PackBits negative repeat count -(count - 1), modulo 256. + let header = u8::try_from(257 - count).expect("PackBits repeat header fits u8"); + row.extend_from_slice(&[header, sample]); + remaining -= count; + } + rows.push(row); + } + let mut data = Vec::new(); + data.extend_from_slice(&1u16.to_be_bytes()); // 1 = PackBits/RLE + for row in &rows { + data.extend_from_slice(&u16::try_from(row.len()).expect("row fits PSD u16 length").to_be_bytes()); + } + for row in rows { + data.extend_from_slice(&row); + } + Channel { id, data } +} + +/// Serializes a PSD layer record and its channel payloads separately, in file order. +fn layer(name: &str, rect: (i32, i32, i32, i32), channels: Vec) -> Layer { + let (left, top, right, bottom) = rect; + let name_bytes = name.as_bytes(); + let name_len = u8::try_from(name_bytes.len()).expect("layer name fits Pascal string"); + + let mut extra = Vec::new(); + extra.extend_from_slice(&0u32.to_be_bytes()); // no layer mask + extra.extend_from_slice(&0u32.to_be_bytes()); // no blending ranges + extra.push(name_len); + extra.extend_from_slice(name_bytes); + while extra.len() % 4 != 0 { + extra.push(0); + } + + let mut record = Vec::new(); + for coordinate in [top, left, bottom, right] { + record.extend_from_slice(&coordinate.to_be_bytes()); + } + record.extend_from_slice(&u16::try_from(channels.len()).expect("channel count fits u16").to_be_bytes()); + for channel in &channels { + record.extend_from_slice(&channel.id.to_be_bytes()); + put_len(&mut record, channel.data.len()); + } + record.extend_from_slice(b"8BIMnorm"); + record.extend_from_slice(&[255, 0, 0, 0]); // opacity, clipping, flags, filler + put_len(&mut record, extra.len()); + record.extend_from_slice(&extra); + + let mut pixels = Vec::new(); + for channel in channels { + pixels.extend_from_slice(&channel.data); + } + Layer { record, pixels } +} + +/// Composites the orange rectangle over the checkerboard as an uncompressed +/// merged RGB image, independent of the deliberately damaged layer channel. +fn merged_rgb(background: &[Vec; 3]) -> Vec { + let mut planes = background.clone(); + for y in PANEL.1..PANEL.3 { + for x in PANEL.0..PANEL.2 { + let pixel = y * WIDTH + x; + for channel in 0..3 { + let fg = u32::from(PANEL_RGB[channel]); + let bg = u32::from(background[channel][pixel]); + let alpha = u32::from(PANEL_ALPHA); + let mixed = (fg * alpha + bg * (255 - alpha) + 127) / 255; + planes[channel][pixel] = u8::try_from(mixed).expect("composite pixel fits u8"); + } + } + } + let mut data = Vec::new(); + data.extend_from_slice(&0u16.to_be_bytes()); // merged composite: uncompressed + for plane in &planes { + data.extend_from_slice(plane); + } + data +} + +/// Returns a complete PSD file in memory. `corrupt` changes *only* one RLE row +/// in the green channel of the upper layer; the file is otherwise well framed. +pub fn make_psd(corrupt: bool) -> Vec { + let background = background_rgb(); + let background_layer = + layer("SAFE blue background", (0, 0, WIDTH as i32, HEIGHT as i32), (0..3).map(|ch| raw_channel(ch, &background[ch as usize])).collect()); + + let panel_width = PANEL.2 - PANEL.0; + let panel_height = PANEL.3 - PANEL.1; + let top_layer = layer( + "BROKEN orange RLE", + (PANEL.0 as i32, PANEL.1 as i32, PANEL.2 as i32, PANEL.3 as i32), + vec![ + rle_channel(0, PANEL_RGB[0], panel_width, panel_height, false), + rle_channel(1, PANEL_RGB[1], panel_width, panel_height, corrupt), + rle_channel(2, PANEL_RGB[2], panel_width, panel_height, false), + rle_channel(-1, PANEL_ALPHA, panel_width, panel_height, false), + ], + ); + // Photoshop stores the top layer's record first. + let layers = [top_layer, background_layer]; + let mut layer_info = Vec::new(); + layer_info.extend_from_slice(&2i16.to_be_bytes()); + for layer in &layers { + layer_info.extend_from_slice(&layer.record); + } + for layer in &layers { + layer_info.extend_from_slice(&layer.pixels); + } + if layer_info.len() % 2 != 0 { + layer_info.push(0); // layer info is padded to an even size + } + + let mut layer_and_mask = Vec::new(); + put_len(&mut layer_and_mask, layer_info.len()); + layer_and_mask.extend_from_slice(&layer_info); + layer_and_mask.extend_from_slice(&0u32.to_be_bytes()); // empty global layer mask + + let mut out = Vec::new(); + out.extend_from_slice(b"8BPS"); + out.extend_from_slice(&1u16.to_be_bytes()); // PSD (not PSB) + out.extend_from_slice(&[0u8; 6]); + out.extend_from_slice(&3u16.to_be_bytes()); // RGB channels + out.extend_from_slice(&(HEIGHT as u32).to_be_bytes()); + out.extend_from_slice(&(WIDTH as u32).to_be_bytes()); + out.extend_from_slice(&8u16.to_be_bytes()); // 8 bits per channel + out.extend_from_slice(&3u16.to_be_bytes()); // RGB colour mode + out.extend_from_slice(&0u32.to_be_bytes()); // no colour-mode data + out.extend_from_slice(&0u32.to_be_bytes()); // no image resources + put_len(&mut out, layer_and_mask.len()); + out.extend_from_slice(&layer_and_mask); + out.extend_from_slice(&merged_rgb(&background)); + out +} diff --git a/crates/io/tests/tiff_layers.rs b/crates/io/tests/tiff_layers.rs deleted file mode 100644 index 56e51bf9f9..0000000000 --- a/crates/io/tests/tiff_layers.rs +++ /dev/null @@ -1,394 +0,0 @@ -//! Layered TIFF (Photoshop layer data in tags 37724 / 34377): Document → TIFF → Document round -//! trips in every mode and depth, equivalence with the PSD path, the flat fall-backs, files -//! written by an independent implementation (psdtags, both byte orders), and malformed input. - -mod common; - -use common::*; -use photocraft_color::{ColorMode, SampleType}; -use photocraft_doc::{Document, LayerContent}; -use photocraft_io::*; -use photocraft_psd::tiff::{ByteOrder, ImageSourceData}; - -fn tiff(doc: &Document, layers: bool) -> ExportResult { - export(doc, "x.tif", &ExportOptions { tiff_layers: layers, ..Default::default() }).expect("export") -} - -/// The two Photoshop tags of a TIFF: (resources, layers). -fn tags(bytes: &[u8]) -> (Option>, Option>) { - let (r, l) = photocraft_codecs::tiff_photoshop_tags(bytes); - (r.map(<[u8]>::to_vec), l.map(<[u8]>::to_vec)) -} - -fn roundtrip(doc: &Document) -> Document { - let r = tiff(doc, true); - let (resources, layers) = tags(&r.bytes); - let layers = layers.expect("layer data"); - assert!(resources.is_some_and(|r| !r.is_empty()), "image resources"); - // The layer data matches the container's byte order and is byte-stable through the psd crate. - let order = if photocraft_codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; - let (isd, w) = ImageSourceData::from_bytes(&layers).expect("parse layer data"); - assert!(w.is_empty(), "{w:?}"); - assert_eq!(isd.byte_order, order); - assert_eq!(isd.to_bytes(order).unwrap().0, layers); - // The TIFF is a plain image to any other reader: the composite at the document's size. - let flat = photocraft_codecs::decode(&r.bytes).expect("flat decode"); - assert_eq!(flat.dimensions(), (doc.size.width, doc.size.height)); - // A TIFF holds one transparency channel: extra alpha channels (and a Quick Mask) are not - // kept, and the export says so. - let extra = doc.channels.len() + usize::from(doc.quick_mask.is_some()); - assert_eq!(r.warnings.iter().any(|w| w.contains("alpha channel(s) are not written")), extra > 0, "{:?}", r.warnings); - import("x.tif", &r.bytes).expect("import").document -} - -/// `doc` as a layered TIFF round trip returns it: without extra alpha channels or a Quick Mask. -fn expected(doc: &Document) -> Document { - let mut d = doc.clone(); - d.channels.clear(); - d.quick_mask = None; - d -} - -macro_rules! rt_case { - ($name:ident, $mode:expr, $depth:expr, $f:expr) => { - #[test] - fn $name() { - let d = gen_doc($mode, $depth, $f); - let back = roundtrip(&d); - assert_docs_eq(&expected(&d), &back); - } - }; -} - -rt_case!(rt_rgb8_all, ColorMode::Rgb, SampleType::U8, Features::ALL); -rt_case!(rt_rgb16_all, ColorMode::Rgb, SampleType::U16, Features::ALL); -rt_case!(rt_rgb32_all, ColorMode::Rgb, SampleType::F32, Features::ALL); -rt_case!(rt_gray8_all, ColorMode::Grayscale, SampleType::U8, Features::ALL); -rt_case!(rt_gray16_all, ColorMode::Grayscale, SampleType::U16, Features::ALL); -rt_case!(rt_gray32_all, ColorMode::Grayscale, SampleType::F32, Features::ALL); -rt_case!(rt_cmyk8_all, ColorMode::Cmyk, SampleType::U8, Features::ALL); -rt_case!(rt_cmyk16_all, ColorMode::Cmyk, SampleType::U16, Features::ALL); -rt_case!(rt_rgb8_pixels, ColorMode::Rgb, SampleType::U8, Features::PIXELS); -rt_case!(rt_gray16_pixels, ColorMode::Grayscale, SampleType::U16, Features::PIXELS); -rt_case!(rt_cmyk8_pixels, ColorMode::Cmyk, SampleType::U8, Features::PIXELS); - -#[test] -fn tiff_and_psd_open_as_the_same_document() { - for (mode, depth) in [(ColorMode::Rgb, SampleType::U8), (ColorMode::Cmyk, SampleType::U16), (ColorMode::Grayscale, SampleType::F32)] { - let d = gen_doc(mode, depth, Features::ALL); - let psd = export(&d, "x.psd", &ExportOptions::default()).unwrap(); - let from_psd = import("x.psd", &psd.bytes).unwrap().document; - let from_tif = roundtrip(&d); - assert_docs_eq(&expected(&from_psd), &from_tif); - // The composite the TIFF carries is what the document looks like. - let tif = tiff(&d, true); - let flat = import( - "flat.tif", - &photocraft_codecs::encode(&photocraft_codecs::decode(&tif.bytes).unwrap(), photocraft_codecs::Format::Tiff, &Default::default()).unwrap(), - ) - .unwrap() - .document; - let a = photocraft_compose::flatten(&d).px; - let b = photocraft_compose::flatten(&flat).px; - assert!(max_diff(&a, &b) <= 2.0 / 255.0, "{mode:?} {depth:?}: composite differs by {}", max_diff(&a, &b)); - } -} - -#[test] -fn every_compression_keeps_the_layers() { - use photocraft_codecs::TiffCompression; - let d = gen_doc(ColorMode::Rgb, SampleType::U16, Features::PIXELS); - for c in [TiffCompression::None, TiffCompression::Lzw, TiffCompression::Deflate, TiffCompression::PackBits] { - let mut opts = ExportOptions { tiff_layers: true, ..Default::default() }; - opts.encode.tiff_compression = c; - let r = export(&d, "x.tiff", &opts).unwrap(); - let back = import("x.tiff", &r.bytes).unwrap().document; - assert_docs_eq(&d, &back); - } -} - -/// The Artist tag's text, and the bytes every EXIF assertion looks for. -const ARTIST: &[u8] = b"fixture-artist"; - -/// A minimal little-endian EXIF (TIFF) block with a single Artist tag and no Orientation, so -/// `upright_exif` passes it through untouched. -fn exif() -> Vec { - let mut artist = ARTIST.to_vec(); - artist.push(0); // ASCII NUL terminator - let mut v = b"II*\0\x08\0\0\0".to_vec(); // TIFF header, first IFD at 8 - v.extend_from_slice(&1u16.to_le_bytes()); // one entry - v.extend_from_slice(&0x013bu16.to_le_bytes()); // Artist - v.extend_from_slice(&2u16.to_le_bytes()); // ASCII - v.extend_from_slice(&(artist.len() as u32).to_le_bytes()); - v.extend_from_slice(&26u32.to_le_bytes()); // value offset: 8 + 2 + 12 + 4 - v.extend_from_slice(&0u32.to_le_bytes()); // no next IFD - v.extend_from_slice(&artist); - v -} - -/// #1545: a layered TIFF carries EXIF as Photoshop resource 1058 and reads it back, so it must -/// not report a loss it never makes. A flat TIFF writes it as its own tags and keeps it too. -#[test] -fn layered_tiff_keeps_exif_without_claiming_a_loss() { - let mut d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - d.metadata.exif = Some(std::sync::Arc::new(exif())); - - let r = tiff(&d, true); - assert!(!r.warnings.iter().any(|w| w.contains("EXIF")), "a layered TIFF keeps EXIF, so it must not claim to drop it: {:?}", r.warnings); - // ...and the tags really are in the file, so the missing warning is a true absence of loss. - let back = import("x.tif", &r.bytes).unwrap().document; - let kept = back.metadata.exif.as_deref().expect("EXIF survives the layered export"); - assert!(kept.windows(ARTIST.len()).any(|w| w == ARTIST), "the Artist tag did not survive: {kept:?}"); - - // The flat path writes the EXIF block's tags as the TIFF's own (here IFD0's Artist, which - // reads back as the document's Artist text), so it reports no loss either. - let flat = tiff(&d, false); - assert!(!flat.warnings.iter().any(|w| w.contains("EXIF")), "a flat TIFF keeps EXIF: {:?}", flat.warnings); - let back = import("x.tif", &flat.bytes).unwrap().document; - let artist = std::str::from_utf8(ARTIST).unwrap().trim_end_matches('\0'); - assert!(back.metadata.text.iter().any(|(k, v)| k == "Artist" && v == artist), "{:?}", back.metadata.text); -} - -#[test] -fn discard_layers_writes_a_flat_file() { - let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - let r = tiff(&d, false); - assert_eq!(tags(&r.bytes), (None, None)); - assert!(r.warnings.iter().any(|w| w.contains("flattened")), "{:?}", r.warnings); - let back = import("x.tif", &r.bytes).unwrap().document; - assert_eq!(back.layers.len(), 1); - assert!(!tiff_layers::would_write_layers(&back)); -} - -#[test] -fn a_plain_background_needs_no_layer_data() { - let d = import("x.png", &export(&gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS), "x.png", &Default::default()).unwrap().bytes).unwrap().document; - assert_eq!(d.layers.len(), 1); - assert!(!tiff_layers::would_write_layers(&d)); - let r = tiff(&d, true); - assert_eq!(tags(&r.bytes), (None, None)); - assert!(r.warnings.is_empty(), "{:?}", r.warnings); - let back = import("x.tif", &r.bytes).unwrap().document; - assert_docs_eq(&d, &back); -} - -#[test] -fn lab_documents_are_saved_flat_with_a_warning() { - let d = gen_doc(ColorMode::Lab, SampleType::U8, Features::PIXELS); - let r = tiff(&d, true); - assert_eq!(tags(&r.bytes), (None, None)); - assert!(r.warnings.iter().any(|w| w.contains("Lab") && w.contains("layers are not kept")), "{:?}", r.warnings); - assert!(import("x.tif", &r.bytes).is_ok()); -} - -#[test] -fn transparency_is_straight_in_the_composite() { - // A translucent layer over nothing: the TIFF's composite keeps its colour under the alpha - // instead of being matted against white like a PSD's merged image. - let mut d = Document::new("t", photocraft_geom::Size::new(4, 4), ColorMode::Rgb, SampleType::U8); - let fmt = d.pixel_format(); - let mut l = raster("Red", fmt, photocraft_geom::Rect::new(0, 0, 4, 4), 0, false); - if let LayerContent::Raster(s) = &mut l.content { - let px: Vec = (0..16).flat_map(|_| [1.0, 0.0, 0.0, 0.5]).collect(); - s.write_region(photocraft_geom::Rect::new(0, 0, 4, 4), &px); - } - d.layers.push(l); - let r = tiff(&d, true); - let flat = photocraft_codecs::decode(&r.bytes).unwrap(); - assert!(flat.layout().has_alpha()); - let px = flat.data(); - assert_eq!(&px[..4], &[255, 0, 0, 128]); - let back = import("x.tif", &r.bytes).unwrap().document; - assert_docs_eq(&d, &back); -} - -#[test] -fn big_documents_use_the_psb_signature() { - // Forcing PSB stands in for a > 30000 px document. - let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - let r = export(&d, "x.tif", &ExportOptions { force_psb: true, tiff_layers: true, ..Default::default() }).unwrap(); - let (_, layers) = tags(&r.bytes); - assert!(layers.unwrap().starts_with(photocraft_psd::tiff::SIGNATURE_PSB)); - let back = import("x.tif", &r.bytes).unwrap().document; - assert_docs_eq(&d, &back); -} - -/// Files written by psdtags (`scripts/layered_tiff_fixtures.py`): two RGB layers, "Background" -/// (RLE) and an offset, translucent "Upper é" (ZIP, Multiply, opacity 200), in both byte orders -/// at 8 and 16 bits. Binary files stay out of this repository: run the script to write them -/// to `corpus/layered-tiff/` (gitignored); without them these checks are skipped. -fn fixture(name: &str) -> Option> { - let bytes = std::fs::read(format!("{}/../../corpus/layered-tiff/{name}", env!("CARGO_MANIFEST_DIR"))).ok(); - if bytes.is_none() { - eprintln!("skipped {name}: run `python scripts/layered_tiff_fixtures.py` to write corpus/layered-tiff/"); - } - bytes -} - -#[test] -fn psdtags_fixtures_open_with_their_layers() { - for (name, depth) in [ - ("layered-le-8bit.tif", SampleType::U8), - ("layered-be-8bit.tif", SampleType::U8), - ("layered-le-16bit.tif", SampleType::U16), - ("layered-be-16bit.tif", SampleType::U16), - ] { - let Some(bytes) = fixture(name) else { continue }; - let r = import(name, &bytes).unwrap_or_else(|e| panic!("{name}: {e}")); - let d = &r.document; - assert_eq!(d.size, photocraft_geom::Size::new(6, 4), "{name}"); - assert_eq!(d.mode, ColorMode::Rgb); - assert_eq!(d.depth, depth, "{name}"); - let names: Vec<&str> = d.layers.iter().map(|l| l.name.as_str()).collect(); - assert_eq!(names, ["Background", "Upper \u{e9}"], "{name}"); - let upper = &d.layers[1]; - assert_eq!(upper.blend, photocraft_color::BlendMode::Multiply); - assert!((upper.opacity - 200.0 / 255.0).abs() < 1e-6); - let LayerContent::Raster(s) = &upper.content else { panic!("raster") }; - let bounds = s.content_bounds(); - assert_eq!((bounds.x0, bounds.y0, bounds.width(), bounds.height()), (1, 1, 4, 3), "{name}"); - // Pixel (x=2, y=0) of the upper layer: the generator's ramp, alpha 160/255. - let px = s.pixel(3, 1); - let expect = |v: u32| (v % 256) as f32 / 255.0; - assert!((px[0] - expect(2 * 40 + 7)).abs() < 1e-3, "{name}: {px:?}"); - assert!((px[1] - expect(7 * 3)).abs() < 1e-3, "{name}: {px:?}"); - assert!((px[3] - 160.0 / 255.0).abs() < 1e-3, "{name}: {px:?}"); - let LayerContent::Raster(bg) = &d.layers[0].content else { panic!("raster") }; - let b = bg.pixel(5, 3); - assert!((b[0] - expect(5 * 40 + 1)).abs() < 1e-3, "{name}: {b:?}"); - assert!((b[3] - 1.0).abs() < 1e-6); - assert_eq!(d.resolution_dpi, 300.0); - // tifffile writes a Software tag; nothing else is worth a warning. - assert!(r.warnings.iter().all(|w| w.contains("text metadata")), "{name}: {:?}", r.warnings); - // psdtags wrote the composite's alpha sample without the transparency flag, so by the - // PSD rules it is an alpha channel (which a re-save cannot keep). Re-saving keeps - // everything else; the file is then in the encoder's byte order. - assert_eq!(d.channels.len(), 1, "{name}"); - let out = tiff(d, true); - let back = import(name, &out.bytes).unwrap().document; - assert_docs_eq(&expected(d), &back); - } -} - -/// Advanced Blending (`knko`, `infx`, `clbl`, `tsly`, `lmgm`, `vmgm`), fill opacity (`iOpa`), -/// channel restrictions (`brst`) and Blend If (the records' blending ranges) survive a layered -/// TIFF with its layer data in either byte order: the encoder's own, and the other one made by -/// transcoding the tag (the same length, spliced in place). -#[test] -fn advanced_blending_survives_layered_tiff_in_both_byte_orders() { - use photocraft_doc::{AdvancedBlending, BlendRange, Knockout}; - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - let mut d = gen_doc(ColorMode::Rgb, depth, Features::PIXELS); - let adv = AdvancedBlending { - knockout: Knockout::Deep, - blend_interior: true, - blend_clipped: false, - transparency_shapes: false, - layer_mask_hides_effects: true, - vector_mask_hides_effects: true, - }; - d.layers[1].advanced = adv; - d.layers[1].fill_opacity = 64.0 / 255.0; - d.layers[1].excluded_channels = 0b010; - d.layers[1].blend_if.set(0, [BlendRange { black: [10, 40], white: [200, 250] }, BlendRange { black: [0, 0], white: [128, 128] }]); - d.layers[2].advanced.knockout = Knockout::Shallow; - let r = tiff(&d, true); - let (_, layers) = tags(&r.bytes); - let layers = layers.expect("layer data"); - let (isd, w) = ImageSourceData::from_bytes(&layers).expect("parse"); - assert!(w.is_empty(), "{w:?}"); - let native = isd.byte_order; - let other = if native == ByteOrder::Little { ByteOrder::Big } else { ByteOrder::Little }; - let (swapped, w) = isd.to_bytes(other).expect("transcode"); - assert!(w.is_empty(), "{depth:?}: blocks dropped while transcoding: {w:?}"); - assert_eq!(swapped.len(), layers.len()); - let at = r.bytes.windows(layers.len()).position(|x| x == &layers[..]).expect("tag data in the file"); - let mut flipped = r.bytes.clone(); - flipped[at..at + layers.len()].copy_from_slice(&swapped); - for (order, bytes) in [(native, &r.bytes), (other, &flipped)] { - let back = import("x.tif", bytes).unwrap_or_else(|e| panic!("{depth:?} {order:?}: {e}")).document; - let l = &back.layers[1]; - assert_eq!(l.advanced, adv, "{depth:?} {order:?}"); - assert_eq!(l.fill_opacity, 64.0 / 255.0, "{depth:?} {order:?}"); - assert_eq!(l.excluded_channels, 0b010, "{depth:?} {order:?}"); - assert_eq!(l.blend_if, d.layers[1].blend_if, "{depth:?} {order:?}"); - assert_eq!(back.layers[2].advanced.knockout, Knockout::Shallow, "{depth:?} {order:?}"); - assert_docs_eq(&expected(&d), &back); - } - } -} - -#[test] -fn damaged_layer_data_opens_flattened() { - let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - let r = tiff(&d, true); - let (_, layers) = tags(&r.bytes); - let layers = layers.unwrap(); - let at = r.bytes.windows(layers.len()).position(|w| w == &layers[..]).unwrap(); - // Garbage after the signature: no layers, a warning, the composite as one layer. - let mut bad = r.bytes.clone(); - for b in &mut bad[at + 36..at + 36 + 24] { - *b ^= 0xa5; - } - let r2 = import("x.tif", &bad).unwrap(); - assert_eq!(r2.document.layers.len(), 1); - assert!(r2.warnings.iter().any(|w| w.contains("could not be read")), "{:?}", r2.warnings); - // Every truncation and a sweep of bit flips through the whole file: never a panic. - for cut in (0..r.bytes.len()).step_by(11) { - let _ = import("x.tif", &r.bytes[..cut]); - } - for i in (0..r.bytes.len()).step_by(29) { - let mut b = r.bytes.clone(); - b[i] ^= 0x5a; - let _ = import("x.tif", &b); - } - for name in ["layered-le-8bit.tif", "layered-be-16bit.tif"] { - let Some(bytes) = fixture(name) else { continue }; - for cut in (0..bytes.len()).step_by(7) { - let _ = import(name, &bytes[..cut]); - } - for i in (0..bytes.len()).step_by(5) { - let mut b = bytes.clone(); - b[i] ^= 0xff; - let _ = import(name, &b); - } - } -} - -#[test] -fn default_export_options_write_a_flat_tiff() { - // Scripted and agent saves (CLI, batch, MCP) build on the defaults: layers only on request. - let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - assert!(tiff_layers::would_write_layers(&d)); - let r = export(&d, "x.tif", &ExportOptions::default()).unwrap(); - assert_eq!(tags(&r.bytes), (None, None)); - assert_eq!(import("x.tif", &r.bytes).unwrap().document.layers.len(), 1); -} - -/// #2940: layered TIFF data whose groups nest past the document's cap is rejected with the same -/// limit error as a PSD, instead of being converted. -#[test] -fn layered_tiff_nested_past_the_cap_is_rejected() { - use photocraft_psd::{GroupSpec, LayerSpec, PixelData, PsdBuilder, PsdFile}; - let tiff_nested = |levels: usize| { - let mut b = PsdBuilder::new(4, 4); - for _ in 0..levels { - b.begin_group(GroupSpec::new("g")); - } - b.push_layer(LayerSpec::new("deep", 0, 0, 1, 1, PixelData::Rgba8(vec![1, 2, 3, 4]))); - for _ in 0..levels { - b.end_group().unwrap(); - } - let file = PsdFile::from_bytes(&b.to_bytes().unwrap()).unwrap(); - let order = if photocraft_codecs::tiff_writes_little_endian() { ByteOrder::Little } else { ByteOrder::Big }; - let layers = ImageSourceData::from_psd(&file).to_bytes(order).unwrap().0; - let d = gen_doc(ColorMode::Rgb, SampleType::U8, Features::PIXELS); - let mut img = photocraft_codecs::decode(&tiff(&d, false).bytes).unwrap(); - img.meta.photoshop_layers = Some(layers); - photocraft_codecs::encode(&img, photocraft_codecs::Format::Tiff, &photocraft_codecs::EncodeOptions::default()).unwrap() - }; - let err = import("deep.tif", &tiff_nested(101)).unwrap_err(); - assert!(err.to_string().contains("nested deeper than 100"), "{err}"); - let r = import("hundred.tif", &tiff_nested(100)).unwrap(); - assert_eq!(r.document.max_group_depth(), 100); -} diff --git a/crates/ipp/Cargo.toml b/crates/ipp/Cargo.toml new file mode 100644 index 0000000000..cc9f6aec31 --- /dev/null +++ b/crates/ipp/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "photocraft-ipp" +description = "A small IPP client for CUPS (list printers, default printer, print a PDF), pure Rust, no dependencies" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true + +[dependencies] + +[lints] +workspace = true diff --git a/crates/ipp/README.md b/crates/ipp/README.md new file mode 100644 index 0000000000..a6c03d1113 --- /dev/null +++ b/crates/ipp/README.md @@ -0,0 +1,22 @@ +# photocraft-ipp + +A small IPP (Internet Printing Protocol, RFC 8010/8011) client for the local CUPS server, in pure +Rust with no dependencies. It does in-process what `lp` does from the command line, so PhotoCraft +can print where there is no `lp` binary (a Flatpak sandbox with `--socket=cups`, a minimal +container) and can read the printer list and the default printer first instead of handing +CUPS's errors to the user. + +```rust +let ep = photocraft_ipp::Endpoint::detect().ok_or("no CUPS server")?; // socket, then localhost:631 +let printers = photocraft_ipp::printers(&ep)?; // name, info, state, default +let id = photocraft_ipp::print_job(&ep, &photocraft_ipp::Job { printer: "Office", title: "photo", user: "me", copies: 2, document: &pdf, format: "application/pdf" })?; +``` + +Three operations: `CUPS-Get-Printers`, `CUPS-Get-Default`, `Print-Job`. HTTP/1.1 over the CUPS +domain socket (`/run/cups/cups.sock`, or `CUPS_SERVER` as a path) or TCP (`localhost:631`, or +`CUPS_SERVER` as `host[:port]`), chunked replies included. Replies are capped at 16 MiB and +parsed with bounds checks; an IPP error status becomes `Error::Status` with the server's own +message. Tests run against an in-process fake server over TCP and a Unix socket. + +Standalone: it depends on no other PhotoCraft crate. Used by `photocraft-engine`'s print +commands on Unix; Windows printing lives in `photocraft-winprint`. diff --git a/crates/ipp/src/lib.rs b/crates/ipp/src/lib.rs new file mode 100644 index 0000000000..7fc6532862 --- /dev/null +++ b/crates/ipp/src/lib.rs @@ -0,0 +1,696 @@ +//! `photocraft-ipp`: a small IPP client for CUPS, in pure Rust with no dependencies. +//! +//! Talks to the local print server the way `lp` does, but in-process: over the CUPS domain +//! socket (`/run/cups/cups.sock`, or `CUPS_SERVER` when it names a path) or TCP (`localhost:631`, +//! or `CUPS_SERVER` as `host[:port]`). Three operations, from RFC 8010 / 8011 and CUPS's own +//! extensions: list the printers, read the default, and submit a PDF as a job. +//! +//! Never panics: every read is bounded and every malformed reply is an error. Works on any Unix +//! and on Windows for TCP (CUPS rarely runs there; the Windows print path is separate). + +#![forbid(unsafe_code)] +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +use std::io::{Read, Write}; +use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; +use std::path::PathBuf; +use std::time::Duration; + +/// Where the print server listens. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Endpoint { + /// A Unix domain socket (CUPS's default, also what a Flatpak sees with `--socket=cups`). + Unix(PathBuf), + /// A TCP host and port. + Tcp(String, u16), +} + +/// Why a request failed. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Error { + /// No server could be reached. + Unreachable(String), + /// The HTTP or IPP reply could not be read. + Protocol(String), + /// The server answered with an IPP error status (the code and its message). + Status(u16, String), +} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Unreachable(m) => write!(f, "no print server: {m}"), + Error::Protocol(m) => write!(f, "print server reply: {m}"), + Error::Status(code, m) => write!(f, "print server refused ({code:#06x}): {m}"), + } + } +} + +impl std::error::Error for Error {} + +type Result = std::result::Result; + +/// A printer the server knows. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Printer { + pub name: String, + /// `printer-info`, the human description. + pub info: String, + /// `printer-state`: 3 idle, 4 printing, 5 stopped. + pub state: u32, + pub accepting_jobs: bool, + pub is_default: bool, + /// A CUPS class (a pool of printers) rather than one printer. + pub is_class: bool, +} + +/// Timeouts: connecting, and each request (a Print-Job carries the whole PDF). +const CONNECT: Duration = Duration::from_secs(3); +const REQUEST: Duration = Duration::from_secs(60); +/// The whole reply, however it trickles in: the timeouts above only cover each read. +const REPLY_DEADLINE: Duration = Duration::from_secs(120); +/// Largest reply read. +const MAX_REPLY: usize = 16 << 20; +/// Largest number of attributes taken from a reply. +const MAX_ATTRIBUTES: usize = 100_000; + +pub const OP_PRINT_JOB: u16 = 0x0002; +pub const OP_CUPS_GET_DEFAULT: u16 = 0x4001; +pub const OP_CUPS_GET_PRINTERS: u16 = 0x4002; +pub const OP_CUPS_GET_CLASSES: u16 = 0x4005; + +pub const TAG_OPERATION_GROUP: u8 = 0x01; +pub const TAG_JOB_GROUP: u8 = 0x02; +pub const TAG_END: u8 = 0x03; +pub const TAG_PRINTER_GROUP: u8 = 0x04; +pub const TAG_INTEGER: u8 = 0x21; +pub const TAG_BOOLEAN: u8 = 0x22; +pub const TAG_ENUM: u8 = 0x23; +pub const TAG_TEXT: u8 = 0x41; +pub const TAG_NAME: u8 = 0x42; +pub const TAG_KEYWORD: u8 = 0x44; +pub const TAG_URI: u8 = 0x45; +pub const TAG_CHARSET: u8 = 0x47; +pub const TAG_LANGUAGE: u8 = 0x48; +pub const TAG_MIME: u8 = 0x49; + +impl Endpoint { + /// The server this machine uses: `CUPS_SERVER` (a socket path, or `host[:port]`), else the + /// CUPS socket, else `localhost:631`. `None` when nothing answers. + pub fn detect() -> Option { + if let Some(server) = std::env::var_os("CUPS_SERVER").filter(|v| !v.is_empty()) { + let ep = Endpoint::parse(&server.to_string_lossy()); + return ep.reachable().then_some(ep); + } + [ + Endpoint::Unix(PathBuf::from("/run/cups/cups.sock")), + Endpoint::Unix(PathBuf::from("/var/run/cups/cups.sock")), + // macOS + Endpoint::Unix(PathBuf::from("/private/var/run/cupsd")), + Endpoint::Tcp("localhost".into(), 631), + ] + .into_iter() + .find(Endpoint::reachable) + } + + /// `server` as a socket path (starts with `/`) or `host[:port]` (port 631 by default). An IPv6 + /// server is `[address]` or `[address]:port`; written bare (more than one `:`) it keeps the + /// default port, because its last `:` cannot be told apart from a port separator. Anything else + /// after the bracket (`[::1]:abc`) stays the host, so a mistyped endpoint cannot resolve rather + /// than quietly becoming the default one. + pub fn parse(server: &str) -> Endpoint { + if server.starts_with('/') { + return Endpoint::Unix(PathBuf::from(server)); + } + // `[IPv6]:port` - the brackets tell the address from the port, and are not part of the host. + if let Some(rest) = server.strip_prefix('[') + && let Some((host, tail)) = rest.split_once(']') + { + // Nothing after the bracket, or a real `:port`, is understood. Anything else (`[::1]:abc`, + // `[::1]:70000`, `[::1]junk`) keeps the whole string as the host, so the mistyped endpoint + // stays unresolvable instead of quietly becoming the default one. + let port = if tail.is_empty() { + 631 + } else if let Some(port) = tail.strip_prefix(':').and_then(|p| p.parse().ok()) { + port + } else { + return Endpoint::Tcp(server.to_string(), 631); + }; + return Endpoint::Tcp(host.to_string(), port); + } + // A bare IPv6 literal has more than one ':', so splitting on the last one would cut the + // address in half: it is the host, and the port stays the default. A zone id (`fe80::1%eth0`) + // carries no ':', so there a port can still follow the address. + let host_port = if server.matches(':').count() > 1 && !server.contains('%') { None } else { server.rsplit_once(':') }; + let (host, port) = host_port.and_then(|(h, p)| p.parse().ok().map(|p| (h.to_string(), p))).unwrap_or((server.to_string(), 631)); + Endpoint::Tcp(host, port) + } + + /// Whether a connection opens. + pub fn reachable(&self) -> bool { + self.connect().is_ok() + } + + fn connect(&self) -> Result { + match self { + #[cfg(unix)] + Endpoint::Unix(path) => { + if !path.exists() { + return Err(Error::Unreachable(format!("{} does not exist", path.display()))); + } + let s = std::os::unix::net::UnixStream::connect(path).map_err(|e| Error::Unreachable(format!("{}: {e}", path.display())))?; + let _ = s.set_read_timeout(Some(REQUEST)); + let _ = s.set_write_timeout(Some(REQUEST)); + Ok(Stream::Unix(s)) + } + #[cfg(not(unix))] + Endpoint::Unix(path) => Err(Error::Unreachable(format!("{}: Unix sockets are not available here", path.display()))), + Endpoint::Tcp(host, port) => { + let addr = (host.as_str(), *port).to_socket_addrs().map_err(|e| Error::Unreachable(format!("{host}:{port}: {e}")))?.next(); + let addr = addr.ok_or_else(|| Error::Unreachable(format!("{host}:{port}: no address")))?; + open_tcp(host, *port, addr, |addr| TcpStream::connect_timeout(&addr, CONNECT)) + } + } + } + + /// The endpoint as an HTTP authority: an IPv6 host is bracketed, or its last colon reads as the + /// port separator and the request carries a host CUPS has never heard of. A scoped address keeps + /// its zone id and writes it escaped as "%25": "%" is not valid inside an authority (RFC 3986 + /// section 3.2.2 reserves it for escaping), and the escaped form is what CUPS, curl and the + /// ipp:// URIs CUPS writes use for a zone. RFC 9844 (Aug 2025) obsoleted RFC 6874, which had made + /// that form URI syntax, so this is interop rather than a standard; dropping the zone instead + /// would name a different host on the wire than the socket was opened to. The socket still gets + /// the raw address%zone, which is the form a resolver understands. + fn host(&self) -> String { + match self { + Endpoint::Unix(_) => "localhost".into(), + Endpoint::Tcp(host, port) if host.contains(':') => format!("[{}]:{port}", host.replace('%', "%25")), + Endpoint::Tcp(host, port) => format!("{host}:{port}"), + } + } +} + +impl std::fmt::Display for Endpoint { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Endpoint::Unix(p) => write!(f, "{}", p.display()), + Endpoint::Tcp(h, p) if h.contains(':') => write!(f, "[{h}]:{p}"), + Endpoint::Tcp(h, p) => write!(f, "{h}:{p}"), + } + } +} + +/// Open one address and put the request timeouts on it. The connect call is a parameter so the +/// refusal path can be tested at all: no port is guaranteed to stay free between picking one and +/// using it, and what a refused connect reported is what the caller is supposed to hear. +fn open_tcp(host: &str, port: u16, addr: SocketAddr, open: impl FnOnce(SocketAddr) -> std::io::Result) -> Result { + let s = open(addr).map_err(|e| Error::Unreachable(format!("{host}:{port}: {e}")))?; + let _ = s.set_read_timeout(Some(REQUEST)); + let _ = s.set_write_timeout(Some(REQUEST)); + Ok(Stream::Tcp(s)) +} + +enum Stream { + #[cfg(unix)] + Unix(std::os::unix::net::UnixStream), + Tcp(TcpStream), +} + +impl Read for Stream { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.read(buf), + Stream::Tcp(s) => s.read(buf), + } + } +} + +impl Write for Stream { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.write(buf), + Stream::Tcp(s) => s.write(buf), + } + } + fn flush(&mut self) -> std::io::Result<()> { + match self { + #[cfg(unix)] + Stream::Unix(s) => s.flush(), + Stream::Tcp(s) => s.flush(), + } + } +} + +/// Builds an IPP request body. +pub struct Request { + buf: Vec, +} + +impl Request { + pub fn new(op: u16, id: u32) -> Self { + let mut buf = vec![2, 0]; + buf.extend_from_slice(&op.to_be_bytes()); + buf.extend_from_slice(&id.to_be_bytes()); + buf.push(TAG_OPERATION_GROUP); + let mut r = Request { buf }; + r.attr(TAG_CHARSET, "attributes-charset", b"utf-8"); + r.attr(TAG_LANGUAGE, "attributes-natural-language", b"en"); + r + } + + /// Appends an attribute to the current group. + pub fn attr(&mut self, tag: u8, name: &str, value: &[u8]) { + // IPP lengths are 16-bit; longer names and values are cut, both the length and the bytes. + let name = name.as_bytes().get(..name.len().min(0xffff)).unwrap_or_default(); + let value = value.get(..value.len().min(0xffff)).unwrap_or_default(); + self.buf.push(tag); + self.buf.extend_from_slice(&(name.len() as u16).to_be_bytes()); + self.buf.extend_from_slice(name); + self.buf.extend_from_slice(&(value.len() as u16).to_be_bytes()); + self.buf.extend_from_slice(value); + } + + /// An attribute with several values (the extra ones carry an empty name). + pub fn attr_many(&mut self, tag: u8, name: &str, values: &[&str]) { + for (i, v) in values.iter().enumerate() { + self.attr(tag, if i == 0 { name } else { "" }, v.as_bytes()); + } + } + + pub fn integer(&mut self, name: &str, v: i32) { + self.attr(TAG_INTEGER, name, &v.to_be_bytes()); + } + + /// Starts a new attribute group. + pub fn group(&mut self, tag: u8) { + self.buf.push(tag); + } + + /// The message with its end tag. + pub fn end(mut self) -> Vec { + self.buf.push(TAG_END); + self.buf + } +} + +/// One attribute of a reply: the group it sits in, its tag, name and values. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Attribute { + pub group: u8, + pub tag: u8, + pub name: String, + pub values: Vec>, +} + +impl Attribute { + /// The first value as text. + pub fn text(&self) -> String { + self.values.first().map(|v| String::from_utf8_lossy(v).into_owned()).unwrap_or_default() + } + /// The first value as a big-endian integer. + pub fn int(&self) -> Option { + self.values.first().and_then(|v| <[u8; 4]>::try_from(v.as_slice()).ok()).map(i32::from_be_bytes) + } +} + +/// A decoded reply. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Reply { + pub status: u16, + pub request_id: u32, + pub attributes: Vec, +} + +/// The attribute range for one group delimiter in a parsed message. Ranges are half-open and +/// include empty groups, so repeated group tags remain distinct. +#[derive(Debug, Clone, Copy)] +struct GroupRange { + tag: u8, + start: usize, + end: usize, +} + +/// Parsed message details kept private so the public flat `Reply` API stays source-compatible. +struct ParsedMessage { + reply: Reply, + end: usize, + groups: Vec, +} + +impl Reply { + /// Parses an IPP message (RFC 8010 §3): header, then groups of attributes until the end tag. + pub fn parse(b: &[u8]) -> Result { + parse_message(b).map(|parsed| parsed.reply) + } + + /// Parses a message and returns where its attributes end (document data may follow). + fn parse_at(b: &[u8]) -> Result<(Reply, usize)> { + parse_message(b).map(|parsed| (parsed.reply, parsed.end)) + } + + /// The server's own message for an error status, or a name for the code. + fn status_message(&self) -> String { + self.attributes.iter().find(|a| a.name == "status-message").map(Attribute::text).filter(|m| !m.is_empty()).unwrap_or_else(|| { + match self.status { + 0x0400 => "bad request", + 0x0401 => "forbidden", + 0x0402 => "not authenticated", + 0x0403 => "not authorized", + 0x0406 => "not found", + 0x0409 => "unsupported document format", + 0x0500 => "internal server error", + 0x0506 => "the printer is not accepting jobs", + _ => "error", + } + .to_string() + }) + } +} + +/// Parses one IPP message while preserving each group instance for private consumers that need +/// object boundaries. The public reply remains a flat list of attributes. +fn parse_message(b: &[u8]) -> Result { + let bad = |m: &str| Error::Protocol(m.to_string()); + if b.len() < 9 { + return Err(bad("reply shorter than an IPP header")); + } + let status = u16::from_be_bytes([b[2], b[3]]); + let request_id = u32::from_be_bytes([b[4], b[5], b[6], b[7]]); + let mut pos = 8; + let mut group = 0u8; + let mut active_group = None; + let mut last_attribute_group = None; + let mut attributes: Vec = Vec::new(); + let mut groups: Vec = Vec::new(); + loop { + let tag = *b.get(pos).ok_or_else(|| bad("reply ends before the end-of-attributes tag"))?; + pos += 1; + if tag == TAG_END { + if let Some(last) = groups.last_mut() { + last.end = attributes.len(); + } + return Ok(ParsedMessage { reply: Reply { status, request_id, attributes }, end: pos, groups }); + } + if tag < 0x10 { + if let Some(last) = groups.last_mut() { + last.end = attributes.len(); + } + if groups.len() >= MAX_ATTRIBUTES { + return Err(bad("too many attribute groups")); + } + group = tag; + let group_index = groups.len(); + groups.push(GroupRange { tag, start: attributes.len(), end: attributes.len() }); + active_group = Some(group_index); + continue; + } + let name_len = + usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?])); + pos += 2; + let name = b.get(pos..pos + name_len).ok_or_else(|| bad("cut attribute name"))?; + pos += name_len; + let value_len = + usize::from(u16::from_be_bytes([*b.get(pos).ok_or_else(|| bad("cut attribute"))?, *b.get(pos + 1).ok_or_else(|| bad("cut attribute"))?])); + pos += 2; + let value = b.get(pos..pos + value_len).ok_or_else(|| bad("cut attribute value"))?.to_vec(); + pos += value_len; + if name_len == 0 { + // Additional values can continue an attribute only within this exact group instance. + match attributes.last_mut() { + Some(last) if last_attribute_group == active_group => last.values.push(value), + _ => return Err(bad("an additional value with no attribute before it")), + } + } else { + attributes.push(Attribute { group, tag, name: String::from_utf8_lossy(name).into_owned(), values: vec![value] }); + last_attribute_group = active_group; + } + if attributes.len() > MAX_ATTRIBUTES { + return Err(bad("too many attributes")); + } + } +} + +/// POSTs `body` (IPP, with any document data after it) to `path` and returns the parsed reply with +/// its private group boundaries. +fn post(endpoint: &Endpoint, path: &str, body: &[u8]) -> Result { + let mut stream = endpoint.connect()?; + let head = format!( + "POST {path} HTTP/1.1\r\nHost: {}\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", + endpoint.host(), + body.len() + ); + stream + .write_all(head.as_bytes()) + .and_then(|()| stream.write_all(body)) + .and_then(|()| stream.flush()) + .map_err(|e| Error::Protocol(format!("sending: {e}")))?; + let raw = read_response(&mut stream)?; + let (status, headers, body) = http_response(&raw)?; + if status != 200 { + return Err(Error::Protocol(match status { + 401 => "the print server asks for authentication (HTTP 401)".to_string(), + 403 => "the print server forbids this request (HTTP 403)".to_string(), + s => format!("HTTP {s}"), + })); + } + let body = if headers.iter().any(|(k, v)| k == "transfer-encoding" && v.contains("chunked")) { dechunk(body)? } else { body.to_vec() }; + let reply = parse_message(&body)?; + if reply.reply.status >= 0x0400 { + return Err(Error::Status(reply.reply.status, reply.reply.status_message())); + } + Ok(reply) +} + +/// Lowercase HTTP headers. +type Headers = Vec<(String, String)>; + +/// Reads one HTTP response: the headers, then as much body as `Content-Length` says, or up to +/// the last chunk, or to the end of the stream when the server says neither. A server that keeps +/// the connection open would otherwise hold the reply until the read times out. +fn read_response(stream: &mut Stream) -> Result> { + let mut raw = Vec::new(); + let mut buf = [0u8; 16 * 1024]; + let mut body_at = None; + let mut expected: Option = None; + let mut chunked = false; + // Where the chunk walk resumes, so each read costs its own bytes, not the whole body again. + let mut chunk_at = 0usize; + let started = std::time::Instant::now(); + loop { + if started.elapsed() > REPLY_DEADLINE { + return Err(Error::Protocol("the print server took too long to reply".into())); + } + let n = stream.read(&mut buf).map_err(|e| Error::Protocol(format!("reading: {e}")))?; + if n == 0 { + return Ok(raw); + } + raw.extend_from_slice(buf.get(..n).unwrap_or_default()); + if raw.len() > MAX_REPLY { + return Err(Error::Protocol("the reply is larger than 16 MiB".into())); + } + if body_at.is_none() + && let Some(end) = raw.windows(4).position(|w| w == b"\r\n\r\n") + { + body_at = Some(end + 4); + let (_, headers, _) = http_response(&raw)?; + expected = headers.iter().find(|(k, _)| k == "content-length").and_then(|(_, v)| v.parse().ok()); + chunked = headers.iter().any(|(k, v)| k == "transfer-encoding" && v.contains("chunked")); + } + let Some(at) = body_at else { continue }; + let body = raw.get(at..).unwrap_or_default(); + let complete = match expected { + Some(len) if !chunked => body.len() >= len, + _ if chunked => chunked_complete(body, &mut chunk_at), + _ => false, + }; + if complete { + if let Some(len) = expected.filter(|_| !chunked) { + raw.truncate(at + len); + } + return Ok(raw); + } + } +} + +/// Splits an HTTP/1.1 response into status, lowercase headers and body. +fn http_response(raw: &[u8]) -> Result<(u16, Headers, &[u8])> { + let end = raw.windows(4).position(|w| w == b"\r\n\r\n").ok_or_else(|| Error::Protocol("no HTTP header".into()))?; + let head = String::from_utf8_lossy(raw.get(..end).unwrap_or(&[])); + let mut lines = head.split("\r\n"); + let status_line = lines.next().unwrap_or_default(); + let status: u16 = + status_line.split_whitespace().nth(1).and_then(|s| s.parse().ok()).ok_or_else(|| Error::Protocol(format!("bad status line `{status_line}`")))?; + let headers = lines.filter_map(|l| l.split_once(':')).map(|(k, v)| (k.trim().to_ascii_lowercase(), v.trim().to_ascii_lowercase())).collect(); + Ok((status, headers, raw.get(end + 4..).unwrap_or(&[]))) +} + +/// Whether a chunked body has reached its last (zero-size) chunk, walking the chunk sizes so a +/// `0\r\n\r\n` inside a chunk's payload (an attribute value can hold it) does not count. A +/// malformed or impossible size counts as complete, so [`dechunk`] reports it instead of +/// waiting for more. +fn chunked_complete(body: &[u8], resume: &mut usize) -> bool { + let mut b = body.get(*resume..).unwrap_or_default(); + loop { + // Chunks already walked are not walked again: `resume` marks the start of this one. + *resume = body.len() - b.len(); + let Some(line_end) = b.windows(2).position(|w| w == b"\r\n") else { return false }; + let size_str = String::from_utf8_lossy(b.get(..line_end).unwrap_or(&[])); + let Ok(size) = usize::from_str_radix(size_str.split(';').next().unwrap_or_default().trim(), 16) else { return true }; + b = b.get(line_end + 2..).unwrap_or(&[]); + if size == 0 { + return true; + } + // A size past the reply cap can never be satisfied: complete, so `dechunk` rejects it + // (checked, so `ffffffffffffffff` cannot overflow the slice arithmetic). + let Some(end) = size.checked_add(2).filter(|_| size <= MAX_REPLY) else { return true }; + let Some(rest) = b.get(end..) else { return false }; + b = rest; + } +} + +/// Joins the chunks of a chunked transfer encoding. +fn dechunk(mut b: &[u8]) -> Result> { + let mut out = Vec::new(); + loop { + let line_end = b.windows(2).position(|w| w == b"\r\n").ok_or_else(|| Error::Protocol("cut chunk size".into()))?; + let size_str = String::from_utf8_lossy(b.get(..line_end).unwrap_or(&[])); + let size = usize::from_str_radix(size_str.split(';').next().unwrap_or_default().trim(), 16).map_err(|_| Error::Protocol("bad chunk size".into()))?; + b = b.get(line_end + 2..).unwrap_or(&[]); + if size == 0 { + return Ok(out); + } + let chunk = b.get(..size).ok_or_else(|| Error::Protocol("cut chunk".into()))?; + out.extend_from_slice(chunk); + if out.len() > MAX_REPLY { + return Err(Error::Protocol("the reply is larger than 16 MiB".into())); + } + b = b.get(size + 2..).ok_or_else(|| Error::Protocol("cut chunk end".into()))?; + } +} + +/// Where `/path` lands on the server: `/printers/` or `/classes/`. +fn queue_path(name: &str, is_class: bool) -> Result { + if name.is_empty() || name.bytes().any(|b| b < 0x20 || b == 0x7f) { + return Err(Error::Protocol("printer name contains an invalid control character".into())); + } + let mut encoded = String::with_capacity(name.len()); + for &b in name.as_bytes() { + if b.is_ascii_alphanumeric() || matches!(b, b'-' | b'.' | b'_' | b'~') { + encoded.push(char::from(b)); + } else { + encoded.push_str(&format!("%{b:02X}")); + } + } + Ok(format!("/{}/{encoded}", if is_class { "classes" } else { "printers" })) +} + +/// The printers the server knows (CUPS-Get-Printers), then its classes (CUPS-Get-Classes, which +/// `lp -d` accepts just the same), the default one flagged. +pub fn printers(endpoint: &Endpoint) -> Result> { + let mut out = queues(endpoint, OP_CUPS_GET_PRINTERS, false)?; + // A server without classes answers "not found"; an older server may not know the operation. + out.extend(queues(endpoint, OP_CUPS_GET_CLASSES, true).unwrap_or_default()); + let default = default_printer(endpoint).unwrap_or(None); + for p in &mut out { + p.is_default = default.as_deref() == Some(p.name.as_str()); + } + Ok(out) +} + +fn queues(endpoint: &Endpoint, op: u16, is_class: bool) -> Result> { + let mut r = Request::new(op, 1); + r.attr_many(TAG_KEYWORD, "requested-attributes", &["printer-name", "printer-info", "printer-state", "printer-is-accepting-jobs"]); + let parsed = match post(endpoint, "/", &r.end()) { + Ok(reply) => reply, + // CUPS answers "not found" ("No destinations added.") when no printer is set up. + Err(Error::Status(0x0406, _)) => return Ok(Vec::new()), + Err(e) => return Err(e), + }; + let mut out = Vec::new(); + let attributes = &parsed.reply.attributes; + for group in parsed.groups.iter().filter(|group| group.tag == TAG_PRINTER_GROUP) { + let Some(group_attributes) = attributes.get(group.start..group.end) else { + return Err(Error::Protocol("invalid printer attribute group range".into())); + }; + let mut name = None; + let mut info = String::new(); + let mut state = 3; + let mut accepting_jobs = true; + for a in group_attributes { + match a.name.as_str() { + "printer-name" if name.is_none() => name = Some(a.text()), + "printer-info" => info = a.text(), + "printer-state" => state = a.int().unwrap_or(3).clamp(0, 5) as u32, + "printer-is-accepting-jobs" => accepting_jobs = a.values.first().is_none_or(|v| v.first() != Some(&0)), + _ => {} + } + } + if let Some(name) = name.filter(|name| !name.is_empty()) { + out.push(Printer { name, info, state, accepting_jobs, is_default: false, is_class }); + } + } + Ok(out) +} + +/// The server's default printer (CUPS-Get-Default), `None` when there is none. +pub fn default_printer(endpoint: &Endpoint) -> Result> { + let mut r = Request::new(OP_CUPS_GET_DEFAULT, 2); + r.attr_many(TAG_KEYWORD, "requested-attributes", &["printer-name"]); + match post(endpoint, "/", &r.end()) { + Ok(reply) => Ok(reply.reply.attributes.iter().find(|a| a.name == "printer-name").map(Attribute::text).filter(|n| !n.is_empty())), + // CUPS answers "not found" when no default is set. + Err(Error::Status(0x0406, _)) => Ok(None), + Err(e) => Err(e), + } +} + +/// What to print. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Job<'a> { + pub printer: &'a str, + /// `printer` names a class. + pub is_class: bool, + /// Shown in the queue. + pub title: &'a str, + pub user: &'a str, + pub copies: u32, + /// The document, as `format` (`application/pdf`). + pub document: &'a [u8], + pub format: &'a str, +} + +/// Submits `job` (Print-Job) and returns the job id. +pub fn print_job(endpoint: &Endpoint, job: &Job<'_>) -> Result { + let mut r = Request::new(OP_PRINT_JOB, 3); + let path = queue_path(job.printer, job.is_class)?; + r.attr(TAG_URI, "printer-uri", format!("ipp://{}{path}", endpoint.host()).as_bytes()); + r.attr(TAG_NAME, "requesting-user-name", job.user.as_bytes()); + r.attr(TAG_NAME, "job-name", job.title.as_bytes()); + r.attr(TAG_MIME, "document-format", job.format.as_bytes()); + if job.copies > 1 { + r.buf.push(TAG_JOB_GROUP); + r.integer("copies", job.copies.clamp(1, 999) as i32); + } + let mut body = r.end(); + body.extend_from_slice(job.document); + let reply = post(endpoint, &path, &body)?.reply; + reply + .attributes + .iter() + .find(|a| a.name == "job-id") + .and_then(Attribute::int) + .map(|id| id.max(0) as u32) + .ok_or_else(|| Error::Protocol("no job id in the reply".into())) +} + +/// A request as a server sees it: the operation (the request's status field), its attributes +/// and the document data after the end tag. For fakes in tests and tools. +pub fn parse_request(b: &[u8]) -> Result<(u16, Vec, Vec)> { + let (reply, end) = Reply::parse_at(b)?; + Ok((reply.status, reply.attributes, b.get(end..).unwrap_or(&[]).to_vec())) +} + +#[cfg(test)] +mod tests; diff --git a/crates/ipp/src/tests.rs b/crates/ipp/src/tests.rs new file mode 100644 index 0000000000..14ddf43846 --- /dev/null +++ b/crates/ipp/src/tests.rs @@ -0,0 +1,653 @@ +//! The client against an in-process fake CUPS: TCP and Unix socket, content-length and chunked +//! replies, every operation, error statuses and broken replies. + +use super::*; +use std::io::{BufRead, BufReader}; +use std::sync::mpsc; + +/// What the fake received: op, attributes, document. +type Seen = (u16, Vec, Vec); + +/// A fake server answering one request with `reply_body` (IPP bytes), chunked or not, under an +/// HTTP status. Returns the endpoint and a channel with what it received. +fn fake(reply: Vec, chunked: bool, http_status: u16) -> (Endpoint, mpsc::Receiver) { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let (tx, rx) = mpsc::channel(); + std::thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + serve(Stream::Tcp(stream), reply, chunked, http_status, &tx); + }); + (Endpoint::Tcp("127.0.0.1".into(), port), rx) +} + +fn serve(stream: Stream, reply: Vec, chunked: bool, http_status: u16, tx: &mpsc::Sender) { + let mut reader = BufReader::new(stream); + let mut line = String::new(); + let mut content_length = 0usize; + let mut first = String::new(); + loop { + line.clear(); + reader.read_line(&mut line).unwrap(); + if first.is_empty() { + first = line.clone(); + } + if line == "\r\n" || line.is_empty() { + break; + } + if let Some(v) = line.to_ascii_lowercase().strip_prefix("content-length:") { + content_length = v.trim().parse().unwrap(); + } + } + if first.is_empty() { + return; // a reachability probe: connect and close + } + assert!(first.starts_with("POST "), "{first}"); + let mut body = vec![0u8; content_length]; + reader.read_exact(&mut body).unwrap(); + let _ = tx.send(parse_request(&body).unwrap()); + let mut out = reader.into_inner(); + let status_text = if http_status == 200 { "OK" } else { "Nope" }; + if chunked { + let mut resp = format!("HTTP/1.1 {http_status} {status_text}\r\nContent-Type: application/ipp\r\nTransfer-Encoding: chunked\r\n\r\n").into_bytes(); + for chunk in reply.chunks(7) { + resp.extend_from_slice(format!("{:x}\r\n", chunk.len()).as_bytes()); + resp.extend_from_slice(chunk); + resp.extend_from_slice(b"\r\n"); + } + resp.extend_from_slice(b"0\r\n\r\n"); + out.write_all(&resp).unwrap(); + } else { + out.write_all(format!("HTTP/1.1 {http_status} {status_text}\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\n\r\n", reply.len()).as_bytes()) + .unwrap(); + out.write_all(&reply).unwrap(); + } + out.flush().unwrap(); +} + +/// An attribute to write: tag, name, values. +type Attr<'a> = (u8, &'a str, &'a [&'a [u8]]); +/// A group to write: tag and attributes. +type Group<'a> = (u8, &'a [Attr<'a>]); + +/// An IPP reply body. +fn ipp(status: u16, groups: &[Group<'_>]) -> Vec { + let mut b = vec![2, 0]; + b.extend_from_slice(&status.to_be_bytes()); + b.extend_from_slice(&1u32.to_be_bytes()); + for (group, attrs) in groups { + b.push(*group); + for (tag, name, values) in *attrs { + for (i, v) in values.iter().enumerate() { + b.push(*tag); + let n = if i == 0 { name.as_bytes() } else { b"" }; + b.extend_from_slice(&(n.len() as u16).to_be_bytes()); + b.extend_from_slice(n); + b.extend_from_slice(&(v.len() as u16).to_be_bytes()); + b.extend_from_slice(v); + } + } + } + b.push(TAG_END); + b +} + +fn ok_header() -> Group<'static> { + (TAG_OPERATION_GROUP, &[(TAG_CHARSET, "attributes-charset", &[b"utf-8"]), (TAG_LANGUAGE, "attributes-natural-language", &[b"en"])]) +} + +#[test] +fn printers_are_listed_with_state_and_default() { + let three = ipp( + 0, + &[ + ok_header(), + ( + TAG_PRINTER_GROUP, + &[ + (TAG_NAME, "printer-name", &[b"Office"]), + (TAG_TEXT, "printer-info", &[b"HP in the office"]), + (TAG_ENUM, "printer-state", &[&3u32.to_be_bytes()]), + (TAG_BOOLEAN, "printer-is-accepting-jobs", &[&[1]]), + ], + ), + ( + TAG_PRINTER_GROUP, + &[ + (TAG_NAME, "printer-name", &[b"Stopped"]), + (TAG_ENUM, "printer-state", &[&5u32.to_be_bytes()]), + (TAG_BOOLEAN, "printer-is-accepting-jobs", &[&[0]]), + ], + ), + ], + ); + for chunked in [false, true] { + let (ep, rx) = fake(three.clone(), chunked, 200); + // `printers` asks for the default too; that second request reaches a server that has + // closed, so the default is simply unknown here. + let list = printers(&ep).unwrap(); + let (op, attrs, doc) = rx.recv().unwrap(); + assert_eq!(op, OP_CUPS_GET_PRINTERS); + assert!(attrs.iter().any(|a| a.name == "requested-attributes" && a.values.len() == 4 && a.tag == TAG_KEYWORD)); + assert!(doc.is_empty()); + assert_eq!(list.len(), 2, "chunked {chunked}"); + assert_eq!((list[0].name.as_str(), list[0].info.as_str(), list[0].state, list[0].accepting_jobs), ("Office", "HP in the office", 3, true)); + assert_eq!((list[1].name.as_str(), list[1].state, list[1].accepting_jobs), ("Stopped", 5, false)); + assert!(!list[0].is_default && !list[1].is_default); + } +} + +#[test] +fn printer_attributes_before_name_are_preserved() { + let state = 5u32.to_be_bytes(); + let base: [Attr<'_>; 4] = [ + (TAG_TEXT, "printer-info", &[b"Studio Printer"]), + (TAG_ENUM, "printer-state", &[&state]), + (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]), + (TAG_NAME, "printer-name", &[b"studio"]), + ]; + let body = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &base)]); + for chunked in [false, true] { + let (ep, _) = fake(body.clone(), chunked, 200); + let list = printers(&ep).unwrap(); + assert_eq!(list.len(), 1, "chunked {chunked}"); + assert_eq!((list[0].name.as_str(), list[0].info.as_str(), list[0].state, list[0].accepting_jobs), ("studio", "Studio Printer", 5, false)); + assert!(!list[0].is_class); + } +} + +#[test] +fn printer_groups_do_not_share_metadata() { + let state_a = 4u32.to_be_bytes(); + let state_b = 5u32.to_be_bytes(); + let body = ipp( + 0, + &[ + ok_header(), + ( + TAG_PRINTER_GROUP, + &[(TAG_NAME, "printer-name", &[b"first"]), (TAG_TEXT, "printer-info", &[b"First printer"]), (TAG_ENUM, "printer-state", &[&state_a])], + ), + ( + TAG_PRINTER_GROUP, + &[(TAG_TEXT, "printer-info", &[b"Second printer"]), (TAG_ENUM, "printer-state", &[&state_b]), (TAG_NAME, "printer-name", &[b"second"])], + ), + ], + ); + let (ep, _) = fake(body, false, 200); + let list = queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap(); + assert_eq!(list.len(), 2); + assert_eq!((list[0].name.as_str(), list[0].info.as_str(), list[0].state), ("first", "First printer", 4)); + assert_eq!((list[1].name.as_str(), list[1].info.as_str(), list[1].state), ("second", "Second printer", 5)); +} + +#[test] +fn printer_attribute_order_does_not_change_results() { + let state = 5u32.to_be_bytes(); + let fields: [Attr<'_>; 4] = [ + (TAG_TEXT, "printer-info", &[b"Studio Printer"]), + (TAG_ENUM, "printer-state", &[&state]), + (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]), + (TAG_NAME, "printer-name", &[b"studio"]), + ]; + let expected = Printer { name: "studio".into(), info: "Studio Printer".into(), state: 5, accepting_jobs: false, is_default: false, is_class: false }; + let mut permutations = 0; + for first in 0..4 { + for second in 0..4 { + for third in 0..4 { + for fourth in 0..4 { + let order = [first, second, third, fourth]; + if order[0] == order[1] + || order[0] == order[2] + || order[0] == order[3] + || order[1] == order[2] + || order[1] == order[3] + || order[2] == order[3] + { + continue; + } + permutations += 1; + let ordered = order.iter().map(|index| fields[*index]).collect::>(); + let body = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &ordered)]); + let (ep, _) = fake(body, false, 200); + assert_eq!(queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap(), vec![expected.clone()], "order {order:?}"); + } + } + } + } + assert_eq!(permutations, 24); +} + +#[test] +fn class_attributes_before_name_are_preserved() { + let state = 5u32.to_be_bytes(); + let body = ipp( + 0, + &[ + ok_header(), + ( + TAG_PRINTER_GROUP, + &[ + (TAG_BOOLEAN, "printer-is-accepting-jobs", &[b"\0"]), + (TAG_TEXT, "printer-info", &[b"Shared studio"]), + (TAG_ENUM, "printer-state", &[&state]), + (TAG_NAME, "printer-name", &[b"studio-class"]), + ], + ), + ], + ); + let (ep, rx) = fake(body, true, 200); + let classes = queues(&ep, OP_CUPS_GET_CLASSES, true).unwrap(); + assert_eq!(rx.recv().unwrap().0, OP_CUPS_GET_CLASSES); + assert_eq!(classes.len(), 1); + assert_eq!( + (classes[0].name.as_str(), classes[0].info.as_str(), classes[0].state, classes[0].accepting_jobs, classes[0].is_class), + ("studio-class", "Shared studio", 5, false, true) + ); +} + +#[test] +fn empty_or_unnamed_groups_do_not_leak_metadata() { + let body = ipp( + 0, + &[ + ok_header(), + (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"named"])]), + (TAG_PRINTER_GROUP, &[]), + (TAG_PRINTER_GROUP, &[(TAG_TEXT, "printer-info", &[b"orphan metadata"])]), + (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"next"]), (TAG_ENUM, "printer-state", &[&3u32.to_be_bytes()])]), + ], + ); + let (ep, _) = fake(body, false, 200); + let list = queues(&ep, OP_CUPS_GET_PRINTERS, false).unwrap(); + assert_eq!(list.len(), 2); + assert_eq!((list[0].name.as_str(), list[0].info.as_str()), ("named", "")); + assert_eq!((list[1].name.as_str(), list[1].info.as_str()), ("next", "")); +} + +#[test] +fn additional_values_do_not_cross_group_boundaries() { + let valid = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_KEYWORD, "supported", &[b"one", b"two"])])]); + let reply = Reply::parse(&valid).unwrap(); + assert_eq!(reply.attributes.iter().find(|a| a.name == "supported").map(|a| a.values.as_slice()), Some(&[b"one".to_vec(), b"two".to_vec()][..])); + + let mut body = vec![2, 0, 0, 0, 0, 0, 0, 1, TAG_PRINTER_GROUP]; + body.extend_from_slice(&[TAG_NAME, 0, 12]); + body.extend_from_slice(b"printer-name"); + body.extend_from_slice(&[0, 5]); + body.extend_from_slice(b"first"); + body.extend_from_slice(&[TAG_TEXT, 0, 12]); + body.extend_from_slice(b"printer-info"); + body.extend_from_slice(&[0, 6]); + body.extend_from_slice(b"before"); + body.push(TAG_PRINTER_GROUP); + body.extend_from_slice(&[TAG_TEXT, 0, 0, 0, 5]); + body.extend_from_slice(b"after"); + body.extend_from_slice(&[TAG_NAME, 0, 12]); + body.extend_from_slice(b"printer-name"); + body.extend_from_slice(&[0, 6]); + body.extend_from_slice(b"second"); + body.push(TAG_END); + let (ep, _) = fake(body, true, 200); + assert!(matches!(queues(&ep, OP_CUPS_GET_PRINTERS, false), Err(Error::Protocol(_)))); +} + +#[test] +fn default_printer_is_read_and_absence_is_not_an_error() { + let (ep, rx) = fake(ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"Office"])])]), false, 200); + assert_eq!(default_printer(&ep).unwrap(), Some("Office".into())); + assert_eq!(rx.recv().unwrap().0, OP_CUPS_GET_DEFAULT); + let (ep, _rx) = fake(ipp(0x0406, &[ok_header(), (TAG_OPERATION_GROUP, &[(TAG_TEXT, "status-message", &[b"No default printer"])])]), false, 200); + assert_eq!(default_printer(&ep).unwrap(), None); +} + +#[test] +fn a_print_job_carries_the_document_and_returns_its_id() { + let (ep, rx) = fake(ipp(0, &[ok_header(), (TAG_JOB_GROUP, &[(TAG_INTEGER, "job-id", &[&42u32.to_be_bytes()])])]), true, 200); + let pdf = b"%PDF-1.4 fake".to_vec(); + let id = + print_job(&ep, &Job { printer: "Office", is_class: false, title: "photo.psd", user: "diego", copies: 2, document: &pdf, format: "application/pdf" }) + .unwrap(); + assert_eq!(id, 42); + let (op, attrs, doc) = rx.recv().unwrap(); + assert_eq!(op, OP_PRINT_JOB); + assert_eq!(doc, pdf); + let get = |n: &str| attrs.iter().find(|a| a.name == n).cloned().unwrap_or_else(|| panic!("{n}")); + let uri = String::from_utf8(get("printer-uri").values[0].clone()).unwrap(); + // Spelled out here rather than echoed back from host(), so the printer-uri the server receives is + // pinned down even when the authority is an IP literal. + assert!(uri.starts_with("ipp://127.0.0.1:") && uri.ends_with("/printers/Office"), "{uri}"); + assert_eq!(get("job-name").values[0], b"photo.psd"); + assert_eq!(get("requesting-user-name").values[0], b"diego"); + assert_eq!(get("document-format").values[0], b"application/pdf"); + let copies = get("copies"); + assert_eq!((copies.group, copies.tag, copies.int()), (TAG_JOB_GROUP, TAG_INTEGER, Some(2))); + // One copy: no job attributes group at all. + let (ep, rx) = fake(ipp(0, &[ok_header(), (TAG_JOB_GROUP, &[(TAG_INTEGER, "job-id", &[&7u32.to_be_bytes()])])]), false, 200); + print_job(&ep, &Job { printer: "P", is_class: false, title: "t", user: "u", copies: 1, document: b"x", format: "application/pdf" }).unwrap(); + assert!(!rx.recv().unwrap().1.iter().any(|a| a.name == "copies")); +} + +#[test] +fn error_statuses_and_broken_replies_are_errors() { + let (ep, _rx) = fake(ipp(0x0506, &[ok_header(), (TAG_OPERATION_GROUP, &[(TAG_TEXT, "status-message", &[b"Printer is not accepting jobs"])])]), false, 200); + let e = print_job(&ep, &Job { printer: "P", is_class: false, title: "t", user: "u", copies: 1, document: b"x", format: "application/pdf" }).unwrap_err(); + assert_eq!(e, Error::Status(0x0506, "Printer is not accepting jobs".into())); + assert!(e.to_string().contains("0x0506")); + // A CUPS with no printer at all answers "not found": that is an empty list, not an error. + let (ep, _rx) = fake(ipp(0x0406, &[ok_header(), (TAG_OPERATION_GROUP, &[(TAG_TEXT, "status-message", &[b"No destinations added."])])]), false, 200); + assert_eq!(printers(&ep).unwrap(), Vec::::new()); + // HTTP 401, a reply cut short, garbage. + let (ep, _rx) = fake(ipp(0, &[ok_header()]), false, 401); + assert!(matches!(printers(&ep), Err(Error::Protocol(m)) if m.contains("401"))); + let (ep, _rx) = fake(vec![2, 0, 0, 0, 0, 0, 0, 1, TAG_OPERATION_GROUP, TAG_NAME, 0, 5], true, 200); + assert!(matches!(printers(&ep), Err(Error::Protocol(_)))); + let (ep, _rx) = fake(b"not ipp at all".to_vec(), false, 200); + assert!(matches!(printers(&ep), Err(Error::Protocol(_)))); + // Nobody reachable. A port is never guaranteed to be free, let alone to stay free: that is what + // made the old port-1 case fail on a FreeBSD CI VM. A name that can never resolve (RFC 2606) + // reaches the same exit without owning or releasing anything. + let e = printers(&Endpoint::Tcp("printer.invalid".into(), 631)).unwrap_err(); + assert!(matches!(e, Error::Unreachable(_)), "{e}"); + assert!(e.to_string().contains("printer.invalid"), "{e} does not name the server it tried"); +} + +#[test] +fn a_refused_connection_is_unreachable_and_keeps_why() { + // The refused-connect path, with the machine no longer deciding what is closed: the connect call + // is a parameter, so this test cannot race for an ephemeral port with anything. + let addr: SocketAddr = "192.0.2.9:631".parse().unwrap(); + let asked_for = std::cell::RefCell::new(None); + let open = |seen: SocketAddr| -> std::io::Result { + *asked_for.borrow_mut() = Some(seen); + Err(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "nobody is listening on this port")) + }; + let e = match open_tcp("printserver", 631, addr, open) { + Ok(_) => panic!("nothing opens {addr}"), + Err(e) => e, + }; + assert_eq!(*asked_for.borrow(), Some(addr), "the connector is handed the resolved address"); + match &e { + Error::Unreachable(m) => { + assert!(m.contains("printserver:631"), "{e} does not name the endpoint"); + assert!(m.contains("nobody is listening on this port"), "{e} loses why the connect failed"); + } + other => panic!("expected Unreachable, got {other}"), + } +} + +#[test] +fn reply_parsing_is_bounded() { + assert!(Reply::parse(&[]).is_err()); + assert!(Reply::parse(&[2, 0, 0, 0, 0, 0, 0, 1]).is_err(), "no end tag"); + let mut many = vec![2, 0, 0, 0, 0, 0, 0, 1, TAG_OPERATION_GROUP]; + for _ in 0..MAX_ATTRIBUTES + 1 { + many.extend_from_slice(&[TAG_NAME, 0, 1, b'a', 0, 0]); + } + many.push(TAG_END); + assert!(matches!(Reply::parse(&many), Err(Error::Protocol(m)) if m.contains("too many"))); + // An additional value before any attribute. + assert!(Reply::parse(&[2, 0, 0, 0, 0, 0, 0, 1, TAG_OPERATION_GROUP, TAG_NAME, 0, 0, 0, 1, b'x', TAG_END]).is_err()); + assert!(dechunk(b"zz\r\n").is_err() && dechunk(b"5\r\nab").is_err()); + assert_eq!(dechunk(b"3\r\nabc\r\n0\r\n\r\n").unwrap(), b"abc"); +} + +#[cfg(unix)] +#[test] +fn the_unix_socket_endpoint_works_and_detect_honours_cups_server() { + let dir = std::env::temp_dir().join(format!("pc-ipp-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let sock = dir.join("cups.sock"); + let listener = std::os::unix::net::UnixListener::bind(&sock).unwrap(); + let (tx, rx) = mpsc::channel(); + let reply = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"Sock"])])]); + std::thread::spawn(move || { + for _ in 0..3 { + let (stream, _) = listener.accept().unwrap(); + serve(Stream::Unix(stream), reply.clone(), false, 200, &tx); + } + }); + let ep = Endpoint::Unix(sock.clone()); + assert!(ep.reachable()); + assert_eq!(default_printer(&ep).unwrap(), Some("Sock".into())); + assert_eq!(rx.recv().unwrap().0, OP_CUPS_GET_DEFAULT); + assert!(!Endpoint::Unix(dir.join("missing.sock")).reachable()); + assert_eq!(ep.to_string(), sock.display().to_string()); + let _ = std::fs::remove_dir_all(&dir); +} + +/// An IPv6 print server is not a `host:port` split: a bare address has more than one ':', and the +/// brackets of `[address]:port` must not end up inside the host, where no resolver can read them. +#[test] +fn an_ipv6_print_server_is_parsed_as_a_host_not_as_host_port() { + assert_eq!(Endpoint::parse("::1"), Endpoint::Tcp("::1".into(), 631)); + assert_eq!(Endpoint::parse("fe80::1"), Endpoint::Tcp("fe80::1".into(), 631)); + assert_eq!(Endpoint::parse("2001:db8::1"), Endpoint::Tcp("2001:db8::1".into(), 631)); + assert_eq!(Endpoint::parse("[::1]"), Endpoint::Tcp("::1".into(), 631)); + assert_eq!(Endpoint::parse("[::1]:631"), Endpoint::Tcp("::1".into(), 631)); + assert_eq!(Endpoint::parse("[fe80::1]:6310"), Endpoint::Tcp("fe80::1".into(), 6310)); + // A zone id carries no ':', so a port can still follow it without brackets. + assert_eq!(Endpoint::parse("fe80::1%eth0:631"), Endpoint::Tcp("fe80::1%eth0".into(), 631)); + // What already worked is unchanged. + assert_eq!(Endpoint::parse("cups.example"), Endpoint::Tcp("cups.example".into(), 631)); + assert_eq!(Endpoint::parse("cups.example:6310"), Endpoint::Tcp("cups.example".into(), 6310)); + assert_eq!(Endpoint::parse("127.0.0.1:6310"), Endpoint::Tcp("127.0.0.1".into(), 6310)); + assert_eq!(Endpoint::parse("/var/run/cups/cups.sock"), Endpoint::Unix("/var/run/cups/cups.sock".into())); + // A mistyped bracketed endpoint is not quietly the default one: the whole string stays the host, + // so nothing can resolve it and detect() reports the endpoint unreachable. + assert_eq!(Endpoint::parse("[::1]:abc"), Endpoint::Tcp("[::1]:abc".into(), 631)); + assert_eq!(Endpoint::parse("[::1]:70000"), Endpoint::Tcp("[::1]:70000".into(), 631)); + assert_eq!(Endpoint::parse("[::1]junk"), Endpoint::Tcp("[::1]junk".into(), 631)); + assert!(!Endpoint::parse("[::1]:abc").reachable()); +} + +/// The authority a request carries (the `Host:` header and the `ipp://` printer-uri) has to bracket an +/// IPv6 host, or its last colon reads as the port separator and CUPS sees a nonsense host. +#[test] +fn an_ipv6_endpoint_writes_its_authority_in_brackets() { + assert_eq!(Endpoint::parse("::1").host(), "[::1]:631"); + assert_eq!(Endpoint::parse("[::1]:631").host(), "[::1]:631"); + assert_eq!(Endpoint::parse("[fe80::1]:6310").host(), "[fe80::1]:6310"); + assert_eq!(Endpoint::parse("cups.example").host(), "cups.example:631"); + assert_eq!(Endpoint::parse("cups.example:6310").host(), "cups.example:6310"); + assert_eq!(Endpoint::parse("127.0.0.1:6310").host(), "127.0.0.1:6310"); + // A scoped address keeps its zone id, but the authority writes it "%25"-escaped: a bare "%" is + // not valid inside an authority, and the escaped form is what CUPS and curl-style ipp:// URIs + // use. The host itself stays raw, because that is what the resolver gets. + assert_eq!(Endpoint::parse("fe80::1%3:631").host(), "[fe80::1%253]:631"); + assert_eq!(Endpoint::parse("[fe80::1%3]:6310").host(), "[fe80::1%253]:6310"); + assert_eq!(Endpoint::parse("fe80::1%eth0:631").host(), "[fe80::1%25eth0]:631"); + assert_eq!(Endpoint::parse("fe80::1%3:631"), Endpoint::Tcp("fe80::1%3".into(), 631)); + // What the user is shown is written the same way, with the zone exactly as it was typed. + assert_eq!(Endpoint::parse("::1").to_string(), "[::1]:631"); + assert_eq!(Endpoint::parse("cups.example:6310").to_string(), "cups.example:6310"); + assert_eq!(Endpoint::parse("fe80::1%3:631").to_string(), "[fe80::1%3]:631"); +} + +#[test] +fn requests_start_with_the_mandatory_attributes() { + let r = Request::new(OP_PRINT_JOB, 9).end(); + let (op, attrs, doc) = parse_request(&r).unwrap(); + assert_eq!(op, OP_PRINT_JOB); + assert_eq!(Reply::parse(&r).unwrap().request_id, 9); + assert_eq!(attrs[0].name, "attributes-charset"); + assert_eq!(attrs[1].name, "attributes-natural-language"); + assert!(doc.is_empty()); +} + +/// A server that keeps the connection open after its reply (a printer reached directly, a proxy) +/// must not hold the client until the read times out: the reply ends at Content-Length, or at +/// the last chunk. +#[test] +fn replies_end_at_content_length_or_the_last_chunk_without_waiting_for_close() { + for chunked in [false, true] { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let one = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"Office"])])]); + let none = ipp(0x0406, &[ok_header()]); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(stream) = stream else { return }; + let mut reader = BufReader::new(stream); + let (mut line, mut len) = (String::new(), 0usize); + loop { + line.clear(); + if reader.read_line(&mut line).unwrap_or(0) == 0 || line == "\r\n" { + break; + } + if let Some(v) = line.to_ascii_lowercase().strip_prefix("content-length:") { + len = v.trim().parse().unwrap_or(0); + } + } + if len == 0 { + continue; + } + let mut body = vec![0u8; len]; + if reader.read_exact(&mut body).is_err() { + continue; + } + let op = parse_request(&body).map(|r| r.0).unwrap_or(0); + let reply = if op == OP_CUPS_GET_PRINTERS { one.clone() } else { none.clone() }; + let mut out = reader.into_inner(); + let mut resp = Vec::new(); + if chunked { + resp.extend_from_slice(b"HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nTransfer-Encoding: chunked\r\n\r\n"); + for chunk in reply.chunks(5) { + resp.extend_from_slice(format!("{:x}\r\n", chunk.len()).as_bytes()); + resp.extend_from_slice(chunk); + resp.extend_from_slice(b"\r\n"); + } + resp.extend_from_slice(b"0\r\n\r\n"); + } else { + resp.extend_from_slice(format!("HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nContent-Length: {}\r\n\r\n", reply.len()).as_bytes()); + resp.extend_from_slice(&reply); + } + if out.write_all(&resp).and_then(|()| out.flush()).is_err() { + continue; + } + // Keep this connection open for a while, on its own thread, and go on serving. + std::thread::spawn(move || { + std::thread::sleep(Duration::from_secs(10)); + drop(out); + }); + } + }); + let ep = Endpoint::Tcp("127.0.0.1".into(), port); + let started = std::time::Instant::now(); + let list = printers(&ep).unwrap(); + assert_eq!(list.len(), 1, "chunked {chunked}"); + assert!(started.elapsed() < Duration::from_secs(5), "waited for the server to close (chunked {chunked}): {:?}", started.elapsed()); + } +} + +/// A chunked reply whose payload contains the bytes of a last chunk (`\r\n0\r\n\r\n`, here inside +/// a printer name) and arrives split right after them: the reader must wait for the real last +/// chunk instead of returning a cut reply. +#[test] +fn a_last_chunk_pattern_inside_a_value_does_not_end_the_reply() { + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let reply = ipp(0, &[ok_header(), (TAG_PRINTER_GROUP, &[(TAG_NAME, "printer-name", &[b"Of\r\n0\r\n\r\nfice"])])]); + let cut = reply.windows(7).position(|w| w == b"\r\n0\r\n\r\n").unwrap() + 7; + std::thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut reader = BufReader::new(stream); + let (mut line, mut len) = (String::new(), 0usize); + loop { + line.clear(); + if reader.read_line(&mut line).unwrap_or(0) == 0 || line == "\r\n" { + break; + } + if let Some(v) = line.to_ascii_lowercase().strip_prefix("content-length:") { + len = v.trim().parse().unwrap_or(0); + } + } + let mut body = vec![0u8; len]; + reader.read_exact(&mut body).unwrap(); + let mut out = reader.into_inner(); + let (first, second) = reply.split_at(cut); + // Part one ends exactly on the pattern, inside the first chunk's payload. + let mut a = b"HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nTransfer-Encoding: chunked\r\n\r\n".to_vec(); + a.extend_from_slice(format!("{:x}\r\n", first.len()).as_bytes()); + a.extend_from_slice(first); + out.write_all(&a).and_then(|()| out.flush()).unwrap(); + std::thread::sleep(Duration::from_millis(400)); + let mut b = b"\r\n".to_vec(); + b.extend_from_slice(format!("{:x}\r\n", second.len()).as_bytes()); + b.extend_from_slice(second); + b.extend_from_slice(b"\r\n0\r\n\r\n"); + out.write_all(&b).and_then(|()| out.flush()).unwrap(); + }); + let ep = Endpoint::Tcp("127.0.0.1".into(), port); + let list = printers(&ep).unwrap(); + assert_eq!(list.len(), 1); + assert_eq!(list[0].name, "Of\r\n0\r\n\r\nfice"); + // The boundary walk itself: complete only at a zero-size chunk, never on payload bytes. + assert!(!complete(b"5\r\nab\r\n0")); + assert!(!complete(b"7\r\n\r\n0\r\n\r\nx\r\n")); + assert!(complete(b"7\r\n\r\n0\r\n\r\nx\r\n0\r\n\r\n")); + assert!(complete(b"zz\r\n") && dechunk(b"zz\r\n").is_err()); +} + +/// [`chunked_complete`] on a whole body at once. +fn complete(body: &[u8]) -> bool { + chunked_complete(body, &mut 0) +} + +/// Fed a byte at a time, as a server dripping the reply would, the walk resumes where it stopped +/// and agrees with the one-shot answer at every length. +#[test] +fn the_chunk_walk_resumes_instead_of_starting_over() { + let body = b"3\r\nabc\r\n10\r\n0123456789abcdef\r\n0\r\n\r\n"; + let mut at = 0; + for len in 0..=body.len() { + let prefix = &body[..len]; + assert_eq!(chunked_complete(prefix, &mut at), complete(prefix), "{len}"); + assert!(at <= len); + } + assert!(at > 0, "it moved past the chunks already walked"); +} + +/// A chunk size that cannot be satisfied (`usize::MAX`, `usize::MAX - 1`, anything past the +/// reply cap) is a protocol error, through the real reader, never an overflow panic. +#[test] +fn an_impossible_chunk_size_is_a_protocol_error_not_a_panic() { + for size_line in ["ffffffffffffffff", "fffffffffffffffe", "1000001"] { + assert!(complete(format!("{size_line}\r\n").as_bytes())); + assert!(dechunk(format!("{size_line}\r\nab\r\n").as_bytes()).is_err()); + let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = listener.local_addr().unwrap().port(); + let line = size_line.to_string(); + std::thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut reader = BufReader::new(stream); + let (mut l, mut len) = (String::new(), 0usize); + loop { + l.clear(); + if reader.read_line(&mut l).unwrap_or(0) == 0 || l == "\r\n" { + break; + } + if let Some(v) = l.to_ascii_lowercase().strip_prefix("content-length:") { + len = v.trim().parse().unwrap_or(0); + } + } + let mut body = vec![0u8; len]; + reader.read_exact(&mut body).unwrap(); + let mut out = reader.into_inner(); + let resp = format!("HTTP/1.1 200 OK\r\nContent-Type: application/ipp\r\nTransfer-Encoding: chunked\r\n\r\n{line}\r\n"); + let _ = out.write_all(resp.as_bytes()).and_then(|()| out.flush()); + }); + let ep = Endpoint::Tcp("127.0.0.1".into(), port); + match printers(&ep) { + Err(Error::Protocol(_)) => {} + other => panic!("{size_line}: expected a protocol error, got {other:?}"), + } + } +} + +#[test] +fn printer_names_are_encoded_as_one_request_target_segment() { + assert_eq!(queue_path("office printer/1", false).unwrap(), "/printers/office%20printer%2F1"); + assert!(queue_path("printer\r\nX-Injected: yes", false).is_err()); +} diff --git a/crates/ops/src/lib.rs b/crates/ops/src/lib.rs index fdf6dcff9f..bc5dd20ce9 100644 --- a/crates/ops/src/lib.rs +++ b/crates/ops/src/lib.rs @@ -6,7 +6,8 @@ //! same approach Photoshop's History panel exposes to users (one state per step). //! //! Memory is bounded by `max_states` plus an approximate byte budget. The current document's -//! tiles and tiles only history holds count against it, including selection masks. +//! tiles and tiles only history holds count against it, including selection masks, and so do the +//! embedded contents of smart objects (shared `Arc` bytes, counted once however many states hold them). #![forbid(unsafe_code)] #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] @@ -33,6 +34,11 @@ pub struct HistoryState { pub doc: Arc, /// The layers targeted when this state was created. pub layers: LayerTarget, + /// The layers whose visibility the step that created this state changed, as stepping to this + /// state from the one before it in the list must apply it; see [`History::current_visibility`]. + pub visibility: Vec, + /// Stable while the state is held: indices shift when older states fall off, ids don't. + pub id: u64, } #[derive(Clone, Debug)] @@ -43,15 +49,61 @@ pub struct History { /// Lazily initialized when a byte budget is used; unlimited histories pay no counting cost. tiles: Option, pub max_states: usize, - /// Pixel memory budget in bytes for the current document plus the tiles only history holds - /// (0 = unlimited). [`History::trim`] drops the oldest states beyond it. + /// Pixel memory budget in bytes for the current document plus the tiles (and embedded smart + /// object contents) only history holds (0 = unlimited). [`History::trim`] drops the oldest states beyond it. pub max_bytes: usize, /// Label of the step that produced the current document. current_label: String, /// The layers targeted when the current document was created (opened or edited). current_layers: LayerTarget, + /// [`HistoryState::visibility`] of the current state. + current_visibility: Vec, + /// History Options › Allow Non-Linear History: a step recorded while later states exist + /// (after stepping back) keeps them and goes to the end of the list, instead of discarding + /// them; see [`History::record`] and [`History::remove`]. + pub non_linear: bool, + /// The most recent [`History::record`], while it is still the latest change to the list; + /// `None` once anything else changed it. [`History::rollback`] needs it to take the step back + /// exactly. + last_record: Option, + /// The states History panel › Clear History took off the list, kept so the next undo puts + /// them back (Clear History is itself undoable). Any new step drops them. + cleared: Option>, + /// [`HistoryState::id`] of the current state. + current_id: u64, + /// The id the next state gets: never reused, so a discarded state's id names no other. + next_id: u64, + /// The History Brush's source state, set in the History panel (`None`: the default). + brush_source: Option, } +/// What [`History::rollback`] needs to take the most recent [`History::record`] back. +#[derive(Clone, Debug)] +struct LastRecord { + /// The document it replaced (the state a rollback makes current again). + before: Arc, + /// How many former redo states it moved onto the undo side (always 0 in linear history). + moved: usize, + /// The states it (and the [`History::trim`] after it) dropped for the state limit or the + /// byte budget, in the order they were taken off, each with its index in the undo list at + /// that moment (not always the front: the History Brush's source is never dropped). + /// Their tiles are no longer counted; they are only held until the next change to the + /// list, so a rollback can put them back where they were. + evicted: Vec<(usize, HistoryState)>, +} + +/// The undo and redo states [`History::clear_states`] removed, and the History Brush source set +/// before it. +#[derive(Clone, Debug)] +struct Cleared { + undo: VecDeque, + redo: Vec, + brush_source: Option, +} + +/// The undo label of the step [`History::clear_states`] adds (Edit › Undo Clear History). +pub const CLEAR_HISTORY_LABEL: &str = "Clear History"; + impl Default for History { fn default() -> Self { Self::new(50) @@ -68,38 +120,78 @@ impl History { max_bytes: 0, current_label: "Open".into(), current_layers: LayerTarget::default(), + current_visibility: Vec::new(), + non_linear: false, + last_record: None, + cleared: None, + current_id: 0, + next_id: 1, + brush_source: None, } } /// Record that `before` was replaced by a new current document via step `label`, which left /// `layers` targeted. + /// + /// Linear history (the default) discards the redo states, as Photoshop does when you edit + /// after selecting an earlier state. With [`History::non_linear`] they are kept: the list + /// reads `…, before, the former redo states (oldest first), the new step`, so the new step is + /// appended at the end and every earlier state can still be stepped to. pub fn record(&mut self, label: impl Into, before: Arc, layers: LayerTarget) { if self.max_bytes == 0 { self.tiles = None; } + // The tile counter also counted the cleared states this step forgets: rebuild it lazily. + if self.cleared.take().is_some() { + self.tiles = None; + } if let Some(tiles) = &mut self.tiles { tiles.add(&before); } let prev_label = std::mem::replace(&mut self.current_label, label.into()); let prev_layers = std::mem::replace(&mut self.current_layers, layers); - self.undo.push_back(HistoryState { label: prev_label, doc: before, layers: prev_layers }); - self.clear_redo(); + let prev_visibility = std::mem::take(&mut self.current_visibility); + let prev_id = std::mem::replace(&mut self.current_id, self.next_id); + self.next_id = self.next_id.wrapping_add(1); + self.undo.push_back(HistoryState { label: prev_label, doc: before.clone(), layers: prev_layers, visibility: prev_visibility, id: prev_id }); + let mut moved = 0; + if self.non_linear { + // `redo` holds the next state last; the list order is oldest first. Their tiles stay + // counted: they only move from the redo side to the undo side. + while let Some(state) = self.redo.pop() { + // The new step follows the last of them in the list but was made from `before`: + // stepping back to them walks back over their steps. + merge_ids(&mut self.current_visibility, &state.visibility); + self.undo.push_back(state); + moved += 1; + } + } else { + self.clear_redo(); + } + let mut evicted = Vec::new(); while self.undo.len() > self.max_states { - if let Some(state) = self.undo.pop_front() - && let Some(tiles) = &mut self.tiles - { + // The History Brush's source doesn't fall off (as in Photoshop): the next oldest does. + let pinned = self.max_states > 1 && self.undo.front().is_some_and(|s| Some(s.id) == self.brush_source); + let at = usize::from(pinned); + let Some(state) = self.undo.remove(at) else { break }; + if let Some(tiles) = &mut self.tiles { tiles.remove(&state.doc); } + evicted.push((at, state)); } + self.last_record = Some(LastRecord { before, moved, evicted }); } pub fn can_undo(&self) -> bool { - !self.undo.is_empty() + !self.undo.is_empty() || self.cleared.is_some() } pub fn can_redo(&self) -> bool { !self.redo.is_empty() } pub fn undo_label(&self) -> Option<&str> { + if self.cleared.is_some() { + return Some(CLEAR_HISTORY_LABEL); + } self.can_undo().then_some(self.current_label.as_str()) } pub fn redo_label(&self) -> Option<&str> { @@ -116,30 +208,60 @@ impl History { self.current_layers = layers; } + /// The layers whose visibility the step that produced the current document changed, as the + /// session reported them with [`History::add_current_visibility`]. With History Options › + /// Make Layer Visibility Changes Undoable off, showing or hiding a layer is not a step, so + /// stepping through history keeps every other layer's visibility as it is; these layers take + /// the visibility of the state stepped to, since a recorded step changed them. + pub fn current_visibility(&self) -> &[LayerId] { + &self.current_visibility + } + + /// Note layers whose visibility the current step changed (after [`History::record`], and + /// again when a later change coalesces into the step). + pub fn add_current_visibility(&mut self, ids: &[LayerId]) { + merge_ids(&mut self.current_visibility, ids); + } + /// Undo: `current` becomes redoable; returns the document to make current and the layers it /// targeted when it was created. pub fn undo(&mut self, current: Arc) -> Option<(Arc, LayerTarget)> { + // Undo Clear History: the cleared states come back; the document doesn't change. + if let Some(cleared) = self.cleared.take() { + let Cleared { undo, redo, brush_source } = *cleared; + self.undo = undo; + self.redo = redo; + self.brush_source = brush_source; + self.tiles = None; + return Some((current, self.current_layers.clone())); + } let prev = self.undo.pop_back()?; + self.last_record = None; if let Some(tiles) = &mut self.tiles { tiles.remove(&prev.doc); tiles.add(¤t); } let label = std::mem::replace(&mut self.current_label, prev.label); let layers = std::mem::replace(&mut self.current_layers, prev.layers.clone()); - self.redo.push(HistoryState { label, doc: current, layers }); + let visibility = std::mem::replace(&mut self.current_visibility, prev.visibility); + let id = std::mem::replace(&mut self.current_id, prev.id); + self.redo.push(HistoryState { label, doc: current, layers, visibility, id }); Some((prev.doc, prev.layers)) } /// Redo: the mirror of [`Self::undo`]. pub fn redo(&mut self, current: Arc) -> Option<(Arc, LayerTarget)> { let next = self.redo.pop()?; + self.last_record = None; if let Some(tiles) = &mut self.tiles { tiles.remove(&next.doc); tiles.add(¤t); } let label = std::mem::replace(&mut self.current_label, next.label); let layers = std::mem::replace(&mut self.current_layers, next.layers.clone()); - self.undo.push_back(HistoryState { label, doc: current, layers }); + let visibility = std::mem::replace(&mut self.current_visibility, next.visibility); + let id = std::mem::replace(&mut self.current_id, next.id); + self.undo.push_back(HistoryState { label, doc: current, layers, visibility, id }); Some((next.doc, next.layers)) } @@ -154,6 +276,59 @@ impl History { self.undo.get(i).map(|s| s.doc.clone()) } + /// Id of History panel row `i`: the [`History::entries`] (0 = oldest, then the current state), + /// then the redo states, the next redo first ([`History::redo_labels`]). `None` past the end. + pub fn row_id(&self, i: usize) -> Option { + let past = self.undo.len(); + match i.checked_sub(past) { + None => self.undo.get(i).map(|s| s.id), + Some(0) => Some(self.current_id), + Some(k) => self.redo.iter().rev().nth(k - 1).map(|s| s.id), + } + } + + /// The History panel row of the state with id `id`, if it is still held. + pub fn row_of(&self, id: u64) -> Option { + let past = self.undo.len(); + if id == self.current_id { + return Some(past); + } + if let Some(i) = self.undo.iter().position(|s| s.id == id) { + return Some(i); + } + self.redo.iter().rev().position(|s| s.id == id).map(|k| past + 1 + k) + } + + /// Document of History panel row `i` (see [`History::row_id`]); `current` is the current + /// document, which the history does not hold. + pub fn row_doc(&self, i: usize, current: &Arc) -> Option> { + let past = self.undo.len(); + match i.checked_sub(past) { + None => self.state(i), + Some(0) => Some(current.clone()), + Some(k) => self.redo.iter().rev().nth(k - 1).map(|s| s.doc.clone()), + } + } + + /// Make row `i` the History Brush's source (the History panel's source column). It stays on + /// that state while older states fall off, and the state itself is kept when it would fall off + /// the history (by count or memory); `false` if there is no such row. + pub fn set_brush_source(&mut self, i: usize) -> bool { + let Some(id) = self.row_id(i) else { return false }; + self.brush_source = Some(id); + true + } + + /// The History Brush's source row: the oldest state held by default, else the state set with + /// [`History::set_brush_source`]. `None` once that state is gone (deleted, or discarded by a + /// new step after stepping back): as in Photoshop, there is then no source until one is set. + pub fn brush_source(&self) -> Option { + match self.brush_source { + None => Some(0), + Some(id) => self.row_of(id), + } + } + /// Number of past states (entries before the current one). pub fn past_len(&self) -> usize { self.undo.len() @@ -168,15 +343,127 @@ impl History { /// last step can no longer be undone and its pixels are released. pub fn purge_last(&mut self) -> bool { self.clear_redo(); + self.last_record = None; + if self.cleared.take().is_some() { + self.tiles = None; + return true; + } let Some(state) = self.undo.pop_back() else { return false }; if let Some(tiles) = &mut self.tiles { tiles.remove(&state.doc); } + // The current step now starts where the purged one did. + merge_ids(&mut self.current_visibility, &state.visibility); true } + /// Delete the past state at entry `i` (indexed like [`History::entries`]) and nothing else, + /// as the History panel's Delete does in non-linear history. The current state is not held + /// here (the session steps back first) and the oldest state can't be removed this way + /// either, since every later state would lose its starting point in the list. Returns + /// whether a state was removed. + pub fn remove(&mut self, i: usize) -> bool { + if i == 0 { + return false; + } + let Some(state) = self.undo.remove(i) else { return false }; + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&state.doc); + } + // The state after it now follows the one before it. + match self.undo.get_mut(i) { + Some(next) => merge_ids(&mut next.visibility, &state.visibility), + None => merge_ids(&mut self.current_visibility, &state.visibility), + } + true + } + + /// Delete the later (redo) state `j` (0 = the next redo, as [`History::redo_labels`] lists + /// them) and nothing else: the History panel's Delete on a dimmed state in non-linear + /// history. Returns whether a state was removed. + pub fn remove_later(&mut self, j: usize) -> bool { + let Some(k) = self.redo.len().checked_sub(j.saturating_add(1)) else { return false }; + let state = self.redo.remove(k); + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&state.doc); + } + // The state after it in the list (the one before it in `redo`) now follows the state + // before it. + if let Some(next) = k.checked_sub(1).and_then(|n| self.redo.get_mut(n)) { + merge_ids(&mut next.visibility, &state.visibility); + } + true + } + + /// Step back like [`History::undo`] but drop the current document instead of making it + /// redoable: the current state is deleted while the redo states (later states in the + /// list) stay. Returns the document to make current and the layers it targeted. + pub fn drop_current(&mut self) -> Option<(Arc, LayerTarget)> { + let prev = self.undo.pop_back()?; + self.last_record = None; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&prev.doc); + } + self.current_label = prev.label; + self.current_layers = prev.layers.clone(); + self.current_id = prev.id; + let dropped = std::mem::replace(&mut self.current_visibility, prev.visibility); + // The next later state now follows `prev`. + if let Some(next) = self.redo.last_mut() { + merge_ids(&mut next.visibility, &dropped); + } + Some((prev.doc, prev.layers)) + } + + /// Take back the most recent [`History::record`] exactly, for a compound command whose later + /// part failed: the document it replaced becomes current again, the current document is + /// dropped (nothing to redo), and in non-linear history the states that record moved from + /// the redo side go back there, so the list reads as before the command, with the oldest + /// states the state limit or the byte budget dropped for it back too. Returns `None` + /// (changing nothing) when something else changed the list since that record, e.g. an undo; + /// the caller then falls back to [`History::undo`]. + pub fn rollback(&mut self) -> Option<(Arc, LayerTarget)> { + let held = self.last_record.as_ref()?; + if self.undo.len().saturating_add(held.evicted.len()) <= held.moved { + return None; + } + let LastRecord { moved, evicted, .. } = self.last_record.take()?; + // Put each back where it was taken off, the last one taken off first. + for (at, state) in evicted.into_iter().rev() { + if let Some(tiles) = &mut self.tiles { + tiles.add(&state.doc); + } + self.undo.insert(at.min(self.undo.len()), state); + } + for _ in 0..moved { + // Tiles stay counted: they only move back to the redo side. + let state = self.undo.pop_back()?; + self.redo.push(state); + } + let prev = self.undo.pop_back()?; + if let Some(tiles) = &mut self.tiles { + tiles.remove(&prev.doc); + } + self.current_label = prev.label; + self.current_layers = prev.layers.clone(); + self.current_visibility = prev.visibility; + self.current_id = prev.id; + Some((prev.doc, prev.layers)) + } + + /// The document the most recent [`History::record`] replaced, while [`History::rollback`] + /// can still take that record back. + pub fn recorded_before(&self) -> Option> { + self.last_record.as_ref().map(|held| held.before.clone()) + } + /// Forget the redo states, e.g. after undoing half of a compound step that failed. pub fn clear_redo(&mut self) { + if !self.redo.is_empty() { + self.last_record = None; + } if let Some(tiles) = &mut self.tiles { for state in &self.redo { tiles.remove(&state.doc); @@ -185,28 +472,78 @@ impl History { self.redo.clear(); } + /// Forget every past and redo state (Edit › Purge › Histories). The History Brush's source + /// goes back to the default, the oldest state held (now the current one): a source set on a + /// state the purge removed would otherwise leave no source. pub fn clear(&mut self) { + self.last_record = None; + self.current_visibility.clear(); self.undo.clear(); self.redo.clear(); self.tiles = None; + self.cleared = None; + self.reset_brush_source(); + } + + /// Make the History Brush's source the default again (the oldest state held), pinning no state. + pub fn reset_brush_source(&mut self) { + self.brush_source = None; + } + + /// History panel › Clear History: take every state but the current one off the list without + /// changing the document. Unlike [`Self::clear`] (Edit › Purge › Histories) it is undoable: + /// the next [`Self::undo`] puts the states back, and the next recorded step forgets them. + /// The History Brush's source goes back to the default until that undo, as in [`Self::clear`]. + /// Returns false (and changes nothing) when the current state is the only one. + pub fn clear_states(&mut self) -> bool { + if self.undo.is_empty() && self.redo.is_empty() { + return false; + } + let undo = std::mem::take(&mut self.undo); + let redo = std::mem::take(&mut self.redo); + let brush_source = self.brush_source.take(); + self.cleared = Some(Box::new(Cleared { undo, redo, brush_source })); + self.last_record = None; + self.tiles = None; + true + } + + /// Whether the last change to the list was Clear History (so undo restores the states). + pub fn is_cleared(&self) -> bool { + self.cleared.is_some() + } + + /// The undo and redo states history holds: the cleared ones while Clear History can still be + /// undone (the list is then empty), so memory figures and the byte budget count them. + fn held(&self) -> (&VecDeque, &Vec) { + match &self.cleared { + Some(c) => (&c.undo, &c.redo), + None => (&self.undo, &self.redo), + } + } + + /// Every state history holds, cleared ones included (see [`Self::held`]). + fn held_states(&self) -> impl Iterator { + let (undo, redo) = self.held(); + undo.iter().chain(redo.iter()) } /// Approximate unique pixel bytes held by history (tiles not shared with `current`). pub fn unique_bytes(&self, current: &Document) -> usize { let mut seen = HashSet::new(); tile_bytes(current, &mut seen); - self.undo.iter().chain(self.redo.iter()).map(|s| tile_bytes(&s.doc, &mut seen)).sum() + self.held_states().map(|s| tile_bytes(&s.doc, &mut seen)).sum() } - /// Pixel bytes of `current` plus the tiles only history holds (what [`History::trim`] - /// bounds by [`History::max_bytes`]). + /// Pixel bytes of `current` plus the tiles only history holds, embedded smart object contents + /// included (what [`History::trim`] bounds by [`History::max_bytes`]). pub fn pixel_bytes(&self, current: &Document) -> usize { if let Some(tiles) = &self.tiles { return usize::try_from(tiles.bytes_with(current)).unwrap_or(usize::MAX); } let mut seen = HashSet::new(); let own = tile_bytes(current, &mut seen); - self.undo.iter().chain(self.redo.iter()).fold(own, |n, s| n.saturating_add(tile_bytes(&s.doc, &mut seen))) + self.held_states().fold(own, |n, s| n.saturating_add(tile_bytes(&s.doc, &mut seen))) } /// Bytes of the tiles only `extra` documents hold (History panel snapshots, #4530): not @@ -217,7 +554,7 @@ impl History { } let mut seen = HashSet::new(); tile_bytes(current, &mut seen); - for s in self.undo.iter().chain(self.redo.iter()) { + for s in self.held_states() { tile_bytes(&s.doc, &mut seen); } extra.iter().fold(0, |n, d| n.saturating_add(tile_bytes(d, &mut seen))) @@ -231,22 +568,31 @@ impl History { for d in keep { tile_bytes(d, &mut seen); } - self.undo.iter().chain(self.redo.iter()).fold(0, |n, s| n.saturating_add(tile_bytes(&s.doc, &mut seen))) + self.held_states().fold(0, |n, s| n.saturating_add(tile_bytes(&s.doc, &mut seen))) } /// Keep pixel memory within [`History::max_bytes`]: the current document's tiles plus the - /// tiles only history holds (newest states first). The oldest undo states that don't fit are - /// dropped; the most recent one is always kept so the last step can be undone. Returns how - /// many states were dropped. + /// tiles only history holds (newest states first), both with embedded smart object contents. + /// The oldest undo states that don't fit are dropped; the most recent one is always kept so + /// the last step can be undone. The History Brush's source state ([`History::set_brush_source`]) + /// is never dropped: its unique tiles are counted first, and the newest states fill what + /// remains. Returns how many states were dropped. pub fn trim(&mut self, current: &Document) -> usize { self.trim_with(current, &[]) } - /// [`History::trim`] counting the tiles only `pinned` documents hold (History panel - /// snapshots, which outlive the states) against the budget too, so they can't take memory - /// beyond it unseen (#4530). The most recent state is still always kept. + /// [`History::trim`] counting the tiles (and embedded smart object contents) only `pinned` + /// documents hold (History panel snapshots, which outlive the states) against the budget too, + /// so they can't take memory beyond it unseen (#4530). The most recent state is still always + /// kept. pub fn trim_with(&mut self, current: &Document, pinned: &[&Document]) -> usize { - if self.max_bytes == 0 || self.undo.len() <= 1 { + // While Clear History can be undone, the cleared states are what history holds: the + // oldest of them go first, as the list's would. The source set before it stays pinned. + let (undo, redo, brush_source) = match &mut self.cleared { + Some(c) => (&mut c.undo, &c.redo, c.brush_source), + None => (&mut self.undo, &self.redo, self.brush_source), + }; + if self.max_bytes == 0 || undo.len() <= 1 { return 0; } // An upper bound of what only `pinned` holds (some of it may be in states too): if the @@ -260,7 +606,7 @@ impl History { }; let tiles = self.tiles.get_or_insert_with(|| { let mut tiles = tiles::Tiles::default(); - for state in self.undo.iter().chain(&self.redo) { + for state in undo.iter().chain(redo) { tiles.add(&state.doc); } tiles @@ -273,580 +619,66 @@ impl History { for d in pinned { total = total.saturating_add(tile_bytes(d, &mut seen)); } - for s in self.redo.iter().rev() { + for s in redo.iter().rev() { total = total.saturating_add(tile_bytes(&s.doc, &mut seen)); } + // The History Brush's source is kept whatever its age, so its own tiles come first; the + // newest states then fill what remains of the budget (the most recent one always). + let source = brush_source.and_then(|id| undo.iter().position(|s| s.id == id)); + if let Some(state) = source.and_then(|i| undo.get(i)) { + total = total.saturating_add(tile_bytes(&state.doc, &mut seen)); + } let mut keep = 0; - for s in self.undo.iter().rev() { + for (i, s) in undo.iter().enumerate().rev() { + if Some(i) == source { + continue; + } total = total.saturating_add(tile_bytes(&s.doc, &mut seen)); - if total > self.max_bytes && keep >= 1 { + if total > self.max_bytes && (keep >= 1 || source == Some(undo.len() - 1)) { break; } keep += 1; } - let drop = self.undo.len() - keep; - for state in self.undo.drain(..drop) { - tiles.remove(&state.doc); + let mut kept = VecDeque::with_capacity(keep + 1); + let mut dropped = 0; + for (i, state) in std::mem::take(undo).into_iter().enumerate().rev() { + if Some(i) == source || kept.len() < keep + usize::from(source.is_some_and(|j| j > i)) { + kept.push_front(state); + } else { + tiles.remove(&state.doc); + dropped += 1; + // Held for a rollback of the step that pushed them out. Taking them off from the + // highest index down keeps each index valid, so a rollback can insert them back. + if let Some(held) = &mut self.last_record { + held.evicted.push((i, state)); + } + } } - drop + *undo = kept; + dropped } } -/// Bytes of the pixel tiles of `doc` (layers, masks, fill caches, smart filter masks, alpha channels, selection) not already in `seen`. +/// Add the ids of `from` that `into` lacks. +fn merge_ids(into: &mut Vec, from: &[LayerId]) { + for id in from { + if !into.contains(id) { + into.push(*id); + } + } +} + +/// Bytes of the pixel tiles of `doc` (layers, masks, fill caches, smart filter masks, alpha channels, selection) and the +/// embedded contents of its smart objects not already in `seen`. fn tile_bytes(doc: &Document, seen: &mut HashSet) -> usize { let mut n = 0usize; - tiles::surfaces(doc, |surface| { - for (_, tile) in surface.tiles() { - if seen.insert(Arc::as_ptr(tile) as usize) { - n = n.saturating_add(tile.bytes().len()); - } + tiles::blocks(doc, |ptr, len| { + if seen.insert(ptr) { + n = n.saturating_add(len); } }); n } #[cfg(test)] -mod tests { - use super::*; - use photocraft_doc::{Color, ColorMode, Layer, SampleType, Size}; - - fn base() -> Document { - Document::with_background("h", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE) - } - - /// Apply an edit the way the engine does: snapshot, mutate a clone, record. - fn edit(h: &mut History, cur: &mut Arc, label: &str, f: impl FnOnce(&mut Document)) { - let before = cur.clone(); - let mut d = (**cur).clone(); - f(&mut d); - *cur = Arc::new(d); - h.record(label, before, LayerTarget::default()); - } - - #[test] - fn undo_redo_cycle() { - let mut h = History::default(); - let mut cur = Arc::new(base()); - edit(&mut h, &mut cur, "New Layer", |d| { - d.insert_above(None, Layer::raster("L", d.pixel_format())); - }); - assert_eq!(cur.layers.len(), 2); - assert_eq!(h.undo_label(), Some("New Layer")); - cur = h.undo(cur).unwrap().0; - assert_eq!(cur.layers.len(), 1); - assert_eq!(h.redo_label(), Some("New Layer")); - cur = h.redo(cur).unwrap().0; - assert_eq!(cur.layers.len(), 2); - assert!(h.redo(cur.clone()).is_none()); - } - - #[test] - fn undo_and_redo_return_the_layers_each_state_targeted_when_created() { - let mut h = History::default(); - let open = Arc::new(base()); - let bg = open.layers[0].id; - let target = |id: LayerId| LayerTarget { active: Some(id), selected: vec![id] }; - h.set_current_layers(target(bg)); - // New Layer targets the new layer; the background is then selected (not a step) and filled. - let mut d = (*open).clone(); - let new = d.insert_above(None, Layer::raster("L", d.pixel_format())); - let layered = Arc::new(d); - h.record("New Layer", open, target(new)); - let mut d = (*layered).clone(); - d.name = "filled".into(); - h.record("Fill", layered, target(bg)); - let (d, layers) = h.undo(Arc::new(d)).unwrap(); - assert_eq!(layers, target(new), "New Layer's state targeted the new layer when created"); - let (d, layers) = h.undo(d).unwrap(); - assert_eq!(layers, target(bg), "the opened document's target"); - let (d, layers) = h.redo(d).unwrap(); - assert_eq!(layers, target(new)); - let (_, layers) = h.redo(d).unwrap(); - assert_eq!(layers, target(bg), "Fill's state targeted the background"); - } - - #[test] - fn state_accessor_matches_entries() { - let mut h = History::default(); - let mut cur = Arc::new(base()); - let open = cur.clone(); - edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); - edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); - assert_eq!(h.entries().len(), h.past_len() + 1); - assert!(Arc::ptr_eq(&h.state(0).unwrap(), &open)); - assert_eq!(h.state(1).unwrap().name, "a"); - assert!(h.state(2).is_none(), "the current document is not held"); - cur = h.undo(cur).unwrap().0; - h.undo(cur).unwrap(); - assert_eq!(h.redo_labels().collect::>(), ["A", "B"], "next redo first"); - } - - #[test] - fn new_edit_clears_redo() { - let mut h = History::default(); - let mut cur = Arc::new(base()); - edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); - cur = h.undo(cur).unwrap().0; - edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); - assert!(!h.can_redo()); - assert_eq!(h.entries(), vec!["Open".to_string(), "B".to_string()]); - } - - #[test] - fn max_states_bound() { - let mut h = History::new(3); - let mut cur = Arc::new(base()); - for i in 0..10 { - edit(&mut h, &mut cur, &format!("step {i}"), |d| d.name = format!("{i}")); - } - let mut n = 0; - while let Some((d, _)) = h.undo(cur.clone()) { - cur = d; - n += 1; - } - assert_eq!(n, 3); - assert_eq!(cur.name, "6"); - } - - #[test] - fn byte_budget_drops_oldest_states_but_keeps_one() { - let mut h = History::new(50); - let mut cur = Arc::new(base()); - let tile = 256 * 256 * 4; - // Each step repaints the background's tile: one unique tile per state. - for i in 0..6 { - edit(&mut h, &mut cur, &format!("paint {i}"), |d| { - let id = d.layers[0].id; - d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); - }); - } - assert_eq!(h.trim(&cur), 0, "unlimited by default"); - assert_eq!(h.past_len(), 6); - // The current tile plus three history tiles fit. - h.max_bytes = 4 * tile; - assert_eq!(h.trim(&cur), 3); - assert_eq!(h.past_len(), 3); - assert_eq!(h.unique_bytes(&cur), 3 * tile); - assert_eq!(h.pixel_bytes(&cur), 4 * tile, "the current tile plus history's"); - assert_eq!(h.entries()[0], "paint 2"); - // A budget smaller than one state still keeps the last step undoable. - h.max_bytes = 1; - h.trim(&cur); - assert_eq!(h.past_len(), 1); - assert!(h.undo(cur.clone()).is_some()); - } - - /// Snapshot-only tiles count against the budget (#4530); tiles a snapshot shares with the - /// document or a state count once, and a purge frees only what no snapshot keeps. - #[test] - fn snapshot_tiles_count_against_the_budget() { - let mut h = History::new(50); - let mut cur = Arc::new(base()); - let tile = 256 * 256 * 4; - let paint = |d: &mut Document, v: f32| { - let id = d.layers[0].id; - d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[v, 0.0, 0.0, 1.0]); - }; - // A snapshot holding a tile nothing else does, then one sharing the first state's tile. - let mut own = (*cur).clone(); - paint(&mut own, 0.9); - edit(&mut h, &mut cur, "paint 0", |d| paint(d, 0.1)); - let shared = h.state(0).unwrap(); - for i in 1..6 { - edit(&mut h, &mut cur, &format!("paint {i}"), |d| paint(d, i as f32 / 8.0)); - } - let snaps = [&own, &*shared]; - assert_eq!(h.extra_bytes(&cur, &snaps), tile, "the shared tile is history's"); - assert_eq!(h.extra_bytes(&cur, &[]), 0); - assert_eq!(h.unique_bytes(&cur), 6 * tile); - assert_eq!(h.unique_bytes_besides(&cur, &snaps), 5 * tile, "clearing keeps the shared tile"); - // Both snapshot tiles stay whatever is trimmed (the shared one too), so beside the - // current tile they leave room for two history tiles; without them, four would fit. - h.max_bytes = 5 * tile; - assert_eq!(h.trim_with(&cur, &snaps), 4); - assert_eq!(h.past_len(), 2); - assert_eq!(h.pixel_bytes(&cur) + h.extra_bytes(&cur, &snaps), 5 * tile); - // Still keeps the last step undoable. - h.max_bytes = 1; - h.trim_with(&cur, &snaps); - assert_eq!(h.past_len(), 1); - } - - #[test] - fn history_shares_untouched_tiles() { - let mut h = History::default(); - let mut cur = Arc::new(base()); - assert_eq!(h.unique_bytes(&cur), 0); - // rename only: no pixel changes, history holds no unique tiles - edit(&mut h, &mut cur, "Rename", |d| d.name = "x".into()); - assert_eq!(h.unique_bytes(&cur), 0); - // paint one pixel: exactly one tile becomes unique to history - edit(&mut h, &mut cur, "Paint", |d| { - let id = d.layers[0].id; - d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[0.0, 0.0, 0.0, 1.0]); - }); - assert_eq!(h.unique_bytes(&cur), 256 * 256 * 4); - } - - #[test] - fn fill_caches_and_smart_filter_masks_count_toward_history_bytes() { - use photocraft_doc::{Affine, Fill, FillCache, LayerContent, LayerMask, PixelFormat, SmartObject, SmartSource, Surface}; - let mut doc = Document::new("fills", Size::new(64, 64), ColorMode::Rgb, SampleType::U8); - let mut fill = Layer::new("Fill", LayerContent::Fill(Fill::Solid(Color::WHITE))); - let mut cache = Surface::new(PixelFormat::GRAY8); - cache.write_pixel(1, 1, &[1.0]); - fill.fill_cache = Some(FillCache { fill: Fill::Solid(Color::WHITE), surface: cache }); - doc.insert_above(None, fill); - let mut smart_obj = SmartObject::new(SmartSource::Linked { path: "a.png".into() }, Affine::IDENTITY, None); - let mut mask = LayerMask::reveal_all(); - mask.surface.write_pixel(1, 1, &[0.5]); - smart_obj.filter_mask = Some(mask); - doc.insert_above(None, Layer::new("Smart", LayerContent::Smart(smart_obj))); - let tile = 256 * 256; - let mut cur = Arc::new(doc); - let mut h = History { max_bytes: usize::MAX, ..History::default() }; - assert_eq!(h.pixel_bytes(&cur), 2 * tile, "fill cache and smart filter mask are counted"); - edit(&mut h, &mut cur, "Rename", |doc| doc.name = "renamed".into()); - assert_eq!(h.pixel_bytes(&cur), 2 * tile, "shared tiles count once"); - edit(&mut h, &mut cur, "Fill cache", |doc| { - let id = doc.layers[0].id; - if let Some(cache) = doc.layer_mut(id).and_then(|l| l.fill_cache.as_mut()) { - cache.surface.write_pixel(1, 1, &[0.0]); - } - }); - assert_eq!(h.pixel_bytes(&cur), 3 * tile, "the copied fill cache tile is history-only"); - assert_eq!(h.unique_bytes(&cur), tile); - h.trim(&cur); - assert_counter(&h, &cur); - h.max_bytes = 2 * tile; - assert_eq!(h.trim(&cur), 1, "the copied fill cache tile pushes history over budget"); - assert_eq!(h.pixel_bytes(&cur), 3 * tile, "the most recent undo state is always kept"); - assert_counter(&h, &cur); - } - - #[test] - fn selection_tiles_count_current_and_history_without_double_counting() { - use photocraft_doc::{PixelFormat, Surface}; - for depth in SampleType::ALL { - let mut doc = Document::new("selection", Size::new(300, 64), ColorMode::Rgb, depth); - let mut selection = Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false)); - selection.write_pixel(1, 1, &[1.0]); - selection.write_pixel(257, 1, &[1.0]); - doc.selection = Some(selection); - let mut cur = Arc::new(doc); - let tile = match depth { - SampleType::U8 => 65_536, - SampleType::U16 => 131_072, - SampleType::F32 => 262_144, - }; - let mut h = History { max_bytes: usize::MAX, ..History::default() }; - assert_eq!(h.pixel_bytes(&cur), 2 * tile, "current selection counts without any history"); - assert_eq!(h.unique_bytes(&cur), 0); - edit(&mut h, &mut cur, "Rename", |doc| doc.name = "renamed".into()); - assert_eq!(h.pixel_bytes(&cur), 2 * tile, "the shared selection tiles count once"); - edit(&mut h, &mut cur, "Selection", |doc| { - doc.selection.as_mut().unwrap().write_pixel(1, 1, &[0.5]); - }); - assert_eq!(h.pixel_bytes(&cur), 3 * tile, "only the changed selection tile is copied"); - assert_eq!(h.unique_bytes(&cur), tile); - h.trim(&cur); - assert_counter(&h, &cur); - let selected_value = cur.selection.as_ref().unwrap().sample_channel(1, 1, 0); - edit(&mut h, &mut cur, "Deselect", |doc| doc.selection = None); - assert_eq!(h.pixel_bytes(&cur), 3 * tile, "deselecting leaves the previous masks in history"); - assert_eq!(h.unique_bytes(&cur), 3 * tile); - assert_counter(&h, &cur); - cur = h.undo(cur).unwrap().0; - assert_eq!(cur.selection.as_ref().unwrap().sample_channel(1, 1, 0), selected_value); - assert_eq!(h.pixel_bytes(&cur), 3 * tile); - assert_eq!(h.unique_bytes(&cur), tile); - assert_counter(&h, &cur); - // Coalescing can change the current mask without recording another state. - Arc::make_mut(&mut cur).selection.as_mut().unwrap().write_pixel(257, 1, &[0.25]); - assert_eq!(h.pixel_bytes(&cur), 4 * tile); - assert_eq!(h.trim(&cur), 0); - assert_counter(&h, &cur); - cur = h.redo(cur).unwrap().0; - assert!(cur.selection.is_none()); - assert_eq!(h.pixel_bytes(&cur), 4 * tile); - assert_counter(&h, &cur); - } - } - - #[test] - fn selection_byte_budget_drops_old_masks_and_keeps_the_last_undo() { - use photocraft_doc::{PixelFormat, Surface}; - for depth in SampleType::ALL { - let mut doc = Document::new("selection", Size::new(64, 64), ColorMode::Rgb, depth); - doc.selection = Some(Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false))); - let mut cur = Arc::new(doc); - let tile = 256 * 256 * depth.bytes(); - let mut h = History::default(); - for i in 1..=6 { - edit(&mut h, &mut cur, "Selection", |doc| { - doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0]); - }); - } - assert_eq!(h.pixel_bytes(&cur), 6 * tile); - h.max_bytes = 3 * tile; - assert_eq!(h.trim(&cur), 4); - assert_eq!(h.past_len(), 2); - assert_eq!(h.pixel_bytes(&cur), 3 * tile); - assert_eq!(h.unique_bytes(&cur), 2 * tile); - assert_counter(&h, &cur); - h.max_bytes = 1; - assert_eq!(h.trim(&cur), 1); - assert_eq!(h.past_len(), 1, "the most recent undo survives even below one mask's size"); - assert_eq!(h.pixel_bytes(&cur), 2 * tile); - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - } - } - - fn assert_counter(h: &History, current: &Document) { - let counter = h.tiles.as_ref().expect("budget initializes the counter"); - let total = tile_bytes(current, &mut HashSet::new()).saturating_add(h.unique_bytes(current)); - assert_eq!(counter.bytes_with(current), total as u128); - assert_eq!(h.pixel_bytes(current), total); - let mut seen = HashSet::new(); - let retained: usize = h.undo.iter().chain(&h.redo).map(|s| tile_bytes(&s.doc, &mut seen)).sum(); - let empty = Document::new("empty", Size::new(1, 1), ColorMode::Rgb, SampleType::U8); - assert_eq!(counter.bytes_with(&empty), retained as u128); - } - - fn video_document(depth: SampleType, frames: usize) -> Document { - let mut doc = Document::new("video", Size::new(64, 64), ColorMode::Rgb, depth); - let mut layer = Layer::raster("Video", doc.pixel_format()); - let footage: Vec<_> = (0..frames) - .map(|i| { - let mut surface = photocraft_doc::Surface::new(doc.pixel_format()); - surface.write_pixel(1, 1, &[i as f32 / frames as f32, 0.0, 0.0, 1.0]); - surface - }) - .collect(); - layer.content = photocraft_doc::LayerContent::Raster(footage[0].clone()); - layer.video = Some(photocraft_doc::VideoData::new(footage, 24.0)); - doc.layers.push(layer); - doc - } - - #[test] - fn video_memory_counts_originals_and_deduplicates_displayed_frames() { - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - let mut doc = video_document(depth, 3); - let tile = doc.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len(); - let video = doc.layers[0].video.as_mut().unwrap(); - video.original_frames = Some(video.frames.clone()); - // Original footage and the displayed frame alias the stack, except this painted frame. - video.frames[2].write_pixel(1, 1, &[0.0, 1.0, 0.0, 1.0]); - let mut cur = Arc::new(doc); - let mut h = History { max_bytes: usize::MAX, ..History::default() }; - assert_eq!(h.pixel_bytes(&cur), 4 * tile); - for name in ["A", "B"] { - edit(&mut h, &mut cur, "Rename", |doc| doc.name = name.into()); - h.trim(&cur); - } - assert_counter(&h, &cur); - assert_eq!(h.pixel_bytes(&cur), 4 * tile); - assert_eq!(h.unique_bytes(&cur), 0); - // Rasterizing keeps the displayed frame; the rest of the clip survives only in undo. - edit(&mut h, &mut cur, "Rasterize", |doc| doc.layers[0].video = None); - assert_eq!(h.unique_bytes(&cur), 3 * tile); - assert_eq!(h.pixel_bytes(&cur), 4 * tile); - assert_counter(&h, &cur); - cur = h.undo(cur).unwrap().0; - assert_eq!(h.unique_bytes(&cur), 0); - assert_counter(&h, &cur); - cur = h.redo(cur).unwrap().0; - assert_eq!(h.unique_bytes(&cur), 3 * tile); - assert_counter(&h, &cur); - h.clear(); - assert_eq!(h.pixel_bytes(&cur), tile); - } - } - - #[test] - fn video_memory_budget_evicts_deleted_off_playhead_frames() { - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - let mut cur = Arc::new(video_document(depth, 6)); - let tile = cur.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len(); - let mut h = History::default(); - for _ in 0..4 { - edit(&mut h, &mut cur, "Delete Frame", |doc| { - doc.layers[0].video.as_mut().unwrap().frames.remove(1); - }); - } - assert_eq!(h.pixel_bytes(&cur), 6 * tile); - assert_eq!(h.unique_bytes(&cur), 4 * tile); - h.max_bytes = 3 * tile; - assert_eq!(h.trim(&cur), 3); - assert_eq!(h.past_len(), 1); - assert_eq!(h.pixel_bytes(&cur), 3 * tile); - assert_eq!(h.unique_bytes(&cur), tile); - assert_counter(&h, &cur); - cur = h.undo(cur).unwrap().0; - assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 3); - assert_counter(&h, &cur); - cur = h.redo(cur).unwrap().0; - assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 2); - assert_counter(&h, &cur); - // A budget below the remaining clip still keeps the latest step undoable. - h.max_bytes = 1; - assert_eq!(h.trim(&cur), 0); - assert!(h.can_undo()); - } - } - - /// A pattern fill or layer style copies its pattern into the document, so the pattern's tiles - /// count like layer pixels, shared ones once; undoing that step leaves them to the redo states. - #[test] - fn document_patterns_count_as_document_pixels() { - use photocraft_doc::{Pattern, Surface}; - for depth in SampleType::ALL { - let tile = 4 * 256 * 256 * depth.bytes(); - let mut cur = Arc::new(Document::new("patterns", Size::new(64, 64), ColorMode::Rgb, depth)); - let mut h = History { max_bytes: usize::MAX, ..History::default() }; - for name in ["A", "B"] { - edit(&mut h, &mut cur, "Rename", |doc| doc.name = name.into()); - h.trim(&cur); - } - let mut surface = Surface::new(cur.pixel_format()); - surface.write_pixel(1, 1, &[0.0, 0.0, 1.0, 1.0]); - let pattern = Pattern::new("Pattern", surface, 4, 4); - edit(&mut h, &mut cur, "Pattern Fill", |doc| doc.patterns.push(pattern)); - assert_eq!(h.pixel_bytes(&cur), tile, "the document's pattern"); - assert_eq!(h.unique_bytes(&cur), 0); - assert_counter(&h, &cur); - edit(&mut h, &mut cur, "Rename", |doc| doc.name = "C".into()); - assert_eq!(h.pixel_bytes(&cur), tile, "shared with history, it counts once"); - assert_counter(&h, &cur); - for _ in 0..2 { - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - } - assert!(cur.patterns.is_empty()); - assert_eq!(h.unique_bytes(&cur), tile, "only the redo states hold the pattern"); - assert_eq!(h.pixel_bytes(&cur), tile); - cur = h.redo(cur).unwrap().0; - assert_eq!(h.unique_bytes(&cur), 0); - assert_counter(&h, &cur); - h.clear(); - assert_eq!(h.pixel_bytes(&cur), tile); - } - } - - #[test] - fn counter_tracks_eviction_undo_redo_branching_purge_and_clones() { - for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { - let mut cur = Arc::new(Document::with_background("h", Size::new(300, 64), ColorMode::Rgb, depth, Color::WHITE)); - Arc::make_mut(&mut cur).selection = Some(photocraft_doc::Surface::new(photocraft_doc::PixelFormat::GRAY8)); - let mut h = History::new(4); - h.max_bytes = usize::MAX; - for i in 0..8 { - edit(&mut h, &mut cur, "Paint", |doc| { - doc.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0, 0.0, 0.0, 1.0]); - doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0]); - }); - h.trim(&cur); - if i >= 1 { - assert_counter(&h, &cur); - } - } - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - cur = h.redo(cur).unwrap().0; - assert_counter(&h, &cur); - let copy = h.clone(); - edit(&mut h, &mut cur, "Branch", |doc| doc.name = "branch".into()); - assert!(!h.can_redo()); - assert_counter(&h, &cur); - assert_counter(©, &cur); - cur = h.undo(cur).unwrap().0; - h.clear_redo(); - assert_counter(&h, &cur); - h.purge_last(); - assert_counter(&h, &cur); - h.clear(); - assert!(h.tiles.is_none()); - assert_eq!(h.pixel_bytes(&cur), tile_bytes(&cur, &mut HashSet::new())); - } - } - - #[test] - fn shared_masks_channels_and_repeated_snapshot_references_count_once() { - use photocraft_doc::{AlphaChannel, LayerMask, PixelFormat, Surface}; - let mut doc = base(); - let mut gray = Surface::new(PixelFormat::GRAY8); - gray.write_pixel(1, 1, &[1.0]); - doc.layers[0].mask = Some(LayerMask { surface: gray.clone(), ..LayerMask::reveal_all() }); - doc.channels.push(AlphaChannel::new("alpha", gray.clone())); - doc.selection = Some(gray.clone()); - doc.quick_mask = Some(AlphaChannel::new("quick", gray)); - doc.layers.push(doc.layers[0].clone()); - let mut cur = Arc::new(doc); - let bytes = tile_bytes(&cur, &mut HashSet::new()); - let mut h = History::new(50); - // Counting current separately would double count its tiles; the exact union fits. - h.max_bytes = bytes; - for _ in 0..6 { - h.record("Same", cur.clone(), LayerTarget::default()); - assert_eq!(h.trim(&cur), 0); - } - assert_counter(&h, &cur); - for _ in 0..3 { - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - } - h.clear_redo(); - h.purge_last(); - assert_counter(&h, &cur); - assert_eq!(h.pixel_bytes(&cur), bytes); - } - - #[test] - fn threshold_rechecks_coalesced_current_and_changed_budget() { - let mut h = History::new(50); - let mut cur = Arc::new(base()); - h.max_bytes = usize::MAX; - for i in 0..6 { - edit(&mut h, &mut cur, "Paint", |doc| { - doc.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0, 0.0, 0.0, 1.0]); - }); - h.trim(&cur); - } - // A coalesced edit may change current without recording another history state. - Arc::make_mut(&mut cur).layers[0].surface_mut().unwrap().write_pixel(300, 1, &[1.0; 4]); - let tile = 256 * 256 * 4; - h.max_bytes = 4 * tile; - assert_eq!(h.trim(&cur), 4); - assert_eq!(h.past_len(), 2); - assert_counter(&h, &cur); - cur = h.undo(cur).unwrap().0; - assert_counter(&h, &cur); - h.max_bytes = 0; - edit(&mut h, &mut cur, "Unlimited", |doc| doc.name = "u".into()); - assert!(h.tiles.is_none()); - h.max_bytes = 1; - h.trim(&cur); - assert_eq!(h.past_len(), 1, "always retain the most recent undo"); - assert_counter(&h, &cur); - } - - #[test] - fn transferred_tiles_cannot_make_the_budget_check_underestimate_current() { - use photocraft_doc::{PixelFormat, Surface}; - let before = Arc::new(base()); - let mut current = (*before).clone(); - let mut large = Surface::new(PixelFormat::new(ColorMode::Cmyk, SampleType::F32, true)); - large.write_pixel(256, 0, &[1.0; 5]); - current.layers[0].surface_mut().unwrap().put_tiles(large.take_tiles(photocraft_doc::Rect::from_xywh(256, 0, 1, 1))); - let mut h = History::new(50); - h.max_bytes = tile_bytes(¤t, &mut HashSet::new()) - 1; - h.record("A", before.clone(), LayerTarget::default()); - h.record("B", before, LayerTarget::default()); - assert_eq!(h.trim(¤t), 1, "current alone exceeds the budget; only the mandatory undo survives"); - assert_counter(&h, ¤t); - } -} +mod tests; diff --git a/crates/ops/src/tests.rs b/crates/ops/src/tests.rs new file mode 100644 index 0000000000..a99d1d557a --- /dev/null +++ b/crates/ops/src/tests.rs @@ -0,0 +1,1215 @@ +use super::*; +use photocraft_doc::{Color, ColorMode, Layer, SampleType, Size}; + +fn base() -> Document { + Document::with_background("h", Size::new(64, 64), ColorMode::Rgb, SampleType::U8, Color::WHITE) +} + +/// Apply an edit the way the engine does: snapshot, mutate a clone, record. +fn edit(h: &mut History, cur: &mut Arc, label: &str, f: impl FnOnce(&mut Document)) { + let before = cur.clone(); + let mut d = (**cur).clone(); + f(&mut d); + *cur = Arc::new(d); + h.record(label, before, LayerTarget::default()); +} + +#[test] +fn undo_redo_cycle() { + let mut h = History::default(); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "New Layer", |d| { + d.insert_above(None, Layer::raster("L", d.pixel_format())); + }); + assert_eq!(cur.layers.len(), 2); + assert_eq!(h.undo_label(), Some("New Layer")); + cur = h.undo(cur).unwrap().0; + assert_eq!(cur.layers.len(), 1); + assert_eq!(h.redo_label(), Some("New Layer")); + cur = h.redo(cur).unwrap().0; + assert_eq!(cur.layers.len(), 2); + assert!(h.redo(cur.clone()).is_none()); +} + +#[test] +fn undo_and_redo_return_the_layers_each_state_targeted_when_created() { + let mut h = History::default(); + let open = Arc::new(base()); + let bg = open.layers[0].id; + let target = |id: LayerId| LayerTarget { active: Some(id), selected: vec![id] }; + h.set_current_layers(target(bg)); + // New Layer targets the new layer; the background is then selected (not a step) and filled. + let mut d = (*open).clone(); + let new = d.insert_above(None, Layer::raster("L", d.pixel_format())); + let layered = Arc::new(d); + h.record("New Layer", open, target(new)); + let mut d = (*layered).clone(); + d.name = "filled".into(); + h.record("Fill", layered, target(bg)); + let (d, layers) = h.undo(Arc::new(d)).unwrap(); + assert_eq!(layers, target(new), "New Layer's state targeted the new layer when created"); + let (d, layers) = h.undo(d).unwrap(); + assert_eq!(layers, target(bg), "the opened document's target"); + let (d, layers) = h.redo(d).unwrap(); + assert_eq!(layers, target(new)); + let (_, layers) = h.redo(d).unwrap(); + assert_eq!(layers, target(bg), "Fill's state targeted the background"); +} + +#[test] +fn state_accessor_matches_entries() { + let mut h = History::default(); + let mut cur = Arc::new(base()); + let open = cur.clone(); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert_eq!(h.entries().len(), h.past_len() + 1); + assert!(Arc::ptr_eq(&h.state(0).unwrap(), &open)); + assert_eq!(h.state(1).unwrap().name, "a"); + assert!(h.state(2).is_none(), "the current document is not held"); + cur = h.undo(cur).unwrap().0; + h.undo(cur).unwrap(); + assert_eq!(h.redo_labels().collect::>(), ["A", "B"], "next redo first"); +} + +/// The History Brush source is a state, not an index: it stays on its state through undo, +/// redo and older states falling off, is kept when it would fall off itself, and is gone +/// (no source) once a new step discards it. +#[test] +fn brush_source_follows_its_state() { + let mut h = History::new(3); + let mut cur = Arc::new(base()); + assert_eq!(h.brush_source(), Some(0), "default: the oldest state"); + assert!(!h.set_brush_source(1), "no such row yet"); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert!(h.set_brush_source(1)); + assert_eq!(h.row_doc(1, &cur).unwrap().name, "a"); + // Undo / redo: the rows stay put, and A's state is still row 1. + cur = h.undo(cur).unwrap().0; + assert_eq!((h.brush_source(), h.row_doc(1, &cur).unwrap().name.as_str()), (Some(1), "a")); + assert_eq!(h.row_doc(2, &cur).unwrap().name, "b", "a redo row"); + cur = h.redo(cur).unwrap().0; + // Open falls off the 3-state limit: A's state moves up to row 0 and stays the source. + edit(&mut h, &mut cur, "C", |d| d.name = "c".into()); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.entries(), ["A", "B", "C", "D"]); + assert_eq!(h.brush_source(), Some(0)); + // A would fall off next: it is kept, and the states after it fall off instead. + edit(&mut h, &mut cur, "E", |d| d.name = "e".into()); + edit(&mut h, &mut cur, "F", |d| d.name = "f".into()); + assert_eq!(h.entries(), ["A", "D", "E", "F"]); + assert_eq!((h.brush_source(), h.row_doc(0, &cur).unwrap().name.as_str()), (Some(0), "a")); + // A redo state as the source, discarded by a new step: no source until one is set. + cur = h.undo(cur).unwrap().0; + assert!(h.set_brush_source(3), "the greyed F row"); + assert_eq!((h.brush_source(), h.row_doc(3, &cur).unwrap().name.as_str()), (Some(3), "f")); + assert!(h.row_doc(4, &cur).is_none() && h.row_id(4).is_none()); + edit(&mut h, &mut cur, "G", |d| d.name = "g".into()); + assert_eq!(h.brush_source(), None); + // The current state as the source keeps it once it becomes a past state; once it is no + // longer the source, it falls off as usual. + assert!(h.set_brush_source(3)); + edit(&mut h, &mut cur, "H", |d| d.name = "h".into()); + assert_eq!((h.brush_source(), h.row_doc(2, &cur).unwrap().name.as_str()), (Some(2), "g")); + assert!(h.set_brush_source(1)); + edit(&mut h, &mut cur, "I", |d| d.name = "i".into()); + assert_eq!(h.entries(), ["E", "G", "H", "I"]); +} + +#[test] +fn byte_budget_keeps_the_brush_source() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert!(h.set_brush_source(1)); + h.max_bytes = 1; + let dropped = h.trim(&cur); + assert_eq!(h.entries(), ["paint 0", "paint 3", "paint 4"], "the source is kept with the last step"); + assert_eq!(dropped, 3); + assert_eq!(h.brush_source(), Some(0)); +} + +/// The pinned source counts against the byte budget: with room for three states, the +/// source and the two newest are kept, never more than the budget (#3536 review). +#[test] +fn byte_budget_counts_the_brush_source_first() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + let tile = h.pixel_bytes(&cur); + assert!(tile > 0); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + // Every state holds one tile of its own; the source is the oldest (Open). + assert!(h.set_brush_source(0)); + h.max_bytes = tile * 4; + assert_eq!(h.trim(&cur), 2); + assert_eq!(h.entries(), ["Open", "paint 2", "paint 3", "paint 4"]); + assert!(h.pixel_bytes(&cur) <= h.max_bytes, "{} > {}", h.pixel_bytes(&cur), h.max_bytes); + assert_eq!(h.trim(&cur), 0, "it fits now"); + // A new step trims to the budget again, still keeping the source. + edit(&mut h, &mut cur, "paint 5", |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[1.0, 0.0, 0.0, 1.0]); + }); + assert_eq!(h.trim(&cur), 1); + assert_eq!(h.entries(), ["Open", "paint 3", "paint 4", "paint 5"]); + assert!(h.pixel_bytes(&cur) <= h.max_bytes); + assert_eq!(h.brush_source(), Some(0)); +} + +/// Edit › Purge › Histories clears every state: a source set on one of them goes back to the +/// default (the oldest state held) instead of leaving no source (#3536 review). +#[test] +fn clear_resets_the_brush_source() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert!(h.set_brush_source(1)); + h.clear(); + assert_eq!(h.brush_source(), Some(0)); + assert_eq!(h.row_doc(0, &cur).unwrap().name, "b"); +} + +/// History panel › Clear History leaves a usable (default) source, and its undo restores the +/// source set on a past state. +#[test] +fn clear_states_resets_the_brush_source_until_undo() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert!(h.set_brush_source(1)); + assert!(h.clear_states()); + assert_eq!(h.brush_source(), Some(0)); + assert_eq!(h.row_doc(0, &cur).unwrap().name, "b"); + cur = h.undo(cur).unwrap().0; + assert_eq!((h.brush_source(), h.row_doc(1, &cur).unwrap().name.as_str()), (Some(1), "a")); +} + +/// The byte budget keeps the source set before Clear History among the cleared states, so +/// Undo Clear History brings it back as the History Brush's source. +#[test] +fn byte_budget_keeps_the_brush_source_of_the_cleared_states() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert!(h.set_brush_source(1)); + let source = h.state(1).unwrap(); + assert!(h.clear_states()); + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 3); + assert_counter(&h, &cur); + assert!(h.is_cleared() && h.past_len() == 0, "the list stays cleared"); + cur = h.undo(cur).unwrap().0; + assert_eq!(h.entries(), ["paint 0", "paint 3", "paint 4"], "the source is kept with the last step"); + assert_eq!(h.brush_source(), Some(0)); + assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source), "the History Brush paints from the same state"); +} + +/// When the History Brush's source is the newest past state, it is the state the byte budget +/// always keeps, and no older state is kept beside it. +#[test] +fn byte_budget_counts_a_newest_brush_source_as_the_kept_state() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert!(h.set_brush_source(4)); + let source = h.state(4).unwrap(); + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 4); + assert_counter(&h, &cur); + assert_eq!(h.past_len(), 1); + assert_eq!(h.brush_source(), Some(0)); + assert!(Arc::ptr_eq(&h.row_doc(0, &cur).unwrap(), &source)); +} + +#[test] +fn new_edit_clears_redo() { + let mut h = History::default(); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert!(!h.can_redo()); + assert_eq!(h.entries(), vec!["Open".to_string(), "B".to_string()]); +} + +/// Open, A, B, C, then two steps back: A is current, B and C are redo states. +fn stepped_back(non_linear: bool) -> (History, Arc) { + let mut h = History { non_linear, ..History::default() }; + let mut cur = Arc::new(base()); + for n in ["A", "B", "C"] { + edit(&mut h, &mut cur, n, |d| d.name = n.to_lowercase()); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert_eq!(cur.name, "a"); + (h, cur) +} + +#[test] +fn non_linear_history_keeps_later_states_and_appends_the_new_step() { + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = format!("{}+d", d.name)); + assert_eq!(h.entries(), ["Open", "A", "B", "C", "D"], "B and C are kept, D goes to the end"); + assert!(!h.can_redo()); + assert_eq!(cur.name, "a+d", "D was made from the state selected (A), not from C"); + // Stepping back walks the list: C, B, A, Open, each with its own document. + let mut names = Vec::new(); + while let Some((d, _)) = h.undo(cur.clone()) { + cur = d; + names.push(cur.name.clone()); + } + assert_eq!(names, ["c", "b", "a", "h"]); + // Linear history (the default) discards them. + let (mut h, mut cur) = stepped_back(false); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.entries(), ["Open", "A", "D"]); +} + +#[test] +fn non_linear_steps_respect_the_state_limit_and_the_tile_counter() { + let mut h = History::new(3); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..4 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "branch", |d| d.name = "branch".into()); + assert_eq!(h.past_len(), 3, "the oldest states beyond the limit go"); + assert_eq!(h.entries(), ["paint 1", "paint 2", "paint 3", "branch"]); + assert_counter(&h, &cur); +} + +#[test] +fn remove_deletes_one_past_state_and_drop_current_keeps_the_redo_states() { + let (mut h, _) = stepped_back(true); + assert!(!h.remove(0), "the oldest state stays"); + assert!(!h.remove(1), "the current state (A) is not held"); + assert!(!h.remove(99)); + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert!(h.remove(1)); + assert_eq!(h.entries(), ["Open", "B", "C", "D"]); + assert_eq!(h.state(1).unwrap().name, "b", "each state keeps its own document"); + // Deleting the current state: the one before becomes current, the later ones stay. + let (mut h, _) = stepped_back(true); + let (d, _) = h.drop_current().unwrap(); + assert_eq!(d.name, "h"); + assert_eq!(h.entries(), ["Open"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert!(h.drop_current().is_none(), "nothing before Open"); + // The tile counter follows both. + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..4 { + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + assert!(h.remove(2)); + assert_counter(&h, &cur); + cur = h.drop_current().unwrap().0; + assert_counter(&h, &cur); +} + +#[test] +fn remove_later_deletes_one_redo_state() { + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(!h.remove_later(2) && !h.remove_later(usize::MAX)); + assert!(h.remove_later(0), "B"); + assert_eq!(h.redo_labels().collect::>(), ["C"]); + assert_counter(&h, &cur); + let (c, _) = h.redo(cur).unwrap(); + assert_eq!(h.entries(), ["Open", "A", "C"]); + assert_counter(&h, &c); +} + +/// Each state keeps the layers whose visibility its own step changed; when a state goes +/// (Purge, a non-linear Delete) or a step is appended after the kept later states, the step +/// that now follows covers the changes of the steps it now spans. +#[test] +fn step_visibility_follows_steps_and_spans_removed_ones() { + let vis = |h: &History| { + let mut v: Vec = h.current_visibility().iter().map(|l| l.0).collect(); + v.sort_unstable(); + v + }; + // Open, then steps A, B, C, D changing layers 1, 2, 3, 4. + let steps = |n: usize, non_linear: bool| { + let mut h = History { non_linear, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, label) in ["A", "B", "C", "D"].into_iter().take(n).enumerate() { + edit(&mut h, &mut cur, label, |d| d.name = label.to_lowercase()); + h.add_current_visibility(&[LayerId(i as u64 + 1)]); + } + (h, cur) + }; + let (mut h, cur) = steps(3, false); + assert_eq!(vis(&h), [3]); + let (cur, _) = h.undo(cur).unwrap(); + assert_eq!(vis(&h), [2]); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [3]); + // Purge: C now starts from A. + h.purge_last(); + assert_eq!(vis(&h), [2, 3]); + // Non-linear Delete of B (between A and C): C now follows A. + let (mut h, cur) = steps(4, true); + assert!(h.remove(2)); + h.undo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // Of the later B (A current): C follows A. + let (mut h, mut cur) = steps(3, true); + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(h.remove_later(0)); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // Of the current B (C later): A is current, C follows it. + let (mut h, cur) = steps(3, true); + h.undo(cur).unwrap(); + let cur = h.drop_current().unwrap().0; + assert_eq!(vis(&h), [1]); + h.redo(cur).unwrap(); + assert_eq!(vis(&h), [2, 3]); + // A step after the kept later states B and C (made from A) follows C in the list. + let (mut h, mut cur) = steps(3, true); + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + h.add_current_visibility(&[LayerId(4)]); + assert_eq!(vis(&h), [2, 3, 4]); + h.rollback().unwrap(); + assert_eq!(vis(&h), [1], "rollback restores A's"); + h.clear(); + assert_eq!(vis(&h), Vec::::new()); +} + +#[test] +fn rollback_takes_the_last_record_back_exactly_in_both_modes() { + // Non-linear, with redo states: the moved states go back to the redo side and the + // document the step replaced (A) is current again, not the last former redo state (C). + let (mut h, mut cur) = stepped_back(true); + let a = cur.clone(); + let entries = h.entries(); + let redo: Vec = h.redo_labels().map(str::to_string).collect(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.recorded_before().map(|d| d.name.clone()).as_deref(), Some("a")); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a)); + assert_eq!(h.entries(), entries); + assert_eq!(h.redo_labels().map(str::to_string).collect::>(), redo); + assert!(h.rollback().is_none(), "only once"); + // Redoing still walks the kept states. + let (b, _) = h.redo(d).unwrap(); + assert_eq!(b.name, "b"); + // Linear: the same as an undo whose redo state is dropped. + let (mut h, mut cur) = stepped_back(false); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.rollback().unwrap().0.name, "a"); + assert_eq!(h.entries(), ["Open", "A"]); + assert!(!h.can_redo()); + // Anything else since the record: no rollback (the caller undoes instead). + let (mut h, mut cur) = stepped_back(true); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + cur = h.undo(cur).unwrap().0; + assert!(h.recorded_before().is_none() && h.rollback().is_none()); + h.redo(cur).unwrap(); + assert!(h.rollback().is_none()); + // The tile counter follows. + let mut h = History::new(50); + h.non_linear = true; + h.max_bytes = usize::MAX; + let mut cur = Arc::new(base()); + for i in 0..3 { + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + edit(&mut h, &mut cur, "Paint", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + cur = h.rollback().unwrap().0; + assert_counter(&h, &cur); +} + +/// A failed compound command whose first step moved more later states than the state limit +/// leaves room for (or whose step the byte budget trimmed for) is still taken back exactly: +/// the states the limit dropped for it come back, as in the list before the command. +#[test] +fn rollback_brings_back_the_states_the_limit_dropped_for_the_step() { + for max in 1..=4 { + let (mut h, mut cur) = stepped_back(true); + h.max_states = max; + let a = cur.clone(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert!(h.past_len() <= max); + assert_eq!(h.recorded_before().map(|d| d.name.clone()).as_deref(), Some("a"), "max {max}"); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a), "max {max}: back on A, not on an Undo of C"); + assert_eq!(h.entries(), ["Open", "A"], "max {max}"); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"], "max {max}"); + assert!(h.rollback().is_none()); + } + // Once anything else changed the list, the dropped states stay dropped. + let (mut h, mut cur) = stepped_back(true); + h.max_states = 2; + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + cur = h.undo(cur).unwrap().0; + h.redo(cur).unwrap(); + assert!(h.rollback().is_none()); + assert_eq!(h.entries(), ["B", "C", "D"]); + // The byte budget: the trim after the step keeps only its newest state; the rollback + // brings back the rest and the tile counter follows. + let mut h = History { non_linear: true, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + h.max_bytes = 1; + edit(&mut h, &mut cur, "D", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + assert!(h.trim(&cur) > 0); + assert_eq!(h.entries(), ["C", "D"]); + assert_counter(&h, &cur); + cur = h.rollback().unwrap().0; + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_counter(&h, &cur); +} + +/// The History Brush's source never falls off, so the limit and the byte budget drop states +/// behind it, not at the front; a rollback still puts each back where it was. +#[test] +fn rollback_puts_states_back_behind_the_pinned_brush_source() { + let (mut h, mut cur) = stepped_back(true); + assert!(h.set_brush_source(0)); + h.max_states = 2; + let a = cur.clone(); + edit(&mut h, &mut cur, "D", |d| d.name = "d".into()); + assert_eq!(h.entries(), ["Open", "C", "D"], "A and B go, the source (Open) stays"); + let (d, _) = h.rollback().unwrap(); + assert!(Arc::ptr_eq(&d, &a)); + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_eq!(h.brush_source(), Some(0)); + assert_eq!(h.state(0).unwrap().name, "h"); + // The byte budget, with the source on the oldest state. + let mut h = History { non_linear: true, ..History::default() }; + let mut cur = Arc::new(base()); + for (i, n) in ["A", "B", "C"].into_iter().enumerate() { + edit(&mut h, &mut cur, n, |d| { + d.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + cur = h.undo(cur).unwrap().0; + cur = h.undo(cur).unwrap().0; + assert!(h.set_brush_source(0)); + h.max_bytes = 1; + edit(&mut h, &mut cur, "D", |d| { + d.layers[0].surface_mut().unwrap().write_pixel(2, 2, &[1.0, 0.0, 0.0, 1.0]); + }); + assert!(h.trim(&cur) > 0); + assert_eq!(h.entries(), ["Open", "C", "D"]); + cur = h.rollback().unwrap().0; + assert_eq!(cur.name, "h"); + assert_eq!(h.entries(), ["Open", "A"]); + assert_eq!(h.redo_labels().collect::>(), ["B", "C"]); + assert_eq!(h.state(0).unwrap().name, "h"); + assert_counter(&h, &cur); +} + +#[test] +fn max_states_bound() { + let mut h = History::new(3); + let mut cur = Arc::new(base()); + for i in 0..10 { + edit(&mut h, &mut cur, &format!("step {i}"), |d| d.name = format!("{i}")); + } + let mut n = 0; + while let Some((d, _)) = h.undo(cur.clone()) { + cur = d; + n += 1; + } + assert_eq!(n, 3); + assert_eq!(cur.name, "6"); +} + +#[test] +fn byte_budget_drops_oldest_states_but_keeps_one() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + let tile = 256 * 256 * 4; + // Each step repaints the background's tile: one unique tile per state. + for i in 0..6 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + assert_eq!(h.trim(&cur), 0, "unlimited by default"); + assert_eq!(h.past_len(), 6); + // The current tile plus three history tiles fit. + h.max_bytes = 4 * tile; + assert_eq!(h.trim(&cur), 3); + assert_eq!(h.past_len(), 3); + assert_eq!(h.unique_bytes(&cur), 3 * tile); + assert_eq!(h.pixel_bytes(&cur), 4 * tile, "the current tile plus history's"); + assert_eq!(h.entries()[0], "paint 2"); + // A budget smaller than one state still keeps the last step undoable. + h.max_bytes = 1; + h.trim(&cur); + assert_eq!(h.past_len(), 1); + assert!(h.undo(cur.clone()).is_some()); +} + +/// Snapshot-only tiles count against the budget (#4530); tiles a snapshot shares with the +/// document or a state count once, and a purge frees only what no snapshot keeps. +#[test] +fn snapshot_tiles_count_against_the_budget() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + let tile = 256 * 256 * 4; + let paint = |d: &mut Document, v: f32| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[v, 0.0, 0.0, 1.0]); + }; + // A snapshot holding a tile nothing else does, then one sharing the first state's tile. + let mut own = (*cur).clone(); + paint(&mut own, 0.9); + edit(&mut h, &mut cur, "paint 0", |d| paint(d, 0.1)); + let shared = h.state(0).unwrap(); + for i in 1..6 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| paint(d, i as f32 / 8.0)); + } + let snaps = [&own, &*shared]; + assert_eq!(h.extra_bytes(&cur, &snaps), tile, "the shared tile is history's"); + assert_eq!(h.extra_bytes(&cur, &[]), 0); + assert_eq!(h.unique_bytes(&cur), 6 * tile); + assert_eq!(h.unique_bytes_besides(&cur, &snaps), 5 * tile, "clearing keeps the shared tile"); + // Both snapshot tiles stay whatever is trimmed (the shared one too), so beside the + // current tile they leave room for two history tiles; without them, four would fit. + h.max_bytes = 5 * tile; + assert_eq!(h.trim_with(&cur, &snaps), 4); + assert_eq!(h.past_len(), 2); + assert_eq!(h.pixel_bytes(&cur) + h.extra_bytes(&cur, &snaps), 5 * tile); + // Still keeps the last step undoable. + h.max_bytes = 1; + h.trim_with(&cur, &snaps); + assert_eq!(h.past_len(), 1); +} + +#[test] +fn history_shares_untouched_tiles() { + let mut h = History::default(); + let mut cur = Arc::new(base()); + assert_eq!(h.unique_bytes(&cur), 0); + // rename only: no pixel changes, history holds no unique tiles + edit(&mut h, &mut cur, "Rename", |d| d.name = "x".into()); + assert_eq!(h.unique_bytes(&cur), 0); + // paint one pixel: exactly one tile becomes unique to history + edit(&mut h, &mut cur, "Paint", |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[0.0, 0.0, 0.0, 1.0]); + }); + assert_eq!(h.unique_bytes(&cur), 256 * 256 * 4); +} + +#[test] +fn fill_caches_and_smart_filter_masks_count_toward_history_bytes() { + use photocraft_doc::{Affine, Fill, FillCache, LayerContent, LayerMask, PixelFormat, SmartObject, SmartSource, Surface}; + let mut doc = Document::new("fills", Size::new(64, 64), ColorMode::Rgb, SampleType::U8); + let mut fill = Layer::new("Fill", LayerContent::Fill(Fill::Solid(Color::WHITE))); + let mut cache = Surface::new(PixelFormat::GRAY8); + cache.write_pixel(1, 1, &[1.0]); + fill.fill_cache = Some(FillCache { fill: Fill::Solid(Color::WHITE), surface: cache }); + doc.insert_above(None, fill); + let mut smart_obj = SmartObject::new(SmartSource::Linked { path: "a.png".into() }, Affine::IDENTITY, None); + let mut mask = LayerMask::reveal_all(); + mask.surface.write_pixel(1, 1, &[0.5]); + smart_obj.filter_mask = Some(mask); + doc.insert_above(None, Layer::new("Smart", LayerContent::Smart(smart_obj))); + let tile = 256 * 256; + let mut cur = Arc::new(doc); + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + assert_eq!(h.pixel_bytes(&cur), 2 * tile, "fill cache and smart filter mask are counted"); + edit(&mut h, &mut cur, "Rename", |doc| doc.name = "renamed".into()); + assert_eq!(h.pixel_bytes(&cur), 2 * tile, "shared tiles count once"); + edit(&mut h, &mut cur, "Fill cache", |doc| { + let id = doc.layers[0].id; + if let Some(cache) = doc.layer_mut(id).and_then(|l| l.fill_cache.as_mut()) { + cache.surface.write_pixel(1, 1, &[0.0]); + } + }); + assert_eq!(h.pixel_bytes(&cur), 3 * tile, "the copied fill cache tile is history-only"); + assert_eq!(h.unique_bytes(&cur), tile); + h.trim(&cur); + assert_counter(&h, &cur); + h.max_bytes = 2 * tile; + assert_eq!(h.trim(&cur), 1, "the copied fill cache tile pushes history over budget"); + assert_eq!(h.pixel_bytes(&cur), 3 * tile, "the most recent undo state is always kept"); + assert_counter(&h, &cur); +} + +const TILE: usize = 256 * 256 * 4; +const SOURCE: usize = 1_000_000; + +fn embedded(len: usize) -> photocraft_doc::SmartSource { + photocraft_doc::SmartSource::Embedded { file_name: "source.pcraft".into(), bytes: Arc::new(vec![7; len]) } +} + +/// `base()` with one smart object holding `source`; returns the document and the layer's id. +fn smart_document(source: photocraft_doc::SmartSource) -> (Document, photocraft_doc::LayerId) { + use photocraft_doc::{Affine, LayerContent, SmartObject}; + let mut doc = base(); + let layer = Layer::new("Smart", LayerContent::Smart(SmartObject::new(source, Affine::IDENTITY, None))); + let id = layer.id; + doc.insert_above(None, layer); + (doc, id) +} + +fn replace_source(doc: &mut Document, id: photocraft_doc::LayerId, source: photocraft_doc::SmartSource) { + if let Some(photocraft_doc::LayerContent::Smart(sm)) = doc.layer_mut(id).map(|l| &mut l.content) { + sm.source = source; + } +} + +#[test] +fn embedded_smart_object_contents_count_toward_history_bytes() { + let (with, _) = smart_document(embedded(SOURCE)); + let without = base(); + let mut h = History::default(); + assert_eq!(h.pixel_bytes(&with), h.pixel_bytes(&without) + SOURCE, "uncounted history walks the document"); + assert_eq!(h.pixel_bytes(&without), TILE); + // With a budget the counter keeps the figure incrementally. + let mut cur = Arc::new(with.clone()); + h.max_bytes = usize::MAX; + edit(&mut h, &mut cur, "Rename", |d| d.name = "renamed".into()); + edit(&mut h, &mut cur, "Rename again", |d| d.name = "renamed again".into()); + h.trim(&cur); + assert_eq!(h.pixel_bytes(&cur), TILE + SOURCE); + assert_counter(&h, &cur); + // A linked source holds no bytes of its own. + let (linked, _) = smart_document(photocraft_doc::SmartSource::Linked { path: "a.png".into() }); + assert_eq!(History::default().pixel_bytes(&linked), TILE); +} + +#[test] +fn states_sharing_one_embedded_source_count_it_once() { + let (doc, _) = smart_document(embedded(SOURCE)); + let mut cur = Arc::new(doc); + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + for i in 0..10 { + edit(&mut h, &mut cur, &format!("Rename {i}"), |d| d.name = format!("renamed {i}")); + } + h.trim(&cur); + assert_eq!(h.past_len(), 10); + assert_eq!(h.pixel_bytes(&cur), TILE + SOURCE); + assert_eq!(h.unique_bytes(&cur), 0); + assert_counter(&h, &cur); + // A duplicated layer shares the same bytes too. + edit(&mut h, &mut cur, "Duplicate", |d| { + if let Some(copy) = d.layers.last().cloned() { + d.insert_above(None, copy); + } + }); + h.trim(&cur); + assert_eq!(h.pixel_bytes(&cur), TILE + SOURCE); + assert_counter(&h, &cur); +} + +#[test] +fn a_replaced_embedded_source_counts_until_the_state_holding_it_is_trimmed() { + const REPLACED: usize = 400_000; + let (doc, id) = smart_document(embedded(SOURCE)); + let mut cur = Arc::new(doc); + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + edit(&mut h, &mut cur, "Replace Contents", |d| replace_source(d, id, embedded(REPLACED))); + assert_eq!(h.pixel_bytes(&cur), TILE + SOURCE + REPLACED, "the state before still holds the old contents"); + assert_eq!(h.unique_bytes(&cur), SOURCE); + edit(&mut h, &mut cur, "Rename", |d| d.name = "renamed".into()); + h.trim(&cur); + assert_counter(&h, &cur); + h.max_bytes = TILE + REPLACED; + assert_eq!(h.trim(&cur), 1, "dropping the oldest state releases the old contents"); + assert_eq!(h.pixel_bytes(&cur), TILE + REPLACED); + assert_counter(&h, &cur); +} + +#[test] +fn embedded_contents_push_old_states_out_of_the_budget() { + let (doc, id) = smart_document(embedded(SOURCE)); + let mut cur = Arc::new(doc); + let mut h = History::new(50); + for i in 0..5 { + // Each step swaps in contents of its own; only the tile is shared between states. + edit(&mut h, &mut cur, &format!("Replace {i}"), |d| replace_source(d, id, embedded(SOURCE))); + } + assert_eq!(h.trim(&cur), 0, "unlimited by default"); + assert_eq!(h.pixel_bytes(&cur), TILE + 6 * SOURCE); + // The current contents and the two newest states fit. + h.max_bytes = TILE + 3 * SOURCE; + assert_eq!(h.trim(&cur), 3); + assert_eq!(h.past_len(), 2); + assert_eq!(h.pixel_bytes(&cur), TILE + 3 * SOURCE); + assert_counter(&h, &cur); +} + +#[test] +fn selection_tiles_count_current_and_history_without_double_counting() { + use photocraft_doc::{PixelFormat, Surface}; + for depth in SampleType::ALL { + let mut doc = Document::new("selection", Size::new(300, 64), ColorMode::Rgb, depth); + let mut selection = Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false)); + selection.write_pixel(1, 1, &[1.0]); + selection.write_pixel(257, 1, &[1.0]); + doc.selection = Some(selection); + let mut cur = Arc::new(doc); + let tile = match depth { + SampleType::U8 => 65_536, + SampleType::U16 => 131_072, + SampleType::F32 => 262_144, + }; + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + assert_eq!(h.pixel_bytes(&cur), 2 * tile, "current selection counts without any history"); + assert_eq!(h.unique_bytes(&cur), 0); + edit(&mut h, &mut cur, "Rename", |doc| doc.name = "renamed".into()); + assert_eq!(h.pixel_bytes(&cur), 2 * tile, "the shared selection tiles count once"); + edit(&mut h, &mut cur, "Selection", |doc| { + doc.selection.as_mut().unwrap().write_pixel(1, 1, &[0.5]); + }); + assert_eq!(h.pixel_bytes(&cur), 3 * tile, "only the changed selection tile is copied"); + assert_eq!(h.unique_bytes(&cur), tile); + h.trim(&cur); + assert_counter(&h, &cur); + let selected_value = cur.selection.as_ref().unwrap().sample_channel(1, 1, 0); + edit(&mut h, &mut cur, "Deselect", |doc| doc.selection = None); + assert_eq!(h.pixel_bytes(&cur), 3 * tile, "deselecting leaves the previous masks in history"); + assert_eq!(h.unique_bytes(&cur), 3 * tile); + assert_counter(&h, &cur); + cur = h.undo(cur).unwrap().0; + assert_eq!(cur.selection.as_ref().unwrap().sample_channel(1, 1, 0), selected_value); + assert_eq!(h.pixel_bytes(&cur), 3 * tile); + assert_eq!(h.unique_bytes(&cur), tile); + assert_counter(&h, &cur); + // Coalescing can change the current mask without recording another state. + Arc::make_mut(&mut cur).selection.as_mut().unwrap().write_pixel(257, 1, &[0.25]); + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + assert_eq!(h.trim(&cur), 0); + assert_counter(&h, &cur); + cur = h.redo(cur).unwrap().0; + assert!(cur.selection.is_none()); + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + assert_counter(&h, &cur); + } +} + +#[test] +fn selection_byte_budget_drops_old_masks_and_keeps_the_last_undo() { + use photocraft_doc::{PixelFormat, Surface}; + for depth in SampleType::ALL { + let mut doc = Document::new("selection", Size::new(64, 64), ColorMode::Rgb, depth); + doc.selection = Some(Surface::new(PixelFormat::new(ColorMode::Grayscale, depth, false))); + let mut cur = Arc::new(doc); + let tile = 256 * 256 * depth.bytes(); + let mut h = History::default(); + for i in 1..=6 { + edit(&mut h, &mut cur, "Selection", |doc| { + doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0]); + }); + } + assert_eq!(h.pixel_bytes(&cur), 6 * tile); + h.max_bytes = 3 * tile; + assert_eq!(h.trim(&cur), 4); + assert_eq!(h.past_len(), 2); + assert_eq!(h.pixel_bytes(&cur), 3 * tile); + assert_eq!(h.unique_bytes(&cur), 2 * tile); + assert_counter(&h, &cur); + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 1); + assert_eq!(h.past_len(), 1, "the most recent undo survives even below one mask's size"); + assert_eq!(h.pixel_bytes(&cur), 2 * tile); + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + } +} + +fn assert_counter(h: &History, current: &Document) { + let counter = h.tiles.as_ref().expect("budget initializes the counter"); + let total = tile_bytes(current, &mut HashSet::new()).saturating_add(h.unique_bytes(current)); + assert_eq!(counter.bytes_with(current), total as u128); + assert_eq!(h.pixel_bytes(current), total); + let mut seen = HashSet::new(); + let retained: usize = h.held_states().map(|s| tile_bytes(&s.doc, &mut seen)).sum(); + let empty = Document::new("empty", Size::new(1, 1), ColorMode::Rgb, SampleType::U8); + assert_eq!(counter.bytes_with(&empty), retained as u128); +} + +fn video_document(depth: SampleType, frames: usize) -> Document { + let mut doc = Document::new("video", Size::new(64, 64), ColorMode::Rgb, depth); + let mut layer = Layer::raster("Video", doc.pixel_format()); + let footage: Vec<_> = (0..frames) + .map(|i| { + let mut surface = photocraft_doc::Surface::new(doc.pixel_format()); + surface.write_pixel(1, 1, &[i as f32 / frames as f32, 0.0, 0.0, 1.0]); + surface + }) + .collect(); + layer.content = photocraft_doc::LayerContent::Raster(footage[0].clone()); + layer.video = Some(photocraft_doc::VideoData::new(footage, 24.0)); + doc.layers.push(layer); + doc +} + +#[test] +fn video_memory_counts_originals_and_deduplicates_displayed_frames() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut doc = video_document(depth, 3); + let tile = doc.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len(); + let video = doc.layers[0].video.as_mut().unwrap(); + video.original_frames = Some(video.frames.clone()); + // Original footage and the displayed frame alias the stack, except this painted frame. + video.frames[2].write_pixel(1, 1, &[0.0, 1.0, 0.0, 1.0]); + let mut cur = Arc::new(doc); + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + for name in ["A", "B"] { + edit(&mut h, &mut cur, "Rename", |doc| doc.name = name.into()); + h.trim(&cur); + } + assert_counter(&h, &cur); + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + assert_eq!(h.unique_bytes(&cur), 0); + // Rasterizing keeps the displayed frame; the rest of the clip survives only in undo. + edit(&mut h, &mut cur, "Rasterize", |doc| doc.layers[0].video = None); + assert_eq!(h.unique_bytes(&cur), 3 * tile); + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + assert_counter(&h, &cur); + cur = h.undo(cur).unwrap().0; + assert_eq!(h.unique_bytes(&cur), 0); + assert_counter(&h, &cur); + cur = h.redo(cur).unwrap().0; + assert_eq!(h.unique_bytes(&cur), 3 * tile); + assert_counter(&h, &cur); + h.clear(); + assert_eq!(h.pixel_bytes(&cur), tile); + } +} + +#[test] +fn video_memory_budget_evicts_deleted_off_playhead_frames() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut cur = Arc::new(video_document(depth, 6)); + let tile = cur.layers[0].surface().unwrap().tiles().next().unwrap().1.bytes().len(); + let mut h = History::default(); + for _ in 0..4 { + edit(&mut h, &mut cur, "Delete Frame", |doc| { + doc.layers[0].video.as_mut().unwrap().frames.remove(1); + }); + } + assert_eq!(h.pixel_bytes(&cur), 6 * tile); + assert_eq!(h.unique_bytes(&cur), 4 * tile); + h.max_bytes = 3 * tile; + assert_eq!(h.trim(&cur), 3); + assert_eq!(h.past_len(), 1); + assert_eq!(h.pixel_bytes(&cur), 3 * tile); + assert_eq!(h.unique_bytes(&cur), tile); + assert_counter(&h, &cur); + cur = h.undo(cur).unwrap().0; + assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 3); + assert_counter(&h, &cur); + cur = h.redo(cur).unwrap().0; + assert_eq!(cur.layers[0].video.as_ref().unwrap().frames.len(), 2); + assert_counter(&h, &cur); + // A budget below the remaining clip still keeps the latest step undoable. + h.max_bytes = 1; + assert_eq!(h.trim(&cur), 0); + assert!(h.can_undo()); + } +} + +/// A pattern fill or layer style copies its pattern into the document, so the pattern's tiles +/// count like layer pixels, shared ones once; undoing that step leaves them to the redo states. +#[test] +fn document_patterns_count_as_document_pixels() { + use photocraft_doc::{Pattern, Surface}; + for depth in SampleType::ALL { + let tile = 4 * 256 * 256 * depth.bytes(); + let mut cur = Arc::new(Document::new("patterns", Size::new(64, 64), ColorMode::Rgb, depth)); + let mut h = History { max_bytes: usize::MAX, ..History::default() }; + for name in ["A", "B"] { + edit(&mut h, &mut cur, "Rename", |doc| doc.name = name.into()); + h.trim(&cur); + } + let mut surface = Surface::new(cur.pixel_format()); + surface.write_pixel(1, 1, &[0.0, 0.0, 1.0, 1.0]); + let pattern = Pattern::new("Pattern", surface, 4, 4); + edit(&mut h, &mut cur, "Pattern Fill", |doc| doc.patterns.push(pattern)); + assert_eq!(h.pixel_bytes(&cur), tile, "the document's pattern"); + assert_eq!(h.unique_bytes(&cur), 0); + assert_counter(&h, &cur); + edit(&mut h, &mut cur, "Rename", |doc| doc.name = "C".into()); + assert_eq!(h.pixel_bytes(&cur), tile, "shared with history, it counts once"); + assert_counter(&h, &cur); + for _ in 0..2 { + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + } + assert!(cur.patterns.is_empty()); + assert_eq!(h.unique_bytes(&cur), tile, "only the redo states hold the pattern"); + assert_eq!(h.pixel_bytes(&cur), tile); + cur = h.redo(cur).unwrap().0; + assert_eq!(h.unique_bytes(&cur), 0); + assert_counter(&h, &cur); + h.clear(); + assert_eq!(h.pixel_bytes(&cur), tile); + } +} + +#[test] +fn counter_tracks_eviction_undo_redo_branching_purge_and_clones() { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let mut cur = Arc::new(Document::with_background("h", Size::new(300, 64), ColorMode::Rgb, depth, Color::WHITE)); + Arc::make_mut(&mut cur).selection = Some(photocraft_doc::Surface::new(photocraft_doc::PixelFormat::GRAY8)); + let mut h = History::new(4); + h.max_bytes = usize::MAX; + for i in 0..8 { + edit(&mut h, &mut cur, "Paint", |doc| { + doc.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0, 0.0, 0.0, 1.0]); + doc.selection.as_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0]); + }); + h.trim(&cur); + if i >= 1 { + assert_counter(&h, &cur); + } + } + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + cur = h.redo(cur).unwrap().0; + assert_counter(&h, &cur); + let copy = h.clone(); + edit(&mut h, &mut cur, "Branch", |doc| doc.name = "branch".into()); + assert!(!h.can_redo()); + assert_counter(&h, &cur); + assert_counter(©, &cur); + cur = h.undo(cur).unwrap().0; + h.clear_redo(); + assert_counter(&h, &cur); + h.purge_last(); + assert_counter(&h, &cur); + h.clear(); + assert!(h.tiles.is_none()); + assert_eq!(h.pixel_bytes(&cur), tile_bytes(&cur, &mut HashSet::new())); + } +} + +#[test] +fn shared_masks_channels_and_repeated_snapshot_references_count_once() { + use photocraft_doc::{AlphaChannel, LayerMask, PixelFormat, Surface}; + let mut doc = base(); + let mut gray = Surface::new(PixelFormat::GRAY8); + gray.write_pixel(1, 1, &[1.0]); + doc.layers[0].mask = Some(LayerMask { surface: gray.clone(), ..LayerMask::reveal_all() }); + doc.channels.push(AlphaChannel::new("alpha", gray.clone())); + doc.selection = Some(gray.clone()); + doc.quick_mask = Some(AlphaChannel::new("quick", gray)); + doc.layers.push(doc.layers[0].clone()); + let mut cur = Arc::new(doc); + let bytes = tile_bytes(&cur, &mut HashSet::new()); + let mut h = History::new(50); + // Counting current separately would double count its tiles; the exact union fits. + h.max_bytes = bytes; + for _ in 0..6 { + h.record("Same", cur.clone(), LayerTarget::default()); + assert_eq!(h.trim(&cur), 0); + } + assert_counter(&h, &cur); + for _ in 0..3 { + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + } + h.clear_redo(); + h.purge_last(); + assert_counter(&h, &cur); + assert_eq!(h.pixel_bytes(&cur), bytes); +} + +#[test] +fn threshold_rechecks_coalesced_current_and_changed_budget() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + h.max_bytes = usize::MAX; + for i in 0..6 { + edit(&mut h, &mut cur, "Paint", |doc| { + doc.layers[0].surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 10.0, 0.0, 0.0, 1.0]); + }); + h.trim(&cur); + } + // A coalesced edit may change current without recording another history state. + Arc::make_mut(&mut cur).layers[0].surface_mut().unwrap().write_pixel(300, 1, &[1.0; 4]); + let tile = 256 * 256 * 4; + h.max_bytes = 4 * tile; + assert_eq!(h.trim(&cur), 4); + assert_eq!(h.past_len(), 2); + assert_counter(&h, &cur); + cur = h.undo(cur).unwrap().0; + assert_counter(&h, &cur); + h.max_bytes = 0; + edit(&mut h, &mut cur, "Unlimited", |doc| doc.name = "u".into()); + assert!(h.tiles.is_none()); + h.max_bytes = 1; + h.trim(&cur); + assert_eq!(h.past_len(), 1, "always retain the most recent undo"); + assert_counter(&h, &cur); +} + +#[test] +fn transferred_tiles_cannot_make_the_budget_check_underestimate_current() { + use photocraft_doc::{PixelFormat, Surface}; + let before = Arc::new(base()); + let mut current = (*before).clone(); + let mut large = Surface::new(PixelFormat::new(ColorMode::Cmyk, SampleType::F32, true)); + large.write_pixel(256, 0, &[1.0; 5]); + current.layers[0].surface_mut().unwrap().put_tiles(large.take_tiles(photocraft_doc::Rect::from_xywh(256, 0, 1, 1))); + let mut h = History::new(50); + h.max_bytes = tile_bytes(¤t, &mut HashSet::new()) - 1; + h.record("A", before.clone(), LayerTarget::default()); + h.record("B", before, LayerTarget::default()); + assert_eq!(h.trim(¤t), 1, "current alone exceeds the budget; only the mandatory undo survives"); + assert_counter(&h, ¤t); +} + +#[test] +fn clear_states_keeps_the_document_and_undo_brings_the_list_back() { + let mut h = History::new(10); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + edit(&mut h, &mut cur, "C", |d| d.name = "c".into()); + let (d, _) = h.undo(cur.clone()).unwrap(); + cur = d; + let entries = h.entries(); + assert_eq!(entries, ["Open", "A", "B"]); + assert!(h.clear_states()); + assert_eq!(h.entries(), ["B"], "only the current state is left"); + assert!(!h.can_redo() && h.can_undo() && h.is_cleared()); + assert_eq!(h.undo_label(), Some(CLEAR_HISTORY_LABEL)); + assert!(!h.clear_states(), "nothing left to clear"); + assert!(h.is_cleared(), "and the cleared states are still held"); + let (d, _) = h.undo(cur.clone()).unwrap(); + assert!(Arc::ptr_eq(&d, &cur), "undoing Clear History doesn't change the document"); + assert_eq!(h.entries(), entries); + assert_eq!(h.redo_labels().collect::>(), ["C"], "the redo state is back too"); + assert!(!h.is_cleared()); +} + +#[test] +fn cleared_states_still_count_in_the_memory_figures_and_the_budget() { + let mut h = History::new(50); + let mut cur = Arc::new(base()); + let tile = 256 * 256 * 4; + for i in 0..5 { + edit(&mut h, &mut cur, &format!("paint {i}"), |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[i as f32 / 8.0, 0.0, 0.0, 1.0]); + }); + } + cur = h.undo(cur).unwrap().0; + let (unique, pixels) = (h.unique_bytes(&cur), h.pixel_bytes(&cur)); + assert_eq!(unique, 5 * tile, "four undo states and one redo state"); + assert!(h.clear_states()); + assert_eq!(h.unique_bytes(&cur), unique, "Clear History frees nothing while it can be undone"); + assert_eq!(h.pixel_bytes(&cur), pixels); + assert_eq!(h.extra_bytes(&cur, &[&base()]), tile, "a snapshot's own tile, not the cleared states'"); + assert_eq!(h.unique_bytes_besides(&cur, &[]), unique); + // The budget counts them too: the oldest cleared states go first. + h.max_bytes = 4 * tile; + assert_eq!(h.trim(&cur), 2); + assert_counter(&h, &cur); + assert_eq!(h.pixel_bytes(&cur), 4 * tile); + assert!(h.is_cleared() && h.past_len() == 0, "the list stays cleared"); + h.undo(cur.clone()).unwrap(); + assert_eq!(h.entries(), ["paint 1", "paint 2", "paint 3"], "undo brings back what the budget kept"); + assert_eq!(h.redo_labels().collect::>(), ["paint 4"]); + // A new step after Clear History forgets the cleared states, and the counter with them. + assert!(h.clear_states()); + h.trim(&cur); + edit(&mut h, &mut cur, "paint 5", |d| { + let id = d.layers[0].id; + d.layer_mut(id).unwrap().surface_mut().unwrap().write_pixel(1, 1, &[1.0, 0.0, 0.0, 1.0]); + }); + assert_eq!(h.unique_bytes(&cur), tile, "only the state before the new step"); + assert_eq!(h.pixel_bytes(&cur), 2 * tile); +} + +#[test] +fn a_new_step_or_purge_forgets_the_cleared_states() { + let mut h = History::new(10); + let mut cur = Arc::new(base()); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + assert!(h.clear_states()); + edit(&mut h, &mut cur, "B", |d| d.name = "b".into()); + assert_eq!(h.entries(), ["A", "B"]); + assert!(!h.is_cleared()); + assert!(h.undo(cur.clone()).is_some()); + assert!(!h.can_undo(), "the cleared states are gone"); + let mut h = History::new(10); + edit(&mut h, &mut cur, "A", |d| d.name = "a".into()); + assert!(h.clear_states()); + assert!(h.purge_last()); + assert!(!h.can_undo() && !h.is_cleared()); + assert!(!h.clear_states(), "a single state can't be cleared"); +} diff --git a/crates/ops/src/tiles.rs b/crates/ops/src/tiles.rs index ec191f301a..50243cc8d8 100644 --- a/crates/ops/src/tiles.rs +++ b/crates/ops/src/tiles.rs @@ -1,7 +1,7 @@ //! Tile references owned by history snapshots. The snapshots keep these addresses alive; //! COW edits cannot change a retained tile in place. Count occurrences (including aliases //! within one document), so removing a state releases a tile only after its last reference. -use photocraft_doc::{Document, LayerContent, Surface}; +use photocraft_doc::{Document, LayerContent, SmartSource, Surface}; use std::collections::{HashMap, HashSet}; use std::sync::Arc; @@ -47,29 +47,42 @@ pub(crate) fn surfaces(doc: &Document, mut visit: impl FnMut(&Surface)) { } } +/// Every shared allocation `doc` holds that history can retain, as `visit(address, length)`: +/// the tiles of [`surfaces`] and the embedded contents of smart objects (an `Arc` that undo +/// states and duplicated layers share, replaced by Replace Contents). +pub(crate) fn blocks(doc: &Document, mut visit: impl FnMut(usize, usize)) { + surfaces(doc, |surface| { + for (_, tile) in surface.tiles() { + visit(Arc::as_ptr(tile) as usize, tile.bytes().len()); + } + }); + for (_, _, layer) in doc.walk() { + if let LayerContent::Smart(smart) = &layer.content + && let SmartSource::Embedded { bytes, .. } = &smart.source + { + visit(Arc::as_ptr(bytes) as usize, bytes.len()); + } + } +} + impl Tiles { pub(crate) fn add(&mut self, doc: &Document) { - surfaces(doc, |surface| { - for (_, tile) in surface.tiles() { - let count = self.refs.entry(Arc::as_ptr(tile) as usize).or_insert_with(|| { - self.bytes += tile.bytes().len() as u128; - (0, tile.bytes().len()) - }); - count.0 += 1; - } + blocks(doc, |ptr, len| { + let count = self.refs.entry(ptr).or_insert_with(|| { + self.bytes += len as u128; + (0, len) + }); + count.0 += 1; }); } pub(crate) fn remove(&mut self, doc: &Document) { - surfaces(doc, |surface| { - for (_, tile) in surface.tiles() { - let key = Arc::as_ptr(tile) as usize; - if let Some(count) = self.refs.get_mut(&key) { - count.0 -= 1; - if count.0 == 0 { - self.bytes -= count.1 as u128; - self.refs.remove(&key); - } + blocks(doc, |ptr, _| { + if let Some(count) = self.refs.get_mut(&ptr) { + count.0 -= 1; + if count.0 == 0 { + self.bytes -= count.1 as u128; + self.refs.remove(&ptr); } } }); @@ -85,12 +98,9 @@ impl Tiles { pub(crate) fn bytes_with(&self, current: &Document) -> u128 { let mut total = self.bytes; let mut seen = HashSet::new(); - surfaces(current, |surface| { - for (_, tile) in surface.tiles() { - let key = Arc::as_ptr(tile) as usize; - if !self.refs.contains_key(&key) && seen.insert(key) { - total += tile.bytes().len() as u128; - } + blocks(current, |ptr, len| { + if !self.refs.contains_key(&ptr) && seen.insert(ptr) { + total += len as u128; } }); total diff --git a/crates/paint/Cargo.toml b/crates/paint/Cargo.toml index afb9a196d0..f07ffeefaf 100644 --- a/crates/paint/Cargo.toml +++ b/crates/paint/Cargo.toml @@ -12,6 +12,12 @@ photocraft-geom = { workspace = true } photocraft-color = { workspace = true } photocraft-raster = { workspace = true } serde = { workspace = true } +rustc-hash = { workspace = true } + +# Large tips rasterize, accumulate and composite in parallel (the same pixels as serially). +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +rayon = { workspace = true } [dev-dependencies] +photocraft-cms = { workspace = true } serde_json = { workspace = true } diff --git a/crates/paint/examples/bench_brush.rs b/crates/paint/examples/bench_brush.rs index a9360b45fb..efd9c010f1 100644 --- a/crates/paint/examples/bench_brush.rs +++ b/crates/paint/examples/bench_brush.rs @@ -8,7 +8,7 @@ use std::time::Instant; use photocraft_color::{PixelFormat, SampleType}; use photocraft_geom::Rect; -use photocraft_paint::*; +use photocraft_paint::{BrushSettings, Dynamic, Scattering, ShapeDynamics, StrokePoint, StrokeRenderer, Texture, presets}; use photocraft_raster::Surface; fn run(fmt: PixelFormat, label: &str, brush: &BrushSettings) { diff --git a/crates/paint/examples/big_dab_bench.rs b/crates/paint/examples/big_dab_bench.rs new file mode 100644 index 0000000000..b8231b9003 --- /dev/null +++ b/crates/paint/examples/big_dab_bench.rs @@ -0,0 +1,59 @@ +//! Where a large brush's time goes: one dab of a 2000 px tip, by stage. +//! `cargo run --release -p photocraft-paint --example big_dab_bench [size] [hardness] [angle]` +use photocraft_color::PixelFormat; +use photocraft_geom::Rect; +use photocraft_paint::{BrushContext, BrushSettings, Dab, StrokePoint, StrokeRenderer}; +use photocraft_raster::Surface; +use std::time::Instant; + +fn main() { + let args: Vec = std::env::args().collect(); + let size: f32 = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(2000.0); + let hardness: f32 = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0.0); + let angle: f32 = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(0.0); + let brush = BrushSettings { size, hardness, angle, spacing: 0.25, color: [1.0, 0.0, 0.0, 1.0], ..Default::default() }; + let fmt = PixelFormat::RGBA8; + let mut pre = Surface::new(fmt); + pre.fill_rect(Rect::new(0, 0, 6000, 4000), &[0.5, 0.5, 0.5, 1.0]); + let mut target = pre.clone(); + // Stage 1: the tip's coverage alone. + let ctx = BrushContext::new(&brush); + let mut dab = Dab::round(photocraft_geom::Point::new(3000.0, 2000.0), size / 2.0, 1.0); + dab.angle = angle; + let rect = ctx.dab_rect(&dab, false); + let mut cov = Vec::new(); + // Warm the reusable coverage buffer and take the median of seven rasterizations. + ctx.rasterize(&dab, false, rect, &mut cov); + let mut samples = Vec::new(); + for _ in 0..7 { + let t = Instant::now(); + ctx.rasterize(std::hint::black_box(&dab), false, rect, &mut cov); + std::hint::black_box(&cov); + samples.push(t.elapsed()); + } + samples.sort(); + let raster = samples.get(3).copied().unwrap_or_default(); + println!( + "tip {size} px, hardness {hardness}, angle {angle} (24 MP document): dab rect {}x{} = {:.1} MP", + rect.width(), + rect.height(), + rect.width() as f64 * rect.height() as f64 / 1e6 + ); + println!(" rasterize (coverage): {:>7.3} ms", raster.as_secs_f64() * 1e3); + // Stage 2+3: push through the renderer (rasterize + accumulate), then composite. + let mut r = StrokeRenderer::new(&brush, Some(fmt), 1.0); + let t = Instant::now(); + // Two points a pixel apart: the generator places the first dab at once (a lone point waits). + r.push(&[StrokePoint::new(3000.0, 2000.0, 1.0), StrokePoint::new(3001.0, 2000.0, 1.0)]); + let push = t.elapsed(); + println!(" push (rasterize+accumulate): {:>5.1} ms (accumulate ≈ {:.1} ms)", push.as_secs_f64() * 1e3, push.saturating_sub(raster).as_secs_f64() * 1e3); + let t = Instant::now(); + let dmg = r.composite(&pre, &mut target, None, false, false); + let comp = t.elapsed(); + println!(" composite (dirty tiles): {:>7.1} ms over {}x{}", comp.as_secs_f64() * 1e3, dmg.width(), dmg.height()); + // A second dab half a spacing away, as a stroke does: the same again. + let t = Instant::now(); + r.push(&[StrokePoint::new(3000.0 + f64::from(size) * 0.25, 2000.0, 1.0), StrokePoint::new(3002.0 + f64::from(size) * 0.25, 2000.0, 1.0)]); + let dmg = r.composite(&pre, &mut target, None, false, false); + println!(" next dab push+composite: {:>7.1} ms over {}x{}", t.elapsed().as_secs_f64() * 1e3, dmg.width(), dmg.height()); +} diff --git a/crates/paint/examples/coverage_bench.rs b/crates/paint/examples/coverage_bench.rs new file mode 100644 index 0000000000..469738c4c8 --- /dev/null +++ b/crates/paint/examples/coverage_bench.rs @@ -0,0 +1,53 @@ +//! Isolate dab accumulation over a 6016×6016 (36 MP) plane, without rasterization/compositing. +//! Run in release; reports median of five runs after warmup. Compare the same harness on both +//! revisions. Resident tiles model a long stroke; moving dabs also exercise new tile allocation. +use std::hint::black_box; +use std::time::Instant; + +use photocraft_geom::Rect; +use photocraft_paint::render::CoverageMap; + +fn run(size: i32, moving: bool) -> (f64, f32) { + let mut map = CoverageMap::new(3); + // A long stroke has already visited 2048 tiles, including negative coordinates. + for y in -16..16 { + for x in -32..32 { + map.accumulate(Rect::new(x * 64, y * 64, x * 64 + 1, y * 64 + 1), &[0.1], 0.8, None, None); + } + } + let vals: Vec = (0..size * size) + .map(|i| { + let x = (i % size) as f32 + 0.5 - size as f32 / 2.0; + let y = (i / size) as f32 + 0.5 - size as f32 / 2.0; + (1.0 - (x * x + y * y).sqrt() / (size as f32 / 2.0)).max(0.0) * 0.1 + }) + .collect(); + let dabs = 4000; + let start = Instant::now(); + for i in 0..dabs { + let (x, y) = if moving { ((i * 17) % (6016 - size), (i * 7) % (6016 - size)) } else { (i % 31 - 15, i % 29 - 14) }; + map.accumulate(black_box(Rect::new(x, y, x + size, y + size)), black_box(&vals), 0.8, None, None); + } + let micros = start.elapsed().as_secs_f64() * 1e6 / f64::from(dabs); + let checksum = black_box(map.get(0, 0) + map.get(128, 128)); + (micros, checksum) +} + +fn main() { + for size in [20, 50, 128, 256] { + for moving in [false, true] { + black_box(run(size, moving)); + let mut times = Vec::new(); + let mut checksum = 0.0; + for _ in 0..5 { + let (micros, value) = run(size, moving); + times.push(micros); + checksum += value; + } + times.sort_by(f64::total_cmp); + if let Some(median) = times.get(2) { + println!("size={size} moving={moving} us_per_dab={median:.3} checksum={checksum:.7}"); + } + } + } +} diff --git a/crates/paint/examples/stroke_bench.rs b/crates/paint/examples/stroke_bench.rs new file mode 100644 index 0000000000..2a6b59cfa7 --- /dev/null +++ b/crates/paint/examples/stroke_bench.rs @@ -0,0 +1,64 @@ +//! A long stroke of one tip, timed per dab: a zigzag over a 4928×3280 RGBA8 document that sweeps +//! the whole page, `push` then `composite` after every point, as painting does per frame. Small +//! tips are the everyday case; a long stroke grows the stroke's tile map to thousands of tiles, +//! and the time per dab must not grow with it. +//! `cargo run --release -p photocraft-paint --example stroke_bench -- [size] [hardness] [dabs]` +use photocraft_color::PixelFormat; +use photocraft_geom::Rect; +use photocraft_paint::{BrushSettings, StrokePoint, StrokeRenderer}; +use photocraft_raster::Surface; +use std::time::Instant; + +fn main() { + let args: Vec = std::env::args().collect(); + let size: f32 = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(20.0); + let hardness: f32 = args.get(2).and_then(|s| s.parse().ok()).unwrap_or(0.0); + let dabs: usize = args.get(3).and_then(|s| s.parse().ok()).unwrap_or(6000); + let (w, h) = (4928.0f64, 3280.0f64); + let brush = BrushSettings { size, hardness, spacing: 0.25, color: [1.0, 0.0, 0.0, 1.0], ..Default::default() }; + let fmt = PixelFormat::RGBA8; + let mut pre = Surface::new(fmt); + pre.fill_rect(Rect::new(0, 0, w as i32, h as i32), &[0.5, 0.5, 0.5, 1.0]); + let mut target = pre.clone(); + // Segments from the top margin to the bottom one and back, advancing across the width so that + // `dabs` dabs at the brush's spacing sweep the document once. + let step = f64::from(size) * 0.25; + let margin = f64::from(size); + let span = h - 2.0 * margin; + let segments = ((dabs as f64 * step) / span).ceil().max(1.0) as usize; + let dx = (w - 2.0 * margin) / segments as f64; + let mut r = StrokeRenderer::new(&brush, Some(fmt), 1.0); + let (mut t_push, mut t_comp) = (0.0f64, 0.0f64); + let t_all = Instant::now(); + let mut pos = (margin, margin); + r.push(&[StrokePoint::new(pos.0, pos.1, 1.0)]); + let mut pushes = 0usize; + 'stroke: for seg in 0..segments { + let (x0, y0) = pos; + let (x1, y1) = (margin + (seg + 1) as f64 * dx, if seg % 2 == 0 { h - margin } else { margin }); + let n = (((x1 - x0).powi(2) + (y1 - y0).powi(2)).sqrt() / step).ceil().max(1.0) as usize; + for i in 1..=n { + let f = i as f64 / n as f64; + let t = Instant::now(); + r.push(&[StrokePoint::new(x0 + (x1 - x0) * f, y0 + (y1 - y0) * f, 1.0)]); + let tp = t.elapsed().as_secs_f64(); + r.composite(&pre, &mut target, None, false, false); + t_comp += t.elapsed().as_secs_f64() - tp; + t_push += tp; + pushes += 1; + if r.dab_count() >= dabs { + break 'stroke; + } + } + pos = (x1, y1); + } + let total = t_all.elapsed().as_secs_f64(); + let placed = r.dab_count(); + println!( + "tip {size} px, hardness {hardness}: {placed} dabs in {pushes} pushes over {segments} segments: {:.0} ms (push {:.0}, composite {:.0}), {:.1} µs per dab", + total * 1e3, + t_push * 1e3, + t_comp * 1e3, + total * 1e6 / placed.max(1) as f64 + ); +} diff --git a/crates/paint/src/bg_erase.rs b/crates/paint/src/bg_erase.rs index ec0ce0907b..323a90036a 100644 --- a/crates/paint/src/bg_erase.rs +++ b/crates/paint/src/bg_erase.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use crate::Stroke; use crate::replace::{Limits, Sampling}; -use crate::retouch::{Region, alpha_index, apply_dab_stroke}; +use crate::retouch::{Footprint, Region, alpha_index, apply_dab_stroke}; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] @@ -49,16 +49,34 @@ fn dist(a: &[f32], b: &[f32]) -> f32 { /// the selection limits the effect. Returns the damaged rectangle; empty when the surface has no /// alpha channel. pub fn apply_background_eraser(target: &mut Surface, stroke: &Stroke, bs: &BgEraseSettings, selection: Option<&Surface>) -> Rect { - let fmt = target.format(); - let Some(a) = alpha_index(&fmt) else { return Rect::EMPTY }; + let Some(effect) = background_eraser_effect(target, bs) else { return Rect::EMPTY }; + apply_dab_stroke(target, &background_eraser_stroke(stroke), selection, false, 0, effect) +} + +/// The stroke [`apply_background_eraser`] runs its dabs along: full opacity (the tool has none), +/// never the brush's erase flag. Live previews run the same dabs. +pub fn background_eraser_stroke(stroke: &Stroke) -> Stroke { + let mut stroke = stroke.clone(); + stroke.brush.opacity = 1.0; + stroke.brush.erase = false; + stroke +} + +/// One Background Eraser dab's edit of a working copy of `pre` (the target before the stroke): +/// shared by [`apply_background_eraser`] and live previews that run the dabs as they come, so both +/// erase the same pixels. `None` when `pre` has no alpha channel (nothing can be erased). +pub fn background_eraser_effect(pre: &Surface, bs: &BgEraseSettings) -> Option> { + let fmt = pre.format(); + let a = alpha_index(&fmt)?; let nc = a; let tol = if bs.tolerance.is_finite() { bs.tolerance.clamp(0.0, 1.0) } else { 0.0 }; let soft = bs.limits != Limits::FindEdges; let native = |c: [f32; 4]| -> Vec { from_rgba(&fmt, [c[0], c[1], c[2], 1.0])[..nc].to_vec() }; let swatch = native(bs.background); let fg = native(bs.foreground); + let (sampling, limits, protect_foreground) = (bs.sampling, bs.limits, bs.protect_foreground); let mut once: Option>> = None; - let match_w = |d: f32| -> f32 { + let match_w = move |d: f32| -> f32 { if d > tol { 0.0 } else if soft && tol > 0.0 { @@ -68,18 +86,20 @@ pub fn apply_background_eraser(target: &mut Surface, stroke: &Stroke, bs: &BgEra 1.0 } }; - let mut stroke = stroke.clone(); - stroke.brush.opacity = 1.0; - stroke.brush.erase = false; - // The pixels before the stroke, taken at the first dab. Sampling and matching read these, so a - // dab landing on pixels earlier dabs already erased still samples and matches (#1280). + let pre = pre.clone(); + // The pixels before the stroke under the working copy, read once per working area. Sampling + // and matching read these, so a dab landing on pixels earlier dabs already erased still + // samples and matches (#1280). let mut orig: Option = None; - apply_dab_stroke(target, &stroke, selection, false, 0, |work: &mut Region, fp| { + Some(move |work: &mut Region, fp: &Footprint| { let r = fp.rect.intersect(&work.rect); if r.is_empty() { return; } - let orig = &*orig.get_or_insert_with(|| work.clone()); + if orig.as_ref().is_none_or(|o| o.rect != work.rect) { + orig = Some(Region::read(&pre, work.rect)); + } + let Some(orig) = orig.as_ref() else { return }; let (cx, cy) = (fp.dab.center.x.floor() as i32, fp.dab.center.y.floor() as i32); // The hotspot's colour (None where it was transparent before the stroke or is off the // working area). @@ -90,7 +110,7 @@ pub fn apply_background_eraser(target: &mut Surface, stroke: &Stroke, bs: &BgEra let p = orig.px(cx, cy); (p[a] > 0.0).then(|| p[..nc].to_vec()) }; - let sampled = match bs.sampling { + let sampled = match sampling { Sampling::Continuous => hotspot(), Sampling::Once => once.get_or_insert_with(hotspot).clone(), Sampling::BackgroundSwatch => Some(swatch.clone()), @@ -110,13 +130,13 @@ pub fn apply_background_eraser(target: &mut Surface, stroke: &Stroke, bs: &BgEra continue; } let c = &p[..nc]; - if bs.protect_foreground && dist(c, &fg) <= tol { + if protect_foreground && dist(c, &fg) <= tol { continue; } weight[(y - r.y0) as usize * w + (x - r.x0) as usize] = match_w(dist(c, &sampled)); } } - if bs.limits != Limits::Discontiguous { + if limits != Limits::Discontiguous { keep_connected(&mut weight, w, h, (cx.clamp(r.x0, r.x1 - 1) - r.x0) as usize, (cy.clamp(r.y0, r.y1 - 1) - r.y0) as usize); } for y in r.y0..r.y1 { diff --git a/crates/paint/src/brush.rs b/crates/paint/src/brush.rs index 54e74e2e1f..e43da363c5 100644 --- a/crates/paint/src/brush.rs +++ b/crates/paint/src/brush.rs @@ -410,9 +410,9 @@ pub struct SectionLocks { pub protect_texture: bool, } -/// Brush Pose: stylus values used instead of (override) or for missing device data. +/// Brush Pose: fixed values used for the selected stylus-component overrides. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] -#[serde(default, rename_all = "camelCase")] +#[serde(from = "PoseWire", into = "PoseWire")] pub struct Pose { pub enabled: bool, /// Degrees, -90..90 (W3C Pointer Events convention). @@ -422,7 +422,8 @@ pub struct Pose { pub rotation: f32, /// 0..1. pub pressure: f32, - pub override_tilt: bool, + pub override_tilt_x: bool, + pub override_tilt_y: bool, pub override_rotation: bool, pub override_pressure: bool, } @@ -435,13 +436,88 @@ impl Default for Pose { tilt_y: 0.0, rotation: 0.0, pressure: 1.0, - override_tilt: false, + override_tilt_x: false, + override_tilt_y: false, override_rotation: false, override_pressure: false, } } } +// Old presets used one combined flag. Keep that JSON input and readback while the +// model has only the two independent sources of truth. Explicit axis fields win. +#[derive(Serialize, Deserialize)] +#[serde(default, rename_all = "camelCase")] +struct PoseWire { + enabled: bool, + tilt_x: f32, + tilt_y: f32, + rotation: f32, + pressure: f32, + override_tilt: bool, + #[serde(default, deserialize_with = "pose_axis_bool")] + override_tilt_x: Option, + #[serde(default, deserialize_with = "pose_axis_bool")] + override_tilt_y: Option, + override_rotation: bool, + override_pressure: bool, +} + +// Missing axis keys migrate from the old flag; a present null/non-boolean is an error. +fn pose_axis_bool<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { + bool::deserialize(d).map(Some) +} + +impl Default for PoseWire { + fn default() -> Self { + Self { + enabled: false, + tilt_x: 0.0, + tilt_y: 0.0, + rotation: 0.0, + pressure: 1.0, + override_tilt: false, + override_tilt_x: None, + override_tilt_y: None, + override_rotation: false, + override_pressure: false, + } + } +} + +impl From for Pose { + fn from(p: PoseWire) -> Self { + Self { + enabled: p.enabled, + tilt_x: p.tilt_x, + tilt_y: p.tilt_y, + rotation: p.rotation, + pressure: p.pressure, + override_tilt_x: p.override_tilt_x.unwrap_or(p.override_tilt), + override_tilt_y: p.override_tilt_y.unwrap_or(p.override_tilt), + override_rotation: p.override_rotation, + override_pressure: p.override_pressure, + } + } +} + +impl From for PoseWire { + fn from(p: Pose) -> Self { + Self { + enabled: p.enabled, + tilt_x: p.tilt_x, + tilt_y: p.tilt_y, + rotation: p.rotation, + pressure: p.pressure, + override_tilt: p.override_tilt_x && p.override_tilt_y, + override_tilt_x: Some(p.override_tilt_x), + override_tilt_y: Some(p.override_tilt_y), + override_rotation: p.override_rotation, + override_pressure: p.override_pressure, + } + } +} + /// Stroke smoothing (Photoshop's Smoothing % and its options). #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(default, rename_all = "camelCase")] @@ -500,6 +576,8 @@ pub struct BrushSettings { pub background: [f32; 4], /// Erase instead of paint. pub erase: bool, + /// Paint beneath existing pixels; opaque pixels and transparency-locked layers are unchanged. + pub behind: bool, /// Brush blend mode (Normal, Multiply, Dissolve…). pub mode: BlendMode, // --- Brush Tip Shape --- @@ -558,6 +636,7 @@ impl Default for BrushSettings { color_cmyk: None, background: [1.0, 1.0, 1.0, 1.0], erase: false, + behind: false, mode: BlendMode::Normal, tip: TipShape::Round, angle: 0.0, @@ -589,9 +668,19 @@ impl Default for BrushSettings { impl BrushSettings { /// Resize the primary tip and keep an enabled Dual Brush tip at the same relative size. /// An explicit Dual Brush size edit should bypass this and retain the requested diameter. + /// + /// As in Photoshop, the dual diameter is multiplied by the size ratio unrounded and only capped + /// at the top ([`MAX_BRUSH_SIZE`]): a 100 px brush with a 20 px dual tip shrunk to 1 px has a + /// 0.2 px dual tip and grows back to exactly 20 px. A sub-pixel floor here would lose the + /// proportion on the way back up (#4809); rendering bounds the diameter itself + /// ([`Self::bounded_for_render`]). pub fn resize_with_dual_brush(&mut self, new_size: f32) { if self.dual_brush.enabled && self.size.is_finite() && self.size > 0.0 && new_size.is_finite() && new_size > 0.0 { - self.dual_brush.size = (self.dual_brush.size * (new_size / self.size)).clamp(0.5, MAX_BRUSH_SIZE); + let scaled = self.dual_brush.size * (new_size / self.size); + // NaN fails the comparison; infinity is capped. + if scaled > 0.0 { + self.dual_brush.size = scaled.min(MAX_BRUSH_SIZE); + } } self.size = new_size; } diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs index d0d7de4110..8b024de5a6 100644 --- a/crates/paint/src/brush_tests.rs +++ b/crates/paint/src/brush_tests.rs @@ -155,6 +155,8 @@ fn pencil_dabs_sit_on_the_pixel_grid() { // Odd sizes centre on the pixel holding the point, even ones on the nearest corner. assert_eq!(grid_center(10.0, 5.0, 1.0), (10.5, 5.5)); assert_eq!(grid_center(10.9, 5.2, 3.0), (10.5, 5.5)); + assert_eq!(grid_center(10.0 - 5e-8, 5.0 - 5e-8, 1.0), (10.5, 5.5)); + assert_eq!(grid_center(10.0 - 2e-7, 5.0 - 2e-7, 1.0), (9.5, 4.5)); assert_eq!(grid_center(10.4, 5.6, 2.0), (10.0, 6.0)); assert_eq!(grid_square(10.2, 5.7, 1.0), [10.0, 5.0, 11.0, 6.0]); assert_eq!(grid_square(10.2, 5.7, 4.0), [8.0, 4.0, 12.0, 8.0]); @@ -189,28 +191,98 @@ fn pencil_dabs_sit_on_the_pixel_grid() { #[test] fn pencil_1px_line_is_a_clean_8_connected_staircase() { - // #2139: a 1 px Pencil line is one pixel per column (or row, when steep) on the ideal line - // between the end pixels: no doubled pixels at the steps. + // #2139: a 1 px Pencil line is one pixel per column (or row, when steep) on the line: no + // doubled pixels at the steps. let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() }; let cases = [((2.5, 2.5), (22.5, 9.5)), ((3.5, 30.5), (10.5, 5.5)), ((1.2, 1.7), (25.8, 4.1)), ((5.5, 5.5), (17.5, 17.5)), ((20.3, 3.6), (4.9, 12.2))]; for ((ax, ay), (bx, by)) in cases { let s = paint(&b, &[StrokePoint::new(ax, ay, 1.0), StrokePoint::new(bx, by, 1.0)], 40, 40); let painted: Vec<(i32, i32)> = (0..40).flat_map(|y| (0..40).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect(); let (a, e) = ((ax.floor(), ay.floor()), (bx.floor(), by.floor())); - let steep = (e.1 - a.1).abs() > (e.0 - a.0).abs(); + let steep = (by - ay).abs() > (bx - ax).abs(); let major = |p: (f64, f64)| if steep { (p.1, p.0) } else { (p.0, p.1) }; - let ((m0, n0), (m1, n1)) = (major(a), major(e)); + let ((m0, _), (m1, _)) = (major(a), major(e)); + let ((am, an), (bm, bn)) = (major((ax, ay)), major((bx, by))); assert_eq!(painted.len() as f64, (m1 - m0).abs() + 1.0, "{a:?}->{e:?}: one pixel per step, got {painted:?}"); for &(x, y) in &painted { - let (m, n) = major((f64::from(x), f64::from(y))); - let ideal = n0 + (n1 - n0) * (m - m0) / (m1 - m0); - assert!((n - ideal).abs() <= 0.5 + 1e-9, "{a:?}->{e:?}: ({x},{y}) is off the line"); + if (f64::from(x), f64::from(y)) == a { + continue; // The pixel the stroke starts in. + } + // The pixel's centre is within half a pixel of the line where it crosses its column. + let (m, n) = major((f64::from(x) + 0.5, f64::from(y) + 0.5)); + let on_line = an + (bn - an) * (m - am) / (bm - am); + assert!((n - on_line).abs() <= 0.5 + 1e-9, "{a:?}->{e:?}: ({x},{y}) is off the line"); } let columns: std::collections::BTreeSet = painted.iter().map(|&(x, y)| major((f64::from(x), f64::from(y))).0 as i64).collect(); assert_eq!(columns.len(), painted.len(), "{a:?}->{e:?}: a column holds two pixels"); } } +#[test] +fn pencil_1px_line_is_the_same_whichever_way_and_however_smoothed() { + // #6336: a ⇧-click line between pixel centres painted different pixels depending on which end + // it started from, and Smoothing (which splits it into pieces along the line) bent it. + let pixels = |a: (f64, f64), e: (f64, f64), amount: f32| { + let mut b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() }; + b.smoothing.amount = amount; + let s = paint(&b, &[StrokePoint::new(a.0, a.1, 1.0), StrokePoint::new(e.0, e.1, 1.0)], 24, 24); + (0..24).flat_map(|y| (0..24).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect::>() + }; + for (a, e) in [((3.5, 3.5), (4.5, 5.5)), ((3.5, 3.5), (8.5, 5.5)), ((2.5, 20.5), (9.5, 17.5)), ((1.5, 1.5), (20.5, 9.5))] { + let line = pixels(a, e, 0.0); + assert_eq!(line, pixels(e, a, 0.0), "{a:?}->{e:?} differs from {e:?}->{a:?}"); + for amount in [0.1, 0.5, 0.9] { + assert_eq!(pixels(a, e, amount), line, "{a:?}->{e:?} at Smoothing {amount}"); + assert_eq!(pixels(e, a, amount), line, "{e:?}->{a:?} at Smoothing {amount}"); + } + } + // One right and two down: three pixels, one per row. + assert_eq!(pixels((3.5, 3.5), (4.5, 5.5), 0.1), vec![(3, 3), (4, 4), (4, 5)]); + // A straight line fed as many pointer samples along it paints the line's pixels. + let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() }; + let samples: Vec = (0..=37).map(|i| StrokePoint::new(1.5 + 19.0 * f64::from(i) / 37.0, 1.5 + 8.0 * f64::from(i) / 37.0, 1.0)).collect(); + let s = paint(&b, &samples, 24, 24); + let sampled: Vec<(i32, i32)> = (0..24).flat_map(|y| (0..24).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect(); + assert_eq!(sampled, pixels((1.5, 1.5), (20.5, 9.5), 0.0)); +} + +#[test] +fn pencil_1px_freehand_line_never_breaks_where_it_turns() { + // #8040: each segment of a freehand stroke samples its own line, so where the stroke turned + // the last pixel of one segment and the first of the next could sit two pixels apart. + let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() }; + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut rnd = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as f64 / (1u64 << 53) as f64 + }; + for _ in 0..300 { + let mut pts = vec![StrokePoint::new(20.0 + rnd() * 8.0, 20.0 + rnd() * 8.0, 1.0)]; + for _ in 0..6 { + let l = *pts.last().unwrap(); + let (dx, dy) = ((rnd() - 0.5) * 6.0, (rnd() - 0.5) * 6.0); + pts.push(StrokePoint::new((l.x + dx).clamp(1.0, 46.0), (l.y + dy).clamp(1.0, 46.0), 1.0)); + } + let s = paint(&b, &pts, 48, 48); + let painted: Vec<(i32, i32)> = (0..48).flat_map(|y| (0..48).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect(); + // Every painted pixel is reachable from the first through 8-connected neighbours. + let mut reached = vec![painted[0]]; + let mut i = 0; + while i < reached.len() { + let (x, y) = reached[i]; + for &q in &painted { + if (q.0 - x).abs() <= 1 && (q.1 - y).abs() <= 1 && !reached.contains(&q) { + reached.push(q); + } + } + i += 1; + } + let path: Vec<(f64, f64)> = pts.iter().map(|p| (p.x, p.y)).collect(); + assert_eq!(reached.len(), painted.len(), "the line breaks along {path:?}"); + } +} // ---------- spacing ---------- #[test] @@ -243,6 +315,58 @@ fn stroke_has_no_seams() { } } +#[test] +fn hard_round_presets_have_smooth_large_stroke_outlines() { + // #8842: the centre can be solid while widely spaced discs scallop the outline. + // Measure the rendered 50% edge of a 150 px stroke, including its sub-pixel position. + for preset in crate::presets::builtin().into_iter().filter(|p| p.name.starts_with("Hard Round")) { + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for smoothing in [0.0, 0.49] { + let mut b = preset.brush.clone(); + b.size = 150.0; + b.smoothing.amount = smoothing; + let mut s = Surface::new(fmt); + render_stroke(&mut s, &b, &line(100.0, 800.0, 120.0), None, false, 1.0); + let (mut min, mut max) = (f32::INFINITY, f32::NEG_INFINITY); + for x in 220..680 { + let edge = (0..120) + .find_map(|y| { + let (a, next) = (s.rgba(x, y)[3], s.rgba(x, y + 1)[3]); + (a <= 0.5 && next > 0.5).then(|| y as f32 + (0.5 - a) / (next - a)) + }) + .expect("a full-pressure stroke has a half-coverage edge"); + min = min.min(edge); + max = max.max(edge); + } + assert!(max - min < 0.25, "{} {fmt:?} smoothing {smoothing}: scalloped edge spans {} px", preset.name, max - min); + } + } + } +} + +#[test] +fn hard_round_presets_have_continuous_pressure_opacity_tapers() { + // #8842 also shows nested disc boundaries when pressure controls opacity and size. + // A smooth input ramp must not turn into several-percent steps between adjacent pixels. + let points: Vec<_> = + (0..=100).map(|i| StrokePoint { time: i as f64 * 8.0, ..StrokePoint::new(100.0 + i as f64 * 7.0, 120.0, 0.95 - i as f32 * 0.009) }).collect(); + for preset in crate::presets::builtin().into_iter().filter(|p| p.name.starts_with("Hard Round")) { + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for smoothing in [0.0, 0.49] { + let mut b = preset.brush.clone(); + b.size = 150.0; + b.pressure_opacity = true; + b.smoothing.amount = smoothing; + let mut s = Surface::new(fmt); + render_stroke(&mut s, &b, &points, None, false, 1.0); + assert!(s.rgba(200, 120)[3] > 0.7 && s.rgba(699, 120)[3] < 0.3, "{}: the stroke must still taper", preset.name); + let max_jump = (200..699).map(|x| (s.rgba(x + 1, 120)[3] - s.rgba(x, 120)[3]).abs()).fold(0.0f32, f32::max); + assert!(max_jump < 0.02, "{} {fmt:?} smoothing {smoothing}: opacity jumps by {max_jump}", preset.name); + } + } + } +} + // ---------- dynamics ---------- #[test] @@ -874,6 +998,165 @@ fn smoothing_without_catch_up_stops_while_the_pointer_pauses() { assert_eq!(out.len(), n, "held still: the brush doesn't move"); } +#[test] +fn a_smoothed_brush_keeps_the_pressure_of_where_it_is_when_the_pen_lifts() { + // #5713: a fast stroke at full pressure, lifted in the last sample. The brush trails far + // behind the pointer; it used to take the lift's pressure there, and the end catch-up drew + // a hairline from the trailing brush to the pointer. + let pts: Vec = + (0..=20).map(|i| StrokePoint { time: f64::from(i) * 4.0, ..StrokePoint::new(f64::from(i) * 10.0, 0.0, if i < 20 { 1.0 } else { 0.05 }) }).collect(); + let out = smooth_points(&Smoothing { amount: 0.8, ..Default::default() }, 1.0, &pts); + let (last, rest) = out.split_last().unwrap(); + assert_eq!((last.x, last.pressure), (200.0, 0.05), "the catch-up still ends at the pen's last sample"); + let lifted = rest.last().unwrap(); + assert!(lifted.x < 190.0, "the brush lags the pointer: {}", lifted.x); + for p in rest { + assert!(p.pressure > 0.5, "the trailing brush at x {} has pressure {}", p.x, p.pressure); + } +} + +#[test] +fn a_pulled_string_brush_takes_the_pose_along_the_string() { + let ps = Smoothing { amount: 0.2, pulled_string: true, catch_up_on_end: false, ..Default::default() }; + let a = StrokePoint { rotation: 350.0, ..StrokePoint::new(0.0, 0.0, 1.0) }; + let b = StrokePoint { rotation: 10.0, tilt_x: 40.0, ..StrokePoint::new(100.0, 0.0, 0.0) }; + let out = smooth_points(&ps, 1.0, &[a, b]); + // The 20 px string leaves the brush 80 % of the way to the pointer. + let p = out.last().unwrap(); + assert!((p.x - 80.0).abs() < 1e-4, "{}", p.x); + assert!((p.pressure - 0.2).abs() < 1e-4, "{}", p.pressure); + assert!((p.tilt_x - 32.0).abs() < 1e-3, "{}", p.tilt_x); + // Rotation the short way round: 350° + 80 % of 20°. + assert!((p.rotation - 6.0).abs() < 1e-3, "{}", p.rotation); +} + +#[test] +fn a_smoothed_brush_catches_up_with_a_pen_lifted_in_place() { + // #7747: only the pressure changes while the pointer stands still. The brush lags in pose, + // so catch-up must keep running and the stroke end at the pen's last pressure. + let cfg = Smoothing { amount: 0.8, ..Default::default() }; + let mut s = Smoother::new(&cfg, 1.0); + let mut out = Vec::new(); + s.push(StrokePoint::new(50.0, 50.0, 1.0), &mut out); + s.push(StrokePoint { time: 32.0, ..StrokePoint::new(50.0, 50.0, 0.0) }, &mut out); + assert!(out.last().unwrap().pressure > 0.5, "the brush trails in pressure: {:?}", out.last()); + assert!(s.lagging(), "a pose behind the pointer is still catching up"); + // Held long enough, the brush arrives and stops asking for time. + for i in 1..=200 { + s.push(StrokePoint { time: 32.0 + f64::from(i) * 16.0, ..StrokePoint::new(50.0, 50.0, 0.0) }, &mut out); + } + assert!(!s.lagging(), "{:?}", out.last()); + s.finish(&mut out); + assert_eq!(out.last().unwrap().pressure, 0.0); +} + +#[test] +fn catch_up_on_stroke_end_finishes_the_pose_of_a_still_pen() { + // #7747: tilt, rotation and wheel changed in place; the end catch-up lands on all of them. + let cfg = Smoothing { amount: 0.8, ..Default::default() }; + let a = StrokePoint { rotation: 350.0, ..StrokePoint::new(50.0, 50.0, 1.0) }; + let b = StrokePoint { time: 16.0, tilt_x: 40.0, tilt_y: -20.0, rotation: 10.0, wheel: 0.5, ..StrokePoint::new(50.0, 50.0, 1.0) }; + let mut s = Smoother::new(&cfg, 1.0); + let mut out = Vec::new(); + s.push(a, &mut out); + s.push(b, &mut out); + assert!(s.lagging()); + s.finish(&mut out); + let p = out.last().unwrap(); + assert_eq!((p.tilt_x, p.tilt_y, p.rotation, p.wheel), (40.0, -20.0, 10.0, 0.5)); + // Nothing left to catch up: no extra point. + let n = out.len(); + s.finish(&mut out); + assert_eq!(out.len(), n); +} + +#[test] +fn pose_axes_override_only_the_selected_stylus_components() { + let input = StrokePoint { x: 20.0, y: 30.0, pressure: 0.71, tilt_x: 13.0, tilt_y: -27.0, rotation: 67.0, wheel: 0.24, time: 73.0 }; + for enabled in [false, true] { + for (x, y) in [(false, false), (true, false), (false, true), (true, true)] { + let b: BrushSettings = serde_json::from_value(serde_json::json!({"pose": { + "enabled": enabled, "tiltX": 45, "tiltY": -18, + "overrideTiltX": x, "overrideTiltY": y, + "pressure": 0.4, "rotation": 120 + }})) + .unwrap(); + let mut expected = input; + if enabled && x { + expected.tilt_x = 45.0; + } + if enabled && y { + expected.tilt_y = -18.0; + } + assert_eq!(crate::dynamics::apply_pose(&b, input), expected, "enabled={enabled}, X={x}, Y={y}"); + } + } +} + +#[test] +fn pose_axes_reach_dabs_and_pixels_at_all_rgba_depths() { + let points = [ + StrokePoint { tilt_x: 13.0, tilt_y: -27.0, ..StrokePoint::new(20.0, 30.0, 0.71) }, + StrokePoint { tilt_x: -31.0, tilt_y: 42.0, ..StrokePoint::new(75.0, 45.0, 0.83) }, + ]; + for (x, y) in [(true, false), (false, true), (true, true)] { + let b: BrushSettings = serde_json::from_value(serde_json::json!({ + "size": 20, "roundness": 0.47, "angle": -23, + "pressureSize": false, "smoothing": {"enabled": false}, + "shapeDynamics": {"enabled": true, "brushProjection": true}, + "pose": {"enabled": true, "tiltX": 45, "tiltY": -18, "overrideTiltX": x, "overrideTiltY": y} + })) + .unwrap(); + let expected_points: Vec<_> = + points.iter().map(|p| StrokePoint { tilt_x: if x { 45.0 } else { p.tilt_x }, tilt_y: if y { -18.0 } else { p.tilt_y }, ..*p }).collect(); + let mut expected_brush = b.clone(); + expected_brush.pose.enabled = false; + let actual_dabs = dabs_of(&b, &points); + let expected_dabs = dabs_of(&expected_brush, &expected_points); + assert_eq!(actual_dabs.len(), expected_dabs.len()); + for (index, (actual, expected)) in actual_dabs.iter().zip(&expected_dabs).enumerate() { + assert_eq!(actual, expected, "resolved tilt reaches dab {index}"); + } + for format in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let mut actual = Surface::new(format); + let mut expected = Surface::new(format); + render_stroke(&mut actual, &b, &points, None, false, 1.0); + render_stroke(&mut expected, &expected_brush, &expected_points, None, false, 1.0); + assert!(actual.rgba(20, 30)[3] > 0.0, "the comparison includes painted pixels"); + for py in 0..80 { + for px in 0..100 { + assert_eq!(actual.rgba(px, py), expected.rgba(px, py), "{format:?}, X={x}, Y={y}, pixel ({px},{py})"); + } + } + } + } +} + +#[test] +fn pose_axes_legacy_combined_override_and_existing_rotation_pressure_are_preserved() { + let input = StrokePoint { tilt_x: 13.0, tilt_y: -27.0, pressure: 0.71, rotation: 67.0, ..Default::default() }; + for enabled in [false, true] { + for legacy in [false, true] { + let b: BrushSettings = serde_json::from_value(serde_json::json!({"pose": { + "enabled": enabled, "tiltX": 45, "tiltY": -18, "overrideTilt": legacy, + "pressure": 0.4, "overridePressure": true, "rotation": 120, "overrideRotation": true + }})) + .unwrap(); + let expected = StrokePoint { + tilt_x: if enabled && legacy { 45.0 } else { input.tilt_x }, + tilt_y: if enabled && legacy { -18.0 } else { input.tilt_y }, + pressure: if enabled { 0.4 } else { input.pressure }, + rotation: if enabled { 120.0 } else { input.rotation }, + ..input + }; + assert_eq!(crate::dynamics::apply_pose(&b, input), expected); + let stored = serde_json::to_vec(&b).unwrap(); + let restored: BrushSettings = serde_json::from_slice(&stored).unwrap(); + assert_eq!(crate::dynamics::apply_pose(&restored, input), expected, "legacy brush settings retain their painting behavior through persistence"); + } + } +} + #[test] fn pose_overrides_stylus_values() { let b = BrushSettings { @@ -958,6 +1241,41 @@ fn blend_mode_and_dissolve() { assert!(vals.iter().all(|&v| v == 0.0 || v == 1.0) && vals.contains(&0.0) && vals.contains(&1.0)); } +#[test] +fn behind_preserves_native_channels_and_respects_alpha_selection_and_locks() { + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for sample in [SampleType::U8, SampleType::U16, SampleType::F32] { + let fmt = PixelFormat::new(mode, sample, true); + let b = BrushSettings { size: 12.0, behind: true, opacity: 0.5, color: [0.1, 0.3, 0.8, 1.0], ..brush() }; + let mut s = Surface::new(fmt); + let native = photocraft_raster::from_rgba(&fmt, [0.8, 0.2, 0.1, 0.5]); + s.fill_rect(Rect::new(0, 0, 12, 12), &native); + let before = s.pixel(6, 6); + let mut selection = Surface::new(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false)); + selection.fill_rect(Rect::new(0, 0, 8, 12), &[0.5]); + render_stroke(&mut s, &b, &[StrokePoint::new(6.0, 6.0, 1.0)], Some(&selection), false, 1.0); + let source = photocraft_raster::from_rgba(&fmt, b.color); + let a = fmt.channels() - 1; + let under = 0.25 * (1.0 - before[a]); + let alpha = before[a] + under; + let got = s.pixel(6, 6); + assert!((got[a] - alpha).abs() < 0.005, "{fmt:?}: {got:?}"); + for c in 0..a { + let expected = (before[c] * before[a] + source[c] * under) / alpha; + assert!((got[c] - expected).abs() < 0.005, "{fmt:?} channel {c}: {got:?}, expected {expected}"); + } + assert_eq!(s.pixel(9, 6), before, "outside selection"); + let locked = s.clone(); + render_stroke(&mut s, &b, &[StrokePoint::new(6.0, 6.0, 1.0)], None, true, 1.0); + assert_eq!(s.read_region(Rect::new(0, 0, 12, 12)), locked.read_region(Rect::new(0, 0, 12, 12)), "locked transparency"); + s.fill_rect(Rect::new(0, 0, 12, 12), &photocraft_raster::from_rgba(&fmt, [0.8, 0.2, 0.1, 1.0])); + let opaque = s.pixel(6, 6); + render_stroke(&mut s, &b, &[StrokePoint::new(6.0, 6.0, 1.0)], None, false, 1.0); + assert_eq!(s.pixel(6, 6), opaque, "opaque pixels"); + } + } +} + #[test] fn presets_render_and_round_trip() { let presets = crate::presets::builtin(); @@ -1033,6 +1351,7 @@ fn live_union_of_passes_matches_merging_the_finished_passes_after_every_chunk() brushes.push(b); brushes.push(BrushSettings { wet_edges: true, ..base.clone() }); brushes.push(BrushSettings { mode: BlendMode::Dissolve, ..base.clone() }); + brushes.push(BrushSettings { behind: true, ..base.clone() }); brushes.push(BrushSettings { erase: true, ..base.clone() }); let mut b = base.clone(); b.texture.enabled = true; @@ -1493,3 +1812,186 @@ fn an_aliased_stroke_at_partial_opacity_is_one_flat_colour() { assert_eq!(s.rgba(x, 5), first, "pixel {x} of the Pencil line"); } } + +/// Large tips rasterize, accumulate and composite in parallel (rows of a dab, tiles of a stroke); +/// the pixels are the ones the serial path gives, with 8-bit dithering, a selection, Dissolve and +/// per-dab colour included. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn large_tips_render_the_same_in_parallel_and_serially() { + let mut sel = Surface::new(PixelFormat::GRAY8); + sel.fill_rect(Rect::new(0, 0, 900, 700), &[1.0]); + sel.fill_rect(Rect::new(300, 0, 400, 700), &[0.0]); + let strokes: Vec<(BrushSettings, Vec)> = vec![ + (BrushSettings { size: 700.0, hardness: 0.0, flow: 0.6, color: [0.9, 0.2, 0.1, 1.0], ..brush() }, line(200.0, 700.0, 350.0)), + ( + BrushSettings { size: 420.0, hardness: 1.0, opacity: 0.5, mode: BlendMode::Dissolve, color: [0.1, 0.5, 0.9, 1.0], ..brush() }, + line(150.0, 750.0, 300.0), + ), + ( + BrushSettings { + size: 500.0, + hardness: 0.3, + color_dynamics: ColorDynamics { enabled: true, hue_jitter: 0.5, per_tip: true, ..Default::default() }, + color: [0.2, 0.8, 0.3, 1.0], + ..brush() + }, + line(100.0, 800.0, 400.0), + ), + ]; + let render = |parallel_from: usize| -> Vec> { + crate::render::set_parallel_min_pixels(parallel_from); + strokes + .iter() + .map(|(b, pts)| { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 900, 700), &[0.5, 0.5, 0.5, 1.0]); + let (mut with_sel, mut plain) = (s.clone(), s); + render_stroke(&mut with_sel, b, pts, Some(&sel), false, 1.0); + render_stroke(&mut plain, b, pts, None, false, 1.0); + let mut px = with_sel.read_region(Rect::new(0, 0, 900, 700)); + px.extend(plain.read_region(Rect::new(0, 0, 900, 700))); + px + }) + .collect() + }; + // The threshold is process-wide: put the default back however the test ends, a panic + // inside a render included. + struct Restore; + impl Drop for Restore { + fn drop(&mut self) { + crate::render::set_parallel_min_pixels(crate::render::DEFAULT_PARALLEL_MIN_PIXELS); + } + } + let _restore = Restore; + let parallel = render(0); + let serial = render(usize::MAX); + for (i, (p, s)) in parallel.iter().zip(&serial).enumerate() { + assert_eq!(p.len(), s.len()); + let diff = p.iter().zip(s).filter(|(a, b)| a != b).count(); + assert_eq!(diff, 0, "stroke {i}: {diff} pixels differ between the parallel and the serial render"); + assert!(p.iter().any(|v| (*v - 0.5).abs() > 0.1), "stroke {i} painted something"); + } +} + +/// A CMYK document with its own profile: blend modes other than Normal convert each pixel through +/// that profile, which is active per thread, so a stroke composited in parallel gives the pixels the +/// serial path gives (the workers enter the caller's CMYK space too). +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_parallel_composite_converts_through_the_documents_cmyk_profile() { + use photocraft_cms::synth::{CmykParams, cmyk_profile}; + use photocraft_color::convert::{CmykSpace, with_cmyk_space}; + let profile = + cmyk_profile(&CmykParams { description: "Brush Test CMYK".into(), tvi: [0.26, 0.26, 0.26, 0.3], grid_a2b: 5, grid_b2a: 9, ..Default::default() }); + let bytes = profile.to_bytes(); + let Some(space) = CmykSpace::for_profile(Some(&bytes)) else { panic!("the synthetic profile is a CMYK space") }; + let fmt = PixelFormat::new(ColorMode::Cmyk, SampleType::U16, true); + // A 40 px Multiply line over 1100 px dirties more than 16 tiles: the composite's batch is + // worth the threads at the default threshold, and always at threshold 0. + let b = BrushSettings { size: 40.0, hardness: 0.5, mode: BlendMode::Multiply, color: [0.2, 0.4, 0.9, 1.0], ..brush() }; + let pts = line(40.0, 1140.0, 60.0); + let full = Rect::new(0, 0, 1200, 120); + let render = |parallel_from: usize| -> Vec { + crate::render::set_parallel_min_pixels(parallel_from); + let mut s = Surface::new(fmt); + s.fill_rect(full, &[0.1, 0.5, 0.3, 0.05, 1.0]); + with_cmyk_space(Some(&space), || render_stroke(&mut s, &b, &pts, None, false, 1.0)); + s.read_region(full) + }; + struct Restore; + impl Drop for Restore { + fn drop(&mut self) { + crate::render::set_parallel_min_pixels(crate::render::DEFAULT_PARALLEL_MIN_PIXELS); + } + } + let _restore = Restore; + let parallel = render(0); + let serial = render(usize::MAX); + let diff = parallel.iter().zip(&serial).filter(|(a, b)| a != b).count(); + assert_eq!(diff, 0, "{diff} samples differ between the parallel and the serial composite in a profiled CMYK document"); + assert!(serial.chunks(5).any(|p| (p[1] - 0.5).abs() > 0.05), "the stroke painted something"); +} + +/// Photoshop (measured): a 100 px brush with a 20 px Dual Brush tip resized to 50 / 2 / 1 px has a +/// 10 / 0.4 / 0.2 px dual tip, unrounded and with no floor, and back at 100 px it is 20 px again; past +/// 5000 px the dual tip is capped there and the proportion is lost (#4809). +#[test] +fn resizing_scales_the_dual_tip_like_photoshop() { + let mut b = BrushSettings { size: 100.0, ..brush() }; + b.dual_brush.enabled = true; + b.dual_brush.size = 20.0; + for (main, dual) in [(50.0, 10.0), (2.0, 0.4), (1.0, 0.2), (100.0, 20.0), (240.0, 48.0), (100.0, 20.0)] { + b.resize_with_dual_brush(main); + assert_eq!(b.size, main); + assert!((b.dual_brush.size - dual).abs() < 1e-4, "main {main}: dual {} != {dual}", b.dual_brush.size); + } + // The cap: 300 px at 100 px would be 15000 px at 5000 px; Photoshop stores 5000 and scales from it. + b.dual_brush.size = 300.0; + b.resize_with_dual_brush(MAX_BRUSH_SIZE); + assert_eq!(b.dual_brush.size, MAX_BRUSH_SIZE); + b.resize_with_dual_brush(100.0); + assert_eq!(b.dual_brush.size, 100.0); + // A sub-pixel dual tip renders at the renderer's smallest diameter. + b.dual_brush.size = 20.0; + b.resize_with_dual_brush(1.0); + assert_eq!(b.bounded_for_render().dual_brush.size, 0.5); +} + +#[test] +fn resizing_with_bad_sizes_leaves_the_dual_tip_alone() { + let mut b = BrushSettings { size: 100.0, ..brush() }; + b.dual_brush.enabled = true; + b.dual_brush.size = 20.0; + for bad in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0, -5.0] { + b.size = 100.0; + b.resize_with_dual_brush(bad); + assert_eq!(b.dual_brush.size, 20.0, "{bad}"); + } + for bad_dual in [f32::NAN, 0.0, -3.0] { + b.size = 100.0; + b.dual_brush.size = bad_dual; + b.resize_with_dual_brush(50.0); + assert_eq!(b.dual_brush.size.to_bits(), bad_dual.to_bits(), "a bad dual size {bad_dual} is left as it was"); + } + // A disabled dual tip keeps its size. + b.dual_brush = DualBrush { enabled: false, size: 20.0, ..Default::default() }; + b.size = 100.0; + b.resize_with_dual_brush(50.0); + assert_eq!(b.dual_brush.size, 20.0); +} + +/// `touches` is true wherever `coverage_at` can be above 0, false only where it is 0 for sure. +#[test] +fn touches_covers_every_pixel_with_coverage() { + let b = BrushSettings { size: 6.0, ..brush() }; + let mut r = StrokeRenderer::new(&b, None, 1.0); + r.push(&line(100.0, 300.0, 200.0)); + r.finish(); + for y in (0..400).step_by(3) { + for x in (0..500).step_by(3) { + if r.coverage_at(x, y) > 0.0 { + assert!(r.touches(Rect::new(x, y, x + 1, y + 1)), "({x}, {y})"); + } + } + } + // Across the origin and tile edges (64 px): every pixel, both sides of each edge. + let mut o = StrokeRenderer::new(&b, None, 1.0); + o.push(&[StrokePoint::new(-70.0, -70.0, 1.0), StrokePoint::new(70.0, 70.0, 1.0)]); + o.finish(); + for y in -140..140 { + for x in -140..140 { + if o.coverage_at(x, y) > 0.0 { + assert!(o.touches(Rect::new(x, y, x + 1, y + 1)), "({x}, {y})"); + } + } + } + assert!(!o.touches(Rect::new(-128, 64, -64, 128)), "a tile the diagonal misses"); + assert!(!o.touches(Rect::new(-200, -200, -128, -128)), "ends exactly at a tile edge"); + assert!(o.touches(Rect::new(-200, -200, -127, -127)), "one pixel into the next tile"); + assert!(r.touches(Rect::new(150, 199, 151, 201))); + assert!(!r.touches(Rect::new(0, 0, 50, 50)), "far from the stroke"); + assert!(!r.touches(Rect::new(400, 400, 400, 400)), "empty"); + assert!(r.touches(Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX)), "huge, without a huge scan"); + assert!(!r.touches(Rect::new(1_000_000, 1_000_000, i32::MAX, i32::MAX))); +} diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs index ab846f1959..02d54c18ff 100644 --- a/crates/paint/src/dynamics.rs +++ b/crates/paint/src/dynamics.rs @@ -21,11 +21,25 @@ pub const SPEED_SPACING_MS: f64 = 8.0; /// Longest pixel-grid segment (in pixels) before falling back to distance spacing. const MAX_GRID_STEPS: f64 = 1e7; +/// Widest gap (in pixels) [`PathWalker::bridge`] fills between two pixel-grid segments. +const MAX_BRIDGE: f64 = 4.0; /// Densest speed spacing, in pixels between dabs (a slow, long stroke never floods the buffer). const MIN_SPEED_STEP: f64 = 0.5; /// Maximum airbrush catch-up dabs for one input segment. const MAX_AIRBRUSH_DABS_PER_SEGMENT: usize = 512; +/// Most steps one distance-spaced segment emits: two far-apart points at a quarter-pixel spacing +/// would otherwise be billions of steps, all buffered before the first is drawn. +const MAX_DISTANCE_STEPS_PER_SEGMENT: usize = 1 << 20; + +/// The dab centre of the pixel-grid cell holding `v` (see [`PathWalker::pixel_grid`]): the +/// pixel's centre for `offset` 0.5, the nearest pixel corner for 0. A value within rounding +/// noise of a cell boundary counts as on it, so snapping and line stepping agree. +pub(crate) fn grid_cell(v: f64, offset: f64) -> f64 { + let w = v - offset + 0.5; + let w = if (w - w.round()).abs() < 1e-7 { w.round() } else { w }; + w.floor() + offset +} #[inline] fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint { @@ -46,6 +60,39 @@ fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint { // Smoothing // --------------------------------------------------------------------------- +/// The smoothed brush moved `f` of the way from `brush` towards the pointer `p`. Its pose +/// (pressure, tilt, rotation, wheel) moves with it, so the lagging brush paints with the pose the +/// pen had along the way, not the pointer's newest one. Taking the pointer's pressure made a fast +/// stroke's lift thin the brush while it still trailed far behind, and the end catch-up a hairline +/// (#5713). Time is the pointer's. +fn pull(brush: &StrokePoint, p: &StrokePoint, f: f64) -> StrokePoint { + let ff = f as f32; + // Barrel rotation takes the shorter way round. + let dr = (p.rotation - brush.rotation + 540.0).rem_euclid(360.0) - 180.0; + StrokePoint { + x: brush.x + (p.x - brush.x) * f, + y: brush.y + (p.y - brush.y) * f, + pressure: brush.pressure + (p.pressure - brush.pressure) * ff, + tilt_x: brush.tilt_x + (p.tilt_x - brush.tilt_x) * ff, + tilt_y: brush.tilt_y + (p.tilt_y - brush.tilt_y) * ff, + rotation: (brush.rotation + dr * ff).rem_euclid(360.0), + wheel: brush.wheel + (p.wheel - brush.wheel) * ff, + time: p.time, + } +} + +/// Does the brush's pose (pressure, tilt, rotation, wheel) differ from `p`'s by more than `eps`? +/// [`pull`] moves the pose with the position, so a pen that only changes pressure or tilt in place +/// leaves the brush behind too (#7747). +fn pose_differs(brush: &StrokePoint, p: &StrokePoint, eps: f32) -> bool { + let dr = (p.rotation - brush.rotation + 540.0).rem_euclid(360.0) - 180.0; + (p.pressure - brush.pressure).abs() > eps + || (p.tilt_x - brush.tilt_x).abs() > eps + || (p.tilt_y - brush.tilt_y).abs() > eps + || dr.abs() > eps + || (p.wheel - brush.wheel).abs() > eps +} + /// Incremental stroke smoothing: exponential (with optional time-based catch-up) or pulled string. #[derive(Clone, Debug)] pub struct Smoother { @@ -87,7 +134,7 @@ impl Smoother { let d = (dx * dx + dy * dy).sqrt(); if d > len { let k = (d - len) / d; - let np = StrokePoint { x: pos.x + dx * k, y: pos.y + dy * k, ..p }; + let np = pull(&pos, &p, k); self.pos = Some(np); out.push(np); } @@ -113,7 +160,7 @@ impl Smoother { }; let mut cur = pos; for _ in 0..iters { - cur = StrokePoint { x: cur.x + (p.x - cur.x) * step, y: cur.y + (p.y - cur.y) * step, ..p }; + cur = pull(&cur, &p, step); out.push(cur); } self.pos = Some(cur); @@ -123,7 +170,7 @@ impl Smoother { /// stroke needs time to keep passing (repeats of the last point) while the pointer is held. pub fn lagging(&self) -> bool { match (self.active() && self.cfg.catch_up && !self.cfg.pulled_string, self.pos, self.last_input) { - (true, Some(pos), Some(last)) => (pos.x - last.x).abs() + (pos.y - last.y).abs() > 0.01, + (true, Some(pos), Some(last)) => (pos.x - last.x).abs() + (pos.y - last.y).abs() > 0.01 || pose_differs(&pos, &last, 1e-3), _ => false, } } @@ -136,13 +183,14 @@ impl Smoother { (self.active() && (pos.x != last.x || pos.y != last.y)).then_some(([pos.x, pos.y], [last.x, last.y])) } - /// End of stroke: with Catch-Up On Stroke End the brush finishes at the last pointer position. + /// End of stroke: with Catch-Up On Stroke End the brush finishes at the last pointer position + /// and pose. pub fn finish(&mut self, out: &mut Vec) { if !self.active() || !self.cfg.catch_up_on_end { return; } if let (Some(pos), Some(last)) = (self.pos, self.last_input) - && (pos.x != last.x || pos.y != last.y) + && (pos.x != last.x || pos.y != last.y || pose_differs(&pos, &last, 0.0)) { self.pos = Some(last); out.push(last); @@ -196,6 +244,8 @@ pub struct PathWalker { /// Pixel-grid stepping for small aliased tips: the dab-centre offset inside a pixel (see /// [`PathWalker::pixel_grid`]). grid: Option, + /// The grid cell of the last pixel-grid step, so the next segment joins it (#8040). + cell: Option<(f64, f64)>, } impl PathWalker { @@ -212,14 +262,19 @@ impl PathWalker { speed: None, speed_acc: 0.0, grid: None, + cell: None, } } /// Step pixel by pixel instead of by distance: one step per pixel along the segment's major - /// axis, on the line between the pixels holding its ends, each at the pixel's dab centre - /// (`offset` 0.5 for odd tip diameters, 0 for even ones, as `render::grid_center` snaps). A - /// small aliased tip (the Pencil) then draws a clean 8-connected staircase: stepping by - /// distance put two dabs in a row wherever the line crossed a pixel corner (#2139). + /// axis, each at the dab centre (`offset` 0.5 for odd tip diameters, 0 for even ones, as + /// `render::grid_center` snaps) of the pixel the segment's line crosses at that column (or + /// row, when steep). A small aliased tip (the Pencil) then draws a clean 8-connected + /// staircase: stepping by distance put two dabs in a row wherever the line crossed a pixel + /// corner (#2139). The line is sampled where it really runs, not between the pixels holding + /// the segment's ends, so a straight line split into pieces (by Smoothing, or the pointer + /// samples of a ⇧-drag) paints the pixels of the whole line, whichever end it starts from + /// (#6336). pub fn pixel_grid(mut self, offset: f64) -> Self { self.grid = Some(offset); self @@ -228,24 +283,50 @@ impl PathWalker { /// Emits the pixel steps from `a` to `p` (see [`PathWalker::pixel_grid`]). False, emitting /// nothing, when the span is not finite or implausibly long: distance spacing takes over. fn grid_steps(&mut self, a: &StrokePoint, p: &StrokePoint, offset: f64, out: &mut impl FnMut(StepInput)) -> bool { - let cell = |v: f64| if offset > 0.0 { v.floor() } else { v.round() }; - let (x0, y0) = (cell(a.x), cell(a.y)); - let (dx, dy) = (cell(p.x) - x0, cell(p.y) - y0); - let n = dx.abs().max(dy.abs()); - if !n.is_finite() || n > MAX_GRID_STEPS { + // The pieces of a 45° line differ in rounding noise: keep them all on one major axis. + let steep = (p.y - a.y).abs() > (p.x - a.x).abs() * (1.0 + 1e-9); + let (am, an, pm, pn) = if steep { (a.y, a.x, p.y, p.x) } else { (a.x, a.y, p.x, p.y) }; + let (m0, m1) = (grid_cell(am, offset), grid_cell(pm, offset)); + let (dm, dn) = (pm - am, pn - an); + let n = (m1 - m0).abs(); + if !(n.is_finite() && dn.is_finite()) || n > MAX_GRID_STEPS || dm == 0.0 { return false; } - let steps = n as u64; - for i in 1..=steps { - let i = i as f64; - let mut q = lerp_pt(a, p, i / n); - q.x = x0 + (dx * i / n).round() + offset; - q.y = y0 + (dy * i / n).round() + offset; + let dir = (m1 - m0).signum(); + for i in 1..=n as u64 { + let c = m0 + dir * i as f64; + // The last column's centre can sit up to half a pixel past `p`: the line goes on to + // it, so the next piece of a straight line picks up where this one left off. + let t = (c - am) / dm; + let mut q = lerp_pt(a, p, t.clamp(0.0, 1.0)); + let cn = grid_cell(an + dn * t, offset); + (q.x, q.y) = if steep { (cn, c) } else { (c, cn) }; + self.bridge(&q, out); self.emit(q, out); } true } + /// Fills the pixels between the last grid step and `q` when they don't touch. Each segment + /// samples its own line, so where a freehand stroke turns, the last pixel of one segment and + /// the first of the next can sit two pixels apart, and the 1 px Pencil line breaks (#8040). + fn bridge(&mut self, q: &StrokePoint, out: &mut impl FnMut(StepInput)) { + let Some((mut x, mut y)) = self.cell else { return }; + // Only a turn's small gap: a step cut off by the walker's budget is not filled in. + if (q.x - x).abs().max((q.y - y).abs()) > MAX_BRIDGE { + return; + } + while (q.x - x).abs() > 1.5 || (q.y - y).abs() > 1.5 { + if (q.x - x).abs() > 0.5 { + x += (q.x - x).signum(); + } + if (q.y - y).abs() > 0.5 { + y += (q.y - y).signum(); + } + self.emit(StrokePoint { x, y, ..*q }, out); + } + } + /// Space steps by stroke time instead of distance ([`SPEED_SPACING_MS`]): faster movement /// spreads them out. Segments without timestamps are spaced by distance. pub fn speed_spacing(mut self, interval_ms: f64) -> Self { @@ -254,6 +335,9 @@ impl PathWalker { } fn emit(&mut self, point: StrokePoint, out: &mut impl FnMut(StepInput)) { + if let Some(g) = self.grid { + self.cell = Some((grid_cell(point.x, g), grid_cell(point.y, g))); + } let s = StepInput { point, direction: self.dir, initial_direction: self.initial_dir.unwrap_or(self.dir), step: self.step }; self.step += 1; out(s); @@ -284,12 +368,17 @@ impl PathWalker { match self.speed.filter(|_| dt > 0.0 && dt.is_finite()) { None => { if !self.grid.is_some_and(|g| self.grid_steps(&a, &p, g, out)) { - while self.next_at <= len + 1e-9 { + let mut emitted = 0usize; + while self.next_at <= len + 1e-9 && emitted < MAX_DISTANCE_STEPS_PER_SEGMENT { let q = lerp_pt(&a, &p, (self.next_at / len).min(1.0)); self.emit(q, out); self.next_at += step_len(&q).max(0.25); + emitted += 1; } - self.next_at -= len; + // Cut short: the rest of the segment is skipped, spacing restarts at its end. + // Only when the cap held back a step that was still due; a segment that ends right at the cap keeps its carry-over. + let cut_short = emitted == MAX_DISTANCE_STEPS_PER_SEGMENT && self.next_at <= len + 1e-9; + self.next_at = if cut_short { step_len(&p).max(0.25) } else { self.next_at - len }; } } Some(iv) => { @@ -654,8 +743,10 @@ impl DualBuilder { pub fn apply_pose(b: &BrushSettings, mut p: StrokePoint) -> StrokePoint { let pose = &b.pose; if pose.enabled { - if pose.override_tilt { + if pose.override_tilt_x { p.tilt_x = pose.tilt_x; + } + if pose.override_tilt_y { p.tilt_y = pose.tilt_y; } if pose.override_rotation { @@ -761,3 +852,38 @@ impl DabGenerator { } } } + +#[cfg(test)] +mod walker_budget_tests { + use super::*; + + #[test] + fn one_far_segment_emits_a_bounded_number_of_steps() { + let steps = std::cell::Cell::new(0usize); + let mut count = |_: StepInput| steps.set(steps.get() + 1); + let mut walker = PathWalker::new(None); + walker.push(StrokePoint::new(0.0, 0.0, 1.0), &|_: &StrokePoint| 1.0, &mut count); + // At a quarter pixel, a billion-pixel segment is four billion steps. + walker.push(StrokePoint::new(1.0e9, 0.0, 1.0), &|_: &StrokePoint| 0.25, &mut count); + assert!(steps.get() <= MAX_DISTANCE_STEPS_PER_SEGMENT + 1, "{}", steps.get()); + // Spacing carries on from the end of the cut segment. + let before = steps.get(); + walker.push(StrokePoint::new(1.0e9 + 10.0, 0.0, 1.0), &|_: &StrokePoint| 1.0, &mut count); + assert!(steps.get() > before && steps.get() - before <= 11, "{} more steps", steps.get() - before); + } + + #[test] + fn a_segment_ending_at_the_cap_keeps_its_carry_over() { + let steps = std::cell::Cell::new(0usize); + let mut count = |_: StepInput| steps.set(steps.get() + 1); + let mut walker = PathWalker::new(None); + let one = |_: &StrokePoint| 1.0; + walker.push(StrokePoint::new(0.0, 0.0, 1.0), &one, &mut count); + // Exactly the cap in steps, then 0.75 left before the next is due. + let end = MAX_DISTANCE_STEPS_PER_SEGMENT as f64 + 0.25; + walker.push(StrokePoint::new(end, 0.0, 1.0), &one, &mut count); + let before = steps.get(); + walker.push(StrokePoint::new(end + 0.8, 0.0, 1.0), &one, &mut count); + assert_eq!(steps.get(), before + 1); + } +} diff --git a/crates/paint/src/lib.rs b/crates/paint/src/lib.rs index 2f65fc1a0b..214a28177d 100644 --- a/crates/paint/src/lib.rs +++ b/crates/paint/src/lib.rs @@ -166,6 +166,45 @@ pub fn tip_shape(radius: f32, hardness: f32) -> (f32, f32) { ((r0 * radius).min(radius - edge).max(0.0), (s * radius).max(edge).max(1e-3)) } +/// Dab-invariant round-tip geometry, shared by all pixels and subpixel samples. +#[derive(Clone, Copy)] +pub(crate) struct TipProfile { + core: f32, + shoulder: f32, + reach: f32, +} + +impl TipProfile { + #[inline] + pub(crate) fn new(radius: f32, hardness: f32) -> Self { + let (core, shoulder) = tip_shape(radius, hardness); + Self { core, shoulder, reach: core + 2.6 * shoulder } + } + + #[inline] + pub(crate) fn reach(self) -> f32 { + self.reach + } + + #[inline] + pub(crate) fn falloff(self, d: f32) -> f32 { + if d <= self.core { + return 1.0; + } + // Keep division: multiplying by a cached reciprocal changes coverage bits. + let x = (d - self.core) / self.shoulder; + soft_exp(x * x) + } + + #[inline] + pub(crate) fn coverage(self, d: f32) -> f32 { + if d.is_nan() || d >= self.reach + 0.5 { + return 0.0; + } + self.falloff(d) + } +} + /// `exp(-u)` as `(1 − u/64)⁶⁴` by six squarings: within 1/255 of `exp` where it matters, exact /// arithmetic (the same on every platform) and exactly 0 from `u = 64`. #[inline] @@ -180,12 +219,7 @@ fn soft_exp(u: f32) -> f32 { /// Opacity of a round tip at distance `d` from its centre, edge included (see [`tip_shape`]). #[inline] pub fn tip_falloff(d: f32, radius: f32, hardness: f32) -> f32 { - let (r0, s) = tip_shape(radius, hardness); - if d <= r0 { - return 1.0; - } - let x = (d - r0) / s; - soft_exp(x * x) + TipProfile::new(radius, hardness).falloff(d) } /// How far from its centre a round tip still paints (where [`tip_falloff`] drops below ~0.1 %). @@ -207,11 +241,7 @@ pub fn half_coverage_radius(radius: f32, hardness: f32) -> f32 { /// Coverage of a round dab at distance `d` from its centre. #[inline] pub fn dab_coverage(d: f32, radius: f32, hardness: f32) -> f32 { - // NaN distances paint nothing. - if d.is_nan() || d >= tip_reach(radius, hardness) + 0.5 { - return 0.0; - } - tip_falloff(d, radius, hardness) + TipProfile::new(radius, hardness).coverage(d) } /// Rasterize a stroke onto `target`, optionally limited by a selection (grayscale coverage surface). @@ -321,6 +351,24 @@ mod tests { assert_eq!(s.pixel(70, 10)[3], 0.0); } + #[test] + fn lock_transparency_blends_source_atop_on_semi_transparent_pixels() { + // Half-transparent white under a black stroke at 50%: the colour moves halfway (Photoshop's + // source-atop), not by the source-over weight 0.5 / 0.75 (#5783). + for f in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for mode in [photocraft_color::BlendMode::Normal, photocraft_color::BlendMode::Multiply] { + let mut s = Surface::new(f); + s.fill_rect(Rect::new(0, 0, 100, 20), &[1.0, 1.0, 1.0, 0.5]); + let mut st = stroke(&[(10.0, 10.0), (90.0, 10.0)], 8.0); + st.brush.opacity = 0.5; + st.brush.mode = mode; + apply_stroke(&mut s, &st, None, true); + let p = s.rgba(50, 10); + assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{f:?} {mode:?} {p:?}"); + } + } + } + #[test] fn pressure_changes_size() { let mut st = stroke(&[(0.0, 0.0), (100.0, 0.0)], 20.0); diff --git a/crates/paint/src/mixer.rs b/crates/paint/src/mixer.rs index afb8092cd2..a9a7485fa5 100644 --- a/crates/paint/src/mixer.rs +++ b/crates/paint/src/mixer.rs @@ -6,7 +6,7 @@ //! weight. The reservoir amount drains per dab unless `load` is 100 %, so low Load strokes dry out. //! A dry brush (`wet = 0`) paints only reservoir colour; an empty, wet brush smears canvas colour. -use photocraft_raster::{Surface, from_rgba, to_rgba}; +use photocraft_raster::{Surface, from_rgba, from_rgba_into, to_rgba}; use serde::{Deserialize, Serialize}; use crate::Stroke; @@ -26,11 +26,28 @@ pub struct MixerSettings { pub flow: f32, /// Pick up colour from all visible layers instead of the target layer only. pub sample_all_layers: bool, + /// Load the brush with the stroke color after each stroke (the automatic Load option). + #[serde(alias = "reloadAfterStroke")] + pub load_after_stroke: bool, + /// Empty the brush after each stroke (the automatic Clean option). + pub clean_after_stroke: bool, } impl Default for MixerSettings { fn default() -> Self { - Self { wet: 0.5, load: 0.5, mix: 0.5, flow: 1.0, sample_all_layers: false } + Self { wet: 0.5, load: 0.5, mix: 0.5, flow: 1.0, sample_all_layers: false, clean_after_stroke: true, load_after_stroke: true } + } +} + +impl MixerSettings { + /// Effective per-dab wetness, including Transfer's Wetness control. + pub fn effective_wet(&self, dab: &crate::Dab) -> f32 { + self.wet.clamp(0.0, 1.0) * dab.wet.clamp(0.0, 1.0) + } + + /// Whether this dab can update the pickup well from the canvas. + pub fn samples_canvas(&self, dab: &crate::Dab) -> bool { + self.effective_wet(dab) > 0.0 } } @@ -44,18 +61,104 @@ pub struct MixerState { pub amount: f32, /// Colour picked up from the canvas (straight RGBA), None = clean. pub pickup: Option<[f32; 4]>, + /// The foreground colour in effect when the reservoir was loaded from the canvas + /// (`load_sampled`), None when it holds the foreground itself. + pub sampled_over: Option<[f32; 4]>, + /// A bounded 64 x 64 spatial load in tip-local coordinates. Empty cells carry no paint. + pub spatial: Option>, + /// The color recorded by the last stroke (its color) or the last Load Brush or Clean Brush + /// (the foreground). None = no stroke, load or clean yet. + pub color: Option<[f32; 4]>, } impl MixerState { - /// Load the reservoir with a colour (full). + /// Load Brush: fill the reservoir with a color (full). pub fn load(&mut self, color: [f32; 4]) { + self.spatial = None; self.reservoir = Some(color); self.amount = 1.0; + self.sampled_over = None; } - /// Clean the brush (drop the picked-up colour). + + /// Load the reservoir with a colour sampled from the canvas (⌥-click) while `foreground` is + /// the foreground colour; `holds_sample` stays true until the foreground changes. + pub fn load_sampled(&mut self, color: [f32; 4], foreground: [f32; 4]) { + self.load(color); + self.sampled_over = Some(foreground); + self.color = Some(foreground); + } + /// Clean Brush: empty the reservoir and the pickup well. pub fn clean(&mut self) { + self.spatial = None; + self.reservoir = None; + self.amount = 0.0; self.pickup = None; + self.sampled_over = None; } + + /// Whether the reservoir holds a canvas sample taken while `foreground` was the foreground. + pub fn holds_sample(&self, foreground: [f32; 4]) -> bool { + self.reservoir.is_some() && self.sampled_over == Some(foreground) + } + /// Before a stroke in `color`: choosing a color other than [`MixerState::color`] loads the + /// reservoir with it. + pub fn begin_stroke(&mut self, color: [f32; 4]) { + if self.color != Some(color) { + self.load(color); + self.color = Some(color); + } + } + /// After a stroke in `color`: the automatic Clean empties the brush, then the automatic Load + /// fills the reservoir again. + pub fn end_stroke(&mut self, color: [f32; 4], clean: bool, load: bool) { + // Automatic Load replenishes a sampled tip as well as a solid-color load. + // Move its bounded buffer through Clean instead of allocating a copy. + let sampled = self.holds_sample(color).then_some(color); + let spatial = if load { self.spatial.take() } else { None }; + let paint = if spatial.is_some() || sampled.is_some() { self.reservoir.unwrap_or(color) } else { color }; + if clean { + self.clean(); + } + if load { + self.load(paint); + self.spatial = spatial; + self.sampled_over = sampled; + } + self.color = Some(color); + } + /// The reservoir color the next stroke in `color` starts with (the Current Brush Load + /// swatch); None = a clean brush. + pub fn next_load(&self, color: [f32; 4]) -> Option<[f32; 4]> { + if self.color != Some(color) { Some(color) } else { self.reservoir } + } +} + +/// Sample a spatial load at a dab pixel using the dab's rotation, aspect and flips. +/// Keeping the reservoir in tip coordinates lets it follow subsequent brush poses. +pub fn load_at(load: &[[f32; 4]], dab: &crate::Dab, x: f32, y: f32) -> [f32; 4] { + let mut dx = x - dab.center.x as f32; + let mut dy = y - dab.center.y as f32; + if dab.proj_scale < 0.999 { + let (sin, cos) = dab.proj_angle.sin_cos(); + let along = (dx * cos - dy * sin) * (1.0 / dab.proj_scale.max(0.05) - 1.0); + dx += along * cos; + dy -= along * sin; + } + let (sin, cos) = dab.angle.sin_cos(); + let mut u = (dx * cos - dy * sin) / dab.radius.max(0.01); + let mut v = (dx * sin + dy * cos) / (dab.radius * dab.roundness).max(0.5); + if dab.flip_x { + u = -u; + } + if dab.flip_y { + v = -v; + } + if !(-1.0..1.0).contains(&u) || !(-1.0..1.0).contains(&v) { + return [0.0; 4]; + } + let col = ((u + 1.0) * 32.0) as usize; + let row = ((v + 1.0) * 32.0) as usize; + load.get(row * 64 + col).copied().unwrap_or([0.0; 4]) } /// Paint a Mixer Brush stroke. `sample` is the surface colour is picked up from (the layer itself @@ -70,6 +173,50 @@ pub fn apply_mixer_stroke( lock_transparency: bool, ) -> Rect { let fmt = target.format(); + let mut st = stroke.clone(); + st.brush.opacity = 1.0; + apply_dab_stroke(target, &st, selection, lock_transparency, 0, |work, fp| { + apply_mixer_dab(work, fp, fmt, sample, m, state, lock_transparency); + }) +} + +/// One incremental wet-paint dab, shared by recorded strokes and the live document transaction. +pub fn apply_mixer_dab( + work: &mut crate::retouch::Region, + fp: &crate::retouch::Footprint, + fmt: photocraft_color::PixelFormat, + sample: Option<&Surface>, + m: &MixerSettings, + state: &mut MixerState, + lock_transparency: bool, +) { + let sample = sample.filter(|_| m.samples_canvas(&fp.dab)).map(|surface| { + let source = crate::retouch::Region::read(surface, work.rect); + if surface.format().mode == fmt.mode && surface.format().alpha == fmt.alpha { + source + } else { + let mut native = crate::retouch::Region::new(work.rect, fmt.channels()); + for (src, dst) in source.data.chunks_exact(source.ch).zip(native.data.chunks_exact_mut(native.ch)) { + from_rgba_into(&fmt, to_rgba(&surface.format(), src), dst); + } + native + } + }); + apply_mixer_dab_region(work, fp, fmt, sample.as_ref(), m, state, lock_transparency); +} + +/// Contiguous native-channel sample variant. `sample` must have the same rectangle and +/// channel layout as `work`. The live compositor supplies it directly, avoiding a tiled +/// temporary surface and a heap allocation for every sampled/deposited pixel. +pub fn apply_mixer_dab_region( + work: &mut crate::retouch::Region, + fp: &crate::retouch::Footprint, + fmt: photocraft_color::PixelFormat, + sample: Option<&crate::retouch::Region>, + m: &MixerSettings, + state: &mut MixerState, + lock_transparency: bool, +) { let n = fmt.channels(); let a = alpha_index(&fmt); let nc = fmt.mode.color_channels(); @@ -83,31 +230,22 @@ pub fn apply_mixer_stroke( let res = state.reservoir.map(native); let mut amount = if res.is_some() { state.amount.clamp(0.0, 1.0) } else { 0.0 }; let mut pickup: Option> = state.pickup.map(native); - let (wet, mix, flow, load) = (m.wet.clamp(0.0, 1.0), m.mix.clamp(0.0, 1.0), m.flow.clamp(0.0, 1.0), m.load.clamp(0.0, 1.0)); - let mut st = stroke.clone(); - st.brush.opacity = 1.0; - let mut acc = vec![0.0f32; n]; + let (mix, flow, load) = (m.mix.clamp(0.0, 1.0), m.flow.clamp(0.0, 1.0), m.load.clamp(0.0, 1.0)); let mut paint = vec![0.0f32; n]; - let dmg = apply_dab_stroke(target, &st, selection, lock_transparency, 0, |work, fp| { - // Transfer › Wetness / Mix Jitter scale the settings per dab. - let (wet, mix) = (wet * fp.dab.wet.clamp(0.0, 1.0), mix * fp.dab.mix.clamp(0.0, 1.0)); - // Sample the canvas under the dab. - acc.iter_mut().for_each(|v| *v = 0.0); + // Transfer › Wetness / Mix Jitter scale the settings per dab. + let (wet, mix) = (m.effective_wet(&fp.dab), mix * fp.dab.mix.clamp(0.0, 1.0)); + // Sample the canvas under the dab. + let r = fp.rect.intersect(&work.rect); + if wet > 0.0 { + let mut acc = vec![0.0f32; n]; let (mut wsum, mut asum) = (0.0f32, 0.0f32); - let r = fp.rect.intersect(&work.rect); for y in r.y0..r.y1 { for x in r.x0..r.x1 { let w = fp.at(x, y); if w <= 0.0 { continue; } - let px: Vec = match sample { - Some(s) => { - let v = s.pixel(x, y); - if s.format() == fmt { v } else { from_rgba(&fmt, s.rgba(x, y)) } - } - None => work.px(x, y).to_vec(), - }; + let px = sample.unwrap_or(work).px(x, y); let al = a.map_or(1.0, |ai| px[ai]); for c in 0..nc { acc[c] += px[c] * w * al; @@ -116,7 +254,7 @@ pub fn apply_mixer_stroke( asum += w * al; } } - if asum > 1e-6 && wet > 0.0 { + if asum > 1e-6 { let ca = asum / wsum.max(1e-6); let canvas: Vec = (0..n).map(|c| if c < nc { acc[c] / asum } else { 1.0 }).collect(); pickup = Some(match pickup.take() { @@ -124,30 +262,51 @@ pub fn apply_mixer_stroke( Some(p) => p.iter().zip(&canvas).map(|(p, c)| p + (c - p) * PICKUP_RATE * wet * ca).collect(), }); } - let wr = if res.is_some() { amount * (1.0 - mix * wet) } else { 0.0 }; - let wp = if pickup.is_some() { mix * wet } else { 0.0 }; - let total = wr + wp; - if total > 1e-6 { - for c in 0..n { - let rv = res.as_ref().map_or(0.0, |v| v[c]); - let pv = pickup.as_ref().map_or(0.0, |v| v[c]); - paint[c] = (rv * wr + pv * wp) / total; - } - let dep = total.min(1.0) * flow; - for y in r.y0..r.y1 { - for x in r.x0..r.x1 { - let k = fp.at(x, y) * dep; - if k > 0.0 { + } + let wr = if res.is_some() { amount * (1.0 - mix * wet) } else { 0.0 }; + let wp = if pickup.is_some() { mix * wet } else { 0.0 }; + let total = wr + wp; + if total > 1e-6 { + for c in 0..n { + let rv = res.as_ref().map_or(0.0, |v| v[c]); + let pv = pickup.as_ref().map_or(0.0, |v| v[c]); + paint[c] = (rv * wr + pv * wp) / total; + } + let dep = total.min(1.0) * flow; + let mut local = vec![0.0; n]; + let mut mixed = vec![0.0; n]; + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let k = fp.at(x, y) * dep; + if k > 0.0 { + if let Some(load) = &state.spatial { + let loaded = load_at(load, &fp.dab, x as f32 + 0.5, y as f32 + 0.5); + from_rgba_into(&fmt, loaded, &mut local); + let local_wr = wr * loaded[3]; + let local_total = local_wr + wp; + if local_total > 1e-6 { + for (c, value) in mixed.iter_mut().enumerate() { + *value = (local.get(c).copied().unwrap_or(0.0) * local_wr + + pickup.as_ref().and_then(|p| p.get(c)).copied().unwrap_or(0.0) * wp) + / local_total; + } + if let Some(ai) = a + && let Some(alpha) = mixed.get_mut(ai) + { + *alpha = 1.0; + } + over_native(&fmt, work.px_mut(x, y), &mixed, fp.at(x, y) * local_total.min(1.0) * flow, lock_transparency); + } + } else { over_native(&fmt, work.px_mut(x, y), &paint, k, lock_transparency); } } } } - amount *= 1.0 - 0.08 * (1.0 - load); - }); + } + amount *= 1.0 - 0.08 * (1.0 - load); state.amount = amount; state.pickup = pickup.map(|p| to_rgba(&fmt, &p)); - dmg } #[cfg(test)] @@ -209,6 +368,80 @@ mod tests { assert!(st.pickup.is_none()); } + #[test] + fn a_clean_brush_paints_only_the_canvas_color_it_picks_up() { + let mut st = MixerState::default(); + st.load([0.0, 0.0, 1.0, 1.0]); + st.pickup = Some([0.0, 1.0, 0.0, 1.0]); + st.clean(); + assert_eq!((st.reservoir, st.amount, st.pickup), (None, 0.0, None)); + // On red, the clean wet brush smears red and lays down none of the blue it was loaded with. + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 200, 20), &[1.0, 0.0, 0.0, 1.0]); + apply_mixer_stroke(&mut s, None, &stroke(10.0), &MixerSettings::default(), &mut st, None, false); + let c = s.rgba(100, 10); + assert!(c[0] > 0.99 && c[1] < 0.01 && c[2] < 0.01, "{c:?}"); + } + + #[test] + fn a_new_color_loads_the_brush_and_the_automatic_options_run_after_the_stroke() { + let (blue, green) = ([0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 1.0]); + let mut st = MixerState::default(); + // A brush never loaded takes the stroke's color. + assert_eq!(st.next_load(blue), Some(blue)); + st.begin_stroke(blue); + assert_eq!((st.reservoir, st.amount), (Some(blue), 1.0)); + // Clean on, Load off: the next stroke in the same color starts empty. + st.end_stroke(blue, true, false); + assert_eq!(st.next_load(blue), None); + st.begin_stroke(blue); + assert_eq!((st.reservoir, st.pickup), (None, None)); + // Choosing another color loads it. + assert_eq!(st.next_load(green), Some(green)); + st.begin_stroke(green); + assert_eq!((st.reservoir, st.amount), (Some(green), 1.0)); + // Load on, Clean off: the reservoir is full again and the pickup stays. + st.amount = 0.1; + st.pickup = Some(blue); + st.end_stroke(green, false, true); + assert_eq!((st.reservoir, st.amount, st.pickup), (Some(green), 1.0, Some(blue))); + // Both on: a fresh load with no pickup. + st.end_stroke(green, true, true); + assert_eq!((st.reservoir, st.amount, st.pickup), (Some(green), 1.0, None)); + } + + /// Spatial paint reservoirs honor independent automatic clean and reload choices. + #[test] + fn spatial_load_obeys_automatic_clean_and_load() { + let foreground = [0.0, 0.0, 1.0, 1.0]; + let sampled = [1.0, 0.0, 0.0, 1.0]; + let tip = vec![sampled; 4096]; + let mut state = MixerState::default(); + state.load(sampled); + state.spatial = Some(tip.clone()); + state.color = Some(foreground); + state.amount = 0.1; + state.end_stroke(foreground, true, true); + assert_eq!(state.spatial.as_ref(), Some(&tip)); + assert_eq!((state.reservoir, state.amount, state.pickup), (Some(sampled), 1.0, None)); + state.begin_stroke(foreground); + assert_eq!(state.spatial.as_ref(), Some(&tip)); + state.end_stroke(foreground, true, false); + assert!(state.spatial.is_none() && state.reservoir.is_none()); + state.begin_stroke(foreground); + assert!(state.reservoir.is_none(), "Clean stays empty until a new color or Load"); + } + + /// Older serialized reload settings remain compatible with the automatic load field. + #[test] + fn earlier_reload_setting_deserializes_as_automatic_load() { + let settings: MixerSettings = serde_json::from_value(serde_json::json!({"reloadAfterStroke":false})).unwrap(); + assert!(!settings.load_after_stroke); + let saved = serde_json::to_value(settings).unwrap(); + assert_eq!(saved["loadAfterStroke"], false); + assert!(saved.get("reloadAfterStroke").is_none()); + } + #[test] fn dry_empty_brush_paints_nothing() { let mut s = Surface::new(PixelFormat::RGBA8); @@ -238,3 +471,6 @@ mod tests { assert!(run(1.0).is_some_and(|p| p[2] > 0.2)); } } + +#[cfg(test)] +mod reference_tests; diff --git a/crates/paint/src/mixer/reference_tests.rs b/crates/paint/src/mixer/reference_tests.rs new file mode 100644 index 0000000000..0f6f0a2a82 --- /dev/null +++ b/crates/paint/src/mixer/reference_tests.rs @@ -0,0 +1,207 @@ +// Frozen pre-optimization dab kernel. Keep independent of the optimized implementation. +use super::*; +/// Keep the allocation-heavy scalar kernel independent so optimizations cannot silently change pickup or blending. +fn reference( + work: &mut crate::retouch::Region, + fp: &crate::retouch::Footprint, + fmt: photocraft_color::PixelFormat, + sample: Option<&Surface>, + m: &MixerSettings, + state: &mut MixerState, + lock_transparency: bool, +) { + let n = fmt.channels(); + let a = alpha_index(&fmt); + let nc = fmt.mode.color_channels(); + let native = |c: [f32; 4]| { + let mut v = from_rgba(&fmt, c); + if let Some(ai) = a { + v[ai] = 1.0; + } + v + }; + let res = state.reservoir.map(native); + let mut amount = if res.is_some() { state.amount.clamp(0.0, 1.0) } else { 0.0 }; + let mut pickup: Option> = state.pickup.map(native); + let (wet, mix, flow, load) = (m.wet.clamp(0.0, 1.0), m.mix.clamp(0.0, 1.0), m.flow.clamp(0.0, 1.0), m.load.clamp(0.0, 1.0)); + let mut acc = vec![0.0f32; n]; + let mut paint = vec![0.0f32; n]; + // Transfer › Wetness / Mix Jitter scale the settings per dab. + let (wet, mix) = (wet * fp.dab.wet.clamp(0.0, 1.0), mix * fp.dab.mix.clamp(0.0, 1.0)); + // Sample the canvas under the dab. + acc.iter_mut().for_each(|v| *v = 0.0); + let (mut wsum, mut asum) = (0.0f32, 0.0f32); + let r = fp.rect.intersect(&work.rect); + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let w = fp.at(x, y); + if w <= 0.0 { + continue; + } + let px: Vec = match sample { + Some(s) => { + let v = s.pixel(x, y); + if s.format() == fmt { v } else { from_rgba(&fmt, s.rgba(x, y)) } + } + None => work.px(x, y).to_vec(), + }; + let al = a.map_or(1.0, |ai| px[ai]); + for c in 0..nc { + acc[c] += px[c] * w * al; + } + wsum += w; + asum += w * al; + } + } + if asum > 1e-6 && wet > 0.0 { + let ca = asum / wsum.max(1e-6); + let canvas: Vec = (0..n).map(|c| if c < nc { acc[c] / asum } else { 1.0 }).collect(); + pickup = Some(match pickup.take() { + None => canvas, + Some(p) => p.iter().zip(&canvas).map(|(p, c)| p + (c - p) * PICKUP_RATE * wet * ca).collect(), + }); + } + let wr = if res.is_some() { amount * (1.0 - mix * wet) } else { 0.0 }; + let wp = if pickup.is_some() { mix * wet } else { 0.0 }; + let total = wr + wp; + if total > 1e-6 { + for c in 0..n { + let rv = res.as_ref().map_or(0.0, |v| v[c]); + let pv = pickup.as_ref().map_or(0.0, |v| v[c]); + paint[c] = (rv * wr + pv * wp) / total; + } + let dep = total.min(1.0) * flow; + for y in r.y0..r.y1 { + for x in r.x0..r.x1 { + let k = fp.at(x, y) * dep; + if k > 0.0 { + if let Some(load) = &state.spatial { + let loaded = load_at(load, &fp.dab, x as f32 + 0.5, y as f32 + 0.5); + let local = from_rgba(&fmt, loaded); + let local_wr = wr * loaded[3]; + let local_total = local_wr + wp; + if local_total > 1e-6 { + let mut mixed = paint.clone(); + for (c, value) in mixed.iter_mut().enumerate() { + *value = (local.get(c).copied().unwrap_or(0.0) * local_wr + + pickup.as_ref().and_then(|p| p.get(c)).copied().unwrap_or(0.0) * wp) + / local_total; + } + if let Some(ai) = a + && let Some(alpha) = mixed.get_mut(ai) + { + *alpha = 1.0; + } + over_native(&fmt, work.px_mut(x, y), &mixed, fp.at(x, y) * local_total.min(1.0) * flow, lock_transparency); + } + } else { + over_native(&fmt, work.px_mut(x, y), &paint, k, lock_transparency); + } + } + } + } + } + amount *= 1.0 - 0.08 * (1.0 - load); + state.amount = amount; + state.pickup = pickup.map(|p| to_rgba(&fmt, &p)); +} + +/// The incremental kernel matches the frozen reference across color models, bit depths and spatial loads. +#[test] +fn contiguous_mixer_matches_frozen_kernel_across_models_depths_and_spatial_loads() { + use crate::retouch::Region; + use crate::{BrushContext, BrushSettings, Stroke, StrokePoint}; + use photocraft_color::{ColorMode, PixelFormat, SampleType}; + let rect = Rect::new(-16, -16, 32, 32); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for alpha in [false, true] { + let fmt = PixelFormat::new(mode, depth, alpha); + let mut surface = Surface::new(fmt); + let values: Vec<_> = (0..rect.width() * rect.height()).flat_map(|i| from_rgba(&fmt, [(i % 31) as f32 / 30.0, 0.3, 0.7, 0.6])).collect(); + surface.write_region(rect, &values); + let mut source = Surface::new(PixelFormat::RGBA32F); + source.fill_rect(rect, &[0.8, 0.2, 0.1, 0.75]); + let brush = BrushSettings { size: 24.0, angle: 33.0, roundness: 0.6, pressure_size: false, ..Default::default() }; + let dabs = crate::dabs(&Stroke { brush: brush.clone(), points: vec![StrokePoint::new(4.0, 4.0, 1.0)] }); + let ctx = BrushContext::new(&brush); + for spatial in [false, true] { + for sample in [None, Some(&source), Some(&surface)] { + for lock in [false, true] { + let mut state = MixerState::default(); + state.load([0.1, 0.8, 0.2, 1.0]); + if spatial { + state.spatial = + Some((0..4096).map(|i| [if i % 64 < 32 { 1.0 } else { 0.0 }, 0.2, 0.8, if i % 7 == 0 { 0.0 } else { 1.0 }]).collect()); + } + let mut expected_state = state.clone(); + let mut actual = Region::read(&surface, rect); + let mut expected = actual.clone(); + let settings = MixerSettings::default(); + for (index, dab) in dabs.iter().enumerate() { + let fp = ctx.footprint(dab, index); + reference(&mut expected, &fp, fmt, sample, &settings, &mut expected_state, lock); + apply_mixer_dab(&mut actual, &fp, fmt, sample, &settings, &mut state, lock); + } + assert_eq!(actual, expected, "{mode:?} {depth:?} alpha={alpha} spatial={spatial} lock={lock}"); + assert_eq!(state, expected_state); + } + } + } + } + } + } +} + +/// Dry dabs must preserve both the painted pixels and the carried pickup across transitions. +#[test] +fn dry_and_wet_transitions_match_frozen_kernel() { + use crate::retouch::Region; + use crate::{BrushContext, BrushSettings, Dab}; + use photocraft_color::{ColorMode, PixelFormat, SampleType}; + use photocraft_geom::Point; + + let rect = Rect::new(0, 0, 48, 48); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for alpha in [false, true] { + let fmt = PixelFormat::new(mode, depth, alpha); + let mut surface = Surface::new(fmt); + surface.fill_rect(rect, &from_rgba(&fmt, [0.25, 0.5, 0.75, 0.6])); + let mut source = Surface::new(PixelFormat::RGBA32F); + source.fill_rect(rect, &[0.7, 0.1, 0.3, 0.8]); + let ctx = BrushContext::new(&BrushSettings { size: 16.0, ..Default::default() }); + for spatial in [false, true] { + for loaded in [false, true] { + for sample in [None, Some(&source)] { + for lock in [false, true] { + let mut actual = Region::read(&surface, rect); + let mut expected = actual.clone(); + let mut state = MixerState::default(); + if loaded { + state.load([0.8, 0.2, 0.1, 1.0]); + } + if spatial { + state.spatial = Some(vec![[0.2, 0.8, 0.4, 0.7]; 4096]); + } + let mut expected_state = state.clone(); + for (i, (base_wet, dab_wet, mix)) in + [(0.7, 1.0, 0.0), (0.0, 1.0, 0.7), (0.7, 0.0, 0.7), (0.7, 1.0, 0.7)].into_iter().enumerate() + { + let mut dab = Dab::round(Point::new(20.0 + i as f64, 20.0), 8.0, 1.0); + dab.wet = dab_wet; + let fp = ctx.footprint(&dab, i); + let settings = MixerSettings { wet: base_wet, mix, ..Default::default() }; + reference(&mut expected, &fp, fmt, sample, &settings, &mut expected_state, lock); + apply_mixer_dab(&mut actual, &fp, fmt, sample, &settings, &mut state, lock); + assert_eq!(actual, expected, "{mode:?} {depth:?} alpha={alpha} spatial={spatial} loaded={loaded} lock={lock} dab={i}"); + assert_eq!(state, expected_state, "mixer state after dab {i}"); + } + } + } + } + } + } + } + } +} diff --git a/crates/paint/src/presets.rs b/crates/paint/src/presets.rs index f711d0eb51..e40902a78f 100644 --- a/crates/paint/src/presets.rs +++ b/crates/paint/src/presets.rs @@ -4,7 +4,10 @@ //! Special Effects. Every preset is plain [`BrushSettings`] data, so it serialises, journals and //! can be edited like any user preset. -use crate::brush::*; +use crate::brush::{ + BrushPreset, BrushSettings, ColorDynamics, Control, DualBrush, Dynamic, MaskMode, Pattern, PatternStyle, Scattering, ShapeDynamics, Smoothing, Texture, + TipShape, Transfer, +}; use crate::procedural::{bristle_tip, chalk_tip, charcoal_tip, grass_tip, leaf_tip, rake_tip, spatter_tip, sponge_tip, star_tip}; /// Preset groups, in panel order. @@ -36,19 +39,18 @@ fn paper(seed: u32, depth: f32) -> Texture { fn general(base: &BrushSettings) -> Vec { let g = |n: &str, b: BrushSettings| preset("General", n, b); + // Dense dabs keep large hard-round outlines and pressure-opacity tapers continuous (#8842). + let hard_round = BrushSettings { spacing: 0.05, ..base.clone() }; vec![ - g("Hard Round", BrushSettings { size: 30.0, hardness: 1.0, ..base.clone() }), + g("Hard Round", BrushSettings { size: 30.0, hardness: 1.0, ..hard_round.clone() }), g("Soft Round", BrushSettings { size: 45.0, hardness: 0.0, ..base.clone() }), - g("Hard Round Fine", BrushSettings { size: 5.0, hardness: 1.0, spacing: 0.1, ..base.clone() }), + g("Hard Round Fine", BrushSettings { size: 5.0, hardness: 1.0, ..hard_round.clone() }), g("Soft Round Large", BrushSettings { size: 300.0, hardness: 0.0, spacing: 0.15, ..base.clone() }), - g("Hard Round Pressure Size", BrushSettings { size: 30.0, hardness: 1.0, shape_dynamics: size_dyn(pressure()), ..base.clone() }), + g("Hard Round Pressure Size", BrushSettings { size: 30.0, hardness: 1.0, shape_dynamics: size_dyn(pressure()), ..hard_round.clone() }), g("Soft Round Pressure Size", BrushSettings { size: 45.0, hardness: 0.0, shape_dynamics: size_dyn(pressure()), ..base.clone() }), - g("Hard Round Pressure Opacity", BrushSettings { size: 30.0, hardness: 1.0, transfer: transfer(pressure(), Dynamic::default()), ..base.clone() }), + g("Hard Round Pressure Opacity", BrushSettings { size: 30.0, hardness: 1.0, transfer: transfer(pressure(), Dynamic::default()), ..hard_round.clone() }), g("Soft Round Pressure Opacity", BrushSettings { size: 45.0, hardness: 0.0, transfer: transfer(pressure(), Dynamic::default()), ..base.clone() }), - g( - "Hard Round Pressure Opacity and Flow", - BrushSettings { size: 30.0, hardness: 0.9, transfer: transfer(pressure(), pressure_min(0.2)), ..base.clone() }, - ), + g("Hard Round Pressure Opacity and Flow", BrushSettings { size: 30.0, hardness: 0.9, transfer: transfer(pressure(), pressure_min(0.2)), ..hard_round }), g("Airbrush Soft", BrushSettings { size: 80.0, hardness: 0.0, flow: 0.1, spacing: 0.1, build_up: true, build_up_rate: 25.0, ..base.clone() }), g( "Airbrush Pressure Flow", diff --git a/crates/paint/src/procedural.rs b/crates/paint/src/procedural.rs index 5a14521fba..5469dacebc 100644 --- a/crates/paint/src/procedural.rs +++ b/crates/paint/src/procedural.rs @@ -87,8 +87,20 @@ pub fn chalk_tip(n: u32, seed: u32) -> GrayTile { }) } +/// Most primitives one generated tip is drawn from, and most (pixel × primitive) visits it costs: +/// every pixel is tested against every primitive, so both counts are the caller's to multiply. +const MAX_PRIMITIVES: u32 = 4096; +const MAX_TIP_WORK: u64 = 1 << 27; + +/// `count` limited so that an `n`×`n` tip tests at most [`MAX_TIP_WORK`] pixel/primitive pairs. +fn clamp_primitives(n: u32, count: u32) -> u32 { + let per_primitive = (u64::from(n) * u64::from(n)).max(1); + count.min(MAX_PRIMITIVES).min(u32::try_from(MAX_TIP_WORK / per_primitive).unwrap_or(u32::MAX)) +} + /// A spatter tip: a cluster of round droplets of different sizes. pub fn spatter_tip(n: u32, seed: u32, drops: u32) -> GrayTile { + let drops = clamp_primitives(n, drops); let salt = u64::from(seed) + 202; let s = n as f32; let blobs: Vec<(f32, f32, f32)> = (0..drops) @@ -162,6 +174,7 @@ pub fn leaf_tip(n: u32) -> GrayTile { /// Grass: a tuft of tapered, slightly curved blades rising from the bottom edge. pub fn grass_tip(n: u32, seed: u32, blades: u32) -> GrayTile { + let blades = clamp_primitives(n, blades); let salt = u64::from(seed) + 505; let s = n as f32; let bl: Vec<(f32, f32, f32, f32)> = (0..blades) @@ -217,7 +230,7 @@ pub fn star_tip(n: u32) -> GrayTile { pub fn rake_tip(n: u32, seed: u32, bristles: u32) -> GrayTile { let salt = u64::from(seed) + 707; let s = n as f32; - let k = bristles.max(1); + let k = clamp_primitives(n, bristles).max(1); let dots: Vec<(f32, f32, f32)> = (0..k) .map(|i| { let x = s * (i as f32 + 0.5) / k as f32; @@ -239,6 +252,19 @@ pub fn rake_tip(n: u32, seed: u32, bristles: u32) -> GrayTile { mod tests { use super::*; + #[test] + fn tips_with_absurd_primitive_counts_stay_cheap() { + // 4 billion drops over a 1024² tip would be 4·10¹⁵ pixel tests. + let t = std::time::Instant::now(); + assert_eq!(spatter_tip(1024, 1, u32::MAX).width, 1024); + assert_eq!(grass_tip(1024, 1, u32::MAX).height, 1024); + assert_eq!(rake_tip(1024, 1, u32::MAX).width, 1024); + assert!(t.elapsed().as_secs() < 60, "{:?}", t.elapsed()); + assert!(clamp_primitives(1024, u32::MAX) <= MAX_PRIMITIVES); + assert!(clamp_primitives(60_000, 100) < 100, "a huge tip gets fewer primitives"); + assert_eq!(clamp_primitives(0, 7), 7); + } + #[test] fn patterns_are_seamless_and_in_range() { for st in [PatternStyle::Noise, PatternStyle::Paper, PatternStyle::Canvas, PatternStyle::Dots] { diff --git a/crates/paint/src/render.rs b/crates/paint/src/render.rs index 5f59aafad1..83243bdafa 100644 --- a/crates/paint/src/render.rs +++ b/crates/paint/src/render.rs @@ -7,19 +7,21 @@ //! buffer is sparse and compositing always starts from the pre-stroke pixels, strokes can be fed in //! chunks and re-composited incrementally for interactive painting (see [`StrokeRenderer`]). -use std::collections::{HashMap, HashSet}; +use std::collections::HashSet; use std::sync::Arc; +use rustc_hash::FxHashMap; + use photocraft_color::{BlendMode, ColorMode, PixelFormat, SampleType}; use photocraft_geom::Rect; use photocraft_raster::{Surface, from_rgba_into, to_rgba}; use crate::brush::{BrushSettings, MaskMode, Pattern, TipShape}; -use crate::dynamics::DabGenerator; +use crate::dynamics::{DabGenerator, grid_cell}; use crate::retouch::{Footprint, alpha_index, over_native}; use crate::rng::hash2; use crate::tile::{Mips, PatternImage}; -use crate::{Dab, StrokePoint, dab_coverage, tip_falloff, tip_reach}; +use crate::{Dab, StrokePoint, TipProfile}; /// Salt of the 8-bit dither's per-pixel noise. const DITHER_SALT: u64 = 0x0000_6469_7468_6572; @@ -39,6 +41,57 @@ fn dither8(px: &mut [f32], x: i32, y: i32) { /// Edge of a stroke-buffer tile. pub const COV_TILE: i32 = 64; + +/// Work on at least this many pixels runs in parallel (rows of a dab, tiles of a stroke): below +/// it the threads cost more than they save. A large soft tip is tens of megapixels per dab, and +/// every stage of it is independent per row or tile, so the pixels come out the same. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) const DEFAULT_PARALLEL_MIN_PIXELS: usize = 256 * 256; + +/// The threshold in use: [`DEFAULT_PARALLEL_MIN_PIXELS`] (tests move it). +#[cfg(not(target_arch = "wasm32"))] +static PARALLEL_MIN_PIXELS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(DEFAULT_PARALLEL_MIN_PIXELS); + +/// Whether `pixels` of independent work are worth the thread pool (the web has none: there every +/// stage takes its serial path). +#[cfg(not(target_arch = "wasm32"))] +fn parallel(pixels: usize) -> bool { + pixels >= PARALLEL_MIN_PIXELS.load(std::sync::atomic::Ordering::Relaxed) +} + +/// Tests: the pixel count from which work runs in parallel (`usize::MAX` keeps everything serial). +/// The static is process-wide, so brush tests on other threads see the change too; that is +/// harmless, because every stage gives the same pixels serially and in parallel (the point of +/// the test that uses this), so no test can tell which way it ran. +#[cfg(all(test, not(target_arch = "wasm32")))] +pub(crate) fn set_parallel_min_pixels(n: usize) { + PARALLEL_MIN_PIXELS.store(n, std::sync::atomic::Ordering::Relaxed); +} + +/// `f` over `items`, in parallel when `pixels` are worth it. +fn for_each_item(items: &mut [T], pixels: usize, f: impl Fn(&mut T) + Sync) { + #[cfg(not(target_arch = "wasm32"))] + if parallel(pixels) { + use rayon::prelude::*; + items.par_iter_mut().for_each(&f); + return; + } + #[cfg(target_arch = "wasm32")] + let _ = pixels; + items.iter_mut().for_each(&f); +} + +/// `f` over `items`, collecting its results in order, in parallel when `pixels` are worth it. +fn map_items(items: &[T], pixels: usize, f: impl Fn(&T) -> R + Sync) -> Vec { + #[cfg(not(target_arch = "wasm32"))] + if parallel(pixels) { + use rayon::prelude::*; + return items.par_iter().map(&f).collect(); + } + #[cfg(target_arch = "wasm32")] + let _ = pixels; + items.iter().map(&f).collect() +} /// Computed tips with a smaller radius (in pixels) are rasterised by area, not at pixel centres. const SMALL_TIP_RADIUS: f32 = 3.0; /// Sub-pixel sample offsets (a 4×4 grid) for the area coverage of small tips. @@ -52,7 +105,7 @@ const NOISE_SALT: u64 = 0x006E_6F69_7365; /// the Pencil cursor shows ([`grid_square`]). pub fn grid_center(x: f64, y: f64, diameter: f32) -> (f32, f32) { let n = if diameter.is_finite() { diameter.round().max(1.0) as i64 } else { 1 }; - let snap = |v: f64| if n % 2 == 1 { v.floor() + 0.5 } else { v.round() }; + let snap = |v: f64| if n % 2 == 1 { grid_cell(v, 0.5) } else { v.round() }; (snap(x) as f32, snap(y) as f32) } @@ -260,7 +313,8 @@ impl BrushContext { let r = d.radius; let ro = d.roundness.max(0.5 / r).min(1.0); let rm = (r * ro).max(0.5); - let reach = if aliased || mips.is_some() { rm } else { crate::tip_reach(rm, hardness).max(rm) }; + let profile = TipProfile::new(rm, hardness); + let reach = if aliased || mips.is_some() { rm } else { profile.reach().max(rm) }; let reach2 = (reach + 0.5) * (reach + 0.5); // Sampled-tip mapping. let (level, inv_scale, tw, th) = match mips { @@ -291,7 +345,9 @@ impl BrushContext { if !(mips.is_some() || small || (aliased && b.square) || noise || tex_tip || wet) && depth.is_none() && proj.is_none() - && d.angle == 0.0 + // Rotation preserves a circle's distance; ellipses still need the transform. + && (d.angle == 0.0 || ro == 1.0) + && d.angle.is_finite() && d.radius.is_finite() && d.roundness.is_finite() && rm.is_finite() @@ -301,7 +357,7 @@ impl BrushContext { && d.alpha.is_finite() && w > 0 { - rasterize_round(d, rect, out, hardness, aliased); + rasterize_round(d, rect, out, profile, aliased); return; } for yy in 0..h { @@ -331,7 +387,7 @@ impl BrushContext { let sd = ((su * ro).powi(2) + sv * sv).sqrt(); // Each sample takes the tip's own edge, so the covered area // changes smoothly with the dab's position. - sum += tip_falloff(sd, rm, hardness); + sum += profile.falloff(sd); } } (sum / (SUBPIXEL.len() * SUBPIXEL.len()) as f32, d2.sqrt() / rm) @@ -342,7 +398,7 @@ impl BrushContext { continue; } let dd = d2.sqrt(); - let val = if aliased { if dd <= rm { 1.0 } else { 0.0 } } else { dab_coverage(dd, rm, hardness) }; + let val = if aliased { if dd <= rm { 1.0 } else { 0.0 } } else { profile.coverage(dd) }; (val, dd / rm) } Some(m) => { @@ -424,10 +480,28 @@ struct CovTile { col: Vec, } +impl CovTile { + fn new() -> Self { + Self { cov: vec![0.0; (COV_TILE * COV_TILE) as usize], depth: Vec::new(), col: Vec::new() } + } +} + +/// Preserve both signed coordinates without collisions. FxHash mixes the packed word so +/// horizontal and vertical runs both distribute across the table's buckets. +#[inline] +fn tile_key((tx, ty): (i32, i32)) -> u64 { + (u64::from(tx as u32) << 32) | u64::from(ty as u32) +} + +#[inline] +fn tile_coords(key: u64) -> (i32, i32) { + ((key >> 32) as i32, key as i32) +} + /// A sparse tiled coverage (+ optional colour) buffer. #[derive(Clone, Debug, Default)] pub struct CoverageMap { - tiles: HashMap<(i32, i32), CovTile>, + tiles: FxHashMap, nc: usize, bounds: Rect, dirty: HashSet<(i32, i32)>, @@ -443,14 +517,14 @@ impl CoverageMap { #[inline] pub fn get(&self, x: i32, y: i32) -> f32 { let (tx, ty) = (x.div_euclid(COV_TILE), y.div_euclid(COV_TILE)); - self.tiles.get(&(tx, ty)).map_or(0.0, |t| t.cov[((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize]) + self.tiles.get(&tile_key((tx, ty))).map_or(0.0, |t| t.cov[((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize]) } /// Wet Edges depth at a pixel (see [`CovTile::depth`]); `None` when the stroke has none. #[inline] pub fn depth(&self, x: i32, y: i32) -> Option { let (tx, ty) = (x.div_euclid(COV_TILE), y.div_euclid(COV_TILE)); - self.tiles.get(&(tx, ty)).and_then(|t| t.depth.get(((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize).copied()) + self.tiles.get(&tile_key((tx, ty))).and_then(|t| t.depth.get(((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize).copied()) } /// Accumulate dab values over `rect`: flow builds up towards the dab's opacity ceiling @@ -466,61 +540,57 @@ impl CoverageMap { let (t0x, t0y) = (rect.x0.div_euclid(COV_TILE), rect.y0.div_euclid(COV_TILE)); let (t1x, t1y) = ((rect.x1 - 1).div_euclid(COV_TILE), (rect.y1 - 1).div_euclid(COV_TILE)); let want_col = color.is_some() && nc > 0; - for ty in t0y..=t1y { - for tx in t0x..=t1x { + let dab = DabValues { vals, rect, w, ceil, color: if want_col { color } else { None }, depth, nc }; + // Iterate directly for small dabs: no work-list allocations or removing existing tiles. + let keys = (t0y..=t1y).flat_map(|ty| { + (t0x..=t1x).map(move |tx| { let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&rect); - if tr.is_empty() { - continue; - } - // Skip tiles this dab doesn't touch. - let any = (tr.y0..tr.y1).any(|y| { - let row = (y - rect.y0) as usize * w; - vals[row + (tr.x0 - rect.x0) as usize..row + (tr.x1 - rect.x0) as usize].iter().any(|&v| v > 0.0) - }); - if !any { - continue; - } - let tile = self.tiles.entry((tx, ty)).or_insert_with(|| CovTile { - cov: vec![0.0; (COV_TILE * COV_TILE) as usize], - depth: Vec::new(), - col: if want_col { vec![0.0; (COV_TILE * COV_TILE) as usize * nc] } else { Vec::new() }, - }); - if want_col && tile.col.is_empty() { - tile.col = vec![0.0; (COV_TILE * COV_TILE) as usize * nc]; - } - if depth.is_some() && tile.depth.is_empty() { - tile.depth = vec![0.0; (COV_TILE * COV_TILE) as usize]; - } - self.dirty.insert((tx, ty)); - for y in tr.y0..tr.y1 { - let row = (y - rect.y0) as usize * w; - let trow = ((y - ty * COV_TILE) * COV_TILE) as usize; - for x in tr.x0..tr.x1 { - let v = vals[row + (x - rect.x0) as usize]; - if v <= 0.0 { - continue; - } - let ti = trow + (x - tx * COV_TILE) as usize; - if let (Some(dv), Some(slot)) = (depth.and_then(|dp| dp.get(row + (x - rect.x0) as usize)), tile.depth.get_mut(ti)) { - *slot = slot.max(*dv); - } - let c = tile.cov[ti]; - let nv = if ceil > c { c + v * (ceil - c) } else { c }; - if nv <= c { - continue; - } - if let (true, Some(col)) = (want_col, color) { - let k = if c < 1.0 { (nv - c) / (1.0 - c) } else { 0.0 }; - let p = &mut tile.col[ti * nc..(ti + 1) * nc]; - for i in 0..nc { - p[i] = col[i] * k + p[i] * (1.0 - k); - } - } - tile.cov[ti] = nv; + ((tx, ty), tr) + }) + }); + #[cfg(not(target_arch = "wasm32"))] + { + let n = (COV_TILE * COV_TILE) as usize; + let count = (i64::from(t1x) - i64::from(t0x) + 1) as usize; + let count = count.saturating_mul((i64::from(t1y) - i64::from(t0y) + 1) as usize); + if parallel(count.saturating_mul(n)) { + let keys: Vec<_> = keys.collect(); + let touched = map_items(&keys, count.saturating_mul(n), |(_, tr)| dab.touches(*tr)); + let keys: Vec<_> = keys.into_iter().zip(touched).filter_map(|(key, touched)| touched.then_some(key)).collect(); + if parallel(keys.len().saturating_mul(n)) { + use rayon::prelude::*; + // Only genuinely parallel work takes ownership of tiles out of the map. + let mut tiles: Vec<_> = keys + .into_iter() + .map(|(key, tr)| { + self.dirty.insert(key); + let tile = self.tiles.remove(&tile_key(key)).unwrap_or_else(CovTile::new); + (key, tr, tile) + }) + .collect(); + tiles.par_iter_mut().for_each(|(key, tr, tile)| { + dab.prepare(tile); + dab.tile(*tr, *key, tile); + }); + for (key, _, tile) in tiles { + self.tiles.insert(tile_key(key), tile); } + } else { + self.accumulate_serial(keys.into_iter(), dab); } + return; } } + self.accumulate_serial(keys.filter(|(_, tr)| dab.touches(*tr)), dab); + } + + fn accumulate_serial(&mut self, keys: impl Iterator, dab: DabValues<'_>) { + for (key, tr) in keys { + let tile = self.tiles.entry(tile_key(key)).or_insert_with(CovTile::new); + dab.prepare(tile); + dab.tile(tr, key, tile); + self.dirty.insert(key); + } } fn take_dirty(&mut self) -> Vec<(i32, i32)> { @@ -532,7 +602,7 @@ impl CoverageMap { /// The finished coverage over the tiles `keys`: `tail`'s copy of a tile where finishing the /// stroke changes it (see [`StrokeRenderer::tail_preview`]), else this map's own. fn finished_subset(&self, tail: Option<&Self>, keys: &HashSet<(i32, i32)>) -> Self { - let tile = |k: &(i32, i32)| tail.and_then(|t| t.tiles.get(k)).or_else(|| self.tiles.get(k)).map(|t| (*k, t.clone())); + let tile = |k: &(i32, i32)| tail.and_then(|t| t.tiles.get(&tile_key(*k))).or_else(|| self.tiles.get(&tile_key(*k))).map(|t| (tile_key(*k), t.clone())); Self { tiles: keys.iter().filter_map(tile).collect(), nc: self.nc, bounds: tail.map_or(self.bounds, |t| t.bounds), dirty: HashSet::new() } } @@ -543,7 +613,7 @@ impl CoverageMap { for (&key, source) in &other.tiles { let Some(target) = self.tiles.get_mut(&key) else { self.tiles.insert(key, source.clone()); - self.dirty.insert(key); + self.dirty.insert(tile_coords(key)); continue; }; if target.depth.is_empty() { @@ -563,7 +633,7 @@ impl CoverageMap { } } } - self.dirty.insert(key); + self.dirty.insert(tile_coords(key)); } } } @@ -587,6 +657,72 @@ impl UnionLive { } } +/// One dab's coverage (`vals` over `rect`, `w` wide) as [`CoverageMap::accumulate`] adds it to +/// a tile: the dab's opacity ceiling, its colour when the stroke has per-dab colour (`nc` channels), +/// its Wet Edges depth. +#[derive(Clone, Copy)] +struct DabValues<'a> { + vals: &'a [f32], + rect: Rect, + w: usize, + ceil: f32, + color: Option<&'a [f32]>, + depth: Option<&'a [f32]>, + nc: usize, +} + +impl DabValues<'_> { + fn touches(self, tr: Rect) -> bool { + (tr.y0..tr.y1).any(|y| { + let row = (y - self.rect.y0) as usize * self.w; + self.vals[row + (tr.x0 - self.rect.x0) as usize..row + (tr.x1 - self.rect.x0) as usize].iter().any(|&v| v > 0.0) + }) + } + + fn prepare(self, tile: &mut CovTile) { + let n = (COV_TILE * COV_TILE) as usize; + if self.color.is_some() && tile.col.is_empty() { + tile.col = vec![0.0; n * self.nc]; + } + if self.depth.is_some() && tile.depth.is_empty() { + tile.depth = vec![0.0; n]; + } + } + + /// Adds the dab over `tr` (within tile `(tx, ty)`) to `tile`. + #[inline(always)] + fn tile(self, tr: Rect, (tx, ty): (i32, i32), tile: &mut CovTile) { + let DabValues { vals, rect, w, ceil, color, depth, nc } = self; + for y in tr.y0..tr.y1 { + let row = (y - rect.y0) as usize * w; + let trow = ((y - ty * COV_TILE) * COV_TILE) as usize; + for x in tr.x0..tr.x1 { + let v = vals[row + (x - rect.x0) as usize]; + if v <= 0.0 { + continue; + } + let ti = trow + (x - tx * COV_TILE) as usize; + if let (Some(dv), Some(slot)) = (depth.and_then(|dp| dp.get(row + (x - rect.x0) as usize)), tile.depth.get_mut(ti)) { + *slot = slot.max(*dv); + } + let c = tile.cov[ti]; + let nv = if ceil > c { c + v * (ceil - c) } else { c }; + if nv <= c { + continue; + } + if let Some(col) = color { + let k = if c < 1.0 { (nv - c) / (1.0 - c) } else { 0.0 }; + let p = &mut tile.col[ti * nc..(ti + 1) * nc]; + for i in 0..nc { + p[i] = col[i] * k + p[i] * (1.0 - k); + } + } + tile.cov[ti] = nv; + } + } + } +} + /// Incremental stroke rendering: feed points, composite dirty tiles from the pre-stroke pixels. #[derive(Clone, Debug)] pub struct StrokeRenderer { @@ -603,6 +739,19 @@ pub struct StrokeRenderer { dab_buf: Vec, dual_buf: Vec, all_dabs: Option>, + regions: RegionPool, +} + +/// The tile buffers [`StrokeRenderer::composite`] works in, kept from one composite to the next: +/// a small stroke's few tiles reuse theirs instead of growing and trimming the heap every frame +/// (that cost a 50 px stroke a third of its time). A clone starts with none. +#[derive(Debug, Default)] +struct RegionPool(Vec>); + +impl Clone for RegionPool { + fn clone(&self) -> Self { + Self::default() + } } impl StrokeRenderer { @@ -642,9 +791,9 @@ impl StrokeRenderer { /// Every coverage tile (stroke and Dual Brush), into `keys`. fn keys_into(&self, keys: &mut HashSet<(i32, i32)>) { - keys.extend(self.cov.tiles.keys()); + keys.extend(self.cov.tiles.keys().copied().map(tile_coords)); if let Some(d) = &self.dual { - keys.extend(d.tiles.keys()); + keys.extend(d.tiles.keys().copied().map(tile_coords)); } } @@ -695,6 +844,7 @@ impl StrokeRenderer { dab_buf: Vec::new(), dual_buf: Vec::new(), all_dabs: None, + regions: RegionPool::default(), }) } Some(m) => { @@ -720,7 +870,7 @@ impl StrokeRenderer { target.write_region(old, ®ion); dmg = dmg.union(&old); } - if merged.cov.tiles.contains_key(&(tx, ty)) { + if merged.cov.tiles.contains_key(&tile_key((tx, ty))) { dmg = dmg.union(&tile.intersect(&after)); } } @@ -746,6 +896,7 @@ impl StrokeRenderer { dab_buf: Vec::new(), dual_buf: Vec::new(), all_dabs: None, + regions: RegionPool::default(), } } @@ -858,7 +1009,7 @@ impl StrokeRenderer { } } let subset = |m: &CoverageMap| CoverageMap { - tiles: keys.iter().filter_map(|k| m.tiles.get(k).map(|t| (*k, t.clone()))).collect(), + tiles: keys.iter().filter_map(|k| m.tiles.get(&tile_key(*k)).map(|t| (tile_key(*k), t.clone()))).collect(), nc: m.nc, bounds: m.bounds, dirty: HashSet::new(), @@ -876,10 +1027,11 @@ impl StrokeRenderer { dab_buf: dabs, dual_buf: duals, all_dabs: None, + regions: RegionPool::default(), }; t.raster_pending(); // Tiles whose coverage the tail doesn't change still composite the same: redraw them all. - t.cov.dirty.extend(t.cov.tiles.keys().copied()); + t.cov.dirty.extend(t.cov.tiles.keys().copied().map(tile_coords)); Some(t) } @@ -890,7 +1042,7 @@ impl StrokeRenderer { } for ty in r.y0.div_euclid(COV_TILE)..=(r.y1 - 1).div_euclid(COV_TILE) { for tx in r.x0.div_euclid(COV_TILE)..=(r.x1 - 1).div_euclid(COV_TILE) { - if self.cov.tiles.contains_key(&(tx, ty)) { + if self.cov.tiles.contains_key(&tile_key((tx, ty))) { self.cov.dirty.insert((tx, ty)); } } @@ -927,12 +1079,28 @@ impl StrokeRenderer { self.ctx.stroke_mask_wet(c, self.cov.depth(x, y), self.dual.as_ref().map(|d| d.get(x, y)), x, y) } + /// Whether any coverage tile overlaps `r`: outside them [`coverage_at`](Self::coverage_at) is + /// 0, so a caller scanning a big area can skip it. + pub fn touches(&self, r: Rect) -> bool { + if r.is_empty() { + return false; + } + let (t0x, t0y) = (r.x0.div_euclid(COV_TILE), r.y0.div_euclid(COV_TILE)); + let (t1x, t1y) = ((r.x1 - 1).div_euclid(COV_TILE), (r.y1 - 1).div_euclid(COV_TILE)); + let span = (i64::from(t1x) - i64::from(t0x) + 1).saturating_mul(i64::from(t1y) - i64::from(t0y) + 1); + if span > self.cov.tiles.len() as i64 { + return self.cov.tiles.keys().copied().map(tile_coords).any(|(tx, ty)| (t0x..=t1x).contains(&tx) && (t0y..=t1y).contains(&ty)); + } + (t0y..=t1y).any(|ty| (t0x..=t1x).any(|tx| self.cov.tiles.contains_key(&tile_key((tx, ty))))) + } + /// Dense final coverage over the stroke bounds. pub fn dense_coverage(&self) -> (Rect, Vec) { let b = self.bounds(); let w = b.width() as usize; let mut out = vec![0.0f32; w * b.height() as usize]; - for (&(tx, ty), t) in &self.cov.tiles { + for (&key, t) in &self.cov.tiles { + let (tx, ty) = tile_coords(key); let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&b); for y in tr.y0..tr.y1 { for x in tr.x0..tr.x1 { @@ -955,7 +1123,7 @@ impl StrokeRenderer { pub fn composite(&mut self, pre: &Surface, target: &mut Surface, selection: Option<&Surface>, lock_transparency: bool, all: bool) -> Rect { let keys: Vec<(i32, i32)> = if all { self.cov.dirty.clear(); - let mut k: Vec<_> = self.cov.tiles.keys().copied().collect(); + let mut k: Vec<_> = self.cov.tiles.keys().copied().map(tile_coords).collect(); k.sort_unstable(); k } else { @@ -989,76 +1157,120 @@ impl StrokeRenderer { // colour, as in Photoshop, not grain that a tolerance-0 Magic Wand or Paint Bucket trips on. let dither = fmt.sample == SampleType::U8 && !b.aliased; let mut dmg = Rect::EMPTY; - let mut region = Vec::new(); - for (tx, ty) in keys { - let Some(tile) = self.cov.tiles.get(&(tx, ty)) else { continue }; - let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&bounds); - if tr.is_empty() { - continue; - } - pre.read_region_into(tr, &mut region); - let sel = selection.map(|s| (s.channels(), s.read_region(tr))); - let w = tr.width() as usize; - for y in tr.y0..tr.y1 { - for x in tr.x0..tr.x1 { - let ti = ((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize; - let c = tile.cov[ti]; - if c <= 0.0 { - continue; - } - let i = (y - tr.y0) as usize * w + (x - tr.x0) as usize; - let m = self.ctx.stroke_mask_wet(c, tile.depth.get(ti).copied(), self.dual.as_ref().map(|d| d.get(x, y)), x, y); - let s = sel.as_ref().map_or(1.0, |(sc, v)| v[i * sc]); - let mut k = (m * opacity * s).min(1.0); - if k <= 0.0 { - continue; - } - if b.mode == BlendMode::Dissolve { - k = if hash2(x, y, b.seed) < k { 1.0 } else { 0.0 }; - if k == 0.0 { + // Each tile composites into a buffer of its own (a large stroke's tiles in parallel, to + // the same pixels); the writes to the target go one by one, in order. Batches keep the + // pending buffers few, and the pool keeps them from one composite to the next. + const BATCH: usize = 256; + let mut pool = std::mem::take(&mut self.regions.0); + // The document's CMYK profile is active per thread: the workers enter the caller's, or a + // blend mode other than Normal would convert their tiles through the built-in coated CMYK. + let cmyk = photocraft_color::convert::active_cmyk_space(); + for batch in keys.chunks(BATCH) { + let mut items: Vec<((i32, i32), Rect, Vec)> = batch.iter().map(|&key| (key, Rect::EMPTY, pool.pop().unwrap_or_default())).collect(); + let composite_tile = |(key, done, region): &mut ((i32, i32), Rect, Vec)| { + let (tx, ty) = *key; + let Some(tile) = self.cov.tiles.get(&tile_key((tx, ty))) else { return }; + let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&bounds); + if tr.is_empty() { + return; + } + pre.read_region_into(tr, region); + let sel = selection.map(|s| (s.channels(), s.read_region(tr))); + // Per-dab colour rewrites the source per pixel, so each tile works on its own copy + // that starts from the brush colour. (Before the tiles ran in parallel, one `src` + // carried over from tile to tile; a tile without per-dab colour then painted with + // whatever colour the previous tile's last pixel had left in it. Such a tile only + // occurs when the stroke has no per-dab colour at all, and then `src` is never + // rewritten, so the pixels are the same either way; the copy per tile is the + // intended reading.) + let mut src = src; + let w = tr.width() as usize; + for y in tr.y0..tr.y1 { + for x in tr.x0..tr.x1 { + let ti = ((y - ty * COV_TILE) * COV_TILE + (x - tx * COV_TILE)) as usize; + let c = tile.cov[ti]; + if c <= 0.0 { continue; } - } - let px = &mut region[i * n..(i + 1) * n]; - if b.erase { - if let (Some(a), false) = (a_idx, lock_transparency) { - px[a] *= 1.0 - k; + let i = (y - tr.y0) as usize * w + (x - tr.x0) as usize; + let m = self.ctx.stroke_mask_wet(c, tile.depth.get(ti).copied(), self.dual.as_ref().map(|d| d.get(x, y)), x, y); + let s = sel.as_ref().map_or(1.0, |(sc, v)| v[i * sc]); + let mut k = (m * opacity * s).min(1.0); + if k <= 0.0 { + continue; + } + if b.mode == BlendMode::Dissolve { + k = if hash2(x, y, b.seed) < k { 1.0 } else { 0.0 }; + if k == 0.0 { + continue; + } + } + let px = &mut region[i * n..(i + 1) * n]; + if b.erase { + if let (Some(a), false) = (a_idx, lock_transparency) { + px[a] *= 1.0 - k; + } + if dither { + dither8(px, x, y); + } + continue; + } + if lock_transparency && a_idx.is_some_and(|a| px[a] <= 0.0) { + continue; + } + if self.per_dab_color && !tile.col.is_empty() { + let p = &tile.col[ti * nc..(ti + 1) * nc]; + for ch in 0..nc { + src[ch] = p[ch] / c; + } + } + if b.behind { + let Some(a) = a_idx else { continue }; + let da = px[a]; + if lock_transparency || da >= 1.0 { + continue; + } + let underneath = src[a] * k * (1.0 - da); + let alpha = da + underneath; + if alpha > 0.0 { + for channel in 0..a { + px[channel] = (px[channel] * da + src[channel] * underneath) / alpha; + } + px[a] = alpha; + } + } else if matches!(b.mode, BlendMode::Normal | BlendMode::Dissolve) { + over_native(&fmt, px, &src[..n], k, lock_transparency); + } else { + let d = to_rgba(&fmt, px); + let sr = to_rgba(&fmt, &src[..n]); + let o = if lock_transparency { + photocraft_color::blend::composite_atop(b.mode, d, sr, k) + } else { + photocraft_color::blend::composite(b.mode, d, sr, k) + }; + let mut enc = [0.0f32; 8]; + from_rgba_into(&fmt, o, &mut enc); + px.copy_from_slice(&enc[..n]); } if dither { dither8(px, x, y); } - continue; - } - if lock_transparency && a_idx.is_some_and(|a| px[a] <= 0.0) { - continue; - } - if self.per_dab_color && !tile.col.is_empty() { - let p = &tile.col[ti * nc..(ti + 1) * nc]; - for ch in 0..nc { - src[ch] = p[ch] / c; - } - } - if matches!(b.mode, BlendMode::Normal | BlendMode::Dissolve) { - over_native(&fmt, px, &src[..n], k, lock_transparency); - } else { - let d = to_rgba(&fmt, px); - let sr = to_rgba(&fmt, &src[..n]); - let mut o = photocraft_color::blend::composite(b.mode, d, sr, k); - if lock_transparency { - o[3] = d[3]; - } - let mut enc = [0.0f32; 8]; - from_rgba_into(&fmt, o, &mut enc); - px.copy_from_slice(&enc[..n]); - } - if dither { - dither8(px, x, y); } } + *done = tr; + }; + for_each_item(&mut items, batch.len() * (COV_TILE * COV_TILE) as usize, |item| { + photocraft_color::convert::with_cmyk_space(cmyk.as_ref(), || composite_tile(item)) + }); + for (_, tr, region) in items { + if !tr.is_empty() { + target.write_region(tr, ®ion); + dmg = dmg.union(&tr); + } + pool.push(region); } - target.write_region(tr, ®ion); - dmg = dmg.union(&tr); } + self.regions.0 = pool; if all { bounds } else { dmg } } } @@ -1082,34 +1294,72 @@ pub fn render_stroke( r.composite(&pre, target, selection, lock_transparency, true) } -// The zero-angle transform only changes signs; squaring gives the same radial distance. +// With zero angle, the transform only changes signs. For circles, any rotation preserves +// radial distance. Both cases can omit the transform (projection is excluded by the caller). #[inline(never)] -fn rasterize_round(d: &Dab, rect: Rect, out: &mut [f32], hardness: f32, aliased: bool) { +fn rasterize_round(d: &Dab, rect: Rect, out: &mut [f32], profile: TipProfile, aliased: bool) { let (cx, cy) = if aliased { grid_center(d.center.x, d.center.y, 2.0 * d.radius) } else { (d.center.x as f32, d.center.y as f32) }; let ro = d.roundness.max(0.5 / d.radius).min(1.0); let rm = (d.radius * ro).max(0.5); let w = rect.width() as usize; - let reach = if aliased { rm } else { tip_reach(rm, hardness).max(rm) }; - let reach2 = (reach + 0.5) * (reach + 0.5); + if w == 0 { + return; + } + let reach = if aliased { rm } else { profile.reach().max(rm) }; + let tip = RoundTip { cx, cy, ro, rm, reach2: (reach + 0.5) * (reach + 0.5), profile, alpha: d.alpha, aliased, x0: rect.x0, y0: rect.y0 }; + // Rows are independent: a large tip's rows run in parallel, to the same values. The serial + // path stays a plain loop over an inlined row: the same code as before the threads (a closure + // passed to a helper cost a small tip half its rasterization time). + #[cfg(not(target_arch = "wasm32"))] + if parallel(out.len()) { + use rayon::prelude::*; + out.par_chunks_exact_mut(w).enumerate().for_each(|(yy, row)| tip.row(yy, row)); + return; + } for (yy, row) in out.chunks_exact_mut(w).enumerate() { - let dy = (rect.y0 + yy as i32) as f32 + 0.5 - cy; - if aliased { + tip.row(yy, row); + } +} + +/// A round tip's coverage, computed row by row (see [`rasterize_round`]). +#[derive(Clone, Copy)] +struct RoundTip { + cx: f32, + cy: f32, + ro: f32, + rm: f32, + reach2: f32, + profile: TipProfile, + alpha: f32, + aliased: bool, + x0: i32, + y0: i32, +} + +impl RoundTip { + /// Row `yy` of the tip's rectangle into `row`. + #[inline(always)] + fn row(self, yy: usize, row: &mut [f32]) { + let dy = (self.y0 + yy as i32) as f32 + 0.5 - self.cy; + if self.aliased { for (xx, dst) in row.iter_mut().enumerate() { - let dx = (rect.x0 + xx as i32) as f32 + 0.5 - cx; - let d2 = (dx * ro).powi(2) + dy * dy; - *dst = if d2 < reach2 && d2.sqrt() <= rm { d.alpha } else { 0.0 }; + let dx = (self.x0 + xx as i32) as f32 + 0.5 - self.cx; + let d2 = (dx * self.ro).powi(2) + dy * dy; + *dst = if d2 < self.reach2 && d2.sqrt() <= self.rm { self.alpha } else { 0.0 }; } } else { for (xx, dst) in row.iter_mut().enumerate() { - let dx = (rect.x0 + xx as i32) as f32 + 0.5 - cx; - let d2 = (dx * ro).powi(2) + dy * dy; - let val = if d2 >= reach2 { 0.0 } else { tip_falloff(d2.sqrt(), rm, hardness) }; - *dst = if val <= 0.0 { 0.0 } else { val * d.alpha }; + let dx = (self.x0 + xx as i32) as f32 + 0.5 - self.cx; + let d2 = (dx * self.ro).powi(2) + dy * dy; + let val = if d2 >= self.reach2 { 0.0 } else { self.profile.falloff(d2.sqrt()) }; + *dst = if val <= 0.0 { 0.0 } else { val * self.alpha }; } } } } #[cfg(test)] -#[path = "render_reference.rs"] mod reference; + +#[cfg(test)] +mod coverage_tests; diff --git a/crates/paint/src/render/coverage_tests.rs b/crates/paint/src/render/coverage_tests.rs new file mode 100644 index 0000000000..0b992308ad --- /dev/null +++ b/crates/paint/src/render/coverage_tests.rs @@ -0,0 +1,190 @@ +use super::*; + +use std::collections::{HashMap, HashSet}; + +#[test] +fn packed_tile_coordinates_round_trip_without_collisions() { + let values = [i32::MIN, i32::MIN + 1, -1_000_003, -1, 0, 1, 1_000_003, i32::MAX - 1, i32::MAX]; + let coords: Vec<_> = values.iter().flat_map(|&x| values.iter().map(move |&y| (x, y))).collect(); + let keys: HashSet<_> = coords.iter().copied().map(tile_key).collect(); + assert_eq!(keys.len(), coords.len()); + for coord in coords { + assert_eq!(tile_coords(tile_key(coord)), coord); + } +} + +struct DabInput { + rect: Rect, + vals: Vec, + ceil: f32, + color: Option>, + depth: Option>, +} + +fn serial(map: &mut CoverageMap, dab: &DabInput) { + if dab.rect.is_empty() { + return; + } + map.bounds = map.bounds.union(&dab.rect); + let (x0, y0) = (dab.rect.x0.div_euclid(COV_TILE), dab.rect.y0.div_euclid(COV_TILE)); + let (x1, y1) = ((dab.rect.x1 - 1).div_euclid(COV_TILE), (dab.rect.y1 - 1).div_euclid(COV_TILE)); + let w = dab.rect.width() as usize; + let values = DabValues { vals: &dab.vals, rect: dab.rect, w, ceil: dab.ceil, color: dab.color.as_deref(), depth: dab.depth.as_deref(), nc: map.nc }; + let keys = (y0..=y1).flat_map(|ty| { + (x0..=x1).map(move |tx| { + let tr = Rect::new(tx * COV_TILE, ty * COV_TILE, (tx + 1) * COV_TILE, (ty + 1) * COV_TILE).intersect(&dab.rect); + ((tx, ty), tr) + }) + }); + map.accumulate_serial(keys.filter(|(_, tr)| values.touches(*tr)), values); +} + +fn pixel_index(rect: Rect, x: i32, y: i32) -> usize { + (y - rect.y0) as usize * rect.width() as usize + (x - rect.x0) as usize +} + +fn make_dab(rect: Rect, seed: usize, color: bool, depth: bool) -> DabInput { + let w = rect.width() as usize; + let h = rect.height() as usize; + let n = w * h; + let vals = (0..n).map(|i| if (i + seed).is_multiple_of(11) { 0.0 } else { (((i * 7 + seed * 3) % 13) as f32 + 1.0) / 17.0 }).collect(); + let colors = color.then(|| vec![seed as f32 / 10.0, 0.75]); + let depths = depth.then(|| (0..n).map(|i| ((i * 3 + seed * 2) % 17) as f32 / 16.0).collect()); + DabInput { rect, vals, ceil: if seed.is_multiple_of(2) { 0.72 } else { 0.93 }, color: colors, depth: depths } +} + +#[derive(Default)] +struct OraclePixel { + cov: f32, + color: [f32; 2], + has_color: bool, + depth: Option, +} + +fn apply_oracle(oracle: &mut HashMap<(i32, i32), OraclePixel>, dab: &DabInput) { + for y in dab.rect.y0..dab.rect.y1 { + for x in dab.rect.x0..dab.rect.x1 { + let i = pixel_index(dab.rect, x, y); + let v = dab.vals[i]; + if v <= 0.0 { + continue; + } + let p = oracle.entry((x, y)).or_default(); + if let Some(depth) = dab.depth.as_ref().and_then(|d| d.get(i)) { + p.depth = Some(p.depth.map_or(*depth, |old| old.max(*depth))); + } + let old = p.cov; + let new = if dab.ceil > old { old + v * (dab.ceil - old) } else { old }; + if new > old { + if let Some(color) = &dab.color { + let k = (new - old) / (1.0 - old); + for (channel, out) in p.color.iter_mut().enumerate() { + *out = color[channel] * k + *out * (1.0 - k); + } + p.has_color = true; + } + p.cov = new; + } + } + } +} + +#[test] +fn overlapping_accumulation_matches_dense_pixel_oracle_and_serial_path() { + let mut zero_only = make_dab(Rect::new(384, 0, 386, 2), 6, true, true); + zero_only.vals.fill(0.0); + let dabs = [ + make_dab(Rect::new(-67, -3, 68, 67), 1, false, false), // crosses negative and positive tile edges + make_dab(Rect::new(-70, -130, 260, 195), 2, true, true), // lazily enables colour and depth across more tiles + make_dab(Rect::new(62, 60, 70, 68), 3, true, true), + make_dab(Rect::new(-70, 64, -63, 70), 4, false, true), // depth continues after coverage ceiling + make_dab(Rect::new(192, 0, 193, 1), 5, true, true), // revisit an existing tile + zero_only, // all-zero tile must be skipped + ]; + let mut actual = CoverageMap::new(2); + let mut expected_serial = CoverageMap::new(2); + let mut oracle = HashMap::new(); + let mut bounds = Rect::default(); + for dab in &dabs { + actual.accumulate(dab.rect, &dab.vals, dab.ceil, dab.color.as_deref(), dab.depth.as_deref()); + serial(&mut expected_serial, dab); + apply_oracle(&mut oracle, dab); + bounds = bounds.union(&dab.rect); + } + + assert_eq!(actual.bounds(), bounds); + assert_eq!(actual.bounds(), expected_serial.bounds()); + let expected_dirty: HashSet<_> = expected_serial.dirty.iter().copied().collect(); + assert_eq!(actual.take_dirty().into_iter().collect::>(), expected_dirty); + assert_eq!(expected_serial.take_dirty().into_iter().collect::>(), expected_dirty); + + let tile_coords_actual: HashSet<_> = actual.tiles.keys().copied().map(tile_coords).collect(); + assert_eq!(tile_coords_actual, expected_dirty); + assert!(!tile_coords_actual.contains(&(6, 0))); // the zero-only tile was never allocated + assert!(tile_coords_actual.len() >= 16); + + for y in -130..195 { + for x in -70..260 { + let i = pixel_index_for_tile(x, y); + let tx = x.div_euclid(COV_TILE); + let ty = y.div_euclid(COV_TILE); + let a = actual.tiles.get(&tile_key((tx, ty))); + let b = expected_serial.tiles.get(&tile_key((tx, ty))); + if let (Some(a), Some(b)) = (a, b) { + assert_eq!(a.cov[i].to_bits(), b.cov[i].to_bits(), "coverage at ({x}, {y})"); + assert_eq!(a.depth.get(i).map(|v| v.to_bits()), b.depth.get(i).map(|v| v.to_bits()), "depth at ({x}, {y})"); + assert_eq!( + a.col.get(i * 2..i * 2 + 2).map(|s| [s[0].to_bits(), s[1].to_bits()]), + b.col.get(i * 2..i * 2 + 2).map(|s| [s[0].to_bits(), s[1].to_bits()]), + "color at ({x}, {y})" + ); + } else { + assert!(a.is_none() && b.is_none()); + } + let p = oracle.get(&(x, y)); + let expected_cov = p.map_or(0.0, |p| p.cov); + assert_eq!(actual.get(x, y).to_bits(), expected_cov.to_bits(), "dense oracle coverage at ({x}, {y})"); + let expected_depth = p.and_then(|p| p.depth).unwrap_or(0.0); + assert_eq!(actual.depth(x, y).unwrap_or(0.0).to_bits(), expected_depth.to_bits(), "dense oracle depth at ({x}, {y})"); + if let Some(p) = p.filter(|p| p.has_color) { + let color = &actual.tiles[&tile_key((tx, ty))].col; + assert_eq!(color[i * 2].to_bits(), p.color[0].to_bits(), "dense oracle color at ({x}, {y})"); + assert_eq!(color[i * 2 + 1].to_bits(), p.color[1].to_bits(), "dense oracle color at ({x}, {y})"); + } + } + } + + // Depth still records a later dab even where coverage already reached its ceiling. + let (x, y) = (-67, 64); + let mut repeated = CoverageMap::new(0); + let values = [1.0]; + repeated.accumulate(Rect::new(x, y, x + 1, y + 1), &values, 0.5, None, None); + repeated.accumulate(Rect::new(x, y, x + 1, y + 1), &values, 0.5, None, Some(&[0.2])); + repeated.accumulate(Rect::new(x, y, x + 1, y + 1), &values, 0.5, None, Some(&[0.8])); + assert_eq!(repeated.get(x, y), 0.5); + assert_eq!(repeated.depth(x, y), Some(0.8)); +} + +fn pixel_index_for_tile(x: i32, y: i32) -> usize { + (y.rem_euclid(COV_TILE) * COV_TILE + x.rem_euclid(COV_TILE)) as usize +} + +#[test] +fn sparse_large_footprint_only_allocates_and_dirties_nonzero_tiles() { + let rect = Rect::new(-128, -128, 256, 256); + let mut vals = vec![0.0; rect.width() as usize * rect.height() as usize]; + vals[pixel_index(rect, -1, 64)] = 0.5; + let mut map = CoverageMap::new(0); + map.accumulate(rect, &vals, 0.8, Some(&[1.0]), None); + assert_eq!(map.tiles.len(), 1); + assert_eq!(map.take_dirty(), vec![(-1, 1)]); + assert_eq!(map.get(-1, 64), 0.4); + assert!(map.tiles.values().all(|tile| tile.col.is_empty() && tile.depth.is_empty())); + vals.fill(0.0); + map.accumulate(rect, &vals, 0.8, None, None); + assert!(map.take_dirty().is_empty()); + assert_eq!(map.tiles.len(), 1); + let bounds = map.bounds(); + map.accumulate(Rect::new(0, 0, 0, 0), &[], 0.8, None, None); + assert_eq!(map.bounds(), bounds); +} diff --git a/crates/paint/src/render_reference.rs b/crates/paint/src/render/reference.rs similarity index 69% rename from crates/paint/src/render_reference.rs rename to crates/paint/src/render/reference.rs index 10172e6fea..e10fdd099f 100644 --- a/crates/paint/src/render_reference.rs +++ b/crates/paint/src/render/reference.rs @@ -1,6 +1,7 @@ //! Frozen scalar rasterizer used only as a pixel oracle for the computed-tip fast path. use super::*; use crate::tile::GrayTile; +use crate::{soft_exp, tip_reach, tip_shape}; use photocraft_geom::Point; impl BrushContext { @@ -161,6 +162,64 @@ fn computed_tips_match_scalar_bits() { } } +#[test] +fn circular_tips_ignore_finite_rotation() { + for aliased in [false, true] { + for hardness in [0.0, 0.5, 1.0] { + let ctx = BrushContext::new(&BrushSettings { aliased, hardness, ..Default::default() }); + for radius in [3.0, 15.25, 150.0] { + for roundness in [1.0, 1.5] { + let mut d = Dab::round(Point::new(-31.25, 63.75), radius, 0.37); + d.roundness = roundness; + let rect = ctx.dab_rect(&d, false); + let expected: Vec<_> = [false, true] + .into_iter() + .map(|dual| { + let mut coverage = Vec::new(); + ctx.rasterize_reference(&d, dual, rect, &mut coverage, None); + coverage + }) + .collect(); + for angle in [-2.3, 0.7, std::f32::consts::FRAC_PI_2, std::f32::consts::PI] { + for (flip_x, flip_y) in [(false, false), (true, false), (false, true), (true, true)] { + d.angle = angle; + d.flip_x = flip_x; + d.flip_y = flip_y; + for (dual, expected) in [false, true].into_iter().zip(&expected) { + let mut actual = vec![0.75; 19]; + ctx.rasterize(&d, dual, rect, &mut actual); + assert_eq!(actual.len(), expected.len()); + let mismatch = actual.iter().zip(expected).enumerate().find(|(_, (a, b))| a.to_bits() != b.to_bits()); + assert!( + mismatch.is_none(), + "circle changed with rotation: {mismatch:?}; {d:?}, aliased={aliased}, hardness={hardness}, dual={dual}" + ); + } + } + } + } + } + } + } +} + +#[test] +fn rotated_ellipses_and_projected_circles_match_scalar_bits() { + for aliased in [false, true] { + let ctx = BrushContext::new(&BrushSettings { aliased, hardness: 0.5, ..Default::default() }); + for (roundness, proj_scale) in [(0.4, 1.0), (1.0, 0.6)] { + for angle in [0.0, 0.7, -2.3] { + let mut d = Dab::round(Point::new(-31.25, 63.75), 15.25, 0.37); + d.roundness = roundness; + d.angle = angle; + d.proj_scale = proj_scale; + d.proj_angle = 0.3; + compare(&ctx, &d, false, ctx.dab_rect(&d, false), false); + } + } + } +} + #[test] fn large_corner_dabs_keep_off_canvas_coverage() { for (hardness, aliased) in [(0.0, false), (1.0, false), (1.0, true)] { @@ -243,3 +302,58 @@ fn computed_tips_match_at_pixel_and_float_boundaries() { } } } + +#[test] +fn cached_profiles_match_scalar_for_small_and_transformed_dabs() { + for hardness in [-0.5, 0.0, 0.035, 0.37, 0.95, 1.0, 1.5, f32::NAN, f32::INFINITY] { + let ctx = BrushContext::new(&BrushSettings { hardness, ..Default::default() }); + for radius in [0.5, 1.25, 2.99, 3.0, 7.5, 100.0] { + for dual in [false, true] { + for with_depth in [false, true] { + for angle in [0.0, 0.7] { + let mut d = Dab::round(Point::new(4.25, -3.75), radius, 0.73); + d.angle = angle; + d.roundness = 0.6; + compare(&ctx, &d, dual, ctx.dab_rect(&d, dual), with_depth); + } + } + } + } + } +} + +// Keep the pre-cache scalar coverage equations independent of TipProfile. +fn tip_falloff(d: f32, radius: f32, hardness: f32) -> f32 { + let (r0, s) = tip_shape(radius, hardness); + if d <= r0 { + return 1.0; + } + let x = (d - r0) / s; + soft_exp(x * x) +} + +fn dab_coverage(d: f32, radius: f32, hardness: f32) -> f32 { + // NaN distances paint nothing. + if d.is_nan() || d >= tip_reach(radius, hardness) + 0.5 { + return 0.0; + } + tip_falloff(d, radius, hardness) +} + +#[test] +fn cached_profile_preserves_public_coverage_at_boundaries() { + for radius in [-1.0, 0.0, 0.5, 3.0, 100.0, f32::NAN, f32::INFINITY] { + for hardness in [-1.0, 0.0, 0.035, 0.37, 1.0, 2.0, f32::NAN, f32::INFINITY] { + let profile = TipProfile::new(radius, hardness); + let (core, shoulder) = tip_shape(radius, hardness); + for d in [-1.0, -0.0, core, core + shoulder, profile.reach(), profile.reach() + 0.5, f32::INFINITY, f32::NEG_INFINITY, f32::NAN] { + let expected = dab_coverage(d, radius, hardness); + assert_eq!(profile.coverage(d).to_bits(), expected.to_bits()); + assert_eq!(crate::dab_coverage(d, radius, hardness).to_bits(), expected.to_bits()); + let expected = tip_falloff(d, radius, hardness); + let actual = crate::tip_falloff(d, radius, hardness); + assert!(actual.to_bits() == expected.to_bits() || (actual.is_nan() && expected.is_nan())); + } + } + } +} diff --git a/crates/paint/src/replace.rs b/crates/paint/src/replace.rs index 3cb02e9a24..20619cd48a 100644 --- a/crates/paint/src/replace.rs +++ b/crates/paint/src/replace.rs @@ -127,6 +127,8 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut return; } let (w, h) = (r.width() as usize, r.height() as usize); + let mut pre_px = vec![[0.0f32; 4]; w * h]; + pre.read_rgba_into(r, &mut pre_px); // Per-pixel match weight. let mut weight = vec![0.0f32; w * h]; for y in r.y0..r.y1 { @@ -136,7 +138,7 @@ pub fn color_replacement_effect(pre: Surface, rs: ReplaceSettings) -> impl FnMut } // Match against the pre-stroke colours, so pixels recoloured by earlier dabs of // this stroke still count as the sampled colour. - let c = pre.rgba(x, y); + let c = pre_px[(y - r.y0) as usize * w + (x - r.x0) as usize]; if c[3] <= 0.0 { continue; } diff --git a/crates/paint/src/retouch.rs b/crates/paint/src/retouch.rs index 9ef837c0c0..06bafe6cfb 100644 --- a/crates/paint/src/retouch.rs +++ b/crates/paint/src/retouch.rs @@ -130,6 +130,24 @@ pub fn stroke_coverage(stroke: &Stroke) -> (Rect, Vec) { r.dense_coverage() } +/// [`stroke_coverage`] that admits the **rendered** extent before it is made dense: `None` when the +/// stroke covers more than `max_pixels`. +/// +/// Rendering itself is tiled, so it holds only the tiles the dabs touch. The bounds are read from +/// those tiles, after tips, dynamics and scattering have placed every dab, so this is the stroke's +/// real extent rather than an estimate from the input points. Only then is the `width × height` +/// buffer allocated, which is what callers need bounded. +pub fn stroke_coverage_within(stroke: &Stroke, max_pixels: u64) -> Option<(Rect, Vec)> { + let mut r = crate::StrokeRenderer::new(&stroke.brush, None, 1.0); + r.push(&stroke.points); + r.finish(); + let b = r.bounds(); + if u64::from(b.width()).saturating_mul(u64::from(b.height())) > max_pixels { + return None; + } + Some(r.dense_coverage()) +} + /// Index of the alpha channel, if the format has one. #[inline] pub fn alpha_index(fmt: &PixelFormat) -> Option { @@ -143,15 +161,20 @@ pub fn over_native(fmt: &PixelFormat, dst: &mut [f32], src: &[f32], k: f32, lock match alpha_index(fmt) { Some(a) => { let (da, sa) = (dst[a], src[a] * k); + if lock_transparency { + // Alpha kept: source atop, the colour moves by the source alpha alone (#5783). + for c in 0..a { + dst[c] += (src[c] - dst[c]) * sa; + } + return; + } let oa = sa + da * (1.0 - sa); if oa > 0.0 { for c in 0..a { dst[c] = (src[c] * sa + dst[c] * da * (1.0 - sa)) / oa; } } - if !lock_transparency { - dst[a] = oa; - } + dst[a] = oa; } None => { for c in 0..dst.len() { @@ -202,10 +225,8 @@ pub fn apply_coverage( } else { let d = to_rgba(&fmt, px); let s = to_rgba(&fmt, src); - let mut o = photocraft_color::blend::composite(blend, d, s, k); - if lock_transparency { - o[3] = d[3]; - } + let o = + if lock_transparency { photocraft_color::blend::composite_atop(blend, d, s, k) } else { photocraft_color::blend::composite(blend, d, s, k) }; let mut enc = [0.0f32; 8]; from_rgba_into(&fmt, o, &mut enc); px.copy_from_slice(&enc[..n]); @@ -263,6 +284,10 @@ pub fn mix_back(target: &mut Surface, orig: &Region, work: &Region, rect: Rect, let wv = work.px(x, y); let i = out.index(x, y) * n; let o = &mut out.data[i..i + n]; + // Untouched by the dabs: keep the pixel exactly (the premultiplied mix can round it). + if *o == *wv { + continue; + } match a { // Alpha changes: mix premultiplied, so colour under transparent pixels doesn't bleed in. Some(a) if !lock_transparency => mix_premultiplied(o, wv, a, k), @@ -315,8 +340,14 @@ pub struct Smudge { } impl Smudge { + /// Radius of the carried-colour buffer, including pixels outside a pressure-sized dab. + /// Both the first pickup and every subsequent pickup need this entire neighbourhood. + pub fn sampling_radius(max_brush_size: f32) -> i32 { + ((max_brush_size / 2.0).ceil() as i32).saturating_add(2) + } + pub fn new(strength: f32, finger: Option>, max_brush_size: f32) -> Self { - Self { strength: strength.clamp(0.0, 1.0), finger, radius: (max_brush_size / 2.0).ceil() as i32 + 2, alpha: None, carry: None } + Self { strength: strength.clamp(0.0, 1.0), finger, radius: Self::sampling_radius(max_brush_size), alpha: None, carry: None } } /// Mix in premultiplied space using alpha channel `a` (the format's [`alpha_index`]). @@ -463,6 +494,23 @@ mod tests { } } + #[test] + fn coverage_composite_under_lock_blends_source_atop() { + // Locked half-transparent white, black at coverage 0.5: colour halfway, alpha kept (#5783). + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for mode in [BlendMode::Normal, BlendMode::Multiply] { + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 4, 4), &[1.0, 1.0, 1.0, 0.5]); + let r = Rect::new(0, 0, 4, 4); + let mut paint = Region::new(r, 4); + paint.data.chunks_mut(4).for_each(|p| p.copy_from_slice(&[0.0, 0.0, 0.0, 1.0])); + apply_coverage(&mut s, r, &[1.0; 16], 0.5, None, true, &paint, mode); + let p = s.rgba(1, 1); + assert!((p[0] - 0.5).abs() < 0.01 && (p[3] - 0.5).abs() < 0.01, "{fmt:?} {mode:?} {p:?}"); + } + } + } + #[test] fn dab_stroke_runs_sequentially_and_respects_selection() { let mut s = Surface::new(PixelFormat::GRAY8); diff --git a/crates/paint/src/tile.rs b/crates/paint/src/tile.rs index 38ca38410e..8ea0e776b6 100644 --- a/crates/paint/src/tile.rs +++ b/crates/paint/src/tile.rs @@ -40,7 +40,7 @@ impl GrayTile { self.data.iter().map(|&v| f32::from(v) / 65535.0).collect() } pub fn is_valid(&self) -> bool { - self.width > 0 && self.height > 0 && self.data.len() == self.width as usize * self.height as usize + self.width > 0 && self.height > 0 && (self.width as usize).checked_mul(self.height as usize) == Some(self.data.len()) } /// A box-filtered copy whose larger side is at most `max_side` (the whole tile when it is @@ -157,7 +157,9 @@ impl<'de> Deserialize<'de> for GrayTile { fn deserialize>(d: D) -> Result { use serde::de::Error; let t = GrayTileIn::deserialize(d)?; - let n = t.width as usize * t.height as usize; + let Some(n) = (t.width as usize).checked_mul(t.height as usize) else { + return Err(D::Error::custom("tile dimensions overflow the address space")); + }; let data: Vec = match t.data { DataIn::B64(s) => { let b = b64_decode(&s).ok_or_else(|| D::Error::custom("invalid base64 in tile data"))?; @@ -232,6 +234,9 @@ pub struct Mips { impl Mips { pub fn new(t: &GrayTile) -> Self { + if !t.is_valid() { + return Self { levels: vec![(1, 1, vec![0.0])] }; + } let mut levels = vec![(t.width as usize, t.height as usize, t.to_f32())]; while let Some((w, h, v)) = levels.last() { if *w <= 1 && *h <= 1 { @@ -335,6 +340,18 @@ mod tests { let f: GrayTile = serde_json::from_str(r#"{"width":2,"height":1,"data":[0.0,1.0]}"#).unwrap(); assert_eq!(f.data, vec![0, 65535].into()); assert!(serde_json::from_str::(r#"{"width":2,"height":2,"data":[0.0,1.0]}"#).is_err()); + assert!(serde_json::from_str::(r#"{"width":4294967295,"height":4294967295,"data":[0.0]}"#).is_err()); + let invalid = GrayTile { width: u32::MAX, height: u32::MAX, data: vec![1].into() }; + assert!(!invalid.is_valid()); + assert_eq!(Mips::new(&invalid).levels, vec![(1, 1, vec![0.0])]); + } + + /// `u32::MAX * u32::MAX` only overflows `usize` on 32-bit targets (wasm); 64-bit hits the length check. + #[cfg(target_pointer_width = "32")] + #[test] + fn oversized_dimensions_report_address_space_overflow() { + let err = serde_json::from_str::(r#"{"width":4294967295,"height":4294967295,"data":[0.0]}"#).unwrap_err(); + assert!(err.to_string().contains("tile dimensions overflow the address space")); } #[test] diff --git a/crates/plugins/Cargo.toml b/crates/plugins/Cargo.toml index 09a69ca838..a88b7711f3 100644 --- a/crates/plugins/Cargo.toml +++ b/crates/plugins/Cargo.toml @@ -24,6 +24,10 @@ wasmi = { version = "2.0.0", default-features = false, features = ["std", "valid [target.'cfg(not(target_arch = "wasm32"))'.dependencies] rayon = { workspace = true } +# Opening a plug-in file without blocking on a FIFO (the flag constant only). +[target.'cfg(unix)'.dependencies] +rustix = { version = "1.1", default-features = false, features = ["std", "fs"] } + [dev-dependencies] # WebAssembly text -> binary for the hostile-module fixtures (Apache-2.0 WITH LLVM-exception). wat = "1.228" diff --git a/crates/plugins/src/apply.rs b/crates/plugins/src/apply.rs index 7a144a28b9..52e3b85826 100644 --- a/crates/plugins/src/apply.rs +++ b/crates/plugins/src/apply.rs @@ -70,6 +70,9 @@ fn read_clamped(s: &Surface, rect: Rect, extent: Rect) -> Vec { out } +/// Most bands one run is split into. +const MAX_BANDS: u64 = 1 << 16; + impl Plugin { /// The rectangle a run writes: the layer's content (grown by the plug-in's overlap) or the /// canvas, limited to the selection's bounds. @@ -113,6 +116,13 @@ impl Plugin { } let mut bands = Vec::new(); let (t0, t1) = (TileCoord::containing(area.x0, area.y0), TileCoord::containing(area.x1 - 1, area.y1 - 1)); + // Counted before any is made: a few distant tiles make `area` a grid of millions of + // bands, each a call into the plug-in, planned before the deadline starts. + let columns = (i64::from(t1.tx) - i64::from(t0.tx) + 1).max(0) as u64; + let rows = (i64::from(t1.ty) - i64::from(t0.ty) + 1).max(0) as u64; + if columns.div_ceil(across as u64).saturating_mul(rows) > MAX_BANDS { + return Err(Error::Limit(format!("more than {MAX_BANDS} bands (the layer's content is spread too widely)"))); + } for ty in t0.ty..=t1.ty { let mut tx = t0.tx; while tx <= t1.tx { @@ -130,9 +140,10 @@ impl Plugin { } r }; - // A few bands at a time (one per worker), so peak memory stays near one band per thread. + // A few bands at a time: one per worker, and no more instances than the run's memory + // budget holds at their cap, so peak memory stays bounded however many cores run (#8603). #[cfg(not(target_arch = "wasm32"))] - let group = rayon::current_num_threads().max(1); + let group = self.limits().parallel_bands(rayon::current_num_threads()); #[cfg(target_arch = "wasm32")] let group = 1; for chunk in bands.chunks(group) { @@ -200,7 +211,8 @@ impl Plugin { v = v.clamp(0.0, 1.0); } if let Some(d) = out.get_mut(di + ch) { - *d = o + (v - o) * k; + // As a built-in edit mixes through the selection (#9483). + *d = o * (1.0 - k) + v * k; } } } diff --git a/crates/plugins/src/lib.rs b/crates/plugins/src/lib.rs index 57ee000471..ce6e44ccd6 100644 --- a/crates/plugins/src/lib.rs +++ b/crates/plugins/src/lib.rs @@ -26,7 +26,7 @@ pub mod registry; mod runtime; pub use apply::format_code; -pub use manifest::{Area, Kind, Manifest, ParamSpec}; +pub use manifest::{Area, Kind, Manifest, ParamSpec, Unit}; pub use runtime::{ABI_VERSION, Limits, Plugin}; /// A plug-in error. Every way a module can misbehave ends here, never in a panic. diff --git a/crates/plugins/src/manifest.rs b/crates/plugins/src/manifest.rs index ccabf47d55..133c0fa843 100644 --- a/crates/plugins/src/manifest.rs +++ b/crates/plugins/src/manifest.rs @@ -30,12 +30,47 @@ pub enum Area { Canvas, } +/// What a `number` or `int` parameter measures (`"unit"` in the manifest). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Unit { + /// No unit (the default): a level, factor, count or percentage, the same at any image size. + #[default] + None, + /// A length in document pixels (a radius, distance, shift or size). On a reduced live preview + /// the host divides it by the reduction, so the preview matches what OK applies. + Px, +} + +impl Unit { + fn is_none(&self) -> bool { + *self == Unit::None + } +} + +/// Reads `"unit"` leniently: `"px"` is [`Unit::Px`]; anything else (an unknown string such as +/// `"pt"`, a number, `null`, an array…) is unitless, as when the field is absent. Manifests from +/// before `unit` existed could carry any value there (unknown fields were ignored), and they must +/// keep installing. +fn lenient_unit<'de, D: serde::Deserializer<'de>>(d: D) -> std::result::Result { + Ok(match Value::deserialize(d)? { + Value::String(s) if s == "px" => Unit::Px, + _ => Unit::None, + }) +} + /// One declared parameter. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "camelCase")] pub enum ParamSpec { /// A real number in `min..=max` (a slider in the dialog). - Number { min: f64, max: f64, default: f64 }, + Number { + min: f64, + max: f64, + default: f64, + #[serde(default, deserialize_with = "lenient_unit", skip_serializing_if = "Unit::is_none")] + unit: Unit, + }, /// A whole number in `min..=max`. Int { #[serde(default = "int_min")] @@ -44,6 +79,8 @@ pub enum ParamSpec { max: i64, #[serde(default)] default: i64, + #[serde(default, deserialize_with = "lenient_unit", skip_serializing_if = "Unit::is_none")] + unit: Unit, }, /// A checkbox. Bool { @@ -122,10 +159,8 @@ impl Manifest { return Err(Error::Manifest(format!("invalid parameter name {k:?}"))); } match spec { - ParamSpec::Number { min, max, default } => { - if !(min.is_finite() && max.is_finite() && default.is_finite()) || min > max { - return Err(Error::Manifest(format!("parameter `{k}`: bad range"))); - } + ParamSpec::Number { min, max, default, .. } if !(min.is_finite() && max.is_finite() && default.is_finite()) || min > max => { + return Err(Error::Manifest(format!("parameter `{k}`: bad range"))); } ParamSpec::Int { min, max, .. } if min > max => return Err(Error::Manifest(format!("parameter `{k}`: bad range"))), ParamSpec::Choice { options, default } => { @@ -149,7 +184,7 @@ impl Manifest { .params .iter() .map(|(k, spec)| match spec { - ParamSpec::Number { min, max, default } => format!("\"{k}\":{min}..{max}={default}"), + ParamSpec::Number { min, max, default, .. } => format!("\"{k}\":{min}..{max}={default}"), ParamSpec::Int { default, .. } => format!("\"{k}\":int={default}"), ParamSpec::Bool { default } => format!("\"{k}\":bool={default}"), ParamSpec::Choice { options, default } => { @@ -181,11 +216,11 @@ impl Manifest { let v = given.get(k).filter(|v| !v.is_null()); let wrong = |what: &str| Error::Params(format!("`{k}` must be {what}")); let resolved = match spec { - ParamSpec::Number { min, max, default } => match v { + ParamSpec::Number { min, max, default, .. } => match v { None => json!((*default).clamp(*min, *max)), Some(v) => json!(v.as_f64().filter(|f| f.is_finite()).ok_or_else(|| wrong("a number"))?.clamp(*min, *max)), }, - ParamSpec::Int { min, max, default } => match v { + ParamSpec::Int { min, max, default, .. } => match v { None => json!((*default).clamp(*min, *max)), Some(v) => { let f = v.as_f64().filter(|f| f.is_finite()).ok_or_else(|| wrong("a number"))?; @@ -211,6 +246,61 @@ impl Manifest { } Ok(out) } + + /// Whether parameter `key` is a length in pixels (`"unit": "px"`). + pub fn is_pixel_param(&self, key: &str) -> bool { + self.pixel_params().any(|(k, ..)| k == key) + } + + /// `(key, min, max, default, is_int)` of each parameter declared `"unit": "px"`. + fn pixel_params(&self) -> impl Iterator { + self.params.iter().filter_map(|(k, spec)| match spec { + ParamSpec::Number { min, max, default, unit: Unit::Px } => Some((k.as_str(), *min, *max, *default, false)), + ParamSpec::Int { min, max, default, unit: Unit::Px } => Some((k.as_str(), *min as f64, *max as f64, *default as f64, true)), + _ => None, + }) + } + + /// The value [`Manifest::resolve_params`] would use for a pixel parameter: the given number, or + /// the default when missing, within `min..=max`. `None` for a value of the wrong type (which + /// `resolve_params` rejects) or one that isn't finite. + fn pixel_value(params: &Map, key: &str, min: f64, max: f64, default: f64) -> Option { + let v = match params.get(key).filter(|v| !v.is_null()) { + None => default, + Some(v) => v.as_f64()?, + }; + // `max`/`min` rather than `clamp`, which panics on a reversed or NaN range. + v.is_finite().then(|| v.max(min).min(max)) + } + + /// Proxy factor for a live preview with `params` on a document reduced `k` times: the largest + /// factor up to `k` at which no pixel parameter with a positive minimum, divided by it, drops + /// below that minimum. A clamped value would change the feature size (as for built-in filters, + /// #2063), so such a preview runs less reduced instead. Always at least 1. + pub fn preview_factor(&self, params: &Map, k: u32) -> u32 { + self.pixel_params().fold(k.max(1), |k, (key, min, max, default, _)| match Self::pixel_value(params, key, min, max, default) { + Some(v) if min > 0.0 => k.min((v / min).floor().max(1.0).min(f64::from(k)) as u32), + Some(v) if max < 0.0 => k.min((v / max).floor().max(1.0).min(f64::from(k)) as u32), + _ => k, + }) + } + + /// Rewrites the plug-in's `params` for a live preview on a copy of the document reduced `k` + /// times: each pixel parameter (the given value, or its default when missing) is divided by + /// `k`, rounded for `int`s and kept within its range, so the preview shows the same lengths in + /// document pixels as OK applies (#7524). Unitless parameters, unknown keys and values of the + /// wrong type (which [`Manifest::resolve_params`] rejects) are left as they are. + pub fn scale_for_preview(&self, params: &mut Map, k: u32) { + if k <= 1 { + return; + } + let k = f64::from(k); + for (key, min, max, default, int) in self.pixel_params() { + let Some(v) = Self::pixel_value(params, key, min, max, default) else { continue }; + let x = (v / k).max(min).min(max); + params.insert(key.to_string(), if int { json!(x.round().max(min).min(max) as i64) } else { json!(x) }); + } + } } /// Reads the `params` object keeping its key order (serde_json's `Map` would sort it). @@ -226,10 +316,16 @@ fn ordered_params<'de, D: serde::Deserializer<'de>>(d: D) -> std::result::Result } fn visit_map>(self, mut map: A) -> std::result::Result { let mut out = Vec::new(); - while let Some((k, v)) = map.next_entry::()? { + while let Some(k) = map.next_key::()? { + // A repeated key would give one value two dialog controls, the last spec winning. + // Named before its spec is read or counted, so the error is about the key. + if out.iter().any(|(seen, _)| *seen == k) { + return Err(serde::de::Error::custom(format!("parameter {k:?} is declared twice"))); + } if out.len() >= MAX_PARAMS { return Err(serde::de::Error::custom(format!("too many parameters (limit {MAX_PARAMS})"))); } + let v = map.next_value::()?; out.push((k, v)); } Ok(out) @@ -308,8 +404,12 @@ fn visible(c: char) -> bool { !c.is_whitespace() && !c.is_control() && !in_ranges(c, FORMAT) && !in_ranges(c, BLANK) } +/// Names the host's `plugin.run` command takes for itself: a parameter of the same name would be +/// read as the command's routing (which layer or channel is filtered), not as the plug-in's. +const HOST_KEYS: [&str; 4] = ["id", "layer", "target", "channel"]; + fn valid_key(k: &str) -> bool { - (1..=64).contains(&k.len()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && !k.starts_with('_') && k != "id" && k != "layer" + (1..=64).contains(&k.len()) && k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') && !k.starts_with('_') && !HOST_KEYS.contains(&k) } #[cfg(test)] @@ -381,6 +481,9 @@ mod tests { r#"{"id":"a","name":"x","kind":"filter","params":{"r":{"type":"number","min":5,"max":1,"default":1}}}"#, r#"{"id":"a","name":"x","kind":"filter","params":{"c":{"type":"choice","options":[]}}}"#, r#"{"id":"a","name":"x","kind":"filter","params":{"__x":{"type":"bool"}}}"#, + // Names the host command routes by: a plug-in default must not steer the filter. + r#"{"id":"a","name":"x","kind":"filter","params":{"target":{"type":"bool","default":false}}}"#, + r#"{"id":"a","name":"x","kind":"filter","params":{"channel":{"type":"int","min":0,"max":3,"default":1}}}"#, r#"{"id":"a","name":"x","kind":"filter","params":[1]}"#, ] { assert!(Manifest::parse(bad.as_bytes()).is_err(), "{bad}"); @@ -389,6 +492,128 @@ mod tests { assert!(Manifest::parse(&vec![b' '; MAX_MANIFEST_BYTES + 1]).is_err()); } + #[test] + fn rejects_a_parameter_declared_twice() { + // Both entries were kept: two dialog controls for one value, and the last spec won. + let twice = br#"{"id":"a","name":"x","kind":"filter", + "params":{"n":{"type":"bool"},"n":{"type":"number","min":0,"max":1,"default":0}}}"#; + assert!(matches!(Manifest::parse(twice), Err(Error::Manifest(_)))); + // The error names the key, also when the second spec is broken or past the limit. + let broken = br#"{"id":"a","name":"x","kind":"filter","params":{"n":{"type":"bool"},"n":{"type":"nope"}}}"#; + let many: Vec = (0..MAX_PARAMS).map(|i| format!(r#""p{i}":{{"type":"bool"}}"#)).collect(); + let past = format!(r#"{{"id":"a","name":"x","kind":"filter","params":{{{},"p0":{{"type":"bool"}}}}}}"#, many.join(",")); + for (json, key) in [(&broken[..], "n"), (past.as_bytes(), "p0")] { + let e = Manifest::parse(json).err().map(|e| e.to_string()).unwrap_or_default(); + assert!(e.contains(&format!("parameter {key:?} is declared twice")), "{e}"); + } + // Keys differing in case are different parameters. + let m = Manifest::parse(br#"{"id":"a","name":"x","kind":"filter","params":{"n":{"type":"bool"},"N":{"type":"bool"}}}"#).unwrap(); + assert_eq!(m.params_notation(), r#"{"n":bool=false,"N":bool=false}"#); + } + + /// Chromatic Aberration-like: a pixel shift, a pixel int, a signed pixel offset and a unitless + /// `size` (a name the built-in filters treat as pixels). + const PIXELS: &str = r#"{"id":"org.example.fringe","name":"Fringe","kind":"filter", + "params":{"amount":{"type":"number","min":0,"max":100,"default":12,"unit":"px"}, + "radius":{"type":"int","min":2,"max":50,"default":10,"unit":"px"}, + "shift":{"type":"number","min":-40,"max":40,"default":0,"unit":"px"}, + "size":{"type":"number","min":0,"max":100,"default":50}, + "mix":{"type":"number","min":0,"max":1,"default":1,"unit":"none"}}}"#; + + #[test] + fn parses_units_and_keeps_old_manifests_unitless() { + let m = Manifest::parse(PIXELS.as_bytes()).unwrap(); + assert!(m.is_pixel_param("amount") && m.is_pixel_param("radius") && m.is_pixel_param("shift")); + assert!(!m.is_pixel_param("size") && !m.is_pixel_param("mix") && !m.is_pixel_param("missing")); + assert!(matches!(m.params[0].1, ParamSpec::Number { unit: Unit::Px, .. })); + assert!(matches!(m.params[1].1, ParamSpec::Int { unit: Unit::Px, .. })); + // No `unit` is unitless, and unitless params serialize as before (no `unit` key). + let old = Manifest::parse(FULL.as_bytes()).unwrap(); + assert!(old.params.iter().all(|(k, _)| !old.is_pixel_param(k))); + let schema = serde_json::to_value(&m).unwrap(); + assert_eq!(schema["params"]["amount"]["unit"], "px"); + assert!(schema["params"]["size"].get("unit").is_none() && schema["params"]["mix"].get("unit").is_none()); + assert_eq!(Manifest::parse(serde_json::to_string(&m).unwrap().as_bytes()).unwrap(), m); + // A unit is a `number`/`int` property; the dialog notation is unchanged by it. + assert_eq!(m.params_notation(), r#"{"amount":0..100=12,"radius":int=10,"shift":-40..40=0,"size":0..100=50,"mix":0..1=1}"#); + } + + #[test] + fn unknown_units_are_unitless() { + // Unknown fields were always ignored, so a manifest may already carry any `unit`: it must + // keep installing, unscaled, exactly as when the field is absent. + for unit in [r#""pt""#, r#""PX""#, r#""""#, "5", "true", "null", r#"["px"]"#, r#"{"px":1}"#] { + let text = format!(r#"{{"id":"a","name":"x","kind":"filter","params":{{"r":{{"type":"number","min":0,"max":9,"default":1,"unit":{unit}}}}}}}"#); + for text in [text.clone(), text.replace(r#""type":"number""#, r#""type":"int""#)] { + let m = Manifest::parse(text.as_bytes()).unwrap_or_else(|e| panic!("{text}: {e}")); + assert!(!m.is_pixel_param("r"), "{text}"); + let mut p = json!({"r": 8}).as_object().unwrap().clone(); + m.scale_for_preview(&mut p, 4); + assert_eq!(Value::Object(p), json!({"r": 8}), "{text}"); + assert!(serde_json::to_value(&m).unwrap()["params"]["r"].get("unit").is_none(), "{text}"); + } + } + } + + #[test] + fn preview_scales_pixel_params_only() { + // #7524: at a reduction of 4, a 12 px shift must be 3 proxy pixels (12 document pixels). + let m = Manifest::parse(PIXELS.as_bytes()).unwrap(); + let given = json!({"id": "org.example.fringe", "amount": 12, "radius": 10, "shift": -20, "size": 40, "mix": 0.5}); + let mut p = given.as_object().unwrap().clone(); + m.scale_for_preview(&mut p, 4); + assert_eq!( + Value::Object(p.clone()), + json!({"id": "org.example.fringe", "amount": 3.0, "radius": 3, "shift": -5.0, "size": 40, "mix": 0.5}), + "px params divided (ints rounded, signs kept); unitless and other keys untouched" + ); + // The scaled values are what the plug-in receives. + assert_eq!(m.resolve_params(&Value::Object(p)).unwrap()["amount"], json!(3.0)); + // Missing px params get their scaled default, so the default isn't previewed unscaled. + let mut p = Map::new(); + m.scale_for_preview(&mut p, 2); + assert_eq!(Value::Object(p), json!({"amount": 6.0, "radius": 5, "shift": 0.0})); + // Never below the minimum, and values are clamped into range first, as `resolve_params` does. + let mut p = json!({"radius": 3, "amount": 1000}).as_object().unwrap().clone(); + m.scale_for_preview(&mut p, 4); + assert_eq!(Value::Object(p), json!({"radius": 2, "amount": 25.0, "shift": 0.0})); + // A full-resolution preview changes nothing. + for k in [0, 1] { + let mut p = given.as_object().unwrap().clone(); + m.scale_for_preview(&mut p, k); + assert_eq!(Value::Object(p), given); + } + // Wrong types and non-numbers are left for `resolve_params` to reject, never a panic. + let mut p = json!({"amount": "lots", "radius": true, "shift": [1]}).as_object().unwrap().clone(); + m.scale_for_preview(&mut p, 4); + assert_eq!(Value::Object(p.clone()), json!({"amount": "lots", "radius": true, "shift": [1]})); + assert!(m.resolve_params(&Value::Object(p)).is_err()); + // A plug-in without px params is never touched, even with built-in pixel names. + let old = Manifest::parse(br#"{"id":"a","name":"x","kind":"filter","params":{"radius":{"type":"number","min":0,"max":9,"default":4}}}"#).unwrap(); + let mut p = json!({"radius": 8}).as_object().unwrap().clone(); + old.scale_for_preview(&mut p, 4); + assert_eq!(Value::Object(p), json!({"radius": 8})); + } + + #[test] + fn preview_factor_keeps_pixel_params_above_their_minimum() { + let m = Manifest::parse(PIXELS.as_bytes()).unwrap(); + let p = |v: Value| v.as_object().unwrap().clone(); + // radius min 2: 10 px allows 4, 5 px allows 2, 3 px only full resolution. + assert_eq!(m.preview_factor(&p(json!({"radius": 10})), 4), 4); + assert_eq!(m.preview_factor(&p(json!({"radius": 5})), 4), 2); + assert_eq!(m.preview_factor(&p(json!({"radius": 3})), 4), 1); + // Missing: the default (10). Params with a zero or negative minimum don't limit it. + assert_eq!(m.preview_factor(&Map::new(), 8), 5); + assert_eq!(m.preview_factor(&p(json!({"radius": 50, "amount": 0, "shift": -40})), 8), 8); + // Never more than the document's factor, never 0; bad values don't limit it. + assert_eq!(m.preview_factor(&p(json!({"radius": 50})), 2), 2); + assert_eq!(m.preview_factor(&p(json!({"radius": 50})), 0), 1); + assert_eq!(m.preview_factor(&p(json!({"radius": "x"})), 4), 4); + let unitless = Manifest::parse(FULL.as_bytes()).unwrap(); + assert_eq!(unitless.preview_factor(&p(json!({"amount": 0.01})), 4), 4); + } + fn with_name(name: &str) -> Result { Manifest::parse(json!({"id": "org.example.tone", "name": name, "kind": "filter"}).to_string().as_bytes()) } diff --git a/crates/plugins/src/registry.rs b/crates/plugins/src/registry.rs index 92eb5f3f1b..c819fb3de6 100644 --- a/crates/plugins/src/registry.rs +++ b/crates/plugins/src/registry.rs @@ -54,19 +54,35 @@ pub fn revision() -> u64 { REVISION.load(Ordering::Relaxed) } +/// Opens `path` for reading without waiting on a FIFO or device that has no writer. +#[cfg(unix)] +fn open_regular(path: &std::path::Path) -> std::io::Result { + use std::os::unix::fs::OpenOptionsExt; + // O_NONBLOCK: opening a writer-less FIFO read-only returns at once; the caller then sees it + // is not a regular file. Regular files ignore the flag. + std::fs::OpenOptions::new().read(true).custom_flags(rustix::fs::OFlags::NONBLOCK.bits() as i32).open(path) +} + +#[cfg(not(unix))] +fn open_regular(path: &std::path::Path) -> std::io::Result { + std::fs::File::open(path) +} + /// Reads a `.wasm` file (refusing anything that isn't a regular file or exceeds the module limit) /// and loads it. #[cfg(not(target_arch = "wasm32"))] pub fn load_file(path: &std::path::Path, limits: Limits) -> Result { use std::io::Read; - let meta = std::fs::metadata(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; + // Open first, without blocking on a FIFO, and judge the file that was opened: a name checked + // and then opened again can be swapped for a special file in between. + let f = open_regular(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; + let meta = f.metadata().map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; if !meta.is_file() { return Err(Error::Io(format!("{} is not a file", path.display()))); } if meta.len() > limits.max_module_bytes as u64 { return Err(Error::Module(format!("{} is {} bytes (limit {})", path.display(), meta.len(), limits.max_module_bytes))); } - let f = std::fs::File::open(path).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; let mut bytes = Vec::new(); f.take(limits.max_module_bytes as u64 + 1).read_to_end(&mut bytes).map_err(|e| Error::Io(format!("{}: {e}", path.display())))?; Ok(Plugin::load(&bytes, limits)?.with_source(path.display().to_string())) diff --git a/crates/plugins/src/runtime.rs b/crates/plugins/src/runtime.rs index e436133b92..c3dd8d9ccc 100644 --- a/crates/plugins/src/runtime.rs +++ b/crates/plugins/src/runtime.rs @@ -23,6 +23,10 @@ pub struct Limits { pub max_module_bytes: usize, /// Cap on a plug-in's linear memory. pub max_memory_bytes: usize, + /// Cap on the linear memory of all of one run's instances together: bands run in parallel + /// only as far as their [`Limits::max_memory_bytes`] fit in it, so a run's peak memory does + /// not grow with the number of cores (#8603). + pub max_total_memory_bytes: usize, /// Target size of one band of `f32` pixels handed to the plug-in. pub band_bytes: usize, /// Instructions (fuel) any call may use, plus [`Limits::fuel_per_sample`] per sample for @@ -37,10 +41,12 @@ pub struct Limits { } impl Default for Limits { + /// The limits plug-ins run under unless a caller narrows them; see `docs/plugins.md`. fn default() -> Self { Self { max_module_bytes: 32 << 20, max_memory_bytes: 512 << 20, + max_total_memory_bytes: 2 << 30, band_bytes: 4 << 20, fuel_base: 50_000_000, fuel_per_sample: 4_000, @@ -50,6 +56,16 @@ impl Default for Limits { } } +impl Limits { + /// How many bands of one run may hold an instance at once on `threads` workers: no more + /// than [`Limits::max_memory_bytes`] fits into [`Limits::max_total_memory_bytes`], and always + /// at least one, so a tight budget runs bands one at a time instead of refusing the run. + pub fn parallel_bands(&self, threads: usize) -> usize { + let fit = self.max_total_memory_bytes.checked_div(self.max_memory_bytes).unwrap_or(threads); + threads.min(fit).max(1) + } +} + /// A loaded, validated plug-in. Cheap to share (`Arc`) and safe to run from several threads: /// every run gets its own store and instance. pub struct Plugin { @@ -115,6 +131,42 @@ fn wasm_err(e: wasmi::Error) -> Error { if e.as_trap_code() == Some(wasmi::TrapCode::OutOfFuel) { Error::Limit("instruction budget".into()) } else { Error::Trap(e.to_string()) } } +/// Reads an unsigned LEB128 `u32` at the start of `bytes`: the value and its length. +fn leb_u32(bytes: &[u8]) -> Option<(u32, usize)> { + let mut value = 0u32; + for (i, &b) in bytes.iter().take(5).enumerate() { + value |= u32::from(b & 0x7f).checked_shl(7 * i as u32)?; + if b & 0x80 == 0 { + return Some((value, i + 1)); + } + } + None +} + +/// Refuses a code section that declares no functions but more bytes than that count. The spec +/// makes the count the whole section then; wasmi 2.0's strict limits divide the section size by +/// the function count before validating it, so such a module (crafted or truncated) panicked. +/// Every code section is checked, since a duplicate one also reaches that division before +/// validation refuses it. Anything else, malformed or not, is left to wasmi. +fn check_code_section(bytes: &[u8]) -> Result<()> { + if bytes.get(4..8) != Some(&[1, 0, 0, 0][..]) { + return Ok(()); + } + let mut pos = 8usize; + while let Some(&id) = bytes.get(pos) { + let Some((size, len)) = bytes.get(pos + 1..).and_then(leb_u32) else { return Ok(()) }; + let start = pos + 1 + len; + if id == 10 + && let Some((0, count_len)) = bytes.get(start..).and_then(leb_u32) + && size as usize > count_len + { + return Err(Error::Module(format!("the code section declares no functions but {size} bytes"))); + } + pos = start.saturating_add(size as usize); + } + Ok(()) +} + impl Plugin { /// Validates and loads a module: checks the ABI version and reads the manifest. pub fn load(bytes: &[u8], limits: Limits) -> Result { @@ -124,6 +176,7 @@ impl Plugin { if !bytes.starts_with(b"\0asm") { return Err(Error::Module("not a WebAssembly binary (missing \\0asm header)".into())); } + check_code_section(bytes)?; let mut config = Config::default(); config.consume_fuel(true).enforced_limits(EnforcedLimits::strict()).set_max_recursion_depth(limits.max_recursion_depth.max(16)); let engine = Engine::new(&config); @@ -184,8 +237,9 @@ impl Plugin { let limits = StoreLimitsBuilder::new().memory_size(self.limits.max_memory_bytes).memories(1).tables(4).table_elements(100_000).instances(1).build(); let mut store = Store::new(&self.engine, limits); store.limiter(|l| l); - // The start function (if any) runs on this fuel. - store.set_fuel(self.limits.fuel_base).map_err(wasm_err)?; + // The start function (if any) runs on this fuel. It cannot be interrupted to check the + // deadline the way a call can, so it gets no more than one slice, however much a call may use. + store.set_fuel(self.limits.fuel_base.min(FUEL_SLICE)).map_err(wasm_err)?; let linker = Linker::::new(&self.engine); let instance = linker.instantiate_and_start(&mut store, &self.module).map_err(wasm_err)?; let memory = instance.get_memory(&store, "memory").ok_or_else(|| Error::Abi("the module must export its linear memory as `memory`".into()))?; diff --git a/crates/plugins/tests/abi.rs b/crates/plugins/tests/abi.rs index a75eedd88b..bb06069ac8 100644 --- a/crates/plugins/tests/abi.rs +++ b/crates/plugins/tests/abi.rs @@ -219,6 +219,25 @@ fn malformed_and_oversized_modules_are_refused() { assert!(matches!(e, Error::Abi(_)), "{e}"); } +#[test] +fn a_code_section_claiming_no_functions_but_many_bytes_is_refused() { + // Header, then a code section (id 10) declaring 2000 bytes whose function count is 0, cut off + // after the count (a truncated download) or padded to its full size. The spec makes the + // count's own byte the whole section; wasmi's strict limits divided the size by the count. + let truncated = b"\0asm\x01\0\0\0\x0a\xd0\x0f\x00".to_vec(); + let mut padded = truncated.clone(); + padded.resize(padded.len() + 1999, 0); + // The same section after a valid empty one: a duplicate reaches the division before validation. + let mut second = b"\0asm\x01\0\0\0\x0a\x01\x00".to_vec(); + second.extend_from_slice(&padded[8..]); + for (name, bytes) in [("truncated", truncated), ("padded", padded), ("second", second)] { + assert!(matches!(Plugin::load(&bytes, Limits::default()), Err(Error::Module(_))), "{name}"); + } + // An empty code section (count 0, nothing after it) is valid; it fails only for the missing ABI exports. + let empty = b"\0asm\x01\0\0\0\x0a\x01\x00"; + assert!(matches!(Plugin::load(empty, Limits::default()), Err(Error::Abi(_)))); +} + #[test] fn budgets_stop_runaway_plugins() { let spin = ok_module("(loop $l (br $l)) (i32.const 0)"); @@ -317,3 +336,81 @@ fn folder_loading_skips_bad_files() { assert!(photocraft_plugins::registry::load_file(&dir, Limits::default()).is_err(), "a directory is not a module"); std::fs::remove_dir_all(&dir).ok(); } + +/// #8603: a run whose memory budget holds a single instance still covers every band on a +/// many-worker pool, one band at a time, with the same pixels as a run of parallel bands. +#[test] +fn one_band_at_a_time_matches_parallel_bands() { + let pool = rayon::ThreadPoolBuilder::new().num_threads(8).build().unwrap(); + let s = surface(PixelFormat::RGBA8, 600, 300); + let canvas = Rect::new(0, 0, 600, 300); + let run = |limits: Limits| { + let p = Plugin::load(INVERT, limits).unwrap(); + pool.install(|| p.apply(&s, canvas, None, &json!({}))).unwrap() + }; + let parallel = run(Limits { band_bytes: 1, ..Limits::default() }); + let serial = run(Limits { band_bytes: 1, max_total_memory_bytes: 512 << 20, ..Limits::default() }); + assert_eq!(serial, parallel); + assert_ne!(serial, s, "the filter ran"); +} + +/// #8603: one run on 16 workers must not hold 16 instances at the 512 MiB cap (8 GiB). With the +/// default limits it holds at most four (2 GiB); a smaller per-instance cap lets every worker +/// run, and a budget under one cap still runs one band at a time rather than none. +#[test] +fn parallel_bands_fit_the_total_memory_budget() { + let d = Limits::default(); + assert_eq!(d.parallel_bands(16), 4); + assert!(d.parallel_bands(64) * d.max_memory_bytes <= d.max_total_memory_bytes); + assert_eq!(d.parallel_bands(2), 2, "fewer workers than the budget allows"); + let small = Limits { max_memory_bytes: 64 << 20, ..Limits::default() }; + assert_eq!(small.parallel_bands(16), 16, "small instances keep every worker busy"); + let tight = Limits { max_total_memory_bytes: 1 << 20, ..Limits::default() }; + assert_eq!(tight.parallel_bands(16), 1, "at least one band runs"); + assert_eq!(d.parallel_bands(0), 1); +} + +#[test] +fn a_start_function_that_spins_is_cut_off_at_one_slice() { + // `fuel_base` is what a call may use; the start function cannot be interrupted to check the + // clock, so it gets one slice of it at most. + let spin = "(func $spin (loop $l (br $l))) (start $spin)"; + let limits = Limits { fuel_base: u64::MAX / 4, ..Limits::default() }; + let (error, took) = run_err(&module(MANIFEST, 1, "(i32.const 0)", spin), limits); + assert!(matches!(error, Error::Trap(_) | Error::Limit(_)), "{error:?}"); + assert!(took < Duration::from_secs(30), "{took:?}"); +} + +#[test] +fn content_spread_over_millions_of_bands_is_refused_before_planning_them() { + let p = Plugin::load(INVERT, Limits::default()).unwrap(); + let mut s = Surface::new(PixelFormat::RGBA8); + s.write_pixel(0, 0, &[0.1, 0.2, 0.3, 1.0]); + s.write_pixel(4_000_000, 4_000_000, &[0.1, 0.2, 0.3, 1.0]); + let t = Instant::now(); + let e = p.apply(&s, Rect::new(0, 0, 8, 8), None, &json!({})).expect_err("a 15600 × 15600 tile grid is not one run"); + assert!(matches!(e, Error::Limit(_)), "{e:?}"); + assert!(t.elapsed() < Duration::from_secs(5), "{:?}", t.elapsed()); +} + +#[cfg(unix)] +#[test] +fn a_fifo_in_place_of_a_module_file_does_not_block_loading() { + let dir = std::env::temp_dir().join(format!("photocraft-plugin-fifo-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("evil.wasm"); + let _ = std::fs::remove_file(&path); + // `rustix::fs::mknodat` is unavailable on Apple targets. `mkfifo` is the + // POSIX utility for creating this special file and keeps this regression + // test available on macOS as well as other Unix platforms. + let status = std::process::Command::new("mkfifo").arg(&path).status().unwrap(); + assert!(status.success(), "mkfifo failed with {status}"); + let (tx, rx) = std::sync::mpsc::channel(); + let p = path.clone(); + std::thread::spawn(move || { + let _ = tx.send(photocraft_plugins::registry::load_file(&p, Limits::default()).map(|_| ())); + }); + let r = rx.recv_timeout(Duration::from_secs(10)).expect("loading a FIFO must not block"); + assert!(matches!(r, Err(Error::Io(_))), "{r:?}"); + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/psd/README.md b/crates/psd/README.md index d745fd5bd8..71d12e9f12 100644 --- a/crates/psd/README.md +++ b/crates/psd/README.md @@ -13,6 +13,7 @@ A standalone reader and writer for Adobe Photoshop **PSD** (version 1) and **PSB | `PsdFile::from_bytes(b)?.to_bytes()? == b` for unmodified files | Channel data keeps its original encoding. Unknown blocks and resources are stored raw. Exact padding is recorded wherever it can vary: tagged blocks, layer info, section trailers. | | `from_bytes(to_bytes(f)) == f` for any model `f` | Canonical defaults: `padding: None` means "zero-pad to even", and the parser yields `None` whenever the file used that default. | | Lazy decoding | The parser only copies encoded bytes. Channels are decoded when asked (`ChannelData::decode`, `ImageData::decode`). | +| Lazy reading | `PsdFile::from_source` reads only the structure. Channel data and the merged image stay in the source as `Stored` ranges and are read when decoded, so opening a 2 GB file doesn't need 2 GB for the file itself. The model equals what `from_bytes` builds, and it writes back the same bytes. | | No compositing | `PsdBuilder` takes the merged image from the caller. Without one, it writes a white placeholder and sets `has_real_merged_data = false` in resource 1057. | ## Public API (overview) @@ -22,6 +23,8 @@ A standalone reader and writer for Adobe Photoshop **PSD** (version 1) and **PSB PsdFile::from_bytes(&[u8]) -> Result PsdFile::from_bytes_and_merged(&[u8], want) -> Result<(PsdFile, Option>>)> // + decode_merged() when want(&file): ZIP inflated once +PsdFile::from_source(Source) -> Result // channel data and merged image left in the source +PsdFile::from_source_and_merged_recovering(Source, want, recover) PsdFile::to_bytes(&self) -> Result> PsdFile::open(path) / save(path) // native only PsdFile { header, color_mode_data, resources, layer_info, layer_info_placement, @@ -36,6 +39,11 @@ PsdFile::composite_rgba8() -> Result PsdFile::resource(id), global_block(key), icc_profile(), resolution(), has_real_merged_data(), merged_has_alpha(), validate() +// Sources (lazy reading) +trait ReadAt { len(), read_at(offset, buf) } // [u8], Vec, Arc, FileSource (pread / seek_read) +Source::new(impl ReadAt) / read_vec(offset, len) // shared handle; equal when the same source +Stored { source, offset, len }::read() / read_part(at, len) + // Header Header { version: Version::{Psd,Psb}, channels, width, height, depth, color_mode, reserved } ColorMode::{Bitmap, Grayscale, Indexed, Rgb, Cmyk, Multichannel, Duotone, Lab, Unknown(u16)} @@ -47,7 +55,8 @@ LayerRecord { rect, channels: Vec, blend_mode, opacity, clipping, f LayerRecord::name() // prefers `luni` LayerRecord::section_divider(), section_type(), layer_id(), fill_opacity(), is_visible(), block(key), block_mut(key), channel(id), channel_rect(id), decode_channel(id, depth, version) -ChannelData { id, compression: Option, data } // encoded bytes kept verbatim +ChannelData { id, compression: Option, data, stored: Option } // encoded bytes kept verbatim +ChannelData::new(id, compression, data); encoded() -> Cow<[u8]>; encoded_len(); load() ChannelData::encode(id, compression, decoded, w, h, depth, version) ChannelData::decode(w, h, depth, version); set_decoded(...) MaskData::{None, Mask(LayerMask), Raw(Vec)} @@ -106,6 +115,7 @@ testgen::{all_cases, merged_only, layered, small, sample_descriptor, pattern_pla | PSB 8-byte-length keys | ✓ | ✓ | – | LMsk Lr16 Lr32 Layr Mt16 Mt32 Mtrn Alph FMsk lnk2 FEid FXid PxSD | | Lr16 / Lr32 / Layr layer info | ✓ | ✓ | ✓ | lifted into `layer_info`; re-inserted at its original position | | luni lsct lsdk lyid lnsr clbl infx knko lspf lclr iOpa shmd | ✓ | ✓ | ✓ | `TaggedBlock::parsed()` | +| Layer XMP (`shmd` › `cust` › `layerXMP`) | ✓ | ✓ | ✓ | `metadata::layer_xmp` / `take_layer_xmp` / `put_layer_xmp`, laid out as Photoshop 2026 writes it | | ActionDescriptor (all OSTypes) | ✓ | ✓ | ✓ | including references, `ObAr` (best effort), `UnFl`, `Pth `, version-16 wrapper | | Layer tree (groups) | ✓ | ✓ | ✓ | open/closed folders plus bounding dividers; tolerant of malformed nesting | | RGBA8 extraction | ✓ | – | – | RGB / gray / CMYK layers; merged image also supports indexed / bitmap / duotone | @@ -144,7 +154,7 @@ allocations for padded channels return an error on failure. * Builder → `rgba8` correctness. * Unknown blend modes and unknown blocks pass through unchanged. -`tests/corpus.rs` walks `corpus/psd/**/*.{psd,psb}` at the workspace root when that directory exists. For each file it asserts that parsing succeeds and the file round-trips byte for byte, and it prints a result per file. Without the directory it skips silently. +`tests/it/corpus.rs` walks `corpus/psd/**/*.{psd,psb}` at the workspace root when that directory exists. For each file it asserts that parsing succeeds and the file round-trips byte for byte, and it prints a result per file. Without the directory it skips silently. A `cargo-fuzz` skeleton lives in `fuzz/`. It is its own workspace and is excluded from photocraft's. Run it with `cargo +nightly fuzz run parse` (or `descriptor`) from `crates/psd`. diff --git a/crates/psd/examples/atn_dump.rs b/crates/psd/examples/atn_dump.rs index 8a39bbb121..04e268da94 100644 --- a/crates/psd/examples/atn_dump.rs +++ b/crates/psd/examples/atn_dump.rs @@ -66,8 +66,8 @@ fn main() { for item in &a.items { let flags = format!("{}{}", if item.enabled { "" } else { " off" }, if item.with_dialog { " dialog" } else { "" }); match &item.descriptor { - Some(d) => println!(" {:<22} {:<28}{flags} {}", item.event.id(), item.dictionary, keys(d)), - None => println!(" {:<22} {:<28}{flags}", item.event.id(), item.dictionary), + Some(d) => println!(" {:<22} {:<28}{flags} {}", item.event.id(), item.label(), keys(d)), + None => println!(" {:<22} {:<28}{flags}", item.event.id(), item.label()), } } } diff --git a/crates/psd/examples/atn_roundtrip.rs b/crates/psd/examples/atn_roundtrip.rs new file mode 100644 index 0000000000..b73151b745 --- /dev/null +++ b/crates/psd/examples/atn_roundtrip.rs @@ -0,0 +1,54 @@ +//! Check `.atn` files for byte-exact round trips: parse, write, compare, and show the first +//! difference with its context: `cargo run -p photocraft-psd --example atn_roundtrip -- set.atn…` +//! +//! Exit status 1 when any file differs or fails to parse. + +use photocraft_psd::atn; + +fn hex(b: &[u8]) -> String { + b.iter().map(|x| format!("{x:02x}")).collect::>().join(" ") +} + +fn main() { + let mut failed = false; + for path in std::env::args().skip(1) { + let bytes = match std::fs::read(&path) { + Ok(b) => b, + Err(e) => { + eprintln!("{path}: {e}"); + failed = true; + continue; + } + }; + let file = match atn::parse(&bytes) { + Ok(f) => f, + Err(e) => { + println!("{path}: parse failed: {e}"); + failed = true; + continue; + } + }; + let written = match atn::write(&file) { + Ok(w) => w, + Err(e) => { + println!("{path}: write failed: {e}"); + failed = true; + continue; + } + }; + if written == bytes { + println!("{path}: identical ({} bytes)", bytes.len()); + continue; + } + failed = true; + let at = bytes.iter().zip(&written).position(|(a, b)| a != b).unwrap_or(bytes.len().min(written.len())); + let lo = at.saturating_sub(24); + println!("{path}: differs at byte {at} (original {} bytes, rewritten {} bytes)", bytes.len(), written.len()); + println!(" original : {}", hex(bytes.get(lo..(at + 40).min(bytes.len())).unwrap_or(&[]))); + println!(" rewritten: {}", hex(written.get(lo..(at + 40).min(written.len())).unwrap_or(&[]))); + println!(" original : {}", String::from_utf8_lossy(bytes.get(lo..(at + 40).min(bytes.len())).unwrap_or(&[])).replace(|c: char| c.is_control(), ".")); + } + if failed { + std::process::exit(1); + } +} diff --git a/crates/psd/examples/bench_layer_block.rs b/crates/psd/examples/bench_layer_block.rs index 4d34f9b8f3..4e48f4b3dc 100644 --- a/crates/psd/examples/bench_layer_block.rs +++ b/crates/psd/examples/bench_layer_block.rs @@ -1,6 +1,6 @@ //! Serialization of one 24 MP raw RGB layer in Lr16/Lr32, without compression or disk I/O. //! Run with `cargo run --release -p photocraft-psd --example bench_layer_block`. -use photocraft_psd::*; +use photocraft_psd::{ChannelData, ColorMode, Compression, Header, ImageData, LayerInfo, LayerInfoPlacement, LayerRecord, Rect, Result, Version, testgen}; use std::hint::black_box; use std::time::Instant; @@ -11,12 +11,12 @@ fn main() -> Result<()> { let plane_bytes = 6000 * 4000 * usize::from(depth / 8); let mut layer = LayerRecord { rect: Rect::from_xywh(0, 0, 6000, 4000), name: b"RGB".to_vec(), ..Default::default() }; for id in 0..3 { - layer.channels.push(ChannelData { id, compression: Some(Compression::Raw), data: vec![0; plane_bytes] }); + layer.channels.push(ChannelData::new(id, Some(Compression::Raw), vec![0; plane_bytes])); } file.layer_info = Some(LayerInfo { merged_alpha: false, layers: vec![layer], padding: None }); file.layer_info_placement = LayerInfoPlacement::GlobalBlock { index: 0, signature: *b"8BIM", key: if depth == 16 { *b"Lr16" } else { *b"Lr32" }, padding: None }; - file.image_data = ImageData { compression: Compression::Raw, data: vec![0; plane_bytes * 3] }; + file.image_data = ImageData::new(Compression::Raw, vec![0; plane_bytes * 3]); black_box(file.to_bytes()?); let mut samples = Vec::new(); let mut size = 0; diff --git a/crates/psd/examples/big_psb.rs b/crates/psd/examples/big_psb.rs index 332936c1e5..23bf4de776 100644 --- a/crates/psd/examples/big_psb.rs +++ b/crates/psd/examples/big_psb.rs @@ -1,186 +1,24 @@ -//! Writes multi-gigabyte PSB files for large-file tests (#375, #216), straight from the Adobe -//! PSD spec and independent of this crate's writer, so the fixtures don't inherit its limits. +//! Writes multi-gigabyte PSB files (and the 2 GB PSD) for large-file tests (#375, #216), straight +//! from the Adobe PSD spec and independent of this crate's writer, so the fixtures don't inherit +//! its limits. The generator is `photocraft_psd::fixtures` (feature `testgen`). //! //! `cargo run --release -p photocraft-psd --example big_psb -- [case...]` //! -//! Every sample follows [`sample`], so a reader can check any pixel without the file. RLE cases -//! keep each row constant (tiny files, full decoded size); raw cases vary along x too. +//! Every sample follows `fixtures::sample`, so a reader can check any pixel without the file. RLE +//! cases keep each row constant (tiny files, full decoded size); raw cases vary along x too. -use std::fs::File; -use std::io::{self, BufWriter, Write}; -use std::path::Path; +use photocraft_psd::fixtures::{PSB_CASES, PSD_2GB}; -/// One generated file. -struct Case { - name: &'static str, - width: u32, - height: u32, - depth: u16, - /// Raw (0) or RLE (1). - rle: bool, - /// One full-canvas RGBA layer, or a flattened file (composite only). - layered: bool, -} - -const CASES: &[Case] = &[ - // Composite of 2.52 GB in one decode. - Case { name: "flat8-29k-rle", width: 29_000, height: 29_000, depth: 8, rle: true, layered: false }, - // A 4.8 GB file: offsets past 4 GiB, composite of 4.8 GB. - Case { name: "flat8-40k-raw", width: 40_000, height: 40_000, depth: 8, rle: false, layered: false }, - // 4.38 GB file: layer channels of 625 MB, composite of 1.9 GB, data past 4 GiB. - Case { name: "layer8-25k-raw", width: 25_000, height: 25_000, depth: 8, rle: false, layered: true }, - // Layer channels of 900 MB, composite of 2.7 GB. - Case { name: "layer8-30k-rle", width: 30_000, height: 30_000, depth: 8, rle: true, layered: true }, - // Layer channels of 2.18 GB each. - Case { name: "layer16-33k-rle", width: 33_000, height: 33_000, depth: 16, rle: true, layered: true }, -]; - -/// The 8-bit sample of channel `c` (0..3 colour, 3 = alpha) at (`x`, `y`). RLE files ignore `x`. -/// 16-bit files store `v * 257`. -pub fn sample(x: u32, y: u32, c: u32, rle: bool) -> u8 { - if c == 3 { - return 255; - } - let x = if rle { 0 } else { x }; - (x.wrapping_mul(7).wrapping_add(y.wrapping_mul(13)).wrapping_add(c.wrapping_mul(101)) & 0xff) as u8 -} - -fn main() -> io::Result<()> { +fn main() -> std::io::Result<()> { let args: Vec = std::env::args().skip(1).collect(); let out = args.first().map_or_else(|| std::env::temp_dir().join("photocraft-big-psb"), std::path::PathBuf::from); std::fs::create_dir_all(&out)?; let wanted = args.get(1..).unwrap_or(&[]); - for case in CASES.iter().filter(|c| wanted.is_empty() || wanted.iter().any(|w| w == c.name)) { - let path = out.join(format!("{}.psb", case.name)); + for case in PSB_CASES.iter().chain([&PSD_2GB]).filter(|c| wanted.is_empty() || wanted.iter().any(|w| w == c.name)) { + let path = out.join(case.file_name()); let t = std::time::Instant::now(); - write_case(case, &path)?; - let len = std::fs::metadata(&path)?.len(); + let len = case.write_file(&path)?; println!("{}: {:.2} GB in {:.1} s", path.display(), len as f64 / 1e9, t.elapsed().as_secs_f64()); } Ok(()) } - -fn row_bytes(c: &Case) -> u64 { - u64::from(c.width) * u64::from(c.depth / 8) -} - -/// PackBits for a row of `n` equal bytes: runs of 128, then the remainder. -fn rle_row(n: u64, b: u8) -> Vec { - let mut v = Vec::new(); - let mut left = n; - while left > 0 { - let run = left.min(128); - if run == 1 { - v.extend_from_slice(&[0, b]); - } else { - v.extend_from_slice(&[(257 - run) as u8, b]); - } - left -= run; - } - v -} - -/// The bytes of one row of channel `ch`. -fn row(c: &Case, y: u32, ch: u32, buf: &mut Vec) { - buf.clear(); - if c.rle { - buf.extend(rle_row(row_bytes(c), sample(0, y, ch, true))); - return; - } - for x in 0..c.width { - let s = sample(x, y, ch, false); - buf.push(s); - if c.depth == 16 { - buf.push(s); - } - } -} - -/// Size of a channel block (compression word, RLE counts, rows) of `planes` planes. -fn channel_len(c: &Case, planes: u64) -> u64 { - let rows = planes * u64::from(c.height); - if c.rle { 2 + rows * 4 + rows * rle_row(row_bytes(c), 0).len() as u64 } else { 2 + rows * row_bytes(c) } -} - -/// Writes the compression word, the counts and the rows of `chans`. -fn write_planes(w: &mut impl Write, c: &Case, chans: &[u32]) -> io::Result<()> { - w.write_all(&u16::from(c.rle).to_be_bytes())?; - if c.rle { - let n = rle_row(row_bytes(c), 0).len() as u32; - for _ in 0..chans.len() as u64 * u64::from(c.height) { - w.write_all(&n.to_be_bytes())?; - } - } - let mut buf = Vec::new(); - for &ch in chans { - for y in 0..c.height { - row(c, y, ch, &mut buf); - w.write_all(&buf)?; - } - } - Ok(()) -} - -fn write_case(c: &Case, path: &Path) -> io::Result<()> { - let mut w = BufWriter::with_capacity(8 << 20, File::create(path)?); - // Header: PSB (version 2), 3 channels, RGB. - w.write_all(b"8BPS")?; - w.write_all(&2u16.to_be_bytes())?; - w.write_all(&[0; 6])?; - w.write_all(&3u16.to_be_bytes())?; - w.write_all(&c.height.to_be_bytes())?; - w.write_all(&c.width.to_be_bytes())?; - w.write_all(&c.depth.to_be_bytes())?; - w.write_all(&3u16.to_be_bytes())?; - // Colour mode data and image resources: empty. - w.write_all(&0u32.to_be_bytes())?; - w.write_all(&0u32.to_be_bytes())?; - - if c.layered { - // Transparency (-1), then R, G, B: one block per channel. - let ids: [(i16, u32); 4] = [(-1, 3), (0, 0), (1, 1), (2, 2)]; - let ch_len = channel_len(c, 1); - let name = b"Layer 1"; - let mut rec = Vec::new(); - for v in [0i32, 0, c.height as i32, c.width as i32] { - rec.extend_from_slice(&v.to_be_bytes()); - } - rec.extend_from_slice(&4u16.to_be_bytes()); - for (id, _) in ids { - rec.extend_from_slice(&id.to_be_bytes()); - rec.extend_from_slice(&ch_len.to_be_bytes()); - } - rec.extend_from_slice(b"8BIMnorm"); - rec.extend_from_slice(&[255, 0, 0, 0]); - // Extra data: no mask, no blending ranges, the Pascal name padded to 4. - let mut extra = Vec::new(); - extra.extend_from_slice(&0u32.to_be_bytes()); - extra.extend_from_slice(&0u32.to_be_bytes()); - extra.push(name.len() as u8); - extra.extend_from_slice(name); - while extra.len() % 4 != 0 { - extra.push(0); - } - rec.extend_from_slice(&(extra.len() as u32).to_be_bytes()); - rec.extend_from_slice(&extra); - - let mut layer_info = 2 + rec.len() as u64 + 4 * ch_len; - let pad = layer_info % 2; - layer_info += pad; - // Layer and mask information: layer info (8-byte length) + empty global mask (4 bytes). - w.write_all(&(8 + layer_info + 4).to_be_bytes())?; - w.write_all(&layer_info.to_be_bytes())?; - w.write_all(&1i16.to_be_bytes())?; - w.write_all(&rec)?; - for (_, ch) in ids { - write_planes(&mut w, c, &[ch])?; - } - w.write_all(&vec![0; pad as usize])?; - w.write_all(&0u32.to_be_bytes())?; - } else { - w.write_all(&0u64.to_be_bytes())?; - } - // The merged image. - write_planes(&mut w, c, &[0, 1, 2])?; - w.flush() -} diff --git a/crates/psd/fuzz/Cargo.toml b/crates/psd/fuzz/Cargo.toml index 1c874bf4f0..8682bbb725 100644 --- a/crates/psd/fuzz/Cargo.toml +++ b/crates/psd/fuzz/Cargo.toml @@ -36,6 +36,13 @@ test = false doc = false bench = false +[[bin]] +name = "asl" +path = "fuzz_targets/asl.rs" +test = false +doc = false +bench = false + [[bin]] name = "image_source_data" path = "fuzz_targets/image_source_data.rs" diff --git a/crates/psd/fuzz/fuzz_targets/asl.rs b/crates/psd/fuzz/fuzz_targets/asl.rs new file mode 100644 index 0000000000..cbf2c9169c --- /dev/null +++ b/crates/psd/fuzz/fuzz_targets/asl.rs @@ -0,0 +1,15 @@ +//! Fuzz the `.asl` style library parser: any input must return `Ok` or `Err`, never panic, and +//! anything that parses must write back and parse to the same number of styles. +#![no_main] + +use libfuzzer_sys::fuzz_target; +use photocraft_psd::asl; + +fuzz_target!(|data: &[u8]| { + if let Ok(f) = asl::parse(data) + && let Ok(bytes) = asl::write(&f) + { + let back = asl::parse(&bytes).expect("a written library parses"); + assert_eq!(back.styles.len(), f.styles.len()); + } +}); diff --git a/crates/psd/fuzz/fuzz_targets/grd.rs b/crates/psd/fuzz/fuzz_targets/grd.rs index 2779a96846..16ab26a137 100644 --- a/crates/psd/fuzz/fuzz_targets/grd.rs +++ b/crates/psd/fuzz/fuzz_targets/grd.rs @@ -3,7 +3,7 @@ //! //! The cap assertions below are invariants, not fast checks: at libFuzzer default `-max_len` //! of 4096 no input can carry 10 001 gradients or 1001 stops, so they only fire on long runs with -//! a raised limit. The inputs that do reach those caps are in `crates/psd/tests/swatch_malformed.rs`. +//! a raised limit. The inputs that do reach those caps are in `crates/psd/tests/it/swatch_malformed.rs`. #![no_main] use libfuzzer_sys::fuzz_target; use photocraft_psd::grd::{self, GrdColor}; diff --git a/crates/psd/src/abr.rs b/crates/psd/src/abr.rs index c7cf3eb3cc..35f4135a53 100644 --- a/crates/psd/src/abr.rs +++ b/crates/psd/src/abr.rs @@ -43,8 +43,11 @@ use crate::patterns::{PatternBudget, PsdPattern, parse_pattern_block_with, write pub const MAX_EDGE: u32 = 8192; /// Most brushes read from one file. pub const MAX_BRUSHES: usize = 20_000; -/// Cap on decoded sample bytes over the whole file. -pub const MAX_TOTAL_BYTES: usize = 512 << 20; +/// Cap on decoded sample bytes over the whole file. 32-bit targets (wasm) keep 512 MiB: the file, +/// its base64 copies and the decoded tips must all fit a 4 GiB address space, and running out +/// there aborts instead of returning an error. +pub const MAX_TOTAL_BYTES: usize = if cfg!(target_pointer_width = "64") { 1 << 30 } else { 512 << 20 }; +const _: () = assert!(cfg!(target_pointer_width = "64") || MAX_TOTAL_BYTES == 512 << 20); /// A sampled tip: one grayscale plane where the maximum value is full paint. #[derive(Debug, Clone, PartialEq)] @@ -432,6 +435,7 @@ fn parse_v6_within(r: &mut Rd, out: &mut AbrFile, budget: &mut Budget) -> Result return Err(PsdError::Unsupported(format!("brush file subversion {}", out.subversion))); } let mut hierarchy = None; + let mut patterns = PatternBudget::default(); while r.left() >= 12 { let sig = r.arr::<4>()?; if &sig != b"8BIM" { @@ -455,16 +459,17 @@ fn parse_v6_within(r: &mut Rd, out: &mut AbrFile, budget: &mut Budget) -> Result match &key { b"samp" => read_samp(data, out.subversion, out, budget)?, b"patt" => { - // Patterns share the samples' budget, so neither can claim it twice. + // Patterns share the samples' budget and one padding budget across every `patt` + // section, so neither can be claimed twice and failed blocks still deduct their + // decoded charges. let limit = u64::try_from(budget.0).unwrap_or(u64::MAX); - let mut patterns = PatternBudget::with_decoded_limit(limit); + patterns.cap_decoded_to(limit); let left = patterns.decoded_left(); - match parse_pattern_block_with(data, &mut patterns) { - Ok(p) => { - let spent = usize::try_from(left.saturating_sub(patterns.decoded_left())).unwrap_or(usize::MAX); - budget.spend(spent)?; - out.patterns.extend(p); - } + let res = parse_pattern_block_with(data, &mut patterns); + let spent = usize::try_from(left.saturating_sub(patterns.decoded_left())).unwrap_or(usize::MAX); + budget.spend(spent)?; + match res { + Ok(p) => out.patterns.extend(p), Err(e) => out.warnings.push(format!("embedded patterns unreadable: {e}")), } } @@ -668,274 +673,4 @@ pub fn write_v6_folders( } #[cfg(test)] -mod tests { - use super::*; - use crate::descriptor::UnicodeString; - - fn sample(id: &str, w: u32, h: u32, depth: u16) -> AbrSample { - let bpp = usize::from(depth / 8); - let data = (0..w as usize * h as usize * bpp).map(|i| if (i / bpp) % 3 == 0 { 255 } else { (i % 7) as u8 * 30 }).collect(); - AbrSample { id: id.into(), width: w, height: h, depth, data } - } - - #[test] - fn embedded_patterns_spend_the_samples_budget() { - let p = PsdPattern { - mode: 3, - width: 5, - height: 3, - name: "p".into(), - id: "id".into(), - palette: None, - depth: 8, - channels: vec![vec![1; 15], vec![2; 15], vec![3; 15]], - alpha: None, - }; - let abr = write_v6(2, &[sample("s", 10, 10, 8)], &[p.clone(), p], &[], true).unwrap(); - let parse_within = |left: usize| { - let mut out = AbrFile::default(); - let mut budget = Budget(left); - parse_v6_within(&mut Rd::new(&abr[2..]), &mut out, &mut budget).unwrap(); - (out, budget.0) - }; - // 100 sample bytes plus two 45-byte patterns. - let (out, left) = parse_within(190); - assert_eq!((out.samples.len(), out.patterns.len(), left), (1, 2, 0)); - // One byte short: the patterns no longer fit after the sample (they used not to count). - let (out, left) = parse_within(189); - assert_eq!((out.samples.len(), out.patterns.len(), left), (1, 0, 89)); - assert!(out.warnings.iter().any(|w| w.contains("embedded patterns unreadable")), "{:?}", out.warnings); - } - - #[test] - fn v1_v2_round_trip_raw_and_rle() { - for version in [1u16, 2] { - for rle in [false, true] { - let brushes = vec![ - LegacyBrush { - name: String::new(), - spacing: 25, - anti_alias: true, - tip: LegacyTip::Computed { diameter: 19, hardness: 80, angle: -30, roundness: 50 }, - }, - LegacyBrush { - name: if version == 2 { "Leaf ✓".into() } else { String::new() }, - spacing: 40, - anti_alias: false, - tip: LegacyTip::Sampled(sample("", 13, 9, 8)), - }, - LegacyBrush { name: String::new(), spacing: 10, anti_alias: true, tip: LegacyTip::Sampled(sample("", 4, 6, 16)) }, - ]; - let bytes = write_v12(version, &brushes, rle).unwrap(); - let f = parse(&bytes).unwrap(); - assert_eq!(f.version, version); - assert_eq!(f.legacy, brushes, "v{version} rle={rle}"); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - } - } - } - - #[test] - fn v6_round_trip_both_subversions() { - let samples = vec![sample("$abc", 20, 11, 8), sample("$def", 5, 5, 16)]; - let preset = Descriptor::new("brushPreset") - .with("Nm ", Value::Text(UnicodeString::new_nul("Grass"))) - .with("Brsh", Value::Descriptor(Descriptor::new("sampledBrush").with("sampledData", Value::Text(UnicodeString::new_nul("$abc"))))); - for sub in [1u16, 2] { - for rle in [false, true] { - let bytes = write_v6(sub, &samples, &[], std::slice::from_ref(&preset), rle).unwrap(); - let f = parse(&bytes).unwrap(); - assert_eq!((f.version, f.subversion), (6, sub)); - assert_eq!(f.samples, samples); - assert_eq!(f.presets, vec![preset.clone()]); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - } - } - assert_eq!(samples[1].to_unit().len(), 25); - } - - #[test] - fn hostile_input_errors_without_panicking() { - assert!(parse(&[]).is_err()); - assert!(parse(&[0, 3, 0, 0]).is_err()); - assert!(parse(&[0, 6, 0, 9]).is_err()); - // Absurd sample bounds. - let mut s = sample("", 2, 2, 8); - s.width = MAX_EDGE + 1; - assert!(write_v12(1, &[LegacyBrush { name: String::new(), spacing: 1, anti_alias: true, tip: LegacyTip::Sampled(s) }], false).is_err()); - let good = write_v6(2, &[sample("$a", 30, 30, 8)], &[], &[Descriptor::new("brushPreset")], true).unwrap(); - for cut in 0..good.len() { - let _ = parse(&good[..cut]); - } - let v2 = write_v12(2, &[LegacyBrush { name: "x".into(), spacing: 1, anti_alias: true, tip: LegacyTip::Sampled(sample("", 9, 9, 16)) }], true).unwrap(); - for cut in 0..v2.len() { - let _ = parse(&v2[..cut]); - } - // Flip every byte once: never panics. - for i in 0..good.len() { - let mut b = good.clone(); - b[i] ^= 0xa5; - let _ = parse(&b); - } - // A huge declared count with no data is an error, not an allocation. - assert!(parse(&[0, 2, 0xff, 0xff]).is_err()); - } - - fn named(n: &str) -> Descriptor { - Descriptor::new("brushPreset").with("Nm ", Value::Text(UnicodeString::new_nul(n))) - } - - /// Hierarchy tokens: `G(name)` opens, `P` is a preset, `E` closes. - enum Tok<'a> { - G(&'a str), - P, - E, - } - - /// A v6 file with `n` presets and a hand-made `phry` token stream. - fn with_phry(n: usize, toks: &[Tok]) -> Vec { - let presets: Vec = (0..n).map(|i| named(&format!("B{i}"))).collect(); - let mut bytes = write_v6(2, &[], &[], &presets, false).unwrap(); - section(&mut bytes, b"phry", &phry_bytes(toks)); - bytes - } - - /// A `phry` section body holding `toks`. - fn phry_bytes(toks: &[Tok]) -> Vec { - let items = toks - .iter() - .map(|t| { - Value::Descriptor(match t { - Tok::G(name) => Descriptor::new("Grup") - .with("Nm ", Value::Text(UnicodeString::new_nul(name))) - .with("zuid", Value::Text(UnicodeString::new_nul("c9ee8fd5-eb7d-924c-9e95-ddc98e40bb6c"))), - Tok::P => Descriptor::new("preset"), - Tok::E => Descriptor::new("groupEnd"), - }) - }) - .collect(); - VersionedDescriptor::new(Descriptor::new("null").with("hierarchy", Value::List(items))).to_bytes() - } - - fn path(p: &[&str]) -> Vec { - p.iter().map(|s| s.to_string()).collect() - } - - #[test] - fn phry_flat_folders_like_photoshop_25() { - // The layout of Photoshop 25.1's default brushes: four folders, every preset in one. - use Tok::*; - let f = parse(&with_phry(5, &[G("General"), P, P, E, G("Dry Media"), P, E, G("Wet Media"), P, E, G("Special Effects"), P, E])).unwrap(); - assert_eq!(f.folders, vec![path(&["General"]), path(&["General"]), path(&["Dry Media"]), path(&["Wet Media"]), path(&["Special Effects"])]); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - } - - #[test] - fn phry_nested_folders_and_top_level_presets() { - use Tok::*; - let f = parse(&with_phry(5, &[P, G("Animals"), G("Birds"), P, E, P, G("Fish"), P, E, E, P])).unwrap(); - assert_eq!(f.folders, vec![path(&[]), path(&["Animals", "Birds"]), path(&["Animals"]), path(&["Animals", "Fish"]), path(&[])]); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - // No folder at all: no hierarchy. - let f = parse(&with_phry(2, &[P, P])).unwrap(); - assert!(f.folders.is_empty()); - } - - #[test] - fn phry_mismatches_become_warnings() { - use Tok::*; - // A stray groupEnd is ignored; an unclosed folder still holds its presets. - let f = parse(&with_phry(2, &[E, G("A"), P, E, E, G("B"), P])).unwrap(); - assert_eq!(f.folders, vec![path(&["A"]), path(&["B"])]); - assert!(f.warnings.iter().any(|w| w.contains("folder end")), "{:?}", f.warnings); - // Fewer preset tokens than presets: the rest are at the top level. - let f = parse(&with_phry(3, &[G("A"), P, E])).unwrap(); - assert_eq!(f.folders, vec![path(&["A"]), path(&[]), path(&[])]); - assert!(f.warnings.iter().any(|w| w.contains("list 1 brushes but the file holds 3")), "{:?}", f.warnings); - // More preset tokens than presets: extras ignored. - let f = parse(&with_phry(1, &[G("A"), P, P, P, E])).unwrap(); - assert_eq!(f.folders, vec![path(&["A"])]); - assert_eq!(f.presets.len(), 1); - assert!(!f.warnings.is_empty()); - // A depth bomb: folders deeper than the limit merge into their ancestor at the limit. - let mut toks: Vec = (0..10_000).map(|_| G("deep")).collect(); - toks.push(P); - toks.extend((0..10_000).map(|_| E)); - toks.extend([G("next"), P, E]); - let f = parse(&with_phry(2, &toks)).unwrap(); - assert_eq!(f.folders[0].len(), MAX_FOLDER_DEPTH); - assert_eq!(f.folders[1], path(&["next"])); - assert!(f.warnings.iter().any(|w| w.contains("deeper")), "{:?}", f.warnings); - // An unreadable hierarchy: imported flat, with a warning. - let mut bytes = write_v6(2, &[], &[], &[named("x")], false).unwrap(); - section(&mut bytes, b"phry", &[0, 0, 0, 16, 0xff, 0xff]); - let f = parse(&bytes).unwrap(); - assert!(f.folders.is_empty() && f.presets.len() == 1); - assert!(f.warnings.iter().any(|w| w.contains("phry")), "{:?}", f.warnings); - // Truncations and bit flips of a file with a hierarchy never panic. - let good = with_phry(3, &[G("A"), P, G("B"), P, E, E, P]); - for cut in 0..good.len() { - let _ = parse(&good[..cut]); - } - for i in 0..good.len() { - let mut b = good.clone(); - b[i] ^= 0x5a; - let _ = parse(&b); - } - } - - #[test] - fn phry_builds_paths_only_for_the_files_presets() { - use Tok::*; - // A few-KB section listing 100,000 deep presets for a file with 2: two paths are built. - let mut toks: Vec = (0..MAX_FOLDER_DEPTH).map(|_| G("folder")).collect(); - toks.extend((0..100_000).map(|_| P)); - let mut warnings = Vec::new(); - let folders = read_phry(&phry_bytes(&toks), 2, &mut warnings).unwrap(); - assert_eq!(folders.len(), 2); - assert_eq!(folders.capacity(), 2); - assert_eq!(folders[1].len(), MAX_FOLDER_DEPTH); - assert!(warnings.iter().any(|w| w.contains("list 100000 brushes but the file holds 2")), "{warnings:?}"); - // `phry` before `desc` still pairs with the file's presets. - let mut bytes = write_v6(2, &[], &[], &[named("a"), named("b")], false).unwrap(); - let at = bytes.windows(8).position(|w| w == b"8BIMdesc").unwrap(); - let mut phry = Vec::new(); - section(&mut phry, b"phry", &phry_bytes(&[G("A"), P, E, P])); - bytes.splice(at..at, phry); - let f = parse(&bytes).unwrap(); - assert_eq!(f.folders, vec![path(&["A"]), path(&[])]); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - } - - #[test] - fn phry_folder_paths_stay_within_their_byte_budget() { - use Tok::*; - // Thousands of presets under 32 folders of 255-character names would need ~27 MB of paths. - let long = "x".repeat(MAX_FOLDER_NAME); - let mut toks: Vec = (0..MAX_FOLDER_DEPTH).map(|_| G(&long)).collect(); - toks.extend((0..3_000).map(|_| P)); - let mut warnings = Vec::new(); - let folders = read_phry(&phry_bytes(&toks), 3_000, &mut warnings).unwrap(); - assert_eq!(folders.len(), 3_000); - let held: usize = folders.iter().flatten().map(|n| n.len() + size_of::()).sum(); - assert!(held <= MAX_FOLDER_BYTES, "{held}"); - assert_eq!(folders[0].len(), MAX_FOLDER_DEPTH); - assert!(folders[2_999].is_empty(), "past the budget: top level"); - assert!(warnings.iter().any(|w| w.contains("too large")), "{warnings:?}"); - } - - #[test] - fn phry_round_trips_through_the_writer() { - let presets: Vec = (0..6).map(|i| named(&format!("B{i}"))).collect(); - let folders = vec![path(&[]), path(&["Set", "Ink"]), path(&["Set", "Ink"]), path(&["Set"]), path(&["Other"]), path(&["Set", "Ink"])]; - let bytes = write_v6_folders(2, &[], &[], &presets, &folders, true).unwrap(); - let f = parse(&bytes).unwrap(); - assert_eq!(f.presets, presets); - assert_eq!(f.folders, folders); - assert!(f.warnings.is_empty(), "{:?}", f.warnings); - // No folders: no phry section, same bytes as write_v6. - assert_eq!(write_v6_folders(2, &[], &[], &presets, &vec![Vec::new(); 6], true).unwrap(), write_v6(2, &[], &[], &presets, true).unwrap()); - // Mismatched lengths are an error. - assert!(write_v6_folders(2, &[], &[], &presets, &folders[..2], true).is_err()); - } -} +mod tests; diff --git a/crates/psd/src/abr/tests.rs b/crates/psd/src/abr/tests.rs new file mode 100644 index 0000000000..29ed91ec6e --- /dev/null +++ b/crates/psd/src/abr/tests.rs @@ -0,0 +1,298 @@ +use super::*; +use crate::descriptor::UnicodeString; + +fn sample(id: &str, w: u32, h: u32, depth: u16) -> AbrSample { + let bpp = usize::from(depth / 8); + let data = (0..w as usize * h as usize * bpp).map(|i| if (i / bpp) % 3 == 0 { 255 } else { (i % 7) as u8 * 30 }).collect(); + AbrSample { id: id.into(), width: w, height: h, depth, data } +} + +#[test] +fn embedded_patterns_spend_the_samples_budget() { + let p = PsdPattern { + mode: 3, + width: 5, + height: 3, + name: "p".into(), + id: "id".into(), + palette: None, + depth: 8, + channels: vec![vec![1; 15], vec![2; 15], vec![3; 15]], + alpha: None, + }; + let abr = write_v6(2, &[sample("s", 10, 10, 8)], &[p.clone(), p], &[], true).unwrap(); + let parse_within = |left: usize| { + let mut out = AbrFile::default(); + let mut budget = Budget(left); + parse_v6_within(&mut Rd::new(&abr[2..]), &mut out, &mut budget).unwrap(); + (out, budget.0) + }; + // 100 sample bytes plus two 45-byte patterns. + let (out, left) = parse_within(190); + assert_eq!((out.samples.len(), out.patterns.len(), left), (1, 2, 0)); + // One byte short: the patterns no longer fit after the sample (they used not to count). + let (out, left) = parse_within(189); + assert_eq!((out.samples.len(), out.patterns.len(), left), (1, 0, 14)); + assert!(out.warnings.iter().any(|w| w.contains("embedded patterns unreadable")), "{:?}", out.warnings); +} + +#[test] +fn failed_pattern_section_does_not_refund_budget_to_later_sections() { + let p = PsdPattern { + mode: 3, + width: 5, + height: 3, + name: "p".into(), + id: "id".into(), + palette: None, + depth: 8, + channels: vec![vec![1; 15], vec![2; 15], vec![3; 15]], + alpha: None, + }; + let valid = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let mut malformed = valid.clone(); + malformed.extend_from_slice(&100u32.to_be_bytes()); + + let mut body = 2u16.to_be_bytes().to_vec(); + section(&mut body, b"patt", &malformed); + section(&mut body, b"patt", &valid); + let mut out = AbrFile::default(); + let mut budget = Budget(60); + parse_v6_within(&mut Rd::new(&body), &mut out, &mut budget).unwrap(); + + assert!(out.patterns.is_empty(), "a failed block is discarded atomically"); + assert_eq!(budget.0, 0, "the failed block's first 45 bytes and later partial 15-byte charge are retained"); + assert_eq!(out.warnings.iter().filter(|w| w.contains("embedded patterns unreadable")).count(), 2); +} + +#[test] +fn v1_v2_round_trip_raw_and_rle() { + for version in [1u16, 2] { + for rle in [false, true] { + let brushes = vec![ + LegacyBrush { + name: String::new(), + spacing: 25, + anti_alias: true, + tip: LegacyTip::Computed { diameter: 19, hardness: 80, angle: -30, roundness: 50 }, + }, + LegacyBrush { + name: if version == 2 { "Leaf ✓".into() } else { String::new() }, + spacing: 40, + anti_alias: false, + tip: LegacyTip::Sampled(sample("", 13, 9, 8)), + }, + LegacyBrush { name: String::new(), spacing: 10, anti_alias: true, tip: LegacyTip::Sampled(sample("", 4, 6, 16)) }, + ]; + let bytes = write_v12(version, &brushes, rle).unwrap(); + let f = parse(&bytes).unwrap(); + assert_eq!(f.version, version); + assert_eq!(f.legacy, brushes, "v{version} rle={rle}"); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + } + } +} + +#[test] +fn v6_round_trip_both_subversions() { + let samples = vec![sample("$abc", 20, 11, 8), sample("$def", 5, 5, 16)]; + let preset = Descriptor::new("brushPreset") + .with("Nm ", Value::Text(UnicodeString::new_nul("Grass"))) + .with("Brsh", Value::Descriptor(Descriptor::new("sampledBrush").with("sampledData", Value::Text(UnicodeString::new_nul("$abc"))))); + for sub in [1u16, 2] { + for rle in [false, true] { + let bytes = write_v6(sub, &samples, &[], std::slice::from_ref(&preset), rle).unwrap(); + let f = parse(&bytes).unwrap(); + assert_eq!((f.version, f.subversion), (6, sub)); + assert_eq!(f.samples, samples); + assert_eq!(f.presets, vec![preset.clone()]); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + } + } + assert_eq!(samples[1].to_unit().len(), 25); +} + +#[test] +fn hostile_input_errors_without_panicking() { + assert!(parse(&[]).is_err()); + assert!(parse(&[0, 3, 0, 0]).is_err()); + assert!(parse(&[0, 6, 0, 9]).is_err()); + // Absurd sample bounds. + let mut s = sample("", 2, 2, 8); + s.width = MAX_EDGE + 1; + assert!(write_v12(1, &[LegacyBrush { name: String::new(), spacing: 1, anti_alias: true, tip: LegacyTip::Sampled(s) }], false).is_err()); + let good = write_v6(2, &[sample("$a", 30, 30, 8)], &[], &[Descriptor::new("brushPreset")], true).unwrap(); + for cut in 0..good.len() { + let _ = parse(&good[..cut]); + } + let v2 = write_v12(2, &[LegacyBrush { name: "x".into(), spacing: 1, anti_alias: true, tip: LegacyTip::Sampled(sample("", 9, 9, 16)) }], true).unwrap(); + for cut in 0..v2.len() { + let _ = parse(&v2[..cut]); + } + // Flip every byte once: never panics. + for i in 0..good.len() { + let mut b = good.clone(); + b[i] ^= 0xa5; + let _ = parse(&b); + } + // A huge declared count with no data is an error, not an allocation. + assert!(parse(&[0, 2, 0xff, 0xff]).is_err()); +} + +fn named(n: &str) -> Descriptor { + Descriptor::new("brushPreset").with("Nm ", Value::Text(UnicodeString::new_nul(n))) +} + +/// Hierarchy tokens: `G(name)` opens, `P` is a preset, `E` closes. +enum Tok<'a> { + G(&'a str), + P, + E, +} + +/// A v6 file with `n` presets and a hand-made `phry` token stream. +fn with_phry(n: usize, toks: &[Tok]) -> Vec { + let presets: Vec = (0..n).map(|i| named(&format!("B{i}"))).collect(); + let mut bytes = write_v6(2, &[], &[], &presets, false).unwrap(); + section(&mut bytes, b"phry", &phry_bytes(toks)); + bytes +} + +/// A `phry` section body holding `toks`. +fn phry_bytes(toks: &[Tok]) -> Vec { + let items = toks + .iter() + .map(|t| { + Value::Descriptor(match t { + Tok::G(name) => Descriptor::new("Grup") + .with("Nm ", Value::Text(UnicodeString::new_nul(name))) + .with("zuid", Value::Text(UnicodeString::new_nul("c9ee8fd5-eb7d-924c-9e95-ddc98e40bb6c"))), + Tok::P => Descriptor::new("preset"), + Tok::E => Descriptor::new("groupEnd"), + }) + }) + .collect(); + VersionedDescriptor::new(Descriptor::new("null").with("hierarchy", Value::List(items))).to_bytes() +} + +fn path(p: &[&str]) -> Vec { + p.iter().map(|s| s.to_string()).collect() +} + +#[test] +fn phry_flat_folders_like_photoshop_25() { + // The layout of Photoshop 25.1's default brushes: four folders, every preset in one. + use Tok::*; + let f = parse(&with_phry(5, &[G("General"), P, P, E, G("Dry Media"), P, E, G("Wet Media"), P, E, G("Special Effects"), P, E])).unwrap(); + assert_eq!(f.folders, vec![path(&["General"]), path(&["General"]), path(&["Dry Media"]), path(&["Wet Media"]), path(&["Special Effects"])]); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); +} + +#[test] +fn phry_nested_folders_and_top_level_presets() { + use Tok::*; + let f = parse(&with_phry(5, &[P, G("Animals"), G("Birds"), P, E, P, G("Fish"), P, E, E, P])).unwrap(); + assert_eq!(f.folders, vec![path(&[]), path(&["Animals", "Birds"]), path(&["Animals"]), path(&["Animals", "Fish"]), path(&[])]); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + // No folder at all: no hierarchy. + let f = parse(&with_phry(2, &[P, P])).unwrap(); + assert!(f.folders.is_empty()); +} + +#[test] +fn phry_mismatches_become_warnings() { + use Tok::*; + // A stray groupEnd is ignored; an unclosed folder still holds its presets. + let f = parse(&with_phry(2, &[E, G("A"), P, E, E, G("B"), P])).unwrap(); + assert_eq!(f.folders, vec![path(&["A"]), path(&["B"])]); + assert!(f.warnings.iter().any(|w| w.contains("folder end")), "{:?}", f.warnings); + // Fewer preset tokens than presets: the rest are at the top level. + let f = parse(&with_phry(3, &[G("A"), P, E])).unwrap(); + assert_eq!(f.folders, vec![path(&["A"]), path(&[]), path(&[])]); + assert!(f.warnings.iter().any(|w| w.contains("list 1 brushes but the file holds 3")), "{:?}", f.warnings); + // More preset tokens than presets: extras ignored. + let f = parse(&with_phry(1, &[G("A"), P, P, P, E])).unwrap(); + assert_eq!(f.folders, vec![path(&["A"])]); + assert_eq!(f.presets.len(), 1); + assert!(!f.warnings.is_empty()); + // A depth bomb: folders deeper than the limit merge into their ancestor at the limit. + let mut toks: Vec = (0..10_000).map(|_| G("deep")).collect(); + toks.push(P); + toks.extend((0..10_000).map(|_| E)); + toks.extend([G("next"), P, E]); + let f = parse(&with_phry(2, &toks)).unwrap(); + assert_eq!(f.folders[0].len(), MAX_FOLDER_DEPTH); + assert_eq!(f.folders[1], path(&["next"])); + assert!(f.warnings.iter().any(|w| w.contains("deeper")), "{:?}", f.warnings); + // An unreadable hierarchy: imported flat, with a warning. + let mut bytes = write_v6(2, &[], &[], &[named("x")], false).unwrap(); + section(&mut bytes, b"phry", &[0, 0, 0, 16, 0xff, 0xff]); + let f = parse(&bytes).unwrap(); + assert!(f.folders.is_empty() && f.presets.len() == 1); + assert!(f.warnings.iter().any(|w| w.contains("phry")), "{:?}", f.warnings); + // Truncations and bit flips of a file with a hierarchy never panic. + let good = with_phry(3, &[G("A"), P, G("B"), P, E, E, P]); + for cut in 0..good.len() { + let _ = parse(&good[..cut]); + } + for i in 0..good.len() { + let mut b = good.clone(); + b[i] ^= 0x5a; + let _ = parse(&b); + } +} + +#[test] +fn phry_builds_paths_only_for_the_files_presets() { + use Tok::*; + // A few-KB section listing 100,000 deep presets for a file with 2: two paths are built. + let mut toks: Vec = (0..MAX_FOLDER_DEPTH).map(|_| G("folder")).collect(); + toks.extend((0..100_000).map(|_| P)); + let mut warnings = Vec::new(); + let folders = read_phry(&phry_bytes(&toks), 2, &mut warnings).unwrap(); + assert_eq!(folders.len(), 2); + assert_eq!(folders.capacity(), 2); + assert_eq!(folders[1].len(), MAX_FOLDER_DEPTH); + assert!(warnings.iter().any(|w| w.contains("list 100000 brushes but the file holds 2")), "{warnings:?}"); + // `phry` before `desc` still pairs with the file's presets. + let mut bytes = write_v6(2, &[], &[], &[named("a"), named("b")], false).unwrap(); + let at = bytes.windows(8).position(|w| w == b"8BIMdesc").unwrap(); + let mut phry = Vec::new(); + section(&mut phry, b"phry", &phry_bytes(&[G("A"), P, E, P])); + bytes.splice(at..at, phry); + let f = parse(&bytes).unwrap(); + assert_eq!(f.folders, vec![path(&["A"]), path(&[])]); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); +} + +#[test] +fn phry_folder_paths_stay_within_their_byte_budget() { + use Tok::*; + // Thousands of presets under 32 folders of 255-character names would need ~27 MB of paths. + let long = "x".repeat(MAX_FOLDER_NAME); + let mut toks: Vec = (0..MAX_FOLDER_DEPTH).map(|_| G(&long)).collect(); + toks.extend((0..3_000).map(|_| P)); + let mut warnings = Vec::new(); + let folders = read_phry(&phry_bytes(&toks), 3_000, &mut warnings).unwrap(); + assert_eq!(folders.len(), 3_000); + let held: usize = folders.iter().flatten().map(|n| n.len() + size_of::()).sum(); + assert!(held <= MAX_FOLDER_BYTES, "{held}"); + assert_eq!(folders[0].len(), MAX_FOLDER_DEPTH); + assert!(folders[2_999].is_empty(), "past the budget: top level"); + assert!(warnings.iter().any(|w| w.contains("too large")), "{warnings:?}"); +} + +#[test] +fn phry_round_trips_through_the_writer() { + let presets: Vec = (0..6).map(|i| named(&format!("B{i}"))).collect(); + let folders = vec![path(&[]), path(&["Set", "Ink"]), path(&["Set", "Ink"]), path(&["Set"]), path(&["Other"]), path(&["Set", "Ink"])]; + let bytes = write_v6_folders(2, &[], &[], &presets, &folders, true).unwrap(); + let f = parse(&bytes).unwrap(); + assert_eq!(f.presets, presets); + assert_eq!(f.folders, folders); + assert!(f.warnings.is_empty(), "{:?}", f.warnings); + // No folders: no phry section, same bytes as write_v6. + assert_eq!(write_v6_folders(2, &[], &[], &presets, &vec![Vec::new(); 6], true).unwrap(), write_v6(2, &[], &[], &presets, true).unwrap()); + // Mismatched lengths are an error. + assert!(write_v6_folders(2, &[], &[], &presets, &folders[..2], true).is_err()); +} diff --git a/crates/psd/src/aco.rs b/crates/psd/src/aco.rs index 11ff653caf..24f0b1c6f1 100644 --- a/crates/psd/src/aco.rs +++ b/crates/psd/src/aco.rs @@ -166,6 +166,36 @@ pub fn parse(data: &[u8]) -> Result { } } +/// How many bytes the `.aco` sections at the start of `data` span (what [`parse`] reads), or +/// `None` when they are truncated or the version is unknown. Tells a real `.aco` apart from +/// bytes that only start like one, such as an `.act` whose first colour is `0, 1, x`. +pub fn sections_len(data: &[u8]) -> Option { + fn skip_section(r: &mut Reader<'_>, named: bool) -> Result<()> { + let count = r.u16()?; + for _ in 0..count { + r.skip(10)?; + if named { + let n = u64::from(r.u32()?); + r.bytes_u64(n * 2)?; + } + } + Ok(()) + } + let mut r = Reader::new(data); + match r.u16().ok()? { + 1 => { + skip_section(&mut r, false).ok()?; + if r.remaining() >= 4 && r.peek_rest().get(..2) == Some(&[0, 2]) { + r.u16().ok()?; + skip_section(&mut r, true).ok()?; + } + } + 2 => skip_section(&mut r, true).ok()?, + _ => return None, + } + Some(r.pos()) +} + /// Write an `.aco` file: a version 1 section followed by a version 2 section with the names, /// as Photoshop writes them. Fails when there are more than [`MAX_SWATCHES`] swatches. pub fn write(swatches: &[AcoSwatch]) -> Result> { @@ -210,6 +240,18 @@ mod tests { ] } + #[test] + fn sections_len_is_what_parse_reads() { + let file = write(&sample()).unwrap(); + assert_eq!(sections_len(&file), Some(file.len())); + let mut padded = file.clone(); + padded.extend_from_slice(&[0; 6]); + assert_eq!(sections_len(&padded), Some(file.len()), "trailing bytes are not part of it"); + assert_eq!(sections_len(&file[..file.len() - 1]), None, "truncated"); + assert_eq!(sections_len(&[0, 3, 0, 0]), None, "unknown version"); + assert_eq!(sections_len(&[0, 1, 0, 0]), Some(4)); + } + #[test] fn round_trips_every_space_at_16_bits() { let s = sample(); diff --git a/crates/psd/src/act.rs b/crates/psd/src/act.rs new file mode 100644 index 0000000000..58e872bf97 --- /dev/null +++ b/crates/psd/src/act.rs @@ -0,0 +1,143 @@ +//! Photoshop Color Table files (`.act`): raw 8-bit RGB palettes. +//! +//! Layout from Adobe's public "Photoshop File Formats Specification" (Color Table): 256 RGB +//! triplets (768 bytes), optionally followed by a 4-byte trailer: a big-endian `u16` color +//! count ("the number of colors to use") and a big-endian `u16` transparent color index. +//! There are no names, no groups and no other color spaces. The trailer is kept verbatim so a +//! read → write round trip never loses it; interpreting it is the caller's job. + +use crate::error::{PsdError, Result}; +use crate::io::{Reader, WriteExt}; + +/// Colors in a `.act` palette. +pub const COLOR_COUNT: usize = 256; +/// Palette bytes: 256 RGB triplets. +pub const PALETTE_LEN: usize = COLOR_COUNT * 3; +/// Trailer bytes (color count + transparent index). +pub const TRAILER_LEN: usize = 4; + +/// A parsed `.act` file: the full 256-color palette plus the optional trailer, kept verbatim. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ActFile { + /// All 256 palette colors as RGB triplets. + pub colors: [[u8; 3]; COLOR_COUNT], + /// Trailer color count ("the number of colors to use"), if the file has one. + pub count: Option, + /// Trailer transparent color index, if the file has one. + pub transparent: Option, +} + +impl ActFile { + /// Colors to use: the trailer count clamped to the palette, else the whole palette. + pub fn used(&self) -> &[[u8; 3]] { + let n = self.count.map_or(COLOR_COUNT, |c| (c as usize).min(COLOR_COUNT)); + &self.colors[..n] + } + + /// The transparent color index, if the trailer names one inside the used colors. + pub fn transparent_index(&self) -> Option { + let t = usize::from(self.transparent?); + (t < self.used().len()).then_some(t) + } +} + +/// Parse an `.act` file. Anything but 768 bytes (palette only) or 772 bytes (plus the +/// trailer) is an error; never panics. +pub fn parse(data: &[u8]) -> Result { + if data.len() != PALETTE_LEN && data.len() != PALETTE_LEN + TRAILER_LEN { + return Err(PsdError::invalid("an .act file is 768 bytes, or 772 with the count trailer")); + } + let mut r = Reader::new(data); + let raw = r.bytes(PALETTE_LEN)?; + let mut colors = [[0u8; 3]; COLOR_COUNT]; + for (i, c) in colors.iter_mut().enumerate() { + let o = i * 3; + c.copy_from_slice(&raw[o..o + 3]); + } + let (count, transparent) = if r.remaining() >= TRAILER_LEN { (Some(r.u16()?), Some(r.u16()?)) } else { (None, None) }; + Ok(ActFile { colors, count, transparent }) +} + +/// Write an `.act` file: the 256-color palette plus the trailer when `count` or `transparent` is given. +/// Short palettes are padded with black; fails on a palette longer than 256 colors. +pub fn write(colors: &[[u8; 3]], count: Option, transparent: Option) -> Result> { + if colors.len() > COLOR_COUNT { + return Err(PsdError::LimitExceeded("an .act file holds at most 256 colors")); + } + let mut out = Vec::with_capacity(PALETTE_LEN + TRAILER_LEN); + for c in colors { + out.extend_from_slice(c); + } + out.resize(PALETTE_LEN, 0); + if count.is_some() || transparent.is_some() { + out.put_u16(count.unwrap_or(COLOR_COUNT as u16)); + out.put_u16(transparent.unwrap_or(0xFFFF)); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sample() -> [[u8; 3]; COLOR_COUNT] { + let mut colors = [[0u8; 3]; COLOR_COUNT]; + for (i, c) in colors.iter_mut().enumerate() { + let v = i as u8; + *c = [v, v.wrapping_mul(7), v.wrapping_add(13)]; + } + colors[0] = [255, 255, 255]; + colors + } + + #[test] + fn round_trips_with_and_without_the_trailer() { + let colors = sample(); + let bare = write(&colors, None, None).unwrap(); + assert_eq!(bare.len(), PALETTE_LEN); + let f = parse(&bare).unwrap(); + assert_eq!((f.count, f.transparent), (None, None)); + assert_eq!(f.used(), &colors); + assert_eq!(f.transparent_index(), None); + assert_eq!(write(&f.colors, f.count, f.transparent).unwrap(), bare, "byte-stable"); + + let trailer = write(&colors, Some(40), Some(7)).unwrap(); + assert_eq!(trailer.len(), PALETTE_LEN + TRAILER_LEN); + let f = parse(&trailer).unwrap(); + assert_eq!((f.count, f.transparent), (Some(40), Some(7))); + assert_eq!(f.used().len(), 40); + assert_eq!(f.transparent_index(), Some(7)); + assert_eq!(write(&f.colors, f.count, f.transparent).unwrap(), trailer, "byte-stable"); + } + + #[test] + fn short_palettes_pad_and_long_ones_fail() { + let few = vec![[1u8, 2, 3]; 3]; + let bytes = write(&few, None, None).unwrap(); + assert_eq!(bytes.len(), PALETTE_LEN); + assert_eq!(&bytes[..9], &[1u8, 2, 3, 1, 2, 3, 1, 2, 3]); + assert_eq!(&bytes[9..12], &[0u8, 0, 0]); + let many = vec![[0u8; 3]; COLOR_COUNT + 1]; + assert!(write(&many, None, None).is_err()); + } + + #[test] + fn hostile_lengths_are_errors_not_panics() { + let good = write(&sample(), Some(256), Some(255)).unwrap(); + assert_eq!(good.len(), PALETTE_LEN + TRAILER_LEN); + for n in 0..good.len() { + // Only the two exact lengths parse (768 reads as a trailer-less palette). + if n == PALETTE_LEN { + assert!(parse(&good[..n]).is_ok(), "{n}"); + } else { + assert!(parse(&good[..n]).is_err(), "{n}"); + } + } + assert!(parse(&[]).is_err()); + assert!(parse(&vec![0u8; PALETTE_LEN + TRAILER_LEN + 1]).is_err(), "trailing byte"); + // A count past the palette clamps; a transparent index past it is none. + let f = parse(&write(&sample(), Some(1000), Some(999)).unwrap()).unwrap(); + assert_eq!(f.used().len(), COLOR_COUNT); + assert_eq!(f.transparent_index(), None); + } +} diff --git a/crates/psd/src/ase.rs b/crates/psd/src/ase.rs index 7433c4797e..f63db5481a 100644 --- a/crates/psd/src/ase.rs +++ b/crates/psd/src/ase.rs @@ -22,6 +22,8 @@ use crate::io::{Reader, WriteExt}; pub const SIGNATURE: &[u8; 4] = b"ASEF"; /// Most colour entries read from one file. pub const MAX_SWATCHES: usize = 100_000; +/// Most groups read from one file. +pub const MAX_GROUPS: usize = 10_000; /// Longest name read or written, in UTF-16 code units. pub const MAX_NAME_UNITS: usize = 1024; // `put_name` writes the name length as `units.len() as u16 + 1`, so the cap must leave room in a @@ -172,9 +174,9 @@ fn read_color(body: &mut Reader<'_>) -> Result { } /// Parse an `.ase` file. A wrong signature or version, a truncated block, a block count beyond -/// the data or more than [`MAX_SWATCHES`] colours is an error; never panics. Unknown block -/// types are skipped, a group end without a start is ignored, and a group left open at the end -/// (or a group started inside another) is closed. +/// the data, more than [`MAX_SWATCHES`] colors or more than [`MAX_GROUPS`] groups is an error; +/// never panics. Unknown block types are skipped, a group end without a start is ignored, and a +/// group left open at the end (or a group started inside another) is closed. pub fn parse(data: &[u8]) -> Result { let mut r = Reader::new(data); let sig: [u8; 4] = r.array()?; @@ -192,12 +194,17 @@ pub fn parse(data: &[u8]) -> Result { let mut entries = Vec::new(); let mut open: Option<(String, Vec)> = None; let mut total = 0usize; + let mut groups = 0usize; for _ in 0..count { let kind = r.u16()?; let len = u64::from(r.u32()?); let mut body = r.sub(len)?; match kind { GROUP_START => { + groups += 1; + if groups > MAX_GROUPS { + return Err(PsdError::LimitExceeded("too many groups in an .ase file")); + } if let Some((name, swatches)) = open.take() { entries.push(AseEntry::Group { name, swatches }); } @@ -279,11 +286,15 @@ fn put_color(out: &mut Vec, s: &AseSwatch) -> Result<()> { } /// Write an `.ase` file (version 1.0). Fails only when a block or the block count overflows the -/// format's 32-bit fields or there are more than [`MAX_SWATCHES`] colours. +/// format's 32-bit fields or there are more than [`MAX_SWATCHES`] colors or more than +/// [`MAX_GROUPS`] groups. pub fn write(file: &AseFile) -> Result> { if file.swatch_count() > MAX_SWATCHES { return Err(PsdError::LimitExceeded("too many swatches for an .ase file")); } + if file.entries.iter().filter(|e| matches!(e, AseEntry::Group { .. })).count() > MAX_GROUPS { + return Err(PsdError::LimitExceeded("too many groups for an .ase file")); + } let mut body = Vec::with_capacity(64 * file.swatch_count().max(1)); let mut blocks = 0u64; for e in &file.entries { @@ -415,4 +426,14 @@ mod tests { let f = AseFile { entries: vec![AseEntry::Group { name: "g".into(), swatches: vec![s; MAX_SWATCHES + 1] }] }; assert!(write(&f).is_err()); } + + #[test] + fn too_many_groups() { + let g = AseEntry::Group { name: "g".into(), swatches: vec![] }; + let mut f = AseFile { entries: vec![g; MAX_GROUPS + 1] }; + assert!(write(&f).is_err()); + // At the limit the file is written, and every group reads back. + f.entries.pop(); + assert_eq!(parse(&write(&f).unwrap()).unwrap().entries.len(), MAX_GROUPS); + } } diff --git a/crates/psd/src/asl.rs b/crates/psd/src/asl.rs new file mode 100644 index 0000000000..26cd27b9bc --- /dev/null +++ b/crates/psd/src/asl.rs @@ -0,0 +1,276 @@ +//! Photoshop layer style libraries (`.asl`, the Styles panel's Import Styles… / Export Styles…). +//! +//! Layout of a version 3 style library: +//! +//! ```text +//! file := u16 2, "8BSL", u16 3 (sub-version), +//! u32 pattern section length, patterns (the `Patt` block record format), +//! u32 style count, style × count +//! style := u32 length, then that many bytes: identity, style descriptor, zero padding +//! identity:= versioned descriptor (class `null`): `Nm ` name, `Idnt` id (Unicode strings) +//! style descriptor := versioned descriptor (class `Styl`): `documentMode`, `Lefx` (the same +//! tree as a layer's `lfx2` block), optional `blendOptions` (`Md `, `Opct`, +//! `fillOpacity`) +//! ``` +//! +//! The reader skips up to three padding bytes after each style record whatever its length says, +//! so it reads files whose record length counts the padding and files whose length does not. +//! The writer counts the padding in the record length, which reads correctly either way. +//! +//! Malformed input is an error, never a panic. A pattern section that doesn't decode is reported +//! in [`AslFile::pattern_error`] and skipped (its length is known), so the styles still load. + +use crate::descriptor::{Descriptor, UnicodeString, Value, VersionedDescriptor}; +use crate::error::{PsdError, Result}; +use crate::patterns::{PsdPattern, parse_pattern_block, write_pattern_block}; + +/// Most styles read from one file. +pub const MAX_STYLES: usize = 10_000; + +/// One style of a library. +#[derive(Debug, Clone, PartialEq)] +pub struct AslStyle { + /// The name shown in the Styles panel (may be empty or repeated). + pub name: String, + /// The style's unique id (usually a UUID). + pub id: String, + /// The style descriptor (class `Styl`): `Lefx`, `blendOptions`, `documentMode`. + pub style: Descriptor, +} + +/// A parsed style library. +#[derive(Debug, Clone, PartialEq, Default)] +pub struct AslFile { + /// Patterns embedded for the styles' Pattern Overlays, pattern strokes and bevel textures. + pub patterns: Vec, + /// Why the pattern section was skipped, if it didn't decode (read only). + pub pattern_error: Option, + /// Styles in file order. + pub styles: Vec, +} + +struct Rd<'a> { + b: &'a [u8], + p: usize, +} + +impl<'a> Rd<'a> { + fn take(&mut self, n: usize) -> Result<&'a [u8]> { + let end = self.p.checked_add(n).filter(|&e| e <= self.b.len()).ok_or(PsdError::UnexpectedEof { offset: self.p, needed: n })?; + let s = self.b.get(self.p..end).ok_or(PsdError::UnexpectedEof { offset: self.p, needed: n })?; + self.p = end; + Ok(s) + } + fn u16(&mut self) -> Result { + let b = self.take(2)?; + Ok(u16::from_be_bytes([b[0], b[1]])) + } + fn u32(&mut self) -> Result { + let b = self.take(4)?; + Ok(u32::from_be_bytes([b[0], b[1], b[2], b[3]])) + } + fn remaining(&self) -> usize { + self.b.len().saturating_sub(self.p) + } +} + +fn text(d: &Descriptor, key: &str) -> String { + match d.get(key) { + Some(Value::Text(t)) => t.to_string_lossy(), + _ => String::new(), + } +} + +fn read_style(body: &[u8]) -> Result { + let (identity, used) = VersionedDescriptor::parse_prefix(body)?; + let rest = body.get(used..).ok_or(PsdError::UnexpectedEof { offset: used, needed: 4 })?; + let (style, _) = VersionedDescriptor::parse_prefix(rest)?; + let id = &identity.descriptor; + Ok(AslStyle { name: text(id, "Nm "), id: text(id, "Idnt"), style: style.descriptor }) +} + +/// Parses an `.asl` style library. +pub fn parse(data: &[u8]) -> Result { + let mut r = Rd { b: data, p: 0 }; + let version = r.u16()?; + let sig = r.take(4)?; + if sig != b"8BSL" { + return Err(PsdError::InvalidSignature { expected: "8BSL", found: [sig[0], sig[1], sig[2], sig[3]] }); + } + let sub = r.u16()?; + if version != 2 || sub != 3 { + return Err(PsdError::Unsupported(format!("style library version {version}.{sub} (only 2.3, Photoshop 6 and later)"))); + } + let plen = r.u32()? as usize; + let section = r.take(plen)?; + let (patterns, pattern_error) = match parse_pattern_block(section) { + Ok(p) => (p, None), + Err(e) => (Vec::new(), Some(e.to_string())), + }; + let count = r.u32()? as usize; + // Every style needs at least its own length word. + if count > r.remaining() / 4 { + return Err(PsdError::invalid(format!("{count} styles cannot fit in the remaining {} bytes", r.remaining()))); + } + if count > MAX_STYLES { + return Err(PsdError::LimitExceeded("too many styles in one library")); + } + let mut styles = Vec::with_capacity(count); + for _ in 0..count { + let len = r.u32()? as usize; + let body = r.take(len)?; + styles.push(read_style(body)?); + let pad = (4 - len % 4) % 4; + r.p = r.p.saturating_add(pad).min(data.len()); + } + Ok(AslFile { patterns, pattern_error: pattern_error.filter(|_| !section.is_empty()), styles }) +} + +/// Writes a version 3 `.asl` style library. +pub fn write(file: &AslFile) -> Result> { + let mut out = Vec::new(); + out.extend_from_slice(&2u16.to_be_bytes()); + out.extend_from_slice(b"8BSL"); + out.extend_from_slice(&3u16.to_be_bytes()); + let patterns = write_pattern_block(&file.patterns)?; + let plen = u32::try_from(patterns.len()).map_err(|_| PsdError::LimitExceeded("pattern section larger than 4 GB"))?; + out.extend_from_slice(&plen.to_be_bytes()); + out.extend_from_slice(&patterns); + let count = u32::try_from(file.styles.len()).map_err(|_| PsdError::LimitExceeded("too many styles"))?; + out.extend_from_slice(&count.to_be_bytes()); + for s in &file.styles { + let identity = + Descriptor::new("null").with("Nm ", Value::Text(UnicodeString::new_nul(&s.name))).with("Idnt", Value::Text(UnicodeString::new_nul(&s.id))); + let mut body = VersionedDescriptor::new(identity).to_bytes(); + body.extend(VersionedDescriptor::new(s.style.clone()).to_bytes()); + body.resize(body.len().div_ceil(4) * 4, 0); + let len = u32::try_from(body.len()).map_err(|_| PsdError::LimitExceeded("style larger than 4 GB"))?; + out.extend_from_slice(&len.to_be_bytes()); + out.extend_from_slice(&body); + } + Ok(out) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::descriptor::Id; + + fn pattern() -> PsdPattern { + PsdPattern { + mode: 3, + width: 3, + height: 2, + name: "Dots".into(), + id: "a1b2".into(), + palette: None, + depth: 8, + channels: vec![vec![1, 2, 3, 4, 5, 6], vec![9; 6], vec![200; 6]], + alpha: None, + } + } + + fn style(name: &str) -> AslStyle { + let lefx = Descriptor::new("null").with("masterFXSwitch", Value::Boolean(true)).with( + "SoFi", + Value::Descriptor(Descriptor::new("SoFi").with("enab", Value::Boolean(true)).with("Opct", Value::UnitFloat { unit: *b"#Prc", value: 50.0 })), + ); + let blend = Descriptor::new("blendOptions") + .with("Md ", Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new("Mltp") }) + .with("fillOpacity", Value::UnitFloat { unit: *b"#Prc", value: 25.0 }); + AslStyle { + name: name.into(), + id: format!("id-{name}"), + style: Descriptor::new("Styl") + .with("documentMode", Value::Descriptor(Descriptor::new("documentMode"))) + .with("Lefx", Value::Descriptor(lefx)) + .with("blendOptions", Value::Descriptor(blend)), + } + } + + fn sample() -> AslFile { + AslFile { patterns: vec![pattern()], pattern_error: None, styles: vec![style("Gold"), style(""), style("Ünïcode ✓")] } + } + + #[test] + fn round_trip() { + let f = sample(); + let b = write(&f).unwrap(); + assert_eq!(&b[..8], b"\0\x028BSL\0\x03"); + assert_eq!(b.len() % 4, 0); + assert_eq!(parse(&b).unwrap(), f); + let empty = AslFile::default(); + assert_eq!(parse(&write(&empty).unwrap()).unwrap(), empty); + } + + #[test] + fn record_lengths_without_padding_also_read() { + // A writer that leaves the padding out of each record length. + let f = sample(); + let mut out = write(&AslFile { styles: vec![], ..f.clone() }).unwrap(); + out.truncate(out.len() - 4); + out.extend_from_slice(&(f.styles.len() as u32).to_be_bytes()); + for s in &f.styles { + let identity = + Descriptor::new("null").with("Nm ", Value::Text(UnicodeString::new_nul(&s.name))).with("Idnt", Value::Text(UnicodeString::new_nul(&s.id))); + let mut body = VersionedDescriptor::new(identity).to_bytes(); + body.extend(VersionedDescriptor::new(s.style.clone()).to_bytes()); + body.push(7); // odd length + out.extend_from_slice(&(body.len() as u32).to_be_bytes()); + let pad = (4 - body.len() % 4) % 4; + out.extend_from_slice(&body); + out.extend(std::iter::repeat_n(0, pad)); + } + assert_eq!(parse(&out).unwrap().styles, f.styles); + } + + #[test] + fn bad_pattern_section_is_skipped() { + let mut b = write(&AslFile { patterns: vec![], pattern_error: None, styles: vec![style("A")] }).unwrap(); + // Replace the empty pattern section with 8 bytes of garbage. + let tail = b.split_off(12); + b.truncate(8); + b.extend_from_slice(&8u32.to_be_bytes()); + b.extend_from_slice(&[0, 0, 0, 4, 0xff, 0xff, 0xff, 0xff]); + b.extend(tail); + let f = parse(&b).unwrap(); + assert!(f.pattern_error.is_some()); + assert_eq!(f.styles.len(), 1); + } + + #[test] + fn hostile_input_errors() { + assert!(parse(b"").is_err()); + assert!(parse(b"\0\x028BSL\0\x02").is_err()); + assert!(parse(b"\0\x028BPS\0\x03").is_err()); + // A count far beyond the data. + let mut b = b"\0\x028BSL\0\x03\0\0\0\0".to_vec(); + b.extend_from_slice(&u32::MAX.to_be_bytes()); + assert!(parse(&b).is_err()); + // A pattern length beyond the data. + assert!(parse(b"\0\x028BSL\0\x03\xff\xff\xff\xff").is_err()); + let b = write(&sample()).unwrap(); + for cut in 0..b.len() { + assert!(parse(&b[..cut]).is_err(), "cut at {cut}"); + } + for i in 0..b.len() { + for x in [0x5a, 0xff, 0x01] { + let mut c = b.clone(); + c[i] ^= x; + let _ = parse(&c); + } + } + // Seeded random noise after a valid header. + let mut seed = 0x1234_5678u32; + for _ in 0..500 { + let mut c = b"\0\x028BSL\0\x03".to_vec(); + for _ in 0..(seed % 200) { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + c.push(seed as u8); + } + let _ = parse(&c); + } + } +} diff --git a/crates/psd/src/atn.rs b/crates/psd/src/atn.rs index 2d8bf9990f..6bb900d435 100644 --- a/crates/psd/src/atn.rs +++ b/crates/psd/src/atn.rs @@ -17,8 +17,10 @@ //! //! The descriptor is a plain ActionDescriptor body ([`Descriptor`], no version prefix): the //! event's parameters, exactly as Photoshop's own "Make", "Set", "Gaussian Blur"… events -//! carry them. Photoshop 5-era files (version 12) use a different, undocumented layout and are -//! rejected as unsupported. +//! carry them. The event name and the panel name are byte strings (ASCII, or the platform's +//! legacy encoding for a localised panel name: Mac Roman "Définir"), kept verbatim; +//! [`AtnItem::label`] gives a readable name. Photoshop 5-era files (version 12) use a +//! different, undocumented layout and are rejected as unsupported. //! //! [`parse`] never panics: every count is bounds-checked against the bytes left, descriptor //! nesting is bounded, and allocations are capped. [`write`] produces the same layout, so @@ -26,7 +28,7 @@ use crate::descriptor::{Descriptor, UnicodeString}; use crate::error::{PsdError, Result}; -use crate::io::{Reader, WriteExt}; +use crate::io::{Reader, WriteExt, decode_legacy_name}; /// The only version this module reads and writes (Photoshop 6 and every later release). pub const VERSION: u32 = 16; @@ -84,8 +86,9 @@ pub struct AtnItem { pub dialog_options: u8, /// The event. pub event: AtnEvent, - /// The name the Actions panel shows for the step ("Gaussian Blur", "Make", …). - pub dictionary: String, + /// The name the Actions panel shows for the step ("Gaussian Blur", "Make", …), as stored: + /// ASCII, or the authoring platform's legacy encoding when localised. See [`Self::label`]. + pub dictionary: Vec, /// The event's parameters; `None` for events without any. pub descriptor: Option, } @@ -98,49 +101,71 @@ impl Default for AtnItem { with_dialog: false, dialog_options: 0, event: AtnEvent::Code(*b" "), - dictionary: String::new(), + dictionary: Vec::new(), descriptor: None, } } } +impl AtnItem { + /// The panel name as text: UTF-8 when it is, otherwise decoded byte by byte (Latin-1). + pub fn dictionary(&self) -> String { + decode_legacy_name(&self.dictionary) + } + + /// A readable name for the step: the descriptor's class name when Photoshop stored one + /// (Unicode, so a localised name survives), else [`Self::dictionary`], else the event id. + pub fn label(&self) -> String { + let class = self.descriptor.as_ref().map(|d| d.name.to_string_lossy()).unwrap_or_default(); + if !class.trim().is_empty() { + return class; + } + let dict = self.dictionary(); + if dict.trim().is_empty() { self.event.id() } else { dict } + } +} + /// A Photoshop event id: a four-character code (`GsnB`) or a string name (`trim`). #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum AtnEvent { /// Stored as `long` + 4 bytes. Code([u8; 4]), - /// Stored as `TEXT` + length-prefixed ASCII. - Name(String), + /// Stored as `TEXT` + length-prefixed bytes (ASCII in every file seen). + Name(Vec), } impl AtnEvent { + /// A string-named event. + pub fn name(s: &str) -> Self { + AtnEvent::Name(s.as_bytes().to_vec()) + } /// The event id as text: the code's four characters, or the name. pub fn id(&self) -> String { match self { AtnEvent::Code(c) => String::from_utf8_lossy(c).into_owned(), - AtnEvent::Name(n) => n.clone(), + AtnEvent::Name(n) => String::from_utf8_lossy(n).into_owned(), } } /// `true` if the event is the code or name `s`. pub fn is(&self, s: &str) -> bool { match self { AtnEvent::Code(c) => c == s.as_bytes(), - AtnEvent::Name(n) => n == s, + AtnEvent::Name(n) => n == s.as_bytes(), } } } -/// Reads a u32 length + ASCII bytes (event and dictionary names). -fn read_ascii(r: &mut Reader<'_>) -> Result { +/// Reads a u32 length + bytes (event and dictionary names), verbatim. +fn read_ascii(r: &mut Reader<'_>) -> Result> { let n = r.u32()?; - Ok(String::from_utf8_lossy(r.bytes_u64(u64::from(n))?).into_owned()) + Ok(r.bytes_u64(u64::from(n))?.to_vec()) } -fn write_ascii(out: &mut Vec, s: &str) -> Result<()> { +fn write_ascii(out: &mut Vec, s: &[u8]) -> Result<()> { let n = u32::try_from(s.len()).map_err(|_| PsdError::LimitExceeded("action name too long"))?; out.put_u32(n); - out.put(s.as_bytes()); + out.put(s); Ok(()) } @@ -277,12 +302,12 @@ mod tests { with_dialog: true, dialog_options: 2, event: AtnEvent::Code(*b"GsnB"), - dictionary: "Gaussian Blur".into(), + dictionary: b"Gaussian Blur".to_vec(), descriptor: Some(Descriptor::new("null").with("Rds ", px(2.5))), }; let trim = AtnItem { - event: AtnEvent::Name("trim".into()), - dictionary: "Trim".into(), + event: AtnEvent::name("trim"), + dictionary: b"Trim".to_vec(), descriptor: Some( Descriptor { name: UnicodeString::new_nul("Trim"), class_id: crate::descriptor::Id::Str(b"trim".to_vec()), items: Vec::new() } .with( @@ -293,7 +318,7 @@ mod tests { ), ..Default::default() }; - let flatten = AtnItem { enabled: false, event: AtnEvent::Code(*b"Fltn"), dictionary: "Flatten Image".into(), ..Default::default() }; + let flatten = AtnItem { enabled: false, event: AtnEvent::Code(*b"Fltn"), dictionary: b"Flatten Image".to_vec(), ..Default::default() }; AtnFile { name: UnicodeString::new_nul("Set ✓"), expanded: true, @@ -324,6 +349,21 @@ mod tests { assert_eq!(parsed.actions[0].items[0].event.id(), "GsnB"); assert!(parsed.actions[1].items[0].event.is("trim")); assert_eq!(parsed.actions[1].items[0].event.id(), "trim"); + assert_eq!(parsed.actions[0].items[0].label(), "Gaussian Blur"); + assert_eq!(parsed.actions[1].items[0].label(), "Trim"); + // A localised panel name in the platform's legacy encoding survives a round trip and + // the Unicode class name is the readable label. + let localised = AtnItem { + event: AtnEvent::name("set"), + dictionary: b"D\x8efinir".to_vec(), + descriptor: Some(Descriptor { name: UnicodeString::new_nul("Définir"), class_id: crate::descriptor::Id::new("setd"), items: Vec::new() }), + ..Default::default() + }; + assert_eq!(localised.label(), "Définir"); + let f = AtnFile { actions: vec![AtnAction { items: vec![localised.clone()], ..Default::default() }], ..Default::default() }; + let bytes = write(&f).unwrap(); + assert_eq!(parse(&bytes).unwrap().actions[0].items[0], localised); + assert_eq!(AtnItem { event: AtnEvent::name("x"), ..Default::default() }.label(), "x"); } /// The layout Photoshop writes for a one-step Trim action, assembled by hand from the @@ -338,8 +378,8 @@ mod tests { expanded: true, items: vec![AtnItem { expanded: true, - event: AtnEvent::Name("trim".into()), - dictionary: "Trim".into(), + event: AtnEvent::name("trim"), + dictionary: b"Trim".to_vec(), descriptor: Some(Descriptor { name: UnicodeString::new_nul("Trim"), class_id: crate::descriptor::Id::Str(b"trim".to_vec()), diff --git a/crates/psd/src/builder.rs b/crates/psd/src/builder.rs index d419f8edac..6606bdb351 100644 --- a/crates/psd/src/builder.rs +++ b/crates/psd/src/builder.rs @@ -460,6 +460,7 @@ impl PsdBuilder { global_blocks: Vec::new(), layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, }) } diff --git a/crates/psd/src/compression.rs b/crates/psd/src/compression.rs index aab785dff4..eb5d05d609 100644 --- a/crates/psd/src/compression.rs +++ b/crates/psd/src/compression.rs @@ -111,9 +111,10 @@ pub mod packbits { /// /// Errors if the input is exhausted early or a packet would overflow the /// row. Trailing input after the row is complete is ignored except for - /// `-128` no-op bytes (which are always permitted). - pub fn decode_into(src: &[u8], expected: usize, out: &mut Vec) -> Result<()> { - let target = out.len() + expected; + /// `-128` no-op bytes (which are always permitted). Errors, rather than + /// overflowing, when `out.len() + expected` does not fit in `usize`. + pub(crate) fn decode_into(src: &[u8], expected: usize, out: &mut Vec) -> Result<()> { + let target = out.len().checked_add(expected).ok_or(PsdError::LimitExceeded("PackBits output length overflows usize"))?; let mut i = 0; while out.len() < target { let Some(&h) = src.get(i) else { @@ -126,7 +127,7 @@ pub mod packbits { let Some(lit) = src.get(i..i + len) else { return Err(PsdError::Decompress("PackBits literal truncated".into())); }; - if out.len() + len > target { + if len > target - out.len() { return Err(PsdError::Decompress("PackBits literal overflows row".into())); } out.extend_from_slice(lit); @@ -138,7 +139,7 @@ pub mod packbits { return Err(PsdError::Decompress("PackBits run truncated".into())); }; i += 1; - if out.len() + len > target { + if len > target - out.len() { return Err(PsdError::Decompress("PackBits run overflows row".into())); } out.extend(std::iter::repeat_n(b, len)); @@ -224,6 +225,28 @@ pub fn decode_planes(compression: Compression, data: &[u8], layout: &PlaneLayout } } +/// Bytes a PSD RLE stream (two-byte row counts) of `rows` rows grows by when written as PSB +/// (four-byte row counts); an error when `data` is too short to hold the count table. +pub(crate) fn rle_widening(data: &[u8], rows: usize) -> Result { + let table = rows.checked_mul(2).ok_or(PsdError::LimitExceeded("row count overflow"))?; + if data.len() < table { + return Err(PsdError::UnexpectedEof { offset: data.len(), needed: table - data.len() }); + } + Ok(table as u64) +} + +/// Writes a PSD RLE stream (two-byte row counts) as the PSB one (four-byte row counts): the packed +/// rows are the same in both versions, only the count table widens. +pub(crate) fn put_widened_rle(out: &mut Vec, data: &[u8], rows: usize) -> Result<()> { + let table_len = usize::try_from(rle_widening(data, rows)?).map_err(|_| PsdError::LimitExceeded("row count overflow"))?; + let (table, packed) = data.split_at_checked(table_len).ok_or_else(|| PsdError::invalid("RLE row-count table out of range"))?; + for count in table.as_chunks::<2>().0 { + out.extend_from_slice(&[0, 0, count[0], count[1]]); + } + out.extend_from_slice(packed); + Ok(()) +} + /// Encodes planar data (excluding the 2-byte compression field). pub fn encode_planes(compression: Compression, decoded: &[u8], layout: &PlaneLayout) -> Result> { let total = layout.decoded_len()?; @@ -538,347 +561,7 @@ pub fn unpredict(buf: &mut [u8], layout: &PlaneLayout) -> Result<()> { } #[cfg(test)] -mod tests { - use super::*; - - #[test] - fn checked_zip_validates_trailer_size_and_padding_budget() { - let layout = PlaneLayout { planes: 1, width: 3, height: 1, depth: 8, version: Version::Psd }; - for compression in [Compression::Zip, Compression::ZipPrediction] { - let data = encode_planes(compression, &[1, 2, 3], &layout).unwrap(); - for cut in 0..data.len() { - assert!(decode_planes(compression, &data[..cut], &layout).is_err(), "cut {cut}"); - assert!(validate_planes(compression, &data[..cut], &layout).is_err()); - } - let mut corrupt = data; - *corrupt.last_mut().unwrap() ^= 1; - assert!(decode_planes(compression, &corrupt, &layout).is_err()); - assert!(validate_planes(compression, &corrupt, &layout).is_err()); - } - let padded = zip_compress(&[1, 2, 3, 0]); - assert_eq!(decode_planes(Compression::Zip, &padded, &layout).unwrap(), [1, 2, 3]); - let bomb = zip_compress(&vec![0; (1 << 20) + 4]); - assert!(decode_planes(Compression::Zip, &bomb, &layout).is_err()); - assert!(validate_planes(Compression::Zip, &bomb, &layout).is_err()); - } - - fn layout(planes: usize, width: usize, height: usize, depth: u16, version: Version) -> PlaneLayout { - PlaneLayout { planes, width, height, depth, version } - } - - fn pb_roundtrip(src: &[u8]) -> Vec { - let enc = packbits::encode_vec(src); - let dec = packbits::decode(&enc, src.len()).unwrap(); - assert_eq!(dec, src, "encoded: {enc:?}"); - enc - } - - #[test] - fn zip_absurd_declared_size_is_an_error() { - let z = zip_compress(&[7u8; 64]); - for expected in [1usize << 40, usize::MAX / 2, usize::MAX] { - assert!(zip_decompress(&z, expected).is_err(), "expected {expected}"); - } - // Through decode_planes: a tiny ZIP channel claiming a huge (but capped) plane. - let l = layout(1, 1 << 16, 1 << 15, 8, Version::Psb); - assert!(decode_planes(Compression::Zip, &z, &l).is_err()); - assert!(decode_planes(Compression::ZipPrediction, &z, &l).is_err()); - // Past MAX_DECODED_BYTES. - let l = layout(4, 1 << 20, 1 << 20, 32, Version::Psb); - assert!(decode_planes(Compression::Zip, &z, &l).is_err()); - // Garbage that is not zlib at all. - assert!(zip_decompress(&[0xff; 16], 1 << 30).is_err()); - } - - #[test] - fn zip_decompress_exact_and_truncated() { - let src: Vec = (0..3_000_000u32).map(|i| (i % 251) as u8).collect(); - let z = zip_compress(&src); - assert_eq!(zip_decompress(&z, src.len()).unwrap(), src); - // Asking for fewer bytes returns exactly that many. - assert_eq!(zip_decompress(&z, 1000).unwrap(), &src[..1000]); - assert!(zip_decompress(&z, src.len() + 1).is_err()); - assert!(zip_decompress(&z, 0).unwrap().is_empty()); - } - - #[test] - fn packbits_empty() { - assert!(pb_roundtrip(&[]).is_empty()); - } - - #[test] - fn packbits_single() { - assert_eq!(pb_roundtrip(&[7]), vec![0, 7]); - } - - #[test] - fn packbits_run_of_two() { - assert_eq!(pb_roundtrip(&[5, 5]), vec![0xff, 5]); - } - - #[test] - fn packbits_run_128() { - let src = vec![9u8; 128]; - assert_eq!(pb_roundtrip(&src), vec![0x81, 9]); - } - - #[test] - fn packbits_run_129() { - let src = vec![9u8; 129]; - assert_eq!(pb_roundtrip(&src), vec![0x81, 9, 0, 9]); - } - - #[test] - fn packbits_run_130_and_256() { - pb_roundtrip(&[1u8; 130]); - let enc = pb_roundtrip(&[1u8; 256]); - assert_eq!(enc, vec![0x81, 1, 0x81, 1]); - } - - #[test] - fn packbits_literal_128_and_129() { - let src: Vec = (0..128u32).map(|i| i as u8).collect(); - let enc = pb_roundtrip(&src); - assert_eq!(enc[0], 127); - assert_eq!(enc.len(), 129); - let src: Vec = (0..129u32).map(|i| i as u8).collect(); - let enc = pb_roundtrip(&src); - assert_eq!(enc[0], 127); - assert_eq!(enc[129], 0); - assert_eq!(enc.len(), 131); - } - - #[test] - fn packbits_literal_run_boundary() { - pb_roundtrip(&[1, 2, 3, 3, 3, 3, 4, 5]); - pb_roundtrip(&[1, 1, 2, 2, 3, 3]); - pb_roundtrip(&[1, 2, 2, 3]); - pb_roundtrip(&[0, 0, 0, 1, 0, 0, 0]); - let enc = pb_roundtrip(&[1, 2, 3, 3, 3]); - assert_eq!(enc, vec![1, 1, 2, 0xfe, 3]); - } - - #[test] - fn packbits_never_emits_noop() { - for n in 0..300usize { - let src: Vec = (0..n).map(|i| ((i / 3) % 5) as u8).collect(); - let enc = pb_roundtrip(&src); - // Walk the packets to find headers. - let mut i = 0; - while i < enc.len() { - let h = enc[i] as i8; - assert_ne!(h, -128); - i += if h >= 0 { h as usize + 2 } else { 2 }; - } - } - } - - #[test] - fn packbits_decode_noop_skipped() { - assert_eq!(packbits::decode(&[0x80, 0, 42], 1).unwrap(), vec![42]); - } - - #[test] - fn packbits_decode_errors() { - assert!(packbits::decode(&[], 1).is_err()); - assert!(packbits::decode(&[2, 1, 2], 3).is_err()); // literal truncated - assert!(packbits::decode(&[0xfe], 3).is_err()); // run truncated - assert!(packbits::decode(&[0xfd, 1], 3).is_err()); // run overflows (4 > 3) - assert!(packbits::decode(&[3, 1, 2, 3, 4], 3).is_err()); // literal overflows - } - - #[test] - fn packbits_zero_expected_ignores_input() { - assert_eq!(packbits::decode(&[0xff, 1], 0).unwrap(), Vec::::new()); - } - - #[test] - fn compression_values() { - for v in 0..6 { - assert_eq!(Compression::from_u16(v).as_u16(), v); - } - assert_eq!(Compression::from_u16(9), Compression::Unknown(9)); - } - - fn sample_data(l: &PlaneLayout) -> Vec { - let n = l.decoded_len().unwrap(); - (0..n).map(|i| ((i * 7 + i / 5) % 251) as u8).collect() - } - - #[test] - fn all_codecs_roundtrip_all_depths() { - for version in [Version::Psd, Version::Psb] { - for depth in [1u16, 8, 16, 32] { - for (w, h) in [(0, 0), (1, 1), (3, 2), (17, 5), (0, 3), (4, 0)] { - for planes in [1, 3] { - let l = layout(planes, w, h, depth, version); - let data = sample_data(&l); - for c in Compression::ALL { - let enc = encode_planes(c, &data, &l).unwrap(); - validate_planes(c, &enc, &l).unwrap(); - let dec = decode_planes(c, &enc, &l).unwrap(); - assert_eq!(dec, data, "{c:?} {depth} {w}x{h} {version:?}"); - } - } - } - } - } - } - - #[test] - fn rle_count_width_psd_vs_psb() { - let data = vec![0u8; 4]; - let psd = encode_planes(Compression::Rle, &data, &layout(1, 2, 2, 8, Version::Psd)).unwrap(); - let psb = encode_planes(Compression::Rle, &data, &layout(1, 2, 2, 8, Version::Psb)).unwrap(); - assert_eq!(psd, vec![0, 2, 0, 2, 0xff, 0, 0xff, 0]); - assert_eq!(psb, vec![0, 0, 0, 2, 0, 0, 0, 2, 0xff, 0, 0xff, 0]); - assert!(decode_planes(Compression::Rle, &psd, &layout(1, 2, 2, 8, Version::Psb)).is_err()); - } - - #[test] - fn rle_odd_width() { - let l = layout(1, 7, 3, 16, Version::Psd); - let data = sample_data(&l); - let enc = encode_planes(Compression::Rle, &data, &l).unwrap(); - assert_eq!(decode_planes(Compression::Rle, &enc, &l).unwrap(), data); - } - - #[test] - fn rle_truncated_fails() { - let l = layout(1, 10, 10, 8, Version::Psd); - let data = sample_data(&l); - let enc = encode_planes(Compression::Rle, &data, &l).unwrap(); - for cut in 0..enc.len() { - assert!(decode_planes(Compression::Rle, &enc[..cut], &l).is_err(), "cut {cut}"); - assert!(validate_planes(Compression::Rle, &enc[..cut], &l).is_err(), "cut {cut}"); - } - } - - #[test] - fn zip_truncated_fails() { - let l = layout(1, 10, 10, 8, Version::Psd); - let data = sample_data(&l); - let enc = encode_planes(Compression::Zip, &data, &l).unwrap(); - for cut in 0..enc.len() - 4 { - assert!(decode_planes(Compression::Zip, &enc[..cut], &l).is_err(), "cut {cut}"); - assert!(validate_planes(Compression::Zip, &enc[..cut], &l).is_err(), "cut {cut}"); - } - } - - /// #2926: a ZIP stream declaring more than `MAX_DECODED_BYTES` is not inflated at parse - /// time. Garbage zlib data proves it: decompressing it would fail, skipping it passes, and - /// decoding the layout still reports the size limit. - #[test] - fn zip_over_budget_is_not_decompressed_at_parse() { - let l = layout(4, 300_000, 300_000, 8, Version::Psb); - assert!(l.total_bytes().unwrap() > MAX_DECODED_BYTES); - let garbage = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03]; - for c in [Compression::Zip, Compression::ZipPrediction] { - validate_planes(c, &garbage, &l).unwrap(); - assert!(matches!(decode_planes(c, &garbage, &l), Err(PsdError::LimitExceeded(_))), "{c:?}"); - } - // Within the budget the stream is still checked. - let small = layout(1, 4, 4, 8, Version::Psd); - assert!(validate_planes(Compression::Zip, &garbage, &small).is_err()); - } - - #[test] - fn raw_short_fails() { - let l = layout(2, 3, 3, 8, Version::Psd); - assert!(decode_planes(Compression::Raw, &[0; 17], &l).is_err()); - assert!(decode_planes(Compression::Raw, &[0; 18], &l).is_ok()); - } - - #[test] - fn unknown_compression_errors() { - let l = layout(1, 1, 1, 8, Version::Psd); - assert!(decode_planes(Compression::Unknown(7), &[0], &l).is_err()); - assert!(encode_planes(Compression::Unknown(7), &[0], &l).is_err()); - } - - #[test] - fn encode_rejects_wrong_size() { - let l = layout(1, 2, 2, 8, Version::Psd); - assert!(encode_planes(Compression::Raw, &[0; 3], &l).is_err()); - } - - #[test] - fn decoded_len_limits() { - let l = layout(56, 300_000, 300_000, 32, Version::Psb); - assert!(matches!(l.decoded_len(), Err(PsdError::LimitExceeded(_)))); - assert!(decode_planes(Compression::Rle, &[0; 16], &l).is_err()); - } - - /// Real PSBs go past 2 GiB in one decode (#375): a 30000² RGB merged image and a 33000² - /// 16-bit layer channel. Only checks the limit, so nothing is allocated. - #[test] - #[cfg(target_pointer_width = "64")] - fn decoded_len_allows_large_psb_planes() { - assert_eq!(layout(3, 30_000, 30_000, 8, Version::Psb).decoded_len().unwrap(), 2_700_000_000); - assert_eq!(layout(1, 33_000, 33_000, 16, Version::Psb).decoded_len().unwrap(), 2_178_000_000); - assert!(layout(4, 40_000, 40_000, 16, Version::Psb).decoded_len().is_err()); - } - - #[test] - fn prediction_8bit_known_vector() { - let l = layout(1, 4, 1, 8, Version::Psd); - let mut v = vec![10, 12, 11, 255]; - predict(&mut v, &l).unwrap(); - assert_eq!(v, vec![10, 2, 255, 244]); - unpredict(&mut v, &l).unwrap(); - assert_eq!(v, vec![10, 12, 11, 255]); - } - - #[test] - fn prediction_16bit_known_vector() { - let l = layout(1, 3, 1, 16, Version::Psd); - let mut v = vec![0x01, 0x00, 0x01, 0x05, 0x00, 0x00]; - predict(&mut v, &l).unwrap(); - assert_eq!(v, vec![0x01, 0x00, 0x00, 0x05, 0xfe, 0xfb]); - unpredict(&mut v, &l).unwrap(); - assert_eq!(v, vec![0x01, 0x00, 0x01, 0x05, 0x00, 0x00]); - } - - #[test] - fn prediction_32bit_plane_shuffle() { - // Two samples: AABBCCDD and 11223344 → planes [AA,11][BB,22][CC,33][DD,44] - let l = layout(1, 2, 1, 32, Version::Psd); - let orig = vec![0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22, 0x33, 0x44]; - let mut v = orig.clone(); - predict(&mut v, &l).unwrap(); - let planes = [0xAAu8, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44]; - let mut expect = planes; - for i in (1..8).rev() { - expect[i] = expect[i].wrapping_sub(expect[i - 1]); - } - assert_eq!(v, expect); - unpredict(&mut v, &l).unwrap(); - assert_eq!(v, orig); - } - - #[test] - fn prediction_32bit_floats_roundtrip() { - let l = layout(1, 5, 2, 32, Version::Psd); - let mut v = Vec::new(); - for i in 0..10 { - v.extend_from_slice(&(i as f32 * 0.37 - 1.0).to_be_bytes()); - } - let enc = encode_planes(Compression::ZipPrediction, &v, &l).unwrap(); - assert_eq!(decode_planes(Compression::ZipPrediction, &enc, &l).unwrap(), v); - } - - #[test] - fn zip_bomb_is_bounded() { - let big = zip_compress(&vec![0u8; 1 << 20]); - let out = zip_decompress(&big, 10).unwrap(); - assert_eq!(out.len(), 10); - } - - #[test] - fn zip_garbage_errors() { - assert!(zip_decompress(&[1, 2, 3, 4], 4).is_err()); - } -} +mod tests; #[cfg(test)] mod parallel_rle_equivalence { diff --git a/crates/psd/src/compression/tests.rs b/crates/psd/src/compression/tests.rs new file mode 100644 index 0000000000..a9700ded31 --- /dev/null +++ b/crates/psd/src/compression/tests.rs @@ -0,0 +1,353 @@ +use super::*; + +#[test] +fn checked_zip_validates_trailer_size_and_padding_budget() { + let layout = PlaneLayout { planes: 1, width: 3, height: 1, depth: 8, version: Version::Psd }; + for compression in [Compression::Zip, Compression::ZipPrediction] { + let data = encode_planes(compression, &[1, 2, 3], &layout).unwrap(); + for cut in 0..data.len() { + assert!(decode_planes(compression, &data[..cut], &layout).is_err(), "cut {cut}"); + assert!(validate_planes(compression, &data[..cut], &layout).is_err()); + } + let mut corrupt = data; + *corrupt.last_mut().unwrap() ^= 1; + assert!(decode_planes(compression, &corrupt, &layout).is_err()); + assert!(validate_planes(compression, &corrupt, &layout).is_err()); + } + let padded = zip_compress(&[1, 2, 3, 0]); + assert_eq!(decode_planes(Compression::Zip, &padded, &layout).unwrap(), [1, 2, 3]); + let bomb = zip_compress(&vec![0; (1 << 20) + 4]); + assert!(decode_planes(Compression::Zip, &bomb, &layout).is_err()); + assert!(validate_planes(Compression::Zip, &bomb, &layout).is_err()); +} + +fn layout(planes: usize, width: usize, height: usize, depth: u16, version: Version) -> PlaneLayout { + PlaneLayout { planes, width, height, depth, version } +} + +fn pb_roundtrip(src: &[u8]) -> Vec { + let enc = packbits::encode_vec(src); + let dec = packbits::decode(&enc, src.len()).unwrap(); + assert_eq!(dec, src, "encoded: {enc:?}"); + enc +} + +#[test] +fn zip_absurd_declared_size_is_an_error() { + let z = zip_compress(&[7u8; 64]); + for expected in [1usize << 40, usize::MAX / 2, usize::MAX] { + assert!(zip_decompress(&z, expected).is_err(), "expected {expected}"); + } + // Through decode_planes: a tiny ZIP channel claiming a huge (but capped) plane. + let l = layout(1, 1 << 16, 1 << 15, 8, Version::Psb); + assert!(decode_planes(Compression::Zip, &z, &l).is_err()); + assert!(decode_planes(Compression::ZipPrediction, &z, &l).is_err()); + // Past MAX_DECODED_BYTES. + let l = layout(4, 1 << 20, 1 << 20, 32, Version::Psb); + assert!(decode_planes(Compression::Zip, &z, &l).is_err()); + // Garbage that is not zlib at all. + assert!(zip_decompress(&[0xff; 16], 1 << 30).is_err()); +} + +#[test] +fn zip_decompress_exact_and_truncated() { + let src: Vec = (0..3_000_000u32).map(|i| (i % 251) as u8).collect(); + let z = zip_compress(&src); + assert_eq!(zip_decompress(&z, src.len()).unwrap(), src); + // Asking for fewer bytes returns exactly that many. + assert_eq!(zip_decompress(&z, 1000).unwrap(), &src[..1000]); + assert!(zip_decompress(&z, src.len() + 1).is_err()); + assert!(zip_decompress(&z, 0).unwrap().is_empty()); +} + +#[test] +fn packbits_empty() { + assert!(pb_roundtrip(&[]).is_empty()); +} + +#[test] +fn packbits_single() { + assert_eq!(pb_roundtrip(&[7]), vec![0, 7]); +} + +#[test] +fn packbits_run_of_two() { + assert_eq!(pb_roundtrip(&[5, 5]), vec![0xff, 5]); +} + +#[test] +fn packbits_run_128() { + let src = vec![9u8; 128]; + assert_eq!(pb_roundtrip(&src), vec![0x81, 9]); +} + +#[test] +fn packbits_run_129() { + let src = vec![9u8; 129]; + assert_eq!(pb_roundtrip(&src), vec![0x81, 9, 0, 9]); +} + +#[test] +fn packbits_run_130_and_256() { + pb_roundtrip(&[1u8; 130]); + let enc = pb_roundtrip(&[1u8; 256]); + assert_eq!(enc, vec![0x81, 1, 0x81, 1]); +} + +#[test] +fn packbits_literal_128_and_129() { + let src: Vec = (0..128u32).map(|i| i as u8).collect(); + let enc = pb_roundtrip(&src); + assert_eq!(enc[0], 127); + assert_eq!(enc.len(), 129); + let src: Vec = (0..129u32).map(|i| i as u8).collect(); + let enc = pb_roundtrip(&src); + assert_eq!(enc[0], 127); + assert_eq!(enc[129], 0); + assert_eq!(enc.len(), 131); +} + +#[test] +fn packbits_literal_run_boundary() { + pb_roundtrip(&[1, 2, 3, 3, 3, 3, 4, 5]); + pb_roundtrip(&[1, 1, 2, 2, 3, 3]); + pb_roundtrip(&[1, 2, 2, 3]); + pb_roundtrip(&[0, 0, 0, 1, 0, 0, 0]); + let enc = pb_roundtrip(&[1, 2, 3, 3, 3]); + assert_eq!(enc, vec![1, 1, 2, 0xfe, 3]); +} + +#[test] +fn packbits_never_emits_noop() { + for n in 0..300usize { + let src: Vec = (0..n).map(|i| ((i / 3) % 5) as u8).collect(); + let enc = pb_roundtrip(&src); + // Walk the packets to find headers. + let mut i = 0; + while i < enc.len() { + let h = enc[i] as i8; + assert_ne!(h, -128); + i += if h >= 0 { h as usize + 2 } else { 2 }; + } + } +} + +#[test] +fn packbits_decode_noop_skipped() { + assert_eq!(packbits::decode(&[0x80, 0, 42], 1).unwrap(), vec![42]); +} + +#[test] +fn packbits_decode_errors() { + assert!(packbits::decode(&[], 1).is_err()); + assert!(packbits::decode(&[2, 1, 2], 3).is_err()); // literal truncated + assert!(packbits::decode(&[0xfe], 3).is_err()); // run truncated + assert!(packbits::decode(&[0xfd, 1], 3).is_err()); // run overflows (4 > 3) + assert!(packbits::decode(&[3, 1, 2, 3, 4], 3).is_err()); // literal overflows +} + +/// `out.len() + expected` past `usize::MAX` used to panic in debug builds and, in release, +/// wrap and return `Ok` having decoded nothing. +#[test] +fn packbits_decode_into_output_length_overflow_is_an_error() { + let mut out = vec![7u8]; + let r = packbits::decode_into(&[0x00, 0xaa], usize::MAX, &mut out); + assert!(matches!(r, Err(PsdError::LimitExceeded(_))), "{r:?}"); + assert_eq!(out, [7]); + // The largest `expected` that fits still decodes up to the input, then reports it short. + let r = packbits::decode_into(&[0x00, 0xaa], usize::MAX - 1, &mut out); + assert!(matches!(r, Err(PsdError::Decompress(_))), "{r:?}"); + assert_eq!(out, [7, 0xaa]); +} + +#[test] +fn packbits_zero_expected_ignores_input() { + assert_eq!(packbits::decode(&[0xff, 1], 0).unwrap(), Vec::::new()); +} + +#[test] +fn compression_values() { + for v in 0..6 { + assert_eq!(Compression::from_u16(v).as_u16(), v); + } + assert_eq!(Compression::from_u16(9), Compression::Unknown(9)); +} + +fn sample_data(l: &PlaneLayout) -> Vec { + let n = l.decoded_len().unwrap(); + (0..n).map(|i| ((i * 7 + i / 5) % 251) as u8).collect() +} + +#[test] +fn all_codecs_roundtrip_all_depths() { + for version in [Version::Psd, Version::Psb] { + for depth in [1u16, 8, 16, 32] { + for (w, h) in [(0, 0), (1, 1), (3, 2), (17, 5), (0, 3), (4, 0)] { + for planes in [1, 3] { + let l = layout(planes, w, h, depth, version); + let data = sample_data(&l); + for c in Compression::ALL { + let enc = encode_planes(c, &data, &l).unwrap(); + validate_planes(c, &enc, &l).unwrap(); + let dec = decode_planes(c, &enc, &l).unwrap(); + assert_eq!(dec, data, "{c:?} {depth} {w}x{h} {version:?}"); + } + } + } + } + } +} + +#[test] +fn rle_count_width_psd_vs_psb() { + let data = vec![0u8; 4]; + let psd = encode_planes(Compression::Rle, &data, &layout(1, 2, 2, 8, Version::Psd)).unwrap(); + let psb = encode_planes(Compression::Rle, &data, &layout(1, 2, 2, 8, Version::Psb)).unwrap(); + assert_eq!(psd, vec![0, 2, 0, 2, 0xff, 0, 0xff, 0]); + assert_eq!(psb, vec![0, 0, 0, 2, 0, 0, 0, 2, 0xff, 0, 0xff, 0]); + assert!(decode_planes(Compression::Rle, &psd, &layout(1, 2, 2, 8, Version::Psb)).is_err()); +} + +#[test] +fn rle_odd_width() { + let l = layout(1, 7, 3, 16, Version::Psd); + let data = sample_data(&l); + let enc = encode_planes(Compression::Rle, &data, &l).unwrap(); + assert_eq!(decode_planes(Compression::Rle, &enc, &l).unwrap(), data); +} + +#[test] +fn rle_truncated_fails() { + let l = layout(1, 10, 10, 8, Version::Psd); + let data = sample_data(&l); + let enc = encode_planes(Compression::Rle, &data, &l).unwrap(); + for cut in 0..enc.len() { + assert!(decode_planes(Compression::Rle, &enc[..cut], &l).is_err(), "cut {cut}"); + assert!(validate_planes(Compression::Rle, &enc[..cut], &l).is_err(), "cut {cut}"); + } +} + +#[test] +fn zip_truncated_fails() { + let l = layout(1, 10, 10, 8, Version::Psd); + let data = sample_data(&l); + let enc = encode_planes(Compression::Zip, &data, &l).unwrap(); + for cut in 0..enc.len() - 4 { + assert!(decode_planes(Compression::Zip, &enc[..cut], &l).is_err(), "cut {cut}"); + assert!(validate_planes(Compression::Zip, &enc[..cut], &l).is_err(), "cut {cut}"); + } +} + +/// #2926: a ZIP stream declaring more than `MAX_DECODED_BYTES` is not inflated at parse +/// time. Garbage zlib data proves it: decompressing it would fail, skipping it passes, and +/// decoding the layout still reports the size limit. +#[test] +fn zip_over_budget_is_not_decompressed_at_parse() { + let l = layout(4, 300_000, 300_000, 8, Version::Psb); + assert!(l.total_bytes().unwrap() > MAX_DECODED_BYTES); + let garbage = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03]; + for c in [Compression::Zip, Compression::ZipPrediction] { + validate_planes(c, &garbage, &l).unwrap(); + assert!(matches!(decode_planes(c, &garbage, &l), Err(PsdError::LimitExceeded(_))), "{c:?}"); + } + // Within the budget the stream is still checked. + let small = layout(1, 4, 4, 8, Version::Psd); + assert!(validate_planes(Compression::Zip, &garbage, &small).is_err()); +} + +#[test] +fn raw_short_fails() { + let l = layout(2, 3, 3, 8, Version::Psd); + assert!(decode_planes(Compression::Raw, &[0; 17], &l).is_err()); + assert!(decode_planes(Compression::Raw, &[0; 18], &l).is_ok()); +} + +#[test] +fn unknown_compression_errors() { + let l = layout(1, 1, 1, 8, Version::Psd); + assert!(decode_planes(Compression::Unknown(7), &[0], &l).is_err()); + assert!(encode_planes(Compression::Unknown(7), &[0], &l).is_err()); +} + +#[test] +fn encode_rejects_wrong_size() { + let l = layout(1, 2, 2, 8, Version::Psd); + assert!(encode_planes(Compression::Raw, &[0; 3], &l).is_err()); +} + +#[test] +fn decoded_len_limits() { + let l = layout(56, 300_000, 300_000, 32, Version::Psb); + assert!(matches!(l.decoded_len(), Err(PsdError::LimitExceeded(_)))); + assert!(decode_planes(Compression::Rle, &[0; 16], &l).is_err()); +} + +/// Real PSBs go past 2 GiB in one decode (#375): a 30000² RGB merged image and a 33000² +/// 16-bit layer channel. Only checks the limit, so nothing is allocated. +#[test] +#[cfg(target_pointer_width = "64")] +fn decoded_len_allows_large_psb_planes() { + assert_eq!(layout(3, 30_000, 30_000, 8, Version::Psb).decoded_len().unwrap(), 2_700_000_000); + assert_eq!(layout(1, 33_000, 33_000, 16, Version::Psb).decoded_len().unwrap(), 2_178_000_000); + assert!(layout(4, 40_000, 40_000, 16, Version::Psb).decoded_len().is_err()); +} + +#[test] +fn prediction_8bit_known_vector() { + let l = layout(1, 4, 1, 8, Version::Psd); + let mut v = vec![10, 12, 11, 255]; + predict(&mut v, &l).unwrap(); + assert_eq!(v, vec![10, 2, 255, 244]); + unpredict(&mut v, &l).unwrap(); + assert_eq!(v, vec![10, 12, 11, 255]); +} + +#[test] +fn prediction_16bit_known_vector() { + let l = layout(1, 3, 1, 16, Version::Psd); + let mut v = vec![0x01, 0x00, 0x01, 0x05, 0x00, 0x00]; + predict(&mut v, &l).unwrap(); + assert_eq!(v, vec![0x01, 0x00, 0x00, 0x05, 0xfe, 0xfb]); + unpredict(&mut v, &l).unwrap(); + assert_eq!(v, vec![0x01, 0x00, 0x01, 0x05, 0x00, 0x00]); +} + +#[test] +fn prediction_32bit_plane_shuffle() { + // Two samples: AABBCCDD and 11223344 → planes [AA,11][BB,22][CC,33][DD,44] + let l = layout(1, 2, 1, 32, Version::Psd); + let orig = vec![0xAA, 0xBB, 0xCC, 0xDD, 0x11, 0x22, 0x33, 0x44]; + let mut v = orig.clone(); + predict(&mut v, &l).unwrap(); + let planes = [0xAAu8, 0x11, 0xBB, 0x22, 0xCC, 0x33, 0xDD, 0x44]; + let mut expect = planes; + for i in (1..8).rev() { + expect[i] = expect[i].wrapping_sub(expect[i - 1]); + } + assert_eq!(v, expect); + unpredict(&mut v, &l).unwrap(); + assert_eq!(v, orig); +} + +#[test] +fn prediction_32bit_floats_roundtrip() { + let l = layout(1, 5, 2, 32, Version::Psd); + let mut v = Vec::new(); + for i in 0..10 { + v.extend_from_slice(&(i as f32 * 0.37 - 1.0).to_be_bytes()); + } + let enc = encode_planes(Compression::ZipPrediction, &v, &l).unwrap(); + assert_eq!(decode_planes(Compression::ZipPrediction, &enc, &l).unwrap(), v); +} + +#[test] +fn zip_bomb_is_bounded() { + let big = zip_compress(&vec![0u8; 1 << 20]); + let out = zip_decompress(&big, 10).unwrap(); + assert_eq!(out.len(), 10); +} + +#[test] +fn zip_garbage_errors() { + assert!(zip_decompress(&[1, 2, 3, 4], 4).is_err()); +} diff --git a/crates/psd/src/error.rs b/crates/psd/src/error.rs index da519e6698..a1fc1a5c98 100644 --- a/crates/psd/src/error.rs +++ b/crates/psd/src/error.rs @@ -37,6 +37,9 @@ pub enum PsdError { /// A compressed stream could not be decoded. #[error("decompression failed: {0}")] Decompress(String), + /// Reading from a [`crate::source::ReadAt`] source failed. + #[error("read failed: {0}")] + Read(String), /// An operation is not supported for this data (e.g. unknown compression). #[error("unsupported: {0}")] Unsupported(String), diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index e14bc45aaf..828e7bc032 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -1,6 +1,6 @@ //! The top-level [`PsdFile`] model, parser and writer. -use crate::compression::{Compression, MAX_DECODED_BYTES, decode_planes, validate_planes}; +use crate::compression::{Compression, MAX_DECODED_BYTES, decode_planes, put_widened_rle, rle_widening, validate_planes}; use crate::error::{PsdError, Result}; use crate::header::{ColorMode, Header, Version}; use crate::image_data::ImageData; @@ -39,6 +39,14 @@ pub struct GlobalLayerMask { } impl GlobalLayerMask { + /// The mask Photoshop writes for a layered file that has no global mask of its own: + /// an RGB red overlay at 50% opacity, kind 128 (use the per-layer setting), plus the + /// filler byte. A zero-length `LMsk` block in a TIFF makes Photoshop give up on the + /// layer data (it reads this 14-byte record from the block). + pub fn photoshop_default() -> Self { + GlobalLayerMask { data: vec![0, 0, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 50, 128, 0] } + } + fn be16(&self, at: usize) -> Option { self.data.get(at..at + 2).map(|b| u16::from_be_bytes([b[0], b[1]])) } @@ -63,7 +71,8 @@ impl GlobalLayerMask { /// A complete PSD/PSB file. /// /// `PsdFile::from_bytes(b)?.to_bytes()? == b` for unmodified well-formed -/// files, and `from_bytes(to_bytes(f)) == f` for any model `f`. +/// files, and `from_bytes(to_bytes(f)) == f` for any model `f` not marked +/// [`Self::write_as_psb`] (a marked one reads back as the PSB it was written as). #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct PsdFile { @@ -86,6 +95,12 @@ pub struct PsdFile { pub layer_mask_trailing: Vec, /// Merged image data. pub image_data: ImageData, + /// Write this PSD model as PSB (set by [`Self::promote_to_psb`]). The model itself stays a + /// consistent PSD: its channel data keeps two-byte RLE row counts, so everything that reads + /// it with `header.version` decodes it as before. Only [`Self::to_bytes`] and + /// [`Self::encoded_len`] write version 2, long length fields and four-byte row counts. + #[cfg_attr(feature = "serde", serde(default))] + pub write_as_psb: bool, } impl PsdFile { @@ -174,14 +189,37 @@ impl PsdFile { layer_mask_trailing = s.peek_rest().to_vec(); } } - let image_data = ImageData { compression: Compression::Raw, data: Vec::new() }; - let mut file = - PsdFile { header, color_mode_data, resources, layer_info, layer_info_placement, global_layer_mask, global_blocks, layer_mask_trailing, image_data }; + let image_data = ImageData::new(Compression::Raw, Vec::new()); + let mut file = PsdFile { + header, + color_mode_data, + resources, + layer_info, + layer_info_placement, + global_layer_mask, + global_blocks, + layer_mask_trailing, + image_data, + write_as_psb: false, + }; + let want = want(&file); + let merged = file.read_merged_section(&mut r, want, recover)?; + Ok((file, merged)) + } + + /// Reads the merged image section from `rest` (the bytes after the layer and mask section) as + /// [`Self::from_bytes_and_merged_recovering`] does, decoding it. + pub(crate) fn read_merged_recovering(&mut self, rest: &[u8], recover: impl FnOnce(&PsdFile) -> bool) -> Result>>> { + self.read_merged_section(&mut Reader::new(rest), true, recover) + } + + fn read_merged_section(&mut self, r: &mut Reader<'_>, want: bool, recover: impl FnOnce(&PsdFile) -> bool) -> Result>>> { + let file = self; let mut encoded = r.clone(); - let merged = if want(&file) { - file.read_merged_decoded(&mut r).map(Some) + let merged = if want { + file.read_merged_decoded(r).map(Some) } else { - ImageData::read(&mut r, &file.header).map(|image| { + ImageData::read(r, &file.header).map(|image| { file.image_data = image; None }) @@ -189,7 +227,7 @@ impl PsdFile { let merged = match merged { Ok(merged) => merged, Err(error) => { - if matches!(error, PsdError::LimitExceeded(_)) || !recover(&file) { + if matches!(error, PsdError::LimitExceeded(_)) || !recover(file) { return Err(error); } let compression = Compression::from_u16(encoded.u16()?); @@ -197,11 +235,11 @@ impl PsdFile { let mut data = Vec::new(); data.try_reserve_exact(source.len()).map_err(|_| PsdError::LimitExceeded("not enough memory to retain the damaged merged image"))?; data.extend_from_slice(source); - file.image_data = ImageData { compression, data }; + file.image_data = ImageData::new(compression, data); Some(Err(error)) } }; - Ok((file, merged)) + Ok(merged) } /// Reads the merged image into `self.image_data` and decodes it. A ZIP stream within the @@ -224,21 +262,31 @@ impl PsdFile { if decoded.is_err() { validate_planes(compression, data, &layout)?; } - self.image_data = ImageData { compression, data: data.to_vec() }; + self.image_data = ImageData::new(compression, data.to_vec()); r.skip(data.len())?; Ok(decoded) } /// Serializes the file. pub fn to_bytes(&self) -> Result> { - let v = self.header.version; + let (v, widen) = self.write_form(); let psb = v.is_psb(); // Reserve the pixel data up front: appending it to an empty Vec reallocates and copies // repeatedly. Everything else (headers, resources, records) is small by comparison. - let hint = self.layer_info.as_ref().map_or(0, LayerInfo::channel_data_len).saturating_add(self.image_data.data.len()); + let widened = if widen { self.rle_widening()? } else { 0 }; + let hint = self + .layer_info + .as_ref() + .map_or(0, LayerInfo::channel_data_len) + .saturating_add(usize::try_from(self.image_data.encoded_len()).unwrap_or(usize::MAX)) + .saturating_add(usize::try_from(widened).unwrap_or(usize::MAX)); let mut out = Vec::new(); out.try_reserve(hint).map_err(|_| PsdError::LimitExceeded("not enough memory to serialize the file"))?; - self.header.write(&mut out); + if widen { + Header { version: v, ..self.header.clone() }.write(&mut out); + } else { + self.header.write(&mut out); + } out.put_len(self.color_mode_data.len() as u64, false)?; out.put(&self.color_mode_data); resources::write_section(&mut out, &self.resources)?; @@ -253,7 +301,7 @@ impl PsdFile { if !section_empty { let li = out.begin_len(psb); if let (Some(info), false) = (&self.layer_info, in_block) { - info.write_body(&mut out, v)?; + info.write_body_as(&mut out, v, widen)?; } out.end_len(li, psb)?; let need_mask_field = self.global_layer_mask.is_some() || !self.global_blocks.is_empty() || (in_block && self.layer_info.is_some()); @@ -273,7 +321,7 @@ impl PsdFile { let long = uses_long_length(v, key); let at = out.begin_len(long); let start = out.len(); - info.write_body(&mut out, v)?; + info.write_body_as(&mut out, v, widen)?; let len = out.len() - start; out.end_len(at, long)?; match padding { @@ -289,10 +337,121 @@ impl PsdFile { out.end_len(at, psb)?; out.put_u16(self.image_data.compression.as_u16()); - out.put(&self.image_data.data); + match self.merged_widened_rows(widen)? { + 0 => out.put(&self.image_data.encoded()?), + rows => put_widened_rle(&mut out, &self.image_data.encoded()?, rows)?, + } Ok(out) } + /// The version the file is written as, and whether its RLE count tables widen on the way + /// (a PSD model marked [`Self::write_as_psb`]). + fn write_form(&self) -> (Version, bool) { + let widen = self.write_as_psb && !self.header.version.is_psb(); + (if widen { Version::Psb } else { self.header.version }, widen) + } + + /// The version [`Self::to_bytes`] writes: PSB for a PSD model marked [`Self::write_as_psb`], + /// else `header.version`. + pub fn written_version(&self) -> Version { + self.write_form().0 + } + + /// Rows of the merged image's RLE count table that widen on write (0 when nothing widens). + fn merged_widened_rows(&self, widen: bool) -> Result { + if widen && self.image_data.compression == Compression::Rle { ImageData::layout(&self.header).rows() } else { Ok(0) } + } + + /// Bytes every RLE count table of this PSD model grows by when written as PSB; an error + /// when a table is too short for its rows. + fn rle_widening(&self) -> Result { + let mut n = 0u64; + for layer in self.layers() { + for c in &layer.channels { + match layer.widened_rows(c, true)? { + 0 => {} + rows => n = n.saturating_add(rle_widening(&c.encoded()?, rows)?), + } + } + } + match self.merged_widened_rows(true)? { + 0 => Ok(n), + rows => Ok(n.saturating_add(rle_widening(&self.image_data.encoded()?, rows)?)), + } + } + + /// The number of bytes [`Self::to_bytes`] writes, computed without serializing the channel + /// data (only the layer records, which are small, are serialized to measure them). Lets a + /// writer check a file against the 2 GB PSD limit before choosing the version. + pub fn encoded_len(&self) -> Result { + let (v, widen) = self.write_form(); + let psb = v.is_psb(); + let len_field = |long: bool| if long { 8u64 } else { 4 }; + let blocks_len = |blocks: &[TaggedBlock]| blocks.iter().fold(0u64, |n, b| n.saturating_add(b.encoded_len_aligned(v, 4) as u64)); + let body_len = |info: &LayerInfo| -> Result { + let unpadded = info.unpadded_len_as(v, widen)?; + let padding = info.padding.as_ref().map_or(unpadded % 2, |p| p.len() as u64); + Ok(unpadded.saturating_add(padding)) + }; + // Header, colour mode data, resources. + let mut n = 26u64.saturating_add(4).saturating_add(self.color_mode_data.len() as u64).saturating_add(resources::section_len(&self.resources)); + + let in_block = matches!(self.layer_info_placement, LayerInfoPlacement::GlobalBlock { .. }); + let section_empty = + self.layer_info.is_none() && self.global_layer_mask.is_none() && self.global_blocks.is_empty() && self.layer_mask_trailing.is_empty(); + n = n.saturating_add(len_field(psb)); + if !section_empty { + n = n.saturating_add(len_field(psb)); + if let (Some(info), false) = (&self.layer_info, in_block) { + n = n.saturating_add(body_len(info)?); + } + let need_mask_field = self.global_layer_mask.is_some() || !self.global_blocks.is_empty() || (in_block && self.layer_info.is_some()); + if need_mask_field { + n = n.saturating_add(4).saturating_add(self.global_layer_mask.as_ref().map_or(0, |g| g.data.len() as u64)); + } + n = n.saturating_add(blocks_len(&self.global_blocks)); + if let (Some(info), LayerInfoPlacement::GlobalBlock { key, padding, .. }) = (&self.layer_info, &self.layer_info_placement) { + let body = body_len(info)?; + let pad = padding.as_ref().map_or((4 - body % 4) % 4, |p| p.len() as u64); + n = n.saturating_add(8).saturating_add(len_field(uses_long_length(v, key))).saturating_add(body).saturating_add(pad); + } + n = n.saturating_add(self.layer_mask_trailing.len() as u64); + } + let merged = match self.merged_widened_rows(widen)? { + 0 => 0, + rows => rle_widening(&self.image_data.encoded()?, rows)?, + }; + Ok(n.saturating_add(2).saturating_add(self.image_data.encoded_len()).saturating_add(merged)) + } + + /// Marks a PSD model to be written as the equivalent PSB ([`Self::write_as_psb`]) without + /// touching or re-encoding any pixels: the packed RLE rows are the same in both versions, so + /// [`Self::to_bytes`] only widens the row-count tables of the layer channels and the merged + /// image (two to four bytes per row) and the length fields while it copies them out. Raw and + /// ZIP channel data is the same in both. A PSB model is left as is. + /// + /// Every RLE count table is checked first: an error leaves the model unmarked, and the write + /// cannot fail on a table later. The layer info padding is kept; a writer that pads to an + /// alignment re-pads with [`Self::pad_layer_info_to`]. + pub fn promote_to_psb(&mut self) -> Result<()> { + if self.header.version.is_psb() || self.write_as_psb { + return Ok(()); + } + self.rle_widening()?; + self.write_as_psb = true; + Ok(()) + } + + /// Pads the layer info body to a multiple of `align` bytes as it will be written (see + /// [`LayerInfo::pad_to`]), RLE count tables widened when [`Self::write_as_psb`] is set. + pub fn pad_layer_info_to(&mut self, align: u64) -> Result<()> { + let (v, widen) = self.write_form(); + if let Some(info) = &mut self.layer_info { + info.padding = Some(vec![0; info.pad_len_as(v, widen, align)?]); + } + Ok(()) + } + /// Layer records in file order (bottom-most first); empty if none. pub fn layers(&self) -> &[LayerRecord] { self.layer_info.as_ref().map_or(&[], |i| &i.layers) @@ -352,9 +511,12 @@ impl PsdFile { } /// `true` if the merged image carries a transparency channel: its first extra channel, when - /// the layer count was stored negative or a "Saving Merged Transparency" global block (`Mtrn`, - /// `Mt16`, `Mt32`) is present. Photoshop marks 32-bit documents with `Mt32` and a positive - /// layer count (psd-tools transparentbg.psd). + /// the layer count was stored negative or a "Saving Merged Transparency" global block (see + /// [`TaggedBlock::is_merged_transparency_marker`]) is present. Photoshop marks 32-bit documents + /// with `Mt32` and a positive layer count (psd-tools transparentbg.psd). Photoshop also names + /// the merged transparency "Transparency" and lists it first among the alpha channel names, + /// and some of its 16-bit PSBs carry no other mark (#4654): when the layers sit in a global + /// `Lr16`/`Lr32` block, that name with one name per extra channel marks it too. pub fn merged_has_alpha(&self) -> bool { let Some(color) = self.header.color_mode.color_channels() else { return false; @@ -362,13 +524,42 @@ impl PsdFile { if self.header.channels <= color { return false; } - let marked = self.global_blocks.iter().any(|b| matches!(&b.key, b"Mtrn" | b"Mt16" | b"Mt32")); + let marked = self.global_blocks.iter().any(TaggedBlock::is_merged_transparency_marker); match &self.layer_info { - Some(li) => li.merged_alpha || marked, + Some(li) => { + let in_block = matches!(self.layer_info_placement, LayerInfoPlacement::GlobalBlock { .. }); + li.merged_alpha || marked || (in_block && self.names_merged_transparency(self.header.channels - color)) + } None => true, } } + /// `true` when the alpha channel names (resource 1045, else 1006) hold exactly `extra` names + /// and the first is "Transparency", Photoshop's name for the merged transparency. + fn names_merged_transparency(&self, extra: u16) -> bool { + const NAME: &str = "Transparency"; + let mut names = Vec::new(); + if let Some(r) = self.resource(1045) { + let mut at = 0usize; + while let Some(len) = r.data.get(at..at.saturating_add(4)).and_then(|b| b.try_into().ok()).map(u32::from_be_bytes) { + at = at.saturating_add(4); + let end = (len as usize).checked_mul(2).and_then(|n| at.checked_add(n)); + let Some(units) = end.and_then(|end| r.data.get(at..end)) else { break }; + let units: Vec = units.as_chunks::<2>().0.iter().map(|c| u16::from_be_bytes(*c)).collect(); + names.push(String::from_utf16_lossy(&units).trim_end_matches('\0').to_string()); + at = at.saturating_add(units.len() * 2); + } + } else if let Some(r) = self.resource(1006) { + let mut at = 0usize; + while let Some(&len) = r.data.get(at) { + let Some(name) = r.data.get(at + 1..at + 1 + usize::from(len)) else { break }; + names.push(String::from_utf8_lossy(name).into_owned()); + at += 1 + usize::from(len); + } + } + names.len() == usize::from(extra) && names.first().is_some_and(|n| n == NAME) + } + /// Checks structural invariants that the writer relies on. pub fn validate(&self) -> Result<()> { self.header.validate()?; @@ -400,6 +591,89 @@ mod write_tests { use super::*; use crate::{Compression, testgen}; + /// `encoded_len` is exactly what `to_bytes` writes, for every generated file and both layer + /// info placements, with and without padding (#5285). + #[test] + fn encoded_len_matches_the_written_length() { + for case in testgen::all_cases() { + assert_eq!(case.file.encoded_len().unwrap(), case.file.to_bytes().unwrap().len() as u64, "{}", case.name); + } + for version in [Version::Psd, Version::Psb] { + for key in [*b"Lr16", *b"Layr"] { + for padding in [None, Some(vec![]), Some(vec![1, 2, 3])] { + for body_padding in [None, Some(vec![]), Some(vec![9])] { + let mut file = testgen::small(version, Compression::Rle); + file.global_blocks = vec![TaggedBlock::new(*b"zz01", vec![11]), TaggedBlock::new(*b"lnk2", vec![1; 5])]; + file.layer_info_placement = LayerInfoPlacement::GlobalBlock { index: 1, signature: *b"8BIM", key, padding: padding.clone() }; + file.layer_info.as_mut().unwrap().padding = body_padding; + file.layer_mask_trailing = vec![0; 3]; + file.resources.push(ImageResource { name: b"odd".to_vec(), ..ImageResource::new(4000, vec![1, 2, 3]) }); + assert_eq!(file.encoded_len().unwrap(), file.to_bytes().unwrap().len() as u64, "{version:?} {key:?}"); + } + } + } + } + let mut empty = testgen::small(Version::Psd, Compression::Raw); + empty.layer_info = None; + empty.global_layer_mask = None; + empty.global_blocks.clear(); + assert_eq!(empty.encoded_len().unwrap(), empty.to_bytes().unwrap().len() as u64); + } + + /// A promoted PSD writes exactly the PSB written directly from the same pixels, and reads + /// back as that PSB; the model itself stays a PSD that every reader decodes as before (#5285). + #[test] + fn promote_to_psb_writes_the_psb_written_directly() { + let cases = testgen::all_cases(); + let (psd, psb) = cases.split_at(cases.len() / 2); + for (psd, psb) in psd.iter().zip(psb) { + assert_eq!(psd.file.header.version, Version::Psd, "{}", psd.name); + let mut promoted = psd.file.clone(); + promoted.promote_to_psb().unwrap(); + assert!(promoted.write_as_psb); + assert_eq!(promoted.written_version(), Version::Psb); + let bytes = promoted.to_bytes().unwrap(); + assert!(bytes == psb.file.to_bytes().unwrap(), "{}", psd.name); + assert_eq!(promoted.encoded_len().unwrap(), bytes.len() as u64, "{}", psd.name); + assert_eq!(PsdFile::from_bytes(&bytes).unwrap(), psb.file, "{}", psd.name); + // Unchanged as a model: same header, same (two-byte) channel data. + assert_eq!(PsdFile { write_as_psb: false, ..promoted.clone() }, psd.file, "{}", psd.name); + assert_eq!(promoted.decode_merged().ok(), psd.file.decode_merged().ok(), "{}", psd.name); + } + } + + /// Re-padding a promoted model pads what is written, as `pad_to` does for a PSB. + #[test] + fn pad_layer_info_to_pads_the_written_body() { + let mut promoted = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); + promoted.promote_to_psb().unwrap(); + promoted.pad_layer_info_to(4).unwrap(); + let mut direct = testgen::layered(Version::Psb, ColorMode::Rgb, 8, Compression::Rle); + direct.layer_info.as_mut().unwrap().pad_to(Version::Psb, 4).unwrap(); + assert!(promoted.to_bytes().unwrap() == direct.to_bytes().unwrap()); + } + + /// A damaged RLE table is refused before anything changes: the file stays a valid PSD. + #[test] + fn promote_to_psb_is_atomic() { + let mut file = testgen::small(Version::Psd, Compression::Rle); + // The last layer with pixels ("b", two rows): its red channel loses its row-count table. + let layers = &mut file.layer_info.as_mut().unwrap().layers; + let red = layers[2].channels.iter_mut().find(|c| c.id == 0).unwrap(); + assert_eq!(red.compression, Some(Compression::Rle)); + red.data.truncate(1); + let before = file.clone(); + assert!(file.promote_to_psb().is_err()); + assert_eq!(file, before); + assert!(!file.write_as_psb); + // A table damaged after promotion fails the write instead of panicking. + let mut file = testgen::small(Version::Psd, Compression::Rle); + file.promote_to_psb().unwrap(); + file.image_data.data.truncate(3); + assert!(file.to_bytes().is_err()); + assert!(file.encoded_len().is_err()); + } + #[test] fn global_blocks_default_to_four_byte_padding() { for version in [Version::Psd, Version::Psb] { @@ -551,4 +825,27 @@ mod write_tests { } } } + + /// A positive layer count plus a merged-transparency marker still makes the first extra + /// channel the transparency, for the specification's keys and for the `MTrn` Photoshop + /// writes in a layered TIFF. + #[test] + fn merged_transparency_markers_mark_the_first_extra_channel() { + let mut file = testgen::small(Version::Psd, Compression::Raw); + if let Some(li) = &mut file.layer_info { + li.merged_alpha = false; + } + assert!(!file.merged_has_alpha()); + for key in [*b"Mtrn", *b"MTrn", *b"Mt16", *b"Mt32"] { + let mut marked = file.clone(); + marked.global_blocks.push(TaggedBlock::new(key, Vec::new())); + assert!(marked.merged_has_alpha(), "{}", String::from_utf8_lossy(&key)); + } + // Other keys, and the marker's letters in another case, do not. + for key in [*b"MTRN", *b"mtrn", *b"Lr16"] { + let mut other = file.clone(); + other.global_blocks.push(TaggedBlock::new(key, Vec::new())); + assert!(!other.merged_has_alpha(), "{}", String::from_utf8_lossy(&key)); + } + } } diff --git a/crates/psd/src/filter_effects.rs b/crates/psd/src/filter_effects.rs index e8b5efb698..cadb3ac8d0 100644 --- a/crates/psd/src/filter_effects.rs +++ b/crates/psd/src/filter_effects.rs @@ -170,7 +170,10 @@ impl FilterEffects { let pad = ((4 - len % 4) % 4).min(r.remaining() as u64); r.skip(pad as usize)?; let rest = r.peek_rest(); - if rest.iter().all(|&b| b == 0) && rest.len() <= 3 { + // Padding is at most three bytes. Check the length before scanning the suffix for + // zeros: otherwise each iteration scans the whole remaining buffer, which is + // quadratic when a block contains many empty sections. + if rest.len() <= 3 && rest.iter().all(|&b| b == 0) { break; } if rest.len() < 8 { diff --git a/crates/psd/src/fixtures.rs b/crates/psd/src/fixtures.rs new file mode 100644 index 0000000000..a8bd3d43c9 --- /dev/null +++ b/crates/psd/src/fixtures.rs @@ -0,0 +1,566 @@ +//! Large-file stress fixtures (#216, #375, #5107), generated at test time and never committed. +//! +//! Two kinds: +//! +//! * [`Streamed`]: multi-gigabyte PSD/PSB files written straight from the Adobe PSD spec, row by +//! row, independent of [`PsdBuilder`] (so they don't inherit its limits, and the generator needs +//! a few megabytes of memory however large the file). One full-canvas layer (or none) plus the +//! merged image; every sample follows [`sample`], so a reader can check any pixel without the +//! file. [`PSD_2GB`] is the largest file Photoshop saves as PSD; [`PSB_CASES`] are 2.5–5 GB. +//! * [`Grid`]: layered documents built with [`PsdBuilder`]: a background and a grid of layers in +//! groups, with masks, a hidden layer, a closed group and drop shadows. [`LAYERED_20MP`] is the +//! 20 MP photo-sized document, [`LAYOUT_150`] the 150-layer A4 layout. [`Grid::expected`] is +//! the merged colour at any point away from the shadows ([`Grid::sample_points`] lists some). +//! +//! Photoshop's limits, which the presets respect: a PSD is at most 30,000 px a side and 2 GB; a +//! PSB at most 300,000 px a side. + +use std::io::{self, Write}; + +use crate::builder::{GroupSpec, LayerSpec, MaskSpec, PixelData, PsdBuilder}; +use crate::descriptor::{Descriptor, Id, Value, VersionedDescriptor}; +use crate::error::Result; +use crate::file::PsdFile; +use crate::header::Version; +use crate::layer::Rect; +use crate::tagged::TaggedBlock; + +/// Photoshop's PSD size cap: files of 2 GB or more must be PSB. +pub const PSD_MAX_FILE_BYTES: u64 = 2 * 1024 * 1024 * 1024; +/// Photoshop's PSD side cap (px). +pub const PSD_MAX_SIDE: u32 = 30_000; + +/// The 8-bit sample of channel `c` (0..3 colour, 3 = alpha) at (`x`, `y`) in a [`Streamed`] file. +/// RLE files ignore `x` (constant rows compress to almost nothing). 16-bit files store `v * 257`. +pub fn sample(x: u32, y: u32, c: u32, rle: bool) -> u8 { + if c == 3 { + return 255; + } + let x = if rle { 0 } else { x }; + (x.wrapping_mul(7).wrapping_add(y.wrapping_mul(13)).wrapping_add(c.wrapping_mul(101)) & 0xff) as u8 +} + +/// A large RGB file written row by row. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Streamed { + /// File stem, e.g. `layer8-25k-raw`. + pub name: &'static str, + /// PSD (v1) or PSB (v2). + pub version: Version, + /// Width (px). + pub width: u32, + /// Height (px). + pub height: u32, + /// 8 or 16. + pub depth: u16, + /// RLE (PackBits) or raw. + pub rle: bool, + /// One full-canvas RGBA layer, or a flattened file (merged image only). + pub layered: bool, +} + +const fn psb(name: &'static str, side: u32, depth: u16, rle: bool, layered: bool) -> Streamed { + Streamed { name, version: Version::Psb, width: side, height: side, depth, rle, layered } +} + +/// A 2.02 GB layered PSD just under Photoshop's 2 GB cap: 17,000² px, one raw full-canvas layer +/// (289 MB per channel) and the raw merged image. +pub const PSD_2GB: Streamed = + Streamed { name: "psd-layer8-17k-raw", version: Version::Psd, width: 17_000, height: 17_000, depth: 8, rle: false, layered: true }; + +/// The 4–5 GB PSB generated for #375 and #216. +pub const PSB_CASES: [Streamed; 5] = [ + // Composite of 2.52 GB in one decode. + psb("flat8-29k-rle", 29_000, 8, true, false), + // A 4.8 GB file: offsets past 4 GiB, composite of 4.8 GB. + psb("flat8-40k-raw", 40_000, 8, false, false), + // 4.38 GB file: layer channels of 625 MB, composite of 1.9 GB, data past 4 GiB. + PSB_4GB, + // Layer channels of 900 MB, composite of 2.7 GB. + psb("layer8-30k-rle", 30_000, 8, true, true), + // Layer channels of 2.18 GB each. + psb("layer16-33k-rle", 33_000, 16, true, true), +]; + +/// The layered 4.38 GB PSB (25,000² px, raw): layer data and the merged image past 4 GiB. +pub const PSB_4GB: Streamed = psb("layer8-25k-raw", 25_000, 8, false, true); + +/// PackBits for a row of `n` equal bytes: runs of 128, then the remainder. +fn rle_row(n: u64, b: u8) -> Vec { + let mut v = Vec::new(); + let mut left = n; + while left > 0 { + let run = left.min(128); + // `run` is 1..=128, so 257 - run is 129..=256 and only 256 (run 1) needs the literal form. + if run == 1 { + v.extend_from_slice(&[0, b]); + } else { + v.extend_from_slice(&[(257 - run) as u8, b]); + } + left -= run; + } + v +} + +fn too_big(what: &str) -> io::Error { + io::Error::new(io::ErrorKind::InvalidInput, format!("{what} does not fit a PSD (v1) length field")) +} + +impl Streamed { + /// File name with the right extension. + pub fn file_name(&self) -> String { + format!("{}.{}", self.name, if self.version == Version::Psb { "psb" } else { "psd" }) + } + + fn psb(&self) -> bool { + self.version == Version::Psb + } + + fn row_bytes(&self) -> u64 { + u64::from(self.width) * u64::from(self.depth / 8) + } + + /// Bytes of one RLE row (all rows have the same length). + fn rle_len(&self) -> u64 { + rle_row(self.row_bytes(), 0).len() as u64 + } + + /// Size of a channel block (compression word, RLE counts, rows) of `planes` planes. + fn channel_len(&self, planes: u64) -> u64 { + let rows = planes * u64::from(self.height); + let count = if self.psb() { 4 } else { 2 }; + if self.rle { 2 + rows * count + rows * self.rle_len() } else { 2 + rows * self.row_bytes() } + } + + /// Writes a length field: 8 bytes in PSB, 4 in PSD. + fn put_len(&self, out: &mut Vec, v: u64, what: &str) -> io::Result<()> { + if self.psb() { + out.extend_from_slice(&v.to_be_bytes()); + } else { + out.extend_from_slice(&u32::try_from(v).map_err(|_| too_big(what))?.to_be_bytes()); + } + Ok(()) + } + + /// The layer record of the one layer: transparency (-1), then R, G, B. + fn layer_record(&self) -> io::Result> { + let ch_len = self.channel_len(1); + let name = b"Layer 1"; + let mut rec = Vec::new(); + for v in [0, 0, self.height, self.width] { + rec.extend_from_slice(&v.to_be_bytes()); + } + rec.extend_from_slice(&4u16.to_be_bytes()); + for id in [-1i16, 0, 1, 2] { + rec.extend_from_slice(&id.to_be_bytes()); + self.put_len(&mut rec, ch_len, "a layer channel")?; + } + rec.extend_from_slice(b"8BIMnorm"); + rec.extend_from_slice(&[255, 0, 0, 0]); + // Extra data: no mask, no blending ranges, the Pascal name padded to 4. + let mut extra = Vec::new(); + extra.extend_from_slice(&0u32.to_be_bytes()); + extra.extend_from_slice(&0u32.to_be_bytes()); + extra.push(name.len() as u8); + extra.extend_from_slice(name); + while extra.len() % 4 != 0 { + extra.push(0); + } + rec.extend_from_slice(&(extra.len() as u32).to_be_bytes()); + rec.extend_from_slice(&extra); + Ok(rec) + } + + /// The header, the empty colour-mode and resource sections and the layer section's head + /// (everything before the first channel's data), plus the padding after the layer channels. + fn head(&self) -> io::Result<(Vec, u64)> { + let mut h = Vec::new(); + h.extend_from_slice(b"8BPS"); + h.extend_from_slice(&(if self.psb() { 2u16 } else { 1 }).to_be_bytes()); + h.extend_from_slice(&[0; 6]); + h.extend_from_slice(&3u16.to_be_bytes()); + h.extend_from_slice(&self.height.to_be_bytes()); + h.extend_from_slice(&self.width.to_be_bytes()); + h.extend_from_slice(&self.depth.to_be_bytes()); + h.extend_from_slice(&3u16.to_be_bytes()); + // Colour mode data and image resources: empty. + h.extend_from_slice(&0u32.to_be_bytes()); + h.extend_from_slice(&0u32.to_be_bytes()); + if !self.layered { + self.put_len(&mut h, 0, "the layer section")?; + return Ok((h, 0)); + } + let rec = self.layer_record()?; + let mut layer_info = 2 + rec.len() as u64 + 4 * self.channel_len(1); + let pad = layer_info % 2; + layer_info += pad; + let len_field = if self.psb() { 8 } else { 4 }; + // Layer and mask information: layer info (with its length) + empty global mask (4 bytes). + self.put_len(&mut h, len_field + layer_info + 4, "the layer section")?; + self.put_len(&mut h, layer_info, "the layer info")?; + h.extend_from_slice(&1i16.to_be_bytes()); + h.extend_from_slice(&rec); + Ok((h, pad)) + } + + /// The exact size of the file [`write_to`](Self::write_to) writes, without writing it. + pub fn file_len(&self) -> io::Result { + let (head, pad) = self.head()?; + let layers = if self.layered { 4 * self.channel_len(1) + pad + 4 } else { 0 }; + Ok(head.len() as u64 + layers + self.channel_len(3)) + } + + /// The bytes of one row of channel `ch`. + fn row(&self, y: u32, ch: u32, buf: &mut Vec) { + buf.clear(); + if self.rle { + buf.extend(rle_row(self.row_bytes(), sample(0, y, ch, true))); + return; + } + for x in 0..self.width { + let s = sample(x, y, ch, false); + buf.push(s); + if self.depth == 16 { + buf.push(s); + } + } + } + + /// Writes the compression word, the counts and the rows of `chans`. + fn write_planes(&self, w: &mut impl Write, chans: &[u32]) -> io::Result<()> { + w.write_all(&u16::from(self.rle).to_be_bytes())?; + if self.rle { + let n = self.rle_len(); + let one = if self.psb() { + u32::try_from(n).map_err(|_| too_big("an RLE row"))?.to_be_bytes().to_vec() + } else { + u16::try_from(n).map_err(|_| too_big("an RLE row"))?.to_be_bytes().to_vec() + }; + for _ in 0..chans.len() as u64 * u64::from(self.height) { + w.write_all(&one)?; + } + } + let mut buf = Vec::new(); + for &ch in chans { + for y in 0..self.height { + self.row(y, ch, &mut buf); + w.write_all(&buf)?; + } + } + Ok(()) + } + + /// Streams the file into `w` (buffer it). Returns the bytes written. A PSD whose lengths + /// don't fit its 32-bit fields is refused before anything is written. + pub fn write_to(&self, w: &mut impl Write) -> io::Result { + let len = self.file_len()?; + let (head, pad) = self.head()?; + w.write_all(&head)?; + if self.layered { + // Transparency, then R, G, B. + for ch in [3, 0, 1, 2] { + self.write_planes(w, &[ch])?; + } + w.write_all(&vec![0; pad as usize])?; + w.write_all(&0u32.to_be_bytes())?; + } + self.write_planes(w, &[0, 1, 2])?; + w.flush()?; + Ok(len) + } + + /// Writes the file to `path` (created or truncated). Returns its size. + #[cfg(not(target_arch = "wasm32"))] + pub fn write_file(&self, path: &std::path::Path) -> io::Result { + let mut w = io::BufWriter::with_capacity(8 << 20, std::fs::File::create(path)?); + self.write_to(&mut w) + } + + /// The 8-bit value of channel `c` (3 = alpha) at (`x`, `y`): the layer's and the merged + /// image's (they are the same). + pub fn sample(&self, x: u32, y: u32, c: u32) -> u8 { + sample(x, y, c, self.rle) + } +} + +/// A layered document: a background and a grid of layers, one group per grid row. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Grid { + /// Canvas width (px). + pub width: u32, + /// Canvas height (px). + pub height: u32, + /// Layers in the grid (the background is extra). + pub layers: u32, + /// Grid columns; a group per row of `cols` layers. + pub cols: u32, +} + +/// A 20 MP (5472 × 3648) layered photo: background, 5 layers in 2 groups (one closed), a +/// mask and a drop shadow. +pub const LAYERED_20MP: Grid = Grid { width: 5472, height: 3648, layers: 5, cols: 3 }; + +/// A 150-layer A4 layout at 300 ppi (2480 × 3508): background + 149 layers in 15 groups, with +/// masks, hidden layers, closed groups and drop shadows. +pub const LAYOUT_150: Grid = Grid { width: 2480, height: 3508, layers: 149, cols: 10 }; + +/// What a grid layer carries. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct GridLayer { + /// Bounds in document coordinates. + pub rect: Rect, + /// Base colour; the layer adds its local x to it (a horizontal ramp). + pub base: [u8; 3], + /// Has a mask showing only the left half. + pub masked: bool, + /// Hidden. + pub hidden: bool, + /// Has a drop shadow (`lfx2`). + pub shadow: bool, +} + +/// The `lfx2` descriptor of a drop shadow: multiply, black, 75 %, 120°, distance 5, size 5. +fn drop_shadow() -> Vec { + let px = |v: f64| Value::UnitFloat { unit: *b"#Pxl", value: v }; + let color = Descriptor::new("RGBC").with("Rd ", Value::Double(0.0)).with("Grn ", Value::Double(0.0)).with("Bl ", Value::Double(0.0)); + let shadow = Descriptor::new("DrSh") + .with("enab", Value::Boolean(true)) + .with("Md ", Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new("Mltp") }) + .with("Clr ", Value::Descriptor(color)) + .with("Opct", Value::UnitFloat { unit: *b"#Prc", value: 75.0 }) + .with("uglg", Value::Boolean(false)) + .with("lagl", Value::UnitFloat { unit: *b"#Ang", value: 120.0 }) + .with("Dstn", px(5.0)) + .with("Ckmt", px(0.0)) + .with("blur", px(5.0)); + let root = Descriptor::new("null") + .with("Scl ", Value::UnitFloat { unit: *b"#Prc", value: 100.0 }) + .with("masterFXSwitch", Value::Boolean(true)) + .with("DrSh", Value::Descriptor(shadow)); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor::new(root).to_bytes()); + data +} + +impl Grid { + /// Grid rows (= groups). + pub fn rows(&self) -> u32 { + self.layers.div_ceil(self.cols.max(1)) + } + + fn cell(&self) -> (u32, u32) { + (self.width / self.cols.max(1), self.height / self.rows().max(1)) + } + + /// Gap between a layer and its cell's edge (larger than any shadow's reach). + fn inset(&self) -> u32 { + let (cw, ch) = self.cell(); + (cw.min(ch) / 8).max(1) + } + + /// Layer `i` of the grid (0-based, bottom to top), or `None` past the end. + pub fn layer(&self, i: u32) -> Option { + if i >= self.layers { + return None; + } + let (cw, ch) = self.cell(); + let inset = self.inset(); + let (col, row) = (i % self.cols.max(1), i / self.cols.max(1)); + let rect = + Rect::from_xywh((col * cw + inset) as i32, (row * ch + inset) as i32, cw.saturating_sub(2 * inset).max(1), ch.saturating_sub(2 * inset).max(1)); + let masked = i % 5 == 1; + let hidden = i % 13 == 12; + Some(GridLayer { + rect, + base: [(i * 37 + 20) as u8, (i * 91 + 50) as u8, (i * 53 + 90) as u8], + masked, + hidden, + shadow: i % 7 == 3 && !masked && !hidden, + }) + } + + /// Name of grid layer `i`. + pub fn layer_name(i: u32) -> String { + format!("Layer {}", i + 1) + } + + /// Name of the group of grid row `r`. + pub fn group_name(r: u32) -> String { + format!("Row {}", r + 1) + } + + /// Groups of rows `r % 4 == 3` are closed in the layers panel. + pub fn group_open(r: u32) -> bool { + r % 4 != 3 + } + + /// The background's colour at (`x`, `y`): [`sample`] in runs of 16 px (RLE has runs to find). + pub fn background(x: u32, y: u32) -> [u8; 3] { + [0, 1, 2].map(|c| sample(x / 16, y, c, false)) + } + + /// Grid layer `i`'s pixel colour at document (`x`, `y`), ignoring visibility and mask. + fn layer_color(l: &GridLayer, x: u32) -> [u8; 3] { + let lx = x.wrapping_sub(l.rect.left as u32); + l.base.map(|b| (u32::from(b) + lx) as u8) + } + + /// The merged colour at (`x`, `y`). Exact except within a few pixels of a shadowed layer's + /// edges (the merged image this generator stores leaves shadows out). + pub fn expected(&self, x: u32, y: u32) -> [u8; 3] { + let (cw, ch) = self.cell(); + let (col, row) = (x / cw.max(1), y / ch.max(1)); + if col < self.cols + && let Some(l) = self.layer(row * self.cols + col) + { + let (xi, yi) = (x as i32, y as i32); + let inside = xi >= l.rect.left && xi < l.rect.right && yi >= l.rect.top && yi < l.rect.bottom; + let shown = !l.masked || i64::from(xi) < i64::from(l.rect.left) + l.rect.width() / 2; + if inside && shown && !l.hidden { + return Self::layer_color(&l, x); + } + } + Self::background(x, y) + } + + /// Points to check, away from every shadow: in each of a few layers, its shown half, its + /// masked-out half, and the gap at its cell's corner. + pub fn sample_points(&self) -> Vec<(u32, u32)> { + let step = (self.layers / 12).max(1); + let mut v = vec![(0, 0), (self.width - 1, self.height - 1)]; + for i in (0..self.layers).step_by(step as usize) { + let Some(l) = self.layer(i) else { continue }; + let (w, h) = (l.rect.width() as u32, l.rect.height() as u32); + let (x0, y0) = (l.rect.left as u32, l.rect.top as u32); + v.push((x0 + w / 4, y0 + h / 2)); + v.push((x0 + w * 3 / 4, y0 + h / 2)); + v.push((x0 - self.inset() / 2, y0 - self.inset() / 2)); + } + v + } + + /// Builds the file (8-bit RGB, RLE): layers bottom to top, the merged image from + /// [`expected`](Self::expected). + pub fn build(&self, version: Version) -> Result { + let (w, h) = (self.width, self.height); + let mut b = PsdBuilder::new(w, h).version(version); + let mut bg = Vec::with_capacity(w as usize * h as usize * 4); + for y in 0..h { + for x in 0..w { + bg.extend(Self::background(x, y)); + bg.push(255); + } + } + b.push_layer(LayerSpec::new("Background", 0, 0, w, h, PixelData::Rgba8(bg))); + for row in 0..self.rows() { + b.begin_group(GroupSpec { open: Self::group_open(row), ..GroupSpec::new(Self::group_name(row)) }); + for i in row * self.cols..((row + 1) * self.cols).min(self.layers) { + let Some(l) = self.layer(i) else { continue }; + let (lw, lh) = (l.rect.width() as u32, l.rect.height() as u32); + let mut px = Vec::with_capacity(lw as usize * lh as usize * 4); + for _ in 0..lh { + for lx in 0..lw { + px.extend(Self::layer_color(&l, l.rect.left as u32 + lx)); + px.push(255); + } + } + let mut spec = LayerSpec::new(Self::layer_name(i), l.rect.left, l.rect.top, lw, lh, PixelData::Rgba8(px)); + spec.visible = !l.hidden; + if l.masked { + let data = (0..lh).flat_map(|_| (0..lw).map(move |x| if x < lw / 2 { 255 } else { 0 })).collect(); + spec.mask = Some(MaskSpec { rect: l.rect, data, default_color: 0, disabled: false }); + } + if l.shadow { + spec.extra_blocks.push(TaggedBlock::new(*b"lfx2", drop_shadow())); + } + b.push_layer(spec); + } + b.end_group()?; + } + let mut merged = Vec::with_capacity(w as usize * h as usize * 4); + for y in 0..h { + for x in 0..w { + merged.extend(self.expected(x, y)); + merged.push(255); + } + } + b.composite(PixelData::Rgba8(merged)); + b.build() + } +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::indexing_slicing)] +mod tests { + use super::*; + + #[test] + fn presets_respect_photoshop_limits() { + let psd = PSD_2GB.file_len().unwrap(); + assert!(psd < PSD_MAX_FILE_BYTES && psd > 2_000_000_000, "{psd}"); + const { assert!(PSD_2GB.width <= PSD_MAX_SIDE && PSD_2GB.height <= PSD_MAX_SIDE) }; + for c in PSB_CASES { + assert!(c.file_len().is_ok() && c.width <= 300_000, "{}", c.name); + } + assert!(PSB_4GB.file_len().unwrap() > 4 << 30); + // A PSD too big for its 32-bit lengths is refused, not wrapped. + let huge = Streamed { width: 33_000, height: 33_000, ..PSD_2GB }; + assert!(huge.file_len().is_err()); + assert!(huge.write_to(&mut io::sink()).is_err()); + } + + /// Small streamed files parse, and their pixels are [`sample`]'s. + #[test] + fn small_streamed_files_parse() { + for version in [Version::Psd, Version::Psb] { + for (rle, layered, depth) in [(false, true, 8), (true, true, 8), (true, false, 16), (false, false, 8)] { + let c = Streamed { name: "t", version, width: 300, height: 7, depth, rle, layered }; + let mut out = Vec::new(); + let n = c.write_to(&mut out).unwrap(); + assert_eq!(n, out.len() as u64, "{c:?}"); + let file = PsdFile::from_bytes(&out).unwrap(); + let merged = file.image_data.decode(&file.header).unwrap(); + let bps = usize::from(depth / 8); + let plane = 300 * 7 * bps; + for (x, y, ch) in [(0u32, 0u32, 0u32), (299, 6, 2), (150, 3, 1)] { + let i = ch as usize * plane + (y as usize * 300 + x as usize) * bps; + assert_eq!(merged[i], c.sample(x, y, ch), "{c:?} ({x}, {y}) {ch}"); + } + assert_eq!(file.layers().len(), usize::from(layered), "{c:?}"); + } + } + } + + #[test] + fn grid_layout_is_consistent() { + for g in [LAYERED_20MP, LAYOUT_150] { + let last = g.layer(g.layers - 1).unwrap(); + assert!(last.rect.right <= g.width as i32 && last.rect.bottom <= g.height as i32); + assert!(g.layer(g.layers).is_none()); + let all: Vec<_> = (0..g.layers).filter_map(|i| g.layer(i)).collect(); + assert!(all.iter().any(|l| l.masked) && all.iter().any(|l| l.shadow)); + for (x, y) in g.sample_points() { + assert!(x < g.width && y < g.height); + } + } + assert!((0..LAYOUT_150.layers).filter_map(|i| LAYOUT_150.layer(i)).any(|l| l.hidden)); + } + + /// A small grid builds, parses, and its merged image is [`Grid::expected`]. + #[test] + fn small_grid_builds() { + let g = Grid { width: 120, height: 90, layers: 14, cols: 4 }; + let file = g.build(Version::Psd).unwrap(); + let bytes = file.to_bytes().unwrap(); + let file = PsdFile::from_bytes(&bytes).unwrap(); + // Background + 14 layers + 4 groups (two records each). + assert_eq!(file.layers().len(), 1 + 14 + 8); + let merged = file.image_data.decode(&file.header).unwrap(); + for (x, y) in g.sample_points() { + let want = g.expected(x, y); + for c in 0..3 { + assert_eq!(merged[c * 120 * 90 + (y * 120 + x) as usize], want[c], "({x}, {y}) {c}"); + } + } + } +} diff --git a/crates/psd/src/image_data.rs b/crates/psd/src/image_data.rs index 864486b4ac..9e27af2917 100644 --- a/crates/psd/src/image_data.rs +++ b/crates/psd/src/image_data.rs @@ -1,13 +1,20 @@ //! Merged (composite) image data section. +use std::borrow::Cow; + use crate::compression::{Compression, PlaneLayout, decode_planes, encode_planes, validate_planes}; use crate::error::Result; use crate::header::Header; use crate::io::Reader; +use crate::source::Stored; /// The merged image: compression plus the encoded bytes of all channels /// (to the end of the file), kept verbatim. -#[derive(Debug, Clone, PartialEq, Eq)] +/// +/// As with [`crate::ChannelData`], a file parsed by +/// [`PsdFile::from_source`](crate::PsdFile::from_source) may leave the bytes in the source +/// (`stored`, with `data` empty); equality compares the bytes wherever they are. +#[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ImageData { /// Compression method for the whole image. @@ -15,9 +22,50 @@ pub struct ImageData { /// Encoded data. For RLE: the byte-count table for every row of every /// channel followed by the rows. pub data: Vec, + /// Where the encoded bytes are when they were left in the source. (Not serialized.) + #[cfg_attr(feature = "serde", serde(skip))] + pub stored: Option, } +impl PartialEq for ImageData { + fn eq(&self, other: &Self) -> bool { + if self.compression != other.compression || self.encoded_len() != other.encoded_len() { + return false; + } + match (self.encoded(), other.encoded()) { + (Ok(a), Ok(b)) => a == b, + _ => false, + } + } +} + +impl Eq for ImageData {} + impl ImageData { + /// A merged image whose encoded bytes are in memory. + pub fn new(compression: Compression, data: Vec) -> Self { + ImageData { compression, data, stored: None } + } + + /// The encoded bytes: borrowed from `data`, or read from the source. + pub fn encoded(&self) -> Result> { + crate::source::encoded(&self.data, self.stored.as_ref()) + } + + /// Number of encoded bytes, wherever they are. + pub fn encoded_len(&self) -> u64 { + self.stored.as_ref().map_or(self.data.len() as u64, |s| s.len) + } + + /// Reads bytes left in the source into `data`. + pub fn load(&mut self) -> Result<()> { + if let Some(s) = &self.stored { + self.data = s.read()?; + self.stored = None; + } + Ok(()) + } + /// Plane layout for the merged image described by `header`. pub fn layout(header: &Header) -> PlaneLayout { PlaneLayout { @@ -31,12 +79,12 @@ impl ImageData { /// Encodes planar data (`channels × height × row_bytes`). pub fn encode(compression: Compression, decoded: &[u8], header: &Header) -> Result { - Ok(ImageData { compression, data: encode_planes(compression, decoded, &Self::layout(header))? }) + Ok(ImageData::new(compression, encode_planes(compression, decoded, &Self::layout(header))?)) } /// Decodes all channels into planar big-endian samples. pub fn decode(&self, header: &Header) -> Result> { - decode_planes(self.compression, &self.data, &Self::layout(header)) + decode_planes(self.compression, &self.encoded()?, &Self::layout(header)) } pub(crate) fn read(r: &mut Reader<'_>, header: &Header) -> Result { @@ -45,6 +93,6 @@ impl ImageData { validate_planes(compression, data, &Self::layout(header))?; let data = data.to_vec(); r.skip(data.len())?; - Ok(ImageData { compression, data }) + Ok(ImageData::new(compression, data)) } } diff --git a/crates/psd/src/layer.rs b/crates/psd/src/layer.rs index 5b11411dfb..cbc4ccb424 100644 --- a/crates/psd/src/layer.rs +++ b/crates/psd/src/layer.rs @@ -1,10 +1,13 @@ //! Layer records, layer masks, blending ranges and per-layer channel data. +use std::borrow::Cow; + use crate::blend::BlendMode; -use crate::compression::{Compression, PlaneLayout, decode_planes, encode_planes}; +use crate::compression::{Compression, PlaneLayout, decode_planes, encode_planes, put_widened_rle, rle_widening}; use crate::error::{PsdError, Result}; use crate::header::{MAX_DIMENSION, Version}; use crate::io::{Reader, WriteExt, decode_legacy_name, read_pascal, write_pascal}; +use crate::source::Stored; use crate::tagged::{BlockData, SectionDivider, SectionType, TaggedBlock, read_blocks, uses_long_length, write_blocks}; /// Channel id of the transparency mask. @@ -368,7 +371,12 @@ impl BlendingRanges { } /// Encoded image data for one layer channel. -#[derive(Debug, Clone, PartialEq, Eq)] +/// +/// The encoded bytes are in `data`, or, for a file parsed by +/// [`PsdFile::from_source`](crate::PsdFile::from_source), still in the source at `stored` (and +/// `data` is empty). Every method here reads them from wherever they are; two channels are equal +/// when their bytes are, wherever each one keeps them. +#[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ChannelData { /// Channel id: 0.. color channels, -1 transparency, -2 user mask, @@ -377,22 +385,65 @@ pub struct ChannelData { /// Compression; `None` when the stored length was 0 (no compression /// field at all). pub compression: Option, - /// Encoded bytes following the compression field, kept verbatim. + /// Encoded bytes following the compression field, kept verbatim (empty while `stored`). pub data: Vec, + /// Where the encoded bytes are when they were left in the source: see [`Self::load`]. + /// (Not serialized: load the channel first.) + #[cfg_attr(feature = "serde", serde(skip))] + pub stored: Option, } +impl PartialEq for ChannelData { + fn eq(&self, other: &Self) -> bool { + if self.id != other.id || self.compression != other.compression || self.encoded_len() != other.encoded_len() { + return false; + } + match (self.encoded(), other.encoded()) { + (Ok(a), Ok(b)) => a == b, + _ => false, + } + } +} + +impl Eq for ChannelData {} + impl ChannelData { /// Encodes planar samples (`height × row_bytes`). pub fn encode(id: i16, compression: Compression, decoded: &[u8], width: usize, height: usize, depth: u16, version: Version) -> Result { let layout = PlaneLayout { planes: 1, width, height, depth, version }; - Ok(ChannelData { id, compression: Some(compression), data: encode_planes(compression, decoded, &layout)? }) + Ok(ChannelData::new(id, Some(compression), encode_planes(compression, decoded, &layout)?)) + } + + /// A channel whose encoded bytes are in memory. + pub fn new(id: i16, compression: Option, data: Vec) -> Self { + ChannelData { id, compression, data, stored: None } + } + + /// The encoded bytes (after the compression field): borrowed from `data`, or read from the + /// source when they were left there. + pub fn encoded(&self) -> Result> { + crate::source::encoded(&self.data, self.stored.as_ref()) + } + + /// Number of encoded bytes, wherever they are. + pub fn encoded_len(&self) -> u64 { + self.stored.as_ref().map_or(self.data.len() as u64, |s| s.len) + } + + /// Reads bytes left in the source into `data`, so the channel no longer needs its source. + pub fn load(&mut self) -> Result<()> { + if let Some(s) = &self.stored { + self.data = s.read()?; + self.stored = None; + } + Ok(()) } /// Decodes into planar big-endian samples. pub fn decode(&self, width: usize, height: usize, depth: u16, version: Version) -> Result> { let layout = PlaneLayout { planes: 1, width, height, depth, version }; match self.compression { - Some(c) => decode_planes(c, &self.data, &layout), + Some(c) => decode_planes(c, &self.encoded()?, &layout), None => { if layout.decoded_len()? == 0 { Ok(Vec::new()) @@ -412,7 +463,7 @@ impl ChannelData { fn stored_len(&self) -> u64 { match self.compression { - Some(_) => 2 + self.data.len() as u64, + Some(_) => 2 + self.encoded_len(), None => 0, } } @@ -556,7 +607,7 @@ impl LayerRecord { ch.decode(w, h, depth, version) } - fn read(r: &mut Reader<'_>, version: Version) -> Result<(Self, Vec)> { + pub(crate) fn read(r: &mut Reader<'_>, version: Version) -> Result<(Self, Vec)> { let rect = Rect::read(r)?; let nch = r.u16()?; if nch > MAX_LAYER_CHANNELS { @@ -567,7 +618,7 @@ impl LayerRecord { for _ in 0..nch { let id = r.i16()?; let len = r.len_field(version.is_psb())?; - channels.push(ChannelData { id, compression: None, data: Vec::new() }); + channels.push(ChannelData::new(id, None, Vec::new())); lens.push(len); } let sig = r.array::<4>()?; @@ -598,6 +649,12 @@ impl LayerRecord { Ok((LayerRecord { rect, channels, blend_mode, opacity, clipping, flags, filler, mask, blending_ranges, name, blocks, extra_trailing }, lens)) } + /// Rows of a channel's RLE count table that grow when a PSD model is written as PSB + /// (`widen`, see [`crate::PsdFile::promote_to_psb`]); 0 for other channels. + pub(crate) fn widened_rows(&self, c: &ChannelData, widen: bool) -> Result { + if widen && c.compression == Some(Compression::Rle) { Ok(self.channel_rect(c.id).size()?.1) } else { Ok(0) } + } + /// Bytes [`Self::write`] produces, computed without serializing. Errors where `write` would. fn encoded_len(&self, version: Version) -> Result { let too_big = || PsdError::LimitExceeded("section too large for 32-bit length (use PSB)"); @@ -628,13 +685,15 @@ impl LayerRecord { Ok((16 + 2 + self.channels.len() as u64 * (2 + channel_len) + 8 + 4 + 4).saturating_add(extra)) } - fn write(&self, out: &mut Vec, version: Version) -> Result<()> { + fn write(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { self.rect.write(out); let nch = u16::try_from(self.channels.len()).map_err(|_| PsdError::LimitExceeded("too many channels"))?; out.put_u16(nch); for c in &self.channels { out.put_i16(c.id); - out.put_len(c.stored_len(), version.is_psb())?; + let rows = self.widened_rows(c, widen)?; + let grown = if rows == 0 { 0 } else { rle_widening(&c.encoded()?, rows)? }; + out.put_len(c.stored_len().saturating_add(grown), version.is_psb())?; } out.put(b"8BIM"); out.put(&self.blend_mode.key()); @@ -709,14 +768,24 @@ impl LayerInfo { /// Length of the body (layer count, records and channel data) without /// its trailing padding. pub fn unpadded_len(&self, version: Version) -> Result { + self.unpadded_len_as(version, false) + } + + /// [`Self::unpadded_len`] of the body written with its RLE count tables widened (`widen`). + pub(crate) fn unpadded_len_as(&self, version: Version, widen: bool) -> Result { // Computed from the field sizes: serializing the records just to measure them would copy - // every tagged block. + // every tagged block. Widening changes channel data only: the record's length fields keep + // their width. let mut n = 2u64; for l in &self.layers { n = n.saturating_add(l.encoded_len(version)?); } - for c in self.layers.iter().flat_map(|l| &l.channels).filter(|c| c.compression.is_some()) { - n = n.saturating_add(2).saturating_add(c.data.len() as u64); + for l in &self.layers { + for c in l.channels.iter().filter(|c| c.compression.is_some()) { + let rows = l.widened_rows(c, widen)?; + let grown = if rows == 0 { 0 } else { rle_widening(&c.encoded()?, rows)? }; + n = n.saturating_add(2).saturating_add(c.encoded_len()).saturating_add(grown); + } } Ok(n) } @@ -732,29 +801,47 @@ impl LayerInfo { /// Number of zero bytes [`Self::pad_to`] would set, without touching `self`. pub(crate) fn pad_len(&self, version: Version, align: u64) -> Result { + self.pad_len_as(version, false, align) + } + + /// [`Self::pad_len`] for the body written with its RLE count tables widened (`widen`). + pub(crate) fn pad_len_as(&self, version: Version, widen: bool, align: u64) -> Result { let align = align.max(1); - let len = self.unpadded_len(version)?; + let len = self.unpadded_len_as(version, widen)?; Ok(((align - len % align) % align) as usize) } /// Bytes of channel data and their compression fields: the part of the body worth reserving up /// front (the records are small next to the pixels). Saturates instead of overflowing. pub(crate) fn channel_data_len(&self) -> usize { - self.layers.iter().flat_map(|l| &l.channels).filter(|c| c.compression.is_some()).fold(0usize, |n, c| n.saturating_add(2).saturating_add(c.data.len())) + self.layers + .iter() + .flat_map(|l| &l.channels) + .filter(|c| c.compression.is_some()) + .fold(0usize, |n, c| n.saturating_add(2).saturating_add(usize::try_from(c.encoded_len()).unwrap_or(usize::MAX))) } /// Serializes the body without any trailing padding (the padding field is ignored). pub(crate) fn write_unpadded(&self, out: &mut Vec, version: Version) -> Result<()> { + self.write_unpadded_as(out, version, false) + } + + /// [`Self::write_unpadded`], widening the RLE count tables of a PSD model written as PSB + /// (`widen`) on the way out. + fn write_unpadded_as(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { let n = i16::try_from(self.layers.len()).map_err(|_| PsdError::LimitExceeded("more than 32767 layers"))?; out.put_i16(if self.merged_alpha { -n } else { n }); for l in &self.layers { - l.write(out, version)?; + l.write(out, version, widen)?; } for l in &self.layers { for c in &l.channels { if let Some(comp) = c.compression { out.put_u16(comp.as_u16()); - out.put(&c.data); + match l.widened_rows(c, widen)? { + 0 => out.put(&c.encoded()?), + rows => put_widened_rle(out, &c.encoded()?, rows)?, + } } } } @@ -762,9 +849,15 @@ impl LayerInfo { } /// Serializes the body (without a length field). + #[cfg(test)] pub(crate) fn write_body(&self, out: &mut Vec, version: Version) -> Result<()> { + self.write_body_as(out, version, false) + } + + /// [`Self::write_body`], widening RLE count tables when `widen` (see [`Self::write_unpadded_as`]). + pub(crate) fn write_body_as(&self, out: &mut Vec, version: Version, widen: bool) -> Result<()> { let start = out.len(); - self.write_unpadded(out, version)?; + self.write_unpadded_as(out, version, widen)?; match &self.padding { Some(p) => out.put(p), None => { @@ -778,384 +871,4 @@ impl LayerInfo { } #[cfg(test)] -mod tests { - use super::*; - - fn sample_record(version: Version) -> LayerRecord { - let rect = Rect::from_xywh(-3, 2, 5, 4); - let px: Vec = (0..20).collect(); - let mut channels = Vec::new(); - for (id, comp) in [(-1, Compression::Rle), (0, Compression::Raw), (1, Compression::Zip), (2, Compression::ZipPrediction)] { - channels.push(ChannelData::encode(id, comp, &px, 5, 4, 8, version).unwrap()); - } - let mask = LayerMask::new(Rect::from_xywh(0, 0, 2, 2), 255, LayerMask::FLAG_RELATIVE); - channels.push(ChannelData::encode(-2, Compression::Rle, &[1, 2, 3, 4], 2, 2, 8, version).unwrap()); - LayerRecord { - rect, - channels, - blend_mode: BlendMode::Screen, - opacity: 200, - clipping: 1, - flags: LayerFlags(LayerFlags::HIDDEN), - mask: MaskData::Mask(mask), - blending_ranges: BlendingRanges::full(3), - name: b"abc".to_vec(), - blocks: vec![TaggedBlock::unicode_name("\u{e4}bc"), TaggedBlock::layer_id(5)], - ..Default::default() - } - } - - fn rt(info: &LayerInfo, version: Version) -> Vec { - let mut out = Vec::new(); - info.write_body(&mut out, version).unwrap(); - let back = LayerInfo::read_body(&out, version).unwrap(); - assert_eq!(&back, info); - let mut again = Vec::new(); - back.write_body(&mut again, version).unwrap(); - assert_eq!(again, out); - out - } - - #[test] - fn empty_layer_info() { - let out = rt(&LayerInfo::default(), Version::Psd); - assert_eq!(out, vec![0, 0]); - } - - #[test] - fn record_roundtrip_psd_psb() { - for v in [Version::Psd, Version::Psb] { - let info = LayerInfo { merged_alpha: true, layers: vec![sample_record(v), LayerRecord::default()], padding: None }; - let out = rt(&info, v); - assert_eq!(out.len() % 2, 0); - assert_eq!(&out[..2], &(-2i16).to_be_bytes()); - } - } - - #[test] - fn psb_channel_lengths_are_8_bytes() { - let mut rec = LayerRecord::default(); - rec.channels.push(ChannelData { id: 0, compression: Some(Compression::Raw), data: vec![] }); - let info = LayerInfo { layers: vec![rec], ..Default::default() }; - let psd = rt(&info, Version::Psd); - let psb = rt(&info, Version::Psb); - assert_eq!(psb.len(), psd.len() + 4); - } - - #[test] - fn channel_decode_via_record() { - let rec = sample_record(Version::Psd); - let px: Vec = (0..20).collect(); - for id in [-1, 0, 1, 2] { - assert_eq!(rec.decode_channel(id, 8, Version::Psd).unwrap(), px); - } - assert_eq!(rec.decode_channel(-2, 8, Version::Psd).unwrap(), vec![1, 2, 3, 4]); - assert!(rec.decode_channel(7, 8, Version::Psd).is_err()); - } - - #[test] - fn record_accessors() { - let rec = sample_record(Version::Psd); - assert_eq!(rec.name(), "\u{e4}bc"); - assert!(!rec.is_visible()); - assert_eq!(rec.layer_id(), Some(5)); - assert_eq!(rec.fill_opacity(), 255); - assert_eq!(rec.section_type(), SectionType::Other); - assert!(rec.layer_mask().is_some()); - assert_eq!(rec.channel_rect(-2), Rect::from_xywh(0, 0, 2, 2)); - assert_eq!(rec.channel_rect(0), rec.rect); - let mut plain = LayerRecord { name: b"legacy".to_vec(), ..Default::default() }; - assert_eq!(plain.name(), "legacy"); - plain.blocks.push(TaggedBlock::fill_opacity(10)); - assert_eq!(plain.fill_opacity(), 10); - plain.block_mut(b"iOpa").unwrap().data[0] = 11; - assert_eq!(plain.fill_opacity(), 11); - } - - #[test] - fn mask_variants_roundtrip() { - let base = LayerMask::new(Rect::from_xywh(1, 2, 3, 4), 0, 0); - let with_real = LayerMask { real: Some(RealMask { flags: 1, background: 255, rect: Rect::from_xywh(0, 0, 9, 9) }), trailing: vec![], ..base.clone() }; - let with_params = LayerMask { - flags: LayerMask::FLAG_PARAMETERS, - parameters: Some(MaskParameters { - flags: 0b1111, - user_density: Some(128), - user_feather: Some(2.5), - vector_density: Some(64), - vector_feather: Some(0.5), - }), - real: Some(RealMask::default()), - trailing: vec![0, 0], - ..base.clone() - }; - let partial_params = LayerMask { - flags: LayerMask::FLAG_PARAMETERS, - parameters: Some(MaskParameters { flags: 0b0010, user_feather: Some(1.0), ..Default::default() }), - trailing: vec![], - ..base.clone() - }; - for m in [base, with_real, with_params, partial_params] { - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(LayerMask::parse(&out).unwrap(), m); - } - } - - /// Photoshop stores the real-mask fields before the mask parameters (psd-tools - /// mask-density-layervectormask.psd): user + vector mask with both densities set. - #[test] - fn real_mask_before_parameters_photoshop_layout() { - let mut d = Vec::new(); - Rect::from_xywh(15, -1, 18, 10).write(&mut d); - d.extend([0, LayerMask::FLAG_PARAMETERS | 8]); - d.extend([0, 255]); - Rect::from_xywh(0, 0, 32, 8).write(&mut d); - d.extend([0b0101, 64, 64, 0]); - let m = LayerMask::parse(&d).unwrap(); - assert_eq!(m.real.map(|r| r.rect), Some(Rect::from_xywh(0, 0, 32, 8))); - assert_eq!(m.real.map(|r| r.background), Some(255)); - let p = m.parameters.unwrap(); - assert_eq!((p.user_density, p.vector_density), (Some(64), Some(64))); - assert_eq!(m.trailing, vec![0]); - assert!(m.real_first); - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(out, d); - } - - /// A density-only record in the spec's order (#949): the parameter flags byte at 18 says one - /// value byte, so the spec's reading excludes the 38-byte length only if the real mask is - /// ignored, while byte 36 (inside the real rectangle's `right`) is 0 and makes Photoshop's - /// reading fit too. Photoshop's reading puts the density (64) where a 0-or-255 background - /// belongs, so the spec's order wins and the density survives. - #[test] - fn density_only_spec_layout_is_not_read_as_photoshop_order() { - for (density, right) in [(64u8, 1u32), (200, 1), (64, 300), (1, 7)] { - let mut d = Vec::new(); - Rect::from_xywh(0, 0, 1, 1).write(&mut d); - d.extend([255, LayerMask::FLAG_PARAMETERS]); - d.extend([0b0001, density]); - d.extend([0, 0]); - Rect::from_xywh(0, 0, right, 1).write(&mut d); - let m = LayerMask::parse(&d).unwrap(); - assert!(!m.real_first, "density {density}, right {right}: {m:?}"); - assert_eq!(m.parameters.unwrap().user_density, Some(density)); - assert_eq!(m.real, Some(RealMask { flags: 0, background: 0, rect: Rect::from_xywh(0, 0, right, 1) })); - assert!(m.trailing.is_empty()); - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(out, d); - } - // Photoshop's order with the same lengths still reads as Photoshop's order. - let mut d = Vec::new(); - Rect::from_xywh(0, 0, 1, 1).write(&mut d); - d.extend([255, LayerMask::FLAG_PARAMETERS]); - d.extend([0, 255]); - Rect::from_xywh(0, 0, 1, 1).write(&mut d); - d.extend([0b0001, 64]); - let m = LayerMask::parse(&d).unwrap(); - assert!(m.real_first, "{m:?}"); - assert_eq!(m.parameters.unwrap().user_density, Some(64)); - assert_eq!(m.real.map(|r| r.background), Some(255)); - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(out, d); - } - - /// The spec's order (parameters, then real fields) still parses and stays byte-stable. - #[test] - fn real_mask_after_parameters_spec_layout() { - let mut d = Vec::new(); - Rect::from_xywh(1, 2, 3, 4).write(&mut d); - d.extend([255, LayerMask::FLAG_PARAMETERS]); - d.extend([0b0011, 200]); - d.extend(1.5f64.to_be_bytes()); - d.extend([0, 255]); - Rect::from_xywh(0, 0, 3, 3).write(&mut d); - let m = LayerMask::parse(&d).unwrap(); - assert!(!m.real_first); - assert_eq!(m.real.map(|r| r.rect), Some(Rect::from_xywh(0, 0, 3, 3))); - assert_eq!(m.parameters.unwrap().user_feather, Some(1.5)); - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(out, d); - // Parameters alone (no real mask) keep reading in the spec's order. - let mut d = Vec::new(); - Rect::from_xywh(1, 2, 3, 4).write(&mut d); - d.extend([255, LayerMask::FLAG_PARAMETERS, 0b1010]); - d.extend(1.0f64.to_be_bytes()); - d.extend(2.0f64.to_be_bytes()); - d.extend([0, 0]); - let m = LayerMask::parse(&d).unwrap(); - assert_eq!(m.real, None); - assert_eq!(m.parameters.map(|p| (p.user_feather, p.vector_feather)), Some((Some(1.0), Some(2.0)))); - } - - #[test] - fn mask_sizes() { - let mut out = Vec::new(); - LayerMask::new(Rect::default(), 0, 0).write(&mut out); - assert_eq!(out.len(), 20); - let m = LayerMask { real: Some(RealMask::default()), trailing: vec![], ..LayerMask::new(Rect::default(), 0, 0) }; - let mut out = Vec::new(); - m.write(&mut out); - assert_eq!(out.len(), 36); - } - - #[test] - fn short_mask_kept_raw() { - let mut rec = LayerRecord { mask: MaskData::Raw(vec![1, 2, 3]), ..Default::default() }; - rec.channels.clear(); - let info = LayerInfo { layers: vec![rec], ..Default::default() }; - rt(&info, Version::Psd); - } - - #[test] - fn flags_helpers() { - let mut f = LayerFlags::default(); - f.set_hidden(true); - f.set_transparency_protected(true); - assert!(f.hidden() && f.transparency_protected()); - f.set_pixel_data_irrelevant(true); - assert!(f.bit4_useful() && f.pixel_data_irrelevant()); - f.set_hidden(false); - assert!(!f.hidden()); - } - - #[test] - fn blending_ranges_parse() { - let br = BlendingRanges::full(3); - assert_eq!(br.data.len(), 32); - let r = br.ranges(); - assert_eq!(r.len(), 4); - assert_eq!(r[0].source, [0, 0, 255, 255]); - } - - #[test] - fn rect_helpers() { - let r = Rect::from_xywh(-5, -6, 10, 3); - assert_eq!((r.width(), r.height()), (10, 3)); - assert_eq!(r.size().unwrap(), (10, 3)); - assert!(!r.is_empty()); - let bad = Rect { top: 5, left: 0, bottom: 0, right: 1 }; - assert!(bad.size().is_err()); - assert!(bad.is_empty()); - let huge = Rect { top: 0, left: i32::MIN, bottom: 1, right: i32::MAX }; - assert!(huge.size().is_err()); - } - - #[test] - fn truncation_errors() { - let info = LayerInfo { layers: vec![sample_record(Version::Psd)], ..Default::default() }; - let mut out = Vec::new(); - info.write_body(&mut out, Version::Psd).unwrap(); - // The final byte may be the (optional) even padding; stop before it. - for cut in 0..out.len() - 1 { - assert!(LayerInfo::read_body(&out[..cut], Version::Psd).is_err(), "cut {cut}"); - } - } - - #[test] - fn bad_blend_signature() { - let info = LayerInfo { layers: vec![LayerRecord::default()], ..Default::default() }; - let mut out = Vec::new(); - info.write_body(&mut out, Version::Psd).unwrap(); - // count(2) + rect(16) + nch(2) = 20 → signature - out[20] = b'X'; - assert!(matches!(LayerInfo::read_body(&out, Version::Psd), Err(PsdError::InvalidSignature { .. }))); - } - - #[test] - fn too_many_layers_rejected() { - let out = 1000i16.to_be_bytes(); - assert!(LayerInfo::read_body(&out, Version::Psd).is_err()); - } - - #[test] - fn zero_length_channel() { - let mut rec = LayerRecord::default(); - rec.channels.push(ChannelData { id: 0, compression: None, data: vec![] }); - let info = LayerInfo { layers: vec![rec], ..Default::default() }; - rt(&info, Version::Psd); - assert_eq!(info.layers[0].decode_channel(0, 8, Version::Psd).unwrap(), Vec::::new()); - } - - /// The computed length matches the bytes written for every mask form, long names, custom block - /// padding, long-length blocks and trailing bytes. - #[test] - fn unpadded_len_matches_the_written_body() { - for version in [Version::Psd, Version::Psb] { - let mut layers = vec![sample_record(version), LayerRecord::default()]; - for (params, real, real_first) in [(true, true, true), (true, true, false), (true, false, true), (false, true, true)] { - let mut mask = LayerMask::new(Rect::from_xywh(1, 2, 3, 4), 0, LayerMask::FLAG_PARAMETERS); - mask.parameters = params.then_some(MaskParameters { - flags: 0x0f, - user_density: Some(1), - user_feather: Some(2.5), - vector_density: Some(3), - vector_feather: None, - }); - mask.real = real.then_some(RealMask { flags: 0, background: 255, rect: Rect::from_xywh(0, 0, 1, 1) }); - mask.real_first = real_first; - mask.trailing = vec![0; 3]; - let mut rec = sample_record(version); - rec.mask = MaskData::Mask(mask); - rec.name = vec![b'n'; 300]; - rec.blocks.push(TaggedBlock { signature: *b"8BIM", key: *b"lsct", data: vec![1; 5], padding: Some(vec![0; 3]) }); - rec.blocks.push(TaggedBlock { signature: *b"8BIM", key: *b"Lr16", data: vec![2; 7], padding: None }); - rec.extra_trailing = vec![9; 2]; - layers.push(rec); - } - layers.push(LayerRecord { mask: MaskData::Raw(vec![5; 11]), name: b"ab".to_vec(), ..Default::default() }); - let info = LayerInfo { layers, ..Default::default() }; - let mut out = Vec::new(); - info.write_unpadded(&mut out, version).unwrap(); - assert_eq!(info.unpadded_len(version).unwrap(), out.len() as u64, "{version:?}"); - } - } - - /// Padding without cloning the layers writes the same bytes as `pad_to` on a clone, and the - /// channel data length hint is exact. - #[test] - fn unpadded_write_matches_pad_to_on_a_clone() { - use crate::{ColorMode, Compression, testgen}; - for version in [Version::Psd, Version::Psb] { - for depth in [8u16, 16, 32] { - for compression in [Compression::Raw, Compression::Rle] { - let file = testgen::layered(version, ColorMode::Rgb, depth, compression); - let mut info = file.layer_info.clone().unwrap(); - for align in [1u64, 2, 4, 8] { - let mut cloned = info.clone(); - cloned.pad_to(version, align).unwrap(); - let mut want = Vec::new(); - cloned.write_body(&mut want, version).unwrap(); - let pad = info.pad_len(version, align).unwrap(); - let mut got = Vec::new(); - info.write_unpadded(&mut got, version).unwrap(); - assert!(got.len() >= info.channel_data_len()); - got.resize(got.len() + pad, 0); - assert_eq!(got, want, "{version:?} {depth} {compression:?} align {align}"); - } - info.padding = Some(vec![7, 7]); - let mut with_padding = Vec::new(); - info.write_body(&mut with_padding, version).unwrap(); - assert!(with_padding.ends_with(&[7, 7])); - } - } - } - } - - #[test] - fn nondefault_padding_kept() { - let info = LayerInfo { padding: Some(vec![0, 0]), ..Default::default() }; - let out = rt(&info, Version::Psd); - assert_eq!(out.len(), 4); - } - - #[test] - fn unknown_blend_and_blocks_passthrough() { - let rec = LayerRecord { blend_mode: BlendMode::Unknown(*b"wxyz"), blocks: vec![TaggedBlock::new(*b"Zzzz", vec![1, 2, 3, 4, 5])], ..Default::default() }; - rt(&LayerInfo { layers: vec![rec], ..Default::default() }, Version::Psd); - } -} +mod tests; diff --git a/crates/psd/src/layer/tests.rs b/crates/psd/src/layer/tests.rs new file mode 100644 index 0000000000..c831b1c6d2 --- /dev/null +++ b/crates/psd/src/layer/tests.rs @@ -0,0 +1,374 @@ +use super::*; + +fn sample_record(version: Version) -> LayerRecord { + let rect = Rect::from_xywh(-3, 2, 5, 4); + let px: Vec = (0..20).collect(); + let mut channels = Vec::new(); + for (id, comp) in [(-1, Compression::Rle), (0, Compression::Raw), (1, Compression::Zip), (2, Compression::ZipPrediction)] { + channels.push(ChannelData::encode(id, comp, &px, 5, 4, 8, version).unwrap()); + } + let mask = LayerMask::new(Rect::from_xywh(0, 0, 2, 2), 255, LayerMask::FLAG_RELATIVE); + channels.push(ChannelData::encode(-2, Compression::Rle, &[1, 2, 3, 4], 2, 2, 8, version).unwrap()); + LayerRecord { + rect, + channels, + blend_mode: BlendMode::Screen, + opacity: 200, + clipping: 1, + flags: LayerFlags(LayerFlags::HIDDEN), + mask: MaskData::Mask(mask), + blending_ranges: BlendingRanges::full(3), + name: b"abc".to_vec(), + blocks: vec![TaggedBlock::unicode_name("\u{e4}bc"), TaggedBlock::layer_id(5)], + ..Default::default() + } +} + +fn rt(info: &LayerInfo, version: Version) -> Vec { + let mut out = Vec::new(); + info.write_body(&mut out, version).unwrap(); + let back = LayerInfo::read_body(&out, version).unwrap(); + assert_eq!(&back, info); + let mut again = Vec::new(); + back.write_body(&mut again, version).unwrap(); + assert_eq!(again, out); + out +} + +#[test] +fn empty_layer_info() { + let out = rt(&LayerInfo::default(), Version::Psd); + assert_eq!(out, vec![0, 0]); +} + +#[test] +fn record_roundtrip_psd_psb() { + for v in [Version::Psd, Version::Psb] { + let info = LayerInfo { merged_alpha: true, layers: vec![sample_record(v), LayerRecord::default()], padding: None }; + let out = rt(&info, v); + assert_eq!(out.len() % 2, 0); + assert_eq!(&out[..2], &(-2i16).to_be_bytes()); + } +} + +#[test] +fn psb_channel_lengths_are_8_bytes() { + let mut rec = LayerRecord::default(); + rec.channels.push(ChannelData::new(0, Some(Compression::Raw), vec![])); + let info = LayerInfo { layers: vec![rec], ..Default::default() }; + let psd = rt(&info, Version::Psd); + let psb = rt(&info, Version::Psb); + assert_eq!(psb.len(), psd.len() + 4); +} + +#[test] +fn channel_decode_via_record() { + let rec = sample_record(Version::Psd); + let px: Vec = (0..20).collect(); + for id in [-1, 0, 1, 2] { + assert_eq!(rec.decode_channel(id, 8, Version::Psd).unwrap(), px); + } + assert_eq!(rec.decode_channel(-2, 8, Version::Psd).unwrap(), vec![1, 2, 3, 4]); + assert!(rec.decode_channel(7, 8, Version::Psd).is_err()); +} + +#[test] +fn record_accessors() { + let rec = sample_record(Version::Psd); + assert_eq!(rec.name(), "\u{e4}bc"); + assert!(!rec.is_visible()); + assert_eq!(rec.layer_id(), Some(5)); + assert_eq!(rec.fill_opacity(), 255); + assert_eq!(rec.section_type(), SectionType::Other); + assert!(rec.layer_mask().is_some()); + assert_eq!(rec.channel_rect(-2), Rect::from_xywh(0, 0, 2, 2)); + assert_eq!(rec.channel_rect(0), rec.rect); + let mut plain = LayerRecord { name: b"legacy".to_vec(), ..Default::default() }; + assert_eq!(plain.name(), "legacy"); + plain.blocks.push(TaggedBlock::fill_opacity(10)); + assert_eq!(plain.fill_opacity(), 10); + plain.block_mut(b"iOpa").unwrap().data[0] = 11; + assert_eq!(plain.fill_opacity(), 11); +} + +#[test] +fn mask_variants_roundtrip() { + let base = LayerMask::new(Rect::from_xywh(1, 2, 3, 4), 0, 0); + let with_real = LayerMask { real: Some(RealMask { flags: 1, background: 255, rect: Rect::from_xywh(0, 0, 9, 9) }), trailing: vec![], ..base.clone() }; + let with_params = LayerMask { + flags: LayerMask::FLAG_PARAMETERS, + parameters: Some(MaskParameters { + flags: 0b1111, + user_density: Some(128), + user_feather: Some(2.5), + vector_density: Some(64), + vector_feather: Some(0.5), + }), + real: Some(RealMask::default()), + trailing: vec![0, 0], + ..base.clone() + }; + let partial_params = LayerMask { + flags: LayerMask::FLAG_PARAMETERS, + parameters: Some(MaskParameters { flags: 0b0010, user_feather: Some(1.0), ..Default::default() }), + trailing: vec![], + ..base.clone() + }; + for m in [base, with_real, with_params, partial_params] { + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(LayerMask::parse(&out).unwrap(), m); + } +} + +/// Photoshop stores the real-mask fields before the mask parameters (psd-tools +/// mask-density-layervectormask.psd): user + vector mask with both densities set. +#[test] +fn real_mask_before_parameters_photoshop_layout() { + let mut d = Vec::new(); + Rect::from_xywh(15, -1, 18, 10).write(&mut d); + d.extend([0, LayerMask::FLAG_PARAMETERS | 8]); + d.extend([0, 255]); + Rect::from_xywh(0, 0, 32, 8).write(&mut d); + d.extend([0b0101, 64, 64, 0]); + let m = LayerMask::parse(&d).unwrap(); + assert_eq!(m.real.map(|r| r.rect), Some(Rect::from_xywh(0, 0, 32, 8))); + assert_eq!(m.real.map(|r| r.background), Some(255)); + let p = m.parameters.unwrap(); + assert_eq!((p.user_density, p.vector_density), (Some(64), Some(64))); + assert_eq!(m.trailing, vec![0]); + assert!(m.real_first); + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(out, d); +} + +/// A density-only record in the spec's order (#949): the parameter flags byte at 18 says one +/// value byte, so the spec's reading excludes the 38-byte length only if the real mask is +/// ignored, while byte 36 (inside the real rectangle's `right`) is 0 and makes Photoshop's +/// reading fit too. Photoshop's reading puts the density (64) where a 0-or-255 background +/// belongs, so the spec's order wins and the density survives. +#[test] +fn density_only_spec_layout_is_not_read_as_photoshop_order() { + for (density, right) in [(64u8, 1u32), (200, 1), (64, 300), (1, 7)] { + let mut d = Vec::new(); + Rect::from_xywh(0, 0, 1, 1).write(&mut d); + d.extend([255, LayerMask::FLAG_PARAMETERS]); + d.extend([0b0001, density]); + d.extend([0, 0]); + Rect::from_xywh(0, 0, right, 1).write(&mut d); + let m = LayerMask::parse(&d).unwrap(); + assert!(!m.real_first, "density {density}, right {right}: {m:?}"); + assert_eq!(m.parameters.unwrap().user_density, Some(density)); + assert_eq!(m.real, Some(RealMask { flags: 0, background: 0, rect: Rect::from_xywh(0, 0, right, 1) })); + assert!(m.trailing.is_empty()); + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(out, d); + } + // Photoshop's order with the same lengths still reads as Photoshop's order. + let mut d = Vec::new(); + Rect::from_xywh(0, 0, 1, 1).write(&mut d); + d.extend([255, LayerMask::FLAG_PARAMETERS]); + d.extend([0, 255]); + Rect::from_xywh(0, 0, 1, 1).write(&mut d); + d.extend([0b0001, 64]); + let m = LayerMask::parse(&d).unwrap(); + assert!(m.real_first, "{m:?}"); + assert_eq!(m.parameters.unwrap().user_density, Some(64)); + assert_eq!(m.real.map(|r| r.background), Some(255)); + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(out, d); +} + +/// The spec's order (parameters, then real fields) still parses and stays byte-stable. +#[test] +fn real_mask_after_parameters_spec_layout() { + let mut d = Vec::new(); + Rect::from_xywh(1, 2, 3, 4).write(&mut d); + d.extend([255, LayerMask::FLAG_PARAMETERS]); + d.extend([0b0011, 200]); + d.extend(1.5f64.to_be_bytes()); + d.extend([0, 255]); + Rect::from_xywh(0, 0, 3, 3).write(&mut d); + let m = LayerMask::parse(&d).unwrap(); + assert!(!m.real_first); + assert_eq!(m.real.map(|r| r.rect), Some(Rect::from_xywh(0, 0, 3, 3))); + assert_eq!(m.parameters.unwrap().user_feather, Some(1.5)); + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(out, d); + // Parameters alone (no real mask) keep reading in the spec's order. + let mut d = Vec::new(); + Rect::from_xywh(1, 2, 3, 4).write(&mut d); + d.extend([255, LayerMask::FLAG_PARAMETERS, 0b1010]); + d.extend(1.0f64.to_be_bytes()); + d.extend(2.0f64.to_be_bytes()); + d.extend([0, 0]); + let m = LayerMask::parse(&d).unwrap(); + assert_eq!(m.real, None); + assert_eq!(m.parameters.map(|p| (p.user_feather, p.vector_feather)), Some((Some(1.0), Some(2.0)))); +} + +#[test] +fn mask_sizes() { + let mut out = Vec::new(); + LayerMask::new(Rect::default(), 0, 0).write(&mut out); + assert_eq!(out.len(), 20); + let m = LayerMask { real: Some(RealMask::default()), trailing: vec![], ..LayerMask::new(Rect::default(), 0, 0) }; + let mut out = Vec::new(); + m.write(&mut out); + assert_eq!(out.len(), 36); +} + +#[test] +fn short_mask_kept_raw() { + let mut rec = LayerRecord { mask: MaskData::Raw(vec![1, 2, 3]), ..Default::default() }; + rec.channels.clear(); + let info = LayerInfo { layers: vec![rec], ..Default::default() }; + rt(&info, Version::Psd); +} + +#[test] +fn flags_helpers() { + let mut f = LayerFlags::default(); + f.set_hidden(true); + f.set_transparency_protected(true); + assert!(f.hidden() && f.transparency_protected()); + f.set_pixel_data_irrelevant(true); + assert!(f.bit4_useful() && f.pixel_data_irrelevant()); + f.set_hidden(false); + assert!(!f.hidden()); +} + +#[test] +fn blending_ranges_parse() { + let br = BlendingRanges::full(3); + assert_eq!(br.data.len(), 32); + let r = br.ranges(); + assert_eq!(r.len(), 4); + assert_eq!(r[0].source, [0, 0, 255, 255]); +} + +#[test] +fn rect_helpers() { + let r = Rect::from_xywh(-5, -6, 10, 3); + assert_eq!((r.width(), r.height()), (10, 3)); + assert_eq!(r.size().unwrap(), (10, 3)); + assert!(!r.is_empty()); + let bad = Rect { top: 5, left: 0, bottom: 0, right: 1 }; + assert!(bad.size().is_err()); + assert!(bad.is_empty()); + let huge = Rect { top: 0, left: i32::MIN, bottom: 1, right: i32::MAX }; + assert!(huge.size().is_err()); +} + +#[test] +fn truncation_errors() { + let info = LayerInfo { layers: vec![sample_record(Version::Psd)], ..Default::default() }; + let mut out = Vec::new(); + info.write_body(&mut out, Version::Psd).unwrap(); + // The final byte may be the (optional) even padding; stop before it. + for cut in 0..out.len() - 1 { + assert!(LayerInfo::read_body(&out[..cut], Version::Psd).is_err(), "cut {cut}"); + } +} + +#[test] +fn bad_blend_signature() { + let info = LayerInfo { layers: vec![LayerRecord::default()], ..Default::default() }; + let mut out = Vec::new(); + info.write_body(&mut out, Version::Psd).unwrap(); + // count(2) + rect(16) + nch(2) = 20 → signature + out[20] = b'X'; + assert!(matches!(LayerInfo::read_body(&out, Version::Psd), Err(PsdError::InvalidSignature { .. }))); +} + +#[test] +fn too_many_layers_rejected() { + let out = 1000i16.to_be_bytes(); + assert!(LayerInfo::read_body(&out, Version::Psd).is_err()); +} + +#[test] +fn zero_length_channel() { + let mut rec = LayerRecord::default(); + rec.channels.push(ChannelData::new(0, None, vec![])); + let info = LayerInfo { layers: vec![rec], ..Default::default() }; + rt(&info, Version::Psd); + assert_eq!(info.layers[0].decode_channel(0, 8, Version::Psd).unwrap(), Vec::::new()); +} + +/// The computed length matches the bytes written for every mask form, long names, custom block +/// padding, long-length blocks and trailing bytes. +#[test] +fn unpadded_len_matches_the_written_body() { + for version in [Version::Psd, Version::Psb] { + let mut layers = vec![sample_record(version), LayerRecord::default()]; + for (params, real, real_first) in [(true, true, true), (true, true, false), (true, false, true), (false, true, true)] { + let mut mask = LayerMask::new(Rect::from_xywh(1, 2, 3, 4), 0, LayerMask::FLAG_PARAMETERS); + mask.parameters = + params.then_some(MaskParameters { flags: 0x0f, user_density: Some(1), user_feather: Some(2.5), vector_density: Some(3), vector_feather: None }); + mask.real = real.then_some(RealMask { flags: 0, background: 255, rect: Rect::from_xywh(0, 0, 1, 1) }); + mask.real_first = real_first; + mask.trailing = vec![0; 3]; + let mut rec = sample_record(version); + rec.mask = MaskData::Mask(mask); + rec.name = vec![b'n'; 300]; + rec.blocks.push(TaggedBlock { signature: *b"8BIM", key: *b"lsct", data: vec![1; 5], padding: Some(vec![0; 3]) }); + rec.blocks.push(TaggedBlock { signature: *b"8BIM", key: *b"Lr16", data: vec![2; 7], padding: None }); + rec.extra_trailing = vec![9; 2]; + layers.push(rec); + } + layers.push(LayerRecord { mask: MaskData::Raw(vec![5; 11]), name: b"ab".to_vec(), ..Default::default() }); + let info = LayerInfo { layers, ..Default::default() }; + let mut out = Vec::new(); + info.write_unpadded(&mut out, version).unwrap(); + assert_eq!(info.unpadded_len(version).unwrap(), out.len() as u64, "{version:?}"); + } +} + +/// Padding without cloning the layers writes the same bytes as `pad_to` on a clone, and the +/// channel data length hint is exact. +#[test] +fn unpadded_write_matches_pad_to_on_a_clone() { + use crate::{ColorMode, Compression, testgen}; + for version in [Version::Psd, Version::Psb] { + for depth in [8u16, 16, 32] { + for compression in [Compression::Raw, Compression::Rle] { + let file = testgen::layered(version, ColorMode::Rgb, depth, compression); + let mut info = file.layer_info.clone().unwrap(); + for align in [1u64, 2, 4, 8] { + let mut cloned = info.clone(); + cloned.pad_to(version, align).unwrap(); + let mut want = Vec::new(); + cloned.write_body(&mut want, version).unwrap(); + let pad = info.pad_len(version, align).unwrap(); + let mut got = Vec::new(); + info.write_unpadded(&mut got, version).unwrap(); + assert!(got.len() >= info.channel_data_len()); + got.resize(got.len() + pad, 0); + assert_eq!(got, want, "{version:?} {depth} {compression:?} align {align}"); + } + info.padding = Some(vec![7, 7]); + let mut with_padding = Vec::new(); + info.write_body(&mut with_padding, version).unwrap(); + assert!(with_padding.ends_with(&[7, 7])); + } + } + } +} + +#[test] +fn nondefault_padding_kept() { + let info = LayerInfo { padding: Some(vec![0, 0]), ..Default::default() }; + let out = rt(&info, Version::Psd); + assert_eq!(out.len(), 4); +} + +#[test] +fn unknown_blend_and_blocks_passthrough() { + let rec = LayerRecord { blend_mode: BlendMode::Unknown(*b"wxyz"), blocks: vec![TaggedBlock::new(*b"Zzzz", vec![1, 2, 3, 4, 5])], ..Default::default() }; + rt(&LayerInfo { layers: vec![rec], ..Default::default() }, Version::Psd); +} diff --git a/crates/psd/src/lazy.rs b/crates/psd/src/lazy.rs new file mode 100644 index 0000000000..d81d102d11 --- /dev/null +++ b/crates/psd/src/lazy.rs @@ -0,0 +1,437 @@ +//! Parsing from a [`Source`] without reading the whole file: [`PsdFile::from_source`]. +//! +//! The structure (header, resources, layer records, global blocks) is read into memory as +//! [`PsdFile::from_bytes`] would hold it. The bulk of a layered file is left in the source: every +//! channel's encoded bytes and the merged image become [`Stored`] ranges, read when they are +//! decoded. Apart from those ranges, the model is the one `from_bytes` builds from the same bytes +//! (the parse tests hold the two to that), with one difference: a merged image the caller does +//! not ask for is not read, so it is not checked either. + +use crate::compression::Compression; +use crate::error::{PsdError, Result}; +use crate::file::{GlobalLayerMask, LayerInfoPlacement, PsdFile}; +use crate::header::{Header, Version}; +use crate::image_data::ImageData; +use crate::io::Reader; +use crate::layer::{LayerInfo, LayerRecord}; +use crate::resources::ImageResource; +use crate::source::{Source, Stored}; +use crate::tagged::{TaggedBlock, is_sig, uses_long_length}; + +/// Bytes read per attempt at the layer records; doubled until they all fit. (Small in tests, so +/// they grow it.) +const RECORDS_CHUNK: u64 = if cfg!(test) { 64 } else { 1 << 20 }; + +/// A bounds-checked cursor over `start..end` of a source. +struct Cursor<'a> { + src: &'a Source, + pos: u64, + end: u64, +} + +impl<'a> Cursor<'a> { + fn new(src: &'a Source) -> Self { + Cursor { src, pos: 0, end: src.get().len() } + } + + fn remaining(&self) -> u64 { + self.end.saturating_sub(self.pos) + } + + fn is_empty(&self) -> bool { + self.remaining() == 0 + } + + fn eof(&self, needed: u64) -> PsdError { + PsdError::UnexpectedEof { + offset: usize::try_from(self.pos).unwrap_or(usize::MAX), + needed: usize::try_from(needed.saturating_sub(self.remaining())).unwrap_or(usize::MAX), + } + } + + fn need(&self, n: u64) -> Result<()> { + if n > self.remaining() { Err(self.eof(n)) } else { Ok(()) } + } + + /// Reads `n` bytes. + fn bytes(&mut self, n: u64) -> Result> { + self.need(n)?; + let v = self.src.read_vec(self.pos, n)?; + self.pos += n; + Ok(v) + } + + /// Up to `n` bytes from the current position, without consuming. + fn peek(&self, n: u64) -> Result> { + self.src.read_vec(self.pos, n.min(self.remaining())) + } + + fn array(&mut self) -> Result<[u8; N]> { + self.need(N as u64)?; + let mut b = [0u8; N]; + self.src.get().read_at(self.pos, &mut b)?; + self.pos += N as u64; + Ok(b) + } + + fn u16(&mut self) -> Result { + self.array().map(u16::from_be_bytes) + } + + fn i16(&mut self) -> Result { + self.array().map(i16::from_be_bytes) + } + + fn u32(&mut self) -> Result { + self.array().map(u32::from_be_bytes) + } + + fn len_field(&mut self, long: bool) -> Result { + if long { self.array().map(u64::from_be_bytes) } else { self.u32().map(u64::from) } + } + + /// The next `n` bytes as a cursor of their own; this one moves past them. + fn sub(&mut self, n: u64) -> Result> { + self.need(n)?; + let sub = Cursor { src: self.src, pos: self.pos, end: self.pos + n }; + self.pos += n; + Ok(sub) + } + + fn skip(&mut self, n: u64) -> Result<()> { + self.need(n)?; + self.pos += n; + Ok(()) + } + + fn rest(&mut self) -> Result> { + self.bytes(self.remaining()) + } + + /// The rest as a stored range; the cursor moves to the end. + fn stored_rest(&mut self) -> Stored { + let s = Stored { source: self.src.clone(), offset: self.pos, len: self.remaining() }; + self.pos = self.end; + s + } +} + +impl PsdFile { + /// Parses a file from `source`, leaving channel data and the merged image in it: see + /// [`crate::source`]. Decoding a channel then reads its bytes from the source, which must + /// stay unchanged while the model is in use. + pub fn from_source(source: Source) -> Result { + Self::from_source_and_merged_recovering(source, |_| false, |_| false).map(|(file, _)| file) + } + + /// [`Self::from_bytes_and_merged_recovering`] for a [`Source`]: the merged image is read only + /// when `want` asks for it (and is then decoded as there, with the same recovery); otherwise + /// it stays in the source, unread and unchecked. + pub fn from_source_and_merged_recovering( + source: Source, + want: impl FnOnce(&PsdFile) -> bool, + recover: impl FnOnce(&PsdFile) -> bool, + ) -> Result<(Self, Option>>)> { + let mut c = Cursor::new(&source); + let header = Header::read(&mut Reader::new(&c.bytes(26)?))?; + let version = header.version; + let psb = version.is_psb(); + let cmd_len = c.u32()?; + let color_mode_data = c.bytes(u64::from(cmd_len))?; + let res_len = c.u32()?; + let res = c.bytes(u64::from(res_len))?; + let mut rr = Reader::new(&res); + let mut resources = Vec::new(); + while !rr.is_empty() { + resources.push(ImageResource::read(&mut rr)?); + } + + let lm_len = c.len_field(psb)?; + let mut s = c.sub(lm_len)?; + let mut layer_info = None; + let mut global_layer_mask = None; + let mut global_blocks = Vec::new(); + let mut layer_mask_trailing = Vec::new(); + let mut layer_info_placement = LayerInfoPlacement::Section; + if !s.is_empty() { + let li_len = s.len_field(psb)?; + if li_len > 0 { + layer_info = Some(read_layer_info(s.sub(li_len)?, version)?); + } + if s.remaining() >= 4 { + let n = s.u32()?; + global_layer_mask = Some(GlobalLayerMask { data: s.bytes(u64::from(n))? }); + let target_key = match header.depth { + 16 => Some(b"Lr16"), + 32 => Some(b"Lr32"), + _ => None, + }; + let mut candidate_seen = match target_key { + Some(_) => false, + None => layer_info.as_ref().is_some_and(|i: &LayerInfo| !i.layers.is_empty()), + }; + let trailing = read_blocks( + &mut s, + version, + |index, signature, key, data, padding| { + let is_candidate = match target_key { + Some(k) => &key == k, + None => matches!(&key, b"Layr" | b"Lr16" | b"Lr32"), + }; + if candidate_seen || !is_candidate { + return Ok(None); + } + candidate_seen = true; + let info = match read_layer_info(data, version) { + Ok(info) => info, + // A failed read of the source is not damage: fail the parse, as + // reading the whole file would. + Err(e @ PsdError::Read(_)) => return Err(e), + Err(_) => { + if target_key.is_some() { + layer_info = None; + } + return Ok(None); + } + }; + layer_info = Some(info); + layer_info_placement = LayerInfoPlacement::GlobalBlock { index, signature, key, padding: padding.clone() }; + Ok(Some(())) + }, + &mut global_blocks, + )?; + layer_mask_trailing = trailing; + } else { + layer_mask_trailing = s.rest()?; + } + } + let mut file = PsdFile { + header, + color_mode_data, + resources, + layer_info, + layer_info_placement, + global_layer_mask, + global_blocks, + layer_mask_trailing, + image_data: ImageData::new(Compression::Raw, Vec::new()), + write_as_psb: false, + }; + if !want(&file) { + let compression = Compression::from_u16(c.u16()?); + file.image_data = ImageData { compression, data: Vec::new(), stored: Some(c.stored_rest()) }; + return Ok((file, None)); + } + // The merged image is wanted: read it and decode it as `from_bytes` does. + let rest = c.rest()?; + let merged = file.read_merged_recovering(&rest, recover)?; + Ok((file, merged)) + } +} + +/// Reads tagged blocks as [`crate::tagged::read_blocks_with`] does, from a cursor: `consume` +/// sees each block's data as a cursor and returns `Some` when it took the block; the others are +/// read into memory and pushed to `blocks`. Returns the trailing bytes. +fn read_blocks<'a>( + r: &mut Cursor<'a>, + version: Version, + mut consume: impl FnMut(usize, [u8; 4], [u8; 4], Cursor<'a>, &Option>) -> Result>, + blocks: &mut Vec, +) -> Result> { + const ALIGNMENT: usize = 4; + loop { + let head = r.peek(12)?; + if head.len() < 12 || !is_sig(&head) { + return r.rest(); + } + let signature = r.array::<4>()?; + let key = r.array::<4>()?; + let len = r.len_field(uses_long_length(version, &key))?; + let data = r.sub(len)?; + // Padding as `read_blocks_with` detects it: the smallest k in 0..=3 such that the next + // signature (or the end of the region) follows k bytes later. + let rest = r.peek(7)?; + let mut pad = None; + for k in 0..=3usize { + if k == rest.len() || rest.get(k..).is_some_and(is_sig) { + pad = Some(k); + break; + } + } + let default = TaggedBlock::default_padding((len % ALIGNMENT as u64) as usize, ALIGNMENT); + let pad = pad.unwrap_or_else(|| if rest.get(..default).is_some_and(|p| p.iter().all(|&b| b == 0)) { default } else { 0 }); + let pad_bytes = rest.get(..pad).map(<[u8]>::to_vec).unwrap_or_default(); + r.skip(pad as u64)?; + let padding = if pad_bytes.len() == default && pad_bytes.iter().all(|&b| b == 0) { None } else { Some(pad_bytes) }; + let (start, end) = (data.pos, data.end); + if consume(blocks.len(), signature, key, data, &padding)?.is_none() { + let data = Cursor { src: r.src, pos: start, end }.rest()?; + blocks.push(TaggedBlock { signature, key, data, padding }); + } + } +} + +/// [`LayerInfo::read_body`] from a cursor over the body: the records are read into memory, and +/// each channel's encoded bytes become a [`Stored`] range after its compression field. +fn read_layer_info(mut r: Cursor<'_>, version: Version) -> Result { + let body_start = r.pos; + let count = r.i16()?; + let merged_alpha = count < 0; + let n = count.unsigned_abs(); + // Minimum record size, as `read_body` checks it. + if u64::from(n) * 34 > r.remaining() { + return Err(r.eof(u64::from(n) * 34)); + } + // The records' total size is only known once they are parsed: read a chunk and grow it until + // they all fit (or the body is exhausted, when their error is the body's). + let mut chunk = RECORDS_CHUNK.min(r.remaining()); + let (layers, all_lens, used) = loop { + let buf = r.peek(chunk)?; + let mut rr = Reader::new(&buf); + let mut layers = Vec::with_capacity(usize::from(n)); + let mut all_lens = Vec::with_capacity(usize::from(n)); + let parsed = (0..n).try_for_each(|_| { + let (rec, lens) = LayerRecord::read(&mut rr, version)?; + layers.push(rec); + all_lens.push(lens); + Ok::<_, PsdError>(()) + }); + match parsed { + Ok(()) => break (layers, all_lens, rr.pos() as u64), + Err(PsdError::UnexpectedEof { .. }) if chunk < r.remaining() => chunk = chunk.saturating_mul(2).min(r.remaining()), + Err(e) => return Err(e), + } + }; + r.skip(used)?; + let mut layers = layers; + for (rec, lens) in layers.iter_mut().zip(all_lens) { + for (ch, len) in rec.channels.iter_mut().zip(lens) { + if len == 0 { + continue; + } + if len == 1 { + return Err(PsdError::invalid("channel data length 1")); + } + ch.compression = Some(Compression::from_u16(r.u16()?)); + let data = r.sub(len - 2)?; + ch.stored = Some(Stored { source: data.src.clone(), offset: data.pos, len: data.remaining() }); + } + } + let body_len = r.pos - body_start; + let rest = r.rest()?; + let padding = if rest.len() as u64 == body_len % 2 && rest.iter().all(|&b| b == 0) { None } else { Some(rest) }; + Ok(LayerInfo { merged_alpha, layers, padding }) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::testgen; + + /// Every generated file, plus the layered 16/32-bit ones with their layers in a global block. + fn samples() -> Vec> { + let mut v = Vec::new(); + for case in testgen::all_cases() { + let mut file = case.file; + v.push(file.to_bytes().unwrap()); + let key = match file.header.depth { + 16 => *b"Lr16", + 32 => *b"Lr32", + _ => continue, + }; + if file.layer_info.as_ref().is_some_and(|i| !i.layers.is_empty()) { + file.global_layer_mask.get_or_insert_with(GlobalLayerMask::default); + file.layer_info_placement = LayerInfoPlacement::GlobalBlock { index: 0, signature: *b"8BIM", key, padding: None }; + v.push(file.to_bytes().unwrap()); + } + } + v + } + + fn same(bytes: Vec) { + let eager = PsdFile::from_bytes(&bytes).unwrap(); + let lazy = PsdFile::from_source(Source::new(bytes.clone())).unwrap(); + assert_eq!(lazy, eager); + assert_eq!(lazy.to_bytes().unwrap(), bytes); + assert!(lazy.layers().iter().flat_map(|l| &l.channels).all(|c| c.data.is_empty())); + assert!(lazy.image_data.data.is_empty() && lazy.image_data.stored.is_some()); + } + + #[test] + fn lazy_parse_matches_the_eager_one() { + for bytes in samples() { + same(bytes); + } + } + + /// A PSD read lazily and written as PSB widens the RLE count tables of channels still in the + /// source, byte for byte as an eager read does (main's PSB promotion, #5285, on #6583's reader). + #[test] + fn a_lazy_psd_promotes_to_the_same_psb_as_an_eager_one() { + let bytes = testgen::layered(Version::Psd, crate::ColorMode::Rgb, 8, Compression::Rle).to_bytes().unwrap(); + let mut eager = PsdFile::from_bytes(&bytes).unwrap(); + let mut lazy = PsdFile::from_source(Source::new(bytes)).unwrap(); + eager.promote_to_psb().unwrap(); + lazy.promote_to_psb().unwrap(); + let written = eager.to_bytes().unwrap(); + assert_eq!(lazy.encoded_len().unwrap(), written.len() as u64); + assert!(lazy.to_bytes().unwrap() == written, "the lazily read PSD writes another PSB"); + } + + #[test] + fn lazy_parse_fails_where_the_eager_one_does() { + for bytes in samples() { + for cut in [1, 2, 7, 26, 40, bytes.len() / 3, bytes.len() / 2, bytes.len() - 3] { + let Some(b) = bytes.get(..cut) else { continue }; + let eager = PsdFile::from_bytes(b); + let lazy = PsdFile::from_source(Source::new(b.to_vec())); + // A truncated merged image is only noticed when it is read. + match eager { + Ok(eager) => assert_eq!(lazy.unwrap(), eager, "cut at {cut}"), + Err(e) => { + let records_ok = PsdFile::from_bytes_and_merged_recovering(b, |_| false, |_| true).is_ok(); + assert!(lazy.is_err() || records_ok, "cut at {cut}: eager {e:?}"); + } + } + } + } + } + + /// A source whose `fail_at`-th read fails once. + struct FailOnce { + bytes: Vec, + fail_at: usize, + reads: std::sync::atomic::AtomicUsize, + } + + impl crate::source::ReadAt for FailOnce { + fn len(&self) -> u64 { + self.bytes.len() as u64 + } + + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + if self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == self.fail_at { + return Err(PsdError::Read("injected".into())); + } + self.bytes.read_at(offset, buf) + } + } + + #[test] + fn a_failed_read_fails_the_parse_instead_of_dropping_the_layers() { + for bytes in samples() { + let eager = PsdFile::from_bytes(&bytes).unwrap(); + let clean = std::sync::Arc::new(FailOnce { bytes: bytes.clone(), fail_at: usize::MAX, reads: 0.into() }); + PsdFile::from_source(Source::from_arc(clean.clone())).unwrap(); + let reads = clean.reads.load(std::sync::atomic::Ordering::SeqCst); + for fail_at in 0..reads { + let src = FailOnce { bytes: bytes.clone(), fail_at, reads: 0.into() }; + match PsdFile::from_source(Source::new(src)) { + Err(PsdError::Read(_)) => {} + Ok(lazy) => assert_eq!(lazy, eager, "read {fail_at} failed silently"), + Err(e) => panic!("read {fail_at}: {e:?}"), + } + } + } + } +} diff --git a/crates/psd/src/lib.rs b/crates/psd/src/lib.rs index 00d73c4003..f5ffbed8b7 100644 --- a/crates/psd/src/lib.rs +++ b/crates/psd/src/lib.rs @@ -42,7 +42,9 @@ pub mod abr; pub mod aco; +pub mod act; pub mod ase; +pub mod asl; pub mod atn; pub mod blend; pub mod builder; @@ -51,19 +53,24 @@ pub mod descriptor; pub mod error; pub mod file; pub mod filter_effects; +#[cfg(any(test, feature = "testgen"))] +pub mod fixtures; pub mod grd; pub mod hdr; pub mod header; pub mod image_data; mod io; pub mod layer; +mod lazy; pub mod metadata; pub mod path; pub mod patterns; pub mod pixels; pub mod resources; +pub mod rows; pub mod shc; pub mod slices; +pub mod source; pub mod tagged; #[cfg(any(test, feature = "testgen"))] // Test-data generator (tests and fuzz seeds only): failing loudly is the point. @@ -83,5 +90,9 @@ pub use image_data::ImageData; pub use layer::{BlendingRanges, ChannelData, LayerFlags, LayerInfo, LayerMask, LayerRecord, MaskData, MaskParameters, RealMask, Rect}; pub use pixels::{GrayImage, Layer, RgbaImage}; pub use resources::{ImageResource, ResolutionInfo, ResourceData}; +pub use rows::RowDecoder; +#[cfg(any(unix, windows))] +pub use source::FileSource; +pub use source::{ReadAt, Source, Stored}; pub use tagged::{BlockData, SectionDivider, SectionType, TaggedBlock}; pub use tree::LayerNode; diff --git a/crates/psd/src/metadata.rs b/crates/psd/src/metadata.rs index 9e85d8cee7..24d2eebca6 100644 --- a/crates/psd/src/metadata.rs +++ b/crates/psd/src/metadata.rs @@ -1,11 +1,12 @@ //! `shmd` (metadata setting) layer block: a list of keyed sub-blocks, such as `cmls` (layer comp -//! state, a versioned descriptor), `cust` and `mlst`. +//! state, a versioned descriptor), `cust` (the layer's modify time and XMP packet) and `mlst`. //! //! Layout (Adobe PSD spec, "Metadata setting"): u32 count, then per item the signature `8BIM`, //! a 4-byte key, a copy-on-sheet-duplication byte, 3 padding bytes, a u32 length and the data. //! Photoshop pads item data to an even length (the padding is part of the stored length), so //! parsing and writing are exact: `write(parse(b)) == b`. +use crate::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; use crate::error::{PsdError, Result}; use crate::io::{Reader, WriteExt}; @@ -71,10 +72,166 @@ pub fn write_shmd(items: &[MetadataItem]) -> Vec { out } +/// The `shmd` item that holds a layer's own metadata (its modify time and XMP). +const CUST: [u8; 4] = *b"cust"; +/// Key of a layer's XMP packet in the `cust` descriptor. +const LAYER_XMP: &str = "layerXMP"; + +// A layer's XMP packet (Photoshop's `layer.xmpMetadata.rawData`), as observed in files Photoshop +// 2026 saves: a `TEXT` item `layerXMP` (UTF-16 with a terminating NUL) in the `metadata` +// descriptor (version 16, class name a lone NUL) of the `shmd` item `cust`, after the +// `layerTime` double that every Photoshop layer has. A layer without XMP has no `layerXMP` item. + +fn pad_even(mut v: Vec) -> Vec { + if v.len() % 2 == 1 { + v.push(0); + } + v +} + +/// The `cust` descriptor of `item`, or `None` when it isn't one. +fn cust_descriptor(item: &MetadataItem) -> Option { + (item.key == CUST).then(|| VersionedDescriptor::parse_prefix(&item.data).ok().map(|(d, _)| d)).flatten() +} + +/// The XMP packet stored on a layer whose `shmd` block data is `shmd`, or `None` when it has +/// none (or the block can't be read). +pub fn layer_xmp(shmd: &[u8]) -> Option { + let items = parse_shmd(shmd).ok()?; + items.iter().filter_map(cust_descriptor).find_map(|d| match d.descriptor.get(LAYER_XMP) { + Some(Value::Text(t)) => Some(t.to_string_lossy()), + _ => None, + }) +} + +/// Splits a layer's XMP packet out of its `shmd` block data: the packet, and the block without +/// it (`None` when nothing is left). `None` when the block holds no packet, so the caller keeps +/// the original bytes. A `cust` item left empty is dropped, the other items are kept as stored. +pub fn take_layer_xmp(shmd: &[u8]) -> Option<(String, Option>)> { + let mut items = parse_shmd(shmd).ok()?; + let (i, mut vd) = items.iter().enumerate().find_map(|(i, it)| cust_descriptor(it).filter(|d| d.descriptor.get(LAYER_XMP).is_some()).map(|d| (i, d)))?; + let at = vd.descriptor.items.iter().position(|(k, _)| k.is(LAYER_XMP))?; + let xmp = match vd.descriptor.items.remove(at).1 { + Value::Text(t) => t.to_string_lossy(), + _ => return None, + }; + if vd.descriptor.items.is_empty() { + items.remove(i); + } else { + items[i].data = pad_even(vd.to_bytes()); + } + Some((xmp, (!items.is_empty()).then(|| write_shmd(&items)))) +} + +/// `shmd` block data (`shmd`, or a new block when `None`) with `xmp` as the layer's XMP packet, +/// written as Photoshop writes it: it replaces an existing packet in place, or is appended to +/// the `cust` descriptor (a new `cust` item when there is none). An unreadable block is an +/// error, so the caller never replaces data it couldn't read. +pub fn put_layer_xmp(shmd: Option<&[u8]>, xmp: &str) -> Result> { + let mut items = match shmd { + Some(b) => parse_shmd(b)?, + None => Vec::new(), + }; + let value = Value::Text(UnicodeString::new_nul(xmp)); + match items.iter().position(|it| it.key == CUST) { + Some(i) => { + let (mut vd, _) = VersionedDescriptor::parse_prefix(&items[i].data)?; + match vd.descriptor.items.iter_mut().find(|(k, _)| k.is(LAYER_XMP)) { + Some(entry) => entry.1 = value, + None => vd.descriptor.items.push((Id::new(LAYER_XMP), value)), + } + items[i].data = pad_even(vd.to_bytes()); + } + None => { + let d = Descriptor { name: UnicodeString::new_nul(""), class_id: Id::new("metadata"), items: vec![(Id::new(LAYER_XMP), value)] }; + items.push(MetadataItem::new(CUST, VersionedDescriptor::new(d).to_bytes())); + } + } + Ok(write_shmd(&items)) +} + #[cfg(test)] mod tests { use super::*; + fn unhex(lines: &[&str]) -> Vec { + let h: String = lines.concat(); + (0..h.len()).step_by(2).map(|i| u8::from_str_radix(&h[i..i + 2], 16).unwrap()).collect() + } + + /// `shmd` of a layer saved by Photoshop 2026 after a script set its XMP to [`PS_PACKET`]. + const PS_WITH_XMP: &[&str] = &[ + "000000013842494d63757374000000000000024800000010000000010000000000086d65746164617461000000020000", + "00096c6179657254696d65646f756241dab643ffd54363000000086c61796572584d505445585400000100003c007800", + "3a0078006d0070006d00650074006100200078006d006c006e0073003a0078003d002200610064006f00620065003a00", + "6e0073003a006d006500740061002f0022003e003c007200640066003a00520044004600200078006d006c006e007300", + "3a007200640066003d00220068007400740070003a002f002f007700770077002e00770033002e006f00720067002f00", + "31003900390039002f00300032002f00320032002d007200640066002d00730079006e007400610078002d006e007300", + "230022003e003c007200640066003a004400650073006300720069007000740069006f006e0020007200640066003a00", + "610062006f00750074003d0022002200200078006d006c006e0073003a00730061006d0070006c0065003d0022006800", + "7400740070003a002f002f006500780061006d0070006c0065002e0063006f006d002f00730061006d0070006c006500", + "2f0031002e0030002f0022003e003c00730061006d0070006c0065003a00760061006c00750065003e0061006c007000", + "680061002d003100320033003c002f00730061006d0070006c0065003a00760061006c00750065003e003c002f007200", + "640066003a004400650073006300720069007000740069006f006e003e003c002f007200640066003a00520044004600", + "3e003c002f0078003a0078006d0070006d006500740061003e000000", + ]; + /// `shmd` of a layer without XMP in the same file (its own `layerTime` only). + const PS_WITHOUT_XMP: &[&str] = + &["000000013842494d63757374000000000000003400000010000000010000000000086d6574616461746100000001000000096c6179657254696d65646f756241dab643ffd86bec00"]; + const PS_PACKET: &str = concat!( + r#""#, + r#"alpha-123"#, + r#""# + ); + + #[test] + fn reads_photoshop_layer_xmp() { + assert_eq!(layer_xmp(&unhex(PS_WITH_XMP)).as_deref(), Some(PS_PACKET)); + assert_eq!(layer_xmp(&unhex(PS_WITHOUT_XMP)), None); + assert!(take_layer_xmp(&unhex(PS_WITHOUT_XMP)).is_none()); + } + + #[test] + fn take_then_put_is_exact() { + let ps = unhex(PS_WITH_XMP); + let (xmp, rest) = take_layer_xmp(&ps).unwrap(); + assert_eq!(xmp, PS_PACKET); + let rest = rest.unwrap(); + // What's left is a layer without XMP, shaped as Photoshop writes one. + assert_eq!(rest.len(), unhex(PS_WITHOUT_XMP).len()); + assert_eq!(layer_xmp(&rest), None); + assert_eq!(put_layer_xmp(Some(&rest), &xmp).unwrap(), ps); + } + + #[test] + fn put_replaces_in_place_and_creates_a_block() { + let ps = unhex(PS_WITH_XMP); + let edited = put_layer_xmp(Some(&ps), "").unwrap(); + assert_eq!(layer_xmp(&edited).as_deref(), Some("")); + // `layerTime` is kept, still first. + let items = parse_shmd(&edited).unwrap(); + let d = cust_descriptor(&items[0]).unwrap().descriptor; + assert!(d.items[0].0.is("layerTime") && d.items[1].0.is(LAYER_XMP) && d.items.len() == 2); + + let fresh = put_layer_xmp(None, PS_PACKET).unwrap(); + assert_eq!(layer_xmp(&fresh).as_deref(), Some(PS_PACKET)); + assert_eq!(take_layer_xmp(&fresh), Some((PS_PACKET.to_string(), None))); + // Other items (here a layer comp state) stay as they were. + let comps = write_shmd(&[MetadataItem::new(*b"cmls", vec![1, 2])]); + let both = put_layer_xmp(Some(&comps), PS_PACKET).unwrap(); + assert_eq!(parse_shmd(&both).unwrap()[0], parse_shmd(&comps).unwrap()[0]); + assert_eq!(take_layer_xmp(&both).unwrap().1.unwrap(), comps); + } + + #[test] + fn put_refuses_an_unreadable_block() { + assert!(put_layer_xmp(Some(&[0, 0, 0, 9]), PS_PACKET).is_err()); + let bad_cust = write_shmd(&[MetadataItem::new(CUST, vec![0, 0, 0, 16, 1])]); + assert!(put_layer_xmp(Some(&bad_cust), PS_PACKET).is_err()); + assert_eq!(layer_xmp(&bad_cust), None); + assert!(take_layer_xmp(&bad_cust).is_none()); + } + #[test] fn roundtrip_exact() { let items = vec![MetadataItem::new(*b"cmls", vec![1, 2, 3]), MetadataItem { copy: true, ..MetadataItem::new(*b"cust", vec![9; 4]) }]; diff --git a/crates/psd/src/patterns.rs b/crates/psd/src/patterns.rs index 39132df9b0..af4afea0a2 100644 --- a/crates/psd/src/patterns.rs +++ b/crates/psd/src/patterns.rs @@ -18,7 +18,9 @@ //! `.pat` file is `8BPT`, version u16 (1), count u32, then the patterns without length prefixes. //! Photoshop writes the channel count as 24 and only marks the real channels as written; like //! ag-psd (MIT) we take the first written arrays as the colour channels and the next one as -//! transparency. +//! transparency. Photoshop stores a pattern's transparency in the last (sheet mask) array and +//! leaves the user mask unwritten (psd-tools multichannel-pattern-fill.psd, Photoshop CC 2019); a +//! file with transparency in the user mask array is refused by Photoshop (#8054). use crate::compression::{Compression, MAX_DECODED_BYTES, PlaneLayout, decode_planes, encode_planes}; use crate::error::{PsdError, Result}; @@ -29,7 +31,7 @@ const MAX_EDGE: u32 = 30_000; /// Budget for zero-padded channel planes summed over every pattern of one parse (a block, a `.pat` /// file, an `.abr`). Photoshop writes every channel at the pattern rectangle, so a smaller channel /// rectangle is only padding; without this cap a few hundred bytes (1x1 channels, a huge pattern -/// rectangle) would allocate up to `MAX_DECODED_BYTES` of zeros. Matches `abr::MAX_TOTAL_BYTES`. +/// rectangle) would allocate up to `MAX_DECODED_BYTES` of zeros. const MAX_PADDED_PATTERN_BYTES: u64 = 512 << 20; // A PackBits run packet emits at most 128 bytes from a 2-byte packet. const MAX_RLE_EXPANSION: u64 = 64; @@ -56,6 +58,12 @@ impl PatternBudget { PatternBudget { decoded_left: d.decoded_left.min(decoded), padded_left: d.padded_left.min(decoded) } } + /// Cap both decoded and padded bytes to the amount still available to a containing file. + pub fn cap_decoded_to(&mut self, decoded: u64) { + self.decoded_left = self.decoded_left.min(decoded); + self.padded_left = self.padded_left.min(decoded); + } + /// Decoded bytes still available. pub fn decoded_left(&self) -> u64 { self.decoded_left @@ -137,7 +145,15 @@ impl<'a> Rd<'a> { } } -fn read_pattern(r: &mut Rd, budget: &mut PatternBudget) -> Result { +/// A pattern's mode, name, id and palette, up to its virtual memory array list. +struct PatternHeader { + mode: u32, + name: String, + id: String, + palette: Option>, +} + +fn read_header(r: &mut Rd) -> Result { let version = r.u32()?; if version != 1 { return Err(PsdError::invalid(format!("pattern version {version}"))); @@ -163,12 +179,22 @@ fn read_pattern(r: &mut Rd, budget: &mut PatternBudget) -> Result { } else { None }; + Ok(PatternHeader { mode, name, id, palette }) +} + +/// The body of a pattern's virtual memory array list (after its version and length). +fn read_vma_body<'a>(r: &mut Rd<'a>) -> Result<&'a [u8]> { let vver = r.u32()?; if vver != 3 { return Err(PsdError::invalid(format!("virtual memory array list version {vver}"))); } let len = r.u32()? as usize; - let body = r.take(len)?; + r.take(len) +} + +fn read_pattern(r: &mut Rd, budget: &mut PatternBudget) -> Result { + let PatternHeader { mode, name, id, palette } = read_header(r)?; + let body = read_vma_body(r)?; let mut v = Rd { b: body, p: 0 }; let [top, left, bottom, right] = v.rect()?; let w = right.checked_sub(left).ok_or(PsdError::LimitExceeded("pattern width overflow"))?; @@ -365,6 +391,10 @@ fn write_pattern(p: &PsdPattern, out: &mut Vec) -> Result<()> { out.extend_from_slice(&(p.height as u16).to_be_bytes()); out.extend_from_slice(&(p.width as u16).to_be_bytes()); let units: Vec = p.name.encode_utf16().chain(std::iter::once(0)).collect(); + // The reader refuses longer names (see `read_pattern`). + if units.len() > 65_536 { + return Err(PsdError::LimitExceeded("pattern name length")); + } out.extend_from_slice(&(units.len() as u32).to_be_bytes()); for u in units { out.extend_from_slice(&u.to_be_bytes()); @@ -407,12 +437,12 @@ fn write_pattern(p: &PsdPattern, out: &mut Vec) -> Result<()> { for _ in nc..SLOTS as usize { body.extend_from_slice(&0u32.to_be_bytes()); } - // User mask slot: transparency; sheet mask slot: unused. + // User mask slot: unused; sheet mask slot: transparency, where Photoshop keeps it (#8054). + body.extend_from_slice(&0u32.to_be_bytes()); match &p.alpha { Some(a) => array(a, &mut body)?, None => body.extend_from_slice(&0u32.to_be_bytes()), } - body.extend_from_slice(&0u32.to_be_bytes()); out.extend_from_slice(&3u32.to_be_bytes()); out.extend_from_slice(&(body.len() as u32).to_be_bytes()); out.extend_from_slice(&body); @@ -425,9 +455,8 @@ pub fn parse_pattern_block(data: &[u8]) -> Result> { } /// [`parse_pattern_block`] charging `budget`, so several blocks of one document share one limit. -/// The budget is charged only for patterns that are returned. +/// Charges are monotonic even when a later record in the block fails. pub fn parse_pattern_block_with(data: &[u8], budget: &mut PatternBudget) -> Result> { - let mut left = budget.clone(); let mut r = Rd { b: data, p: 0 }; let mut out = Vec::new(); while r.p + 4 <= data.len() { @@ -436,14 +465,55 @@ pub fn parse_pattern_block_with(data: &[u8], budget: &mut PatternBudget) -> Resu break; } let body = r.take(len)?; - out.push(read_pattern(&mut Rd { b: body, p: 0 }, &mut left)?); + out.push(read_pattern(&mut Rd { b: body, p: 0 }, budget)?); let pad = (4 - len % 4) % 4; r.p = (r.p + pad).min(data.len()); } - *budget = left; Ok(out) } +/// Whether `Patt` / `Pat2` / `Pat3` block data keeps a pattern's transparency in the user mask +/// array, as PhotoCraft wrote it before #8054. Photoshop refuses such a file, so a save must +/// rewrite the block rather than keep it. Unreadable data counts as `false`. +pub fn block_has_user_mask(data: &[u8]) -> bool { + let mut r = Rd { b: data, p: 0 }; + while r.p + 4 <= data.len() { + let Ok(len) = r.u32() else { return false }; + if len == 0 { + break; + } + let Ok(body) = r.take(len as usize) else { return false }; + if pattern_has_user_mask(&mut Rd { b: body, p: 0 }).unwrap_or(false) { + return true; + } + let pad = (4 - len as usize % 4) % 4; + r.p = (r.p + pad).min(data.len()); + } + false +} + +fn pattern_has_user_mask(r: &mut Rd) -> Result { + read_header(r)?; + let mut v = Rd { b: read_vma_body(r)?, p: 0 }; + v.rect()?; + let count = v.u32()?; + // Every array takes at least 4 bytes, so the body bounds this loop whatever `count` says. + for i in 0..count.saturating_add(2) { + if v.p >= v.b.len() { + break; + } + if v.u32()? == 0 { + continue; + } + let alen = v.u32()? as usize; + if i == count && alen != 0 { + return Ok(true); + } + v.take(alen)?; + } + Ok(false) +} + /// Serializes patterns as `Patt` / `Pat2` / `Pat3` block data. pub fn write_pattern_block(patterns: &[PsdPattern]) -> Result> { let mut out = Vec::new(); @@ -467,6 +537,9 @@ pub fn block_key(depth: u16) -> [u8; 4] { } } +/// The most patterns a `.pat` file may hold: more are refused on read and on write. +pub const MAX_PAT_PATTERNS: usize = 100_000; + /// Parses a `.pat` pattern file. pub fn parse_pat_file(data: &[u8]) -> Result> { let mut r = Rd { b: data, p: 0 }; @@ -475,10 +548,13 @@ pub fn parse_pat_file(data: &[u8]) -> Result> { return Err(PsdError::InvalidSignature { expected: "8BPT", found: [sig[0], sig[1], sig[2], sig[3]] }); } let _version = r.u16()?; - let count = r.u32()?; + let count = r.u32()? as usize; + if count > MAX_PAT_PATTERNS { + return Err(PsdError::LimitExceeded("pattern count")); + } let mut out = Vec::new(); let mut budget = PatternBudget::default(); - for _ in 0..count.min(100_000) { + for _ in 0..count { out.push(read_pattern(&mut r, &mut budget)?); } Ok(out) @@ -486,6 +562,9 @@ pub fn parse_pat_file(data: &[u8]) -> Result> { /// Writes a `.pat` pattern file. pub fn write_pat_file(patterns: &[PsdPattern]) -> Result> { + if patterns.len() > MAX_PAT_PATTERNS { + return Err(PsdError::LimitExceeded("pattern count")); + } let mut out = b"8BPT".to_vec(); out.extend_from_slice(&1u16.to_be_bytes()); out.extend_from_slice(&(patterns.len() as u32).to_be_bytes()); @@ -496,281 +575,4 @@ pub fn write_pat_file(patterns: &[PsdPattern]) -> Result> { } #[cfg(test)] -mod tests { - use super::*; - - fn pat(depth: u16, alpha: bool) -> PsdPattern { - let (w, h) = (5u32, 3u32); - let bpp = usize::from(depth / 8); - let plane = |s: u8| (0..w as usize * h as usize * bpp).map(|i| (i as u8).wrapping_mul(7).wrapping_add(s)).collect::>(); - PsdPattern { - mode: 3, - width: w, - height: h, - name: "Test ✓".into(), - id: "0bd2d3ba-1234-11d4-8f8f-aabbccddeeff".into(), - palette: None, - depth, - channels: vec![plane(1), plane(2), plane(3)], - alpha: alpha.then(|| plane(9)), - } - } - - #[test] - fn block_and_pat_file_round_trip_at_every_depth() { - for depth in [8, 16, 32] { - for alpha in [false, true] { - let ps = vec![pat(depth, alpha), PsdPattern { name: "Second".into(), ..pat(depth, false) }]; - let blk = write_pattern_block(&ps).unwrap(); - assert_eq!(blk.len() % 4, 0); - assert_eq!(parse_pattern_block(&blk).unwrap(), ps); - let f = write_pat_file(&ps).unwrap(); - assert_eq!(parse_pat_file(&f).unwrap(), ps); - // Re-serialising is byte-stable. - assert_eq!(write_pattern_block(&parse_pattern_block(&blk).unwrap()).unwrap(), blk); - } - } - } - - #[test] - fn malformed_input_errors_without_panicking() { - let blk = write_pattern_block(&[pat(8, true)]).unwrap(); - for cut in [3, 10, 40, blk.len() - 5] { - let _ = parse_pattern_block(&blk[..cut]); - } - assert!(parse_pat_file(b"8BPS\0\x01").is_err()); - assert!(parse_pattern_block(&[]).unwrap().is_empty()); - } - - fn pattern_vma_rect_offset(pattern_start: usize, p: &PsdPattern) -> usize { - let name_units = p.name.encode_utf16().count() + 1; - pattern_start + 16 + name_units * 2 + 1 + p.id.len() + if p.mode == 2 { 768 } else { 0 } + 8 - } - - fn set_i32(bytes: &mut [u8], offset: usize, value: i32) { - bytes[offset..offset + 4].copy_from_slice(&value.to_be_bytes()); - } - - #[test] - fn hostile_pattern_rectangles_are_rejected_through_all_import_routes() { - let p = pat(8, false); - - // A subtraction of the hostile endpoints overflowed before rectangle validation. - let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - set_i32(&mut global, vma + 4, i32::MIN); - set_i32(&mut global, vma + 12, i32::MAX); - assert!(parse_pattern_block(&global).is_err()); - - let mut standalone = write_pat_file(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(10, &p); - set_i32(&mut standalone, vma + 4, i32::MIN); - set_i32(&mut standalone, vma + 12, i32::MAX); - assert!(parse_pat_file(&standalone).is_err()); - - let mut abr = crate::abr::write_v6( - 1, - &[crate::abr::AbrSample { id: String::new(), width: 1, height: 1, depth: 8, data: vec![0] }], - std::slice::from_ref(&p), - &[], - true, - ) - .unwrap(); - let section = abr.windows(4).position(|w| w == b"patt").unwrap(); - let vma = pattern_vma_rect_offset(section + 8, &p) + 4; - set_i32(&mut abr, vma + 4, i32::MIN); - set_i32(&mut abr, vma + 12, i32::MAX); - let parsed = crate::abr::parse(&abr).unwrap(); - assert!(parsed.patterns.is_empty()); - assert!(parsed.warnings.iter().any(|warning| warning.contains("embedded patterns unreadable"))); - } - - #[test] - fn huge_pattern_rect_with_tiny_channel_payload_is_rejected() { - // RGB32 needs 10.8 GB; CMYK16 plus alpha needs 9 GB. Both exceed the - // existing pattern budget, but their first plane fits on 64-bit targets. - // The complete metadata must be checked before allocating that first plane. - huge_pattern_rect_is_rejected(pat(32, false)); - let mut cmyk = pat(16, true); - cmyk.mode = 4; - cmyk.channels.push(vec![0; 5 * 3 * 2]); - huge_pattern_rect_is_rejected(cmyk); - } - - fn huge_pattern_rect_is_rejected(p: PsdPattern) { - let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - set_i32(&mut global, vma + 8, 30_000); - set_i32(&mut global, vma + 12, 30_000); - assert!(matches!(parse_pattern_block(&global), Err(PsdError::LimitExceeded(_)))); - - let mut standalone = write_pat_file(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(10, &p); - set_i32(&mut standalone, vma + 8, 30_000); - set_i32(&mut standalone, vma + 12, 30_000); - assert!(matches!(parse_pat_file(&standalone), Err(PsdError::LimitExceeded(_)))); - - let mut abr = crate::abr::write_v6( - 1, - &[crate::abr::AbrSample { id: String::new(), width: 1, height: 1, depth: 8, data: vec![0] }], - std::slice::from_ref(&p), - &[], - true, - ) - .unwrap(); - let section = abr.windows(4).position(|w| w == b"patt").unwrap(); - let vma = pattern_vma_rect_offset(section + 8, &p) + 4; - set_i32(&mut abr, vma + 8, 30_000); - set_i32(&mut abr, vma + 12, 30_000); - let parsed = crate::abr::parse(&abr).unwrap(); - assert!(parsed.patterns.is_empty()); - assert!(parsed.warnings.iter().any(|warning| warning.contains("embedded patterns unreadable"))); - } - - #[test] - fn offset_channel_rectangles_are_placed_inside_the_pattern() { - for depth in [8, 16, 32] { - let original = pat(depth, true); - let mut block = write_pattern_block(std::slice::from_ref(&original)).unwrap(); - let vma = pattern_vma_rect_offset(4, &original); - set_i32(&mut block, vma, -2); - set_i32(&mut block, vma + 4, -3); - set_i32(&mut block, vma + 8, 4); - set_i32(&mut block, vma + 12, 5); - - let parsed = parse_pattern_block(&block).unwrap(); - let parsed = parsed.first().unwrap(); - assert_eq!((parsed.width, parsed.height), (8, 6)); - let bytes_per_sample = usize::from(depth / 8); - for (actual, expected) in parsed.channels.iter().zip(&original.channels).chain(parsed.alpha.iter().zip(original.alpha.iter())) { - for y in 0..6usize { - for x in 0..8usize { - let at = (y * 8 + x) * bytes_per_sample; - let actual_sample = actual.get(at..at + bytes_per_sample).unwrap(); - if (2..5).contains(&y) && (3..8).contains(&x) { - let source_at = ((y - 2) * 5 + (x - 3)) * bytes_per_sample; - assert_eq!(actual_sample, expected.get(source_at..source_at + bytes_per_sample).unwrap()); - } else { - assert!(actual_sample.iter().all(|&sample| sample == 0)); - } - } - } - } - } - } - - #[test] - fn one_bit_offset_channel_placement_preserves_msb_first_pixels() { - let source = PlaneLayout { planes: 1, width: 5, height: 2, depth: 1, version: Version::Psd }; - let destination = PlaneLayout { planes: 1, width: 8, height: 4, depth: 1, version: Version::Psd }; - let placed = place_channel_plane(vec![0b1010_1000, 0b0101_0000], &source, 2, 1, &destination).unwrap(); - assert_eq!(placed, [0, 0b0010_1010, 0b0001_0100, 0]); - } - - #[test] - fn full_size_channels_reuse_the_decoded_allocation() { - for depth in [1, 8, 16, 32] { - let layout = PlaneLayout { planes: 1, width: 5, height: 3, depth, version: Version::Psd }; - let plane = vec![0x55; layout.decoded_len().unwrap()]; - let original = plane.as_ptr(); - let placed = place_channel_plane(plane, &layout, 0, 0, &layout).unwrap(); - assert_eq!(placed.as_ptr(), original); - let expected = if depth == 1 { 0x50 } else { 0x55 }; - assert!(placed.iter().all(|&byte| byte == expected)); - } - } - - #[test] - fn large_channel_padding_within_the_existing_budget_is_allowed() { - let mut p = pat(8, false); - p.mode = 1; - let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - // More than 64 MiB of implicit padding is valid: the fix must not introduce - // a smaller, padding-specific format limit to avoid the oversized RGB case. - set_i32(&mut block, vma + 8, 8193); - set_i32(&mut block, vma + 12, 8193); - let parsed = parse_pattern_block(&block).unwrap(); - let plane = &parsed[0].channels[0]; - assert_eq!(plane.len(), 8193 * 8193); - for y in 0..3 { - assert_eq!(&plane[y * 8193..y * 8193 + 5], &p.channels[0][y * 5..y * 5 + 5]); - } - assert_eq!(plane[5], 0); - assert_eq!(plane[plane.len() - 1], 0); - } - - #[test] - fn padded_plane_allocation_failure_is_an_error() { - // Capacity overflow is deterministic and never asks the OS for huge memory. - assert!(matches!(zeroed_pattern_plane(usize::MAX), Err(PsdError::LimitExceeded("not enough memory for the padded pattern channel")))); - } - - #[test] - fn tiny_channels_in_a_huge_pattern_rect_are_not_zero_padded_without_bound() { - // 3 channels of 5x3 padded to 30000x30000 would allocate 2.7 GB of zeros. - let p = pat(8, false); - let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - set_i32(&mut global, vma + 8, 30_000); - set_i32(&mut global, vma + 12, 30_000); - assert!(matches!(parse_pattern_block(&global), Err(PsdError::LimitExceeded("padded pattern channels exceed MAX_PADDED_PATTERN_BYTES")))); - } - - /// A one-channel gray pattern whose 5x3 channel is padded into a `size`x`size` rectangle. - fn padded_gray_block(size: i32) -> Vec { - let mut p = pat(8, false); - p.mode = 1; - p.channels.truncate(1); - let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - set_i32(&mut block, vma + 8, size); - set_i32(&mut block, vma + 12, size); - block - } - - #[test] - fn padding_budget_is_shared_by_every_pattern_of_a_parse() { - // Each pattern pads 100x100 = 10000 bytes; a 15000-byte budget fits one, not two. Before - // the fix each pattern got a fresh allowance, so N tiny patterns committed N times the cap. - let one = padded_gray_block(100); - let mut budget = PatternBudget::with_decoded_limit(15_000); - assert_eq!(parse_pattern_block_with(&one, &mut budget).unwrap().len(), 1); - assert_eq!(budget.decoded_left(), 5_000); - - let two = [one.clone(), one.clone()].concat(); - let mut budget = PatternBudget::with_decoded_limit(15_000); - assert!(matches!(parse_pattern_block_with(&two, &mut budget), Err(PsdError::LimitExceeded(_)))); - // A failed parse keeps nothing, so it charges nothing. - assert_eq!(budget.decoded_left(), 15_000); - - // Two blocks of one document share a budget too. - let mut budget = PatternBudget::with_decoded_limit(15_000); - assert!(parse_pattern_block_with(&one, &mut budget).is_ok()); - assert!(parse_pattern_block_with(&one, &mut budget).is_err()); - } - - #[test] - fn channels_at_different_depths_are_rejected() { - // The second colour channel claims 8 bits in a 16-bit pattern. It used to be accepted with - // the pattern's depth taken from the last channel, so the result couldn't be written back. - let p = pat(16, false); - let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let first = pattern_vma_rect_offset(4, &p) + 20; - let first_len = u32::from_be_bytes(block[first + 4..first + 8].try_into().unwrap()) as usize; - let second = first + 8 + first_len; - block[second + 8..second + 12].copy_from_slice(&8u32.to_be_bytes()); - block[second + 28..second + 30].copy_from_slice(&8u16.to_be_bytes()); - let err = parse_pattern_block(&block).unwrap_err().to_string(); - assert!(err.contains("different depths"), "{err}"); - } - - #[test] - fn zero_size_pattern_rectangles_are_rejected() { - let p = pat(8, false); - let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); - let vma = pattern_vma_rect_offset(4, &p); - set_i32(&mut block, vma + 8, 0); - assert!(parse_pattern_block(&block).is_err()); - } -} +mod tests; diff --git a/crates/psd/src/patterns/tests.rs b/crates/psd/src/patterns/tests.rs new file mode 100644 index 0000000000..c66c8189d4 --- /dev/null +++ b/crates/psd/src/patterns/tests.rs @@ -0,0 +1,352 @@ +use super::*; + +fn pat(depth: u16, alpha: bool) -> PsdPattern { + let (w, h) = (5u32, 3u32); + let bpp = usize::from(depth / 8); + let plane = |s: u8| (0..w as usize * h as usize * bpp).map(|i| (i as u8).wrapping_mul(7).wrapping_add(s)).collect::>(); + PsdPattern { + mode: 3, + width: w, + height: h, + name: "Test ✓".into(), + id: "0bd2d3ba-1234-11d4-8f8f-aabbccddeeff".into(), + palette: None, + depth, + channels: vec![plane(1), plane(2), plane(3)], + alpha: alpha.then(|| plane(9)), + } +} + +/// #9318 review: a name the reader refuses (more than 65,536 UTF-16 units with its NUL) is +/// refused on write, so a saved library can't become a file that loads nothing. +#[test] +fn a_name_the_reader_refuses_is_refused_on_write() { + let long = PsdPattern { name: "a".repeat(65_536), ..pat(8, false) }; + assert!(matches!(write_pat_file(&[long]), Err(PsdError::LimitExceeded(_)))); + let longest = PsdPattern { name: "a".repeat(65_535), ..pat(8, false) }; + assert_eq!(parse_pat_file(&write_pat_file(std::slice::from_ref(&longest)).unwrap()).unwrap(), [longest]); +} + +/// #9318 review: more patterns than a `.pat` holds are refused on write, and a file claiming +/// more is refused on read rather than silently cut to the first 100,000. +#[test] +fn more_patterns_than_a_pat_file_holds_are_refused() { + let many = vec![pat(8, false); MAX_PAT_PATTERNS + 1]; + assert!(matches!(write_pat_file(&many), Err(PsdError::LimitExceeded(_)))); + let mut header = b"8BPT".to_vec(); + header.extend_from_slice(&1u16.to_be_bytes()); + header.extend_from_slice(&(MAX_PAT_PATTERNS as u32 + 1).to_be_bytes()); + assert!(matches!(parse_pat_file(&header), Err(PsdError::LimitExceeded(_)))); +} + +#[test] +fn block_and_pat_file_round_trip_at_every_depth() { + for depth in [8, 16, 32] { + for alpha in [false, true] { + let ps = vec![pat(depth, alpha), PsdPattern { name: "Second".into(), ..pat(depth, false) }]; + let blk = write_pattern_block(&ps).unwrap(); + assert_eq!(blk.len() % 4, 0); + assert_eq!(parse_pattern_block(&blk).unwrap(), ps); + let f = write_pat_file(&ps).unwrap(); + assert_eq!(parse_pat_file(&f).unwrap(), ps); + // Re-serialising is byte-stable. + assert_eq!(write_pattern_block(&parse_pattern_block(&blk).unwrap()).unwrap(), blk); + } + } +} + +/// Offsets of the virtual memory arrays of a one-pattern block (each at its "written" flag), +/// with whether each is written, and the end of the array list. +fn vma_slots(block: &[u8], p: &PsdPattern) -> (Vec<(usize, bool)>, usize) { + let rect = pattern_vma_rect_offset(4, p); + let end = rect + u32::from_be_bytes(block[rect - 4..rect].try_into().unwrap()) as usize; + let (mut at, mut slots) = (rect + 20, Vec::new()); + while at < end { + let written = u32::from_be_bytes(block[at..at + 4].try_into().unwrap()) != 0; + slots.push((at, written)); + at += 4; + if written { + at += 4 + u32::from_be_bytes(block[at..at + 4].try_into().unwrap()) as usize; + } + } + (slots, end) +} + +#[test] +fn transparency_goes_in_the_sheet_mask_array_like_photoshop() { + // #8054: transparency in the user mask array (slot 24) made Photoshop refuse the file; + // Photoshop keeps it in the sheet mask array (slot 25). + for depth in [8, 16, 32] { + let p = pat(depth, true); + let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let (slots, end) = vma_slots(&block, &p); + let written: Vec = slots.iter().enumerate().filter(|(_, s)| s.1).map(|(i, _)| i).collect(); + assert_eq!((slots.len(), written), (26, vec![0, 1, 2, 25]), "{depth}-bit"); + assert!(!block_has_user_mask(&block)); + // The layout older PhotoCraft builds wrote still opens, and is detected. + block[slots[24].0..end].rotate_left(4); + let (slots, _) = vma_slots(&block, &p); + assert!(slots[24].1 && !slots[25].1); + assert!(block_has_user_mask(&block)); + assert_eq!(parse_pattern_block(&block).unwrap(), vec![p]); + } + assert!(!block_has_user_mask(&write_pattern_block(&[pat(8, false)]).unwrap())); + for junk in [&[][..], &[0, 0, 0, 9, 1], &[0xff; 64]] { + assert!(!block_has_user_mask(junk)); + } +} + +#[test] +fn malformed_input_errors_without_panicking() { + let blk = write_pattern_block(&[pat(8, true)]).unwrap(); + for cut in [3, 10, 40, blk.len() - 5] { + let _ = parse_pattern_block(&blk[..cut]); + } + assert!(parse_pat_file(b"8BPS\0\x01").is_err()); + assert!(parse_pattern_block(&[]).unwrap().is_empty()); +} + +fn pattern_vma_rect_offset(pattern_start: usize, p: &PsdPattern) -> usize { + let name_units = p.name.encode_utf16().count() + 1; + pattern_start + 16 + name_units * 2 + 1 + p.id.len() + if p.mode == 2 { 768 } else { 0 } + 8 +} + +fn set_i32(bytes: &mut [u8], offset: usize, value: i32) { + bytes[offset..offset + 4].copy_from_slice(&value.to_be_bytes()); +} + +#[test] +fn hostile_pattern_rectangles_are_rejected_through_all_import_routes() { + let p = pat(8, false); + + // A subtraction of the hostile endpoints overflowed before rectangle validation. + let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + set_i32(&mut global, vma + 4, i32::MIN); + set_i32(&mut global, vma + 12, i32::MAX); + assert!(parse_pattern_block(&global).is_err()); + + let mut standalone = write_pat_file(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(10, &p); + set_i32(&mut standalone, vma + 4, i32::MIN); + set_i32(&mut standalone, vma + 12, i32::MAX); + assert!(parse_pat_file(&standalone).is_err()); + + let mut abr = crate::abr::write_v6( + 1, + &[crate::abr::AbrSample { id: String::new(), width: 1, height: 1, depth: 8, data: vec![0] }], + std::slice::from_ref(&p), + &[], + true, + ) + .unwrap(); + let section = abr.windows(4).position(|w| w == b"patt").unwrap(); + let vma = pattern_vma_rect_offset(section + 8, &p) + 4; + set_i32(&mut abr, vma + 4, i32::MIN); + set_i32(&mut abr, vma + 12, i32::MAX); + let parsed = crate::abr::parse(&abr).unwrap(); + assert!(parsed.patterns.is_empty()); + assert!(parsed.warnings.iter().any(|warning| warning.contains("embedded patterns unreadable"))); +} + +#[test] +fn huge_pattern_rect_with_tiny_channel_payload_is_rejected() { + // RGB32 needs 10.8 GB; CMYK16 plus alpha needs 9 GB. Both exceed the + // existing pattern budget, but their first plane fits on 64-bit targets. + // The complete metadata must be checked before allocating that first plane. + huge_pattern_rect_is_rejected(pat(32, false)); + let mut cmyk = pat(16, true); + cmyk.mode = 4; + cmyk.channels.push(vec![0; 5 * 3 * 2]); + huge_pattern_rect_is_rejected(cmyk); +} + +fn huge_pattern_rect_is_rejected(p: PsdPattern) { + let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + set_i32(&mut global, vma + 8, 30_000); + set_i32(&mut global, vma + 12, 30_000); + assert!(matches!(parse_pattern_block(&global), Err(PsdError::LimitExceeded(_)))); + + let mut standalone = write_pat_file(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(10, &p); + set_i32(&mut standalone, vma + 8, 30_000); + set_i32(&mut standalone, vma + 12, 30_000); + assert!(matches!(parse_pat_file(&standalone), Err(PsdError::LimitExceeded(_)))); + + let mut abr = crate::abr::write_v6( + 1, + &[crate::abr::AbrSample { id: String::new(), width: 1, height: 1, depth: 8, data: vec![0] }], + std::slice::from_ref(&p), + &[], + true, + ) + .unwrap(); + let section = abr.windows(4).position(|w| w == b"patt").unwrap(); + let vma = pattern_vma_rect_offset(section + 8, &p) + 4; + set_i32(&mut abr, vma + 8, 30_000); + set_i32(&mut abr, vma + 12, 30_000); + let parsed = crate::abr::parse(&abr).unwrap(); + assert!(parsed.patterns.is_empty()); + assert!(parsed.warnings.iter().any(|warning| warning.contains("embedded patterns unreadable"))); +} + +#[test] +fn offset_channel_rectangles_are_placed_inside_the_pattern() { + for depth in [8, 16, 32] { + let original = pat(depth, true); + let mut block = write_pattern_block(std::slice::from_ref(&original)).unwrap(); + let vma = pattern_vma_rect_offset(4, &original); + set_i32(&mut block, vma, -2); + set_i32(&mut block, vma + 4, -3); + set_i32(&mut block, vma + 8, 4); + set_i32(&mut block, vma + 12, 5); + + let parsed = parse_pattern_block(&block).unwrap(); + let parsed = parsed.first().unwrap(); + assert_eq!((parsed.width, parsed.height), (8, 6)); + let bytes_per_sample = usize::from(depth / 8); + for (actual, expected) in parsed.channels.iter().zip(&original.channels).chain(parsed.alpha.iter().zip(original.alpha.iter())) { + for y in 0..6usize { + for x in 0..8usize { + let at = (y * 8 + x) * bytes_per_sample; + let actual_sample = actual.get(at..at + bytes_per_sample).unwrap(); + if (2..5).contains(&y) && (3..8).contains(&x) { + let source_at = ((y - 2) * 5 + (x - 3)) * bytes_per_sample; + assert_eq!(actual_sample, expected.get(source_at..source_at + bytes_per_sample).unwrap()); + } else { + assert!(actual_sample.iter().all(|&sample| sample == 0)); + } + } + } + } + } +} + +#[test] +fn one_bit_offset_channel_placement_preserves_msb_first_pixels() { + let source = PlaneLayout { planes: 1, width: 5, height: 2, depth: 1, version: Version::Psd }; + let destination = PlaneLayout { planes: 1, width: 8, height: 4, depth: 1, version: Version::Psd }; + let placed = place_channel_plane(vec![0b1010_1000, 0b0101_0000], &source, 2, 1, &destination).unwrap(); + assert_eq!(placed, [0, 0b0010_1010, 0b0001_0100, 0]); +} + +#[test] +fn full_size_channels_reuse_the_decoded_allocation() { + for depth in [1, 8, 16, 32] { + let layout = PlaneLayout { planes: 1, width: 5, height: 3, depth, version: Version::Psd }; + let plane = vec![0x55; layout.decoded_len().unwrap()]; + let original = plane.as_ptr(); + let placed = place_channel_plane(plane, &layout, 0, 0, &layout).unwrap(); + assert_eq!(placed.as_ptr(), original); + let expected = if depth == 1 { 0x50 } else { 0x55 }; + assert!(placed.iter().all(|&byte| byte == expected)); + } +} + +#[test] +fn large_channel_padding_within_the_existing_budget_is_allowed() { + let mut p = pat(8, false); + p.mode = 1; + let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + // More than 64 MiB of implicit padding is valid: the fix must not introduce + // a smaller, padding-specific format limit to avoid the oversized RGB case. + set_i32(&mut block, vma + 8, 8193); + set_i32(&mut block, vma + 12, 8193); + let parsed = parse_pattern_block(&block).unwrap(); + let plane = &parsed[0].channels[0]; + assert_eq!(plane.len(), 8193 * 8193); + for y in 0..3 { + assert_eq!(&plane[y * 8193..y * 8193 + 5], &p.channels[0][y * 5..y * 5 + 5]); + } + assert_eq!(plane[5], 0); + assert_eq!(plane[plane.len() - 1], 0); +} + +#[test] +fn padded_plane_allocation_failure_is_an_error() { + // Capacity overflow is deterministic and never asks the OS for huge memory. + assert!(matches!(zeroed_pattern_plane(usize::MAX), Err(PsdError::LimitExceeded("not enough memory for the padded pattern channel")))); +} + +#[test] +fn tiny_channels_in_a_huge_pattern_rect_are_not_zero_padded_without_bound() { + // 3 channels of 5x3 padded to 30000x30000 would allocate 2.7 GB of zeros. + let p = pat(8, false); + let mut global = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + set_i32(&mut global, vma + 8, 30_000); + set_i32(&mut global, vma + 12, 30_000); + assert!(matches!(parse_pattern_block(&global), Err(PsdError::LimitExceeded("padded pattern channels exceed MAX_PADDED_PATTERN_BYTES")))); +} + +/// A one-channel gray pattern whose 5x3 channel is padded into a `size`x`size` rectangle. +fn padded_gray_block(size: i32) -> Vec { + let mut p = pat(8, false); + p.mode = 1; + p.channels.truncate(1); + let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + set_i32(&mut block, vma + 8, size); + set_i32(&mut block, vma + 12, size); + block +} + +#[test] +fn failed_pattern_block_keeps_partial_charge_for_the_next_block() { + let one = padded_gray_block(100); + let mut malformed = one.clone(); + malformed.extend_from_slice(&100u32.to_be_bytes()); + let mut budget = PatternBudget::with_decoded_limit(15_000); + assert!(parse_pattern_block_with(&malformed, &mut budget).is_err()); + assert_eq!(budget.decoded_left(), 5_000); + assert!(parse_pattern_block_with(&one, &mut budget).is_err()); + assert_eq!(budget.decoded_left(), 5_000); +} + +#[test] +fn padding_budget_is_shared_by_every_pattern_of_a_parse() { + // Each pattern pads 100x100 = 10000 bytes; a 15000-byte budget fits one, not two. Before + // the fix each pattern got a fresh allowance, so N tiny patterns committed N times the cap. + let one = padded_gray_block(100); + let mut budget = PatternBudget::with_decoded_limit(15_000); + assert_eq!(parse_pattern_block_with(&one, &mut budget).unwrap().len(), 1); + assert_eq!(budget.decoded_left(), 5_000); + + let two = [one.clone(), one.clone()].concat(); + let mut budget = PatternBudget::with_decoded_limit(15_000); + assert!(matches!(parse_pattern_block_with(&two, &mut budget), Err(PsdError::LimitExceeded(_)))); + // Charges are monotonic: the first pattern's 10000 bytes stay charged even when the second + // record fails. + assert_eq!(budget.decoded_left(), 5_000); + + // Two blocks of one document share a budget too. + let mut budget = PatternBudget::with_decoded_limit(15_000); + assert!(parse_pattern_block_with(&one, &mut budget).is_ok()); + assert!(parse_pattern_block_with(&one, &mut budget).is_err()); +} + +#[test] +fn channels_at_different_depths_are_rejected() { + // The second colour channel claims 8 bits in a 16-bit pattern. It used to be accepted with + // the pattern's depth taken from the last channel, so the result couldn't be written back. + let p = pat(16, false); + let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let first = pattern_vma_rect_offset(4, &p) + 20; + let first_len = u32::from_be_bytes(block[first + 4..first + 8].try_into().unwrap()) as usize; + let second = first + 8 + first_len; + block[second + 8..second + 12].copy_from_slice(&8u32.to_be_bytes()); + block[second + 28..second + 30].copy_from_slice(&8u16.to_be_bytes()); + let err = parse_pattern_block(&block).unwrap_err().to_string(); + assert!(err.contains("different depths"), "{err}"); +} + +#[test] +fn zero_size_pattern_rectangles_are_rejected() { + let p = pat(8, false); + let mut block = write_pattern_block(std::slice::from_ref(&p)).unwrap(); + let vma = pattern_vma_rect_offset(4, &p); + set_i32(&mut block, vma + 8, 0); + assert!(parse_pattern_block(&block).is_err()); +} diff --git a/crates/psd/src/pixels.rs b/crates/psd/src/pixels.rs index d4948457f9..47828c82d5 100644 --- a/crates/psd/src/pixels.rs +++ b/crates/psd/src/pixels.rs @@ -180,6 +180,20 @@ fn color_channel_count(mode: ColorMode) -> Result { } } +/// One 8-bit sample matted against white at alpha `a`, back to straight colour (rounded, at most +/// 255). At `a == 0` there is no colour to recover and the sample is kept. +fn unmatte_u8(c: u8, a: u8) -> u8 { + let a = u32::from(a); + ((u32::from(c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a).map_or(c, |straight| straight.min(255) as u8) +} + +/// [`unmatte_u8`] of every sample, one row per alpha: `table[a][c]`. Built per call (64 KiB, a +/// division per entry) rather than per pixel, and not as a static, which would add 64 KiB to +/// every binary that links this crate. +fn unmatte_u8_table() -> Vec<[u8; 256]> { + (0..=255).map(|a| std::array::from_fn(|c| unmatte_u8(c as u8, a))).collect() +} + fn unmatte_to_u8(color: &[u8], alpha: &[u8], depth: u16) -> Result> { let convert = |c: f32, a: f32| { let straight = if a > 0.0 { (c + a - 1.0) / a } else { c }; @@ -231,12 +245,14 @@ impl<'a> Layer<'a> { let rect = self.record.rect; let (w, h) = rect.size()?; let n = w.checked_mul(h).ok_or(PsdError::LimitExceeded("layer size overflow"))?; - if n.checked_mul(4).is_none_or(|len| len as u64 > crate::compression::MAX_DECODED_BYTES) { - return Err(PsdError::LimitExceeded("RGBA output exceeds MAX_DECODED_BYTES")); - } let depth = self.header.depth; let mode = self.header.color_mode; let cc = color_channel_count(mode)?; + // The planes (one per colour channel, filled in for channels the layer lacks, and alpha) + // coexist with the RGBA output, so the cap covers all of them, not the output alone. + if n.checked_mul(cc + 2 + 4).is_none_or(|len| len as u64 > crate::compression::MAX_DECODED_BYTES) { + return Err(PsdError::LimitExceeded("RGBA output exceeds MAX_DECODED_BYTES")); + } let mut color = Vec::with_capacity(cc); for id in 0..cc as i16 { let plane = if self.record.channel(id).is_some() { @@ -341,12 +357,11 @@ impl PsdFile { let alpha = if self.merged_has_alpha() { Some(get(cc)?) } else { None }; let mut data = interleave(h.color_mode, &color, alpha.as_deref(), &self.color_mode_data, n)?; if h.color_mode == ColorMode::Rgb && h.depth == 8 && alpha.is_some() { + let table = unmatte_u8_table(); for pixel in data.as_chunks_mut::<4>().0 { - let a = u32::from(pixel[3]); + let row = &table[usize::from(pixel[3])]; for c in pixel.iter_mut().take(3) { - if let Some(straight) = ((u32::from(*c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a) { - *c = straight.min(255) as u8; - } + *c = row[usize::from(*c)]; } } } @@ -368,6 +383,29 @@ mod tests { assert_eq!(file.decode_merged().unwrap(), native); } + /// #5694: the 8-bit RGB composite unmattes through a table; every (sample, alpha) pair comes + /// out exactly as the per-pixel division it replaced (kept here as the reference). + #[test] + fn rgba8_composite_unmattes_every_sample_and_alpha_as_the_division_did() { + let mut file = crate::testgen::merged_only(Version::Psd, ColorMode::Rgb, 8, crate::Compression::Raw, 256, 256); + file.header.channels = 4; + // Pixel (x, y): red x, green 255 - x, blue x ^ y, alpha y. + let (x, y) = (|i: usize| (i % 256) as u8, |i: usize| (i / 256) as u8); + let plane = |f: &dyn Fn(usize) -> u8| (0..256 * 256).map(f).collect::>(); + let planes = [plane(&|i| x(i)), plane(&|i| 255 - x(i)), plane(&|i| x(i) ^ y(i)), plane(&|i| y(i))]; + file.image_data = crate::ImageData::encode(crate::Compression::Raw, &planes.concat(), &file.header).unwrap(); + let mut expected: Vec = (0..256 * 256).flat_map(|i| planes.each_ref().map(|p| p[i])).collect(); + for pixel in expected.as_chunks_mut::<4>().0 { + let a = u32::from(pixel[3]); + for c in pixel.iter_mut().take(3) { + if let Some(straight) = ((u32::from(*c) + a).saturating_sub(255) * 255 + a / 2).checked_div(a) { + *c = straight.min(255) as u8; + } + } + } + assert_eq!(file.composite_rgba8().unwrap().data, expected); + } + #[test] fn oversized_layer_preview_rejects_missing_channels_before_allocating() { let mut file = crate::testgen::small(Version::Psb, crate::Compression::Raw); diff --git a/crates/psd/src/resources.rs b/crates/psd/src/resources.rs index 4d78a062be..33ce147101 100644 --- a/crates/psd/src/resources.rs +++ b/crates/psd/src/resources.rs @@ -246,6 +246,16 @@ pub(crate) fn read_section(r: &mut Reader<'_>) -> Result> { Ok(v) } +/// Serialized size of the resource section [`write_section`] writes, its length field included. +pub(crate) fn section_len(res: &[ImageResource]) -> u64 { + res.iter().fold(4u64, |n, r| { + let name = 1 + r.name.len().min(255); + let name = name + name % 2; + let data = r.data.len().saturating_add(r.data.len() % 2); + n.saturating_add((4 + 2 + name + 4) as u64).saturating_add(data as u64) + }) +} + pub(crate) fn write_section(out: &mut Vec, res: &[ImageResource]) -> Result<()> { let at = out.begin_len(false); for r in res { diff --git a/crates/psd/src/rows.rs b/crates/psd/src/rows.rs new file mode 100644 index 0000000000..6413c801df --- /dev/null +++ b/crates/psd/src/rows.rs @@ -0,0 +1,234 @@ +//! Decoding a channel a band of rows at a time: [`ChannelData::row_decoder`]. +//! +//! [`ChannelData::decode`] returns the whole plane, so turning a layer into tiles holds every +//! channel's plane plus an interleaved copy at once: for a 20000×15000 RGBA layer that is 2.4 GB +//! on top of the tiles. A [`RowDecoder`] hands out any band of rows instead. RLE and raw channels +//! are read band by band (a channel still in its [`Stored`](crate::Stored) source reads only the +//! band's bytes: RLE's per-row byte counts give each row's offset); ZIP streams cannot be entered +//! part way, so a ZIP channel is inflated once, when the decoder is made. + +use std::borrow::Cow; + +use crate::compression::{Compression, MAX_DECODED_BYTES, PlaneLayout, decode_planes, packbits}; +use crate::error::{PsdError, Result}; +use crate::header::Version; +use crate::layer::ChannelData; +use crate::source::Stored; + +/// Where a channel's encoded bytes are. +enum Bytes<'a> { + Memory(&'a [u8]), + Source(&'a Stored), +} + +impl Bytes<'_> { + fn len(&self) -> u64 { + match self { + Bytes::Memory(b) => b.len() as u64, + Bytes::Source(s) => s.len, + } + } + + fn get(&self, at: u64, len: u64) -> Result> { + match self { + Bytes::Memory(b) => { + let range = usize::try_from(at).ok().zip(usize::try_from(len).ok()).and_then(|(a, n)| b.get(a..a.checked_add(n)?)); + range + .map(Cow::Borrowed) + .ok_or(PsdError::UnexpectedEof { offset: usize::try_from(at).unwrap_or(usize::MAX), needed: usize::try_from(len).unwrap_or(usize::MAX) }) + } + Bytes::Source(s) => { + if at.checked_add(len).is_none_or(|end| end > s.len) { + return Err(PsdError::UnexpectedEof { + offset: usize::try_from(at).unwrap_or(usize::MAX), + needed: usize::try_from(len).unwrap_or(usize::MAX), + }); + } + s.read_part(at, len).map(Cow::Owned) + } + } + } +} + +enum Kind<'a> { + /// Rows at `row * row_bytes`. + Raw(Bytes<'a>), + /// `offsets[r]..offsets[r + 1]` are row `r`'s PackBits bytes (offsets from the start of the + /// data, after the count table). + Rle { bytes: Bytes<'a>, offsets: Vec }, + /// The whole decoded plane. + Decoded(Vec), + /// No data and no rows. + Empty, +} + +/// Decodes bands of rows of one channel: see the [module docs](self). +pub struct RowDecoder<'a> { + kind: Kind<'a>, + row_bytes: usize, + height: usize, +} + +impl ChannelData { + /// A decoder of this channel's rows, for a `width × height` plane. Fails where + /// [`Self::decode`] would on everything it can check without decoding the rows (sizes, the + /// RLE count table, the whole of a ZIP stream); a damaged row fails when its band is decoded. + pub fn row_decoder(&self, width: usize, height: usize, depth: u16, version: Version) -> Result> { + let layout = PlaneLayout { planes: 1, width, height, depth, version }; + let total = layout.decoded_len()?; + let row_bytes = layout.row_bytes(); + let bytes = match &self.stored { + Some(s) => Bytes::Source(s), + None => Bytes::Memory(&self.data), + }; + let kind = match self.compression { + None if total == 0 => Kind::Empty, + None => return Err(PsdError::invalid("channel has no data")), + Some(Compression::Raw) => { + if bytes.len() < total as u64 { + return Err(PsdError::UnexpectedEof { offset: usize::try_from(bytes.len()).unwrap_or(usize::MAX), needed: total - bytes.len() as usize }); + } + Kind::Raw(bytes) + } + Some(Compression::Rle) => { + let size: u64 = if version.is_psb() { 4 } else { 2 }; + let table_len = (height as u64).saturating_mul(size); + if table_len > bytes.len() { + return Err(PsdError::UnexpectedEof { offset: 0, needed: usize::try_from(table_len - bytes.len()).unwrap_or(usize::MAX) }); + } + let table = bytes.get(0, table_len)?; + let mut offsets = Vec::new(); + offsets.try_reserve_exact(height + 1).map_err(|_| PsdError::LimitExceeded("not enough memory for the RLE row table"))?; + let mut at = table_len; + offsets.push(at); + for c in table.chunks_exact(size as usize) { + let n = match *c { + [a, b] => u64::from(u16::from_be_bytes([a, b])), + [a, b, c, d] => u64::from(u32::from_be_bytes([a, b, c, d])), + _ => 0, + }; + at = at.saturating_add(n); + offsets.push(at); + } + let data_len = bytes.len() - table_len; + // As `decode_planes` checks: PackBits expands at most 64x. + if (total as u64) > data_len.saturating_mul(64) { + return Err(PsdError::Decompress("RLE data too short for declared size".into())); + } + if at > bytes.len() { + return Err(PsdError::UnexpectedEof { + offset: usize::try_from(bytes.len()).unwrap_or(usize::MAX), + needed: usize::try_from(at - bytes.len()).unwrap_or(usize::MAX), + }); + } + Kind::Rle { bytes, offsets } + } + Some(c) => Kind::Decoded(decode_planes(c, &self.encoded()?, &layout)?), + }; + Ok(RowDecoder { kind, row_bytes, height }) + } +} + +impl RowDecoder<'_> { + /// Bytes per decoded row. + pub fn row_bytes(&self) -> usize { + self.row_bytes + } + + /// Number of rows. + pub fn height(&self) -> usize { + self.height + } + + /// Rows `r0..r1` as planar big-endian samples (`(r1 - r0) × row_bytes` bytes). + pub fn rows(&self, r0: usize, r1: usize) -> Result> { + if r0 > r1 || r1 > self.height { + return Err(PsdError::invalid(format!("rows {r0}..{r1} outside 0..{}", self.height))); + } + let n = (r1 - r0).checked_mul(self.row_bytes).ok_or(PsdError::LimitExceeded("band size overflow"))?; + if n as u64 > MAX_DECODED_BYTES { + return Err(PsdError::LimitExceeded("decoded channel data exceeds MAX_DECODED_BYTES")); + } + let mut out = Vec::new(); + out.try_reserve_exact(n).map_err(|_| PsdError::LimitExceeded("not enough memory for the decoded channel data"))?; + match &self.kind { + Kind::Empty => {} + Kind::Raw(bytes) => out.extend_from_slice(&bytes.get((r0 * self.row_bytes) as u64, n as u64)?), + Kind::Decoded(plane) => { + out.extend_from_slice(plane.get(r0 * self.row_bytes..r1 * self.row_bytes).ok_or_else(|| PsdError::invalid("decoded plane too short"))?) + } + Kind::Rle { bytes, offsets } => { + let (Some(&start), Some(&end)) = (offsets.get(r0), offsets.get(r1)) else { + return Err(PsdError::invalid("RLE row table too short")); + }; + let band = bytes.get(start, end - start)?; + for r in r0..r1 { + let (Some(&a), Some(&b)) = (offsets.get(r), offsets.get(r + 1)) else { + return Err(PsdError::invalid("RLE row table too short")); + }; + let row = band.get((a - start) as usize..(b - start) as usize).ok_or_else(|| PsdError::invalid("RLE row outside its band"))?; + packbits::decode_into(row, self.row_bytes, &mut out)?; + } + } + } + Ok(out) + } +} + +#[cfg(test)] +mod tests { + use crate::source::Source; + use crate::{PsdFile, testgen}; + + /// Every channel of every generated file, decoded in uneven bands from memory and from a + /// source, is its whole-plane decode. + #[test] + fn bands_join_up_to_the_whole_plane() { + let mut checked = 0; + for case in testgen::all_cases() { + let bytes = case.file.to_bytes().unwrap(); + let eager = PsdFile::from_bytes(&bytes).unwrap(); + let lazy = PsdFile::from_source(Source::new(bytes)).unwrap(); + let (d, v) = (eager.header.depth, eager.header.version); + for file in [&eager, &lazy] { + for rec in file.layers() { + for ch in &rec.channels { + let (w, h) = rec.channel_rect(ch.id).size().unwrap(); + let whole = ch.decode(w, h, d, v); + let dec = ch.row_decoder(w, h, d, v); + let (Ok(whole), Ok(dec)) = (whole, dec) else { continue }; + let mut joined = Vec::new(); + let mut r = 0; + for step in [1, 3, 2, 7].iter().cycle() { + if r >= h { + break; + } + let r1 = (r + step).min(h); + joined.extend(dec.rows(r, r1).unwrap()); + r = r1; + } + assert_eq!(joined, whole, "{}", case.name); + assert!(dec.rows(0, h + 1).is_err()); + checked += 1; + } + } + } + } + assert!(checked > 100, "{checked}"); + } + + #[test] + fn a_damaged_rle_row_fails_its_band() { + use crate::Compression; + use crate::layer::ChannelData; + // Two rows of 4 bytes: a valid row, then one that ends early. + let mut data = vec![0, 2, 0, 2]; + data.extend([0xFD, 7]); // header -3: 1 - (-3) = 4 copies of 7 + data.extend([0x01, 9]); // a literal of 2 bytes, only 1 present + let ch = ChannelData::new(0, Some(Compression::Rle), data); + let dec = ch.row_decoder(4, 2, 8, crate::Version::Psd).unwrap(); + assert_eq!(dec.rows(0, 1).unwrap(), vec![7; 4]); + assert!(dec.rows(1, 2).is_err()); + assert!(ch.decode(4, 2, 8, crate::Version::Psd).is_err()); + } +} diff --git a/crates/psd/src/slices.rs b/crates/psd/src/slices.rs index 54bf4c8fcd..5d9cd50b39 100644 --- a/crates/psd/src/slices.rs +++ b/crates/psd/src/slices.rs @@ -10,6 +10,8 @@ //! version reads. Unknown fields are not kept here; callers that need a byte-exact round trip //! keep the raw resource while the slices are unchanged. +use std::collections::HashMap; + use crate::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; use crate::error::{PsdError, Result}; use crate::io::{Reader, WriteExt}; @@ -274,13 +276,22 @@ impl SlicesResource { && let Ok(vd) = VersionedDescriptor::read(r) && let Some(Value::List(items)) = vd.descriptor.get("slices") { + // Index each slice ID to its first occurrence once, so matching trailing + // descriptors against slices is linear rather than quadratic. Both the slice + // count and the descriptor-ID list are file-controlled, so a plain + // `find()` per descriptor is O(n*m) and lets a crafted resource burn CPU. + // `or_insert` preserves the original first-match behavior for duplicate IDs. + let mut id_to_index: HashMap = HashMap::with_capacity(slices.len()); + for (i, s) in slices.iter().enumerate() { + id_to_index.entry(s.id).or_insert(i); + } for v in items { let Value::Descriptor(d) = v else { continue }; // An entry without a usable id belongs to no slice (not to slice 0). let Some(id) = d_u32(d, "sliceID") else { continue }; let extra = slice_from_descriptor(d); - if let Some(s) = slices.iter_mut().find(|s| s.id == id) { - s.outsets = extra.outsets; + if let Some(&i) = id_to_index.get(&id) { + slices[i].outsets = extra.outsets; } } } @@ -493,4 +504,28 @@ mod tests { assert!(SlicesResource::from_bytes(&huge).is_err()); assert!(SlicesResource::from_bytes(&9u32.to_be_bytes()).is_err()); } + + #[test] + fn trailing_descriptor_applies_outsets_to_first_matching_slice() { + // Two slices share id 1; the trailing descriptor must update the first one only, + // matching the pre-index behavior of `iter_mut().find()`. + let mut plain = SlicesResource { + version: 6, + bounds: [0, 0, 80, 100], + group_name: "site".into(), + slices: vec![ + SliceRecord { id: 1, origin: 0, kind: 1, rect: [0, 0, 10, 10], ..Default::default() }, + SliceRecord { id: 1, origin: 0, kind: 1, rect: [10, 10, 20, 20], ..Default::default() }, + ], + }; + for s in &mut plain.slices { + s.outsets = [0; 4]; + } + let mut bytes = plain.to_bytes(); + let extra = Descriptor::new("slice").with("sliceID", Value::Integer(1)).with("topOutset", Value::Integer(7)); + VersionedDescriptor::new(Descriptor::new("null").with("slices", Value::List(vec![Value::Descriptor(extra)]))).write(&mut bytes); + let back = SlicesResource::from_bytes(&bytes).unwrap(); + assert_eq!(back.slices[0].outsets, [7, 0, 0, 0], "first slice receives the outset"); + assert_eq!(back.slices[1].outsets, [0, 0, 0, 0], "later duplicate is untouched"); + } } diff --git a/crates/psd/src/source.rs b/crates/psd/src/source.rs new file mode 100644 index 0000000000..2e7c8039dd --- /dev/null +++ b/crates/psd/src/source.rs @@ -0,0 +1,270 @@ +//! Random-access byte sources, for parsing a file without reading all of it. +//! +//! [`PsdFile::from_source`](crate::PsdFile::from_source) reads the structure of a file (header, +//! resources, layer records, tagged blocks) and leaves the bulk of it, the encoded channel data +//! and the merged image, where it is: each [`ChannelData`](crate::ChannelData) records a +//! [`Stored`] range instead of a copy, and the bytes are read when the channel is decoded. A +//! 2 GB layered file then never needs 2 GB of memory just to be opened. + +use std::borrow::Cow; +use std::fmt; +use std::sync::Arc; + +use crate::error::{PsdError, Result}; + +/// Bytes that can be read at any offset, from any thread. +/// +/// Implemented for byte buffers and, on native targets, for [`std::fs::File`] (as +/// [`FileSource`]). The source must not change while a file parsed from it is in use: a +/// [`Stored`] range is an offset into it. +pub trait ReadAt: Send + Sync { + /// Total length in bytes. + fn len(&self) -> u64; + + /// `true` when the source holds no bytes. + fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Fills `buf` with the bytes at `offset..offset + buf.len()`. Fails, without a partial + /// result, when the range is not all inside the source or the read fails. + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()>; +} + +fn out_of_range(offset: u64, needed: usize, len: u64) -> PsdError { + PsdError::UnexpectedEof { + offset: usize::try_from(offset).unwrap_or(usize::MAX), + needed: needed.saturating_sub(usize::try_from(len.saturating_sub(offset)).unwrap_or(usize::MAX)), + } +} + +impl ReadAt for [u8] { + fn len(&self) -> u64 { + <[u8]>::len(self) as u64 + } + + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + let src = usize::try_from(offset) + .ok() + .and_then(|start| self.get(start..start.checked_add(buf.len())?)) + .ok_or_else(|| out_of_range(offset, buf.len(), ReadAt::len(self)))?; + buf.copy_from_slice(src); + Ok(()) + } +} + +impl ReadAt for Vec { + fn len(&self) -> u64 { + self.as_slice().len() as u64 + } + + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + self.as_slice().read_at(offset, buf) + } +} + +impl ReadAt for Arc { + fn len(&self) -> u64 { + (**self).len() + } + + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + (**self).read_at(offset, buf) + } +} + +/// Largest single read from a [`FileSource`]. +#[cfg(any(unix, windows))] +const MAX_READ: usize = 64 << 20; + +/// An open file read at offsets (`pread` on Unix, `seek_read` on Windows), with its length taken +/// when it was opened. Reads from several threads at once need no lock. +#[cfg(any(unix, windows))] +#[derive(Debug)] +pub struct FileSource { + file: std::fs::File, + len: u64, +} + +#[cfg(any(unix, windows))] +impl FileSource { + /// Opens `path` for reading. + pub fn open(path: &std::path::Path) -> std::io::Result { + Self::new(std::fs::File::open(path)?) + } + + /// Wraps an open file. + pub fn new(file: std::fs::File) -> std::io::Result { + let len = file.metadata()?.len(); + Ok(FileSource { file, len }) + } + + /// The file. + pub fn file(&self) -> &std::fs::File { + &self.file + } +} + +#[cfg(any(unix, windows))] +impl ReadAt for FileSource { + fn len(&self) -> u64 { + self.len + } + + fn read_at(&self, offset: u64, buf: &mut [u8]) -> Result<()> { + if offset.checked_add(buf.len() as u64).is_none_or(|end| end > self.len) { + return Err(out_of_range(offset, buf.len(), self.len)); + } + let n = buf.len(); + let failed = |e: std::io::Error| PsdError::Read(format!("reading {n} bytes at offset {offset}: {e}")); + // Bounded reads, as `photocraft-format`'s `read_file` makes them: some Windows file + // systems reject a single read of gigabytes (#375). + let mut done = 0usize; + while done < n { + let Some(rest) = buf.get_mut(done..n.min(done + MAX_READ)) else { break }; + let at = offset + done as u64; + #[cfg(unix)] + let read = { + use std::os::unix::fs::FileExt; + self.file.read_at(rest, at) + }; + #[cfg(windows)] + let read = { + use std::os::windows::fs::FileExt; + self.file.seek_read(rest, at) + }; + match read { + Ok(0) => return Err(failed(std::io::ErrorKind::UnexpectedEof.into())), + Ok(k) => done += k, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {} + Err(e) => return Err(failed(e)), + } + } + Ok(()) + } +} + +/// A shared [`ReadAt`]: what [`Stored`] ranges point into. Two handles are equal when they are +/// the same source. +#[derive(Clone)] +pub struct Source(Arc); + +impl Source { + /// Shares `source`. + pub fn new(source: impl ReadAt + 'static) -> Self { + Source(Arc::new(source)) + } + + /// Shares an already shared source. + pub fn from_arc(source: Arc) -> Self { + Source(source) + } + + /// The source. + pub fn get(&self) -> &dyn ReadAt { + &*self.0 + } + + /// Reads `len` bytes at `offset` into a new buffer. The buffer is reserved fallibly: a + /// range larger than memory is an error, not an abort. + pub fn read_vec(&self, offset: u64, len: u64) -> Result> { + let n = usize::try_from(len).map_err(|_| PsdError::LimitExceeded("length exceeds address space"))?; + if offset.checked_add(len).is_none_or(|end| end > self.0.len()) { + return Err(out_of_range(offset, n, self.0.len())); + } + let mut out = Vec::new(); + out.try_reserve_exact(n).map_err(|_| PsdError::LimitExceeded("not enough memory to read the data"))?; + out.resize(n, 0); + self.0.read_at(offset, &mut out)?; + Ok(out) + } +} + +impl fmt::Debug for Source { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Source({} bytes)", self.0.len()) + } +} + +impl PartialEq for Source { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for Source {} + +/// Encoded bytes left in their [`Source`] by a lazy parse: `len` bytes at `offset`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Stored { + /// Where the bytes are. + pub source: Source, + /// Offset of the first byte. + pub offset: u64, + /// Number of bytes. + pub len: u64, +} + +impl Stored { + /// Reads the bytes. + pub fn read(&self) -> Result> { + self.source.read_vec(self.offset, self.len) + } + + /// Reads `len` bytes starting `at` bytes into the range. + pub fn read_part(&self, at: u64, len: u64) -> Result> { + if at.checked_add(len).is_none_or(|end| end > self.len) { + return Err(PsdError::invalid("read past the end of stored data")); + } + self.source.read_vec(self.offset + at, len) + } +} + +/// The bytes of a value that is either in memory (`data`) or still in its source (`stored`). +pub(crate) fn encoded<'a>(data: &'a [u8], stored: Option<&Stored>) -> Result> { + match stored { + Some(s) => s.read().map(Cow::Owned), + None => Ok(Cow::Borrowed(data)), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn slice_reads_are_bounds_checked() { + let v = vec![1u8, 2, 3, 4]; + let mut b = [0u8; 2]; + v.read_at(2, &mut b).unwrap(); + assert_eq!(b, [3, 4]); + assert!(v.read_at(3, &mut b).is_err()); + assert!(v.read_at(u64::MAX, &mut b).is_err()); + let s = Source::new(v); + assert_eq!(s.read_vec(1, 2).unwrap(), vec![2, 3]); + assert!(s.read_vec(1, u64::MAX).is_err()); + let st = Stored { source: s.clone(), offset: 1, len: 3 }; + assert_eq!(st.read().unwrap(), vec![2, 3, 4]); + assert_eq!(st.read_part(1, 2).unwrap(), vec![3, 4]); + assert!(st.read_part(2, 2).is_err()); + assert_eq!(st.source, s); + assert_ne!(st.source, Source::new(vec![1u8, 2, 3, 4])); + } + + #[cfg(any(unix, windows))] + #[test] + fn file_source_reads_at_offsets() { + use std::io::Write; + let dir = std::env::temp_dir().join(format!("photocraft-psd-source-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("f.bin"); + std::fs::File::create(&path).unwrap().write_all(&[9, 8, 7, 6, 5]).unwrap(); + let f = FileSource::open(&path).unwrap(); + assert_eq!(ReadAt::len(&f), 5); + let mut b = [0u8; 3]; + f.read_at(2, &mut b).unwrap(); + assert_eq!(b, [7, 6, 5]); + assert!(f.read_at(3, &mut b).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/psd/src/tagged.rs b/crates/psd/src/tagged.rs index 7728d53fd1..6fce19bbe9 100644 --- a/crates/psd/src/tagged.rs +++ b/crates/psd/src/tagged.rs @@ -155,12 +155,20 @@ impl TaggedBlock { Self::is_foreign_order_signature(&self.signature) } + /// `true` for a "Saving Merged Transparency" marker: the merged image's first extra channel + /// is its transparency. The specification's keys are `Mtrn`, `Mt16` and `Mt32`; Photoshop + /// (27.11) writes `MTrn`, with a positive layer count, in a layered TIFF saved with Save + /// Transparency next to spot and alpha channels. + pub fn is_merged_transparency_marker(&self) -> bool { + matches!(&self.key, b"Mtrn" | b"MTrn" | b"Mt16" | b"Mt32") + } + /// Key as a string (lossy). pub fn key_str(&self) -> String { String::from_utf8_lossy(&self.key).into_owned() } - fn default_padding(len: usize, alignment: usize) -> usize { + pub(crate) fn default_padding(len: usize, alignment: usize) -> usize { (alignment - len % alignment) % alignment } @@ -352,9 +360,14 @@ impl TaggedBlock { /// Serialized size in bytes as a layer block (two-byte alignment). pub fn encoded_len(&self, version: Version) -> usize { + self.encoded_len_aligned(version, 2) + } + + /// Serialized size in bytes as [`Self::write`] writes it with `alignment`. + pub(crate) fn encoded_len_aligned(&self, version: Version, alignment: usize) -> usize { let l = if uses_long_length(version, &self.key) { 8 } else { 4 }; - let p = self.padding.as_ref().map_or(Self::default_padding(self.data.len(), 2), Vec::len); - 8 + l + self.data.len() + p + let p = self.padding.as_ref().map_or(Self::default_padding(self.data.len(), alignment), Vec::len); + (8 + l + p).saturating_add(self.data.len()) } } @@ -436,7 +449,7 @@ fn parse_section(d: &[u8]) -> Result { /// `8BIM` / `8B64`, or the markers `MIB8` / `46B8` that [`crate::tiff`] gives a block kept /// verbatim from a little-endian TIFF when its layout is unknown (its data is in that order). -fn is_sig(b: &[u8]) -> bool { +pub(crate) fn is_sig(b: &[u8]) -> bool { b.starts_with(b"8BIM") || b.starts_with(b"8B64") || TaggedBlock::is_foreign_order_signature(b) } diff --git a/crates/psd/src/testgen.rs b/crates/psd/src/testgen.rs index d2d4784cb9..4898984445 100644 --- a/crates/psd/src/testgen.rs +++ b/crates/psd/src/testgen.rs @@ -113,7 +113,7 @@ pub fn merged_only(version: Version, mode: ColorMode, depth: u16, compression: C for c in 0..channels { planes.extend(pattern_plane(w as usize, h as usize, depth, u32::from(c))); } - let image_data = ImageData::encode(compression, &planes, &header).unwrap_or(ImageData { compression, data: Vec::new() }); + let image_data = ImageData::encode(compression, &planes, &header).unwrap_or(ImageData::new(compression, Vec::new())); PsdFile { header, color_mode_data: color_mode_data(mode), @@ -124,6 +124,7 @@ pub fn merged_only(version: Version, mode: ColorMode, depth: u16, compression: C global_blocks: Vec::new(), layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, } } @@ -179,7 +180,7 @@ fn raster_layer(g: LayerGen<'_>) -> LayerRecord { fn group_record(header: &Header, name: &str, kind: SectionType, blend: BlendMode, id: u32) -> LayerRecord { let cc = mode_channels(header.color_mode) as i16; - let channels = (-1..cc).map(|c| ChannelData { id: c, compression: Some(Compression::Raw), data: Vec::new() }).collect(); + let channels = (-1..cc).map(|c| ChannelData::new(c, Some(Compression::Raw), Vec::new())).collect(); let lsct = if kind == SectionType::BoundingDivider { TaggedBlock::section_divider(kind, None, None) } else { @@ -316,6 +317,7 @@ pub fn layered(version: Version, mode: ColorMode, depth: u16, compression: Compr global_blocks: blocks, layer_mask_trailing: Vec::new(), image_data, + write_as_psb: false, } } @@ -351,6 +353,7 @@ pub fn small(version: Version, compression: Compression) -> PsdFile { global_blocks: vec![TaggedBlock::new(*b"Patt", vec![1, 2])], layer_mask_trailing: Vec::new(), image_data: ImageData::encode(compression, &planes, &header).expect("encode"), + write_as_psb: false, } } diff --git a/crates/psd/src/tiff.rs b/crates/psd/src/tiff.rs index 31c85d4885..b669fcc637 100644 --- a/crates/psd/src/tiff.rs +++ b/crates/psd/src/tiff.rs @@ -217,6 +217,7 @@ impl ImageSourceData { global_blocks: self.global_blocks, layer_mask_trailing: self.trailing, image_data, + write_as_psb: false, } } } @@ -268,2166 +269,7 @@ pub fn resources_to_bytes(resources: &[ImageResource]) -> Result> { /// so output lengths equal input lengths and every length field stays valid. Layouts follow the /// Adobe PSD specification; the walker only needs to know where the integers, doubles and /// four-character keys are, not what they mean. -pub mod transcode { - use super::*; - - /// Converts the tagged blocks after the 37724 header string from `from` to `to`. - pub fn transcode_blocks(data: &[u8], version: Version, from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { - let mut t = Tx::new(data, version, from, to); - t.blocks()?; - Ok(t.finish()) - } - - /// Converts a layer-info body (the data of a `Layr` / `Lr16` / `Lr32` block) between orders. - pub fn transcode_layer_info(data: &[u8], version: Version, from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { - let mut t = Tx::new(data, version, from, to); - t.layer_info()?; - Ok(t.finish()) - } - - /// Converts image-resource blocks (a 34377 tag) between orders. - pub fn transcode_resources(data: &[u8], from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { - let mut t = Tx::new(data, Version::Psd, from, to); - t.resources()?; - Ok(t.finish()) - } - - /// A block signature as stored in `little` or big-endian data. Big-endian data (the model) - /// also carries the `MIB8` / `46B8` markers of blocks kept from a little-endian file. - fn is_sig(b: &[u8], little: bool) -> bool { - if little { - b.starts_with(b"MIB8") || b.starts_with(b"46B8") - } else { - b.starts_with(b"8BIM") || b.starts_with(b"8B64") || TaggedBlock::is_foreign_order_signature(b) - } - } - - fn height(top: i32, bottom: i32) -> usize { - usize::try_from(bottom.saturating_sub(top)).unwrap_or(0) - } - - /// Per layer record: the channel (id, length) list and the heights of the layer, mask and - /// real-mask rects. - type RecordShape = (Vec<(i16, u64)>, [usize; 3]); - - struct Tx<'a> { - r: Reader<'a>, - out: Vec, - from: ByteOrder, - to: ByteOrder, - version: Version, - warnings: Vec, - depth: usize, - } - - macro_rules! copy_int { - ($name:ident, $ty:ty) => { - fn $name(&mut self) -> Result<$ty> { - let v = self.r.$name()?; - self.out.extend_from_slice(&if self.to.is_little() { v.to_le_bytes() } else { v.to_be_bytes() }); - Ok(v) - } - }; - } - - impl<'a> Tx<'a> { - fn new(data: &'a [u8], version: Version, from: ByteOrder, to: ByteOrder) -> Self { - let r = if from.is_little() { Reader::new_little(data) } else { Reader::new(data) }; - Tx { r, out: Vec::with_capacity(data.len()), from, to, version, warnings: Vec::new(), depth: 0 } - } - - fn finish(self) -> (Vec, Vec) { - (self.out, self.warnings) - } - - /// A walker over `data` (a nested structure) that writes into this walker's output: it - /// takes the buffer, so a nested structure is written once, not copied up per level. Hand - /// the buffer back with [`Self::adopt`] before returning, error or not. - fn nested<'b>(&mut self, data: &'b [u8], depth: usize) -> Tx<'b> { - let r = if self.from.is_little() { Reader::new_little(data) } else { Reader::new(data) }; - Tx { r, out: std::mem::take(&mut self.out), from: self.from, to: self.to, version: self.version, warnings: Vec::new(), depth } - } - - /// Takes the output buffer back from a [`Self::nested`] walker. - fn adopt(&mut self, child: &mut Tx<'_>) { - self.out = std::mem::take(&mut child.out); - } - - fn same_order(&self) -> bool { - self.from == self.to - } - - fn remaining(&self) -> usize { - self.r.remaining() - } - - copy_int!(u16, u16); - copy_int!(i16, i16); - copy_int!(u32, u32); - copy_int!(i32, i32); - copy_int!(u64, u64); - copy_int!(i64, i64); - copy_int!(f64, f64); - - fn u8(&mut self) -> Result { - let v = self.r.u8()?; - self.out.push(v); - Ok(v) - } - - fn f32(&mut self) -> Result<()> { - let b = self.r.array::<4>()?; - let v = if self.from.is_little() { f32::from_le_bytes(b) } else { f32::from_be_bytes(b) }; - self.out.extend_from_slice(&if self.to.is_little() { v.to_le_bytes() } else { v.to_be_bytes() }); - Ok(()) - } - - /// Bytes copied as they are. - fn raw(&mut self, n: usize) -> Result<()> { - let b = self.r.bytes(n)?; - self.out.extend_from_slice(b); - Ok(()) - } - - fn rest(&mut self) -> Result<()> { - let n = self.remaining(); - self.raw(n) - } - - /// A four-character signature or key: stored reversed in little-endian data. Returns the - /// canonical (big-endian) spelling. - fn key(&mut self) -> Result<[u8; 4]> { - let mut k = self.r.array::<4>()?; - if self.from.is_little() { - k.reverse(); - } - let mut w = k; - if self.to.is_little() { - w.reverse(); - } - self.out.extend_from_slice(&w); - Ok(k) - } - - fn len(&mut self, long: bool) -> Result { - if long { self.u64() } else { Ok(u64::from(self.u32()?)) } - } - - fn u16s(&mut self, n: usize) -> Result<()> { - for _ in 0..n { - self.u16()?; - } - Ok(()) - } - - /// Up to `n` `u16`s: a block shorter than its full layout keeps what it has. - fn u16s_upto(&mut self, n: usize) -> Result<()> { - for _ in 0..n { - if self.remaining() < 2 { - break; - } - self.u16()?; - } - Ok(()) - } - - fn i32s(&mut self, n: usize) -> Result<()> { - for _ in 0..n { - self.i32()?; - } - Ok(()) - } - - fn f64s(&mut self, n: usize) -> Result<()> { - for _ in 0..n { - self.f64()?; - } - Ok(()) - } - - /// `u16`s to the end (a lone odd byte is copied). - fn u16s_to_end(&mut self) -> Result<()> { - while self.remaining() >= 2 { - self.u16()?; - } - self.rest() - } - - fn u32s_to_end(&mut self) -> Result<()> { - while self.remaining() >= 4 { - self.u32()?; - } - self.rest() - } - - /// A Photoshop Unicode string: u32 count + UTF-16 units. - fn unicode(&mut self) -> Result<()> { - let n = self.u32()?; - self.r.check_count(u64::from(n), 2)?; - self.u16s(n as usize) - } - - /// A Pascal string padded to `align`. - fn pascal(&mut self, align: usize) -> Result<()> { - let n = usize::from(self.u8()?); - self.raw(n)?; - let total = 1 + n; - self.raw((align - total % align) % align) - } - - /// Transcodes the next `len` bytes with `f`; whatever `f` leaves is copied verbatim. - fn region(&mut self, len: u64, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { - let data = self.r.bytes_u64(len)?; - let start = self.out.len(); - let mut child = self.nested(data, self.depth); - let res = f(&mut child).and_then(|()| child.rest()); - self.adopt(&mut child); - res?; - if self.out.len() - start != data.len() { - return Err(PsdError::invalid("byte-order conversion changed a structure's length")); - } - self.warnings.append(&mut child.warnings); - Ok(()) - } - - /// Reads a length field (4 bytes, or 8 when `long`), transcodes that many bytes with `f` - /// and writes the length of the result: blocks dropped inside shrink the field. - fn sized_region(&mut self, long: bool, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { - self.sized_region_counting(long, false, f) - } - - /// [`Self::sized_region`] for a u32 length that counts its own 4 bytes. - fn sized_region_inclusive(&mut self, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { - self.sized_region_counting(false, true, f) - } - - fn sized_region_counting(&mut self, long: bool, inclusive: bool, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { - let mut len = self.r.len_field(long)?; - let at = self.out.len(); - self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); - if inclusive { - len = len.checked_sub(4).ok_or_else(|| PsdError::invalid("length shorter than its own field"))?; - } - let data = self.r.bytes_u64(len)?; - let start = self.out.len(); - let mut child = self.nested(data, self.depth); - let res = f(&mut child).and_then(|()| child.rest()); - self.adopt(&mut child); - res?; - self.warnings.append(&mut child.warnings); - self.patch_len(at, long, (self.out.len() - start) as u64 + if inclusive { 4 } else { 0 })?; - Ok(()) - } - - /// Tries `f` on the rest of the data; it counts as a match only when at most three - /// trailing zero bytes are left. On a miss nothing is consumed or written. - fn attempt(&mut self, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> bool { - let (reader, written) = (self.r.clone(), self.out.len()); - let ok = f(self).is_ok() && self.remaining() <= 3 && self.r.peek_rest().iter().all(|&b| b == 0); - if !ok { - self.r = reader; - self.out.truncate(written); - } - ok - } - - /// Blocks whose layout is not known by key are very often a versioned descriptor, - /// optionally behind a u32 version (every Photoshop block introduced since CS6 is): - /// recognized by parsing to the end. - fn descriptor_shaped(&mut self) -> bool { - self.attempt(|t| t.versioned_descriptor()) - || self.attempt(|t| { - t.u32()?; - t.versioned_descriptor() - }) - } - - /// Writes `n` into the length placeholder at `at`. - fn patch_len(&mut self, at: usize, long: bool, n: u64) -> Result<()> { - if long { - let b = if self.to.is_little() { n.to_le_bytes() } else { n.to_be_bytes() }; - if let Some(slot) = self.out.get_mut(at..at + 8) { - slot.copy_from_slice(&b); - } - } else { - let n = u32::try_from(n).map_err(|_| PsdError::LimitExceeded("structure too large for a 32-bit length"))?; - let b = if self.to.is_little() { n.to_le_bytes() } else { n.to_be_bytes() }; - if let Some(slot) = self.out.get_mut(at..at + 4) { - slot.copy_from_slice(&b); - } - } - Ok(()) - } - - /// An RLE row-count table (`rows` entries of 2 bytes, 4 in PSB data). - fn rle_counts(&mut self, rows: usize) -> Result<()> { - for _ in 0..rows { - if self.version.is_psb() { - if self.remaining() < 4 { - break; - } - self.u32()?; - } else { - if self.remaining() < 2 { - break; - } - self.u16()?; - } - } - Ok(()) - } - - /// Encoded channel data for a plane of `rows` rows: the compression field, then the RLE - /// row counts (container order) and the samples (always big-endian: copied). - fn channel_data(&mut self, rows: usize, long_counts: bool) -> Result<()> { - let saved = self.version; - if long_counts { - self.version = Version::Psb; - } - let compression = self.u16()?; - let res = if compression == 1 { self.rle_counts(rows) } else { Ok(()) }; - self.version = saved; - res?; - self.rest() - } - - // ---- image resources (34377) ---- - - fn resources(&mut self) -> Result<()> { - while self.remaining() >= 12 && is_sig(self.r.peek_rest(), self.from.is_little()) { - self.key()?; - let id = self.u16()?; - self.pascal(2)?; - let odd = self.r.clone().u32()? % 2 == 1; - self.sized_region(false, |t| t.resource_data(id))?; - if odd && self.remaining() > 0 { - self.raw(1)?; - } - } - self.rest() - } - - /// Typed numeric resources; every other payload is a byte blob (ICC, XMP, EXIF, …) or - /// unknown and is copied as stored. - fn resource_data(&mut self, id: u16) -> Result<()> { - match id { - // Resolution info: fixed 16.16 h res, u16 unit, u16 width unit, then the same for v. - 1005 => { - self.u32()?; - self.u16()?; - self.u16()?; - self.u32()?; - self.u16()?; - self.u16()?; - } - // Layer state (u16 target), layer group info (u16 per layer). - 1024 | 1026 => self.u16s_to_end()?, - // Global angle / altitude. - 1037 | 1049 => { - self.u32()?; - } - // Layer selection ids: u16 count + u32 ids. - 1069 => { - self.u16()?; - self.u32s_to_end()?; - } - // Version info: u32 version, u8 has real merged data, two Unicode strings, u32. - 1057 => { - self.u32()?; - self.u8()?; - self.unicode()?; - self.unicode()?; - self.u32()?; - } - // Grid and guides: u32 version, 2 × u32 grid, u32 count, (u32 location, u8 direction)… - 1032 => { - self.u32()?; - self.u32()?; - self.u32()?; - let n = self.u32()?; - for _ in 0..n { - if self.remaining() < 5 { - break; - } - self.u32()?; - self.u8()?; - } - } - // Path resources: 26-byte records. - 2000..=2997 | 2999 => self.path_records()?, - _ => {} - } - Ok(()) - } - - // ---- tagged blocks ---- - - /// Tagged blocks to the end; bytes that do not form a block are copied. - fn blocks(&mut self) -> Result<()> { - loop { - let rest = self.r.peek_rest(); - if rest.len() < 12 || !is_sig(rest, self.from.is_little()) { - return self.rest(); - } - self.block()?; - } - } - - fn block(&mut self) -> Result<()> { - let before = self.out.len(); - // A `MIB8` / `46B8` marker in big-endian data: a block kept verbatim from a - // little-endian file. Written back as it was into little-endian output (its `8BIM` - // signature reversed, as every signature there), copied as is otherwise. - if !self.from.is_little() && TaggedBlock::is_foreign_order_signature(self.r.peek_rest()) { - let sig = self.r.array::<4>()?; - let mut key = self.r.array::<4>()?; - // Only reached with `from == Big`: the key is canonical here whatever the - // target order. `TaggedBlock::write` sizes the length field from that same key. - // Reversing it for the target order before the check - // asked about a key `PSB_LONG_KEYS` never contains, so a PSB long key's 8-byte - // length was read as 4 and every later block was copied unconverted. - let long = uses_long_length(self.version, &key); - let len = self.r.len_field(long)?; - let data = self.r.bytes_u64(len)?; - if self.to.is_little() { - key.reverse(); - } - self.out.extend_from_slice(&sig); - self.out.extend_from_slice(&key); - let n = self.out.len(); - self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); - self.patch_len(n, long, len)?; - self.out.extend_from_slice(data); - let rest = self.r.peek_rest(); - let pad = (0..=3usize).find(|&k| k == rest.len() || (k < rest.len() && is_sig(&rest[k..], self.from.is_little()))).unwrap_or(0); - let pad_bytes = self.r.bytes(pad)?; - self.out.extend_from_slice(pad_bytes); - return Ok(()); - } - let sig_at = self.out.len(); - let sig = self.key()?; - let key = self.key()?; - let long = uses_long_length(self.version, &key); - let len = self.r.len_field(long)?; - let len_at = self.out.len(); - self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); - let data = self.r.bytes_u64(len)?; - // The body is transcoded straight into this output, after the length placeholder. - let data_at = self.out.len(); - let mut child = self.nested(data, self.depth + 1); - let handled = if child.depth > MAX_DEPTH { - Err(PsdError::LimitExceeded("tagged block nesting too deep")) - } else if data.is_empty() { - Ok(true) - } else { - child.block_body(&key) - }; - let rest = if matches!(handled, Ok(true)) { child.rest() } else { Ok(()) }; - self.adopt(&mut child); - rest?; - if !matches!(handled, Ok(true)) { - // A body that was not transcoded leaves nothing: it is copied as stored or dropped. - self.out.truncate(data_at); - } - // Padding: the smallest k in 0..=3 after which the next block (or the end) starts. - let rest = self.r.peek_rest(); - let pad = (0..=3usize).find(|&k| k == rest.len() || (k < rest.len() && is_sig(&rest[k..], self.from.is_little()))).unwrap_or(0); - let pad_bytes = self.r.bytes(pad)?; - match handled { - Ok(true) => { - self.warnings.append(&mut child.warnings); - self.patch_len(len_at, long, (self.out.len() - data_at) as u64)?; - self.out.extend_from_slice(pad_bytes); - } - Ok(false) if self.same_order() => { - self.patch_len(len_at, long, data.len() as u64)?; - self.out.extend_from_slice(data); - self.out.extend_from_slice(pad_bytes); - } - // Little-endian in, big-endian model: keep the bytes as they are, marked. - Ok(false) if self.from.is_little() => { - let marker: &[u8; 4] = if &sig == b"8B64" { b"46B8" } else { b"MIB8" }; - self.out[sig_at..sig_at + 4].copy_from_slice(marker); - self.patch_len(len_at, long, data.len() as u64)?; - self.out.extend_from_slice(data); - self.out.extend_from_slice(pad_bytes); - } - Ok(false) => { - self.out.truncate(before); - self.warnings.push(format!( - "{} block ({} bytes) dropped: its layout is unknown and cannot be converted to the file's byte order", - String::from_utf8_lossy(&key), - data.len() - )); - } - Err(e) if self.same_order() => return Err(PsdError::invalid(format!("{} block: {e}", String::from_utf8_lossy(&key)))), - Err(e) if self.from.is_little() => { - let marker: &[u8; 4] = if &sig == b"8B64" { b"46B8" } else { b"MIB8" }; - self.out[sig_at..sig_at + 4].copy_from_slice(marker); - self.patch_len(len_at, long, data.len() as u64)?; - self.out.extend_from_slice(data); - self.out.extend_from_slice(pad_bytes); - self.warnings.push(format!("{} block ({} bytes) kept as stored, in little-endian layout: {e}", String::from_utf8_lossy(&key), data.len())); - } - Err(e) => { - self.out.truncate(before); - self.warnings.push(format!("{} block ({} bytes) dropped: {e}", String::from_utf8_lossy(&key), data.len())); - } - } - Ok(()) - } - - /// Transcodes a block's data by key. `Ok(false)` for keys without a known layout. - fn block_body(&mut self, key: &[u8; 4]) -> Result { - match key { - b"Layr" | b"Lr16" | b"Lr32" => self.layer_info()?, - b"luni" => self.unicode()?, - b"lsct" | b"lsdk" => { - self.u32()?; - if self.remaining() >= 8 { - self.key()?; - self.key()?; - } - if self.remaining() >= 4 { - self.u32()?; - } - } - // lyvr layer version, sn2P and vowv (shape and vector flags). - b"lyid" | b"lspf" | b"lyvr" | b"sn2P" | b"vowv" => { - self.u32()?; - } - // Text-engine global data: a textual structure, no byte order. - b"Txt2" => self.rest()?, - b"lnsr" => { - self.key()?; - } - // Advanced Blending flags and fill opacity: one byte, then padding (Adobe spec, - // "Additional Layer Information"; `lmgm`/`vmgm` = layer/vector mask hides effects). - b"clbl" | b"infx" | b"knko" | b"tsly" | b"lmgm" | b"vmgm" | b"iOpa" => { - self.u8()?; - } - b"lclr" => self.u16s_upto(4)?, - b"fxrp" => { - while self.remaining() >= 8 { - self.f64()?; - } - } - b"brst" => self.u32s_to_end()?, - b"LMsk" => self.u16s_upto(6)?, - b"FMsk" => self.u16s_to_end()?, - b"shmd" => self.metadata_setting()?, - // Effects (lfxs: the effects as applied to a group), content credentials (CAI). - b"lfx2" | b"lmfx" | b"lfxs" | b"CAI " => { - self.u32()?; - self.versioned_descriptor()?; - } - b"SoCo" | b"GdFl" | b"PtFl" | b"vstk" | b"blwh" | b"vibA" | b"CgEd" | b"artb" | b"artd" | b"cinf" | b"cust" | b"PxSD" | b"GenI" | b"OCIO" - | b"extn" | b"pths" => { - self.versioned_descriptor()?; - } - // Color Lookup: u16 version, then the descriptor. - b"clrL" => { - self.u16()?; - self.versioned_descriptor()?; - } - b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE" => self.linked_layers()?, - b"Anno" => self.annotations()?, - b"vogk" => { - self.u32()?; - self.versioned_descriptor()?; - } - b"vscg" => { - self.key()?; - self.versioned_descriptor()?; - } - b"SoLd" | b"SoLE" => { - self.key()?; - self.u32()?; - self.versioned_descriptor()?; - } - b"PlLd" => { - self.key()?; - self.u32()?; - self.pascal(1)?; - for _ in 0..4 { - self.u32()?; - } - self.f64s(8)?; - self.u32()?; - self.versioned_descriptor()?; - } - // Type: version, transform, text version + descriptor, warp version + descriptor, - // and the bounds as four 32-bit values (the specification says doubles; Photoshop - // writes 16 bytes). - b"TySh" => { - self.u16()?; - self.f64s(6)?; - self.u16()?; - self.versioned_descriptor()?; - self.u16()?; - self.versioned_descriptor()?; - self.i32s(4)?; - } - b"vmsk" | b"vsms" => { - self.u32()?; - self.u32()?; - self.path_records()?; - } - b"Patt" | b"Pat2" | b"Pat3" => { - while self.remaining() >= 4 { - let len = self.r.clone().u32()?; - if len == 0 { - self.u32()?; - continue; - } - self.sized_region(false, Tx::pattern)?; - // Each pattern is padded to a multiple of 4. - let pad = ((4 - len % 4) % 4) as usize; - self.raw(pad.min(self.remaining()))?; - } - } - b"FEid" | b"FXid" => self.filter_effects()?, - b"lrFX" => self.legacy_effects()?, - b"levl" => { - self.u16()?; - for _ in 0..29 * 5 { - if self.remaining() < 2 { - break; - } - self.u16()?; - } - if self.remaining() >= 4 { - self.key()?; - } - self.u16s_to_end()?; - } - b"curv" => self.curves()?, - b"hue2" => { - self.u16()?; - self.u8()?; - self.u8()?; - self.u16s_to_end()?; - } - b"brit" => self.u16s_upto(3)?, - b"thrs" | b"post" => { - self.u16()?; - } - b"nvrt" => {} - b"expA" => { - self.u16()?; - for _ in 0..3 { - self.f32()?; - } - } - b"blnc" => self.u16s_upto(9)?, - b"mixr" | b"selc" => self.u16s_to_end()?, - b"phfl" => match self.u16()? { - 2 => { - self.u16s(5)?; - self.u32()?; - self.u8()?; - } - 3 => { - for _ in 0..4 { - self.u32()?; - } - self.u8()?; - } - v => return Err(PsdError::Unsupported(format!("photo filter version {v}"))), - }, - b"grdm" => self.gradient_map()?, - _ if !self.same_order() => return Ok(self.descriptor_shaped()), - _ => return Ok(false), - } - Ok(true) - } - - /// `lnk2` / `lnk3` / `lnkD` / `lnkE` (linked and embedded smart-object files): items of - /// u64 length + data, each padded to 4. Item: type (`liFD` embedded, `liFE` external, - /// `liFA` alias), version 1–7, Pascal uuid, Unicode name, file type and creator keys, u64 - /// data length, open-descriptor flag (+ descriptor), per-type fields, the file bytes for - /// `liFD`, then the fields later versions added. - fn linked_layers(&mut self) -> Result<()> { - while self.remaining() >= 8 { - let len = self.r.clone().u64()?; - self.sized_region(true, |t| { - let kind = t.key()?; - let version = t.u32()?; - t.pascal(1)?; - t.unicode()?; - t.key()?; - t.key()?; - let data_len = t.u64()?; - if t.u8()? != 0 { - t.versioned_descriptor()?; - } - if &kind == b"liFE" && version > 3 { - t.i32()?; - for _ in 0..4 { - t.u8()?; - } - t.f64()?; - } - if &kind == b"liFE" { - t.u64()?; - } - if &kind == b"liFA" { - t.raw(8)?; - } - let data_len = usize::try_from(data_len).map_err(|_| PsdError::LimitExceeded("linked file length"))?; - if &kind == b"liFD" { - t.raw(data_len)?; - } - if version >= 5 { - t.unicode()?; - } - if version >= 6 { - t.f64()?; - } - if version >= 7 { - t.u8()?; - } - if &kind == b"liFE" && version == 2 { - t.raw(data_len)?; - } - Ok(()) - })?; - let pad = ((4 - len % 4) % 4) as usize; - self.raw(pad.min(self.remaining()))?; - } - Ok(()) - } - - /// `Anno`: u16 major, u16 minor, u32 count, then per annotation a self-counting u32 - /// length, type key, open and flags bytes, u16 optional blocks, icon and popup rects, a - /// colour, three even-padded Pascal strings, and a self-counting data block (key, u32 - /// size, text with its own byte-order mark). - fn annotations(&mut self) -> Result<()> { - self.u16()?; - self.u16()?; - let n = self.u32()?; - for _ in 0..n { - if self.remaining() < 4 { - break; - } - self.sized_region_inclusive(|t| { - t.key()?; - t.u8()?; - t.u8()?; - t.u16()?; - t.i32s(8)?; - t.u16s(5)?; - t.pascal(2)?; - t.pascal(2)?; - t.pascal(2)?; - t.sized_region_inclusive(|d| { - d.key()?; - d.u32()?; - Ok(()) - }) - })?; - } - Ok(()) - } - - // ---- layer info ---- - - fn layer_info(&mut self) -> Result<()> { - let count = self.i16()?; - let n = usize::from(count.unsigned_abs()); - self.r.check_count(n as u64, 34)?; - // Per layer: the channel ids and lengths, and the heights of the layer, mask and - // real-mask rects (what each channel's RLE count table spans). - let mut records: Vec = Vec::with_capacity(n); - for _ in 0..n { - let top = self.i32()?; - self.i32()?; - let bottom = self.i32()?; - self.i32()?; - let nch = self.u16()?; - if nch > crate::layer::MAX_LAYER_CHANNELS { - return Err(PsdError::LimitExceeded("too many channels in layer")); - } - let mut chans = Vec::with_capacity(usize::from(nch)); - let psb = self.version.is_psb(); - for _ in 0..nch { - let id = self.i16()?; - let len = self.len(psb)?; - chans.push((id, len)); - } - let sig = self.key()?; - if &sig != b"8BIM" { - return Err(PsdError::InvalidSignature { expected: "8BIM (layer blend mode)", found: sig }); - } - self.key()?; - for _ in 0..4 { - self.u8()?; - } - let mut heights = [height(top, bottom), 0, 0]; - let mut mask_heights = (0usize, 0usize); - self.sized_region(false, |t| { - let mut mh = (0, 0); - t.sized_region(false, |m| { - mh = m.mask()?; - Ok(()) - })?; - mask_heights = mh; - // Blending ranges are bytes (black low/high, white low/high per channel), so - // Photoshop stores them in the same order in either byte order: an Intel-order - // layered TIFF holds the default ranges as `00 00 ff ff`. Swapping them as u32s - // turned every range into `ff ff 00 00`, which hides every pixel of the layer. - t.sized_region(false, |x| x.rest())?; - t.pascal(4)?; - t.blocks() - })?; - heights[1] = mask_heights.0; - heights[2] = mask_heights.1; - records.push((chans, heights)); - } - for (chans, heights) in &records { - for &(id, len) in chans { - if len == 0 { - continue; - } - if len == 1 { - return Err(PsdError::invalid("channel data length 1")); - } - let rows = match id { - -2 => heights[1], - -3 => heights[2], - _ => heights[0], - }; - self.region(len, |t| t.channel_data(rows, false))?; - } - } - self.rest() - } - - /// A layer mask record. Returns the heights of the mask and real-mask rects. - fn mask(&mut self) -> Result<(usize, usize)> { - let whole = self.r.peek_rest(); - if whole.is_empty() { - return Ok((0, 0)); - } - let top = self.i32()?; - self.i32()?; - let bottom = self.i32()?; - self.i32()?; - self.u8()?; - let flags = self.u8()?; - let mut real_h = 0; - if self.remaining() <= 2 { - return Ok((height(top, bottom), 0)); - } - let has_parameters = flags & LayerMask::FLAG_PARAMETERS != 0; - // The helper only looks at lengths and the parameter-flags byte, so it reads the - // same in either order. - let real_first = has_parameters && LayerMask::real_before_parameters(whole); - let mut read_real = |t: &mut Tx<'_>| -> Result<()> { - if t.remaining() >= 18 { - t.u8()?; - t.u8()?; - let rt = t.i32()?; - t.i32()?; - let rb = t.i32()?; - t.i32()?; - real_h = height(rt, rb); - } - Ok(()) - }; - if real_first { - read_real(self)?; - } - if has_parameters { - let pf = self.u8()?; - let need = mask_parameter_bytes(pf); - if need > self.remaining() { - return Err(PsdError::UnexpectedEof { offset: self.r.pos(), needed: need - self.remaining() }); - } - if pf & 1 != 0 { - self.u8()?; - } - if pf & 2 != 0 { - self.f64()?; - } - if pf & 4 != 0 { - self.u8()?; - } - if pf & 8 != 0 { - self.f64()?; - } - } - if !real_first { - read_real(self)?; - } - Ok((height(top, bottom), real_h)) - } - - // ---- descriptors ---- - - fn versioned_descriptor(&mut self) -> Result<()> { - self.u32()?; - self.descriptor() - } - - fn descriptor(&mut self) -> Result<()> { - if self.depth > MAX_DEPTH { - return Err(PsdError::LimitExceeded("descriptor nesting too deep")); - } - self.depth += 1; - self.unicode()?; - self.id()?; - let n = self.u32()?; - self.r.check_count(u64::from(n), 10)?; - for _ in 0..n { - self.id()?; - self.value()?; - } - self.depth -= 1; - Ok(()) - } - - fn id(&mut self) -> Result<()> { - let n = self.u32()?; - if n == 0 { - self.key()?; - } else { - self.raw(usize::try_from(n).map_err(|_| PsdError::LimitExceeded("id length"))?)?; - } - Ok(()) - } - - fn class(&mut self) -> Result<()> { - self.unicode()?; - self.id() - } - - fn value(&mut self) -> Result<()> { - if self.depth > MAX_DEPTH { - return Err(PsdError::LimitExceeded("descriptor nesting too deep")); - } - let ty = self.key()?; - match &ty { - b"obj " => { - let n = self.u32()?; - self.r.check_count(u64::from(n), 8)?; - for _ in 0..n { - self.reference_item()?; - } - } - b"Objc" | b"GlbO" => self.descriptor()?, - b"VlLs" => { - let n = self.u32()?; - self.r.check_count(u64::from(n), 4)?; - self.depth += 1; - for _ in 0..n { - self.value()?; - } - self.depth -= 1; - } - b"doub" => { - self.f64()?; - } - b"UntF" => { - self.key()?; - self.f64()?; - } - b"UnFl" => { - self.key()?; - let n = self.u32()?; - self.r.check_count(u64::from(n), 8)?; - self.f64s(n as usize)?; - } - b"TEXT" => self.unicode()?, - b"enum" => { - self.id()?; - self.id()?; - } - b"long" => { - self.i32()?; - } - b"comp" => { - self.i64()?; - } - b"bool" => { - self.u8()?; - } - b"type" | b"GlbC" => self.class()?, - b"alis" | b"Pth " | b"tdta" => { - let n = self.u32()?; - self.raw(usize::try_from(n).map_err(|_| PsdError::LimitExceeded("descriptor data length"))?)?; - } - b"ObAr" => { - self.u32()?; - self.descriptor()?; - } - other => return Err(PsdError::Unsupported(format!("descriptor OSType {:?}", String::from_utf8_lossy(other)))), - } - Ok(()) - } - - fn reference_item(&mut self) -> Result<()> { - let ty = self.key()?; - match &ty { - b"prop" => { - self.class()?; - self.id()?; - } - b"Clss" => self.class()?, - b"Enmr" => { - self.class()?; - self.id()?; - self.id()?; - } - b"rele" => { - self.class()?; - self.i32()?; - } - b"Idnt" | b"indx" => { - self.u32()?; - } - b"name" => { - self.class()?; - self.unicode()?; - } - other => return Err(PsdError::Unsupported(format!("reference item type {:?}", String::from_utf8_lossy(other)))), - } - Ok(()) - } - - // ---- other block layouts ---- - - /// `shmd`: u32 count, then per item signature, key, copy flag, 3 bytes, u32 length, data. - /// An item whose layout is unknown is dropped on its own (and the count patched) rather - /// than taking the whole block with it. - fn metadata_setting(&mut self) -> Result<()> { - let n = self.r.u32()?; - self.r.check_count(u64::from(n), 16)?; - let count_at = self.out.len(); - self.out.extend_from_slice(&[0; 4]); - let mut kept = 0u32; - for _ in 0..n { - let before = self.out.len(); - self.key()?; - let key = self.key()?; - self.u8()?; - self.raw(3)?; - let same = self.same_order(); - let res = self.sized_region(false, |t| match &key { - b"cmls" | b"mlst" | b"cust" | b"tmln" | b"sgrp" | b"cinf" => t.versioned_descriptor(), - // mdyn (dynamic metadata) is one u32. - b"mdyn" => t.u32s_to_end(), - _ if same => Ok(()), - _ if t.descriptor_shaped() => Ok(()), - _ => Err(PsdError::Unsupported(format!("metadata item {:?}", String::from_utf8_lossy(&key)))), - }); - match res { - Ok(()) => kept += 1, - Err(e) if same => return Err(e), - Err(e) => { - self.out.truncate(before); - self.warnings.push(format!("shmd item {} dropped: {e}", String::from_utf8_lossy(&key))); - } - } - } - self.patch_len(count_at, false, u64::from(kept)) - } - - /// 26-byte path records: a u16 selector then, for knots, six fixed-point coordinates. - fn path_records(&mut self) -> Result<()> { - while self.remaining() >= 26 { - let selector = self.u16()?; - match selector { - 1 | 2 | 4 | 5 => self.i32s(6)?, - // Length records: knot count, path operation, then fields Photoshop writes as - // 16-bit values (zero in practice). - 0 | 3 => self.u16s(12)?, - // Clipboard: four fixed-point coordinates and a resolution. - 7 => { - self.i32s(5)?; - self.raw(4)?; - } - 8 => { - self.u16()?; - self.raw(22)?; - } - _ => self.raw(24)?, - } - } - Ok(()) - } - - /// One pattern of a `Patt` block (the data after its u32 length). - fn pattern(t: &mut Tx<'_>) -> Result<()> { - t.u32()?; - let mode = t.u32()?; - t.u16()?; - t.u16()?; - t.unicode()?; - t.pascal(1)?; - if mode == 2 { - t.raw(768)?; - // Some writers follow the palette with 4 extra bytes before the array list version. - let next_is_version = t.r.clone().u32().is_ok_and(|v| v == 3); - if !next_is_version && t.remaining() >= 4 { - t.raw(4)?; - } - } - t.u32()?; - t.sized_region(false, |v| { - v.i32s(4)?; - let count = v.u32()?; - for _ in 0..count.saturating_add(2) { - if v.remaining() < 4 { - break; - } - if v.u32()? == 0 { - continue; - } - if v.r.clone().u32()? == 0 { - v.u32()?; - continue; - } - v.sized_region(false, |c| { - c.u32()?; - let ct = c.i32()?; - c.i32()?; - let cb = c.i32()?; - c.i32()?; - c.u16()?; - let comp = c.u8()?; - if comp == 1 { - let saved = c.version; - c.version = Version::Psd; - let res = c.rle_counts(height(ct, cb)); - c.version = saved; - res?; - } - Ok(()) - })?; - } - Ok(()) - }) - } - - /// `FEid` / `FXid`: version, u64 length, items with per-channel planes (4-byte RLE counts). - fn filter_effects(&mut self) -> Result<()> { - self.u32()?; - self.sized_region(true, |t| { - while !t.r.is_empty() { - t.pascal(1)?; - t.u32()?; - t.sized_region(true, |it| { - let top = it.i32()?; - it.i32()?; - let bottom = it.i32()?; - it.i32()?; - let rect_h = height(top, bottom); - it.u32()?; - let max = it.u32()?; - if max > crate::filter_effects::MAX_SLOTS { - return Err(PsdError::LimitExceeded("filter effects: too many channels")); - } - for _ in 0..max + 2 { - if it.u32()? != 0 { - it.effects_plane(rect_h)?; - } - } - Ok(()) - })?; - if !t.r.is_empty() && t.u8()? != 0 { - let top = t.i32()?; - t.i32()?; - let bottom = t.i32()?; - t.i32()?; - t.effects_plane(height(top, bottom))?; - } - } - Ok(()) - }) - } - - fn effects_plane(&mut self, rows: usize) -> Result<()> { - self.sized_region(true, |p| p.channel_data(rows, true)) - } - - /// `lrFX`: version, count, then effects (`cmnS`, `dsdw`, `isdw`, `oglw`, `iglw`, `bevl`, - /// `sofi`), each a signature, key, u32 size and fixed fields. - fn legacy_effects(&mut self) -> Result<()> { - self.u16()?; - let n = self.u16()?; - for _ in 0..n { - if self.remaining() < 12 { - break; - } - self.key()?; - let key = self.key()?; - self.sized_region(false, |t| { - // A colour: u16 space + 4 × u16 components. - let colour = |t: &mut Tx<'_>| t.u16s(5); - let blend = |t: &mut Tx<'_>| -> Result<()> { - t.key()?; - t.key()?; - Ok(()) - }; - match &key { - b"cmnS" => { - t.u32()?; - t.u8()?; - t.u16()?; - } - b"dsdw" | b"isdw" => { - t.u32()?; - t.i32s(4)?; - colour(t)?; - blend(t)?; - t.u8()?; - t.u8()?; - t.u8()?; - if t.remaining() >= 10 { - colour(t)?; - } - } - b"oglw" => { - t.u32()?; - t.i32s(2)?; - colour(t)?; - blend(t)?; - t.u8()?; - t.u8()?; - if t.remaining() >= 10 { - colour(t)?; - } - } - b"iglw" => { - t.u32()?; - t.i32s(2)?; - colour(t)?; - blend(t)?; - t.u8()?; - t.u8()?; - if t.remaining() >= 11 { - t.u8()?; - colour(t)?; - } - } - b"bevl" => { - t.u32()?; - t.i32s(3)?; - blend(t)?; - blend(t)?; - colour(t)?; - colour(t)?; - for _ in 0..6 { - t.u8()?; - } - if t.remaining() >= 20 { - colour(t)?; - colour(t)?; - } - } - b"sofi" => { - t.u32()?; - blend(t)?; - colour(t)?; - t.u8()?; - t.u8()?; - if t.remaining() >= 10 { - colour(t)?; - } - } - _ => {} - } - Ok(()) - })?; - } - Ok(()) - } - - /// `curv`: pad byte, version, channel bitmap, per set bit a point list; then an optional - /// `Crv ` extension with per-channel point lists. - fn curves(&mut self) -> Result<()> { - self.u8()?; - self.u16()?; - let bits = self.u32()?; - for bit in 0..32 { - if bits & (1 << bit) == 0 { - continue; - } - if self.remaining() < 2 { - break; - } - let n = self.u16()?; - self.u16s(usize::from(n) * 2)?; - } - if self.remaining() >= 10 { - self.key()?; - self.u16()?; - let n = self.u32()?; - for _ in 0..n { - if self.remaining() < 4 { - break; - } - self.u16()?; - let pts = self.u16()?; - self.u16s(usize::from(pts) * 2)?; - } - } - Ok(()) - } - - /// `grdm`: version, reverse, dither, [method key], name, colour stops, transparency - /// stops, then the noise-gradient fields (a u32 seed among u16s). - fn gradient_map(&mut self) -> Result<()> { - let version = self.u16()?; - self.u8()?; - self.u8()?; - if version == 3 { - self.key()?; - } - self.unicode()?; - let n = self.u16()?; - for _ in 0..n { - if self.remaining() < 20 { - break; - } - self.u32()?; - self.u32()?; - self.u16s(6)?; - } - let nt = self.u16()?; - for _ in 0..nt { - if self.remaining() < 10 { - break; - } - self.u32()?; - self.u32()?; - self.u16()?; - } - // expansion, interpolation, length, mode, u32 seed, then u16 fields. - for _ in 0..4 { - if self.remaining() < 2 { - return Ok(()); - } - self.u16()?; - } - if self.remaining() >= 4 { - self.u32()?; - } - self.u16s_to_end() - } - } -} +pub mod transcode; #[cfg(test)] -mod tests { - use super::transcode::*; - use super::*; - use crate::compression::Compression; - use crate::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; - use crate::header::ColorMode; - use crate::testgen; - - /// Keys the generator plants that the transcoder (rightly) does not know. - const UNKNOWN: [&[u8; 4]; 3] = [b"zOdd", b"zNop", b"Txt2"]; - - /// `d` without the blocks a little-endian round trip drops, and without the layer-info - /// padding (which follows the body length, so it changes when blocks are dropped). - fn known(d: &ImageSourceData) -> ImageSourceData { - let mut d = d.clone(); - d.byte_order = ByteOrder::Big; - d.global_blocks.retain(|b| !UNKNOWN.contains(&&b.key)); - if d.layer_info.is_none() { - d.layer_info_key = *b"Layr"; - } - if let Some(info) = &mut d.layer_info { - info.padding = None; - for l in &mut info.layers { - l.blocks.retain(|b| !UNKNOWN.contains(&&b.key)); - } - } - d - } - - fn roundtrip(d: &ImageSourceData) -> ImageSourceData { - let (big, w) = d.to_bytes(ByteOrder::Big).unwrap(); - assert!(w.is_empty(), "{w:?}"); - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(w.iter().all(|w| UNKNOWN.iter().any(|k| w.starts_with(std::str::from_utf8(*k).unwrap()))), "{w:?}"); - if w.is_empty() { - assert_eq!(big.len(), little.len()); - } - if big.len() > 36 { - assert_ne!(big, little); - assert!(little[36..].starts_with(b"MIB8")); - } - let (back_big, w) = ImageSourceData::from_bytes(&big).unwrap(); - assert!(w.is_empty(), "{w:?}"); - let (back_little, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - if little.len() > 36 { - assert_eq!(back_little.byte_order, ByteOrder::Little); - } - assert_eq!(known(&back_little), known(&back_big)); - // The little-endian bytes are a fixed point (from the second generation on when blocks - // were dropped, since the layer-info padding then follows the shorter body). - let (little2, _) = back_little.to_bytes(ByteOrder::Little).unwrap(); - let little3 = ImageSourceData::from_bytes(&little2).unwrap().0.to_bytes(ByteOrder::Little).unwrap().0; - assert_eq!(little3, little2); - back_big - } - - #[test] - fn generated_files_survive_both_orders() { - for case in testgen::all_cases() { - let d = ImageSourceData::from_psd(&case.file); - let back = roundtrip(&d); - assert_eq!(back.version, d.version, "{}", case.name); - assert_eq!(known(&back), known(&d), "{}", case.name); - // Nothing but the planted unknown blocks is lost on the little-endian path. - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(known(&back), known(&d), "{}", case.name); - } - } - - #[test] - fn same_order_transcoding_is_the_identity() { - for case in testgen::all_cases() { - let d = ImageSourceData::from_psd(&case.file); - let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); - let (same, w) = transcode_blocks(&big[36..], d.version, ByteOrder::Big, ByteOrder::Big).unwrap(); - assert!(w.is_empty()); - assert_eq!(same, big[36..].to_vec(), "{}", case.name); - } - } - - fn sample_layers() -> ImageSourceData { - let file = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); - ImageSourceData::from_psd(&file) - } - - #[test] - fn signature_selects_the_version() { - let mut d = sample_layers(); - let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); - assert!(b.starts_with(SIGNATURE)); - d.version = Version::Psb; - let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); - assert!(b.starts_with(SIGNATURE_PSB)); - assert_eq!(ImageSourceData::from_bytes(&b).unwrap().0.version, Version::Psb); - roundtrip(&d); - } - - #[test] - fn layer_key_follows_depth() { - assert_eq!(layer_info_key(8), *b"Layr"); - assert_eq!(layer_info_key(16), *b"Lr16"); - assert_eq!(layer_info_key(32), *b"Lr32"); - for (depth, comp) in [(16, Compression::ZipPrediction), (32, Compression::ZipPrediction), (16, Compression::Rle)] { - let file = testgen::layered(Version::Psd, ColorMode::Rgb, depth, comp); - let d = ImageSourceData::from_psd(&file); - assert_eq!(d.layer_info_key, layer_info_key(depth)); - let back = roundtrip(&d); - assert_eq!(back.layer_info_key, d.layer_info_key); - // The 16/32-bit model goes back into a PSD as a global block, like Photoshop's. - let psd = back.into_psd(file.header.clone(), file.resources.clone(), file.image_data.clone()); - assert!(matches!(psd.layer_info_placement, LayerInfoPlacement::GlobalBlock { .. })); - let again = PsdFile::from_bytes(&psd.to_bytes().unwrap()).unwrap(); - assert_eq!(known(&ImageSourceData::from_psd(&again)).layer_info, known(&d).layer_info); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(known(&back).layer_info, known(&d).layer_info, "depth {depth}"); - } - } - - #[test] - fn blocks_are_padded_to_four() { - let d = sample_layers(); - let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); - let mut at = 36; - while at < b.len() { - assert_eq!(&b[at..at + 4], b"8BIM"); - let len = u32::from_be_bytes(b[at + 8..at + 12].try_into().unwrap()) as usize; - at += 12 + len; - at += (4 - at % 4) % 4; - } - assert_eq!(at, b.len()); - } - - fn block_rt(key: [u8; 4], data: Vec) -> (Vec, Vec) { - let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(key, data.clone())], ..Default::default() }; - let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - assert_eq!(back.global_blocks[0].data, data); - assert_eq!(back.to_bytes(ByteOrder::Big).unwrap().0, big); - (big, little) - } - - #[test] - fn descriptor_blocks_swap_keys_and_numbers() { - let mut desc = Descriptor::new("null"); - desc.items.push((Id::new("Opct"), Value::UnitFloat { unit: *b"#Prc", value: 75.5 })); - desc.items.push((Id::new("Nm "), Value::Text(UnicodeString::new_nul("Drop Shadow")))); - desc.items.push((Id::new("enab"), Value::Boolean(true))); - desc.items.push((Id::new("Md "), Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new("Mltp") })); - desc.items.push((Id::new("list"), Value::List(vec![Value::Integer(-3), Value::Double(1.5), Value::LargeInteger(7)]))); - desc.items.push((Id::new("longKeyName"), Value::Descriptor(Descriptor::new("innr")))); - let mut data = 0u32.to_be_bytes().to_vec(); - data.extend(VersionedDescriptor::new(desc).to_bytes()); - let (big, little) = block_rt(*b"lfx2", data); - // Keys are reversed and the version field swapped in the little-endian bytes. - assert!(little[36..].starts_with(b"MIB82xfl")); - let body_at = 48; - assert_eq!(&little[body_at..body_at + 4], &[0, 0, 0, 0]); - assert_eq!(&little[body_at + 4..body_at + 8], &[16, 0, 0, 0]); - assert_eq!(&big[body_at + 4..body_at + 8], &[0, 0, 0, 16]); - assert!(little.windows(4).any(|w| w == b"crP#"), "unit key reversed"); - assert!(little.windows(11).any(|w| w == b"longKeyName"), "string ids are not reversed"); - } - - #[test] - fn type_block_layout() { - let mut data = Vec::new(); - data.put_u16(1); - for v in [1.0, 0.0, 0.0, 1.0, 12.5, -3.0] { - data.put_f64(v); - } - data.put_u16(50); - data.extend(VersionedDescriptor::new(Descriptor::new("TxLr")).to_bytes()); - data.put_u16(1); - data.extend(VersionedDescriptor::new(Descriptor::new("warp")).to_bytes()); - for v in [0.0, 0.0, 100.0, 20.0] { - data.put_f64(v); - } - let (_, little) = block_rt(*b"TySh", data); - assert!(little[48..].starts_with(&[1, 0])); - assert_eq!(&little[50..58], &1.0f64.to_le_bytes()); - } - - #[test] - fn legacy_effects_and_adjustments() { - // lrFX with a drop shadow and a solid fill. - let mut fx = Vec::new(); - fx.put_u16(0); - fx.put_u16(3); - fx.put(b"8BIMcmnS"); - fx.put_u32(7); - fx.put_u32(0); - fx.put_u8(1); - fx.put_u16(0); - fx.put(b"8BIMdsdw"); - let mut sh = Vec::new(); - sh.put_u32(2); - for v in [5 << 16, 75 << 16, 120 << 16, 7 << 16] { - sh.put_i32(v); - } - for _ in 0..5 { - sh.put_u16(0); - } - sh.put(b"8BIMmul "); - sh.put_u8(1); - sh.put_u8(1); - sh.put_u8(191); - sh.put_u16(0); - sh.put_u16(0x1234); - sh.put_u16(0); - sh.put_u16(0); - sh.put_u16(0); - fx.put_u32(sh.len() as u32); - fx.put(&sh); - fx.put(b"8BIMsofi"); - let mut so = Vec::new(); - so.put_u32(2); - so.put(b"8BIMnorm"); - so.put_u16(0); - so.put_u16(0xffff); - so.put_u16(0); - so.put_u16(0); - so.put_u16(0); - so.put_u8(255); - so.put_u8(1); - so.put_u16(0); - so.put_u16(0xffff); - so.put_u16(0); - so.put_u16(0); - so.put_u16(0); - fx.put_u32(so.len() as u32); - fx.put(&so); - let (_, little) = block_rt(*b"lrFX", fx); - assert!(little.windows(8).any(|w| w == b"MIB8wdsd"), "effect keys reversed"); - assert!(little.windows(2).any(|w| w == [0x34, 0x12]), "colour component swapped"); - - // levl: version + 29 records + the Lvls extension. - let mut lv = Vec::new(); - lv.put_u16(2); - for i in 0..29 * 5 { - lv.put_u16(i as u16); - } - lv.put(b"Lvls"); - lv.put_u16(3); - lv.put_u16(33); - for i in 0..4 * 5 { - lv.put_u16(1000 + i as u16); - } - let (_, little) = block_rt(*b"levl", lv); - assert!(little.windows(4).any(|w| w == b"slvL")); - - // curv with the Crv extension. - let mut cv = vec![0u8]; - cv.put_u16(1); - cv.put_u32(0b101); - cv.put_u16(2); - cv.put_u16(0); - cv.put_u16(0); - cv.put_u16(255); - cv.put_u16(255); - cv.put_u16(1); - cv.put_u16(128); - cv.put_u16(100); - cv.put(b"Crv "); - cv.put_u16(4); - cv.put_u32(1); - cv.put_u16(2); - cv.put_u16(1); - cv.put_u16(7); - cv.put_u16(9); - block_rt(*b"curv", cv); - - for (key, data) in [ - (*b"hue2", vec![0, 2, 0, 0, 0, 10, 0, 20, 0, 30, 0, 40, 0, 50, 0, 60]), - (*b"brit", vec![0, 10, 0, 20, 0, 30, 1, 0]), - (*b"thrs", vec![0, 128, 0, 0]), - (*b"post", vec![0, 4, 0, 0]), - (*b"nvrt", vec![]), - (*b"expA", [1u16.to_be_bytes().to_vec(), 0.5f32.to_be_bytes().to_vec(), 0.0f32.to_be_bytes().to_vec(), 1.0f32.to_be_bytes().to_vec()].concat()), - (*b"blnc", vec![0; 20]), - (*b"mixr", vec![0, 1, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0]), - (*b"selc", vec![0, 1, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8]), - (*b"phfl", vec![0, 3, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 25, 1, 0]), - (*b"phfl", vec![0, 2, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 25, 1, 0]), - (*b"lclr", vec![0, 1, 0, 0, 0, 0, 0, 0]), - (*b"fxrp", [1.5f64.to_be_bytes().to_vec(), 2.5f64.to_be_bytes().to_vec()].concat()), - (*b"brst", vec![0, 0, 0, 1, 0, 0, 0, 2]), - (*b"FMsk", vec![0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 50]), - (*b"lsct", [3u32.to_be_bytes().to_vec(), b"8BIMpass".to_vec()].concat()), - (*b"lspf", vec![0, 0, 0, 5]), - (*b"lnsr", b"layr".to_vec()), - (*b"knko", vec![1, 0, 0, 0]), - (*b"knko", vec![2, 0, 0, 0]), - (*b"infx", vec![1, 0, 0, 0]), - (*b"clbl", vec![0, 0, 0, 0]), - (*b"tsly", vec![0, 0, 0, 0]), - (*b"lmgm", vec![1, 0, 0, 0]), - (*b"vmgm", vec![1, 0, 0, 0]), - (*b"iOpa", vec![128, 0, 0, 0]), - ] { - let (big, little) = block_rt(key, data.clone()); - if data.len() >= 2 && key != *b"nvrt" { - assert_ne!(big, little, "{}", String::from_utf8_lossy(&key)); - } - } - } - - #[test] - fn global_layer_mask_block() { - let d = ImageSourceData { global_layer_mask: Some(GlobalLayerMask { data: vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 128, 0] }), ..Default::default() }; - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(w.is_empty()); - assert!(little[36..].starts_with(b"MIB8ksML")); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(back.global_layer_mask, d.global_layer_mask); - assert_eq!(back.global_layer_mask.unwrap().opacity(), Some(50)); - } - - #[test] - fn gradient_map_layout() { - let mut g = Vec::new(); - g.put_u16(3); - g.put_u8(0); - g.put_u8(1); - g.put(b"Lnr "); - g.put_u32(2); - g.put_u16(u16::from(b'a')); - g.put_u16(0); - g.put_u16(2); - for (loc, mid) in [(0u32, 50u32), (4096, 50)] { - g.put_u32(loc); - g.put_u32(mid); - g.put_u16(0); - g.put_u16(0x1111); - g.put_u16(0x2222); - g.put_u16(0x3333); - g.put_u16(0); - g.put_u16(0); - } - g.put_u16(2); - for loc in [0u32, 4096] { - g.put_u32(loc); - g.put_u32(50); - g.put_u16(100); - } - g.put_u16(2); - g.put_u16(1); - g.put_u16(32); - g.put_u16(0); - g.put_u32(0xdead_beef); - for _ in 0..12 { - g.put_u16(0); - } - let (_, little) = block_rt(*b"grdm", g); - assert!(little.windows(4).any(|w| w == 0xdead_beefu32.to_le_bytes())); - assert!(little.windows(4).any(|w| w == b" rnL")); - } - - #[test] - fn vector_mask_records() { - let mut v = Vec::new(); - v.put_u32(3); - v.put_u32(0); - // Closed length record, then two linked knots. - v.put_u16(0); - v.put_u16(2); - v.put_i16(1); - v.put(&[0; 20]); - for sel in [1u16, 1] { - v.put_u16(sel); - for i in 0..6 { - v.put_i32((i + 1) * 0x0010_0000); - } - } - let (_, little) = block_rt(*b"vmsk", v); - assert!(little.windows(4).any(|w| w == [0, 0, 0x10, 0]), "fixed-point coordinate swapped"); - } - - #[test] - fn pattern_block() { - let plane: Vec = (0..16u8).collect(); - let p = crate::patterns::PsdPattern { - mode: 3, - width: 4, - height: 4, - name: "dots".into(), - id: "abc".into(), - palette: None, - depth: 8, - channels: vec![plane.clone(), plane.clone(), plane.clone()], - alpha: Some(plane), - }; - let data = crate::patterns::write_pattern_block(std::slice::from_ref(&p)).unwrap(); - block_rt(*b"Patt", data.clone()); - // Little-endian pattern data parses back into the same pattern. - let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"Patt", data)], ..Default::default() }; - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(crate::patterns::parse_pattern_block(&back.global_blocks[0].data).unwrap(), vec![p]); - } - - #[test] - fn filter_effects_block() { - use crate::filter_effects::{EffectsPlane, FilterEffects, FilterEffectsItem}; - let plane = EffectsPlane::encode(Compression::Rle, &[1, 2, 3, 4, 5, 6], 3, 2, 8).unwrap(); - let mask = EffectsPlane::encode(Compression::Raw, &[9; 6], 3, 2, 8).unwrap(); - let rect = crate::layer::Rect { top: 0, left: 0, bottom: 2, right: 3 }; - let fx = FilterEffects { - version: 1, - items: vec![FilterEffectsItem { - id: "x".into(), - version: 1, - rect, - depth: 8, - max_channels: 1, - slots: vec![Some(plane.clone()), None, Some(plane)], - mask: Some((rect, mask)), - }], - }; - let data = fx.to_bytes(); - block_rt(*b"FEid", data.clone()); - let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"FEid", data)], ..Default::default() }; - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(FilterEffects::parse(&back.global_blocks[0].data).unwrap(), fx); - } - - #[test] - fn metadata_setting_items() { - let desc = VersionedDescriptor::new(Descriptor::new("null")).to_bytes(); - let items = vec![crate::metadata::MetadataItem::new(*b"cmls", desc)]; - block_rt(*b"shmd", crate::metadata::write_shmd(&items)); - // An unknown item cannot be converted: it is dropped on its own, the rest stays. - let items = vec![ - crate::metadata::MetadataItem::new(*b"zzzz", vec![1, 2, 3, 4, 5, 6, 7, 8]), - crate::metadata::MetadataItem::new(*b"mdyn", vec![0, 0, 0, 1]), - crate::metadata::MetadataItem::new(*b"cmls", VersionedDescriptor::new(Descriptor::new("null")).to_bytes()), - ]; - let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"shmd", crate::metadata::write_shmd(&items))], ..Default::default() }; - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert_eq!(w.len(), 1, "{w:?}"); - assert!(w[0].starts_with("shmd item zzzz dropped"), "{}", w[0]); - let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert!(w.is_empty()); - let back_items = crate::metadata::parse_shmd(&back.global_blocks[0].data).unwrap(); - assert_eq!(back_items, items[1..].to_vec()); - } - - #[test] - fn unknown_descriptor_shaped_blocks_are_converted() { - // A key the transcoder has never heard of, holding a versioned descriptor (as every - // block Photoshop added since CS6 does): converted, not dropped. - let mut desc = Descriptor::new("null"); - desc.items.push((Id::new("Nm "), Value::Text(UnicodeString::new_nul("x")))); - let data = VersionedDescriptor::new(desc.clone()).to_bytes(); - block_rt(*b"Qqqq", data); - let mut with_version = 3u32.to_be_bytes().to_vec(); - with_version.extend(VersionedDescriptor::new(desc).to_bytes()); - block_rt(*b"Qqqq", with_version); - // Empty data needs no conversion either. - block_rt(*b"Mt32", Vec::new()); - } - - #[test] - fn linked_layer_items() { - // One embedded file (liFD, version 7) as photocraft-io writes it. - let mut item = Vec::new(); - item.put(b"liFD"); - item.put_u32(7); - item.put_u8(4); - item.put(b"uuid"); - let units: Vec = "a.png\0".encode_utf16().collect(); - item.put_u32(units.len() as u32); - for u in units { - item.put_u16(u); - } - item.put(b"png "); - item.put(b"8BIM"); - let bytes = [0x89, b'P', b'N', b'G', 1, 2, 3]; - item.put_u64(bytes.len() as u64); - item.put_u8(0); - item.put(&bytes); - item.put_u32(0); - item.put_f64(0.0); - item.put_u8(0); - let mut data = (item.len() as u64).to_be_bytes().to_vec(); - data.put(&item); - while !data.len().is_multiple_of(4) { - data.push(0); - } - let (_, little) = block_rt(*b"lnk2", data); - assert!(little.windows(4).any(|w| w == b"DFil"), "item type key reversed"); - assert!(little.windows(7).any(|w| w == bytes), "file bytes untouched"); - } - - #[test] - fn annotation_block() { - let mut a = Vec::new(); - a.put_u16(2); - a.put_u16(1); - a.put_u32(1); - let mut item = Vec::new(); - item.put(b"txtA"); - item.put_u8(0); - item.put_u8(1); - item.put_u16(0); - for v in [10, 20, 30, 40, 50, 60, 70, 80] { - item.put_i32(v); - } - item.put_u16(0); - for v in [0xffff, 0, 0, 0] { - item.put_u16(v); - } - for s in [&b"me"[..], b"note", b"2026"] { - item.put_u8(s.len() as u8); - item.put(s); - if (1 + s.len()) % 2 == 1 { - item.put_u8(0); - } - } - let text = [0xfe, 0xff, 0, b'h', 0, b'i']; - let mut d = Vec::new(); - d.put(b"txtC"); - d.put_u32(text.len() as u32); - d.put(&text); - item.put_u32(d.len() as u32 + 4); - item.put(&d); - a.put_u32(item.len() as u32 + 4); - a.put(&item); - let (_, little) = block_rt(*b"Anno", a); - assert!(little.windows(4).any(|w| w == b"Atxt")); - assert!(little.windows(6).any(|w| w == text), "text kept as stored"); - } - - #[test] - fn unknown_blocks_kept_in_big_dropped_in_little() { - let mut d = sample_layers(); - d.global_blocks.push(TaggedBlock::new(*b"Zzzz", vec![1, 2, 3, 4, 5])); - let (big, w) = d.to_bytes(ByteOrder::Big).unwrap(); - assert!(w.is_empty()); - let (back, w) = ImageSourceData::from_bytes(&big).unwrap(); - assert!(w.is_empty()); - assert_eq!(back.global_blocks, d.global_blocks); - // Big-endian data of unknown layout cannot go into a little-endian file. - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert_eq!(w.iter().filter(|w| w.starts_with("Zzzz block (5 bytes) dropped")).count(), 1, "{w:?}"); - let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert!(w.is_empty()); - assert_eq!(back.global_blocks.iter().map(|b| b.key).collect::>(), vec![*b"Patt", *b"FMsk"]); - assert_eq!(known(&back).layer_info, known(&d).layer_info); - } - - #[test] - fn unknown_blocks_from_a_little_endian_file_are_kept_with_their_byte_order() { - let d = sample_layers(); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - // A little-endian file with a block of unknown layout, among known ones. - let mut raw = little.clone(); - raw.extend_from_slice(b"MIB8zzzZ"); - raw.extend_from_slice(&6u32.to_le_bytes()); - raw.extend_from_slice(&[9, 8, 7, 6, 5, 4, 0, 0]); - raw.extend_from_slice(b"MIB8diyl"); - raw.extend_from_slice(&4u32.to_le_bytes()); - raw.extend_from_slice(&7u32.to_le_bytes()); - let (back, w) = ImageSourceData::from_bytes(&raw).unwrap(); - assert!(w.is_empty(), "{w:?}"); - assert_eq!(known(&back).layer_info, known(&d).layer_info); - let kept = back.global_blocks.iter().find(|b| &b.key == b"Zzzz").expect("kept"); - assert!(kept.is_foreign_order()); - assert_eq!(kept.signature, *b"MIB8"); - assert_eq!(kept.data, [9, 8, 7, 6, 5, 4]); - // The known block after it was still converted. - assert_eq!(back.global_blocks.iter().find(|b| &b.key == b"lyid").unwrap().data, 7u32.to_be_bytes()); - // Written back to a little-endian TIFF, the bytes are exactly what was read. - let (again, w) = back.to_bytes(ByteOrder::Little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - let tail = &raw[raw.len() - 18 - 16..]; - assert!(again.windows(tail.len()).any(|x| x == tail), "foreign-order block restored verbatim"); - assert_eq!(ImageSourceData::from_bytes(&again).unwrap().0.global_blocks, back.global_blocks); - // The big-endian (PSD) model keeps the marker, so a PSD writer can tell it apart. - let (big, _) = back.to_bytes(ByteOrder::Big).unwrap(); - assert!(big.windows(8).any(|x| x == b"MIB8Zzzz")); - assert_eq!(ImageSourceData::from_bytes(&big).unwrap().0.global_blocks, back.global_blocks); - } - - /// A PSB keeps `Mt16`/`Mt32`/`Mtrn`/`Alph` blocks of unknown layout verbatim, and their - /// length field is 8 bytes (a PSB long key). Writing them into a little-endian TIFF must - /// read that width; the old code looked the key up after reversing it for the target - /// order, which never matches `PSB_LONG_KEYS`. - #[test] - fn psb_foreign_order_long_key_roundtrips() { - let d = ImageSourceData { - version: Version::Psb, - byte_order: ByteOrder::Little, - global_blocks: vec![TaggedBlock { signature: *b"MIB8", key: *b"Mt16", data: vec![9, 8, 7, 6, 5, 4], padding: None }], - ..Default::default() - }; - let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert!(w.is_empty(), "{w:?}"); - assert_eq!(back.global_blocks, d.global_blocks, "the 8-byte length and the bytes survive"); - } - - #[test] - fn rle_counts_follow_the_container_and_samples_do_not() { - let file = testgen::layered(Version::Psd, ColorMode::Rgb, 16, Compression::Rle); - let d = ImageSourceData::from_psd(&file); - let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - let d = known(&d); - let (a, b) = (d.layer_info.as_ref().unwrap(), back.layer_info.as_ref().unwrap()); - for (la, lb) in a.layers.iter().zip(&b.layers) { - for (ca, cb) in la.channels.iter().zip(&lb.channels) { - assert_eq!(ca, cb); - let (w, h) = la.channel_rect(ca.id).size().unwrap(); - assert_eq!(ca.decode(w, h, 16, Version::Psd).unwrap(), cb.decode(w, h, 16, Version::Psd).unwrap()); - } - } - // The encoded samples (after the count table) are identical bytes in both files, while - // the count table itself is swapped. - let (rec, ch) = a - .layers - .iter() - .flat_map(|l| l.channels.iter().filter(|c| c.id >= -1 && c.compression == Some(Compression::Rle)).map(move |c| (l, c))) - .find(|(l, c)| l.rect.height() > 1 && c.data.len() > l.rect.height() as usize * 2 + 8) - .unwrap(); - let table = rec.rect.height() as usize * 2; - let samples = &ch.data[table..]; - assert!(big.windows(samples.len()).any(|w| w == samples)); - assert!(little.windows(samples.len()).any(|w| w == samples)); - let counts = &ch.data[..table]; - let swapped: Vec = counts.chunks(2).flat_map(|c| [c[1], c[0]]).collect(); - assert!(little.windows(table + samples.len()).any(|w| w[..table] == swapped[..] && w[table..] == *samples)); - } - - #[test] - fn blending_ranges_are_bytes_in_either_order() { - let mut file = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); - let ranges: Vec = [0u8, 0, 255, 255].repeat(10); - let marked: Vec = (1u8..=40).collect(); - for (i, l) in file.layer_info.as_mut().unwrap().layers.iter_mut().enumerate() { - l.blending_ranges.data = if i == 0 { marked.clone() } else { ranges.clone() }; - } - let d = ImageSourceData::from_psd(&file); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - // Written verbatim (what Photoshop writes in an `II` TIFF), not swapped per u32. - assert!(little.windows(marked.len()).any(|w| w == marked)); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - for (a, b) in d.layer_info.as_ref().unwrap().layers.iter().zip(&back.layer_info.as_ref().unwrap().layers) { - assert_eq!(a.blending_ranges, b.blending_ranges); - } - } - - #[test] - fn psb_sized_data_uses_long_lengths_and_counts() { - let file = testgen::layered(Version::Psb, ColorMode::Cmyk, 8, Compression::Rle); - let d = ImageSourceData::from_psd(&file); - assert_eq!(d.version, Version::Psb); - let back = roundtrip(&d); - assert_eq!(back.layer_info, d.layer_info); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(little[36..].starts_with(b"MIB8")); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(known(&back).layer_info, known(&d).layer_info); - } - - #[test] - fn resources_both_orders() { - let res = vec![ - ImageResource::new(1005, vec![0, 72, 0, 0, 0, 1, 0, 1, 0, 72, 0, 0, 0, 1, 0, 1]), - ImageResource::new(1037, vec![0, 0, 0, 120]), - ImageResource::new(1039, vec![1, 2, 3]), - ImageResource { signature: *b"8BIM", id: 1026, name: b"grp".to_vec(), data: vec![0, 1, 0, 1, 0, 0] }, - ]; - let big = resources_to_bytes(&res).unwrap(); - let (back, w) = resources_from_bytes(&big).unwrap(); - assert!(w.is_empty()); - assert_eq!(back, res); - let (little, w) = transcode_resources(&big, ByteOrder::Big, ByteOrder::Little).unwrap(); - assert!(w.is_empty()); - assert!(little.starts_with(b"MIB8")); - assert_eq!(little.len(), big.len()); - assert_eq!(&little[4..6], &[0xed, 3]); - let (back, w) = resources_from_bytes(&little).unwrap(); - assert!(w.is_empty()); - assert_eq!(back, res); - // The ICC blob is untouched by the swap. - assert!(little.windows(3).any(|w| w == [1, 2, 3])); - // An odd trailing pad byte is tolerated. - let mut padded = big.clone(); - padded.push(0); - assert_eq!(resources_from_bytes(&padded).unwrap().0, res); - } - - #[test] - fn malformed_input_errors_without_panicking() { - assert!(ImageSourceData::from_bytes(b"").is_err()); - assert!(ImageSourceData::from_bytes(b"Adobe Photoshop Document Data Block").is_err()); - let mut bad = SIGNATURE.to_vec(); - bad.extend_from_slice(b"XXXXLayr"); - assert!(ImageSourceData::from_bytes(&bad).is_err()); - let d = sample_layers(); - for order in [ByteOrder::Big, ByteOrder::Little] { - let (bytes, _) = d.to_bytes(order).unwrap(); - for cut in (0..bytes.len()).step_by(7).chain(bytes.len().saturating_sub(8)..bytes.len()) { - let _ = ImageSourceData::from_bytes(&bytes[..cut]); - } - // Bit flips in the structure (not the signature). - for i in (36..bytes.len()).step_by(13) { - let mut b = bytes.clone(); - b[i] ^= 0x5a; - let _ = ImageSourceData::from_bytes(&b); - } - } - // A block that claims more data than exists. - let mut huge = SIGNATURE.to_vec(); - huge.extend_from_slice(b"8BIMluni"); - huge.extend_from_slice(&u32::MAX.to_be_bytes()); - assert!(ImageSourceData::from_bytes(&huge).is_err()); - assert!(resources_from_bytes(b"8BIM\x03\xed\x00").is_err()); - } - - #[test] - fn into_psd_roundtrips_through_the_psd_writer() { - let file = testgen::layered(Version::Psd, ColorMode::Grayscale, 8, Compression::Zip); - let d = ImageSourceData::from_psd(&file); - let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); - let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); - let psd = back.into_psd(file.header.clone(), file.resources.clone(), file.image_data.clone()); - let bytes = psd.to_bytes().unwrap(); - let again = PsdFile::from_bytes(&bytes).unwrap(); - let expected = known(&ImageSourceData::from_psd(&file)); - assert_eq!(known(&ImageSourceData::from_psd(&again)).layer_info, expected.layer_info); - assert_eq!(again.global_blocks, expected.global_blocks); - assert_eq!(again.layer_tree().len(), file.layer_tree().len()); - } - - /// Global blocks are padded as they are written, not on a copy: the bytes are those of the - /// former path (each block cloned with explicit padding to 4, written with `write_blocks`). - #[test] - fn global_blocks_written_without_copies_keep_their_bytes() { - for version in [Version::Psd, Version::Psb] { - let mut d = ImageSourceData { version, global_layer_mask: Some(GlobalLayerMask { data: vec![0, 1, 0, 2, 0, 3, 0, 4, 9] }), ..Default::default() }; - for n in 0..8u8 { - d.global_blocks.push(TaggedBlock::new(*b"FMsk", (0..n).collect())); - } - let mut explicit = TaggedBlock::new(*b"FMsk", vec![1, 2, 3]); - explicit.padding = Some(vec![]); - d.global_blocks.push(explicit); - let mut odd = TaggedBlock::new(*b"FMsk", vec![1, 2]); - odd.padding = Some(vec![0, 0, 0, 0, 0]); - d.global_blocks.push(odd); - let mut copies = vec![padded_block(*b"LMsk", d.global_layer_mask.as_ref().unwrap().data.clone())]; - for b in &d.global_blocks { - let mut b = b.clone(); - if b.padding.is_none() { - b.padding = Some(vec![0; (4 - b.data.len() % 4) % 4]); - } - copies.push(b); - } - let mut body = Vec::new(); - crate::tagged::write_blocks(&mut body, &copies, version, 2).unwrap(); - let signature = if version.is_psb() { SIGNATURE_PSB } else { SIGNATURE }; - assert_eq!(d.to_bytes(ByteOrder::Big).unwrap().0, [signature, &body[..]].concat()); - let (little, _) = transcode_blocks(&body, version, ByteOrder::Big, ByteOrder::Little).unwrap(); - assert_eq!(d.to_bytes(ByteOrder::Little).unwrap().0, [signature, &little[..]].concat()); - } - } - - /// Nested structures are transcoded into one shared buffer. A block whose body fails - /// part-way leaves no partial bytes: dropped on the way out (with the enclosing lengths - /// patched as if it had never been there), or kept exactly as stored on the way in. - #[test] - fn a_nested_block_that_fails_part_way_leaves_nothing_behind() { - let clean = sample_layers(); - let mut fx = Vec::new(); - fx.put_u32(0); - fx.extend_from_slice(&VersionedDescriptor::new(Descriptor::new("null").with("Opct", Value::Double(1.0))).to_bytes()); - // A whole number of 4-byte units, so dropping it cannot change any padding. - fx.resize(fx.len().next_multiple_of(4), 0); - let mut d = clean.clone(); - d.layer_info.as_mut().unwrap().layers[0].blocks.insert(0, TaggedBlock::new(*b"lfx2", fx.clone())); - let (mut little, w) = d.to_bytes(ByteOrder::Little).unwrap(); - assert!(!w.iter().any(|w| w.starts_with("lfx2")), "{w:?}"); - // An unknown value type after the descriptor's header: parsing fails part-way. - let at = little.windows(4).position(|w| w == b"buod").unwrap(); - little[at..at + 4].copy_from_slice(b"????"); - let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); - assert_eq!(w.iter().filter(|w| w.starts_with("lfx2 block")).count(), 1, "{w:?}"); - let kept = &back.layer_info.as_ref().unwrap().layers[0].blocks[0]; - assert!(kept.is_foreign_order()); - let stored = little.windows(4).position(|w| w == b"2xfl").unwrap() + 8; - assert_eq!(kept.data, little[stored..stored + fx.len()]); - // Big-endian data that fails part-way cannot go into a little-endian file: dropped. - let mut big_bad = d.clone(); - let mut broken = fx.clone(); - let at = broken.windows(4).position(|w| w == b"doub").unwrap(); - broken[at..at + 4].copy_from_slice(b"????"); - big_bad.layer_info.as_mut().unwrap().layers[0].blocks[0].data = broken; - let (little, w) = big_bad.to_bytes(ByteOrder::Little).unwrap(); - assert_eq!(w.iter().filter(|w| w.starts_with("lfx2 block")).count(), 1, "{w:?}"); - assert_eq!(little, clean.to_bytes(ByteOrder::Little).unwrap().0); - } -} +mod tests; diff --git a/crates/psd/src/tiff/tests.rs b/crates/psd/src/tiff/tests.rs new file mode 100644 index 0000000000..3836822bf4 --- /dev/null +++ b/crates/psd/src/tiff/tests.rs @@ -0,0 +1,1012 @@ +use super::transcode::*; +use super::*; +use crate::compression::Compression; +use crate::descriptor::{Descriptor, Id, UnicodeString, Value, VersionedDescriptor}; +use crate::header::ColorMode; +use crate::testgen; + +/// Keys the generator plants that the transcoder (rightly) does not know. +const UNKNOWN: [&[u8; 4]; 3] = [b"zOdd", b"zNop", b"Txt2"]; + +/// `d` without the blocks a little-endian round trip drops, and without the layer-info +/// padding (which follows the body length, so it changes when blocks are dropped). +fn known(d: &ImageSourceData) -> ImageSourceData { + let mut d = d.clone(); + d.byte_order = ByteOrder::Big; + d.global_blocks.retain(|b| !UNKNOWN.contains(&&b.key)); + if d.layer_info.is_none() { + d.layer_info_key = *b"Layr"; + } + if let Some(info) = &mut d.layer_info { + info.padding = None; + for l in &mut info.layers { + l.blocks.retain(|b| !UNKNOWN.contains(&&b.key)); + } + } + d +} + +fn roundtrip(d: &ImageSourceData) -> ImageSourceData { + let (big, w) = d.to_bytes(ByteOrder::Big).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(w.iter().all(|w| UNKNOWN.iter().any(|k| w.starts_with(std::str::from_utf8(*k).unwrap()))), "{w:?}"); + if w.is_empty() { + assert_eq!(big.len(), little.len()); + } + if big.len() > 36 { + assert_ne!(big, little); + assert!(little[36..].starts_with(b"MIB8")); + } + let (back_big, w) = ImageSourceData::from_bytes(&big).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let (back_little, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + if little.len() > 36 { + assert_eq!(back_little.byte_order, ByteOrder::Little); + } + assert_eq!(known(&back_little), known(&back_big)); + // The little-endian bytes are a fixed point (from the second generation on when blocks + // were dropped, since the layer-info padding then follows the shorter body). + let (little2, _) = back_little.to_bytes(ByteOrder::Little).unwrap(); + let little3 = ImageSourceData::from_bytes(&little2).unwrap().0.to_bytes(ByteOrder::Little).unwrap().0; + assert_eq!(little3, little2); + back_big +} + +#[test] +fn generated_files_survive_both_orders() { + for case in testgen::all_cases() { + let d = ImageSourceData::from_psd(&case.file); + let back = roundtrip(&d); + assert_eq!(back.version, d.version, "{}", case.name); + assert_eq!(known(&back), known(&d), "{}", case.name); + // Nothing but the planted unknown blocks is lost on the little-endian path. + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(known(&back), known(&d), "{}", case.name); + } +} + +#[test] +fn same_order_transcoding_is_the_identity() { + for case in testgen::all_cases() { + let d = ImageSourceData::from_psd(&case.file); + let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); + let (same, w) = transcode_blocks(&big[36..], d.version, ByteOrder::Big, ByteOrder::Big).unwrap(); + assert!(w.is_empty()); + assert_eq!(same, big[36..].to_vec(), "{}", case.name); + } +} + +fn sample_layers() -> ImageSourceData { + let file = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); + ImageSourceData::from_psd(&file) +} + +#[test] +fn signature_selects_the_version() { + let mut d = sample_layers(); + let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); + assert!(b.starts_with(SIGNATURE)); + d.version = Version::Psb; + let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); + assert!(b.starts_with(SIGNATURE_PSB)); + assert_eq!(ImageSourceData::from_bytes(&b).unwrap().0.version, Version::Psb); + roundtrip(&d); +} + +#[test] +fn layer_key_follows_depth() { + assert_eq!(layer_info_key(8), *b"Layr"); + assert_eq!(layer_info_key(16), *b"Lr16"); + assert_eq!(layer_info_key(32), *b"Lr32"); + for (depth, comp) in [(16, Compression::ZipPrediction), (32, Compression::ZipPrediction), (16, Compression::Rle)] { + let file = testgen::layered(Version::Psd, ColorMode::Rgb, depth, comp); + let d = ImageSourceData::from_psd(&file); + assert_eq!(d.layer_info_key, layer_info_key(depth)); + let back = roundtrip(&d); + assert_eq!(back.layer_info_key, d.layer_info_key); + // The 16/32-bit model goes back into a PSD as a global block, like Photoshop's. + let psd = back.into_psd(file.header.clone(), file.resources.clone(), file.image_data.clone()); + assert!(matches!(psd.layer_info_placement, LayerInfoPlacement::GlobalBlock { .. })); + let again = PsdFile::from_bytes(&psd.to_bytes().unwrap()).unwrap(); + assert_eq!(known(&ImageSourceData::from_psd(&again)).layer_info, known(&d).layer_info); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(known(&back).layer_info, known(&d).layer_info, "depth {depth}"); + } +} + +#[test] +fn blocks_are_padded_to_four() { + let d = sample_layers(); + let (b, _) = d.to_bytes(ByteOrder::Big).unwrap(); + let mut at = 36; + while at < b.len() { + assert_eq!(&b[at..at + 4], b"8BIM"); + let len = u32::from_be_bytes(b[at + 8..at + 12].try_into().unwrap()) as usize; + at += 12 + len; + at += (4 - at % 4) % 4; + } + assert_eq!(at, b.len()); +} + +fn block_rt(key: [u8; 4], data: Vec) -> (Vec, Vec) { + let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(key, data.clone())], ..Default::default() }; + let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(back.global_blocks[0].data, data); + assert_eq!(back.to_bytes(ByteOrder::Big).unwrap().0, big); + (big, little) +} + +#[test] +fn descriptor_blocks_swap_keys_and_numbers() { + let mut desc = Descriptor::new("null"); + desc.items.push((Id::new("Opct"), Value::UnitFloat { unit: *b"#Prc", value: 75.5 })); + desc.items.push((Id::new("Nm "), Value::Text(UnicodeString::new_nul("Drop Shadow")))); + desc.items.push((Id::new("enab"), Value::Boolean(true))); + desc.items.push((Id::new("Md "), Value::Enumerated { type_id: Id::new("BlnM"), value: Id::new("Mltp") })); + desc.items.push((Id::new("list"), Value::List(vec![Value::Integer(-3), Value::Double(1.5), Value::LargeInteger(7)]))); + desc.items.push((Id::new("longKeyName"), Value::Descriptor(Descriptor::new("innr")))); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor::new(desc).to_bytes()); + let (big, little) = block_rt(*b"lfx2", data); + // Keys are reversed and the version field swapped in the little-endian bytes. + assert!(little[36..].starts_with(b"MIB82xfl")); + let body_at = 48; + assert_eq!(&little[body_at..body_at + 4], &[0, 0, 0, 0]); + assert_eq!(&little[body_at + 4..body_at + 8], &[16, 0, 0, 0]); + assert_eq!(&big[body_at + 4..body_at + 8], &[0, 0, 0, 16]); + assert!(little.windows(4).any(|w| w == b"crP#"), "unit key reversed"); + assert!(little.windows(11).any(|w| w == b"longKeyName"), "string ids are not reversed"); +} + +#[test] +fn descriptor_references_by_identifier_and_index_keep_their_class() { + use crate::descriptor::{Class, ReferenceItem}; + let class = || Class { name: UnicodeString::new(""), class_id: Id::new("Lyr ") }; + let desc = Descriptor::new("null") + .with("null", Value::Reference(vec![ReferenceItem::Identifier { class: class(), id: 7 }, ReferenceItem::Index { class: class(), index: 2 }])); + let mut data = 0u32.to_be_bytes().to_vec(); + data.extend(VersionedDescriptor::new(desc).to_bytes()); + block_rt(*b"lfx2", data); +} + +fn type_layer_data() -> Vec { + let mut data = Vec::new(); + data.put_u16(1); + for value in [1.5, 0.0, -0.25, 1.75, 12.5, -3.25] { + data.put_f64(value); + } + data.put_u16(50); + let text = Descriptor::new("TxLr").with("Txt ", Value::Text(UnicodeString::new_nul("Synthetic Ω 🦀"))); + data.extend(VersionedDescriptor::new(text).to_bytes()); + data.put_u16(1); + data.extend(VersionedDescriptor::new(Descriptor::new("warp")).to_bytes()); + for value in [11, -22, 333, 4_444] { + data.put_i32(value); + } + data +} + +fn tagged_payload<'a>(bytes: &'a [u8], signature_and_key: &[u8; 8], little: bool) -> &'a [u8] { + let block_at = bytes.windows(8).position(|window| window == signature_and_key).expect("tagged block"); + let length_at = block_at + 8; + let length_bytes: [u8; 4] = bytes.get(length_at..length_at + 4).expect("tagged block length").try_into().expect("four-byte length"); + let length = if little { u32::from_le_bytes(length_bytes) } else { u32::from_be_bytes(length_bytes) }; + let data_at = length_at + 4; + let data_end = data_at + usize::try_from(length).expect("test block length fits usize"); + bytes.get(data_at..data_end).expect("tagged block payload") +} + +fn legacy_little_endian_type_block() -> Vec { + let mut tysh = Vec::new(); + tysh.extend_from_slice(&1u16.to_le_bytes()); + for value in [1.5f64, 0.0, -0.25, 1.75, 12.5, -3.25] { + tysh.extend_from_slice(&value.to_le_bytes()); + } + tysh.extend_from_slice(&50u16.to_le_bytes()); + // Independently encode a little-endian `VersionedDescriptor` with a single TEXT value. + tysh.extend_from_slice(&16u32.to_le_bytes()); + tysh.extend_from_slice(&0u32.to_le_bytes()); // empty descriptor name + tysh.extend_from_slice(&0u32.to_le_bytes()); // four-character class id + tysh.extend_from_slice(b"rLxT"); // little-endian four-character class id + tysh.extend_from_slice(&1u32.to_le_bytes()); // one item + tysh.extend_from_slice(&0u32.to_le_bytes()); // four-character key + tysh.extend_from_slice(b" txT"); // little-endian four-character key + tysh.extend_from_slice(b"TXET"); // little-endian OSType + let mut units: Vec = "Synthetic Ω 🦀".encode_utf16().collect(); + units.push(0); // `new_nul`, matching the canonical fixture + tysh.extend_from_slice(&u32::try_from(units.len()).expect("short test string").to_le_bytes()); + for unit in units { + tysh.extend_from_slice(&unit.to_le_bytes()); + } + tysh.extend_from_slice(&1u16.to_le_bytes()); + // Empty warp descriptor, also encoded independently in little-endian order. + tysh.extend_from_slice(&16u32.to_le_bytes()); + tysh.extend_from_slice(&0u32.to_le_bytes()); // empty descriptor name + tysh.extend_from_slice(&4u32.to_le_bytes()); // `warp` is a four-byte string id + tysh.extend_from_slice(b"warp"); + tysh.extend_from_slice(&0u32.to_le_bytes()); // no items + for value in [11i32, -22, 333, 4_444] { + tysh.extend_from_slice(&value.to_le_bytes()); + } + + let mut image_source = super::SIGNATURE.to_vec(); + image_source.extend_from_slice(b"MIB8hSyT"); + image_source.extend_from_slice(&u32::try_from(tysh.len()).expect("short test block").to_le_bytes()); + image_source.extend_from_slice(&tysh); + let padding = (4 - tysh.len() % 4) % 4; + image_source.resize(image_source.len() + padding, 0); + image_source +} + +#[test] +fn type_block_write_keeps_text_descriptor_in_psd_order() { + let canonical = type_layer_data(); + let source = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"TySh", canonical.clone())], ..Default::default() }; + + let (little, warnings) = source.to_bytes(ByteOrder::Little).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + let payload = tagged_payload(&little, b"MIB8hSyT", true); + assert_eq!(payload.get(..2), Some(&[1, 0][..]), "TySh header stays in TIFF order"); + for (index, expected) in [1.5f64, 0.0, -0.25, 1.75, 12.5, -3.25].into_iter().enumerate() { + let start = 2 + index * 8; + let bytes: [u8; 8] = payload.get(start..start + 8).unwrap().try_into().unwrap(); + assert_eq!(f64::from_le_bytes(bytes), expected, "matrix element {index}"); + } + + let (text_descriptor, descriptor_len) = VersionedDescriptor::parse_prefix(canonical.get(52..).unwrap()).unwrap(); + let text_section_len = 2 + descriptor_len; + assert_eq!(payload.get(50..52), Some(&50u16.to_be_bytes()[..])); + assert_eq!(payload.get(52..56), Some(&16u32.to_be_bytes()[..])); + assert_eq!(payload.get(50..50 + text_section_len), canonical.get(50..50 + text_section_len)); + assert!(matches!(text_descriptor.descriptor.get("Txt "), Some(Value::Text(value)) if value.to_string_lossy() == "Synthetic Ω 🦀")); + let text_descriptor_from_output = VersionedDescriptor::parse_prefix(payload.get(52..).unwrap()).unwrap().0; + assert!(matches!(text_descriptor_from_output.descriptor.get("Txt "), Some(Value::Text(value)) if value.to_string_lossy() == "Synthetic Ω 🦀")); + + let warp_at = 50 + text_section_len; + assert_eq!(payload.get(warp_at..warp_at + 2), Some(&1u16.to_le_bytes()[..])); + let (_, warp_len) = VersionedDescriptor::parse_prefix(canonical.get(warp_at + 2..).unwrap()).unwrap(); + let warp_descriptor_at = warp_at + 2; + assert_eq!(payload.get(warp_descriptor_at..warp_descriptor_at + 4), Some(&16u32.to_le_bytes()[..])); + assert_eq!(payload.get(warp_descriptor_at + 4..warp_descriptor_at + 8), Some(&0u32.to_le_bytes()[..])); + assert_eq!(payload.get(warp_descriptor_at + 8..warp_descriptor_at + 12), Some(&4u32.to_le_bytes()[..])); + assert_eq!(payload.get(warp_descriptor_at + 12..warp_descriptor_at + 16), Some(b"warp".as_slice())); + assert_eq!(payload.get(warp_descriptor_at + 16..warp_descriptor_at + 20), Some(&0u32.to_le_bytes()[..])); + let bounds_at = warp_descriptor_at + warp_len; + for (index, expected) in [11i32, -22, 333, 4_444].into_iter().enumerate() { + let start = bounds_at + index * 4; + assert_eq!(payload.get(start..start + 4), Some(&expected.to_le_bytes()[..]), "bound {index}"); + } + + let (big, warnings) = source.to_bytes(ByteOrder::Big).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(tagged_payload(&big, b"8BIMTySh", false), canonical); + + let (reopened, warnings) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(reopened.global_blocks[0].data, canonical); + assert_eq!(reopened.to_bytes(ByteOrder::Big).unwrap().0, big); +} + +#[test] +fn legacy_little_endian_type_descriptor_still_imports() { + let input = legacy_little_endian_type_block(); + let (reopened, warnings) = ImageSourceData::from_bytes(&input).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert_eq!(reopened.global_blocks[0].data, type_layer_data()); +} + +#[test] +fn type_block_layout() { + let mut data = Vec::new(); + data.put_u16(1); + for v in [1.0, 0.0, 0.0, 1.0, 12.5, -3.0] { + data.put_f64(v); + } + data.put_u16(50); + data.extend(VersionedDescriptor::new(Descriptor::new("TxLr")).to_bytes()); + data.put_u16(1); + data.extend(VersionedDescriptor::new(Descriptor::new("warp")).to_bytes()); + for v in [0.0, 0.0, 100.0, 20.0] { + data.put_f64(v); + } + let (_, little) = block_rt(*b"TySh", data); + assert!(little[48..].starts_with(&[1, 0])); + assert_eq!(&little[50..58], &1.0f64.to_le_bytes()); +} + +#[test] +fn photoshop_type_block_switches_to_big_endian_after_its_matrix() { + let mut data = Vec::new(); + data.put_u16(1); + for v in [1.8054202887117468, 0.0, -0.143, 1.8063853899082987, 247.25, 2864.5] { + data.put_f64(v); + } + data.put_u16(50); + let text = Descriptor::new("TxLr").with("Txt ", Value::Text(UnicodeString::new_nul("Synthetic headline"))); + data.extend(VersionedDescriptor::new(text).to_bytes()); + data.put_u16(1); + data.extend(VersionedDescriptor::new(Descriptor::new("warp")).to_bytes()); + for v in [0, 0, 120, 30] { + data.put_i32(v); + } + let (_, mut little) = block_rt(*b"TySh", data.clone()); + // Photoshop uses big-endian text but returns to little-endian for warp/bounds. + let (_, used) = VersionedDescriptor::parse_prefix(&data[52..]).unwrap(); + little[98..100 + used].copy_from_slice(&data[50..52 + used]); + let (back, warnings) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(warnings.is_empty(), "{warnings:?}"); + assert!(!back.global_blocks[0].is_foreign_order()); + assert_eq!(back.global_blocks[0].data, data); + // The normalized model remains writable in either order. + for order in [ByteOrder::Big, ByteOrder::Little] { + let (bytes, warnings) = back.to_bytes(order).unwrap(); + assert!(warnings.is_empty()); + let (again, warnings) = ImageSourceData::from_bytes(&bytes).unwrap(); + assert!(warnings.is_empty()); + assert_eq!(again.global_blocks[0].data, data); + } +} + +#[test] +fn legacy_effects_and_adjustments() { + // lrFX with a drop shadow and a solid fill. + let mut fx = Vec::new(); + fx.put_u16(0); + fx.put_u16(3); + fx.put(b"8BIMcmnS"); + fx.put_u32(7); + fx.put_u32(0); + fx.put_u8(1); + fx.put_u16(0); + fx.put(b"8BIMdsdw"); + let mut sh = Vec::new(); + sh.put_u32(2); + for v in [5 << 16, 75 << 16, 120 << 16, 7 << 16] { + sh.put_i32(v); + } + for _ in 0..5 { + sh.put_u16(0); + } + sh.put(b"8BIMmul "); + sh.put_u8(1); + sh.put_u8(1); + sh.put_u8(191); + sh.put_u16(0); + sh.put_u16(0x1234); + sh.put_u16(0); + sh.put_u16(0); + sh.put_u16(0); + fx.put_u32(sh.len() as u32); + fx.put(&sh); + fx.put(b"8BIMsofi"); + let mut so = Vec::new(); + so.put_u32(2); + so.put(b"8BIMnorm"); + so.put_u16(0); + so.put_u16(0xffff); + so.put_u16(0); + so.put_u16(0); + so.put_u16(0); + so.put_u8(255); + so.put_u8(1); + so.put_u16(0); + so.put_u16(0xffff); + so.put_u16(0); + so.put_u16(0); + so.put_u16(0); + fx.put_u32(so.len() as u32); + fx.put(&so); + let (_, little) = block_rt(*b"lrFX", fx); + assert!(little.windows(8).any(|w| w == b"MIB8wdsd"), "effect keys reversed"); + assert!(little.windows(2).any(|w| w == [0x34, 0x12]), "colour component swapped"); + + // levl: version + 29 records + the Lvls extension. + let mut lv = Vec::new(); + lv.put_u16(2); + for i in 0..29 * 5 { + lv.put_u16(i as u16); + } + lv.put(b"Lvls"); + lv.put_u16(3); + lv.put_u16(33); + for i in 0..4 * 5 { + lv.put_u16(1000 + i as u16); + } + let (_, little) = block_rt(*b"levl", lv); + assert!(little.windows(4).any(|w| w == b"slvL")); + + // curv with the Crv extension. + let mut cv = vec![0u8]; + cv.put_u16(1); + cv.put_u32(0b101); + cv.put_u16(2); + cv.put_u16(0); + cv.put_u16(0); + cv.put_u16(255); + cv.put_u16(255); + cv.put_u16(1); + cv.put_u16(128); + cv.put_u16(100); + cv.put(b"Crv "); + cv.put_u16(4); + cv.put_u32(1); + cv.put_u16(2); + cv.put_u16(1); + cv.put_u16(7); + cv.put_u16(9); + block_rt(*b"curv", cv); + + for (key, data) in [ + (*b"hue2", vec![0, 2, 0, 0, 0, 10, 0, 20, 0, 30, 0, 40, 0, 50, 0, 60]), + (*b"brit", vec![0, 10, 0, 20, 0, 30, 1, 0]), + (*b"thrs", vec![0, 128, 0, 0]), + (*b"post", vec![0, 4, 0, 0]), + (*b"nvrt", vec![]), + (*b"expA", [1u16.to_be_bytes().to_vec(), 0.5f32.to_be_bytes().to_vec(), 0.0f32.to_be_bytes().to_vec(), 1.0f32.to_be_bytes().to_vec()].concat()), + (*b"blnc", vec![0; 20]), + (*b"mixr", vec![0, 1, 0, 0, 0, 100, 0, 0, 0, 0, 0, 0, 0, 0]), + (*b"selc", vec![0, 1, 0, 0, 0, 5, 0, 6, 0, 7, 0, 8]), + (*b"phfl", vec![0, 3, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 25, 1, 0]), + (*b"phfl", vec![0, 2, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 25, 1, 0]), + (*b"lclr", vec![0, 1, 0, 0, 0, 0, 0, 0]), + (*b"fxrp", [1.5f64.to_be_bytes().to_vec(), 2.5f64.to_be_bytes().to_vec()].concat()), + (*b"brst", vec![0, 0, 0, 1, 0, 0, 0, 2]), + (*b"FMsk", vec![0, 0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 50]), + (*b"lsct", [3u32.to_be_bytes().to_vec(), b"8BIMpass".to_vec()].concat()), + (*b"lspf", vec![0, 0, 0, 5]), + (*b"lnsr", b"layr".to_vec()), + (*b"knko", vec![1, 0, 0, 0]), + (*b"knko", vec![2, 0, 0, 0]), + (*b"infx", vec![1, 0, 0, 0]), + (*b"clbl", vec![0, 0, 0, 0]), + (*b"tsly", vec![0, 0, 0, 0]), + (*b"lmgm", vec![1, 0, 0, 0]), + (*b"vmgm", vec![1, 0, 0, 0]), + (*b"iOpa", vec![128, 0, 0, 0]), + ] { + let (big, little) = block_rt(key, data.clone()); + if data.len() >= 2 && key != *b"nvrt" { + assert_ne!(big, little, "{}", String::from_utf8_lossy(&key)); + } + } +} + +#[test] +fn global_layer_mask_block() { + let d = ImageSourceData { global_layer_mask: Some(GlobalLayerMask { data: vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 128, 0] }), ..Default::default() }; + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(w.is_empty()); + assert!(little[36..].starts_with(b"MIB8ksML")); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(back.global_layer_mask, d.global_layer_mask); + assert_eq!(back.global_layer_mask.unwrap().opacity(), Some(50)); +} + +#[test] +fn gradient_map_layout() { + let mut g = Vec::new(); + g.put_u16(3); + g.put_u8(0); + g.put_u8(1); + g.put(b"Lnr "); + g.put_u32(2); + g.put_u16(u16::from(b'a')); + g.put_u16(0); + g.put_u16(2); + for (loc, mid) in [(0u32, 50u32), (4096, 50)] { + g.put_u32(loc); + g.put_u32(mid); + g.put_u16(0); + g.put_u16(0x1111); + g.put_u16(0x2222); + g.put_u16(0x3333); + g.put_u16(0); + g.put_u16(0); + } + g.put_u16(2); + for loc in [0u32, 4096] { + g.put_u32(loc); + g.put_u32(50); + g.put_u16(100); + } + g.put_u16(2); + g.put_u16(1); + g.put_u16(32); + g.put_u16(0); + g.put_u32(0xdead_beef); + for _ in 0..12 { + g.put_u16(0); + } + let (_, little) = block_rt(*b"grdm", g); + assert!(little.windows(4).any(|w| w == 0xdead_beefu32.to_le_bytes())); + assert!(little.windows(4).any(|w| w == b" rnL")); +} + +#[test] +fn vector_mask_records() { + let mut v = Vec::new(); + v.put_u32(3); + v.put_u32(0); + // Closed length record, then two linked knots. + v.put_u16(0); + v.put_u16(2); + v.put_i16(1); + v.put(&[0; 20]); + for sel in [1u16, 1] { + v.put_u16(sel); + for i in 0..6 { + v.put_i32((i + 1) * 0x0010_0000); + } + } + let (_, little) = block_rt(*b"vmsk", v); + assert!(little.windows(4).any(|w| w == [0, 0, 0x10, 0]), "fixed-point coordinate swapped"); +} + +#[test] +fn pattern_block() { + let plane: Vec = (0..16u8).collect(); + let p = crate::patterns::PsdPattern { + mode: 3, + width: 4, + height: 4, + name: "dots".into(), + id: "abc".into(), + palette: None, + depth: 8, + channels: vec![plane.clone(), plane.clone(), plane.clone()], + alpha: Some(plane), + }; + let data = crate::patterns::write_pattern_block(std::slice::from_ref(&p)).unwrap(); + block_rt(*b"Patt", data.clone()); + // Little-endian pattern data parses back into the same pattern. + let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"Patt", data)], ..Default::default() }; + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(crate::patterns::parse_pattern_block(&back.global_blocks[0].data).unwrap(), vec![p]); +} + +#[test] +fn filter_effects_block() { + use crate::filter_effects::{EffectsPlane, FilterEffects, FilterEffectsItem}; + let plane = EffectsPlane::encode(Compression::Rle, &[1, 2, 3, 4, 5, 6], 3, 2, 8).unwrap(); + let mask = EffectsPlane::encode(Compression::Raw, &[9; 6], 3, 2, 8).unwrap(); + let rect = crate::layer::Rect { top: 0, left: 0, bottom: 2, right: 3 }; + let fx = FilterEffects { + version: 1, + items: vec![FilterEffectsItem { + id: "x".into(), + version: 1, + rect, + depth: 8, + max_channels: 1, + slots: vec![Some(plane.clone()), None, Some(plane)], + mask: Some((rect, mask)), + }], + }; + let data = fx.to_bytes(); + block_rt(*b"FEid", data.clone()); + let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"FEid", data)], ..Default::default() }; + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(FilterEffects::parse(&back.global_blocks[0].data).unwrap(), fx); +} + +/// Photoshop writes one `FEid` section per smart object: every section's lengths and +/// numbers are swapped, not only the first one's. +#[test] +fn filter_effects_block_swaps_every_section() { + use crate::filter_effects::{EffectsPlane, FilterEffects, FilterEffectsItem}; + let rect = crate::layer::Rect { top: 0, left: 0, bottom: 2, right: 3 }; + let item = |id: &str, v: u8| FilterEffectsItem { + id: id.into(), + version: 1, + rect, + depth: 8, + max_channels: 1, + slots: vec![Some(EffectsPlane::encode(Compression::Raw, &[v; 6], 3, 2, 8).unwrap()), None, None], + mask: None, + }; + let fx = FilterEffects { version: 1, items: vec![item("first", 1), item("second-object", 2)] }; + let data = fx.to_bytes(); + let len1 = u64::from_be_bytes(data[4..12].try_into().unwrap()) as usize; + let second = 12 + len1 + (4 - len1 % 4) % 4; + let len2 = &data[second..second + 8]; + block_rt(*b"FEid", data.clone()); + let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"FEid", data.clone())], ..Default::default() }; + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + // 36-byte signature, then `MIB8`, the reversed key and a u32 length: the block data. + let body = &little[36 + 12..]; + let mut swapped = len2.to_vec(); + swapped.reverse(); + assert_eq!(&body[second..second + 8], &swapped[..], "second section's length is little-endian"); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(FilterEffects::parse(&back.global_blocks[0].data).unwrap(), fx); +} + +#[test] +fn metadata_setting_items() { + let desc = VersionedDescriptor::new(Descriptor::new("null")).to_bytes(); + let items = vec![crate::metadata::MetadataItem::new(*b"cmls", desc)]; + block_rt(*b"shmd", crate::metadata::write_shmd(&items)); + // An unknown item cannot be converted: it is dropped on its own, the rest stays. + let items = vec![ + crate::metadata::MetadataItem::new(*b"zzzz", vec![1, 2, 3, 4, 5, 6, 7, 8]), + crate::metadata::MetadataItem::new(*b"mdyn", vec![0, 0, 0, 1]), + crate::metadata::MetadataItem::new(*b"cmls", VersionedDescriptor::new(Descriptor::new("null")).to_bytes()), + ]; + let d = ImageSourceData { global_blocks: vec![TaggedBlock::new(*b"shmd", crate::metadata::write_shmd(&items))], ..Default::default() }; + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert_eq!(w.len(), 1, "{w:?}"); + assert!(w[0].starts_with("shmd item zzzz dropped"), "{}", w[0]); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty()); + let back_items = crate::metadata::parse_shmd(&back.global_blocks[0].data).unwrap(); + assert_eq!(back_items, items[1..].to_vec()); +} + +#[test] +fn unknown_descriptor_shaped_blocks_are_converted() { + // A key the transcoder has never heard of, holding a versioned descriptor (as every + // block Photoshop added since CS6 does): converted, not dropped. + let mut desc = Descriptor::new("null"); + desc.items.push((Id::new("Nm "), Value::Text(UnicodeString::new_nul("x")))); + let data = VersionedDescriptor::new(desc.clone()).to_bytes(); + block_rt(*b"Qqqq", data); + let mut with_version = 3u32.to_be_bytes().to_vec(); + with_version.extend(VersionedDescriptor::new(desc).to_bytes()); + block_rt(*b"Qqqq", with_version); + // Empty data needs no conversion either. + block_rt(*b"Mt32", Vec::new()); +} + +#[test] +fn linked_layer_items() { + // One embedded file (liFD, version 7) as photocraft-io writes it. + let mut item = Vec::new(); + item.put(b"liFD"); + item.put_u32(7); + item.put_u8(4); + item.put(b"uuid"); + let units: Vec = "a.png\0".encode_utf16().collect(); + item.put_u32(units.len() as u32); + for u in units { + item.put_u16(u); + } + item.put(b"png "); + item.put(b"8BIM"); + let bytes = [0x89, b'P', b'N', b'G', 1, 2, 3]; + item.put_u64(bytes.len() as u64); + item.put_u8(0); + item.put(&bytes); + item.put_u32(0); + item.put_f64(0.0); + item.put_u8(0); + let mut data = (item.len() as u64).to_be_bytes().to_vec(); + data.put(&item); + while !data.len().is_multiple_of(4) { + data.push(0); + } + let (_, little) = block_rt(*b"lnk2", data); + assert!(little.windows(4).any(|w| w == b"DFil"), "item type key reversed"); + assert!(little.windows(7).any(|w| w == bytes), "file bytes untouched"); +} + +#[test] +fn annotation_block() { + let mut a = Vec::new(); + a.put_u16(2); + a.put_u16(1); + a.put_u32(1); + let mut item = Vec::new(); + item.put(b"txtA"); + item.put_u8(0); + item.put_u8(1); + item.put_u16(0); + for v in [10, 20, 30, 40, 50, 60, 70, 80] { + item.put_i32(v); + } + item.put_u16(0); + for v in [0xffff, 0, 0, 0] { + item.put_u16(v); + } + for s in [&b"me"[..], b"note", b"2026"] { + item.put_u8(s.len() as u8); + item.put(s); + if (1 + s.len()) % 2 == 1 { + item.put_u8(0); + } + } + let text = [0xfe, 0xff, 0, b'h', 0, b'i']; + let mut d = Vec::new(); + d.put(b"txtC"); + d.put_u32(text.len() as u32); + d.put(&text); + item.put_u32(d.len() as u32 + 4); + item.put(&d); + a.put_u32(item.len() as u32 + 4); + a.put(&item); + let (_, little) = block_rt(*b"Anno", a); + assert!(little.windows(4).any(|w| w == b"Atxt")); + assert!(little.windows(6).any(|w| w == text), "text kept as stored"); +} + +#[test] +fn unknown_blocks_kept_in_big_dropped_in_little() { + let mut d = sample_layers(); + d.global_blocks.push(TaggedBlock::new(*b"Zzzz", vec![1, 2, 3, 4, 5])); + let (big, w) = d.to_bytes(ByteOrder::Big).unwrap(); + assert!(w.is_empty()); + let (back, w) = ImageSourceData::from_bytes(&big).unwrap(); + assert!(w.is_empty()); + assert_eq!(back.global_blocks, d.global_blocks); + // Big-endian data of unknown layout cannot go into a little-endian file. + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert_eq!(w.iter().filter(|w| w.starts_with("Zzzz block (5 bytes) dropped")).count(), 1, "{w:?}"); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty()); + assert_eq!(back.global_blocks.iter().map(|b| b.key).collect::>(), vec![*b"Patt", *b"FMsk"]); + assert_eq!(known(&back).layer_info, known(&d).layer_info); +} + +#[test] +fn unknown_blocks_from_a_little_endian_file_are_kept_with_their_byte_order() { + let d = sample_layers(); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + // A little-endian file with a block of unknown layout, among known ones. + let mut raw = little.clone(); + raw.extend_from_slice(b"MIB8zzzZ"); + raw.extend_from_slice(&6u32.to_le_bytes()); + raw.extend_from_slice(&[9, 8, 7, 6, 5, 4, 0, 0]); + raw.extend_from_slice(b"MIB8diyl"); + raw.extend_from_slice(&4u32.to_le_bytes()); + raw.extend_from_slice(&7u32.to_le_bytes()); + let (back, w) = ImageSourceData::from_bytes(&raw).unwrap(); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(known(&back).layer_info, known(&d).layer_info); + let kept = back.global_blocks.iter().find(|b| &b.key == b"Zzzz").expect("kept"); + assert!(kept.is_foreign_order()); + assert_eq!(kept.signature, *b"MIB8"); + assert_eq!(kept.data, [9, 8, 7, 6, 5, 4]); + // The known block after it was still converted. + assert_eq!(back.global_blocks.iter().find(|b| &b.key == b"lyid").unwrap().data, 7u32.to_be_bytes()); + // Written back to a little-endian TIFF, the bytes are exactly what was read. + let (again, w) = back.to_bytes(ByteOrder::Little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let tail = &raw[raw.len() - 18 - 16..]; + assert!(again.windows(tail.len()).any(|x| x == tail), "foreign-order block restored verbatim"); + assert_eq!(ImageSourceData::from_bytes(&again).unwrap().0.global_blocks, back.global_blocks); + // The big-endian (PSD) model keeps the marker, so a PSD writer can tell it apart. + let (big, _) = back.to_bytes(ByteOrder::Big).unwrap(); + assert!(big.windows(8).any(|x| x == b"MIB8Zzzz")); + assert_eq!(ImageSourceData::from_bytes(&big).unwrap().0.global_blocks, back.global_blocks); +} + +/// A PSB keeps `Mt16`/`Mt32`/`Mtrn`/`Alph` blocks of unknown layout verbatim, and their +/// length field is 8 bytes (a PSB long key). Writing them into a little-endian TIFF must +/// read that width; the old code looked the key up after reversing it for the target +/// order, which never matches `PSB_LONG_KEYS`. +#[test] +fn psb_foreign_order_long_key_roundtrips() { + let d = ImageSourceData { + version: Version::Psb, + byte_order: ByteOrder::Little, + global_blocks: vec![TaggedBlock { signature: *b"MIB8", key: *b"Mt16", data: vec![9, 8, 7, 6, 5, 4], padding: None }], + ..Default::default() + }; + let (little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert!(w.is_empty(), "{w:?}"); + assert_eq!(back.global_blocks, d.global_blocks, "the 8-byte length and the bytes survive"); +} + +#[test] +fn rle_counts_follow_the_container_and_samples_do_not() { + let file = testgen::layered(Version::Psd, ColorMode::Rgb, 16, Compression::Rle); + let d = ImageSourceData::from_psd(&file); + let (big, _) = d.to_bytes(ByteOrder::Big).unwrap(); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + let d = known(&d); + let (a, b) = (d.layer_info.as_ref().unwrap(), back.layer_info.as_ref().unwrap()); + for (la, lb) in a.layers.iter().zip(&b.layers) { + for (ca, cb) in la.channels.iter().zip(&lb.channels) { + assert_eq!(ca, cb); + let (w, h) = la.channel_rect(ca.id).size().unwrap(); + assert_eq!(ca.decode(w, h, 16, Version::Psd).unwrap(), cb.decode(w, h, 16, Version::Psd).unwrap()); + } + } + // The encoded samples (after the count table) are identical bytes in both files, while + // the count table itself is swapped. + let (rec, ch) = a + .layers + .iter() + .flat_map(|l| l.channels.iter().filter(|c| c.id >= -1 && c.compression == Some(Compression::Rle)).map(move |c| (l, c))) + .find(|(l, c)| l.rect.height() > 1 && c.data.len() > l.rect.height() as usize * 2 + 8) + .unwrap(); + let table = rec.rect.height() as usize * 2; + let samples = &ch.data[table..]; + assert!(big.windows(samples.len()).any(|w| w == samples)); + assert!(little.windows(samples.len()).any(|w| w == samples)); + let counts = &ch.data[..table]; + let swapped: Vec = counts.chunks(2).flat_map(|c| [c[1], c[0]]).collect(); + assert!(little.windows(table + samples.len()).any(|w| w[..table] == swapped[..] && w[table..] == *samples)); +} + +#[test] +fn blending_ranges_are_bytes_in_either_order() { + let mut file = testgen::layered(Version::Psd, ColorMode::Rgb, 8, Compression::Rle); + let ranges: Vec = [0u8, 0, 255, 255].repeat(10); + let marked: Vec = (1u8..=40).collect(); + for (i, l) in file.layer_info.as_mut().unwrap().layers.iter_mut().enumerate() { + l.blending_ranges.data = if i == 0 { marked.clone() } else { ranges.clone() }; + } + let d = ImageSourceData::from_psd(&file); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + // Written verbatim (what Photoshop writes in an `II` TIFF), not swapped per u32. + assert!(little.windows(marked.len()).any(|w| w == marked)); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + for (a, b) in d.layer_info.as_ref().unwrap().layers.iter().zip(&back.layer_info.as_ref().unwrap().layers) { + assert_eq!(a.blending_ranges, b.blending_ranges); + } +} + +#[test] +fn psb_sized_data_uses_long_lengths_and_counts() { + let file = testgen::layered(Version::Psb, ColorMode::Cmyk, 8, Compression::Rle); + let d = ImageSourceData::from_psd(&file); + assert_eq!(d.version, Version::Psb); + let back = roundtrip(&d); + assert_eq!(back.layer_info, d.layer_info); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(little[36..].starts_with(b"MIB8")); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(known(&back).layer_info, known(&d).layer_info); +} + +#[test] +fn resources_both_orders() { + let res = vec![ + ImageResource::new(1005, vec![0, 72, 0, 0, 0, 1, 0, 1, 0, 72, 0, 0, 0, 1, 0, 1]), + ImageResource::new(1037, vec![0, 0, 0, 120]), + ImageResource::new(1039, vec![1, 2, 3]), + ImageResource { signature: *b"8BIM", id: 1026, name: b"grp".to_vec(), data: vec![0, 1, 0, 1, 0, 0] }, + ]; + let big = resources_to_bytes(&res).unwrap(); + let (back, w) = resources_from_bytes(&big).unwrap(); + assert!(w.is_empty()); + assert_eq!(back, res); + let (little, w) = transcode_resources(&big, ByteOrder::Big, ByteOrder::Little).unwrap(); + assert!(w.is_empty()); + assert!(little.starts_with(b"MIB8")); + assert_eq!(little.len(), big.len()); + assert_eq!(&little[4..6], &[0xed, 3]); + let (back, w) = resources_from_bytes(&little).unwrap(); + assert!(w.is_empty()); + assert_eq!(back, res); + // The ICC blob is untouched by the swap. + assert!(little.windows(3).any(|w| w == [1, 2, 3])); + // An odd trailing pad byte is tolerated. + let mut padded = big.clone(); + padded.push(0); + assert_eq!(resources_from_bytes(&padded).unwrap().0, res); +} + +#[test] +fn malformed_input_errors_without_panicking() { + assert!(ImageSourceData::from_bytes(b"").is_err()); + assert!(ImageSourceData::from_bytes(b"Adobe Photoshop Document Data Block").is_err()); + let mut bad = SIGNATURE.to_vec(); + bad.extend_from_slice(b"XXXXLayr"); + assert!(ImageSourceData::from_bytes(&bad).is_err()); + let d = sample_layers(); + for order in [ByteOrder::Big, ByteOrder::Little] { + let (bytes, _) = d.to_bytes(order).unwrap(); + for cut in (0..bytes.len()).step_by(7).chain(bytes.len().saturating_sub(8)..bytes.len()) { + let _ = ImageSourceData::from_bytes(&bytes[..cut]); + } + // Bit flips in the structure (not the signature). + for i in (36..bytes.len()).step_by(13) { + let mut b = bytes.clone(); + b[i] ^= 0x5a; + let _ = ImageSourceData::from_bytes(&b); + } + } + // A block that claims more data than exists. + let mut huge = SIGNATURE.to_vec(); + huge.extend_from_slice(b"8BIMluni"); + huge.extend_from_slice(&u32::MAX.to_be_bytes()); + assert!(ImageSourceData::from_bytes(&huge).is_err()); + assert!(resources_from_bytes(b"8BIM\x03\xed\x00").is_err()); +} + +#[test] +fn into_psd_roundtrips_through_the_psd_writer() { + let file = testgen::layered(Version::Psd, ColorMode::Grayscale, 8, Compression::Zip); + let d = ImageSourceData::from_psd(&file); + let (little, _) = d.to_bytes(ByteOrder::Little).unwrap(); + let (back, _) = ImageSourceData::from_bytes(&little).unwrap(); + let psd = back.into_psd(file.header.clone(), file.resources.clone(), file.image_data.clone()); + let bytes = psd.to_bytes().unwrap(); + let again = PsdFile::from_bytes(&bytes).unwrap(); + let expected = known(&ImageSourceData::from_psd(&file)); + assert_eq!(known(&ImageSourceData::from_psd(&again)).layer_info, expected.layer_info); + assert_eq!(again.global_blocks, expected.global_blocks); + assert_eq!(again.layer_tree().len(), file.layer_tree().len()); +} + +/// Global blocks are padded as they are written, not on a copy: the bytes are those of the +/// former path (each block cloned with explicit padding to 4, written with `write_blocks`). +#[test] +fn global_blocks_written_without_copies_keep_their_bytes() { + for version in [Version::Psd, Version::Psb] { + let mut d = ImageSourceData { version, global_layer_mask: Some(GlobalLayerMask { data: vec![0, 1, 0, 2, 0, 3, 0, 4, 9] }), ..Default::default() }; + for n in 0..8u8 { + d.global_blocks.push(TaggedBlock::new(*b"FMsk", (0..n).collect())); + } + let mut explicit = TaggedBlock::new(*b"FMsk", vec![1, 2, 3]); + explicit.padding = Some(vec![]); + d.global_blocks.push(explicit); + let mut odd = TaggedBlock::new(*b"FMsk", vec![1, 2]); + odd.padding = Some(vec![0, 0, 0, 0, 0]); + d.global_blocks.push(odd); + let mut copies = vec![padded_block(*b"LMsk", d.global_layer_mask.as_ref().unwrap().data.clone())]; + for b in &d.global_blocks { + let mut b = b.clone(); + if b.padding.is_none() { + b.padding = Some(vec![0; (4 - b.data.len() % 4) % 4]); + } + copies.push(b); + } + let mut body = Vec::new(); + crate::tagged::write_blocks(&mut body, &copies, version, 2).unwrap(); + let signature = if version.is_psb() { SIGNATURE_PSB } else { SIGNATURE }; + assert_eq!(d.to_bytes(ByteOrder::Big).unwrap().0, [signature, &body[..]].concat()); + let (little, _) = transcode_blocks(&body, version, ByteOrder::Big, ByteOrder::Little).unwrap(); + assert_eq!(d.to_bytes(ByteOrder::Little).unwrap().0, [signature, &little[..]].concat()); + } +} + +/// Nested structures are transcoded into one shared buffer. A block whose body fails +/// part-way leaves no partial bytes: dropped on the way out (with the enclosing lengths +/// patched as if it had never been there), or kept exactly as stored on the way in. +#[test] +fn a_nested_block_that_fails_part_way_leaves_nothing_behind() { + let clean = sample_layers(); + let mut fx = Vec::new(); + fx.put_u32(0); + fx.extend_from_slice(&VersionedDescriptor::new(Descriptor::new("null").with("Opct", Value::Double(1.0))).to_bytes()); + // A whole number of 4-byte units, so dropping it cannot change any padding. + fx.resize(fx.len().next_multiple_of(4), 0); + let mut d = clean.clone(); + d.layer_info.as_mut().unwrap().layers[0].blocks.insert(0, TaggedBlock::new(*b"lfx2", fx.clone())); + let (mut little, w) = d.to_bytes(ByteOrder::Little).unwrap(); + assert!(!w.iter().any(|w| w.starts_with("lfx2")), "{w:?}"); + // An unknown value type after the descriptor's header: parsing fails part-way. + let at = little.windows(4).position(|w| w == b"buod").unwrap(); + little[at..at + 4].copy_from_slice(b"????"); + let (back, w) = ImageSourceData::from_bytes(&little).unwrap(); + assert_eq!(w.iter().filter(|w| w.starts_with("lfx2 block")).count(), 1, "{w:?}"); + let kept = &back.layer_info.as_ref().unwrap().layers[0].blocks[0]; + assert!(kept.is_foreign_order()); + let stored = little.windows(4).position(|w| w == b"2xfl").unwrap() + 8; + assert_eq!(kept.data, little[stored..stored + fx.len()]); + // Big-endian data that fails part-way cannot go into a little-endian file: dropped. + let mut big_bad = d.clone(); + let mut broken = fx.clone(); + let at = broken.windows(4).position(|w| w == b"doub").unwrap(); + broken[at..at + 4].copy_from_slice(b"????"); + big_bad.layer_info.as_mut().unwrap().layers[0].blocks[0].data = broken; + let (little, w) = big_bad.to_bytes(ByteOrder::Little).unwrap(); + assert_eq!(w.iter().filter(|w| w.starts_with("lfx2 block")).count(), 1, "{w:?}"); + assert_eq!(little, clean.to_bytes(ByteOrder::Little).unwrap().0); +} diff --git a/crates/psd/src/tiff/transcode.rs b/crates/psd/src/tiff/transcode.rs new file mode 100644 index 0000000000..31d12b702f --- /dev/null +++ b/crates/psd/src/tiff/transcode.rs @@ -0,0 +1,1384 @@ +use super::{ByteOrder, LayerMask, MAX_DEPTH, PsdError, Reader, Result, TaggedBlock, Version, mask_parameter_bytes, uses_long_length}; + +/// Converts the tagged blocks after the 37724 header string from `from` to `to`. +pub fn transcode_blocks(data: &[u8], version: Version, from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { + let mut t = Tx::new(data, version, from, to); + t.blocks()?; + Ok(t.finish()) +} + +/// Converts a layer-info body (the data of a `Layr` / `Lr16` / `Lr32` block) between orders. +pub fn transcode_layer_info(data: &[u8], version: Version, from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { + let mut t = Tx::new(data, version, from, to); + t.layer_info()?; + Ok(t.finish()) +} + +/// Converts image-resource blocks (a 34377 tag) between orders. +pub fn transcode_resources(data: &[u8], from: ByteOrder, to: ByteOrder) -> Result<(Vec, Vec)> { + let mut t = Tx::new(data, Version::Psd, from, to); + t.resources()?; + Ok(t.finish()) +} + +/// A block signature as stored in `little` or big-endian data. Big-endian data (the model) +/// also carries the `MIB8` / `46B8` markers of blocks kept from a little-endian file. +fn is_sig(b: &[u8], little: bool) -> bool { + if little { + b.starts_with(b"MIB8") || b.starts_with(b"46B8") + } else { + b.starts_with(b"8BIM") || b.starts_with(b"8B64") || TaggedBlock::is_foreign_order_signature(b) + } +} + +fn height(top: i32, bottom: i32) -> usize { + usize::try_from(bottom.saturating_sub(top)).unwrap_or(0) +} + +/// Per layer record: the channel (id, length) list and the heights of the layer, mask and +/// real-mask rects. +type RecordShape = (Vec<(i16, u64)>, [usize; 3]); + +struct Tx<'a> { + r: Reader<'a>, + out: Vec, + from: ByteOrder, + to: ByteOrder, + version: Version, + warnings: Vec, + depth: usize, +} + +macro_rules! copy_int { + ($name:ident, $ty:ty) => { + fn $name(&mut self) -> Result<$ty> { + let v = self.r.$name()?; + self.out.extend_from_slice(&if self.to.is_little() { v.to_le_bytes() } else { v.to_be_bytes() }); + Ok(v) + } + }; +} + +impl<'a> Tx<'a> { + fn new(data: &'a [u8], version: Version, from: ByteOrder, to: ByteOrder) -> Self { + let r = if from.is_little() { Reader::new_little(data) } else { Reader::new(data) }; + Tx { r, out: Vec::with_capacity(data.len()), from, to, version, warnings: Vec::new(), depth: 0 } + } + + fn finish(self) -> (Vec, Vec) { + (self.out, self.warnings) + } + + /// A walker over `data` (a nested structure) that writes into this walker's output: it + /// takes the buffer, so a nested structure is written once, not copied up per level. Hand + /// the buffer back with [`Self::adopt`] before returning, error or not. + fn nested<'b>(&mut self, data: &'b [u8], depth: usize) -> Tx<'b> { + let r = if self.from.is_little() { Reader::new_little(data) } else { Reader::new(data) }; + Tx { r, out: std::mem::take(&mut self.out), from: self.from, to: self.to, version: self.version, warnings: Vec::new(), depth } + } + + /// Takes the output buffer back from a [`Self::nested`] walker. + fn adopt(&mut self, child: &mut Tx<'_>) { + self.out = std::mem::take(&mut child.out); + } + + fn same_order(&self) -> bool { + self.from == self.to + } + + fn remaining(&self) -> usize { + self.r.remaining() + } + + copy_int!(u16, u16); + copy_int!(i16, i16); + copy_int!(u32, u32); + copy_int!(i32, i32); + copy_int!(u64, u64); + copy_int!(i64, i64); + copy_int!(f64, f64); + + fn u8(&mut self) -> Result { + let v = self.r.u8()?; + self.out.push(v); + Ok(v) + } + + fn f32(&mut self) -> Result<()> { + let b = self.r.array::<4>()?; + let v = if self.from.is_little() { f32::from_le_bytes(b) } else { f32::from_be_bytes(b) }; + self.out.extend_from_slice(&if self.to.is_little() { v.to_le_bytes() } else { v.to_be_bytes() }); + Ok(()) + } + + /// Bytes copied as they are. + fn raw(&mut self, n: usize) -> Result<()> { + let b = self.r.bytes(n)?; + self.out.extend_from_slice(b); + Ok(()) + } + + fn rest(&mut self) -> Result<()> { + let n = self.remaining(); + self.raw(n) + } + + /// A four-character signature or key: stored reversed in little-endian data. Returns the + /// canonical (big-endian) spelling. + fn key(&mut self) -> Result<[u8; 4]> { + let mut k = self.r.array::<4>()?; + if self.from.is_little() { + k.reverse(); + } + let mut w = k; + if self.to.is_little() { + w.reverse(); + } + self.out.extend_from_slice(&w); + Ok(k) + } + + fn len(&mut self, long: bool) -> Result { + if long { self.u64() } else { Ok(u64::from(self.u32()?)) } + } + + fn u16s(&mut self, n: usize) -> Result<()> { + for _ in 0..n { + self.u16()?; + } + Ok(()) + } + + /// Up to `n` `u16`s: a block shorter than its full layout keeps what it has. + fn u16s_upto(&mut self, n: usize) -> Result<()> { + for _ in 0..n { + if self.remaining() < 2 { + break; + } + self.u16()?; + } + Ok(()) + } + + fn i32s(&mut self, n: usize) -> Result<()> { + for _ in 0..n { + self.i32()?; + } + Ok(()) + } + + fn f64s(&mut self, n: usize) -> Result<()> { + for _ in 0..n { + self.f64()?; + } + Ok(()) + } + + /// `u16`s to the end (a lone odd byte is copied). + fn u16s_to_end(&mut self) -> Result<()> { + while self.remaining() >= 2 { + self.u16()?; + } + self.rest() + } + + fn u32s_to_end(&mut self) -> Result<()> { + while self.remaining() >= 4 { + self.u32()?; + } + self.rest() + } + + /// A Photoshop Unicode string: u32 count + UTF-16 units. + fn unicode(&mut self) -> Result<()> { + let n = self.u32()?; + self.r.check_count(u64::from(n), 2)?; + self.u16s(n as usize) + } + + /// A Pascal string padded to `align`. + fn pascal(&mut self, align: usize) -> Result<()> { + let n = usize::from(self.u8()?); + self.raw(n)?; + let total = 1 + n; + self.raw((align - total % align) % align) + } + + /// Transcodes the next `len` bytes with `f`; whatever `f` leaves is copied verbatim. + fn region(&mut self, len: u64, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { + let data = self.r.bytes_u64(len)?; + let start = self.out.len(); + let mut child = self.nested(data, self.depth); + let res = f(&mut child).and_then(|()| child.rest()); + self.adopt(&mut child); + res?; + if self.out.len() - start != data.len() { + return Err(PsdError::invalid("byte-order conversion changed a structure's length")); + } + self.warnings.append(&mut child.warnings); + Ok(()) + } + + /// Reads a length field (4 bytes, or 8 when `long`), transcodes that many bytes with `f` + /// and writes the length of the result: blocks dropped inside shrink the field. + fn sized_region(&mut self, long: bool, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { + self.sized_region_counting(long, false, f) + } + + /// [`Self::sized_region`] for a u32 length that counts its own 4 bytes. + fn sized_region_inclusive(&mut self, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { + self.sized_region_counting(false, true, f) + } + + fn sized_region_counting(&mut self, long: bool, inclusive: bool, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> Result<()> { + let mut len = self.r.len_field(long)?; + let at = self.out.len(); + self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); + if inclusive { + len = len.checked_sub(4).ok_or_else(|| PsdError::invalid("length shorter than its own field"))?; + } + let data = self.r.bytes_u64(len)?; + let start = self.out.len(); + let mut child = self.nested(data, self.depth); + let res = f(&mut child).and_then(|()| child.rest()); + self.adopt(&mut child); + res?; + self.warnings.append(&mut child.warnings); + self.patch_len(at, long, (self.out.len() - start) as u64 + if inclusive { 4 } else { 0 })?; + Ok(()) + } + + /// Tries `f` on the rest of the data; it counts as a match only when at most three + /// trailing zero bytes are left. On a miss nothing is consumed or written. + fn attempt(&mut self, f: impl FnOnce(&mut Tx<'_>) -> Result<()>) -> bool { + let (reader, written) = (self.r.clone(), self.out.len()); + let ok = f(self).is_ok() && self.remaining() <= 3 && self.r.peek_rest().iter().all(|&b| b == 0); + if !ok { + self.r = reader; + self.out.truncate(written); + } + ok + } + + /// Blocks whose layout is not known by key are very often a versioned descriptor, + /// optionally behind a u32 version (every Photoshop block introduced since CS6 is): + /// recognized by parsing to the end. + fn descriptor_shaped(&mut self) -> bool { + self.attempt(|t| t.versioned_descriptor()) + || self.attempt(|t| { + t.u32()?; + t.versioned_descriptor() + }) + } + + /// Writes `n` into the length placeholder at `at`. + fn patch_len(&mut self, at: usize, long: bool, n: u64) -> Result<()> { + if long { + let b = if self.to.is_little() { n.to_le_bytes() } else { n.to_be_bytes() }; + if let Some(slot) = self.out.get_mut(at..at + 8) { + slot.copy_from_slice(&b); + } + } else { + let n = u32::try_from(n).map_err(|_| PsdError::LimitExceeded("structure too large for a 32-bit length"))?; + let b = if self.to.is_little() { n.to_le_bytes() } else { n.to_be_bytes() }; + if let Some(slot) = self.out.get_mut(at..at + 4) { + slot.copy_from_slice(&b); + } + } + Ok(()) + } + + /// An RLE row-count table (`rows` entries of 2 bytes, 4 in PSB data). + fn rle_counts(&mut self, rows: usize) -> Result<()> { + for _ in 0..rows { + if self.version.is_psb() { + if self.remaining() < 4 { + break; + } + self.u32()?; + } else { + if self.remaining() < 2 { + break; + } + self.u16()?; + } + } + Ok(()) + } + + /// Encoded channel data for a plane of `rows` rows: the compression field, then the RLE + /// row counts (container order) and the samples (always big-endian: copied). + fn channel_data(&mut self, rows: usize, long_counts: bool) -> Result<()> { + let saved = self.version; + if long_counts { + self.version = Version::Psb; + } + let compression = self.u16()?; + let res = if compression == 1 { self.rle_counts(rows) } else { Ok(()) }; + self.version = saved; + res?; + self.rest() + } + + // ---- image resources (34377) ---- + + fn resources(&mut self) -> Result<()> { + while self.remaining() >= 12 && is_sig(self.r.peek_rest(), self.from.is_little()) { + self.key()?; + let id = self.u16()?; + self.pascal(2)?; + let odd = self.r.clone().u32()? % 2 == 1; + self.sized_region(false, |t| t.resource_data(id))?; + if odd && self.remaining() > 0 { + self.raw(1)?; + } + } + self.rest() + } + + /// Typed numeric resources; every other payload is a byte blob (ICC, XMP, EXIF, …) or + /// unknown and is copied as stored. + fn resource_data(&mut self, id: u16) -> Result<()> { + match id { + // Resolution info: fixed 16.16 h res, u16 unit, u16 width unit, then the same for v. + 1005 => { + self.u32()?; + self.u16()?; + self.u16()?; + self.u32()?; + self.u16()?; + self.u16()?; + } + // Layer state (u16 target), layer group info (u16 per layer). + 1024 | 1026 => self.u16s_to_end()?, + // Global angle / altitude. + 1037 | 1049 => { + self.u32()?; + } + // Layer selection ids: u16 count + u32 ids. + 1069 => { + self.u16()?; + self.u32s_to_end()?; + } + // Version info: u32 version, u8 has real merged data, two Unicode strings, u32. + 1057 => { + self.u32()?; + self.u8()?; + self.unicode()?; + self.unicode()?; + self.u32()?; + } + // Grid and guides: u32 version, 2 × u32 grid, u32 count, (u32 location, u8 direction)… + 1032 => { + self.u32()?; + self.u32()?; + self.u32()?; + let n = self.u32()?; + for _ in 0..n { + if self.remaining() < 5 { + break; + } + self.u32()?; + self.u8()?; + } + } + // Path resources: 26-byte records. + 2000..=2997 | 2999 => self.path_records()?, + _ => {} + } + Ok(()) + } + + // ---- tagged blocks ---- + + /// Tagged blocks to the end; bytes that do not form a block are copied. + fn blocks(&mut self) -> Result<()> { + loop { + let rest = self.r.peek_rest(); + if rest.len() < 12 || !is_sig(rest, self.from.is_little()) { + return self.rest(); + } + self.block()?; + } + } + + fn block(&mut self) -> Result<()> { + let before = self.out.len(); + // A `MIB8` / `46B8` marker in big-endian data: a block kept verbatim from a + // little-endian file. Written back as it was into little-endian output (its `8BIM` + // signature reversed, as every signature there), copied as is otherwise. + if !self.from.is_little() && TaggedBlock::is_foreign_order_signature(self.r.peek_rest()) { + let sig = self.r.array::<4>()?; + let mut key = self.r.array::<4>()?; + // Only reached with `from == Big`: the key is canonical here whatever the + // target order. `TaggedBlock::write` sizes the length field from that same key. + // Reversing it for the target order before the check + // asked about a key `PSB_LONG_KEYS` never contains, so a PSB long key's 8-byte + // length was read as 4 and every later block was copied unconverted. + let long = uses_long_length(self.version, &key); + let len = self.r.len_field(long)?; + let data = self.r.bytes_u64(len)?; + if self.to.is_little() { + key.reverse(); + } + self.out.extend_from_slice(&sig); + self.out.extend_from_slice(&key); + let n = self.out.len(); + self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); + self.patch_len(n, long, len)?; + self.out.extend_from_slice(data); + let rest = self.r.peek_rest(); + let pad = (0..=3usize).find(|&k| k == rest.len() || (k < rest.len() && is_sig(&rest[k..], self.from.is_little()))).unwrap_or(0); + let pad_bytes = self.r.bytes(pad)?; + self.out.extend_from_slice(pad_bytes); + return Ok(()); + } + let sig_at = self.out.len(); + let sig = self.key()?; + let key = self.key()?; + let long = uses_long_length(self.version, &key); + let len = self.r.len_field(long)?; + let len_at = self.out.len(); + self.out.extend_from_slice(if long { &[0u8; 8] } else { &[0u8; 4] }); + let data = self.r.bytes_u64(len)?; + // The body is transcoded straight into this output, after the length placeholder. + let data_at = self.out.len(); + let mut child = self.nested(data, self.depth + 1); + let handled = if child.depth > MAX_DEPTH { + Err(PsdError::LimitExceeded("tagged block nesting too deep")) + } else if data.is_empty() { + Ok(true) + } else { + child.block_body(&key) + }; + let rest = if matches!(handled, Ok(true)) { child.rest() } else { Ok(()) }; + self.adopt(&mut child); + rest?; + if !matches!(handled, Ok(true)) { + // A body that was not transcoded leaves nothing: it is copied as stored or dropped. + self.out.truncate(data_at); + } + // Padding: the smallest k in 0..=3 after which the next block (or the end) starts. + let rest = self.r.peek_rest(); + let pad = (0..=3usize).find(|&k| k == rest.len() || (k < rest.len() && is_sig(&rest[k..], self.from.is_little()))).unwrap_or(0); + let pad_bytes = self.r.bytes(pad)?; + match handled { + Ok(true) => { + self.warnings.append(&mut child.warnings); + self.patch_len(len_at, long, (self.out.len() - data_at) as u64)?; + self.out.extend_from_slice(pad_bytes); + } + Ok(false) if self.same_order() => { + self.patch_len(len_at, long, data.len() as u64)?; + self.out.extend_from_slice(data); + self.out.extend_from_slice(pad_bytes); + } + // Little-endian in, big-endian model: keep the bytes as they are, marked. + Ok(false) if self.from.is_little() => { + let marker: &[u8; 4] = if &sig == b"8B64" { b"46B8" } else { b"MIB8" }; + self.out[sig_at..sig_at + 4].copy_from_slice(marker); + self.patch_len(len_at, long, data.len() as u64)?; + self.out.extend_from_slice(data); + self.out.extend_from_slice(pad_bytes); + } + Ok(false) => { + self.out.truncate(before); + self.warnings.push(format!( + "{} block ({} bytes) dropped: its layout is unknown and cannot be converted to the file's byte order", + String::from_utf8_lossy(&key), + data.len() + )); + } + Err(e) if self.same_order() => return Err(PsdError::invalid(format!("{} block: {e}", String::from_utf8_lossy(&key)))), + Err(e) if self.from.is_little() => { + let marker: &[u8; 4] = if &sig == b"8B64" { b"46B8" } else { b"MIB8" }; + self.out[sig_at..sig_at + 4].copy_from_slice(marker); + self.patch_len(len_at, long, data.len() as u64)?; + self.out.extend_from_slice(data); + self.out.extend_from_slice(pad_bytes); + self.warnings.push(format!("{} block ({} bytes) kept as stored, in little-endian layout: {e}", String::from_utf8_lossy(&key), data.len())); + } + Err(e) => { + self.out.truncate(before); + self.warnings.push(format!("{} block ({} bytes) dropped: {e}", String::from_utf8_lossy(&key), data.len())); + } + } + Ok(()) + } + + /// Transcodes a block's data by key. `Ok(false)` for keys without a known layout. + fn block_body(&mut self, key: &[u8; 4]) -> Result { + match key { + b"Layr" | b"Lr16" | b"Lr32" => self.layer_info()?, + b"luni" => self.unicode()?, + b"lsct" | b"lsdk" => { + self.u32()?; + if self.remaining() >= 8 { + self.key()?; + self.key()?; + } + if self.remaining() >= 4 { + self.u32()?; + } + } + // lyvr layer version, sn2P and vowv (shape and vector flags). + b"lyid" | b"lspf" | b"lyvr" | b"sn2P" | b"vowv" => { + self.u32()?; + } + // Text-engine global data: a textual structure, no byte order. + b"Txt2" => self.rest()?, + b"lnsr" => { + self.key()?; + } + // Advanced Blending flags and fill opacity: one byte, then padding (Adobe spec, + // "Additional Layer Information"; `lmgm`/`vmgm` = layer/vector mask hides effects). + b"clbl" | b"infx" | b"knko" | b"tsly" | b"lmgm" | b"vmgm" | b"iOpa" => { + self.u8()?; + } + b"lclr" => self.u16s_upto(4)?, + b"fxrp" => { + while self.remaining() >= 8 { + self.f64()?; + } + } + b"brst" => self.u32s_to_end()?, + b"LMsk" => self.u16s_upto(6)?, + b"FMsk" => self.u16s_to_end()?, + b"shmd" => self.metadata_setting()?, + // Effects (lfxs: the effects as applied to a group), content credentials (CAI). + b"lfx2" | b"lmfx" | b"lfxs" | b"CAI " => { + self.u32()?; + self.versioned_descriptor()?; + } + b"SoCo" | b"GdFl" | b"PtFl" | b"vstk" | b"blwh" | b"vibA" | b"CgEd" | b"artb" | b"artd" | b"cinf" | b"cust" | b"PxSD" | b"GenI" | b"OCIO" + | b"extn" | b"pths" => { + self.versioned_descriptor()?; + } + // Color Lookup: u16 version, then the descriptor. + b"clrL" => { + self.u16()?; + self.versioned_descriptor()?; + } + b"lnk2" | b"lnk3" | b"lnkD" | b"lnkE" => self.linked_layers()?, + b"Anno" => self.annotations()?, + b"vogk" => { + self.u32()?; + self.versioned_descriptor()?; + } + b"vscg" => { + self.key()?; + self.versioned_descriptor()?; + } + b"SoLd" | b"SoLE" => { + self.key()?; + self.u32()?; + self.versioned_descriptor()?; + } + b"PlLd" => { + self.key()?; + self.u32()?; + self.pascal(1)?; + for _ in 0..4 { + self.u32()?; + } + self.f64s(8)?; + self.u32()?; + self.versioned_descriptor()?; + } + // Type: version, transform, text version + descriptor, warp version + descriptor, + // and the bounds as four 32-bit values (the specification says doubles; Photoshop + // writes 16 bytes). + b"TySh" => self.type_layer()?, + b"vmsk" | b"vsms" => { + self.u32()?; + self.u32()?; + self.path_records()?; + } + b"Patt" | b"Pat2" | b"Pat3" => { + while self.remaining() >= 4 { + let len = self.r.clone().u32()?; + if len == 0 { + self.u32()?; + continue; + } + self.sized_region(false, Tx::pattern)?; + // Each pattern is padded to a multiple of 4. + let pad = ((4 - len % 4) % 4) as usize; + self.raw(pad.min(self.remaining()))?; + } + } + b"FEid" | b"FXid" => self.filter_effects()?, + b"lrFX" => self.legacy_effects()?, + b"levl" => { + self.u16()?; + for _ in 0..29 * 5 { + if self.remaining() < 2 { + break; + } + self.u16()?; + } + if self.remaining() >= 4 { + self.key()?; + } + self.u16s_to_end()?; + } + b"curv" => self.curves()?, + b"hue2" => { + self.u16()?; + self.u8()?; + self.u8()?; + self.u16s_to_end()?; + } + b"brit" => self.u16s_upto(3)?, + b"thrs" | b"post" => { + self.u16()?; + } + b"nvrt" => {} + b"expA" => { + self.u16()?; + for _ in 0..3 { + self.f32()?; + } + } + b"blnc" => self.u16s_upto(9)?, + b"mixr" | b"selc" => self.u16s_to_end()?, + b"phfl" => match self.u16()? { + 2 => { + self.u16s(5)?; + self.u32()?; + self.u8()?; + } + 3 => { + for _ in 0..4 { + self.u32()?; + } + self.u8()?; + } + v => return Err(PsdError::Unsupported(format!("photo filter version {v}"))), + }, + b"grdm" => self.gradient_map()?, + _ if !self.same_order() => return Ok(self.descriptor_shaped()), + _ => return Ok(false), + } + Ok(true) + } + + /// `lnk2` / `lnk3` / `lnkD` / `lnkE` (linked and embedded smart-object files): items of + /// u64 length + data, each padded to 4. Item: type (`liFD` embedded, `liFE` external, + /// `liFA` alias), version 1–7, Pascal uuid, Unicode name, file type and creator keys, u64 + /// data length, open-descriptor flag (+ descriptor), per-type fields, the file bytes for + /// `liFD`, then the fields later versions added. + fn linked_layers(&mut self) -> Result<()> { + while self.remaining() >= 8 { + let len = self.r.clone().u64()?; + self.sized_region(true, |t| { + let kind = t.key()?; + let version = t.u32()?; + t.pascal(1)?; + t.unicode()?; + t.key()?; + t.key()?; + let data_len = t.u64()?; + if t.u8()? != 0 { + t.versioned_descriptor()?; + } + if &kind == b"liFE" && version > 3 { + t.i32()?; + for _ in 0..4 { + t.u8()?; + } + t.f64()?; + } + if &kind == b"liFE" { + t.u64()?; + } + if &kind == b"liFA" { + t.raw(8)?; + } + let data_len = usize::try_from(data_len).map_err(|_| PsdError::LimitExceeded("linked file length"))?; + if &kind == b"liFD" { + t.raw(data_len)?; + } + if version >= 5 { + t.unicode()?; + } + if version >= 6 { + t.f64()?; + } + if version >= 7 { + t.u8()?; + } + if &kind == b"liFE" && version == 2 { + t.raw(data_len)?; + } + Ok(()) + })?; + let pad = ((4 - len % 4) % 4) as usize; + self.raw(pad.min(self.remaining()))?; + } + Ok(()) + } + + /// `Anno`: u16 major, u16 minor, u32 count, then per annotation a self-counting u32 + /// length, type key, open and flags bytes, u16 optional blocks, icon and popup rects, a + /// colour, three even-padded Pascal strings, and a self-counting data block (key, u32 + /// size, text with its own byte-order mark). + fn annotations(&mut self) -> Result<()> { + self.u16()?; + self.u16()?; + let n = self.u32()?; + for _ in 0..n { + if self.remaining() < 4 { + break; + } + self.sized_region_inclusive(|t| { + t.key()?; + t.u8()?; + t.u8()?; + t.u16()?; + t.i32s(8)?; + t.u16s(5)?; + t.pascal(2)?; + t.pascal(2)?; + t.pascal(2)?; + t.sized_region_inclusive(|d| { + d.key()?; + d.u32()?; + Ok(()) + }) + })?; + } + Ok(()) + } + + // ---- layer info ---- + + fn layer_info(&mut self) -> Result<()> { + let count = self.i16()?; + let n = usize::from(count.unsigned_abs()); + self.r.check_count(n as u64, 34)?; + // Per layer: the channel ids and lengths, and the heights of the layer, mask and + // real-mask rects (what each channel's RLE count table spans). + let mut records: Vec = Vec::with_capacity(n); + for _ in 0..n { + let top = self.i32()?; + self.i32()?; + let bottom = self.i32()?; + self.i32()?; + let nch = self.u16()?; + if nch > crate::layer::MAX_LAYER_CHANNELS { + return Err(PsdError::LimitExceeded("too many channels in layer")); + } + let mut chans = Vec::with_capacity(usize::from(nch)); + let psb = self.version.is_psb(); + for _ in 0..nch { + let id = self.i16()?; + let len = self.len(psb)?; + chans.push((id, len)); + } + let sig = self.key()?; + if &sig != b"8BIM" { + return Err(PsdError::InvalidSignature { expected: "8BIM (layer blend mode)", found: sig }); + } + self.key()?; + for _ in 0..4 { + self.u8()?; + } + let mut heights = [height(top, bottom), 0, 0]; + let mut mask_heights = (0usize, 0usize); + self.sized_region(false, |t| { + let mut mh = (0, 0); + t.sized_region(false, |m| { + mh = m.mask()?; + Ok(()) + })?; + mask_heights = mh; + // Blending ranges are bytes (black low/high, white low/high per channel), so + // Photoshop stores them in the same order in either byte order: an Intel-order + // layered TIFF holds the default ranges as `00 00 ff ff`. Swapping them as u32s + // turned every range into `ff ff 00 00`, which hides every pixel of the layer. + t.sized_region(false, |x| x.rest())?; + t.pascal(4)?; + t.blocks() + })?; + heights[1] = mask_heights.0; + heights[2] = mask_heights.1; + records.push((chans, heights)); + } + for (chans, heights) in &records { + for &(id, len) in chans { + if len == 0 { + continue; + } + if len == 1 { + return Err(PsdError::invalid("channel data length 1")); + } + let rows = match id { + -2 => heights[1], + -3 => heights[2], + _ => heights[0], + }; + self.region(len, |t| t.channel_data(rows, false))?; + } + } + self.rest() + } + + /// A layer mask record. Returns the heights of the mask and real-mask rects. + fn mask(&mut self) -> Result<(usize, usize)> { + let whole = self.r.peek_rest(); + if whole.is_empty() { + return Ok((0, 0)); + } + let top = self.i32()?; + self.i32()?; + let bottom = self.i32()?; + self.i32()?; + self.u8()?; + let flags = self.u8()?; + let mut real_h = 0; + if self.remaining() <= 2 { + return Ok((height(top, bottom), 0)); + } + let has_parameters = flags & LayerMask::FLAG_PARAMETERS != 0; + // The helper only looks at lengths and the parameter-flags byte, so it reads the + // same in either order. + let real_first = has_parameters && LayerMask::real_before_parameters(whole); + let mut read_real = |t: &mut Tx<'_>| -> Result<()> { + if t.remaining() >= 18 { + t.u8()?; + t.u8()?; + let rt = t.i32()?; + t.i32()?; + let rb = t.i32()?; + t.i32()?; + real_h = height(rt, rb); + } + Ok(()) + }; + if real_first { + read_real(self)?; + } + if has_parameters { + let pf = self.u8()?; + let need = mask_parameter_bytes(pf); + if need > self.remaining() { + return Err(PsdError::UnexpectedEof { offset: self.r.pos(), needed: need - self.remaining() }); + } + if pf & 1 != 0 { + self.u8()?; + } + if pf & 2 != 0 { + self.f64()?; + } + if pf & 4 != 0 { + self.u8()?; + } + if pf & 8 != 0 { + self.f64()?; + } + } + if !real_first { + read_real(self)?; + } + Ok((height(top, bottom), real_h)) + } + + // ---- descriptors ---- + + /// Photoshop's Intel-order TIFF type blocks can use PSD byte order for the + /// text descriptor, then container order again for warp and bounds (#4758). + fn type_layer(&mut self) -> Result<()> { + self.u16()?; + self.f64s(6)?; + self.type_descriptor(50, ByteOrder::Big)?; + self.type_descriptor(1, self.to)?; + self.i32s(4) + } + + fn type_descriptor(&mut self, version: u16, to: ByteOrder) -> Result<()> { + let [hi, lo] = version.to_be_bytes(); + let from = if self.from.is_little() && self.r.peek_rest().starts_with(&[hi, lo, 0, 0, 0, 16]) { ByteOrder::Big } else { self.from }; + let data = self.r.peek_rest(); + let mut descriptor = Tx::new(data, self.version, from, to); + descriptor.depth = self.depth; + descriptor.u16()?; + descriptor.versioned_descriptor()?; + let used = data.len() - descriptor.remaining(); + self.r.bytes(used)?; + self.warnings.append(&mut descriptor.warnings); + self.out.extend_from_slice(&descriptor.out); + Ok(()) + } + + fn versioned_descriptor(&mut self) -> Result<()> { + self.u32()?; + self.descriptor() + } + + fn descriptor(&mut self) -> Result<()> { + if self.depth > MAX_DEPTH { + return Err(PsdError::LimitExceeded("descriptor nesting too deep")); + } + self.depth += 1; + self.unicode()?; + self.id()?; + let n = self.u32()?; + self.r.check_count(u64::from(n), 10)?; + for _ in 0..n { + self.id()?; + self.value()?; + } + self.depth -= 1; + Ok(()) + } + + fn id(&mut self) -> Result<()> { + let n = self.u32()?; + if n == 0 { + self.key()?; + } else { + self.raw(usize::try_from(n).map_err(|_| PsdError::LimitExceeded("id length"))?)?; + } + Ok(()) + } + + fn class(&mut self) -> Result<()> { + self.unicode()?; + self.id() + } + + fn value(&mut self) -> Result<()> { + if self.depth > MAX_DEPTH { + return Err(PsdError::LimitExceeded("descriptor nesting too deep")); + } + let ty = self.key()?; + match &ty { + b"obj " => { + let n = self.u32()?; + self.r.check_count(u64::from(n), 8)?; + for _ in 0..n { + self.reference_item()?; + } + } + b"Objc" | b"GlbO" => self.descriptor()?, + b"VlLs" => { + let n = self.u32()?; + self.r.check_count(u64::from(n), 4)?; + self.depth += 1; + for _ in 0..n { + self.value()?; + } + self.depth -= 1; + } + b"doub" => { + self.f64()?; + } + b"UntF" => { + self.key()?; + self.f64()?; + } + b"UnFl" => { + self.key()?; + let n = self.u32()?; + self.r.check_count(u64::from(n), 8)?; + self.f64s(n as usize)?; + } + b"TEXT" => self.unicode()?, + b"enum" => { + self.id()?; + self.id()?; + } + b"long" => { + self.i32()?; + } + b"comp" => { + self.i64()?; + } + b"bool" => { + self.u8()?; + } + b"type" | b"GlbC" => self.class()?, + b"alis" | b"Pth " | b"tdta" => { + let n = self.u32()?; + self.raw(usize::try_from(n).map_err(|_| PsdError::LimitExceeded("descriptor data length"))?)?; + } + b"ObAr" => { + self.u32()?; + self.descriptor()?; + } + other => return Err(PsdError::Unsupported(format!("descriptor OSType {:?}", String::from_utf8_lossy(other)))), + } + Ok(()) + } + + fn reference_item(&mut self) -> Result<()> { + let ty = self.key()?; + match &ty { + b"prop" => { + self.class()?; + self.id()?; + } + b"Clss" => self.class()?, + b"Enmr" => { + self.class()?; + self.id()?; + self.id()?; + } + b"rele" => { + self.class()?; + self.i32()?; + } + b"Idnt" | b"indx" => { + self.class()?; + self.u32()?; + } + b"name" => { + self.class()?; + self.unicode()?; + } + other => return Err(PsdError::Unsupported(format!("reference item type {:?}", String::from_utf8_lossy(other)))), + } + Ok(()) + } + + // ---- other block layouts ---- + + /// `shmd`: u32 count, then per item signature, key, copy flag, 3 bytes, u32 length, data. + /// An item whose layout is unknown is dropped on its own (and the count patched) rather + /// than taking the whole block with it. + fn metadata_setting(&mut self) -> Result<()> { + let n = self.r.u32()?; + self.r.check_count(u64::from(n), 16)?; + let count_at = self.out.len(); + self.out.extend_from_slice(&[0; 4]); + let mut kept = 0u32; + for _ in 0..n { + let before = self.out.len(); + self.key()?; + let key = self.key()?; + self.u8()?; + self.raw(3)?; + let same = self.same_order(); + let res = self.sized_region(false, |t| match &key { + b"cmls" | b"mlst" | b"cust" | b"tmln" | b"sgrp" | b"cinf" => t.versioned_descriptor(), + // mdyn (dynamic metadata) is one u32. + b"mdyn" => t.u32s_to_end(), + _ if same => Ok(()), + _ if t.descriptor_shaped() => Ok(()), + _ => Err(PsdError::Unsupported(format!("metadata item {:?}", String::from_utf8_lossy(&key)))), + }); + match res { + Ok(()) => kept += 1, + Err(e) if same => return Err(e), + Err(e) => { + self.out.truncate(before); + self.warnings.push(format!("shmd item {} dropped: {e}", String::from_utf8_lossy(&key))); + } + } + } + self.patch_len(count_at, false, u64::from(kept)) + } + + /// 26-byte path records: a u16 selector then, for knots, six fixed-point coordinates. + fn path_records(&mut self) -> Result<()> { + while self.remaining() >= 26 { + let selector = self.u16()?; + match selector { + 1 | 2 | 4 | 5 => self.i32s(6)?, + // Length records: knot count, path operation, then fields Photoshop writes as + // 16-bit values (zero in practice). + 0 | 3 => self.u16s(12)?, + // Clipboard: four fixed-point coordinates and a resolution. + 7 => { + self.i32s(5)?; + self.raw(4)?; + } + 8 => { + self.u16()?; + self.raw(22)?; + } + _ => self.raw(24)?, + } + } + Ok(()) + } + + /// One pattern of a `Patt` block (the data after its u32 length). + fn pattern(t: &mut Tx<'_>) -> Result<()> { + t.u32()?; + let mode = t.u32()?; + t.u16()?; + t.u16()?; + t.unicode()?; + t.pascal(1)?; + if mode == 2 { + t.raw(768)?; + // Some writers follow the palette with 4 extra bytes before the array list version. + let next_is_version = t.r.clone().u32().is_ok_and(|v| v == 3); + if !next_is_version && t.remaining() >= 4 { + t.raw(4)?; + } + } + t.u32()?; + t.sized_region(false, |v| { + v.i32s(4)?; + let count = v.u32()?; + for _ in 0..count.saturating_add(2) { + if v.remaining() < 4 { + break; + } + if v.u32()? == 0 { + continue; + } + if v.r.clone().u32()? == 0 { + v.u32()?; + continue; + } + v.sized_region(false, |c| { + c.u32()?; + let ct = c.i32()?; + c.i32()?; + let cb = c.i32()?; + c.i32()?; + c.u16()?; + let comp = c.u8()?; + if comp == 1 { + let saved = c.version; + c.version = Version::Psd; + let res = c.rle_counts(height(ct, cb)); + c.version = saved; + res?; + } + Ok(()) + })?; + } + Ok(()) + }) + } + + /// `FEid` / `FXid`: version, then one section per smart object (u64 length, items with + /// per-channel planes and 4-byte RLE counts), each padded to 4 bytes. + fn filter_effects(&mut self) -> Result<()> { + self.u32()?; + loop { + let len = self.r.clone().len_field(true)?; + self.filter_effects_section()?; + // Mirrors `FilterEffects::parse`: the last section's padding may be cut short. + let pad = usize::try_from((4 - len % 4) % 4).unwrap_or(0).min(self.remaining()); + self.raw(pad)?; + let rest = self.r.peek_rest(); + if rest.len() <= 3 && rest.iter().all(|&b| b == 0) { + return Ok(()); + } + if rest.len() < 8 { + return Err(PsdError::invalid(format!("filter effects: {} unexpected bytes after the data", rest.len()))); + } + } + } + + fn filter_effects_section(&mut self) -> Result<()> { + self.sized_region(true, |t| { + while !t.r.is_empty() { + t.pascal(1)?; + t.u32()?; + t.sized_region(true, |it| { + let top = it.i32()?; + it.i32()?; + let bottom = it.i32()?; + it.i32()?; + let rect_h = height(top, bottom); + it.u32()?; + let max = it.u32()?; + if max > crate::filter_effects::MAX_SLOTS { + return Err(PsdError::LimitExceeded("filter effects: too many channels")); + } + for _ in 0..max + 2 { + if it.u32()? != 0 { + it.effects_plane(rect_h)?; + } + } + Ok(()) + })?; + if !t.r.is_empty() && t.u8()? != 0 { + let top = t.i32()?; + t.i32()?; + let bottom = t.i32()?; + t.i32()?; + t.effects_plane(height(top, bottom))?; + } + } + Ok(()) + }) + } + + fn effects_plane(&mut self, rows: usize) -> Result<()> { + self.sized_region(true, |p| p.channel_data(rows, true)) + } + + /// `lrFX`: version, count, then effects (`cmnS`, `dsdw`, `isdw`, `oglw`, `iglw`, `bevl`, + /// `sofi`), each a signature, key, u32 size and fixed fields. + fn legacy_effects(&mut self) -> Result<()> { + self.u16()?; + let n = self.u16()?; + for _ in 0..n { + if self.remaining() < 12 { + break; + } + self.key()?; + let key = self.key()?; + self.sized_region(false, |t| { + // A colour: u16 space + 4 × u16 components. + let colour = |t: &mut Tx<'_>| t.u16s(5); + let blend = |t: &mut Tx<'_>| -> Result<()> { + t.key()?; + t.key()?; + Ok(()) + }; + match &key { + b"cmnS" => { + t.u32()?; + t.u8()?; + t.u16()?; + } + b"dsdw" | b"isdw" => { + t.u32()?; + t.i32s(4)?; + colour(t)?; + blend(t)?; + t.u8()?; + t.u8()?; + t.u8()?; + if t.remaining() >= 10 { + colour(t)?; + } + } + b"oglw" => { + t.u32()?; + t.i32s(2)?; + colour(t)?; + blend(t)?; + t.u8()?; + t.u8()?; + if t.remaining() >= 10 { + colour(t)?; + } + } + b"iglw" => { + t.u32()?; + t.i32s(2)?; + colour(t)?; + blend(t)?; + t.u8()?; + t.u8()?; + if t.remaining() >= 11 { + t.u8()?; + colour(t)?; + } + } + b"bevl" => { + t.u32()?; + t.i32s(3)?; + blend(t)?; + blend(t)?; + colour(t)?; + colour(t)?; + for _ in 0..6 { + t.u8()?; + } + if t.remaining() >= 20 { + colour(t)?; + colour(t)?; + } + } + b"sofi" => { + t.u32()?; + blend(t)?; + colour(t)?; + t.u8()?; + t.u8()?; + if t.remaining() >= 10 { + colour(t)?; + } + } + _ => {} + } + Ok(()) + })?; + } + Ok(()) + } + + /// `curv`: pad byte, version, channel bitmap, per set bit a point list; then an optional + /// `Crv ` extension with per-channel point lists. + fn curves(&mut self) -> Result<()> { + self.u8()?; + self.u16()?; + let bits = self.u32()?; + for bit in 0..32 { + if bits & (1 << bit) == 0 { + continue; + } + if self.remaining() < 2 { + break; + } + let n = self.u16()?; + self.u16s(usize::from(n) * 2)?; + } + if self.remaining() >= 10 { + self.key()?; + self.u16()?; + let n = self.u32()?; + for _ in 0..n { + if self.remaining() < 4 { + break; + } + self.u16()?; + let pts = self.u16()?; + self.u16s(usize::from(pts) * 2)?; + } + } + Ok(()) + } + + /// `grdm`: version, reverse, dither, [method key], name, colour stops, transparency + /// stops, then the noise-gradient fields (a u32 seed among u16s). + fn gradient_map(&mut self) -> Result<()> { + let version = self.u16()?; + self.u8()?; + self.u8()?; + if version == 3 { + self.key()?; + } + self.unicode()?; + let n = self.u16()?; + for _ in 0..n { + if self.remaining() < 20 { + break; + } + self.u32()?; + self.u32()?; + self.u16s(6)?; + } + let nt = self.u16()?; + for _ in 0..nt { + if self.remaining() < 10 { + break; + } + self.u32()?; + self.u32()?; + self.u16()?; + } + // expansion, interpolation, length, mode, u32 seed, then u16 fields. + for _ in 0..4 { + if self.remaining() < 2 { + return Ok(()); + } + self.u16()?; + } + if self.remaining() >= 4 { + self.u32()?; + } + self.u16s_to_end() + } +} diff --git a/crates/psd/src/tree.rs b/crates/psd/src/tree.rs index 75ea750e6a..606d9b2087 100644 --- a/crates/psd/src/tree.rs +++ b/crates/psd/src/tree.rs @@ -68,10 +68,28 @@ impl PsdFile { /// [`layer_tree`](Self::layer_tree) with at most `max_depth` levels of /// groups: a group nested `max_depth` or more groups deep (a root node is - /// at depth 0) keeps its node but not its children. Built iteratively, so + /// at depth 0) keeps its node but not its children. Layers under a divider + /// that is never closed sit at the enclosing level, as in `layer_tree`. Built iteratively, so /// no file can make it recurse, and the result is no deeper than /// `max_depth + 1` levels, so walking or dropping it recursively is bounded. pub fn layer_tree_max_depth(&self, max_depth: usize) -> Vec { + let layers = self.layers(); + // First pass: find the dividers a folder closes. A divider that is never closed opens no + // group; its layers belong to the enclosing level, so it opens no frame and every + // frame's depth is the final depth of its nodes (#5758). + let mut closed = vec![false; layers.len()]; + let mut open: Vec = Vec::new(); + for (i, rec) in layers.iter().enumerate() { + match rec.section_type() { + SectionType::BoundingDivider => open.push(i), + SectionType::OpenFolder | SectionType::ClosedFolder => { + if let Some(slot) = open.pop().and_then(|d| closed.get_mut(d)) { + *slot = true; + } + } + _ => {} + } + } // Stack of (divider index, children); frame `k` holds the nodes at depth `k`. Frames // past `max_depth` are tracked (to match dividers to folders) but collect nothing. let mut stack: Vec<(Option, Vec)> = vec![(None, Vec::new())]; @@ -82,9 +100,13 @@ impl PsdFile { top.1.push(node); } }; - for (i, rec) in self.layers().iter().enumerate() { + for (i, rec) in layers.iter().enumerate() { match rec.section_type() { - SectionType::BoundingDivider => stack.push((Some(i), Vec::new())), + SectionType::BoundingDivider => { + if closed.get(i).copied().unwrap_or(false) { + stack.push((Some(i), Vec::new())); + } + } SectionType::OpenFolder | SectionType::ClosedFolder => { let (divider, children) = if stack.len() > 1 { stack.pop().unwrap_or_default() } else { (None, Vec::new()) }; push(&mut stack, LayerNode::Group { index: i, divider, children }); @@ -92,6 +114,7 @@ impl PsdFile { _ => push(&mut stack, LayerNode::Layer { index: i }), } } + // Only closed dividers open frames, so none is left here; this just guards the invariant. while stack.len() > 1 { let (_, children) = stack.pop().unwrap_or_default(); if let Some(top) = stack.last_mut() { diff --git a/crates/psd/tests/builder.rs b/crates/psd/tests/it/builder.rs similarity index 100% rename from crates/psd/tests/builder.rs rename to crates/psd/tests/it/builder.rs diff --git a/crates/psd/tests/corpus.rs b/crates/psd/tests/it/corpus.rs similarity index 100% rename from crates/psd/tests/corpus.rs rename to crates/psd/tests/it/corpus.rs diff --git a/crates/psd/tests/it/corpus_atn.rs b/crates/psd/tests/it/corpus_atn.rs new file mode 100644 index 0000000000..32f6367714 --- /dev/null +++ b/crates/psd/tests/it/corpus_atn.rs @@ -0,0 +1,60 @@ +//! Corpus test (feature `corpus`; run with `cargo xtask test-corpus`): every `.atn` under +//! `corpus/atn/` (gitignored, fetched by `cargo xtask corpus --atn`) parses, and writing it back +//! reproduces the bytes exactly. A missing corpus fails. +#![cfg(feature = "corpus")] + +use std::path::{Path, PathBuf}; + +use photocraft_psd::atn; + +fn collect(dir: &Path, out: &mut Vec) { + // An unreadable folder fails the test rather than shrinking the corpus. + let rd = std::fs::read_dir(dir).unwrap_or_else(|e| panic!("{}: {e}", dir.display())); + for e in rd { + let p = e.unwrap_or_else(|e| panic!("{}: {e}", dir.display())).path(); + if p.is_dir() { + collect(&p, out); + } else if p.extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("atn")) { + out.push(p); + } + } +} + +/// Sets in the corpus; raise, never lower. +const FLOOR: usize = 16; + +#[test] +fn atn_corpus_parses_and_is_byte_stable() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/atn"); + assert!(root.is_dir(), "{} is missing: run `cargo xtask corpus --atn`", root.display()); + let mut files = Vec::new(); + collect(&root, &mut files); + files.sort(); + let mut failures = Vec::new(); + let (mut actions, mut steps) = (0usize, 0usize); + for p in &files { + let name = p.strip_prefix(&root).unwrap_or(p).display().to_string(); + let bytes = std::fs::read(p).unwrap(); + let file = match atn::parse(&bytes) { + Ok(f) => f, + Err(e) => { + failures.push(format!("{name}: {e}")); + continue; + } + }; + if !file.warnings.is_empty() { + failures.push(format!("{name}: warnings {:?}", file.warnings)); + } + actions += file.actions.len(); + steps += file.actions.iter().map(|a| a.items.len()).sum::(); + match atn::write(&file) { + Ok(written) if written == bytes => {} + Ok(written) => failures.push(format!("{name}: rewritten bytes differ ({} vs {} bytes)", written.len(), bytes.len())), + Err(e) => failures.push(format!("{name}: write failed: {e}")), + } + println!("{name}: {} actions, {} steps", file.actions.len(), file.actions.iter().map(|a| a.items.len()).sum::()); + } + println!("{} files, {actions} actions, {steps} steps", files.len()); + assert!(files.len() >= FLOOR, "only {} .atn files; the corpus floor is {FLOOR}", files.len()); + assert!(failures.is_empty(), "{} failures:\n{}", failures.len(), failures.join("\n")); +} diff --git a/crates/psd/tests/layer_block_order.rs b/crates/psd/tests/it/layer_block_order.rs similarity index 100% rename from crates/psd/tests/layer_block_order.rs rename to crates/psd/tests/it/layer_block_order.rs diff --git a/crates/psd/tests/it/main.rs b/crates/psd/tests/it/main.rs new file mode 100644 index 0000000000..ee6879aa66 --- /dev/null +++ b/crates/psd/tests/it/main.rs @@ -0,0 +1,12 @@ +//! The crate's integration tests, linked into one test binary. + +mod builder; +mod corpus; +mod corpus_atn; +mod layer_block_order; +mod malformed; +mod merged_recovery; +mod proptests; +mod roundtrip; +mod swatch_malformed; +mod tree; diff --git a/crates/psd/tests/malformed.rs b/crates/psd/tests/it/malformed.rs similarity index 100% rename from crates/psd/tests/malformed.rs rename to crates/psd/tests/it/malformed.rs diff --git a/crates/psd/tests/merged_recovery.rs b/crates/psd/tests/it/merged_recovery.rs similarity index 100% rename from crates/psd/tests/merged_recovery.rs rename to crates/psd/tests/it/merged_recovery.rs diff --git a/crates/psd/tests/proptests.rs b/crates/psd/tests/it/proptests.rs similarity index 100% rename from crates/psd/tests/proptests.rs rename to crates/psd/tests/it/proptests.rs diff --git a/crates/psd/tests/roundtrip.rs b/crates/psd/tests/it/roundtrip.rs similarity index 99% rename from crates/psd/tests/roundtrip.rs rename to crates/psd/tests/it/roundtrip.rs index 1f23ee6284..49ad412e7c 100644 --- a/crates/psd/tests/roundtrip.rs +++ b/crates/psd/tests/it/roundtrip.rs @@ -232,6 +232,7 @@ fn global_mask_accessors() { assert_eq!(gm.opacity(), Some(50)); assert_eq!(gm.kind(), Some(128)); assert_eq!(GlobalLayerMask::default().kind(), None); + assert_eq!(GlobalLayerMask::photoshop_default().data, gm.data); } #[test] diff --git a/crates/psd/tests/swatch_malformed.rs b/crates/psd/tests/it/swatch_malformed.rs similarity index 97% rename from crates/psd/tests/swatch_malformed.rs rename to crates/psd/tests/it/swatch_malformed.rs index 8fa88e7d68..91a83b4285 100644 --- a/crates/psd/tests/swatch_malformed.rs +++ b/crates/psd/tests/it/swatch_malformed.rs @@ -444,6 +444,32 @@ fn ase_swatch_cap_is_enforced_on_hostile_input() { assert!(ase::parse(&f).is_ok(), "a file exactly at MAX_SWATCHES must still parse"); } +/// A file with more group starts than [`ase::MAX_GROUPS`] must be refused. Each block here is an +/// empty group start, six bytes long. +#[test] +fn ase_group_cap_is_enforced_on_hostile_input() { + let mut block = Vec::new(); + block.extend_from_slice(&0xC001u16.to_be_bytes()); // block type: group start + block.extend_from_slice(&0u32.to_be_bytes()); // empty body: no name + + let total = ase::MAX_GROUPS + 1; + let mut f = b"ASEF".to_vec(); + f.extend_from_slice(&1u16.to_be_bytes()); // major + f.extend_from_slice(&0u16.to_be_bytes()); // minor + f.extend_from_slice(&(total as u32).to_be_bytes()); + for _ in 0..total { + f.extend_from_slice(&block); + } + assert!(matches!(ase::parse(&f), Err(PsdError::LimitExceeded(_))), "a file over MAX_GROUPS must be refused"); + // With one block fewer the file is exactly at the cap and parses. + f.truncate(f.len() - block.len()); + let trimmed = total - 1; + // signature (4) + major (2) + minor (2) puts the block count at offset 8. + f[8..12].copy_from_slice(&(trimmed as u32).to_be_bytes()); + let parsed = ase::parse(&f).expect("a file exactly at MAX_GROUPS must still parse"); + assert_eq!(parsed.entries.len(), ase::MAX_GROUPS, "every group up to the cap is kept"); +} + /// A name longer than [`ase::MAX_NAME_UNITS`] is truncated on read rather than refused: the file /// is legal, only the name is long, and `read_name` is the only thing standing between the declared /// length and the string it builds. diff --git a/crates/psd/tests/tree.rs b/crates/psd/tests/it/tree.rs similarity index 72% rename from crates/psd/tests/tree.rs rename to crates/psd/tests/it/tree.rs index ea4eaeec2a..ef448651fc 100644 --- a/crates/psd/tests/tree.rs +++ b/crates/psd/tests/it/tree.rs @@ -137,6 +137,60 @@ fn layer_tree_max_depth_drops_deeper_children() { assert_eq!(names(&f, &f.layer_tree_max_depth(0)), "g0[],top"); } +fn depth(nodes: &[LayerNode]) -> usize { + // Iterative so a deep tree cannot overflow the test's stack. + let mut max = 0; + let mut todo: Vec<(&LayerNode, usize)> = nodes.iter().map(|n| (n, 1)).collect(); + while let Some((n, d)) = todo.pop() { + max = max.max(d); + todo.extend(n.children().iter().map(|c| (c, d + 1))); + } + max +} + +/// #5758: layers under a divider that is never closed are hoisted to the enclosing level, as +/// `layer_tree` does, also when the cap is shallower than the unclosed divider. +#[test] +fn layer_tree_max_depth_hoists_unclosed_divider_children() { + let f = file_with(vec![rec("a", None), rec("", END), rec("b", None), rec("c", None)]); + assert_eq!(names(&f, &f.layer_tree()), "a,b,c"); + assert_eq!(names(&f, &f.layer_tree_max_depth(1)), "a,b,c"); + assert_eq!(names(&f, &f.layer_tree_max_depth(0)), "a,b,c"); +} + +/// #5758: a closed group inside unclosed dividers sits at the depth it ends up at after hoisting, +/// so the cap keeps its node and drops only its deeper children. +#[test] +fn layer_tree_max_depth_keeps_closed_group_inside_unclosed_dividers() { + let mut v = vec![rec("", END), rec("", END)]; + v.extend([rec("", END), rec("", END), rec("x", None), rec("g1", OPEN), rec("g0", OPEN)]); + let f = file_with(v); + assert_eq!(names(&f, &f.layer_tree()), "g0[g1[x]]"); + assert_eq!(names(&f, &f.layer_tree_max_depth(1)), "g0[g1[]]"); + assert_eq!(names(&f, &f.layer_tree_max_depth(0)), "g0[]"); +} + +/// #5758: thousands of unclosed dividers around a closed chain stay bounded by the cap and keep +/// every layer the full tree has. +#[test] +fn layer_tree_max_depth_bounds_deep_unclosed_chain() { + let mut v = Vec::new(); + for i in 0..16_000 { + v.push(rec("", END)); + v.push(rec(&format!("l{i}"), None)); + } + v.extend([rec("", END), rec("", END), rec("", END), rec("x", None)]); + v.extend([rec("g2", OPEN), rec("g1", OPEN), rec("g0", OPEN)]); + let f = file_with(v); + let full = f.layer_tree(); + assert_eq!(full.len(), 16_001); + assert_eq!(depth(&full), 4); + let capped = f.layer_tree_max_depth(1); + assert!(depth(&capped) <= 2); + assert_eq!(capped.len(), 16_001); + assert_eq!(names(&f, &capped[16_000..]), "g0[g1[]]"); + assert_eq!(names(&f, &capped[..2]), "l0,l1"); +} #[test] fn folder_without_divider_is_empty_group() { let f = file_with(vec![rec("a", None), rec("G", OPEN), rec("b", None)]); diff --git a/crates/raster/Cargo.toml b/crates/raster/Cargo.toml index 956d1c9545..41b188a94f 100644 --- a/crates/raster/Cargo.toml +++ b/crates/raster/Cargo.toml @@ -12,5 +12,9 @@ photocraft-geom = { workspace = true } photocraft-color = { workspace = true } serde = { workspace = true } +# Native only: converting a region's tiles is independent per tile, so it runs on all cores. +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +rayon = { workspace = true } + [dev-dependencies] proptest = { workspace = true } diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs index 20b809764a..af3c6d48e2 100644 --- a/crates/raster/src/lib.rs +++ b/crates/raster/src/lib.rs @@ -14,7 +14,7 @@ use std::collections::BTreeMap; use std::fmt; use std::sync::Arc; -use photocraft_color::{ColorMode, PixelFormat, SampleType, read_sample, write_sample}; +use photocraft_color::{ColorMode, PixelFormat, SampleType, read_sample, read_samples, write_sample}; use photocraft_geom::{Rect, TILE_SIZE, TileCoord}; /// A checked surface allocation could not be represented or reserved. @@ -35,6 +35,24 @@ impl fmt::Display for AllocationError { impl std::error::Error for AllocationError {} +/// One shared tile per pixel value (and tile size), for [`Surface::share_uniform_tiles`]. +#[derive(Debug, Default)] +pub struct TileInterner { + tiles: std::collections::HashMap, Arc>>, +} + +impl TileInterner { + /// Number of distinct uniform tiles held. + pub fn len(&self) -> usize { + self.tiles.values().map(|m| m.len()).sum() + } + + /// `true` when no tile is held. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } +} + /// Pixel storage for one tile: `TILE_SIZE² × bytes_per_pixel`, row-major, interleaved channels. #[derive(Clone, Debug, PartialEq, Eq)] pub struct Tile { @@ -97,12 +115,17 @@ impl Tile { return None; } let ts = TILE_SIZE as usize; + // A row of `dp`, compared block by block: an untouched row (most rows of a soft brush + // stroke's tiles) costs a few slice compares instead of one per pixel. A pixel differs + // where any of its bytes does, so the first and last differing bytes give the first and + // last differing pixels. + let blank = dp.repeat(ts); let (mut x0, mut y0, mut x1, mut y1) = (usize::MAX, usize::MAX, 0usize, 0usize); for (y, row) in self.data.chunks_exact(ts * bpp).enumerate() { - let Some(first) = row.chunks_exact(bpp).position(|px| px != dp) else { continue }; - let last = ts - 1 - row.chunks_exact(bpp).rev().position(|px| px != dp).unwrap_or(0); - x0 = x0.min(first); - x1 = x1.max(last + 1); + let Some(first) = first_difference(row, &blank) else { continue }; + let Some(last) = last_difference(row, &blank) else { continue }; + x0 = x0.min(first / bpp); + x1 = x1.max(last / bpp + 1); y0 = y0.min(y); y1 = y + 1; } @@ -110,6 +133,21 @@ impl Tile { } } +/// Bytes compared at once while looking for a difference in a tile row. +const DIFF_BLOCK: usize = 64; + +/// Index of the first byte where `a` and `b` (the same length) differ. +fn first_difference(a: &[u8], b: &[u8]) -> Option { + let (k, (x, y)) = a.chunks(DIFF_BLOCK).zip(b.chunks(DIFF_BLOCK)).enumerate().find(|(_, (x, y))| x != y)?; + x.iter().zip(y).position(|(p, q)| p != q).map(|i| k * DIFF_BLOCK + i) +} + +/// Index of the last byte where `a` and `b` (the same length) differ. +fn last_difference(a: &[u8], b: &[u8]) -> Option { + let (k, (x, y)) = a.chunks(DIFF_BLOCK).zip(b.chunks(DIFF_BLOCK)).enumerate().rfind(|(_, (x, y))| x != y)?; + x.iter().zip(y).rposition(|(p, q)| p != q).map(|i| k * DIFF_BLOCK + i) +} + #[derive(Clone, Debug)] pub struct Surface { format: PixelFormat, @@ -117,24 +155,33 @@ pub struct Surface { tiles: BTreeMap>, } +/// Use stack storage when it fits, otherwise reuse a dynamically sized sample buffer. +fn sample_buffer<'a>(channels: usize, stack: &'a mut [f32], heap: &'a mut Vec) -> &'a mut [f32] { + if let Some(samples) = stack.get_mut(..channels) { + samples + } else { + heap.resize(channels, 0.0); + heap + } +} + /// Tiles of one pixel value met while moving a surface ([`Surface::translated`]): each scanned /// once, and one tile kept per value so the targets share it. #[derive(Default)] struct UniformTiles { /// Per source tile (by address): the index of its value's tile in `canonical`, or `None`. - of: std::collections::HashMap<*const Tile, Option>, + of: std::collections::HashMap>, canonical: Vec>, } impl UniformTiles { /// The tile to share for a target reading window `r` of `s`, when every tile under it holds /// one and the same pixel value (`r` must lie on existing tiles). - fn window(&mut self, s: &Surface, r: Rect) -> Option> { - let bpp = s.format.bytes_per_pixel(); + fn window(&self, s: &Surface, r: Rect) -> Option> { let mut found = None; for c in r.tiles() { let t = s.tiles.get(&c)?; - let k = self.value_of(t, bpp)?; + let k = (*self.of.get(&(Arc::as_ptr(t) as usize))?)?; if found.is_some_and(|f| f != k) { return None; } @@ -144,7 +191,8 @@ impl UniformTiles { } fn value_of(&mut self, t: &Arc, bpp: usize) -> Option { - if let Some(k) = self.of.get(&Arc::as_ptr(t)) { + let key = Arc::as_ptr(t) as usize; + if let Some(k) = self.of.get(&key) { return *k; } let mut px = t.data.chunks_exact(bpp); @@ -155,7 +203,7 @@ impl UniformTiles { self.canonical.len() - 1 } }); - self.of.insert(Arc::as_ptr(t), k); + self.of.insert(key, k); k } } @@ -166,6 +214,105 @@ impl PartialEq for Surface { } } +/// Converts the `data` span covering one tile into that tile's bytes. +fn write_tile(t: &mut Tile, tc: TileCoord, r: Rect, data: &[f32], w: usize, n: usize, sample: SampleType) { + let tr = tc.rect().intersect(&r); + // Convert contiguous channel samples a row at a time. Dispatching on depth here removes the + // per-pixel channel loop and lets the compiler vectorise conversion. + let span = tr.width() as usize * n; + for y in tr.y0..tr.y1 { + let o = (((y - r.y0) as usize) * w + (tr.x0 - r.x0) as usize) * n; + let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (tr.x0 - tc.tx * TILE_SIZE) as usize) * n; + let src = &data[o..o + span]; + let bytes = sample.bytes(); + let dst = &mut t.data[base * bytes..(base + span) * bytes]; + match sample { + SampleType::U8 => { + for (d, v) in dst.iter_mut().zip(src) { + *d = (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + } + } + SampleType::U16 => { + for (d, v) in dst.as_chunks_mut::<2>().0.iter_mut().zip(src) { + let q = (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16; + d.copy_from_slice(&q.to_ne_bytes()); + } + } + SampleType::F32 => { + for (d, v) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src) { + d.copy_from_slice(&v.to_ne_bytes()); + } + } + } + } +} + +/// Converts the tiles of a region write. A tile holds no state another tile reads, so the tiles +/// are converted on all cores; each writes only its own bytes, in the same arithmetic. +#[cfg(not(target_arch = "wasm32"))] +fn convert_tiles(tiles: &mut [(TileCoord, Arc)], r: Rect, data: &[f32], w: usize, n: usize, sample: SampleType) { + use rayon::prelude::*; + tiles.par_iter_mut().for_each(|(tc, t)| write_tile(Arc::make_mut(t), *tc, r, data, w, n, sample)); +} + +/// Serial counterpart, for the web build (there are no threads to spread tiles over). +#[cfg(target_arch = "wasm32")] +fn convert_tiles(tiles: &mut [(TileCoord, Arc)], r: Rect, data: &[f32], w: usize, n: usize, sample: SampleType) { + for (tc, t) in tiles.iter_mut() { + write_tile(Arc::make_mut(t), *tc, r, data, w, n, sample); + } +} + +fn decode_tile_row_rgba(fmt: &PixelFormat, t: &Tile, base: usize, dst: &mut [[f32; 4]], row: &mut Vec) { + let n = fmt.channels(); + match (fmt.mode, fmt.sample, fmt.alpha) { + (ColorMode::Rgb, SampleType::U8, true) => { + let src = &t.data[base..base + dst.len() * 4]; + for (d, s) in dst.iter_mut().zip(src.as_chunks::<4>().0) { + *d = [s[0] as f32 / 255.0, s[1] as f32 / 255.0, s[2] as f32 / 255.0, s[3] as f32 / 255.0]; + } + } + (ColorMode::Grayscale, SampleType::U8, true) => { + let src = &t.data[base..base + dst.len() * 2]; + for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { + let g = s[0] as f32 / 255.0; + *d = [g, g, g, s[1] as f32 / 255.0]; + } + } + (ColorMode::Cmyk, SampleType::U8, _) => { + // Identical ink bytes share the exact ICC result; alpha is independent. + let mut previous: Option<([u8; 4], [f32; 3])> = None; + let src = &t.data[base..base + dst.len() * n]; + for (d, s) in dst.iter_mut().zip(src.chunks_exact(n)) { + let ink = [s[0], s[1], s[2], s[3]]; + let rgb = match previous { + Some((key, rgb)) if key == ink => rgb, + _ => { + let rgb = photocraft_color::convert::cmyk_to_rgb(ink.map(|v| v as f32 / 255.0)); + previous = Some((ink, rgb)); + rgb + } + }; + *d = [rgb[0], rgb[1], rgb[2], s.get(4).map_or(1.0, |v| *v as f32 / 255.0)]; + } + } + _ => { + // The row's samples in one pass, then its colour model resolved once: + // RGB and grey take their channels as `to_rgba` does, other models go + // through it. + row.resize(dst.len() * n, 0.0); + read_samples(&t.data, fmt.sample, base, row); + let a = |s: &[f32], i: usize| if fmt.alpha { s[i] } else { 1.0 }; + let pixels = dst.iter_mut().zip(row.chunks_exact(n)); + match fmt.mode { + ColorMode::Rgb => pixels.for_each(|(d, s)| *d = [s[0], s[1], s[2], a(s, 3)]), + ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => pixels.for_each(|(d, s)| *d = [s[0], s[0], s[0], a(s, 1)]), + _ => pixels.for_each(|(d, s)| *d = to_rgba(fmt, s)), + } + } + } +} + impl Surface { /// Reads a region into a newly allocated buffer, returning an error instead of aborting if /// the requested pixel buffer cannot be represented or reserved. @@ -239,11 +386,20 @@ impl Surface { decode_pixel(&self.format, &self.default_pixel) } + /// Decode the default pixel, using dynamic storage only when the stack buffer is too small. + fn default_samples<'a>(&self, stack: &'a mut [f32], heap: &'a mut Vec) -> &'a mut [f32] { + let px = sample_buffer(self.channels(), stack, heap); + for (i, v) in px.iter_mut().enumerate() { + *v = read_sample(&self.default_pixel, self.format.sample, i); + } + px + } + /// Mutable access to a tile, allocating it (filled with the default pixel) or un-sharing it. pub fn tile_mut(&mut self, c: TileCoord) -> &mut Tile { let fmt = self.format; - let dp = self.default_pixel.clone(); - let arc = self.tiles.entry(c).or_insert_with(|| Arc::new(Tile::filled(&fmt, &dp))); + let dp = &self.default_pixel; + let arc = self.tiles.entry(c).or_insert_with(|| Arc::new(Tile::filled(&fmt, dp))); Arc::make_mut(arc) } @@ -253,10 +409,20 @@ impl Surface { if !self.tiles.contains_key(&c) { let tile = Arc::new(Tile::try_filled(&self.format, &self.default_pixel)?); self.tiles.insert(c, tile); - } else if self.tiles.get(&c).is_some_and(|t| Arc::strong_count(t) > 1) { - let original = self.tiles.get(&c).ok_or(AllocationError::SizeOverflow)?; - let tile = Arc::new(original.try_clone()?); - self.tiles.insert(c, tile); + } else if self.tiles.get_mut(&c).is_some_and(|t| Arc::get_mut(t).is_none()) { + let original = self.tiles.remove(&c).ok_or(AllocationError::SizeOverflow)?; + let tile = match Arc::try_unwrap(original) { + // Weak cache references must not force a pixel-buffer copy. + Ok(tile) => tile, + Err(original) => match original.try_clone() { + Ok(tile) => tile, + Err(error) => { + self.tiles.insert(c, original); + return Err(error); + } + }, + }; + self.tiles.insert(c, Arc::new(tile)); } self.tiles.get_mut(&c).and_then(Arc::get_mut).ok_or(AllocationError::SizeOverflow) } @@ -317,11 +483,33 @@ impl Surface { out } + /// Makes every tile that holds one pixel value throughout share the interner's tile for that + /// value: a white background, an opaque mask or a solid fill then costs one tile however + /// large it is, and across every surface that shares `interner`. Tiles are copy-on-write, so + /// editing one splits it off again. Returns the bytes no longer held by this surface alone. + pub fn share_uniform_tiles(&mut self, interner: &mut TileInterner) -> usize { + let bpp = self.format.bytes_per_pixel(); + let mut saved = 0; + for t in self.tiles.values_mut() { + let mut px = t.data.chunks_exact(bpp); + let Some(p0) = px.next() else { continue }; + if !px.all(|p| p == p0) { + continue; + } + let shared = interner.tiles.entry(t.data.len()).or_default().entry(p0.to_vec()).or_insert_with(|| t.clone()); + if !Arc::ptr_eq(shared, t) { + saved += t.data.len(); + *t = shared.clone(); + } + } + saved + } + /// Drop tiles that are entirely the default pixel. pub fn prune(&mut self) { - let dp = self.default_pixel.clone(); + let dp = self.default_pixel.as_ref(); let bpp = dp.len(); - self.tiles.retain(|_, t| t.data.chunks_exact(bpp).any(|px| px != &*dp)); + self.tiles.retain(|_, t| t.data.chunks_exact(bpp).any(|px| px != dp)); } #[inline] @@ -378,30 +566,37 @@ impl Surface { /// Channel `c` at `(x0 + i·step, y)` for `i in 0..out.len()`, looking each tile up once per run /// (much faster than per-sample [`Surface::sample_channel`] for strided scans). + /// Steps below 1 read consecutive pixels; positions beyond `i32::MAX` read the default pixel. pub fn sample_row_strided(&self, y: i32, x0: i32, step: i32, c: usize, out: &mut [f32]) { - let step = step.max(1); + let step = i64::from(step.max(1)); let dp = read_sample(&self.default_pixel, self.format.sample, c); let n = self.format.channels(); let ty = y.div_euclid(TILE_SIZE); let ly = y.rem_euclid(TILE_SIZE) as usize; let mut i = 0; + let mut x = i64::from(x0); while i < out.len() { - let x = x0 + i as i32 * step; - let tx = x.div_euclid(TILE_SIZE); + if x > i64::from(i32::MAX) { + out[i..].fill(dp); + break; + } + let tx = x.div_euclid(i64::from(TILE_SIZE)); // Samples that fall in this tile. - let tile_end = (tx + 1) * TILE_SIZE; + let tile_end = (tx + 1) * i64::from(TILE_SIZE); let count = (((tile_end - x) + step - 1) / step).max(1) as usize; - let end = (i + count).min(out.len()); - match self.tiles.get(&TileCoord { tx, ty }) { + let count = count.min(out.len() - i); + let end = i + count; + match self.tiles.get(&TileCoord { tx: tx as i32, ty }) { None => out[i..end].fill(dp), Some(t) => { for (k, o) in out[i..end].iter_mut().enumerate() { - let lx = (x + k as i32 * step - tx * TILE_SIZE) as usize; + let lx = (x + k as i64 * step - tx * i64::from(TILE_SIZE)) as usize; *o = read_sample(&t.data, self.format.sample, (ly * TILE_SIZE as usize + lx) * n + c); } } } i = end; + x += count as i64 * step; } } @@ -411,24 +606,36 @@ impl Surface { let w = r.width() as usize; out.clear(); out.resize(w * r.height() as usize * n, 0.0); - let dp = self.default_pixel(); + self.read_samples_into(r, out); + } + + /// Copy samples into a zero-initialized buffer sized for `r`. + fn read_samples_into(&self, r: Rect, out: &mut [f32]) { + let n = self.channels(); + let w = r.width() as usize; + let mut stack = [0.0; 8]; + let mut heap = Vec::new(); + let dp = self.default_samples(&mut stack, &mut heap); + let zero = self.default_pixel.iter().all(|&b| b == 0); for tc in r.tiles() { let tr = tc.rect().intersect(&r); let tile = self.tiles.get(&tc); + if tile.is_none() && zero { + continue; + } for y in tr.y0..tr.y1 { let o = (((y - r.y0) as usize) * w + (tr.x0 - r.x0) as usize) * n; let span = tr.width() as usize * n; - let dst = &mut out[o..o + span]; + let Some(dst) = out.get_mut(o..o + span) else { continue }; match tile { Some(t) => { let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (tr.x0 - tc.tx * TILE_SIZE) as usize) * n; - for (i, d) in dst.iter_mut().enumerate() { - *d = read_sample(&t.data, self.format.sample, base + i); - } + read_samples(&t.data, self.format.sample, base, dst); } + None if n == 1 => dst.fill(dp[0]), None => { for px in dst.chunks_exact_mut(n) { - px.copy_from_slice(&dp); + px.copy_from_slice(dp); } } } @@ -445,7 +652,10 @@ impl Surface { debug_assert_eq!(out.len(), w * r.height() as usize); let fmt = self.format; let n = fmt.channels(); - let dp = to_rgba(&fmt, &self.default_pixel()); + let mut stack = [0.0; 8]; + let mut heap = Vec::new(); + let dp = to_rgba(&fmt, self.default_samples(&mut stack, &mut heap)); + let mut row = Vec::new(); for tc in r.tiles() { let tr = tc.rect().intersect(&r); let tile = self.tiles.get(&tc); @@ -457,47 +667,7 @@ impl Surface { continue; }; let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (tr.x0 - tc.tx * TILE_SIZE) as usize) * n; - match (fmt.mode, fmt.sample, fmt.alpha) { - (ColorMode::Rgb, SampleType::U8, true) => { - let src = &t.data[base..base + dst.len() * 4]; - for (d, s) in dst.iter_mut().zip(src.as_chunks::<4>().0) { - *d = [s[0] as f32 / 255.0, s[1] as f32 / 255.0, s[2] as f32 / 255.0, s[3] as f32 / 255.0]; - } - } - (ColorMode::Grayscale, SampleType::U8, true) => { - let src = &t.data[base..base + dst.len() * 2]; - for (d, s) in dst.iter_mut().zip(src.as_chunks::<2>().0) { - let g = s[0] as f32 / 255.0; - *d = [g, g, g, s[1] as f32 / 255.0]; - } - } - (ColorMode::Cmyk, SampleType::U8, _) => { - // Identical ink bytes share the exact ICC result; alpha is independent. - let mut previous: Option<([u8; 4], [f32; 3])> = None; - let src = &t.data[base..base + dst.len() * n]; - for (d, s) in dst.iter_mut().zip(src.chunks_exact(n)) { - let ink = [s[0], s[1], s[2], s[3]]; - let rgb = match previous { - Some((key, rgb)) if key == ink => rgb, - _ => { - let rgb = photocraft_color::convert::cmyk_to_rgb(ink.map(|v| v as f32 / 255.0)); - previous = Some((ink, rgb)); - rgb - } - }; - *d = [rgb[0], rgb[1], rgb[2], s.get(4).map_or(1.0, |v| *v as f32 / 255.0)]; - } - } - _ => { - let mut px = [0.0f32; 8]; - for (i, d) in dst.iter_mut().enumerate() { - for (c, v) in px.iter_mut().enumerate().take(n) { - *v = read_sample(&t.data, fmt.sample, base + i * n + c); - } - *d = to_rgba(&fmt, &px[..n]); - } - } - } + decode_tile_row_rgba(&fmt, t, base, dst, &mut row); } } } @@ -510,7 +680,9 @@ impl Surface { let fmt = self.format; let rgba8 = matches!((fmt.mode, fmt.sample, fmt.alpha), (ColorMode::Rgb, SampleType::U8, true)); let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - let dp = to_rgba(&fmt, &self.default_pixel()); + let mut stack = [0.0; 8]; + let mut heap = Vec::new(); + let dp = to_rgba(&fmt, self.default_samples(&mut stack, &mut heap)); let dp8 = [q(dp[0]), q(dp[1]), q(dp[2]), q(dp[3])]; let mut row = Vec::new(); for tc in r.tiles() { @@ -551,68 +723,43 @@ impl Surface { let n = self.channels(); let w = r.width() as usize; let mut out = vec![0.0f32; w * r.height() as usize * n]; - let dp = self.default_pixel(); - for tc in r.tiles() { - let tr = tc.rect().intersect(&r); - let tile = self.tiles.get(&tc); - for y in tr.y0..tr.y1 { - for x in tr.x0..tr.x1 { - let o = (((y - r.y0) as usize) * w + (x - r.x0) as usize) * n; - match tile { - Some(t) => { - let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (x - tc.tx * TILE_SIZE) as usize) * n; - for i in 0..n { - out[o + i] = read_sample(&t.data, self.format.sample, base + i); - } - } - None => out[o..o + n].copy_from_slice(&dp), - } - } - } - } + self.read_samples_into(r, &mut out); out } - /// Write interleaved normalised floats into a rectangle. + /// Write interleaved normalised floats into a rectangle, on the calling thread. + /// + /// This never enters Rayon, so it is safe inside a `OnceLock` initializer or any other + /// section that must not run another Rayon job on its stack. [`Surface::write_region_par`] + /// writes the same bytes on all cores. pub fn write_region(&mut self, r: Rect, data: &[f32]) { let n = self.channels(); let w = r.width() as usize; assert_eq!(data.len(), w * r.height() as usize * n, "region data length mismatch"); let sample = self.format.sample; for tc in r.tiles() { - let tr = tc.rect().intersect(&r); - let t = self.tile_mut(tc); - // Convert contiguous channel samples a row at a time. Dispatching on depth here - // removes the per-pixel channel loop and lets the compiler vectorise conversion. - let span = tr.width() as usize * n; - for y in tr.y0..tr.y1 { - let o = (((y - r.y0) as usize) * w + (tr.x0 - r.x0) as usize) * n; - let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (tr.x0 - tc.tx * TILE_SIZE) as usize) * n; - let src = &data[o..o + span]; - let bytes = sample.bytes(); - let dst = &mut t.data[base * bytes..(base + span) * bytes]; - match sample { - SampleType::U8 => { - for (d, v) in dst.iter_mut().zip(src) { - *d = (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; - } - } - SampleType::U16 => { - for (d, v) in dst.as_chunks_mut::<2>().0.iter_mut().zip(src) { - let q = (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16; - d.copy_from_slice(&q.to_ne_bytes()); - } - } - SampleType::F32 => { - for (d, v) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src) { - d.copy_from_slice(&v.to_ne_bytes()); - } - } - } - } + write_tile(self.tile_mut(tc), tc, r, data, w, n, sample); } } + /// [`Surface::write_region`] with the tiles converted on all cores (serially on the web). + /// + /// This enters Rayon: while it waits for its tiles, the calling worker may run any other + /// queued Rayon job. Callers inside a `OnceLock` initializer or another section that must + /// not do that use [`Surface::write_region`]. + pub fn write_region_par(&mut self, r: Rect, data: &[f32]) { + let n = self.channels(); + let w = r.width() as usize; + assert_eq!(data.len(), w * r.height() as usize * n, "region data length mismatch"); + let sample = self.format.sample; + // The tiles are disjoint, so converting them in any order (and on any thread) writes + // exactly the same bytes. `take_tiles` gives the missing tiles as one shared + // default-filled tile, copied per tile by `Arc::make_mut` in `convert_tiles`. + let mut tiles = self.take_tiles(r); + convert_tiles(&mut tiles, r, data, w, n, sample); + self.put_tiles(tiles); + } + /// Take the tiles covering `r` out of the surface (missing ones filled with the default /// pixel), so they can be edited elsewhere, e.g. in parallel: `Arc::unwrap_or_clone` copies a /// tile only if an undo snapshot still shares it. Give them back with [`Surface::put_tiles`]. @@ -630,7 +777,9 @@ impl Surface { /// Fallible counterpart to [`Surface::take_tiles`]. Reserves the complete result and any /// default tile before removing a tile, so a reservation failure leaves the surface intact. pub fn try_take_tiles(&mut self, r: Rect) -> Result)>, AllocationError> { - let count = r.tiles().count(); + // Counted in u64 and checked before narrowing: on 32-bit targets the grid of a sparse + // surface's bounds can pass `usize` (and visiting it to count it takes just as long). + let count = usize::try_from(r.tile_count()).map_err(|_| AllocationError::SizeOverflow)?; let bytes = count.checked_mul(std::mem::size_of::<(TileCoord, Arc)>()).ok_or(AllocationError::SizeOverflow)?; let mut out = Vec::new(); out.try_reserve_exact(count).map_err(|_| AllocationError::NotEnoughMemory { bytes })?; @@ -769,6 +918,9 @@ impl Surface { let moved = content.translate(dx, dy); let targets: std::collections::BTreeSet = sources().flat_map(|(c, _)| c.rect().translate(dx, dy).intersect(&moved).tiles()).collect(); let mut uniform = UniformTiles::default(); + for (_, t) in sources() { + uniform.value_of(t, bpp); + } // The fill row: one tile row of the default pixel, so the regions a shift vacates // fill with bulk copies (empty when the default is zero — the tile starts zeroed). @@ -785,7 +937,11 @@ impl Surface { // One tile row of the default pixel, to compare a finished tile with a row at a time. let default_row = self.default_pixel.repeat(TILE_SIZE.unsigned_abs() as usize); - for dc in targets { + // Target tiles rebuilt from the same source tiles at the same offset are the same tile: + // a bundle can point thousands of tile references at one payload, and a move would + // otherwise copy it into a separate buffer for each. + let built: std::sync::Mutex, Option>>> = Default::default(); + let build_target = |dc: TileCoord| -> Option<(TileCoord, Arc)> { let dr = dc.rect(); let sr = dr.translate(dx.saturating_neg(), dy.saturating_neg()); // Every pixel of the target comes from a source tile (none from the default pixel). @@ -797,10 +953,15 @@ impl Surface { && sr.tiles().all(|sc| self.tiles.contains_key(&sc)); if covered && let Some(shared) = uniform.window(self, sr) { // A tile of only the default pixel is left out, as `prune` would. - if shared.data.get(..bpp) != Some(&*self.default_pixel) { - out.tiles.insert(dc, shared); - } - continue; + return (shared.data.get(..bpp) != Some(&*self.default_pixel)).then_some((dc, shared)); + } + // The identities of the source tiles under a covered window (the offset is the same + // for every target of this call). + let memo_key: Option> = covered.then(|| sr.tiles().map(|sc| self.tiles.get(&sc).map_or(0, |t| Arc::as_ptr(t) as usize)).collect()); + if let Some(k) = &memo_key + && let Some(hit) = built.lock().unwrap_or_else(std::sync::PoisonError::into_inner).get(k) + { + return hit.clone().map(|t| (dc, t)); } let mut tile = Tile { data: vec![0u8; (TILE_SIZE * TILE_SIZE) as usize * bpp].into_boxed_slice() }; @@ -882,9 +1043,23 @@ impl Surface { } // A target that got only default pixels isn't stored, as `prune` would drop it. Checked // after the copy, a row at a time: a tile with content stops at its first such row. - if tile.data.chunks(default_row.len()).any(|row| row != default_row) { - out.tiles.insert(dc, Arc::new(tile)); - } + let finished = tile.data.chunks(default_row.len()).any(|row| row != default_row).then(|| Arc::new(tile)); + // Workers that missed the memo at the same time each built a copy: all of them take + // the one stored first, so the targets share it however many threads raced. + let finished = match memo_key { + Some(k) => built.lock().unwrap_or_else(std::sync::PoisonError::into_inner).entry(k).or_insert(finished).clone(), + None => finished, + }; + finished.map(|t| (dc, t)) + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + out.tiles = targets.into_par_iter().filter_map(build_target).collect(); + } + #[cfg(target_arch = "wasm32")] + { + out.tiles = targets.into_iter().filter_map(build_target).collect(); } out } @@ -989,7 +1164,8 @@ impl Surface { /// Composite-friendly RGBA read (converts from the surface's model). pub fn rgba(&self, x: i32, y: i32) -> [f32; 4] { - let p = self.pixel(x, y); + let mut p = [0.0f32; 5]; + self.read_pixel(x, y, &mut p); to_rgba(&self.format, &p) } } @@ -1101,515 +1277,7 @@ pub fn is_hdr(sample: SampleType) -> bool { } #[cfg(test)] -mod tests { - /// Distinct tile allocations in `s`. - fn distinct_tiles(s: &Surface) -> usize { - let mut seen: Vec<*const Tile> = s.tiles().map(|(_, t)| Arc::as_ptr(t)).collect(); - seen.sort(); - seen.dedup(); - seen.len() - } - - /// Converting or tone-mapping a surface keeps a shared tile shared (#4531) and gives the - /// same pixels as converting each pixel: a filled background is one tile across the grid, - /// plus some distinct tiles. - #[test] - fn conversion_keeps_shared_tiles_shared() { - let mut s = Surface::new(PixelFormat::RGBA8); - s.fill_rect(Rect::new(0, 0, 2048, 1024), &[0.9, 0.5, 0.2, 1.0]); - s.fill_rect(Rect::new(10, 10, 20, 20), &[0.1, 0.2, 0.3, 0.5]); - let shared_before = distinct_tiles(&s); - assert!(shared_before <= 2 && s.tile_count() == 32, "{shared_before} distinct of {}", s.tile_count()); - let r = Rect::new(0, 0, 2048, 1024); - for to in [PixelFormat::RGBA16, PixelFormat::RGBA32F] { - for out in [s.convert(to), s.try_convert(to).expect("memory")] { - assert_eq!(distinct_tiles(&out), shared_before, "{to:?}: still shared"); - assert_eq!(out.tile_count(), 32); - let (a, b) = (out.read_region(r), s.read_region(r)); - for (pa, pb) in a.as_chunks::<4>().0.iter().zip(b.as_chunks::<4>().0) { - let want = convert_pixel(&s.format(), &to, pb); - assert!(pa.iter().zip(&want).all(|(x, y)| (x - y).abs() < 1e-6), "{to:?}: {pa:?} vs {want:?}"); - } - } - } - let mut m = s.clone(); - m.map_color(|v| v * 0.5); - assert_eq!(distinct_tiles(&m), shared_before, "map_color keeps it shared"); - assert!((m.pixel(1000, 500)[0] - s.pixel(1000, 500)[0] * 0.5).abs() < 1.0 / 255.0); - assert_eq!(s.pixel(1000, 500), vec![s.pixel(1000, 500)[0], s.pixel(1000, 500)[1], s.pixel(1000, 500)[2], 1.0], "the source is untouched"); - } - - use super::*; - use photocraft_color::PixelFormat; - use proptest::prelude::*; - - fn write_region_reference(surface: &mut Surface, r: Rect, data: &[f32]) { - let n = surface.channels(); - let w = r.width() as usize; - assert_eq!(data.len(), w * r.height() as usize * n, "region data length mismatch"); - let sample = surface.format.sample; - for tc in r.tiles() { - let tr = tc.rect().intersect(&r); - let t = surface.tile_mut(tc); - for y in tr.y0..tr.y1 { - for x in tr.x0..tr.x1 { - let o = (((y - r.y0) as usize) * w + (x - r.x0) as usize) * n; - let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (x - tc.tx * TILE_SIZE) as usize) * n; - for i in 0..n { - write_sample(&mut t.data, sample, base + i, data[o + i]); - } - } - } - } - } - - #[test] - fn region_writes_match_scalar_bytes_for_every_format() { - let special = [ - 0.0, - -0.0, - 1.0, - -1.0, - 2.0, - f32::INFINITY, - f32::NEG_INFINITY, - f32::NAN, - f32::from_bits(0x7fc12345), - f32::from_bits(0xffc12345), - f32::MIN_POSITIVE, - f32::from_bits(1), - 0.5 / 255.0, - 0.5 / 65535.0, - ]; - for mode in [ - ColorMode::Bitmap, - ColorMode::Grayscale, - ColorMode::Indexed, - ColorMode::Rgb, - ColorMode::Cmyk, - ColorMode::Lab, - ColorMode::Multichannel, - ColorMode::Duotone, - ] { - for sample in SampleType::ALL { - for alpha in [false, true] { - let fmt = PixelFormat::new(mode, sample, alpha); - let mut actual = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); - actual.write_pixel(900, 900, &vec![0.75; fmt.channels()]); - let mut expected = actual.clone(); - for rect in - [Rect::EMPTY, Rect::new(-270, -2, 270, 3), Rect::new(256, 256, 512, 257), Rect::new(-64, -64, 0, 0), Rect::new(255, 250, 258, 270)] - { - let data: Vec<_> = (0..rect.width() as usize * rect.height() as usize * fmt.channels()) - .map(|i| { - if i % 3 == 0 { - special[i / 3 % special.len()] - } else { - // Integer rounding boundaries and deterministic arbitrary f32 bit patterns. - let bits = (i as u32).wrapping_mul(2654435761); - if i % 3 == 1 { ((bits % 65536) as f32 + 0.5) / 65535.0 } else { f32::from_bits(bits) } - } - }) - .collect(); - let snapshot = actual.clone(); - let snapshot_bytes = snapshot.to_interleaved(rect.inflate(1)); - actual.write_region(rect, &data); - write_region_reference(&mut expected, rect, &data); - assert_eq!(actual, expected, "{fmt:?}, {rect:?}"); - assert_eq!(snapshot.to_interleaved(rect.inflate(1)), snapshot_bytes, "COW snapshot changed"); - // Repeat against already materialised, unshared tiles too. - actual.write_region(rect, &data); - assert_eq!(actual, expected); - } - } - } - } - } - - #[test] - fn from_rgba_into_matches_from_rgba() { - for fmt in [PixelFormat::RGBA8, PixelFormat::GRAY8, PixelFormat::GRAYA8, PixelFormat::CMYKA8, PixelFormat::new(ColorMode::Lab, SampleType::F32, true)] { - let mut buf = [0.0f32; 8]; - let n = from_rgba_into(&fmt, [0.2, 0.5, 0.7, 0.4], &mut buf); - assert_eq!(&buf[..n], &from_rgba(&fmt, [0.2, 0.5, 0.7, 0.4])[..]); - } - } - - #[test] - fn zero_alloc_accessors_match_read_region() { - for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAYA8, PixelFormat::CMYKA8, PixelFormat::GRAY8] { - let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); - let r = Rect::new(-300, -20, 300, 40); - for (i, (x, y)) in [(-299, -19), (0, 0), (255, 39), (256, 10), (299, 0)].into_iter().enumerate() { - let px: Vec = (0..fmt.channels()).map(|c| ((i * 3 + c) % 5) as f32 / 4.0).collect(); - s.write_pixel(x, y, &px); - } - let region = s.read_region(r); - let mut into = vec![9.0; 3]; - s.read_region_into(r, &mut into); - assert_eq!(region, into); - let mut rgba = vec![[0.0; 4]; (r.width() * r.height()) as usize]; - s.read_rgba_into(r, &mut rgba); - let n = fmt.channels(); - for (i, p) in region.chunks_exact(n).enumerate() { - let want = to_rgba(&fmt, p); - for c in 0..4 { - assert!((rgba[i][c] - want[c]).abs() < 1e-6, "{fmt:?} px {i}"); - } - let (x, y) = (r.x0 + (i % r.width() as usize) as i32, r.y0 + (i / r.width() as usize) as i32); - assert_eq!(s.sample_channel(x, y, 0), p[0]); - } - assert!(s.has_tiles_in(Rect::new(0, 0, 1, 1))); - assert!(!s.has_tiles_in(Rect::new(1000, 1000, 1001, 1001))); - } - } - - #[test] - fn fallible_region_reads_report_size_overflow() { - let surface = Surface::new(PixelFormat::RGBA8); - let huge = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); - assert_eq!(surface.try_read_region(huge), Err(AllocationError::SizeOverflow)); - assert_eq!(surface.try_to_interleaved(huge), Err(AllocationError::SizeOverflow)); - } - - #[test] - fn fallible_region_write_rejects_bad_input_without_mutating_surface() { - let mut surface = Surface::new(PixelFormat::RGBA8); - let before = surface.clone(); - assert_eq!(surface.try_write_region(Rect::new(0, 0, 2, 2), &[1.0]), Err(AllocationError::SizeOverflow)); - assert_eq!(surface, before); - } - - #[test] - fn fill_rect_shares_covered_tiles_and_edits_stay_local() { - // A 300000² background used to materialize ~360 GB of tiles (#705). - let mut s = Surface::new(PixelFormat::RGBA8); - s.fill_rect(Rect::new(0, 0, 300_000, 300_000), &[1.0, 0.5, 0.0, 1.0]); - let t = |s: &Surface, x, y| s.tile(TileCoord::new(x, y)).unwrap().clone(); - assert!(Arc::ptr_eq(&t(&s, 0, 0), &t(&s, 1000, 900))); - // 300000 is not a multiple of 256: the edge tile is partial and stays its own. - let edge = 300_000 / TILE_SIZE; - assert!(!Arc::ptr_eq(&t(&s, 0, 0), &t(&s, edge, 0))); - assert_eq!(s.pixel(299_999, 299_999), vec![1.0, 128.0 / 255.0, 0.0, 1.0]); - assert_eq!(s.pixel(300_000, 0), vec![0.0; 4]); - s.write_pixel(5, 5, &[0.0, 0.0, 1.0, 1.0]); - assert_eq!(s.pixel(5, 5), vec![0.0, 0.0, 1.0, 1.0]); - assert_eq!(s.pixel(300, 5), vec![1.0, 128.0 / 255.0, 0.0, 1.0]); - assert!(!Arc::ptr_eq(&t(&s, 0, 0), &t(&s, 1, 0))); - } - - #[test] - fn empty_surface_reads_default() { - let s = Surface::new(PixelFormat::RGBA8); - assert_eq!(s.pixel(10, -500), vec![0.0; 4]); - let m = Surface::with_default(PixelFormat::GRAY8, &[1.0]); - assert_eq!(m.pixel(123_456, -9), vec![1.0]); - assert_eq!(m.tile_count(), 0); - } - - #[test] - fn write_read_pixel_every_depth() { - for sample in SampleType::ALL { - let f = PixelFormat::RGBA8.with_sample(sample); - let mut s = Surface::new(f); - s.write_pixel(-3, 700, &[0.2, 0.4, 0.6, 1.0]); - let p = s.pixel(-3, 700); - for (a, b) in p.iter().zip([0.2, 0.4, 0.6, 1.0]) { - assert!((a - b).abs() <= 1.0 / 255.0, "{sample:?}"); - } - assert_eq!(s.tile_count(), 1); - assert_eq!(s.tile_bounds(), TileCoord::containing(-3, 700).rect()); - } - } - - #[test] - fn copy_on_write_shares_untouched_tiles() { - let mut a = Surface::new(PixelFormat::RGBA8); - a.fill_rect(Rect::new(0, 0, 512, 512), &[1.0, 0.0, 0.0, 1.0]); - let mut b = a.clone(); - b.write_pixel(10, 10, &[0.0, 1.0, 0.0, 1.0]); - // a unchanged - assert_eq!(a.pixel(10, 10), vec![1.0, 0.0, 0.0, 1.0]); - assert_eq!(b.pixel(10, 10), vec![0.0, 1.0, 0.0, 1.0]); - // only tile (0,0) was copied - let shared = a.tiles().filter(|(c, t)| Arc::ptr_eq(t, b.tile(**c).unwrap())).count(); - assert_eq!(shared, 3); - } - - #[test] - fn content_bounds_and_prune() { - let mut s = Surface::new(PixelFormat::RGBA8); - s.fill_rect(Rect::new(300, 10, 310, 20), &[1.0, 1.0, 1.0, 1.0]); - s.write_pixel(-5, -5, &[0.0, 0.0, 0.0, 1.0]); - assert_eq!(s.content_bounds(), Rect::new(-5, -5, 310, 20)); - // erase the single pixel -> its tile becomes default and is pruned - s.write_pixel(-5, -5, &[0.0, 0.0, 0.0, 0.0]); - s.prune(); - assert_eq!(s.tile_count(), 1); - assert_eq!(s.content_bounds(), Rect::new(300, 10, 310, 20)); - } - - #[test] - fn interleaved_roundtrip_crossing_tiles() { - let r = Rect::new(-20, 250, 280, 262); - let bpp = PixelFormat::RGBA16.bytes_per_pixel(); - let bytes: Vec = (0..r.width() as usize * r.height() as usize * bpp).map(|i| (i * 31 % 251) as u8).collect(); - let s = Surface::from_interleaved(PixelFormat::RGBA16, r, &bytes); - assert_eq!(s.to_interleaved(r), bytes); - assert_eq!(s.tile_count(), 6); // x tiles -1..=1, y tiles 0..=1 - } - - #[test] - fn region_outside_reads_default() { - let s = Surface::with_default(PixelFormat::GRAY8, &[1.0]); - let v = s.read_region(Rect::new(0, 0, 3, 3)); - assert_eq!(v, vec![1.0; 9]); - } - - #[test] - fn convert_depths_and_models() { - let mut s = Surface::new(PixelFormat::RGBA8); - s.write_pixel(1, 1, &[1.0, 1.0, 1.0, 1.0]); - let g = s.convert(PixelFormat::GRAYA8); - assert!((g.pixel(1, 1)[0] - 1.0).abs() < 1e-2); - let f = s.convert(PixelFormat::RGBA32F); - assert_eq!(f.pixel(1, 1), vec![1.0, 1.0, 1.0, 1.0]); - let c = s.convert(PixelFormat::CMYKA8); - let p = c.pixel(1, 1); - assert!(p[3] < 0.01 && (p[4] - 1.0).abs() < 1e-6); - let back = c.convert(PixelFormat::RGBA8); - assert_eq!(back.pixel(1, 1), vec![1.0, 1.0, 1.0, 1.0]); - } - - #[test] - fn rgba_from_gray_and_cmyk() { - let mut g = Surface::new(PixelFormat::GRAYA8); - g.write_pixel(0, 0, &[0.5, 1.0]); - let v = g.rgba(0, 0); - assert!((v[0] - v[2]).abs() < 1e-6 && (v[3] - 1.0).abs() < 1e-6); - } - - #[test] - fn repeated_cmyk_reads_match_individual_pixels_bitwise() { - let rect = Rect::new(-2, -1, 515, 2); - for depth in SampleType::ALL { - for alpha in [false, true] { - let fmt = PixelFormat::new(ColorMode::Cmyk, depth, alpha); - let mut s = Surface::with_default(fmt, &[0.2, 0.4, 0.6, 0.3, 0.7]); - for (i, x) in (-1..514).enumerate() { - let c = if i / 2 % 256 == 0 { -0.0 } else { (i / 2 % 256) as f32 / 255.0 }; - s.write_pixel(x, 0, &[c, 0.4, 0.6, 0.3, (i % 9) as f32 / 8.0]); - } - let mut out = vec![[0.0; 4]; (rect.width() * rect.height()) as usize]; - s.read_rgba_into(rect, &mut out); - for (i, actual) in out.iter().enumerate() { - let expected = s.rgba(rect.x0 + (i % rect.width() as usize) as i32, rect.y0 + (i / rect.width() as usize) as i32); - assert_eq!(actual.map(f32::to_bits), expected.map(f32::to_bits), "{depth:?}, alpha={alpha}, pixel={i}"); - } - } - } - } - - #[test] - fn tile_content_bounds_scans_against_the_given_pixel() { - let mut s = Surface::new(PixelFormat::GRAY8); - s.write_pixel(3, 4, &[1.0]); - s.write_pixel(200, 9, &[1.0]); - let t = s.tile(TileCoord::new(0, 0)).expect("written tile"); - assert_eq!(t.content_bounds(&[0]), Some((3, 4, 201, 10))); - assert_eq!(t.content_bounds(&[255]).map(|b| (b.0, b.1)), Some((0, 0))); - // A malformed pixel is no content, not a panic. - assert_eq!(t.content_bounds(&[]), None); - } - - #[test] - fn translated_matches_reading_the_moved_pixels() { - // Any offset (whole tiles, negative, across tile edges), every depth, a non-zero default. - for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { - let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); - let r = Rect::new(-37, 200, 530, 300); - let v: Vec = (0..r.width() * r.height() * fmt.channels() as u32).map(|i| (i % 251) as f32 / 250.0).collect(); - s.write_region(r, &v); - for (dx, dy) in [(0, 0), (256, -512), (1, 0), (-3, 7), (300, -257), (-1000, 999)] { - let t = s.translated(dx, dy, s.content_bounds()); - let moved = r.translate(dx, dy).inflate(3); - assert_eq!(t.read_region(moved), s.read_region(moved.translate(-dx, -dy)), "{fmt:?} by ({dx}, {dy})"); - assert_eq!(t.default_pixel(), s.default_pixel()); - assert_eq!(t.to_interleaved(r.translate(dx, dy)), s.to_interleaved(r), "{fmt:?} by ({dx}, {dy}): not an exact copy"); - } - // Whole tiles: the tiles are shared, not copied. - let t = s.translated(512, -256, s.content_bounds()); - assert!(s.tiles().zip(t.tiles()).all(|((_, a), (_, b))| Arc::ptr_eq(a, b))); - // Tiles without content (allocated, default pixels) are left behind. - let mut padded = s.clone(); - padded.fill_rect(Rect::new(2000, 2000, 2100, 2100), &s.default_pixel()); - assert_eq!(padded.translated(5, 5, s.content_bounds()).tile_count(), s.translated(5, 5, s.content_bounds()).tile_count()); - } - // A solid fill moved off the tile grid keeps sharing its one tile inside, at every depth - // and over a non-zero default pixel, and reads exactly like the copied pixels. - for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { - let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); - let r = Rect::new(-300, -10, 1300, 1030); - s.fill_rect(r, &vec![0.75; fmt.channels()]); - let fill = s.tile(TileCoord::new(1, 1)).cloned().expect("an inner fill tile"); - for (dx, dy) in [(10, 7), (-1, 0), (0, 300), (-257, 513)] { - let t = s.translated(dx, dy, s.content_bounds()); - let moved = r.translate(dx, dy); - assert_eq!(t.read_region(moved.inflate(3)), s.read_region(moved.inflate(3).translate(-dx, -dy)), "{fmt:?} by ({dx}, {dy})"); - // Targets read only from the fill's whole tiles (x -256..1280, y 0..1024). - let whole = Rect::new(-256, 0, 1280, 1024).translate(dx, dy); - let inner: Vec<_> = t.tiles().filter(|(c, _)| whole.contains_rect(&c.rect())).collect(); - assert!(!inner.is_empty() && inner.iter().all(|(_, a)| Arc::ptr_eq(a, &fill)), "{fmt:?} by ({dx}, {dy}): inner tiles copied"); - } - // Nudged back and forth, the inside keeps sharing one tile (copies of uniform edge - // tiles count as that value too) and the pixels stay exact. - let mut t = s.clone(); - for k in 0..8 { - let d = if k % 2 == 0 { 10 } else { -10 }; - t = t.translated(d, d / 2, t.content_bounds()); - } - // (Targets whose last window read uniform tiles only: one tile in from the fill's whole - // tiles on the sides the last move came from.) - let inner: Vec<_> = t.tiles().filter(|(c, _)| Rect::new(-256, 0, 1024, 768).contains_rect(&c.rect())).map(|(_, a)| a).collect(); - assert!(inner.len() >= 4 && inner.iter().all(|a| Arc::ptr_eq(a, inner[0])), "{fmt:?}: repeated moves copied the inside"); - assert_eq!(t.read_region(r.inflate(3)), s.read_region(r.inflate(3)), "{fmt:?}: back where it started"); - } - // Two solid fills of different values side by side: windows across the seam are - // copied (each tile is uniform, but not with the same value), the rest shared. - for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA32F] { - let mut s = Surface::new(fmt); - s.fill_rect(Rect::new(0, 0, 1024, 768), &vec![0.2; fmt.channels()]); - s.fill_rect(Rect::new(1024, 0, 2048, 768), &vec![0.9; fmt.channels()]); - let t = s.translated(10, 7, s.content_bounds()); - let all = Rect::new(-20, -20, 2100, 800); - assert_eq!(t.read_region(all), s.read_region(all.translate(-10, -7)), "{fmt:?}: two fills"); - let seam = t.tile(TileCoord::new(4, 1)).expect("seam tile"); - let (left, right) = (t.tile(TileCoord::new(2, 1)).expect("left"), t.tile(TileCoord::new(6, 1)).expect("right")); - assert!(!Arc::ptr_eq(seam, left) && !Arc::ptr_eq(seam, right) && !Arc::ptr_eq(left, right), "{fmt:?}: seam aliased"); - } - // An offset of i32::MIN saturates its negation, so the source window isn't the exact - // preimage: no shortcut may apply (a zero pre-fill would leak into the output). - let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.5]); - s.fill_rect(Rect::new(i32::MAX - 1024, 0, i32::MAX - 512, 512), &[0.25]); - let (fill, default) = (s.sample_channel(i32::MAX - 600, 10, 0), s.default_pixel()[0]); - for (dx, dy) in [(i32::MIN, 1), (1, i32::MIN), (i32::MIN, 0)] { - let t = s.translated(dx, dy, s.content_bounds()); - for (c, _) in t.tiles() { - assert!(t.read_region(c.rect()).iter().all(|v| *v == fill || *v == default), "({dx}, {dy}): tile {c:?} holds a pre-fill"); - } - } - // One tile shared but not uniform: each target is copied, not aliased. - let mut s = Surface::new(PixelFormat::GRAY8); - s.write_pixel(3, 4, &[1.0]); - let odd = s.tile(TileCoord::new(0, 0)).cloned().expect("written tile"); - let grid: Vec<_> = (0..9).map(|i| (TileCoord::new(i % 3, i / 3), odd.clone())).collect(); - s.put_tiles(grid); - let t = s.translated(10, 7, s.content_bounds()); - let all = Rect::new(-20, -20, 800, 800); - assert_eq!(t.read_region(all), s.read_region(all.translate(-10, -7))); - assert!(t.tiles().all(|(_, a)| !Arc::ptr_eq(a, &odd))); - // Offsets at the limits don't panic. - let mut s = Surface::new(PixelFormat::GRAY8); - s.write_pixel(5, 5, &[1.0]); - let all = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); - let _ = s.translated(i32::MAX, i32::MIN, all); - let _ = s.translated(i32::MIN + 1, 3, all); - } - - #[test] - fn translated_stores_no_tiles_of_only_the_default_pixel() { - let only_default = |t: &Surface| { - let dp = t.default_pixel(); - t.tiles().filter(|(c, _)| t.read_region(c.rect()).chunks_exact(dp.len()).all(|p| p == dp.as_slice())).count() - }; - // Two blocks a few tiles apart, moved off the tile grid and back: the default-pixel edges - // of their tiles land on tiles of their own, which aren't stored. - for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { - for default in [0.0, 0.25] { - let mut s = Surface::with_default(fmt, &vec![default; fmt.channels()]); - s.fill_rect(Rect::new(0, 0, 300, 300), &vec![1.0; fmt.channels()]); - s.fill_rect(Rect::new(2000, 1000, 2300, 1300), &vec![0.5; fmt.channels()]); - let all = Rect::new(-600, -600, 3000, 2000); - let mut t = s.clone(); - for (dx, dy) in [(100, 56), (-100, -56), (10, 7), (-3, 40), (130, -90), (-137, 43)] { - let moved = t.translated(dx, dy, t.content_bounds()); - assert_eq!(moved.read_region(all), t.read_region(all.translate(-dx, -dy)), "{fmt:?} over {default} by ({dx}, {dy})"); - assert_eq!(only_default(&moved), 0, "{fmt:?} over {default} by ({dx}, {dy})"); - t = moved; - } - assert_eq!(t.read_region(all), s.read_region(all), "{fmt:?} over {default}: back where it started"); - assert_eq!(t.tile_count(), s.tile_count(), "{fmt:?} over {default}: back where it started"); - } - } - // Stored tiles of the default pixel inside `content`: the targets that would share one of - // them (#4414's uniform windows) are left out too. - let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.25]); - s.fill_rect(Rect::new(0, 0, 1024, 1024), &[0.25]); - s.write_pixel(600, 600, &[1.0]); - let content = Rect::new(0, 0, 1024, 1024); - let t = s.translated(10, 7, content); - assert_eq!(only_default(&t), 0); - assert_eq!(t.tile_count(), 1); - assert_eq!(t.read_region(Rect::new(-20, -20, 1100, 1100)), s.read_region(Rect::new(-30, -27, 1090, 1093))); - } - - proptest! { - #[test] - fn region_write_read_roundtrip(x in -600i32..600, y in -600i32..600, w in 1u32..90, h in 1u32..90, seed in any::()) { - let r = Rect::from_xywh(x, y, w, h); - let n = 4usize; - let data: Vec = (0..(w * h) as usize * n).map(|i| ((i as u32).wrapping_mul(2654435761).wrapping_add(seed) % 256) as f32 / 255.0).collect(); - let mut s = Surface::new(PixelFormat::RGBA8); - s.write_region(r, &data); - let back = s.read_region(r); - for (a, b) in back.iter().zip(&data) { - prop_assert!((a - b).abs() < 1e-6); - } - prop_assert_eq!(s.content_bounds().union(&r), r); - } - - #[test] - fn clone_mutation_isolated(x in -300i32..300, y in -300i32..300) { - let mut a = Surface::new(PixelFormat::RGBA8); - a.fill_rect(Rect::new(-256, -256, 256, 256), &[0.5, 0.5, 0.5, 1.0]); - let snapshot = a.clone(); - a.write_pixel(x, y, &[1.0, 0.0, 0.0, 1.0]); - let expect = if Rect::new(-256, -256, 256, 256).contains(x, y) { vec![0.5f32, 0.5, 0.5, 1.0] } else { vec![0.0; 4] }; - let got = snapshot.pixel(x, y); - for (g, e) in got.iter().zip(&expect) { - prop_assert!((g - e).abs() <= 1.0 / 255.0); - } - } - } - - #[test] - fn strided_row_sampling_matches_sample_channel() { - let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.25]); - s.fill_rect(Rect::new(-300, 10, 200, 40), &[1.0]); - s.fill_rect(Rect::new(250, 10, 700, 20), &[0.5]); - for (y, x0, step) in [(15, -520, 7), (15, -1, 1), (30, 3, 64), (500, 0, 9)] { - let mut out = vec![0.0; 120]; - s.sample_row_strided(y, x0, step, 0, &mut out); - for (i, v) in out.iter().enumerate() { - assert_eq!(*v, s.sample_channel(x0 + i as i32 * step, y, 0), "y={y} x0={x0} step={step} i={i}"); - } - } - } - - #[test] - fn content_bounds_with_tile_skipping() { - // Interior content only; ring tiles present but all-default (unpruned). - let mut s = Surface::new(PixelFormat::GRAY8); - s.fill_rect(Rect::new(0, 0, 1024, 1024), &[0.0]); - s.fill_rect(Rect::new(300, 310, 700, 720), &[1.0]); - assert_eq!(s.content_bounds(), Rect::new(300, 310, 700, 720)); - // Full-canvas content plus a far-away speck. - let mut s = Surface::new(PixelFormat::GRAY8); - s.fill_rect(Rect::new(0, 0, 2000, 1500), &[1.0]); - s.fill_rect(Rect::new(-700, 4000, -699, 4001), &[0.5]); - assert_eq!(s.content_bounds(), Rect::new(-700, 0, 2000, 4001)); - } -} +mod tests; #[cfg(test)] mod echelon_case { diff --git a/crates/raster/src/tests.rs b/crates/raster/src/tests.rs new file mode 100644 index 0000000000..47e78008fd --- /dev/null +++ b/crates/raster/src/tests.rs @@ -0,0 +1,812 @@ +/// Manually compare allocating reads with the previous implementation. +#[test] +#[ignore = "manual 24 MP release benchmark"] +fn benchmark_sparse_reads() { + use std::{hint::black_box, time::Instant}; + let rect = Rect::new(0, 0, 6000, 4000); + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + for default in [0.0, 0.25] { + let mut s = Surface::with_default(PixelFormat::GRAY8.with_sample(depth), &[default]); + s.fill_rect(Rect::new(257, 257, 266, 266), &[0.75]); + assert_eq!(s.read_region(rect), previous_read_region(&s, rect)); + for trial in 0..6 { + let mut paths = [("main", previous_read_region as fn(&Surface, Rect) -> Vec), ("split", Surface::read_region)]; + if trial % 2 == 1 { + paths.reverse(); + } + for (name, path) in paths { + let t = Instant::now(); + let out = black_box(path)(&s, rect); + let elapsed = t.elapsed().as_secs_f64() * 1000.0; + black_box(&out); + println!("sparse,{depth:?}-{default},{trial},{name},{elapsed:.6}"); + } + } + } + } +} +/// Previous allocating read path, retained as the bitwise-equivalence oracle. +fn previous_read_region(surface: &Surface, r: Rect) -> Vec { + let n = surface.channels(); + let w = r.width() as usize; + let mut out = vec![0.0f32; w * r.height() as usize * n]; + let dp = surface.default_pixel(); + for tc in r.tiles() { + let tr = tc.rect().intersect(&r); + let tile = surface.tiles.get(&tc); + for y in tr.y0..tr.y1 { + for x in tr.x0..tr.x1 { + let o = (((y - r.y0) as usize) * w + (x - r.x0) as usize) * n; + match tile { + Some(t) => { + let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (x - tc.tx * TILE_SIZE) as usize) * n; + for i in 0..n { + out[o + i] = read_sample(&t.data, surface.format.sample, base + i); + } + } + None => out[o..o + n].copy_from_slice(&dp), + } + } + } + } + out +} +/// Distinct tile allocations in `s`. +fn distinct_tiles(s: &Surface) -> usize { + let mut seen: Vec<*const Tile> = s.tiles().map(|(_, t)| Arc::as_ptr(t)).collect(); + seen.sort(); + seen.dedup(); + seen.len() +} + +/// Uniform tiles written pixel by pixel end up as one shared tile per value, across +/// surfaces, with every pixel unchanged; editing one splits it off. +#[test] +fn uniform_tiles_are_shared() { + let mut interner = TileInterner::default(); + let r = Rect::new(0, 0, 1024, 600); + let mut bytes = Vec::new(); + for y in 0..600 { + for x in 0..1024 { + // White, except a gradient in the tiles of column 3. + bytes.extend(if x >= 768 { [x as u8, y as u8, 0, 255] } else { [255, 255, 255, 255] }); + } + } + let mut a = Surface::from_interleaved(PixelFormat::RGBA8, r, &bytes); + let mut b = Surface::from_interleaved(PixelFormat::RGBA8, r, &bytes); + let before = a.to_interleaved(r); + assert_eq!(distinct_tiles(&a), 12); + // Rows 512..600 cover only part of their tiles: the rest is the default pixel, so those + // tiles are not uniform. + let saved = a.share_uniform_tiles(&mut interner); + assert_eq!(distinct_tiles(&a), 1 + 3 + 3); + assert_eq!(saved, 5 * TILE_SIZE as usize * TILE_SIZE as usize * 4); + b.share_uniform_tiles(&mut interner); + assert_eq!(interner.len(), 1); + assert!(Arc::ptr_eq(a.tile(TileCoord { tx: 0, ty: 0 }).unwrap(), b.tile(TileCoord { tx: 2, ty: 1 }).unwrap())); + assert_eq!(a.to_interleaved(r), before); + a.write_pixel(5, 5, &[0.0, 0.0, 0.0, 1.0]); + assert_eq!(b.pixel(5, 5), vec![1.0, 1.0, 1.0, 1.0]); + assert_eq!(a.pixel(5, 5), vec![0.0, 0.0, 0.0, 1.0]); + assert_eq!(a.pixel(300, 5), vec![1.0, 1.0, 1.0, 1.0]); +} + +/// Converting or tone-mapping a surface keeps a shared tile shared (#4531) and gives the +/// same pixels as converting each pixel: a filled background is one tile across the grid, +/// plus some distinct tiles. +#[test] +fn conversion_keeps_shared_tiles_shared() { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 2048, 1024), &[0.9, 0.5, 0.2, 1.0]); + s.fill_rect(Rect::new(10, 10, 20, 20), &[0.1, 0.2, 0.3, 0.5]); + let shared_before = distinct_tiles(&s); + assert!(shared_before <= 2 && s.tile_count() == 32, "{shared_before} distinct of {}", s.tile_count()); + let r = Rect::new(0, 0, 2048, 1024); + for to in [PixelFormat::RGBA16, PixelFormat::RGBA32F] { + for out in [s.convert(to), s.try_convert(to).expect("memory")] { + assert_eq!(distinct_tiles(&out), shared_before, "{to:?}: still shared"); + assert_eq!(out.tile_count(), 32); + let (a, b) = (out.read_region(r), s.read_region(r)); + for (pa, pb) in a.as_chunks::<4>().0.iter().zip(b.as_chunks::<4>().0) { + let want = convert_pixel(&s.format(), &to, pb); + assert!(pa.iter().zip(&want).all(|(x, y)| (x - y).abs() < 1e-6), "{to:?}: {pa:?} vs {want:?}"); + } + } + } + let mut m = s.clone(); + m.map_color(|v| v * 0.5); + assert_eq!(distinct_tiles(&m), shared_before, "map_color keeps it shared"); + assert!((m.pixel(1000, 500)[0] - s.pixel(1000, 500)[0] * 0.5).abs() < 1.0 / 255.0); + assert_eq!(s.pixel(1000, 500), vec![s.pixel(1000, 500)[0], s.pixel(1000, 500)[1], s.pixel(1000, 500)[2], 1.0], "the source is untouched"); +} + +use super::*; +use photocraft_color::PixelFormat; +use proptest::prelude::*; + +fn write_region_reference(surface: &mut Surface, r: Rect, data: &[f32]) { + let n = surface.channels(); + let w = r.width() as usize; + assert_eq!(data.len(), w * r.height() as usize * n, "region data length mismatch"); + let sample = surface.format.sample; + for tc in r.tiles() { + let tr = tc.rect().intersect(&r); + let t = surface.tile_mut(tc); + for y in tr.y0..tr.y1 { + for x in tr.x0..tr.x1 { + let o = (((y - r.y0) as usize) * w + (x - r.x0) as usize) * n; + let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (x - tc.tx * TILE_SIZE) as usize) * n; + for i in 0..n { + write_sample(&mut t.data, sample, base + i, data[o + i]); + } + } + } + } +} + +#[test] +fn region_writes_match_scalar_bytes_for_every_format() { + let special = [ + 0.0, + -0.0, + 1.0, + -1.0, + 2.0, + f32::INFINITY, + f32::NEG_INFINITY, + f32::NAN, + f32::from_bits(0x7fc12345), + f32::from_bits(0xffc12345), + f32::MIN_POSITIVE, + f32::from_bits(1), + 0.5 / 255.0, + 0.5 / 65535.0, + ]; + for mode in [ + ColorMode::Bitmap, + ColorMode::Grayscale, + ColorMode::Indexed, + ColorMode::Rgb, + ColorMode::Cmyk, + ColorMode::Lab, + ColorMode::Multichannel, + ColorMode::Duotone, + ] { + for sample in SampleType::ALL { + for alpha in [false, true] { + let fmt = PixelFormat::new(mode, sample, alpha); + let mut actual = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); + actual.write_pixel(900, 900, &vec![0.75; fmt.channels()]); + let mut expected = actual.clone(); + for rect in [Rect::EMPTY, Rect::new(-270, -2, 270, 3), Rect::new(256, 256, 512, 257), Rect::new(-64, -64, 0, 0), Rect::new(255, 250, 258, 270)] + { + let data: Vec<_> = (0..rect.width() as usize * rect.height() as usize * fmt.channels()) + .map(|i| { + if i % 3 == 0 { + special[i / 3 % special.len()] + } else { + // Integer rounding boundaries and deterministic arbitrary f32 bit patterns. + let bits = (i as u32).wrapping_mul(2654435761); + if i % 3 == 1 { ((bits % 65536) as f32 + 0.5) / 65535.0 } else { f32::from_bits(bits) } + } + }) + .collect(); + let snapshot = actual.clone(); + let snapshot_bytes = snapshot.to_interleaved(rect.inflate(1)); + let mut par = actual.clone(); + actual.write_region(rect, &data); + par.write_region_par(rect, &data); + write_region_reference(&mut expected, rect, &data); + assert_eq!(actual, expected, "{fmt:?}, {rect:?}"); + assert_eq!(par, expected, "parallel {fmt:?}, {rect:?}"); + assert_eq!(snapshot.to_interleaved(rect.inflate(1)), snapshot_bytes, "COW snapshot changed"); + // Repeat against already materialised, unshared tiles too. + actual.write_region(rect, &data); + par.write_region_par(rect, &data); + assert_eq!(actual, expected); + assert_eq!(par, expected); + } + } + } + } +} + +/// Mask builds call `write_region` inside a `OnceLock` initializer, where entering Rayon can +/// run another tile's job that waits on the same lock. With every global worker held, a write +/// that queued work on Rayon could not finish. +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn write_region_does_not_enter_rayon() { + use std::sync::{Barrier, mpsc}; + let workers = rayon::current_num_threads(); + let held = Arc::new(Barrier::new(workers + 1)); + let release = Arc::new(Barrier::new(workers + 1)); + let (h, rel) = (held.clone(), release.clone()); + rayon::spawn_broadcast(move |_| { + h.wait(); + rel.wait(); + }); + held.wait(); + let (tx, rx) = mpsc::channel(); + std::thread::spawn(move || { + let r = Rect::new(0, 0, 2 * TILE_SIZE, 2 * TILE_SIZE); + let mut s = Surface::new(PixelFormat::RGBA8); + s.write_region(r, &vec![0.5; (r.width() * r.height() * 4) as usize]); + tx.send(()).ok(); + }); + let done = rx.recv_timeout(std::time::Duration::from_secs(5)).is_ok(); + release.wait(); + assert!(done, "write_region waited on the Rayon pool"); +} + +#[test] +fn from_rgba_into_matches_from_rgba() { + for fmt in [PixelFormat::RGBA8, PixelFormat::GRAY8, PixelFormat::GRAYA8, PixelFormat::CMYKA8, PixelFormat::new(ColorMode::Lab, SampleType::F32, true)] { + let mut buf = [0.0f32; 8]; + let n = from_rgba_into(&fmt, [0.2, 0.5, 0.7, 0.4], &mut buf); + assert_eq!(&buf[..n], &from_rgba(&fmt, [0.2, 0.5, 0.7, 0.4])[..]); + } +} + +/// Sample counts above the stack capacity use writable storage without truncating channels. +#[test] +fn sample_buffer_keeps_stack_fast_path_and_supports_dynamic_channels() { + for channels in [1, 8, 9, 32] { + let mut stack = [0.0; 8]; + let mut heap = Vec::new(); + let samples = sample_buffer(channels, &mut stack, &mut heap); + assert_eq!(samples.len(), channels); + samples.fill(0.25); + if channels <= stack.len() { + assert!(heap.is_empty()); + assert_eq!(&stack[..channels], vec![0.25; channels]); + } else { + assert_eq!(heap, vec![0.25; channels]); + } + } +} + +/// Compare sparse reads and converted accessors with the previous allocating path. +#[test] +fn zero_alloc_accessors_match_read_region() { + for (fmt, default) in [ + PixelFormat::RGBA8, + PixelFormat::RGBA16, + PixelFormat::RGBA32F, + PixelFormat::GRAYA8, + PixelFormat::CMYKA8, + PixelFormat::GRAY8, + PixelFormat::GRAY8.with_sample(SampleType::F32), + PixelFormat::new(ColorMode::Lab, SampleType::U16, true), + ] + .into_iter() + .flat_map(|fmt| [0.0, 0.25, -0.0].map(|default| (fmt, default))) + { + let mut s = Surface::with_default(fmt, &vec![default; fmt.channels()]); + let mut stack = [0.0; 8]; + let mut heap = Vec::new(); + for capacity in [0, stack.len()] { + let decoded = s.default_samples(&mut stack[..capacity], &mut heap); + assert!(decoded.iter().map(|v| v.to_bits()).eq(s.default_pixel().into_iter().map(f32::to_bits))); + } + let r = Rect::new(-300, -20, 300, 40); + for (i, (x, y)) in [(-299, -19), (0, 0), (255, 39), (256, 10), (299, 0)].into_iter().enumerate() { + let px: Vec = (0..fmt.channels()).map(|c| ((i * 3 + c) % 5) as f32 / 4.0).collect(); + s.write_pixel(x, y, &px); + } + let region = s.read_region(r); + assert!(region.iter().map(|v| v.to_bits()).eq(previous_read_region(&s, r).into_iter().map(f32::to_bits))); + let mut into = vec![9.0; 3]; + s.read_region_into(r, &mut into); + assert_eq!(region, into); + let mut rgba = vec![[0.0; 4]; (r.width() * r.height()) as usize]; + s.read_rgba_into(r, &mut rgba); + let mut rgba8 = vec![[0; 4]; rgba.len()]; + s.read_rgba8_into(r, &mut rgba8); + let n = fmt.channels(); + for (i, p) in region.chunks_exact(n).enumerate() { + let want = to_rgba(&fmt, p); + assert_eq!(rgba8[i], want.map(|v| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8)); + for c in 0..4 { + assert!((rgba[i][c] - want[c]).abs() < 1e-6, "{fmt:?} px {i}"); + } + let (x, y) = (r.x0 + (i % r.width() as usize) as i32, r.y0 + (i / r.width() as usize) as i32); + for c in 0..n { + let bits = s.sample_channel(x, y, c).to_bits(); + assert_eq!(bits, p[c].to_bits()); + assert_eq!(bits, into[i * n + c].to_bits()); + } + } + assert!(s.has_tiles_in(Rect::new(0, 0, 1, 1))); + assert!(!s.has_tiles_in(Rect::new(1000, 1000, 1001, 1001))); + } +} + +#[test] +fn fallible_region_reads_report_size_overflow() { + let surface = Surface::new(PixelFormat::RGBA8); + let huge = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); + assert_eq!(surface.try_read_region(huge), Err(AllocationError::SizeOverflow)); + assert_eq!(surface.try_to_interleaved(huge), Err(AllocationError::SizeOverflow)); +} + +#[test] +fn fallible_region_write_rejects_bad_input_without_mutating_surface() { + let mut surface = Surface::new(PixelFormat::RGBA8); + let before = surface.clone(); + assert_eq!(surface.try_write_region(Rect::new(0, 0, 2, 2), &[1.0]), Err(AllocationError::SizeOverflow)); + assert_eq!(surface, before); +} + +#[test] +fn fallible_tile_edits_move_weakly_cached_pixels_and_copy_shared_pixels() { + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F] { + let mut surface = Surface::new(fmt); + let coord = TileCoord::new(0, 0); + let pixels = surface.try_tile_mut(coord).unwrap().bytes().as_ptr(); + let weak = Arc::downgrade(surface.tile(coord).unwrap()); + let tile = surface.try_tile_mut(coord).unwrap(); + assert_eq!(tile.bytes().as_ptr(), pixels); + assert!(weak.upgrade().is_none()); + let snapshot = surface.clone(); + surface.try_write_region(Rect::new(0, 0, 1, 1), &[1.0; 4]).unwrap(); + assert_eq!(snapshot.pixel(0, 0), vec![0.0; 4]); + assert_eq!(surface.pixel(0, 0), vec![1.0; 4]); + } +} + +#[test] +fn fill_rect_shares_covered_tiles_and_edits_stay_local() { + // A 300000² background used to materialize ~360 GB of tiles (#705). + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 300_000, 300_000), &[1.0, 0.5, 0.0, 1.0]); + let t = |s: &Surface, x, y| s.tile(TileCoord::new(x, y)).unwrap().clone(); + assert!(Arc::ptr_eq(&t(&s, 0, 0), &t(&s, 1000, 900))); + // 300000 is not a multiple of 256: the edge tile is partial and stays its own. + let edge = 300_000 / TILE_SIZE; + assert!(!Arc::ptr_eq(&t(&s, 0, 0), &t(&s, edge, 0))); + assert_eq!(s.pixel(299_999, 299_999), vec![1.0, 128.0 / 255.0, 0.0, 1.0]); + assert_eq!(s.pixel(300_000, 0), vec![0.0; 4]); + s.write_pixel(5, 5, &[0.0, 0.0, 1.0, 1.0]); + assert_eq!(s.pixel(5, 5), vec![0.0, 0.0, 1.0, 1.0]); + assert_eq!(s.pixel(300, 5), vec![1.0, 128.0 / 255.0, 0.0, 1.0]); + assert!(!Arc::ptr_eq(&t(&s, 0, 0), &t(&s, 1, 0))); +} + +#[test] +fn empty_surface_reads_default() { + let s = Surface::new(PixelFormat::RGBA8); + assert_eq!(s.pixel(10, -500), vec![0.0; 4]); + let m = Surface::with_default(PixelFormat::GRAY8, &[1.0]); + assert_eq!(m.pixel(123_456, -9), vec![1.0]); + assert_eq!(m.tile_count(), 0); +} + +#[test] +fn write_read_pixel_every_depth() { + for sample in SampleType::ALL { + let f = PixelFormat::RGBA8.with_sample(sample); + let mut s = Surface::new(f); + s.write_pixel(-3, 700, &[0.2, 0.4, 0.6, 1.0]); + let p = s.pixel(-3, 700); + for (a, b) in p.iter().zip([0.2, 0.4, 0.6, 1.0]) { + assert!((a - b).abs() <= 1.0 / 255.0, "{sample:?}"); + } + assert_eq!(s.tile_count(), 1); + assert_eq!(s.tile_bounds(), TileCoord::containing(-3, 700).rect()); + } +} + +#[test] +fn copy_on_write_shares_untouched_tiles() { + let mut a = Surface::new(PixelFormat::RGBA8); + a.fill_rect(Rect::new(0, 0, 512, 512), &[1.0, 0.0, 0.0, 1.0]); + let mut b = a.clone(); + b.write_pixel(10, 10, &[0.0, 1.0, 0.0, 1.0]); + // a unchanged + assert_eq!(a.pixel(10, 10), vec![1.0, 0.0, 0.0, 1.0]); + assert_eq!(b.pixel(10, 10), vec![0.0, 1.0, 0.0, 1.0]); + // only tile (0,0) was copied + let shared = a.tiles().filter(|(c, t)| Arc::ptr_eq(t, b.tile(**c).unwrap())).count(); + assert_eq!(shared, 3); +} + +#[test] +fn content_bounds_and_prune() { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(300, 10, 310, 20), &[1.0, 1.0, 1.0, 1.0]); + s.write_pixel(-5, -5, &[0.0, 0.0, 0.0, 1.0]); + assert_eq!(s.content_bounds(), Rect::new(-5, -5, 310, 20)); + // erase the single pixel -> its tile becomes default and is pruned + s.write_pixel(-5, -5, &[0.0, 0.0, 0.0, 0.0]); + s.prune(); + assert_eq!(s.tile_count(), 1); + assert_eq!(s.content_bounds(), Rect::new(300, 10, 310, 20)); +} + +#[test] +fn interleaved_roundtrip_crossing_tiles() { + let r = Rect::new(-20, 250, 280, 262); + let bpp = PixelFormat::RGBA16.bytes_per_pixel(); + let bytes: Vec = (0..r.width() as usize * r.height() as usize * bpp).map(|i| (i * 31 % 251) as u8).collect(); + let s = Surface::from_interleaved(PixelFormat::RGBA16, r, &bytes); + assert_eq!(s.to_interleaved(r), bytes); + assert_eq!(s.tile_count(), 6); // x tiles -1..=1, y tiles 0..=1 +} + +#[test] +fn region_outside_reads_default() { + let s = Surface::with_default(PixelFormat::GRAY8, &[1.0]); + let v = s.read_region(Rect::new(0, 0, 3, 3)); + assert_eq!(v, vec![1.0; 9]); +} + +#[test] +fn convert_depths_and_models() { + let mut s = Surface::new(PixelFormat::RGBA8); + s.write_pixel(1, 1, &[1.0, 1.0, 1.0, 1.0]); + let g = s.convert(PixelFormat::GRAYA8); + assert!((g.pixel(1, 1)[0] - 1.0).abs() < 1e-2); + let f = s.convert(PixelFormat::RGBA32F); + assert_eq!(f.pixel(1, 1), vec![1.0, 1.0, 1.0, 1.0]); + let c = s.convert(PixelFormat::CMYKA8); + let p = c.pixel(1, 1); + assert!(p[3] < 0.01 && (p[4] - 1.0).abs() < 1e-6); + let back = c.convert(PixelFormat::RGBA8); + assert_eq!(back.pixel(1, 1), vec![1.0, 1.0, 1.0, 1.0]); +} + +#[test] +fn rgba_from_gray_and_cmyk() { + let mut g = Surface::new(PixelFormat::GRAYA8); + g.write_pixel(0, 0, &[0.5, 1.0]); + let v = g.rgba(0, 0); + assert!((v[0] - v[2]).abs() < 1e-6 && (v[3] - 1.0).abs() < 1e-6); +} + +#[test] +fn repeated_cmyk_reads_match_individual_pixels_bitwise() { + let rect = Rect::new(-2, -1, 515, 2); + for depth in SampleType::ALL { + for alpha in [false, true] { + let fmt = PixelFormat::new(ColorMode::Cmyk, depth, alpha); + let mut s = Surface::with_default(fmt, &[0.2, 0.4, 0.6, 0.3, 0.7]); + for (i, x) in (-1..514).enumerate() { + let c = if i / 2 % 256 == 0 { -0.0 } else { (i / 2 % 256) as f32 / 255.0 }; + s.write_pixel(x, 0, &[c, 0.4, 0.6, 0.3, (i % 9) as f32 / 8.0]); + } + let mut out = vec![[0.0; 4]; (rect.width() * rect.height()) as usize]; + s.read_rgba_into(rect, &mut out); + for (i, actual) in out.iter().enumerate() { + let expected = s.rgba(rect.x0 + (i % rect.width() as usize) as i32, rect.y0 + (i / rect.width() as usize) as i32); + assert_eq!(actual.map(f32::to_bits), expected.map(f32::to_bits), "{depth:?}, alpha={alpha}, pixel={i}"); + } + } + } +} + +#[test] +fn rgba_reads_match_individual_pixels_bitwise_in_every_model_and_depth() { + // Across tile edges and a missing tile; float samples include -0, out-of-range and NaN. + let rect = Rect::new(-3, -1, 300, 2); + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Lab, ColorMode::Cmyk] { + for depth in SampleType::ALL { + for alpha in [false, true] { + let fmt = PixelFormat::new(mode, depth, alpha); + let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); + let values = [-0.0, 0.1, 0.5, 1.0, 1.5, -0.25, f32::NAN, 0.75]; + for (i, x) in (-2..260).enumerate() { + let px: Vec = (0..fmt.channels()).map(|c| values[(i + c * 3) % values.len()]).collect(); + s.write_pixel(x, 0, &px); + } + let mut out = vec![[0.0; 4]; (rect.width() * rect.height()) as usize]; + s.read_rgba_into(rect, &mut out); + for (i, actual) in out.iter().enumerate() { + let expected = s.rgba(rect.x0 + (i % rect.width() as usize) as i32, rect.y0 + (i / rect.width() as usize) as i32); + assert_eq!(actual.map(f32::to_bits), expected.map(f32::to_bits), "{fmt:?}, pixel={i}"); + } + } + } + } +} + +#[test] +fn tile_content_bounds_scans_against_the_given_pixel() { + let mut s = Surface::new(PixelFormat::GRAY8); + s.write_pixel(3, 4, &[1.0]); + s.write_pixel(200, 9, &[1.0]); + let t = s.tile(TileCoord::new(0, 0)).expect("written tile"); + assert_eq!(t.content_bounds(&[0]), Some((3, 4, 201, 10))); + assert_eq!(t.content_bounds(&[255]).map(|b| (b.0, b.1)), Some((0, 0))); + // A malformed pixel is no content, not a panic. + assert_eq!(t.content_bounds(&[]), None); +} + +#[test] +fn tile_content_bounds_match_a_per_pixel_scan() { + // One differing byte at each position of a pixel, at and around the compare-block + // boundaries, in every pixel size; and a pixel that differs from `dp` in every byte. + fn per_pixel(t: &Tile, dp: &[u8]) -> Option<(u16, u16, u16, u16)> { + let (bpp, ts) = (dp.len(), TILE_SIZE as usize); + let mut b: Option<(usize, usize, usize, usize)> = None; + for (i, px) in t.bytes().chunks_exact(bpp).enumerate() { + if px != dp { + let (x, y) = (i % ts, i / ts); + let (x0, y0, x1, y1) = b.unwrap_or((x, y, x, y)); + b = Some((x0.min(x), y0.min(y), x1.max(x), y1.max(y))); + } + } + b.map(|(x0, y0, x1, y1)| (x0 as u16, y0 as u16, x1 as u16 + 1, y1 as u16 + 1)) + } + for bpp in [1, 2, 3, 4, 6, 8, 16] { + let dp: Vec = (0..bpp).map(|i| (i * 37 + 5) as u8).collect(); + let row = TILE_SIZE as usize * bpp; + for spots in [vec![], vec![0], vec![row - 1], vec![63, 64], vec![row * 7 + 127, row * 7 + 128, row * 200 + 5], vec![row * 255 + row / 2]] { + for byte in 0..bpp { + let mut data = dp.repeat(TILE_SIZE as usize * TILE_SIZE as usize); + for &s in &spots { + data[s - s % bpp + byte] ^= 0x5a; + } + let t = Tile { data: data.into_boxed_slice() }; + assert_eq!(t.content_bounds(&dp), per_pixel(&t, &dp), "bpp {bpp}, spots {spots:?}, byte {byte}"); + } + } + let mut data = dp.repeat(TILE_SIZE as usize * TILE_SIZE as usize); + data[row * 3 + 9 * bpp..row * 3 + 10 * bpp].iter_mut().for_each(|b| *b = !*b); + let t = Tile { data: data.into_boxed_slice() }; + assert_eq!(t.content_bounds(&dp), Some((9, 3, 10, 4)), "bpp {bpp}"); + } +} + +#[test] +fn translated_matches_reading_the_moved_pixels() { + // Any offset (whole tiles, negative, across tile edges), every depth, a non-zero default. + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { + let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); + let r = Rect::new(-37, 200, 530, 300); + let v: Vec = (0..r.width() * r.height() * fmt.channels() as u32).map(|i| (i % 251) as f32 / 250.0).collect(); + s.write_region(r, &v); + for (dx, dy) in [(0, 0), (256, -512), (1, 0), (-3, 7), (300, -257), (-1000, 999)] { + let t = s.translated(dx, dy, s.content_bounds()); + let moved = r.translate(dx, dy).inflate(3); + assert_eq!(t.read_region(moved), s.read_region(moved.translate(-dx, -dy)), "{fmt:?} by ({dx}, {dy})"); + assert_eq!(t.default_pixel(), s.default_pixel()); + assert_eq!(t.to_interleaved(r.translate(dx, dy)), s.to_interleaved(r), "{fmt:?} by ({dx}, {dy}): not an exact copy"); + } + // Whole tiles: the tiles are shared, not copied. + let t = s.translated(512, -256, s.content_bounds()); + assert!(s.tiles().zip(t.tiles()).all(|((_, a), (_, b))| Arc::ptr_eq(a, b))); + // Tiles without content (allocated, default pixels) are left behind. + let mut padded = s.clone(); + padded.fill_rect(Rect::new(2000, 2000, 2100, 2100), &s.default_pixel()); + assert_eq!(padded.translated(5, 5, s.content_bounds()).tile_count(), s.translated(5, 5, s.content_bounds()).tile_count()); + } + // A solid fill moved off the tile grid keeps sharing its one tile inside, at every depth + // and over a non-zero default pixel, and reads exactly like the copied pixels. + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { + let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]); + let r = Rect::new(-300, -10, 1300, 1030); + s.fill_rect(r, &vec![0.75; fmt.channels()]); + let fill = s.tile(TileCoord::new(1, 1)).cloned().expect("an inner fill tile"); + for (dx, dy) in [(10, 7), (-1, 0), (0, 300), (-257, 513)] { + let t = s.translated(dx, dy, s.content_bounds()); + let moved = r.translate(dx, dy); + assert_eq!(t.read_region(moved.inflate(3)), s.read_region(moved.inflate(3).translate(-dx, -dy)), "{fmt:?} by ({dx}, {dy})"); + // Targets read only from the fill's whole tiles (x -256..1280, y 0..1024). + let whole = Rect::new(-256, 0, 1280, 1024).translate(dx, dy); + let inner: Vec<_> = t.tiles().filter(|(c, _)| whole.contains_rect(&c.rect())).collect(); + assert!(!inner.is_empty() && inner.iter().all(|(_, a)| Arc::ptr_eq(a, &fill)), "{fmt:?} by ({dx}, {dy}): inner tiles copied"); + } + // Nudged back and forth, the inside keeps sharing one tile (copies of uniform edge + // tiles count as that value too) and the pixels stay exact. + let mut t = s.clone(); + for k in 0..8 { + let d = if k % 2 == 0 { 10 } else { -10 }; + t = t.translated(d, d / 2, t.content_bounds()); + } + // (Targets whose last window read uniform tiles only: one tile in from the fill's whole + // tiles on the sides the last move came from.) + let inner: Vec<_> = t.tiles().filter(|(c, _)| Rect::new(-256, 0, 1024, 768).contains_rect(&c.rect())).map(|(_, a)| a).collect(); + assert!(inner.len() >= 4 && inner.iter().all(|a| Arc::ptr_eq(a, inner[0])), "{fmt:?}: repeated moves copied the inside"); + assert_eq!(t.read_region(r.inflate(3)), s.read_region(r.inflate(3)), "{fmt:?}: back where it started"); + } + // Two solid fills of different values side by side: windows across the seam are + // copied (each tile is uniform, but not with the same value), the rest shared. + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA32F] { + let mut s = Surface::new(fmt); + s.fill_rect(Rect::new(0, 0, 1024, 768), &vec![0.2; fmt.channels()]); + s.fill_rect(Rect::new(1024, 0, 2048, 768), &vec![0.9; fmt.channels()]); + let t = s.translated(10, 7, s.content_bounds()); + let all = Rect::new(-20, -20, 2100, 800); + assert_eq!(t.read_region(all), s.read_region(all.translate(-10, -7)), "{fmt:?}: two fills"); + let seam = t.tile(TileCoord::new(4, 1)).expect("seam tile"); + let (left, right) = (t.tile(TileCoord::new(2, 1)).expect("left"), t.tile(TileCoord::new(6, 1)).expect("right")); + assert!(!Arc::ptr_eq(seam, left) && !Arc::ptr_eq(seam, right) && !Arc::ptr_eq(left, right), "{fmt:?}: seam aliased"); + } + // An offset of i32::MIN saturates its negation, so the source window isn't the exact + // preimage: no shortcut may apply (a zero pre-fill would leak into the output). + let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.5]); + s.fill_rect(Rect::new(i32::MAX - 1024, 0, i32::MAX - 512, 512), &[0.25]); + let (fill, default) = (s.sample_channel(i32::MAX - 600, 10, 0), s.default_pixel()[0]); + for (dx, dy) in [(i32::MIN, 1), (1, i32::MIN), (i32::MIN, 0)] { + let t = s.translated(dx, dy, s.content_bounds()); + for (c, _) in t.tiles() { + assert!(t.read_region(c.rect()).iter().all(|v| *v == fill || *v == default), "({dx}, {dy}): tile {c:?} holds a pre-fill"); + } + } + // One tile shared but not uniform: each target is copied, not aliased. + let mut s = Surface::new(PixelFormat::GRAY8); + s.write_pixel(3, 4, &[1.0]); + let odd = s.tile(TileCoord::new(0, 0)).cloned().expect("written tile"); + let grid: Vec<_> = (0..9).map(|i| (TileCoord::new(i % 3, i / 3), odd.clone())).collect(); + s.put_tiles(grid); + let t = s.translated(10, 7, s.content_bounds()); + let all = Rect::new(-20, -20, 800, 800); + assert_eq!(t.read_region(all), s.read_region(all.translate(-10, -7))); + assert!(t.tiles().all(|(_, a)| !Arc::ptr_eq(a, &odd))); + // Offsets at the limits don't panic. + let mut s = Surface::new(PixelFormat::GRAY8); + s.write_pixel(5, 5, &[1.0]); + let all = Rect::new(i32::MIN, i32::MIN, i32::MAX, i32::MAX); + let _ = s.translated(i32::MAX, i32::MIN, all); + let _ = s.translated(i32::MIN + 1, 3, all); +} + +/// Many references to one payload, moved off the tile grid: the rebuilt tiles that read the +/// same source tiles at the same offset are one tile, not a copy each. +#[test] +fn translated_shares_tiles_rebuilt_from_shared_sources() { + let fmt = PixelFormat::RGBA8; + let mut one = Surface::new(fmt); + let r = Rect::new(0, 0, TILE_SIZE, TILE_SIZE); + let px: Vec = (0..r.width() * r.height()).flat_map(|i| [(i % 251) as f32 / 255.0, (i % 7) as f32 / 7.0, 0.5, 1.0]).collect(); + one.write_region(r, &px); + let payload = Arc::clone(one.tile(TileCoord::new(0, 0)).unwrap()); + let mut s = Surface::new(fmt); + s.put_tiles((0..16).flat_map(|ty| (0..16).map(move |tx| TileCoord::new(tx, ty))).map(|c| (c, Arc::clone(&payload)))); + let moved = s.translated(7, 5, s.content_bounds()); + let distinct: std::collections::HashSet<_> = moved.tiles().map(|(_, t)| Arc::as_ptr(t)).collect(); + // The 15 × 15 interior tiles read the same four source tiles at the same offset, so they are + // one tile, however many threads build them; only the 64 border tiles (cut by the + // content's edge) are built one by one. + assert!(moved.tile_count() == 289 && distinct.len() == 64 + 1, "{} tiles, {} distinct", moved.tile_count(), distinct.len()); + let all = Rect::new(-50, -50, 16 * TILE_SIZE + 50, 16 * TILE_SIZE + 50); + assert_eq!(moved.read_region(all), s.read_region(all.translate(-7, -5))); +} + +#[test] +fn a_surface_bounds_tile_count_is_counted_not_walked() { + let mut s = Surface::new(PixelFormat::RGBA8); + let far = Rect::new(0, 0, i32::MAX, i32::MAX); + assert_eq!(far.tile_count(), u64::from(i32::MAX as u32 / TILE_SIZE as u32 + 1).pow(2)); + // Not allocated: 2^46 entries is far past any reservation. + assert!(s.try_take_tiles(far).is_err()); +} + +#[test] +fn translated_stores_no_tiles_of_only_the_default_pixel() { + let only_default = |t: &Surface| { + let dp = t.default_pixel(); + t.tiles().filter(|(c, _)| t.read_region(c.rect()).chunks_exact(dp.len()).all(|p| p == dp.as_slice())).count() + }; + // Two blocks a few tiles apart, moved off the tile grid and back: the default-pixel edges + // of their tiles land on tiles of their own, which aren't stored. + for fmt in [PixelFormat::RGBA8, PixelFormat::RGBA16, PixelFormat::RGBA32F, PixelFormat::GRAY8] { + for default in [0.0, 0.25] { + let mut s = Surface::with_default(fmt, &vec![default; fmt.channels()]); + s.fill_rect(Rect::new(0, 0, 300, 300), &vec![1.0; fmt.channels()]); + s.fill_rect(Rect::new(2000, 1000, 2300, 1300), &vec![0.5; fmt.channels()]); + let all = Rect::new(-600, -600, 3000, 2000); + let mut t = s.clone(); + for (dx, dy) in [(100, 56), (-100, -56), (10, 7), (-3, 40), (130, -90), (-137, 43)] { + let moved = t.translated(dx, dy, t.content_bounds()); + assert_eq!(moved.read_region(all), t.read_region(all.translate(-dx, -dy)), "{fmt:?} over {default} by ({dx}, {dy})"); + assert_eq!(only_default(&moved), 0, "{fmt:?} over {default} by ({dx}, {dy})"); + t = moved; + } + assert_eq!(t.read_region(all), s.read_region(all), "{fmt:?} over {default}: back where it started"); + assert_eq!(t.tile_count(), s.tile_count(), "{fmt:?} over {default}: back where it started"); + } + } + // Stored tiles of the default pixel inside `content`: the targets that would share one of + // them (#4414's uniform windows) are left out too. + let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.25]); + s.fill_rect(Rect::new(0, 0, 1024, 1024), &[0.25]); + s.write_pixel(600, 600, &[1.0]); + let content = Rect::new(0, 0, 1024, 1024); + let t = s.translated(10, 7, content); + assert_eq!(only_default(&t), 0); + assert_eq!(t.tile_count(), 1); + assert_eq!(t.read_region(Rect::new(-20, -20, 1100, 1100)), s.read_region(Rect::new(-30, -27, 1090, 1093))); +} + +proptest! { + #[test] + fn region_write_read_roundtrip(x in -600i32..600, y in -600i32..600, w in 1u32..90, h in 1u32..90, seed in any::()) { + let r = Rect::from_xywh(x, y, w, h); + let n = 4usize; + let data: Vec = (0..(w * h) as usize * n).map(|i| ((i as u32).wrapping_mul(2654435761).wrapping_add(seed) % 256) as f32 / 255.0).collect(); + let mut s = Surface::new(PixelFormat::RGBA8); + s.write_region(r, &data); + let back = s.read_region(r); + for (a, b) in back.iter().zip(&data) { + prop_assert!((a - b).abs() < 1e-6); + } + prop_assert_eq!(s.content_bounds().union(&r), r); + } + + #[test] + fn clone_mutation_isolated(x in -300i32..300, y in -300i32..300) { + let mut a = Surface::new(PixelFormat::RGBA8); + a.fill_rect(Rect::new(-256, -256, 256, 256), &[0.5, 0.5, 0.5, 1.0]); + let snapshot = a.clone(); + a.write_pixel(x, y, &[1.0, 0.0, 0.0, 1.0]); + let expect = if Rect::new(-256, -256, 256, 256).contains(x, y) { vec![0.5f32, 0.5, 0.5, 1.0] } else { vec![0.0; 4] }; + let got = snapshot.pixel(x, y); + for (g, e) in got.iter().zip(&expect) { + prop_assert!((g - e).abs() <= 1.0 / 255.0); + } + } +} + +#[test] +fn strided_row_sampling_handles_extreme_coordinates_and_steps() { + for sample in SampleType::ALL { + let mut s = Surface::with_default(PixelFormat::new(ColorMode::Grayscale, sample, false), &[0.25]); + for (x, value) in [(i32::MIN, 0.1), (-1, 0.3), (0, 0.4), (i32::MAX - 1, 0.6), (i32::MAX, 0.9)] { + s.write_pixel(x, 0, &[value]); + } + for y in [0, 256] { + for (x0, step, len) in [(i32::MAX, 1, 1), (i32::MIN, i32::MAX, 3), (0, i32::MAX, 2), (i32::MAX - 1, 1, 2), (i32::MAX, i32::MAX, 0)] { + let mut out = vec![0.0; len]; + s.sample_row_strided(y, x0, step, 0, &mut out); + for (i, &got) in out.iter().enumerate() { + let x = i32::try_from(i64::from(x0) + i as i64 * i64::from(step)).unwrap(); + assert_eq!(got, s.sample_channel(x, y, 0), "{sample:?} y={y} x0={x0} step={step} i={i}"); + } + } + } + } +} + +#[test] +fn strided_row_sampling_uses_default_past_coordinate_range() { + for sample in SampleType::ALL { + let mut s = Surface::with_default(PixelFormat::new(ColorMode::Grayscale, sample, false), &[0.25]); + s.write_pixel(i32::MAX, 0, &[0.75]); + s.write_pixel(i32::MIN, 0, &[1.0]); + for step in [1, i32::MAX] { + let mut out = [0.0; 4]; + s.sample_row_strided(0, i32::MAX, step, 0, &mut out); + assert_eq!(out, [s.sample_channel(i32::MAX, 0, 0), s.default_pixel()[0], s.default_pixel()[0], s.default_pixel()[0]]); + } + } +} + +#[test] +fn strided_row_sampling_matches_sample_channel() { + let mut s = Surface::with_default(PixelFormat::GRAY8, &[0.25]); + s.fill_rect(Rect::new(-300, 10, 200, 40), &[1.0]); + s.fill_rect(Rect::new(250, 10, 700, 20), &[0.5]); + for (y, x0, step) in [(15, -520, 7), (15, -1, 1), (30, 3, 64), (500, 0, 9)] { + let mut out = vec![0.0; 120]; + s.sample_row_strided(y, x0, step, 0, &mut out); + for (i, v) in out.iter().enumerate() { + assert_eq!(*v, s.sample_channel(x0 + i as i32 * step, y, 0), "y={y} x0={x0} step={step} i={i}"); + } + } +} + +#[test] +fn content_bounds_with_tile_skipping() { + // Interior content only; ring tiles present but all-default (unpruned). + let mut s = Surface::new(PixelFormat::GRAY8); + s.fill_rect(Rect::new(0, 0, 1024, 1024), &[0.0]); + s.fill_rect(Rect::new(300, 310, 700, 720), &[1.0]); + assert_eq!(s.content_bounds(), Rect::new(300, 310, 700, 720)); + // Full-canvas content plus a far-away speck. + let mut s = Surface::new(PixelFormat::GRAY8); + s.fill_rect(Rect::new(0, 0, 2000, 1500), &[1.0]); + s.fill_rect(Rect::new(-700, 4000, -699, 4001), &[0.5]); + assert_eq!(s.content_bounds(), Rect::new(-700, 0, 2000, 4001)); +} diff --git a/crates/raw/README.md b/crates/raw/README.md index bb4815a4fc..03c95390a4 100644 --- a/crates/raw/README.md +++ b/crates/raw/README.md @@ -25,9 +25,9 @@ Implemented only from public specifications, papers and observation of files: * ITU-T T.81 (ISO 10918-1) Annex H: lossless JPEG, process 14 ("LJ92"). * The published description of Canon's CR2 container (header, raw IFD, slice tag 0xC640). * Publicly documented maker-note / private tags (ExifTool's tag tables): Canon ModelID (0x0010), - SensorInfo (0x00E0) and ColorData (0x4001), Nikon WB_RBLevels (0x000C) and BlackLevel (0x003D), Sony + SensorInfo (0x00E0) and ColorData (0x4001), Nikon WB_RBLevels (0x000C), ColorSpace (0x001E) and BlackLevel (0x003D), Sony BlackLevel (0x7310), WB_RGGBLevels (0x7313), SonyRawFileType (0x7000) and SonyToneCurve - (0x7010), the PanasonicRaw IFD0 tags, Olympus ImageProcessing (0x2040) and CameraSettings + (0x7010), SR2Private Offset / Length / Key (0x7200 / 0x7201 / 0x7221), the PanasonicRaw IFD0 tags, Olympus ImageProcessing (0x2040) and CameraSettings (0x2020) preview tags, the Fujifilm RAF header and metadata records (RawImageCropTopLeft / RawImageCroppedSize, FujiLayout, XTransLayout, WB_GRGBLevels, RawExposureBias) and FujiIFD (RawImageFullWidth / Height, BitsPerSample, StripOffsets / ByteCounts, BlackLevel, @@ -41,6 +41,8 @@ Implemented only from public specifications, papers and observation of files: * Sony cRAW (ARW 2): H. Dietz, "Sony ARW2 Compression: Artifacts And Credible Repair" (Electronic Imaging 2016) and the RawDigger / diglloyd write-ups of the 11 + 7-bit scheme; the exact bit layout and tone-curve scale were established by observation of sample files. + Encrypted SR2SubIFD metadata with key bytes `11 22 33 44` was measured independently against + ExifTool's metadata dumps; see [Sony metadata evidence](../../docs/sony-arw-metadata.md). * Panasonic RW2 RawFormat 5, uncompressed Olympus ORF and uncompressed Fujifilm RAF: established by observation of sample files (bit packing, page layout, sample justification; for RAF the three sample storages and the reversed order of the X-Trans / Bayer layout records, each @@ -67,13 +69,13 @@ camera colour tables were copied. | Format | Status | |---|---| -| DNG | Uncompressed (8–16 bit, packed or not), Deflate (compression 8, predictors 1 and 2), lossless JPEG, lossy JPEG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, gray for CFA, YCbCr-coded RGB for LinearRaw; decoded by `zune-jpeg`, the stored 8-bit values then go through the same LinearizationTable / BlackLevel / WhiteLevel as every other depth) and JPEG XL (DNG 1.7, e.g. Samsung Expert RAW; decoded by `jxl-oxide`; not in wasm32 builds, where it is `Unsupported`); strips and tiles; CFA (Bayer) and LinearRaw (three-sample colour, or one-sample monochrome such as Leica Monochrom and film-scanner DNGs, developed as neutral grey with no white balance or colour matrix); LinearizationTable, BlackLevel (+ repeat, DeltaH/V), WhiteLevel, ActiveArea, DefaultCrop, ColorMatrix1/2, CameraCalibration, ForwardMatrix, AnalogBalance, AsShotNeutral / AsShotWhiteXY, BaselineExposure, Orientation, OpcodeList2 GainMap (lens shading) | -| DNG (lossy JPEG, floating point; opcodes other than GainMap; ProfileGainTableMap) | Unsupported / not applied (reported) | +| DNG | Uncompressed (8–16 bit, packed or not), Deflate (compression 8, predictors 1 and 2), lossless JPEG, lossy JPEG (DNG 1.4 compression 34892: 8-bit baseline JPEG tiles or strips, gray for CFA, YCbCr-coded RGB for LinearRaw; decoded by `zune-jpeg`, the stored 8-bit values then go through the same LinearizationTable / BlackLevel / WhiteLevel as every other depth) and JPEG XL (DNG 1.7, e.g. Samsung Expert RAW; decoded by `jxl-oxide`; not in wasm32 builds, where it is `Unsupported`); strips and tiles; CFA (Bayer) and LinearRaw (three-sample colour, or one-sample monochrome such as Leica Monochrom and film-scanner DNGs, developed as neutral grey with no white balance or colour matrix); LinearizationTable, BlackLevel (+ repeat, DeltaH/V), WhiteLevel, ActiveArea, DefaultCrop, ColorMatrix1/2, CameraCalibration, ForwardMatrix, AnalogBalance, AsShotNeutral / AsShotWhiteXY, BaselineExposure, Orientation, floating-point JPEG XL raw data (DNG 1.7 lossy LinearRaw, as Adobe DNG Converter writes it; scaled by the highest WhiteLevel), OpcodeList2 GainMap (lens shading) and MapPolynomial | +| DNG (floating point other than JPEG XL; opcodes other than OpcodeList2 GainMap and MapPolynomial; ProfileGainTableMap) | Unsupported / not applied (reported) | | CR2 | Lossless JPEG with slices, borders and as-shot white balance from the maker note, black measured on the masked border. CR2 has no CFA tag and the row phase varies by model, so it is measured from the data (the green diagonal), with a Canon model-ID table as the fallback (see `src/cr2.rs`) | | CR2 sRAW / mRAW | Unsupported | | NEF / NRW, ARW, PEF and other TIFF/EP raws | Uncompressed and lossless-JPEG (incl. Sony lossless ARW) CFA data | | Nikon compressed NEF (compression 34713): lossless and lossy type 1 / 2, 12 and 14 bit | Decoded: fixed Huffman tables + maker-note `0x0096` seeds and curve (see `src/nefc.rs`). BlackLevel `0x003d` is read in 14-bit units. "Lossy after split" files are unsupported (preview fallback) | -| Sony compressed ARW ("cRAW", SonyRawFileType 2) | Decoded: 11-bit min/max + 7-bit delta blocks, SonyToneCurve to 14 bits, the curve's 512 black level when no BlackLevel tag is written (the first-generation bodies, e.g. the ILCE-7) | +| Sony compressed ARW ("cRAW", SonyRawFileType 2) | Decoded: 11-bit min/max + 7-bit delta blocks, SonyToneCurve to 14 bits; applied WB and black from plain tags or encrypted SR2SubIFD with the measured common key. If metadata is unreadable, black falls back to 512 and WB to grey-world. Measured ILCE-7 / ILCE-6000 image area; camera colour calibration remains incomplete | | Panasonic / Leica RW2, RawFormat 5 (12- and 14-bit packed) | Decoded, with PanasonicRaw black / white / WB / sensor borders | | Panasonic / Leica RW2, RawFormat 3 and 4 (vendor-compressed, 12-bit) | Decoded; the black level is read 15 counts above the recorded tag; PanasonicRaw white / WB / sensor borders | | Olympus ORF, uncompressed 16-bit (E-1, E-400…) | Decoded, with ImageProcessing black / WB / ValidBits / crop | @@ -119,7 +121,11 @@ out). One cell in three is held out: the look is used only when it shows the sam the matrix is plausible and the held-out mean CIE76 ΔE drops to at most 85 % of the fallback's. Pass it as `DevelopOptions::camera_look`; `develop` then notes "colour fitted to the camera's embedded JPEG (mean ΔE …)". The crate cannot decode JPEG itself: `photocraft-io` decodes the -smallest preview at least 512 pixels long, fits and develops in one go. +smallest preview at least 512 pixels long, fits and develops in one go. A preview without an ICC +profile is read in the colour space the raw states (`embedded_preview_color_space`): the EXIF +ColorSpace (a Sony body set to Adobe RGB writes Uncalibrated with Interoperability "R03"), else a +Nikon maker note's ColorSpace (0x001E; Nikon writes no EXIF ColorSpace), sRGB when the file says +nothing. Camera Raw's tuned import fits at the default exposure and as-shot white balance first, then applies the user's Exposure, Temperature and Tint to that same rendering. The D5100's measured image area is 4928 × 3264 at (8, 8) in a 4992 × 3280 @@ -149,7 +155,10 @@ Raw rendering (quarter size, medians over the frames) the measured profile is 0. luminance with a mean CIE76 ΔE of 1.8; the look fitted to the embedded JPEG would be 0.30 stop and ΔE 6.4, as it follows the camera's picture style instead. The open-time re-develop (`photocraft-io`'s `import_raw_tuned`) and the tone curve its previews use (`tone_curve`) take the -same look as the default open, so changing a slider does not change the colour rendering. +same look as the default open, so changing a slider does not change the colour rendering. The D4's +saturation level was measured on 14-bit files and follows the sample depth (a 12-bit frame is the +14-bit one shifted right by two, so its level is a quarter of it): the three public 12-bit D4 files +develop within 0.02 stop of their 14-bit twin. ## Tools diff --git a/crates/raw/examples/inspect.rs b/crates/raw/examples/inspect.rs index ca7b136a90..4dd6e5709a 100644 --- a/crates/raw/examples/inspect.rs +++ b/crates/raw/examples/inspect.rs @@ -1,5 +1,5 @@ //! Prints the raw metadata of files: `cargo run -p photocraft-raw --example inspect -- a.cr2 b.nef` -//! (handy while adding corpus files; the oracle expectations in `tests/raw_corpus.rs` come from it). +//! (handy while adding corpus files; the oracle expectations in `tests/it/raw_corpus.rs` come from it). use std::env; fn main() { diff --git a/crates/raw/examples/rawinfo.rs b/crates/raw/examples/rawinfo.rs index b71e2393bd..6b1722b720 100644 --- a/crates/raw/examples/rawinfo.rs +++ b/crates/raw/examples/rawinfo.rs @@ -9,7 +9,7 @@ use std::time::Instant; -use photocraft_raw::{Demosaic, DevelopOptions, decode, develop_sensor, dump_structure, embedded_preview, identify}; +use photocraft_raw::{Demosaic, DevelopOptions, decode, develop_sensor, dump_structure, embedded_preview, embedded_preview_color_space, identify}; fn main() { let mut args = std::env::args().skip(1); @@ -76,7 +76,13 @@ fn main() { println!("{}", dump_structure(&bytes)); } if let Some(p) = embedded_preview(&bytes) { - println!(" preview: {}x{} JPEG, {} bytes", p.width, p.height, p.jpeg.len()); + println!( + " preview: {}x{} JPEG, {} bytes, colour space stated by the file: {:?}", + p.width, + p.height, + p.jpeg.len(), + embedded_preview_color_space(&bytes) + ); if let Some(dir) = png.as_ref().filter(|d| std::path::Path::new(d).is_dir()) { let stem = std::path::Path::new(&f).file_stem().map(|s| s.to_string_lossy().into_owned()).unwrap_or_default(); let _ = std::fs::write(format!("{dir}/{stem}.preview.jpg"), p.jpeg); diff --git a/crates/raw/src/cameras.rs b/crates/raw/src/cameras.rs index 84204c6bce..321ecf1223 100644 --- a/crates/raw/src/cameras.rs +++ b/crates/raw/src/cameras.rs @@ -11,7 +11,9 @@ //! (2 px of optical black on the left, 50 + 8 on the right show as a purple stripe otherwise). The //! data is already black-subtracted (the darkest image pixels reach 0). Green saturates at //! 15546–15672 while red and blue reach 15687–16383, so one level of 15520 makes clipped -//! highlights white without shifting the white balance. Matrix, curve and black point copy +//! highlights white without shifting the white balance. That level is for 14-bit files: the body +//! also writes 12-bit ones, whose samples are the 14-bit values shifted right by two, so the +//! level follows the sample depth (`Profile::white_at`). Matrix, curve and black point copy //! Photoshop Camera Raw's default rendering of these files, measured black-box: 29 NEFs opened //! in Photoshop 25.4 against our own linear development of the same data (8 × 8 means, pixels //! clipped in neither). The matrix is our first ForwardMatrix (fitted to the camera's embedded @@ -92,7 +94,10 @@ struct Profile { size: (usize, usize), /// The image area: x, y, width, height. crop: (usize, usize, usize, usize), + /// The saturation level, in codes of `white_bits`-bit samples. white: f32, + /// The sample depth `white` was measured at. + white_bits: u32, /// The black level is known to be 0 (the data is black-subtracted in camera). black_zero: bool, forward: Mat3, @@ -102,12 +107,25 @@ struct Profile { black_point: f32, } +impl Profile { + /// The saturation level for `bits`-bit samples. A body that writes several depths stores the + /// lower ones as the deepest value shifted right (a 12-bit D4 sample is the 14-bit sample + /// divided by four), so the level scales by the same power of two. At the depth the level was + /// measured at it is returned unchanged. + fn white_at(&self, bits: u32) -> f32 { + let bits = bits.clamp(1, 16); + let measured = self.white_bits.clamp(1, 16); + if bits >= measured { self.white * f32::from(1u16 << (bits - measured)) } else { self.white / f32::from(1u16 << (measured - bits)) } + } +} + const PROFILES: &[Profile] = &[Profile { make: "NIKON CORPORATION", model: "NIKON D4", size: (4992, 3292), crop: (8, 6, 4928, 3280), white: 15520.0, + white_bits: 14, black_zero: true, forward: D4_FORWARD, color: D4_COLOR, @@ -131,6 +149,9 @@ const PROFILES: &[Profile] = &[Profile { /// 4352 x 2868 sensor data, giving the camera JPEG's 4288 x 2848 frame /// (10 pixels of margin on the right and bottom; the right border reads the /// camera's flat 255 from column 4310 on). +/// * **ILCE-6000** (3 files): the same 12 x 12 origin and 6000 x 4000 image; +/// confirmed by the file's EXIF size and SR2SubIFD's area [12, 12, 6012, 4012], +/// and checked against the matching camera JPEGs. No colour profile is inferred. struct ImageArea { make: &'static str, model: &'static str, @@ -145,11 +166,13 @@ const IMAGE_AREAS: &[ImageArea] = &[ ImageArea { make: "NIKON CORPORATION", model: "NIKON D5100", size: (4992, 3280), crop: (8, 8, 4928, 3264) }, ImageArea { make: "NIKON CORPORATION", model: "NIKON D800E", size: (7424, 4924), crop: (8, 6, 7360, 4912) }, ImageArea { make: "SONY", model: "ILCE-7", size: (6048, 4024), crop: (12, 12, 6000, 4000) }, + ImageArea { make: "SONY", model: "ILCE-6000", size: (6048, 4024), crop: (12, 12, 6000, 4000) }, ]; /// Applies the measured profile of the camera that made `s`, when there is one, -/// and its measured image area when the profile is not measured yet. -pub(crate) fn apply(s: &mut Sensor) { +/// and its measured image area when the profile is not measured yet. `bits` is the depth of +/// the stored samples (the profile's saturation level depends on it). +pub(crate) fn apply(s: &mut Sensor, bits: u32) { let (Some(make), Some(model)) = (s.make.as_deref(), s.model.as_deref()) else { return }; let (make, model) = (make.trim(), model.trim()); if let Some(a) = IMAGE_AREAS.iter().find(|a| a.make == make && a.model == model && a.size == (s.width, s.height) && s.crop == s.active) { @@ -164,7 +187,7 @@ pub(crate) fn apply(s: &mut Sensor) { } let (x, y, w, h) = p.crop; s.crop = Rect::new(x, y, w, h); - s.white = [p.white; 3]; + s.white = [p.white_at(bits); 3]; if p.black_zero { s.warnings.retain(|w| !w.contains("black level not recorded")); } @@ -217,7 +240,7 @@ mod tests { #[test] fn a_d4_nef_gets_its_crop_levels_matrix_and_curve() { let mut s = nef("NIKON D4", 4992, 3292); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(8, 6, 4928, 3280)); assert_eq!(s.white, [15520.0; 3]); assert_eq!(s.color.calibrations.len(), 1); @@ -229,6 +252,28 @@ mod tests { assert!(s.color.balanced_to_xyz_d50([0.5, 1.0, 0.7]).is_some()); } + #[test] + fn the_d4_white_level_follows_the_sample_depth() { + // 14 bits is the depth it was measured at: unchanged. + let mut s = nef("NIKON D4", 4992, 3292); + apply(&mut s, 14); + assert_eq!(s.white, [15520.0; 3]); + // A 12-bit sample is the 14-bit one shifted right by two. + let mut s = nef("NIKON D4", 4992, 3292); + apply(&mut s, 12); + assert_eq!(s.white, [3880.0; 3]); + // Everything else of the profile is independent of the depth. + assert_eq!(s.crop, Rect::new(8, 6, 4928, 3280)); + assert_eq!(s.tone_curve.len(), D4_TONE.len()); + assert!(s.warnings.is_empty()); + // Other depths scale the same way and stay inside their own range; nonsense stays finite. + for (bits, white) in [(1, 15520.0 / 8192.0), (8, 15520.0 / 64.0), (13, 7760.0), (16, 62080.0), (0, 15520.0 / 8192.0), (200, 62080.0)] { + let mut s = nef("NIKON D4", 4992, 3292); + apply(&mut s, bits); + assert_eq!(s.white, [white; 3], "{bits} bits"); + } + } + #[test] fn d5100_image_area_respects_explicit_crops_and_exact_sensor_size() { let mut s = nef("NIKON D5100", 4992, 3280); @@ -236,7 +281,7 @@ mod tests { s.active = Rect::new(0, 0, 4984, 3280); s.crop = s.active; let wb = s.camera_wb; - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(8, 8, 4928, 3264)); assert_eq!(s.active, Rect::new(0, 0, 4984, 3280)); assert_eq!(s.camera_wb, wb); @@ -244,11 +289,11 @@ mod tests { assert_eq!(s.white, [16383.0; 3]); s.crop = Rect::new(16, 12, 4800, 3200); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(16, 12, 4800, 3200)); for (model, w, h) in [("NIKON D5100", 2496, 1640), ("NIKON D5200", 4992, 3280)] { let mut other = nef(model, w, h); - apply(&mut other); + apply(&mut other, 14); assert_eq!(other.crop, other.active); } } @@ -256,36 +301,49 @@ mod tests { #[test] fn measured_image_areas_crop_the_borders_without_touching_the_rest() { let mut s = nef("NIKON D800E", 7424, 4924); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(8, 6, 7360, 4912)); // Only the image area is measured: levels, colour and tone stay untouched. assert!(s.color.calibrations.is_empty() && s.tone_curve.is_empty()); assert_eq!(s.white, [16383.0; 3]); let mut s = sensor("SONY", "ILCE-7", 6048, 4024); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(12, 12, 6000, 4000)); + let mut s = sensor("SONY", "ILCE-6000", 6048, 4024); + apply(&mut s, 14); + assert_eq!(s.crop, Rect::new(12, 12, 6000, 4000)); + assert!(s.color.calibrations.is_empty() && s.tone_curve.is_empty()); + // A file's explicit crop wins, including a different in-camera aspect ratio. + s.crop = Rect::new(12, 324, 6000, 3376); + apply(&mut s, 14); + assert_eq!(s.crop, Rect::new(12, 324, 6000, 3376)); + + let mut s = sensor("SONY", "ILCE-6000", 3024, 2012); + apply(&mut s, 14); + assert_eq!(s.crop, Rect::new(0, 0, 3024, 2012)); + // Another size or model of the same maker is left alone. let mut s = nef("NIKON D800E", 2496, 1646); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(0, 0, 2496, 1646)); let mut s = nef("NIKON D800", 7424, 4924); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(0, 0, 7424, 4924)); } #[test] fn d90_image_area_matches_the_camera_jpeg() { let mut s = nef("NIKON D90", 4352, 2868); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(10, 10, 4288, 2848)); // Only the image area is measured: levels, colour and tone stay untouched. assert!(s.color.calibrations.is_empty() && s.tone_curve.is_empty()); assert_eq!(s.white, [16383.0; 3]); // An explicit crop in the file wins over the measured area. s.crop = Rect::new(0, 0, 4288, 2848); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(0, 0, 4288, 2848)); } @@ -298,7 +356,7 @@ mod tests { calibrations: vec![Calibration { temperature: 6504.0, color_matrix: IDENTITY, forward_matrix: None, camera_calibration: IDENTITY }], ..ColorInfo::default() }; - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.color.calibrations.len(), 1); assert_eq!(s.color.calibrations[0].temperature, 6504.0, "the file's own calibration stands"); assert!(s.tone_curve.is_empty() && s.black_point == 0.0); @@ -309,7 +367,7 @@ mod tests { fn other_cameras_and_sizes_are_left_alone() { for (model, w, h) in [("NIKON D4S", 4992, 3292), ("NIKON D4", 2496, 1646), ("NIKON D800", 7424, 4924)] { let mut s = nef(model, w, h); - apply(&mut s); + apply(&mut s, 14); assert_eq!(s.crop, Rect::new(0, 0, w, h), "{model} {w}x{h}"); assert!(s.tone_curve.is_empty() && s.color.calibrations.is_empty() && s.white == [16383.0; 3]); assert_eq!(s.warnings.len(), 1); diff --git a/crates/raw/src/cr3.rs b/crates/raw/src/cr3.rs index bb2119e7d4..04e7e2ec5f 100644 --- a/crates/raw/src/cr3.rs +++ b/crates/raw/src/cr3.rs @@ -141,11 +141,7 @@ fn walk<'a>(raw: &'a [u8], b: &'a [u8], depth: usize, budget: &mut usize, found: } boxes(b, budget, |item, budget| match item.kind { b"moov" => walk(raw, item.body, depth + 1, budget, found), - b"trak" => { - if found.previews.len() < 128 { - found.previews.extend(track_preview(raw, item.body, budget, &found.media)); - } - } + b"trak" if found.previews.len() < 128 => found.previews.extend(track_preview(raw, item.body, budget, &found.media)), b"uuid" => { if item.body.get(..16) == Some(META_UUID) && let Some(children) = item.body.get(16..) diff --git a/crates/raw/src/demosaic.rs b/crates/raw/src/demosaic.rs index 8b87f14a69..6303931811 100644 --- a/crates/raw/src/demosaic.rs +++ b/crates/raw/src/demosaic.rs @@ -101,6 +101,11 @@ impl Padded { Padded { data: vec![0.0; stride * (h + 2 * PAD)], w, h, stride } } + /// Whether the plane is non-empty and holds all its rows. + fn fits(&self) -> bool { + self.w != 0 && self.h != 0 && self.data.len() >= self.stride * (self.h + 2 * PAD) + } + /// From an unpadded plane (tests and small inputs). #[cfg(test)] pub fn from_plane(plane: &[f32], w: usize, h: usize) -> Self { @@ -190,51 +195,174 @@ fn kernels(phase: [u8; 4], method: Demosaic, stride: usize) -> [[Vec<(isize, f32 pub(crate) fn demosaic(p: &Padded, phase: [u8; 4], method: Demosaic, to_xyz: &Mat3, ahd_jobs: usize) -> Vec { let (w, h, s) = (p.w, p.h, p.stride); let mut out = vec![0.0f32; w * h * 3]; - if w == 0 || h == 0 || p.data.len() < s * (h + 2 * PAD) { + if !p.fits() { return out; } if method == Demosaic::Ahd { - ahd(p, phase, to_xyz, &mut out, ahd_jobs); + let (d, m, labf) = (&p.data, ahd_matrix(to_xyz), LabF::new()); + for_bands_grouped( + &mut out, + AHD_BAND * w * 3, + ahd_bands(h), + ahd_jobs, + 1, + || AhdScratch::for_image(h, s), + |band, chunk, sc| { + ahd_band(d, w, s, phase, &m, &labf, band, chunk, sc); + }, + ); return out; } let ks = kernels(phase, method, s); let rows = par::band_rows(w); - let d = &p.data; - par::chunks_mut(&mut out, rows * w * 3, |band, chunk| { - let y0 = band * rows; - for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() { - let y = y0 + r; - let base = (y + PAD) * s + PAD; - for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() { - let pos = (y & 1) * 2 + (x & 1); - let i = base + x; - if method == Demosaic::Bilinear { - // Direct Bayer bilinear (the kernels' result without the tap loops). - let own = usize::from(phase[pos]).min(2); - let (n, so, e, wv) = (d[i - s], d[i + s], d[i + 1], d[i - 1]); - if own == 1 { - let hc = usize::from(phase[(y & 1) * 2 + ((x + 1) & 1)]).min(2); - o[1] = d[i]; - o[hc] = (e + wv) * 0.5; - o[2 - hc] = (n + so) * 0.5; - } else { - o[own] = d[i]; - o[1] = (n + so + e + wv) * 0.25; - o[2 - own] = (d[i - s - 1] + d[i - s + 1] + d[i + s - 1] + d[i + s + 1]) * 0.25; - } - continue; - } - for (c, k) in ks[pos].iter().enumerate() { - let mut v = 0.0f32; - for &(off, wt) in k { - v += wt * d[(i as isize + off) as usize]; - } - o[c] = v.clamp(0.0, 1.0); - } + par::chunks_mut(&mut out, rows * w * 3, |band, chunk| linear_band(&p.data, w, s, phase, method, &ks, band * rows, 0, chunk)); + out +} + +/// [`demosaic`] without the full RGB frame and without the padded plane: every band loads the +/// rows it reads (`fill(y, row)` writes the normalized CFA row `y` of the image, `w` samples; +/// the mirrored borders are added here), demosaics into a band buffer, and `emit(rgb, out)` +/// turns it into the band's `out` samples (the same rows, `w * 3` per row). Each pixel depends +/// only on the plane around it, never on how rows are split or ordered, so the result is that +/// of `emit` over [`demosaic`] of the filled plane. +#[allow(clippy::too_many_arguments)] +pub(crate) fn demosaic_fused( + w: usize, + h: usize, + phase: [u8; 4], + method: Demosaic, + to_xyz: &Mat3, + jobs: usize, + fill: &(impl Fn(usize, &mut [f32]) + Sync), + out: &mut [u16], + emit: &(impl Fn(&[f32], &mut [u16]) + Sync), +) { + if w == 0 || h == 0 || out.len() < w * h * 3 { + return; + } + let s = w + 2 * PAD; + let out = &mut out[..w * h * 3]; + // Padded rows of the output rows `y0..y0 + rows` (`rows + 2 * PAD` of them, row `PAD` is `y0`) + // with the mirror reflection `Padded::fill_borders` applies. + let load = |win: &mut [f32], y0: usize, rows: usize| { + for (j, prow) in win.chunks_exact_mut(s).take(rows + 2 * PAD).enumerate() { + fill(reflect((y0 + j) as isize - PAD as isize, h), &mut prow[PAD..PAD + w]); + for px in (0..PAD).chain(PAD + w..s) { + prow[px] = prow[PAD + reflect(px as isize - PAD as isize, w)]; } } + }; + if method == Demosaic::Ahd { + let (m, labf) = (ahd_matrix(to_xyz), LabF::new()); + // The rows of the tallest band, as `develop`'s memory check counts them. + let rows = h.min(AHD_BAND); + let init = || (AhdScratch::for_image(h, s), vec![0.0f32; rows * w * 3], vec![0.0f32; (rows + 2 * PAD) * s]); + for_bands_grouped(out, AHD_BAND * w * 3, ahd_bands(h), jobs, 1, init, |band, chunk, (sc, buf, win)| { + load(win, band * AHD_BAND, chunk.len() / (w * 3)); + let buf = &mut buf[..chunk.len()]; + // The window starts at a multiple of AHD_BAND (even), so it is band 0 of its own rows. + ahd_band(win, w, s, phase, &m, &labf, 0, buf, sc); + emit(buf, chunk); + }); + return; + } + let ks = kernels(phase, method, s); + let rows = h.min(FUSED_ROWS); + let init = || (vec![0.0f32; rows * w * 3], vec![0.0f32; (rows + 2 * PAD) * s]); + for_bands(out, FUSED_ROWS * w * 3, h.div_ceil(FUSED_ROWS), jobs, init, |band, chunk, (buf, win)| { + load(win, band * FUSED_ROWS, chunk.len() / (w * 3)); + let buf = &mut buf[..chunk.len()]; + linear_band(win, w, s, phase, method, &ks, band * FUSED_ROWS, band * FUSED_ROWS, buf); + emit(buf, chunk); + }); +} + +/// Output rows per band of the fused bilinear / MHC path (the window adds `2 * PAD` rows). +pub(crate) const FUSED_ROWS: usize = 32; + +/// Rows `y0..` of bilinear or MHC output (`chunk.len() / (w * 3)` of them); `d` is the padded +/// plane from image row `dy` on (`dy` 0: all of it). +#[allow(clippy::too_many_arguments)] +fn linear_band(d: &[f32], w: usize, s: usize, phase: [u8; 4], method: Demosaic, ks: &[[Vec<(isize, f32)>; 3]; 4], y0: usize, dy: usize, chunk: &mut [f32]) { + for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() { + let y = y0 + r; + let base = (y - dy + PAD) * s + PAD; + for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() { + let pos = (y & 1) * 2 + (x & 1); + let i = base + x; + if method == Demosaic::Bilinear { + // Direct Bayer bilinear (the kernels' result without the tap loops). + let own = usize::from(phase[pos]).min(2); + let (n, so, e, wv) = (d[i - s], d[i + s], d[i + 1], d[i - 1]); + if own == 1 { + let hc = usize::from(phase[(y & 1) * 2 + ((x + 1) & 1)]).min(2); + o[1] = d[i]; + o[hc] = (e + wv) * 0.5; + o[2 - hc] = (n + so) * 0.5; + } else { + o[own] = d[i]; + o[1] = (n + so + e + wv) * 0.25; + o[2 - own] = (d[i - s - 1] + d[i - s + 1] + d[i + s - 1] + d[i + s + 1]) * 0.25; + } + continue; + } + for (c, k) in ks[pos].iter().enumerate() { + let mut v = 0.0f32; + for &(off, wt) in k { + v += wt * d[(i as isize + off) as usize]; + } + o[c] = v.clamp(0.0, 1.0); + } + } + } +} + +/// Runs `f(band, chunk, scratch)` over the `unit`-sized chunks of `out` (`bands` of them), with +/// at most `jobs` scratch values alive. When every worker may hold one, each band is its own +/// rayon task and borrows a scratch from a pool (one per worker at most); otherwise a few groups +/// of consecutive bands per job run, `jobs` at a time, each building its scratch once. +pub(crate) fn for_bands( + out: &mut [T], + unit: usize, + bands: usize, + jobs: usize, + init: impl Fn() -> S + Sync + Send, + f: impl Fn(usize, &mut [T], &mut S) + Sync + Send, +) { + for_bands_grouped(out, unit, bands, jobs, 4, init, f); +} + +/// Bands per group when `bands` are split into `per_worker` groups for each worker (at most `jobs`). +fn bands_per_group(bands: usize, jobs: usize, per_worker: usize) -> usize { + let groups = (par::workers().min(jobs.max(1)) * per_worker).clamp(1, bands.max(1)); + bands.div_ceil(groups).max(1) +} + +/// [`for_bands`] with `per_worker` groups of consecutive bands for each of the `jobs` workers when +/// fewer than all may hold a scratch. A large scratch (AHD: 16 planes) wants one group per job: +/// with more groups each would build and zero its own scratch for a band or two. +fn for_bands_grouped( + out: &mut [T], + unit: usize, + bands: usize, + jobs: usize, + per_worker: usize, + init: impl Fn() -> S + Sync + Send, + f: impl Fn(usize, &mut [T], &mut S) + Sync + Send, +) { + // One task per band keeps rayon's stealing, so a busy or slow worker doesn't hold up a + // static share of the bands (as `par::chunks_mut_limited` does for the same reason). + if jobs >= par::workers() { + par::chunks_mut_pooled(out, unit, init, |sc, band, chunk| f(band, chunk, sc)); + return; + } + let per_group = bands_per_group(bands, jobs, per_worker); + par::chunks_mut_limited(out, per_group * unit, jobs, |group, gchunk| { + let mut sc = init(); + for (j, chunk) in gchunk.chunks_mut(unit).enumerate() { + f(group * per_group + j, chunk, &mut sc); + } }); - out } /// A CFA repeat as seen from the plane's origin: colour of plane pixel @@ -318,10 +446,36 @@ fn phase_taps(pat: &Pattern, px: isize, py: isize) -> Taps { /// without the edge-directed weights it is no faster and leaves zipper /// artefacts along every edge of an X-Trans image. pub(crate) fn demosaic_cfa(p: &Padded, pat: &Pattern) -> Vec { - let (w, h, s) = (p.w, p.h, p.stride); + let (w, h) = (p.w, p.h); let mut out = vec![0.0f32; w * h * 3]; - if w == 0 || h == 0 || pat.w == 0 || pat.h == 0 || pat.colors.len() < pat.w * pat.h || p.data.len() < s * (h + 2 * PAD) { - return out; + let band = par::band_rows(w); + cfa_with(p, pat, |rows| par::chunks_mut(&mut out, band * w * 3, |b, chunk| rows(b * band, chunk))); + out +} + +/// [`demosaic_cfa`] without the full RGB frame (the green plane stays): see [`demosaic_fused`]. +pub(crate) fn demosaic_cfa_fused(p: &Padded, pat: &Pattern, jobs: usize, out: &mut [u16], emit: &(impl Fn(&[f32], &mut [u16]) + Sync)) { + let (w, h) = (p.w, p.h); + if out.len() < w * h * 3 { + return; + } + let band = par::band_rows(w); + let init = || vec![0.0f32; band.min(h) * w * 3]; + cfa_with(p, pat, |rows| { + for_bands(&mut out[..w * h * 3], band * w * 3, h.div_ceil(band), jobs, init, |b, chunk, buf| { + let buf = &mut buf[..chunk.len()]; + rows(b * band, buf); + emit(buf, chunk); + }); + }); +} + +/// The generic demosaic's green plane, then `run` with a function that fills the RGB rows +/// `y0..` of a chunk (`chunk.len() / (w * 3)` of them). Not called for an unusable plane or pattern. +fn cfa_with(p: &Padded, pat: &Pattern, run: impl FnOnce(&(dyn Fn(usize, &mut [f32]) + Sync))) { + let (w, h, s) = (p.w, p.h, p.stride); + if !p.fits() || pat.w == 0 || pat.h == 0 || pat.colors.len() < pat.w * pat.h { + return; } let taps: Vec = (0..pat.w * pat.h).map(|i| phase_taps(pat, (i % pat.w) as isize, (i / pat.w) as isize)).collect(); let tap_at = |x: usize, y: usize| &taps[(y % pat.h) * pat.w + x % pat.w]; @@ -389,9 +543,9 @@ pub(crate) fn demosaic_cfa(p: &Padded, pat: &Pattern) -> Vec { // 2. Red and blue from colour differences. let gref = &green; let gat = |x: usize, y: usize, o: Off| get(gref, w, 0, x, y, o); - par::chunks_mut(&mut out, band * w * 3, |b, chunk| { + run(&|y0: usize, chunk: &mut [f32]| { for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() { - let y = b * band + r; + let y = y0 + r; for (x, o) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() { let t = tap_at(x, y); let g = gref.get(y * w + x).copied().unwrap_or(0.0); @@ -418,7 +572,6 @@ pub(crate) fn demosaic_cfa(p: &Padded, pat: &Pattern) -> Vec { } } }); - out } /// Regularizes the inverse-gradient weights (normalized units, about 1/256 of full scale). @@ -426,9 +579,10 @@ const GRAD_EPS: f32 = 1.0 / 256.0; /// Regularizes the green-similarity weights of the colour differences. const DIFF_EPS: f32 = 1.0 / 64.0; -/// CIE f(t) for L*a*b*, tabulated on 0..2 with linear interpolation. +/// CIE f(t) for L*a*b*, tabulated on 0..2 with linear interpolation. Each entry holds the +/// sample and the step to the next one (one load per lookup; the same `a + (b - a) * f`). struct LabF { - table: Vec, + pairs: Vec<[f32; 2]>, } const LABF_STEPS: usize = 1 << 14; @@ -436,7 +590,8 @@ const LABF_RANGE: f32 = 2.0; impl LabF { fn new() -> Self { - LabF { table: (0..=LABF_STEPS + 1).map(|i| Self::exact(i as f32 * LABF_RANGE / LABF_STEPS as f32)).collect() } + let table: Vec = (0..=LABF_STEPS + 1).map(|i| Self::exact(i as f32 * LABF_RANGE / LABF_STEPS as f32)).collect(); + LabF { pairs: table.windows(2).map(|p| [p[0], p[1] - p[0]]).collect() } } fn exact(t: f32) -> f32 { @@ -450,29 +605,237 @@ impl LabF { } let x = t * (LABF_STEPS as f32 / LABF_RANGE); let i = (x as usize).min(LABF_STEPS); - let a = self.table[i]; - a + (self.table[i + 1] - a) * (x - i as f32) + let [a, step] = self.pairs[i]; + a + step * (x - i as f32) } } /// Output rows per AHD band. -const AHD_BAND: usize = 32; +pub(crate) const AHD_BAND: usize = 32; /// Extra band rows above and below the output rows. const M: usize = 3; -/// Maximum scratch allocation of one AHD band: two green, RGB and Lab float -/// planes and two byte homogeneity planes, including their border rows. +/// Scratch rows one AHD worker allocates: the output rows of the tallest band +/// this image has, plus their border rows. +fn ahd_scratch_rows(h: usize) -> usize { + h.min(AHD_BAND) + 2 * M +} + +/// Scratch allocation of one AHD worker ([`AhdScratch::for_image`]): two green, +/// RGB and Lab float planes and two byte homogeneity planes over +/// [`ahd_scratch_rows`], plus the two `u32` rows of vertical sums. pub(crate) fn ahd_scratch(w: u64, h: u64) -> u64 { - w.saturating_add(2 * PAD as u64).saturating_mul(h.min(AHD_BAND as u64).saturating_add(2 * M as u64)).saturating_mul(58) + let s = w.saturating_add(2 * PAD as u64); + let rows = h.min(AHD_BAND as u64).saturating_add(2 * M as u64); + s.saturating_mul(rows.saturating_mul(58).saturating_add(8)) } pub(crate) fn ahd_bands(h: usize) -> usize { h.div_ceil(AHD_BAND) } -pub(crate) const AHD_TABLE_BYTES: u64 = (LABF_STEPS as u64 + 2) * 4; +pub(crate) const AHD_TABLE_BYTES: u64 = (LABF_STEPS as u64 + 1) * 8; -fn ahd(p: &Padded, phase: [u8; 4], to_xyz: &Mat3, out: &mut [f32], jobs: usize) { +/// `v` limited to `lo..=hi` like `f32::clamp`, without its panic on NaN bounds (hostile black +/// levels can put NaN in the plane): same result for every non-NaN bound. +#[inline(always)] +fn clampf(v: f32, lo: f32, hi: f32) -> f32 { + let v = if v < lo { lo } else { v }; + if v > hi { hi } else { v } +} + +/// One AHD band's working planes, reused for every band a worker runs (sized for the tallest band). +struct AhdScratch { + /// Green interpolated horizontally (0) and vertically (1). + g: [Vec; 2], + /// Per direction, the RGB candidate (planar). + rgb: [[Vec; 3]; 2], + /// Per direction, its CIELAB (planar). + lab: [[Vec; 3]; 2], + hom: [Vec; 2], + /// Vertical 3-row sums of `hom`, one output row. + vs: [Vec; 2], +} + +impl AhdScratch { + fn new(len: usize, s: usize) -> Self { + let f = || vec![0.0f32; len]; + AhdScratch { + g: [f(), f()], + rgb: [[f(), f(), f()], [f(), f(), f()]], + lab: [[f(), f(), f()], [f(), f(), f()]], + hom: [vec![0u8; len], vec![0u8; len]], + vs: [vec![0u32; s], vec![0u32; s]], + } + } + + /// The scratch for an image `h` rows tall with stride `s`: the size [`ahd_scratch`] budgets. + fn for_image(h: usize, s: usize) -> Self { + AhdScratch::new(ahd_scratch_rows(h) * s, s) + } +} + +/// The camera → XYZ matrix with the rows divided by the white point. +fn ahd_matrix(to_xyz: &Mat3) -> [[f32; 3]; 3] { + let white = [0.9642f32, 1.0, 0.8249]; + std::array::from_fn(|i| std::array::from_fn(|j| to_xyz[i][j] as f32 / white[i])) +} + +/// One AHD band (see [`ahd_ref`] for the plain statement): the same arithmetic in the same +/// order, per row on slices, planar scratch reused across a worker's bands. Every scratch cell +/// a stage reads was written earlier in the same band, so the planes are never cleared between +/// bands. +#[allow(clippy::too_many_arguments)] +fn ahd_band(d: &[f32], w: usize, s: usize, phase: [u8; 4], m: &[[f32; 3]; 3], labf: &LabF, band: usize, chunk: &mut [f32], sc: &mut AhdScratch) { + let rows = chunk.len() / (w * 3); + let n = rows + 2 * M; + let py = |b: usize| band * AHD_BAND + PAD - M + b; + let drow = |y: usize| &d[y * s..(y + 1) * s]; + // 1. Green interpolated horizontally (0) and vertically (1), columns 2..s-2. + { + let [g0, g1] = &mut sc.g; + for b in 0..n { + let y = py(b); + let (rm2, rm1, r0, rp1, rp2) = (drow(y - 2), drow(y - 1), drow(y), drow(y + 1), drow(y + 2)); + let gh_row = &mut g0[b * s..(b + 1) * s]; + let gv_row = &mut g1[b * s..(b + 1) * s]; + let green = [phase[(y & 1) * 2] == 1, phase[(y & 1) * 2 + 1] == 1]; + for x in 2..s - 2 { + let c = r0[x]; + if green[x & 1] { + gh_row[x] = c; + gv_row[x] = c; + continue; + } + let (l, r) = (r0[x - 1], r0[x + 1]); + let gh = (l + r) * 0.5 + (2.0 * c - r0[x - 2] - r0[x + 2]) * 0.25; + gh_row[x] = clampf(gh, l.min(r), l.max(r)); + let (u, dn) = (rm1[x], rp1[x]); + let gv = (u + dn) * 0.5 + (2.0 * c - rm2[x] - rp2[x]) * 0.25; + gv_row[x] = clampf(gv, u.min(dn), u.max(dn)); + } + } + } + // 2. RGB candidates and their Lab, band rows 1..n-1, columns 3..s-3. + for dir in 0..2 { + let gd = &sc.g[dir]; + let [cr, cg, cb] = &mut sc.rgb[dir]; + let [ll, la, lb] = &mut sc.lab[dir]; + for b in 1..n - 1 { + let y = py(b); + let (dm1, d0, dp1) = (drow(y - 1), drow(y), drow(y + 1)); + let (gm1, g0, gp1) = (&gd[(b - 1) * s..b * s], &gd[b * s..(b + 1) * s], &gd[(b + 1) * s..(b + 2) * s]); + let at = b * s; + let (orr, org, orb) = (&mut cr[at..at + s], &mut cg[at..at + s], &mut cb[at..at + s]); + let (ol, oa, ob) = (&mut ll[at..at + s], &mut la[at..at + s], &mut lb[at..at + s]); + let pr = (y & 1) * 2; + for x in 3..s - 3 { + let own = phase[pr + (x & 1)]; + let gv = g0[x]; + // `va` goes to channel `ch`, `vb` to `2 - ch` (as `px[ch] = va; px[2 - ch] = vb`, + // built from values: an indexed store into `px` stalls store forwarding). + let (ch, va, vb) = if own == 1 { + ( + phase[pr + ((x + 1) & 1)].min(2), + gv + 0.5 * ((d0[x - 1] - g0[x - 1]) + (d0[x + 1] - g0[x + 1])), + gv + 0.5 * ((dm1[x] - gm1[x]) + (dp1[x] - gp1[x])), + ) + } else { + ( + own.min(2), + d0[x], + gv + 0.25 * ((dm1[x - 1] - gm1[x - 1]) + (dm1[x + 1] - gm1[x + 1]) + (dp1[x - 1] - gp1[x - 1]) + (dp1[x + 1] - gp1[x + 1])), + ) + }; + let px = match ch { + 0 => [va, gv, vb], + 2 => [vb, gv, va], + _ => [gv, vb, gv], + }; + let px = px.map(|v| v.clamp(0.0, 1.0)); + orr[x] = px[0]; + org[x] = px[1]; + orb[x] = px[2]; + let xyz: [f32; 3] = std::array::from_fn(|k| m[k][0] * px[0] + m[k][1] * px[1] + m[k][2] * px[2]); + let (fx, fy, fz) = (labf.f(xyz[0]), labf.f(xyz[1]), labf.f(xyz[2])); + ol[x] = 116.0 * fy - 16.0; + oa[x] = 500.0 * (fx - fy); + ob[x] = 200.0 * (fy - fz); + } + } + } + // 3. Homogeneity maps, band rows 2..n-2, columns 4..s-4. + { + let [l0, a0, b0] = &sc.lab[0]; + let [l1, a1, b1] = &sc.lab[1]; + let [h0, h1] = &mut sc.hom; + for b in 2..n - 2 { + fn row(pl: &[f32], bb: usize, s: usize) -> &[f32] { + &pl[bb * s..(bb + 1) * s] + } + let (l0u, l0c, l0d) = (row(l0, b - 1, s), row(l0, b, s), row(l0, b + 1, s)); + let (a0u, a0c, a0d) = (row(a0, b - 1, s), row(a0, b, s), row(a0, b + 1, s)); + let (b0u, b0c, b0d) = (row(b0, b - 1, s), row(b0, b, s), row(b0, b + 1, s)); + let (l1u, l1c, l1d) = (row(l1, b - 1, s), row(l1, b, s), row(l1, b + 1, s)); + let (a1u, a1c, a1d) = (row(a1, b - 1, s), row(a1, b, s), row(a1, b + 1, s)); + let (b1u, b1c, b1d) = (row(b1, b - 1, s), row(b1, b, s), row(b1, b + 1, s)); + let (h0r, h1r) = (&mut h0[b * s..(b + 1) * s], &mut h1[b * s..(b + 1) * s]); + for x in 4..s - 4 { + // Neighbours left, right, up, down; (|ΔL|, Δa² + Δb²) from the centre. + let dist = |lc: f32, ac: f32, bc: f32, ln: f32, an: f32, bn: f32| ((lc - ln).abs(), (ac - an) * (ac - an) + (bc - bn) * (bc - bn)); + let (lc, ac, bc) = (l0c[x], a0c[x], b0c[x]); + let dh = [ + dist(lc, ac, bc, l0c[x - 1], a0c[x - 1], b0c[x - 1]), + dist(lc, ac, bc, l0c[x + 1], a0c[x + 1], b0c[x + 1]), + dist(lc, ac, bc, l0u[x], a0u[x], b0u[x]), + dist(lc, ac, bc, l0d[x], a0d[x], b0d[x]), + ]; + let (lc, ac, bc) = (l1c[x], a1c[x], b1c[x]); + let dv = [ + dist(lc, ac, bc, l1c[x - 1], a1c[x - 1], b1c[x - 1]), + dist(lc, ac, bc, l1c[x + 1], a1c[x + 1], b1c[x + 1]), + dist(lc, ac, bc, l1u[x], a1u[x], b1u[x]), + dist(lc, ac, bc, l1d[x], a1d[x], b1d[x]), + ]; + let eps_l = dh[0].0.max(dh[1].0).min(dv[2].0.max(dv[3].0)); + let eps_c = dh[0].1.max(dh[1].1).min(dv[2].1.max(dv[3].1)); + let count = |v: &[(f32, f32); 4]| v.iter().map(|(l, c)| u8::from(*l <= eps_l && *c <= eps_c)).sum::(); + h0r[x] = count(&dh); + h1r[x] = count(&dv); + } + } + } + // 4. Per pixel, the candidate with the larger 3×3 homogeneity sum. + let [vs0, vs1] = &mut sc.vs; + for r in 0..rows { + let b = r + M; + for (hm, vs) in [(&sc.hom[0], &mut *vs0), (&sc.hom[1], &mut *vs1)] { + let (u, c, dn) = (&hm[(b - 1) * s..b * s], &hm[b * s..(b + 1) * s], &hm[(b + 1) * s..(b + 2) * s]); + for x in PAD - 1..PAD + w + 1 { + vs[x] = u32::from(u[x]) + u32::from(c[x]) + u32::from(dn[x]); + } + } + let at = b * s; + let (r0, g0, b0) = (&sc.rgb[0][0][at..at + s], &sc.rgb[0][1][at..at + s], &sc.rgb[0][2][at..at + s]); + let (r1, g1, b1) = (&sc.rgb[1][0][at..at + s], &sc.rgb[1][1][at..at + s], &sc.rgb[1][2][at..at + s]); + let orow = &mut chunk[r * w * 3..(r + 1) * w * 3]; + for (x, o) in orow.as_chunks_mut::<3>().0.iter_mut().enumerate() { + let px = x + PAD; + let sh = vs0[px - 1] + vs0[px] + vs0[px + 1]; + let sv = vs1[px - 1] + vs1[px] + vs1[px + 1]; + let (c0, c1) = ([r0[px], g0[px], b0[px]], [r1[px], g1[px], b1[px]]); + *o = match sh.cmp(&sv) { + std::cmp::Ordering::Greater => c0, + std::cmp::Ordering::Less => c1, + std::cmp::Ordering::Equal => [0.5 * (c0[0] + c1[0]), 0.5 * (c0[1] + c1[1]), 0.5 * (c0[2] + c1[2])], + }; + } + } +} + +/// The plain AHD statement the optimized [`ahd`] must match bit for bit. +#[cfg(test)] +fn ahd_ref(p: &Padded, phase: [u8; 4], to_xyz: &Mat3, out: &mut [f32], jobs: usize) { let (w, s) = (p.w, p.stride); let d = &p.data; // Colour at padded coordinates (PAD is even, so the phase is unchanged). @@ -500,10 +863,10 @@ fn ahd(p: &Padded, phase: [u8; 4], to_xyz: &Mat3, out: &mut [f32], jobs: usize) } let (l, r) = (d[i - 1], d[i + 1]); let gh = (l + r) * 0.5 + (2.0 * c - d[i - 2] - d[i + 2]) * 0.25; - g[0][k] = gh.clamp(l.min(r), l.max(r)); + g[0][k] = clampf(gh, l.min(r), l.max(r)); let (u, dn) = (d[i - s], d[i + s]); let gv = (u + dn) * 0.5 + (2.0 * c - d[i - 2 * s] - d[i + 2 * s]) * 0.25; - g[1][k] = gv.clamp(u.min(dn), u.max(dn)); + g[1][k] = clampf(gv, u.min(dn), u.max(dn)); } } // 2. RGB candidates and their Lab, band rows 1..n-1, columns 3..s-3. @@ -588,6 +951,8 @@ fn ahd(p: &Padded, phase: [u8; 4], to_xyz: &Mat3, out: &mut [f32], jobs: usize) mod tests { use super::*; use crate::color::IDENTITY; + use std::sync::atomic::{AtomicUsize, Ordering::SeqCst}; + use std::time::{Duration, Instant}; #[test] fn ahd_band_concurrency_does_not_change_pixels() { @@ -600,6 +965,219 @@ mod tests { } } + #[test] + fn ahd_survives_nan_planes() { + let p = Padded::from_plane(&[f32::NAN; 64], 8, 8); + assert_eq!(demosaic(&p, [0, 1, 1, 2], Demosaic::Ahd, &IDENTITY, 1).len(), 192); + } + + #[test] + fn ahd_matches_reference_bit_for_bit() { + let m = [[0.41, 0.36, 0.18], [0.21, 0.72, 0.07], [0.02, 0.12, 0.95]]; + for (w, h) in [(1, 1), (2, 3), (7, 5), (53, 103), (64, 70), (33, 31)] { + let cfa: Vec = (0..w * h).map(|i| ((i * 7919 + i / w * 104729) % 1013) as f32 / 1000.0 - 0.006).collect(); + let p = Padded::from_plane(&cfa, w, h); + for phase in [[0, 1, 1, 2], [2, 1, 1, 0], [1, 0, 2, 1], [1, 2, 0, 1]] { + for jobs in [1, 3, 64] { + let mut want = vec![0.0f32; w * h * 3]; + ahd_ref(&p, phase, &m, &mut want, jobs); + let got = demosaic(&p, phase, Demosaic::Ahd, &m, jobs); + assert_eq!( + want.iter().map(|v| v.to_bits()).collect::>(), + got.iter().map(|v| v.to_bits()).collect::>(), + "{w}x{h} {phase:?} {jobs}" + ); + } + } + } + } + + #[test] + fn fused_demosaic_matches_the_full_frame_demosaic() { + let m = [[0.41, 0.36, 0.18], [0.21, 0.72, 0.07], [0.02, 0.12, 0.95]]; + // Sizes around the bands (AHD 32 rows, bilinear / MHC 32 rows) and the padding. + for (w, h) in [(1, 1), (2, 3), (7, 5), (53, 103), (64, 64), (33, 31), (5, 70), (31, 65)] { + let cfa: Vec = (0..w * h).map(|i| ((i * 7919 + i / w * 104729) % 1013) as f32 / 1000.0 - 0.006).collect(); + let p = Padded::from_plane(&cfa, w, h); + let fill = |y: usize, row: &mut [f32]| row.copy_from_slice(&cfa[y * w..(y + 1) * w]); + // Any per-sample map stands in for the encode. + let emit = |rgb: &[f32], out: &mut [u16]| { + for (o, v) in out.iter_mut().zip(rgb) { + *o = (v.clamp(0.0, 1.0) * 65535.0 + 0.5) as u16; + } + }; + for method in [Demosaic::Bilinear, Demosaic::Mhc, Demosaic::Ahd] { + for phase in [[0, 1, 1, 2], [2, 1, 1, 0], [1, 0, 2, 1], [1, 2, 0, 1]] { + for jobs in [1, 3, 64] { + let full = demosaic(&p, phase, method, &m, jobs); + let mut want = vec![0u16; w * h * 3]; + emit(&full, &mut want); + let mut got = vec![0u16; w * h * 3]; + demosaic_fused(w, h, phase, method, &m, jobs, &fill, &mut got, &emit); + assert_eq!(want, got, "{w}x{h} {method:?} {phase:?} {jobs}"); + } + } + } + } + } + + /// Bytes the scratch planes actually hold. + fn scratch_bytes(sc: &AhdScratch) -> u64 { + let f = sc.g.iter().chain(sc.rgb.iter().flatten()).chain(sc.lab.iter().flatten()).map(|v| v.capacity() * 4).sum::(); + let b = sc.hom.iter().map(Vec::capacity).sum::(); + let u = sc.vs.iter().map(|v| v.capacity() * 4).sum::(); + (f + b + u) as u64 + } + + /// The memory check in `develop` admits a file on [`ahd_scratch`] per worker; the scratch + /// `ahd` allocates must not exceed it. A short wide strip used to allocate the 38 rows of a + /// full band against a budget of `h + 6` rows (5.4x at `h = 1`). + #[test] + fn ahd_scratch_fits_its_budget() { + for w in [1usize, 2, 33, 4000] { + for h in [1usize, 2, 7, 31, 32, 33, 100, 5000] { + let s = w + 2 * PAD; + let sc = AhdScratch::for_image(h, s); + let budget = ahd_scratch(w as u64, h as u64); + assert_eq!(scratch_bytes(&sc), budget, "{w}x{h}: allocated vs budgeted bytes"); + // The tallest band `ahd` hands out (at most 32 rows, plus 3 above and below) fits. + assert!(sc.g[0].len() >= (h.min(32) + 6) * s, "{w}x{h}"); + } + } + } + + /// One scratch serves many bands (1100 rows are 35 bands, the last one short, over one scratch + /// with jobs 1 and two with jobs 2 on a bigger pool), so a stage reading a cell left over from + /// an earlier band would show. + #[test] + fn ahd_matches_reference_with_reused_scratch() { + let m = [[0.41, 0.36, 0.18], [0.21, 0.72, 0.07], [0.02, 0.12, 0.95]]; + let (w, h) = (37, 1100); + let cfa: Vec = (0..w * h).map(|i| ((i * 7919 + i / w * 104729) % 1013) as f32 / 1000.0 - 0.006).collect(); + let p = Padded::from_plane(&cfa, w, h); + for jobs in [1, 2] { + assert!(bands_per_group(ahd_bands(h), jobs, 1) >= 2, "jobs {jobs}: each scratch must serve several bands"); + for phase in [[0, 1, 1, 2], [2, 1, 1, 0], [1, 0, 2, 1], [1, 2, 0, 1]] { + let mut want = vec![0.0f32; w * h * 3]; + ahd_ref(&p, phase, &m, &mut want, jobs); + let got = demosaic(&p, phase, Demosaic::Ahd, &m, jobs); + assert_eq!(want.len(), got.len()); + assert!(want.iter().zip(&got).all(|(a, b)| a.to_bits() == b.to_bits()), "{w}x{h} {phase:?} jobs {jobs}"); + } + } + } + + /// On a 4-worker pool with room for 3 scratches, 35 bands go in groups of 12 (the last of 11), + /// so groups after the first start at band `group * 12` and every scratch is reused for several + /// bands; with room for 4, every band is its own task and most borrow a used scratch. A wrong band + /// index or a stale scratch cell would change pixels. The split does not depend on the machine. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn ahd_groups_of_several_bands_match_reference() { + let m = [[0.41, 0.36, 0.18], [0.21, 0.72, 0.07], [0.02, 0.12, 0.95]]; + let (w, h) = (37, 1100); + let cfa: Vec = (0..w * h).map(|i| ((i * 7919 + i / w * 104729) % 1013) as f32 / 1000.0 - 0.006).collect(); + let p = Padded::from_plane(&cfa, w, h); + let pool = rayon::ThreadPoolBuilder::new().num_threads(4).build().unwrap(); + pool.install(|| { + assert_eq!(par::workers(), 4); + assert_eq!(bands_per_group(ahd_bands(h), 3, 1), 12); + // One group per job, never more groups than jobs, however many bands there are. + assert_eq!(bands_per_group(126, 3, 1), 42); + for jobs in [3, 4] { + for phase in [[0, 1, 1, 2], [1, 2, 0, 1]] { + let mut want = vec![0.0f32; w * h * 3]; + ahd_ref(&p, phase, &m, &mut want, jobs); + let got = demosaic(&p, phase, Demosaic::Ahd, &m, jobs); + assert!(want.iter().zip(&got).all(|(a, b)| a.to_bits() == b.to_bits()), "{phase:?} jobs {jobs}"); + } + } + }); + } + + /// A scratch that counts how many are alive at once. + struct Live<'a>(&'a AtomicUsize); + + impl<'a> Live<'a> { + fn new(now: &'a AtomicUsize, peak: &AtomicUsize) -> Self { + peak.fetch_max(now.fetch_add(1, SeqCst) + 1, SeqCst); + Live(now) + } + } + + impl Drop for Live<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, SeqCst); + } + } + + /// Every band runs once with its own index, and no more than `jobs` scratches (what `develop`'s + /// memory check admits) or one per worker are alive at once, on either side of `jobs == workers`. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn for_bands_keeps_at_most_jobs_scratches_alive() { + let pool = rayon::ThreadPoolBuilder::new().num_threads(8).build().unwrap(); + pool.install(|| { + for jobs in [1, 2, 3, 7, 8, 9, usize::MAX] { + for per_worker in [1, 4] { + let (now, peak) = (AtomicUsize::new(0), AtomicUsize::new(0)); + let mut out = vec![0usize; 37 * 10 - 4]; + for_bands_grouped( + &mut out, + 10, + 37, + jobs, + per_worker, + || Live::new(&now, &peak), + |band, chunk, _| { + std::thread::sleep(Duration::from_millis(1)); + chunk.iter_mut().for_each(|v| *v += band + 1); + }, + ); + assert!(out.iter().enumerate().all(|(i, &v)| v == i / 10 + 1), "jobs {jobs}"); + let peak = peak.load(SeqCst); + assert!((1..=jobs.min(8)).contains(&peak), "jobs {jobs} per_worker {per_worker}: {peak} scratches alive"); + assert_eq!(now.load(SeqCst), 0); + } + } + }); + } + + /// With room for a scratch on every worker, the bands are separate rayon tasks: a worker held up + /// on band 0 (by other work on the shared pool, or a slow core) leaves the bands after it to + /// the others. With a fixed share of 9 bands per worker, 8 of them would wait for band 0, so at + /// most 26 of the other 34 could finish meanwhile. + #[cfg(not(target_arch = "wasm32"))] + #[test] + fn a_slow_band_does_not_hold_up_the_bands_after_it() { + let pool = rayon::ThreadPoolBuilder::new().num_threads(4).build().unwrap(); + pool.install(|| { + let (done, seen) = (AtomicUsize::new(0), AtomicUsize::new(0)); + let mut out = vec![0u8; 35]; + for_bands_grouped( + &mut out, + 1, + 35, + 4, + 1, + || (), + |band, _, _| { + if band > 0 { + done.fetch_add(1, SeqCst); + return; + } + let start = Instant::now(); + while done.load(SeqCst) <= 26 && start.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(1)); + } + seen.store(done.load(SeqCst), SeqCst); + }, + ); + assert_eq!(done.load(SeqCst), 34); + assert!(seen.load(SeqCst) > 26, "only {} bands ran while band 0 was held up", seen.load(SeqCst)); + }); + } + /// Mosaics an RGB image with the given phase. fn mosaic(rgb: &[[f32; 3]], w: usize, h: usize, phase: [u8; 4]) -> Vec { (0..w * h).map(|i| rgb[i][phase[((i / w) & 1) * 2 + ((i % w) & 1)] as usize]).collect() diff --git a/crates/raw/src/develop.rs b/crates/raw/src/develop.rs index e3ab70b0c7..aaeb390eff 100644 --- a/crates/raw/src/develop.rs +++ b/crates/raw/src/develop.rs @@ -5,7 +5,7 @@ //! orientation. use crate::color::{self, Mat3}; -use crate::demosaic::{Demosaic, PAD, Padded, Pattern, demosaic, demosaic_cfa}; +use crate::demosaic::{Demosaic, PAD, Padded, Pattern, demosaic, demosaic_cfa, demosaic_cfa_fused, demosaic_fused, for_bands}; use crate::error::{RawError, Result}; use crate::look::{CameraLook, ToneCurve}; use crate::sensor::Sensor; @@ -194,6 +194,12 @@ pub(crate) fn resolve_neutral(s: &Sensor, opts: &DevelopOptions, sc: &Scale, war /// Develops sensor data. pub fn develop_sensor(s: &Sensor, opts: &DevelopOptions) -> Result { + develop(s, opts, true) +} + +/// [`develop_sensor`]; `allow_fused` false keeps the full-frame path (what the tests compare the +/// fused one against). +fn develop(s: &Sensor, opts: &DevelopOptions, allow_fused: bool) -> Result { let c = s.crop; let samples = s.width.checked_mul(s.height).and_then(|n| n.checked_mul(s.samples)); if c.is_empty() @@ -219,7 +225,12 @@ pub fn develop_sensor(s: &Sensor, opts: &DevelopOptions) -> Result { return Err(RawError::malformed("invalid sensor CFA pattern")); } let bayer = s.cfa.as_ref().is_none_or(|cfa| cfa.is_bayer()); - let ahd_jobs = working_memory(c.width, c.height, s.cfa.is_some(), bayer, opts, par::workers())?; + // The default rendering (no Dehaze, no local tone, no black point) needs no statistic of + // the whole frame: demosaic and encode band by band, with no full RGB frame (the same + // condition under which `Haze::new` and `ToneMap::new` return `None`). + let fused = allow_fused && opts.tone.is_neutral() && s.black_point <= 0.0; + let orient_copy = opts.orient && (2..=8).contains(&s.orientation); + let jobs = working_memory(c.width, c.height, s.cfa.is_some(), bayer, Shape { fused, orient_copy }, opts, par::workers())?; let mut warnings = s.warnings.clone(); let sc = Scale::new(s); @@ -256,143 +267,207 @@ pub fn develop_sensor(s: &Sensor, opts: &DevelopOptions) -> Result { let grey_gain = gain as f32; let (w, h) = (c.width, c.height); let band = par::band_rows(w); - let mut rgb: Vec; - let pattern: Option; - match &s.cfa { - Some(cfa) => { - // Normalized, balanced, clipped CFA plane. - let mut plane = Padded::new(w, h); - let stride = plane.stride; - // Fast path: black varies at most per 2×2 position, so black and gain - // repeat every `period` columns (the CFA width, made even). - let bl = &s.black; - let periodic = bl.delta_h.is_empty() && bl.delta_v.is_empty() && matches!(bl.rows, 1 | 2) && matches!(bl.cols, 1 | 2) && cfa.width <= 16; - let period = if cfa.width % 2 == 0 { cfa.width.max(2) } else { cfa.width * 2 }; - plane.fill_rows(band, |y0, chunk| { - for (r, prow) in chunk.chunks_exact_mut(stride).enumerate() { - let row = &mut prow[PAD..PAD + w]; - let y = c.y + y0 + r; - if periodic { - // Per column of the period: black and the combined scale × white-balance gain. - let k: Vec<(f32, f32)> = (0..period) - .map(|p| { - let x = c.x + p; - let black = bl.at(x.saturating_sub(s.active.x), y.saturating_sub(s.active.y), 0, 1); - let range = s.white[0] - black; - let gain = if range >= 1.0 { mult[usize::from(cfa.color(x, y)).min(2)] / range } else { 0.0 }; - (black, gain) - }) - .collect(); - let src = &s.data[y * s.width + c.x..y * s.width + c.x + w]; - let ks = k.iter().cycle(); - match &sc.lin { - None => { - for ((v, &raw), &(black, gain)) in row.iter_mut().zip(src).zip(ks) { - *v = ((f32::from(raw) - black) * gain).clamp(0.0, 1.0); - } - } - Some(l) => { - for ((v, &raw), &(black, gain)) in row.iter_mut().zip(src).zip(ks) { - let lin = f32::from(l.get(usize::from(raw)).or_else(|| l.last()).copied().unwrap_or(raw)); - *v = ((lin - black) * gain).clamp(0.0, 1.0); - } - } - } - } else { - for (i, v) in row.iter_mut().enumerate() { - let x = c.x + i; - let ch = usize::from(cfa.color(x, y)).min(2); - *v = (sc.value(x, y, 0) * mult[ch]).clamp(0.0, 1.0); - } - } - // Lens-shading gain maps, after the white-balance clip so clipped - // highlights stay neutral. - if let Some(g) = gain_row(s, y, c.x, w, 0) { - for (v, g) in row.iter_mut().zip(g) { - *v = (*v * g).clamp(0.0, 1.0); - } - } - } - }); - plane.fill_borders(); - let names = |p: &[u8]| p.iter().map(|c| ['R', 'G', 'B'][usize::from(*c).min(2)]).collect::(); - if bayer { - let p = cfa.phase(c.x, c.y); - pattern = Some(names(&p)); - rgb = demosaic(&plane, p, opts.demosaic, &to_xyz, ahd_jobs); - } else { - let pat = Pattern::of(cfa, c.x, c.y); - pattern = Some(pat.colors.chunks(pat.w.max(1)).map(names).collect::>().join("/")); - rgb = demosaic_cfa(&plane, &pat); - } - drop(plane); - } - None => { - pattern = None; - rgb = vec![0.0f32; w * h * 3]; - par::chunks_mut(&mut rgb, band * w * 3, |b, chunk| { - for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() { - let y = c.y + b * band + r; - for i in 0..w { - for k in 0..3 { - // Monochrome: the one sample in all three channels. - let sample = k.min(s.samples.saturating_sub(1)); - row[i * 3 + k] = (sc.value(c.x + i, y, sample) * mult[k]).clamp(0.0, 1.0); - } - } - for k in 0..s.samples.min(3) { - if let Some(g) = gain_row(s, y, c.x, w, k) { - for (i, g) in g.into_iter().enumerate() { - row[i * 3 + k] = (row[i * 3 + k] * g).clamp(0.0, 1.0); - } - } - } - } - }); - } - } - // Camera → ProPhoto, exposure, Dehaze, Shadows / Highlights / Clarity / Texture and the black // point, the look's (or the profile's) tone curve, gamma 1.8, 16 bits. let y_row = color::rgb_to_xyz(color::ROMM, color::D50_XY)[1].map(|v| v as f32); let lum = |v: &[f32; 3]| y_row[0] * v[0] + y_row[1] * v[1] + y_row[2] * v[2]; // Monochrome: the one sample, grey; no colour matrix. let pro = |p: &[f32]| if mono { [grey_gain * p[0]; 3] } else { [0, 1, 2].map(|k| out_m[k][0] * p[0] + out_m[k][1] * p[1] + out_m[k][2] * p[2]) }; - let haze = Haze::new(w, h, |i| pro(&rgb[i * 3..i * 3 + 3]), opts.tone.dehaze); - // Linear ProPhoto of pixel `i` after Dehaze. - let linear = |i: usize, p: &[f32]| { - let v = pro(p); - haze.as_ref().map_or(v, |z| z.apply(i % w, i / w, v)) - }; - let mut map = ToneMap::new(w, h, |i| lum(&linear(i, &rgb[i * 3..i * 3 + 3])), opts.tone, s.black_point); - if let (Some(m), Some(_)) = (&mut map, &haze) { - // The black point belongs to the default rendering: Dehaze does not move it (taken after - // Dehaze it fell to almost 0, and Dehaze darkened less than Photoshop's). - m.black = black_level(w, h, |i| lum(&pro(&rgb[i * 3..i * 3 + 3])), s.black_point); - } - let mut out = vec![0u16; w * h * 3]; let lut = match look { Some(l) => GammaLut::with_tone(l.tone.as_ref()), None => GammaLut::with_curve(&s.tone_curve), }; - par::chunks_mut(&mut out, band * w * 3, |b, chunk| { - let start = b * band * w * 3; - let src = &rgb[start..start + chunk.len()]; - for (i, (o, p)) in chunk.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<3>().0).enumerate() { - let mut v = linear(b * band * w + i, p); - if let Some(m) = &map { - let g = m.gain(i % w, b * band + i / w, lum(&v)); - // The black point first, as Camera Raw: lifting before it would flatten the - // deepest shadows' contrast. - v = v.map(|c| m.unblack(c) * g); + // The default rendering: matrix then encode, in blocks so the encode's clamp and square root + // run over whole slices. + let encode_plain = |src: &[f32], dst: &mut [u16]| { + let mut buf = [0.0f32; ENCODE_BLOCK]; + for (oc, sc) in dst.chunks_mut(ENCODE_BLOCK).zip(src.chunks(ENCODE_BLOCK)) { + let n = sc.len(); + for (t, p) in buf[..n].as_chunks_mut::<3>().0.iter_mut().zip(sc.as_chunks::<3>().0) { + *t = pro(p); } - for k in 0..3 { - o[k] = lut.encode(v[k]); + lut.encode_into(&buf[..n], oc); + } + }; + // Rows `y0..` of a LinearRaw (3-sample or monochrome) frame, balanced and clipped. + let linear_rows = |y0: usize, chunk: &mut [f32]| { + for (r, row) in chunk.chunks_exact_mut(w * 3).enumerate() { + let y = c.y + y0 + r; + for i in 0..w { + for k in 0..3 { + // Monochrome: the one sample in all three channels. + let sample = k.min(s.samples.saturating_sub(1)); + row[i * 3 + k] = (sc.value(c.x + i, y, sample) * mult[k]).clamp(0.0, 1.0); + } + } + for k in 0..s.samples.min(3) { + if let Some(g) = gain_row(s, y, c.x, w, k) { + for (i, g) in g.into_iter().enumerate() { + row[i * 3 + k] = (row[i * 3 + k] * g).clamp(0.0, 1.0); + } + } } } - }); - rgb.clear(); - rgb.shrink_to_fit(); + }; + let mut rgb: Vec = Vec::new(); + // The encoded frame, when the fused path made it. + let mut fused_out: Option> = None; + let pattern: Option; + match &s.cfa { + Some(cfa) => { + // One normalized, balanced, clipped CFA row (image row `iy` of the crop). + // Fast path: black varies at most per 2×2 position, so black and gain + // repeat every `period` columns (the CFA width, made even). + let bl = &s.black; + let periodic = bl.delta_h.is_empty() && bl.delta_v.is_empty() && matches!(bl.rows, 1 | 2) && matches!(bl.cols, 1 | 2) && cfa.width <= 16; + let period = if cfa.width % 2 == 0 { cfa.width.max(2) } else { cfa.width * 2 }; + let fill_row = |iy: usize, row: &mut [f32]| { + let y = c.y + iy; + if periodic { + // Per column of the period: black and the combined scale × white-balance gain. + let k: Vec<(f32, f32)> = (0..period) + .map(|p| { + let x = c.x + p; + let black = bl.at(x.saturating_sub(s.active.x), y.saturating_sub(s.active.y), 0, 1); + let range = s.white[0] - black; + // A non-finite black (a BlackLevel DOUBLE beyond f32) gives 0, as + // `Scale::value` does, instead of (raw - inf) * 0 = NaN. + if !(range >= 1.0 && black.is_finite()) { + return (0.0, 0.0); + } + (black, mult[usize::from(cfa.color(x, y)).min(2)] / range) + }) + .collect(); + let src = &s.data[y * s.width + c.x..y * s.width + c.x + w]; + let ks = k.iter().cycle(); + match &sc.lin { + None => { + for ((v, &raw), &(black, gain)) in row.iter_mut().zip(src).zip(ks) { + *v = ((f32::from(raw) - black) * gain).clamp(0.0, 1.0); + } + } + Some(l) => { + for ((v, &raw), &(black, gain)) in row.iter_mut().zip(src).zip(ks) { + let lin = f32::from(l.get(usize::from(raw)).or_else(|| l.last()).copied().unwrap_or(raw)); + *v = ((lin - black) * gain).clamp(0.0, 1.0); + } + } + } + } else { + for (i, v) in row.iter_mut().enumerate() { + let x = c.x + i; + let ch = usize::from(cfa.color(x, y)).min(2); + *v = (sc.value(x, y, 0) * mult[ch]).clamp(0.0, 1.0); + } + } + // Lens-shading gain maps, after the white-balance clip so clipped + // highlights stay neutral. + if let Some(g) = gain_row(s, y, c.x, w, 0) { + for (v, g) in row.iter_mut().zip(g) { + *v = (*v * g).clamp(0.0, 1.0); + } + } + }; + let names = |p: &[u8]| p.iter().map(|c| ['R', 'G', 'B'][usize::from(*c).min(2)]).collect::(); + if bayer && fused { + // No padded plane either: every band normalizes its own rows. + let p = cfa.phase(c.x, c.y); + pattern = Some(names(&p)); + let mut o = vec![0u16; w * h * 3]; + demosaic_fused(w, h, p, opts.demosaic, &to_xyz, jobs, &fill_row, &mut o, &encode_plain); + fused_out = Some(o); + } else { + let mut plane = Padded::new(w, h); + let stride = plane.stride; + plane.fill_rows(band, |y0, chunk| { + for (r, prow) in chunk.chunks_exact_mut(stride).enumerate() { + fill_row(y0 + r, &mut prow[PAD..PAD + w]); + } + }); + plane.fill_borders(); + if bayer { + let p = cfa.phase(c.x, c.y); + pattern = Some(names(&p)); + rgb = demosaic(&plane, p, opts.demosaic, &to_xyz, jobs); + } else { + let pat = Pattern::of(cfa, c.x, c.y); + pattern = Some(pat.colors.chunks(pat.w.max(1)).map(names).collect::>().join("/")); + if fused { + let mut o = vec![0u16; w * h * 3]; + demosaic_cfa_fused(&plane, &pat, jobs, &mut o, &encode_plain); + fused_out = Some(o); + } else { + rgb = demosaic_cfa(&plane, &pat); + } + } + } + } + None => { + pattern = None; + if fused { + let mut o = vec![0u16; w * h * 3]; + for_bands( + &mut o, + band * w * 3, + h.div_ceil(band), + jobs, + || vec![0.0f32; band.min(h) * w * 3], + |b, chunk, buf| { + let buf = &mut buf[..chunk.len()]; + linear_rows(b * band, buf); + encode_plain(buf, chunk); + }, + ); + fused_out = Some(o); + } else { + rgb = vec![0.0f32; w * h * 3]; + par::chunks_mut(&mut rgb, band * w * 3, |b, chunk| linear_rows(b * band, chunk)); + } + } + } + + let out = match fused_out { + Some(out) => out, + None => { + let haze = Haze::new(w, h, |i| pro(&rgb[i * 3..i * 3 + 3]), opts.tone.dehaze); + // Linear ProPhoto of pixel `i` after Dehaze. + let linear = |i: usize, p: &[f32]| { + let v = pro(p); + haze.as_ref().map_or(v, |z| z.apply(i % w, i / w, v)) + }; + let mut map = ToneMap::new(w, h, |i| lum(&linear(i, &rgb[i * 3..i * 3 + 3])), opts.tone, s.black_point); + if let (Some(m), Some(_)) = (&mut map, &haze) { + // The black point belongs to the default rendering: Dehaze does not move it (taken after + // Dehaze it fell to almost 0, and Dehaze darkened less than Photoshop's). + m.black = black_level(w, h, |i| lum(&pro(&rgb[i * 3..i * 3 + 3])), s.black_point); + } + let mut out = vec![0u16; w * h * 3]; + let plain = map.is_none() && haze.is_none(); + par::chunks_mut(&mut out, band * w * 3, |b, chunk| { + let start = b * band * w * 3; + let src = &rgb[start..start + chunk.len()]; + if plain { + encode_plain(src, chunk); + return; + } + for (i, (o, p)) in chunk.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<3>().0).enumerate() { + let mut v = linear(b * band * w + i, p); + if let Some(m) = &map { + let g = m.gain(i % w, b * band + i / w, lum(&v)); + // The black point first, as Camera Raw: lifting before it would flatten the + // deepest shadows' contrast. + v = v.map(|c| m.unblack(c) * g); + } + for k in 0..3 { + o[k] = lut.encode(v[k]); + } + } + }); + rgb.clear(); + rgb.shrink_to_fit(); + out + } + }; let (out, ow, oh) = if opts.orient { orient(out, w, h, s.orientation) } else { (out, w, h) }; let nmax = mult.iter().cloned().fold(f32::MAX, f32::min).max(1e-6); @@ -415,29 +490,79 @@ pub fn develop_sensor(s: &Sensor, opts: &DevelopOptions) -> Result { }) } +/// How the frame is developed, for [`working_memory`]. +#[derive(Clone, Copy)] +struct Shape { + /// Demosaic and encode band by band (no full RGB frame). + fused: bool, + /// The orientation copies the encoded frame. + orient_copy: bool, +} + +/// Bytes one worker of the fused path holds for a band of `band_rows` rows: the f32 RGB band +/// and, for a Bayer sensor, the padded CFA window it reads. +fn fused_band_bytes(w: usize, h: usize, band_rows: usize, window: bool) -> u64 { + let rows = band_rows.min(h) as u64; + let rgb = rows.saturating_mul(w as u64).saturating_mul(12); + let window = if window { (rows + 2 * PAD as u64).saturating_mul((w + 2 * PAD) as u64).saturating_mul(4) } else { 0 }; + rgb.saturating_add(window) +} + /// Checks the peak of the two development phases. The CFA and non-Bayer green -/// planes are freed before the u16 output is allocated; AHD scratch is bounded -/// by reducing simultaneous bands when needed. Sensor storage belongs to the -/// caller and is checked separately by the decoder. -fn working_memory(w: usize, h: usize, cfa: bool, bayer: bool, opts: &DevelopOptions, workers: usize) -> Result { +/// planes are freed before the u16 output is allocated; the AHD scratch and the fused band +/// buffers are bounded by reducing simultaneous bands when needed. The fused path never holds +/// the f32 RGB frame: it has the u16 output (6 bytes a pixel) next to the CFA plane, plus a band +/// buffer and the scratch per simultaneous band, and an orientation copy of the output afterwards. Sensor storage belongs +/// to the caller and is checked separately by the decoder. +fn working_memory(w: usize, h: usize, cfa: bool, bayer: bool, shape: Shape, opts: &DevelopOptions, workers: usize) -> Result { let limits = &opts.limits; - limits.check(w as u64, h as u64, 18)?; + let Shape { fused, orient_copy } = shape; + limits.check(w as u64, h as u64, if fused { 6 } else { 18 })?; let pixels = (w as u64).saturating_mul(h as u64); - let output = pixels.saturating_mul(18).saturating_add((GAMMA_STEPS as u64 + 2) * 4); - let mut peak = output; + let table = (GAMMA_STEPS as u64 + 2) * 4; + // Encode phase: the f32 RGB frame and the output, or (fused) the output and its oriented copy. + let output = pixels.saturating_mul(match (fused, orient_copy) { + (false, _) => 18, + (true, false) => 6, + (true, true) => 12, + }); + let mut peak = output.saturating_add(table); let mut jobs = workers.max(1); + // Fused: the u16 output and, per worker running a band, one band of f32 RGB. + let band_of = |rows: usize, window: bool| if fused { fused_band_bytes(w, h, rows, window) } else { 0 }; + // Fewer bands at once when the bands of every worker don't fit next to `base` (at least one). + let with_bands = |jobs: &mut usize, base: u64, rows: usize, band: u64| { + let available = limits.max_alloc.saturating_sub(base) / band.max(1); + *jobs = (*jobs).min(h.div_ceil(rows.max(1))).min(usize::try_from(available).unwrap_or(usize::MAX)).max(1); + base.saturating_add(band.saturating_mul(*jobs as u64)) + }; if cfa { let plane = (w as u64).saturating_add(2 * PAD as u64).saturating_mul((h as u64).saturating_add(2 * PAD as u64)).saturating_mul(4); - let mut mosaic = pixels.saturating_mul(if bayer { 12 } else { 16 }).saturating_add(plane); + // Bytes a pixel next to the plane: the f32 RGB frame (and the non-Bayer green plane), or + // the u16 output (and the green plane, which the fused path keeps). + let frame = match (fused, bayer) { + (false, true) => 12, + (false, false) => 16, + (true, true) => 6, + (true, false) => 10, + }; + // The fused Bayer path has no whole plane: each worker's window is counted with its band. + let plane = if fused && bayer { 0 } else { plane }; + let mut mosaic = pixels.saturating_mul(frame).saturating_add(plane).saturating_add(table); if bayer && opts.demosaic == Demosaic::Ahd { mosaic = mosaic.saturating_add(crate::demosaic::AHD_TABLE_BYTES); - let scratch = crate::demosaic::ahd_scratch(w as u64, h as u64); - let available = limits.max_alloc.saturating_sub(mosaic) / scratch.max(1); - jobs = jobs.min(crate::demosaic::ahd_bands(h)).min(usize::try_from(available).unwrap_or(usize::MAX)); + let scratch = crate::demosaic::ahd_scratch(w as u64, h as u64).saturating_add(band_of(crate::demosaic::AHD_BAND, true)); // At least one band must fit, even when no parallelism is available. - mosaic = mosaic.saturating_add(scratch.saturating_mul(jobs.max(1) as u64)); + mosaic = with_bands(&mut jobs, mosaic, crate::demosaic::AHD_BAND, scratch); + } else { + let rows = if bayer { crate::demosaic::FUSED_ROWS } else { par::band_rows(w) }; + mosaic = with_bands(&mut jobs, mosaic, rows, band_of(rows, bayer)); } peak = peak.max(mosaic); + } else if fused { + // LinearRaw: the output and the bands. + let rows = par::band_rows(w); + peak = peak.max(with_bands(&mut jobs, pixels.saturating_mul(6), rows, band_of(rows, false))); } if peak > limits.max_alloc || usize::try_from(peak).is_err() { return Err(RawError::LimitExceeded(format!("{peak} bytes of RAW working memory exceed {}", limits.max_alloc))); @@ -460,12 +585,11 @@ fn gain_row(s: &Sensor, y: usize, x0: usize, w: usize, k: usize) -> Option= m.plane.saturating_add(m.planes) || ay < m.top || ay >= m.bottom || !(ay - m.top).is_multiple_of(m.row_pitch) { continue; } - let row = m.row(rv, k - m.plane); let o = out.get_or_insert_with(|| vec![1.0; w]); for (i, f) in o.iter_mut().enumerate() { let x = x0 + i; if x >= a.x && m.covers(x - a.x, ay) { - *f *= m.at(&row, ((x - a.x) as f64 + 0.5) / a.width as f64); + *f *= m.at_rv(rv, k - m.plane, ((x - a.x) as f64 + 0.5) / a.width as f64); } } } @@ -477,9 +601,13 @@ fn gain_row(s: &Sensor, y: usize, x0: usize, w: usize, k: usize) -> Option, + /// `[table[i], table[i + 1] - table[i]]`: the same interpolation from one load. + pairs: Vec<[f32; 2]>, } const GAMMA_STEPS: usize = 4096; +/// Samples per block of [`GammaLut::encode_into`] (whole pixels). +const ENCODE_BLOCK: usize = 3 * 256; impl GammaLut { #[cfg(test)] @@ -495,7 +623,7 @@ impl GammaLut { tone(curve, s * s).clamp(0.0, 1.0).powf(1.0 / 1.8) * 65535.0 }) .collect(); - GammaLut { table } + Self::from_table(table) } /// The gamma-1.8 encoding after a fitted [`ToneCurve`] (`None`: none, plain gamma 1.8). @@ -509,7 +637,30 @@ impl GammaLut { } }) .collect(); - GammaLut { table } + Self::from_table(table) + } + + fn from_table(table: Vec) -> Self { + let pairs = table.windows(2).map(|p| [p[0], p[1] - p[0]]).collect(); + GammaLut { table, pairs } + } + + /// [`Self::encode`] of every sample of `v` into `out` (at most [`ENCODE_BLOCK`] samples): + /// the clamp and square root first over the whole block, then the table. + #[inline] + fn encode_into(&self, v: &[f32], out: &mut [u16]) { + let mut xs = [0.0f32; ENCODE_BLOCK]; + let n = v.len().min(ENCODE_BLOCK).min(out.len()); + for (x, &v) in xs[..n].iter_mut().zip(&v[..n]) { + let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; + *x = v.sqrt() * GAMMA_STEPS as f32; + } + for (o, &x) in out[..n].iter_mut().zip(&xs[..n]) { + let i = (x as usize).min(GAMMA_STEPS); + let f = x - i as f32; + let [a, step] = self.pairs[i]; + *o = (a + step * f + 0.5) as u16; + } } #[inline] @@ -535,7 +686,8 @@ pub(crate) fn tone(curve: &[[f32; 2]], v: f32) -> f32 { } let i = curve.partition_point(|p| p[0] <= v).clamp(1, curve.len() - 1); let ([x0, y0], [x1, y1]) = (curve[i - 1], curve[i]); - let f = (v.log2() - x0.log2()) / (x1.log2() - x0.log2()); + // Logarithmic between knots; linear from a knot at black, where log₂ has no value. + let f = if x0 > 0.0 { (v.log2() - x0.log2()) / (x1.log2() - x0.log2()) } else { (v - x0) / (x1 - x0) }; y0 + (y1 - y0) * f } @@ -589,32 +741,234 @@ pub(crate) fn orient(src: Vec, w: usize, h: usize, o: u16) -> (Vec, us mod tests { use super::*; + const FULL: Shape = Shape { fused: false, orient_copy: false }; + const FUSED: Shape = Shape { fused: true, orient_copy: false }; + + /// A `w × h` sensor with a pseudo-random gradient, black 64, white 4000. + fn test_sensor(w: usize, h: usize, cfa: Option, samples: usize) -> Sensor { + use crate::*; + let data = (0..w * h * samples).map(|i| 64 + ((i * 7919 + i / w * 104_729 + i / 5 * 31) % 4100) as u16).collect(); + Sensor { + format: RawFormat::Dng, + make: None, + model: None, + width: w, + height: h, + samples, + data, + cfa, + linearization: None, + black: BlackLevels::uniform(64.0), + white: [4000.0; 3], + active: Rect::new(0, 0, w, h), + crop: Rect::new(0, 0, w, h), + color: ColorInfo::default(), + camera_wb: None, + orientation: 1, + baseline_exposure: 0.0, + gain_maps: Vec::new(), + tone_curve: vec![[0.18, 0.3], [1.0, 1.0]], + black_point: 0.0, + warnings: Vec::new(), + } + } + + /// `f` on a pool of `n` threads (the browser builds have none). + fn with_threads(n: usize, f: impl FnOnce() -> R + Send) -> R { + #[cfg(not(target_arch = "wasm32"))] + return rayon::ThreadPoolBuilder::new().num_threads(n).build().unwrap().install(f); + #[cfg(target_arch = "wasm32")] + { + let _ = n; + f() + } + } + + fn assert_fused_matches_full(s: &Sensor, opts: &DevelopOptions, what: &str) { + let full = develop(s, opts, false).unwrap(); + for threads in [1, 3, 16] { + let fused = with_threads(threads, || develop(s, opts, true).unwrap()); + assert_eq!((full.width, full.height), (fused.width, fused.height), "{what} {threads}"); + assert!(full.rgb == fused.rgb, "{what} {threads} threads: fused output differs"); + } + } + + #[test] + fn fused_development_matches_the_full_frame_path() { + use crate::Cfa; + // Odd sizes, tiny images, band borders (AHD bands are 32 rows) and several bands. + let sizes = [(1, 1), (2, 3), (5, 3), (33, 31), (64, 64), (65, 97), (70, 131), (3, 300)]; + for (w, h) in sizes { + for method in [Demosaic::Ahd, Demosaic::Bilinear, Demosaic::Mhc] { + for orientation in [1, 6] { + for pattern in ["RGGB", "BGGR", "GRBG"] { + let mut s = test_sensor(w, h, Cfa::bayer(pattern), 1); + s.orientation = orientation; + let opts = DevelopOptions { demosaic: method, ..Default::default() }; + assert_fused_matches_full(&s, &opts, &format!("{w}x{h} {method:?} o{orientation} {pattern}")); + } + } + } + } + } + + #[test] + fn fused_development_matches_for_every_orientation_and_look() { + use crate::Cfa; + for orientation in 1..=8 { + let mut s = test_sensor(67, 45, Cfa::bayer("RGGB"), 1); + s.orientation = orientation; + s.baseline_exposure = 0.5; + s.linearization = Some((0..4100u32).map(|v| (v * 15 / 16) as u16).collect()); + assert_fused_matches_full(&s, &DevelopOptions::default(), &format!("orientation {orientation}")); + } + } + + #[test] + fn fused_development_matches_for_xtrans_and_linear_raw() { + use crate::Cfa; + // 6 × 6 X-Trans repeat. + let rows = ["GGRGGB", "GGBGGR", "BRGRBG", "GGBGGR", "GGRGGB", "RBGBRG"]; + let colors: Vec = rows + .iter() + .flat_map(|r| { + r.bytes().map(|c| match c { + b'R' => 0, + b'G' => 1, + _ => 2, + }) + }) + .collect(); + // 600 samples wide: bands of 109 rows, so 350 rows are four (the last short) and 50 rows + // are less than one. + for (w, h) in [(6, 6), (37, 41), (80, 150), (600, 350), (600, 50)] { + let mut s = test_sensor(w, h, Some(Cfa { width: 6, height: 6, colors: colors.clone(), origin_x: 0, origin_y: 0 }), 1); + s.orientation = 8; + assert_fused_matches_full(&s, &DevelopOptions::default(), &format!("xtrans {w}x{h}")); + } + for samples in [1, 3] { + for (w, h) in [(1, 1), (7, 5), (90, 111), (600, 350), (600, 50)] { + let mut s = test_sensor(w, h, None, samples); + s.orientation = 3; + assert_fused_matches_full(&s, &DevelopOptions::default(), &format!("linear {samples} {w}x{h}")); + } + } + } + + #[test] + fn fused_only_where_no_whole_frame_statistic_is_needed() { + use crate::Cfa; + // The condition for the fused path is exactly "Haze and ToneMap are None". + let px = |_: usize| [0.5f32; 3]; + for tone in [Tone::NEUTRAL, Tone { shadows: 10.0, ..Tone::NEUTRAL }, Tone { dehaze: 5.0, ..Tone::NEUTRAL }, Tone { texture: 1.0, ..Tone::NEUTRAL }] { + for black_point in [0.0, 0.5] { + let none = Haze::new(8, 8, px, tone.dehaze).is_none() && ToneMap::new(8, 8, |_| 0.5, tone, black_point).is_none(); + assert_eq!(none, tone.is_neutral() && black_point <= 0.0, "{tone:?} {black_point}"); + } + } + // A tone slider or a black point is still developed (differently from the plain rendering). + let s = test_sensor(40, 40, Cfa::bayer("RGGB"), 1); + let plain = develop_sensor(&s, &DevelopOptions::default()).unwrap(); + let toned = develop_sensor(&s, &DevelopOptions { tone: Tone { shadows: 50.0, ..Tone::NEUTRAL }, ..Default::default() }).unwrap(); + assert!(plain.rgb != toned.rgb); + } + + #[test] + fn block_encode_matches_per_sample_encode() { + let lut = GammaLut::with_curve(&[[0.25, 0.5], [1.0, 1.0]]); + let v: Vec = (0..2000).map(|i| (i as f32 - 300.0) / 1500.0).chain([f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0, 1.0]).collect(); + for n in [0, 1, 3, 255, ENCODE_BLOCK, v.len().min(ENCODE_BLOCK)] { + let mut out = vec![0u16; n]; + lut.encode_into(&v[..n], &mut out); + assert_eq!(out, v[..n].iter().map(|&x| lut.encode(x)).collect::>(), "{n}"); + } + } + #[test] fn hundred_megapixel_bayer_fits_without_raising_default_limits() { let mut opts = DevelopOptions::default(); for method in [Demosaic::Bilinear, Demosaic::Mhc, Demosaic::Ahd] { opts.demosaic = method; - for workers in [1, 10, 32, 128] { - let jobs = working_memory(11648, 8736, true, true, &opts, workers).unwrap(); - assert!(jobs <= workers); - if method == Demosaic::Ahd && workers >= 32 { - assert!(jobs < workers); + for (fused, orient_copy) in [(false, false), (true, false), (true, true)] { + let shape = Shape { fused, orient_copy }; + for workers in [1, 10, 32, 128] { + let jobs = working_memory(11648, 8736, true, true, shape, &opts, workers).unwrap(); + assert!(jobs <= workers); + if method == Demosaic::Ahd && workers >= 32 && !fused { + assert!(jobs < workers); + } } } } opts.limits.max_alloc = 1 << 30; - assert!(working_memory(11648, 8736, true, true, &opts, 1).is_err()); + assert!(working_memory(11648, 8736, true, true, FULL, &opts, 1).is_err()); + // Fused: the u16 output and the CFA plane (+ one band) is about 10 bytes a pixel. + opts.demosaic = Demosaic::Bilinear; + assert!(working_memory(11648, 8736, true, true, FUSED, &opts, 1).is_ok()); + } + + /// The fused bilinear / MHC, X-Trans and LinearRaw paths run fewer bands at once when the bands + /// of every worker don't fit next to the output, as AHD does. A very wide, short Bayer frame + /// (262144 x 455: 15 bands of 32 rows, about 147 MB a band with its window) needs 2.92 GB + /// with all 15 at once against the default 2 GiB, and develops with 9. + #[test] + fn fused_bands_at_once_are_capped_by_the_memory_limit() { + let opts = DevelopOptions { demosaic: Demosaic::Bilinear, ..Default::default() }; + let table = (GAMMA_STEPS as u64 + 2) * 4; + let (w, h) = (262144usize, 455usize); + let band = 32 * 262144 * 12 + (32 + 2 * PAD as u64) * (262144 + 2 * PAD as u64) * 4; + for (workers, want) in [(1, 1), (8, 8), (9, 9), (10, 9), (16, 9), (32, 9)] { + assert_eq!(working_memory(w, h, true, true, FUSED, &opts, workers).unwrap(), want, "{workers} workers"); + } + // Never more jobs than bands, and one when only one band fits. + assert_eq!(working_memory(1000, 40, true, true, FUSED, &opts, 32).unwrap(), 2); + let mut tight = opts.clone(); + tight.limits.max_alloc = 262144 * 455 * 6 + table + band; + assert_eq!(working_memory(w, h, true, true, FUSED, &tight, 32).unwrap(), 1); + tight.limits.max_alloc -= 1; + assert!(working_memory(w, h, true, true, FUSED, &tight, 32).is_err()); + // X-Trans (bands of `band_rows(1000)` = 65 rows next to its planes) and LinearRaw likewise. + let plane = (1000 + 2 * PAD as u64) * (2000 + 2 * PAD as u64) * 4; + let mut x = opts.clone(); + x.limits.max_alloc = 1000 * 2000 * 10 + plane + table + 3 * 65 * 1000 * 12; + assert_eq!(working_memory(1000, 2000, true, false, FUSED, &x, 32).unwrap(), 3); + x.limits.max_alloc = 1000 * 2000 * 6 + 5 * 65 * 1000 * 12; + assert_eq!(working_memory(1000, 2000, false, true, FUSED, &x, 32).unwrap(), 5); } #[test] fn working_memory_accounts_for_padding_and_at_least_one_ahd_band() { let mut opts = DevelopOptions { demosaic: Demosaic::Ahd, ..Default::default() }; opts.limits.max_alloc = 100 * 100 * 22; - assert!(working_memory(100, 100, true, true, &opts, 1).is_err()); + assert!(working_memory(100, 100, true, true, FULL, &opts, 1).is_err()); opts.demosaic = Demosaic::Mhc; - assert!(working_memory(100, 100, true, true, &opts, 1).is_ok()); + assert!(working_memory(100, 100, true, true, FULL, &opts, 1).is_ok()); opts.limits.max_alloc = 100 * 100 * 18; - assert!(working_memory(100, 100, false, true, &opts, 1).is_err()); + assert!(working_memory(100, 100, false, true, FULL, &opts, 1).is_err()); + } + + #[test] + fn fused_working_memory_charges_the_output_the_window_and_the_bands() { + let mut opts = DevelopOptions { demosaic: Demosaic::Mhc, ..Default::default() }; + let (w, h) = (1000u64, 1000u64); + let table = (GAMMA_STEPS as u64 + 2) * 4; + let plane = (w + 2 * PAD as u64) * (h + 2 * PAD as u64) * 4; + let pad = PAD as u64; + let exact = |opts: &mut DevelopOptions, cfa: bool, bayer: bool, shape: Shape, need: u64| { + opts.limits.max_alloc = need - 1; + assert!(working_memory(1000, 1000, cfa, bayer, shape, opts, 1).is_err(), "{need} - 1"); + opts.limits.max_alloc = need; + assert!(working_memory(1000, 1000, cfa, bayer, shape, opts, 1).is_ok(), "{need}"); + }; + // Bayer: the u16 output and one band with its padded window, no whole plane. + let band = 32 * w * 12 + (32 + 2 * pad) * (w + 2 * pad) * 4; + exact(&mut opts, true, true, FUSED, w * h * 6 + table + band); + // An oriented frame needs the output twice; a monochrome sensor has no plane. + let mono_band = 65 * w * 12; + exact(&mut opts, false, true, Shape { fused: true, orient_copy: true }, w * h * 12 + table); + exact(&mut opts, false, true, FUSED, w * h * 6 + mono_band); + // X-Trans keeps its plane and its green plane. + exact(&mut opts, true, false, FUSED, w * h * 10 + plane + table + mono_band); } #[test] diff --git a/crates/raw/src/dng.rs b/crates/raw/src/dng.rs index e8422b5419..8329a80d39 100644 --- a/crates/raw/src/dng.rs +++ b/crates/raw/src/dng.rs @@ -193,18 +193,26 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { if scale.len() == 2 && (scale[0] - scale[1]).abs() > 1e-6 { warnings.push("non-square pixels (DefaultScale) are not resampled".to_string()); } - // Opcode lists: OpcodeList2 gain maps (lens shading) are applied; everything else is reported. + // Opcode lists: OpcodeList2 gain maps (lens shading) and polynomials (the tone mapping lossy + // DNGs carry per plane, #8125) are applied; everything else is reported. let mut gain_maps = Vec::new(); + let mut polynomials = Vec::new(); for (tg, what) in [(tag::OPCODE_LIST_1, 1), (tag::OPCODE_LIST_2, 2), (tag::OPCODE_LIST_3, 3)] { let Some(bytes) = raw.get(tg).and_then(|e| t.raw(e)) else { continue }; - let (maps, skipped) = opcodes::parse(bytes); + let ops = opcodes::parse(bytes); if what == 2 { - gain_maps = maps; - } else if !maps.is_empty() { - warnings.push(format!("DNG OpcodeList{what}: GainMap is not applied")); + gain_maps = ops.gain_maps; + polynomials = ops.polynomials; + } else { + if !ops.gain_maps.is_empty() { + warnings.push(format!("DNG OpcodeList{what}: GainMap is not applied")); + } + if !ops.polynomials.is_empty() { + warnings.push(format!("DNG OpcodeList{what}: MapPolynomial is not applied")); + } } - if !skipped.is_empty() { - warnings.push(format!("DNG OpcodeList{what}: {} not applied", skipped.join(", "))); + if !ops.skipped.is_empty() { + warnings.push(format!("DNG OpcodeList{what}: {} not applied", ops.skipped.join(", "))); } } @@ -215,14 +223,18 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { } let max = ((1u32 << plane.bits) - 1) as f32; - let linearization = raw.get(tag::LINEARIZATION_TABLE).map(|e| t.uints(e)).filter(|v| !v.is_empty() && v.len() <= 65536); - let linearization: Option> = linearization.map(|v| v.into_iter().map(|x| x.min(65535) as u16).collect()); + // A LinearizationTable maps integer samples; floating-point data is linear already. + let linearization = + raw.get(tag::LINEARIZATION_TABLE).filter(|_| plane.float_scale.is_none()).map(|e| t.uints(e)).filter(|v| !v.is_empty() && v.len() <= 65536); + let mut linearization: Option> = linearization.map(|v| v.into_iter().map(|x| x.min(65535) as u16).collect()); let white_default = match &linearization { Some(l) => f32::from(l.iter().copied().max().unwrap_or(u16::MAX)), None => max, }; + // Floating-point levels are in the floats' units: scale them as the samples were. + let unit = plane.float_scale.unwrap_or(1.0); let wl = t.tag_floats(&raw, tag::WHITE_LEVEL); - let white = [0, 1, 2].map(|s| wl.get(s).or_else(|| wl.first()).map(|v| *v as f32).filter(|v| *v > 0.0).unwrap_or(white_default)); + let white = [0, 1, 2].map(|s| wl.get(s).or_else(|| wl.first()).map(|v| *v as f32 * unit).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(white_default)); let (rows, cols) = match t.tag_uints(&raw, tag::BLACK_LEVEL_REPEAT_DIM).as_slice() { [r, c] if (1..=16).contains(r) && (1..=16).contains(c) => (*r as usize, *c as usize), @@ -230,17 +242,29 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { }; let bl = t.tag_floats(&raw, tag::BLACK_LEVEL); let mut black = if bl.len() == rows * cols * samples { - BlackLevels { rows, cols, values: bl.iter().map(|v| *v as f32).collect(), delta_h: Vec::new(), delta_v: Vec::new() } + BlackLevels { rows, cols, values: bl.iter().map(|v| *v as f32 * unit).collect(), delta_h: Vec::new(), delta_v: Vec::new() } } else { - BlackLevels::uniform(bl.first().copied().unwrap_or(0.0) as f32) + BlackLevels::uniform(bl.first().copied().unwrap_or(0.0) as f32 * unit) }; let dh = t.tag_floats(&raw, tag::BLACK_LEVEL_DELTA_H); if dh.len() == active.width { - black.delta_h = dh.iter().map(|v| *v as f32).collect(); + black.delta_h = dh.iter().map(|v| *v as f32 * unit).collect(); } let dv = t.tag_floats(&raw, tag::BLACK_LEVEL_DELTA_V); if dv.len() == active.height { - black.delta_v = dv.iter().map(|v| *v as f32).collect(); + black.delta_v = dv.iter().map(|v| *v as f32 * unit).collect(); + } + + // MapPolynomial maps linear reference values: linearize the samples first (the table is then + // done with), and map them in place. + let mut data = plane.data; + if !polynomials.is_empty() { + if let Some(l) = linearization.take() { + for v in &mut data { + *v = l.get(usize::from(*v)).or_else(|| l.last()).copied().unwrap_or(*v); + } + } + opcodes::apply_polynomials(&mut data, plane.width, samples, active, &polynomials, &black, white); } let color = color_info(t, &ifd0); @@ -258,7 +282,7 @@ pub(crate) fn decode(t: &Tiff, limits: &Limits) -> Result { width: plane.width, height: plane.height, samples, - data: plane.data, + data, cfa, linearization, black, diff --git a/crates/raw/src/jxl.rs b/crates/raw/src/jxl.rs index 0717dbb3cd..8dc2ff064f 100644 --- a/crates/raw/src/jxl.rs +++ b/crates/raw/src/jxl.rs @@ -18,15 +18,18 @@ use crate::error::{RawError, Result}; /// The browser build has no JPEG XL decoder (see the module docs). #[cfg(target_arch = "wasm32")] -pub(crate) fn decode(_src: &[u8], _w: usize, _h: usize, _samples: usize, _bits: u32, _limits: &Limits) -> Result> { +pub(crate) fn decode(_src: &[u8], _w: usize, _h: usize, _samples: usize, _bits: u32, _float_scale: Option, _limits: &Limits) -> Result> { Err(RawError::unsupported("JPEG XL-compressed DNG (DNG 1.7) can't be opened in the browser version of PhotoCraft; open it in the desktop app")) } /// Decodes one segment into `w * h * samples` interleaved samples at `bits` per sample (the -/// TIFF BitsPerSample, which DNG requires the codestream's bit depth to match). +/// TIFF BitsPerSample, which DNG requires the codestream's bit depth to match). Floating-point +/// samples (SampleFormat 3) come back multiplied by `float_scale`, clipped to 0..=65535. #[cfg(not(target_arch = "wasm32"))] -pub(crate) fn decode(src: &[u8], w: usize, h: usize, samples: usize, bits: u32, limits: &Limits) -> Result> { - limits.check(w as u64, h as u64, samples as u64 * 2)?; +pub(crate) fn decode(src: &[u8], w: usize, h: usize, samples: usize, bits: u32, float_scale: Option, limits: &Limits) -> Result> { + // Floating-point samples are read into an f32 buffer first: four bytes a sample, not two. + let sample_bytes = if float_scale.is_some() { 4 } else { 2 }; + limits.check(w as u64, h as u64, samples as u64 * sample_bytes)?; // jxl-oxide decodes into f32/i32 planes: allow a few times the output size, and never more // than the caller's per-buffer limit. let budget = (w as u64).saturating_mul(h as u64).saturating_mul(samples as u64).saturating_mul(16).min(limits.max_alloc); @@ -50,6 +53,13 @@ pub(crate) fn decode(src: &[u8], w: usize, h: usize, samples: usize, bits: u32, return Err(RawError::unsupported("rotated JPEG XL raw tile")); } let n = w.checked_mul(h).and_then(|p| p.checked_mul(samples)).ok_or_else(|| RawError::malformed("JPEG XL raw tile is too large"))?; + if let Some(scale) = float_scale { + let mut f = vec![0f32; n]; + if stream.write_to_buffer(&mut f) != n { + return Err(RawError::malformed("JPEG XL raw data is truncated")); + } + return Ok(f.into_iter().map(|v| if v.is_nan() { 0 } else { (v * scale).clamp(0.0, 65535.0).round() as u16 }).collect()); + } let mut out = vec![0u16; n]; if stream.write_to_buffer(&mut out) != n { return Err(RawError::malformed("JPEG XL raw data is truncated")); diff --git a/crates/raw/src/lib.rs b/crates/raw/src/lib.rs index 0d1a99e796..43a3ff47c8 100644 --- a/crates/raw/src/lib.rs +++ b/crates/raw/src/lib.rs @@ -64,6 +64,7 @@ mod rw2; mod saturation; mod sensor; mod sony; +mod sony_metadata; mod tiff; mod tiffep; mod tone; @@ -76,8 +77,8 @@ pub use crate::demosaic::Demosaic; pub use crate::develop::{DevelopOptions, Developed, RawInfo, WhiteBalance, develop_sensor}; pub use crate::error::RawError; pub use crate::look::{CameraLook, LookRejected, PROXY_EDGE, Reference, ToneCurve, fit_look}; -pub use crate::opcodes::GainMap; -pub use crate::preview::{Preview, PreviewColorSpace, embedded_preview, embedded_preview_color_space, embedded_previews}; +pub use crate::opcodes::{GainMap, MapPolynomial}; +pub use crate::preview::{DecodedPreview, Preview, PreviewColorSpace, embedded_jxl_preview, embedded_preview, embedded_preview_color_space, embedded_previews}; pub use crate::sensor::{BlackLevels, Cfa, Rect, Sensor}; pub use crate::tone::{Tone, retone}; diff --git a/crates/raw/src/ljpeg.rs b/crates/raw/src/ljpeg.rs index e9c42bdeb4..070b3757fd 100644 --- a/crates/raw/src/ljpeg.rs +++ b/crates/raw/src/ljpeg.rs @@ -249,7 +249,7 @@ impl<'a> Bits<'a> { } /// A difference value: Huffman-coded category SSSS then SSSS extra bits (T.81 H.1.2.2). - #[inline] + #[inline(always)] fn diff(&mut self, h: &Huffman) -> Result { if self.n < 32 { self.refill(); @@ -284,8 +284,11 @@ pub(crate) fn frame(data: &[u8]) -> Result = Option<(S, S)>; /// Right-click while transforming, in Photoshop's order: what the box's handles do, then Rotate /// and Flip, which turn the open box without committing it (`transform_tool::intercept`). @@ -137,6 +145,33 @@ pub const TYPE_EDIT_MENU: &[Row] = &[ Some(("Warp Text…", "type.warpText")), ]; +/// Right-click with a Type tool over type that isn't being edited (#7324): the type edit menu's +/// layer rows. Its edit rows would act on the document instead of the text, and Faux Bold/Italic +/// on characters being edited, so they are left out. +pub const TYPE_LAYER_MENU: &[Row] = &[ + Some(("Check Spelling…", "edit.checkSpelling")), + Some(("Find and Replace Text…", "edit.findAndReplaceText")), + None, + Some(("Rasterize Type", "type.rasterizeTypeLayer")), + Some(("Create Work Path", "type.createWorkPath")), + Some(("Convert to Shape", "type.convertToShape")), + None, + Some(("Horizontal", "type.orientation.horizontal")), + Some(("Vertical", "type.orientation.vertical")), + None, + Some(("None", "type.antiAlias.none")), + Some(("Sharp", "type.antiAlias.sharp")), + Some(("Crisp", "type.antiAlias.crisp")), + Some(("Strong", "type.antiAlias.strong")), + Some(("Smooth", "type.antiAlias.smooth")), + Some(("Windows LCD", "type.antiAlias.windowsLcd")), + Some(("Windows", "type.antiAlias.windows")), + None, + Some(("Convert to Paragraph Text", "type.convertToParagraphText")), + Some(("Convert to Point Text", "type.convertToPointText")), + Some(("Warp Text…", "type.warpText")), +]; + /// The type edit menu's Faux Bold and Faux Italic rows. pub const FAUX_BOLD: &str = "type.fauxBold"; pub const FAUX_ITALIC: &str = "type.fauxItalic"; @@ -165,6 +200,146 @@ pub const GUIDE_MENU: &[Row] = &[ Some(("Clear Selected Artboard Guides", "view.clearSelectedArtboardGuides")), ]; +/// The crop menu's row ids. A ratio row is `crop.ratio.`, the options bar's preset dropdown +/// key (`crop_size::dropdown_options`); a saved preset is `crop.preset.`, the n-th name the menu +/// opened with; an overlay row is `crop.overlay.` or `crop.overlayShow.`. +const CROP_RATIO: &str = "crop.ratio."; +const CROP_PRESET: &str = "crop.preset."; +const CROP_OVERLAY: &str = "crop.overlay."; +const CROP_SHOW: &str = "crop.overlayShow."; +pub const CROP_CLEAR: &str = "crop.clear"; +pub const CROP_NEW: &str = "crop.newPreset"; +pub const CROP_DELETE: &str = "crop.deletePreset"; +pub const CROP_CANCEL: &str = "crop.cancel"; +pub const CROP_COMMIT: &str = "crop.commit"; + +/// The crop menu (right-click on a pending crop, #6561): the options bar's preset dropdown, in its +/// order, and its Clear; the overlay and when it shows; then Cancel and Crop, the bar's ⊘ and ✓. +/// Built from the bar's own lists, so the two cannot disagree. Empty for any other menu. +pub fn crop_rows(menu: &CanvasToolMenu) -> Vec> { + use crate::crop_overlay::{CropOverlay, OverlayShow}; + let Some(users) = menu.crop.as_ref() else { return Vec::new() }; + let ratio = |(k, l): &(String, &str)| Some((l.to_string(), format!("{CROP_RATIO}{k}"))); + let (before, after): (Vec<_>, Vec<_>) = crate::crop_size::dropdown_options().into_iter().partition(|(k, _)| k != crate::crop_size::FRONT); + let mut rows: Vec> = before.iter().map(ratio).collect(); + rows.extend(users.iter().enumerate().map(|(i, name)| Some((name.clone(), format!("{CROP_PRESET}{i}"))))); + rows.extend(after.iter().map(ratio)); + rows.push(Some(("Clear".into(), CROP_CLEAR.into()))); + rows.push(None); + rows.push(Some(("New Crop Preset…".into(), CROP_NEW.into()))); + rows.push(Some(("Delete Crop Preset…".into(), CROP_DELETE.into()))); + rows.push(None); + rows.extend(CropOverlay::ALL.iter().map(|k| Some((k.label().to_string(), format!("{CROP_OVERLAY}{}", k.id()))))); + rows.push(None); + rows.extend(OverlayShow::ALL.iter().map(|k| Some((k.label().to_string(), format!("{CROP_SHOW}{}", k.id()))))); + rows.push(None); + rows.push(Some(("Cancel".into(), CROP_CANCEL.into()))); + rows.push(Some(("Crop".into(), CROP_COMMIT.into()))); + rows +} + +/// Is a crop row's label UI text (translated), rather than a saved preset's name? +pub fn crop_label_is_ui(id: &str) -> bool { + !id.starts_with(CROP_PRESET) +} + +/// Is `command` one of the crop menu's rows? +fn is_crop(menu: &CanvasToolMenu, command: &str) -> bool { + crop_rows(menu).iter().flatten().any(|(_, id)| id == command) +} + +/// The saved crop preset a `crop.preset.` row picks. +fn crop_preset<'a>(menu: &'a CanvasToolMenu, command: &str) -> Option<&'a String> { + let n = command.strip_prefix(CROP_PRESET)?.parse::().ok()?; + menu.crop.as_ref()?.get(n) +} + +/// The crop menu's checkmarks, as the options bar shows them: the dropdown's entry (a saved +/// preset the fields hold, else the mode or ratio, so a size preset checks W x H x Resolution), +/// the overlay and when it shows. +fn crop_checked(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool { + let o = &app.ui.tool_options; + let user = crate::crop_presets::current_user(app); + if let Some(k) = command.strip_prefix(CROP_RATIO) { + return user.is_none() && k == o.crop_ratio; + } + if command.starts_with(CROP_PRESET) { + return user.is_some() && crop_preset(menu, command) == user.as_ref(); + } + if let Some(k) = command.strip_prefix(CROP_OVERLAY) { + return k == o.crop_overlay.id(); + } + command.strip_prefix(CROP_SHOW).is_some_and(|k| k == o.crop_overlay_show.id()) +} + +fn crop_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool { + match command { + CROP_NEW => crate::crop_presets::savable(&app.ui.tool_options).is_some(), + CROP_DELETE => menu.crop.as_ref().is_some_and(|u| !u.is_empty()), + c => is_crop(menu, c), + } +} + +/// Right-click with `tool` on a pending crop: the crop menu at screen point `pos`. Only the Crop +/// tool itself, not one held over it (Space's Hand). +pub fn open_crop(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]) -> bool { + if tool != Tool::Crop || !crate::crop_ui::pending(app) || !pos.iter().all(|v| v.is_finite()) { + return false; + } + app.ui.brush_picker = None; + app.ui.layer_menu = None; + let users = crate::crop_presets::user_presets(app).into_iter().map(|(name, _)| name).collect(); + app.ui.canvas_tool_menu = Some(CanvasToolMenu { + pos, + tool, + has_selection: false, + has_path: false, + path_name: None, + transform: false, + pasteboard: false, + anchor: None, + text: false, + type_layer: false, + guide: false, + spelling: None, + crop: Some(users), + }); + crate::canvas_popup::mark_open(ctx, menu_id()); + true +} + +/// A crop row: each runs what its options-bar twin runs. +fn choose_crop(app: &mut PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> Result<(), String> { + use crate::crop_overlay::{CropOverlay, OverlayShow}; + use crate::crop_presets::{Choice, apply_choice}; + match command { + CROP_CANCEL => crate::crop_ui::cancel(app), + CROP_COMMIT => crate::canvas::commit_crop(app), + CROP_NEW => apply_choice(app, Choice::New), + CROP_DELETE => apply_choice(app, Choice::Delete), + _ => { + let before = crate::crop_ui::preset_ratio(app); + if let Some(k) = command.strip_prefix(CROP_RATIO) { + apply_choice(app, Choice::Builtin(k.to_string())); + } else if let Some(name) = crop_preset(menu, command) { + apply_choice(app, Choice::User(name.clone())); + } else if command == CROP_CLEAR { + crate::crop_size::clear_ratio(&mut app.ui.tool_options); + } else if let Some(k) = command.strip_prefix(CROP_OVERLAY).and_then(CropOverlay::from_id) { + crate::crop_overlay::set_overlay(&mut app.ui.tool_options, k); + } else if let Some(k) = command.strip_prefix(CROP_SHOW).and_then(OverlayShow::from_id) { + app.ui.tool_options.crop_overlay_show = k; + } + // As the options bar does: a saved preset or Clear changes the ratio without refitting + // the frame, so refit it when the ratio (or W x H) the frame follows changed. + if crate::crop_ui::preset_ratio(app) != before { + crate::crop_ui::apply_ratio(app); + } + } + } + Ok(()) +} + /// The pasteboard colours in the menu's order, which Space+F steps through. const CANVAS_COLORS: [CanvasColor; 6] = [CanvasColor::Default, CanvasColor::Black, CanvasColor::DarkGray, CanvasColor::MediumGray, CanvasColor::LightGray, CanvasColor::Custom]; @@ -179,18 +354,17 @@ pub fn cycle_canvas_color(app: &mut PhotocraftApp, backward: bool) -> Result<(), app.run("prefs.set", json!({"values": {"interface.canvasColor": name}})).map(|_| ()) } -/// The Zoom and Hand tools' context menu: the zoom steps and fixed zooms of the View menu, plus -/// the Hand tool's Fill Screen. +/// The Zoom and Hand tools' context menu: the fixed zooms of the View menu (plus the Hand tool's +/// Fill Screen), then the zoom steps last, in Photoshop's order (#6005). pub const VIEW_MENU: &[Row] = &[ - Some(("Zoom In", "view.zoomIn")), - Some(("Zoom Out", "view.zoomOut")), - None, Some(("Fit on Screen", "view.fitOnScreen")), Some(("Fill Screen", FILL_SCREEN)), - None, Some(("100%", "view.actualPixels")), Some(("200%", "view.twoHundredPercent")), Some(("Print Size", "view.printSize")), + None, + Some(("Zoom In", "view.zoomIn")), + Some(("Zoom Out", "view.zoomOut")), ]; /// The view menu's Fill Screen row. Photoshop has no View menu command for it, so the menu sets @@ -284,8 +458,17 @@ pub fn applies(tool: Tool) -> bool { | Tool::MagneticLasso | Tool::MagicWand | Tool::ObjectSelection - | Tool::Pen - ) + // Its brush is set from the options bar's chip; the canvas right-click is the + // selection menu, as in Photoshop (#7268). + | Tool::QuickSelection + ) || path_menu(tool) +} + +/// Tools whose canvas right-click opens the path menu ([`PEN_MENU`]): the Pen, and the Path +/// Selection and Direct Selection tools, whose Photoshop menu offers the same whole-path actions +/// for the selected path (#1056). +pub fn path_menu(tool: Tool) -> bool { + matches!(tool, Tool::Pen | Tool::PathSelection | Tool::DirectSelection) } /// Tools whose plain canvas right-click offers the view menu. @@ -300,7 +483,10 @@ pub fn selection_rows(has_selection: bool) -> &'static [Row] { /// The rows of an open menu. pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] { - if menu.guide { + if menu.crop.is_some() { + // Built per menu: [`crop_rows`]. + &[] + } else if menu.guide { GUIDE_MENU } else if menu.pasteboard { PASTEBOARD_MENU @@ -310,7 +496,9 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] { VIEW_MENU } else if menu.text { TYPE_EDIT_MENU - } else if menu.tool == Tool::Pen { + } else if menu.type_layer { + TYPE_LAYER_MENU + } else if path_menu(menu.tool) { PEN_MENU } else { selection_rows(menu.has_selection) @@ -319,20 +507,23 @@ pub fn rows(menu: &CanvasToolMenu) -> &'static [Row] { /// Is `command` one of the open menu's rows, and enabled? pub fn available(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool { - (rows(menu).iter().flatten().any(|(_, id)| *id == command) || is_spelling(menu, command)) && entry_enabled(app, menu, command) + (rows(menu).iter().flatten().any(|(_, id)| *id == command) || is_spelling(menu, command) || is_crop(menu, command)) && entry_enabled(app, menu, command) } /// Is `command` the transform box's current mode (checked in the transform menu)? /// Scale stays Free Transform's mode (only Free Transform is checked). Rotate is its own mode. /// Warp is not a box mode (it is its own session), and `for_command` would read it as Free. pub(crate) fn mode_checked(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool { + if menu.crop.is_some() { + return crop_checked(app, menu, command); + } if menu.guide { return command == "view.lockGuides" && app.ui.extras.lock_guides; } if menu.pasteboard { return pasteboard_choice(command) == Some(app.session.prefs().interface.canvas_color); } - if menu.text { + if menu.text || menu.type_layer { return type_checked(app, command); } menu.transform @@ -358,6 +549,32 @@ pub fn editing_text(app: &PhotocraftApp, tool: Tool) -> bool { tool.is_type() && app.ui.text_edit.is_some() } +/// The committed type layer under document point `doc` when a right-click there with a Type tool +/// opens the type layer menu (#7324). The Type Mask tools are left out: to them existing type is +/// just pixels. +pub fn type_layer_at(app: &mut PhotocraftApp, tool: Tool, doc: [f64; 2]) -> Option { + if !tool.is_type() || tool.is_type_mask() || app.ui.text_edit.is_some() || !doc.iter().all(|v| v.is_finite()) { + return None; + } + crate::type_tool::hit_layer(app, doc[0], doc[1]) +} + +/// Make the right-clicked type layer `id` the active layer, which the structural rows (Rasterize +/// Type, Convert to Shape…) act on and the checks show. Inside a multiselection the selection stays, +/// so the formatting rows still act on every selected type layer, as the Type menu's do; outside it +/// the type replaces the selection, as a click with the Type tool does. +fn target_type_layer(app: &mut PhotocraftApp, id: photocraft_doc::LayerId) -> bool { + let Some(st) = app.session.active() else { return false }; + let selected = st.selected_layers(); + if st.active_layer == Some(id) { + true + } else if selected.contains(&id) { + photocraft_engine::layer_multi_cmds::set_selection(&mut app.session, selected, Some(id), Some(id)).is_ok() + } else { + app.session.select_layer(id).is_ok() + } +} + /// The misspelled word of the edited text under document point `doc`, with its suggestions. fn spelling_at(app: &mut PhotocraftApp, doc: [f64; 2]) -> Option { let layer = app.ui.text_edit.as_ref()?.layer; @@ -418,8 +635,10 @@ pub fn open_transform(app: &mut PhotocraftApp, ctx: &Context, pos: [f32; 2]) -> pasteboard: false, anchor: None, text: false, + type_layer: false, guide: false, spelling: None, + crop: None, }); crate::canvas_popup::mark_open(ctx, menu_id()); true @@ -434,6 +653,9 @@ fn pasteboard_choice(command: &str) -> Option { /// The engine command (and params) a pasteboard row runs, for the automation policy: every row /// changes a preference. pub fn engine_call(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> (String, Value) { + if menu.crop.is_some() && command == CROP_COMMIT { + return ("image.crop".into(), json!({})); + } if menu.guide { let selected = crate::guide_select::selected(app); return match command { @@ -471,7 +693,7 @@ pub fn pasteboard_at(app: &PhotocraftApp, tool: Tool, doc: [f64; 2]) -> bool { let Some(st) = app.session.active() else { return false }; let (w, h) = (f64::from(st.doc.size.width), f64::from(st.doc.size.height)); let outside = doc.iter().all(|v| v.is_finite()) && (doc[0] < 0.0 || doc[1] < 0.0 || doc[0] >= w || doc[1] >= h); - outside && tool != Tool::Pen && !crate::paint_mouse::has_brush_picker(tool) && !crate::paint_mouse::right_erases(app, tool) && app.ui.text_edit.is_none() + outside && !path_menu(tool) && !crate::paint_mouse::has_brush_picker(tool) && !crate::paint_mouse::right_erases(app, tool) && app.ui.text_edit.is_none() } /// Right-click on the pasteboard: its colour menu at screen point `pos`. @@ -491,15 +713,18 @@ pub fn open_pasteboard(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: pasteboard: true, anchor: None, text: false, + type_layer: false, guide: false, spelling: None, + crop: None, }); crate::canvas_popup::mark_open(ctx, menu_id()); true } /// Right-click at document point `doc` on a guide that `tool` could drag (the Move tool, or ⌘ with -/// a selection tool): select the guide and open the guide menu at screen point `pos`. +/// a tool on which ⌘ moves guides, `canvas::command_guide_at`): select the guide and open the guide +/// menu at screen point `pos`. pub fn open_guide(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, mods: egui::Modifiers, pos: [f32; 2], doc: [f64; 2]) -> bool { if !pos.iter().all(|v| v.is_finite()) || !doc.iter().all(|v| v.is_finite()) { return false; @@ -519,8 +744,10 @@ pub fn open_guide(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, mods: egui pasteboard: false, anchor: None, text: false, + type_layer: false, guide: true, spelling: None, + crop: None, }); crate::canvas_popup::mark_open(ctx, menu_id()); true @@ -548,6 +775,9 @@ fn choose_pasteboard(app: &mut PhotocraftApp, menu: &CanvasToolMenu, command: &s } pub fn entry_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool { + if menu.crop.is_some() { + return crop_enabled(app, menu, command); + } if menu.guide { return match command { GUIDE_EDIT | GUIDE_CLEAR => crate::guide_select::selected(app).is_some(), @@ -572,7 +802,10 @@ pub fn entry_enabled(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) c => TYPE_EDIT_MENU.iter().flatten().any(|(_, id)| *id == c) && crate::menus::is_enabled(app, c), }; } - if menu.transform || menu.tool != Tool::Pen { + if menu.type_layer { + return TYPE_LAYER_MENU.iter().flatten().any(|(_, id)| *id == command) && crate::menus::is_enabled(app, command); + } + if menu.transform || !path_menu(menu.tool) { return crate::menus::is_enabled(app, command); } let Some(st) = app.session.active() else { return false }; @@ -615,10 +848,15 @@ pub fn open(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2]) - } /// [`open`] for a right-click at document point `doc`: over a misspelled word of the text being -/// edited, the menu starts with its spelling suggestions. +/// edited, the menu starts with its spelling suggestions; over committed type with a Type tool, the +/// type layer menu. pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2], doc: Option<[f64; 2]>) -> bool { let text = editing_text(app, tool); - if !(applies(tool) || text) || !pos.iter().all(|v| v.is_finite()) { + if !pos.iter().all(|v| v.is_finite()) { + return false; + } + let type_layer = !text && doc.and_then(|d| type_layer_at(app, tool, d)).is_some_and(|id| target_type_layer(app, id)); + if !(applies(tool) || text || type_layer) { return false; } app.ui.brush_picker = None; @@ -627,8 +865,21 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2] let has_path = crate::vector_ui::active_path_name(app).is_some() || app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2); let path_name = crate::vector_ui::active_path_name(app); let spelling = if text { doc.and_then(|d| spelling_at(app, d)) } else { None }; - app.ui.canvas_tool_menu = - Some(CanvasToolMenu { pos, tool, has_selection, has_path, path_name, transform: false, pasteboard: false, anchor: None, text, guide: false, spelling }); + app.ui.canvas_tool_menu = Some(CanvasToolMenu { + pos, + tool, + has_selection, + has_path, + path_name, + transform: false, + pasteboard: false, + anchor: None, + text, + type_layer, + guide: false, + spelling, + crop: None, + }); crate::canvas_popup::mark_open(ctx, menu_id()); true } @@ -651,8 +902,10 @@ pub fn open_view(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; pasteboard: false, anchor: Some(doc), text: false, + type_layer: false, guide: false, spelling: None, + crop: None, }); crate::canvas_popup::mark_open(ctx, menu_id()); true @@ -666,6 +919,8 @@ fn choose_view(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, co let v = app.ui.views.get_mut(i).ok_or("no document view")?; v.fit_pending = false; v.fill_pending = true; + v.request_zoom_animation(app.session.prefs().general.animated_zoom); + v.zoom_anchor = None; return Ok(()); } let before = app.ui.views.get(i).map(|v| (v.zoom, v.center)); @@ -681,6 +936,7 @@ fn choose_view(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, co // from the centre shrinks (or grows) by the zoom ratio, whatever the rotation or flip. let ratio = zoom0 / v.zoom; v.center = if center_on_point || !ratio.is_finite() { d } else { [d[0] - (d[0] - c[0]) * ratio, d[1] - (d[1] - c[1]) * ratio] }; + v.zoom_anchor = (v.zoom_animating && !center_on_point).then_some(([d[0] as f64, d[1] as f64], menu.pos)); Ok(()) } @@ -689,7 +945,9 @@ pub fn choose(app: &mut PhotocraftApp, ctx: &Context, command: &str) { if !available(app, &menu, command) { return; } - let result = if menu.guide { + let result = if menu.crop.is_some() { + choose_crop(app, &menu, command) + } else if menu.guide { choose_guide(app, ctx, command) } else if menu.pasteboard { choose_pasteboard(app, &menu, command) @@ -697,9 +955,9 @@ pub fn choose(app: &mut PhotocraftApp, ctx: &Context, command: &str) { choose_view(app, ctx, &menu, command) } else if menu.text && is_spelling(&menu, command) { choose_spelling(app, &menu, command) - } else if menu.text { + } else if menu.text || menu.type_layer { choose_type(app, ctx, command) - } else if menu.tool == Tool::Pen && !menu.transform { + } else if path_menu(menu.tool) && !menu.transform { choose_pen(app, ctx, &menu, command) } else if command == "select.toWorkPath" { // Photoshop's Make Work Path… asks for the tolerance first. @@ -783,6 +1041,10 @@ fn pen_dialog(app: &mut PhotocraftApp, command: &str, label: &str, name: &str) { } else { fields.insert("name".into(), json!(name)); } + if command == "path.toSelection" { + // OK deselects and hides the path, as ⌘↩ does (#6079). + fields.insert("__deselectPath".into(), json!(true)); + } if command == "path.fill" { let fg = app.session.tools.foreground; let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; @@ -796,6 +1058,23 @@ fn pen_dialog(app: &mut PhotocraftApp, command: &str, label: &str, name: &str) { spec_dialog(app, command, label, spec, fields); } +/// The Pen options bar's Make Selection (Path mode, #5703): the canvas menu's Make Selection… +/// dialog for the path being drawn (committed first, as the menu does) or the active path. +pub(crate) fn make_selection(app: &mut PhotocraftApp) -> Result<(), String> { + let pending = app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2); + // Where `pen_commit` puts the path being drawn: a shape layer or the targeted vector mask, + // else the Paths panel path it adds to (a selected saved path, or the Work Path), which it + // selects so it is the active path. + let pending_layer = pending && (app.ui.tool_options.vector_mode == "shape" || crate::vector_ui::targeted_vector_mask(app).is_some()); + if pending && !crate::vector_ui::pen_commit(app, false) { + return Err(app.ui.status.clone()); + } + let name = if pending_layer { Some("layer".to_string()) } else { crate::vector_ui::active_path_name(app) } + .ok_or_else(|| "No path is available for this action".to_string())?; + pen_dialog(app, "path.toSelection", "Make Selection…", &name); + Ok(()) +} + fn choose_pen(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, command: &str) -> Result<(), String> { if command == "select.isolateLayers" { crate::menus::invoke(app, ctx, command, json!({}))?; @@ -807,8 +1086,8 @@ fn choose_pen(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, com } let pending = app.ui.pen.as_ref().is_some_and(|p| p.knots.len() >= 2); let pending_layer = pending && (app.ui.tool_options.vector_mode == "shape" || app.ui.vector_mask_target); - if pending { - crate::vector_ui::pen_commit(app, false); + if pending && !crate::vector_ui::pen_commit(app, false) { + return Err(app.ui.status.clone()); } let name = if pending { Some(if pending_layer { "layer" } else { "work" }.to_string()) } else { menu.path_name.clone() } .ok_or_else(|| "No path is available for this action".to_string())?; @@ -825,7 +1104,7 @@ fn choose_pen(app: &mut PhotocraftApp, ctx: &Context, menu: &CanvasToolMenu, com } fn menu_id() -> egui::Id { - egui::Id::new("canvas-selection-menu") + egui::Id::unique("canvas-selection-menu") } pub fn show(app: &mut PhotocraftApp, ctx: &Context) { @@ -834,6 +1113,8 @@ pub fn show(app: &mut PhotocraftApp, ctx: &Context) { || app.ui.tool != menu.tool || (menu.transform && app.ui.transform.is_none()) || (menu.text && app.ui.text_edit.is_none()) + || (menu.type_layer && app.ui.text_edit.is_some()) + || (menu.crop.is_some() && !crate::crop_ui::pending(app)) { app.ui.canvas_tool_menu = None; return; @@ -868,6 +1149,17 @@ pub fn show(app: &mut PhotocraftApp, ctx: &Context) { if !spelling.is_empty() { ui.separator(); } + for row in crop_rows(&menu) { + let Some((label, command)) = row else { + ui.separator(); + continue; + }; + let text = if crop_label_is_ui(&command) { tl!(&label).to_string() } else { label }; + let item = egui::Button::selectable(mode_checked(app, &menu, &command), text).min_size(egui::vec2(200.0, 20.0)); + if ui.add_enabled(entry_enabled(app, &menu, &command), item).clicked() { + selected = Some(command); + } + } for row in rows(&menu) { let Some((label, command)) = *row else { ui.separator(); @@ -889,743 +1181,4 @@ pub fn show(app: &mut PhotocraftApp, ctx: &Context) { } #[cfg(test)] -mod tests { - use super::*; - use egui::{Modifiers, PointerButton, Pos2, vec2}; - use egui_kittest::{Harness, kittest::Queryable}; - - fn app() -> PhotocraftApp { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 32, "height": 32})).unwrap(); - app - } - - fn work_path(app: &mut PhotocraftApp) { - app.run( - "path.set", - json!({"name": "work", "path": {"subpaths": [{"closed": true, "knots": [ - {"anchor": [2, 2], "in": [2, 2], "out": [2, 2]}, - {"anchor": [20, 2], "in": [20, 2], "out": [20, 2]}, - {"anchor": [20, 20], "in": [20, 20], "out": [20, 20]} - ]}]}}), - ) - .unwrap(); - } - - fn choose_open(app: &mut PhotocraftApp, command: &str) { - assert!(open(app, &Context::default(), Tool::Pen, [10.0, 10.0])); - choose(app, &Context::default(), command); - } - - fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> { - app.sync_views(); - let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( - |ui, app: &mut PhotocraftApp| { - let ctx = ui.ctx().clone(); - if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { - return; - } - egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); - }, - app, - ); - PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); - h.run_steps(4); - h - } - - fn right_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, modifiers: Modifiers) { - h.event(egui::Event::ModifiersChanged(modifiers)); - h.event(egui::Event::PointerMoved(p)); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers }); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers }); - h.event(egui::Event::ModifiersChanged(Modifiers::NONE)); - h.run_steps(2); - } - - #[test] - fn canvas_gesture_preserves_selection_and_command_override() { - let mut app = app(); - app.ui.tool = Tool::Lasso; - app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); - let mut h = harness(app); - let center = h.state().last_canvas_rect.center(); - let revision = h.state().session.active().unwrap().revision; - right_click(&mut h, center, Modifiers::NONE); - assert!(h.state().ui.canvas_tool_menu.is_some()); - assert_eq!(h.state().session.active().unwrap().revision, revision, "right-click cannot alter selection"); - h.key_press(egui::Key::Escape); - h.run_steps(2); - assert!(h.state().ui.canvas_tool_menu.is_none()); - right_click(&mut h, center, Modifiers::COMMAND); - assert!(h.state().ui.canvas_tool_menu.is_none(), "command right-click belongs to layer picker"); - } - - /// A type layer reading "Hello", being edited with every character selected. - fn editing_type(app: &mut PhotocraftApp) -> u64 { - app.ui.tool = Tool::Type; - let id = app.run("type.create", json!({"text": "Hello", "size": 12, "x": 4, "y": 20})).unwrap()["layer"].as_u64().unwrap(); - crate::menus::invoke(app, &Context::default(), "type.editText", json!({})).unwrap(); - id - } - - fn text_of(app: &PhotocraftApp, id: u64) -> Option { - match &app.session.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().content { - LayerContent::Text(t) => Some(t.clone()), - _ => None, - } - } - - #[test] - fn right_click_while_typing_opens_the_type_edit_menu_and_esc_keeps_the_edit() { - let mut app = app(); - editing_type(&mut app); - app.sync_views(); - let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( - |ui, app: &mut PhotocraftApp| { - let ctx = ui.ctx().clone(); - if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { - return; - } - // The app's order: shortcuts (inline type's keys among them), then the canvas. - crate::shortcuts::handle(app, &ctx); - egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); - }, - app, - ); - PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); - h.run_steps(4); - let center = h.state().last_canvas_rect.center(); - right_click(&mut h, center, Modifiers::NONE); - let menu = h.state().ui.canvas_tool_menu.clone().expect("right-click in a type edit opens a menu"); - assert!(menu.text && !menu.pasteboard && !menu.transform); - assert_eq!(rows(&menu), TYPE_EDIT_MENU); - let edit = h.state().ui.text_edit.clone().expect("the edit goes on under the menu"); - assert_eq!((edit.anchor, edit.caret), (0, 5), "the right-click keeps the selection for Cut and Copy"); - h.key_press(egui::Key::Escape); - h.run_steps(2); - assert!(h.state().ui.canvas_tool_menu.is_none(), "Esc closes the menu"); - assert!(h.state().ui.text_edit.is_some(), "and only the menu: the edit isn't committed"); - } - - #[test] - fn type_edit_menu_rows_act_on_the_text_and_the_layer() { - let mut app = app(); - let id = editing_type(&mut app); - let ctx = Context::default(); - let reopen = |app: &mut PhotocraftApp| assert!(open(app, &Context::default(), Tool::Type, [10.0, 10.0])); - // Faux Bold toggles the style of the selected characters, and shows checked. - reopen(&mut app); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - assert!(!mode_checked(&app, &menu, FAUX_BOLD) && mode_checked(&app, &menu, "type.antiAlias.smooth")); - choose(&mut app, &ctx, FAUX_BOLD); - assert!(text_of(&app, id).unwrap().char_runs().iter().all(|r| r.style.faux_bold)); - assert!(mode_checked(&app, &menu, FAUX_BOLD)); - // Cut takes the selection out of the text; with nothing selected Copy and Cut grey out. - reopen(&mut app); - choose(&mut app, &ctx, "edit.cut"); - assert_eq!(text_of(&app, id).unwrap().text, ""); - reopen(&mut app); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - assert!(!entry_enabled(&app, &menu, "edit.copy") && !entry_enabled(&app, &menu, "edit.cut")); - app.ui.canvas_tool_menu = None; - crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap(); - assert_eq!(text_of(&app, id).unwrap().text, "Hello"); - // Select All selects every character, not the canvas. - reopen(&mut app); - choose(&mut app, &ctx, "select.all"); - let edit = app.ui.text_edit.clone().unwrap(); - assert_eq!((edit.anchor, edit.caret), (0, 5)); - assert!(app.session.active().unwrap().doc.selection.is_none()); - // Warp Text… opens its dialog; Rasterize Type ends the edit and rasterizes the layer. - reopen(&mut app); - choose(&mut app, &ctx, "type.warpText"); - assert!(!app.ui.dialogs.is_empty(), "Warp Text… asks for its settings"); - app.ui.dialogs.clear(); - reopen(&mut app); - choose(&mut app, &ctx, "type.rasterizeTypeLayer"); - assert!(app.ui.text_edit.is_none() && text_of(&app, id).is_none(), "the type layer is pixels now"); - } - - #[test] - fn type_edit_menu_lists_spelling_suggestions_for_the_word_under_the_pointer() { - let mut app = app(); - app.ui.tool = Tool::Type; - let id = app.run("type.create", json!({"text": "Teh cat", "size": 12, "x": 4, "y": 20})).unwrap()["layer"].as_u64().unwrap(); - let ctx = Context::default(); - crate::menus::invoke(&mut app, &ctx, "type.editText", json!({})).unwrap(); - // Away from the misspelling (past the end of the line): no spelling rows. - assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([600.0, 15.0]))); - assert!(spelling_rows(app.ui.canvas_tool_menu.as_ref().unwrap()).is_empty()); - // Over "Teh": its suggestions head the menu, then Add to Dictionary. - assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([5.0, 15.0]))); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - let rows = spelling_rows(&menu); - assert!(rows.iter().any(|(l, _)| l == "The"), "{rows:?}"); - assert_eq!(rows.last().map(|(_, id)| id.as_str()), Some(SPELLING_ADD)); - assert!(rows.len() <= MAX_SUGGESTIONS + 1, "at most {MAX_SUGGESTIONS} suggestions and Add to Dictionary: {rows:?}"); - assert!(available(&app, &menu, &rows[0].1)); - let pick = rows.iter().find(|(l, _)| l == "The").unwrap().1.clone(); - choose(&mut app, &ctx, &pick); - assert_eq!(text_of(&app, id).unwrap().text, "The cat"); - assert_eq!(app.ui.text_edit.as_ref().map(|e| (e.caret, e.anchor)), Some((3, 3)), "the caret lands after the replacement"); - // Add to Dictionary stops flagging a word. - app.run("type.insertText", json!({"layer": id, "text": " Zqx"})).unwrap(); - assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([600.0, 15.0]))); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - assert!(menu.spelling.as_ref().is_some_and(|s| s.word == "Zqx")); - choose(&mut app, &ctx, SPELLING_ADD); - assert!(app.session.prefs().user_dictionary.iter().any(|w| w == "Zqx")); - } - - #[test] - fn right_click_outside_the_image_picks_the_pasteboard_color() { - let mut app = app(); - app.ui.tool = Tool::RectMarquee; - let mut h = harness(app); - let rect = h.state().last_canvas_rect; - let outside = rect.min + vec2(5.0, 5.0); - right_click(&mut h, rect.center(), Modifiers::NONE); - assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| !m.pasteboard), "inside: the tool's own menu"); - h.state_mut().ui.canvas_tool_menu = None; - right_click(&mut h, outside, Modifiers::NONE); - let menu = h.state().ui.canvas_tool_menu.clone().unwrap(); - assert!(menu.pasteboard && rows(&menu) == PASTEBOARD_MENU, "outside: the pasteboard menu"); - let ctx = h.ctx.clone(); - choose(h.state_mut(), &ctx, "pasteboard.black"); - assert_eq!(crate::prefs_ui::pasteboard_color(h.state()), Some(egui::Color32::BLACK)); - // Select Custom Color… opens the Color Picker; OK makes its colour the custom pasteboard. - right_click(&mut h, outside, Modifiers::NONE); - choose(h.state_mut(), &ctx, PASTEBOARD_PICK); - let mut f = h.state().ui.dialogs.last().unwrap().fields.clone(); - f.insert("color".into(), json!("#123456")); - crate::color_picker_ui::confirm(h.state_mut(), &f).unwrap(); - assert_eq!(crate::prefs_ui::pasteboard_color(h.state()), Some(egui::Color32::from_rgb(0x12, 0x34, 0x56))); - // A painting tool keeps its Brush Preset picker there. - h.state_mut().ui.tool = Tool::Brush; - h.run_steps(2); - right_click(&mut h, outside, Modifiers::NONE); - assert!(h.state().ui.canvas_tool_menu.is_none() && h.state().ui.brush_picker.is_some()); - } - - /// #2832: right-click over an open Free Transform box: Photoshop's transform menu. Skew - /// switches the box's mode; Rotate and Flip turn the box about its reference point without - /// committing it; Content-Aware Scale and Puppet Warp are greyed. - #[test] - fn right_click_during_free_transform_switches_modes_rotates_and_flips_the_box() { - let mut app = app(); - app.ui.tool = Tool::RectMarquee; - app.run("layer.new.layer", json!({})).unwrap(); - app.run("select.rect", json!({"x": 4, "y": 6, "width": 10, "height": 8})).unwrap(); - app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); - app.run("select.deselect", json!({})).unwrap(); - let mut h = harness(app); - let ctx = h.ctx.clone(); - crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransform", json!({})).unwrap(); - h.run_steps(2); - let center = h.state().last_canvas_rect.center(); - let (revision, journal) = (h.state().session.active().unwrap().revision, h.state().session.journal.len()); - right_click(&mut h, center, Modifiers::NONE); - let menu = h.state().ui.canvas_tool_menu.clone().expect("the transform menu"); - assert!(menu.transform && rows(&menu) == TRANSFORM_MENU); - for (id, on) in - [("edit.contentAwareScale", false), ("edit.puppetWarp", false), ("edit.transform.rotate90Cw", true), ("edit.transform.flipVertical", true)] - { - assert_eq!(entry_enabled(h.state(), &menu, id), on, "{id}"); - } - choose(h.state_mut(), &ctx, "edit.transform.skew"); - assert_eq!(h.state().ui.transform.as_ref().unwrap().mode, crate::state::TransformMode::Skew); - let t = h.state().ui.transform.clone().unwrap(); - let (q0, c) = (t.quad, t.pivot); - right_click(&mut h, center, Modifiers::NONE); - choose(h.state_mut(), &ctx, "edit.transform.flipHorizontal"); - let q = h.state().ui.transform.as_ref().expect("the box stays open").quad; - for (a, b) in q.iter().zip(q0) { - assert!((a[0] - (2.0 * c[0] - b[0])).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9, "mirrored about the reference point: {q0:?} -> {q:?}"); - } - right_click(&mut h, center, Modifiers::NONE); - choose(h.state_mut(), &ctx, "edit.transform.rotate180"); - let q = h.state().ui.transform.as_ref().unwrap().quad; - for (a, b) in q.iter().zip(q0) { - assert!((a[1] - (2.0 * c[1] - b[1])).abs() < 1e-9 && (a[0] - b[0]).abs() < 1e-9, "flipped then turned 180°: {q0:?} -> {q:?}"); - } - let st = h.state().session.active().unwrap(); - assert_eq!((st.revision, h.state().session.journal.len()), (revision, journal), "nothing is committed"); - } - - #[test] - fn transform_menu_follows_photoshop_order() { - assert_eq!( - labels(TRANSFORM_MENU), - vec![ - Some("Free Transform"), - Some("Scale"), - Some("Rotate"), - Some("Skew"), - Some("Distort"), - Some("Perspective"), - Some("Warp"), - None, - Some("Content-Aware Scale"), - Some("Puppet Warp"), - None, - Some("Rotate 180°"), - Some("Rotate 90° Clockwise"), - Some("Rotate 90° Counter Clockwise"), - None, - Some("Flip Horizontal"), - Some("Flip Vertical"), - ] - ); - // Over a box on editable type, Distort and Perspective are greyed (#2630). - let mut app = app(); - app.run("type.create", json!({"x": 2, "y": 20, "text": "Hi", "size": 16})).unwrap(); - crate::transform_tool::begin(&mut app, &Context::default()).unwrap(); - assert!(open_transform(&mut app, &Context::default(), [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - for (id, on) in [("edit.transform.skew", true), ("edit.transform.distort", false), ("edit.transform.perspective", false)] { - assert_eq!(entry_enabled(&app, &menu, id), on, "{id}"); - } - } - - fn labels(rows: &[Row]) -> Vec> { - rows.iter().map(|row| row.map(|(label, _)| label)).collect() - } - - #[test] - fn selection_menus_follow_photoshop_order() { - assert_eq!( - labels(SELECTION_MENU), - vec![ - Some("Deselect"), - Some("Select Inverse"), - Some("Feather…"), - Some("Select and Mask…"), - None, - Some("Save Selection…"), - Some("Make Work Path…"), - None, - Some("Layer via Copy"), - Some("Layer via Cut"), - Some("New Layer…"), - None, - Some("Free Transform"), - Some("Transform Selection"), - Some("Distort"), - Some("Perspective"), - None, - Some("Delete and Fill Selection"), - Some("Fill…"), - Some("Stroke…"), - Some("Content-Aware Fill…"), - None, - Some("Last Filter"), - None, - Some("Fade…"), - ] - ); - assert_eq!(labels(NO_SELECTION_MENU), vec![Some("Select All"), Some("Reselect"), Some("Color Range…"), None, Some("Load Selection…")]); - // Every row runs a real command: one the menu bar has, or an engine command. - let app = app(); - let items = crate::menus::menu_items(&app); - for (label, id) in [SELECTION_MENU, NO_SELECTION_MENU, PEN_MENU, TRANSFORM_MENU].into_iter().flatten().flatten() { - assert!(items.iter().any(|i| i.id == *id) || photocraft_engine::commands::find(id).is_some(), "{label}: unknown command {id}"); - } - } - - #[test] - fn selection_menu_routes_actions_through_commands() { - let mut app = app(); - app.ui.tool = Tool::RectMarquee; - app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); - assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); - assert_eq!(rows(app.ui.canvas_tool_menu.as_ref().unwrap()), SELECTION_MENU); - let before = app.session.journal.len(); - choose(&mut app, &Context::default(), "select.inverse"); - assert!(app.ui.canvas_tool_menu.is_none()); - assert!(app.session.journal[before..].iter().any(|(id, _)| id == "select.inverse")); - let before = app.session.journal.len(); - assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); - choose(&mut app, &Context::default(), "select.deselect"); - assert!(app.session.active().unwrap().doc.selection.is_none()); - assert!(app.session.journal[before..].iter().any(|(id, _)| id == "select.deselect")); - // Nothing selected: the menu that makes a selection. - assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); - assert_eq!(rows(app.ui.canvas_tool_menu.as_ref().unwrap()), NO_SELECTION_MENU); - choose(&mut app, &Context::default(), "select.reselect"); - assert!(app.session.active().unwrap().doc.selection.is_some()); - assert!(app.session.journal.iter().any(|(id, _)| id == "select.reselect")); - app.run("select.deselect", json!({})).unwrap(); - assert!(open(&mut app, &Context::default(), Tool::Lasso, [10.0, 10.0])); - choose(&mut app, &Context::default(), "select.all"); - assert!(app.session.active().unwrap().doc.selection.is_some()); - } - - #[test] - fn selection_menu_layer_and_path_actions() { - let mut app = app(); - app.ui.tool = Tool::EllipseMarquee; - app.run("layer.new.layer", json!({})).unwrap(); - app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); - app.run("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap(); - let layers = app.session.active().unwrap().doc.layers.len(); - assert!(open(&mut app, &Context::default(), Tool::EllipseMarquee, [10.0, 10.0])); - choose(&mut app, &Context::default(), "layer.new.layerViaCopy"); - assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1, "Layer via Copy adds a layer"); - assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("layer.new.layerViaCopy")); - // Make Work Path… asks for the tolerance, then traces the selection. - app.run("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap(); - assert!(open(&mut app, &Context::default(), Tool::EllipseMarquee, [10.0, 10.0])); - choose(&mut app, &Context::default(), "select.toWorkPath"); - let dialog = app.ui.dialogs.last().map(|d| d.id).expect("tolerance dialog"); - assert!(app.session.active().unwrap().doc.work_path.is_none(), "nothing happens before OK"); - crate::dialogs::confirm(&mut app, dialog).unwrap(); - assert!(app.session.active().unwrap().doc.work_path.is_some()); - assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("select.toWorkPath")); - } - - #[test] - fn disabled_selection_rows_do_nothing() { - let mut app = app(); - app.ui.tool = Tool::MagicWand; - app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); - assert!(open(&mut app, &Context::default(), Tool::MagicWand, [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - // No filter has run and nothing can be faded yet. - for id in ["filter.lastFilter", "edit.fade"] { - assert!(!entry_enabled(&app, &menu, id), "{id} is greyed out"); - } - let before = app.session.journal.len(); - choose(&mut app, &Context::default(), "edit.fade"); - assert_eq!(app.session.journal.len(), before); - assert!(app.ui.dialogs.is_empty()); - // Rows of the other variant are not part of this menu. - assert!(open(&mut app, &Context::default(), Tool::MagicWand, [10.0, 10.0])); - choose(&mut app, &Context::default(), "select.all"); - assert_eq!(app.session.journal.len(), before); - } - - #[test] - fn view_menu_follows_photoshop_order() { - assert_eq!( - labels(VIEW_MENU), - vec![Some("Zoom In"), Some("Zoom Out"), None, Some("Fit on Screen"), Some("Fill Screen"), None, Some("100%"), Some("200%"), Some("Print Size")] - ); - // Every row but Fill Screen (the Hand tool's options-bar button) is a View menu command. - let app = app(); - let items = crate::menus::menu_items(&app); - for (label, id) in VIEW_MENU.iter().flatten().filter(|(_, id)| *id != FILL_SCREEN) { - assert!(items.iter().any(|i| i.id == *id), "{label}: unknown command {id}"); - } - } - - fn zoom_app(w: u32, h: u32) -> PhotocraftApp { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": w, "height": h})).unwrap(); - app - } - - fn screen_of(app: &PhotocraftApp, doc: [f32; 2]) -> Pos2 { - crate::canvas::ViewXform::active(app).unwrap().to_screen(doc[0], doc[1]) - } - - #[test] - fn zoom_and_hand_right_click_opens_the_view_menu() { - for tool in [Tool::Zoom, Tool::Hand] { - let mut app = zoom_app(400, 300); - app.ui.tool = tool; - let mut h = harness(app); - let zoom = h.state().ui.views[0].zoom; - let inside = screen_of(h.state(), [300.0, 100.0]); - right_click(&mut h, inside, Modifiers::NONE); - let menu = h.state().ui.canvas_tool_menu.clone().unwrap_or_else(|| panic!("{tool:?}: right-click opens no menu")); - assert!(rows(&menu) == VIEW_MENU, "{tool:?}: the view menu"); - assert_eq!(h.state().ui.views[0].zoom, zoom, "{tool:?}: the right-click alone does not zoom"); - assert!(entry_enabled(h.state(), &menu, FILL_SCREEN) && entry_enabled(h.state(), &menu, "view.printSize")); - // Outside the image the pasteboard colour menu wins, as for the other tools. - h.state_mut().ui.canvas_tool_menu = None; - let outside = h.state().last_canvas_rect.min + vec2(5.0, 5.0); - right_click(&mut h, outside, Modifiers::NONE); - assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.pasteboard), "{tool:?}: pasteboard menu outside"); - } - } - - #[test] - fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() { - let mut app = zoom_app(400, 300); - app.ui.tool = Tool::Zoom; - let mut h = harness(app); - let zoom = h.state().ui.views[0].zoom; - let doc = [300.0, 100.0]; - let p = screen_of(h.state(), doc); - right_click(&mut h, p, Modifiers::NONE); - // Chosen in the menu itself: the canvas frame that draws the menu must keep the new view. - h.get_by_label("Zoom In").click(); - h.run_steps(3); - assert!(h.state().ui.canvas_tool_menu.is_none()); - let after = h.state().ui.views[0].zoom; - assert_eq!(after, crate::zoom_levels::step(zoom, 1, [400, 300]), "one Zoom In step"); - let s = screen_of(h.state(), doc); - assert!((s - p).length() < 1.0, "the right-clicked point stays put: {s:?} vs {p:?}"); - assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("view.zoomIn")); - // Zoom Out returns to where it was. - right_click(&mut h, p, Modifiers::NONE); - h.get_by_label("Zoom Out").click(); - h.run_steps(3); - assert_eq!(h.state().ui.views[0].zoom, zoom); - assert!((screen_of(h.state(), doc) - p).length() < 1.0); - } - - #[test] - fn view_menu_zoom_in_centres_the_clicked_point_with_the_preference() { - let mut app = zoom_app(400, 300); - app.ui.tool = Tool::Hand; - app.run("prefs.set", json!({"path": "tools.zoomClickedPointToCenter", "value": true})).unwrap(); - let mut h = harness(app); - let doc = [300.0, 100.0]; - let p = screen_of(h.state(), doc); - right_click(&mut h, p, Modifiers::NONE); - h.get_by_label("Zoom In").click(); - h.run_steps(3); - assert_eq!(h.state().ui.views[0].center, doc); - } - - #[test] - fn view_menu_fits_fills_and_sets_fixed_zooms() { - let mut app = zoom_app(400, 300); - app.ui.tool = Tool::Zoom; - let mut h = harness(app); - let p = screen_of(h.state(), [200.0, 150.0]); - let area = crate::rulers::content_rect(h.state(), h.state().last_canvas_rect).size(); - let ppp = h.state().canvas_ppp(); - let pick = |h: &mut Harness<'static, PhotocraftApp>, label: &str| { - right_click(h, p, Modifiers::NONE); - h.get_by_label(label).click(); - h.run_steps(3); - h.state().ui.views[0].zoom - }; - assert_eq!(pick(&mut h, "200%"), 2.0); - // Fill Screen: the image covers the whole canvas (one axis can overflow). - let fill = pick(&mut h, "Fill Screen") / ppp; - assert!(400.0 * fill >= area.x - 1.0 && 300.0 * fill >= area.y - 1.0, "fill {fill} for {area:?}"); - assert!(400.0 * fill <= area.x + 1.0 || 300.0 * fill <= area.y + 1.0, "fill {fill} for {area:?}"); - assert_eq!(h.state().ui.views[0].center, [200.0, 150.0]); - // Fit on Screen: back to the whole image (capped at 100%, as on open). - let fit = pick(&mut h, "Fit on Screen") / ppp; - assert!(400.0 * fit <= area.x && 300.0 * fit <= area.y && fit < fill, "fit {fit} fill {fill}"); - } - - #[test] - fn view_menu_needs_finite_points_and_a_view_tool() { - let mut app = app(); - assert!(!open_view(&mut app, &Context::default(), Tool::Brush, [1.0, 1.0], [1.0, 1.0])); - assert!(!open_view(&mut app, &Context::default(), Tool::Zoom, [f32::NAN, 1.0], [1.0, 1.0])); - assert!(!open_view(&mut app, &Context::default(), Tool::Zoom, [1.0, 1.0], [f64::INFINITY, 1.0])); - assert!(app.ui.canvas_tool_menu.is_none()); - assert!(open_view(&mut app, &Context::default(), Tool::Zoom, [1.0, 1.0], [1.0, 1.0])); - let before = app.session.journal.len(); - choose(&mut app, &Context::default(), "select.all"); - assert_eq!(app.session.journal.len(), before, "the view menu cannot run another menu's row"); - // With no document every row is greyed out and choosing one does nothing. - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - assert!(open_view(&mut app, &Context::default(), Tool::Hand, [1.0, 1.0], [1.0, 1.0])); - let menu = app.ui.canvas_tool_menu.clone().unwrap(); - for (_, id) in VIEW_MENU.iter().flatten() { - assert!(!entry_enabled(&app, &menu, id), "{id} is greyed out with no document"); - } - choose(&mut app, &Context::default(), FILL_SCREEN); - choose(&mut app, &Context::default(), "view.zoomIn"); - } - - #[test] - fn unsupported_tools_and_nonfinite_positions_do_not_open_menu() { - let mut app = app(); - assert!(!open(&mut app, &Context::default(), Tool::Brush, [1.0, 1.0])); - assert!(!open(&mut app, &Context::default(), Tool::Lasso, [f32::NAN, 1.0])); - assert!(app.ui.canvas_tool_menu.is_none()); - assert!(open(&mut app, &Context::default(), Tool::MagicWand, [1.0, 1.0])); - let before = app.session.journal.len(); - choose(&mut app, &Context::default(), "file.new"); - assert_eq!(app.session.journal.len(), before, "context menu cannot invoke an unrelated command"); - } - - #[test] - fn completed_pen_path_right_click_makes_selection() { - let mut app = app(); - app.ui.tool = Tool::Pen; - for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { - crate::vector_ui::pen_down(&mut app, x, y, false); - crate::vector_ui::pen_up(&mut app); - } - crate::vector_ui::pen_commit(&mut app, true); - assert!(app.session.active().unwrap().doc.work_path.is_some()); - let mut h = harness(app); - let center = h.state().last_canvas_rect.center(); - right_click(&mut h, center, Modifiers::NONE); - assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == Tool::Pen && m.has_path)); - let ctx = h.ctx.clone(); - choose(h.state_mut(), &ctx, "path.toSelection"); - let dialog = h.state().ui.dialogs.last().map(|d| d.id).unwrap(); - crate::dialogs::confirm(h.state_mut(), dialog).unwrap(); - assert!(h.state().session.active().unwrap().doc.selection.is_some()); - assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("path.toSelection")); - } - - #[test] - fn pen_menu_finishes_in_progress_path_before_selection() { - let mut app = app(); - app.ui.tool = Tool::Pen; - for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { - crate::vector_ui::pen_down(&mut app, x, y, false); - crate::vector_ui::pen_up(&mut app); - } - assert!(app.session.active().unwrap().doc.work_path.is_none()); - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); - assert!(entry_enabled(&app, menu, "path.toSelection")); - choose(&mut app, &Context::default(), "path.toSelection"); - let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); - crate::dialogs::confirm(&mut app, dialog).unwrap(); - assert!(app.session.active().unwrap().doc.work_path.is_some()); - assert!(app.session.active().unwrap().doc.selection.is_some()); - } - - #[test] - fn pen_menu_has_all_reference_rows_in_order_and_guards_unavailable_actions() { - let labels: Vec<_> = PEN_MENU.iter().map(|row| row.map(|(label, _)| label)).collect(); - assert_eq!( - labels, - vec![ - Some("Create Vector Mask"), - Some("Delete Path"), - None, - Some("Define Custom Shape…"), - None, - Some("Make Selection…"), - Some("New Guides From Shape"), - Some("Fill Path…"), - Some("Stroke Path…"), - None, - Some("Clipping Path…"), - None, - Some("Free Transform Path"), - None, - Some("Unite Shapes"), - Some("Subtract Front Shape"), - Some("Unite Shapes at Overlap"), - Some("Subtract Shapes at Overlap"), - None, - Some("Copy Fill"), - Some("Copy Complete Stroke"), - None, - Some("Paste Fill"), - Some("Paste Complete Stroke"), - None, - Some("Isolate Layers"), - None, - Some("Make Symmetry Path"), - Some("Disable Symmetry Path"), - ] - ); - let mut app = app(); - app.ui.tool = Tool::Pen; - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); - assert!(!entry_enabled(&app, menu, "path.toSelection")); - let before = app.session.journal.len(); - choose(&mut app, &Context::default(), "path.toSelection"); - assert_eq!(app.session.journal.len(), before); - assert!(app.ui.dialogs.is_empty()); - } - - #[test] - fn pen_menu_creates_mask_deletes_path_and_opens_named_path_dialogs() { - let mut app = app(); - app.ui.tool = Tool::Pen; - work_path(&mut app); - choose_open(&mut app, "layer.vectorMask.fromPath"); - let st = app.session.active().unwrap(); - assert!(st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.vector_mask.is_some())); - // The new vector mask's path becomes the active one (#4728); pick the Work Path again. - assert_eq!(app.ui.selected_path.as_deref(), Some("layer")); - app.ui.selected_path = Some("work".into()); - choose_open(&mut app, "edit.defineCustomShape"); - let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); - let fields = &app.ui.dialogs.last().unwrap().fields; - assert_eq!(fields.get("path"), Some(&json!("work"))); - app.ui.dialog_mut(id).unwrap().fields.insert("name".into(), json!("Menu Shape")); - crate::dialogs::confirm(&mut app, id).unwrap(); - assert!(app.session.edit_state.custom_shapes.iter().any(|s| s.name == "Menu Shape")); - choose_open(&mut app, "path.fill"); - let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); - assert_eq!(app.ui.dialogs.last().unwrap().fields.get("name"), Some(&json!("work"))); - crate::dialogs::confirm(&mut app, id).unwrap(); - assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("path.fill")); - choose_open(&mut app, "path.delete"); - assert!(app.session.active().unwrap().doc.work_path.is_none()); - } - - #[test] - fn pen_menu_saved_path_clipping_and_numeric_transform() { - let mut app = app(); - app.ui.tool = Tool::Pen; - work_path(&mut app); - app.run("path.rename", json!({"name": "work", "to": "Cutout"})).unwrap(); - app.ui.selected_path = Some("Cutout".into()); - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - assert!(entry_enabled(&app, app.ui.canvas_tool_menu.as_ref().unwrap(), "path.clippingPath.set")); - choose(&mut app, &Context::default(), "path.clippingPath.set"); - let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); - app.ui.dialog_mut(id).unwrap().fields.insert("flatness".into(), json!(2.0)); - crate::dialogs::confirm(&mut app, id).unwrap(); - assert_eq!(app.session.active().unwrap().doc.clipping_path.as_ref().map(|p| p.name.as_str()), Some("Cutout")); - choose_open(&mut app, "path.transform"); - let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); - app.ui.dialog_mut(id).unwrap().fields.insert("translateX".into(), json!(5.0)); - crate::dialogs::confirm(&mut app, id).unwrap(); - assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("path.transform")); - } - - #[test] - fn pen_shape_menu_enables_shape_ops_and_style_copy_only_when_applicable() { - let mut app = app(); - app.ui.tool = Tool::Pen; - app.run( - "shape.create", - json!({"kind":"path", "path":{"subpaths":[ - {"closed":true,"knots":[[2,2],[20,2],[20,20]]}, - {"closed":true,"knots":[[5,5],[10,5],[10,10]]} - ]}, "fill":"#ff0000", "stroke":{"width":2,"color":"#000000"}}), - ) - .unwrap(); - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); - assert!(entry_enabled(&app, menu, "layer.combineShapes.unite")); - assert!(entry_enabled(&app, menu, "path.style.copyFill")); - assert!(!entry_enabled(&app, menu, "path.style.pasteFill")); - assert!(!entry_enabled(&app, menu, "path.fill")); - choose(&mut app, &Context::default(), "path.style.copyFill"); - assert!(app.session.path_fill_clipboard.is_some()); - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - assert!(entry_enabled(&app, app.ui.canvas_tool_menu.as_ref().unwrap(), "path.style.pasteFill")); - } - - #[test] - fn unfinished_shape_path_disables_actions_that_would_target_the_previous_layer() { - let mut app = app(); - app.ui.tool = Tool::Pen; - app.ui.tool_options.vector_mode = "shape".into(); - for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { - crate::vector_ui::pen_down(&mut app, x, y, false); - crate::vector_ui::pen_up(&mut app); - } - assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); - let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); - for id in ["layer.vectorMask.fromPath", "path.delete", "path.fill", "path.stroke", "path.clippingPath.set", "view.newGuidesFromShape"] { - assert!(!entry_enabled(&app, menu, id), "{id} must not act on the layer replaced by the pending shape"); - } - assert!(entry_enabled(&app, menu, "path.toSelection")); - } -} +mod tests; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs new file mode 100644 index 0000000000..6f8d0a3aa8 --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs @@ -0,0 +1,1220 @@ +use super::*; +use egui::{Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::{Harness, kittest::Queryable}; + +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 32, "height": 32})).unwrap(); + app +} + +fn work_path(app: &mut PhotocraftApp) { + app.run( + "path.set", + json!({"name": "work", "path": {"subpaths": [{"closed": true, "knots": [ + {"anchor": [2, 2], "in": [2, 2], "out": [2, 2]}, + {"anchor": [20, 2], "in": [20, 2], "out": [20, 2]}, + {"anchor": [20, 20], "in": [20, 20], "out": [20, 20]} + ]}]}}), + ) + .unwrap(); +} + +fn choose_open(app: &mut PhotocraftApp, command: &str) { + assert!(open(app, &Context::default(), Tool::Pen, [10.0, 10.0])); + choose(app, &Context::default(), command); +} + +fn harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> { + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.run_steps(4); + h +} + +fn right_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, modifiers: Modifiers) { + h.event(egui::Event::ModifiersChanged(modifiers)); + h.event(egui::Event::PointerMoved(p)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers }); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers }); + h.event(egui::Event::ModifiersChanged(Modifiers::NONE)); + h.run_steps(2); +} + +#[test] +fn canvas_gesture_preserves_selection_and_command_override() { + let mut app = app(); + app.ui.tool = Tool::Lasso; + app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + let revision = h.state().session.active().unwrap().revision; + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_some()); + assert_eq!(h.state().session.active().unwrap().revision, revision, "right-click cannot alter selection"); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_none()); + right_click(&mut h, center, Modifiers::COMMAND); + assert!(h.state().ui.canvas_tool_menu.is_none(), "command right-click belongs to layer picker"); +} + +/// A type layer reading "Hello", being edited with every character selected. +fn editing_type(app: &mut PhotocraftApp) -> u64 { + app.ui.tool = Tool::Type; + let id = app.run("type.create", json!({"text": "Hello", "size": 12, "x": 4, "y": 20})).unwrap()["layer"].as_u64().unwrap(); + crate::menus::invoke(app, &Context::default(), "type.editText", json!({})).unwrap(); + id +} + +fn text_of(app: &PhotocraftApp, id: u64) -> Option { + match &app.session.active().unwrap().doc.layer(photocraft_doc::LayerId(id)).unwrap().content { + LayerContent::Text(t) => Some(t.clone()), + _ => None, + } +} + +#[test] +fn right_click_while_typing_opens_the_type_edit_menu_and_esc_keeps_the_edit() { + let mut app = app(); + editing_type(&mut app); + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + // The app's order: shortcuts (inline type's keys among them), then the canvas. + crate::shortcuts::handle(app, &ctx); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.run_steps(4); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().expect("right-click in a type edit opens a menu"); + assert!(menu.text && !menu.pasteboard && !menu.transform); + assert_eq!(rows(&menu), TYPE_EDIT_MENU); + let edit = h.state().ui.text_edit.clone().expect("the edit goes on under the menu"); + assert_eq!((edit.anchor, edit.caret), (0, 5), "the right-click keeps the selection for Cut and Copy"); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_none(), "Esc closes the menu"); + assert!(h.state().ui.text_edit.is_some(), "and only the menu: the edit isn't committed"); +} + +#[test] +fn type_edit_menu_rows_act_on_the_text_and_the_layer() { + let mut app = app(); + let id = editing_type(&mut app); + let ctx = Context::default(); + let reopen = |app: &mut PhotocraftApp| assert!(open(app, &Context::default(), Tool::Type, [10.0, 10.0])); + // Faux Bold toggles the style of the selected characters, and shows checked. + reopen(&mut app); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(!mode_checked(&app, &menu, FAUX_BOLD) && mode_checked(&app, &menu, "type.antiAlias.smooth")); + choose(&mut app, &ctx, FAUX_BOLD); + assert!(text_of(&app, id).unwrap().char_runs().iter().all(|r| r.style.faux_bold)); + assert!(mode_checked(&app, &menu, FAUX_BOLD)); + // Cut takes the selection out of the text; with nothing selected Copy and Cut grey out. + reopen(&mut app); + choose(&mut app, &ctx, "edit.cut"); + assert_eq!(text_of(&app, id).unwrap().text, ""); + reopen(&mut app); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(!entry_enabled(&app, &menu, "edit.copy") && !entry_enabled(&app, &menu, "edit.cut")); + app.ui.canvas_tool_menu = None; + crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap(); + assert_eq!(text_of(&app, id).unwrap().text, "Hello"); + // Select All selects every character, not the canvas. + reopen(&mut app); + choose(&mut app, &ctx, "select.all"); + let edit = app.ui.text_edit.clone().unwrap(); + assert_eq!((edit.anchor, edit.caret), (0, 5)); + assert!(app.session.active().unwrap().doc.selection.is_none()); + // Warp Text… opens its dialog; Rasterize Type ends the edit and rasterizes the layer. + reopen(&mut app); + choose(&mut app, &ctx, "type.warpText"); + assert!(!app.ui.dialogs.is_empty(), "Warp Text… asks for its settings"); + app.ui.dialogs.clear(); + reopen(&mut app); + choose(&mut app, &ctx, "type.rasterizeTypeLayer"); + assert!(app.ui.text_edit.is_none() && text_of(&app, id).is_none(), "the type layer is pixels now"); +} + +/// A committed type layer reading "Hello" over the middle of the canvas, no edit running. +fn committed_type(app: &mut PhotocraftApp) -> u64 { + app.ui.tool = Tool::Type; + let id = app.run("type.create", json!({"text": "Hello", "size": 14, "x": 1, "y": 22})).unwrap()["layer"].as_u64().unwrap(); + assert!(app.ui.text_edit.is_none()); + id +} + +#[test] +fn right_click_on_committed_type_opens_the_type_layer_menu() { + use photocraft_doc::text::Orientation; + let mut app = app(); + let id = committed_type(&mut app); + assert_eq!(crate::type_tool::hit_layer(&mut app, 16.0, 16.0).map(|l| l.0), Some(id), "the canvas centre is on the type"); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().expect("right-click on committed type opens a menu (#7324)"); + assert!(menu.type_layer && !menu.text && !menu.pasteboard); + assert_eq!(rows(&menu), TYPE_LAYER_MENU); + assert!(h.state().ui.text_edit.is_none(), "the right-click doesn't start an edit"); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_some(), "the menu stays open without an edit"); + // An edit started some other way (here, as an agent's Type › Edit Text would) closes the + // menu, which has no text edit rows. + crate::menus::invoke(h.state_mut(), &Context::default(), "type.editText", json!({})).unwrap(); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_none(), "an edit closes the type layer menu"); + crate::type_tool::commit(h.state_mut()); + right_click(&mut h, center, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().expect("committed again, the menu opens again"); + assert!(menu.type_layer && mode_checked(h.state(), &menu, "type.orientation.horizontal")); + choose(h.state_mut(), &Context::default(), "type.orientation.vertical"); + assert_eq!(text_of(h.state(), id).unwrap().orientation, Orientation::Vertical); +} + +#[test] +fn type_layer_menu_targets_the_type_under_the_pointer() { + let mut app = app(); + let id = committed_type(&mut app); + app.run("layer.new.layer", json!({})).unwrap(); + let ctx = Context::default(); + let active = |app: &PhotocraftApp| app.session.active().unwrap().active_layer.map(|l| l.0); + assert_ne!(active(&app), Some(id)); + // Away from the type, or with a Type Mask tool, nothing opens and nothing is selected. + assert!(!open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([30.0, 2.0]))); + assert!(!open_at(&mut app, &ctx, Tool::HorizontalTypeMask, [10.0, 10.0], Some([16.0, 16.0]))); + assert!(!open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([f64::NAN, 16.0]))); + assert_ne!(active(&app), Some(id)); + // Over the type: its layer becomes the target, and the menu has no text edit rows. + assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([16.0, 16.0]))); + assert_eq!(active(&app), Some(id)); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + for edit_row in ["edit.undo", "edit.cut", "edit.copy", "edit.paste", "select.all", FAUX_BOLD, FAUX_ITALIC] { + assert!(!available(&app, &menu, edit_row), "{edit_row} belongs to the edit menu only"); + } + assert!(entry_enabled(&app, &menu, "type.convertToParagraphText") && !entry_enabled(&app, &menu, "type.convertToPointText")); + choose(&mut app, &ctx, "type.rasterizeTypeLayer"); + assert!(text_of(&app, id).is_none(), "the type layer is pixels now"); + assert!(app.ui.text_edit.is_none()); +} + +/// Two selected type layers: the one right-clicked becomes active, and the selection stays. +#[test] +fn type_layer_menu_in_a_multiselection_acts_on_the_clicked_type() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.ui.tool = Tool::Type; + let top = app.run("type.create", json!({"text": "Top", "size": 30, "x": 10, "y": 40})).unwrap()["layer"].as_u64().unwrap(); + let bottom = app.run("type.create", json!({"text": "Low", "size": 30, "x": 10, "y": 90})).unwrap()["layer"].as_u64().unwrap(); + app.run("layer.select", json!({"layer": bottom})).unwrap(); + app.run("layer.select", json!({"layer": top, "mode": "add"})).unwrap(); + let at = [25.0, 80.0]; + assert_eq!(crate::type_tool::hit_layer(&mut app, at[0], at[1]).map(|l| l.0), Some(bottom)); + assert_eq!(app.session.active().unwrap().active_layer.map(|l| l.0), Some(top)); + let ctx = Context::default(); + assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some(at))); + let st = app.session.active().unwrap(); + assert_eq!(st.active_layer.map(|l| l.0), Some(bottom), "the clicked type is the target"); + assert_eq!(st.selected_layers().len(), 2, "the multiselection stays"); + choose(&mut app, &ctx, "type.rasterizeTypeLayer"); + assert!(text_of(&app, bottom).is_none(), "the clicked type is rasterized"); + assert!(text_of(&app, top).is_some(), "not the other selected type"); +} + +#[test] +fn type_edit_menu_lists_spelling_suggestions_for_the_word_under_the_pointer() { + let mut app = app(); + app.ui.tool = Tool::Type; + let id = app.run("type.create", json!({"text": "Teh cat", "size": 12, "x": 4, "y": 20})).unwrap()["layer"].as_u64().unwrap(); + let ctx = Context::default(); + crate::menus::invoke(&mut app, &ctx, "type.editText", json!({})).unwrap(); + // Away from the misspelling (past the end of the line): no spelling rows. + assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([600.0, 15.0]))); + assert!(spelling_rows(app.ui.canvas_tool_menu.as_ref().unwrap()).is_empty()); + // Over "Teh": its suggestions head the menu, then Add to Dictionary. + assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([5.0, 15.0]))); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + let rows = spelling_rows(&menu); + assert!(rows.iter().any(|(l, _)| l == "The"), "{rows:?}"); + assert_eq!(rows.last().map(|(_, id)| id.as_str()), Some(SPELLING_ADD)); + assert!(rows.len() <= MAX_SUGGESTIONS + 1, "at most {MAX_SUGGESTIONS} suggestions and Add to Dictionary: {rows:?}"); + assert!(available(&app, &menu, &rows[0].1)); + let pick = rows.iter().find(|(l, _)| l == "The").unwrap().1.clone(); + choose(&mut app, &ctx, &pick); + assert_eq!(text_of(&app, id).unwrap().text, "The cat"); + assert_eq!(app.ui.text_edit.as_ref().map(|e| (e.caret, e.anchor)), Some((3, 3)), "the caret lands after the replacement"); + // Add to Dictionary stops flagging a word. + app.run("type.insertText", json!({"layer": id, "text": " Zqx"})).unwrap(); + assert!(open_at(&mut app, &ctx, Tool::Type, [10.0, 10.0], Some([600.0, 15.0]))); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(menu.spelling.as_ref().is_some_and(|s| s.word == "Zqx")); + choose(&mut app, &ctx, SPELLING_ADD); + assert!(app.session.prefs().user_dictionary.iter().any(|w| w == "Zqx")); +} + +#[test] +fn right_click_outside_the_image_picks_the_pasteboard_color() { + let mut app = app(); + app.ui.tool = Tool::RectMarquee; + let mut h = harness(app); + let rect = h.state().last_canvas_rect; + let outside = rect.min + vec2(5.0, 5.0); + right_click(&mut h, rect.center(), Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| !m.pasteboard), "inside: the tool's own menu"); + h.state_mut().ui.canvas_tool_menu = None; + right_click(&mut h, outside, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().unwrap(); + assert!(menu.pasteboard && rows(&menu) == PASTEBOARD_MENU, "outside: the pasteboard menu"); + let ctx = h.ctx.clone(); + choose(h.state_mut(), &ctx, "pasteboard.black"); + assert_eq!(crate::prefs_ui::pasteboard_color(h.state()), Some(egui::Color32::BLACK)); + // Select Custom Color… opens the Color Picker; OK makes its colour the custom pasteboard. + right_click(&mut h, outside, Modifiers::NONE); + choose(h.state_mut(), &ctx, PASTEBOARD_PICK); + let mut f = h.state().ui.dialogs.last().unwrap().fields.clone(); + f.insert("color".into(), json!("#123456")); + crate::color_picker_ui::confirm(h.state_mut(), &f).unwrap(); + assert_eq!(crate::prefs_ui::pasteboard_color(h.state()), Some(egui::Color32::from_rgb(0x12, 0x34, 0x56))); + // A painting tool keeps its Brush Preset picker there. + h.state_mut().ui.tool = Tool::Brush; + h.run_steps(2); + right_click(&mut h, outside, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_none() && h.state().ui.brush_picker.is_some()); +} + +/// #2832: right-click over an open Free Transform box: Photoshop's transform menu. Skew +/// switches the box's mode; Rotate and Flip turn the box about its reference point without +/// committing it; Content-Aware Scale and Puppet Warp are greyed. +#[test] +fn right_click_during_free_transform_switches_modes_rotates_and_flips_the_box() { + let mut app = app(); + app.ui.tool = Tool::RectMarquee; + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 4, "y": 6, "width": 10, "height": 8})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + let mut h = harness(app); + let ctx = h.ctx.clone(); + crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransform", json!({})).unwrap(); + h.run_steps(2); + let center = h.state().last_canvas_rect.center(); + let (revision, journal) = (h.state().session.active().unwrap().revision, h.state().session.journal.len()); + right_click(&mut h, center, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().expect("the transform menu"); + assert!(menu.transform && rows(&menu) == TRANSFORM_MENU); + for (id, on) in [("edit.contentAwareScale", false), ("edit.puppetWarp", false), ("edit.transform.rotate90Cw", true), ("edit.transform.flipVertical", true)] + { + assert_eq!(entry_enabled(h.state(), &menu, id), on, "{id}"); + } + choose(h.state_mut(), &ctx, "edit.transform.skew"); + assert_eq!(h.state().ui.transform.as_ref().unwrap().mode, crate::state::TransformMode::Skew); + let t = h.state().ui.transform.clone().unwrap(); + let (q0, c) = (t.quad, t.pivot); + right_click(&mut h, center, Modifiers::NONE); + choose(h.state_mut(), &ctx, "edit.transform.flipHorizontal"); + let q = h.state().ui.transform.as_ref().expect("the box stays open").quad; + for (a, b) in q.iter().zip(q0) { + assert!((a[0] - (2.0 * c[0] - b[0])).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9, "mirrored about the reference point: {q0:?} -> {q:?}"); + } + right_click(&mut h, center, Modifiers::NONE); + choose(h.state_mut(), &ctx, "edit.transform.rotate180"); + let q = h.state().ui.transform.as_ref().unwrap().quad; + for (a, b) in q.iter().zip(q0) { + assert!((a[1] - (2.0 * c[1] - b[1])).abs() < 1e-9 && (a[0] - b[0]).abs() < 1e-9, "flipped then turned 180°: {q0:?} -> {q:?}"); + } + let st = h.state().session.active().unwrap(); + assert_eq!((st.revision, h.state().session.journal.len()), (revision, journal), "nothing is committed"); +} + +#[test] +fn transform_menu_follows_photoshop_order() { + assert_eq!( + labels(TRANSFORM_MENU), + vec![ + Some("Free Transform"), + Some("Scale"), + Some("Rotate"), + Some("Skew"), + Some("Distort"), + Some("Perspective"), + Some("Warp"), + None, + Some("Content-Aware Scale"), + Some("Puppet Warp"), + None, + Some("Rotate 180°"), + Some("Rotate 90° Clockwise"), + Some("Rotate 90° Counter Clockwise"), + None, + Some("Flip Horizontal"), + Some("Flip Vertical"), + ] + ); + // Over a box on editable type, Distort and Perspective are greyed (#2630). + let mut app = app(); + app.run("type.create", json!({"x": 2, "y": 20, "text": "Hi", "size": 16})).unwrap(); + crate::transform_tool::begin(&mut app, &Context::default()).unwrap(); + assert!(open_transform(&mut app, &Context::default(), [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + for (id, on) in [("edit.transform.skew", true), ("edit.transform.distort", false), ("edit.transform.perspective", false)] { + assert_eq!(entry_enabled(&app, &menu, id), on, "{id}"); + } +} + +fn labels(rows: &[Row]) -> Vec> { + rows.iter().map(|row| row.map(|(label, _)| label)).collect() +} + +#[test] +fn selection_menus_follow_photoshop_order() { + assert_eq!( + labels(SELECTION_MENU), + vec![ + Some("Deselect"), + Some("Select Inverse"), + Some("Feather…"), + Some("Select and Mask…"), + None, + Some("Save Selection…"), + Some("Make Work Path…"), + None, + Some("Layer via Copy"), + Some("Layer via Cut"), + Some("New Layer…"), + None, + Some("Free Transform"), + Some("Transform Selection"), + Some("Distort"), + Some("Perspective"), + None, + Some("Delete and Fill Selection"), + Some("Fill…"), + Some("Stroke…"), + Some("Content-Aware Fill…"), + None, + Some("Last Filter"), + None, + Some("Fade…"), + ] + ); + assert_eq!(labels(NO_SELECTION_MENU), vec![Some("Select All"), Some("Reselect"), Some("Color Range…"), None, Some("Load Selection…")]); + // Every row runs a real command: one the menu bar has, or an engine command. + let app = app(); + let items = crate::menus::menu_items(&app); + for (label, id) in [SELECTION_MENU, NO_SELECTION_MENU, PEN_MENU, TRANSFORM_MENU].into_iter().flatten().flatten() { + assert!(items.iter().any(|i| i.id == *id) || photocraft_engine::commands::find(id).is_some(), "{label}: unknown command {id}"); + } +} + +#[test] +fn selection_menu_routes_actions_through_commands() { + let mut app = app(); + app.ui.tool = Tool::RectMarquee; + app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); + assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); + assert_eq!(rows(app.ui.canvas_tool_menu.as_ref().unwrap()), SELECTION_MENU); + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), "select.inverse"); + assert!(app.ui.canvas_tool_menu.is_none()); + assert!(app.session.journal[before..].iter().any(|(id, _)| id == "select.inverse")); + let before = app.session.journal.len(); + assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); + choose(&mut app, &Context::default(), "select.deselect"); + assert!(app.session.active().unwrap().doc.selection.is_none()); + assert!(app.session.journal[before..].iter().any(|(id, _)| id == "select.deselect")); + // Nothing selected: the menu that makes a selection. + assert!(open(&mut app, &Context::default(), Tool::RectMarquee, [10.0, 10.0])); + assert_eq!(rows(app.ui.canvas_tool_menu.as_ref().unwrap()), NO_SELECTION_MENU); + choose(&mut app, &Context::default(), "select.reselect"); + assert!(app.session.active().unwrap().doc.selection.is_some()); + assert!(app.session.journal.iter().any(|(id, _)| id == "select.reselect")); + app.run("select.deselect", json!({})).unwrap(); + assert!(open(&mut app, &Context::default(), Tool::Lasso, [10.0, 10.0])); + choose(&mut app, &Context::default(), "select.all"); + assert!(app.session.active().unwrap().doc.selection.is_some()); +} + +#[test] +fn selection_menu_layer_and_path_actions() { + let mut app = app(); + app.ui.tool = Tool::EllipseMarquee; + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap(); + let layers = app.session.active().unwrap().doc.layers.len(); + assert!(open(&mut app, &Context::default(), Tool::EllipseMarquee, [10.0, 10.0])); + choose(&mut app, &Context::default(), "layer.new.layerViaCopy"); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers + 1, "Layer via Copy adds a layer"); + assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("layer.new.layerViaCopy")); + // Make Work Path… asks for the tolerance, then traces the selection. + app.run("select.rect", json!({"x": 4, "y": 4, "width": 10, "height": 10})).unwrap(); + assert!(open(&mut app, &Context::default(), Tool::EllipseMarquee, [10.0, 10.0])); + choose(&mut app, &Context::default(), "select.toWorkPath"); + let dialog = app.ui.dialogs.last().map(|d| d.id).expect("tolerance dialog"); + assert!(app.session.active().unwrap().doc.work_path.is_none(), "nothing happens before OK"); + crate::dialogs::confirm(&mut app, dialog).unwrap(); + assert!(app.session.active().unwrap().doc.work_path.is_some()); + assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("select.toWorkPath")); +} + +#[test] +fn disabled_selection_rows_do_nothing() { + let mut app = app(); + app.ui.tool = Tool::MagicWand; + app.run("select.rect", json!({"x": 1, "y": 1, "width": 8, "height": 8})).unwrap(); + assert!(open(&mut app, &Context::default(), Tool::MagicWand, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + // No filter has run and nothing can be faded yet. + for id in ["filter.lastFilter", "edit.fade"] { + assert!(!entry_enabled(&app, &menu, id), "{id} is greyed out"); + } + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), "edit.fade"); + assert_eq!(app.session.journal.len(), before); + assert!(app.ui.dialogs.is_empty()); + // Rows of the other variant are not part of this menu. + assert!(open(&mut app, &Context::default(), Tool::MagicWand, [10.0, 10.0])); + choose(&mut app, &Context::default(), "select.all"); + assert_eq!(app.session.journal.len(), before); +} + +/// Photoshop's Zoom tool menu is Fit on Screen, 100%, 200%, Print Size, then Zoom In and Zoom +/// Out after a separator (#6005); Fill Screen (the Hand tool's) keeps its place by Fit on Screen. +#[test] +fn view_menu_follows_photoshop_order() { + assert_eq!( + labels(VIEW_MENU), + vec![Some("Fit on Screen"), Some("Fill Screen"), Some("100%"), Some("200%"), Some("Print Size"), None, Some("Zoom In"), Some("Zoom Out")] + ); + // Every row but Fill Screen (the Hand tool's options-bar button) is a View menu command. + let app = app(); + let items = crate::menus::menu_items(&app); + for (label, id) in VIEW_MENU.iter().flatten().filter(|(_, id)| *id != FILL_SCREEN) { + assert!(items.iter().any(|i| i.id == *id), "{label}: unknown command {id}"); + } +} + +fn zoom_app(w: u32, h: u32) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": w, "height": h})).unwrap(); + app +} + +fn screen_of(app: &PhotocraftApp, doc: [f32; 2]) -> Pos2 { + crate::canvas::ViewXform::active(app).unwrap().to_screen(doc[0], doc[1]) +} + +#[test] +fn zoom_and_hand_right_click_opens_the_view_menu() { + for tool in [Tool::Zoom, Tool::Hand] { + let mut app = zoom_app(400, 300); + app.ui.tool = tool; + let mut h = harness(app); + let zoom = h.state().ui.views[0].zoom; + let inside = screen_of(h.state(), [300.0, 100.0]); + right_click(&mut h, inside, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().unwrap_or_else(|| panic!("{tool:?}: right-click opens no menu")); + assert!(rows(&menu) == VIEW_MENU, "{tool:?}: the view menu"); + assert_eq!(h.state().ui.views[0].zoom, zoom, "{tool:?}: the right-click alone does not zoom"); + assert!(entry_enabled(h.state(), &menu, FILL_SCREEN) && entry_enabled(h.state(), &menu, "view.printSize")); + // Outside the image the pasteboard colour menu wins, as for the other tools. + h.state_mut().ui.canvas_tool_menu = None; + let outside = h.state().last_canvas_rect.min + vec2(5.0, 5.0); + right_click(&mut h, outside, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.pasteboard), "{tool:?}: pasteboard menu outside"); + } +} + +#[test] +fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() { + let mut app = zoom_app(400, 300); + app.ui.tool = Tool::Zoom; + let mut h = harness(app); + let zoom = h.state().ui.views[0].zoom; + let doc = [300.0, 100.0]; + let p = screen_of(h.state(), doc); + right_click(&mut h, p, Modifiers::NONE); + // Chosen in the menu itself: the canvas frame that draws the menu must keep the new view. + h.get_by_label("Zoom In").click(); + h.run_steps(3); + assert!(h.state().ui.canvas_tool_menu.is_none()); + let after = h.state().ui.views[0].zoom; + assert_eq!(after, crate::zoom_levels::step(zoom, 1, [400, 300]), "one Zoom In step"); + let s = screen_of(h.state(), doc); + assert!((s - p).length() < 1.0, "the right-clicked point stays put: {s:?} vs {p:?}"); + assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("view.zoomIn")); + // Zoom Out returns to where it was. + right_click(&mut h, p, Modifiers::NONE); + h.get_by_label("Zoom Out").click(); + h.run_steps(3); + assert_eq!(h.state().ui.views[0].zoom, zoom); + assert!((screen_of(h.state(), doc) - p).length() < 1.0); +} + +#[test] +fn animated_zoom_off_view_menu_keeps_the_anchor_without_a_transition() { + for command in ["view.zoomIn", "view.zoomOut", FILL_SCREEN] { + let mut app = zoom_app(400, 300); + app.ui.tool = Tool::Zoom; + app.run("prefs.set", json!({"values": {"general.animatedZoom": false, "tools.zoomClickedPointToCenter": false}})).unwrap(); + let mut h = harness(app); + let doc = [300.0, 100.0]; + let p = screen_of(h.state(), doc); + right_click(&mut h, p, Modifiers::NONE); + let ctx = h.ctx.clone(); + choose(h.state_mut(), &ctx, command); + assert!(!h.state().ui.views[0].zoom_animating, "{command}"); + h.step(); + let v = &h.state().ui.views[0]; + let xf = crate::canvas::ViewXform::active(h.state()).unwrap(); + assert_eq!((xf.zoom, xf.center), (v.zoom, v.center), "{command}"); + assert!(v.zoom_anchor.is_none() && v.zoom_transition.is_none(), "{command}"); + if command != FILL_SCREEN { + assert!(screen_of(h.state(), doc).distance(p) < 1.0, "{command}: preserve the clicked point"); + } + } +} + +#[test] +fn view_menu_zoom_keeps_its_anchor_during_the_animation() { + let mut app = zoom_app(400, 300); + app.ui.tool = Tool::Zoom; + let mut h = harness(app); + let zoom = h.state().ui.views[0].zoom; + let doc = [300.0, 100.0]; + let p = screen_of(h.state(), doc); + right_click(&mut h, p, Modifiers::NONE); + let ctx = h.ctx.clone(); + // Choose without the widget click helper, which runs repaint frames until settled. + choose(h.state_mut(), &ctx, "view.zoomIn"); + h.step(); + h.input_mut().time = Some(ctx.input(|i| i.time) + 0.11); + h.step(); + let displayed = crate::canvas::ViewXform::active(h.state()).unwrap(); + assert!(displayed.zoom > zoom && displayed.zoom < h.state().ui.views[0].zoom); + assert!(displayed.to_screen(doc[0], doc[1]).distance(p) < 1.0); + h.input_mut().time = Some(ctx.input(|i| i.time) + 0.3); + h.step(); + assert!(!h.state().ui.views[0].zoom_animating); +} + +#[test] +fn view_menu_zoom_in_centres_the_clicked_point_with_the_preference() { + let mut app = zoom_app(400, 300); + app.ui.tool = Tool::Hand; + app.run("prefs.set", json!({"path": "tools.zoomClickedPointToCenter", "value": true})).unwrap(); + let mut h = harness(app); + let doc = [300.0, 100.0]; + let p = screen_of(h.state(), doc); + right_click(&mut h, p, Modifiers::NONE); + h.get_by_label("Zoom In").click(); + h.run_steps(3); + assert_eq!(h.state().ui.views[0].center, doc); +} + +#[test] +fn view_menu_fits_fills_and_sets_fixed_zooms() { + let mut app = zoom_app(400, 300); + app.ui.tool = Tool::Zoom; + let mut h = harness(app); + let p = screen_of(h.state(), [200.0, 150.0]); + let area = crate::rulers::content_rect(h.state(), h.state().last_canvas_rect).size(); + let ppp = h.state().canvas_ppp(); + let pick = |h: &mut Harness<'static, PhotocraftApp>, label: &str| { + right_click(h, p, Modifiers::NONE); + h.get_by_label(label).click(); + h.run_steps(3); + h.state().ui.views[0].zoom + }; + assert_eq!(pick(&mut h, "200%"), 2.0); + // Fill Screen: the image covers the whole canvas (one axis can overflow). + let fill = pick(&mut h, "Fill Screen") / ppp; + assert!(400.0 * fill >= area.x - 1.0 && 300.0 * fill >= area.y - 1.0, "fill {fill} for {area:?}"); + assert!(400.0 * fill <= area.x + 1.0 || 300.0 * fill <= area.y + 1.0, "fill {fill} for {area:?}"); + assert_eq!(h.state().ui.views[0].center, [200.0, 150.0]); + // Fit on Screen: back to the whole image (capped at 100%, as on open). + let fit = pick(&mut h, "Fit on Screen") / ppp; + assert!(400.0 * fit <= area.x && 300.0 * fit <= area.y && fit < fill, "fit {fit} fill {fill}"); +} + +#[test] +fn view_menu_needs_finite_points_and_a_view_tool() { + let mut app = app(); + assert!(!open_view(&mut app, &Context::default(), Tool::Brush, [1.0, 1.0], [1.0, 1.0])); + assert!(!open_view(&mut app, &Context::default(), Tool::Zoom, [f32::NAN, 1.0], [1.0, 1.0])); + assert!(!open_view(&mut app, &Context::default(), Tool::Zoom, [1.0, 1.0], [f64::INFINITY, 1.0])); + assert!(app.ui.canvas_tool_menu.is_none()); + assert!(open_view(&mut app, &Context::default(), Tool::Zoom, [1.0, 1.0], [1.0, 1.0])); + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), "select.all"); + assert_eq!(app.session.journal.len(), before, "the view menu cannot run another menu's row"); + // With no document every row is greyed out and choosing one does nothing. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + assert!(open_view(&mut app, &Context::default(), Tool::Hand, [1.0, 1.0], [1.0, 1.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + for (_, id) in VIEW_MENU.iter().flatten() { + assert!(!entry_enabled(&app, &menu, id), "{id} is greyed out with no document"); + } + choose(&mut app, &Context::default(), FILL_SCREEN); + choose(&mut app, &Context::default(), "view.zoomIn"); +} + +#[test] +fn unsupported_tools_and_nonfinite_positions_do_not_open_menu() { + let mut app = app(); + assert!(!open(&mut app, &Context::default(), Tool::Brush, [1.0, 1.0])); + assert!(!open(&mut app, &Context::default(), Tool::Lasso, [f32::NAN, 1.0])); + assert!(app.ui.canvas_tool_menu.is_none()); + assert!(open(&mut app, &Context::default(), Tool::MagicWand, [1.0, 1.0])); + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), "file.new"); + assert_eq!(app.session.journal.len(), before, "context menu cannot invoke an unrelated command"); +} + +#[test] +fn completed_pen_path_right_click_makes_selection() { + let mut app = app(); + app.ui.tool = Tool::Pen; + for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + crate::vector_ui::pen_commit(&mut app, true); + assert!(app.session.active().unwrap().doc.work_path.is_some()); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == Tool::Pen && m.has_path)); + let ctx = h.ctx.clone(); + choose(h.state_mut(), &ctx, "path.toSelection"); + let dialog = h.state().ui.dialogs.last().map(|d| d.id).unwrap(); + crate::dialogs::confirm(h.state_mut(), dialog).unwrap(); + assert!(h.state().session.active().unwrap().doc.selection.is_some()); + assert_eq!(h.state().session.journal.last().map(|(id, _)| id.as_str()), Some("path.toSelection")); + // Like ⌘↩, it deselects and hides the work path, which stays in the document (#6079). + assert_eq!(h.state().ui.selected_path, None); + assert!(h.state().session.active().unwrap().doc.work_path.is_some()); + assert!(!crate::vector_ui::work_path_shown(h.state())); + // The hidden work path is no longer the active path: a second right-click offers no path + // commands, so Delete Path can't remove it unseen. + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.tool == Tool::Pen && !m.has_path && m.path_name.is_none())); + choose(h.state_mut(), &ctx, "path.delete"); + assert!(h.state().session.active().unwrap().doc.work_path.is_some(), "the hidden work path survives Delete Path"); +} + +#[test] +fn pen_menu_finishes_in_progress_path_before_selection() { + let mut app = app(); + app.ui.tool = Tool::Pen; + for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + assert!(app.session.active().unwrap().doc.work_path.is_none()); + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); + assert!(entry_enabled(&app, menu, "path.toSelection")); + choose(&mut app, &Context::default(), "path.toSelection"); + let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); + crate::dialogs::confirm(&mut app, dialog).unwrap(); + assert!(app.session.active().unwrap().doc.work_path.is_some()); + assert!(app.session.active().unwrap().doc.selection.is_some()); +} + +/// #5703: the Pen options bar's Make Selection (Path mode) opens the same dialog for the path +/// being drawn (finished into the Work Path first), and OK selects inside it; with no path at +/// all it reports that instead. +#[test] +fn pen_options_bar_makes_a_selection_from_the_path() { + let mut app = app(); + app.ui.tool = Tool::Pen; + assert!(make_selection(&mut app).is_err(), "no path yet"); + assert!(app.ui.dialogs.is_empty()); + for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + make_selection(&mut app).unwrap(); + let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); + crate::dialogs::confirm(&mut app, dialog).unwrap(); + let st = app.session.active().unwrap(); + assert!(st.doc.work_path.is_some() && st.doc.selection.is_some()); + // That hid the Work Path (#6079): it is not the active path until it is selected in the + // Paths panel again, and then it is, with no pen path in progress. + app.run("select.deselect", json!({})).unwrap(); + assert!(make_selection(&mut app).is_err(), "the hidden Work Path is not the active path"); + assert!(app.ui.dialogs.is_empty()); + app.ui.selected_path = Some("work".into()); + make_selection(&mut app).unwrap(); + let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); + crate::dialogs::confirm(&mut app, dialog).unwrap(); + assert!(app.session.active().unwrap().doc.selection.is_some()); + // Drawn with a saved path selected, the path is added to that one (#4721): the dialog is + // for it, not for a Work Path. + app.run("path.rename", json!({"name": "work", "to": "Cutout"})).unwrap(); + app.ui.selected_path = Some("Cutout".into()); + for (x, y) in [(4.0, 24.0), (12.0, 24.0), (12.0, 30.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + make_selection(&mut app).unwrap(); + assert_eq!(app.ui.dialogs.last().and_then(|d| d.fields.get("name")), Some(&json!("Cutout"))); +} + +/// #1056: the Path Selection and Direct Selection tools' right-click opens the path menu for +/// the selected path, as the Pen's does, instead of nothing. +#[test] +fn path_selection_tools_open_the_path_menu() { + for tool in [Tool::PathSelection, Tool::DirectSelection] { + let mut app = app(); + app.ui.tool = Tool::Pen; + for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + crate::vector_ui::pen_commit(&mut app, false); + assert!(app.session.active().unwrap().doc.work_path.is_some()); + app.ui.tool = tool; + assert!(applies(tool)); + assert!(open(&mut app, &Context::default(), tool, [10.0, 10.0]), "{tool:?}"); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert_eq!(rows(&menu), PEN_MENU, "{tool:?}"); + assert!(menu.has_path && entry_enabled(&app, &menu, "path.toSelection"), "{tool:?}"); + choose(&mut app, &Context::default(), "path.toSelection"); + let dialog = app.ui.dialogs.last().map(|d| d.id).unwrap(); + crate::dialogs::confirm(&mut app, dialog).unwrap(); + assert!(app.session.active().unwrap().doc.selection.is_some(), "{tool:?}: Make Selection made one"); + } +} + +#[test] +fn pen_menu_has_all_reference_rows_in_order_and_guards_unavailable_actions() { + let labels: Vec<_> = PEN_MENU.iter().map(|row| row.map(|(label, _)| label)).collect(); + assert_eq!( + labels, + vec![ + Some("Create Vector Mask"), + Some("Delete Path"), + None, + Some("Define Custom Shape…"), + None, + Some("Make Selection…"), + Some("New Guides From Shape"), + Some("Fill Path…"), + Some("Stroke Path…"), + None, + Some("Clipping Path…"), + None, + Some("Free Transform Path"), + None, + Some("Unite Shapes"), + Some("Subtract Front Shape"), + Some("Unite Shapes at Overlap"), + Some("Subtract Shapes at Overlap"), + None, + Some("Copy Fill"), + Some("Copy Complete Stroke"), + None, + Some("Paste Fill"), + Some("Paste Complete Stroke"), + None, + Some("Isolate Layers"), + None, + Some("Make Symmetry Path"), + Some("Disable Symmetry Path"), + ] + ); + let mut app = app(); + app.ui.tool = Tool::Pen; + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); + assert!(!entry_enabled(&app, menu, "path.toSelection")); + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), "path.toSelection"); + assert_eq!(app.session.journal.len(), before); + assert!(app.ui.dialogs.is_empty()); +} + +#[test] +fn pen_menu_creates_mask_deletes_path_and_opens_named_path_dialogs() { + let mut app = app(); + app.ui.tool = Tool::Pen; + work_path(&mut app); + choose_open(&mut app, "layer.vectorMask.fromPath"); + let st = app.session.active().unwrap(); + assert!(st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.vector_mask.is_some())); + // The new vector mask's path becomes the active one (#4728); pick the Work Path again. + assert_eq!(app.ui.selected_path.as_deref(), Some("layer")); + app.ui.selected_path = Some("work".into()); + choose_open(&mut app, "edit.defineCustomShape"); + let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); + let fields = &app.ui.dialogs.last().unwrap().fields; + assert_eq!(fields.get("path"), Some(&json!("work"))); + app.ui.dialog_mut(id).unwrap().fields.insert("name".into(), json!("Menu Shape")); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert!(app.session.edit_state.custom_shapes.iter().any(|s| s.name == "Menu Shape")); + choose_open(&mut app, "path.fill"); + let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); + assert_eq!(app.ui.dialogs.last().unwrap().fields.get("name"), Some(&json!("work"))); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("path.fill")); + choose_open(&mut app, "path.delete"); + assert!(app.session.active().unwrap().doc.work_path.is_none()); +} + +#[test] +fn pen_menu_saved_path_clipping_and_numeric_transform() { + let mut app = app(); + app.ui.tool = Tool::Pen; + work_path(&mut app); + app.run("path.rename", json!({"name": "work", "to": "Cutout"})).unwrap(); + app.ui.selected_path = Some("Cutout".into()); + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + assert!(entry_enabled(&app, app.ui.canvas_tool_menu.as_ref().unwrap(), "path.clippingPath.set")); + choose(&mut app, &Context::default(), "path.clippingPath.set"); + let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); + app.ui.dialog_mut(id).unwrap().fields.insert("flatness".into(), json!(2.0)); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert_eq!(app.session.active().unwrap().doc.clipping_path.as_ref().map(|p| p.name.as_str()), Some("Cutout")); + choose_open(&mut app, "path.transform"); + let id = app.ui.dialogs.last().map(|d| d.id).unwrap(); + app.ui.dialog_mut(id).unwrap().fields.insert("translateX".into(), json!(5.0)); + crate::dialogs::confirm(&mut app, id).unwrap(); + assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("path.transform")); +} + +#[test] +fn pen_shape_menu_enables_shape_ops_and_style_copy_only_when_applicable() { + let mut app = app(); + app.ui.tool = Tool::Pen; + app.run( + "shape.create", + json!({"kind":"path", "path":{"subpaths":[ + {"closed":true,"knots":[[2,2],[20,2],[20,20]]}, + {"closed":true,"knots":[[5,5],[10,5],[10,10]]} + ]}, "fill":"#ff0000", "stroke":{"width":2,"color":"#000000"}}), + ) + .unwrap(); + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); + assert!(entry_enabled(&app, menu, "layer.combineShapes.unite")); + assert!(entry_enabled(&app, menu, "path.style.copyFill")); + assert!(!entry_enabled(&app, menu, "path.style.pasteFill")); + assert!(!entry_enabled(&app, menu, "path.fill")); + choose(&mut app, &Context::default(), "path.style.copyFill"); + assert!(app.session.path_fill_clipboard.is_some()); + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + assert!(entry_enabled(&app, app.ui.canvas_tool_menu.as_ref().unwrap(), "path.style.pasteFill")); +} + +#[test] +fn unfinished_shape_path_disables_actions_that_would_target_the_previous_layer() { + let mut app = app(); + app.ui.tool = Tool::Pen; + app.ui.tool_options.vector_mode = "shape".into(); + for (x, y) in [(2.0, 2.0), (20.0, 2.0), (20.0, 20.0)] { + crate::vector_ui::pen_down(&mut app, x, y, false); + crate::vector_ui::pen_up(&mut app); + } + assert!(open(&mut app, &Context::default(), Tool::Pen, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.as_ref().unwrap(); + for id in ["layer.vectorMask.fromPath", "path.delete", "path.fill", "path.stroke", "path.clippingPath.set", "view.newGuidesFromShape"] { + assert!(!entry_enabled(&app, menu, id), "{id} must not act on the layer replaced by the pending shape"); + } + assert!(entry_enabled(&app, menu, "path.toSelection")); +} + +/// A 32 × 32 document with the Crop tool picked (Classic Mode, so the view stays put) and its +/// untouched default frame. +fn crop_app() -> PhotocraftApp { + let mut app = app(); + app.ui.tool = Tool::Crop; + app.ui.tool_options.crop_shield.classic_mode = true; + app.ui.extras.snap = false; + crate::crop_ui::ensure_frame(&mut app); + app +} + +/// Draws a crop frame from (4, 4) to (28, 28): a pending crop. Returns the frame. +fn draw_crop(app: &mut PhotocraftApp) -> [f64; 4] { + use crate::canvas::{ToolEvent, tool_event}; + tool_event(app, ToolEvent::Down { x: 4.0, y: 4.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: 16.0, y: 16.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Move { x: 28.0, y: 28.0, pressure: 1.0 }, Modifiers::NONE); + tool_event(app, ToolEvent::Up { x: 28.0, y: 28.0 }, Modifiers::NONE); + assert!(crate::crop_ui::pending(app), "the drag draws a pending frame"); + app.ui.crop_rect.unwrap() +} + +fn aspect(r: [f64; 4]) -> f64 { + (r[2] - r[0]) / (r[3] - r[1]) +} + +/// #6561: right-click on a pending crop opens its menu without touching the frame, and a ratio +/// row refits the frame as the options bar's dropdown does. +#[test] +fn right_click_on_a_pending_crop_opens_the_crop_menu_and_a_ratio_refits_the_frame() { + let mut app = crop_app(); + let frame = draw_crop(&mut app); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().expect("right-click on a pending crop opens a menu"); + assert_eq!(h.state().ui.crop_rect, Some(frame), "the right-click leaves the frame alone"); + assert!(available(h.state(), &menu, "crop.ratio.16:9"), "the menu lists the options bar's ratios"); + let ctx = h.ctx.clone(); + choose(h.state_mut(), &ctx, "crop.ratio.16:9"); + assert_eq!(h.state().ui.tool_options.crop_ratio, "16:9"); + let r = h.state().ui.crop_rect.unwrap(); + assert!((aspect(r) - 16.0 / 9.0).abs() < 1e-9, "the frame is 16 : 9 now: {r:?}"); + assert!(r[0] >= frame[0] && r[1] >= frame[1] && r[2] <= frame[2] && r[3] <= frame[3], "fitted inside the old frame: {r:?}"); + assert!(h.state().ui.canvas_tool_menu.is_none()); +} + +#[test] +fn untouched_crop_frame_has_no_menu_and_a_pending_one_wins_over_the_pasteboard() { + let mut h = harness(crop_app()); + let rect = h.state().last_canvas_rect; + let outside = rect.min + vec2(5.0, 5.0); + right_click(&mut h, rect.center(), Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_none(), "the tool's untouched frame is no crop in progress"); + right_click(&mut h, outside, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.pasteboard), "outside: the pasteboard menu, as before"); + h.state_mut().ui.canvas_tool_menu = None; + draw_crop(h.state_mut()); + h.run_steps(2); + right_click(&mut h, outside, Modifiers::NONE); + let menu = h.state().ui.canvas_tool_menu.clone().unwrap(); + assert!(menu.crop.is_some() && !menu.pasteboard, "with a crop pending, its menu also outside the image"); +} + +/// A harness in the app's order: shortcuts (the crop's Esc and ↵ among them), then the canvas. +fn shortcut_harness(mut app: PhotocraftApp) -> Harness<'static, PhotocraftApp> { + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::shortcuts::handle(app, &ctx); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.run_steps(4); + h +} + +#[test] +fn esc_closes_only_the_crop_menu() { + let mut app = crop_app(); + let frame = draw_crop(&mut app); + let mut h = shortcut_harness(app); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.crop.is_some())); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_none(), "Esc closes the menu"); + assert_eq!(h.state().ui.crop_rect, Some(frame), "and only the menu: the crop is not cancelled"); + assert!(crate::crop_ui::pending(h.state())); +} + +/// ↵ under the open crop menu doesn't commit the crop beneath it. +#[test] +fn enter_under_the_crop_menu_does_not_crop() { + let mut app = crop_app(); + let frame = draw_crop(&mut app); + let mut h = shortcut_harness(app); + let size = h.state().session.active().map(|s| s.doc.size); + let center = h.state().last_canvas_rect.center(); + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.crop.is_some())); + h.key_press(egui::Key::Enter); + h.run_steps(2); + assert_eq!(h.state().session.active().map(|s| s.doc.size), size, "↵ under the menu doesn't crop"); + assert_eq!(h.state().ui.crop_rect, Some(frame)); + assert!(crate::crop_ui::pending(h.state())); +} + +/// A right-click while the left button drags the crop frame opens no menu over the drag, on +/// the image or outside it (no pasteboard menu either), and the menu opens again once the left +/// button is up, though egui ended that drag without a release of its own. +#[test] +fn no_crop_menu_while_the_left_button_drags_the_frame() { + let mut app = crop_app(); + draw_crop(&mut app); + let mut h = harness(app); + let rect = h.state().last_canvas_rect; + let (center, outside) = (rect.center(), rect.min + vec2(5.0, 5.0)); + let left = |pos, pressed| egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + h.event(egui::Event::PointerMoved(center)); + h.run_steps(1); + h.event(left(center, true)); + h.run_steps(1); + for k in 1..=4 { + h.event(egui::Event::PointerMoved(center + vec2(8.0 * k as f32, 0.0))); + h.run_steps(1); + } + assert!(h.state().crop.drag.is_some(), "the left button drags the frame"); + let held = center + vec2(32.0, 0.0); + right_click(&mut h, held, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_none(), "no crop menu over the drag"); + right_click(&mut h, outside, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_none(), "nor the pasteboard menu outside the image"); + h.event(egui::Event::PointerMoved(held)); + h.event(left(held, false)); + h.run_steps(2); + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.crop.is_some()), "with the button up the menu opens"); +} + +/// The left button down with no crop gesture under way (a press egui doesn't take for a drag +/// yet, or one whose release was lost to an Alt+Tab or a cancelled touch) doesn't keep the +/// crop menu shut. +#[test] +fn a_left_button_down_without_a_crop_drag_keeps_the_crop_menu() { + let mut app = crop_app(); + draw_crop(&mut app); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + h.event(egui::Event::PointerMoved(center)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: center, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + assert!(h.ctx.input(|i| i.pointer.primary_down()) && h.state().crop.drag.is_none(), "down, dragging nothing"); + right_click(&mut h, center, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.crop.is_some()), "the crop menu opens"); +} + +/// Nor does a right-click open a menu over a Straighten line the left button still draws. +#[test] +fn no_crop_menu_while_the_left_button_draws_a_straighten_line() { + let mut app = crop_app(); + draw_crop(&mut app); + crate::crop_straighten::toggle(&mut app); + let mut h = harness(app); + let center = h.state().last_canvas_rect.center(); + let left = |pos, pressed| egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + h.event(egui::Event::PointerMoved(center)); + h.run_steps(1); + h.event(left(center, true)); + h.run_steps(1); + for k in 1..=4 { + h.event(egui::Event::PointerMoved(center + vec2(8.0 * k as f32, 4.0))); + h.run_steps(1); + } + assert!(h.state().crop.straighten.line.is_some() && h.state().crop.drag.is_none(), "the left button draws a Straighten line"); + right_click(&mut h, center + vec2(32.0, 4.0), Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_none(), "no menu over the line"); +} + +/// A finger held still on a pending crop (a touch screen's press-and-hold right-click) opens +/// the crop menu, though the touch counts as the primary button down. +#[test] +fn a_finger_held_on_a_pending_crop_opens_the_crop_menu() { + let mut app = crop_app(); + let frame = draw_crop(&mut app); + let mut h = harness(app); + let at = h.state().last_canvas_rect.center(); + let touch = |phase| egui::Event::Touch { device_id: egui::TouchDeviceId(1), id: egui::TouchId(1), phase, pos: at, force: None }; + let button = |pressed| egui::Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + h.input_mut().events.extend([egui::Event::PointerMoved(at), touch(egui::TouchPhase::Start), button(true)]); + h.step(); + // Held past egui's max_click_duration (0.8 s) without moving. + for _ in 0..120 { + if h.state().ui.canvas_tool_menu.is_some() { + break; + } + h.step(); + } + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.crop.is_some()), "the held finger opens the crop menu"); + h.input_mut().events.extend([touch(egui::TouchPhase::End), button(false)]); + h.run_steps(2); + assert_eq!(h.state().ui.crop_rect, Some(frame), "the hold leaves the frame alone"); +} + +#[test] +fn crop_menu_rows_act_like_the_options_bar() { + use crate::crop_overlay::{CropOverlay, OverlayShow}; + let ctx = Context::default(); + let mut app = crop_app(); + draw_crop(&mut app); + // Space's Hand held over the Crop tool keeps its own right-click. + assert!(!open_crop(&mut app, &ctx, Tool::Hand, [10.0, 10.0])); + let reopen = |app: &mut PhotocraftApp| { + assert!(open_crop(app, &Context::default(), Tool::Crop, [10.0, 10.0])); + app.ui.canvas_tool_menu.clone().unwrap() + }; + // Nothing to save with a free ratio, nothing to delete without saved presets. + let menu = reopen(&mut app); + assert!(!entry_enabled(&app, &menu, CROP_NEW) && !entry_enabled(&app, &menu, CROP_DELETE)); + assert!(mode_checked(&app, &menu, "crop.ratio."), "free Ratio is checked, as in the dropdown"); + // A size preset fills W x H x Resolution, which the menu checks, as the dropdown does. + choose(&mut app, &ctx, "crop.ratio.preset:4x5in"); + let menu = reopen(&mut app); + assert!(mode_checked(&app, &menu, "crop.ratio.whr") && !mode_checked(&app, &menu, "crop.ratio.preset:4x5in")); + assert!((aspect(app.ui.crop_rect.unwrap()) - 0.8).abs() < 1e-9); + assert!(entry_enabled(&app, &menu, CROP_NEW)); + // Clear empties W x H x Resolution, as the bar's Clear. + choose(&mut app, &ctx, CROP_CLEAR); + assert_eq!(app.ui.tool_options.crop_width, ""); + // Overlays: another overlay starts at its first orientation; when it shows. + app.ui.tool_options.crop_overlay = CropOverlay::GoldenSpiral; + app.ui.tool_options.crop_overlay_orientation = 2; + reopen(&mut app); + choose(&mut app, &ctx, "crop.overlay.triangle"); + assert_eq!((app.ui.tool_options.crop_overlay, app.ui.tool_options.crop_overlay_orientation), (CropOverlay::Triangle, 0)); + reopen(&mut app); + choose(&mut app, &ctx, "crop.overlayShow.never"); + assert_eq!(app.ui.tool_options.crop_overlay_show, OverlayShow::Never); + let menu = reopen(&mut app); + assert!(mode_checked(&app, &menu, "crop.overlay.triangle") && mode_checked(&app, &menu, "crop.overlayShow.never")); + // A saved preset is listed by name; picking it refits the frame to its W x H, though the + // ratio key is "whr" before and after. + app.ui.canvas_tool_menu = None; + app.ui.tool_options.crop_ratio = crate::crop_size::WHR.into(); + let saved = json!({"crop_ratio": "whr", "crop_width": "300 px", "crop_height": "100 px", "crop_resolution": "72", "crop_resolution_unit": "px/in"}); + app.run("tool.presets.new", json!({"name": "Banner", "tool": "crop", "options": {"toolOptions": saved}})).unwrap(); + let menu = reopen(&mut app); + let banner = crop_rows(&menu).into_iter().flatten().find(|(l, _)| l == "Banner").expect("the saved preset is a row").1; + assert!(entry_enabled(&app, &menu, CROP_DELETE)); + choose(&mut app, &ctx, &banner); + assert_eq!(app.ui.tool_options.crop_width, "300 px"); + assert!((aspect(app.ui.crop_rect.unwrap()) - 3.0).abs() < 1e-9, "the frame follows the preset: {:?}", app.ui.crop_rect); + let menu = reopen(&mut app); + assert!(mode_checked(&app, &menu, &banner)); + // Crop commits as ↵ does: the preset's size. + choose(&mut app, &ctx, CROP_COMMIT); + let size = app.session.active().unwrap().doc.size; + assert_eq!((size.width, size.height), (300, 100)); + // Cancel drops a pending crop. + draw_crop(&mut app); + reopen(&mut app); + choose(&mut app, &ctx, CROP_CANCEL); + assert!(!crate::crop_ui::pending(&app)); +} diff --git a/crates/ui-egui/src/canvas/channel_view.rs b/crates/ui-egui/src/canvas/channel_view.rs new file mode 100644 index 0000000000..4d7602e4a1 --- /dev/null +++ b/crates/ui-egui/src/canvas/channel_view.rs @@ -0,0 +1,390 @@ +//! Channel display on the canvas: alpha / spot / Quick Mask colour overlays, single colour +//! channel views (grayscale) and hidden colour channels. +//! +//! The document composite (GPU or CPU) is untouched. When the Channels panel shows anything other +//! than the plain composite, one extra texture is drawn over it: transparent overlay pixels when +//! the composite stays visible, an opaque picture otherwise. The texture is keyed by document +//! revision and view state and updated per damage rectangle while painting, so nothing is +//! recomposited per frame. + +use egui::{Color32, TextureOptions}; +use photocraft_doc::{AlphaChannel, ColorMode, Document}; +use photocraft_engine::channel_cmds::{ChannelView, color_count}; +use photocraft_engine::mask_view_cmds::MaskViewMode; +use photocraft_geom::Rect; +use photocraft_raster::{Surface, from_rgba, to_rgba}; + +use crate::PhotocraftApp; + +/// Largest overlay texture side; bigger documents show a downsampled overlay. +const MAX_SIDE: u32 = 4096; +/// Limit the sampled float composite to 128 MiB across open documents. +const MAX_COMPOSITE_CACHE_PIXELS: usize = 8 * 1024 * 1024; + +struct SampledComposite { + revision: u64, + /// `revision` minus the view-only part: what the pixels depend on. + content: u64, + preview_key: u64, + factor: u32, + pixels: Vec<[f32; 4]>, +} + +struct CompositeSource<'a> { + cached: Option<&'a [[f32; 4]]>, + fresh: &'a mut Option>, +} + +pub struct Cache { + revision: u64, + key: u64, + /// The canvas document shown by a live stroke (0 is the committed document). + pub(crate) preview_key: u64, + factor: u32, + /// False while the plain composite shows: the texture is kept (so toggling a view back on + /// reuses its allocation) but its pixels are stale. + valid: bool, + tex: egui::TextureHandle, + source: Option, +} + +fn sample(s: &Surface, r: Rect, factor: u32) -> Vec { + let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); + if factor == 1 { + let mut v = Vec::new(); + s.read_region_into(r, &mut v); + let ch = s.channels(); + return if ch > 1 { v.chunks_exact(ch).map(|p| p[0]).collect() } else { v }; + } + let mut out = Vec::with_capacity((w * h) as usize); + let mut row = vec![0.0f32; w as usize]; + for y in 0..h { + s.sample_row_strided(r.y0 + (y * factor) as i32, r.x0, factor as i32, 0, &mut row); + out.extend_from_slice(&row); + } + out +} + +/// How the view draws what it shows. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Look { + /// Single colour channels tinted instead of grayscale (Photoshop's "Show Channels in Color"). + pub in_color: bool, + /// The layer mask rubylith's colour and opacity (0..1): Layer Mask Display Options. + pub mask_rgb: [f32; 3], + pub mask_opacity: f32, +} + +impl Default for Look { + /// Photoshop's: grayscale channels, a red rubylith at 50 %. + fn default() -> Self { + Self { in_color: false, mask_rgb: AlphaChannel::DEFAULT_COLOR.to_rgb(), mask_opacity: 0.5 } + } +} + +impl Look { + /// The current preferences of `s`. + pub fn of(s: &photocraft_engine::Session) -> Self { + let p = s.prefs(); + Self { in_color: p.interface.show_channels_in_color, mask_rgb: p.layer_mask_overlay.rgb(), mask_opacity: p.layer_mask_overlay.alpha() } + } +} + +/// Composite RGBA (over white, like Photoshop's channel views) at `factor` stride. +/// Render in bands instead of allocating a full-resolution float composite to sample a preview. +fn composite(doc: &Document, r: Rect, factor: u32) -> Vec<[f32; 4]> { + let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); + let mut out = Vec::with_capacity((w * h) as usize); + let f = factor as i32; + let _ = photocraft_compose::render_bands(doc, r, 0, |band| -> Result<(), ()> { + let first = band.rect.y0 + (r.y0 - band.rect.y0).rem_euclid(f); + for y in (first..band.rect.y1).step_by(factor as usize) { + for x in (r.x0..r.x1).step_by(factor as usize) { + let p = band.get(x, y); + let a = p[3].clamp(0.0, 1.0); + out.push([p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0]); + } + } + Ok(()) + }); + out +} + +/// The channel view of `r` (document pixels, sampled every `factor` px) as premultiplied pixels, +/// or `None` when the plain composite is all there is to show. +pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, look: Look, active: Option) -> Option> { + let mut unused = None; + render_with_source(doc, v, r, factor, look, active, CompositeSource { cached: None, fresh: &mut unused }) +} + +fn render_with_source( + doc: &Document, + v: &ChannelView, + r: Rect, + factor: u32, + look: Look, + active: Option, + source: CompositeSource<'_>, +) -> Option> { + if v.is_plain(doc) || r.is_empty() { + return None; + } + let in_color = look.in_color; + let (mask_rgb, mask_opacity) = (look.mask_rgb, look.mask_opacity.clamp(0.0, 1.0)); + let layer_mask = v.shown_layer_mask(doc); + // ⌥-click mask view (#196): the pixel mask alone, in grayscale (white reveals); ⇧⌥ with + // nothing else shown: just the rubylith. Use the compositor's cached feather for the pixel + // mask, but leave vector masks out: this view targets the pixel mask channel by itself. + let feathered_mask = v + .layer_mask + .and_then(|view| doc.layer(view.layer)) + .filter(|layer| layer.mask.as_ref().is_some_and(|mask| mask.enabled && photocraft_compose::masks::feather_sigma(mask.feather) > 0.0)) + .and_then(|layer| { + let mut pixel_mask_layer = layer.clone(); + pixel_mask_layer.vector_mask = None; + photocraft_compose::masks::combined_mask(&pixel_mask_layer, doc.bounds()) + }); + let shown_mask = layer_mask.map(|(m, mode)| (feathered_mask.as_ref().unwrap_or(&m.surface), mode)); + let only_mask = ChannelView { layer_mask: None, ..v.clone() }.is_plain(doc); + match shown_mask { + Some((m, MaskViewMode::Gray)) => return Some(mask_px(m, r, factor, |g| Color32::from_gray(q(g)))), + Some((m, MaskViewMode::Overlay)) if only_mask => { + let c = mask_rgb; + return Some(mask_px(m, r, factor, |g| { + let a = mask_opacity * (1.0 - g.clamp(0.0, 1.0)); + Color32::from_rgba_premultiplied(q(c[0] * a), q(c[1] * a), q(c[2] * a), q(a)) + })); + } + _ => {} + } + let colors = color_count(doc); + let vis = v.visible_colors(colors); + let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); + let n = (w * h) as usize; + let mut overlays: Vec<&AlphaChannel> = (0..doc.channels.len()).filter(|i| v.alpha_shown(*i)).map(|i| &doc.channels[i]).collect(); + let gray = |s: &Surface| sample(s, r, factor).into_iter().map(|g| [g, g, g, 1.0]).collect(); + // Premultiplied RGBA accumulator. With no colour channel left, the channel being edited + // (alpha, Quick Mask, or layer mask) is the picture — grayscale, not a rubylith over nothing. + let mut solo_quick = false; + let mut solo_mask = false; + let mut out: Vec<[f32; 4]> = if vis == 0 { + if !overlays.is_empty() { + let first = overlays.remove(0); + gray(&first.surface) + } else if let Some(q) = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden) { + solo_quick = true; + gray(&q.surface) + } else if let Some((m, _)) = shown_mask { + solo_mask = true; + gray(m) + } else { + let m = active.and_then(|id| doc.layer(id)).and_then(|l| l.mask.as_ref())?; + solo_mask = true; + gray(&m.surface) + } + } else if vis < colors { + let fmt = doc.pixel_format(); + let mode = fmt.mode; + let shown: Vec = (0..colors).filter(|k| v.color_visible(*k)).collect(); + let computed = if source.cached.is_some_and(|px| px.len() == n) { None } else { Some(composite(doc, r, factor)) }; + let pixels = source.cached.filter(|px| px.len() == n).or(computed.as_deref())?; + let converted = pixels + .iter() + .copied() + .map(|rgba| { + let mut native = from_rgba(&fmt, rgba); + if shown.len() == 1 && !in_color { + let x = native[shown[0]]; + // Ink channels read dark where there is ink. + let g = if mode == ColorMode::Cmyk { 1.0 - x } else { x }; + return [g, g, g, 1.0]; + } + for (k, x) in native[..colors].iter_mut().enumerate() { + if !v.color_visible(k) { + // Hidden channels contribute nothing (Lab: neutral a/b, and a neutral + // Lightness under Show Channels in Color, so an isolated a/b channel + // tints over mid gray instead of black, as Photoshop does). + *x = if mode == ColorMode::Lab && (k > 0 || in_color) { 0.5 } else { 0.0 }; + } + } + let c = to_rgba(&fmt, &native); + [c[0], c[1], c[2], 1.0] + }) + .collect(); + if n <= MAX_COMPOSITE_CACHE_PIXELS { + *source.fresh = computed; + } + converted + } else { + vec![[0.0; 4]; n] + }; + // Rubylith: `color` at `opacity` × coverage over the accumulated pixels. + let mut tint = |values: Vec, color: photocraft_color::Color, opacity: f32, coverage: &dyn Fn(f32) -> f32| { + let c = color.to_rgb(); + for (o, val) in out.iter_mut().zip(values) { + let a = (coverage(val.clamp(0.0, 1.0)) * opacity).clamp(0.0, 1.0); + for j in 0..3 { + o[j] = c[j] * a + o[j] * (1.0 - a); + } + o[3] = a + o[3] * (1.0 - a); + } + }; + let quick = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden && !solo_quick); + for ch in overlays.into_iter().chain(quick) { + let (color, opacity) = match ch.spot { + // Spot inks show more solid as solidity rises. + Some((ink, solidity)) => (ink, 0.5 + 0.5 * solidity), + None => (ch.color, ch.opacity), + }; + tint(sample(&ch.surface, r, factor), color, opacity, &|x| ch.overlay_coverage(x)); + } + // ⇧⌥-click mask view (#196): the layer mask tinted over the hidden areas in the Layer Mask + // Display Options colour and opacity (red at 50 % unless set). + if !solo_mask && let Some((m, MaskViewMode::Overlay)) = shown_mask { + let [cr, cg, cb] = mask_rgb; + tint(sample(m, r, factor), photocraft_color::Color::rgb(cr, cg, cb), mask_opacity, &|x| 1.0 - x); + } + Some(out.into_iter().map(|p| Color32::from_rgba_premultiplied(q(p[0]), q(p[1]), q(p[2]), q(p[3]))).collect()) +} + +/// `r` of a one-channel surface sampled every `factor` px, mapped to colours row by row (in +/// parallel off the web). +fn mask_px(s: &Surface, r: Rect, factor: u32, color: impl Fn(f32) -> Color32 + Sync) -> Vec { + let (w, h) = (r.width().div_ceil(factor) as usize, r.height().div_ceil(factor) as usize); + let mut px = vec![Color32::TRANSPARENT; w * h]; + let row = |y: usize, out: &mut [Color32]| { + let mut vals = vec![0.0f32; out.len()]; + s.sample_row_strided(r.y0 + (y as u32 * factor) as i32, r.x0, factor as i32, 0, &mut vals); + for (o, v) in out.iter_mut().zip(vals) { + *o = color(v); + } + }; + if w == 0 { + return px; + } + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + px.par_chunks_mut(w).enumerate().for_each(|(y, out)| row(y, out)); + } + #[cfg(target_arch = "wasm32")] + for (y, out) in px.chunks_mut(w).enumerate() { + row(y, out); + } + px +} + +/// A float in 0..=1 as an 8-bit channel value. +fn q(x: f32) -> u8 { + (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8 +} + +fn view_key(v: &ChannelView, look: Look) -> u64 { + let s = serde_json::to_string(&(v, look.mask_rgb, look.mask_opacity)).unwrap_or_default(); + s.bytes().fold(look.in_color as u64 + 1, |h, b| h.wrapping_mul(1_099_511_628_211).wrapping_add(b as u64)) +} + +/// Make the channel-view texture of document `idx` current; `None` = nothing to draw. +pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Option { + let st = app.session.documents().get(idx)?; + let view = st.channel_view.clone(); + let active = st.active_layer; + let (revision, last_damage) = (st.revision, st.last_damage); + let content = revision.wrapping_sub(st.view_revision); + let id = st.doc.id.0; + if view.is_plain(&st.doc) { + if let Some(c) = app.channel_views.get_mut(&id) { + c.valid = false; + } + return None; + } + // Mask and channel views are drawn over the canvas: show the same live document, + // otherwise an opaque grayscale mask hides the stroke until it is committed (#2078). + // The plain-composite path above needs no preview work. + let (doc, preview_key) = crate::canvas::filter_channel_preview(app, idx, ctx).unwrap_or_else(|| crate::canvas::display_doc(app, idx)); + // Interface ▸ Show Channels in Color tints single colour channel views on the canvas too, + // not just the panel thumbnails (#2385). The view key carries the flag and the Layer Mask + // Display Options, so changing them rebuilds the texture. + let look = Look::of(&app.session); + let key = view_key(&view, look); + // Never exceed what the GPU accepts (egui panics on oversized textures). + let max_side = (ctx.input(|i| i.max_texture_side) as u32).clamp(256, MAX_SIDE); + let factor = doc.size.width.max(doc.size.height).div_ceil(max_side).max(1); + // A live preview can mutate the same Arc in place. Content revision and preview_key, not + // pointer identity, determine whether these flattened pixels are still current. + if let Some(c) = app.channel_views.get_mut(&id) { + if let Some(s) = c.source.as_mut().filter(|s| s.factor == factor && s.content == content && s.preview_key == preview_key) { + s.revision = revision; + } else { + c.source = None; + } + } + if let Some(c) = app.channel_views.get(&id) + && c.key == key + && c.factor == factor + && c.valid + && c.revision == revision + && c.preview_key == preview_key + { + return Some(c.tex.id()); + } + let damage = + app.channel_views.get(&id).and_then(|c| crate::canvas::damage_since(app, idx, (c.revision, c.preview_key), (revision, preview_key), 0, last_damage)); + if let Some(c) = app.channel_views.get_mut(&id) + && c.key == key + && c.factor == factor + && c.valid + && let Some(d) = damage + { + // Re-render just the damage, snapped to the sampling grid so a downsampled texture + // (big documents) updates in place too. + let f = factor as i32; + let d = d.intersect(&doc.bounds()); + let r = Rect::new(d.x0.div_euclid(f) * f, d.y0.div_euclid(f) * f, d.x1, d.y1).intersect(&doc.bounds()); + if !r.is_empty() + && let Some(px) = render(&doc, &view, r, factor, look, active) + { + let img = egui::ColorImage::new([r.width().div_ceil(factor) as usize, r.height().div_ceil(factor) as usize], px); + c.tex.set_partial([(r.x0 / f) as usize, (r.y0 / f) as usize], img, TextureOptions::LINEAR); + } + c.revision = revision; + c.preview_key = preview_key; + return Some(c.tex.id()); + } + let b = doc.bounds(); + let cached = app.channel_views.get(&id).and_then(|c| c.source.as_ref()).map(|s| s.pixels.as_slice()); + let mut fresh = None; + let px = render_with_source(&doc, &view, b, factor, look, active, CompositeSource { cached, fresh: &mut fresh })?; + let new_source = fresh.is_some(); + let size = [b.width().div_ceil(factor) as usize, b.height().div_ceil(factor) as usize]; + let img = egui::ColorImage::new(size, px); + match app.channel_views.get_mut(&id) { + Some(c) if c.tex.size() == size => { + c.tex.set(img, TextureOptions::LINEAR); + c.revision = revision; + c.preview_key = preview_key; + c.key = key; + c.factor = factor; + c.valid = true; + if let Some(pixels) = fresh.take() { + c.source = Some(SampledComposite { revision, content, preview_key, factor, pixels }); + } + } + _ => { + let tex = ctx.load_texture(format!("channel-view-{id}"), img, TextureOptions::LINEAR); + let source = fresh.map(|pixels| SampledComposite { revision, content, preview_key, factor, pixels }); + app.channel_views.insert(id, Cache { revision, key, preview_key, factor, valid: true, tex, source }); + } + } + if new_source { + for (other_id, c) in &mut app.channel_views { + if *other_id != id { + c.source = None; + } + } + } + app.channel_views.get(&id).map(|c| c.tex.id()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/channel_view/tests.rs b/crates/ui-egui/src/canvas/channel_view/tests.rs new file mode 100644 index 0000000000..d8581ebbbb --- /dev/null +++ b/crates/ui-egui/src/canvas/channel_view/tests.rs @@ -0,0 +1,351 @@ +use super::*; +use photocraft_engine::Session; +use photocraft_engine::channel_cmds::ChannelTarget; +use serde_json::json; + +fn session() -> Session { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 4})).unwrap(); + s +} + +#[test] +fn composite_bands_keep_the_downsample_grid() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 41, "height": 700})).unwrap(); + s.execute("edit.fill", json!({"color": "#398db2"})).unwrap(); + let doc = &s.active().unwrap().doc; + let rect = doc.bounds(); + let full = photocraft_compose::render(doc, rect); + let full_ref = &full; + for factor in [1, 2, 3, 5] { + let expected: Vec<_> = (0..rect.height()) + .step_by(factor as usize) + .flat_map(|y| { + (0..rect.width()).step_by(factor as usize).map(move |x| { + let p = full_ref.get(rect.x0 + x as i32, rect.y0 + y as i32); + let a = p[3].clamp(0.0, 1.0); + [p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0] + }) + }) + .collect(); + assert_eq!(composite(doc, rect, factor), expected, "factor {factor}"); + } +} + +#[test] +fn channel_eyes_reuse_composite_until_pixels_change() { + let mut app = PhotocraftApp::new(Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 32})).unwrap(); + app.run("edit.fill", json!({"color": "#4080c0"})).unwrap(); + app.sync_views(); + let ctx = egui::Context::default(); + app.run("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + let id = app.session.active().unwrap().doc.id.0; + let first = app.channel_views.get_mut(&id).unwrap().source.as_mut().unwrap(); + let original_sample = first.pixels[0]; + // Mark the cache: a recomposite overwrites it, while a freed buffer's address can come back. + first.pixels[0] = [0.0, 1.0, 0.0, 1.0]; + + app.run("channel.setVisible", json!({"channel": "green", "visible": false})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + let reused = app.channel_views.get(&id).unwrap().source.as_ref().unwrap(); + assert_eq!(reused.pixels[0], [0.0, 1.0, 0.0, 1.0], "visibility does not recompose the pixels"); + + app.run("edit.fill", json!({"color": "#e02010"})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + // A partial damage upload discards the source; a full redraw may replace it. + // In either case, cached samples must never refer to the pre-edit pixels. + if let Some(source) = app.channel_views.get(&id).unwrap().source.as_ref() { + assert_ne!(source.pixels[0], original_sample, "a full redraw replaces stale pixels"); + } + app.run("channel.setVisible", json!({"channel": "red", "visible": true})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + let refreshed = app.channel_views.get(&id).unwrap().source.as_ref().unwrap(); + let st = app.session.active().unwrap(); + assert_eq!(refreshed.revision, st.revision); + assert_ne!(refreshed.pixels[0], original_sample, "new pixels must be recomposited"); + let expected = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + let mut unused = None; + let actual = render_with_source( + &st.doc, + &st.channel_view, + st.doc.bounds(), + 1, + Look::default(), + st.active_layer, + CompositeSource { cached: Some(&refreshed.pixels), fresh: &mut unused }, + ) + .unwrap(); + assert_eq!(actual, expected, "reused pixels must preserve channel colours"); + assert!(unused.is_none(), "reuse must not create a second composite"); +} + +#[test] +fn plain_composite_draws_nothing() { + let s = session(); + let st = s.active().unwrap(); + assert!(render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).is_none()); +} + +#[test] +fn quick_mask_overlay_is_red_over_masked_areas() { + let mut s = session(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap(); + s.execute("select.editInQuickMaskMode", json!({})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(px[0], Color32::TRANSPARENT, "selected: no overlay"); + assert_eq!(px[6], Color32::from_rgba_premultiplied(128, 0, 0, 128), "masked: 50% red"); +} + +#[test] +fn quick_mask_without_selection_shows_no_overlay_in_either_mode() { + // #2743: nothing selected is a colourless mask, whichever way the colour reads. + for (before, after) in [(None, Some("selected")), (Some("selected"), None), (Some("selected"), Some("masked"))] { + let mut s = session(); + if let Some(i) = before { + s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap(); + } + s.execute("select.editInQuickMaskMode", json!({})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?}"); + if let Some(i) = after { + s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?} → {i}"); + } + } + // Selected Areas with Select All: the whole canvas is coloured. + let mut s = session(); + s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap(); + s.execute("select.all", json!({})).unwrap(); + s.execute("select.editInQuickMaskMode", json!({})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert!(px.iter().all(|p| *p == Color32::from_rgba_premultiplied(128, 0, 0, 128))); +} + +#[test] +fn layer_mask_views_and_damage_on_the_downsampled_grid() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 37, "height": 21})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("paint.gradient", json!({"from": [0, 0], "to": [37, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); + s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); + let st = s.active().unwrap(); + let b = st.doc.bounds(); + let full = render(&st.doc, &st.channel_view, b, 1, Look::default(), st.active_layer).unwrap(); + assert!(full[0].r() < 10 && full[36].r() > 245 && full[0].a() == 255, "opaque grayscale ramp"); + // A sub-rect starting on the factor grid matches the same pixels of the full render. + for f in [1u32, 2, 3] { + let all = render(&st.doc, &st.channel_view, b, f, Look::default(), st.active_layer).unwrap(); + let w = b.width().div_ceil(f) as usize; + let r = Rect::new(3 * f as i32, 2 * f as i32, 30, 19); + let part = render(&st.doc, &st.channel_view, r, f, Look::default(), st.active_layer).unwrap(); + let pw = r.width().div_ceil(f) as usize; + for (i, p) in part.iter().enumerate() { + assert_eq!(*p, all[(i / pw + 2) * w + i % pw + 3], "factor {f}, pixel {i}"); + } + } + s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, b, 1, Look::default(), st.active_layer).unwrap(); + assert!(px[0].r() >= 124 && px[0].g() == 0 && px[0].a() >= 124, "about 50% red over the hidden end: {:?}", px[0]); + assert!(px[36].a() <= 4, "next to nothing over the revealed end: {:?}", px[36]); +} + +#[test] +fn layer_mask_views_show_the_feathered_mask() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 37, "height": 21})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("paint.gradient", json!({"from": [0, 0], "to": [37, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); + s.execute("layer.layerMask.edit", json!({"feather": 4})).unwrap(); + s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); + + let st = s.active().unwrap(); + let bounds = st.doc.bounds(); + let layer = st.doc.layer(st.active_layer.unwrap()).unwrap(); + let raw = layer.mask.as_ref().unwrap().surface.sample_channel(0, 0, 0); + let effective = photocraft_compose::masks::combined_mask(layer, bounds).unwrap(); + let softened = effective.sample_channel(0, 0, 0); + assert!((softened - raw).abs() > 0.01, "the fixture has a visible feather at its edge"); + + let gray = render(&st.doc, &st.channel_view, bounds, 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(gray[0], Color32::from_gray(q(softened)), "gray view uses the compositor's feathered mask"); + + s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap(); + let st = s.active().unwrap(); + let overlay = render(&st.doc, &st.channel_view, bounds, 1, Look::default(), st.active_layer).unwrap(); + let alpha = 0.5 * (1.0 - softened); + let red = AlphaChannel::DEFAULT_COLOR.to_rgb(); + assert_eq!(overlay[0], Color32::from_rgba_premultiplied(q(red[0] * alpha), q(red[1] * alpha), q(red[2] * alpha), q(alpha))); +} + +/// Layer Mask Display Options: the rubylith takes their colour and opacity, alone over the +/// composite and with a colour channel hidden. +#[test] +fn layer_mask_overlay_takes_its_display_options() { + let mut s = session(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + s.execute("layer.layerMask.hideAll", json!({})).unwrap(); + s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap(); + let green = Look { mask_rgb: [0.0, 1.0, 0.0], mask_opacity: 0.25, ..Look::default() }; + let st = s.active().unwrap(); + let b = st.doc.bounds(); + assert_eq!(render(&st.doc, &st.channel_view, b, 1, Look::default(), st.active_layer).unwrap()[0], Color32::from_rgba_premultiplied(128, 0, 0, 128)); + assert_eq!(render(&st.doc, &st.channel_view, b, 1, green, st.active_layer).unwrap()[0], Color32::from_rgba_premultiplied(0, 64, 0, 64)); + // Red hidden: the white picture shows cyan, then green at 25 % over it. + s.execute("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap(); + let st = s.active().unwrap(); + assert_eq!(render(&st.doc, &st.channel_view, b, 1, green, st.active_layer).unwrap()[0], Color32::from_rgb(0, 255, 191)); +} + +/// `cargo test --release -p photocraft-ui-egui --lib mask_view_switch_24mp -- --ignored --nocapture` +#[test] +#[ignore = "benchmark"] +fn mask_view_switch_24mp() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 6000, "height": 4000})).unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + s.execute("edit.fill", json!({"color": "#d0402b"})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + s.execute("paint.gradient", json!({"from": [0, 0], "to": [6000, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); + let factor = 6000u32.div_ceil(MAX_SIDE); + for mode in ["gray", "overlay"] { + s.execute("view.layerMask", json!({"mode": mode})).unwrap(); + let st = s.active().unwrap(); + let mut best = f64::MAX; + for _ in 0..8 { + let t = std::time::Instant::now(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), factor, Look::default(), st.active_layer).unwrap(); + let img = egui::ColorImage::new([3000, 2000], px); + std::hint::black_box(img); + best = best.min(t.elapsed().as_secs_f64() * 1000.0); + } + println!("24 MP mask view {mode}: {best:.1} ms (render at 1/{factor})"); + } +} + +/// Interface ▸ Show Channels in Color (#2385): a solo RGB / CMYK channel tints the canvas +/// instead of staying gray, like the panel thumbnails wired in #1986. +#[test] +fn show_channels_in_color_tints_the_canvas() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 4, "height": 4, "mode": "rgb"})).unwrap(); + s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); + s.execute("channel.target", json!({"channel": "green"})).unwrap(); + let st = s.active().unwrap(); + let b = st.doc.bounds(); + assert_eq!(render(&st.doc, &st.channel_view, b, 1, Look::default(), st.active_layer).unwrap()[0], Color32::from_gray(128)); + assert_eq!(render(&st.doc, &st.channel_view, b, 1, Look { in_color: true, ..Look::default() }, st.active_layer).unwrap()[0], Color32::from_rgb(0, 128, 0)); + + let mut s = Session::new(); + s.execute("file.new", json!({"width": 4, "height": 4, "mode": "cmyk"})).unwrap(); + s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); + s.execute("channel.target", json!({"channel": "cyan"})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look { in_color: true, ..Look::default() }, st.active_layer).unwrap()[0]; + assert!(px.r() < 100 && px.g() > 150 && px.b() > 150, "cyan ink, not gray: {px:?}"); + let gray = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap()[0]; + assert_eq!(gray.r(), gray.g(), "off stays grayscale"); +} + +/// Lab: a and b tint over a neutral Lightness; Lightness itself stays gray (Adobe's exception). +#[test] +fn show_channels_in_color_lab_lightness_stays_gray() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 4, "height": 4, "mode": "lab"})).unwrap(); + s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); + let b = s.active().unwrap().doc.bounds(); + s.execute("channel.target", json!({"channel": {"color": 0}})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, b, 1, Look { in_color: true, ..Look::default() }, st.active_layer).unwrap()[0]; + let spread = px.r().max(px.g().max(px.b())) as i32 - px.r().min(px.g().min(px.b())) as i32; + assert!(spread <= 3, "Lightness stays gray: {px:?}"); + s.execute("channel.target", json!({"channel": {"color": 1}})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, b, 1, Look { in_color: true, ..Look::default() }, st.active_layer).unwrap()[0]; + assert!(px.r() != px.g() || px.g() != px.b(), "a channel is tinted, not gray: {px:?}"); +} + +/// The canvas texture cache is keyed by the preference, so toggling it rebuilds the view. +#[test] +fn ensure_honours_show_channels_in_color() { + let mut app = PhotocraftApp::new(Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 8, "height": 8})).unwrap(); + app.sync_views(); + app.run("channel.target", json!({"channel": "green"})).unwrap(); + let ctx = egui::Context::default(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + let key_off = app.channel_views.values().next().unwrap().key; + app.session.execute("prefs.set", json!({"values": {"interface.showChannelsInColor": true}})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + let key_on = app.channel_views.values().next().unwrap().key; + assert_ne!(key_off, key_on, "the preference changes the cached view key"); + // So do the Layer Mask Display Options. + app.session.execute("channel.options", json!({"channel": "mask", "color": "#00ff00", "opacity": 70})).unwrap(); + assert!(ensure(&mut app, &ctx, 0).is_some()); + assert_ne!(app.channel_views.values().next().unwrap().key, key_on, "the overlay options change the cached view key"); +} + +#[test] +fn solo_alpha_is_grayscale_and_colour_channels_split() { + let mut s = session(); + s.execute("edit.fill", json!({"color": "#ff8000"})).unwrap(); + s.execute("select.rect", json!({"x": 0, "y": 0, "width": 2, "height": 4})).unwrap(); + s.execute("channel.new", json!({"fill": "selection"})).unwrap(); + let st = s.active().unwrap(); + assert_eq!(st.channel_view.target, ChannelTarget::Alpha(0)); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!((px[0], px[5]), (Color32::WHITE, Color32::BLACK)); + // Green alone: grayscale of the green channel; with blue hidden only. + s.execute("channel.target", json!({"channel": "green"})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(px[0], Color32::from_gray(128)); + s.execute("channel.target", json!({"channel": "composite"})).unwrap(); + s.execute("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 2, Look::default(), st.active_layer).unwrap(); + assert_eq!(px.len(), 4 * 2); + assert_eq!(px[0], Color32::from_rgb(0, 128, 0)); +} + +#[test] +fn rgb_off_shows_alpha_quick_mask_or_the_layer_mask() { + let mut s = session(); + s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + s.execute("channel.new", json!({"fill": "white"})).unwrap(); + s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap(); + s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); + let st = s.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(px[0], Color32::WHITE, "Alpha alone is the channel, not the red image"); + + let mut q = session(); + q.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + q.execute("select.editInQuickMaskMode", json!({})).unwrap(); + q.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); + let st = q.active().unwrap(); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(px[0], Color32::WHITE, "Quick Mask alone is grayscale, not the red composite"); + + let mut m = session(); + m.execute("layer.new.layer", json!({})).unwrap(); + m.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + m.execute("layer.layerMask.hideAll", json!({})).unwrap(); + m.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); + let st = m.active().unwrap(); + assert_eq!(st.channel_view.visible_colors(3), 0); + let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, Look::default(), st.active_layer).unwrap(); + assert_eq!(px[0], Color32::BLACK, "the hidden mask reads black, not the red layer"); +} diff --git a/crates/ui-egui/src/canvas/cpu_background_tests.rs b/crates/ui-egui/src/canvas/cpu_background_tests.rs new file mode 100644 index 0000000000..dfd7ea77e2 --- /dev/null +++ b/crates/ui-egui/src/canvas/cpu_background_tests.rs @@ -0,0 +1,259 @@ +//! CPU-canvas opacity invalidation and the software renderer's hidden checker pass. +use super::*; +use serde_json::json; + +#[test] +fn cpu_canvas_omits_only_a_checker_hidden_by_an_opaque_texture() { + let ctx = egui::Context::default(); + let image = ctx.load_texture("image", egui::ColorImage::filled([2, 2], Color32::WHITE), TextureOptions::LINEAR); + let checker = ctx.load_texture("checker-test", egui::ColorImage::filled([2, 2], Color32::GRAY), TextureOptions::NEAREST); + for rotated in [false, true] { + for (opaque, texture, want_checker) in [(true, Some(image.id()), false), (false, Some(image.id()), true), (true, None, true)] { + let mut output = ctx.run_ui(Default::default(), |ui| { + let xf = ViewXform { + rect: Rect::from_min_size(Pos2::ZERO, vec2(300.0, 240.0)), + zoom: 1.0, + center: [50.0, 40.0], + flip: false, + rotation: if rotated { 23.0 } else { 0.0 }, + aspect: 1.0, + }; + paint_cpu_document(ui.painter(), &xf, DRect::new(0, 0, 100, 80), rotated, Some((checker.id(), 8.0)), texture, opaque); + }); + let drawn = output.shapes.iter().any(|s| matches!(&s.shape, egui::Shape::Mesh(m) if m.texture_id == checker.id())); + assert_eq!(drawn, want_checker, "rotated={rotated}, opaque={opaque}, texture={texture:?}"); + output.textures_delta.clear(); + } + } +} + +#[test] +fn cpu_texture_opacity_follows_full_and_partial_uploads() { + for depth in [8, 16, 32] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 32, "height": 24, "depth": depth, "background": "transparent"})).unwrap(); + let key = cache_key(app.session.documents()[0].doc.id, None); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(!app.canvases[&key].texture_opaque, "{depth}: transparent canvas needs its checker"); + app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); + // Force a full upload to classify the complete image, as the first opaque image does. + app.canvases.get_mut(&key).unwrap().texture = None; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(app.canvases[&key].texture_opaque); + app.run("select.rect", json!({"x": 2, "y": 3, "width": 4, "height": 5})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + app.run("edit.fill", json!({"color": "#e04010"})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "rect"); + assert!(app.canvases[&key].texture_opaque, "an opaque patch keeps the guarantee"); + app.run("edit.clear", json!({})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "rect"); + assert!(!app.canvases[&key].texture_opaque, "erasing a patch must show the checker again"); + app.run("edit.undo", json!({})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + app.canvases.get_mut(&key).unwrap().texture = None; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(app.canvases[&key].texture_opaque, "a full opaque upload can omit the checker again"); + app.perf.last_refresh = ""; + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.perf.last_refresh, "", "unchanged frames do not upload or classify pixels"); + } +} + +type Harness = egui_kittest::Harness<'static, PhotocraftApp>; + +/// The CPU-canvas fixture on the offscreen software adapter, or `None` when this machine has no +/// graphics adapter. Only creating the renderer may be skipped: a failing fixture command or a +/// panic in the first frames must fail the test, not pass it as "no adapter". +fn harness(size: [u32; 2], background: &str) -> Option { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + // app.gpu remains None: compose on the CPU, use the harness's software window adapter. + app.run("file.new", json!({"width": size[0], "height": size[1], "background": background})).unwrap(); + app.run("select.rect", json!({"x": size[0] / 2, "y": size[1] / 2, "width": size[0] / 10, "height": size[1] / 10})).unwrap(); + app.run("edit.fill", json!({"color": "#2050d0"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + // `wgpu()` creates the render state and panics when no adapter is found. + let builder = std::panic::catch_unwind(|| { + egui_kittest::Harness::::builder().with_size(egui::vec2(1440.0, 900.0)).with_pixels_per_point(1.0).with_max_steps(64).wgpu() + }) + .ok()?; + let mut h = builder.build_eframe(move |cc| { + PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); + if let Some(rs) = &cc.wgpu_render_state { + app.perf.gpu_info.set_adapter(&rs.adapter.get_info()); + } + app + }); + h.run_steps(12); + Some(h) +} + +fn assert_fixture(h: &Harness, background: &str) { + let key = cache_key(h.state().session.documents()[0].doc.id, None); + assert!(h.state().session.prefs().transparency_and_gamut.square().is_some()); + assert_eq!(h.state().canvases[&key].texture_opaque, background == "white", "fixture opacity"); +} + +/// Draw the unchanged reference path by temporarily disabling only the opacity guarantee. +fn reference_frame(h: &mut Harness) { + let flags: Vec<_> = h.state().canvases.iter().map(|(k, c)| (*k, c.texture_opaque)).collect(); + for c in h.state_mut().canvases.values_mut() { + c.texture_opaque = false; + } + h.step(); + for (k, opaque) in flags { + if let Some(c) = h.state_mut().canvases.get_mut(&k) { + c.texture_opaque = opaque; + } + } +} + +#[test] +#[should_panic(expected = "called `Result::unwrap()` on an `Err` value")] +fn fixture_failures_are_not_skipped_as_a_missing_adapter() { + // An invalid fixture document must fail on any machine, with or without a graphics adapter. + let _ = harness([0, 0], "white"); +} + +#[test] +fn omitting_an_occluded_checker_preserves_cpu_canvas_pixels() { + let Some(mut h) = harness([320, 240], "white") else { + eprintln!("skipping: no offscreen graphics adapter"); + return; + }; + assert_fixture(&h, "white"); + eprintln!("CPU pixel comparison adapter: {}", serde_json::to_string(&h.state().perf.gpu_info).unwrap()); + for (rotation, flip, pattern) in + [(0.0, false, false), (23.0, false, false), (0.0, true, false), (0.0, false, true), (23.0, false, true), (0.0, true, true), (23.0, true, true)] + { + h.state_mut().ui.views[0].rotation = rotation; + h.state_mut().ui.view.flip_horizontal = flip; + h.state_mut().ui.view.pattern_preview = pattern; + reference_frame(&mut h); + let reference = h.render().unwrap(); + h.step(); + let optimized = h.render().unwrap(); + if let Some(dir) = std::env::var_os("PHOTOCRAFT_CPU_PIXEL_OUTPUT") { + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).unwrap(); + let tag = format!("rotation-{rotation}-flip-{flip}-pattern-{pattern}"); + reference.save(dir.join(format!("{tag}-reference.png"))).unwrap(); + optimized.save(dir.join(format!("{tag}-optimized.png"))).unwrap(); + let mut changed = 0; + let mut bounds = [u32::MAX, u32::MAX, 0, 0]; + for (x, y, pixel) in reference.enumerate_pixels() { + if pixel != optimized.get_pixel(x, y) { + changed += 1; + bounds = [bounds[0].min(x), bounds[1].min(y), bounds[2].max(x), bounds[3].max(y)]; + } + } + let report = json!({"adapter": h.state().perf.gpu_info, "rotation": rotation, "flip": flip, + "pattern": pattern, "changed_pixels": changed, "difference_bounds": (changed > 0).then_some(bounds)}); + std::fs::write(dir.join(format!("{tag}.json")), serde_json::to_string_pretty(&report).unwrap()).unwrap(); + } + assert!(reference == optimized, "rotation={rotation} flip={flip} pattern={pattern}"); + } +} + +#[test] +fn pattern_preview_keeps_the_legacy_checker_for_every_copy() { + let Some(mut h) = harness([320, 240], "white") else { + eprintln!("skipping: no offscreen graphics adapter"); + return; + }; + assert_fixture(&h, "white"); + let checker = h.state().checker.as_ref().unwrap().id(); + let checker_meshes = |h: &Harness| { + h.output() + .shapes + .iter() + .filter_map(|s| match &s.shape { + egui::Shape::Mesh(m) if m.texture_id == checker => Some(m.clone()), + _ => None, + }) + .collect::>() + }; + for (rotation, flip) in [(0.0, false), (23.0, false), (0.0, true), (23.0, true)] { + h.state_mut().ui.view.pattern_preview = true; + h.state_mut().ui.view.flip_horizontal = flip; + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.25; + v.center = [159.25, 119.75]; + v.rotation = rotation; + v.fit_pending = false; + v.fill_pending = false; + reference_frame(&mut h); + let reference = checker_meshes(&h); + assert!(reference.len() > 1, "the document and its visible copies need their checker"); + h.step(); + assert_eq!(checker_meshes(&h), reference, "rotation={rotation} flip={flip}: retain the legacy checker submissions"); + } + h.state_mut().ui.view.pattern_preview = false; + h.step(); + assert!(checker_meshes(&h).is_empty(), "ordinary opaque CPU canvases still omit the hidden checker"); +} + +#[test] +#[ignore = "manual release benchmark: 24 MP CPU canvas on the offscreen software adapter"] +fn cpu_canvas_background_benchmark() { + use std::time::Instant; + let mut results = Vec::new(); + let output = std::env::var_os("PHOTOCRAFT_CPU_BENCH_OUTPUT").map(std::path::PathBuf::from); + for background in ["white", "transparent"] { + let mut h = harness([6000, 4000], background).expect("an offscreen graphics adapter"); + assert_fixture(&h, background); + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.0; + v.center = [3000.0, 2000.0]; + v.fit_pending = false; + v.fill_pending = false; + for _ in 0..3 { + h.step(); + h.render().unwrap(); + } + let mut before = Vec::new(); + let mut after = Vec::new(); + // Alternate order to reduce warm-up/thermal bias. Same cached image and UI in both paths. + for n in 0..24 { + for reference in if n % 2 == 0 { [true, false] } else { [false, true] } { + h.state_mut().ui.views[0].center[0] = 3000.0 + if n % 2 == 0 { -3.0 } else { 3.0 }; + let t = Instant::now(); + if reference { + reference_frame(&mut h); + } else { + h.step(); + } + h.render().unwrap(); + (if reference { &mut before } else { &mut after }).push(t.elapsed().as_secs_f64() * 1000.0); + } + } + reference_frame(&mut h); + let old = h.render().unwrap(); + h.step(); + let new = h.render().unwrap(); + assert!(old == new, "{background}: same rendered pixels"); + if let Some(dir) = &output { + std::fs::create_dir_all(dir).unwrap(); + old.save(dir.join(format!("cpu-{background}-before.png"))).unwrap(); + new.save(dir.join(format!("cpu-{background}-after.png"))).unwrap(); + } + before.sort_by(f64::total_cmp); + after.sort_by(f64::total_cmp); + results.push(json!({ + "background": background, "document_pixels": 24_000_000, "viewport": [1440, 900], + "opaque_texture": background == "white", "checker_enabled": true, + "adapter": h.state().perf.gpu_info, "samples_per_path": before.len(), + "reference_p50_ms": before[11], "reference_p95_ms": before[22], + "optimized_p50_ms": after[11], "optimized_p95_ms": after[22], + "reference_ms": before, "optimized_ms": after, + "includes": "UI frame, cached CPU canvas rasterization and synchronous readback; no document recomposite", + })); + } + let report = serde_json::to_string_pretty(&results).unwrap(); + println!("{report}"); + if let Some(dir) = output { + std::fs::write(dir.join("cpu-background-benchmark.json"), report).unwrap(); + } +} diff --git a/crates/ui-egui/src/canvas/cursors.rs b/crates/ui-egui/src/canvas/cursors.rs new file mode 100644 index 0000000000..f87e74ebae --- /dev/null +++ b/crates/ui-egui/src/canvas/cursors.rs @@ -0,0 +1,219 @@ +use super::tool_input::ToolEvent; +use super::view_xform::ViewXform; +use super::{Color32, PhotocraftApp, Pos2, Rect, Stroke, Tool, json, pos2, vec2}; + +/// Windows' crosshair cursor inverts the pixels under it, so over mid-grey (the pasteboard, many +/// photos) it vanishes (#737), and the Linux themes' and macOS's differ. Use the same black-and-white +/// bitmap on every desktop platform, carried by the OS so its movement does not wait for the canvas +/// to render (#1343). +pub(super) fn visible_crosshair(icon: egui::CursorIcon, painter: &egui::Painter, p: Pos2, draw: bool) -> egui::CursorIcon { + if !draw || icon != egui::CursorIcon::Crosshair { + return icon; + } + crate::tool_cursor::crosshair(painter, p, 8.0, 2.0) +} + +/// The Pencil's cursor at `doc` (document pixels): the whole-pixel square its dab fills +/// (`paint::grid_square`), in screen points with its edges on physical pixels (`ppp` = pixels +/// per point), so it lines up with the pixel grid at any zoom. +pub(crate) fn pencil_cursor_rect(xf: &ViewXform, doc: [f64; 2], size: f32, ppp: f32) -> Rect { + let [x0, y0, x1, y1] = photocraft_engine::paint::grid_square(doc[0], doc[1], size); + let r = Rect::from_two_pos(xf.to_screen(x0 as f32, y0 as f32), xf.to_screen(x1 as f32, y1 as f32)); + let ppp = if ppp.is_finite() && ppp > 0.0 { ppp } else { 1.0 }; + let snap = |v: f32| (v * ppp).round() / ppp; + Rect::from_min_max(pos2(snap(r.min.x), snap(r.min.y)), pos2(snap(r.max.x), snap(r.max.y))) +} + +/// Whether an aliased (Pencil) dab of `size` pixels is round rather than its whole +/// [`pencil_cursor_rect`] square. The dab fills the pixels whose centres lie within its radius of +/// the grid centre (`paint::grid_center`), so up to 3 px the corner pixels are in and the dab is +/// a square; from 4 px on they fall outside and it is a disc (#2662). +pub(crate) fn pencil_tip_is_round(size: f32) -> bool { + let n = if size.is_finite() { size.round().clamp(1.0, 100_000.0) } else { 1.0 }; + let h = n / 2.0; + (h - 0.5) * std::f32::consts::SQRT_2 > h +} + +/// Whether a brush-tip circle draws its centre mark. +/// Clone Stamp and Healing Brush stay an empty circle until Option is held, the source-point +/// cursor. Other brushes follow Preferences › Cursors › Show Crosshair in Brush Tip, as in +/// Photoshop; the Background Eraser always shows its sampling hotspot. +pub(super) fn brush_tip_centre(tool: Tool, option: bool, show_crosshair: bool) -> bool { + if tool == Tool::QuickSelection { + return false; + } + if matches!(tool, Tool::CloneStamp | Tool::Healing) { + return option || show_crosshair; + } + show_crosshair || tool == Tool::BackgroundEraser +} + +/// The display colour of an RGB triple (0..1 per channel) as an egui colour. +fn display_color(c: [f32; 3]) -> Color32 { + let b = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; + Color32::from_rgb(b(c[0]), b(c[1]), b(c[2])) +} + +/// The Eyedropper ring's two colours at document point (x, y): the colour under the pointer +/// (what a click would pick) and the current foreground, or `None` where there is no colour +/// to sample (#213). +pub(crate) fn eyedropper_ring_colors(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<(Color32, Color32)> { + let new = eyedropper_color(app, x, y)?; + let fg = app.session.tools.foreground; + Some((display_color(new), display_color([fg[0], fg[1], fg[2]]))) +} + +/// Draw the Eyedropper's comparison ring at `p`: an opaque neutral grey band around the +/// pointer with the sampled colour on its upper half and the current one on the lower, +/// framed by thin dark and light hairlines so every edge reads on any image, and the +/// pipette at its centre marking the sampled pixel (#213, #6973). Like Photoshop it shows +/// only while the mouse button is held (`held`): hovering shows the plain pipette and +/// samples nothing. `None` when not held, there is nothing to sample, or the Precise-cursor +/// preference wants the plain crosshair. +pub(super) fn eyedropper_ring(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, p: Pos2, held: bool) -> Option { + // The options bar's Show Sampling Ring turns it off (#1649). + if !held || !app.ui.tool_options.eyedropper_ring || app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise { + return None; + } + let [x, y] = xf.to_doc(p); + let (new, current) = eyedropper_ring_colors(app, x, y)?; + // Photoshop's proportions (#6973): a ring about 210 pt across whose opaque grey outer + // band and coloured inner band are each about 17 pt wide, over a transparent centre. + // In points, so the ring scales with Interface › UI Scale like the rest of the cursor. + let (r_out, band) = (105.0_f32, 17.0_f32); + let r_in = r_out - 2.0 * band; + // Screen y grows down, so PI..TAU is the upper half (the sampled colour) and 0..PI the + // lower (the current one); the halves meet at 3 and 9 o'clock, as in Photoshop. + use std::f32::consts::{PI, TAU}; + let upper: Vec = (0..=48) + .map(|i| { + let a = PI + (TAU - PI) * i as f32 / 48.0; + p + vec2(a.cos(), a.sin()) * (r_in + band / 2.0) + }) + .collect(); + // `circle_stroke` paints its stroke outside the radius, `Shape::line` centres it. + painter.circle_stroke(p, r_in, Stroke::new(band, current)); + painter.add(egui::Shape::line(upper, Stroke::new(band, new))); + painter.circle_stroke(p, r_in + band, Stroke::new(band, RING_GREY)); + // Thin dark and light hairlines on the ring's two edges, one physical pixel wide, so it + // reads on any colour including a flat mid-grey that matches the band itself (#6973). + let px = 1.0 / painter.ctx().pixels_per_point().max(1.0); + let (dark, light) = (Stroke::new(px, Color32::from_black_alpha(160)), Stroke::new(px, Color32::from_white_alpha(160))); + painter.circle_stroke(p, r_out, dark); + painter.circle_stroke(p, r_out + px, light); + painter.circle_stroke(p, r_in - px, dark); + painter.circle_stroke(p, r_in - 2.0 * px, light); + // Photoshop keeps the pipette visible at the centre, its tip on the sampled pixel; a + // ring this size says nothing about where it samples without it. + Some(pipette_cursor(painter.ctx(), p)) +} + +/// The Eyedropper ring's opaque neutral grey, as Photoshop measures it (#6973). +const RING_GREY: Color32 = Color32::from_gray(127); + +/// Tools on which holding ⌥ (Alt) switches to the Eyedropper: a click or drag sets the +/// foreground colour, as with the Eyedropper itself (#417). +pub(crate) fn alt_samples(tool: Tool, mods: egui::Modifiers) -> bool { + // Control+Alt is the brush-resize drag (`brush_resize`), not sampling. + mods.alt && !mods.ctrl && matches!(tool, Tool::Brush | Tool::Pencil | Tool::Gradient | Tool::PaintBucket) +} + +/// Preferences › Cursors › Other Cursors asks for the precise crosshair, or Caps Lock swaps the +/// standard cursor for it (and back), as in Photoshop (#4639). +pub(super) fn precise_other_cursor(app: &PhotocraftApp) -> bool { + (app.session.prefs().cursors.other == photocraft_engine::prefs::OtherCursor::Precise) != app.caps_lock +} + +/// A pen tool's standard cursor: its toolbar icon with the nib (the Convert Point tool: the +/// anchor) on `p`. Draws it at `p` and returns the OS cursor to set (hidden). +pub(crate) fn pen_cursor(ctx: &egui::Context, tool: Tool, p: Pos2) -> egui::CursorIcon { + // Where each icon's point is in its 24-unit box: the full-size pen's nib at (2.3, 2.3); the + // Freeform and Curvature pens draw it at 0.68 × from (7, 7); Convert Point's apex anchor. + let hot = match tool { + Tool::FreeformPen | Tool::CurvaturePen => vec2(8.56, 8.56), + Tool::ConvertPoint => vec2(12.0, 7.0), + _ => vec2(2.3, 2.3), + }; + crate::icons::cursor(ctx, crate::icons::tool_icon(tool), p, hot / 24.0, 22.0); + egui::CursorIcon::None +} + +/// The sampling cursor: a pipette whose tip is the sampled pixel. Draws it at `p` and returns the +/// OS cursor to set (hidden). +pub(crate) fn pipette_cursor(ctx: &egui::Context, p: Pos2) -> egui::CursorIcon { + // The tip of the icon's pipette is at (2, 22) of its 24-unit box. + crate::icons::cursor(ctx, "pipette", p, vec2(2.0, 22.0) / 24.0, 20.0); + egui::CursorIcon::None +} + +/// The Paint Bucket's cursor: a bucket whose pouring corner is the filled pixel. Draws it at `p` +/// and returns the OS cursor to set (hidden). +pub(crate) fn paint_bucket_cursor(ctx: &egui::Context, p: Pos2) -> egui::CursorIcon { + // The left corner of the icon's bucket is at (2, 12) of its 24-unit box. + crate::icons::cursor(ctx, "paint-bucket", p, vec2(2.0, 12.0) / 24.0, 22.0); + egui::CursorIcon::None +} + +/// Decided when the press starts, so ⌥ pressed or released mid-stroke never switches between +/// painting and sampling. +pub(super) fn alt_eyedropper(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool { + if matches!(ev, ToolEvent::Down { .. }) { + let physical_right = app.services.right_button_down.as_ref().is_some_and(|f| f()); + app.alt_sampling = !physical_right && !app.secondary_erase && alt_samples(app.active_tool(), mods); + } + if !app.alt_sampling { + return false; + } + match ev { + // Without ⌥: the sample sets the foreground colour, whatever the Eyedropper would do. + ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, egui::Modifiers::NONE), + ToolEvent::Up { .. } => app.alt_sampling = false, + } + true +} + +pub(super) fn sample_eyedropper(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) { + if let Some(mut rgb) = eyedropper_color(app, x, y) { + if super::paint_target(app) != json!("pixels") { + rgb = [photocraft_color::convert::rgb_to_gray(rgb); 3]; + } + let key = if mods.alt { "background" } else { "foreground" }; + let _ = app.run("tools.setColors", json!({ key: [rgb[0],rgb[1],rgb[2],1.0] })); + } +} + +/// The colour at document point (x, y) with the Eyedropper's Sample Size (the composite pixel, +/// or the average of the square around it) from the layers `sample_layer` names (`document.sampleColor`). +/// `None` off the image or over transparency. +fn sample_color(app: &mut PhotocraftApp, x: f64, y: f64, sample_layer: &str) -> Option<[f32; 3]> { + if !(x.is_finite() && y.is_finite()) { + return None; + } + let size = app.ui.tool_options.eyedropper_size; + let v = app.run("document.sampleColor", json!({"x": x, "y": y, "size": size, "sampleLayer": sample_layer})).ok()?; + match serde_json::from_value::>(v).ok()?[..] { + [r, g, b, a] if a > 0.0 => Some([r, g, b]), + _ => None, + } +} + +/// What the Eyedropper tool (and a painting tool's ⌥-click) picks at document point (x, y): its +/// options bar's Sample Size and Sample (#1649). +pub(crate) fn eyedropper_color(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<[f32; 3]> { + let layers = app.ui.tool_options.eyedropper_sample.clone(); + sample_color(app, x, y, &layers) +} + +/// The active document's composite colour at document point (x, y), averaged over the +/// Eyedropper's Sample Size as Photoshop's dialog eyedroppers (Curves, Color Picker) do. +/// `None` off the image or over transparency. +pub(crate) fn composite_color(app: &mut PhotocraftApp, x: f64, y: f64) -> Option<[f32; 3]> { + sample_color(app, x, y, "all") +} + +pub(crate) fn alt_loads_mixer(tool: Tool, mods: egui::Modifiers) -> bool { + mods.alt && !mods.ctrl && tool == Tool::MixerBrush +} +pub(crate) fn alt_picks(tool: Tool, mods: egui::Modifiers) -> bool { + alt_samples(tool, mods) || alt_loads_mixer(tool, mods) +} diff --git a/crates/ui-egui/src/canvas/display.rs b/crates/ui-egui/src/canvas/display.rs new file mode 100644 index 0000000000..3b111232bd --- /dev/null +++ b/crates/ui-egui/src/canvas/display.rs @@ -0,0 +1,300 @@ +use super::strokes::live_stroke; +use super::{Color32, Document, LayerContent, PhotocraftApp, TextureOptions}; + +pub(super) fn checker(app: &mut PhotocraftApp, ctx: &egui::Context) -> egui::TextureId { + // Preferences › Transparency & Gamut colours (the texture is rebuilt when they change). + let [ca, cb] = app.session.prefs().transparency_and_gamut.colors(); + if app.prefs_rt.checker_key != Some([ca, cb]) { + app.prefs_rt.checker_key = Some([ca, cb]); + app.checker = None; + } + app.checker + .get_or_insert_with(|| { + let (a, b) = (Color32::from_rgb(ca[0], ca[1], ca[2]), Color32::from_rgb(cb[0], cb[1], cb[2])); + let img = egui::ColorImage::new([2, 2], vec![a, b, b, a]); + let opts = TextureOptions { + magnification: egui::TextureFilter::Nearest, + minification: egui::TextureFilter::Nearest, + wrap_mode: egui::TextureWrapMode::Repeat, + mipmap_mode: None, + }; + ctx.load_texture("checker", img, opts) + }) + .id() +} + +fn buffer_to_image(buf: &photocraft_compose::Buffer) -> egui::ColorImage { + // `buf.to_rgba8()` then `color_image`, fused: one parallel pass, no intermediate image. + let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8; + let mut pixels = vec![egui::Color32::TRANSPARENT; buf.px.len()]; + let convert = |out: &mut [egui::Color32], src: &[[f32; 4]]| { + for (o, p) in out.iter_mut().zip(src) { + *o = egui::Color32::from_rgba_unmultiplied(q(p[0]), q(p[1]), q(p[2]), q(p[3])); + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + pixels.par_chunks_mut(16 * 1024).zip(buf.px.par_chunks(16 * 1024)).for_each(|(o, s)| convert(o, s)); + } + #[cfg(target_arch = "wasm32")] + convert(&mut pixels, &buf.px); + egui::ColorImage::new([buf.rect.width() as usize, buf.rect.height() as usize], pixels) +} + +/// `img` as an egui image: `ColorImage::from_rgba_unmultiplied`, in parallel off wasm. egui's own +/// conversion runs on one thread and took 37 ms of a 24 MP canvas or preview update, three times +/// the quantize before it. +fn color_image(img: &photocraft_raster::Rgba8Image) -> egui::ColorImage { + let size = [img.width as usize, img.height as usize]; + let src = img.pixels.as_chunks::<4>().0; + let mut pixels = vec![egui::Color32::TRANSPARENT; src.len()]; + let convert = |out: &mut [egui::Color32], src: &[[u8; 4]]| { + for (o, &[r, g, b, a]) in out.iter_mut().zip(src) { + *o = egui::Color32::from_rgba_unmultiplied(r, g, b, a); + } + }; + #[cfg(not(target_arch = "wasm32"))] + { + use rayon::prelude::*; + pixels.par_chunks_mut(16 * 1024).zip(src.par_chunks(16 * 1024)).for_each(|(o, s)| convert(o, s)); + } + #[cfg(target_arch = "wasm32")] + convert(&mut pixels, src); + egui::ColorImage::new(size, pixels) +} + +/// A composite as monitor pixels: document profile → monitor profile (nothing to do in the +/// common sRGB-on-sRGB case). +pub(crate) fn display_image(display: Option<&photocraft_engine::display_color::CanvasDisplay>, buf: &photocraft_compose::Buffer) -> egui::ColorImage { + match display { + Some(d) if !d.is_identity() => color_image(&d.to_rgba8(buf)), + _ => buffer_to_image(buf), + } +} + +/// The canvas display of `doc` on `display` (`None`: the main window's) and a key that changes +/// with it (folded into the canvas caches' preview keys, so a monitor or profile change +/// re-renders). +pub(crate) fn canvas_display( + app: &PhotocraftApp, + doc: &Document, + display: Option, +) -> (Option>, u64) { + match app.session.color.canvas_display_for(doc, display.or(app.session.color.main_display)) { + Ok(d) => { + let k = d.key; + (Some(d), k) + } + Err(_) => (None, 0), + } +} + +/// [`canvas_display`]'s key for the GPU canvas texture: what the texture stores, the same on +/// every display (the display LUT holds the monitor profile). +pub(super) fn texture_key(display: Option<&photocraft_engine::display_color::CanvasDisplay>) -> u64 { + display.map_or(0, |d| d.texture_key) +} + +/// `app.canvases` key: CPU canvas textures are per document and display (they hold monitor +/// values); the GPU canvas state uses [`GPU_OUTPUT`]. +pub(crate) fn cache_key(doc: photocraft_doc::DocId, display: Option) -> (photocraft_doc::DocId, u32) { + (doc, display.unwrap_or(0)) +} + +/// `app.canvases` output of the GPU canvas state (one texture per document for all displays). +pub(crate) const GPU_OUTPUT: u32 = u32::MAX; + +/// The document to render: the committed one, or a clone with the live adjustment preview applied. +pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> (std::sync::Arc, u64) { + if let Some(shown) = crate::type_panels_ui::color_picker::display_doc(app, idx) { + return shown; + } + if let Some(shown) = crate::solid_fill_ui::display_doc(app, idx) { + return shown; + } + if let Some(shown) = crate::pattern_fill_ui::display_doc(app, idx) { + return shown; + } + if let Some(shown) = crate::shape_fill_ui::display_doc(app, idx) { + return shown; + } + if let Some(shown) = crate::type_transform::display_doc(app, idx) { + return shown; + } + if let Some(shown) = crate::type_font_preview::display_doc(app, idx) { + return shown; + } + // Vanishing Point's strokes over the document as it is now (the Layers panel may have + // changed it since the options bar synced them this frame). + crate::vp_ui::sync_preview(app); + // Puppet / Perspective Warp previews hide the layer they draw on a mesh. + if let Some(shown) = crate::distort_ui::display_doc(app, idx) { + return shown; + } + // Image › Adjustments dialog: a temporary adjustment layer clipped to the target. + if let Some(shown) = crate::adjust_preview::display_doc(app, idx) { + return shown; + } + // Gradient tool (live) drag, or a stop dragged in the Properties panel. + if let Some(shown) = crate::gradient_ui::display_doc(app, idx) { + return shown; + } + // Move tool drag: the moving layers at the pointer. + if let Some(shown) = crate::move_ui::display_doc(app, idx) { + return shown; + } + // Patch Tool drag: the selection healed from where the pointer is. + if let Some(shown) = crate::patch_preview::display_doc(app, idx) { + return shown; + } + // Content-Aware Move drag or box: the result, once computed in the background. It comes + // before the reorder preview, which is built from the session document and would drop it. + if let Some(shown) = crate::cam_preview::display_doc(app, idx) { + return shown; + } + // Live Layers-panel reorder previews the actual composite before mouse release. + if let Some(shown) = crate::layers_panel_ui::display_reorder(app, idx) { + return shown; + } + // A blend mode hovered in the Layers panel. + if let Some(shown) = crate::blend_preview::display_doc(app, idx) { + return shown; + } + crate::transform_tool::follow_edits(app); + let st = &app.session.documents()[idx]; + if let Some(l) = live_stroke(app, idx) { + return (l.stroke.doc().clone(), l.display_key()); + } + if let (Some(t), Some(pv)) = (&app.ui.transform, &app.transform_preview) + && app.session.active_index() == Some(idx) + && pv.doc.id == st.doc.id + && st.doc.layer(photocraft_doc::LayerId(t.layer)).is_some() + { + return (pv.doc.clone(), (1 << 40) + pv.session); + } + // Layer Style dialog: show its effects live (Cancel just drops the preview). + let style = app.ui.dialogs.iter().find(|d| d.kind == crate::state::DialogKind::LayerStyle); + // The Preview checkbox off shows the document as it was when the dialog opened; + // edits still land in the dialog state and OK applies them whatever it says. + let preview_on = style.and_then(|d| d.fields.get("preview")).and_then(serde_json::Value::as_bool).unwrap_or(true); + if style.is_none() || !preview_on { + app.style_preview = None; + } + if let Some(d) = style.filter(|_| preview_on) + && app.session.active_index() == Some(idx) + { + let fields = crate::layer_style::color_picker::preview_fields(app, d.id, &d.fields); + let key = (crate::layer_style::preview_hash(&fields) ^ st.revision.wrapping_mul(0x9e37_79b9_7f4a_7c15)) | 1 << 63; + if app.style_preview.as_ref().map(|p| p.0) != Some(key) { + let shown = crate::layer_style::preview_document(&st.doc, &app.session.patterns, &fields).map(std::sync::Arc::new); + if let Err(error) = &shown { + log::warn!("Layer Style preview: {error}"); + } + app.style_preview = Some((key, shown)); + } + if let Some((_, Ok(doc))) = &app.style_preview { + return (doc.clone(), key); + } + } + let revision = st.revision; + let snapshot = st.doc.clone(); + if let Some((layer, params)) = app.live_adjust.clone() + && let Some(l) = snapshot.layer(layer) + && let LayerContent::Adjustment(a) = &l.content + { + let key = crate::adjust_preview::live_key(app, &snapshot, revision, layer, ¶ms); + let shown = memo_display(app, &snapshot, key, || { + let kind = photocraft_engine::commands::adjustment_kind(a); + let preview = photocraft_engine::adjust_params::from_params(kind, ¶ms, Some(a), snapshot.mode).unwrap_or_else(|_| a.clone()); + let mut doc = (*snapshot).clone(); + if let Some(lm) = doc.layer_mut(layer) { + lm.content = LayerContent::Adjustment(preview); + } + Some(doc) + }); + if let Some(doc) = shown { + return (doc, key); + } + } + // Duotone documents display through their inks. + if let Some(shown) = memo_display(app, &snapshot, 1 << 41, || photocraft_engine::mode_cmds::display_document(&snapshot)) { + return (shown, 1 << 41); + } + (snapshot, 0) +} + +/// `display_doc`'s live-adjust and Duotone documents, built once per document snapshot and key +/// rather than on each of its 3–4 calls a frame (#4527): each build clones the whole document. +fn memo_display( + app: &mut PhotocraftApp, + snapshot: &std::sync::Arc, + key: u64, + build: impl FnOnce() -> Option, +) -> Option> { + let id = snapshot.id; + if let Some((doc, k, shown)) = app.display_memo.get(&id) + && *k == key + && doc.ptr_eq(&std::sync::Arc::downgrade(snapshot)) + { + return Some(shown.clone()); + } + let Some(shown) = build().map(std::sync::Arc::new) else { + app.display_memo.remove(&id); + return None; + }; + // Entries of closed documents go when another is stored. + app.display_memo.retain(|_, (doc, _, _)| doc.strong_count() > 0); + app.display_memo.insert(id, (std::sync::Arc::downgrade(snapshot), key, shown.clone())); + Some(shown) +} + +/// Lattice size of the canvas display LUT (colour management, Proof Colors, Gamut Warning). +const DISPLAY_LUT: usize = 33; + +/// Keep the GPU display LUT under `key` (the document's texture or a preview of it) in step with +/// `doc`'s colour management: document → monitor profile and View › Proof Colors / Gamut +/// Warning (the 32-bit preview is applied by the canvas shader, see [`hdr_preview`]). Returns the canvas `display` mode (0 none — the identity, e.g. sRGB on +/// an sRGB monitor —, 1 LUT, 2 LUT + gamut warning, 3 matrix/shaper). +pub(super) fn sync_display_lut(app: &mut PhotocraftApp, doc: &photocraft_doc::Document, key: u64, display: Option) -> u8 { + let Some(gpu) = app.gpu.clone() else { return 0 }; + let output = display.unwrap_or(0); + // Rebuild only when anything feeding the LUT changes. + let sig = app.session.color.display_signature_for(doc, display); + if let Some((s, mode)) = gpu.display_lut_signature(key, output) + && s == sig + { + return mode; + } + // Keep the curve/matrix/curve stages separate: interpolating a finished display + // transform on a 33³ lattice crushes shadows when the monitor curve is steep near zero. + if let Ok(Some(shaper)) = app.session.color.gpu_canvas_matrix_shaper_for(doc, display) { + gpu.set_display_matrix_shaper(key, output, &shaper); + gpu.cache_display_lut_signature(key, output, sig, 3); + return 3; + } + let gamut = app.session.color.proof(doc.id).gamut_warning; + let mode = match app.session.color.gpu_canvas_lut_for(doc, DISPLAY_LUT, display) { + Ok(Some(bytes)) => { + gpu.set_display_lut(key, output, DISPLAY_LUT as u32, Some(&bytes)); + if gamut { 2 } else { 1 } + } + Ok(None) => { + gpu.set_display_lut(key, output, DISPLAY_LUT as u32, None); + 0 + } + Err(e) => { + let template = "Color management: {error}"; + app.ui.status = crate::i18n::fmt(tl!(template), &[("error", &e.to_string())]); + gpu.set_display_lut(key, output, DISPLAY_LUT as u32, None); + 0 + } + }; + gpu.cache_display_lut_signature(key, output, sig, mode); + mode +} + +/// View › 32-bit Preview Options for the GPU canvas shader: (exposure, gamma) when active. +pub(super) fn hdr_preview(app: &PhotocraftApp, doc: &photocraft_doc::Document) -> Option<[f32; 2]> { + app.session.color.hdr_preview(doc).map(|h| [h.exposure, h.gamma]) +} diff --git a/crates/ui-egui/src/canvas_display_memo_tests.rs b/crates/ui-egui/src/canvas/display_memo_tests.rs similarity index 100% rename from crates/ui-egui/src/canvas_display_memo_tests.rs rename to crates/ui-egui/src/canvas/display_memo_tests.rs diff --git a/crates/ui-egui/src/canvas/drag.rs b/crates/ui-egui/src/canvas/drag.rs new file mode 100644 index 0000000000..ac3da2eacb --- /dev/null +++ b/crates/ui-egui/src/canvas/drag.rs @@ -0,0 +1,399 @@ +use super::view_xform::ViewXform; +use super::{PhotocraftApp, Pos2, Rect, Stroke, Tool, vec2}; + +/// An in-progress pointer gesture on the canvas. +#[derive(Clone, Debug)] +pub struct Drag { + pub tool: Tool, + pub start: [f64; 2], + pub points: Vec<[f64; 3]>, + pub modifiers: egui::Modifiers, + /// A Brush/Eraser stroke that erases: the Eraser, or a right-button Brush drag with + /// Preferences › Tools › Right-click with painting tools set to Erase. + pub erase: bool, + /// A Brush or Pencil stroke in Photoshop's Clear mode (`paint_mouse::clears`, #3439): it + /// erases, sent as `"mode": "clear"` so History names the tool rather than the Eraser. + pub clear: bool, + /// ⇧ constraint of a painting stroke (stroke_constraint.rs). + pub constrain: Option, + /// Modifiers held now (updated on every pointer event of the gesture). + pub live: egui::Modifiers, + /// Which of the modifiers held at the press have been released since. + pub released: egui::Modifiers, + /// The reposition key (Space) is held: pointer moves move the whole gesture instead of + /// sizing it (marquees, lasso, shapes; `hold_keys`). + pub reposition: bool, + /// A marquee or lasso drag that started inside the selection moves it instead of drawing: + /// `Some(false)` moves the outline, `Some(true)` moves the floating piece (`select.float`), as + /// every Move-tool drag with a selection does (`move_ui::moves_selected_pixels_with`). + pub sel_move: Option, + /// The last ⇧-locked offset of a selection-tool drag of the selection (`move_mods`, #6231); + /// keeps the locked direction stable between events. The Move tool's own drags use `move_mods`. + pub sel_axis: Option<[f64; 2]>, + pub lasso: Option, + /// Freeform Pen and Path Selection gestures belong to their source document and target. + pub(super) vector_owner: Option, + pub(super) pen_owner: Option, +} + +impl Drag { + pub fn new(tool: Tool, start: [f64; 2], points: Vec<[f64; 3]>, modifiers: egui::Modifiers, erase: bool) -> Self { + Self { + tool, + start, + points, + modifiers, + erase, + clear: false, + constrain: None, + live: modifiers, + released: egui::Modifiers::NONE, + reposition: false, + sel_move: None, + sel_axis: None, + lasso: None, + vector_owner: None, + pen_owner: None, + } + } + + /// Vector drags capture their owner at the press, the Freeform Pen also its destination, before + /// later ⌥-clicks or a temporary Direct Selection can finish them. + pub(crate) fn capture_vector_owners(&mut self, app: &PhotocraftApp) { + if matches!(self.tool, Tool::FreeformPen | Tool::PathSelection) { + self.vector_owner = VectorGestureOwner::capture(app); + } + if self.tool == Tool::FreeformPen { + self.pen_owner = crate::vector_ui::capture_pen_owner(app); + } + } + + /// Reposition: move the start and every point so the last one lands on `to` (same size). + pub fn shift_to(&mut self, to: [f64; 2]) { + let last = self.points.last().map_or(self.start, |p| [p[0], p[1]]); + let (dx, dy) = (to[0] - last[0], to[1] - last[1]); + if !(dx.is_finite() && dy.is_finite()) { + return; + } + self.start = [self.start[0] + dx, self.start[1] + dy]; + for p in &mut self.points { + p[0] += dx; + p[1] += dy; + } + if self.points.is_empty() { + self.points.push([to[0], to[1], 1.0]); + } + } + + /// Record the modifiers of a pointer event. + pub(crate) fn track(&mut self, mods: egui::Modifiers) { + self.live = mods; + self.released.shift |= !mods.shift; + self.released.alt |= !mods.alt; + } + + /// ⇧ (square / circle) and ⌥ (from the centre) for a marquee drag. A modifier held at the press + /// picks the selection mode instead (add / subtract, #188) until it is released and pressed + /// again, as in Photoshop. + pub fn marquee_mods(&self) -> (bool, bool) { + let fresh = |now: bool, at_press: bool, released: bool| now && (!at_press || released); + (fresh(self.live.shift, self.modifiers.shift, self.released.shift), fresh(self.live.alt, self.modifiers.alt, self.released.alt)) + } +} + +/// The marquee dragged from `d.start` to `end` as two document-space corners: the options-bar +/// style (fixed ratio / fixed size), ⇧ for a square or circle, ⌥ to draw from the centre. +pub fn marquee_corners(o: &crate::state::ToolOptions, d: &Drag, end: [f64; 2]) -> ([f64; 2], [f64; 2]) { + let (shift, alt) = d.marquee_mods(); + if o.marquee_style == "fixedSize" { + // A fixed box is positioned, not sized, by the drag. Using the press plus the drag's + // sign pinned it there and flipped it by its full width/height when the pointer crossed + // the press. The preview and release both follow the current pointer instead. + let (w, h) = (f64::from(o.marquee_width).max(1.0), f64::from(o.marquee_height).max(1.0)); + let a = if alt { [end[0] - w / 2.0, end[1] - h / 2.0] } else { end }; + return (a, [a[0] + w, a[1] + h]); + } + let e = crate::chrome_ui::marquee_end(&o.marquee_style, o.marquee_width as f64, o.marquee_height as f64, shift, d.start, end); + if !alt { + return (d.start, e); + } + let (dx, dy) = (e[0] - d.start[0], e[1] - d.start[1]); + // For a sized marquee the drag is the half extent when drawing from the centre. + ([d.start[0] - dx, d.start[1] - dy], [d.start[0] + dx, d.start[1] + dy]) +} + +/// The pixel rectangle a Rectangular/Elliptical Marquee drag selects so far: shown live (ants +/// and readout) exactly as the release commits it, on whole pixel edges as in Photoshop. `None` +/// while the gesture is still a click (it never left the press point by more than a few screen +/// pixels at `zoom`, points per document pixel) or the rectangle has no area: a release then +/// deselects. A one-pixel row or column is a selection (#2391). With the Fixed Size style a +/// click places the selection, as in Photoshop. +pub(crate) fn marquee_preview_px(o: &crate::state::ToolOptions, d: &Drag, zoom: f32) -> Option<[f64; 4]> { + let fixed = o.marquee_style == "fixedSize"; + if !matches!(d.tool, Tool::RectMarquee | Tool::EllipseMarquee) || (!fixed && crate::shape_dialog::is_click(d.start, &d.points, zoom)) { + return None; + } + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let (a, b) = marquee_corners(o, d, last); + let mut r = if fixed { fixed_size_px(a, b, o.marquee_width, o.marquee_height) } else { marquee_px(a, b) }; + if marquee_is_square(o, d) { + r = square_px(r, d.start, last); + } + (r[2] > r[0] && r[3] > r[1]).then_some(r) +} + +/// The pixel rectangle `[x0, y0, x1, y1]` a marquee between two corners selects. +pub fn marquee_px(a: [f64; 2], b: [f64; 2]) -> [f64; 4] { + [a[0].min(b[0]).floor(), a[1].min(b[1]).floor(), a[0].max(b[0]).ceil(), a[1].max(b[1]).ceil()] +} + +/// The pixel rectangle of a Fixed Size marquee between the corners `a` and `b`: exactly `w` × `h` +/// whole pixels. Snapping both edges outwards (`marquee_px`) added a pixel to each side whenever +/// the press point sat between pixels, so 10 × 10 selected 11 × 11 (#6015). The near edge goes +/// to the closest pixel edge and the far one is the size away. +pub fn fixed_size_px(a: [f64; 2], b: [f64; 2], w: f32, h: f32) -> [f64; 4] { + let (w, h) = (f64::from(w).round().max(1.0), f64::from(h).round().max(1.0)); + let (x0, y0) = (a[0].min(b[0]).round(), a[1].min(b[1]).round()); + [x0, y0, x0 + w, y0 + h] +} + +/// Whether the marquee drag `d` is constrained to a square (or circle): ⇧ during the drag with +/// the Normal style, or a Fixed Ratio of 1:1. Mirrors the branches of `chrome_ui::marquee_end`. +fn marquee_is_square(o: &crate::state::ToolOptions, d: &Drag) -> bool { + let (w, h) = (f64::from(o.marquee_width), f64::from(o.marquee_height)); + match o.marquee_style.as_str() { + "fixedSize" => false, + "fixedRatio" if w > 0.0 && h > 0.0 => w == h, + _ => d.marquee_mods().0, + } +} + +/// A constrained square `r` (`marquee_px` of its corners) with equal sides on whole pixels. +/// +/// The square is built in continuous coordinates, and snapping each axis to the pixel grid on +/// its own (floor the near edge, ceil the far one) made one side a pixel longer than the other +/// whenever the press point sat between pixels: a ⇧ drag read 101 × 102 (#5472). The shorter +/// side grows to the longer one, away from the press point (`start`, towards `end`), so the +/// square still covers every pixel the drag touched. +pub fn square_px(r: [f64; 4], start: [f64; 2], end: [f64; 2]) -> [f64; 4] { + let [x0, y0, x1, y1] = r; + let (w, h) = (x1 - x0, y1 - y0); + let n = w.max(h); + let grow = |lo: f64, hi: f64, extent: f64, from: f64, to: f64| { + let d = (n - extent).max(0.0); + if to < from { (lo - d, hi) } else { (lo, hi + d) } + }; + let (x0, x1) = grow(x0, x1, w, start[0], end[0]); + let (y0, y1) = grow(y0, y1, h, start[1], end[1]); + [x0, y0, x1, y1] +} + +/// The one-pixel line `[x0, y0, x1, y1]` a Single Row (Single Column) Marquee at `p` selects: the +/// pixel row (column) under `p`, kept on the canvas, across the whole `size`. `None` for other +/// tools, an empty canvas or a point that is not a number. +pub(crate) fn single_line_px(tool: Tool, size: [u32; 2], p: [f64; 2]) -> Option<[f64; 4]> { + let [w, h] = size.map(f64::from); + if w < 1.0 || h < 1.0 || !p[0].is_finite() || !p[1].is_finite() { + return None; + } + match tool { + Tool::SingleRowMarquee => { + let y = p[1].floor().clamp(0.0, h - 1.0); + Some([0.0, y, w, y + 1.0]) + } + Tool::SingleColumnMarquee => { + let x = p[0].floor().clamp(0.0, w - 1.0); + Some([x, 0.0, x + 1.0, h]) + } + _ => None, + } +} + +/// The line a Single Row / Single Column Marquee gesture `d` selects so far: it follows the +/// pointer until the release. +pub(super) fn single_line_drag_px(app: &PhotocraftApp, d: &Drag) -> Option<[f64; 4]> { + let st = app.session.active()?; + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + single_line_px(d.tool, [st.doc.size.width, st.doc.size.height], last) +} + +/// The marching ants of a marquee drag: its rectangle, or the ellipse inscribed in it, built in +/// document space and mapped point by point through the view, so the hinting lands where the +/// release commits the selection. +/// +/// A rotated view projects the rectangle onto a rotated one, and an axis-aligned screen box +/// through two of its corners is a different shape in a different place: the commit was always +/// right, only the hinting was drawn elsewhere (#2512). The points are not snapped: the ants snap +/// straight edges to device pixels as they draw them (`crate::ants`), like every other outline, so +/// the lines stay crisp at any zoom (#172) and display scale (#7489). +pub(crate) fn marquee_ants(xf: &ViewXform, tool: Tool, a: [f64; 2], b: [f64; 2]) -> Vec { + let (x0, x1) = (a[0].min(b[0]), a[0].max(b[0])); + let (y0, y1) = (a[1].min(b[1]), a[1].max(b[1])); + let at = |x: f64, y: f64| xf.to_screen(x as f32, y as f32); + if tool != Tool::EllipseMarquee { + return vec![at(x0, y0), at(x1, y0), at(x1, y1), at(x0, y1)]; + } + // An axis-aligned view (rotation a multiple of 90 degrees) maps the ellipse onto the one + // inscribed in the screen box of two opposite corners. The ants draw its vertices where they + // are: snapping each on its own turns the nearly flat arcs at the edge midpoints into 1 px + // stairs, whose antialiased steps wash the ants out to grey on white and black. + let quarter = xf.rotation.rem_euclid(90.0); + if quarter < 1e-4 || 90.0 - quarter < 1e-4 { + let r = Rect::from_two_pos(at(x0, y0), at(x1, y1)); + return crate::tool_feedback::ellipse_points(r); + } + // As many vertices as the same ellipse would get drawn on screen (`ellipse_points`). + let scr = Rect::from_points(&[xf.to_screen(x0 as f32, y0 as f32), xf.to_screen(x1 as f32, y1 as f32)]); + let n = (((scr.width() + scr.height()) * 0.25) as usize).clamp(16, 256); + let (cx, cy, rx, ry) = ((x0 + x1) * 0.5, (y0 + y1) * 0.5, (x1 - x0) * 0.5, (y1 - y0) * 0.5); + (0..n) + .map(|i| { + let t = i as f64 / n as f64 * std::f64::consts::TAU; + at(cx + rx * t.cos(), cy + ry * t.sin()) + }) + .collect() +} + +/// The ants of the marquee being drawn: `marquee` when the drag has one (the options-bar style +/// and the modifiers applied), the raw press and pointer corners otherwise. This is the whole of +/// what the hinting draws for the selection tools, so the test in `marquee_tests` checks it +/// against the selection the release commits (#2512). +pub(crate) fn marquee_drag_ants(d: &Drag, xf: &ViewXform, marquee: Option<[f64; 4]>) -> Vec { + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let (a, b) = marquee.map_or((d.start, last), |[x0, y0, x1, y1]| ([x0, y0], [x1, y1])); + marquee_ants(xf, d.tool, a, b) +} + +/// The size readout shown beside the cursor while dragging a marquee: the width and height values. +pub fn marquee_readout(r: [f64; 4]) -> [String; 2] { + [format!("{} px", r[2] - r[0]), format!("{} px", r[3] - r[1])] +} + +/// [`marquee_readout`] in the Units & Rulers unit, as Photoshop shows it (cm when the rulers are in +/// centimeters), from the active document's resolution; pixels without a document. +pub fn size_readout(app: &PhotocraftApp, r: [f64; 4]) -> [String; 2] { + let Some(d) = app.session.active() else { return marquee_readout(r) }; + let (dpi, size) = (f64::from(d.doc.resolution_dpi), d.doc.size); + readout_in(&app.session.prefs().units_and_rulers, dpi, [size.width as f64, size.height as f64], r) +} + +/// `r`'s width and height formatted in `ur`'s ruler unit at `dpi` (percent of `extent`). +pub fn readout_in(ur: &photocraft_engine::prefs::UnitsAndRulers, dpi: f64, extent: [f64; 2], r: [f64; 4]) -> [String; 2] { + let s = ur.rulers.suffix(); + [format!("{} {s}", ur.format(r[2] - r[0], dpi, extent[0])), format!("{} {s}", ur.format(r[3] - r[1], dpi, extent[1]))] +} + +/// Draw the marquee size readout below-right of the cursor (kept on screen), like Photoshop's: +/// two rows, `W:` / `H:` labels on the left and the values right-aligned. +pub(super) fn draw_marquee_readout(app: &PhotocraftApp, ctx: &egui::Context, cursor: Pos2, values: [String; 2]) { + draw_value_readout(app, ctx, "marquee-readout", cursor, ["W:", "H:"], values); +} + +/// A turn of `deg` degrees as the readout beside the pointer shows it, to 0.1° in (-180, 180]. +/// Rounded first so a hair below zero never reads "-0.0°" and a hair above -180 reads "180.0°". +pub(crate) fn angle_readout(deg: f64) -> String { + let d = (deg * 10.0).round() / 10.0; + let d = if d == 0.0 { + 0.0 + } else if d == -180.0 { + 180.0 + } else { + d + }; + format!("{d:.1}°") +} + +/// A transformation-values readout beside the pointer (a marquee's or the crop frame's size, an +/// angle), unless Preferences › Tools › Show Transformation Values is off, as in Photoshop (#204). +/// The brush HUD isn't one: Show Transformation Values doesn't hide it, so it calls [`draw_readout`]. +pub(crate) fn draw_value_readout(app: &PhotocraftApp, ctx: &egui::Context, id: &str, cursor: Pos2, labels: [&str; N], values: [String; N]) { + if app.session.prefs().tools.show_transformation_values { + draw_readout(ctx, id, cursor, labels, values); + } +} + +/// A readout of `N` rows beside the pointer (labels left, values right-aligned), above everything. +pub(crate) fn draw_readout(ctx: &egui::Context, id: &str, cursor: Pos2, labels: [&str; N], values: [String; N]) { + let t = crate::theme::Tokens::get(ctx); + let font = egui::FontId::proportional(11.5); + let width = |text: &str| ctx.fonts_mut(|f| f.layout_no_wrap(text.to_owned(), font.clone(), t.text).size().x); + let labels = labels.map(|label| tl!(label)); + let (lw, vw) = (labels.map(width), values.each_ref().map(|v| width(v))); + let (label_col, value_col) = (lw.iter().fold(0.0f32, |a, &b| a.max(b)), vw.iter().fold(0.0f32, |a, &b| a.max(b))); + egui::Area::new(egui::Id::unique(id)).order(egui::Order::Tooltip).fixed_pos(cursor + vec2(16.0, 18.0)).interactable(false).constrain(true).show( + ctx, + |ui| { + egui::Frame::new() + .fill(t.card) + .stroke(Stroke::new(1.0, t.card_border)) + .corner_radius(t.radius_sm) + .inner_margin(egui::Margin::symmetric(7, 4)) + .show(ui, |ui| { + ui.spacing_mut().item_spacing = vec2(0.0, 1.0); + for (i, value) in values.into_iter().enumerate() { + // Labels left, values right-aligned on one edge, sharing a baseline. + ui.horizontal(|ui| { + ui.add(egui::Label::new(egui::RichText::new(labels[i]).font(font.clone()).color(t.text_dim)).extend()); + ui.add_space(label_col - lw[i] + 12.0 + value_col - vw[i]); + ui.add(egui::Label::new(egui::RichText::new(value).font(font.clone()).color(t.text)).extend()); + }); + } + }); + }, + ); +} + +/// The document, revision and target a vector drag started on. +#[derive(Clone, Debug)] +pub(super) struct VectorGestureOwner { + doc: photocraft_doc::DocId, + document: std::sync::Weak, + revision: u64, + layer: Option, + selected_path: Option, + vector_mask: bool, + mode: String, + tool: Tool, +} + +/// Cancel a vector drag before it can preview or finish on another document or target; the rest of +/// its pointer events go nowhere. True when one was cancelled. +pub(crate) fn cancel_stale_vector_drag(app: &mut PhotocraftApp) -> bool { + let stale = app.drag.as_ref().and_then(|d| d.vector_owner.as_ref()).is_some_and(|owner| !owner.valid(app)); + if stale { + app.drag = None; + app.cancelled_vector_drag = true; + } + stale +} + +impl VectorGestureOwner { + fn capture(app: &PhotocraftApp) -> Option { + let st = app.session.active()?; + Some(Self { + doc: st.doc.id, + document: std::sync::Arc::downgrade(&st.doc), + revision: st.revision, + layer: st.active_layer, + selected_path: app.ui.selected_path.clone(), + vector_mask: app.ui.vector_mask_target, + mode: app.ui.tool_options.vector_mode.clone(), + tool: app.active_tool(), + }) + } + + pub(super) fn valid(&self, app: &PhotocraftApp) -> bool { + Self::capture(app).is_some_and(|now| { + self.doc == now.doc + && self.document.ptr_eq(&now.document) + && self.revision == now.revision + && self.layer == now.layer + && self.selected_path == now.selected_path + && self.vector_mask == now.vector_mask + && self.mode == now.mode + && self.tool == now.tool + }) + } +} + +use photocraft_doc::Document; diff --git a/crates/ui-egui/src/canvas/filter_preview.rs b/crates/ui-egui/src/canvas/filter_preview.rs new file mode 100644 index 0000000000..a7d3f8dc94 --- /dev/null +++ b/crates/ui-egui/src/canvas/filter_preview.rs @@ -0,0 +1,308 @@ +use super::display::canvas_display; +use super::textures::texture_buffer; +use super::{Document, LayerContent, PhotocraftApp}; + +/// The proxy factor of a live preview of filter `cmd` with `params` on `doc`, the active document: +/// reduced on a large document (Preferences › Performance › Low Resolution Previews), except for +/// filters whose features don't scale with a proxy (#2063), and never coarser than the active view, +/// so at 100 % the preview shows every pixel instead of k-pixel blocks (#6326, as adjustment +/// previews have since #5764). Zoomed out, the proxy keeps previews of large documents fast. +/// +/// The in-dialog preview window ([`crate::filter_preview_ui`]) sizes its own textures from this +/// too, so the canvas and the window never disagree about the factor. +/// +/// Full resolution costs a whole filter per parameter change, which only the background worker +/// absorbs. The web build has no worker and computes the preview inline on the UI thread (#1676), +/// where that would freeze the page: there the document's proxy stands. The test and +/// `PHOTOCRAFT_INLINE_JOBS` inline paths keep the cap deliberately, because `background_jobs` is +/// flipped mid-session (a shell save during `actions.play`): a factor that followed it would change +/// [`crate::filter_dialog::FilterPreviewKey`] under a finished preview and recompute it inline, +/// the very stall this avoids. +pub(crate) fn filter_preview_factor(app: &PhotocraftApp, doc: &Document, cmd: &str, params: &serde_json::Value) -> u32 { + // A smart filter edit re-renders the whole stack: every stage constrains the factor. + let k = crate::filter_dialog::smart_preview_factor(doc, cmd, params, crate::proxy::preview_factor(doc, crate::proxy::reduced_previews(app))); + if cfg!(target_arch = "wasm32") { k } else { crate::proxy::view_factor(k, app.current_zoom()) } +} + +/// Live preview for an open filter dialog: run the filter on the proxy and upload it. With +/// background jobs (the desktop app) it computes on one background worker +/// (`filter_preview_worker`); otherwise (web, tests, `PHOTOCRAFT_INLINE_JOBS`) it keeps the +/// deterministic inline path (#1676), which also lets headless/CPU tests inspect previews +/// without a GPU texture. +pub(crate) fn ensure_filter_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Context) -> Option<(u32, u64, [u32; 2])> { + if let Some(held) = committed_filter_preview(app, idx) { + return held; + } + // Command dialogs always edit the active document. Never show their preview in another tab. + if app.session.active_index() != Some(idx) { + return None; + } + if crate::adjust_preview::on_layer(app, idx) { + return None; + } + let d = app.ui.dialogs.iter().find(|d| d.fields.contains_key("__filter"))?; + if d.fields.get("__preview").and_then(serde_json::Value::as_bool) != Some(true) { + return None; + } + let cmd = d.fields.get("__command")?.as_str()?.to_string(); + // Previews edit what the command will: a targeted layer mask included (#780). + let (cmd, mut params) = if d.fields.contains_key("__smartFilter") { + crate::smart_filter_dialog::edit_command(app, &d.fields).ok()? + } else { + (cmd.clone(), app.with_mask_target(&cmd, crate::filter_dialog::params_of(&d.fields))) + }; + // Select and Mask previews in its view mode; holding X shows the image without one. + if crate::select_mask_view::owns(&d.fields) { + if crate::select_mask_view::hides_view(&d.fields) { + return None; + } + params = crate::select_mask_view::request_params(&d.fields, params); + } + let st = app.session.documents().get(idx)?; + let k = filter_preview_factor(app, &st.doc, &cmd, ¶ms); + let request = crate::filter_dialog::FilterPreviewKey { + doc: st.doc.id, + revision: st.revision, + dialog: d.id, + active: st.active_layer, + target: st.channel_view.target, + command: cmd, + params, + k, + }; + let key = request.doc.0 ^ (1u64 << 61); + // Every filter's preview computes off the UI thread in the desktop app: Gaussian Blur at + // 1000 px takes long enough to freeze the window. Once the slider settles, a preview still + // running for an older value is cancelled. + #[cfg(not(target_arch = "wasm32"))] + if app.background_jobs { + app.filter_preview_worker.supersede(&request, crate::gpu_canvas::now_ms()); + if let Some((finished, computed)) = app.filter_preview_worker.poll() { + match computed { + // The dialog still holds these exact parameters. Publish as before, including an + // empty result: that is how a filter which no longer applies clears the canvas. + Ok(computed) if finished == request => publish_filter_preview(app, idx, finished, computed.result, computed.ms), + // Mid-drag, a frame for the value the pointer has just left is what makes the + // canvas follow the drag. Three things have to hold for it: it belongs to this + // preview, it has a frame to show (an empty one would blank a good preview), and + // it must not displace a frame that is already exact for the dialog, which is + // what a slider dragged out and back would otherwise make the canvas do. + Ok(computed) + if finished.same_target(&request) && computed.result.is_some() && !app.filter_preview.as_ref().is_some_and(|p| p.key == request) => + { + publish_filter_preview(app, idx, finished, computed.result, computed.ms); + } + Ok(_) => {} + // Errors stay on the exact request: a value the pointer has already dragged past + // must not raise a notice, and a still one will raise it when it is recomputed. + Err(error) if finished == request => { + app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: finished, result: None, committing: false }); + crate::notices::error(app, error); + } + Err(_) => {} + } + } + let fresh = app.filter_preview.as_ref().is_some_and(|p| p.key == request); + if !fresh && !app.filter_preview_worker.busy() { + let doc = app.session.documents().get(idx)?.doc.clone(); + let documents: Vec<_> = app.session.documents().iter().map(|d| d.doc.clone()).collect(); + let color = app.session.color.settings.clone(); + let task = request.clone(); + if let Err(error) = app.filter_preview_worker.start(request.clone(), ctx.clone(), move |cancel| { + let t0 = crate::gpu_canvas::now_ms(); + let result = crate::filter_dialog::preview_document_for_target( + &doc, + task.active, + &task.command, + &task.params, + task.k, + Some(cancel), + task.target, + &documents, + &color, + ) + .map(|doc| { + let buffer = photocraft_compose::flatten(&doc); + (std::sync::Arc::new(doc), buffer) + }); + crate::dialogs::filter_preview_worker::Computed { result, ms: crate::gpu_canvas::now_ms() - t0 } + }) { + app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request.clone(), result: None, committing: false }); + crate::notices::error(app, error); + } + } + if app.filter_preview_worker.busy() { + ctx.request_repaint_after(std::time::Duration::from_millis(16)); + } + let preview = app.filter_preview.as_ref()?; + // Keep the last preview during a drag, but never one from another source or dialog. + if !preview.key.same_target(&request) { + return None; + } + let result = preview.result.as_ref()?; + return Some((preview.key.k, key, [result.size.width, result.size.height])); + } + // Web and explicitly inline sessions keep the deterministic synchronous path (#1676): + // computed inline, uploaded when a GPU texture exists, still inspectable without one. + // Flattened once, as the background worker does; `publish_filter_preview` uploads it (#7490). + let _ = ctx; + if app.filter_preview.as_ref().is_none_or(|p| p.key != request) { + let doc = app.session.documents().get(idx)?.doc.clone(); + let documents: Vec<_> = app.session.documents().iter().map(|d| d.doc.clone()).collect(); + let t0 = crate::gpu_canvas::now_ms(); + let computed = crate::filter_dialog::preview_document_for_target( + &doc, + request.active, + &request.command, + &request.params, + request.k, + None, + request.target, + &documents, + &app.session.color.settings, + ) + .map(|doc| { + let buffer = photocraft_compose::flatten(&doc); + (std::sync::Arc::new(doc), buffer) + }); + publish_filter_preview(app, idx, request, computed, crate::gpu_canvas::now_ms() - t0); + } + let preview = app.filter_preview.as_ref()?; + let result = preview.result.as_ref()?; + Some((preview.key.k, key, [result.size.width, result.size.height])) +} + +/// Channel overlays must use the filter's current proxy too, including on the CPU canvas. +/// The cache key changes with the completed request; unchanged frames reuse the overlay texture. +pub(crate) fn filter_channel_preview(app: &mut PhotocraftApp, idx: usize, ctx: &egui::Context) -> Option<(std::sync::Arc, u64)> { + use std::hash::{Hash, Hasher}; + ensure_filter_preview(app, idx, ctx)?; + let preview = app.filter_preview.as_ref()?; + let doc = preview.result.as_ref()?.clone(); + let mut key = std::collections::hash_map::DefaultHasher::new(); + format!("{:?}", preview.key).hash(&mut key); + Some((doc, key.finish() | (1 << 59))) +} + +fn publish_filter_preview( + app: &mut PhotocraftApp, + idx: usize, + request: crate::filter_dialog::FilterPreviewKey, + computed: Option<(std::sync::Arc, photocraft_compose::Buffer)>, + ms: f64, +) { + let result = computed.and_then(|(result, buffer)| { + let t0 = crate::gpu_canvas::now_ms(); + let doc = app.session.documents().get(idx)?.doc.clone(); + // Shown through the preview's own colour management, as the result will be: Assign and + // Convert to Profile change the profile (and Convert the mode), not just the pixels. + let (display, _) = canvas_display(app, &result, None); + // A GPU texture is an optimisation: headless/CPU tests must still see the preview. + if let Some(gpu) = app.gpu.as_ref() { + gpu.upload_buffer_full(request.doc.0 ^ (1u64 << 61), &texture_buffer(display.as_deref(), &buffer), doc.depth); + } + app.perf.record("filter-preview", result.size.area(), ms, crate::gpu_canvas::now_ms() - t0); + Some(result) + }); + app.filter_preview = Some(crate::filter_dialog::FilterPreview { key: request, result, committing: false }); +} + +/// The document whose colour management the canvas texture under `key` is shown through: a filter +/// preview's own result (it keeps the document's id, so Proof Colors still apply), else `doc`. +/// Assign Profile changes only the profile, so through `doc`'s its preview looked unchanged. +pub(super) fn filter_preview_display_doc(app: &PhotocraftApp, doc: &std::sync::Arc, key: u64) -> std::sync::Arc { + if key != doc.id.0 ^ (1u64 << 61) { + return doc.clone(); + } + app.filter_preview.as_ref().and_then(|p| p.result.clone()).filter(|r| r.id == doc.id).unwrap_or_else(|| doc.clone()) +} + +/// After a filter dialog's OK: `Some(shown)` while its preview stands in for the background job +/// that commits it (the document unchanged and the job still running, for this document's views), +/// `None` otherwise (other documents' views included). A job that lands, fails or is cancelled +/// drops it. +pub(crate) fn committed_filter_preview(app: &mut PhotocraftApp, idx: usize) -> Option> { + let p = app.filter_preview.as_ref().filter(|p| p.committing)?; + let st = app.session.documents().get(idx)?; + if st.doc.id != p.key.doc { + return None; + } + let size = p.result.as_ref().map(|r| [r.size.width, r.size.height]); + if let Some(size) = size + && st.revision == p.key.revision + && app.session.job_on(p.key.doc).is_some() + { + return Some(Some((p.key.k, p.key.doc.0 ^ (1u64 << 61), size))); + } + app.filter_preview = None; + Some(None) +} + +/// Zoomed-out Image › Adjustments preview on a large document: the wgpu compositor renders the +/// reduced preview document (`adjust_preview::gpu_proxy`) into its own texture, over the target's +/// area after the first frame. Returns (factor, gpu key, dimensions) to draw. +pub(super) fn ensure_adjust_proxy(app: &mut PhotocraftApp, idx: usize, zoom: f32) -> Option<(u32, u64, [u32; 2])> { + let frame = crate::adjust_preview::gpu_proxy(app, idx, zoom)?; + let key = frame.doc.id.0; + let size = [frame.doc.size.width, frame.doc.size.height]; + let gpu = app.gpu.clone()?; + if frame.stale || !gpu.has(key, size) { + let doc = app.session.documents().get(idx)?.doc.clone(); + let (display, _) = canvas_display(app, &doc, None); + let r = gpu.refresh(key, &frame.doc, frame.damage, display.as_deref()); + app.perf.record(if r.kind.starts_with("gpu") { "gpu-adjust-proxy" } else { "adjust-proxy" }, r.px, r.composite_ms, r.upload_ms); + if let Some(t) = r.gpu { + app.perf.record_gpu(t); + } + crate::adjust_preview::proxy_drawn(app, &frame); + } + Some((frame.k, key, size)) +} + +/// If a live adjustment preview is active on a large document, composite it on the proxy and upload +/// it under its own GPU key. Returns (factor, gpu key, dimensions) when the proxy should be drawn. +/// `zoom` is the view's (physical screen pixels per document pixel). +pub(super) fn ensure_proxy_preview(app: &mut PhotocraftApp, idx: usize, zoom: f32) -> Option<(u32, u64, [u32; 2])> { + let (layer, params) = app.live_adjust.clone()?; + let (doc_id, revision, doc) = { + let st = app.session.documents().get(idx)?; + (st.doc.id, st.revision, st.doc.clone()) + }; + // Low-resolution previews off: k is 1 and the canvas shows `display_doc` (full size). + let mut k = crate::proxy::preview_factor(&doc, crate::proxy::reduced_previews(app)); + // Zoomed in, a proxy coarser than the screen showed the drag in k-pixel blocks (#5764): when + // the wgpu compositor draws the full-size document, use a proxy only as coarse as the screen + // (none at 50 % and closer). Otherwise the full size would be a CPU composite per change. + let fine = crate::proxy::view_factor(k, zoom); + if fine < k && app.gpu.as_ref().is_some_and(|g| g.supports(&doc)) { + k = fine; + } + if k <= 1 { + return None; + } + let fresh = matches!(&app.proxy, Some((d, r, kk, _)) if *d == doc_id && *r == revision && *kk == k); + if !fresh { + app.proxy = Some((doc_id, revision, k, std::sync::Arc::new(crate::proxy::proxy_document(&doc, k)))); + } + let proxy = app.proxy.as_ref()?.3.clone(); + let key = doc_id.0 ^ (1u64 << 62); + let hash = params.to_string().bytes().fold(k as u64, |h, b| h.wrapping_mul(31).wrapping_add(b as u64)); + if app.proxy_uploaded != Some((doc_id, hash)) { + let l = proxy.layer(layer)?; + let LayerContent::Adjustment(a) = &l.content else { return None }; + let kind = photocraft_engine::commands::adjustment_kind(a); + let preview = photocraft_engine::adjust_params::from_params(kind, ¶ms, Some(a), proxy.mode).unwrap_or_else(|_| a.clone()); + let mut p = (*proxy).clone(); + if let Some(lm) = p.layer_mut(layer) { + lm.content = LayerContent::Adjustment(preview); + } + let t0 = crate::gpu_canvas::now_ms(); + let buf = photocraft_compose::flatten(&p); + let t1 = crate::gpu_canvas::now_ms(); + let (display, _) = canvas_display(app, &doc, None); + app.gpu.as_ref()?.upload_buffer_full(key, &texture_buffer(display.as_deref(), &buf), doc.depth); + app.perf.record("proxy", p.size.area(), t1 - t0, crate::gpu_canvas::now_ms() - t1); + app.proxy_uploaded = Some((doc_id, hash)); + } + Some((k, key, [proxy.size.width, proxy.size.height])) +} diff --git a/crates/ui-egui/src/canvas/gesture.rs b/crates/ui-egui/src/canvas/gesture.rs new file mode 100644 index 0000000000..6e4e015d9f --- /dev/null +++ b/crates/ui-egui/src/canvas/gesture.rs @@ -0,0 +1,297 @@ +use super::drag::{Drag, marquee_preview_px, single_line_drag_px}; +use super::selection::finish_selection_drag; +use super::strokes::{ + EngineStroke, commit_brush_stroke, feed_live_stroke, live_retouch_command, offer_live_retouch, paint_target, snap_aliased_tip, stroke_params, +}; +use super::textures::shown_as_document; +use super::{PhotocraftApp, Tool, json}; + +pub(crate) fn finish_gesture(app: &mut PhotocraftApp, d: Drag) { + // Control can select a different tool and release before the next frame cancels the drag. + // Cancel before even recording a Shift-stroke anchor, including the preview fallback paths. + if d.tool == Tool::MixerBrush && app.ui.tool != Tool::MixerBrush { + app.live_stroke = None; + return; + } + if d.vector_owner.as_ref().is_some_and(|owner| !owner.valid(app)) { + return; + } + let mut end = d.points.last().copied().unwrap_or([d.start[0], d.start[1], 1.0]); + let (sx, sy) = snap_aliased_tip(app, d.tool, end[0], end[1]); + end[0] = sx; + end[1] = sy; + if let Some(floating) = d.sel_move { + finish_selection_drag(app, floating, d.start, [end[0], end[1]]); + return; + } + // Where the next ⇧-click line starts. + if crate::stroke_constraint::connects(d.tool) + && let Some(st) = app.session.active() + { + app.last_stroke_end = Some((st.doc.id, end)); + } + if d.tool == Tool::MixerBrush { + // The release can carry a new endpoint, including moves deferred in this frame. + // Feed those samples before offering the already-rendered document for commit. + app.drag = Some(d); + let deferred = std::mem::replace(&mut app.defer_live_stroke, false); + feed_live_stroke(app); + app.defer_live_stroke = deferred; + let Some(d) = app.drag.take() else { return }; + let live = app.live_stroke.take(); + if let Some(l) = live { + let (doc, key, steps) = (l.doc, l.key, l.damage.len() as u64); + if let EngineStroke::Mixer(mixer) = l.stroke { + if l.preview_failed { + let mut p = stroke_params(app, &d, &app.stylus.stroke_points(&d.points)); + p["seed"] = json!(mixer.seed); + drop(mixer); + let _ = app.run("paint.mixerBrush", p); + return; + } + match mixer.prepare_commit(&mut app.session) { + Ok(p) => { + if app.run("paint.mixerBrush", p).is_ok() { + shown_as_document(app, doc, |k| k == 0 || (key..=key + steps).contains(&k)); + } + } + Err(e) => { + app.ui.status = e.to_string(); + app.ui.status_error = true; + } + } + } + } else { + // No preview (a tip above its size): render the whole stroke now. + let p = stroke_params(app, &d, &app.stylus.stroke_points(&d.points)); + let _ = app.run("paint.mixerBrush", p); + } + return; + } + // A live retouching preview ends here; the commit below replaces it (the Clone Stamp's, + // History Brush's and sequential-dab tools' with the pixels it already drew). + offer_live_retouch(app, &d); + if live_retouch_command(d.tool).is_some() || crate::retouch_ui::dab_params(app, d.tool).is_some() || d.tool == Tool::ColorReplacement { + app.live_stroke = None; + } + let finished = crate::eraser_ui::finish_stroke(app, d.tool, &d.points) || crate::retouch_ui::finish_stroke(app, d.tool, &d.points, d.modifiers); + app.session.clear_live_stroke(); + if finished { + return; + } + match d.tool { + Tool::ObjectSelection => crate::retouch_ui::finish_object_selection(app, d.start, [end[0], end[1]], d.modifiers), + Tool::FreeformPen => { + if let Some(owner) = d.pen_owner { + crate::vector_ui::freeform_finish_owned(app, &d.points, d.lasso.as_ref().map_or(&[], |l| l.straight()), owner); + } + } + // Photoshop: a click without dragging opens Create Rectangle / Ellipse / … instead. + t if crate::vector_ui::is_shape_tool(t) + && crate::shape_dialog::is_click(d.start, &d.points, app.point_zoom()) + && crate::shape_dialog::open(app, t, d.start).is_some() => {} + t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::finish_shape(app, t, d.start, [end[0], end[1]], d.live), + Tool::PathSelection => crate::vector_ui::path_selection_finish(app, d.start, [end[0], end[1]]), + Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => crate::type_tool::pointer_up(app, d.start, [end[0], end[1]]), + Tool::Brush | Tool::Pencil | Tool::Eraser => commit_brush_stroke(app, &d), + Tool::RectMarquee | Tool::EllipseMarquee => { + // Exactly what the drag showed (`marquee_preview_px`); a click deselects. + let Some([x0, y0, x1, y1]) = marquee_preview_px(&app.ui.tool_options, &d, app.point_zoom()) else { + if app.session.is_enabled("select.deselect") { + let _ = app.run("select.deselect", json!({})); + } + return; + }; + let mode = selection_mode(app, d.modifiers); + let (aa, feather) = (app.ui.tool_options.anti_alias, app.ui.tool_options.feather); + let _ = app.run("select.rect", json!({"x": x0, "y": y0, "width": x1 - x0, "height": y1 - y0, "mode": mode, "ellipse": d.tool == Tool::EllipseMarquee, "antiAlias": aa, "feather": feather})); + } + // A click (or the end of a drag) selects the line under the pointer, never deselects. + Tool::SingleRowMarquee | Tool::SingleColumnMarquee => { + let Some([x0, y0, x1, y1]) = single_line_drag_px(app, &d) else { return }; + let mode = selection_mode(app, d.modifiers); + let feather = app.ui.tool_options.feather; + // `line` names the History step after the tool (Single Row / Single Column Marquee). + let line = if d.tool == Tool::SingleRowMarquee { "row" } else { "column" }; + let _ = app.run( + "select.rect", + json!({"x": x0, "y": y0, "width": x1 - x0, "height": y1 - y0, "mode": mode, "line": line, "antiAlias": false, "feather": feather}), + ); + } + Tool::Patch if crate::retouch_ui::patch_drags_selection(app, d.start, d.modifiers) => crate::retouch_ui::finish_patch(app, d.start, [end[0], end[1]]), + Tool::ContentAwareMove if crate::retouch_ui::patch_drags_selection(app, d.start, d.modifiers) => { + crate::retouch_ui::finish_content_aware_move(app, d.start, [end[0], end[1]]) + } + Tool::Lasso | Tool::Patch | Tool::ContentAwareMove => { + let pts: Vec<[f64; 2]> = d.points.iter().map(|p| [p[0], p[1]]).collect(); + if pts.len() >= 3 { + let mode = selection_mode(app, d.modifiers); + let o = &app.ui.tool_options; + // The Patch and Content-Aware Move tools have no Feather in their options bar. + let feather = if d.tool == Tool::Lasso { o.feather } else { 0.0 }; + let made = app.run("select.lasso", json!({"points": pts, "mode": mode, "antiAlias": o.anti_alias, "feather": feather})); + // The lasso only outlines the patch; say that it is dragged next (#1715). + if made.is_ok() && d.tool != Tool::Lasso { + crate::retouch_ui::patch_hint(app); + } + } else if app.session.is_enabled("select.deselect") { + let _ = app.run("select.deselect", json!({})); + } + } + Tool::Gradient if (end[0] - d.start[0]).abs() + (end[1] - d.start[1]).abs() >= 2.0 => { + let o = app.ui.tool_options.clone(); + let _ = app.run( + "paint.gradient", + json!({"from": [d.start[0], d.start[1]], "to": [end[0], end[1]], "style": o.gradient_style, "reverse": o.gradient_reverse, "dither": o.gradient_dither, "opacity": o.fill_opacity, "mode": o.gradient_blend_mode.label(), "target": paint_target(app)}), + ); + } + Tool::Move => { + let (dx, dy) = ((end[0] - d.start[0]).round(), (end[1] - d.start[1]).round()); + crate::move_ui::finish(app, dx, dy); + } + _ => {} + } +} + +/// Selection mode from the options bar, overridden by modifier keys (⇧ add, ⌥ subtract, ⇧⌥ intersect). +/// The cursor badge announces the same mode (`tool_feedback`). +pub(crate) fn selection_mode(app: &PhotocraftApp, m: egui::Modifiers) -> &'static str { + crate::tool_feedback::document_selection_mode(app, Tool::Lasso, m) +} + +/// Shift constrains a Polygonal Lasso segment to Photoshop's 45° directions. +/// Work in document coordinates so zoom, view rotation and mirroring cannot skew it. +pub(crate) fn constrained_polygon_point(from: [f64; 2], point: [f64; 2]) -> [f64; 2] { + let dx = point[0] - from[0]; + let dy = point[1] - from[1]; + if !dx.is_finite() || !dy.is_finite() { + return point; + } + let length = dx.hypot(dy); + if length == 0.0 { + return point; + } + let angle = (dy.atan2(dx) / std::f64::consts::FRAC_PI_4).round() * std::f64::consts::FRAC_PI_4; + [from[0] + length * angle.cos(), from[1] + length * angle.sin()] +} + +/// A polygonal lasso click adds a vertex; clicking near the first vertex closes the polygon. The +/// first click fixes the selection mode; a new-selection polygon hides the old outline while it is +/// drawn, and replaces it in one history step when it closes. +pub(super) fn polygon_click(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) { + polygon_cancel_stale(app); + let close = app + .ui + .polygon + .first() + .is_some_and(|p0| app.ui.polygon.len() >= 3 && ((p0[0] - x).powi(2) + (p0[1] - y).powi(2)).sqrt() < 8.0 / app.point_zoom().max(0.01) as f64); + if close { + commit_polygon(app); + return; + } + if app.ui.polygon.is_empty() { + app.ui.polygon_doc = app.session.active().map(|st| st.doc.id); + // ⌥ with nothing selected draws freehand; there is nothing to subtract from. + let nothing_selected = app.session.active().is_none_or(|st| st.doc.selection.is_none()); + let intent = if nothing_selected { egui::Modifiers { alt: false, ..mods } } else { mods }; + app.ui.polygon_mode = selection_mode(app, intent).into(); + } + // Only constrain segments after the first vertex; the first click still sets selection intent. + // Alt-drag is freehand and must not snap to an angle. + let point = if mods.shift && !mods.alt { app.ui.polygon.last().copied().map_or([x, y], |last| constrained_polygon_point(last, [x, y])) } else { [x, y] }; + app.ui.polygon.push(point); +} + +/// Remove the Polygonal Lasso's last vertex (⌫, Delete or a right-click while drawing, #1229); +/// removing the only one cancels the polygon. False when no polygon is being drawn. +pub fn polygon_retract(app: &mut PhotocraftApp) -> bool { + if app.ui.polygon.pop().is_none() { + return false; + } + if app.ui.polygon.is_empty() { + app.ui.polygon_mode.clear(); + app.ui.polygon_doc = None; + } + true +} + +/// The polygon in progress belongs to the document it was started on (`UiState::polygon_doc`): +/// once another document becomes active it is dropped, so its outline doesn't show on (or get +/// committed to) the other document, and the selection it would have replaced is shown again. +/// Runs before canvas input and shortcuts as well as in `PhotocraftApp::sync_views`. Like the Lasso's +/// ([`crate::lasso_ui::cancel_stale`]) and the Magnetic Lasso's ([`crate::magnetic_lasso_ui::frame`]). +pub fn polygon_cancel_stale(app: &mut PhotocraftApp) { + let active = app.session.active().map(|st| st.doc.id); + if app.ui.polygon.is_empty() { + app.ui.polygon_doc = None; + } else if app.ui.tool != Tool::PolygonLasso || app.ui.polygon_doc.is_some_and(|d| Some(d) != active) { + app.ui.polygon.clear(); + app.ui.polygon_mode.clear(); + app.ui.polygon_doc = None; + } +} + +/// A new-selection polygonal (or magnetic) lasso is being drawn, so the selection it will replace +/// is hidden. +pub fn polygon_replaces_selection(app: &PhotocraftApp) -> bool { + (!app.ui.polygon.is_empty() && app.ui.polygon_mode == "replace") || crate::magnetic_lasso_ui::replaces_selection(app) +} + +/// Close the polygonal lasso and make the selection in the mode it started in. +pub fn commit_polygon(app: &mut PhotocraftApp) { + polygon_cancel_stale(app); + let pts = std::mem::take(&mut app.ui.polygon); + let mode = std::mem::take(&mut app.ui.polygon_mode); + app.ui.polygon_doc = None; + if pts.len() >= 3 { + let mode = if mode.is_empty() { selection_mode(app, egui::Modifiers::NONE).to_owned() } else { mode }; + let o = &app.ui.tool_options; + let _ = app.run("select.lasso", json!({"points": pts, "mode": mode, "antiAlias": o.anti_alias, "feather": o.feather})); + } +} + +/// Apply the crop tool's rectangle. When `image.crop` refuses it (the error is in the status bar), +/// the frame, its turn and its box Undo stay for fixing. +pub fn commit_crop(app: &mut PhotocraftApp) { + // Never another document's frame (#1918). + crate::crop_ui::cancel_stale(app); + let kept = crate::crop_ui::keep(app); + // Committing ends the box's own Undo/Redo: from here Edit › Undo reverts the document. + crate::crop_ui::clear_history(app); + // ↵ during a drag ends it: the release must not bring back the old document's frame. + app.crop.drag = None; + let Some(r) = app.ui.crop_rect.take() else { return }; + // The crop's default mode leaves the view upright (`crop_mode`). + crate::crop_mode::upright(app); + // The untouched default frame around the whole canvas crops nothing (Photoshop's ↵ on it does + // nothing); one framing the selection's bounds crops to them (#1789). + if std::mem::take(&mut app.crop.default_frame) { + let whole = app.session.active().map(|st| st.doc.bounds()).map(|b| [f64::from(b.x0), f64::from(b.y0), f64::from(b.x1), f64::from(b.y1)]); + if whole.is_none_or(|b| b == r) { + return; + } + } + let (x, y) = (r[0].round(), r[1].round()); + let (w, h) = ((r[2] - r[0]).round().max(1.0), (r[3] - r[1]).round().max(1.0)); + let delete = app.ui.tool_options.crop_delete; + let mut p = json!({"x": x, "y": y, "width": w, "height": h, "deleteCroppedPixels": delete}); + // Fill › Content-Aware Fill (#3823): what a turned or extending frame uncovers is filled from + // its surroundings instead of the background colour. + if app.ui.tool_options.crop_content_aware { + p["contentAware"] = json!(true); + } + // A turned frame (#1792): the document turns with it, then is cropped to it. (The untouched + // default frame above is never turned: turning it makes it a real frame.) + let angle = crate::crop_ui::angle(app); + app.ui.crop_angle = 0.0; + if angle != 0.0 { + p["angle"] = json!(angle); + } + // W x H x Resolution (#2443): resample to that size and set the resolution. + crate::crop_size::commit_params(app, &mut p); + if app.run("image.crop", p).is_err() { + crate::crop_ui::restore(app, kept); + } else if let Some(i) = app.session.active_index() { + app.ui.views[i].fit_pending = true; + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas.rs b/crates/ui-egui/src/canvas/gpu_canvas.rs new file mode 100644 index 0000000000..48aeb07e0f --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas.rs @@ -0,0 +1,1008 @@ +//! GPU document canvas: an `egui_wgpu` paint callback that draws one document view with a custom +//! WGSL shader. +//! +//! The document's composite lives on the GPU as one or more mipmapped textures (premultiplied +//! alpha, sRGB-encoded values, matching how the CPU path and egui blend): `Rgba8Unorm` for 8-bit +//! documents, `Rgba16Float` for 16- and 32-bit documents when the adapter can render to and +//! filter it (so smooth gradients don't band and 32-bit values above 1.0 survive for the 32-bit +//! preview), within [`F16_BUDGET_PX`]; larger high-bit documents fall back to `Rgba8Unorm`. +//! Uploads are either full (new document, size change, re-render) or a damage rect (brush +//! strokes); after an upload only the affected region of each mip level is regenerated by a +//! render-pass blit chain. Documents larger than the device's maximum texture size are split +//! into tiles. +//! +//! Per frame the shader draws, in *device pixels*: +//! - a soft analytic drop shadow around the document on the pasteboard (erf-blurred box), +//! - a transparency checkerboard (8 pt squares, grays 255/204, the same size at every zoom) +//! under the document, anchored to the document's top-left corner so it moves with the image, +//! - the document itself, sampled trilinearly when zoomed out, bilinearly between 1× and 2×, and +//! with an anti-aliased "sharp nearest" filter at integer scales and ≥ 2×, +//! - a one-device-pixel pixel grid above 500% zoom, over pixels with content only. +//! +//! GPU resources live in the renderer's `callback_resources` type map; the per-frame callback only +//! carries plain view parameters, so it is `Send + Sync` on every target (including wasm). +//! +//! The document pixels still come from the CPU compositor (`photocraft-compose`) for now; the wgpu +//! compositor (M5) will render straight into these tiles. + +use std::collections::HashMap; +use std::num::NonZeroU64; +use std::sync::atomic::Ordering; + +use eframe::egui_wgpu::{self, CallbackResources, CallbackTrait, RenderState, ScreenDescriptor}; +use eframe::wgpu; + +use crate::render_path::CpuReason; + +pub use photocraft_gpu::{DeviceHealth, Fault}; + +mod budget; +mod callback; +mod doc_textures; +mod perf; +mod resources; +mod setup; +mod shaders; +mod texels; + +#[cfg(all(test, target_os = "linux"))] +use budget::nvidia_video_memory; +pub use budget::{ + DEFER_PX, MIN_GPU_BUDGET, OVER_BUDGET, OVER_CANVAS_BUDGET, REFRESH_DEFERRED, REFRESH_OVER_BUDGET, canvas_budget, canvas_texture_bytes, memory_budget, + physical_memory, video_memory, vram_share, +}; +#[cfg(test)] +use budget::{NVIDIA_VENDOR, bounded_output, drm_video_memory, parse_nvidia_smi}; +#[cfg(all(test, windows))] +use budget::{detect_physical_memory, detect_video_memory}; +use callback::CanvasCallback; +#[cfg(test)] +use callback::{filter_mode, map_doc_px, tile_screen_aabb}; +use doc_textures::init_buffer; +#[cfg(test)] +use doc_textures::{mip_count, mip_damage}; +pub use perf::{GpuInfo, Perf, backend_name, now_ms}; +pub use resources::CanvasStyle; +use resources::{DocTextures, Resources, identity_lut_bytes, lut_bind_group}; +#[cfg(test)] +use setup::device_limits_for; +use setup::texture_buffer; +pub use setup::{Refresh, device_descriptor, device_limits, display_texels, timing_features, use_adapter_limits, use_adapter_limits_with, wgpu_setup}; +#[cfg(test)] +use shaders::{CANVAS_WGSL, ENCODE_WGSL, MIP_WGSL}; +#[cfg(not(target_arch = "wasm32"))] +use texels::texel_to_f32; +pub use texels::{LayerFit, premultiply_rgba8, premultiply_rgba16f}; +#[cfg(test)] +use texels::{f16_to_f32, f32_to_f16}; +use texels::{texel_bytes, texels, texels_into}; + +/// Default tile side; documents up to this size (and the device limit) use a single texture. +pub const DEFAULT_TILE: u32 = 8192; +/// Mip levels no larger than this on either side are redrawn whole on every edit rather than +/// just their damage (see [`mip_damage`], #6418). Every corrupted level measured was at most +/// 64 px; this leaves a factor of two. +const WHOLE_MIP_SIDE: u32 = 128; +const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm; +/// Canvas texture format of high-bit documents (see [`GpuCanvas::format_for`]). +const FORMAT_HIGH: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float; +/// Largest high-bit document (in pixels) given an `Rgba16Float` canvas texture: 100 MP is +/// 800 MB at level 0, about 1.07 GB with mips (twice the `Rgba8Unorm` cost). Bigger 16/32-bit +/// documents (a 14000² one would need 2.1 GB) use `Rgba8Unorm`, so they stay on the GPU like +/// 8-bit ones. `PHOTOCRAFT_CANVAS_F16=0` turns the float canvas off, `=1` ignores this budget. +pub const F16_BUDGET_PX: u64 = 100_000_000; +const VIEW_UNIFORM_SIZE: u64 = 128; +const VIEW_FLOATS: usize = 32; +const TILE_UNIFORM_SIZE: u64 = 16; + +/// Whether this view can use the GPU canvas. Flip and non-square pixel aspect correction are +/// still CPU blits (the shader's filtering assumes square pixels); rotation is a shader uniform. +pub fn gpu_view_ok(flip: bool, _rotation: f32, aspect: f32) -> bool { + !flip && (aspect - 1.0).abs() < 1e-6 +} + +/// Parameters for drawing one document view. Positions are in egui points. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ViewParams { + /// Document key used with [`GpuCanvas::upload_full`]. + pub doc: u64, + /// Document size in pixels. + pub doc_size: [u32; 2], + /// Screen points per document pixel. + pub zoom: f32, + /// Document point shown at the centre of the canvas rect. + pub center: [f32; 2], + /// Camera rotation in radians, clockwise in Y-down screen space. Rotate around [`Self::center`] + /// before sampling. Flip stays on the CPU path. + pub rotation: f32, + /// Draw the drop shadow on the pasteboard. + pub shadow: bool, + /// Draw the pixel grid at high zoom. + pub pixel_grid: bool, + /// Distinguishes views painted in the same frame (main canvas, extra windows). + pub view_key: u64, + /// Display transform: 0 none, 1 the document's display LUT (document → monitor profile, + /// Proof Colors), 2 LUT plus the gamut warning, 3 matrix/shaper (see [`GpuCanvas::set_display_lut`]). + pub display: u8, + /// The display the view is on (its LUT is per document and display, #569; 0 = unknown). + pub output: u32, + /// View › 32-bit Preview Options (exposure in stops, gamma), applied to the texture values + /// before the display LUT; `None` when off. The display LUT must then leave it out + /// (`ColorState::gpu_canvas_lut`). + pub hdr: Option<[f32; 2]>, + /// Where this draw puts the document, in document pixels from its own place: `[0, 0]` for the + /// document, a whole number of widths and heights for a View › Pattern Preview copy (#1067). + /// A copy samples the same texture, so nothing is composited again. + pub offset: [f32; 2], +} + +/// When high-bit documents get an `Rgba16Float` canvas texture. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum HighPolicy { + /// The adapter can't render to or filter `Rgba16Float`, or `PHOTOCRAFT_CANVAS_F16=0`. + Off, + /// Up to [`F16_BUDGET_PX`]. + Budget, + /// Always (`PHOTOCRAFT_CANVAS_F16=1`). + Always, +} + +/// Uploads documents to the GPU and paints them. Cheap to clone. +/// +/// Once the device is lost or reports an uncaptured error (see [`GpuCanvas::fault`]), every +/// method is a no-op or an `Err`, so nothing reaches the GPU again; the app then switches to the +/// CPU canvas (#243). +#[derive(Clone)] +pub struct GpuCanvas { + rs: RenderState, + tile: u32, + high: HighPolicy, + health: photocraft_gpu::DeviceHealth, + /// [`video_memory`] of the adapter, detected once. + vram: Option, + /// [`canvas_budget`], shared with the paint callback's resources: [`GpuCanvas::format_for`] + /// reads it while the renderer is locked. + canvas_budget: std::sync::Arc, + /// Camera Raw's preview pass, built the first time the dialog asks (#6419). + camera_raw: crate::camera_raw_gpu::Slot, + camera_raw_started: std::sync::Arc, +} + +/// Whether `regenerate_mips` builds each level in a scratch texture and copies it back: on +/// Intel adapters under DX12 only, where some drivers corrupt reduced previews when a pass +/// samples one mip level and renders into another of the same texture (#892, Intel UHD 620). +/// Everywhere else it renders straight into the level: on an NVIDIA DX12 adapter the scratch +/// textures, created per level on every refresh, cost about 1 ms of CPU time per brush dab +/// (#4515). `PHOTOCRAFT_SEPARATE_MIPS` (`setting`) forces it on (`1`) or off (`0`). +pub fn separate_mip_targets(backend: wgpu::Backend, vendor: u32, setting: Option<&str>) -> bool { + const INTEL: u32 = 0x8086; + match setting.map(str::trim) { + Some("1") => true, + Some("0") => false, + _ => backend == wgpu::Backend::Dx12 && vendor == INTEL, + } +} + +/// Can `adapter` use `Rgba16Float` as a sampled, filtered, render-target and copy texture? +/// (Core WebGPU guarantees it; WebGL2 needs `EXT_color_buffer_float`, and some drivers lack it.) +pub fn supports_f16_canvas(adapter: &wgpu::Adapter) -> bool { + let f = adapter.get_texture_format_features(FORMAT_HIGH); + let usages = wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST | wgpu::TextureUsages::COPY_SRC; + f.allowed_usages.contains(usages) && f.flags.contains(wgpu::TextureFormatFeatureFlags::FILTERABLE) +} + +impl GpuCanvas { + /// Create pipelines and register resources with the egui renderer. + pub fn new(rs: &RenderState) -> Self { + Self::with_tile(rs, None) + } + + /// Like [`GpuCanvas::new`] with a preferred texture tile side (`PHOTOCRAFT_GPU_TILE` wins). + pub fn with_tile(rs: &RenderState, tile: Option) -> Self { + let max = rs.device.limits().max_texture_dimension_2d; + let env_tile = std::env::var("PHOTOCRAFT_GPU_TILE").ok().and_then(|v| v.parse::().ok()); + let tile = env_tile.or(tile).unwrap_or(DEFAULT_TILE).clamp(64, max); + let high = match std::env::var("PHOTOCRAFT_CANVAS_F16").ok().as_deref() { + _ if !supports_f16_canvas(&rs.adapter) => HighPolicy::Off, + Some("0") => HighPolicy::Off, + Some("1") => HighPolicy::Always, + _ => HighPolicy::Budget, + }; + // Device loss and uncaptured errors mark this flag instead of panicking (#243). + let health = photocraft_gpu::DeviceHealth::watch(&rs.device); + let mut res = Resources::new(&rs.device, &rs.queue, rs.target_format, high != HighPolicy::Off); + res.health = health.clone(); + let info = rs.adapter.get_info(); + res.separate_mip_targets = separate_mip_targets(info.backend, info.vendor, std::env::var("PHOTOCRAFT_SEPARATE_MIPS").ok().as_deref()); + let vram = video_memory(&info); + res.canvas_budget.store(canvas_budget(physical_memory(), vram), Ordering::Relaxed); + let budget = res.canvas_budget.clone(); + rs.renderer.write().callback_resources.insert(res); + log::info!( + "gpu canvas: target {:?}, max texture {max}, tile {tile}, 16F canvas {high:?}, video memory {}", + rs.target_format, + vram.map_or_else(|| "unknown".to_owned(), |b| format!("{} MB", b >> 20)) + ); + Self { rs: rs.clone(), tile, high, health, vram, canvas_budget: budget, camera_raw: Default::default(), camera_raw_started: Default::default() } + } + + /// The device's health flag (shared with the wgpu compositor and the paint callback). + pub fn health(&self) -> &photocraft_gpu::DeviceHealth { + &self.health + } + + /// Camera Raw's GPU preview develop on this device; the first call starts building its + /// pipeline. `None` on a software adapter, without `Rgba32Float` render targets, after a + /// fault, or when the driver couldn't build the pipeline; the dialog then develops on the CPU. + pub(crate) fn camera_raw(&self) -> Option { + if self.fault().is_some() { + return None; + } + if !self.camera_raw_started.swap(true, Ordering::AcqRel) { + crate::camera_raw_gpu::Pass::start(&self.rs.adapter, &self.rs.device, &self.camera_raw); + } + if matches!(self.camera_raw.get(), Some(None)) { + return None; + } + Some(crate::camera_raw_gpu::CameraRawGpu::new(self.rs.device.clone(), self.rs.queue.clone(), self.health.clone(), self.camera_raw.clone())) + } + + /// Whether [`Self::camera_raw`] started building the pass. + #[cfg(test)] + pub(crate) fn camera_raw_started(&self) -> bool { + self.camera_raw_started.load(Ordering::Acquire) + } + + /// Waits until the pass is built or refused. + #[cfg(test)] + pub(crate) fn wait_camera_raw_pass(&self) { + let _ = self.camera_raw.wait(); + } + + /// Builds Camera Raw's pass even where [`Self::camera_raw`] wouldn't (egui_kittest's software + /// adapter), so tests drive the GPU develop. + #[cfg(test)] + pub(crate) fn force_camera_raw_pass(&self) { + self.camera_raw_started.store(true, Ordering::Release); + let _ = self.camera_raw.set(crate::camera_raw_gpu::Pass::build_on(&self.rs.device)); + } + + /// Why the GPU can't be used any more (`None` while it can). + pub fn fault(&self) -> Option { + self.health.fault() + } + + /// Free every GPU resource the canvas holds (compositor pages, document textures, LUTs) after + /// a fault; the paint callback draws nothing from then on. + pub fn release(&self) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.compositor = None; + res.compositor_failed = Some(photocraft_gpu::Unsupported(self.fault().map_or_else(|| "GPU canvas released".into(), |f| f.to_string()))); + res.docs.clear(); + res.luts.clear(); + res.display_lut_signatures.clear(); + res.views.clear(); + } + } + + /// The adapter this canvas renders with. + pub fn adapter_info(&self) -> wgpu::AdapterInfo { + self.rs.adapter.get_info() + } + + /// The adapter's own memory where it has one and the platform reports it (see + /// [`video_memory`]); it caps [`memory_budget`] and [`canvas_budget`]. + pub fn video_memory(&self) -> Option { + self.vram + } + + /// The canvas texture format for a document of `depth` and `size`: `Rgba16Float` for 16/32-bit + /// documents when the adapter supports it and the document fits [`F16_BUDGET_PX`], else + /// `Rgba8Unorm`. + /// The device's largest 2D texture side (egui textures can use it too). + pub fn max_texture_side(&self) -> usize { + self.rs.device.limits().max_texture_dimension_2d as usize + } + + pub fn format_for(&self, depth: photocraft_doc::SampleType, size: [u32; 2]) -> wgpu::TextureFormat { + let px = size[0] as u64 * size[1] as u64; + let high = match self.high { + HighPolicy::Off => false, + // Within the canvas budget too: a 16-bit canvas a small card can't hold as floats + // stays on the GPU as `Rgba8Unorm` rather than falling back to the CPU canvas. + HighPolicy::Budget => { + px <= F16_BUDGET_PX && canvas_texture_bytes(size, self.tile, texel_bytes(FORMAT_HIGH)) <= self.canvas_budget.load(Ordering::Relaxed) + } + HighPolicy::Always => true, + }; + if high && depth != photocraft_doc::SampleType::U8 { FORMAT_HIGH } else { FORMAT } + } + + /// Format and GPU bytes (all tiles, all mip levels) of document `key`'s canvas texture. + pub fn texture_info(&self, key: u64) -> Option<(wgpu::TextureFormat, u64)> { + let r = self.rs.renderer.read(); + let d = r.callback_resources.get::()?.docs.get(&key)?; + let bpp = texel_bytes(d.format); + let bytes = d + .tiles + .iter() + .map(|t| (0..t.levels.len() as u32).map(|l| ((t.rect[2] >> l).max(1) as u64) * ((t.rect[3] >> l).max(1) as u64) * bpp).sum::()) + .sum(); + Some((d.format, bytes)) + } + + /// Set the GPU memory the wgpu compositor may hold for layer pages and caches (see + /// [`memory_budget`]); pages beyond it are evicted least recently used first. + pub fn set_memory_budget(&self, bytes: u64) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.compositor_budget = Some(bytes); + } + } + + /// The compositor's memory budget (`None` until [`GpuCanvas::set_memory_budget`]). + pub fn memory_budget(&self) -> Option { + let r = self.rs.renderer.read(); + r.callback_resources.get::()?.compositor_budget + } + + /// Set the GPU bytes one document's display texture set may take (see [`canvas_budget`]). + pub fn set_canvas_budget(&self, bytes: u64) { + self.canvas_budget.store(bytes, Ordering::Relaxed); + } + + /// Whether `doc`'s display textures fit [`canvas_budget`]; when they don't, nothing is + /// created for it and it is drawn on the CPU canvas. + pub fn fits_canvas(&self, doc: &photocraft_doc::Document) -> bool { + self.canvas_fit(doc).is_some_and(|(bytes, budget)| bytes <= budget) + } + + /// The bytes `doc`'s display textures would take and [`canvas_budget`] (`None` without a GPU + /// canvas): the numbers of a [`CpuReason::CanvasTooLarge`]. + pub fn canvas_fit(&self, doc: &photocraft_doc::Document) -> Option<(u64, u64)> { + let size = [doc.size.width, doc.size.height]; + let bytes = canvas_texture_bytes(size, self.tile, texel_bytes(self.format_for(doc.depth, size))); + let r = self.rs.renderer.read(); + r.callback_resources.get::().map(|res| (bytes, res.canvas_budget.load(Ordering::Relaxed))) + } + + /// Act as if the driver couldn't build the compositor's pipelines (the DX12 FXC X3507 case, + /// #8733): every later refresh goes to the CPU compositor with `msg` as the reason. + #[cfg(test)] + pub(crate) fn fail_compositor(&self, msg: &str) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.compositor = None; + res.compositor_failed = Some(photocraft_gpu::Unsupported(msg.into())); + } + } + + /// Why the compositor couldn't be built, once it couldn't (see [`GpuCanvas::composite`]). + fn compositor_failed(&self) -> Option { + let r = self.rs.renderer.read(); + r.callback_resources.get::()?.compositor_failed.as_ref().map(|e| e.0.clone()) + } + + /// A [`photocraft_gpu::Unsupported`] from [`GpuCanvas::composite`] as a typed reason, with + /// the numbers of the budget it names. + fn reason(&self, doc: &photocraft_doc::Document, e: photocraft_gpu::Unsupported) -> CpuReason { + if let Some(f) = self.fault() { + return CpuReason::from_fault(&f); + } + if e.0 == OVER_BUDGET { + let fit = self.layer_fit(); + return CpuReason::OverBudget { needed: fit.map_or(0, |f| f.needed), budget: fit.map_or(0, |f| f.budget) }; + } + if e.0 == OVER_CANVAS_BUDGET { + let (bytes, budget) = self.canvas_fit(doc).unwrap_or_default(); + return CpuReason::CanvasTooLarge { bytes, budget }; + } + if self.compositor_failed().is_some_and(|f| f == e.0) { + return CpuReason::CompositorBuild(e.0); + } + CpuReason::Unsupported(e.0) + } + + /// Cap lower-stack composite reuse; zero disables it for profiling the uncached path. + pub fn set_composite_cache_budget(&self, bytes: u64) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.compositor_cache_budget = Some(bytes); + if let Some(comp) = &mut res.compositor { + comp.set_composite_cache_budget(bytes); + } + } + } + + /// The document area the view shows: full refreshes composite it last, so its layer pages + /// are the ones still resident for the edits that follow. + pub fn set_focus(&self, focus: Option) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.compositor_focus = focus; + } + } + + /// Take any GPU measurement that has landed since the last call, without compositing + /// anything. [`Compositor::render`] carries measurements out in its stats, but a refresh is + /// the only thing that collects them, so the last refresh before the app goes idle would + /// never be reported. The frame loop calls this so it is. + pub fn take_gpu_timing(&self) -> Option { + let mut r = self.rs.renderer.write(); + r.callback_resources.get_mut::()?.compositor.as_mut()?.take_gpu_timing() + } + + /// GPU bytes the wgpu compositor holds: resident layer pages and caches (effect maps and + /// lower-stack chunks). Keep the scorecard's memory measurements inclusive of reuse. + pub fn compositor_bytes(&self) -> Option<(u64, usize)> { + let r = self.rs.renderer.read(); + let c = r.callback_resources.get::()?.compositor.as_ref()?; + Some((c.resident_bytes(), c.fx_cache_bytes().saturating_add(usize::try_from(c.composite_cache_bytes()).unwrap_or(usize::MAX)))) + } + + /// Whether `doc`'s layers fit the budget as it now stands, measured without a refresh (a GPU + /// Memory change shows in the status bar at once). `None` before the compositor exists. + pub fn measure_fit(&self, doc: &photocraft_doc::Document) -> Option { + let mut r = self.rs.renderer.write(); + let res = r.callback_resources.get_mut::()?; + let budget = res.compositor_budget; + let comp = res.compositor.as_mut()?; + if let Some(b) = budget { + comp.set_memory_budget(b); + } + let needed = comp.working_set(doc, doc.bounds()); + let fit = LayerFit { doc: doc.id.0, needed, budget: comp.fit_layers(needed, true) }; + res.layer_fit = Some(fit); + Some(fit) + } + + /// Whether the layers of the last full (or large) refresh fit the GPU memory budget, and by + /// how much (`None` before the first one). + pub fn layer_fit(&self) -> Option { + let r = self.rs.renderer.read(); + r.callback_resources.get::()?.layer_fit + } + + /// Set the checkerboard and gamut warning colours. + pub fn set_style(&self, style: CanvasStyle) { + let mut r = self.rs.renderer.write(); + if let Some(res) = r.callback_resources.get_mut::() { + res.style = style; + } + } + + /// Whether document `doc` has been uploaded with this size. + pub fn has(&self, doc: u64, size: [u32; 2]) -> bool { + let r = self.rs.renderer.read(); + r.callback_resources.get::().and_then(|res| res.docs.get(&doc)).is_some_and(|d| d.size == size) + } + + /// Whether [`GpuCanvas::composite`] would draw `doc` with the wgpu compositor (rather than + /// fall back to the CPU compositor). + pub fn supports(&self, doc: &photocraft_doc::Document) -> bool { + if std::env::var_os("PHOTOCRAFT_CPU_COMPOSE").is_some() || doc.size.width == 0 || doc.size.height == 0 { + return false; + } + let r = self.rs.renderer.read(); + let Some(res) = r.callback_resources.get::() else { return false }; + if res.compositor_failed.is_some() { + return false; + } + match &res.compositor { + Some(c) => c.supports(doc).is_ok(), + // Layers larger than the texture limit are stored in pages, so any size qualifies. + None => photocraft_gpu::plan(doc).is_ok(), + } + } + + /// Replace the whole document image with an `Rgba8Unorm` texture. `rgba` is premultiplied + /// RGBA8, `size[0] * size[1] * 4` bytes (anything else is ignored). + pub fn upload_full(&self, doc: u64, size: [u32; 2], rgba: &[u8]) { + self.upload_full_as(doc, size, FORMAT, rgba); + } + + /// Replace the whole document image with a `format` texture of premultiplied `texels`. + fn upload_full_as(&self, doc: u64, size: [u32; 2], format: wgpu::TextureFormat, texels: &[u8]) { + if size[0] == 0 || size[1] == 0 || texels.len() as u64 != size[0] as u64 * size[1] as u64 * texel_bytes(format) || !self.health.is_ok() { + return; + } + let (device, queue) = (&self.rs.device, &self.rs.queue); + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return }; + if res.docs.get(&doc).is_none_or(|d| d.size != size || d.format != format) { + let Some(tex) = DocTextures::new(device, res, size, self.tile, format) else { + res.docs.remove(&doc); + return; + }; + res.docs.insert(doc, tex); + } + let Some(d) = res.docs.get(&doc) else { return }; + d.write(queue, [0, 0], size, size[0], texels); + d.regenerate_mips(device, queue, res, [0, 0, size[0], size[1]]); + } + + /// Replace a rectangle `origin..origin+size` of an uploaded document with premultiplied texels + /// in its texture format (`size[0] * size[1]` texels: RGBA8 or RGBA16F). Returns false if the + /// document isn't uploaded, the rectangle doesn't fit or the buffer has the wrong size. + pub fn upload_rect(&self, doc: u64, origin: [u32; 2], size: [u32; 2], texels: &[u8]) -> bool { + if !self.health.is_ok() { + return false; + } + let (device, queue) = (&self.rs.device, &self.rs.queue); + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return false }; + let Some(d) = res.docs.get(&doc) else { return false }; + if texels.len() as u64 != size[0] as u64 * size[1] as u64 * texel_bytes(d.format) + || origin[0].saturating_add(size[0]) > d.size[0] + || origin[1].saturating_add(size[1]) > d.size[1] + { + return false; + } + if size[0] == 0 || size[1] == 0 { + return true; + } + d.write(queue, origin, size, size[0], texels); + d.regenerate_mips(device, queue, res, [origin[0], origin[1], origin[0] + size[0], origin[1] + size[1]]); + true + } + + /// The texture format of uploaded document `doc`. + fn format_of(&self, doc: u64) -> Option { + let r = self.rs.renderer.read(); + r.callback_resources.get::().and_then(|res| res.docs.get(&doc)).map(|d| d.format) + } + + /// Upload a CPU composite covering the whole document (a preview of a document of `depth`: + /// see [`GpuCanvas::format_for`]). + pub fn upload_buffer_full(&self, doc: u64, buf: &photocraft_compose::Buffer, depth: photocraft_doc::SampleType) { + let size = [buf.rect.width(), buf.rect.height()]; + let format = self.format_for(depth, size); + self.upload_full_as(doc, size, format, &texels(format, &buf.px)); + } + + /// Upload a CPU composite of a sub-rectangle (in document pixels, `buf.rect`). + pub fn upload_buffer_rect(&self, doc: u64, buf: &photocraft_compose::Buffer) -> bool { + let r = buf.rect; + let Some(format) = self.format_of(doc) else { return false }; + if r.x0 < 0 || r.y0 < 0 { + return false; + } + self.upload_rect(doc, [r.x0 as u32, r.y0 as u32], [r.width(), r.height()], &texels(format, &buf.px)) + } + + /// Composite `region` of `doc` with the wgpu compositor straight into its display texture + /// (no CPU pixels, no upload of the composite). `encode_srgb` stores the sRGB encoding of the + /// composite (linear documents, see `photocraft_engine::display_color`). Returns `Err` when the + /// document uses features the GPU compositor doesn't cover yet; the caller then falls back to + /// the CPU compositor. + pub fn composite( + &self, + doc: &photocraft_doc::Document, + region: photocraft_geom::Rect, + encode_srgb: bool, + ) -> Result { + let (device, queue) = (&self.rs.device, &self.rs.queue); + let size = [doc.size.width, doc.size.height]; + if size[0] == 0 || size[1] == 0 { + return Err(photocraft_gpu::Unsupported("empty document".into())); + } + if std::env::var_os("PHOTOCRAFT_CPU_COMPOSE").is_some() { + return Err(photocraft_gpu::Unsupported("disabled by PHOTOCRAFT_CPU_COMPOSE".into())); + } + if let Some(f) = self.fault() { + return Err(photocraft_gpu::Unsupported(f.to_string())); + } + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return Err(photocraft_gpu::Unsupported("no GPU canvas".into())) }; + // A driver that couldn't build the pipelines once won't later: stay on the CPU compositor. + if let Some(e) = &res.compositor_failed { + return Err(e.clone()); + } + let mut comp = match res.compositor.take() { + Some(c) => c, + None => match photocraft_gpu::Compositor::try_new_with_format(device, photocraft_gpu::Compositor::preferred_acc_format(&self.rs.adapter)) { + Ok(c) => c, + Err(e) => { + // `render_path::note` logs it, once, as this document's reason. + res.compositor_failed = Some(e.clone()); + return Err(e); + } + }, + }; + comp.set_health(self.health.clone()); + // Measured, not waited for: the timestamps come back a frame or two later. + comp.measure_gpu_time(device, queue); + if let Some(b) = res.compositor_budget { + comp.set_memory_budget(b); + } + if let Some(b) = res.compositor_cache_budget { + comp.set_composite_cache_budget(b); + } + comp.set_focus(res.compositor_focus); + let key = doc.id.0; + let format = self.format_for(doc.depth, size); + let fresh = res.docs.get(&key).is_none_or(|d| d.size != size || d.format != format); + let region = if fresh { doc.bounds() } else { region.intersect(&doc.bounds()) }; + // Checked before allocating a new canvas texture only: `render` plans (and refuses) itself. + if fresh && let Err(e) = comp.supports(doc) { + res.compositor = Some(comp); + return Err(e); + } + // A full refresh whose layer pages don't fit the memory budget would re-upload the + // evicted ones every time (no faster than the CPU, and more memory): the banded CPU + // compositor does it (in the background on desktop). So does a large damage rect, such + // as hiding a big layer (#4024); small edits in the view stay on the GPU. + let large = region.width() as u64 * region.height() as u64 >= DEFER_PX; + if region == doc.bounds() || large { + let needed = comp.working_set(doc, region); + let fit = LayerFit { doc: doc.id.0, needed, budget: comp.fit_layers(needed, region == doc.bounds()) }; + res.layer_fit = Some(fit); + if !fit.fits() { + res.compositor = Some(comp); + return Err(photocraft_gpu::Unsupported(OVER_BUDGET.into())); + } + } + if fresh { + let Some(tex) = DocTextures::new(device, res, size, self.tile, format) else { + res.docs.remove(&key); + res.compositor = Some(comp); + return Err(photocraft_gpu::Unsupported(OVER_CANVAS_BUDGET.into())); + }; + res.docs.insert(key, tex); + } + let (Some(pipe), Some(d)) = (res.encode_pipeline(format), res.docs.get(&key)) else { + res.compositor = Some(comp); + return Err(photocraft_gpu::Unsupported(format!("no {format:?} canvas texture or pipeline"))); + }; + let bgl = &res.encode_bgl; + // A float canvas keeps values above 1.0 (32-bit documents, for the 32-bit preview). + let keep_hdr = i32::from(format == FORMAT_HIGH); + // Last-resort guard: a wgpu panic (e.g. polling a device lost mid-refresh) marks the + // device lost instead of taking the app and its documents down. + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + comp.render(device, queue, doc, region, |enc, out| { + for t in &d.tiles { + let [tx, ty, tw, th] = t.rect.map(|v| v as i32); + let r = out.rect.intersect(&photocraft_geom::Rect::from_xywh(tx, ty, tw as u32, th as u32)); + if r.is_empty() { + continue; + } + let offset = [out.rect.x0 - tx, out.rect.y0 - ty, i32::from(encode_srgb), keep_hdr]; + let ubuf = init_buffer(device, "pc_encode", &offset.iter().flat_map(|v| v.to_le_bytes()).collect::>(), wgpu::BufferUsages::UNIFORM); + let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_encode"), + layout: bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(out.view) }, + wgpu::BindGroupEntry { binding: 1, resource: ubuf.as_entire_binding() }, + ], + }); + let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("pc_encode"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &t.levels[0], + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { load: wgpu::LoadOp::Load, store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(pipe); + pass.set_bind_group(0, &bg, &[]); + pass.set_scissor_rect((r.x0 - tx) as u32, (r.y0 - ty) as u32, r.width(), r.height()); + pass.draw(0..3, 0..1); + } + }) + })); + let Ok(result) = result else { + self.health.mark(photocraft_gpu::Fault::Lost("the GPU compositor stopped (internal error)".into())); + res.docs.remove(&key); + return Err(photocraft_gpu::Unsupported("GPU device lost".into())); + }; + // Lost during the refresh: its output can't be trusted. + let result = match self.fault() { + Some(f) => Err(photocraft_gpu::Unsupported(f.to_string())), + None => result, + }; + if result.is_ok() { + d.regenerate_mips(device, queue, res, [region.x0 as u32, region.y0 as u32, region.x1 as u32, region.y1 as u32]); + if std::env::var_os("PHOTOCRAFT_GPU_SYNC").is_some() { + // Benchmarking: wait for the GPU so callers can time the whole refresh. + #[cfg(not(target_arch = "wasm32"))] + self.health.wait(device, None); + } + } else if fresh { + res.docs.remove(&key); + } + res.compositor = Some(comp); + result + } + + /// Bring document `key`'s display texture up to date with `doc`: only `damage` when given (a + /// brush stroke on an already uploaded document), else everything. The wgpu compositor renders + /// straight into the texture when it supports the document; otherwise the CPU compositor does. + /// `display` is the canvas colour transform: the GPU path stores the sRGB encoding when it + /// asks for it, and every CPU band or rect goes through its `texture_buffer`. + pub fn refresh( + &self, + key: u64, + doc: &photocraft_doc::Document, + damage: Option, + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + ) -> Refresh { + self.refresh_with(key, doc, damage, display, false) + } + + /// [`GpuCanvas::refresh`], except that with `defer`, a refresh of at least [`DEFER_PX`] the + /// CPU compositor would draw is left to the caller (`background_redraw`): the texture is made + /// ready (keeping what it shows when it already fits) and the kind is [`REFRESH_DEFERRED`]. + pub fn refresh_with( + &self, + key: u64, + doc: &photocraft_doc::Document, + damage: Option, + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + defer: bool, + ) -> Refresh { + if let Some(f) = self.fault() { + // Nothing reaches the GPU any more; the app switches to the CPU canvas. + return Refresh { kind: "lost", fallback: Some(CpuReason::from_fault(&f)), ..Default::default() }; + } + if !self.fits_canvas(doc) { + // Neither compositor can fill textures that are never created: the CPU canvas draws + // it. A set left from before the canvas grew is stale, so it goes too. + if let Some(res) = self.rs.renderer.write().callback_resources.get_mut::() { + res.docs.remove(&key); + } + let (bytes, budget) = self.canvas_fit(doc).unwrap_or_default(); + return Refresh { kind: REFRESH_OVER_BUDGET, fallback: Some(CpuReason::CanvasTooLarge { bytes, budget }), ..Default::default() }; + } + let bounds = doc.bounds(); + let size = [doc.size.width, doc.size.height]; + let format = self.format_for(doc.depth, size); + let damage = damage.filter(|_| self.has(key, size) && self.format_of(key) == Some(format)).map(|r| r.intersect(&bounds)); + let t0 = now_ms(); + let mut out = Refresh::default(); + if damage.is_some_and(|r| r.is_empty()) { + // Nothing changed on the canvas (selecting a layer, say): don't even plan. + out.kind = "rect"; + return out; + } + let region = damage.unwrap_or(bounds); + let encode_srgb = display.is_some_and(|d| d.encode_srgb); + let gpu = if key == doc.id.0 { self.composite(doc, region, encode_srgb) } else { Err(photocraft_gpu::Unsupported("preview texture".into())) }; + match gpu { + Ok(stats) => { + out.kind = if damage.is_some() { "gpu-rect" } else { "gpu-full" }; + out.px = region.width() as u64 * region.height() as u64; + // Encoding and submitting only: the GPU is still running this work. + out.composite_ms = now_ms() - t0; + out.uploads = stats.tiles_uploaded as u64; + out.gpu = stats.gpu_time; + return out; + } + Err(e) => out.fallback = Some(self.reason(doc, e)), + } + let px = region.width() as u64 * region.height() as u64; + if defer && px >= DEFER_PX && self.prepare_texture(key, doc).is_some() { + out.kind = REFRESH_DEFERRED; + out.px = px; + return out; + } + if let Some(r) = damage { + if r.is_empty() { + out.kind = "rect"; + return out; + } + let buf = photocraft_compose::render(doc, r); + let t1 = now_ms(); + if self.upload_buffer_rect(key, &texture_buffer(display, &buf)) { + out.kind = "rect"; + out.px = r.width() as u64 * r.height() as u64; + out.composite_ms = t1 - t0; + out.upload_ms = now_ms() - t1; + return out; + } + } + self.upload_composite(key, doc, display); + out.kind = "full"; + out.px = bounds.width() as u64 * bounds.height() as u64; + out.composite_ms = now_ms() - t0; + out + } + + /// Composite the whole of `doc` with the CPU compositor into document `key`'s texture, band by + /// band (each band converted and uploaded before the next is rendered), so a huge document + /// never needs a full-size float composite or texture-format copy in memory. + pub fn upload_composite(&self, key: u64, doc: &photocraft_doc::Document, display: Option<&photocraft_engine::display_color::CanvasDisplay>) { + let size = [doc.size.width, doc.size.height]; + let Some(format) = self.prepare_texture(key, doc) else { return }; + let (device, queue) = (&self.rs.device, &self.rs.queue); + let mut rgba = Vec::new(); + let _ = photocraft_compose::render_bands(doc, doc.bounds(), 0, |band| -> Result<(), ()> { + if !self.health.is_ok() { + return Err(()); + } + texels_into(format, &texture_buffer(display, &band).px, &mut rgba); + let renderer = self.rs.renderer.read(); + let d = renderer.callback_resources.get::().and_then(|r| r.docs.get(&key)).ok_or(())?; + d.write(queue, [0, band.rect.y0 as u32], [size[0], band.rect.height()], size[0], &rgba); + drop(renderer); + // Flush the staged band so its staging memory is reclaimed while the next renders. + queue.submit(std::iter::empty::()); + self.health.poll(device); + Ok(()) + }); + if !self.health.is_ok() { + return; + } + let mut renderer = self.rs.renderer.write(); + if let Some(res) = renderer.callback_resources.get_mut::() + && let Some(d) = res.docs.get(&key) + { + d.regenerate_mips(device, queue, res, [0, 0, size[0], size[1]]); + } + } + + /// Make sure document `key` has a display texture of `doc`'s size and format, keeping the one + /// it has when that fits. Returns its format, or `None` when it can't be created. + pub fn prepare_texture(&self, key: u64, doc: &photocraft_doc::Document) -> Option { + let size = [doc.size.width, doc.size.height]; + if size[0] == 0 || size[1] == 0 || !self.health.is_ok() { + return None; + } + let format = self.format_for(doc.depth, size); + let mut renderer = self.rs.renderer.write(); + let res = renderer.callback_resources.get_mut::()?; + if res.docs.get(&key).is_none_or(|d| d.size != size || d.format != format) { + let Some(tex) = DocTextures::new(&self.rs.device, res, size, self.tile, format) else { + res.docs.remove(&key); + return None; + }; + res.docs.insert(key, tex); + } + Some(format) + } + + /// Free textures, display LUTs and their signatures for documents not in `live`. + pub fn retain(&self, live: &[u64]) { + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return }; + if res.docs.keys().any(|k| !live.contains(k)) { + res.docs.retain(|k, _| live.contains(k)); + } + // Everything the compositor holds for documents that aren't open, also when their + // texture entry was dropped earlier (over budget, a failed render), which used to leave + // their pages and effect maps resident after closing (#4529). + if let Some(c) = res.compositor.as_mut() { + c.retain_docs(|d| live.contains(&d.0)); + } + // LUTs and identity-transform signatures can outlive document textures. Prune both, + // or a preserved-ID reopen can skip rebuilding a missing LUT. + res.luts.retain(|k, _| live.contains(&k.0)); + res.display_lut_signatures.retain(|k, _| live.contains(&k.0)); + } + + pub(crate) fn display_lut_signature(&self, doc: u64, output: u32) -> Option<(u64, u8)> { + let renderer = self.rs.renderer.read(); + renderer.callback_resources.get::()?.display_lut_signatures.get(&(doc, output)).copied() + } + + /// Whether document `doc` has a display LUT for display `output` (tests). + #[cfg(test)] + pub(crate) fn has_display_lut(&self, doc: u64, output: u32) -> bool { + let renderer = self.rs.renderer.read(); + renderer.callback_resources.get::().is_some_and(|r| r.luts.contains_key(&(doc, output))) + } + + pub(crate) fn cache_display_lut_signature(&self, doc: u64, output: u32, signature: u64, mode: u8) { + let mut renderer = self.rs.renderer.write(); + if let Some(res) = renderer.callback_resources.get_mut::() { + res.display_lut_signatures.insert((doc, output), (signature, mode)); + } + } + + /// Set (or clear with `None`) the display LUT of document `doc` on display `output`: + /// `size`³ RGBA8 texels, red fastest. RGB is the display colour for each lattice input; + /// alpha 255 marks out-of-gamut colours for the gamut warning. + pub fn set_display_lut(&self, doc: u64, output: u32, size: u32, rgba: Option<&[u8]>) { + if !self.health.is_ok() { + return; + } + let (device, queue) = (&self.rs.device, &self.rs.queue); + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return }; + res.display_lut_signatures.remove(&(doc, output)); + match rgba { + None => { + res.luts.remove(&(doc, output)); + } + Some(bytes) => { + if size < 2 || bytes.len() as u64 != (size as u64).pow(3) * 4 { + log::warn!("set_display_lut: {} bytes for a {size}³ LUT; ignored", bytes.len()); + return; + } + let bg = lut_bind_group(device, queue, &res.lut_bgl, size, bytes, None); + res.luts.insert((doc, output), bg); + } + } + } + + /// Set a separately sampled curve/matrix/curve display transform. CMS validates this + /// representation and provides bounded tables; no profile interpretation lives in the UI. + pub fn set_display_matrix_shaper(&self, doc: u64, output: u32, shaper: &photocraft_cms::GpuMatrixShaper) { + if !self.health.is_ok() { + return; + } + let mut renderer = self.rs.renderer.write(); + let Some(res) = renderer.callback_resources.get_mut::() else { return }; + let bg = lut_bind_group(&self.rs.device, &self.rs.queue, &res.lut_bgl, 2, &identity_lut_bytes(), Some(shaper)); + res.display_lut_signatures.remove(&(doc, output)); + res.luts.insert((doc, output), bg); + } + + /// Read back level 0 of document `key`'s canvas texture: its format, size and the stored + /// (premultiplied) texels as `f32` RGBA, row-major. Waits for the GPU; for tests, benchmarks + /// and debugging. + #[cfg(not(target_arch = "wasm32"))] + pub fn read_texels(&self, key: u64) -> Option<(wgpu::TextureFormat, [u32; 2], Vec<[f32; 4]>)> { + if !self.health.is_ok() { + return None; + } + let (device, queue) = (&self.rs.device, &self.rs.queue); + let renderer = self.rs.renderer.read(); + let d = renderer.callback_resources.get::()?.docs.get(&key)?; + let bpp = texel_bytes(d.format); + let (w, h) = (d.size[0] as usize, d.size[1] as usize); + let mut out = vec![[0.0f32; 4]; w.checked_mul(h)?]; + for t in &d.tiles { + let [tx, ty, tw, th] = t.rect; + let row = (tw as u64 * bpp).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as u64); + let buf = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_canvas_readback"), + size: row * th as u64, + usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ, + mapped_at_creation: false, + }); + let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_canvas_readback") }); + enc.copy_texture_to_buffer( + t.texture.as_image_copy(), + wgpu::TexelCopyBufferInfo { + buffer: &buf, + layout: wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(row as u32), rows_per_image: Some(th) }, + }, + wgpu::Extent3d { width: tw, height: th, depth_or_array_layers: 1 }, + ); + queue.submit([enc.finish()]); + let slice = buf.slice(..); + slice.map_async(wgpu::MapMode::Read, |_| {}); + if !self.health.wait(device, None) { + return None; + } + let data = slice.get_mapped_range().ok()?; + for y in 0..th as usize { + let src = data.get(y * row as usize..)?; + for x in 0..tw as usize { + let o = out.get_mut((ty as usize + y) * w + tx as usize + x)?; + *o = texel_to_f32(d.format, src.get(x * bpp as usize..(x + 1) * bpp as usize)?); + } + } + } + Some((d.format, d.size, out)) + } + + /// Add the paint callback drawing `params` into `rect` (clipped by the painter's clip rect). + pub fn paint(painter: &egui::Painter, rect: egui::Rect, params: ViewParams) { + painter.add(egui_wgpu::Callback::new_paint_callback(rect, CanvasCallback { rect, params })); + } +} + +#[cfg(test)] +mod mip_tests; + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/gpu_canvas/budget.rs b/crates/ui-egui/src/canvas/gpu_canvas/budget.rs new file mode 100644 index 0000000000..9f5df3ebd6 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/budget.rs @@ -0,0 +1,280 @@ +use super::doc_textures::mip_count; +use super::wgpu; + +/// Floor of the compositor's memory budget: enough for a viewport's pages of a dozen layers. +pub const MIN_GPU_BUDGET: u64 = 512 << 20; + +/// Why a full refresh whose layer pages don't fit [`memory_budget`] (or GPU memory) goes to the +/// CPU compositor. +pub const OVER_BUDGET: &str = photocraft_gpu::OVER_BUDGET; + +/// GPU memory the wgpu compositor may hold for layer pages and effect maps. +/// +/// `fixed` (Preferences › Performance › GPU Memory, bytes) sets it when it isn't 0, within +/// [`vram_share`] of a discrete GPU's own memory (`vram`, from [`video_memory`]). Otherwise it is +/// Memory Usage (`allowance`) within a quarter of physical memory (`ram`; 16 GB assumed when +/// unknown) and the VRAM share. The document's pixels and History (`pixels`) come off Memory Usage +/// unless the card's own memory is known: a discrete card doesn't hold them, while integrated and +/// software adapters (and a card whose memory can't be read) share the system's. Never below +/// [`MIN_GPU_BUDGET`]. Layers beyond it redraw full refreshes on the CPU ([`OVER_BUDGET`]). +pub fn memory_budget(allowance: u64, pixels: u64, ram: Option, vram: Option, fixed: u64) -> u64 { + let card = vram.map_or(u64::MAX, vram_share); + if fixed > 0 { + return fixed.min(card).max(MIN_GPU_BUDGET); + } + let ram = ram.unwrap_or(16 << 30); + let charged = if vram.is_some() { 0 } else { pixels }; + allowance.saturating_sub(charged).min(ram / 4).min(card).max(MIN_GPU_BUDGET) +} + +/// The share of a discrete GPU's memory each of [`memory_budget`] and [`canvas_budget`] may +/// plan for: three eighths, so the two together leave a quarter of the card to the window's +/// swapchain, egui and other applications instead of over-committing it and letting the driver +/// page (#4536). +pub fn vram_share(vram: u64) -> u64 { + vram / 8 * 3 +} + +/// Why a document whose display textures don't fit [`canvas_budget`] is drawn on the CPU canvas. +pub const OVER_CANVAS_BUDGET: &str = "the canvas exceeds the GPU texture budget; drawing it on the CPU canvas"; + +/// [`Refresh::kind`] when the document's display textures don't fit [`canvas_budget`]: nothing +/// was created, and the caller draws the CPU canvas instead. +pub const REFRESH_OVER_BUDGET: &str = "over-budget"; + +/// [`Refresh::kind`] of a CPU refresh left to the caller ([`GpuCanvas::refresh_with`]). +pub const REFRESH_DEFERRED: &str = "deferred"; + +/// The smallest CPU refresh (pixels) [`GpuCanvas::refresh_with`] defers: below it, the +/// synchronous composite takes a fraction of a second even with many layers. +pub const DEFER_PX: u64 = 1 << 22; + +/// GPU bytes one document's display texture set (every tile and mip level) may take: a quarter +/// of physical memory (`ram`; 16 GB assumed when unknown) and at most [`vram_share`] of a +/// discrete GPU's own memory (`vram`), at least [`MIN_GPU_BUDGET`]. The set covers the whole +/// canvas at full resolution, so its size follows the declared canvas, not the layer pixels; a +/// larger canvas is drawn on the (reduced) CPU canvas instead (#2934). +pub fn canvas_budget(ram: Option, vram: Option) -> u64 { + (ram.unwrap_or(16 << 30) / 4).min(vram.map_or(u64::MAX, vram_share)).max(MIN_GPU_BUDGET) +} + +/// GPU bytes of a display texture set for a `size` canvas in `tile`-sided tiles of `bpp` bytes +/// per texel, with every mip level (what [`GpuCanvas::texture_info`] reports once it exists). +/// Closed form over the (at most four) distinct tile shapes, so any size is cheap to price. +pub fn canvas_texture_bytes(size: [u32; 2], tile: u32, bpp: u64) -> u64 { + let tile = tile.max(1); + // (how many tiles, their side) along one axis: whole tiles, then the remainder. + let spans = |n: u32| [(n / tile, tile), (u32::from(!n.is_multiple_of(tile)), n % tile)]; + let mut total = 0u64; + for (nx, w) in spans(size[0]) { + for (ny, h) in spans(size[1]) { + if nx == 0 || ny == 0 { + continue; + } + let one = (0..mip_count(w, h)).map(|l| u64::from((w >> l).max(1)) * u64::from((h >> l).max(1)) * bpp).fold(0, u64::saturating_add); + total = total.saturating_add(one.saturating_mul(u64::from(nx) * u64::from(ny))); + } + } + total +} + +/// Physical memory of this machine in bytes, if known. `PHOTOCRAFT_RAM_MB` overrides it (to +/// simulate a smaller machine). +pub fn physical_memory() -> Option { + static RAM: std::sync::OnceLock> = std::sync::OnceLock::new(); + *RAM.get_or_init(|| { + if let Some(mb) = std::env::var("PHOTOCRAFT_RAM_MB").ok().and_then(|v| v.trim().parse::().ok()) { + return mb.checked_mul(1 << 20); + } + detect_physical_memory() + }) +} + +#[cfg(target_os = "macos")] +fn detect_physical_memory() -> Option { + let out = std::process::Command::new("/usr/sbin/sysctl").args(["-n", "hw.memsize"]).output().ok()?; + String::from_utf8(out.stdout).ok()?.trim().parse().ok() +} + +#[cfg(target_os = "linux")] +fn detect_physical_memory() -> Option { + let info = std::fs::read_to_string("/proc/meminfo").ok()?; + let kb: u64 = info.lines().find_map(|l| l.strip_prefix("MemTotal:"))?.trim().trim_end_matches("kB").trim().parse().ok()?; + kb.checked_mul(1024) +} + +#[cfg(target_os = "freebsd")] +fn detect_physical_memory() -> Option { + let out = std::process::Command::new("/sbin/sysctl").args(["-n", "hw.physmem"]).output().ok()?; + String::from_utf8(out.stdout).ok()?.trim().parse().ok() +} + +#[cfg(windows)] +pub(super) fn detect_physical_memory() -> Option { + winsafe::GlobalMemoryStatusEx().map(|m| m.ullTotalPhys).ok().filter(|b| *b > 0) +} + +#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "freebsd", windows)))] +fn detect_physical_memory() -> Option { + None +} + +/// The memory of `adapter` itself, in bytes, when it is a discrete GPU and the platform reports +/// it: DXGI's dedicated video memory on Windows, amdgpu's VRAM size or `nvidia-smi`'s report on +/// Linux. `None` for integrated and software adapters, whose textures live in system memory (the +/// RAM-based budgets already cover them), and where nothing reports it. `PHOTOCRAFT_VRAM_MB` +/// overrides it for any adapter, simulating a discrete card of that size: on an integrated GPU the +/// budget then stops charging the document's pixels too, as for a real card (a development switch). +pub fn video_memory(adapter: &wgpu::AdapterInfo) -> Option { + if let Some(mb) = std::env::var("PHOTOCRAFT_VRAM_MB").ok().and_then(|v| v.trim().parse::().ok()) { + return mb.checked_mul(1 << 20).filter(|b| *b > 0); + } + if adapter.device_type != wgpu::DeviceType::DiscreteGpu { + return None; + } + // Detected once per adapter, as `physical_memory` is: on Windows each detection builds a + // DXGI factory, and startup asks three times. Keyed by the adapter, since a GPU retry can + // pick another one. + /// (vendor, device) ids and the memory found for them. + type Seen = Vec<((u32, u32), Option)>; + static SEEN: std::sync::Mutex = std::sync::Mutex::new(Vec::new()); + let key = (adapter.vendor, adapter.device); + let mut seen = SEEN.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + if let Some((_, bytes)) = seen.iter().find(|(k, _)| *k == key) { + return *bytes; + } + let bytes = detect_video_memory(key.0, key.1).filter(|b| *b > 0); + seen.push((key, bytes)); + bytes +} + +#[cfg(windows)] +pub(super) fn detect_video_memory(vendor: u32, device: u32) -> Option { + use winsafe::prelude::*; + let factory = winsafe::CreateDXGIFactory1().ok()?; + factory + .EnumAdapters1() + .filter_map(|a| a.ok()?.GetDesc1().ok()) + .find(|d| d.VendorId == vendor && d.DeviceId == device) + .and_then(|d| u64::try_from(d.DedicatedVideoMemory).ok()) +} + +#[cfg(target_os = "linux")] +fn detect_video_memory(vendor: u32, device: u32) -> Option { + drm_video_memory(std::path::Path::new("/sys/class/drm"), vendor, device).or_else(|| nvidia_video_memory(vendor, device)) +} + +/// PCI vendor id of NVIDIA, whose proprietary Linux driver writes none of amdgpu's +/// `mem_info_vram_total` sysfs files (a card of its own still reads as [`None`] above): without +/// this, a discrete NVIDIA card was charged for the document's pixels like an integrated one, +/// `memory_budget` shrank to Memory Usage minus them, and a many-layer document whose pages then +/// missed that budget redrew whole-document edits on the CPU every time (P60/P61 on an NVIDIA +/// Linux box: 1.5 s where the fix's own benchmark measures tens of milliseconds elsewhere). +#[cfg(any(target_os = "linux", test))] +pub(super) const NVIDIA_VENDOR: u32 = 0x10de; + +/// How long `nvidia-smi` gets to answer: it runs on the UI thread while the window opens, and a +/// driver that hangs the query must not keep PhotoCraft from starting. +#[cfg(target_os = "linux")] +const NVIDIA_SMI_DEADLINE: std::time::Duration = std::time::Duration::from_secs(2); + +/// NVIDIA's own memory size on Linux, from `nvidia-smi` (installed with the driver that put this +/// adapter there in the first place): absent, slower than [`NVIDIA_SMI_DEADLINE`] or odd output +/// all read as `None`, never a hang or a panic. +#[cfg(target_os = "linux")] +pub(super) fn nvidia_video_memory(vendor: u32, device: u32) -> Option { + if vendor != NVIDIA_VENDOR { + return None; + } + let mut cmd = std::process::Command::new("nvidia-smi"); + cmd.args(["--query-gpu=pci.device_id,memory.total", "--format=csv,noheader,nounits"]); + let out = bounded_output(cmd, NVIDIA_SMI_DEADLINE)?; + parse_nvidia_smi(&String::from_utf8_lossy(&out), vendor, device) +} + +/// The first 64 KiB of `cmd`'s stdout when it exits successfully within `deadline`; `None` when +/// it can't start, fails or is still running by then. A late one is killed and reaped on a thread +/// of its own (a process stuck in the driver may not die at once), so the caller waits about +/// `deadline` at most. +#[cfg(any(target_os = "linux", test))] +pub(super) fn bounded_output(mut cmd: std::process::Command, deadline: std::time::Duration) -> Option> { + use std::io::Read; + use std::process::{Child, Stdio}; + use std::time::{Duration, Instant}; + fn abandon(mut child: Child) { + let _ = child.kill(); + let _ = std::thread::Builder::new().name("reap".into()).spawn(move || child.wait()); + } + let end = Instant::now().checked_add(deadline)?; + let mut child = cmd.stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null()).spawn().ok()?; + let Some(stdout) = child.stdout.take() else { + abandon(child); + return None; + }; + let (tx, rx) = std::sync::mpsc::channel(); + let reader = std::thread::Builder::new().name("child-stdout".into()).spawn(move || { + let mut buf = Vec::new(); + let _ = stdout.take(64 << 10).read_to_end(&mut buf); + let _ = tx.send(buf); + }); + if reader.is_err() { + abandon(child); + return None; + } + let status = loop { + match child.try_wait() { + Ok(Some(status)) => break status, + Ok(None) if Instant::now() < end => std::thread::sleep(Duration::from_millis(10)), + _ => { + abandon(child); + return None; + } + } + }; + if !status.success() { + return None; + } + // The child has exited, so its stdout is at EOF unless it handed the pipe on to a process of + // its own: bounded too, rather than joining the reader. + rx.recv_timeout(end.saturating_duration_since(Instant::now()).max(Duration::from_millis(50))).ok() +} + +/// `nvidia-smi --query-gpu=pci.device_id,memory.total --format=csv,noheader,nounits`'s own +/// format: one `0xDDDDVVVV, size-in-MiB` line per card (device id the high half, vendor id the +/// low, as `lspci -n` and wgpu's own ids read them). The matching line's size, in bytes, and the +/// smallest when several cards share those ids (the ids can't tell which one wgpu picked, so the +/// budget assumes the smaller); `None` when no line names this `vendor`/`device`, or the output +/// isn't shaped like that at all. +#[cfg(any(target_os = "linux", test))] +pub(super) fn parse_nvidia_smi(output: &str, vendor: u32, device: u32) -> Option { + output + .lines() + .filter_map(|line| { + let (id, mb) = line.split_once(',')?; + let id = u32::from_str_radix(id.trim().trim_start_matches("0x").trim_start_matches("0X"), 16).ok()?; + if id & 0xffff != vendor || id >> 16 != device { + return None; + } + mb.trim().parse::().ok()?.checked_mul(1 << 20).filter(|b| *b > 0) + }) + .min() +} + +/// amdgpu's VRAM size (`/device/mem_info_vram_total`, bytes) of the card under `drm` (the +/// sysfs `/sys/class/drm` folder) whose PCI `vendor` and `device` ids (hex files beside it) match. +#[cfg(any(target_os = "linux", test))] +pub(super) fn drm_video_memory(drm: &std::path::Path, vendor: u32, device: u32) -> Option { + let hex = |p: std::path::PathBuf| u32::from_str_radix(std::fs::read_to_string(p).ok()?.trim().trim_start_matches("0x"), 16).ok(); + std::fs::read_dir(drm).ok()?.flatten().find_map(|card| { + let dev = card.path().join("device"); + if hex(dev.join("vendor"))? != vendor || hex(dev.join("device"))? != device { + return None; + } + std::fs::read_to_string(dev.join("mem_info_vram_total")).ok()?.trim().parse().ok() + }) +} + +#[cfg(not(any(target_os = "linux", windows)))] +fn detect_video_memory(_vendor: u32, _device: u32) -> Option { + None +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/callback.rs b/crates/ui-egui/src/canvas/gpu_canvas/callback.rs new file mode 100644 index 0000000000..23e11399b0 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/callback.rs @@ -0,0 +1,207 @@ +use super::resources::{CanvasStyle, Resources, ViewGpu, f32_bytes}; +use super::{CallbackResources, CallbackTrait, ScreenDescriptor, VIEW_FLOATS, VIEW_UNIFORM_SIZE, ViewParams, wgpu}; + +// --------------------------------------------------------------------------------------------- +// Per-frame callback + +pub(super) struct CanvasCallback { + pub(super) rect: egui::Rect, + pub(super) params: ViewParams, +} + +/// Sampling mode: 0 trilinear, 1 bilinear, 2 anti-aliased nearest. +pub(super) fn filter_mode(scale: f32) -> (f32, f32) { + if scale < 0.999 { + (0.0, (-scale.log2()).max(0.0)) + } else if (scale - scale.round()).abs() < 1e-3 || scale >= 2.0 { + (2.0, 0.0) + } else { + (1.0, 0.0) + } +} + +pub(super) fn map_doc_px(origin: [f32; 2], scale: f32, center: [f32; 2], cos: f32, sin: f32, x: f32, y: f32) -> [f32; 2] { + let dx = x - center[0]; + let dy = y - center[1]; + let rx = dx * cos - dy * sin; + let ry = dx * sin + dy * cos; + [origin[0] + center[0] * scale + rx * scale, origin[1] + center[1] * scale + ry * scale] +} + +pub(super) fn tile_screen_aabb(origin: [f32; 2], scale: f32, center: [f32; 2], cos: f32, sin: f32, tile: [f32; 4]) -> (f32, f32, f32, f32) { + let [tx, ty, tw, th] = tile; + if sin.abs() < 1e-8 && (cos - 1.0).abs() < 1e-8 { + let x0 = origin[0] + tx * scale; + let y0 = origin[1] + ty * scale; + return (x0, y0, x0 + tw * scale, y0 + th * scale); + } + let pts = [ + map_doc_px(origin, scale, center, cos, sin, tx, ty), + map_doc_px(origin, scale, center, cos, sin, tx + tw, ty), + map_doc_px(origin, scale, center, cos, sin, tx + tw, ty + th), + map_doc_px(origin, scale, center, cos, sin, tx, ty + th), + ]; + let mut x0 = pts[0][0]; + let mut y0 = pts[0][1]; + let mut x1 = x0; + let mut y1 = y0; + for p in pts { + x0 = x0.min(p[0]); + y0 = y0.min(p[1]); + x1 = x1.max(p[0]); + y1 = y1.max(p[1]); + } + (x0, y0, x1, y1) +} + +impl CanvasCallback { + /// Screen-pixel origin of document (0, 0) (snapped to whole pixels) and device px per doc px. + fn placement(&self, ppp: f32) -> ([f32; 2], f32) { + let p = &self.params; + let scale = p.zoom * ppp; + let c = self.rect.center(); + let ox = (c.x * ppp - p.center[0] * scale).round(); + let oy = (c.y * ppp - p.center[1] * scale).round(); + ([ox, oy], scale) + } + + fn uniforms(&self, screen: [u32; 2], ppp: f32, out_linear: bool, style: &CanvasStyle) -> [f32; VIEW_FLOATS] { + let p = &self.params; + let (origin, scale) = self.placement(ppp); + let (mode, lod) = filter_mode(scale); + // The pixel grid shows above 500% (the constants are shared with the CPU path, + // pixel_grid.rs). `p.zoom` is screen points per document pixel; the shared threshold is + // in device pixels, so scale by the display's pixels per point. + let grid = if p.pixel_grid && crate::pixel_grid::shows_at(p.zoom * ppp) { crate::pixel_grid::STRENGTH } else { 0.0 }; + let square = if style.checker_square > 0.0 { (style.checker_square * ppp).round().max(1.0) } else { 0.0 }; + let (l, d, g) = (style.checker_light, style.checker_dark, style.gamut_color); + // 32-bit preview: linear-light gain 2^exposure (0 = off) and 1 / gamma. + let (gain, inv_gamma) = match p.hdr { + Some([e, gm]) if e.is_finite() && gm.is_finite() && gm > 0.0 => (2f32.powf(e.clamp(-20.0, 20.0)), 1.0 / gm.max(0.01)), + _ => (0.0, 1.0), + }; + [ + screen[0] as f32, + screen[1] as f32, + scale, + ppp, + origin[0], + origin[1], + p.doc_size[0] as f32, + p.doc_size[1] as f32, + mode, + lod, + grid, + square, + if p.offset[0].is_finite() { p.offset[0] } else { 0.0 }, + if p.offset[1].is_finite() { p.offset[1] } else { 0.0 }, + p.display as f32, + if out_linear { 1.0 } else { 0.0 }, + l[0], + l[1], + l[2], + style.gamut_opacity, + d[0], + d[1], + d[2], + gain, + g[0], + g[1], + g[2], + inv_gamma, + { + let r = if p.rotation.is_finite() { p.rotation } else { 0.0 }; + if r.abs() < 1e-8 { 1.0 } else { r.cos() } + }, + { + let r = if p.rotation.is_finite() { p.rotation } else { 0.0 }; + if r.abs() < 1e-8 { 0.0 } else { r.sin() } + }, + p.center[0], + p.center[1], + ] + } +} + +impl CallbackTrait for CanvasCallback { + fn prepare( + &self, + device: &wgpu::Device, + queue: &wgpu::Queue, + screen: &ScreenDescriptor, + _encoder: &mut wgpu::CommandEncoder, + resources: &mut CallbackResources, + ) -> Vec { + let Some(res) = resources.get_mut::() else { return Vec::new() }; + if !res.health.is_ok() { + return Vec::new(); + } + let data = self.uniforms(screen.size_in_pixels, screen.pixels_per_point, res.out_linear, &res.style); + let (bgl, sampler) = (&res.view_bgl, &res.sampler); + let v = res.views.entry(self.params.view_key).or_insert_with(|| { + let buffer = device.create_buffer(&wgpu::BufferDescriptor { + label: Some("pc_canvas_view"), + size: VIEW_UNIFORM_SIZE, + usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + mapped_at_creation: false, + }); + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_canvas_view"), + layout: bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: buffer.as_entire_binding() }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(sampler) }, + ], + }); + ViewGpu { buffer, bind_group, frame_data: [f32::NAN; VIEW_FLOATS] } + }); + if v.frame_data != data { + queue.write_buffer(&v.buffer, 0, &f32_bytes(&data)); + v.frame_data = data; + } + Vec::new() + } + + fn paint(&self, info: egui::PaintCallbackInfo, pass: &mut wgpu::RenderPass<'static>, resources: &CallbackResources) { + let Some(res) = resources.get::() else { return }; + if !res.health.is_ok() { + return; + } + let Some(view) = res.views.get(&self.params.view_key) else { return }; + let [sw, sh] = info.screen_size_px; + let clip = info.clip_rect_in_pixels(); + if clip.width_px <= 0 || clip.height_px <= 0 || sw == 0 || sh == 0 { + return; + } + // Work in whole-screen pixel coordinates; the scissor does the clipping. + pass.set_viewport(0.0, 0.0, sw as f32, sh as f32, 0.0, 1.0); + pass.set_scissor_rect(clip.left_px.max(0) as u32, clip.top_px.max(0) as u32, clip.width_px as u32, clip.height_px as u32); + pass.set_bind_group(0, &view.bind_group, &[]); + if self.params.shadow { + pass.set_pipeline(&res.shadow_pipeline); + pass.draw(0..6, 0..1); + } + let Some(doc) = res.docs.get(&self.params.doc) else { return }; + if doc.size != self.params.doc_size { + return; + } + let (origin, scale) = self.placement(info.pixels_per_point); + let (cx0, cy0) = (clip.left_px as f32, clip.top_px as f32); + let (cx1, cy1) = (cx0 + clip.width_px as f32, cy0 + clip.height_px as f32); + let rot = if self.params.rotation.is_finite() { self.params.rotation } else { 0.0 }; + let (cos, sin) = if rot.abs() < 1e-8 { (1.0, 0.0) } else { (rot.cos(), rot.sin()) }; + let cen = self.params.center; + let [ox, oy] = self.params.offset.map(|v| if v.is_finite() { v } else { 0.0 }); + pass.set_pipeline(&res.tile_pipeline); + pass.set_bind_group(2, res.luts.get(&(self.params.doc, self.params.output)).unwrap_or(&res.identity_lut), &[]); + for t in &doc.tiles { + let [tx, ty, tw, th] = t.rect.map(|v| v as f32); + let (x0, y0, x1, y1) = tile_screen_aabb(origin, scale, cen, cos, sin, [tx + ox, ty + oy, tw, th]); + if x1 < cx0 || y1 < cy0 || x0 > cx1 || y0 > cy1 { + continue; + } + pass.set_bind_group(1, &t.bind_group, &[]); + pass.draw(0..6, 0..1); + } + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs new file mode 100644 index 0000000000..fb09f08e14 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/doc_textures.rs @@ -0,0 +1,206 @@ +use super::budget::canvas_texture_bytes; +use super::resources::{DocTextures, Resources, Tile, f32_bytes}; +use super::texels::texel_bytes; +use super::{Ordering, WHOLE_MIP_SIDE, wgpu}; + +pub(super) fn mip_count(w: u32, h: u32) -> u32 { + 32 - w.max(h).max(1).leading_zeros() +} + +/// The rect (x0, y0, x1, y1) of a `lw`×`lh` mip level to redraw when `prev` changed in the level +/// above: that damage halved and padded by one texel for odd-size rounding, or the whole level +/// when it is at most [`WHOLE_MIP_SIDE`] on either side. On AMD's Vulkan driver a pass that draws +/// only part of a level of 64 px or less corrupts texels outside its scissor, at the level's +/// top-left (#6418: RX 6900 XT, driver 26.8.1; DX12 on the same card is fine). Such a level costs +/// next to nothing to redraw, so this holds for every adapter; the levels after it are smaller +/// still, so the rest of the chain is redrawn whole too. +pub(super) fn mip_damage([x0, y0, x1, y1]: [u32; 4], lw: u32, lh: u32) -> [u32; 4] { + if lw.max(lh) <= WHOLE_MIP_SIDE { + return [0, 0, lw, lh]; + } + [(x0 / 2).saturating_sub(1), (y0 / 2).saturating_sub(1), (x1.div_ceil(2) + 1).min(lw), (y1.div_ceil(2) + 1).min(lh)] +} + +/// A buffer holding `contents`, like `DeviceExt::create_buffer_init` but without its panic when +/// the device was lost (an invalid buffer has no mapped range): the buffer is then just invalid. +pub(super) fn init_buffer(device: &wgpu::Device, label: &str, contents: &[u8], usage: wgpu::BufferUsages) -> wgpu::Buffer { + let size = (contents.len() as u64).next_multiple_of(wgpu::COPY_BUFFER_ALIGNMENT).max(wgpu::COPY_BUFFER_ALIGNMENT); + let buffer = device.create_buffer(&wgpu::BufferDescriptor { label: Some(label), size, usage, mapped_at_creation: true }); + if let Ok(mut view) = buffer.slice(..).get_mapped_range_mut() + && view.len() >= contents.len() + { + view.slice(..contents.len()).copy_from_slice(contents); + } + buffer.unmap(); + buffer +} + +impl DocTextures { + /// The texture set for a `size` canvas, or `None` (nothing created) when it doesn't fit + /// the canvas budget or the GPU's memory. A set the GPU can't hold (a 3 GP canvas is 16 GB + /// of textures, #4017) lowers the budget below it, so that canvas and larger ones are drawn + /// on the CPU canvas instead of the out-of-memory error disabling the GPU for the session. + pub(super) fn new(device: &wgpu::Device, res: &Resources, size: [u32; 2], tile: u32, format: wgpu::TextureFormat) -> Option { + let bytes = canvas_texture_bytes(size, tile, texel_bytes(format)); + if bytes > res.canvas_budget.load(Ordering::Relaxed) { + return None; + } + let validation = device.push_error_scope(wgpu::ErrorFilter::Validation); + let memory = device.push_error_scope(wgpu::ErrorFilter::OutOfMemory); + let set = Self::create(device, res, size, tile, format); + // Native wgpu reports these synchronously; where they arrive later (WebGPU) none is seen. + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + let mut error = None; + for pop in [memory.pop(), validation.pop()] { + if let std::task::Poll::Ready(Some(e)) = std::pin::pin!(pop).poll(&mut cx) { + error.get_or_insert(e); + } + } + if let Some(e) = error { + log::warn!("{} MB of canvas textures failed ({e}); larger canvases are drawn on the CPU", bytes >> 20); + res.canvas_budget.fetch_min(bytes.saturating_sub(1), Ordering::Relaxed); + return None; + } + Some(set) + } + + fn create(device: &wgpu::Device, res: &Resources, size: [u32; 2], tile: u32, format: wgpu::TextureFormat) -> Self { + let mut tiles = Vec::new(); + for ty in (0..size[1]).step_by(tile as usize) { + for tx in (0..size[0]).step_by(tile as usize) { + let (w, h) = (tile.min(size[0] - tx), tile.min(size[1] - ty)); + let levels_n = mip_count(w, h); + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_canvas_tile"), + size: wgpu::Extent3d { width: w, height: h, depth_or_array_layers: 1 }, + mip_level_count: levels_n, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format, + usage: wgpu::TextureUsages::TEXTURE_BINDING + | wgpu::TextureUsages::COPY_DST + | wgpu::TextureUsages::COPY_SRC + | wgpu::TextureUsages::RENDER_ATTACHMENT, + view_formats: &[], + }); + let levels = (0..levels_n) + .map(|l| texture.create_view(&wgpu::TextureViewDescriptor { base_mip_level: l, mip_level_count: Some(1), ..Default::default() })) + .collect(); + let full = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let uniform = init_buffer(device, "pc_canvas_tile", &f32_bytes(&[tx as f32, ty as f32, w as f32, h as f32]), wgpu::BufferUsages::UNIFORM); + let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_canvas_tile"), + layout: &res.tile_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&full) }, + wgpu::BindGroupEntry { binding: 1, resource: uniform.as_entire_binding() }, + ], + }); + tiles.push(Tile { rect: [tx, ty, w, h], texture, levels, bind_group, _uniform: uniform }); + } + } + Self { size, format, tiles } + } + + /// Write `size` pixels at document `origin` from `texels` (in the texture format, row stride + /// `stride` pixels) into every overlapping tile's level 0. + pub(super) fn write(&self, queue: &wgpu::Queue, origin: [u32; 2], size: [u32; 2], stride: u32, texels: &[u8]) { + let bpp = texel_bytes(self.format); + let (x0, y0, x1, y1) = (origin[0], origin[1], origin[0] + size[0], origin[1] + size[1]); + for t in &self.tiles { + let [tx, ty, tw, th] = t.rect; + let (ix0, iy0, ix1, iy1) = (x0.max(tx), y0.max(ty), x1.min(tx + tw), y1.min(ty + th)); + if ix0 >= ix1 || iy0 >= iy1 { + continue; + } + let offset = ((iy0 - y0) as u64 * stride as u64 + (ix0 - x0) as u64) * bpp; + queue.write_texture( + wgpu::TexelCopyTextureInfo { + texture: &t.texture, + mip_level: 0, + origin: wgpu::Origin3d { x: ix0 - tx, y: iy0 - ty, z: 0 }, + aspect: wgpu::TextureAspect::All, + }, + texels, + wgpu::TexelCopyBufferLayout { offset, bytes_per_row: Some(stride * bpp as u32), rows_per_image: Some(iy1 - iy0) }, + wgpu::Extent3d { width: ix1 - ix0, height: iy1 - iy0, depth_or_array_layers: 1 }, + ); + } + } + + /// Rebuild the mip chain inside document rect `r` (x0, y0, x1, y1) with 2×2 box filtering. + pub(super) fn regenerate_mips(&self, device: &wgpu::Device, queue: &wgpu::Queue, res: &Resources, r: [u32; 4]) { + let Some(mip_pipeline) = res.mip_pipeline(self.format) else { return }; + let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("pc_mips") }); + for t in &self.tiles { + let [tx, ty, tw, th] = t.rect; + let (mut x0, mut y0, mut x1, mut y1) = (r[0].max(tx), r[1].max(ty), r[2].min(tx + tw), r[3].min(ty + th)); + if x0 >= x1 || y0 >= y1 { + continue; + } + (x0, y0, x1, y1) = (x0 - tx, y0 - ty, x1 - tx, y1 - ty); + for level in 1..t.levels.len() { + let (lw, lh) = ((tw >> level).max(1), (th >> level).max(1)); + [x0, y0, x1, y1] = mip_damage([x0, y0, x1, y1], lw, lh); + let bg = device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_mip"), + layout: &res.mip_bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&t.levels[level - 1]) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::Sampler(&res.mip_sampler) }, + ], + }); + // Keep the sampled source and render attachment in separate resources on Intel + // DX12 adapters ([`separate_mip_targets`]): some of their drivers corrupt reduced + // previews when both are mip levels of the same texture, even though the + // subresources do not overlap. Elsewhere render straight into the level, which + // spares a scratch texture and a copy per level on every edit. + let scratch = res.separate_mip_targets.then(|| { + let scratch = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_mip_scratch"), + size: wgpu::Extent3d { width: lw, height: lh, depth_or_array_layers: 1 }, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: self.format, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_SRC, + view_formats: &[], + }); + let view = scratch.create_view(&Default::default()); + (scratch, view) + }); + let (target, load) = match &scratch { + Some((_, view)) => (view, wgpu::LoadOp::Clear(wgpu::Color::TRANSPARENT)), + None => (&t.levels[level], wgpu::LoadOp::Load), + }; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("pc_mip"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + resolve_target: None, + depth_slice: None, + ops: wgpu::Operations { load, store: wgpu::StoreOp::Store }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + pass.set_pipeline(mip_pipeline); + pass.set_bind_group(0, &bg, &[]); + pass.set_scissor_rect(x0, y0, x1 - x0, y1 - y0); + pass.draw(0..3, 0..1); + drop(pass); + if let Some((scratch, _)) = &scratch { + let origin = wgpu::Origin3d { x: x0, y: y0, z: 0 }; + encoder.copy_texture_to_texture( + wgpu::TexelCopyTextureInfo { texture: scratch, mip_level: 0, origin, aspect: wgpu::TextureAspect::All }, + wgpu::TexelCopyTextureInfo { texture: &t.texture, mip_level: level as u32, origin, aspect: wgpu::TextureAspect::All }, + wgpu::Extent3d { width: x1 - x0, height: y1 - y0, depth_or_array_layers: 1 }, + ); + } + } + } + queue.submit([encoder.finish()]); + } +} diff --git a/crates/ui-egui/src/gpu_canvas_mip_tests.rs b/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs similarity index 56% rename from crates/ui-egui/src/gpu_canvas_mip_tests.rs rename to crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs index 82e911bae9..feb0df375e 100644 --- a/crates/ui-egui/src/gpu_canvas_mip_tests.rs +++ b/crates/ui-egui/src/canvas/gpu_canvas/mip_tests.rs @@ -64,6 +64,31 @@ fn canvas_mips_preserve_opaque_pixels_and_partial_updates() { gpu.rs.renderer.write().callback_resources.get_mut::().unwrap().separate_mip_targets = separate; check_mips(&gpu); } + // On the same device (see the top of the file). + canvas_textures_the_gpu_refuses_leave_it_working(&gpu); +} + +/// #4017: a 3 GP canvas's 16 GB of textures failed with Out of Memory, and the uncaptured error +/// disabled the GPU for the session. A refused set is now caught: nothing is kept, the budget +/// drops below it (that canvas goes to the CPU canvas) and the device stays usable. A texture +/// wider than the device allows is refused the same way, synchronously, on any adapter. +fn canvas_textures_the_gpu_refuses_leave_it_working(gpu: &GpuCanvas) { + let max = gpu.rs.device.limits().max_texture_dimension_2d; + let (size, tile) = ([max + 1, 1], max + 1); + let bytes = canvas_texture_bytes(size, tile, texel_bytes(FORMAT)); + { + let mut renderer = gpu.rs.renderer.write(); + let res = renderer.callback_resources.get_mut::().unwrap(); + res.canvas_budget.store(u64::MAX, Ordering::Relaxed); + assert!(DocTextures::new(&gpu.rs.device, res, size, tile, FORMAT).is_none()); + let budget = res.canvas_budget.load(Ordering::Relaxed); + assert!(budget < bytes, "{budget} >= {bytes}"); + } + assert!(gpu.health.is_ok(), "{:?}", gpu.fault()); + // Smaller canvases still get GPU textures. + gpu.upload_buffer_full(3, &Buffer::filled(Rect::from_xywh(0, 0, 64, 64), [0.5, 0.5, 0.5, 1.0]), SampleType::U8); + assert!(gpu.rs.renderer.read().callback_resources.get::().unwrap().docs.contains_key(&3)); + assert!(gpu.health.is_ok(), "{:?}", gpu.fault()); } fn check_mips(gpu: &GpuCanvas) { @@ -102,3 +127,47 @@ fn check_mips(gpu: &GpuCanvas) { } } } + +/// #6418: on AMD's Vulkan driver, a pass that drew only part of a small mip level corrupted texels +/// outside its scissor, at the level's top-left (RX 6900 XT, driver 26.8.1). Whether a texture is +/// hit depends on where the driver placed it, so this paints dabs on canvases of several sizes at +/// both depths and compares every level with a full rebuild of the same pixels. +#[test] +fn partial_updates_match_a_full_rebuild_at_every_mip_level() { + let _gpu = photocraft_testkit::gpu_lock(); + let rs = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| egui_kittest::wgpu::create_render_state(wgpu_setup(), Default::default()))); + let Ok(rs) = rs else { + eprintln!("skipping mip rebuild comparison: no GPU adapter"); + return; + }; + let gpu = GpuCanvas::new(&rs); + for (depth, w, h) in [(SampleType::U8, 255, 255), (SampleType::U8, 1024, 768), (SampleType::F32, 512, 512), (SampleType::F32, 1791, 1083)] { + let mut full = Buffer::filled(Rect::from_xywh(0, 0, w, h), [0.0; 4]); + for (i, px) in full.px.iter_mut().enumerate() { + let (x, y) = (i as u32 % w, i as u32 / w); + *px = [x as f32 / w as f32, y as f32 / h as f32, ((x + y) % 7) as f32 / 7.0, 1.0]; + } + gpu.upload_buffer_full(1, &full, depth); + let dabs = [Rect::from_xywh((w / 2) as i32, (h / 2) as i32, w / 3, h / 3), Rect::from_xywh(w as i32 - 1, h as i32 - 1, 1, 1)]; + for (dab, color) in dabs.into_iter().zip([[1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]]) { + assert!(gpu.upload_buffer_rect(1, &Buffer::filled(dab, color))); + for y in dab.y0..dab.y1 { + for x in dab.x0..dab.x1 { + full.px[y as usize * w as usize + x as usize] = color; + } + } + gpu.upload_buffer_full(2, &full, depth); + for (level, (partial, rebuilt)) in read_mips(&gpu, 1).iter().zip(&read_mips(&gpu, 2)).enumerate() { + let lw = (w >> level).max(1) as usize; + for (i, (a, b)) in partial.iter().zip(rebuilt).enumerate() { + assert!( + a.iter().zip(b).all(|(a, b)| (a - b).abs() < 0.005), + "{depth:?} {w}x{h} dab {dab:?} mip {level} at ({}, {}): {a:?} != {b:?}", + i % lw, + i / lw + ); + } + } + } + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/perf.rs b/crates/ui-egui/src/canvas/gpu_canvas/perf.rs new file mode 100644 index 0000000000..58b12774b2 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/perf.rs @@ -0,0 +1,255 @@ +use super::budget::video_memory; +use super::setup::Refresh; +use super::texels::LayerFit; +use super::wgpu; + +/// Milliseconds since the process (or page) started, from a monotonic clock. +/// +/// `web_time::Instant` is `std::time::Instant` natively and `performance.now()` on wasm, so frame +/// caps, autosave intervals, preference retry backoff, stroke traces and screenshot deadlines all +/// measure durations the same way on both. The old wasm value was a constant 0, which made every +/// interval comparison (`now >= deadline`) permanently false on the web. +pub fn now_ms() -> f64 { + static START: std::sync::OnceLock = std::sync::OnceLock::new(); + START.get_or_init(web_time::Instant::now).elapsed().as_secs_f64() * 1000.0 +} + +/// UI-thread timings (CPU side), exponentially smoothed. +#[derive(Clone, Debug, Default, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Perf { + /// Average CPU time building the UI per frame (ms). + pub ui_ms: f64, + /// Worst UI frame of the last ~second (ms). + pub ui_max_ms: f64, + /// Last canvas refresh, CPU side (ms): the CPU composite on the CPU path, and on the GPU + /// path the cost of *encoding and submitting* the composite — not of running it, which is + /// `gpu_ms`. + pub composite_ms: f64, + /// Last canvas refresh: conversion + upload (ms). + pub upload_ms: f64, + /// Last canvas refresh kind: "full", "rect" or "none". + pub last_refresh: &'static str, + /// Pixels in the last refresh. + pub last_refresh_px: u64, + /// Whether the GPU canvas path is active. + pub gpu: bool, + /// Layer tiles uploaded by the last GPU composite. + pub gpu_uploads: u64, + /// How long the GPU spent executing a recent composite (ms), from timestamp queries read + /// back after the fact. `None` on the CPU compositor, on a device without timestamp queries + /// and on wasm — there is then no honest number to report, and `composite_ms` is not one. + /// Excludes layer-tile uploads and end-of-batch work (see `photocraft_gpu::timing`), so it + /// is a few per cent under the GPU's full share of a warm refresh and further under a cold + /// one. + pub gpu_ms: Option, + /// Pixels of the refresh `gpu_ms` measured, which is a frame or two behind + /// `last_refresh_px` and may be a different size. + pub gpu_ms_px: u64, + /// Why the active view is drawn on the CPU (None = GPU composited): the current + /// [`crate::render_path`] reason as text, written only by `render_path`. + pub gpu_fallback: Option, + /// Adapter, backend, driver and fallback state (Help › System Info). + pub gpu_info: GpuInfo, + /// GPU memory the compositor may hold for layer pages and effect maps (MB; see + /// [`memory_budget`]). + pub gpu_budget_mb: u64, + /// The Memory Usage allowance and the GPU Memory preference (bytes) `gpu_budget_mb` was + /// computed from. + #[serde(skip)] + pub gpu_budget_allowance: (u64, u64), + /// The layer pages the last full refresh needed against the part of the budget they may + /// hold; when they don't fit, edits that redraw the whole document do it on the CPU. + pub gpu_layer_fit: Option, + /// Last command run through the app and its duration (ms). + pub last_command: String, + pub command_ms: f64, + /// Most recent cost of occasional UI work (outline tracing, thumbnails, …), in ms. + pub spans: std::collections::BTreeMap<&'static str, f64>, + #[serde(skip)] + max_decay: f64, + /// This frame's stages, gathered as it runs (see `frame_stages`). + #[serde(skip)] + pub(crate) stage: crate::frame_stages::FrameStages, +} + +/// What the app renders with and why (Help › System Info, Preferences › Performance, the `perf` +/// control-channel output). +#[derive(Clone, Debug, Default, PartialEq, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct GpuInfo { + /// Adapter name ("" before the window opens). + pub adapter: String, + /// wgpu backend of the window's device ("vulkan", "dx12", "metal", "gl", …). + pub backend: String, + /// Device type ("discreteGpu", "integratedGpu", "cpu", …). + pub device_type: String, + /// Driver name and version as the driver reports them. + pub driver: String, + /// The adapter's own memory in MB ([`video_memory`]): discrete GPUs where the platform + /// reports it. It caps the GPU memory budgets. + pub video_memory_mb: Option, + /// `performance.gpuBackend` at launch. + pub preference: String, + /// Every graphics adapter the window could have used, by name (Preferences › Performance › + /// Graphics Adapter lists them). Empty in hosts that don't set it. + pub adapters: Vec, + /// The backend startup chose ("auto", "dx12", "cpu", …, or "env" for `WGPU_BACKEND`). + pub selected: String, + /// Why startup didn't use the preference as is (previous start crashed, `--safe-gpu`, + /// `WGPU_BACKEND`, Intel on Windows). + pub fallback: Option, + /// "gpu" (wgpu canvas and compositor) or "cpu" (CPU compositor, egui textures). + pub canvas: String, + /// Why the GPU canvas was dropped during this session (device lost or a GPU error). + pub lost: Option, + /// Present mode and frame latency ("fifo, latency 2"; launch overrides marked, see the + /// app's `presentation`). Empty in hosts that don't set it. + pub present: String, +} + +impl GpuInfo { + /// Record the adapters the instance found, without duplicates (one GPU shows up once per backend). + pub fn set_available(&mut self, adapters: &[wgpu::Adapter]) { + self.adapters.clear(); + for a in adapters { + let name = a.get_info().name; + if !name.is_empty() && !self.adapters.contains(&name) { + self.adapters.push(name); + } + } + } + + /// Fill the adapter fields from `info`. + pub fn set_adapter(&mut self, info: &wgpu::AdapterInfo) { + self.adapter = info.name.clone(); + self.backend = backend_name(info.backend).to_string(); + self.device_type = match info.device_type { + wgpu::DeviceType::DiscreteGpu => "discreteGpu", + wgpu::DeviceType::IntegratedGpu => "integratedGpu", + wgpu::DeviceType::VirtualGpu => "virtualGpu", + wgpu::DeviceType::Cpu => "cpu", + wgpu::DeviceType::Other => "other", + } + .to_string(); + self.driver = [info.driver.as_str(), info.driver_info.as_str()].iter().filter(|s| !s.is_empty()).copied().collect::>().join(" "); + self.video_memory_mb = video_memory(info).map(|b| b >> 20); + } + + /// Human-readable lines for Help › System Info. + pub fn lines(&self) -> Vec { + let or = |s: &str, d: &str| if s.is_empty() || (s == "auto" && d == "auto") { tl!(d).to_string() } else { s.to_string() }; + let mut v = vec![ + { + let template = "Graphics adapter: {adapter}"; + crate::i18n::fmt(tl!(template), &[("adapter", &or(&self.adapter, "unknown"))]) + }, + { + let template = "Backend: {backend} ({type})"; + crate::i18n::fmt(tl!(template), &[("backend", &or(&self.backend, "unknown")), ("type", &or(&self.device_type, "unknown type"))]) + }, + { + let template = "Driver: {driver}"; + crate::i18n::fmt(tl!(template), &[("driver", &or(&self.driver, "unknown"))]) + }, + ]; + // The card's own memory caps the GPU budgets, so it explains a huge canvas falling back + // to the CPU. Only discrete cards report one (integrated GPUs share system memory). + if let Some(mb) = self.video_memory_mb { + let template = "Graphics memory: {size}"; + v.push(crate::i18n::fmt(tl!(template), &[("size", &crate::chrome_ui::fmt_bytes(mb.saturating_mul(1 << 20)))])); + } + v.extend([ + { + let template = "GPU backend preference: {backend}"; + crate::i18n::fmt(tl!(template), &[("backend", &or(&self.preference, "auto"))]) + }, + { + let template = "Selected at launch: {backend}"; + crate::i18n::fmt(tl!(template), &[("backend", &or(&self.selected, "auto"))]) + }, + { + let template = "Image compositor: {processor}"; + crate::i18n::fmt(tl!(template), &[("processor", if self.canvas == "gpu" { "GPU" } else { "CPU" })]) + }, + { + let template = "Window renderer: {renderer}"; + let renderer = if self.device_type == "cpu" { + "Software adapter" + } else if self.adapter.is_empty() { + "Unknown" + } else { + "Graphics adapter" + }; + crate::i18n::fmt(tl!(template), &[("renderer", tl!(renderer))]) + }, + ]); + if let Some(f) = &self.fallback { + v.push({ + let template = "Fallback: {reason}"; + crate::i18n::fmt(tl!(template), &[("reason", f)]) + }); + } + if let Some(l) = &self.lost { + v.push({ + let template = "This session: {reason}"; + crate::i18n::fmt(tl!(template), &[("reason", l)]) + }); + } + v + } +} + +/// The preference name of a wgpu backend. +pub fn backend_name(b: wgpu::Backend) -> &'static str { + match b { + wgpu::Backend::Vulkan => "vulkan", + wgpu::Backend::Dx12 => "dx12", + wgpu::Backend::Metal => "metal", + wgpu::Backend::Gl => "gl", + wgpu::Backend::BrowserWebGpu => "webgpu", + wgpu::Backend::Noop => "noop", + } +} + +impl Perf { + pub fn record(&mut self, kind: &'static str, px: u64, composite_ms: f64, upload_ms: f64) { + self.stage.refresh(kind, px, composite_ms, upload_ms); + self.last_refresh = kind; + self.last_refresh_px = px; + self.composite_ms = composite_ms; + self.upload_ms = upload_ms; + if !kind.starts_with("gpu") { + self.gpu_uploads = 0; + // The CPU compositor did this one: there is no GPU time, and the last GPU + // refresh's would be read as this refresh's. + self.gpu_ms = None; + self.gpu_ms_px = 0; + } + } + + /// Record a canvas refresh and any GPU measurement that came back with it. + pub fn record_refresh(&mut self, r: &Refresh) { + self.record(r.kind, r.px, r.composite_ms, r.upload_ms); + if let Some(t) = r.gpu { + self.record_gpu(t); + } + } + + /// Record a GPU composite measurement that came back (see [`photocraft_gpu::GpuTiming`]). + pub fn record_gpu(&mut self, t: photocraft_gpu::GpuTiming) { + self.gpu_ms = Some(t.ms); + self.gpu_ms_px = t.px; + } + pub fn span(&mut self, name: &'static str, ms: f64) { + self.spans.insert(name, (ms * 100.0).round() / 100.0); + } + pub fn frame(&mut self, ms: f64) { + self.ui_ms = if self.ui_ms == 0.0 { ms } else { self.ui_ms * 0.9 + ms * 0.1 }; + self.max_decay += 1.0; + if ms > self.ui_max_ms || self.max_decay > 60.0 { + self.ui_max_ms = ms; + self.max_decay = 0.0; + } + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/resources.rs b/crates/ui-egui/src/canvas/gpu_canvas/resources.rs new file mode 100644 index 0000000000..721377e928 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/resources.rs @@ -0,0 +1,383 @@ +use super::budget::{canvas_budget, physical_memory}; +use super::doc_textures::init_buffer; +use super::shaders::{CANVAS_WGSL, ENCODE_WGSL, MIP_WGSL}; +use super::texels::LayerFit; +use super::{FORMAT, FORMAT_HIGH, HashMap, NonZeroU64, TILE_UNIFORM_SIZE, VIEW_FLOATS, VIEW_UNIFORM_SIZE, wgpu}; + +pub(super) struct Resources { + pub(super) view_bgl: wgpu::BindGroupLayout, + pub(super) tile_bgl: wgpu::BindGroupLayout, + pub(super) shadow_pipeline: wgpu::RenderPipeline, + pub(super) tile_pipeline: wgpu::RenderPipeline, + pub(super) sampler: wgpu::Sampler, + pub(super) mip_bgl: wgpu::BindGroupLayout, + mip_pipeline: wgpu::RenderPipeline, + /// `mip_pipeline` and `encode_pipeline` for `Rgba16Float` tiles (when the adapter supports them). + mip_pipeline_high: Option, + encode_pipeline_high: Option, + pub(super) mip_sampler: wgpu::Sampler, + pub(super) docs: HashMap, + pub(super) views: HashMap, + pub(super) out_linear: bool, + /// Build each mip level in a scratch texture and copy it back instead of rendering into + /// the level directly (see [`separate_mip_targets`]). + pub(super) separate_mip_targets: bool, + /// The wgpu layer compositor (created on first use). + pub(super) compositor: Option, + /// Why the wgpu compositor couldn't be created (then the CPU compositor is used). + pub(super) compositor_failed: Option, + /// The device's health: the paint callback issues no GPU work once it's lost. + pub(super) health: photocraft_gpu::DeviceHealth, + /// GPU memory the compositor may hold (`None`: its default), and the document area the + /// view shows; applied before every composite. + pub(super) compositor_budget: Option, + /// The layer pages the last full (or large) refresh needed and the budget they had to fit + /// (bytes): when they don't fit, full refreshes go to the CPU ([`OVER_BUDGET`]). + pub(super) layer_fit: Option, + pub(super) compositor_cache_budget: Option, + pub(super) compositor_focus: Option, + /// GPU bytes one document's display texture set may take (see [`canvas_budget`]). + pub(super) canvas_budget: std::sync::Arc, + pub(super) encode_bgl: wgpu::BindGroupLayout, + encode_pipeline: wgpu::RenderPipeline, + pub(super) lut_bgl: wgpu::BindGroupLayout, + /// Display LUTs per (document, display) (monitor profile, Proof Colors / Gamut Warning); + /// `identity_lut` otherwise. + pub(super) luts: HashMap<(u64, u32), wgpu::BindGroup>, + /// Signatures belong to their renderer resources, including identity transforms (mode 0). + pub(super) display_lut_signatures: HashMap<(u64, u32), (u64, u8)>, + pub(super) identity_lut: wgpu::BindGroup, + /// Transparency checkerboard and gamut warning colours (Preferences › Transparency & Gamut). + pub(super) style: CanvasStyle, +} + +/// Canvas colours from Preferences › Transparency & Gamut. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct CanvasStyle { + /// Checker square side in points (0 = no checkerboard: transparency shows white). + pub checker_square: f32, + pub checker_light: [f32; 3], + pub checker_dark: [f32; 3], + pub gamut_color: [f32; 3], + /// Gamut warning opacity 0..1. + pub gamut_opacity: f32, +} + +impl Default for CanvasStyle { + fn default() -> Self { + Self { checker_square: 8.0, checker_light: [1.0; 3], checker_dark: [0.8; 3], gamut_color: [0.5; 3], gamut_opacity: 1.0 } + } +} + +/// A display transform: either the existing 3D LUT or separate full-float shapers and +/// matrix. Pack curves into short rows to stay within WebGL2's texture-size limits. +pub(super) fn lut_bind_group( + device: &wgpu::Device, + queue: &wgpu::Queue, + bgl: &wgpu::BindGroupLayout, + size: u32, + bytes: &[u8], + shaper: Option<&photocraft_cms::GpuMatrixShaper>, +) -> wgpu::BindGroup { + let extent = wgpu::Extent3d { width: size, height: size, depth_or_array_layers: size }; + let texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_display_lut"), + size: extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D3, + format: FORMAT, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + bytes, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(size * 4), rows_per_image: Some(size) }, + extent, + ); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let (width, height, curves, matrix) = if let Some(s) = shaper { + let width = 1024usize; + let count = s.input_table().len() + s.output_table().len(); + let height = count.div_ceil(width); + let mut curves = Vec::with_capacity(width * height * 16); + for value in s.input_table().iter().chain(s.output_table()).flatten() { + curves.extend_from_slice(&value.to_le_bytes()); + } + curves.resize(width * height * 16, 0); + (width as u32, height as u32, curves, s.matrix_rows()) + } else { + (1, 1, vec![0; 16], [[0.0; 4]; 3]) + }; + let extent = wgpu::Extent3d { width, height, depth_or_array_layers: 1 }; + let curves_texture = device.create_texture(&wgpu::TextureDescriptor { + label: Some("pc_display_shapers"), + size: extent, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + // textureLoad + explicit interpolation needs no optional float-filtering feature. + format: wgpu::TextureFormat::Rgba32Float, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }); + queue.write_texture( + wgpu::TexelCopyTextureInfo { texture: &curves_texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All }, + &curves, + wgpu::TexelCopyBufferLayout { offset: 0, bytes_per_row: Some(width * 16), rows_per_image: Some(height) }, + extent, + ); + let curves_view = curves_texture.create_view(&wgpu::TextureViewDescriptor::default()); + let matrix_bytes: Vec = matrix.iter().flatten().flat_map(|v| v.to_le_bytes()).collect(); + let matrix_buffer = init_buffer(device, "pc_display_matrix", &matrix_bytes, wgpu::BufferUsages::UNIFORM); + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("pc_display_lut"), + layout: bgl, + entries: &[ + wgpu::BindGroupEntry { binding: 0, resource: wgpu::BindingResource::TextureView(&view) }, + wgpu::BindGroupEntry { binding: 1, resource: wgpu::BindingResource::TextureView(&curves_view) }, + wgpu::BindGroupEntry { binding: 2, resource: matrix_buffer.as_entire_binding() }, + ], + }) +} + +/// The 2³ identity LUT (unused unless a document has none and `display` is set). +pub(super) fn identity_lut_bytes() -> Vec { + (0..8).flat_map(|i| [if i & 1 != 0 { 255 } else { 0 }, if i & 2 != 0 { 255 } else { 0 }, if i & 4 != 0 { 255 } else { 0 }, 0]).collect() +} + +pub(super) struct ViewGpu { + pub(super) buffer: wgpu::Buffer, + pub(super) bind_group: wgpu::BindGroup, + pub(super) frame_data: [f32; VIEW_FLOATS], +} + +pub(super) struct DocTextures { + pub(super) size: [u32; 2], + pub(super) format: wgpu::TextureFormat, + pub(super) tiles: Vec, +} + +pub(super) struct Tile { + /// Document-pixel rect: x, y, w, h. + pub(super) rect: [u32; 4], + pub(super) texture: wgpu::Texture, + pub(super) levels: Vec, + pub(super) bind_group: wgpu::BindGroup, + pub(super) _uniform: wgpu::Buffer, +} + +fn uniform_entry(binding: u32, size: u64, vis: wgpu::ShaderStages) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: vis, + ty: wgpu::BindingType::Buffer { ty: wgpu::BufferBindingType::Uniform, has_dynamic_offset: false, min_binding_size: NonZeroU64::new(size) }, + count: None, + } +} + +fn texture_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + }, + count: None, + } +} + +fn sampler_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { + wgpu::BindGroupLayoutEntry { + binding, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + } +} + +pub(super) fn f32_bytes(v: &[f32]) -> Vec { + v.iter().flat_map(|f| f.to_ne_bytes()).collect() +} + +fn pipeline( + device: &wgpu::Device, + label: &str, + layout: &wgpu::PipelineLayout, + module: &wgpu::ShaderModule, + (vs, fs): (&str, &str), + format: wgpu::TextureFormat, + blend: Option, +) -> wgpu::RenderPipeline { + device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some(label), + layout: Some(layout), + vertex: wgpu::VertexState { module, entry_point: Some(vs), buffers: &[], compilation_options: Default::default() }, + primitive: wgpu::PrimitiveState { topology: wgpu::PrimitiveTopology::TriangleList, ..Default::default() }, + depth_stencil: None, + multisample: wgpu::MultisampleState { count: 1, mask: !0, alpha_to_coverage_enabled: false }, + fragment: Some(wgpu::FragmentState { + module, + entry_point: Some(fs), + targets: &[Some(wgpu::ColorTargetState { format, blend, write_mask: wgpu::ColorWrites::ALL })], + compilation_options: Default::default(), + }), + multiview_mask: None, + cache: None, + }) +} + +impl Resources { + pub(super) fn new(device: &wgpu::Device, queue: &wgpu::Queue, target: wgpu::TextureFormat, high: bool) -> Self { + let module = + device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_canvas"), source: wgpu::ShaderSource::Wgsl(CANVAS_WGSL.into()) }); + let vis = wgpu::ShaderStages::VERTEX | wgpu::ShaderStages::FRAGMENT; + let view_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_canvas_view"), + entries: &[uniform_entry(0, VIEW_UNIFORM_SIZE, vis), sampler_entry(1)], + }); + let tile_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_canvas_tile"), + entries: &[texture_entry(0), uniform_entry(1, TILE_UNIFORM_SIZE, vis)], + }); + let lut_entry = wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D3, + }, + count: None, + }; + let curves_entry = wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: false }, + view_dimension: wgpu::TextureViewDimension::D2, + }, + count: None, + }; + let lut_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_display_lut"), + entries: &[lut_entry, curves_entry, uniform_entry(2, 48, wgpu::ShaderStages::FRAGMENT)], + }); + let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("pc_canvas"), + bind_group_layouts: &[Some(&view_bgl), Some(&tile_bgl), Some(&lut_bgl)], + immediate_size: 0, + }); + let shadow_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("pc_canvas_shadow"), + bind_group_layouts: &[Some(&view_bgl)], + immediate_size: 0, + }); + // Premultiplied "over", with egui's alpha rule (keeps a transparent window's alpha sane). + let blend = wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add, + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::OneMinusDstAlpha, + dst_factor: wgpu::BlendFactor::One, + operation: wgpu::BlendOperation::Add, + }, + }; + let shadow_pipeline = pipeline(device, "pc_canvas_shadow", &shadow_layout, &module, ("vs_shadow", "fs_shadow"), target, Some(blend)); + let tile_pipeline = pipeline(device, "pc_canvas_tile", &layout, &module, ("vs_tile", "fs_tile"), target, Some(blend)); + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("pc_canvas_trilinear"), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + mipmap_filter: wgpu::MipmapFilterMode::Linear, + ..Default::default() + }); + + let mip_module = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_mip"), source: wgpu::ShaderSource::Wgsl(MIP_WGSL.into()) }); + let mip_bgl = + device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("pc_mip"), entries: &[texture_entry(0), sampler_entry(1)] }); + let mip_layout = + device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("pc_mip"), bind_group_layouts: &[Some(&mip_bgl)], immediate_size: 0 }); + let mip_pipeline = pipeline(device, "pc_mip", &mip_layout, &mip_module, ("vs", "fs"), FORMAT, None); + let mip_pipeline_high = high.then(|| pipeline(device, "pc_mip_16f", &mip_layout, &mip_module, ("vs", "fs"), FORMAT_HIGH, None)); + let mip_sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("pc_mip"), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + ..Default::default() + }); + // Compositor output (straight RGBA32F chunk) -> premultiplied RGBA8 display tile. + let encode_module = + device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("pc_encode"), source: wgpu::ShaderSource::Wgsl(ENCODE_WGSL.into()) }); + let unfilterable = wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + multisampled: false, + sample_type: wgpu::TextureSampleType::Float { filterable: false }, + view_dimension: wgpu::TextureViewDimension::D2, + }, + count: None, + }; + let encode_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("pc_encode"), + entries: &[unfilterable, uniform_entry(1, 16, wgpu::ShaderStages::FRAGMENT)], + }); + let encode_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("pc_encode"), + bind_group_layouts: &[Some(&encode_bgl)], + immediate_size: 0, + }); + let encode_pipeline = pipeline(device, "pc_encode", &encode_layout, &encode_module, ("vs", "fs"), FORMAT, None); + let encode_pipeline_high = high.then(|| pipeline(device, "pc_encode_16f", &encode_layout, &encode_module, ("vs", "fs"), FORMAT_HIGH, None)); + let identity_lut = lut_bind_group(device, queue, &lut_bgl, 2, &identity_lut_bytes(), None); + Self { + lut_bgl, + luts: HashMap::new(), + display_lut_signatures: HashMap::new(), + identity_lut, + style: CanvasStyle::default(), + view_bgl, + tile_bgl, + shadow_pipeline, + tile_pipeline, + sampler, + mip_bgl, + mip_pipeline, + mip_pipeline_high, + encode_pipeline_high, + mip_sampler, + docs: HashMap::new(), + views: HashMap::new(), + out_linear: target.is_srgb(), + separate_mip_targets: false, + compositor: None, + compositor_failed: None, + health: photocraft_gpu::DeviceHealth::new(), + compositor_budget: None, + layer_fit: None, + compositor_cache_budget: None, + compositor_focus: None, + canvas_budget: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(canvas_budget(physical_memory(), None))), + encode_bgl, + encode_pipeline, + } + } + + /// The pipeline encoding compositor output into a `format` tile. + pub(super) fn encode_pipeline(&self, format: wgpu::TextureFormat) -> Option<&wgpu::RenderPipeline> { + if format == FORMAT_HIGH { self.encode_pipeline_high.as_ref() } else { Some(&self.encode_pipeline) } + } + + /// The mip-chain pipeline for `format` tiles. + pub(super) fn mip_pipeline(&self, format: wgpu::TextureFormat) -> Option<&wgpu::RenderPipeline> { + if format == FORMAT_HIGH { self.mip_pipeline_high.as_ref() } else { Some(&self.mip_pipeline) } + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/setup.rs b/crates/ui-egui/src/canvas/gpu_canvas/setup.rs new file mode 100644 index 0000000000..b9d3f98bbe --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/setup.rs @@ -0,0 +1,118 @@ +use super::texels::texels; +use super::{egui_wgpu, wgpu}; +use crate::render_path::CpuReason; + +/// A CPU composite as the canvas texture stores it (`display`'s encoding; as is without one). +pub(super) fn texture_buffer<'a>( + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + buf: &'a photocraft_compose::Buffer, +) -> std::borrow::Cow<'a, photocraft_compose::Buffer> { + match display { + Some(d) => d.texture_buffer(buf), + None => std::borrow::Cow::Borrowed(buf), + } +} + +/// A CPU composite `buf` as texels of a `format` canvas texture, in `display`'s encoding: what +/// [`GpuCanvas::upload_rect`] takes. Runs anywhere (a redraw's worker converts its own pieces). +pub fn display_texels( + format: wgpu::TextureFormat, + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + buf: &photocraft_compose::Buffer, +) -> Vec { + texels(format, &texture_buffer(display, buf).px) +} + +/// What a [`GpuCanvas::refresh`] did. +#[derive(Clone, Debug, Default)] +pub struct Refresh { + /// "gpu-rect", "gpu-full" (wgpu compositor), "rect" or "full" (CPU compositor); "lost" or + /// [`REFRESH_OVER_BUDGET`] when nothing was drawn and the caller should use the CPU canvas. + pub kind: &'static str, + /// Pixels refreshed. + pub px: u64, + pub composite_ms: f64, + pub upload_ms: f64, + /// Layer tiles the wgpu compositor uploaded. + pub uploads: u64, + /// How long the GPU spent executing a recent wgpu composite, once its timestamps came back + /// (a frame or two after that refresh, so usually an earlier one than this). It carries its + /// own pixel count and is never this refresh's unless they match. `None` when the device has + /// no timestamp queries, on the CPU compositor, and on wasm: no number at all is reported + /// rather than `composite_ms`, which measures encoding and submitting, not executing. It + /// leaves out layer-tile uploads (`uploads` above counts them), so a first refresh of a + /// document under-reports by more than the few per cent of a warm one. + pub gpu: Option, + /// Why the wgpu compositor wasn't used. + pub fallback: Option, +} + +/// How the app creates its wgpu device: egui's defaults clamped to the adapter's supported limits, +/// with the adapter's own texture and buffer sizes rather than egui's 8192 px cap, so large +/// documents stay on the GPU compositor. +pub fn wgpu_setup() -> egui_wgpu::WgpuSetup { + let mut setup = egui_wgpu::WgpuSetup::without_display_handle(); + use_adapter_limits(&mut setup); + setup +} + +/// Make `setup` request the adapter's own texture and buffer size limits instead of egui's +/// default 8192 px cap (which sent every document wider or taller than 8192 px to the CPU +/// compositor). Other limits use egui's defaults (WebGL2's on GL), clamped to adapter support; +/// features stay egui's default. +pub fn use_adapter_limits(setup: &mut egui_wgpu::WgpuSetup) { + use_adapter_limits_with(setup, |_| {}); +} + +/// [`use_adapter_limits`], calling `on_adapter` with the chosen adapter just before the device +/// is requested (the desktop app records it in its crash-safe startup marker). +pub fn use_adapter_limits_with(setup: &mut egui_wgpu::WgpuSetup, on_adapter: impl Fn(&wgpu::Adapter) + Send + Sync + 'static) { + if let egui_wgpu::WgpuSetup::CreateNew(create) = setup { + create.device_descriptor = std::sync::Arc::new(move |adapter| { + on_adapter(adapter); + device_descriptor(adapter) + }); + } +} + +/// How the app asks for its device: egui's defaults, the adapter's texture and buffer size +/// limits ([`device_limits`]), and timestamp queries where the adapter has them, so the canvas +/// can report how long the GPU actually spends compositing ([`photocraft_gpu::GpuTimer`]). +/// A bench or test that builds its own device should use this rather than [`device_limits`] +/// alone, or it measures a device the app never creates. (`bevel_bench`, `fx_bench` and +/// `interactive_bench` still call [`device_limits`] directly and so report no GPU time.) +pub fn device_descriptor(adapter: &wgpu::Adapter) -> wgpu::DeviceDescriptor<'static> { + wgpu::DeviceDescriptor { + label: Some("photocraft wgpu device"), + required_limits: device_limits(adapter), + required_features: timing_features(adapter), + ..Default::default() + } +} + +/// The timestamp-query features when the adapter supports all of them, else none: asking for a +/// feature an adapter lacks fails device creation, and a canvas without GPU timing is far better +/// than no canvas. +/// +/// `PHOTOCRAFT_NO_GPU_TIMING` suppresses the request as well as the timer +/// ([`photocraft_gpu::Compositor::measure_gpu_time`]), so the variable also rescues a driver +/// that advertises the features and then fails or misbehaves on device creation — not only one +/// whose timestamps come back wrong. +pub fn timing_features(adapter: &wgpu::Adapter) -> wgpu::Features { + let want = photocraft_gpu::timing::FEATURES; + if std::env::var_os("PHOTOCRAFT_NO_GPU_TIMING").is_some() { + return wgpu::Features::empty(); + } + if adapter.features().contains(want) { want } else { wgpu::Features::empty() } +} + +/// egui's base limits (WebGL2's on GL), clamped to adapter support, with its maximum texture and buffer sizes. +pub fn device_limits(adapter: &wgpu::Adapter) -> wgpu::Limits { + device_limits_for(adapter.get_info().backend, adapter.limits()) +} + +pub(super) fn device_limits_for(backend: wgpu::Backend, supported: wgpu::Limits) -> wgpu::Limits { + let base = if backend == wgpu::Backend::Gl { wgpu::Limits::downlevel_webgl2_defaults() } else { wgpu::Limits::default() }; + let base = base.or_worse_values_from(&supported); + wgpu::Limits { max_texture_dimension_2d: supported.max_texture_dimension_2d, max_buffer_size: supported.max_buffer_size, ..base } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/shaders.rs b/crates/ui-egui/src/canvas/gpu_canvas/shaders.rs new file mode 100644 index 0000000000..c29376a936 --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/shaders.rs @@ -0,0 +1,273 @@ +pub(super) const CANVAS_WGSL: &str = r#" +struct View { + a: vec4, // screen_w, screen_h, scale (device px per doc px), pixels_per_point + b: vec4, // doc origin x, y (device px), doc w, h (doc px) + c: vec4, // filter mode, lod, grid alpha, checker square (device px) + d: vec4, // pattern preview offset x, y (doc px), display (0 none, 1 LUT, 2 LUT + gamut, 3 matrix/shaper), output linear + e: vec4, // checker light rgb, gamut warning opacity + f: vec4, // checker dark rgb, 32-bit preview gain (2^exposure; 0 = off) + g: vec4, // gamut warning rgb, 32-bit preview 1 / gamma + h: vec4, // cos, sin, center_doc_x, center_doc_y +}; +struct Tile { r: vec4 }; // x, y, w, h in doc px + +@group(0) @binding(0) var view: View; +@group(0) @binding(1) var samp: sampler; +@group(1) @binding(0) var tex: texture_2d; +@group(1) @binding(1) var tile: Tile; +@group(2) @binding(0) var lut: texture_3d; +@group(2) @binding(1) var display_curves: texture_2d; +struct DisplayMatrix { r: vec4, g: vec4, b: vec4 }; +@group(2) @binding(2) var display_matrix: DisplayMatrix; + +fn curve_entry(index: u32) -> vec3 { + let width = textureDimensions(display_curves).x; + return textureLoad(display_curves, vec2(i32(index % width), i32(index / width)), 0).rgb; +} + +fn display_curve(c: vec3, offset: u32, intervals: f32) -> vec3 { + let p = clamp(c, vec3(0.0), vec3(1.0)) * intervals; + let lo = vec3(floor(p)); + let hi = min(lo + vec3(1u), vec3(u32(intervals))); + let f = fract(p); + return vec3( + mix(curve_entry(offset + lo.r).r, curve_entry(offset + hi.r).r, f.r), + mix(curve_entry(offset + lo.g).g, curve_entry(offset + hi.g).g, f.g), + mix(curve_entry(offset + lo.b).b, curve_entry(offset + hi.b).b, f.b) + ); +} + +fn matrix_display(c: vec3) -> vec3 { + // Layout matches cms::GpuMatrixShaper: 4097 input entries, then 4096 inverse-curve + // entries indexed by the fourth root. This preserves near-black precision without + // interpolating across clipped corners of a coarse RGB cube. + let v = vec4(display_curve(c, 0u, 4096.0), 1.0); + let linear = vec3(dot(display_matrix.r, v), dot(display_matrix.g, v), dot(display_matrix.b, v)); + let u = sqrt(sqrt(clamp(linear, vec3(0.0), vec3(1.0)))); + return display_curve(u, 4097u, 4095.0); +} + +struct VOut { @builtin(position) pos: vec4 }; + +fn corner(i: u32) -> vec2 { + var c = array, 6>(vec2(0.0, 0.0), vec2(1.0, 0.0), vec2(0.0, 1.0), vec2(0.0, 1.0), vec2(1.0, 0.0), vec2(1.0, 1.0)); + return c[i]; +} + +fn px_to_clip(p: vec2) -> vec4 { + let n = p / view.a.xy * 2.0 - 1.0; + return vec4(n.x, -n.y, 0.0, 1.0); +} + +fn screen_center() -> vec2 { + return view.b.xy + view.h.zw * view.a.z; +} + +fn doc_to_px(d: vec2) -> vec2 { + let cs = view.h.xy; + let q = d - view.h.zw; + let r = vec2(q.x * cs.x - q.y * cs.y, q.x * cs.y + q.y * cs.x); + return screen_center() + r * view.a.z; +} + +fn px_to_doc(p: vec2) -> vec2 { + let cs = view.h.xy; + let q = (p - screen_center()) / view.a.z; + return vec2(q.x * cs.x + q.y * cs.y, -q.x * cs.y + q.y * cs.x) + view.h.zw; +} + +fn doc_min() -> vec2 { return view.b.xy; } +fn doc_max() -> vec2 { return view.b.xy + view.b.zw * view.a.z; } + +fn srgb_to_linear(c: vec3) -> vec3 { + let lo = c / 12.92; + let hi = pow((c + 0.055) / 1.055, vec3(2.4)); + return select(hi, lo, c <= vec3(0.04045)); +} + +fn linear_to_srgb(c: vec3) -> vec3 { + let hi = 1.055 * pow(max(c, vec3(1e-12)), vec3(1.0 / 2.4)) - 0.055; + return select(hi, c * 12.92, c <= vec3(0.0031308)); +} + +// --- drop shadow --------------------------------------------------------------------------- + +fn erf2(x: vec2) -> vec2 { + let s = sign(x); + let a = abs(x); + var t = 1.0 + (0.278393 + (0.230389 + 0.078108 * (a * a)) * a) * a; + t = t * t; + return s - s / (t * t); +} + +// Coverage of the box [lo, hi] blurred by a Gaussian of std-dev `sigma`, at point p. +fn box_blur(p: vec2, lo: vec2, hi: vec2, sigma: f32) -> f32 { + let k = 0.70710678 / sigma; + let q = (erf2((p - lo) * k) - erf2((p - hi) * k)) * 0.5; + return q.x * q.y; +} + +@vertex +fn vs_shadow(@builtin(vertex_index) vi: u32) -> VOut { + let pad = 64.0 * view.a.w; + let c0 = doc_to_px(vec2(0.0, 0.0)); + let c1 = doc_to_px(vec2(view.b.z, 0.0)); + let c2 = doc_to_px(vec2(view.b.z, view.b.w)); + let c3 = doc_to_px(vec2(0.0, view.b.w)); + let lo = min(min(c0, c1), min(c2, c3)) - vec2(pad); + let hi = max(max(c0, c1), max(c2, c3)) + vec2(pad); + let p = mix(lo, hi, corner(vi)); + return VOut(px_to_clip(p)); +} + +@fragment +fn fs_shadow(in: VOut) -> @location(0) vec4 { + let p = in.pos.xy; + let d = px_to_doc(p); + if (all(d >= vec2(0.0)) && all(d < view.b.zw)) { + discard; + } + let p_align = view.b.xy + d * view.a.z; + let lo = doc_min(); + let hi = doc_max(); + let u = view.a.w; + let ambient = box_blur(p_align - vec2(0.0, 7.0 * u), lo, hi, 16.0 * u) * 0.40; + let contact = box_blur(p_align - vec2(0.0, 0.75 * u), lo, hi, 1.25 * u) * 0.30; + let a = 1.0 - (1.0 - ambient) * (1.0 - contact); + return vec4(0.0, 0.0, 0.0, a); +} + +// --- document tiles ------------------------------------------------------------------------ + +@vertex +fn vs_tile(@builtin(vertex_index) vi: u32) -> VOut { + // A View › Pattern Preview copy sits a whole number of document sizes away (view.d.xy). + let p = doc_to_px(view.d.xy + tile.r.xy + tile.r.zw * corner(vi)); + return VOut(px_to_clip(p)); +} + +// Cells are screen-sized (view.c.w device px) but anchored to the document's top-left corner, so +// the pattern moves with the image as it is panned or zoomed. `d` is in document pixels (each +// Pattern Preview copy has the same cells). +fn checker(d: vec2) -> vec3 { + if (view.c.w <= 0.0) { + return vec3(1.0); + } + let c = floor(d * view.a.z / view.c.w); + let odd = fract((c.x + c.y) * 0.5) > 0.25; + return select(view.e.xyz, view.f.xyz, odd); +} + +@fragment +fn fs_tile(in: VOut) -> @location(0) vec4 { + let p = in.pos.xy; + let scale = view.a.z; + let d = px_to_doc(p) - view.d.xy; // document pixel coordinates (within this copy) + let size = tile.r.zw; + let t = d - tile.r.xy; // texel coordinates within this tile + let mode = view.c.x; + var col: vec4; + if (mode < 0.5) { + // Zoomed out: trilinear through the mip chain. + col = textureSampleLevel(tex, samp, t / size, view.c.y); + } else if (mode < 1.5) { + col = textureSampleLevel(tex, samp, t / size, 0.0); + } else { + // Crisp texels with a one-device-pixel anti-aliased edge ("sharp bilinear"). + let tt = t - 0.5; + let i = floor(tt); + let f = clamp((tt - i - 0.5) * scale + 0.5, vec2(0.0), vec2(1.0)); + col = textureSampleLevel(tex, samp, (i + 0.5 + f) / size, 0.0); + } + if (view.f.w > 0.0 && col.a > 0.0) { + // View › 32-bit Preview Options: exposure and gamma in linear light on the sRGB-encoded + // texture value, before the display LUT. A float canvas keeps values above 1.0, so they + // are brought into range by the exposure instead of clipped. + let lin = srgb_to_linear(max(col.rgb / col.a, vec3(0.0))) * view.f.w; + let v = linear_to_srgb(min(pow(max(lin, vec3(1e-12)), vec3(view.g.w)), vec3(1.0))); + col = vec4(v * col.a, col.a); + } + if (view.d.z > 0.5 && col.a > 0.0) { + // Colour management, Proof Colors, Gamut Warning: the document's display LUT (alpha flags + // out-of-gamut colours). + let c = clamp(col.rgb / col.a, vec3(0.0), vec3(1.0)); + var shown: vec3; + if (view.d.z > 2.5) { + shown = matrix_display(c); + } else { + let n = f32(textureDimensions(lut).x); + let l = textureSampleLevel(lut, samp, (c * (n - 1.0) + 0.5) / n, 0.0); + shown = select(l.rgb, mix(l.rgb, view.g.xyz, view.e.w), view.d.z > 1.5 && l.a > 0.5); + } + col = vec4(shown * col.a, col.a); + } + var rgb = col.rgb + checker(d) * (1.0 - col.a); + // The pixel grid is drawn over pixels with content, not over empty checker. + let grid = view.c.z * col.a; + if (grid > 0.0) { + let e = fract(d) * scale; + if (e.x < 1.0 || e.y < 1.0) { + // White over dark pixels, a weaker black over light ones, cross-faded by luma so a + // mid-tone image never flips line colour from one cell to the next (pixel_grid.rs + // `light_share`: same band, same numbers). + let t = smoothstep(0.35, 0.75, dot(rgb, vec3(0.299, 0.587, 0.114))); + let white = grid * (1.0 - t); + let black = grid * 0.64 * t; + rgb = rgb * (1.0 - white - black) + vec3(white); + } + } + if (view.d.w > 0.5) { + rgb = srgb_to_linear(rgb); + } + return vec4(rgb, 1.0); +} +"#; + +pub(super) const ENCODE_WGSL: &str = r#" +@group(0) @binding(0) var acc: texture_2d; +@group(0) @binding(1) var offset: vec4; // x, y, encode sRGB, keep values above 1.0 + +@vertex +fn vs(@builtin(vertex_index) vi: u32) -> @builtin(position) vec4 { + let uv = vec2(f32((vi << 1u) & 2u), f32(vi & 2u)); + return vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0); +} + +fn srgb_encode(c: vec3) -> vec3 { + return select(1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, c * 12.92, c <= vec3(0.0031308)); +} + +@fragment +fn fs(@builtin(position) pos: vec4) -> @location(0) vec4 { + let c = textureLoad(acc, vec2(floor(pos.xy)) - offset.xy, 0); + let a = clamp(c.a, 0.0, 1.0); + var rgb = max(c.rgb, vec3(0.0)); + if (offset.w == 0) { + rgb = min(rgb, vec3(1.0)); + } + if (offset.z != 0) { + // Linear composite: store its sRGB encoding (8-bit precision where it matters). + rgb = srgb_encode(rgb); + } + return vec4(rgb * a, a); +} +"#; + +pub(super) const MIP_WGSL: &str = r#" +@group(0) @binding(0) var src: texture_2d; +@group(0) @binding(1) var samp: sampler; + +struct V { @builtin(position) pos: vec4, @location(0) uv: vec2 }; + +@vertex +fn vs(@builtin(vertex_index) vi: u32) -> V { + let uv = vec2(f32((vi << 1u) & 2u), f32(vi & 2u)); + return V(vec4(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0), uv); +} + +@fragment +fn fs(in: V) -> @location(0) vec4 { + // A bilinear tap at the destination texel centre averages the 2×2 source block. + return textureSampleLevel(src, samp, in.uv, 0.0); +} +"#; diff --git a/crates/ui-egui/src/canvas/gpu_canvas/tests.rs b/crates/ui-egui/src/canvas/gpu_canvas/tests.rs new file mode 100644 index 0000000000..74305c024b --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/tests.rs @@ -0,0 +1,399 @@ +use super::*; + +/// A GPU measurement arrives a frame or two after its refresh, so most GPU refreshes carry +/// none; the last one reported must stand until a newer one lands. A CPU refresh is +/// different: there is no GPU time at all then, and keeping the old number would show it as +/// this refresh's. Runs anywhere — the GPU timing tests need hardware and CI has none. +#[test] +fn a_gpu_measurement_stands_until_replaced_and_a_cpu_refresh_clears_it() { + let timing = |ms, px| photocraft_gpu::GpuTiming { ms, px, spans: 1 }; + let refresh = |kind, gpu| Refresh { kind, gpu, ..Default::default() }; + let mut perf = Perf::default(); + assert_eq!(perf.gpu_ms, None, "nothing is reported before a measurement"); + + perf.record_refresh(&refresh("gpu-full", Some(timing(83.9, 24_000_000)))); + assert_eq!((perf.gpu_ms, perf.gpu_ms_px), (Some(83.9), 24_000_000)); + + // The common case: a GPU refresh whose own measurement has not come back yet. + perf.record_refresh(&refresh("gpu-rect", None)); + assert_eq!((perf.gpu_ms, perf.gpu_ms_px), (Some(83.9), 24_000_000), "the last measurement stands"); + + perf.record_refresh(&refresh("gpu-rect", Some(timing(11.2, 1_098_640)))); + assert_eq!((perf.gpu_ms, perf.gpu_ms_px), (Some(11.2), 1_098_640), "a newer measurement replaces it"); + + // The CPU compositor did this one: the GPU number belongs to no refresh on screen. + perf.record_refresh(&refresh("full", None)); + assert_eq!((perf.gpu_ms, perf.gpu_ms_px), (None, 0), "a CPU refresh clears it rather than showing a stale one"); + + // And it comes back on the next GPU refresh that carries one. + perf.record_refresh(&refresh("gpu-full", Some(timing(42.0, 6_000_000)))); + assert_eq!((perf.gpu_ms, perf.gpu_ms_px), (Some(42.0), 6_000_000)); +} + +#[test] +fn device_limits_support_v3d_shader_variable_limit() { + let supported = wgpu::Limits { max_texture_dimension_2d: 7680, max_inter_stage_shader_variables: 15, ..wgpu::Limits::default() }; + let requested = device_limits_for(wgpu::Backend::Vulkan, supported.clone()); + assert_eq!(requested.max_inter_stage_shader_variables, 15); + assert!(requested.check_limits(&supported)); + assert_eq!(requested.max_texture_dimension_2d, 7680); + assert_eq!(requested.max_buffer_size, supported.max_buffer_size); +} + +#[test] +fn device_limits_support_weaker_gl_adapters() { + let supported = wgpu::Limits { + max_texture_dimension_2d: 1024, + max_texture_dimension_3d: 128, + max_bind_groups: 2, + max_buffer_size: 128 << 20, + ..wgpu::Limits::downlevel_webgl2_defaults() + }; + let requested = device_limits_for(wgpu::Backend::Gl, supported.clone()); + assert!(requested.check_limits(&supported)); + assert_eq!(requested, supported); +} + +#[test] +fn device_limits_preserve_backend_defaults_on_capable_adapters() { + let supported = wgpu::Limits { + max_texture_dimension_2d: 16_384, + max_buffer_size: 1 << 30, + max_bind_groups: 8, + max_inter_stage_shader_variables: 32, + min_uniform_buffer_offset_alignment: 64, + min_storage_buffer_offset_alignment: 64, + ..wgpu::Limits::default() + }; + for (backend, base) in [ + (wgpu::Backend::Vulkan, wgpu::Limits::default()), + (wgpu::Backend::Dx12, wgpu::Limits::default()), + (wgpu::Backend::Metal, wgpu::Limits::default()), + (wgpu::Backend::Gl, wgpu::Limits::downlevel_webgl2_defaults()), + ] { + let requested = device_limits_for(backend, supported.clone()); + assert!(requested.check_limits(&supported)); + assert_eq!(requested, wgpu::Limits { max_texture_dimension_2d: 16_384, max_buffer_size: 1 << 30, ..base }, "{backend:?}"); + } +} + +#[test] +fn device_limits_support_larger_minimum_alignments() { + let supported = wgpu::Limits { min_uniform_buffer_offset_alignment: 512, min_storage_buffer_offset_alignment: 1024, ..wgpu::Limits::default() }; + for backend in [wgpu::Backend::Vulkan, wgpu::Backend::Gl] { + let requested = device_limits_for(backend, supported.clone()); + assert!(requested.check_limits(&supported)); + assert_eq!(requested.min_uniform_buffer_offset_alignment, 512); + assert_eq!(requested.min_storage_buffer_offset_alignment, 1024); + } +} + +/// The scratch-texture workaround is for Intel's DX12 drivers (#892); other DX12 adapters +/// render straight into the level like every other backend (#4515). +#[test] +fn separate_mip_targets_only_for_intel_on_dx12() { + use wgpu::Backend::{Dx12, Metal, Vulkan}; + let (intel, nvidia, amd) = (0x8086, 0x10de, 0x1002); + assert!(separate_mip_targets(Dx12, intel, None)); + assert!(!separate_mip_targets(Dx12, nvidia, None)); + assert!(!separate_mip_targets(Dx12, amd, None)); + assert!(!separate_mip_targets(Vulkan, intel, None)); + assert!(!separate_mip_targets(Metal, 0, None)); + // PHOTOCRAFT_SEPARATE_MIPS forces either way; anything else leaves the default. + assert!(separate_mip_targets(Dx12, nvidia, Some("1"))); + assert!(!separate_mip_targets(Dx12, intel, Some(" 0 "))); + assert!(separate_mip_targets(Dx12, intel, Some("yes"))); +} + +/// #6418: levels of at most WHOLE_MIP_SIDE are redrawn whole; larger ones keep the damage. +#[test] +fn small_mip_levels_are_redrawn_whole() { + // A 3 × 3 dab at (800, 500) of a 1791 × 1083 tile, at mip 1 (895 × 541): halved and padded. + assert_eq!(mip_damage([800, 500, 803, 503], 895, 541), [399, 249, 403, 253]); + // At most 128 px on either side: the whole level, wherever the damage is. + assert_eq!(mip_damage([100, 60, 102, 63], 111, 67), [0, 0, 111, 67]); + assert_eq!(mip_damage([0, 0, 1, 1], 128, 2), [0, 0, 128, 2]); + assert_eq!(mip_damage([0, 0, 1, 1], 129, 2), [0, 0, 2, 2]); +} + +#[test] +fn gpu_budget_follows_memory_usage_and_ram() { + const G: u64 = 1 << 30; + // Card memory unknown (integrated, software, or unreadable): what Memory Usage leaves + // after the document, within a quarter of RAM. + assert_eq!(memory_budget(8 * G, 3 * G, Some(48 * G), None, 0), 5 * G); + assert_eq!(memory_budget(8 * G, 3 * G, Some(16 * G), None, 0), 4 * G); + assert_eq!(memory_budget(64 * G, 3 * G, Some(16 * G), None, 0), 4 * G); + // Never below the floor, even with nothing left; 16 GB assumed when RAM is unknown. + assert_eq!(memory_budget(8 * G, 10 * G, Some(48 * G), None, 0), MIN_GPU_BUDGET); + assert_eq!(memory_budget(64 * G, 0, None, None, 0), 4 * G); + // A card's own memory, known, doesn't hold the document's pixels: 10 GB of layers no + // longer squeeze the budget, which stays within 3/8 of the card (#4536). + assert_eq!(memory_budget(8 * G, 10 * G, Some(32 * G), Some(32 * G), 0), 8 * G); + assert_eq!(memory_budget(8 * G, 10 * G, Some(32 * G), Some(16 * G), 0), 6 * G); + assert_eq!(memory_budget(8 * G, 10 * G, Some(16 * G), Some(32 * G), 0), 4 * G); + // GPU Memory set: that much, within the card's share, above the floor. + assert_eq!(memory_budget(8 * G, 10 * G, Some(16 * G), None, 12 * G), 12 * G); + assert_eq!(memory_budget(8 * G, 10 * G, Some(16 * G), Some(16 * G), 12 * G), 6 * G); + assert_eq!(memory_budget(8 * G, 0, None, Some(32 * G), 1 << 20), MIN_GPU_BUDGET); + assert!(physical_memory().is_none_or(|b| b >= 256 << 20)); +} + +/// An 8 GB card in a 64 GB machine: both budgets used to plan for 16 GB, twice the card (#4536). +#[test] +fn gpu_budgets_fit_a_discrete_gpus_own_memory() { + const G: u64 = 1 << 30; + let (ram, vram) = (Some(64 * G), Some(8 * G)); + assert_eq!(memory_budget(64 * G, 3 * G, ram, vram, 0), 3 * G); + assert_eq!(canvas_budget(ram, vram), 3 * G); + assert!(memory_budget(64 * G, 0, ram, vram, 0) + canvas_budget(ram, vram) <= 6 * G, "a quarter of the card stays free"); + // A card larger than a quarter of RAM caps nothing (its own memory isn't charged for the + // document's 3 GB); a tiny one keeps the floor. + assert_eq!(memory_budget(8 * G, 3 * G, Some(48 * G), Some(24 * G), 0), 8 * G); + assert_eq!(canvas_budget(Some(16 * G), Some(24 * G)), 4 * G); + assert_eq!(memory_budget(64 * G, 0, ram, Some(G), 0), MIN_GPU_BUDGET); + assert_eq!(canvas_budget(ram, Some(G)), MIN_GPU_BUDGET); +} + +/// DXGI reports each adapter's dedicated memory, found by the vendor and device ids wgpu gives. +#[cfg(windows)] +#[test] +fn windows_reports_the_adapters_own_memory() { + use winsafe::prelude::*; + let descs: Vec<_> = winsafe::CreateDXGIFactory1().unwrap().EnumAdapters1().map(|a| a.unwrap().GetDesc1().unwrap()).collect(); + assert!(!descs.is_empty(), "DXGI lists at least its software adapter"); + for d in descs.iter().filter(|d| d.DedicatedVideoMemory > 0) { + // The first adapter with these ids is the one matched. + let first = descs.iter().find(|e| e.VendorId == d.VendorId && e.DeviceId == d.DeviceId).unwrap(); + assert_eq!(detect_video_memory(d.VendorId, d.DeviceId), Some(first.DedicatedVideoMemory as u64)); + } + assert_eq!(detect_video_memory(0xdead, 0xbeef), None, "no such adapter"); +} + +/// System Info names the card's own memory, after the driver, when the card reports one; an +/// integrated GPU or an unknown card shows no such line. +#[test] +fn system_info_shows_the_cards_memory() { + let info = GpuInfo { adapter: "Card".into(), driver: "Driver 1".into(), video_memory_mb: Some(8192), ..Default::default() }; + let lines = info.lines(); + assert_eq!(lines.get(2).map(String::as_str), Some("Driver: Driver 1")); + assert_eq!(lines.get(3).map(String::as_str), Some("Graphics memory: 8.0G")); + let none = GpuInfo { video_memory_mb: None, ..info }.lines(); + assert!(none.iter().all(|l| !l.starts_with("Graphics memory")), "{none:?}"); + assert_eq!(none.len() + 1, lines.len()); +} + +/// The Linux reader finds the card by its PCI ids in a `/sys/class/drm`-shaped folder and reads +/// amdgpu's VRAM size; a card without one, unreadable ids or a malformed size give nothing. +#[test] +fn drm_video_memory_reads_the_matching_cards_vram() { + let drm = std::env::temp_dir().join(format!("pc-drm-{}", std::process::id())); + let card = |name: &str, vendor: &str, device: &str, vram: Option<&str>| { + let dev = drm.join(name).join("device"); + std::fs::create_dir_all(&dev).unwrap(); + std::fs::write(dev.join("vendor"), vendor).unwrap(); + std::fs::write(dev.join("device"), device).unwrap(); + if let Some(v) = vram { + std::fs::write(dev.join("mem_info_vram_total"), v).unwrap(); + } + }; + // An Intel iGPU (no VRAM file), an AMD card, and an entry with garbage ids. + card("card0", "0x8086\n", "0x9a49\n", None); + card("card1", "0x1002\n", "0x73bf\n", Some("17163091968\n")); + card("card2", "not hex\n", "0x73bf\n", Some("1\n")); + card("card3", "0x10de\n", "0x2684\n", Some("lots\n")); + assert_eq!(drm_video_memory(&drm, 0x1002, 0x73bf), Some(17_163_091_968)); + assert_eq!(drm_video_memory(&drm, 0x8086, 0x9a49), None, "no amdgpu VRAM file"); + assert_eq!(drm_video_memory(&drm, 0x10de, 0x2684), None, "a malformed size"); + assert_eq!(drm_video_memory(&drm, 0xdead, 0xbeef), None, "no such card"); + assert_eq!(drm_video_memory(&drm.join("missing"), 0x1002, 0x73bf), None, "no drm folder"); + std::fs::remove_dir_all(&drm).unwrap(); +} + +/// #P60/P61: amdgpu's sysfs files have no NVIDIA equivalent, so a discrete NVIDIA card's own +/// memory comes from `nvidia-smi`'s CSV instead (real output: `0x2C0510DE, 16303`, device id +/// the high half of the hex, vendor the low). A second card, an upper-case `0X` prefix, extra +/// spaces, a non-matching id, a missing comma, a non-numeric size, empty output and a size +/// that would overflow all give nothing rather than the wrong (or a panicking) answer. +#[test] +fn parses_nvidia_smis_own_report_of_the_cards_memory() { + let two = "0x2C0510DE, 16303\n0x061C10DE, 24576\n"; + assert_eq!(parse_nvidia_smi(two, NVIDIA_VENDOR, 0x2c05), Some(16303 << 20)); + assert_eq!(parse_nvidia_smi(two, NVIDIA_VENDOR, 0x061c), Some(24576 << 20)); + assert_eq!(parse_nvidia_smi("0X2C0510DE, 16303 \n", NVIDIA_VENDOR, 0x2c05), Some(16303 << 20), "upper-case prefix, loose spacing"); + assert_eq!(parse_nvidia_smi(two, NVIDIA_VENDOR, 0xdead), None, "no such device id"); + assert_eq!(parse_nvidia_smi(two, 0x1002, 0x2c05), None, "no such vendor id"); + assert_eq!(parse_nvidia_smi("not csv at all", NVIDIA_VENDOR, 0x2c05), None); + assert_eq!(parse_nvidia_smi("0x2C0510DE 16303\n", NVIDIA_VENDOR, 0x2c05), None, "no comma"); + assert_eq!(parse_nvidia_smi("0x2C0510DE, lots\n", NVIDIA_VENDOR, 0x2c05), None, "not a number"); + assert_eq!(parse_nvidia_smi("", NVIDIA_VENDOR, 0x2c05), None, "empty output"); + assert_eq!(parse_nvidia_smi(&format!("0x2C0510DE, {}\n", u64::MAX), NVIDIA_VENDOR, 0x2c05), None, "would overflow"); +} + +/// Two cards of one model (same vendor and device id) with different memory: wgpu's ids can't +/// tell which one it picked, so the budget assumes the smaller rather than whichever came first. +#[test] +fn nvidia_smi_cards_sharing_an_id_report_the_smaller_memory() { + assert_eq!(parse_nvidia_smi("0x2C0510DE, 16303\n0x2C0510DE, 8192\n", NVIDIA_VENDOR, 0x2c05), Some(8192 << 20)); + assert_eq!(parse_nvidia_smi("0x2C0510DE, 8192\n0x2C0510DE, 16303\n", NVIDIA_VENDOR, 0x2c05), Some(8192 << 20)); + assert_eq!(parse_nvidia_smi("0x2C0510DE, lots\n0x2C0510DE, 16303\n", NVIDIA_VENDOR, 0x2c05), Some(16303 << 20), "a bad line is skipped"); + assert_eq!(parse_nvidia_smi("0x2C0510DE, 0\n0x2C0510DE, 16303\n", NVIDIA_VENDOR, 0x2c05), Some(16303 << 20), "a zero size is no answer"); +} + +/// `nvidia-smi` runs on the UI thread at startup, so a query the driver never answers must +/// give up at the deadline instead of keeping the window from opening. +#[cfg(any(unix, windows))] +#[test] +fn a_child_that_never_answers_gives_up_at_the_deadline() { + let slow = if cfg!(windows) { + let mut c = std::process::Command::new("ping"); + c.args(["-n", "11", "127.0.0.1"]); + c + } else { + let mut c = std::process::Command::new("sleep"); + c.arg("10"); + c + }; + let start = std::time::Instant::now(); + assert_eq!(bounded_output(slow, std::time::Duration::from_millis(200)), None); + assert!(start.elapsed() < std::time::Duration::from_secs(2), "took {:?}", start.elapsed()); + + let quick = if cfg!(windows) { + let mut c = std::process::Command::new("cmd"); + c.args(["/C", "echo 0x2C0510DE, 16303"]); + c + } else { + let mut c = std::process::Command::new("printf"); + c.arg("0x2C0510DE, 16303\n"); + c + }; + let out = bounded_output(quick, std::time::Duration::from_secs(10)).expect("a quick command answers"); + assert_eq!(parse_nvidia_smi(&String::from_utf8_lossy(&out), NVIDIA_VENDOR, 0x2c05), Some(16303 << 20)); + + let failing = if cfg!(windows) { + let mut c = std::process::Command::new("cmd"); + c.args(["/C", "exit 3"]); + c + } else { + std::process::Command::new("false") + }; + assert_eq!(bounded_output(failing, std::time::Duration::from_secs(10)), None, "a failed run"); + assert_eq!(bounded_output(std::process::Command::new("photocraft-no-such-tool"), std::time::Duration::from_secs(1)), None, "not installed"); +} + +/// Only a discrete NVIDIA adapter asks `nvidia-smi`: nothing else spawns a process whose +/// answer it would then ignore anyway (`nvidia_video_memory` gates on the vendor id first). +#[cfg(target_os = "linux")] +#[test] +fn nvidia_video_memory_only_asks_for_an_nvidia_vendor_id() { + assert_eq!(nvidia_video_memory(0x1002, 0x73bf), None, "an AMD id never runs nvidia-smi"); + assert_eq!(nvidia_video_memory(0x8086, 0x9a49), None, "an Intel id never runs nvidia-smi"); +} + +/// Windows used to report no RAM at all, so the budget assumed 16 GB on every machine. +#[cfg(windows)] +#[test] +fn windows_reports_installed_ram() { + let ram = detect_physical_memory().unwrap_or(0); + assert!(ram >= 256 << 20, "{ram} bytes"); + assert!(ram < 1 << 50, "{ram} bytes"); +} + +#[test] +fn canvas_texture_bytes_price_every_tile_and_mip_level() { + // The same sum DocTextures builds tile by tile (and texture_info reports). + let brute = |size: [u32; 2], tile: u32, bpp: u64| -> u64 { + let mut total = 0; + for ty in (0..size[1]).step_by(tile as usize) { + for tx in (0..size[0]).step_by(tile as usize) { + let (w, h) = (tile.min(size[0] - tx), tile.min(size[1] - ty)); + total += (0..mip_count(w, h)).map(|l| u64::from((w >> l).max(1)) * u64::from((h >> l).max(1)) * bpp).sum::(); + } + } + total + }; + for (size, tile) in [([1, 1], 64), ([300, 200], 64), ([128, 64], 64), ([1000, 7], 256), ([8192, 8193], 8192), ([20_000, 9_000], 8192)] { + for bpp in [4, 8] { + assert_eq!(canvas_texture_bytes(size, tile, bpp), brute(size, tile, bpp), "{size:?} tile {tile}"); + } + } + assert_eq!(canvas_texture_bytes([0, 500], 64, 4), 0); + // A canvas declared in a few bytes of a file is priced without building (or looping over) it. + let huge = canvas_texture_bytes([300_000, 300_000], 8192, 4); + assert!(huge > 300_000u64 * 300_000 * 4 && huge > canvas_budget(Some(1 << 40), None)); + assert_eq!(canvas_texture_bytes([u32::MAX, u32::MAX], 64, 8), u64::MAX, "saturates"); + // A quarter of RAM (16 GB assumed), never below the compositor's floor. + assert_eq!(canvas_budget(None, None), 4 << 30); + assert_eq!(canvas_budget(Some(1 << 30), None), MIN_GPU_BUDGET); + assert!(canvas_texture_bytes([12_000, 8_000], DEFAULT_TILE, 8) < canvas_budget(None, None), "a 96 MP 16-bit canvas stays on the GPU"); +} + +#[test] +fn view_uniform_packs_rotation() { + assert_eq!(VIEW_FLOATS, 32); + assert_eq!(VIEW_UNIFORM_SIZE, 128); + assert_eq!(VIEW_FLOATS * 4, VIEW_UNIFORM_SIZE as usize); + assert!(gpu_view_ok(false, 0.0, 1.0)); + assert!(gpu_view_ok(false, 45.0_f32.to_radians(), 1.0)); + assert!(!gpu_view_ok(true, 45.0_f32.to_radians(), 1.0)); + assert!(!gpu_view_ok(false, 0.0, 2.0), "pixel aspect correction draws on the CPU"); + // Identity packing: rotation 0 matches the unrotated origin + scale map. + let origin = [10.0, 20.0]; + let (scale, center) = (2.0, [40.0, 8.0]); + let a = map_doc_px(origin, scale, center, 1.0, 0.0, 5.0, 9.0); + assert!((a[0] - (origin[0] + 5.0 * scale)).abs() < 1e-4); + assert!((a[1] - (origin[1] + 9.0 * scale)).abs() < 1e-4); + let (x0, y0, x1, y1) = tile_screen_aabb(origin, scale, center, 1.0, 0.0, [0.0, 0.0, 10.0, 4.0]); + assert!((x0 - origin[0]).abs() < 1e-4 && (y0 - origin[1]).abs() < 1e-4); + assert!((x1 - (origin[0] + 20.0)).abs() < 1e-4 && (y1 - (origin[1] + 8.0)).abs() < 1e-4); +} + +#[test] +fn filter_modes() { + assert_eq!(filter_mode(0.25), (0.0, 2.0)); + assert_eq!(filter_mode(1.0).0, 2.0); + assert_eq!(filter_mode(1.5).0, 1.0); + assert_eq!(filter_mode(3.3).0, 2.0); +} + +#[test] +fn mips_and_premultiply() { + assert_eq!(mip_count(1, 1), 1); + assert_eq!(mip_count(4096, 100), 13); + assert_eq!(mip_count(5, 3), 3); + assert_eq!(premultiply_rgba8(&[[1.0, 0.5, 0.0, 0.5]]), vec![128, 64, 0, 128]); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn half_floats() { + // Every finite half survives the round trip; ties round to even; overflow saturates. + for h in 0..=u16::MAX { + if h & 0x7c00 != 0x7c00 && h != 0x8000 { + assert_eq!(f32_to_f16(f16_to_f32(h)), h, "{h:#06x}"); + } + } + assert_eq!(f32_to_f16(1.0), 0x3c00); + assert_eq!(f32_to_f16(1.0 + 2f32.powi(-11)), 0x3c00); + assert_eq!(f32_to_f16(1.0 + 3.0 * 2f32.powi(-11)), 0x3c02); + assert_eq!(f32_to_f16(2f32.powi(-24)), 1); + assert_eq!(f32_to_f16(2f32.powi(-26)), 0); + assert_eq!(f32_to_f16(65504.0), 0x7bff); + assert_eq!(f32_to_f16(1e9), 0x7bff); + assert_eq!(f32_to_f16(f32::INFINITY), 0x7bff); + assert_eq!(f32_to_f16(f32::NAN), 0); + assert_eq!(f32_to_f16(-2.0), 0xc000); + assert_eq!(premultiply_rgba16f(&[[2.0, -1.0, f32::NAN, 0.5]]), [0x00, 0x3c, 0, 0, 0, 0, 0x00, 0x38]); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn shaders_validate() { + use wgpu::naga; + for (name, src) in [("canvas", CANVAS_WGSL), ("mip", MIP_WGSL), ("encode", ENCODE_WGSL)] { + let module = naga::front::wgsl::parse_str(src).unwrap_or_else(|e| panic!("{name}: {}", e.emit_to_string(src))); + naga::valid::Validator::new(naga::valid::ValidationFlags::all(), naga::valid::Capabilities::empty()) + .validate(&module) + .unwrap_or_else(|e| panic!("{name}: {e:?}")); + } +} diff --git a/crates/ui-egui/src/canvas/gpu_canvas/texels.rs b/crates/ui-egui/src/canvas/gpu_canvas/texels.rs new file mode 100644 index 0000000000..5350ea5fae --- /dev/null +++ b/crates/ui-egui/src/canvas/gpu_canvas/texels.rs @@ -0,0 +1,145 @@ +use super::{FORMAT, FORMAT_HIGH, wgpu}; + +/// Straight-alpha f32 RGBA (sRGB-encoded) → premultiplied RGBA8, split across threads on native. +pub fn premultiply_rgba8(px: &[[f32; 4]]) -> Vec { + let mut out = Vec::new(); + texels_into(FORMAT, px, &mut out); + out +} + +/// Straight-alpha f32 RGBA (sRGB-encoded) → premultiplied RGBA16F (little-endian), split across +/// threads on native. Colour values above 1.0 are kept (32-bit documents); negatives and NaN +/// become 0. +pub fn premultiply_rgba16f(px: &[[f32; 4]]) -> Vec { + let mut out = Vec::new(); + texels_into(FORMAT_HIGH, px, &mut out); + out +} + +/// Bytes per texel of a canvas texture format. +pub(super) fn texel_bytes(format: wgpu::TextureFormat) -> u64 { + if format == FORMAT_HIGH { 8 } else { 4 } +} + +/// Straight-alpha f32 RGBA → premultiplied texels of `format`. +pub(super) fn texels(format: wgpu::TextureFormat, px: &[[f32; 4]]) -> Vec { + let mut out = Vec::new(); + texels_into(format, px, &mut out); + out +} + +/// [`texels`] into `out` (resized; reusing it across bands saves page-faulting a fresh buffer). +pub(super) fn texels_into(format: wgpu::TextureFormat, px: &[[f32; 4]], out: &mut Vec) { + fn rgba8(src: &[[f32; 4]], dst: &mut [u8]) { + for (o, p) in dst.as_chunks_mut::<4>().0.iter_mut().zip(src) { + let a = p[3].clamp(0.0, 1.0); + o[0] = (p[0].clamp(0.0, 1.0) * a * 255.0 + 0.5) as u8; + o[1] = (p[1].clamp(0.0, 1.0) * a * 255.0 + 0.5) as u8; + o[2] = (p[2].clamp(0.0, 1.0) * a * 255.0 + 0.5) as u8; + o[3] = (a * 255.0 + 0.5) as u8; + } + } + fn rgba16f(src: &[[f32; 4]], dst: &mut [u8]) { + for (o, p) in dst.as_chunks_mut::<8>().0.iter_mut().zip(src) { + let a = if p[3].is_nan() { 0.0 } else { p[3].clamp(0.0, 1.0) }; + let [r, g, b, a] = [p[0].max(0.0) * a, p[1].max(0.0) * a, p[2].max(0.0) * a, a].map(|v| f32_to_f16(v).to_le_bytes()); + *o = [r[0], r[1], g[0], g[1], b[0], b[1], a[0], a[1]]; + } + } + let bpp = texel_bytes(format) as usize; + let convert = if format == FORMAT_HIGH { rgba16f } else { rgba8 }; + out.clear(); + out.resize(px.len() * bpp, 0); + #[cfg(not(target_arch = "wasm32"))] + if px.len() > 1 << 18 { + let threads = std::thread::available_parallelism().map_or(4, |n| n.get()).min(16); + let chunk = px.len().div_ceil(threads); + std::thread::scope(|s| { + for (src, dst) in px.chunks(chunk).zip(out.chunks_mut(chunk * bpp)) { + s.spawn(move || convert(src, dst)); + } + }); + return; + } + convert(px, out); +} + +/// IEEE 754 binary32 → binary16, round to nearest even; overflow saturates to the largest +/// finite half (65504) so HDR values never become infinite, NaN becomes 0. Branch-light: the +/// rounding is done with integer adds on the f32 bits (and an f32 add for subnormals). +pub(super) fn f32_to_f16(v: f32) -> u16 { + // 2^-14: the smallest normal half; below it the result is subnormal (or zero). + const MIN_NORMAL: u32 = 113 << 23; + // 2^16: halves overflow from here (65520 and up round to infinity; saturate instead). + const OVERFLOW: u32 = 143 << 23; + // Adding 0.5 (as f32) shifts a subnormal half's bits to the bottom of the mantissa. + const DENORM_MAGIC: u32 = 126 << 23; + let bits = v.to_bits(); + let sign = ((bits >> 16) & 0x8000) as u16; + let a = bits & 0x7fff_ffff; + if a > 0x7f80_0000 { + return 0; + } + let h = if a >= OVERFLOW { + 0x7bff + } else if a < MIN_NORMAL { + (f32::from_bits(a) + f32::from_bits(DENORM_MAGIC)).to_bits().wrapping_sub(DENORM_MAGIC) as u16 + } else { + // Rebias the exponent and round the 13 dropped mantissa bits to nearest even. + let odd = (a >> 13) & 1; + (((a.wrapping_add(0xc800_0fff).wrapping_add(odd)) >> 13).min(0x7bff)) as u16 + }; + sign | h +} + +/// IEEE 754 binary16 → binary32. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn f16_to_f32(h: u16) -> f32 { + let sign = if h & 0x8000 != 0 { -1.0 } else { 1.0 }; + let exp = i32::from((h >> 10) & 0x1f); + let man = f32::from(h & 0x3ff); + sign * match exp { + 0 => man * 2f32.powi(-24), + 0x1f => { + if man == 0.0 { + f32::INFINITY + } else { + f32::NAN + } + } + e => (1.0 + man / 1024.0) * 2f32.powi(e - 15), + } +} + +/// One stored texel of `format` as f32 RGBA. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn texel_to_f32(format: wgpu::TextureFormat, b: &[u8]) -> [f32; 4] { + let mut o = [0.0f32; 4]; + for (i, v) in o.iter_mut().enumerate() { + *v = if format == FORMAT_HIGH { + f16_to_f32(u16::from_le_bytes([b.get(i * 2).copied().unwrap_or(0), b.get(i * 2 + 1).copied().unwrap_or(0)])) + } else { + f32::from(b.get(i).copied().unwrap_or(0)) / 255.0 + }; + } + o +} + +// --------------------------------------------------------------------------------------------- +// GPU resources + +/// The GPU memory a refresh's layer pages need against what they may hold (bytes). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct LayerFit { + /// The document refreshed. + pub doc: u64, + pub needed: u64, + pub budget: u64, +} + +impl LayerFit { + pub fn fits(&self) -> bool { + self.needed <= self.budget + } +} diff --git a/crates/ui-egui/src/guide_select.rs b/crates/ui-egui/src/canvas/guide_select.rs similarity index 100% rename from crates/ui-egui/src/guide_select.rs rename to crates/ui-egui/src/canvas/guide_select.rs diff --git a/crates/ui-egui/src/guide_select/tests.rs b/crates/ui-egui/src/canvas/guide_select/tests.rs similarity index 100% rename from crates/ui-egui/src/guide_select/tests.rs rename to crates/ui-egui/src/canvas/guide_select/tests.rs diff --git a/crates/ui-egui/src/layer_pick_ui.rs b/crates/ui-egui/src/canvas/layer_pick_ui.rs similarity index 99% rename from crates/ui-egui/src/layer_pick_ui.rs rename to crates/ui-egui/src/canvas/layer_pick_ui.rs index b75fe2b47b..3a3bf1efa1 100644 --- a/crates/ui-egui/src/layer_pick_ui.rs +++ b/crates/ui-egui/src/canvas/layer_pick_ui.rs @@ -62,7 +62,7 @@ pub fn choose(app: &mut PhotocraftApp, layer: u64) { } fn menu_id() -> egui::Id { - egui::Id::new("canvas-layer-menu") + egui::Id::unique("canvas-layer-menu") } /// Show the open menu. Escape or a click outside closes it. diff --git a/crates/ui-egui/src/canvas/move_ui.rs b/crates/ui-egui/src/canvas/move_ui.rs new file mode 100644 index 0000000000..29920474f7 --- /dev/null +++ b/crates/ui-egui/src/canvas/move_ui.rs @@ -0,0 +1,301 @@ +//! Move tool drags shown live (#128): while the pointer drags, the canvas shows the document with +//! the moving layers already at the pointer, exactly as `layer.translate` will leave them, and +//! recomposites only where they were and where they are. Releasing commits one `layer.translate` +//! (one history step) whose damage rect refreshes the same area. +//! +//! With a selection, the Move tool drags the selected pixels instead (#578): they float +//! (`select.float`, the canvas shows the piece at the pointer) until ↩, Deselect or any other +//! command drops them, as one "Move" history step. + +use std::sync::Arc; + +use photocraft_doc::{DocId, Document, LayerId}; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// Does the Move tool drag (and do the arrow keys nudge) the selected pixels of the active layer +/// rather than the whole layer, with `tool` in effect? With a selection, as in Photoshop; a layer +/// whose pixels can't float (type, shape, Smart Object, group, position-locked), a hidden layer or +/// several selected layers still move whole, as without one. The canvas cursor asks before the +/// press, and a pointer event passes `canvas::event_tool`, which also sees a ⌘ sent with the event +/// (#2768). +pub(crate) fn moves_selected_pixels_with(app: &PhotocraftApp, tool: Tool) -> bool { + let Some(st) = app.session.active() else { return false }; + tool == Tool::Move + && app.ui.transform.is_none() + && st.doc.selection.is_some() + && st.selected_layers().len() <= 1 + && st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.visible) + && app.session.is_enabled("select.float") +} + +/// Floats the selected pixels and moves them by (dx, dy) (`select.float`): cuts them the first +/// time, or with `copy` (⌥) floats a duplicate and leaves them; `copy` on a piece already floating +/// drops it where it is and floats a copy of it. False when it failed (reported in the status bar). +pub(crate) fn float_selected(app: &mut PhotocraftApp, copy: bool, dx: f64, dy: f64) -> bool { + let floating = app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + let dropped = if copy && floating { app.run("select.drop", json!({})) } else { Ok(json!(null)) }; + match dropped.and_then(|_| app.run("select.float", json!({"dx": dx, "dy": dy, "copy": copy}))) { + Ok(_) => { + crate::snap_ui::follow_cut(app); + true + } + Err(e) => { + app.ui.status = e; + app.ui.status_error = true; + false + } + } +} + +/// Preview keys of move drags: `BASE + n`, one per offset shown (see `canvas::display_doc`). +const BASE: u64 = 1 << 39; + +pub(crate) struct MovePreview { + doc: DocId, + revision: u64, + /// Layers that move ([`photocraft_engine::layer_multi_cmds::move_targets`]). + ids: Vec, + /// `ids` and every layer inside them: the layers shown at the drag's offset. + moving: Vec, + /// What moving them can change at offset (0, 0), not clipped to the canvas: a layer larger + /// than the canvas brings its pixels from beyond the edge into view (`None` = anything). + bounds: Option, + canvas: Rect, + /// Offsets shown so far, by preview key (`BASE + index`). + offsets: Vec<(i32, i32)>, + /// The document at the latest offset. + shown: Option>, + /// Shows a floating selection (`select.float`): offsets are the piece's, from where it was cut. + floating: bool, +} + +impl MovePreview { + fn key(&self) -> u64 { + BASE + self.offsets.len() as u64 + } + fn offset_of(&self, key: u64) -> Option<(i32, i32)> { + if key == 0 { + return Some((0, 0)); + } + let i = usize::try_from(key.checked_sub(BASE + 1)?).ok()?; + self.offsets.get(i).copied() + } + fn area(&self, d: (i32, i32)) -> Option { + let b = self.bounds?; + Some(if b.is_empty() { b } else { b.translate(d.0, d.1).inflate(1).intersect(&self.canvas) }) + } +} + +/// The whole-pixel offset of the current Move drag on document `idx`, if one is under way. +fn drag_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { + let d = app.drag.as_ref().filter(|d| d.tool == Tool::Move)?; + let end = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let dx = (end[0] - d.start[0]).round().clamp(-1e7, 1e7) as i32; + let dy = (end[1] - d.start[1]).round().clamp(-1e7, 1e7) as i32; + Some((dx, dy)) +} + +/// The ΔX/ΔY values shown beside the pointer while the Move tool drags layers or selected pixels, +/// as in Photoshop (#5727): the whole pixels the release will move them. Free Transform shows its +/// own (`transform_tool::live_readout`). `None` until the pointer moves. +pub(crate) fn offset_readout(app: &PhotocraftApp) -> Option<[String; 2]> { + app.drag.as_ref().filter(|d| d.points.len() > 1)?; + let (dx, dy) = drag_offset(app)?; + Some([dx, dy].map(|v| format!("{v} px"))) +} + +/// The document to show while a Move drag is under way on document `idx`: the moving layers at +/// the pointer. `None` without a drag (or when the layers can't move: locked, say; the drag then +/// shows its arrow and the release reports why). +pub(crate) fn display_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc, u64)> { + if app.session.active_index() == Some(idx) + && let Some(st) = app.session.documents().get(idx) + && photocraft_engine::float_cmds::floating(st).is_some() + { + return floating_doc(app, idx); + } + // Preferences › Interface › Show bounding box when dragging layer: outline and arrow only. + if app.session.active_index() != Some(idx) || app.session.prefs().interface.show_bounding_box_when_dragging_layer { + return None; + } + let Some(offset) = drag_offset(app) else { + app.move_preview = None; + return None; + }; + let mask_target = app.layer_mask_targeted(); + let st = app.session.documents().get(idx)?; + let (doc_id, revision, doc) = (st.doc.id, st.revision, st.doc.clone()); + let fresh = app.move_preview.as_ref().is_some_and(|p| p.doc == doc_id && p.revision == revision); + if !fresh { + let ids = photocraft_engine::layer_multi_cmds::move_targets(&doc, &st.selected_layers()); + if ids.is_empty() { + return None; + } + let all = Rect::new(i32::MIN / 2, i32::MIN / 2, i32::MAX / 2, i32::MAX / 2); + let bounds = ids.iter().try_fold(Rect::EMPTY, |acc, id| Some(acc.union(&photocraft_compose::change_bounds(doc.layer(*id)?, all)?))); + let canvas = doc.bounds(); + let moving = with_descendants(&doc, &ids); + let bounds = bounds.map(|b| moving.iter().filter_map(|id| shape_reach(doc.layer(*id)?, canvas)).fold(b, |acc, r| acc.union(&r))); + app.move_preview = Some(MovePreview { doc: doc_id, revision, ids, moving, bounds, canvas, offsets: Vec::new(), shown: None, floating: false }); + } + let p = app.move_preview.as_mut()?; + if offset == (0, 0) && p.offsets.is_empty() { + return None; + } + if p.offsets.last() != Some(&offset) { + let t0 = crate::gpu_canvas::now_ms(); + match photocraft_engine::layer_multi_cmds::moved_target(&doc, &p.ids, offset.0, offset.1, mask_target) { + Ok(d) => { + // Duotone documents display through their inks. + let d = photocraft_engine::mode_cmds::display_document(&d).unwrap_or(d); + p.shown = Some(Arc::new(d)); + p.offsets.push(offset); + } + Err(_) => { + p.shown = None; + return None; + } + } + app.perf.span("move preview", crate::gpu_canvas::now_ms() - t0); + } + let p = app.move_preview.as_ref()?; + Some((p.shown.clone()?, p.key())) +} + +/// Where shape layer `l` paints, effects included, not cut at the canvas as its rendered pixels +/// are: a drag brings the rest of it into view (#3946). `None` for other layers. +fn shape_reach(l: &photocraft_doc::Layer, canvas: Rect) -> Option { + let photocraft_doc::LayerContent::Shape(sh) = &l.content else { return None }; + let b = photocraft_vector::CompiledShape::new(sh, photocraft_vector::DEFAULT_TOLERANCE, canvas).bounds()?; + let m = if photocraft_compose::effects::has_effects(l) { photocraft_compose::effects::margin(l) } else { 0 }; + Some(b.inflate(m)) +} + +/// `ids` and every layer inside them (groups move their contents). +fn with_descendants(doc: &Document, ids: &[LayerId]) -> Vec { + let walk = doc.walk(); + let roots: Vec<_> = walk.iter().filter(|(_, _, l)| ids.contains(&l.id)).map(|(p, _, _)| p.as_slice()).collect(); + walk.iter().filter(|(p, _, _)| roots.iter().any(|r| p.starts_with(r))).map(|(_, _, l)| l.id).collect() +} + +/// Where the canvas shows layer `id` of document `doc` at `revision` relative to where it is: the +/// offset of the latest Move drag frame drawn ([`display_doc`]) when the layer moves with the drag, +/// so its Layer Edges outline moves in lockstep with its pixels (#2413). `None` when it shows +/// where it is. +pub(crate) fn shown_layer_offset(app: &PhotocraftApp, doc: DocId, revision: u64, id: LayerId) -> Option<(i32, i32)> { + let p = app.move_preview.as_ref().filter(|p| p.doc == doc && p.revision == revision && !p.floating && p.shown.is_some())?; + if !p.moving.contains(&id) { + return None; + } + p.offsets.last().copied() +} + +/// A floating selection's offset plus a drag of it in progress (`canvas::selection_drag_delta`). +pub(crate) fn floating_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { + let f = photocraft_engine::float_cmds::floating(app.session.active()?)?; + let d = crate::canvas::selection_drag_delta(app).unwrap_or((0, 0)); + Some((f.offset.0 + d.0, f.offset.1 + d.1)) +} + +/// The document shown while a selection floats: the cut piece at its offset, over the cut-out +/// layer. Only the piece's area changes between offsets, so each move costs the piece, not the +/// layer. +fn floating_doc(app: &mut PhotocraftApp, idx: usize) -> Option<(Arc, u64)> { + let offset = floating_offset(app)?; + let st = app.session.documents().get(idx)?; + let f = photocraft_engine::float_cmds::floating(st)?; + let (doc_id, revision) = (st.doc.id, st.revision); + let fresh = app.move_preview.as_ref().is_some_and(|p| p.doc == doc_id && p.revision == revision && p.floating); + if !fresh { + let bounds = st.doc.selection.as_ref().map(|s| s.content_bounds()); + let canvas = st.doc.bounds(); + app.move_preview = Some(MovePreview { + doc: doc_id, + revision, + ids: vec![f.layer], + moving: Vec::new(), + bounds, + canvas, + offsets: Vec::new(), + shown: None, + floating: true, + }); + } + if offset == (0, 0) && app.move_preview.as_ref().is_none_or(|p| p.offsets.is_empty()) { + return None; + } + let st = app.session.documents().get(idx)?; + let shown = app.move_preview.as_ref()?.offsets.last() != Some(&offset); + if shown { + let t0 = crate::gpu_canvas::now_ms(); + let d = photocraft_engine::float_cmds::displayed(st, crate::canvas::selection_drag_delta(app).unwrap_or((0, 0)))?; + let d = photocraft_engine::mode_cmds::display_document(&d).unwrap_or(d); + let p = app.move_preview.as_mut()?; + p.shown = Some(Arc::new(d)); + p.offsets.push(offset); + app.perf.span("floating preview", crate::gpu_canvas::now_ms() - t0); + } + let p = app.move_preview.as_ref()?; + Some((p.shown.clone()?, p.key())) +} + +/// What changed between preview (or document) key `seen` and `now` of the current Move drag on +/// document `doc` at `revision`: where the moving layers were in both. `None` when either key +/// isn't one of this drag's (the canvas then recomposites everything). +pub(crate) fn damage(app: &PhotocraftApp, doc: DocId, revision: u64, seen: u64, now: u64) -> Option { + let p = app.move_preview.as_ref().filter(|p| p.doc == doc && p.revision == revision)?; + if seen == now || (seen < BASE && seen != 0) || (now < BASE && now != 0) { + return None; + } + let (a, b) = (p.area(p.offset_of(seen)?)?, p.area(p.offset_of(now)?)?); + Some(if a.is_empty() { + b + } else if b.is_empty() { + a + } else { + a.union(&b) + }) +} + +/// Whether the canvas shows a Move drag's layers at the pointer. +pub(crate) fn showing(app: &PhotocraftApp) -> bool { + app.move_preview.as_ref().is_some_and(|p| p.shown.is_some()) +} + +/// Whether `key` is a Move drag preview key. +pub(crate) fn is_preview_key(key: u64) -> bool { + key > BASE && key < BASE << 1 +} + +/// Does a Move drag of whole layers take the selection along? With several layers selected, as +/// in Photoshop (#7538): their selected pixels can't float, so the layers move and the marching +/// ants follow them. +pub(crate) fn carries_selection(app: &PhotocraftApp) -> bool { + app.session.active().is_some_and(|st| st.doc.selection.is_some() && st.selected_layers().len() > 1) +} + +/// Release: one `layer.translate` by the drag's offset. The canvas already shows the result, so +/// its caches are told it showed the document: the command's damage rect refreshes that area. +pub(crate) fn finish(app: &mut PhotocraftApp, dx: f64, dy: f64) { + // Within the range the preview and the ΔX/ΔY readout show (`drag_offset`), so the release + // commits the offset they showed even at the lowest zoom of a huge document. + let (dx, dy) = (dx.clamp(-1e7, 1e7), dy.clamp(-1e7, 1e7)); + // Only when the canvas shows this very offset (else it recomposites everything once). + let at = (dx as i32, dy as i32); + let shown = app.move_preview.take().filter(|p| p.shown.is_some() && p.offsets.last() == Some(&at)); + if dx == 0.0 && dy == 0.0 { + return; + } + if app.run("layer.translate", json!({"dx": dx, "dy": dy, "selection": carries_selection(app)})).is_err() { + return; + } + let Some(p) = shown else { return }; + crate::canvas::shown_as_document(app, p.doc, is_preview_key); +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/move_ui/tests.rs b/crates/ui-egui/src/canvas/move_ui/tests.rs new file mode 100644 index 0000000000..80bea71dc2 --- /dev/null +++ b/crates/ui-egui/src/canvas/move_ui/tests.rs @@ -0,0 +1,345 @@ +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ToolEvent; +use crate::state::Tool; + +#[test] +fn dragging_a_layer_larger_than_the_canvas_redraws_what_comes_in_from_beyond_the_edge() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.sync_views(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session + .edit("paint", |doc, a| { + doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(-32, -32, 96, 96), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.ui.tool = Tool::Move; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 32.0, y: 32.0, pressure: 1.0 }, m); + let mut keys = Vec::new(); + for x in [42.0, 52.0] { + crate::canvas::tool_event(&mut app, ToolEvent::Move { x, y: 32.0, pressure: 1.0 }, m); + keys.push(super::display_doc(&mut app, 0).unwrap().1); + } + let st = app.session.active().unwrap(); + // From +10 to +20 the strip x 0..10 shows pixels that were off the canvas a frame ago. + let r = super::damage(&app, st.doc.id, st.revision, keys[0], keys[1]).unwrap(); + assert_eq!(r, Rect::new(0, 0, 64, 64)); +} + +/// The Move tool shows how far it has moved the layer, ΔX/ΔY in whole pixels, as Photoshop +/// and Free Transform do (#5727): once the pointer moves, until the release. +#[test] +fn a_move_drag_reads_out_the_offset_it_will_commit() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.sync_views(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session + .edit("paint", |doc, a| { + doc.layer_mut(a.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(16, 16, 32, 32), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.ui.tool = Tool::Move; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let m = egui::Modifiers::NONE; + assert_eq!(super::offset_readout(&app), None, "no drag"); + // Pressed on the red square (16..32): with Auto-Select (on by default) an empty spot would + // start the layer marquee instead of a move. + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 24.0, y: 24.0, pressure: 1.0 }, m); + assert_eq!(super::offset_readout(&app), None, "pressed, not moved yet"); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 34.4, y: 20.8, pressure: 1.0 }, m); + assert_eq!(super::offset_readout(&app), Some(["10 px".to_string(), "-3 px".to_string()])); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 34.4, y: 20.8 }, m); + assert_eq!(super::offset_readout(&app), None, "released"); +} + +/// A release past the ±10,000,000 px the preview and the readout show commits the offset they +/// showed, not the raw pointer distance. +#[test] +fn a_release_commits_the_offset_the_readout_caps_at() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap(); + app.sync_views(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + super::finish(&mut app, 2.5e7, -2.5e7); + let st = app.session.active().unwrap(); + let s = st.active_layer.and_then(|id| st.doc.layer(id)).and_then(|l| l.surface()).unwrap(); + assert!(s.pixel(16 + 10_000_000, 16 - 10_000_000)[3] > 0.5, "moved by the capped offset"); +} + +/// A shape reaching past the canvas edge renders cut at the canvas, but its path goes on: a +/// drag frame must redraw where the rest of it was, or the cut edge stays behind (#3946). +#[test] +fn dragging_a_shape_past_the_canvas_edge_leaves_no_trail() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap(); + app.sync_views(); + let id = app.session.execute("shape.create", json!({"kind": "rect", "rect": [40, 16, 50, 32], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + let id = photocraft_doc::LayerId(id); + app.ui.tool = Tool::Move; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 32.0, pressure: 1.0 }, m); + let mut frames = Vec::new(); + for x in [40.0, 10.0] { + crate::canvas::tool_event(&mut app, ToolEvent::Move { x, y: 32.0, pressure: 1.0 }, m); + frames.push(super::display_doc(&mut app, 0).unwrap()); + } + let st = app.session.active().unwrap(); + let r = super::damage(&app, st.doc.id, st.revision, frames[0].1, frames[1].1).unwrap(); + let alpha = |d: &photocraft_doc::Document, x, y| d.layer(id).and_then(|l| l.surface()).map_or(0.0, |s| s.pixel(x, y)[3]); + for y in 0..64 { + for x in 0..64 { + if alpha(&frames[0].0, x, y) != alpha(&frames[1].0, x, y) { + assert!(r.contains(x, y), "({x}, {y}) changed outside the redrawn {r:?}"); + } + } + } +} + +/// Dragging with an unlinked mask targeted moves the mask alone, shown live and committed; +/// with the mask linked (or the pixels targeted) the layer moves. +#[test] +fn dragging_with_an_unlinked_mask_targeted_moves_the_mask_alone() { + for (linked, mask_target) in [(false, true), (false, false), (true, true)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent"})).unwrap(); + app.sync_views(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + app.session.execute("layer.layerMask.revealAll", json!({})).unwrap(); + app.session.execute("layer.layerMask.linked", json!({"linked": linked})).unwrap(); + app.ui.mask_target = mask_target; + app.ui.tool = Tool::Move; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 36.0, y: 16.0, pressure: 1.0 }, m); + let shown = super::display_doc(&mut app, 0).unwrap().0; + let id = app.session.active().unwrap().active_layer.unwrap(); + let layer_moved = |d: &photocraft_doc::Document| d.layer(id).and_then(|l| l.surface()).is_some_and(|s| s.pixel(28, 16)[3] > 0.5); + let alone = !linked && mask_target; + assert_eq!(layer_moved(&shown), !alone, "preview, linked {linked} mask target {mask_target}"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 36.0, y: 16.0 }, m); + let d = &app.session.active().unwrap().doc; + assert_eq!(layer_moved(d), !alone, "commit, linked {linked} mask target {mask_target}"); + } +} + +/// Mid-drag on a painted layer: is the layer shown at the pointer? +fn live(outline: bool) -> bool { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.sync_views(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session.execute("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + app.session.edit_prefs(|p| p.interface.show_bounding_box_when_dragging_layer = outline); + app.ui.tool = Tool::Move; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 20.0, pressure: 1.0 }, m); + let shown = super::display_doc(&mut app, 0).is_some(); + assert_eq!(shown, super::showing(&app)); + shown +} + +#[test] +fn bounding_box_preference_turns_the_live_drag_off() { + assert!(live(false), "by default the layer follows the pointer"); + assert!(!live(true), "with the preference on, only the outline and arrow move"); +} + +/// A `depth`-bit transparent document whose layer holds a red square (8..24)² and a blue one +/// (40..56)×(8..24), the red one selected; the Move tool with Auto-Select on. +fn selected_pixels(depth: u32) -> (PhotocraftApp, photocraft_doc::LayerId) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 64, "height": 64, "background": "transparent", "depth": depth})).unwrap(); + app.sync_views(); + for (x, color) in [(40, "#0000ff"), (8, "#ff0000")] { + app.session.execute("select.rect", json!({"x": x, "y": 8, "width": 16, "height": 16})).unwrap(); + app.session.execute("edit.fill", json!({"color": color})).unwrap(); + } + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = true; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let id = app.session.active().unwrap().active_layer.unwrap(); + (app, id) +} + +fn alpha(app: &PhotocraftApp, id: photocraft_doc::LayerId, x: i32, y: i32) -> f32 { + app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().rgba(x, y)[3] +} + +fn offset(app: &PhotocraftApp) -> Option<(i32, i32)> { + photocraft_engine::float_cmds::floating(app.session.active().unwrap()).map(|f| f.offset) +} + +fn drag(app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], m: egui::Modifiers) { + crate::canvas::tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, m); + crate::canvas::tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, m); + crate::canvas::tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, m); +} + +/// ↩ through the app's key handling. +fn enter(app: &mut PhotocraftApp) { + let ctx = egui::Context::default(); + let key = egui::Event::Key { key: egui::Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }; + let input = egui::RawInput { events: vec![key], ..Default::default() }; + ctx.run_ui(input, |ui| crate::shortcuts::handle(app, ui.ctx())).textures_delta.clear(); +} + +/// #578: with a selection the Move tool drags the selected pixels (from anywhere, without an +/// Auto-Select pick), not the layer; ↩ drops them as one "Move" step that one undo takes back. +#[test] +fn with_a_selection_the_move_tool_moves_only_the_selected_pixels() { + for depth in [8, 16, 32] { + let (mut app, id) = selected_pixels(depth); + let steps = app.session.active().unwrap().history.past_len(); + drag(&mut app, [50.0, 40.0], [60.0, 45.0], egui::Modifiers::NONE); + assert_eq!(offset(&app), Some((10, 5)), "{depth}-bit: floating"); + let shown = super::display_doc(&mut app, 0).unwrap().0; + let shown_alpha = |x, y| shown.layer(id).unwrap().surface().unwrap().rgba(x, y)[3]; + assert!(shown_alpha(10, 10) == 0.0 && shown_alpha(30, 25) == 1.0, "{depth}-bit: shown moved"); + enter(&mut app); + assert_eq!(offset(&app), None, "{depth}-bit: ↩ dropped it"); + assert_eq!(alpha(&app, id, 10, 10), 0.0, "{depth}-bit: the hole is transparent"); + assert_eq!(alpha(&app, id, 30, 25), 1.0, "{depth}-bit: the red square moved"); + assert_eq!(alpha(&app, id, 48, 16), 1.0, "{depth}-bit: the unselected blue one stayed"); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.selection.as_ref().unwrap().content_bounds(), Rect::new(18, 13, 34, 29), "{depth}-bit"); + assert_eq!((st.history.past_len(), st.history.undo_label()), (steps + 1, Some("Move")), "{depth}-bit"); + assert!(app.session.undo()); + assert!(alpha(&app, id, 10, 10) == 1.0 && alpha(&app, id, 30, 25) == 0.0, "{depth}-bit: undone"); + } +} + +/// ⌥ copies the selected pixels (no layer copy), ⇧ locks the drag to multiples of 45°. +#[test] +fn alt_copies_and_shift_constrains_the_selected_pixels() { + for depth in [8, 16, 32] { + let (mut app, id) = selected_pixels(depth); + let layers = app.session.active().unwrap().doc.layers.len(); + drag(&mut app, [16.0, 16.0], [30.0, 28.0], egui::Modifiers::ALT | egui::Modifiers::SHIFT); + assert_eq!(offset(&app), Some((13, 13)), "{depth}-bit: 45°"); + app.run("select.drop", json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().doc.layers.len(), layers, "{depth}-bit: no layer copy"); + assert!(alpha(&app, id, 10, 10) == 1.0 && alpha(&app, id, 35, 35) == 1.0, "{depth}-bit: original and copy"); + assert!(app.session.undo()); + assert_eq!(alpha(&app, id, 35, 35), 0.0, "{depth}-bit: undone"); + } +} + +/// #7538: a loose marquee round the red square shrinks to it as the drag starts: the canvas +/// outlines the moved pixels while they float, and the dropped selection is theirs. Alt-drag +/// (a copy) too. +#[test] +fn dragging_selected_pixels_shrinks_the_ants_to_them() { + for depth in [8, 16, 32] { + for m in [egui::Modifiers::NONE, egui::Modifiers::ALT] { + let (mut app, _) = selected_pixels(depth); + app.session.execute("select.rect", json!({"x": 2, "y": 2, "width": 30, "height": 30})).unwrap(); + drag(&mut app, [16.0, 16.0], [26.0, 21.0], m); + assert_eq!(offset(&app), Some((10, 5)), "{depth}-bit, {m:?}: floating"); + let ants = crate::canvas::floating_outline(&app, 0).map(|s| s.content_bounds()); + assert_eq!(ants, Some(Rect::new(8, 8, 24, 24)), "{depth}-bit, {m:?}: the ants hug the red square"); + assert!(crate::canvas::floating_outline(&app, 1).is_none(), "only the active document's"); + app.run("select.drop", json!({})).unwrap(); + let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds()); + assert_eq!(sel, Some(Rect::new(18, 13, 34, 29)), "{depth}-bit, {m:?}: dropped"); + assert!(crate::canvas::floating_outline(&app, 0).is_none(), "nothing floats"); + } + } +} + +/// ⌘-drag with a painting tool drags the selected pixels as the Move tool does, also when ⌘ +/// comes with the event (control channel, MCP) rather than as the held-key override. +#[test] +fn command_drag_with_a_painting_tool_moves_only_the_selected_pixels() { + let (mut app, id) = selected_pixels(8); + app.ui.tool = Tool::Brush; + app.ui.tool_options.move_auto_select = false; + let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() }; + drag(&mut app, [50.0, 40.0], [60.0, 45.0], cmd); + assert_eq!(offset(&app), Some((10, 5)), "the selected pixels float"); + drag(&mut app, [50.0, 40.0], [60.0, 45.0], cmd); + assert_eq!(offset(&app), Some((20, 10)), "a second drag moves the same floating piece"); + app.run("select.drop", json!({})).unwrap(); + assert!(alpha(&app, id, 10, 10) == 0.0 && alpha(&app, id, 40, 30) == 1.0, "the red square moved"); + assert_eq!(alpha(&app, id, 48, 16), 1.0, "the unselected blue one stayed"); +} + +/// A click moves nothing and leaves nothing floating (Undo isn't spent putting it back). +#[test] +fn a_click_with_a_selection_moves_nothing() { + let (mut app, id) = selected_pixels(8); + let steps = app.session.active().unwrap().history.past_len(); + drag(&mut app, [16.0, 16.0], [16.0, 16.0], egui::Modifiers::NONE); + assert_eq!(offset(&app), None); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + assert!(alpha(&app, id, 10, 10) == 1.0 && alpha(&app, id, 48, 16) == 1.0); +} + +#[test] +fn arrow_keys_nudge_the_selected_pixels() { + let (mut app, id) = selected_pixels(16); + crate::move_mods::nudge(&mut app, 10.0, 0.0, false); + crate::move_mods::nudge(&mut app, 0.0, 1.0, false); + assert_eq!(offset(&app), Some((10, 1))); + // ⌥ leaves the piece where it is and nudges a copy of it. + crate::move_mods::nudge(&mut app, 1.0, 0.0, true); + assert_eq!(offset(&app), Some((1, 0))); + app.run("select.drop", json!({})).unwrap(); + assert!(alpha(&app, id, 10, 10) == 0.0 && alpha(&app, id, 18, 20) == 1.0 && alpha(&app, id, 34, 20) == 1.0); + assert_eq!(alpha(&app, id, 48, 16), 1.0, "the layer itself didn't move"); +} + +/// Without floatable pixels (a position-locked layer) the Move tool behaves as without a +/// selection: it moves the layer, which the lock refuses. +#[test] +fn a_layer_that_cannot_float_moves_whole_as_before() { + let (mut app, id) = selected_pixels(8); + app.session.execute("layer.lockLayers", json!({"position": true})).unwrap(); + drag(&mut app, [16.0, 16.0], [26.0, 16.0], egui::Modifiers::NONE); + assert_eq!(offset(&app), None); + assert!(alpha(&app, id, 10, 10) == 1.0 && alpha(&app, id, 30, 16) == 0.0, "nothing moved"); + // Without a selection the layer moves whole. + app.session.execute("layer.lockLayers", json!({"position": false})).unwrap(); + app.session.execute("select.deselect", json!({})).unwrap(); + drag(&mut app, [16.0, 16.0], [26.0, 16.0], egui::Modifiers::NONE); + assert!(alpha(&app, id, 10, 10) == 0.0 && alpha(&app, id, 58, 16) == 1.0, "the whole layer moved"); +} + +#[test] +fn floating_selection_does_not_snap_to_its_own_starting_outline_or_preview_at_zero() { + let (mut app, _id) = selected_pixels(8); + app.ui.extras.snap = true; + app.ui.view.snap_to.document_bounds = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 16.0, y: 16.0, pressure: 1.0 }, m); + assert!(super::display_doc(&mut app, 0).is_none(), "no preview composite at offset (0, 0) before the pointer moves"); + // A small 2 px move within the snap radius must move immediately instead of snapping back to (0, 0). + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 18.0, y: 17.0, pressure: 1.0 }, m); + assert_eq!(super::floating_offset(&app), Some((2, 1)), "small drag moves without snapping to own selection bounds"); + assert!(super::display_doc(&mut app, 0).is_some(), "preview is shown once offset is non-zero"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 18.0, y: 17.0 }, m); + assert_eq!(offset(&app), Some((2, 1))); +} diff --git a/crates/ui-egui/src/canvas/navigator.rs b/crates/ui-egui/src/canvas/navigator.rs new file mode 100644 index 0000000000..413bf82686 --- /dev/null +++ b/crates/ui-egui/src/canvas/navigator.rs @@ -0,0 +1,138 @@ +use super::display::{canvas_display, display_doc, display_image}; +use super::strokes::live_stroke; +use super::textures::{effect_reach, ensure_texture}; +use super::{Document, PhotocraftApp, Rect, TextureOptions}; + +/// Longest side of the Navigator panel's image (about twice the panel's width, for HiDPI). +pub const NAVIGATOR_SIDE: u32 = 512; + +/// The navigator image of a document: its snapshot, preview key and revision, and the texture. +pub(crate) type NavigatorCache = (std::sync::Weak, u64, u64, egui::TextureHandle); + +/// How long adjustment-dialog settings must stay unchanged before the navigator shows them. +const NAVIGATOR_SETTLE_MS: f64 = 200.0; + +/// While a Move or Patch drag or a live paint stroke is under way, the navigator keeps its image +/// of document `idx` and catches up on release: each step of the drag is a new preview, and a +/// thumbnail composite of the whole document per step makes painting lag on large images. +pub(super) fn navigator_waits_for_drag(app: &PhotocraftApp, idx: usize) -> bool { + crate::move_ui::showing(app) || crate::patch_preview::showing(app) || crate::cam_preview::showing(app) || live_stroke(app, idx).is_some() +} + +/// The Navigator panel's image of document `idx`. With the CPU canvas that is the canvas texture +/// itself; with the GPU canvas a thumbnail cached per revision, so an edit to a huge document +/// doesn't also pay a full-resolution CPU composite for the navigator. +/// The Navigator's preview from the GPU canvas (#4518): the main canvas's texture of the +/// document, drawn at `rect` with its own mip chain, so an edit costs no CPU composite. `None` +/// while the canvas isn't showing the plain document on the GPU (the CPU thumbnail then). +pub fn navigator_gpu_view(app: &PhotocraftApp, idx: usize, rect: Rect, ctx: &egui::Context) -> Option { + app.gpu.as_ref()?; + let st = app.session.documents().get(idx)?; + let params = *app.navigator_gpu.get(&st.doc.id)?; + let [w, h] = params.doc_size; + if w == 0 || h == 0 || !rect.is_positive() || [st.doc.size.width, st.doc.size.height] != params.doc_size { + return None; + } + Some(crate::gpu_canvas::ViewParams { + zoom: rect.width() / w as f32, + center: [w as f32 / 2.0, h as f32 / 2.0], + rotation: 0.0, + shadow: false, + pixel_grid: false, + view_key: egui::Id::unique(("pc-navigator", ctx.viewport_id(), idx)).value(), + offset: [0.0, 0.0], + ..params + }) +} + +/// Frames the Navigator waits for the GPU canvas to draw a document it hasn't drawn yet, rather +/// than build a CPU thumbnail it would only use once (a full composite on a large document). +const NAVIGATOR_WAIT_FRAMES: u32 = 3; + +/// Whether the Navigator should wait a frame for the GPU canvas (#4518): the GPU canvas is on but +/// hasn't drawn document `idx` yet (the Navigator can draw before the canvas in a frame). After +/// [`NAVIGATOR_WAIT_FRAMES`] it stops waiting, so it never stays empty. +pub fn navigator_waits_for_gpu(app: &PhotocraftApp, idx: usize, ctx: &egui::Context) -> bool { + if app.gpu.is_none() { + return false; + } + let Some(id) = app.session.documents().get(idx).map(|st| st.doc.id) else { return false }; + if app.canvases.keys().any(|(d, _)| *d == id) { + return false; + } + let key = egui::Id::unique(("navigator-waits", id.0)); + let waited = ctx.data(|d| d.get_temp::(key)).unwrap_or(0); + if waited >= NAVIGATOR_WAIT_FRAMES { + return false; + } + ctx.data_mut(|d| d.insert_temp(key, waited + 1)); + ctx.request_repaint(); + true +} + +pub fn navigator_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Option { + if app.gpu.is_none() { + return ensure_texture(app, ctx, idx, app.session.color.main_display).map(|(t, _)| t); + } + // Keyed by the document snapshot, not its revision: selecting a layer changes no pixels. + let (snapshot, id, revision, last_damage) = + app.session.documents().get(idx).map(|st| (std::sync::Arc::downgrade(&st.doc), st.doc.id, st.revision, st.last_damage))?; + let (doc, preview_key) = display_doc(app, idx); + let (display, display_key) = canvas_display(app, &doc, None); + let preview_key = preview_key ^ display_key; + let cached = app.navigator_textures.get(&id).cloned(); + if let Some((r, p, _, t)) = &cached + && r.ptr_eq(&snapshot) + && *p == preview_key + { + return Some(t.id()); + } + if let Some((_, _, _, t)) = &cached + && navigator_waits_for_drag(app, idx) + { + return Some(t.id()); + } + // While an adjustment dialog's settings are changing, the navigator keeps its image and + // catches up once they settle (a thumbnail composite per change would cost more than the canvas). + if let Some((r, _, _, t)) = &cached + && r.ptr_eq(&snapshot) + && crate::adjust_preview::settling(app, NAVIGATOR_SETTLE_MS) + { + ctx.request_repaint_after(std::time::Duration::from_millis(NAVIGATOR_SETTLE_MS as u64)); + return Some(t.id()); + } + let t0 = crate::gpu_canvas::now_ms(); + // One edit since the cached image, with the area it changed: update just that part (nothing + // for a rename, a selection or a new empty layer). A thumbnail composite of the whole + // document on every edit cost a full-resolution CPU composite on the UI thread. + let size = photocraft_compose::thumbnail_size(&doc, NAVIGATOR_SIDE); + if let Some((_, p, rev, t)) = &cached + && *p == preview_key + && rev.checked_add(1) == Some(revision) + && let Some(d) = last_damage + && t.size() == [size.0 as usize, size.1 as usize] + { + let mut t = t.clone(); + if !d.is_empty() { + let buf = photocraft_compose::render_reduced_damage(&doc, size.0, size.1, d.inflate(effect_reach(&doc.layers))); + let r = buf.rect; + if !r.is_empty() { + t.set_partial([r.x0 as usize, r.y0 as usize], display_image(display.as_deref(), &buf), TextureOptions::LINEAR); + } + } + app.perf.span("navigator", crate::gpu_canvas::now_ms() - t0); + app.navigator_textures.insert(id, (snapshot, preview_key, revision, t.clone())); + return Some(t.id()); + } + let image = display_image(display.as_deref(), &photocraft_compose::thumbnail_buffer(&doc, NAVIGATOR_SIDE)); + let tex = match cached { + Some((_, _, _, mut t)) => { + t.set(image, TextureOptions::LINEAR); + t + } + None => ctx.load_texture(format!("navigator-{}", id.0), image, TextureOptions::LINEAR), + }; + app.perf.span("navigator", crate::gpu_canvas::now_ms() - t0); + app.navigator_textures.insert(id, (snapshot, preview_key, revision, tex.clone())); + Some(tex.id()) +} diff --git a/crates/ui-egui/src/outline.rs b/crates/ui-egui/src/canvas/outline.rs similarity index 100% rename from crates/ui-egui/src/outline.rs rename to crates/ui-egui/src/canvas/outline.rs diff --git a/crates/ui-egui/src/canvas/paint.rs b/crates/ui-egui/src/canvas/paint.rs new file mode 100644 index 0000000000..415bb071a7 --- /dev/null +++ b/crates/ui-egui/src/canvas/paint.rs @@ -0,0 +1,615 @@ +use super::display::{canvas_display, checker, display_doc, display_image}; +use super::drag::{ + angle_readout, draw_marquee_readout, draw_value_readout, marquee_ants, marquee_drag_ants, marquee_preview_px, single_line_drag_px, size_readout, +}; +use super::gesture::constrained_polygon_point; +use super::strokes::{EngineStroke, SPOT_HEALING_TRAIL, draw_smoothing_string, strokes_live}; +use super::textures::texture_side; +use super::view_xform::ViewXform; +use super::{Color32, DRect, Document, Mesh, PhotocraftApp, Pos2, Rect, Stroke, TextureOptions, Tool, json, pos2, vec2}; + +/// A texture mapped over the `size` document area starting at `origin` (follows a rotated view). +pub(super) fn paint_mapped_image_at(painter: &egui::Painter, xf: &ViewXform, tex: egui::TextureId, uv: Rect, origin: [f32; 2], size: [f32; 2], tint: Color32) { + let ([x, y], [w, h]) = (origin, size); + let pts = [xf.to_screen(x, y), xf.to_screen(x + w, y), xf.to_screen(x + w, y + h), xf.to_screen(x, y + h)]; + let uvs = [uv.min, pos2(uv.max.x, uv.min.y), uv.max, pos2(uv.min.x, uv.max.y)]; + let mut mesh = Mesh::with_texture(tex); + for i in 0..4 { + mesh.vertices.push(egui::epaint::Vertex { pos: pts[i], uv: uvs[i], color: tint }); + } + mesh.add_triangle(0, 1, 2); + mesh.add_triangle(0, 2, 3); + painter.add(egui::Shape::mesh(mesh)); +} + +/// The GPU canvas view, then its View › Pattern Preview copies (#1067): the same texture drawn +/// again whole document sizes away, each with its own view uniform and no shadow. +pub(super) fn paint_gpu_view(painter: &egui::Painter, rect: Rect, params: crate::gpu_canvas::ViewParams, copies: &[[i32; 2]]) { + crate::gpu_canvas::GpuCanvas::paint(painter, rect, params); + for (k, &c) in copies.iter().enumerate() { + let copy = crate::gpu_canvas::ViewParams { + offset: crate::pattern_preview::offset(c, params.doc_size), + shadow: false, + // Keyed by slot, not by tile index, so panning reuses the same few uniform buffers. + view_key: egui::Id::unique((params.view_key, "pattern", k)).value(), + ..params + }; + crate::gpu_canvas::GpuCanvas::paint(painter, rect, copy); + } +} + +/// The mapping that draws Pattern Preview copy `c` of a `size` document where it sits next to +/// the document: the same view, looking the matching number of document sizes back. +pub(super) fn pattern_xf(xf: &ViewXform, c: [i32; 2], size: [u32; 2]) -> ViewXform { + let [ox, oy] = crate::pattern_preview::offset(c, size); + ViewXform { center: [xf.center[0] - ox, xf.center[1] - oy], ..*xf } +} + +/// CPU canvas: the transparency checkerboard (or white) and the display texture, for the +/// document or one of its Pattern Preview copies (`xf` from [`pattern_xf`]). +pub(super) fn paint_cpu_document( + painter: &egui::Painter, + xf: &ViewXform, + bounds: DRect, + rotated: bool, + checker: Option<(egui::TextureId, f32)>, + tex: Option, + opaque: bool, +) { + let size = [bounds.width() as f32, bounds.height() as f32]; + let origin = [bounds.x0 as f32, bounds.y0 as f32]; + let img_rect = xf.doc_rect(bounds); + match checker { + Some((checker_id, square)) if !opaque || tex.is_none() => { + if rotated { + let tiles = vec2(size[0], size[1]) / (2.0 * square); + paint_mapped_image_at(painter, xf, checker_id, Rect::from_min_max(Pos2::ZERO, pos2(tiles.x, tiles.y)), origin, size, Color32::WHITE); + } else { + let tiles = img_rect.size() / (2.0 * square); + painter.image(checker_id, img_rect, Rect::from_min_max(Pos2::ZERO, pos2(tiles.x, tiles.y)), Color32::WHITE); + } + } + // On software window adapters this hidden full-canvas pass is CPU rasterization too. + // Keep the white-background branch unchanged (its antialiased outer edge is visible). + Some(_) => {} + None => { + if rotated { + paint_mapped_quad(painter, xf, origin, size, Color32::WHITE); + } else { + painter.rect_filled(img_rect, 0.0, Color32::WHITE); + } + } + } + if let Some(tex) = tex { + if rotated { + paint_mapped_image_at(painter, xf, tex, Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)), origin, size, Color32::WHITE); + } else { + let uv = if xf.flip { Rect::from_min_max(pos2(1.0, 0.0), pos2(0.0, 1.0)) } else { Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0)) }; + painter.image(tex, img_rect, uv, Color32::WHITE); + } + } +} + +/// [`ensure_texture_in_view`]'s detail texture over document area `area` (x0, y0, x1, y1), on its +/// own checkerboard (or white), so the reduced texture under it doesn't show through where the +/// image is transparent. The checkerboard keeps [`paint_cpu_document`]'s phase. +pub(super) fn paint_cpu_detail( + painter: &egui::Painter, + xf: &ViewXform, + bounds: DRect, + rotated: bool, + checker: Option<(egui::TextureId, f32)>, + tex: egui::TextureId, + area: [f32; 4], +) { + let [x0, y0, x1, y1] = area; + let corners = [[x0, y0], [x1, y0], [x1, y1], [x0, y1]]; + let pts = corners.map(|[x, y]| xf.to_screen(x, y)); + let quad = |tex: egui::TextureId, uvs: [Pos2; 4]| { + let mut mesh = Mesh::with_texture(tex); + for (pos, uv) in pts.into_iter().zip(uvs) { + mesh.vertices.push(egui::epaint::Vertex { pos, uv, color: Color32::WHITE }); + } + mesh.add_triangle(0, 1, 2); + mesh.add_triangle(0, 2, 3); + painter.add(egui::Shape::mesh(mesh)); + }; + match checker { + Some((checker_id, square)) => { + let tile = 2.0 * square; + let uvs = if rotated { + corners.map(|[x, y]| pos2(x / tile, y / tile)) + } else { + let min = xf.doc_rect(bounds).min; + pts.map(|p| ((p - min) / tile).to_pos2()) + }; + quad(checker_id, uvs); + } + None => { + painter.add(egui::Shape::convex_polygon(pts.to_vec(), Color32::WHITE, Stroke::NONE)); + } + } + quad(tex, [pos2(0.0, 0.0), pos2(1.0, 0.0), pos2(1.0, 1.0), pos2(0.0, 1.0)]); +} + +fn paint_mapped_quad(painter: &egui::Painter, xf: &ViewXform, origin: [f32; 2], size: [f32; 2], color: Color32) { + let ([x, y], [w, h]) = (origin, size); + let pts = vec![xf.to_screen(x, y), xf.to_screen(x + w, y), xf.to_screen(x + w, y + h), xf.to_screen(x, y + h)]; + painter.add(egui::Shape::convex_polygon(pts, color, Stroke::NONE)); +} + +/// Draw boundary segments as marching ants: white base, black dashes phased along x + y. +pub(super) fn marching_ants_segments(painter: &egui::Painter, xf: &ViewXform, segs: &[crate::outline::Segment], time: f64) { + marching_ants_mapped(painter, segs, time, |p| xf.to_screen(p[0] as f32, p[1] as f32)); +} + +/// [`marching_ants_segments`] with each outline point placed on screen by `to_screen` (a +/// transformed outline: Content-Aware Move's Transform On Drop). +pub(crate) fn marching_ants_mapped(painter: &egui::Painter, segs: &[crate::outline::Segment], time: f64, to_screen: impl Fn([i32; 2]) -> Pos2) { + marching_ants_lines(painter, segs.iter().map(|(a, b)| (to_screen(*a), to_screen(*b))), time); +} + +/// The part of the line `a`–`b` inside `r` (Liang–Barsky), if any. +pub(crate) fn clip_line(r: Rect, a: Pos2, b: Pos2) -> Option<(Pos2, Pos2)> { + // Perspective can put endpoints billions of pixels away. f32 subtraction/interpolation + // loses viewport-sized differences there, clipping visible ants to the wrong endpoints. + let (ax, ay) = (f64::from(a.x), f64::from(a.y)); + let (dx, dy) = (f64::from(b.x) - ax, f64::from(b.y) - ay); + let (mut t0, mut t1) = (0.0f64, 1.0f64); + for (p, q) in [(-dx, ax - f64::from(r.min.x)), (dx, f64::from(r.max.x) - ax), (-dy, ay - f64::from(r.min.y)), (dy, f64::from(r.max.y) - ay)] { + if p == 0.0 { + if q < 0.0 { + return None; + } + } else if p < 0.0 { + t0 = t0.max(q / p); + } else { + t1 = t1.min(q / p); + } + } + let at = |t: f64| pos2((ax + dx * t) as f32, (ay + dy * t) as f32); + (t0 <= t1).then(|| (at(t0), at(t1))) +} + +/// Marching ants along lines already placed on screen (a warped outline is a run of short ones), +/// in device pixels like Photoshop's (`crate::ants`, #7489). +pub(crate) fn marching_ants_lines(painter: &egui::Painter, lines: impl Iterator, time: f64) { + crate::ants::draw(painter, lines, time, true); +} + +/// Photoshop crop overlay: the `shield` outside (`crop_shield::fill`; none when disabled), bright +/// frame, the composition `guides` (unit-square polylines, `crop_overlay::guides`), corner and +/// edge handles. +fn crop_overlay(painter: &egui::Painter, r: Rect, shield: Option, guides: &[crate::crop_overlay::Polyline]) { + let clip = painter.clip_rect(); + if let Some(dim) = shield { + for band in [ + Rect::from_min_max(clip.min, egui::pos2(clip.max.x, r.min.y)), + Rect::from_min_max(egui::pos2(clip.min.x, r.max.y), clip.max), + Rect::from_min_max(egui::pos2(clip.min.x, r.min.y), egui::pos2(r.min.x, r.max.y)), + Rect::from_min_max(egui::pos2(r.max.x, r.min.y), egui::pos2(clip.max.x, r.max.y)), + ] { + painter.rect_filled(band, 0.0, dim); + } + } + painter.rect_stroke(r, 0.0, Stroke::new(1.0, Color32::WHITE), egui::StrokeKind::Middle); + draw_crop_guides(painter, guides, |a, b| r.min + r.size() * vec2(a, b)); + let h = Stroke::new(3.0, Color32::WHITE); + let l = 14.0f32.min(r.width() / 3.0).min(r.height() / 3.0); + for (c, dx, dy) in [(r.left_top(), 1.0, 1.0), (r.right_top(), -1.0, 1.0), (r.left_bottom(), 1.0, -1.0), (r.right_bottom(), -1.0, -1.0)] { + painter.line_segment([c, c + vec2(l * dx, 0.0)], h); + painter.line_segment([c, c + vec2(0.0, l * dy)], h); + } + // Edge handles: short bars at the middle of each side. + let (lx, ly) = (l.min(r.width() / 4.0) / 2.0, l.min(r.height() / 4.0) / 2.0); + for (c, horizontal) in [(r.center_top(), true), (r.center_bottom(), true), (r.left_center(), false), (r.right_center(), false)] { + let e = if horizontal { vec2(lx, 0.0) } else { vec2(0.0, ly) }; + painter.line_segment([c - e, c + e], h); + } +} + +/// [`crop_overlay`] for a turned frame (or a rotated view): `q` is the frame's top-left, top-right, +/// bottom-right and bottom-left corner on screen, a parallelogram. +fn crop_overlay_turned(painter: &egui::Painter, q: [Pos2; 4], shield: Option, guides: &[crate::crop_overlay::Polyline]) { + let (u, v) = (q[1] - q[0], q[3] - q[0]); + let (lu, lv) = (u.length(), v.length()); + if !(lu.is_finite() && lv.is_finite()) || lu < 1e-3 || lv < 1e-3 { + return; + } + let at = |a: f32, b: f32| q[0] + u * a + v * b; + // The shield: four trapezoids from the frame out to the frame grown past the whole view. + let clip = painter.clip_rect(); + let reach = clip.size().length() + (clip.center() - at(0.5, 0.5)).length(); + let (eu, ev) = (reach / lu, reach / lv); + let outer = [at(-eu, -ev), at(1.0 + eu, -ev), at(1.0 + eu, 1.0 + ev), at(-eu, 1.0 + ev)]; + if let Some(dim) = shield { + let mut mesh = egui::Mesh::default(); + for p in q.iter().chain(outer.iter()) { + mesh.colored_vertex(*p, dim); + } + for i in 0..4u32 { + let j = (i + 1) % 4; + mesh.add_triangle(i, j, 4 + j); + mesh.add_triangle(i, 4 + j, 4 + i); + } + painter.add(egui::Shape::mesh(mesh)); + } + painter.add(egui::Shape::closed_line(q.to_vec(), Stroke::new(1.0, Color32::WHITE))); + draw_crop_guides(painter, guides, at); + let h = Stroke::new(3.0, Color32::WHITE); + let l = 14.0f32.min(lu / 3.0).min(lv / 3.0); + let (du, dv) = (u / lu, v / lv); + for (c, su, sv) in [(q[0], 1.0, 1.0), (q[1], -1.0, 1.0), (q[3], 1.0, -1.0), (q[2], -1.0, -1.0)] { + painter.line_segment([c, c + du * (l * su)], h); + painter.line_segment([c, c + dv * (l * sv)], h); + } + // Edge handles: short bars at the middle of each side, along it. + let (lx, ly) = (l.min(lu / 4.0) / 2.0, l.min(lv / 4.0) / 2.0); + for (c, e) in [(at(0.5, 0.0), du * lx), (at(0.5, 1.0), du * lx), (at(0.0, 0.5), dv * ly), (at(1.0, 0.5), dv * ly)] { + painter.line_segment([c - e, c + e], h); + } +} + +/// The crop overlay's guides, each unit-square point placed on screen by `at`. +fn draw_crop_guides(painter: &egui::Painter, guides: &[crate::crop_overlay::Polyline], at: impl Fn(f32, f32) -> Pos2) { + let thin = Stroke::new(1.0, Color32::from_white_alpha(90)); + for l in guides { + painter.add(egui::Shape::line(l.iter().map(|p| at(p[0], p[1])).collect(), thin)); + } +} + +/// What the Crop tool shows past the canvas, cached per document state ([`draw_beyond_canvas`]). +#[derive(Clone)] +struct BeyondCanvas { + snapshot: std::sync::Weak, + key: u64, + /// Every layer's pixels and the canvas (`extra_cmds::reveal_all_bounds`). + extent: DRect, + /// The composite around the canvas: each band, the area its texture covers, the texture. + bands: Vec<(DRect, DRect, egui::TextureHandle)>, +} + +/// Band `r` around the canvas `c` (on screen) pushed a point under the canvas's edge, so that the +/// edge's anti-aliased pixels blend with the band rather than with the pasteboard. +fn under_canvas(mut r: Rect, c: Rect) -> Rect { + let near = |a: f32, b: f32| (a - b).abs() < 0.01; + if near(r.max.y, c.min.y) { + r.max.y += 1.0; + } + if near(r.min.y, c.max.y) { + r.min.y -= 1.0; + } + if near(r.max.x, c.min.x) { + r.max.x += 1.0; + } + if near(r.min.x, c.max.x) { + r.min.x -= 1.0; + } + r +} + +/// `outer` minus `inner`: the bands above, below, left and right of it (empty ones dropped). +fn ring(outer: DRect, inner: DRect) -> Vec { + let i = inner.intersect(&outer); + if i.is_empty() { + return if outer.is_empty() { Vec::new() } else { vec![outer] }; + } + [ + DRect::new(outer.x0, outer.y0, outer.x1, i.y0), + DRect::new(outer.x0, i.y1, outer.x1, outer.y1), + DRect::new(outer.x0, i.y0, i.x0, i.y1), + DRect::new(i.x1, i.y0, outer.x1, i.y1), + ] + .into_iter() + .filter(|r| !r.is_empty()) + .collect() +} + +/// Crop tool, while a frame is edited (`crop_ui::shows_beyond_canvas`): like Photoshop's crop +/// preview, the canvas grows to every layer's pixels (a crop with Delete Cropped Pixels off keeps +/// them; a moved layer reaches past the edges) and to the frame, transparency drawn as the +/// checkerboard, all under the shield `crop_overlay` adds. Only the ring around the canvas is +/// composited, on the CPU, once per document state: at most `MAX_TEXTURE` texels per side, from a +/// downsampled proxy past that (so a far-off layer costs no more than a 16 MP composite). +/// Drawn before the canvas, each band reaching a point under its edge. Returns whether anything +/// past the canvas was drawn. +pub(super) fn draw_beyond_canvas(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, idx: usize, output: Option) -> bool { + let ctx = painter.ctx().clone(); + let Some((snapshot, id)) = app.session.documents().get(idx).map(|st| (std::sync::Arc::downgrade(&st.doc), st.doc.id)) else { return false }; + let key = egui::Id::unique(("crop-beyond-canvas", id.0, output)); + let frame = + app.ui.crop_rect.filter(|f| f.iter().all(|v| v.is_finite()) && app.session.active_index() == Some(idx) && crate::crop_ui::shows_beyond_canvas(app)); + let Some(f) = frame else { + ctx.data_mut(|d| d.remove::(key)); + return false; + }; + let (doc, preview_key) = display_doc(app, idx); + if doc.has_artboards() { + return false; + } + let (display, display_key) = canvas_display(app, &doc, output); + let doc_key = preview_key ^ display_key; + let cached: Option = ctx.data(|d| d.get_temp(key)); + let c = match cached.filter(|c| c.snapshot.ptr_eq(&snapshot) && c.key == doc_key) { + Some(c) => c, + None => { + let t0 = crate::gpu_canvas::now_ms(); + let extent = photocraft_engine::extra_cmds::reveal_all_bounds(&doc); + // One texel per `k` document pixels keeps every texture within the device's side. + let k = extent.width().max(extent.height()).div_ceil(texture_side(&ctx)).max(1); + let proxy = (k > 1).then(|| photocraft_compose::proxy::proxy_document(&doc, k)); + let src = proxy.as_ref().unwrap_or(&*doc); + let k = k as i32; + let opts = TextureOptions { magnification: egui::TextureFilter::Nearest, ..TextureOptions::LINEAR }; + let bands = ring(extent, doc.bounds()) + .into_iter() + .map(|b| { + // The proxy pixels over `b`: document pixels / k, rounded outwards. + let up = |v: i32| v.div_euclid(k) + i32::from(v.rem_euclid(k) != 0); + let p = DRect::new(b.x0.div_euclid(k), b.y0.div_euclid(k), up(b.x1), up(b.y1)); + let buf = photocraft_compose::render_reduced_rect(src, p, p.width(), p.height()); + let tex = ctx.load_texture("crop-beyond-canvas", display_image(display.as_deref(), &buf), opts); + (b, DRect::new(p.x0.saturating_mul(k), p.y0.saturating_mul(k), p.x1.saturating_mul(k), p.y1.saturating_mul(k)), tex) + }) + .collect(); + app.perf.span("crop-beyond-canvas", crate::gpu_canvas::now_ms() - t0); + let c = BeyondCanvas { snapshot, key: doc_key, extent, bands }; + ctx.data_mut(|d| d.insert_temp(key, c.clone())); + c + } + }; + // Transparency out to the frame, in phase with the canvas's own checkerboard. + let canvas = xf.doc_rect(doc.bounds()); + // A turned frame reaches as far as its corners. + let f = crate::crop_ui::turned_bounds(f, crate::crop_ui::angle(app)); + let fr = DRect::new(f[0].floor() as i32, f[1].floor() as i32, f[2].ceil() as i32, f[3].ceil() as i32); + let square = app.session.prefs().transparency_and_gamut.square(); + let checker_id = square.map(|_| checker(app, &ctx)); + let around = ring(c.extent.union(&fr), doc.bounds()); + // A turned view (Rotate View, or the Crop tool's default mode turning the image behind its + // box) draws each band as its turned quad: its screen bounding box would stretch the band's + // texture and spread the checkerboard past the band. + let turned = xf.rotation != 0.0; + let checker_uv = |p: Pos2, sq: f32| ((p - canvas.min) / (2.0 * sq)).to_pos2(); + for &b in &around { + if turned { + let q = doc_quad(xf, b); + match square.zip(checker_id) { + Some((sq, id)) => textured_quad(painter, id, q, q.map(|p| checker_uv(p, sq))), + None => { + painter.add(egui::Shape::convex_polygon(q.to_vec(), Color32::WHITE, Stroke::NONE)); + } + } + continue; + } + let r = under_canvas(xf.doc_rect(b), canvas); + match square.zip(checker_id) { + Some((sq, id)) => { + let uv = Rect::from_min_max(checker_uv(r.min, sq), checker_uv(r.max, sq)); + painter.image(id, r, uv, Color32::WHITE); + } + None => { + painter.rect_filled(r, 0.0, Color32::WHITE); + } + } + } + for (b, t, tex) in &c.bands { + let part = |a: i32, o: i32, len: u32| (i64::from(a) - i64::from(o)) as f32 / len.max(1) as f32; + let (mut u0, mut u1) = (part(b.x0, t.x0, t.width()), part(b.x1, t.x0, t.width())); + if turned { + // The quad's corners are the band's own (flip included), so no swap. + let (v0, v1) = (part(b.y0, t.y0, t.height()), part(b.y1, t.y0, t.height())); + textured_quad(painter, tex.id(), doc_quad(xf, *b), [pos2(u0, v0), pos2(u1, v0), pos2(u1, v1), pos2(u0, v1)]); + continue; + } + if xf.flip { + (u0, u1) = (u1, u0); + } + let uv = Rect::from_min_max(pos2(u0, part(b.y0, t.y0, t.height())), pos2(u1, part(b.y1, t.y0, t.height()))); + // The texture's edge texels stretch over the point under the canvas. + let (r, e) = (xf.doc_rect(*b), under_canvas(xf.doc_rect(*b), canvas)); + let at = |p: Pos2| uv.min + (p - r.min) / r.size() * uv.size(); + painter.image(tex.id(), e, Rect::from_min_max(at(e.min), at(e.max)), Color32::WHITE); + } + !around.is_empty() +} + +/// Document rect `b`'s corners on screen, clockwise from its top-left (a parallelogram in a turned +/// view). +fn doc_quad(xf: &ViewXform, b: DRect) -> [Pos2; 4] { + [(b.x0, b.y0), (b.x1, b.y0), (b.x1, b.y1), (b.x0, b.y1)].map(|(x, y)| xf.to_screen(x as f32, y as f32)) +} + +/// Texture `tex` drawn over the quad `q`, `uv` at its corners. +fn textured_quad(painter: &egui::Painter, tex: egui::TextureId, q: [Pos2; 4], uv: [Pos2; 4]) { + if !q.iter().chain(uv.iter()).all(|p| p.x.is_finite() && p.y.is_finite()) { + return; + } + let mut mesh = egui::Mesh::with_texture(tex); + for (pos, uv) in q.into_iter().zip(uv) { + mesh.vertices.push(egui::epaint::Vertex { pos, uv, color: Color32::WHITE }); + } + mesh.add_triangle(0, 1, 2); + mesh.add_triangle(0, 2, 3); + painter.add(egui::Shape::mesh(mesh)); +} + +/// Overlays that persist between gestures: polygonal or magnetic lasso in progress, pending crop box. +pub(super) fn draw_tool_state(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, hover: Option) { + crate::magnetic_lasso_ui::draw(app, painter, xf, hover); + if !app.ui.polygon.is_empty() { + let mut pts: Vec = app.ui.polygon.iter().map(|p| xf.to_screen(p[0] as f32, p[1] as f32)).collect(); + if let Some(h) = hover { + // The rubber-band preview must use the same constrained point as the click, + // including when the canvas is zoomed, rotated or flipped. + let mods = painter.ctx().input(|i| i.modifiers); + if app.ui.tool == Tool::PolygonLasso && mods.shift && !mods.alt { + let raw = xf.to_doc(h); + let last = app.ui.polygon.last().copied().unwrap_or(raw); + let snapped = constrained_polygon_point(last, raw); + pts.push(xf.to_screen(snapped[0] as f32, snapped[1] as f32)); + } else { + pts.push(h); + } + } + // Just the outline and its rubber band (the vertices aren't handles to grab), solid until + // the selection is made (#7495). + crate::tool_feedback::draw_solid_path(painter, &pts, false); + } + crate::crop_straighten::draw(app, painter, |p| xf.to_screen(p[0] as f32, p[1] as f32), hover); + crate::blur_pins::draw(app, painter, xf); + if let Some(c) = app.ui.crop_rect { + let deg = crate::crop_ui::angle(app); + let shield = crate::crop_shield::current(app, painter.ctx()); + if deg == 0.0 && xf.rotation == 0.0 { + let r = Rect::from_two_pos(xf.to_screen(c[0] as f32, c[1] as f32), xf.to_screen(c[2] as f32, c[3] as f32)); + crop_overlay(painter, r, shield, &crate::crop_overlay::current_guides(app, r.width(), r.height())); + } else { + let q = crate::crop_ui::corners(c, deg).map(|p| xf.to_screen(p[0] as f32, p[1] as f32)); + crop_overlay_turned(painter, q, shield, &crate::crop_overlay::current_guides(app, q[0].distance(q[1]), q[0].distance(q[3]))); + } + // The frame's angle beside the pointer while it turns (#1792). + if let (Some(a), Some(h)) = (crate::crop_ui::turning(app), hover) { + draw_value_readout(app, painter.ctx(), "crop-angle-readout", h, ["Angle:"], [angle_readout(a)]); + } else if let (Some([w, h]), Some(at)) = (crate::crop_ui::sizing(app), hover) { + // Its W × H while it is drawn or resized (#1919), like the marquee's. + draw_marquee_readout(app, painter.ctx(), at, size_readout(app, [0.0, 0.0, w, h])); + } + } +} + +pub(super) fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) { + draw_tool_state(app, painter, xf, painter.ctx().input(|i| i.pointer.hover_pos())); + // Remove strokes waiting for ↩ stay tinted between drags. + crate::retouch_ui::draw_remove_pending(app, painter, xf); + let Some(d) = &app.drag else { + app.trail = None; + return; + }; + // The Move tool's ΔX/ΔY beside the pointer, for layers and selected pixels alike (#5727). + if let Some(values) = crate::move_ui::offset_readout(app) { + let at = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + draw_value_readout(app, painter.ctx(), "move-readout", xf.to_screen(at[0] as f32, at[1] as f32), ["ΔX:", "ΔY:"], values); + } + // Dragging the selection: its own ants follow the pointer (`selection_shown_offset`). + if d.sel_move.is_some() { + return; + } + let last = d.points.last().map(|p| [p[0], p[1]]).unwrap_or(d.start); + let marquee = marquee_preview_px(&app.ui.tool_options, d, app.point_zoom()); + if let Some(r) = marquee { + draw_marquee_readout(app, painter.ctx(), xf.to_screen(last[0] as f32, last[1] as f32), size_readout(app, r)); + } + match d.tool { + // The canvas shows the live stroke itself (`LiveStroke`); a stroke that couldn't start one + // (Sample All Layers, say) keeps the trail. + t if strokes_live(t) && app.live_stroke.is_some() => { + // Smoothing holds the brush back from the pointer: draw the string between them. + if let Some(EngineStroke::Brush(l)) = app.live_stroke.as_ref().map(|l| &l.stroke) + && let Some(s) = l.smoothing_string() + { + draw_smoothing_string(painter, xf, s); + } + } + t if t.is_brushlike() || t == Tool::QuickSelection => { + // Retouching strokes preview as a translucent trail of the brush footprint: a mask, + // not a brush-wide egui polyline (which zoomed in tessellates into wedges, #189). + let col = match t { + Tool::QuickSelection => Color32::from_white_alpha(40), + // The Remove Tool tints what it will remove, like Photoshop's. + Tool::Remove => crate::theme::Tokens::get(painter.ctx()).danger.gamma_multiply(0.45), + // Photoshop paints the area to heal in half-opaque dark grey while dragging; a faint + // white trail barely showed on light pixels (#7342). The heal itself is computed on + // release. + Tool::SpotHealing => SPOT_HEALING_TRAIL, + _ => Color32::from_white_alpha(60), + }; + let Some(st) = app.session.active() else { return }; + let size = [st.doc.size.width, st.doc.size.height]; + let doc_rect = xf.doc_rect(st.doc.bounds()); + let trail = app.trail.get_or_insert_with(|| crate::stroke_trail::Trail::new(size)); + let brush = || photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, &json!({})); + match t { + // Quick Selection reads the brush's size alone (`select.quick`). + Tool::QuickSelection => trail.feed(&d.points, app.session.tools.brush.size), + // Remove removes every pixel a dab touches, so its whole reach is tinted: a soft + // tip's reaches well past the nominal radius. + Tool::Remove => trail.feed_reach(&d.points, brush), + // What the stroke covers, dab by dab: soft edges, pressure and shape dynamics + // (Spot Healing #2364; the Mixer Brush, and a tool whose live stroke didn't start). + _ => trail.feed_dabs(&d.points, brush), + } + trail.draw(painter, doc_rect, xf.flip, col); + } + t if crate::vector_ui::is_shape_tool(t) => crate::vector_ui::draw_shape_preview(app, painter, xf, t, d.start, last, d.live), + Tool::SingleRowMarquee | Tool::SingleColumnMarquee => { + // Mapped corner by corner like the other marquees, so a rotated view shows the line + // where the release commits it (#2512). + if let Some([x0, y0, x1, y1]) = single_line_drag_px(app, d) { + crate::tool_feedback::draw_ants(painter, &marquee_ants(xf, d.tool, [x0, y0], [x1, y1]), true); + } + } + // Nothing selected yet: no outline, as the release would deselect. + Tool::RectMarquee | Tool::EllipseMarquee if marquee.is_none() => {} + Tool::RectMarquee | Tool::EllipseMarquee | Tool::ObjectSelection => { + // Marching ants, visible on any pixels (#172): built in document space and mapped + // corner by corner, so a rotated view shows them where the release commits (#2512). + let pts = marquee_drag_ants(d, xf, marquee); + crate::tool_feedback::draw_ants(painter, &pts, true); + } + // Patch / Content-Aware Move dragging the selection: its outline follows the pointer. + Tool::Patch | Tool::ContentAwareMove if crate::retouch_ui::patch_drags_selection(app, d.start, d.modifiers) => { + let start = d.start; + let [dx, dy] = crate::retouch_ui::patch_offset(app, start, last); + if let Some((_, _, segs)) = &app.outline_cache { + let moved: Vec = segs.iter().map(|(a, b)| ([a[0] + dx, a[1] + dy], [b[0] + dx, b[1] + dy])).collect(); + marching_ants_segments(painter, xf, &moved, painter.ctx().input(|i| i.time)); + } + } + Tool::FreeformPen => { + let mut pts: Vec = d.points.iter().map(|p| xf.to_screen(p[0] as f32, p[1] as f32)).collect(); + // Between ⌥-clicks: the straight segment the next click makes. + if let Some(lasso) = &d.lasso { + pts.push(xf.to_screen(lasso.cursor[0] as f32, lasso.cursor[1] as f32)); + } + painter.add(egui::Shape::line(pts, Stroke::new(1.5, crate::theme::Tokens::get(painter.ctx()).accent))); + } + Tool::Lasso | Tool::Patch | Tool::ContentAwareMove => { + let mut pts: Vec = d.points.iter().map(|p| xf.to_screen(p[0] as f32, p[1] as f32)).collect(); + if let Some(lasso) = &d.lasso { + pts.push(xf.to_screen(lasso.cursor[0] as f32, lasso.cursor[1] as f32)); + } + // The Lasso's outline is solid until the release makes the selection (#7495); a Patch + // or Content-Aware Move area keeps its ants. + if d.tool == Tool::Lasso { + // On pixel centres, so a 1 pt line stays crisp wherever the canvas sits (a pixel + // edge would blur it to grey). + let pts: Vec = pts.into_iter().map(|p| p.round() + vec2(0.5, 0.5)).collect(); + crate::tool_feedback::draw_solid_path(painter, &pts, false); + } else { + crate::tool_feedback::draw_ants(painter, &pts, false); + } + } + Tool::Gradient => { + let a = xf.to_screen(d.start[0] as f32, d.start[1] as f32); + let b = xf.to_screen(last[0] as f32, last[1] as f32); + painter.line_segment([a, b], Stroke::new(3.0, Color32::from_black_alpha(140))); + painter.line_segment([a, b], Stroke::new(1.0, Color32::WHITE)); + painter.circle_filled(a, 3.0, Color32::WHITE); + painter.circle_filled(b, 3.0, Color32::WHITE); + } + Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask => { + let r = Rect::from_two_pos(xf.to_screen(d.start[0] as f32, d.start[1] as f32), xf.to_screen(last[0] as f32, last[1] as f32)); + let pts = [r.left_top(), r.right_top(), r.right_bottom(), r.left_bottom(), r.left_top()]; + painter.add(egui::Shape::line(pts.to_vec(), Stroke::new(1.0, Color32::WHITE))); + painter.add(egui::Shape::dashed_line(&pts, Stroke::new(1.0, Color32::BLACK), 3.0, 3.0)); + } + // The layers themselves follow the pointer (`move_ui`); the arrow only when they can't. + Tool::Move if !crate::move_ui::showing(app) => { + let from = xf.to_screen(d.start[0] as f32, d.start[1] as f32); + let off = xf.to_screen(last[0] as f32, last[1] as f32) - from; + painter.arrow(from, off, Stroke::new(2.0, crate::theme::Tokens::get(painter.ctx()).accent)); + } + _ => {} + } +} diff --git a/crates/ui-egui/src/canvas/paint_mouse.rs b/crates/ui-egui/src/canvas/paint_mouse.rs new file mode 100644 index 0000000000..c0db27eb87 --- /dev/null +++ b/crates/ui-egui/src/canvas/paint_mouse.rs @@ -0,0 +1,294 @@ +//! Mouse buttons on the canvas. Tools follow the left button. The right button never paints with +//! the tool: with a painting tool it opens the Brush Preset picker at the pointer (Photoshop), or, +//! with Preferences › Tools › Right-click with painting tools set to Erase, a right drag with the +//! Brush erases with the current brush (Krita, Paint). Each right stroke is one `paint.stroke` +//! with `"erase": true`, so one undo step, with the pen pressure and tilt of a normal stroke. + +use egui::{PointerButton, Response}; +use photocraft_engine::BrushSettings; +use photocraft_engine::prefs::RightClickPaint; + +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The brush is a tool option, kept per tool like Photoshop's options bar: switching from the +/// Brush to the Eraser saves the session brush for the old tool and loads the new tool's, so each +/// keeps its own size, hardness, mode, opacity, dynamics and smoothing. A tool seen for the +/// first time takes what the brush already has, at Photoshop's 10 % smoothing. Other tools leave +/// the brush alone. +/// +/// The foreground and background colours, the erase flag and the stroke seed belong to the +/// session rather than to the tool, so a swap never carries them between tools: picking a colour +/// or painting with the Eraser works with any tool's brush. +pub fn sync_tool_brush(app: &mut PhotocraftApp) { + let tool = app.ui.tool; + if !has_brush_picker(tool) || app.ui.brush_tool == tool { + return; + } + // The live brush is the outgoing tool's, so save it there before the new one takes over: any + // edit it made since it was last active is in it, so its copy never goes stale. + let current = app.session.tools.brush.clone(); + save_brush(app, app.ui.brush_tool, current.clone()); + app.session.tools.brush = match app.ui.tool_brushes.iter().find(|(t, _)| *t == tool) { + Some((_, b)) => { + // A brush remembered from the previous run holds its stored tips as references: + // load their full bitmaps (a missing one keeps painting with its preview). + let mut b = session_fields(¤t, b); + let _ = app.session.load_brush_tips(&mut b); + b + } + // A tool seen for the first time takes what the brush has, at Photoshop's smoothing, but + // in Normal mode: each tool's Mode starts at Normal, so a Multiply Brush doesn't make the + // first Clone Stamp stroke multiply (or give Healing a mode its menu doesn't have). + None => BrushSettings { smoothing: first_smoothing(), mode: photocraft_color::BlendMode::Normal, ..current }, + }; + // The picked preset was the outgoing tool's brush: forget it, so Update Brush can't overwrite + // that preset with this tool's brush. + app.session.tools.current_preset = None; + app.session.tools.brush_changed(); + app.ui.brush_tool = tool; +} + +/// The brush `tool` had when it was last active. +fn save_brush(app: &mut PhotocraftApp, tool: Tool, brush: BrushSettings) { + match app.ui.tool_brushes.iter_mut().find(|(t, _)| *t == tool) { + Some(e) => e.1 = brush, + None => app.ui.tool_brushes.push((tool, brush)), + } +} + +/// The session's own brush fields, which a tool swap never carries: the colours, the erase flag +/// and the stroke seed (a stroke's replay seed is the command's, not the tool's). +fn session_fields(current: &BrushSettings, brush: &BrushSettings) -> BrushSettings { + BrushSettings { color: current.color, background: current.background, erase: current.erase, seed: current.seed, ..brush.clone() } +} + +/// A tool's brush as it starts out (Reset Tool, #2825): the default brush at Photoshop's +/// smoothing, with the session's own fields of `current`. +pub(crate) fn fresh_brush(current: &BrushSettings) -> BrushSettings { + session_fields(current, &BrushSettings { smoothing: first_smoothing(), ..BrushSettings::default() }) +} + +/// Photoshop's 10 % stroke smoothing, what a tool's brush starts at. +fn first_smoothing() -> photocraft_engine::paint::brush::Smoothing { + photocraft_engine::paint::brush::Smoothing { amount: 0.1, ..Default::default() } +} + +/// Does a stroke with `tool` starting now paint in Photoshop's Clear mode, erasing with its own +/// brush (#3439)? Clear chosen in its Mode dropdown, or `~` held (`hold_keys::erase_held`). +pub fn clears(app: &PhotocraftApp, tool: Tool) -> bool { + let chosen = match tool { + Tool::Brush => app.ui.tool_options.brush_clear, + Tool::Pencil => app.ui.tool_options.pencil_clear, + _ => false, + }; + crate::hold_keys::erases_while_held(tool) && (chosen || app.held_erase) +} + +/// Which tool events this frame's canvas response produces. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Buttons { + pub started: bool, + pub dragged: bool, + pub stopped: bool, + pub clicked: bool, +} + +/// Tools with the options-bar brush chip, which opens the Brush Preset picker. +pub fn has_brush_picker(tool: Tool) -> bool { + tool.is_brushlike() || tool == Tool::QuickSelection +} + +/// Tools whose canvas right-click opens the Brush Preset picker: the painting tools. The Quick +/// Selection tool has the picker too, but its right-click opens the selection menu (#7268). +pub fn right_click_picker(tool: Tool) -> bool { + has_brush_picker(tool) && !crate::canvas_tool_menu::applies(tool) +} + +/// Does a right-button drag with `tool` erase (rather than open the picker)? +pub fn right_erases(app: &PhotocraftApp, tool: Tool) -> bool { + matches!(tool, Tool::Brush | Tool::Eraser) && app.session.prefs().tools.right_click_with_painting_tools == RightClickPaint::Erase +} + +/// Route the canvas response's buttons: the left one drives the tool; the right one resizes the +/// brush with Alt held (#297), erases (Erase preference) or opens the Brush Preset picker. Arms +/// `secondary_erase` or `brush_resize_armed` for this frame's `Down`. +pub fn canvas_buttons(app: &mut PhotocraftApp, response: &Response, tool: Tool) -> Buttons { + if crate::brush_mode_menu::blocks_canvas(app, &response.ctx) { + app.secondary_erase = false; + return Buttons::default(); + } + let physical_right = app.services.right_button_down.as_ref().is_some_and(|f| f()); + let (mods, mut right_down) = response.ctx.input(|i| (i.modifiers, i.pointer.secondary_down())); + right_down |= physical_right; + let right_started = response.drag_started_by(PointerButton::Secondary) || (physical_right && response.drag_started()); + let right_dragged = response.dragged_by(PointerButton::Secondary) || (physical_right && response.dragged()); + let resizing = app.brush_resize.is_some_and(|r| r.secondary); + let right_stopped = response.drag_stopped_by(PointerButton::Secondary) || (resizing && response.drag_stopped()); + + if physical_right && crate::brush_resize::is_right_gesture(crate::workspace_ui::sticky_mods(app, mods)) { + app.alt_sampling = false; + } + + // Alt+right-drag resizes the brush (brush_resize.rs); its events reach `tool_event` like a + // left drag's, and nothing paints. A resize whose release was missed ends here. + crate::brush_resize::release_stale(app, right_down || response.drag_stopped_by(PointerButton::Secondary) || response.drag_stopped()); + let resize_start = crate::brush_resize::applies(tool) + && app.drag.is_none() + && crate::brush_resize::is_right_gesture(crate::workspace_ui::sticky_mods(app, mods)) + && (right_started || (physical_right && response.dragged() && app.brush_resize.is_none())); + let resizing = app.brush_resize.is_some_and(|r| r.secondary); + app.brush_resize_armed = resize_start || (physical_right && resizing); + // ⌘/Ctrl+right-click lists the layers under the pointer instead (layer_pick_ui.rs, #307). + let layer_menu = crate::layer_pick_ui::is_gesture(tool, mods); + // Decided by the modifiers held when the button went down, not by those of the release frame that reports the click. + let mode_menu = crate::brush_mode_menu::is_gesture(tool, crate::workspace_ui::sticky_mods(app, crate::brush_mode_menu::gesture_mods(&response.ctx, mods))); + if mode_menu && !resizing && app.drag.is_none() { + app.secondary_erase = false; + if response.secondary_clicked() + && let Some(p) = response.interact_pointer_pos() + { + crate::brush_mode_menu::open(app, &response.ctx, [p.x, p.y]); + } + return Buttons { + started: response.drag_started_by(PointerButton::Primary), + dragged: response.dragged_by(PointerButton::Primary), + stopped: response.drag_stopped_by(PointerButton::Primary), + clicked: response.clicked(), + }; + } + let erase = right_erases(app, tool) && !resize_start && !resizing && !layer_menu; + // Shift+Alt+right-press opens Photoshop's HUD Color Picker (#6098) instead of the picker. + // Tools whose right-click is the canvas tool menu (Quick Selection, #7268) keep that menu. + let sticky = crate::workspace_ui::sticky_mods(app, mods); + let hud_gesture = right_click_picker(tool) && !layer_menu && !erase && sticky.shift && sticky.alt; + let right_press = response.ctx.input(|i| i.pointer.button_pressed(PointerButton::Secondary)) && response.contains_pointer(); + if hud_gesture + && right_press + && let Some(p) = response.ctx.input(|i| i.pointer.hover_pos()) + { + crate::color_hud::open(app, [p.x, p.y], true); + } + let right_stroke = erase && app.drag.is_some(); + let right_start = erase && right_started; + let right_click = response.secondary_clicked(); + if right_click + && !erase + && !layer_menu + && !crate::color_hud::is_open(&app.ui) + && right_click_picker(tool) + && let Some(p) = response.interact_pointer_pos() + { + crate::brush_picker::open(&mut app.ui, &response.ctx, [p.x, p.y]); + } + let erase_click = erase && right_click; + app.secondary_erase = right_start || erase_click; + let right_drag = right_stroke || resizing; + Buttons { + started: response.drag_started_by(PointerButton::Primary) || right_start || resize_start, + dragged: response.dragged_by(PointerButton::Primary) || (right_drag && right_dragged), + stopped: response.drag_stopped_by(PointerButton::Primary) || (right_drag && right_stopped), + clicked: response.clicked() || erase_click, + } +} + +/// `ui.pointer` with `"button": "secondary"`: true when its events should reach the tool (an +/// Alt+right-drag brush resize, `brush_resize_armed` for its `Down`; or an erasing right stroke, +/// `secondary_erase` armed for its `Down`). Otherwise a right-click with a painting tool opens +/// the Brush Preset picker at screen point `at`, and nothing paints. +pub fn pointer_secondary(app: &mut PhotocraftApp, ctx: &egui::Context, down: bool, mods: egui::Modifiers, at: [f32; 2]) -> bool { + let tool = app.ui.tool; + // Shift+Alt+right is Photoshop's HUD Color Picker (#6098): the down opens it, the up closes + // it. As on the canvas, the right-erase preference wins over the HUD, and a tool whose + // right-click is the canvas tool menu keeps that menu (handled by the caller). + let hud_gesture = right_click_picker(tool) && mods.shift && mods.alt && !right_erases(app, tool); + // A control-channel request carries one modifier set, so the Up need not repeat the Down's: + // the caller may release Shift and Alt first. Any Up therefore closes a non-held HUD that is + // still open; `release` leaves a real-mouse (held) one to its button release. + let hud_up = !down && app.ui.color_hud.is_some_and(|h| !h.hold); + if hud_gesture || hud_up { + if down { + crate::color_hud::open(app, at, false); + } else { + crate::color_hud::release(&mut app.ui); + } + return false; + } + if crate::brush_resize::applies(tool) && (crate::brush_resize::is_right_gesture(mods) || app.brush_resize.is_some_and(|r| r.secondary)) { + app.brush_resize_armed = down && app.drag.is_none(); + return true; + } + if crate::brush_mode_menu::is_gesture(tool, mods) && app.drag.is_none() && app.brush_resize.is_none() { + app.secondary_erase = false; + if down { + crate::brush_mode_menu::open(app, ctx, at); + } + return false; + } + if right_erases(app, tool) { + app.secondary_erase = down; + return true; + } + if down && right_click_picker(tool) { + crate::brush_picker::open(&mut app.ui, ctx, at); + } + false +} + +/// The Brush Preset picker a right-click or the options-bar brush chip opened, at the pointer. It +/// edits the session brush like the Brushes panel; a press outside, Escape, Enter or a +/// double-click on a preset closes it. +pub fn show_picker(app: &mut PhotocraftApp, ctx: &egui::Context) { + let Some([x, y]) = app.ui.brush_picker else { return }; + if !has_brush_picker(app.ui.tool) { + app.ui.brush_picker = None; + return; + } + // Escape and Enter close it once this frame's edits are in (a typed size applies), unless they + // end a rename in its rename bar. + let key_close = app.ui.brush_picker_list.renaming.is_none() && ctx.input(|i| i.key_pressed(egui::Key::Escape) || i.key_pressed(egui::Key::Enter)); + let screen = ctx.content_rect(); + // The picker's content size: the one its grip was dragged to, or the one it last closed with + // (kept in egui's own memory, which outlives the app's), or the default. The frame's chrome + // sits around it, and together they keep the whole picker on screen. + let id = crate::brush_picker::picker_id(); + let size_id = id.with("content-size"); + let content = app.ui.brush_picker_size.or_else(|| ctx.data_mut(|d| d.get_persisted::<[f32; 2]>(size_id))).map_or_else( + || egui::vec2(crate::brush_picker::DEFAULT_SIZE[0], crate::brush_picker::DEFAULT_SIZE[1]), + |s| crate::brush_picker::clamp_size(s, screen.size()), + ); + // Remember it for the picker's next open, and — egui's memory being saved — the next run. + ctx.data_mut(|d| d.insert_persisted(size_id, [content.x, content.y])); + let style = ctx.global_style(); + let size = content + style.spacing.menu_margin.sum() + egui::Vec2::splat(2.0 * style.visuals.window_stroke().width); + let pos = egui::pos2(x.min(screen.right() - size.x).max(screen.left()), y.min(screen.bottom() - size.y).max(screen.top())); + let area = egui::Area::new(id).order(egui::Order::Foreground).fixed_pos(pos).show(ctx, |ui| { + let before = app.session.tools.brush.clone(); + let mut b = before.clone(); + let list = &mut app.ui.brush_picker_list; + let frame = egui::Frame::popup(ui.style()) + .show(ui, |ui| crate::brush_picker::body(ui, &mut b, &app.session.tools.presets, app.session.tools.current_preset.as_deref(), list, content)); + let picks = frame.inner; + // The corner grip resizes the picker, and the size is remembered across opens. + if let Some(new) = crate::brush_picker::resize_grip(ui, frame.response.rect, content) { + app.ui.brush_picker_size = Some([new.x, new.y]); + } + crate::brush_panel::commit_gesture(app, ui.ctx(), &before, &b); + let closes = picks.iter().any(crate::brush_picker::Pick::closes); + crate::brush_picker::apply(app, ui.ctx(), picks); + closes + }); + // Not a press on a menu the picker opened (the gear, a preset's context menu), nor on the + // options-bar chip, whose click toggles the picker. A menu floats over the picker in the + // foreground order, so the press is told apart by the layer it lands on, not by any popup + // just being open: a press elsewhere closes the picker in the same click that closes the menu. + let press = ctx.input(|i| i.pointer.any_pressed().then(|| i.pointer.interact_pos()).flatten()); + let on_menu = press.is_some_and(|p| ctx.layer_id_at(p).is_some_and(|l| l.order == egui::Order::Foreground)); + let outside = crate::canvas_popup::outside_pressed(ctx, id, area.response.rect) && press.is_some_and(|p| !crate::brush_picker::on_chip(ctx, p)) && !on_menu; + if outside || key_close || area.inner { + crate::brush_picker::close(&mut app.ui); + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/paint_mouse/tests.rs b/crates/ui-egui/src/canvas/paint_mouse/tests.rs new file mode 100644 index 0000000000..72c74cd62e --- /dev/null +++ b/crates/ui-egui/src/canvas/paint_mouse/tests.rs @@ -0,0 +1,692 @@ +use egui::{Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use serde_json::json; + +use super::*; +use crate::canvas::{ToolEvent, tool_event}; + +fn harness(prefs: Option<&str>) -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("tools.setColors", json!({"foreground": "#0000ff"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 20, "hardness": 1.0}})).unwrap(); + if let Some(v) = prefs { + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": v})).unwrap(); + } + app.ui.tool = Tool::Brush; + app.sync_views(); + let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + h +} + +fn press(h: &mut Harness<'static, PhotocraftApp>, pos: Pos2, button: PointerButton, pressed: bool) { + h.event(egui::Event::PointerButton { pos, button, pressed, modifiers: Modifiers::NONE }); + h.run_steps(2); +} + +/// Drag across the canvas centre with `button`; returns the document points it covered. +fn drag(h: &mut Harness<'static, PhotocraftApp>, button: PointerButton) -> (Pos2, Pos2) { + let c = h.state().last_canvas_rect.center(); + let (a, b) = (c - vec2(80.0, 0.0), c + vec2(80.0, 0.0)); + h.event(egui::Event::PointerMoved(a)); + h.run_steps(1); + press(h, a, button, true); + for i in 1..=8 { + h.event(egui::Event::PointerMoved(a + (b - a) * (i as f32 / 8.0))); + h.run_steps(1); + } + press(h, b, button, false); + (a, b) +} + +fn alpha_at(h: &Harness<'static, PhotocraftApp>, screen: Pos2) -> f32 { + let app = h.state(); + let v = app.ui.views[0].clone(); + let r = app.last_canvas_rect; + let d = (screen - r.center()) / v.zoom; + let (x, y) = ((d.x + v.center[0]) as i32, (d.y + v.center[1]) as i32); + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y)[3] +} + +fn strokes(h: &Harness<'static, PhotocraftApp>) -> Vec { + h.state().session.journal.iter().filter(|(id, _)| id == "paint.stroke").map(|(_, p)| p.clone()).collect() +} + +fn mode_right_click(h: &mut Harness<'static, PhotocraftApp>, mods: Modifiers) { + let p = h.state().last_canvas_rect.center(); + h.event(egui::Event::ModifiersChanged(mods)); + h.event(egui::Event::PointerMoved(p)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: mods }); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: mods }); + h.event(egui::Event::ModifiersChanged(Modifiers::NONE)); + h.run_steps(2); +} + +#[test] +fn shift_right_mode_picker_selects_journals_and_clear_erases() { + use egui_kittest::kittest::Queryable; + let mut h = harness(Some("erase")); + let history = h.state().session.active().unwrap().history.past_len(); + let journal = h.state().session.journal.len(); + mode_right_click(&mut h, Modifiers::SHIFT); + assert!(h.state().ui.brush_mode_menu.is_some()); + assert!(h.state().ui.brush_picker.is_none() && h.state().drag.is_none()); + assert_eq!(h.state().session.journal.len(), journal); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history); + h.get_by_label("Multiply").click(); + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_none()); + assert_eq!(h.state().session.tools.brush.mode, photocraft_doc::BlendMode::Multiply); + assert_eq!(h.state().session.journal.last().unwrap(), &("tools.setBrush".into(), json!({"brush": {"mode": "Multiply", "behind": false}}))); + let (a, b) = drag(&mut h, PointerButton::Primary); + let mid = a + (b - a) * 0.5; + assert!(alpha_at(&h, mid) > 0.9); + mode_right_click(&mut h, Modifiers::SHIFT); + h.get_by_label("Clear").click(); + h.run_steps(2); + assert!(h.state().ui.tool_options.brush_clear); + assert_eq!(h.state().session.tools.brush.mode, photocraft_doc::BlendMode::Multiply, "Clear keeps the underlying mode, as the options bar does"); + drag(&mut h, PointerButton::Primary); + assert_eq!(alpha_at(&h, mid), 0.0); + assert!(strokes(&h).last().unwrap()["erase"].as_bool().unwrap()); +} + +#[test] +fn mode_picker_outside_dismissal_does_not_paint_or_erase() { + for button in [PointerButton::Primary, PointerButton::Secondary] { + let mut h = harness(Some("erase")); + drag(&mut h, PointerButton::Primary); + let journal = h.state().session.journal.len(); + let history = h.state().session.active().unwrap().history.past_len(); + mode_right_click(&mut h, Modifiers::SHIFT); + // A full outside drag must only dismiss, including its release click. + drag(&mut h, button); + assert!(h.state().ui.brush_mode_menu.is_none()); + assert_eq!(h.state().session.journal.len(), journal, "dismissal must not paint/erase with {button:?}"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history); + } +} + +#[test] +fn mode_control_can_pick_a_row_with_ui_click() { + use crate::control::{ControlRequest, Outcome, handle}; + use egui_kittest::kittest::Queryable; + let mut h = harness(Some("erase")); + let ctx = h.ctx.clone(); + let before = h.state().session.journal.len(); + let (req, _) = ControlRequest::new( + "ui.pointer", + json!({"button": "right", "shift": true, + "events": [{"kind": "down", "x": 200, "y": 150}, {"kind": "up", "x": 200, "y": 150}]}), + ); + assert!(matches!(handle(h.state_mut(), &ctx, &req), Outcome::Done(_))); + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_some()); + assert_eq!(h.state().session.journal.len(), before); + let at = h.get_by_label("Darken").rect().center(); + let (req, _) = ControlRequest::new("ui.click", json!({"x": at.x, "y": at.y})); + assert!(matches!(handle(h.state_mut(), &ctx, &req), Outcome::AfterInput)); + // The app injects these queued control events into its next frame's input. + let events = std::mem::take(&mut h.state_mut().synthetic); + for event in events { + h.event(event); + } + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_none()); + assert_eq!(h.state().session.tools.brush.mode, photocraft_doc::BlendMode::Darken); + assert_eq!(h.state().session.journal.last().unwrap(), &("tools.setBrush".into(), json!({"brush": {"mode": "Darken", "behind": false}}))); +} + +#[test] +fn shift_right_mode_picker_cancels_and_other_gestures_keep_their_routes() { + let mut h = harness(None); + let journal = h.state().session.journal.len(); + mode_right_click(&mut h, Modifiers::SHIFT); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_none()); + mode_right_click(&mut h, Modifiers::SHIFT); + let outside = h.state().last_canvas_rect.min + vec2(2.0, 2.0); + h.event(egui::Event::PointerMoved(outside)); + press(&mut h, outside, PointerButton::Secondary, true); + press(&mut h, outside, PointerButton::Secondary, false); + assert!(h.state().ui.brush_mode_menu.is_none()); + mode_right_click(&mut h, Modifiers::SHIFT); + h.state_mut().ui.tool = Tool::Pencil; + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_none()); + mode_right_click(&mut h, Modifiers::SHIFT); + assert!(h.state().ui.brush_mode_menu.is_none() && h.state().ui.brush_picker.is_some()); + h.state_mut().ui.brush_picker = None; + h.state_mut().ui.tool = Tool::Brush; + mode_right_click(&mut h, Modifiers::NONE); + assert!(h.state().ui.brush_picker.is_some() && h.state().ui.brush_mode_menu.is_none()); + assert_eq!(h.state().session.journal.len(), journal, "opening/cancelling never changes brush or pixels"); +} + +#[test] +fn escape_dismissal_consumes_a_held_secondary_release() { + for release_shift in [false, true] { + let mut h = harness(Some("erase")); + let journal = h.state().session.journal.len(); + let history = h.state().session.active().unwrap().history.past_len(); + let p = h.state().last_canvas_rect.center(); + h.event(egui::Event::ModifiersChanged(Modifiers::SHIFT)); + h.event(egui::Event::PointerMoved(p)); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::SHIFT }); + h.run_steps(1); + // A quick second press can arrive in the frame that opens the menu for the first + // click's release, leaving the secondary button held while the picker is visible. + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::SHIFT }); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::SHIFT }); + h.run_steps(1); + assert!(h.state().ui.brush_mode_menu.is_some()); + assert!(h.ctx.input(|i| i.pointer.secondary_down())); + h.key_press(egui::Key::Escape); + h.run_steps(1); + assert!(h.state().ui.brush_mode_menu.is_none()); + let mods = if release_shift { Modifiers::NONE } else { Modifiers::SHIFT }; + h.event(egui::Event::ModifiersChanged(mods)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: mods }); + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_none(), "Escape must consume the pending release"); + assert!(h.state().ui.brush_picker.is_none() && h.state().drag.is_none() && !h.state().secondary_erase); + assert_eq!(h.state().session.journal.len(), journal); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history); + mode_right_click(&mut h, Modifiers::SHIFT); + assert!(h.state().ui.brush_mode_menu.is_some(), "the next gesture must still work"); + } +} + +#[test] +fn shift_released_before_the_right_button_still_opens_the_mode_picker() { + // egui reports `secondary_clicked` on release. With Erase on, a click that lost Shift a + // frame early must open the menu, not erase a dab. + let mut h = harness(Some("erase")); + let history = h.state().session.active().unwrap().history.past_len(); + let journal = h.state().session.journal.len(); + let p = h.state().last_canvas_rect.center(); + h.event(egui::Event::ModifiersChanged(Modifiers::SHIFT)); + h.event(egui::Event::PointerMoved(p)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::SHIFT }); + h.run_steps(1); + // Shift goes up in a frame of its own while the right button is still down. + h.event(egui::Event::ModifiersChanged(Modifiers::NONE)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(2); + assert!(h.state().ui.brush_mode_menu.is_some(), "the press decided the gesture"); + assert!(h.state().drag.is_none() && !h.state().secondary_erase); + assert_eq!(h.state().session.journal.len(), journal, "no erase dab"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history); +} + +#[test] +fn left_drag_paints() { + let mut h = harness(None); + let (a, b) = drag(&mut h, PointerButton::Primary); + assert_eq!(strokes(&h).len(), 1); + assert!(alpha_at(&h, a + (b - a) * 0.5) > 0.9); +} + +#[test] +fn shift_click_connects_to_the_previous_brush_stroke() { + use egui::{Event, Modifiers}; + + let mut h = harness(None); + let (_, end) = drag(&mut h, PointerButton::Primary); + let previous = h + .state() + .session + .journal + .iter() + .rev() + .find(|(id, _)| id == "paint.stroke") + .map(|(_, p)| p["points"].as_array().unwrap().last().unwrap().clone()) + .unwrap(); + let target = end + vec2(120.0, 80.0); + h.event(Event::PointerMoved(target)); + h.run_steps(1); + for pressed in [true, false] { + h.event(Event::PointerButton { pos: target, button: PointerButton::Primary, pressed, modifiers: Modifiers::SHIFT }); + h.run_steps(2); + } + + let strokes = strokes(&h); + assert_eq!(strokes.len(), 2, "a Shift-click commits one connected stroke"); + let points = strokes[1]["points"].as_array().unwrap(); + // The previous endpoint, then the clicked one. While the button is held, smoothing that is + // still catching up repeats the clicked point with later times. + assert_eq!(points[0][0], previous[0]); + assert_eq!(points[0][1], previous[1]); + let clicked = &points[1]; + assert!(points[1..].iter().all(|p| p[0] == clicked[0] && p[1] == clicked[1]), "{points:?}"); + assert!(alpha_at(&h, end + vec2(60.0, 40.0)) > 0.9, "the segment between the strokes is painted"); +} + +#[test] +fn right_click_opens_the_brush_picker_and_never_paints() { + let mut h = harness(None); + let undo = h.state().session.active().unwrap().history.past_len(); + let c = h.state().last_canvas_rect.center(); + h.event(egui::Event::PointerMoved(c)); + h.run_steps(1); + press(&mut h, c, PointerButton::Secondary, true); + press(&mut h, c, PointerButton::Secondary, false); + assert_eq!(h.state().ui.brush_picker, Some([c.x, c.y]), "opened at the pointer"); + assert!(strokes(&h).is_empty()); + // The picker shows over the canvas at the pointer, moved up just enough to stay on screen. + h.run_steps(2); + let shown = h.ctx.memory(|m| m.area_rect(egui::Id::unique("canvas-brush-picker"))).expect("picker shown"); + let screen = h.ctx.content_rect(); + assert!((shown.min.x - c.x).abs() < 1.0 && shown.min.y <= c.y + 1.0, "{shown:?}"); + assert!(screen.contains_rect(shown.shrink(0.5)) && shown.width() > 250.0 && shown.height() > 150.0, "{shown:?}"); + // It is the real preset library, not a fixed set of round tips (#258). + let names: Vec = h.state().session.tools.presets.iter().take(3).map(|p| p.name.clone()).collect(); + { + use egui_kittest::kittest::Queryable; + for n in &names { + assert!(h.query_by_label(n).is_some(), "{n} listed"); + } + } + // A right drag doesn't paint either. + drag(&mut h, PointerButton::Secondary); + assert!(strokes(&h).is_empty(), "right-drag never paints with the brush picker preference"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), undo, "no edit"); + // Escape closes it. + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(1); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert_eq!(h.state().ui.brush_picker, None); +} + +#[test] +fn click_outside_closes_the_picker() { + let mut h = harness(None); + let c = h.state().last_canvas_rect.center(); + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(2); + let far = c - vec2(300.0, 200.0); + h.event(egui::Event::PointerMoved(far)); + press(&mut h, far, PointerButton::Primary, true); + press(&mut h, far, PointerButton::Primary, false); + assert_eq!(h.state().ui.brush_picker, None); +} + +/// #1031: the right-click picker picks brushes, not just the size. A click picks a preset and +/// keeps the picker open, a double-click picks one and closes it, and Enter applies a typed +/// size before it closes the picker. +#[test] +fn picker_click_picks_a_preset_and_double_click_closes_it() { + use egui_kittest::kittest::Queryable; + let mut h = harness(None); + let c = h.state().last_canvas_rect.center(); + h.event(egui::Event::PointerMoved(c)); + h.run_steps(1); + press(&mut h, c, PointerButton::Secondary, true); + press(&mut h, c, PointerButton::Secondary, false); + h.run_steps(2); + let names: Vec = h.state().session.tools.presets.iter().skip(1).take(2).map(|p| p.name.clone()).collect(); + let picks = |h: &Harness<'static, PhotocraftApp>, name: &str| { + h.state().session.journal.iter().filter(|(id, p)| id == "tools.setBrush" && *p == json!({ "preset": name })).count() + }; + h.get_by_label(&names[0]).click(); + h.run_steps(3); + assert_eq!(picks(&h, &names[0]), 1); + assert!(h.state().ui.brush_picker.is_some(), "a click keeps the picker open"); + let at = h.get_by_label(&names[1]).rect().center(); + h.event(egui::Event::PointerMoved(at)); + h.run_steps(1); + // Two clicks 2 frames apart; well after the first click, so not a triple click. + h.run_steps(40); + for pressed in [true, false, true, false] { + h.event(egui::Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.run_steps(2); + assert_eq!(picks(&h, &names[1]), 1, "picked once: the second click only closes"); + assert_eq!(h.state().ui.brush_picker, None, "a double-click closes the picker"); + assert!(strokes(&h).is_empty(), "nothing painted under the picker"); + // Enter applies the typed size, then closes the picker. + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(2); + h.get_all_by_role(egui::accesskit::Role::SpinButton).next().expect("the Size field").click(); + h.run_steps(1); + h.event(egui::Event::Text("30*2".into())); + h.run_steps(1); + h.key_press(egui::Key::Enter); + h.run_steps(2); + assert_eq!((h.state().session.tools.brush.size, h.state().ui.brush_picker), (60.0, None)); +} + +/// A sampled tip has no hardness: the picker shows only its size (Photoshop). +#[test] +fn picker_shows_hardness_only_for_round_tips() { + use egui_kittest::kittest::Queryable; + let mut h = harness(None); + let c = h.state().last_canvas_rect.center(); + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(2); + assert!(h.query_all_by_label("Hardness").next().is_some()); + let sampled = h.state().session.tools.presets.iter().find(|p| p.brush.tip != photocraft_engine::paint::TipShape::Round).map(|p| p.name.clone()); + h.state_mut().run("tools.setBrush", json!({ "preset": sampled.expect("a sampled preset") })).unwrap(); + h.run_steps(2); + assert!(h.query_all_by_label("Hardness").next().is_none()); + assert!(h.state().ui.brush_picker.is_some()); +} + +/// The gear menu's New Brush Preset… opens the New Brush Preset dialog (#4490), which saves +/// the preset under the typed name on OK. +#[test] +fn picker_gear_menu_saves_and_names_a_new_preset() { + use egui_kittest::kittest::Queryable; + let mut h = harness(None); + let c = h.state().last_canvas_rect.center(); + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(2); + h.get_by_label("Brush Preset Options").click(); + h.run_steps(2); + let before = h.state().session.tools.presets.len(); + h.get_by_label("New Brush Preset…").click(); + h.run_steps(3); + let dialog = h.state().ui.dialogs.iter().find(|d| crate::brush_preset_dialog::owns(&d.fields)).map(|d| d.id).expect("the dialog asks for a name"); + assert_eq!(h.state().session.tools.presets.len(), before, "nothing is saved before OK"); + assert!(h.state().ui.brush_picker.is_none(), "the dialog takes the picker's place"); + // This harness draws the canvas only: OK as automation does (the dialog's own tests type). + h.state_mut().ui.dialog_mut(dialog).unwrap().fields.insert("name".into(), json!("Inky")); + crate::dialogs::confirm(h.state_mut(), dialog).unwrap(); + h.run_steps(2); + assert!(h.state().session.tools.presets.iter().any(|p| p.name == "Inky"), "saved under the typed name"); + assert_eq!(h.state().session.tools.current_preset.as_deref(), Some("Inky")); + h.state_mut().ui.brush_picker = Some([c.x, c.y]); + h.run_steps(2); + // The gear's boxes pick the card parts; the last one on can't be turned off. + h.get_by_label("Brush Preset Options").click(); + h.run_steps(2); + h.get_by_label("Brush Tip").click(); + h.run_steps(2); + assert!(!h.state().ui.brush_picker_list.show_tip); + h.get_by_label("Brush Stroke").click(); + h.run_steps(2); + h.get_by_label("Brush Name").click(); + h.run_steps(2); + assert!(h.state().ui.brush_picker_list.show_name, "the last part on stays on"); + assert!(!h.state().ui.brush_picker_list.show_stroke && !h.state().ui.brush_picker_list.show_tip); + assert!(h.state().ui.brush_picker.is_some()); + // A rename left open goes with the picker. + h.state_mut().ui.brush_picker_list.renaming = + Some(crate::brush_panel::Renaming { group: false, name: "Inky".into(), folder: Vec::new(), text: String::new() }); + h.run_steps(2); + let far = c - vec2(300.0, 200.0); + h.event(egui::Event::PointerMoved(far)); + press(&mut h, far, PointerButton::Primary, true); + press(&mut h, far, PointerButton::Primary, false); + assert!(h.state().ui.brush_picker.is_none() && h.state().ui.brush_picker_list.renaming.is_none()); +} + +#[test] +fn right_drag_erases_with_the_erase_preference() { + let mut h = harness(Some("erase")); + let (a, b) = drag(&mut h, PointerButton::Primary); + let mid = a + (b - a) * 0.5; + assert!(alpha_at(&h, mid) > 0.9); + let undo = h.state().session.active().unwrap().history.past_len(); + drag(&mut h, PointerButton::Secondary); + let s = strokes(&h); + assert_eq!(s.len(), 2); + assert_eq!(s[1]["erase"], json!(true), "the right stroke erases"); + assert_eq!(alpha_at(&h, mid), 0.0, "erased to transparency"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), undo + 1, "one undo step"); + assert_eq!(h.state().ui.brush_picker, None, "no picker in erase mode"); + // The next left stroke paints again. + drag(&mut h, PointerButton::Primary); + assert_eq!(strokes(&h)[2]["erase"], json!(false)); + assert!(alpha_at(&h, mid) > 0.9); +} + +#[test] +fn right_stroke_erases_with_pen_pressure_through_the_control_channel_path() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80})).unwrap(); + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "erase"})).unwrap(); + app.ui.tool = Tool::Brush; + let m = Modifiers::NONE; + assert!(pointer_secondary(&mut app, &egui::Context::default(), true, egui::Modifiers::NONE, [0.0, 0.0])); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 40.0, pressure: 0.4 }, m); + assert!(!app.secondary_erase, "armed for one stroke only"); + tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 40.0, pressure: 0.8 }, m); + tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 40.0 }, m); + let p = app.session.journal.iter().rev().find(|(id, _)| id == "paint.stroke").map(|(_, p)| p.clone()).unwrap(); + assert_eq!(p["erase"], json!(true)); + assert_eq!(p["points"][0], json!([10.0, 40.0, 0.4f32 as f64]), "pressure reaches the erasing stroke"); + // On the Background the erasing stroke paints the background colour (#76). + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers[0].surface().unwrap().rgba(50, 40), [1.0, 1.0, 1.0, 1.0]); + // With the default preference the right button opens the picker and nothing paints. + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "brushPicker"})).unwrap(); + assert!(!pointer_secondary(&mut app, &egui::Context::default(), true, egui::Modifiers::NONE, [0.0, 0.0])); + assert!(app.ui.brush_picker.is_some() && !app.secondary_erase); +} + +#[test] +fn shift_alt_right_through_the_control_channel_respects_the_erase_preference() { + // As on the canvas, with the right-erase preference the erase stroke wins over the HUD + // for Brush/Eraser; without it, Shift+Alt+right opens the HUD (#6098). + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80})).unwrap(); + app.ui.tool = Tool::Brush; + let shift_alt = egui::Modifiers { shift: true, alt: true, ..Default::default() }; + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "erase"})).unwrap(); + assert!(pointer_secondary(&mut app, &egui::Context::default(), true, shift_alt, [10.0, 10.0]), "the events reach the tool"); + assert!(app.ui.color_hud.is_none(), "no HUD with the erase preference"); + assert!(app.secondary_erase, "the down arms the erasing stroke"); + pointer_secondary(&mut app, &egui::Context::default(), false, shift_alt, [10.0, 10.0]); + // With the default preference the same gesture opens the HUD instead. + app.run("prefs.set", json!({"path": "tools.rightClickWithPaintingTools", "value": "brushPicker"})).unwrap(); + assert!(!pointer_secondary(&mut app, &egui::Context::default(), true, shift_alt, [10.0, 10.0])); + assert!(app.ui.color_hud.is_some_and(|h| !h.hold), "the control-channel HUD opened"); +} + +#[test] +fn a_control_channel_up_closes_the_hud_without_repeating_its_modifiers() { + // Each control-channel request carries its own modifiers, so an Up sent after the keys + // are released arrives without Shift+Alt: it must still close the HUD it opened, or the + // HUD stays up and keeps picking from the hover (#6098 review). + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80})).unwrap(); + app.ui.tool = Tool::Brush; + let shift_alt = egui::Modifiers { shift: true, alt: true, ..Default::default() }; + assert!(!pointer_secondary(&mut app, &egui::Context::default(), true, shift_alt, [10.0, 10.0])); + assert!(app.ui.color_hud.is_some_and(|h| !h.hold), "the control-channel HUD opened"); + assert!(!pointer_secondary(&mut app, &egui::Context::default(), false, egui::Modifiers::NONE, [10.0, 10.0])); + assert!(app.ui.color_hud.is_none(), "the Up closes it whatever the modifiers say"); + // A real-mouse HUD belongs to the canvas gesture and waits for its own button release. + crate::color_hud::open(&mut app, [10.0, 10.0], true); + pointer_secondary(&mut app, &egui::Context::default(), false, egui::Modifiers::NONE, [10.0, 10.0]); + assert!(app.ui.color_hud.is_some_and(|h| h.hold), "a held HUD is left to the canvas"); +} + +#[test] +fn the_brush_is_a_per_tool_option_that_presets_keep() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + let amount = |app: &PhotocraftApp| app.session.tools.brush.smoothing.amount; + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + assert_eq!(amount(&app), 0.1, "Photoshop's default"); + // Picking a preset (whose own smoothing is 0) keeps the tool's smoothing. + let preset = app.session.tools.presets.iter().find(|p| p.brush.smoothing.amount == 0.0).map(|p| p.name.clone()).unwrap(); + app.run("tools.setBrush", json!({"preset": preset})).unwrap(); + assert_eq!(amount(&app), 0.1); + // A user value (the options-bar field) survives a preset too, mode included. + app.session.tools.brush.smoothing.amount = 0.42; + app.session.tools.brush.smoothing.pulled_string = true; + app.run("tools.setBrush", json!({"preset": preset})).unwrap(); + assert_eq!((amount(&app), app.session.tools.brush.smoothing.pulled_string), (0.42, true)); + // The Eraser has its own (10 % at first); switching back restores the Brush's. + app.ui.tool = Tool::Eraser; + sync_tool_brush(&mut app); + assert_eq!(amount(&app), 0.1); + app.session.tools.brush.smoothing.amount = 0.0; + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + assert_eq!((amount(&app), app.session.tools.brush.smoothing.pulled_string), (0.42, true)); + // Other tools leave it alone; the Eraser kept its 0 %. + app.ui.tool = Tool::Move; + sync_tool_brush(&mut app); + assert_eq!(amount(&app), 0.42); + app.ui.tool = Tool::Eraser; + // A stroke picks up the tool's brush even without a frame in between. + tool_event(&mut app, ToolEvent::Down { x: 5.0, y: 5.0, pressure: 1.0 }, Modifiers::NONE); + assert_eq!(amount(&app), 0.0); + tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 5.0 }, Modifiers::NONE); +} + +/// #218: Photoshop keeps a brush per painting tool, so the size, hardness, mode, opacity and +/// dynamics of one tool never leak into another's, and each tool gets its own back. +#[test] +fn each_tool_keeps_its_own_brush() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + let brush = |app: &PhotocraftApp| app.session.tools.brush.clone(); + let size = |b: &BrushSettings| b.size; + let hardness = |b: &BrushSettings| b.hardness; + let opacity = |b: &BrushSettings| b.opacity; + let mode = |b: &BrushSettings| b.mode; + + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + app.run("tools.setBrush", json!({"brush": {"size": 40, "hardness": 0.2, "opacity": 0.7, "mode": "Multiply"}})).unwrap(); + assert_eq!(size(&brush(&app)), 40.0); + + // A tool seen for the first time starts from the brush the old tool left (Photoshop's + // 10 % smoothing), so nothing is lost by switching. + app.ui.tool = Tool::Eraser; + sync_tool_brush(&mut app); + assert_eq!(size(&brush(&app)), 40.0, "a new tool inherits the current brush"); + + // The Eraser edits its own size; the Brush keeps 40 px. + app.run("tools.setBrush", json!({"brush": {"size": 8, "hardness": 1.0}})).unwrap(); + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + assert_eq!(size(&brush(&app)), 40.0, "the Brush keeps its own size"); + assert_eq!(hardness(&brush(&app)), 0.2, "and its hardness"); + app.ui.tool = Tool::Eraser; + sync_tool_brush(&mut app); + assert_eq!(size(&brush(&app)), 8.0, "the Eraser keeps its own size"); + assert_eq!(hardness(&brush(&app)), 1.0, "and its hardness"); + + // Mode, opacity, flow and the dynamics sections are per tool too. + app.ui.tool = Tool::CloneStamp; + sync_tool_brush(&mut app); + assert_eq!(mode(&brush(&app)), photocraft_color::BlendMode::Normal, "a new tool starts in Normal, not the Brush's Multiply"); + app.run("tools.setBrush", json!({"brush": {"mode": "Screen", "opacity": 0.3}})).unwrap(); + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + assert_eq!((mode(&brush(&app)), opacity(&brush(&app))), (photocraft_color::BlendMode::Multiply, 0.7), "the Brush's own mode and opacity"); + app.ui.tool = Tool::CloneStamp; + sync_tool_brush(&mut app); + assert_eq!((mode(&brush(&app)), opacity(&brush(&app))), (photocraft_color::BlendMode::Screen, 0.3), "the Clone Stamp keeps its own"); + + // The colours, the erase flag and the seed belong to the session, so a swap keeps what is + // live now instead of restoring another tool's copy (a picked colour reaches every tool). + app.run("tools.setBrush", json!({"brush": {"color": [0.0, 1.0, 0.0, 1.0], "background": [1.0, 0.0, 1.0, 1.0], "erase": true, "seed": 42}})).unwrap(); + app.ui.tool = Tool::Brush; + sync_tool_brush(&mut app); + let b = brush(&app); + assert_eq!( + (b.color, b.background, b.erase, b.seed), + ([0.0, 1.0, 0.0, 1.0], [1.0, 0.0, 1.0, 1.0], true, 42), + "the session's own brush fields survive the swap" + ); + + // Tools with no brush of their own (the Move) leave the brush alone. + let before = brush(&app); + app.ui.tool = Tool::Move; + sync_tool_brush(&mut app); + assert_eq!(brush(&app), before); +} + +#[test] +fn eraser_tool_on_the_background_paints_the_background_colour() { + // #76: the Eraser on the Background (locked transparency) paints the background colour. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 120, "height": 80})).unwrap(); + app.run("tools.setColors", json!({"background": "#ff0000"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 12, "hardness": 1.0}})).unwrap(); + app.ui.tool = Tool::Eraser; + let m = Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 40.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Move { x: 100.0, y: 40.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 40.0 }, m); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers[0].name, "Background"); + assert_eq!(st.doc.layers[0].surface().unwrap().rgba(50, 40), [1.0, 0.0, 0.0, 1.0]); + assert_eq!(st.history.undo_label(), Some("Eraser")); + assert!(!app.ui.status_error, "{}", app.ui.status); +} + +/// Assert the one committed stroke contains a point at each queued screen position. +fn assert_committed_points(h: &Harness<'static, PhotocraftApp>, queued: &[Pos2]) { + let s = strokes(h); + assert_eq!(s.len(), 1); + let points = s[0]["points"].as_array().unwrap(); + let app = h.state(); + let v = app.ui.views[0].clone(); + let r = app.last_canvas_rect; + for p in queued { + let d = (*p - r.center()) / v.zoom; + let want = [d.x as f64 + v.center[0] as f64, d.y as f64 + v.center[1] as f64]; + assert!( + points.iter().any(|q| { + let q = q.as_array().unwrap(); + (q[0].as_f64().unwrap() - want[0]).abs() < 1.0 && (q[1].as_f64().unwrap() - want[1]).abs() < 1.0 + }), + "queued move {want:?} missing from {points:?}" + ); + } +} + +#[test] +fn one_frame_feeds_every_pointer_move_not_just_the_latest() { + // Fast strokes used to lose the moves between two frames: the canvas read only + // `interact_pointer_pos()` once per frame, so the stroke was a coarse polyline. Every + // `PointerMoved` egui-winit delivered in one frame must now reach the stroke. + let mut h = harness(None); + let c = h.state().last_canvas_rect.center(); + let a = c - vec2(90.0, 0.0); + h.event(egui::Event::PointerMoved(a)); + h.run_steps(1); + press(&mut h, a, PointerButton::Primary, true); + // Cross egui's drag threshold on its own frame. + h.event(egui::Event::PointerMoved(a + vec2(20.0, 0.0))); + h.run_steps(1); + // Several moves in ONE frame: `Harness::event` would make one frame per event, so push + // them straight into the next frame's input. All of them must reach the committed stroke. + let queued: Vec = (1..=6).map(|i| a + vec2(20.0 + i as f32 * 8.0, 0.0)).collect(); + for p in &queued { + h.input_mut().events.push(egui::Event::PointerMoved(*p)); + } + h.run_steps(1); + let end = *queued.last().unwrap(); + press(&mut h, end, PointerButton::Primary, false); + assert_committed_points(&h, &queued); +} diff --git a/crates/ui-egui/src/patch_preview.rs b/crates/ui-egui/src/canvas/patch_preview.rs similarity index 100% rename from crates/ui-egui/src/patch_preview.rs rename to crates/ui-egui/src/canvas/patch_preview.rs diff --git a/crates/ui-egui/src/pattern_preview.rs b/crates/ui-egui/src/canvas/pattern_preview.rs similarity index 100% rename from crates/ui-egui/src/pattern_preview.rs rename to crates/ui-egui/src/canvas/pattern_preview.rs diff --git a/crates/ui-egui/src/canvas/pixel_alignment_tests.rs b/crates/ui-egui/src/canvas/pixel_alignment_tests.rs new file mode 100644 index 0000000000..14a4079a7b --- /dev/null +++ b/crates/ui-egui/src/canvas/pixel_alignment_tests.rs @@ -0,0 +1,160 @@ +use super::*; + +fn xform(ppp: f32, physical_size: [f32; 2]) -> ViewXform { + ViewXform { + rect: Rect::from_min_size(pos2(40.0 / ppp, 100.0 / ppp), vec2(physical_size[0] / ppp, physical_size[1] / ppp)), + zoom: 1.0 / ppp, + center: [1500.0, 1000.0], + flip: false, + rotation: 0.0, + aspect: 1.0, + } +} + +fn assert_pixel_edges(xf: ViewXform, ppp: f32) { + for [x, y] in [[0.0, 0.0], [1.0, 1.0], [17.0, 29.0], [1500.0, 1000.0]] { + let p = xf.to_screen(x, y) * ppp; + assert!((p.x - p.x.round()).abs() < 0.001, "x edge: {p:?}, ppp {ppp}"); + assert!((p.y - p.y.round()).abs() < 0.001, "y edge: {p:?}, ppp {ppp}"); + } +} + +#[test] +fn actual_pixels_align_texel_edges_in_odd_viewports_at_every_display_scale() { + for ppp in [1.0, 1.25, 1.5, 2.0] { + for size in [[523.0, 470.0], [524.0, 471.0], [523.0, 471.0]] { + let before = xform(ppp, size); + let aligned = before.pixel_aligned(ppp); + assert_pixel_edges(aligned, ppp); + let delta = (aligned.to_screen(0.0, 0.0) - before.to_screen(0.0, 0.0)) * ppp; + assert!(delta.x.abs() <= 0.501 && delta.y.abs() <= 0.501); + let again = aligned.pixel_aligned(ppp); + assert!((again.to_screen(0.0, 0.0) - aligned.to_screen(0.0, 0.0)).length() < 0.001); + let back = aligned.to_doc(aligned.to_screen(123.25, 45.75)); + assert!((back[0] - 123.25).abs() < 0.001 && (back[1] - 45.75).abs() < 0.001); + } + } +} + +#[test] +fn integer_magnification_aligns_flipped_and_aspect_corrected_views() { + for flip in [false, true] { + let before = ViewXform { + rect: Rect::from_min_size(pos2(-20.25, 30.25), vec2(523.0, 471.0)), + zoom: 2.0, + center: [1500.125, 999.75], + flip, + aspect: 1.5, + ..xform(1.0, [523.0, 471.0]) + }; + assert_pixel_edges(before.pixel_aligned(1.0), 1.0); + } + let even = xform(1.0, [524.0, 470.0]); + assert_eq!(even.pixel_aligned(1.0).center, even.center); +} + +#[test] +fn fractional_zoom_rotation_and_invalid_scales_preserve_the_camera() { + let original = xform(1.0, [523.0, 471.0]); + for zoom in [0.25, 0.5, 0.75, 1.25, 1.5, 2.5, 0.0, -1.0, f32::NAN, f32::INFINITY] { + let xf = ViewXform { zoom, ..original }; + assert_eq!(xf.pixel_aligned(1.0).center, xf.center); + } + for rotation in [0.2, 30.0, 90.0, f32::NAN] { + let xf = ViewXform { rotation, ..original }; + assert_eq!(xf.pixel_aligned(1.0).center, xf.center); + } + for ppp in [0.0, -1.0, f32::NAN, f32::INFINITY] { + assert_eq!(original.pixel_aligned(ppp).center, original.center); + } + let aspect = ViewXform { aspect: 1.2, ..original }; + assert_eq!(aspect.pixel_aligned(1.0).center, aspect.center); +} + +#[test] +fn active_transform_uses_the_same_alignment_without_changing_saved_pan() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", serde_json::json!({"width": 3000, "height": 2000})).unwrap(); + app.sync_views(); + for ppp in [1.0, 1.25, 1.5, 2.0] { + app.ppp = ppp; + app.last_canvas_rect = xform(ppp, [523.0, 471.0]).rect; + app.ui.views[0].center = [1500.0, 1000.0]; + app.ui.views[0].zoom = 1.0; + let before = app.ui.views[0].center; + let active = ViewXform::active(&app).unwrap(); + assert_pixel_edges(active, ppp); + assert_eq!(app.ui.views[0].center, before); + } +} + +#[test] +fn cpu_canvas_paints_its_image_on_physical_pixel_edges() { + for ppp in [1.0, 1.25, 2.0] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", serde_json::json!({"width": 64, "height": 64, "background": "white"})).unwrap(); + app.sync_views(); + app.ppp = ppp; + app.ui.views[0].zoom = 1.0; + app.ui.views[0].center = [32.0, 32.0]; + app.ui.views[0].fit_pending = false; + app.ui.views[0].doc_size = [64, 64]; + let ctx = egui::Context::default(); + ctx.set_pixels_per_point(ppp); + let tex = ensure_texture(&mut app, &ctx, 0, None).unwrap().0; + let view = app.ui.views[0].clone(); + let saved_center = view.center; + let mut out = ctx.run_ui(egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(900.0, 600.0))), ..Default::default() }, |ui| { + canvas_view(&mut app, ui, 0, xform(ppp, [523.0, 471.0]).rect, view.clone(), false); + }); + out.textures_delta.clear(); + let mesh = out + .shapes + .iter() + .find_map(|s| match &s.shape { + egui::Shape::Mesh(mesh) if mesh.texture_id == tex => Some(mesh), + _ => None, + }) + .expect("CPU document image quad"); + assert_eq!(mesh.vertices.len(), 4); + for vertex in &mesh.vertices { + let p = vertex.pos * ppp; + assert!((p.x - p.x.round()).abs() < 0.001 && (p.y - p.y.round()).abs() < 0.001, "ppp {ppp}: {p:?}"); + } + assert_eq!(app.ui.views[0].center, saved_center); + } +} + +#[test] +fn crop_image_movement_keeps_its_continuous_camera_mapping() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.ui.tool = Tool::Crop; + let xf = xform(1.0, [523.0, 471.0]); + assert_eq!(xf.pixel_aligned_for(&app, 1.0).center, xf.center); + app.ui.tool_options.crop_shield.classic_mode = true; + assert_pixel_edges(xf.pixel_aligned_for(&app, 1.0), 1.0); +} + +#[test] +fn animated_active_transform_aligns_the_display_camera_and_preserves_target_pan() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", serde_json::json!({"width": 3000, "height": 2000})).unwrap(); + app.run("prefs.set", serde_json::json!({"values": {"general.animatedZoom": true}})).unwrap(); + app.sync_views(); + for ppp in [1.0, 1.25, 1.5, 2.0] { + app.ppp = ppp; + app.last_canvas_rect = xform(ppp, [523.0, 471.0]).rect; + let v = &mut app.ui.views[0]; + v.zoom = 3.0; + v.center = [1500.0, 1000.0]; + v.zoom_animating = true; + v.display_camera = Some((1.0, [1490.25, 990.75])); + let active = ViewXform::active(&app).unwrap(); + assert_eq!(active.zoom, 1.0 / ppp); + assert_pixel_edges(active, ppp); + assert!((active.center[0] - 1490.25).abs() <= 0.501); + assert!((active.center[1] - 990.75).abs() <= 0.501); + assert_eq!(app.ui.views[0].center, [1500.0, 1000.0]); + assert_eq!(app.ui.views[0].display_camera, Some((1.0, [1490.25, 990.75]))); + } +} diff --git a/crates/ui-egui/src/pixel_grid.rs b/crates/ui-egui/src/canvas/pixel_grid.rs similarity index 99% rename from crates/ui-egui/src/pixel_grid.rs rename to crates/ui-egui/src/canvas/pixel_grid.rs index 4ec68ccae4..e7a565ae2a 100644 --- a/crates/ui-egui/src/pixel_grid.rs +++ b/crates/ui-egui/src/canvas/pixel_grid.rs @@ -176,9 +176,10 @@ pub fn paint(app: &mut PhotocraftApp, painter: &Painter, xf: &ViewXform, idx: us cache.grid = Some(GridLines::build(&doc, revision, preview_key, cache_region(visible, doc.bounds()))); } let Some(grid) = &cache.grid else { return }; + let width = 1.0 / painter.ctx().pixels_per_point(); for s in &grid.segs { let (a, b) = (xf.to_screen(s.from[0] as f32, s.from[1] as f32), xf.to_screen(s.to[0] as f32, s.to[1] as f32)); - painter.line_segment([a, b], Stroke::new(1.0, s.color)); + painter.line_segment([a, b], Stroke::new(width, s.color)); } } diff --git a/crates/ui-egui/src/quick_pick.rs b/crates/ui-egui/src/canvas/quick_pick.rs similarity index 100% rename from crates/ui-egui/src/quick_pick.rs rename to crates/ui-egui/src/canvas/quick_pick.rs diff --git a/crates/ui-egui/src/canvas/rotate_view.rs b/crates/ui-egui/src/canvas/rotate_view.rs new file mode 100644 index 0000000000..8b6706327f --- /dev/null +++ b/crates/ui-egui/src/canvas/rotate_view.rs @@ -0,0 +1,273 @@ +//! Rotate View tool: turn the canvas camera around its centre without rewriting pixels. +//! +//! Drag around the view centre: the image follows the pointer (clockwise increases the angle, or +//! decreases it in a flipped view). Shift snaps to 15°. Reset View, +//! Escape, a double-click, or the on-canvas compass put the angle back to 0. History is not +//! recorded. Trackpad rotate is gated by Preferences › Enhanced Controls › Rotate View with +//! Trackpad; a two-finger twist on a touch screen also needs Enable Gestures (#204). + +use egui::{Align2, Pos2, Rect, Stroke, pos2, vec2}; +use serde_json::{Value, json}; + +use crate::PhotocraftApp; +use crate::canvas::ViewXform; +use crate::paint_mouse::Buttons; +use crate::state::View; +use crate::theme::Tokens; + +/// Snap increment, in degrees, while Shift is held. +pub const SNAP_DEG: f32 = 15.0; + +/// A Rotate View drag in progress. +#[derive(Clone, Copy, Debug, PartialEq)] +struct RotateDrag { + /// Screen point of the view centre when the drag began. + pivot: Pos2, + /// Pointer angle, in degrees, at the press (atan2, Y-down: clockwise is positive). + start_pointer: f32, + /// View rotation, in degrees, at the press. + start_rotation: f32, +} + +fn id() -> egui::Id { + egui::Id::unique("pc-rotate-view-drag") +} + +fn compass_id() -> egui::Id { + egui::Id::unique("pc-rotate-view-compass") +} + +/// Wrap `a` degrees into (−180, 180]. Non-finite values become 0 so a hostile angle never +/// propagates into the camera. +pub fn wrap_deg(a: f32) -> f32 { + if !a.is_finite() { + return 0.0; + } + let mut x = f64::from(a) % 360.0; + if x > 180.0 { + x -= 360.0; + } else if x <= -180.0 { + x += 360.0; + } + x as f32 +} + +/// Nearest multiple of `step` degrees, wrapped. A non-positive or non-finite step is ignored. +pub fn snap_deg(a: f32, step: f32) -> f32 { + if !(step.is_finite() && step > 0.0) { + return wrap_deg(a); + } + wrap_deg((a / step).round() * step) +} + +/// Turn a pointer or trackpad angle change (screen degrees, clockwise positive) into a change of +/// `View::rotation`. [`ViewXform::to_screen`] mirrors x after rotating, so in a flipped view a +/// larger angle turns the image the other way on screen; negating keeps it under the pointer. +pub fn screen_delta(delta: f32, flip: bool) -> f32 { + let d = wrap_deg(delta); + if flip { wrap_deg(-d) } else { d } +} + +/// Pointer angle in degrees around `pivot` (Y-down, so clockwise is positive). +pub fn pointer_deg(p: Pos2, pivot: Pos2) -> f32 { + let d = p - pivot; + if !(d.x.is_finite() && d.y.is_finite()) || d.length_sq() < 1e-8 { + return 0.0; + } + d.y.atan2(d.x).to_degrees() +} + +fn finite_number(p: &Value, key: &str) -> Result, String> { + match p.get(key) { + None => Ok(None), + Some(v) => v + .as_f64() + .filter(|n| n.is_finite() && n.abs() <= f64::from(f32::MAX)) + .map(|n| Some(n as f32)) + .ok_or_else(|| format!("{key} must be a finite number representable as f32")), + } +} + +/// Apply `view.rotateView` params to `current`. Empty params, a non-object, or a missing +/// `angle`/`delta`/`reset` are errors (never a silent no-op). +pub fn apply_params(current: f32, p: &Value) -> Result { + let obj = p.as_object().ok_or("params must be an object")?; + for k in obj.keys() { + if !matches!(k.as_str(), "angle" | "delta" | "reset") { + return Err(format!("unknown field `{k}`")); + } + } + let reset = match obj.get("reset") { + None => false, + Some(v) => v.as_bool().ok_or_else(|| "reset must be a boolean".to_string())?, + }; + let angle = finite_number(p, "angle")?; + let delta = finite_number(p, "delta")?; + if !reset && angle.is_none() && delta.is_none() { + return Err("expected angle, delta, or reset".into()); + } + if reset { + return Ok(0.0); + } + let mut r = angle.unwrap_or(current); + if let Some(d) = delta { + r += d; + if !r.is_finite() { + return Err("delta overflowed the angle".into()); + } + } + Ok(wrap_deg(r)) +} + +/// Trackpad rotate: `delta_rad` is egui's rotation delta (radians, same sign as [`wrap_deg`]). +/// Returns the new angle when the preference is on and the delta is a finite non-zero value. +pub fn apply_trackpad(current: f32, enabled: bool, delta_rad: f32) -> Option { + if !enabled || !delta_rad.is_finite() || delta_rad == 0.0 { + return None; + } + let deg = delta_rad.to_degrees(); + if !deg.is_finite() { + return None; + } + let next = current + deg; + if !next.is_finite() { + return None; + } + Some(wrap_deg(next)) +} + +/// This frame's trackpad rotation in degrees, if the preference is on and egui reported a delta. +/// The trackpad's own `Rotate` events always count; a touch screen's two-finger twist (egui's +/// multi-touch rotation) only while `touch_gestures` (Enhanced Controls › Enable Gestures) is on. +pub fn trackpad_delta(ctx: &egui::Context, enabled: bool, touch_gestures: bool) -> Option { + if !enabled { + return None; + } + let rad = ctx.input(|i| { + let from_events: f32 = i + .events + .iter() + .filter_map(|e| match e { + egui::Event::Rotate(r) if r.is_finite() => Some(*r), + _ => None, + }) + .sum(); + if from_events != 0.0 { + from_events + } else if touch_gestures { + i.rotation_delta() + } else { + 0.0 + } + }); + apply_trackpad(0.0, true, rad).filter(|_| rad != 0.0) +} + +/// `view.rotateView`: set or add degrees on the active document's camera. +pub fn command(app: &mut PhotocraftApp, p: &Value) -> Result { + let i = app.session.active_index().ok_or("no document open")?; + let current = app.ui.views.get(i).map(|v| v.rotation).ok_or("no document open")?; + let rotation = apply_params(current, p)?; + let v = app.ui.views.get_mut(i).ok_or("no document open")?; + v.rotation = rotation; + Ok(json!({"rotation": v.rotation})) +} + +/// `view.resetView`: angle 0, zoom and centre unchanged. +pub fn reset(app: &mut PhotocraftApp) -> Result { + let i = app.session.active_index().ok_or("no document open")?; + let v = app.ui.views.get_mut(i).ok_or("no document open")?; + v.rotation = 0.0; + Ok(json!({"rotation": 0.0, "zoom": v.zoom, "center": v.center})) +} + +/// Route canvas buttons for Rotate View. Returns true when the drag is owned (the canvas then +/// ignores the tool underneath). +pub fn drag(ctx: &egui::Context, view: &mut View, xf: &ViewXform, b: &Buttons, pointer: Option) -> bool { + if b.started { + let pivot = xf.rect.center(); + let origin = ctx.input(|i| i.pointer.press_origin()).filter(|p| xf.rect.contains(*p)).or(pointer); + if let Some(p) = origin { + ctx.data_mut(|d| { + d.insert_temp(id(), RotateDrag { pivot, start_pointer: pointer_deg(p, pivot), start_rotation: view.rotation }); + }); + } + } + let Some(z) = ctx.data(|d| d.get_temp::(id())) else { + return b.started || b.dragged || b.stopped; + }; + if let Some(p) = pointer.filter(|_| b.dragged || b.stopped) { + let delta = screen_delta(pointer_deg(p, z.pivot) - z.start_pointer, xf.flip); + let mut angle = z.start_rotation + delta; + if !angle.is_finite() { + angle = z.start_rotation; + } + if ctx.input(|i| i.modifiers.shift) { + angle = snap_deg(angle, SNAP_DEG); + } + view.rotation = wrap_deg(angle); + } + if b.stopped { + ctx.data_mut(|d| d.remove::(id())); + } + true +} + +/// Compass in the top-right of the canvas (north = document up). Hidden at 0°. +pub fn compass_rect(canvas: Rect) -> Rect { + const S: f32 = 36.0; + const M: f32 = 10.0; + Rect::from_min_size(pos2(canvas.right() - M - S, canvas.top() + M), vec2(S, S)) +} + +/// True when the compass is showing (angle is not ~0). +pub fn compass_visible(rotation: f32) -> bool { + rotation.is_finite() && rotation.abs() > 0.05 +} + +/// Draw the compass and handle a click / Escape / double-click reset. Returns true when the +/// pointer over the compass should not start a rotate drag. +pub fn overlay(app: &PhotocraftApp, ctx: &egui::Context, painter: &egui::Painter, xf: &ViewXform, view: &mut View, response: &egui::Response) -> bool { + if app.ui.tool == crate::state::Tool::RotateView && ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) && view.rotation != 0.0 { + view.rotation = 0.0; + ctx.data_mut(|d| d.remove::(id())); + } + if app.ui.tool == crate::state::Tool::RotateView && (response.double_clicked() || response.triple_clicked()) { + view.rotation = 0.0; + ctx.data_mut(|d| d.remove::(id())); + } + if !compass_visible(view.rotation) { + ctx.data_mut(|d| d.remove::(compass_id())); + return false; + } + let r = compass_rect(xf.rect); + ctx.data_mut(|d| d.insert_temp(compass_id(), r)); + let t = Tokens::get(ctx); + let hover = response.hover_pos().is_some_and(|p| r.contains(p)); + painter.circle_filled(r.center(), r.width() * 0.5, if hover { t.hover } else { t.chrome }); + painter.circle_stroke(r.center(), r.width() * 0.5 - 0.5, Stroke::new(1.0, t.separator)); + // Document up: (0, −1) in document space, rotated then flipped like [`ViewXform::to_screen`]. + let rad = view.rotation.to_radians(); + let (c, s) = if view.rotation.abs() < 1e-6 { (1.0, 0.0) } else { (rad.cos(), rad.sin()) }; + let (rx, ry) = (s, -c); + let sx = if xf.flip { -1.0 } else { 1.0 }; + let needle = vec2(rx * sx, ry) * (r.width() * 0.32); + let tip = r.center() + needle; + painter.line_segment([r.center(), tip], Stroke::new(2.0, t.accent)); + painter.circle_filled(tip, 2.5, t.accent); + painter.text(r.center() + vec2(0.0, r.width() * 0.18), Align2::CENTER_CENTER, "N", egui::FontId::proportional(9.0), t.text_faint); + if response.clicked() && response.interact_pointer_pos().is_some_and(|p| r.contains(p)) { + view.rotation = 0.0; + ctx.data_mut(|d| d.remove::(id())); + return true; + } + hover +} + +/// Cursor for the Rotate View tool. +pub fn cursor(dragged: bool) -> egui::CursorIcon { + if dragged { egui::CursorIcon::Grabbing } else { egui::CursorIcon::Grab } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/rotate_view/tests.rs b/crates/ui-egui/src/canvas/rotate_view/tests.rs new file mode 100644 index 0000000000..2705c28ac9 --- /dev/null +++ b/crates/ui-egui/src/canvas/rotate_view/tests.rs @@ -0,0 +1,369 @@ +use super::*; +use egui::{Event, PointerButton, pos2}; +use egui_kittest::Harness; +use serde_json::json; + +#[test] +fn wrap_deg_folds_huge_and_hostile_angles() { + assert_eq!(wrap_deg(0.0), 0.0); + assert_eq!(wrap_deg(180.0), 180.0); + assert_eq!(wrap_deg(-180.0), 180.0); + assert!((wrap_deg(190.0) + 170.0).abs() < 1e-4); + assert!((wrap_deg(-190.0) - 170.0).abs() < 1e-4); + assert!((wrap_deg(360.0)).abs() < 1e-4); + assert!((wrap_deg(1.0e10)).abs() <= 180.0); + assert_eq!(wrap_deg(f32::NAN), 0.0); + assert_eq!(wrap_deg(f32::INFINITY), 0.0); + assert_eq!(wrap_deg(f32::NEG_INFINITY), 0.0); +} + +#[test] +fn snap_deg_lands_on_fifteens() { + assert_eq!(snap_deg(0.0, SNAP_DEG), 0.0); + assert_eq!(snap_deg(7.0, SNAP_DEG), 0.0); + assert_eq!(snap_deg(8.0, SNAP_DEG), 15.0); + assert_eq!(snap_deg(-7.0, SNAP_DEG), 0.0); + assert_eq!(snap_deg(-8.0, SNAP_DEG), -15.0); + assert_eq!(snap_deg(40.0, SNAP_DEG), 45.0); + assert_eq!(snap_deg(f32::NAN, SNAP_DEG), 0.0); + assert_eq!(snap_deg(40.0, 0.0), wrap_deg(40.0)); + assert_eq!(snap_deg(40.0, f32::NAN), wrap_deg(40.0)); +} + +#[test] +fn apply_params_sets_adds_resets_and_rejects() { + assert_eq!(apply_params(0.0, &json!({"angle": 30.0})).unwrap(), 30.0); + assert_eq!(apply_params(30.0, &json!({"delta": 15.0})).unwrap(), 45.0); + assert_eq!(apply_params(45.0, &json!({"reset": true})).unwrap(), 0.0); + assert_eq!(apply_params(170.0, &json!({"delta": 20.0})).unwrap(), -170.0); + assert!(apply_params(0.0, &json!({})).is_err()); + assert!(apply_params(0.0, &Value::Null).is_err()); + assert!(apply_params(0.0, &json!("30")).is_err()); + assert!(apply_params(0.0, &json!({"angle": f64::NAN})).is_err()); + assert!(apply_params(0.0, &json!({"angle": f64::INFINITY})).is_err()); + assert!(apply_params(0.0, &json!({"delta": f64::INFINITY})).is_err()); + // JSON can contain finite f64 values whose f32 conversion overflows to infinity. + // Silently wrapping infinity to 0 would reset a rotated view. + assert!(apply_params(32.0, &json!({"angle": 1.0e100})).is_err()); + assert!(apply_params(32.0, &json!({"delta": -1.0e100})).is_err()); + assert!(apply_params(0.0, &json!({"reset": "yes"})).is_err()); + assert!(apply_params(0.0, &json!({"zoom": 1})).is_err()); + let huge = apply_params(0.0, &json!({"angle": 1.0e20})).unwrap(); + assert!(huge.is_finite() && huge.abs() <= 180.0); +} + +#[test] +fn native_rotate_event_reaches_canvas_when_trackpad_rotation_is_enabled() { + let ctx = egui::Context::default(); + let mut enabled = None; + let mut disabled = None; + let raw = egui::RawInput { events: vec![Event::Rotate(std::f32::consts::FRAC_PI_6)], ..Default::default() }; + let mut output = ctx.run_ui(raw, |ui| { + enabled = trackpad_delta(ui.ctx(), true, true); + disabled = trackpad_delta(ui.ctx(), false, true); + }); + output.textures_delta.clear(); + + assert!(enabled.is_some_and(|degrees| (degrees - 30.0).abs() < 1e-3), "{enabled:?}"); + assert_eq!(disabled, None); +} + +/// Preferences › Enhanced Controls › Enable Gestures (#204): off, a two-finger twist on a +/// touch screen doesn't turn the view; the trackpad's `Rotate` event still does. +#[test] +fn enable_gestures_switches_touch_screen_rotation() { + use egui::TouchPhase::{Move, Start}; + let touch = |id: u64, phase, pos| Event::Touch { device_id: egui::TouchDeviceId(1), id: egui::TouchId(id), phase, pos, force: None }; + let twist = |touch_gestures: bool, extra: Vec| { + let ctx = egui::Context::default(); + let frames = [ + // egui only starts a multi-touch gesture once it knows where the pointer is. + vec![Event::PointerMoved(pos2(50.0, 50.0))], + vec![touch(1, Start, pos2(40.0, 50.0)), touch(2, Start, pos2(60.0, 50.0))], + // A quarter turn about the centre between the fingers. + [vec![touch(1, Move, pos2(50.0, 40.0)), touch(2, Move, pos2(50.0, 60.0))], extra].concat(), + ]; + let mut last = None; + for (n, events) in (0u32..).zip(frames) { + let raw = egui::RawInput { time: Some(f64::from(n) / 60.0), events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| last = trackpad_delta(ui.ctx(), true, touch_gestures)); + out.textures_delta.clear(); + } + last + }; + let on = twist(true, Vec::new()); + assert!(on.is_some_and(|d| (d.abs() - 90.0).abs() < 1e-3), "on: the fingers turn the view: {on:?}"); + assert_eq!(twist(false, Vec::new()), None, "off: the fingers don't"); + let trackpad = twist(false, vec![Event::Rotate(std::f32::consts::FRAC_PI_6)]); + assert!(trackpad.is_some_and(|d| (d - 30.0).abs() < 1e-3), "off: a trackpad rotate still turns: {trackpad:?}"); +} + +#[test] +fn trackpad_pref_gates_rotation() { + assert_eq!(apply_trackpad(10.0, false, 0.5), None); + assert_eq!(apply_trackpad(10.0, true, 0.0), None); + assert_eq!(apply_trackpad(10.0, true, f32::NAN), None); + assert_eq!(apply_trackpad(10.0, true, f32::INFINITY), None); + let a = apply_trackpad(10.0, true, std::f32::consts::FRAC_PI_2).expect("finite delta"); + assert!((a - 100.0).abs() < 0.05, "{a}"); +} + +#[test] +fn view_xform_identity_matches_unrotated_mapping() { + for zoom in [0.5, 1.0, 2.5] { + for center in [[0.0, 0.0], [320.0, 240.0], [10.0, -4.0]] { + for flip in [false, true] { + let rect = Rect::from_min_size(pos2(100.0, 50.0), vec2(800.0, 600.0)); + let a = ViewXform { rect, zoom, center, flip, rotation: 0.0, aspect: 1.0 }; + let sx = if flip { -1.0 } else { 1.0 }; + let want = rect.center() + vec2((10.0 - center[0]) * zoom * sx, (20.0 - center[1]) * zoom); + let got = a.to_screen(10.0, 20.0); + assert!((got.x - want.x).abs() < 1e-4 && (got.y - want.y).abs() < 1e-4, "{got:?} vs {want:?}"); + } + } + } +} + +#[test] +fn view_xform_roundtrip_and_ninety_degrees() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0)); + for rot in [15.0, 90.0, -45.0, 180.0] { + for flip in [false, true] { + let xf = ViewXform { rect, zoom: 2.0, center: [80.0, 40.0], flip, rotation: rot, aspect: 1.0 }; + let s = xf.to_screen(10.0, 20.0); + let d = xf.to_doc(s); + assert!((d[0] - 10.0).abs() < 1e-3 && (d[1] - 20.0).abs() < 1e-3, "rot={rot} flip={flip} {d:?}"); + } + } + // 90°: a point on +X in document space lands on +Y on screen (Y-down = clockwise). + let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: false, rotation: 90.0, aspect: 1.0 }; + let s = xf.to_screen(1.0, 0.0); + let c = rect.center(); + assert!((s.x - c.x).abs() < 1e-3 && (s.y - (c.y + 1.0)).abs() < 1e-3, "{s:?} vs {c:?}"); + // Flip after rotate: 0° +X goes left. + let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 0.0, aspect: 1.0 }; + assert!(xf.to_screen(1.0, 0.0).x < rect.center().x); + let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 90.0, aspect: 1.0 }; + let s = xf.to_screen(1.0, 0.0); + assert!((s.x - c.x).abs() < 1e-3 && (s.y - (c.y + 1.0)).abs() < 1e-3, "flip then rotate {s:?}"); +} + +/// Screen angle, about the canvas centre, of the document point that was under `p0` at the press. +fn followed_angle(h: &Harness<'static, PhotocraftApp>, p0: Pos2, start_rotation: f32) -> f32 { + let st = h.state(); + let (rect, v, flip) = (st.last_canvas_rect, &st.ui.views[0], st.ui.view.flip_horizontal); + let before = ViewXform { rect, zoom: 1.0, center: v.center, flip, rotation: start_rotation, aspect: 1.0 }; + let d = before.to_doc(p0); + let after = ViewXform { rotation: v.rotation, ..before }; + let s = after.to_screen(d[0] as f32, d[1] as f32) - rect.center(); + wrap_deg(s.y.atan2(s.x).to_degrees() - pointer_deg(p0, rect.center())) +} + +#[test] +fn drag_follows_the_pointer_in_a_flipped_view() { + for flip in [false, true] { + let mut h = harness(); + h.state_mut().ui.view.flip_horizontal = flip; + h.run_steps(2); + let c = h.state().last_canvas_rect.center(); + let p0 = c + vec2(120.0, 0.0); + h.event(Event::PointerMoved(p0)); + h.step(); + press(&mut h, p0, true, false); + // 30° clockwise about the pivot: the image turns 30° clockwise on screen. + let p1 = c + vec2(120.0 * 30f32.to_radians().cos(), 120.0 * 30f32.to_radians().sin()); + h.event(Event::PointerMoved(p1)); + h.step(); + let got = followed_angle(&h, p0, 0.0); + assert!((got - 30.0).abs() < 0.75, "flip={flip}: the image turned {got}°, the pointer 30°"); + press(&mut h, p1, false, false); + } +} + +#[test] +fn screen_delta_mirrors_only_in_a_flipped_view() { + assert_eq!(screen_delta(12.5, false), 12.5); + assert_eq!(screen_delta(12.5, true), -12.5); + assert_eq!(screen_delta(-180.0, true), 180.0); + assert_eq!(screen_delta(f32::NAN, true), 0.0); + assert_eq!(screen_delta(f32::INFINITY, false), 0.0); + // A flipped view still lands on the snapped angle with Shift. + assert_eq!(snap_deg(0.0 + screen_delta(-40.0, true), SNAP_DEG), 45.0); +} + +fn app_docs(n: usize) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for i in 0..n { + app.run("file.new", json!({"width": 200 + i as u32 * 10, "height": 100})).unwrap(); + } + app.sync_views(); + app +} + +#[test] +fn commands_rotate_reset_and_reject_without_a_document() { + let ctx = egui::Context::default(); + let mut empty = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + assert_eq!(crate::menus::invoke(&mut empty, &ctx, "view.rotateView", json!({"angle": 30})).unwrap_err(), "no document open"); + assert_eq!(crate::menus::invoke(&mut empty, &ctx, "view.resetView", json!({})).unwrap_err(), "no document open"); + + let mut app = app_docs(1); + let r = crate::menus::invoke(&mut app, &ctx, "view.rotateView", json!({"angle": 30})).unwrap(); + assert_eq!(r["rotation"], 30.0); + assert_eq!(app.ui.views[0].rotation, 30.0); + let r = crate::menus::invoke(&mut app, &ctx, "view.rotateView", json!({"delta": 15})).unwrap(); + assert_eq!(r["rotation"], 45.0); + let z = app.ui.views[0].zoom; + let c = app.ui.views[0].center; + let r = crate::menus::invoke(&mut app, &ctx, "view.resetView", json!({})).unwrap(); + assert_eq!(r["rotation"], 0.0); + assert_eq!(app.ui.views[0].rotation, 0.0); + assert_eq!(app.ui.views[0].zoom, z); + assert_eq!(app.ui.views[0].center, c); + assert!(crate::menus::invoke(&mut app, &ctx, "view.rotateView", json!({})).is_err()); + assert!(crate::menus::invoke(&mut app, &ctx, "view.rotateView", json!({"angle": f64::NAN})).is_err()); + assert_eq!(app.ui.views[0].rotation, 0.0); + assert_eq!(app.session.documents()[0].history.past_len(), 0); +} + +#[test] +fn ui_set_selects_the_tool_and_sets_rotation() { + let ctx = egui::Context::default(); + let mut app = app_docs(1); + let (req, _) = crate::control::ControlRequest::new("ui.set", json!({"tool": "RotateView", "rotation": 30})); + let out = crate::control::handle(&mut app, &ctx, &req); + let crate::control::Outcome::Done(v) = out else { panic!("done") }; + assert_eq!(v["ok"], true, "{v}"); + assert_eq!(app.ui.tool, crate::state::Tool::RotateView); + assert_eq!(app.ui.views[0].rotation, 30.0); + let (req, _) = crate::control::ControlRequest::new("ui.inspect", json!({})); + let crate::control::Outcome::Done(ins) = crate::control::handle(&mut app, &ctx, &req) else { panic!("inspect") }; + assert_eq!(ins["result"]["tool"], "RotateView"); + assert_eq!(ins["result"]["views"][0]["rotation"], 30.0); + let (req, _) = crate::control::ControlRequest::new("ui.set", json!({"rotation": f64::NAN})); + let crate::control::Outcome::Done(bad) = crate::control::handle(&mut app, &ctx, &req) else { panic!("nan") }; + assert_eq!(bad["ok"], false); + assert_eq!(app.ui.views[0].rotation, 30.0); + let mut empty = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let (req, _) = crate::control::ControlRequest::new("ui.set", json!({"tool": "RotateView"})); + let crate::control::Outcome::Done(ok) = crate::control::handle(&mut empty, &ctx, &req) else { panic!("tool") }; + assert_eq!(ok["ok"], true, "{ok}"); + assert_eq!(empty.ui.tool, crate::state::Tool::RotateView); + let (req, _) = crate::control::ControlRequest::new("ui.set", json!({"rotation": 15})); + let crate::control::Outcome::Done(err) = crate::control::handle(&mut empty, &ctx, &req) else { panic!("no doc") }; + assert_eq!(err["ok"], false); + assert!(err["error"].as_str().unwrap().contains("no document open"), "{err}"); +} + +#[test] +fn match_rotation_copies_the_angle_and_needs_two_documents() { + let ctx = egui::Context::default(); + let one = app_docs(1); + assert_eq!(crate::view_cmds::is_enabled(&one, "window.arrange.matchRotation"), Some(false)); + let mut app = app_docs(2); + assert_eq!(crate::view_cmds::is_enabled(&app, "window.arrange.matchRotation"), Some(true)); + app.ui.views[1].rotation = 30.0; + let r = crate::menus::invoke(&mut app, &ctx, "window.arrange.matchRotation", json!({})).unwrap(); + assert_eq!(r["rotation"], 30.0); + assert!(app.ui.views.iter().all(|v| (v.rotation - 30.0).abs() < 1e-4)); + app.ui.views[1].rotation = 12.0; + app.ui.views[1].zoom = 2.0; + crate::menus::invoke(&mut app, &ctx, "window.arrange.matchAll", json!({})).unwrap(); + assert!(app.ui.views.iter().all(|v| (v.rotation - 12.0).abs() < 1e-4 && (v.zoom - 2.0).abs() < 1e-4)); +} + +fn harness() -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = false; + app.ui.tool = crate::state::Tool::RotateView; + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + h +} + +fn press(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, pressed: bool, shift: bool) { + let modifiers = egui::Modifiers { shift, ..Default::default() }; + h.event(Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers }); + h.step(); +} + +#[test] +fn shift_drag_snaps_to_fifteen_degrees() { + let mut h = harness(); + let r = h.state().last_canvas_rect; + let c = r.center(); + let p0 = c + vec2(120.0, 0.0); + h.event(Event::PointerMoved(p0)); + h.step(); + h.event(Event::ModifiersChanged(egui::Modifiers::SHIFT)); + h.step(); + press(&mut h, p0, true, true); + // ~40° clockwise: Shift rounds to 45°. + let p1 = c + vec2(120.0 * 40f32.to_radians().cos(), 120.0 * 40f32.to_radians().sin()); + h.event(Event::PointerMoved(p1)); + h.step(); + let rot = h.state().ui.views[0].rotation; + assert!((rot - 45.0).abs() < 0.75, "{rot}"); + press(&mut h, p1, false, true); + h.run_steps(2); + assert_eq!(h.state().session.documents()[0].history.past_len(), 0); + assert_eq!(crate::state::Tool::from_name("RotateView"), Some(crate::state::Tool::RotateView)); + assert_eq!(crate::state::Tool::RotateView.key(), 'R'); +} + +#[test] +fn escape_and_reset_zero_the_angle_without_history() { + let ctx = egui::Context::default(); + let mut app = app_docs(1); + crate::menus::invoke(&mut app, &ctx, "view.rotateView", json!({"angle": 33})).unwrap(); + assert_eq!(app.session.documents()[0].history.past_len(), 0); + crate::menus::invoke(&mut app, &ctx, "view.resetView", json!({})).unwrap(); + assert_eq!(app.ui.views[0].rotation, 0.0); + assert_eq!(app.session.documents()[0].history.past_len(), 0); + + let mut h = harness(); + h.state_mut().ui.views[0].rotation = 40.0; + h.event(Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() }); + h.step(); + h.run_steps(2); + assert!((h.state().ui.views[0].rotation).abs() < 0.05, "{}", h.state().ui.views[0].rotation); + assert_eq!(h.state().session.documents()[0].history.past_len(), 0); +} + +#[test] +fn rotated_view_keeps_engine_selection_in_document_space() { + let mut app = app_docs(1); + app.ui.views[0].rotation = 30.0; + app.run("select.rect", json!({"x": 10, "y": 20, "width": 40, "height": 10})).unwrap(); + let st = app.session.active().unwrap(); + let b = st.doc.selection.as_ref().and_then(|s| photocraft_compose::bounds::content_bounds(s).into()).expect("selection"); + // Axis-aligned in the file: the outline is drawn rotated via ViewXform. + assert!(b.width() >= 40 && b.height() >= 10, "{b:?}"); + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [100.0, 50.0], flip: false, rotation: 30.0, aspect: 1.0 }; + let a = xf.to_screen(10.0, 20.0); + let c = xf.to_screen(50.0, 20.0); + let v = c - a; + let ang = v.y.atan2(v.x).to_degrees(); + assert!((wrap_deg(ang) - 30.0).abs() < 0.5, "{ang}"); + assert_eq!(st.history.past_len(), 1, "only the selection is a history step"); +} + +#[test] +fn gpu_path_stays_on_with_rotation() { + // Flip is the only CPU-blit fallback; rotation is a shader uniform. + assert!(crate::gpu_canvas::gpu_view_ok(false, 45.0, 1.0)); + assert!(!crate::gpu_canvas::gpu_view_ok(true, 45.0, 1.0)); + assert!(crate::gpu_canvas::gpu_view_ok(false, 0.0, 1.0)); +} diff --git a/crates/ui-egui/src/canvas/rulers.rs b/crates/ui-egui/src/canvas/rulers.rs new file mode 100644 index 0000000000..ce1ae599b5 --- /dev/null +++ b/crates/ui-egui/src/canvas/rulers.rs @@ -0,0 +1,597 @@ +//! View extras: rulers (⌘R), grid (⌘'), guides (⌘;). Guides are dragged out of the rulers and +//! moved with the Move tool (drag one off the canvas or onto a ruler to delete it), all through +//! undoable engine commands (`view.newGuide` / `view.moveGuide` / `view.deleteGuide`). + +use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, pos2, vec2}; +use photocraft_doc::Document; +use photocraft_engine::prefs::{GuidesGridAndSlices, Unit}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::ViewXform; +use crate::theme::Tokens; + +pub const RULER: f32 = 16.0; + +/// A `#rrggbb` preference colour. +fn pref_color(s: &str, fallback: Color32) -> Color32 { + photocraft_engine::prefs::parse_hex(s).map_or(fallback, |c| Color32::from_rgb(c[0], c[1], c[2])) +} + +/// Draw a line in a Guides/Grid preference style (lines, dashed lines, dots). +fn styled_line(painter: &egui::Painter, a: Pos2, b: Pos2, stroke: Stroke, style: photocraft_engine::prefs::LineStyle) { + use photocraft_engine::prefs::LineStyle; + match style { + LineStyle::Lines => { + painter.line_segment([a, b], stroke); + } + LineStyle::Dashed => { + painter.extend(egui::Shape::dashed_line(&[a, b], stroke, 4.0, 3.0)); + } + LineStyle::Dots => { + painter.extend(egui::Shape::dotted_line(&[a, b], stroke.color, 3.0, 0.6)); + } + } +} + +/// Ruler step in units so labelled ticks are at least `min` units apart: …0.1, 0.25, 0.5, 1, 2, 5, +/// 10… (whole steps only for pixels). +fn nice_step(min: f64, whole: bool) -> f64 { + let mut step = if whole { 1.0 } else { 0.001 }; + loop { + for m in [1.0, 2.0, 2.5, 5.0] { + if (m != 2.5 || !whole) && step * m >= min { + return step * m; + } + } + step *= 10.0; + } +} + +/// English display name for a length unit, as shown in Preferences › Units & Rulers. +fn unit_label(u: Unit) -> &'static str { + match u { + Unit::Pixels => "Pixels", + Unit::Inches => "Inches", + Unit::Centimeters => "Centimeters", + Unit::Millimeters => "Millimeters", + Unit::Points => "Points", + Unit::Picas => "Picas", + Unit::Percent => "Percent", + } +} + +/// Every length unit, in the Preferences › Units & Rulers order. +const UNITS: [Unit; 7] = [Unit::Pixels, Unit::Inches, Unit::Centimeters, Unit::Millimeters, Unit::Points, Unit::Picas, Unit::Percent]; + +/// The units Photoshop's Create Rectangle (and the other shape dialogs) offer on a right-click of +/// Width or Height: the lengths, without Percent. +pub(crate) const LENGTH_UNITS: [Unit; 6] = [Unit::Pixels, Unit::Inches, Unit::Centimeters, Unit::Millimeters, Unit::Points, Unit::Picas]; + +/// The ruler right-click menu: every unit of `units` with its (translated) label and whether it's +/// the one in effect. Picking one changes the ruler unit exactly as the preference pane does. +fn unit_menu_items(current: Unit, units: &[Unit]) -> Vec<(String, &'static str, bool)> { + units.iter().map(|&u| (tl!(unit_label(u)).to_string(), u.name(), u == current)).collect() +} + +/// The length units menu that right-clicking a ruler (or Free Transform's W and H fields) opens; +/// the chosen unit's name is returned (`None` if the menu is dismissed without a pick). +pub(crate) fn unit_menu(ui: &mut egui::Ui, current: Unit) -> Option<&'static str> { + unit_menu_of(ui, current, &UNITS) +} + +/// [`unit_menu`] with only `units` in it. +pub(crate) fn unit_menu_of(ui: &mut egui::Ui, current: Unit, units: &[Unit]) -> Option<&'static str> { + crate::widgets::menu_scroll(ui, |ui| { + ui.set_min_width(150.0); + let mut chosen = None; + for (label, name, checked) in unit_menu_items(current, units) { + let label = if checked { format!("✓ {label}") } else { label }; + if ui.button(label).clicked() { + ui.close(); + chosen = Some(name); + } + } + chosen + }) +} + +/// A guide being dragged: from a ruler (new) or an existing one (index). +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct GuideDrag { + pub vertical: bool, + pub index: Option, + pub pos: f64, + /// Where the pointer went down, on the guide's axis: released there, an existing guide is + /// clicked (selected), not moved. + pub press: f64, + /// Whether the pointer was over a ruler when the guide was let go (#6286): a guide released + /// there is removed, whatever its own axis reads. + pub over_ruler: bool, + pub flipped: bool, +} + +impl GuideDrag { + pub fn shows_vertical(&self) -> bool { + self.vertical != self.flipped + } + pub fn track(&mut self, at: [f64; 2], alt: bool) { + self.flipped = alt; + self.pos = if self.shows_vertical() { at[0] } else { at[1] }; + } + + /// An existing guide taken at document point `p`. + pub fn existing(vertical: bool, index: usize, p: [f64; 2]) -> Self { + let at = if vertical { p[0] } else { p[1] }; + Self { vertical, index: Some(index), pos: at, press: at, over_ruler: false, flipped: false } + } +} + +/// Canvas area left after the rulers. +pub fn content_rect(app: &PhotocraftApp, rect: Rect) -> Rect { + if app.ui.extras.rulers { Rect::from_min_max(rect.min + vec2(RULER, RULER), rect.max) } else { rect } +} + +/// Whether the screen point `p` is over a ruler, inside the full canvas rect `full`. A guide let go +/// there is removed, as in Photoshop, and either ruler removes it (#6286): the top ruler measures +/// document x and the left one y, so the ruler the guide was not dragged out of can lie over a +/// coordinate that is still inside the document on the guide's own axis. +pub(crate) fn dropped_on_a_ruler(app: &PhotocraftApp, full: Rect, p: Pos2) -> bool { + if !app.ui.extras.rulers { + return false; + } + // The rulers take the strips [`content_rect`] trims off the canvas rect, and nothing else: a + // canvas point on the boundary is canvas. + let content = content_rect(app, full); + full.contains(p) && (p.x < content.min.x || p.y < content.min.y) +} + +/// Tick step (document px) so major ticks are at least ~60 screen px apart: 1, 2, 5, 10, 20, 50… +#[cfg(test)] +fn tick_step(zoom: f32) -> f64 { + nice_step(60.0 / zoom.max(1e-4) as f64, true) +} + +/// Grid spacing in document pixels. Percentages use each axis's own extent. +pub(crate) fn grid_major_px(g: &GuidesGridAndSlices, dpi: f64, size: [f64; 2], ppi: f64) -> [f64; 2] { + size.map(|extent| g.major_px(dpi, extent, ppi)) +} + +/// View › Show › Grid, from Preferences › Guides, Grid & Slices (spacing, subdivisions, colour, +/// style). Photoshop's default is a gridline every inch with 4 subdivisions. +pub fn draw_grid(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, doc: &Document) { + let g = &app.session.prefs().guides_grid_and_slices; + let ppi = app.session.prefs().units_and_rulers.point_size.per_inch(); + let (w, h) = (doc.size.width as f64, doc.size.height as f64); + let major = grid_major_px(g, doc.resolution_dpi.max(1.0) as f64, [w, h], ppi); + let minor = major.map(|step| step / g.subdivisions.max(1) as f64); + let base = pref_color(&g.grid_color, Color32::from_gray(140)); + for (steps, alpha) in [(minor, 0.45f32), (major, 1.0)] { + if alpha < 1.0 && g.subdivisions <= 1 { + continue; + } + let st = Stroke::new(1.0, base.gamma_multiply(alpha)); + let [step_x, step_y] = steps; + if step_x * (xf.zoom_x() as f64) >= 6.0 { + let mut x = step_x; + while x < w { + styled_line(painter, xf.to_screen(x as f32, 0.0), xf.to_screen(x as f32, h as f32), st, g.grid_style); + x += step_x; + } + } + if step_y * (xf.zoom as f64) >= 6.0 { + let mut y = step_y; + while y < h { + styled_line(painter, xf.to_screen(0.0, y as f32), xf.to_screen(w as f32, y as f32), st, g.grid_style); + y += step_y; + } + } + } +} + +/// Screen end points of a guide across `clip`: the document line x = `pos` (vertical) or +/// y = `pos`, spanning every document point the clip rect shows. Guides belong to the document, +/// so on a rotated view they turn with the image (#3277); unrotated this is the clip's full +/// height (or width). +fn guide_segment(xf: &ViewXform, clip: Rect, vertical: bool, pos: f64) -> [Pos2; 2] { + let axis = usize::from(vertical); + let (mut lo, mut hi) = (f64::INFINITY, f64::NEG_INFINITY); + for c in [clip.left_top(), clip.right_top(), clip.right_bottom(), clip.left_bottom()] { + let d = xf.to_doc(c)[axis]; + lo = lo.min(d); + hi = hi.max(d); + } + let (pos, lo, hi) = (pos as f32, lo as f32, hi as f32); + if vertical { [xf.to_screen(pos, lo), xf.to_screen(pos, hi)] } else { [xf.to_screen(lo, pos), xf.to_screen(hi, pos)] } +} + +/// The cursor over a guide: the arrow across it as the view shows it. A vertical guide moves left +/// and right in the document; a rotated view turns that, as it turns the guide (#3277). +pub fn guide_cursor(xf: &ViewXform, vertical: bool) -> egui::CursorIcon { + crate::tool_cursor::resize(if vertical { [1.0, 0.0] } else { [0.0, 1.0] }, xf) +} + +pub fn draw_guides(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, doc: &Document) { + let clip = painter.clip_rect(); + let drag = app.guide_drag; + let g = &app.session.prefs().guides_grid_and_slices; + let preview = crate::guide_layout_ui::preview(app, doc); + let guides = preview.as_ref().map(|(guides, _)| guides).unwrap_or(&doc.guides); + let (guide, style) = + (preview.as_ref().map(|(_, color)| *color).unwrap_or_else(|| pref_color(&g.guide_color, Color32::from_rgb(74, 255, 255))), g.guide_style); + let line = |vertical: bool, pos: f64, color: Color32| { + let [a, b] = guide_segment(xf, clip, vertical, pos); + styled_line(painter, a, b, Stroke::new(1.0, color), style); + }; + // A guide given a colour in New Guide keeps it; the rest use the preference colour (or, while + // New Guide Layout previews, the colour it will set). The selected guide is Photoshop's blue. + let own = |vertical: bool, p: f64| { + if crate::guide_select::is_selected(app, vertical, p) { + return crate::guide_select::SELECTED_COLOR; + } + guides.color(vertical, p).map_or(guide, |[r, g, b]| Color32::from_rgb(r, g, b)) + }; + if app.ui.extras.guides || preview.is_some() { + for (vertical, list) in [(true, &guides.vertical), (false, &guides.horizontal)] { + for (i, p) in list.iter().enumerate() { + if drag.is_some_and(|d| d.vertical == vertical && d.index == Some(i)) { + continue; + } + line(vertical, *p, own(vertical, *p)); + } + } + } + if let Some(d) = drag { + // A guide being moved shows its own colour; a new one dragged from a ruler the preference's. + let list = if d.vertical { &doc.guides.vertical } else { &doc.guides.horizontal }; + line(d.shows_vertical(), d.pos, d.index.and_then(|i| list.get(i)).map_or(guide, |p| own(d.vertical, *p))); + } +} + +/// The position readout of a guide being dragged: `X:` for a vertical guide, `Y:` for a horizontal +/// one, measured from the ruler zero point in the ruler unit. Pixels keep one decimal, as +/// Photoshop's "X: 103.2 px". +pub fn guide_drag_readout(app: &PhotocraftApp, d: GuideDrag) -> Option<(&'static str, String)> { + let doc = &app.session.active()?.doc; + let ur = &app.session.prefs().units_and_rulers; + let axis = usize::from(!d.shows_vertical()); + let px = d.pos - doc.ruler_origin.get(axis).copied().unwrap_or(0.0); + let extent = if d.shows_vertical() { doc.size.width } else { doc.size.height } as f64; + let value = match ur.rulers { + Unit::Pixels => photocraft_engine::prefs::fmt_decimals(px, 1), + _ => ur.format(px, f64::from(doc.resolution_dpi), extent), + }; + Some((if d.shows_vertical() { "X:" } else { "Y:" }, format!("{value} {}", ur.rulers.suffix()))) +} + +/// Draw [`guide_drag_readout`] beside the pointer while a guide is dragged. +pub fn draw_guide_drag_readout(app: &PhotocraftApp, ctx: &egui::Context) { + let (Some(d), Some(at)) = (app.guide_drag, ctx.pointer_latest_pos()) else { return }; + if let Some((label, value)) = guide_drag_readout(app, d) { + crate::canvas::draw_value_readout(app, ctx, "guide-drag-readout", at, [label], [value]); + } +} + +/// Existing guide under a document point (within 4 screen px). +pub fn guide_at(app: &PhotocraftApp, x: f64, y: f64) -> Option<(bool, usize)> { + // Hidden canvas guides are not interactive: the Move tool must reach the layer below. + // Keep hit testing consistent with the visibility gate in canvas::draw. + if !app.ui.extras.guides || !app.ui.view.shows(app.ui.view.show.canvas_guides) || app.ui.extras.lock_guides { + return None; + } + let doc = &app.session.active()?.doc; + let tol = 4.0 / app.point_zoom().max(0.01) as f64; + for (i, g) in doc.guides.vertical.iter().enumerate() { + if (*g - x).abs() <= tol { + return Some((true, i)); + } + } + for (i, g) in doc.guides.horizontal.iter().enumerate() { + if (*g - y).abs() <= tol { + return Some((false, i)); + } + } + None +} + +/// Finish a guide drag: create, move, or delete (dropped outside the canvas or on a ruler). An +/// existing guide released where it was pressed is a click: it's selected and stays put, as in +/// Photoshop; a moved one stays selected. +pub fn finish_drag(app: &mut PhotocraftApp, d: GuideDrag) { + let Some(size) = app.session.active().map(|s| s.doc.size) else { return }; + // A drop on a ruler removes the guide even when the drag held its own axis, which otherwise + // reads as a click: dragged sideways onto the left ruler, a horizontal guide never moves in y. + if let Some(i) = d.index + && !d.over_ruler + && !d.flipped + && (d.pos - d.press).abs() * f64::from(app.point_zoom().max(0.01)) < 0.5 + { + crate::guide_select::select(app, d.vertical, i); + return; + } + let shows = d.shows_vertical(); + let extent = if shows { size.width } else { size.height } as f64; + let orientation = if d.vertical { "vertical" } else { "horizontal" }; + let inside = !d.over_ruler && d.pos >= 0.0 && d.pos <= extent; + let pos = (d.pos * 1000.0).round() / 1000.0; + let _ = match (d.index, inside) { + (None, true) => app.run("view.newGuide", json!({"orientation": if shows {"vertical"}else{"horizontal"}, "position": pos})), + (Some(i), true) => app + .run("view.moveGuide", json!({"orientation": orientation,"toOrientation":if shows {"vertical"}else{"horizontal"}, "index": i, "position": pos})) + .inspect(|_| { + let index = if d.flipped { + app.session.active().map_or(0, |st| if shows { st.doc.guides.vertical.len() } else { st.doc.guides.horizontal.len() }).saturating_sub(1) + } else { + i + }; + crate::guide_select::select(app, shows, index); + }), + (Some(i), false) => app.run("view.deleteGuide", json!({"orientation": orientation, "index": i})), + (None, false) => Ok(serde_json::Value::Null), + }; + if d.index.is_none() && inside { + app.ui.extras.guides = true; + } +} + +/// The mapping the rulers measure with. Rulers are screen chrome that stays level while +/// View › Rotate View turns the canvas, so they use the view without its rotation: the same zoom, +/// flip and pixel aspect (ticks never stretch), anchored so that 0 sits at the screen position of +/// the document origin as the rotated view draws it, as in Photoshop. Tilted guides then meet the +/// rulers near their numbers. Measuring along the rotated axes instead squeezed the ticks into a +/// few pixels (or none at 90°) and left the rest of the ruler empty (#3233). +/// +/// The returned mapping measures from the rulers' zero point `zero` (document px, #2955): its +/// document coordinates are distances from `zero`, and 0 sits where the canvas draws `zero`. +fn ruler_xform(xf: &ViewXform, zero: [f64; 2]) -> ViewXform { + let level = ViewXform { rotation: 0.0, ..*xf }; + let (zx, zy) = (zero[0] as f32, zero[1] as f32); + // The level view is an axis-aligned scale, so moving the zero point by `shift` on screen moves + // every point by it: re-centre on the document point that sat `shift` before the centre, then + // count from the zero point. + let shift = xf.to_screen(zx, zy) - level.to_screen(zx, zy); + let center = level.to_doc(xf.rect.center() - shift); + let center = [center[0] as f32 - zx, center[1] as f32 - zy]; + if !(center[0].is_finite() && center[1].is_finite()) { + return level; + } + ViewXform { center, ..level } +} + +/// Visible ruler range in ruler units, normalised to `(min, max)` so the tick loop runs for +/// flipped views too (#1814), measured from the zero point `zero`. `xf` is the canvas mapping; +/// rotation is ignored (see [`ruler_xform`]). Shared by the renderer and its tests. +fn ruler_range(xf: &ViewXform, a: Pos2, b: Pos2, axis: usize, px_per_unit: f64, zero: [f64; 2]) -> (f64, f64) { + let rxf = ruler_xform(xf, zero); + let d0 = rxf.to_doc(a)[axis]; + let d1 = rxf.to_doc(b)[axis]; + let (u0, u1) = (d0 / px_per_unit, d1 / px_per_unit); + (u0.min(u1), u0.max(u1)) +} + +/// Snap a document position to the nearest ruler tick, measured from the zero point `origin` +/// (`tick` px apart). Shift-dragging the zero point lands it on a tick, as in Photoshop. +fn snap_px_to_tick(px: f64, origin: f64, tick: f64) -> f64 { + if !(tick.is_finite() && tick > 0.0) { + return px; + } + origin + ((px - origin) / tick).round() * tick +} + +/// Where a zero-point drag from the ruler corner lands for the pointer at document `d`: on the +/// nearest ruler tick (`ticks`, px per axis) with Shift, else View › Snap To targets. +fn origin_drop(app: &mut PhotocraftApp, d: [f64; 2], shift: bool, origin: [f64; 2], ticks: [f64; 2]) -> [f64; 2] { + if shift { + [snap_px_to_tick(d[0], origin[0], ticks[0]), snap_px_to_tick(d[1], origin[1], ticks[1])] + } else { + [crate::snap_ui::snap_guide(app, true, d[0]), crate::snap_ui::snap_guide(app, false, d[1])] + } +} + +/// Screen coordinate along the ruler (x for the top ruler, y for the left one) of the value `u` +/// in ruler units from the zero point `zero`. The inverse of [`ruler_range`]'s mapping. +fn ruler_pos(xf: &ViewXform, vertical: bool, u: f64, px_per_unit: f64, zero: [f64; 2]) -> f32 { + let px = (u * px_per_unit) as f32; + let rxf = ruler_xform(xf, zero); + if vertical { rxf.to_screen(0.0, px).y } else { rxf.to_screen(px, 0.0).x } +} + +/// Document pixels per ruler unit and the labelled tick step (in ruler units) of the top ruler +/// (`y_axis` false) or the left one, from Preferences › Units & Rulers and the zoom. Minor ticks +/// sit at tenths of the step. +fn ruler_scale(app: &PhotocraftApp, xf: &ViewXform, y_axis: bool) -> (f64, f64) { + ruler_scale_of(app, app.session.active().map(|d| &*d.doc), xf, y_axis) +} + +/// [`ruler_scale`] for the view of `doc`. +fn ruler_scale_of(app: &PhotocraftApp, doc: Option<&photocraft_doc::Document>, xf: &ViewXform, y_axis: bool) -> (f64, f64) { + let ur = &app.session.prefs().units_and_rulers; + let (unit, ppi) = (ur.rulers, ur.point_size.per_inch()); + let (dpi, size) = doc.map_or((72.0, [1.0, 1.0]), |d| (d.resolution_dpi.max(1.0) as f64, [d.size.width as f64, d.size.height as f64])); + let px_per_unit = unit.to_px(1.0, dpi, if y_axis { size[1] } else { size[0] }, ppi).max(1e-9); + // The top ruler runs along document x, which pixel aspect correction stretches. + let zoom = if y_axis { xf.zoom } else { xf.zoom_x() }; + (px_per_unit, nice_step(60.0 / (zoom as f64 * px_per_unit).max(1e-6), unit == Unit::Pixels)) +} + +/// `pos` (document px) moved to the nearest tick mark the ruler draws (counted from the zero point): where a guide lands when +/// Shift is held while dragging it out of a ruler, as in Photoshop (#3770). +fn snap_to_tick(app: &PhotocraftApp, xf: &ViewXform, vertical: bool, pos: f64) -> f64 { + // A vertical guide sits at an x position, which the top ruler measures. + let (px_per_unit, step) = ruler_scale(app, xf, !vertical); + // Ticks count from the zero point (#2955), which a vertical guide measures along x. + let origin = app.session.active().map_or(0.0, |d| d.doc.ruler_origin[usize::from(!vertical)]); + snap_px_to_tick(pos, origin, step / 10.0 * px_per_unit) +} + +/// The number a ruler prints at the tick `v` (in ruler units). Like Photoshop, rulers show the +/// distance from the origin, so values left of / above it read without a minus sign. +fn ruler_label(v: f64, step: f64, whole: bool) -> String { + let v = v.abs(); + if step >= 1.0 || whole { format!("{}", v.round() as i64) } else { crate::widgets::fmt_num2(v) } +} + +/// The rulers of `doc`'s view in `full`: background, ticks and labels from its zero point. +/// Returns the top, left and corner rects, the painter, the zero point and the minor tick sizes. +#[allow(clippy::type_complexity)] +fn paint( + app: &PhotocraftApp, + ui: &egui::Ui, + full: Rect, + xf: &ViewXform, + doc: &photocraft_doc::Document, +) -> (Rect, Rect, Rect, egui::Painter, [f64; 2], [f64; 2]) { + let t = Tokens::get(ui.ctx()); + let top = Rect::from_min_max(pos2(full.left() + RULER, full.top()), pos2(full.right(), full.top() + RULER)); + let left = Rect::from_min_max(pos2(full.left(), full.top() + RULER), pos2(full.left() + RULER, full.bottom())); + let corner = Rect::from_min_size(full.min, vec2(RULER, RULER)); + let p = ui.painter_at(full); + let bg = t.chrome; + for r in [top, left, corner] { + p.rect_filled(r, 0.0, bg); + } + p.line_segment([top.left_bottom(), top.right_bottom()], Stroke::new(1.0, t.separator)); + p.line_segment([left.right_top(), left.right_bottom()], Stroke::new(1.0, t.separator)); + let font = egui::FontId::proportional(9.0); + let tick = Stroke::new(1.0, t.text_faint); + // Preferences › Units & Rulers: labels in the ruler unit (percent of the document's side). + let unit = app.session.prefs().units_and_rulers.rulers; + let whole = unit == Unit::Pixels; + // The zero point (#2955): rulers read 0 there and count the distance from it. + let origin = doc.ruler_origin; + let mut ticks = [1.0; 2]; + for vertical in [false, true] { + let p = p.with_clip_rect(if vertical { left } else { top }); + let (px_per_unit, step) = ruler_scale_of(app, Some(doc), xf, vertical); + ticks[usize::from(vertical)] = step * px_per_unit / 10.0; + let (u0, u1) = if vertical { + ruler_range(xf, left.left_top(), left.left_bottom(), 1, px_per_unit, origin) + } else { + ruler_range(xf, top.left_top(), top.right_top(), 0, px_per_unit, origin) + }; + let mut v = (u0 / step).floor() * step; + while v <= u1 { + let at = |u: f64| -> f32 { ruler_pos(xf, vertical, u, px_per_unit, origin) }; + let c = at(v); + if vertical { + p.line_segment([pos2(left.right() - RULER, c), pos2(left.right(), c)], tick); + // Labels stacked, as in Photoshop, each character centred in the ruler's width + // (a narrow "1" used to hug the left edge while a "0" filled the strip). + for (i, ch) in ruler_label(v, step, whole).chars().enumerate() { + p.text(pos2(left.left() + RULER * 0.5, c + 2.0 + i as f32 * 8.5), Align2::CENTER_TOP, ch, font.clone(), t.text_dim); + } + } else { + p.line_segment([pos2(c, top.bottom() - RULER), pos2(c, top.bottom())], tick); + p.text(pos2(c + 2.0, top.top() + 1.0), Align2::LEFT_TOP, ruler_label(v, step, whole), font.clone(), t.text_dim); + } + for k in 1..10 { + let m = at(v + step * k as f64 / 10.0); + let len = if k == 5 { 6.0 } else { 3.0 }; + if vertical { + p.line_segment([pos2(left.right() - len, m), pos2(left.right(), m)], tick); + } else { + p.line_segment([pos2(m, top.bottom() - len), pos2(m, top.bottom())], tick); + } + } + v += step; + } + } + (top, left, corner, p, origin, ticks) +} + +/// Rulers for a document view that isn't the active one (another tile of Window › Arrange, a +/// floating window): shown as the active view's are, but guides and the zero point are dragged +/// in the active view (#6017). +pub fn paint_rulers(app: &PhotocraftApp, ui: &egui::Ui, full: Rect, xf: &ViewXform, doc: &photocraft_doc::Document) { + paint(app, ui, full, xf, doc); +} + +/// Rulers along the top and left of `full` (the canvas rect before `content_rect`), with the +/// pointer position marked; dragging out of a ruler creates a guide. +pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &ViewXform) { + let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return }; + let (top, left, corner, p, origin, ticks) = paint(app, ui, full, xf, &doc); + let t = Tokens::get(ui.ctx()); + let unit = app.session.prefs().units_and_rulers.rulers; + // Pointer position markers. + if let Some(h) = ui.ctx().pointer_hover_pos().filter(|h| full.contains(*h)) { + let m = Stroke::new(1.0, t.text); + p.with_clip_rect(top).line_segment([pos2(h.x, top.top()), pos2(h.x, top.bottom())], m); + p.with_clip_rect(left).line_segment([pos2(left.left(), h.y), pos2(left.right(), h.y)], m); + } + // Drag a new guide out of a ruler; right-click to switch the ruler unit. + let mut chosen_unit: Option<&'static str> = None; + for (r, vertical, salt) in [(top, false, "ruler-top"), (left, true, "ruler-left"), (corner, false, "ruler-corner")] { + // Guides drag out of the top/left rulers and the zero point out of the corner; all three + // sense clicks so the right-click unit menu opens anywhere on the ruler. + let resp = ui.interact(r, ui.scope_id().with(salt), Sense::click_and_drag()); + if salt == "ruler-corner" { + corner_origin(app, ui, &resp, &p, xf, origin, ticks); + } else if resp.hovered() { + ui.ctx().set_cursor_icon(if vertical { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::ResizeVertical }); + } + if let Some(pos) = resp.interact_pointer_pos().filter(|_| salt != "ruler-corner" && (resp.dragged() || resp.drag_started())) { + // ⌥/Alt turns the guide the other way, as in Photoshop: a vertical guide out of the top + // ruler, a horizontal one out of the left (#5698), switching as Alt goes up and down. + let vertical = vertical != ui.input(|i| i.modifiers.alt); + let d = xf.to_doc(pos); + let along = if vertical { d[0] } else { d[1] }; + let pos = if ui.input(|i| i.modifiers.shift) { snap_to_tick(app, xf, vertical, along) } else { crate::snap_ui::snap_guide(app, vertical, along) }; + app.guide_drag = Some(GuideDrag { vertical, index: None, pos, press: pos, over_ruler: false, flipped: false }); + } + if resp.drag_stopped() + && let Some(mut d) = app.guide_drag.take() + { + // Let go over a ruler (the other one, say) the drag is discarded, not placed (#6286). + d.over_ruler = resp.interact_pointer_pos().is_some_and(|p| dropped_on_a_ruler(app, full, p)); + finish_drag(app, d); + } + resp.context_menu(|ui| { + if let Some(name) = unit_menu(ui, unit) { + chosen_unit = Some(name); + } + }); + } + if let Some(name) = chosen_unit { + let _ = app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": name})); + } + let _ = Pos2::ZERO; +} + +/// The ruler corner: drag the zero point out of it (cross hairs mark where it lands; Shift snaps +/// to the ruler ticks), double-click it to put the zero point back at the image's top-left. +fn corner_origin(app: &mut PhotocraftApp, ui: &egui::Ui, resp: &egui::Response, p: &egui::Painter, xf: &ViewXform, origin: [f64; 2], ticks: [f64; 2]) { + let id = resp.id.with("origin-drag"); + if resp.hovered() || resp.dragged() { + ui.ctx().set_cursor_icon(egui::CursorIcon::Crosshair); + } + if resp.double_clicked() { + let _ = app.run("view.resetRulerOrigin", json!({})); + return; + } + // Dropped back on the corner (or a slow click), the zero point stays where it was. + if let Some(pos) = resp.interact_pointer_pos().filter(|_| resp.dragged() || resp.drag_started()) { + if resp.rect.contains(pos) { + ui.data_mut(|d| d.remove_temp::<[f64; 2]>(id)); + return; + } + let shift = ui.input(|i| i.modifiers.shift); + let at = origin_drop(app, xf.to_doc(pos), shift, origin, ticks); + ui.data_mut(|d| d.insert_temp(id, at)); + // Cross hairs along the document axes through the new zero point, drawn like guides so + // they turn with a rotated view. + let t = Tokens::get(ui.ctx()); + for (vertical, pos) in [(true, at[0]), (false, at[1])] { + p.line_segment(guide_segment(xf, p.clip_rect(), vertical, pos), Stroke::new(1.0, t.text)); + } + } + if resp.drag_stopped() + && let Some(at) = ui.data_mut(|d| d.remove_temp::<[f64; 2]>(id)) + { + let round = |v: f64| (v * 1000.0).round() / 1000.0; + let _ = app.run("view.setRulerOrigin", json!({"x": round(at[0]), "y": round(at[1])})); + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/rulers/tests.rs b/crates/ui-egui/src/canvas/rulers/tests.rs new file mode 100644 index 0000000000..9b08137eed --- /dev/null +++ b/crates/ui-egui/src/canvas/rulers/tests.rs @@ -0,0 +1,969 @@ +use super::*; + +#[test] +fn ruler_ticks_labels_and_pointer_stay_out_of_the_origin_corner() { + let mut failures = Vec::new(); + for theme in crate::theme::ThemeKind::ALL { + for (zoom, flip, rotation, width) in [(1.0, false, 0.0, 560.0), (1.0, false, 0.0, 800.0), (0.25, true, 0.0, 560.0), (2.0, false, 30.0, 560.0)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + let full = Rect::from_min_size(pos2(32.0, 32.0), vec2(width - 80.0, 320.0)); + let corner = Rect::from_min_size(full.min, vec2(RULER, RULER)); + let builder = egui_kittest::Harness::builder().with_size(vec2(width, 400.0)); + let mut h = builder.build_ui_state( + move |ui, app| { + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom, center: [(width - 80.0) / 2.0, 160.0], flip, rotation, aspect: 1.0 }; + draw_rulers(app, ui, full, &xf); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, theme); + h.event(egui::Event::PointerMoved(corner.center())); + h.run_steps(4); + let mut ticks = 0; + let mut labels = 0; + for shape in &h.output().shapes { + if matches!(shape.shape, egui::Shape::LineSegment { .. } | egui::Shape::Text(_)) { + ticks += usize::from(matches!(shape.shape, egui::Shape::LineSegment { .. })); + labels += usize::from(matches!(shape.shape, egui::Shape::Text(_))); + let visible = shape.shape.visual_bounding_rect().intersect(shape.clip_rect); + if visible.intersect(corner.shrink(0.5)).is_positive() { + failures.push(format!("{theme:?}: zoom={zoom}, flip={flip}, rotation={rotation}, {visible:?}")); + } + } + } + assert!(ticks > 2 && labels > 0, "rulers still draw ticks and labels"); + } + } + assert!(failures.is_empty(), "ruler drawing overlaps the origin corner: {failures:?}"); +} + +/// #7314: the stacked characters of the left ruler's labels are centred on the strip, so a +/// narrow "1" lines up with a wide "0". +#[test] +fn left_ruler_label_characters_are_centred() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + let (mut h, full, _) = ruler_harness(app); + h.run_steps(2); + let centres: Vec = h + .output() + .shapes + .iter() + .filter_map(|s| if let egui::Shape::Text(t) = &s.shape { Some(t) } else { None }) + .filter(|t| t.pos.x < full.min.x + RULER && t.pos.y > full.min.y + RULER) + .map(|t| t.pos.x + t.galley.size().x * 0.5) + .collect(); + assert!(centres.len() > 2, "the left ruler draws labels: {centres:?}"); + for c in ¢res { + assert!((c - (full.min.x + RULER * 0.5)).abs() < 0.01, "character centre {c} in {centres:?}"); + } +} + +#[test] +fn shift_snaps_the_zero_point_to_ruler_ticks_from_the_current_origin() { + assert_eq!(snap_px_to_tick(47.0, 0.0, 10.0), 50.0); + assert_eq!(snap_px_to_tick(44.9, 0.0, 10.0), 40.0); + // Ticks run from the current zero point, so they sit at 3 + 10k. + assert_eq!(snap_px_to_tick(47.0, 3.0, 10.0), 43.0); + assert_eq!(snap_px_to_tick(-8.0, 3.0, 10.0), -7.0); + // A degenerate tick never panics or moves the point. + for tick in [0.0, -1.0, f64::NAN, f64::INFINITY] { + assert_eq!(snap_px_to_tick(47.5, 3.0, tick), 47.5); + } +} + +/// Rulers alone in a 400×300 frame at 100 %, centred on a 256×128 document. +fn ruler_harness(app: PhotocraftApp) -> (egui_kittest::Harness<'static, PhotocraftApp>, Rect, ViewXform) { + let full = Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let h = egui_kittest::Harness::builder() + .with_size(vec2(400.0, 300.0)) + .with_step_dt(1.0 / 60.0) + .build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), app); + (h, full, xf) +} + +fn press(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, pos: Pos2, pressed: bool, modifiers: egui::Modifiers) { + h.event(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }); + h.run_steps(2); +} + +fn drag_corner_to(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, full: Rect, to: Pos2, modifiers: egui::Modifiers) { + let corner = Rect::from_min_size(full.min, vec2(RULER, RULER)).center(); + h.event(egui::Event::ModifiersChanged(modifiers)); + h.event(egui::Event::PointerMoved(corner)); + h.run_steps(1); + press(h, corner, true, modifiers); + for p in [corner + vec2(10.0, 10.0), to] { + h.event(egui::Event::PointerMoved(p)); + h.run_steps(2); + } + press(h, to, false, modifiers); + h.event(egui::Event::ModifiersChanged(egui::Modifiers::NONE)); + h.run_steps(1); +} + +fn origin(h: &egui_kittest::Harness<'static, PhotocraftApp>) -> [f64; 2] { + h.state().session.active().unwrap().doc.ruler_origin +} + +/// #2955: dragging out of the ruler corner sets the zero point (cross hairs while dragging), +/// the rulers then read 0 there, Shift snaps it to a tick, and double-clicking the corner +/// resets it. +#[test] +fn dragging_the_ruler_corner_sets_the_zero_point_and_double_click_resets_it() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + app.ui.extras.snap = false; + let (mut h, full, xf) = ruler_harness(app); + h.run_steps(2); + let target = xf.to_screen(100.0, 40.0); + drag_corner_to(&mut h, full, target, egui::Modifiers::NONE); + let o = origin(&h); + assert!((o[0] - 100.0).abs() < 0.01 && (o[1] - 40.0).abs() < 0.01, "zero point lands under the pointer: {o:?}"); + // The top ruler's "0" now sits at the new zero point, "100" (a distance, unsigned as in + // Photoshop) at the image's left edge. + h.run_steps(2); + let labels: Vec<(String, f32)> = h + .output() + .shapes + .iter() + .filter_map(|s| if let egui::Shape::Text(t) = &s.shape { Some((t.galley.text().to_string(), t.pos.x)) } else { None }) + .collect(); + let near = |text: &str, x: f32| labels.iter().any(|(t, lx)| t == text && (lx - (x + 2.0)).abs() < 1.0); + assert!(near("0", target.x), "a 0 label at the zero point: {labels:?}"); + assert!(near("100", xf.to_screen(0.0, 0.0).x), "100 at the image's left edge: {labels:?}"); + + // Mid-drag, cross hairs run through the pointer across the canvas. + let corner = Rect::from_min_size(full.min, vec2(RULER, RULER)).center(); + let mid = xf.to_screen(150.0, 90.0); + press(&mut h, corner, true, egui::Modifiers::NONE); + for p in [corner + vec2(10.0, 10.0), mid] { + h.event(egui::Event::PointerMoved(p)); + h.run_steps(2); + } + let crosshair = h.output().shapes.iter().any(|s| { + matches!(s.shape, egui::Shape::LineSegment { points: [a, b], .. } if (a.x - mid.x).abs() < 0.5 && (b.x - mid.x).abs() < 0.5 && (b.y - a.y).abs() > 200.0) + }); + assert!(crosshair, "a vertical cross-hair line through the pointer while dragging"); + assert_eq!(origin(&h), o, "the zero point moves on release only"); + press(&mut h, mid, false, egui::Modifiers::NONE); + assert_eq!(origin(&h), [150.0, 90.0]); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(origin(&h), o, "setting the zero point is undoable"); + + // A drag dropped back on the corner leaves it alone. + drag_corner_to(&mut h, full, full.min + vec2(4.0, 4.0), egui::Modifiers::NONE); + assert_eq!(origin(&h), o); + + // Shift: ticks are 10 px apart at 100 %, measured from the current zero point (100, 40). + drag_corner_to(&mut h, full, xf.to_screen(123.0, 57.0), egui::Modifiers::SHIFT); + assert_eq!(origin(&h), [120.0, 60.0]); + + for _ in 0..40 { + h.step(); + } + let corner = Rect::from_min_size(full.min, vec2(RULER, RULER)).center(); + h.event(egui::Event::PointerMoved(corner)); + h.step(); + for _ in 0..2 { + for pressed in [true, false] { + h.event(egui::Event::PointerButton { pos: corner, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }); + h.step(); + } + } + h.run_steps(2); + assert_eq!(origin(&h), [0.0, 0.0], "double-clicking the corner resets the zero point"); +} + +/// #2955: the Info panel's X/Y are measured from the rulers' zero point. +#[test] +fn info_panel_position_reads_from_the_zero_point() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.run("view.setRulerOrigin", json!({"x": 20, "y": 5})).unwrap(); + app.hover_doc = Some([50.5, 20.5]); + let mut h = egui_kittest::Harness::builder() + .with_size(vec2(260.0, 240.0)) + .build_ui_state(|ui, app| crate::panels::dock_body(app, ui, crate::dock::Group::Navigator, 2), app); + h.run_steps(2); + assert!(h.query_by_label("30").is_some() && h.query_by_label("15").is_some(), "X 30, Y 15 from the zero point at (20, 5)"); + assert!(h.query_by_label("50").is_none(), "not the raw image x"); +} + +/// #5728: dragging a guide shows its coordinate in the Info panel on one axis only. +#[test] +fn info_panel_shows_guide_drag_position_outside_canvas() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.hover_doc = None; + app.guide_drag = Some(GuideDrag { vertical: true, index: None, pos: 42.0, press: 42.0, over_ruler: false, flipped: false }); + let mut h = egui_kittest::Harness::builder() + .with_size(vec2(260.0, 240.0)) + .build_ui_state(|ui, app| crate::panels::dock_body(app, ui, crate::dock::Group::Navigator, 2), app); + h.run_steps(2); + assert!(h.query_by_label("42").is_some(), "vertical guide: X readout"); + h.state_mut().guide_drag = Some(GuideDrag { vertical: false, index: None, pos: 17.0, press: 17.0, over_ruler: false, flipped: false }); + h.run_steps(2); + assert!(h.query_by_label("17").is_some(), "horizontal guide: Y readout"); +} + +/// A dragged guide shows its position beside the pointer: X for a vertical guide, Y for a +/// horizontal one, from the zero point, in the ruler unit (pixels to one decimal). +#[test] +fn guide_drag_shows_its_position_beside_the_pointer() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + let v = GuideDrag { vertical: true, index: Some(0), pos: 103.24, press: 50.0, over_ruler: false, flipped: false }; + assert_eq!(guide_drag_readout(&app, v), Some(("X:", "103.2 px".to_owned()))); + let h = GuideDrag { vertical: false, index: None, pos: 40.0, press: 40.0, over_ruler: false, flipped: false }; + assert_eq!(guide_drag_readout(&app, h), Some(("Y:", "40 px".to_owned()))); + app.run("view.setRulerOrigin", json!({"x": 3.0, "y": 10.0})).unwrap(); + assert_eq!(guide_drag_readout(&app, h), Some(("Y:", "30 px".to_owned())), "from the zero point"); + app.run("view.resetRulerOrigin", json!({})).unwrap(); + app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": "percent"})).unwrap(); + assert_eq!( + guide_drag_readout(&app, GuideDrag { vertical: true, index: None, pos: 103.0, press: 0.0, over_ruler: false, flipped: false }).map(|r| r.0), + Some("X:") + ); + app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": "pixels"})).unwrap(); + + app.guide_drag = Some(v); + let mut h = egui_kittest::Harness::builder().with_size(vec2(300.0, 200.0)).build_ui_state(|ui, app| draw_guide_drag_readout(app, ui.ctx()), app); + h.event(egui::Event::PointerMoved(pos2(50.0, 50.0))); + h.run_steps(2); + assert!(h.query_by_label("X:").is_some() && h.query_by_label("103.2 px").is_some()); + h.state_mut().guide_drag = None; + h.run_steps(2); + assert!(h.query_by_label("X:").is_none(), "no readout once the drag ends"); +} + +type AppHarness = egui_kittest::Harness<'static, PhotocraftApp>; + +/// The real canvas on a 400 × 300 document with a horizontal guide at y = 100, at 100 %, +/// with the Move tool and snapping off. +fn canvas_harness(rulers: bool) -> AppHarness { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 100})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = rulers; + app.ui.extras.snap = false; + app.ui.tool = crate::state::Tool::Move; + let mut h = egui_kittest::Harness::builder().with_size(vec2(800.0, 600.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + let v = &mut h.state_mut().ui.views[0]; + (v.zoom, v.center, v.fit_pending) = (1.0, [200.0, 150.0], false); + h.run_steps(2); + h +} + +/// Screen point of document point `(x, y)` in [`canvas_harness`]. +fn canvas_at(h: &AppHarness, x: f32, y: f32) -> Pos2 { + let app = h.state(); + let v = &app.ui.views[0]; + let rect = content_rect(app, app.last_canvas_rect); + ViewXform { rect, zoom: v.zoom, center: v.center, flip: false, rotation: 0.0, aspect: 1.0 }.to_screen(x, y) +} + +/// Moving a horizontal guide with the Move tool on the real canvas: `Y:` follows the pointer. +#[test] +fn moving_a_guide_on_the_canvas_shows_its_y() { + use egui_kittest::kittest::Queryable; + let mut h = canvas_harness(false); + let at = canvas_at; + let press = at(&h, 200.0, 100.0); + h.event(egui::Event::PointerMoved(press)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: press, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + for y in [110.0, 125.0, 140.0] { + h.event(egui::Event::PointerMoved(at(&h, 210.0, y))); + h.run_steps(2); + } + assert!(h.state().guide_drag.is_some_and(|d| !d.vertical), "a horizontal guide is being dragged"); + assert!(h.query_by_label("Y:").is_some() && h.query_by_label("140 px").is_some(), "Y readout at the guide"); + assert!(h.query_by_label("X:").is_none()); + let up = at(&h, 210.0, 140.0); + h.event(egui::Event::PointerButton { pos: up, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + assert_eq!(h.state().session.active().unwrap().doc.guides.horizontal, vec![140.0]); + assert!(h.query_by_label("Y:").is_none(), "gone after the drop"); +} + +/// A guide dragged out of the top ruler: in each frame the readout shows where the guide is +/// after that frame's pointer move (the ruler handles the drag after the canvas is drawn), and +/// the frame that drops it shows none. +#[test] +fn ruler_drag_readout_is_current_every_frame() { + use egui_kittest::kittest::Queryable; + let mut h = canvas_harness(true); + let from = pos2(canvas_at(&h, 200.0, 0.0).x, h.state().last_canvas_rect.top() + RULER / 2.0); + h.event(egui::Event::PointerMoved(from)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + for y in [40.0, 60.0, 75.0] { + h.event(egui::Event::PointerMoved(canvas_at(&h, 200.0, y))); + // egui 0.37 lays an Area's contents out a pass after it is shown; step to the settled + // frame with the pointer still at `y` (readout included) rather than the first pass. + h.run_steps(2); + let pos = h.state().guide_drag.map(|d| d.pos); + assert_eq!(pos, Some(f64::from(y)), "the drag follows the pointer"); + assert!(h.query_by_label(&format!("{y} px")).is_some(), "readout at {y} in the same frame"); + } + let up = canvas_at(&h, 200.0, 75.0); + h.event(egui::Event::PointerButton { pos: up, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE }); + h.run_steps(2); + assert!(h.state().guide_drag.is_none()); + assert!(h.query_by_label("Y:").is_none(), "no readout in the frame that drops the guide"); + assert!(h.state().session.active().unwrap().doc.guides.horizontal.contains(&75.0), "the guide lands at y = 75"); +} + +#[test] +fn percentage_grid_spacing_uses_both_canvas_dimensions() { + let mut g = GuidesGridAndSlices { grid_unit: Unit::Percent, gridline_every: 20.0, subdivisions: 2, ..Default::default() }; + assert_eq!(grid_major_px(&g, 72.0, [1280.0, 720.0], 72.0), [256.0, 144.0]); + // Absolute units remain equal on both axes, irrespective of aspect ratio. + g.grid_unit = Unit::Pixels; + g.gridline_every = 100.0; + assert_eq!(grid_major_px(&g, 72.0, [1280.0, 720.0], 72.0), [100.0, 100.0]); +} + +#[test] +fn unit_steps_allow_fractions() { + assert_eq!(nice_step(0.3, false), 0.5); + assert_eq!(nice_step(0.2, false), 0.2); + assert_eq!(nice_step(0.22, false), 0.25); + assert_eq!(nice_step(0.3, true), 1.0); + assert_eq!(nice_step(23.0, true), 50.0); +} + +#[test] +fn ticks_follow_1_2_5_series() { + assert_eq!(tick_step(1.0), 100.0); + assert_eq!(tick_step(0.1), 1000.0); + assert_eq!(tick_step(4.0), 20.0); + assert_eq!(tick_step(64.0), 1.0); + assert_eq!(tick_step(0.25), 500.0); +} + +#[test] +fn hidden_guides_are_not_hit_tested() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.sync_views(); + app.session.execute("view.newGuide", json!({"orientation": "vertical", "position": 50})).unwrap(); + app.session.execute("view.newGuide", json!({"orientation": "horizontal", "position": 25})).unwrap(); + let positions = app.session.active().unwrap().doc.guides.clone(); + + // Visible, unlocked guides remain interactive in either orientation. + assert_eq!(guide_at(&app, 50.0, 10.0), Some((true, 0))); + assert_eq!(guide_at(&app, 10.0, 25.0), Some((false, 0))); + + // All ways to hide the canvas guides must also disable hit testing. + app.ui.extras.guides = false; + assert_eq!(guide_at(&app, 50.0, 10.0), None); + assert_eq!(guide_at(&app, 10.0, 25.0), None); + app.ui.extras.guides = true; + + app.ui.view.show.canvas_guides = false; + assert_eq!(guide_at(&app, 50.0, 10.0), None); + assert_eq!(guide_at(&app, 10.0, 25.0), None); + app.ui.view.show.canvas_guides = true; + + app.ui.view.extras = false; + assert_eq!(guide_at(&app, 50.0, 10.0), None); + assert_eq!(guide_at(&app, 10.0, 25.0), None); + app.ui.view.extras = true; + + // Hiding/showing must not change the stored guide positions. + assert_eq!(app.session.active().unwrap().doc.guides, positions); + assert_eq!(guide_at(&app, 50.0, 10.0), Some((true, 0))); + assert_eq!(guide_at(&app, 10.0, 25.0), Some((false, 0))); + + app.ui.extras.lock_guides = true; + assert_eq!(guide_at(&app, 50.0, 10.0), None); + assert_eq!(guide_at(&app, 10.0, 25.0), None); +} + +#[test] +fn unit_menu_lists_every_unit_with_the_current_one_checked() { + let items = unit_menu_items(Unit::Inches, &UNITS); + assert_eq!(items.len(), Unit::NAMES.len()); + // Every unit is offered, labelled, and in the preference's order. + assert_eq!(items[0], ("Pixels".to_string(), "pixels", false)); + assert_eq!(items.iter().find(|(_, name, _)| *name == "cm").unwrap().0, "Centimeters"); + // Exactly the current unit is checked. + let checked: Vec<_> = items.iter().filter(|(_, _, c)| *c).map(|(_, name, _)| *name).collect(); + assert_eq!(checked, vec!["inches"]); +} + +#[test] +fn picking_a_ruler_unit_changes_the_preference() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + assert_eq!(app.session.prefs().units_and_rulers.rulers, Unit::Pixels); + // The context menu applies its pick exactly as the preference pane does. + app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": "cm"})).unwrap(); + assert_eq!(app.session.prefs().units_and_rulers.rulers, Unit::Centimeters); +} + +/// View › Rulers is remembered: the next launch, and every document created or opened in it, +/// shows the rulers as last set, on or off (#2918). +#[test] +fn rulers_open_as_last_set_after_a_restart() { + use std::{cell::RefCell, rc::Rc}; + let saved = Rc::new(RefCell::new(None::)); + let launch = |saved: &Rc>>| { + let (load, store) = (saved.clone(), saved.clone()); + PhotocraftApp::new( + photocraft_engine::Session::new(), + crate::Services { + load_prefs: Some(Box::new(move || load.borrow().clone())), + save_prefs: Some(Box::new(move |text| { + *store.borrow_mut() = Some(text.to_string()); + Ok(()) + })), + ..Default::default() + }, + ) + }; + for on in [true, false] { + let mut app = launch(&saved); + assert_eq!(crate::menus::invoke(&mut app, &egui::Context::default(), "view.rulers", json!({})).unwrap(), json!(on)); + crate::prefs_ui::save_preferences(&mut app).unwrap(); + let mut next = launch(&saved); + next.session.execute("file.new", json!({"width": 20, "height": 10})).unwrap(); + next.sync_views(); + assert_eq!(next.ui.extras.rulers, on, "a new document after a restart shows the rulers as last set"); + } +} + +#[test] +fn guide_drags_create_move_and_delete() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.sync_views(); + finish_drag(&mut app, GuideDrag { vertical: true, index: None, pos: 50.0, press: 50.0, over_ruler: false, flipped: false }); + finish_drag(&mut app, GuideDrag { vertical: false, index: None, pos: 500.0, press: 500.0, over_ruler: false, flipped: false }); // off canvas: ignored + assert_eq!(app.session.active().unwrap().doc.guides.vertical, vec![50.0]); + assert!(app.session.active().unwrap().doc.guides.horizontal.is_empty()); + assert_eq!(guide_at(&app, 51.0, 10.0), Some((true, 0))); + finish_drag(&mut app, GuideDrag { vertical: true, index: Some(0), pos: 120.0, press: 50.0, over_ruler: false, flipped: false }); + assert_eq!(app.session.active().unwrap().doc.guides.vertical, vec![120.0]); + finish_drag(&mut app, GuideDrag { vertical: true, index: Some(0), pos: -30.0, press: 120.0, over_ruler: false, flipped: false }); + assert!(app.session.active().unwrap().doc.guides.vertical.is_empty()); + app.ui.extras.lock_guides = true; + assert_eq!(guide_at(&app, 0.0, 0.0), None); +} + +/// ⌘ is the Move tool with any tool, so a ⌘-drag over a guide moves the guide, never the layer +/// (#2690); with Lock Guides a ⌘-drag with a marquee moves the layer as before. +#[test] +fn command_drag_over_a_guide_moves_the_guide_with_any_tool() { + use crate::canvas::{ToolEvent, tool_event}; + use crate::state::Tool; + for (tool, locked) in [ + (Tool::Brush, false), + (Tool::Eraser, false), + (Tool::Gradient, false), + (Tool::Frame, false), + (Tool::Artboard, false), + (Tool::RectMarquee, false), + (Tool::Lasso, false), + (Tool::MagicWand, false), + (Tool::RectMarquee, true), + ] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60, "background": "transparent"})).unwrap(); + app.sync_views(); + app.ui.tool = tool; + app.ui.extras.lock_guides = locked; + let layer = app.session.active().unwrap().active_layer.unwrap(); + app.session + .edit("paint", |doc, _| { + doc.layer_mut(layer).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 50})).unwrap(); + let cmd = egui::Modifiers::COMMAND; + tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 40.0, pressure: 1.0 }, cmd); + tool_event(&mut app, ToolEvent::Move { x: 65.0, y: 40.0, pressure: 1.0 }, cmd); + tool_event(&mut app, ToolEvent::Up { x: 65.0, y: 40.0 }, cmd); + let doc = &app.session.active().unwrap().doc; + let moved = doc.layer(layer).unwrap().surface().unwrap().rgba(12, 12)[3] == 0.0; + if locked { + assert_eq!((doc.guides.vertical.clone(), moved), (vec![50.0], true), "{tool:?}, guides locked: the layer moves"); + } else { + assert_eq!((doc.guides.vertical.clone(), moved), (vec![65.0], false), "{tool:?}: the guide moves, the layer stays"); + } + } +} + +/// #3233: a rotated view keeps the rulers level and fully ticked at zoom scale. The top ruler +/// spans the same number of document pixels as the unrotated view, the ticks map back onto +/// its ends, and both rulers read 0 at the screen position of the (rotated) document origin, +/// as Photoshop does. +#[test] +fn rotated_view_keeps_full_rulers() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let (a, b) = (rect.left_top(), rect.right_top()); + for rotation in [0.0f32, 30.0, 45.0, 90.0, -119.0, 180.0] { + for flip in [false, true] { + let xf = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip, rotation, aspect: 1.0 }; + let (u0, u1) = ruler_range(&xf, a, b, 0, 1.0, [0.0; 2]); + assert!((u1 - u0 - 1600.0).abs() < 1e-2, "rotation {rotation}: top ruler covers {u0}..{u1}"); + let (first, last) = if flip { (u1, u0) } else { (u0, u1) }; + assert!((ruler_pos(&xf, false, first, 1.0, [0.0; 2]) - a.x).abs() < 1e-2, "rotation {rotation}: first tick"); + assert!((ruler_pos(&xf, false, last, 1.0, [0.0; 2]) - b.x).abs() < 1e-2, "rotation {rotation}: last tick"); + // 0 at the projected origin, 500 document px = 500 × zoom screen px further on. + let origin = xf.to_screen(0.0, 0.0); + assert!((ruler_pos(&xf, false, 0.0, 1.0, [0.0; 2]) - origin.x).abs() < 1e-2, "rotation {rotation}: top ruler 0"); + assert!((ruler_pos(&xf, true, 0.0, 1.0, [0.0; 2]) - origin.y).abs() < 1e-2, "rotation {rotation}: left ruler 0"); + let dir = if flip { -1.0 } else { 1.0 }; + assert!((ruler_pos(&xf, false, 500.0, 1.0, [0.0; 2]) - origin.x - dir * 250.0).abs() < 1e-2, "rotation {rotation}: top scale"); + assert!((ruler_pos(&xf, true, 500.0, 1.0, [0.0; 2]) - origin.y - 250.0).abs() < 1e-2, "rotation {rotation}: left scale"); + } + } + // Distances left of / above the origin read without a minus sign, as in Photoshop. + let xf = ViewXform { rect, zoom: 0.5, center: [0.0, 0.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let (u0, u1) = ruler_range(&xf, a, b, 0, 1.0, [0.0; 2]); + assert!(u0 < -500.0 && u1 > 500.0, "the view spans the origin: {u0}..{u1}"); + for (step, whole) in [(100.0, true), (100.0, false), (0.25, false)] { + let mut v = (u0 / step).floor() * step; + while v <= u1 { + let s = ruler_label(v, step, whole); + assert!(!s.contains('-'), "tick {v} (step {step}) is labelled {s:?}"); + v += step; + } + } + assert_eq!(ruler_label(-500.0, 100.0, true), "500"); + assert_eq!(ruler_label(-0.25, 0.25, false), "0.25"); +} + +/// #2955 with #3233: a zero point moved off the image's top-left reads 0 where the canvas draws +/// it, also on a rotated or flipped view, and the ruler keeps its full length and scale. +#[test] +fn rulers_count_from_the_zero_point_on_a_rotated_view() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let (a, b) = (rect.left_top(), rect.right_top()); + let zero = [300.0, 120.0]; + for rotation in [0.0f32, 30.0, 90.0, -119.0] { + for flip in [false, true] { + let xf = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip, rotation, aspect: 1.0 }; + let at = xf.to_screen(zero[0] as f32, zero[1] as f32); + assert!((ruler_pos(&xf, false, 0.0, 1.0, zero) - at.x).abs() < 1e-2, "rotation {rotation}: top ruler 0"); + assert!((ruler_pos(&xf, true, 0.0, 1.0, zero) - at.y).abs() < 1e-2, "rotation {rotation}: left ruler 0"); + let (u0, u1) = ruler_range(&xf, a, b, 0, 1.0, zero); + assert!((u1 - u0 - 1600.0).abs() < 1e-2, "rotation {rotation}: top ruler covers {u0}..{u1}"); + let dir = if flip { -1.0 } else { 1.0 }; + assert!((ruler_pos(&xf, false, 500.0, 1.0, zero) - at.x - dir * 250.0).abs() < 1e-2, "rotation {rotation}: top scale"); + } + } + // Unrotated, ruler value u sits over document x = zero + u. + let xf = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip: false, rotation: 0.0, aspect: 1.0 }; + assert!((ruler_pos(&xf, false, -300.0, 1.0, zero) - xf.to_screen(0.0, 0.0).x).abs() < 1e-2); + assert!((ruler_pos(&xf, true, 80.0, 1.0, zero) - xf.to_screen(0.0, 200.0).y).abs() < 1e-2); +} + +/// #3277: guides belong to the document, so a rotated view draws them along the document +/// line; unrotated they still span the whole clip rect. +#[test] +fn guides_turn_with_a_rotated_view() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let level = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let [a, b] = guide_segment(&level, rect, true, 500.0); + assert!((a.x - b.x).abs() < 1e-3 && (a.y - rect.top()).abs() < 1e-3 && (b.y - rect.bottom()).abs() < 1e-3, "{a:?} {b:?}"); + for rotation in [30.0f32, 90.0, -119.0] { + let xf = ViewXform { rotation, ..level }; + for (vertical, pos) in [(true, 500.0), (false, 900.0)] { + for end in guide_segment(&xf, rect, vertical, pos) { + let d = xf.to_doc(end); + let on_line = if vertical { d[0] } else { d[1] }; + assert!((on_line - pos).abs() < 1e-2, "rotation {rotation}: {end:?} maps to {d:?}, off the guide at {pos}"); + } + } + } +} + +/// The cursor over a guide turns with the view as the guide does: on a view turned 90°, a +/// vertical guide shows as a horizontal line and moves up and down. +#[test] +fn guide_cursors_turn_with_a_rotated_view() { + use egui::CursorIcon::{ResizeHorizontal as H, ResizeNeSw as NeSw, ResizeNwSe as NwSe, ResizeVertical as V}; + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let level = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip: false, rotation: 0.0, aspect: 1.0 }; + // View rotation, flip → the cursor over a vertical guide, over a horizontal guide. + for (rotation, flip, want) in [(0.0, false, [H, V]), (90.0, false, [V, H]), (45.0, false, [NwSe, NeSw]), (0.0, true, [H, V])] { + let xf = ViewXform { rotation, flip, ..level }; + assert_eq!([guide_cursor(&xf, true), guide_cursor(&xf, false)], want, "view turned {rotation}°, flip {flip}"); + } +} + +#[test] +fn ruler_range_is_normalised_when_flipped() { + // #1814: with View > Flip Horizontal, `to_doc` negates x, so the raw + // (d0, d1) comes out reversed. ruler_range must normalise to (min, max) + // so the tick loop still runs. Exercises the production helper. + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let top = Rect::from_min_size(rect.min, vec2(rect.width(), RULER)); + for flip in [false, true] { + let xf = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip, rotation: 0.0, aspect: 1.0 }; + let (u0, u1) = ruler_range(&xf, top.left_top(), top.right_top(), 0, 1.0, [0.0; 2]); + assert!(u0 <= u1, "flip={flip}: u0 ({u0}) must be <= u1 ({u1})"); + assert!(u1 - u0 > 0.0, "flip={flip}: range is empty"); + } +} + +/// #5698: as in Photoshop, an Alt-drag out of the top ruler makes a vertical guide and one out +/// of the left ruler a horizontal guide; without Alt each ruler makes its own kind. +#[test] +fn alt_drags_a_guide_of_the_other_orientation_out_of_a_ruler() { + let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let drag = |from: Pos2, modifiers: egui::Modifiers| { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)).build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), app); + let to = xf.to_screen(37.0, 90.0); + h.event_modifiers(egui::Event::PointerMoved(from), modifiers); + h.event_modifiers(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers }, modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerMoved(to), modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers }, modifiers); + h.run_steps(2); + let g = &h.state().session.active().unwrap().doc.guides; + (g.vertical.clone(), g.horizontal.clone()) + }; + let (top, left) = (pos2(200.0, RULER / 2.0), pos2(RULER / 2.0, 150.0)); + assert_eq!(drag(top, egui::Modifiers::NONE), (vec![], vec![90.0]), "top ruler: horizontal"); + assert_eq!(drag(left, egui::Modifiers::NONE), (vec![37.0], vec![]), "left ruler: vertical"); + assert_eq!(drag(top, egui::Modifiers::ALT), (vec![37.0], vec![]), "Alt from the top ruler: vertical"); + assert_eq!(drag(left, egui::Modifiers::ALT), (vec![], vec![90.0]), "Alt from the left ruler: horizontal"); +} + +/// #6286: a guide let go over a ruler is removed, whichever ruler it is. The top ruler measures +/// document x and the left one y, so a horizontal guide released on the left ruler is still +/// inside the document on its own axis, and used to be placed instead of discarded. +#[test] +fn a_guide_dropped_on_either_ruler_is_removed() { + let new_app = || { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + app.ui.extras.rulers = true; + // No snapping, so a guide that is kept lands exactly where it is let go. + app.ui.extras.snap = false; + app + }; + let drag = |from: Pos2, to: Pos2| { + let (mut h, _, _) = ruler_harness(new_app()); + h.event(egui::Event::PointerMoved(from)); + press(&mut h, from, true, egui::Modifiers::NONE); + h.event(egui::Event::PointerMoved(to)); + h.run_steps(1); + press(&mut h, to, false, egui::Modifiers::NONE); + let g = &h.state().session.active().unwrap().doc.guides; + (g.vertical.clone(), g.horizontal.clone()) + }; + // #5698: out of the top ruler is a horizontal guide, out of the left one a vertical guide. + let (top, left) = (pos2(200.0, RULER / 2.0), pos2(RULER / 2.0, 150.0)); + assert_eq!(drag(top, left), (vec![], vec![]), "out of the top ruler, released on the left ruler"); + assert_eq!(drag(left, top), (vec![], vec![]), "out of the left ruler, released on the top ruler"); + // Released on the canvas the guide lands where it was let go, as before. + assert_eq!(drag(top, pos2(200.0, 150.0)), (vec![], vec![56.0]), "released on the canvas"); + assert_eq!(drag(left, pos2(120.0, 150.0)), (vec![40.0], vec![]), "released on the canvas"); +} + +/// #6286: the guide is removed when it is let go over a ruler, and moved when it is not. The +/// release can be inside the document on the guide's own axis and still be a ruler drop, so the +/// two are told apart by where the pointer is, not by the position it reads. +#[test] +fn a_guide_let_go_on_a_ruler_is_removed_not_moved() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 100})).unwrap(); + app.sync_views(); + let horizontal = |app: &PhotocraftApp| app.session.active().unwrap().doc.guides.horizontal.clone(); + for drag in [ + GuideDrag { vertical: false, index: Some(0), pos: 50.0, press: 10.0, over_ruler: true, flipped: false }, + // Dragged sideways onto the left ruler, a horizontal guide holds its y, which on its own + // reads as a click: the ruler still removes it. + GuideDrag { vertical: false, index: Some(0), pos: 50.0, press: 50.0, over_ruler: true, flipped: false }, + ] { + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 50})).unwrap(); + finish_drag(&mut app, drag); + assert!(horizontal(&app).is_empty(), "let go on a ruler: removed, {drag:?}"); + } + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 50})).unwrap(); + finish_drag(&mut app, GuideDrag { vertical: false, index: Some(0), pos: 80.0, press: 10.0, over_ruler: false, flipped: false }); + assert_eq!(horizontal(&app), vec![80.0], "let go on the canvas: moved"); + // Clicked where it was pressed, on the canvas: selected and kept, as before. + finish_drag(&mut app, GuideDrag { vertical: false, index: Some(0), pos: 80.0, press: 80.0, over_ruler: false, flipped: false }); + assert_eq!(horizontal(&app), vec![80.0], "clicked on the canvas: kept"); + assert!(app.ui.selected_guide.is_some(), "clicked on the canvas: selected"); +} + +/// The ruler bands are the strips the rulers take off the canvas rect, and only while the rulers +/// are shown: with them hidden the whole canvas rect is canvas (#6286). +#[test] +fn only_the_ruler_bands_count_as_a_ruler() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let full = Rect::from_min_size(pos2(16.0, 42.0), vec2(968.0, 642.0)); + let content = Rect::from_min_size(pos2(32.0, 58.0), vec2(952.0, 626.0)); + app.ui.extras.rulers = true; + assert!(dropped_on_a_ruler(&app, full, pos2(full.min.x + RULER / 2.0, 371.0)), "the left ruler"); + assert!(dropped_on_a_ruler(&app, full, pos2(508.0, full.min.y + RULER / 2.0)), "the top ruler"); + assert!(dropped_on_a_ruler(&app, full, full.min), "the ruler corner"); + assert!(!dropped_on_a_ruler(&app, full, pos2(508.0, 371.0)), "the canvas"); + assert!(!dropped_on_a_ruler(&app, full, content.min), "the canvas' own top-left corner"); + assert!(!dropped_on_a_ruler(&app, full, pos2(content.min.x + 0.5, 371.0)), "the first canvas column"); + assert!(!dropped_on_a_ruler(&app, full, pos2(508.0, content.min.y + 0.5)), "the first canvas row"); + assert!(!dropped_on_a_ruler(&app, full, pos2(full.min.x - 1.0, 371.0)), "outside the canvas rect"); + app.ui.extras.rulers = false; + for p in [pos2(full.min.x + RULER / 2.0, 371.0), pos2(508.0, full.min.y + RULER / 2.0), full.min] { + assert!(!dropped_on_a_ruler(&app, full, p), "rulers hidden: no band at {p:?}"); + } +} + +/// The canvas the guides live on, rulers on, at 100 %. +fn tool_canvas_harness(tool: crate::state::Tool) -> egui_kittest::Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = true; + app.ui.extras.snap = false; + app.ui.tool = tool; + let mut h = egui_kittest::Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + let ctx = ui.ctx().clone(); + if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::shortcuts::handle(app, &ctx); + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + let v = &mut h.state_mut().ui.views[0]; + v.zoom = 1.0; + v.center = [200.0, 150.0]; + v.fit_pending = false; + h.run_steps(2); + h +} + +/// #6286: an existing guide dragged onto a ruler with the Move tool is deleted, though the +/// release is inside the document on the guide's own axis: the left ruler measures y, so a +/// horizontal guide let go there still reads a y inside the canvas and used to be moved. Held +/// level, that release reads as a click on the guide, which a ruler drop also outranks. +#[test] +fn a_guide_dragged_onto_a_ruler_is_deleted_through_the_canvas() { + for below in [50.0f32, 0.0] { + let mut h = tool_canvas_harness(crate::state::Tool::Move); + h.state_mut().run("view.newGuide", json!({"orientation": "horizontal", "position": 150})).unwrap(); + h.run_steps(1); + let at = |h: &egui_kittest::Harness<'static, PhotocraftApp>, x: f32, y: f32| { + let app = h.state(); + let v = &app.ui.views[0]; + let xf = ViewXform { + rect: content_rect(app, app.last_canvas_rect), + zoom: v.zoom, + center: v.center, + flip: app.ui.view.flip_horizontal, + rotation: v.rotation, + aspect: app.ui.view.display_aspect(), + }; + xf.to_screen(x, y) + }; + let on_the_guide = at(&h, 200.0, 150.0); + // In the left ruler's band, `below` document px under the press: inside the canvas on the + // guide's own axis, which is where the old code moved the guide instead of removing it. + let canvas = h.state().last_canvas_rect; + let onto_the_left_ruler = pos2(canvas.min.x + RULER / 2.0, on_the_guide.y + below); + // Along the guide, so a held-level release keeps its y, the way a sideways drag does. + let dragged = pos2(on_the_guide.x - 12.0, on_the_guide.y); + h.event(egui::Event::PointerMoved(on_the_guide)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: on_the_guide, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE }); + h.run_steps(1); + // Past egui's click distance, so the drag starts at the press, on the guide. + h.event(egui::Event::PointerMoved(dragged)); + h.run_steps(2); + assert!(h.state().guide_drag.is_some(), "the Move tool takes the guide"); + h.event(egui::Event::PointerMoved(onto_the_left_ruler)); + h.run_steps(1); + h.event(egui::Event::PointerButton { + pos: onto_the_left_ruler, + button: egui::PointerButton::Primary, + pressed: false, + modifiers: egui::Modifiers::NONE, + }); + h.run_steps(2); + let g = &h.state().session.active().unwrap().doc.guides; + assert!(g.horizontal.is_empty(), "{below} px below the press, released on the left ruler: got {:?}", g.horizontal); + assert!(h.state().guide_drag.is_none(), "the drag is over"); + } +} + +/// #3770: a guide Shift-dragged out of a ruler lands on the nearest tick the ruler draws +/// (tenths of the labelled step, in the ruler unit); without Shift it lands where it's dropped. +#[test] +fn shift_dragged_guides_land_on_ruler_ticks() { + let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let new_app = || { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + app + }; + let drag_guide = |modifiers: egui::Modifiers| { + let builder = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)); + let mut h = builder.build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), new_app()); + // Out of the left ruler: a vertical guide, dropped at document x = 37. + let (from, to) = (pos2(RULER / 2.0, 100.0), xf.to_screen(37.0, 64.0)); + h.event_modifiers(egui::Event::PointerMoved(from), modifiers); + h.event_modifiers(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers }, modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerMoved(to), modifiers); + h.run_steps(1); + h.event_modifiers(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers }, modifiers); + h.run_steps(2); + h.state().session.active().unwrap().doc.guides.vertical.clone() + }; + assert_eq!(drag_guide(egui::Modifiers::NONE), vec![37.0]); + // #5698: Alt turns it into a horizontal guide (at the drop's y, the document's centre). + assert_eq!(drag_guide(egui::Modifiers::ALT), Vec::::new(), "no vertical guide with Alt"); + // Pixels at 100%: labels every 100 px, ticks every 10 px. + assert_eq!(drag_guide(egui::Modifiers::SHIFT), vec![40.0]); + + let mut app = new_app(); + // At 200% the labels are every 50 px, so the ticks every 5 px. + assert_eq!(snap_to_tick(&app, &ViewXform { zoom: 2.0, ..xf }, true, 37.0), 35.0); + // Percent of each side: ticks every 2.5% of 256 px across, every 5% of 128 px down. + app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": "percent"})).unwrap(); + assert!((snap_to_tick(&app, &xf, true, 37.0) - 38.4).abs() < 1e-9); + assert!((snap_to_tick(&app, &xf, false, 37.0) - 38.4).abs() < 1e-9); + // Ticks count from the zero point (#2955): a vertical guide follows the x origin only. + let mut app = new_app(); + app.run("view.setRulerOrigin", json!({"x": 3, "y": 0})).unwrap(); + assert_eq!(snap_to_tick(&app, &xf, true, 37.0), 33.0); + assert_eq!(snap_to_tick(&app, &xf, false, 37.0), 40.0); +} + +/// #5034: ⌥ turns a guide dragged out of a ruler, as in Photoshop: out of the left ruler it +/// comes out horizontal, at the pointer's y. +#[test] +fn alt_turns_a_guide_dragged_out_of_a_ruler() { + let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)).build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), app); + // ⌥ held for the whole drag (`event_modifiers` would let go of it after each event). + let alt = egui::Modifiers::ALT; + h.event(egui::Event::ModifiersChanged(alt)); + let (from, to) = (pos2(RULER / 2.0, 100.0), xf.to_screen(37.0, 51.0)); + h.event(egui::Event::PointerMoved(from)); + h.event(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers: alt }); + h.run_steps(1); + h.event(egui::Event::PointerMoved(to)); + h.run_steps(1); + assert!(h.state().guide_drag.is_some_and(|d| !d.shows_vertical()), "drawn horizontal while ⌥ is held"); + h.event(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers: alt }); + h.run_steps(2); + let g = &h.state().session.active().unwrap().doc.guides; + assert_eq!((g.vertical.clone(), g.horizontal.clone()), (vec![], vec![51.0])); +} + +/// #5034: with the Move tool, ⌥ while dragging a guide turns it about the pointer, in one +/// undo step; letting go of ⌥ before the release turns it back. An ⌥-click turns it in place. +#[test] +fn alt_turns_a_guide_moved_with_the_move_tool() { + use crate::canvas::{ToolEvent, tool_event}; + let new_app = || { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + app.sync_views(); + app.ui.tool = crate::state::Tool::Move; + app.run("view.newGuide", json!({"orientation": "vertical", "position": 50})).unwrap(); + app + }; + let guides = |app: &PhotocraftApp| { + let g = &app.session.active().unwrap().doc.guides; + (g.vertical.clone(), g.horizontal.clone()) + }; + let (none, alt) = (egui::Modifiers::NONE, egui::Modifiers::ALT); + + let mut app = new_app(); + tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 44.0, pressure: 1.0 }, none); + tool_event(&mut app, ToolEvent::Move { x: 63.0, y: 17.0, pressure: 1.0 }, alt); + assert!(app.guide_drag.is_some_and(|d| !d.shows_vertical() && d.pos == 17.0)); + tool_event(&mut app, ToolEvent::Up { x: 63.0, y: 17.0 }, alt); + assert_eq!(guides(&app), (vec![], vec![17.0])); + assert!(app.session.undo()); + assert_eq!(guides(&app), (vec![50.0], vec![]), "one undo step"); + + // ⌥ let go before the release: still vertical, moved along x. + let mut app = new_app(); + tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 44.0, pressure: 1.0 }, none); + tool_event(&mut app, ToolEvent::Move { x: 63.0, y: 17.0, pressure: 1.0 }, alt); + tool_event(&mut app, ToolEvent::Up { x: 63.0, y: 17.0 }, none); + assert_eq!(guides(&app), (vec![63.0], vec![])); + + // An ⌥-click turns the guide where it was clicked. + let mut app = new_app(); + tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 44.0, pressure: 1.0 }, alt); + tool_event(&mut app, ToolEvent::Up { x: 50.0, y: 44.0 }, alt); + assert_eq!(guides(&app), (vec![], vec![44.0])); +} + +/// #5034: a guide dropped on either ruler is deleted, whichever way it runs; a horizontal +/// guide over the left ruler is still level with the image, so this needs the pointer. +#[test] +fn a_guide_dropped_on_either_ruler_is_deleted() { + let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max); + let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 }; + for (pointer, over) in [(pos2(RULER / 2.0, 150.0), true), (pos2(200.0, RULER / 2.0), true), (pos2(200.0, 150.0), false)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 256, "height": 128})).unwrap(); + app.ui.extras.rulers = true; + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 30})).unwrap(); + app.guide_drag = Some(GuideDrag { vertical: false, index: Some(0), pos: 64.0, ..Default::default() }); + let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)).build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), app); + h.event(egui::Event::PointerMoved(pointer)); + h.run_steps(1); + let mut d = h.state().guide_drag.unwrap(); + // The canvas release owns pointer classification in the current routing. + d.over_ruler = dropped_on_a_ruler(h.state(), full, pointer); + assert_eq!(d.over_ruler, over, "{pointer:?}"); + let app = h.state_mut(); + app.guide_drag = None; + finish_drag(app, d); + let left = app.session.active().unwrap().doc.guides.horizontal.clone(); + assert_eq!(left, if over { vec![] } else { vec![64.0] }, "{pointer:?}"); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().doc.guides.horizontal, vec![30.0], "one Undo restores the guide"); + } +} diff --git a/crates/ui-egui/src/scrollbars.rs b/crates/ui-egui/src/canvas/scrollbars.rs similarity index 97% rename from crates/ui-egui/src/scrollbars.rs rename to crates/ui-egui/src/canvas/scrollbars.rs index e0867436c4..375edc13b6 100644 --- a/crates/ui-egui/src/scrollbars.rs +++ b/crates/ui-egui/src/canvas/scrollbars.rs @@ -315,7 +315,18 @@ mod tests { assert_eq!(clamp_axis(-400.0, 1000.0, 500.0, 0.2), -250.0, "the image's right edge on the view's"); assert_eq!(clamp_axis(1400.0, 1000.0, 500.0, 0.2), 1250.0, "its left edge on the view's"); assert_eq!(clamp_axis(500.0, 1000.0, 1000.0, 1.0), 500.0, "exactly the view's size: centred"); - let mut v = View { zoom: 1.0, center: [-500.0, 20.0], fit_pending: false, fill_pending: false, doc_size: [1000, 800], rotation: 0.0 }; + let mut v = View { + zoom: 1.0, + center: [-500.0, 20.0], + fit_pending: false, + fill_pending: false, + doc_size: [1000, 800], + rotation: 0.0, + zoom_animating: false, + zoom_anchor: None, + display_camera: None, + zoom_transition: None, + }; assert!(clamp_view(&mut v, vec2(500.0, 400.0), 1.0)); assert_eq!(v.center, [250.0, 200.0]); assert!(!clamp_view(&mut v, vec2(500.0, 400.0), 1.0), "stable: no repaint loop"); @@ -334,7 +345,18 @@ mod tests { let (at, len) = s.thumb((0.0, 1000.0), 0.0); assert!(at.is_finite() && len.is_finite()); assert_eq!(s.doc_per_point((0.0, 1000.0), 0.0), 0.0); - let mut v = View { zoom: 1.0, center: [0.0, 0.0], fit_pending: false, fill_pending: false, doc_size: [0, 0], rotation: 0.0 }; + let mut v = View { + zoom: 1.0, + center: [0.0, 0.0], + fit_pending: false, + fill_pending: false, + doc_size: [0, 0], + rotation: 0.0, + zoom_animating: false, + zoom_anchor: None, + display_camera: None, + zoom_transition: None, + }; assert!(!clamp_view(&mut v, vec2(500.0, 400.0), 1.0)); } diff --git a/crates/ui-egui/src/select_mask_view.rs b/crates/ui-egui/src/canvas/select_mask_view.rs similarity index 89% rename from crates/ui-egui/src/select_mask_view.rs rename to crates/ui-egui/src/canvas/select_mask_view.rs index 9f5440c4aa..b4962d512d 100644 --- a/crates/ui-egui/src/select_mask_view.rs +++ b/crates/ui-egui/src/canvas/select_mask_view.rs @@ -57,7 +57,7 @@ impl Mode { Mode::ALL.into_iter().find(|m| m.id() == id) } - fn label(self) -> &'static str { + pub(crate) fn label(self) -> &'static str { match self { Mode::OnionSkin => tl!("Onion Skin"), Mode::MarchingAnts => tl!("Marching Ants"), @@ -70,7 +70,7 @@ impl Mode { } /// The single-letter key that picks this view. - fn key(self) -> egui::Key { + pub(crate) fn key(self) -> egui::Key { match self { Mode::OnionSkin => egui::Key::O, Mode::MarchingAnts => egui::Key::M, @@ -83,7 +83,7 @@ impl Mode { } /// The view's opacity (percent) until it is changed; `None` for views without one. - fn default_opacity(self) -> Option { + pub(crate) fn default_opacity(self) -> Option { match self { Mode::OnionSkin | Mode::Overlay => Some(50.0), Mode::OnBlack | Mode::OnWhite => Some(100.0), @@ -91,7 +91,7 @@ impl Mode { } } - fn next(self) -> Mode { + pub(crate) fn next(self) -> Mode { let i = Mode::ALL.iter().position(|m| *m == self).unwrap_or(0); Mode::ALL.get(i + 1).copied().unwrap_or(Mode::OnionSkin) } @@ -183,7 +183,7 @@ pub fn controls(ui: &mut egui::Ui, fields: &mut Map) { ui.horizontal(|ui| { ui.label(egui::RichText::new(tl!("View")).color(t.text_dim)); let options: Vec<(Mode, &str)> = Mode::ALL.iter().map(|m| (*m, m.label())).collect(); - crate::widgets::dropdown(ui, "select-mask-view", &mut current, &options, 170.0); + crate::widgets::dropdown(ui, "select-mask-view", tl!("View"), &mut current, &options, 170.0); }); fields.insert("__view".into(), json!(current.id())); if current.default_opacity().is_some() { @@ -197,12 +197,25 @@ pub fn controls(ui: &mut egui::Ui, fields: &mut Map) { fields.insert(field.into(), json!(on)); } ui.label(egui::RichText::new(tl!("F cycles the views; hold X to see the image")).color(t.text_dim).small()); + // Photoshop's Remember Settings, off by default: the next Select and Mask starts from these + // settings instead of the defaults (#9423). + let mut remember = remembers(fields); + crate::widgets::checkbox(ui, &mut remember, tl!("Remember Settings")); + fields.insert(REMEMBER.into(), json!(remember)); ui.add_space(4.0); crate::widgets::hairline(ui); ui.add_space(6.0); brush_controls(ui, fields); } +/// The dialog field behind Remember Settings. +pub const REMEMBER: &str = "__remember"; + +/// Whether Remember Settings is on in `fields` (a dialog's, or the remembered ones). +pub fn remembers(fields: &Map) -> bool { + flag(fields, REMEMBER) +} + /// The Refine Edge Brush (R): painting on the canvas sets where edge refinement happens. fn brush_controls(ui: &mut egui::Ui, fields: &mut Map) { let t = Tokens::get(ui.ctx()); @@ -214,7 +227,7 @@ fn brush_controls(ui: &mut egui::Ui, fields: &mut Map) { ui.horizontal(|ui| { ui.label(egui::RichText::new(tl!("Mode")).color(t.text_dim)); let options = [(false, tl!("Expand Detection Area")), (true, tl!("Restore Original Edge"))]; - crate::widgets::dropdown(ui, "select-mask-brush-mode", &mut subtract, &options, 170.0); + crate::widgets::dropdown(ui, "select-mask-brush-mode", tl!("Mode"), &mut subtract, &options, 170.0); }); fields.insert("__brushMode".into(), json!(if subtract { "subtract" } else { "add" })); let mut size = brush_size(fields); @@ -425,6 +438,10 @@ struct View { opacity: f32, show_edge: bool, show_original: bool, + /// Overlay's colour (Photoshop's default rubylith red) and whether it marks the selected + /// areas rather than the masked ones. + color: Color, + selected: bool, } impl View { @@ -436,6 +453,16 @@ impl View { opacity: v.and_then(|v| v.get("opacity")).and_then(Value::as_f64).filter(|o| o.is_finite()).unwrap_or(0.5).clamp(0.0, 1.0) as f32, show_edge: b("showEdge"), show_original: b("showOriginal"), + color: v + .and_then(|v| v.get("color")) + .and_then(Value::as_array) + .filter(|c| c.len() == 3) + .and_then(|c| { + let ch = |i: usize| c.get(i).and_then(Value::as_f64).filter(|v| v.is_finite()).map(|v| v.clamp(0.0, 1.0) as f32); + Some(Color::rgb(ch(0)?, ch(1)?, ch(2)?)) + }) + .unwrap_or(Color::rgb(1.0, 0.0, 0.0)), + selected: v.and_then(|v| v.get("indicates")).and_then(Value::as_str) == Some("selected"), } } } @@ -448,16 +475,31 @@ fn length(params: &Value, key: &str, k: u32) -> f64 { /// The preview of the Select and Mask dialog for `doc`: the proxy reduced by `k`, shown in the /// requested view. `None` when the refinement fails or is cancelled. pub fn preview_document(doc: &Document, active: Option, params: &Value, k: u32, cancel: Option<&photocraft_engine::jobs::JobCtx>) -> Option { + let k = k.max(1); + preview_proxy(crate::proxy::proxy_document(doc, k), active, params, k, cancel) +} + +/// [`preview_document`] on `proxy`, the document already reduced by `k` (the Select and Mask +/// workspace builds it itself, its selection already carrying the workspace's tool edits). +pub fn preview_proxy( + mut proxy: Document, + active: Option, + params: &Value, + k: u32, + cancel: Option<&photocraft_engine::jobs::JobCtx>, +) -> Option { let view = View::of(params); let k = k.max(1); - let mut proxy = crate::proxy::proxy_document(doc, k); let active = active.filter(|id| proxy.layer(*id).is_some()); // What Select and Mask starts from: the selection, else the active layer's mask. let original = proxy.selection.clone().or_else(|| active.and_then(|id| proxy.layer(id)).and_then(|l| l.mask.as_ref()).map(|m| m.surface.clone())); let canvas = Rect::new(0, 0, i32::try_from(proxy.size.width).unwrap_or(i32::MAX), i32::try_from(proxy.size.height).unwrap_or(i32::MAX)); let coverage = if view.show_edge { let band = Coverage::edge_band(original.as_ref(), canvas, length(params, "radius", k).round().clamp(1.0, 250.0) as usize); - band.with_brush(scaled_strokes(params, k).as_ref().and_then(|s| photocraft_algo::matting::EdgeBrush::new(s, canvas)).as_ref(), canvas) + // With nothing selected OK refines nothing (`matting::refine_area`), so the brush shows nothing either. + let selects = original.as_ref().is_some_and(|s| !s.content_bounds().intersect(&canvas).is_empty()); + let brush = if selects { scaled_strokes(params, k).and_then(|s| photocraft_algo::matting::EdgeBrush::new(&s, canvas)) } else { None }; + band.with_brush(brush.as_ref(), canvas) } else if view.show_original { Coverage::read(original.as_ref(), canvas) } else { @@ -546,7 +588,10 @@ fn show(doc: &mut Document, active: Option, view: &View, coverage: &Cov doc.selection = Some(coverage.surface(|v| v)); match view.mode { Mode::MarchingAnts => {} - Mode::Overlay => doc.layers.push(veil("Overlay", Color::rgb(1.0, 0.0, 0.0), o, Some(coverage.surface(|v| 1.0 - v)))), + Mode::Overlay => { + let marked = if view.selected { coverage.surface(|v| v) } else { coverage.surface(|v| 1.0 - v) }; + doc.layers.push(veil("Overlay", view.color, o, Some(marked))); + } Mode::OnBlack => doc.layers.push(veil("On Black", Color::BLACK, o, Some(coverage.surface(|v| 1.0 - v)))), Mode::OnWhite => doc.layers.push(veil("On White", Color::WHITE, o, Some(coverage.surface(|v| 1.0 - v)))), Mode::BlackWhite => { @@ -692,6 +737,24 @@ mod tests { assert!(pixel(&edge, 35, 10)[0] > 0.99 && pixel(&edge, 55, 10)[0] < 0.01, "Show Edge includes the painted area"); } + /// Overlay takes Photoshop's colour swatch and Indicates: the masked (default) or the selected + /// areas carry the tint. + #[test] + fn overlay_colour_and_what_it_indicates() { + let doc = half_selected(); + let active = doc.layers.first().map(|l| l.id); + let near = |a: &[f32], b: &[f32]| a.iter().zip(b).all(|(a, b)| (a - b).abs() < 0.02); + let view = |indicates: &str| json!({"__view": {"mode": "overlay", "opacity": 1.0, "color": [0.0, 0.0, 1.0], "indicates": indicates}}); + let masked = preview_document(&doc, active, &view("masked"), 1, None).expect("a preview"); + assert!(near(&pixel(&masked, 35, 10), &[0.0, 0.0, 1.0, 1.0]) && near(&pixel(&masked, 5, 10), &[1.0, 1.0, 1.0, 1.0])); + let selected = preview_document(&doc, active, &view("selected"), 1, None).expect("a preview"); + assert!(near(&pixel(&selected, 5, 10), &[0.0, 0.0, 1.0, 1.0]) && near(&pixel(&selected, 35, 10), &[1.0, 1.0, 1.0, 1.0])); + // A hostile colour falls back to red. + let bad = json!({"__view": {"mode": "overlay", "opacity": 1.0, "color": ["x", 2, null]}}); + let red = preview_document(&doc, active, &bad, 1, None).expect("a preview"); + assert!(near(&pixel(&red, 35, 10), &[1.0, 0.0, 0.0, 1.0])); + } + /// Show Edge shows the band around the selection's edge; Show Original the unrefined selection. #[test] fn show_edge_and_show_original() { diff --git a/crates/ui-egui/src/canvas/selection.rs b/crates/ui-egui/src/canvas/selection.rs new file mode 100644 index 0000000000..67577e2a27 --- /dev/null +++ b/crates/ui-egui/src/canvas/selection.rs @@ -0,0 +1,105 @@ +use super::tool_input::selection_drag_kind; +use super::{PhotocraftApp, Tool, json}; + +/// Photoshop's cursor over a selection: what a press (or the drag under way) would do. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum SelCursor { + /// A marquee or lasso inside the selection moves the outline (the move cursor, #1428), also + /// while it is being dragged. + Outline, + /// Arrow with scissors: the Move tool (⌘ with a marquee) cuts and moves the selected pixels. + Cut, + /// Arrow with a second arrow: ⌥ lifts a copy of them instead. + Copy, + /// Plain arrow: a floating piece, moved again by a plain drag until it is dropped. + Piece, + /// Hollow arrowhead: the selected pixels are being dragged. + Dragging, +} + +/// The cursor at document point `p` with `tool` in effect and `mods` held (`None`: not over a +/// selection the press would move). The Move tool moves the selected pixels from anywhere +/// (`move_ui::moves_selected_pixels_with`); a selection tool from inside the selection, or on the +/// floating piece (`selection_drag_kind`). +pub fn selection_cursor(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option { + if let Some(d) = &app.drag { + return match d.sel_move { + Some(true) => Some(SelCursor::Dragging), + Some(false) => Some(SelCursor::Outline), + None => None, + }; + } + let floating = app.session.active().and_then(photocraft_engine::float_cmds::floating).is_some(); + let pixels = if tool == Tool::Move { + crate::move_ui::moves_selected_pixels_with(app, tool) + } else { + match selection_drag_kind(app, tool, p, mods)? { + false => return Some(SelCursor::Outline), + true => true, + } + }; + if !pixels { + return None; + } + // ⌥ (⌘⌥ with a selection tool) floats a copy, of the layer's pixels or of the piece. + let copy = mods.alt && (tool == Tool::Move || mods.command); + Some(match (floating, copy) { + (_, true) => SelCursor::Copy, + (true, false) => SelCursor::Piece, + (false, false) => SelCursor::Cut, + }) +} + +/// Whole-pixel offset of a selection drag in progress (`Drag::sel_move`). +pub(crate) fn selection_drag_delta(app: &PhotocraftApp) -> Option<(i32, i32)> { + let d = app.drag.as_ref().filter(|d| d.sel_move.is_some())?; + let end = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + Some(((end[0] - d.start[0]).round().clamp(-1e7, 1e7) as i32, (end[1] - d.start[1]).round().clamp(-1e7, 1e7) as i32)) +} + +/// The selection a floating piece on document `idx` shows its ants for, before its offset: the +/// moved pixels' outline, as in Photoshop (#7538); `None` outlines the document's selection. The +/// surface's tiles are shared, so the clone is cheap. +pub(crate) fn floating_outline(app: &PhotocraftApp, idx: usize) -> Option { + if app.session.active_index() != Some(idx) { + return None; + } + photocraft_engine::float_cmds::floating(app.session.active()?)?.parts.outline().cloned() +} + +/// Where the selection is drawn relative to where it is: a floating piece's offset plus a drag +/// in progress. +pub(super) fn selection_shown_offset(app: &PhotocraftApp) -> Option<(i32, i32)> { + crate::move_ui::floating_offset(app).or_else(|| selection_drag_delta(app)).or_else(|| carried_selection_delta(app)) +} + +/// Whole-pixel offset of a Move drag of several layers, which carries the selection along +/// (`move_ui::carries_selection`): their ants follow the layers while they move (#7538). +fn carried_selection_delta(app: &PhotocraftApp) -> Option<(i32, i32)> { + let d = app.drag.as_ref().filter(|d| d.tool == Tool::Move && d.sel_move.is_none())?; + if !crate::move_ui::carries_selection(app) { + return None; + } + let end = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + Some(((end[0] - d.start[0]).round().clamp(-1e7, 1e7) as i32, (end[1] - d.start[1]).round().clamp(-1e7, 1e7) as i32)) +} + +/// End of a selection drag: move the outline, or the floating piece. A click without moving +/// deselects, like a marquee click (a click on a floating piece leaves it floating, unless it never +/// moved: then it is put back, so Undo isn't spent on it). +pub(super) fn finish_selection_drag(app: &mut PhotocraftApp, floating: bool, start: [f64; 2], end: [f64; 2]) { + let (dx, dy) = ((end[0] - start[0]).round(), (end[1] - start[1]).round()); + let unmoved = app.session.active().and_then(photocraft_engine::float_cmds::floating).is_some_and(|f| f.offset == (0, 0)); + let r = match (floating, dx == 0.0 && dy == 0.0) { + (true, true) if unmoved => app.run("select.drop", json!({})), + (true, true) => return, + (false, true) if app.session.is_enabled("select.deselect") => app.run("select.deselect", json!({})), + (false, true) => return, + (true, false) => app.run("select.float", json!({"dx": dx, "dy": dy})), + (false, false) => app.run("select.transformSelection", json!({"dx": dx, "dy": dy})), + }; + if let Err(e) = r { + app.ui.status = e; + app.ui.status_error = true; + } +} diff --git a/crates/ui-egui/src/canvas/snap_ui.rs b/crates/ui-egui/src/canvas/snap_ui.rs new file mode 100644 index 0000000000..4e1537d41b --- /dev/null +++ b/crates/ui-egui/src/canvas/snap_ui.rs @@ -0,0 +1,607 @@ +//! Snapping in the shell: builds [`photocraft_engine::snap`] targets from View › Snap To and the +//! document, snaps tool gestures (Move tool, marquees, crop, shapes, pen anchors, Free Transform +//! handles and guides), and draws smart guides (magenta alignment lines) while layers move. +//! +//! Snapping is on when View › Snap (⇧⌘;) is; holding Ctrl while dragging turns it off, except when +//! Ctrl is the corner-distortion modifier. Smart guides follow View › Show › Smart Guides and also snap the Move +//! tool to other layers' edges and centres. The threshold is 8 screen pixels at the current zoom. + +use egui::{Color32, Stroke, pos2}; +use photocraft_doc::LayerId; +use photocraft_engine::snap::{SnapKind, SnapLine, SnapOptions, SnapTargets, layer_rect, union}; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, ViewXform}; +use crate::state::Tool; + +/// Snap distance in screen pixels. +pub const SNAP_PX: f64 = 8.0; + +/// What the current drag snaps. +#[derive(Clone, Debug, PartialEq)] +pub enum Gesture { + /// A point (marquee corner, crop or shape corner, pen anchor, transform handle). + Point, + /// A transform corner: distortion snaps the corner itself, keeping the handle grab offset. + TransformCorner { index: usize, point: [f64; 2] }, + /// Layers being moved: their bounds at the start of the drag. + Move { rect: [f64; 4] }, + /// Free Transform box dragged from inside: its bounds at the start of the drag. + TransformMove { rect: [f64; 4] }, + /// A selection outline (or its floating piece) dragged with a selection tool: its bounds at + /// the start of the drag. + SelectionMove { rect: [f64; 4] }, + /// A marquee, Object Selection box or shape being drawn, repositioned with Space held + /// (`hold_keys`): its bounds when Space went down. + DrawMove { rect: [f64; 4] }, + /// A guide being moved with the Move tool. + Guide, + /// Default crop mode, a drag inside an upright box: the image moves under it, so the frame + /// moves the other way over the document and its edges and centre snap ([`crop_image`]). + CropImage, +} + +/// Snapping state of the drag in progress. +#[derive(Clone, Debug)] +pub struct ActiveSnap { + pub gesture: Gesture, + pub start: [f64; 2], + pub targets: SnapTargets, + /// Layer alignments only (smart guides), when View › Snap and Smart Guides are on. + pub smart: SnapTargets, + pub disabled: bool, +} + +/// Snap To options from the View menu and the Guides/Grid preferences. +pub fn options(app: &PhotocraftApp) -> SnapOptions { + let st = &app.ui.view.snap_to; + let e = &app.ui.extras; + let grid_step = app.session.active().map_or([18.0; 2], |d| { + let g = &app.session.prefs().guides_grid_and_slices; + let ppi = app.session.prefs().units_and_rulers.point_size.per_inch(); + crate::rulers::grid_major_px(g, d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64], ppi) + .map(|step| step / g.subdivisions.max(1) as f64) + }); + SnapOptions { + guides: st.guides && e.guides && app.ui.view.shows(app.ui.view.show.canvas_guides), + grid: st.grid && e.grid, + layers: st.layers, + document: st.document_bounds, + selection: true, + grid_step, + } +} + +/// Snap tolerance in document pixels at the current zoom. +pub fn tolerance(app: &PhotocraftApp) -> f64 { + SNAP_PX / app.point_zoom().max(0.01) as f64 +} + +fn smart_on(app: &PhotocraftApp) -> bool { + app.ui.view.shows(app.ui.view.show.smart_guides) +} + +fn snap_on(app: &PhotocraftApp) -> bool { + app.ui.extras.snap +} + +/// Targets of the active document (smart = layer alignments only). +fn build(app: &PhotocraftApp, exclude: &[LayerId], smart: bool) -> SnapTargets { + let Some(st) = app.session.active() else { return SnapTargets::default() }; + let opts = + if smart { SnapOptions { guides: false, grid: false, layers: true, document: true, selection: false, grid_step: [0.0; 2] } } else { options(app) }; + let t = SnapTargets::from_document(&st.doc, &opts, exclude); + if smart { t.filtered(SnapKind::is_smart) } else { t } +} + +/// What a move with `tool` in effect drags: the selection's bounds where its pixels float +/// (`move_ui`), else the union of the selected layers' bounds. +fn moving_rect(app: &PhotocraftApp, tool: Tool) -> Option<[f64; 4]> { + let st = app.session.active()?; + if crate::move_ui::moves_selected_pixels_with(app, tool) { + return selection_rect(app); + } + union(st.selected_layers().into_iter().filter_map(|id| layer_rect(&st.doc, id))) +} + +/// The bounds of the pixels a selection moves, where they are shown: its floating piece's outline +/// at its offset (#7538), else (nothing cut yet, or a mask or empty piece) the selection's bounds. +fn selection_rect(app: &PhotocraftApp) -> Option<[f64; 4]> { + let st = app.session.active()?; + let sel = st.doc.selection.as_ref()?; + let r = match photocraft_engine::float_cmds::floating(st) { + Some(f) => f.parts.outline().unwrap_or(sel).content_bounds().translate(f.offset.0, f.offset.1), + None => sel.content_bounds(), + }; + (!r.is_empty()).then(|| [r.x0, r.y0, r.x1, r.y1].map(f64::from)) +} + +/// A press cut the selected pixels after snapping began (`filter_event` runs before the tools): +/// the move now snaps the cut piece's outline, not the selection it was cut with (#7538). +pub(crate) fn follow_cut(app: &mut PhotocraftApp) { + let Some(rect) = selection_rect(app) else { return }; + if let Some(snap) = app.prefs_rt.snap.as_mut() { + match &mut snap.gesture { + Gesture::Move { rect: r } | Gesture::SelectionMove { rect: r } => *r = rect, + _ => {} + } + } +} + +fn is_point_tool(t: Tool) -> bool { + matches!( + t, + Tool::RectMarquee + | Tool::EllipseMarquee + | Tool::SingleRowMarquee + | Tool::SingleColumnMarquee + | Tool::Crop + | Tool::ObjectSelection + | Tool::Pen + | Tool::CurvaturePen + | Tool::Rectangle + | Tool::EllipseShape + | Tool::Triangle + | Tool::Polygon + | Tool::Line + | Tool::Type + | Tool::VerticalType + | Tool::HorizontalTypeMask + | Tool::VerticalTypeMask + ) +} + +fn quad_rect(q: &[[f64; 2]; 4]) -> [f64; 4] { + let xs = q.iter().map(|p| p[0]); + let ys = q.iter().map(|p| p[1]); + [xs.clone().fold(f64::MAX, f64::min), ys.clone().fold(f64::MAX, f64::min), xs.fold(f64::MIN, f64::max), ys.fold(f64::MIN, f64::max)] +} + +/// Is `p` inside the quad (even-odd)? +fn in_quad(q: &[[f64; 2]; 4], p: [f64; 2]) -> bool { + let mut inside = false; + for i in 0..4 { + let (a, b) = (q[i], q[(i + 3) % 4]); + if (a[1] > p[1]) != (b[1] > p[1]) && p[0] < (b[0] - a[0]) * (p[1] - a[1]) / (b[1] - a[1]) + a[0] { + inside = !inside; + } + } + inside +} + +/// Near a Free Transform corner or edge handle (within its grab radius)? +fn near_handle(q: &[[f64; 2]; 4], p: [f64; 2], tol: f64) -> bool { + (0..4).any(|i| { + let (a, b) = (q[i], q[(i + 1) % 4]); + let mid = [(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0]; + [a, mid].iter().any(|h| (h[0] - p[0]).hypot(h[1] - p[1]) <= tol) + }) +} + +/// Photoshop: holding Ctrl (Windows) / Control (macOS) *during* a drag temporarily turns snapping +/// off. Only the held state while dragging counts: Ctrl at the press belongs to the tool (Ctrl-click +/// auto-selects with the Move tool), and ⌘ on the Mac never overrides snapping. +fn override_held(mods: egui::Modifiers) -> bool { + mods.ctrl && !mods.mac_cmd +} + +fn corner_distorts(app: &PhotocraftApp, gesture: &Gesture, mods: egui::Modifiers) -> bool { + matches!(gesture, Gesture::TransformCorner { index, .. } if crate::transform_tool::corner_distorts(app, *index, mods)) +} + +fn override_for_gesture(app: &PhotocraftApp, mods: egui::Modifiers) -> bool { + override_held(mods) && !(mods.command && app.prefs_rt.snap.as_ref().is_some_and(|s| corner_distorts(app, &s.gesture, mods))) +} + +/// The Move tool's press at `p` picked other layers (Auto-Select): the drag moves those, so its +/// box, what it snaps and what it leaves out of the targets are theirs, not the ones selected +/// before (a group or an empty layer selected before had no box at all). +pub(crate) fn retarget_move(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) { + begin(app, p, mods); +} + +/// Start snapping for a drag beginning at `p` with `mods` held (called on pointer down). +fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) { + app.prefs_rt.snap = None; + app.prefs_rt.snap_lines.clear(); + // Puppet Warp's pins and Perspective Warp's quads go where they are dragged, as in Photoshop: + // the press is theirs, not the tool's (with the Move tool it took a layer move's Smart Guides). + if app.session.active().is_none() || app.distort.active() { + return; + } + // The tool the press acts as: ⌘ makes a painting tool the Move tool, and a selection tool + // outside the selection moves the layer as the Move tool does (#2681). + let tool = crate::canvas::event_tool(app, mods); + let moves_layer = crate::canvas::command_moves_layer(app, tool, p, mods); + let crop_tol = crate::crop_ui::tolerance(app); + let gesture = if let Some(t) = &app.ui.transform { + let corner = match crate::transform_tool::hit(t, p, crate::transform_tool::handle_tolerance(app)) { + crate::transform_tool::Hit::Corner(index) => { + t.quad.get(index).map(|&point| (Gesture::TransformCorner { index, point }, crate::transform_tool::moving_layers(t))) + } + _ => None, + }; + if t.mode == crate::state::TransformMode::Distort { + corner + } else if near_handle(&t.quad, p, crate::transform_tool::handle_tolerance(app)) { + corner.or_else(|| Some((Gesture::Point, crate::transform_tool::moving_layers(t)))) + } else if in_quad(&t.quad, p) { + Some((Gesture::TransformMove { rect: quad_rect(&t.quad) }, crate::transform_tool::moving_layers(t))) + } else { + None + } + } else if tool == Tool::Move && crate::rulers::guide_at(app, p[0], p[1]).is_some() { + Some((Gesture::Guide, Vec::new())) + } else if tool == Tool::Move || moves_layer { + let exclude = app.session.active().map(|s| s.selected_layers()).unwrap_or_default(); + moving_rect(app, tool).map(|rect| (Gesture::Move { rect }, exclude)) + } else if tool == Tool::Crop + && (crate::crop_mode::classic(app) || app.crop.default_frame) + && let Some(rect) = + app.ui.crop_rect.filter(|r| crate::crop_ui::angle(app) == 0.0 && crate::crop_ui::hit(*r, p, crop_tol) == crate::crop_ui::Hit::Inside) + { + // Inside the untouched default frame draws a new crop (in both modes): snap its corner, not + // the old canvas-sized frame's bounds. An edited Classic Mode frame still moves and snaps + // its edges. + let gesture = if app.crop.default_frame { Gesture::Point } else { Gesture::Move { rect } }; + Some((gesture, Vec::new())) + } else if tool == Tool::Crop + && crate::crop_mode::moves_image(app) + && !app.crop.default_frame + && crate::crop_ui::angle(app) == 0.0 + && app.ui.crop_rect.is_some_and(|r| crate::crop_ui::hit(r, p, crop_tol) == crate::crop_ui::Hit::Inside) + { + // The default mode moves the image under an upright box: the frame's edges and centre + // snap, as an edited Classic Mode frame's do, so content can be centred in it (#8580). + Some((Gesture::CropImage, Vec::new())) + } else if tool == Tool::Crop + && (crate::crop_ui::turns_at(app, p) + || app.ui.crop_rect.is_some_and(|r| crate::crop_ui::hit_turned(r, crate::crop_ui::angle(app), p, crop_tol) == crate::crop_ui::Hit::Inside)) + { + // Turning the frame, moving a turned one (its edges don't line up with anything), or in + // the default mode turning the image or moving it under a turned box: no snapping. + None + } else if let Some(cut) = crate::canvas::selection_drag_kind(app, tool, p, mods) { + // A marquee or lasso inside the selection moves the outline (⌘: cuts its pixels into a + // floating piece): its bounds snap, as the Move tool's do, not the pointer. A cut piece + // doesn't snap to its own layer. + let exclude = if cut { app.session.active().map(|s| s.selected_layers()).unwrap_or_default() } else { Vec::new() }; + selection_rect(app).map(|rect| (Gesture::SelectionMove { rect }, exclude)) + } else if tool.is_type() + && let Some(layer) = app.ui.text_edit.as_ref().map(|e| LayerId(e.layer)) + { + // Editing type: a paragraph-box handle drag snaps to guides and other layers, not to the + // text being resized; a press in the text places the caret where it is (#6819). + crate::type_tool::box_handle_at(app, layer, p[0], p[1]).map(|_| (Gesture::Point, vec![layer])) + } else if is_point_tool(tool) { + Some((Gesture::Point, Vec::new())) + } else { + None + }; + let Some((gesture, exclude)) = gesture else { return }; + let mut targets = if snap_on(app) { build(app, &exclude, false) } else { SnapTargets::default() }; + if app.ui.transform.as_ref().is_some_and(|t| t.mode == crate::state::TransformMode::Distort) { + // Explicit Distort fits corners to guides; preserve free placement near layer edges. + targets = targets.filtered(|k| k == SnapKind::Guide); + } + if gesture == Gesture::Guide { + // A guide never snaps to itself (or other guides). + targets = targets.filtered(|k| k != SnapKind::Guide); + } + if matches!(gesture, Gesture::SelectionMove { .. }) || (matches!(gesture, Gesture::Move { .. }) && crate::move_ui::moves_selected_pixels_with(app, tool)) { + // A moving selection never snaps back to where it started. + targets = targets.filtered(|k| k != SnapKind::Selection); + } + let smart = if snap_on(app) && matches!(gesture, Gesture::Move { .. } | Gesture::CropImage) && smart_on(app) { + build(app, &exclude, true) + } else { + SnapTargets::default() + }; + app.prefs_rt.snap = Some(ActiveSnap { gesture, start: p, targets, smart, disabled: false }); +} + +/// Snap a guide drag that ⌘ with a selection tool starts at `p` (`canvas::command_guide_at`, +/// #2690) as the Move tool's guide drag: never to guides, so not back onto itself. +pub fn begin_guide(app: &mut PhotocraftApp, p: [f64; 2]) { + app.prefs_rt.snap_lines.clear(); + let targets = if snap_on(app) { build(app, &[], false).filtered(|k| k != SnapKind::Guide) } else { SnapTargets::default() }; + app.prefs_rt.snap = Some(ActiveSnap { gesture: Gesture::Guide, start: p, targets, smart: SnapTargets::default(), disabled: false }); +} + +/// Round to whole pixels when Preferences › Tools asks vector tools and transforms to snap to +/// the pixel grid. +fn pixel_round(app: &PhotocraftApp, p: [f64; 2]) -> [f64; 2] { + if app.session.prefs().tools.snap_vector_tools_and_transforms_to_pixel_grid + && matches!(app.ui.tool, Tool::Pen | Tool::CurvaturePen | Tool::Rectangle | Tool::EllipseShape | Tool::Triangle | Tool::Polygon | Tool::Line) + { + [p[0].round(), p[1].round()] + } else { + p + } +} + +/// Snap one pointer position for the active gesture; records the alignment lines. +fn apply(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) -> [f64; 2] { + let tol = tolerance(app); + let Some(snap) = app.prefs_rt.snap.as_ref() else { return p }; + if !snap_on(app) || snap.disabled { + app.prefs_rt.snap_lines.clear(); + return p; + } + let (out, lines) = match &snap.gesture { + Gesture::Point => crop_handle(app, p, mods, &snap.targets, tol).unwrap_or_else(|| snap.targets.snap_point(p, tol)), + Gesture::TransformCorner { point, .. } if corner_distorts(app, &snap.gesture, mods) => { + let proposed = [point[0] + p[0] - snap.start[0], point[1] + p[1] - snap.start[1]]; + let (corner, lines) = snap.targets.snap_point(proposed, tol); + ([p[0] + corner[0] - proposed[0], p[1] + corner[1] - proposed[1]], lines) + } + Gesture::TransformCorner { .. } => snap.targets.snap_point(p, tol), + Gesture::Guide => snap.targets.snap_point(p, tol), + Gesture::Move { rect } | Gesture::TransformMove { rect } | Gesture::SelectionMove { rect } | Gesture::DrawMove { rect } => { + let d = [p[0] - snap.start[0], p[1] - snap.start[1]]; + let moved = [rect[0] + d[0], rect[1] + d[1], rect[2] + d[0], rect[3] + d[1]]; + let set = if snap.targets.is_empty() { &snap.smart } else { &snap.targets }; + let (adj, lines) = set.snap_rect(moved, tol); + ([p[0] + adj[0], p[1] + adj[1]], lines) + } + Gesture::CropImage => { + // Smart guides still snap when nothing in View › Snap To is within reach: the + // document's bounds alone keep `targets` from ever being empty. + let snapped = |set| crop_image(app, p, set, tol).filter(|(_, lines)| !lines.is_empty()); + snapped(&snap.targets).or_else(|| snapped(&snap.smart)).unwrap_or((p, Vec::new())) + } + }; + app.prefs_rt.snap_lines = lines; + out +} + +/// A corner stopped by the transform's convexity constraint is not aligned to the proposed +/// target. Keep only the lines the final corner actually reaches, after the transform applies. +pub(crate) fn retain_transform_lines(app: &mut PhotocraftApp, mods: egui::Modifiers) { + let Some(snap) = app.prefs_rt.snap.as_ref() else { return }; + let Gesture::TransformCorner { index, .. } = snap.gesture else { return }; + if !corner_distorts(app, &snap.gesture, mods) { + return; + } + let Some(point) = app.ui.transform.as_ref().and_then(|t| t.quad.get(index)).copied() else { return }; + app.prefs_rt.snap_lines.retain(|line| (if line.vertical { point[0] } else { point[1] } - line.pos).abs() < 1e-6); +} + +/// Snap a crop handle being dragged: the handle itself (the frame's edge or corner), not the +/// pointer, which grabbed it up to the grab radius away (#1919: snapping the pointer left a strip +/// as wide as that offset between the frame and the edge it showed as snapped to). With Auto +/// Center Preview the handle is found on the image, calculated from the original camera: snapping +/// the pointer in the moving camera feeds the previous frame's pan back into the resize and makes +/// a stationary pointer oscillate near the canvas edges. +fn crop_handle(app: &PhotocraftApp, p: [f64; 2], mods: egui::Modifiers, targets: &SnapTargets, tol: f64) -> Option<([f64; 2], Vec)> { + if app.ui.tool != Tool::Crop { + return None; + } + let crate::crop_ui::CropDrag::Resize { hx, hy, start, rect, view } = app.crop.drag? else { return None }; + let deg = crate::crop_ui::angle(app); + let ratio = crate::crop_ui::resize_ratio(app, rect, mods); + let q = view.map_or(p, |view| crate::crop_mode::to_press(app, Some(view), p)); + // The frame for the pointer at `q`, as `crop_ui` resizes it. + let frame_at = |q: [f64; 2]| match view { + Some(_) => crate::crop_mode::resized_frame(crate::crop_mode::LiveResize { rect, deg, hx, hy, start, q, ratio, alt: mods.alt }).map(|(f, _)| f), + None => Some(crate::crop_ui::resized_turned(rect, deg, hx, hy, [q[0] - start[0], q[1] - start[1]], ratio, mods.alt)), + }; + let frame = frame_at(q)?; + let at = |r: [f64; 4]| { + let c = crate::crop_ui::center(r); + crate::crop_ui::turn([c[0] + f64::from(hx) * (r[2] - r[0]) / 2.0, c[1] + f64::from(hy) * (r[3] - r[1]) / 2.0], c, deg) + }; + let handle = at(frame); + let (snapped, mut lines) = targets.snap_point(handle, tol); + let mut d = crate::crop_ui::turn([snapped[0] - handle[0], snapped[1] - handle[1]], [0.0, 0.0], -deg); + // An edge handle controls only its own axis, not the midpoint along the other one. + if hx == 0 { + d[0] = 0.0; + } + if hy == 0 { + d[1] = 0.0; + } + if deg == 0.0 { + lines.retain(|line| if line.vertical { hx != 0 } else { hy != 0 }); + } + if lines.is_empty() { + return Some((p, lines)); + } + // Auto Center Preview moves both screen edges. On the image the opposite edge stays put, + // so the grabbed edge travels twice as far as the pointer (once with Alt). Without it the + // handle follows the pointer. + let scale = if view.is_some() && !mods.alt { 2.0 } else { 1.0 }; + // How much the frame's extent grows per unit of pointer travel: both sides with Auto Center + // Preview or Alt, else the grabbed one. + let grow = if view.is_some() || mods.alt { 2.0 } else { 1.0 }; + let original = at(rect); + let travel = crate::crop_ui::turn([handle[0] - original[0], handle[1] - original[1]], [0.0, 0.0], -deg); + let mut local = [(travel[0] + d[0]) / scale, (travel[1] + d[1]) / scale]; + if deg == 0.0 + && hx != 0 + && hy != 0 + && let Some(k) = ratio + { + // A corner with a ratio has one degree of freedom. Put the other axis on the same + // ratio, rather than letting the raw pointer's unused axis override the snapped edge. + if lines.iter().any(|line| line.vertical) { + local[1] = f64::from(hy) * ((rect[2] - rect[0] + grow * f64::from(hx) * local[0]) / k - (rect[3] - rect[1])) / grow; + } else { + local[0] = f64::from(hx) * ((rect[3] - rect[1] + grow * f64::from(hy) * local[1]) * k - (rect[2] - rect[0])) / grow; + } + } + let d = crate::crop_ui::turn(local, [0.0, 0.0], deg); + let q = [start[0] + d[0], start[1] + d[1]]; + if let Some(snapped_frame) = frame_at(q) { + let h = at(snapped_frame); + lines.retain(|line| (if line.vertical { h[0] } else { h[1] } - line.pos).abs() < 1e-3); + } + Some((view.map_or(q, |view| crate::crop_mode::from_press(app, view, q)), lines)) +} + +/// Snap the image being moved under the crop box (default mode): the frame moves by the image's +/// travel the other way over the document, so its edges and centre snap and the pointer moves by +/// the frame's correction the other way too (#8580). As `crop_ui` reads this drag, the pointer is +/// read in the camera at the press, since the view pans with the frame meanwhile. +fn crop_image(app: &PhotocraftApp, p: [f64; 2], targets: &SnapTargets, tol: f64) -> Option<([f64; 2], Vec)> { + let crate::crop_ui::CropDrag::MoveImage { start, rect, view } = app.crop.drag? else { return None }; + let q = crate::crop_mode::to_press(app, view, p); + let d = [q[0] - start[0], q[1] - start[1]]; + let (adj, lines) = targets.snap_rect([rect[0] - d[0], rect[1] - d[1], rect[2] - d[0], rect[3] - d[1]], tol); + if adj == [0.0, 0.0] { + return Some((p, lines)); + } + let q = [q[0] - adj[0], q[1] - adj[1]]; + Some((view.map_or(q, |view| crate::crop_mode::from_press(app, view, q)), lines)) +} + +/// The bounds `[x0, y0, x1, y1]` of what drag `d` draws so far, for the tools whose drag Space +/// repositions and that snap (`None` for the rest, or a shape still too small to draw). +fn drawn_rect(app: &PhotocraftApp, d: &crate::canvas::Drag) -> Option<[f64; 4]> { + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let (a, b) = match d.tool { + Tool::RectMarquee | Tool::EllipseMarquee => crate::canvas::marquee_corners(&app.ui.tool_options, d, last), + Tool::ObjectSelection => (d.start, last), + t if crate::vector_ui::is_shape_tool(t) => return crate::vector_ui::shape_bounds(&app.ui.tool_options, t, d.start, last, d.live), + _ => return None, + }; + Some([a[0].min(b[0]), a[1].min(b[1]), a[0].max(b[0]), a[1].max(b[1])]) +} + +/// Space held while a marquee, Object Selection box or shape is drawn moves the whole of it +/// (`hold_keys`): its edges and centre snap, as a moved selection's do, not just the corner under +/// the pointer. Releasing Space goes back to snapping that corner. +fn follow_reposition(app: &mut PhotocraftApp) { + let Some(d) = app.drag.as_ref() else { return }; + let Some(snap) = app.prefs_rt.snap.as_ref() else { return }; + let gesture = match (&snap.gesture, d.reposition) { + (Gesture::Point, true) => match drawn_rect(app, d) { + Some(rect) => Gesture::DrawMove { rect }, + None => return, + }, + (Gesture::DrawMove { .. }, false) => Gesture::Point, + _ => return, + }; + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + if let Some(snap) = app.prefs_rt.snap.as_mut() { + snap.gesture = gesture; + snap.start = last; + } +} + +/// Snap a tool event's coordinates (called by `canvas::tool_event` before the tools see it). +pub fn filter_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> ToolEvent { + match ev { + ToolEvent::Down { x, y, pressure } => { + begin(app, [x, y], mods); + // Moves snap their delta, which is zero at the start: keep the press point. + let p = match app.prefs_rt.snap.as_ref().map(|s| &s.gesture) { + Some(Gesture::Point) + if !(app.ui.tool == Tool::Crop + && app.ui.crop_rect.is_some_and(|r| { + matches!( + crate::crop_ui::hit_turned(r, crate::crop_ui::angle(app), [x, y], crate::crop_ui::tolerance(app)), + crate::crop_ui::Hit::Handle(..) + ) + })) => + { + let q = apply(app, [x, y], mods); + pixel_round(app, q) + } + _ => [x, y], + }; + ToolEvent::Down { x: p[0], y: p[1], pressure } + } + ToolEvent::Move { x, y, pressure } => { + if app.prefs_rt.snap.is_none() { + return ev; + } + follow_reposition(app); + let disabled = override_for_gesture(app, mods); + if let Some(s) = app.prefs_rt.snap.as_mut() { + s.disabled = disabled; + } + let q = apply(app, [x, y], mods); + let p = pixel_round(app, q); + ToolEvent::Move { x: p[0], y: p[1], pressure } + } + ToolEvent::Up { x, y } => { + if app.prefs_rt.snap.is_none() { + return ev; + } + follow_reposition(app); + let disabled = override_for_gesture(app, mods); + if let Some(s) = app.prefs_rt.snap.as_mut() { + s.disabled = disabled; + } + let q = apply(app, [x, y], mods); + let p = pixel_round(app, q); + app.prefs_rt.snap = None; + app.prefs_rt.snap_lines.clear(); + ToolEvent::Up { x: p[0], y: p[1] } + } + } +} + +/// Snap a guide being dragged out of a ruler (`vertical` guides snap their x). +pub fn snap_guide(app: &mut PhotocraftApp, vertical: bool, pos: f64) -> f64 { + if !snap_on(app) { + return pos; + } + let Some(st) = app.session.active() else { return pos }; + // The Snap To options (grid, layers, document bounds, grid step) change the targets too. + let key = (st.doc.id, st.revision, options(app)); + if app.prefs_rt.guide_targets.as_ref().is_none_or(|(k, _)| *k != key) { + let t = build(app, &[], false).filtered(|k| k != SnapKind::Guide); + app.prefs_rt.guide_targets = Some((key, t)); + } + let tol = tolerance(app); + let t = app.prefs_rt.guide_targets.as_ref().map(|(_, t)| t); + t.and_then(|t| t.nearest(vertical, pos, tol)).map_or(pos, |t| t.pos) +} + +fn hex_color(s: &str, fallback: Color32) -> Color32 { + photocraft_engine::prefs::parse_hex(s).map_or(fallback, |c| Color32::from_rgb(c[0], c[1], c[2])) +} + +/// The Smart Guides colour from Preferences › Guides, Grid & Slices (magenta by default). +pub(crate) fn smart_color(app: &PhotocraftApp) -> Color32 { + hex_color(&app.session.prefs().guides_grid_and_slices.smart_guide_color, Color32::from_rgb(255, 0, 255)) +} + +/// Draw smart guides (and the moving bounds) for the drag in progress. +pub fn draw(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) { + let Some(snap) = &app.prefs_rt.snap else { return }; + let smart_color = smart_color(app); + // The moved layers' bounds follow the Move tool (Free Transform draws its own box). + if let (Gesture::Move { rect }, Some(d)) = + (&snap.gesture, app.drag.as_ref().and_then(|d| d.points.last()).map(|p| [p[0] - snap.start[0], p[1] - snap.start[1]])) + { + let r = egui::Rect::from_two_pos( + xf.to_screen((rect[0] + d[0]) as f32, (rect[1] + d[1]) as f32), + xf.to_screen((rect[2] + d[0]) as f32, (rect[3] + d[1]) as f32), + ); + painter.rect_stroke(r, 0.0, Stroke::new(1.0, crate::theme::Tokens::get(painter.ctx()).accent), egui::StrokeKind::Middle); + } + if !smart_on(app) { + return; + } + for l in &app.prefs_rt.snap_lines { + if !l.kind.is_smart() { + continue; + } + draw_line(painter, xf, l, smart_color); + } +} + +fn draw_line(painter: &egui::Painter, xf: &ViewXform, l: &SnapLine, color: Color32) { + let (a, b) = if l.vertical { + (xf.to_screen(l.pos as f32, l.from as f32), xf.to_screen(l.pos as f32, l.to as f32)) + } else { + (xf.to_screen(l.from as f32, l.pos as f32), xf.to_screen(l.to as f32, l.pos as f32)) + }; + painter.line_segment([pos2(a.x.round() + 0.5, a.y.round() + 0.5), pos2(b.x.round() + 0.5, b.y.round() + 0.5)], Stroke::new(1.0, color)); +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/snap_ui/tests.rs b/crates/ui-egui/src/canvas/snap_ui/tests.rs new file mode 100644 index 0000000000..56617d906e --- /dev/null +++ b/crates/ui-egui/src/canvas/snap_ui/tests.rs @@ -0,0 +1,756 @@ +use super::*; +use serde_json::json; + +fn app_with_box() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.views[0].zoom = 1.0; + app.ui.views[0].fit_pending = false; + // Two boxes: a target at (300..350, 200..260) and the moving one at (40..90, 40..80). + app.run("layer.new.layer", json!({"name": "target"})).unwrap(); + app.run("select.rect", json!({"x": 300, "y": 200, "width": 50, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("layer.new.layer", json!({"name": "mover"})).unwrap(); + app.run("select.rect", json!({"x": 40, "y": 40, "width": 50, "height": 40})).unwrap(); + app.run("edit.fill", json!({"color": "#0000ff"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app +} + +/// #7538: moving selected pixels snaps (and frames) the pixels, not a loose marquee round them. +/// Snapping starts before the press cuts them, so the cut retargets it to their outline. +#[test] +fn moving_selected_pixels_snaps_their_bounds_not_the_marquee() { + for alt in [false, true] { + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + let m = egui::Modifiers { alt, ..Default::default() }; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone()); + assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 90.0, 80.0] }), "alt {alt}: the blue box, not the marquee"); + // 263 px right puts the box's left edge 3 px from the target's (the marquee's 20 px off). + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + app.run("select.drop", json!({})).unwrap(); + let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds().x0); + assert_eq!(sel, Some(300), "alt {alt}: snapped to the target's edge"); + } + // A marquee's ⌘-drag cuts them the same way. + let mut app = app_with_box(); + app.ui.tool = Tool::RectMarquee; + app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() }; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, cmd); + let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone()); + assert_eq!(gesture, Some(Gesture::SelectionMove { rect: [40.0, 40.0, 90.0, 80.0] }), "⌘-drag with the marquee"); + // The Background has no transparency to shrink to. + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + app.run("layer.select", json!({"name": "Background"})).unwrap(); + app.run("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, egui::Modifiers::NONE); + let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone()); + assert_eq!(gesture, Some(Gesture::Move { rect: [20.0, 20.0, 120.0, 110.0] }), "the Background"); +} + +fn mover_bounds(app: &PhotocraftApp) -> photocraft_geom::Rect { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().content_bounds() +} + +#[test] +fn drawing_a_new_crop_snaps_its_corner_not_the_default_frame() { + // In both crop modes: the default mode draws a new box inside the untouched frame too. + for (classic, snap, end) in [(true, false, [300.0, 220.0]), (true, true, [294.0, 217.0]), (false, false, [300.0, 220.0]), (false, true, [294.0, 217.0])] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 640, "height": 480, "background": "white"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [100, 80, 200, 140], "fill": "#d59b40"})).unwrap(); + app.sync_views(); + app.ppp = 2.0; + app.ui.views[0].zoom = 1.0; + app.ui.views[0].fit_pending = false; + app.ui.tool = Tool::Crop; + app.ui.tool_options.crop_shield.classic_mode = classic; + app.ui.extras.snap = snap; + app.ui.view.show.smart_guides = true; + crate::crop_ui::ensure_frame(&mut app); + let view = app.ui.views[0].clone(); + let history = app.session.active().unwrap().history.past_len(); + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 80.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: end[0], y: end[1], pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: end[0], y: end[1] }, m); + // Smart Guides must not snap the old canvas-sized frame as if it were moving. + // With View Snap on, the new corner still snaps to the nearby shape's edges. + assert_eq!(app.ui.crop_rect, Some([100.0, 80.0, 300.0, 220.0]), "Classic Mode: {classic}, View Snap: {snap}"); + // The default mode's Auto Center Preview re-centres the drawn box; Classic Mode keeps the view. + if classic { + assert_eq!(app.ui.views[0], view); + } + assert_eq!(app.session.active().unwrap().history.past_len(), history); + } +} + +#[test] +fn move_tool_snaps_to_other_layer_edges_with_smart_guides() { + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + // Drag so the mover's left edge lands 3 px right of the target's (x = 303). + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m); + assert!(app.prefs_rt.snap_lines.iter().any(|l| l.vertical && l.pos == 300.0 && l.kind.is_smart()), "{:?}", app.prefs_rt.snap_lines); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + assert_eq!(mover_bounds(&app).x0, 300); + assert!(app.prefs_rt.snap.is_none()); +} + +/// Puppet Warp's pins go where they are dragged: they don't snap, with View › Snap on or +/// off, whatever tool was active when it started. With the Move tool a pin drag took the +/// layer move's Smart Guides and jumped to the other layer's edges. +#[test] +fn puppet_warp_pins_never_snap() { + for (tool, snap) in [(Tool::Move, false), (Tool::Move, true), (Tool::RectMarquee, true), (Tool::Brush, false)] { + let mut app = app_with_box(); + app.ui.tool = tool; + app.ui.extras.snap = snap; + app.ui.view.show.smart_guides = true; + app.run("view.newGuide", json!({"orientation": "vertical", "position": 321})).unwrap(); + let ctx = egui::Context::default(); + crate::distort_ui::menu(&mut app, &ctx, "edit.puppetWarp", &json!({})).unwrap().unwrap(); + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + // The mover's left edge 3 px right of the target's, the pin 2 px from the guide. + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m); + assert!(app.prefs_rt.snap_lines.is_empty(), "{tool:?}, snap {snap}: {:?}", app.prefs_rt.snap_lines); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + let pins = &app.distort.puppet.as_ref().unwrap().warp.pins; + assert_eq!(pins.iter().map(|p| p.dst).collect::>(), vec![[323.0, 100.0]], "{tool:?}, snap {snap}"); + } +} + +/// ⌘ turns a painting tool, or a marquee outside the selection, into the Move tool for the +/// drag: it gets the Move tool's box and smart-guide snapping, not the selected tool's. +#[test] +fn command_drag_temporary_move_snaps_like_the_move_tool() { + for tool in [Tool::Brush, Tool::RectMarquee] { + let mut app = app_with_box(); + app.ui.tool = tool; + let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() }; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, cmd); + let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone()); + assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 90.0, 80.0] }), "{tool:?}: the mover's box"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, cmd); + assert_eq!(mover_bounds(&app).x0, 300, "{tool:?}: snapped to the target's edge"); + } +} + +/// The drag's box and snapping belong to the layer Auto-Select picks on the press, not to the +/// one selected before: another layer (its box would move instead), or an empty layer (no box, +/// no snapping at all). +#[test] +fn move_tool_box_and_snapping_follow_the_auto_selected_layer() { + for before in ["target", "empty"] { + let mut app = app_with_box(); + let mover = app.session.active().unwrap().active_layer.unwrap(); + if before == "empty" { + app.run("layer.new.layer", json!({"name": "empty"})).unwrap(); + } else { + let st = app.session.active().unwrap(); + let target = st.doc.walk().into_iter().find(|(_, _, l)| l.name == "target").unwrap().2.id; + app.run("layer.select", json!({"layer": target.0})).unwrap(); + } + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = true; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + assert_eq!(app.session.active().unwrap().active_layer, Some(mover), "{before}: Auto-Select picked the mover"); + let gesture = app.prefs_rt.snap.as_ref().map(|s| s.gesture.clone()); + assert_eq!(gesture, Some(Gesture::Move { rect: [40.0, 40.0, 90.0, 80.0] }), "{before}: the box is the mover's"); + // The mover's left edge lands 3 px right of the target's and snaps to it. + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 323.0, y: 100.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + assert_eq!(mover_bounds(&app).x0, 300, "{before}: snapped to the target's edge"); + } +} + +#[test] +fn view_snap_off_blocks_smart_guide_snapping() { + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + app.ui.view.show.smart_guides = true; + app.ui.extras.snap = false; + app.ui.tool_options.move_auto_select = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + assert_eq!(mover_bounds(&app).x0, 303, "View › Snap off must not snap via smart guides"); +} + +#[test] +fn ctrl_or_snap_off_disables_snapping() { + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + app.ui.view.show.smart_guides = false; + app.ui.extras.snap = false; + // The presses land beside the moved layer: keep moving it (no Auto-Select pick). + app.ui.tool_options.move_auto_select = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + assert_eq!(mover_bounds(&app).x0, 303); + app.ui.extras.snap = true; + let ctrl = egui::Modifiers { ctrl: true, ..Default::default() }; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 62.0, y: 60.0 }, ctrl); + assert_eq!(mover_bounds(&app).x0, 305, "no snap to the target's right edge at 350? (moved freely)"); + // Ctrl only at the press (Ctrl-click auto-select) doesn't turn snapping off for the drag. + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, ctrl); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 61.0, y: 60.0, pressure: 1.0 }, m); + assert!(!app.prefs_rt.snap.as_ref().is_some_and(|s| s.disabled)); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 61.0, y: 60.0 }, m); + // ⌘ on the Mac is not the override. + let cmd = egui::Modifiers { mac_cmd: true, command: true, ..Default::default() }; + assert!(!override_held(cmd) && override_held(ctrl)); +} + +#[test] +fn snap_off_disables_all_snapping_even_with_smart_guides() { + let mut app = app_with_box(); + app.ui.tool = Tool::Move; + assert!(app.ui.view.show.smart_guides); + app.ui.extras.snap = false; + app.ui.tool_options.move_auto_select = false; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m); + assert_eq!(mover_bounds(&app).x0, 303); + assert!(app.prefs_rt.snap_lines.is_empty()); +} + +#[test] +fn marquee_corner_snaps_to_guides_and_document_edges() { + let mut app = app_with_box(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 120})).unwrap(); + app.ui.tool = Tool::RectMarquee; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 3.0, y: 2.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 116.0, y: 50.0 }, m); + let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!((sel.x0, sel.y0, sel.x1), (0, 0, 120)); + // Zoomed in to 800%, 4 px is beyond the 1 px threshold: no snap. + app.ui.views[0].zoom = 8.0; + // (Deselect first: a drag starting inside the selection would move it.) + app.run("select.deselect", json!({})).unwrap(); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 116.0, y: 50.0 }, m); + let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!(sel.x1, 116); +} + +/// Space while drawing a marquee, Object Selection box or shape moves it: its edges snap, not +/// only the corner under the pointer; with Space released the corner snaps again. +#[test] +fn drawing_repositioned_with_space_snaps_its_edges() { + for tool in [Tool::RectMarquee, Tool::EllipseMarquee, Tool::ObjectSelection, Tool::Rectangle, Tool::Line] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.views[0].zoom = 1.0; + app.ui.views[0].fit_pending = false; + app.run("view.newGuide", json!({"orientation": "vertical", "position": 220})).unwrap(); + app.ui.tool = tool; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 100.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 150.0, y: 140.0, pressure: 1.0 }, m); + // Space down: the 50 × 40 box moves so its left edge lands 4 px short of the guide; + // the pointer corner (x = 266) is near nothing. + app.drag.as_mut().unwrap().reposition = true; + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 266.0, y: 140.0, pressure: 1.0 }, m); + assert_eq!(app.drag.as_ref().unwrap().start, [220.0, 100.0], "{tool:?}"); + if tool != Tool::RectMarquee { + continue; + } + // Space up: sizing resumes and the corner snaps to the document's right edge. + app.drag.as_mut().unwrap().reposition = false; + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 397.0, y: 140.0 }, m); + let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!((sel.x0, sel.y0, sel.x1, sel.y1), (220, 100, 400, 140)); + } +} + +/// Dragging inside a selection with a marquee or lasso moves the outline (⌘ cuts the pixels +/// into a floating piece): its bounds snap, not the pointer. +#[test] +fn selection_drag_snaps_the_selection_bounds() { + for (tool, cut) in [(Tool::RectMarquee, false), (Tool::Lasso, false), (Tool::RectMarquee, true)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + app.sync_views(); + app.ui.views[0].zoom = 1.0; + app.ui.views[0].fit_pending = false; + app.run("view.newGuide", json!({"orientation": "vertical", "position": 220})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 50, "height": 40})).unwrap(); + app.ui.tool = tool; + let m = if cut { egui::Modifiers { mac_cmd: true, command: true, ..Default::default() } } else { egui::Modifiers::NONE }; + // The outline's right edge lands 2 px short of the guide (x = 218); the pointer ends + // 7 px from the document centre (x = 200), which it must not snap to. + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 125.0, y: 117.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 193.0, y: 117.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 193.0, y: 117.0 }, m); + let st = app.session.active().unwrap(); + let off = photocraft_engine::float_cmds::floating(st).map_or((0, 0), |f| f.offset); + let sel = st.doc.selection.as_ref().unwrap().content_bounds().translate(off.0, off.1); + assert_eq!((sel.x0, sel.y0, sel.x1, sel.y1), (170, 100, 220, 140), "{tool:?} cut: {cut}"); + } +} + +#[test] +fn distort_corner_snaps_from_positive_grab_offset() { + distort_corner_snaps_from_grab_offset([4.0, 3.0]); +} + +#[test] +fn distort_corner_snaps_from_negative_grab_offset() { + distort_corner_snaps_from_grab_offset([-4.0, -3.0]); +} + +fn distort_corner_snaps_from_grab_offset(offset: [f64; 2]) { + let mut app = app_with_box(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 110})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 30})).unwrap(); + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "edit.transform.distort", json!({})).unwrap(); + let before = app.ui.transform.as_ref().unwrap().quad; + let press = [before[1][0] + offset[0], before[1][1] + offset[1]]; + // The proposed corner is (108, 32); the pointer keeps its original grab offset. + let release = [108.0 + offset[0], 32.0 + offset[1]]; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: press[0], y: press[1], pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: release[0], y: release[1], pressure: 1.0 }, m); + let moved = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(moved[1], [110.0, 30.0], "grab offset {offset:?}"); + assert_eq!([moved[0], moved[2], moved[3]], [before[0], before[2], before[3]]); + assert!(app.prefs_rt.snap_lines.iter().any(|l| l.vertical && l.pos == 110.0 && l.kind == SnapKind::Guide)); + assert!(app.prefs_rt.snap_lines.iter().any(|l| !l.vertical && l.pos == 30.0 && l.kind == SnapKind::Guide)); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: release[0], y: release[1] }, m); + assert_eq!(app.ui.transform.as_ref().unwrap().quad, moved); + assert!(app.prefs_rt.snap.is_none() && app.prefs_rt.snap_lines.is_empty()); +} + +fn app_with_corner_guides(menu: &str) -> PhotocraftApp { + let mut app = app_with_box(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 110})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 30})).unwrap(); + crate::menus::invoke(&mut app, &egui::Context::default(), menu, json!({})).unwrap(); + app +} + +fn move_corner(app: &mut PhotocraftApp, index: usize, offset: [f64; 2], end: [f64; 2], down: egui::Modifiers, moving: egui::Modifiers) { + let corner = app.ui.transform.as_ref().unwrap().quad[index]; + crate::canvas::tool_event(app, ToolEvent::Down { x: corner[0] + offset[0], y: corner[1] + offset[1], pressure: 1.0 }, down); + crate::canvas::tool_event(app, ToolEvent::Move { x: end[0] + offset[0], y: end[1] + offset[1], pressure: 1.0 }, moving); +} + +#[test] +fn distort_corner_respects_guide_axes_tolerance_and_toggles() { + for (snap, shown, guides, end, expected) in [ + (true, true, true, [108.0, 20.0], [110.0, 20.0]), + (true, true, true, [100.0, 32.0], [100.0, 30.0]), + (true, true, true, [100.0, 20.0], [100.0, 20.0]), + (false, true, true, [108.0, 32.0], [108.0, 32.0]), + (true, false, true, [108.0, 32.0], [108.0, 32.0]), + (true, true, false, [108.0, 32.0], [108.0, 32.0]), + ] { + let mut app = app_with_corner_guides("edit.transform.distort"); + app.ui.extras.snap = snap; + app.ui.extras.guides = shown; + app.ui.view.snap_to.guides = guides; + move_corner(&mut app, 1, [4.0, 3.0], end, egui::Modifiers::NONE, egui::Modifiers::NONE); + assert_eq!(app.ui.transform.as_ref().unwrap().quad[1], expected, "Snap: {snap}, shown: {shown}, guides: {guides}, proposed: {end:?}"); + assert!(app.prefs_rt.snap_lines.iter().all(|line| (if line.vertical { expected[0] } else { expected[1] } - line.pos).abs() < 1e-6)); + } +} + +#[test] +fn free_transform_command_corner_snaps_with_the_real_grab_offset() { + let windows = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + let mac = egui::Modifiers { command: true, mac_cmd: true, ..egui::Modifiers::NONE }; + for m in [windows, mac] { + for offset in [[4.0, 3.0], [-4.0, -3.0], [0.0, 0.0]] { + for down in [m, egui::Modifiers::NONE] { + let mut app = app_with_corner_guides("edit.freeTransform"); + let before = app.ui.transform.as_ref().unwrap().quad; + move_corner(&mut app, 1, offset, [108.0, 32.0], down, m); + let moved = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(moved[1], [110.0, 30.0], "{m:?}, press {down:?}, offset {offset:?}"); + assert_eq!([moved[0], moved[2], moved[3]], [before[0], before[2], before[3]]); + assert_eq!(app.prefs_rt.snap_lines.len(), 2); + assert!(!app.prefs_rt.snap.as_ref().unwrap().disabled); + } + } + } +} + +#[test] +fn control_alone_still_disables_snapping_in_menu_distort() { + let mut app = app_with_corner_guides("edit.transform.distort"); + let ctrl = egui::Modifiers { ctrl: true, ..egui::Modifiers::NONE }; + move_corner(&mut app, 1, [4.0, 3.0], [108.0, 32.0], ctrl, ctrl); + assert_eq!(app.ui.transform.as_ref().unwrap().quad[1], [108.0, 32.0]); + assert!(app.prefs_rt.snap.as_ref().unwrap().disabled); + assert!(app.prefs_rt.snap_lines.is_empty()); +} + +#[test] +fn skew_control_corner_keeps_its_axis_instead_of_snapping_as_distort() { + let ctrl = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + for legacy in [false, true] { + for (menu, expected, snapping) in [ + ("edit.transform.skew", [[40.0, 40.0], [108.0, 40.0], [90.0, 80.0], [40.0, 80.0]], false), + ("edit.transform.distort", [[40.0, 40.0], [110.0, 30.0], [90.0, 80.0], [40.0, 80.0]], true), + ("edit.transform.perspective", [[22.0, 40.0], [108.0, 40.0], [90.0, 80.0], [40.0, 80.0]], false), + ] { + let mut app = app_with_corner_guides(menu); + app.run("prefs.set", json!({"path": "general.useLegacyFreeTransform", "value": legacy})).unwrap(); + let doc = app.session.active().unwrap().doc.clone(); + let past = app.session.active().unwrap().history.past_len(); + let prefs = serde_json::to_value(app.session.prefs()).unwrap(); + // Ctrl arrives after the raw, off-centre press, as on the actual canvas path. + move_corner(&mut app, 1, [4.0, 3.0], [108.0, 32.0], egui::Modifiers::NONE, ctrl); + assert_eq!(app.ui.transform.as_ref().unwrap().quad, expected, "{menu}, legacy {legacy}"); + assert_eq!(!app.prefs_rt.snap.as_ref().unwrap().disabled, snapping, "{menu}"); + assert_eq!(app.prefs_rt.snap_lines.len(), if snapping { 2 } else { 0 }, "{menu}"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 112.0, y: 35.0 }, ctrl); + assert_eq!(app.ui.transform.as_ref().unwrap().quad, expected, "release preserves {menu}"); + crate::transform_tool::cancel(&mut app); + assert!(app.ui.transform.is_none() && app.prefs_rt.snap.is_none() && app.prefs_rt.snap_lines.is_empty()); + assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &doc), "cancel preserves {menu}'s document"); + assert_eq!(app.session.active().unwrap().history.past_len(), past, "cancel preserves {menu}'s history"); + assert_eq!(serde_json::to_value(app.session.prefs()).unwrap(), prefs, "{menu} does not change preferences"); + } + } +} + +#[test] +fn skew_control_corner_on_editable_type_slides_the_whole_edge() { + let ctrl = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + for legacy in [false, true] { + let mut app = app_with_box(); + app.run("type.create", json!({"x": 40, "y": 60, "text": "Hello"})).unwrap(); + app.run("prefs.set", json!({"path": "general.useLegacyFreeTransform", "value": legacy})).unwrap(); + crate::menus::invoke(&mut app, &egui::Context::default(), "edit.transform.skew", json!({})).unwrap(); + let before = app.ui.transform.as_ref().unwrap().quad; + let end = [before[1][0] + 18.0, before[1][1] - 8.0]; + let doc = app.session.active().unwrap().doc.clone(); + let past = app.session.active().unwrap().history.past_len(); + move_corner(&mut app, 1, [2.0, 1.0], end, egui::Modifiers::NONE, ctrl); + let mut expected = before; + expected[0][0] += 18.0; + expected[1][0] += 18.0; + assert_eq!(app.ui.transform.as_ref().unwrap().quad, expected, "type remains affine, legacy {legacy}"); + assert!(app.prefs_rt.snap.as_ref().unwrap().disabled && app.prefs_rt.snap_lines.is_empty()); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: end[0] + 2.0, y: end[1] + 1.0 }, ctrl); + assert_eq!(app.ui.transform.as_ref().unwrap().quad, expected, "release preserves the type's edge"); + crate::transform_tool::cancel(&mut app); + assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &doc)); + assert_eq!(app.session.active().unwrap().history.past_len(), past); + } +} + +#[test] +fn transform_non_distortion_gestures_keep_control_snap_override() { + let ctrl = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + let perspective = egui::Modifiers { alt: true, shift: true, ..ctrl }; + for (menu, press, m) in [ + ("edit.freeTransform", [65.0, 40.0], ctrl), // edge + ("edit.freeTransform", [65.0, 60.0], ctrl), // pivot + ("edit.freeTransform", [55.0, 52.0], ctrl), // inside + ("edit.freeTransform", [90.0, 40.0], perspective), + ("edit.transform.perspective", [90.0, 40.0], ctrl), + ("edit.transform.warp", [90.0, 40.0], ctrl), + ("edit.contentAwareScale", [90.0, 40.0], ctrl), + ] { + let mut app = app_with_corner_guides(menu); + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: press[0], y: press[1], pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 108.0, y: 32.0, pressure: 1.0 }, m); + assert!(app.prefs_rt.snap.as_ref().unwrap().disabled, "{menu}, {press:?}, {m:?}"); + assert!(app.prefs_rt.snap_lines.is_empty(), "{menu}, {press:?}"); + } +} + +#[test] +fn editable_type_refusal_keeps_control_snap_override() { + let mut app = app_with_box(); + app.run("type.create", json!({"x": 40, "y": 60, "text": "Hello"})).unwrap(); + crate::menus::invoke(&mut app, &egui::Context::default(), "edit.freeTransform", json!({})).unwrap(); + let corner = app.ui.transform.as_ref().unwrap().quad[1]; + app.run("view.newGuide", json!({"orientation": "vertical", "position": corner[0] + 20.0})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": corner[1] - 10.0})).unwrap(); + let ctrl = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + move_corner(&mut app, 1, [0.0, 0.0], [corner[0] + 18.0, corner[1] - 8.0], ctrl, ctrl); + assert!(app.prefs_rt.snap.as_ref().unwrap().disabled); + assert!(app.prefs_rt.snap_lines.is_empty()); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(q[0][1], q[1][1], "editable type still scales, rather than distorting"); +} + +#[test] +fn a_foldover_constraint_never_claims_a_guide_alignment() { + let mut app = app_with_corner_guides("edit.transform.distort"); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 100})).unwrap(); + let before = app.ui.transform.as_ref().unwrap().quad; + move_corner(&mut app, 0, [4.0, 3.0], [108.0, 98.0], egui::Modifiers::NONE, egui::Modifiers::NONE); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_ne!(q[0], [110.0, 100.0], "the guide intersection would fold the quad"); + assert_eq!([q[1], q[2], q[3]], [before[1], before[2], before[3]]); + for i in 0..4 { + let (a, b, c) = (q[i], q[(i + 1) % 4], q[(i + 2) % 4]); + assert!((b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]) > 0.0, "still convex: {q:?}"); + } + assert!(app.prefs_rt.snap_lines.is_empty(), "{:?}", app.prefs_rt.snap_lines); +} + +#[test] +fn a_snapped_distort_commits_one_undoable_transform() { + let mut app = app_with_corner_guides("edit.transform.distort"); + let before_doc = app.session.active().unwrap().doc.clone(); + let before_bounds = mover_bounds(&app); + let past = app.session.active().unwrap().history.past_len(); + move_corner(&mut app, 1, [4.0, 3.0], [108.0, 32.0], egui::Modifiers::NONE, egui::Modifiers::NONE); + assert!(std::sync::Arc::ptr_eq(&app.session.active().unwrap().doc, &before_doc), "preview does not edit the document"); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 112.0, y: 35.0 }, egui::Modifiers::NONE); + crate::transform_tool::commit(&mut app); + assert!(!app.ui.status_error, "{}", app.ui.status); + let after_bounds = mover_bounds(&app); + assert_ne!(after_bounds, before_bounds); + assert_eq!(app.session.active().unwrap().history.past_len(), past + 1); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(mover_bounds(&app), before_bounds); + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(mover_bounds(&app), after_bounds); +} + +#[test] +fn transform_corner_press_is_not_adjusted_near_an_original_guide() { + for menu in ["edit.transform.distort", "edit.freeTransform"] { + let mut app = app_with_corner_guides(menu); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 95})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 45})).unwrap(); + let raw = ToolEvent::Down { x: 94.0, y: 43.0, pressure: 1.0 }; + let ToolEvent::Down { x, y, .. } = filter_event(&mut app, raw, egui::Modifiers::NONE) else { panic!("still a press") }; + assert_eq!([x, y], [94.0, 43.0], "{menu}: filtering must preserve the handle press"); + } +} + +#[test] +fn corner_near_original_guides_keeps_the_raw_press_in_the_canvas_path() { + for (menu, m) in + [("edit.transform.distort", egui::Modifiers::NONE), ("edit.freeTransform", egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE })] + { + let mut app = app_with_corner_guides(menu); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 95})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 45})).unwrap(); + let before = app.ui.transform.as_ref().unwrap().quad; + // The command modifier is added after Down, including in Free Transform. + move_corner(&mut app, 1, [4.0, 3.0], [93.0, 43.0], egui::Modifiers::NONE, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(q[1], [95.0, 45.0], "{menu}"); + assert_eq!([q[0], q[2], q[3]], [before[0], before[2], before[3]]); + } +} + +#[test] +fn corner_distortion_does_not_snap_to_guides_hidden_by_view_flags() { + for menu in ["edit.transform.distort", "edit.freeTransform"] { + for hide_extras in [false, true] { + let mut app = app_with_corner_guides(menu); + if hide_extras { + app.ui.view.extras = false; + } else { + app.ui.view.show.canvas_guides = false; + } + let m = egui::Modifiers { command: true, ctrl: true, ..egui::Modifiers::NONE }; + move_corner(&mut app, 1, [4.0, 3.0], [108.0, 32.0], m, m); + assert_eq!(app.ui.transform.as_ref().unwrap().quad[1], [108.0, 32.0], "{menu}, hide Extras: {hide_extras}"); + assert!(app.prefs_rt.snap_lines.is_empty()); + } + } +} + +#[test] +fn marquee_and_corner_snapping_share_the_canvas_guide_visibility() { + for (extras, canvas_guides, shown) in [(true, true, true), (false, true, false), (true, false, false)] { + let mut app = app_with_box(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 110})).unwrap(); + app.run("view.newGuide", json!({"orientation": "horizontal", "position": 30})).unwrap(); + app.ui.tool = Tool::RectMarquee; + // Isolate guides: the mover's y=40 edge would otherwise be a valid fallback target. + app.ui.view.snap_to.layers = false; + app.ui.view.snap_to.document_bounds = false; + app.ui.view.snap_to.grid = false; + app.ui.view.extras = extras; + app.ui.view.show.canvas_guides = canvas_guides; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 12.0, y: 12.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 108.0, y: 32.0 }, m); + let rect = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!((rect.x0, rect.y0), (12, 12)); + assert_eq!((rect.x1, rect.y1), if shown { (110, 30) } else { (108, 32) }, "Extras: {extras}, Canvas Guides: {canvas_guides}"); + } +} + +#[test] +fn distort_corner_does_not_snap_to_layer_edges() { + let mut app = app_with_box(); + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "edit.transform.distort", json!({})).unwrap(); + let q = app.ui.transform.as_ref().unwrap().quad; + let m = egui::Modifiers::NONE; + // Drag the top-right corner to 2 px short of the target's right edge (x = 350). + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: q[1][0], y: q[1][1], pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 348.0, y: q[1][1] }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(q[1][0], 348.0, "the corner stays where it was dropped"); + assert!(app.prefs_rt.snap_lines.is_empty()); +} + +#[test] +fn free_transform_drags_and_handles_snap() { + let mut app = app_with_box(); + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).unwrap(); + assert!(app.ui.transform.is_some()); + let m = egui::Modifiers::NONE; + // Drag from inside the box (40..90 × 40..80) so its left edge lands 2 px from x = 300. + // (Away from the centre, which holds the reference point.) + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 50.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 312.0, y: 50.0 }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert!((q[0][0] - 300.0).abs() < 1e-6, "{q:?}"); + // The right-edge handle snaps to the target's right edge (x = 350). + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: q[1][0], y: (q[1][1] + q[2][1]) / 2.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 353.0, y: 60.0 }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert!((q[1][0] - 350.0).abs() < 1e-6, "{q:?}"); + crate::transform_tool::commit(&mut app); + assert_eq!(mover_bounds(&app).x0, 300); +} + +/// #5751: a Rectangle drawn on a 1080 px canvas, then resized with Free Transform under the +/// Move tool. With Snap Vector Tools and Transforms to Pixel Grid on (the default) the box's +/// edges stay on whole pixels: the grab offset and a proportional scale used to leave them at +/// 20.6 and 1040.4. Off, the box follows the pointer exactly. +#[test] +fn free_transform_of_a_shape_keeps_whole_pixels() { + for grid in [true, false] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 1080, "height": 1080, "resolution": 72})).unwrap(); + app.run("prefs.set", json!({"path": "tools.snapVectorToolsAndTransformsToPixelGrid", "value": grid})).unwrap(); + app.sync_views(); + app.ui.views[0].zoom = 0.6; + app.ui.views[0].fit_pending = false; + app.ui.tool = Tool::Rectangle; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 40.0, y: 31.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 300.0, y: 700.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 481.0, y: 1000.0 }, m); + app.ui.tool = Tool::Move; + crate::menus::invoke(&mut app, &egui::Context::default(), "edit.freeTransform", json!({})).unwrap(); + let whole = |q: [[f64; 2]; 4]| q.iter().flatten().all(|v| v.fract() == 0.0); + // The left edge handle, grabbed 1.3 px inside, dropped away from any snap target. + let q = app.ui.transform.as_ref().unwrap().quad; + let mid_y = (q[0][1] + q[3][1]) / 2.0; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: q[0][0] + 1.3, y: mid_y, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Move { x: 60.0, y: mid_y, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 80.6, y: mid_y }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(whole(q), grid, "grid {grid}: left edge {q:?}"); + // The bottom-right corner scaled proportionally (the default), off any snap target. + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: q[2][0], y: q[2][1], pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 450.4, y: 900.1 }, m); + let q = app.ui.transform.as_ref().unwrap().quad; + assert_eq!(whole(q), grid, "grid {grid}: corner {q:?}"); + crate::transform_tool::commit(&mut app); + let st = app.session.active().unwrap(); + let photocraft_doc::LayerContent::Shape(sh) = &st.doc.layer(st.active_layer.unwrap()).unwrap().content else { panic!("a shape layer") }; + let (x0, y0, x1, y1) = sh.path.control_bounds().unwrap(); + if grid { + for v in [x0, y0, x1, y1] { + assert!((v - v.round()).abs() < 1e-6, "committed shape {:?}", (x0, y0, x1, y1)); + } + } + } +} + +#[test] +fn guides_snap_to_layer_edges() { + let mut app = app_with_box(); + assert_eq!(snap_guide(&mut app, true, 297.0), 300.0); + assert_eq!(snap_guide(&mut app, false, 150.0), 150.0, "document centre"); + assert_eq!(snap_guide(&mut app, false, 170.0), 170.0); + // Dragging an existing guide with the Move tool while a selection is active still snaps to the selection. + app.run("view.newGuide", json!({"orientation": "vertical", "position": 10})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 50, "height": 40})).unwrap(); + app.ui.tool = Tool::Move; + begin(&mut app, [10.0, 50.0], egui::Modifiers::NONE); + assert_eq!(apply(&mut app, [98.0, 50.0], egui::Modifiers::NONE)[0], 100.0); + app.ui.extras.snap = false; + assert_eq!(snap_guide(&mut app, true, 297.0), 297.0); +} + +/// The cached ruler-guide targets follow the Snap To options, not just document edits. +#[test] +fn guide_targets_follow_snap_to_options() { + let mut app = app_with_box(); + app.run( + "prefs.set", + json!({"values": {"guidesGridAndSlices.gridlineEvery": 100, "guidesGridAndSlices.gridUnit": "pixels", "guidesGridAndSlices.subdivisions": 4}}), + ) + .unwrap(); + app.ui.extras.grid = false; + assert_eq!(snap_guide(&mut app, true, 23.0), 23.0, "grid hidden"); + app.ui.extras.grid = true; + app.ui.view.snap_to.grid = true; + assert_eq!(snap_guide(&mut app, true, 23.0), 25.0, "grid shown"); + app.ui.extras.grid = false; + app.ui.view.snap_to.layers = false; + assert_eq!(snap_guide(&mut app, true, 297.0), 297.0, "Snap To Layers off"); +} + +#[test] +fn percentage_grid_snaps_at_separate_axis_intervals() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 1280, "height": 720})).unwrap(); + app.ui.extras.grid = true; + app.ui.view.snap_to.grid = true; + app.run( + "prefs.set", + json!({"values": {"guidesGridAndSlices.gridlineEvery": 20, "guidesGridAndSlices.gridUnit": "percent", "guidesGridAndSlices.subdivisions": 2}}), + ) + .unwrap(); + let opts = options(&app); + assert_eq!(opts.grid_step, [128.0, 72.0]); + let targets = SnapTargets::from_document(&app.session.active().unwrap().doc, &opts, &[]); + assert_eq!(targets.snap_point([127.0, 71.0], 2.0).0, [128.0, 72.0]); +} + +#[test] +fn grid_snapping_uses_preferences() { + let mut app = app_with_box(); + app.ui.extras.grid = true; + app.run( + "prefs.set", + json!({"values": {"guidesGridAndSlices.gridlineEvery": 100, "guidesGridAndSlices.gridUnit": "pixels", "guidesGridAndSlices.subdivisions": 4}}), + ) + .unwrap(); + assert_eq!(options(&app).grid_step, [25.0; 2]); + app.ui.tool = Tool::RectMarquee; + let m = egui::Modifiers::NONE; + crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 152.0, y: 127.0, pressure: 1.0 }, m); + crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 199.0, y: 176.0 }, m); + let sel = app.session.active().unwrap().doc.selection.as_ref().unwrap().content_bounds(); + assert_eq!((sel.x0, sel.y0, sel.x1, sel.y1), (150, 125, 200, 175)); +} diff --git a/crates/ui-egui/src/canvas/start_screen.rs b/crates/ui-egui/src/canvas/start_screen.rs new file mode 100644 index 0000000000..e8b6633e5a --- /dev/null +++ b/crates/ui-egui/src/canvas/start_screen.rs @@ -0,0 +1,177 @@ +use super::tabs::{open_on_double_click, paint_dots}; +use super::{PhotocraftApp, Rect, Sense}; + +/// The Home screen's card is this wide when the canvas has room, and as wide as the canvas when it doesn't. +const HOME_CARD_WIDTH: f32 = 460.0; + +/// Least width of each of the Home screen's New document and Open buttons. +const HOME_BUTTON_WIDTH: f32 = 190.0; + +/// Space between the Home screen's two buttons. +const HOME_BUTTON_GAP: f32 = 12.0; + +pub(super) fn start_screen(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + let t = crate::theme::Tokens::get(ui.ctx()); + let area = ui.available_rect_before_wrap(); + paint_dots(ui, area, app.session.prefs().interface.show_canvas_dots); + open_on_double_click(app, ui, area); + // File › Open Recent, newest first (on the web there are no paths to reopen). + let show_recent = app.session.prefs().interface.show_home_recent_files; + let recent: Vec = + if cfg!(target_arch = "wasm32") || !show_recent { Vec::new() } else { app.ui.recent_files.iter().take(HOME_RECENT).cloned().collect() }; + let recent_h = if recent.is_empty() { 0.0 } else { 34.0 + recent.len() as f32 * HOME_RECENT_ROW }; + // A tiling window manager can give the canvas less room than the card wants: shrink the card + // to fit, and stack the buttons when they don't fit side by side. + let new_label = crate::shortcuts::command_label(app, "New document…", "file.new"); + let open_label = crate::shortcuts::command_label(app, "Open…", "file.open"); + let card_width = HOME_CARD_WIDTH.min(area.width()); + let button_width = HOME_BUTTON_WIDTH.max(crate::widgets::button_width(ui, &[&new_label, &open_label])); + let side_by_side = 2.0 * button_width + HOME_BUTTON_GAP <= card_width; + // Stacked buttons take one more row. + let stacked_h = if side_by_side { 0.0 } else { 44.0 }; + let card = Rect::from_center_size(area.center(), egui::vec2(card_width, 330.0 + recent_h + stacked_h)); + ui.scope_builder(egui::UiBuilder::new().max_rect(card), |ui| { + ui.vertical_centered(|ui| { + ui.horizontal(|ui| { + let title = ui.painter().layout_no_wrap(tl!("PhotoCraft").into(), crate::theme::semibold(38.0), t.text); + let by = ui.painter().layout_no_wrap(tl!("open source").into(), egui::FontId::proportional(13.0), t.text_faint); + let total = title.size().x + by.size().x + 10.0; + ui.add_space(((card.width() - total) / 2.0).max(0.0)); + let (r, _) = ui.allocate_exact_size(title.size(), Sense::hover()); + ui.painter().galley(r.min, title, t.text); + let (r2, _) = ui.allocate_exact_size(egui::vec2(by.size().x + 10.0, r.height()), Sense::hover()); + ui.painter().galley(egui::pos2(r2.left() + 10.0, r.bottom() - by.size().y - 8.0), by, t.text_faint); + }); + ui.add_space(6.0); + ui.label(egui::RichText::new(tl!("Create a new document or open an existing file.")).color(t.text_dim).size(14.0)); + ui.add_space(22.0); + let (mut new_clicked, mut open_clicked) = (false, false); + if side_by_side { + ui.horizontal(|ui| { + ui.add_space(((card.width() - 2.0 * button_width - HOME_BUTTON_GAP) / 2.0).max(0.0)); + ui.spacing_mut().item_spacing.x = HOME_BUTTON_GAP; + new_clicked = crate::widgets::primary_button(ui, &new_label, button_width).clicked(); + open_clicked = crate::widgets::secondary_button(ui, &open_label, button_width).clicked(); + }); + } else { + new_clicked = crate::widgets::primary_button(ui, &new_label, button_width).clicked(); + ui.add_space(HOME_BUTTON_GAP); + open_clicked = crate::widgets::secondary_button(ui, &open_label, button_width).clicked(); + } + if new_clicked { + let fields = app.new_document_fields(); + app.ui.open_dialog(crate::state::DialogKind::NewDocument, fields); + } + if open_clicked { + let _ = app.open_dialog_file(); + } + ui.add_space(22.0); + ui.horizontal(|ui| { + let msg = start_screen_drop_hint(app); + let g = ui.painter().layout_no_wrap(msg.to_string(), egui::FontId::proportional(12.5), t.text_faint); + ui.add_space(((card.width() - g.size().x - 24.0) / 2.0).max(0.0)); + let (r, _) = ui.allocate_exact_size(egui::vec2(18.0, 18.0), Sense::hover()); + crate::icons::paint(ui, r, "image", 15.0, t.text_faint); + ui.label(egui::RichText::new(msg).color(t.text_faint)); + }); + if !recent.is_empty() { + ui.add_space(22.0); + home_recent(app, ui, &recent); + } + ui.add_space(26.0); + crate::links::discord_button(app, ui, 190.0); + ui.add_space(10.0); + crate::links::link_row(app, ui); + }); + }); +} + +/// Wayland has no native file drops (winit 0.30, #386), so the hint offers File › Open and paste. +pub(super) fn start_screen_drop_hint(app: &PhotocraftApp) -> std::borrow::Cow<'static, str> { + if app.services.is_wayland { + crate::i18n::fmt(tl!("Use File › Open, or paste a copied image with {paste}."), &[("paste", &crate::notices::paste_hint(app))]).into() + } else { + tl!("Drop an image or PSD anywhere to open it.").into() + } +} + +/// Recent files listed on the Home screen. +const HOME_RECENT: usize = 6; + +/// Height of one Home-screen recent-file row. +const HOME_RECENT_ROW: f32 = 30.0; + +/// The Home screen's "Recent" list: file name, its folder on the right, click to open. The +/// hovered row shows a × that takes it off the list, and right-click offers Remove from Recent +/// and Clear Recent Files (#2691). +fn home_recent(app: &mut PhotocraftApp, ui: &mut egui::Ui, recent: &[String]) { + let t = crate::theme::Tokens::get(ui.ctx()); + let width = 380.0; + ui.horizontal(|ui| { + // Line the heading up with the file icons. + ui.add_space(((ui.available_width() - width) / 2.0).max(0.0) + 8.0); + ui.label(egui::RichText::new(tl!("Recent")).font(crate::theme::semibold(12.5)).color(t.text_dim)); + }); + ui.add_space(4.0); + let mut open = None; + let mut remove = None; + let mut clear = false; + for path in recent { + let name = crate::file_open::display_name(path); + let folder = std::path::Path::new(path).parent().map(|d| d.to_string_lossy().into_owned()).unwrap_or_default(); + let (row, resp) = ui.allocate_exact_size(egui::vec2(width, HOME_RECENT_ROW), Sense::click()); + // The × sits on top of the row, so the row counts as hovered while the pointer is on it. + let hovered = ui.rect_contains_pointer(row); + if hovered { + ui.painter().rect_filled(row, t.radius_sm, t.hover); + } + let close = Rect::from_center_size(egui::pos2(row.right() - 16.0, row.center().y), egui::vec2(22.0, 22.0)); + let right = if hovered { close.left() - 4.0 } else { row.right() - 10.0 }; + let icon = Rect::from_center_size(egui::pos2(row.left() + 16.0, row.center().y), egui::vec2(16.0, 16.0)); + crate::icons::paint(ui, icon, "file", 14.0, t.text_dim); + let x = row.left() + 32.0; + let name_g = crate::tab_strip::elided(ui, &name, egui::FontId::proportional(12.5), t.text, 170.0); + let name_w = name_g.size().x; + ui.painter().galley(egui::pos2(x, row.center().y - name_g.size().y / 2.0), name_g, t.text); + let folder_max = (right - (x + name_w + 14.0)).max(0.0); + if folder_max > 20.0 && !folder.is_empty() { + let fg = crate::tab_strip::elided(ui, &folder, egui::FontId::proportional(11.5), t.text_faint, folder_max); + ui.painter().galley(egui::pos2(right - fg.size().x, row.center().y - fg.size().y / 2.0), fg, t.text_faint); + } + resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &name)); + let resp = resp.on_hover_text(path).on_hover_cursor(egui::CursorIcon::PointingHand); + if hovered { + let x = ui.interact(close, resp.id.with("remove"), Sense::click()); + let label = crate::i18n::fmt(tl!("Remove {name} from Recent"), &[("name", &name)]); + x.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, &label)); + let color = crate::icons::button_chrome(ui, close, false, x.hovered()); + crate::icons::paint(ui, close, "x", 12.0, color); + if x.on_hover_text(tl!("Remove from Recent")).clicked() { + remove = Some(path.clone()); + } + } + if resp.clicked() && remove.is_none() { + open = Some(path.clone()); + } + resp.context_menu(|ui| { + if ui.button(tl!("Remove from Recent")).clicked() { + remove = Some(path.clone()); + ui.close(); + } + if ui.button(tl!("Clear Recent Files")).clicked() { + clear = true; + ui.close(); + } + }); + } + if clear { + app.clear_recent(); + } else if let Some(path) = remove { + app.remove_recent(&path); + } + if let Some(path) = open + && let Err(e) = app.open_path(&path) + { + app.open_failed(&crate::file_open::display_name(&path), &e); + } +} diff --git a/crates/ui-egui/src/canvas/strokes.rs b/crates/ui-egui/src/canvas/strokes.rs new file mode 100644 index 0000000000..32e9b19fcc --- /dev/null +++ b/crates/ui-egui/src/canvas/strokes.rs @@ -0,0 +1,715 @@ +use super::display::{canvas_display, display_image}; +use super::drag::Drag; +use super::textures::{canvas_texture_options, shown_as_document}; +use super::view_xform::ViewXform; +use super::{Color32, DRect, PhotocraftApp, Pos2, Stroke, Tool, json, pos2}; + +pub(crate) struct ClonePreviewCache { + pub(super) key: String, + /// The brush a clipped overlay took its coverage from: a hardness or tip change redraws it. + pub(super) brush: Option, + /// The document area the texture covers. + pub(super) rect: DRect, + pub(super) texture: egui::TextureHandle, +} + +/// The most document pixels one dab's coverage is rendered over for the overlay: a 1000 px brush, +/// the largest the overlay draws, at hardness 0 reaches about 1.73 × its radius (`tip_reach`). +const CLONE_PREVIEW_MAX_PIXELS: u64 = 1800 * 1800; + +/// The largest side `retouch_cmds::clone_preview_periodic` samples, for the overlay's area and for +/// the source area it reads (`Mapping::source_rect`: the overlay's, mapped, with a 2 px apron). +const CLONE_PREVIEW_MAX_SIDE: u32 = 1024; + +/// The coverage of one dab of `brush` at `at`, as the brush engine paints it with opacity and flow +/// at full: its tip, angle, roundness and hardness (a soft tip reaches past its radius). The area +/// it covers and its `width × height` coverage values in 0..1. A very large soft tip, or a source +/// the Clone Source panel scales down (which reads a larger area than it covers), is cropped +/// around `at` to the sampling budget, keeping the middle of the dab. +fn clone_dab_coverage( + brush: &photocraft_paint::BrushSettings, + at: [f64; 2], + map: &photocraft_engine::presets::clone_source::Mapping, +) -> Option<(DRect, Vec)> { + let brush = photocraft_paint::BrushSettings { opacity: 1.0, flow: 1.0, erase: false, ..brush.clone() }; + let stroke = photocraft_paint::Stroke { brush, points: vec![photocraft_paint::StrokePoint::new(at[0], at[1], 1.0)] }; + let (rect, cov) = photocraft_paint::retouch::stroke_coverage_within(&stroke, CLONE_PREVIEW_MAX_PIXELS)?; + let fits = |r: DRect| r.width() <= CLONE_PREVIEW_MAX_SIDE && r.height() <= CLONE_PREVIEW_MAX_SIDE; + let (cx, cy) = (at[0].floor() as i32, at[1].floor() as i32); + let around = |half: i32| rect.intersect(&DRect::new(cx.saturating_sub(half), cy.saturating_sub(half), cx.saturating_add(half), cy.saturating_add(half))); + // The source area scales with the overlay's: shrink by what overflows, a few times for the + // apron's rounding. + let mut half = i32::try_from((CLONE_PREVIEW_MAX_SIDE - 4) / 2).unwrap_or(510); + let mut window = around(half); + for _ in 0..8 { + let src = map.source_rect(window); + if fits(window) && fits(src) { + break; + } + let over = f64::from(src.width().max(src.height())) / f64::from(CLONE_PREVIEW_MAX_SIDE - 4); + half = ((f64::from(half) / over.max(1.0)).floor() as i32).saturating_sub(2).max(1); + window = around(half); + } + if window.is_empty() || !fits(window) || !fits(map.source_rect(window)) { + return None; + } + if window == rect { + return Some((rect, cov)); + } + let stride = rect.width() as usize; + let mut cropped = Vec::with_capacity(window.width() as usize * window.height() as usize); + for y in window.y0..window.y1 { + let row = (y - rect.y0) as usize * stride; + let from = row + (window.x0 - rect.x0) as usize; + cropped.extend_from_slice(cov.get(from..from + window.width() as usize)?); + } + Some((window, cropped)) +} + +/// A transient source image beneath the brush outline; never part of the document render. The +/// Clone Source panel drives the Clone Stamp and the Healing Brush alike, so both show it. +pub(super) fn draw_clone_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, at: [f64; 2], output: Option) { + use photocraft_engine::presets::clone_source::{Mapping, overlay_blend, transform_matrix}; + let overlay = app.session.presets.clone.overlay.clone(); + let tool = app.ui.tool; + if !matches!(tool, Tool::CloneStamp | Tool::Healing) || !overlay.show || (overlay.auto_hide && app.drag.is_some()) { + return; + } + let slot = app.session.presets.clone.active().clone(); + let Some(source) = slot.source else { return }; + let radius = f64::from(app.session.tools.brush.size) / 2.0; + if !radius.is_finite() || radius <= 0.0 || radius > 500.0 || !at.iter().all(|v| v.is_finite() && v.abs() < 1_000_000.0) { + return; + } + // View › Pattern Preview repeats the source for the Clone Stamp only (`paint.cloneStamp`'s + // `wrap`); the Healing Brush samples it as it is. + let wrap = tool == Tool::CloneStamp && app.ui.view.pattern_preview; + let stroke_start = app.drag.as_ref().map(|d| d.start); + let anchor = if app.ui.tool_options.clone_aligned { slot.anchor.or(stroke_start) } else { stroke_start }.unwrap_or(at); + let map = + Mapping { source: (source[0], source[1]), anchor: (anchor[0], anchor[1]), m: transform_matrix(slot.scale, slot.rotation, slot.flip_h, slot.flip_v) }; + let Some(st) = app.session.active() else { return }; + let (display, display_key) = canvas_display(app, &st.doc, output); + let key = format!( + "{:?}:{}:{:?}:{:?}:{map:?}:{}:{}:{}:{}:{wrap}:{}:{}", + st.doc.id, + st.revision, + st.active_layer, + at.map(f64::to_bits), + app.ui.tool_options.clone_sample, + display_key, + overlay.invert, + overlay.blend, + overlay.clipped, + radius.to_bits() + ); + let brush = overlay.clipped.then_some(&app.session.tools.brush); + if app.clone_preview.as_ref().is_none_or(|c| c.key != key || c.brush.as_ref() != brush) { + // Clipped (Photoshop's default): the dab the tool paints here, so a soft brush previews + // soft and a shaped tip keeps its outline. Unclipped: the brush's whole square. + let (rect, coverage) = match brush { + Some(b) => { + let Some((rect, coverage)) = clone_dab_coverage(b, at, &map) else { return }; + (rect, Some(coverage)) + } + None => ( + DRect::new((at[0] - radius).floor() as i32, (at[1] - radius).floor() as i32, (at[0] + radius).ceil() as i32, (at[1] + radius).ceil() as i32), + None, + ), + }; + let Ok(mut buf) = photocraft_engine::retouch_cmds::clone_preview_periodic(&app.session, rect, &map, &app.ui.tool_options.clone_sample, wrap) else { + return; + }; + if overlay.invert { + for px in &mut buf.px { + for c in px.iter_mut().take(3) { + *c = 1.0 - *c; + } + } + } + // Darken, Lighten and Difference mix the source with the image under the brush (the + // composite, whatever Sample reads), so Difference turns black where they line up. + if overlay.blend != "normal" { + let here = Mapping { source: (0.0, 0.0), anchor: (0.0, 0.0), m: [1.0, 0.0, 0.0, 1.0] }; + let Ok(under) = photocraft_engine::retouch_cmds::clone_preview_periodic(&app.session, rect, &here, "all", wrap) else { + return; + }; + for (px, d) in buf.px.iter_mut().zip(&under.px) { + *px = overlay_blend(&overlay.blend, *px, *d); + } + } + if let Some(coverage) = &coverage { + for (px, c) in buf.px.iter_mut().zip(coverage) { + px[3] *= c.clamp(0.0, 1.0); + } + } + let image = display_image(display.as_deref(), &buf); + let brush = brush.cloned(); + match app.clone_preview.as_mut() { + Some(cache) => { + cache.texture.set(image, canvas_texture_options(1)); + cache.key = key; + cache.brush = brush; + cache.rect = rect; + } + None => { + let texture = painter.ctx().load_texture("clone-preview", image, canvas_texture_options(1)); + app.clone_preview = Some(ClonePreviewCache { key, brush, rect, texture }); + } + } + } + let Some(cache) = &app.clone_preview else { return }; + let rect = cache.rect; + let tint = Color32::from_white_alpha((overlay.opacity.clamp(0.0, 100.0) * 2.55).round() as u8); + // A textured quad over the covered area: its texels carry the clip, in mirrored and rotated + // canvas views too. + let mut mesh = egui::Mesh::with_texture(cache.texture.id()); + for (x, y, u, v) in [(rect.x0, rect.y0, 0.0, 0.0), (rect.x1, rect.y0, 1.0, 0.0), (rect.x1, rect.y1, 1.0, 1.0), (rect.x0, rect.y1, 0.0, 1.0)] { + mesh.vertices.push(egui::epaint::Vertex { pos: xf.to_screen(x as f32, y as f32), uv: pos2(u, v), color: tint }); + } + mesh.indices.extend_from_slice(&[0, 1, 2, 0, 2, 3]); + painter.add(egui::Shape::mesh(mesh)); +} + +/// The engine's live stroke of a tool: the Brush, Pencil and Eraser paint dabs; the Clone Stamp, +/// Healing Brush and History Brush composite their source. +pub(super) enum EngineStroke { + Brush(Box), + Retouch(Box), + Dab(Box), + Mixer(Box), +} + +impl EngineStroke { + /// The brush engine's strokes can change while the pointer is held still (see + /// `LiveStroke::wants_time`); the retouch strokes don't. + pub(super) fn wants_time(&self) -> bool { + match self { + EngineStroke::Brush(l) => l.wants_time(), + EngineStroke::Mixer(l) => l.wants_time(), + EngineStroke::Retouch(_) | EngineStroke::Dab(_) => false, + } + } + + pub(super) fn doc(&self) -> &std::sync::Arc { + match self { + Self::Brush(s) => &s.doc, + Self::Retouch(s) => &s.doc, + Self::Dab(s) => &s.doc, + Self::Mixer(s) => &s.doc, + } + } + fn bounds(&self) -> DRect { + match self { + Self::Brush(s) => s.bounds(), + Self::Retouch(s) => s.bounds(), + Self::Dab(s) => s.bounds(), + Self::Mixer(s) => s.bounds(), + } + } + fn push(&mut self, pts: &[photocraft_engine::paint::StrokePoint]) -> photocraft_engine::Result { + match self { + Self::Brush(s) => s.push(pts), + Self::Retouch(s) => s.push(pts), + Self::Dab(s) => s.push(pts), + Self::Mixer(s) => s.push(pts), + } + } + /// The jitter seed the commit must reuse (the Clone Stamp's comes from the session brush). + fn seed(&self) -> Option { + match self { + Self::Brush(s) => Some(s.seed), + Self::Mixer(s) => Some(s.seed), + Self::Retouch(_) => None, + Self::Dab(_) => None, + } + } +} + +/// A stroke shown while it is drawn: the engine renders the real result onto a copy of the +/// document, and the canvas redraws only what each step changed. +pub(crate) struct LiveStroke { + pub(super) preview_failed: bool, + pub(super) stroke: EngineStroke, + pub(super) doc: photocraft_doc::DocId, + revision: u64, + /// Preview key of the stroke; step `n` displays as `key + n`. + pub(super) key: u64, + /// Damage of each step (step 0 = the document before the stroke). + pub(super) damage: Vec, + /// Drag points rendered so far. + fed: usize, +} + +impl LiveStroke { + pub(super) fn display_key(&self) -> u64 { + self.key + self.damage.len() as u64 + } + + /// What changed since the canvas showed preview `key` (0 = the document before the stroke). + pub(super) fn since(&self, key: u64) -> Option { + let seen = if key == 0 { 0 } else { usize::try_from(key.checked_sub(self.key)?).ok()? }; + Some(self.damage.get(seen..)?.iter().fold(DRect::EMPTY, |a, r| a.union(r))) + } +} + +/// A live stroke whose pixels its commit took over (`"fromLive"`): document `doc` at `revision` +/// holds exactly the live stroke's last step, so a cache that showed step `n` of it only misses +/// the steps after `n` (step 0 = the document before the stroke: everything). +pub(crate) struct CommittedStroke { + pub(crate) doc: photocraft_doc::DocId, + pub(crate) revision: u64, + key: u64, + damage: Vec, + /// What the commit repaints beyond the stroke whatever a cache showed: the layer's whole + /// composite when effects laid out over its frame moved with it (a Gradient Overlay spans the + /// grown layer, #4053; `photocraft_compose::relayout_damage`). + pub(super) relayout: DRect, +} + +impl CommittedStroke { + #[cfg(test)] + pub(crate) fn for_test(key: u64, damage: Vec) -> Self { + Self { doc: photocraft_doc::DocId(0), revision: 0, key, damage, relayout: DRect::EMPTY } + } + + /// The raw preview key of the live stroke's last step: what a cache showed at release. + #[cfg(test)] + pub(crate) fn last_key(&self) -> u64 { + self.key + self.damage.len() as u64 + } + + /// What a cache that showed raw preview `key` of this stroke at the revision before misses. + pub(crate) fn since(&self, key: u64) -> Option { + let seen = if key == 0 { 0 } else { usize::try_from(key.checked_sub(self.key)?).ok()? }; + Some(self.damage.get(seen..)?.iter().fold(DRect::EMPTY, |a, r| a.union(r))) + } +} + +/// The live stroke on document `idx`, while it is current. +pub(super) fn live_stroke(app: &PhotocraftApp, idx: usize) -> Option<&LiveStroke> { + let st = app.session.documents().get(idx)?; + app.live_stroke.as_ref().filter(|l| app.drag.is_some() && l.doc == st.doc.id && l.revision == st.revision) +} + +/// `paint.stroke` params for a Brush/Eraser drag (shared by the live preview and the commit). The +/// stroke smoothing is the session brush's (the options bar's Smoothing %). +pub(super) fn stroke_params(app: &PhotocraftApp, d: &Drag, points: &[Vec]) -> serde_json::Value { + let tool = d.tool; + let mut p = json!({ "points": points, "erase": d.erase, "zoom": app.point_zoom(), "target": paint_target(app) }); + if d.clear { + p["mode"] = json!("clear"); + } + if tool == Tool::Pencil { + p["autoErase"] = json!(app.ui.tool_options.pencil_auto_erase); + } + // Block mode: the engine sizes the square from the stroke's zoom (16 / zoom px, #2770). + if crate::eraser_ui::block_mode(app, tool) { + p["block"] = json!(true); + } + p +} + +/// Tools whose strokes the engine renders while they are drawn (`LiveStroke`). +pub(crate) fn strokes_live(tool: Tool) -> bool { + matches!( + tool, + Tool::Brush + | Tool::Pencil + | Tool::MixerBrush + | Tool::Eraser + | Tool::ColorReplacement + | Tool::Blur + | Tool::Sharpen + | Tool::Smudge + | Tool::Dodge + | Tool::Burn + | Tool::Sponge + ) || live_retouch_command(tool).is_some() +} + +/// The Spot Healing Brush's stroke while dragging: Photoshop shows the area it will heal as a +/// semi-opaque dark overlay (an image overlay, the same in every UI theme) and heals on release. +pub(crate) const SPOT_HEALING_TRAIL: Color32 = Color32::from_black_alpha(128); + +/// The retouching tools drawn live (`LiveRetouch`; the Background Eraser's and an Impressionist +/// Pattern Stamp's dabs are laid one by one, `LiveDab`), by the command their live stroke +/// previews. The Healing Brush shows its texture while drawing; the blend comes on release. +pub(super) fn live_retouch_command(tool: Tool) -> Option<&'static str> { + match tool { + Tool::CloneStamp => Some("paint.cloneStamp"), + Tool::Healing => Some("paint.healingBrush"), + Tool::HistoryBrush => Some("paint.historyBrush"), + Tool::PatternStamp => Some("paint.patternStamp"), + Tool::BackgroundEraser => Some("paint.backgroundEraser"), + _ => None, + } +} + +/// The command a live-stroking tool commits: the Pencil's (and the Pencil- or Block-mode +/// Eraser's) `paint.pencil`, else `paint.stroke`. +pub(crate) fn stroke_command(app: &PhotocraftApp, tool: Tool) -> &'static str { + if tool == Tool::Pencil || crate::eraser_ui::pencil_mode(app, tool) || crate::eraser_ui::block_mode(app, tool) { "paint.pencil" } else { "paint.stroke" } +} + +pub(super) fn begin_live_stroke(app: &PhotocraftApp) -> Option { + static STROKES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let st = app.session.active()?; + let d = app.drag.as_ref()?; + let stroke = if d.tool == Tool::MixerBrush { + let points = app.stylus.stroke_points(&d.points); + let p = stroke_params(app, d, points.get(..1)?); + EngineStroke::Mixer(Box::new(photocraft_engine::brush_cmds::mixer_live::LiveMixer::begin(&app.session, &p).ok()?)) + } else if d.tool == Tool::ColorReplacement { + // The params `retouch_ui::finish_stroke` commits. + let mut p = crate::retouch_ui::color_replacement_params(app); + p["points"] = json!(d.points); + EngineStroke::Dab(Box::new(photocraft_engine::brush_cmds::live_color_replacement(&app.session, &p).ok()?)) + } else if let Some((cmd, mut p)) = crate::retouch_ui::dab_params(app, d.tool) { + // The params `retouch_ui::finish_stroke` commits; Sample All Layers isn't previewed. + p["points"] = json!(d.points); + p["target"] = paint_target(app); + EngineStroke::Dab(Box::new(photocraft_engine::retouch_cmds::LiveDab::begin(&app.session, cmd, &p).ok()?)) + } else if d.tool == Tool::PatternStamp { + // The params `retouch_ui::finish_stroke` commits. + let mut p = crate::retouch_ui::pattern_stamp_params(app, &d.points); + p["points"] = json!(d.points); + p["target"] = paint_target(app); + if app.ui.tool_options.pattern_stamp_impressionist { + EngineStroke::Dab(Box::new(photocraft_engine::retouch_cmds::live_pattern_impressionist(&app.session, &p).ok()?)) + } else { + EngineStroke::Retouch(Box::new(photocraft_engine::retouch_cmds::LiveRetouch::begin(&app.session, "paint.patternStamp", &p).ok()?)) + } + } else if d.tool == Tool::BackgroundEraser { + // The params `eraser_ui::finish_stroke` commits (layer pixels only). + let p = crate::eraser_ui::background_eraser_params(app, &d.points); + EngineStroke::Dab(Box::new(photocraft_engine::eraser_cmds::live_background_eraser(&app.session, &p).ok()?)) + } else if let Some(cmd) = live_retouch_command(d.tool) { + // The params `retouch_ui::finish_stroke` commits. + let mut p = if d.tool == Tool::HistoryBrush { crate::retouch_ui::history_brush_params(app) } else { crate::retouch_ui::clone_params(app)? }; + p["points"] = json!(d.points); + p["target"] = paint_target(app); + EngineStroke::Retouch(Box::new(photocraft_engine::retouch_cmds::LiveRetouch::begin(&app.session, cmd, &p).ok()?)) + } else { + let p = stroke_params(app, d, &app.stylus.stroke_points(&d.points)); + EngineStroke::Brush(Box::new(photocraft_engine::brush_cmds::LiveStroke::begin_with(&app.session, stroke_command(app, d.tool), &p).ok()?)) + }; + let n = STROKES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) & 0xff_ffff; + let damage = vec![stroke.bounds()]; + Some(LiveStroke { + stroke, + doc: st.doc.id, + revision: st.revision, + key: (1 << 44) | (n << 20), + damage, + fed: if d.tool == Tool::MixerBrush { 1 } else { d.points.len() }, + preview_failed: false, + }) +} + +/// Cancel uncommitted mixer paint when its document, tool or input context goes away. +pub(crate) fn cancel_mixer_on_context_change(app: &mut PhotocraftApp, ctx: &egui::Context) -> bool { + if !app.drag.as_ref().is_some_and(|drag| drag.tool == Tool::MixerBrush) { + return false; + } + let changed = app.ui.tool != Tool::MixerBrush + || app.live_stroke.as_ref().is_some_and(|live| !app.session.active().is_some_and(|st| st.doc.id == live.doc && st.revision == live.revision)); + if changed || ctx.input_mut(|input| input.consume_key(egui::Modifiers::NONE, egui::Key::Escape)) { + app.drag = None; + app.live_stroke = None; + return true; + } + false +} + +/// Render the drag points the live stroke hasn't seen yet, with the pen pressure, tilt and +/// rotation the commit's `paint.stroke` gets for them. +pub(super) fn feed_live_stroke(app: &mut PhotocraftApp) { + // A batched move replays several samples in one frame and calls this once at the end, so the + // per-sample calls from `tool_event` are skipped (see `canvas_view`). + if app.defer_live_stroke { + return; + } + let (Some(l), Some(d)) = (app.live_stroke.as_mut(), app.drag.as_ref()) else { return }; + if l.preview_failed { + return; + } + let pose = &app.stylus.stroke; + // The same times `stroke_points` gives the commit (from the drag's first point). + let t0 = app.stylus.point_ms(0); + let pts: Vec<_> = (l.fed..d.points.len()) + .filter_map(|i| { + let p = d.points.get(i)?; + let t = pose.get(i).or(pose.last()).copied().unwrap_or_default(); + let mut sp = photocraft_engine::paint::StrokePoint::new(p[0], p[1], p[2] as f32); + (sp.tilt_x, sp.tilt_y, sp.rotation) = (t[0], t[1], t[2]); + sp.time = app.stylus.point_ms(i) - t0; + Some(sp) + }) + .collect(); + if pts.is_empty() { + return; + } + l.fed = d.points.len(); + let started = crate::gpu_canvas::now_ms(); + match l.stroke.push(&pts) { + Ok(r) => l.damage.push(r), + Err(e) => { + if d.tool == Tool::MixerBrush { + // Keep the last valid preview and its document/revision guard. All input + // stays in the drag; release replays it without the preview tile budget. + l.preview_failed = true; + app.ui.status = e.to_string(); + app.ui.status_error = true; + } else { + app.live_stroke = None; + } + } + } + app.perf.stage.stroke_ms += crate::gpu_canvas::now_ms() - started; +} + +/// The points the live retouching stroke has rendered. +#[cfg(test)] +pub(crate) fn live_retouch_points(app: &PhotocraftApp) -> Option> { + match &app.live_stroke.as_ref()?.stroke { + EngineStroke::Retouch(l) => Some(l.points().to_vec()), + _ => None, + } +} + +/// Offer the Clone Stamp's or History Brush's live stroke, or a Dodge, Burn, Sponge, Blur, Sharpen +/// or Smudge one, with the drag points it hasn't rendered yet, to the command +/// `retouch_ui::finish_stroke` commits, which then takes its pixels instead of rendering the +/// stroke again. +pub(super) fn offer_live_retouch(app: &mut PhotocraftApp, d: &Drag) { + let dab = crate::retouch_ui::dab_params(app, d.tool).is_some(); + if !dab && !matches!(d.tool, Tool::CloneStamp | Tool::HistoryBrush) { + return; + } + let Some(LiveStroke { stroke, fed, .. }) = app.live_stroke.take() else { return }; + let rest: Vec<_> = d.points.get(fed..).unwrap_or_default().iter().map(|q| photocraft_engine::paint::StrokePoint::new(q[0], q[1], q[2] as f32)).collect(); + // A refused offer (the brush changed mid-stroke, say) leaves the commit to render the stroke. + match stroke { + EngineStroke::Retouch(mut live) if !dab => { + if rest.is_empty() || live.push(&rest).is_ok() { + let _ = app.session.offer_live_retouch(*live); + } + } + EngineStroke::Dab(mut live) if dab => { + if rest.is_empty() || live.push(&rest).is_ok() { + let _ = app.session.offer_live_dab(*live); + } + } + _ => {} + } +} + +/// A pen tap (a drag still at its one press point when released) paints its dab at the highest +/// pressure the pen reached during the contact, not just the first sample's (#1798). A mouse is +/// unchanged at full pressure. +pub(super) fn tap_at_peak_pressure(app: &mut PhotocraftApp) { + if app.stylus.sample().is_none() { + return; + } + let peak = f64::from(app.stylus.tap_pressure()); + let mut refresh_mixer = false; + if let Some(d) = app.drag.as_mut().filter(|d| d.points.len() == 1) + && let Some(p) = d.points.first_mut() + { + refresh_mixer = d.tool == Tool::MixerBrush && peak > p[2]; + p[2] = p[2].max(peak); + } + if refresh_mixer { + app.live_stroke = begin_live_stroke(app); + } +} + +/// While a stroke is held still, its point repeats this often (in ms) so time-based brush +/// features (airbrush build-up, smoothing catch-up) keep going, as when the pen moves. +pub(super) const HOLD_REPEAT_MS: f64 = 16.0; + +/// Is the live stroke one that changes while the pointer is held still (airbrush Build-up, or +/// smoothing catching up)? Only then does a held pointer repeat its point and keep repainting. +pub(super) fn stroke_wants_time(app: &PhotocraftApp) -> bool { + app.live_stroke.as_ref().is_some_and(|l| l.stroke.wants_time()) +} + +/// Time of sample `k` of the `n` samples a frame delivered, spread evenly after `base` (the +/// previous point) up to `now` (this frame): egui's pointer events carry no timestamps. +pub(crate) fn spread_ms(base: f64, now: f64, k: usize, n: usize) -> f64 { + // Not after `base` (or not comparable, NaN): every sample takes the frame time. + if n == 0 || now.partial_cmp(&base) != Some(std::cmp::Ordering::Greater) { + return now; + } + base + (now - base) * (k + 1) as f64 / n as f64 +} + +/// Tools whose gesture follows a freehand path (a polyline of the input points), so every pointer +/// sample the OS delivered improves the result. Other tools are driven by the pointer's latest +/// position (endpoints, anchors, guides), so feeding them a whole frame's moves only repeats work. +pub(crate) fn freehand_tool(tool: Tool) -> bool { + matches!( + tool, + Tool::Brush + | Tool::Pencil + | Tool::MixerBrush + | Tool::ColorReplacement + | Tool::Eraser + | Tool::BackgroundEraser + | Tool::HistoryBrush + | Tool::Remove + | Tool::ArtHistoryBrush + | Tool::SpotHealing + | Tool::Healing + | Tool::CloneStamp + | Tool::PatternStamp + | Tool::Blur + | Tool::Sharpen + | Tool::Smudge + | Tool::Dodge + | Tool::Burn + | Tool::Sponge + | Tool::Lasso + | Tool::MagneticLasso + | Tool::Patch + | Tool::ContentAwareMove + | Tool::QuickSelection + | Tool::FreeformPen + ) +} + +/// The pointer moves this frame delivered while `button` was held, in order. `down_at_start` is +/// whether the button was already down when the frame began. A move before a press or after a +/// release in the same frame is dropped, so a gesture never picks up input from outside its own +/// press..release interval. +pub(super) fn pointer_moves(events: &[egui::Event], button: egui::PointerButton, down_at_start: bool) -> Vec { + let mut down = down_at_start; + let mut out = Vec::new(); + for e in events { + match e { + egui::Event::PointerButton { button: b, pressed, .. } if *b == button => down = *pressed, + egui::Event::PointerMoved(p) if down => out.push(*p), + _ => {} + } + } + out +} + +/// Modifiers attached to the pointer event itself. Some input sources provide these without a +/// separate `ModifiersChanged` event, so use them for click gestures before falling back to the +/// frame-wide modifier state. +pub(super) fn pointer_button_modifiers(events: &[egui::Event], button: egui::PointerButton) -> Option { + events.iter().rev().find_map(|e| match e { + egui::Event::PointerButton { button: b, modifiers, .. } if *b == button => Some(*modifiers), + _ => None, + }) +} + +/// The line from the brush to the pointer while stroke smoothing lags (Pulled String Mode and +/// Stroke Catch-up, #3479): a dark underlay and a light line stay visible on any pixels, and a dot +/// marks the brush end. +pub(super) fn draw_smoothing_string(painter: &egui::Painter, xf: &ViewXform, (brush, pointer): ([f64; 2], [f64; 2])) { + let (a, b) = (xf.to_screen(brush[0] as f32, brush[1] as f32), xf.to_screen(pointer[0] as f32, pointer[1] as f32)); + if a.distance(b) < 2.0 { + return; + } + painter.line_segment([a, b], Stroke::new(3.0, Color32::from_black_alpha(110))); + painter.line_segment([a, b], Stroke::new(1.0, Color32::WHITE)); + painter.circle_filled(a, 2.5, Color32::WHITE); +} + +/// Commit a Brush, Pencil or Eraser drag. The live stroke already rendered it: its pixels are +/// offered to the commit (`Session::offer_live_stroke`), so release costs no second render, and +/// the canvas then refreshes only what it hadn't shown yet (`CommittedStroke`). +pub(super) fn commit_brush_stroke(app: &mut PhotocraftApp, d: &Drag) { + let live = app.live_stroke.take(); + let mut p = stroke_params(app, d, &app.stylus.stroke_points(&d.points)); + if let Some(seed) = live.as_ref().and_then(|l| l.stroke.seed()) { + p["seed"] = json!(seed); + } + let Some(mut l) = live else { + let _ = app.run(stroke_command(app, d.tool), p); + return; + }; + // Points the live stroke hasn't rendered yet (the release frame's last moves). + let t0 = app.stylus.point_ms(0); + let rest: Vec<_> = (l.fed..d.points.len()) + .filter_map(|i| { + let q = d.points.get(i)?; + let t = app.stylus.stroke.get(i).or(app.stylus.stroke.last()).copied().unwrap_or_default(); + let mut sp = photocraft_engine::paint::StrokePoint::new(q[0], q[1], q[2] as f32); + (sp.tilt_x, sp.tilt_y, sp.rotation) = (t[0], t[1], t[2]); + sp.time = app.stylus.point_ms(i) - t0; + Some(sp) + }) + .collect(); + if !rest.is_empty() { + match l.stroke.push(&rest) { + Ok(r) => l.damage.push(r), + // The live stroke can't show these points: the commit renders the stroke itself. + Err(_) => { + let _ = app.run(stroke_command(app, d.tool), p); + return; + } + } + } + let LiveStroke { stroke, doc, key, damage, .. } = l; + let before = app.session.documents().iter().find(|st| st.doc.id == doc).map(|st| st.doc.clone()); + if let EngineStroke::Brush(stroke) = stroke + && app.session.offer_live_stroke(*stroke).is_err() + { + app.session.clear_live_stroke(); + } + let started = crate::gpu_canvas::now_ms(); + let result = app.run(stroke_command(app, d.tool), p); + app.perf.stage.stroke_ms += crate::gpu_canvas::now_ms() - started; + app.session.clear_live_stroke(); + let Ok(r) = result else { return }; + let after = app.session.documents().iter().find(|st| st.doc.id == doc).map(|st| (st.revision, st.doc.clone())); + if r.get("fromLive").and_then(serde_json::Value::as_bool) == Some(true) + && let Some((revision, after)) = after + { + let relayout = before.map_or(DRect::EMPTY, |b| photocraft_compose::relayout_damage(&b, &after)); + app.committed_stroke = Some(CommittedStroke { doc, revision, key, damage, relayout }); + } else { + // Rendered again from scratch: the commit's damage rect refreshes the stroke. + let shown = CommittedStroke { doc, revision: 0, key, damage, relayout: DRect::EMPTY }; + // Raw preview key 0 = the document itself (its colour display folded in). + shown_as_document(app, doc, |k| shown.since(k).is_some()); + } +} + +/// Pencil (and a Pencil-mode Eraser) dabs sit on the pixel grid. Clicks anywhere inside one +/// pixel must produce the same point, or a ⇧-click line changes with the sub-pixel position. +pub(super) fn snap_aliased_tip(app: &PhotocraftApp, tool: Tool, x: f64, y: f64) -> (f64, f64) { + let pencil = tool == Tool::Pencil || crate::eraser_ui::pencil_mode(app, tool); + if !pencil { + return (x, y); + } + let (cx, cy) = photocraft_engine::paint::grid_center(x, y, app.session.tools.brush.size); + (f64::from(cx), f64::from(cy)) +} + +/// "mask" when the Layers panel targets (or the canvas shows) the active layer's mask, or the layer +/// is an adjustment layer with one (it has no pixels: #4298), else "pixels". +pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value { + use photocraft_engine::channel_cmds::ChannelTarget; + let Some(st) = app.session.active() else { return json!("pixels") }; + // A targeted alpha channel (Channels panel) or Quick Mask mode wins over the layer. + match st.channel_view.target { + ChannelTarget::Alpha(i) if i < st.doc.channels.len() => return json!({ "channel": i }), + ChannelTarget::Composite if st.doc.quick_mask.is_some() => return json!("quickMask"), + ChannelTarget::LayerMask => return json!("mask"), + _ => {} + } + // A smart object's targeted (or shown) filter mask takes the paint (FILE-215-3). + if app.filter_mask_targeted() { + return json!("filterMask"); + } + let has_mask = st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| l.mask.is_some()); + // Viewing the mask (⌥-click its thumbnail, #196) paints the mask. + let viewing = photocraft_engine::mask_view_cmds::current(st).is_some_and(|v| !v.filter); + let mask = app.ui.mask_target || viewing || crate::adjustment_layer_active(st); + json!(if mask && has_mask { "mask" } else { "pixels" }) +} diff --git a/crates/ui-egui/src/canvas/tabs.rs b/crates/ui-egui/src/canvas/tabs.rs new file mode 100644 index 0000000000..d3aa28546a --- /dev/null +++ b/crates/ui-egui/src/canvas/tabs.rs @@ -0,0 +1,789 @@ +use super::start_screen::start_screen; +use super::textures::retain_gpu_documents; +use super::{Color32, PhotocraftApp, Pos2, Rect, Sense, Stroke, TextureOptions, canvas_view, fmt_zoom, json, mode_label, pos2}; + +/// Tabs + canvas for the active document, or the start screen; then drops layers dragged onto +/// another document (`layer_transfer`). +pub fn document_area(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + crate::distort_ui::drop_foreign(app); + documents(app, ui); + crate::layer_transfer::finish(app, ui.ctx()); + // Once a frame, not per document view: the right-click or options-bar Brush Preset picker. + crate::paint_mouse::show_picker(app, ui.ctx()); + // And the Shift+Alt+right-click HUD Color Picker (#6098), while its gesture is held. + crate::color_hud::show(app, ui.ctx()); + crate::brush_mode_menu::show(app, ui.ctx()); +} + +pub(crate) fn arranged_cells(app: &PhotocraftApp, rect: Rect) -> Option> { + let documents = docked_document_indices(app); + let n = documents.len(); + if n == 0 { + return None; + } + let cells = crate::view_cmds::cells(&app.ui.view.arrange, rect, n)?; + // Keep stable tiles, expressing the remembered document index in the filtered docked list. + let start = documents.partition_point(|&d| d < app.arrange_start).min(n.saturating_sub(cells.len())); + let idx = app.session.active_index().and_then(|d| documents.iter().position(|&p| p == d)); + let start = idx.map_or(start, |idx| if idx < start { idx } else { start.max((idx + 1).saturating_sub(cells.len())) }); + Some(documents[start..].iter().copied().zip(cells).collect()) +} + +fn documents(app: &mut PhotocraftApp, ui: &mut egui::Ui) { + app.drop_canvas_rect = None; + app.tab_strip = None; + retain_gpu_documents(app); + crate::transform_tool::track_steps(app, ui.ctx()); + crate::cas_transform::tick(app, ui.ctx()); + crate::symmetry_ui::track(app, ui.ctx()); + let n = app.session.documents().len(); + // Files opening in the background (#210) have tabs before they have documents. + let opening = !app.jobs.opens.is_empty(); + if !opening && app.ui.chrome.shows_home(n, app.session.prefs().general.auto_show_home_screen) { + start_screen(app, ui); + return; + } + if n == 0 && !opening { + // Auto show the Home Screen is off: an empty workspace, like Photoshop. + let area = ui.available_rect_before_wrap(); + paint_dots(ui, area, app.session.prefs().interface.show_canvas_dots); + open_on_double_click(app, ui, area); + return; + } + if n > 0 && !opening && docked_document_indices(app).is_empty() { + // A torn-off document belongs to its OS window. Leaving its canvas in the main + // viewport would make the same page appear twice, which is what “New Window” does. + paint_dots(ui, ui.available_rect_before_wrap(), app.session.prefs().interface.show_canvas_dots); + return; + } + if !app.ui.view.hides_tabs() || opening { + // Flat Spectrum tabs sit directly against the canvas. The parent's default vertical + // item spacing would otherwise leave a visible gap below the tab strip (#2188). + let spacing = ui.spacing().item_spacing.y; + if crate::theme::Tokens::get(ui.ctx()).pro { + ui.spacing_mut().item_spacing.y = 0.0; + } + app.tab_strip = Some(tabs(app, ui)); + ui.spacing_mut().item_spacing.y = spacing; + drop_slot_line(app, ui); + } + if let Some(job) = app.jobs.focus.or_else(|| (n == 0).then(|| app.jobs.opens.last().map(|o| o.job)).flatten()) { + crate::jobs_ui::open_card(app, ui, job); + return; + } + let Some(active_doc) = app.session.active_index() else { return }; + let Some(idx) = main_canvas_document(app) else { + paint_dots(ui, ui.available_rect_before_wrap(), app.session.prefs().interface.show_canvas_dots); + return; + }; + let main_focused = ui.ctx().input(|i| i.viewport().focused.unwrap_or(true)); + let rect = ui.available_rect_before_wrap(); + app.last_canvas_rect = rect; + app.drop_canvas_rect = Some(rect); + // Window › Arrange: tiled / n-up layouts show several documents side by side; the active one + // takes input, a click elsewhere activates that document. + if let Some(shown) = arranged_cells(app, rect) { + app.arrange_start = shown.first().map_or(0, |(d, _)| *d); + let t = crate::theme::Tokens::get(ui.ctx()); + for (d, cell) in shown { + let cell = cell.shrink(1.0); + let view = app.ui.views[d].clone(); + let v = canvas_view(app, ui, d, cell, view, d == active_doc); + if d != active_doc { + app.ui.views[d] = v; + if main_focused && ui.input(|i| i.pointer.primary_pressed() && i.pointer.interact_pos().is_some_and(|p| cell.contains(p))) { + app.session.set_active(d); + } + } + let stroke = if d == active_doc { Stroke::new(1.5, t.accent) } else { Stroke::new(1.0, Color32::from_black_alpha(160)) }; + ui.painter().rect_stroke(cell, 0, stroke, egui::StrokeKind::Inside); + } + return; + } + if idx != active_doc && main_focused && ui.input(|i| i.pointer.primary_pressed() && i.pointer.interact_pos().is_some_and(|p| rect.contains(p))) { + app.session.set_active(idx); + } + let view = app.ui.views[idx].clone(); + canvas_view(app, ui, idx, rect, view, app.session.active_index() == Some(idx)); +} + +/// The tab strip; returns where its document tabs are. +pub(super) fn tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip { + let t = crate::theme::Tokens::get(ui.ctx()); + if t.pro { + return pro_tabs(app, ui); + } + let active = app.session.active_index(); + let documents = docked_document_indices(app); + let tab_count = documents.len(); + let document_count = app.session.documents().len(); + let mut doc_tabs = Vec::with_capacity(tab_count); + let mut activate = None; + let mut close = None; + let mut tab_action = None; + let mut tab_move = None; + let mut tear_off = None; + let (mut focus_open, mut cancel_open) = (None, None); + let focused_open = app.jobs.focus.is_some(); + // Layers dragged over a tab show its document (`layer_transfer`). + let dragging = crate::layer_transfer::pointer_if_armed(app, ui.ctx()); + let mut drag_over = None; + let large_tabs = app.session.prefs().workspace.large_tabs; + let tab_h = if large_tabs { 34.0 } else { 26.0 }; + let font = crate::theme::medium(if large_tabs { 14.0 } else { 12.5 }); + let meta_font = egui::FontId::proportional(if large_tabs { 12.0 } else { 10.5 }); + // Files opening in the background (#210) are tabs too; they share the fit's index space, + // after the documents. + let opening = crate::jobs_ui::open_tabs(app); + // Every tab's natural width, whether or not it stays on the strip. + let natural: Vec = documents + .iter() + .filter_map(|&doc| app.session.documents().get(doc)) + .map(|st| { + let name = studio_tab_name(app, st); + ui.painter().layout_no_wrap(name, font.clone(), t.text).size().x + + ui.painter().layout_no_wrap(format!("{}/{}", mode_label(&st.doc), st.doc.depth.bits()), meta_font.clone(), t.text_faint).size().x + + STUDIO_TAB_PAD + }) + .chain(opening.iter().map(|(_, name, frac)| { + ui.painter().layout_no_wrap(name.clone(), font.clone(), t.text).size().x + + ui.painter().layout_no_wrap(format!("{:.0}%", frac * 100.0), meta_font.clone(), t.text_faint).size().x + + STUDIO_TAB_PAD + })) + .collect(); + let selected = app + .jobs + .focus + .and_then(|job| opening.iter().position(|(open, _, _)| *open == job)) + .map_or_else(|| app.session.active_index().and_then(|doc| documents.iter().position(|&d| d == doc)).unwrap_or(0), |p| tab_count + p); + let frame = egui::Frame::NONE.fill(t.canvas).inner_margin(egui::Margin { left: 8, right: 8, top: 6, bottom: 4 }).show(ui, |ui| { + let (row, _) = ui.allocate_exact_size(egui::vec2(ui.available_width(), tab_h), Sense::hover()); + let f = fit_doc_tabs(&natural, selected, row.width()); + let mut x = row.left(); + let mut placed: Vec<(usize, Rect)> = Vec::with_capacity(f.shown.len()); + for &(i, w) in &f.shown { + // The old horizontal layout put 4 pt between tabs; `w` carries it, the tab doesn't. + let r = Rect::from_min_size(egui::pos2(x, row.top()), egui::vec2((w - STUDIO_TAB_GAP).max(8.0), row.height())); + placed.push((i, r)); + x = r.right() + STUDIO_TAB_GAP; + } + for &(i, r) in placed.iter().filter(|(i, _)| *i < tab_count) { + let Some(&doc) = documents.get(i) else { continue }; + let Some(st) = app.session.documents().get(doc) else { continue }; + let sel = Some(doc) == active && !focused_open; + let name = studio_tab_name(app, st); + let meta = format!("{}/{}", mode_label(&st.doc), st.doc.depth.bits()); + let natural_w = natural.get(i).copied().unwrap_or(0.0); + let meta_g = ui.painter().layout_no_wrap(meta, meta_font.clone(), t.text_faint); + let name_max = (r.width() - STUDIO_TAB_PAD + STUDIO_TAB_GAP - meta_g.size().x).max(1.0); + let name_g = crate::tab_strip::elided(ui, &name, font.clone(), t.text, name_max); + // The title was cut: the tooltip has the rest of it. + let cut = name_g.size().x + 0.5 < natural_w - STUDIO_TAB_PAD + STUDIO_TAB_GAP - meta_g.size().x; + let resp = ui.interact(r, ui.scope_id().with(("dtab", doc)), Sense::click_and_drag()); + let saving = crate::jobs_ui::save_progress(app, st.doc.id); + resp.widget_info(|| { + let label = saving.as_ref().map_or_else(|| st.doc.name.clone(), |s| format!("{} - {s}", st.doc.name)); + egui::WidgetInfo::selected(egui::Role::Button, true, sel, label) + }); + doc_tabs.push((doc, r)); + if sel { + ui.painter().rect_filled(r, t.radius_sm, t.card); + ui.painter().rect_stroke(r, t.radius_sm, Stroke::new(1.0, t.card_border), egui::StrokeKind::Inside); + } else if resp.hovered() { + ui.painter().rect_filled(r, t.radius_sm, t.hover.gamma_multiply(0.5)); + } + if dragging.is_some_and(|p| r.contains(p)) { + drag_over = Some(doc); + ui.painter().rect_stroke(r, t.radius_sm, Stroke::new(1.5, t.accent), egui::StrokeKind::Inside); + } + let color = if sel { t.text } else { t.text_dim }; + let ny = r.center().y - name_g.size().y / 2.0; + ui.painter().galley_with_override_text_color(egui::pos2(r.left() + 10.0, ny), name_g.clone(), color); + ui.painter().galley(egui::pos2(r.left() + 16.0 + name_g.size().x, r.center().y - meta_g.size().y / 2.0), meta_g, t.text_faint); + let xr = Rect::from_center_size(egui::pos2(r.right() - 12.0, r.center().y), egui::vec2(16.0, 16.0)); + let xresp = ui.interact(xr, ui.scope_id().with(("dtabx", doc)), Sense::click()); + if xresp.hovered() { + ui.painter().rect_filled(xr, 4.0, t.hover); + } + crate::icons::paint(ui, xr, "x", 11.0, if xresp.hovered() { t.text } else { t.text_faint }); + let resp = if cut { resp.on_hover_text(name) } else { resp }; + if tab_close_clicked(&resp, &xresp) { + close = Some(doc); + } else if resp.clicked() { + activate = Some(doc); + } + match tab_drag(ui, &resp, i, r, row, &placed, &t) { + Some(TabDrop::Move(to)) => tab_move = Some((doc, move_target_after_filter(&documents, doc, to, document_count))), + Some(TabDrop::Float(at)) => tear_off = Some((doc, at)), + None => {} + } + let has_path = st.path.is_some(); + resp.context_menu(|ui| { + tab_action = tab_context_menu(ui, doc, document_count, has_path); + }); + } + // Files opening in the background: a tab with a progress underline; × cancels. + for &(i, r) in placed.iter().filter(|(i, _)| *i >= tab_count) { + let Some((job, name, frac)) = opening.get(i - tab_count) else { continue }; + let sel = app.jobs.focus == Some(*job); + let meta_g = ui.painter().layout_no_wrap(format!("{:.0}%", frac * 100.0), meta_font.clone(), t.text_faint); + let name_g = crate::tab_strip::elided(ui, name, font.clone(), t.text, (r.width() - STUDIO_TAB_PAD + STUDIO_TAB_GAP - meta_g.size().x).max(1.0)); + let resp = ui.interact(r, ui.scope_id().with(("dtabjob", job.0)), Sense::click()); + if sel { + ui.painter().rect_filled(r, t.radius_sm, t.card); + ui.painter().rect_stroke(r, t.radius_sm, Stroke::new(1.0, t.card_border), egui::StrokeKind::Inside); + } else if resp.hovered() { + ui.painter().rect_filled(r, t.radius_sm, t.hover.gamma_multiply(0.5)); + } + let ny = r.center().y - name_g.size().y / 2.0; + ui.painter().galley_with_override_text_color(egui::pos2(r.left() + 10.0, ny), name_g.clone(), if sel { t.text } else { t.text_dim }); + ui.painter().galley(egui::pos2(r.left() + 16.0 + name_g.size().x, r.center().y - meta_g.size().y / 2.0), meta_g, t.text_faint); + crate::jobs_ui::tab_underline(ui, r, *frac, &t); + let xr = Rect::from_center_size(egui::pos2(r.right() - 12.0, r.center().y), egui::vec2(16.0, 16.0)); + let xresp = ui.interact(xr, ui.scope_id().with(("dtabjobx", job.0)), Sense::click()); + if xresp.hovered() { + ui.painter().rect_filled(xr, 4.0, t.hover); + } + crate::icons::paint(ui, xr, "x", 11.0, if xresp.hovered() { t.text } else { t.text_faint }); + if xresp.on_hover_text(tl!("Cancel opening")).clicked() { + cancel_open = Some(*job); + } else if resp.clicked() { + focus_open = Some(*job); + } + } + if !f.overflow.is_empty() { + // Pinned to the strip's right end, as Photoshop's is, not after the last tab (#8290). + let left = (row.right() - crate::tab_strip::CHEVRON_W).max(row.left()); + let r = Rect::from_min_size(egui::pos2(left, row.top()), egui::vec2(crate::tab_strip::CHEVRON_W, row.height())); + let labels: Vec<&str> = documents + .iter() + .filter_map(|&doc| app.session.documents().get(doc).map(|st| st.doc.name.as_str())) + .chain(opening.iter().map(|(_, name, _)| name.as_str())) + .collect(); + let mut picked = None; + // Like Photoshop's, the menu lists every open document, not only the hidden ones, and checks + // the active one (#4487). + let every: Vec = (0..labels.len()).collect(); + crate::tab_strip::overflow_button(ui, ui.scope_id().with("dtab-overflow"), r, tl!("More documents"), &labels, &every, Some(selected), &mut picked); + match picked { + Some(i) if i < tab_count => activate = documents.get(i).copied(), + Some(i) => focus_open = opening.get(i - tab_count).map(|(job, _, _)| *job).or(focus_open), + None => {} + } + } + }); + open_tab_clicks(app, activate, focus_open, cancel_open); + if let Some(i) = drag_over { + crate::layer_transfer::over_tab(app, ui.ctx(), i); + } + if let Some(i) = close { + let _ = crate::menus::invoke(app, ui.ctx(), "file.close", json!({"document": i})); + } + if let Some((from, to)) = tab_move + && let Err(e) = crate::menus::invoke(app, ui.ctx(), "document.move", json!({"document": from, "to": to})) + { + app.ui.status = e; + app.ui.status_error = true; + } + float_torn_off(app, ui.ctx(), tear_off); + if let Some((id, params)) = tab_action + && let Err(e) = crate::menus::invoke(app, ui.ctx(), id, params) + { + app.ui.status = e; + app.ui.status_error = true; + } + TabStrip { rect: frame.response.rect, tabs: doc_tabs } +} + +/// Whether a document tab was asked to close: its × was clicked (#619), or the tab was clicked +/// with the middle button (#2570), as in a browser. A middle click over the × counts too, so the +/// whole tab behaves the same way. Both strips share this rule; the caller routes it through the +/// guarded `file.close`, so a document with unsaved changes still gets the save prompt. +fn tab_close_clicked(resp: &egui::Response, xresp: &egui::Response) -> bool { + xresp.clicked() || xresp.middle_clicked() || resp.middle_clicked() +} + +/// All tab actions go through the same guarded File commands as the menu bar, including the +/// unsaved-changes prompt. The clicked tab is explicit even when another document is active. +pub(super) fn tab_context_items(index: usize, count: usize, has_path: bool) -> [(&'static str, &'static str, serde_json::Value, bool); 4] { + [ + ("Close", "file.close", json!({"document": index}), true), + ("Close Others", "file.closeOthers", json!({"document": index}), count > 1), + ("Close All", "file.closeAll", json!({}), true), + (crate::menus::reveal_label(), "file.revealInFinder", json!({"document": index}), has_path), + ] +} + +fn tab_context_menu(ui: &mut egui::Ui, index: usize, count: usize, has_path: bool) -> Option<(&'static str, serde_json::Value)> { + crate::widgets::menu_scroll(ui, |ui| { + ui.set_min_width(170.0); + for (label, id, params, enabled) in tab_context_items(index, count, has_path) { + if ui.add_enabled(enabled, egui::Button::new(tl!(label))).clicked() { + ui.close(); + return Some((id, params)); + } + } + None + }) +} + +/// Where a tab dragged to `x` would land: before the first shown tab whose middle is right of +/// `x`, else after the last shown tab (its document index + 1), like [`TabStrip::slot`]. +/// How far (points) above or below the tab strip a dragged tab must be to tear off (#3733). +pub(super) const TEAR_OFF: f32 = 24.0; + +/// Whether a tab dragged to `p` has left the strip and floats into its own window, as in +/// Photoshop; sideways drags along the strip still reorder (#217). +pub(super) fn torn_off(strip: Rect, p: egui::Pos2) -> bool { + p.y > strip.bottom() + TEAR_OFF || p.y < strip.top() - TEAR_OFF +} + +/// What a finished document-tab drag asks for. +#[derive(Clone, Copy, Debug, PartialEq)] +enum TabDrop { + /// Reorder the tab to this index. + Move(usize), + /// Float the document in its own OS window at this screen position (points), when known. + Float(Option<[f32; 2]>), +} + +/// Drag a document tab: along the strip it reorders (a line shows where it will land); off the +/// strip it tears off into a floating window that can go anywhere, other screens included +/// (an outline under the pointer shows it). Returns the drop once the drag ends. +fn tab_drag(ui: &egui::Ui, resp: &egui::Response, i: usize, r: Rect, strip: Rect, placed: &[(usize, Rect)], t: &crate::theme::Tokens) -> Option { + if resp.dragged() + && let Some(p) = resp.interact_pointer_pos() + { + if torn_off(strip, p) { + let ghost = Rect::from_min_size(p - egui::vec2(40.0, 12.0), egui::vec2(240.0, 160.0)); + let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Tooltip, ui.scope_id().with("tab-tear-off"))); + painter.rect_filled(ghost, t.radius_sm, t.accent.gamma_multiply(0.12)); + painter.rect_stroke(ghost, t.radius_sm, Stroke::new(1.5, t.accent), egui::StrokeKind::Inside); + } else { + tab_drop_line(ui, placed, p.x, t); + } + } + if !resp.drag_stopped() { + return None; + } + let p = resp.interact_pointer_pos().or_else(|| ui.input(|i| i.pointer.interact_pos())); + if let Some(p) = p.filter(|&p| torn_off(strip, p)) { + // The pointer is in this window's points; the OS wants screen coordinates. Keep the new + // window's title bar under the pointer, as the ghost showed. + let at = ui.ctx().input(|i| i.viewport().inner_rect).map(|inner| { + let s = inner.min + p.to_vec2() - egui::vec2(40.0, 12.0); + [s.x, s.y] + }); + return Some(TabDrop::Float(at)); + } + let to = tab_move_target(i, tab_drop_index(placed, p.map_or(r.center().x, |p| p.x))); + (to != i).then_some(TabDrop::Move(to)) +} + +/// Float a torn-off tab's document in its own window (`window.arrange.floatInWindow`). +fn float_torn_off(app: &mut PhotocraftApp, ctx: &egui::Context, tear_off: Option<(usize, Option<[f32; 2]>)>) { + let Some((doc, at)) = tear_off else { return }; + let params = match at { + Some(at) => json!({"document": doc, "position": at}), + None => json!({"document": doc}), + }; + if let Err(e) = crate::menus::invoke(app, ctx, "window.arrange.floatInWindow", params) { + app.ui.status = e; + app.ui.status_error = true; + } +} + +pub(super) fn tab_drop_index(placed: &[(usize, Rect)], x: f32) -> usize { + match placed.iter().find(|(_, r)| r.center().x >= x) { + Some(&(i, _)) => i, + None => placed.last().map_or(0, |&(i, _)| i.saturating_add(1)), + } +} + +/// The `document.move` target for dropping the tab at `from` at insertion index `k`: the slot +/// shifts down when the dragged tab itself is removed first; next to itself it stays put. +pub(super) fn tab_move_target(from: usize, k: usize) -> usize { + if k > from { k - 1 } else { k } +} + +/// The insertion line shown while a tab is dragged to reorder it. +fn tab_drop_line(ui: &egui::Ui, placed: &[(usize, Rect)], x: f32, t: &crate::theme::Tokens) { + match placed.iter().find(|(_, r)| r.center().x >= x) { + Some(&(_, r)) => crate::widgets::drop_line(ui, r, false, true, t), + None => { + if let Some(&(_, r)) = placed.last() { + crate::widgets::drop_line(ui, r, true, true, t); + } + } + } +} + +/// Apply tab-strip clicks: a document tab shows that document, an opening tab its progress, and +/// an opening tab's × cancels the open (closing the half-open tab). +fn open_tab_clicks( + app: &mut PhotocraftApp, + activate: Option, + focus_open: Option, + cancel_open: Option, +) { + if let Some(i) = activate { + app.session.set_active(i); + app.jobs.focus = None; + } + if let Some(job) = focus_open { + app.jobs.focus = Some(job); + } + if let Some(job) = cancel_open { + crate::jobs_ui::cancel(app, job); + } +} + +/// Document tabs: the chrome around a title (the × and its gaps). +const DOC_TAB_PAD: f32 = 42.0; + +/// The Studio strip's tabs carried 4 pt of spacing between them and 44 pt of chrome; `fit` sees +/// one number per tab, so the gap rides in the natural width and comes back out when painted. +const STUDIO_TAB_PAD: f32 = 48.0; + +const STUDIO_TAB_GAP: f32 = 4.0; + +/// Fit document tabs as Photoshop does (#4487): a tab never shrinks below its whole title, so the +/// tabs that don't fit move into the » menu instead. Only the active tab, when it alone is wider +/// than the strip, is cut. +fn fit_doc_tabs(natural: &[f32], selected: usize, avail: f32) -> crate::tab_strip::TabFit { + let widest = natural.iter().copied().filter(|w| w.is_finite()).fold(0.0, f32::max); + crate::tab_strip::fit(natural, selected, avail, widest, crate::tab_strip::CHEVRON_W) +} + +/// Photoshop's tab title: "name @ 50% (Layer 1, RGB/8)", "(Layer 1, Layer Mask/8)" when the mask +/// is targeted, "(Quick Mask/8)" in Quick Mask mode; the Background layer's name is omitted. +pub(super) fn pro_tab_title(app: &PhotocraftApp, i: usize) -> String { + tab_title_at(app, i, app.ui.views.get(i).map_or(100.0, |v| v.zoom * 100.0)) +} + +/// [`pro_tab_title`] at `zoom` percent (a workspace with its own view, such as Select and Mask). +pub(crate) fn tab_title_at(app: &PhotocraftApp, i: usize, zoom: f32) -> String { + use photocraft_engine::channel_cmds::ChannelTarget; + let Some(st) = app.session.documents().get(i) else { return String::new() }; + // Quick Mask mode targets the mask, not the layer (see `paint_target`): painting and Free + // Transform change the red overlay, so the tab says so, as Photoshop's does. + let quick_mask = st.doc.quick_mask.is_some() && st.channel_view.target == ChannelTarget::Composite; + let active_layer = st.active_layer.and_then(|id| st.doc.layer(id)).filter(|l| !quick_mask && !st.doc.is_background(l.id)); + let mask = app.session.active_index() == Some(i) && app.ui.mask_target && active_layer.is_some_and(|l| l.mask.is_some()); + let model = if quick_mask { + tl!("Quick Mask").to_string() + } else if mask { + tl!("Layer Mask").to_string() + } else { + mode_label(&st.doc).to_string() + }; + let layer = active_layer.map(|l| format!("{}, ", l.name)).unwrap_or_default(); + // A save in progress: "name @ 100% (Layer 1, RGB/16) - Saving 45%", as Photoshop does (#3389). + let saving = crate::jobs_ui::save_progress(app, st.doc.id).map(|s| format!(" - {s}")).unwrap_or_default(); + format!("{} @ {}% ({layer}{model}/{}){}{saving}", st.doc.name, fmt_zoom(zoom), st.doc.depth.bits(), if st.is_dirty() { "*" } else { "" }) +} + +/// The Studio strip's tab name: "name *" when unsaved, "name * - Saving 45%" while it saves (#3389). +fn studio_tab_name(app: &PhotocraftApp, st: &photocraft_engine::DocState) -> String { + let saving = crate::jobs_ui::save_progress(app, st.doc.id).map(|s| format!(" - {s}")).unwrap_or_default(); + format!("{}{}{saving}", st.doc.name, if st.is_dirty() { " *" } else { "" }) +} + +/// Photoshop document tabs: "name @ 33.3% (RGB/8)" on a dark strip; active tab matches panels. +/// Tabs keep their whole titles, and the ones that don't fit move into a » menu at the end of the +/// strip; the active document's tab always stays on the strip (#1276, #4487). +fn pro_tabs(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> TabStrip { + let t = crate::theme::Tokens::get(ui.ctx()); + let documents = docked_document_indices(app); + let active_doc = app.session.active_index().filter(|_| app.jobs.focus.is_none()); + let active = active_doc.and_then(|doc| documents.iter().position(|&d| d == doc)); + let (mut activate, mut close) = (None, None); + let mut tab_action = None; + let mut tab_move = None; + let mut tear_off = None; + let tab_count = documents.len(); + let document_count = app.session.documents().len(); + let dragging = crate::layer_transfer::pointer_if_armed(app, ui.ctx()); + let mut drag_over = None; + let mac = ui.ctx().os() == egui::os::OperatingSystem::Mac; + let large_tabs = app.session.prefs().workspace.large_tabs; + let font = egui::FontId::proportional(if large_tabs { 13.0 } else { 11.5 }); + let tab_h = if large_tabs { 34.0 } else { 26.0 }; + let (strip, _) = ui.allocate_exact_size(egui::vec2(ui.available_width(), tab_h), Sense::hover()); + ui.painter().rect_filled(strip, 0.0, t.tab_strip); + // Files opening in the background (#210) are tabs too ("name (Opening… 45%)" with a progress + // underline); they share the fit's index space, after the documents. + let opening = crate::jobs_ui::open_tabs(app); + let mut titles: Vec = documents.iter().map(|&i| pro_tab_title(app, i)).collect(); + titles.extend(opening.iter().map(|(_, name, frac)| format!("{name} ({} {:.0}%)", tl!("Opening…"), frac * 100.0))); + // Every tab's natural width, whether or not it stays on the strip. + let natural: Vec = titles.iter().map(|title| ui.painter().layout_no_wrap(title.clone(), font.clone(), t.text).size().x + DOC_TAB_PAD).collect(); + let selected = app.jobs.focus.and_then(|job| opening.iter().position(|(open, _, _)| *open == job)).map_or_else(|| active.unwrap_or(0), |p| tab_count + p); + let f = fit_doc_tabs(&natural, selected, strip.width()); + let mut x = strip.left(); + let mut placed: Vec<(usize, Rect)> = Vec::with_capacity(f.shown.len()); + for &(i, w) in &f.shown { + let r = Rect::from_min_size(egui::pos2(x, strip.top()), egui::vec2(w, strip.height())); + placed.push((i, r)); + x = r.right(); + } + let mut doc_tabs = Vec::with_capacity(tab_count); + let (mut focus_open, mut cancel_open) = (None, None); + for &(i, r) in placed.iter().filter(|(i, _)| *i < tab_count) { + let Some(&doc) = documents.get(i) else { continue }; + let Some(title) = titles.get(i) else { continue }; + let g = crate::tab_strip::elided(ui, title, font.clone(), t.text, (r.width() - DOC_TAB_PAD).max(1.0)); + let resp = ui.interact(r, ui.scope_id().with(("ptab", doc)), Sense::click_and_drag()); + let sel = Some(i) == active; + if sel { + ui.painter().rect_filled(r, 0.0, t.chrome); + } else if resp.hovered() { + ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.35)); + } + if dragging.is_some_and(|p| r.contains(p)) { + drag_over = Some(doc); + ui.painter().rect_stroke(r, 0.0, Stroke::new(1.5, t.accent), egui::StrokeKind::Inside); + } + ui.painter().line_segment([r.right_top(), r.right_bottom()], Stroke::new(1.0, t.separator)); + let (xr, at) = pro_tab_layout(r, g.size().y, mac); + let xresp = ui.interact(xr, ui.scope_id().with(("ptabx", doc)), Sense::click()); + // Painted: name the tab and its × for accessibility (and so tests and agents can find them). + resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, sel, g.text())); + xresp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Close"))); + crate::icons::paint(ui, xr, "x", 10.0, if xresp.hovered() { t.text } else { t.text_faint }); + ui.painter().galley_with_override_text_color(at, g, if sel { t.text } else { t.text_faint }); + let cut = natural.get(i).copied().unwrap_or(0.0) > r.width() + 0.5; + let resp = if cut { resp.on_hover_text(title.as_str()) } else { resp }; + if tab_close_clicked(&resp, &xresp) { + close = Some(doc); + } else if resp.clicked() { + activate = Some(doc); + } + match tab_drag(ui, &resp, i, r, strip, &placed, &t) { + Some(TabDrop::Move(to)) => tab_move = Some((doc, move_target_after_filter(&documents, doc, to, document_count))), + Some(TabDrop::Float(at)) => tear_off = Some((doc, at)), + None => {} + } + let has_path = app.session.documents().get(doc).is_some_and(|d| d.path.is_some()); + resp.context_menu(|ui| { + tab_action = tab_context_menu(ui, doc, document_count, has_path); + }); + doc_tabs.push((doc, r)); + } + for &(i, r) in placed.iter().filter(|(i, _)| *i >= tab_count) { + let Some((job, _, frac)) = opening.get(i - tab_count) else { continue }; + let Some(title) = titles.get(i) else { continue }; + let g = crate::tab_strip::elided(ui, title, font.clone(), t.text, (r.width() - DOC_TAB_PAD).max(1.0)); + let resp = ui.interact(r, ui.scope_id().with(("ptabjob", job.0)), Sense::click()); + let sel = app.jobs.focus == Some(*job); + if sel { + ui.painter().rect_filled(r, 0.0, t.chrome); + } else if resp.hovered() { + ui.painter().rect_filled(r, 0.0, t.hover.gamma_multiply(0.35)); + } + ui.painter().line_segment([r.right_top(), r.right_bottom()], Stroke::new(1.0, t.separator)); + crate::jobs_ui::tab_underline(ui, r, *frac, &t); + let (xr, at) = pro_tab_layout(r, g.size().y, mac); + let xresp = ui.interact(xr, ui.scope_id().with(("ptabjobx", job.0)), Sense::click()); + xresp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, tl!("Close"))); + crate::icons::paint(ui, xr, "x", 10.0, if xresp.hovered() { t.text } else { t.text_faint }); + ui.painter().galley_with_override_text_color(at, g, if sel { t.text } else { t.text_faint }); + if xresp.on_hover_text(tl!("Cancel opening")).clicked() { + cancel_open = Some(*job); + } else if resp.clicked() { + focus_open = Some(*job); + } + } + if !f.overflow.is_empty() { + // Pinned to the strip's right end, as Photoshop's is, not after the last tab (#8290). + let left = (strip.right() - crate::tab_strip::CHEVRON_W).max(strip.left()); + let r = Rect::from_min_size(egui::pos2(left, strip.top()), egui::vec2(crate::tab_strip::CHEVRON_W, strip.height())); + let labels: Vec<&str> = titles.iter().map(String::as_str).collect(); + let mut picked = None; + // Like Photoshop's, the menu lists every open document, not only the hidden ones, and checks + // the active one (#4487). + let every: Vec = (0..labels.len()).collect(); + crate::tab_strip::overflow_button(ui, ui.scope_id().with("ptab-overflow"), r, tl!("More documents"), &labels, &every, Some(selected), &mut picked); + match picked { + Some(i) if i < tab_count => activate = documents.get(i).copied(), + Some(i) => focus_open = opening.get(i - tab_count).map(|(job, _, _)| *job).or(focus_open), + None => {} + } + } + open_tab_clicks(app, activate, focus_open, cancel_open); + if let Some(i) = drag_over { + crate::layer_transfer::over_tab(app, ui.ctx(), i); + } + if let Some(i) = close { + let _ = crate::menus::invoke(app, ui.ctx(), "file.close", json!({"document": i})); + } + if let Some((from, to)) = tab_move + && let Err(e) = crate::menus::invoke(app, ui.ctx(), "document.move", json!({"document": from, "to": to})) + { + app.ui.status = e; + app.ui.status_error = true; + } + float_torn_off(app, ui.ctx(), tear_off); + if let Some((id, params)) = tab_action + && let Err(e) = crate::menus::invoke(app, ui.ctx(), id, params) + { + app.ui.status = e; + app.ui.status_error = true; + } + TabStrip { rect: strip, tabs: doc_tabs } +} + +/// Where the document tabs were drawn (opening files' tabs aren't slots): a file dropped on the +/// strip opens at the slot under the pointer. +#[derive(Clone, Debug, PartialEq)] +pub struct TabStrip { + pub rect: Rect, + /// The document tabs shown, left to right, with their document index. When the tabs overflow + /// into the » menu, some documents have no tab here. + pub tabs: Vec<(usize, Rect)>, +} + +impl TabStrip { + /// The document position a drop at `x` opens at: before the first shown tab whose middle is + /// right of it, else after the last shown tab. + pub fn slot(&self, x: f32) -> usize { + match self.tabs.iter().find(|(_, r)| r.center().x >= x) { + Some(&(i, _)) => i, + None => self.tabs.last().map_or(0, |&(i, _)| i.saturating_add(1)), + } + } +} + +/// While files are dragged over the tab strip, an insertion line where they would open. +fn drop_slot_line(app: &mut PhotocraftApp, ui: &egui::Ui) { + if ui.input(|i| i.raw.hovered_files.is_empty()) { + return; + } + let at = app.services.cursor_pos.as_mut().and_then(|f| f(ui.ctx())); + let crate::file_open::DropTarget::Tabs(slot) = app.drop_target(ui.ctx(), at) else { return }; + let Some(tabs) = app.tab_strip.as_ref().map(|s| &s.tabs) else { return }; + let Some((r, after)) = tabs.iter().find(|(i, _)| *i == slot).map(|&(_, r)| (r, false)).or_else(|| tabs.last().map(|&(_, r)| (r, true))) else { + return; + }; + crate::widgets::drop_line(ui, r, after, true, &crate::theme::Tokens::get(ui.ctx())); +} + +/// A Photoshop-style tab's close button and where its title starts. Photoshop puts the × after the +/// title on Windows (and so Linux) and before it on macOS (#619). +fn pro_tab_layout(tab: Rect, title_height: f32, mac: bool) -> (Rect, egui::Pos2) { + let (x, title) = if mac { (tab.left() + 13.0, tab.left() + 26.0) } else { (tab.right() - 13.0, tab.left() + 16.0) }; + (Rect::from_center_size(egui::pos2(x, tab.center().y), egui::vec2(14.0, 14.0)), egui::pos2(title, tab.center().y - title_height / 2.0)) +} + +/// Repeating dot-grid texture for the canvas surround. A theme whose dots are its canvas colour +/// has no pattern to show, so it gets no texture at all rather than an invisible one (#4542). +pub(super) fn dots(ctx: &egui::Context, t: &crate::theme::Tokens) -> Option { + if t.bevel || t.canvas_dot == t.canvas { + return None; + } + let key = egui::Id::unique(("canvas-dots", format!("{:?}", t.kind))); + if let Some(tex) = ctx.data(|d| d.get_temp::(key)) { + return Some(tex.id()); + } + let n = 22usize; + let mut px = vec![Color32::TRANSPARENT; n * n]; + for (dx, dy, a) in [(0usize, 0usize, 255u8), (1, 0, 110), (0, 1, 110), (1, 1, 60)] { + let c = t.canvas_dot; + px[(n / 2 + dy) * n + n / 2 + dx] = Color32::from_rgba_unmultiplied(c.r(), c.g(), c.b(), a); + } + let opts = TextureOptions { + magnification: egui::TextureFilter::Linear, + minification: egui::TextureFilter::Linear, + wrap_mode: egui::TextureWrapMode::Repeat, + mipmap_mode: None, + }; + let tex = ctx.load_texture("canvas-dots", egui::ColorImage::new([n, n], px), opts); + let id = tex.id(); + ctx.data_mut(|d| d.insert_temp(key, tex)); + Some(id) +} + +pub(super) fn paint_dots(ui: &egui::Ui, rect: Rect, enabled: bool) { + if !enabled { + return; + } + let t = crate::theme::Tokens::get(ui.ctx()); + if let Some(id) = dots(ui.ctx(), &t) { + let uv = Rect::from_min_max(Pos2::ZERO, pos2(rect.width() / 22.0, rect.height() / 22.0)); + ui.painter_at(rect).image(id, rect, uv, Color32::WHITE); + } +} + +/// With no document open, double-clicking the empty workspace shows File › Open, as in Photoshop +/// (#5367), and so does the Home screen's blank background. Registered before anything drawn over +/// `area`, so the buttons and links there keep their own clicks; not a keyboard focus stop. +pub(super) fn open_on_double_click(app: &mut PhotocraftApp, ui: &egui::Ui, area: Rect) { + if app.session.documents().is_empty() && ui.interact(area, ui.scope_id().with("empty-workspace"), Sense::CLICK).double_clicked() { + let _ = app.open_dialog_file(); + } +} + +/// Extra OS windows showing documents (multi-window / multi-monitor). +pub fn extra_windows(app: &mut PhotocraftApp, ctx: &egui::Context) { + let wins = app.ui.windows.clone(); + for w in wins.into_iter().filter(|w| w.open) { + let Some(st) = app.session.documents().get(w.document) else { continue }; + let title = format!("{} — window {}", st.doc.name, w.id); + let vid = egui::ViewportId::from_hash_of(("docwin", w.id)); + let mut builder = egui::ViewportBuilder::default().with_title(title).with_inner_size([800.0, 600.0]); + if let Some([x, y]) = w.position { + builder = builder.with_position([x, y]); + } + let mut view = w.view.clone(); + let mut close = false; + let mut focused = false; + ctx.show_viewport_immediate(vid, builder, |ui, class| { + if ui.ctx().input(|i| i.viewport().close_requested()) { + close = true; + } + focused = ui.ctx().input(|i| i.viewport().focused == Some(true)); + if focused && !w.focused && app.session.active_index() != Some(w.document) { + app.session.set_active(w.document); + } + egui::CentralPanel::default().frame(egui::Frame::NONE.fill(crate::theme::Tokens::get(ui.ctx()).canvas)).show(ui, |ui| { + let rect = ui.available_rect_before_wrap(); + view = canvas_view(app, ui, w.document, rect, view.clone(), focused); + // Preferences › Zoom Resizes Windows: a zoom wraps the window around the image. + crate::zoom_window_fit::follow(app, ui.ctx(), class, vid, &w, &mut view, rect); + }); + }); + if let Some(win) = app.ui.windows.iter_mut().find(|x| x.id == w.id) { + win.view = view; + win.focused = focused; + if close { + win.open = false; + } + } + } + app.ui.windows.retain(|w| w.open); +} + +/// Window › Arrange tiles: each document shown with its tile in `rect`, or `None` when the +/// active document fills the area. +pub(super) fn docked_document_indices(app: &PhotocraftApp) -> Vec { + (0..app.session.documents().len()).filter(|&doc| !crate::view_cmds::document_is_floating(app, doc)).collect() +} + +pub(super) fn main_canvas_document(app: &PhotocraftApp) -> Option { + let active = app.session.active_index()?; + if !crate::view_cmds::document_is_floating(app, active) { + return Some(active); + } + docked_document_indices(app).into_iter().next() +} + +/// Convert a final tab index in the visible (docked-only) strip to the document list index, +/// keeping hidden floating documents in their existing relative order. +pub(super) fn move_target_after_filter(visible: &[usize], from: usize, to: usize, document_count: usize) -> usize { + let remaining: Vec = visible.iter().copied().filter(|&doc| doc != from).collect(); + match remaining.get(to) { + Some(&doc) if doc > from => doc - 1, + Some(&doc) => doc, + None => document_count.saturating_sub(1), + } +} diff --git a/crates/ui-egui/src/canvas/tabs_tests.rs b/crates/ui-egui/src/canvas/tabs_tests.rs index 4cc2bca0f0..677949600b 100644 --- a/crates/ui-egui/src/canvas/tabs_tests.rs +++ b/crates/ui-egui/src/canvas/tabs_tests.rs @@ -1,5 +1,6 @@ -//! #1276: a document tab strip with more tabs than fit keeps the active document's tab on the -//! strip, shrinks and elides the others, and moves the rest into a » menu (whole app, real tree). +//! #1276, #4487: a document tab strip with more tabs than fit keeps the active document's tab on +//! the strip, keeps whole titles on the tabs it shows, and moves the rest into a » menu (whole +//! app, real tree). use egui::vec2; use egui_kittest::Harness; @@ -166,3 +167,82 @@ fn ctrl_tab_with_one_document_or_a_focused_field() { } assert!(!Focus::Widget.allows(&crate::shortcuts::parse("Tab").unwrap()), "a focused widget keeps plain Tab"); } + +#[test] +fn quick_mask_mode_names_the_quick_mask_in_the_tab() { + let mut h = harness(1, ThemeKind::Pro); + h.state_mut().run("layer.new.layer", json!({})).unwrap(); + assert_eq!(pro_tab_title(h.state(), 0), "ch1-p01.png @ 100% (Layer 1, RGB/8)*"); + h.state_mut().run("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + assert_eq!(pro_tab_title(h.state(), 0), "ch1-p01.png @ 100% (Quick Mask/8)*"); + h.state_mut().run("select.editInQuickMaskMode", json!({"on": false})).unwrap(); + assert_eq!(pro_tab_title(h.state(), 0), "ch1-p01.png @ 100% (Layer 1, RGB/8)*"); +} + +/// Whether any title painted on the strip was cut with '…'. +fn elided_on_strip(h: &Harness<'_, PhotocraftApp>) -> bool { + fn elided(shape: &egui::Shape, strip: egui::Rect) -> bool { + match shape { + egui::Shape::Text(text) => text.galley.elided && strip.intersects(text.visual_bounding_rect()), + egui::Shape::Vec(shapes) => shapes.iter().any(|shape| elided(shape, strip)), + _ => false, + } + } + let strip = strip(h).rect; + h.output().shapes.iter().any(|shape| elided(&shape.shape, strip)) +} + +/// #4487: a crowded strip keeps whole names on the tabs it shows and moves the rest into the » +/// menu instead of shrinking them all, and the menu lists every open document with the active one +/// checked, as Photoshop's does. +#[test] +fn crowded_tabs_keep_their_whole_names_and_the_menu_lists_every_document() { + for theme in [ThemeKind::Pro, ThemeKind::Studio] { + let mut h = harness(14, theme); + let s = strip(&h); + assert!(s.tabs.len() > 1 && s.tabs.len() < 14, "{theme:?}: {} shown", s.tabs.len()); + assert!(!elided_on_strip(&h), "{theme:?}: no tab title is cut"); + // The last new document is active; then one that was behind the menu, then the first. + for active in [13, 5, 0] { + h.state_mut().session.set_active(active); + h.run_steps(2); + let pro = crate::theme::Tokens::get(&h.ctx).pro; + let labels: Vec = + (0..14).map(|i| if pro { pro_tab_title(h.state(), i) } else { h.state().session.documents()[i].doc.name.clone() }).collect(); + h.get_by_label("More documents").click(); + h.run_steps(2); + for (i, label) in labels.iter().enumerate() { + // The menu bar's padding: an unchecked row keeps the check mark's room, so names line up. + let (on, off) = (format!("✔ {label}"), format!(" {label}")); + let (want, other) = if i == active { (&on, &off) } else { (&off, &on) }; + assert_eq!(h.query_all_by_label(want).count(), 1, "{theme:?}, active {active}: document {i} is in the menu as {want:?}"); + assert_eq!(h.query_all_by_label(other).count(), 0, "{theme:?}, active {active}: document {i} is not {other:?}"); + } + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.query_by_label(&format!("✔ {}", labels[active])).is_none(), "{theme:?}: the menu closed"); + } + } +} + +/// #8290: the » button sits at the strip's right end, as Photoshop's does, not right after the +/// last tab that fits (which left a gap of up to a tab's width after it), in both layouts: it is +/// in the same place however many tabs fit before it. +#[test] +fn the_overflow_button_is_pinned_to_the_right_end() { + for theme in [ThemeKind::Pro, ThemeKind::Studio] { + let at = |docs: usize| { + let h = harness(docs, theme); + let s = strip(&h); + let button = h.get_by_label("More documents").rect(); + let last = s.tabs.iter().map(|(_, r)| r.right()).fold(f32::MIN, f32::max); + assert!(button.left() >= last - 0.5, "{theme:?}: » {button:?} overlaps the last tab (ends at {last})"); + // At the end of the strip (the Studio row keeps a small inset). + assert!(button.right() <= s.rect.right() + 0.5 && button.right() >= s.rect.right() - 10.0, "{theme:?}: » at {button:?} in {:?}", s.rect); + (button, last) + }; + let ((a, last_a), (b, last_b)) = (at(14), at(23)); + assert!((last_a - last_b).abs() > 1.0, "{theme:?}: the fits must end at different places to tell"); + assert_eq!(a, b, "{theme:?}: the » button doesn't follow the last tab"); + } +} diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs new file mode 100644 index 0000000000..6bce89cc0d --- /dev/null +++ b/crates/ui-egui/src/canvas/tests.rs @@ -0,0 +1,3008 @@ +/// #5472: a ⇧ square pressed between pixels snapped to 101 × 102; the short side grows to +/// match, on the side the drag points to. +#[test] +fn square_px_equalises_the_sides_away_from_the_press_point() { + use super::{marquee_px, square_px}; + // Down-right drag: the continuous square (50.3, 50.7)..(150.8, 151.2) snaps unevenly. + let r = marquee_px([50.3, 50.7], [150.8, 151.2]); + assert_eq!(r, [50.0, 50.0, 151.0, 152.0], "uneven before the fix"); + assert_eq!(square_px(r, [50.3, 50.7], [150.8, 90.0]), [50.0, 50.0, 152.0, 152.0]); + // Up-left drag: the short side grows towards lower coordinates. + let r = marquee_px([150.8, 151.2], [50.3, 50.7]); + assert_eq!(square_px(r, [150.8, 151.2], [60.0, 50.7]), [49.0, 50.0, 151.0, 152.0]); + // Already square: unchanged. + assert_eq!(square_px([10.0, 20.0, 30.0, 40.0], [10.0, 20.0], [30.0, 40.0]), [10.0, 20.0, 30.0, 40.0]); +} + +/// #6015: a Fixed Size marquee pressed between pixels selected one pixel more on each side. +#[test] +fn fixed_size_px_is_exactly_the_size() { + use super::fixed_size_px; + // Down-right from (10.4, 20.6): 10 × 10 from the closest pixel edges. + assert_eq!(fixed_size_px([10.4, 20.6], [20.4, 30.6], 10.0, 10.0), [10.0, 21.0, 20.0, 31.0]); + // Up-left: the far corner is the low one; still exactly the size. + assert_eq!(fixed_size_px([10.4, 20.6], [-53.6, -11.4], 64.0, 32.0), [-54.0, -11.0, 10.0, 21.0]); + // 1 × 1 is one pixel, and a fractional size rounds to whole pixels. + assert_eq!(fixed_size_px([3.5, 3.5], [4.5, 4.5], 1.0, 1.0), [4.0, 4.0, 5.0, 5.0]); + assert_eq!(fixed_size_px([0.0, 0.0], [2.6, 2.4], 2.6, 2.4), [0.0, 0.0, 3.0, 2.0]); +} + +#[test] +fn display_image_matches_egui_unmultiplied_conversion() { + // Several parallel pieces and a ragged one; translucent, out-of-range and NaN values. + let n: usize = 3 * 16 * 1024 + 77; + let px: Vec<[f32; 4]> = (0..n) + .map(|i| { + let v = match i % 9 { + 0 => f32::NAN, + 1 => -0.5, + 2 => 3.25, + _ => (i % 1001) as f32 / 1000.0, + }; + [v, (i % 257) as f32 / 256.0, 1.0 - (i % 113) as f32 / 112.0, (i % 6) as f32 / 5.0] + }) + .collect(); + let buf = photocraft_compose::Buffer { rect: photocraft_geom::Rect::new(0, 0, n as i32, 1), px }; + let reference = |img: photocraft_raster::Rgba8Image| egui::ColorImage::from_rgba_unmultiplied([img.width as usize, img.height as usize], &img.pixels); + assert_eq!(super::display_image(None, &buf).pixels, reference(buf.to_rgba8()).pixels); + let mut doc = photocraft_doc::Document::new("t", photocraft_geom::Size::new(1, 1), photocraft_doc::ColorMode::Rgb, photocraft_doc::SampleType::U8); + doc.icc_profile = Some(photocraft_cms::Builtin::AdobeRgbCompat.profile().to_bytes()); + let mut color = photocraft_engine::color_cmds::ColorState::default(); + color.settings.monitor_profile = "srgb".into(); + let d = color.canvas_display(&doc).unwrap(); + assert!(!d.is_identity()); + assert_eq!(super::display_image(Some(&d), &buf).pixels, reference(d.to_rgba8(&buf)).pixels); +} + +/// #7490: the inline (web) preview path composites and uploads each changed preview once, as +/// the background worker does. It used to flatten and upload it twice and record it twice, +/// so the frame's refresh time counted the preview twice. +/// The CPU canvas (no usable GPU; a flipped or pixel-aspect-corrected view, or a GPU fallback, +/// on a GPU machine) shows a filter dialog's live preview. It showed the document unfiltered. +#[test] +fn the_cpu_canvas_shows_a_filter_dialogs_preview() { + let pixels = |ctx: &egui::Context, id: egui::TextureId| { + let mut delta = ctx.tex_manager().write().take_delta(); + let egui::ImageData::Color(image) = &delta.set.get(&id).and_then(|d| d.last()).expect("the canvas texture was written").image; + let (size, px) = (image.size, image.pixels.clone()); + delta.clear(); + (size, px) + }; + for (size, zoom) in [([64u32, 48u32], 1.0f32), ([2400, 2200], 0.25)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": size[0], "height": size[1], "background": "white"})).unwrap(); + app.run("edit.fill", json!({"color": "#3399cc"})).unwrap(); + app.ui.views.get_mut(0).expect("the view").zoom = zoom; + let ctx = egui::Context::default(); + let (tex, _) = ensure_texture(&mut app, &ctx, 0, None).unwrap(); + let (_, plain) = pixels(&ctx, tex); + crate::filter_dialog::open(&mut app, "filter.stylize.solarize").unwrap(); + let (tex, _) = ensure_texture(&mut app, &ctx, 0, None).unwrap(); + let (tex_size, shown) = pixels(&ctx, tex); + let preview = app.filter_preview.as_ref().and_then(|p| p.result.clone()).expect("the dialog's preview"); + let (display, _) = canvas_display(&app, &preview, None); + let expected = display_image(display.as_deref(), &photocraft_compose::flatten(&preview)); + assert_eq!(tex_size, expected.size, "{size:?}: the preview's own size (a proxy when zoomed out)"); + assert_eq!(shown, expected.pixels, "{size:?}: the canvas shows the preview"); + assert_ne!(shown.first(), plain.first(), "{size:?}: not the unfiltered document"); + } + // A preview that changes the canvas's dimensions covers its own area, centred on the + // document's (as the GPU canvas centres it), rather than being stretched over it. + let size = |w, h| photocraft_geom::Size::new(w, h); + assert_eq!(preview_bounds(size(64, 32), size(64, 32), 1), DRect::new(0, 0, 64, 32)); + assert_eq!(preview_bounds(size(2401, 2200), size(1201, 1100), 2), DRect::new(0, 0, 2401, 2200), "a proxy"); + assert_eq!(preview_bounds(size(64, 32), size(32, 64), 1), DRect::new(16, -16, 48, 48), "rotated"); + assert_eq!(preview_bounds(size(64, 32), size(80, 40), 1), DRect::new(-8, -4, 72, 36), "canvas grown"); + assert_eq!(preview_bounds(size(4000, 2000), size(1000, 2000), 2), DRect::new(1000, -1000, 3000, 3000), "a rotated proxy"); + + // Image › Image Rotation › Arbitrary 90° on a 64×32 document: its 32×64 preview is drawn over + // the centred 32×64 area. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 64, "height": 32, "background": "white"})).unwrap(); + let id = crate::filter_dialog::open(&mut app, "image.rotation.arbitrary").unwrap(); + let d = app.ui.dialogs.iter_mut().find(|d| d.id == id).unwrap(); + d.fields.insert("angle".into(), json!(90.0)); + d.fields.insert("__preview".into(), json!(true)); + let ctx = egui::Context::default(); + let t = ensure_texture_in_view(&mut app, &ctx, 0, None, None).unwrap(); + let (size, _) = pixels(&ctx, t.texture); + assert_eq!(size, [32, 64], "the rotated preview's texture"); + assert_eq!(t.bounds, DRect::new(16, -16, 48, 48), "centred on the document"); + let centre = |r: DRect| (r.x0 + r.x1, r.y0 + r.y1); + assert_eq!(centre(t.bounds), centre(app.session.active().unwrap().doc.bounds()), "the same centre, so the view's pan holds"); + // Preview off, then Cancel: the document again, unchanged. + let revision = app.session.active().unwrap().revision; + app.ui.dialogs.iter_mut().find(|d| d.id == id).unwrap().fields.insert("__preview".into(), json!(false)); + let t = ensure_texture_in_view(&mut app, &ctx, 0, None, None).unwrap(); + assert_eq!(t.bounds, DRect::new(0, 0, 64, 32), "Preview off"); + crate::dialogs::cancel(&mut app, id).unwrap(); + let t = ensure_texture_in_view(&mut app, &ctx, 0, None, None).unwrap(); + assert_eq!(t.bounds, DRect::new(0, 0, 64, 32), "after Cancel"); + assert_eq!(app.session.active().unwrap().revision, revision, "the document untouched"); + assert_eq!(app.session.active().unwrap().doc.size, photocraft_geom::Size::new(64, 32)); +} + +/// A channel overlay drawn from a size-changing preview covers the preview's own area, as the +/// image does, not the document's: a 90° rotation of a 64×32 document shows a 32×64 overlay. +#[test] +fn a_channel_overlay_follows_a_size_changing_preview() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 64, "height": 32, "background": "white"})).unwrap(); + app.run("channel.new", json!({"name": "Alpha", "fill": "black"})).unwrap(); + let id = crate::filter_dialog::open(&mut app, "image.rotation.arbitrary").unwrap(); + let d = app.ui.dialogs.iter_mut().find(|d| d.id == id).unwrap(); + d.fields.insert("angle".into(), json!(90.0)); + d.fields.insert("__preview".into(), json!(true)); + let ctx = egui::Context::default(); + for rotation in [0.0f32, 30.0] { + let view = View { zoom: 4.0, center: [32.0, 16.0], rotation, ..app.ui.views[0].clone() }; + let mut output = ctx.run_ui(Default::default(), |ui| { + canvas_view(&mut app, ui, 0, Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)), view.clone(), true); + }); + output.textures_delta.clear(); + let overlay = crate::channel_view::ensure(&mut app, &ctx, 0).expect("the channel overlay"); + let mesh = output + .shapes + .iter() + .find_map(|s| match &s.shape { + egui::Shape::Mesh(m) if m.texture_id == overlay => Some(m.clone()), + _ => None, + }) + .expect("the overlay is painted"); + // The quad's sides, in screen points: the two shortest distances from a corner (the + // third is the diagonal; a rotated view turns the quad, not its side lengths). + let v: Vec = mesh.vertices.iter().map(|v| v.pos).collect(); + let mut d: Vec = v[1..4].iter().map(|p| v[0].distance(*p)).collect(); + d.sort_by(f32::total_cmp); + let (short, long) = (d[0], d[1]); + // Which side is the width: the one along the view's x axis turned by the rotation. + let along = |p: Pos2| { + let r = rotation.to_radians(); + ((p - v[0]).x * r.cos() + (p - v[0]).y * r.sin()).abs() + }; + let width = v[1..4].iter().map(|p| along(*p)).fold(0.0f32, f32::max); + assert!((long / short - 2.0).abs() < 0.01, "rotation {rotation}: sides {short}×{long}, the preview's 1:2"); + assert!((width - short).abs() < 0.5, "rotation {rotation}: the short side ({short}) runs across the view (width {width})"); + } +} + +#[test] +fn inline_filter_preview_is_composited_and_recorded_once() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 320, "height": 240})).unwrap(); + crate::filter_dialog::open(&mut app, "filter.stylize.solarize").unwrap(); + app.perf.stage = Default::default(); + let (_, _, size) = ensure_filter_preview(&mut app, 0, &egui::Context::default()).expect("a preview"); + assert_eq!(size, [320, 240]); + assert_eq!(app.perf.last_refresh, "filter-preview"); + assert_eq!(app.perf.last_refresh_px, 320 * 240); + assert_eq!(app.perf.stage.composite_ms + app.perf.stage.upload_ms, app.perf.composite_ms + app.perf.upload_ms, "one record per preview"); + // Unchanged request: nothing is recomputed or recorded again. + ensure_filter_preview(&mut app, 0, &egui::Context::default()).expect("the same preview"); + assert_eq!(app.perf.stage.composite_ms + app.perf.stage.upload_ms, app.perf.composite_ms + app.perf.upload_ms); +} + +/// #6326: a filter's live preview is never coarser than the screen. On a document big enough +/// for a reduced proxy, the view at 100 % previews at full resolution, and only a view zoomed +/// out far enough uses the proxy. Through `ensure_filter_preview`, so the factor the canvas +/// actually requests is the one under test. +#[test] +fn filter_preview_at_100_percent_is_full_resolution() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.background_jobs = false; + app.run("file.new", json!({"width": 2400, "height": 2200})).unwrap(); + assert_eq!(crate::proxy::preview_factor(&app.session.active().unwrap().doc, true), 2, "big enough for a proxy"); + crate::filter_dialog::open(&mut app, "filter.stylize.solarize").unwrap(); + let ctx = egui::Context::default(); + let preview_at = |app: &mut PhotocraftApp, zoom: f32| { + app.ui.views.get_mut(0).expect("the new document's view").zoom = zoom; + assert_eq!(app.current_zoom(), zoom); + let (k, _, size) = ensure_filter_preview(app, 0, &ctx).expect("a preview"); + (k, size) + }; + assert_eq!(preview_at(&mut app, 1.0), (1, [2400, 2200]), "100 %: every pixel"); + assert_eq!(preview_at(&mut app, 0.25), (2, [1200, 1100]), "zoomed out: the document's proxy"); + // Low Resolution Previews off: full resolution at any zoom, exactly as before #6326. + app.run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": false})).unwrap(); + assert_eq!(preview_at(&mut app, 0.25), (1, [2400, 2200]), "the preference still decides"); +} + +/// The factor itself over the zoom range, and that Preferences › Performance › Low +/// Resolution Previews still decides whether there is a proxy at all (#6326). +#[test] +fn filter_preview_factor_caps_at_the_view_without_overriding_the_preference() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + // 9 MP: reduced previews halve it (empty tiles, so nothing is allocated). + app.run("file.new", json!({"width": 3000, "height": 3000, "background": "transparent"})).unwrap(); + let doc = app.session.active().unwrap().doc.clone(); + let params = json!({"amount": 100, "radius": 4.0, "threshold": 0}); + let k = |app: &mut PhotocraftApp, zoom: f32| { + app.ui.views.get_mut(0).expect("the new document's view").zoom = zoom; + super::filter_preview_factor(app, &doc, "filter.sharpen.unsharpMask", ¶ms) + }; + assert_eq!(k(&mut app, 0.25), 2, "zoomed out the preview stays reduced"); + assert_eq!(k(&mut app, 0.5), 2); + assert_eq!(k(&mut app, 0.75), 1, "past the proxy's resolution it shows every pixel"); + assert_eq!(k(&mut app, 1.0), 1); + assert_eq!(k(&mut app, 4.0), 1); + // A corrupt or unset zoom must not pick a coarser proxy than the document's own. + assert_eq!(k(&mut app, f32::NAN), 1); + assert_eq!(k(&mut app, 0.0), 1); + // Low Resolution Previews off: full resolution at any zoom, the cap a no-op. + app.run("prefs.set", json!({"path": "performance.lowResolutionPreviews", "value": false})).unwrap(); + for zoom in [0.1, 0.25, 1.0, 4.0] { + assert_eq!(k(&mut app, zoom), 1, "zoom {zoom}"); + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn every_filter_previews_off_the_ui_thread_and_a_newer_value_cancels_the_old() { + // Dragging Gaussian Blur's radius to 1000 px froze the window: the preview ran inline in + // the frame, and every slider step waited for the previous blur to finish. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.background_jobs = true; + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let ctx = egui::Context::default(); + let wait_for = |app: &mut PhotocraftApp, radius: f64| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while app.filter_preview.as_ref().is_none_or(|p| p.key.params["radius"].as_f64() != Some(radius)) { + assert!(std::time::Instant::now() < deadline, "no preview for radius {radius}"); + ensure_filter_preview(app, 0, &ctx); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + }; + // The first frame hands the blur to the worker and returns. + assert!(ensure_filter_preview(&mut app, 0, &ctx).is_none()); + assert!(app.filter_preview_worker.busy(), "the preview computes on the worker"); + let first = app.ui.dialogs[0].fields["radius"].as_f64().unwrap(); + wait_for(&mut app, first); + assert!(app.filter_preview.as_ref().unwrap().result.is_some()); + + // A preview for old values occupies the worker until it is cancelled. + let st = app.session.active().unwrap(); + let d = &app.ui.dialogs[0]; + let stale = crate::filter_dialog::FilterPreviewKey { + doc: st.doc.id, + revision: st.revision, + dialog: d.id, + active: st.active_layer, + target: st.channel_view.target, + command: "filter.blur.gaussianBlur".into(), + params: json!({"radius": 999.0}), + k: 1, + }; + let (told, cancelled) = std::sync::mpsc::channel(); + app.filter_preview_worker + .start(stale, ctx.clone(), move |job| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + while !job.cancelled() && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(1)); + } + let _ = told.send(job.cancelled()); + crate::dialogs::filter_preview_worker::Computed { result: None, ms: 0.0 } + }) + .unwrap(); + // The slider moves on and stops: once the value settles, the stale preview is cancelled + // instead of being waited out. + app.ui.dialogs[0].fields.insert("radius".into(), json!(12.0)); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + let was_cancelled = loop { + ensure_filter_preview(&mut app, 0, &ctx); + if let Ok(c) = cancelled.try_recv() { + break c; + } + assert!(std::time::Instant::now() < deadline, "the stale preview was never cancelled"); + std::thread::sleep(std::time::Duration::from_millis(5)); + }; + assert!(was_cancelled, "the stale preview stopped because it was cancelled, not by timing out"); + wait_for(&mut app, 12.0); + assert!(app.filter_preview.as_ref().unwrap().result.is_some()); +} + +// Starts a worker job that computes a real preview for the dialog as it stands, but only +// finishes when the returned sender is signalled. Gating it here keeps these tests off the +// clock: `supersede` cancels a running job once the values have been still for SETTLE_MS, so a +// test that relies on a real blur beating a 150 ms timer is a test that fails on a loaded box. +#[cfg(not(target_arch = "wasm32"))] +fn gated_blur_preview(app: &mut PhotocraftApp, ctx: &egui::Context) -> (std::sync::mpsc::Sender<()>, f64) { + let d = app.ui.dialogs.first().expect("a dialog is open"); + let st = app.session.active().expect("a document is active"); + let command = "filter.blur.gaussianBlur"; + let params = app.with_mask_target(command, crate::filter_dialog::params_of(&d.fields)); + assert_eq!( + crate::filter_dialog::preview_factor(command, ¶ms, 1), + 1, + "a 64x48 document previews at full resolution, and these tests rely on k staying 1" + ); + let key = crate::filter_dialog::FilterPreviewKey { + doc: st.doc.id, + revision: st.revision, + dialog: d.id, + active: st.active_layer, + target: st.channel_view.target, + command: command.into(), + params, + k: 1, + }; + let doc = app.session.documents()[0].doc.clone(); + let documents: Vec<_> = app.session.documents().iter().map(|d| d.doc.clone()).collect(); + let radius = key.params["radius"].as_f64().expect("the dialog seeds a radius"); + let (release, wait) = std::sync::mpsc::channel::<()>(); + app.filter_preview_worker + .start(key.clone(), ctx.clone(), move |_| { + wait.recv_timeout(std::time::Duration::from_secs(60)).expect("the test releases the job"); + let result = crate::filter_dialog::preview_document_for_target( + &doc, + key.active, + &key.command, + &key.params, + key.k, + None, + key.target, + &documents, + &Default::default(), + ) + .map(|d| { + let buffer = photocraft_compose::flatten(&d); + (std::sync::Arc::new(d), buffer) + }); + crate::dialogs::filter_preview_worker::Computed { result, ms: 0.0 } + }) + .expect("the worker starts"); + (release, radius) +} + +// The worker hands its result to the channel only after `compute` returns, so a signal sent +// from inside `compute` would still race that send. These tests drive the canvas until the +// state they need appears instead of assuming a timing. +#[cfg(not(target_arch = "wasm32"))] +fn drive_until(app: &mut PhotocraftApp, ctx: &egui::Context, what: &str, done: impl Fn(&PhotocraftApp) -> bool) { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + while !done(app) { + assert!(std::time::Instant::now() < deadline, "timed out waiting for {what}"); + ensure_filter_preview(app, 0, ctx); + std::thread::sleep(std::time::Duration::from_millis(1)); + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_preview_computed_mid_drag_reaches_the_canvas_and_then_converges() { + // A drag used to paint nothing at all: every completion was dropped for carrying stale + // parameters, so the canvas only ever showed an effect once the pointer had come to rest. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.background_jobs = true; + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let ctx = egui::Context::default(); + let (release, first) = gated_blur_preview(&mut app, &ctx); + + // The pointer moves on before the frame lands. + let moved = first + 4.0; + app.ui.dialogs[0].fields.insert("radius".into(), json!(moved)); + release.send(()).expect("released"); + drive_until(&mut app, &ctx, "the mid-drag frame", |a| a.filter_preview.is_some()); + + // The finished frame is painted even though the dialog has moved on. + let shown = app.filter_preview.as_ref().expect("the mid-drag frame was published"); + assert_eq!(shown.key.params["radius"].as_f64(), Some(first), "the frame is the one the worker finished"); + assert!(shown.result.is_some()); + assert_eq!(app.ui.dialogs[0].fields["radius"].as_f64(), Some(moved), "the slider kept moving"); + // The display gate lets it through, so the canvas has something to draw: this is what + // re-adding `params` to that gate would break. + assert!(ensure_filter_preview(&mut app, 0, &ctx).is_some(), "the canvas draws the mid-drag frame"); + + // And the preview still converges on the value the dialog holds. `fresh` has to stay an + // exact match for that: relaxed, the canvas would believe it already had the current + // request after the first frame of a drag and never compute again. + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); + while app.filter_preview.as_ref().is_none_or(|p| p.key.params["radius"].as_f64() != Some(moved)) { + assert!(std::time::Instant::now() < deadline, "the preview never converged on the current radius"); + ensure_filter_preview(&mut app, 0, &ctx); + std::thread::sleep(std::time::Duration::from_millis(1)); + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_mid_drag_frame_does_not_replace_a_frame_that_is_already_exact() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.background_jobs = true; + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let ctx = egui::Context::default(); + + // A frame for the radius the dialog holds right now reaches the canvas. This also proves + // the hand-built key is the one the canvas builds: it can only be published by the exact + // arm, which requires `finished == request`. + let (release, first) = gated_blur_preview(&mut app, &ctx); + release.send(()).expect("released"); + drive_until(&mut app, &ctx, "the first frame", |a| a.filter_preview.is_some()); + assert_eq!(app.filter_preview.as_ref().expect("published").key.params["radius"].as_f64(), Some(first)); + + // The slider moves away and a frame for the new radius is computed... + let moved = first + 4.0; + app.ui.dialogs[0].fields.insert("radius".into(), json!(moved)); + let (release_moved, _) = gated_blur_preview(&mut app, &ctx); + // ...and back to `first` before that frame lands. + app.ui.dialogs[0].fields.insert("radius".into(), json!(first)); + release_moved.send(()).expect("released"); + + // The frame for `moved` arrives late. It belongs to this preview, but the frame on screen + // is already exact for the dialog, so it must never be shown in place of it. Checked on + // every pass: a wrong frame published and then overwritten before the loop ends would + // otherwise never be seen. + drive_until(&mut app, &ctx, "the late frame to arrive", |a| { + if let Some(shown) = &a.filter_preview { + assert_ne!(shown.key.params["radius"].as_f64(), Some(moved), "a late frame replaced an exact one"); + } + !a.filter_preview_worker.busy() + }); + let shown = app.filter_preview.as_ref().expect("a frame is on screen"); + assert_eq!(shown.key.params["radius"].as_f64(), Some(first), "a late frame replaced an exact one"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_mid_drag_empty_result_leaves_the_preview_alone() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.background_jobs = true; + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let ctx = egui::Context::default(); + + let (release, first) = gated_blur_preview(&mut app, &ctx); + release.send(()).expect("released"); + drive_until(&mut app, &ctx, "the first frame", |a| a.filter_preview.is_some()); + assert!(app.filter_preview.as_ref().expect("published").result.is_some()); + + // The slider moves and a job for the new radius is started, then the slider moves again + // before that job lands: its result is stale by the time it arrives. + let moved = first + 4.0; + app.ui.dialogs[0].fields.insert("radius".into(), json!(moved)); + let d = app.ui.dialogs.first().expect("a dialog is open"); + let st = app.session.active().expect("a document is active"); + let key = crate::filter_dialog::FilterPreviewKey { + doc: st.doc.id, + revision: st.revision, + dialog: d.id, + active: st.active_layer, + target: st.channel_view.target, + command: "filter.blur.gaussianBlur".into(), + params: json!({"radius": moved}), + k: 1, + }; + let (release_empty, wait) = std::sync::mpsc::channel::<()>(); + app.filter_preview_worker + .start(key, ctx.clone(), move |_| { + wait.recv_timeout(std::time::Duration::from_secs(60)).expect("released"); + crate::dialogs::filter_preview_worker::Computed { result: None, ms: 0.0 } + }) + .expect("the worker starts"); + app.ui.dialogs[0].fields.insert("radius".into(), json!(moved + 4.0)); + release_empty.send(()).expect("released"); + + // Storing an empty result here would drop the canvas back to the unfiltered layer for a + // frame. The exact request may still store one; a stale one may not. Checked on every + // pass, because the reopened preview that follows would otherwise repair the symptom. + drive_until(&mut app, &ctx, "the empty frame to arrive", |a| { + if let Some(shown) = &a.filter_preview { + assert!(shown.result.is_some(), "an empty mid-drag result blanked the canvas"); + } + !a.filter_preview_worker.busy() + }); + assert!(app.filter_preview.as_ref().expect("a preview is still shown").result.is_some(), "an empty mid-drag result blanked the canvas"); +} + +#[cfg(not(target_arch = "wasm32"))] +#[test] +fn a_reopened_dialog_never_paints_the_previous_dialogs_preview() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); + app.background_jobs = true; + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let ctx = egui::Context::default(); + let first_dialog = app.ui.dialogs.first().expect("a dialog is open").id; + let (release, _) = gated_blur_preview(&mut app, &ctx); + + // Close that dialog and open another one for the same layer and the same command. Only the + // dialog id differs, which is exactly what `same_target` has to notice. + app.ui.dialogs.clear(); + crate::filter_dialog::open(&mut app, "filter.blur.gaussianBlur").unwrap(); + let second_dialog = app.ui.dialogs.first().expect("a dialog is open").id; + assert_ne!(second_dialog, first_dialog, "a reopened dialog gets a new id"); + release.send(()).expect("released"); + + // The first dialog's frame arrives late and must not be painted under the second one. + // `discard_closed` usually reclaims it a frame earlier, which is why this drives the gate + // directly: the id check is the part that has to hold when that cleanup misses. The + // invariant is checked on every pass, not only at the end, because a wrongly published + // frame can still be replaced by the reopened dialog's own preview before we stop. + drive_until(&mut app, &ctx, "the first dialog's frame to arrive", |a| { + if let Some(preview) = &a.filter_preview { + assert_ne!(preview.key.dialog, first_dialog, "a reopened dialog painted the previous dialog's preview"); + } + !a.filter_preview_worker.busy() + }); +} + +#[test] +fn windows_uses_a_visible_crosshair() { + // #737: Windows' inverting crosshair vanishes over mid-grey; use a black/white glyph. + let ctx = egui::Context::default(); + let painter = egui::Painter::new(ctx, egui::LayerId::background(), egui::Rect::EVERYTHING); + let p = egui::pos2(10.0, 10.0); + assert_eq!(super::visible_crosshair(egui::CursorIcon::Crosshair, &painter, p, true), egui::CursorIcon::None); + assert_eq!(super::visible_crosshair(egui::CursorIcon::Crosshair, &painter, p, false), egui::CursorIcon::Crosshair); + assert_eq!(super::visible_crosshair(egui::CursorIcon::Move, &painter, p, true), egui::CursorIcon::Move); +} + +use super::*; + +/// #569: displays 1 (sRGB) and 4 (Display P3) side by side, and a document filled with an +/// sRGB colour. +fn app_on_two_displays() -> PhotocraftApp { + use photocraft_engine::color_cmds::resolve_profile; + use photocraft_engine::display_color::Display; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.run("edit.fill", json!({"color": "#cc8040"})).unwrap(); + let icc = |id: &str| Some(resolve_profile(id, None, None).unwrap().to_bytes()); + app.session.color.set_displays(Ok(vec![ + Display { id: 1, name: "A".into(), frame: [0.0, 0.0, 100.0, 100.0], profile_name: None, icc: icc("srgb") }, + Display { id: 4, name: "B".into(), frame: [100.0, 0.0, 100.0, 100.0], profile_name: None, icc: icc("display-p3") }, + ])); + app +} + +#[test] +#[ignore = "manual texture-retention measurement on six 6 MP documents"] +fn closed_canvas_memory_measurement() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + ctx.input_mut(|i| i.max_texture_side = 8192); + for cycle in 1..=6 { + app.run("file.new", json!({"width":3000,"height":2000})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + // Drain uploaded image deltas just as a renderer does; count live texture owners. + ctx.tex_manager().write().take_delta().clear(); + app.run("file.close", json!({})).unwrap(); + let bytes: usize = ctx.tex_manager().read().allocated().map(|(_, m)| m.bytes_used()).sum(); + println!("closed cycle={cycle} texture_bytes={bytes} rss={:?}", photocraft_testkit::perf::current_rss_bytes()); + } +} + +#[test] +fn closed_documents_release_navigator_but_keep_open_document_textures() { + let mut app = app_on_two_displays(); + let ctx = egui::Context::default(); + let first = app.session.active().unwrap().doc.clone(); + let a = ensure_texture(&mut app, &ctx, 0, Some(1)).unwrap().0; + // The same handle ownership as navigator_texture, without requiring a GPU adapter. + let tex = ctx.load_texture("navigator-test", egui::ColorImage::filled([64, 64], egui::Color32::WHITE), TextureOptions::LINEAR); + let nav = tex.id(); + app.navigator_textures.insert(first.id, (std::sync::Arc::downgrade(&first), 0, 0, tex)); + app.run("file.new", json!({"width":32,"height":32})).unwrap(); + let other = app.session.active().unwrap().doc.id; + let b = ensure_texture(&mut app, &ctx, 1, Some(1)).unwrap().0; + drop(app.session.close(0)); + app.sync_views(); + assert!(ctx.tex_manager().read().meta(a).is_none()); + assert!(ctx.tex_manager().read().meta(nav).is_none()); + assert!(ctx.tex_manager().read().meta(b).is_some()); + assert!(app.canvases.keys().all(|(id, _)| *id == other)); + assert!(app.navigator_textures.is_empty()); + app.session.add_document((*first).clone(), None); + app.sync_views(); + assert_ne!(ensure_texture(&mut app, &ctx, 1, Some(1)).unwrap().0, a, "reopening a preserved ID creates a fresh texture"); +} + +#[test] +fn only_themes_with_a_dot_pattern_tile_the_canvas_surround() { + // #4542: switching to Pro filled the empty workspace with red blocks on one GPU. Pro's + // dots are its canvas colour, so the tiled texture had nothing to show in the first place. + use crate::theme::ThemeKind; + let tiles = |ctx: &egui::Context, kind: ThemeKind| { + crate::theme::apply(ctx, kind); + let mut output = ctx.run_ui(Default::default(), |ui| paint_dots(ui, ui.max_rect(), true)); + output.textures_delta.clear(); + output.shapes.iter().any(|s| matches!(s.shape, egui::Shape::Mesh(_))) + }; + let uploaded = |ctx: &egui::Context| ctx.tex_manager().read().allocated().filter(|(_, meta)| meta.name == "canvas-dots").count(); + for kind in ThemeKind::ALL { + let ctx = egui::Context::default(); + let dotted = matches!(kind, ThemeKind::Studio | ThemeKind::StudioLight | ThemeKind::SolarizedDark); + assert_eq!(tiles(&ctx, kind), dotted, "{kind:?}"); + assert_eq!(uploaded(&ctx), usize::from(dotted), "{kind:?}"); + } + // The reported switch: a dotted theme, then Pro. + let ctx = egui::Context::default(); + assert!(tiles(&ctx, ThemeKind::Studio)); + assert!(!tiles(&ctx, ThemeKind::Pro)); + assert!(!tiles(&ctx, ThemeKind::ProMedium)); + assert_eq!(uploaded(&ctx), 1, "only Studio's dots were uploaded"); +} + +#[test] +fn closed_documents_release_cpu_canvas_textures() { + let mut app = app_on_two_displays(); + let ctx = egui::Context::default(); + let original = app.session.active().unwrap().doc.clone(); + for cycle in 0..12 { + if cycle > 0 { + app.session.add_document((*original).clone(), None); + app.sync_views(); + } + let a = ensure_texture(&mut app, &ctx, 0, Some(1)).unwrap().0; + let b = ensure_texture(&mut app, &ctx, 0, Some(4)).unwrap().0; + assert_ne!(a, b); + app.run("file.close", json!({})).unwrap(); + assert!(app.canvases.is_empty(), "closed canvas metadata retained at cycle {cycle}"); + assert!(ctx.tex_manager().read().meta(a).is_none(), "first display texture retained"); + assert!(ctx.tex_manager().read().meta(b).is_none(), "second display texture retained"); + } +} + +#[test] +fn cpu_canvas_textures_are_per_display() { + // Two windows of one document on different displays: each has its own texture of + // monitor values, and drawing them in turn re-renders neither. + let mut app = app_on_two_displays(); + let ctx = egui::Context::default(); + let id = app.session.documents()[0].doc.id; + let a = ensure_texture(&mut app, &ctx, 0, Some(1)).unwrap().0; + let b = ensure_texture(&mut app, &ctx, 0, Some(4)).unwrap().0; + assert_ne!(a, b); + app.perf.last_refresh = ""; + for _ in 0..3 { + assert_eq!(ensure_texture(&mut app, &ctx, 0, Some(1)).unwrap().0, a); + assert_eq!(ensure_texture(&mut app, &ctx, 0, Some(4)).unwrap().0, b); + } + assert_eq!(app.perf.last_refresh, "", "no re-render"); + let doc = app.session.documents()[0].doc.clone(); + assert!(canvas_display(&app, &doc, Some(1)).0.unwrap().is_identity()); + assert!(!canvas_display(&app, &doc, Some(4)).0.unwrap().is_identity()); + assert_ne!(app.canvases[&(id, 1)].tex_preview_key, app.canvases[&(id, 4)].tex_preview_key); + // An edit updates both. + app.run("edit.fill", json!({"color": "#2060c0"})).unwrap(); + ensure_texture(&mut app, &ctx, 0, Some(1)); + ensure_texture(&mut app, &ctx, 0, Some(4)); + let rev = app.session.documents()[0].revision; + assert_eq!((app.canvases[&(id, 1)].tex_revision, app.canvases[&(id, 4)].tex_revision), (rev, rev)); +} + +#[test] +fn cpu_canvas_shows_crisp_pixels_when_zoomed_in() { + // #4810: above 100% the CPU canvas shows each pixel as a square, like the GPU canvas's + // sharp nearest filter; below 100% it still filters. Both the first upload and a + // damage-rect update carry the sampling, since the renderer takes it from each delta. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 2, "height": 2})).unwrap(); + let ctx = egui::Context::default(); + let tex = ensure_texture(&mut app, &ctx, 0, None).unwrap().0; + let uploads = |ctx: &egui::Context| { + let mut delta = ctx.tex_manager().write().take_delta(); + let seen = delta.set.get(&tex).into_iter().flatten().map(|d| (d.pos.is_some(), d.options)).collect::>(); + delta.clear(); + seen + }; + let crisp = TextureOptions { magnification: egui::TextureFilter::Nearest, ..TextureOptions::LINEAR }; + assert_eq!(uploads(&ctx), vec![(false, crisp)]); + app.run("select.rect", json!({"x": 0, "y": 0, "width": 1, "height": 1})).unwrap(); + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + uploads(&ctx); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + assert_eq!(ensure_texture(&mut app, &ctx, 0, None).unwrap().0, tex); + assert_eq!(uploads(&ctx), vec![(true, crisp)], "a partial update keeps the crisp sampling"); +} + +/// A device that allows smaller textures than MAX_TEXTURE (2048 on some older GPUs and WebGL +/// contexts, and on the fallback adapter the GPU tests sometimes get under load): the canvas +/// texture is reduced to fit it. A 2600 px document was uploaded at full size, which egui +/// asserts on and the backend can't create. +#[test] +fn cpu_canvas_texture_fits_the_device_limit() { + for (side, (w, h), want) in [(2048, (2600, 1700), [1300, 850]), (2048, (5000, 2000), [1666, 666]), (8192, (5000, 2000), [2500, 1000])] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": w, "height": h})).unwrap(); + let ctx = egui::Context::default(); + ctx.input_mut(|i| i.max_texture_side = side); + let (tex, _) = ensure_texture(&mut app, &ctx, 0, None).unwrap(); + let mut delta = ctx.tex_manager().write().take_delta(); + let sizes: Vec<_> = delta.set.get(&tex).into_iter().flatten().map(|d| d.image.size()).collect(); + delta.clear(); + assert_eq!(sizes, vec![want], "{w}x{h} on a {side} px device"); + } +} + +#[test] +fn cpu_canvas_of_a_reduced_document_stays_bilinear() { + // Over MAX_TEXTURE the texture holds one texel per `factor` pixels: a texel is not a + // document pixel, so showing it as a square would draw blocks that aren't in the image. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": MAX_TEXTURE + 1, "height": 2})).unwrap(); + let ctx = egui::Context::default(); + let (tex, scale) = ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert!(scale < 1.0); + let mut delta = ctx.tex_manager().write().take_delta(); + let uploads: Vec<_> = delta.set.get(&tex).into_iter().flatten().map(|d| d.options).collect(); + delta.clear(); + assert_eq!(uploads, vec![TextureOptions::LINEAR]); +} + +/// #7568: a document over the texture limit was drawn from one reduced texture, so at 100 % +/// it was magnified and blurred. The view now gets the part in sight at one texel per +/// document pixel, about the window's size, whatever the device's limit. +#[test] +fn cpu_canvas_detail_plan_shows_large_documents_at_full_resolution() { + let size = [12000, 8000]; + // A 1920×1080 view in the middle (`visible_doc_rect` adds 2 px around it). + let screen = DRect::new(4998, 2998, 6922, 4082); + for limit in [16384, 8192, 4096, 2048] { + let side = clamp_texture_side(limit); + assert!(texture_factor(size, side) > 1, "the whole document is reduced on a {limit} px device"); + for zoom in [1.0, 2.0, 8.0] { + let plan = detail_plan(size, side, zoom, screen).unwrap_or_else(|| panic!("{limit} px device at {zoom}")); + assert_eq!(plan.factor, 1, "one texel per document pixel at {zoom} on a {limit} px device"); + assert!(plan.region.contains_rect(&screen), "{plan:?}"); + assert!(plan.region.width() <= 1924 + 2 * 256 && plan.region.height() <= 1084 + 2 * 256, "about the view's size: {plan:?}"); + assert!(plan.region.width() < side && plan.region.height() < side, "{plan:?}"); + } + } + // A small pan reuses the region (no new composite). + assert_eq!(detail_plan(size, 4096, 1.0, screen), detail_plan(size, 4096, 1.0, screen.translate(8, -8))); + // A window wider than a small device's limit coarsens rather than exceed it. + let wide = DRect::new(0, 0, 3000, 2000); + let plan = detail_plan(size, 2048, 1.0, wide).unwrap(); + assert_eq!(plan.factor, 2); + assert!(plan.region.width() / 2 < 2048 && plan.region.contains_rect(&wide), "{plan:?}"); + // Zoomed out a little: finer than the reduced whole, as fine as the screen. + let plan = detail_plan(size, 4096, 0.4, DRect::new(0, 0, 4800, 2700)).unwrap(); + assert_eq!(plan.factor, 2); + // Zoomed out far enough, the reduced whole is as fine as the screen: nothing to add. + let whole = DRect::new(0, 0, 12000, 8000); + assert_eq!(detail_plan(size, 4096, 0.25, whole), None); + assert_eq!(detail_plan(size, 4096, 1.0 / 3.0, whole), None, "a detail as large as the document would not fit"); + // A document that fits is uploaded whole at full resolution. + assert_eq!(detail_plan([4000, 3000], 4096, 1.0, screen), None); + // Nothing of the document in view, or a broken zoom. + assert_eq!(detail_plan(size, 4096, 1.0, DRect::new(-500, -500, -10, -10)), None); + for zoom in [0.0, -1.0, f32::NAN, f32::INFINITY] { + assert_eq!(detail_plan(size, 4096, zoom, screen), None, "{zoom}"); + } + assert_eq!(detail_plan([0, 0], 4096, 1.0, screen), None); + assert_eq!(detail_plan([u32::MAX, 8], 4096, 1.0, screen), None); +} + +#[test] +fn cpu_canvas_detail_shows_a_large_documents_own_pixels_at_100_percent() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 3 * MAX_TEXTURE, "height": 8})).unwrap(); + app.run("select.rect", json!({"x": 101, "y": 0, "width": 1, "height": 8})).unwrap(); + app.run("edit.fill", json!({"color": "#000000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + let ctx = egui::Context::default(); + ctx.input_mut(|i| i.max_texture_side = 16384); + let uploads = |ctx: &egui::Context, tex: egui::TextureId| { + let mut delta = ctx.tex_manager().write().take_delta(); + let seen: Vec<_> = delta.set.get(&tex).into_iter().flatten().map(|d| (d.pos, d.options, d.image.clone())).collect(); + delta.clear(); + seen + }; + let row = |image: &egui::ImageData| { + let egui::ImageData::Color(image) = image; + (image.size, image.pixels.clone()) + }; + let view = InView { visible: DRect::new(0, 0, 1000, 8), zoom: 1.0, key: 1 }; + let t = ensure_texture_in_view(&mut app, &ctx, 0, None, Some(view)).unwrap(); + assert!(t.scale < 1.0, "the whole document is reduced"); + let (detail, area) = t.detail.expect("a detail texture at 100 %"); + assert_eq!(area, [0.0, 0.0, 1024.0, 8.0]); + let crisp = TextureOptions { magnification: egui::TextureFilter::Nearest, ..TextureOptions::LINEAR }; + let up = uploads(&ctx, detail); + assert_eq!(up.len(), 1); + let (pos, options, image) = &up[0]; + assert_eq!((*pos, *options), (None, crisp)); + let (size, px) = row(image); + assert_eq!(size, [1024, 8]); + assert_eq!((px[100], px[101], px[102]), (Color32::WHITE, Color32::BLACK, Color32::WHITE), "the 1 px line stays 1 px"); + // Unchanged: nothing is composited again, and a small pan keeps the region. + let panned = InView { visible: DRect::new(4, 0, 1004, 8), ..view }; + assert_eq!(ensure_texture_in_view(&mut app, &ctx, 0, None, Some(panned)).unwrap().detail, Some((detail, area))); + assert!(uploads(&ctx, detail).is_empty()); + // The Navigator's call (no view) leaves the view's detail alone. + ensure_texture(&mut app, &ctx, 0, None).unwrap(); + assert_eq!(app.canvases.values().map(|c| c.detail.len()).sum::(), 1); + // An edit in view updates just its texels. + app.run("select.rect", json!({"x": 300, "y": 0, "width": 1, "height": 8})).unwrap(); + ensure_texture_in_view(&mut app, &ctx, 0, None, Some(view)).unwrap(); + uploads(&ctx, detail); + app.run("edit.fill", json!({"color": "#000000"})).unwrap(); + assert_eq!(ensure_texture_in_view(&mut app, &ctx, 0, None, Some(view)).unwrap().detail.map(|d| d.0), Some(detail)); + let up = uploads(&ctx, detail); + assert_eq!(up.len(), 1); + let (pos, options, image) = &up[0]; + assert_eq!(*options, crisp); + let [x, y] = pos.expect("a partial update"); + let (size, px) = row(image); + assert!(y == 0 && x < 300 && x + size[0] > 300, "the edit's texels: {x} {size:?}"); + assert_eq!((px[299 - x], px[300 - x]), (Color32::WHITE, Color32::BLACK)); + // Zoomed out, the reduced texture is enough: the detail goes. + let far = InView { zoom: 0.25, ..view }; + assert!(ensure_texture_in_view(&mut app, &ctx, 0, None, Some(far)).unwrap().detail.is_none()); + assert!(app.canvases.values().all(|c| c.detail.is_empty())); +} + +#[test] +fn gpu_display_luts_are_per_display() { + let _gpu = photocraft_testkit::gpu_lock(); + let Ok(rs) = std::panic::catch_unwind(|| egui_kittest::wgpu::create_render_state(crate::gpu_canvas::wgpu_setup(), Default::default())) else { + eprintln!("skipping: no GPU adapter"); + return; + }; + let mut app = app_on_two_displays(); + app.set_wgpu(rs); + let doc = app.session.documents()[0].doc.clone(); + let key = doc.id.0; + // sRGB on the sRGB display needs no LUT; on the P3 display it does. + assert_eq!(sync_display_lut(&mut app, &doc, key, Some(1)), 0); + assert_eq!(sync_display_lut(&mut app, &doc, key, Some(4)), 3); + let gpu = app.gpu.clone().unwrap(); + let (s1, s4) = (gpu.display_lut_signature(key, 1).unwrap(), gpu.display_lut_signature(key, 4).unwrap()); + assert_ne!(s1.0, s4.0); + assert!(gpu.has_display_lut(key, 4) && !gpu.has_display_lut(key, 1)); + // Drawing one window leaves the other's LUT alone. + for _ in 0..3 { + assert_eq!(sync_display_lut(&mut app, &doc, key, Some(1)), 0); + assert_eq!(sync_display_lut(&mut app, &doc, key, Some(4)), 3); + } + assert_eq!((gpu.display_lut_signature(key, 1), gpu.display_lut_signature(key, 4)), (Some(s1), Some(s4))); + assert!(gpu.has_display_lut(key, 4)); + // The shared texture's key is the same for both displays. + assert_eq!(texture_key(canvas_display(&app, &doc, Some(1)).0.as_deref()), texture_key(canvas_display(&app, &doc, Some(4)).0.as_deref())); +} + +/// The navigator's update after an edit (its image with the edit's damage, grown by the +/// effect reach, rendered over it) equals a new thumbnail of the edited document. +#[test] +fn navigator_damage_updates_match_a_full_thumbnail() { + use photocraft_compose::{render_reduced_damage, thumbnail_buffer, thumbnail_size}; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 1500, "height": 900, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 200, "y": 150, "width": 300, "height": 200})).unwrap(); + app.run("edit.fill", json!({"color": "#3070a0"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.run("layer.layerStyle.dropShadow", json!({"distance": 30, "size": 20})).unwrap(); + let steps = [ + ("layer.translate", json!({"dx": 37, "dy": -11})), + ("layer.setProps", json!({"opacity": 0.4})), + ("paint.stroke", json!({"points": [[700, 500], [900, 520]], "size": 25})), + ("layer.setProps", json!({"blend": "Multiply"})), + ("layer.new.layer", json!({})), + ]; + for (id, p) in steps { + let before = app.session.active().unwrap().doc.clone(); + app.run(id, p).unwrap(); + let st = app.session.active().unwrap(); + let d = st.last_damage.unwrap_or_else(|| panic!("{id}: a bounded damage")); + let (w, h) = thumbnail_size(&st.doc, NAVIGATOR_SIDE); + let mut shown = thumbnail_buffer(&before, NAVIGATOR_SIDE); + assert_eq!((shown.rect.width(), shown.rect.height()), (w, h)); + if !d.is_empty() { + let patch = render_reduced_damage(&st.doc, w, h, d.inflate(effect_reach(&st.doc.layers))); + let r = patch.rect; + for y in 0..r.height() as usize { + let at = (r.y0 as usize + y) * w as usize + r.x0 as usize; + shown.px[at..at + r.width() as usize].copy_from_slice(&patch.px[y * r.width() as usize..][..r.width() as usize]); + } + } + assert!(shown.px == thumbnail_buffer(&st.doc, NAVIGATOR_SIDE).px, "{id}: the updated navigator differs from a new thumbnail"); + } +} + +#[test] +fn tab_context_uses_clicked_document_and_existing_close_commands() { + let items = tab_context_items(2, 3, true); + assert_eq!(items.iter().map(|(_, id, _, _)| *id).collect::>(), ["file.close", "file.closeOthers", "file.closeAll", "file.revealInFinder"]); + assert_eq!(items[0].2, json!({"document": 2})); + assert_eq!(items[1].2, json!({"document": 2})); + assert_eq!(items[2].2, json!({})); + assert_eq!(items[3].2, json!({"document": 2})); + assert!(items[3].3, "a saved document can be revealed in the file manager"); + assert!(!tab_context_items(2, 3, false)[3].3, "an unsaved document cannot"); + assert!(!tab_context_items(0, 1, false)[1].3); + assert!(items.iter().all(|(_, id, _, _)| photocraft_engine::commands::find(id).is_some())); +} + +#[test] +fn tab_context_reveal_names_the_platform_file_manager() { + let (label, id, _, _) = &tab_context_items(0, 1, true)[3]; + assert_eq!((*label, *id), (crate::menus::reveal_label(), "file.revealInFinder")); +} + +/// Photoshop's document tab × is after the title on Windows and Linux, before it on macOS (#619). +#[test] +fn document_tab_close_button_sits_on_the_platform_side() { + use egui::os::OperatingSystem as Os; + use egui_kittest::kittest::Queryable; + for (os, after_title) in [(Os::Windows, true), (Os::Nix, true), (Os::Mac, false)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 8, "height": 8})).unwrap(); + app.sync_views(); + // Drawn from the second frame, once the Pro theme is set. + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(600.0, 40.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Pro); + h.ctx.set_os(os); + h.state_mut().1 = true; + h.run_steps(2); + let tab = h.get_by_label_contains("Untitled @").rect(); + let x = h.get_by_label("Close").rect(); + assert!(tab.contains_rect(x), "{os:?}: the × is inside its tab"); + assert_eq!(x.center().x > tab.center().x, after_title, "{os:?}: × at {x:?} in tab {tab:?}"); + h.get_by_label("Close").click(); + h.run_steps(2); + assert!(h.state().0.session.documents().is_empty(), "{os:?}: the × closes the document"); + } +} + +/// Middle-clicking a document tab closes it (#2570), in both tab strips, and the tab under the +/// pointer is the one that goes even when another document is active. +#[test] +fn middle_clicking_a_tab_closes_that_document() { + use egui_kittest::kittest::Queryable; + for kind in [crate::theme::ThemeKind::Studio, crate::theme::ThemeKind::Pro] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in ["keep", "doomed", "active"] { + app.run("file.new", json!({"width": 8, "height": 8, "name": name})).unwrap(); + } + app.sync_views(); + assert_eq!(app.session.active_index(), Some(2)); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(700.0, 40.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.state_mut().1 = true; + h.run_steps(2); + h.get_by_label_contains("doomed").click_button(egui::PointerButton::Middle); + h.run_steps(2); + let names: Vec = h.state().0.session.documents().iter().map(|d| d.doc.name.clone()).collect(); + assert_eq!(names, ["keep", "active"], "{kind:?}: the middle-clicked tab closed"); + // A clean document closes straight away; a dirty one would park in `discard`. + assert!(h.state().0.discard.is_none(), "{kind:?}: nothing to prompt about"); + } +} + +/// A middle click goes through the same guarded `file.close` as the ×, so unsaved changes +/// still get the Save / Don’t Save / Cancel prompt instead of being thrown away (#2570). +#[test] +fn middle_clicking_an_edited_tab_prompts_instead_of_discarding() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 8, "height": 8, "name": "edited"})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.sync_views(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(700.0, 40.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Pro); + h.state_mut().1 = true; + h.run_steps(2); + h.get_by_label_contains("edited").click_button(egui::PointerButton::Middle); + h.run_steps(2); + assert!(h.state().0.discard.is_some(), "the unsaved-changes prompt must come up"); + assert_eq!(h.state().0.session.documents().len(), 1, "and the document is still open"); +} + +#[test] +fn spectrum_document_tabs_meet_canvas_without_a_gap() { + for kind in [crate::theme::ThemeKind::Pro, crate::theme::ThemeKind::ProMedium] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + app.sync_views(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(680.0, 480.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| document_area(app, ui)); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.state_mut().1 = true; + h.run_steps(3); + let app = &h.state().0; + let strip = app.tab_strip.as_ref().expect("a document opens with tabs").rect; + let gap = app.last_canvas_rect.top() - strip.bottom(); + assert!(gap.abs() < 0.1, "{kind:?}: document strip should touch canvas, got {gap} pt"); + } +} + +#[test] +fn large_tabs_preference_increases_tab_strip_height() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 8, "height": 8})).unwrap(); + app.sync_views(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(600.0, 60.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::Pro); + h.state_mut().1 = true; + h.run_steps(2); + let normal_h = h.get_by_label_contains("Untitled @").rect().height(); + assert_eq!(normal_h, 26.0); + + h.state_mut().0.session.execute("prefs.set", json!({"values": {"workspace.largeTabs": true}})).unwrap(); + h.run_steps(2); + let large_h = h.get_by_label_contains("Untitled @").rect().height(); + assert_eq!(large_h, 34.0); +} + +#[test] +fn tab_close_others_prompts_for_unsaved_nonactive_document() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 8, "height": 8, "name": "Keep"})).unwrap(); + app.run("file.new", json!({"width": 8, "height": 8, "name": "Edited"})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + assert_eq!(app.session.active_index(), Some(1)); + let (_, id, params, _) = tab_context_items(0, 2, false)[1].clone(); + crate::menus::invoke(&mut app, &egui::Context::default(), id, params).unwrap(); + assert!(app.discard.is_some(), "close others must ask before discarding the edited tab"); + assert_eq!(app.session.documents().len(), 2); +} + +/// The pure half of tab drag-to-reorder (UI-217-6): where a drop lands and which +/// `document.move` target that is. +#[test] +fn tab_drop_index_and_move_target_cover_reorder_drops() { + let r = |x: f32| Rect::from_min_size(egui::pos2(x, 0.0), egui::vec2(100.0, 26.0)); + let placed = vec![(0, r(0.0)), (1, r(100.0)), (2, r(200.0))]; + assert_eq!(tab_drop_index(&placed, 10.0), 0); + assert_eq!(tab_drop_index(&placed, 160.0), 2); + assert_eq!(tab_drop_index(&placed, 350.0), 3, "past the end lands after the last shown tab"); + assert_eq!(tab_move_target(0, 3), 2); + assert_eq!(tab_move_target(2, 0), 0); + assert_eq!(tab_move_target(1, 1), 1, "before itself: no move"); + assert_eq!(tab_move_target(1, 2), 1, "just after itself: no move"); + assert_eq!(tab_move_target(0, 1), 0); +} + +/// Dragging a tab onto another's slot reorders the open documents (UI-217-6), in both the +/// studio and the Photoshop-style strips. +#[test] +fn dragging_a_tab_reorders_the_documents() { + use egui_kittest::kittest::Queryable; + for kind in [crate::theme::ThemeKind::Studio, crate::theme::ThemeKind::Pro] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in ["a", "b", "c"] { + app.run("file.new", json!({"width": 8, "height": 8, "name": name})).unwrap(); + } + app.sync_views(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(700.0, 40.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.state_mut().1 = true; + h.run_steps(2); + let b = h.get_by_label_contains("b").rect(); + let a = h.get_by_label_contains("a").rect(); + let target = egui::pos2(a.left() + 2.0, a.center().y); + h.drag_at(b.center()); + h.run_steps(1); + h.hover_at(target); + h.run_steps(1); + h.drop_at(target); + h.run_steps(2); + let names: Vec = h.state().0.session.documents().iter().map(|d| d.doc.name.clone()).collect(); + assert_eq!(names, ["b", "a", "c"], "{kind:?}: the dragged tab took the dropped slot"); + } +} + +/// Dragging a tab off the strip floats its document in its own OS window instead of +/// reordering (#3733), in both strips; a short wobble below the strip still reorders. +#[test] +fn dragging_a_tab_off_the_strip_floats_it_in_a_window() { + use egui_kittest::kittest::Queryable; + let strip = Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(700.0, 26.0)); + assert!(!torn_off(strip, egui::pos2(50.0, 13.0))); + assert!(!torn_off(strip, egui::pos2(50.0, 26.0 + TEAR_OFF - 1.0)), "a wobble below the strip still reorders"); + assert!(torn_off(strip, egui::pos2(50.0, 26.0 + TEAR_OFF + 1.0))); + assert!(torn_off(strip, egui::pos2(50.0, -TEAR_OFF - 1.0)), "dragged up, off the window's top"); + for kind in [crate::theme::ThemeKind::Studio, crate::theme::ThemeKind::Pro] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in ["a", "b", "c"] { + app.run("file.new", json!({"width": 8, "height": 8, "name": name})).unwrap(); + } + app.sync_views(); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(700.0, 300.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + tabs(app, ui); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.state_mut().1 = true; + h.run_steps(2); + let b = h.get_by_label_contains("b").rect(); + let target = egui::pos2(b.center().x, b.bottom() + 150.0); + h.drag_at(b.center()); + h.run_steps(1); + h.hover_at(target); + h.run_steps(1); + h.drop_at(target); + h.run_steps(2); + let app = &h.state().0; + let names: Vec<&str> = app.session.documents().iter().map(|d| d.doc.name.as_str()).collect(); + assert_eq!(names, ["a", "b", "c"], "{kind:?}: tearing off doesn't reorder"); + assert_eq!(app.ui.windows.iter().map(|w| w.document).collect::>(), [1], "{kind:?}: b floats in its own window"); + assert_eq!(app.session.active_index(), Some(1), "{kind:?}: the floated document is active"); + } +} + +/// An app with a 64 × 64 white document, a clone source and the Clone Stamp at `size` px. +fn clone_overlay_app(size: f32) -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.run("cloneSource.set", json!({"source": [16, 16]})).unwrap(); + app.ui.tool = Tool::CloneStamp; + app.session.tools.brush.size = size; + app +} + +/// One frame of the overlay at (32, 32): its shapes and texture uploads. +fn clone_overlay_frame(ctx: &egui::Context, app: &mut PhotocraftApp, xf: &ViewXform) -> egui::FullOutput { + ctx.run_ui(Default::default(), |ui| { + let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("clone-test"))); + draw_clone_preview(app, &painter, xf, [32.0, 32.0], None); + }) +} + +/// The overlay texture a frame uploaded and the alpha at a document pixel, read from the +/// document area the overlay covers (transparent outside it). +fn clone_overlay_alpha(app: &PhotocraftApp, output: &mut egui::FullOutput) -> impl Fn(i32, i32) -> u8 + use<> { + let cache = app.clone_preview.as_ref().expect("an overlay was drawn"); + let image = output + .textures_delta + .set + .get(&cache.texture.id()) + .and_then(|deltas| deltas.last()) + .map(|d| match &d.image { + egui::ImageData::Color(c) => c.clone(), + }) + .expect("the overlay uploads a texture"); + output.textures_delta.clear(); + let rect = cache.rect; + assert_eq!(image.size, [rect.width() as usize, rect.height() as usize], "the texture covers the overlay's area"); + move |x, y| { + if x < rect.x0 || y < rect.y0 || x >= rect.x1 || y >= rect.y1 { + return 0; + } + image.pixels[(y - rect.y0) as usize * image.width() + (x - rect.x0) as usize].a() + } +} + +fn clone_overlay_view(side: f32, zoom: f32) -> ViewXform { + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(side, side)), zoom, center: [32.0, 32.0], flip: false, rotation: 0.0, aspect: 1.0 } +} + +#[test] +fn clone_overlay_is_circular_and_respects_visibility() { + let ctx = egui::Context::default(); + let mut app = clone_overlay_app(20.0); + let xf = clone_overlay_view(100.0, 1.0); + let revision = app.session.active().unwrap().revision; + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + assert!(output.shapes.iter().any(|s| matches!(s.shape, egui::Shape::Mesh(_))), "the overlay is drawn"); + let alpha = clone_overlay_alpha(&app, &mut output); + assert_eq!(alpha(32, 32), 255, "the centre shows the source"); + assert_eq!(alpha(32 + 8, 32), 255, "inside the hard 10 px radius"); + assert_eq!(alpha(32 + 8, 32 + 8), 0, "the brush square's corner, outside the circle, is clipped"); + assert_eq!(alpha(32 + 12, 32), 0, "past the hard tip's rim"); + assert_eq!(app.session.active().unwrap().revision, revision); + assert!(app.session.presets.clone.active().anchor.is_none()); + let ordinary_key = app.clone_preview.as_ref().unwrap().key.clone(); + app.ui.view.pattern_preview = true; + let mut periodic_output = clone_overlay_frame(&ctx, &mut app, &xf); + assert_ne!(app.clone_preview.as_ref().unwrap().key, ordinary_key, "toggling Pattern Preview invalidates source pixels"); + assert!(!periodic_output.textures_delta.set.is_empty(), "the overlay texture is refreshed without a document edit"); + periodic_output.textures_delta.clear(); + assert_eq!(app.session.active().unwrap().revision, revision); + app.session.presets.clone.overlay.show = false; + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + output.textures_delta.clear(); + assert!(output.shapes.iter().all(|s| !matches!(s.shape, egui::Shape::Mesh(_)))); +} + +/// A soft brush previews soft: the overlay fades towards the rim with the brush's hardness, as +/// the stroke it previews does, and reaches past the radius as a soft tip paints. A brush +/// change redraws an overlay that hasn't moved. +#[test] +fn clone_overlay_follows_the_brush_hardness() { + let ctx = egui::Context::default(); + let mut app = clone_overlay_app(20.0); + let xf = clone_overlay_view(100.0, 1.0); + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + let hard = clone_overlay_alpha(&app, &mut output); + app.session.tools.brush.hardness = 0.0; + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + let soft = clone_overlay_alpha(&app, &mut output); + assert!(hard(32, 32) == 255 && soft(32, 32) > 240, "both show the source at the centre: {} {}", hard(32, 32), soft(32, 32)); + assert_eq!(hard(32 + 8, 32), 255); + assert!(soft(32 + 8, 32) < 200, "a soft tip fades before its rim: {}", soft(32 + 8, 32)); + assert!(soft(32 + 4, 32) > soft(32 + 8, 32), "and fades outwards"); + assert!(hard(32 + 12, 32) == 0 && soft(32 + 13, 32) > 0, "a soft tip reaches past the radius, a hard one doesn't"); + // The largest brush the overlay draws still shows at its softest. + let mut big = clone_overlay_app(1000.0); + big.session.tools.brush.hardness = 0.0; + let mut output = clone_overlay_frame(&ctx, &mut big, &xf); + assert_eq!(clone_overlay_alpha(&big, &mut output)(32, 32), 255, "a 1000 px soft brush previews too"); +} + +/// A scaled Clone Source reads a larger source area than the overlay covers: a large brush's +/// overlay shrinks around the pointer until that area fits the sampling budget, rather than +/// vanishing. +#[test] +fn clone_overlay_of_a_scaled_source_stays_within_the_sampling_budget() { + let ctx = egui::Context::default(); + let xf = clone_overlay_view(100.0, 1.0); + for (size, scale) in [(600.0, 200), (1000.0, 300), (1000.0, 50)] { + let mut app = clone_overlay_app(size); + app.run("cloneSource.set", json!({"width": scale, "height": scale})).unwrap(); + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + assert!(output.shapes.iter().any(|s| matches!(s.shape, egui::Shape::Mesh(_))), "{size} px at {scale} %: the overlay shows"); + assert_eq!(clone_overlay_alpha(&app, &mut output)(32, 32), 255, "{size} px at {scale} %: the source under the pointer"); + } +} + +/// The Healing Brush shares the Clone Stamp's source and overlay (Photoshop's Clone Source +/// panel drives both); other tools show none. +#[test] +fn clone_overlay_shows_for_the_healing_brush_too() { + let ctx = egui::Context::default(); + let xf = clone_overlay_view(100.0, 1.0); + for (tool, shown) in [(Tool::Healing, true), (Tool::CloneStamp, true), (Tool::Brush, false), (Tool::SpotHealing, false)] { + let mut app = clone_overlay_app(20.0); + app.ui.tool = tool; + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + assert_eq!(output.shapes.iter().any(|s| matches!(s.shape, egui::Shape::Mesh(_))), shown, "{tool:?}"); + if shown { + assert_eq!(clone_overlay_alpha(&app, &mut output)(32, 32), 255, "{tool:?}: the source shows under the brush"); + } + output.textures_delta.clear(); + } +} + +#[test] +fn clone_overlay_keeps_magnified_pixels_crisp_on_upload_and_refresh() { + let ctx = egui::Context::default(); + let mut app = clone_overlay_app(20.0); + let xf = clone_overlay_view(200.0, 4.0); + for invert in [false, true] { + app.session.presets.clone.overlay.invert = invert; + let mut output = ctx.run_ui(Default::default(), |ui| { + draw_clone_preview(&mut app, ui.painter(), &xf, [32.0, 32.0], None); + }); + let id = app.clone_preview.as_ref().unwrap().texture.id(); + let upload = output.textures_delta.set.get(&id).and_then(|d| d.last()).expect("clone overlay upload"); + let options = upload.options; + output.textures_delta.clear(); + assert_eq!(options.magnification, egui::TextureFilter::Nearest, "invert={invert}"); + assert_eq!(options.minification, egui::TextureFilter::Linear, "zoomed-out previews remain filtered"); + } +} + +#[test] +fn clone_overlay_blend_mode_mixes_with_the_image_under_the_brush() { + let ctx = egui::Context::default(); + let mut app = clone_overlay_app(20.0); + let xf = clone_overlay_view(100.0, 1.0); + // The overlay texel at the brush centre for `mode`. + let mut centre = |mode: &str| { + app.run("cloneSource.overlay", json!({"blend": mode})).unwrap(); + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + let cache = app.clone_preview.as_ref().unwrap(); + let image = output.textures_delta.set.get(&cache.texture.id()).and_then(|d| d.last()).map(|d| match &d.image { + egui::ImageData::Color(c) => c.clone(), + }); + output.textures_delta.clear(); + let image = image.expect("the overlay uploads a texture"); + let rect = cache.rect; + image.pixels[(32 - rect.y0) as usize * image.width() + (32 - rect.x0) as usize] + }; + // A white source over the white image: Normal shows white, Difference black. + assert_eq!(centre("normal"), Color32::WHITE); + assert_eq!(centre("difference"), Color32::BLACK, "Difference turns black where source and image match"); + assert_eq!(centre("lighten"), Color32::WHITE); +} + +/// A shaped tip clips the Clone Source overlay to its outline, not to the brush circle. +#[test] +fn clone_overlay_is_clipped_to_a_shaped_tip() { + let ctx = egui::Context::default(); + let mut app = clone_overlay_app(20.0); + app.session.tools.brush.roundness = 0.25; + let xf = clone_overlay_view(100.0, 1.0); + let mut output = clone_overlay_frame(&ctx, &mut app, &xf); + let alpha = clone_overlay_alpha(&app, &mut output); + assert_eq!(alpha(32, 32), 255, "the tip's centre shows the source"); + assert_eq!(alpha(32, 24), 0, "inside the circle but outside the flat tip: clipped"); + assert!(alpha(25, 32) > 240, "the tip's long axis stays: {}", alpha(25, 32)); +} + +#[test] +fn wayland_start_screen_hint_does_not_claim_file_drop_works() { + let x11 = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let wayland = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services { is_wayland: true, ..Default::default() }); + assert!(start_screen_drop_hint(&x11).starts_with("Drop")); + // Pasting a copied image file works on Wayland (#338): the hint says how. + let hint = start_screen_drop_hint(&wayland); + assert!(!hint.contains("Drop"), "{hint}"); + assert!(hint.ends_with(&format!("paste a copied image with {}.", crate::notices::paste_hint(&wayland))), "{hint}"); +} + +/// A tiling window manager can hand PhotoCraft a canvas narrower than the Home card (460 +/// points): the two buttons must stay inside the window instead of being clipped at its edges. +#[test] +fn start_screen_buttons_stay_inside_a_narrow_canvas() { + use egui_kittest::kittest::Queryable; + for width in [390.0, 420.0, 520.0] { + let app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(width, 760.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| document_area(app, ui)); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.state_mut().1 = true; + h.run_steps(3); + for label in ["New document", "Join us on Discord"] { + for node in h.query_all_by_label_contains(label) { + let r = node.rect(); + assert!(r.left() >= 0.0 && r.right() <= width, "`{label}` is clipped in a {width}-point-wide canvas: {r:?}"); + } + } + } +} + +/// #4382: Interface › Show home recent files off hides the Home screen's list, not the recents. +#[test] +fn home_recent_list_hides_when_the_preference_is_off() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.push_recent("/tmp/photocraft-217/hidden.psd"); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(900.0, 760.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| document_area(app, ui)); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.state_mut().1 = true; + h.run_steps(3); + assert!(h.query_by_label("hidden.psd").is_some(), "listed by default"); + h.state_mut().0.run("prefs.set", serde_json::json!({"values": {"interface.showHomeRecentFiles": false}})).unwrap(); + h.run_steps(3); + assert!(h.query_by_label("hidden.psd").is_none(), "hidden when off"); + assert_eq!(h.state().0.ui.recent_files.len(), 1, "the list itself is kept"); +} + +/// The Home screen's × takes a file off Recent without opening it, and its right-click menu +/// removes one entry or clears the list; the preferences keep the change (#2691). +#[test] +fn home_recent_entries_can_be_removed() { + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in ["c.tif", "b.psd", "a.png"] { + app.push_recent(&format!("/tmp/photocraft-2691/{name}")); + } + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(900.0, 760.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| document_area(app, ui)); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.state_mut().1 = true; + h.run_steps(3); + let listed = |h: &egui_kittest::Harness<'_, (PhotocraftApp, bool)>| { + let app = &h.state().0; + assert_eq!(app.session.prefs().file_handling.recent_files, app.ui.recent_files, "stored in the preferences"); + app.ui.recent_files.iter().map(|p| crate::file_open::display_name(p)).collect::>() + }; + assert_eq!(listed(&h), ["a.png", "b.psd", "c.tif"]); + assert!(h.query_by_label("Remove b.psd from Recent").is_none(), "the × shows on the hovered row only"); + + h.get_by_label("b.psd").hover(); + h.run_steps(2); + h.get_by_label("Remove b.psd from Recent").click(); + h.run_steps(2); + assert_eq!(listed(&h), ["a.png", "c.tif"]); + assert!(h.state().0.session.documents().is_empty(), "the file was not opened"); + + h.get_by_label("c.tif").click_secondary(); + h.run_steps(2); + h.get_by_label("Remove from Recent").click(); + h.run_steps(2); + assert_eq!(listed(&h), ["a.png"]); + + h.get_by_label("a.png").click_secondary(); + h.run_steps(2); + h.get_by_label("Clear Recent Files").click(); + h.run_steps(2); + assert!(listed(&h).is_empty()); + assert!(h.state().0.session.documents().is_empty()); +} + +#[test] +fn pointer_moves_are_bounded_by_the_press_and_release() { + use egui::{Event, PointerButton, pos2}; + let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + let events = [ + Event::PointerMoved(pos2(1.0, 0.0)), + button(pos2(2.0, 0.0), true), + Event::PointerMoved(pos2(3.0, 0.0)), + Event::PointerMoved(pos2(4.0, 0.0)), + button(pos2(5.0, 0.0), false), + Event::PointerMoved(pos2(6.0, 0.0)), + ]; + // The press is in this frame: only the moves between press and release count. + assert_eq!(pointer_moves(&events, PointerButton::Primary, false), vec![pos2(3.0, 0.0), pos2(4.0, 0.0)]); + // The button was already down when the frame began: the move before the release counts, + // the one after it does not. + assert_eq!(pointer_moves(&events, PointerButton::Primary, true), vec![pos2(1.0, 0.0), pos2(3.0, 0.0), pos2(4.0, 0.0)]); +} + +#[test] +fn freehand_tools_are_the_ones_that_follow_a_path() { + for t in [ + Tool::Brush, + Tool::Pencil, + Tool::MixerBrush, + Tool::ColorReplacement, + Tool::Eraser, + Tool::BackgroundEraser, + Tool::CloneStamp, + Tool::PatternStamp, + Tool::Smudge, + Tool::Dodge, + Tool::Lasso, + Tool::QuickSelection, + ] { + assert!(freehand_tool(t), "{t:?} paints or retouches along a path"); + } + for t in [Tool::Move, Tool::Eyedropper, Tool::Gradient, Tool::Crop, Tool::RectMarquee, Tool::Type, Tool::Hand] { + assert!(!freehand_tool(t), "{t:?} is driven by the pointer's latest position"); + } +} + +#[test] +fn clone_stamp_centre_appears_only_with_option() { + assert!(!brush_tip_centre(Tool::CloneStamp, false, false)); + assert!(brush_tip_centre(Tool::CloneStamp, true, false)); + assert!(brush_tip_centre(Tool::Healing, true, false)); + assert!(!brush_tip_centre(Tool::Healing, false, false)); + assert!(brush_tip_centre(Tool::CloneStamp, false, true)); + assert!(!brush_tip_centre(Tool::PatternStamp, true, false), "Pattern Stamp keeps the brush cursor; Option does not switch it to a clone-source mark"); + assert!(!brush_tip_centre(Tool::QuickSelection, false, true)); + assert!(brush_tip_centre(Tool::BackgroundEraser, false, false)); +} + +#[test] +fn brush_tip_centre_follows_the_crosshair_preference_at_any_size() { + // A large tip used to show the centre mark whatever Show Crosshair in Brush Tip said. + for tool in [Tool::Brush, Tool::Eraser, Tool::PatternStamp, Tool::Dodge] { + assert!(!brush_tip_centre(tool, false, false), "{tool:?}: off"); + assert!(brush_tip_centre(tool, false, true), "{tool:?}: on"); + } +} + +#[test] +fn view_transform_roundtrip() { + for flip in [false, true] { + let xf = + ViewXform { rect: Rect::from_min_size(pos2(100.0, 50.0), vec2(800.0, 600.0)), zoom: 2.5, center: [320.0, 240.0], flip, rotation: 0.0, aspect: 1.0 }; + let s = xf.to_screen(10.0, 20.0); + let d = xf.to_doc(s); + assert!((d[0] - 10.0).abs() < 1e-3 && (d[1] - 20.0).abs() < 1e-3); + assert_eq!(xf.to_screen(320.0, 240.0), xf.rect.center()); + // Flipped, document x grows to the left. + assert_eq!(xf.to_screen(330.0, 240.0).x > xf.rect.center().x, !flip); + let r = xf.doc_rect(DRect::new(0, 0, 10, 10)); + assert!(r.width() > 0.0 && r.height() > 0.0); + } +} + +#[test] +fn eyedropper_ring_colors_pair_the_sampled_and_current_colours() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 20, "background": "transparent"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [0, 0, 20, 20], "fill": "#ff0000"})).unwrap(); + app.run("tools.setColors", json!({"foreground": [0.0, 0.2, 1.0, 1.0]})).unwrap(); + let (new, current) = super::eyedropper_ring_colors(&mut app, 10.0, 10.0).expect("over the red square"); + let near = |c: egui::Color32, r: u8, g: u8, b: u8| { + let d = |a: u8, b: u8| i32::from(a).abs_diff(i32::from(b)); + d(c.r(), r) <= 1 && d(c.g(), g) <= 1 && d(c.b(), b) <= 1 + }; + assert!(near(new, 255, 0, 0), "{new:?}"); + assert!(near(current, 0, 51, 255), "{current:?}"); + // Transparency and points outside the canvas have nothing to compare. + assert!(super::eyedropper_ring_colors(&mut app, 30.0, 10.0).is_none()); + assert!(super::eyedropper_ring_colors(&mut app, -1.0, 10.0).is_none()); +} + +#[test] +fn eyedropper_drag_updates_the_sampled_colour() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 20, "background": "transparent"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [0, 0, 20, 20], "fill": "#ff0000"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [20, 0, 20, 20], "fill": "#00ff00"})).unwrap(); + app.ui.tool = Tool::Eyedropper; + + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE); + assert!(app.session.tools.foreground[0] > 0.99 && app.session.tools.foreground[1] < 0.01); + + tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE); + assert!(app.session.tools.foreground[1] > 0.99 && app.session.tools.foreground[0] < 0.01); +} + +/// #4298: an adjustment layer has no pixels, so after its icon thumbnail is clicked (the +/// Layers panel no longer targets the mask) Fill, the fill keys and the Paint Bucket still +/// edit its mask, as in Photoshop, instead of failing with "not a pixel layer". +#[test] +fn fills_on_an_adjustment_layer_edit_its_mask_when_the_icon_is_targeted() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 20, "background": "white"})).unwrap(); + app.run("layer.newAdjustmentLayer.curves", json!({})).unwrap(); + // Click the Curves icon thumbnail: the panel targets the layer, not its mask. + app.ui.mask_target = false; + app.sync_mask_targets(); + assert!(!app.ui.mask_target, "the panel's target is the icon"); + let mask_at = |app: &PhotocraftApp| { + let st = app.session.active().unwrap(); + st.doc.layer(st.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface.sample_channel(10, 10, 0) + }; + assert_eq!(paint_target(&app), json!("mask")); + assert!(crate::menus::is_enabled(&app, "edit.fill"), "Edit > Fill (⇧F5) is enabled"); + assert!(crate::menus::is_enabled(&app, "edit.fillForeground")); + app.ui.tool = Tool::PaintBucket; + app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 0.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]})).unwrap(); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 10.0, y: 10.0 }, egui::Modifiers::NONE); + assert_eq!(mask_at(&app), 0.0, "the Paint Bucket filled the mask black"); + app.run("edit.fill", json!({"contents": "white"})).unwrap(); + assert_eq!(mask_at(&app), 1.0, "Edit > Fill filled the mask white"); + assert!(!app.ui.mask_target, "filling doesn't move the panel's target"); + // A pixel layer with an untargeted mask still paints its pixels. + app.run("layer.new.layer", json!({})).unwrap(); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.ui.mask_target = false; + assert_eq!(paint_target(&app), json!("pixels")); + assert!(!app.layer_mask_targeted()); +} + +#[test] +fn alt_with_a_painting_tool_samples_the_foreground_instead_of_painting() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 40, "height": 20, "background": "transparent"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [0, 0, 20, 20], "fill": "#ff0000"})).unwrap(); + app.run("shape.create", json!({"kind": "rect", "rect": [20, 0, 20, 20], "fill": "#00ff00"})).unwrap(); + let alt = egui::Modifiers::ALT; + for tool in [Tool::Brush, Tool::Pencil, Tool::Gradient, Tool::PaintBucket] { + app.ui.tool = tool; + app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 1.0, 1.0], "background": [1.0, 1.0, 1.0, 1.0]})).unwrap(); + let rev = app.session.active().unwrap().revision; + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, alt); + assert_eq!(app.session.tools.foreground, [1.0, 0.0, 0.0, 1.0], "{tool:?}: ⌥-click sets the foreground"); + // A drag keeps sampling, even after ⌥ is let go; the background is untouched. + tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE); + assert_eq!(app.session.tools.foreground, [0.0, 1.0, 0.0, 1.0], "{tool:?}"); + tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 10.0 }, egui::Modifiers::NONE); + assert_eq!(app.session.tools.background, [1.0, 1.0, 1.0, 1.0]); + assert_eq!(app.session.active().unwrap().revision, rev, "{tool:?}: sampling must not edit the document"); + assert!(app.drag.is_none() && !app.alt_sampling); + } + // Agents get the same through `ui.pointer` (MCP `ui_pointer`). + app.ui.tool = Tool::Brush; + let ctx = egui::Context::default(); + let events = json!([{"kind": "down", "x": 10, "y": 10}, {"kind": "up", "x": 10, "y": 10}]); + let (req, _rx) = crate::control::ControlRequest::new("ui.pointer", json!({"modifiers": {"alt": true}, "events": events})); + let _ = crate::control::handle(&mut app, &ctx, &req); + assert_eq!(app.session.tools.foreground, [1.0, 0.0, 0.0, 1.0]); + app.run("tools.setColors", json!({"foreground": [0.0, 1.0, 0.0, 1.0]})).unwrap(); + // Without ⌥ the Brush paints again, and ⌥ pressed mid-stroke doesn't switch to sampling. + app.run("layer.new.layer", json!({})).unwrap(); + let rev = app.session.active().unwrap().revision; + tool_event(&mut app, ToolEvent::Down { x: 5.0, y: 5.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 5.0, pressure: 1.0 }, alt); + tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 5.0 }, alt); + assert_eq!(app.session.tools.foreground, [0.0, 1.0, 0.0, 1.0]); + assert!(app.session.active().unwrap().revision > rev, "the stroke was painted"); + // Control+Alt stays the brush-resize gesture, never a sample. + assert!(!alt_samples(Tool::Brush, egui::Modifiers { alt: true, ctrl: true, ..Default::default() })); + assert!(!alt_samples(Tool::Eraser, alt), "⌥ with the Eraser is not the Eyedropper"); +} + +/// #2078: the opaque mask-view texture must show the live stroke, not cover it with +/// the committed mask until the mouse is released. +#[test] +fn mask_view_uploads_each_live_stroke_before_release() { + for depth in [8, 16, 32] { + for mode in ["gray", "overlay"] { + for (width, factor) in [(120u32, 1u32), (513, 3)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": width, "height": 60, "depth": depth})).unwrap(); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.run("view.layerMask", json!({"mode": mode})).unwrap(); + app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 0.0, 1.0]})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 12, "hardness": 1.0, "smoothing": {"amount": 0.0}}})).unwrap(); + app.ui.tool = Tool::Brush; + let ctx = egui::Context::default(); + ctx.input_mut(|i| i.max_texture_side = 256); + let tex = crate::channel_view::ensure(&mut app, &ctx, 0).unwrap(); + ctx.tex_manager().write().take_delta().clear(); + let revision = app.session.documents()[0].revision; + let steps = app.session.documents()[0].history.past_len(); + let layer = app.session.documents()[0].active_layer.unwrap(); + let original = app.session.documents()[0].doc.layer(layer).unwrap().clone(); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + for x in [30.0, 50.0, 70.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + let shown = display_doc(&mut app, 0).0; + assert!(shown.layer(layer).unwrap().mask.as_ref().unwrap().value(x as i32, 30) < 0.1); + assert_eq!(crate::channel_view::ensure(&mut app, &ctx, 0), Some(tex)); + let mut delta = ctx.tex_manager().write().take_delta(); + let uploads = delta.set.get(&tex).unwrap_or_else(|| panic!("{depth}-bit {mode}: live mask texture must upload before release at {x}")); + assert_eq!(uploads.len(), 1); + let upload = &uploads[0]; + let pos = upload.pos.expect("only the damaged region is uploaded"); + let egui::ImageData::Color(image) = &upload.image; + assert!(image.pixels.len() < (width.div_ceil(factor) * 60u32.div_ceil(factor)) as usize, "the whole mask is not uploaded"); + let f = factor as i32; + let r = DRect::new(pos[0] as i32 * f, pos[1] as i32 * f, (pos[0] + image.size[0]) as i32 * f, (pos[1] + image.size[1]) as i32 * f) + .intersect(&shown.bounds()); + let expected = crate::channel_view::render( + &shown, + &app.session.documents()[0].channel_view, + r, + factor, + crate::channel_view::Look::default(), + app.session.documents()[0].active_layer, + ) + .unwrap(); + assert_eq!(image.pixels, expected, "uploaded mask values match the live stroke"); + delta.clear(); + assert_eq!(app.session.documents()[0].revision, revision); + assert_eq!(app.session.documents()[0].history.past_len(), steps); + assert_eq!(app.session.documents()[0].doc.layer(layer).unwrap(), &original); + crate::channel_view::ensure(&mut app, &ctx, 0); + assert!(ctx.tex_manager().write().take_delta().set.is_empty(), "idle frame does not upload"); + } + tool_event(&mut app, ToolEvent::Up { x: 70.0, y: 30.0 }, egui::Modifiers::NONE); + let st = &app.session.documents()[0]; + assert_eq!(st.history.past_len(), steps + 1); + assert!(st.doc.layer(layer).unwrap().mask.as_ref().unwrap().value(50, 30) < 0.1); + assert_eq!(st.doc.layer(layer).unwrap().content, original.content); + crate::channel_view::ensure(&mut app, &ctx, 0); + let mut delta = ctx.tex_manager().write().take_delta(); + // The commit takes the live stroke's pixels, which the texture already shows: at + // most a partial upload, never the whole mask again. + assert!(delta.set.get(&tex).is_none_or(|u| u.iter().all(|u| u.pos.is_some())), "commit keeps its partial upload"); + delta.clear(); + app.session.undo(); + assert_eq!(app.session.documents()[0].doc.layer(layer).unwrap(), &original); + crate::channel_view::ensure(&mut app, &ctx, 0); + let mut delta = ctx.tex_manager().write().take_delta(); + let upload = &delta.set.get(&tex).unwrap()[0]; + let egui::ImageData::Color(image) = &upload.image; + let st = &app.session.documents()[0]; + // Undo redraws only the tiles the stroke changed (#9329): a partial upload of them. + let f = factor as i32; + let pos = upload.pos.expect("undo uploads only the changed tiles, not the whole mask"); + let r = DRect::new(pos[0] as i32 * f, pos[1] as i32 * f, (pos[0] + image.size[0]) as i32 * f, (pos[1] + image.size[1]) as i32 * f) + .intersect(&st.doc.bounds()); + // It covers the stroke's span (x 10..70 along y 30, size 12), so it + // proves coverage and not only values. + assert!(r.contains_rect(&DRect::new(10, 26, 70, 34)), "undo covers the stroke: {r:?}"); + assert_eq!( + image.pixels, + crate::channel_view::render(&st.doc, &st.channel_view, r, factor, crate::channel_view::Look::default(), st.active_layer).unwrap(), + "undo restores the shown mask" + ); + delta.clear(); + } + } + } +} + +#[test] +fn channel_views_show_live_targets_and_discarded_strokes() { + for target in ["quickMask", "alpha", "red"] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60})).unwrap(); + match target { + "quickMask" => { + app.run("select.rect", json!({"x": 0, "y": 0, "width": 120, "height": 60})).unwrap(); + app.run("select.editInQuickMaskMode", json!({})).unwrap(); + } + "alpha" => { + app.run("channel.new", json!({"fill": "white"})).unwrap(); + } + _ => { + app.run("channel.target", json!({"channel": "red"})).unwrap(); + } + } + app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 0.0, 1.0]})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 12, "hardness": 1.0, "smoothing": {"amount": 0.0}}})).unwrap(); + app.ui.tool = Tool::Brush; + let ctx = egui::Context::default(); + let tex = crate::channel_view::ensure(&mut app, &ctx, 0).unwrap(); + ctx.tex_manager().write().take_delta().clear(); + let original = app.session.documents()[0].doc.clone(); + let steps = app.session.documents()[0].history.past_len(); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: 50.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + let shown = display_doc(&mut app, 0).0; + crate::channel_view::ensure(&mut app, &ctx, 0); + let mut delta = ctx.tex_manager().write().take_delta(); + let upload = &delta.set.get(&tex).expect("live channel upload")[0]; + let pos = upload.pos.expect("partial channel upload"); + let egui::ImageData::Color(image) = &upload.image; + let r = DRect::new(pos[0] as i32, pos[1] as i32, (pos[0] + image.size[0]) as i32, (pos[1] + image.size[1]) as i32); + assert_eq!( + image.pixels, + crate::channel_view::render( + &shown, + &app.session.documents()[0].channel_view, + r, + 1, + crate::channel_view::Look::default(), + app.session.documents()[0].active_layer + ) + .unwrap(), + "{target}" + ); + assert_ne!( + image.pixels, + crate::channel_view::render( + &original, + &app.session.documents()[0].channel_view, + r, + 1, + crate::channel_view::Look::default(), + app.session.documents()[0].active_layer + ) + .unwrap(), + "target was painted" + ); + delta.clear(); + assert_eq!(app.session.documents()[0].history.past_len(), steps); + assert!(std::sync::Arc::ptr_eq(&original, &app.session.documents()[0].doc)); + // Dropping a preview without committing must restore its texture, even though the + // document revision did not change. There is no committed damage rectangle to use. + app.drag = None; + app.live_stroke = None; + crate::channel_view::ensure(&mut app, &ctx, 0); + let mut delta = ctx.tex_manager().write().take_delta(); + let upload = &delta.set.get(&tex).unwrap()[0]; + assert!(upload.pos.is_none(), "discarding a preview refreshes the view"); + let egui::ImageData::Color(image) = &upload.image; + assert_eq!( + image.pixels, + crate::channel_view::render( + &original, + &app.session.documents()[0].channel_view, + original.bounds(), + 1, + crate::channel_view::Look::default(), + app.session.documents()[0].active_layer + ) + .unwrap() + ); + delta.clear(); + } +} + +/// The held drag shows true mixed paint while Alt sampling remains separate from painting. +#[test] +fn mixer_drag_shows_actual_mixed_paint_before_release_and_alt_never_paints() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("tools.setColors", json!({"foreground":"#ff0000"})).unwrap(); + app.run("tools.setBrush", json!({"brush":{"size":16,"mixer":{"wet":1.0,"mix":0.7,"sampleAllLayers":true},"smoothing":{"amount":0.0}}})).unwrap(); + app.ui.tool = Tool::MixerBrush; + let before = app.session.active().unwrap().doc.clone(); + let count = app.session.active().unwrap().history.past_len(); + let alt = egui::Modifiers { alt: true, ..Default::default() }; + tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 30.0, pressure: 1.0 }, alt); + tool_event(&mut app, ToolEvent::Move { x: 35.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Up { x: 35.0, y: 30.0 }, egui::Modifiers::NONE); + assert!(app.drag.is_none()); + assert!(app.session.tools.mixer.spatial.is_some()); + assert!(std::sync::Arc::ptr_eq(&before, &app.session.active().unwrap().doc)); + app.run("brush.mixer.load", json!({})).unwrap(); + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + tool_event(&mut app, ToolEvent::Move { x: 70.0, y: 30.0, pressure: 0.6 }, egui::Modifiers::NONE); + assert!(app.live_stroke.is_some()); + let shown = display_doc(&mut app, 0).0; + let id = app.session.active().unwrap().active_layer.unwrap(); + let pixel = shown.layer(id).unwrap().surface().unwrap().rgba(45, 30); + assert!(pixel[0] > 0.0 && pixel[1] > 0.0 && pixel[3] > 0.0, "actual red/green mixture {pixel:?}"); + assert!(std::sync::Arc::ptr_eq(&before, &app.session.active().unwrap().doc)); + assert_eq!(app.session.active().unwrap().history.past_len(), count); + tool_event(&mut app, ToolEvent::Up { x: 70.0, y: 30.0 }, egui::Modifiers::NONE); + let st = app.session.active().unwrap(); + assert_eq!(st.history.past_len(), count + 1); + assert_eq!( + shown.layer(id).unwrap().surface().unwrap().read_region(before.bounds()), + st.doc.layer(id).unwrap().surface().unwrap().read_region(before.bounds()) + ); + app.session.undo(); + assert_eq!(app.session.active().unwrap().doc.layer(id).unwrap().surface().unwrap().rgba(45, 30)[3], 0.0); +} + +/// Release incorporates its final endpoint and deferred samples in both live and fallback paths. +#[test] +fn mixer_release_includes_a_new_endpoint_and_deferred_frame_samples() { + for (deferred, failed) in [(false, false), (true, false), (false, true)] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("tools.setBrush", json!({"size":12,"pressureSize":false})).unwrap(); + app.ui.tool = Tool::MixerBrush; + let before = app.session.active().unwrap().doc.clone(); + let m = egui::Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, m); + app.defer_live_stroke = deferred; + if failed { + app.live_stroke.as_mut().unwrap().preview_failed = true; + } + tool_event(&mut app, ToolEvent::Move { x: 50.0, y: 30.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Up { x: 95.0, y: 30.0 }, m); + let doc = app.session.active().unwrap().doc.clone(); + let id = app.session.active().unwrap().active_layer.unwrap(); + assert!(doc.layer(id).unwrap().surface().unwrap().rgba(92, 30)[3] > 0.0); + let params = app.session.journal.iter().rev().find(|(name, _)| name == "paint.mixerBrush").unwrap().1.clone(); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(*app.session.active().unwrap().doc, *before); + app.run("paint.mixerBrush", params).unwrap(); + assert_eq!(*app.session.active().unwrap().doc, *doc); + } +} + +/// Reproduce a tool change and release in one control call, before a UI frame can cancel the old gesture. +#[test] +fn mixer_control_tool_switch_before_release_cancels_every_preview_path() { + for preview in 0..3 { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("tools.setBrush", json!({"size":12,"pressureSize":false})).unwrap(); + let before = app.session.active().unwrap().doc.clone(); + let steps = app.session.active().unwrap().history.past_len(); + let anchor = app.last_stroke_end; + let ctx = egui::Context::default(); + let (request, _reply) = crate::control::ControlRequest::new( + "ui.pointer", + json!({"tool":"mixerBrush","events":[{"kind":"down","x":20,"y":30},{"kind":"move","x":50,"y":30}]}), + ); + let _ = crate::control::handle(&mut app, &ctx, &request); + assert!(app.drag.is_some() && app.live_stroke.is_some()); + if preview == 1 { + app.live_stroke.as_mut().unwrap().preview_failed = true; + } else if preview == 2 { + app.live_stroke = None; + } + // No intervening UI frame: control selects the new tool before dispatching Up. + let (request, _reply) = crate::control::ControlRequest::new("ui.pointer", json!({"tool":"brush","events":[{"kind":"up","x":95,"y":30}]})); + let _ = crate::control::handle(&mut app, &ctx, &request); + assert_eq!(app.ui.tool, Tool::Brush); + assert!(app.drag.is_none() && app.live_stroke.is_none()); + assert!(std::sync::Arc::ptr_eq(&before, &app.session.active().unwrap().doc)); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + assert_eq!(app.last_stroke_end, anchor); + assert!(!app.session.journal.iter().any(|(id, _)| id == "paint.mixerBrush")); + } +} + +/// Escape and tool changes cancel both ordinary previews and failed-preview fallback gestures. +#[test] +fn mixer_escape_and_tool_switch_cancel_without_an_edit() { + for mode in 0..4 { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.ui.tool = Tool::MixerBrush; + let before = app.session.active().unwrap().doc.clone(); + let steps = app.session.active().unwrap().history.past_len(); + tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + assert!(app.live_stroke.is_some()); + if mode >= 2 { + app.live_stroke.as_mut().unwrap().preview_failed = true; + } + let mode = mode % 2; + let ctx = egui::Context::default(); + let mut raw = egui::RawInput { focused: true, ..Default::default() }; + if mode == 0 { + raw.events.push(egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }); + } + if mode == 1 { + app.ui.tool = Tool::Brush; + } + ctx.begin_pass(raw); + assert!(cancel_mixer_on_context_change(&mut app, &ctx)); + let mut output = ctx.end_pass(); + output.textures_delta.clear(); + tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 30.0 }, egui::Modifiers::NONE); + assert!(app.drag.is_none() && app.live_stroke.is_none()); + assert!(std::sync::Arc::ptr_eq(&before, &app.session.active().unwrap().doc)); + assert_eq!(app.session.active().unwrap().history.past_len(), steps); + } +} + +/// Control-driven strokes remain live in a background window, matching the ordinary Brush. +#[test] +fn mixer_background_window_keeps_and_commits_control_strokes() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"transparent"})).unwrap(); + app.run("tools.setBrush", json!({"brush":{"size":12,"pressureSize":false}})).unwrap(); + let steps = app.session.active().unwrap().history.past_len(); + let ctx = egui::Context::default(); + ctx.begin_pass(egui::RawInput { focused: false, ..Default::default() }); + let (request, _reply) = + crate::control::ControlRequest::new("ui.pointer", json!({"tool":"mixerBrush","events":[{"kind":"down","x":20,"y":30},{"kind":"move","x":60,"y":30}]})); + let _ = crate::control::handle(&mut app, &ctx, &request); + assert!(!cancel_mixer_on_context_change(&mut app, &ctx)); + assert!(app.drag.is_some()); + let (request, _reply) = crate::control::ControlRequest::new("ui.pointer", json!({"tool":"mixerBrush","events":[{"kind":"up","x":95,"y":30}]})); + let _ = crate::control::handle(&mut app, &ctx, &request); + let mut output = ctx.end_pass(); + output.textures_delta.clear(); + assert_eq!(app.session.active().unwrap().history.past_len(), steps + 1); + assert!(app.session.journal.iter().any(|(id, _)| id == "paint.mixerBrush")); + let st = app.session.active().unwrap(); + assert!(st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(60, 30)[3] > 0.0); +} + +/// A tip above the live preview's 1024 px paints on release, as main and Photoshop paint it +/// up to 5000 px; a stroke that can't paint at all is still refused on press. +#[test] +fn mixer_drag_above_the_preview_size_paints_on_release() { + for locked in [false, true] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + if locked { + app.run("layer.lockLayers", json!({"pixels": true})).unwrap(); + } + app.run("tools.setColors", json!({"foreground":"#ff0000"})).unwrap(); + app.run("tools.setBrush", json!({"brush":{"size":2000,"hardness":1.0,"pressureSize":false,"mixer":{"wet":0.0,"mix":0.0}}})).unwrap(); + app.ui.tool = Tool::MixerBrush; + let steps = app.session.active().unwrap().history.past_len(); + let m = egui::Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, m); + assert!(app.live_stroke.is_none(), "no live preview above 1024 px"); + assert_eq!(app.drag.is_some(), !locked, "a valid 2000 px stroke is kept for release"); + assert_eq!(app.ui.status_error, locked, "{}", app.ui.status); + tool_event(&mut app, ToolEvent::Move { x: 50.0, y: 30.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Up { x: 50.0, y: 30.0 }, m); + let st = app.session.active().unwrap(); + assert_eq!(st.history.past_len(), steps + usize::from(!locked)); + let pixel = st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(60, 30); + assert_eq!(pixel[0] > 0.9 && pixel[3] > 0.9, !locked, "{pixel:?}"); + } +} + +/// ⌥ with a held temporary tool (macOS ⌘ = Move) is that tool's, not a Mixer Brush paint load. +#[test] +fn mixer_alt_with_a_held_temporary_tool_does_not_load_paint() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width":120,"height":60,"background":"#00ff00"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.ui.tool = Tool::MixerBrush; + app.tool_override = Some(Tool::Move); + let alt = egui::Modifiers { alt: true, ..Default::default() }; + tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 30.0, pressure: 1.0 }, alt); + tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 30.0 }, alt); + assert!(app.session.tools.mixer.spatial.is_none(), "the held Move tool doesn't sample"); + app.tool_override = None; + tool_event(&mut app, ToolEvent::Down { x: 30.0, y: 30.0, pressure: 1.0 }, alt); + assert!(app.session.tools.mixer.spatial.is_some(), "⌥ with the Mixer Brush itself samples"); +} + +#[test] +fn brush_drag_shows_the_real_stroke_and_commits_it() { + // The drag used to draw a hard, flat stand-in and only showed the soft brush on release. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60, "background": "transparent"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 20, "hardness": 0.0}})).unwrap(); + app.ui.tool = Tool::Brush; + let m = egui::Modifiers::NONE; + let alpha = |d: &Document, x, y| d.layers[0].surface().unwrap().rgba(x, y)[3]; + let rev = app.session.documents()[0].revision; + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 30.0, pressure: 1.0 }, m); + for x in [40.0, 70.0, 100.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 1.0 }, m); + } + let (shown, key) = display_doc(&mut app, 0); + assert_ne!(key, 0); + // A partial value two thirds of the way out: the soft tip, not a hard flat stand-in. + assert!(alpha(&shown, 40, 30) > 0.5 && (0.01..0.95).contains(&alpha(&shown, 40, 36)), "soft stroke while drawing"); + assert_eq!(alpha(&app.session.documents()[0].doc, 40, 30), 0.0, "not committed yet"); + // The canvas redraws only what the stroke touched. + let dk = canvas_display(&app, &app.session.documents()[0].doc, None).1; + let d = damage_since(&app, 0, (rev, dk), (rev, key), dk, None).unwrap(); + assert!(d.contains(40, 30) && !d.contains(40, 2) && d.height() < 60, "{d:?}"); + tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 30.0 }, m); + let doc = app.session.documents()[0].doc.clone(); + assert!(app.live_stroke.is_none() && display_doc(&mut app, 0).1 == 0); + assert_eq!((alpha(&doc, 40, 30), alpha(&doc, 40, 38)), (alpha(&shown, 40, 30), alpha(&shown, 40, 38)), "commit matches the preview"); +} + +#[test] +fn live_stroke_uses_pen_tilt_and_updates_a_reduced_texture_partially() { + // Pen tilt drives the size; the preview must use the tilt the commit gets. The document + // is wider than MAX_TEXTURE, so the CPU texture is reduced (factor 2) and each step + // must update only the reduced pixels it touched. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + ctx.input_mut(|i| i.max_texture_side = 8192); + app.run("file.new", json!({"width": 4097, "height": 90, "background": "transparent"})).unwrap(); + let tilt = json!({"size": 30, "hardness": 1.0, "spacing": 0.05, "shapeDynamics": {"enabled": true, "size": {"control": "penTilt"}}}); + app.run("tools.setBrush", json!({ "brush": tilt })).unwrap(); + app.ui.tool = Tool::Brush; + assert_eq!(ensure_texture(&mut app, &ctx, 0, None).map(|t| t.1), Some(0.5)); + let m = egui::Modifiers::NONE; + app.stylus.feed.set(Some(crate::stylus::PenSample { pressure: 1.0, tilt_x: 60.0, tilt_y: 0.0, rotation: 0.0, eraser: false })); + tool_event(&mut app, ToolEvent::Down { x: 100.0, y: 45.0, pressure: 1.0 }, m); + for x in [300.0, 600.0, 900.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 45.0, pressure: 1.0 }, m); + ensure_texture(&mut app, &ctx, 0, None); + assert_eq!(app.perf.last_refresh, "rect"); + assert!(app.perf.last_refresh_px < 4097 * 90 / 4, "{}", app.perf.last_refresh_px); + } + let shown = display_doc(&mut app, 0).0; + tool_event(&mut app, ToolEvent::Up { x: 900.0, y: 45.0 }, m); + ensure_texture(&mut app, &ctx, 0, None); + assert_eq!(app.perf.last_refresh, "rect", "the commit refreshes only the stroke"); + let doc = app.session.documents()[0].doc.clone(); + let (a, b) = (doc.layers[0].surface().unwrap(), shown.layers[0].surface().unwrap()); + // 60° tilt shrinks the dab well below 30 px: the preview shows that size, as committed. + assert!(a.rgba(500, 45)[3] > 0.5 && a.rgba(500, 45 + 12)[3] == 0.0); + // (The smoothed tail catches up to the end point only when the stroke finishes.) + assert!((0..90).all(|y| (0..700).all(|x| a.rgba(x, y) == b.rgba(x, y))), "commit matches the preview"); +} + +/// Brush drag along y = 40 with one canvas frame per pointer move; returns the document the +/// canvas showed at the last move, the committed one, and whether the release refreshed only +/// the stroke's rectangle. +fn drag_frames(smoothing: f32, xs: &[f64]) -> (std::sync::Arc, std::sync::Arc, bool, PhotocraftApp) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 200, "height": 80, "background": "transparent"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 16, "hardness": 0.5}})).unwrap(); + // What the options bar's Smoothing field writes. + app.session.tools.brush.smoothing.amount = smoothing; + app.ui.tool = Tool::Brush; + ensure_texture(&mut app, &ctx, 0, None); + let m = egui::Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 40.0, pressure: 1.0 }, m); + for &x in xs { + tool_event(&mut app, ToolEvent::Move { x, y: 40.0 + (x / 7.0).sin() * 8.0, pressure: 1.0 }, m); + ensure_texture(&mut app, &ctx, 0, None); + } + let live = display_doc(&mut app, 0).0; + let last = *xs.last().unwrap(); + tool_event(&mut app, ToolEvent::Up { x: last, y: 40.0 + (last / 7.0).sin() * 8.0 }, m); + let (next, key) = display_doc(&mut app, 0); + assert_eq!(key, 0, "the frame after release shows the committed document"); + ensure_texture(&mut app, &ctx, 0, None); + let partial = app.perf.last_refresh == "rect"; + (live, next, partial, app) +} + +fn same_pixels(a: &Document, b: &Document) -> bool { + let (a, b) = (a.layers[0].surface().unwrap(), b.layers[0].surface().unwrap()); + (0..80).all(|y| (0..200).all(|x| a.rgba(x, y) == b.rgba(x, y))) +} + +#[test] +fn release_shows_nothing_new_without_smoothing() { + // #73: at 0 % the stroke follows the pointer; the last frame drawn while dragging is + // exactly the committed stroke, so release changes nothing on screen. + let (live, done, partial, app) = drag_frames(0.0, &[40.0, 70.0, 100.0, 130.0, 160.0]); + assert!(same_pixels(&live, &done), "preview at the last move = committed stroke"); + assert!(partial, "the handover refreshes only the stroke"); + let p = app.session.journal.iter().rev().find(|(id, _)| id == "paint.stroke").map(|(_, p)| p.clone()).unwrap(); + assert!(p.get("smoothing").is_none(), "the commit uses the session brush's smoothing, not a hard-coded one"); +} + +#[test] +fn navigator_keeps_its_image_during_a_live_brush_stroke() { + // #1631: every step of a live stroke is a new preview key, so a navigator keyed by it + // re-composited the whole document per frame while painting. It waits for the release. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 80, "background": "transparent"})).unwrap(); + app.ui.tool = Tool::Brush; + let m = egui::Modifiers::NONE; + assert!(!navigator_waits_for_drag(&app, 0)); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 40.0, pressure: 1.0 }, m); + let mut keys = Vec::new(); + for x in [40.0, 70.0, 100.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 40.0, pressure: 1.0 }, m); + keys.push(display_doc(&mut app, 0).1); + assert!(navigator_waits_for_drag(&app, 0), "painting at x = {x}"); + } + assert!(keys.windows(2).all(|k| k[0] != k[1]), "each step is a new preview: {keys:?}"); + tool_event(&mut app, ToolEvent::Up { x: 100.0, y: 40.0 }, m); + assert!(!navigator_waits_for_drag(&app, 0), "the navigator catches up on release"); +} + +#[test] +fn smoothed_stroke_shows_its_catch_up_tail_while_drawing() { + // #73: with smoothing the brush lags behind the pointer and catches up at the end; the + // preview draws that tail live, so no frame after release is missing the end. + let xs = [30.0, 50.0, 70.0, 90.0, 110.0, 130.0, 150.0, 170.0]; + let (live, done, partial, _) = drag_frames(0.5, &xs); + assert!(same_pixels(&live, &done), "preview at the last move = committed stroke, tail included"); + assert!(partial); + let end = |d: &Document| (0..200).rev().find(|&x| (0..80).any(|y| d.layers[0].surface().unwrap().rgba(x, y)[3] > 0.0)).unwrap(); + assert!(end(&live) >= 170, "the end reaches the pointer while drawing ({})", end(&live)); + // The options-bar value reaches the stroke: 50 % smooths the wiggle, 0 % doesn't. + let (_, rough, _, _) = drag_frames(0.0, &xs); + assert!(!same_pixels(&rough, &done), "smoothing changes the stroke"); + // And the commit is what `paint.stroke` gives with that smoothing. + let mut s = photocraft_engine::Session::new(); + s.execute("file.new", json!({"width": 200, "height": 80, "background": "transparent"})).unwrap(); + s.execute("tools.setBrush", json!({"brush": {"size": 16, "hardness": 0.5, "smoothing": {"amount": 0.5}}})).unwrap(); + let mut pts = vec![json!([10.0, 40.0, 1.0])]; + pts.extend(xs.iter().map(|&x| json!([x, 40.0 + (x / 7.0).sin() * 8.0, 1.0]))); + // (Round tips without dynamics draw the same for any seed.) + s.execute("paint.stroke", json!({"points": pts, "seed": 0})).unwrap(); + assert!(same_pixels(&s.active().unwrap().doc, &done)); +} + +#[test] +fn smoothing_preview_redraws_the_tail_each_step() { + // The tail drawn at one step must not linger once the brush moves on: a sharp turn would + // leave a stale tail behind if it weren't restored. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 120, "background": "transparent"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 10, "hardness": 1.0, "smoothing": {"amount": 0.6}}})).unwrap(); + app.ui.tool = Tool::Brush; + let m = egui::Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 20.0, pressure: 1.0 }, m); + for (x, y) in [(60.0, 20.0), (110.0, 20.0), (110.0, 70.0), (110.0, 110.0), (60.0, 110.0)] { + tool_event(&mut app, ToolEvent::Move { x, y, pressure: 1.0 }, m); + } + let live = display_doc(&mut app, 0).0; + tool_event(&mut app, ToolEvent::Up { x: 60.0, y: 110.0 }, m); + let done = app.session.documents()[0].doc.clone(); + let (a, b) = (live.layers[0].surface().unwrap(), done.layers[0].surface().unwrap()); + assert!((0..120).all(|y| (0..200).all(|x| a.rgba(x, y) == b.rgba(x, y))), "no stale tails in the preview"); +} + +/// Shapes the drag preview paints for `app` this frame. +fn drag_preview_shapes(app: &mut PhotocraftApp, ctx: &egui::Context, zoom: f32) -> Vec { + let out = ctx.run_ui(egui::RawInput::default(), |ui| { + let ctx = ui.ctx(); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let painter = ctx.layer_painter(egui::LayerId::background()).with_clip_rect(rect); + let xf = ViewXform { rect, zoom, center: [60.0, 30.0], flip: false, rotation: 0.0, aspect: 1.0 }; + draw_drag_preview(app, &painter, &xf); + }); + let egui::FullOutput { mut textures_delta, shapes, .. } = out; + textures_delta.clear(); + shapes +} + +#[test] +fn a_huge_view_centre_draws_the_selection_outline_without_overflow() { + // #980: `ui.set` takes any finite centre, and the outline's visible region was quantised + // with unchecked i32 arithmetic: the frame panicked ("attempt to multiply/subtract with + // overflow") and release builds wrapped to an inverted region. + let ctx = egui::Context::default(); + // Zoomed far out the quantisation step, and so the padding, is largest (64 · 256 px). + let cases = [([1e30, 1e30], false), ([-1e30, -1e30], false), ([3e9, -3e9], false), ([512.0, 512.0], true)]; + for ((centre, traced), zoom) in cases.into_iter().flat_map(|c| [(c, 1.0), (c, 1.0 / 64.0)]) { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 1024, "height": 1024})).unwrap(); + app.run("select.rect", json!({"x": 0, "y": 0, "width": 512, "height": 512})).unwrap(); + app.sync_views(); + let (req, _rx) = crate::control::ControlRequest::new("ui.set", json!({ "center": centre, "zoom": zoom })); + let _ = crate::control::handle(&mut app, &ctx, &req); + let view = app.ui.views[0].clone(); + assert_eq!(view.zoom, zoom, "ui.set applied the zoom"); + let mut out = ctx.run_ui(egui::RawInput::default(), |ui| { + canvas_view(&mut app, ui, 0, Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), view.clone(), false); + }); + out.textures_delta.clear(); + // Control: a centre on the selection's corner still traces the outline (the selection + // spans several 64 px trace cells even zoomed out). + let segs = app.outline_cache.as_ref().map_or(0, |(_, _, s)| s.len()); + assert_eq!(segs > 0, traced, "{centre:?} at zoom {zoom}"); + } +} + +#[test] +fn a_one_pixel_selection_traces_at_hundred_percent_on_a_scaled_display() { + // #1943 review: the outline step is display resolution in device pixels, not points, so a + // 1 px selection still traces at 100 % on a 2x display (a points-based step sampled every + // second pixel and lost it: zero segments). + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("select.rect", json!({"x": 50, "y": 50, "width": 1, "height": 100})).unwrap(); + app.run("view.actualPixels", json!({})).unwrap(); + app.sync_views(); + let view = app.ui.views[0].clone(); + let mut input = egui::RawInput::default(); + input.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(2.0); + let mut out = ctx.run_ui(input, |ui| { + canvas_view(&mut app, ui, 0, Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), view.clone(), true); + }); + out.textures_delta.clear(); + assert_eq!(app.ppp, 2.0, "the primary canvas records the display scale"); + let segs = app.outline_cache.as_ref().map_or(0, |(_, _, s)| s.len()); + assert!(segs > 0, "a 1 px selection traces at 100 % on a 2x display"); +} + +#[test] +fn a_secondary_canvas_does_not_overwrite_the_main_canvas_scale() { + // #1943 review: a document window can sit on a display with another scale; only the + // primary canvas records `app.ppp`, so the main mapping doesn't change under it. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("view.actualPixels", json!({})).unwrap(); + app.sync_views(); + let view = app.ui.views[0].clone(); + let frame = |app: &mut PhotocraftApp, ppp: f32, primary: bool| { + let mut input = egui::RawInput::default(); + input.viewports.entry(egui::ViewportId::ROOT).or_default().native_pixels_per_point = Some(ppp); + let mut out = ctx.run_ui(input, |ui| { + canvas_view(app, ui, 0, Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), view.clone(), primary); + }); + out.textures_delta.clear(); + }; + frame(&mut app, 2.0, true); + assert_eq!(app.ppp, 2.0); + frame(&mut app, 1.0, false); + assert_eq!(app.ppp, 2.0, "a secondary window's scale doesn't replace the main canvas's"); + // The main canvas mapping still uses its own scale: 100 % is 1 doc px per physical px. + let xf = ViewXform::active(&app).unwrap(); + let step = xf.to_screen(1.0, 0.0) - xf.to_screen(0.0, 0.0); + assert!((step.x - 0.5).abs() < 1e-3, "{step:?}"); +} + +#[test] +fn a_flicked_hand_drag_glides_on_only_with_flick_panning() { + // #204: Preferences › Tools › Enable Flick Panning. A fast Hand drag let go while moving + // keeps the view gliding for a while; off, the view stops where the drag ended. + for flick in [true, false] { + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 2000, "height": 1500})).unwrap(); + app.run("prefs.set", json!({"path": "tools.enableFlickPanning", "value": flick})).unwrap(); + app.ui.tool = Tool::Hand; + app.sync_views(); + let mut view = app.ui.views[0].clone(); + view.zoom = 1.0; + view.center = [1000.0, 750.0]; + view.fit_pending = false; + view.fill_pending = false; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let mut frame = |app: &mut PhotocraftApp, events: Vec| -> View { + let input = egui::RawInput { screen_rect: Some(rect), events, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + view = canvas_view(app, ui, 0, rect, view.clone(), true); + }); + out.textures_delta.clear(); + view.clone() + }; + let start = Pos2::new(200.0, 150.0); + let press = + |pos: Pos2, pressed: bool| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + frame(&mut app, vec![egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true)]); + let mut end = start; + for i in 1..=8 { + end = start + vec2(-20.0 * i as f32, 0.0); + frame(&mut app, vec![egui::Event::PointerMoved(end)]); + } + let released = frame(&mut app, vec![press(end, false)]).center; + let mut centers = vec![released]; + for _ in 0..5 { + centers.push(frame(&mut app, vec![]).center); + } + if flick { + // Dragged left: the image follows the pointer, the view keeps moving right. + for w in centers.windows(2) { + assert!(w[1][0] > w[0][0], "glides on: {centers:?}"); + } + // ...and comes to rest. + let mut last = *centers.last().unwrap(); + for _ in 0..120 { + last = frame(&mut app, vec![]).center; + } + assert_eq!(frame(&mut app, vec![]).center, last, "the glide stops"); + } else { + assert!(centers.iter().all(|c| *c == released), "no glide when off: {centers:?}"); + } + } +} + +#[test] +fn a_secondary_canvas_hand_pans_while_a_transform_is_open() { + // #2302 review: `app.ui.transform` is global, but only the primary canvas draws and drives + // the box. A secondary window's Hand must still pan while the main window transforms. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.run("view.actualPixels", json!({})).unwrap(); + app.ui.tool = Tool::Hand; + app.sync_views(); + crate::transform_tool::begin(&mut app, &ctx).expect("a transform starts"); + let mut view0 = app.ui.views[0].clone(); + view0.zoom = 1.0; + view0.center = [200.0, 150.0]; + view0.fit_pending = false; + view0.fill_pending = false; + let center0 = view0.center; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let frame = |app: &mut PhotocraftApp, events: Vec| -> View { + let input = egui::RawInput { screen_rect: Some(rect), events, ..Default::default() }; + let mut seen = view0.clone(); + let mut out = ctx.run_ui(input, |ui| { + seen = canvas_view(app, ui, 0, rect, view0.clone(), false); + }); + out.textures_delta.clear(); + seen + }; + let start = Pos2::new(200.0, 150.0); + let press = |pos: Pos2, pressed: bool| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }; + frame(&mut app, vec![egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true)]); + for dx in [10.0, 20.0, 40.0] { + frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(dx, 0.0))]); + } + let dragged = frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(60.0, 0.0))]); + frame(&mut app, vec![press(start + vec2(60.0, 0.0), false)]); + assert_ne!(dragged.center, center0, "a secondary window's Hand pans while transforming"); +} + +#[test] +fn letting_go_of_alt_during_an_alt_sample_press_does_not_paint() { + // #7802: Alt-pressing with the Brush samples a colour on the press. If Alt was let go + // before egui recognised the drag, that drag started a stroke from the sampled point. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("view.actualPixels", json!({})).unwrap(); + app.ui.tool = Tool::Brush; + app.sync_views(); + let mut view = app.ui.views[0].clone(); + view.zoom = 1.0; + view.center = [200.0, 150.0]; + view.fit_pending = false; + view.fill_pending = false; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let frame = |app: &mut PhotocraftApp, events: Vec| { + let input = egui::RawInput { screen_rect: Some(rect), events, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + canvas_view(app, ui, 0, rect, view.clone(), true); + }); + out.textures_delta.clear(); + }; + let alt = egui::Modifiers::ALT; + let none = egui::Modifiers::NONE; + let start = Pos2::new(200.0, 150.0); + let press = + |pos: Pos2, pressed: bool, modifiers: egui::Modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }; + // Alt released mid-press, and Alt held throughout: both only sample. + for release_alt in [true, false] { + app.run("tools.setColors", json!({"foreground": [0.0, 0.0, 1.0, 1.0]})).unwrap(); + let rev = app.session.active().unwrap().revision; + frame(&mut app, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true, alt)]); + assert_eq!(app.session.tools.foreground, [1.0, 1.0, 1.0, 1.0], "the press sampled the white canvas"); + let later = if release_alt { none } else { alt }; + let end = start + vec2(20.0, 0.0); + frame(&mut app, vec![egui::Event::ModifiersChanged(later), egui::Event::PointerMoved(end)]); + frame(&mut app, vec![press(end, false, later)]); + frame(&mut app, vec![egui::Event::ModifiersChanged(none)]); + assert_eq!(app.session.active().unwrap().revision, rev, "release_alt={release_alt}: an Alt-sample press must not paint"); + assert!(app.drag.is_none() && !app.alt_sampling, "release_alt={release_alt}"); + } + // Without Alt the same gesture paints. + let rev = app.session.active().unwrap().revision; + frame(&mut app, vec![egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true, none)]); + frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(20.0, 0.0))]); + frame(&mut app, vec![press(start + vec2(20.0, 0.0), false, none)]); + assert!(app.session.active().unwrap().revision > rev, "a plain drag paints"); +} + +#[test] +fn letting_go_of_alt_during_a_mixer_brush_sample_press_does_not_paint() { + // As #7802 for the Brush: an Alt-press with the Mixer Brush loads paint from the canvas. + // If Alt was let go before egui recognised the drag, that drag started a Mixer stroke. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("view.actualPixels", json!({})).unwrap(); + app.ui.tool = Tool::MixerBrush; + app.sync_views(); + let mut view = app.ui.views[0].clone(); + view.zoom = 1.0; + view.center = [200.0, 150.0]; + view.fit_pending = false; + view.fill_pending = false; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let frame = |app: &mut PhotocraftApp, events: Vec| { + let input = egui::RawInput { screen_rect: Some(rect), events, ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + canvas_view(app, ui, 0, rect, view.clone(), true); + }); + out.textures_delta.clear(); + }; + let (alt, none) = (egui::Modifiers::ALT, egui::Modifiers::NONE); + let start = Pos2::new(200.0, 150.0); + let press = + |pos: Pos2, pressed: bool, modifiers: egui::Modifiers| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers }; + // Alt released mid-press, and Alt held throughout: both only load paint. + for release_alt in [true, false] { + let rev = app.session.active().unwrap().revision; + frame(&mut app, vec![egui::Event::ModifiersChanged(alt), egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true, alt)]); + let later = if release_alt { none } else { alt }; + let end = start + vec2(20.0, 0.0); + frame(&mut app, vec![egui::Event::ModifiersChanged(later), egui::Event::PointerMoved(end)]); + frame(&mut app, vec![press(end, false, later)]); + frame(&mut app, vec![egui::Event::ModifiersChanged(none)]); + assert_eq!(app.session.active().unwrap().revision, rev, "release_alt={release_alt}: an Alt-sample press must not paint"); + assert!(app.drag.is_none() && !app.mixer_sampling, "release_alt={release_alt}"); + } + // Without Alt the same gesture paints. + let rev = app.session.active().unwrap().revision; + frame(&mut app, vec![egui::Event::PointerMoved(start)]); + frame(&mut app, vec![press(start, true, none)]); + frame(&mut app, vec![egui::Event::PointerMoved(start + vec2(20.0, 0.0))]); + frame(&mut app, vec![press(start + vec2(20.0, 0.0), false, none)]); + assert!(app.session.active().unwrap().revision > rev, "a plain drag paints"); +} + +#[test] +fn guides_draw_over_the_transform_preview_and_under_its_box() { + // #5086: a guide crossing a layer under Free Transform was hidden behind the preview of + // the moving pixels. The preview is image content, drawn before the grid and guides like + // the document is; the box and handles still go over them, as in Photoshop. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 150})).unwrap(); + app.ui.extras.guides = true; + app.ui.extras.rulers = false; + app.sync_views(); + crate::transform_tool::begin(&mut app, &ctx).expect("a transform starts"); + let mut view = app.ui.views[0].clone(); + view.zoom = 1.0; + view.center = [200.0, 150.0]; + view.fit_pending = false; + view.fill_pending = false; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + canvas_view(&mut app, ui, 0, rect, view.clone(), true); + }); + out.textures_delta.clear(); + // At 100 % centred on the document, screen and document coordinates coincide. + let layer = Rect::from_min_max(pos2(100.0, 100.0), pos2(200.0, 160.0)); + let near = |r: Rect| (r.min - layer.min).length() < 1.0 && (r.max - layer.max).length() < 1.0; + let position = |pred: &dyn Fn(&egui::Shape) -> bool| out.shapes.iter().position(|s| pred(&s.shape)); + // The moving pixels: the mesh that spans the layer's box. + let preview = position(&|s| matches!(s, egui::Shape::Mesh(m) if near(m.calc_bounds()))); + // The guide: a vertical line at x = 150. + let guide = position(&|s| matches!(s, egui::Shape::LineSegment { points, .. } if points.iter().all(|p| (p.x - 150.0).abs() < 0.01))); + // The box: the closed four-point outline round the layer. + let frame = position(&|s| matches!(s, egui::Shape::Path(p) if p.closed && p.points.len() == 4 && near(Rect::from_points(&p.points)))); + let (Some(preview), Some(guide), Some(frame)) = (preview, guide, frame) else { + panic!("all painted: preview {preview:?}, guide {guide:?}, box {frame:?}"); + }; + assert!(preview < guide, "the guide ({guide}) draws over the transform preview ({preview})"); + assert!(guide < frame, "the box ({frame}) draws over the guide ({guide})"); +} + +#[test] +fn guides_draw_over_the_puppet_perspective_and_vanishing_point_previews() { + // #7781: like Free Transform's (#5086), the warped pixels of Puppet Warp and Perspective + // Warp and Vanishing Point's paste are image content. They were drawn with their pins and + // planes, after the guides, hiding every guide across them; the guides now go over them, + // the mesh lines, pins and planes over the guides, as in Photoshop. + for command in ["edit.puppetWarp", "edit.perspectiveWarp", photocraft_engine::vp_cmds::VP] { + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + if command == photocraft_engine::vp_cmds::VP { + app.run("edit.copy", json!({})).unwrap(); + } + app.run("select.deselect", json!({})).unwrap(); + app.run("view.newGuide", json!({"orientation": "vertical", "position": 150})).unwrap(); + app.ui.extras.guides = true; + app.ui.extras.rulers = false; + app.sync_views(); + crate::distort_ui::menu(&mut app, &ctx, command, &json!({})).expect("an on-canvas mode").expect("it starts"); + if command == photocraft_engine::vp_cmds::VP { + // A plane over the document; the clipboard pastes onto it. + for (x, y) in [(20.0, 280.0), (380.0, 280.0), (300.0, 40.0), (100.0, 40.0)] { + crate::distort_ui::pointer(&mut app, ToolEvent::Down { x, y, pressure: 1.0 }, egui::Modifiers::NONE); + crate::distort_ui::pointer(&mut app, ToolEvent::Up { x, y }, egui::Modifiers::NONE); + } + } else if command == "edit.perspectiveWarp" { + // One plane, dragged out across the guide. + crate::distort_ui::pointer(&mut app, ToolEvent::Down { x: 60.0, y: 70.0, pressure: 1.0 }, egui::Modifiers::NONE); + crate::distort_ui::pointer(&mut app, ToolEvent::Move { x: 240.0, y: 190.0, pressure: 1.0 }, egui::Modifiers::NONE); + crate::distort_ui::pointer(&mut app, ToolEvent::Up { x: 240.0, y: 190.0 }, egui::Modifiers::NONE); + assert_eq!(app.distort.perspective.as_ref().map(|s| s.planes.len()), Some(1), "the drag makes a plane"); + } + let mut view = app.ui.views[0].clone(); + view.zoom = 1.0; + view.center = [200.0, 150.0]; + view.fit_pending = false; + view.fill_pending = false; + let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)); + let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() }; + let mut out = ctx.run_ui(input, |ui| { + canvas_view(&mut app, ui, 0, rect, view.clone(), true); + }); + out.textures_delta.clear(); + // At 100 % centred on the document, screen and document coordinates coincide. + let layer = Rect::from_min_max(pos2(100.0, 100.0), pos2(200.0, 160.0)); + // Puppet Warp's mesh reaches its Expansion (2 px) past the pixels. + let near = |r: Rect| (r.min - layer.min).length() < 4.0 && (r.max - layer.max).length() < 4.0; + let shapes: Vec<&egui::Shape> = out.shapes.iter().map(|s| &s.shape).collect(); + // The warped pixels (or the paste): a textured mesh over the layer (or on the plane, + // well inside the document, unlike the checkerboard and document image). + let preview = shapes.iter().position(|s| match s { + egui::Shape::Mesh(m) if m.texture_id != egui::TextureId::default() => { + let r = m.calc_bounds(); + if command == photocraft_engine::vp_cmds::VP { r.width() > 10.0 && r.width() < 300.0 && r.min.x < 150.0 && r.max.x > 150.0 } else { near(r) } + } + _ => false, + }); + // The guide: a vertical line at x = 150 across the whole view. + let is_guide = |s: &egui::Shape| matches!(s, egui::Shape::LineSegment { points, .. } if points.iter().all(|p| (p.x - 150.0).abs() < 0.01) && (points[1].y - points[0].y).abs() > 200.0); + let guide = shapes.iter().position(|s| is_guide(s)); + // Over the guide: Puppet Warp's mesh lines, the Vanishing Point plane's outline, the + // Perspective Warp plane's outline (exactly the dragged rectangle). + let plane = Rect::from_min_max(pos2(60.0, 70.0), pos2(240.0, 190.0)); + let overlay = shapes.iter().position(|s| match s { + egui::Shape::LineSegment { points, .. } => { + command != "edit.perspectiveWarp" && !is_guide(s) && points.iter().all(|p| layer.expand(3.0).contains(*p)) + } + egui::Shape::Path(p) => { + p.closed + && p.points.len() == 4 + && (command != "edit.perspectiveWarp" + || (Rect::from_points(&p.points).min - plane.min).length() + (Rect::from_points(&p.points).max - plane.max).length() < 0.5) + } + _ => false, + }); + let (Some(preview), Some(guide)) = (preview, guide) else { + panic!("{command}: all painted: preview {preview:?}, guide {guide:?}"); + }; + assert!(preview < guide, "{command}: the guide ({guide}) draws over the warped pixels ({preview})"); + let Some(overlay) = overlay else { panic!("{command}: its mesh or plane is drawn") }; + assert!(guide < overlay, "{command}: the mesh or plane ({overlay}) draws over the guide ({guide})"); + } +} + +#[test] +fn brush_drags_paint_no_stand_in_shape_over_the_canvas() { + // #189: v0.1 drew the stroke being dragged as a foreground-coloured egui polyline as wide + // as the brush; zoomed in, egui tessellated it into hard black wedges fanning out from the + // start. The canvas shows the real stroke (`LiveStroke`) and nothing is drawn over it. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 30, "hardness": 0.0}})).unwrap(); + // A smoothed stroke also draws its string line (see + // `smoothed_brush_drag_draws_the_string_from_brush_to_pointer`): that is no stand-in shape. + let overlay = |app: &mut PhotocraftApp, ctx: &egui::Context| { + drag_preview_shapes(app, ctx, 12.0).iter().filter(|s| !matches!(s.shape, egui::Shape::LineSegment { .. } | egui::Shape::Circle(_))).count() + }; + for tool in [Tool::Brush, Tool::Eraser] { + app.ui.tool = tool; + let idle = overlay(&mut app, &ctx); + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + for i in 1..40 { + let t = f64::from(i); + tool_event(&mut app, ToolEvent::Move { x: 20.0 + t, y: 30.0 + (t / 3.0).sin() * 4.0, pressure: 1.0 }, egui::Modifiers::NONE); + } + assert!(app.live_stroke.is_some(), "{tool:?}: the canvas shows the live stroke"); + assert_eq!(overlay(&mut app, &ctx), idle, "{tool:?}: no overlay while dragging"); + tool_event(&mut app, ToolEvent::Up { x: 59.0, y: 30.0 }, egui::Modifiers::NONE); + } +} + +#[test] +fn smoothed_brush_drag_draws_the_string_from_brush_to_pointer() { + // #3479: with smoothing the brush lags behind the pointer; a line joins them while drawing. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60})).unwrap(); + app.ui.tool = Tool::Brush; + let lines = |app: &mut PhotocraftApp| drag_preview_shapes(app, &ctx, 4.0).iter().filter(|s| matches!(s.shape, egui::Shape::LineSegment { .. })).count(); + for (amount, string) in [(0.0, false), (0.8, true)] { + app.run("tools.setBrush", json!({"brush": {"size": 10, "smoothing": {"amount": amount}}})).unwrap(); + tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + for x in [30.0, 60.0, 90.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + } + assert_eq!(lines(&mut app) > 0, string, "smoothing {amount}"); + tool_event(&mut app, ToolEvent::Up { x: 90.0, y: 30.0 }, egui::Modifiers::NONE); + } +} + +#[test] +fn retouch_drags_show_the_footprint_without_wedges() { + // The retouching tools' trail was the same brush-wide polyline (#189): now a mask of the + // footprint, with nothing outside it, however small the steps. The Remove Tool's is where + // the stroke's dabs reach at all, which is what it removes, so a soft tip's whole reach + // past the radius is tinted. + for hardness in [1.0, 0.0] { + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"size": 30, "hardness": hardness}})).unwrap(); + app.ui.tool = Tool::Remove; + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, egui::Modifiers::NONE); + for i in 1..80 { + let t = f64::from(i) * 0.5; + tool_event(&mut app, ToolEvent::Move { x: 20.0 + t, y: 30.0 + (t / 2.0).sin() * 3.0, pressure: 1.0 }, egui::Modifiers::NONE); + if i % 20 == 0 { + // One mesh for the trail, however long the drag. + let shapes = drag_preview_shapes(&mut app, &ctx, 12.0); + assert!(!shapes.iter().any(|s| matches!(s.shape, egui::Shape::Path(_))), "no polyline"); + } + } + drag_preview_shapes(&mut app, &ctx, 12.0); + // The stroke the removal gets on release, and the pixels its hole takes. + let points = app.drag.as_ref().unwrap().points.iter().map(|p| photocraft_paint::StrokePoint::new(p[0], p[1], p[2] as f32)).collect(); + let brush = photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, &json!({})); + let (b, cov) = photocraft_paint::retouch::stroke_coverage(&photocraft_paint::Stroke { brush, points }); + let trail = app.trail.as_ref().unwrap(); + assert_eq!(trail.scale(), 1.0); + let mut beyond = 0; + for y in 0..60 { + for x in 0..120 { + let removed = b.contains(x, y) && cov[(y - b.y0) as usize * b.width() as usize + (x - b.x0) as usize] > 0.0; + assert_eq!(trail.coverage(x as usize, y as usize), if removed { 255 } else { 0 }, "hardness {hardness}: ({x}, {y})"); + // Past the 15 px radius of the path's ends and sides (y 27..33). + beyond += usize::from(removed && !(12..=48).contains(&y)); + } + } + if hardness == 0.0 { + assert!(beyond > 100, "a soft tip's removal reaches past its radius: {beyond} px"); + } + tool_event(&mut app, ToolEvent::Up { x: 59.5, y: 30.0 }, egui::Modifiers::NONE); + drag_preview_shapes(&mut app, &ctx, 12.0); + assert!(app.trail.is_none(), "the trail ends with the drag"); + } +} + +#[test] +fn spot_healing_trail_is_the_coverage_the_heal_uses() { + // #2364: a soft brush with pen-pressure size showed as a hard footprint at full size. + let ctx = egui::Context::default(); + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 120, "height": 60})).unwrap(); + let brush = json!({"size": 30, "hardness": 0.0, "shapeDynamics": {"enabled": true, "size": {"control": "penPressure"}}}); + app.run("tools.setBrush", json!({ "brush": brush })).unwrap(); + app.ui.tool = Tool::SpotHealing; + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 0.5 }, egui::Modifiers::NONE); + for x in [40.0, 60.0, 80.0] { + tool_event(&mut app, ToolEvent::Move { x, y: 30.0, pressure: 0.5 }, egui::Modifiers::NONE); + drag_preview_shapes(&mut app, &ctx, 12.0); + } + // The stroke the heal gets on release, and its coverage. + let points = app.drag.as_ref().unwrap().points.iter().map(|p| photocraft_paint::StrokePoint::new(p[0], p[1], p[2] as f32)).collect(); + let brush = photocraft_engine::retouch_cmds::stroke_brush(&app.session.tools.brush, &json!({})); + let (b, cov) = photocraft_paint::retouch::stroke_coverage(&photocraft_paint::Stroke { brush, points }); + let trail = app.trail.as_ref().unwrap(); + let mut soft = 0; + for y in 0..60 { + for x in 0..120 { + let want = if b.contains(x, y) { cov[(y - b.y0) as usize * b.width() as usize + (x - b.x0) as usize] } else { 0.0 }; + let got = trail.coverage(x as usize, y as usize); + assert!((f32::from(got) - want * 255.0).abs() <= 1.0, "({x}, {y}): trail {got}, heal {want}"); + soft += usize::from(got > 0 && got < 200); + } + } + assert!(soft > 50, "a soft brush shows a soft edge"); + // Half pressure halves the 30 px tip: nothing past its (soft) reach. + let reach = photocraft_paint::tip_reach(7.5, app.session.tools.brush.hardness).ceil() as usize + 2; + assert_eq!(trail.coverage(50, 30 + reach), 0, "half pressure halves the 30 px tip"); +} + +#[test] +fn layer_style_dialog_previews_live_and_cancel_restores() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 16, "height": 16})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + let fx = |d: &Document| d.layers.iter().map(|l| l.effects.items.len()).sum::(); + let id = crate::layer_style::open(&mut app, Some("colorOverlay")).unwrap(); + let (shown, key) = display_doc(&mut app, 0); + assert_eq!((fx(&shown), fx(&app.session.documents()[0].doc)), (1, 0), "previewed, not committed"); + // Adding a stroke through the dialog's instance list re-renders the canvas. + if let Some(d) = app.ui.dialog_mut(id) { + d.fields["effects"].as_array_mut().unwrap().push(json!({"id": "fx9", "kind": "stroke", "on": true, "params": {"size": 3}})); + } + let (shown, key2) = display_doc(&mut app, 0); + assert_eq!(fx(&shown), 2); + assert_ne!(key, key2, "an edit re-renders the canvas"); + app.ui.close_dialog(id); + let (shown, key) = display_doc(&mut app, 0); + assert_eq!((fx(&shown), key), (0, 0)); + assert!(app.style_preview.is_none()); +} + +/// Layer Style opened while Free Transform is open applies the box first, as Photoshop does, +/// so the dialog previews on the transformed layer (#4821): a dropped file (placed in the box, +/// untouched) and a box opened by hand and moved. +#[test] +fn layer_style_opened_during_free_transform_applies_the_box_and_previews() { + for placed in [true, false] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 8, "y": 8, "width": 16, "height": 16})).unwrap(); + app.run("edit.fill", json!({"color": "#0000ff"})).unwrap(); + app.run("select.deselect", json!({})).unwrap(); + app.sync_views(); + let ctx = egui::Context::default(); + let dx = if placed { + crate::transform_tool::begin_placed(&mut app, &ctx).unwrap(); + 0 + } else { + crate::transform_tool::begin(&mut app, &ctx).unwrap(); + app.ui.transform.as_mut().unwrap().quad = [[28.0, 8.0], [44.0, 8.0], [44.0, 24.0], [28.0, 24.0]]; + 20 + }; + crate::layer_style::open(&mut app, Some("dropShadow")).unwrap(); + assert!(app.ui.transform.is_none() && app.transform_preview.is_none(), "placed {placed}: the box is applied"); + let (shown, _) = display_doc(&mut app, 0); + let rect = photocraft_geom::Rect::new(0, 0, 64, 64); + let with = photocraft_compose::render(&shown, rect); + let without = photocraft_compose::render(&app.session.documents()[0].doc, rect); + assert!( + with.get(16 + dx, 16)[0] < 0.1 && with.get(16 + dx, 16)[2] > 0.9, + "placed {placed}: the blue block where the box left it, not the white under it" + ); + // The shadow darkens the white around the block, which the document doesn't have yet. + let block = |x: i32, y: i32| (8 + dx..24 + dx).contains(&x) && (8..24).contains(&y); + let shadow = + (0..64).flat_map(|y| (0..64).map(move |x| (x, y))).filter(|&(x, y)| !block(x, y) && without.get(x, y)[0] - with.get(x, y)[0] > 0.05).count(); + assert!(shadow > 20, "placed {placed}: the Drop Shadow is previewed ({shadow} px)"); + } +} + +#[test] +fn fill_view_covers_the_canvas_and_centres_the_document() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 200})).unwrap(); + let doc = app.session.active().unwrap().doc.clone(); + let mut view = View { fill_pending: true, ..View::default() }; + + fill_view(&mut view, &doc, vec2(600.0, 600.0), 1.0, 1.0); + + assert_eq!(view.zoom, 3.0, "the short edge fills the available height"); + assert_eq!(view.center, [200.0, 100.0]); + assert!(!view.fill_pending); + + // A scaled display stores the physical factor: the same point zoom is 2× the device px. + let mut view = View { fill_pending: true, ..View::default() }; + fill_view(&mut view, &doc, vec2(600.0, 600.0), 2.0, 1.0); + assert_eq!(view.zoom, 6.0); + + // Fit never magnifies past 100% = one image pixel per physical pixel, at any display + // scale (it used to cap at one per *point*, so a 150% display opened images at 150%). + for ppp in [1.0f32, 1.5, 2.0] { + let mut view = View { fit_pending: true, ..View::default() }; + fit_view(&mut view, &doc, vec2(600.0, 600.0), ppp, 1.0); + assert_eq!(view.zoom, 1.0, "ppp {ppp}: a small image opens at 100%"); + } + // A large image still shrinks to fit: 1000×500 into 440 points is 0.44 pt/px, so 0.66 + // physical at ppp 1.5. + let big = { + app.run("file.new", json!({"width": 1000, "height": 500})).unwrap(); + app.session.active().unwrap().doc.clone() + }; + let mut view = View { fit_pending: true, ..View::default() }; + fit_view(&mut view, &big, vec2(480.0, 480.0), 1.5, 1.0); + assert!((view.zoom - 0.66).abs() < 1e-5, "{}", view.zoom); +} + +#[test] +fn a_hundred_percent_is_one_physical_pixel_per_document_pixel() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 200})).unwrap(); + app.sync_views(); + for ppp in [1.0f32, 1.25, 2.0] { + app.ppp = ppp; + let xf = ViewXform::active(&app).unwrap(); + // 100% zoom: one document pixel is one physical pixel, i.e. 1/ppp egui points. + let step = xf.to_screen(1.0, 0.0) - xf.to_screen(0.0, 0.0); + // `to_screen` differences around the view centre cancel in f32; 1e-3 is still exact + // enough to see a wrong ppp factor (which would be a 20–100% error). + assert!((step.x - 1.0 / ppp).abs() < 1e-3, "ppp {ppp}: {step:?} want {:?}", 1.0 / ppp); + assert!((app.point_zoom() - 1.0 / ppp).abs() < 1e-5, "ppp {ppp}: {}", app.point_zoom()); + // Screen ↔ document stays an exact round trip. + let back = xf.to_doc(xf.to_screen(123.0, 45.0)); + assert!((back[0] - 123.0).abs() < 1e-3 && (back[1] - 45.0).abs() < 1e-3, "ppp {ppp}: {back:?}"); + } +} + +#[test] +fn wheel_anchor_is_the_pointer_over_the_image_and_the_nearest_image_point_off_it() { + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + for (rotation, flip) in [(0.0, false), (30.0, false), (90.0, true)] { + let xf = ViewXform { rect, zoom: 0.5, center: [400.0, 300.0], flip, rotation, aspect: 1.0 }; + let inside = xf.to_screen(100.0, 200.0); + assert_eq!(wheel_anchor(&xf, inside, [800, 600]), inside); + for (p, want) in [((-300.0, -200.0), [0.0, 0.0]), ((1200.0, 300.0), [800.0, 300.0]), ((400.0, 900.0), [400.0, 600.0])] { + let a = xf.to_doc(wheel_anchor(&xf, xf.to_screen(p.0, p.1), [800, 600])); + assert!((a[0] - want[0]).abs() < 0.01 && (a[1] - want[1]).abs() < 0.01, "rot {rotation}: {p:?} -> {a:?}"); + } + } + let p = pos2(5.0, 5.0); + let xf = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip: false, rotation: 0.0, aspect: 1.0 }; + assert_eq!(wheel_anchor(&xf, p, [0, 0]), p, "no document: the pointer"); +} + +#[test] +fn zoom_about_centres_the_clicked_point_with_the_preference() { + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let p = pos2(600.0, 200.0); + for ppp in [1.0f32, 2.0] { + // `View::zoom` is physical; the transform works in points. + let xf = ViewXform { rect, zoom: 1.0 / ppp, center: [400.0, 300.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let doc = xf.to_doc(p); + // Off: the document point under the pointer stays under it. + let mut view = View { zoom: 1.0, center: [400.0, 300.0], ..Default::default() }; + zoom_about(&mut view, &xf, p, 2.0, false, ppp); + let after = ViewXform { rect, zoom: 2.0 / ppp, center: view.center, flip: false, rotation: 0.0, aspect: 1.0 }; + let s = after.to_screen(doc[0] as f32, doc[1] as f32); + assert!((s - p).length() < 0.5, "ppp {ppp}: {s:?} vs {p:?}"); + // On: the clicked point is the view centre. + let mut view = View { zoom: 1.0, center: [400.0, 300.0], ..Default::default() }; + zoom_about(&mut view, &xf, p, 2.0, true, ppp); + assert!((view.center[0] - doc[0] as f32).abs() < 0.5, "ppp {ppp}: {:?}", view.center); + assert!((view.center[1] - doc[1] as f32).abs() < 0.5, "ppp {ppp}: {:?}", view.center); + } +} + +#[test] +fn damage_grows_by_nested_effect_reach() { + use photocraft_doc::{Effect, Layer}; + let fmt = photocraft_color::PixelFormat::RGBA8; + assert_eq!(effect_reach(&[Layer::raster("plain", fmt)]), 0); + let mut inner = Layer::raster("inner", fmt); + inner.effects.items.push(Effect::default_drop_shadow()); + let m = photocraft_compose::effects::margin(&inner); + let mut group = Layer::group("g", vec![inner.clone()]); + group.effects.items.push(Effect::default_drop_shadow()); + assert_eq!(effect_reach(std::slice::from_ref(&inner)), m); + // A child's edit moves the group's shape, whose effects reach further. + assert_eq!(effect_reach(&[group.clone()]), 2 * m); + group.visible = false; + assert_eq!(effect_reach(&[group]), 0); +} + +#[test] +fn tab_reorder_keeps_floating_documents_in_place() { + let visible = [0, 2]; + assert_eq!(move_target_after_filter(&visible, 0, 1, 3), 2); + assert_eq!(move_target_after_filter(&visible, 2, 0, 3), 0); +} + +#[test] +fn arrange_layout_only_includes_docked_documents() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + for name in ["first", "floating", "last"] { + app.run("file.new", json!({"width": 80, "height": 60, "name": name})).unwrap(); + } + app.ui.view.arrange = "threeUpStacked".into(); + crate::menus::invoke(&mut app, &egui::Context::default(), "window.arrange.floatInWindow", json!({"document": 1})).unwrap(); + let rect = Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0)); + let shown: Vec<_> = arranged_cells(&app, rect).unwrap().into_iter().map(|(doc, _)| doc).collect(); + assert_eq!(shown, vec![0, 2]); + + crate::menus::invoke(&mut app, &egui::Context::default(), "window.arrange.floatInWindow", json!({"document": 0})).unwrap(); + crate::menus::invoke(&mut app, &egui::Context::default(), "window.arrange.floatInWindow", json!({"document": 2})).unwrap(); + assert!(arranged_cells(&app, rect).is_none(), "the main canvas should be blank when every document is floating"); +} + +#[test] +fn torn_off_document_is_not_drawn_again_in_the_main_window() { + for kind in [crate::theme::ThemeKind::Pro, crate::theme::ThemeKind::Studio] { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 80, "height": 60, "name": "floating"})).unwrap(); + app.run("file.new", json!({"width": 80, "height": 60, "name": "docked"})).unwrap(); + app.sync_views(); + let ctx = egui::Context::default(); + crate::menus::invoke(&mut app, &ctx, "window.arrange.floatInWindow", json!({"document": 0})).unwrap(); + assert!(app.ui.windows[0].floating); + assert_eq!(app.session.active_index(), Some(0), "{kind:?}: the floated page remains active"); + assert_eq!(main_canvas_document(&app), Some(1), "{kind:?}: the main canvas picks a docked page instead"); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(680.0, 480.0)).build_ui_state( + |ui, (app, ready): &mut (PhotocraftApp, bool)| { + if *ready { + egui::CentralPanel::default().frame(egui::Frame::NONE).show(ui, |ui| document_area(app, ui)); + } + }, + (app, false), + ); + PhotocraftApp::setup_context(&h.ctx, kind); + h.state_mut().1 = true; + h.run_steps(3); + let tabs = h.state().0.tab_strip.as_ref().map(|s| s.tabs.iter().map(|(doc, _)| *doc).collect::>()); + assert_eq!(tabs, Some(vec![1]), "{kind:?}: only the docked document stays on the main tab strip"); + } +} diff --git a/crates/ui-egui/src/canvas/textures.rs b/crates/ui-egui/src/canvas/textures.rs new file mode 100644 index 0000000000..c6aaeb436b --- /dev/null +++ b/crates/ui-egui/src/canvas/textures.rs @@ -0,0 +1,688 @@ +use super::display::{GPU_OUTPUT, cache_key, canvas_display, display_doc, display_image, texture_key}; +use super::filter_preview::filter_channel_preview; +use super::navigator::navigator_waits_for_drag; +use super::strokes::live_stroke; +use super::view_xform::ViewXform; +use super::{CanvasCache, CpuTextures, DRect, DetailTexture, Document, InView, LayerContent, PhotocraftApp, TextureOptions, pos2}; + +/// Largest texture side we upload; bigger documents are uploaded downsampled, with the part in +/// view added at the screen's resolution when zoomed in (`detail_plan`). +pub const MAX_TEXTURE: u32 = 4096; + +/// The largest texture side to upload on this device: [`MAX_TEXTURE`], or less where the GPU +/// allows less (2048 on some older or low-end GPUs and WebGL contexts). Past the device's limit +/// egui asserts and the backend fails to create the texture. +pub(super) fn texture_side(ctx: &egui::Context) -> u32 { + clamp_texture_side(ctx.input(|i| i.max_texture_side)) +} + +/// [`texture_side`] for a device whose largest texture side is `max_texture_side`. +pub(super) fn clamp_texture_side(max_texture_side: usize) -> u32 { + u32::try_from(max_texture_side).unwrap_or(MAX_TEXTURE).clamp(1, MAX_TEXTURE) +} + +/// How many document pixels per side one texel of the CPU canvas texture of a `size` document +/// holds when textures are at most `side` texels per side: 1 (full resolution) when it fits. +pub(super) fn texture_factor(size: [u32; 2], side: u32) -> u32 { + size[0].max(size[1]).div_ceil(side.max(1)).max(1) +} + +/// What the CPU canvas draws over its reduced texture so a large document shows its own pixels +/// at 100 % (#7568): the part of the document in view (`region`, document pixels) composited at +/// one texel per `factor` pixels, a power of two no coarser than the screen. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) struct DetailPlan { + pub factor: u32, + pub region: DRect, +} + +/// The detail region snaps outward to multiples of this many texels, so a small pan reuses it. +const DETAIL_QUANTUM: i32 = 256; + +/// The detail texture a CPU canvas view of a `size` document needs, at `zoom` physical pixels +/// per document pixel over `visible` (document pixels), with textures at most `side` texels per +/// side: `None` when the whole-document texture is already as fine as the screen (a document +/// that fits, or a view zoomed out far enough), or nothing of the document is in view. +/// +/// A document over `side` (4096 px, or the device's smaller limit) is uploaded reduced, so at +/// 100 % it was drawn magnified and blurred. The detail costs what the window shows, not what the +/// document holds: its side is capped by `side` like any texture, coarsening by powers of two. +pub(crate) fn detail_plan(size: [u32; 2], side: u32, zoom: f32, visible: DRect) -> Option { + if !zoom.is_finite() || zoom <= 0.0 { + return None; + } + let base = texture_factor(size, side); + let mut factor = crate::adjust_preview::proxy_factor(zoom); + let bounds = DRect::new(0, 0, i32::try_from(size[0]).ok()?, i32::try_from(size[1]).ok()?); + let v = visible.intersect(&bounds); + if v.is_empty() { + return None; + } + while factor < base { + let q = DETAIL_QUANTUM.saturating_mul(i32::try_from(factor).unwrap_or(i32::MAX)); + let up = |x: i32| x.div_euclid(q).saturating_add(i32::from(x.rem_euclid(q) != 0)).saturating_mul(q); + let region = DRect::new(v.x0.div_euclid(q).saturating_mul(q), v.y0.div_euclid(q).saturating_mul(q), up(v.x1), up(v.y1)).intersect(&bounds); + // A reduced region's texel count can round one up (`render_reduced_damage`'s cells). + let fits = |n: u32| n.div_ceil(factor).saturating_add(1) <= side; + // Snapped when that fits, else just what is in view (a window near the device's limit). + for region in [region, v] { + if !region.is_empty() && fits(region.width()) && fits(region.height()) { + return Some(DetailPlan { factor, region }); + } + } + factor = factor.saturating_mul(2); + } + None +} + +/// The CPU canvas texture's sampling (#4810). Zoomed in, a full-resolution texture shows each +/// document pixel as a crisp square, as the GPU canvas's sharp nearest filter and Photoshop do; +/// zoomed out it is still filtered. A reduced texture (`factor` > 1) stays bilinear: its texels +/// are averages of several pixels, and squares of them would be blocks the image doesn't have. +pub(super) fn canvas_texture_options(factor: u32) -> TextureOptions { + if factor > 1 { TextureOptions::LINEAR } else { TextureOptions { magnification: egui::TextureFilter::Nearest, ..TextureOptions::LINEAR } } +} + +/// Make sure the canvas texture for document `idx` on `display` is current; returns (texture +/// id, scale). +pub fn ensure_texture(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, display: Option) -> Option<(egui::TextureId, f32)> { + ensure_texture_in_view(app, ctx, idx, display, None).map(|t| (t.texture, t.scale)) +} + +/// The document area a filter preview of `preview` size covers, for a `doc` sized document +/// previewed at proxy factor `k`: the document's bounds when it is the document's size (reduced by +/// `k`), else its own size (times `k`) centred on the document's centre, as the GPU canvas centres +/// a size-changing preview (Image › Rotation). +pub(super) fn preview_bounds(doc: photocraft_geom::Size, preview: photocraft_geom::Size, k: u32) -> DRect { + let k = k.max(1); + let full = DRect::from_size(doc); + if doc.width.div_ceil(k) == preview.width && doc.height.div_ceil(k) == preview.height { + return full; + } + let side = |p: u32, d: u32| { + let covered = i64::from(p) * i64::from(k); + let x0 = (i64::from(d) - covered).div_euclid(2); + let clamp = |v: i64| v.clamp(i64::from(i32::MIN), i64::from(i32::MAX)) as i32; + (clamp(x0), clamp(x0 + covered)) + }; + let (x0, x1) = side(preview.width, doc.width); + let (y0, y1) = side(preview.height, doc.height); + DRect::new(x0, y0, x1, y1) +} + +/// [`ensure_texture`], and for a canvas showing `view` of a document too large for one +/// full-resolution texture, the detail texture of the part in view ([`detail_plan`], #7568). +pub(crate) fn ensure_texture_in_view( + app: &mut PhotocraftApp, + ctx: &egui::Context, + idx: usize, + display: Option, + view: Option, +) -> Option { + let output = display; + let (revision, last_damage, id) = { + let st = app.session.documents().get(idx)?; + (st.revision, st.last_damage.map(|r| if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }), st.doc.id) + }; + let (mut doc, mut preview_key) = display_doc(app, idx); + // A filter dialog's live preview: the GPU canvas draws it from its own texture, but this CPU + // canvas (no usable GPU, a flipped or pixel-aspect-corrected view, a GPU fallback) showed the + // document unfiltered. Its result is drawn here instead, as the channel views draw it. + // A preview reduced for a zoomed-out view (a proxy, factor `k`) covers the whole document at + // its own size. One whose dimensions change otherwise (Image › Rotation) covers its own area, + // centred on the document's, as the GPU canvas draws it (`preview_bounds`). + let committed = app.session.documents().get(idx).map(|st| st.doc.bounds()).unwrap_or(doc.bounds()); + let mut bounds = committed; + let mut reduced = false; + if let Some((preview, key)) = filter_channel_preview(app, idx, ctx) { + let k = app.filter_preview.as_ref().map_or(1, |p| p.key.k.max(1)); + bounds = preview_bounds(committed.size(), preview.size, k); + (doc, preview_key, reduced) = (preview, key, k > 1); + } + // Detail textures and partial redraws work in the document's own pixels. + let reduced = reduced || bounds != committed; + let view = view.filter(|_| !reduced); + // A flipped view draws a GPU machine's canvas through here: an adjustment dialog's preview of + // a large document (taken by the GPU path) would cost a full-size CPU composite per change. + if crate::adjust_preview::shown_key(app) == Some(preview_key) + && let Some(st) = app.session.documents().get(idx) + && crate::proxy::preview_factor(&st.doc, crate::proxy::reduced_previews(app)) > 1 + { + (doc, preview_key) = (st.doc.clone(), 0); + } + let (display, display_key) = canvas_display(app, &doc, output); + let seen = app.canvases.get(&cache_key(id, output)).map(|c| (c.tex_revision, c.tex_preview_key)); + let damage = seen.and_then(|seen| damage_since(app, idx, seen, (revision, preview_key), display_key, last_damage)).filter(|_| !reduced); + // The detail texture can lag the whole-document one (made or moved later): its own damage. + let detail_seen = view.and_then(|v| { + let c = app.canvases.get(&cache_key(id, output))?; + c.detail.iter().find(|(k, _)| *k == v.key).map(|(_, d)| (d.revision, d.preview_key)) + }); + let detail_damage = match detail_seen { + Some(s) if Some(s) == seen => damage, + Some(s) => damage_since(app, idx, s, (revision, preview_key), display_key, last_damage), + None => None, + }; + let preview_key = preview_key ^ display_key; + let cache = app.canvases.entry(cache_key(id, output)).or_insert(CanvasCache { + revision: 0, + texture: None, + scale: 1.0, + preview_key: 0, + on_gpu: false, + tex_revision: 0, + tex_preview_key: 0, + texture_opaque: false, + grid: None, + detail: Vec::new(), + }); + if cache.tex_revision != revision || cache.texture.is_none() || cache.tex_preview_key != preview_key { + let t0 = crate::gpu_canvas::now_ms(); + let factor = texture_factor([doc.size.width, doc.size.height], texture_side(ctx)); + let (w, h) = ((doc.size.width / factor).max(1), (doc.size.height / factor).max(1)); + let current = cache.texture.as_mut().filter(|t| t.size() == [w as usize, h as usize]); + if let (Some(d), Some(t)) = (damage, current) { + // Only what the edit or stroke touched: the reduced texture's pixels over it, with the + // values the whole reduction gives them (factor 1 is the plain composite of `d`). + let buf = photocraft_compose::render_reduced_damage(&doc, w, h, d); + let r = buf.rect; + if !r.is_empty() { + let t1 = crate::gpu_canvas::now_ms(); + let image = display_image(display.as_deref(), &buf); + // Partial uploads can disprove opacity. Once transparent, conservatively keep + // the checker until a full upload proves the whole texture opaque again. + cache.texture_opaque &= image.pixels.iter().all(|p| p.is_opaque()); + t.set_partial([r.x0 as usize, r.y0 as usize], image, canvas_texture_options(factor)); + let px = (r.width() as u64 * r.height() as u64).saturating_mul(u64::from(factor).pow(2)); + app.perf.record("rect", px, t1 - t0, crate::gpu_canvas::now_ms() - t1); + } + } else { + // Reduced in bands straight from the compositor: no full-size composite in memory. + let full = photocraft_compose::render_reduced(&doc, w, h); + let t1 = crate::gpu_canvas::now_ms(); + let (img, scale) = (display_image(display.as_deref(), &full), 1.0 / factor as f32); + cache.texture_opaque = img.pixels.iter().all(|p| p.is_opaque()); + match cache.texture.as_mut() { + Some(t) if t.size() == img.size => t.set(img, canvas_texture_options(factor)), + _ => cache.texture = Some(ctx.load_texture(format!("canvas-{}-{}", id.0, cache_key(id, output).1), img, canvas_texture_options(factor))), + } + cache.scale = scale; + app.perf.record("full", doc.size.width as u64 * doc.size.height as u64, t1 - t0, crate::gpu_canvas::now_ms() - t1); + } + cache.tex_revision = revision; + cache.tex_preview_key = preview_key; + } + let texture = cache.texture.as_ref()?.id(); + let scale = cache.scale; + // Without a view (the Navigator's call) the views' detail textures stay as they are. + let Some(view) = view else { return Some(CpuTextures { texture, scale, bounds, detail: None }) }; + let old = cache.detail.iter().position(|(k, _)| *k == view.key).map(|i| cache.detail.remove(i).1); + let Some(plan) = detail_plan([doc.size.width, doc.size.height], texture_side(ctx), view.zoom, view.visible) else { + return Some(CpuTextures { texture, scale, bounds, detail: None }); + }; + let t0 = crate::gpu_canvas::now_ms(); + let name = format!("canvas-detail-{}-{}", id.0, cache_key(id, output).1); + let detail = update_detail(ctx, &doc, display.as_deref(), name, plan, old, detail_damage, (revision, preview_key)); + if detail.as_ref().is_some_and(|d| d.rendered) { + app.perf.span("canvas detail", crate::gpu_canvas::now_ms() - t0); + } + let Some(mut detail) = detail else { return Some(CpuTextures { texture, scale, bounds, detail: None }) }; + detail.rendered = false; + // Where its texels lie in the document: reduced texel t covers [t·W/w, (t+1)·W/w). + let (w, h) = ((doc.size.width / plan.factor).max(1), (doc.size.height / plan.factor).max(1)); + let (sx, sy) = (doc.size.width as f32 / w as f32, doc.size.height as f32 / h as f32); + let out = detail.out; + let shown = (detail.texture.id(), [out.x0 as f32 * sx, out.y0 as f32 * sy, out.x1 as f32 * sx, out.y1 as f32 * sy]); + cache.detail.push((view.key, detail)); + // Views that stopped asking (a closed window, a zoom out) give theirs up past a few. + if cache.detail.len() > MAX_DETAIL_VIEWS { + cache.detail.remove(0); + } + Some(CpuTextures { texture, scale, bounds, detail: Some(shown) }) +} + +/// Most CPU canvas views of one document on one display that keep a detail texture. +const MAX_DETAIL_VIEWS: usize = 4; + +/// The detail texture for `plan` at document state `now` (revision, preview key with the display +/// key folded in): `old` as it is when current, updated over `damage` when only that changed +/// within the same region, or rendered again. `None` when the region holds no texels. +#[allow(clippy::too_many_arguments)] +fn update_detail( + ctx: &egui::Context, + doc: &Document, + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + name: String, + plan: DetailPlan, + old: Option, + damage: Option, + now: (u64, u64), +) -> Option { + let (w, h) = ((doc.size.width / plan.factor).max(1), (doc.size.height / plan.factor).max(1)); + let options = canvas_texture_options(plan.factor); + let (kept, spare) = match old { + Some(d) if d.plan == plan => (Some(d), None), + d => (None, d.map(|d| d.texture)), + }; + if let Some(mut d) = kept { + if (d.revision, d.preview_key) == now { + return Some(d); + } + // An edit or a stroke step in view: only the texels it touched. + if let Some(dmg) = damage { + let dmg = dmg.intersect(&plan.region); + let buf = (!dmg.is_empty()).then(|| photocraft_compose::render_reduced_damage(doc, w, h, dmg)); + let fits = buf.as_ref().is_none_or(|b| d.out.contains_rect(&b.rect)); + if fits { + if let Some(b) = buf.filter(|b| !b.rect.is_empty()) { + let at = [b.rect.x0.abs_diff(d.out.x0) as usize, b.rect.y0.abs_diff(d.out.y0) as usize]; + d.texture.set_partial(at, display_image(display, &b), options); + d.rendered = true; + } + (d.revision, d.preview_key) = now; + return Some(d); + } + } + return render_detail(ctx, doc, display, name, plan, Some(d.texture), now); + } + render_detail(ctx, doc, display, name, plan, spare, now) +} + +/// [`update_detail`]'s whole render of `plan`'s region, into `texture` when it is the same size. +fn render_detail( + ctx: &egui::Context, + doc: &Document, + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + name: String, + plan: DetailPlan, + texture: Option, + now: (u64, u64), +) -> Option { + let (w, h) = ((doc.size.width / plan.factor).max(1), (doc.size.height / plan.factor).max(1)); + let options = canvas_texture_options(plan.factor); + let buf = photocraft_compose::render_reduced_damage(doc, w, h, plan.region); + let out = buf.rect; + if out.is_empty() { + return None; + } + let img = display_image(display, &buf); + let texture = match texture { + Some(mut t) if t.size() == img.size => { + t.set(img, options); + t + } + _ => ctx.load_texture(name, img, options), + }; + Some(DetailTexture { texture, plan, out, revision: now.0, preview_key: now.1, rendered: true }) +} + +/// What changed since a canvas cache showed (`revision`, `preview key`) `seen`, when only a +/// rectangle did: the last edit's damage, or the live stroke's dabs since then. Cached keys have +/// the colour display's key folded in (`^ display_key`); `now`'s is still raw. +pub(crate) fn damage_since(app: &PhotocraftApp, idx: usize, seen: (u64, u64), now: (u64, u64), display_key: u64, last_damage: Option) -> Option { + // A brush stroke committed with the pixels its live preview already showed: only the steps + // this cache hadn't shown yet (usually nothing), not the stroke's whole bounding box. + if seen.0 + 1 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(c) = app.committed_stroke.as_ref().filter(|c| c.doc == st.doc.id && c.revision == now.0) + && let Some(r) = c.since(seen.1 ^ display_key) + { + let r = if c.relayout.is_empty() { + r + } else if r.is_empty() { + c.relayout + } else { + r.union(&c.relayout) + }; + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + if seen.1 == now.1 ^ display_key && seen.0 + 1 == now.0 { + return last_damage; + } + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::type_transform::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + // Between an adjustment dialog's previews (and the document): the target's area. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::adjust_preview::switch_region(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(r); + } + // Between a Properties-panel adjustment-layer drag's previews (and the document): the + // layer's area. Without this every parameter value recomposited the whole document (#6839). + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::adjust_preview::live_switch_region(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(r); + } + // Between a Move drag's offsets: where the moving layers were and are (Auto-Select's pick + // at the press changes no pixels). + if (seen.0 == now.0 || (seen.0 + 1 == now.0 && last_damage.is_some_and(|r| r.is_empty()))) + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::move_ui::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + // Between a Patch drag's previews: the areas they healed. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::patch_preview::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + // Between Vanishing Point stamp previews (and the document): the areas the strokes painted. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::vp_ui::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + // Between hovered blend modes (and the document): where the layer's blending shows. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::blend_preview::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + // Between hovered fonts (and the document): the text layer's old/new ink bounds. + if seen.0 == now.0 + && let Some(st) = app.session.documents().get(idx) + && let Some(r) = crate::type_font_preview::damage(app, st.doc.id, now.0, seen.1 ^ display_key, now.1) + { + return Some(if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }); + } + let l = live_stroke(app, idx).filter(|l| seen.0 == now.0 && l.display_key() == now.1)?; + let r = l.since(seen.1 ^ display_key)?; + Some(if r.is_empty() { r } else { r.inflate(effect_reach(&l.stroke.doc().layers)) }) +} + +/// Document `doc`'s canvas caches showed a preview that the edit just committed reproduces +/// (`was_preview` tells its keys): count it as the document itself, so the commit's damage rect +/// refreshes only that area instead of everything. +pub(crate) fn shown_as_document(app: &mut PhotocraftApp, doc: photocraft_doc::DocId, was_preview: impl Fn(u64) -> bool) { + let Some(d) = app.session.documents().iter().find(|st| st.doc.id == doc).map(|st| st.doc.clone()) else { return }; + if let Some(c) = app.channel_views.get_mut(&doc.0) + && was_preview(c.preview_key) + { + c.preview_key = 0; + } + // Every display's cache of the document: the GPU state folds in the texture key, CPU + // textures their display's key. + let outputs: Vec = app.canvases.keys().filter(|k| k.0 == doc).map(|k| k.1).collect(); + for out in outputs { + let (display, key) = canvas_display(app, &d, (out != 0 && out != GPU_OUTPUT).then_some(out)); + let gpu_key = texture_key(display.as_deref()); + let Some(c) = app.canvases.get_mut(&(doc, out)) else { continue }; + if was_preview(c.preview_key ^ gpu_key) { + c.preview_key = gpu_key; + } + if was_preview(c.tex_preview_key ^ key) { + c.tex_preview_key = key; + } + } +} + +/// How far beyond an edit's damage rect the composite can change: layer effects (shadows, glows, +/// strokes, …) on the edited layer and on the groups around it reach that far. +pub(crate) fn effect_reach(layers: &[photocraft_doc::Layer]) -> i32 { + layers + .iter() + .filter(|l| l.visible) + .map(|l| { + let own = if photocraft_compose::effects::has_effects(l) { photocraft_compose::effects::margin(l) } else { 0 }; + let inner = match &l.content { + LayerContent::Group(g) => effect_reach(&g.children), + _ => 0, + }; + own + inner + }) + .max() + .unwrap_or(0) +} + +/// GPU path: make sure document `idx` is current in the GPU canvas. Edits draw their damage; +/// unchanged lower-stack composites can be reused by the compositor inside that region. +/// Returns false if there is no GPU canvas. +pub(super) fn ensure_gpu(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize, visible: DRect) -> bool { + let Some(gpu) = app.gpu.clone() else { return false }; + let Some((revision, last_damage, id)) = app + .session + .documents() + .get(idx) + .map(|st| (st.revision, st.last_damage.map(|r| if r.is_empty() { r } else { r.inflate(effect_reach(&st.doc.layers)) }), st.doc.id)) + else { + return false; + }; + let (doc, raw_key) = display_doc(app, idx); + let (display, _) = canvas_display(app, &doc, None); + // Repeated pattern tiles sample the same texture, including pixels outside the unwrapped + // viewport. Refresh the affected document region while Pattern Preview is enabled. + let blend_visible = if app.ui.view.pattern_preview { doc.bounds() } else { visible }; + // The texture is shared by every display: only what it stores counts, not the monitor. + let display_key = texture_key(display.as_deref()); + let seen = app.canvases.get(&(id, GPU_OUTPUT)).map(|c| (c.revision, c.preview_key)); + let damage_start = crate::gpu_canvas::now_ms(); + let mut damage = seen.and_then(|seen| damage_since(app, idx, seen, (revision, raw_key), display_key, last_damage)); + if let Some(region) = damage { + damage = Some(crate::blend_preview::clip_damage(app, id, revision, raw_key, region, blend_visible)); + } + app.perf.stage.damage_ms += crate::gpu_canvas::now_ms() - damage_start; + // To an adjustment dialog's preview: only what the view shows now (the rest as it moves there). + if damage.is_some() + && let Some(r) = seen.filter(|s| s.0 == revision).and_then(|s| crate::adjust_preview::switch_region(app, id, revision, s.1 ^ display_key, raw_key)) + { + damage = Some(crate::adjust_preview::switch_damage(app, raw_key, r, visible)); + } + // The same for a Properties-panel drag's previews. + if damage.is_some() + && let Some(r) = seen.filter(|s| s.0 == revision).and_then(|s| crate::adjust_preview::live_switch_region(app, id, revision, s.1 ^ display_key, raw_key)) + { + damage = Some(crate::adjust_preview::live_switch_damage(app, raw_key, r, visible)); + } + let preview_key = raw_key ^ display_key; + // The Navigator may draw this texture while it holds the plain document or a drag preview + // that updates the full texture (not an adjustment preview composited only where the view is). + if raw_key == 0 || navigator_waits_for_drag(app, idx) { + app.navigator_plain.insert(id); + } else { + app.navigator_plain.remove(&id); + app.navigator_gpu.remove(&id); + } + let size = [doc.size.width, doc.size.height]; + // A full CPU redraw running in the background: show the blocks it has finished. + crate::background_redraw::poll(app, &gpu, id); + let cache = app.canvases.entry((id, GPU_OUTPUT)).or_insert(CanvasCache { + revision: 0, + texture: None, + scale: 1.0, + preview_key: 0, + on_gpu: false, + tex_revision: 0, + tex_preview_key: 0, + texture_opaque: false, + grid: None, + detail: Vec::new(), + }); + let present = cache.on_gpu && gpu.has(id.0, size); + let current = cache.revision == revision && cache.preview_key == preview_key; + // GPU Memory or Memory Usage changed with nothing to redraw: the new budget, and whether the + // layers fit it, show at once (the status bar's GPU Memory Full). + if present && app.perf.gpu_budget_allowance != budget_allowance(app) { + gpu_budget(app, &gpu, idx, false, visible); + } + // Switched to a document whose canvas is already current: size the budget for it and measure + // whether its layers fit, so GPU Memory Full shows (or clears) at once rather than at its + // next full refresh. Once per switch: the fit then names this document. + if present && app.perf.gpu_layer_fit.is_none_or(|f| f.doc != id.0) { + gpu_budget(app, &gpu, idx, false, visible); + let fit = gpu.measure_fit(&doc).unwrap_or(crate::gpu_canvas::LayerFit { doc: id.0, needed: 0, budget: 0 }); + crate::render_path::layer_fit(app, fit); + } + if present && current { + // An adjustment preview composited where the view was: catch up where it moved to. + if let Some(r) = crate::adjust_preview::uncovered(app, id, raw_key, visible) { + let r = gpu.refresh(id.0, &doc, Some(r), display.as_deref()); + app.perf.record_refresh(&r); + } + if let Some(r) = crate::adjust_preview::live_uncovered(app, id, raw_key, visible) { + let r = gpu.refresh(id.0, &doc, Some(r), display.as_deref()); + app.perf.record_refresh(&r); + } + // Blend previews keep offscreen pixels untouched until the view reaches them. + // Use the normal refresh path so CPU fallback and pending redraws stay synchronized. + damage = crate::blend_preview::uncovered(app, id, revision, raw_key, blend_visible); + if damage.is_none() { + return true; + } + } + // Display textures for a canvas this large would not fit the GPU: the reduced CPU canvas + // draws it (checked before the memory budget, which walks the history, every frame). + if !gpu.fits_canvas(&doc) { + crate::background_redraw::cancel(app, id); + // Drawn on the CPU canvas: no layer budget applies to it. + if app.perf.gpu_layer_fit.is_some_and(|f| f.doc == id.0) { + app.perf.gpu_layer_fit = None; + } + let (bytes, budget) = gpu.canvas_fit(&doc).unwrap_or_default(); + crate::render_path::note(app, id.0, crate::render_path::Path::Cpu(crate::render_path::CpuReason::CanvasTooLarge { bytes, budget })); + return false; + } + let partial = present && damage.is_some(); + let budget_start = crate::gpu_canvas::now_ms(); + gpu_budget(app, &gpu, idx, partial, visible); + app.perf.stage.damage_ms += crate::gpu_canvas::now_ms() - budget_start; + let defer = crate::background_redraw::enabled(app); + let r = gpu.refresh_with(id.0, &doc, if partial { damage } else { None }, display.as_deref(), defer); + app.perf.gpu_layer_fit = gpu.layer_fit(); + if r.kind == "lost" || r.kind == crate::gpu_canvas::REFRESH_OVER_BUDGET { + // The device is gone (`gpu_status` then drops the GPU canvas) or the canvas is over the + // texture budget: draw this frame on the CPU path. + crate::background_redraw::cancel(app, id); + if let Some(reason) = r.fallback { + crate::render_path::note(app, id.0, crate::render_path::Path::Cpu(reason)); + } + return false; + } + if r.kind == crate::gpu_canvas::REFRESH_DEFERRED { + // Composited off the UI thread, the visible part first (#4024). + let region = if partial { damage.unwrap_or(doc.bounds()) } else { doc.bounds() }; + crate::background_redraw::start(app, ctx, &gpu, doc.clone(), display.clone(), visible, region, r.fallback.clone()); + } else if partial { + // The edit drew its damage; blocks of a running redraw still to come follow the edit. + crate::background_redraw::restart(app, ctx, &gpu, doc.clone(), display.clone(), visible); + } else { + crate::background_redraw::cancel(app, id); + } + match &r.fallback { + Some(reason) => crate::render_path::note(app, id.0, crate::render_path::Path::Cpu(reason.clone())), + None if r.kind.starts_with("gpu") => crate::render_path::note(app, id.0, crate::render_path::Path::Gpu { full: r.kind == "gpu-full" }), + None => {} + } + if r.kind != crate::gpu_canvas::REFRESH_DEFERRED { + app.perf.record_refresh(&r); + } + if r.kind == "full" { + crate::notices::slow_cpu_refresh(app, id.0, r.composite_ms + r.upload_ms, r.fallback.as_ref()); + } + if r.kind.starts_with("gpu") { + app.perf.gpu_uploads = r.uploads; + } + let Some(cache) = app.canvases.get_mut(&(id, GPU_OUTPUT)) else { return true }; + cache.revision = revision; + cache.preview_key = preview_key; + cache.on_gpu = true; + cache.texture = None; + true +} + +/// Point the wgpu compositor at what the view shows, and size its memory budget from GPU Memory, +/// or Memory Usage, the document's pixels and History (charged where the GPU shares the system's +/// memory), physical memory and the GPU's own: on full refreshes (pixels counted once per +/// structural change, not per brush dab) and whenever either preference changes. +fn gpu_budget(app: &mut PhotocraftApp, gpu: &crate::gpu_canvas::GpuCanvas, idx: usize, partial: bool, visible: DRect) { + gpu.set_focus(Some(visible)); + let allowance = budget_allowance(app); + let changed = app.perf.gpu_budget_allowance != allowance; + if partial && !changed && gpu.memory_budget().is_some() { + return; + } + let Some(st) = app.session.documents().get(idx) else { return }; + let pixels = st.memory_bytes() as u64; + let budget = crate::gpu_canvas::memory_budget(allowance.0, pixels, crate::gpu_canvas::physical_memory(), gpu.video_memory(), allowance.1); + gpu.set_memory_budget(budget); + app.perf.gpu_budget_mb = budget >> 20; + app.perf.gpu_budget_allowance = allowance; + if changed && let Some(fit) = gpu.measure_fit(&st.doc) { + crate::render_path::layer_fit(app, fit); + } +} + +/// Memory Usage and GPU Memory (Preferences › Performance), in bytes. +fn budget_allowance(app: &PhotocraftApp) -> (u64, u64) { + let performance = &app.session.prefs().performance; + (u64::from(performance.effective_memory_usage_mb()).saturating_mul(1 << 20), u64::from(performance.gpu_memory_mb).saturating_mul(1 << 20)) +} + +/// The document pixels a view shows, with a margin for filtering. Uses all four canvas corners +/// so a rotated camera still covers the visible tiles. +pub(super) fn visible_doc_rect(xf: &ViewXform) -> DRect { + let r = xf.rect; + let docs = [xf.to_doc(r.min), xf.to_doc(pos2(r.max.x, r.min.y)), xf.to_doc(r.max), xf.to_doc(pos2(r.min.x, r.max.y))]; + let mut x0 = docs[0][0]; + let mut y0 = docs[0][1]; + let mut x1 = x0; + let mut y1 = y0; + for d in docs { + x0 = x0.min(d[0]); + y0 = y0.min(d[1]); + x1 = x1.max(d[0]); + y1 = y1.max(d[1]); + } + let c = |v: f64| v.clamp(-1e9, 1e9) as i32; + DRect::new(c(x0.floor()) - 2, c(y0.floor()) - 2, c(x1.ceil()) + 2, c(y1.ceil()) + 2) +} + +/// Prioritize deferred redraw blocks visible at either the target or the animated display camera. +/// The displayed camera may still show old texture contents outside the target's visible area. +pub(super) fn redraw_focus(target: &ViewXform, displayed: &ViewXform) -> DRect { + visible_doc_rect(target).union(&visible_doc_rect(displayed)) +} + +/// A CPU composite as the GPU canvas texture stores it (sRGB-encoded for linear documents). +pub(super) fn texture_buffer<'a>( + display: Option<&photocraft_engine::display_color::CanvasDisplay>, + buf: &'a photocraft_compose::Buffer, +) -> std::borrow::Cow<'a, photocraft_compose::Buffer> { + match display { + Some(d) => d.texture_buffer(buf), + None => std::borrow::Cow::Borrowed(buf), + } +} + +pub(crate) fn retain_gpu_documents(app: &mut PhotocraftApp) { + // Remove a closed adjustment owner's proxy before collecting its live GPU keys. + crate::adjust_preview::retain_documents(app); + if let Some(gpu) = &app.gpu { + // Keep each document's texture plus its preview textures (filter preview, adjustment proxy); + // retaining only document ids freed the previews every frame (blank canvas while previewing). + let mut live: Vec = app.session.documents().iter().flat_map(|st| [st.doc.id.0, st.doc.id.0 ^ (1u64 << 61), st.doc.id.0 ^ (1u64 << 62)]).collect(); + live.extend(crate::adjust_preview::gpu_keys(app)); + gpu.retain(&live); + } + crate::background_redraw::retain(app); + // Upload markers must not outlive the resources they describe: native reopen can reuse + // both the document ID and revision before the next frame while a preview dialog stays open. + let documents = app.session.documents(); + if app.filter_preview.as_ref().is_some_and(|preview| !documents.iter().any(|st| st.doc.id == preview.key.doc)) { + app.filter_preview = None; + } + if app.proxy_uploaded.is_some_and(|(doc, _)| !documents.iter().any(|st| st.doc.id == doc)) { + app.proxy_uploaded = None; + app.proxy = None; + } +} diff --git a/crates/ui-egui/src/tile_overlay.rs b/crates/ui-egui/src/canvas/tile_overlay.rs similarity index 99% rename from crates/ui-egui/src/tile_overlay.rs rename to crates/ui-egui/src/canvas/tile_overlay.rs index 731ddc06b8..2afc0c98ef 100644 --- a/crates/ui-egui/src/tile_overlay.rs +++ b/crates/ui-egui/src/canvas/tile_overlay.rs @@ -141,7 +141,7 @@ pub fn paint(app: &PhotocraftApp, ctx: &egui::Context, painter: &Painter, xf: &V let Some(st) = app.session.documents().get(idx) else { return }; let Some(surface) = st.active_layer.and_then(|id| st.doc.layer(id)).and_then(|l| l.surface()) else { return }; let revision = st.revision; - let mem = egui::Id::new(("pc-tile-overlay", st.doc.id.0, st.active_layer.map(|l| l.0))); + let mem = egui::Id::unique(("pc-tile-overlay", st.doc.id.0, st.active_layer.map(|l| l.0))); let cached = ctx.data(|d| d.get_temp::(mem)).filter(|c| c.revision == revision); let tiles = match cached { Some(c) => c.tiles, diff --git a/crates/ui-egui/src/tool_cursor.rs b/crates/ui-egui/src/canvas/tool_cursor.rs similarity index 77% rename from crates/ui-egui/src/tool_cursor.rs rename to crates/ui-egui/src/canvas/tool_cursor.rs index 8b67b96d60..fbe108f256 100644 --- a/crates/ui-egui/src/tool_cursor.rs +++ b/crates/ui-egui/src/canvas/tool_cursor.rs @@ -92,6 +92,43 @@ fn close_ring() -> Vec { (0..16).map(|i| (i as f32 / 16.0 * std::f32::consts::TAU, vec2(9.0, 9.0))).map(|(a, c)| c + vec2(a.cos(), a.sin()) * 3.5).collect() } +/// Closed outline of an I-beam, centred on the hotspot. +fn i_beam() -> Vec { + [(-3.0, -8.0), (3.0, -8.0), (3.0, -7.0), (0.5, -7.0), (0.5, 7.0), (3.0, 7.0), (3.0, 8.0), (-3.0, 8.0), (-3.0, 7.0), (-0.5, 7.0), (-0.5, -7.0), (-3.0, -7.0)] + .into_iter() + .map(|(x, y)| vec2(x, y)) + .collect() +} + +/// A dot (a tiny closed square) at `c`. +fn dot(c: Vec2) -> Vec { + vec![c + vec2(-0.75, -0.75), c + vec2(0.75, -0.75), c + vec2(0.75, 0.75), c + vec2(-0.75, 0.75)] +} + +/// Photoshop's area-type pointer (#218): the I-beam inside a dotted ellipse. +pub(crate) fn area_type_rings() -> Vec> { + let mut rings = vec![i_beam()]; + rings.extend((0..12).map(|i| i as f32 / 12.0 * std::f32::consts::TAU).map(|a| dot(vec2(a.cos() * 8.0, a.sin() * 10.5)))); + rings +} + +/// Photoshop's type-on-a-path pointer (#218): the I-beam crossed by a dotted wave. +pub(crate) fn path_type_rings() -> Vec> { + let mut rings = vec![i_beam()]; + rings.extend((0..9).map(|i| -10.0 + i as f32 * 2.5).map(|x| dot(vec2(x, 4.0 + (x * 0.4).sin() * 2.0)))); + rings +} + +/// The Type tool over the inside of a closed path: a click starts area type there. +pub(crate) fn area_type(painter: &Painter, at: Pos2) -> CursorIcon { + outline(painter, at, area_type_rings(), false) +} + +/// The Type tool over a path's outline: a click starts type on the path there. +pub(crate) fn path_type(painter: &Painter, at: Pos2) -> CursorIcon { + outline(painter, at, path_type_rings(), false) +} + /// Can this pass show an OS cursor bitmap? eframe's immediate viewports and its web integration /// don't upload cursor images; every desktop root viewport does (winit custom cursors). Wayland at /// any scale but 1 doesn't either: winit sizes the bitmap there with a `wp_viewport` destination @@ -103,14 +140,61 @@ fn native(ctx: &egui::Context) -> bool { cfg!(not(target_arch = "wasm32")) && ctx.viewport_id() == egui::ViewportId::ROOT && !scaled_wayland(ctx) } +/// Whether the pointer this viewport last saw has moved on to another of the app's windows, +/// `others` (the current viewport among them is skipped): from the frame one of them reports the +/// pointer arriving until this viewport gets a pointer event again. macOS sends a window no +/// pointer-left event when the pointer moves onto another of the app's windows lying over it (a +/// floating panel's own OS window), so egui's pointer stays where it was last seen. This also +/// covers the pointer moving on from that window to the desktop, where this one still hears +/// nothing. Always false where egui embeds viewports, as they share one window's pointer. +pub(crate) fn pointer_elsewhere(ctx: &egui::Context, others: &[egui::ViewportId]) -> bool { + let own = ctx.viewport_id(); + let key = egui::Id::unique(("pc-pointer-elsewhere", own)); + let before = ctx.data(|d| d.get_temp::(key)).unwrap_or(false); + let separate = !ctx.embed_viewports() && others.iter().any(|&v| v != own); + if !separate && !before { + return false; + } + let here = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::PointerMoved(_) | egui::Event::PointerButton { .. } | egui::Event::PointerGone))); + // A pointer that arrived: moving or pressing there. One that only stayed (a hidden window + // keeps its last one) does not take the pointer from this window. + let arrived = |i: &egui::InputState| { + i.pointer.has_pointer() && i.events.iter().any(|e| matches!(e, egui::Event::PointerMoved(_) | egui::Event::PointerButton { .. })) + }; + // Where this window has pointer events of its own too, another window's report counts only + // if this window's pointer lies inside that window: one just outside its edge, where both + // may hear the pointer, stays this window's. + let at = ctx.input(|i| i.viewport().inner_rect.zip(i.pointer.latest_pos()).map(|(r, p)| r.min + p.to_vec2())); + let there = separate && others.iter().any(|&v| v != own && ctx.input_for(v, |i| arrived(i) && (!here || covers(i.viewport().inner_rect, at)))); + let now = separate && elsewhere(before, here, there); + if now != before { + ctx.data_mut(|d| d.insert_temp(key, now)); + } + now +} + +/// Whether a window at `rect` (desktop points) lies over the desktop point `at`; unknown when +/// either is, which counts as covering. +fn covers(rect: Option, at: Option) -> bool { + rect.zip(at).is_none_or(|(r, p)| r.contains(p)) +} + +/// [`pointer_elsewhere`] for one frame: whether the pointer belonged to another window `before`, +/// whether this window got a pointer event (`here`) and whether another window reports the +/// pointer arriving (`there`). Both in one frame mean the pointer just crossed into the other +/// window: this window's last events come from before the crossing. +fn elsewhere(before: bool, here: bool, there: bool) -> bool { + there || (before && !here) +} + /// Mark this window as a Wayland surface (or not), for [`native`]; the shell calls it every pass. pub(crate) fn set_wayland(ctx: &egui::Context, wayland: bool) { - ctx.data_mut(|d| d.insert_temp(egui::Id::new("photocraft-cursor-wayland"), wayland)); + ctx.data_mut(|d| d.insert_temp(egui::Id::unique("photocraft-cursor-wayland"), wayland)); } /// A Wayland window whose scale is not 1, where winit's cursor bitmaps are unreliable. fn scaled_wayland(ctx: &egui::Context) -> bool { - let wayland = cfg!(target_os = "linux") && ctx.data(|d| d.get_temp::(egui::Id::new("photocraft-cursor-wayland"))).unwrap_or(false); + let wayland = cfg!(target_os = "linux") && ctx.data(|d| d.get_temp::(egui::Id::unique("photocraft-cursor-wayland"))).unwrap_or(false); wayland && ctx.native_pixels_per_point().unwrap_or_else(|| ctx.pixels_per_point() / ctx.zoom_factor()) != 1.0 } @@ -127,7 +211,34 @@ pub(crate) fn native_scale(ctx: &egui::Context) -> f32 { /// and Warp splits), the type box, Content-Aware Move's box, the Artboard tool and guides pick /// their arrows here. pub(crate) fn resize(d: [f64; 2], xf: &ViewXform) -> CursorIcon { - let s = xf.map_vec(vec2(d[0] as f32, d[1] as f32)); + resize_on_screen(xf.map_vec(vec2(d[0] as f32, d[1] as f32))) +} + +/// The resize arrow for corner `i` of a box's `quad` (corners in order round the box), as the view +/// shows it: along the corner's bisector, so an upright box's corners always show a diagonal arrow, +/// as in Photoshop, however wide or tall the box is. Aiming from the opposite corner instead gave a +/// box wider than about 2.4:1 the horizontal arrow at its corners (and a tall one the vertical). +pub(crate) fn corner_resize(quad: &[[f64; 2]; 4], i: usize, xf: &ViewXform) -> CursorIcon { + let at = quad[i % 4]; + let edge = |k: usize| { + let to = quad[k % 4]; + let v = xf.map_vec(vec2((at[0] - to[0]) as f32, (at[1] - to[1]) as f32)); + let l = v.length(); + (l.is_finite() && l > 1e-6).then(|| v / l) + }; + let bisector = edge(i + 1).zip(edge(i + 3)).map(|(a, b)| a + b).filter(|s| s.length() > 1e-3); + match bisector { + Some(s) => resize_on_screen(s), + // A collapsed or folded corner has no bisector: aim away from the opposite corner. + None => { + let opposite = quad[(i + 2) % 4]; + resize([at[0] - opposite[0], at[1] - opposite[1]], xf) + } + } +} + +/// The nearest of the four resize arrows to screen direction `s`. +fn resize_on_screen(s: Vec2) -> CursorIcon { let angle = f64::from(s.y).atan2(f64::from(s.x)).rem_euclid(std::f64::consts::PI); match (angle / std::f64::consts::FRAC_PI_4).round() as u8 % 4 { 0 => CursorIcon::ResizeHorizontal, @@ -152,6 +263,12 @@ fn hand_with(ctx: &egui::Context, closed: bool, bitmap: bool) -> CursorIcon { if closed { CursorIcon::Grabbing } else { CursorIcon::Grab } } +/// The bitmap [`hand`] hands Windows, for tests that check the Hand's pointer on every platform. +#[cfg(test)] +pub(crate) fn hand_bitmap(ctx: &egui::Context, closed: bool) -> Option { + rasterize(&Shape::Hand { closed }, native_scale(ctx)) +} + /// The Zoom tool's pointer: a magnifier with a plus, or a minus when it zooms `out`. winit maps /// `ZoomIn` and `ZoomOut` to the plain arrow on Windows (#3735), so there it is a bitmap; macOS and /// the Linux themes already draw magnifiers. @@ -189,7 +306,7 @@ fn show(painter: &Painter, at: Pos2, shape: Shape) -> CursorIcon { } fn cached_image(ctx: &egui::Context, shape: &Shape, scale: f32) -> Option { - let id = egui::Id::new("photocraft-tool-cursor"); + let id = egui::Id::unique("photocraft-tool-cursor"); if let Some(cached) = ctx.data(|d| d.get_temp::(id)) && cached.shape == *shape && cached.scale == scale diff --git a/crates/ui-egui/src/tool_cursor/hand.rs b/crates/ui-egui/src/canvas/tool_cursor/hand.rs similarity index 100% rename from crates/ui-egui/src/tool_cursor/hand.rs rename to crates/ui-egui/src/canvas/tool_cursor/hand.rs diff --git a/crates/ui-egui/src/tool_cursor/tests.rs b/crates/ui-egui/src/canvas/tool_cursor/tests.rs similarity index 82% rename from crates/ui-egui/src/tool_cursor/tests.rs rename to crates/ui-egui/src/canvas/tool_cursor/tests.rs index d68be241f5..c4a9c10364 100644 --- a/crates/ui-egui/src/tool_cursor/tests.rs +++ b/crates/ui-egui/src/canvas/tool_cursor/tests.rs @@ -380,6 +380,80 @@ fn harness() -> Harness<'static, crate::PhotocraftApp> { h } +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn brush_cursor_follows_pen_orientation_6350() { + for control in ["penTilt", "rotation"] { + let mut h = harness(); + h.state_mut() + .run( + "tools.setBrush", + json!({"brush": { + "size": 60, "hardness": 1.0, "roundness": 0.25, + "shapeDynamics": {"enabled": true, "angle": {"control": control}} + }}), + ) + .unwrap(); + h.state_mut().stylus.feed.set(Some(crate::stylus::PenSample { tilt_x: 45.0, ..Default::default() })); + h.run_steps(3); + let initial = h.output().platform_output.cursor_image.clone().expect("initial brush cursor"); + h.state_mut().stylus.feed.set(Some(crate::stylus::PenSample { tilt_y: -45.0, rotation: 90.0, ..Default::default() })); + h.run_steps(3); + let turned = h.output().platform_output.cursor_image.clone().expect("turned brush cursor"); + save_pen_cursor_image(&format!("{control}-initial"), &initial); + save_pen_cursor_image(&format!("{control}-turned"), &turned); + let pen = h.state().stylus.sample().expect("simulated pen reaches the canvas"); + assert_eq!((pen.tilt_y, pen.rotation), (-45.0, 90.0)); + let point = photocraft_paint::StrokePoint { tilt_y: pen.tilt_y, rotation: pen.rotation, ..Default::default() }; + let step = photocraft_paint::dynamics::StepInput { point, direction: 0.0, initial_direction: 0.0, step: 0 }; + let mut dabs = Vec::new(); + photocraft_paint::dynamics::DabBuilder::new(&h.state().session.tools.brush).build(&step, &mut dabs); + assert_eq!(dabs.len(), 1); + assert!((dabs[0].angle.to_degrees() - 90.0).abs() < 0.001, "painted dab turns: {control}"); + assert_ne!(initial.rgba, turned.rgba, "cursor must follow pen orientation: {control}"); + // The dynamically rotated cursor must match the same tip set to that angle directly. + h.state_mut().session.tools.brush.shape_dynamics.enabled = false; + h.state_mut().session.tools.brush.angle = 90.0; + h.run_steps(3); + let reference = h.output().platform_output.cursor_image.clone().expect("static rotated reference"); + assert_eq!(turned.size, reference.size); + assert_eq!(turned.hotspot, reference.hotspot); + assert_eq!(turned.rgba, reference.rgba, "dynamic cursor matches the dab angle: {control}"); + } +} + +#[test] +#[cfg(not(target_arch = "wasm32"))] +fn round_cursor_shows_brush_projection_6350() { + let mut h = harness(); + h.state_mut().session.tools.brush.shape_dynamics.enabled = true; + h.state_mut().session.tools.brush.shape_dynamics.brush_projection = true; + h.state_mut().stylus.feed.set(Some(crate::stylus::PenSample::default())); + h.run_steps(3); + let upright = h.output().platform_output.cursor_image.clone().unwrap(); + h.state_mut().stylus.feed.set(Some(crate::stylus::PenSample { tilt_x: 60.0, ..Default::default() })); + h.run_steps(3); + let tilted = h.output().platform_output.cursor_image.clone().unwrap(); + save_pen_cursor_image("projection-upright", &upright); + save_pen_cursor_image("projection-tilted", &tilted); + assert_ne!(upright.rgba, tilted.rgba, "a projected round brush must not keep a circular cursor"); + h.state_mut().stylus.feed.set(None); + h.run_steps(3); + assert_eq!(upright.rgba, h.output().platform_output.cursor_image.clone().unwrap().rgba, "mouse restores the unprojected tip"); +} + +#[cfg(not(target_arch = "wasm32"))] +fn save_pen_cursor_image(name: &str, cursor: &egui::CustomCursorImage) { + let Some(dir) = std::env::var_os("PHOTOCRAFT_CURSOR_OUTPUT") else { return }; + let dir = std::path::PathBuf::from(dir); + std::fs::create_dir_all(&dir).unwrap(); + let image = + photocraft_codecs::Image::from_u8(u32::from(cursor.size[0]), u32::from(cursor.size[1]), photocraft_codecs::ChannelLayout::Rgba, cursor.rgba.to_vec()) + .unwrap(); + let png = photocraft_codecs::encode(&image, photocraft_codecs::Format::Png, &Default::default()).unwrap(); + std::fs::write(dir.join(format!("{name}.png")), png).unwrap(); +} + #[test] #[cfg(not(target_arch = "wasm32"))] fn brush_hover_uses_an_os_bitmap_that_survives_motion_but_not_tool_or_panel_changes() { @@ -515,3 +589,30 @@ fn resize_follows_the_view() { assert_eq!(got, want, "view turned {rotation}°, flip {flip}"); } } + +#[test] +fn type_path_cursors_are_small_and_centred() { + for rings in [super::area_type_rings(), super::path_type_rings()] { + assert!(rings.len() > 3); + for v in rings.iter().flatten() { + assert!(v.x.abs() <= 12.0 && v.y.abs() <= 12.0, "{v:?}"); + } + } +} + +#[test] +fn the_pointer_belongs_to_another_window_from_its_report_until_this_one_hears_it_again() { + // The main window, then a floating panel's window over it, the desktop, and back. + let mut state = false; + let mut frame = |here, there| { + state = elsewhere(state, here, there); + state + }; + assert!(!frame(true, false), "over the main window"); + assert!(frame(true, true), "crossing into the floating window"); + assert!(frame(false, true), "moving in the floating window"); + assert!(frame(false, false), "on the desktop: the main window still heard nothing"); + assert!(!frame(true, false), "back over the main window"); + let float = Some(egui::Rect::from_min_size(egui::pos2(100.0, 100.0), egui::vec2(200.0, 300.0))); + assert!(covers(float, Some(egui::pos2(150.0, 150.0))) && !covers(float, Some(egui::pos2(98.0, 150.0))), "just outside its edge stays here"); +} diff --git a/crates/ui-egui/src/tool_cursor/zoom.rs b/crates/ui-egui/src/canvas/tool_cursor/zoom.rs similarity index 100% rename from crates/ui-egui/src/tool_cursor/zoom.rs rename to crates/ui-egui/src/canvas/tool_cursor/zoom.rs diff --git a/crates/ui-egui/src/tool_feedback.rs b/crates/ui-egui/src/canvas/tool_feedback.rs similarity index 81% rename from crates/ui-egui/src/tool_feedback.rs rename to crates/ui-egui/src/canvas/tool_feedback.rs index 2e66ae07c1..ce6cbb9e49 100644 --- a/crates/ui-egui/src/tool_feedback.rs +++ b/crates/ui-egui/src/canvas/tool_feedback.rs @@ -168,53 +168,24 @@ pub fn draw_badge(painter: &egui::Painter, p: Pos2, b: Badge, centred: bool) { } } -/// Dash length of the marching ants, in screen points. -pub const DASH: f32 = 4.0; - -/// Marching ants along a polyline: a solid white line under black dashes, the dashes shifted by -/// `phase` (animate it with time). Visible on any image, like Photoshop's selection edges. -pub fn ants(points: &[Pos2], closed: bool, phase: f32) -> Vec { - let Some(&first) = points.first().filter(|_| points.len() >= 2) else { return Vec::new() }; - let mut pts = points.to_vec(); - if closed { - pts.push(first); - } - let mut out = vec![Shape::line(pts.clone(), Stroke::new(1.0, Color32::WHITE))]; - let black = Stroke::new(1.0, Color32::BLACK); - // Arc length so dashes run continuously across vertices. - let mut along = -phase.rem_euclid(DASH * 2.0); - for w in pts.windows(2) { - let (a, b) = (w[0], w[1]); - let len = a.distance(b); - if len < 1e-3 { - continue; - } - let dir = (b - a) / len; - // Position of this segment's start within the dash period. - let mut t = -(along.rem_euclid(DASH * 2.0)); - while t < len { - let (s0, s1) = (t.max(0.0), (t + DASH).min(len)); - if s1 > s0 { - out.push(Shape::line_segment([a + dir * s0, a + dir * s1], black)); - } - t += DASH * 2.0; - } - along += len; - } - out -} - -/// Marching-ants phase for the frame time `time` (s), and keep the animation running. -pub fn ants_phase(ctx: &egui::Context) -> f32 { - let time = ctx.input(|i| i.time); - ctx.request_repaint_after(std::time::Duration::from_millis(100)); - ((time * 10.0) % (DASH as f64 * 2.0)) as f32 -} - -/// Draw animated marching ants along `points`. +/// Draw animated marching ants along `points`: the same ants as a selection's (`crate::ants`), +/// visible on any image, like Photoshop's selection edges. pub fn draw_ants(painter: &egui::Painter, points: &[Pos2], closed: bool) { - let phase = ants_phase(painter.ctx()); - painter.extend(ants(points, closed, phase)); + crate::ants::draw_polyline(painter, points, closed); +} + +/// A selection outline still being drawn (Lasso, Polygonal Lasso, Magnetic Lasso): a solid line, +/// as Photoshop draws it until the selection is made (#7495). Marching ants are for selections +/// that exist. A thin dark line on a soft light edge: Photoshop's look on light pixels, and still +/// visible on dark ones. +pub fn solid_path(points: &[Pos2], closed: bool) -> Vec { + let make = |s: Stroke| if closed { Shape::closed_line(points.to_vec(), s) } else { Shape::line(points.to_vec(), s) }; + vec![make(Stroke::new(3.0, Color32::from_white_alpha(140))), make(Stroke::new(1.0, Color32::BLACK))] +} + +/// Draw [`solid_path`]. +pub fn draw_solid_path(painter: &egui::Painter, points: &[Pos2], closed: bool) { + painter.extend(solid_path(points, closed)); } /// Polygon approximating the ellipse inscribed in `r` (enough vertices to look smooth). @@ -294,24 +265,21 @@ mod tests { assert!(badge_glyph(Badge::Intersect, c, 3.0).iter().all(|[a, b]| a.x != b.x && a.y != b.y)); } + /// #7495: an outline still being drawn is one unbroken line (no dashes, nothing marching), + /// black over a light edge. #[test] - fn ants_use_two_contrasting_colours_and_march() { + fn solid_path_is_one_unbroken_dark_line_on_a_light_edge() { + let pts = [pos2(0.0, 0.0), pos2(40.0, 0.0), pos2(40.0, 30.0)]; + for closed in [false, true] { + let shapes = solid_path(&pts, closed); + assert!(shapes.iter().all(|s| matches!(s, Shape::Path(p) if p.closed == closed && p.points.len() == 3)), "{shapes:?}"); + assert_eq!(strokes(&shapes), vec![Color32::from_white_alpha(140), Color32::BLACK], "the dark line on top"); + } + } + + #[test] + fn shape_previews_contrast_and_ellipses_close() { let pts = [pos2(0.0, 0.0), pos2(40.0, 0.0), pos2(40.0, 30.0)]; - let a = ants(&pts, true, 0.0); - let cols = strokes(&a); - assert!(cols.contains(&Color32::WHITE) && cols.contains(&Color32::BLACK), "{cols:?}"); - // Roughly half the length is black dashes: (40 + 30 + 50) / 8 ≈ 15 dashes. - let dashes = cols.iter().filter(|c| **c == Color32::BLACK).count(); - assert!((13..=18).contains(&dashes), "{dashes}"); - // The phase moves the dashes (the ants march). - let first = |s: &[Shape]| { - s.iter().find_map(|s| match s { - Shape::LineSegment { points, stroke } if stroke.color == Color32::BLACK => Some(points[0]), - _ => None, - }) - }; - assert_ne!(first(&a), first(&ants(&pts, true, 2.0))); - assert!(ants(&pts[..1], false, 0.0).is_empty()); // Shape previews: the accent path over a dark halo. let s = contrast_path(pts.to_vec(), true, Color32::from_rgb(0, 120, 255)); let cols = strokes(&s); diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs new file mode 100644 index 0000000000..f3791f23eb --- /dev/null +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -0,0 +1,675 @@ +use super::cancel_stale_vector_drag; +use super::cursors::{alt_eyedropper, sample_eyedropper}; +use super::drag::Drag; +use super::gesture::{finish_gesture, polygon_click, selection_mode}; +use super::selection::finish_selection_drag; +use super::strokes::{HOLD_REPEAT_MS, begin_live_stroke, feed_live_stroke, paint_target, snap_aliased_tip, stroke_params, strokes_live}; +use super::{PhotocraftApp, Tool, json}; + +/// Abstract tool event, produced by the mouse or by automation (`ui.pointer`). +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ToolEvent { + Down { x: f64, y: f64, pressure: f32 }, + Move { x: f64, y: f64, pressure: f32 }, + Up { x: f64, y: f64 }, +} + +/// The body of a `Move` for every tool: tracked position, ⇧ constraint, the moving layer, and so +/// on. It does not feed the live stroke, so the canvas can push a whole frame's recovered samples +/// and update the live stroke once (see `canvas_view`). +fn tool_move(app: &mut PhotocraftApp, x: f64, y: f64, pressure: f32, mods: egui::Modifiers) { + let tool = app.active_tool(); + if tool.is_type() && app.drag.is_none() { + crate::type_tool::pointer_move(app, x, y, mods); + } + if tool == Tool::Pen && mods.alt { + crate::vector_ui::pen_break_handle(app); + } + if tool == Tool::Pen { + crate::vector_ui::pen_move(app, x, y, mods.shift); + } else if tool == Tool::CurvaturePen { + crate::vector_ui::curvature_move(app, x, y); + } + let zoom = app.point_zoom(); + if let Some(d) = app.drag.as_mut().filter(|d| d.reposition) { + d.track(mods); + d.shift_to([x, y]); + } else if let Some(d) = &mut app.drag { + d.track(mods); + // ⇧: straight 0/45/90° strokes, 45° gradient angles (stroke_constraint.rs). + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let [x, y] = crate::stroke_constraint::constrain(d.tool, &mut d.constrain, d.start, last, [x, y], mods.shift, zoom); + // ⇧: a selection-tool drag of the selection locks to multiples of 45° (#6231). The Move + // tool's own drags were already locked by `move_mods::filter_event`. + let [x, y] = crate::move_mods::selection_drag_to(d, [x, y], mods.shift && tool != Tool::Move); + let moved = d.points.last().is_none_or(|p| (p[0] - x).abs() + (p[1] - y).abs() > 0.25); + // A held pen repeats its point now and then, so the stroke's time keeps advancing. + let held = app.stylus.last_point_ms().is_some_and(|t| app.stylus.clock_ms - t >= HOLD_REPEAT_MS) + && app.live_stroke.as_ref().is_some_and(|l| l.stroke.wants_time()); + if moved || held { + d.points.push([x, y, pressure as f64]); + app.stylus.record_point(); + } + } +} + +fn preflight_tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> Option<(bool, egui::Modifiers)> { + // A press anywhere but on a guide deselects the selected guide (`guide_select`). + if let ToolEvent::Down { x, y, .. } = ev + && app.ui.selected_guide.is_some() + && crate::rulers::guide_at(app, x, y).is_none() + { + crate::guide_select::clear(app); + } + if crate::symmetry_ui::pointer(app, ev, crate::workspace_ui::sticky_mods(app, mods)) { + return None; + } + // An Alt+right-drag armed for this press (`paint_mouse`): taken before anything else can + // consume the event, so it never outlives the press it was armed for (#297). + let armed = std::mem::take(&mut app.brush_resize_armed); + crate::transform_tool::end_if_left(app); + let mods = crate::workspace_ui::sticky_mods(app, mods); + if app.ui.transform.is_none() && crate::type_transform::pointer(app, ev, mods) { + return None; + } + // Content-Aware Move's Transform On Drop box takes every press until ↩ or Esc. + if crate::cam_transform::pointer(app, ev, mods) { + return None; + } + // A Mixer Brush press that loaded paint stays a sample press until it is released, with ⌥ + // let go or not (`mixer_sampling`; `canvas_view` holds back egui's later drag start too). + match ev { + ToolEvent::Down { .. } => app.mixer_sampling = false, + ToolEvent::Up { .. } if app.mixer_sampling => { + app.mixer_sampling = false; + return None; + } + _ if app.mixer_sampling => return None, + _ => {} + } + // The tool in use, so a held temporary tool (⌘ = Move) with ⌥ doesn't load paint. + if app.active_tool() == Tool::MixerBrush && mods.alt && !mods.ctrl && app.drag.is_none() && !armed && app.brush_resize.is_none() { + if let ToolEvent::Down { x, y, pressure } = ev { + app.mixer_sampling = true; + let pen = app.stylus.sample().unwrap_or_default(); + let p = json!({"points":[[x,y,pressure,pen.tilt_x,pen.tilt_y,pen.rotation]],"target":paint_target(app)}); + if let Err(e) = app.run("brush.mixer.sample", p) { + app.ui.status = e; + app.ui.status_error = true; + } + } + return None; + } + Some((armed, mods)) +} + +fn filter_event_before_tool(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers, armed: bool) -> Option<(ToolEvent, ToolEvent, egui::Modifiers)> { + // View › Snap / Snap To and smart guides (snap_ui.rs). + let raw = ev; + // The Move tool's marquee selecting layers takes its moves and release unsnapped (move_box.rs). + if crate::move_box::pointer(app, raw) { + return None; + } + // A press anywhere but on the floating piece (or with ⇧ / ⌥, to draw) drops it first; the + // Move tool (⌘ with a painting tool too, `event_tool`) drags it from anywhere. + if let ToolEvent::Down { x, y, .. } = raw + && app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()) + && !crate::move_ui::moves_selected_pixels_with(app, event_tool(app, mods)) + && selection_drag_kind(app, app.ui.tool, [x, y], mods) != Some(true) + { + let _ = app.run("select.drop", json!({})); + } + let ev = crate::snap_ui::filter_event(app, ev, mods); + // Free Transform picks the handle under the press where the user clicked: the snapped press + // can land outside the corner's grab area and turn a corner drag into a move of the whole box. + let transform_ev = if matches!(raw, ToolEvent::Down { .. }) { raw } else { ev }; + if crate::transform_tool::pointer(app, transform_ev, mods) { + return None; + } + if crate::distort_ui::pointer(app, ev, mods) { + return None; + } + if crate::perspective_crop_ui::pointer(app, ev) { + return None; + } + // Window › Modifier Keys: sticky Shift/⌘/⌥ act as held keys. + let mods = crate::workspace_ui::sticky_mods(app, mods); + // ⌘⌥⌃-click with any tool selects the topmost layer with pixels there (quick_pick.rs); it + // must run before the ⌃⌥ brush resize below, which the same keys would trigger. + if crate::quick_pick::pointer(app, ev, mods) { + return None; + } + // Control+Alt-drag or Alt+right-drag with a painting tool resizes the brush instead of + // painting (#231, #297). + if crate::brush_resize::pointer(app, ev, mods, armed) { + return None; + } + // ⌥ (Alt) with a painting tool is the Eyedropper for that press. + if alt_eyedropper(app, ev, mods) { + return None; + } + // Move tool: ⇧ locks the axis, ⌥ duplicates (move_mods.rs). + let ev = crate::move_mods::filter_event(app, ev, mods); + // A live shape's corner radius widgets (Path Selection, the shape tools, Move with its + // transform controls). Unsnapped, as Free Transform's handles: snapping would pull the press + // off a widget onto a nearby vertex or guide, and the shape tool would draw a new shape. + if crate::corner_radius::pointer(app, raw, mods) { + return None; + } + // Direct Selection, and the Pen's ⌘ (Direct Selection) and ⌥ (Convert Point) modes (#790). + if crate::direct_select::pointer(app, ev, mods) { + return None; + } + // Add / Delete Anchor Point tools (#1044). + if crate::anchor_tools::pointer(app, ev, mods) { + return None; + } + // Ruler, Count and Note tools. + if crate::analysis_ui::pointer(app, ev, mods) { + return None; + } + // Slice and Slice Select tools. + if crate::slice_ui::pointer(app, ev, mods) { + return None; + } + // Frame tool: draw rectangular and elliptical frames. + if crate::frame_tool::pointer(app, ev, mods) { + return None; + } + // Artboard tool: draw, select, move and resize boards. + if crate::artboard_tool::pointer(app, ev, mods) { + return None; + } + // Crop tool: draw, move and resize the frame. + if crate::crop_ui::pointer(app, ev, mods) { + return None; + } + // Gradient tool, live mode: draw and edit Gradient Fill layers. + if crate::gradient_ui::pointer(app, ev, mods) { + return None; + } + // ⌘ over a guide takes the guide (#2690, #6288); the guide drag below moves it. + if let ToolEvent::Down { x, y, .. } = raw + && let Some((vertical, i)) = command_guide_at(app, app.active_tool(), [x, y], mods) + { + let mut guide = crate::rulers::GuideDrag::existing(vertical, i, [x, y]); + guide.track([x, y], mods.alt); + app.guide_drag = Some(guide); + crate::snap_ui::begin_guide(app, [x, y]); + return None; + } + if crate::lasso_ui::pointer(app, ev, mods) { + return None; + } + // Magnetic Lasso: fastening points and the border following the pointer. + if crate::magnetic_lasso_ui::pointer(app, ev, mods) { + return None; + } + Some((raw, ev, mods)) +} + +fn handle_move_pointer(app: &mut PhotocraftApp, ev: ToolEvent, tool: Tool, mods: egui::Modifiers) -> bool { + match ev { + ToolEvent::Down { x, y, pressure } if tool == Tool::Move => { + if let Some((vertical, i)) = crate::rulers::guide_at(app, x, y) { + // ⌥-click turns the guide, through the point clicked, as in Photoshop. + if mods.alt && !mods.shift && !mods.command { + crate::guide_select::flip(app, vertical, i, [x, y]); + return true; + } + let mut guide = crate::rulers::GuideDrag::existing(vertical, i, [x, y]); + guide.track([x, y], mods.alt); + app.guide_drag = Some(guide); + return true; + } + // Auto-Select (or ⌘-click while it is off) picks the layer under the pointer first + // (not when the selected pixels move: those are the active layer's). + if !crate::move_ui::moves_selected_pixels_with(app, tool) && app.ui.tool_options.move_auto_select != mods.command { + let target = app.ui.tool_options.move_target.clone(); + let mode = if mods.shift { "add" } else { "replace" }; + let before = app.session.active().map(|st| st.selected_layers()); + // A locked layer under the pointer (the Background) can't move. Photoshop draws + // the layer marquee instead of reporting the lock (#4058). Probe without + // selecting so a locked hit doesn't become the selection first. + let probe = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "select": false})); + let locked_hit = probe.as_ref().ok().and_then(|v| v["layer"].as_u64()).is_some_and(|id| { + app.session.active().is_some_and(|st| { + let id = photocraft_doc::LayerId(id); + let locks = st.doc.effective_locks(id); + locks.position || locks.all || st.doc.is_background(id) + }) + }); + if locked_hit || (probe.as_ref().is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y])) { + if locked_hit { + // A click still selects the locked layer, as in Photoshop; a drag's + // marquee then replaces that selection on release. + let _ = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); + } + crate::move_box::begin(app, [x, y], mods.shift); + return true; + } + let picked = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode})); + // No pixels there at all (an empty spot, or off the canvas): drag a marquee that + // selects the layers it touches instead (move_box.rs). + if picked.is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y]) { + crate::move_box::begin(app, [x, y], mods.shift); + return true; + } + // Snapping (and the drag's box) started before the pick: point it at what moves. + if app.session.active().map(|st| st.selected_layers()) != before { + crate::snap_ui::retarget_move(app, [x, y], mods); + } + } + // A locked layer: no drag, and Photoshop's message once the pointer moves (`move_lock`). + if crate::move_lock::blocked(app, tool, [x, y], mods) { + app.move_blocked = true; + return true; + } + // With a selection: cut the selected pixels (⌥ copies them) and drag them as a floating + // piece, from anywhere, as a marquee ⌘-drag does. + if crate::move_ui::moves_selected_pixels_with(app, tool) { + if crate::move_ui::float_selected(app, mods.alt, 0.0, 0.0) { + let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false); + d.sel_move = Some(true); + app.drag = Some(d); + } + return true; + } + } + ToolEvent::Move { x, y, .. } => { + if let Some(d) = app.guide_drag.as_mut().filter(|d| d.index.is_some()) { + d.track([x, y], mods.alt); + return true; + } + } + ToolEvent::Up { x, y } => { + if let Some(mut d) = app.guide_drag.filter(|d| d.index.is_some()) { + app.guide_drag = None; + d.track([x, y], mods.alt); + crate::rulers::finish_drag(app, d); + return true; + } + } + _ => {} + } + false +} + +fn consumes_cancelled_vector_event(app: &mut PhotocraftApp, ev: ToolEvent) -> bool { + cancel_stale_vector_drag(app); + if app.cancelled_vector_drag { + match ev { + ToolEvent::Down { .. } => app.cancelled_vector_drag = false, + ToolEvent::Move { .. } => return true, + ToolEvent::Up { .. } => { + app.cancelled_vector_drag = false; + return true; + } + } + } + false +} + +fn handle_eyedropper_event(app: &mut PhotocraftApp, tool: Tool, ev: ToolEvent, mods: egui::Modifiers) -> bool { + if tool != Tool::Eyedropper { + return false; + } + match ev { + ToolEvent::Down { x, y, .. } | ToolEvent::Move { x, y, .. } => sample_eyedropper(app, x, y, mods), + ToolEvent::Up { .. } => {} + } + true +} + +/// Tool state machine. Shared by mouse input and automation. +pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) { + if consumes_cancelled_vector_event(app, ev) { + return; + } + let Some((armed, mods)) = preflight_tool_event(app, ev, mods) else { return }; + let Some((raw, ev, mods)) = filter_event_before_tool(app, ev, mods, armed) else { return }; + let tool = event_tool(app, mods); + if handle_eyedropper_event(app, tool, ev, mods) { + return; + } + // A locked layer (the Background without a selection, say): no drag, and Photoshop's message + // once the pointer moves (a click says nothing). ⌘ with a selection tool here (#896); the Move + // tool below, once Auto-Select has picked the layer. + match ev { + ToolEvent::Down { x, y, .. } if tool != Tool::Move && app.ui.transform.is_none() && crate::move_lock::blocked(app, tool, [x, y], mods) => { + app.move_blocked = true; + return; + } + ToolEvent::Move { .. } if app.move_blocked => { + app.move_blocked = false; + crate::move_lock::prompt(app); + return; + } + ToolEvent::Up { .. } if app.move_blocked => { + app.move_blocked = false; + return; + } + _ => {} + } + if handle_move_pointer(app, ev, tool, mods) { + return; + } + + match ev { + ToolEvent::Down { x, y, pressure } => { + // Painting a type, shape, Smart Object or fill layer asks to rasterize it first + // (⌥-click with the Clone Stamp or Healing Brush only sets the source). + let sets_source = matches!(tool, Tool::CloneStamp | Tool::Healing) && mods.alt; + // Painting on or moving a hidden layer is refused at the press, as in Photoshop (#571). + if !sets_source && let Some(why) = hidden_target(app, tool) { + app.ui.status = why.into(); + app.ui.status_error = true; + return; + } + if !sets_source && crate::rasterize_prompt::intercept(app, tool, x, y, pressure) { + return; + } + // ⌘ with a selection tool is the Move tool for the drag: outside the selection (or + // without one) it moves the whole layer, ⌘⌥ a duplicate of it. It never combines + // selections. + if command_moves_layer(app, tool, [x, y], mods) { + if mods.alt + && let Err(e) = app.run("layer.duplicate", json!({"inPlace": true})) + { + app.ui.status = e; + app.ui.status_error = true; + return; + } + app.drag = Some(Drag::new(Tool::Move, [x, y], vec![[x, y, pressure as f64]], mods, false)); + return; + } + // Marquee / lasso inside the selection: drag the outline, or ⌘-drag to cut the selected + // pixels into a floating piece (`select.float`) and drag that. + if let Some(cut) = selection_drag_kind(app, tool, [x, y], mods) { + // ⌘⌥ on a floating piece leaves it where it is and drags a copy of it. + if cut + && mods.alt + && mods.command + && app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()) + && let Err(e) = app.run("select.drop", json!({})) + { + app.ui.status = e; + app.ui.status_error = true; + return; + } + if cut + && app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_none()) + && let Err(e) = app.run("select.float", json!({"dx": 0, "dy": 0, "copy": mods.alt})) + { + app.ui.status = e; + app.ui.status_error = true; + return; + } + crate::snap_ui::follow_cut(app); + let mut d = Drag::new(tool, [x, y], vec![[x, y, pressure as f64]], mods, false); + d.sel_move = Some(cut); + app.drag = Some(d); + return; + } + match tool { + Tool::PathSelection => { + let tol = 6.0 / f64::from(app.point_zoom().max(0.01)); + if let Some(st) = app.session.active() + && let Some(id) = crate::vector_ui::pick_shape_at(&st.doc, [x, y], tol) + { + let _ = app.run("layer.select", json!({"layer": id.0})); + } + // The component pressed is selected, so the drag moves it (and the others selected). + crate::path_components::press(app, [x, y], mods.shift); + } + Tool::Pen => { + if !(mods.alt && crate::vector_ui::pen_grab_handle(app, x, y)) { + crate::vector_ui::pen_down(app, x, y, mods.shift); + } + return; + } + Tool::CurvaturePen => { + crate::vector_ui::curvature_down(app, x, y); + return; + } + Tool::CloneStamp | Tool::Healing if mods.alt => { + crate::retouch_ui::set_source(app, x, y); + return; + } + Tool::MagicWand => { + let o = app.ui.tool_options.clone(); + let mode = selection_mode(app, mods); + let _ = app.run("select.magicWand", json!({"x": x.floor(), "y": y.floor(), "tolerance": o.tolerance, "contiguous": o.contiguous, "antiAlias": o.anti_alias, "sampleAllLayers": o.sample_all.magic_wand, "mode": mode})); + return; + } + Tool::MagicEraser => { + crate::eraser_ui::click(app, tool, x, y); + return; + } + Tool::PaintBucket => { + let o = app.ui.tool_options.clone(); + let contents = if o.bucket_fill_pattern { "pattern" } else { "foreground" }; + let mode = if o.bucket_clear { "clear" } else { o.bucket_blend_mode.label() }; + let _ = app.run("paint.bucket", json!({"x": x.floor(), "y": y.floor(), "tolerance": o.tolerance, "contiguous": o.contiguous, "antiAlias": o.anti_alias, "sampleAllLayers": o.sample_all.paint_bucket, "opacity": o.fill_opacity, "contents": contents, "mode": mode, "target": paint_target(app)})); + return; + } + Tool::RedEye => { + let o = app.ui.tool_options.clone(); + let _ = app.run( + "paint.redEye", + json!({ + "x": x.floor(), + "y": y.floor(), + "pupilSize": o.red_eye_pupil_size.clamp(1.0, 100.0), + "darken": o.red_eye_darken.clamp(0.0, 100.0), + "target": paint_target(app), + }), + ); + return; + } + Tool::PolygonLasso => { + polygon_click(app, x, y, mods); + return; + } + // The Type tools pick the box handle or glyph under the press where the user + // clicked: the snapped press can land off an edge handle's grab area (#6819). + Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask + if { + let [px, py] = if let ToolEvent::Down { x, y, .. } = raw { [x, y] } else { [x, y] }; + crate::type_tool::pointer_down(app, px, py, mods) + } => + { + return; + } + _ => {} + } + crate::paint_mouse::sync_tool_brush(app); + // Clear mode (`~` held, or chosen in the options bar) erases with the tool's own brush. + let clear = crate::paint_mouse::clears(app, tool); + let erase = std::mem::take(&mut app.secondary_erase) || tool == Tool::Eraser || clear; + // A 1 px pencil line depends on the pixels, not where inside the pixel the click + // landed (#4071). Snap both ends of the stroke to the aliased tip's grid. + let (x, y) = snap_aliased_tip(app, tool, x, y); + // ⇧-click connects to the last endpoint, including its pen pressure (#2827), so + // the renderer interpolates size and opacity across the segment. + let active = app.session.active().map(|st| st.doc.id); + let from = app.last_stroke_end.filter(|(doc, _)| mods.shift && crate::stroke_constraint::connects(tool) && Some(*doc) == active).map(|(_, p)| { + let (sx, sy) = snap_aliased_tip(app, tool, p[0], p[1]); + [sx, sy, p[2]] + }); + let mut points = vec![[x, y, pressure as f64]]; + if let Some(p) = from { + points.insert(0, p); + } + // ⌥ flips Dodge/Burn and Blur/Sharpen for this stroke. + let tool = crate::retouch_ui::alt_flipped(tool, mods.alt); + let mut drag = Drag { clear, ..Drag::new(tool, from.map_or([x, y], |p| [p[0], p[1]]), points, mods, erase) }; + drag.capture_vector_owners(app); + app.drag = Some(drag); + app.trail = None; + app.stylus.begin_stroke(); + if from.is_some() { + app.stylus.record_point(); + } + app.live_stroke = if strokes_live(tool) { begin_live_stroke(app) } else { None }; + if tool == Tool::MixerBrush && app.live_stroke.is_none() { + use photocraft_engine::brush_cmds::mixer_live::LiveMixer; + let p = app.drag.as_ref().map(|d| stroke_params(app, d, &app.stylus.stroke_points(&d.points))); + // A tip above the preview size keeps the drag: `finish_gesture` renders it on release. + // Otherwise the first dab itself is invalid. Do not queue an unexpected release edit. + if let Some(p) = p + && !LiveMixer::defers_to_release(&app.session, &p).is_ok_and(|deferred| deferred) + { + if let Err(e) = LiveMixer::begin(&app.session, &p) { + app.ui.status = e.to_string(); + app.ui.status_error = true; + } + app.drag = None; + } + } else if tool == Tool::MixerBrush { + feed_live_stroke(app); + } + } + ToolEvent::Move { x, y, pressure } => { + // Polygonal Lasso: ⌥ held while the button is down draws freehand into the polygon; + // releasing ⌥ goes back to straight segments (the polygon stays open). + if tool == Tool::PolygonLasso && mods.alt && app.drag.is_none() && !app.ui.polygon.is_empty() { + if app.ui.polygon.last().is_none_or(|l| (l[0] - x).hypot(l[1] - y) >= 1.0) { + app.ui.polygon.push([x, y]); + } + return; + } + tool_move(app, x, y, pressure, mods); + feed_live_stroke(app); + } + ToolEvent::Up { x, y } => { + if tool.is_type() + && let Some(e) = app.ui.text_edit.as_mut() + { + e.dragging = false; + e.resize = None; + app.ui.caret_blink_time = app.last_frame_time; + } + if tool == Tool::Pen { + crate::vector_ui::pen_up(app); + } else if tool == Tool::CurvaturePen { + crate::vector_ui::curvature_up(app); + } + let zoom = app.point_zoom(); + let Some(mut d) = app.drag.take() else { return }; + d.track(mods); + if d.reposition { + d.shift_to([x, y]); + } + let last = d.points.last().map_or(d.start, |p| [p[0], p[1]]); + let [x, y] = crate::stroke_constraint::constrain(d.tool, &mut d.constrain, d.start, last, [x, y], mods.shift, zoom); + let [x, y] = crate::move_mods::selection_drag_to(&mut d, [x, y], mods.shift && app.active_tool() != Tool::Move); + // A Move-tool click (released where it was pressed) selects, it never moves: snapping + // the release point would otherwise nudge the layer onto a nearby edge. + if d.tool == Tool::Move && d.points.len() < 2 && matches!(raw, ToolEvent::Up { x, y } if [x, y] == d.start) { + app.move_preview = None; + crate::move_mods::finish(app); + if d.sel_move.is_some() { + finish_selection_drag(app, true, d.start, d.start); + } + return; + } + if d.points.last().is_none_or(|p| p[0] != x || p[1] != y) { + d.points.push([x, y, d.points.last().map_or(1.0, |p| p[2])]); + app.stylus.record_point(); + } + finish_gesture(app, d); + crate::move_mods::finish(app); + } + } +} + +/// Why a press with `tool` must not start: it would paint on or move a hidden target layer (the +/// engine refuses the command; refusing the press keeps the live stroke from drawing first). +fn hidden_target(app: &PhotocraftApp, tool: Tool) -> Option<&'static str> { + let cmd = match tool { + Tool::Move => "layer.translate", + // The live Gradient makes a new Gradient Fill layer above the target, which Photoshop + // allows over a hidden layer; only the classic one paints the target itself. + Tool::Gradient if !app.ui.tool_options.gradient_classic => return None, + // Every painting command follows the same target (layer, mask or channel) as a stroke. + t if t.is_brushlike() || matches!(t, Tool::Gradient | Tool::PaintBucket | Tool::MagicEraser) => "paint.stroke", + _ => return None, + }; + photocraft_engine::hidden_target::refusal(&app.session, cmd, &json!({})) +} + +/// Is document point `p` inside the active document's selection? +fn inside_selection(app: &PhotocraftApp, p: [f64; 2]) -> bool { + app.session.active().and_then(|st| st.doc.selection.as_ref()).is_some_and(|sel| covers(sel, p)) +} + +/// Does selection surface `sel` cover document point `p`? +fn covers(sel: &photocraft_raster::Surface, p: [f64; 2]) -> bool { + let mut v = [0.0f32]; + sel.read_pixel(p[0].floor() as i32, p[1].floor() as i32, &mut v); + v[0] > 0.0 +} + +/// Does a press with `tool` at `p` drag the selection rather than draw a new one? `Some(true)` +/// moves the floating piece (⌘ cuts one first, ⌘⌥ copies; a floating piece moves with a plain +/// drag; the Polygonal Lasso and Magic Wand need ⌘, as a plain press there places or samples), +/// `Some(false)` just the outline (no ⇧ / ⌥ and the options bar on New Selection, so a combining +/// drag still draws). +pub fn selection_drag_kind(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option { + // Click-driven selection tools (a press places a point or samples): only ⌘ drags the + // selection, and not while a polygon is being drawn. + let clicky = matches!(tool, Tool::PolygonLasso | Tool::MagicWand); + if !(clicky || matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::Lasso)) || (clicky && !app.ui.polygon.is_empty()) { + return None; + } + // ⌘ cuts, ⌘⌥ copies (`select.float`'s `copy`). + let cut = mods.command && !mods.shift; + if let Some(f) = app.session.active().and_then(photocraft_engine::float_cmds::floating) { + // A press on the piece's outline (where its ants are) drags it; one in the margin the cut + // left of the selection starts a new selection (#7538). + let at = [p[0] - f64::from(f.offset.0), p[1] - f64::from(f.offset.1)]; + let on = f.parts.outline().map_or_else(|| inside_selection(app, at), |o| covers(o, at)); + return (on && !mods.shift && (!mods.alt || mods.command) && (cut || !clicky)).then_some(true); + } + if !inside_selection(app, p) { + return None; + } + if cut { + return Some(true); + } + (!clicky && !mods.command && selection_mode(app, mods) == "replace").then_some(false) +} + +/// The tool a pointer event with `mods` goes to: ⌘ held is the Move tool (`hold_keys::cmd_moves`). +/// The canvas resolves the held key before the event (`tool_override`); automation and tests send +/// the modifier with the event. +pub(crate) fn event_tool(app: &PhotocraftApp, mods: egui::Modifiers) -> Tool { + match app.active_tool() { + t if app.tool_override.is_none() && mods.command && crate::hold_keys::cmd_moves(t) && app.ui.transform.is_none() && app.ui.text_edit.is_none() => { + Tool::Move + } + t => t, + } +} + +/// Does a ⌘ (⌘⌥) press with selection tool `tool` at `p` move the whole layer (a duplicate with +/// ⌥), as the Move tool would? Outside the selection or without one; inside it, ⌘ drags the +/// selected pixels instead (`selection_drag_kind`). +pub(crate) fn command_moves_layer(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> bool { + let selection_tool = matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::Lasso | Tool::PolygonLasso | Tool::MagicWand); + let floating = app.session.active().is_some_and(|st| photocraft_engine::float_cmds::floating(st).is_some()); + selection_tool && mods.command && !mods.shift && !floating && app.ui.polygon.is_empty() && !inside_selection(app, p) +} + +/// The guide a ⌘ press with `tool` at `p` drags (vertical?, index), as the Move tool's press +/// would (#2690): ⌘ is the Move tool, and a guide under the pointer comes before the layer or the +/// selected pixels. The selection tools, and every tool on which ⌘ is the Move tool +/// (`hold_keys::cmd_moves`, #6288); the canvas shows the guide cursor where this finds a guide. +/// Not while a polygon or lasso outline, a stroke, Free Transform or type editing is under way. +pub(crate) fn command_guide_at(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option<(bool, usize)> { + let selection_tool = matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::Lasso | Tool::PolygonLasso | Tool::MagicWand); + let moves = selection_tool || crate::hold_keys::cmd_moves(tool); + let idle = app.ui.polygon.is_empty() && !crate::lasso_ui::active(app) && app.ui.transform.is_none() && app.ui.text_edit.is_none() && app.drag.is_none(); + (moves && mods.command && idle).then(|| crate::rulers::guide_at(app, p[0], p[1])).flatten() +} diff --git a/crates/ui-egui/src/canvas/transform_controls.rs b/crates/ui-egui/src/canvas/transform_controls.rs new file mode 100644 index 0000000000..7e00fd8cb3 --- /dev/null +++ b/crates/ui-egui/src/canvas/transform_controls.rs @@ -0,0 +1,140 @@ +use super::selection::floating_outline; +use super::view_xform::ViewXform; +use super::{Color32, LayerContent, PhotocraftApp, Pos2, Rect, Stroke, Tool, Vec2, pos2, vec2}; + +/// View › Show › Layer Edges: the colour of the active layer's outline. +pub(crate) const LAYER_EDGES: Color32 = Color32::from_rgb(0x2d, 0x8c, 0xeb); + +/// View › Show › Layer Edges: the active layer's content bounds (cached per tile) where the canvas +/// shows them. During a Move drag that is at the offset the image was drawn with this frame, so +/// the outline moves in lockstep with the layer's pixels (#2413). +pub(crate) fn layer_edges_rect(app: &PhotocraftApp, idx: usize) -> Option { + if !app.ui.view.shows(app.ui.view.show.layer_edges) { + return None; + } + let st = app.session.documents().get(idx)?; + let id = st.active_layer?; + let b = st.doc.layer(id)?.surface().map(photocraft_compose::bounds::content_bounds)?; + if b.is_empty() { + return None; + } + let (dx, dy) = crate::move_ui::shown_layer_offset(app, st.doc.id, st.revision, id).unwrap_or((0, 0)); + Some(b.translate(dx, dy)) +} + +/// The layers Move › Show Transform Controls frames: every selected layer but the Background. +/// A corner radius drag hides the box, as in Photoshop. So does View › Extras (⌘H, #6699): the +/// controls are an Extra, hidden and inert until Extras comes back, while Show Transform Controls +/// keeps its setting. +pub(super) fn transform_controls_layers(app: &PhotocraftApp) -> Vec<&photocraft_doc::Layer> { + if app.ui.tool != Tool::Move + || !app.ui.tool_options.move_show_transform + || !app.ui.view.extras + || app.ui.transform.is_some() + || app.drag.is_some() + || app.ui.corner_radius.is_some() + { + return Vec::new(); + } + let Some(st) = app.session.active() else { return Vec::new() }; + st.selected_layers().into_iter().filter_map(|id| st.doc.layer(id)).filter(|l| !crate::doc_props_ui::is_background(&st.doc, l)).collect() +} + +/// Move tool › Show Transform Controls: the boxes Free Transform would open on the selected +/// layers, joined into one. +pub(super) fn transform_controls_rect(app: &PhotocraftApp, xf: &ViewXform) -> Option { + let st = app.session.active()?; + let layers = transform_controls_layers(app); + let b = layers.iter().map(|l| photocraft_engine::transform_cmds::transform_frame(&st.doc, l)).filter(|b| !b.is_empty()).reduce(|a, b| a.union(&b))?; + // Floating selected pixels are framed by their outline, as their ants are (#7538), not by the + // selection they were cut with; a frame round other selected layers too keeps them all. + let alone = matches!(layers.as_slice(), [l] if photocraft_engine::float_cmds::floating(st).is_some_and(|f| f.layer == l.id)); + let b = app.session.active_index().filter(|_| alone).and_then(|idx| floating_outline(app, idx)).map_or(b, |o| o.content_bounds()); + let (dx, dy) = crate::move_ui::floating_offset(app).unwrap_or((0, 0)); + let b = b.translate(dx, dy); + if b.is_empty() { + return None; + } + Some(Rect::from_two_pos(xf.to_screen(b.x0 as f32, b.y0 as f32), xf.to_screen(b.x1 as f32, b.y1 as f32))) +} + +/// The Move tool shows transform handles even before a Free Transform session exists. +/// Use the same hit areas for pointer-down and hover, so a control never behaves like +/// resize/rotate while showing the ordinary four-arrow Move cursor (#2871). +pub(super) fn transform_controls_cursor(r: Rect, p: Pos2) -> Option { + let handles = [r.left_top(), r.center_top(), r.right_top(), r.right_center(), r.right_bottom(), r.center_bottom(), r.left_bottom(), r.left_center()]; + let grab = crate::transform_tool::HANDLE_PX as f32; + for (i, handle) in handles.into_iter().enumerate() { + if handle.distance(p) <= grab { + return Some(match i { + 0 | 4 => egui::CursorIcon::ResizeNwSe, + 2 | 6 => egui::CursorIcon::ResizeNeSw, + 1 | 5 => egui::CursorIcon::ResizeVertical, + _ => egui::CursorIcon::ResizeHorizontal, + }); + } + } + // The rotation band: the pointer gives way to the curved rotate arrow (`transform_controls_turn`). + in_rotation_band(r, p).then_some(egui::CursorIcon::None) +} + +fn in_rotation_band(r: Rect, p: Pos2) -> bool { + r.expand(18.0).contains(p) && !r.expand(5.0).contains(p) +} + +/// In the rotation band just outside the Move tool's transform controls, the screen direction the +/// curved rotate arrow bends round: into the box along the nearest corner's bisector, as Free +/// Transform draws it (#4658). Before, the band showed a plain alias pointer, so nothing said a +/// drag there rotates. +pub(super) fn transform_controls_turn(r: Rect, p: Pos2) -> Option { + if !in_rotation_band(r, p) + || [r.left_top(), r.center_top(), r.right_top(), r.right_center(), r.right_bottom(), r.center_bottom(), r.left_bottom(), r.left_center()] + .into_iter() + .any(|h| h.distance(p) <= crate::transform_tool::HANDLE_PX as f32) + { + return None; + } + let corner = pos2(if p.x < r.center().x { r.left() } else { r.right() }, if p.y < r.center().y { r.top() } else { r.bottom() }); + Some(vec2((r.center().x - corner.x).signum(), (r.center().y - corner.y).signum()).normalized() * 16.0) +} + +/// A visible handle starts scaling; the narrow band just outside the box starts rotation. +/// The interior stays the normal Move-tool drag target. +pub(super) fn transform_controls_hit(r: Rect, p: Pos2) -> bool { + transform_controls_cursor(r, p).is_some() +} + +/// Enter the existing Free Transform session when a Move-tool transform control is pressed: on +/// every selected layer, as the frame shows them (#6501). +pub(super) fn begin_transform_controls_at(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXform, p: Pos2) -> bool { + let Some(r) = transform_controls_rect(app, xf) else { return false }; + if !transform_controls_hit(r, p) { + return false; + } + match crate::transform_tool::begin(app, ctx) { + Ok(()) => true, + Err(e) => { + app.ui.status = e; + false + } + } +} + +/// Move tool › Show Transform Controls: the selected layers' bounding box, with its eight handles, +/// and the outline of every selected shape layer, as in Photoshop. +pub(super) fn draw_transform_controls(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) { + let Some(r) = transform_controls_rect(app, xf) else { return }; + let accent = crate::theme::Tokens::get(painter.ctx()).accent; + let to_scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32); + for l in transform_controls_layers(app) { + if let LayerContent::Shape(sh) = &l.content { + crate::vector_ui::draw_outline(painter, &sh.path, &to_scr, accent); + } + } + painter.rect_stroke(r, 0.0, Stroke::new(1.0, accent), egui::StrokeKind::Middle); + for p in [r.left_top(), r.center_top(), r.right_top(), r.right_center(), r.right_bottom(), r.center_bottom(), r.left_bottom(), r.left_center()] { + let h = Rect::from_center_size(p, vec2(7.0, 7.0)); + painter.rect_filled(h, 0.0, Color32::WHITE); + painter.rect_stroke(h, 0.0, Stroke::new(1.0, accent), egui::StrokeKind::Inside); + } +} diff --git a/crates/ui-egui/src/canvas/transform_controls_tests.rs b/crates/ui-egui/src/canvas/transform_controls_tests.rs new file mode 100644 index 0000000000..19fd52f080 --- /dev/null +++ b/crates/ui-egui/src/canvas/transform_controls_tests.rs @@ -0,0 +1,359 @@ +use super::*; +use serde_json::json; + +/// #7538: as in Photoshop, the box frames a loose selection until its pixels are moved, then +/// the pixels themselves, while they float and once dropped. +#[test] +fn transform_controls_frame_the_moved_pixels_not_the_marquee() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200, "background": "transparent"})).unwrap(); + app.session.execute("select.rect", json!({"x": 40, "y": 40, "width": 50, "height": 40})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let frame = |app: &PhotocraftApp| { + let r = transform_controls_rect(app, &xf).unwrap(); + let (lt, rb) = (xf.to_doc(r.left_top()), xf.to_doc(r.right_bottom())); + [lt[0], lt[1], rb[0], rb[1]].map(|v| v.round() as i32) + }; + assert_eq!(frame(&app), [20, 20, 120, 110], "the selection, before anything moves"); + app.run("select.float", json!({"dx": 10, "dy": 5})).unwrap(); + assert_eq!(frame(&app), [50, 45, 100, 85], "the floating pixels"); + app.run("select.drop", json!({})).unwrap(); + assert_eq!(frame(&app), [50, 45, 100, 85], "dropped"); +} + +/// #7538: with several layers selected the Move tool moves them whole, and the selection goes +/// with them, as in Photoshop: its ants follow the drag and it lands with them in one step. +#[test] +fn moving_several_layers_carries_the_selection() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200, "background": "transparent"})).unwrap(); + let a = app.session.active().unwrap().active_layer.unwrap(); + app.session.execute("select.rect", json!({"x": 40, "y": 40, "width": 50, "height": 40})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session.execute("select.rect", json!({"x": 120, "y": 120, "width": 30, "height": 30})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + app.session.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap(); + app.session.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_auto_select = false; + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + let steps = app.session.active().unwrap().history.past_len(); + let m = egui::Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Move { x: 72.0, y: 68.0, pressure: 1.0 }, m); + assert_eq!(selection_shown_offset(&app), Some((12, 8)), "the ants follow the layers"); + tool_event(&mut app, ToolEvent::Up { x: 72.0, y: 68.0 }, m); + let st = app.session.active().unwrap(); + assert!(photocraft_engine::float_cmds::floating(st).is_none(), "whole layers moved, nothing floats"); + assert_eq!(st.doc.selection.as_ref().map(|s| s.content_bounds()), Some(photocraft_geom::Rect::new(32, 28, 132, 118))); + assert_eq!(st.doc.layer(a).unwrap().surface().unwrap().content_bounds(), photocraft_geom::Rect::new(52, 48, 102, 88)); + assert_eq!(st.history.past_len(), steps + 1, "one Move step"); + assert!(app.session.undo()); + let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds()); + assert_eq!(sel, Some(photocraft_geom::Rect::new(20, 20, 120, 110)), "Undo takes the selection back too"); + + // A piece floating (a marquee ⌘-drag with both selected) when the layers are dragged: the + // press drops it first, so the ants follow the drag from where it was dropped. + app.run("select.float", json!({"dx": 10, "dy": 5})).unwrap(); + let outline = photocraft_engine::float_cmds::floating(app.session.active().unwrap()).unwrap().parts.outline().unwrap().content_bounds(); + tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m); + let st = app.session.active().unwrap(); + assert!(photocraft_engine::float_cmds::floating(st).is_none(), "the press dropped the piece"); + assert_eq!(st.doc.selection.as_ref().map(|s| s.content_bounds()), Some(outline.translate(10, 5)), "at its offset"); + tool_event(&mut app, ToolEvent::Move { x: 72.0, y: 68.0, pressure: 1.0 }, m); + assert_eq!(selection_shown_offset(&app), Some((12, 8)), "then the ants follow the drag"); + tool_event(&mut app, ToolEvent::Up { x: 72.0, y: 68.0 }, m); + let sel = app.session.active().unwrap().doc.selection.as_ref().map(|s| s.content_bounds()); + assert_eq!(sel, Some(outline.translate(22, 13)), "released where the ants were"); +} + +/// A transparent 200² document: a red square (40..90, 40..80) on its layer, a loose marquee +/// (20..120, 20..110) round it, cut and floated 10, 5 to the right and down. +fn floating_square() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200, "background": "transparent"})).unwrap(); + app.session.execute("select.rect", json!({"x": 40, "y": 40, "width": 50, "height": 40})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.session.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + app.sync_views(); + app.session.execute("select.float", json!({"dx": 10, "dy": 5})).unwrap(); + app +} + +/// #7538: a selection tool's press on a floating piece's ants drags it; one in the margin its +/// cut left of the loose marquee draws a new selection, as the ants say. +#[test] +fn a_press_in_the_margin_of_a_floating_piece_starts_a_new_selection() { + let app = floating_square(); + let none = egui::Modifiers::NONE; + for tool in [Tool::RectMarquee, Tool::Lasso] { + assert_eq!(selection_drag_kind(&app, tool, [70.0, 60.0], none), Some(true), "{tool:?}: on the square drags it"); + assert_eq!(selection_drag_kind(&app, tool, [33.0, 28.0], none), None, "{tool:?}: in the old marquee's margin"); + } +} + +/// Several selected layers keep one frame round them all while the active one's pixels float: +/// the piece's outline frames it only when it is the only layer framed. +#[test] +fn a_floating_piece_among_several_selected_layers_keeps_the_shared_frame() { + let mut app = floating_square(); + app.session.execute("select.drop", json!({})).unwrap(); + let a = app.session.active().unwrap().active_layer.unwrap(); + app.session.execute("layer.new.layer", json!({})).unwrap(); + app.session.execute("select.rect", json!({"x": 150, "y": 150, "width": 20, "height": 20})).unwrap(); + app.session.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); + app.session.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap(); + app.session.execute("select.rect", json!({"x": 20, "y": 20, "width": 100, "height": 90})).unwrap(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + assert_eq!(transform_controls_layers(&app).len(), 2, "two layers framed"); + app.session.execute("select.float", json!({"dx": 5, "dy": 0})).unwrap(); + assert!(photocraft_engine::float_cmds::floating(app.session.active().unwrap()).is_some()); + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let r = transform_controls_rect(&app, &xf).unwrap(); + let (lt, rb) = (xf.to_doc(r.left_top()), xf.to_doc(r.right_bottom())); + assert!(rb[0] - lt[0] > 60.0 && rb[1] - lt[1] > 60.0, "the frame isn't shrunk to the one piece: {lt:?} {rb:?}"); +} + +#[test] +fn several_selected_layers_share_one_frame_that_transforms_them_all() { + // As in Photoshop: Show Transform Controls frames every selected layer, and its handles + // start one Free Transform of all of them (#6501). + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 40, 40], "fill": "#ff0000"})).unwrap(); + let a = app.session.active().unwrap().active_layer.unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [100, 120, 50, 30], "fill": "#ff0000"})).unwrap(); + let b = app.session.active().unwrap().active_layer.unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let one = transform_controls_rect(&app, &xf).unwrap(); + app.session.execute("layer.select", json!({"layer": a.0, "mode": "add"})).unwrap(); + let both = transform_controls_rect(&app, &xf).unwrap(); + assert!(both.contains_rect(one) && both.width() > one.width() && both.height() > one.height(), "{both:?} frames both"); + let (lt, rb) = (xf.to_doc(both.left_top()), xf.to_doc(both.right_bottom())); + assert!((lt[0] - 20.0).abs() < 1e-3 && (lt[1] - 30.0).abs() < 1e-3 && (rb[0] - 150.0).abs() < 1e-3 && (rb[1] - 150.0).abs() < 1e-3, "{lt:?} {rb:?}"); + // The Background never joins the frame. + let bg = app.session.active().unwrap().doc.layers[0].id; + app.session.execute("layer.select", json!({"layer": bg.0, "mode": "add"})).unwrap(); + assert_eq!(transform_controls_layers(&app).len(), 2); + assert_eq!(transform_controls_rect(&app, &xf), Some(both)); + let ctx = egui::Context::default(); + assert!(begin_transform_controls_at(&mut app, &ctx, &xf, both.right_bottom())); + let t = app.ui.transform.as_ref().expect("the shared frame's handle starts Free Transform"); + assert_eq!(t.rect, [20.0, 30.0, 150.0, 150.0]); + assert_eq!(t.layers, [a.0, b.0]); +} + +#[test] +fn move_transform_controls_start_free_transform_for_vector_shapes() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 80, 40], "fill": "#ff0000"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let r = transform_controls_rect(&app, &xf).expect("shape layers have transform bounds"); + assert!(transform_controls_hit(r, r.right_bottom())); + + let ctx = egui::Context::default(); + assert!(begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom())); + let t = app.ui.transform.as_ref().expect("control press starts Free Transform"); + assert_eq!(t.rect, [20.0, 30.0, 100.0, 70.0]); +} + +#[test] +fn hiding_extras_hides_the_move_transform_controls() { + // #6699: as in Photoshop, View › Extras (⌘H) hides the Move tool's transform box and + // handles; again brings them back. Show Transform Controls and the layer stay as they were. + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 80, 40], "fill": "#ff0000"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let ctx = egui::Context::default(); + let shown = transform_controls_rect(&app, &xf).expect("the box shows with Extras on"); + let layer = app.session.active().unwrap().doc.layers.clone(); + let painted = |app: &mut PhotocraftApp| { + let out = ctx.run_ui(egui::RawInput::default(), |ui| { + let painter = ui.ctx().layer_painter(egui::LayerId::background()); + draw_transform_controls(app, &painter, &xf); + }); + let egui::FullOutput { mut textures_delta, shapes, .. } = out; + textures_delta.clear(); + shapes.len() + }; + // The shape's outline, the box and its eight handles. + assert!(painted(&mut app) > 8, "the box and handles are drawn"); + + crate::menus::invoke(&mut app, &ctx, "view.extras", json!({})).unwrap(); + assert!(!app.ui.view.extras); + assert!(transform_controls_rect(&app, &xf).is_none(), "no box with Extras hidden"); + assert_eq!(painted(&mut app), 0, "nothing is drawn"); + // Hidden handles don't act: a press on a corner doesn't start Free Transform. + assert!(!begin_transform_controls_at(&mut app, &ctx, &xf, shown.right_bottom())); + assert!(app.ui.transform.is_none()); + assert!(app.ui.tool_options.move_show_transform, "Show Transform Controls is unchanged"); + assert_eq!(app.session.active().unwrap().doc.layers, layer, "the layer is unchanged"); + + crate::menus::invoke(&mut app, &ctx, "view.extras", json!({})).unwrap(); + assert!(app.ui.view.extras); + assert_eq!(transform_controls_rect(&app, &xf), Some(shown), "⌘H again restores the box"); +} + +#[test] +fn move_transform_controls_follow_floating_selected_pixels() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + assert!(crate::move_ui::float_selected(&mut app, false, 100.0, 50.0)); + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [200.0, 150.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let r = transform_controls_rect(&app, &xf).unwrap(); + assert_eq!(r, Rect::from_min_max(pos2(200.0, 150.0), pos2(300.0, 210.0))); + let ctx = egui::Context::default(); + assert!(begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom())); + assert_eq!(app.ui.transform.as_ref().unwrap().rect, [200.0, 150.0, 300.0, 210.0]); +} + +/// #5297: with a selection the Move tool's transform controls are the selection's rectangle, +/// though the layer fills only part of it, and a press on them opens that box. +#[test] +fn move_transform_controls_frame_the_selection() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + app.run("select.rect", json!({"x": 60, "y": 80, "width": 100, "height": 120})).unwrap(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [200.0, 150.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let r = transform_controls_rect(&app, &xf).unwrap(); + assert_eq!(r, Rect::from_min_max(pos2(60.0, 80.0), pos2(160.0, 200.0))); + let ctx = egui::Context::default(); + assert!(begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom())); + assert_eq!(app.ui.transform.as_ref().unwrap().rect, [60.0, 80.0, 160.0, 200.0]); +} + +/// #2585: Free Transform on a position-locked layer is refused before the box opens, with +/// Photoshop's alert, from ⌘T and from the Move tool's transform controls alike. It used to +/// open, preview every drag and fail only at commit. +#[test] +fn free_transform_on_a_locked_layer_is_refused_up_front() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 80, 40], "fill": "#ff0000"})).unwrap(); + app.session + .edit("lock", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().locks.position = true; + Ok(()) + }) + .unwrap(); + app.sync_views(); + let errors = |app: &PhotocraftApp| -> Vec { + app.ui + .dialogs + .iter() + .filter(|d| d.kind == crate::state::DialogKind::Error) + .filter_map(|d| d.fields.get("message")?.as_str().map(String::from)) + .collect() + }; + let ctx = egui::Context::default(); + // ⌘T. + let r = crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})); + assert_eq!(r, Err(crate::transform_tool::LOCKED.to_string())); + assert!(app.ui.transform.is_none() && app.transform_preview.is_none(), "no box opened"); + assert_eq!(errors(&app), [crate::transform_tool::LOCKED]); + app.ui.dialogs.clear(); + // The Move tool's transform controls. + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let r = transform_controls_rect(&app, &xf).unwrap(); + assert!(!begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom())); + assert!(app.ui.transform.is_none()); + assert_eq!(errors(&app), [crate::transform_tool::LOCKED]); +} + +#[test] +fn move_transform_controls_show_resize_and_rotate_cursors_before_first_interaction() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.session.execute("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.session.execute("shape.create", json!({"kind": "rect", "rect": [20, 30, 80, 40], "fill": "#ff0000"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Move; + app.ui.tool_options.move_show_transform = true; + let xf = + ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 }; + + let r = transform_controls_rect(&app, &xf).expect("visible shape controls"); + assert!(app.ui.transform.is_none(), "no transform session is needed for hover"); + for (p, expected) in [ + (r.left_top(), egui::CursorIcon::ResizeNwSe), + (r.right_bottom(), egui::CursorIcon::ResizeNwSe), + (r.right_top(), egui::CursorIcon::ResizeNeSw), + (r.left_bottom(), egui::CursorIcon::ResizeNeSw), + (r.center_top(), egui::CursorIcon::ResizeVertical), + (r.center_bottom(), egui::CursorIcon::ResizeVertical), + (r.right_center(), egui::CursorIcon::ResizeHorizontal), + (r.left_center(), egui::CursorIcon::ResizeHorizontal), + // Between handles: the rotation band (a point above the top-centre handle is that handle), + // where the curved rotate arrow replaces the pointer (#4658). + (pos2(r.left() + r.width() * 0.25, r.top() - 12.0), egui::CursorIcon::None), + ] { + assert_eq!(transform_controls_cursor(r, p), Some(expected), "hover at {p:?}"); + assert!(transform_controls_hit(r, p), "same region must start a transform"); + } + assert_eq!(transform_controls_cursor(r, r.center()), None, "interior stays Move"); + assert_eq!(transform_controls_cursor(r, pos2(r.center().x, r.top() - 30.0)), None); + // The rotate arrow bends round the nearest corner, pointing into the box; handles and the + // interior have none. + let into = |p: Pos2| transform_controls_turn(r, p).map(|v| (v.x.signum(), v.y.signum())); + assert_eq!(into(pos2(r.left() + r.width() * 0.25, r.top() - 12.0)), Some((1.0, 1.0)), "top left"); + assert_eq!(into(pos2(r.right() + 12.0, r.bottom() - r.height() * 0.25)), Some((-1.0, -1.0)), "bottom right"); + assert_eq!(into(pos2(r.left() - 12.0, r.bottom() + 12.0)), Some((1.0, -1.0)), "beyond the bottom-left corner"); + assert_eq!(into(r.left_top()), None); + assert_eq!(into(r.center()), None); + + // Changing the active shape changes the hover bounds without requiring a drag first. + app.session.execute("shape.create", json!({"kind": "rect", "rect": [120, 100, 40, 50], "fill": "#00ff00"})).unwrap(); + let next = transform_controls_rect(&app, &xf).expect("new active shape controls"); + assert_ne!(r, next); + assert_eq!(transform_controls_cursor(next, next.right_bottom()), Some(egui::CursorIcon::ResizeNwSe)); + assert_eq!(transform_controls_cursor(next, next.center()), None); +} + +#[test] +fn transform_control_hit_keeps_move_interior_free_and_has_rotation_band() { + let r = Rect::from_min_max(pos2(20.0, 30.0), pos2(100.0, 70.0)); + assert!(transform_controls_hit(r, r.left_top())); + assert!(transform_controls_hit(r, pos2(60.0, 18.0))); + assert!(!transform_controls_hit(r, r.center())); + assert!(!transform_controls_hit(r, pos2(60.0, 4.0))); +} diff --git a/crates/ui-egui/src/canvas/view_xform.rs b/crates/ui-egui/src/canvas/view_xform.rs new file mode 100644 index 0000000000..3a0b42ca4e --- /dev/null +++ b/crates/ui-egui/src/canvas/view_xform.rs @@ -0,0 +1,262 @@ +use super::{DRect, Document, PhotocraftApp, Pos2, Rect, Vec2, View, pos2, vec2}; + +/// Document ↔ screen mapping for a canvas rect and a view. +#[derive(Clone, Copy, Debug)] +pub struct ViewXform { + pub rect: Rect, + pub zoom: f32, + pub center: [f32; 2], + /// View › Flip Horizontal: the view is mirrored left-right (the document is not). + pub flip: bool, + /// Camera rotation in degrees around [`Self::center`]. Clockwise (Y-down) is positive; applied + /// before flip. + pub rotation: f32, + /// View › Pixel Aspect Ratio Correction: a document pixel is drawn this many times wider than + /// tall (1 = square). Applied in document space, before rotation, so a rotated view still + /// shows the stretched image turned as a whole. + pub aspect: f32, +} + +impl ViewXform { + /// The main canvas's mapping for the active document as last laid out (inside the rulers). + pub fn active(app: &PhotocraftApp) -> Option { + let v = app.ui.views.get(app.session.active_index()?)?; + let (zoom, center) = + if v.zoom_animating && app.session.prefs().general.animated_zoom { v.display_camera.unwrap_or((v.zoom, v.center)) } else { (v.zoom, v.center) }; + Some( + Self { + rect: crate::rulers::content_rect(app, app.last_canvas_rect), + zoom: zoom / app.canvas_ppp(), + center, + flip: app.ui.view.flip_horizontal, + rotation: v.rotation, + aspect: app.ui.view.display_aspect(), + } + .pixel_aligned_for(app, app.canvas_ppp()), + ) + } + + pub(super) fn pixel_aligned_for(self, app: &PhotocraftApp, ppp: f32) -> Self { + // Default Crop counter-moves the frame and camera with fractional pointer deltas. + // Preserve that continuous mapping so its fixed frame does not wobble during a drag. + if crate::crop_mode::moves_image(app) { self } else { self.pixel_aligned(ppp) } + } + + /// At integer physical magnifications, texel edges must land on display pixel edges. + /// Keep this derived camera shared by images, overlays and input without changing the saved pan. + pub(super) fn pixel_aligned(mut self, ppp: f32) -> Self { + if !ppp.is_finite() || ppp <= 0.0 || !self.zoom.is_finite() || self.zoom <= 0.0 || !self.rotation.is_finite() || self.rotation.abs() >= 1e-8 { + return self; + } + let scale = vec2(self.zoom_x(), self.zoom) * ppp; + if !scale.is_finite() + || scale.x.round() < 1.0 + || scale.y.round() < 1.0 + || (scale.x - scale.x.round()).abs() > 1e-5 + || (scale.y - scale.y.round()).abs() > 1e-5 + { + return self; + } + let origin = self.to_screen(0.0, 0.0); + let physical = origin * ppp; + if !physical.is_finite() { + return self; + } + let aligned = pos2(physical.x.round(), physical.y.round()) / ppp; + let offset = self.unmap_vec((aligned - origin) / self.zoom); + let center = [self.center[0] - offset.x, self.center[1] - offset.y]; + if center.iter().all(|v| v.is_finite()) { + self.center = center; + } + self + } + + fn sincos(self) -> (f32, f32) { + if self.rotation.abs() < 1e-8 { + (1.0, 0.0) + } else { + let r = self.rotation.to_radians(); + (r.cos(), r.sin()) + } + } + + /// Document pixel width over height on screen: [`Self::aspect`], or 1 when it is unusable. + fn sx(self) -> f32 { + if self.aspect.is_finite() && self.aspect > 0.0 { self.aspect } else { 1.0 } + } + + /// Screen points per document pixel along the document's x axis. + pub fn zoom_x(&self) -> f32 { + self.zoom * self.sx() + } + + /// Unflip, unrotate and unstretch a screen-space vector (no zoom). Hand pan and zoom-about + /// use this. + pub fn unmap_vec(self, d: Vec2) -> Vec2 { + let d = if self.flip { vec2(-d.x, d.y) } else { d }; + let (c, s) = self.sincos(); + vec2((d.x * c + d.y * s) / self.sx(), -d.x * s + d.y * c) + } + + /// A document-space vector on screen: stretched, rotated, flipped and zoomed as the view + /// draws it (the inverse of [`Self::unmap_vec`], with the zoom). + pub fn map_vec(self, d: Vec2) -> Vec2 { + let (c, s) = self.sincos(); + let dx = d.x * self.sx(); + let v = vec2(dx * c - d.y * s, dx * s + d.y * c) * self.zoom; + if self.flip { vec2(-v.x, v.y) } else { v } + } + + pub fn to_screen(&self, x: f32, y: f32) -> Pos2 { + let (c, s) = self.sincos(); + let dx = (x - self.center[0]) * self.sx(); + let dy = y - self.center[1]; + let rx = dx * c - dy * s; + let ry = dx * s + dy * c; + let sx = if self.flip { -1.0 } else { 1.0 }; + self.rect.center() + vec2(rx * self.zoom * sx, ry * self.zoom) + } + pub fn to_doc(&self, p: Pos2) -> [f64; 2] { + let d = (p - self.rect.center()) / self.zoom; + let u = self.unmap_vec(d); + [(u.x + self.center[0]) as f64, (u.y + self.center[1]) as f64] + } + /// On-screen bounds of the image, including camera rotation and pixel aspect correction. + pub fn image_bounds(self, size: [u32; 2]) -> Rect { + let (c, s) = self.sincos(); + let w = size[0] as f32 * self.sx() * self.zoom; + let h = size[1] as f32 * self.zoom; + let center = self.to_screen(size[0] as f32 / 2.0, size[1] as f32 / 2.0); + Rect::from_center_size(center, vec2(c.abs() * w + s.abs() * h, s.abs() * w + c.abs() * h)) + } + pub fn doc_rect(&self, r: DRect) -> Rect { + let pts = [ + self.to_screen(r.x0 as f32, r.y0 as f32), + self.to_screen(r.x1 as f32, r.y0 as f32), + self.to_screen(r.x1 as f32, r.y1 as f32), + self.to_screen(r.x0 as f32, r.y1 as f32), + ]; + let mut min = pts[0]; + let mut max = pts[0]; + for p in pts { + min = min.min(p); + max = max.max(p); + } + Rect::from_min_max(min, max) + } +} + +/// Document point under a screen position on the main canvas, or `None` when the position is +/// outside it: the rulers are not part of the canvas, so drops on the ruler strip are not +/// canvas drops. Preset-panel drops and drag-and-drop placement share this so both land exactly +/// where the canvas draws. +pub fn doc_point_at(app: &PhotocraftApp, pos: Pos2) -> Option<[f64; 2]> { + let xf = ViewXform::active(app)?; + xf.rect.contains(pos).then(|| xf.to_doc(pos)) +} + +/// Fit the document into `area` (egui points) for a display with `ppp` physical pixels per +/// point; `View::zoom` is stored in physical pixels per document pixel. `aspect` is the +/// displayed pixel aspect ratio ([`ViewXform::aspect`]): the image is that much wider on screen. +pub fn fit_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32, aspect: f32) { + let (w, h) = (doc.size.width as f32, doc.size.height as f32); + let shown_w = w * if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 }; + // Fit never magnifies past 100%: one image pixel per *physical* pixel, so the cap applies + // after the points → device pixels conversion (a 150% display would otherwise open at 150%). + let fit = ((area.x - 40.0) / shown_w).min((area.y - 40.0) / h) * ppp; + let zoom = crate::zoom_levels::clamp(fit.min(1.0), [doc.size.width, doc.size.height]); + view.zoom = zoom; + view.center = [w / 2.0, h / 2.0]; + view.fit_pending = false; + view.fill_pending = false; +} + +/// Ease a transient presentation camera over 220 ms without changing the requested camera. +pub(super) fn animated_view(view: &mut View, xf: ViewXform, ppp: f32, animated_zoom: bool, time: f64) -> View { + if !animated_zoom { + view.request_zoom_animation(false); + } + let target = (view.zoom, view.center); + // Clamping, a canvas resize, or another camera command can make the original anchor + // incompatible with the requested camera. The target must always win. + if let Some((doc, screen)) = view.zoom_anchor { + let offset = xf.unmap_vec((pos2(screen[0], screen[1]) - xf.rect.center()) / (view.zoom / ppp)); + if (doc[0] as f32 - offset.x - view.center[0]).abs() > 0.01 || (doc[1] as f32 - offset.y - view.center[1]).abs() > 0.01 { + view.zoom_anchor = None; + } + } + let mut shown = view.clone(); + if view.zoom_animating { + if view.zoom_transition.is_none_or(|a| a.target != target) { + view.zoom_transition = Some(crate::state::ZoomTransition { from: view.display_camera.unwrap_or(target), target, started: time }); + } + if let Some(a) = view.zoom_transition { + let elapsed = (time - a.started) / 0.22; + let t = if elapsed.is_finite() { elapsed.clamp(0.0, 1.0) as f32 } else { 1.0 }; + let ease = t * t * (3.0 - 2.0 * t); + let lerp = |from: f32, to: f32| from + (to - from) * ease; + shown.zoom = lerp(a.from.0, target.0); + shown.center = [lerp(a.from.1[0], target.1[0]), lerp(a.from.1[1], target.1[1])]; + if let Some((doc, screen)) = view.zoom_anchor { + let zoom = shown.zoom / ppp; + if zoom.is_finite() && zoom > 0.0 { + let offset = xf.unmap_vec((pos2(screen[0], screen[1]) - xf.rect.center()) / zoom); + shown.center = [doc[0] as f32 - offset.x, doc[1] as f32 - offset.y]; + } + } + if t >= 1.0 { + view.zoom_animating = false; + } + } + } + if !view.zoom_animating { + shown.zoom = target.0; + shown.center = target.1; + view.zoom_anchor = None; + view.zoom_transition = None; + } + view.display_camera = Some((shown.zoom, shown.center)); + shown +} + +/// Centre the document and zoom until it fills the canvas. One axis can extend beyond the +/// viewport, matching the Hand tool's Fill Screen action. +pub fn fill_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32, aspect: f32) { + let (w, h) = (doc.size.width as f32, doc.size.height as f32); + let shown_w = w * if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 }; + let points = (area.x / shown_w).max(area.y / h); + let zoom = crate::zoom_levels::clamp(points * ppp, [doc.size.width, doc.size.height]); + view.zoom = zoom; + view.center = [w / 2.0, h / 2.0]; + view.fit_pending = false; + view.fill_pending = false; +} + +/// Where a wheel or pinch zoom anchors for a pointer at `p`: the pointer itself over the image, +/// otherwise the nearest point of the image (#3720). Anchoring on a pointer out on the gray +/// pasteboard slid the image away from it, out of the window after a few notches; Photoshop keeps +/// the image in view. Clamped in document space, so a rotated or flipped view behaves the same. +pub(super) fn wheel_anchor(xf: &ViewXform, p: Pos2, doc_size: [u32; 2]) -> Pos2 { + let [w, h] = doc_size; + let d = xf.to_doc(p); + if w == 0 || h == 0 || !(d[0].is_finite() && d[1].is_finite()) { + return p; + } + let c = [d[0].clamp(0.0, f64::from(w)), d[1].clamp(0.0, f64::from(h))]; + if c == d { p } else { xf.to_screen(c[0] as f32, c[1] as f32) } +} + +/// Zoom to `new_zoom` about `p`: keep its document point under the pointer, or move it to the +/// view centre when Zoom Clicked Point to Center (`center_on_point`) is enabled. +pub(super) fn zoom_about(view: &mut View, xf: &ViewXform, p: Pos2, new_zoom: f32, center_on_point: bool, ppp: f32) { + let before = xf.to_doc(p); + view.zoom = new_zoom; + if center_on_point { + view.center = [before[0] as f32, before[1] as f32]; + return; + } + // `new_zoom` is device px per document pixel; the pointer vector is in egui points. + let ppp = if ppp.is_finite() && ppp > 0.0 { ppp } else { 1.0 }; + let u = xf.unmap_vec((p - xf.rect.center()) / (new_zoom / ppp)); + view.center = [before[0] as f32 - u.x, before[1] as f32 - u.y]; +} diff --git a/crates/ui-egui/src/canvas/wheel_nav.rs b/crates/ui-egui/src/canvas/wheel_nav.rs new file mode 100644 index 0000000000..0d190a0633 --- /dev/null +++ b/crates/ui-egui/src/canvas/wheel_nav.rs @@ -0,0 +1,542 @@ +//! Mouse wheel and trackpad navigation over the canvas (#293, #635), as in Photoshop: +//! +//! - The wheel scrolls up and down; ⇧ or ⌘/Ctrl + wheel scrolls sideways. +//! - ⌥/Alt + wheel zooms around the pointer, 10% per wheel notch (eased or instant, see below). +//! ⇧/Shift + ⌥/Alt + wheel snaps to the next zoom ladder level instead. +//! - Preferences › General › Zoom with Scroll Wheel swaps the two: the wheel zooms and +//! ⌥/Alt + wheel scrolls; ⇧/Shift + wheel snaps to the next zoom ladder level. +//! - A trackpad pinch zooms around the pointer, unless Preferences › Enhanced Controls › +//! Zoom with Trackpad Pinch is off (then a pinch does nothing, and Windows' Ctrl + wheel +//! fractions scroll sideways like the wheel, because the fold below is skipped). +//! - A two-finger pinch on a touch screen zooms the same way, unless Preferences › Enhanced +//! Controls › Enable Gestures is off (#204): then the fingers leave the view alone, and a +//! trackpad pinch still zooms ([`pinch_factor`]). +//! +//! [`configure`] makes egui fold ⌘/Ctrl + wheel into a sideways scroll as it does ⇧ + wheel, so +//! only a real pinch (egui's `Event::Zoom`, or a touch screen) reaches `zoom_delta`. macOS and +//! browsers deliver a pinch as its own event, but Windows hands a precision touchpad pinch to apps +//! as Ctrl + wheel in fractions of a notch (also on some Linux Wayland systems): +//! [`fold_legacy_pinch`] turns those back into a pinch +//! before egui sees them, so Ctrl + a notched mouse wheel scrolls sideways and the pinch still +//! zooms. +//! +//! Either way a wheel notch zooms by exactly ×1.1 in total. How that step is spread over frames +//! is set by Preferences › General › Animated Zoom (`general.animatedZoom`, on by default): +//! +//! - On: the zoom is eased over several frames (the curve of egui's wheel smoothing, fed by the zoom +//! notches only), so the factors of the frames of one notch multiply to ×1.1. An unfinished notch +//! is superseded, not carried, when ⇧ snap zoom or a pan takes over or the pointer leaves. +//! - Off: one instant ×1.1 step per notch, applied the frame it arrives with no easing in between +//! (the Photoshop behaviour, measured frame by frame on a screen recording). Zooming reads the +//! wheel events themselves and ignores egui's smoothed tail. +//! +//! In both modes panning keeps the smoothed delta, and ⇧/Shift snap zoom reads the wheel events +//! and stays instant. The Alt state is taken from the wheel events (and the Alt key's own press +//! and release, `wheel_events`) and kept until the next wheel event: releasing Alt while the tail +//! is still settling must not turn it into a pan. + +use egui::{Context, Event, Id, InputState, Key, Modifiers, MouseWheelUnit, RawInput, TouchPhase, Vec2}; + +/// Zoom factor of one wheel notch: every step is exactly ×1.1. +pub const NOTCH: f32 = 1.1; + +/// A Ctrl + wheel event this soon (seconds) after a pinch step still belongs to the pinch, even +/// when it happens to be a whole notch. +const PINCH_GAP: f64 = 0.25; + +/// What the wheel does to the view this frame. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Wheel { + /// Multiply the zoom by this factor around the pointer. + Zoom(f32), + /// Step N rungs on the zoom ladder (positive = in, negative = out). + SnapZoom(i32), + /// Move the image by this many screen points (egui's content direction). + Pan(Vec2), +} + +/// Inputs of one frame's wheel gesture. +#[derive(Clone, Copy, Debug)] +pub struct Input { + /// Smoothed scroll delta (points, content direction); what panning uses. In the animated zoom + /// modes it is the eased zoom delta of that mode's own wheel events only. + pub scroll: Vec2, + /// This frame's wheel events for the gesture in progress, unsmoothed (points, content + /// direction); what the instant zoom and the snap zoom use. + pub raw: Vec2, + /// egui's pinch zoom factor (1 = none). + pub zoom_delta: f32, + /// ⌥/Alt was held for the wheel gesture in progress. + pub alt: bool, + /// ⇧/Shift was held for the wheel gesture in progress (snap zoom). + pub shift: bool, + /// Preferences › General › Zoom with Scroll Wheel. + pub zoom_with_wheel: bool, + /// Preferences › Enhanced Controls › Zoom with Trackpad Pinch. + pub pinch_zooms: bool, + /// Preferences › General › Animated Zoom: the wheel zoom follows the smoothed delta (`scroll`) + /// across frames; off, it is one instant step from this frame's wheel events (`raw`). + pub animated_zoom: bool, + /// Points per wheel notch (egui's `line_scroll_speed`). + pub notch: f32, +} + +/// The view change for one frame of wheel input, if any. +pub fn classify(i: Input) -> Option { + if i.pinch_zooms && i.zoom_delta.is_finite() && i.zoom_delta > 0.0 && i.zoom_delta != 1.0 { + return Some(Wheel::Zoom(i.zoom_delta)); + } + // One notch (`notch` points) is one ×1.1 step. + let step = |dy: f32| { + let notch = if i.notch.is_finite() && i.notch > 0.0 { i.notch } else { 40.0 }; + let f = NOTCH.powf(dy / notch); + (f.is_finite() && f > 0.0 && f != 1.0).then_some(Wheel::Zoom(f)) + }; + let snap = |dy: f32| { + if dy == 0.0 { + return None; + } + let notch = if i.notch.is_finite() && i.notch > 0.0 { i.notch } else { 40.0 }; + let n = dy / notch; + if !n.is_finite() { + return None; + } + let steps = n.clamp(-10.0, 10.0).round() as i32; + if steps == 0 { None } else { Some(Wheel::SnapZoom(steps)) } + }; + let scroll_finite = i.scroll.x.is_finite() && i.scroll.y.is_finite(); + match (i.alt, i.zoom_with_wheel) { + // ⇧ + ⌥ + scroll: snap to the zoom ladder instead of continuous ×1.1. + (true, false) if i.shift => snap(i.raw.y + i.raw.x), + // ⌥ + scroll: continuous ×1.1 zoom. Animated Zoom on: follow the smoothed delta across frames. + (true, false) if i.animated_zoom && scroll_finite => step(i.scroll.y + i.scroll.x), + // Animated Zoom off: one instant step from the wheel events. The smoothed tail is ignored. + (true, false) if !i.animated_zoom => step(i.raw.y + i.raw.x), + // ⇧ + scroll wheel with the preference on: snap to the zoom ladder. + // Some systems deliver Shift+wheel as horizontal (delta in x, y = 0). + (false, true) if i.shift => snap(i.raw.y + i.raw.x), + // ⌘/Ctrl folds the wheel into x: those still scroll sideways. + (false, true) if i.animated_zoom && scroll_finite && i.scroll.y != 0.0 => step(i.scroll.y), + (false, true) if !i.animated_zoom && (i.raw.y != 0.0 || i.scroll.y != 0.0) => step(i.raw.y), + _ if !scroll_finite || i.scroll == Vec2::ZERO => None, + _ => Some(Wheel::Pan(i.scroll)), + } +} + +/// Set egui's wheel modifiers (once, at setup): ⌘/Ctrl + wheel scrolls sideways like ⇧ + wheel +/// instead of zooming, everywhere in the app. +pub fn configure(ctx: &Context) { + ctx.options_mut(|o| { + o.input_options.zoom_modifier = Modifiers::NONE; + o.input_options.horizontal_scroll_modifier = Modifiers::SHIFT | Modifiers::COMMAND; + }); +} + +/// Turn a Windows or Linux touchpad pinch, which arrives as Ctrl + wheel in fractions of a notch or points, into +/// pinch events (`Event::Zoom`) at egui's Ctrl-scroll zoom speed (`notch` points per line, +/// `speed` per point). Ctrl + wheel in whole notches (a mouse wheel) is left to scroll sideways, +/// unless it comes within [`PINCH_GAP`] of a pinch step. `last` is the time of the previous pinch +/// step; returns the time of the latest one. +pub fn legacy_pinch_to_zoom(events: &mut [Event], now: f64, last: Option, notch: f32, speed: f32) -> Option { + let ctrl_wheel = |e: &Event| match e { + Event::MouseWheel { unit: MouseWheelUnit::Line, delta, modifiers, .. } if modifiers.ctrl => Some((*delta, false)), + // Wayland precision touchpads can use point units. These are already actual + // screen points, not line notches; normalize before the exponential below. + Event::MouseWheel { unit: MouseWheelUnit::Point, delta, modifiers, .. } if modifiers.ctrl => Some((*delta / notch.max(1.0), true)), + _ => None, + }; + let fraction = |d: Vec2| d.x.fract() != 0.0 || d.y.fract() != 0.0; + let pinching = + last.is_some_and(|t| (0.0..PINCH_GAP).contains(&(now - t))) || events.iter().filter_map(ctrl_wheel).any(|(d, high_res)| high_res || fraction(d)); + if !pinching { + return last; + } + let mut latest = last; + for e in events.iter_mut() { + if let Some((d, _)) = ctrl_wheel(e) { + let f = (speed * notch * (d.x + d.y)).exp(); + *e = Event::Zoom(if f.is_finite() && f > 0.0 { f } else { 1.0 }); + latest = Some(now); + } + } + latest +} + +fn pinch_id() -> Id { + Id::unique("pc-wheel-legacy-pinch") +} + +/// [`legacy_pinch_to_zoom`] for one frame's raw input (`raw_input_hook`, Windows and Linux). +pub fn fold_legacy_pinch(ctx: &Context, raw: &mut RawInput) { + // Native gesture events already carry their own magnification. Some backends + // also emit a Ctrl-wheel event in the same frame: folding that wheel event + // would apply the same pinch twice. End any legacy continuation as well, + // so a later real Ctrl+wheel notch isn't treated as part of that pinch. + if raw.events.iter().any(|event| matches!(event, Event::Zoom(_))) { + ctx.data_mut(|d| d.remove::(pinch_id())); + return; + } + let (notch, speed) = ctx.options(|o| (o.input_options.line_scroll_speed, o.input_options.scroll_zoom_speed)); + let now = raw.time.unwrap_or_else(|| ctx.input(|i| i.time)); + let last = ctx.data(|d| d.get_temp::(pinch_id())); + let latest = legacy_pinch_to_zoom(&mut raw.events, now, last, notch, speed); + if latest != last + && let Some(t) = latest + { + ctx.data_mut(|d| d.insert_temp(pinch_id(), t)); + } +} + +fn alt_id() -> Id { + Id::unique("pc-wheel-alt") +} + +fn shift_id() -> Id { + Id::unique("pc-wheel-shift") +} + +fn snap_residual_id(owner: Id) -> Id { + Id::unique("pc-wheel-snap-residual").with(owner) +} + +fn snap_time_id(owner: Id) -> Id { + Id::unique("pc-wheel-snap-time").with(owner) +} + +fn snap_frame_result_id(owner: Id) -> Id { + Id::unique("pc-wheel-snap-frame-result").with(owner) +} + +fn active_snap_owner_id() -> Id { + Id::unique("pc-wheel-active-snap-owner") +} + +const SNAP_IDLE_TIMEOUT: f64 = 0.5; + +/// egui's own wheel smoothing (`InputState::begin_pass`): 90 % of a notch is released in 0.1 s. +/// The eased zoom uses the same curve, so it paces like the eased pan. +const EASE_REACH: f32 = 0.90; +const EASE_REACH_SECS: f32 = 0.1; + +/// An unfinished eased notch belongs to the frames that follow it. It is dropped, never carried +/// into a later notch, once its owner was not driven for more than this many passes (the pointer +/// left the viewport, or another gesture took over there)... +const ZOOM_EASE_MAX_GAP_PASSES: u64 = 3; +/// ...or, when at least one pass was skipped, for this long (seconds). A single slow frame is +/// not a gap: the owner was driven in the pass right before it, so the notch still finishes. +const ZOOM_EASE_STALE_SECS: f64 = 0.5; + +#[derive(Clone, Copy)] +struct ZoomEase { + residual: f32, + time: f64, + frame: u64, + released: f32, +} + +fn zoom_ease_id(owner: Id) -> Id { + Id::unique(("wheel-zoom-ease", owner)) +} + +/// Eases `delta` (points of this viewport's zoom-mode wheel events) over the next frames and +/// returns what is released this frame. Same curve as egui's wheel smoothing, but fed by the +/// zoom notches only, so the tail of a pan notch never leaks in. What is released adds up to +/// exactly what was fed. A second call in the same frame returns the cached result. +fn ease_zoom_delta(ctx: &Context, owner: Id, delta: f32) -> f32 { + let id = zoom_ease_id(owner); + let frame = ctx.cumulative_pass_nr(); + let (now, dt) = ctx.input(|i| (i.time, i.stable_dt.min(0.1))); + let prev = ctx.data(|d| d.get_temp::(id)); + if let Some(e) = prev.filter(|e| e.frame == frame) { + return e.released; + } + // Fresh: driven in the previous pass (a slow frame, however long, keeps its notch), or within a + // few passes and a short time (a one-pass hover flicker). Anything older is stale. + let fresh = |e: &ZoomEase| { + let gap = frame.saturating_sub(e.frame); + e.residual.is_finite() && (gap <= 1 || (gap <= ZOOM_EASE_MAX_GAP_PASSES && now - e.time < ZOOM_EASE_STALE_SECS)) + }; + let carried = prev.filter(fresh).map_or(0.0, |e| e.residual); + let mut residual = carried + if delta.is_finite() { delta } else { 0.0 }; + let t = egui::emath::exponential_smooth_factor(EASE_REACH, EASE_REACH_SECS, dt); + let released = if residual.abs() < 1.0 { residual } else { t * residual }; + residual -= released; + ctx.data_mut(|d| d.insert_temp(id, ZoomEase { residual, time: now, frame, released })); + // Callers do not repaint on their own after consuming the zoom: keep frames coming until the + // notch has been fully released, or a reactive UI would stop below ×1.1. + if residual != 0.0 { + ctx.request_repaint(); + } + released +} + +/// A zoom limit stopped the eased zoom of `owner`: drop what is left of the notch. It would no +/// longer zoom, only cancel the first notch back (flick to 12800 %, and the notch back did +/// nothing). This frame's release stays cached, so a second read in the same frame is unchanged. +pub fn settle(ctx: &Context, owner: Id) { + let id = zoom_ease_id(owner); + ctx.data_mut(|d| { + if let Some(e) = d.get_temp::(id) { + d.insert_temp(id, ZoomEase { residual: 0.0, ..e }); + } + }); +} + +/// Whether a trackpad touch is in progress (egui's `InTouch`), at the start and at the end of a +/// frame: the start is kept, so every read in one frame sees the same states. +#[derive(Clone, Copy)] +struct Touch { + frame: u64, + at_start: bool, + at_end: bool, +} + +/// Per viewport, as egui keeps its wheel state per viewport. +fn touch_id(ctx: &Context) -> Id { + Id::unique(("wheel-nav-touch", ctx.viewport_id())) +} + +/// This frame's wheel events (in order) with, for each, whether egui leaves it unsmoothed: during a +/// trackpad touch, or a point delta under 8 points (`WheelState::on_wheel_event`). Those are +/// already smooth, and easing them again would only add latency; the rest (notches, and the +/// big pixel steps browsers report for a mouse wheel) is what Animated Zoom eases. +fn mark_unsmoothed(ctx: &Context, events: Vec<(MouseWheelUnit, Vec2, Modifiers, TouchPhase)>) -> Vec<(MouseWheelUnit, Vec2, Modifiers, bool)> { + let frame = ctx.cumulative_pass_nr(); + let (horizontal, vertical) = ctx.options(|o| (o.input_options.horizontal_scroll_modifier, o.input_options.vertical_scroll_modifier)); + // Not read in the previous pass (no canvas then): a touch could have ended unseen, and a + // stuck one would leave every later notch un-eased. + let id = touch_id(ctx); + let mut touch = match ctx.data(|d| d.get_temp::(id)) { + Some(t) if t.frame == frame => t.at_start, + Some(t) if t.frame.saturating_add(1) == frame => t.at_end, + _ => false, + }; + let at_start = touch; + let mut wheel = Vec::with_capacity(events.len()); + for (unit, delta, modifiers, phase) in events { + match phase { + TouchPhase::Start => touch = true, + TouchPhase::End | TouchPhase::Cancel => touch = false, + TouchPhase::Move => {} + } + // egui folds a scroll onto one axis by its modifiers before it measures the step. + let step = match (modifiers.matches_any(horizontal), modifiers.matches_any(vertical)) { + (true, false) | (false, true) => (delta.x + delta.y).abs(), + _ => delta.length(), + }; + let instant = touch || (unit == MouseWheelUnit::Point && step < 8.0); + wheel.push((unit, delta, modifiers, instant)); + } + ctx.data_mut(|d| d.insert_temp(id, Touch { frame, at_start, at_end: touch })); + wheel +} + +/// Read this frame's wheel input. Call it every frame (hovered or not) so the Alt and Shift +/// states of the gesture follow the latest wheel event. `owner` scopes the snap-zoom +/// accumulator per viewport. `hovered` gates accumulation: only the viewport receiving +/// navigation may add raw input to the snap residual. +pub fn read(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, animated_zoom: bool, owner: Id, hovered: bool) -> Option { + read_with(ctx, zoom_with_wheel, pinch_zooms, animated_zoom, owner, hovered, true, true) +} + +/// [`read`], choosing how a plain wheel scroll pans: `smooth` glides it over a few frames (egui's +/// smoothed delta); off, as with Interface › Enable Animations off, the view moves the whole notch +/// in the frame the wheel turns and stops there (#5721), as scroll areas already do. +/// `touch_gestures` is Preferences › Enhanced Controls › Enable Gestures: off, a touch screen's +/// two-finger pinch doesn't zoom ([`pinch_factor`]). +/// `animated_zoom` is Preferences › General › Animated Zoom: on, the wheel zoom is eased over the +/// frames of a notch; off, a notch is one instant ×1.1 step. +#[allow(clippy::too_many_arguments)] +pub fn read_with( + ctx: &Context, + zoom_with_wheel: bool, + pinch_zooms: bool, + animated_zoom: bool, + owner: Id, + hovered: bool, + smooth: bool, + touch_gestures: bool, +) -> Option { + let notch = ctx.options(|o| o.input_options.line_scroll_speed); + + // --- Modifier tracking (always runs for all callers) --- + let (scroll, zoom_delta, latest_alt, latest_shift, wheel, page) = ctx.input(|i| { + let wheel = wheel_events(i); + let latest_alt = wheel.last().map(|(_, _, m, _)| m.alt && !m.command && !m.ctrl); + let latest_shift = wheel.last().map(|(_, _, m, _)| m.shift && !m.command && !m.ctrl); + (i.smooth_scroll_delta, pinch_factor(i, touch_gestures), latest_alt, latest_shift, wheel, i.content_rect().height()) + }); + let wheel = mark_unsmoothed(ctx, wheel); + let alt = match latest_alt { + Some(a) => { + ctx.data_mut(|d| d.insert_temp(alt_id(), a)); + a + } + None => ctx.data(|d| d.get_temp::(alt_id())).unwrap_or(false), + }; + let shift = match latest_shift { + Some(s) => { + ctx.data_mut(|d| d.insert_temp(shift_id(), s)); + s + } + None => ctx.data(|d| d.get_temp::(shift_id())).unwrap_or(false), + }; + + if !hovered { + return None; + } + + // --- Input classification (hovered viewport only) --- + // The wheel events that belong to the gesture in progress: Shift+Alt (snap zoom), Alt-only + // (continuous zoom), Shift-only (snap zoom with zoom_with_wheel), or plain (scroll/zoom). + // `instant`: the part of `raw` egui leaves unsmoothed (a trackpad; see `wheel_events`). + let (raw, instant) = wheel + .iter() + .filter(|(_, _, m, _)| { + if alt && shift { + m.alt && m.shift && !m.command && !m.ctrl + } else if alt { + m.alt && !m.shift && !m.command && !m.ctrl + } else if shift { + m.shift && !m.alt && !m.command && !m.ctrl + } else { + !m.alt && !m.shift && !m.command && !m.ctrl + } + }) + .fold((Vec2::ZERO, Vec2::ZERO), |(sum, instant), (unit, delta, _, unsmoothed)| { + let d = *delta + * match unit { + MouseWheelUnit::Point => 1.0, + MouseWheelUnit::Line => notch, + MouseWheelUnit::Page => page, + }; + (sum + d, if *unsmoothed { instant + d } else { instant }) + }); + let snap_mode = shift && ((alt && !zoom_with_wheel) || (!alt && zoom_with_wheel)); + let has_pinch = pinch_zooms && zoom_delta.is_finite() && zoom_delta > 0.0 && zoom_delta != 1.0; + + if snap_mode && !has_pinch { + // Shift snap zoom takes over: drop this owner's unfinished eased notch, so a continuous + // zoom started shortly after Shift is released cannot carry its residual. + ctx.data_mut(|d| d.remove::(zoom_ease_id(owner))); + let dy = raw.y + raw.x; + if dy == 0.0 { + return None; + } + + // Per-frame idempotency: a second read in the same frame returns the cached result. + let frame_nr = ctx.cumulative_pass_nr(); + if let Some((cached_frame, cached_steps)) = ctx.data(|d| d.get_temp::<(u64, i32)>(snap_frame_result_id(owner))) + && cached_frame == frame_nr + { + return if cached_steps == 0 { None } else { Some(Wheel::SnapZoom(cached_steps)) }; + } + + // When the receiving viewport changes, clear the old viewport's residual so a + // partial gesture doesn't reappear when returning to it. + let prev_active = ctx.data(|d| d.get_temp::(active_snap_owner_id())); + if let Some(old) = prev_active.filter(|&p| p != owner) { + ctx.data_mut(|d| { + d.remove::(snap_residual_id(old)); + d.remove::(snap_time_id(old)); + }); + } + ctx.data_mut(|d| d.insert_temp(active_snap_owner_id(), owner)); + + let now = ctx.input(|i| i.time); + let last_time = ctx.data(|d| d.get_temp::(snap_time_id(owner))); + let prev = if last_time.is_some_and(|t| now - t < SNAP_IDLE_TIMEOUT && (now - t).is_finite()) { + ctx.data(|d| d.get_temp::(snap_residual_id(owner))).unwrap_or(0.0) + } else { + 0.0 + }; + let notch_val = if notch.is_finite() && notch > 0.0 { notch } else { 40.0 }; + let total = prev + dy; + let n = if total.is_finite() { (total / notch_val).trunc() } else { 0.0 }; + let remainder = total - n * notch_val; + let steps = n.clamp(-10.0, 10.0) as i32; + ctx.data_mut(|d| { + d.insert_temp(snap_residual_id(owner), if remainder.is_finite() { remainder } else { 0.0 }); + d.insert_temp(snap_time_id(owner), now); + d.insert_temp(snap_frame_result_id(owner), (frame_nr, steps)); + }); + if steps == 0 { None } else { Some(Wheel::SnapZoom(steps)) } + } else { + if !snap_mode { + ctx.data_mut(|d| { + d.remove::(snap_residual_id(owner)); + d.remove::(snap_time_id(owner)); + }); + } + // Zoom-mode input: ⌥ + wheel, or the plain wheel with Zoom with Scroll Wheel. With Animated + // Zoom on it eases from this mode's own wheel events: egui's smoothed delta has no notion of + // modifiers, so it would also carry the unfinished tail of a pan notch into the zoom. + let wheel_zooms = !shift && (alt != zoom_with_wheel); + let scroll = if animated_zoom && wheel_zooms { + // Eased as egui eases a wheel: what it leaves unsmoothed (a trackpad) zooms at once. + let along = |v: Vec2| if alt { v.y + v.x } else { v.y }; + let eased = ease_zoom_delta(ctx, owner, along(raw - instant)) + along(instant); + // The plain wheel zooms through `y`; its sideways part (⌘/Ctrl + wheel, a trackpad's + // horizontal scroll) is not a zoom event, so it pans like any scroll: smoothed, or + // with Enable Animations off at once (#5721). + if alt { Vec2::new(0.0, eased) } else { Vec2::new(if smooth { scroll.x } else { raw.x }, eased) } + } else { + ctx.data_mut(|d| d.remove::(zoom_ease_id(owner))); + // Unsmoothed, a plain scroll pans by this frame's own wheel events (none: no tail). + if smooth || alt || shift { scroll } else { raw } + }; + classify(Input { scroll, raw, zoom_delta, alt, shift, zoom_with_wheel, pinch_zooms, animated_zoom, notch }) + } +} + +fn is_alt_key(key: Key) -> bool { + matches!(key, Key::AltLeft | Key::AltRight) +} + +/// This frame's wheel events, in order, with ⌥/Alt added to their modifiers while an Alt key is +/// down (#8049). The modifiers a wheel event carries are the windowing layer's modifier state, +/// which on Windows can miss a held Alt (the wheel then scrolled until another modifier was +/// tapped), while the Alt key's own press and release still arrive; the menu bar follows those +/// too (`menu_keys`). Alt is down from its press until its release, starting from whether it +/// was down before this frame's first Alt key event (egui's `keys_down` when there is none). +fn wheel_events(i: &InputState) -> Vec<(MouseWheelUnit, Vec2, Modifiers, TouchPhase)> { + let first_alt_key = i.events.iter().find_map(|e| match e { + // A repeat means the key was already down; a release means it was down until then. + Event::Key { key, pressed, repeat, .. } if is_alt_key(*key) => Some(!*pressed || *repeat), + _ => None, + }); + let mut alt_key = first_alt_key.unwrap_or_else(|| i.key_down(Key::AltLeft) || i.key_down(Key::AltRight)); + let mut wheel = Vec::new(); + for e in &i.events { + match e { + Event::Key { key, pressed, .. } if is_alt_key(*key) => alt_key = *pressed, + Event::MouseWheel { unit, delta, phase, modifiers, .. } => { + let modifiers = Modifiers { alt: modifiers.alt || alt_key, ..*modifiers }; + wheel.push((*unit, *delta, modifiers, *phase)); + } + _ => {} + } + } + wheel +} + +/// This frame's pinch zoom factor (1 = none). While two fingers are down on a touch screen, egui's +/// `zoom_delta` reports their pinch in place of the trackpad's `Event::Zoom`. With Preferences › +/// Enhanced Controls › Enable Gestures off (`touch_gestures` false) the fingers are ignored and only +/// the trackpad's own pinch events count (Windows' Ctrl + wheel pinch included: [`fold_legacy_pinch`] +/// has already turned it into `Event::Zoom`). +pub fn pinch_factor(i: &InputState, touch_gestures: bool) -> f32 { + if touch_gestures || i.multi_touch().is_none() { + return i.zoom_delta(); + } + i.events + .iter() + .filter_map(|e| match e { + Event::Zoom(f) if f.is_finite() && *f > 0.0 => Some(*f), + _ => None, + }) + .product() +} + +#[cfg(test)] +mod tests; diff --git a/crates/ui-egui/src/canvas/wheel_nav/tests.rs b/crates/ui-egui/src/canvas/wheel_nav/tests.rs new file mode 100644 index 0000000000..d2211a7daa --- /dev/null +++ b/crates/ui-egui/src/canvas/wheel_nav/tests.rs @@ -0,0 +1,1015 @@ +use egui::TouchPhase; + +use super::*; + +fn input(scroll: Vec2, alt: bool) -> Input { + Input { scroll, raw: scroll, zoom_delta: 1.0, alt, shift: false, zoom_with_wheel: false, pinch_zooms: true, animated_zoom: true, notch: 40.0 } +} + +fn wheel(delta: Vec2, modifiers: Modifiers) -> Event { + Event::MouseWheel { source: egui::MouseWheelSource::Unknown, unit: MouseWheelUnit::Line, delta, phase: TouchPhase::Move, modifiers } +} + +const CTRL: Modifiers = Modifiers { ctrl: true, command: true, ..Modifiers::NONE }; +const CMD: Modifiers = Modifiers { mac_cmd: true, command: true, ..Modifiers::NONE }; + +/// Feed `events` through an egui context set up like the app's (`legacy`: the Windows pinch +/// fold on) and add up what the wheel did to the view over the following second. +/// +/// `gesture` runs with Animated Zoom off (a notch is one instant step); see [`gesture_with`]. +fn gesture(frames: Vec>, zoom_with_wheel: bool, legacy: bool) -> (Vec2, f32, i32) { + gesture_with(frames, zoom_with_wheel, legacy, false) +} + +fn gesture_with(frames: Vec>, zoom_with_wheel: bool, legacy: bool, animated_zoom: bool) -> (Vec2, f32, i32) { + let owner = Id::unique("test-gesture"); + let ctx = Context::default(); + configure(&ctx); + let (mut pan, mut zoom, mut snaps) = (Vec2::ZERO, 1.0, 0i32); + let mut frames = frames.into_iter(); + for n in 0..60 { + let mut raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events: frames.next().unwrap_or_default(), ..Default::default() }; + if legacy { + fold_legacy_pinch(&ctx, &mut raw); + } + let mut out = ctx.run_ui(raw, |ui| match read(ui.ctx(), zoom_with_wheel, true, animated_zoom, owner, true) { + Some(Wheel::Pan(d)) => pan += d, + Some(Wheel::Zoom(f)) => zoom *= f, + Some(Wheel::SnapZoom(n)) => snaps += n, + None => {} + }); + out.textures_delta.clear(); + } + (pan, zoom, snaps) +} + +fn wheel_point(delta: Vec2, modifiers: Modifiers) -> Event { + Event::MouseWheel { source: egui::MouseWheelSource::Unknown, unit: MouseWheelUnit::Point, delta, phase: TouchPhase::Move, modifiers } +} + +fn near(a: Vec2, b: Vec2) -> bool { + (a - b).length() < 0.01 +} + +/// #5721: with smoothing off a wheel notch pans the whole notch in the frame it arrives and +/// nothing after; smoothed (the default) it glides, reaching the same total over later frames. +#[test] +fn unsmoothed_wheel_pans_at_once_without_a_glide() { + let pans = |smooth: bool| -> Vec { + let owner = Id::unique("test-unsmoothed"); + let ctx = Context::default(); + configure(&ctx); + (0..30) + .map(|n| { + let events = if n == 0 { vec![wheel(Vec2::new(0.0, -1.0), Modifiers::NONE)] } else { Vec::new() }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut pan = Vec2::ZERO; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Pan(d)) = read_with(ui.ctx(), false, true, true, owner, true, smooth, true) { + pan = d; + } + }); + out.textures_delta.clear(); + pan + }) + .collect() + }; + let notch = { + let ctx = Context::default(); + configure(&ctx); + ctx.options(|o| o.input_options.line_scroll_speed) + }; + let direct = pans(false); + assert!(near(direct[0], Vec2::new(0.0, -notch)), "the whole notch at once: {:?}", direct[0]); + assert!(direct[1..].iter().all(|p| *p == Vec2::ZERO), "no glide after it"); + let smooth = pans(true); + assert!(smooth[0].y.abs() < notch - 0.5, "smoothed, the first frame moves only part of it: {:?}", smooth[0]); + assert!(smooth[1..].iter().any(|p| p.y != 0.0), "and the rest follows"); +} + +/// Interface › Enable Animations off only stops the *pan* gliding. A plain wheel that zooms +/// (Zoom with Scroll Wheel) with Animated Zoom on must still ease over the frames of a notch, +/// exactly as it does with the pan smoothing on. +#[test] +fn animated_wheel_zoom_keeps_its_eased_tail_when_pan_smoothing_is_off() { + let frames = |smooth: bool| -> Vec { + let owner = Id::unique("test-animated-zoom-unsmoothed-pan"); + let ctx = Context::default(); + configure(&ctx); + (0..60) + .map(|n| { + let events = if n == 0 { vec![wheel(Vec2::new(0.0, 1.0), Modifiers::NONE)] } else { Vec::new() }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut factor = 1.0; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Zoom(f)) = read_with(ui.ctx(), true, true, true, owner, true, smooth, true) { + factor = f; + } + }); + out.textures_delta.clear(); + factor + }) + .collect() + }; + let unsmoothed = frames(false); + assert!(unsmoothed[0] > 1.0 && unsmoothed[0] < NOTCH - 1e-3, "the first frame carries only part of it: {:?}", &unsmoothed[..4]); + assert!(unsmoothed[1..].iter().any(|f| *f > 1.0), "and the rest follows: {unsmoothed:?}"); + let product: f32 = unsmoothed.iter().product(); + assert!((product - NOTCH).abs() < 1e-4, "still x1.1 in total: {product}"); + let smoothed = frames(true); + assert!(unsmoothed.iter().zip(&smoothed).all(|(a, b)| (a - b).abs() < 1e-6), "same pacing as with pan smoothing on"); +} + +#[test] +fn plain_scroll_pans() { + assert_eq!(classify(input(Vec2::new(3.0, -40.0), false)), Some(Wheel::Pan(Vec2::new(3.0, -40.0)))); + assert_eq!(classify(input(Vec2::ZERO, false)), None); +} + +#[test] +fn alt_scroll_zooms_ten_percent_per_notch() { + let Some(Wheel::Zoom(f)) = classify(input(Vec2::new(0.0, 40.0), true)) else { panic!("zoom") }; + assert!((f - 1.1).abs() < 1e-5, "{f}"); + let Some(Wheel::Zoom(f)) = classify(input(Vec2::new(0.0, -40.0), true)) else { panic!("zoom") }; + assert!((f - 1.0 / 1.1).abs() < 1e-5, "{f}"); + // Two notches in one frame are two steps, x1.1 each. + let Some(Wheel::Zoom(f)) = classify(input(Vec2::new(0.0, 80.0), true)) else { panic!("zoom") }; + assert!((f - 1.21).abs() < 1e-4, "{f}"); +} + +/// Animated Zoom off: a notch is one instant step, and egui's smoothed tail does nothing. +#[test] +fn a_notch_zooms_the_frame_it_arrives_and_its_smoothed_tail_does_nothing() { + // egui keeps feeding a smoothed delta for several frames after the wheel event. Zooming + // must not follow it (that would ease the zoom), nor turn it into a pan. + let off = |i: Input| Input { animated_zoom: false, ..i }; + let tail = off(Input { raw: Vec2::ZERO, ..input(Vec2::new(0.0, 12.0), true) }); + assert_eq!(classify(tail), None); + let tail = off(Input { raw: Vec2::ZERO, zoom_with_wheel: true, ..input(Vec2::new(0.0, 12.0), false) }); + assert_eq!(classify(tail), None); + // The frame the notch arrives carries it whole, whatever the smoothed delta is. + let arrival = off(Input { scroll: Vec2::new(0.0, 12.0), raw: Vec2::new(0.0, 40.0), ..input(Vec2::ZERO, true) }); + assert!(matches!(classify(arrival), Some(Wheel::Zoom(f)) if (f - 1.1).abs() < 1e-5)); + // Plain scrolling without the preference still pans from the smoothed delta. + let pan = off(Input { raw: Vec2::ZERO, ..input(Vec2::new(0.0, 12.0), false) }); + assert_eq!(classify(pan), Some(Wheel::Pan(Vec2::new(0.0, 12.0)))); +} + +/// Animated Zoom on: the zoom follows egui's smoothed delta over the frames of a notch, and +/// never turns that tail into a pan. +#[test] +fn animated_zoom_follows_the_smoothed_tail_of_a_notch() { + let tail = Input { raw: Vec2::ZERO, ..input(Vec2::new(0.0, 12.0), true) }; + assert!(matches!(classify(tail), Some(Wheel::Zoom(f)) if f > 1.0)); + let tail = Input { raw: Vec2::ZERO, zoom_with_wheel: true, ..input(Vec2::new(0.0, 12.0), false) }; + assert!(matches!(classify(tail), Some(Wheel::Zoom(f)) if f > 1.0)); + // The size of the step comes from the smoothed delta, not the raw one. + let part = Input { scroll: Vec2::new(0.0, 20.0), raw: Vec2::new(0.0, 40.0), ..input(Vec2::ZERO, true) }; + assert!(matches!(classify(part), Some(Wheel::Zoom(f)) if (f - 1.1f32.powf(0.5)).abs() < 1e-5)); + // Plain scrolling without the preference still pans from the smoothed delta. + let pan = Input { raw: Vec2::ZERO, ..input(Vec2::new(0.0, 12.0), false) }; + assert_eq!(classify(pan), Some(Wheel::Pan(Vec2::new(0.0, 12.0)))); + // ⇧ + snap zoom stays on the wheel events and instant, in both modes. + for animated_zoom in [false, true] { + let tail = Input { shift: true, zoom_with_wheel: true, raw: Vec2::ZERO, animated_zoom, ..input(Vec2::new(12.0, 0.0), false) }; + assert_eq!(classify(tail), None, "animated_zoom {animated_zoom}"); + let snap = Input { shift: true, zoom_with_wheel: true, raw: Vec2::new(0.0, 40.0), animated_zoom, ..input(Vec2::new(0.0, 12.0), false) }; + assert_eq!(classify(snap), Some(Wheel::SnapZoom(1)), "animated_zoom {animated_zoom}"); + } +} + +/// The factor of every frame of one notch, through a real egui context (60 frames at 60 fps). +fn zoom_frames(modifiers: Modifiers, zoom_with_wheel: bool, animated_zoom: bool) -> Vec { + let owner = Id::unique("test-zoom-frames"); + let ctx = Context::default(); + configure(&ctx); + (0..60) + .map(|n| { + let events = if n == 0 { vec![wheel(Vec2::new(0.0, 1.0), modifiers)] } else { Vec::new() }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut factor = 1.0; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Zoom(f)) = read(ui.ctx(), zoom_with_wheel, true, animated_zoom, owner, true) { + factor = f; + } + }); + out.textures_delta.clear(); + factor + }) + .collect() +} + +/// The property the eased zoom relies on: however egui spreads a notch over frames, the +/// factors of those frames multiply to exactly ×1.1 (and ×1/1.1 for a notch the other way). +#[test] +fn the_eased_frames_of_one_notch_multiply_to_ten_percent() { + for (modifiers, zoom_with_wheel) in [(Modifiers::ALT, false), (Modifiers::NONE, true)] { + let frames = zoom_frames(modifiers, zoom_with_wheel, true); + assert!(frames.iter().all(|f| f.is_finite() && *f >= 1.0), "{frames:?}"); + assert!(frames.iter().filter(|f| **f != 1.0).count() > 1, "eased over several frames: {frames:?}"); + let product: f32 = frames.iter().product(); + assert!((product - NOTCH).abs() < 1e-4, "{modifiers:?} zoom_with_wheel {zoom_with_wheel}: {product} from {frames:?}"); + } + // The other way round: ×1/1.1. + let owner = Id::unique("test-zoom-frames-out"); + let ctx = Context::default(); + configure(&ctx); + let mut product = 1.0f32; + for n in 0..60 { + let events = if n == 0 { vec![wheel(Vec2::new(0.0, -1.0), Modifiers::ALT)] } else { Vec::new() }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Zoom(f)) = read(ui.ctx(), false, true, true, owner, true) { + product *= f; + } + }); + out.textures_delta.clear(); + } + assert!((product - 1.0 / NOTCH).abs() < 1e-4, "{product}"); +} + +/// Animated Zoom changes the pacing of a notch, never its size: off, the whole ×1.1 lands in +/// the frame the notch arrives and nothing follows; on, that frame carries only part of it. +#[test] +fn animated_zoom_changes_the_pacing_of_a_notch_not_its_size() { + for (modifiers, zoom_with_wheel) in [(Modifiers::ALT, false), (Modifiers::NONE, true)] { + let instant = zoom_frames(modifiers, zoom_with_wheel, false); + assert!((instant[0] - NOTCH).abs() < 1e-5, "the whole notch at once: {:?}", &instant[..4]); + assert!(instant[1..].iter().all(|f| *f == 1.0), "no tail after it: {instant:?}"); + let eased = zoom_frames(modifiers, zoom_with_wheel, true); + assert!(eased[0] > 1.0 && eased[0] < NOTCH - 1e-3, "the first frame carries only part of it: {:?}", &eased[..4]); + assert!(eased[1..].iter().any(|f| *f > 1.0), "and the rest follows"); + } +} + +/// Animated Zoom is only about the wheel zoom: ⇧ snap zoom and plain scrolling are the same +/// in both modes. +#[test] +fn animated_zoom_leaves_snap_zoom_and_panning_alone() { + let notch = Vec2::new(0.0, 1.0); + let shift_alt = Modifiers { shift: true, alt: true, ..Modifiers::NONE }; + for animated_zoom in [false, true] { + let (pan, zoom, snaps) = gesture_with(vec![vec![wheel(notch, shift_alt)]], false, false, animated_zoom); + assert!(pan == Vec2::ZERO && zoom == 1.0 && snaps == 1, "⇧ + ⌥: {pan:?} {zoom} {snaps}"); + let (pan, zoom, snaps) = gesture_with(vec![vec![wheel(notch, Modifiers::NONE)]], false, false, animated_zoom); + assert!(near(pan, Vec2::new(0.0, 40.0)) && zoom == 1.0 && snaps == 0, "plain wheel pans: {pan:?} {zoom} {snaps}"); + // A notch zooms ×1.1 in total in either mode. + let (pan, zoom, _) = gesture_with(vec![vec![wheel(notch, Modifiers::ALT)]], false, false, animated_zoom); + assert!(pan == Vec2::ZERO && (zoom - NOTCH).abs() < 1e-4, "animated_zoom {animated_zoom}: {pan:?} {zoom}"); + } +} + +/// A pan notch still gliding when an ⌥ notch arrives (and the same the other way round with +/// Zoom with Scroll Wheel): the zoom is that one notch's ×1.1, not the sum of both tails. +#[test] +fn a_pan_tail_does_not_leak_into_the_animated_zoom() { + let notch = Vec2::new(0.0, 1.0); + // Wheel pans, ⌥ zooms: plain notch, then ⌥ notch one frame later. + let frames = vec![vec![wheel(notch, Modifiers::NONE)], vec![wheel(notch, Modifiers::ALT)]]; + let (pan, zoom, snaps) = gesture_with(frames, false, false, true); + assert!((zoom - NOTCH).abs() < 1e-4, "only the ⌥ notch zooms: {zoom}"); + assert!(pan.y > 0.0 && pan.y < 40.0 && snaps == 0, "the plain notch only panned its first frame: {pan:?} {snaps}"); + // Wheel zooms, ⌥ pans: ⌥ notch, then plain notch. + let frames = vec![vec![wheel(notch, Modifiers::ALT)], vec![wheel(notch, Modifiers::NONE)]]; + let (_, zoom, snaps) = gesture_with(frames, true, false, true); + assert!((zoom - NOTCH).abs() < 1e-4, "only the plain notch zooms: {zoom}"); + assert_eq!(snaps, 0); +} + +/// The zoom factor of each of `frames` frames with Animated Zoom on and ⌥ zooming. `time` gives a +/// frame's timestamp and `hovered` whether the owner is hovered in it. +fn run_zoom(frames: u32, events: impl Fn(u32) -> Vec, time: impl Fn(u32) -> f64, hovered: impl Fn(u32) -> bool) -> Vec { + let owner = Id::unique("test-run-zoom"); + let ctx = Context::default(); + configure(&ctx); + (0..frames) + .map(|n| { + let raw = RawInput { time: Some(time(n)), predicted_dt: 1.0 / 60.0, events: events(n), ..Default::default() }; + let mut factor = 1.0; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Zoom(f)) = read(ui.ctx(), false, true, true, owner, hovered(n)) { + factor = f; + } + }); + out.textures_delta.clear(); + factor + }) + .collect() +} + +/// A slow frame in the middle of an eased notch (a big image redrawing) must not drop what is +/// left of it: the notch still adds up to exactly ×1.1. +#[test] +fn a_slow_frame_does_not_cut_an_eased_notch_short() { + let notch = Vec2::new(0.0, 1.0); + let time = |n: u32| if n == 0 { 0.0 } else { 0.7 + f64::from(n - 1) / 60.0 }; + let frames = run_zoom(60, |n| if n == 0 { vec![wheel(notch, Modifiers::ALT)] } else { Vec::new() }, time, |_| true); + let product: f32 = frames.iter().product(); + assert!((product - NOTCH).abs() < 1e-4, "still x1.1 in total: {product} from {frames:?}"); +} + +/// What is left of an eased notch when the pointer leaves the viewport is dropped: a notch +/// made after it comes back is ×1.1 on its own, not ×1.1 plus the old remainder. +#[test] +fn an_unfinished_notch_is_not_carried_into_a_notch_after_the_pointer_left() { + let notch = Vec2::new(0.0, 1.0); + let events = |n: u32| if n == 0 || n == 40 { vec![wheel(notch, Modifiers::ALT)] } else { Vec::new() }; + let frames = run_zoom(100, events, |n| f64::from(n) / 60.0, |n| !(1..30).contains(&n)); + let first: f32 = frames[..40].iter().product(); + assert!(first > 1.0 && first < NOTCH - 1e-3, "the first notch was left unfinished: {first}"); + let second: f32 = frames[40..].iter().product(); + assert!((second - NOTCH).abs() < 1e-4, "the second notch is ×1.1 on its own: {second}"); +} + +/// Zoom with Scroll Wheel + Animated Zoom (the defaults of both): ⌘/Ctrl + wheel still scrolls +/// sideways, as it does with Animated Zoom off. +#[test] +fn animated_zoom_keeps_ctrl_wheel_sideways_scroll_with_zoom_with_wheel() { + let (pan, zoom, snaps) = gesture_with(vec![vec![wheel(Vec2::new(0.0, 1.0), CTRL)]], true, false, true); + assert!(near(pan, Vec2::new(40.0, 0.0)) && zoom == 1.0 && snaps == 0, "{pan:?} {zoom} {snaps}"); +} + +/// Shift snap zoom interrupting an unfinished animated notch drops that notch's residual: a +/// continuous zoom started right after Shift is released is its own ×1.1 and nothing more. +#[test] +fn shift_snap_zoom_drops_the_unfinished_eased_notch() { + let notch = Vec2::new(0.0, 1.0); + let shift_alt = Modifiers { shift: true, ..Modifiers::ALT }; + let owner = Id::unique("test-snap-interrupts-ease"); + let ctx = Context::default(); + configure(&ctx); + let (mut zoom, mut zoom_after_first, mut snaps) = (1.0f32, 1.0f32, 0i32); + for n in 0..60u32 { + // ⌥ notch (eases), ⇧⌥ notch (snap zoom takes over), then ⌥ notch again within 0.5 s. + let events = match n { + 0 | 2 => vec![wheel(notch, Modifiers::ALT)], + 1 => vec![wheel(notch, shift_alt)], + _ => Vec::new(), + }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| match read(ui.ctx(), false, true, true, owner, true) { + Some(Wheel::Zoom(f)) => zoom *= f, + Some(Wheel::SnapZoom(s)) => snaps += s, + _ => {} + }); + out.textures_delta.clear(); + if n == 0 { + zoom_after_first = zoom; + } + } + assert_eq!(snaps, 1); + let second = zoom / zoom_after_first; + assert!((second - NOTCH).abs() < 1e-4, "the second ⌥ notch zooms ×1.1 on its own: {second}"); +} + +/// A trackpad's ⌥ + two-finger scroll comes in points and is already smooth: egui leaves it +/// unsmoothed because smoothing adds latency, so Animated Zoom must not ease it either. Each +/// frame zooms by its own events, at once. +#[test] +fn animated_zoom_does_not_ease_a_trackpad() { + let frames = run_zoom(30, |n| if n == 0 { vec![wheel_point(Vec2::new(0.0, 4.0), Modifiers::ALT)] } else { Vec::new() }, |n| f64::from(n) / 60.0, |_| true); + assert!((frames[0] - NOTCH.powf(0.1)).abs() < 1e-6, "all of it in the frame it arrives: {:?}", &frames[..4]); + assert!(frames[1..].iter().all(|f| *f == 1.0), "no tail after it: {frames:?}"); +} + +/// Zoom with Scroll Wheel + Animated Zoom with Enable Animations off: the wheel zooms eased, but +/// a sideways wheel still pans without a glide (#5721), all of it in the frame it turns. +#[test] +fn animated_zoom_keeps_the_sideways_wheel_unsmoothed_with_animations_off() { + let owner = Id::unique("test-sideways-unsmoothed"); + let ctx = Context::default(); + configure(&ctx); + let pans: Vec = (0..30) + .map(|n| { + let events = if n == 0 { vec![wheel(Vec2::new(1.0, 0.0), Modifiers::NONE)] } else { Vec::new() }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut pan = Vec2::ZERO; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Pan(d)) = read_with(ui.ctx(), true, true, true, owner, true, false, true) { + pan = d; + } + }); + out.textures_delta.clear(); + pan + }) + .collect(); + assert!(near(pans[0], Vec2::new(40.0, 0.0)), "the whole notch at once: {:?}", &pans[..4]); + assert!(pans[1..].iter().all(|p| *p == Vec2::ZERO), "no glide after it: {pans:?}"); +} + +/// A diagonal point step with ⌥ is measured as egui measures it, folded onto one axis: (5, 5) +/// is a 10-point step, which egui smooths, so Animated Zoom eases it. +#[test] +fn a_diagonal_alt_point_step_is_measured_like_egui() { + let frames = run_zoom(60, |n| if n == 0 { vec![wheel_point(Vec2::new(5.0, 5.0), Modifiers::ALT)] } else { Vec::new() }, |n| f64::from(n) / 60.0, |_| true); + assert!(frames[0] < NOTCH.powf(0.25) - 1e-4 && frames[1] > 1.0, "eased: {:?}", &frames[..4]); +} + +/// A touch whose end nobody read (no canvas in that pass, e.g. the last document closed +/// during a trackpad scroll) must not leave later mouse notches un-eased. +#[test] +fn a_touch_end_nobody_read_does_not_stick() { + let owner = Id::unique("test-touch-unread-end"); + let ctx = Context::default(); + configure(&ctx); + let point = |phase: TouchPhase| Event::MouseWheel { + source: egui::MouseWheelSource::Unknown, + unit: MouseWheelUnit::Point, + delta: Vec2::ZERO, + phase, + modifiers: Modifiers::ALT, + }; + let mut first = 1.0; + for n in 0..10u32 { + let events = match n { + 0 => vec![point(TouchPhase::Start)], + 2 => vec![point(TouchPhase::End)], + 5 => vec![wheel(Vec2::new(0.0, 1.0), Modifiers::ALT)], + _ => Vec::new(), + }; + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + // Frames 2-3: nothing reads the wheel. + if !(2..4).contains(&n) + && let Some(Wheel::Zoom(f)) = read(ui.ctx(), false, true, true, owner, true) + && n == 5 + { + first = f; + } + }); + out.textures_delta.clear(); + } + assert!(first > 1.0 && first < NOTCH - 1e-3, "the notch is eased: {first}"); +} + +/// Quick back-and-forth wheeling still lands on whole ×1.1 steps: three notches in and one +/// out a frame later is ×1.1², whatever was still easing. +#[test] +fn a_quick_notch_back_nets_out() { + for back_at in [1, 3] { + let events = |n: u32| match n { + 0 => vec![wheel(Vec2::new(0.0, 3.0), Modifiers::ALT)], + n if n == back_at => vec![wheel(Vec2::new(0.0, -1.0), Modifiers::ALT)], + _ => Vec::new(), + }; + let total: f32 = run_zoom(60, events, |n| f64::from(n) / 60.0, |_| true).iter().product(); + assert!((total - NOTCH * NOTCH).abs() < 1e-4, "back at frame {back_at}: {total}"); + } +} + +/// A browser reports a mouse notch as a big pixel step (about 100 points in Chrome), which +/// egui smooths like a notch: Animated Zoom eases it too. Within a trackpad touch (egui's +/// `InTouch`) even big point steps zoom at once. +#[test] +fn animated_zoom_eases_big_pixel_steps_but_not_a_touch() { + let point = |dy: f32, phase: TouchPhase| Event::MouseWheel { + source: egui::MouseWheelSource::Unknown, + unit: MouseWheelUnit::Point, + delta: Vec2::new(0.0, dy), + phase, + modifiers: Modifiers::ALT, + }; + let browser = run_zoom(60, |n| if n == 0 { vec![point(100.0, TouchPhase::Move)] } else { Vec::new() }, |n| f64::from(n) / 60.0, |_| true); + assert!(browser[0] > 1.0 && browser[0] < NOTCH.powf(2.5) - 1e-3, "eased: {:?}", &browser[..4]); + let total: f32 = browser.iter().product(); + assert!((total - NOTCH.powf(2.5)).abs() < 1e-3, "{total}"); + let events = |n: u32| match n { + 0 => vec![point(0.0, TouchPhase::Start)], + 1 => vec![point(20.0, TouchPhase::Move)], + _ => Vec::new(), + }; + let touch = run_zoom(30, events, |n| f64::from(n) / 60.0, |_| true); + assert!((touch[1] - NOTCH.powf(0.5)).abs() < 1e-5, "at once: {:?}", &touch[..4]); + assert!(touch[2..].iter().all(|f| *f == 1.0), "no tail: {touch:?}"); +} + +#[test] +fn pinch_zooms() { + let i = Input { zoom_delta: 1.2, ..input(Vec2::ZERO, false) }; + assert_eq!(classify(i), Some(Wheel::Zoom(1.2))); + // The pinch wins over Alt. + let i = Input { zoom_delta: 0.8, ..input(Vec2::new(0.0, 40.0), true) }; + assert_eq!(classify(i), Some(Wheel::Zoom(0.8))); +} + +/// Preferences › Enhanced Controls › Zoom with Trackpad Pinch off: the pinch is ignored, and +/// a scroll in the same frame pans as it would without a pinch. +#[test] +fn the_pinch_preference_switches_pinch_zooming() { + let i = Input { zoom_delta: 1.2, pinch_zooms: false, ..input(Vec2::ZERO, false) }; + assert_eq!(classify(i), None); + let i = Input { zoom_delta: 1.2, pinch_zooms: false, ..input(Vec2::new(0.0, 12.0), false) }; + assert_eq!(classify(i), Some(Wheel::Pan(Vec2::new(0.0, 12.0)))); + // On (the default) the pinch still zooms. + let i = Input { zoom_delta: 1.2, pinch_zooms: true, ..input(Vec2::ZERO, false) }; + assert_eq!(classify(i), Some(Wheel::Zoom(1.2))); +} + +/// The Windows pinch fold follows the preference too: off, the Ctrl + wheel fractions stay +/// wheel events (which egui folds into a sideways scroll). +#[test] +fn the_windows_pinch_fold_follows_the_preference() { + use eframe::App as _; + if !cfg!(target_os = "windows") { + return; + } + let mut app = crate::PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + let ctx = Context::default(); + let event = || Event::MouseWheel { + unit: MouseWheelUnit::Line, + delta: egui::vec2(0.0, 0.25), + phase: egui::TouchPhase::Move, + source: egui::MouseWheelSource::Unknown, + modifiers: Modifiers { ctrl: true, command: true, ..Modifiers::NONE }, + }; + let mut raw = RawInput { time: Some(1.0), events: vec![event()], ..Default::default() }; + app.raw_input_hook(&ctx, &mut raw); + assert!(matches!(raw.events[0], Event::Zoom(_)), "the fraction is a pinch: {:?}", raw.events[0]); + app.run("prefs.set", serde_json::json!({"path": "enhancedControls.zoomWithTrackpadPinch", "value": false})).unwrap(); + let mut raw = RawInput { time: Some(2.0), events: vec![event()], ..Default::default() }; + app.raw_input_hook(&ctx, &mut raw); + assert!(matches!(raw.events[0], Event::MouseWheel { .. }), "off: it stays a wheel event: {:?}", raw.events[0]); +} + +#[test] +fn zoom_with_scroll_wheel_preference_swaps_wheel_and_alt_wheel() { + let pref = |scroll, alt| Input { zoom_with_wheel: true, ..input(scroll, alt) }; + let Some(Wheel::Zoom(f)) = classify(pref(Vec2::new(0.0, 40.0), false)) else { panic!("zoom") }; + assert!((f - 1.1).abs() < 1e-5, "{f}"); + assert_eq!(classify(pref(Vec2::new(0.0, 40.0), true)), Some(Wheel::Pan(Vec2::new(0.0, 40.0))), "⌥ + wheel scrolls"); + assert_eq!(classify(pref(Vec2::new(40.0, 0.0), false)), Some(Wheel::Pan(Vec2::new(40.0, 0.0))), "sideways still scrolls"); +} + +#[test] +fn shift_scroll_snaps_to_zoom_ladder_with_preference() { + let snap = |dy: f32| Input { shift: true, zoom_with_wheel: true, ..input(Vec2::new(0.0, dy), false) }; + assert_eq!(classify(snap(40.0)), Some(Wheel::SnapZoom(1)), "one notch up → +1 step"); + assert_eq!(classify(snap(-40.0)), Some(Wheel::SnapZoom(-1)), "one notch down → −1 step"); + assert_eq!(classify(snap(80.0)), Some(Wheel::SnapZoom(2)), "two notches → +2 steps"); + assert_eq!(classify(snap(0.0)), None, "no scroll → nothing"); + // Some systems deliver Shift+wheel as horizontal (delta in x, y = 0). + let snap_h = |dx: f32| Input { shift: true, zoom_with_wheel: true, raw: Vec2::new(dx, 0.0), ..input(Vec2::new(dx, 0.0), false) }; + assert_eq!(classify(snap_h(40.0)), Some(Wheel::SnapZoom(1)), "horizontal notch → +1 step"); + assert_eq!(classify(snap_h(-40.0)), Some(Wheel::SnapZoom(-1)), "horizontal notch down → −1 step"); +} + +#[test] +fn shift_scroll_tail_does_not_snap_or_pan() { + let tail = Input { shift: true, zoom_with_wheel: true, raw: Vec2::ZERO, ..input(Vec2::new(12.0, 0.0), false) }; + assert_eq!(classify(tail), None, "smoothed tail of ⇧ + scroll produces nothing"); +} + +#[test] +fn shift_alt_scroll_snaps_without_preference() { + let snap = |dy: f32| Input { shift: true, ..input(Vec2::new(0.0, dy), true) }; + assert_eq!(classify(snap(40.0)), Some(Wheel::SnapZoom(1)), "⇧ + ⌥ + notch up → +1 step"); + assert_eq!(classify(snap(-40.0)), Some(Wheel::SnapZoom(-1)), "⇧ + ⌥ + notch down → −1 step"); + assert_eq!(classify(snap(0.0)), None, "no scroll → nothing"); +} + +#[test] +fn shift_only_without_zoom_with_wheel_does_not_snap() { + let i = Input { shift: true, zoom_with_wheel: false, ..input(Vec2::new(0.0, 40.0), false) }; + assert_eq!(classify(i), Some(Wheel::Pan(Vec2::new(0.0, 40.0))), "⇧ alone without the preference: no zoom"); +} + +#[test] +fn shift_scroll_snaps_through_egui_context() { + let notch = Vec2::new(0.0, 1.0); + // Shift + wheel with zoom_with_wheel on. + let (pan, zoom, snaps) = gesture(vec![vec![wheel(notch, Modifiers::SHIFT)]], true, false); + assert_eq!(snaps, 1, "⇧ + wheel with zoom_with_wheel → snap"); + assert!(pan == Vec2::ZERO, "no pan: {pan:?}"); + assert_eq!(zoom, 1.0, "no continuous zoom: {zoom}"); + let (_, _, snaps) = gesture(vec![vec![wheel(-notch, Modifiers::SHIFT)]], true, false); + assert_eq!(snaps, -1, "⇧ + wheel down → snap out"); + // Some systems deliver Shift+wheel as horizontal (delta in x, y = 0). + let h_notch = Vec2::new(1.0, 0.0); + let (pan, zoom, snaps) = gesture(vec![vec![wheel(h_notch, Modifiers::SHIFT)]], true, false); + assert_eq!(snaps, 1, "⇧ + horizontal wheel with zoom_with_wheel → snap"); + assert!(pan == Vec2::ZERO, "no pan: {pan:?}"); + assert_eq!(zoom, 1.0, "no continuous zoom: {zoom}"); + // Shift + Alt + wheel without the preference. + let shift_alt = Modifiers { shift: true, alt: true, ..Modifiers::NONE }; + let (pan, zoom, snaps) = gesture(vec![vec![wheel(notch, shift_alt)]], false, false); + assert_eq!(snaps, 1, "⇧ + ⌥ + wheel → snap"); + assert!(pan == Vec2::ZERO, "no pan: {pan:?}"); + assert_eq!(zoom, 1.0, "no continuous zoom: {zoom}"); +} + +#[test] +fn each_modifier_maps_like_photoshop() { + let notch = Vec2::new(0.0, 1.0); + let one = |m: Modifiers| gesture(vec![vec![wheel(notch, m)]], false, false); + let (pan, zoom, _) = one(Modifiers::NONE); + assert!(near(pan, Vec2::new(0.0, 40.0)) && zoom == 1.0, "wheel scrolls vertically: {pan:?} {zoom}"); + for m in [Modifiers::SHIFT, CTRL, CMD, Modifiers::CTRL] { + let (pan, zoom, _) = one(m); + assert!(near(pan, Vec2::new(40.0, 0.0)) && zoom == 1.0, "{m:?} + wheel scrolls sideways: {pan:?} {zoom}"); + } + let (pan, zoom, _) = one(Modifiers::ALT); + assert!(pan == Vec2::ZERO && (zoom - 1.1).abs() < 1e-4, "⌥ + wheel zooms 10%, at once: {pan:?} {zoom}"); + // Zoom with Scroll Wheel on: swapped. + let (pan, zoom, _) = gesture(vec![vec![wheel(notch, Modifiers::ALT)]], true, false); + assert!(near(pan, Vec2::new(0.0, 40.0)) && zoom == 1.0, "{pan:?} {zoom}"); + let (pan, zoom, _) = gesture(vec![vec![wheel(notch, Modifiers::NONE)]], true, false); + assert!(pan == Vec2::ZERO && (zoom - 1.1).abs() < 1e-4, "{pan:?} {zoom}"); + let (pan, zoom, _) = gesture(vec![vec![wheel(notch, CTRL)]], true, false); + assert!(near(pan, Vec2::new(40.0, 0.0)) && zoom == 1.0, "⌘/Ctrl + wheel still scrolls sideways: {pan:?} {zoom}"); +} + +#[test] +fn pinch_zooms_and_ctrl_wheel_pans() { + // macOS trackpad and browsers: the pinch is its own event. + let (pan, zoom, _) = gesture(vec![vec![Event::Zoom(1.1)], vec![Event::Zoom(1.1)]], false, false); + assert!(pan == Vec2::ZERO && (zoom - 1.21).abs() < 1e-4, "{pan:?} {zoom}"); + // Windows: a touchpad pinch is Ctrl + wheel in fractions of a notch, a mouse wheel whole notches. + let (pan, zoom, _) = gesture(vec![vec![wheel(Vec2::new(0.0, 0.25), CTRL)], vec![wheel(Vec2::new(0.0, 0.5), CTRL)]], false, true); + let speed = egui::InputOptions::default().scroll_zoom_speed; + assert!(pan == Vec2::ZERO && (zoom - (speed * 40.0 * 0.75).exp()).abs() < 1e-4, "the pinch zooms: {pan:?} {zoom}"); + let (pan, zoom, _) = gesture(vec![vec![wheel(Vec2::new(0.0, 1.0), CTRL)]], false, true); + assert!(near(pan, Vec2::new(40.0, 0.0)) && zoom == 1.0, "Ctrl + mouse wheel scrolls sideways: {pan:?} {zoom}"); +} + +#[test] +fn linux_wayland_point_unit_pinch_keeps_mouse_notches_for_sideways_scroll() { + // Some Wayland compositors report fractional Ctrl-wheel pinch steps in points + // rather than line units. The delta must not be multiplied by notch twice. + let mut point = [Event::MouseWheel { + source: egui::MouseWheelSource::Unknown, + unit: MouseWheelUnit::Point, + delta: Vec2::new(0.0, 8.0), + phase: TouchPhase::Move, + modifiers: CTRL, + }]; + assert_eq!(legacy_pinch_to_zoom(&mut point, 1.0, None, 40.0, 0.005), Some(1.0)); + assert!(matches!(point[0], Event::Zoom(f) if (f - (8.0f32 * 0.005).exp()).abs() < 1e-5)); + let mut wheel_only = [wheel(Vec2::new(0.0, 1.0), CTRL)]; + assert_eq!(legacy_pinch_to_zoom(&mut wheel_only, 2.0, Some(1.0), 40.0, 0.005), Some(1.0)); + assert!(matches!(wheel_only[0], Event::MouseWheel { .. }), "whole mouse notch is not a pinch"); +} + +#[test] +fn native_pinch_is_not_zoomed_twice_when_ctrl_wheel_arrives_in_the_same_frame() { + let (_, zoom, _) = gesture(vec![vec![Event::Zoom(1.2), wheel(Vec2::new(0.0, 0.25), CTRL)]], false, true); + assert!((zoom - 1.2).abs() < 1e-4, "native zoom must win over fallback: {zoom}"); +} + +#[test] +fn native_pinch_resets_legacy_gesture_continuation() { + let ctx = Context::default(); + let mut legacy = RawInput { time: Some(1.0), events: vec![wheel(Vec2::new(0.0, 0.25), CTRL)], ..Default::default() }; + fold_legacy_pinch(&ctx, &mut legacy); + assert!(matches!(legacy.events[0], Event::Zoom(_))); + + // Native magnification takes over before the synthetic gesture times out. + let mut native = RawInput { time: Some(1.1), events: vec![Event::Zoom(1.1), wheel(Vec2::new(0.0, 0.25), CTRL)], ..Default::default() }; + fold_legacy_pinch(&ctx, &mut native); + assert!(matches!(native.events[0], Event::Zoom(f) if (f - 1.1).abs() < 1e-5)); + assert!(matches!(native.events[1], Event::MouseWheel { .. })); + + // A notched mouse scroll must no longer inherit the previous pinch. + let mut notch = RawInput { time: Some(1.12), events: vec![wheel(Vec2::new(0.0, 1.0), CTRL)], ..Default::default() }; + fold_legacy_pinch(&ctx, &mut notch); + assert!(matches!(notch.events[0], Event::MouseWheel { .. })); +} + +#[test] +fn a_whole_notch_inside_a_pinch_stays_a_pinch() { + let mut ev = [wheel(Vec2::new(0.0, 0.5), CTRL)]; + let t = legacy_pinch_to_zoom(&mut ev, 1.0, None, 40.0, 0.005); + assert_eq!(t, Some(1.0)); + assert!(matches!(ev[0], Event::Zoom(f) if (f - 0.1f32.exp()).abs() < 1e-5), "{ev:?}"); + // The next step lands on a whole notch: still the pinch. + let mut ev = [wheel(Vec2::new(0.0, -1.0), CTRL)]; + assert_eq!(legacy_pinch_to_zoom(&mut ev, 1.1, t, 40.0, 0.005), Some(1.1)); + assert!(matches!(ev[0], Event::Zoom(f) if f < 1.0), "{ev:?}"); + // Long after: a mouse wheel notch, untouched. + let mut ev = [wheel(Vec2::new(0.0, -1.0), CTRL)]; + assert_eq!(legacy_pinch_to_zoom(&mut ev, 2.0, Some(1.1), 40.0, 0.005), Some(1.1)); + assert!(matches!(ev[0], Event::MouseWheel { .. })); + // Without Ctrl nothing is a pinch. + let mut ev = [wheel(Vec2::new(0.0, 0.5), Modifiers::NONE)]; + assert_eq!(legacy_pinch_to_zoom(&mut ev, 2.0, None, 40.0, 0.005), None); + assert!(matches!(ev[0], Event::MouseWheel { .. })); +} + +#[test] +fn hostile_numbers_never_produce_a_bad_zoom() { + for s in [f32::NAN, f32::INFINITY, -f32::INFINITY, 1e30, -1e30] { + for alt in [false, true] { + for notch in [0.0, -1.0, f32::NAN, 40.0] { + for (zd, animated_zoom) in [f32::NAN, 0.0, -1.0, f32::INFINITY, 1.0].into_iter().flat_map(|zd| [(zd, false), (zd, true)]) { + let i = Input { + scroll: Vec2::new(0.0, s), + raw: Vec2::new(0.0, s), + zoom_delta: zd, + alt, + shift: false, + zoom_with_wheel: !alt, + pinch_zooms: true, + animated_zoom, + notch, + }; + if let Some(Wheel::Zoom(f)) = classify(i) { + assert!(f.is_finite() && f > 0.0, "{f} from {i:?}"); + } + let si = Input { + shift: true, + zoom_with_wheel: true, + ..Input { + scroll: Vec2::new(0.0, s), + raw: Vec2::new(0.0, s), + zoom_delta: zd, + alt: false, + shift: false, + zoom_with_wheel: true, + pinch_zooms: true, + animated_zoom, + notch, + } + }; + if let Some(Wheel::SnapZoom(n)) = classify(si) { + assert!(n.abs() <= 10, "snap steps clamped: {n} from {si:?}"); + } + } + let mut ev = [wheel(Vec2::new(s, 0.5), CTRL)]; + let _ = legacy_pinch_to_zoom(&mut ev, f64::NAN, Some(f64::NAN), notch, 0.005); + let _ = legacy_pinch_to_zoom(&mut ev, 0.0, None, notch, f32::NAN); + if let Event::Zoom(f) = ev[0] { + assert!(f.is_finite() && f > 0.0, "{f}"); + } + } + } + } +} + +/// A gesture helper that runs each frame at 1/60 s spacing with Point-unit events, +/// collecting snaps, and using a specific owner ID. +fn snap_gesture(frames: Vec>, zoom_with_wheel: bool, owner: Id) -> i32 { + let ctx = Context::default(); + configure(&ctx); + let mut snaps = 0i32; + let mut frames = frames.into_iter(); + for n in 0..60 { + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events: frames.next().unwrap_or_default(), ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), zoom_with_wheel, true, true, owner, true) { + snaps += s; + } + }); + out.textures_delta.clear(); + } + snaps +} + +#[test] +fn fractional_point_events_accumulate_across_frames() { + let quarter = Vec2::new(0.0, 10.0); + let batched = snap_gesture(vec![vec![wheel_point(Vec2::new(0.0, 40.0), Modifiers::SHIFT)]], true, Id::unique("accum-batched")); + let split = snap_gesture( + vec![ + vec![wheel_point(quarter, Modifiers::SHIFT)], + vec![wheel_point(quarter, Modifiers::SHIFT)], + vec![wheel_point(quarter, Modifiers::SHIFT)], + vec![wheel_point(quarter, Modifiers::SHIFT)], + ], + true, + Id::unique("accum-split"), + ); + assert_eq!(batched, 1, "40 points batched → 1 step"); + assert_eq!(split, 1, "4 × 10 points split → 1 step"); +} + +#[test] +fn fractional_accumulation_negative_and_horizontal() { + let neg_quarter = Vec2::new(0.0, -10.0); + let neg_split = snap_gesture( + vec![ + vec![wheel_point(neg_quarter, Modifiers::SHIFT)], + vec![wheel_point(neg_quarter, Modifiers::SHIFT)], + vec![wheel_point(neg_quarter, Modifiers::SHIFT)], + vec![wheel_point(neg_quarter, Modifiers::SHIFT)], + ], + true, + Id::unique("accum-neg"), + ); + assert_eq!(neg_split, -1, "4 × −10 points → −1 step"); + let h_quarter = Vec2::new(10.0, 0.0); + let h_split = snap_gesture( + vec![ + vec![wheel_point(h_quarter, Modifiers::SHIFT)], + vec![wheel_point(h_quarter, Modifiers::SHIFT)], + vec![wheel_point(h_quarter, Modifiers::SHIFT)], + vec![wheel_point(h_quarter, Modifiers::SHIFT)], + ], + true, + Id::unique("accum-horiz"), + ); + assert_eq!(h_split, 1, "horizontal 4 × 10 → 1 step"); +} + +#[test] +fn accumulation_handles_empty_frames_and_direction_reversal() { + let snaps = snap_gesture( + vec![ + vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], + vec![], // empty frame + vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], + vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], + vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], + ], + true, + Id::unique("accum-empty"), + ); + assert_eq!(snaps, 1, "empty frame preserves residual"); + let reversal = snap_gesture( + vec![vec![wheel_point(Vec2::new(0.0, 30.0), Modifiers::SHIFT)], vec![wheel_point(Vec2::new(0.0, -15.0), Modifiers::SHIFT)]], + true, + Id::unique("accum-reversal"), + ); + assert_eq!(reversal, 0, "30 − 15 = 15 points, below 1 notch → 0 steps"); +} + +#[test] +fn accumulation_shift_alt_without_preference() { + let shift_alt = Modifiers { shift: true, alt: true, ..Modifiers::NONE }; + let split = snap_gesture( + vec![ + vec![wheel_point(Vec2::new(0.0, 10.0), shift_alt)], + vec![wheel_point(Vec2::new(0.0, 10.0), shift_alt)], + vec![wheel_point(Vec2::new(0.0, 10.0), shift_alt)], + vec![wheel_point(Vec2::new(0.0, 10.0), shift_alt)], + ], + false, + Id::unique("accum-shift-alt"), + ); + assert_eq!(split, 1, "Shift+Alt 4 × 10 → 1 step without preference"); +} + +#[test] +fn cross_viewport_does_not_leak_residual() { + let owner_a = Id::unique("owner-a"); + let owner_b = Id::unique("owner-b"); + let ctx = Context::default(); + configure(&ctx); + let mut snaps_a = 0i32; + let mut snaps_b = 0i32; + // Frame 0: 30 points over A (hovered), B not hovered. + let raw = RawInput { time: Some(0.0), predicted_dt: 1.0 / 60.0, events: vec![wheel_point(Vec2::new(0.0, 30.0), Modifiers::SHIFT)], ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_a, true) { + snaps_a += s; + } + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_b, false) { + snaps_b += s; + } + }); + out.textures_delta.clear(); + assert_eq!(snaps_a, 0, "A: 30 points, below 1 notch"); + assert_eq!(snaps_b, 0, "B: not hovered, nothing"); + // Frame 1: 10 points, pointer moved to B (B hovered, A not). + let raw = + RawInput { time: Some(1.0 / 60.0), predicted_dt: 1.0 / 60.0, events: vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_a, false) { + snaps_a += s; + } + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_b, true) { + snaps_b += s; + } + }); + out.textures_delta.clear(); + assert_eq!(snaps_b, 0, "B: only 10 points of its own, no snap"); + // Frame 2–4: 30 more points over B. + for n in 2..5 { + let raw = RawInput { + time: Some(f64::from(n) / 60.0), + predicted_dt: 1.0 / 60.0, + events: vec![wheel_point(Vec2::new(0.0, 10.0), Modifiers::SHIFT)], + ..Default::default() + }; + let mut out = ctx.run_ui(raw, |ui| { + let _ = read(ui.ctx(), true, true, true, owner_a, false); + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_b, true) { + snaps_b += s; + } + }); + out.textures_delta.clear(); + } + assert_eq!(snaps_b, 1, "B: 10+10+10+10 = 40 → 1 step"); + // Frame 5: switch back to A — A's residual was cleared on the viewport switch. + let raw = + RawInput { time: Some(5.0 / 60.0), predicted_dt: 1.0 / 60.0, events: vec![wheel_point(Vec2::new(0.0, 30.0), Modifiers::SHIFT)], ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner_a, true) { + snaps_a += s; + } + let _ = read(ui.ctx(), true, true, true, owner_b, false); + }); + out.textures_delta.clear(); + assert_eq!(snaps_a, 0, "A: fresh start after switch, 30 points, no snap yet"); +} + +#[test] +fn two_reads_same_owner_same_frame_do_not_double_count() { + let owner = Id::unique("same-owner"); + let ctx = Context::default(); + configure(&ctx); + let mut total_snaps = 0i32; + let raw = RawInput { time: Some(0.0), predicted_dt: 1.0 / 60.0, events: vec![wheel_point(Vec2::new(0.0, 20.0), Modifiers::SHIFT)], ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner, true) { + total_snaps += s; + } + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner, true) { + total_snaps += s; + } + }); + out.textures_delta.clear(); + assert_eq!(total_snaps, 0, "20 points read twice must not become 40"); + // Now add another 20 in the next frame — should snap since accumulated 20+20=40. + let raw = + RawInput { time: Some(1.0 / 60.0), predicted_dt: 1.0 / 60.0, events: vec![wheel_point(Vec2::new(0.0, 20.0), Modifiers::SHIFT)], ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::SnapZoom(s)) = read(ui.ctx(), true, true, true, owner, true) { + total_snaps += s; + } + }); + out.textures_delta.clear(); + assert_eq!(total_snaps, 1, "20 + 20 across two frames → 1 step"); +} + +#[test] +fn accumulation_hostile_numbers() { + for dy in [1e30, -1e30, 1e-10, -1e-10] { + let snaps = snap_gesture(vec![vec![wheel_point(Vec2::new(0.0, dy), Modifiers::SHIFT)]], true, Id::unique("accum-hostile")); + assert!(snaps.abs() <= 10, "hostile {dy} clamped: {snaps}"); + } +} + +#[test] +fn line_unit_snap_still_works_with_accumulation() { + let (_, _, snaps) = gesture(vec![vec![wheel(Vec2::new(0.0, 1.0), Modifiers::SHIFT)]], true, false); + assert_eq!(snaps, 1, "Line-unit notch produces 1 step"); + let (_, _, snaps) = gesture(vec![vec![wheel(Vec2::new(0.0, -1.0), Modifiers::SHIFT)]], true, false); + assert_eq!(snaps, -1, "Line-unit notch down produces −1 step"); +} + +/// Two fingers on a touch screen: down 20 points apart, spread to 40 (a ×2 pinch), lifted. +fn touch_pinch_frames() -> Vec> { + let touch = |id: u64, phase: TouchPhase, x: f32| Event::Touch { + device_id: egui::TouchDeviceId(1), + id: egui::TouchId(id), + phase, + pos: egui::pos2(x, 50.0), + force: None, + }; + vec![ + // egui only starts a multi-touch gesture once it knows where the pointer is. + vec![Event::PointerMoved(egui::pos2(40.0, 50.0))], + vec![touch(1, TouchPhase::Start, 40.0), touch(2, TouchPhase::Start, 60.0)], + vec![touch(1, TouchPhase::Move, 30.0), touch(2, TouchPhase::Move, 70.0)], + vec![touch(1, TouchPhase::End, 30.0), touch(2, TouchPhase::End, 70.0)], + ] +} + +/// The total zoom [`read_with`] reports over `frames`, Enable Gestures set to `touch_gestures`. +fn touch_zoom(frames: Vec>, touch_gestures: bool) -> f32 { + let owner = Id::unique("test-touch"); + let ctx = Context::default(); + configure(&ctx); + let mut zoom = 1.0; + for (n, events) in (0u32..).zip(frames) { + let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() }; + let mut out = ctx.run_ui(raw, |ui| { + if let Some(Wheel::Zoom(f)) = read_with(ui.ctx(), false, true, true, owner, true, true, touch_gestures) { + zoom *= f; + } + }); + out.textures_delta.clear(); + } + zoom +} + +/// Preferences › Enhanced Controls › Enable Gestures (#204): on (the default) a two-finger +/// pinch on a touch screen zooms; off, it leaves the view alone. +#[test] +fn enable_gestures_switches_touch_screen_pinch_zoom() { + let on = touch_zoom(touch_pinch_frames(), true); + assert!((on - 2.0).abs() < 1e-4, "on: the fingers zoom ×2: {on}"); + let off = touch_zoom(touch_pinch_frames(), false); + assert_eq!(off, 1.0, "off: the fingers don't zoom"); +} + +/// Enable Gestures off only silences the touch screen: a trackpad pinch still zooms, also in a +/// frame where two fingers rest on the screen (egui's `zoom_delta` would report theirs instead). +#[test] +fn enable_gestures_off_keeps_the_trackpad_pinch() { + let zoom = touch_zoom(vec![vec![Event::Zoom(1.25)]], false); + assert!((zoom - 1.25).abs() < 1e-4, "no fingers down: {zoom}"); + let mut frames = touch_pinch_frames(); + frames[2] = vec![Event::Zoom(1.25)]; + let zoom = touch_zoom(frames, false); + assert!((zoom - 1.25).abs() < 1e-4, "fingers resting: {zoom}"); +} diff --git a/crates/ui-egui/src/canvas/zoom_hold.rs b/crates/ui-egui/src/canvas/zoom_hold.rs new file mode 100644 index 0000000000..8da4535e01 --- /dev/null +++ b/crates/ui-egui/src/canvas/zoom_hold.rs @@ -0,0 +1,367 @@ +//! Animated Zoom: press and hold with the Zoom tool to zoom continuously (#6210). +//! +//! With Preferences › General › Animated Zoom on (the default, as in Photoshop) a press held +//! still past [`DELAY`] zooms in smoothly about the press point until the button comes up, or out +//! with ⌥. The release is then no click step. A quick click still steps the zoom (the canvas), a +//! drag still scrubs or draws a zoom rectangle (`zoom_tool.rs`), and with the preference off a +//! hold does nothing more than a click. +//! +//! The rate is per second of wall time, so the zoom grows the same at any frame rate. + +use egui::Pos2; + +use crate::canvas::ViewXform; +use crate::state::View; + +/// Seconds a press must be held still before it zooms continuously (a click is shorter). +pub const DELAY: f64 = 0.3; +/// Doublings (or halvings, zooming out) per second while held. +pub const RATE: f32 = 2.0; +/// Screen points the pointer may wander before the press counts as a drag instead. +const SLOP: f32 = 6.0; +/// The longest frame gap a hold zooms over: a stalled frame never jumps the zoom far. +const MAX_DT: f64 = 0.25; + +/// A Zoom-tool press being held. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Hold { + /// Screen point where the button went down. + anchor: Pos2, + /// The document point under it (it stays under the anchor). + doc: [f64; 2], + /// When the button went down (egui seconds). + t0: f64, + /// The time the zoom last advanced to, once zooming. + last: Option, + /// The pointer moved before the hold began: this press is a drag, never a hold. + dragged: bool, +} + +/// Where a canvas keeps its hold: each canvas (document `idx` of the tiled windows, in the main +/// or a secondary window) has its own, so a hold on one never zooms another. +pub fn hold_id(idx: usize, primary: bool, viewport: egui::ViewportId) -> egui::Id { + egui::Id::unique(("pc-zoom-tool-hold", idx, primary, viewport)) +} + +/// The zoom after holding for `dt` seconds at `zoom` (out: shrinking), within the range for a +/// document of `size` px. A non-finite or negative `dt` changes nothing. +pub fn hold_zoom(zoom: f32, dt: f64, out: bool, size: [u32; 2]) -> f32 { + let dt = if dt.is_finite() { dt.clamp(0.0, MAX_DT) as f32 } else { 0.0 }; + let e = if out { -RATE * dt } else { RATE * dt }; + crate::zoom_levels::clamp(zoom * e.exp2(), size) +} + +/// Is a held press zooming right now? (It owns the gesture: no scrub, no click step.) +pub fn active(ctx: &egui::Context, id: egui::Id) -> bool { + ctx.data(|d| d.get_temp::(id)).is_some_and(|h| h.last.is_some()) +} + +/// Advance the Zoom-tool hold of the canvas whose [`hold_id`] is `id` by one frame. `enabled` is +/// the Animated Zoom preference, `out` the zoom direction (⌥ or a temporary zoom-out key, read +/// live), `on_canvas` whether the canvas owns the press (not a dialog, a scrollbar or the compass +/// over it); only such a press can become a hold. Returns true when the press is a hold zoom (on +/// its release frame too), so the canvas skips the scrub and the click step. +#[allow(clippy::too_many_arguments)] +pub fn update(ctx: &egui::Context, view: &mut View, xf: &ViewXform, enabled: bool, out: bool, ppp: f32, on_canvas: bool, id: egui::Id) -> bool { + let (down, origin, start, pos, now) = + ctx.input(|i| (i.pointer.primary_down(), i.pointer.press_origin(), i.pointer.press_start_time(), i.pointer.latest_pos(), i.time)); + let held = ctx.data(|d| d.get_temp::(id)); + if !enabled || !down { + if held.is_some() { + ctx.data_mut(|d| d.remove::(id)); + } + return enabled && held.is_some_and(|h| h.last.is_some()); + } + let start = start.filter(|t| t.is_finite()); + // A hold left from an earlier press (the tool changed before its release, so no frame here + // saw the button come up) is not this press's. + let mine = |h: &Hold| origin == Some(h.anchor) && start.is_none_or(|t| t == h.t0); + let mut h = match held.filter(mine) { + Some(h) => h, + None => { + // A press that began on the canvas (not one dragged in from a panel) and that the + // canvas owns. + let Some(anchor) = origin.filter(|p| on_canvas && xf.rect.contains(*p)) else { + ctx.data_mut(|d| d.remove::(id)); + return false; + }; + let t0 = start.unwrap_or(now); + let h = Hold { anchor, doc: xf.to_doc(anchor), t0, last: None, dragged: false }; + ctx.data_mut(|d| d.insert_temp(id, h)); + h + } + }; + if h.dragged { + return false; + } + if h.last.is_none() { + // Moved before the hold began: a drag (scrubby zoom or rectangle), not a hold. + if pos.is_some_and(|p| p.distance(h.anchor) > SLOP) { + h.dragged = true; + ctx.data_mut(|d| d.insert_temp(id, h)); + return false; + } + let begin = h.t0 + DELAY; + if now < begin { + ctx.request_repaint_after(std::time::Duration::from_secs_f64((begin - now).clamp(0.0, DELAY))); + return false; + } + h.last = Some(begin); + } + let dt = now - h.last.unwrap_or(now); + let zoom = hold_zoom(view.zoom, dt, out, view.doc_size); + // At the zoom limit the view stays as it is (a window wrapped around the image centred it). + if zoom.to_bits() != view.zoom.to_bits() { + crate::zoom_tool::anchor_view(view, xf, h.anchor, h.doc, zoom, ppp); + } + h.last = Some(now); + ctx.data_mut(|d| d.insert_temp(id, h)); + ctx.request_repaint(); + true +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::PhotocraftApp; + use egui::{Event, PointerButton, vec2}; + use egui_kittest::Harness; + use serde_json::json; + + const DOC: [u32; 2] = [400, 300]; + + /// The hold of the harness's one canvas. + fn hid() -> egui::Id { + hold_id(0, true, egui::ViewportId::ROOT) + } + + #[test] + fn hold_zoom_is_frame_rate_independent_and_safe() { + // One second in one step or in 60 steps: two doublings either way. + let mut z = 1.0; + for _ in 0..60 { + z = hold_zoom(z, 1.0 / 60.0, false, DOC); + } + let big = hold_zoom(hold_zoom(hold_zoom(hold_zoom(1.0, 0.25, false, DOC), 0.25, false, DOC), 0.25, false, DOC), 0.25, false, DOC); + assert!((z - 4.0).abs() < 1e-3 && (big - 4.0).abs() < 1e-3, "{z} {big}"); + assert!((hold_zoom(1.0, 0.25, true, DOC) - 2f32.powf(-0.5)).abs() < 1e-5); + // Nothing for no time, a negative or broken dt, or a non-finite zoom (100 %). + assert_eq!(hold_zoom(1.5, 0.0, false, DOC), 1.5); + assert_eq!(hold_zoom(1.5, -3.0, false, DOC), 1.5); + assert_eq!(hold_zoom(1.5, f64::NAN, true, DOC), 1.5); + assert_eq!(hold_zoom(1.5, f64::INFINITY, true, DOC), 1.5); + assert_eq!(hold_zoom(f32::NAN, 0.1, false, DOC), 1.0); + // A stall is capped; the range is kept. + assert!((hold_zoom(1.0, 10.0, false, DOC) - 2f32.powf(0.5)).abs() < 1e-5); + assert_eq!(hold_zoom(crate::zoom_levels::MAX, 0.2, false, DOC), crate::zoom_levels::MAX); + let min = crate::zoom_levels::min(DOC); + assert_eq!(hold_zoom(min, 0.2, true, DOC), min); + assert_eq!(hold_zoom(1.0, 0.1, false, [0, 0]), hold_zoom(1.0, 0.1, false, [0, 0])); + } + + fn harness(animated: bool) -> Harness<'static, PhotocraftApp> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.run("prefs.set", json!({"values": {"general.animatedZoom": animated, "tools.zoomClickedPointToCenter": false}})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = false; + app.ui.tool = crate::state::Tool::Zoom; + app.ui.tool_options.zoom_scrubby = true; + // Each step is 0.1 s of egui time. + let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).with_step_dt(0.1).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui)); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]); + h.run_steps(4); + let view = &mut h.state_mut().ui.views[0]; + view.zoom = 1.0; + view.center = [200.0, 150.0]; + view.fit_pending = false; + view.fill_pending = false; + h.run_steps(2); + h + } + + fn xf(h: &Harness<'static, PhotocraftApp>) -> ViewXform { + ViewXform::active(h.state()).unwrap() + } + + fn button(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, pressed: bool, alt: bool) { + let modifiers = egui::Modifiers { alt, ..Default::default() }; + h.event(Event::ModifiersChanged(modifiers)); + h.event(Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers }); + h.step(); + } + + /// Press at `p`, hold still for `frames` steps, release; returns the zoom after every step, + /// then after the release step and two more frames. + fn hold(h: &mut Harness<'static, PhotocraftApp>, p: Pos2, frames: usize, alt: bool) -> Vec { + h.event(Event::PointerMoved(p)); + h.step(); + button(h, p, true, alt); + let mut zs = vec![h.state().current_zoom()]; + for _ in 0..frames { + h.step(); + zs.push(h.state().current_zoom()); + } + button(h, p, false, alt); + zs.push(h.state().current_zoom()); + h.run_steps(2); + zs.push(h.state().current_zoom()); + zs + } + + #[test] + fn holding_zooms_in_continuously_about_the_press_point() { + let mut h = harness(true); + let p = h.state().last_canvas_rect.center() + vec2(-120.0, 70.0); + let doc = xf(&h).to_doc(p); + let zs = hold(&mut h, p, 8, false); + // Nothing during the delay, then more every frame; the release adds no click step. + assert_eq!(zs[0], 1.0, "{zs:?}"); + let rising = &zs[4..=8]; + assert!(rising.windows(2).all(|w| w[1] > w[0]), "{zs:?}"); + // The release frame ends it: no click step, nothing more afterwards. + assert_eq!(zs[10], zs[9], "{zs:?}"); + assert!(zs[9] < zs[8] * 1.2, "{zs:?}"); + // About 0.5 s held past the delay at RATE doublings per second. + let expected = (RATE * 0.5).exp2(); + assert!((zs[8] - expected).abs() < 0.2, "{zs:?} vs {expected}"); + assert!(xf(&h).to_screen(doc[0] as f32, doc[1] as f32).distance(p) < 0.5); + assert_eq!(h.state().session.documents()[0].history.past_len(), 0); + } + + #[test] + fn alt_hold_zooms_out() { + let mut h = harness(true); + let p = h.state().last_canvas_rect.center() + vec2(50.0, -30.0); + let doc = xf(&h).to_doc(p); + let zs = hold(&mut h, p, 8, true); + assert!(zs[4..=8].windows(2).all(|w| w[1] < w[0]), "{zs:?}"); + assert_eq!(zs[10], zs[9], "{zs:?}"); + assert!(zs[9] > zs[8] / 1.2, "{zs:?}"); + assert!(xf(&h).to_screen(doc[0] as f32, doc[1] as f32).distance(p) < 0.5); + } + + #[test] + fn a_hold_at_the_zoom_limit_leaves_the_view_alone() { + // Zoom Resizes Windows centres the image in a window wrapped around it + // (zoom_window_fit.rs); a hold that can't zoom further must not move it back. + let mut h = harness(true); + let p = h.state().last_canvas_rect.center() + vec2(50.0, -30.0); + h.event(Event::PointerMoved(p)); + h.step(); + button(&mut h, p, true, true); + let min = crate::zoom_levels::min(DOC); + for _ in 0..200 { + if h.state().current_zoom() == min { + break; + } + h.step(); + } + assert_eq!(h.state().current_zoom(), min); + h.state_mut().ui.views[0].center = [200.0, 150.0]; + h.run_steps(3); + assert_eq!(h.state().ui.views[0].center, [200.0, 150.0]); + button(&mut h, p, false, true); + } + + #[test] + fn with_animated_zoom_off_a_hold_is_one_click_step() { + let mut h = harness(false); + let p = h.state().last_canvas_rect.center(); + // Shorter than egui's longest click, so the release is a click. + let zs = hold(&mut h, p, 5, false); + assert!(zs[..=5].iter().all(|&z| z == 1.0), "{zs:?}"); + assert_eq!(*zs.last().unwrap(), crate::zoom_levels::step(1.0, 1, DOC)); + } + + #[test] + fn a_quick_click_steps_and_a_drag_still_scrubs() { + let mut h = harness(true); + let p = h.state().last_canvas_rect.center(); + let zs = hold(&mut h, p, 0, false); + assert_eq!(*zs.last().unwrap(), crate::zoom_levels::step(1.0, 1, DOC), "{zs:?}"); + // Scrubby Zoom: a drag zooms by its distance however long it is held, never by time. + let z0 = h.state().current_zoom(); + h.event(Event::PointerMoved(p)); + h.step(); + button(&mut h, p, true, false); + h.event(Event::PointerMoved(p + vec2(50.0, 0.0))); + h.step(); + for _ in 0..6 { + h.step(); + } + let expected = crate::zoom_tool::scrub_zoom(z0, 50.0, DOC); + assert!((h.state().current_zoom() - expected).abs() < 1e-4, "{} vs {expected}", h.state().current_zoom()); + button(&mut h, p + vec2(50.0, 0.0), false, false); + h.run_steps(2); + assert!((h.state().current_zoom() - expected).abs() < 1e-4); + assert!(!active(&h.ctx, hid())); + } + + #[test] + fn a_hold_is_only_reused_by_its_own_press_on_its_own_canvas() { + // The Hold only goes away on a Zoom-tool frame with the button up. When the tool changed + // before the release, the next Zoom press can be the first such frame: it is a new press. + let h = harness(true); + let (xf, view) = (xf(&h), h.state().ui.views[0].clone()); + let p = xf.rect.center(); + let press = |t: f64| egui::RawInput { + time: Some(t), + events: vec![Event::PointerMoved(p), Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Default::default() }], + ..Default::default() + }; + let elsewhere = p + vec2(-100.0, 30.0); + let outside = xf.rect.right_bottom() + vec2(50.0, 50.0); + for stale in [ + Hold { anchor: elsewhere, doc: xf.to_doc(elsewhere), t0: 1.0, last: Some(1.6), dragged: false }, + Hold { anchor: elsewhere, doc: xf.to_doc(elsewhere), t0: 1.0, last: None, dragged: true }, + // Same point, earlier press. + Hold { anchor: p, doc: xf.to_doc(p), t0: 1.0, last: Some(1.6), dragged: false }, + // A hold on another (tiled) canvas. + Hold { anchor: outside, doc: [0.0, 0.0], t0: 10.0, last: Some(10.0), dragged: false }, + ] { + let ctx = egui::Context::default(); + ctx.data_mut(|d| d.insert_temp(hid(), stale)); + let mut v = view.clone(); + ctx.begin_pass(press(10.0)); + let zooming = update(&ctx, &mut v, &xf, true, false, 1.0, true, hid()); + ctx.end_pass().textures_delta.clear(); + assert!(!zooming, "{stale:?}"); + assert_eq!(v.zoom, view.zoom, "{stale:?}"); + let fresh = ctx.data(|d| d.get_temp::(hid())); + assert_eq!(fresh.map(|h| (h.anchor, h.last, h.dragged)), Some((p, None, false)), "{stale:?}"); + } + // Tiled windows: canvas 0's hold zooms canvas 0 only. Canvas 1 (which doesn't own the + // press) neither picks it up nor ends it. + let ctx = egui::Context::default(); + let ours = Hold { anchor: p, doc: xf.to_doc(p), t0: 10.0, last: Some(10.0), dragged: false }; + ctx.data_mut(|d| d.insert_temp(hid(), ours)); + let mut v = view.clone(); + ctx.begin_pass(press(10.0)); + let other = hold_id(1, true, egui::ViewportId::ROOT); + assert!(!update(&ctx, &mut v, &xf, true, false, 1.0, false, other)); + ctx.end_pass().textures_delta.clear(); + assert_eq!(v.zoom, view.zoom); + assert_eq!(ctx.data(|d| d.get_temp::(hid())), Some(ours)); + } + + #[test] + fn a_press_the_canvas_does_not_own_never_becomes_a_hold() { + let mut h = harness(true); + crate::dialogs::open_command_dialog(h.state_mut(), "image.adjustments.brightnessContrast", "Brightness/Contrast…"); + h.run_steps(3); + assert_eq!(h.state().ui.dialogs.len(), 1); + let p = h.state().last_canvas_rect.center() + vec2(-120.0, 70.0); + let zs = hold(&mut h, p, 8, false); + assert!(zs.iter().all(|&z| z == 1.0), "{zs:?}"); + assert!(!active(&h.ctx, hid())); + } +} diff --git a/crates/ui-egui/src/zoom_levels.rs b/crates/ui-egui/src/canvas/zoom_levels.rs similarity index 100% rename from crates/ui-egui/src/zoom_levels.rs rename to crates/ui-egui/src/canvas/zoom_levels.rs diff --git a/crates/ui-egui/src/canvas/zoom_window_fit.rs b/crates/ui-egui/src/canvas/zoom_window_fit.rs new file mode 100644 index 0000000000..75e2f7495f --- /dev/null +++ b/crates/ui-egui/src/canvas/zoom_window_fit.rs @@ -0,0 +1,434 @@ +//! Preferences › General › Zoom Resizes Windows, which is also the Zoom tool's Resize Windows to +//! Fit (#204). +//! +//! As in Photoshop the two are one setting, so turning it on in either place shows it on in the +//! other. With it on, zooming a document that floats in its own window resizes that window to wrap +//! the image at the new zoom, no bigger than the screen it is on; with it off (the default) the +//! image zooms inside a window that keeps its size. Tabbed documents never resize the main window. +//! +//! Only a change of zoom resizes. Fit on Screen derives the zoom from the window, so a fit leaves +//! the window alone and the two can never chase each other. +//! +//! A zoom about the pointer leaves the image off centre, so a window grown to wrap it also +//! re-centres the image in what the scrollbars leave free; a window the screen caps keeps the +//! zoomed spot where it is. + +use egui::{Context, Id, Pos2, Rect, Vec2, ViewportClass, ViewportCommand, ViewportId, vec2}; + +use crate::PhotocraftApp; +use crate::state::{DocWindow, View}; + +/// The smallest window a zoom asks for (points), so zooming far out keeps the window usable. +pub const MIN_SIZE: Vec2 = vec2(240.0, 160.0); + +/// Room left on the screen for the window's title bar and the taskbar or Dock (points). +pub const SCREEN_MARGIN: f32 = 48.0; + +/// What wrapping a document window needs besides the view itself. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Layout { + /// The window's device pixels per egui point (`View::zoom` is in device pixels). + pub ppp: f32, + /// View › Pixel Aspect Ratio Correction: document pixels are shown this much wider. + pub aspect: f32, + /// The window's own chrome around the image, the rulers, in points. + pub chrome: Vec2, + /// The scrollbars along the right and bottom (points; 0 when hidden). They are drawn over the + /// canvas, so the window makes room for them too. + pub bars: f32, + /// View › Flip Horizontal. + pub flip: bool, + /// The size of the screen the window is on, in points, when the platform reports it. + pub monitor: Option, +} + +/// The inner size (points) of a window that wraps `view`'s document at its zoom and rotation, +/// at least [`MIN_SIZE`] but never bigger than the screen, even a screen smaller than that +/// minimum. `None` until the view has a document size. +pub fn wrapped_size(view: &View, layout: &Layout) -> Option { + wrap(view, layout).map(|(_, size)| size) +} + +/// The window size the image and chrome need, and the size [`wrapped_size`] asks for. +fn wrap(view: &View, layout: &Layout) -> Option<(Vec2, Vec2)> { + let usable = |x: f32| x.is_finite() && x > 0.0; + if !usable(view.zoom) || !usable(layout.ppp) { + return None; + } + let aspect = if usable(layout.aspect) { layout.aspect } else { 1.0 }; + let w = view.doc_size[0] as f32 * aspect * view.zoom / layout.ppp; + let h = view.doc_size[1] as f32 * view.zoom / layout.ppp; + if !usable(w) || !usable(h) { + return None; + } + // A turned view (View › Rotate View) shows the image's bounding box. + let (sin, cos) = view.rotation.to_radians().sin_cos(); + let (sin, cos) = (sin.abs(), cos.abs()); + let image = vec2(w * cos + h * sin, w * sin + h * cos); + let bars = if usable(layout.bars) { layout.bars } else { 0.0 }; + let chrome = if layout.chrome.is_finite() { layout.chrome.max(Vec2::ZERO) } else { Vec2::ZERO } + Vec2::splat(bars); + // The screen is the hard cap: applied last, it wins over the minimum on a tiny screen. + let screen = + layout.monitor.filter(|m| usable(m.x) && usable(m.y)).map_or(Vec2::splat(f32::INFINITY), |m| (m - Vec2::splat(SCREEN_MARGIN)).max(Vec2::splat(1.0))); + Some((image + chrome, (image + chrome).max(MIN_SIZE).min(screen))) +} + +/// The view centre that shows the whole image in the middle of a window of [`wrapped_size`]: +/// the image centred in the canvas less its scrollbars. `None` when the screen capped the window +/// and the image doesn't fit, so the zoomed spot stays put. +pub fn wrapped_center(view: &View, layout: &Layout) -> Option<[f32; 2]> { + let (needed, size) = wrap(view, layout)?; + if needed.x > size.x + 0.5 || needed.y > size.y + 0.5 { + return None; + } + let bars = if layout.bars.is_finite() && layout.bars > 0.0 { layout.bars } else { 0.0 }; + let middle = [view.doc_size[0] as f32 / 2.0, view.doc_size[1] as f32 / 2.0]; + // The canvas centre sits half a bar right of and below the middle of the free area. + let xf = crate::canvas::ViewXform { + rect: Rect::from_center_size(Pos2::ZERO, Vec2::ZERO), + zoom: view.zoom / layout.ppp, + center: middle, + flip: layout.flip, + rotation: view.rotation, + aspect: layout.aspect, + }; + let [x, y] = xf.to_doc(Pos2::new(bars / 2.0, bars / 2.0)); + let center = [x as f32, y as f32]; + center.iter().all(|c| c.is_finite()).then_some(center) +} + +/// Where the zoom a window was last wrapped for (or first seen at) is kept between frames. +fn last_zoom_id(window: u64) -> Id { + Id::unique(("pc-zoom-resizes-windows", window)) +} + +/// The inner size floating window `window` should take this frame, if any: when `on` (the +/// preference) and its zoom moved from `before` (its view as the frame began) to `after` (the view +/// its canvas left), other than by a fit. Every call records the zoom, so the first frame of a new +/// window and the frames with the preference off never resize later. +pub fn resize_for(ctx: &Context, window: u64, on: bool, before: &View, after: &View, layout: &Layout) -> Option { + let id = last_zoom_id(window); + let last = ctx.data_mut(|d| { + let last = d.get_temp::(id); + d.insert_temp(id, after.zoom); + last + }); + let fitting = before.fit_pending || before.fill_pending || after.fit_pending || after.fill_pending; + // Compare the bits: any change of zoom counts, and NaN never matches itself into a resize loop. + if !on || fitting || last?.to_bits() == after.zoom.to_bits() { + return None; + } + wrapped_size(after, layout) +} + +/// Called by a floating document window (`canvas::extra_windows`) after its canvas drew in `rect`: +/// with Zoom Resizes Windows on, a zoom resizes the window around the image. A window drawn +/// embedded in the main one (a backend without multiple windows, the web) is never resized, as the +/// command would resize the main window instead, and a maximized or full-screen one keeps the +/// size the user gave it. A window that grows around the whole image also centres it in `after`. +/// Returns the size asked for. +pub fn follow( + app: &PhotocraftApp, + ctx: &Context, + class: ViewportClass, + viewport: ViewportId, + window: &DocWindow, + after: &mut View, + rect: Rect, +) -> Option { + if class != ViewportClass::Immediate { + return None; + } + // Still recorded while maximized, so leaving it doesn't resize for a zoom made meanwhile. + let pinned = ctx.input(|i| i.viewport().maximized == Some(true) || i.viewport().fullscreen == Some(true)); + let layout = Layout { + ppp: ctx.pixels_per_point(), + aspect: app.ui.view.display_aspect(), + chrome: rect.size() - crate::rulers::content_rect(app, rect).size(), + bars: if app.ui.view.shows_scrollbars() { crate::scrollbars::THICKNESS } else { 0.0 }, + flip: app.ui.view.flip_horizontal, + monitor: ctx.input(|i| i.viewport().monitor_size), + }; + let on = app.session.prefs().general.zoom_resizes_windows && !pinned; + let size = resize_for(ctx, window.id, on, &window.view, after, &layout)?; + ctx.send_viewport_cmd_to(viewport, ViewportCommand::InnerSize(size)); + if let Some(center) = wrapped_center(after, &layout) { + after.center = center; + // The canvas drew before this: show the centred view on the next frame. + ctx.request_repaint_of(viewport); + } + Some(size) +} + +#[cfg(test)] +mod tests { + use serde_json::json; + + use super::*; + + fn view(doc_size: [u32; 2], zoom: f32) -> View { + View { zoom, doc_size, fit_pending: false, ..View::default() } + } + + fn layout() -> Layout { + Layout { ppp: 1.0, aspect: 1.0, chrome: Vec2::ZERO, bars: 0.0, flip: false, monitor: Some(vec2(1920.0, 1080.0)) } + } + + #[test] + fn the_window_wraps_the_image_at_its_zoom() { + assert_eq!(wrapped_size(&view([400, 300], 1.0), &layout()), Some(vec2(400.0, 300.0))); + assert_eq!(wrapped_size(&view([400, 300], 2.0), &layout()), Some(vec2(800.0, 600.0))); + // `zoom` is device pixels: on a 2× display 100% is half as many points. + assert_eq!(wrapped_size(&view([800, 600], 1.0), &Layout { ppp: 2.0, ..layout() }), Some(vec2(400.0, 300.0))); + // The rulers sit inside the window, around the image. + assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { chrome: vec2(16.0, 16.0), ..layout() }), Some(vec2(416.0, 316.0))); + // Pixel Aspect Ratio Correction shows the pixels wider. + assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { aspect: 2.0, ..layout() }), Some(vec2(800.0, 300.0))); + } + + #[test] + fn the_window_stays_between_the_minimum_and_the_screen() { + assert_eq!(wrapped_size(&view([400, 300], 0.05), &layout()), Some(MIN_SIZE)); + let screen = vec2(1920.0, 1080.0) - Vec2::splat(SCREEN_MARGIN); + assert_eq!(wrapped_size(&view([400, 300], 16.0), &layout()), Some(screen)); + // One axis can be clamped while the other still wraps. + assert_eq!(wrapped_size(&view([4000, 300], 1.0), &layout()), Some(vec2(screen.x, 300.0))); + // Without a reported screen only the minimum applies. + assert_eq!(wrapped_size(&view([400, 300], 16.0), &Layout { monitor: None, ..layout() }), Some(vec2(6400.0, 4800.0))); + // A nonsense screen size is ignored rather than shrinking the window to nothing. + assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { monitor: Some(vec2(0.0, f32::NAN)), ..layout() }), Some(vec2(400.0, 300.0))); + // A screen smaller than the minimum still caps the window: it never outgrows the screen. + let tiny = Layout { monitor: Some(vec2(260.0, 190.0)), ..layout() }; + assert_eq!(wrapped_size(&view([400, 300], 1.0), &tiny), Some(vec2(260.0, 190.0) - Vec2::splat(SCREEN_MARGIN))); + assert_eq!(wrapped_size(&view([400, 300], 0.05), &tiny), Some(vec2(260.0, 190.0) - Vec2::splat(SCREEN_MARGIN))); + } + + #[test] + fn a_turned_view_wraps_the_bounding_box() { + let turned = View { rotation: 90.0, ..view([400, 300], 1.0) }; + let size = wrapped_size(&turned, &layout()).unwrap(); + assert!((size - vec2(300.0, 400.0)).length() < 0.01, "{size:?}"); + let turned = View { rotation: 45.0, ..view([400, 400], 1.0) }; + let size = wrapped_size(&turned, &layout()).unwrap(); + assert!((size.x - 400.0 * std::f32::consts::SQRT_2).abs() < 0.01, "{size:?}"); + } + + #[test] + fn nothing_to_wrap_asks_for_nothing() { + assert_eq!(wrapped_size(&view([0, 0], 1.0), &layout()), None, "the view hasn't shown its document yet"); + assert_eq!(wrapped_size(&view([400, 300], 0.0), &layout()), None); + assert_eq!(wrapped_size(&view([400, 300], f32::NAN), &layout()), None); + assert_eq!(wrapped_size(&view([400, 300], 1.0), &Layout { ppp: 0.0, ..layout() }), None); + } + + #[test] + fn only_a_zoom_change_with_the_preference_on_resizes() { + let ctx = Context::default(); + let at = |zoom| view([400, 300], zoom); + // The first frame of a window only records its zoom. + assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(1.0), &layout()), None); + // The same zoom again: nothing to do. + assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(1.0), &layout()), None); + // Zoom in (the Zoom tool, the wheel, Window › Arrange › Match Zoom): the window follows. + assert_eq!(resize_for(&ctx, 7, true, &at(1.0), &at(2.0), &layout()), Some(vec2(800.0, 600.0))); + assert_eq!(resize_for(&ctx, 7, true, &at(2.0), &at(2.0), &layout()), None, "once per change"); + // A zoom changed outside the canvas (Match Zoom) still counts on the next frame. + assert_eq!(resize_for(&ctx, 7, true, &at(0.5), &at(0.5), &layout()), Some(vec2(240.0, 160.0))); + // Another window keeps its own record. + assert_eq!(resize_for(&ctx, 8, true, &at(0.5), &at(0.5), &layout()), None); + } + + #[test] + fn with_the_preference_off_the_window_keeps_its_size() { + let ctx = Context::default(); + let at = |zoom| view([400, 300], zoom); + assert_eq!(resize_for(&ctx, 1, false, &at(1.0), &at(1.0), &layout()), None); + assert_eq!(resize_for(&ctx, 1, false, &at(1.0), &at(3.0), &layout()), None); + // Turning it on later doesn't catch up on zooms made while it was off. + assert_eq!(resize_for(&ctx, 1, true, &at(3.0), &at(3.0), &layout()), None); + assert_eq!(resize_for(&ctx, 1, true, &at(3.0), &at(1.0), &layout()), Some(vec2(400.0, 300.0))); + } + + #[test] + fn a_fit_never_resizes_the_window() { + let ctx = Context::default(); + let at = |zoom| view([400, 300], zoom); + assert_eq!(resize_for(&ctx, 2, true, &at(1.0), &at(1.0), &layout()), None); + // The fit takes its zoom from the window; resizing for it would start a loop. + let fitting = View { fit_pending: true, ..at(1.0) }; + assert_eq!(resize_for(&ctx, 2, true, &fitting, &at(0.4), &layout()), None); + let filling = View { fill_pending: true, ..at(0.4) }; + assert_eq!(resize_for(&ctx, 2, true, &filling, &at(0.6), &layout()), None); + // The zoom the fit chose is the new baseline. + assert_eq!(resize_for(&ctx, 2, true, &at(0.6), &at(0.6), &layout()), None); + } + + #[test] + fn follow_resizes_only_real_floating_windows_with_the_preference_on() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.ui.extras.rulers = false; + let ctx = Context::default(); + let viewport = ViewportId::from_hash_of(("docwin", 3_u64)); + let rect = Rect::from_min_size(egui::Pos2::ZERO, vec2(800.0, 600.0)); + let at = |zoom| view([400, 300], zoom); + let window = DocWindow { id: 3, document: 0, view: at(1.0), open: true, position: None, floating: false, focused: false }; + let frame = |app: &PhotocraftApp, class, after: &View| follow(app, &ctx, class, viewport, &window, &mut after.clone(), rect); + // Off by default, as in Photoshop. + assert!(!app.session.prefs().general.zoom_resizes_windows); + assert_eq!(frame(&app, ViewportClass::Immediate, &at(1.0)), None); + assert_eq!(frame(&app, ViewportClass::Immediate, &at(2.0)), None); + app.run("prefs.set", json!({"path": "general.zoomResizesWindows", "value": true})).unwrap(); + // The scrollbars are drawn over the canvas: the window makes room for them. + assert!(app.ui.view.shows_scrollbars()); + let bars = Vec2::splat(crate::scrollbars::THICKNESS); + assert_eq!(frame(&app, ViewportClass::Immediate, &at(1.0)), Some(vec2(400.0, 300.0) + bars)); + // Drawn inside the main window, a resize command would resize the main window: skipped. + assert_eq!(frame(&app, ViewportClass::EmbeddedWindow, &at(2.0)), None); + // With the rulers on, the window makes room for them. + app.ui.extras.rulers = true; + let ruled = frame(&app, ViewportClass::Immediate, &at(2.0)).unwrap(); + assert!(ruled.x > 800.0 && ruled.y > 600.0, "{ruled:?}"); + } + + /// Where the image lands in a window of the wrapped size, and the canvas area the scrollbars + /// leave free, both in points from the window's top-left. + fn landing(view: &View, layout: &Layout) -> (Rect, Rect) { + let size = wrapped_size(view, layout).unwrap(); + let canvas = Rect::from_min_max(Pos2::ZERO + layout.chrome, Pos2::ZERO + size); + let free = Rect::from_min_max(canvas.min, canvas.max - Vec2::splat(layout.bars)); + let xf = crate::canvas::ViewXform { + rect: canvas, + zoom: view.zoom / layout.ppp, + center: view.center, + flip: layout.flip, + rotation: view.rotation, + aspect: layout.aspect, + }; + (xf.image_bounds(view.doc_size), free) + } + + fn near(a: Rect, b: Rect) -> bool { + (a.min - b.min).length() < 0.01 && (a.max - b.max).length() < 0.01 + } + + #[test] + fn the_window_makes_room_for_the_scrollbars() { + let barred = Layout { bars: 12.0, chrome: vec2(16.0, 16.0), ..layout() }; + assert_eq!(wrapped_size(&view([400, 300], 1.0), &barred), Some(vec2(428.0, 328.0))); + } + + #[test] + fn a_wrapped_window_shows_the_whole_image_centred() { + // Zoomed in about the top-left corner: the view centre is far from the image's middle. + let zoomed = View { center: [60.0, 40.0], ..view([400, 300], 2.0) }; + for layout in [ + layout(), + Layout { bars: 12.0, chrome: vec2(16.0, 16.0), ..layout() }, + Layout { bars: 12.0, flip: true, ..layout() }, + Layout { bars: 12.0, ppp: 2.0, aspect: 1.5, ..layout() }, + ] { + for rotation in [0.0, 90.0, -30.0] { + let mut v = View { rotation, ..zoomed.clone() }; + let (before, free) = landing(&v, &layout); + assert!(!near(before, free), "{layout:?} {rotation}: off centre before"); + v.center = wrapped_center(&v, &layout).unwrap(); + let (after, free) = landing(&v, &layout); + assert!(near(after, free), "{layout:?} {rotation}: {after:?} fills {free:?}"); + } + } + // Zoomed far out the window keeps its minimum size, and the image sits in its middle. + let small = View { center: [0.0, 0.0], ..view([400, 300], 0.1) }; + let c = wrapped_center(&small, &layout()).unwrap(); + assert!((c[0] - 200.0).abs() < 0.01 && (c[1] - 150.0).abs() < 0.01, "{c:?}"); + } + + #[test] + fn a_window_the_screen_caps_keeps_the_zoomed_spot() { + let zoomed = View { center: [60.0, 40.0], ..view([400, 300], 16.0) }; + assert_eq!(wrapped_center(&zoomed, &layout()), None); + } + + #[test] + fn follow_centres_the_image_in_the_grown_window() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.run("prefs.set", json!({"path": "general.zoomResizesWindows", "value": true})).unwrap(); + let ctx = Context::default(); + let viewport = ViewportId::from_hash_of(("docwin", 4_u64)); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let window = DocWindow { id: 4, document: 0, view: view([400, 300], 1.0), open: true, position: None, floating: false, focused: false }; + let mut first = view([400, 300], 1.0); + assert_eq!(follow(&app, &ctx, ViewportClass::Immediate, viewport, &window, &mut first, rect), None); + // A Zoom tool click near the top-left corner doubles the zoom about that spot. + let mut zoomed = View { center: [60.0, 40.0], ..view([400, 300], 2.0) }; + assert!(follow(&app, &ctx, ViewportClass::Immediate, viewport, &window, &mut zoomed, rect).is_some()); + let half_bar = crate::scrollbars::THICKNESS / 2.0 / 2.0; + assert_eq!(zoomed.center, [200.0 + half_bar, 150.0 + half_bar], "the whole image, centred"); + assert_eq!(zoomed.zoom, 2.0); + } + + #[test] + fn a_maximized_or_full_screen_window_keeps_its_size() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + app.sync_views(); + app.run("prefs.set", json!({"path": "general.zoomResizesWindows", "value": true})).unwrap(); + let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)); + let window = DocWindow { id: 5, document: 0, view: view([400, 300], 1.0), open: true, position: None, floating: false, focused: false }; + for (maximized, fullscreen) in [(true, false), (false, true)] { + let ctx = Context::default(); + let mut raw = egui::RawInput::default(); + let info = egui::ViewportInfo { maximized: Some(maximized), fullscreen: Some(fullscreen), ..Default::default() }; + raw.viewports.insert(ViewportId::ROOT, info); + let viewport = ViewportId::ROOT; + let mut sizes = Vec::new(); + let frame = |raw: egui::RawInput, zoom: f32, sizes: &mut Vec>| { + let mut out = ctx.run_ui(raw, |ui| { + let mut v = View { center: [60.0, 40.0], ..view([400, 300], zoom) }; + sizes.push(follow(&app, ui.ctx(), ViewportClass::Immediate, viewport, &window, &mut v, rect)); + assert_eq!(v.center, [60.0, 40.0]); + }); + out.textures_delta.clear(); + }; + let mut normal = egui::RawInput::default(); + normal.viewports.insert(ViewportId::ROOT, egui::ViewportInfo { maximized: Some(false), fullscreen: Some(false), ..Default::default() }); + frame(normal.clone(), 1.0, &mut sizes); + frame(raw, 2.0, &mut sizes); + // Back to a normal window: the zoom made meanwhile is not caught up on. + frame(normal, 2.0, &mut sizes); + assert_eq!(sizes, [None, None, None], "maximized {maximized}, full screen {fullscreen}"); + } + } + + #[test] + fn the_zoom_tool_options_bar_shows_the_same_setting() { + // Photoshop's Zoom tool option Resize Windows to Fit is Preferences › Zoom Resizes Windows. + use egui_kittest::Harness; + use egui_kittest::kittest::Queryable; + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 64, "height": 64})).unwrap(); + app.sync_views(); + app.ui.tool = crate::state::Tool::Zoom; + let mut h = Harness::builder().with_size(egui::vec2(1400.0, 60.0)).build_ui_state( + |ui, app: &mut PhotocraftApp| { + if !ui.ctx().fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) { + return; + } + crate::panels::options_bar(app, ui); + }, + app, + ); + PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::default()); + h.run_steps(4); + assert!(!h.state().session.prefs().general.zoom_resizes_windows); + h.get_by_label("Resize Windows to Fit").click(); + h.run_steps(2); + assert!(h.state().session.prefs().general.zoom_resizes_windows, "the option turns the preference on"); + h.get_by_label("Resize Windows to Fit").click(); + h.run_steps(2); + assert!(!h.state().session.prefs().general.zoom_resizes_windows, "and off again"); + } +} diff --git a/crates/ui-egui/src/canvas_menu_tests.rs b/crates/ui-egui/src/canvas_menu_tests.rs index f1bbbe8d0c..dfed592653 100644 --- a/crates/ui-egui/src/canvas_menu_tests.rs +++ b/crates/ui-egui/src/canvas_menu_tests.rs @@ -119,7 +119,18 @@ fn check_reopening(tool: Tool, modifiers: Modifiers, popup: Popup) { #[test] fn secondary_taps_keep_selection_menus_open() { - for tool in [Tool::RectMarquee, Tool::EllipseMarquee, Tool::Lasso, Tool::PolygonLasso, Tool::MagneticLasso, Tool::MagicWand, Tool::ObjectSelection] { + // Quick Selection too, though it has a brush: Photoshop's right-click there is the selection + // menu (#7268), the brush is set from the options bar. + for tool in [ + Tool::RectMarquee, + Tool::EllipseMarquee, + Tool::Lasso, + Tool::PolygonLasso, + Tool::MagneticLasso, + Tool::MagicWand, + Tool::ObjectSelection, + Tool::QuickSelection, + ] { check_reopening(tool, Modifiers::NONE, Popup::Tool); } } @@ -130,6 +141,30 @@ fn secondary_taps_keep_canvas_layer_menus_open() { check_reopening(Tool::RectMarquee, Modifiers::COMMAND, Popup::Layers); } +/// A macOS Control-click (control_click.rs) reaches the canvas as a right-click that still holds +/// Control: it opens the same menu as a plain right-click and paints nothing. +#[test] +fn control_clicks_open_the_right_click_menus() { + for (tool, popup) in [(Tool::RectMarquee, Popup::Tool), (Tool::Pen, Popup::Tool), (Tool::Move, Popup::Layers), (Tool::Brush, Popup::Brush)] { + let mut h = harness(tool, true, 1.0); + let at = fractional_point(&h, 1.0); + let before = document_state(&h); + let mut clicks = crate::control_click::ControlClick::default(); + h.event(Event::ModifiersChanged(Modifiers::CTRL)); + h.event(Event::PointerMoved(at)); + h.run_steps(2); + for pressed in [true, false] { + let mut events = vec![Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Modifiers::CTRL }]; + clicks.normalize(&mut events, true); + h.input_mut().events.extend(events); + h.run_steps(1); + } + h.run_steps(3); + assert!(is_open(&h, popup), "{tool:?}"); + assert!(document_state(&h) == before, "{tool:?}: a Control-click paints nothing"); + } +} + #[test] fn reopened_canvas_menus_still_choose_their_actions() { let mut h = harness(Tool::RectMarquee, true, 1.0); @@ -150,7 +185,7 @@ fn reopened_canvas_menus_still_choose_their_actions() { dismiss_outside(&mut h, Popup::Layers); tap(&mut h, at, Modifiers::NONE, true); assert!(is_open(&h, Popup::Layers)); - let menu_rect = h.ctx.memory(|m| m.area_rect(egui::Id::new("canvas-layer-menu"))).unwrap(); + let menu_rect = h.ctx.memory(|m| m.area_rect(egui::Id::unique("canvas-layer-menu"))).unwrap(); h.query_all_by_label("Background").find(|node| menu_rect.contains(node.rect().center())).expect("Background in the layer menu").click(); h.run_steps(3); assert!(!is_open(&h, Popup::Layers)); @@ -229,7 +264,8 @@ fn control_channel_opening_uses_the_same_dismissal_guard() { (Tool::RectMarquee, Popup::Tool, true), (Tool::Brush, Popup::Brush, false), (Tool::Eraser, Popup::Brush, false), - (Tool::QuickSelection, Popup::Brush, false), + // Its right-click is the selection menu (#7268). + (Tool::QuickSelection, Popup::Tool, false), ] { for scale in [1.0, 2.0] { let mut h = harness(tool, true, scale); @@ -248,16 +284,32 @@ fn control_channel_opening_uses_the_same_dismissal_guard() { } } +/// #7324: an agent's right-click on committed type opens the type layer menu, as on the canvas. +#[test] +fn control_channel_right_click_on_committed_type_opens_the_type_layer_menu() { + let mut h = harness(Tool::Type, false, 1.0); + h.state_mut().run("type.create", json!({"text": "Hello", "size": 72, "x": 100, "y": 180})).unwrap(); + h.run_steps(3); + let at = fractional_point(&h, 1.0); + let before = document_state(&h); + control_tap(&mut h, at, Popup::Tool); + let menu = h.state().ui.canvas_tool_menu.clone().unwrap(); + assert!(menu.type_layer && !menu.text); + assert!(h.state().ui.text_edit.is_none()); + // The new type is already the active layer, so the menu selects nothing either. + assert!(document_state(&h) == before, "opening the menu changes nothing"); +} + #[test] fn brush_picker_taps_preserve_pixels_selection_and_history_after_reopening() { - for tool in [Tool::Brush, Tool::Eraser, Tool::QuickSelection] { + for tool in [Tool::Brush, Tool::Eraser] { check_reopening(tool, Modifiers::NONE, Popup::Brush); } } #[test] fn brush_chip_toggles_after_canvas_and_control_opening() { - for tool in [Tool::Brush, Tool::Eraser, Tool::QuickSelection] { + for tool in [Tool::Brush, Tool::Eraser] { for scale in [1.0, 2.0] { let mut h = harness(tool, true, scale); let before = document_state(&h); @@ -283,13 +335,34 @@ fn brush_chip_toggles_after_canvas_and_control_opening() { } } +/// #7268: the Quick Selection tool's brush chip still opens and closes the Brush Preset picker; +/// only its canvas right-click is the selection menu. +#[test] +fn quick_selection_chip_toggles_the_picker_and_right_click_opens_the_selection_menu() { + let mut h = harness(Tool::QuickSelection, true, 1.0); + let before = document_state(&h); + for _ in 0..2 { + h.get_by_label(crate::i18n::t("Brush Preset picker")).click(); + h.run_steps(3); + assert!(h.state().ui.brush_picker.is_some(), "the chip opens the picker"); + h.get_by_label(crate::i18n::t("Brush Preset picker")).click(); + h.run_steps(3); + assert!(h.state().ui.brush_picker.is_none(), "the chip closes it"); + } + let at = fractional_point(&h, 1.0); + tap(&mut h, at, Modifiers::NONE, true); + assert!(h.state().ui.brush_picker.is_none(), "no picker on a right-click"); + assert!(h.state().ui.canvas_tool_menu.as_ref().is_some_and(|m| m.has_selection), "the selection menu"); + assert!(document_state(&h) == before); +} + #[test] fn brush_preset_menu_action_outside_the_picker_keeps_it_open() { let mut h = harness(Tool::Brush, true, 1.0); let before = document_state(&h); let at = fractional_point(&h, 1.0); tap(&mut h, at, Modifiers::NONE, true); - let picker = h.ctx.memory(|m| m.area_rect(egui::Id::new("canvas-brush-picker"))).unwrap(); + let picker = h.ctx.memory(|m| m.area_rect(egui::Id::unique("canvas-brush-picker"))).unwrap(); let name = h.state().session.tools.presets.first().unwrap().name.clone(); let card = h.query_all_by_label(&name).find(|n| picker.contains(n.rect().center())).expect("visible preset card").rect(); // Open by the right edge so the action lands outside the picker on a foreground menu. diff --git a/crates/ui-egui/src/channel_view.rs b/crates/ui-egui/src/channel_view.rs deleted file mode 100644 index 5cbc960b8e..0000000000 --- a/crates/ui-egui/src/channel_view.rs +++ /dev/null @@ -1,691 +0,0 @@ -//! Channel display on the canvas: alpha / spot / Quick Mask colour overlays, single colour -//! channel views (grayscale) and hidden colour channels. -//! -//! The document composite (GPU or CPU) is untouched. When the Channels panel shows anything other -//! than the plain composite, one extra texture is drawn over it: transparent overlay pixels when -//! the composite stays visible, an opaque picture otherwise. The texture is keyed by document -//! revision and view state and updated per damage rectangle while painting, so nothing is -//! recomposited per frame. - -use egui::{Color32, TextureOptions}; -use photocraft_doc::{AlphaChannel, ColorMode, Document}; -use photocraft_engine::channel_cmds::{ChannelView, color_count}; -use photocraft_engine::mask_view_cmds::MaskViewMode; -use photocraft_geom::Rect; -use photocraft_raster::{Surface, from_rgba, to_rgba}; - -use crate::PhotocraftApp; - -/// Largest overlay texture side; bigger documents show a downsampled overlay. -const MAX_SIDE: u32 = 4096; -/// Limit the sampled float composite to 128 MiB across open documents. -const MAX_COMPOSITE_CACHE_PIXELS: usize = 8 * 1024 * 1024; - -struct SampledComposite { - revision: u64, - /// `revision` minus the view-only part: what the pixels depend on. - content: u64, - preview_key: u64, - factor: u32, - pixels: Vec<[f32; 4]>, -} - -struct CompositeSource<'a> { - cached: Option<&'a [[f32; 4]]>, - fresh: &'a mut Option>, -} - -pub struct Cache { - revision: u64, - key: u64, - /// The canvas document shown by a live stroke (0 is the committed document). - pub(crate) preview_key: u64, - factor: u32, - /// False while the plain composite shows: the texture is kept (so toggling a view back on - /// reuses its allocation) but its pixels are stale. - valid: bool, - tex: egui::TextureHandle, - source: Option, -} - -fn sample(s: &Surface, r: Rect, factor: u32) -> Vec { - let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); - if factor == 1 { - let mut v = Vec::new(); - s.read_region_into(r, &mut v); - let ch = s.channels(); - return if ch > 1 { v.chunks_exact(ch).map(|p| p[0]).collect() } else { v }; - } - let mut out = Vec::with_capacity((w * h) as usize); - let mut row = vec![0.0f32; w as usize]; - for y in 0..h { - s.sample_row_strided(r.y0 + (y * factor) as i32, r.x0, factor as i32, 0, &mut row); - out.extend_from_slice(&row); - } - out -} - -/// Composite RGBA (over white, like Photoshop's channel views) at `factor` stride. -/// Render in bands instead of allocating a full-resolution float composite to sample a preview. -fn composite(doc: &Document, r: Rect, factor: u32) -> Vec<[f32; 4]> { - let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); - let mut out = Vec::with_capacity((w * h) as usize); - let f = factor as i32; - let _ = photocraft_compose::render_bands(doc, r, 0, |band| -> Result<(), ()> { - let first = band.rect.y0 + (r.y0 - band.rect.y0).rem_euclid(f); - for y in (first..band.rect.y1).step_by(factor as usize) { - for x in (r.x0..r.x1).step_by(factor as usize) { - let p = band.get(x, y); - let a = p[3].clamp(0.0, 1.0); - out.push([p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0]); - } - } - Ok(()) - }); - out -} - -/// The channel view of `r` (document pixels, sampled every `factor` px) as premultiplied pixels, -/// or `None` when the plain composite is all there is to show. `in_color` shows single colour -/// channels tinted instead of grayscale (Photoshop's "Show Channels in Color"). -pub fn render(doc: &Document, v: &ChannelView, r: Rect, factor: u32, in_color: bool, active: Option) -> Option> { - let mut unused = None; - render_with_source(doc, v, r, factor, in_color, active, CompositeSource { cached: None, fresh: &mut unused }) -} - -fn render_with_source( - doc: &Document, - v: &ChannelView, - r: Rect, - factor: u32, - in_color: bool, - active: Option, - source: CompositeSource<'_>, -) -> Option> { - if v.is_plain(doc) || r.is_empty() { - return None; - } - let layer_mask = v.shown_layer_mask(doc); - // ⌥-click mask view (#196): the pixel mask alone, in grayscale (white reveals); ⇧⌥ with - // nothing else shown: just the rubylith. Use the compositor's cached feather for the pixel - // mask, but leave vector masks out: this view targets the pixel mask channel by itself. - let feathered_mask = v - .layer_mask - .and_then(|view| doc.layer(view.layer)) - .filter(|layer| layer.mask.as_ref().is_some_and(|mask| mask.enabled && photocraft_compose::masks::feather_sigma(mask.feather) > 0.0)) - .and_then(|layer| { - let mut pixel_mask_layer = layer.clone(); - pixel_mask_layer.vector_mask = None; - photocraft_compose::masks::combined_mask(&pixel_mask_layer, doc.bounds()) - }); - let shown_mask = layer_mask.map(|(m, mode)| (feathered_mask.as_ref().unwrap_or(&m.surface), mode)); - let only_mask = ChannelView { layer_mask: None, ..v.clone() }.is_plain(doc); - match shown_mask { - Some((m, MaskViewMode::Gray)) => return Some(mask_px(m, r, factor, |g| Color32::from_gray(q(g)))), - Some((m, MaskViewMode::Overlay)) if only_mask => { - let c = AlphaChannel::DEFAULT_COLOR.to_rgb(); - return Some(mask_px(m, r, factor, |g| { - let a = 0.5 * (1.0 - g.clamp(0.0, 1.0)); - Color32::from_rgba_premultiplied(q(c[0] * a), q(c[1] * a), q(c[2] * a), q(a)) - })); - } - _ => {} - } - let colors = color_count(doc); - let vis = v.visible_colors(colors); - let (w, h) = (r.width().div_ceil(factor), r.height().div_ceil(factor)); - let n = (w * h) as usize; - let mut overlays: Vec<&AlphaChannel> = (0..doc.channels.len()).filter(|i| v.alpha_shown(*i)).map(|i| &doc.channels[i]).collect(); - let gray = |s: &Surface| sample(s, r, factor).into_iter().map(|g| [g, g, g, 1.0]).collect(); - // Premultiplied RGBA accumulator. With no colour channel left, the channel being edited - // (alpha, Quick Mask, or layer mask) is the picture — grayscale, not a rubylith over nothing. - let mut solo_quick = false; - let mut solo_mask = false; - let mut out: Vec<[f32; 4]> = if vis == 0 { - if !overlays.is_empty() { - let first = overlays.remove(0); - gray(&first.surface) - } else if let Some(q) = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden) { - solo_quick = true; - gray(&q.surface) - } else if let Some((m, _)) = shown_mask { - solo_mask = true; - gray(m) - } else { - let m = active.and_then(|id| doc.layer(id)).and_then(|l| l.mask.as_ref())?; - solo_mask = true; - gray(&m.surface) - } - } else if vis < colors { - let fmt = doc.pixel_format(); - let mode = fmt.mode; - let shown: Vec = (0..colors).filter(|k| v.color_visible(*k)).collect(); - let computed = if source.cached.is_some_and(|px| px.len() == n) { None } else { Some(composite(doc, r, factor)) }; - let pixels = source.cached.filter(|px| px.len() == n).or(computed.as_deref())?; - let converted = pixels - .iter() - .copied() - .map(|rgba| { - let mut native = from_rgba(&fmt, rgba); - if shown.len() == 1 && !in_color { - let x = native[shown[0]]; - // Ink channels read dark where there is ink. - let g = if mode == ColorMode::Cmyk { 1.0 - x } else { x }; - return [g, g, g, 1.0]; - } - for (k, x) in native[..colors].iter_mut().enumerate() { - if !v.color_visible(k) { - // Hidden channels contribute nothing (Lab: neutral a/b, and a neutral - // Lightness under Show Channels in Color, so an isolated a/b channel - // tints over mid gray instead of black, as Photoshop does). - *x = if mode == ColorMode::Lab && (k > 0 || in_color) { 0.5 } else { 0.0 }; - } - } - let c = to_rgba(&fmt, &native); - [c[0], c[1], c[2], 1.0] - }) - .collect(); - if n <= MAX_COMPOSITE_CACHE_PIXELS { - *source.fresh = computed; - } - converted - } else { - vec![[0.0; 4]; n] - }; - // Rubylith: `color` at `opacity` × coverage over the accumulated pixels. - let mut tint = |values: Vec, color: photocraft_color::Color, opacity: f32, coverage: &dyn Fn(f32) -> f32| { - let c = color.to_rgb(); - for (o, val) in out.iter_mut().zip(values) { - let a = (coverage(val.clamp(0.0, 1.0)) * opacity).clamp(0.0, 1.0); - for j in 0..3 { - o[j] = c[j] * a + o[j] * (1.0 - a); - } - o[3] = a + o[3] * (1.0 - a); - } - }; - let quick = doc.quick_mask.as_ref().filter(|_| !v.quick_mask_hidden && !solo_quick); - for ch in overlays.into_iter().chain(quick) { - let (color, opacity) = match ch.spot { - // Spot inks show more solid as solidity rises. - Some((ink, solidity)) => (ink, 0.5 + 0.5 * solidity), - None => (ch.color, ch.opacity), - }; - tint(sample(&ch.surface, r, factor), color, opacity, &|x| ch.overlay_coverage(x)); - } - // ⇧⌥-click mask view (#196): the layer mask as Quick Mask's red over the hidden areas - // (Photoshop's Layer Mask Display Options default). - if !solo_mask && let Some((m, MaskViewMode::Overlay)) = shown_mask { - tint(sample(m, r, factor), AlphaChannel::DEFAULT_COLOR, 0.5, &|x| 1.0 - x); - } - Some(out.into_iter().map(|p| Color32::from_rgba_premultiplied(q(p[0]), q(p[1]), q(p[2]), q(p[3]))).collect()) -} - -/// `r` of a one-channel surface sampled every `factor` px, mapped to colours row by row (in -/// parallel off the web). -fn mask_px(s: &Surface, r: Rect, factor: u32, color: impl Fn(f32) -> Color32 + Sync) -> Vec { - let (w, h) = (r.width().div_ceil(factor) as usize, r.height().div_ceil(factor) as usize); - let mut px = vec![Color32::TRANSPARENT; w * h]; - let row = |y: usize, out: &mut [Color32]| { - let mut vals = vec![0.0f32; out.len()]; - s.sample_row_strided(r.y0 + (y as u32 * factor) as i32, r.x0, factor as i32, 0, &mut vals); - for (o, v) in out.iter_mut().zip(vals) { - *o = color(v); - } - }; - if w == 0 { - return px; - } - #[cfg(not(target_arch = "wasm32"))] - { - use rayon::prelude::*; - px.par_chunks_mut(w).enumerate().for_each(|(y, out)| row(y, out)); - } - #[cfg(target_arch = "wasm32")] - for (y, out) in px.chunks_mut(w).enumerate() { - row(y, out); - } - px -} - -/// A float in 0..=1 as an 8-bit channel value. -fn q(x: f32) -> u8 { - (x.clamp(0.0, 1.0) * 255.0 + 0.5) as u8 -} - -fn view_key(v: &ChannelView, in_color: bool) -> u64 { - let s = serde_json::to_string(v).unwrap_or_default(); - s.bytes().fold(in_color as u64 + 1, |h, b| h.wrapping_mul(1_099_511_628_211).wrapping_add(b as u64)) -} - -/// Make the channel-view texture of document `idx` current; `None` = nothing to draw. -pub fn ensure(app: &mut PhotocraftApp, ctx: &egui::Context, idx: usize) -> Option { - let st = app.session.documents().get(idx)?; - let view = st.channel_view.clone(); - let active = st.active_layer; - let (revision, last_damage) = (st.revision, st.last_damage); - let content = revision.wrapping_sub(st.view_revision); - let id = st.doc.id.0; - if view.is_plain(&st.doc) { - if let Some(c) = app.channel_views.get_mut(&id) { - c.valid = false; - } - return None; - } - // Mask and channel views are drawn over the canvas: show the same live document, - // otherwise an opaque grayscale mask hides the stroke until it is committed (#2078). - // The plain-composite path above needs no preview work. - let (doc, preview_key) = crate::canvas::filter_channel_preview(app, idx, ctx).unwrap_or_else(|| crate::canvas::display_doc(app, idx)); - // Interface ▸ Show Channels in Color tints single colour channel views on the canvas too, - // not just the panel thumbnails (#2385). The view key carries the flag, so toggling it - // rebuilds the texture. - let in_color = app.session.prefs().interface.show_channels_in_color; - let key = view_key(&view, in_color); - // Never exceed what the GPU accepts (egui panics on oversized textures). - let max_side = (ctx.input(|i| i.max_texture_side) as u32).clamp(256, MAX_SIDE); - let factor = doc.size.width.max(doc.size.height).div_ceil(max_side).max(1); - // A live preview can mutate the same Arc in place. Content revision and preview_key, not - // pointer identity, determine whether these flattened pixels are still current. - if let Some(c) = app.channel_views.get_mut(&id) { - if let Some(s) = c.source.as_mut().filter(|s| s.factor == factor && s.content == content && s.preview_key == preview_key) { - s.revision = revision; - } else { - c.source = None; - } - } - if let Some(c) = app.channel_views.get(&id) - && c.key == key - && c.factor == factor - && c.valid - && c.revision == revision - && c.preview_key == preview_key - { - return Some(c.tex.id()); - } - let damage = - app.channel_views.get(&id).and_then(|c| crate::canvas::damage_since(app, idx, (c.revision, c.preview_key), (revision, preview_key), 0, last_damage)); - if let Some(c) = app.channel_views.get_mut(&id) - && c.key == key - && c.factor == factor - && c.valid - && let Some(d) = damage - { - // Re-render just the damage, snapped to the sampling grid so a downsampled texture - // (big documents) updates in place too. - let f = factor as i32; - let d = d.intersect(&doc.bounds()); - let r = Rect::new(d.x0.div_euclid(f) * f, d.y0.div_euclid(f) * f, d.x1, d.y1).intersect(&doc.bounds()); - if !r.is_empty() - && let Some(px) = render(&doc, &view, r, factor, in_color, active) - { - let img = egui::ColorImage::new([r.width().div_ceil(factor) as usize, r.height().div_ceil(factor) as usize], px); - c.tex.set_partial([(r.x0 / f) as usize, (r.y0 / f) as usize], img, TextureOptions::LINEAR); - } - c.revision = revision; - c.preview_key = preview_key; - return Some(c.tex.id()); - } - let b = doc.bounds(); - let cached = app.channel_views.get(&id).and_then(|c| c.source.as_ref()).map(|s| s.pixels.as_slice()); - let mut fresh = None; - let px = render_with_source(&doc, &view, b, factor, in_color, active, CompositeSource { cached, fresh: &mut fresh })?; - let new_source = fresh.is_some(); - let size = [b.width().div_ceil(factor) as usize, b.height().div_ceil(factor) as usize]; - let img = egui::ColorImage::new(size, px); - match app.channel_views.get_mut(&id) { - Some(c) if c.tex.size() == size => { - c.tex.set(img, TextureOptions::LINEAR); - c.revision = revision; - c.preview_key = preview_key; - c.key = key; - c.factor = factor; - c.valid = true; - if let Some(pixels) = fresh.take() { - c.source = Some(SampledComposite { revision, content, preview_key, factor, pixels }); - } - } - _ => { - let tex = ctx.load_texture(format!("channel-view-{id}"), img, TextureOptions::LINEAR); - let source = fresh.map(|pixels| SampledComposite { revision, content, preview_key, factor, pixels }); - app.channel_views.insert(id, Cache { revision, key, preview_key, factor, valid: true, tex, source }); - } - } - if new_source { - for (other_id, c) in &mut app.channel_views { - if *other_id != id { - c.source = None; - } - } - } - app.channel_views.get(&id).map(|c| c.tex.id()) -} - -#[cfg(test)] -mod tests { - use super::*; - use photocraft_engine::Session; - use photocraft_engine::channel_cmds::ChannelTarget; - use serde_json::json; - - fn session() -> Session { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 8, "height": 4})).unwrap(); - s - } - - #[test] - fn composite_bands_keep_the_downsample_grid() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 41, "height": 700})).unwrap(); - s.execute("edit.fill", json!({"color": "#398db2"})).unwrap(); - let doc = &s.active().unwrap().doc; - let rect = doc.bounds(); - let full = photocraft_compose::render(doc, rect); - let full_ref = &full; - for factor in [1, 2, 3, 5] { - let expected: Vec<_> = (0..rect.height()) - .step_by(factor as usize) - .flat_map(|y| { - (0..rect.width()).step_by(factor as usize).map(move |x| { - let p = full_ref.get(rect.x0 + x as i32, rect.y0 + y as i32); - let a = p[3].clamp(0.0, 1.0); - [p[0] * a + 1.0 - a, p[1] * a + 1.0 - a, p[2] * a + 1.0 - a, 1.0] - }) - }) - .collect(); - assert_eq!(composite(doc, rect, factor), expected, "factor {factor}"); - } - } - - #[test] - fn channel_eyes_reuse_composite_until_pixels_change() { - let mut app = PhotocraftApp::new(Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 64, "height": 32})).unwrap(); - app.run("edit.fill", json!({"color": "#4080c0"})).unwrap(); - app.sync_views(); - let ctx = egui::Context::default(); - app.run("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - let id = app.session.active().unwrap().doc.id.0; - let first = app.channel_views.get_mut(&id).unwrap().source.as_mut().unwrap(); - let original_sample = first.pixels[0]; - // Mark the cache: a recomposite overwrites it, while a freed buffer's address can come back. - first.pixels[0] = [0.0, 1.0, 0.0, 1.0]; - - app.run("channel.setVisible", json!({"channel": "green", "visible": false})).unwrap(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - let reused = app.channel_views.get(&id).unwrap().source.as_ref().unwrap(); - assert_eq!(reused.pixels[0], [0.0, 1.0, 0.0, 1.0], "visibility does not recompose the pixels"); - - app.run("edit.fill", json!({"color": "#e02010"})).unwrap(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - // A partial damage upload discards the source; a full redraw may replace it. - // In either case, cached samples must never refer to the pre-edit pixels. - if let Some(source) = app.channel_views.get(&id).unwrap().source.as_ref() { - assert_ne!(source.pixels[0], original_sample, "a full redraw replaces stale pixels"); - } - app.run("channel.setVisible", json!({"channel": "red", "visible": true})).unwrap(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - let refreshed = app.channel_views.get(&id).unwrap().source.as_ref().unwrap(); - let st = app.session.active().unwrap(); - assert_eq!(refreshed.revision, st.revision); - assert_ne!(refreshed.pixels[0], original_sample, "new pixels must be recomposited"); - let expected = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - let mut unused = None; - let actual = render_with_source( - &st.doc, - &st.channel_view, - st.doc.bounds(), - 1, - false, - st.active_layer, - CompositeSource { cached: Some(&refreshed.pixels), fresh: &mut unused }, - ) - .unwrap(); - assert_eq!(actual, expected, "reused pixels must preserve channel colours"); - assert!(unused.is_none(), "reuse must not create a second composite"); - } - - #[test] - fn plain_composite_draws_nothing() { - let s = session(); - let st = s.active().unwrap(); - assert!(render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).is_none()); - } - - #[test] - fn quick_mask_overlay_is_red_over_masked_areas() { - let mut s = session(); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap(); - s.execute("select.editInQuickMaskMode", json!({})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!(px[0], Color32::TRANSPARENT, "selected: no overlay"); - assert_eq!(px[6], Color32::from_rgba_premultiplied(128, 0, 0, 128), "masked: 50% red"); - } - - #[test] - fn quick_mask_without_selection_shows_no_overlay_in_either_mode() { - // #2743: nothing selected is a colourless mask, whichever way the colour reads. - for (before, after) in [(None, Some("selected")), (Some("selected"), None), (Some("selected"), Some("masked"))] { - let mut s = session(); - if let Some(i) = before { - s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap(); - } - s.execute("select.editInQuickMaskMode", json!({})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?}"); - if let Some(i) = after { - s.execute("channel.options", json!({"channel": "quickMask", "indicates": i})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert!(px.iter().all(|p| *p == Color32::TRANSPARENT), "{before:?} → {i}"); - } - } - // Selected Areas with Select All: the whole canvas is coloured. - let mut s = session(); - s.execute("channel.options", json!({"channel": "quickMask", "indicates": "selected"})).unwrap(); - s.execute("select.all", json!({})).unwrap(); - s.execute("select.editInQuickMaskMode", json!({})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert!(px.iter().all(|p| *p == Color32::from_rgba_premultiplied(128, 0, 0, 128))); - } - - #[test] - fn layer_mask_views_and_damage_on_the_downsampled_grid() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 37, "height": 21})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("edit.fill", json!({"color": "#0000ff"})).unwrap(); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [37, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); - s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); - let st = s.active().unwrap(); - let b = st.doc.bounds(); - let full = render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap(); - assert!(full[0].r() < 10 && full[36].r() > 245 && full[0].a() == 255, "opaque grayscale ramp"); - // A sub-rect starting on the factor grid matches the same pixels of the full render. - for f in [1u32, 2, 3] { - let all = render(&st.doc, &st.channel_view, b, f, false, st.active_layer).unwrap(); - let w = b.width().div_ceil(f) as usize; - let r = Rect::new(3 * f as i32, 2 * f as i32, 30, 19); - let part = render(&st.doc, &st.channel_view, r, f, false, st.active_layer).unwrap(); - let pw = r.width().div_ceil(f) as usize; - for (i, p) in part.iter().enumerate() { - assert_eq!(*p, all[(i / pw + 2) * w + i % pw + 3], "factor {f}, pixel {i}"); - } - } - s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap(); - assert!(px[0].r() >= 124 && px[0].g() == 0 && px[0].a() >= 124, "about 50% red over the hidden end: {:?}", px[0]); - assert!(px[36].a() <= 4, "next to nothing over the revealed end: {:?}", px[36]); - } - - #[test] - fn layer_mask_views_show_the_feathered_mask() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 37, "height": 21})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [37, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); - s.execute("layer.layerMask.edit", json!({"feather": 4})).unwrap(); - s.execute("view.layerMask", json!({"mode": "gray"})).unwrap(); - - let st = s.active().unwrap(); - let bounds = st.doc.bounds(); - let layer = st.doc.layer(st.active_layer.unwrap()).unwrap(); - let raw = layer.mask.as_ref().unwrap().surface.sample_channel(0, 0, 0); - let effective = photocraft_compose::masks::combined_mask(layer, bounds).unwrap(); - let softened = effective.sample_channel(0, 0, 0); - assert!((softened - raw).abs() > 0.01, "the fixture has a visible feather at its edge"); - - let gray = render(&st.doc, &st.channel_view, bounds, 1, false, st.active_layer).unwrap(); - assert_eq!(gray[0], Color32::from_gray(q(softened)), "gray view uses the compositor's feathered mask"); - - s.execute("view.layerMask", json!({"mode": "overlay"})).unwrap(); - let st = s.active().unwrap(); - let overlay = render(&st.doc, &st.channel_view, bounds, 1, false, st.active_layer).unwrap(); - let alpha = 0.5 * (1.0 - softened); - let red = AlphaChannel::DEFAULT_COLOR.to_rgb(); - assert_eq!(overlay[0], Color32::from_rgba_premultiplied(q(red[0] * alpha), q(red[1] * alpha), q(red[2] * alpha), q(alpha))); - } - - /// `cargo test --release -p photocraft-ui-egui --lib mask_view_switch_24mp -- --ignored --nocapture` - #[test] - #[ignore = "benchmark"] - fn mask_view_switch_24mp() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 6000, "height": 4000})).unwrap(); - s.execute("layer.new.layer", json!({})).unwrap(); - s.execute("edit.fill", json!({"color": "#d0402b"})).unwrap(); - s.execute("layer.layerMask.revealAll", json!({})).unwrap(); - s.execute("paint.gradient", json!({"from": [0, 0], "to": [6000, 0], "colors": ["#000000", "#ffffff"], "target": "mask"})).unwrap(); - let factor = 6000u32.div_ceil(MAX_SIDE); - for mode in ["gray", "overlay"] { - s.execute("view.layerMask", json!({"mode": mode})).unwrap(); - let st = s.active().unwrap(); - let mut best = f64::MAX; - for _ in 0..8 { - let t = std::time::Instant::now(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), factor, false, st.active_layer).unwrap(); - let img = egui::ColorImage::new([3000, 2000], px); - std::hint::black_box(img); - best = best.min(t.elapsed().as_secs_f64() * 1000.0); - } - println!("24 MP mask view {mode}: {best:.1} ms (render at 1/{factor})"); - } - } - - /// Interface ▸ Show Channels in Color (#2385): a solo RGB / CMYK channel tints the canvas - /// instead of staying gray, like the panel thumbnails wired in #1986. - #[test] - fn show_channels_in_color_tints_the_canvas() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 4, "height": 4, "mode": "rgb"})).unwrap(); - s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); - s.execute("channel.target", json!({"channel": "green"})).unwrap(); - let st = s.active().unwrap(); - let b = st.doc.bounds(); - assert_eq!(render(&st.doc, &st.channel_view, b, 1, false, st.active_layer).unwrap()[0], Color32::from_gray(128)); - assert_eq!(render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0], Color32::from_rgb(0, 128, 0)); - - let mut s = Session::new(); - s.execute("file.new", json!({"width": 4, "height": 4, "mode": "cmyk"})).unwrap(); - s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); - s.execute("channel.target", json!({"channel": "cyan"})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, true, st.active_layer).unwrap()[0]; - assert!(px.r() < 100 && px.g() > 150 && px.b() > 150, "cyan ink, not gray: {px:?}"); - let gray = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap()[0]; - assert_eq!(gray.r(), gray.g(), "off stays grayscale"); - } - - /// Lab: a and b tint over a neutral Lightness; Lightness itself stays gray (Adobe's exception). - #[test] - fn show_channels_in_color_lab_lightness_stays_gray() { - let mut s = Session::new(); - s.execute("file.new", json!({"width": 4, "height": 4, "mode": "lab"})).unwrap(); - s.execute("edit.fill", json!({"color": "#4080c0"})).unwrap(); - let b = s.active().unwrap().doc.bounds(); - s.execute("channel.target", json!({"channel": {"color": 0}})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0]; - let spread = px.r().max(px.g().max(px.b())) as i32 - px.r().min(px.g().min(px.b())) as i32; - assert!(spread <= 3, "Lightness stays gray: {px:?}"); - s.execute("channel.target", json!({"channel": {"color": 1}})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, b, 1, true, st.active_layer).unwrap()[0]; - assert!(px.r() != px.g() || px.g() != px.b(), "a channel is tinted, not gray: {px:?}"); - } - - /// The canvas texture cache is keyed by the preference, so toggling it rebuilds the view. - #[test] - fn ensure_honours_show_channels_in_color() { - let mut app = PhotocraftApp::new(Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 8, "height": 8})).unwrap(); - app.sync_views(); - app.run("channel.target", json!({"channel": "green"})).unwrap(); - let ctx = egui::Context::default(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - let key_off = app.channel_views.values().next().unwrap().key; - app.session.execute("prefs.set", json!({"values": {"interface.showChannelsInColor": true}})).unwrap(); - assert!(ensure(&mut app, &ctx, 0).is_some()); - let key_on = app.channel_views.values().next().unwrap().key; - assert_ne!(key_off, key_on, "the preference changes the cached view key"); - } - - #[test] - fn solo_alpha_is_grayscale_and_colour_channels_split() { - let mut s = session(); - s.execute("edit.fill", json!({"color": "#ff8000"})).unwrap(); - s.execute("select.rect", json!({"x": 0, "y": 0, "width": 2, "height": 4})).unwrap(); - s.execute("channel.new", json!({"fill": "selection"})).unwrap(); - let st = s.active().unwrap(); - assert_eq!(st.channel_view.target, ChannelTarget::Alpha(0)); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!((px[0], px[5]), (Color32::WHITE, Color32::BLACK)); - // Green alone: grayscale of the green channel; with blue hidden only. - s.execute("channel.target", json!({"channel": "green"})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!(px[0], Color32::from_gray(128)); - s.execute("channel.target", json!({"channel": "composite"})).unwrap(); - s.execute("channel.setVisible", json!({"channel": "red", "visible": false})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 2, false, st.active_layer).unwrap(); - assert_eq!(px.len(), 4 * 2); - assert_eq!(px[0], Color32::from_rgb(0, 128, 0)); - } - - #[test] - fn rgb_off_shows_alpha_quick_mask_or_the_layer_mask() { - let mut s = session(); - s.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - s.execute("channel.new", json!({"fill": "white"})).unwrap(); - s.execute("channel.setVisible", json!({"channel": "composite", "visible": true})).unwrap(); - s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); - let st = s.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!(px[0], Color32::WHITE, "Alpha alone is the channel, not the red image"); - - let mut q = session(); - q.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - q.execute("select.editInQuickMaskMode", json!({})).unwrap(); - q.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); - let st = q.active().unwrap(); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!(px[0], Color32::WHITE, "Quick Mask alone is grayscale, not the red composite"); - - let mut m = session(); - m.execute("layer.new.layer", json!({})).unwrap(); - m.execute("edit.fill", json!({"color": "#ff0000"})).unwrap(); - m.execute("layer.layerMask.hideAll", json!({})).unwrap(); - m.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap(); - let st = m.active().unwrap(); - assert_eq!(st.channel_view.visible_colors(3), 0); - let px = render(&st.doc, &st.channel_view, st.doc.bounds(), 1, false, st.active_layer).unwrap(); - assert_eq!(px[0], Color32::BLACK, "the hidden mask reads black, not the red layer"); - } -} diff --git a/crates/ui-egui/src/channels_panel.rs b/crates/ui-egui/src/channels_panel.rs deleted file mode 100644 index 7f175b6209..0000000000 --- a/crates/ui-egui/src/channels_panel.rs +++ /dev/null @@ -1,715 +0,0 @@ -//! The Channels panel (tab of the Layers card): composite, colour, alpha / spot and Quick Mask -//! rows with thumbnails and eye toggles. Every action is an engine command (`channel.*`, -//! `select.loadSelection`, `select.saveSelection`, …); the targeted channel and the eyes live in -//! the engine's `DocState::channel_view`, so agents see the same state through `inspect`. -//! Rows can be dragged onto the footer's buttons, as in Photoshop (#4689): New duplicates the -//! channel, Delete deletes it, and Load Channel as Selection loads it. - -use egui::{Align2, Color32, Rect, RichText, Sense, Stroke, StrokeKind, pos2, vec2}; -use photocraft_doc::ColorIndicates; -use photocraft_engine::channel_cmds::{self, ChannelTarget}; -use serde_json::{Value, json}; - -use crate::theme::{self, Tokens}; -use crate::{PhotocraftApp, icons, widgets}; - -#[derive(Clone, Copy, Debug, PartialEq)] -enum Row { - Composite, - Color(usize), - Alpha(usize), - QuickMask, - /// The selected layer's mask: a temporary channel, listed while the layer is selected. - LayerMask, -} - -impl Row { - /// The channel reference the engine commands take. - fn reference(self) -> Value { - match self { - Row::Composite => json!("composite"), - Row::Color(k) => json!({ "color": k }), - Row::Alpha(i) => json!(i), - Row::QuickMask => json!("quickMask"), - Row::LayerMask => json!("mask"), - } - } -} - -/// ⌘ = new selection, ⌘⇧ add, ⌘⌥ subtract, ⌘⌥⇧ intersect (Photoshop's thumbnail ⌘-clicks). -pub fn load_operation(m: egui::Modifiers) -> &'static str { - match (m.shift, m.alt) { - (true, true) => "intersect", - (true, false) => "add", - (false, true) => "subtract", - _ => "new", - } -} - -pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) { - let t = Tokens::get(ui.ctx()); - let Some(st) = app.session.active() else { - ui.add_space(6.0); - ui.label(RichText::new(tl!("No document")).color(t.text_faint)); - return; - }; - let doc = st.doc.clone(); - let view = st.channel_view.clone(); - // ⌥-click mask view (#196): gray shows the mask alone (colour eyes off), overlay over the composite. - let mask_view = photocraft_engine::mask_view_cmds::current(st).map(|v| v.mode); - let gray_view = mask_view == Some(photocraft_engine::mask_view_cmds::MaskViewMode::Gray); - let active_layer = st.active_layer; - let mode = doc.pixel_format().mode; - let colors = mode.color_channels(); - let quick = doc.quick_mask.is_some(); - let ctx = ui.ctx().clone(); - let thumbs = app.channel_thumbs(&ctx); - // The active layer's mask (Photoshop lists it, in italics, below the colour channels). - let masked = active_layer.and_then(|id| doc.layer(id)).filter(|l| l.mask.is_some()); - let mask_tex = masked.as_ref().and_then(|l| Some(app.mask_thumb(&ctx, &doc, l.id, l.mask.as_ref()?))); - let mask_targeted = masked.is_some() - && (app.ui.mask_target || view.target == ChannelTarget::LayerMask) - && (view.target == ChannelTarget::Composite || view.target == ChannelTarget::LayerMask) - && !quick; - // Multichannel images are their ink channels only (no composite or colour rows). - let multichannel = doc.mode == photocraft_doc::ColorMode::Multichannel; - let mut rows = if multichannel { Vec::new() } else { vec![Row::Composite] }; - if colors > 1 && !multichannel { - rows.extend((0..colors).map(Row::Color)); - } - if masked.is_some() { - rows.push(Row::LayerMask); - } - rows.extend((0..doc.channels.len()).map(Row::Alpha)); - if quick { - rows.push(Row::QuickMask); - } - let shown_colors = if colors > 1 { colors } else { 0 }; - let mut actions: Vec<(String, Value)> = Vec::new(); - // The buttons sit in a footer at the panel's bottom, like Photoshop's. - let footer = widgets::footer_height(ui) + ui.spacing().item_spacing.y; - let fill = ui.available_height() > footer + 60.0; - let rows_h = if fill { ui.available_height() - footer } else { f32::INFINITY }; - let mut mask_click = None; - let mut drawn = Vec::new(); - egui::ScrollArea::vertical().max_height(rows_h).min_scrolled_height(if fill { rows_h } else { 0.0 }).auto_shrink([false, !fill]).show(ui, |ui| { - for row in rows { - let (name, thumb_idx, slot, visible, selected) = match row { - Row::Composite => ( - channel_cmds::composite_name(mode).to_string(), - 0, - Some(2), - view.visible_colors(colors) == colors && !gray_view, - view.target == ChannelTarget::Composite && !quick && !mask_targeted, - ), - Row::Color(k) => ( - channel_cmds::color_names(mode)[k].to_string(), - 1 + k, - Some(3 + k), - view.color_visible(k) && !gray_view, - (view.target == ChannelTarget::Composite && !quick && !mask_targeted) || view.target == ChannelTarget::Color(k), - ), - Row::Alpha(i) => ( - doc.channels[i].name.clone(), - 1 + shown_colors + i, - Some(3 + shown_colors + i).filter(|s| *s <= 9), - view.alpha_shown(i), - view.target == ChannelTarget::Alpha(i), - ), - Row::QuickMask => { - ("Quick Mask".to_string(), 1 + shown_colors + doc.channels.len(), None, !view.quick_mask_hidden, view.target == ChannelTarget::Composite) - } - Row::LayerMask => { - (masked.as_ref().map(|l| format!("{} Mask", l.name)).unwrap_or_default(), usize::MAX, None, mask_view.is_some(), mask_targeted) - } - }; - let row_h = if t.pro { 36.0 } else { 40.0 }; - let (rect, resp) = ui.allocate_exact_size(vec2(ui.available_width(), row_h), Sense::click_and_drag()); - if resp.drag_started() { - ctx.data_mut(|d| d.insert_temp(drag_id(), (row, name.clone()))); - } - let painter = ui.painter_at(rect.expand(1.0)); - if selected { - painter.rect_filled(rect, if t.pro { 0.0 } else { t.radius }, if t.pro { t.row_selected } else { t.hover }); - } else if resp.hovered() { - painter.rect_filled(rect, if t.pro { 0.0 } else { t.radius }, t.hover.gamma_multiply(0.5)); - } - if t.pro { - painter.line_segment([pos2(rect.left() + 30.0, rect.top()), pos2(rect.left() + 30.0, rect.bottom())], Stroke::new(1.0, t.separator)); - painter.line_segment([rect.left_bottom(), rect.right_bottom()], Stroke::new(1.0, t.separator)); - } - let eye = Rect::from_min_size(pos2(rect.left() + 6.0, rect.center().y - 11.0), vec2(22.0, 22.0)); - let eye_resp = ui.interact(eye, ui.id().with(("chan-eye", format!("{:?}", row.reference()))), Sense::click()); - // Like Photoshop, the temporary mask row's eye is empty until the mask is shown. - if row != Row::LayerMask || visible || eye_resp.hovered() { - icons::paint(ui, eye, if visible { "eye" } else { "eye-off" }, 14.0, if visible { t.icon } else { t.text_faint }); - } - let eye_resp = eye_resp.on_hover_text(if row == Row::LayerMask { tl!("Show the layer mask as an overlay") } else { tl!("Toggle visibility") }); - eye_resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Visibility {name}"))); - let layer_id = masked.as_ref().map(|l| l.id.0); - if eye_resp.clicked() { - let view_cmd = photocraft_engine::mask_view_cmds::ID.to_string(); - match row { - // The mask's eye: the overlay on, or the mask view off. - Row::LayerMask => actions.push((view_cmd, json!({ "layer": layer_id, "mode": if visible { "off" } else { "overlay" } }))), - // Showing the composite again over a gray mask view keeps the mask as an overlay. - Row::Composite if gray_view => actions.push((view_cmd, json!({ "layer": layer_id, "mode": "overlay" }))), - _ => actions.push(("channel.setVisible".into(), json!({ "channel": row.reference(), "visible": !visible }))), - } - } - let ts = if t.pro { 28.0 } else { 30.0 }; - let cell = Rect::from_min_size(pos2(rect.left() + 36.0, rect.center().y - ts / 2.0), vec2(ts, ts)); - // Thumbnails keep the document's aspect ratio (the textures are letterboxed squares). - let (thumb, uv) = fit_thumb(cell, doc.size.width, doc.size.height); - let tex = if row == Row::LayerMask { mask_tex } else { thumbs.get(thumb_idx).copied() }; - if let Some(tex) = tex { - painter.image(tex, thumb, uv, Color32::WHITE); - } - drawn.push((name.clone(), thumb)); - // Rows are painted: name them for screen readers and UI tests. - resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, selected, &name)); - // Spot channels: a swatch of the ink on the thumbnail corner. - if let Row::Alpha(i) = row - && let Some((ink, _)) = doc.channels[i].spot - { - let c = ink.to_rgb().map(|v| (v.clamp(0.0, 1.0) * 255.0) as u8); - painter.rect_filled(Rect::from_min_size(thumb.right_bottom() - vec2(9.0, 9.0), vec2(9.0, 9.0)), 0.0, Color32::from_rgb(c[0], c[1], c[2])); - } - painter.rect_stroke(thumb, 0.0, Stroke::new(1.0, if t.pro { Color32::from_gray(20) } else { t.field_border }), StrokeKind::Outside); - let italic = matches!(row, Row::QuickMask | Row::LayerMask) || matches!(row, Row::Alpha(i) if doc.channels[i].spot.is_some()); - let mut job = egui::text::LayoutJob::default(); - let font = if selected && !t.pro { theme::medium(12.5) } else { egui::FontId::proportional(12.0) }; - // Built-in labels only: alpha/spot names and "{layer} Mask" stay verbatim. - let label = match row { - Row::Composite | Row::Color(_) | Row::QuickMask => tl!(&name).to_owned(), - Row::Alpha(_) | Row::LayerMask => name.clone(), - }; - job.append(&label, 0.0, egui::TextFormat { font_id: font, color: t.text, italics: italic, ..Default::default() }); - let galley = painter.layout_job(job); - let text_pos = pos2(cell.right() + 10.0, rect.center().y - galley.size().y / 2.0); - painter.galley(text_pos, galley, t.text); - if let Some(slot) = slot { - painter.text( - pos2(rect.right() - 8.0, rect.center().y), - Align2::RIGHT_CENTER, - crate::shortcuts::pretty(&format!("Cmd+{slot}")), - theme::mono(11.0), - t.text_faint, - ); - } - let rename_id = egui::Id::new(("chan-rename", doc.id.0, format!("{:?}", row.reference()))); - if resp.clicked() && !eye_resp.clicked() { - let m = ui.input(|i| i.modifiers); - if m.command && row == Row::LayerMask { - let layer = masked.as_ref().map(|l| l.id.0); - actions.push(("select.loadSelection".into(), json!({ "channel": "mask", "layer": layer, "operation": load_operation(m) }))); - } else if m.command { - actions.push(("select.loadSelection".into(), json!({ "channel": row.reference(), "operation": load_operation(m) }))); - } else if row == Row::LayerMask { - // Targets the mask, as clicking its thumbnail in the Layers panel does. - mask_click = Some(true); - actions.push(("channel.target".into(), json!({ "channel": "mask" }))); - } else { - if matches!(row, Row::Composite | Row::Color(_)) { - mask_click = Some(false); - } - let target = if row == Row::QuickMask { json!("composite") } else { row.reference() }; - actions.push(("channel.target".into(), json!({ "channel": target }))); - } - } - // Like Photoshop: double-clicking the thumbnail opens Channel Options (where an alpha - // channel becomes a spot channel, #3761), double-clicking the name renames it. - let on_thumb = resp.interact_pointer_pos().is_some_and(|p| p.x < cell.right() + 4.0); - if row == Row::QuickMask && resp.double_clicked() { - actions.push(("ui.channelOptions".into(), json!("quickMask"))); - } - if let Row::Alpha(i) = row { - if resp.double_clicked() && on_thumb { - actions.push(("ui.channelOptions".into(), json!(i))); - } else if resp.double_clicked() { - ctx.data_mut(|d| d.insert_temp(rename_id, name.clone())); - } - if let Some(mut text) = ctx.data(|d| d.get_temp::(rename_id)) { - let edit_rect = Rect::from_min_max(pos2(text_pos.x - 3.0, rect.center().y - 11.0), pos2(rect.right() - 36.0, rect.center().y + 11.0)); - let te = ui.put(edit_rect, egui::TextEdit::singleline(&mut text).font(egui::FontId::proportional(12.5))); - if !te.has_focus() && !te.lost_focus() { - te.request_focus(); - } - let (enter, esc) = ui.input(|i| (i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Escape))); - if esc { - ctx.data_mut(|d| d.remove::(rename_id)); - } else if enter || te.lost_focus() { - ctx.data_mut(|d| d.remove::(rename_id)); - if !text.trim().is_empty() && text != name { - actions.push(("channel.rename".into(), json!({ "channel": i, "name": text.trim() }))); - } - } else { - ctx.data_mut(|d| d.insert_temp(rename_id, text)); - } - } - } - resp.context_menu(|ui| { - crate::widgets::menu_scroll(ui, |ui| { - ui.set_min_width(210.0); - let a = &mut actions; - match row { - Row::Alpha(i) => { - let ch = &doc.channels[i]; - item(ui, a, "Duplicate Channel", "channel.duplicate", json!({ "channel": i })); - item(ui, a, "Delete Channel", "channel.delete", json!({ "channel": i })); - item(ui, a, "Rename Channel…", "ui.renameChannel", json!(i)); - item(ui, a, "Channel Options…", "ui.channelOptions", json!(i)); - ui.separator(); - if ch.spot.is_some() { - item(ui, a, "Merge Spot Channel", "channel.mergeSpot", json!({ "channel": i })); - item(ui, a, "Convert to Alpha Channel", "channel.options", json!({ "channel": i, "indicates": "masked" })); - } else { - indicates_items(ui, a, json!(i), ch.indicates); - overlay_colors(ui, a, json!(i)); - } - } - Row::QuickMask => { - item(ui, a, "Exit Quick Mask", "select.editInQuickMaskMode", json!({ "on": false })); - item(ui, a, "Quick Mask Options…", "ui.channelOptions", json!("quickMask")); - indicates_items(ui, a, json!("quickMask"), doc.quick_mask.as_ref().map(|q| q.indicates).unwrap_or_default()); - overlay_colors(ui, a, json!("quickMask")); - } - Row::Composite | Row::Color(_) => { - item(ui, a, "Duplicate Channel", "channel.duplicate", json!({ "channel": row.reference() })); - } - Row::LayerMask => { - let enabled = masked.as_ref().and_then(|l| l.mask.as_ref()).is_none_or(|m| m.enabled); - item(ui, a, crate::layer_menu_ui::mask_toggle_label(enabled), "layer.layerMask.enabled", json!({})); - item(ui, a, "Delete Layer Mask", "layer.layerMask.delete", json!({})); - } - } - ui.separator(); - item(ui, a, "New Channel…", "channel.new", json!({})); - item(ui, a, "New Spot Channel…", "ui.newSpotChannel", json!({})); - ui.separator(); - item(ui, a, "Split Channels", "channel.split", json!({})); - item(ui, a, "Merge Channels…", "channel.merge", json!({})); - }); - }); - } - }); - // A row being dragged: its name follows the pointer until it is dropped. - let dragged = ctx.data(|d| d.get_temp::<(Row, String)>(drag_id())); - if let Some((_, name)) = &dragged - && let Some(p) = ctx.input(|i| i.pointer.interact_pos()) - { - crate::layer_transfer::ghost(&ctx, p, name); - } - let mask_layer = masked.as_ref().map(|l| l.id.0); - let drop = |ui: &egui::Ui, button: &egui::Response, on: Footer| { - let (row, name) = dragged.as_ref()?; - let action = drop_action(*row, name, mask_layer, on)?; - crate::panels::dropped_on(ui, button).then_some(action) - }; - // Photoshop's order from the left: Load as selection, Save selection, New, Delete. - widgets::panel_footer(ui, |ui| { - let target_ref = match view.target { - ChannelTarget::Alpha(i) => json!(i), - ChannelTarget::Color(k) => json!({ "color": k }), - ChannelTarget::Composite => json!("composite"), - ChannelTarget::LayerMask => json!("mask"), - ChannelTarget::VectorMask => json!("vectorMask"), - }; - let trash = icons::button(ui, "trash", 26.0, false, tl!("Delete current channel")); - if trash.clicked() { - actions.push(("channel.delete".into(), json!({}))); - } - actions.extend(drop(ui, &trash, Footer::Delete)); - let new = icons::button(ui, "plus", 26.0, false, tl!("Create new channel")); - if new.clicked() { - actions.push(("channel.new".into(), json!({}))); - } - actions.extend(drop(ui, &new, Footer::New)); - if icons::button(ui, "square-dashed", 26.0, false, tl!("Save selection as channel")).clicked() { - actions.push(("select.saveSelection".into(), json!({}))); - } - let load = icons::button(ui, "circle-dashed", 26.0, false, tl!("Load channel as selection")); - if load.clicked() { - // The selected mask row can coexist with a Composite engine target while mask - // painting is active. The footer follows the row the user sees as targeted. - let params = - if mask_targeted { json!({ "channel": "mask", "layer": masked.as_ref().map(|layer| layer.id.0) }) } else { json!({ "channel": target_ref }) }; - actions.push(("select.loadSelection".into(), params)); - } - actions.extend(drop(ui, &load, Footer::Load)); - }); - // The drag ends on any release, after the buttons have had a chance to take the drop. - if ctx.input(|i| i.pointer.any_released()) { - ctx.data_mut(|d| d.remove::<(Row, String)>(drag_id())); - } - ctx.data_mut(|d| d.insert_temp(thumbs_id(), drawn)); - for (id, p) in actions { - if id == "ui.renameChannel" { - if let Some(i) = p.as_u64().and_then(|i| doc.channels.get(i as usize)) { - let row = Row::Alpha(p.as_u64().unwrap_or(0) as usize); - ctx.data_mut(|d| d.insert_temp(egui::Id::new(("chan-rename", doc.id.0, format!("{:?}", row.reference()))), i.name.clone())); - } - continue; - } - if id == "ui.newSpotChannel" { - crate::channel_options_ui::open_new_spot(app); - continue; - } - if id == "ui.channelOptions" { - crate::channel_options_ui::open(app, &p); - continue; - } - let _ = app.run(&id, p); - } - if let Some(on) = mask_click { - app.ui.mask_target = on; - app.sync_mask_targets(); - } -} - -/// The footer buttons a row can be dropped on. -#[derive(Clone, Copy)] -enum Footer { - Delete, - New, - Load, -} - -/// What dropping `row` (named `name`) on a footer button does, as in Photoshop; `None` where -/// Photoshop does nothing either (colour channels can't be deleted here). `mask_layer` is the -/// layer whose mask the temporary mask row shows. -fn drop_action(row: Row, name: &str, mask_layer: Option, on: Footer) -> Option<(String, Value)> { - match (on, row) { - (Footer::Delete, Row::Alpha(i)) => Some(("channel.delete".into(), json!({ "channel": i }))), - // Asks "Apply mask to layer before removing?", like dropping the mask's Layers thumbnail. - (Footer::Delete, Row::LayerMask) => Some(crate::mask_trash_prompt::drop_action(mask_layer?, crate::mask_thumbs_ui::MaskKind::Pixel)), - (Footer::Delete, _) => None, - // The engine would name a mask's copy after the composite; Photoshop says "Layer 1 Mask copy". - (Footer::New, Row::LayerMask) => Some(("channel.duplicate".into(), json!({ "channel": "mask", "name": format!("{name} copy") }))), - (Footer::New, _) => Some(("channel.duplicate".into(), json!({ "channel": row.reference() }))), - (Footer::Load, Row::LayerMask) => Some(("select.loadSelection".into(), json!({ "channel": "mask", "layer": mask_layer? }))), - (Footer::Load, _) => Some(("select.loadSelection".into(), json!({ "channel": row.reference() }))), - } -} - -fn drag_id() -> egui::Id { - egui::Id::new("channel-row-drag") -} - -fn thumbs_id() -> egui::Id { - egui::Id::new("channel-thumb-rects") -} - -/// The Channels rows drawn last frame: (name, thumbnail rect). -pub fn recorded(ctx: &egui::Context) -> Vec<(String, Rect)> { - ctx.data(|d| d.get_temp(thumbs_id())).unwrap_or_default() -} - -/// The part of a square `cell` a `w`×`h` document fills, and the matching UVs of a square, -/// letterboxed thumbnail texture: thumbnails keep the document's aspect ratio. -pub fn fit_thumb(cell: Rect, w: u32, h: u32) -> (Rect, Rect) { - let (w, h) = (w.max(1) as f32, h.max(1) as f32); - let (fw, fh) = (w / w.max(h), h / w.max(h)); - let size = vec2(cell.width() * fw, cell.height() * fh).max(vec2(1.0, 1.0)); - let uv = Rect::from_center_size(pos2(0.5, 0.5), vec2(fw, fh)); - (Rect::from_center_size(cell.center(), size), uv) -} - -fn item(ui: &mut egui::Ui, actions: &mut Vec<(String, Value)>, label: &str, cmd: &str, p: Value) { - if ui.button(tl!(&label)).clicked() { - actions.push((cmd.into(), p)); - ui.close(); - } -} - -/// Channel Options › Color Indicates. -fn indicates_items(ui: &mut egui::Ui, actions: &mut Vec<(String, Value)>, channel: Value, current: ColorIndicates) { - let masked = current == ColorIndicates::MaskedAreas; - let check = |on: bool, s: &str| if on { format!("✓ {s}") } else { s.to_string() }; - item(ui, actions, &check(masked, "Color Indicates Masked Areas"), "channel.options", json!({ "channel": channel.clone(), "indicates": "masked" })); - item(ui, actions, &check(!masked, "Color Indicates Selected Areas"), "channel.options", json!({ "channel": channel, "indicates": "selected" })); -} - -/// Overlay colour presets (Channel Options › Color) and opacity. -fn overlay_colors(ui: &mut egui::Ui, actions: &mut Vec<(String, Value)>, channel: Value) { - ui.menu_button(tl!("Overlay Color"), |ui| { - for (label, hex) in [ - (tl!("Red"), "#ff0000"), - (tl!("Green"), "#00c000"), - (tl!("Blue"), "#0050ff"), - (tl!("Cyan"), "#00d0e0"), - (tl!("Magenta"), "#e000c0"), - (tl!("Yellow"), "#f0d000"), - ] { - if ui.button(tl!(&label)).clicked() { - actions.push(("channel.options".into(), json!({ "channel": channel.clone(), "color": hex }))); - ui.close(); - } - } - }); - ui.menu_button(tl!("Overlay Opacity"), |ui| { - for o in [25, 50, 75, 100] { - if ui.button(format!("{o}%")).clicked() { - actions.push(("channel.options".into(), json!({ "channel": channel.clone(), "opacity": o }))); - ui.close(); - } - } - }); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn modifier_operations() { - let m = |shift, alt| egui::Modifiers { command: true, shift, alt, ..Default::default() }; - assert_eq!(load_operation(m(false, false)), "new"); - assert_eq!(load_operation(m(true, false)), "add"); - assert_eq!(load_operation(m(false, true)), "subtract"); - assert_eq!(load_operation(m(true, true)), "intersect"); - } - - #[test] - fn rename_field_interrupts_the_ime_only_when_it_takes_focus() { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 32, "height": 32})).unwrap(); - app.run("channel.new", json!({})).unwrap(); - let ctx = egui::Context::default(); - crate::theme::install_fonts(&ctx); - ctx.run_ui(Default::default(), |_| {}).textures_delta.clear(); - let doc = app.session.active().unwrap().doc.id.0; - let name = app.session.active().unwrap().doc.channels[0].name.clone(); - ctx.data_mut(|d| d.insert_temp(egui::Id::new(("chan-rename", doc, format!("{:?}", Row::Alpha(0).reference()))), name)); - // The field takes focus on the first pass and owns the IME from the second on (#585). - let interrupts: Vec> = (0..4) - .map(|_| { - let mut out = ctx.run_ui(Default::default(), |ui| show(&mut app, ui)); - out.textures_delta.clear(); - out.platform_output.ime.map(|ime| ime.should_interrupt_composition) - }) - .collect(); - assert_eq!(interrupts, [None, Some(false), Some(false), Some(false)]); - } - - #[test] - fn channel_thumbs_honours_show_channels_in_color() { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 16, "height": 16})).unwrap(); - let ctx = egui::Context::default(); - let thumbs_gray = app.channel_thumbs(&ctx); - assert!(!thumbs_gray.is_empty()); - - app.session.execute("prefs.set", json!({"values": {"interface.showChannelsInColor": true}})).unwrap(); - let thumbs_color = app.channel_thumbs(&ctx); - assert_eq!(thumbs_gray.len(), thumbs_color.len()); - } - - /// A selection-only step keeps the thumbnails; a pixel edit rebuilds them into the same - /// textures rather than new ones (#4518). - #[test] - fn channel_thumbs_skip_selection_steps_and_reuse_textures() { - let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); - app.run("file.new", json!({"width": 64, "height": 48})).unwrap(); - let ctx = egui::Context::default(); - // Texture uploads one pass makes (new textures and updates both count). - let pass = |app: &mut PhotocraftApp| { - ctx.begin_pass(Default::default()); - let ids = app.channel_thumbs(&ctx); - let mut delta = ctx.end_pass().textures_delta; - let n = delta.set.len(); - delta.clear(); - (ids, n) - }; - let (first, uploads) = pass(&mut app); - assert!(uploads > 0); - app.run("select.rect", json!({"x": 4, "y": 4, "width": 20, "height": 20})).unwrap(); - assert_eq!(pass(&mut app), (first.clone(), 0), "a marquee changes no pixels"); - app.run("select.deselect", json!({})).unwrap(); - assert_eq!(pass(&mut app), (first.clone(), 0), "neither does Deselect"); - app.run("edit.fill", json!({"color": "#336699"})).unwrap(); - let (after, uploads) = pass(&mut app); - assert!(uploads > 0, "a fill rebuilds them"); - assert_eq!(after, first, "into the same textures"); - } - - /// The Channels tab (the Layers card's second tab) of a 64×48 white document with a layer - /// mask on "Masked" and one alpha channel. - fn channels_harness() -> (egui_kittest::Harness<'static, PhotocraftApp>, u64) { - let mut s = photocraft_engine::Session::new(); - s.execute("file.new", json!({"width": 64, "height": 48})).unwrap(); - let masked = s.execute("layer.new.layer", json!({"name": "Masked"})).unwrap()["layer"].as_u64().unwrap(); - s.execute("layer.layerMask.revealAll", json!({"layer": masked})).unwrap(); - s.execute("channel.new", json!({})).unwrap(); - let mut h = egui_kittest::Harness::builder().with_size(vec2(1440.0, 1000.0)).with_max_steps(64).build_eframe(move |cc| { - PhotocraftApp::setup_context(&cc.egui_ctx, Default::default()); - PhotocraftApp::new(s, crate::Services::default()) - }); - let ctx = h.ctx.clone(); - let (req, _rx) = crate::control::ControlRequest::new( - "ui.set", - json!({"dockWidth": 290.0, "dockTabs": {"layers": 1}, "dock": {"collapsed": ["color", "properties", "history", "navigator"]}}), - ); - crate::control::handle(h.state_mut(), &ctx, &req); - h.run_steps(8); - (h, masked) - } - - /// The centre of the row or button labelled `label` (not its tooltip, which a hover leaves open). - fn at(h: &egui_kittest::Harness<'_, PhotocraftApp>, label: &str) -> egui::Pos2 { - use egui::accesskit::Role; - use egui_kittest::kittest::{NodeT, Queryable}; - h.query_all_by_label(label) - .find(|n| matches!(n.accesskit_node().role(), Role::Button | Role::ListBoxOption | Role::Label) && n.rect().height() > 20.0) - .unwrap_or_else(|| panic!("no row or button {label}")) - .rect() - .center() - } - - /// A real press, drag and release. - fn drag(h: &mut egui_kittest::Harness<'_, PhotocraftApp>, from: egui::Pos2, to: egui::Pos2) { - use egui::{Modifiers, PointerButton}; - h.hover_at(from); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); - h.run_steps(1); - for i in 1..=6 { - h.hover_at(from + (to - from) * (i as f32 / 6.0)); - h.run_steps(1); - } - h.event(egui::Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); - h.run_steps(3); - } - - /// Drags the row or button labelled `from` onto the one labelled `to`. - fn drag_to(h: &mut egui_kittest::Harness<'_, PhotocraftApp>, from: &str, to: &str) { - let (from, to) = (at(h, from), at(h, to)); - drag(h, from, to); - } - - fn alphas(h: &egui_kittest::Harness<'_, PhotocraftApp>) -> Vec { - h.state().session.active().unwrap().doc.channels.iter().map(|c| c.name.clone()).collect() - } - - /// #4689: as in Photoshop, a channel dropped on New is duplicated, on Delete deleted, and on - /// Load Channel as Selection loaded. - #[test] - fn rows_dropped_on_the_footer_duplicate_delete_and_load() { - let (mut h, _) = channels_harness(); - assert_eq!(alphas(&h), ["Alpha 1"]); - let steps = h.state().session.active().unwrap().history.past_len(); - - drag_to(&mut h, "Alpha 1", "Create new channel"); - assert_eq!(alphas(&h), ["Alpha 1", "Alpha 1 copy"]); - assert_eq!(h.state().session.active().unwrap().history.past_len(), steps + 1, "one history step"); - drag_to(&mut h, "Red", "Create new channel"); - assert_eq!(alphas(&h), ["Alpha 1", "Alpha 1 copy", "Red copy"]); - drag_to(&mut h, "Masked Mask", "Create new channel"); - assert_eq!(alphas(&h), ["Alpha 1", "Alpha 1 copy", "Red copy", "Masked Mask copy"]); - - drag_to(&mut h, "Alpha 1 copy", "Delete current channel"); - assert_eq!(alphas(&h), ["Alpha 1", "Red copy", "Masked Mask copy"]); - // Colour channels stay: Photoshop would turn the image into Multichannel. - drag_to(&mut h, "Green", "Delete current channel"); - assert_eq!(alphas(&h), ["Alpha 1", "Red copy", "Masked Mask copy"]); - assert_eq!(h.state().session.active().unwrap().doc.mode, photocraft_doc::ColorMode::Rgb); - - assert!(h.state().session.active().unwrap().doc.selection.is_none()); - drag_to(&mut h, "Red copy", "Load channel as selection"); - assert!(h.state().session.active().unwrap().doc.selection.is_some(), "the white channel selects everything"); - } - - /// The footer follows the targeted row shown by the panel when mask painting is active, - /// even though the engine's channel-view target remains Composite. Other channel targets - /// continue to load their own values, and each load remains one undoable selection change. - #[test] - fn footer_loads_the_selected_layer_mask_and_preserves_color_alpha_targets() { - let (mut h, _) = channels_harness(); - h.state_mut().run("select.rect", json!({"x": 0, "y": 0, "width": 32, "height": 48})).unwrap(); - h.state_mut().run("layer.layerMask.hideAll", json!({})).unwrap(); - h.state_mut().run("layer.layerMask.revealSelection", json!({})).unwrap(); - h.state_mut().run("channel.target", json!({"channel": "composite"})).unwrap(); - h.state_mut().ui.mask_target = true; - h.run_steps(2); - - let click_load = |h: &mut egui_kittest::Harness<'_, PhotocraftApp>| { - use egui::{Modifiers, PointerButton}; - let p = at(h, "Load channel as selection"); - h.hover_at(p); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); - h.run_steps(2); - }; - let sample = - |h: &egui_kittest::Harness<'_, PhotocraftApp>, x, y| h.state().session.active().unwrap().doc.selection.as_ref().unwrap().sample_channel(x, y, 0); - - assert_eq!(h.state().session.active().unwrap().channel_view.target, ChannelTarget::Composite); - click_load(&mut h); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 0.0), "mask row selection wins over the Composite engine target"); - h.state_mut().run("edit.undo", json!({})).unwrap(); - assert!(h.state().session.active().unwrap().doc.selection.is_none()); - h.state_mut().run("edit.redo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 0.0)); - - h.state_mut().ui.mask_target = false; - h.state_mut().run("channel.target", json!({"channel": "composite"})).unwrap(); - click_load(&mut h); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 1.0), "composite luminosity remains available"); - h.state_mut().run("edit.undo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 0.0)); - h.state_mut().run("edit.redo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 1.0)); - - h.state_mut().run("select.rect", json!({"x": 0, "y": 0, "width": 32, "height": 48})).unwrap(); - h.state_mut().run("channel.new", json!({"fill": "selection"})).unwrap(); - h.state_mut().run("select.deselect", json!({})).unwrap(); - h.state_mut().run("channel.target", json!({"channel": "red"})).unwrap(); - click_load(&mut h); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 1.0), "color channel still loads its own plane"); - h.state_mut().run("edit.undo", json!({})).unwrap(); - h.state_mut().run("edit.redo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 1.0)); - - h.state_mut().run("channel.target", json!({"channel": 1})).unwrap(); - click_load(&mut h); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 0.0), "alpha channel still loads its own plane"); - h.state_mut().run("edit.undo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 1.0)); - h.state_mut().run("edit.redo", json!({})).unwrap(); - assert_eq!((sample(&h, 8, 8), sample(&h, 48, 8)), (1.0, 0.0)); - } - - /// A plain click on a row still targets it, and a drag released elsewhere does nothing. - #[test] - fn clicks_still_target_and_a_drop_elsewhere_does_nothing() { - let (mut h, _) = channels_harness(); - let p = at(&h, "Alpha 1"); - h.hover_at(p); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }); - h.run_steps(1); - h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }); - h.run_steps(3); - assert_eq!(h.state().session.active().unwrap().channel_view.target, ChannelTarget::Alpha(0)); - - let steps = h.state().session.active().unwrap().history.past_len(); - let (from, to) = (at(&h, "Alpha 1"), at(&h, "Blue")); - drag(&mut h, from, to); - drag_to(&mut h, "Alpha 1", "Save selection as channel"); - assert_eq!(alphas(&h), ["Alpha 1"]); - assert_eq!(h.state().session.active().unwrap().history.past_len(), steps, "nothing ran"); - assert!(h.ctx.data(|d| d.get_temp::<(Row, String)>(drag_id())).is_none(), "the drag ended"); - } - - /// The layer mask's row asks "Apply mask to layer before removing?", like its Layers thumbnail. - #[test] - fn the_layer_mask_row_on_the_trash_asks_first() { - let (mut h, masked) = channels_harness(); - drag_to(&mut h, "Masked Mask", "Delete current channel"); - assert!(h.state().ui.dialogs.iter().any(|d| crate::mask_trash_prompt::owns(&d.fields)), "the prompt is open"); - let st = h.state().session.active().unwrap(); - assert!(st.doc.layer(photocraft_doc::LayerId(masked)).unwrap().mask.is_some(), "nothing deleted yet"); - } -} diff --git a/crates/ui-egui/src/cjk_fonts.rs b/crates/ui-egui/src/cjk_fonts.rs deleted file mode 100644 index e4c2739fb4..0000000000 --- a/crates/ui-egui/src/cjk_fonts.rs +++ /dev/null @@ -1,940 +0,0 @@ -//! Lazy CJK and other-script fallback fonts for the UI. -//! -//! The bundled Inter / JetBrains Mono have no Japanese, Chinese or Korean glyphs, and the OS -//! fonts that do are large (Hiragino ~10 MB, Apple SD Gothic Neo 28 MB, PingFang 78 MB, Noto -//! Sans CJK ~20 MB). Instead of reading them at startup, an egui plugin scans each frame's text -//! for CJK characters no registered font covers, and registers the next system font for that -//! character's script (`ctx.add_font`, active from the next frame, which it requests). Fonts are -//! appended at the lowest priority to every family, so Latin text keeps Inter. -//! -//! Other scripts the bundled fonts lack (Thai, Arabic, Hebrew, Devanagari and the other Indic -//! scripts, ...: [`photocraft_text::scripts`]) work the same way, each with its own attempt state, -//! independent of the CJK order: the first installed candidate file whose character map really -//! has the missing character is registered, once per script. egui shapes each font run with -//! HarfRust, so Thai marks and Arabic joining come from the font; it has no bidi reordering yet. -//! -//! Script order follows the UI locale ([`photocraft_text::cjk::script_order`]): Kana prefers a -//! Japanese font, Hangul a Korean one, Bopomofo a Traditional Chinese one, and Han the -//! locale's script (Japanese forms only for a Japanese locale). At most one font is read per -//! frame, each file at most once, and once every script has been tried the scan stops. -//! -//! Builds made with the optional craft-fonts input (`CRAFT_FONTS_DIR`, -//! [`photocraft_text::craft_fonts`]) carry Japanese fonts (BIZ UDPGothic first): they are tried -//! before the system Japanese fonts, in the Japanese slot of the same locale order. Without -//! craft-fonts (and on the web, which never embeds them) nothing changes. - -use egui::epaint::text::{FontInsert, FontPriority, InsertFontFamily}; -use egui::{FontData, FontFamily, FontId, Shape}; -use photocraft_text::cjk::{self, CjkChar, CjkScript, FontFile}; -use photocraft_text::craft_fonts::{self, CraftFont}; -use photocraft_text::scripts::{self, Script}; -use std::path::PathBuf; - -/// Skip absurdly large files (a corrupt or non-font path must not eat memory). -pub const MAX_FONT_BYTES: u64 = 128 << 20; -/// At most this many candidate files are read for one script (bounded I/O on a miss). -const MAX_SCRIPT_READS: usize = 6; -/// Name prefix of the registered fallback fonts. -pub const FONT_PREFIX: &str = "system-cjk"; - -/// Where fonts come from; swapped out in tests. -#[derive(Clone)] -pub struct Sources { - pub locale: fn() -> Option, - pub files: fn(CjkScript) -> Vec, - pub last_resort: fn() -> Vec, - /// Candidate files for a non-CJK script, most preferred first; read only when a visible - /// character of that script is missing. - pub scripts: fn(Script) -> Vec, - /// The face of a font file whose character map has the character (`None`: not this file). - pub covers: fn(&[u8], char) -> Option, - /// Embedded fonts tried before `files` for a script (craft-fonts' Japanese fonts). - pub embedded: fn(CjkScript) -> Vec<&'static CraftFont>, -} - -/// craft-fonts' Japanese fonts for the Japanese script, UI face first (empty without craft-fonts). -pub fn craft_embedded(script: CjkScript) -> Vec<&'static CraftFont> { - if script == CjkScript::Japanese { craft_fonts::japanese_for_ui() } else { Vec::new() } -} - -/// No embedded fonts (tests that pin the system-font behaviour). -pub fn no_embedded(_: CjkScript) -> Vec<&'static CraftFont> { - Vec::new() -} - -impl Sources { - #[cfg(not(target_arch = "wasm32"))] - pub fn system() -> Self { - cjk::start_fontconfig_cjk_scan(); - Self { - locale: || match crate::i18n::current().code() { - code @ ("ja" | "ko" | "zh-hans" | "zh-hant") => Some(code.to_string()), - _ => cjk::ui_locale().map(str::to_string), - }, - files: cjk::font_files, - last_resort: cjk::last_resort_files, - scripts: scripts::system_candidates, - covers: scripts::covering_face, - embedded: craft_embedded, - } - } - #[cfg(target_arch = "wasm32")] - pub fn system() -> Self { - Self { - locale: || None, - files: |_| Vec::new(), - last_resort: Vec::new, - scripts: |_| Vec::new(), - covers: scripts::covering_face, - embedded: craft_embedded, - } - } -} - -/// Headless test frame pumps do not wait for `request_repaint_after`, so let the optional -/// Fontconfig worker finish before asserting on installed system fonts. -#[cfg(all(test, target_os = "linux"))] -pub(crate) fn wait_for_fontconfig_cjk_scan() { - cjk::start_fontconfig_cjk_scan(); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10); - while cjk::fontconfig_cjk_scan_pending() && std::time::Instant::now() < deadline { - std::thread::sleep(std::time::Duration::from_millis(10)); - } - assert!(!cjk::fontconfig_cjk_scan_pending(), "Fontconfig CJK lookup did not finish within 10 seconds"); -} - -/// The lazy loader's state: which scripts were tried and which files were read. -pub struct CjkFallback { - sources: Sources, - /// set_fonts takes effect next pass; do not collide with a previous loader's font names. - defer_after_reset: bool, - order: Option<[CjkScript; 4]>, - tried: Vec, - last_resort_tried: bool, - /// Non-CJK scripts whose candidates were tried (each at most once per install). - scripts_tried: Vec